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);
3795 s->start_id = start_id;
3796 s->thread_args.start_id = s->start_id;
3798 s->log_branches = !s->log_branches;
3800 if (s->thread_args.pack_fds == NULL) {
3801 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3805 err = got_repo_open(&s->thread_args.repo,
3806 got_repo_get_path(s->repo), NULL,
3807 s->thread_args.pack_fds);
3811 err = tog_load_refs(s->repo, 0);
3814 err = got_commit_graph_open(&s->thread_args.graph,
3815 s->in_repo_path, !s->log_branches);
3818 err = got_commit_graph_iter_start(s->thread_args.graph,
3819 s->start_id, s->repo, NULL, NULL);
3822 free_commits(&s->real_commits);
3823 free_commits(&s->limit_commits);
3824 s->first_displayed_entry = NULL;
3825 s->last_displayed_entry = NULL;
3826 s->selected_entry = NULL;
3828 s->thread_args.log_complete = 0;
3830 s->thread_args.commits_needed = view->lines;
3831 s->matched_entry = NULL;
3832 s->search_entry = NULL;
3837 err = view_request_new(new_view, view, TOG_VIEW_REF);
3847 static const struct got_error *
3848 apply_unveil(const char *repo_path, const char *worktree_path)
3850 const struct got_error *error;
3853 if (unveil("gmon.out", "rwc") != 0)
3854 return got_error_from_errno2("unveil", "gmon.out");
3856 if (repo_path && unveil(repo_path, "r") != 0)
3857 return got_error_from_errno2("unveil", repo_path);
3859 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3860 return got_error_from_errno2("unveil", worktree_path);
3862 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3863 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3865 error = got_privsep_unveil_exec_helpers();
3869 if (unveil(NULL, NULL) != 0)
3870 return got_error_from_errno("unveil");
3879 * Override default signal handlers before starting ncurses.
3880 * This should prevent ncurses from installing its own
3881 * broken cleanup() signal handler.
3883 signal(SIGWINCH, tog_sigwinch);
3884 signal(SIGPIPE, tog_sigpipe);
3885 signal(SIGCONT, tog_sigcont);
3886 signal(SIGINT, tog_sigint);
3887 signal(SIGTERM, tog_sigterm);
3891 halfdelay(1); /* Do fast refresh while initial view is loading. */
3894 intrflush(stdscr, FALSE);
3895 keypad(stdscr, TRUE);
3897 if (getenv("TOG_COLORS") != NULL) {
3899 use_default_colors();
3903 static const struct got_error *
3904 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3905 struct got_repository *repo, struct got_worktree *worktree)
3907 const struct got_error *err = NULL;
3910 *in_repo_path = strdup("/");
3911 if (*in_repo_path == NULL)
3912 return got_error_from_errno("strdup");
3917 const char *prefix = got_worktree_get_path_prefix(worktree);
3920 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3923 if (asprintf(in_repo_path, "%s%s%s", prefix,
3924 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3926 err = got_error_from_errno("asprintf");
3927 *in_repo_path = NULL;
3931 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3936 static const struct got_error *
3937 cmd_log(int argc, char *argv[])
3939 const struct got_error *error;
3940 struct got_repository *repo = NULL;
3941 struct got_worktree *worktree = NULL;
3942 struct got_object_id *start_id = NULL;
3943 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3944 char *start_commit = NULL, *label = NULL;
3945 struct got_reference *ref = NULL;
3946 const char *head_ref_name = NULL;
3947 int ch, log_branches = 0;
3948 struct tog_view *view;
3949 int *pack_fds = NULL;
3951 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3957 start_commit = optarg;
3960 repo_path = realpath(optarg, NULL);
3961 if (repo_path == NULL)
3962 return got_error_from_errno2("realpath",
3977 error = got_repo_pack_fds_open(&pack_fds);
3981 if (repo_path == NULL) {
3982 cwd = getcwd(NULL, 0);
3984 return got_error_from_errno("getcwd");
3985 error = got_worktree_open(&worktree, cwd);
3986 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3990 strdup(got_worktree_get_repo_path(worktree));
3992 repo_path = strdup(cwd);
3993 if (repo_path == NULL) {
3994 error = got_error_from_errno("strdup");
3999 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4003 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4010 error = apply_unveil(got_repo_get_path(repo),
4011 worktree ? got_worktree_get_root_path(worktree) : NULL);
4015 /* already loaded by tog_log_with_path()? */
4016 if (TAILQ_EMPTY(&tog_refs)) {
4017 error = tog_load_refs(repo, 0);
4022 if (start_commit == NULL) {
4023 error = got_repo_match_object_id(&start_id, &label,
4024 worktree ? got_worktree_get_head_ref_name(worktree) :
4025 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4028 head_ref_name = label;
4030 error = got_ref_open(&ref, repo, start_commit, 0);
4032 head_ref_name = got_ref_get_name(ref);
4033 else if (error->code != GOT_ERR_NOT_REF)
4035 error = got_repo_match_object_id(&start_id, NULL,
4036 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4041 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4043 error = got_error_from_errno("view_open");
4046 error = open_log_view(view, start_id, repo, head_ref_name,
4047 in_repo_path, log_branches);
4051 /* Release work tree lock. */
4052 got_worktree_close(worktree);
4055 error = view_loop(view);
4065 const struct got_error *close_err = got_repo_close(repo);
4070 got_worktree_close(worktree);
4072 const struct got_error *pack_err =
4073 got_repo_pack_fds_close(pack_fds);
4085 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4086 "object1 object2\n", getprogname());
4091 match_line(const char *line, regex_t *regex, size_t nmatch,
4092 regmatch_t *regmatch)
4094 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4097 static struct tog_color *
4098 match_color(struct tog_colors *colors, const char *line)
4100 struct tog_color *tc = NULL;
4102 STAILQ_FOREACH(tc, colors, entry) {
4103 if (match_line(line, &tc->regex, 0, NULL))
4110 static const struct got_error *
4111 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4112 WINDOW *window, int skipcol, regmatch_t *regmatch)
4114 const struct got_error *err = NULL;
4116 wchar_t *wline = NULL;
4117 int rme, rms, n, width, scrollx;
4118 int width0 = 0, width1 = 0, width2 = 0;
4119 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4123 rms = regmatch->rm_so;
4124 rme = regmatch->rm_eo;
4126 err = expand_tab(&exstr, line);
4130 /* Split the line into 3 segments, according to match offsets. */
4131 seg0 = strndup(exstr, rms);
4133 err = got_error_from_errno("strndup");
4136 seg1 = strndup(exstr + rms, rme - rms);
4138 err = got_error_from_errno("strndup");
4141 seg2 = strdup(exstr + rme);
4143 err = got_error_from_errno("strndup");
4147 /* draw up to matched token if we haven't scrolled past it */
4148 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4152 n = MAX(width0 - skipcol, 0);
4155 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4156 wlimit, col_tab_align, 1);
4159 waddwstr(window, &wline[scrollx]);
4169 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4173 wlen = wcslen(wline);
4175 width = wcwidth(wline[i]);
4177 /* should not happen, tabs are expanded */
4178 err = got_error(GOT_ERR_RANGE);
4181 if (width0 + w + width > skipcol)
4186 /* draw (visible part of) matched token (if scrolled into it) */
4187 if (width1 - w > 0) {
4188 wattron(window, A_STANDOUT);
4189 waddwstr(window, &wline[i]);
4190 wattroff(window, A_STANDOUT);
4191 wlimit -= (width1 - w);
4192 *wtotal += (width1 - w);
4196 if (wlimit > 0) { /* draw rest of line */
4198 if (skipcol > width0 + width1) {
4199 err = format_line(&wline, &width2, &scrollx, seg2,
4200 skipcol - (width0 + width1), wlimit,
4204 waddwstr(window, &wline[scrollx]);
4206 err = format_line(&wline, &width2, NULL, seg2, 0,
4207 wlimit, col_tab_align, 1);
4210 waddwstr(window, wline);
4224 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4227 int *eof, *first, *selected;
4229 if (view->type == TOG_VIEW_DIFF) {
4230 struct tog_diff_view_state *s = &view->state.diff;
4232 first = &s->first_displayed_line;
4236 } else if (view->type == TOG_VIEW_HELP) {
4237 struct tog_help_view_state *s = &view->state.help;
4239 first = &s->first_displayed_line;
4243 } else if (view->type == TOG_VIEW_BLAME) {
4244 struct tog_blame_view_state *s = &view->state.blame;
4246 first = &s->first_displayed_line;
4247 selected = &s->selected_line;
4253 /* Center gline in the middle of the page like vi(1). */
4254 if (*lineno < view->gline - (view->nlines - 3) / 2)
4256 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4265 *selected = view->gline <= (view->nlines - 3) / 2 ?
4266 view->gline : (view->nlines - 3) / 2 + 1;
4272 static const struct got_error *
4273 draw_file(struct tog_view *view, const char *header)
4275 struct tog_diff_view_state *s = &view->state.diff;
4276 regmatch_t *regmatch = &view->regmatch;
4277 const struct got_error *err;
4280 size_t linesize = 0;
4284 int max_lines = view->nlines;
4285 int nlines = s->nlines;
4288 s->lineno = s->first_displayed_line - 1;
4289 line_offset = s->lines[s->first_displayed_line - 1].offset;
4290 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4291 return got_error_from_errno("fseek");
4293 werase(view->window);
4295 if (view->gline > s->nlines - 1)
4296 view->gline = s->nlines - 1;
4299 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4300 1 : view->gline - (view->nlines - 3) / 2 :
4301 s->lineno + s->selected_line;
4303 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4304 return got_error_from_errno("asprintf");
4305 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4311 if (view_needs_focus_indication(view))
4312 wstandout(view->window);
4313 waddwstr(view->window, wline);
4316 while (width++ < view->ncols)
4317 waddch(view->window, ' ');
4318 if (view_needs_focus_indication(view))
4319 wstandend(view->window);
4329 while (max_lines > 0 && nprinted < max_lines) {
4330 enum got_diff_line_type linetype;
4333 linelen = getline(&line, &linesize, s->f);
4334 if (linelen == -1) {
4340 return got_ferror(s->f, GOT_ERR_IO);
4343 if (++s->lineno < s->first_displayed_line)
4345 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4347 if (s->lineno == view->hiline)
4350 /* Set view->maxx based on full line length. */
4351 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4357 view->maxx = MAX(view->maxx, width);
4361 linetype = s->lines[s->lineno].type;
4362 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4363 linetype < GOT_DIFF_LINE_CONTEXT)
4364 attr |= COLOR_PAIR(linetype);
4366 wattron(view->window, attr);
4367 if (s->first_displayed_line + nprinted == s->matched_line &&
4368 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4369 err = add_matched_line(&width, line, view->ncols, 0,
4370 view->window, view->x, regmatch);
4377 err = format_line(&wline, &width, &skip, line,
4378 view->x, view->ncols, 0, view->x ? 1 : 0);
4383 waddwstr(view->window, &wline[skip]);
4387 if (s->lineno == view->hiline) {
4388 /* highlight full gline length */
4389 while (width++ < view->ncols)
4390 waddch(view->window, ' ');
4392 if (width <= view->ncols - 1)
4393 waddch(view->window, '\n');
4396 wattroff(view->window, attr);
4397 if (++nprinted == 1)
4398 s->first_displayed_line = s->lineno;
4402 s->last_displayed_line = s->first_displayed_line +
4405 s->last_displayed_line = s->first_displayed_line;
4410 while (nprinted < view->nlines) {
4411 waddch(view->window, '\n');
4415 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4421 wstandout(view->window);
4422 waddwstr(view->window, wline);
4425 wstandend(view->window);
4432 get_datestr(time_t *time, char *datebuf)
4434 struct tm mytm, *tm;
4437 tm = gmtime_r(time, &mytm);
4440 s = asctime_r(tm, datebuf);
4443 p = strchr(s, '\n');
4449 static const struct got_error *
4450 get_changed_paths(struct got_pathlist_head *paths,
4451 struct got_commit_object *commit, struct got_repository *repo)
4453 const struct got_error *err = NULL;
4454 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4455 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4456 struct got_object_qid *qid;
4458 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4460 struct got_commit_object *pcommit;
4461 err = got_object_open_as_commit(&pcommit, repo,
4466 tree_id1 = got_object_id_dup(
4467 got_object_commit_get_tree_id(pcommit));
4468 if (tree_id1 == NULL) {
4469 got_object_commit_close(pcommit);
4470 return got_error_from_errno("got_object_id_dup");
4472 got_object_commit_close(pcommit);
4477 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4482 tree_id2 = got_object_commit_get_tree_id(commit);
4483 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4487 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4488 got_diff_tree_collect_changed_paths, paths, 0);
4491 got_object_tree_close(tree1);
4493 got_object_tree_close(tree2);
4498 static const struct got_error *
4499 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4500 off_t off, uint8_t type)
4502 struct got_diff_line *p;
4504 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4506 return got_error_from_errno("reallocarray");
4508 (*lines)[*nlines].offset = off;
4509 (*lines)[*nlines].type = type;
4515 static const struct got_error *
4516 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4517 struct got_object_id *commit_id, struct got_reflist_head *refs,
4518 struct got_repository *repo, FILE *outfile)
4520 const struct got_error *err = NULL;
4521 char datebuf[26], *datestr;
4522 struct got_commit_object *commit;
4523 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4524 time_t committer_time;
4525 const char *author, *committer;
4526 char *refs_str = NULL;
4527 struct got_pathlist_head changed_paths;
4528 struct got_pathlist_entry *pe;
4532 TAILQ_INIT(&changed_paths);
4535 err = build_refs_str(&refs_str, refs, commit_id, repo);
4540 err = got_object_open_as_commit(&commit, repo, commit_id);
4544 err = got_object_id_str(&id_str, commit_id);
4546 err = got_error_from_errno("got_object_id_str");
4550 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4554 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4555 refs_str ? refs_str : "", refs_str ? ")" : "");
4557 err = got_error_from_errno("fprintf");
4561 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4565 n = fprintf(outfile, "from: %s\n",
4566 got_object_commit_get_author(commit));
4568 err = got_error_from_errno("fprintf");
4572 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4576 committer_time = got_object_commit_get_committer_time(commit);
4577 datestr = get_datestr(&committer_time, datebuf);
4579 n = fprintf(outfile, "date: %s UTC\n", datestr);
4581 err = got_error_from_errno("fprintf");
4585 err = add_line_metadata(lines, nlines, outoff,
4586 GOT_DIFF_LINE_DATE);
4590 author = got_object_commit_get_author(commit);
4591 committer = got_object_commit_get_committer(commit);
4592 if (strcmp(author, committer) != 0) {
4593 n = fprintf(outfile, "via: %s\n", committer);
4595 err = got_error_from_errno("fprintf");
4599 err = add_line_metadata(lines, nlines, outoff,
4600 GOT_DIFF_LINE_AUTHOR);
4604 if (got_object_commit_get_nparents(commit) > 1) {
4605 const struct got_object_id_queue *parent_ids;
4606 struct got_object_qid *qid;
4608 parent_ids = got_object_commit_get_parent_ids(commit);
4609 STAILQ_FOREACH(qid, parent_ids, entry) {
4610 err = got_object_id_str(&id_str, &qid->id);
4613 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4615 err = got_error_from_errno("fprintf");
4619 err = add_line_metadata(lines, nlines, outoff,
4620 GOT_DIFF_LINE_META);
4628 err = got_object_commit_get_logmsg(&logmsg, commit);
4632 while ((line = strsep(&s, "\n")) != NULL) {
4633 n = fprintf(outfile, "%s\n", line);
4635 err = got_error_from_errno("fprintf");
4639 err = add_line_metadata(lines, nlines, outoff,
4640 GOT_DIFF_LINE_LOGMSG);
4645 err = get_changed_paths(&changed_paths, commit, repo);
4648 TAILQ_FOREACH(pe, &changed_paths, entry) {
4649 struct got_diff_changed_path *cp = pe->data;
4650 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4652 err = got_error_from_errno("fprintf");
4656 err = add_line_metadata(lines, nlines, outoff,
4657 GOT_DIFF_LINE_CHANGES);
4660 free((char *)pe->path);
4664 fputc('\n', outfile);
4666 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4668 got_pathlist_free(&changed_paths);
4672 got_object_commit_close(commit);
4681 static const struct got_error *
4682 create_diff(struct tog_diff_view_state *s)
4684 const struct got_error *err = NULL;
4689 s->lines = malloc(sizeof(*s->lines));
4690 if (s->lines == NULL)
4691 return got_error_from_errno("malloc");
4696 err = got_error_from_errno("got_opentemp");
4699 if (s->f && fclose(s->f) == EOF) {
4700 err = got_error_from_errno("fclose");
4706 err = got_object_get_type(&obj_type, s->repo, s->id1);
4708 err = got_object_get_type(&obj_type, s->repo, s->id2);
4713 case GOT_OBJ_TYPE_BLOB:
4714 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4715 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4716 s->label1, s->label2, tog_diff_algo, s->diff_context,
4717 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4719 case GOT_OBJ_TYPE_TREE:
4720 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4721 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4722 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4723 s->force_text_diff, s->repo, s->f);
4725 case GOT_OBJ_TYPE_COMMIT: {
4726 const struct got_object_id_queue *parent_ids;
4727 struct got_object_qid *pid;
4728 struct got_commit_object *commit2;
4729 struct got_reflist_head *refs;
4731 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4734 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4735 /* Show commit info if we're diffing to a parent/root commit. */
4736 if (s->id1 == NULL) {
4737 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4738 refs, s->repo, s->f);
4742 parent_ids = got_object_commit_get_parent_ids(commit2);
4743 STAILQ_FOREACH(pid, parent_ids, entry) {
4744 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4745 err = write_commit_info(&s->lines,
4746 &s->nlines, s->id2, refs, s->repo,
4754 got_object_commit_close(commit2);
4756 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4757 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4758 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4759 s->force_text_diff, s->repo, s->f);
4763 err = got_error(GOT_ERR_OBJ_TYPE);
4767 if (s->f && fflush(s->f) != 0 && err == NULL)
4768 err = got_error_from_errno("fflush");
4773 diff_view_indicate_progress(struct tog_view *view)
4775 mvwaddstr(view->window, 0, 0, "diffing...");
4780 static const struct got_error *
4781 search_start_diff_view(struct tog_view *view)
4783 struct tog_diff_view_state *s = &view->state.diff;
4785 s->matched_line = 0;
4790 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4791 size_t *nlines, int **first, int **last, int **match, int **selected)
4793 struct tog_diff_view_state *s = &view->state.diff;
4796 *nlines = s->nlines;
4797 *line_offsets = NULL;
4798 *match = &s->matched_line;
4799 *first = &s->first_displayed_line;
4800 *last = &s->last_displayed_line;
4801 *selected = &s->selected_line;
4804 static const struct got_error *
4805 search_next_view_match(struct tog_view *view)
4807 const struct got_error *err = NULL;
4811 size_t linesize = 0;
4813 off_t *line_offsets;
4815 int *first, *last, *match, *selected;
4817 if (!view->search_setup)
4818 return got_error_msg(GOT_ERR_NOT_IMPL,
4819 "view search not supported");
4820 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4823 if (!view->searching) {
4824 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4829 if (view->searching == TOG_SEARCH_FORWARD)
4830 lineno = *match + 1;
4832 lineno = *match - 1;
4834 lineno = *first - 1 + *selected;
4839 if (lineno <= 0 || lineno > nlines) {
4841 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4845 if (view->searching == TOG_SEARCH_FORWARD)
4851 offset = view->type == TOG_VIEW_DIFF ?
4852 view->state.diff.lines[lineno - 1].offset :
4853 line_offsets[lineno - 1];
4854 if (fseeko(f, offset, SEEK_SET) != 0) {
4856 return got_error_from_errno("fseeko");
4858 linelen = getline(&line, &linesize, f);
4859 if (linelen != -1) {
4861 err = expand_tab(&exstr, line);
4864 if (match_line(exstr, &view->regex, 1,
4866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4873 if (view->searching == TOG_SEARCH_FORWARD)
4888 static const struct got_error *
4889 close_diff_view(struct tog_view *view)
4891 const struct got_error *err = NULL;
4892 struct tog_diff_view_state *s = &view->state.diff;
4898 if (s->f && fclose(s->f) == EOF)
4899 err = got_error_from_errno("fclose");
4901 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4902 err = got_error_from_errno("fclose");
4904 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4905 err = got_error_from_errno("fclose");
4907 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4908 err = got_error_from_errno("close");
4910 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4911 err = got_error_from_errno("close");
4919 static const struct got_error *
4920 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4921 struct got_object_id *id2, const char *label1, const char *label2,
4922 int diff_context, int ignore_whitespace, int force_text_diff,
4923 struct tog_view *parent_view, struct got_repository *repo)
4925 const struct got_error *err;
4926 struct tog_diff_view_state *s = &view->state.diff;
4928 memset(s, 0, sizeof(*s));
4932 if (id1 != NULL && id2 != NULL) {
4934 err = got_object_get_type(&type1, repo, id1);
4937 err = got_object_get_type(&type2, repo, id2);
4942 return got_error(GOT_ERR_OBJ_TYPE);
4944 s->first_displayed_line = 1;
4945 s->last_displayed_line = view->nlines;
4946 s->selected_line = 1;
4954 s->id1 = got_object_id_dup(id1);
4956 return got_error_from_errno("got_object_id_dup");
4960 s->id2 = got_object_id_dup(id2);
4961 if (s->id2 == NULL) {
4962 err = got_error_from_errno("got_object_id_dup");
4966 s->f1 = got_opentemp();
4967 if (s->f1 == NULL) {
4968 err = got_error_from_errno("got_opentemp");
4972 s->f2 = got_opentemp();
4973 if (s->f2 == NULL) {
4974 err = got_error_from_errno("got_opentemp");
4978 s->fd1 = got_opentempfd();
4980 err = got_error_from_errno("got_opentempfd");
4984 s->fd2 = got_opentempfd();
4986 err = got_error_from_errno("got_opentempfd");
4990 s->first_displayed_line = 1;
4991 s->last_displayed_line = view->nlines;
4992 s->diff_context = diff_context;
4993 s->ignore_whitespace = ignore_whitespace;
4994 s->force_text_diff = force_text_diff;
4995 s->parent_view = parent_view;
4998 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5001 rc = init_pair(GOT_DIFF_LINE_MINUS,
5002 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5004 rc = init_pair(GOT_DIFF_LINE_PLUS,
5005 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5007 rc = init_pair(GOT_DIFF_LINE_HUNK,
5008 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5010 rc = init_pair(GOT_DIFF_LINE_META,
5011 get_color_value("TOG_COLOR_DIFF_META"), -1);
5013 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5014 get_color_value("TOG_COLOR_DIFF_META"), -1);
5016 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5017 get_color_value("TOG_COLOR_DIFF_META"), -1);
5019 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5020 get_color_value("TOG_COLOR_DIFF_META"), -1);
5022 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5023 get_color_value("TOG_COLOR_AUTHOR"), -1);
5025 rc = init_pair(GOT_DIFF_LINE_DATE,
5026 get_color_value("TOG_COLOR_DATE"), -1);
5028 err = got_error(GOT_ERR_RANGE);
5033 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5034 view_is_splitscreen(view))
5035 show_log_view(parent_view); /* draw border */
5036 diff_view_indicate_progress(view);
5038 err = create_diff(s);
5040 view->show = show_diff_view;
5041 view->input = input_diff_view;
5042 view->reset = reset_diff_view;
5043 view->close = close_diff_view;
5044 view->search_start = search_start_diff_view;
5045 view->search_setup = search_setup_diff_view;
5046 view->search_next = search_next_view_match;
5049 close_diff_view(view);
5053 static const struct got_error *
5054 show_diff_view(struct tog_view *view)
5056 const struct got_error *err;
5057 struct tog_diff_view_state *s = &view->state.diff;
5058 char *id_str1 = NULL, *id_str2, *header;
5059 const char *label1, *label2;
5062 err = got_object_id_str(&id_str1, s->id1);
5065 label1 = s->label1 ? s->label1 : id_str1;
5067 label1 = "/dev/null";
5069 err = got_object_id_str(&id_str2, s->id2);
5072 label2 = s->label2 ? s->label2 : id_str2;
5074 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5075 err = got_error_from_errno("asprintf");
5083 err = draw_file(view, header);
5088 static const struct got_error *
5089 set_selected_commit(struct tog_diff_view_state *s,
5090 struct commit_queue_entry *entry)
5092 const struct got_error *err;
5093 const struct got_object_id_queue *parent_ids;
5094 struct got_commit_object *selected_commit;
5095 struct got_object_qid *pid;
5098 s->id2 = got_object_id_dup(entry->id);
5100 return got_error_from_errno("got_object_id_dup");
5102 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5105 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5107 pid = STAILQ_FIRST(parent_ids);
5108 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5109 got_object_commit_close(selected_commit);
5113 static const struct got_error *
5114 reset_diff_view(struct tog_view *view)
5116 struct tog_diff_view_state *s = &view->state.diff;
5119 wclear(view->window);
5120 s->first_displayed_line = 1;
5121 s->last_displayed_line = view->nlines;
5122 s->matched_line = 0;
5123 diff_view_indicate_progress(view);
5124 return create_diff(s);
5128 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5132 i = start = s->first_displayed_line - 1;
5134 while (s->lines[i].type != type) {
5138 return; /* do nothing, requested type not in file */
5141 s->selected_line = 1;
5142 s->first_displayed_line = i;
5146 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5150 i = start = s->first_displayed_line + 1;
5152 while (s->lines[i].type != type) {
5153 if (i == s->nlines - 1)
5156 return; /* do nothing, requested type not in file */
5159 s->selected_line = 1;
5160 s->first_displayed_line = i;
5163 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5165 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5168 static const struct got_error *
5169 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5171 const struct got_error *err = NULL;
5172 struct tog_diff_view_state *s = &view->state.diff;
5173 struct tog_log_view_state *ls;
5174 struct commit_queue_entry *old_selected_entry;
5176 size_t linesize = 0;
5178 int i, nscroll = view->nlines - 1, up = 0;
5180 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5187 view->x = MAX(view->maxx - view->ncols / 3, 0);
5192 if (view->x + view->ncols / 3 < view->maxx)
5193 view->x += 2; /* move two columns right */
5199 view->x -= MIN(view->x, 2); /* move two columns back */
5206 s->force_text_diff = !s->force_text_diff;
5208 s->ignore_whitespace = !s->ignore_whitespace;
5209 err = reset_diff_view(view);
5213 s->first_displayed_line = 1;
5222 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5228 if (s->first_displayed_line > 1)
5229 s->first_displayed_line--;
5240 if (s->first_displayed_line == 1) {
5245 while (i++ < nscroll && s->first_displayed_line > 1)
5246 s->first_displayed_line--;
5252 s->first_displayed_line++;
5269 while (!s->eof && i++ < nscroll) {
5270 linelen = getline(&line, &linesize, s->f);
5271 s->first_displayed_line++;
5272 if (linelen == -1) {
5276 err = got_ferror(s->f, GOT_ERR_IO);
5283 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5286 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5289 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5292 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5295 if (s->diff_context > 0) {
5297 s->matched_line = 0;
5298 diff_view_indicate_progress(view);
5299 err = create_diff(s);
5300 if (s->first_displayed_line + view->nlines - 1 >
5302 s->first_displayed_line = 1;
5303 s->last_displayed_line = view->nlines;
5309 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5311 s->matched_line = 0;
5312 diff_view_indicate_progress(view);
5313 err = create_diff(s);
5325 if (s->parent_view == NULL) {
5329 s->parent_view->count = view->count;
5331 if (s->parent_view->type == TOG_VIEW_LOG) {
5332 ls = &s->parent_view->state.log;
5333 old_selected_entry = ls->selected_entry;
5335 err = input_log_view(NULL, s->parent_view,
5336 up ? KEY_UP : KEY_DOWN);
5339 view->count = s->parent_view->count;
5341 if (old_selected_entry == ls->selected_entry)
5344 err = set_selected_commit(s, ls->selected_entry);
5347 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5348 struct tog_blame_view_state *bs;
5349 struct got_object_id *id, *prev_id;
5351 bs = &s->parent_view->state.blame;
5352 prev_id = get_annotation_for_line(bs->blame.lines,
5353 bs->blame.nlines, bs->last_diffed_line);
5355 err = input_blame_view(&view, s->parent_view,
5356 up ? KEY_UP : KEY_DOWN);
5359 view->count = s->parent_view->count;
5361 if (prev_id == NULL)
5363 id = get_selected_commit_id(bs->blame.lines,
5364 bs->blame.nlines, bs->first_displayed_line,
5369 if (!got_object_id_cmp(prev_id, id))
5372 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5376 s->first_displayed_line = 1;
5377 s->last_displayed_line = view->nlines;
5378 s->matched_line = 0;
5381 diff_view_indicate_progress(view);
5382 err = create_diff(s);
5392 static const struct got_error *
5393 cmd_diff(int argc, char *argv[])
5395 const struct got_error *error = NULL;
5396 struct got_repository *repo = NULL;
5397 struct got_worktree *worktree = NULL;
5398 struct got_object_id *id1 = NULL, *id2 = NULL;
5399 char *repo_path = NULL, *cwd = NULL;
5400 char *id_str1 = NULL, *id_str2 = NULL;
5401 char *label1 = NULL, *label2 = NULL;
5402 int diff_context = 3, ignore_whitespace = 0;
5403 int ch, force_text_diff = 0;
5405 struct tog_view *view;
5406 int *pack_fds = NULL;
5408 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5411 force_text_diff = 1;
5414 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5417 errx(1, "number of context lines is %s: %s",
5421 repo_path = realpath(optarg, NULL);
5422 if (repo_path == NULL)
5423 return got_error_from_errno2("realpath",
5425 got_path_strip_trailing_slashes(repo_path);
5428 ignore_whitespace = 1;
5440 usage_diff(); /* TODO show local worktree changes */
5441 } else if (argc == 2) {
5447 error = got_repo_pack_fds_open(&pack_fds);
5451 if (repo_path == NULL) {
5452 cwd = getcwd(NULL, 0);
5454 return got_error_from_errno("getcwd");
5455 error = got_worktree_open(&worktree, cwd);
5456 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5460 strdup(got_worktree_get_repo_path(worktree));
5462 repo_path = strdup(cwd);
5463 if (repo_path == NULL) {
5464 error = got_error_from_errno("strdup");
5469 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5475 error = apply_unveil(got_repo_get_path(repo), NULL);
5479 error = tog_load_refs(repo, 0);
5483 error = got_repo_match_object_id(&id1, &label1, id_str1,
5484 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5488 error = got_repo_match_object_id(&id2, &label2, id_str2,
5489 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5493 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5495 error = got_error_from_errno("view_open");
5498 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5499 ignore_whitespace, force_text_diff, NULL, repo);
5502 error = view_loop(view);
5509 const struct got_error *close_err = got_repo_close(repo);
5514 got_worktree_close(worktree);
5516 const struct got_error *pack_err =
5517 got_repo_pack_fds_close(pack_fds);
5530 "usage: %s blame [-c commit] [-r repository-path] path\n",
5535 struct tog_blame_line {
5537 struct got_object_id *id;
5540 static const struct got_error *
5541 draw_blame(struct tog_view *view)
5543 struct tog_blame_view_state *s = &view->state.blame;
5544 struct tog_blame *blame = &s->blame;
5545 regmatch_t *regmatch = &view->regmatch;
5546 const struct got_error *err;
5547 int lineno = 0, nprinted = 0;
5549 size_t linesize = 0;
5553 struct tog_blame_line *blame_line;
5554 struct got_object_id *prev_id = NULL;
5556 struct tog_color *tc;
5558 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5563 werase(view->window);
5565 if (asprintf(&line, "commit %s", id_str) == -1) {
5566 err = got_error_from_errno("asprintf");
5571 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5576 if (view_needs_focus_indication(view))
5577 wstandout(view->window);
5578 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5580 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5581 waddwstr(view->window, wline);
5582 while (width++ < view->ncols)
5583 waddch(view->window, ' ');
5585 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5586 if (view_needs_focus_indication(view))
5587 wstandend(view->window);
5591 if (view->gline > blame->nlines)
5592 view->gline = blame->nlines;
5594 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5595 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5596 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5598 return got_error_from_errno("asprintf");
5601 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5606 waddwstr(view->window, wline);
5609 if (width < view->ncols - 1)
5610 waddch(view->window, '\n');
5614 while (nprinted < view->nlines - 2) {
5615 linelen = getline(&line, &linesize, blame->f);
5616 if (linelen == -1) {
5617 if (feof(blame->f)) {
5622 return got_ferror(blame->f, GOT_ERR_IO);
5624 if (++lineno < s->first_displayed_line)
5626 if (view->gline && !gotoline(view, &lineno, &nprinted))
5629 /* Set view->maxx based on full line length. */
5630 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5637 view->maxx = MAX(view->maxx, width);
5639 if (nprinted == s->selected_line - 1)
5640 wstandout(view->window);
5642 if (blame->nlines > 0) {
5643 blame_line = &blame->lines[lineno - 1];
5644 if (blame_line->annotated && prev_id &&
5645 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5646 !(nprinted == s->selected_line - 1)) {
5647 waddstr(view->window, " ");
5648 } else if (blame_line->annotated) {
5650 err = got_object_id_str(&id_str,
5656 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5658 wattr_on(view->window,
5659 COLOR_PAIR(tc->colorpair), NULL);
5660 wprintw(view->window, "%.8s", id_str);
5662 wattr_off(view->window,
5663 COLOR_PAIR(tc->colorpair), NULL);
5665 prev_id = blame_line->id;
5667 waddstr(view->window, "........");
5671 waddstr(view->window, "........");
5675 if (nprinted == s->selected_line - 1)
5676 wstandend(view->window);
5677 waddstr(view->window, " ");
5679 if (view->ncols <= 9) {
5681 } else if (s->first_displayed_line + nprinted ==
5683 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5684 err = add_matched_line(&width, line, view->ncols - 9, 9,
5685 view->window, view->x, regmatch);
5693 err = format_line(&wline, &width, &skip, line,
5694 view->x, view->ncols - 9, 9, 1);
5699 waddwstr(view->window, &wline[skip]);
5705 if (width <= view->ncols - 1)
5706 waddch(view->window, '\n');
5707 if (++nprinted == 1)
5708 s->first_displayed_line = lineno;
5711 s->last_displayed_line = lineno;
5718 static const struct got_error *
5719 blame_cb(void *arg, int nlines, int lineno,
5720 struct got_commit_object *commit, struct got_object_id *id)
5722 const struct got_error *err = NULL;
5723 struct tog_blame_cb_args *a = arg;
5724 struct tog_blame_line *line;
5727 if (nlines != a->nlines ||
5728 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5729 return got_error(GOT_ERR_RANGE);
5731 errcode = pthread_mutex_lock(&tog_mutex);
5733 return got_error_set_errno(errcode, "pthread_mutex_lock");
5735 if (*a->quit) { /* user has quit the blame view */
5736 err = got_error(GOT_ERR_ITER_COMPLETED);
5741 goto done; /* no change in this commit */
5743 line = &a->lines[lineno - 1];
5744 if (line->annotated)
5747 line->id = got_object_id_dup(id);
5748 if (line->id == NULL) {
5749 err = got_error_from_errno("got_object_id_dup");
5752 line->annotated = 1;
5754 errcode = pthread_mutex_unlock(&tog_mutex);
5756 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5761 blame_thread(void *arg)
5763 const struct got_error *err, *close_err;
5764 struct tog_blame_thread_args *ta = arg;
5765 struct tog_blame_cb_args *a = ta->cb_args;
5766 int errcode, fd1 = -1, fd2 = -1;
5767 FILE *f1 = NULL, *f2 = NULL;
5769 fd1 = got_opentempfd();
5771 return (void *)got_error_from_errno("got_opentempfd");
5773 fd2 = got_opentempfd();
5775 err = got_error_from_errno("got_opentempfd");
5779 f1 = got_opentemp();
5781 err = (void *)got_error_from_errno("got_opentemp");
5784 f2 = got_opentemp();
5786 err = (void *)got_error_from_errno("got_opentemp");
5790 err = block_signals_used_by_main_thread();
5794 err = got_blame(ta->path, a->commit_id, ta->repo,
5795 tog_diff_algo, blame_cb, ta->cb_args,
5796 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5797 if (err && err->code == GOT_ERR_CANCELLED)
5800 errcode = pthread_mutex_lock(&tog_mutex);
5802 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5806 close_err = got_repo_close(ta->repo);
5812 errcode = pthread_mutex_unlock(&tog_mutex);
5813 if (errcode && err == NULL)
5814 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5817 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5818 err = got_error_from_errno("close");
5819 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5820 err = got_error_from_errno("close");
5821 if (f1 && fclose(f1) == EOF && err == NULL)
5822 err = got_error_from_errno("fclose");
5823 if (f2 && fclose(f2) == EOF && err == NULL)
5824 err = got_error_from_errno("fclose");
5829 static struct got_object_id *
5830 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5831 int first_displayed_line, int selected_line)
5833 struct tog_blame_line *line;
5838 line = &lines[first_displayed_line - 1 + selected_line - 1];
5839 if (!line->annotated)
5845 static struct got_object_id *
5846 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5849 struct tog_blame_line *line;
5851 if (nlines <= 0 || lineno >= nlines)
5854 line = &lines[lineno - 1];
5855 if (!line->annotated)
5861 static const struct got_error *
5862 stop_blame(struct tog_blame *blame)
5864 const struct got_error *err = NULL;
5867 if (blame->thread) {
5869 errcode = pthread_mutex_unlock(&tog_mutex);
5871 return got_error_set_errno(errcode,
5872 "pthread_mutex_unlock");
5873 errcode = pthread_join(blame->thread, (void **)&err);
5875 return got_error_set_errno(errcode, "pthread_join");
5876 errcode = pthread_mutex_lock(&tog_mutex);
5878 return got_error_set_errno(errcode,
5879 "pthread_mutex_lock");
5880 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5882 blame->thread = 0; //NULL;
5884 if (blame->thread_args.repo) {
5885 const struct got_error *close_err;
5886 close_err = got_repo_close(blame->thread_args.repo);
5889 blame->thread_args.repo = NULL;
5892 if (fclose(blame->f) == EOF && err == NULL)
5893 err = got_error_from_errno("fclose");
5897 for (i = 0; i < blame->nlines; i++)
5898 free(blame->lines[i].id);
5900 blame->lines = NULL;
5902 free(blame->cb_args.commit_id);
5903 blame->cb_args.commit_id = NULL;
5904 if (blame->pack_fds) {
5905 const struct got_error *pack_err =
5906 got_repo_pack_fds_close(blame->pack_fds);
5909 blame->pack_fds = NULL;
5914 static const struct got_error *
5915 cancel_blame_view(void *arg)
5917 const struct got_error *err = NULL;
5921 errcode = pthread_mutex_lock(&tog_mutex);
5923 return got_error_set_errno(errcode,
5924 "pthread_mutex_unlock");
5927 err = got_error(GOT_ERR_CANCELLED);
5929 errcode = pthread_mutex_unlock(&tog_mutex);
5931 return got_error_set_errno(errcode,
5932 "pthread_mutex_lock");
5937 static const struct got_error *
5938 run_blame(struct tog_view *view)
5940 struct tog_blame_view_state *s = &view->state.blame;
5941 struct tog_blame *blame = &s->blame;
5942 const struct got_error *err = NULL;
5943 struct got_commit_object *commit = NULL;
5944 struct got_blob_object *blob = NULL;
5945 struct got_repository *thread_repo = NULL;
5946 struct got_object_id *obj_id = NULL;
5947 int obj_type, fd = -1;
5948 int *pack_fds = NULL;
5950 err = got_object_open_as_commit(&commit, s->repo,
5951 &s->blamed_commit->id);
5955 fd = got_opentempfd();
5957 err = got_error_from_errno("got_opentempfd");
5961 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5965 err = got_object_get_type(&obj_type, s->repo, obj_id);
5969 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5970 err = got_error(GOT_ERR_OBJ_TYPE);
5974 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5977 blame->f = got_opentemp();
5978 if (blame->f == NULL) {
5979 err = got_error_from_errno("got_opentemp");
5982 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5983 &blame->line_offsets, blame->f, blob);
5986 if (blame->nlines == 0) {
5987 s->blame_complete = 1;
5991 /* Don't include \n at EOF in the blame line count. */
5992 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5995 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5996 if (blame->lines == NULL) {
5997 err = got_error_from_errno("calloc");
6001 err = got_repo_pack_fds_open(&pack_fds);
6004 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6009 blame->pack_fds = pack_fds;
6010 blame->cb_args.view = view;
6011 blame->cb_args.lines = blame->lines;
6012 blame->cb_args.nlines = blame->nlines;
6013 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6014 if (blame->cb_args.commit_id == NULL) {
6015 err = got_error_from_errno("got_object_id_dup");
6018 blame->cb_args.quit = &s->done;
6020 blame->thread_args.path = s->path;
6021 blame->thread_args.repo = thread_repo;
6022 blame->thread_args.cb_args = &blame->cb_args;
6023 blame->thread_args.complete = &s->blame_complete;
6024 blame->thread_args.cancel_cb = cancel_blame_view;
6025 blame->thread_args.cancel_arg = &s->done;
6026 s->blame_complete = 0;
6028 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6029 s->first_displayed_line = 1;
6030 s->last_displayed_line = view->nlines;
6031 s->selected_line = 1;
6033 s->matched_line = 0;
6037 got_object_commit_close(commit);
6038 if (fd != -1 && close(fd) == -1 && err == NULL)
6039 err = got_error_from_errno("close");
6041 got_object_blob_close(blob);
6048 static const struct got_error *
6049 open_blame_view(struct tog_view *view, char *path,
6050 struct got_object_id *commit_id, struct got_repository *repo)
6052 const struct got_error *err = NULL;
6053 struct tog_blame_view_state *s = &view->state.blame;
6055 STAILQ_INIT(&s->blamed_commits);
6057 s->path = strdup(path);
6058 if (s->path == NULL)
6059 return got_error_from_errno("strdup");
6061 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6067 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6068 s->first_displayed_line = 1;
6069 s->last_displayed_line = view->nlines;
6070 s->selected_line = 1;
6071 s->blame_complete = 0;
6073 s->commit_id = commit_id;
6074 memset(&s->blame, 0, sizeof(s->blame));
6076 STAILQ_INIT(&s->colors);
6077 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6078 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6079 get_color_value("TOG_COLOR_COMMIT"));
6084 view->show = show_blame_view;
6085 view->input = input_blame_view;
6086 view->reset = reset_blame_view;
6087 view->close = close_blame_view;
6088 view->search_start = search_start_blame_view;
6089 view->search_setup = search_setup_blame_view;
6090 view->search_next = search_next_view_match;
6092 return run_blame(view);
6095 static const struct got_error *
6096 close_blame_view(struct tog_view *view)
6098 const struct got_error *err = NULL;
6099 struct tog_blame_view_state *s = &view->state.blame;
6101 if (s->blame.thread)
6102 err = stop_blame(&s->blame);
6104 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6105 struct got_object_qid *blamed_commit;
6106 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6107 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6108 got_object_qid_free(blamed_commit);
6112 free_colors(&s->colors);
6116 static const struct got_error *
6117 search_start_blame_view(struct tog_view *view)
6119 struct tog_blame_view_state *s = &view->state.blame;
6121 s->matched_line = 0;
6126 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6127 size_t *nlines, int **first, int **last, int **match, int **selected)
6129 struct tog_blame_view_state *s = &view->state.blame;
6132 *nlines = s->blame.nlines;
6133 *line_offsets = s->blame.line_offsets;
6134 *match = &s->matched_line;
6135 *first = &s->first_displayed_line;
6136 *last = &s->last_displayed_line;
6137 *selected = &s->selected_line;
6140 static const struct got_error *
6141 show_blame_view(struct tog_view *view)
6143 const struct got_error *err = NULL;
6144 struct tog_blame_view_state *s = &view->state.blame;
6147 if (s->blame.thread == 0 && !s->blame_complete) {
6148 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6149 &s->blame.thread_args);
6151 return got_error_set_errno(errcode, "pthread_create");
6153 halfdelay(1); /* fast refresh while annotating */
6156 if (s->blame_complete)
6157 halfdelay(10); /* disable fast refresh */
6159 err = draw_blame(view);
6165 static const struct got_error *
6166 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6167 struct got_repository *repo, struct got_object_id *id)
6169 struct tog_view *log_view;
6170 const struct got_error *err = NULL;
6174 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6175 if (log_view == NULL)
6176 return got_error_from_errno("view_open");
6178 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6180 view_close(log_view);
6182 *new_view = log_view;
6187 static const struct got_error *
6188 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6190 const struct got_error *err = NULL, *thread_err = NULL;
6191 struct tog_view *diff_view;
6192 struct tog_blame_view_state *s = &view->state.blame;
6193 int eos, nscroll, begin_y = 0, begin_x = 0;
6195 eos = nscroll = view->nlines - 2;
6196 if (view_is_hsplit_top(view))
6204 view->x = MAX(view->maxx - view->ncols / 3, 0);
6209 if (view->x + view->ncols / 3 < view->maxx)
6210 view->x += 2; /* move two columns right */
6216 view->x -= MIN(view->x, 2); /* move two columns back */
6225 s->selected_line = 1;
6226 s->first_displayed_line = 1;
6231 if (s->blame.nlines < eos) {
6232 s->selected_line = s->blame.nlines;
6233 s->first_displayed_line = 1;
6235 s->selected_line = eos;
6236 s->first_displayed_line = s->blame.nlines - (eos - 1);
6243 if (s->selected_line > 1)
6245 else if (s->selected_line == 1 &&
6246 s->first_displayed_line > 1)
6247 s->first_displayed_line--;
6258 if (s->first_displayed_line == 1) {
6259 if (view->count > 1)
6261 s->selected_line = MAX(1, s->selected_line - nscroll);
6265 if (s->first_displayed_line > nscroll)
6266 s->first_displayed_line -= nscroll;
6268 s->first_displayed_line = 1;
6273 if (s->selected_line < eos && s->first_displayed_line +
6274 s->selected_line <= s->blame.nlines)
6276 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6277 s->first_displayed_line++;
6283 struct got_object_id *id = NULL;
6286 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6287 s->first_displayed_line, s->selected_line);
6291 struct got_commit_object *commit, *pcommit;
6292 struct got_object_qid *pid;
6293 struct got_object_id *blob_id = NULL;
6295 err = got_object_open_as_commit(&commit,
6300 got_object_commit_get_parent_ids(commit));
6302 got_object_commit_close(commit);
6305 /* Check if path history ends here. */
6306 err = got_object_open_as_commit(&pcommit,
6310 err = got_object_id_by_path(&blob_id, s->repo,
6312 got_object_commit_close(pcommit);
6314 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6316 got_object_commit_close(commit);
6319 err = got_object_get_type(&obj_type, s->repo,
6322 /* Can't blame non-blob type objects. */
6323 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6324 got_object_commit_close(commit);
6327 err = got_object_qid_alloc(&s->blamed_commit,
6329 got_object_commit_close(commit);
6331 if (got_object_id_cmp(id,
6332 &s->blamed_commit->id) == 0)
6334 err = got_object_qid_alloc(&s->blamed_commit,
6340 thread_err = stop_blame(&s->blame);
6344 STAILQ_INSERT_HEAD(&s->blamed_commits,
6345 s->blamed_commit, entry);
6346 err = run_blame(view);
6352 struct got_object_qid *first;
6355 first = STAILQ_FIRST(&s->blamed_commits);
6356 if (!got_object_id_cmp(&first->id, s->commit_id))
6359 thread_err = stop_blame(&s->blame);
6363 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6364 got_object_qid_free(s->blamed_commit);
6366 STAILQ_FIRST(&s->blamed_commits);
6367 err = run_blame(view);
6374 s->id_to_log = get_selected_commit_id(s->blame.lines,
6375 s->blame.nlines, s->first_displayed_line, s->selected_line);
6377 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6381 struct got_object_id *id = NULL;
6382 struct got_object_qid *pid;
6383 struct got_commit_object *commit = NULL;
6386 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6387 s->first_displayed_line, s->selected_line);
6390 err = got_object_open_as_commit(&commit, s->repo, id);
6393 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6395 /* traversed from diff view, release diff resources */
6396 err = close_diff_view(*new_view);
6399 diff_view = *new_view;
6401 if (view_is_parent_view(view))
6402 view_get_split(view, &begin_y, &begin_x);
6404 diff_view = view_open(0, 0, begin_y, begin_x,
6406 if (diff_view == NULL) {
6407 got_object_commit_close(commit);
6408 err = got_error_from_errno("view_open");
6412 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6413 id, NULL, NULL, 3, 0, 0, view, s->repo);
6414 got_object_commit_close(commit);
6416 view_close(diff_view);
6419 s->last_diffed_line = s->first_displayed_line - 1 +
6422 break; /* still open from active diff view */
6423 if (view_is_parent_view(view) &&
6424 view->mode == TOG_VIEW_SPLIT_HRZN) {
6425 err = view_init_hsplit(view, begin_y);
6431 diff_view->focussed = 1;
6432 diff_view->mode = view->mode;
6433 diff_view->nlines = view->lines - begin_y;
6434 if (view_is_parent_view(view)) {
6435 view_transfer_size(diff_view, view);
6436 err = view_close_child(view);
6439 err = view_set_child(view, diff_view);
6442 view->focus_child = 1;
6444 *new_view = diff_view;
6457 if (s->last_displayed_line >= s->blame.nlines &&
6458 s->selected_line >= MIN(s->blame.nlines,
6459 view->nlines - 2)) {
6463 if (s->last_displayed_line >= s->blame.nlines &&
6464 s->selected_line < view->nlines - 2) {
6466 MIN(nscroll, s->last_displayed_line -
6467 s->first_displayed_line - s->selected_line + 1);
6469 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6470 s->first_displayed_line += nscroll;
6472 s->first_displayed_line =
6473 s->blame.nlines - (view->nlines - 3);
6476 if (s->selected_line > view->nlines - 2) {
6477 s->selected_line = MIN(s->blame.nlines,
6485 return thread_err ? thread_err : err;
6488 static const struct got_error *
6489 reset_blame_view(struct tog_view *view)
6491 const struct got_error *err;
6492 struct tog_blame_view_state *s = &view->state.blame;
6496 err = stop_blame(&s->blame);
6500 return run_blame(view);
6503 static const struct got_error *
6504 cmd_blame(int argc, char *argv[])
6506 const struct got_error *error;
6507 struct got_repository *repo = NULL;
6508 struct got_worktree *worktree = NULL;
6509 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6510 char *link_target = NULL;
6511 struct got_object_id *commit_id = NULL;
6512 struct got_commit_object *commit = NULL;
6513 char *commit_id_str = NULL;
6515 struct tog_view *view;
6516 int *pack_fds = NULL;
6518 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6521 commit_id_str = optarg;
6524 repo_path = realpath(optarg, NULL);
6525 if (repo_path == NULL)
6526 return got_error_from_errno2("realpath",
6541 error = got_repo_pack_fds_open(&pack_fds);
6545 if (repo_path == NULL) {
6546 cwd = getcwd(NULL, 0);
6548 return got_error_from_errno("getcwd");
6549 error = got_worktree_open(&worktree, cwd);
6550 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6554 strdup(got_worktree_get_repo_path(worktree));
6556 repo_path = strdup(cwd);
6557 if (repo_path == NULL) {
6558 error = got_error_from_errno("strdup");
6563 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6567 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6574 error = apply_unveil(got_repo_get_path(repo), NULL);
6578 error = tog_load_refs(repo, 0);
6582 if (commit_id_str == NULL) {
6583 struct got_reference *head_ref;
6584 error = got_ref_open(&head_ref, repo, worktree ?
6585 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6588 error = got_ref_resolve(&commit_id, repo, head_ref);
6589 got_ref_close(head_ref);
6591 error = got_repo_match_object_id(&commit_id, NULL,
6592 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6597 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6599 error = got_error_from_errno("view_open");
6603 error = got_object_open_as_commit(&commit, repo, commit_id);
6607 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6612 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6617 /* Release work tree lock. */
6618 got_worktree_close(worktree);
6621 error = view_loop(view);
6629 got_object_commit_close(commit);
6631 got_worktree_close(worktree);
6633 const struct got_error *close_err = got_repo_close(repo);
6638 const struct got_error *pack_err =
6639 got_repo_pack_fds_close(pack_fds);
6647 static const struct got_error *
6648 draw_tree_entries(struct tog_view *view, const char *parent_path)
6650 struct tog_tree_view_state *s = &view->state.tree;
6651 const struct got_error *err = NULL;
6652 struct got_tree_entry *te;
6655 struct tog_color *tc;
6656 int width, n, nentries, i = 1;
6657 int limit = view->nlines;
6660 if (view_is_hsplit_top(view))
6661 --limit; /* border */
6663 werase(view->window);
6668 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6672 if (view_needs_focus_indication(view))
6673 wstandout(view->window);
6674 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6676 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6677 waddwstr(view->window, wline);
6680 while (width++ < view->ncols)
6681 waddch(view->window, ' ');
6683 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6684 if (view_needs_focus_indication(view))
6685 wstandend(view->window);
6690 if (s->first_displayed_entry) {
6691 i += got_tree_entry_get_index(s->first_displayed_entry);
6692 if (s->tree != s->root)
6693 ++i; /* account for ".." entry */
6695 nentries = got_object_tree_get_nentries(s->tree);
6696 if (asprintf(&index, "[%d/%d] %s",
6697 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6698 return got_error_from_errno("asprintf");
6699 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6703 waddwstr(view->window, wline);
6706 if (width < view->ncols - 1)
6707 waddch(view->window, '\n');
6710 waddch(view->window, '\n');
6714 if (s->first_displayed_entry == NULL) {
6715 te = got_object_tree_get_first_entry(s->tree);
6716 if (s->selected == 0) {
6718 wstandout(view->window);
6719 s->selected_entry = NULL;
6721 waddstr(view->window, " ..\n"); /* parent directory */
6722 if (s->selected == 0 && view->focussed)
6723 wstandend(view->window);
6730 te = s->first_displayed_entry;
6733 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6734 char *line = NULL, *id_str = NULL, *link_target = NULL;
6735 const char *modestr = "";
6738 te = got_object_tree_get_entry(s->tree, i);
6739 mode = got_tree_entry_get_mode(te);
6742 err = got_object_id_str(&id_str,
6743 got_tree_entry_get_id(te));
6745 return got_error_from_errno(
6746 "got_object_id_str");
6748 if (got_object_tree_entry_is_submodule(te))
6750 else if (S_ISLNK(mode)) {
6753 err = got_tree_entry_get_symlink_target(&link_target,
6759 for (i = 0; i < strlen(link_target); i++) {
6760 if (!isprint((unsigned char)link_target[i]))
6761 link_target[i] = '?';
6765 else if (S_ISDIR(mode))
6767 else if (mode & S_IXUSR)
6769 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6770 got_tree_entry_get_name(te), modestr,
6771 link_target ? " -> ": "",
6772 link_target ? link_target : "") == -1) {
6775 return got_error_from_errno("asprintf");
6779 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6785 if (n == s->selected) {
6787 wstandout(view->window);
6788 s->selected_entry = te;
6790 tc = match_color(&s->colors, line);
6792 wattr_on(view->window,
6793 COLOR_PAIR(tc->colorpair), NULL);
6794 waddwstr(view->window, wline);
6796 wattr_off(view->window,
6797 COLOR_PAIR(tc->colorpair), NULL);
6798 if (width < view->ncols - 1)
6799 waddch(view->window, '\n');
6800 if (n == s->selected && view->focussed)
6801 wstandend(view->window);
6807 s->last_displayed_entry = te;
6816 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6818 struct got_tree_entry *te;
6819 int isroot = s->tree == s->root;
6822 if (s->first_displayed_entry == NULL)
6825 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6826 while (i++ < maxscroll) {
6829 s->first_displayed_entry = NULL;
6832 s->first_displayed_entry = te;
6833 te = got_tree_entry_get_prev(s->tree, te);
6837 static const struct got_error *
6838 tree_scroll_down(struct tog_view *view, int maxscroll)
6840 struct tog_tree_view_state *s = &view->state.tree;
6841 struct got_tree_entry *next, *last;
6844 if (s->first_displayed_entry)
6845 next = got_tree_entry_get_next(s->tree,
6846 s->first_displayed_entry);
6848 next = got_object_tree_get_first_entry(s->tree);
6850 last = s->last_displayed_entry;
6851 while (next && n++ < maxscroll) {
6853 s->last_displayed_entry = last;
6854 last = got_tree_entry_get_next(s->tree, last);
6856 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6857 s->first_displayed_entry = next;
6858 next = got_tree_entry_get_next(s->tree, next);
6865 static const struct got_error *
6866 tree_entry_path(char **path, struct tog_parent_trees *parents,
6867 struct got_tree_entry *te)
6869 const struct got_error *err = NULL;
6870 struct tog_parent_tree *pt;
6871 size_t len = 2; /* for leading slash and NUL */
6873 TAILQ_FOREACH(pt, parents, entry)
6874 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6877 len += strlen(got_tree_entry_get_name(te));
6879 *path = calloc(1, len);
6881 return got_error_from_errno("calloc");
6884 pt = TAILQ_LAST(parents, tog_parent_trees);
6886 const char *name = got_tree_entry_get_name(pt->selected_entry);
6887 if (strlcat(*path, name, len) >= len) {
6888 err = got_error(GOT_ERR_NO_SPACE);
6891 if (strlcat(*path, "/", len) >= len) {
6892 err = got_error(GOT_ERR_NO_SPACE);
6895 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6898 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6899 err = got_error(GOT_ERR_NO_SPACE);
6911 static const struct got_error *
6912 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6913 struct got_tree_entry *te, struct tog_parent_trees *parents,
6914 struct got_object_id *commit_id, struct got_repository *repo)
6916 const struct got_error *err = NULL;
6918 struct tog_view *blame_view;
6922 err = tree_entry_path(&path, parents, te);
6926 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6927 if (blame_view == NULL) {
6928 err = got_error_from_errno("view_open");
6932 err = open_blame_view(blame_view, path, commit_id, repo);
6934 if (err->code == GOT_ERR_CANCELLED)
6936 view_close(blame_view);
6938 *new_view = blame_view;
6944 static const struct got_error *
6945 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6946 struct tog_tree_view_state *s)
6948 struct tog_view *log_view;
6949 const struct got_error *err = NULL;
6954 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6955 if (log_view == NULL)
6956 return got_error_from_errno("view_open");
6958 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6962 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6965 view_close(log_view);
6967 *new_view = log_view;
6972 static const struct got_error *
6973 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6974 const char *head_ref_name, struct got_repository *repo)
6976 const struct got_error *err = NULL;
6977 char *commit_id_str = NULL;
6978 struct tog_tree_view_state *s = &view->state.tree;
6979 struct got_commit_object *commit = NULL;
6981 TAILQ_INIT(&s->parents);
6982 STAILQ_INIT(&s->colors);
6984 s->commit_id = got_object_id_dup(commit_id);
6985 if (s->commit_id == NULL)
6986 return got_error_from_errno("got_object_id_dup");
6988 err = got_object_open_as_commit(&commit, repo, commit_id);
6993 * The root is opened here and will be closed when the view is closed.
6994 * Any visited subtrees and their path-wise parents are opened and
6997 err = got_object_open_as_tree(&s->root, repo,
6998 got_object_commit_get_tree_id(commit));
7003 err = got_object_id_str(&commit_id_str, commit_id);
7007 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7008 err = got_error_from_errno("asprintf");
7012 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7013 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7014 if (head_ref_name) {
7015 s->head_ref_name = strdup(head_ref_name);
7016 if (s->head_ref_name == NULL) {
7017 err = got_error_from_errno("strdup");
7023 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7024 err = add_color(&s->colors, "\\$$",
7025 TOG_COLOR_TREE_SUBMODULE,
7026 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7029 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7030 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7033 err = add_color(&s->colors, "/$",
7034 TOG_COLOR_TREE_DIRECTORY,
7035 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7039 err = add_color(&s->colors, "\\*$",
7040 TOG_COLOR_TREE_EXECUTABLE,
7041 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7045 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7046 get_color_value("TOG_COLOR_COMMIT"));
7051 view->show = show_tree_view;
7052 view->input = input_tree_view;
7053 view->close = close_tree_view;
7054 view->search_start = search_start_tree_view;
7055 view->search_next = search_next_tree_view;
7057 free(commit_id_str);
7059 got_object_commit_close(commit);
7061 close_tree_view(view);
7065 static const struct got_error *
7066 close_tree_view(struct tog_view *view)
7068 struct tog_tree_view_state *s = &view->state.tree;
7070 free_colors(&s->colors);
7071 free(s->tree_label);
7072 s->tree_label = NULL;
7074 s->commit_id = NULL;
7075 free(s->head_ref_name);
7076 s->head_ref_name = NULL;
7077 while (!TAILQ_EMPTY(&s->parents)) {
7078 struct tog_parent_tree *parent;
7079 parent = TAILQ_FIRST(&s->parents);
7080 TAILQ_REMOVE(&s->parents, parent, entry);
7081 if (parent->tree != s->root)
7082 got_object_tree_close(parent->tree);
7086 if (s->tree != NULL && s->tree != s->root)
7087 got_object_tree_close(s->tree);
7089 got_object_tree_close(s->root);
7093 static const struct got_error *
7094 search_start_tree_view(struct tog_view *view)
7096 struct tog_tree_view_state *s = &view->state.tree;
7098 s->matched_entry = NULL;
7103 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7105 regmatch_t regmatch;
7107 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7111 static const struct got_error *
7112 search_next_tree_view(struct tog_view *view)
7114 struct tog_tree_view_state *s = &view->state.tree;
7115 struct got_tree_entry *te = NULL;
7117 if (!view->searching) {
7118 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7122 if (s->matched_entry) {
7123 if (view->searching == TOG_SEARCH_FORWARD) {
7124 if (s->selected_entry)
7125 te = got_tree_entry_get_next(s->tree,
7128 te = got_object_tree_get_first_entry(s->tree);
7130 if (s->selected_entry == NULL)
7131 te = got_object_tree_get_last_entry(s->tree);
7133 te = got_tree_entry_get_prev(s->tree,
7137 if (s->selected_entry)
7138 te = s->selected_entry;
7139 else if (view->searching == TOG_SEARCH_FORWARD)
7140 te = got_object_tree_get_first_entry(s->tree);
7142 te = got_object_tree_get_last_entry(s->tree);
7147 if (s->matched_entry == NULL) {
7148 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7151 if (view->searching == TOG_SEARCH_FORWARD)
7152 te = got_object_tree_get_first_entry(s->tree);
7154 te = got_object_tree_get_last_entry(s->tree);
7157 if (match_tree_entry(te, &view->regex)) {
7158 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7159 s->matched_entry = te;
7163 if (view->searching == TOG_SEARCH_FORWARD)
7164 te = got_tree_entry_get_next(s->tree, te);
7166 te = got_tree_entry_get_prev(s->tree, te);
7169 if (s->matched_entry) {
7170 s->first_displayed_entry = s->matched_entry;
7177 static const struct got_error *
7178 show_tree_view(struct tog_view *view)
7180 const struct got_error *err = NULL;
7181 struct tog_tree_view_state *s = &view->state.tree;
7184 err = tree_entry_path(&parent_path, &s->parents, NULL);
7188 err = draw_tree_entries(view, parent_path);
7195 static const struct got_error *
7196 tree_goto_line(struct tog_view *view, int nlines)
7198 const struct got_error *err = NULL;
7199 struct tog_tree_view_state *s = &view->state.tree;
7200 struct got_tree_entry **fte, **lte, **ste;
7201 int g, last, first = 1, i = 1;
7202 int root = s->tree == s->root;
7203 int off = root ? 1 : 2;
7210 else if (g > got_object_tree_get_nentries(s->tree))
7211 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7213 fte = &s->first_displayed_entry;
7214 lte = &s->last_displayed_entry;
7215 ste = &s->selected_entry;
7218 first = got_tree_entry_get_index(*fte);
7219 first += off; /* account for ".." */
7221 last = got_tree_entry_get_index(*lte);
7224 if (g >= first && g <= last && g - first < nlines) {
7225 s->selected = g - first;
7226 return NULL; /* gline is on the current page */
7230 i = got_tree_entry_get_index(*ste);
7235 err = tree_scroll_down(view, g - i);
7238 if (got_tree_entry_get_index(*lte) >=
7239 got_object_tree_get_nentries(s->tree) - 1 &&
7240 first + s->selected < g &&
7241 s->selected < s->ndisplayed - 1) {
7242 first = got_tree_entry_get_index(*fte);
7244 s->selected = g - first;
7247 tree_scroll_up(s, i - g);
7250 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7251 s->selected = g - 1;
7256 static const struct got_error *
7257 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7259 const struct got_error *err = NULL;
7260 struct tog_tree_view_state *s = &view->state.tree;
7261 struct got_tree_entry *te;
7262 int n, nscroll = view->nlines - 3;
7265 return tree_goto_line(view, nscroll);
7269 s->show_ids = !s->show_ids;
7274 if (!s->selected_entry)
7276 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7280 err = view_request_new(new_view, view, TOG_VIEW_REF);
7286 if (s->tree == s->root)
7287 s->first_displayed_entry =
7288 got_object_tree_get_first_entry(s->tree);
7290 s->first_displayed_entry = NULL;
7294 int eos = view->nlines - 3;
7296 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7300 te = got_object_tree_get_last_entry(s->tree);
7301 for (n = 0; n < eos; n++) {
7303 if (s->tree != s->root) {
7304 s->first_displayed_entry = NULL;
7309 s->first_displayed_entry = te;
7310 te = got_tree_entry_get_prev(s->tree, te);
7313 s->selected = n - 1;
7319 if (s->selected > 0) {
7323 tree_scroll_up(s, 1);
7324 if (s->selected_entry == NULL ||
7325 (s->tree == s->root && s->selected_entry ==
7326 got_object_tree_get_first_entry(s->tree)))
7336 if (s->tree == s->root) {
7337 if (got_object_tree_get_first_entry(s->tree) ==
7338 s->first_displayed_entry)
7339 s->selected -= MIN(s->selected, nscroll);
7341 if (s->first_displayed_entry == NULL)
7342 s->selected -= MIN(s->selected, nscroll);
7344 tree_scroll_up(s, MAX(0, nscroll));
7345 if (s->selected_entry == NULL ||
7346 (s->tree == s->root && s->selected_entry ==
7347 got_object_tree_get_first_entry(s->tree)))
7353 if (s->selected < s->ndisplayed - 1) {
7357 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7359 /* can't scroll any further */
7363 tree_scroll_down(view, 1);
7373 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7375 /* can't scroll any further; move cursor down */
7376 if (s->selected < s->ndisplayed - 1)
7377 s->selected += MIN(nscroll,
7378 s->ndisplayed - s->selected - 1);
7383 tree_scroll_down(view, nscroll);
7388 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7389 struct tog_parent_tree *parent;
7390 /* user selected '..' */
7391 if (s->tree == s->root) {
7395 parent = TAILQ_FIRST(&s->parents);
7396 TAILQ_REMOVE(&s->parents, parent,
7398 got_object_tree_close(s->tree);
7399 s->tree = parent->tree;
7400 s->first_displayed_entry =
7401 parent->first_displayed_entry;
7403 parent->selected_entry;
7404 s->selected = parent->selected;
7405 if (s->selected > view->nlines - 3) {
7406 err = offset_selection_down(view);
7411 } else if (S_ISDIR(got_tree_entry_get_mode(
7412 s->selected_entry))) {
7413 struct got_tree_object *subtree;
7415 err = got_object_open_as_tree(&subtree, s->repo,
7416 got_tree_entry_get_id(s->selected_entry));
7419 err = tree_view_visit_subtree(s, subtree);
7421 got_object_tree_close(subtree);
7424 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7425 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7428 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7429 s->selected = view->nlines - 4;
7445 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7450 static const struct got_error *
7451 cmd_tree(int argc, char *argv[])
7453 const struct got_error *error;
7454 struct got_repository *repo = NULL;
7455 struct got_worktree *worktree = NULL;
7456 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7457 struct got_object_id *commit_id = NULL;
7458 struct got_commit_object *commit = NULL;
7459 const char *commit_id_arg = NULL;
7461 struct got_reference *ref = NULL;
7462 const char *head_ref_name = NULL;
7464 struct tog_view *view;
7465 int *pack_fds = NULL;
7467 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7470 commit_id_arg = optarg;
7473 repo_path = realpath(optarg, NULL);
7474 if (repo_path == NULL)
7475 return got_error_from_errno2("realpath",
7490 error = got_repo_pack_fds_open(&pack_fds);
7494 if (repo_path == NULL) {
7495 cwd = getcwd(NULL, 0);
7497 return got_error_from_errno("getcwd");
7498 error = got_worktree_open(&worktree, cwd);
7499 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7503 strdup(got_worktree_get_repo_path(worktree));
7505 repo_path = strdup(cwd);
7506 if (repo_path == NULL) {
7507 error = got_error_from_errno("strdup");
7512 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7516 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7523 error = apply_unveil(got_repo_get_path(repo), NULL);
7527 error = tog_load_refs(repo, 0);
7531 if (commit_id_arg == NULL) {
7532 error = got_repo_match_object_id(&commit_id, &label,
7533 worktree ? got_worktree_get_head_ref_name(worktree) :
7534 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7537 head_ref_name = label;
7539 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7541 head_ref_name = got_ref_get_name(ref);
7542 else if (error->code != GOT_ERR_NOT_REF)
7544 error = got_repo_match_object_id(&commit_id, NULL,
7545 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7550 error = got_object_open_as_commit(&commit, repo, commit_id);
7554 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7556 error = got_error_from_errno("view_open");
7559 error = open_tree_view(view, commit_id, head_ref_name, repo);
7562 if (!got_path_is_root_dir(in_repo_path)) {
7563 error = tree_view_walk_path(&view->state.tree, commit,
7570 /* Release work tree lock. */
7571 got_worktree_close(worktree);
7574 error = view_loop(view);
7583 const struct got_error *close_err = got_repo_close(repo);
7588 const struct got_error *pack_err =
7589 got_repo_pack_fds_close(pack_fds);
7597 static const struct got_error *
7598 ref_view_load_refs(struct tog_ref_view_state *s)
7600 struct got_reflist_entry *sre;
7601 struct tog_reflist_entry *re;
7604 TAILQ_FOREACH(sre, &tog_refs, entry) {
7605 if (strncmp(got_ref_get_name(sre->ref),
7606 "refs/got/", 9) == 0 &&
7607 strncmp(got_ref_get_name(sre->ref),
7608 "refs/got/backup/", 16) != 0)
7611 re = malloc(sizeof(*re));
7613 return got_error_from_errno("malloc");
7615 re->ref = got_ref_dup(sre->ref);
7616 if (re->ref == NULL)
7617 return got_error_from_errno("got_ref_dup");
7618 re->idx = s->nrefs++;
7619 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7622 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7627 ref_view_free_refs(struct tog_ref_view_state *s)
7629 struct tog_reflist_entry *re;
7631 while (!TAILQ_EMPTY(&s->refs)) {
7632 re = TAILQ_FIRST(&s->refs);
7633 TAILQ_REMOVE(&s->refs, re, entry);
7634 got_ref_close(re->ref);
7639 static const struct got_error *
7640 open_ref_view(struct tog_view *view, struct got_repository *repo)
7642 const struct got_error *err = NULL;
7643 struct tog_ref_view_state *s = &view->state.ref;
7645 s->selected_entry = 0;
7648 TAILQ_INIT(&s->refs);
7649 STAILQ_INIT(&s->colors);
7651 err = ref_view_load_refs(s);
7655 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7656 err = add_color(&s->colors, "^refs/heads/",
7657 TOG_COLOR_REFS_HEADS,
7658 get_color_value("TOG_COLOR_REFS_HEADS"));
7662 err = add_color(&s->colors, "^refs/tags/",
7663 TOG_COLOR_REFS_TAGS,
7664 get_color_value("TOG_COLOR_REFS_TAGS"));
7668 err = add_color(&s->colors, "^refs/remotes/",
7669 TOG_COLOR_REFS_REMOTES,
7670 get_color_value("TOG_COLOR_REFS_REMOTES"));
7674 err = add_color(&s->colors, "^refs/got/backup/",
7675 TOG_COLOR_REFS_BACKUP,
7676 get_color_value("TOG_COLOR_REFS_BACKUP"));
7681 view->show = show_ref_view;
7682 view->input = input_ref_view;
7683 view->close = close_ref_view;
7684 view->search_start = search_start_ref_view;
7685 view->search_next = search_next_ref_view;
7688 free_colors(&s->colors);
7692 static const struct got_error *
7693 close_ref_view(struct tog_view *view)
7695 struct tog_ref_view_state *s = &view->state.ref;
7697 ref_view_free_refs(s);
7698 free_colors(&s->colors);
7703 static const struct got_error *
7704 resolve_reflist_entry(struct got_object_id **commit_id,
7705 struct tog_reflist_entry *re, struct got_repository *repo)
7707 const struct got_error *err = NULL;
7708 struct got_object_id *obj_id;
7709 struct got_tag_object *tag = NULL;
7714 err = got_ref_resolve(&obj_id, repo, re->ref);
7718 err = got_object_get_type(&obj_type, repo, obj_id);
7723 case GOT_OBJ_TYPE_COMMIT:
7724 *commit_id = obj_id;
7726 case GOT_OBJ_TYPE_TAG:
7727 err = got_object_open_as_tag(&tag, repo, obj_id);
7731 err = got_object_get_type(&obj_type, repo,
7732 got_object_tag_get_object_id(tag));
7735 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7736 err = got_error(GOT_ERR_OBJ_TYPE);
7739 *commit_id = got_object_id_dup(
7740 got_object_tag_get_object_id(tag));
7741 if (*commit_id == NULL) {
7742 err = got_error_from_errno("got_object_id_dup");
7747 err = got_error(GOT_ERR_OBJ_TYPE);
7753 got_object_tag_close(tag);
7761 static const struct got_error *
7762 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7763 struct tog_reflist_entry *re, struct got_repository *repo)
7765 struct tog_view *log_view;
7766 const struct got_error *err = NULL;
7767 struct got_object_id *commit_id = NULL;
7771 err = resolve_reflist_entry(&commit_id, re, repo);
7773 if (err->code != GOT_ERR_OBJ_TYPE)
7779 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7780 if (log_view == NULL) {
7781 err = got_error_from_errno("view_open");
7785 err = open_log_view(log_view, commit_id, repo,
7786 got_ref_get_name(re->ref), "", 0);
7789 view_close(log_view);
7791 *new_view = log_view;
7797 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7799 struct tog_reflist_entry *re;
7802 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7805 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7806 while (i++ < maxscroll) {
7809 s->first_displayed_entry = re;
7810 re = TAILQ_PREV(re, tog_reflist_head, entry);
7814 static const struct got_error *
7815 ref_scroll_down(struct tog_view *view, int maxscroll)
7817 struct tog_ref_view_state *s = &view->state.ref;
7818 struct tog_reflist_entry *next, *last;
7821 if (s->first_displayed_entry)
7822 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7824 next = TAILQ_FIRST(&s->refs);
7826 last = s->last_displayed_entry;
7827 while (next && n++ < maxscroll) {
7829 s->last_displayed_entry = last;
7830 last = TAILQ_NEXT(last, entry);
7832 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7833 s->first_displayed_entry = next;
7834 next = TAILQ_NEXT(next, entry);
7841 static const struct got_error *
7842 search_start_ref_view(struct tog_view *view)
7844 struct tog_ref_view_state *s = &view->state.ref;
7846 s->matched_entry = NULL;
7851 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7853 regmatch_t regmatch;
7855 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7859 static const struct got_error *
7860 search_next_ref_view(struct tog_view *view)
7862 struct tog_ref_view_state *s = &view->state.ref;
7863 struct tog_reflist_entry *re = NULL;
7865 if (!view->searching) {
7866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7870 if (s->matched_entry) {
7871 if (view->searching == TOG_SEARCH_FORWARD) {
7872 if (s->selected_entry)
7873 re = TAILQ_NEXT(s->selected_entry, entry);
7875 re = TAILQ_PREV(s->selected_entry,
7876 tog_reflist_head, entry);
7878 if (s->selected_entry == NULL)
7879 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7881 re = TAILQ_PREV(s->selected_entry,
7882 tog_reflist_head, entry);
7885 if (s->selected_entry)
7886 re = s->selected_entry;
7887 else if (view->searching == TOG_SEARCH_FORWARD)
7888 re = TAILQ_FIRST(&s->refs);
7890 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7895 if (s->matched_entry == NULL) {
7896 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7899 if (view->searching == TOG_SEARCH_FORWARD)
7900 re = TAILQ_FIRST(&s->refs);
7902 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7905 if (match_reflist_entry(re, &view->regex)) {
7906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7907 s->matched_entry = re;
7911 if (view->searching == TOG_SEARCH_FORWARD)
7912 re = TAILQ_NEXT(re, entry);
7914 re = TAILQ_PREV(re, tog_reflist_head, entry);
7917 if (s->matched_entry) {
7918 s->first_displayed_entry = s->matched_entry;
7925 static const struct got_error *
7926 show_ref_view(struct tog_view *view)
7928 const struct got_error *err = NULL;
7929 struct tog_ref_view_state *s = &view->state.ref;
7930 struct tog_reflist_entry *re;
7933 struct tog_color *tc;
7935 int limit = view->nlines;
7937 werase(view->window);
7940 if (view_is_hsplit_top(view))
7941 --limit; /* border */
7946 re = s->first_displayed_entry;
7948 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7950 return got_error_from_errno("asprintf");
7952 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7957 if (view_needs_focus_indication(view))
7958 wstandout(view->window);
7959 waddwstr(view->window, wline);
7960 while (width++ < view->ncols)
7961 waddch(view->window, ' ');
7962 if (view_needs_focus_indication(view))
7963 wstandend(view->window);
7972 while (re && limit > 0) {
7974 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7977 struct got_commit_object *ci;
7978 struct got_tag_object *tag;
7979 struct got_object_id *id;
7983 err = got_ref_resolve(&id, s->repo, re->ref);
7986 err = got_object_open_as_tag(&tag, s->repo, id);
7988 if (err->code != GOT_ERR_OBJ_TYPE) {
7992 err = got_object_open_as_commit(&ci, s->repo,
7998 t = got_object_commit_get_committer_time(ci);
7999 got_object_commit_close(ci);
8001 t = got_object_tag_get_tagger_time(tag);
8002 got_object_tag_close(tag);
8005 if (gmtime_r(&t, &tm) == NULL)
8006 return got_error_from_errno("gmtime_r");
8007 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8008 return got_error(GOT_ERR_NO_SPACE);
8010 if (got_ref_is_symbolic(re->ref)) {
8011 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8012 ymd : "", got_ref_get_name(re->ref),
8013 got_ref_get_symref_target(re->ref)) == -1)
8014 return got_error_from_errno("asprintf");
8015 } else if (s->show_ids) {
8016 struct got_object_id *id;
8018 err = got_ref_resolve(&id, s->repo, re->ref);
8021 err = got_object_id_str(&id_str, id);
8026 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8027 got_ref_get_name(re->ref), id_str) == -1) {
8028 err = got_error_from_errno("asprintf");
8035 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8036 got_ref_get_name(re->ref)) == -1)
8037 return got_error_from_errno("asprintf");
8039 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8045 if (n == s->selected) {
8047 wstandout(view->window);
8048 s->selected_entry = re;
8050 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8052 wattr_on(view->window,
8053 COLOR_PAIR(tc->colorpair), NULL);
8054 waddwstr(view->window, wline);
8056 wattr_off(view->window,
8057 COLOR_PAIR(tc->colorpair), NULL);
8058 if (width < view->ncols - 1)
8059 waddch(view->window, '\n');
8060 if (n == s->selected && view->focussed)
8061 wstandend(view->window);
8067 s->last_displayed_entry = re;
8070 re = TAILQ_NEXT(re, entry);
8077 static const struct got_error *
8078 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8079 struct tog_reflist_entry *re, struct got_repository *repo)
8081 const struct got_error *err = NULL;
8082 struct got_object_id *commit_id = NULL;
8083 struct tog_view *tree_view;
8087 err = resolve_reflist_entry(&commit_id, re, repo);
8089 if (err->code != GOT_ERR_OBJ_TYPE)
8096 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8097 if (tree_view == NULL) {
8098 err = got_error_from_errno("view_open");
8102 err = open_tree_view(tree_view, commit_id,
8103 got_ref_get_name(re->ref), repo);
8107 *new_view = tree_view;
8113 static const struct got_error *
8114 ref_goto_line(struct tog_view *view, int nlines)
8116 const struct got_error *err = NULL;
8117 struct tog_ref_view_state *s = &view->state.ref;
8118 int g, idx = s->selected_entry->idx;
8125 else if (g > s->nrefs)
8128 if (g >= s->first_displayed_entry->idx + 1 &&
8129 g <= s->last_displayed_entry->idx + 1 &&
8130 g - s->first_displayed_entry->idx - 1 < nlines) {
8131 s->selected = g - s->first_displayed_entry->idx - 1;
8136 err = ref_scroll_down(view, g - idx - 1);
8139 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8140 s->first_displayed_entry->idx + s->selected < g &&
8141 s->selected < s->ndisplayed - 1)
8142 s->selected = g - s->first_displayed_entry->idx - 1;
8143 } else if (idx + 1 > g)
8144 ref_scroll_up(s, idx - g + 1);
8146 if (g < nlines && s->first_displayed_entry->idx == 0)
8147 s->selected = g - 1;
8153 static const struct got_error *
8154 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8156 const struct got_error *err = NULL;
8157 struct tog_ref_view_state *s = &view->state.ref;
8158 struct tog_reflist_entry *re;
8159 int n, nscroll = view->nlines - 1;
8162 return ref_goto_line(view, nscroll);
8166 s->show_ids = !s->show_ids;
8170 s->show_date = !s->show_date;
8174 s->sort_by_date = !s->sort_by_date;
8176 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8177 got_ref_cmp_by_commit_timestamp_descending :
8178 tog_ref_cmp_by_name, s->repo);
8181 got_reflist_object_id_map_free(tog_refs_idmap);
8182 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8183 &tog_refs, s->repo);
8186 ref_view_free_refs(s);
8187 err = ref_view_load_refs(s);
8192 if (!s->selected_entry)
8194 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8198 if (!s->selected_entry)
8200 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8206 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8210 int eos = view->nlines - 1;
8212 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8216 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8217 for (n = 0; n < eos; n++) {
8220 s->first_displayed_entry = re;
8221 re = TAILQ_PREV(re, tog_reflist_head, entry);
8224 s->selected = n - 1;
8230 if (s->selected > 0) {
8234 ref_scroll_up(s, 1);
8235 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8245 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8246 s->selected -= MIN(nscroll, s->selected);
8247 ref_scroll_up(s, MAX(0, nscroll));
8248 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8254 if (s->selected < s->ndisplayed - 1) {
8258 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8259 /* can't scroll any further */
8263 ref_scroll_down(view, 1);
8273 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8274 /* can't scroll any further; move cursor down */
8275 if (s->selected < s->ndisplayed - 1)
8276 s->selected += MIN(nscroll,
8277 s->ndisplayed - s->selected - 1);
8278 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8279 s->selected += s->ndisplayed - s->selected - 1;
8283 ref_scroll_down(view, nscroll);
8288 err = tog_load_refs(s->repo, s->sort_by_date);
8291 ref_view_free_refs(s);
8292 err = ref_view_load_refs(s);
8295 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8296 s->selected = view->nlines - 2;
8310 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8315 static const struct got_error *
8316 cmd_ref(int argc, char *argv[])
8318 const struct got_error *error;
8319 struct got_repository *repo = NULL;
8320 struct got_worktree *worktree = NULL;
8321 char *cwd = NULL, *repo_path = NULL;
8323 struct tog_view *view;
8324 int *pack_fds = NULL;
8326 while ((ch = getopt(argc, argv, "r:")) != -1) {
8329 repo_path = realpath(optarg, NULL);
8330 if (repo_path == NULL)
8331 return got_error_from_errno2("realpath",
8346 error = got_repo_pack_fds_open(&pack_fds);
8350 if (repo_path == NULL) {
8351 cwd = getcwd(NULL, 0);
8353 return got_error_from_errno("getcwd");
8354 error = got_worktree_open(&worktree, cwd);
8355 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8359 strdup(got_worktree_get_repo_path(worktree));
8361 repo_path = strdup(cwd);
8362 if (repo_path == NULL) {
8363 error = got_error_from_errno("strdup");
8368 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8374 error = apply_unveil(got_repo_get_path(repo), NULL);
8378 error = tog_load_refs(repo, 0);
8382 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8384 error = got_error_from_errno("view_open");
8388 error = open_ref_view(view, repo);
8393 /* Release work tree lock. */
8394 got_worktree_close(worktree);
8397 error = view_loop(view);
8402 const struct got_error *close_err = got_repo_close(repo);
8407 const struct got_error *pack_err =
8408 got_repo_pack_fds_close(pack_fds);
8416 static const struct got_error*
8417 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8426 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8430 if (mvwprintw(win, y, x, "%s", str) == ERR)
8431 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8438 static const struct got_error *
8439 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8443 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8445 free(*line_offsets);
8446 *line_offsets = NULL;
8447 return got_error_from_errno("reallocarray");
8451 (*line_offsets)[*nlines] = off;
8456 static const struct got_error *
8457 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8461 for (;n > 0; --n, ++km) {
8465 if (km->keys == NULL)
8468 t = t0 = strdup(km->keys);
8470 return got_error_from_errno("strdup");
8473 while ((k = strsep(&t, " ")) != NULL)
8474 len += strlen(k) > 1 ? 2 : 0;
8476 *ret = MAX(*ret, len);
8483 * Write keymap section headers, keys, and key info in km to f.
8484 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8485 * wrap control and symbolic keys in guillemets, else use <>.
8487 static const struct got_error *
8488 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8493 static const char *u8_glyph[] = {
8494 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8495 "\xe2\x80\xba" /* U+203A (utf8 >) */
8498 int cs, s, first = 1;
8500 cs = got_locale_is_utf8();
8502 t = t0 = strdup(km->keys);
8504 return got_error_from_errno("strdup");
8506 len = strlen(km->keys);
8507 while ((k = strsep(&t, " ")) != NULL) {
8508 s = strlen(k) > 1; /* control or symbolic key */
8509 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8510 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8511 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8514 return got_error_from_errno("fprintf");
8522 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8524 return got_error_from_errno("fprintf");
8530 static const struct got_error *
8531 format_help(struct tog_help_view_state *s)
8533 const struct got_error *err = NULL;
8535 int i, max, n, show = s->all;
8536 static const struct tog_key_map km[] = {
8537 #define KEYMAP_(info, type) { NULL, (info), type }
8538 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8544 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8549 err = max_key_str(&max, km, n);
8553 for (i = 0; i < n; ++i) {
8554 if (km[i].keys == NULL) {
8556 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8557 km[i].type == s->type || s->all)
8561 err = format_help_line(&off, s->f, &km[i], max);
8564 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8571 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8575 static const struct got_error *
8576 create_help(struct tog_help_view_state *s)
8579 const struct got_error *err;
8581 free(s->line_offsets);
8582 s->line_offsets = NULL;
8587 return got_error_from_errno("got_opentemp");
8590 err = format_help(s);
8594 if (s->f && fflush(s->f) != 0)
8595 return got_error_from_errno("fflush");
8600 static const struct got_error *
8601 search_start_help_view(struct tog_view *view)
8603 view->state.help.matched_line = 0;
8608 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8609 size_t *nlines, int **first, int **last, int **match, int **selected)
8611 struct tog_help_view_state *s = &view->state.help;
8614 *nlines = s->nlines;
8615 *line_offsets = s->line_offsets;
8616 *match = &s->matched_line;
8617 *first = &s->first_displayed_line;
8618 *last = &s->last_displayed_line;
8619 *selected = &s->selected_line;
8622 static const struct got_error *
8623 show_help_view(struct tog_view *view)
8625 struct tog_help_view_state *s = &view->state.help;
8626 const struct got_error *err;
8627 regmatch_t *regmatch = &view->regmatch;
8632 int width, nprinted = 0, rc = 0;
8633 int eos = view->nlines;
8635 if (view_is_hsplit_top(view))
8636 --eos; /* account for border */
8640 werase(view->window);
8642 if (view->gline > s->nlines - 1)
8643 view->gline = s->nlines - 1;
8645 err = win_draw_center(view->window, 0, 0, view->ncols,
8646 view_needs_focus_indication(view),
8647 "tog help (press q to return to tog)");
8652 waddstr(view->window, "\n\n");
8658 while (eos > 0 && nprinted < eos) {
8661 linelen = getline(&line, &linesz, s->f);
8662 if (linelen == -1) {
8665 return got_ferror(s->f, GOT_ERR_IO);
8670 if (++s->lineno < s->first_displayed_line)
8672 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8674 if (s->lineno == view->hiline)
8677 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8683 view->maxx = MAX(view->maxx, width);
8688 wattron(view->window, attr);
8689 if (s->first_displayed_line + nprinted == s->matched_line &&
8690 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8691 err = add_matched_line(&width, line, view->ncols - 1, 0,
8692 view->window, view->x, regmatch);
8700 err = format_line(&wline, &width, &skip, line,
8701 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8706 rc = waddwstr(view->window, &wline[skip]);
8710 return got_error_msg(GOT_ERR_IO, "waddwstr");
8712 if (s->lineno == view->hiline) {
8713 while (width++ < view->ncols)
8714 waddch(view->window, ' ');
8716 if (width <= view->ncols)
8717 waddch(view->window, '\n');
8720 wattroff(view->window, attr);
8721 if (++nprinted == 1)
8722 s->first_displayed_line = s->lineno;
8726 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8728 s->last_displayed_line = s->first_displayed_line;
8733 rc = waddnstr(view->window,
8734 "See the tog(1) manual page for full documentation",
8737 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8739 wmove(view->window, view->nlines - 1, 0);
8740 wclrtoeol(view->window);
8741 wstandout(view->window);
8742 rc = waddnstr(view->window, "scroll down for more...",
8745 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8746 if (getcurx(view->window) < view->ncols - 6) {
8747 rc = wprintw(view->window, "[%.0f%%]",
8748 100.00 * s->last_displayed_line / s->nlines);
8750 return got_error_msg(GOT_ERR_IO, "wprintw");
8752 wstandend(view->window);
8758 static const struct got_error *
8759 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8761 struct tog_help_view_state *s = &view->state.help;
8762 const struct got_error *err = NULL;
8768 eos = nscroll = view->nlines;
8769 if (view_is_hsplit_top(view))
8772 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8779 view->x = MAX(view->maxx - view->ncols / 3, 0);
8784 if (view->x + view->ncols / 3 < view->maxx)
8791 view->x -= MIN(view->x, 2);
8797 s->first_displayed_line = 1;
8805 s->first_displayed_line = (s->nlines - eos) + 3;
8810 if (s->first_displayed_line > 1)
8811 --s->first_displayed_line;
8822 if (s->first_displayed_line == 1) {
8826 while (--nscroll > 0 && s->first_displayed_line > 1)
8827 s->first_displayed_line--;
8833 ++s->first_displayed_line;
8849 while (!s->eof && --nscroll > 0) {
8850 linelen = getline(&line, &linesz, s->f);
8851 s->first_displayed_line++;
8852 if (linelen == -1) {
8856 err = got_ferror(s->f, GOT_ERR_IO);
8870 static const struct got_error *
8871 close_help_view(struct tog_view *view)
8873 struct tog_help_view_state *s = &view->state.help;
8875 free(s->line_offsets);
8876 s->line_offsets = NULL;
8877 if (fclose(s->f) == EOF)
8878 return got_error_from_errno("fclose");
8883 static const struct got_error *
8884 reset_help_view(struct tog_view *view)
8886 struct tog_help_view_state *s = &view->state.help;
8889 if (s->f && fclose(s->f) == EOF)
8890 return got_error_from_errno("fclose");
8892 wclear(view->window);
8896 s->first_displayed_line = 1;
8897 s->last_displayed_line = view->nlines;
8898 s->matched_line = 0;
8900 return create_help(s);
8903 static const struct got_error *
8904 open_help_view(struct tog_view *view, struct tog_view *parent)
8906 const struct got_error *err = NULL;
8907 struct tog_help_view_state *s = &view->state.help;
8909 s->type = (enum tog_keymap_type)parent->type;
8910 s->first_displayed_line = 1;
8911 s->last_displayed_line = view->nlines;
8912 s->selected_line = 1;
8914 view->show = show_help_view;
8915 view->input = input_help_view;
8916 view->reset = reset_help_view;
8917 view->close = close_help_view;
8918 view->search_start = search_start_help_view;
8919 view->search_setup = search_setup_help_view;
8920 view->search_next = search_next_view_match;
8922 err = create_help(s);
8926 static const struct got_error *
8927 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8928 enum tog_view_type request, int y, int x)
8930 const struct got_error *err = NULL;
8936 if (view->type == TOG_VIEW_LOG) {
8937 struct tog_log_view_state *s = &view->state.log;
8939 err = open_diff_view_for_commit(new_view, y, x,
8940 s->selected_entry->commit, s->selected_entry->id,
8943 return got_error_msg(GOT_ERR_NOT_IMPL,
8944 "parent/child view pair not supported");
8946 case TOG_VIEW_BLAME:
8947 if (view->type == TOG_VIEW_TREE) {
8948 struct tog_tree_view_state *s = &view->state.tree;
8950 err = blame_tree_entry(new_view, y, x,
8951 s->selected_entry, &s->parents, s->commit_id,
8954 return got_error_msg(GOT_ERR_NOT_IMPL,
8955 "parent/child view pair not supported");
8958 if (view->type == TOG_VIEW_BLAME)
8959 err = log_annotated_line(new_view, y, x,
8960 view->state.blame.repo, view->state.blame.id_to_log);
8961 else if (view->type == TOG_VIEW_TREE)
8962 err = log_selected_tree_entry(new_view, y, x,
8964 else if (view->type == TOG_VIEW_REF)
8965 err = log_ref_entry(new_view, y, x,
8966 view->state.ref.selected_entry,
8967 view->state.ref.repo);
8969 return got_error_msg(GOT_ERR_NOT_IMPL,
8970 "parent/child view pair not supported");
8973 if (view->type == TOG_VIEW_LOG)
8974 err = browse_commit_tree(new_view, y, x,
8975 view->state.log.selected_entry,
8976 view->state.log.in_repo_path,
8977 view->state.log.head_ref_name,
8978 view->state.log.repo);
8979 else if (view->type == TOG_VIEW_REF)
8980 err = browse_ref_tree(new_view, y, x,
8981 view->state.ref.selected_entry,
8982 view->state.ref.repo);
8984 return got_error_msg(GOT_ERR_NOT_IMPL,
8985 "parent/child view pair not supported");
8988 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8989 if (*new_view == NULL)
8990 return got_error_from_errno("view_open");
8991 if (view->type == TOG_VIEW_LOG)
8992 err = open_ref_view(*new_view, view->state.log.repo);
8993 else if (view->type == TOG_VIEW_TREE)
8994 err = open_ref_view(*new_view, view->state.tree.repo);
8996 err = got_error_msg(GOT_ERR_NOT_IMPL,
8997 "parent/child view pair not supported");
8999 view_close(*new_view);
9002 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9003 if (*new_view == NULL)
9004 return got_error_from_errno("view_open");
9005 err = open_help_view(*new_view, view);
9007 view_close(*new_view);
9010 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9017 * If view was scrolled down to move the selected line into view when opening a
9018 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9021 offset_selection_up(struct tog_view *view)
9023 switch (view->type) {
9024 case TOG_VIEW_BLAME: {
9025 struct tog_blame_view_state *s = &view->state.blame;
9026 if (s->first_displayed_line == 1) {
9027 s->selected_line = MAX(s->selected_line - view->offset,
9031 if (s->first_displayed_line > view->offset)
9032 s->first_displayed_line -= view->offset;
9034 s->first_displayed_line = 1;
9035 s->selected_line += view->offset;
9039 log_scroll_up(&view->state.log, view->offset);
9040 view->state.log.selected += view->offset;
9043 ref_scroll_up(&view->state.ref, view->offset);
9044 view->state.ref.selected += view->offset;
9047 tree_scroll_up(&view->state.tree, view->offset);
9048 view->state.tree.selected += view->offset;
9058 * If the selected line is in the section of screen covered by the bottom split,
9059 * scroll down offset lines to move it into view and index its new position.
9061 static const struct got_error *
9062 offset_selection_down(struct tog_view *view)
9064 const struct got_error *err = NULL;
9065 const struct got_error *(*scrolld)(struct tog_view *, int);
9066 int *selected = NULL;
9069 switch (view->type) {
9070 case TOG_VIEW_BLAME: {
9071 struct tog_blame_view_state *s = &view->state.blame;
9074 if (s->selected_line > view->nlines - header) {
9075 offset = abs(view->nlines - s->selected_line - header);
9076 s->first_displayed_line += offset;
9077 s->selected_line -= offset;
9078 view->offset = offset;
9082 case TOG_VIEW_LOG: {
9083 struct tog_log_view_state *s = &view->state.log;
9084 scrolld = &log_scroll_down;
9085 header = view_is_parent_view(view) ? 3 : 2;
9086 selected = &s->selected;
9089 case TOG_VIEW_REF: {
9090 struct tog_ref_view_state *s = &view->state.ref;
9091 scrolld = &ref_scroll_down;
9093 selected = &s->selected;
9096 case TOG_VIEW_TREE: {
9097 struct tog_tree_view_state *s = &view->state.tree;
9098 scrolld = &tree_scroll_down;
9100 selected = &s->selected;
9110 if (selected && *selected > view->nlines - header) {
9111 offset = abs(view->nlines - *selected - header);
9112 view->offset = offset;
9113 if (scrolld && offset) {
9114 err = scrolld(view, offset);
9115 *selected -= offset;
9123 list_commands(FILE *fp)
9127 fprintf(fp, "commands:");
9128 for (i = 0; i < nitems(tog_commands); i++) {
9129 const struct tog_cmd *cmd = &tog_commands[i];
9130 fprintf(fp, " %s", cmd->name);
9136 usage(int hflag, int status)
9138 FILE *fp = (status == 0) ? stdout : stderr;
9140 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9143 fprintf(fp, "lazy usage: %s path\n", getprogname());
9150 make_argv(int argc, ...)
9158 argv = calloc(argc, sizeof(char *));
9161 for (i = 0; i < argc; i++) {
9162 argv[i] = strdup(va_arg(ap, char *));
9163 if (argv[i] == NULL)
9172 * Try to convert 'tog path' into a 'tog log path' command.
9173 * The user could simply have mistyped the command rather than knowingly
9174 * provided a path. So check whether argv[0] can in fact be resolved
9175 * to a path in the HEAD commit and print a special error if not.
9176 * This hack is for mpi@ <3
9178 static const struct got_error *
9179 tog_log_with_path(int argc, char *argv[])
9181 const struct got_error *error = NULL, *close_err;
9182 const struct tog_cmd *cmd = NULL;
9183 struct got_repository *repo = NULL;
9184 struct got_worktree *worktree = NULL;
9185 struct got_object_id *commit_id = NULL, *id = NULL;
9186 struct got_commit_object *commit = NULL;
9187 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9188 char *commit_id_str = NULL, **cmd_argv = NULL;
9189 int *pack_fds = NULL;
9191 cwd = getcwd(NULL, 0);
9193 return got_error_from_errno("getcwd");
9195 error = got_repo_pack_fds_open(&pack_fds);
9199 error = got_worktree_open(&worktree, cwd);
9200 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9204 repo_path = strdup(got_worktree_get_repo_path(worktree));
9206 repo_path = strdup(cwd);
9207 if (repo_path == NULL) {
9208 error = got_error_from_errno("strdup");
9212 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9216 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9221 error = tog_load_refs(repo, 0);
9224 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9225 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9226 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9231 got_worktree_close(worktree);
9235 error = got_object_open_as_commit(&commit, repo, commit_id);
9239 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9241 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9243 fprintf(stderr, "%s: '%s' is no known command or path\n",
9244 getprogname(), argv[0]);
9249 error = got_object_id_str(&commit_id_str, commit_id);
9253 cmd = &tog_commands[0]; /* log */
9255 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9256 error = cmd->cmd_main(argc, cmd_argv);
9259 close_err = got_repo_close(repo);
9264 got_object_commit_close(commit);
9266 got_worktree_close(worktree);
9268 const struct got_error *pack_err =
9269 got_repo_pack_fds_close(pack_fds);
9274 free(commit_id_str);
9281 for (i = 0; i < argc; i++)
9290 main(int argc, char *argv[])
9292 const struct got_error *error = NULL;
9293 const struct tog_cmd *cmd = NULL;
9294 int ch, hflag = 0, Vflag = 0;
9295 char **cmd_argv = NULL;
9296 static const struct option longopts[] = {
9297 { "version", no_argument, NULL, 'V' },
9300 char *diff_algo_str = NULL;
9302 if (!isatty(STDIN_FILENO))
9303 errx(1, "standard input is not a tty");
9305 setlocale(LC_CTYPE, "");
9307 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9327 got_version_print_str();
9332 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9340 /* Build an argument vector which runs a default command. */
9341 cmd = &tog_commands[0];
9343 cmd_argv = make_argv(argc, cmd->name);
9347 /* Did the user specify a command? */
9348 for (i = 0; i < nitems(tog_commands); i++) {
9349 if (strncmp(tog_commands[i].name, argv[0],
9350 strlen(argv[0])) == 0) {
9351 cmd = &tog_commands[i];
9357 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9358 if (diff_algo_str) {
9359 if (strcasecmp(diff_algo_str, "patience") == 0)
9360 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9361 if (strcasecmp(diff_algo_str, "myers") == 0)
9362 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9368 /* No command specified; try log with a path */
9369 error = tog_log_with_path(argc, argv);
9374 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9381 for (i = 0; i < argc; i++)
9386 if (error && error->code != GOT_ERR_CANCELLED &&
9387 error->code != GOT_ERR_EOF &&
9388 error->code != GOT_ERR_PRIVSEP_EXIT &&
9389 error->code != GOT_ERR_PRIVSEP_PIPE &&
9390 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9391 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);