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 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
127 struct commit_queue_head head;
131 STAILQ_ENTRY(tog_color) entry;
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
155 } else if (isbackup1 && !isbackup2) {
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 STAILQ_INSERT_HEAD(colors, tc, entry);
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
296 if (strcasecmp(val, "red") == 0)
298 if (strcasecmp(val, "green") == 0)
300 if (strcasecmp(val, "yellow") == 0)
302 if (strcasecmp(val, "blue") == 0)
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
308 if (strcasecmp(val, "white") == 0)
310 if (strcasecmp(val, "default") == 0)
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
322 int first_displayed_line;
323 int last_displayed_line;
326 int ignore_whitespace;
328 struct got_repository *repo;
329 struct tog_colors colors;
335 /* passed from log or blame view; may be NULL */
336 struct tog_view *parent_view;
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 static volatile sig_atomic_t tog_thread_error;
342 struct tog_log_thread_args {
343 pthread_cond_t need_commits;
344 pthread_cond_t commit_loaded;
347 struct got_commit_graph *graph;
348 struct commit_queue *commits;
349 const char *in_repo_path;
350 struct got_object_id *start_id;
351 struct got_repository *repo;
355 struct commit_queue_entry **first_displayed_entry;
356 struct commit_queue_entry **selected_entry;
358 int *search_next_done;
362 struct tog_log_view_state {
363 struct commit_queue commits;
364 struct commit_queue_entry *first_displayed_entry;
365 struct commit_queue_entry *last_displayed_entry;
366 struct commit_queue_entry *selected_entry;
371 struct got_repository *repo;
372 struct got_object_id *start_id;
375 struct tog_log_thread_args thread_args;
376 struct commit_queue_entry *matched_entry;
377 struct commit_queue_entry *search_entry;
378 struct tog_colors colors;
382 #define TOG_COLOR_DIFF_MINUS 1
383 #define TOG_COLOR_DIFF_PLUS 2
384 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
385 #define TOG_COLOR_DIFF_META 4
386 #define TOG_COLOR_TREE_SUBMODULE 5
387 #define TOG_COLOR_TREE_SYMLINK 6
388 #define TOG_COLOR_TREE_DIRECTORY 7
389 #define TOG_COLOR_TREE_EXECUTABLE 8
390 #define TOG_COLOR_COMMIT 9
391 #define TOG_COLOR_AUTHOR 10
392 #define TOG_COLOR_DATE 11
393 #define TOG_COLOR_REFS_HEADS 12
394 #define TOG_COLOR_REFS_TAGS 13
395 #define TOG_COLOR_REFS_REMOTES 14
396 #define TOG_COLOR_REFS_BACKUP 15
398 struct tog_blame_cb_args {
399 struct tog_blame_line *lines; /* one per line */
402 struct tog_view *view;
403 struct got_object_id *commit_id;
407 struct tog_blame_thread_args {
409 struct got_repository *repo;
410 struct tog_blame_cb_args *cb_args;
412 got_cancel_cb cancel_cb;
419 struct tog_blame_line *lines;
423 struct tog_blame_thread_args thread_args;
424 struct tog_blame_cb_args cb_args;
429 struct tog_blame_view_state {
430 int first_displayed_line;
431 int last_displayed_line;
433 int last_diffed_line;
437 struct got_object_id_queue blamed_commits;
438 struct got_object_qid *blamed_commit;
440 struct got_repository *repo;
441 struct got_object_id *commit_id;
442 struct tog_blame blame;
444 struct tog_colors colors;
447 struct tog_parent_tree {
448 TAILQ_ENTRY(tog_parent_tree) entry;
449 struct got_tree_object *tree;
450 struct got_tree_entry *first_displayed_entry;
451 struct got_tree_entry *selected_entry;
455 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 struct tog_tree_view_state {
459 struct got_object_id *commit_id;/* commit which this tree belongs to */
460 struct got_tree_object *root; /* the commit's root tree entry */
461 struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 struct got_tree_entry *first_displayed_entry;
463 struct got_tree_entry *last_displayed_entry;
464 struct got_tree_entry *selected_entry;
465 int ndisplayed, selected, show_ids;
466 struct tog_parent_trees parents; /* parent trees of current sub-tree */
468 struct got_repository *repo;
469 struct got_tree_entry *matched_entry;
470 struct tog_colors colors;
473 struct tog_reflist_entry {
474 TAILQ_ENTRY(tog_reflist_entry) entry;
475 struct got_reference *ref;
479 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 struct tog_ref_view_state {
482 struct tog_reflist_head refs;
483 struct tog_reflist_entry *first_displayed_entry;
484 struct tog_reflist_entry *last_displayed_entry;
485 struct tog_reflist_entry *selected_entry;
486 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 struct got_repository *repo;
488 struct tog_reflist_entry *matched_entry;
489 struct tog_colors colors;
493 * We implement two types of views: parent views and child views.
495 * The 'Tab' key switches focus between a parent view and its child view.
496 * Child views are shown side-by-side to their parent view, provided
497 * there is enough screen estate.
499 * When a new view is opened from within a parent view, this new view
500 * becomes a child view of the parent view, replacing any existing child.
502 * When a new view is opened from within a child view, this new view
503 * becomes a parent view which will obscure the views below until the
504 * user quits the new parent view by typing 'q'.
506 * This list of views contains parent views only.
507 * Child views are only pointed to by their parent view.
509 TAILQ_HEAD(tog_view_list_head, tog_view);
512 TAILQ_ENTRY(tog_view) entry;
515 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 int resized_y, resized_x; /* begin_y/x based on user resizing */
517 int maxx, x; /* max column and current start column */
518 int lines, cols; /* copies of LINES and COLS */
519 int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 int ch, count; /* current keymap and count prefix */
521 int resized; /* set when in a resize event */
522 int focussed; /* Only set on one parent or child view at a time. */
524 struct tog_view *parent;
525 struct tog_view *child;
528 * This flag is initially set on parent views when a new child view
529 * is created. It gets toggled when the 'Tab' key switches focus
530 * between parent and child.
531 * The flag indicates whether focus should be passed on to our child
532 * view if this parent view gets picked for focus after another parent
533 * view was closed. This prevents child views from losing focus in such
538 enum tog_view_mode mode;
539 /* type-specific state */
540 enum tog_view_type type;
542 struct tog_diff_view_state diff;
543 struct tog_log_view_state log;
544 struct tog_blame_view_state blame;
545 struct tog_tree_view_state tree;
546 struct tog_ref_view_state ref;
549 const struct got_error *(*show)(struct tog_view *);
550 const struct got_error *(*input)(struct tog_view **,
551 struct tog_view *, int);
552 const struct got_error *(*reset)(struct tog_view *);
553 const struct got_error *(*resize)(struct tog_view *, int);
554 const struct got_error *(*close)(struct tog_view *);
556 const struct got_error *(*search_start)(struct tog_view *);
557 const struct got_error *(*search_next)(struct tog_view *);
560 #define TOG_SEARCH_FORWARD 1
561 #define TOG_SEARCH_BACKWARD 2
562 int search_next_done;
563 #define TOG_SEARCH_HAVE_MORE 1
564 #define TOG_SEARCH_NO_MORE 2
565 #define TOG_SEARCH_HAVE_NONE 3
570 static const struct got_error *open_diff_view(struct tog_view *,
571 struct got_object_id *, struct got_object_id *,
572 const char *, const char *, int, int, int, struct tog_view *,
573 struct got_repository *);
574 static const struct got_error *show_diff_view(struct tog_view *);
575 static const struct got_error *input_diff_view(struct tog_view **,
576 struct tog_view *, int);
577 static const struct got_error *reset_diff_view(struct tog_view *);
578 static const struct got_error* close_diff_view(struct tog_view *);
579 static const struct got_error *search_start_diff_view(struct tog_view *);
580 static const struct got_error *search_next_diff_view(struct tog_view *);
582 static const struct got_error *open_log_view(struct tog_view *,
583 struct got_object_id *, struct got_repository *,
584 const char *, const char *, int);
585 static const struct got_error * show_log_view(struct tog_view *);
586 static const struct got_error *input_log_view(struct tog_view **,
587 struct tog_view *, int);
588 static const struct got_error *resize_log_view(struct tog_view *, int);
589 static const struct got_error *close_log_view(struct tog_view *);
590 static const struct got_error *search_start_log_view(struct tog_view *);
591 static const struct got_error *search_next_log_view(struct tog_view *);
593 static const struct got_error *open_blame_view(struct tog_view *, char *,
594 struct got_object_id *, struct got_repository *);
595 static const struct got_error *show_blame_view(struct tog_view *);
596 static const struct got_error *input_blame_view(struct tog_view **,
597 struct tog_view *, int);
598 static const struct got_error *reset_blame_view(struct tog_view *);
599 static const struct got_error *close_blame_view(struct tog_view *);
600 static const struct got_error *search_start_blame_view(struct tog_view *);
601 static const struct got_error *search_next_blame_view(struct tog_view *);
603 static const struct got_error *open_tree_view(struct tog_view *,
604 struct got_object_id *, const char *, struct got_repository *);
605 static const struct got_error *show_tree_view(struct tog_view *);
606 static const struct got_error *input_tree_view(struct tog_view **,
607 struct tog_view *, int);
608 static const struct got_error *close_tree_view(struct tog_view *);
609 static const struct got_error *search_start_tree_view(struct tog_view *);
610 static const struct got_error *search_next_tree_view(struct tog_view *);
612 static const struct got_error *open_ref_view(struct tog_view *,
613 struct got_repository *);
614 static const struct got_error *show_ref_view(struct tog_view *);
615 static const struct got_error *input_ref_view(struct tog_view **,
616 struct tog_view *, int);
617 static const struct got_error *close_ref_view(struct tog_view *);
618 static const struct got_error *search_start_ref_view(struct tog_view *);
619 static const struct got_error *search_next_ref_view(struct tog_view *);
621 static volatile sig_atomic_t tog_sigwinch_received;
622 static volatile sig_atomic_t tog_sigpipe_received;
623 static volatile sig_atomic_t tog_sigcont_received;
624 static volatile sig_atomic_t tog_sigint_received;
625 static volatile sig_atomic_t tog_sigterm_received;
628 tog_sigwinch(int signo)
630 tog_sigwinch_received = 1;
634 tog_sigpipe(int signo)
636 tog_sigpipe_received = 1;
640 tog_sigcont(int signo)
642 tog_sigcont_received = 1;
646 tog_sigint(int signo)
648 tog_sigint_received = 1;
652 tog_sigterm(int signo)
654 tog_sigterm_received = 1;
658 tog_fatal_signal_received(void)
660 return (tog_sigpipe_received ||
661 tog_sigint_received || tog_sigint_received);
664 static const struct got_error *
665 view_close(struct tog_view *view)
667 const struct got_error *err = NULL, *child_err = NULL;
670 child_err = view_close(view->child);
674 err = view->close(view);
676 del_panel(view->panel);
678 delwin(view->window);
680 return err ? err : child_err;
683 static struct tog_view *
684 view_open(int nlines, int ncols, int begin_y, int begin_x,
685 enum tog_view_type type)
687 struct tog_view *view = calloc(1, sizeof(*view));
695 view->nlines = nlines ? nlines : LINES - begin_y;
696 view->ncols = ncols ? ncols : COLS - begin_x;
697 view->begin_y = begin_y;
698 view->begin_x = begin_x;
699 view->window = newwin(nlines, ncols, begin_y, begin_x);
700 if (view->window == NULL) {
704 view->panel = new_panel(view->window);
705 if (view->panel == NULL ||
706 set_panel_userptr(view->panel, view) != OK) {
711 keypad(view->window, TRUE);
716 view_split_begin_x(int begin_x)
718 if (begin_x > 0 || COLS < 120)
720 return (COLS - MAX(COLS / 2, 80));
723 /* XXX Stub till we decide what to do. */
725 view_split_begin_y(int lines)
727 return lines * HSPLIT_SCALE;
730 static const struct got_error *view_resize(struct tog_view *);
732 static const struct got_error *
733 view_splitscreen(struct tog_view *view)
735 const struct got_error *err = NULL;
737 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
738 if (view->resized_y && view->resized_y < view->lines)
739 view->begin_y = view->resized_y;
741 view->begin_y = view_split_begin_y(view->nlines);
743 } else if (!view->resized) {
744 if (view->resized_x && view->resized_x < view->cols - 1 &&
746 view->begin_x = view->resized_x;
748 view->begin_x = view_split_begin_x(0);
751 view->nlines = LINES - view->begin_y;
752 view->ncols = COLS - view->begin_x;
755 err = view_resize(view);
759 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
760 view->parent->nlines = view->begin_y;
762 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
763 return got_error_from_errno("mvwin");
768 static const struct got_error *
769 view_fullscreen(struct tog_view *view)
771 const struct got_error *err = NULL;
774 view->begin_y = view->resized ? view->begin_y : 0;
775 view->nlines = view->resized ? view->nlines : LINES;
779 err = view_resize(view);
783 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
784 return got_error_from_errno("mvwin");
790 view_is_parent_view(struct tog_view *view)
792 return view->parent == NULL;
796 view_is_splitscreen(struct tog_view *view)
798 return view->begin_x > 0 || view->begin_y > 0;
802 view_is_fullscreen(struct tog_view *view)
804 return view->nlines == LINES && view->ncols == COLS;
808 view_is_hsplit_top(struct tog_view *view)
810 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
811 view_is_splitscreen(view->child);
815 view_border(struct tog_view *view)
818 const struct tog_view *view_above;
821 return view_border(view->parent);
823 panel = panel_above(view->panel);
827 view_above = panel_userptr(panel);
828 if (view->mode == TOG_VIEW_SPLIT_HRZN)
829 mvwhline(view->window, view_above->begin_y - 1,
830 view->begin_x, got_locale_is_utf8() ?
831 ACS_HLINE : '-', view->ncols);
833 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
834 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
837 static const struct got_error *view_init_hsplit(struct tog_view *, int);
838 static const struct got_error *request_log_commits(struct tog_view *);
839 static const struct got_error *offset_selection_down(struct tog_view *);
840 static void offset_selection_up(struct tog_view *);
841 static void view_get_split(struct tog_view *, int *, int *);
843 static const struct got_error *
844 view_resize(struct tog_view *view)
846 const struct got_error *err = NULL;
847 int dif, nlines, ncols;
849 dif = LINES - view->lines; /* line difference */
851 if (view->lines > LINES)
852 nlines = view->nlines - (view->lines - LINES);
854 nlines = view->nlines + (LINES - view->lines);
855 if (view->cols > COLS)
856 ncols = view->ncols - (view->cols - COLS);
858 ncols = view->ncols + (COLS - view->cols);
861 int hs = view->child->begin_y;
863 if (!view_is_fullscreen(view))
864 view->child->begin_x = view_split_begin_x(view->begin_x);
865 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
866 view->child->begin_x == 0) {
869 view_fullscreen(view->child);
870 if (view->child->focussed)
871 show_panel(view->child->panel);
873 show_panel(view->panel);
875 ncols = view->child->begin_x;
877 view_splitscreen(view->child);
878 show_panel(view->child->panel);
881 * XXX This is ugly and needs to be moved into the above
882 * logic but "works" for now and my attempts at moving it
883 * break either 'tab' or 'F' key maps in horizontal splits.
886 err = view_splitscreen(view->child);
889 if (dif < 0) { /* top split decreased */
890 err = offset_selection_down(view);
897 show_panel(view->child->panel);
898 nlines = view->nlines;
900 } else if (view->parent == NULL)
903 if (view->resize && dif > 0) {
904 err = view->resize(view, dif);
909 if (wresize(view->window, nlines, ncols) == ERR)
910 return got_error_from_errno("wresize");
911 if (replace_panel(view->panel, view->window) == ERR)
912 return got_error_from_errno("replace_panel");
913 wclear(view->window);
915 view->nlines = nlines;
923 static const struct got_error *
924 resize_log_view(struct tog_view *view, int increase)
926 struct tog_log_view_state *s = &view->state.log;
927 const struct got_error *err = NULL;
928 int n = s->selected_entry->idx + view->lines - s->selected;
931 * Request commits to account for the increased
932 * height so we have enough to populate the view.
934 if (s->commits.ncommits < n) {
935 view->nscrolled = n - s->commits.ncommits + increase + 1;
936 err = request_log_commits(view);
943 view_adjust_offset(struct tog_view *view, int n)
948 if (view->parent && view->parent->offset) {
949 if (view->parent->offset + n >= 0)
950 view->parent->offset += n;
952 view->parent->offset = 0;
953 } else if (view->offset) {
954 if (view->offset - n >= 0)
961 static const struct got_error *
962 view_resize_split(struct tog_view *view, int resize)
964 const struct got_error *err = NULL;
965 struct tog_view *v = NULL;
972 if (!v->child || !view_is_splitscreen(v->child))
975 v->resized = v->child->resized = resize; /* lock for resize event */
977 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
978 int y = v->child->begin_y;
980 if (v->child->resized_y)
981 v->child->begin_y = v->child->resized_y;
983 v->child->begin_y -= resize;
985 v->child->begin_y += resize;
986 if (v->child->begin_y < 3) {
988 v->child->begin_y = 3;
989 } else if (v->child->begin_y > LINES - 1) {
991 v->child->begin_y = LINES - 1;
994 v->child->ncols = COLS;
995 view_adjust_offset(view, resize);
996 err = view_init_hsplit(v, v->child->begin_y);
999 v->child->resized_y = v->child->begin_y;
1000 if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1001 v->child->nscrolled = y - v->child->begin_y;
1002 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1003 v->nscrolled = v->child->begin_y - y;
1005 if (v->child->resized_x)
1006 v->child->begin_x = v->child->resized_x;
1008 v->child->begin_x -= resize;
1010 v->child->begin_x += resize;
1011 if (v->child->begin_x < 11) {
1013 v->child->begin_x = 11;
1014 } else if (v->child->begin_x > COLS - 1) {
1016 v->child->begin_x = COLS - 1;
1018 v->child->resized_x = v->child->begin_x;
1021 v->child->mode = v->mode;
1022 v->child->nlines = v->lines - v->child->begin_y;
1023 v->child->ncols = v->cols - v->child->begin_x;
1026 err = view_fullscreen(v);
1029 err = view_splitscreen(v->child);
1033 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1034 err = offset_selection_down(v->child);
1040 err = request_log_commits(v);
1041 else if (v->child->nscrolled)
1042 err = request_log_commits(v->child);
1044 v->resized = v->child->resized = 0;
1050 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1052 struct tog_view *v = src->child ? src->child : src;
1054 dst->resized_x = v->resized_x;
1055 dst->resized_y = v->resized_y;
1058 static const struct got_error *
1059 view_close_child(struct tog_view *view)
1061 const struct got_error *err = NULL;
1063 if (view->child == NULL)
1066 err = view_close(view->child);
1071 static const struct got_error *
1072 view_set_child(struct tog_view *view, struct tog_view *child)
1074 const struct got_error *err = NULL;
1076 view->child = child;
1077 child->parent = view;
1079 err = view_resize(view);
1083 if (view->child->resized_x || view->child->resized_y)
1084 err = view_resize_split(view, 0);
1090 tog_resizeterm(void)
1093 struct winsize size;
1095 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1096 cols = 80; /* Default */
1100 lines = size.ws_row;
1102 resize_term(lines, cols);
1105 static const struct got_error *
1106 view_search_start(struct tog_view *view)
1108 const struct got_error *err = NULL;
1109 struct tog_view *v = view;
1113 if (view->search_started) {
1114 regfree(&view->regex);
1115 view->searching = 0;
1116 memset(&view->regmatch, 0, sizeof(view->regmatch));
1118 view->search_started = 0;
1120 if (view->nlines < 1)
1123 if (view_is_hsplit_top(view))
1126 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1127 wclrtoeol(v->window);
1129 nodelay(view->window, FALSE); /* block for search term input */
1132 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1133 wrefresh(v->window);
1136 nodelay(view->window, TRUE);
1140 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1141 err = view->search_start(view);
1143 regfree(&view->regex);
1146 view->search_started = 1;
1147 view->searching = TOG_SEARCH_FORWARD;
1148 view->search_next_done = 0;
1149 view->search_next(view);
1155 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1156 static const struct got_error *
1157 switch_split(struct tog_view *view)
1159 const struct got_error *err = NULL;
1160 struct tog_view *v = NULL;
1167 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1168 v->mode = TOG_VIEW_SPLIT_VERT;
1170 v->mode = TOG_VIEW_SPLIT_HRZN;
1174 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1175 v->mode = TOG_VIEW_SPLIT_NONE;
1177 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1178 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1179 v->child->begin_y = v->child->resized_y;
1180 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1181 v->child->begin_x = v->child->resized_x;
1184 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1186 v->child->ncols = COLS;
1187 v->child->nscrolled = LINES - v->child->nlines;
1189 err = view_init_hsplit(v, v->child->begin_y);
1193 v->child->mode = v->mode;
1194 v->child->nlines = v->lines - v->child->begin_y;
1197 err = view_fullscreen(v);
1200 err = view_splitscreen(v->child);
1204 if (v->mode == TOG_VIEW_SPLIT_NONE)
1205 v->mode = TOG_VIEW_SPLIT_VERT;
1206 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1207 err = offset_selection_down(v);
1208 err = offset_selection_down(v->child);
1210 offset_selection_up(v);
1211 offset_selection_up(v->child);
1213 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1214 err = request_log_commits(v);
1215 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1216 err = request_log_commits(v->child);
1222 * Compute view->count from numeric input. Assign total to view->count and
1223 * return first non-numeric key entered.
1226 get_compound_key(struct tog_view *view, int c)
1228 struct tog_view *v = view;
1231 if (view_is_hsplit_top(view))
1233 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1237 cbreak(); /* block for input */
1238 wmove(v->window, v->nlines - 1, 0);
1239 wclrtoeol(v->window);
1240 waddch(v->window, ':');
1243 x = getcurx(v->window);
1244 if (x != ERR && x < view->ncols) {
1245 waddch(v->window, c);
1246 wrefresh(v->window);
1250 * Don't overflow. Max valid request should be the greatest
1251 * between the longest and total lines; cap at 10 million.
1256 n = n * 10 + (c - '0');
1257 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1259 /* Massage excessive or inapplicable values at the input handler. */
1265 static const struct got_error *
1266 view_input(struct tog_view **new, int *done, struct tog_view *view,
1267 struct tog_view_list_head *views)
1269 const struct got_error *err = NULL;
1275 /* Clear "no matches" indicator. */
1276 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1277 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1278 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1282 if (view->searching && !view->search_next_done) {
1283 errcode = pthread_mutex_unlock(&tog_mutex);
1285 return got_error_set_errno(errcode,
1286 "pthread_mutex_unlock");
1288 errcode = pthread_mutex_lock(&tog_mutex);
1290 return got_error_set_errno(errcode,
1291 "pthread_mutex_lock");
1292 view->search_next(view);
1296 nodelay(view->window, FALSE);
1297 /* Allow threads to make progress while we are waiting for input. */
1298 errcode = pthread_mutex_unlock(&tog_mutex);
1300 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1301 /* If we have an unfinished count, let C-g or backspace abort. */
1302 if (view->count && --view->count) {
1304 nodelay(view->window, TRUE);
1305 ch = wgetch(view->window);
1306 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1311 ch = wgetch(view->window);
1312 if (ch >= '1' && ch <= '9')
1313 view->ch = ch = get_compound_key(view, ch);
1315 errcode = pthread_mutex_lock(&tog_mutex);
1317 return got_error_set_errno(errcode, "pthread_mutex_lock");
1318 nodelay(view->window, TRUE);
1320 if (tog_sigwinch_received || tog_sigcont_received) {
1322 tog_sigwinch_received = 0;
1323 tog_sigcont_received = 0;
1324 TAILQ_FOREACH(v, views, entry) {
1325 err = view_resize(v);
1328 err = v->input(new, v, KEY_RESIZE);
1332 err = view_resize(v->child);
1335 err = v->child->input(new, v->child,
1339 if (v->child->resized_x || v->child->resized_y) {
1340 err = view_resize_split(v, 0);
1353 view->child->focussed = 1;
1354 view->focus_child = 1;
1355 } else if (view->parent) {
1357 view->parent->focussed = 1;
1358 view->parent->focus_child = 0;
1359 if (!view_is_splitscreen(view)) {
1360 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1361 view->parent->type == TOG_VIEW_LOG) {
1362 err = request_log_commits(view->parent);
1366 offset_selection_up(view->parent);
1367 err = view_fullscreen(view->parent);
1374 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1375 if (view->parent->type == TOG_VIEW_LOG) {
1376 /* might need more commits to fill fullscreen */
1377 err = request_log_commits(view->parent);
1381 offset_selection_up(view->parent);
1383 err = view->input(new, view, ch);
1391 if (view_is_parent_view(view)) {
1392 if (view->child == NULL)
1394 if (view_is_splitscreen(view->child)) {
1396 view->child->focussed = 1;
1397 err = view_fullscreen(view->child);
1399 err = view_splitscreen(view->child);
1401 err = view_resize_split(view, 0);
1405 err = view->child->input(new, view->child,
1408 if (view_is_splitscreen(view)) {
1409 view->parent->focussed = 0;
1411 err = view_fullscreen(view);
1413 err = view_splitscreen(view);
1414 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1415 err = view_resize(view->parent);
1417 err = view_resize_split(view, 0);
1421 err = view->input(new, view, KEY_RESIZE);
1425 if (view->type == TOG_VIEW_LOG) {
1426 err = request_log_commits(view);
1431 err = offset_selection_down(view->parent);
1433 err = offset_selection_down(view);
1437 err = switch_split(view);
1440 err = view_resize_split(view, -1);
1443 err = view_resize_split(view, 1);
1449 if (view->search_start)
1450 view_search_start(view);
1452 err = view->input(new, view, ch);
1456 if (view->search_started && view->search_next) {
1457 view->searching = (ch == 'n' ?
1458 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1459 view->search_next_done = 0;
1460 view->search_next(view);
1462 err = view->input(new, view, ch);
1465 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1466 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1468 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1469 TAILQ_FOREACH(v, views, entry) {
1475 if (v->child && v->child->reset) {
1476 err = v->child->reset(v->child);
1483 err = view->input(new, view, ch);
1491 view_needs_focus_indication(struct tog_view *view)
1493 if (view_is_parent_view(view)) {
1494 if (view->child == NULL || view->child->focussed)
1496 if (!view_is_splitscreen(view->child))
1498 } else if (!view_is_splitscreen(view))
1501 return view->focussed;
1504 static const struct got_error *
1505 view_loop(struct tog_view *view)
1507 const struct got_error *err = NULL;
1508 struct tog_view_list_head views;
1509 struct tog_view *new_view;
1511 int fast_refresh = 10;
1512 int done = 0, errcode;
1514 mode = getenv("TOG_VIEW_SPLIT_MODE");
1515 if (!mode || !(*mode == 'h' || *mode == 'H'))
1516 view->mode = TOG_VIEW_SPLIT_VERT;
1518 view->mode = TOG_VIEW_SPLIT_HRZN;
1520 errcode = pthread_mutex_lock(&tog_mutex);
1522 return got_error_set_errno(errcode, "pthread_mutex_lock");
1525 TAILQ_INSERT_HEAD(&views, view, entry);
1528 err = view->show(view);
1533 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1534 !tog_fatal_signal_received()) {
1535 /* Refresh fast during initialization, then become slower. */
1536 if (fast_refresh && fast_refresh-- == 0)
1537 halfdelay(10); /* switch to once per second */
1539 err = view_input(&new_view, &done, view, &views);
1543 struct tog_view *v, *prev = NULL;
1545 if (view_is_parent_view(view))
1546 prev = TAILQ_PREV(view, tog_view_list_head,
1548 else if (view->parent)
1549 prev = view->parent;
1552 view->parent->child = NULL;
1553 view->parent->focus_child = 0;
1554 /* Restore fullscreen line height. */
1555 view->parent->nlines = view->parent->lines;
1556 err = view_resize(view->parent);
1559 /* Make resized splits persist. */
1560 view_transfer_size(view->parent, view);
1562 TAILQ_REMOVE(&views, view, entry);
1564 err = view_close(view);
1569 TAILQ_FOREACH(v, &views, entry) {
1573 if (view == NULL && new_view == NULL) {
1574 /* No view has focus. Try to pick one. */
1577 else if (!TAILQ_EMPTY(&views)) {
1578 view = TAILQ_LAST(&views,
1579 tog_view_list_head);
1582 if (view->focus_child) {
1583 view->child->focussed = 1;
1591 struct tog_view *v, *t;
1592 /* Only allow one parent view per type. */
1593 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1594 if (v->type != new_view->type)
1596 TAILQ_REMOVE(&views, v, entry);
1597 err = view_close(v);
1602 TAILQ_INSERT_TAIL(&views, new_view, entry);
1606 if (view_is_parent_view(view)) {
1607 if (view->child && view->child->focussed)
1610 if (view->parent && view->parent->focussed)
1611 view = view->parent;
1613 show_panel(view->panel);
1614 if (view->child && view_is_splitscreen(view->child))
1615 show_panel(view->child->panel);
1616 if (view->parent && view_is_splitscreen(view)) {
1617 err = view->parent->show(view->parent);
1621 err = view->show(view);
1625 err = view->child->show(view->child);
1634 while (!TAILQ_EMPTY(&views)) {
1635 const struct got_error *close_err;
1636 view = TAILQ_FIRST(&views);
1637 TAILQ_REMOVE(&views, view, entry);
1638 close_err = view_close(view);
1639 if (close_err && err == NULL)
1643 errcode = pthread_mutex_unlock(&tog_mutex);
1644 if (errcode && err == NULL)
1645 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1655 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1660 /* Create newly allocated wide-character string equivalent to a byte string. */
1661 static const struct got_error *
1662 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1665 const struct got_error *err = NULL;
1668 *wlen = mbstowcs(NULL, s, 0);
1669 if (*wlen == (size_t)-1) {
1671 if (errno != EILSEQ)
1672 return got_error_from_errno("mbstowcs");
1674 /* byte string invalid in current encoding; try to "fix" it */
1675 err = got_mbsavis(&vis, &vislen, s);
1678 *wlen = mbstowcs(NULL, vis, 0);
1679 if (*wlen == (size_t)-1) {
1680 err = got_error_from_errno("mbstowcs"); /* give up */
1685 *ws = calloc(*wlen + 1, sizeof(**ws));
1687 err = got_error_from_errno("calloc");
1691 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1692 err = got_error_from_errno("mbstowcs");
1703 static const struct got_error *
1704 expand_tab(char **ptr, const char *src)
1707 size_t len, n, idx = 0, sz = 0;
1710 n = len = strlen(src);
1711 dst = malloc(n + 1);
1713 return got_error_from_errno("malloc");
1715 while (idx < len && src[idx]) {
1716 const char c = src[idx];
1719 size_t nb = TABSIZE - sz % TABSIZE;
1722 p = realloc(dst, n + nb);
1725 return got_error_from_errno("realloc");
1730 memset(dst + sz, ' ', nb);
1733 dst[sz++] = src[idx];
1743 * Advance at most n columns from wline starting at offset off.
1744 * Return the index to the first character after the span operation.
1745 * Return the combined column width of all spanned wide character in
1749 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1751 int width, i, cols = 0;
1758 for (i = off; wline[i] != L'\0'; ++i) {
1759 if (wline[i] == L'\t')
1760 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1762 width = wcwidth(wline[i]);
1769 if (cols + width > n)
1779 * Format a line for display, ensuring that it won't overflow a width limit.
1780 * With scrolling, the width returned refers to the scrolled version of the
1781 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1783 static const struct got_error *
1784 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1785 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1787 const struct got_error *err = NULL;
1789 wchar_t *wline = NULL;
1798 err = expand_tab(&exstr, line);
1803 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1808 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1810 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1811 wline[wlen - 1] = L'\0';
1814 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1815 wline[wlen - 1] = L'\0';
1819 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1825 *scrollxp = scrollx;
1833 static const struct got_error*
1834 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1835 struct got_object_id *id, struct got_repository *repo)
1837 static const struct got_error *err = NULL;
1838 struct got_reflist_entry *re;
1844 TAILQ_FOREACH(re, refs, entry) {
1845 struct got_tag_object *tag = NULL;
1846 struct got_object_id *ref_id;
1849 name = got_ref_get_name(re->ref);
1850 if (strcmp(name, GOT_REF_HEAD) == 0)
1852 if (strncmp(name, "refs/", 5) == 0)
1854 if (strncmp(name, "got/", 4) == 0 &&
1855 strncmp(name, "got/backup/", 11) != 0)
1857 if (strncmp(name, "heads/", 6) == 0)
1859 if (strncmp(name, "remotes/", 8) == 0) {
1861 s = strstr(name, "/" GOT_REF_HEAD);
1862 if (s != NULL && s[strlen(s)] == '\0')
1865 err = got_ref_resolve(&ref_id, repo, re->ref);
1868 if (strncmp(name, "tags/", 5) == 0) {
1869 err = got_object_open_as_tag(&tag, repo, ref_id);
1871 if (err->code != GOT_ERR_OBJ_TYPE) {
1875 /* Ref points at something other than a tag. */
1880 cmp = got_object_id_cmp(tag ?
1881 got_object_tag_get_object_id(tag) : ref_id, id);
1884 got_object_tag_close(tag);
1888 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1889 s ? ", " : "", name) == -1) {
1890 err = got_error_from_errno("asprintf");
1901 static const struct got_error *
1902 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1907 smallerthan = strchr(author, '<');
1908 if (smallerthan && smallerthan[1] != '\0')
1909 author = smallerthan + 1;
1910 author[strcspn(author, "@>")] = '\0';
1911 return format_line(wauthor, author_width, NULL, author, 0, limit,
1915 static const struct got_error *
1916 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1917 struct got_object_id *id, const size_t date_display_cols,
1918 int author_display_cols)
1920 struct tog_log_view_state *s = &view->state.log;
1921 const struct got_error *err = NULL;
1922 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1923 char *logmsg0 = NULL, *logmsg = NULL;
1924 char *author = NULL;
1925 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1926 int author_width, logmsg_width;
1927 char *newline, *line = NULL;
1928 int col, limit, scrollx;
1929 const int avail = view->ncols;
1931 time_t committer_time;
1932 struct tog_color *tc;
1934 committer_time = got_object_commit_get_committer_time(commit);
1935 if (gmtime_r(&committer_time, &tm) == NULL)
1936 return got_error_from_errno("gmtime_r");
1937 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1938 return got_error(GOT_ERR_NO_SPACE);
1940 if (avail <= date_display_cols)
1941 limit = MIN(sizeof(datebuf) - 1, avail);
1943 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1944 tc = get_color(&s->colors, TOG_COLOR_DATE);
1946 wattr_on(view->window,
1947 COLOR_PAIR(tc->colorpair), NULL);
1948 waddnstr(view->window, datebuf, limit);
1950 wattr_off(view->window,
1951 COLOR_PAIR(tc->colorpair), NULL);
1958 err = got_object_id_str(&id_str, id);
1961 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1963 wattr_on(view->window,
1964 COLOR_PAIR(tc->colorpair), NULL);
1965 wprintw(view->window, "%.8s ", id_str);
1967 wattr_off(view->window,
1968 COLOR_PAIR(tc->colorpair), NULL);
1975 if (s->use_committer)
1976 author = strdup(got_object_commit_get_committer(commit));
1978 author = strdup(got_object_commit_get_author(commit));
1979 if (author == NULL) {
1980 err = got_error_from_errno("strdup");
1983 err = format_author(&wauthor, &author_width, author, avail - col, col);
1986 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1988 wattr_on(view->window,
1989 COLOR_PAIR(tc->colorpair), NULL);
1990 waddwstr(view->window, wauthor);
1992 wattr_off(view->window,
1993 COLOR_PAIR(tc->colorpair), NULL);
1994 col += author_width;
1995 while (col < avail && author_width < author_display_cols + 2) {
1996 waddch(view->window, ' ');
2003 err = got_object_commit_get_logmsg(&logmsg0, commit);
2007 while (*logmsg == '\n')
2009 newline = strchr(logmsg, '\n');
2012 limit = avail - col;
2013 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2014 limit--; /* for the border */
2015 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2019 waddwstr(view->window, &wlogmsg[scrollx]);
2020 col += MAX(logmsg_width, 0);
2021 while (col < avail) {
2022 waddch(view->window, ' ');
2034 static struct commit_queue_entry *
2035 alloc_commit_queue_entry(struct got_commit_object *commit,
2036 struct got_object_id *id)
2038 struct commit_queue_entry *entry;
2040 entry = calloc(1, sizeof(*entry));
2045 entry->commit = commit;
2050 pop_commit(struct commit_queue *commits)
2052 struct commit_queue_entry *entry;
2054 entry = TAILQ_FIRST(&commits->head);
2055 TAILQ_REMOVE(&commits->head, entry, entry);
2056 got_object_commit_close(entry->commit);
2057 commits->ncommits--;
2058 /* Don't free entry->id! It is owned by the commit graph. */
2063 free_commits(struct commit_queue *commits)
2065 while (!TAILQ_EMPTY(&commits->head))
2066 pop_commit(commits);
2069 static const struct got_error *
2070 match_commit(int *have_match, struct got_object_id *id,
2071 struct got_commit_object *commit, regex_t *regex)
2073 const struct got_error *err = NULL;
2074 regmatch_t regmatch;
2075 char *id_str = NULL, *logmsg = NULL;
2079 err = got_object_id_str(&id_str, id);
2083 err = got_object_commit_get_logmsg(&logmsg, commit);
2087 if (regexec(regex, got_object_commit_get_author(commit), 1,
2088 ®match, 0) == 0 ||
2089 regexec(regex, got_object_commit_get_committer(commit), 1,
2090 ®match, 0) == 0 ||
2091 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2092 regexec(regex, logmsg, 1, ®match, 0) == 0)
2100 static const struct got_error *
2101 queue_commits(struct tog_log_thread_args *a)
2103 const struct got_error *err = NULL;
2106 * We keep all commits open throughout the lifetime of the log
2107 * view in order to avoid having to re-fetch commits from disk
2108 * while updating the display.
2111 struct got_object_id *id;
2112 struct got_commit_object *commit;
2113 struct commit_queue_entry *entry;
2116 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2118 if (err || id == NULL)
2121 err = got_object_open_as_commit(&commit, a->repo, id);
2124 entry = alloc_commit_queue_entry(commit, id);
2125 if (entry == NULL) {
2126 err = got_error_from_errno("alloc_commit_queue_entry");
2130 errcode = pthread_mutex_lock(&tog_mutex);
2132 err = got_error_set_errno(errcode,
2133 "pthread_mutex_lock");
2137 entry->idx = a->commits->ncommits;
2138 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2139 a->commits->ncommits++;
2141 if (*a->searching == TOG_SEARCH_FORWARD &&
2142 !*a->search_next_done) {
2144 err = match_commit(&have_match, id, commit, a->regex);
2148 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2151 errcode = pthread_mutex_unlock(&tog_mutex);
2152 if (errcode && err == NULL)
2153 err = got_error_set_errno(errcode,
2154 "pthread_mutex_unlock");
2157 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2163 select_commit(struct tog_log_view_state *s)
2165 struct commit_queue_entry *entry;
2168 entry = s->first_displayed_entry;
2170 if (ncommits == s->selected) {
2171 s->selected_entry = entry;
2174 entry = TAILQ_NEXT(entry, entry);
2179 static const struct got_error *
2180 draw_commits(struct tog_view *view)
2182 const struct got_error *err = NULL;
2183 struct tog_log_view_state *s = &view->state.log;
2184 struct commit_queue_entry *entry = s->selected_entry;
2185 const int limit = view->nlines;
2187 int ncommits, author_cols = 4;
2188 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2189 char *refs_str = NULL;
2191 struct tog_color *tc;
2192 static const size_t date_display_cols = 12;
2194 if (s->selected_entry &&
2195 !(view->searching && view->search_next_done == 0)) {
2196 struct got_reflist_head *refs;
2197 err = got_object_id_str(&id_str, s->selected_entry->id);
2200 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2201 s->selected_entry->id);
2203 err = build_refs_str(&refs_str, refs,
2204 s->selected_entry->id, s->repo);
2210 if (s->thread_args.commits_needed == 0)
2211 halfdelay(10); /* disable fast refresh */
2213 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2214 if (asprintf(&ncommits_str, " [%d/%d] %s",
2215 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2216 (view->searching && !view->search_next_done) ?
2217 "searching..." : "loading...") == -1) {
2218 err = got_error_from_errno("asprintf");
2222 const char *search_str = NULL;
2224 if (view->searching) {
2225 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2226 search_str = "no more matches";
2227 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2228 search_str = "no matches found";
2229 else if (!view->search_next_done)
2230 search_str = "searching...";
2233 if (asprintf(&ncommits_str, " [%d/%d] %s",
2234 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2235 search_str ? search_str :
2236 (refs_str ? refs_str : "")) == -1) {
2237 err = got_error_from_errno("asprintf");
2242 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2243 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2244 "........................................",
2245 s->in_repo_path, ncommits_str) == -1) {
2246 err = got_error_from_errno("asprintf");
2250 } else if (asprintf(&header, "commit %s%s",
2251 id_str ? id_str : "........................................",
2252 ncommits_str) == -1) {
2253 err = got_error_from_errno("asprintf");
2257 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2261 werase(view->window);
2263 if (view_needs_focus_indication(view))
2264 wstandout(view->window);
2265 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2267 wattr_on(view->window,
2268 COLOR_PAIR(tc->colorpair), NULL);
2269 waddwstr(view->window, wline);
2271 wattr_off(view->window,
2272 COLOR_PAIR(tc->colorpair), NULL);
2273 while (width < view->ncols) {
2274 waddch(view->window, ' ');
2277 if (view_needs_focus_indication(view))
2278 wstandend(view->window);
2283 /* Grow author column size if necessary, and set view->maxx. */
2284 entry = s->first_displayed_entry;
2288 struct got_commit_object *c = entry->commit;
2289 char *author, *eol, *msg, *msg0;
2290 wchar_t *wauthor, *wmsg;
2292 if (ncommits >= limit - 1)
2294 if (s->use_committer)
2295 author = strdup(got_object_commit_get_committer(c));
2297 author = strdup(got_object_commit_get_author(c));
2298 if (author == NULL) {
2299 err = got_error_from_errno("strdup");
2302 err = format_author(&wauthor, &width, author, COLS,
2304 if (author_cols < width)
2305 author_cols = width;
2310 err = got_object_commit_get_logmsg(&msg0, c);
2314 while (*msg == '\n')
2316 if ((eol = strchr(msg, '\n')))
2318 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2319 date_display_cols + author_cols, 0);
2322 view->maxx = MAX(view->maxx, width);
2326 entry = TAILQ_NEXT(entry, entry);
2329 entry = s->first_displayed_entry;
2330 s->last_displayed_entry = s->first_displayed_entry;
2333 if (ncommits >= limit - 1)
2335 if (ncommits == s->selected)
2336 wstandout(view->window);
2337 err = draw_commit(view, entry->commit, entry->id,
2338 date_display_cols, author_cols);
2339 if (ncommits == s->selected)
2340 wstandend(view->window);
2344 s->last_displayed_entry = entry;
2345 entry = TAILQ_NEXT(entry, entry);
2358 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2360 struct commit_queue_entry *entry;
2363 entry = TAILQ_FIRST(&s->commits.head);
2364 if (s->first_displayed_entry == entry)
2367 entry = s->first_displayed_entry;
2368 while (entry && nscrolled < maxscroll) {
2369 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2371 s->first_displayed_entry = entry;
2377 static const struct got_error *
2378 trigger_log_thread(struct tog_view *view, int wait)
2380 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2383 halfdelay(1); /* fast refresh while loading commits */
2385 while (!ta->log_complete && !tog_thread_error &&
2386 (ta->commits_needed > 0 || ta->load_all)) {
2387 /* Wake the log thread. */
2388 errcode = pthread_cond_signal(&ta->need_commits);
2390 return got_error_set_errno(errcode,
2391 "pthread_cond_signal");
2394 * The mutex will be released while the view loop waits
2395 * in wgetch(), at which time the log thread will run.
2400 /* Display progress update in log view. */
2401 show_log_view(view);
2405 /* Wait right here while next commit is being loaded. */
2406 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2408 return got_error_set_errno(errcode,
2409 "pthread_cond_wait");
2411 /* Display progress update in log view. */
2412 show_log_view(view);
2420 static const struct got_error *
2421 request_log_commits(struct tog_view *view)
2423 struct tog_log_view_state *state = &view->state.log;
2424 const struct got_error *err = NULL;
2426 if (state->thread_args.log_complete)
2429 state->thread_args.commits_needed += view->nscrolled;
2430 err = trigger_log_thread(view, 1);
2431 view->nscrolled = 0;
2436 static const struct got_error *
2437 log_scroll_down(struct tog_view *view, int maxscroll)
2439 struct tog_log_view_state *s = &view->state.log;
2440 const struct got_error *err = NULL;
2441 struct commit_queue_entry *pentry;
2442 int nscrolled = 0, ncommits_needed;
2444 if (s->last_displayed_entry == NULL)
2447 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2448 if (s->commits.ncommits < ncommits_needed &&
2449 !s->thread_args.log_complete) {
2451 * Ask the log thread for required amount of commits.
2453 s->thread_args.commits_needed += maxscroll;
2454 err = trigger_log_thread(view, 1);
2460 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2461 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2464 s->last_displayed_entry = pentry ?
2465 pentry : s->last_displayed_entry;;
2467 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2470 s->first_displayed_entry = pentry;
2471 } while (++nscrolled < maxscroll);
2473 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2474 view->nscrolled += nscrolled;
2476 view->nscrolled = 0;
2481 static const struct got_error *
2482 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2483 struct got_commit_object *commit, struct got_object_id *commit_id,
2484 struct tog_view *log_view, struct got_repository *repo)
2486 const struct got_error *err;
2487 struct got_object_qid *parent_id;
2488 struct tog_view *diff_view;
2490 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2491 if (diff_view == NULL)
2492 return got_error_from_errno("view_open");
2494 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2495 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2496 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2498 *new_view = diff_view;
2502 static const struct got_error *
2503 tree_view_visit_subtree(struct tog_tree_view_state *s,
2504 struct got_tree_object *subtree)
2506 struct tog_parent_tree *parent;
2508 parent = calloc(1, sizeof(*parent));
2510 return got_error_from_errno("calloc");
2512 parent->tree = s->tree;
2513 parent->first_displayed_entry = s->first_displayed_entry;
2514 parent->selected_entry = s->selected_entry;
2515 parent->selected = s->selected;
2516 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2519 s->first_displayed_entry = NULL;
2523 static const struct got_error *
2524 tree_view_walk_path(struct tog_tree_view_state *s,
2525 struct got_commit_object *commit, const char *path)
2527 const struct got_error *err = NULL;
2528 struct got_tree_object *tree = NULL;
2530 char *slash, *subpath = NULL;
2532 /* Walk the path and open corresponding tree objects. */
2535 struct got_tree_entry *te;
2536 struct got_object_id *tree_id;
2542 /* Ensure the correct subtree entry is selected. */
2543 slash = strchr(p, '/');
2545 te_name = strdup(p);
2547 te_name = strndup(p, slash - p);
2548 if (te_name == NULL) {
2549 err = got_error_from_errno("strndup");
2552 te = got_object_tree_find_entry(s->tree, te_name);
2554 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2559 s->first_displayed_entry = s->selected_entry = te;
2561 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2562 break; /* jump to this file's entry */
2564 slash = strchr(p, '/');
2566 subpath = strndup(path, slash - path);
2568 subpath = strdup(path);
2569 if (subpath == NULL) {
2570 err = got_error_from_errno("strdup");
2574 err = got_object_id_by_path(&tree_id, s->repo, commit,
2579 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2584 err = tree_view_visit_subtree(s, tree);
2586 got_object_tree_close(tree);
2600 static const struct got_error *
2601 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2602 struct commit_queue_entry *entry, const char *path,
2603 const char *head_ref_name, struct got_repository *repo)
2605 const struct got_error *err = NULL;
2606 struct tog_tree_view_state *s;
2607 struct tog_view *tree_view;
2609 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2610 if (tree_view == NULL)
2611 return got_error_from_errno("view_open");
2613 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2616 s = &tree_view->state.tree;
2618 *new_view = tree_view;
2620 if (got_path_is_root_dir(path))
2623 return tree_view_walk_path(s, entry->commit, path);
2626 static const struct got_error *
2627 block_signals_used_by_main_thread(void)
2632 if (sigemptyset(&sigset) == -1)
2633 return got_error_from_errno("sigemptyset");
2635 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2636 if (sigaddset(&sigset, SIGWINCH) == -1)
2637 return got_error_from_errno("sigaddset");
2638 if (sigaddset(&sigset, SIGCONT) == -1)
2639 return got_error_from_errno("sigaddset");
2640 if (sigaddset(&sigset, SIGINT) == -1)
2641 return got_error_from_errno("sigaddset");
2642 if (sigaddset(&sigset, SIGTERM) == -1)
2643 return got_error_from_errno("sigaddset");
2645 /* ncurses handles SIGTSTP */
2646 if (sigaddset(&sigset, SIGTSTP) == -1)
2647 return got_error_from_errno("sigaddset");
2649 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2651 return got_error_set_errno(errcode, "pthread_sigmask");
2657 log_thread(void *arg)
2659 const struct got_error *err = NULL;
2661 struct tog_log_thread_args *a = arg;
2665 * Sync startup with main thread such that we begin our
2666 * work once view_input() has released the mutex.
2668 errcode = pthread_mutex_lock(&tog_mutex);
2670 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2674 err = block_signals_used_by_main_thread();
2676 pthread_mutex_unlock(&tog_mutex);
2680 while (!done && !err && !tog_fatal_signal_received()) {
2681 errcode = pthread_mutex_unlock(&tog_mutex);
2683 err = got_error_set_errno(errcode,
2684 "pthread_mutex_unlock");
2687 err = queue_commits(a);
2689 if (err->code != GOT_ERR_ITER_COMPLETED)
2693 } else if (a->commits_needed > 0 && !a->load_all)
2694 a->commits_needed--;
2696 errcode = pthread_mutex_lock(&tog_mutex);
2698 err = got_error_set_errno(errcode,
2699 "pthread_mutex_lock");
2701 } else if (*a->quit)
2703 else if (*a->first_displayed_entry == NULL) {
2704 *a->first_displayed_entry =
2705 TAILQ_FIRST(&a->commits->head);
2706 *a->selected_entry = *a->first_displayed_entry;
2709 errcode = pthread_cond_signal(&a->commit_loaded);
2711 err = got_error_set_errno(errcode,
2712 "pthread_cond_signal");
2713 pthread_mutex_unlock(&tog_mutex);
2718 a->commits_needed = 0;
2720 if (a->commits_needed == 0 && !a->load_all) {
2721 errcode = pthread_cond_wait(&a->need_commits,
2724 err = got_error_set_errno(errcode,
2725 "pthread_cond_wait");
2726 pthread_mutex_unlock(&tog_mutex);
2734 a->log_complete = 1;
2735 errcode = pthread_mutex_unlock(&tog_mutex);
2737 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2740 tog_thread_error = 1;
2741 pthread_cond_signal(&a->commit_loaded);
2746 static const struct got_error *
2747 stop_log_thread(struct tog_log_view_state *s)
2749 const struct got_error *err = NULL, *thread_err = NULL;
2754 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2756 return got_error_set_errno(errcode,
2757 "pthread_cond_signal");
2758 errcode = pthread_mutex_unlock(&tog_mutex);
2760 return got_error_set_errno(errcode,
2761 "pthread_mutex_unlock");
2762 errcode = pthread_join(s->thread, (void **)&thread_err);
2764 return got_error_set_errno(errcode, "pthread_join");
2765 errcode = pthread_mutex_lock(&tog_mutex);
2767 return got_error_set_errno(errcode,
2768 "pthread_mutex_lock");
2772 if (s->thread_args.repo) {
2773 err = got_repo_close(s->thread_args.repo);
2774 s->thread_args.repo = NULL;
2777 if (s->thread_args.pack_fds) {
2778 const struct got_error *pack_err =
2779 got_repo_pack_fds_close(s->thread_args.pack_fds);
2782 s->thread_args.pack_fds = NULL;
2785 if (s->thread_args.graph) {
2786 got_commit_graph_close(s->thread_args.graph);
2787 s->thread_args.graph = NULL;
2790 return err ? err : thread_err;
2793 static const struct got_error *
2794 close_log_view(struct tog_view *view)
2796 const struct got_error *err = NULL;
2797 struct tog_log_view_state *s = &view->state.log;
2800 err = stop_log_thread(s);
2802 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2803 if (errcode && err == NULL)
2804 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2806 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2807 if (errcode && err == NULL)
2808 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2810 free_commits(&s->commits);
2811 free(s->in_repo_path);
2812 s->in_repo_path = NULL;
2815 free(s->head_ref_name);
2816 s->head_ref_name = NULL;
2820 static const struct got_error *
2821 search_start_log_view(struct tog_view *view)
2823 struct tog_log_view_state *s = &view->state.log;
2825 s->matched_entry = NULL;
2826 s->search_entry = NULL;
2830 static const struct got_error *
2831 search_next_log_view(struct tog_view *view)
2833 const struct got_error *err = NULL;
2834 struct tog_log_view_state *s = &view->state.log;
2835 struct commit_queue_entry *entry;
2837 /* Display progress update in log view. */
2838 show_log_view(view);
2842 if (s->search_entry) {
2844 errcode = pthread_mutex_unlock(&tog_mutex);
2846 return got_error_set_errno(errcode,
2847 "pthread_mutex_unlock");
2848 ch = wgetch(view->window);
2849 errcode = pthread_mutex_lock(&tog_mutex);
2851 return got_error_set_errno(errcode,
2852 "pthread_mutex_lock");
2853 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2854 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2857 if (view->searching == TOG_SEARCH_FORWARD)
2858 entry = TAILQ_NEXT(s->search_entry, entry);
2860 entry = TAILQ_PREV(s->search_entry,
2861 commit_queue_head, entry);
2862 } else if (s->matched_entry) {
2863 int matched_idx = s->matched_entry->idx;
2864 int selected_idx = s->selected_entry->idx;
2867 * If the user has moved the cursor after we hit a match,
2868 * the position from where we should continue searching
2869 * might have changed.
2871 if (view->searching == TOG_SEARCH_FORWARD) {
2872 if (matched_idx > selected_idx)
2873 entry = TAILQ_NEXT(s->selected_entry, entry);
2875 entry = TAILQ_NEXT(s->matched_entry, entry);
2877 if (matched_idx < selected_idx)
2878 entry = TAILQ_PREV(s->selected_entry,
2879 commit_queue_head, entry);
2881 entry = TAILQ_PREV(s->matched_entry,
2882 commit_queue_head, entry);
2885 entry = s->selected_entry;
2891 if (entry == NULL) {
2892 if (s->thread_args.log_complete ||
2893 view->searching == TOG_SEARCH_BACKWARD) {
2894 view->search_next_done =
2895 (s->matched_entry == NULL ?
2896 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2897 s->search_entry = NULL;
2901 * Poke the log thread for more commits and return,
2902 * allowing the main loop to make progress. Search
2903 * will resume at s->search_entry once we come back.
2905 s->thread_args.commits_needed++;
2906 return trigger_log_thread(view, 0);
2909 err = match_commit(&have_match, entry->id, entry->commit,
2914 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2915 s->matched_entry = entry;
2919 s->search_entry = entry;
2920 if (view->searching == TOG_SEARCH_FORWARD)
2921 entry = TAILQ_NEXT(entry, entry);
2923 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2926 if (s->matched_entry) {
2927 int cur = s->selected_entry->idx;
2928 while (cur < s->matched_entry->idx) {
2929 err = input_log_view(NULL, view, KEY_DOWN);
2934 while (cur > s->matched_entry->idx) {
2935 err = input_log_view(NULL, view, KEY_UP);
2942 s->search_entry = NULL;
2947 static const struct got_error *
2948 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2949 struct got_repository *repo, const char *head_ref_name,
2950 const char *in_repo_path, int log_branches)
2952 const struct got_error *err = NULL;
2953 struct tog_log_view_state *s = &view->state.log;
2954 struct got_repository *thread_repo = NULL;
2955 struct got_commit_graph *thread_graph = NULL;
2958 if (in_repo_path != s->in_repo_path) {
2959 free(s->in_repo_path);
2960 s->in_repo_path = strdup(in_repo_path);
2961 if (s->in_repo_path == NULL)
2962 return got_error_from_errno("strdup");
2965 /* The commit queue only contains commits being displayed. */
2966 TAILQ_INIT(&s->commits.head);
2967 s->commits.ncommits = 0;
2970 if (head_ref_name) {
2971 s->head_ref_name = strdup(head_ref_name);
2972 if (s->head_ref_name == NULL) {
2973 err = got_error_from_errno("strdup");
2977 s->start_id = got_object_id_dup(start_id);
2978 if (s->start_id == NULL) {
2979 err = got_error_from_errno("got_object_id_dup");
2982 s->log_branches = log_branches;
2984 STAILQ_INIT(&s->colors);
2985 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2986 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2987 get_color_value("TOG_COLOR_COMMIT"));
2990 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2991 get_color_value("TOG_COLOR_AUTHOR"));
2993 free_colors(&s->colors);
2996 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2997 get_color_value("TOG_COLOR_DATE"));
2999 free_colors(&s->colors);
3004 view->show = show_log_view;
3005 view->input = input_log_view;
3006 view->resize = resize_log_view;
3007 view->close = close_log_view;
3008 view->search_start = search_start_log_view;
3009 view->search_next = search_next_log_view;
3011 if (s->thread_args.pack_fds == NULL) {
3012 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3016 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3017 s->thread_args.pack_fds);
3020 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3024 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3025 s->repo, NULL, NULL);
3029 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3031 err = got_error_set_errno(errcode, "pthread_cond_init");
3034 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3036 err = got_error_set_errno(errcode, "pthread_cond_init");
3040 s->thread_args.commits_needed = view->nlines;
3041 s->thread_args.graph = thread_graph;
3042 s->thread_args.commits = &s->commits;
3043 s->thread_args.in_repo_path = s->in_repo_path;
3044 s->thread_args.start_id = s->start_id;
3045 s->thread_args.repo = thread_repo;
3046 s->thread_args.log_complete = 0;
3047 s->thread_args.quit = &s->quit;
3048 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3049 s->thread_args.selected_entry = &s->selected_entry;
3050 s->thread_args.searching = &view->searching;
3051 s->thread_args.search_next_done = &view->search_next_done;
3052 s->thread_args.regex = &view->regex;
3055 close_log_view(view);
3059 static const struct got_error *
3060 show_log_view(struct tog_view *view)
3062 const struct got_error *err;
3063 struct tog_log_view_state *s = &view->state.log;
3065 if (s->thread == NULL) {
3066 int errcode = pthread_create(&s->thread, NULL, log_thread,
3069 return got_error_set_errno(errcode, "pthread_create");
3070 if (s->thread_args.commits_needed > 0) {
3071 err = trigger_log_thread(view, 1);
3077 return draw_commits(view);
3081 log_move_cursor_up(struct tog_view *view, int page, int home)
3083 struct tog_log_view_state *s = &view->state.log;
3085 if (s->selected_entry->idx == 0)
3087 if (s->first_displayed_entry == NULL)
3090 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3092 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3094 if (!page && !home && s->selected > 0)
3097 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3103 static const struct got_error *
3104 log_move_cursor_down(struct tog_view *view, int page)
3106 struct tog_log_view_state *s = &view->state.log;
3107 struct commit_queue_entry *first;
3108 const struct got_error *err = NULL;
3110 first = s->first_displayed_entry;
3111 if (first == NULL) {
3116 if (s->thread_args.log_complete &&
3117 s->selected_entry->idx >= s->commits.ncommits - 1)
3121 int eos = view->nlines - 2;
3123 if (view_is_hsplit_top(view))
3124 --eos; /* border consumes the last line */
3125 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3128 err = log_scroll_down(view, 1);
3129 } else if (s->thread_args.load_all) {
3130 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3131 s->selected += MIN(s->last_displayed_entry->idx -
3132 s->selected_entry->idx, page + 1);
3134 err = log_scroll_down(view, MIN(page,
3135 s->commits.ncommits - s->selected_entry->idx - 1));
3136 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3138 err = log_scroll_down(view, page);
3141 if (first == s->first_displayed_entry && s->selected <
3142 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3143 s->selected = MIN(s->commits.ncommits - 1, page);
3150 * We might necessarily overshoot in horizontal
3151 * splits; if so, select the last displayed commit.
3153 s->selected = MIN(s->selected,
3154 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3158 if (s->thread_args.log_complete &&
3159 s->selected_entry->idx == s->commits.ncommits - 1)
3166 view_get_split(struct tog_view *view, int *y, int *x)
3171 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3172 if (view->child && view->child->resized_y)
3173 *y = view->child->resized_y;
3174 else if (view->resized_y)
3175 *y = view->resized_y;
3177 *y = view_split_begin_y(view->lines);
3178 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3179 if (view->child && view->child->resized_x)
3180 *x = view->child->resized_x;
3181 else if (view->resized_x)
3182 *x = view->resized_x;
3184 *x = view_split_begin_x(view->begin_x);
3188 /* Split view horizontally at y and offset view->state->selected line. */
3189 static const struct got_error *
3190 view_init_hsplit(struct tog_view *view, int y)
3192 const struct got_error *err = NULL;
3196 err = view_resize(view);
3200 err = offset_selection_down(view);
3205 static const struct got_error *
3206 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3208 const struct got_error *err = NULL;
3209 struct tog_log_view_state *s = &view->state.log;
3210 struct tog_view *diff_view = NULL, *tree_view = NULL;
3211 struct tog_view *ref_view = NULL;
3212 struct commit_queue_entry *entry;
3213 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3215 if (s->thread_args.load_all) {
3216 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3217 s->thread_args.load_all = 0;
3218 else if (s->thread_args.log_complete) {
3219 err = log_move_cursor_down(view, s->commits.ncommits);
3220 s->thread_args.load_all = 0;
3225 eos = nscroll = view->nlines - 1;
3226 if (view_is_hsplit_top(view))
3237 view->x = MAX(view->maxx - view->ncols / 2, 0);
3242 if (view->x + view->ncols / 2 < view->maxx)
3243 view->x += 2; /* move two columns right */
3249 view->x -= MIN(view->x, 2); /* move two columns back */
3258 log_move_cursor_up(view, 0, 0);
3262 log_move_cursor_up(view, 0, 1);
3272 log_move_cursor_up(view, nscroll, 0);
3279 err = log_move_cursor_down(view, 0);
3282 s->use_committer = !s->use_committer;
3286 /* We don't know yet how many commits, so we're forced to
3287 * traverse them all. */
3289 if (!s->thread_args.log_complete) {
3290 s->thread_args.load_all = 1;
3291 return trigger_log_thread(view, 0);
3295 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3296 for (n = 0; n < eos; n++) {
3299 s->first_displayed_entry = entry;
3300 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3303 s->selected = n - 1;
3315 err = log_move_cursor_down(view, nscroll);
3318 if (s->selected > view->nlines - 2)
3319 s->selected = view->nlines - 2;
3320 if (s->selected > s->commits.ncommits - 1)
3321 s->selected = s->commits.ncommits - 1;
3323 if (s->commits.ncommits < view->nlines - 1 &&
3324 !s->thread_args.log_complete) {
3325 s->thread_args.commits_needed += (view->nlines - 1) -
3326 s->commits.ncommits;
3327 err = trigger_log_thread(view, 1);
3333 if (s->selected_entry == NULL)
3336 /* get dimensions--don't split till initialisation succeeds */
3337 if (view_is_parent_view(view))
3338 view_get_split(view, &begin_y, &begin_x);
3340 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3341 s->selected_entry->commit, s->selected_entry->id,
3346 if (view_is_parent_view(view) &&
3347 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3348 err = view_init_hsplit(view, begin_y);
3354 diff_view->focussed = 1;
3355 diff_view->mode = view->mode;
3356 diff_view->nlines = view->lines - begin_y;
3358 if (view_is_parent_view(view)) {
3359 view_transfer_size(diff_view, view);
3360 err = view_close_child(view);
3363 err = view_set_child(view, diff_view);
3366 view->focus_child = 1;
3368 *new_view = diff_view;
3372 if (s->selected_entry == NULL)
3374 if (view_is_parent_view(view))
3375 view_get_split(view, &begin_y, &begin_x);
3376 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3377 s->selected_entry, s->in_repo_path, s->head_ref_name,
3381 if (view_is_parent_view(view) &&
3382 view->mode == TOG_VIEW_SPLIT_HRZN) {
3383 err = view_init_hsplit(view, begin_y);
3388 tree_view->focussed = 1;
3389 tree_view->mode = view->mode;
3390 tree_view->nlines = view->lines - begin_y;
3391 if (view_is_parent_view(view)) {
3392 view_transfer_size(tree_view, view);
3393 err = view_close_child(view);
3396 err = view_set_child(view, tree_view);
3399 view->focus_child = 1;
3401 *new_view = tree_view;
3407 if (ch == KEY_BACKSPACE &&
3408 got_path_is_root_dir(s->in_repo_path))
3410 err = stop_log_thread(s);
3413 if (ch == KEY_BACKSPACE) {
3415 err = got_path_dirname(&parent_path, s->in_repo_path);
3418 free(s->in_repo_path);
3419 s->in_repo_path = parent_path;
3420 s->thread_args.in_repo_path = s->in_repo_path;
3421 } else if (ch == CTRL('l')) {
3422 struct got_object_id *start_id;
3423 err = got_repo_match_object_id(&start_id, NULL,
3424 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3425 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3429 s->start_id = start_id;
3430 s->thread_args.start_id = s->start_id;
3432 s->log_branches = !s->log_branches;
3434 if (s->thread_args.pack_fds == NULL) {
3435 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3439 err = got_repo_open(&s->thread_args.repo,
3440 got_repo_get_path(s->repo), NULL,
3441 s->thread_args.pack_fds);
3445 err = tog_load_refs(s->repo, 0);
3448 err = got_commit_graph_open(&s->thread_args.graph,
3449 s->in_repo_path, !s->log_branches);
3452 err = got_commit_graph_iter_start(s->thread_args.graph,
3453 s->start_id, s->repo, NULL, NULL);
3456 free_commits(&s->commits);
3457 s->first_displayed_entry = NULL;
3458 s->last_displayed_entry = NULL;
3459 s->selected_entry = NULL;
3461 s->thread_args.log_complete = 0;
3463 s->thread_args.commits_needed = view->lines;
3464 s->matched_entry = NULL;
3465 s->search_entry = NULL;
3469 if (view_is_parent_view(view))
3470 view_get_split(view, &begin_y, &begin_x);
3471 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3472 if (ref_view == NULL)
3473 return got_error_from_errno("view_open");
3474 err = open_ref_view(ref_view, s->repo);
3476 view_close(ref_view);
3479 if (view_is_parent_view(view) &&
3480 view->mode == TOG_VIEW_SPLIT_HRZN) {
3481 err = view_init_hsplit(view, begin_y);
3486 ref_view->focussed = 1;
3487 ref_view->mode = view->mode;
3488 ref_view->nlines = view->lines - begin_y;
3489 if (view_is_parent_view(view)) {
3490 view_transfer_size(ref_view, view);
3491 err = view_close_child(view);
3494 err = view_set_child(view, ref_view);
3497 view->focus_child = 1;
3499 *new_view = ref_view;
3509 static const struct got_error *
3510 apply_unveil(const char *repo_path, const char *worktree_path)
3512 const struct got_error *error;
3515 if (unveil("gmon.out", "rwc") != 0)
3516 return got_error_from_errno2("unveil", "gmon.out");
3518 if (repo_path && unveil(repo_path, "r") != 0)
3519 return got_error_from_errno2("unveil", repo_path);
3521 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3522 return got_error_from_errno2("unveil", worktree_path);
3524 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3525 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3527 error = got_privsep_unveil_exec_helpers();
3531 if (unveil(NULL, NULL) != 0)
3532 return got_error_from_errno("unveil");
3541 * Override default signal handlers before starting ncurses.
3542 * This should prevent ncurses from installing its own
3543 * broken cleanup() signal handler.
3545 signal(SIGWINCH, tog_sigwinch);
3546 signal(SIGPIPE, tog_sigpipe);
3547 signal(SIGCONT, tog_sigcont);
3548 signal(SIGINT, tog_sigint);
3549 signal(SIGTERM, tog_sigterm);
3553 halfdelay(1); /* Do fast refresh while initial view is loading. */
3556 intrflush(stdscr, FALSE);
3557 keypad(stdscr, TRUE);
3559 if (getenv("TOG_COLORS") != NULL) {
3561 use_default_colors();
3565 static const struct got_error *
3566 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3567 struct got_repository *repo, struct got_worktree *worktree)
3569 const struct got_error *err = NULL;
3572 *in_repo_path = strdup("/");
3573 if (*in_repo_path == NULL)
3574 return got_error_from_errno("strdup");
3579 const char *prefix = got_worktree_get_path_prefix(worktree);
3582 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3585 if (asprintf(in_repo_path, "%s%s%s", prefix,
3586 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3588 err = got_error_from_errno("asprintf");
3589 *in_repo_path = NULL;
3593 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3598 static const struct got_error *
3599 cmd_log(int argc, char *argv[])
3601 const struct got_error *error;
3602 struct got_repository *repo = NULL;
3603 struct got_worktree *worktree = NULL;
3604 struct got_object_id *start_id = NULL;
3605 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3606 char *start_commit = NULL, *label = NULL;
3607 struct got_reference *ref = NULL;
3608 const char *head_ref_name = NULL;
3609 int ch, log_branches = 0;
3610 struct tog_view *view;
3611 int *pack_fds = NULL;
3613 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3619 start_commit = optarg;
3622 repo_path = realpath(optarg, NULL);
3623 if (repo_path == NULL)
3624 return got_error_from_errno2("realpath",
3639 error = got_repo_pack_fds_open(&pack_fds);
3643 if (repo_path == NULL) {
3644 cwd = getcwd(NULL, 0);
3646 return got_error_from_errno("getcwd");
3647 error = got_worktree_open(&worktree, cwd);
3648 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3652 strdup(got_worktree_get_repo_path(worktree));
3654 repo_path = strdup(cwd);
3655 if (repo_path == NULL) {
3656 error = got_error_from_errno("strdup");
3661 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3665 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3672 error = apply_unveil(got_repo_get_path(repo),
3673 worktree ? got_worktree_get_root_path(worktree) : NULL);
3677 /* already loaded by tog_log_with_path()? */
3678 if (TAILQ_EMPTY(&tog_refs)) {
3679 error = tog_load_refs(repo, 0);
3684 if (start_commit == NULL) {
3685 error = got_repo_match_object_id(&start_id, &label,
3686 worktree ? got_worktree_get_head_ref_name(worktree) :
3687 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3690 head_ref_name = label;
3692 error = got_ref_open(&ref, repo, start_commit, 0);
3694 head_ref_name = got_ref_get_name(ref);
3695 else if (error->code != GOT_ERR_NOT_REF)
3697 error = got_repo_match_object_id(&start_id, NULL,
3698 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3703 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3705 error = got_error_from_errno("view_open");
3708 error = open_log_view(view, start_id, repo, head_ref_name,
3709 in_repo_path, log_branches);
3713 /* Release work tree lock. */
3714 got_worktree_close(worktree);
3717 error = view_loop(view);
3727 const struct got_error *close_err = got_repo_close(repo);
3732 got_worktree_close(worktree);
3734 const struct got_error *pack_err =
3735 got_repo_pack_fds_close(pack_fds);
3747 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3748 "[-w] object1 object2\n", getprogname());
3753 match_line(const char *line, regex_t *regex, size_t nmatch,
3754 regmatch_t *regmatch)
3756 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3759 static struct tog_color *
3760 match_color(struct tog_colors *colors, const char *line)
3762 struct tog_color *tc = NULL;
3764 STAILQ_FOREACH(tc, colors, entry) {
3765 if (match_line(line, &tc->regex, 0, NULL))
3772 static const struct got_error *
3773 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3774 WINDOW *window, int skipcol, regmatch_t *regmatch)
3776 const struct got_error *err = NULL;
3778 wchar_t *wline = NULL;
3779 int rme, rms, n, width, scrollx;
3780 int width0 = 0, width1 = 0, width2 = 0;
3781 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3785 rms = regmatch->rm_so;
3786 rme = regmatch->rm_eo;
3788 err = expand_tab(&exstr, line);
3792 /* Split the line into 3 segments, according to match offsets. */
3793 seg0 = strndup(exstr, rms);
3795 err = got_error_from_errno("strndup");
3798 seg1 = strndup(exstr + rms, rme - rms);
3800 err = got_error_from_errno("strndup");
3803 seg2 = strdup(exstr + rme);
3805 err = got_error_from_errno("strndup");
3809 /* draw up to matched token if we haven't scrolled past it */
3810 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3814 n = MAX(width0 - skipcol, 0);
3817 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3818 wlimit, col_tab_align, 1);
3821 waddwstr(window, &wline[scrollx]);
3831 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3835 wlen = wcslen(wline);
3837 width = wcwidth(wline[i]);
3839 /* should not happen, tabs are expanded */
3840 err = got_error(GOT_ERR_RANGE);
3843 if (width0 + w + width > skipcol)
3848 /* draw (visible part of) matched token (if scrolled into it) */
3849 if (width1 - w > 0) {
3850 wattron(window, A_STANDOUT);
3851 waddwstr(window, &wline[i]);
3852 wattroff(window, A_STANDOUT);
3853 wlimit -= (width1 - w);
3854 *wtotal += (width1 - w);
3858 if (wlimit > 0) { /* draw rest of line */
3860 if (skipcol > width0 + width1) {
3861 err = format_line(&wline, &width2, &scrollx, seg2,
3862 skipcol - (width0 + width1), wlimit,
3866 waddwstr(window, &wline[scrollx]);
3868 err = format_line(&wline, &width2, NULL, seg2, 0,
3869 wlimit, col_tab_align, 1);
3872 waddwstr(window, wline);
3885 static const struct got_error *
3886 draw_file(struct tog_view *view, const char *header)
3888 struct tog_diff_view_state *s = &view->state.diff;
3889 regmatch_t *regmatch = &view->regmatch;
3890 const struct got_error *err;
3893 size_t linesize = 0;
3895 struct tog_color *tc;
3898 int max_lines = view->nlines;
3899 int nlines = s->nlines;
3902 line_offset = s->line_offsets[s->first_displayed_line - 1];
3903 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3904 return got_error_from_errno("fseek");
3906 werase(view->window);
3909 if (asprintf(&line, "[%d/%d] %s",
3910 s->first_displayed_line - 1 + s->selected_line, nlines,
3912 return got_error_from_errno("asprintf");
3913 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3919 if (view_needs_focus_indication(view))
3920 wstandout(view->window);
3921 waddwstr(view->window, wline);
3924 if (view_needs_focus_indication(view))
3925 wstandend(view->window);
3926 if (width <= view->ncols - 1)
3927 waddch(view->window, '\n');
3937 while (max_lines > 0 && nprinted < max_lines) {
3938 linelen = getline(&line, &linesize, s->f);
3939 if (linelen == -1) {
3945 return got_ferror(s->f, GOT_ERR_IO);
3948 /* Set view->maxx based on full line length. */
3949 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3955 view->maxx = MAX(view->maxx, width);
3959 tc = match_color(&s->colors, line);
3961 wattr_on(view->window,
3962 COLOR_PAIR(tc->colorpair), NULL);
3963 if (s->first_displayed_line + nprinted == s->matched_line &&
3964 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3965 err = add_matched_line(&width, line, view->ncols, 0,
3966 view->window, view->x, regmatch);
3973 err = format_line(&wline, &width, &skip, line,
3974 view->x, view->ncols, 0, view->x ? 1 : 0);
3979 waddwstr(view->window, &wline[skip]);
3984 wattr_off(view->window,
3985 COLOR_PAIR(tc->colorpair), NULL);
3986 if (width <= view->ncols - 1)
3987 waddch(view->window, '\n');
3992 s->last_displayed_line = s->first_displayed_line +
3995 s->last_displayed_line = s->first_displayed_line;
4000 while (nprinted < view->nlines) {
4001 waddch(view->window, '\n');
4005 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4011 wstandout(view->window);
4012 waddwstr(view->window, wline);
4015 wstandend(view->window);
4022 get_datestr(time_t *time, char *datebuf)
4024 struct tm mytm, *tm;
4027 tm = gmtime_r(time, &mytm);
4030 s = asctime_r(tm, datebuf);
4033 p = strchr(s, '\n');
4039 static const struct got_error *
4040 get_changed_paths(struct got_pathlist_head *paths,
4041 struct got_commit_object *commit, struct got_repository *repo)
4043 const struct got_error *err = NULL;
4044 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4045 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4046 struct got_object_qid *qid;
4048 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4050 struct got_commit_object *pcommit;
4051 err = got_object_open_as_commit(&pcommit, repo,
4056 tree_id1 = got_object_id_dup(
4057 got_object_commit_get_tree_id(pcommit));
4058 if (tree_id1 == NULL) {
4059 got_object_commit_close(pcommit);
4060 return got_error_from_errno("got_object_id_dup");
4062 got_object_commit_close(pcommit);
4067 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4072 tree_id2 = got_object_commit_get_tree_id(commit);
4073 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4077 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4078 got_diff_tree_collect_changed_paths, paths, 0);
4081 got_object_tree_close(tree1);
4083 got_object_tree_close(tree2);
4088 static const struct got_error *
4089 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4093 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4095 return got_error_from_errno("reallocarray");
4097 (*line_offsets)[*nlines] = off;
4102 static const struct got_error *
4103 write_commit_info(off_t **line_offsets, size_t *nlines,
4104 struct got_object_id *commit_id, struct got_reflist_head *refs,
4105 struct got_repository *repo, FILE *outfile)
4107 const struct got_error *err = NULL;
4108 char datebuf[26], *datestr;
4109 struct got_commit_object *commit;
4110 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4111 time_t committer_time;
4112 const char *author, *committer;
4113 char *refs_str = NULL;
4114 struct got_pathlist_head changed_paths;
4115 struct got_pathlist_entry *pe;
4119 TAILQ_INIT(&changed_paths);
4122 err = build_refs_str(&refs_str, refs, commit_id, repo);
4127 err = got_object_open_as_commit(&commit, repo, commit_id);
4131 err = got_object_id_str(&id_str, commit_id);
4133 err = got_error_from_errno("got_object_id_str");
4137 err = add_line_offset(line_offsets, nlines, 0);
4141 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4142 refs_str ? refs_str : "", refs_str ? ")" : "");
4144 err = got_error_from_errno("fprintf");
4148 err = add_line_offset(line_offsets, nlines, outoff);
4152 n = fprintf(outfile, "from: %s\n",
4153 got_object_commit_get_author(commit));
4155 err = got_error_from_errno("fprintf");
4159 err = add_line_offset(line_offsets, nlines, outoff);
4163 committer_time = got_object_commit_get_committer_time(commit);
4164 datestr = get_datestr(&committer_time, datebuf);
4166 n = fprintf(outfile, "date: %s UTC\n", datestr);
4168 err = got_error_from_errno("fprintf");
4172 err = add_line_offset(line_offsets, nlines, outoff);
4176 author = got_object_commit_get_author(commit);
4177 committer = got_object_commit_get_committer(commit);
4178 if (strcmp(author, committer) != 0) {
4179 n = fprintf(outfile, "via: %s\n", committer);
4181 err = got_error_from_errno("fprintf");
4185 err = add_line_offset(line_offsets, nlines, outoff);
4189 if (got_object_commit_get_nparents(commit) > 1) {
4190 const struct got_object_id_queue *parent_ids;
4191 struct got_object_qid *qid;
4193 parent_ids = got_object_commit_get_parent_ids(commit);
4194 STAILQ_FOREACH(qid, parent_ids, entry) {
4195 err = got_object_id_str(&id_str, &qid->id);
4198 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4200 err = got_error_from_errno("fprintf");
4204 err = add_line_offset(line_offsets, nlines, outoff);
4212 err = got_object_commit_get_logmsg(&logmsg, commit);
4216 while ((line = strsep(&s, "\n")) != NULL) {
4217 n = fprintf(outfile, "%s\n", line);
4219 err = got_error_from_errno("fprintf");
4223 err = add_line_offset(line_offsets, nlines, outoff);
4228 err = get_changed_paths(&changed_paths, commit, repo);
4231 TAILQ_FOREACH(pe, &changed_paths, entry) {
4232 struct got_diff_changed_path *cp = pe->data;
4233 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4235 err = got_error_from_errno("fprintf");
4239 err = add_line_offset(line_offsets, nlines, outoff);
4242 free((char *)pe->path);
4246 fputc('\n', outfile);
4248 err = add_line_offset(line_offsets, nlines, outoff);
4250 got_pathlist_free(&changed_paths);
4254 got_object_commit_close(commit);
4256 free(*line_offsets);
4257 *line_offsets = NULL;
4263 static const struct got_error *
4264 create_diff(struct tog_diff_view_state *s)
4266 const struct got_error *err = NULL;
4270 free(s->line_offsets);
4271 s->line_offsets = malloc(sizeof(off_t));
4272 if (s->line_offsets == NULL)
4273 return got_error_from_errno("malloc");
4278 err = got_error_from_errno("got_opentemp");
4281 if (s->f && fclose(s->f) == EOF) {
4282 err = got_error_from_errno("fclose");
4288 err = got_object_get_type(&obj_type, s->repo, s->id1);
4290 err = got_object_get_type(&obj_type, s->repo, s->id2);
4295 case GOT_OBJ_TYPE_BLOB:
4296 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4297 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4298 s->label1, s->label2, tog_diff_algo, s->diff_context,
4299 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4301 case GOT_OBJ_TYPE_TREE:
4302 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4303 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4304 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4305 s->force_text_diff, s->repo, s->f);
4307 case GOT_OBJ_TYPE_COMMIT: {
4308 const struct got_object_id_queue *parent_ids;
4309 struct got_object_qid *pid;
4310 struct got_commit_object *commit2;
4311 struct got_reflist_head *refs;
4313 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4316 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4317 /* Show commit info if we're diffing to a parent/root commit. */
4318 if (s->id1 == NULL) {
4319 err = write_commit_info(&s->line_offsets, &s->nlines,
4320 s->id2, refs, s->repo, s->f);
4324 parent_ids = got_object_commit_get_parent_ids(commit2);
4325 STAILQ_FOREACH(pid, parent_ids, entry) {
4326 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4327 err = write_commit_info(
4328 &s->line_offsets, &s->nlines,
4329 s->id2, refs, s->repo, s->f);
4336 got_object_commit_close(commit2);
4338 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4339 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4340 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4341 s->force_text_diff, s->repo, s->f);
4345 err = got_error(GOT_ERR_OBJ_TYPE);
4351 if (s->f && fflush(s->f) != 0 && err == NULL)
4352 err = got_error_from_errno("fflush");
4357 diff_view_indicate_progress(struct tog_view *view)
4359 mvwaddstr(view->window, 0, 0, "diffing...");
4364 static const struct got_error *
4365 search_start_diff_view(struct tog_view *view)
4367 struct tog_diff_view_state *s = &view->state.diff;
4369 s->matched_line = 0;
4373 static const struct got_error *
4374 search_next_diff_view(struct tog_view *view)
4376 struct tog_diff_view_state *s = &view->state.diff;
4377 const struct got_error *err = NULL;
4380 size_t linesize = 0;
4383 if (!view->searching) {
4384 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4388 if (s->matched_line) {
4389 if (view->searching == TOG_SEARCH_FORWARD)
4390 lineno = s->matched_line + 1;
4392 lineno = s->matched_line - 1;
4394 lineno = s->first_displayed_line;
4399 if (lineno <= 0 || lineno > s->nlines) {
4400 if (s->matched_line == 0) {
4401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 if (view->searching == TOG_SEARCH_FORWARD)
4411 offset = s->line_offsets[lineno - 1];
4412 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4414 return got_error_from_errno("fseeko");
4416 linelen = getline(&line, &linesize, s->f);
4417 if (linelen != -1) {
4419 err = expand_tab(&exstr, line);
4422 if (match_line(exstr, &view->regex, 1,
4424 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4425 s->matched_line = lineno;
4431 if (view->searching == TOG_SEARCH_FORWARD)
4438 if (s->matched_line) {
4439 s->first_displayed_line = s->matched_line;
4440 s->selected_line = 1;
4446 static const struct got_error *
4447 close_diff_view(struct tog_view *view)
4449 const struct got_error *err = NULL;
4450 struct tog_diff_view_state *s = &view->state.diff;
4456 if (s->f && fclose(s->f) == EOF)
4457 err = got_error_from_errno("fclose");
4459 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4460 err = got_error_from_errno("fclose");
4462 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4463 err = got_error_from_errno("fclose");
4465 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4466 err = got_error_from_errno("close");
4468 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4469 err = got_error_from_errno("close");
4471 free_colors(&s->colors);
4472 free(s->line_offsets);
4473 s->line_offsets = NULL;
4478 static const struct got_error *
4479 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4480 struct got_object_id *id2, const char *label1, const char *label2,
4481 int diff_context, int ignore_whitespace, int force_text_diff,
4482 struct tog_view *parent_view, struct got_repository *repo)
4484 const struct got_error *err;
4485 struct tog_diff_view_state *s = &view->state.diff;
4487 memset(s, 0, sizeof(*s));
4491 if (id1 != NULL && id2 != NULL) {
4493 err = got_object_get_type(&type1, repo, id1);
4496 err = got_object_get_type(&type2, repo, id2);
4501 return got_error(GOT_ERR_OBJ_TYPE);
4503 s->first_displayed_line = 1;
4504 s->last_displayed_line = view->nlines;
4505 s->selected_line = 1;
4513 s->id1 = got_object_id_dup(id1);
4515 return got_error_from_errno("got_object_id_dup");
4519 s->id2 = got_object_id_dup(id2);
4520 if (s->id2 == NULL) {
4521 err = got_error_from_errno("got_object_id_dup");
4525 s->f1 = got_opentemp();
4526 if (s->f1 == NULL) {
4527 err = got_error_from_errno("got_opentemp");
4531 s->f2 = got_opentemp();
4532 if (s->f2 == NULL) {
4533 err = got_error_from_errno("got_opentemp");
4537 s->fd1 = got_opentempfd();
4539 err = got_error_from_errno("got_opentempfd");
4543 s->fd2 = got_opentempfd();
4545 err = got_error_from_errno("got_opentempfd");
4549 s->first_displayed_line = 1;
4550 s->last_displayed_line = view->nlines;
4551 s->diff_context = diff_context;
4552 s->ignore_whitespace = ignore_whitespace;
4553 s->force_text_diff = force_text_diff;
4554 s->parent_view = parent_view;
4557 STAILQ_INIT(&s->colors);
4558 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4559 err = add_color(&s->colors,
4560 "^-", TOG_COLOR_DIFF_MINUS,
4561 get_color_value("TOG_COLOR_DIFF_MINUS"));
4564 err = add_color(&s->colors, "^\\+",
4565 TOG_COLOR_DIFF_PLUS,
4566 get_color_value("TOG_COLOR_DIFF_PLUS"));
4569 err = add_color(&s->colors,
4570 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4571 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4575 err = add_color(&s->colors,
4576 "^(commit [0-9a-f]|parent [0-9]|"
4577 "(blob|file|tree|commit) [-+] |"
4578 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4579 get_color_value("TOG_COLOR_DIFF_META"));
4583 err = add_color(&s->colors,
4584 "^(from|via): ", TOG_COLOR_AUTHOR,
4585 get_color_value("TOG_COLOR_AUTHOR"));
4589 err = add_color(&s->colors,
4590 "^date: ", TOG_COLOR_DATE,
4591 get_color_value("TOG_COLOR_DATE"));
4596 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4597 view_is_splitscreen(view))
4598 show_log_view(parent_view); /* draw border */
4599 diff_view_indicate_progress(view);
4601 err = create_diff(s);
4603 view->show = show_diff_view;
4604 view->input = input_diff_view;
4605 view->reset = reset_diff_view;
4606 view->close = close_diff_view;
4607 view->search_start = search_start_diff_view;
4608 view->search_next = search_next_diff_view;
4611 close_diff_view(view);
4615 static const struct got_error *
4616 show_diff_view(struct tog_view *view)
4618 const struct got_error *err;
4619 struct tog_diff_view_state *s = &view->state.diff;
4620 char *id_str1 = NULL, *id_str2, *header;
4621 const char *label1, *label2;
4624 err = got_object_id_str(&id_str1, s->id1);
4627 label1 = s->label1 ? : id_str1;
4629 label1 = "/dev/null";
4631 err = got_object_id_str(&id_str2, s->id2);
4634 label2 = s->label2 ? : id_str2;
4636 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4637 err = got_error_from_errno("asprintf");
4645 err = draw_file(view, header);
4650 static const struct got_error *
4651 set_selected_commit(struct tog_diff_view_state *s,
4652 struct commit_queue_entry *entry)
4654 const struct got_error *err;
4655 const struct got_object_id_queue *parent_ids;
4656 struct got_commit_object *selected_commit;
4657 struct got_object_qid *pid;
4660 s->id2 = got_object_id_dup(entry->id);
4662 return got_error_from_errno("got_object_id_dup");
4664 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4667 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4669 pid = STAILQ_FIRST(parent_ids);
4670 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4671 got_object_commit_close(selected_commit);
4675 static const struct got_error *
4676 reset_diff_view(struct tog_view *view)
4678 struct tog_diff_view_state *s = &view->state.diff;
4681 wclear(view->window);
4682 s->first_displayed_line = 1;
4683 s->last_displayed_line = view->nlines;
4684 s->matched_line = 0;
4685 diff_view_indicate_progress(view);
4686 return create_diff(s);
4689 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4691 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4694 static const struct got_error *
4695 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4697 const struct got_error *err = NULL;
4698 struct tog_diff_view_state *s = &view->state.diff;
4699 struct tog_log_view_state *ls;
4700 struct commit_queue_entry *old_selected_entry;
4702 size_t linesize = 0;
4704 int i, nscroll = view->nlines - 1, up = 0;
4711 view->x = MAX(view->maxx - view->ncols / 3, 0);
4716 if (view->x + view->ncols / 3 < view->maxx)
4717 view->x += 2; /* move two columns right */
4723 view->x -= MIN(view->x, 2); /* move two columns back */
4730 s->force_text_diff = !s->force_text_diff;
4732 s->ignore_whitespace = !s->ignore_whitespace;
4733 err = reset_diff_view(view);
4737 s->first_displayed_line = 1;
4746 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4752 if (s->first_displayed_line > 1)
4753 s->first_displayed_line--;
4764 if (s->first_displayed_line == 1) {
4769 while (i++ < nscroll && s->first_displayed_line > 1)
4770 s->first_displayed_line--;
4776 s->first_displayed_line++;
4793 while (!s->eof && i++ < nscroll) {
4794 linelen = getline(&line, &linesize, s->f);
4795 s->first_displayed_line++;
4796 if (linelen == -1) {
4800 err = got_ferror(s->f, GOT_ERR_IO);
4807 if (s->diff_context > 0) {
4809 s->matched_line = 0;
4810 diff_view_indicate_progress(view);
4811 err = create_diff(s);
4812 if (s->first_displayed_line + view->nlines - 1 >
4814 s->first_displayed_line = 1;
4815 s->last_displayed_line = view->nlines;
4821 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4823 s->matched_line = 0;
4824 diff_view_indicate_progress(view);
4825 err = create_diff(s);
4837 if (s->parent_view == NULL) {
4841 s->parent_view->count = view->count;
4843 if (s->parent_view->type == TOG_VIEW_LOG) {
4844 ls = &s->parent_view->state.log;
4845 old_selected_entry = ls->selected_entry;
4847 err = input_log_view(NULL, s->parent_view,
4848 up ? KEY_UP : KEY_DOWN);
4851 view->count = s->parent_view->count;
4853 if (old_selected_entry == ls->selected_entry)
4856 err = set_selected_commit(s, ls->selected_entry);
4859 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4860 struct tog_blame_view_state *bs;
4861 struct got_object_id *id, *prev_id;
4863 bs = &s->parent_view->state.blame;
4864 prev_id = get_annotation_for_line(bs->blame.lines,
4865 bs->blame.nlines, bs->last_diffed_line);
4867 err = input_blame_view(&view, s->parent_view,
4868 up ? KEY_UP : KEY_DOWN);
4871 view->count = s->parent_view->count;
4873 if (prev_id == NULL)
4875 id = get_selected_commit_id(bs->blame.lines,
4876 bs->blame.nlines, bs->first_displayed_line,
4881 if (!got_object_id_cmp(prev_id, id))
4884 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4888 s->first_displayed_line = 1;
4889 s->last_displayed_line = view->nlines;
4890 s->matched_line = 0;
4893 diff_view_indicate_progress(view);
4894 err = create_diff(s);
4904 static const struct got_error *
4905 cmd_diff(int argc, char *argv[])
4907 const struct got_error *error = NULL;
4908 struct got_repository *repo = NULL;
4909 struct got_worktree *worktree = NULL;
4910 struct got_object_id *id1 = NULL, *id2 = NULL;
4911 char *repo_path = NULL, *cwd = NULL;
4912 char *id_str1 = NULL, *id_str2 = NULL;
4913 char *label1 = NULL, *label2 = NULL;
4914 int diff_context = 3, ignore_whitespace = 0;
4915 int ch, force_text_diff = 0;
4917 struct tog_view *view;
4918 int *pack_fds = NULL;
4920 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4923 force_text_diff = 1;
4926 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4929 errx(1, "number of context lines is %s: %s",
4933 repo_path = realpath(optarg, NULL);
4934 if (repo_path == NULL)
4935 return got_error_from_errno2("realpath",
4937 got_path_strip_trailing_slashes(repo_path);
4940 ignore_whitespace = 1;
4952 usage_diff(); /* TODO show local worktree changes */
4953 } else if (argc == 2) {
4959 error = got_repo_pack_fds_open(&pack_fds);
4963 if (repo_path == NULL) {
4964 cwd = getcwd(NULL, 0);
4966 return got_error_from_errno("getcwd");
4967 error = got_worktree_open(&worktree, cwd);
4968 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4972 strdup(got_worktree_get_repo_path(worktree));
4974 repo_path = strdup(cwd);
4975 if (repo_path == NULL) {
4976 error = got_error_from_errno("strdup");
4981 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4987 error = apply_unveil(got_repo_get_path(repo), NULL);
4991 error = tog_load_refs(repo, 0);
4995 error = got_repo_match_object_id(&id1, &label1, id_str1,
4996 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5000 error = got_repo_match_object_id(&id2, &label2, id_str2,
5001 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5005 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5007 error = got_error_from_errno("view_open");
5010 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5011 ignore_whitespace, force_text_diff, NULL, repo);
5014 error = view_loop(view);
5021 const struct got_error *close_err = got_repo_close(repo);
5026 got_worktree_close(worktree);
5028 const struct got_error *pack_err =
5029 got_repo_pack_fds_close(pack_fds);
5042 "usage: %s blame [-c commit] [-r repository-path] path\n",
5047 struct tog_blame_line {
5049 struct got_object_id *id;
5052 static const struct got_error *
5053 draw_blame(struct tog_view *view)
5055 struct tog_blame_view_state *s = &view->state.blame;
5056 struct tog_blame *blame = &s->blame;
5057 regmatch_t *regmatch = &view->regmatch;
5058 const struct got_error *err;
5059 int lineno = 0, nprinted = 0;
5061 size_t linesize = 0;
5065 struct tog_blame_line *blame_line;
5066 struct got_object_id *prev_id = NULL;
5068 struct tog_color *tc;
5070 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5075 werase(view->window);
5077 if (asprintf(&line, "commit %s", id_str) == -1) {
5078 err = got_error_from_errno("asprintf");
5083 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5088 if (view_needs_focus_indication(view))
5089 wstandout(view->window);
5090 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5092 wattr_on(view->window,
5093 COLOR_PAIR(tc->colorpair), NULL);
5094 waddwstr(view->window, wline);
5096 wattr_off(view->window,
5097 COLOR_PAIR(tc->colorpair), NULL);
5098 if (view_needs_focus_indication(view))
5099 wstandend(view->window);
5102 if (width < view->ncols - 1)
5103 waddch(view->window, '\n');
5105 if (asprintf(&line, "[%d/%d] %s%s",
5106 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5107 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5109 return got_error_from_errno("asprintf");
5112 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5117 waddwstr(view->window, wline);
5120 if (width < view->ncols - 1)
5121 waddch(view->window, '\n');
5125 while (nprinted < view->nlines - 2) {
5126 linelen = getline(&line, &linesize, blame->f);
5127 if (linelen == -1) {
5128 if (feof(blame->f)) {
5133 return got_ferror(blame->f, GOT_ERR_IO);
5135 if (++lineno < s->first_displayed_line)
5138 /* Set view->maxx based on full line length. */
5139 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5146 view->maxx = MAX(view->maxx, width);
5148 if (nprinted == s->selected_line - 1)
5149 wstandout(view->window);
5151 if (blame->nlines > 0) {
5152 blame_line = &blame->lines[lineno - 1];
5153 if (blame_line->annotated && prev_id &&
5154 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5155 !(nprinted == s->selected_line - 1)) {
5156 waddstr(view->window, " ");
5157 } else if (blame_line->annotated) {
5159 err = got_object_id_str(&id_str,
5165 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5167 wattr_on(view->window,
5168 COLOR_PAIR(tc->colorpair), NULL);
5169 wprintw(view->window, "%.8s", id_str);
5171 wattr_off(view->window,
5172 COLOR_PAIR(tc->colorpair), NULL);
5174 prev_id = blame_line->id;
5176 waddstr(view->window, "........");
5180 waddstr(view->window, "........");
5184 if (nprinted == s->selected_line - 1)
5185 wstandend(view->window);
5186 waddstr(view->window, " ");
5188 if (view->ncols <= 9) {
5190 } else if (s->first_displayed_line + nprinted ==
5192 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5193 err = add_matched_line(&width, line, view->ncols - 9, 9,
5194 view->window, view->x, regmatch);
5202 err = format_line(&wline, &width, &skip, line,
5203 view->x, view->ncols - 9, 9, 1);
5208 waddwstr(view->window, &wline[skip]);
5214 if (width <= view->ncols - 1)
5215 waddch(view->window, '\n');
5216 if (++nprinted == 1)
5217 s->first_displayed_line = lineno;
5220 s->last_displayed_line = lineno;
5227 static const struct got_error *
5228 blame_cb(void *arg, int nlines, int lineno,
5229 struct got_commit_object *commit, struct got_object_id *id)
5231 const struct got_error *err = NULL;
5232 struct tog_blame_cb_args *a = arg;
5233 struct tog_blame_line *line;
5236 if (nlines != a->nlines ||
5237 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5238 return got_error(GOT_ERR_RANGE);
5240 errcode = pthread_mutex_lock(&tog_mutex);
5242 return got_error_set_errno(errcode, "pthread_mutex_lock");
5244 if (*a->quit) { /* user has quit the blame view */
5245 err = got_error(GOT_ERR_ITER_COMPLETED);
5250 goto done; /* no change in this commit */
5252 line = &a->lines[lineno - 1];
5253 if (line->annotated)
5256 line->id = got_object_id_dup(id);
5257 if (line->id == NULL) {
5258 err = got_error_from_errno("got_object_id_dup");
5261 line->annotated = 1;
5263 errcode = pthread_mutex_unlock(&tog_mutex);
5265 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5270 blame_thread(void *arg)
5272 const struct got_error *err, *close_err;
5273 struct tog_blame_thread_args *ta = arg;
5274 struct tog_blame_cb_args *a = ta->cb_args;
5275 int errcode, fd1 = -1, fd2 = -1;
5276 FILE *f1 = NULL, *f2 = NULL;
5278 fd1 = got_opentempfd();
5280 return (void *)got_error_from_errno("got_opentempfd");
5282 fd2 = got_opentempfd();
5284 err = got_error_from_errno("got_opentempfd");
5288 f1 = got_opentemp();
5290 err = (void *)got_error_from_errno("got_opentemp");
5293 f2 = got_opentemp();
5295 err = (void *)got_error_from_errno("got_opentemp");
5299 err = block_signals_used_by_main_thread();
5303 err = got_blame(ta->path, a->commit_id, ta->repo,
5304 tog_diff_algo, blame_cb, ta->cb_args,
5305 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5306 if (err && err->code == GOT_ERR_CANCELLED)
5309 errcode = pthread_mutex_lock(&tog_mutex);
5311 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5315 close_err = got_repo_close(ta->repo);
5321 errcode = pthread_mutex_unlock(&tog_mutex);
5322 if (errcode && err == NULL)
5323 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5326 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5327 err = got_error_from_errno("close");
5328 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5329 err = got_error_from_errno("close");
5330 if (f1 && fclose(f1) == EOF && err == NULL)
5331 err = got_error_from_errno("fclose");
5332 if (f2 && fclose(f2) == EOF && err == NULL)
5333 err = got_error_from_errno("fclose");
5338 static struct got_object_id *
5339 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5340 int first_displayed_line, int selected_line)
5342 struct tog_blame_line *line;
5347 line = &lines[first_displayed_line - 1 + selected_line - 1];
5348 if (!line->annotated)
5354 static struct got_object_id *
5355 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5358 struct tog_blame_line *line;
5360 if (nlines <= 0 || lineno >= nlines)
5363 line = &lines[lineno - 1];
5364 if (!line->annotated)
5370 static const struct got_error *
5371 stop_blame(struct tog_blame *blame)
5373 const struct got_error *err = NULL;
5376 if (blame->thread) {
5378 errcode = pthread_mutex_unlock(&tog_mutex);
5380 return got_error_set_errno(errcode,
5381 "pthread_mutex_unlock");
5382 errcode = pthread_join(blame->thread, (void **)&err);
5384 return got_error_set_errno(errcode, "pthread_join");
5385 errcode = pthread_mutex_lock(&tog_mutex);
5387 return got_error_set_errno(errcode,
5388 "pthread_mutex_lock");
5389 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5391 blame->thread = NULL;
5393 if (blame->thread_args.repo) {
5394 const struct got_error *close_err;
5395 close_err = got_repo_close(blame->thread_args.repo);
5398 blame->thread_args.repo = NULL;
5401 if (fclose(blame->f) == EOF && err == NULL)
5402 err = got_error_from_errno("fclose");
5406 for (i = 0; i < blame->nlines; i++)
5407 free(blame->lines[i].id);
5409 blame->lines = NULL;
5411 free(blame->cb_args.commit_id);
5412 blame->cb_args.commit_id = NULL;
5413 if (blame->pack_fds) {
5414 const struct got_error *pack_err =
5415 got_repo_pack_fds_close(blame->pack_fds);
5418 blame->pack_fds = NULL;
5423 static const struct got_error *
5424 cancel_blame_view(void *arg)
5426 const struct got_error *err = NULL;
5430 errcode = pthread_mutex_lock(&tog_mutex);
5432 return got_error_set_errno(errcode,
5433 "pthread_mutex_unlock");
5436 err = got_error(GOT_ERR_CANCELLED);
5438 errcode = pthread_mutex_unlock(&tog_mutex);
5440 return got_error_set_errno(errcode,
5441 "pthread_mutex_lock");
5446 static const struct got_error *
5447 run_blame(struct tog_view *view)
5449 struct tog_blame_view_state *s = &view->state.blame;
5450 struct tog_blame *blame = &s->blame;
5451 const struct got_error *err = NULL;
5452 struct got_commit_object *commit = NULL;
5453 struct got_blob_object *blob = NULL;
5454 struct got_repository *thread_repo = NULL;
5455 struct got_object_id *obj_id = NULL;
5456 int obj_type, fd = -1;
5457 int *pack_fds = NULL;
5459 err = got_object_open_as_commit(&commit, s->repo,
5460 &s->blamed_commit->id);
5464 fd = got_opentempfd();
5466 err = got_error_from_errno("got_opentempfd");
5470 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5474 err = got_object_get_type(&obj_type, s->repo, obj_id);
5478 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5479 err = got_error(GOT_ERR_OBJ_TYPE);
5483 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5486 blame->f = got_opentemp();
5487 if (blame->f == NULL) {
5488 err = got_error_from_errno("got_opentemp");
5491 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5492 &blame->line_offsets, blame->f, blob);
5495 if (blame->nlines == 0) {
5496 s->blame_complete = 1;
5500 /* Don't include \n at EOF in the blame line count. */
5501 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5504 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5505 if (blame->lines == NULL) {
5506 err = got_error_from_errno("calloc");
5510 err = got_repo_pack_fds_open(&pack_fds);
5513 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5518 blame->pack_fds = pack_fds;
5519 blame->cb_args.view = view;
5520 blame->cb_args.lines = blame->lines;
5521 blame->cb_args.nlines = blame->nlines;
5522 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5523 if (blame->cb_args.commit_id == NULL) {
5524 err = got_error_from_errno("got_object_id_dup");
5527 blame->cb_args.quit = &s->done;
5529 blame->thread_args.path = s->path;
5530 blame->thread_args.repo = thread_repo;
5531 blame->thread_args.cb_args = &blame->cb_args;
5532 blame->thread_args.complete = &s->blame_complete;
5533 blame->thread_args.cancel_cb = cancel_blame_view;
5534 blame->thread_args.cancel_arg = &s->done;
5535 s->blame_complete = 0;
5537 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5538 s->first_displayed_line = 1;
5539 s->last_displayed_line = view->nlines;
5540 s->selected_line = 1;
5542 s->matched_line = 0;
5546 got_object_commit_close(commit);
5547 if (fd != -1 && close(fd) == -1 && err == NULL)
5548 err = got_error_from_errno("close");
5550 got_object_blob_close(blob);
5557 static const struct got_error *
5558 open_blame_view(struct tog_view *view, char *path,
5559 struct got_object_id *commit_id, struct got_repository *repo)
5561 const struct got_error *err = NULL;
5562 struct tog_blame_view_state *s = &view->state.blame;
5564 STAILQ_INIT(&s->blamed_commits);
5566 s->path = strdup(path);
5567 if (s->path == NULL)
5568 return got_error_from_errno("strdup");
5570 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5576 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5577 s->first_displayed_line = 1;
5578 s->last_displayed_line = view->nlines;
5579 s->selected_line = 1;
5580 s->blame_complete = 0;
5582 s->commit_id = commit_id;
5583 memset(&s->blame, 0, sizeof(s->blame));
5585 STAILQ_INIT(&s->colors);
5586 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5587 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5588 get_color_value("TOG_COLOR_COMMIT"));
5593 view->show = show_blame_view;
5594 view->input = input_blame_view;
5595 view->reset = reset_blame_view;
5596 view->close = close_blame_view;
5597 view->search_start = search_start_blame_view;
5598 view->search_next = search_next_blame_view;
5600 return run_blame(view);
5603 static const struct got_error *
5604 close_blame_view(struct tog_view *view)
5606 const struct got_error *err = NULL;
5607 struct tog_blame_view_state *s = &view->state.blame;
5609 if (s->blame.thread)
5610 err = stop_blame(&s->blame);
5612 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5613 struct got_object_qid *blamed_commit;
5614 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5615 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5616 got_object_qid_free(blamed_commit);
5620 free_colors(&s->colors);
5624 static const struct got_error *
5625 search_start_blame_view(struct tog_view *view)
5627 struct tog_blame_view_state *s = &view->state.blame;
5629 s->matched_line = 0;
5633 static const struct got_error *
5634 search_next_blame_view(struct tog_view *view)
5636 struct tog_blame_view_state *s = &view->state.blame;
5637 const struct got_error *err = NULL;
5640 size_t linesize = 0;
5643 if (!view->searching) {
5644 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5648 if (s->matched_line) {
5649 if (view->searching == TOG_SEARCH_FORWARD)
5650 lineno = s->matched_line + 1;
5652 lineno = s->matched_line - 1;
5654 lineno = s->first_displayed_line - 1 + s->selected_line;
5659 if (lineno <= 0 || lineno > s->blame.nlines) {
5660 if (s->matched_line == 0) {
5661 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5665 if (view->searching == TOG_SEARCH_FORWARD)
5668 lineno = s->blame.nlines;
5671 offset = s->blame.line_offsets[lineno - 1];
5672 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5674 return got_error_from_errno("fseeko");
5676 linelen = getline(&line, &linesize, s->blame.f);
5677 if (linelen != -1) {
5679 err = expand_tab(&exstr, line);
5682 if (match_line(exstr, &view->regex, 1,
5684 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5685 s->matched_line = lineno;
5691 if (view->searching == TOG_SEARCH_FORWARD)
5698 if (s->matched_line) {
5699 s->first_displayed_line = s->matched_line;
5700 s->selected_line = 1;
5706 static const struct got_error *
5707 show_blame_view(struct tog_view *view)
5709 const struct got_error *err = NULL;
5710 struct tog_blame_view_state *s = &view->state.blame;
5713 if (s->blame.thread == NULL && !s->blame_complete) {
5714 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5715 &s->blame.thread_args);
5717 return got_error_set_errno(errcode, "pthread_create");
5719 halfdelay(1); /* fast refresh while annotating */
5722 if (s->blame_complete)
5723 halfdelay(10); /* disable fast refresh */
5725 err = draw_blame(view);
5731 static const struct got_error *
5732 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5733 struct got_repository *repo, struct got_object_id *id)
5735 struct tog_view *log_view;
5736 const struct got_error *err = NULL;
5740 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5741 if (log_view == NULL)
5742 return got_error_from_errno("view_open");
5744 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5746 view_close(log_view);
5748 *new_view = log_view;
5753 static const struct got_error *
5754 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5756 const struct got_error *err = NULL, *thread_err = NULL;
5757 struct tog_view *diff_view, *log_view;
5758 struct tog_blame_view_state *s = &view->state.blame;
5759 int eos, nscroll, begin_y = 0, begin_x = 0;
5761 eos = nscroll = view->nlines - 2;
5762 if (view_is_hsplit_top(view))
5770 view->x = MAX(view->maxx - view->ncols / 3, 0);
5775 if (view->x + view->ncols / 3 < view->maxx)
5776 view->x += 2; /* move two columns right */
5782 view->x -= MIN(view->x, 2); /* move two columns back */
5791 s->selected_line = 1;
5792 s->first_displayed_line = 1;
5797 if (s->blame.nlines < eos) {
5798 s->selected_line = s->blame.nlines;
5799 s->first_displayed_line = 1;
5801 s->selected_line = eos;
5802 s->first_displayed_line = s->blame.nlines - (eos - 1);
5809 if (s->selected_line > 1)
5811 else if (s->selected_line == 1 &&
5812 s->first_displayed_line > 1)
5813 s->first_displayed_line--;
5824 if (s->first_displayed_line == 1) {
5825 if (view->count > 1)
5827 s->selected_line = MAX(1, s->selected_line - nscroll);
5831 if (s->first_displayed_line > nscroll)
5832 s->first_displayed_line -= nscroll;
5834 s->first_displayed_line = 1;
5839 if (s->selected_line < eos && s->first_displayed_line +
5840 s->selected_line <= s->blame.nlines)
5842 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5843 s->first_displayed_line++;
5849 struct got_object_id *id = NULL;
5852 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5853 s->first_displayed_line, s->selected_line);
5857 struct got_commit_object *commit, *pcommit;
5858 struct got_object_qid *pid;
5859 struct got_object_id *blob_id = NULL;
5861 err = got_object_open_as_commit(&commit,
5866 got_object_commit_get_parent_ids(commit));
5868 got_object_commit_close(commit);
5871 /* Check if path history ends here. */
5872 err = got_object_open_as_commit(&pcommit,
5876 err = got_object_id_by_path(&blob_id, s->repo,
5878 got_object_commit_close(pcommit);
5880 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5882 got_object_commit_close(commit);
5885 err = got_object_get_type(&obj_type, s->repo,
5888 /* Can't blame non-blob type objects. */
5889 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5890 got_object_commit_close(commit);
5893 err = got_object_qid_alloc(&s->blamed_commit,
5895 got_object_commit_close(commit);
5897 if (got_object_id_cmp(id,
5898 &s->blamed_commit->id) == 0)
5900 err = got_object_qid_alloc(&s->blamed_commit,
5906 thread_err = stop_blame(&s->blame);
5910 STAILQ_INSERT_HEAD(&s->blamed_commits,
5911 s->blamed_commit, entry);
5912 err = run_blame(view);
5918 struct got_object_qid *first;
5921 first = STAILQ_FIRST(&s->blamed_commits);
5922 if (!got_object_id_cmp(&first->id, s->commit_id))
5925 thread_err = stop_blame(&s->blame);
5929 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5930 got_object_qid_free(s->blamed_commit);
5932 STAILQ_FIRST(&s->blamed_commits);
5933 err = run_blame(view);
5939 struct got_object_id *id = NULL;
5942 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5943 s->first_displayed_line, s->selected_line);
5947 if (view_is_parent_view(view))
5948 view_get_split(view, &begin_y, &begin_x);
5949 err = log_annotated_line(&log_view, begin_y, begin_x,
5953 if (view_is_parent_view(view) &&
5954 view->mode == TOG_VIEW_SPLIT_HRZN) {
5955 err = view_init_hsplit(view, begin_y);
5961 log_view->focussed = 1;
5962 log_view->mode = view->mode;
5963 log_view->nlines = view->lines - begin_y;
5964 if (view_is_parent_view(view)) {
5965 view_transfer_size(log_view, view);
5966 err = view_close_child(view);
5969 err = view_set_child(view, log_view);
5972 view->focus_child = 1;
5974 *new_view = log_view;
5979 struct got_object_id *id = NULL;
5980 struct got_object_qid *pid;
5981 struct got_commit_object *commit = NULL;
5984 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5985 s->first_displayed_line, s->selected_line);
5988 err = got_object_open_as_commit(&commit, s->repo, id);
5991 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5993 /* traversed from diff view, release diff resources */
5994 err = close_diff_view(*new_view);
5997 diff_view = *new_view;
5999 if (view_is_parent_view(view))
6000 view_get_split(view, &begin_y, &begin_x);
6002 diff_view = view_open(0, 0, begin_y, begin_x,
6004 if (diff_view == NULL) {
6005 got_object_commit_close(commit);
6006 err = got_error_from_errno("view_open");
6010 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6011 id, NULL, NULL, 3, 0, 0, view, s->repo);
6012 got_object_commit_close(commit);
6014 view_close(diff_view);
6017 s->last_diffed_line = s->first_displayed_line - 1 +
6020 break; /* still open from active diff view */
6021 if (view_is_parent_view(view) &&
6022 view->mode == TOG_VIEW_SPLIT_HRZN) {
6023 err = view_init_hsplit(view, begin_y);
6029 diff_view->focussed = 1;
6030 diff_view->mode = view->mode;
6031 diff_view->nlines = view->lines - begin_y;
6032 if (view_is_parent_view(view)) {
6033 view_transfer_size(diff_view, view);
6034 err = view_close_child(view);
6037 err = view_set_child(view, diff_view);
6040 view->focus_child = 1;
6042 *new_view = diff_view;
6055 if (s->last_displayed_line >= s->blame.nlines &&
6056 s->selected_line >= MIN(s->blame.nlines,
6057 view->nlines - 2)) {
6061 if (s->last_displayed_line >= s->blame.nlines &&
6062 s->selected_line < view->nlines - 2) {
6064 MIN(nscroll, s->last_displayed_line -
6065 s->first_displayed_line - s->selected_line + 1);
6067 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6068 s->first_displayed_line += nscroll;
6070 s->first_displayed_line =
6071 s->blame.nlines - (view->nlines - 3);
6074 if (s->selected_line > view->nlines - 2) {
6075 s->selected_line = MIN(s->blame.nlines,
6083 return thread_err ? thread_err : err;
6086 static const struct got_error *
6087 reset_blame_view(struct tog_view *view)
6089 const struct got_error *err;
6090 struct tog_blame_view_state *s = &view->state.blame;
6094 err = stop_blame(&s->blame);
6098 return run_blame(view);
6101 static const struct got_error *
6102 cmd_blame(int argc, char *argv[])
6104 const struct got_error *error;
6105 struct got_repository *repo = NULL;
6106 struct got_worktree *worktree = NULL;
6107 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6108 char *link_target = NULL;
6109 struct got_object_id *commit_id = NULL;
6110 struct got_commit_object *commit = NULL;
6111 char *commit_id_str = NULL;
6113 struct tog_view *view;
6114 int *pack_fds = NULL;
6116 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6119 commit_id_str = optarg;
6122 repo_path = realpath(optarg, NULL);
6123 if (repo_path == NULL)
6124 return got_error_from_errno2("realpath",
6139 error = got_repo_pack_fds_open(&pack_fds);
6143 if (repo_path == NULL) {
6144 cwd = getcwd(NULL, 0);
6146 return got_error_from_errno("getcwd");
6147 error = got_worktree_open(&worktree, cwd);
6148 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6152 strdup(got_worktree_get_repo_path(worktree));
6154 repo_path = strdup(cwd);
6155 if (repo_path == NULL) {
6156 error = got_error_from_errno("strdup");
6161 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6165 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6172 error = apply_unveil(got_repo_get_path(repo), NULL);
6176 error = tog_load_refs(repo, 0);
6180 if (commit_id_str == NULL) {
6181 struct got_reference *head_ref;
6182 error = got_ref_open(&head_ref, repo, worktree ?
6183 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6186 error = got_ref_resolve(&commit_id, repo, head_ref);
6187 got_ref_close(head_ref);
6189 error = got_repo_match_object_id(&commit_id, NULL,
6190 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6195 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6197 error = got_error_from_errno("view_open");
6201 error = got_object_open_as_commit(&commit, repo, commit_id);
6205 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6210 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6215 /* Release work tree lock. */
6216 got_worktree_close(worktree);
6219 error = view_loop(view);
6227 got_object_commit_close(commit);
6229 got_worktree_close(worktree);
6231 const struct got_error *close_err = got_repo_close(repo);
6236 const struct got_error *pack_err =
6237 got_repo_pack_fds_close(pack_fds);
6245 static const struct got_error *
6246 draw_tree_entries(struct tog_view *view, const char *parent_path)
6248 struct tog_tree_view_state *s = &view->state.tree;
6249 const struct got_error *err = NULL;
6250 struct got_tree_entry *te;
6252 struct tog_color *tc;
6253 int width, n, i, nentries;
6254 int limit = view->nlines;
6257 if (view_is_hsplit_top(view))
6258 --limit; /* border */
6260 werase(view->window);
6265 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6269 if (view_needs_focus_indication(view))
6270 wstandout(view->window);
6271 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6273 wattr_on(view->window,
6274 COLOR_PAIR(tc->colorpair), NULL);
6275 waddwstr(view->window, wline);
6277 wattr_off(view->window,
6278 COLOR_PAIR(tc->colorpair), NULL);
6279 if (view_needs_focus_indication(view))
6280 wstandend(view->window);
6283 if (width < view->ncols - 1)
6284 waddch(view->window, '\n');
6287 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6291 waddwstr(view->window, wline);
6294 if (width < view->ncols - 1)
6295 waddch(view->window, '\n');
6298 waddch(view->window, '\n');
6302 if (s->first_displayed_entry == NULL) {
6303 te = got_object_tree_get_first_entry(s->tree);
6304 if (s->selected == 0) {
6306 wstandout(view->window);
6307 s->selected_entry = NULL;
6309 waddstr(view->window, " ..\n"); /* parent directory */
6310 if (s->selected == 0 && view->focussed)
6311 wstandend(view->window);
6318 te = s->first_displayed_entry;
6321 nentries = got_object_tree_get_nentries(s->tree);
6322 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6323 char *line = NULL, *id_str = NULL, *link_target = NULL;
6324 const char *modestr = "";
6327 te = got_object_tree_get_entry(s->tree, i);
6328 mode = got_tree_entry_get_mode(te);
6331 err = got_object_id_str(&id_str,
6332 got_tree_entry_get_id(te));
6334 return got_error_from_errno(
6335 "got_object_id_str");
6337 if (got_object_tree_entry_is_submodule(te))
6339 else if (S_ISLNK(mode)) {
6342 err = got_tree_entry_get_symlink_target(&link_target,
6348 for (i = 0; i < strlen(link_target); i++) {
6349 if (!isprint((unsigned char)link_target[i]))
6350 link_target[i] = '?';
6354 else if (S_ISDIR(mode))
6356 else if (mode & S_IXUSR)
6358 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6359 got_tree_entry_get_name(te), modestr,
6360 link_target ? " -> ": "",
6361 link_target ? link_target : "") == -1) {
6364 return got_error_from_errno("asprintf");
6368 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6374 if (n == s->selected) {
6376 wstandout(view->window);
6377 s->selected_entry = te;
6379 tc = match_color(&s->colors, line);
6381 wattr_on(view->window,
6382 COLOR_PAIR(tc->colorpair), NULL);
6383 waddwstr(view->window, wline);
6385 wattr_off(view->window,
6386 COLOR_PAIR(tc->colorpair), NULL);
6387 if (width < view->ncols - 1)
6388 waddch(view->window, '\n');
6389 if (n == s->selected && view->focussed)
6390 wstandend(view->window);
6396 s->last_displayed_entry = te;
6405 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6407 struct got_tree_entry *te;
6408 int isroot = s->tree == s->root;
6411 if (s->first_displayed_entry == NULL)
6414 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6415 while (i++ < maxscroll) {
6418 s->first_displayed_entry = NULL;
6421 s->first_displayed_entry = te;
6422 te = got_tree_entry_get_prev(s->tree, te);
6426 static const struct got_error *
6427 tree_scroll_down(struct tog_view *view, int maxscroll)
6429 struct tog_tree_view_state *s = &view->state.tree;
6430 struct got_tree_entry *next, *last;
6433 if (s->first_displayed_entry)
6434 next = got_tree_entry_get_next(s->tree,
6435 s->first_displayed_entry);
6437 next = got_object_tree_get_first_entry(s->tree);
6439 last = s->last_displayed_entry;
6440 while (next && n++ < maxscroll) {
6442 last = got_tree_entry_get_next(s->tree, last);
6443 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6444 s->first_displayed_entry = next;
6445 next = got_tree_entry_get_next(s->tree, next);
6452 static const struct got_error *
6453 tree_entry_path(char **path, struct tog_parent_trees *parents,
6454 struct got_tree_entry *te)
6456 const struct got_error *err = NULL;
6457 struct tog_parent_tree *pt;
6458 size_t len = 2; /* for leading slash and NUL */
6460 TAILQ_FOREACH(pt, parents, entry)
6461 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6464 len += strlen(got_tree_entry_get_name(te));
6466 *path = calloc(1, len);
6468 return got_error_from_errno("calloc");
6471 pt = TAILQ_LAST(parents, tog_parent_trees);
6473 const char *name = got_tree_entry_get_name(pt->selected_entry);
6474 if (strlcat(*path, name, len) >= len) {
6475 err = got_error(GOT_ERR_NO_SPACE);
6478 if (strlcat(*path, "/", len) >= len) {
6479 err = got_error(GOT_ERR_NO_SPACE);
6482 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6485 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6486 err = got_error(GOT_ERR_NO_SPACE);
6498 static const struct got_error *
6499 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6500 struct got_tree_entry *te, struct tog_parent_trees *parents,
6501 struct got_object_id *commit_id, struct got_repository *repo)
6503 const struct got_error *err = NULL;
6505 struct tog_view *blame_view;
6509 err = tree_entry_path(&path, parents, te);
6513 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6514 if (blame_view == NULL) {
6515 err = got_error_from_errno("view_open");
6519 err = open_blame_view(blame_view, path, commit_id, repo);
6521 if (err->code == GOT_ERR_CANCELLED)
6523 view_close(blame_view);
6525 *new_view = blame_view;
6531 static const struct got_error *
6532 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6533 struct tog_tree_view_state *s)
6535 struct tog_view *log_view;
6536 const struct got_error *err = NULL;
6541 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6542 if (log_view == NULL)
6543 return got_error_from_errno("view_open");
6545 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6549 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6552 view_close(log_view);
6554 *new_view = log_view;
6559 static const struct got_error *
6560 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6561 const char *head_ref_name, struct got_repository *repo)
6563 const struct got_error *err = NULL;
6564 char *commit_id_str = NULL;
6565 struct tog_tree_view_state *s = &view->state.tree;
6566 struct got_commit_object *commit = NULL;
6568 TAILQ_INIT(&s->parents);
6569 STAILQ_INIT(&s->colors);
6571 s->commit_id = got_object_id_dup(commit_id);
6572 if (s->commit_id == NULL)
6573 return got_error_from_errno("got_object_id_dup");
6575 err = got_object_open_as_commit(&commit, repo, commit_id);
6580 * The root is opened here and will be closed when the view is closed.
6581 * Any visited subtrees and their path-wise parents are opened and
6584 err = got_object_open_as_tree(&s->root, repo,
6585 got_object_commit_get_tree_id(commit));
6590 err = got_object_id_str(&commit_id_str, commit_id);
6594 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6595 err = got_error_from_errno("asprintf");
6599 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6600 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6601 if (head_ref_name) {
6602 s->head_ref_name = strdup(head_ref_name);
6603 if (s->head_ref_name == NULL) {
6604 err = got_error_from_errno("strdup");
6610 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6611 err = add_color(&s->colors, "\\$$",
6612 TOG_COLOR_TREE_SUBMODULE,
6613 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6616 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6617 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6620 err = add_color(&s->colors, "/$",
6621 TOG_COLOR_TREE_DIRECTORY,
6622 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6626 err = add_color(&s->colors, "\\*$",
6627 TOG_COLOR_TREE_EXECUTABLE,
6628 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6632 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6633 get_color_value("TOG_COLOR_COMMIT"));
6638 view->show = show_tree_view;
6639 view->input = input_tree_view;
6640 view->close = close_tree_view;
6641 view->search_start = search_start_tree_view;
6642 view->search_next = search_next_tree_view;
6644 free(commit_id_str);
6646 got_object_commit_close(commit);
6648 close_tree_view(view);
6652 static const struct got_error *
6653 close_tree_view(struct tog_view *view)
6655 struct tog_tree_view_state *s = &view->state.tree;
6657 free_colors(&s->colors);
6658 free(s->tree_label);
6659 s->tree_label = NULL;
6661 s->commit_id = NULL;
6662 free(s->head_ref_name);
6663 s->head_ref_name = NULL;
6664 while (!TAILQ_EMPTY(&s->parents)) {
6665 struct tog_parent_tree *parent;
6666 parent = TAILQ_FIRST(&s->parents);
6667 TAILQ_REMOVE(&s->parents, parent, entry);
6668 if (parent->tree != s->root)
6669 got_object_tree_close(parent->tree);
6673 if (s->tree != NULL && s->tree != s->root)
6674 got_object_tree_close(s->tree);
6676 got_object_tree_close(s->root);
6680 static const struct got_error *
6681 search_start_tree_view(struct tog_view *view)
6683 struct tog_tree_view_state *s = &view->state.tree;
6685 s->matched_entry = NULL;
6690 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6692 regmatch_t regmatch;
6694 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6698 static const struct got_error *
6699 search_next_tree_view(struct tog_view *view)
6701 struct tog_tree_view_state *s = &view->state.tree;
6702 struct got_tree_entry *te = NULL;
6704 if (!view->searching) {
6705 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6709 if (s->matched_entry) {
6710 if (view->searching == TOG_SEARCH_FORWARD) {
6711 if (s->selected_entry)
6712 te = got_tree_entry_get_next(s->tree,
6715 te = got_object_tree_get_first_entry(s->tree);
6717 if (s->selected_entry == NULL)
6718 te = got_object_tree_get_last_entry(s->tree);
6720 te = got_tree_entry_get_prev(s->tree,
6724 if (s->selected_entry)
6725 te = s->selected_entry;
6726 else if (view->searching == TOG_SEARCH_FORWARD)
6727 te = got_object_tree_get_first_entry(s->tree);
6729 te = got_object_tree_get_last_entry(s->tree);
6734 if (s->matched_entry == NULL) {
6735 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6738 if (view->searching == TOG_SEARCH_FORWARD)
6739 te = got_object_tree_get_first_entry(s->tree);
6741 te = got_object_tree_get_last_entry(s->tree);
6744 if (match_tree_entry(te, &view->regex)) {
6745 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6746 s->matched_entry = te;
6750 if (view->searching == TOG_SEARCH_FORWARD)
6751 te = got_tree_entry_get_next(s->tree, te);
6753 te = got_tree_entry_get_prev(s->tree, te);
6756 if (s->matched_entry) {
6757 s->first_displayed_entry = s->matched_entry;
6764 static const struct got_error *
6765 show_tree_view(struct tog_view *view)
6767 const struct got_error *err = NULL;
6768 struct tog_tree_view_state *s = &view->state.tree;
6771 err = tree_entry_path(&parent_path, &s->parents, NULL);
6775 err = draw_tree_entries(view, parent_path);
6782 static const struct got_error *
6783 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6785 const struct got_error *err = NULL;
6786 struct tog_tree_view_state *s = &view->state.tree;
6787 struct tog_view *log_view, *ref_view;
6788 struct got_tree_entry *te;
6789 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6793 s->show_ids = !s->show_ids;
6798 if (!s->selected_entry)
6800 if (view_is_parent_view(view))
6801 view_get_split(view, &begin_y, &begin_x);
6802 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6803 if (view_is_parent_view(view) &&
6804 view->mode == TOG_VIEW_SPLIT_HRZN) {
6805 err = view_init_hsplit(view, begin_y);
6810 log_view->focussed = 1;
6811 log_view->mode = view->mode;
6812 log_view->nlines = view->lines - begin_y;
6813 if (view_is_parent_view(view)) {
6814 view_transfer_size(log_view, view);
6815 err = view_close_child(view);
6818 err = view_set_child(view, log_view);
6821 view->focus_child = 1;
6823 *new_view = log_view;
6827 if (view_is_parent_view(view))
6828 view_get_split(view, &begin_y, &begin_x);
6829 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6830 if (ref_view == NULL)
6831 return got_error_from_errno("view_open");
6832 err = open_ref_view(ref_view, s->repo);
6834 view_close(ref_view);
6837 if (view_is_parent_view(view) &&
6838 view->mode == TOG_VIEW_SPLIT_HRZN) {
6839 err = view_init_hsplit(view, begin_y);
6844 ref_view->focussed = 1;
6845 ref_view->mode = view->mode;
6846 ref_view->nlines = view->lines - begin_y;
6847 if (view_is_parent_view(view)) {
6848 view_transfer_size(ref_view, view);
6849 err = view_close_child(view);
6852 err = view_set_child(view, ref_view);
6855 view->focus_child = 1;
6857 *new_view = ref_view;
6863 if (s->tree == s->root)
6864 s->first_displayed_entry =
6865 got_object_tree_get_first_entry(s->tree);
6867 s->first_displayed_entry = NULL;
6871 int eos = view->nlines - 3;
6873 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6877 te = got_object_tree_get_last_entry(s->tree);
6878 for (n = 0; n < eos; n++) {
6880 if (s->tree != s->root) {
6881 s->first_displayed_entry = NULL;
6886 s->first_displayed_entry = te;
6887 te = got_tree_entry_get_prev(s->tree, te);
6890 s->selected = n - 1;
6896 if (s->selected > 0) {
6900 tree_scroll_up(s, 1);
6901 if (s->selected_entry == NULL ||
6902 (s->tree == s->root && s->selected_entry ==
6903 got_object_tree_get_first_entry(s->tree)))
6913 if (s->tree == s->root) {
6914 if (got_object_tree_get_first_entry(s->tree) ==
6915 s->first_displayed_entry)
6916 s->selected -= MIN(s->selected, nscroll);
6918 if (s->first_displayed_entry == NULL)
6919 s->selected -= MIN(s->selected, nscroll);
6921 tree_scroll_up(s, MAX(0, nscroll));
6922 if (s->selected_entry == NULL ||
6923 (s->tree == s->root && s->selected_entry ==
6924 got_object_tree_get_first_entry(s->tree)))
6930 if (s->selected < s->ndisplayed - 1) {
6934 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6936 /* can't scroll any further */
6940 tree_scroll_down(view, 1);
6950 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6952 /* can't scroll any further; move cursor down */
6953 if (s->selected < s->ndisplayed - 1)
6954 s->selected += MIN(nscroll,
6955 s->ndisplayed - s->selected - 1);
6960 tree_scroll_down(view, nscroll);
6965 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6966 struct tog_parent_tree *parent;
6967 /* user selected '..' */
6968 if (s->tree == s->root) {
6972 parent = TAILQ_FIRST(&s->parents);
6973 TAILQ_REMOVE(&s->parents, parent,
6975 got_object_tree_close(s->tree);
6976 s->tree = parent->tree;
6977 s->first_displayed_entry =
6978 parent->first_displayed_entry;
6980 parent->selected_entry;
6981 s->selected = parent->selected;
6982 if (s->selected > view->nlines - 3) {
6983 err = offset_selection_down(view);
6988 } else if (S_ISDIR(got_tree_entry_get_mode(
6989 s->selected_entry))) {
6990 struct got_tree_object *subtree;
6992 err = got_object_open_as_tree(&subtree, s->repo,
6993 got_tree_entry_get_id(s->selected_entry));
6996 err = tree_view_visit_subtree(s, subtree);
6998 got_object_tree_close(subtree);
7001 } else if (S_ISREG(got_tree_entry_get_mode(
7002 s->selected_entry))) {
7003 struct tog_view *blame_view;
7004 int begin_x = 0, begin_y = 0;
7006 if (view_is_parent_view(view))
7007 view_get_split(view, &begin_y, &begin_x);
7009 err = blame_tree_entry(&blame_view, begin_y, begin_x,
7010 s->selected_entry, &s->parents,
7011 s->commit_id, s->repo);
7015 if (view_is_parent_view(view) &&
7016 view->mode == TOG_VIEW_SPLIT_HRZN) {
7017 err = view_init_hsplit(view, begin_y);
7024 blame_view->focussed = 1;
7025 blame_view->mode = view->mode;
7026 blame_view->nlines = view->lines - begin_y;
7027 if (view_is_parent_view(view)) {
7028 view_transfer_size(blame_view, view);
7029 err = view_close_child(view);
7032 err = view_set_child(view, blame_view);
7035 view->focus_child = 1;
7037 *new_view = blame_view;
7041 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7042 s->selected = view->nlines - 4;
7058 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7063 static const struct got_error *
7064 cmd_tree(int argc, char *argv[])
7066 const struct got_error *error;
7067 struct got_repository *repo = NULL;
7068 struct got_worktree *worktree = NULL;
7069 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7070 struct got_object_id *commit_id = NULL;
7071 struct got_commit_object *commit = NULL;
7072 const char *commit_id_arg = NULL;
7074 struct got_reference *ref = NULL;
7075 const char *head_ref_name = NULL;
7077 struct tog_view *view;
7078 int *pack_fds = NULL;
7080 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7083 commit_id_arg = optarg;
7086 repo_path = realpath(optarg, NULL);
7087 if (repo_path == NULL)
7088 return got_error_from_errno2("realpath",
7103 error = got_repo_pack_fds_open(&pack_fds);
7107 if (repo_path == NULL) {
7108 cwd = getcwd(NULL, 0);
7110 return got_error_from_errno("getcwd");
7111 error = got_worktree_open(&worktree, cwd);
7112 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7116 strdup(got_worktree_get_repo_path(worktree));
7118 repo_path = strdup(cwd);
7119 if (repo_path == NULL) {
7120 error = got_error_from_errno("strdup");
7125 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7129 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7136 error = apply_unveil(got_repo_get_path(repo), NULL);
7140 error = tog_load_refs(repo, 0);
7144 if (commit_id_arg == NULL) {
7145 error = got_repo_match_object_id(&commit_id, &label,
7146 worktree ? got_worktree_get_head_ref_name(worktree) :
7147 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7150 head_ref_name = label;
7152 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7154 head_ref_name = got_ref_get_name(ref);
7155 else if (error->code != GOT_ERR_NOT_REF)
7157 error = got_repo_match_object_id(&commit_id, NULL,
7158 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7163 error = got_object_open_as_commit(&commit, repo, commit_id);
7167 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7169 error = got_error_from_errno("view_open");
7172 error = open_tree_view(view, commit_id, head_ref_name, repo);
7175 if (!got_path_is_root_dir(in_repo_path)) {
7176 error = tree_view_walk_path(&view->state.tree, commit,
7183 /* Release work tree lock. */
7184 got_worktree_close(worktree);
7187 error = view_loop(view);
7196 const struct got_error *close_err = got_repo_close(repo);
7201 const struct got_error *pack_err =
7202 got_repo_pack_fds_close(pack_fds);
7210 static const struct got_error *
7211 ref_view_load_refs(struct tog_ref_view_state *s)
7213 struct got_reflist_entry *sre;
7214 struct tog_reflist_entry *re;
7217 TAILQ_FOREACH(sre, &tog_refs, entry) {
7218 if (strncmp(got_ref_get_name(sre->ref),
7219 "refs/got/", 9) == 0 &&
7220 strncmp(got_ref_get_name(sre->ref),
7221 "refs/got/backup/", 16) != 0)
7224 re = malloc(sizeof(*re));
7226 return got_error_from_errno("malloc");
7228 re->ref = got_ref_dup(sre->ref);
7229 if (re->ref == NULL)
7230 return got_error_from_errno("got_ref_dup");
7231 re->idx = s->nrefs++;
7232 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7235 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7240 ref_view_free_refs(struct tog_ref_view_state *s)
7242 struct tog_reflist_entry *re;
7244 while (!TAILQ_EMPTY(&s->refs)) {
7245 re = TAILQ_FIRST(&s->refs);
7246 TAILQ_REMOVE(&s->refs, re, entry);
7247 got_ref_close(re->ref);
7252 static const struct got_error *
7253 open_ref_view(struct tog_view *view, struct got_repository *repo)
7255 const struct got_error *err = NULL;
7256 struct tog_ref_view_state *s = &view->state.ref;
7258 s->selected_entry = 0;
7261 TAILQ_INIT(&s->refs);
7262 STAILQ_INIT(&s->colors);
7264 err = ref_view_load_refs(s);
7268 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7269 err = add_color(&s->colors, "^refs/heads/",
7270 TOG_COLOR_REFS_HEADS,
7271 get_color_value("TOG_COLOR_REFS_HEADS"));
7275 err = add_color(&s->colors, "^refs/tags/",
7276 TOG_COLOR_REFS_TAGS,
7277 get_color_value("TOG_COLOR_REFS_TAGS"));
7281 err = add_color(&s->colors, "^refs/remotes/",
7282 TOG_COLOR_REFS_REMOTES,
7283 get_color_value("TOG_COLOR_REFS_REMOTES"));
7287 err = add_color(&s->colors, "^refs/got/backup/",
7288 TOG_COLOR_REFS_BACKUP,
7289 get_color_value("TOG_COLOR_REFS_BACKUP"));
7294 view->show = show_ref_view;
7295 view->input = input_ref_view;
7296 view->close = close_ref_view;
7297 view->search_start = search_start_ref_view;
7298 view->search_next = search_next_ref_view;
7301 free_colors(&s->colors);
7305 static const struct got_error *
7306 close_ref_view(struct tog_view *view)
7308 struct tog_ref_view_state *s = &view->state.ref;
7310 ref_view_free_refs(s);
7311 free_colors(&s->colors);
7316 static const struct got_error *
7317 resolve_reflist_entry(struct got_object_id **commit_id,
7318 struct tog_reflist_entry *re, struct got_repository *repo)
7320 const struct got_error *err = NULL;
7321 struct got_object_id *obj_id;
7322 struct got_tag_object *tag = NULL;
7327 err = got_ref_resolve(&obj_id, repo, re->ref);
7331 err = got_object_get_type(&obj_type, repo, obj_id);
7336 case GOT_OBJ_TYPE_COMMIT:
7337 *commit_id = obj_id;
7339 case GOT_OBJ_TYPE_TAG:
7340 err = got_object_open_as_tag(&tag, repo, obj_id);
7344 err = got_object_get_type(&obj_type, repo,
7345 got_object_tag_get_object_id(tag));
7348 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7349 err = got_error(GOT_ERR_OBJ_TYPE);
7352 *commit_id = got_object_id_dup(
7353 got_object_tag_get_object_id(tag));
7354 if (*commit_id == NULL) {
7355 err = got_error_from_errno("got_object_id_dup");
7360 err = got_error(GOT_ERR_OBJ_TYPE);
7366 got_object_tag_close(tag);
7374 static const struct got_error *
7375 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7376 struct tog_reflist_entry *re, struct got_repository *repo)
7378 struct tog_view *log_view;
7379 const struct got_error *err = NULL;
7380 struct got_object_id *commit_id = NULL;
7384 err = resolve_reflist_entry(&commit_id, re, repo);
7386 if (err->code != GOT_ERR_OBJ_TYPE)
7392 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7393 if (log_view == NULL) {
7394 err = got_error_from_errno("view_open");
7398 err = open_log_view(log_view, commit_id, repo,
7399 got_ref_get_name(re->ref), "", 0);
7402 view_close(log_view);
7404 *new_view = log_view;
7410 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7412 struct tog_reflist_entry *re;
7415 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7418 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7419 while (i++ < maxscroll) {
7422 s->first_displayed_entry = re;
7423 re = TAILQ_PREV(re, tog_reflist_head, entry);
7427 static const struct got_error *
7428 ref_scroll_down(struct tog_view *view, int maxscroll)
7430 struct tog_ref_view_state *s = &view->state.ref;
7431 struct tog_reflist_entry *next, *last;
7434 if (s->first_displayed_entry)
7435 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7437 next = TAILQ_FIRST(&s->refs);
7439 last = s->last_displayed_entry;
7440 while (next && n++ < maxscroll) {
7442 last = TAILQ_NEXT(last, entry);
7443 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7444 s->first_displayed_entry = next;
7445 next = TAILQ_NEXT(next, entry);
7452 static const struct got_error *
7453 search_start_ref_view(struct tog_view *view)
7455 struct tog_ref_view_state *s = &view->state.ref;
7457 s->matched_entry = NULL;
7462 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7464 regmatch_t regmatch;
7466 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7470 static const struct got_error *
7471 search_next_ref_view(struct tog_view *view)
7473 struct tog_ref_view_state *s = &view->state.ref;
7474 struct tog_reflist_entry *re = NULL;
7476 if (!view->searching) {
7477 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7481 if (s->matched_entry) {
7482 if (view->searching == TOG_SEARCH_FORWARD) {
7483 if (s->selected_entry)
7484 re = TAILQ_NEXT(s->selected_entry, entry);
7486 re = TAILQ_PREV(s->selected_entry,
7487 tog_reflist_head, entry);
7489 if (s->selected_entry == NULL)
7490 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7492 re = TAILQ_PREV(s->selected_entry,
7493 tog_reflist_head, entry);
7496 if (s->selected_entry)
7497 re = s->selected_entry;
7498 else if (view->searching == TOG_SEARCH_FORWARD)
7499 re = TAILQ_FIRST(&s->refs);
7501 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7506 if (s->matched_entry == NULL) {
7507 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7510 if (view->searching == TOG_SEARCH_FORWARD)
7511 re = TAILQ_FIRST(&s->refs);
7513 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7516 if (match_reflist_entry(re, &view->regex)) {
7517 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7518 s->matched_entry = re;
7522 if (view->searching == TOG_SEARCH_FORWARD)
7523 re = TAILQ_NEXT(re, entry);
7525 re = TAILQ_PREV(re, tog_reflist_head, entry);
7528 if (s->matched_entry) {
7529 s->first_displayed_entry = s->matched_entry;
7536 static const struct got_error *
7537 show_ref_view(struct tog_view *view)
7539 const struct got_error *err = NULL;
7540 struct tog_ref_view_state *s = &view->state.ref;
7541 struct tog_reflist_entry *re;
7544 struct tog_color *tc;
7546 int limit = view->nlines;
7548 werase(view->window);
7551 if (view_is_hsplit_top(view))
7552 --limit; /* border */
7557 re = s->first_displayed_entry;
7559 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7561 return got_error_from_errno("asprintf");
7563 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7568 if (view_needs_focus_indication(view))
7569 wstandout(view->window);
7570 waddwstr(view->window, wline);
7571 if (view_needs_focus_indication(view))
7572 wstandend(view->window);
7577 if (width < view->ncols - 1)
7578 waddch(view->window, '\n');
7583 while (re && limit > 0) {
7585 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7588 struct got_commit_object *ci;
7589 struct got_tag_object *tag;
7590 struct got_object_id *id;
7594 err = got_ref_resolve(&id, s->repo, re->ref);
7597 err = got_object_open_as_tag(&tag, s->repo, id);
7599 if (err->code != GOT_ERR_OBJ_TYPE) {
7603 err = got_object_open_as_commit(&ci, s->repo,
7609 t = got_object_commit_get_committer_time(ci);
7610 got_object_commit_close(ci);
7612 t = got_object_tag_get_tagger_time(tag);
7613 got_object_tag_close(tag);
7616 if (gmtime_r(&t, &tm) == NULL)
7617 return got_error_from_errno("gmtime_r");
7618 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7619 return got_error(GOT_ERR_NO_SPACE);
7621 if (got_ref_is_symbolic(re->ref)) {
7622 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7623 ymd : "", got_ref_get_name(re->ref),
7624 got_ref_get_symref_target(re->ref)) == -1)
7625 return got_error_from_errno("asprintf");
7626 } else if (s->show_ids) {
7627 struct got_object_id *id;
7629 err = got_ref_resolve(&id, s->repo, re->ref);
7632 err = got_object_id_str(&id_str, id);
7637 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7638 got_ref_get_name(re->ref), id_str) == -1) {
7639 err = got_error_from_errno("asprintf");
7646 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7647 got_ref_get_name(re->ref)) == -1)
7648 return got_error_from_errno("asprintf");
7650 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7656 if (n == s->selected) {
7658 wstandout(view->window);
7659 s->selected_entry = re;
7661 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7663 wattr_on(view->window,
7664 COLOR_PAIR(tc->colorpair), NULL);
7665 waddwstr(view->window, wline);
7667 wattr_off(view->window,
7668 COLOR_PAIR(tc->colorpair), NULL);
7669 if (width < view->ncols - 1)
7670 waddch(view->window, '\n');
7671 if (n == s->selected && view->focussed)
7672 wstandend(view->window);
7678 s->last_displayed_entry = re;
7681 re = TAILQ_NEXT(re, entry);
7688 static const struct got_error *
7689 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7690 struct tog_reflist_entry *re, struct got_repository *repo)
7692 const struct got_error *err = NULL;
7693 struct got_object_id *commit_id = NULL;
7694 struct tog_view *tree_view;
7698 err = resolve_reflist_entry(&commit_id, re, repo);
7700 if (err->code != GOT_ERR_OBJ_TYPE)
7707 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7708 if (tree_view == NULL) {
7709 err = got_error_from_errno("view_open");
7713 err = open_tree_view(tree_view, commit_id,
7714 got_ref_get_name(re->ref), repo);
7718 *new_view = tree_view;
7723 static const struct got_error *
7724 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7726 const struct got_error *err = NULL;
7727 struct tog_ref_view_state *s = &view->state.ref;
7728 struct tog_view *log_view, *tree_view;
7729 struct tog_reflist_entry *re;
7730 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7734 s->show_ids = !s->show_ids;
7738 s->show_date = !s->show_date;
7742 s->sort_by_date = !s->sort_by_date;
7744 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7745 got_ref_cmp_by_commit_timestamp_descending :
7746 tog_ref_cmp_by_name, s->repo);
7749 got_reflist_object_id_map_free(tog_refs_idmap);
7750 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7751 &tog_refs, s->repo);
7754 ref_view_free_refs(s);
7755 err = ref_view_load_refs(s);
7760 if (!s->selected_entry)
7762 if (view_is_parent_view(view))
7763 view_get_split(view, &begin_y, &begin_x);
7765 err = log_ref_entry(&log_view, begin_y, begin_x,
7766 s->selected_entry, s->repo);
7770 if (view_is_parent_view(view) &&
7771 view->mode == TOG_VIEW_SPLIT_HRZN) {
7772 err = view_init_hsplit(view, begin_y);
7778 log_view->focussed = 1;
7779 log_view->mode = view->mode;
7780 log_view->nlines = view->lines - begin_y;
7781 if (view_is_parent_view(view)) {
7782 view_transfer_size(log_view, view);
7783 err = view_close_child(view);
7786 err = view_set_child(view, log_view);
7789 view->focus_child = 1;
7791 *new_view = log_view;
7795 if (!s->selected_entry)
7797 if (view_is_parent_view(view))
7798 view_get_split(view, &begin_y, &begin_x);
7799 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7800 s->selected_entry, s->repo);
7801 if (err || tree_view == NULL)
7803 if (view_is_parent_view(view) &&
7804 view->mode == TOG_VIEW_SPLIT_HRZN) {
7805 err = view_init_hsplit(view, begin_y);
7810 tree_view->focussed = 1;
7811 tree_view->mode = view->mode;
7812 tree_view->nlines = view->lines - begin_y;
7813 if (view_is_parent_view(view)) {
7814 view_transfer_size(tree_view, view);
7815 err = view_close_child(view);
7818 err = view_set_child(view, tree_view);
7821 view->focus_child = 1;
7823 *new_view = tree_view;
7829 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7833 int eos = view->nlines - 1;
7835 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7839 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7840 for (n = 0; n < eos; n++) {
7843 s->first_displayed_entry = re;
7844 re = TAILQ_PREV(re, tog_reflist_head, entry);
7847 s->selected = n - 1;
7853 if (s->selected > 0) {
7857 ref_scroll_up(s, 1);
7858 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7868 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7869 s->selected -= MIN(nscroll, s->selected);
7870 ref_scroll_up(s, MAX(0, nscroll));
7871 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7877 if (s->selected < s->ndisplayed - 1) {
7881 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7882 /* can't scroll any further */
7886 ref_scroll_down(view, 1);
7896 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7897 /* can't scroll any further; move cursor down */
7898 if (s->selected < s->ndisplayed - 1)
7899 s->selected += MIN(nscroll,
7900 s->ndisplayed - s->selected - 1);
7901 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7902 s->selected += s->ndisplayed - s->selected - 1;
7906 ref_scroll_down(view, nscroll);
7911 err = tog_load_refs(s->repo, s->sort_by_date);
7914 ref_view_free_refs(s);
7915 err = ref_view_load_refs(s);
7918 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7919 s->selected = view->nlines - 2;
7933 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7938 static const struct got_error *
7939 cmd_ref(int argc, char *argv[])
7941 const struct got_error *error;
7942 struct got_repository *repo = NULL;
7943 struct got_worktree *worktree = NULL;
7944 char *cwd = NULL, *repo_path = NULL;
7946 struct tog_view *view;
7947 int *pack_fds = NULL;
7949 while ((ch = getopt(argc, argv, "r:")) != -1) {
7952 repo_path = realpath(optarg, NULL);
7953 if (repo_path == NULL)
7954 return got_error_from_errno2("realpath",
7969 error = got_repo_pack_fds_open(&pack_fds);
7973 if (repo_path == NULL) {
7974 cwd = getcwd(NULL, 0);
7976 return got_error_from_errno("getcwd");
7977 error = got_worktree_open(&worktree, cwd);
7978 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7982 strdup(got_worktree_get_repo_path(worktree));
7984 repo_path = strdup(cwd);
7985 if (repo_path == NULL) {
7986 error = got_error_from_errno("strdup");
7991 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7997 error = apply_unveil(got_repo_get_path(repo), NULL);
8001 error = tog_load_refs(repo, 0);
8005 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8007 error = got_error_from_errno("view_open");
8011 error = open_ref_view(view, repo);
8016 /* Release work tree lock. */
8017 got_worktree_close(worktree);
8020 error = view_loop(view);
8025 const struct got_error *close_err = got_repo_close(repo);
8030 const struct got_error *pack_err =
8031 got_repo_pack_fds_close(pack_fds);
8040 * If view was scrolled down to move the selected line into view when opening a
8041 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8044 offset_selection_up(struct tog_view *view)
8046 switch (view->type) {
8047 case TOG_VIEW_BLAME: {
8048 struct tog_blame_view_state *s = &view->state.blame;
8049 if (s->first_displayed_line == 1) {
8050 s->selected_line = MAX(s->selected_line - view->offset,
8054 if (s->first_displayed_line > view->offset)
8055 s->first_displayed_line -= view->offset;
8057 s->first_displayed_line = 1;
8058 s->selected_line += view->offset;
8062 log_scroll_up(&view->state.log, view->offset);
8063 view->state.log.selected += view->offset;
8066 ref_scroll_up(&view->state.ref, view->offset);
8067 view->state.ref.selected += view->offset;
8070 tree_scroll_up(&view->state.tree, view->offset);
8071 view->state.tree.selected += view->offset;
8081 * If the selected line is in the section of screen covered by the bottom split,
8082 * scroll down offset lines to move it into view and index its new position.
8084 static const struct got_error *
8085 offset_selection_down(struct tog_view *view)
8087 const struct got_error *err = NULL;
8088 const struct got_error *(*scrolld)(struct tog_view *, int);
8089 int *selected = NULL;
8092 switch (view->type) {
8093 case TOG_VIEW_BLAME: {
8094 struct tog_blame_view_state *s = &view->state.blame;
8097 if (s->selected_line > view->nlines - header) {
8098 offset = abs(view->nlines - s->selected_line - header);
8099 s->first_displayed_line += offset;
8100 s->selected_line -= offset;
8101 view->offset = offset;
8105 case TOG_VIEW_LOG: {
8106 struct tog_log_view_state *s = &view->state.log;
8107 scrolld = &log_scroll_down;
8108 header = view_is_parent_view(view) ? 3 : 2;
8109 selected = &s->selected;
8112 case TOG_VIEW_REF: {
8113 struct tog_ref_view_state *s = &view->state.ref;
8114 scrolld = &ref_scroll_down;
8116 selected = &s->selected;
8119 case TOG_VIEW_TREE: {
8120 struct tog_tree_view_state *s = &view->state.tree;
8121 scrolld = &tree_scroll_down;
8123 selected = &s->selected;
8133 if (selected && *selected > view->nlines - header) {
8134 offset = abs(view->nlines - *selected - header);
8135 view->offset = offset;
8136 if (scrolld && offset) {
8137 err = scrolld(view, offset);
8138 *selected -= offset;
8146 list_commands(FILE *fp)
8150 fprintf(fp, "commands:");
8151 for (i = 0; i < nitems(tog_commands); i++) {
8152 const struct tog_cmd *cmd = &tog_commands[i];
8153 fprintf(fp, " %s", cmd->name);
8159 usage(int hflag, int status)
8161 FILE *fp = (status == 0) ? stdout : stderr;
8163 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8166 fprintf(fp, "lazy usage: %s path\n", getprogname());
8173 make_argv(int argc, ...)
8181 argv = calloc(argc, sizeof(char *));
8184 for (i = 0; i < argc; i++) {
8185 argv[i] = strdup(va_arg(ap, char *));
8186 if (argv[i] == NULL)
8195 * Try to convert 'tog path' into a 'tog log path' command.
8196 * The user could simply have mistyped the command rather than knowingly
8197 * provided a path. So check whether argv[0] can in fact be resolved
8198 * to a path in the HEAD commit and print a special error if not.
8199 * This hack is for mpi@ <3
8201 static const struct got_error *
8202 tog_log_with_path(int argc, char *argv[])
8204 const struct got_error *error = NULL, *close_err;
8205 const struct tog_cmd *cmd = NULL;
8206 struct got_repository *repo = NULL;
8207 struct got_worktree *worktree = NULL;
8208 struct got_object_id *commit_id = NULL, *id = NULL;
8209 struct got_commit_object *commit = NULL;
8210 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8211 char *commit_id_str = NULL, **cmd_argv = NULL;
8212 int *pack_fds = NULL;
8214 cwd = getcwd(NULL, 0);
8216 return got_error_from_errno("getcwd");
8218 error = got_repo_pack_fds_open(&pack_fds);
8222 error = got_worktree_open(&worktree, cwd);
8223 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8227 repo_path = strdup(got_worktree_get_repo_path(worktree));
8229 repo_path = strdup(cwd);
8230 if (repo_path == NULL) {
8231 error = got_error_from_errno("strdup");
8235 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8239 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8244 error = tog_load_refs(repo, 0);
8247 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8248 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8249 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8254 got_worktree_close(worktree);
8258 error = got_object_open_as_commit(&commit, repo, commit_id);
8262 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8264 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8266 fprintf(stderr, "%s: '%s' is no known command or path\n",
8267 getprogname(), argv[0]);
8272 close_err = got_repo_close(repo);
8277 error = got_object_id_str(&commit_id_str, commit_id);
8281 cmd = &tog_commands[0]; /* log */
8283 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8284 error = cmd->cmd_main(argc, cmd_argv);
8287 close_err = got_repo_close(repo);
8292 got_object_commit_close(commit);
8294 got_worktree_close(worktree);
8296 const struct got_error *pack_err =
8297 got_repo_pack_fds_close(pack_fds);
8302 free(commit_id_str);
8309 for (i = 0; i < argc; i++)
8318 main(int argc, char *argv[])
8320 const struct got_error *error = NULL;
8321 const struct tog_cmd *cmd = NULL;
8322 int ch, hflag = 0, Vflag = 0;
8323 char **cmd_argv = NULL;
8324 static const struct option longopts[] = {
8325 { "version", no_argument, NULL, 'V' },
8328 char *diff_algo_str = NULL;
8330 setlocale(LC_CTYPE, "");
8332 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8352 got_version_print_str();
8357 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8365 /* Build an argument vector which runs a default command. */
8366 cmd = &tog_commands[0];
8368 cmd_argv = make_argv(argc, cmd->name);
8372 /* Did the user specify a command? */
8373 for (i = 0; i < nitems(tog_commands); i++) {
8374 if (strncmp(tog_commands[i].name, argv[0],
8375 strlen(argv[0])) == 0) {
8376 cmd = &tog_commands[i];
8382 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8383 if (diff_algo_str) {
8384 if (strcasecmp(diff_algo_str, "patience") == 0)
8385 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8386 if (strcasecmp(diff_algo_str, "myers") == 0)
8387 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8393 /* No command specified; try log with a path */
8394 error = tog_log_with_path(argc, argv);
8399 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8406 for (i = 0; i < argc; i++)
8411 if (error && error->code != GOT_ERR_CANCELLED)
8412 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);