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;
2308 err = got_object_commit_get_logmsg(&msg0, c);
2312 while (*msg == '\n')
2314 if ((eol = strchr(msg, '\n')))
2316 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2317 date_display_cols + author_cols, 0);
2320 view->maxx = MAX(view->maxx, width);
2324 entry = TAILQ_NEXT(entry, entry);
2327 entry = s->first_displayed_entry;
2328 s->last_displayed_entry = s->first_displayed_entry;
2331 if (ncommits >= limit - 1)
2333 if (ncommits == s->selected)
2334 wstandout(view->window);
2335 err = draw_commit(view, entry->commit, entry->id,
2336 date_display_cols, author_cols);
2337 if (ncommits == s->selected)
2338 wstandend(view->window);
2342 s->last_displayed_entry = entry;
2343 entry = TAILQ_NEXT(entry, entry);
2356 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2358 struct commit_queue_entry *entry;
2361 entry = TAILQ_FIRST(&s->commits.head);
2362 if (s->first_displayed_entry == entry)
2365 entry = s->first_displayed_entry;
2366 while (entry && nscrolled < maxscroll) {
2367 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2369 s->first_displayed_entry = entry;
2375 static const struct got_error *
2376 trigger_log_thread(struct tog_view *view, int wait)
2378 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2381 halfdelay(1); /* fast refresh while loading commits */
2383 while (!ta->log_complete && !tog_thread_error &&
2384 (ta->commits_needed > 0 || ta->load_all)) {
2385 /* Wake the log thread. */
2386 errcode = pthread_cond_signal(&ta->need_commits);
2388 return got_error_set_errno(errcode,
2389 "pthread_cond_signal");
2392 * The mutex will be released while the view loop waits
2393 * in wgetch(), at which time the log thread will run.
2398 /* Display progress update in log view. */
2399 show_log_view(view);
2403 /* Wait right here while next commit is being loaded. */
2404 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2406 return got_error_set_errno(errcode,
2407 "pthread_cond_wait");
2409 /* Display progress update in log view. */
2410 show_log_view(view);
2418 static const struct got_error *
2419 request_log_commits(struct tog_view *view)
2421 struct tog_log_view_state *state = &view->state.log;
2422 const struct got_error *err = NULL;
2424 if (state->thread_args.log_complete)
2427 state->thread_args.commits_needed += view->nscrolled;
2428 err = trigger_log_thread(view, 1);
2429 view->nscrolled = 0;
2434 static const struct got_error *
2435 log_scroll_down(struct tog_view *view, int maxscroll)
2437 struct tog_log_view_state *s = &view->state.log;
2438 const struct got_error *err = NULL;
2439 struct commit_queue_entry *pentry;
2440 int nscrolled = 0, ncommits_needed;
2442 if (s->last_displayed_entry == NULL)
2445 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2446 if (s->commits.ncommits < ncommits_needed &&
2447 !s->thread_args.log_complete) {
2449 * Ask the log thread for required amount of commits.
2451 s->thread_args.commits_needed += maxscroll;
2452 err = trigger_log_thread(view, 1);
2458 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2459 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2462 s->last_displayed_entry = pentry ?
2463 pentry : s->last_displayed_entry;;
2465 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2468 s->first_displayed_entry = pentry;
2469 } while (++nscrolled < maxscroll);
2471 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2472 view->nscrolled += nscrolled;
2474 view->nscrolled = 0;
2479 static const struct got_error *
2480 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2481 struct got_commit_object *commit, struct got_object_id *commit_id,
2482 struct tog_view *log_view, struct got_repository *repo)
2484 const struct got_error *err;
2485 struct got_object_qid *parent_id;
2486 struct tog_view *diff_view;
2488 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2489 if (diff_view == NULL)
2490 return got_error_from_errno("view_open");
2492 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2493 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2494 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2496 *new_view = diff_view;
2500 static const struct got_error *
2501 tree_view_visit_subtree(struct tog_tree_view_state *s,
2502 struct got_tree_object *subtree)
2504 struct tog_parent_tree *parent;
2506 parent = calloc(1, sizeof(*parent));
2508 return got_error_from_errno("calloc");
2510 parent->tree = s->tree;
2511 parent->first_displayed_entry = s->first_displayed_entry;
2512 parent->selected_entry = s->selected_entry;
2513 parent->selected = s->selected;
2514 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2517 s->first_displayed_entry = NULL;
2521 static const struct got_error *
2522 tree_view_walk_path(struct tog_tree_view_state *s,
2523 struct got_commit_object *commit, const char *path)
2525 const struct got_error *err = NULL;
2526 struct got_tree_object *tree = NULL;
2528 char *slash, *subpath = NULL;
2530 /* Walk the path and open corresponding tree objects. */
2533 struct got_tree_entry *te;
2534 struct got_object_id *tree_id;
2540 /* Ensure the correct subtree entry is selected. */
2541 slash = strchr(p, '/');
2543 te_name = strdup(p);
2545 te_name = strndup(p, slash - p);
2546 if (te_name == NULL) {
2547 err = got_error_from_errno("strndup");
2550 te = got_object_tree_find_entry(s->tree, te_name);
2552 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2557 s->first_displayed_entry = s->selected_entry = te;
2559 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2560 break; /* jump to this file's entry */
2562 slash = strchr(p, '/');
2564 subpath = strndup(path, slash - path);
2566 subpath = strdup(path);
2567 if (subpath == NULL) {
2568 err = got_error_from_errno("strdup");
2572 err = got_object_id_by_path(&tree_id, s->repo, commit,
2577 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2582 err = tree_view_visit_subtree(s, tree);
2584 got_object_tree_close(tree);
2598 static const struct got_error *
2599 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2600 struct commit_queue_entry *entry, const char *path,
2601 const char *head_ref_name, struct got_repository *repo)
2603 const struct got_error *err = NULL;
2604 struct tog_tree_view_state *s;
2605 struct tog_view *tree_view;
2607 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2608 if (tree_view == NULL)
2609 return got_error_from_errno("view_open");
2611 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2614 s = &tree_view->state.tree;
2616 *new_view = tree_view;
2618 if (got_path_is_root_dir(path))
2621 return tree_view_walk_path(s, entry->commit, path);
2624 static const struct got_error *
2625 block_signals_used_by_main_thread(void)
2630 if (sigemptyset(&sigset) == -1)
2631 return got_error_from_errno("sigemptyset");
2633 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2634 if (sigaddset(&sigset, SIGWINCH) == -1)
2635 return got_error_from_errno("sigaddset");
2636 if (sigaddset(&sigset, SIGCONT) == -1)
2637 return got_error_from_errno("sigaddset");
2638 if (sigaddset(&sigset, SIGINT) == -1)
2639 return got_error_from_errno("sigaddset");
2640 if (sigaddset(&sigset, SIGTERM) == -1)
2641 return got_error_from_errno("sigaddset");
2643 /* ncurses handles SIGTSTP */
2644 if (sigaddset(&sigset, SIGTSTP) == -1)
2645 return got_error_from_errno("sigaddset");
2647 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2649 return got_error_set_errno(errcode, "pthread_sigmask");
2655 log_thread(void *arg)
2657 const struct got_error *err = NULL;
2659 struct tog_log_thread_args *a = arg;
2663 * Sync startup with main thread such that we begin our
2664 * work once view_input() has released the mutex.
2666 errcode = pthread_mutex_lock(&tog_mutex);
2668 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2672 err = block_signals_used_by_main_thread();
2674 pthread_mutex_unlock(&tog_mutex);
2678 while (!done && !err && !tog_fatal_signal_received()) {
2679 errcode = pthread_mutex_unlock(&tog_mutex);
2681 err = got_error_set_errno(errcode,
2682 "pthread_mutex_unlock");
2685 err = queue_commits(a);
2687 if (err->code != GOT_ERR_ITER_COMPLETED)
2691 } else if (a->commits_needed > 0 && !a->load_all)
2692 a->commits_needed--;
2694 errcode = pthread_mutex_lock(&tog_mutex);
2696 err = got_error_set_errno(errcode,
2697 "pthread_mutex_lock");
2699 } else if (*a->quit)
2701 else if (*a->first_displayed_entry == NULL) {
2702 *a->first_displayed_entry =
2703 TAILQ_FIRST(&a->commits->head);
2704 *a->selected_entry = *a->first_displayed_entry;
2707 errcode = pthread_cond_signal(&a->commit_loaded);
2709 err = got_error_set_errno(errcode,
2710 "pthread_cond_signal");
2711 pthread_mutex_unlock(&tog_mutex);
2716 a->commits_needed = 0;
2718 if (a->commits_needed == 0 && !a->load_all) {
2719 errcode = pthread_cond_wait(&a->need_commits,
2722 err = got_error_set_errno(errcode,
2723 "pthread_cond_wait");
2724 pthread_mutex_unlock(&tog_mutex);
2732 a->log_complete = 1;
2733 errcode = pthread_mutex_unlock(&tog_mutex);
2735 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2738 tog_thread_error = 1;
2739 pthread_cond_signal(&a->commit_loaded);
2744 static const struct got_error *
2745 stop_log_thread(struct tog_log_view_state *s)
2747 const struct got_error *err = NULL, *thread_err = NULL;
2752 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2754 return got_error_set_errno(errcode,
2755 "pthread_cond_signal");
2756 errcode = pthread_mutex_unlock(&tog_mutex);
2758 return got_error_set_errno(errcode,
2759 "pthread_mutex_unlock");
2760 errcode = pthread_join(s->thread, (void **)&thread_err);
2762 return got_error_set_errno(errcode, "pthread_join");
2763 errcode = pthread_mutex_lock(&tog_mutex);
2765 return got_error_set_errno(errcode,
2766 "pthread_mutex_lock");
2770 if (s->thread_args.repo) {
2771 err = got_repo_close(s->thread_args.repo);
2772 s->thread_args.repo = NULL;
2775 if (s->thread_args.pack_fds) {
2776 const struct got_error *pack_err =
2777 got_repo_pack_fds_close(s->thread_args.pack_fds);
2780 s->thread_args.pack_fds = NULL;
2783 if (s->thread_args.graph) {
2784 got_commit_graph_close(s->thread_args.graph);
2785 s->thread_args.graph = NULL;
2788 return err ? err : thread_err;
2791 static const struct got_error *
2792 close_log_view(struct tog_view *view)
2794 const struct got_error *err = NULL;
2795 struct tog_log_view_state *s = &view->state.log;
2798 err = stop_log_thread(s);
2800 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2801 if (errcode && err == NULL)
2802 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2804 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2805 if (errcode && err == NULL)
2806 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2808 free_commits(&s->commits);
2809 free(s->in_repo_path);
2810 s->in_repo_path = NULL;
2813 free(s->head_ref_name);
2814 s->head_ref_name = NULL;
2818 static const struct got_error *
2819 search_start_log_view(struct tog_view *view)
2821 struct tog_log_view_state *s = &view->state.log;
2823 s->matched_entry = NULL;
2824 s->search_entry = NULL;
2828 static const struct got_error *
2829 search_next_log_view(struct tog_view *view)
2831 const struct got_error *err = NULL;
2832 struct tog_log_view_state *s = &view->state.log;
2833 struct commit_queue_entry *entry;
2835 /* Display progress update in log view. */
2836 show_log_view(view);
2840 if (s->search_entry) {
2842 errcode = pthread_mutex_unlock(&tog_mutex);
2844 return got_error_set_errno(errcode,
2845 "pthread_mutex_unlock");
2846 ch = wgetch(view->window);
2847 errcode = pthread_mutex_lock(&tog_mutex);
2849 return got_error_set_errno(errcode,
2850 "pthread_mutex_lock");
2851 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2852 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2855 if (view->searching == TOG_SEARCH_FORWARD)
2856 entry = TAILQ_NEXT(s->search_entry, entry);
2858 entry = TAILQ_PREV(s->search_entry,
2859 commit_queue_head, entry);
2860 } else if (s->matched_entry) {
2861 int matched_idx = s->matched_entry->idx;
2862 int selected_idx = s->selected_entry->idx;
2865 * If the user has moved the cursor after we hit a match,
2866 * the position from where we should continue searching
2867 * might have changed.
2869 if (view->searching == TOG_SEARCH_FORWARD) {
2870 if (matched_idx > selected_idx)
2871 entry = TAILQ_NEXT(s->selected_entry, entry);
2873 entry = TAILQ_NEXT(s->matched_entry, entry);
2875 if (matched_idx < selected_idx)
2876 entry = TAILQ_PREV(s->selected_entry,
2877 commit_queue_head, entry);
2879 entry = TAILQ_PREV(s->matched_entry,
2880 commit_queue_head, entry);
2883 entry = s->selected_entry;
2889 if (entry == NULL) {
2890 if (s->thread_args.log_complete ||
2891 view->searching == TOG_SEARCH_BACKWARD) {
2892 view->search_next_done =
2893 (s->matched_entry == NULL ?
2894 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2895 s->search_entry = NULL;
2899 * Poke the log thread for more commits and return,
2900 * allowing the main loop to make progress. Search
2901 * will resume at s->search_entry once we come back.
2903 s->thread_args.commits_needed++;
2904 return trigger_log_thread(view, 0);
2907 err = match_commit(&have_match, entry->id, entry->commit,
2912 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2913 s->matched_entry = entry;
2917 s->search_entry = entry;
2918 if (view->searching == TOG_SEARCH_FORWARD)
2919 entry = TAILQ_NEXT(entry, entry);
2921 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2924 if (s->matched_entry) {
2925 int cur = s->selected_entry->idx;
2926 while (cur < s->matched_entry->idx) {
2927 err = input_log_view(NULL, view, KEY_DOWN);
2932 while (cur > s->matched_entry->idx) {
2933 err = input_log_view(NULL, view, KEY_UP);
2940 s->search_entry = NULL;
2945 static const struct got_error *
2946 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2947 struct got_repository *repo, const char *head_ref_name,
2948 const char *in_repo_path, int log_branches)
2950 const struct got_error *err = NULL;
2951 struct tog_log_view_state *s = &view->state.log;
2952 struct got_repository *thread_repo = NULL;
2953 struct got_commit_graph *thread_graph = NULL;
2956 if (in_repo_path != s->in_repo_path) {
2957 free(s->in_repo_path);
2958 s->in_repo_path = strdup(in_repo_path);
2959 if (s->in_repo_path == NULL)
2960 return got_error_from_errno("strdup");
2963 /* The commit queue only contains commits being displayed. */
2964 TAILQ_INIT(&s->commits.head);
2965 s->commits.ncommits = 0;
2968 if (head_ref_name) {
2969 s->head_ref_name = strdup(head_ref_name);
2970 if (s->head_ref_name == NULL) {
2971 err = got_error_from_errno("strdup");
2975 s->start_id = got_object_id_dup(start_id);
2976 if (s->start_id == NULL) {
2977 err = got_error_from_errno("got_object_id_dup");
2980 s->log_branches = log_branches;
2982 STAILQ_INIT(&s->colors);
2983 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2984 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2985 get_color_value("TOG_COLOR_COMMIT"));
2988 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2989 get_color_value("TOG_COLOR_AUTHOR"));
2991 free_colors(&s->colors);
2994 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2995 get_color_value("TOG_COLOR_DATE"));
2997 free_colors(&s->colors);
3002 view->show = show_log_view;
3003 view->input = input_log_view;
3004 view->resize = resize_log_view;
3005 view->close = close_log_view;
3006 view->search_start = search_start_log_view;
3007 view->search_next = search_next_log_view;
3009 if (s->thread_args.pack_fds == NULL) {
3010 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3014 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3015 s->thread_args.pack_fds);
3018 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3022 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3023 s->repo, NULL, NULL);
3027 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3029 err = got_error_set_errno(errcode, "pthread_cond_init");
3032 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3034 err = got_error_set_errno(errcode, "pthread_cond_init");
3038 s->thread_args.commits_needed = view->nlines;
3039 s->thread_args.graph = thread_graph;
3040 s->thread_args.commits = &s->commits;
3041 s->thread_args.in_repo_path = s->in_repo_path;
3042 s->thread_args.start_id = s->start_id;
3043 s->thread_args.repo = thread_repo;
3044 s->thread_args.log_complete = 0;
3045 s->thread_args.quit = &s->quit;
3046 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3047 s->thread_args.selected_entry = &s->selected_entry;
3048 s->thread_args.searching = &view->searching;
3049 s->thread_args.search_next_done = &view->search_next_done;
3050 s->thread_args.regex = &view->regex;
3053 close_log_view(view);
3057 static const struct got_error *
3058 show_log_view(struct tog_view *view)
3060 const struct got_error *err;
3061 struct tog_log_view_state *s = &view->state.log;
3063 if (s->thread == NULL) {
3064 int errcode = pthread_create(&s->thread, NULL, log_thread,
3067 return got_error_set_errno(errcode, "pthread_create");
3068 if (s->thread_args.commits_needed > 0) {
3069 err = trigger_log_thread(view, 1);
3075 return draw_commits(view);
3079 log_move_cursor_up(struct tog_view *view, int page, int home)
3081 struct tog_log_view_state *s = &view->state.log;
3083 if (s->selected_entry->idx == 0)
3085 if (s->first_displayed_entry == NULL)
3088 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3090 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3092 if (!page && !home && s->selected > 0)
3095 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3101 static const struct got_error *
3102 log_move_cursor_down(struct tog_view *view, int page)
3104 struct tog_log_view_state *s = &view->state.log;
3105 struct commit_queue_entry *first;
3106 const struct got_error *err = NULL;
3108 first = s->first_displayed_entry;
3109 if (first == NULL) {
3114 if (s->thread_args.log_complete &&
3115 s->selected_entry->idx >= s->commits.ncommits - 1)
3119 int eos = view->nlines - 2;
3121 if (view_is_hsplit_top(view))
3122 --eos; /* border consumes the last line */
3123 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3126 err = log_scroll_down(view, 1);
3127 } else if (s->thread_args.load_all) {
3128 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3129 s->selected += MIN(s->last_displayed_entry->idx -
3130 s->selected_entry->idx, page + 1);
3132 err = log_scroll_down(view, MIN(page,
3133 s->commits.ncommits - s->selected_entry->idx - 1));
3134 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3136 err = log_scroll_down(view, page);
3139 if (first == s->first_displayed_entry && s->selected <
3140 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3141 s->selected = MIN(s->commits.ncommits - 1, page);
3148 * We might necessarily overshoot in horizontal
3149 * splits; if so, select the last displayed commit.
3151 s->selected = MIN(s->selected,
3152 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3156 if (s->thread_args.log_complete &&
3157 s->selected_entry->idx == s->commits.ncommits - 1)
3164 view_get_split(struct tog_view *view, int *y, int *x)
3169 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3170 if (view->child && view->child->resized_y)
3171 *y = view->child->resized_y;
3172 else if (view->resized_y)
3173 *y = view->resized_y;
3175 *y = view_split_begin_y(view->lines);
3176 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3177 if (view->child && view->child->resized_x)
3178 *x = view->child->resized_x;
3179 else if (view->resized_x)
3180 *x = view->resized_x;
3182 *x = view_split_begin_x(view->begin_x);
3186 /* Split view horizontally at y and offset view->state->selected line. */
3187 static const struct got_error *
3188 view_init_hsplit(struct tog_view *view, int y)
3190 const struct got_error *err = NULL;
3194 err = view_resize(view);
3198 err = offset_selection_down(view);
3203 static const struct got_error *
3204 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3206 const struct got_error *err = NULL;
3207 struct tog_log_view_state *s = &view->state.log;
3208 struct tog_view *diff_view = NULL, *tree_view = NULL;
3209 struct tog_view *ref_view = NULL;
3210 struct commit_queue_entry *entry;
3211 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3213 if (s->thread_args.load_all) {
3214 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3215 s->thread_args.load_all = 0;
3216 else if (s->thread_args.log_complete) {
3217 err = log_move_cursor_down(view, s->commits.ncommits);
3218 s->thread_args.load_all = 0;
3223 eos = nscroll = view->nlines - 1;
3224 if (view_is_hsplit_top(view))
3235 view->x = MAX(view->maxx - view->ncols / 2, 0);
3240 if (view->x + view->ncols / 2 < view->maxx)
3241 view->x += 2; /* move two columns right */
3247 view->x -= MIN(view->x, 2); /* move two columns back */
3256 log_move_cursor_up(view, 0, 0);
3260 log_move_cursor_up(view, 0, 1);
3270 log_move_cursor_up(view, nscroll, 0);
3277 err = log_move_cursor_down(view, 0);
3280 s->use_committer = !s->use_committer;
3284 /* We don't know yet how many commits, so we're forced to
3285 * traverse them all. */
3287 if (!s->thread_args.log_complete) {
3288 s->thread_args.load_all = 1;
3289 return trigger_log_thread(view, 0);
3293 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3294 for (n = 0; n < eos; n++) {
3297 s->first_displayed_entry = entry;
3298 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3301 s->selected = n - 1;
3313 err = log_move_cursor_down(view, nscroll);
3316 if (s->selected > view->nlines - 2)
3317 s->selected = view->nlines - 2;
3318 if (s->selected > s->commits.ncommits - 1)
3319 s->selected = s->commits.ncommits - 1;
3321 if (s->commits.ncommits < view->nlines - 1 &&
3322 !s->thread_args.log_complete) {
3323 s->thread_args.commits_needed += (view->nlines - 1) -
3324 s->commits.ncommits;
3325 err = trigger_log_thread(view, 1);
3331 if (s->selected_entry == NULL)
3334 /* get dimensions--don't split till initialisation succeeds */
3335 if (view_is_parent_view(view))
3336 view_get_split(view, &begin_y, &begin_x);
3338 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3339 s->selected_entry->commit, s->selected_entry->id,
3344 if (view_is_parent_view(view) &&
3345 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3346 err = view_init_hsplit(view, begin_y);
3352 diff_view->focussed = 1;
3353 diff_view->mode = view->mode;
3354 diff_view->nlines = view->lines - begin_y;
3356 if (view_is_parent_view(view)) {
3357 view_transfer_size(diff_view, view);
3358 err = view_close_child(view);
3361 err = view_set_child(view, diff_view);
3364 view->focus_child = 1;
3366 *new_view = diff_view;
3370 if (s->selected_entry == NULL)
3372 if (view_is_parent_view(view))
3373 view_get_split(view, &begin_y, &begin_x);
3374 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3375 s->selected_entry, s->in_repo_path, s->head_ref_name,
3379 if (view_is_parent_view(view) &&
3380 view->mode == TOG_VIEW_SPLIT_HRZN) {
3381 err = view_init_hsplit(view, begin_y);
3386 tree_view->focussed = 1;
3387 tree_view->mode = view->mode;
3388 tree_view->nlines = view->lines - begin_y;
3389 if (view_is_parent_view(view)) {
3390 view_transfer_size(tree_view, view);
3391 err = view_close_child(view);
3394 err = view_set_child(view, tree_view);
3397 view->focus_child = 1;
3399 *new_view = tree_view;
3405 if (ch == KEY_BACKSPACE &&
3406 got_path_is_root_dir(s->in_repo_path))
3408 err = stop_log_thread(s);
3411 if (ch == KEY_BACKSPACE) {
3413 err = got_path_dirname(&parent_path, s->in_repo_path);
3416 free(s->in_repo_path);
3417 s->in_repo_path = parent_path;
3418 s->thread_args.in_repo_path = s->in_repo_path;
3419 } else if (ch == CTRL('l')) {
3420 struct got_object_id *start_id;
3421 err = got_repo_match_object_id(&start_id, NULL,
3422 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3423 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3427 s->start_id = start_id;
3428 s->thread_args.start_id = s->start_id;
3430 s->log_branches = !s->log_branches;
3432 if (s->thread_args.pack_fds == NULL) {
3433 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3437 err = got_repo_open(&s->thread_args.repo,
3438 got_repo_get_path(s->repo), NULL,
3439 s->thread_args.pack_fds);
3443 err = tog_load_refs(s->repo, 0);
3446 err = got_commit_graph_open(&s->thread_args.graph,
3447 s->in_repo_path, !s->log_branches);
3450 err = got_commit_graph_iter_start(s->thread_args.graph,
3451 s->start_id, s->repo, NULL, NULL);
3454 free_commits(&s->commits);
3455 s->first_displayed_entry = NULL;
3456 s->last_displayed_entry = NULL;
3457 s->selected_entry = NULL;
3459 s->thread_args.log_complete = 0;
3461 s->thread_args.commits_needed = view->lines;
3462 s->matched_entry = NULL;
3463 s->search_entry = NULL;
3467 if (view_is_parent_view(view))
3468 view_get_split(view, &begin_y, &begin_x);
3469 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3470 if (ref_view == NULL)
3471 return got_error_from_errno("view_open");
3472 err = open_ref_view(ref_view, s->repo);
3474 view_close(ref_view);
3477 if (view_is_parent_view(view) &&
3478 view->mode == TOG_VIEW_SPLIT_HRZN) {
3479 err = view_init_hsplit(view, begin_y);
3484 ref_view->focussed = 1;
3485 ref_view->mode = view->mode;
3486 ref_view->nlines = view->lines - begin_y;
3487 if (view_is_parent_view(view)) {
3488 view_transfer_size(ref_view, view);
3489 err = view_close_child(view);
3492 err = view_set_child(view, ref_view);
3495 view->focus_child = 1;
3497 *new_view = ref_view;
3507 static const struct got_error *
3508 apply_unveil(const char *repo_path, const char *worktree_path)
3510 const struct got_error *error;
3513 if (unveil("gmon.out", "rwc") != 0)
3514 return got_error_from_errno2("unveil", "gmon.out");
3516 if (repo_path && unveil(repo_path, "r") != 0)
3517 return got_error_from_errno2("unveil", repo_path);
3519 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3520 return got_error_from_errno2("unveil", worktree_path);
3522 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3523 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3525 error = got_privsep_unveil_exec_helpers();
3529 if (unveil(NULL, NULL) != 0)
3530 return got_error_from_errno("unveil");
3539 * Override default signal handlers before starting ncurses.
3540 * This should prevent ncurses from installing its own
3541 * broken cleanup() signal handler.
3543 signal(SIGWINCH, tog_sigwinch);
3544 signal(SIGPIPE, tog_sigpipe);
3545 signal(SIGCONT, tog_sigcont);
3546 signal(SIGINT, tog_sigint);
3547 signal(SIGTERM, tog_sigterm);
3551 halfdelay(1); /* Do fast refresh while initial view is loading. */
3554 intrflush(stdscr, FALSE);
3555 keypad(stdscr, TRUE);
3557 if (getenv("TOG_COLORS") != NULL) {
3559 use_default_colors();
3563 static const struct got_error *
3564 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3565 struct got_repository *repo, struct got_worktree *worktree)
3567 const struct got_error *err = NULL;
3570 *in_repo_path = strdup("/");
3571 if (*in_repo_path == NULL)
3572 return got_error_from_errno("strdup");
3577 const char *prefix = got_worktree_get_path_prefix(worktree);
3580 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3583 if (asprintf(in_repo_path, "%s%s%s", prefix,
3584 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3586 err = got_error_from_errno("asprintf");
3587 *in_repo_path = NULL;
3591 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3596 static const struct got_error *
3597 cmd_log(int argc, char *argv[])
3599 const struct got_error *error;
3600 struct got_repository *repo = NULL;
3601 struct got_worktree *worktree = NULL;
3602 struct got_object_id *start_id = NULL;
3603 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3604 char *start_commit = NULL, *label = NULL;
3605 struct got_reference *ref = NULL;
3606 const char *head_ref_name = NULL;
3607 int ch, log_branches = 0;
3608 struct tog_view *view;
3609 int *pack_fds = NULL;
3611 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3617 start_commit = optarg;
3620 repo_path = realpath(optarg, NULL);
3621 if (repo_path == NULL)
3622 return got_error_from_errno2("realpath",
3637 error = got_repo_pack_fds_open(&pack_fds);
3641 if (repo_path == NULL) {
3642 cwd = getcwd(NULL, 0);
3644 return got_error_from_errno("getcwd");
3645 error = got_worktree_open(&worktree, cwd);
3646 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3650 strdup(got_worktree_get_repo_path(worktree));
3652 repo_path = strdup(cwd);
3653 if (repo_path == NULL) {
3654 error = got_error_from_errno("strdup");
3659 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3663 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3670 error = apply_unveil(got_repo_get_path(repo),
3671 worktree ? got_worktree_get_root_path(worktree) : NULL);
3675 /* already loaded by tog_log_with_path()? */
3676 if (TAILQ_EMPTY(&tog_refs)) {
3677 error = tog_load_refs(repo, 0);
3682 if (start_commit == NULL) {
3683 error = got_repo_match_object_id(&start_id, &label,
3684 worktree ? got_worktree_get_head_ref_name(worktree) :
3685 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3688 head_ref_name = label;
3690 error = got_ref_open(&ref, repo, start_commit, 0);
3692 head_ref_name = got_ref_get_name(ref);
3693 else if (error->code != GOT_ERR_NOT_REF)
3695 error = got_repo_match_object_id(&start_id, NULL,
3696 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3701 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3703 error = got_error_from_errno("view_open");
3706 error = open_log_view(view, start_id, repo, head_ref_name,
3707 in_repo_path, log_branches);
3711 /* Release work tree lock. */
3712 got_worktree_close(worktree);
3715 error = view_loop(view);
3725 const struct got_error *close_err = got_repo_close(repo);
3730 got_worktree_close(worktree);
3732 const struct got_error *pack_err =
3733 got_repo_pack_fds_close(pack_fds);
3745 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3746 "[-w] object1 object2\n", getprogname());
3751 match_line(const char *line, regex_t *regex, size_t nmatch,
3752 regmatch_t *regmatch)
3754 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3757 static struct tog_color *
3758 match_color(struct tog_colors *colors, const char *line)
3760 struct tog_color *tc = NULL;
3762 STAILQ_FOREACH(tc, colors, entry) {
3763 if (match_line(line, &tc->regex, 0, NULL))
3770 static const struct got_error *
3771 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3772 WINDOW *window, int skipcol, regmatch_t *regmatch)
3774 const struct got_error *err = NULL;
3776 wchar_t *wline = NULL;
3777 int rme, rms, n, width, scrollx;
3778 int width0 = 0, width1 = 0, width2 = 0;
3779 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3783 rms = regmatch->rm_so;
3784 rme = regmatch->rm_eo;
3786 err = expand_tab(&exstr, line);
3790 /* Split the line into 3 segments, according to match offsets. */
3791 seg0 = strndup(exstr, rms);
3793 err = got_error_from_errno("strndup");
3796 seg1 = strndup(exstr + rms, rme - rms);
3798 err = got_error_from_errno("strndup");
3801 seg2 = strdup(exstr + rme);
3803 err = got_error_from_errno("strndup");
3807 /* draw up to matched token if we haven't scrolled past it */
3808 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3812 n = MAX(width0 - skipcol, 0);
3815 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3816 wlimit, col_tab_align, 1);
3819 waddwstr(window, &wline[scrollx]);
3829 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3833 wlen = wcslen(wline);
3835 width = wcwidth(wline[i]);
3837 /* should not happen, tabs are expanded */
3838 err = got_error(GOT_ERR_RANGE);
3841 if (width0 + w + width > skipcol)
3846 /* draw (visible part of) matched token (if scrolled into it) */
3847 if (width1 - w > 0) {
3848 wattron(window, A_STANDOUT);
3849 waddwstr(window, &wline[i]);
3850 wattroff(window, A_STANDOUT);
3851 wlimit -= (width1 - w);
3852 *wtotal += (width1 - w);
3856 if (wlimit > 0) { /* draw rest of line */
3858 if (skipcol > width0 + width1) {
3859 err = format_line(&wline, &width2, &scrollx, seg2,
3860 skipcol - (width0 + width1), wlimit,
3864 waddwstr(window, &wline[scrollx]);
3866 err = format_line(&wline, &width2, NULL, seg2, 0,
3867 wlimit, col_tab_align, 1);
3870 waddwstr(window, wline);
3883 static const struct got_error *
3884 draw_file(struct tog_view *view, const char *header)
3886 struct tog_diff_view_state *s = &view->state.diff;
3887 regmatch_t *regmatch = &view->regmatch;
3888 const struct got_error *err;
3891 size_t linesize = 0;
3893 struct tog_color *tc;
3896 int max_lines = view->nlines;
3897 int nlines = s->nlines;
3900 line_offset = s->line_offsets[s->first_displayed_line - 1];
3901 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3902 return got_error_from_errno("fseek");
3904 werase(view->window);
3907 if (asprintf(&line, "[%d/%d] %s",
3908 s->first_displayed_line - 1 + s->selected_line, nlines,
3910 return got_error_from_errno("asprintf");
3911 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3917 if (view_needs_focus_indication(view))
3918 wstandout(view->window);
3919 waddwstr(view->window, wline);
3922 if (view_needs_focus_indication(view))
3923 wstandend(view->window);
3924 if (width <= view->ncols - 1)
3925 waddch(view->window, '\n');
3935 while (max_lines > 0 && nprinted < max_lines) {
3936 linelen = getline(&line, &linesize, s->f);
3937 if (linelen == -1) {
3943 return got_ferror(s->f, GOT_ERR_IO);
3946 /* Set view->maxx based on full line length. */
3947 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3953 view->maxx = MAX(view->maxx, width);
3957 tc = match_color(&s->colors, line);
3959 wattr_on(view->window,
3960 COLOR_PAIR(tc->colorpair), NULL);
3961 if (s->first_displayed_line + nprinted == s->matched_line &&
3962 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3963 err = add_matched_line(&width, line, view->ncols, 0,
3964 view->window, view->x, regmatch);
3971 err = format_line(&wline, &width, &skip, line,
3972 view->x, view->ncols, 0, view->x ? 1 : 0);
3977 waddwstr(view->window, &wline[skip]);
3982 wattr_off(view->window,
3983 COLOR_PAIR(tc->colorpair), NULL);
3984 if (width <= view->ncols - 1)
3985 waddch(view->window, '\n');
3990 s->last_displayed_line = s->first_displayed_line +
3993 s->last_displayed_line = s->first_displayed_line;
3998 while (nprinted < view->nlines) {
3999 waddch(view->window, '\n');
4003 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4009 wstandout(view->window);
4010 waddwstr(view->window, wline);
4013 wstandend(view->window);
4020 get_datestr(time_t *time, char *datebuf)
4022 struct tm mytm, *tm;
4025 tm = gmtime_r(time, &mytm);
4028 s = asctime_r(tm, datebuf);
4031 p = strchr(s, '\n');
4037 static const struct got_error *
4038 get_changed_paths(struct got_pathlist_head *paths,
4039 struct got_commit_object *commit, struct got_repository *repo)
4041 const struct got_error *err = NULL;
4042 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4043 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4044 struct got_object_qid *qid;
4046 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4048 struct got_commit_object *pcommit;
4049 err = got_object_open_as_commit(&pcommit, repo,
4054 tree_id1 = got_object_id_dup(
4055 got_object_commit_get_tree_id(pcommit));
4056 if (tree_id1 == NULL) {
4057 got_object_commit_close(pcommit);
4058 return got_error_from_errno("got_object_id_dup");
4060 got_object_commit_close(pcommit);
4065 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4070 tree_id2 = got_object_commit_get_tree_id(commit);
4071 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4075 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4076 got_diff_tree_collect_changed_paths, paths, 0);
4079 got_object_tree_close(tree1);
4081 got_object_tree_close(tree2);
4086 static const struct got_error *
4087 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4091 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4093 return got_error_from_errno("reallocarray");
4095 (*line_offsets)[*nlines] = off;
4100 static const struct got_error *
4101 write_commit_info(off_t **line_offsets, size_t *nlines,
4102 struct got_object_id *commit_id, struct got_reflist_head *refs,
4103 struct got_repository *repo, FILE *outfile)
4105 const struct got_error *err = NULL;
4106 char datebuf[26], *datestr;
4107 struct got_commit_object *commit;
4108 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4109 time_t committer_time;
4110 const char *author, *committer;
4111 char *refs_str = NULL;
4112 struct got_pathlist_head changed_paths;
4113 struct got_pathlist_entry *pe;
4117 TAILQ_INIT(&changed_paths);
4120 err = build_refs_str(&refs_str, refs, commit_id, repo);
4125 err = got_object_open_as_commit(&commit, repo, commit_id);
4129 err = got_object_id_str(&id_str, commit_id);
4131 err = got_error_from_errno("got_object_id_str");
4135 err = add_line_offset(line_offsets, nlines, 0);
4139 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4140 refs_str ? refs_str : "", refs_str ? ")" : "");
4142 err = got_error_from_errno("fprintf");
4146 err = add_line_offset(line_offsets, nlines, outoff);
4150 n = fprintf(outfile, "from: %s\n",
4151 got_object_commit_get_author(commit));
4153 err = got_error_from_errno("fprintf");
4157 err = add_line_offset(line_offsets, nlines, outoff);
4161 committer_time = got_object_commit_get_committer_time(commit);
4162 datestr = get_datestr(&committer_time, datebuf);
4164 n = fprintf(outfile, "date: %s UTC\n", datestr);
4166 err = got_error_from_errno("fprintf");
4170 err = add_line_offset(line_offsets, nlines, outoff);
4174 author = got_object_commit_get_author(commit);
4175 committer = got_object_commit_get_committer(commit);
4176 if (strcmp(author, committer) != 0) {
4177 n = fprintf(outfile, "via: %s\n", committer);
4179 err = got_error_from_errno("fprintf");
4183 err = add_line_offset(line_offsets, nlines, outoff);
4187 if (got_object_commit_get_nparents(commit) > 1) {
4188 const struct got_object_id_queue *parent_ids;
4189 struct got_object_qid *qid;
4191 parent_ids = got_object_commit_get_parent_ids(commit);
4192 STAILQ_FOREACH(qid, parent_ids, entry) {
4193 err = got_object_id_str(&id_str, &qid->id);
4196 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4198 err = got_error_from_errno("fprintf");
4202 err = add_line_offset(line_offsets, nlines, outoff);
4210 err = got_object_commit_get_logmsg(&logmsg, commit);
4214 while ((line = strsep(&s, "\n")) != NULL) {
4215 n = fprintf(outfile, "%s\n", line);
4217 err = got_error_from_errno("fprintf");
4221 err = add_line_offset(line_offsets, nlines, outoff);
4226 err = get_changed_paths(&changed_paths, commit, repo);
4229 TAILQ_FOREACH(pe, &changed_paths, entry) {
4230 struct got_diff_changed_path *cp = pe->data;
4231 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4233 err = got_error_from_errno("fprintf");
4237 err = add_line_offset(line_offsets, nlines, outoff);
4240 free((char *)pe->path);
4244 fputc('\n', outfile);
4246 err = add_line_offset(line_offsets, nlines, outoff);
4248 got_pathlist_free(&changed_paths);
4252 got_object_commit_close(commit);
4254 free(*line_offsets);
4255 *line_offsets = NULL;
4261 static const struct got_error *
4262 create_diff(struct tog_diff_view_state *s)
4264 const struct got_error *err = NULL;
4268 free(s->line_offsets);
4269 s->line_offsets = malloc(sizeof(off_t));
4270 if (s->line_offsets == NULL)
4271 return got_error_from_errno("malloc");
4276 err = got_error_from_errno("got_opentemp");
4279 if (s->f && fclose(s->f) == EOF) {
4280 err = got_error_from_errno("fclose");
4286 err = got_object_get_type(&obj_type, s->repo, s->id1);
4288 err = got_object_get_type(&obj_type, s->repo, s->id2);
4293 case GOT_OBJ_TYPE_BLOB:
4294 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4295 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4296 s->label1, s->label2, tog_diff_algo, s->diff_context,
4297 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4299 case GOT_OBJ_TYPE_TREE:
4300 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4301 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4302 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4303 s->force_text_diff, s->repo, s->f);
4305 case GOT_OBJ_TYPE_COMMIT: {
4306 const struct got_object_id_queue *parent_ids;
4307 struct got_object_qid *pid;
4308 struct got_commit_object *commit2;
4309 struct got_reflist_head *refs;
4311 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4314 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4315 /* Show commit info if we're diffing to a parent/root commit. */
4316 if (s->id1 == NULL) {
4317 err = write_commit_info(&s->line_offsets, &s->nlines,
4318 s->id2, refs, s->repo, s->f);
4322 parent_ids = got_object_commit_get_parent_ids(commit2);
4323 STAILQ_FOREACH(pid, parent_ids, entry) {
4324 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4325 err = write_commit_info(
4326 &s->line_offsets, &s->nlines,
4327 s->id2, refs, s->repo, s->f);
4334 got_object_commit_close(commit2);
4336 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4337 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4338 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4339 s->force_text_diff, s->repo, s->f);
4343 err = got_error(GOT_ERR_OBJ_TYPE);
4349 if (s->f && fflush(s->f) != 0 && err == NULL)
4350 err = got_error_from_errno("fflush");
4355 diff_view_indicate_progress(struct tog_view *view)
4357 mvwaddstr(view->window, 0, 0, "diffing...");
4362 static const struct got_error *
4363 search_start_diff_view(struct tog_view *view)
4365 struct tog_diff_view_state *s = &view->state.diff;
4367 s->matched_line = 0;
4371 static const struct got_error *
4372 search_next_diff_view(struct tog_view *view)
4374 struct tog_diff_view_state *s = &view->state.diff;
4375 const struct got_error *err = NULL;
4378 size_t linesize = 0;
4381 if (!view->searching) {
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4386 if (s->matched_line) {
4387 if (view->searching == TOG_SEARCH_FORWARD)
4388 lineno = s->matched_line + 1;
4390 lineno = s->matched_line - 1;
4392 lineno = s->first_displayed_line;
4397 if (lineno <= 0 || lineno > s->nlines) {
4398 if (s->matched_line == 0) {
4399 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4403 if (view->searching == TOG_SEARCH_FORWARD)
4409 offset = s->line_offsets[lineno - 1];
4410 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4412 return got_error_from_errno("fseeko");
4414 linelen = getline(&line, &linesize, s->f);
4415 if (linelen != -1) {
4417 err = expand_tab(&exstr, line);
4420 if (match_line(exstr, &view->regex, 1,
4422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4423 s->matched_line = lineno;
4429 if (view->searching == TOG_SEARCH_FORWARD)
4436 if (s->matched_line) {
4437 s->first_displayed_line = s->matched_line;
4438 s->selected_line = 1;
4444 static const struct got_error *
4445 close_diff_view(struct tog_view *view)
4447 const struct got_error *err = NULL;
4448 struct tog_diff_view_state *s = &view->state.diff;
4454 if (s->f && fclose(s->f) == EOF)
4455 err = got_error_from_errno("fclose");
4457 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4458 err = got_error_from_errno("fclose");
4460 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4461 err = got_error_from_errno("fclose");
4463 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4464 err = got_error_from_errno("close");
4466 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4467 err = got_error_from_errno("close");
4469 free_colors(&s->colors);
4470 free(s->line_offsets);
4471 s->line_offsets = NULL;
4476 static const struct got_error *
4477 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4478 struct got_object_id *id2, const char *label1, const char *label2,
4479 int diff_context, int ignore_whitespace, int force_text_diff,
4480 struct tog_view *parent_view, struct got_repository *repo)
4482 const struct got_error *err;
4483 struct tog_diff_view_state *s = &view->state.diff;
4485 memset(s, 0, sizeof(*s));
4489 if (id1 != NULL && id2 != NULL) {
4491 err = got_object_get_type(&type1, repo, id1);
4494 err = got_object_get_type(&type2, repo, id2);
4499 return got_error(GOT_ERR_OBJ_TYPE);
4501 s->first_displayed_line = 1;
4502 s->last_displayed_line = view->nlines;
4503 s->selected_line = 1;
4511 s->id1 = got_object_id_dup(id1);
4513 return got_error_from_errno("got_object_id_dup");
4517 s->id2 = got_object_id_dup(id2);
4518 if (s->id2 == NULL) {
4519 err = got_error_from_errno("got_object_id_dup");
4523 s->f1 = got_opentemp();
4524 if (s->f1 == NULL) {
4525 err = got_error_from_errno("got_opentemp");
4529 s->f2 = got_opentemp();
4530 if (s->f2 == NULL) {
4531 err = got_error_from_errno("got_opentemp");
4535 s->fd1 = got_opentempfd();
4537 err = got_error_from_errno("got_opentempfd");
4541 s->fd2 = got_opentempfd();
4543 err = got_error_from_errno("got_opentempfd");
4547 s->first_displayed_line = 1;
4548 s->last_displayed_line = view->nlines;
4549 s->diff_context = diff_context;
4550 s->ignore_whitespace = ignore_whitespace;
4551 s->force_text_diff = force_text_diff;
4552 s->parent_view = parent_view;
4555 STAILQ_INIT(&s->colors);
4556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 err = add_color(&s->colors,
4558 "^-", TOG_COLOR_DIFF_MINUS,
4559 get_color_value("TOG_COLOR_DIFF_MINUS"));
4562 err = add_color(&s->colors, "^\\+",
4563 TOG_COLOR_DIFF_PLUS,
4564 get_color_value("TOG_COLOR_DIFF_PLUS"));
4567 err = add_color(&s->colors,
4568 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4569 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4573 err = add_color(&s->colors,
4574 "^(commit [0-9a-f]|parent [0-9]|"
4575 "(blob|file|tree|commit) [-+] |"
4576 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4577 get_color_value("TOG_COLOR_DIFF_META"));
4581 err = add_color(&s->colors,
4582 "^(from|via): ", TOG_COLOR_AUTHOR,
4583 get_color_value("TOG_COLOR_AUTHOR"));
4587 err = add_color(&s->colors,
4588 "^date: ", TOG_COLOR_DATE,
4589 get_color_value("TOG_COLOR_DATE"));
4594 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4595 view_is_splitscreen(view))
4596 show_log_view(parent_view); /* draw border */
4597 diff_view_indicate_progress(view);
4599 err = create_diff(s);
4601 view->show = show_diff_view;
4602 view->input = input_diff_view;
4603 view->reset = reset_diff_view;
4604 view->close = close_diff_view;
4605 view->search_start = search_start_diff_view;
4606 view->search_next = search_next_diff_view;
4609 close_diff_view(view);
4613 static const struct got_error *
4614 show_diff_view(struct tog_view *view)
4616 const struct got_error *err;
4617 struct tog_diff_view_state *s = &view->state.diff;
4618 char *id_str1 = NULL, *id_str2, *header;
4619 const char *label1, *label2;
4622 err = got_object_id_str(&id_str1, s->id1);
4625 label1 = s->label1 ? : id_str1;
4627 label1 = "/dev/null";
4629 err = got_object_id_str(&id_str2, s->id2);
4632 label2 = s->label2 ? : id_str2;
4634 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4635 err = got_error_from_errno("asprintf");
4643 err = draw_file(view, header);
4648 static const struct got_error *
4649 set_selected_commit(struct tog_diff_view_state *s,
4650 struct commit_queue_entry *entry)
4652 const struct got_error *err;
4653 const struct got_object_id_queue *parent_ids;
4654 struct got_commit_object *selected_commit;
4655 struct got_object_qid *pid;
4658 s->id2 = got_object_id_dup(entry->id);
4660 return got_error_from_errno("got_object_id_dup");
4662 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4665 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4667 pid = STAILQ_FIRST(parent_ids);
4668 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4669 got_object_commit_close(selected_commit);
4673 static const struct got_error *
4674 reset_diff_view(struct tog_view *view)
4676 struct tog_diff_view_state *s = &view->state.diff;
4679 wclear(view->window);
4680 s->first_displayed_line = 1;
4681 s->last_displayed_line = view->nlines;
4682 s->matched_line = 0;
4683 diff_view_indicate_progress(view);
4684 return create_diff(s);
4687 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4689 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4692 static const struct got_error *
4693 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4695 const struct got_error *err = NULL;
4696 struct tog_diff_view_state *s = &view->state.diff;
4697 struct tog_log_view_state *ls;
4698 struct commit_queue_entry *old_selected_entry;
4700 size_t linesize = 0;
4702 int i, nscroll = view->nlines - 1, up = 0;
4709 view->x = MAX(view->maxx - view->ncols / 3, 0);
4714 if (view->x + view->ncols / 3 < view->maxx)
4715 view->x += 2; /* move two columns right */
4721 view->x -= MIN(view->x, 2); /* move two columns back */
4728 s->force_text_diff = !s->force_text_diff;
4730 s->ignore_whitespace = !s->ignore_whitespace;
4731 err = reset_diff_view(view);
4735 s->first_displayed_line = 1;
4744 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4750 if (s->first_displayed_line > 1)
4751 s->first_displayed_line--;
4762 if (s->first_displayed_line == 1) {
4767 while (i++ < nscroll && s->first_displayed_line > 1)
4768 s->first_displayed_line--;
4774 s->first_displayed_line++;
4791 while (!s->eof && i++ < nscroll) {
4792 linelen = getline(&line, &linesize, s->f);
4793 s->first_displayed_line++;
4794 if (linelen == -1) {
4798 err = got_ferror(s->f, GOT_ERR_IO);
4805 if (s->diff_context > 0) {
4807 s->matched_line = 0;
4808 diff_view_indicate_progress(view);
4809 err = create_diff(s);
4810 if (s->first_displayed_line + view->nlines - 1 >
4812 s->first_displayed_line = 1;
4813 s->last_displayed_line = view->nlines;
4819 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4821 s->matched_line = 0;
4822 diff_view_indicate_progress(view);
4823 err = create_diff(s);
4835 if (s->parent_view == NULL) {
4839 s->parent_view->count = view->count;
4841 if (s->parent_view->type == TOG_VIEW_LOG) {
4842 ls = &s->parent_view->state.log;
4843 old_selected_entry = ls->selected_entry;
4845 err = input_log_view(NULL, s->parent_view,
4846 up ? KEY_UP : KEY_DOWN);
4849 view->count = s->parent_view->count;
4851 if (old_selected_entry == ls->selected_entry)
4854 err = set_selected_commit(s, ls->selected_entry);
4857 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4858 struct tog_blame_view_state *bs;
4859 struct got_object_id *id, *prev_id;
4861 bs = &s->parent_view->state.blame;
4862 prev_id = get_annotation_for_line(bs->blame.lines,
4863 bs->blame.nlines, bs->last_diffed_line);
4865 err = input_blame_view(&view, s->parent_view,
4866 up ? KEY_UP : KEY_DOWN);
4869 view->count = s->parent_view->count;
4871 if (prev_id == NULL)
4873 id = get_selected_commit_id(bs->blame.lines,
4874 bs->blame.nlines, bs->first_displayed_line,
4879 if (!got_object_id_cmp(prev_id, id))
4882 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4886 s->first_displayed_line = 1;
4887 s->last_displayed_line = view->nlines;
4888 s->matched_line = 0;
4891 diff_view_indicate_progress(view);
4892 err = create_diff(s);
4902 static const struct got_error *
4903 cmd_diff(int argc, char *argv[])
4905 const struct got_error *error = NULL;
4906 struct got_repository *repo = NULL;
4907 struct got_worktree *worktree = NULL;
4908 struct got_object_id *id1 = NULL, *id2 = NULL;
4909 char *repo_path = NULL, *cwd = NULL;
4910 char *id_str1 = NULL, *id_str2 = NULL;
4911 char *label1 = NULL, *label2 = NULL;
4912 int diff_context = 3, ignore_whitespace = 0;
4913 int ch, force_text_diff = 0;
4915 struct tog_view *view;
4916 int *pack_fds = NULL;
4918 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4921 force_text_diff = 1;
4924 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4927 errx(1, "number of context lines is %s: %s",
4931 repo_path = realpath(optarg, NULL);
4932 if (repo_path == NULL)
4933 return got_error_from_errno2("realpath",
4935 got_path_strip_trailing_slashes(repo_path);
4938 ignore_whitespace = 1;
4950 usage_diff(); /* TODO show local worktree changes */
4951 } else if (argc == 2) {
4957 error = got_repo_pack_fds_open(&pack_fds);
4961 if (repo_path == NULL) {
4962 cwd = getcwd(NULL, 0);
4964 return got_error_from_errno("getcwd");
4965 error = got_worktree_open(&worktree, cwd);
4966 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4970 strdup(got_worktree_get_repo_path(worktree));
4972 repo_path = strdup(cwd);
4973 if (repo_path == NULL) {
4974 error = got_error_from_errno("strdup");
4979 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4985 error = apply_unveil(got_repo_get_path(repo), NULL);
4989 error = tog_load_refs(repo, 0);
4993 error = got_repo_match_object_id(&id1, &label1, id_str1,
4994 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4998 error = got_repo_match_object_id(&id2, &label2, id_str2,
4999 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5003 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5005 error = got_error_from_errno("view_open");
5008 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5009 ignore_whitespace, force_text_diff, NULL, repo);
5012 error = view_loop(view);
5019 const struct got_error *close_err = got_repo_close(repo);
5024 got_worktree_close(worktree);
5026 const struct got_error *pack_err =
5027 got_repo_pack_fds_close(pack_fds);
5040 "usage: %s blame [-c commit] [-r repository-path] path\n",
5045 struct tog_blame_line {
5047 struct got_object_id *id;
5050 static const struct got_error *
5051 draw_blame(struct tog_view *view)
5053 struct tog_blame_view_state *s = &view->state.blame;
5054 struct tog_blame *blame = &s->blame;
5055 regmatch_t *regmatch = &view->regmatch;
5056 const struct got_error *err;
5057 int lineno = 0, nprinted = 0;
5059 size_t linesize = 0;
5063 struct tog_blame_line *blame_line;
5064 struct got_object_id *prev_id = NULL;
5066 struct tog_color *tc;
5068 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5073 werase(view->window);
5075 if (asprintf(&line, "commit %s", id_str) == -1) {
5076 err = got_error_from_errno("asprintf");
5081 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5086 if (view_needs_focus_indication(view))
5087 wstandout(view->window);
5088 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5090 wattr_on(view->window,
5091 COLOR_PAIR(tc->colorpair), NULL);
5092 waddwstr(view->window, wline);
5094 wattr_off(view->window,
5095 COLOR_PAIR(tc->colorpair), NULL);
5096 if (view_needs_focus_indication(view))
5097 wstandend(view->window);
5100 if (width < view->ncols - 1)
5101 waddch(view->window, '\n');
5103 if (asprintf(&line, "[%d/%d] %s%s",
5104 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5105 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5107 return got_error_from_errno("asprintf");
5110 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5115 waddwstr(view->window, wline);
5118 if (width < view->ncols - 1)
5119 waddch(view->window, '\n');
5123 while (nprinted < view->nlines - 2) {
5124 linelen = getline(&line, &linesize, blame->f);
5125 if (linelen == -1) {
5126 if (feof(blame->f)) {
5131 return got_ferror(blame->f, GOT_ERR_IO);
5133 if (++lineno < s->first_displayed_line)
5136 /* Set view->maxx based on full line length. */
5137 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5144 view->maxx = MAX(view->maxx, width);
5146 if (nprinted == s->selected_line - 1)
5147 wstandout(view->window);
5149 if (blame->nlines > 0) {
5150 blame_line = &blame->lines[lineno - 1];
5151 if (blame_line->annotated && prev_id &&
5152 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5153 !(nprinted == s->selected_line - 1)) {
5154 waddstr(view->window, " ");
5155 } else if (blame_line->annotated) {
5157 err = got_object_id_str(&id_str,
5163 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5165 wattr_on(view->window,
5166 COLOR_PAIR(tc->colorpair), NULL);
5167 wprintw(view->window, "%.8s", id_str);
5169 wattr_off(view->window,
5170 COLOR_PAIR(tc->colorpair), NULL);
5172 prev_id = blame_line->id;
5174 waddstr(view->window, "........");
5178 waddstr(view->window, "........");
5182 if (nprinted == s->selected_line - 1)
5183 wstandend(view->window);
5184 waddstr(view->window, " ");
5186 if (view->ncols <= 9) {
5188 } else if (s->first_displayed_line + nprinted ==
5190 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5191 err = add_matched_line(&width, line, view->ncols - 9, 9,
5192 view->window, view->x, regmatch);
5200 err = format_line(&wline, &width, &skip, line,
5201 view->x, view->ncols - 9, 9, 1);
5206 waddwstr(view->window, &wline[skip]);
5212 if (width <= view->ncols - 1)
5213 waddch(view->window, '\n');
5214 if (++nprinted == 1)
5215 s->first_displayed_line = lineno;
5218 s->last_displayed_line = lineno;
5225 static const struct got_error *
5226 blame_cb(void *arg, int nlines, int lineno,
5227 struct got_commit_object *commit, struct got_object_id *id)
5229 const struct got_error *err = NULL;
5230 struct tog_blame_cb_args *a = arg;
5231 struct tog_blame_line *line;
5234 if (nlines != a->nlines ||
5235 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5236 return got_error(GOT_ERR_RANGE);
5238 errcode = pthread_mutex_lock(&tog_mutex);
5240 return got_error_set_errno(errcode, "pthread_mutex_lock");
5242 if (*a->quit) { /* user has quit the blame view */
5243 err = got_error(GOT_ERR_ITER_COMPLETED);
5248 goto done; /* no change in this commit */
5250 line = &a->lines[lineno - 1];
5251 if (line->annotated)
5254 line->id = got_object_id_dup(id);
5255 if (line->id == NULL) {
5256 err = got_error_from_errno("got_object_id_dup");
5259 line->annotated = 1;
5261 errcode = pthread_mutex_unlock(&tog_mutex);
5263 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5268 blame_thread(void *arg)
5270 const struct got_error *err, *close_err;
5271 struct tog_blame_thread_args *ta = arg;
5272 struct tog_blame_cb_args *a = ta->cb_args;
5273 int errcode, fd1 = -1, fd2 = -1;
5274 FILE *f1 = NULL, *f2 = NULL;
5276 fd1 = got_opentempfd();
5278 return (void *)got_error_from_errno("got_opentempfd");
5280 fd2 = got_opentempfd();
5282 err = got_error_from_errno("got_opentempfd");
5286 f1 = got_opentemp();
5288 err = (void *)got_error_from_errno("got_opentemp");
5291 f2 = got_opentemp();
5293 err = (void *)got_error_from_errno("got_opentemp");
5297 err = block_signals_used_by_main_thread();
5301 err = got_blame(ta->path, a->commit_id, ta->repo,
5302 tog_diff_algo, blame_cb, ta->cb_args,
5303 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5304 if (err && err->code == GOT_ERR_CANCELLED)
5307 errcode = pthread_mutex_lock(&tog_mutex);
5309 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5313 close_err = got_repo_close(ta->repo);
5319 errcode = pthread_mutex_unlock(&tog_mutex);
5320 if (errcode && err == NULL)
5321 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5324 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5325 err = got_error_from_errno("close");
5326 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5327 err = got_error_from_errno("close");
5328 if (f1 && fclose(f1) == EOF && err == NULL)
5329 err = got_error_from_errno("fclose");
5330 if (f2 && fclose(f2) == EOF && err == NULL)
5331 err = got_error_from_errno("fclose");
5336 static struct got_object_id *
5337 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5338 int first_displayed_line, int selected_line)
5340 struct tog_blame_line *line;
5345 line = &lines[first_displayed_line - 1 + selected_line - 1];
5346 if (!line->annotated)
5352 static struct got_object_id *
5353 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5356 struct tog_blame_line *line;
5358 if (nlines <= 0 || lineno >= nlines)
5361 line = &lines[lineno - 1];
5362 if (!line->annotated)
5368 static const struct got_error *
5369 stop_blame(struct tog_blame *blame)
5371 const struct got_error *err = NULL;
5374 if (blame->thread) {
5376 errcode = pthread_mutex_unlock(&tog_mutex);
5378 return got_error_set_errno(errcode,
5379 "pthread_mutex_unlock");
5380 errcode = pthread_join(blame->thread, (void **)&err);
5382 return got_error_set_errno(errcode, "pthread_join");
5383 errcode = pthread_mutex_lock(&tog_mutex);
5385 return got_error_set_errno(errcode,
5386 "pthread_mutex_lock");
5387 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5389 blame->thread = NULL;
5391 if (blame->thread_args.repo) {
5392 const struct got_error *close_err;
5393 close_err = got_repo_close(blame->thread_args.repo);
5396 blame->thread_args.repo = NULL;
5399 if (fclose(blame->f) == EOF && err == NULL)
5400 err = got_error_from_errno("fclose");
5404 for (i = 0; i < blame->nlines; i++)
5405 free(blame->lines[i].id);
5407 blame->lines = NULL;
5409 free(blame->cb_args.commit_id);
5410 blame->cb_args.commit_id = NULL;
5411 if (blame->pack_fds) {
5412 const struct got_error *pack_err =
5413 got_repo_pack_fds_close(blame->pack_fds);
5416 blame->pack_fds = NULL;
5421 static const struct got_error *
5422 cancel_blame_view(void *arg)
5424 const struct got_error *err = NULL;
5428 errcode = pthread_mutex_lock(&tog_mutex);
5430 return got_error_set_errno(errcode,
5431 "pthread_mutex_unlock");
5434 err = got_error(GOT_ERR_CANCELLED);
5436 errcode = pthread_mutex_unlock(&tog_mutex);
5438 return got_error_set_errno(errcode,
5439 "pthread_mutex_lock");
5444 static const struct got_error *
5445 run_blame(struct tog_view *view)
5447 struct tog_blame_view_state *s = &view->state.blame;
5448 struct tog_blame *blame = &s->blame;
5449 const struct got_error *err = NULL;
5450 struct got_commit_object *commit = NULL;
5451 struct got_blob_object *blob = NULL;
5452 struct got_repository *thread_repo = NULL;
5453 struct got_object_id *obj_id = NULL;
5454 int obj_type, fd = -1;
5455 int *pack_fds = NULL;
5457 err = got_object_open_as_commit(&commit, s->repo,
5458 &s->blamed_commit->id);
5462 fd = got_opentempfd();
5464 err = got_error_from_errno("got_opentempfd");
5468 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5472 err = got_object_get_type(&obj_type, s->repo, obj_id);
5476 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5477 err = got_error(GOT_ERR_OBJ_TYPE);
5481 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5484 blame->f = got_opentemp();
5485 if (blame->f == NULL) {
5486 err = got_error_from_errno("got_opentemp");
5489 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5490 &blame->line_offsets, blame->f, blob);
5493 if (blame->nlines == 0) {
5494 s->blame_complete = 1;
5498 /* Don't include \n at EOF in the blame line count. */
5499 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5502 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5503 if (blame->lines == NULL) {
5504 err = got_error_from_errno("calloc");
5508 err = got_repo_pack_fds_open(&pack_fds);
5511 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5516 blame->pack_fds = pack_fds;
5517 blame->cb_args.view = view;
5518 blame->cb_args.lines = blame->lines;
5519 blame->cb_args.nlines = blame->nlines;
5520 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5521 if (blame->cb_args.commit_id == NULL) {
5522 err = got_error_from_errno("got_object_id_dup");
5525 blame->cb_args.quit = &s->done;
5527 blame->thread_args.path = s->path;
5528 blame->thread_args.repo = thread_repo;
5529 blame->thread_args.cb_args = &blame->cb_args;
5530 blame->thread_args.complete = &s->blame_complete;
5531 blame->thread_args.cancel_cb = cancel_blame_view;
5532 blame->thread_args.cancel_arg = &s->done;
5533 s->blame_complete = 0;
5535 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5536 s->first_displayed_line = 1;
5537 s->last_displayed_line = view->nlines;
5538 s->selected_line = 1;
5540 s->matched_line = 0;
5544 got_object_commit_close(commit);
5545 if (fd != -1 && close(fd) == -1 && err == NULL)
5546 err = got_error_from_errno("close");
5548 got_object_blob_close(blob);
5555 static const struct got_error *
5556 open_blame_view(struct tog_view *view, char *path,
5557 struct got_object_id *commit_id, struct got_repository *repo)
5559 const struct got_error *err = NULL;
5560 struct tog_blame_view_state *s = &view->state.blame;
5562 STAILQ_INIT(&s->blamed_commits);
5564 s->path = strdup(path);
5565 if (s->path == NULL)
5566 return got_error_from_errno("strdup");
5568 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5574 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5575 s->first_displayed_line = 1;
5576 s->last_displayed_line = view->nlines;
5577 s->selected_line = 1;
5578 s->blame_complete = 0;
5580 s->commit_id = commit_id;
5581 memset(&s->blame, 0, sizeof(s->blame));
5583 STAILQ_INIT(&s->colors);
5584 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5585 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5586 get_color_value("TOG_COLOR_COMMIT"));
5591 view->show = show_blame_view;
5592 view->input = input_blame_view;
5593 view->reset = reset_blame_view;
5594 view->close = close_blame_view;
5595 view->search_start = search_start_blame_view;
5596 view->search_next = search_next_blame_view;
5598 return run_blame(view);
5601 static const struct got_error *
5602 close_blame_view(struct tog_view *view)
5604 const struct got_error *err = NULL;
5605 struct tog_blame_view_state *s = &view->state.blame;
5607 if (s->blame.thread)
5608 err = stop_blame(&s->blame);
5610 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5611 struct got_object_qid *blamed_commit;
5612 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5613 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5614 got_object_qid_free(blamed_commit);
5618 free_colors(&s->colors);
5622 static const struct got_error *
5623 search_start_blame_view(struct tog_view *view)
5625 struct tog_blame_view_state *s = &view->state.blame;
5627 s->matched_line = 0;
5631 static const struct got_error *
5632 search_next_blame_view(struct tog_view *view)
5634 struct tog_blame_view_state *s = &view->state.blame;
5635 const struct got_error *err = NULL;
5638 size_t linesize = 0;
5641 if (!view->searching) {
5642 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5646 if (s->matched_line) {
5647 if (view->searching == TOG_SEARCH_FORWARD)
5648 lineno = s->matched_line + 1;
5650 lineno = s->matched_line - 1;
5652 lineno = s->first_displayed_line - 1 + s->selected_line;
5657 if (lineno <= 0 || lineno > s->blame.nlines) {
5658 if (s->matched_line == 0) {
5659 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5663 if (view->searching == TOG_SEARCH_FORWARD)
5666 lineno = s->blame.nlines;
5669 offset = s->blame.line_offsets[lineno - 1];
5670 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5672 return got_error_from_errno("fseeko");
5674 linelen = getline(&line, &linesize, s->blame.f);
5675 if (linelen != -1) {
5677 err = expand_tab(&exstr, line);
5680 if (match_line(exstr, &view->regex, 1,
5682 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5683 s->matched_line = lineno;
5689 if (view->searching == TOG_SEARCH_FORWARD)
5696 if (s->matched_line) {
5697 s->first_displayed_line = s->matched_line;
5698 s->selected_line = 1;
5704 static const struct got_error *
5705 show_blame_view(struct tog_view *view)
5707 const struct got_error *err = NULL;
5708 struct tog_blame_view_state *s = &view->state.blame;
5711 if (s->blame.thread == NULL && !s->blame_complete) {
5712 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5713 &s->blame.thread_args);
5715 return got_error_set_errno(errcode, "pthread_create");
5717 halfdelay(1); /* fast refresh while annotating */
5720 if (s->blame_complete)
5721 halfdelay(10); /* disable fast refresh */
5723 err = draw_blame(view);
5729 static const struct got_error *
5730 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5731 struct got_repository *repo, struct got_object_id *id)
5733 struct tog_view *log_view;
5734 const struct got_error *err = NULL;
5738 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5739 if (log_view == NULL)
5740 return got_error_from_errno("view_open");
5742 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5744 view_close(log_view);
5746 *new_view = log_view;
5751 static const struct got_error *
5752 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5754 const struct got_error *err = NULL, *thread_err = NULL;
5755 struct tog_view *diff_view, *log_view;
5756 struct tog_blame_view_state *s = &view->state.blame;
5757 int eos, nscroll, begin_y = 0, begin_x = 0;
5759 eos = nscroll = view->nlines - 2;
5760 if (view_is_hsplit_top(view))
5768 view->x = MAX(view->maxx - view->ncols / 3, 0);
5773 if (view->x + view->ncols / 3 < view->maxx)
5774 view->x += 2; /* move two columns right */
5780 view->x -= MIN(view->x, 2); /* move two columns back */
5789 s->selected_line = 1;
5790 s->first_displayed_line = 1;
5795 if (s->blame.nlines < eos) {
5796 s->selected_line = s->blame.nlines;
5797 s->first_displayed_line = 1;
5799 s->selected_line = eos;
5800 s->first_displayed_line = s->blame.nlines - (eos - 1);
5807 if (s->selected_line > 1)
5809 else if (s->selected_line == 1 &&
5810 s->first_displayed_line > 1)
5811 s->first_displayed_line--;
5822 if (s->first_displayed_line == 1) {
5823 if (view->count > 1)
5825 s->selected_line = MAX(1, s->selected_line - nscroll);
5829 if (s->first_displayed_line > nscroll)
5830 s->first_displayed_line -= nscroll;
5832 s->first_displayed_line = 1;
5837 if (s->selected_line < eos && s->first_displayed_line +
5838 s->selected_line <= s->blame.nlines)
5840 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5841 s->first_displayed_line++;
5847 struct got_object_id *id = NULL;
5850 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5851 s->first_displayed_line, s->selected_line);
5855 struct got_commit_object *commit, *pcommit;
5856 struct got_object_qid *pid;
5857 struct got_object_id *blob_id = NULL;
5859 err = got_object_open_as_commit(&commit,
5864 got_object_commit_get_parent_ids(commit));
5866 got_object_commit_close(commit);
5869 /* Check if path history ends here. */
5870 err = got_object_open_as_commit(&pcommit,
5874 err = got_object_id_by_path(&blob_id, s->repo,
5876 got_object_commit_close(pcommit);
5878 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5880 got_object_commit_close(commit);
5883 err = got_object_get_type(&obj_type, s->repo,
5886 /* Can't blame non-blob type objects. */
5887 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5888 got_object_commit_close(commit);
5891 err = got_object_qid_alloc(&s->blamed_commit,
5893 got_object_commit_close(commit);
5895 if (got_object_id_cmp(id,
5896 &s->blamed_commit->id) == 0)
5898 err = got_object_qid_alloc(&s->blamed_commit,
5904 thread_err = stop_blame(&s->blame);
5908 STAILQ_INSERT_HEAD(&s->blamed_commits,
5909 s->blamed_commit, entry);
5910 err = run_blame(view);
5916 struct got_object_qid *first;
5919 first = STAILQ_FIRST(&s->blamed_commits);
5920 if (!got_object_id_cmp(&first->id, s->commit_id))
5923 thread_err = stop_blame(&s->blame);
5927 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5928 got_object_qid_free(s->blamed_commit);
5930 STAILQ_FIRST(&s->blamed_commits);
5931 err = run_blame(view);
5937 struct got_object_id *id = NULL;
5940 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5941 s->first_displayed_line, s->selected_line);
5945 if (view_is_parent_view(view))
5946 view_get_split(view, &begin_y, &begin_x);
5947 err = log_annotated_line(&log_view, begin_y, begin_x,
5951 if (view_is_parent_view(view) &&
5952 view->mode == TOG_VIEW_SPLIT_HRZN) {
5953 err = view_init_hsplit(view, begin_y);
5959 log_view->focussed = 1;
5960 log_view->mode = view->mode;
5961 log_view->nlines = view->lines - begin_y;
5962 if (view_is_parent_view(view)) {
5963 view_transfer_size(log_view, view);
5964 err = view_close_child(view);
5967 err = view_set_child(view, log_view);
5970 view->focus_child = 1;
5972 *new_view = log_view;
5977 struct got_object_id *id = NULL;
5978 struct got_object_qid *pid;
5979 struct got_commit_object *commit = NULL;
5982 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5983 s->first_displayed_line, s->selected_line);
5986 err = got_object_open_as_commit(&commit, s->repo, id);
5989 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5991 /* traversed from diff view, release diff resources */
5992 err = close_diff_view(*new_view);
5995 diff_view = *new_view;
5997 if (view_is_parent_view(view))
5998 view_get_split(view, &begin_y, &begin_x);
6000 diff_view = view_open(0, 0, begin_y, begin_x,
6002 if (diff_view == NULL) {
6003 got_object_commit_close(commit);
6004 err = got_error_from_errno("view_open");
6008 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6009 id, NULL, NULL, 3, 0, 0, view, s->repo);
6010 got_object_commit_close(commit);
6012 view_close(diff_view);
6015 s->last_diffed_line = s->first_displayed_line - 1 +
6018 break; /* still open from active diff view */
6019 if (view_is_parent_view(view) &&
6020 view->mode == TOG_VIEW_SPLIT_HRZN) {
6021 err = view_init_hsplit(view, begin_y);
6027 diff_view->focussed = 1;
6028 diff_view->mode = view->mode;
6029 diff_view->nlines = view->lines - begin_y;
6030 if (view_is_parent_view(view)) {
6031 view_transfer_size(diff_view, view);
6032 err = view_close_child(view);
6035 err = view_set_child(view, diff_view);
6038 view->focus_child = 1;
6040 *new_view = diff_view;
6053 if (s->last_displayed_line >= s->blame.nlines &&
6054 s->selected_line >= MIN(s->blame.nlines,
6055 view->nlines - 2)) {
6059 if (s->last_displayed_line >= s->blame.nlines &&
6060 s->selected_line < view->nlines - 2) {
6062 MIN(nscroll, s->last_displayed_line -
6063 s->first_displayed_line - s->selected_line + 1);
6065 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6066 s->first_displayed_line += nscroll;
6068 s->first_displayed_line =
6069 s->blame.nlines - (view->nlines - 3);
6072 if (s->selected_line > view->nlines - 2) {
6073 s->selected_line = MIN(s->blame.nlines,
6081 return thread_err ? thread_err : err;
6084 static const struct got_error *
6085 reset_blame_view(struct tog_view *view)
6087 const struct got_error *err;
6088 struct tog_blame_view_state *s = &view->state.blame;
6092 err = stop_blame(&s->blame);
6096 return run_blame(view);
6099 static const struct got_error *
6100 cmd_blame(int argc, char *argv[])
6102 const struct got_error *error;
6103 struct got_repository *repo = NULL;
6104 struct got_worktree *worktree = NULL;
6105 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6106 char *link_target = NULL;
6107 struct got_object_id *commit_id = NULL;
6108 struct got_commit_object *commit = NULL;
6109 char *commit_id_str = NULL;
6111 struct tog_view *view;
6112 int *pack_fds = NULL;
6114 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6117 commit_id_str = optarg;
6120 repo_path = realpath(optarg, NULL);
6121 if (repo_path == NULL)
6122 return got_error_from_errno2("realpath",
6137 error = got_repo_pack_fds_open(&pack_fds);
6141 if (repo_path == NULL) {
6142 cwd = getcwd(NULL, 0);
6144 return got_error_from_errno("getcwd");
6145 error = got_worktree_open(&worktree, cwd);
6146 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6150 strdup(got_worktree_get_repo_path(worktree));
6152 repo_path = strdup(cwd);
6153 if (repo_path == NULL) {
6154 error = got_error_from_errno("strdup");
6159 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6163 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6170 error = apply_unveil(got_repo_get_path(repo), NULL);
6174 error = tog_load_refs(repo, 0);
6178 if (commit_id_str == NULL) {
6179 struct got_reference *head_ref;
6180 error = got_ref_open(&head_ref, repo, worktree ?
6181 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6184 error = got_ref_resolve(&commit_id, repo, head_ref);
6185 got_ref_close(head_ref);
6187 error = got_repo_match_object_id(&commit_id, NULL,
6188 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6193 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6195 error = got_error_from_errno("view_open");
6199 error = got_object_open_as_commit(&commit, repo, commit_id);
6203 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6208 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6213 /* Release work tree lock. */
6214 got_worktree_close(worktree);
6217 error = view_loop(view);
6225 got_object_commit_close(commit);
6227 got_worktree_close(worktree);
6229 const struct got_error *close_err = got_repo_close(repo);
6234 const struct got_error *pack_err =
6235 got_repo_pack_fds_close(pack_fds);
6243 static const struct got_error *
6244 draw_tree_entries(struct tog_view *view, const char *parent_path)
6246 struct tog_tree_view_state *s = &view->state.tree;
6247 const struct got_error *err = NULL;
6248 struct got_tree_entry *te;
6250 struct tog_color *tc;
6251 int width, n, i, nentries;
6252 int limit = view->nlines;
6255 if (view_is_hsplit_top(view))
6256 --limit; /* border */
6258 werase(view->window);
6263 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6267 if (view_needs_focus_indication(view))
6268 wstandout(view->window);
6269 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6271 wattr_on(view->window,
6272 COLOR_PAIR(tc->colorpair), NULL);
6273 waddwstr(view->window, wline);
6275 wattr_off(view->window,
6276 COLOR_PAIR(tc->colorpair), NULL);
6277 if (view_needs_focus_indication(view))
6278 wstandend(view->window);
6281 if (width < view->ncols - 1)
6282 waddch(view->window, '\n');
6285 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6289 waddwstr(view->window, wline);
6292 if (width < view->ncols - 1)
6293 waddch(view->window, '\n');
6296 waddch(view->window, '\n');
6300 if (s->first_displayed_entry == NULL) {
6301 te = got_object_tree_get_first_entry(s->tree);
6302 if (s->selected == 0) {
6304 wstandout(view->window);
6305 s->selected_entry = NULL;
6307 waddstr(view->window, " ..\n"); /* parent directory */
6308 if (s->selected == 0 && view->focussed)
6309 wstandend(view->window);
6316 te = s->first_displayed_entry;
6319 nentries = got_object_tree_get_nentries(s->tree);
6320 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6321 char *line = NULL, *id_str = NULL, *link_target = NULL;
6322 const char *modestr = "";
6325 te = got_object_tree_get_entry(s->tree, i);
6326 mode = got_tree_entry_get_mode(te);
6329 err = got_object_id_str(&id_str,
6330 got_tree_entry_get_id(te));
6332 return got_error_from_errno(
6333 "got_object_id_str");
6335 if (got_object_tree_entry_is_submodule(te))
6337 else if (S_ISLNK(mode)) {
6340 err = got_tree_entry_get_symlink_target(&link_target,
6346 for (i = 0; i < strlen(link_target); i++) {
6347 if (!isprint((unsigned char)link_target[i]))
6348 link_target[i] = '?';
6352 else if (S_ISDIR(mode))
6354 else if (mode & S_IXUSR)
6356 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6357 got_tree_entry_get_name(te), modestr,
6358 link_target ? " -> ": "",
6359 link_target ? link_target : "") == -1) {
6362 return got_error_from_errno("asprintf");
6366 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6372 if (n == s->selected) {
6374 wstandout(view->window);
6375 s->selected_entry = te;
6377 tc = match_color(&s->colors, line);
6379 wattr_on(view->window,
6380 COLOR_PAIR(tc->colorpair), NULL);
6381 waddwstr(view->window, wline);
6383 wattr_off(view->window,
6384 COLOR_PAIR(tc->colorpair), NULL);
6385 if (width < view->ncols - 1)
6386 waddch(view->window, '\n');
6387 if (n == s->selected && view->focussed)
6388 wstandend(view->window);
6394 s->last_displayed_entry = te;
6403 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6405 struct got_tree_entry *te;
6406 int isroot = s->tree == s->root;
6409 if (s->first_displayed_entry == NULL)
6412 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6413 while (i++ < maxscroll) {
6416 s->first_displayed_entry = NULL;
6419 s->first_displayed_entry = te;
6420 te = got_tree_entry_get_prev(s->tree, te);
6424 static const struct got_error *
6425 tree_scroll_down(struct tog_view *view, int maxscroll)
6427 struct tog_tree_view_state *s = &view->state.tree;
6428 struct got_tree_entry *next, *last;
6431 if (s->first_displayed_entry)
6432 next = got_tree_entry_get_next(s->tree,
6433 s->first_displayed_entry);
6435 next = got_object_tree_get_first_entry(s->tree);
6437 last = s->last_displayed_entry;
6438 while (next && n++ < maxscroll) {
6440 last = got_tree_entry_get_next(s->tree, last);
6441 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6442 s->first_displayed_entry = next;
6443 next = got_tree_entry_get_next(s->tree, next);
6450 static const struct got_error *
6451 tree_entry_path(char **path, struct tog_parent_trees *parents,
6452 struct got_tree_entry *te)
6454 const struct got_error *err = NULL;
6455 struct tog_parent_tree *pt;
6456 size_t len = 2; /* for leading slash and NUL */
6458 TAILQ_FOREACH(pt, parents, entry)
6459 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6462 len += strlen(got_tree_entry_get_name(te));
6464 *path = calloc(1, len);
6466 return got_error_from_errno("calloc");
6469 pt = TAILQ_LAST(parents, tog_parent_trees);
6471 const char *name = got_tree_entry_get_name(pt->selected_entry);
6472 if (strlcat(*path, name, len) >= len) {
6473 err = got_error(GOT_ERR_NO_SPACE);
6476 if (strlcat(*path, "/", len) >= len) {
6477 err = got_error(GOT_ERR_NO_SPACE);
6480 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6483 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6484 err = got_error(GOT_ERR_NO_SPACE);
6496 static const struct got_error *
6497 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6498 struct got_tree_entry *te, struct tog_parent_trees *parents,
6499 struct got_object_id *commit_id, struct got_repository *repo)
6501 const struct got_error *err = NULL;
6503 struct tog_view *blame_view;
6507 err = tree_entry_path(&path, parents, te);
6511 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6512 if (blame_view == NULL) {
6513 err = got_error_from_errno("view_open");
6517 err = open_blame_view(blame_view, path, commit_id, repo);
6519 if (err->code == GOT_ERR_CANCELLED)
6521 view_close(blame_view);
6523 *new_view = blame_view;
6529 static const struct got_error *
6530 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6531 struct tog_tree_view_state *s)
6533 struct tog_view *log_view;
6534 const struct got_error *err = NULL;
6539 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6540 if (log_view == NULL)
6541 return got_error_from_errno("view_open");
6543 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6547 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6550 view_close(log_view);
6552 *new_view = log_view;
6557 static const struct got_error *
6558 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6559 const char *head_ref_name, struct got_repository *repo)
6561 const struct got_error *err = NULL;
6562 char *commit_id_str = NULL;
6563 struct tog_tree_view_state *s = &view->state.tree;
6564 struct got_commit_object *commit = NULL;
6566 TAILQ_INIT(&s->parents);
6567 STAILQ_INIT(&s->colors);
6569 s->commit_id = got_object_id_dup(commit_id);
6570 if (s->commit_id == NULL)
6571 return got_error_from_errno("got_object_id_dup");
6573 err = got_object_open_as_commit(&commit, repo, commit_id);
6578 * The root is opened here and will be closed when the view is closed.
6579 * Any visited subtrees and their path-wise parents are opened and
6582 err = got_object_open_as_tree(&s->root, repo,
6583 got_object_commit_get_tree_id(commit));
6588 err = got_object_id_str(&commit_id_str, commit_id);
6592 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6593 err = got_error_from_errno("asprintf");
6597 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6598 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6599 if (head_ref_name) {
6600 s->head_ref_name = strdup(head_ref_name);
6601 if (s->head_ref_name == NULL) {
6602 err = got_error_from_errno("strdup");
6608 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6609 err = add_color(&s->colors, "\\$$",
6610 TOG_COLOR_TREE_SUBMODULE,
6611 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6614 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6615 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6618 err = add_color(&s->colors, "/$",
6619 TOG_COLOR_TREE_DIRECTORY,
6620 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6624 err = add_color(&s->colors, "\\*$",
6625 TOG_COLOR_TREE_EXECUTABLE,
6626 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6630 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6631 get_color_value("TOG_COLOR_COMMIT"));
6636 view->show = show_tree_view;
6637 view->input = input_tree_view;
6638 view->close = close_tree_view;
6639 view->search_start = search_start_tree_view;
6640 view->search_next = search_next_tree_view;
6642 free(commit_id_str);
6644 got_object_commit_close(commit);
6646 close_tree_view(view);
6650 static const struct got_error *
6651 close_tree_view(struct tog_view *view)
6653 struct tog_tree_view_state *s = &view->state.tree;
6655 free_colors(&s->colors);
6656 free(s->tree_label);
6657 s->tree_label = NULL;
6659 s->commit_id = NULL;
6660 free(s->head_ref_name);
6661 s->head_ref_name = NULL;
6662 while (!TAILQ_EMPTY(&s->parents)) {
6663 struct tog_parent_tree *parent;
6664 parent = TAILQ_FIRST(&s->parents);
6665 TAILQ_REMOVE(&s->parents, parent, entry);
6666 if (parent->tree != s->root)
6667 got_object_tree_close(parent->tree);
6671 if (s->tree != NULL && s->tree != s->root)
6672 got_object_tree_close(s->tree);
6674 got_object_tree_close(s->root);
6678 static const struct got_error *
6679 search_start_tree_view(struct tog_view *view)
6681 struct tog_tree_view_state *s = &view->state.tree;
6683 s->matched_entry = NULL;
6688 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6690 regmatch_t regmatch;
6692 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6696 static const struct got_error *
6697 search_next_tree_view(struct tog_view *view)
6699 struct tog_tree_view_state *s = &view->state.tree;
6700 struct got_tree_entry *te = NULL;
6702 if (!view->searching) {
6703 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6707 if (s->matched_entry) {
6708 if (view->searching == TOG_SEARCH_FORWARD) {
6709 if (s->selected_entry)
6710 te = got_tree_entry_get_next(s->tree,
6713 te = got_object_tree_get_first_entry(s->tree);
6715 if (s->selected_entry == NULL)
6716 te = got_object_tree_get_last_entry(s->tree);
6718 te = got_tree_entry_get_prev(s->tree,
6722 if (s->selected_entry)
6723 te = s->selected_entry;
6724 else if (view->searching == TOG_SEARCH_FORWARD)
6725 te = got_object_tree_get_first_entry(s->tree);
6727 te = got_object_tree_get_last_entry(s->tree);
6732 if (s->matched_entry == NULL) {
6733 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6736 if (view->searching == TOG_SEARCH_FORWARD)
6737 te = got_object_tree_get_first_entry(s->tree);
6739 te = got_object_tree_get_last_entry(s->tree);
6742 if (match_tree_entry(te, &view->regex)) {
6743 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6744 s->matched_entry = te;
6748 if (view->searching == TOG_SEARCH_FORWARD)
6749 te = got_tree_entry_get_next(s->tree, te);
6751 te = got_tree_entry_get_prev(s->tree, te);
6754 if (s->matched_entry) {
6755 s->first_displayed_entry = s->matched_entry;
6762 static const struct got_error *
6763 show_tree_view(struct tog_view *view)
6765 const struct got_error *err = NULL;
6766 struct tog_tree_view_state *s = &view->state.tree;
6769 err = tree_entry_path(&parent_path, &s->parents, NULL);
6773 err = draw_tree_entries(view, parent_path);
6780 static const struct got_error *
6781 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6783 const struct got_error *err = NULL;
6784 struct tog_tree_view_state *s = &view->state.tree;
6785 struct tog_view *log_view, *ref_view;
6786 struct got_tree_entry *te;
6787 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6791 s->show_ids = !s->show_ids;
6796 if (!s->selected_entry)
6798 if (view_is_parent_view(view))
6799 view_get_split(view, &begin_y, &begin_x);
6800 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6801 if (view_is_parent_view(view) &&
6802 view->mode == TOG_VIEW_SPLIT_HRZN) {
6803 err = view_init_hsplit(view, begin_y);
6808 log_view->focussed = 1;
6809 log_view->mode = view->mode;
6810 log_view->nlines = view->lines - begin_y;
6811 if (view_is_parent_view(view)) {
6812 view_transfer_size(log_view, view);
6813 err = view_close_child(view);
6816 err = view_set_child(view, log_view);
6819 view->focus_child = 1;
6821 *new_view = log_view;
6825 if (view_is_parent_view(view))
6826 view_get_split(view, &begin_y, &begin_x);
6827 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6828 if (ref_view == NULL)
6829 return got_error_from_errno("view_open");
6830 err = open_ref_view(ref_view, s->repo);
6832 view_close(ref_view);
6835 if (view_is_parent_view(view) &&
6836 view->mode == TOG_VIEW_SPLIT_HRZN) {
6837 err = view_init_hsplit(view, begin_y);
6842 ref_view->focussed = 1;
6843 ref_view->mode = view->mode;
6844 ref_view->nlines = view->lines - begin_y;
6845 if (view_is_parent_view(view)) {
6846 view_transfer_size(ref_view, view);
6847 err = view_close_child(view);
6850 err = view_set_child(view, ref_view);
6853 view->focus_child = 1;
6855 *new_view = ref_view;
6861 if (s->tree == s->root)
6862 s->first_displayed_entry =
6863 got_object_tree_get_first_entry(s->tree);
6865 s->first_displayed_entry = NULL;
6869 int eos = view->nlines - 3;
6871 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6875 te = got_object_tree_get_last_entry(s->tree);
6876 for (n = 0; n < eos; n++) {
6878 if (s->tree != s->root) {
6879 s->first_displayed_entry = NULL;
6884 s->first_displayed_entry = te;
6885 te = got_tree_entry_get_prev(s->tree, te);
6888 s->selected = n - 1;
6894 if (s->selected > 0) {
6898 tree_scroll_up(s, 1);
6899 if (s->selected_entry == NULL ||
6900 (s->tree == s->root && s->selected_entry ==
6901 got_object_tree_get_first_entry(s->tree)))
6911 if (s->tree == s->root) {
6912 if (got_object_tree_get_first_entry(s->tree) ==
6913 s->first_displayed_entry)
6914 s->selected -= MIN(s->selected, nscroll);
6916 if (s->first_displayed_entry == NULL)
6917 s->selected -= MIN(s->selected, nscroll);
6919 tree_scroll_up(s, MAX(0, nscroll));
6920 if (s->selected_entry == NULL ||
6921 (s->tree == s->root && s->selected_entry ==
6922 got_object_tree_get_first_entry(s->tree)))
6928 if (s->selected < s->ndisplayed - 1) {
6932 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6934 /* can't scroll any further */
6938 tree_scroll_down(view, 1);
6948 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6950 /* can't scroll any further; move cursor down */
6951 if (s->selected < s->ndisplayed - 1)
6952 s->selected += MIN(nscroll,
6953 s->ndisplayed - s->selected - 1);
6958 tree_scroll_down(view, nscroll);
6963 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6964 struct tog_parent_tree *parent;
6965 /* user selected '..' */
6966 if (s->tree == s->root) {
6970 parent = TAILQ_FIRST(&s->parents);
6971 TAILQ_REMOVE(&s->parents, parent,
6973 got_object_tree_close(s->tree);
6974 s->tree = parent->tree;
6975 s->first_displayed_entry =
6976 parent->first_displayed_entry;
6978 parent->selected_entry;
6979 s->selected = parent->selected;
6980 if (s->selected > view->nlines - 3) {
6981 err = offset_selection_down(view);
6986 } else if (S_ISDIR(got_tree_entry_get_mode(
6987 s->selected_entry))) {
6988 struct got_tree_object *subtree;
6990 err = got_object_open_as_tree(&subtree, s->repo,
6991 got_tree_entry_get_id(s->selected_entry));
6994 err = tree_view_visit_subtree(s, subtree);
6996 got_object_tree_close(subtree);
6999 } else if (S_ISREG(got_tree_entry_get_mode(
7000 s->selected_entry))) {
7001 struct tog_view *blame_view;
7002 int begin_x = 0, begin_y = 0;
7004 if (view_is_parent_view(view))
7005 view_get_split(view, &begin_y, &begin_x);
7007 err = blame_tree_entry(&blame_view, begin_y, begin_x,
7008 s->selected_entry, &s->parents,
7009 s->commit_id, s->repo);
7013 if (view_is_parent_view(view) &&
7014 view->mode == TOG_VIEW_SPLIT_HRZN) {
7015 err = view_init_hsplit(view, begin_y);
7022 blame_view->focussed = 1;
7023 blame_view->mode = view->mode;
7024 blame_view->nlines = view->lines - begin_y;
7025 if (view_is_parent_view(view)) {
7026 view_transfer_size(blame_view, view);
7027 err = view_close_child(view);
7030 err = view_set_child(view, blame_view);
7033 view->focus_child = 1;
7035 *new_view = blame_view;
7039 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7040 s->selected = view->nlines - 4;
7056 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7061 static const struct got_error *
7062 cmd_tree(int argc, char *argv[])
7064 const struct got_error *error;
7065 struct got_repository *repo = NULL;
7066 struct got_worktree *worktree = NULL;
7067 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7068 struct got_object_id *commit_id = NULL;
7069 struct got_commit_object *commit = NULL;
7070 const char *commit_id_arg = NULL;
7072 struct got_reference *ref = NULL;
7073 const char *head_ref_name = NULL;
7075 struct tog_view *view;
7076 int *pack_fds = NULL;
7078 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7081 commit_id_arg = optarg;
7084 repo_path = realpath(optarg, NULL);
7085 if (repo_path == NULL)
7086 return got_error_from_errno2("realpath",
7101 error = got_repo_pack_fds_open(&pack_fds);
7105 if (repo_path == NULL) {
7106 cwd = getcwd(NULL, 0);
7108 return got_error_from_errno("getcwd");
7109 error = got_worktree_open(&worktree, cwd);
7110 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7114 strdup(got_worktree_get_repo_path(worktree));
7116 repo_path = strdup(cwd);
7117 if (repo_path == NULL) {
7118 error = got_error_from_errno("strdup");
7123 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7127 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7134 error = apply_unveil(got_repo_get_path(repo), NULL);
7138 error = tog_load_refs(repo, 0);
7142 if (commit_id_arg == NULL) {
7143 error = got_repo_match_object_id(&commit_id, &label,
7144 worktree ? got_worktree_get_head_ref_name(worktree) :
7145 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7148 head_ref_name = label;
7150 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7152 head_ref_name = got_ref_get_name(ref);
7153 else if (error->code != GOT_ERR_NOT_REF)
7155 error = got_repo_match_object_id(&commit_id, NULL,
7156 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7161 error = got_object_open_as_commit(&commit, repo, commit_id);
7165 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7167 error = got_error_from_errno("view_open");
7170 error = open_tree_view(view, commit_id, head_ref_name, repo);
7173 if (!got_path_is_root_dir(in_repo_path)) {
7174 error = tree_view_walk_path(&view->state.tree, commit,
7181 /* Release work tree lock. */
7182 got_worktree_close(worktree);
7185 error = view_loop(view);
7194 const struct got_error *close_err = got_repo_close(repo);
7199 const struct got_error *pack_err =
7200 got_repo_pack_fds_close(pack_fds);
7208 static const struct got_error *
7209 ref_view_load_refs(struct tog_ref_view_state *s)
7211 struct got_reflist_entry *sre;
7212 struct tog_reflist_entry *re;
7215 TAILQ_FOREACH(sre, &tog_refs, entry) {
7216 if (strncmp(got_ref_get_name(sre->ref),
7217 "refs/got/", 9) == 0 &&
7218 strncmp(got_ref_get_name(sre->ref),
7219 "refs/got/backup/", 16) != 0)
7222 re = malloc(sizeof(*re));
7224 return got_error_from_errno("malloc");
7226 re->ref = got_ref_dup(sre->ref);
7227 if (re->ref == NULL)
7228 return got_error_from_errno("got_ref_dup");
7229 re->idx = s->nrefs++;
7230 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7233 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7238 ref_view_free_refs(struct tog_ref_view_state *s)
7240 struct tog_reflist_entry *re;
7242 while (!TAILQ_EMPTY(&s->refs)) {
7243 re = TAILQ_FIRST(&s->refs);
7244 TAILQ_REMOVE(&s->refs, re, entry);
7245 got_ref_close(re->ref);
7250 static const struct got_error *
7251 open_ref_view(struct tog_view *view, struct got_repository *repo)
7253 const struct got_error *err = NULL;
7254 struct tog_ref_view_state *s = &view->state.ref;
7256 s->selected_entry = 0;
7259 TAILQ_INIT(&s->refs);
7260 STAILQ_INIT(&s->colors);
7262 err = ref_view_load_refs(s);
7266 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7267 err = add_color(&s->colors, "^refs/heads/",
7268 TOG_COLOR_REFS_HEADS,
7269 get_color_value("TOG_COLOR_REFS_HEADS"));
7273 err = add_color(&s->colors, "^refs/tags/",
7274 TOG_COLOR_REFS_TAGS,
7275 get_color_value("TOG_COLOR_REFS_TAGS"));
7279 err = add_color(&s->colors, "^refs/remotes/",
7280 TOG_COLOR_REFS_REMOTES,
7281 get_color_value("TOG_COLOR_REFS_REMOTES"));
7285 err = add_color(&s->colors, "^refs/got/backup/",
7286 TOG_COLOR_REFS_BACKUP,
7287 get_color_value("TOG_COLOR_REFS_BACKUP"));
7292 view->show = show_ref_view;
7293 view->input = input_ref_view;
7294 view->close = close_ref_view;
7295 view->search_start = search_start_ref_view;
7296 view->search_next = search_next_ref_view;
7299 free_colors(&s->colors);
7303 static const struct got_error *
7304 close_ref_view(struct tog_view *view)
7306 struct tog_ref_view_state *s = &view->state.ref;
7308 ref_view_free_refs(s);
7309 free_colors(&s->colors);
7314 static const struct got_error *
7315 resolve_reflist_entry(struct got_object_id **commit_id,
7316 struct tog_reflist_entry *re, struct got_repository *repo)
7318 const struct got_error *err = NULL;
7319 struct got_object_id *obj_id;
7320 struct got_tag_object *tag = NULL;
7325 err = got_ref_resolve(&obj_id, repo, re->ref);
7329 err = got_object_get_type(&obj_type, repo, obj_id);
7334 case GOT_OBJ_TYPE_COMMIT:
7335 *commit_id = obj_id;
7337 case GOT_OBJ_TYPE_TAG:
7338 err = got_object_open_as_tag(&tag, repo, obj_id);
7342 err = got_object_get_type(&obj_type, repo,
7343 got_object_tag_get_object_id(tag));
7346 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7347 err = got_error(GOT_ERR_OBJ_TYPE);
7350 *commit_id = got_object_id_dup(
7351 got_object_tag_get_object_id(tag));
7352 if (*commit_id == NULL) {
7353 err = got_error_from_errno("got_object_id_dup");
7358 err = got_error(GOT_ERR_OBJ_TYPE);
7364 got_object_tag_close(tag);
7372 static const struct got_error *
7373 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7374 struct tog_reflist_entry *re, struct got_repository *repo)
7376 struct tog_view *log_view;
7377 const struct got_error *err = NULL;
7378 struct got_object_id *commit_id = NULL;
7382 err = resolve_reflist_entry(&commit_id, re, repo);
7384 if (err->code != GOT_ERR_OBJ_TYPE)
7390 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7391 if (log_view == NULL) {
7392 err = got_error_from_errno("view_open");
7396 err = open_log_view(log_view, commit_id, repo,
7397 got_ref_get_name(re->ref), "", 0);
7400 view_close(log_view);
7402 *new_view = log_view;
7408 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7410 struct tog_reflist_entry *re;
7413 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7416 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7417 while (i++ < maxscroll) {
7420 s->first_displayed_entry = re;
7421 re = TAILQ_PREV(re, tog_reflist_head, entry);
7425 static const struct got_error *
7426 ref_scroll_down(struct tog_view *view, int maxscroll)
7428 struct tog_ref_view_state *s = &view->state.ref;
7429 struct tog_reflist_entry *next, *last;
7432 if (s->first_displayed_entry)
7433 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7435 next = TAILQ_FIRST(&s->refs);
7437 last = s->last_displayed_entry;
7438 while (next && n++ < maxscroll) {
7440 last = TAILQ_NEXT(last, entry);
7441 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7442 s->first_displayed_entry = next;
7443 next = TAILQ_NEXT(next, entry);
7450 static const struct got_error *
7451 search_start_ref_view(struct tog_view *view)
7453 struct tog_ref_view_state *s = &view->state.ref;
7455 s->matched_entry = NULL;
7460 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7462 regmatch_t regmatch;
7464 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7468 static const struct got_error *
7469 search_next_ref_view(struct tog_view *view)
7471 struct tog_ref_view_state *s = &view->state.ref;
7472 struct tog_reflist_entry *re = NULL;
7474 if (!view->searching) {
7475 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7479 if (s->matched_entry) {
7480 if (view->searching == TOG_SEARCH_FORWARD) {
7481 if (s->selected_entry)
7482 re = TAILQ_NEXT(s->selected_entry, entry);
7484 re = TAILQ_PREV(s->selected_entry,
7485 tog_reflist_head, entry);
7487 if (s->selected_entry == NULL)
7488 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7490 re = TAILQ_PREV(s->selected_entry,
7491 tog_reflist_head, entry);
7494 if (s->selected_entry)
7495 re = s->selected_entry;
7496 else if (view->searching == TOG_SEARCH_FORWARD)
7497 re = TAILQ_FIRST(&s->refs);
7499 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7504 if (s->matched_entry == NULL) {
7505 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7508 if (view->searching == TOG_SEARCH_FORWARD)
7509 re = TAILQ_FIRST(&s->refs);
7511 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7514 if (match_reflist_entry(re, &view->regex)) {
7515 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7516 s->matched_entry = re;
7520 if (view->searching == TOG_SEARCH_FORWARD)
7521 re = TAILQ_NEXT(re, entry);
7523 re = TAILQ_PREV(re, tog_reflist_head, entry);
7526 if (s->matched_entry) {
7527 s->first_displayed_entry = s->matched_entry;
7534 static const struct got_error *
7535 show_ref_view(struct tog_view *view)
7537 const struct got_error *err = NULL;
7538 struct tog_ref_view_state *s = &view->state.ref;
7539 struct tog_reflist_entry *re;
7542 struct tog_color *tc;
7544 int limit = view->nlines;
7546 werase(view->window);
7549 if (view_is_hsplit_top(view))
7550 --limit; /* border */
7555 re = s->first_displayed_entry;
7557 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7559 return got_error_from_errno("asprintf");
7561 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7566 if (view_needs_focus_indication(view))
7567 wstandout(view->window);
7568 waddwstr(view->window, wline);
7569 if (view_needs_focus_indication(view))
7570 wstandend(view->window);
7575 if (width < view->ncols - 1)
7576 waddch(view->window, '\n');
7581 while (re && limit > 0) {
7583 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7586 struct got_commit_object *ci;
7587 struct got_tag_object *tag;
7588 struct got_object_id *id;
7592 err = got_ref_resolve(&id, s->repo, re->ref);
7595 err = got_object_open_as_tag(&tag, s->repo, id);
7597 if (err->code != GOT_ERR_OBJ_TYPE) {
7601 err = got_object_open_as_commit(&ci, s->repo,
7607 t = got_object_commit_get_committer_time(ci);
7608 got_object_commit_close(ci);
7610 t = got_object_tag_get_tagger_time(tag);
7611 got_object_tag_close(tag);
7614 if (gmtime_r(&t, &tm) == NULL)
7615 return got_error_from_errno("gmtime_r");
7616 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7617 return got_error(GOT_ERR_NO_SPACE);
7619 if (got_ref_is_symbolic(re->ref)) {
7620 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7621 ymd : "", got_ref_get_name(re->ref),
7622 got_ref_get_symref_target(re->ref)) == -1)
7623 return got_error_from_errno("asprintf");
7624 } else if (s->show_ids) {
7625 struct got_object_id *id;
7627 err = got_ref_resolve(&id, s->repo, re->ref);
7630 err = got_object_id_str(&id_str, id);
7635 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7636 got_ref_get_name(re->ref), id_str) == -1) {
7637 err = got_error_from_errno("asprintf");
7644 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7645 got_ref_get_name(re->ref)) == -1)
7646 return got_error_from_errno("asprintf");
7648 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7654 if (n == s->selected) {
7656 wstandout(view->window);
7657 s->selected_entry = re;
7659 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7661 wattr_on(view->window,
7662 COLOR_PAIR(tc->colorpair), NULL);
7663 waddwstr(view->window, wline);
7665 wattr_off(view->window,
7666 COLOR_PAIR(tc->colorpair), NULL);
7667 if (width < view->ncols - 1)
7668 waddch(view->window, '\n');
7669 if (n == s->selected && view->focussed)
7670 wstandend(view->window);
7676 s->last_displayed_entry = re;
7679 re = TAILQ_NEXT(re, entry);
7686 static const struct got_error *
7687 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7688 struct tog_reflist_entry *re, struct got_repository *repo)
7690 const struct got_error *err = NULL;
7691 struct got_object_id *commit_id = NULL;
7692 struct tog_view *tree_view;
7696 err = resolve_reflist_entry(&commit_id, re, repo);
7698 if (err->code != GOT_ERR_OBJ_TYPE)
7705 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7706 if (tree_view == NULL) {
7707 err = got_error_from_errno("view_open");
7711 err = open_tree_view(tree_view, commit_id,
7712 got_ref_get_name(re->ref), repo);
7716 *new_view = tree_view;
7721 static const struct got_error *
7722 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7724 const struct got_error *err = NULL;
7725 struct tog_ref_view_state *s = &view->state.ref;
7726 struct tog_view *log_view, *tree_view;
7727 struct tog_reflist_entry *re;
7728 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7732 s->show_ids = !s->show_ids;
7736 s->show_date = !s->show_date;
7740 s->sort_by_date = !s->sort_by_date;
7742 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7743 got_ref_cmp_by_commit_timestamp_descending :
7744 tog_ref_cmp_by_name, s->repo);
7747 got_reflist_object_id_map_free(tog_refs_idmap);
7748 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7749 &tog_refs, s->repo);
7752 ref_view_free_refs(s);
7753 err = ref_view_load_refs(s);
7758 if (!s->selected_entry)
7760 if (view_is_parent_view(view))
7761 view_get_split(view, &begin_y, &begin_x);
7763 err = log_ref_entry(&log_view, begin_y, begin_x,
7764 s->selected_entry, s->repo);
7768 if (view_is_parent_view(view) &&
7769 view->mode == TOG_VIEW_SPLIT_HRZN) {
7770 err = view_init_hsplit(view, begin_y);
7776 log_view->focussed = 1;
7777 log_view->mode = view->mode;
7778 log_view->nlines = view->lines - begin_y;
7779 if (view_is_parent_view(view)) {
7780 view_transfer_size(log_view, view);
7781 err = view_close_child(view);
7784 err = view_set_child(view, log_view);
7787 view->focus_child = 1;
7789 *new_view = log_view;
7793 if (!s->selected_entry)
7795 if (view_is_parent_view(view))
7796 view_get_split(view, &begin_y, &begin_x);
7797 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7798 s->selected_entry, s->repo);
7799 if (err || tree_view == NULL)
7801 if (view_is_parent_view(view) &&
7802 view->mode == TOG_VIEW_SPLIT_HRZN) {
7803 err = view_init_hsplit(view, begin_y);
7808 tree_view->focussed = 1;
7809 tree_view->mode = view->mode;
7810 tree_view->nlines = view->lines - begin_y;
7811 if (view_is_parent_view(view)) {
7812 view_transfer_size(tree_view, view);
7813 err = view_close_child(view);
7816 err = view_set_child(view, tree_view);
7819 view->focus_child = 1;
7821 *new_view = tree_view;
7827 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7831 int eos = view->nlines - 1;
7833 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7837 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7838 for (n = 0; n < eos; n++) {
7841 s->first_displayed_entry = re;
7842 re = TAILQ_PREV(re, tog_reflist_head, entry);
7845 s->selected = n - 1;
7851 if (s->selected > 0) {
7855 ref_scroll_up(s, 1);
7856 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7866 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7867 s->selected -= MIN(nscroll, s->selected);
7868 ref_scroll_up(s, MAX(0, nscroll));
7869 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7875 if (s->selected < s->ndisplayed - 1) {
7879 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7880 /* can't scroll any further */
7884 ref_scroll_down(view, 1);
7894 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7895 /* can't scroll any further; move cursor down */
7896 if (s->selected < s->ndisplayed - 1)
7897 s->selected += MIN(nscroll,
7898 s->ndisplayed - s->selected - 1);
7899 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7900 s->selected += s->ndisplayed - s->selected - 1;
7904 ref_scroll_down(view, nscroll);
7909 err = tog_load_refs(s->repo, s->sort_by_date);
7912 ref_view_free_refs(s);
7913 err = ref_view_load_refs(s);
7916 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7917 s->selected = view->nlines - 2;
7931 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7936 static const struct got_error *
7937 cmd_ref(int argc, char *argv[])
7939 const struct got_error *error;
7940 struct got_repository *repo = NULL;
7941 struct got_worktree *worktree = NULL;
7942 char *cwd = NULL, *repo_path = NULL;
7944 struct tog_view *view;
7945 int *pack_fds = NULL;
7947 while ((ch = getopt(argc, argv, "r:")) != -1) {
7950 repo_path = realpath(optarg, NULL);
7951 if (repo_path == NULL)
7952 return got_error_from_errno2("realpath",
7967 error = got_repo_pack_fds_open(&pack_fds);
7971 if (repo_path == NULL) {
7972 cwd = getcwd(NULL, 0);
7974 return got_error_from_errno("getcwd");
7975 error = got_worktree_open(&worktree, cwd);
7976 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7980 strdup(got_worktree_get_repo_path(worktree));
7982 repo_path = strdup(cwd);
7983 if (repo_path == NULL) {
7984 error = got_error_from_errno("strdup");
7989 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7995 error = apply_unveil(got_repo_get_path(repo), NULL);
7999 error = tog_load_refs(repo, 0);
8003 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8005 error = got_error_from_errno("view_open");
8009 error = open_ref_view(view, repo);
8014 /* Release work tree lock. */
8015 got_worktree_close(worktree);
8018 error = view_loop(view);
8023 const struct got_error *close_err = got_repo_close(repo);
8028 const struct got_error *pack_err =
8029 got_repo_pack_fds_close(pack_fds);
8038 * If view was scrolled down to move the selected line into view when opening a
8039 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8042 offset_selection_up(struct tog_view *view)
8044 switch (view->type) {
8045 case TOG_VIEW_BLAME: {
8046 struct tog_blame_view_state *s = &view->state.blame;
8047 if (s->first_displayed_line == 1) {
8048 s->selected_line = MAX(s->selected_line - view->offset,
8052 if (s->first_displayed_line > view->offset)
8053 s->first_displayed_line -= view->offset;
8055 s->first_displayed_line = 1;
8056 s->selected_line += view->offset;
8060 log_scroll_up(&view->state.log, view->offset);
8061 view->state.log.selected += view->offset;
8064 ref_scroll_up(&view->state.ref, view->offset);
8065 view->state.ref.selected += view->offset;
8068 tree_scroll_up(&view->state.tree, view->offset);
8069 view->state.tree.selected += view->offset;
8079 * If the selected line is in the section of screen covered by the bottom split,
8080 * scroll down offset lines to move it into view and index its new position.
8082 static const struct got_error *
8083 offset_selection_down(struct tog_view *view)
8085 const struct got_error *err = NULL;
8086 const struct got_error *(*scrolld)(struct tog_view *, int);
8087 int *selected = NULL;
8090 switch (view->type) {
8091 case TOG_VIEW_BLAME: {
8092 struct tog_blame_view_state *s = &view->state.blame;
8095 if (s->selected_line > view->nlines - header) {
8096 offset = abs(view->nlines - s->selected_line - header);
8097 s->first_displayed_line += offset;
8098 s->selected_line -= offset;
8099 view->offset = offset;
8103 case TOG_VIEW_LOG: {
8104 struct tog_log_view_state *s = &view->state.log;
8105 scrolld = &log_scroll_down;
8106 header = view_is_parent_view(view) ? 3 : 2;
8107 selected = &s->selected;
8110 case TOG_VIEW_REF: {
8111 struct tog_ref_view_state *s = &view->state.ref;
8112 scrolld = &ref_scroll_down;
8114 selected = &s->selected;
8117 case TOG_VIEW_TREE: {
8118 struct tog_tree_view_state *s = &view->state.tree;
8119 scrolld = &tree_scroll_down;
8121 selected = &s->selected;
8131 if (selected && *selected > view->nlines - header) {
8132 offset = abs(view->nlines - *selected - header);
8133 view->offset = offset;
8134 if (scrolld && offset) {
8135 err = scrolld(view, offset);
8136 *selected -= offset;
8144 list_commands(FILE *fp)
8148 fprintf(fp, "commands:");
8149 for (i = 0; i < nitems(tog_commands); i++) {
8150 const struct tog_cmd *cmd = &tog_commands[i];
8151 fprintf(fp, " %s", cmd->name);
8157 usage(int hflag, int status)
8159 FILE *fp = (status == 0) ? stdout : stderr;
8161 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8164 fprintf(fp, "lazy usage: %s path\n", getprogname());
8171 make_argv(int argc, ...)
8179 argv = calloc(argc, sizeof(char *));
8182 for (i = 0; i < argc; i++) {
8183 argv[i] = strdup(va_arg(ap, char *));
8184 if (argv[i] == NULL)
8193 * Try to convert 'tog path' into a 'tog log path' command.
8194 * The user could simply have mistyped the command rather than knowingly
8195 * provided a path. So check whether argv[0] can in fact be resolved
8196 * to a path in the HEAD commit and print a special error if not.
8197 * This hack is for mpi@ <3
8199 static const struct got_error *
8200 tog_log_with_path(int argc, char *argv[])
8202 const struct got_error *error = NULL, *close_err;
8203 const struct tog_cmd *cmd = NULL;
8204 struct got_repository *repo = NULL;
8205 struct got_worktree *worktree = NULL;
8206 struct got_object_id *commit_id = NULL, *id = NULL;
8207 struct got_commit_object *commit = NULL;
8208 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8209 char *commit_id_str = NULL, **cmd_argv = NULL;
8210 int *pack_fds = NULL;
8212 cwd = getcwd(NULL, 0);
8214 return got_error_from_errno("getcwd");
8216 error = got_repo_pack_fds_open(&pack_fds);
8220 error = got_worktree_open(&worktree, cwd);
8221 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8225 repo_path = strdup(got_worktree_get_repo_path(worktree));
8227 repo_path = strdup(cwd);
8228 if (repo_path == NULL) {
8229 error = got_error_from_errno("strdup");
8233 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8237 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8242 error = tog_load_refs(repo, 0);
8245 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8246 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8247 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8252 got_worktree_close(worktree);
8256 error = got_object_open_as_commit(&commit, repo, commit_id);
8260 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8262 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8264 fprintf(stderr, "%s: '%s' is no known command or path\n",
8265 getprogname(), argv[0]);
8270 close_err = got_repo_close(repo);
8275 error = got_object_id_str(&commit_id_str, commit_id);
8279 cmd = &tog_commands[0]; /* log */
8281 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8282 error = cmd->cmd_main(argc, cmd_argv);
8285 close_err = got_repo_close(repo);
8290 got_object_commit_close(commit);
8292 got_worktree_close(worktree);
8294 const struct got_error *pack_err =
8295 got_repo_pack_fds_close(pack_fds);
8300 free(commit_id_str);
8307 for (i = 0; i < argc; i++)
8316 main(int argc, char *argv[])
8318 const struct got_error *error = NULL;
8319 const struct tog_cmd *cmd = NULL;
8320 int ch, hflag = 0, Vflag = 0;
8321 char **cmd_argv = NULL;
8322 static const struct option longopts[] = {
8323 { "version", no_argument, NULL, 'V' },
8326 char *diff_algo_str = NULL;
8328 setlocale(LC_CTYPE, "");
8330 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8350 got_version_print_str();
8355 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8363 /* Build an argument vector which runs a default command. */
8364 cmd = &tog_commands[0];
8366 cmd_argv = make_argv(argc, cmd->name);
8370 /* Did the user specify a command? */
8371 for (i = 0; i < nitems(tog_commands); i++) {
8372 if (strncmp(tog_commands[i].name, argv[0],
8373 strlen(argv[0])) == 0) {
8374 cmd = &tog_commands[i];
8380 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8381 if (diff_algo_str) {
8382 if (strcasecmp(diff_algo_str, "patience") == 0)
8383 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8384 if (strcasecmp(diff_algo_str, "myers") == 0)
8385 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8391 /* No command specified; try log with a path */
8392 error = tog_log_with_path(argc, argv);
8397 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8404 for (i = 0; i < argc; i++)
8409 if (error && error->code != GOT_ERR_CANCELLED)
8410 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);