2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
114 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 enum tog_keymap_type {
116 TOG_KEYMAP_KEYS = -2,
132 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
134 #define TOG_EOF_STRING "(END)"
136 struct commit_queue_entry {
137 TAILQ_ENTRY(commit_queue_entry) entry;
138 struct got_object_id *id;
139 struct got_commit_object *commit;
142 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 struct commit_queue {
145 struct commit_queue_head head;
149 STAILQ_ENTRY(tog_color) entry;
153 STAILQ_HEAD(tog_colors, tog_color);
155 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 static struct got_reflist_object_id_map *tog_refs_idmap;
157 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 static const struct got_error *
160 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 struct got_reference* re2)
163 const char *name1 = got_ref_get_name(re1);
164 const char *name2 = got_ref_get_name(re2);
165 int isbackup1, isbackup2;
167 /* Sort backup refs towards the bottom of the list. */
168 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 if (!isbackup1 && isbackup2) {
173 } else if (isbackup1 && !isbackup2) {
178 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
182 static const struct got_error *
183 tog_load_refs(struct got_repository *repo, int sort_by_date)
185 const struct got_error *err;
187 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
193 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
200 if (tog_refs_idmap) {
201 got_reflist_object_id_map_free(tog_refs_idmap);
202 tog_refs_idmap = NULL;
204 got_ref_list_free(&tog_refs);
207 static const struct got_error *
208 add_color(struct tog_colors *colors, const char *pattern,
209 int idx, short color)
211 const struct got_error *err = NULL;
212 struct tog_color *tc;
215 if (idx < 1 || idx > COLOR_PAIRS - 1)
218 init_pair(idx, color, -1);
220 tc = calloc(1, sizeof(*tc));
222 return got_error_from_errno("calloc");
223 regerr = regcomp(&tc->regex, pattern,
224 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
226 static char regerr_msg[512];
227 static char err_msg[512];
228 regerror(regerr, &tc->regex, regerr_msg,
230 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
232 err = got_error_msg(GOT_ERR_REGEX, err_msg);
237 STAILQ_INSERT_HEAD(colors, tc, entry);
242 free_colors(struct tog_colors *colors)
244 struct tog_color *tc;
246 while (!STAILQ_EMPTY(colors)) {
247 tc = STAILQ_FIRST(colors);
248 STAILQ_REMOVE_HEAD(colors, entry);
254 static struct tog_color *
255 get_color(struct tog_colors *colors, int colorpair)
257 struct tog_color *tc = NULL;
259 STAILQ_FOREACH(tc, colors, entry) {
260 if (tc->colorpair == colorpair)
268 default_color_value(const char *envvar)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 return COLOR_MAGENTA;
272 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
274 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
276 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
278 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 return COLOR_MAGENTA;
280 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
284 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
286 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
288 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
290 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
292 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
294 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 return COLOR_MAGENTA;
296 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
298 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
305 get_color_value(const char *envvar)
307 const char *val = getenv(envvar);
310 return default_color_value(envvar);
312 if (strcasecmp(val, "black") == 0)
314 if (strcasecmp(val, "red") == 0)
316 if (strcasecmp(val, "green") == 0)
318 if (strcasecmp(val, "yellow") == 0)
320 if (strcasecmp(val, "blue") == 0)
322 if (strcasecmp(val, "magenta") == 0)
323 return COLOR_MAGENTA;
324 if (strcasecmp(val, "cyan") == 0)
326 if (strcasecmp(val, "white") == 0)
328 if (strcasecmp(val, "default") == 0)
331 return default_color_value(envvar);
334 struct tog_diff_view_state {
335 struct got_object_id *id1, *id2;
336 const char *label1, *label2;
340 int first_displayed_line;
341 int last_displayed_line;
344 int ignore_whitespace;
346 struct got_repository *repo;
347 struct got_diff_line *lines;
352 /* passed from log or blame view; may be NULL */
353 struct tog_view *parent_view;
356 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 static volatile sig_atomic_t tog_thread_error;
359 struct tog_log_thread_args {
360 pthread_cond_t need_commits;
361 pthread_cond_t commit_loaded;
364 struct got_commit_graph *graph;
365 struct commit_queue *real_commits;
366 const char *in_repo_path;
367 struct got_object_id *start_id;
368 struct got_repository *repo;
372 struct commit_queue_entry **first_displayed_entry;
373 struct commit_queue_entry **selected_entry;
375 int *search_next_done;
379 regex_t *limit_regex;
380 struct commit_queue *limit_commits;
383 struct tog_log_view_state {
384 struct commit_queue *commits;
385 struct commit_queue_entry *first_displayed_entry;
386 struct commit_queue_entry *last_displayed_entry;
387 struct commit_queue_entry *selected_entry;
388 struct commit_queue real_commits;
393 struct got_repository *repo;
394 struct got_object_id *start_id;
397 struct tog_log_thread_args thread_args;
398 struct commit_queue_entry *matched_entry;
399 struct commit_queue_entry *search_entry;
400 struct tog_colors colors;
404 struct commit_queue limit_commits;
407 #define TOG_COLOR_DIFF_MINUS 1
408 #define TOG_COLOR_DIFF_PLUS 2
409 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 #define TOG_COLOR_DIFF_META 4
411 #define TOG_COLOR_TREE_SUBMODULE 5
412 #define TOG_COLOR_TREE_SYMLINK 6
413 #define TOG_COLOR_TREE_DIRECTORY 7
414 #define TOG_COLOR_TREE_EXECUTABLE 8
415 #define TOG_COLOR_COMMIT 9
416 #define TOG_COLOR_AUTHOR 10
417 #define TOG_COLOR_DATE 11
418 #define TOG_COLOR_REFS_HEADS 12
419 #define TOG_COLOR_REFS_TAGS 13
420 #define TOG_COLOR_REFS_REMOTES 14
421 #define TOG_COLOR_REFS_BACKUP 15
423 struct tog_blame_cb_args {
424 struct tog_blame_line *lines; /* one per line */
427 struct tog_view *view;
428 struct got_object_id *commit_id;
432 struct tog_blame_thread_args {
434 struct got_repository *repo;
435 struct tog_blame_cb_args *cb_args;
437 got_cancel_cb cancel_cb;
444 struct tog_blame_line *lines;
448 struct tog_blame_thread_args thread_args;
449 struct tog_blame_cb_args cb_args;
454 struct tog_blame_view_state {
455 int first_displayed_line;
456 int last_displayed_line;
458 int last_diffed_line;
462 struct got_object_id_queue blamed_commits;
463 struct got_object_qid *blamed_commit;
465 struct got_repository *repo;
466 struct got_object_id *commit_id;
467 struct got_object_id *id_to_log;
468 struct tog_blame blame;
470 struct tog_colors colors;
473 struct tog_parent_tree {
474 TAILQ_ENTRY(tog_parent_tree) entry;
475 struct got_tree_object *tree;
476 struct got_tree_entry *first_displayed_entry;
477 struct got_tree_entry *selected_entry;
481 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 struct tog_tree_view_state {
485 struct got_object_id *commit_id;/* commit which this tree belongs to */
486 struct got_tree_object *root; /* the commit's root tree entry */
487 struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 struct got_tree_entry *first_displayed_entry;
489 struct got_tree_entry *last_displayed_entry;
490 struct got_tree_entry *selected_entry;
491 int ndisplayed, selected, show_ids;
492 struct tog_parent_trees parents; /* parent trees of current sub-tree */
494 struct got_repository *repo;
495 struct got_tree_entry *matched_entry;
496 struct tog_colors colors;
499 struct tog_reflist_entry {
500 TAILQ_ENTRY(tog_reflist_entry) entry;
501 struct got_reference *ref;
505 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 struct tog_ref_view_state {
508 struct tog_reflist_head refs;
509 struct tog_reflist_entry *first_displayed_entry;
510 struct tog_reflist_entry *last_displayed_entry;
511 struct tog_reflist_entry *selected_entry;
512 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 struct got_repository *repo;
514 struct tog_reflist_entry *matched_entry;
515 struct tog_colors colors;
518 struct tog_help_view_state {
523 int first_displayed_line;
524 int last_displayed_line;
529 enum tog_keymap_type type;
532 #define GENERATE_HELP \
533 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 KEY_("k C-p Up", "Move cursor or page up one line"), \
536 KEY_("j C-n Down", "Move cursor or page down one line"), \
537 KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 KEY_("C-u u", "Scroll the view up one half page"), \
540 KEY_("C-d d", "Scroll the view down one half page"), \
541 KEY_("g Home", "Go to line N (default: first line)"), \
542 KEY_("G End", "Go to line N (default: last line)"), \
543 KEY_("l Right", "Scroll the view right"), \
544 KEY_("h Left", "Scroll the view left"), \
545 KEY_("$", "Scroll view to the rightmost position"), \
546 KEY_("0", "Scroll view to the leftmost position"), \
547 KEY_("-", "Decrease size of the focussed split"), \
548 KEY_("+", "Increase size of the focussed split"), \
549 KEY_("Tab", "Switch focus between views"), \
550 KEY_("F", "Toggle fullscreen mode"), \
551 KEY_("/", "Open prompt to enter search term"), \
552 KEY_("n", "Find next line/token matching the current search term"), \
553 KEY_("N", "Find previous line/token matching the current search term"),\
554 KEY_("q", "Quit the focussed view; Quit help screen"), \
555 KEY_("Q", "Quit tog"), \
557 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
558 KEY_("< ,", "Move cursor up one commit"), \
559 KEY_("> .", "Move cursor down one commit"), \
560 KEY_("Enter", "Open diff view of the selected commit"), \
561 KEY_("B", "Reload the log view and toggle display of merged commits"), \
562 KEY_("R", "Open ref view of all repository references"), \
563 KEY_("T", "Display tree view of the repository from the selected" \
565 KEY_("@", "Toggle between displaying author and committer name"), \
566 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
567 KEY_("C-g Backspace", "Cancel current search or log operation"), \
568 KEY_("C-l", "Reload the log view with new commits in the repository"), \
570 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
571 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
572 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
573 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
574 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
576 KEY_("(", "Go to the previous file in the diff"), \
577 KEY_(")", "Go to the next file in the diff"), \
578 KEY_("{", "Go to the previous hunk in the diff"), \
579 KEY_("}", "Go to the next hunk in the diff"), \
580 KEY_("[", "Decrease the number of context lines"), \
581 KEY_("]", "Increase the number of context lines"), \
582 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
584 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
585 KEY_("Enter", "Display diff view of the selected line's commit"), \
586 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
587 KEY_("L", "Open log view for the currently selected annotated line"), \
588 KEY_("C", "Reload view with the previously blamed commit"), \
589 KEY_("c", "Reload view with the version of the file found in the" \
590 " selected line's commit"), \
591 KEY_("p", "Reload view with the version of the file found in the" \
592 " selected line's parent commit"), \
594 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
595 KEY_("Enter", "Enter selected directory or open blame view of the" \
597 KEY_("L", "Open log view for the selected entry"), \
598 KEY_("R", "Open ref view of all repository references"), \
599 KEY_("i", "Show object IDs for all tree entries"), \
600 KEY_("Backspace", "Return to the parent directory"), \
602 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
603 KEY_("Enter", "Display log view of the selected reference"), \
604 KEY_("T", "Display tree view of the selected reference"), \
605 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
606 KEY_("m", "Toggle display of last modified date for each reference"), \
607 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
608 KEY_("C-l", "Reload view with all repository references")
613 enum tog_keymap_type type;
617 * We implement two types of views: parent views and child views.
619 * The 'Tab' key switches focus between a parent view and its child view.
620 * Child views are shown side-by-side to their parent view, provided
621 * there is enough screen estate.
623 * When a new view is opened from within a parent view, this new view
624 * becomes a child view of the parent view, replacing any existing child.
626 * When a new view is opened from within a child view, this new view
627 * becomes a parent view which will obscure the views below until the
628 * user quits the new parent view by typing 'q'.
630 * This list of views contains parent views only.
631 * Child views are only pointed to by their parent view.
633 TAILQ_HEAD(tog_view_list_head, tog_view);
636 TAILQ_ENTRY(tog_view) entry;
639 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
640 int resized_y, resized_x; /* begin_y/x based on user resizing */
641 int maxx, x; /* max column and current start column */
642 int lines, cols; /* copies of LINES and COLS */
643 int nscrolled, offset; /* lines scrolled and hsplit line offset */
644 int gline, hiline; /* navigate to and highlight this nG line */
645 int ch, count; /* current keymap and count prefix */
646 int resized; /* set when in a resize event */
647 int focussed; /* Only set on one parent or child view at a time. */
649 struct tog_view *parent;
650 struct tog_view *child;
653 * This flag is initially set on parent views when a new child view
654 * is created. It gets toggled when the 'Tab' key switches focus
655 * between parent and child.
656 * The flag indicates whether focus should be passed on to our child
657 * view if this parent view gets picked for focus after another parent
658 * view was closed. This prevents child views from losing focus in such
663 enum tog_view_mode mode;
664 /* type-specific state */
665 enum tog_view_type type;
667 struct tog_diff_view_state diff;
668 struct tog_log_view_state log;
669 struct tog_blame_view_state blame;
670 struct tog_tree_view_state tree;
671 struct tog_ref_view_state ref;
672 struct tog_help_view_state help;
675 const struct got_error *(*show)(struct tog_view *);
676 const struct got_error *(*input)(struct tog_view **,
677 struct tog_view *, int);
678 const struct got_error *(*reset)(struct tog_view *);
679 const struct got_error *(*resize)(struct tog_view *, int);
680 const struct got_error *(*close)(struct tog_view *);
682 const struct got_error *(*search_start)(struct tog_view *);
683 const struct got_error *(*search_next)(struct tog_view *);
684 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
685 int **, int **, int **, int **);
688 #define TOG_SEARCH_FORWARD 1
689 #define TOG_SEARCH_BACKWARD 2
690 int search_next_done;
691 #define TOG_SEARCH_HAVE_MORE 1
692 #define TOG_SEARCH_NO_MORE 2
693 #define TOG_SEARCH_HAVE_NONE 3
698 static const struct got_error *open_diff_view(struct tog_view *,
699 struct got_object_id *, struct got_object_id *,
700 const char *, const char *, int, int, int, struct tog_view *,
701 struct got_repository *);
702 static const struct got_error *show_diff_view(struct tog_view *);
703 static const struct got_error *input_diff_view(struct tog_view **,
704 struct tog_view *, int);
705 static const struct got_error *reset_diff_view(struct tog_view *);
706 static const struct got_error* close_diff_view(struct tog_view *);
707 static const struct got_error *search_start_diff_view(struct tog_view *);
708 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
709 size_t *, int **, int **, int **, int **);
710 static const struct got_error *search_next_view_match(struct tog_view *);
712 static const struct got_error *open_log_view(struct tog_view *,
713 struct got_object_id *, struct got_repository *,
714 const char *, const char *, int);
715 static const struct got_error * show_log_view(struct tog_view *);
716 static const struct got_error *input_log_view(struct tog_view **,
717 struct tog_view *, int);
718 static const struct got_error *resize_log_view(struct tog_view *, int);
719 static const struct got_error *close_log_view(struct tog_view *);
720 static const struct got_error *search_start_log_view(struct tog_view *);
721 static const struct got_error *search_next_log_view(struct tog_view *);
723 static const struct got_error *open_blame_view(struct tog_view *, char *,
724 struct got_object_id *, struct got_repository *);
725 static const struct got_error *show_blame_view(struct tog_view *);
726 static const struct got_error *input_blame_view(struct tog_view **,
727 struct tog_view *, int);
728 static const struct got_error *reset_blame_view(struct tog_view *);
729 static const struct got_error *close_blame_view(struct tog_view *);
730 static const struct got_error *search_start_blame_view(struct tog_view *);
731 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
732 size_t *, int **, int **, int **, int **);
734 static const struct got_error *open_tree_view(struct tog_view *,
735 struct got_object_id *, const char *, struct got_repository *);
736 static const struct got_error *show_tree_view(struct tog_view *);
737 static const struct got_error *input_tree_view(struct tog_view **,
738 struct tog_view *, int);
739 static const struct got_error *close_tree_view(struct tog_view *);
740 static const struct got_error *search_start_tree_view(struct tog_view *);
741 static const struct got_error *search_next_tree_view(struct tog_view *);
743 static const struct got_error *open_ref_view(struct tog_view *,
744 struct got_repository *);
745 static const struct got_error *show_ref_view(struct tog_view *);
746 static const struct got_error *input_ref_view(struct tog_view **,
747 struct tog_view *, int);
748 static const struct got_error *close_ref_view(struct tog_view *);
749 static const struct got_error *search_start_ref_view(struct tog_view *);
750 static const struct got_error *search_next_ref_view(struct tog_view *);
752 static const struct got_error *open_help_view(struct tog_view *,
754 static const struct got_error *show_help_view(struct tog_view *);
755 static const struct got_error *input_help_view(struct tog_view **,
756 struct tog_view *, int);
757 static const struct got_error *reset_help_view(struct tog_view *);
758 static const struct got_error* close_help_view(struct tog_view *);
759 static const struct got_error *search_start_help_view(struct tog_view *);
760 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
761 size_t *, int **, int **, int **, int **);
763 static volatile sig_atomic_t tog_sigwinch_received;
764 static volatile sig_atomic_t tog_sigpipe_received;
765 static volatile sig_atomic_t tog_sigcont_received;
766 static volatile sig_atomic_t tog_sigint_received;
767 static volatile sig_atomic_t tog_sigterm_received;
770 tog_sigwinch(int signo)
772 tog_sigwinch_received = 1;
776 tog_sigpipe(int signo)
778 tog_sigpipe_received = 1;
782 tog_sigcont(int signo)
784 tog_sigcont_received = 1;
788 tog_sigint(int signo)
790 tog_sigint_received = 1;
794 tog_sigterm(int signo)
796 tog_sigterm_received = 1;
800 tog_fatal_signal_received(void)
802 return (tog_sigpipe_received ||
803 tog_sigint_received || tog_sigterm_received);
806 static const struct got_error *
807 view_close(struct tog_view *view)
809 const struct got_error *err = NULL, *child_err = NULL;
812 child_err = view_close(view->child);
816 err = view->close(view);
818 del_panel(view->panel);
820 delwin(view->window);
822 return err ? err : child_err;
825 static struct tog_view *
826 view_open(int nlines, int ncols, int begin_y, int begin_x,
827 enum tog_view_type type)
829 struct tog_view *view = calloc(1, sizeof(*view));
837 view->nlines = nlines ? nlines : LINES - begin_y;
838 view->ncols = ncols ? ncols : COLS - begin_x;
839 view->begin_y = begin_y;
840 view->begin_x = begin_x;
841 view->window = newwin(nlines, ncols, begin_y, begin_x);
842 if (view->window == NULL) {
846 view->panel = new_panel(view->window);
847 if (view->panel == NULL ||
848 set_panel_userptr(view->panel, view) != OK) {
853 keypad(view->window, TRUE);
858 view_split_begin_x(int begin_x)
860 if (begin_x > 0 || COLS < 120)
862 return (COLS - MAX(COLS / 2, 80));
865 /* XXX Stub till we decide what to do. */
867 view_split_begin_y(int lines)
869 return lines * HSPLIT_SCALE;
872 static const struct got_error *view_resize(struct tog_view *);
874 static const struct got_error *
875 view_splitscreen(struct tog_view *view)
877 const struct got_error *err = NULL;
879 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
880 if (view->resized_y && view->resized_y < view->lines)
881 view->begin_y = view->resized_y;
883 view->begin_y = view_split_begin_y(view->nlines);
885 } else if (!view->resized) {
886 if (view->resized_x && view->resized_x < view->cols - 1 &&
888 view->begin_x = view->resized_x;
890 view->begin_x = view_split_begin_x(0);
893 view->nlines = LINES - view->begin_y;
894 view->ncols = COLS - view->begin_x;
897 err = view_resize(view);
901 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
902 view->parent->nlines = view->begin_y;
904 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
905 return got_error_from_errno("mvwin");
910 static const struct got_error *
911 view_fullscreen(struct tog_view *view)
913 const struct got_error *err = NULL;
916 view->begin_y = view->resized ? view->begin_y : 0;
917 view->nlines = view->resized ? view->nlines : LINES;
921 err = view_resize(view);
925 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
926 return got_error_from_errno("mvwin");
932 view_is_parent_view(struct tog_view *view)
934 return view->parent == NULL;
938 view_is_splitscreen(struct tog_view *view)
940 return view->begin_x > 0 || view->begin_y > 0;
944 view_is_fullscreen(struct tog_view *view)
946 return view->nlines == LINES && view->ncols == COLS;
950 view_is_hsplit_top(struct tog_view *view)
952 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
953 view_is_splitscreen(view->child);
957 view_border(struct tog_view *view)
960 const struct tog_view *view_above;
963 return view_border(view->parent);
965 panel = panel_above(view->panel);
969 view_above = panel_userptr(panel);
970 if (view->mode == TOG_VIEW_SPLIT_HRZN)
971 mvwhline(view->window, view_above->begin_y - 1,
972 view->begin_x, got_locale_is_utf8() ?
973 ACS_HLINE : '-', view->ncols);
975 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
976 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
979 static const struct got_error *view_init_hsplit(struct tog_view *, int);
980 static const struct got_error *request_log_commits(struct tog_view *);
981 static const struct got_error *offset_selection_down(struct tog_view *);
982 static void offset_selection_up(struct tog_view *);
983 static void view_get_split(struct tog_view *, int *, int *);
985 static const struct got_error *
986 view_resize(struct tog_view *view)
988 const struct got_error *err = NULL;
989 int dif, nlines, ncols;
991 dif = LINES - view->lines; /* line difference */
993 if (view->lines > LINES)
994 nlines = view->nlines - (view->lines - LINES);
996 nlines = view->nlines + (LINES - view->lines);
997 if (view->cols > COLS)
998 ncols = view->ncols - (view->cols - COLS);
1000 ncols = view->ncols + (COLS - view->cols);
1003 int hs = view->child->begin_y;
1005 if (!view_is_fullscreen(view))
1006 view->child->begin_x = view_split_begin_x(view->begin_x);
1007 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1008 view->child->begin_x == 0) {
1011 view_fullscreen(view->child);
1012 if (view->child->focussed)
1013 show_panel(view->child->panel);
1015 show_panel(view->panel);
1017 ncols = view->child->begin_x;
1019 view_splitscreen(view->child);
1020 show_panel(view->child->panel);
1023 * XXX This is ugly and needs to be moved into the above
1024 * logic but "works" for now and my attempts at moving it
1025 * break either 'tab' or 'F' key maps in horizontal splits.
1028 err = view_splitscreen(view->child);
1031 if (dif < 0) { /* top split decreased */
1032 err = offset_selection_down(view);
1039 show_panel(view->child->panel);
1040 nlines = view->nlines;
1042 } else if (view->parent == NULL)
1045 if (view->resize && dif > 0) {
1046 err = view->resize(view, dif);
1051 if (wresize(view->window, nlines, ncols) == ERR)
1052 return got_error_from_errno("wresize");
1053 if (replace_panel(view->panel, view->window) == ERR)
1054 return got_error_from_errno("replace_panel");
1055 wclear(view->window);
1057 view->nlines = nlines;
1058 view->ncols = ncols;
1059 view->lines = LINES;
1065 static const struct got_error *
1066 resize_log_view(struct tog_view *view, int increase)
1068 struct tog_log_view_state *s = &view->state.log;
1069 const struct got_error *err = NULL;
1072 if (s->selected_entry)
1073 n = s->selected_entry->idx + view->lines - s->selected;
1076 * Request commits to account for the increased
1077 * height so we have enough to populate the view.
1079 if (s->commits->ncommits < n) {
1080 view->nscrolled = n - s->commits->ncommits + increase + 1;
1081 err = request_log_commits(view);
1088 view_adjust_offset(struct tog_view *view, int n)
1093 if (view->parent && view->parent->offset) {
1094 if (view->parent->offset + n >= 0)
1095 view->parent->offset += n;
1097 view->parent->offset = 0;
1098 } else if (view->offset) {
1099 if (view->offset - n >= 0)
1106 static const struct got_error *
1107 view_resize_split(struct tog_view *view, int resize)
1109 const struct got_error *err = NULL;
1110 struct tog_view *v = NULL;
1117 if (!v->child || !view_is_splitscreen(v->child))
1120 v->resized = v->child->resized = resize; /* lock for resize event */
1122 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1123 if (v->child->resized_y)
1124 v->child->begin_y = v->child->resized_y;
1126 v->child->begin_y -= resize;
1128 v->child->begin_y += resize;
1129 if (v->child->begin_y < 3) {
1131 v->child->begin_y = 3;
1132 } else if (v->child->begin_y > LINES - 1) {
1134 v->child->begin_y = LINES - 1;
1137 v->child->ncols = COLS;
1138 view_adjust_offset(view, resize);
1139 err = view_init_hsplit(v, v->child->begin_y);
1142 v->child->resized_y = v->child->begin_y;
1144 if (v->child->resized_x)
1145 v->child->begin_x = v->child->resized_x;
1147 v->child->begin_x -= resize;
1149 v->child->begin_x += resize;
1150 if (v->child->begin_x < 11) {
1152 v->child->begin_x = 11;
1153 } else if (v->child->begin_x > COLS - 1) {
1155 v->child->begin_x = COLS - 1;
1157 v->child->resized_x = v->child->begin_x;
1160 v->child->mode = v->mode;
1161 v->child->nlines = v->lines - v->child->begin_y;
1162 v->child->ncols = v->cols - v->child->begin_x;
1165 err = view_fullscreen(v);
1168 err = view_splitscreen(v->child);
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1173 err = offset_selection_down(v->child);
1179 err = v->resize(v, 0);
1180 else if (v->child->resize)
1181 err = v->child->resize(v->child, 0);
1183 v->resized = v->child->resized = 0;
1189 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1191 struct tog_view *v = src->child ? src->child : src;
1193 dst->resized_x = v->resized_x;
1194 dst->resized_y = v->resized_y;
1197 static const struct got_error *
1198 view_close_child(struct tog_view *view)
1200 const struct got_error *err = NULL;
1202 if (view->child == NULL)
1205 err = view_close(view->child);
1210 static const struct got_error *
1211 view_set_child(struct tog_view *view, struct tog_view *child)
1213 const struct got_error *err = NULL;
1215 view->child = child;
1216 child->parent = view;
1218 err = view_resize(view);
1222 if (view->child->resized_x || view->child->resized_y)
1223 err = view_resize_split(view, 0);
1228 static const struct got_error *view_dispatch_request(struct tog_view **,
1229 struct tog_view *, enum tog_view_type, int, int);
1231 static const struct got_error *
1232 view_request_new(struct tog_view **requested, struct tog_view *view,
1233 enum tog_view_type request)
1235 struct tog_view *new_view = NULL;
1236 const struct got_error *err;
1241 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1242 view_get_split(view, &y, &x);
1244 err = view_dispatch_request(&new_view, view, request, y, x);
1248 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1249 request != TOG_VIEW_HELP) {
1250 err = view_init_hsplit(view, y);
1256 new_view->focussed = 1;
1257 new_view->mode = view->mode;
1258 new_view->nlines = request == TOG_VIEW_HELP ?
1259 view->lines : view->lines - y;
1261 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1262 view_transfer_size(new_view, view);
1263 err = view_close_child(view);
1266 err = view_set_child(view, new_view);
1269 view->focus_child = 1;
1271 *requested = new_view;
1277 tog_resizeterm(void)
1280 struct winsize size;
1282 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1283 cols = 80; /* Default */
1287 lines = size.ws_row;
1289 resize_term(lines, cols);
1292 static const struct got_error *
1293 view_search_start(struct tog_view *view)
1295 const struct got_error *err = NULL;
1296 struct tog_view *v = view;
1300 if (view->search_started) {
1301 regfree(&view->regex);
1302 view->searching = 0;
1303 memset(&view->regmatch, 0, sizeof(view->regmatch));
1305 view->search_started = 0;
1307 if (view->nlines < 1)
1310 if (view_is_hsplit_top(view))
1313 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1314 wclrtoeol(v->window);
1316 nodelay(view->window, FALSE); /* block for search term input */
1319 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1320 wrefresh(v->window);
1323 nodelay(view->window, TRUE);
1327 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1328 err = view->search_start(view);
1330 regfree(&view->regex);
1333 view->search_started = 1;
1334 view->searching = TOG_SEARCH_FORWARD;
1335 view->search_next_done = 0;
1336 view->search_next(view);
1342 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1343 static const struct got_error *
1344 switch_split(struct tog_view *view)
1346 const struct got_error *err = NULL;
1347 struct tog_view *v = NULL;
1354 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1355 v->mode = TOG_VIEW_SPLIT_VERT;
1357 v->mode = TOG_VIEW_SPLIT_HRZN;
1361 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1362 v->mode = TOG_VIEW_SPLIT_NONE;
1364 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1365 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1366 v->child->begin_y = v->child->resized_y;
1367 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1368 v->child->begin_x = v->child->resized_x;
1371 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1373 v->child->ncols = COLS;
1374 v->child->nscrolled = LINES - v->child->nlines;
1376 err = view_init_hsplit(v, v->child->begin_y);
1380 v->child->mode = v->mode;
1381 v->child->nlines = v->lines - v->child->begin_y;
1384 err = view_fullscreen(v);
1387 err = view_splitscreen(v->child);
1391 if (v->mode == TOG_VIEW_SPLIT_NONE)
1392 v->mode = TOG_VIEW_SPLIT_VERT;
1393 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1394 err = offset_selection_down(v);
1397 err = offset_selection_down(v->child);
1401 offset_selection_up(v);
1402 offset_selection_up(v->child);
1405 err = v->resize(v, 0);
1406 else if (v->child->resize)
1407 err = v->child->resize(v->child, 0);
1413 * Compute view->count from numeric input. Assign total to view->count and
1414 * return first non-numeric key entered.
1417 get_compound_key(struct tog_view *view, int c)
1419 struct tog_view *v = view;
1422 if (view_is_hsplit_top(view))
1424 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1428 cbreak(); /* block for input */
1429 nodelay(view->window, FALSE);
1430 wmove(v->window, v->nlines - 1, 0);
1431 wclrtoeol(v->window);
1432 waddch(v->window, ':');
1435 x = getcurx(v->window);
1436 if (x != ERR && x < view->ncols) {
1437 waddch(v->window, c);
1438 wrefresh(v->window);
1442 * Don't overflow. Max valid request should be the greatest
1443 * between the longest and total lines; cap at 10 million.
1448 n = n * 10 + (c - '0');
1449 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1451 if (c == 'G' || c == 'g') { /* nG key map */
1452 view->gline = view->hiline = n;
1457 /* Massage excessive or inapplicable values at the input handler. */
1463 static const struct got_error *
1464 view_input(struct tog_view **new, int *done, struct tog_view *view,
1465 struct tog_view_list_head *views)
1467 const struct got_error *err = NULL;
1473 /* Clear "no matches" indicator. */
1474 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1475 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1476 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1480 if (view->searching && !view->search_next_done) {
1481 errcode = pthread_mutex_unlock(&tog_mutex);
1483 return got_error_set_errno(errcode,
1484 "pthread_mutex_unlock");
1486 errcode = pthread_mutex_lock(&tog_mutex);
1488 return got_error_set_errno(errcode,
1489 "pthread_mutex_lock");
1490 view->search_next(view);
1494 /* Allow threads to make progress while we are waiting for input. */
1495 errcode = pthread_mutex_unlock(&tog_mutex);
1497 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1498 /* If we have an unfinished count, let C-g or backspace abort. */
1499 if (view->count && --view->count) {
1501 nodelay(view->window, TRUE);
1502 ch = wgetch(view->window);
1503 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1508 ch = wgetch(view->window);
1509 if (ch >= '1' && ch <= '9')
1510 view->ch = ch = get_compound_key(view, ch);
1512 if (view->hiline && ch != ERR && ch != 0)
1513 view->hiline = 0; /* key pressed, clear line highlight */
1514 nodelay(view->window, TRUE);
1515 errcode = pthread_mutex_lock(&tog_mutex);
1517 return got_error_set_errno(errcode, "pthread_mutex_lock");
1519 if (tog_sigwinch_received || tog_sigcont_received) {
1521 tog_sigwinch_received = 0;
1522 tog_sigcont_received = 0;
1523 TAILQ_FOREACH(v, views, entry) {
1524 err = view_resize(v);
1527 err = v->input(new, v, KEY_RESIZE);
1531 err = view_resize(v->child);
1534 err = v->child->input(new, v->child,
1538 if (v->child->resized_x || v->child->resized_y) {
1539 err = view_resize_split(v, 0);
1551 if (view->type == TOG_VIEW_HELP)
1552 err = view->reset(view);
1554 err = view_request_new(new, view, TOG_VIEW_HELP);
1560 view->child->focussed = 1;
1561 view->focus_child = 1;
1562 } else if (view->parent) {
1564 view->parent->focussed = 1;
1565 view->parent->focus_child = 0;
1566 if (!view_is_splitscreen(view)) {
1567 if (view->parent->resize) {
1568 err = view->parent->resize(view->parent,
1573 offset_selection_up(view->parent);
1574 err = view_fullscreen(view->parent);
1581 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1582 if (view->parent->resize) {
1583 /* might need more commits to fill fullscreen */
1584 err = view->parent->resize(view->parent, 0);
1588 offset_selection_up(view->parent);
1590 err = view->input(new, view, ch);
1598 if (view_is_parent_view(view)) {
1599 if (view->child == NULL)
1601 if (view_is_splitscreen(view->child)) {
1603 view->child->focussed = 1;
1604 err = view_fullscreen(view->child);
1606 err = view_splitscreen(view->child);
1608 err = view_resize_split(view, 0);
1612 err = view->child->input(new, view->child,
1615 if (view_is_splitscreen(view)) {
1616 view->parent->focussed = 0;
1618 err = view_fullscreen(view);
1620 err = view_splitscreen(view);
1621 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1622 err = view_resize(view->parent);
1624 err = view_resize_split(view, 0);
1628 err = view->input(new, view, KEY_RESIZE);
1633 err = view->resize(view, 0);
1638 err = offset_selection_down(view->parent);
1640 err = offset_selection_down(view);
1644 err = switch_split(view);
1647 err = view_resize_split(view, -1);
1650 err = view_resize_split(view, 1);
1656 if (view->search_start)
1657 view_search_start(view);
1659 err = view->input(new, view, ch);
1663 if (view->search_started && view->search_next) {
1664 view->searching = (ch == 'n' ?
1665 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1666 view->search_next_done = 0;
1667 view->search_next(view);
1669 err = view->input(new, view, ch);
1672 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1673 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1675 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1676 TAILQ_FOREACH(v, views, entry) {
1682 if (v->child && v->child->reset) {
1683 err = v->child->reset(v->child);
1690 err = view->input(new, view, ch);
1698 view_needs_focus_indication(struct tog_view *view)
1700 if (view_is_parent_view(view)) {
1701 if (view->child == NULL || view->child->focussed)
1703 if (!view_is_splitscreen(view->child))
1705 } else if (!view_is_splitscreen(view))
1708 return view->focussed;
1711 static const struct got_error *
1712 view_loop(struct tog_view *view)
1714 const struct got_error *err = NULL;
1715 struct tog_view_list_head views;
1716 struct tog_view *new_view;
1718 int fast_refresh = 10;
1719 int done = 0, errcode;
1721 mode = getenv("TOG_VIEW_SPLIT_MODE");
1722 if (!mode || !(*mode == 'h' || *mode == 'H'))
1723 view->mode = TOG_VIEW_SPLIT_VERT;
1725 view->mode = TOG_VIEW_SPLIT_HRZN;
1727 errcode = pthread_mutex_lock(&tog_mutex);
1729 return got_error_set_errno(errcode, "pthread_mutex_lock");
1732 TAILQ_INSERT_HEAD(&views, view, entry);
1735 err = view->show(view);
1740 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1741 !tog_fatal_signal_received()) {
1742 /* Refresh fast during initialization, then become slower. */
1743 if (fast_refresh && fast_refresh-- == 0)
1744 halfdelay(10); /* switch to once per second */
1746 err = view_input(&new_view, &done, view, &views);
1750 struct tog_view *v, *prev = NULL;
1752 if (view_is_parent_view(view))
1753 prev = TAILQ_PREV(view, tog_view_list_head,
1755 else if (view->parent)
1756 prev = view->parent;
1759 view->parent->child = NULL;
1760 view->parent->focus_child = 0;
1761 /* Restore fullscreen line height. */
1762 view->parent->nlines = view->parent->lines;
1763 err = view_resize(view->parent);
1766 /* Make resized splits persist. */
1767 view_transfer_size(view->parent, view);
1769 TAILQ_REMOVE(&views, view, entry);
1771 err = view_close(view);
1776 TAILQ_FOREACH(v, &views, entry) {
1780 if (view == NULL && new_view == NULL) {
1781 /* No view has focus. Try to pick one. */
1784 else if (!TAILQ_EMPTY(&views)) {
1785 view = TAILQ_LAST(&views,
1786 tog_view_list_head);
1789 if (view->focus_child) {
1790 view->child->focussed = 1;
1798 struct tog_view *v, *t;
1799 /* Only allow one parent view per type. */
1800 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1801 if (v->type != new_view->type)
1803 TAILQ_REMOVE(&views, v, entry);
1804 err = view_close(v);
1809 TAILQ_INSERT_TAIL(&views, new_view, entry);
1813 if (view_is_parent_view(view)) {
1814 if (view->child && view->child->focussed)
1817 if (view->parent && view->parent->focussed)
1818 view = view->parent;
1820 show_panel(view->panel);
1821 if (view->child && view_is_splitscreen(view->child))
1822 show_panel(view->child->panel);
1823 if (view->parent && view_is_splitscreen(view)) {
1824 err = view->parent->show(view->parent);
1828 err = view->show(view);
1832 err = view->child->show(view->child);
1841 while (!TAILQ_EMPTY(&views)) {
1842 const struct got_error *close_err;
1843 view = TAILQ_FIRST(&views);
1844 TAILQ_REMOVE(&views, view, entry);
1845 close_err = view_close(view);
1846 if (close_err && err == NULL)
1850 errcode = pthread_mutex_unlock(&tog_mutex);
1851 if (errcode && err == NULL)
1852 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1862 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1867 /* Create newly allocated wide-character string equivalent to a byte string. */
1868 static const struct got_error *
1869 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1872 const struct got_error *err = NULL;
1875 *wlen = mbstowcs(NULL, s, 0);
1876 if (*wlen == (size_t)-1) {
1878 if (errno != EILSEQ)
1879 return got_error_from_errno("mbstowcs");
1881 /* byte string invalid in current encoding; try to "fix" it */
1882 err = got_mbsavis(&vis, &vislen, s);
1885 *wlen = mbstowcs(NULL, vis, 0);
1886 if (*wlen == (size_t)-1) {
1887 err = got_error_from_errno("mbstowcs"); /* give up */
1892 *ws = calloc(*wlen + 1, sizeof(**ws));
1894 err = got_error_from_errno("calloc");
1898 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1899 err = got_error_from_errno("mbstowcs");
1910 static const struct got_error *
1911 expand_tab(char **ptr, const char *src)
1914 size_t len, n, idx = 0, sz = 0;
1917 n = len = strlen(src);
1918 dst = malloc(n + 1);
1920 return got_error_from_errno("malloc");
1922 while (idx < len && src[idx]) {
1923 const char c = src[idx];
1926 size_t nb = TABSIZE - sz % TABSIZE;
1929 p = realloc(dst, n + nb);
1932 return got_error_from_errno("realloc");
1937 memset(dst + sz, ' ', nb);
1940 dst[sz++] = src[idx];
1950 * Advance at most n columns from wline starting at offset off.
1951 * Return the index to the first character after the span operation.
1952 * Return the combined column width of all spanned wide character in
1956 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1958 int width, i, cols = 0;
1965 for (i = off; wline[i] != L'\0'; ++i) {
1966 if (wline[i] == L'\t')
1967 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1969 width = wcwidth(wline[i]);
1976 if (cols + width > n)
1986 * Format a line for display, ensuring that it won't overflow a width limit.
1987 * With scrolling, the width returned refers to the scrolled version of the
1988 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1990 static const struct got_error *
1991 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1992 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1994 const struct got_error *err = NULL;
1996 wchar_t *wline = NULL;
2005 err = expand_tab(&exstr, line);
2010 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2015 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2017 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2018 wline[wlen - 1] = L'\0';
2021 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2022 wline[wlen - 1] = L'\0';
2026 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2032 *scrollxp = scrollx;
2040 static const struct got_error*
2041 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2042 struct got_object_id *id, struct got_repository *repo)
2044 static const struct got_error *err = NULL;
2045 struct got_reflist_entry *re;
2051 TAILQ_FOREACH(re, refs, entry) {
2052 struct got_tag_object *tag = NULL;
2053 struct got_object_id *ref_id;
2056 name = got_ref_get_name(re->ref);
2057 if (strcmp(name, GOT_REF_HEAD) == 0)
2059 if (strncmp(name, "refs/", 5) == 0)
2061 if (strncmp(name, "got/", 4) == 0 &&
2062 strncmp(name, "got/backup/", 11) != 0)
2064 if (strncmp(name, "heads/", 6) == 0)
2066 if (strncmp(name, "remotes/", 8) == 0) {
2068 s = strstr(name, "/" GOT_REF_HEAD);
2069 if (s != NULL && s[strlen(s)] == '\0')
2072 err = got_ref_resolve(&ref_id, repo, re->ref);
2075 if (strncmp(name, "tags/", 5) == 0) {
2076 err = got_object_open_as_tag(&tag, repo, ref_id);
2078 if (err->code != GOT_ERR_OBJ_TYPE) {
2082 /* Ref points at something other than a tag. */
2087 cmp = got_object_id_cmp(tag ?
2088 got_object_tag_get_object_id(tag) : ref_id, id);
2091 got_object_tag_close(tag);
2095 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2096 s ? ", " : "", name) == -1) {
2097 err = got_error_from_errno("asprintf");
2108 static const struct got_error *
2109 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2114 smallerthan = strchr(author, '<');
2115 if (smallerthan && smallerthan[1] != '\0')
2116 author = smallerthan + 1;
2117 author[strcspn(author, "@>")] = '\0';
2118 return format_line(wauthor, author_width, NULL, author, 0, limit,
2122 static const struct got_error *
2123 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2124 struct got_object_id *id, const size_t date_display_cols,
2125 int author_display_cols)
2127 struct tog_log_view_state *s = &view->state.log;
2128 const struct got_error *err = NULL;
2129 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2130 char *logmsg0 = NULL, *logmsg = NULL;
2131 char *author = NULL;
2132 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2133 int author_width, logmsg_width;
2134 char *newline, *line = NULL;
2135 int col, limit, scrollx;
2136 const int avail = view->ncols;
2138 time_t committer_time;
2139 struct tog_color *tc;
2141 committer_time = got_object_commit_get_committer_time(commit);
2142 if (gmtime_r(&committer_time, &tm) == NULL)
2143 return got_error_from_errno("gmtime_r");
2144 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2145 return got_error(GOT_ERR_NO_SPACE);
2147 if (avail <= date_display_cols)
2148 limit = MIN(sizeof(datebuf) - 1, avail);
2150 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2151 tc = get_color(&s->colors, TOG_COLOR_DATE);
2153 wattr_on(view->window,
2154 COLOR_PAIR(tc->colorpair), NULL);
2155 waddnstr(view->window, datebuf, limit);
2157 wattr_off(view->window,
2158 COLOR_PAIR(tc->colorpair), NULL);
2165 err = got_object_id_str(&id_str, id);
2168 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2170 wattr_on(view->window,
2171 COLOR_PAIR(tc->colorpair), NULL);
2172 wprintw(view->window, "%.8s ", id_str);
2174 wattr_off(view->window,
2175 COLOR_PAIR(tc->colorpair), NULL);
2182 if (s->use_committer)
2183 author = strdup(got_object_commit_get_committer(commit));
2185 author = strdup(got_object_commit_get_author(commit));
2186 if (author == NULL) {
2187 err = got_error_from_errno("strdup");
2190 err = format_author(&wauthor, &author_width, author, avail - col, col);
2193 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2195 wattr_on(view->window,
2196 COLOR_PAIR(tc->colorpair), NULL);
2197 waddwstr(view->window, wauthor);
2198 col += author_width;
2199 while (col < avail && author_width < author_display_cols + 2) {
2200 waddch(view->window, ' ');
2205 wattr_off(view->window,
2206 COLOR_PAIR(tc->colorpair), NULL);
2210 err = got_object_commit_get_logmsg(&logmsg0, commit);
2214 while (*logmsg == '\n')
2216 newline = strchr(logmsg, '\n');
2219 limit = avail - col;
2220 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2221 limit--; /* for the border */
2222 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2226 waddwstr(view->window, &wlogmsg[scrollx]);
2227 col += MAX(logmsg_width, 0);
2228 while (col < avail) {
2229 waddch(view->window, ' ');
2241 static struct commit_queue_entry *
2242 alloc_commit_queue_entry(struct got_commit_object *commit,
2243 struct got_object_id *id)
2245 struct commit_queue_entry *entry;
2246 struct got_object_id *dup;
2248 entry = calloc(1, sizeof(*entry));
2252 dup = got_object_id_dup(id);
2259 entry->commit = commit;
2264 pop_commit(struct commit_queue *commits)
2266 struct commit_queue_entry *entry;
2268 entry = TAILQ_FIRST(&commits->head);
2269 TAILQ_REMOVE(&commits->head, entry, entry);
2270 got_object_commit_close(entry->commit);
2271 commits->ncommits--;
2277 free_commits(struct commit_queue *commits)
2279 while (!TAILQ_EMPTY(&commits->head))
2280 pop_commit(commits);
2283 static const struct got_error *
2284 match_commit(int *have_match, struct got_object_id *id,
2285 struct got_commit_object *commit, regex_t *regex)
2287 const struct got_error *err = NULL;
2288 regmatch_t regmatch;
2289 char *id_str = NULL, *logmsg = NULL;
2293 err = got_object_id_str(&id_str, id);
2297 err = got_object_commit_get_logmsg(&logmsg, commit);
2301 if (regexec(regex, got_object_commit_get_author(commit), 1,
2302 ®match, 0) == 0 ||
2303 regexec(regex, got_object_commit_get_committer(commit), 1,
2304 ®match, 0) == 0 ||
2305 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2306 regexec(regex, logmsg, 1, ®match, 0) == 0)
2314 static const struct got_error *
2315 queue_commits(struct tog_log_thread_args *a)
2317 const struct got_error *err = NULL;
2320 * We keep all commits open throughout the lifetime of the log
2321 * view in order to avoid having to re-fetch commits from disk
2322 * while updating the display.
2325 struct got_object_id id;
2326 struct got_commit_object *commit;
2327 struct commit_queue_entry *entry;
2328 int limit_match = 0;
2331 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2336 err = got_object_open_as_commit(&commit, a->repo, &id);
2339 entry = alloc_commit_queue_entry(commit, &id);
2340 if (entry == NULL) {
2341 err = got_error_from_errno("alloc_commit_queue_entry");
2345 errcode = pthread_mutex_lock(&tog_mutex);
2347 err = got_error_set_errno(errcode,
2348 "pthread_mutex_lock");
2352 entry->idx = a->real_commits->ncommits;
2353 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2354 a->real_commits->ncommits++;
2357 err = match_commit(&limit_match, &id, commit,
2363 struct commit_queue_entry *matched;
2365 matched = alloc_commit_queue_entry(
2366 entry->commit, entry->id);
2367 if (matched == NULL) {
2368 err = got_error_from_errno(
2369 "alloc_commit_queue_entry");
2372 matched->commit = entry->commit;
2373 got_object_commit_retain(entry->commit);
2375 matched->idx = a->limit_commits->ncommits;
2376 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2378 a->limit_commits->ncommits++;
2382 * This is how we signal log_thread() that we
2383 * have found a match, and that it should be
2384 * counted as a new entry for the view.
2386 a->limit_match = limit_match;
2389 if (*a->searching == TOG_SEARCH_FORWARD &&
2390 !*a->search_next_done) {
2392 err = match_commit(&have_match, &id, commit, a->regex);
2397 if (limit_match && have_match)
2398 *a->search_next_done =
2399 TOG_SEARCH_HAVE_MORE;
2400 } else if (have_match)
2401 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2404 errcode = pthread_mutex_unlock(&tog_mutex);
2405 if (errcode && err == NULL)
2406 err = got_error_set_errno(errcode,
2407 "pthread_mutex_unlock");
2410 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2416 select_commit(struct tog_log_view_state *s)
2418 struct commit_queue_entry *entry;
2421 entry = s->first_displayed_entry;
2423 if (ncommits == s->selected) {
2424 s->selected_entry = entry;
2427 entry = TAILQ_NEXT(entry, entry);
2432 static const struct got_error *
2433 draw_commits(struct tog_view *view)
2435 const struct got_error *err = NULL;
2436 struct tog_log_view_state *s = &view->state.log;
2437 struct commit_queue_entry *entry = s->selected_entry;
2438 int limit = view->nlines;
2440 int ncommits, author_cols = 4;
2441 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2442 char *refs_str = NULL;
2444 struct tog_color *tc;
2445 static const size_t date_display_cols = 12;
2447 if (view_is_hsplit_top(view))
2448 --limit; /* account for border */
2450 if (s->selected_entry &&
2451 !(view->searching && view->search_next_done == 0)) {
2452 struct got_reflist_head *refs;
2453 err = got_object_id_str(&id_str, s->selected_entry->id);
2456 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2457 s->selected_entry->id);
2459 err = build_refs_str(&refs_str, refs,
2460 s->selected_entry->id, s->repo);
2466 if (s->thread_args.commits_needed == 0)
2467 halfdelay(10); /* disable fast refresh */
2469 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2470 if (asprintf(&ncommits_str, " [%d/%d] %s",
2471 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2472 (view->searching && !view->search_next_done) ?
2473 "searching..." : "loading...") == -1) {
2474 err = got_error_from_errno("asprintf");
2478 const char *search_str = NULL;
2479 const char *limit_str = NULL;
2481 if (view->searching) {
2482 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2483 search_str = "no more matches";
2484 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2485 search_str = "no matches found";
2486 else if (!view->search_next_done)
2487 search_str = "searching...";
2490 if (s->limit_view && s->commits->ncommits == 0)
2491 limit_str = "no matches found";
2493 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2494 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2495 search_str ? search_str : (refs_str ? refs_str : ""),
2496 limit_str ? limit_str : "") == -1) {
2497 err = got_error_from_errno("asprintf");
2502 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2503 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2504 "........................................",
2505 s->in_repo_path, ncommits_str) == -1) {
2506 err = got_error_from_errno("asprintf");
2510 } else if (asprintf(&header, "commit %s%s",
2511 id_str ? id_str : "........................................",
2512 ncommits_str) == -1) {
2513 err = got_error_from_errno("asprintf");
2517 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2521 werase(view->window);
2523 if (view_needs_focus_indication(view))
2524 wstandout(view->window);
2525 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2527 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2528 waddwstr(view->window, wline);
2529 while (width < view->ncols) {
2530 waddch(view->window, ' ');
2534 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2535 if (view_needs_focus_indication(view))
2536 wstandend(view->window);
2541 /* Grow author column size if necessary, and set view->maxx. */
2542 entry = s->first_displayed_entry;
2546 struct got_commit_object *c = entry->commit;
2547 char *author, *eol, *msg, *msg0;
2548 wchar_t *wauthor, *wmsg;
2550 if (ncommits >= limit - 1)
2552 if (s->use_committer)
2553 author = strdup(got_object_commit_get_committer(c));
2555 author = strdup(got_object_commit_get_author(c));
2556 if (author == NULL) {
2557 err = got_error_from_errno("strdup");
2560 err = format_author(&wauthor, &width, author, COLS,
2562 if (author_cols < width)
2563 author_cols = width;
2568 err = got_object_commit_get_logmsg(&msg0, c);
2572 while (*msg == '\n')
2574 if ((eol = strchr(msg, '\n')))
2576 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2577 date_display_cols + author_cols, 0);
2580 view->maxx = MAX(view->maxx, width);
2584 entry = TAILQ_NEXT(entry, entry);
2587 entry = s->first_displayed_entry;
2588 s->last_displayed_entry = s->first_displayed_entry;
2591 if (ncommits >= limit - 1)
2593 if (ncommits == s->selected)
2594 wstandout(view->window);
2595 err = draw_commit(view, entry->commit, entry->id,
2596 date_display_cols, author_cols);
2597 if (ncommits == s->selected)
2598 wstandend(view->window);
2602 s->last_displayed_entry = entry;
2603 entry = TAILQ_NEXT(entry, entry);
2616 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2618 struct commit_queue_entry *entry;
2621 entry = TAILQ_FIRST(&s->commits->head);
2622 if (s->first_displayed_entry == entry)
2625 entry = s->first_displayed_entry;
2626 while (entry && nscrolled < maxscroll) {
2627 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2629 s->first_displayed_entry = entry;
2635 static const struct got_error *
2636 trigger_log_thread(struct tog_view *view, int wait)
2638 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2641 halfdelay(1); /* fast refresh while loading commits */
2643 while (!ta->log_complete && !tog_thread_error &&
2644 (ta->commits_needed > 0 || ta->load_all)) {
2645 /* Wake the log thread. */
2646 errcode = pthread_cond_signal(&ta->need_commits);
2648 return got_error_set_errno(errcode,
2649 "pthread_cond_signal");
2652 * The mutex will be released while the view loop waits
2653 * in wgetch(), at which time the log thread will run.
2658 /* Display progress update in log view. */
2659 show_log_view(view);
2663 /* Wait right here while next commit is being loaded. */
2664 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2666 return got_error_set_errno(errcode,
2667 "pthread_cond_wait");
2669 /* Display progress update in log view. */
2670 show_log_view(view);
2678 static const struct got_error *
2679 request_log_commits(struct tog_view *view)
2681 struct tog_log_view_state *state = &view->state.log;
2682 const struct got_error *err = NULL;
2684 if (state->thread_args.log_complete)
2687 state->thread_args.commits_needed += view->nscrolled;
2688 err = trigger_log_thread(view, 1);
2689 view->nscrolled = 0;
2694 static const struct got_error *
2695 log_scroll_down(struct tog_view *view, int maxscroll)
2697 struct tog_log_view_state *s = &view->state.log;
2698 const struct got_error *err = NULL;
2699 struct commit_queue_entry *pentry;
2700 int nscrolled = 0, ncommits_needed;
2702 if (s->last_displayed_entry == NULL)
2705 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2706 if (s->commits->ncommits < ncommits_needed &&
2707 !s->thread_args.log_complete) {
2709 * Ask the log thread for required amount of commits.
2711 s->thread_args.commits_needed +=
2712 ncommits_needed - s->commits->ncommits;
2713 err = trigger_log_thread(view, 1);
2719 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2720 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2723 s->last_displayed_entry = pentry ?
2724 pentry : s->last_displayed_entry;;
2726 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2729 s->first_displayed_entry = pentry;
2730 } while (++nscrolled < maxscroll);
2732 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2733 view->nscrolled += nscrolled;
2735 view->nscrolled = 0;
2740 static const struct got_error *
2741 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2742 struct got_commit_object *commit, struct got_object_id *commit_id,
2743 struct tog_view *log_view, struct got_repository *repo)
2745 const struct got_error *err;
2746 struct got_object_qid *parent_id;
2747 struct tog_view *diff_view;
2749 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2750 if (diff_view == NULL)
2751 return got_error_from_errno("view_open");
2753 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2754 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2755 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2757 *new_view = diff_view;
2761 static const struct got_error *
2762 tree_view_visit_subtree(struct tog_tree_view_state *s,
2763 struct got_tree_object *subtree)
2765 struct tog_parent_tree *parent;
2767 parent = calloc(1, sizeof(*parent));
2769 return got_error_from_errno("calloc");
2771 parent->tree = s->tree;
2772 parent->first_displayed_entry = s->first_displayed_entry;
2773 parent->selected_entry = s->selected_entry;
2774 parent->selected = s->selected;
2775 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2778 s->first_displayed_entry = NULL;
2782 static const struct got_error *
2783 tree_view_walk_path(struct tog_tree_view_state *s,
2784 struct got_commit_object *commit, const char *path)
2786 const struct got_error *err = NULL;
2787 struct got_tree_object *tree = NULL;
2789 char *slash, *subpath = NULL;
2791 /* Walk the path and open corresponding tree objects. */
2794 struct got_tree_entry *te;
2795 struct got_object_id *tree_id;
2801 /* Ensure the correct subtree entry is selected. */
2802 slash = strchr(p, '/');
2804 te_name = strdup(p);
2806 te_name = strndup(p, slash - p);
2807 if (te_name == NULL) {
2808 err = got_error_from_errno("strndup");
2811 te = got_object_tree_find_entry(s->tree, te_name);
2813 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2818 s->first_displayed_entry = s->selected_entry = te;
2820 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2821 break; /* jump to this file's entry */
2823 slash = strchr(p, '/');
2825 subpath = strndup(path, slash - path);
2827 subpath = strdup(path);
2828 if (subpath == NULL) {
2829 err = got_error_from_errno("strdup");
2833 err = got_object_id_by_path(&tree_id, s->repo, commit,
2838 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2843 err = tree_view_visit_subtree(s, tree);
2845 got_object_tree_close(tree);
2859 static const struct got_error *
2860 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2861 struct commit_queue_entry *entry, const char *path,
2862 const char *head_ref_name, struct got_repository *repo)
2864 const struct got_error *err = NULL;
2865 struct tog_tree_view_state *s;
2866 struct tog_view *tree_view;
2868 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2869 if (tree_view == NULL)
2870 return got_error_from_errno("view_open");
2872 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2875 s = &tree_view->state.tree;
2877 *new_view = tree_view;
2879 if (got_path_is_root_dir(path))
2882 return tree_view_walk_path(s, entry->commit, path);
2885 static const struct got_error *
2886 block_signals_used_by_main_thread(void)
2891 if (sigemptyset(&sigset) == -1)
2892 return got_error_from_errno("sigemptyset");
2894 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2895 if (sigaddset(&sigset, SIGWINCH) == -1)
2896 return got_error_from_errno("sigaddset");
2897 if (sigaddset(&sigset, SIGCONT) == -1)
2898 return got_error_from_errno("sigaddset");
2899 if (sigaddset(&sigset, SIGINT) == -1)
2900 return got_error_from_errno("sigaddset");
2901 if (sigaddset(&sigset, SIGTERM) == -1)
2902 return got_error_from_errno("sigaddset");
2904 /* ncurses handles SIGTSTP */
2905 if (sigaddset(&sigset, SIGTSTP) == -1)
2906 return got_error_from_errno("sigaddset");
2908 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2910 return got_error_set_errno(errcode, "pthread_sigmask");
2916 log_thread(void *arg)
2918 const struct got_error *err = NULL;
2920 struct tog_log_thread_args *a = arg;
2924 * Sync startup with main thread such that we begin our
2925 * work once view_input() has released the mutex.
2927 errcode = pthread_mutex_lock(&tog_mutex);
2929 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2933 err = block_signals_used_by_main_thread();
2935 pthread_mutex_unlock(&tog_mutex);
2939 while (!done && !err && !tog_fatal_signal_received()) {
2940 errcode = pthread_mutex_unlock(&tog_mutex);
2942 err = got_error_set_errno(errcode,
2943 "pthread_mutex_unlock");
2946 err = queue_commits(a);
2948 if (err->code != GOT_ERR_ITER_COMPLETED)
2952 } else if (a->commits_needed > 0 && !a->load_all) {
2955 a->commits_needed--;
2957 a->commits_needed--;
2960 errcode = pthread_mutex_lock(&tog_mutex);
2962 err = got_error_set_errno(errcode,
2963 "pthread_mutex_lock");
2965 } else if (*a->quit)
2967 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2968 *a->first_displayed_entry =
2969 TAILQ_FIRST(&a->limit_commits->head);
2970 *a->selected_entry = *a->first_displayed_entry;
2971 } else if (*a->first_displayed_entry == NULL) {
2972 *a->first_displayed_entry =
2973 TAILQ_FIRST(&a->real_commits->head);
2974 *a->selected_entry = *a->first_displayed_entry;
2977 errcode = pthread_cond_signal(&a->commit_loaded);
2979 err = got_error_set_errno(errcode,
2980 "pthread_cond_signal");
2981 pthread_mutex_unlock(&tog_mutex);
2986 a->commits_needed = 0;
2988 if (a->commits_needed == 0 && !a->load_all) {
2989 errcode = pthread_cond_wait(&a->need_commits,
2992 err = got_error_set_errno(errcode,
2993 "pthread_cond_wait");
2994 pthread_mutex_unlock(&tog_mutex);
3002 a->log_complete = 1;
3003 errcode = pthread_mutex_unlock(&tog_mutex);
3005 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3008 tog_thread_error = 1;
3009 pthread_cond_signal(&a->commit_loaded);
3014 static const struct got_error *
3015 stop_log_thread(struct tog_log_view_state *s)
3017 const struct got_error *err = NULL, *thread_err = NULL;
3022 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3024 return got_error_set_errno(errcode,
3025 "pthread_cond_signal");
3026 errcode = pthread_mutex_unlock(&tog_mutex);
3028 return got_error_set_errno(errcode,
3029 "pthread_mutex_unlock");
3030 errcode = pthread_join(s->thread, (void **)&thread_err);
3032 return got_error_set_errno(errcode, "pthread_join");
3033 errcode = pthread_mutex_lock(&tog_mutex);
3035 return got_error_set_errno(errcode,
3036 "pthread_mutex_lock");
3037 s->thread = 0; //NULL;
3040 if (s->thread_args.repo) {
3041 err = got_repo_close(s->thread_args.repo);
3042 s->thread_args.repo = NULL;
3045 if (s->thread_args.pack_fds) {
3046 const struct got_error *pack_err =
3047 got_repo_pack_fds_close(s->thread_args.pack_fds);
3050 s->thread_args.pack_fds = NULL;
3053 if (s->thread_args.graph) {
3054 got_commit_graph_close(s->thread_args.graph);
3055 s->thread_args.graph = NULL;
3058 return err ? err : thread_err;
3061 static const struct got_error *
3062 close_log_view(struct tog_view *view)
3064 const struct got_error *err = NULL;
3065 struct tog_log_view_state *s = &view->state.log;
3068 err = stop_log_thread(s);
3070 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3071 if (errcode && err == NULL)
3072 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3074 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3075 if (errcode && err == NULL)
3076 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3078 free_commits(&s->limit_commits);
3079 free_commits(&s->real_commits);
3080 free(s->in_repo_path);
3081 s->in_repo_path = NULL;
3084 free(s->head_ref_name);
3085 s->head_ref_name = NULL;
3090 * We use two queues to implement the limit feature: first consists of
3091 * commits matching the current limit_regex; second is the real queue
3092 * of all known commits (real_commits). When the user starts limiting,
3093 * we swap queues such that all movement and displaying functionality
3094 * works with very slight change.
3096 static const struct got_error *
3097 limit_log_view(struct tog_view *view)
3099 struct tog_log_view_state *s = &view->state.log;
3100 struct commit_queue_entry *entry;
3101 struct tog_view *v = view;
3102 const struct got_error *err = NULL;
3106 if (view_is_hsplit_top(view))
3108 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3111 /* Get the pattern */
3112 wmove(v->window, v->nlines - 1, 0);
3113 wclrtoeol(v->window);
3114 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3115 nodelay(v->window, FALSE);
3118 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3121 nodelay(v->window, TRUE);
3125 if (*pattern == '\0') {
3127 * Safety measure for the situation where the user
3128 * resets limit without previously limiting anything.
3134 * User could have pressed Ctrl+L, which refreshed the
3135 * commit queues, it means we can't save previously
3136 * (before limit took place) displayed entries,
3137 * because they would point to already free'ed memory,
3138 * so we are forced to always select first entry of
3141 s->commits = &s->real_commits;
3142 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3143 s->selected_entry = s->first_displayed_entry;
3150 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3155 /* Clear the screen while loading limit view */
3156 s->first_displayed_entry = NULL;
3157 s->last_displayed_entry = NULL;
3158 s->selected_entry = NULL;
3159 s->commits = &s->limit_commits;
3161 /* Prepare limit queue for new search */
3162 free_commits(&s->limit_commits);
3163 s->limit_commits.ncommits = 0;
3165 /* First process commits, which are in queue already */
3166 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3169 err = match_commit(&have_match, entry->id,
3170 entry->commit, &s->limit_regex);
3175 struct commit_queue_entry *matched;
3177 matched = alloc_commit_queue_entry(entry->commit,
3179 if (matched == NULL) {
3180 err = got_error_from_errno(
3181 "alloc_commit_queue_entry");
3184 matched->commit = entry->commit;
3185 got_object_commit_retain(entry->commit);
3187 matched->idx = s->limit_commits.ncommits;
3188 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3190 s->limit_commits.ncommits++;
3194 /* Second process all the commits, until we fill the screen */
3195 if (s->limit_commits.ncommits < view->nlines - 1 &&
3196 !s->thread_args.log_complete) {
3197 s->thread_args.commits_needed +=
3198 view->nlines - s->limit_commits.ncommits - 1;
3199 err = trigger_log_thread(view, 1);
3204 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3205 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3211 static const struct got_error *
3212 search_start_log_view(struct tog_view *view)
3214 struct tog_log_view_state *s = &view->state.log;
3216 s->matched_entry = NULL;
3217 s->search_entry = NULL;
3221 static const struct got_error *
3222 search_next_log_view(struct tog_view *view)
3224 const struct got_error *err = NULL;
3225 struct tog_log_view_state *s = &view->state.log;
3226 struct commit_queue_entry *entry;
3228 /* Display progress update in log view. */
3229 show_log_view(view);
3233 if (s->search_entry) {
3235 errcode = pthread_mutex_unlock(&tog_mutex);
3237 return got_error_set_errno(errcode,
3238 "pthread_mutex_unlock");
3239 ch = wgetch(view->window);
3240 errcode = pthread_mutex_lock(&tog_mutex);
3242 return got_error_set_errno(errcode,
3243 "pthread_mutex_lock");
3244 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3245 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3248 if (view->searching == TOG_SEARCH_FORWARD)
3249 entry = TAILQ_NEXT(s->search_entry, entry);
3251 entry = TAILQ_PREV(s->search_entry,
3252 commit_queue_head, entry);
3253 } else if (s->matched_entry) {
3255 * If the user has moved the cursor after we hit a match,
3256 * the position from where we should continue searching
3257 * might have changed.
3259 if (view->searching == TOG_SEARCH_FORWARD)
3260 entry = TAILQ_NEXT(s->selected_entry, entry);
3262 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3265 entry = s->selected_entry;
3271 if (entry == NULL) {
3272 if (s->thread_args.log_complete ||
3273 view->searching == TOG_SEARCH_BACKWARD) {
3274 view->search_next_done =
3275 (s->matched_entry == NULL ?
3276 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3277 s->search_entry = NULL;
3281 * Poke the log thread for more commits and return,
3282 * allowing the main loop to make progress. Search
3283 * will resume at s->search_entry once we come back.
3285 s->thread_args.commits_needed++;
3286 return trigger_log_thread(view, 0);
3289 err = match_commit(&have_match, entry->id, entry->commit,
3294 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3295 s->matched_entry = entry;
3299 s->search_entry = entry;
3300 if (view->searching == TOG_SEARCH_FORWARD)
3301 entry = TAILQ_NEXT(entry, entry);
3303 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3306 if (s->matched_entry) {
3307 int cur = s->selected_entry->idx;
3308 while (cur < s->matched_entry->idx) {
3309 err = input_log_view(NULL, view, KEY_DOWN);
3314 while (cur > s->matched_entry->idx) {
3315 err = input_log_view(NULL, view, KEY_UP);
3322 s->search_entry = NULL;
3327 static const struct got_error *
3328 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3329 struct got_repository *repo, const char *head_ref_name,
3330 const char *in_repo_path, int log_branches)
3332 const struct got_error *err = NULL;
3333 struct tog_log_view_state *s = &view->state.log;
3334 struct got_repository *thread_repo = NULL;
3335 struct got_commit_graph *thread_graph = NULL;
3338 if (in_repo_path != s->in_repo_path) {
3339 free(s->in_repo_path);
3340 s->in_repo_path = strdup(in_repo_path);
3341 if (s->in_repo_path == NULL)
3342 return got_error_from_errno("strdup");
3345 /* The commit queue only contains commits being displayed. */
3346 TAILQ_INIT(&s->real_commits.head);
3347 s->real_commits.ncommits = 0;
3348 s->commits = &s->real_commits;
3350 TAILQ_INIT(&s->limit_commits.head);
3352 s->limit_commits.ncommits = 0;
3355 if (head_ref_name) {
3356 s->head_ref_name = strdup(head_ref_name);
3357 if (s->head_ref_name == NULL) {
3358 err = got_error_from_errno("strdup");
3362 s->start_id = got_object_id_dup(start_id);
3363 if (s->start_id == NULL) {
3364 err = got_error_from_errno("got_object_id_dup");
3367 s->log_branches = log_branches;
3369 STAILQ_INIT(&s->colors);
3370 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3371 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3372 get_color_value("TOG_COLOR_COMMIT"));
3375 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3376 get_color_value("TOG_COLOR_AUTHOR"));
3378 free_colors(&s->colors);
3381 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3382 get_color_value("TOG_COLOR_DATE"));
3384 free_colors(&s->colors);
3389 view->show = show_log_view;
3390 view->input = input_log_view;
3391 view->resize = resize_log_view;
3392 view->close = close_log_view;
3393 view->search_start = search_start_log_view;
3394 view->search_next = search_next_log_view;
3396 if (s->thread_args.pack_fds == NULL) {
3397 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3401 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3402 s->thread_args.pack_fds);
3405 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3409 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3410 s->repo, NULL, NULL);
3414 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3416 err = got_error_set_errno(errcode, "pthread_cond_init");
3419 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3421 err = got_error_set_errno(errcode, "pthread_cond_init");
3425 s->thread_args.commits_needed = view->nlines;
3426 s->thread_args.graph = thread_graph;
3427 s->thread_args.real_commits = &s->real_commits;
3428 s->thread_args.limit_commits = &s->limit_commits;
3429 s->thread_args.in_repo_path = s->in_repo_path;
3430 s->thread_args.start_id = s->start_id;
3431 s->thread_args.repo = thread_repo;
3432 s->thread_args.log_complete = 0;
3433 s->thread_args.quit = &s->quit;
3434 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3435 s->thread_args.selected_entry = &s->selected_entry;
3436 s->thread_args.searching = &view->searching;
3437 s->thread_args.search_next_done = &view->search_next_done;
3438 s->thread_args.regex = &view->regex;
3439 s->thread_args.limiting = &s->limit_view;
3440 s->thread_args.limit_regex = &s->limit_regex;
3441 s->thread_args.limit_commits = &s->limit_commits;
3444 close_log_view(view);
3448 static const struct got_error *
3449 show_log_view(struct tog_view *view)
3451 const struct got_error *err;
3452 struct tog_log_view_state *s = &view->state.log;
3454 if (s->thread == 0) { //NULL) {
3455 int errcode = pthread_create(&s->thread, NULL, log_thread,
3458 return got_error_set_errno(errcode, "pthread_create");
3459 if (s->thread_args.commits_needed > 0) {
3460 err = trigger_log_thread(view, 1);
3466 return draw_commits(view);
3470 log_move_cursor_up(struct tog_view *view, int page, int home)
3472 struct tog_log_view_state *s = &view->state.log;
3474 if (s->first_displayed_entry == NULL)
3476 if (s->selected_entry->idx == 0)
3479 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3481 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3483 if (!page && !home && s->selected > 0)
3486 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3492 static const struct got_error *
3493 log_move_cursor_down(struct tog_view *view, int page)
3495 struct tog_log_view_state *s = &view->state.log;
3496 const struct got_error *err = NULL;
3497 int eos = view->nlines - 2;
3499 if (s->first_displayed_entry == NULL)
3502 if (s->thread_args.log_complete &&
3503 s->selected_entry->idx >= s->commits->ncommits - 1)
3506 if (view_is_hsplit_top(view))
3507 --eos; /* border consumes the last line */
3510 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3513 err = log_scroll_down(view, 1);
3514 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3515 struct commit_queue_entry *entry;
3519 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3520 s->last_displayed_entry = entry;
3521 for (n = 0; n <= eos; n++) {
3524 s->first_displayed_entry = entry;
3525 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3528 s->selected = n - 1;
3530 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3531 s->thread_args.log_complete)
3532 s->selected += MIN(page,
3533 s->commits->ncommits - s->selected_entry->idx - 1);
3535 err = log_scroll_down(view, page);
3541 * We might necessarily overshoot in horizontal
3542 * splits; if so, select the last displayed commit.
3544 if (s->first_displayed_entry && s->last_displayed_entry) {
3545 s->selected = MIN(s->selected,
3546 s->last_displayed_entry->idx -
3547 s->first_displayed_entry->idx);
3552 if (s->thread_args.log_complete &&
3553 s->selected_entry->idx == s->commits->ncommits - 1)
3560 view_get_split(struct tog_view *view, int *y, int *x)
3565 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3566 if (view->child && view->child->resized_y)
3567 *y = view->child->resized_y;
3568 else if (view->resized_y)
3569 *y = view->resized_y;
3571 *y = view_split_begin_y(view->lines);
3572 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3573 if (view->child && view->child->resized_x)
3574 *x = view->child->resized_x;
3575 else if (view->resized_x)
3576 *x = view->resized_x;
3578 *x = view_split_begin_x(view->begin_x);
3582 /* Split view horizontally at y and offset view->state->selected line. */
3583 static const struct got_error *
3584 view_init_hsplit(struct tog_view *view, int y)
3586 const struct got_error *err = NULL;
3590 err = view_resize(view);
3594 err = offset_selection_down(view);
3599 static const struct got_error *
3600 log_goto_line(struct tog_view *view, int nlines)
3602 const struct got_error *err = NULL;
3603 struct tog_log_view_state *s = &view->state.log;
3604 int g, idx = s->selected_entry->idx;
3606 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3612 if (g >= s->first_displayed_entry->idx + 1 &&
3613 g <= s->last_displayed_entry->idx + 1 &&
3614 g - s->first_displayed_entry->idx - 1 < nlines) {
3615 s->selected = g - s->first_displayed_entry->idx - 1;
3621 err = log_move_cursor_down(view, g - idx - 1);
3622 if (!err && g > s->selected_entry->idx + 1)
3623 err = log_move_cursor_down(view,
3624 g - s->first_displayed_entry->idx - 1);
3627 } else if (idx + 1 > g)
3628 log_move_cursor_up(view, idx - g + 1, 0);
3630 if (g < nlines && s->first_displayed_entry->idx == 0)
3631 s->selected = g - 1;
3638 static const struct got_error *
3639 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3641 const struct got_error *err = NULL;
3642 struct tog_log_view_state *s = &view->state.log;
3645 if (s->thread_args.load_all) {
3646 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3647 s->thread_args.load_all = 0;
3648 else if (s->thread_args.log_complete) {
3649 err = log_move_cursor_down(view, s->commits->ncommits);
3650 s->thread_args.load_all = 0;
3656 eos = nscroll = view->nlines - 1;
3657 if (view_is_hsplit_top(view))
3661 return log_goto_line(view, eos);
3665 err = limit_log_view(view);
3674 view->x = MAX(view->maxx - view->ncols / 2, 0);
3679 if (view->x + view->ncols / 2 < view->maxx)
3680 view->x += 2; /* move two columns right */
3686 view->x -= MIN(view->x, 2); /* move two columns back */
3695 log_move_cursor_up(view, 0, 0);
3699 log_move_cursor_up(view, 0, 1);
3709 log_move_cursor_up(view, nscroll, 0);
3716 err = log_move_cursor_down(view, 0);
3719 s->use_committer = !s->use_committer;
3723 /* We don't know yet how many commits, so we're forced to
3724 * traverse them all. */
3726 s->thread_args.load_all = 1;
3727 if (!s->thread_args.log_complete)
3728 return trigger_log_thread(view, 0);
3729 err = log_move_cursor_down(view, s->commits->ncommits);
3730 s->thread_args.load_all = 0;
3741 err = log_move_cursor_down(view, nscroll);
3744 if (s->selected > view->nlines - 2)
3745 s->selected = view->nlines - 2;
3746 if (s->selected > s->commits->ncommits - 1)
3747 s->selected = s->commits->ncommits - 1;
3749 if (s->commits->ncommits < view->nlines - 1 &&
3750 !s->thread_args.log_complete) {
3751 s->thread_args.commits_needed += (view->nlines - 1) -
3752 s->commits->ncommits;
3753 err = trigger_log_thread(view, 1);
3759 if (s->selected_entry == NULL)
3761 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3765 if (s->selected_entry == NULL)
3767 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3773 if (ch == KEY_BACKSPACE &&
3774 got_path_is_root_dir(s->in_repo_path))
3776 err = stop_log_thread(s);
3779 if (ch == KEY_BACKSPACE) {
3781 err = got_path_dirname(&parent_path, s->in_repo_path);
3784 free(s->in_repo_path);
3785 s->in_repo_path = parent_path;
3786 s->thread_args.in_repo_path = s->in_repo_path;
3787 } else if (ch == CTRL('l')) {
3788 struct got_object_id *start_id;
3789 err = got_repo_match_object_id(&start_id, NULL,
3790 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3791 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3793 if (s->head_ref_name == NULL ||
3794 err->code != GOT_ERR_NOT_REF)
3796 /* Try to cope with deleted references. */
3797 free(s->head_ref_name);
3798 s->head_ref_name = NULL;
3799 err = got_repo_match_object_id(&start_id,
3800 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3801 &tog_refs, s->repo);
3806 s->start_id = start_id;
3807 s->thread_args.start_id = s->start_id;
3809 s->log_branches = !s->log_branches;
3811 if (s->thread_args.pack_fds == NULL) {
3812 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3816 err = got_repo_open(&s->thread_args.repo,
3817 got_repo_get_path(s->repo), NULL,
3818 s->thread_args.pack_fds);
3822 err = tog_load_refs(s->repo, 0);
3825 err = got_commit_graph_open(&s->thread_args.graph,
3826 s->in_repo_path, !s->log_branches);
3829 err = got_commit_graph_iter_start(s->thread_args.graph,
3830 s->start_id, s->repo, NULL, NULL);
3833 free_commits(&s->real_commits);
3834 free_commits(&s->limit_commits);
3835 s->first_displayed_entry = NULL;
3836 s->last_displayed_entry = NULL;
3837 s->selected_entry = NULL;
3839 s->thread_args.log_complete = 0;
3841 s->thread_args.commits_needed = view->lines;
3842 s->matched_entry = NULL;
3843 s->search_entry = NULL;
3848 err = view_request_new(new_view, view, TOG_VIEW_REF);
3858 static const struct got_error *
3859 apply_unveil(const char *repo_path, const char *worktree_path)
3861 const struct got_error *error;
3864 if (unveil("gmon.out", "rwc") != 0)
3865 return got_error_from_errno2("unveil", "gmon.out");
3867 if (repo_path && unveil(repo_path, "r") != 0)
3868 return got_error_from_errno2("unveil", repo_path);
3870 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3871 return got_error_from_errno2("unveil", worktree_path);
3873 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3874 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3876 error = got_privsep_unveil_exec_helpers();
3880 if (unveil(NULL, NULL) != 0)
3881 return got_error_from_errno("unveil");
3890 * Override default signal handlers before starting ncurses.
3891 * This should prevent ncurses from installing its own
3892 * broken cleanup() signal handler.
3894 signal(SIGWINCH, tog_sigwinch);
3895 signal(SIGPIPE, tog_sigpipe);
3896 signal(SIGCONT, tog_sigcont);
3897 signal(SIGINT, tog_sigint);
3898 signal(SIGTERM, tog_sigterm);
3902 halfdelay(1); /* Do fast refresh while initial view is loading. */
3905 intrflush(stdscr, FALSE);
3906 keypad(stdscr, TRUE);
3908 if (getenv("TOG_COLORS") != NULL) {
3910 use_default_colors();
3914 static const struct got_error *
3915 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3916 struct got_repository *repo, struct got_worktree *worktree)
3918 const struct got_error *err = NULL;
3921 *in_repo_path = strdup("/");
3922 if (*in_repo_path == NULL)
3923 return got_error_from_errno("strdup");
3928 const char *prefix = got_worktree_get_path_prefix(worktree);
3931 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3934 if (asprintf(in_repo_path, "%s%s%s", prefix,
3935 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3937 err = got_error_from_errno("asprintf");
3938 *in_repo_path = NULL;
3942 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3947 static const struct got_error *
3948 cmd_log(int argc, char *argv[])
3950 const struct got_error *error;
3951 struct got_repository *repo = NULL;
3952 struct got_worktree *worktree = NULL;
3953 struct got_object_id *start_id = NULL;
3954 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3955 char *start_commit = NULL, *label = NULL;
3956 struct got_reference *ref = NULL;
3957 const char *head_ref_name = NULL;
3958 int ch, log_branches = 0;
3959 struct tog_view *view;
3960 int *pack_fds = NULL;
3962 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3968 start_commit = optarg;
3971 repo_path = realpath(optarg, NULL);
3972 if (repo_path == NULL)
3973 return got_error_from_errno2("realpath",
3988 error = got_repo_pack_fds_open(&pack_fds);
3992 if (repo_path == NULL) {
3993 cwd = getcwd(NULL, 0);
3995 return got_error_from_errno("getcwd");
3996 error = got_worktree_open(&worktree, cwd);
3997 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4001 strdup(got_worktree_get_repo_path(worktree));
4003 repo_path = strdup(cwd);
4004 if (repo_path == NULL) {
4005 error = got_error_from_errno("strdup");
4010 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4014 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4021 error = apply_unveil(got_repo_get_path(repo),
4022 worktree ? got_worktree_get_root_path(worktree) : NULL);
4026 /* already loaded by tog_log_with_path()? */
4027 if (TAILQ_EMPTY(&tog_refs)) {
4028 error = tog_load_refs(repo, 0);
4033 if (start_commit == NULL) {
4034 error = got_repo_match_object_id(&start_id, &label,
4035 worktree ? got_worktree_get_head_ref_name(worktree) :
4036 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4039 head_ref_name = label;
4041 error = got_ref_open(&ref, repo, start_commit, 0);
4043 head_ref_name = got_ref_get_name(ref);
4044 else if (error->code != GOT_ERR_NOT_REF)
4046 error = got_repo_match_object_id(&start_id, NULL,
4047 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4052 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4054 error = got_error_from_errno("view_open");
4057 error = open_log_view(view, start_id, repo, head_ref_name,
4058 in_repo_path, log_branches);
4062 /* Release work tree lock. */
4063 got_worktree_close(worktree);
4066 error = view_loop(view);
4076 const struct got_error *close_err = got_repo_close(repo);
4081 got_worktree_close(worktree);
4083 const struct got_error *pack_err =
4084 got_repo_pack_fds_close(pack_fds);
4096 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4097 "object1 object2\n", getprogname());
4102 match_line(const char *line, regex_t *regex, size_t nmatch,
4103 regmatch_t *regmatch)
4105 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4108 static struct tog_color *
4109 match_color(struct tog_colors *colors, const char *line)
4111 struct tog_color *tc = NULL;
4113 STAILQ_FOREACH(tc, colors, entry) {
4114 if (match_line(line, &tc->regex, 0, NULL))
4121 static const struct got_error *
4122 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4123 WINDOW *window, int skipcol, regmatch_t *regmatch)
4125 const struct got_error *err = NULL;
4127 wchar_t *wline = NULL;
4128 int rme, rms, n, width, scrollx;
4129 int width0 = 0, width1 = 0, width2 = 0;
4130 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4134 rms = regmatch->rm_so;
4135 rme = regmatch->rm_eo;
4137 err = expand_tab(&exstr, line);
4141 /* Split the line into 3 segments, according to match offsets. */
4142 seg0 = strndup(exstr, rms);
4144 err = got_error_from_errno("strndup");
4147 seg1 = strndup(exstr + rms, rme - rms);
4149 err = got_error_from_errno("strndup");
4152 seg2 = strdup(exstr + rme);
4154 err = got_error_from_errno("strndup");
4158 /* draw up to matched token if we haven't scrolled past it */
4159 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4163 n = MAX(width0 - skipcol, 0);
4166 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4167 wlimit, col_tab_align, 1);
4170 waddwstr(window, &wline[scrollx]);
4180 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4184 wlen = wcslen(wline);
4186 width = wcwidth(wline[i]);
4188 /* should not happen, tabs are expanded */
4189 err = got_error(GOT_ERR_RANGE);
4192 if (width0 + w + width > skipcol)
4197 /* draw (visible part of) matched token (if scrolled into it) */
4198 if (width1 - w > 0) {
4199 wattron(window, A_STANDOUT);
4200 waddwstr(window, &wline[i]);
4201 wattroff(window, A_STANDOUT);
4202 wlimit -= (width1 - w);
4203 *wtotal += (width1 - w);
4207 if (wlimit > 0) { /* draw rest of line */
4209 if (skipcol > width0 + width1) {
4210 err = format_line(&wline, &width2, &scrollx, seg2,
4211 skipcol - (width0 + width1), wlimit,
4215 waddwstr(window, &wline[scrollx]);
4217 err = format_line(&wline, &width2, NULL, seg2, 0,
4218 wlimit, col_tab_align, 1);
4221 waddwstr(window, wline);
4235 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4238 int *eof, *first, *selected;
4240 if (view->type == TOG_VIEW_DIFF) {
4241 struct tog_diff_view_state *s = &view->state.diff;
4243 first = &s->first_displayed_line;
4247 } else if (view->type == TOG_VIEW_HELP) {
4248 struct tog_help_view_state *s = &view->state.help;
4250 first = &s->first_displayed_line;
4254 } else if (view->type == TOG_VIEW_BLAME) {
4255 struct tog_blame_view_state *s = &view->state.blame;
4257 first = &s->first_displayed_line;
4258 selected = &s->selected_line;
4264 /* Center gline in the middle of the page like vi(1). */
4265 if (*lineno < view->gline - (view->nlines - 3) / 2)
4267 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4276 *selected = view->gline <= (view->nlines - 3) / 2 ?
4277 view->gline : (view->nlines - 3) / 2 + 1;
4283 static const struct got_error *
4284 draw_file(struct tog_view *view, const char *header)
4286 struct tog_diff_view_state *s = &view->state.diff;
4287 regmatch_t *regmatch = &view->regmatch;
4288 const struct got_error *err;
4291 size_t linesize = 0;
4295 int max_lines = view->nlines;
4296 int nlines = s->nlines;
4299 s->lineno = s->first_displayed_line - 1;
4300 line_offset = s->lines[s->first_displayed_line - 1].offset;
4301 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4302 return got_error_from_errno("fseek");
4304 werase(view->window);
4306 if (view->gline > s->nlines - 1)
4307 view->gline = s->nlines - 1;
4310 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4311 1 : view->gline - (view->nlines - 3) / 2 :
4312 s->lineno + s->selected_line;
4314 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4315 return got_error_from_errno("asprintf");
4316 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4322 if (view_needs_focus_indication(view))
4323 wstandout(view->window);
4324 waddwstr(view->window, wline);
4327 while (width++ < view->ncols)
4328 waddch(view->window, ' ');
4329 if (view_needs_focus_indication(view))
4330 wstandend(view->window);
4340 while (max_lines > 0 && nprinted < max_lines) {
4341 enum got_diff_line_type linetype;
4344 linelen = getline(&line, &linesize, s->f);
4345 if (linelen == -1) {
4351 return got_ferror(s->f, GOT_ERR_IO);
4354 if (++s->lineno < s->first_displayed_line)
4356 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4358 if (s->lineno == view->hiline)
4361 /* Set view->maxx based on full line length. */
4362 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4368 view->maxx = MAX(view->maxx, width);
4372 linetype = s->lines[s->lineno].type;
4373 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4374 linetype < GOT_DIFF_LINE_CONTEXT)
4375 attr |= COLOR_PAIR(linetype);
4377 wattron(view->window, attr);
4378 if (s->first_displayed_line + nprinted == s->matched_line &&
4379 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4380 err = add_matched_line(&width, line, view->ncols, 0,
4381 view->window, view->x, regmatch);
4388 err = format_line(&wline, &width, &skip, line,
4389 view->x, view->ncols, 0, view->x ? 1 : 0);
4394 waddwstr(view->window, &wline[skip]);
4398 if (s->lineno == view->hiline) {
4399 /* highlight full gline length */
4400 while (width++ < view->ncols)
4401 waddch(view->window, ' ');
4403 if (width <= view->ncols - 1)
4404 waddch(view->window, '\n');
4407 wattroff(view->window, attr);
4408 if (++nprinted == 1)
4409 s->first_displayed_line = s->lineno;
4413 s->last_displayed_line = s->first_displayed_line +
4416 s->last_displayed_line = s->first_displayed_line;
4421 while (nprinted < view->nlines) {
4422 waddch(view->window, '\n');
4426 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4432 wstandout(view->window);
4433 waddwstr(view->window, wline);
4436 wstandend(view->window);
4443 get_datestr(time_t *time, char *datebuf)
4445 struct tm mytm, *tm;
4448 tm = gmtime_r(time, &mytm);
4451 s = asctime_r(tm, datebuf);
4454 p = strchr(s, '\n');
4460 static const struct got_error *
4461 get_changed_paths(struct got_pathlist_head *paths,
4462 struct got_commit_object *commit, struct got_repository *repo)
4464 const struct got_error *err = NULL;
4465 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4466 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4467 struct got_object_qid *qid;
4469 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4471 struct got_commit_object *pcommit;
4472 err = got_object_open_as_commit(&pcommit, repo,
4477 tree_id1 = got_object_id_dup(
4478 got_object_commit_get_tree_id(pcommit));
4479 if (tree_id1 == NULL) {
4480 got_object_commit_close(pcommit);
4481 return got_error_from_errno("got_object_id_dup");
4483 got_object_commit_close(pcommit);
4488 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4493 tree_id2 = got_object_commit_get_tree_id(commit);
4494 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4498 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4499 got_diff_tree_collect_changed_paths, paths, 0);
4502 got_object_tree_close(tree1);
4504 got_object_tree_close(tree2);
4509 static const struct got_error *
4510 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4511 off_t off, uint8_t type)
4513 struct got_diff_line *p;
4515 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4517 return got_error_from_errno("reallocarray");
4519 (*lines)[*nlines].offset = off;
4520 (*lines)[*nlines].type = type;
4526 static const struct got_error *
4527 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4528 struct got_object_id *commit_id, struct got_reflist_head *refs,
4529 struct got_repository *repo, FILE *outfile)
4531 const struct got_error *err = NULL;
4532 char datebuf[26], *datestr;
4533 struct got_commit_object *commit;
4534 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4535 time_t committer_time;
4536 const char *author, *committer;
4537 char *refs_str = NULL;
4538 struct got_pathlist_head changed_paths;
4539 struct got_pathlist_entry *pe;
4543 TAILQ_INIT(&changed_paths);
4546 err = build_refs_str(&refs_str, refs, commit_id, repo);
4551 err = got_object_open_as_commit(&commit, repo, commit_id);
4555 err = got_object_id_str(&id_str, commit_id);
4557 err = got_error_from_errno("got_object_id_str");
4561 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4565 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4566 refs_str ? refs_str : "", refs_str ? ")" : "");
4568 err = got_error_from_errno("fprintf");
4572 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4576 n = fprintf(outfile, "from: %s\n",
4577 got_object_commit_get_author(commit));
4579 err = got_error_from_errno("fprintf");
4583 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4587 committer_time = got_object_commit_get_committer_time(commit);
4588 datestr = get_datestr(&committer_time, datebuf);
4590 n = fprintf(outfile, "date: %s UTC\n", datestr);
4592 err = got_error_from_errno("fprintf");
4596 err = add_line_metadata(lines, nlines, outoff,
4597 GOT_DIFF_LINE_DATE);
4601 author = got_object_commit_get_author(commit);
4602 committer = got_object_commit_get_committer(commit);
4603 if (strcmp(author, committer) != 0) {
4604 n = fprintf(outfile, "via: %s\n", committer);
4606 err = got_error_from_errno("fprintf");
4610 err = add_line_metadata(lines, nlines, outoff,
4611 GOT_DIFF_LINE_AUTHOR);
4615 if (got_object_commit_get_nparents(commit) > 1) {
4616 const struct got_object_id_queue *parent_ids;
4617 struct got_object_qid *qid;
4619 parent_ids = got_object_commit_get_parent_ids(commit);
4620 STAILQ_FOREACH(qid, parent_ids, entry) {
4621 err = got_object_id_str(&id_str, &qid->id);
4624 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4626 err = got_error_from_errno("fprintf");
4630 err = add_line_metadata(lines, nlines, outoff,
4631 GOT_DIFF_LINE_META);
4639 err = got_object_commit_get_logmsg(&logmsg, commit);
4643 while ((line = strsep(&s, "\n")) != NULL) {
4644 n = fprintf(outfile, "%s\n", line);
4646 err = got_error_from_errno("fprintf");
4650 err = add_line_metadata(lines, nlines, outoff,
4651 GOT_DIFF_LINE_LOGMSG);
4656 err = get_changed_paths(&changed_paths, commit, repo);
4659 TAILQ_FOREACH(pe, &changed_paths, entry) {
4660 struct got_diff_changed_path *cp = pe->data;
4661 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4663 err = got_error_from_errno("fprintf");
4667 err = add_line_metadata(lines, nlines, outoff,
4668 GOT_DIFF_LINE_CHANGES);
4671 free((char *)pe->path);
4675 fputc('\n', outfile);
4677 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4679 got_pathlist_free(&changed_paths);
4683 got_object_commit_close(commit);
4692 static const struct got_error *
4693 create_diff(struct tog_diff_view_state *s)
4695 const struct got_error *err = NULL;
4700 s->lines = malloc(sizeof(*s->lines));
4701 if (s->lines == NULL)
4702 return got_error_from_errno("malloc");
4707 err = got_error_from_errno("got_opentemp");
4710 if (s->f && fclose(s->f) == EOF) {
4711 err = got_error_from_errno("fclose");
4717 err = got_object_get_type(&obj_type, s->repo, s->id1);
4719 err = got_object_get_type(&obj_type, s->repo, s->id2);
4724 case GOT_OBJ_TYPE_BLOB:
4725 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4726 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4727 s->label1, s->label2, tog_diff_algo, s->diff_context,
4728 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4730 case GOT_OBJ_TYPE_TREE:
4731 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4732 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4733 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4734 s->force_text_diff, s->repo, s->f);
4736 case GOT_OBJ_TYPE_COMMIT: {
4737 const struct got_object_id_queue *parent_ids;
4738 struct got_object_qid *pid;
4739 struct got_commit_object *commit2;
4740 struct got_reflist_head *refs;
4742 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4745 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4746 /* Show commit info if we're diffing to a parent/root commit. */
4747 if (s->id1 == NULL) {
4748 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4749 refs, s->repo, s->f);
4753 parent_ids = got_object_commit_get_parent_ids(commit2);
4754 STAILQ_FOREACH(pid, parent_ids, entry) {
4755 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4756 err = write_commit_info(&s->lines,
4757 &s->nlines, s->id2, refs, s->repo,
4765 got_object_commit_close(commit2);
4767 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4768 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4769 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4770 s->force_text_diff, s->repo, s->f);
4774 err = got_error(GOT_ERR_OBJ_TYPE);
4778 if (s->f && fflush(s->f) != 0 && err == NULL)
4779 err = got_error_from_errno("fflush");
4784 diff_view_indicate_progress(struct tog_view *view)
4786 mvwaddstr(view->window, 0, 0, "diffing...");
4791 static const struct got_error *
4792 search_start_diff_view(struct tog_view *view)
4794 struct tog_diff_view_state *s = &view->state.diff;
4796 s->matched_line = 0;
4801 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4802 size_t *nlines, int **first, int **last, int **match, int **selected)
4804 struct tog_diff_view_state *s = &view->state.diff;
4807 *nlines = s->nlines;
4808 *line_offsets = NULL;
4809 *match = &s->matched_line;
4810 *first = &s->first_displayed_line;
4811 *last = &s->last_displayed_line;
4812 *selected = &s->selected_line;
4815 static const struct got_error *
4816 search_next_view_match(struct tog_view *view)
4818 const struct got_error *err = NULL;
4822 size_t linesize = 0;
4824 off_t *line_offsets;
4826 int *first, *last, *match, *selected;
4828 if (!view->search_setup)
4829 return got_error_msg(GOT_ERR_NOT_IMPL,
4830 "view search not supported");
4831 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4834 if (!view->searching) {
4835 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4840 if (view->searching == TOG_SEARCH_FORWARD)
4841 lineno = *match + 1;
4843 lineno = *match - 1;
4845 lineno = *first - 1 + *selected;
4850 if (lineno <= 0 || lineno > nlines) {
4852 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4856 if (view->searching == TOG_SEARCH_FORWARD)
4862 offset = view->type == TOG_VIEW_DIFF ?
4863 view->state.diff.lines[lineno - 1].offset :
4864 line_offsets[lineno - 1];
4865 if (fseeko(f, offset, SEEK_SET) != 0) {
4867 return got_error_from_errno("fseeko");
4869 linelen = getline(&line, &linesize, f);
4870 if (linelen != -1) {
4872 err = expand_tab(&exstr, line);
4875 if (match_line(exstr, &view->regex, 1,
4877 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4884 if (view->searching == TOG_SEARCH_FORWARD)
4899 static const struct got_error *
4900 close_diff_view(struct tog_view *view)
4902 const struct got_error *err = NULL;
4903 struct tog_diff_view_state *s = &view->state.diff;
4909 if (s->f && fclose(s->f) == EOF)
4910 err = got_error_from_errno("fclose");
4912 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4913 err = got_error_from_errno("fclose");
4915 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4916 err = got_error_from_errno("fclose");
4918 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4919 err = got_error_from_errno("close");
4921 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4922 err = got_error_from_errno("close");
4930 static const struct got_error *
4931 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4932 struct got_object_id *id2, const char *label1, const char *label2,
4933 int diff_context, int ignore_whitespace, int force_text_diff,
4934 struct tog_view *parent_view, struct got_repository *repo)
4936 const struct got_error *err;
4937 struct tog_diff_view_state *s = &view->state.diff;
4939 memset(s, 0, sizeof(*s));
4943 if (id1 != NULL && id2 != NULL) {
4945 err = got_object_get_type(&type1, repo, id1);
4948 err = got_object_get_type(&type2, repo, id2);
4953 return got_error(GOT_ERR_OBJ_TYPE);
4955 s->first_displayed_line = 1;
4956 s->last_displayed_line = view->nlines;
4957 s->selected_line = 1;
4965 s->id1 = got_object_id_dup(id1);
4967 return got_error_from_errno("got_object_id_dup");
4971 s->id2 = got_object_id_dup(id2);
4972 if (s->id2 == NULL) {
4973 err = got_error_from_errno("got_object_id_dup");
4977 s->f1 = got_opentemp();
4978 if (s->f1 == NULL) {
4979 err = got_error_from_errno("got_opentemp");
4983 s->f2 = got_opentemp();
4984 if (s->f2 == NULL) {
4985 err = got_error_from_errno("got_opentemp");
4989 s->fd1 = got_opentempfd();
4991 err = got_error_from_errno("got_opentempfd");
4995 s->fd2 = got_opentempfd();
4997 err = got_error_from_errno("got_opentempfd");
5001 s->first_displayed_line = 1;
5002 s->last_displayed_line = view->nlines;
5003 s->diff_context = diff_context;
5004 s->ignore_whitespace = ignore_whitespace;
5005 s->force_text_diff = force_text_diff;
5006 s->parent_view = parent_view;
5009 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5012 rc = init_pair(GOT_DIFF_LINE_MINUS,
5013 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5015 rc = init_pair(GOT_DIFF_LINE_PLUS,
5016 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5018 rc = init_pair(GOT_DIFF_LINE_HUNK,
5019 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5021 rc = init_pair(GOT_DIFF_LINE_META,
5022 get_color_value("TOG_COLOR_DIFF_META"), -1);
5024 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5025 get_color_value("TOG_COLOR_DIFF_META"), -1);
5027 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5028 get_color_value("TOG_COLOR_DIFF_META"), -1);
5030 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5031 get_color_value("TOG_COLOR_DIFF_META"), -1);
5033 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5034 get_color_value("TOG_COLOR_AUTHOR"), -1);
5036 rc = init_pair(GOT_DIFF_LINE_DATE,
5037 get_color_value("TOG_COLOR_DATE"), -1);
5039 err = got_error(GOT_ERR_RANGE);
5044 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5045 view_is_splitscreen(view))
5046 show_log_view(parent_view); /* draw border */
5047 diff_view_indicate_progress(view);
5049 err = create_diff(s);
5051 view->show = show_diff_view;
5052 view->input = input_diff_view;
5053 view->reset = reset_diff_view;
5054 view->close = close_diff_view;
5055 view->search_start = search_start_diff_view;
5056 view->search_setup = search_setup_diff_view;
5057 view->search_next = search_next_view_match;
5060 close_diff_view(view);
5064 static const struct got_error *
5065 show_diff_view(struct tog_view *view)
5067 const struct got_error *err;
5068 struct tog_diff_view_state *s = &view->state.diff;
5069 char *id_str1 = NULL, *id_str2, *header;
5070 const char *label1, *label2;
5073 err = got_object_id_str(&id_str1, s->id1);
5076 label1 = s->label1 ? s->label1 : id_str1;
5078 label1 = "/dev/null";
5080 err = got_object_id_str(&id_str2, s->id2);
5083 label2 = s->label2 ? s->label2 : id_str2;
5085 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5086 err = got_error_from_errno("asprintf");
5094 err = draw_file(view, header);
5099 static const struct got_error *
5100 set_selected_commit(struct tog_diff_view_state *s,
5101 struct commit_queue_entry *entry)
5103 const struct got_error *err;
5104 const struct got_object_id_queue *parent_ids;
5105 struct got_commit_object *selected_commit;
5106 struct got_object_qid *pid;
5109 s->id2 = got_object_id_dup(entry->id);
5111 return got_error_from_errno("got_object_id_dup");
5113 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5116 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5118 pid = STAILQ_FIRST(parent_ids);
5119 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5120 got_object_commit_close(selected_commit);
5124 static const struct got_error *
5125 reset_diff_view(struct tog_view *view)
5127 struct tog_diff_view_state *s = &view->state.diff;
5130 wclear(view->window);
5131 s->first_displayed_line = 1;
5132 s->last_displayed_line = view->nlines;
5133 s->matched_line = 0;
5134 diff_view_indicate_progress(view);
5135 return create_diff(s);
5139 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5143 i = start = s->first_displayed_line - 1;
5145 while (s->lines[i].type != type) {
5149 return; /* do nothing, requested type not in file */
5152 s->selected_line = 1;
5153 s->first_displayed_line = i;
5157 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5161 i = start = s->first_displayed_line + 1;
5163 while (s->lines[i].type != type) {
5164 if (i == s->nlines - 1)
5167 return; /* do nothing, requested type not in file */
5170 s->selected_line = 1;
5171 s->first_displayed_line = i;
5174 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5176 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5179 static const struct got_error *
5180 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5182 const struct got_error *err = NULL;
5183 struct tog_diff_view_state *s = &view->state.diff;
5184 struct tog_log_view_state *ls;
5185 struct commit_queue_entry *old_selected_entry;
5187 size_t linesize = 0;
5189 int i, nscroll = view->nlines - 1, up = 0;
5191 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5198 view->x = MAX(view->maxx - view->ncols / 3, 0);
5203 if (view->x + view->ncols / 3 < view->maxx)
5204 view->x += 2; /* move two columns right */
5210 view->x -= MIN(view->x, 2); /* move two columns back */
5217 s->force_text_diff = !s->force_text_diff;
5219 s->ignore_whitespace = !s->ignore_whitespace;
5220 err = reset_diff_view(view);
5224 s->first_displayed_line = 1;
5233 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5239 if (s->first_displayed_line > 1)
5240 s->first_displayed_line--;
5251 if (s->first_displayed_line == 1) {
5256 while (i++ < nscroll && s->first_displayed_line > 1)
5257 s->first_displayed_line--;
5263 s->first_displayed_line++;
5280 while (!s->eof && i++ < nscroll) {
5281 linelen = getline(&line, &linesize, s->f);
5282 s->first_displayed_line++;
5283 if (linelen == -1) {
5287 err = got_ferror(s->f, GOT_ERR_IO);
5294 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5297 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5300 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5303 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5306 if (s->diff_context > 0) {
5308 s->matched_line = 0;
5309 diff_view_indicate_progress(view);
5310 err = create_diff(s);
5311 if (s->first_displayed_line + view->nlines - 1 >
5313 s->first_displayed_line = 1;
5314 s->last_displayed_line = view->nlines;
5320 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5322 s->matched_line = 0;
5323 diff_view_indicate_progress(view);
5324 err = create_diff(s);
5336 if (s->parent_view == NULL) {
5340 s->parent_view->count = view->count;
5342 if (s->parent_view->type == TOG_VIEW_LOG) {
5343 ls = &s->parent_view->state.log;
5344 old_selected_entry = ls->selected_entry;
5346 err = input_log_view(NULL, s->parent_view,
5347 up ? KEY_UP : KEY_DOWN);
5350 view->count = s->parent_view->count;
5352 if (old_selected_entry == ls->selected_entry)
5355 err = set_selected_commit(s, ls->selected_entry);
5358 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5359 struct tog_blame_view_state *bs;
5360 struct got_object_id *id, *prev_id;
5362 bs = &s->parent_view->state.blame;
5363 prev_id = get_annotation_for_line(bs->blame.lines,
5364 bs->blame.nlines, bs->last_diffed_line);
5366 err = input_blame_view(&view, s->parent_view,
5367 up ? KEY_UP : KEY_DOWN);
5370 view->count = s->parent_view->count;
5372 if (prev_id == NULL)
5374 id = get_selected_commit_id(bs->blame.lines,
5375 bs->blame.nlines, bs->first_displayed_line,
5380 if (!got_object_id_cmp(prev_id, id))
5383 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5387 s->first_displayed_line = 1;
5388 s->last_displayed_line = view->nlines;
5389 s->matched_line = 0;
5392 diff_view_indicate_progress(view);
5393 err = create_diff(s);
5403 static const struct got_error *
5404 cmd_diff(int argc, char *argv[])
5406 const struct got_error *error = NULL;
5407 struct got_repository *repo = NULL;
5408 struct got_worktree *worktree = NULL;
5409 struct got_object_id *id1 = NULL, *id2 = NULL;
5410 char *repo_path = NULL, *cwd = NULL;
5411 char *id_str1 = NULL, *id_str2 = NULL;
5412 char *label1 = NULL, *label2 = NULL;
5413 int diff_context = 3, ignore_whitespace = 0;
5414 int ch, force_text_diff = 0;
5416 struct tog_view *view;
5417 int *pack_fds = NULL;
5419 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5422 force_text_diff = 1;
5425 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5428 errx(1, "number of context lines is %s: %s",
5432 repo_path = realpath(optarg, NULL);
5433 if (repo_path == NULL)
5434 return got_error_from_errno2("realpath",
5436 got_path_strip_trailing_slashes(repo_path);
5439 ignore_whitespace = 1;
5451 usage_diff(); /* TODO show local worktree changes */
5452 } else if (argc == 2) {
5458 error = got_repo_pack_fds_open(&pack_fds);
5462 if (repo_path == NULL) {
5463 cwd = getcwd(NULL, 0);
5465 return got_error_from_errno("getcwd");
5466 error = got_worktree_open(&worktree, cwd);
5467 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5471 strdup(got_worktree_get_repo_path(worktree));
5473 repo_path = strdup(cwd);
5474 if (repo_path == NULL) {
5475 error = got_error_from_errno("strdup");
5480 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5486 error = apply_unveil(got_repo_get_path(repo), NULL);
5490 error = tog_load_refs(repo, 0);
5494 error = got_repo_match_object_id(&id1, &label1, id_str1,
5495 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5499 error = got_repo_match_object_id(&id2, &label2, id_str2,
5500 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5504 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5506 error = got_error_from_errno("view_open");
5509 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5510 ignore_whitespace, force_text_diff, NULL, repo);
5513 error = view_loop(view);
5520 const struct got_error *close_err = got_repo_close(repo);
5525 got_worktree_close(worktree);
5527 const struct got_error *pack_err =
5528 got_repo_pack_fds_close(pack_fds);
5541 "usage: %s blame [-c commit] [-r repository-path] path\n",
5546 struct tog_blame_line {
5548 struct got_object_id *id;
5551 static const struct got_error *
5552 draw_blame(struct tog_view *view)
5554 struct tog_blame_view_state *s = &view->state.blame;
5555 struct tog_blame *blame = &s->blame;
5556 regmatch_t *regmatch = &view->regmatch;
5557 const struct got_error *err;
5558 int lineno = 0, nprinted = 0;
5560 size_t linesize = 0;
5564 struct tog_blame_line *blame_line;
5565 struct got_object_id *prev_id = NULL;
5567 struct tog_color *tc;
5569 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5574 werase(view->window);
5576 if (asprintf(&line, "commit %s", id_str) == -1) {
5577 err = got_error_from_errno("asprintf");
5582 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5587 if (view_needs_focus_indication(view))
5588 wstandout(view->window);
5589 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5591 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5592 waddwstr(view->window, wline);
5593 while (width++ < view->ncols)
5594 waddch(view->window, ' ');
5596 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5597 if (view_needs_focus_indication(view))
5598 wstandend(view->window);
5602 if (view->gline > blame->nlines)
5603 view->gline = blame->nlines;
5605 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5606 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5607 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5609 return got_error_from_errno("asprintf");
5612 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5617 waddwstr(view->window, wline);
5620 if (width < view->ncols - 1)
5621 waddch(view->window, '\n');
5625 while (nprinted < view->nlines - 2) {
5626 linelen = getline(&line, &linesize, blame->f);
5627 if (linelen == -1) {
5628 if (feof(blame->f)) {
5633 return got_ferror(blame->f, GOT_ERR_IO);
5635 if (++lineno < s->first_displayed_line)
5637 if (view->gline && !gotoline(view, &lineno, &nprinted))
5640 /* Set view->maxx based on full line length. */
5641 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5648 view->maxx = MAX(view->maxx, width);
5650 if (nprinted == s->selected_line - 1)
5651 wstandout(view->window);
5653 if (blame->nlines > 0) {
5654 blame_line = &blame->lines[lineno - 1];
5655 if (blame_line->annotated && prev_id &&
5656 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5657 !(nprinted == s->selected_line - 1)) {
5658 waddstr(view->window, " ");
5659 } else if (blame_line->annotated) {
5661 err = got_object_id_str(&id_str,
5667 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5669 wattr_on(view->window,
5670 COLOR_PAIR(tc->colorpair), NULL);
5671 wprintw(view->window, "%.8s", id_str);
5673 wattr_off(view->window,
5674 COLOR_PAIR(tc->colorpair), NULL);
5676 prev_id = blame_line->id;
5678 waddstr(view->window, "........");
5682 waddstr(view->window, "........");
5686 if (nprinted == s->selected_line - 1)
5687 wstandend(view->window);
5688 waddstr(view->window, " ");
5690 if (view->ncols <= 9) {
5692 } else if (s->first_displayed_line + nprinted ==
5694 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5695 err = add_matched_line(&width, line, view->ncols - 9, 9,
5696 view->window, view->x, regmatch);
5704 err = format_line(&wline, &width, &skip, line,
5705 view->x, view->ncols - 9, 9, 1);
5710 waddwstr(view->window, &wline[skip]);
5716 if (width <= view->ncols - 1)
5717 waddch(view->window, '\n');
5718 if (++nprinted == 1)
5719 s->first_displayed_line = lineno;
5722 s->last_displayed_line = lineno;
5729 static const struct got_error *
5730 blame_cb(void *arg, int nlines, int lineno,
5731 struct got_commit_object *commit, struct got_object_id *id)
5733 const struct got_error *err = NULL;
5734 struct tog_blame_cb_args *a = arg;
5735 struct tog_blame_line *line;
5738 if (nlines != a->nlines ||
5739 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5740 return got_error(GOT_ERR_RANGE);
5742 errcode = pthread_mutex_lock(&tog_mutex);
5744 return got_error_set_errno(errcode, "pthread_mutex_lock");
5746 if (*a->quit) { /* user has quit the blame view */
5747 err = got_error(GOT_ERR_ITER_COMPLETED);
5752 goto done; /* no change in this commit */
5754 line = &a->lines[lineno - 1];
5755 if (line->annotated)
5758 line->id = got_object_id_dup(id);
5759 if (line->id == NULL) {
5760 err = got_error_from_errno("got_object_id_dup");
5763 line->annotated = 1;
5765 errcode = pthread_mutex_unlock(&tog_mutex);
5767 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5772 blame_thread(void *arg)
5774 const struct got_error *err, *close_err;
5775 struct tog_blame_thread_args *ta = arg;
5776 struct tog_blame_cb_args *a = ta->cb_args;
5777 int errcode, fd1 = -1, fd2 = -1;
5778 FILE *f1 = NULL, *f2 = NULL;
5780 fd1 = got_opentempfd();
5782 return (void *)got_error_from_errno("got_opentempfd");
5784 fd2 = got_opentempfd();
5786 err = got_error_from_errno("got_opentempfd");
5790 f1 = got_opentemp();
5792 err = (void *)got_error_from_errno("got_opentemp");
5795 f2 = got_opentemp();
5797 err = (void *)got_error_from_errno("got_opentemp");
5801 err = block_signals_used_by_main_thread();
5805 err = got_blame(ta->path, a->commit_id, ta->repo,
5806 tog_diff_algo, blame_cb, ta->cb_args,
5807 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5808 if (err && err->code == GOT_ERR_CANCELLED)
5811 errcode = pthread_mutex_lock(&tog_mutex);
5813 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5817 close_err = got_repo_close(ta->repo);
5823 errcode = pthread_mutex_unlock(&tog_mutex);
5824 if (errcode && err == NULL)
5825 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5828 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5829 err = got_error_from_errno("close");
5830 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5831 err = got_error_from_errno("close");
5832 if (f1 && fclose(f1) == EOF && err == NULL)
5833 err = got_error_from_errno("fclose");
5834 if (f2 && fclose(f2) == EOF && err == NULL)
5835 err = got_error_from_errno("fclose");
5840 static struct got_object_id *
5841 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5842 int first_displayed_line, int selected_line)
5844 struct tog_blame_line *line;
5849 line = &lines[first_displayed_line - 1 + selected_line - 1];
5850 if (!line->annotated)
5856 static struct got_object_id *
5857 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5860 struct tog_blame_line *line;
5862 if (nlines <= 0 || lineno >= nlines)
5865 line = &lines[lineno - 1];
5866 if (!line->annotated)
5872 static const struct got_error *
5873 stop_blame(struct tog_blame *blame)
5875 const struct got_error *err = NULL;
5878 if (blame->thread) {
5880 errcode = pthread_mutex_unlock(&tog_mutex);
5882 return got_error_set_errno(errcode,
5883 "pthread_mutex_unlock");
5884 errcode = pthread_join(blame->thread, (void **)&err);
5886 return got_error_set_errno(errcode, "pthread_join");
5887 errcode = pthread_mutex_lock(&tog_mutex);
5889 return got_error_set_errno(errcode,
5890 "pthread_mutex_lock");
5891 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5893 blame->thread = 0; //NULL;
5895 if (blame->thread_args.repo) {
5896 const struct got_error *close_err;
5897 close_err = got_repo_close(blame->thread_args.repo);
5900 blame->thread_args.repo = NULL;
5903 if (fclose(blame->f) == EOF && err == NULL)
5904 err = got_error_from_errno("fclose");
5908 for (i = 0; i < blame->nlines; i++)
5909 free(blame->lines[i].id);
5911 blame->lines = NULL;
5913 free(blame->cb_args.commit_id);
5914 blame->cb_args.commit_id = NULL;
5915 if (blame->pack_fds) {
5916 const struct got_error *pack_err =
5917 got_repo_pack_fds_close(blame->pack_fds);
5920 blame->pack_fds = NULL;
5925 static const struct got_error *
5926 cancel_blame_view(void *arg)
5928 const struct got_error *err = NULL;
5932 errcode = pthread_mutex_lock(&tog_mutex);
5934 return got_error_set_errno(errcode,
5935 "pthread_mutex_unlock");
5938 err = got_error(GOT_ERR_CANCELLED);
5940 errcode = pthread_mutex_unlock(&tog_mutex);
5942 return got_error_set_errno(errcode,
5943 "pthread_mutex_lock");
5948 static const struct got_error *
5949 run_blame(struct tog_view *view)
5951 struct tog_blame_view_state *s = &view->state.blame;
5952 struct tog_blame *blame = &s->blame;
5953 const struct got_error *err = NULL;
5954 struct got_commit_object *commit = NULL;
5955 struct got_blob_object *blob = NULL;
5956 struct got_repository *thread_repo = NULL;
5957 struct got_object_id *obj_id = NULL;
5958 int obj_type, fd = -1;
5959 int *pack_fds = NULL;
5961 err = got_object_open_as_commit(&commit, s->repo,
5962 &s->blamed_commit->id);
5966 fd = got_opentempfd();
5968 err = got_error_from_errno("got_opentempfd");
5972 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5976 err = got_object_get_type(&obj_type, s->repo, obj_id);
5980 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5981 err = got_error(GOT_ERR_OBJ_TYPE);
5985 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5988 blame->f = got_opentemp();
5989 if (blame->f == NULL) {
5990 err = got_error_from_errno("got_opentemp");
5993 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5994 &blame->line_offsets, blame->f, blob);
5997 if (blame->nlines == 0) {
5998 s->blame_complete = 1;
6002 /* Don't include \n at EOF in the blame line count. */
6003 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6006 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6007 if (blame->lines == NULL) {
6008 err = got_error_from_errno("calloc");
6012 err = got_repo_pack_fds_open(&pack_fds);
6015 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6020 blame->pack_fds = pack_fds;
6021 blame->cb_args.view = view;
6022 blame->cb_args.lines = blame->lines;
6023 blame->cb_args.nlines = blame->nlines;
6024 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6025 if (blame->cb_args.commit_id == NULL) {
6026 err = got_error_from_errno("got_object_id_dup");
6029 blame->cb_args.quit = &s->done;
6031 blame->thread_args.path = s->path;
6032 blame->thread_args.repo = thread_repo;
6033 blame->thread_args.cb_args = &blame->cb_args;
6034 blame->thread_args.complete = &s->blame_complete;
6035 blame->thread_args.cancel_cb = cancel_blame_view;
6036 blame->thread_args.cancel_arg = &s->done;
6037 s->blame_complete = 0;
6039 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6040 s->first_displayed_line = 1;
6041 s->last_displayed_line = view->nlines;
6042 s->selected_line = 1;
6044 s->matched_line = 0;
6048 got_object_commit_close(commit);
6049 if (fd != -1 && close(fd) == -1 && err == NULL)
6050 err = got_error_from_errno("close");
6052 got_object_blob_close(blob);
6059 static const struct got_error *
6060 open_blame_view(struct tog_view *view, char *path,
6061 struct got_object_id *commit_id, struct got_repository *repo)
6063 const struct got_error *err = NULL;
6064 struct tog_blame_view_state *s = &view->state.blame;
6066 STAILQ_INIT(&s->blamed_commits);
6068 s->path = strdup(path);
6069 if (s->path == NULL)
6070 return got_error_from_errno("strdup");
6072 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6078 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6079 s->first_displayed_line = 1;
6080 s->last_displayed_line = view->nlines;
6081 s->selected_line = 1;
6082 s->blame_complete = 0;
6084 s->commit_id = commit_id;
6085 memset(&s->blame, 0, sizeof(s->blame));
6087 STAILQ_INIT(&s->colors);
6088 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6089 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6090 get_color_value("TOG_COLOR_COMMIT"));
6095 view->show = show_blame_view;
6096 view->input = input_blame_view;
6097 view->reset = reset_blame_view;
6098 view->close = close_blame_view;
6099 view->search_start = search_start_blame_view;
6100 view->search_setup = search_setup_blame_view;
6101 view->search_next = search_next_view_match;
6103 return run_blame(view);
6106 static const struct got_error *
6107 close_blame_view(struct tog_view *view)
6109 const struct got_error *err = NULL;
6110 struct tog_blame_view_state *s = &view->state.blame;
6112 if (s->blame.thread)
6113 err = stop_blame(&s->blame);
6115 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6116 struct got_object_qid *blamed_commit;
6117 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6118 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6119 got_object_qid_free(blamed_commit);
6123 free_colors(&s->colors);
6127 static const struct got_error *
6128 search_start_blame_view(struct tog_view *view)
6130 struct tog_blame_view_state *s = &view->state.blame;
6132 s->matched_line = 0;
6137 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6138 size_t *nlines, int **first, int **last, int **match, int **selected)
6140 struct tog_blame_view_state *s = &view->state.blame;
6143 *nlines = s->blame.nlines;
6144 *line_offsets = s->blame.line_offsets;
6145 *match = &s->matched_line;
6146 *first = &s->first_displayed_line;
6147 *last = &s->last_displayed_line;
6148 *selected = &s->selected_line;
6151 static const struct got_error *
6152 show_blame_view(struct tog_view *view)
6154 const struct got_error *err = NULL;
6155 struct tog_blame_view_state *s = &view->state.blame;
6158 if (s->blame.thread == 0 && !s->blame_complete) {
6159 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6160 &s->blame.thread_args);
6162 return got_error_set_errno(errcode, "pthread_create");
6164 halfdelay(1); /* fast refresh while annotating */
6167 if (s->blame_complete)
6168 halfdelay(10); /* disable fast refresh */
6170 err = draw_blame(view);
6176 static const struct got_error *
6177 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6178 struct got_repository *repo, struct got_object_id *id)
6180 struct tog_view *log_view;
6181 const struct got_error *err = NULL;
6185 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6186 if (log_view == NULL)
6187 return got_error_from_errno("view_open");
6189 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6191 view_close(log_view);
6193 *new_view = log_view;
6198 static const struct got_error *
6199 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6201 const struct got_error *err = NULL, *thread_err = NULL;
6202 struct tog_view *diff_view;
6203 struct tog_blame_view_state *s = &view->state.blame;
6204 int eos, nscroll, begin_y = 0, begin_x = 0;
6206 eos = nscroll = view->nlines - 2;
6207 if (view_is_hsplit_top(view))
6215 view->x = MAX(view->maxx - view->ncols / 3, 0);
6220 if (view->x + view->ncols / 3 < view->maxx)
6221 view->x += 2; /* move two columns right */
6227 view->x -= MIN(view->x, 2); /* move two columns back */
6236 s->selected_line = 1;
6237 s->first_displayed_line = 1;
6242 if (s->blame.nlines < eos) {
6243 s->selected_line = s->blame.nlines;
6244 s->first_displayed_line = 1;
6246 s->selected_line = eos;
6247 s->first_displayed_line = s->blame.nlines - (eos - 1);
6254 if (s->selected_line > 1)
6256 else if (s->selected_line == 1 &&
6257 s->first_displayed_line > 1)
6258 s->first_displayed_line--;
6269 if (s->first_displayed_line == 1) {
6270 if (view->count > 1)
6272 s->selected_line = MAX(1, s->selected_line - nscroll);
6276 if (s->first_displayed_line > nscroll)
6277 s->first_displayed_line -= nscroll;
6279 s->first_displayed_line = 1;
6284 if (s->selected_line < eos && s->first_displayed_line +
6285 s->selected_line <= s->blame.nlines)
6287 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6288 s->first_displayed_line++;
6294 struct got_object_id *id = NULL;
6297 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6298 s->first_displayed_line, s->selected_line);
6302 struct got_commit_object *commit, *pcommit;
6303 struct got_object_qid *pid;
6304 struct got_object_id *blob_id = NULL;
6306 err = got_object_open_as_commit(&commit,
6311 got_object_commit_get_parent_ids(commit));
6313 got_object_commit_close(commit);
6316 /* Check if path history ends here. */
6317 err = got_object_open_as_commit(&pcommit,
6321 err = got_object_id_by_path(&blob_id, s->repo,
6323 got_object_commit_close(pcommit);
6325 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6327 got_object_commit_close(commit);
6330 err = got_object_get_type(&obj_type, s->repo,
6333 /* Can't blame non-blob type objects. */
6334 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6335 got_object_commit_close(commit);
6338 err = got_object_qid_alloc(&s->blamed_commit,
6340 got_object_commit_close(commit);
6342 if (got_object_id_cmp(id,
6343 &s->blamed_commit->id) == 0)
6345 err = got_object_qid_alloc(&s->blamed_commit,
6351 thread_err = stop_blame(&s->blame);
6355 STAILQ_INSERT_HEAD(&s->blamed_commits,
6356 s->blamed_commit, entry);
6357 err = run_blame(view);
6363 struct got_object_qid *first;
6366 first = STAILQ_FIRST(&s->blamed_commits);
6367 if (!got_object_id_cmp(&first->id, s->commit_id))
6370 thread_err = stop_blame(&s->blame);
6374 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6375 got_object_qid_free(s->blamed_commit);
6377 STAILQ_FIRST(&s->blamed_commits);
6378 err = run_blame(view);
6385 s->id_to_log = get_selected_commit_id(s->blame.lines,
6386 s->blame.nlines, s->first_displayed_line, s->selected_line);
6388 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6392 struct got_object_id *id = NULL;
6393 struct got_object_qid *pid;
6394 struct got_commit_object *commit = NULL;
6397 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6398 s->first_displayed_line, s->selected_line);
6401 err = got_object_open_as_commit(&commit, s->repo, id);
6404 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6406 /* traversed from diff view, release diff resources */
6407 err = close_diff_view(*new_view);
6410 diff_view = *new_view;
6412 if (view_is_parent_view(view))
6413 view_get_split(view, &begin_y, &begin_x);
6415 diff_view = view_open(0, 0, begin_y, begin_x,
6417 if (diff_view == NULL) {
6418 got_object_commit_close(commit);
6419 err = got_error_from_errno("view_open");
6423 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6424 id, NULL, NULL, 3, 0, 0, view, s->repo);
6425 got_object_commit_close(commit);
6427 view_close(diff_view);
6430 s->last_diffed_line = s->first_displayed_line - 1 +
6433 break; /* still open from active diff view */
6434 if (view_is_parent_view(view) &&
6435 view->mode == TOG_VIEW_SPLIT_HRZN) {
6436 err = view_init_hsplit(view, begin_y);
6442 diff_view->focussed = 1;
6443 diff_view->mode = view->mode;
6444 diff_view->nlines = view->lines - begin_y;
6445 if (view_is_parent_view(view)) {
6446 view_transfer_size(diff_view, view);
6447 err = view_close_child(view);
6450 err = view_set_child(view, diff_view);
6453 view->focus_child = 1;
6455 *new_view = diff_view;
6468 if (s->last_displayed_line >= s->blame.nlines &&
6469 s->selected_line >= MIN(s->blame.nlines,
6470 view->nlines - 2)) {
6474 if (s->last_displayed_line >= s->blame.nlines &&
6475 s->selected_line < view->nlines - 2) {
6477 MIN(nscroll, s->last_displayed_line -
6478 s->first_displayed_line - s->selected_line + 1);
6480 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6481 s->first_displayed_line += nscroll;
6483 s->first_displayed_line =
6484 s->blame.nlines - (view->nlines - 3);
6487 if (s->selected_line > view->nlines - 2) {
6488 s->selected_line = MIN(s->blame.nlines,
6496 return thread_err ? thread_err : err;
6499 static const struct got_error *
6500 reset_blame_view(struct tog_view *view)
6502 const struct got_error *err;
6503 struct tog_blame_view_state *s = &view->state.blame;
6507 err = stop_blame(&s->blame);
6511 return run_blame(view);
6514 static const struct got_error *
6515 cmd_blame(int argc, char *argv[])
6517 const struct got_error *error;
6518 struct got_repository *repo = NULL;
6519 struct got_worktree *worktree = NULL;
6520 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6521 char *link_target = NULL;
6522 struct got_object_id *commit_id = NULL;
6523 struct got_commit_object *commit = NULL;
6524 char *commit_id_str = NULL;
6526 struct tog_view *view;
6527 int *pack_fds = NULL;
6529 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6532 commit_id_str = optarg;
6535 repo_path = realpath(optarg, NULL);
6536 if (repo_path == NULL)
6537 return got_error_from_errno2("realpath",
6552 error = got_repo_pack_fds_open(&pack_fds);
6556 if (repo_path == NULL) {
6557 cwd = getcwd(NULL, 0);
6559 return got_error_from_errno("getcwd");
6560 error = got_worktree_open(&worktree, cwd);
6561 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6565 strdup(got_worktree_get_repo_path(worktree));
6567 repo_path = strdup(cwd);
6568 if (repo_path == NULL) {
6569 error = got_error_from_errno("strdup");
6574 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6578 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6585 error = apply_unveil(got_repo_get_path(repo), NULL);
6589 error = tog_load_refs(repo, 0);
6593 if (commit_id_str == NULL) {
6594 struct got_reference *head_ref;
6595 error = got_ref_open(&head_ref, repo, worktree ?
6596 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6599 error = got_ref_resolve(&commit_id, repo, head_ref);
6600 got_ref_close(head_ref);
6602 error = got_repo_match_object_id(&commit_id, NULL,
6603 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6608 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6610 error = got_error_from_errno("view_open");
6614 error = got_object_open_as_commit(&commit, repo, commit_id);
6618 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6623 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6628 /* Release work tree lock. */
6629 got_worktree_close(worktree);
6632 error = view_loop(view);
6640 got_object_commit_close(commit);
6642 got_worktree_close(worktree);
6644 const struct got_error *close_err = got_repo_close(repo);
6649 const struct got_error *pack_err =
6650 got_repo_pack_fds_close(pack_fds);
6658 static const struct got_error *
6659 draw_tree_entries(struct tog_view *view, const char *parent_path)
6661 struct tog_tree_view_state *s = &view->state.tree;
6662 const struct got_error *err = NULL;
6663 struct got_tree_entry *te;
6666 struct tog_color *tc;
6667 int width, n, nentries, i = 1;
6668 int limit = view->nlines;
6671 if (view_is_hsplit_top(view))
6672 --limit; /* border */
6674 werase(view->window);
6679 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6683 if (view_needs_focus_indication(view))
6684 wstandout(view->window);
6685 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6687 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6688 waddwstr(view->window, wline);
6691 while (width++ < view->ncols)
6692 waddch(view->window, ' ');
6694 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6695 if (view_needs_focus_indication(view))
6696 wstandend(view->window);
6701 if (s->first_displayed_entry) {
6702 i += got_tree_entry_get_index(s->first_displayed_entry);
6703 if (s->tree != s->root)
6704 ++i; /* account for ".." entry */
6706 nentries = got_object_tree_get_nentries(s->tree);
6707 if (asprintf(&index, "[%d/%d] %s",
6708 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6709 return got_error_from_errno("asprintf");
6710 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6714 waddwstr(view->window, wline);
6717 if (width < view->ncols - 1)
6718 waddch(view->window, '\n');
6721 waddch(view->window, '\n');
6725 if (s->first_displayed_entry == NULL) {
6726 te = got_object_tree_get_first_entry(s->tree);
6727 if (s->selected == 0) {
6729 wstandout(view->window);
6730 s->selected_entry = NULL;
6732 waddstr(view->window, " ..\n"); /* parent directory */
6733 if (s->selected == 0 && view->focussed)
6734 wstandend(view->window);
6741 te = s->first_displayed_entry;
6744 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6745 char *line = NULL, *id_str = NULL, *link_target = NULL;
6746 const char *modestr = "";
6749 te = got_object_tree_get_entry(s->tree, i);
6750 mode = got_tree_entry_get_mode(te);
6753 err = got_object_id_str(&id_str,
6754 got_tree_entry_get_id(te));
6756 return got_error_from_errno(
6757 "got_object_id_str");
6759 if (got_object_tree_entry_is_submodule(te))
6761 else if (S_ISLNK(mode)) {
6764 err = got_tree_entry_get_symlink_target(&link_target,
6770 for (i = 0; i < strlen(link_target); i++) {
6771 if (!isprint((unsigned char)link_target[i]))
6772 link_target[i] = '?';
6776 else if (S_ISDIR(mode))
6778 else if (mode & S_IXUSR)
6780 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6781 got_tree_entry_get_name(te), modestr,
6782 link_target ? " -> ": "",
6783 link_target ? link_target : "") == -1) {
6786 return got_error_from_errno("asprintf");
6790 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6796 if (n == s->selected) {
6798 wstandout(view->window);
6799 s->selected_entry = te;
6801 tc = match_color(&s->colors, line);
6803 wattr_on(view->window,
6804 COLOR_PAIR(tc->colorpair), NULL);
6805 waddwstr(view->window, wline);
6807 wattr_off(view->window,
6808 COLOR_PAIR(tc->colorpair), NULL);
6809 if (width < view->ncols - 1)
6810 waddch(view->window, '\n');
6811 if (n == s->selected && view->focussed)
6812 wstandend(view->window);
6818 s->last_displayed_entry = te;
6827 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6829 struct got_tree_entry *te;
6830 int isroot = s->tree == s->root;
6833 if (s->first_displayed_entry == NULL)
6836 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6837 while (i++ < maxscroll) {
6840 s->first_displayed_entry = NULL;
6843 s->first_displayed_entry = te;
6844 te = got_tree_entry_get_prev(s->tree, te);
6848 static const struct got_error *
6849 tree_scroll_down(struct tog_view *view, int maxscroll)
6851 struct tog_tree_view_state *s = &view->state.tree;
6852 struct got_tree_entry *next, *last;
6855 if (s->first_displayed_entry)
6856 next = got_tree_entry_get_next(s->tree,
6857 s->first_displayed_entry);
6859 next = got_object_tree_get_first_entry(s->tree);
6861 last = s->last_displayed_entry;
6862 while (next && n++ < maxscroll) {
6864 s->last_displayed_entry = last;
6865 last = got_tree_entry_get_next(s->tree, last);
6867 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6868 s->first_displayed_entry = next;
6869 next = got_tree_entry_get_next(s->tree, next);
6876 static const struct got_error *
6877 tree_entry_path(char **path, struct tog_parent_trees *parents,
6878 struct got_tree_entry *te)
6880 const struct got_error *err = NULL;
6881 struct tog_parent_tree *pt;
6882 size_t len = 2; /* for leading slash and NUL */
6884 TAILQ_FOREACH(pt, parents, entry)
6885 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6888 len += strlen(got_tree_entry_get_name(te));
6890 *path = calloc(1, len);
6892 return got_error_from_errno("calloc");
6895 pt = TAILQ_LAST(parents, tog_parent_trees);
6897 const char *name = got_tree_entry_get_name(pt->selected_entry);
6898 if (strlcat(*path, name, len) >= len) {
6899 err = got_error(GOT_ERR_NO_SPACE);
6902 if (strlcat(*path, "/", len) >= len) {
6903 err = got_error(GOT_ERR_NO_SPACE);
6906 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6909 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6910 err = got_error(GOT_ERR_NO_SPACE);
6922 static const struct got_error *
6923 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6924 struct got_tree_entry *te, struct tog_parent_trees *parents,
6925 struct got_object_id *commit_id, struct got_repository *repo)
6927 const struct got_error *err = NULL;
6929 struct tog_view *blame_view;
6933 err = tree_entry_path(&path, parents, te);
6937 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6938 if (blame_view == NULL) {
6939 err = got_error_from_errno("view_open");
6943 err = open_blame_view(blame_view, path, commit_id, repo);
6945 if (err->code == GOT_ERR_CANCELLED)
6947 view_close(blame_view);
6949 *new_view = blame_view;
6955 static const struct got_error *
6956 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6957 struct tog_tree_view_state *s)
6959 struct tog_view *log_view;
6960 const struct got_error *err = NULL;
6965 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6966 if (log_view == NULL)
6967 return got_error_from_errno("view_open");
6969 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6973 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6976 view_close(log_view);
6978 *new_view = log_view;
6983 static const struct got_error *
6984 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6985 const char *head_ref_name, struct got_repository *repo)
6987 const struct got_error *err = NULL;
6988 char *commit_id_str = NULL;
6989 struct tog_tree_view_state *s = &view->state.tree;
6990 struct got_commit_object *commit = NULL;
6992 TAILQ_INIT(&s->parents);
6993 STAILQ_INIT(&s->colors);
6995 s->commit_id = got_object_id_dup(commit_id);
6996 if (s->commit_id == NULL)
6997 return got_error_from_errno("got_object_id_dup");
6999 err = got_object_open_as_commit(&commit, repo, commit_id);
7004 * The root is opened here and will be closed when the view is closed.
7005 * Any visited subtrees and their path-wise parents are opened and
7008 err = got_object_open_as_tree(&s->root, repo,
7009 got_object_commit_get_tree_id(commit));
7014 err = got_object_id_str(&commit_id_str, commit_id);
7018 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7019 err = got_error_from_errno("asprintf");
7023 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7024 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7025 if (head_ref_name) {
7026 s->head_ref_name = strdup(head_ref_name);
7027 if (s->head_ref_name == NULL) {
7028 err = got_error_from_errno("strdup");
7034 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7035 err = add_color(&s->colors, "\\$$",
7036 TOG_COLOR_TREE_SUBMODULE,
7037 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7040 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7041 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7044 err = add_color(&s->colors, "/$",
7045 TOG_COLOR_TREE_DIRECTORY,
7046 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7050 err = add_color(&s->colors, "\\*$",
7051 TOG_COLOR_TREE_EXECUTABLE,
7052 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7056 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7057 get_color_value("TOG_COLOR_COMMIT"));
7062 view->show = show_tree_view;
7063 view->input = input_tree_view;
7064 view->close = close_tree_view;
7065 view->search_start = search_start_tree_view;
7066 view->search_next = search_next_tree_view;
7068 free(commit_id_str);
7070 got_object_commit_close(commit);
7072 close_tree_view(view);
7076 static const struct got_error *
7077 close_tree_view(struct tog_view *view)
7079 struct tog_tree_view_state *s = &view->state.tree;
7081 free_colors(&s->colors);
7082 free(s->tree_label);
7083 s->tree_label = NULL;
7085 s->commit_id = NULL;
7086 free(s->head_ref_name);
7087 s->head_ref_name = NULL;
7088 while (!TAILQ_EMPTY(&s->parents)) {
7089 struct tog_parent_tree *parent;
7090 parent = TAILQ_FIRST(&s->parents);
7091 TAILQ_REMOVE(&s->parents, parent, entry);
7092 if (parent->tree != s->root)
7093 got_object_tree_close(parent->tree);
7097 if (s->tree != NULL && s->tree != s->root)
7098 got_object_tree_close(s->tree);
7100 got_object_tree_close(s->root);
7104 static const struct got_error *
7105 search_start_tree_view(struct tog_view *view)
7107 struct tog_tree_view_state *s = &view->state.tree;
7109 s->matched_entry = NULL;
7114 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7116 regmatch_t regmatch;
7118 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7122 static const struct got_error *
7123 search_next_tree_view(struct tog_view *view)
7125 struct tog_tree_view_state *s = &view->state.tree;
7126 struct got_tree_entry *te = NULL;
7128 if (!view->searching) {
7129 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7133 if (s->matched_entry) {
7134 if (view->searching == TOG_SEARCH_FORWARD) {
7135 if (s->selected_entry)
7136 te = got_tree_entry_get_next(s->tree,
7139 te = got_object_tree_get_first_entry(s->tree);
7141 if (s->selected_entry == NULL)
7142 te = got_object_tree_get_last_entry(s->tree);
7144 te = got_tree_entry_get_prev(s->tree,
7148 if (s->selected_entry)
7149 te = s->selected_entry;
7150 else if (view->searching == TOG_SEARCH_FORWARD)
7151 te = got_object_tree_get_first_entry(s->tree);
7153 te = got_object_tree_get_last_entry(s->tree);
7158 if (s->matched_entry == NULL) {
7159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7162 if (view->searching == TOG_SEARCH_FORWARD)
7163 te = got_object_tree_get_first_entry(s->tree);
7165 te = got_object_tree_get_last_entry(s->tree);
7168 if (match_tree_entry(te, &view->regex)) {
7169 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7170 s->matched_entry = te;
7174 if (view->searching == TOG_SEARCH_FORWARD)
7175 te = got_tree_entry_get_next(s->tree, te);
7177 te = got_tree_entry_get_prev(s->tree, te);
7180 if (s->matched_entry) {
7181 s->first_displayed_entry = s->matched_entry;
7188 static const struct got_error *
7189 show_tree_view(struct tog_view *view)
7191 const struct got_error *err = NULL;
7192 struct tog_tree_view_state *s = &view->state.tree;
7195 err = tree_entry_path(&parent_path, &s->parents, NULL);
7199 err = draw_tree_entries(view, parent_path);
7206 static const struct got_error *
7207 tree_goto_line(struct tog_view *view, int nlines)
7209 const struct got_error *err = NULL;
7210 struct tog_tree_view_state *s = &view->state.tree;
7211 struct got_tree_entry **fte, **lte, **ste;
7212 int g, last, first = 1, i = 1;
7213 int root = s->tree == s->root;
7214 int off = root ? 1 : 2;
7221 else if (g > got_object_tree_get_nentries(s->tree))
7222 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7224 fte = &s->first_displayed_entry;
7225 lte = &s->last_displayed_entry;
7226 ste = &s->selected_entry;
7229 first = got_tree_entry_get_index(*fte);
7230 first += off; /* account for ".." */
7232 last = got_tree_entry_get_index(*lte);
7235 if (g >= first && g <= last && g - first < nlines) {
7236 s->selected = g - first;
7237 return NULL; /* gline is on the current page */
7241 i = got_tree_entry_get_index(*ste);
7246 err = tree_scroll_down(view, g - i);
7249 if (got_tree_entry_get_index(*lte) >=
7250 got_object_tree_get_nentries(s->tree) - 1 &&
7251 first + s->selected < g &&
7252 s->selected < s->ndisplayed - 1) {
7253 first = got_tree_entry_get_index(*fte);
7255 s->selected = g - first;
7258 tree_scroll_up(s, i - g);
7261 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7262 s->selected = g - 1;
7267 static const struct got_error *
7268 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7270 const struct got_error *err = NULL;
7271 struct tog_tree_view_state *s = &view->state.tree;
7272 struct got_tree_entry *te;
7273 int n, nscroll = view->nlines - 3;
7276 return tree_goto_line(view, nscroll);
7280 s->show_ids = !s->show_ids;
7285 if (!s->selected_entry)
7287 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7291 err = view_request_new(new_view, view, TOG_VIEW_REF);
7297 if (s->tree == s->root)
7298 s->first_displayed_entry =
7299 got_object_tree_get_first_entry(s->tree);
7301 s->first_displayed_entry = NULL;
7305 int eos = view->nlines - 3;
7307 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7311 te = got_object_tree_get_last_entry(s->tree);
7312 for (n = 0; n < eos; n++) {
7314 if (s->tree != s->root) {
7315 s->first_displayed_entry = NULL;
7320 s->first_displayed_entry = te;
7321 te = got_tree_entry_get_prev(s->tree, te);
7324 s->selected = n - 1;
7330 if (s->selected > 0) {
7334 tree_scroll_up(s, 1);
7335 if (s->selected_entry == NULL ||
7336 (s->tree == s->root && s->selected_entry ==
7337 got_object_tree_get_first_entry(s->tree)))
7347 if (s->tree == s->root) {
7348 if (got_object_tree_get_first_entry(s->tree) ==
7349 s->first_displayed_entry)
7350 s->selected -= MIN(s->selected, nscroll);
7352 if (s->first_displayed_entry == NULL)
7353 s->selected -= MIN(s->selected, nscroll);
7355 tree_scroll_up(s, MAX(0, nscroll));
7356 if (s->selected_entry == NULL ||
7357 (s->tree == s->root && s->selected_entry ==
7358 got_object_tree_get_first_entry(s->tree)))
7364 if (s->selected < s->ndisplayed - 1) {
7368 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7370 /* can't scroll any further */
7374 tree_scroll_down(view, 1);
7384 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7386 /* can't scroll any further; move cursor down */
7387 if (s->selected < s->ndisplayed - 1)
7388 s->selected += MIN(nscroll,
7389 s->ndisplayed - s->selected - 1);
7394 tree_scroll_down(view, nscroll);
7399 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7400 struct tog_parent_tree *parent;
7401 /* user selected '..' */
7402 if (s->tree == s->root) {
7406 parent = TAILQ_FIRST(&s->parents);
7407 TAILQ_REMOVE(&s->parents, parent,
7409 got_object_tree_close(s->tree);
7410 s->tree = parent->tree;
7411 s->first_displayed_entry =
7412 parent->first_displayed_entry;
7414 parent->selected_entry;
7415 s->selected = parent->selected;
7416 if (s->selected > view->nlines - 3) {
7417 err = offset_selection_down(view);
7422 } else if (S_ISDIR(got_tree_entry_get_mode(
7423 s->selected_entry))) {
7424 struct got_tree_object *subtree;
7426 err = got_object_open_as_tree(&subtree, s->repo,
7427 got_tree_entry_get_id(s->selected_entry));
7430 err = tree_view_visit_subtree(s, subtree);
7432 got_object_tree_close(subtree);
7435 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7436 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7439 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7440 s->selected = view->nlines - 4;
7456 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7461 static const struct got_error *
7462 cmd_tree(int argc, char *argv[])
7464 const struct got_error *error;
7465 struct got_repository *repo = NULL;
7466 struct got_worktree *worktree = NULL;
7467 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7468 struct got_object_id *commit_id = NULL;
7469 struct got_commit_object *commit = NULL;
7470 const char *commit_id_arg = NULL;
7472 struct got_reference *ref = NULL;
7473 const char *head_ref_name = NULL;
7475 struct tog_view *view;
7476 int *pack_fds = NULL;
7478 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7481 commit_id_arg = optarg;
7484 repo_path = realpath(optarg, NULL);
7485 if (repo_path == NULL)
7486 return got_error_from_errno2("realpath",
7501 error = got_repo_pack_fds_open(&pack_fds);
7505 if (repo_path == NULL) {
7506 cwd = getcwd(NULL, 0);
7508 return got_error_from_errno("getcwd");
7509 error = got_worktree_open(&worktree, cwd);
7510 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7514 strdup(got_worktree_get_repo_path(worktree));
7516 repo_path = strdup(cwd);
7517 if (repo_path == NULL) {
7518 error = got_error_from_errno("strdup");
7523 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7527 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7534 error = apply_unveil(got_repo_get_path(repo), NULL);
7538 error = tog_load_refs(repo, 0);
7542 if (commit_id_arg == NULL) {
7543 error = got_repo_match_object_id(&commit_id, &label,
7544 worktree ? got_worktree_get_head_ref_name(worktree) :
7545 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7548 head_ref_name = label;
7550 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7552 head_ref_name = got_ref_get_name(ref);
7553 else if (error->code != GOT_ERR_NOT_REF)
7555 error = got_repo_match_object_id(&commit_id, NULL,
7556 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7561 error = got_object_open_as_commit(&commit, repo, commit_id);
7565 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7567 error = got_error_from_errno("view_open");
7570 error = open_tree_view(view, commit_id, head_ref_name, repo);
7573 if (!got_path_is_root_dir(in_repo_path)) {
7574 error = tree_view_walk_path(&view->state.tree, commit,
7581 /* Release work tree lock. */
7582 got_worktree_close(worktree);
7585 error = view_loop(view);
7594 const struct got_error *close_err = got_repo_close(repo);
7599 const struct got_error *pack_err =
7600 got_repo_pack_fds_close(pack_fds);
7608 static const struct got_error *
7609 ref_view_load_refs(struct tog_ref_view_state *s)
7611 struct got_reflist_entry *sre;
7612 struct tog_reflist_entry *re;
7615 TAILQ_FOREACH(sre, &tog_refs, entry) {
7616 if (strncmp(got_ref_get_name(sre->ref),
7617 "refs/got/", 9) == 0 &&
7618 strncmp(got_ref_get_name(sre->ref),
7619 "refs/got/backup/", 16) != 0)
7622 re = malloc(sizeof(*re));
7624 return got_error_from_errno("malloc");
7626 re->ref = got_ref_dup(sre->ref);
7627 if (re->ref == NULL)
7628 return got_error_from_errno("got_ref_dup");
7629 re->idx = s->nrefs++;
7630 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7633 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7638 ref_view_free_refs(struct tog_ref_view_state *s)
7640 struct tog_reflist_entry *re;
7642 while (!TAILQ_EMPTY(&s->refs)) {
7643 re = TAILQ_FIRST(&s->refs);
7644 TAILQ_REMOVE(&s->refs, re, entry);
7645 got_ref_close(re->ref);
7650 static const struct got_error *
7651 open_ref_view(struct tog_view *view, struct got_repository *repo)
7653 const struct got_error *err = NULL;
7654 struct tog_ref_view_state *s = &view->state.ref;
7656 s->selected_entry = 0;
7659 TAILQ_INIT(&s->refs);
7660 STAILQ_INIT(&s->colors);
7662 err = ref_view_load_refs(s);
7666 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7667 err = add_color(&s->colors, "^refs/heads/",
7668 TOG_COLOR_REFS_HEADS,
7669 get_color_value("TOG_COLOR_REFS_HEADS"));
7673 err = add_color(&s->colors, "^refs/tags/",
7674 TOG_COLOR_REFS_TAGS,
7675 get_color_value("TOG_COLOR_REFS_TAGS"));
7679 err = add_color(&s->colors, "^refs/remotes/",
7680 TOG_COLOR_REFS_REMOTES,
7681 get_color_value("TOG_COLOR_REFS_REMOTES"));
7685 err = add_color(&s->colors, "^refs/got/backup/",
7686 TOG_COLOR_REFS_BACKUP,
7687 get_color_value("TOG_COLOR_REFS_BACKUP"));
7692 view->show = show_ref_view;
7693 view->input = input_ref_view;
7694 view->close = close_ref_view;
7695 view->search_start = search_start_ref_view;
7696 view->search_next = search_next_ref_view;
7699 free_colors(&s->colors);
7703 static const struct got_error *
7704 close_ref_view(struct tog_view *view)
7706 struct tog_ref_view_state *s = &view->state.ref;
7708 ref_view_free_refs(s);
7709 free_colors(&s->colors);
7714 static const struct got_error *
7715 resolve_reflist_entry(struct got_object_id **commit_id,
7716 struct tog_reflist_entry *re, struct got_repository *repo)
7718 const struct got_error *err = NULL;
7719 struct got_object_id *obj_id;
7720 struct got_tag_object *tag = NULL;
7725 err = got_ref_resolve(&obj_id, repo, re->ref);
7729 err = got_object_get_type(&obj_type, repo, obj_id);
7734 case GOT_OBJ_TYPE_COMMIT:
7735 *commit_id = obj_id;
7737 case GOT_OBJ_TYPE_TAG:
7738 err = got_object_open_as_tag(&tag, repo, obj_id);
7742 err = got_object_get_type(&obj_type, repo,
7743 got_object_tag_get_object_id(tag));
7746 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7747 err = got_error(GOT_ERR_OBJ_TYPE);
7750 *commit_id = got_object_id_dup(
7751 got_object_tag_get_object_id(tag));
7752 if (*commit_id == NULL) {
7753 err = got_error_from_errno("got_object_id_dup");
7758 err = got_error(GOT_ERR_OBJ_TYPE);
7764 got_object_tag_close(tag);
7772 static const struct got_error *
7773 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7774 struct tog_reflist_entry *re, struct got_repository *repo)
7776 struct tog_view *log_view;
7777 const struct got_error *err = NULL;
7778 struct got_object_id *commit_id = NULL;
7782 err = resolve_reflist_entry(&commit_id, re, repo);
7784 if (err->code != GOT_ERR_OBJ_TYPE)
7790 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7791 if (log_view == NULL) {
7792 err = got_error_from_errno("view_open");
7796 err = open_log_view(log_view, commit_id, repo,
7797 got_ref_get_name(re->ref), "", 0);
7800 view_close(log_view);
7802 *new_view = log_view;
7808 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7810 struct tog_reflist_entry *re;
7813 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7816 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7817 while (i++ < maxscroll) {
7820 s->first_displayed_entry = re;
7821 re = TAILQ_PREV(re, tog_reflist_head, entry);
7825 static const struct got_error *
7826 ref_scroll_down(struct tog_view *view, int maxscroll)
7828 struct tog_ref_view_state *s = &view->state.ref;
7829 struct tog_reflist_entry *next, *last;
7832 if (s->first_displayed_entry)
7833 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7835 next = TAILQ_FIRST(&s->refs);
7837 last = s->last_displayed_entry;
7838 while (next && n++ < maxscroll) {
7840 s->last_displayed_entry = last;
7841 last = TAILQ_NEXT(last, entry);
7843 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7844 s->first_displayed_entry = next;
7845 next = TAILQ_NEXT(next, entry);
7852 static const struct got_error *
7853 search_start_ref_view(struct tog_view *view)
7855 struct tog_ref_view_state *s = &view->state.ref;
7857 s->matched_entry = NULL;
7862 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7864 regmatch_t regmatch;
7866 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7870 static const struct got_error *
7871 search_next_ref_view(struct tog_view *view)
7873 struct tog_ref_view_state *s = &view->state.ref;
7874 struct tog_reflist_entry *re = NULL;
7876 if (!view->searching) {
7877 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7881 if (s->matched_entry) {
7882 if (view->searching == TOG_SEARCH_FORWARD) {
7883 if (s->selected_entry)
7884 re = TAILQ_NEXT(s->selected_entry, entry);
7886 re = TAILQ_PREV(s->selected_entry,
7887 tog_reflist_head, entry);
7889 if (s->selected_entry == NULL)
7890 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7892 re = TAILQ_PREV(s->selected_entry,
7893 tog_reflist_head, entry);
7896 if (s->selected_entry)
7897 re = s->selected_entry;
7898 else if (view->searching == TOG_SEARCH_FORWARD)
7899 re = TAILQ_FIRST(&s->refs);
7901 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7906 if (s->matched_entry == NULL) {
7907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7910 if (view->searching == TOG_SEARCH_FORWARD)
7911 re = TAILQ_FIRST(&s->refs);
7913 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7916 if (match_reflist_entry(re, &view->regex)) {
7917 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7918 s->matched_entry = re;
7922 if (view->searching == TOG_SEARCH_FORWARD)
7923 re = TAILQ_NEXT(re, entry);
7925 re = TAILQ_PREV(re, tog_reflist_head, entry);
7928 if (s->matched_entry) {
7929 s->first_displayed_entry = s->matched_entry;
7936 static const struct got_error *
7937 show_ref_view(struct tog_view *view)
7939 const struct got_error *err = NULL;
7940 struct tog_ref_view_state *s = &view->state.ref;
7941 struct tog_reflist_entry *re;
7944 struct tog_color *tc;
7946 int limit = view->nlines;
7948 werase(view->window);
7951 if (view_is_hsplit_top(view))
7952 --limit; /* border */
7957 re = s->first_displayed_entry;
7959 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7961 return got_error_from_errno("asprintf");
7963 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7968 if (view_needs_focus_indication(view))
7969 wstandout(view->window);
7970 waddwstr(view->window, wline);
7971 while (width++ < view->ncols)
7972 waddch(view->window, ' ');
7973 if (view_needs_focus_indication(view))
7974 wstandend(view->window);
7983 while (re && limit > 0) {
7985 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7988 struct got_commit_object *ci;
7989 struct got_tag_object *tag;
7990 struct got_object_id *id;
7994 err = got_ref_resolve(&id, s->repo, re->ref);
7997 err = got_object_open_as_tag(&tag, s->repo, id);
7999 if (err->code != GOT_ERR_OBJ_TYPE) {
8003 err = got_object_open_as_commit(&ci, s->repo,
8009 t = got_object_commit_get_committer_time(ci);
8010 got_object_commit_close(ci);
8012 t = got_object_tag_get_tagger_time(tag);
8013 got_object_tag_close(tag);
8016 if (gmtime_r(&t, &tm) == NULL)
8017 return got_error_from_errno("gmtime_r");
8018 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8019 return got_error(GOT_ERR_NO_SPACE);
8021 if (got_ref_is_symbolic(re->ref)) {
8022 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8023 ymd : "", got_ref_get_name(re->ref),
8024 got_ref_get_symref_target(re->ref)) == -1)
8025 return got_error_from_errno("asprintf");
8026 } else if (s->show_ids) {
8027 struct got_object_id *id;
8029 err = got_ref_resolve(&id, s->repo, re->ref);
8032 err = got_object_id_str(&id_str, id);
8037 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8038 got_ref_get_name(re->ref), id_str) == -1) {
8039 err = got_error_from_errno("asprintf");
8046 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8047 got_ref_get_name(re->ref)) == -1)
8048 return got_error_from_errno("asprintf");
8050 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8056 if (n == s->selected) {
8058 wstandout(view->window);
8059 s->selected_entry = re;
8061 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8063 wattr_on(view->window,
8064 COLOR_PAIR(tc->colorpair), NULL);
8065 waddwstr(view->window, wline);
8067 wattr_off(view->window,
8068 COLOR_PAIR(tc->colorpair), NULL);
8069 if (width < view->ncols - 1)
8070 waddch(view->window, '\n');
8071 if (n == s->selected && view->focussed)
8072 wstandend(view->window);
8078 s->last_displayed_entry = re;
8081 re = TAILQ_NEXT(re, entry);
8088 static const struct got_error *
8089 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8090 struct tog_reflist_entry *re, struct got_repository *repo)
8092 const struct got_error *err = NULL;
8093 struct got_object_id *commit_id = NULL;
8094 struct tog_view *tree_view;
8098 err = resolve_reflist_entry(&commit_id, re, repo);
8100 if (err->code != GOT_ERR_OBJ_TYPE)
8107 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8108 if (tree_view == NULL) {
8109 err = got_error_from_errno("view_open");
8113 err = open_tree_view(tree_view, commit_id,
8114 got_ref_get_name(re->ref), repo);
8118 *new_view = tree_view;
8124 static const struct got_error *
8125 ref_goto_line(struct tog_view *view, int nlines)
8127 const struct got_error *err = NULL;
8128 struct tog_ref_view_state *s = &view->state.ref;
8129 int g, idx = s->selected_entry->idx;
8136 else if (g > s->nrefs)
8139 if (g >= s->first_displayed_entry->idx + 1 &&
8140 g <= s->last_displayed_entry->idx + 1 &&
8141 g - s->first_displayed_entry->idx - 1 < nlines) {
8142 s->selected = g - s->first_displayed_entry->idx - 1;
8147 err = ref_scroll_down(view, g - idx - 1);
8150 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8151 s->first_displayed_entry->idx + s->selected < g &&
8152 s->selected < s->ndisplayed - 1)
8153 s->selected = g - s->first_displayed_entry->idx - 1;
8154 } else if (idx + 1 > g)
8155 ref_scroll_up(s, idx - g + 1);
8157 if (g < nlines && s->first_displayed_entry->idx == 0)
8158 s->selected = g - 1;
8164 static const struct got_error *
8165 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8167 const struct got_error *err = NULL;
8168 struct tog_ref_view_state *s = &view->state.ref;
8169 struct tog_reflist_entry *re;
8170 int n, nscroll = view->nlines - 1;
8173 return ref_goto_line(view, nscroll);
8177 s->show_ids = !s->show_ids;
8181 s->show_date = !s->show_date;
8185 s->sort_by_date = !s->sort_by_date;
8187 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8188 got_ref_cmp_by_commit_timestamp_descending :
8189 tog_ref_cmp_by_name, s->repo);
8192 got_reflist_object_id_map_free(tog_refs_idmap);
8193 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8194 &tog_refs, s->repo);
8197 ref_view_free_refs(s);
8198 err = ref_view_load_refs(s);
8203 if (!s->selected_entry)
8205 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8209 if (!s->selected_entry)
8211 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8217 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8221 int eos = view->nlines - 1;
8223 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8227 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8228 for (n = 0; n < eos; n++) {
8231 s->first_displayed_entry = re;
8232 re = TAILQ_PREV(re, tog_reflist_head, entry);
8235 s->selected = n - 1;
8241 if (s->selected > 0) {
8245 ref_scroll_up(s, 1);
8246 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8256 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8257 s->selected -= MIN(nscroll, s->selected);
8258 ref_scroll_up(s, MAX(0, nscroll));
8259 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8265 if (s->selected < s->ndisplayed - 1) {
8269 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8270 /* can't scroll any further */
8274 ref_scroll_down(view, 1);
8284 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8285 /* can't scroll any further; move cursor down */
8286 if (s->selected < s->ndisplayed - 1)
8287 s->selected += MIN(nscroll,
8288 s->ndisplayed - s->selected - 1);
8289 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8290 s->selected += s->ndisplayed - s->selected - 1;
8294 ref_scroll_down(view, nscroll);
8299 err = tog_load_refs(s->repo, s->sort_by_date);
8302 ref_view_free_refs(s);
8303 err = ref_view_load_refs(s);
8306 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8307 s->selected = view->nlines - 2;
8321 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8326 static const struct got_error *
8327 cmd_ref(int argc, char *argv[])
8329 const struct got_error *error;
8330 struct got_repository *repo = NULL;
8331 struct got_worktree *worktree = NULL;
8332 char *cwd = NULL, *repo_path = NULL;
8334 struct tog_view *view;
8335 int *pack_fds = NULL;
8337 while ((ch = getopt(argc, argv, "r:")) != -1) {
8340 repo_path = realpath(optarg, NULL);
8341 if (repo_path == NULL)
8342 return got_error_from_errno2("realpath",
8357 error = got_repo_pack_fds_open(&pack_fds);
8361 if (repo_path == NULL) {
8362 cwd = getcwd(NULL, 0);
8364 return got_error_from_errno("getcwd");
8365 error = got_worktree_open(&worktree, cwd);
8366 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8370 strdup(got_worktree_get_repo_path(worktree));
8372 repo_path = strdup(cwd);
8373 if (repo_path == NULL) {
8374 error = got_error_from_errno("strdup");
8379 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8385 error = apply_unveil(got_repo_get_path(repo), NULL);
8389 error = tog_load_refs(repo, 0);
8393 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8395 error = got_error_from_errno("view_open");
8399 error = open_ref_view(view, repo);
8404 /* Release work tree lock. */
8405 got_worktree_close(worktree);
8408 error = view_loop(view);
8413 const struct got_error *close_err = got_repo_close(repo);
8418 const struct got_error *pack_err =
8419 got_repo_pack_fds_close(pack_fds);
8427 static const struct got_error*
8428 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8437 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8441 if (mvwprintw(win, y, x, "%s", str) == ERR)
8442 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8449 static const struct got_error *
8450 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8454 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8456 free(*line_offsets);
8457 *line_offsets = NULL;
8458 return got_error_from_errno("reallocarray");
8462 (*line_offsets)[*nlines] = off;
8467 static const struct got_error *
8468 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8472 for (;n > 0; --n, ++km) {
8476 if (km->keys == NULL)
8479 t = t0 = strdup(km->keys);
8481 return got_error_from_errno("strdup");
8484 while ((k = strsep(&t, " ")) != NULL)
8485 len += strlen(k) > 1 ? 2 : 0;
8487 *ret = MAX(*ret, len);
8494 * Write keymap section headers, keys, and key info in km to f.
8495 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8496 * wrap control and symbolic keys in guillemets, else use <>.
8498 static const struct got_error *
8499 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8504 static const char *u8_glyph[] = {
8505 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8506 "\xe2\x80\xba" /* U+203A (utf8 >) */
8509 int cs, s, first = 1;
8511 cs = got_locale_is_utf8();
8513 t = t0 = strdup(km->keys);
8515 return got_error_from_errno("strdup");
8517 len = strlen(km->keys);
8518 while ((k = strsep(&t, " ")) != NULL) {
8519 s = strlen(k) > 1; /* control or symbolic key */
8520 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8521 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8522 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8525 return got_error_from_errno("fprintf");
8533 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8535 return got_error_from_errno("fprintf");
8541 static const struct got_error *
8542 format_help(struct tog_help_view_state *s)
8544 const struct got_error *err = NULL;
8546 int i, max, n, show = s->all;
8547 static const struct tog_key_map km[] = {
8548 #define KEYMAP_(info, type) { NULL, (info), type }
8549 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8555 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8560 err = max_key_str(&max, km, n);
8564 for (i = 0; i < n; ++i) {
8565 if (km[i].keys == NULL) {
8567 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8568 km[i].type == s->type || s->all)
8572 err = format_help_line(&off, s->f, &km[i], max);
8575 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8582 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8586 static const struct got_error *
8587 create_help(struct tog_help_view_state *s)
8590 const struct got_error *err;
8592 free(s->line_offsets);
8593 s->line_offsets = NULL;
8598 return got_error_from_errno("got_opentemp");
8601 err = format_help(s);
8605 if (s->f && fflush(s->f) != 0)
8606 return got_error_from_errno("fflush");
8611 static const struct got_error *
8612 search_start_help_view(struct tog_view *view)
8614 view->state.help.matched_line = 0;
8619 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8620 size_t *nlines, int **first, int **last, int **match, int **selected)
8622 struct tog_help_view_state *s = &view->state.help;
8625 *nlines = s->nlines;
8626 *line_offsets = s->line_offsets;
8627 *match = &s->matched_line;
8628 *first = &s->first_displayed_line;
8629 *last = &s->last_displayed_line;
8630 *selected = &s->selected_line;
8633 static const struct got_error *
8634 show_help_view(struct tog_view *view)
8636 struct tog_help_view_state *s = &view->state.help;
8637 const struct got_error *err;
8638 regmatch_t *regmatch = &view->regmatch;
8643 int width, nprinted = 0, rc = 0;
8644 int eos = view->nlines;
8646 if (view_is_hsplit_top(view))
8647 --eos; /* account for border */
8651 werase(view->window);
8653 if (view->gline > s->nlines - 1)
8654 view->gline = s->nlines - 1;
8656 err = win_draw_center(view->window, 0, 0, view->ncols,
8657 view_needs_focus_indication(view),
8658 "tog help (press q to return to tog)");
8663 waddstr(view->window, "\n\n");
8669 while (eos > 0 && nprinted < eos) {
8672 linelen = getline(&line, &linesz, s->f);
8673 if (linelen == -1) {
8676 return got_ferror(s->f, GOT_ERR_IO);
8681 if (++s->lineno < s->first_displayed_line)
8683 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8685 if (s->lineno == view->hiline)
8688 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8694 view->maxx = MAX(view->maxx, width);
8699 wattron(view->window, attr);
8700 if (s->first_displayed_line + nprinted == s->matched_line &&
8701 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8702 err = add_matched_line(&width, line, view->ncols - 1, 0,
8703 view->window, view->x, regmatch);
8711 err = format_line(&wline, &width, &skip, line,
8712 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8717 rc = waddwstr(view->window, &wline[skip]);
8721 return got_error_msg(GOT_ERR_IO, "waddwstr");
8723 if (s->lineno == view->hiline) {
8724 while (width++ < view->ncols)
8725 waddch(view->window, ' ');
8727 if (width <= view->ncols)
8728 waddch(view->window, '\n');
8731 wattroff(view->window, attr);
8732 if (++nprinted == 1)
8733 s->first_displayed_line = s->lineno;
8737 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8739 s->last_displayed_line = s->first_displayed_line;
8744 rc = waddnstr(view->window,
8745 "See the tog(1) manual page for full documentation",
8748 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8750 wmove(view->window, view->nlines - 1, 0);
8751 wclrtoeol(view->window);
8752 wstandout(view->window);
8753 rc = waddnstr(view->window, "scroll down for more...",
8756 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8757 if (getcurx(view->window) < view->ncols - 6) {
8758 rc = wprintw(view->window, "[%.0f%%]",
8759 100.00 * s->last_displayed_line / s->nlines);
8761 return got_error_msg(GOT_ERR_IO, "wprintw");
8763 wstandend(view->window);
8769 static const struct got_error *
8770 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8772 struct tog_help_view_state *s = &view->state.help;
8773 const struct got_error *err = NULL;
8779 eos = nscroll = view->nlines;
8780 if (view_is_hsplit_top(view))
8783 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8790 view->x = MAX(view->maxx - view->ncols / 3, 0);
8795 if (view->x + view->ncols / 3 < view->maxx)
8802 view->x -= MIN(view->x, 2);
8808 s->first_displayed_line = 1;
8816 s->first_displayed_line = (s->nlines - eos) + 3;
8821 if (s->first_displayed_line > 1)
8822 --s->first_displayed_line;
8833 if (s->first_displayed_line == 1) {
8837 while (--nscroll > 0 && s->first_displayed_line > 1)
8838 s->first_displayed_line--;
8844 ++s->first_displayed_line;
8860 while (!s->eof && --nscroll > 0) {
8861 linelen = getline(&line, &linesz, s->f);
8862 s->first_displayed_line++;
8863 if (linelen == -1) {
8867 err = got_ferror(s->f, GOT_ERR_IO);
8881 static const struct got_error *
8882 close_help_view(struct tog_view *view)
8884 struct tog_help_view_state *s = &view->state.help;
8886 free(s->line_offsets);
8887 s->line_offsets = NULL;
8888 if (fclose(s->f) == EOF)
8889 return got_error_from_errno("fclose");
8894 static const struct got_error *
8895 reset_help_view(struct tog_view *view)
8897 struct tog_help_view_state *s = &view->state.help;
8900 if (s->f && fclose(s->f) == EOF)
8901 return got_error_from_errno("fclose");
8903 wclear(view->window);
8907 s->first_displayed_line = 1;
8908 s->last_displayed_line = view->nlines;
8909 s->matched_line = 0;
8911 return create_help(s);
8914 static const struct got_error *
8915 open_help_view(struct tog_view *view, struct tog_view *parent)
8917 const struct got_error *err = NULL;
8918 struct tog_help_view_state *s = &view->state.help;
8920 s->type = (enum tog_keymap_type)parent->type;
8921 s->first_displayed_line = 1;
8922 s->last_displayed_line = view->nlines;
8923 s->selected_line = 1;
8925 view->show = show_help_view;
8926 view->input = input_help_view;
8927 view->reset = reset_help_view;
8928 view->close = close_help_view;
8929 view->search_start = search_start_help_view;
8930 view->search_setup = search_setup_help_view;
8931 view->search_next = search_next_view_match;
8933 err = create_help(s);
8937 static const struct got_error *
8938 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8939 enum tog_view_type request, int y, int x)
8941 const struct got_error *err = NULL;
8947 if (view->type == TOG_VIEW_LOG) {
8948 struct tog_log_view_state *s = &view->state.log;
8950 err = open_diff_view_for_commit(new_view, y, x,
8951 s->selected_entry->commit, s->selected_entry->id,
8954 return got_error_msg(GOT_ERR_NOT_IMPL,
8955 "parent/child view pair not supported");
8957 case TOG_VIEW_BLAME:
8958 if (view->type == TOG_VIEW_TREE) {
8959 struct tog_tree_view_state *s = &view->state.tree;
8961 err = blame_tree_entry(new_view, y, x,
8962 s->selected_entry, &s->parents, s->commit_id,
8965 return got_error_msg(GOT_ERR_NOT_IMPL,
8966 "parent/child view pair not supported");
8969 if (view->type == TOG_VIEW_BLAME)
8970 err = log_annotated_line(new_view, y, x,
8971 view->state.blame.repo, view->state.blame.id_to_log);
8972 else if (view->type == TOG_VIEW_TREE)
8973 err = log_selected_tree_entry(new_view, y, x,
8975 else if (view->type == TOG_VIEW_REF)
8976 err = log_ref_entry(new_view, y, x,
8977 view->state.ref.selected_entry,
8978 view->state.ref.repo);
8980 return got_error_msg(GOT_ERR_NOT_IMPL,
8981 "parent/child view pair not supported");
8984 if (view->type == TOG_VIEW_LOG)
8985 err = browse_commit_tree(new_view, y, x,
8986 view->state.log.selected_entry,
8987 view->state.log.in_repo_path,
8988 view->state.log.head_ref_name,
8989 view->state.log.repo);
8990 else if (view->type == TOG_VIEW_REF)
8991 err = browse_ref_tree(new_view, y, x,
8992 view->state.ref.selected_entry,
8993 view->state.ref.repo);
8995 return got_error_msg(GOT_ERR_NOT_IMPL,
8996 "parent/child view pair not supported");
8999 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9000 if (*new_view == NULL)
9001 return got_error_from_errno("view_open");
9002 if (view->type == TOG_VIEW_LOG)
9003 err = open_ref_view(*new_view, view->state.log.repo);
9004 else if (view->type == TOG_VIEW_TREE)
9005 err = open_ref_view(*new_view, view->state.tree.repo);
9007 err = got_error_msg(GOT_ERR_NOT_IMPL,
9008 "parent/child view pair not supported");
9010 view_close(*new_view);
9013 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9014 if (*new_view == NULL)
9015 return got_error_from_errno("view_open");
9016 err = open_help_view(*new_view, view);
9018 view_close(*new_view);
9021 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9028 * If view was scrolled down to move the selected line into view when opening a
9029 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9032 offset_selection_up(struct tog_view *view)
9034 switch (view->type) {
9035 case TOG_VIEW_BLAME: {
9036 struct tog_blame_view_state *s = &view->state.blame;
9037 if (s->first_displayed_line == 1) {
9038 s->selected_line = MAX(s->selected_line - view->offset,
9042 if (s->first_displayed_line > view->offset)
9043 s->first_displayed_line -= view->offset;
9045 s->first_displayed_line = 1;
9046 s->selected_line += view->offset;
9050 log_scroll_up(&view->state.log, view->offset);
9051 view->state.log.selected += view->offset;
9054 ref_scroll_up(&view->state.ref, view->offset);
9055 view->state.ref.selected += view->offset;
9058 tree_scroll_up(&view->state.tree, view->offset);
9059 view->state.tree.selected += view->offset;
9069 * If the selected line is in the section of screen covered by the bottom split,
9070 * scroll down offset lines to move it into view and index its new position.
9072 static const struct got_error *
9073 offset_selection_down(struct tog_view *view)
9075 const struct got_error *err = NULL;
9076 const struct got_error *(*scrolld)(struct tog_view *, int);
9077 int *selected = NULL;
9080 switch (view->type) {
9081 case TOG_VIEW_BLAME: {
9082 struct tog_blame_view_state *s = &view->state.blame;
9085 if (s->selected_line > view->nlines - header) {
9086 offset = abs(view->nlines - s->selected_line - header);
9087 s->first_displayed_line += offset;
9088 s->selected_line -= offset;
9089 view->offset = offset;
9093 case TOG_VIEW_LOG: {
9094 struct tog_log_view_state *s = &view->state.log;
9095 scrolld = &log_scroll_down;
9096 header = view_is_parent_view(view) ? 3 : 2;
9097 selected = &s->selected;
9100 case TOG_VIEW_REF: {
9101 struct tog_ref_view_state *s = &view->state.ref;
9102 scrolld = &ref_scroll_down;
9104 selected = &s->selected;
9107 case TOG_VIEW_TREE: {
9108 struct tog_tree_view_state *s = &view->state.tree;
9109 scrolld = &tree_scroll_down;
9111 selected = &s->selected;
9121 if (selected && *selected > view->nlines - header) {
9122 offset = abs(view->nlines - *selected - header);
9123 view->offset = offset;
9124 if (scrolld && offset) {
9125 err = scrolld(view, offset);
9126 *selected -= offset;
9134 list_commands(FILE *fp)
9138 fprintf(fp, "commands:");
9139 for (i = 0; i < nitems(tog_commands); i++) {
9140 const struct tog_cmd *cmd = &tog_commands[i];
9141 fprintf(fp, " %s", cmd->name);
9147 usage(int hflag, int status)
9149 FILE *fp = (status == 0) ? stdout : stderr;
9151 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9154 fprintf(fp, "lazy usage: %s path\n", getprogname());
9161 make_argv(int argc, ...)
9169 argv = calloc(argc, sizeof(char *));
9172 for (i = 0; i < argc; i++) {
9173 argv[i] = strdup(va_arg(ap, char *));
9174 if (argv[i] == NULL)
9183 * Try to convert 'tog path' into a 'tog log path' command.
9184 * The user could simply have mistyped the command rather than knowingly
9185 * provided a path. So check whether argv[0] can in fact be resolved
9186 * to a path in the HEAD commit and print a special error if not.
9187 * This hack is for mpi@ <3
9189 static const struct got_error *
9190 tog_log_with_path(int argc, char *argv[])
9192 const struct got_error *error = NULL, *close_err;
9193 const struct tog_cmd *cmd = NULL;
9194 struct got_repository *repo = NULL;
9195 struct got_worktree *worktree = NULL;
9196 struct got_object_id *commit_id = NULL, *id = NULL;
9197 struct got_commit_object *commit = NULL;
9198 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9199 char *commit_id_str = NULL, **cmd_argv = NULL;
9200 int *pack_fds = NULL;
9202 cwd = getcwd(NULL, 0);
9204 return got_error_from_errno("getcwd");
9206 error = got_repo_pack_fds_open(&pack_fds);
9210 error = got_worktree_open(&worktree, cwd);
9211 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9215 repo_path = strdup(got_worktree_get_repo_path(worktree));
9217 repo_path = strdup(cwd);
9218 if (repo_path == NULL) {
9219 error = got_error_from_errno("strdup");
9223 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9227 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9232 error = tog_load_refs(repo, 0);
9235 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9236 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9237 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9242 got_worktree_close(worktree);
9246 error = got_object_open_as_commit(&commit, repo, commit_id);
9250 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9252 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9254 fprintf(stderr, "%s: '%s' is no known command or path\n",
9255 getprogname(), argv[0]);
9260 error = got_object_id_str(&commit_id_str, commit_id);
9264 cmd = &tog_commands[0]; /* log */
9266 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9267 error = cmd->cmd_main(argc, cmd_argv);
9270 close_err = got_repo_close(repo);
9275 got_object_commit_close(commit);
9277 got_worktree_close(worktree);
9279 const struct got_error *pack_err =
9280 got_repo_pack_fds_close(pack_fds);
9285 free(commit_id_str);
9292 for (i = 0; i < argc; i++)
9301 main(int argc, char *argv[])
9303 const struct got_error *error = NULL;
9304 const struct tog_cmd *cmd = NULL;
9305 int ch, hflag = 0, Vflag = 0;
9306 char **cmd_argv = NULL;
9307 static const struct option longopts[] = {
9308 { "version", no_argument, NULL, 'V' },
9311 char *diff_algo_str = NULL;
9313 if (!isatty(STDIN_FILENO))
9314 errx(1, "standard input is not a tty");
9316 setlocale(LC_CTYPE, "");
9318 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9338 got_version_print_str();
9343 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9351 /* Build an argument vector which runs a default command. */
9352 cmd = &tog_commands[0];
9354 cmd_argv = make_argv(argc, cmd->name);
9358 /* Did the user specify a command? */
9359 for (i = 0; i < nitems(tog_commands); i++) {
9360 if (strncmp(tog_commands[i].name, argv[0],
9361 strlen(argv[0])) == 0) {
9362 cmd = &tog_commands[i];
9368 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9369 if (diff_algo_str) {
9370 if (strcasecmp(diff_algo_str, "patience") == 0)
9371 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9372 if (strcasecmp(diff_algo_str, "myers") == 0)
9373 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9379 /* No command specified; try log with a path */
9380 error = tog_log_with_path(argc, argv);
9385 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9392 for (i = 0; i < argc; i++)
9397 if (error && error->code != GOT_ERR_CANCELLED &&
9398 error->code != GOT_ERR_EOF &&
9399 error->code != GOT_ERR_PRIVSEP_EXIT &&
9400 error->code != GOT_ERR_PRIVSEP_PIPE &&
9401 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9402 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);