2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
53 #include "got_opentemp.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
60 #include "got_worktree.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define CTRL(x) ((x) & 0x1f)
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
119 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 #define TOG_EOF_STRING "(END)"
123 struct commit_queue_entry {
124 TAILQ_ENTRY(commit_queue_entry) entry;
125 struct got_object_id *id;
126 struct got_commit_object *commit;
129 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 struct commit_queue {
132 struct commit_queue_head head;
136 STAILQ_ENTRY(tog_color) entry;
140 STAILQ_HEAD(tog_colors, tog_color);
142 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 static struct got_reflist_object_id_map *tog_refs_idmap;
144 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 static const struct got_error *
147 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 struct got_reference* re2)
150 const char *name1 = got_ref_get_name(re1);
151 const char *name2 = got_ref_get_name(re2);
152 int isbackup1, isbackup2;
154 /* Sort backup refs towards the bottom of the list. */
155 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 if (!isbackup1 && isbackup2) {
160 } else if (isbackup1 && !isbackup2) {
165 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
169 static const struct got_error *
170 tog_load_refs(struct got_repository *repo, int sort_by_date)
172 const struct got_error *err;
174 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
180 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
187 if (tog_refs_idmap) {
188 got_reflist_object_id_map_free(tog_refs_idmap);
189 tog_refs_idmap = NULL;
191 got_ref_list_free(&tog_refs);
194 static const struct got_error *
195 add_color(struct tog_colors *colors, const char *pattern,
196 int idx, short color)
198 const struct got_error *err = NULL;
199 struct tog_color *tc;
202 if (idx < 1 || idx > COLOR_PAIRS - 1)
205 init_pair(idx, color, -1);
207 tc = calloc(1, sizeof(*tc));
209 return got_error_from_errno("calloc");
210 regerr = regcomp(&tc->regex, pattern,
211 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
213 static char regerr_msg[512];
214 static char err_msg[512];
215 regerror(regerr, &tc->regex, regerr_msg,
217 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
219 err = got_error_msg(GOT_ERR_REGEX, err_msg);
224 STAILQ_INSERT_HEAD(colors, tc, entry);
229 free_colors(struct tog_colors *colors)
231 struct tog_color *tc;
233 while (!STAILQ_EMPTY(colors)) {
234 tc = STAILQ_FIRST(colors);
235 STAILQ_REMOVE_HEAD(colors, entry);
241 static struct tog_color *
242 get_color(struct tog_colors *colors, int colorpair)
244 struct tog_color *tc = NULL;
246 STAILQ_FOREACH(tc, colors, entry) {
247 if (tc->colorpair == colorpair)
255 default_color_value(const char *envvar)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 return COLOR_MAGENTA;
259 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
261 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
265 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
271 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
273 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
275 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
277 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
279 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
281 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
285 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
292 get_color_value(const char *envvar)
294 const char *val = getenv(envvar);
297 return default_color_value(envvar);
299 if (strcasecmp(val, "black") == 0)
301 if (strcasecmp(val, "red") == 0)
303 if (strcasecmp(val, "green") == 0)
305 if (strcasecmp(val, "yellow") == 0)
307 if (strcasecmp(val, "blue") == 0)
309 if (strcasecmp(val, "magenta") == 0)
310 return COLOR_MAGENTA;
311 if (strcasecmp(val, "cyan") == 0)
313 if (strcasecmp(val, "white") == 0)
315 if (strcasecmp(val, "default") == 0)
318 return default_color_value(envvar);
322 struct tog_diff_view_state {
323 struct got_object_id *id1, *id2;
324 const char *label1, *label2;
327 int first_displayed_line;
328 int last_displayed_line;
331 int ignore_whitespace;
333 struct got_repository *repo;
334 struct tog_colors colors;
340 /* passed from log or blame view; may be NULL */
341 struct tog_view *parent_view;
344 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 static volatile sig_atomic_t tog_thread_error;
347 struct tog_log_thread_args {
348 pthread_cond_t need_commits;
349 pthread_cond_t commit_loaded;
352 struct got_commit_graph *graph;
353 struct commit_queue *commits;
354 const char *in_repo_path;
355 struct got_object_id *start_id;
356 struct got_repository *repo;
360 struct commit_queue_entry **first_displayed_entry;
361 struct commit_queue_entry **selected_entry;
363 int *search_next_done;
367 struct tog_log_view_state {
368 struct commit_queue commits;
369 struct commit_queue_entry *first_displayed_entry;
370 struct commit_queue_entry *last_displayed_entry;
371 struct commit_queue_entry *selected_entry;
376 struct got_repository *repo;
377 struct got_object_id *start_id;
380 struct tog_log_thread_args thread_args;
381 struct commit_queue_entry *matched_entry;
382 struct commit_queue_entry *search_entry;
383 struct tog_colors colors;
387 #define TOG_COLOR_DIFF_MINUS 1
388 #define TOG_COLOR_DIFF_PLUS 2
389 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 #define TOG_COLOR_DIFF_META 4
391 #define TOG_COLOR_TREE_SUBMODULE 5
392 #define TOG_COLOR_TREE_SYMLINK 6
393 #define TOG_COLOR_TREE_DIRECTORY 7
394 #define TOG_COLOR_TREE_EXECUTABLE 8
395 #define TOG_COLOR_COMMIT 9
396 #define TOG_COLOR_AUTHOR 10
397 #define TOG_COLOR_DATE 11
398 #define TOG_COLOR_REFS_HEADS 12
399 #define TOG_COLOR_REFS_TAGS 13
400 #define TOG_COLOR_REFS_REMOTES 14
401 #define TOG_COLOR_REFS_BACKUP 15
403 struct tog_blame_cb_args {
404 struct tog_blame_line *lines; /* one per line */
407 struct tog_view *view;
408 struct got_object_id *commit_id;
412 struct tog_blame_thread_args {
414 struct got_repository *repo;
415 struct tog_blame_cb_args *cb_args;
417 got_cancel_cb cancel_cb;
424 struct tog_blame_line *lines;
428 struct tog_blame_thread_args thread_args;
429 struct tog_blame_cb_args cb_args;
434 struct tog_blame_view_state {
435 int first_displayed_line;
436 int last_displayed_line;
438 int last_diffed_line;
442 struct got_object_id_queue blamed_commits;
443 struct got_object_qid *blamed_commit;
445 struct got_repository *repo;
446 struct got_object_id *commit_id;
447 struct tog_blame blame;
449 struct tog_colors colors;
452 struct tog_parent_tree {
453 TAILQ_ENTRY(tog_parent_tree) entry;
454 struct got_tree_object *tree;
455 struct got_tree_entry *first_displayed_entry;
456 struct got_tree_entry *selected_entry;
460 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
462 struct tog_tree_view_state {
464 struct got_object_id *commit_id;/* commit which this tree belongs to */
465 struct got_tree_object *root; /* the commit's root tree entry */
466 struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 struct got_tree_entry *first_displayed_entry;
468 struct got_tree_entry *last_displayed_entry;
469 struct got_tree_entry *selected_entry;
470 int ndisplayed, selected, show_ids;
471 struct tog_parent_trees parents; /* parent trees of current sub-tree */
473 struct got_repository *repo;
474 struct got_tree_entry *matched_entry;
475 struct tog_colors colors;
478 struct tog_reflist_entry {
479 TAILQ_ENTRY(tog_reflist_entry) entry;
480 struct got_reference *ref;
484 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
486 struct tog_ref_view_state {
487 struct tog_reflist_head refs;
488 struct tog_reflist_entry *first_displayed_entry;
489 struct tog_reflist_entry *last_displayed_entry;
490 struct tog_reflist_entry *selected_entry;
491 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 struct got_repository *repo;
493 struct tog_reflist_entry *matched_entry;
494 struct tog_colors colors;
498 * We implement two types of views: parent views and child views.
500 * The 'Tab' key switches focus between a parent view and its child view.
501 * Child views are shown side-by-side to their parent view, provided
502 * there is enough screen estate.
504 * When a new view is opened from within a parent view, this new view
505 * becomes a child view of the parent view, replacing any existing child.
507 * When a new view is opened from within a child view, this new view
508 * becomes a parent view which will obscure the views below until the
509 * user quits the new parent view by typing 'q'.
511 * This list of views contains parent views only.
512 * Child views are only pointed to by their parent view.
514 TAILQ_HEAD(tog_view_list_head, tog_view);
517 TAILQ_ENTRY(tog_view) entry;
520 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 int resized_y, resized_x; /* begin_y/x based on user resizing */
522 int maxx, x; /* max column and current start column */
523 int lines, cols; /* copies of LINES and COLS */
524 int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 int ch, count; /* current keymap and count prefix */
526 int resized; /* set when in a resize event */
527 int focussed; /* Only set on one parent or child view at a time. */
529 struct tog_view *parent;
530 struct tog_view *child;
533 * This flag is initially set on parent views when a new child view
534 * is created. It gets toggled when the 'Tab' key switches focus
535 * between parent and child.
536 * The flag indicates whether focus should be passed on to our child
537 * view if this parent view gets picked for focus after another parent
538 * view was closed. This prevents child views from losing focus in such
543 enum tog_view_mode mode;
544 /* type-specific state */
545 enum tog_view_type type;
547 struct tog_diff_view_state diff;
548 struct tog_log_view_state log;
549 struct tog_blame_view_state blame;
550 struct tog_tree_view_state tree;
551 struct tog_ref_view_state ref;
554 const struct got_error *(*show)(struct tog_view *);
555 const struct got_error *(*input)(struct tog_view **,
556 struct tog_view *, int);
557 const struct got_error *(*reset)(struct tog_view *);
558 const struct got_error *(*resize)(struct tog_view *, int);
559 const struct got_error *(*close)(struct tog_view *);
561 const struct got_error *(*search_start)(struct tog_view *);
562 const struct got_error *(*search_next)(struct tog_view *);
565 #define TOG_SEARCH_FORWARD 1
566 #define TOG_SEARCH_BACKWARD 2
567 int search_next_done;
568 #define TOG_SEARCH_HAVE_MORE 1
569 #define TOG_SEARCH_NO_MORE 2
570 #define TOG_SEARCH_HAVE_NONE 3
575 static const struct got_error *open_diff_view(struct tog_view *,
576 struct got_object_id *, struct got_object_id *,
577 const char *, const char *, int, int, int, struct tog_view *,
578 struct got_repository *);
579 static const struct got_error *show_diff_view(struct tog_view *);
580 static const struct got_error *input_diff_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *reset_diff_view(struct tog_view *);
583 static const struct got_error* close_diff_view(struct tog_view *);
584 static const struct got_error *search_start_diff_view(struct tog_view *);
585 static const struct got_error *search_next_diff_view(struct tog_view *);
587 static const struct got_error *open_log_view(struct tog_view *,
588 struct got_object_id *, struct got_repository *,
589 const char *, const char *, int);
590 static const struct got_error * show_log_view(struct tog_view *);
591 static const struct got_error *input_log_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *resize_log_view(struct tog_view *, int);
594 static const struct got_error *close_log_view(struct tog_view *);
595 static const struct got_error *search_start_log_view(struct tog_view *);
596 static const struct got_error *search_next_log_view(struct tog_view *);
598 static const struct got_error *open_blame_view(struct tog_view *, char *,
599 struct got_object_id *, struct got_repository *);
600 static const struct got_error *show_blame_view(struct tog_view *);
601 static const struct got_error *input_blame_view(struct tog_view **,
602 struct tog_view *, int);
603 static const struct got_error *reset_blame_view(struct tog_view *);
604 static const struct got_error *close_blame_view(struct tog_view *);
605 static const struct got_error *search_start_blame_view(struct tog_view *);
606 static const struct got_error *search_next_blame_view(struct tog_view *);
608 static const struct got_error *open_tree_view(struct tog_view *,
609 struct got_object_id *, const char *, struct got_repository *);
610 static const struct got_error *show_tree_view(struct tog_view *);
611 static const struct got_error *input_tree_view(struct tog_view **,
612 struct tog_view *, int);
613 static const struct got_error *close_tree_view(struct tog_view *);
614 static const struct got_error *search_start_tree_view(struct tog_view *);
615 static const struct got_error *search_next_tree_view(struct tog_view *);
617 static const struct got_error *open_ref_view(struct tog_view *,
618 struct got_repository *);
619 static const struct got_error *show_ref_view(struct tog_view *);
620 static const struct got_error *input_ref_view(struct tog_view **,
621 struct tog_view *, int);
622 static const struct got_error *close_ref_view(struct tog_view *);
623 static const struct got_error *search_start_ref_view(struct tog_view *);
624 static const struct got_error *search_next_ref_view(struct tog_view *);
626 static volatile sig_atomic_t tog_sigwinch_received;
627 static volatile sig_atomic_t tog_sigpipe_received;
628 static volatile sig_atomic_t tog_sigcont_received;
629 static volatile sig_atomic_t tog_sigint_received;
630 static volatile sig_atomic_t tog_sigterm_received;
633 tog_sigwinch(int signo)
635 tog_sigwinch_received = 1;
639 tog_sigpipe(int signo)
641 tog_sigpipe_received = 1;
645 tog_sigcont(int signo)
647 tog_sigcont_received = 1;
651 tog_sigint(int signo)
653 tog_sigint_received = 1;
657 tog_sigterm(int signo)
659 tog_sigterm_received = 1;
663 tog_fatal_signal_received(void)
665 return (tog_sigpipe_received ||
666 tog_sigint_received || tog_sigint_received);
669 static const struct got_error *
670 view_close(struct tog_view *view)
672 const struct got_error *err = NULL, *child_err = NULL;
675 child_err = view_close(view->child);
679 err = view->close(view);
681 del_panel(view->panel);
683 delwin(view->window);
685 return err ? err : child_err;
688 static struct tog_view *
689 view_open(int nlines, int ncols, int begin_y, int begin_x,
690 enum tog_view_type type)
692 struct tog_view *view = calloc(1, sizeof(*view));
700 view->nlines = nlines ? nlines : LINES - begin_y;
701 view->ncols = ncols ? ncols : COLS - begin_x;
702 view->begin_y = begin_y;
703 view->begin_x = begin_x;
704 view->window = newwin(nlines, ncols, begin_y, begin_x);
705 if (view->window == NULL) {
709 view->panel = new_panel(view->window);
710 if (view->panel == NULL ||
711 set_panel_userptr(view->panel, view) != OK) {
716 keypad(view->window, TRUE);
721 view_split_begin_x(int begin_x)
723 if (begin_x > 0 || COLS < 120)
725 return (COLS - MAX(COLS / 2, 80));
728 /* XXX Stub till we decide what to do. */
730 view_split_begin_y(int lines)
732 return lines * HSPLIT_SCALE;
735 static const struct got_error *view_resize(struct tog_view *);
737 static const struct got_error *
738 view_splitscreen(struct tog_view *view)
740 const struct got_error *err = NULL;
742 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
743 if (view->resized_y && view->resized_y < view->lines)
744 view->begin_y = view->resized_y;
746 view->begin_y = view_split_begin_y(view->nlines);
748 } else if (!view->resized) {
749 if (view->resized_x && view->resized_x < view->cols - 1 &&
751 view->begin_x = view->resized_x;
753 view->begin_x = view_split_begin_x(0);
756 view->nlines = LINES - view->begin_y;
757 view->ncols = COLS - view->begin_x;
760 err = view_resize(view);
764 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
765 view->parent->nlines = view->begin_y;
767 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
768 return got_error_from_errno("mvwin");
773 static const struct got_error *
774 view_fullscreen(struct tog_view *view)
776 const struct got_error *err = NULL;
779 view->begin_y = view->resized ? view->begin_y : 0;
780 view->nlines = view->resized ? view->nlines : LINES;
784 err = view_resize(view);
788 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
789 return got_error_from_errno("mvwin");
795 view_is_parent_view(struct tog_view *view)
797 return view->parent == NULL;
801 view_is_splitscreen(struct tog_view *view)
803 return view->begin_x > 0 || view->begin_y > 0;
807 view_is_fullscreen(struct tog_view *view)
809 return view->nlines == LINES && view->ncols == COLS;
813 view_is_hsplit_top(struct tog_view *view)
815 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
816 view_is_splitscreen(view->child);
820 view_border(struct tog_view *view)
823 const struct tog_view *view_above;
826 return view_border(view->parent);
828 panel = panel_above(view->panel);
832 view_above = panel_userptr(panel);
833 if (view->mode == TOG_VIEW_SPLIT_HRZN)
834 mvwhline(view->window, view_above->begin_y - 1,
835 view->begin_x, got_locale_is_utf8() ?
836 ACS_HLINE : '-', view->ncols);
838 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
839 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
842 static const struct got_error *view_init_hsplit(struct tog_view *, int);
843 static const struct got_error *request_log_commits(struct tog_view *);
844 static const struct got_error *offset_selection_down(struct tog_view *);
845 static void offset_selection_up(struct tog_view *);
846 static void view_get_split(struct tog_view *, int *, int *);
848 static const struct got_error *
849 view_resize(struct tog_view *view)
851 const struct got_error *err = NULL;
852 int dif, nlines, ncols;
854 dif = LINES - view->lines; /* line difference */
856 if (view->lines > LINES)
857 nlines = view->nlines - (view->lines - LINES);
859 nlines = view->nlines + (LINES - view->lines);
860 if (view->cols > COLS)
861 ncols = view->ncols - (view->cols - COLS);
863 ncols = view->ncols + (COLS - view->cols);
866 int hs = view->child->begin_y;
868 if (!view_is_fullscreen(view))
869 view->child->begin_x = view_split_begin_x(view->begin_x);
870 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
871 view->child->begin_x == 0) {
874 view_fullscreen(view->child);
875 if (view->child->focussed)
876 show_panel(view->child->panel);
878 show_panel(view->panel);
880 ncols = view->child->begin_x;
882 view_splitscreen(view->child);
883 show_panel(view->child->panel);
886 * XXX This is ugly and needs to be moved into the above
887 * logic but "works" for now and my attempts at moving it
888 * break either 'tab' or 'F' key maps in horizontal splits.
891 err = view_splitscreen(view->child);
894 if (dif < 0) { /* top split decreased */
895 err = offset_selection_down(view);
902 show_panel(view->child->panel);
903 nlines = view->nlines;
905 } else if (view->parent == NULL)
908 if (view->resize && dif > 0) {
909 err = view->resize(view, dif);
914 if (wresize(view->window, nlines, ncols) == ERR)
915 return got_error_from_errno("wresize");
916 if (replace_panel(view->panel, view->window) == ERR)
917 return got_error_from_errno("replace_panel");
918 wclear(view->window);
920 view->nlines = nlines;
928 static const struct got_error *
929 resize_log_view(struct tog_view *view, int increase)
931 struct tog_log_view_state *s = &view->state.log;
932 const struct got_error *err = NULL;
933 int n = s->selected_entry->idx + view->lines - s->selected;
936 * Request commits to account for the increased
937 * height so we have enough to populate the view.
939 if (s->commits.ncommits < n) {
940 view->nscrolled = n - s->commits.ncommits + increase + 1;
941 err = request_log_commits(view);
948 view_adjust_offset(struct tog_view *view, int n)
953 if (view->parent && view->parent->offset) {
954 if (view->parent->offset + n >= 0)
955 view->parent->offset += n;
957 view->parent->offset = 0;
958 } else if (view->offset) {
959 if (view->offset - n >= 0)
966 static const struct got_error *
967 view_resize_split(struct tog_view *view, int resize)
969 const struct got_error *err = NULL;
970 struct tog_view *v = NULL;
977 if (!v->child || !view_is_splitscreen(v->child))
980 v->resized = v->child->resized = resize; /* lock for resize event */
982 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 int y = v->child->begin_y;
985 if (v->child->resized_y)
986 v->child->begin_y = v->child->resized_y;
988 v->child->begin_y -= resize;
990 v->child->begin_y += resize;
991 if (v->child->begin_y < 3) {
993 v->child->begin_y = 3;
994 } else if (v->child->begin_y > LINES - 1) {
996 v->child->begin_y = LINES - 1;
999 v->child->ncols = COLS;
1000 view_adjust_offset(view, resize);
1001 err = view_init_hsplit(v, v->child->begin_y);
1004 v->child->resized_y = v->child->begin_y;
1005 if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1006 v->child->nscrolled = y - v->child->begin_y;
1007 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1008 v->nscrolled = v->child->begin_y - y;
1010 if (v->child->resized_x)
1011 v->child->begin_x = v->child->resized_x;
1013 v->child->begin_x -= resize;
1015 v->child->begin_x += resize;
1016 if (v->child->begin_x < 11) {
1018 v->child->begin_x = 11;
1019 } else if (v->child->begin_x > COLS - 1) {
1021 v->child->begin_x = COLS - 1;
1023 v->child->resized_x = v->child->begin_x;
1026 v->child->mode = v->mode;
1027 v->child->nlines = v->lines - v->child->begin_y;
1028 v->child->ncols = v->cols - v->child->begin_x;
1031 err = view_fullscreen(v);
1034 err = view_splitscreen(v->child);
1038 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1039 err = offset_selection_down(v->child);
1045 err = request_log_commits(v);
1046 else if (v->child->nscrolled)
1047 err = request_log_commits(v->child);
1049 v->resized = v->child->resized = 0;
1055 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1057 struct tog_view *v = src->child ? src->child : src;
1059 dst->resized_x = v->resized_x;
1060 dst->resized_y = v->resized_y;
1063 static const struct got_error *
1064 view_close_child(struct tog_view *view)
1066 const struct got_error *err = NULL;
1068 if (view->child == NULL)
1071 err = view_close(view->child);
1076 static const struct got_error *
1077 view_set_child(struct tog_view *view, struct tog_view *child)
1079 const struct got_error *err = NULL;
1081 view->child = child;
1082 child->parent = view;
1084 err = view_resize(view);
1088 if (view->child->resized_x || view->child->resized_y)
1089 err = view_resize_split(view, 0);
1095 tog_resizeterm(void)
1098 struct winsize size;
1100 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1101 cols = 80; /* Default */
1105 lines = size.ws_row;
1107 resize_term(lines, cols);
1110 static const struct got_error *
1111 view_search_start(struct tog_view *view)
1113 const struct got_error *err = NULL;
1114 struct tog_view *v = view;
1118 if (view->search_started) {
1119 regfree(&view->regex);
1120 view->searching = 0;
1121 memset(&view->regmatch, 0, sizeof(view->regmatch));
1123 view->search_started = 0;
1125 if (view->nlines < 1)
1128 if (view_is_hsplit_top(view))
1131 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1132 wclrtoeol(v->window);
1134 nodelay(view->window, FALSE); /* block for search term input */
1137 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1138 wrefresh(v->window);
1141 nodelay(view->window, TRUE);
1145 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1146 err = view->search_start(view);
1148 regfree(&view->regex);
1151 view->search_started = 1;
1152 view->searching = TOG_SEARCH_FORWARD;
1153 view->search_next_done = 0;
1154 view->search_next(view);
1160 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1161 static const struct got_error *
1162 switch_split(struct tog_view *view)
1164 const struct got_error *err = NULL;
1165 struct tog_view *v = NULL;
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 v->mode = TOG_VIEW_SPLIT_VERT;
1175 v->mode = TOG_VIEW_SPLIT_HRZN;
1179 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1180 v->mode = TOG_VIEW_SPLIT_NONE;
1182 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1183 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1184 v->child->begin_y = v->child->resized_y;
1185 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1186 v->child->begin_x = v->child->resized_x;
1189 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1191 v->child->ncols = COLS;
1192 v->child->nscrolled = LINES - v->child->nlines;
1194 err = view_init_hsplit(v, v->child->begin_y);
1198 v->child->mode = v->mode;
1199 v->child->nlines = v->lines - v->child->begin_y;
1202 err = view_fullscreen(v);
1205 err = view_splitscreen(v->child);
1209 if (v->mode == TOG_VIEW_SPLIT_NONE)
1210 v->mode = TOG_VIEW_SPLIT_VERT;
1211 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1212 err = offset_selection_down(v);
1213 err = offset_selection_down(v->child);
1215 offset_selection_up(v);
1216 offset_selection_up(v->child);
1218 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1219 err = request_log_commits(v);
1220 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1221 err = request_log_commits(v->child);
1227 * Compute view->count from numeric input. Assign total to view->count and
1228 * return first non-numeric key entered.
1231 get_compound_key(struct tog_view *view, int c)
1233 struct tog_view *v = view;
1236 if (view_is_hsplit_top(view))
1238 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1242 cbreak(); /* block for input */
1243 wmove(v->window, v->nlines - 1, 0);
1244 wclrtoeol(v->window);
1245 waddch(v->window, ':');
1248 x = getcurx(v->window);
1249 if (x != ERR && x < view->ncols) {
1250 waddch(v->window, c);
1251 wrefresh(v->window);
1255 * Don't overflow. Max valid request should be the greatest
1256 * between the longest and total lines; cap at 10 million.
1261 n = n * 10 + (c - '0');
1262 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1264 /* Massage excessive or inapplicable values at the input handler. */
1270 static const struct got_error *
1271 view_input(struct tog_view **new, int *done, struct tog_view *view,
1272 struct tog_view_list_head *views)
1274 const struct got_error *err = NULL;
1280 /* Clear "no matches" indicator. */
1281 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1282 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1283 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1287 if (view->searching && !view->search_next_done) {
1288 errcode = pthread_mutex_unlock(&tog_mutex);
1290 return got_error_set_errno(errcode,
1291 "pthread_mutex_unlock");
1293 errcode = pthread_mutex_lock(&tog_mutex);
1295 return got_error_set_errno(errcode,
1296 "pthread_mutex_lock");
1297 view->search_next(view);
1301 nodelay(view->window, FALSE);
1302 /* Allow threads to make progress while we are waiting for input. */
1303 errcode = pthread_mutex_unlock(&tog_mutex);
1305 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1306 /* If we have an unfinished count, let C-g or backspace abort. */
1307 if (view->count && --view->count) {
1309 nodelay(view->window, TRUE);
1310 ch = wgetch(view->window);
1311 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1316 ch = wgetch(view->window);
1317 if (ch >= '1' && ch <= '9')
1318 view->ch = ch = get_compound_key(view, ch);
1320 errcode = pthread_mutex_lock(&tog_mutex);
1322 return got_error_set_errno(errcode, "pthread_mutex_lock");
1323 nodelay(view->window, TRUE);
1325 if (tog_sigwinch_received || tog_sigcont_received) {
1327 tog_sigwinch_received = 0;
1328 tog_sigcont_received = 0;
1329 TAILQ_FOREACH(v, views, entry) {
1330 err = view_resize(v);
1333 err = v->input(new, v, KEY_RESIZE);
1337 err = view_resize(v->child);
1340 err = v->child->input(new, v->child,
1344 if (v->child->resized_x || v->child->resized_y) {
1345 err = view_resize_split(v, 0);
1358 view->child->focussed = 1;
1359 view->focus_child = 1;
1360 } else if (view->parent) {
1362 view->parent->focussed = 1;
1363 view->parent->focus_child = 0;
1364 if (!view_is_splitscreen(view)) {
1365 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1366 view->parent->type == TOG_VIEW_LOG) {
1367 err = request_log_commits(view->parent);
1371 offset_selection_up(view->parent);
1372 err = view_fullscreen(view->parent);
1379 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1380 if (view->parent->type == TOG_VIEW_LOG) {
1381 /* might need more commits to fill fullscreen */
1382 err = request_log_commits(view->parent);
1386 offset_selection_up(view->parent);
1388 err = view->input(new, view, ch);
1396 if (view_is_parent_view(view)) {
1397 if (view->child == NULL)
1399 if (view_is_splitscreen(view->child)) {
1401 view->child->focussed = 1;
1402 err = view_fullscreen(view->child);
1404 err = view_splitscreen(view->child);
1406 err = view_resize_split(view, 0);
1410 err = view->child->input(new, view->child,
1413 if (view_is_splitscreen(view)) {
1414 view->parent->focussed = 0;
1416 err = view_fullscreen(view);
1418 err = view_splitscreen(view);
1419 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1420 err = view_resize(view->parent);
1422 err = view_resize_split(view, 0);
1426 err = view->input(new, view, KEY_RESIZE);
1430 if (view->type == TOG_VIEW_LOG) {
1431 err = request_log_commits(view);
1436 err = offset_selection_down(view->parent);
1438 err = offset_selection_down(view);
1442 err = switch_split(view);
1445 err = view_resize_split(view, -1);
1448 err = view_resize_split(view, 1);
1454 if (view->search_start)
1455 view_search_start(view);
1457 err = view->input(new, view, ch);
1461 if (view->search_started && view->search_next) {
1462 view->searching = (ch == 'n' ?
1463 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1464 view->search_next_done = 0;
1465 view->search_next(view);
1467 err = view->input(new, view, ch);
1470 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1471 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1473 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1474 TAILQ_FOREACH(v, views, entry) {
1480 if (v->child && v->child->reset) {
1481 err = v->child->reset(v->child);
1488 err = view->input(new, view, ch);
1496 view_needs_focus_indication(struct tog_view *view)
1498 if (view_is_parent_view(view)) {
1499 if (view->child == NULL || view->child->focussed)
1501 if (!view_is_splitscreen(view->child))
1503 } else if (!view_is_splitscreen(view))
1506 return view->focussed;
1509 static const struct got_error *
1510 view_loop(struct tog_view *view)
1512 const struct got_error *err = NULL;
1513 struct tog_view_list_head views;
1514 struct tog_view *new_view;
1516 int fast_refresh = 10;
1517 int done = 0, errcode;
1519 mode = getenv("TOG_VIEW_SPLIT_MODE");
1520 if (!mode || !(*mode == 'h' || *mode == 'H'))
1521 view->mode = TOG_VIEW_SPLIT_VERT;
1523 view->mode = TOG_VIEW_SPLIT_HRZN;
1525 errcode = pthread_mutex_lock(&tog_mutex);
1527 return got_error_set_errno(errcode, "pthread_mutex_lock");
1530 TAILQ_INSERT_HEAD(&views, view, entry);
1533 err = view->show(view);
1538 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1539 !tog_fatal_signal_received()) {
1540 /* Refresh fast during initialization, then become slower. */
1541 if (fast_refresh && fast_refresh-- == 0)
1542 halfdelay(10); /* switch to once per second */
1544 err = view_input(&new_view, &done, view, &views);
1548 struct tog_view *v, *prev = NULL;
1550 if (view_is_parent_view(view))
1551 prev = TAILQ_PREV(view, tog_view_list_head,
1553 else if (view->parent)
1554 prev = view->parent;
1557 view->parent->child = NULL;
1558 view->parent->focus_child = 0;
1559 /* Restore fullscreen line height. */
1560 view->parent->nlines = view->parent->lines;
1561 err = view_resize(view->parent);
1564 /* Make resized splits persist. */
1565 view_transfer_size(view->parent, view);
1567 TAILQ_REMOVE(&views, view, entry);
1569 err = view_close(view);
1574 TAILQ_FOREACH(v, &views, entry) {
1578 if (view == NULL && new_view == NULL) {
1579 /* No view has focus. Try to pick one. */
1582 else if (!TAILQ_EMPTY(&views)) {
1583 view = TAILQ_LAST(&views,
1584 tog_view_list_head);
1587 if (view->focus_child) {
1588 view->child->focussed = 1;
1596 struct tog_view *v, *t;
1597 /* Only allow one parent view per type. */
1598 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1599 if (v->type != new_view->type)
1601 TAILQ_REMOVE(&views, v, entry);
1602 err = view_close(v);
1607 TAILQ_INSERT_TAIL(&views, new_view, entry);
1611 if (view_is_parent_view(view)) {
1612 if (view->child && view->child->focussed)
1615 if (view->parent && view->parent->focussed)
1616 view = view->parent;
1618 show_panel(view->panel);
1619 if (view->child && view_is_splitscreen(view->child))
1620 show_panel(view->child->panel);
1621 if (view->parent && view_is_splitscreen(view)) {
1622 err = view->parent->show(view->parent);
1626 err = view->show(view);
1630 err = view->child->show(view->child);
1639 while (!TAILQ_EMPTY(&views)) {
1640 const struct got_error *close_err;
1641 view = TAILQ_FIRST(&views);
1642 TAILQ_REMOVE(&views, view, entry);
1643 close_err = view_close(view);
1644 if (close_err && err == NULL)
1648 errcode = pthread_mutex_unlock(&tog_mutex);
1649 if (errcode && err == NULL)
1650 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1660 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1665 /* Create newly allocated wide-character string equivalent to a byte string. */
1666 static const struct got_error *
1667 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1670 const struct got_error *err = NULL;
1673 *wlen = mbstowcs(NULL, s, 0);
1674 if (*wlen == (size_t)-1) {
1676 if (errno != EILSEQ)
1677 return got_error_from_errno("mbstowcs");
1679 /* byte string invalid in current encoding; try to "fix" it */
1680 err = got_mbsavis(&vis, &vislen, s);
1683 *wlen = mbstowcs(NULL, vis, 0);
1684 if (*wlen == (size_t)-1) {
1685 err = got_error_from_errno("mbstowcs"); /* give up */
1690 *ws = calloc(*wlen + 1, sizeof(**ws));
1692 err = got_error_from_errno("calloc");
1696 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1697 err = got_error_from_errno("mbstowcs");
1708 static const struct got_error *
1709 expand_tab(char **ptr, const char *src)
1712 size_t len, n, idx = 0, sz = 0;
1715 n = len = strlen(src);
1716 dst = malloc(n + 1);
1718 return got_error_from_errno("malloc");
1720 while (idx < len && src[idx]) {
1721 const char c = src[idx];
1724 size_t nb = TABSIZE - sz % TABSIZE;
1727 p = realloc(dst, n + nb);
1730 return got_error_from_errno("realloc");
1735 memset(dst + sz, ' ', nb);
1738 dst[sz++] = src[idx];
1748 * Advance at most n columns from wline starting at offset off.
1749 * Return the index to the first character after the span operation.
1750 * Return the combined column width of all spanned wide character in
1754 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1756 int width, i, cols = 0;
1763 for (i = off; wline[i] != L'\0'; ++i) {
1764 if (wline[i] == L'\t')
1765 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1767 width = wcwidth(wline[i]);
1774 if (cols + width > n)
1784 * Format a line for display, ensuring that it won't overflow a width limit.
1785 * With scrolling, the width returned refers to the scrolled version of the
1786 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1788 static const struct got_error *
1789 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1790 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1792 const struct got_error *err = NULL;
1794 wchar_t *wline = NULL;
1803 err = expand_tab(&exstr, line);
1808 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1813 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1815 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1816 wline[wlen - 1] = L'\0';
1819 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1820 wline[wlen - 1] = L'\0';
1824 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1830 *scrollxp = scrollx;
1838 static const struct got_error*
1839 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1840 struct got_object_id *id, struct got_repository *repo)
1842 static const struct got_error *err = NULL;
1843 struct got_reflist_entry *re;
1849 TAILQ_FOREACH(re, refs, entry) {
1850 struct got_tag_object *tag = NULL;
1851 struct got_object_id *ref_id;
1854 name = got_ref_get_name(re->ref);
1855 if (strcmp(name, GOT_REF_HEAD) == 0)
1857 if (strncmp(name, "refs/", 5) == 0)
1859 if (strncmp(name, "got/", 4) == 0 &&
1860 strncmp(name, "got/backup/", 11) != 0)
1862 if (strncmp(name, "heads/", 6) == 0)
1864 if (strncmp(name, "remotes/", 8) == 0) {
1866 s = strstr(name, "/" GOT_REF_HEAD);
1867 if (s != NULL && s[strlen(s)] == '\0')
1870 err = got_ref_resolve(&ref_id, repo, re->ref);
1873 if (strncmp(name, "tags/", 5) == 0) {
1874 err = got_object_open_as_tag(&tag, repo, ref_id);
1876 if (err->code != GOT_ERR_OBJ_TYPE) {
1880 /* Ref points at something other than a tag. */
1885 cmp = got_object_id_cmp(tag ?
1886 got_object_tag_get_object_id(tag) : ref_id, id);
1889 got_object_tag_close(tag);
1893 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1894 s ? ", " : "", name) == -1) {
1895 err = got_error_from_errno("asprintf");
1906 static const struct got_error *
1907 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1912 smallerthan = strchr(author, '<');
1913 if (smallerthan && smallerthan[1] != '\0')
1914 author = smallerthan + 1;
1915 author[strcspn(author, "@>")] = '\0';
1916 return format_line(wauthor, author_width, NULL, author, 0, limit,
1920 static const struct got_error *
1921 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1922 struct got_object_id *id, const size_t date_display_cols,
1923 int author_display_cols)
1925 struct tog_log_view_state *s = &view->state.log;
1926 const struct got_error *err = NULL;
1927 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1928 char *logmsg0 = NULL, *logmsg = NULL;
1929 char *author = NULL;
1930 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1931 int author_width, logmsg_width;
1932 char *newline, *line = NULL;
1933 int col, limit, scrollx;
1934 const int avail = view->ncols;
1936 time_t committer_time;
1937 struct tog_color *tc;
1939 committer_time = got_object_commit_get_committer_time(commit);
1940 if (gmtime_r(&committer_time, &tm) == NULL)
1941 return got_error_from_errno("gmtime_r");
1942 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1943 return got_error(GOT_ERR_NO_SPACE);
1945 if (avail <= date_display_cols)
1946 limit = MIN(sizeof(datebuf) - 1, avail);
1948 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1949 tc = get_color(&s->colors, TOG_COLOR_DATE);
1951 wattr_on(view->window,
1952 COLOR_PAIR(tc->colorpair), NULL);
1953 waddnstr(view->window, datebuf, limit);
1955 wattr_off(view->window,
1956 COLOR_PAIR(tc->colorpair), NULL);
1963 err = got_object_id_str(&id_str, id);
1966 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1968 wattr_on(view->window,
1969 COLOR_PAIR(tc->colorpair), NULL);
1970 wprintw(view->window, "%.8s ", id_str);
1972 wattr_off(view->window,
1973 COLOR_PAIR(tc->colorpair), NULL);
1980 if (s->use_committer)
1981 author = strdup(got_object_commit_get_committer(commit));
1983 author = strdup(got_object_commit_get_author(commit));
1984 if (author == NULL) {
1985 err = got_error_from_errno("strdup");
1988 err = format_author(&wauthor, &author_width, author, avail - col, col);
1991 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1993 wattr_on(view->window,
1994 COLOR_PAIR(tc->colorpair), NULL);
1995 waddwstr(view->window, wauthor);
1997 wattr_off(view->window,
1998 COLOR_PAIR(tc->colorpair), NULL);
1999 col += author_width;
2000 while (col < avail && author_width < author_display_cols + 2) {
2001 waddch(view->window, ' ');
2008 err = got_object_commit_get_logmsg(&logmsg0, commit);
2012 while (*logmsg == '\n')
2014 newline = strchr(logmsg, '\n');
2017 limit = avail - col;
2018 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2019 limit--; /* for the border */
2020 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2024 waddwstr(view->window, &wlogmsg[scrollx]);
2025 col += MAX(logmsg_width, 0);
2026 while (col < avail) {
2027 waddch(view->window, ' ');
2039 static struct commit_queue_entry *
2040 alloc_commit_queue_entry(struct got_commit_object *commit,
2041 struct got_object_id *id)
2043 struct commit_queue_entry *entry;
2045 entry = calloc(1, sizeof(*entry));
2050 entry->commit = commit;
2055 pop_commit(struct commit_queue *commits)
2057 struct commit_queue_entry *entry;
2059 entry = TAILQ_FIRST(&commits->head);
2060 TAILQ_REMOVE(&commits->head, entry, entry);
2061 got_object_commit_close(entry->commit);
2062 commits->ncommits--;
2063 /* Don't free entry->id! It is owned by the commit graph. */
2068 free_commits(struct commit_queue *commits)
2070 while (!TAILQ_EMPTY(&commits->head))
2071 pop_commit(commits);
2074 static const struct got_error *
2075 match_commit(int *have_match, struct got_object_id *id,
2076 struct got_commit_object *commit, regex_t *regex)
2078 const struct got_error *err = NULL;
2079 regmatch_t regmatch;
2080 char *id_str = NULL, *logmsg = NULL;
2084 err = got_object_id_str(&id_str, id);
2088 err = got_object_commit_get_logmsg(&logmsg, commit);
2092 if (regexec(regex, got_object_commit_get_author(commit), 1,
2093 ®match, 0) == 0 ||
2094 regexec(regex, got_object_commit_get_committer(commit), 1,
2095 ®match, 0) == 0 ||
2096 regexec(regex, id_str, 1, ®match, 0) == 0 ||
2097 regexec(regex, logmsg, 1, ®match, 0) == 0)
2105 static const struct got_error *
2106 queue_commits(struct tog_log_thread_args *a)
2108 const struct got_error *err = NULL;
2111 * We keep all commits open throughout the lifetime of the log
2112 * view in order to avoid having to re-fetch commits from disk
2113 * while updating the display.
2116 struct got_object_id *id;
2117 struct got_commit_object *commit;
2118 struct commit_queue_entry *entry;
2121 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2123 if (err || id == NULL)
2126 err = got_object_open_as_commit(&commit, a->repo, id);
2129 entry = alloc_commit_queue_entry(commit, id);
2130 if (entry == NULL) {
2131 err = got_error_from_errno("alloc_commit_queue_entry");
2135 errcode = pthread_mutex_lock(&tog_mutex);
2137 err = got_error_set_errno(errcode,
2138 "pthread_mutex_lock");
2142 entry->idx = a->commits->ncommits;
2143 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2144 a->commits->ncommits++;
2146 if (*a->searching == TOG_SEARCH_FORWARD &&
2147 !*a->search_next_done) {
2149 err = match_commit(&have_match, id, commit, a->regex);
2153 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2156 errcode = pthread_mutex_unlock(&tog_mutex);
2157 if (errcode && err == NULL)
2158 err = got_error_set_errno(errcode,
2159 "pthread_mutex_unlock");
2162 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2168 select_commit(struct tog_log_view_state *s)
2170 struct commit_queue_entry *entry;
2173 entry = s->first_displayed_entry;
2175 if (ncommits == s->selected) {
2176 s->selected_entry = entry;
2179 entry = TAILQ_NEXT(entry, entry);
2184 static const struct got_error *
2185 draw_commits(struct tog_view *view)
2187 const struct got_error *err = NULL;
2188 struct tog_log_view_state *s = &view->state.log;
2189 struct commit_queue_entry *entry = s->selected_entry;
2190 const int limit = view->nlines;
2192 int ncommits, author_cols = 4;
2193 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2194 char *refs_str = NULL;
2196 struct tog_color *tc;
2197 static const size_t date_display_cols = 12;
2199 if (s->selected_entry &&
2200 !(view->searching && view->search_next_done == 0)) {
2201 struct got_reflist_head *refs;
2202 err = got_object_id_str(&id_str, s->selected_entry->id);
2205 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2206 s->selected_entry->id);
2208 err = build_refs_str(&refs_str, refs,
2209 s->selected_entry->id, s->repo);
2215 if (s->thread_args.commits_needed == 0)
2216 halfdelay(10); /* disable fast refresh */
2218 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2219 if (asprintf(&ncommits_str, " [%d/%d] %s",
2220 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2221 (view->searching && !view->search_next_done) ?
2222 "searching..." : "loading...") == -1) {
2223 err = got_error_from_errno("asprintf");
2227 const char *search_str = NULL;
2229 if (view->searching) {
2230 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2231 search_str = "no more matches";
2232 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2233 search_str = "no matches found";
2234 else if (!view->search_next_done)
2235 search_str = "searching...";
2238 if (asprintf(&ncommits_str, " [%d/%d] %s",
2239 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2240 search_str ? search_str :
2241 (refs_str ? refs_str : "")) == -1) {
2242 err = got_error_from_errno("asprintf");
2247 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2248 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2249 "........................................",
2250 s->in_repo_path, ncommits_str) == -1) {
2251 err = got_error_from_errno("asprintf");
2255 } else if (asprintf(&header, "commit %s%s",
2256 id_str ? id_str : "........................................",
2257 ncommits_str) == -1) {
2258 err = got_error_from_errno("asprintf");
2262 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2266 werase(view->window);
2268 if (view_needs_focus_indication(view))
2269 wstandout(view->window);
2270 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2272 wattr_on(view->window,
2273 COLOR_PAIR(tc->colorpair), NULL);
2274 waddwstr(view->window, wline);
2276 wattr_off(view->window,
2277 COLOR_PAIR(tc->colorpair), NULL);
2278 while (width < view->ncols) {
2279 waddch(view->window, ' ');
2282 if (view_needs_focus_indication(view))
2283 wstandend(view->window);
2288 /* Grow author column size if necessary, and set view->maxx. */
2289 entry = s->first_displayed_entry;
2293 struct got_commit_object *c = entry->commit;
2294 char *author, *eol, *msg, *msg0;
2295 wchar_t *wauthor, *wmsg;
2297 if (ncommits >= limit - 1)
2299 if (s->use_committer)
2300 author = strdup(got_object_commit_get_committer(c));
2302 author = strdup(got_object_commit_get_author(c));
2303 if (author == NULL) {
2304 err = got_error_from_errno("strdup");
2307 err = format_author(&wauthor, &width, author, COLS,
2309 if (author_cols < width)
2310 author_cols = width;
2315 err = got_object_commit_get_logmsg(&msg0, c);
2319 while (*msg == '\n')
2321 if ((eol = strchr(msg, '\n')))
2323 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2324 date_display_cols + author_cols, 0);
2327 view->maxx = MAX(view->maxx, width);
2331 entry = TAILQ_NEXT(entry, entry);
2334 entry = s->first_displayed_entry;
2335 s->last_displayed_entry = s->first_displayed_entry;
2338 if (ncommits >= limit - 1)
2340 if (ncommits == s->selected)
2341 wstandout(view->window);
2342 err = draw_commit(view, entry->commit, entry->id,
2343 date_display_cols, author_cols);
2344 if (ncommits == s->selected)
2345 wstandend(view->window);
2349 s->last_displayed_entry = entry;
2350 entry = TAILQ_NEXT(entry, entry);
2363 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2365 struct commit_queue_entry *entry;
2368 entry = TAILQ_FIRST(&s->commits.head);
2369 if (s->first_displayed_entry == entry)
2372 entry = s->first_displayed_entry;
2373 while (entry && nscrolled < maxscroll) {
2374 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2376 s->first_displayed_entry = entry;
2382 static const struct got_error *
2383 trigger_log_thread(struct tog_view *view, int wait)
2385 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2388 halfdelay(1); /* fast refresh while loading commits */
2390 while (!ta->log_complete && !tog_thread_error &&
2391 (ta->commits_needed > 0 || ta->load_all)) {
2392 /* Wake the log thread. */
2393 errcode = pthread_cond_signal(&ta->need_commits);
2395 return got_error_set_errno(errcode,
2396 "pthread_cond_signal");
2399 * The mutex will be released while the view loop waits
2400 * in wgetch(), at which time the log thread will run.
2405 /* Display progress update in log view. */
2406 show_log_view(view);
2410 /* Wait right here while next commit is being loaded. */
2411 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2413 return got_error_set_errno(errcode,
2414 "pthread_cond_wait");
2416 /* Display progress update in log view. */
2417 show_log_view(view);
2425 static const struct got_error *
2426 request_log_commits(struct tog_view *view)
2428 struct tog_log_view_state *state = &view->state.log;
2429 const struct got_error *err = NULL;
2431 if (state->thread_args.log_complete)
2434 state->thread_args.commits_needed += view->nscrolled;
2435 err = trigger_log_thread(view, 1);
2436 view->nscrolled = 0;
2441 static const struct got_error *
2442 log_scroll_down(struct tog_view *view, int maxscroll)
2444 struct tog_log_view_state *s = &view->state.log;
2445 const struct got_error *err = NULL;
2446 struct commit_queue_entry *pentry;
2447 int nscrolled = 0, ncommits_needed;
2449 if (s->last_displayed_entry == NULL)
2452 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2453 if (s->commits.ncommits < ncommits_needed &&
2454 !s->thread_args.log_complete) {
2456 * Ask the log thread for required amount of commits.
2458 s->thread_args.commits_needed += maxscroll;
2459 err = trigger_log_thread(view, 1);
2465 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2466 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2469 s->last_displayed_entry = pentry ?
2470 pentry : s->last_displayed_entry;;
2472 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2475 s->first_displayed_entry = pentry;
2476 } while (++nscrolled < maxscroll);
2478 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2479 view->nscrolled += nscrolled;
2481 view->nscrolled = 0;
2486 static const struct got_error *
2487 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2488 struct got_commit_object *commit, struct got_object_id *commit_id,
2489 struct tog_view *log_view, struct got_repository *repo)
2491 const struct got_error *err;
2492 struct got_object_qid *parent_id;
2493 struct tog_view *diff_view;
2495 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2496 if (diff_view == NULL)
2497 return got_error_from_errno("view_open");
2499 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2500 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2501 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2503 *new_view = diff_view;
2507 static const struct got_error *
2508 tree_view_visit_subtree(struct tog_tree_view_state *s,
2509 struct got_tree_object *subtree)
2511 struct tog_parent_tree *parent;
2513 parent = calloc(1, sizeof(*parent));
2515 return got_error_from_errno("calloc");
2517 parent->tree = s->tree;
2518 parent->first_displayed_entry = s->first_displayed_entry;
2519 parent->selected_entry = s->selected_entry;
2520 parent->selected = s->selected;
2521 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2524 s->first_displayed_entry = NULL;
2528 static const struct got_error *
2529 tree_view_walk_path(struct tog_tree_view_state *s,
2530 struct got_commit_object *commit, const char *path)
2532 const struct got_error *err = NULL;
2533 struct got_tree_object *tree = NULL;
2535 char *slash, *subpath = NULL;
2537 /* Walk the path and open corresponding tree objects. */
2540 struct got_tree_entry *te;
2541 struct got_object_id *tree_id;
2547 /* Ensure the correct subtree entry is selected. */
2548 slash = strchr(p, '/');
2550 te_name = strdup(p);
2552 te_name = strndup(p, slash - p);
2553 if (te_name == NULL) {
2554 err = got_error_from_errno("strndup");
2557 te = got_object_tree_find_entry(s->tree, te_name);
2559 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2564 s->first_displayed_entry = s->selected_entry = te;
2566 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2567 break; /* jump to this file's entry */
2569 slash = strchr(p, '/');
2571 subpath = strndup(path, slash - path);
2573 subpath = strdup(path);
2574 if (subpath == NULL) {
2575 err = got_error_from_errno("strdup");
2579 err = got_object_id_by_path(&tree_id, s->repo, commit,
2584 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2589 err = tree_view_visit_subtree(s, tree);
2591 got_object_tree_close(tree);
2605 static const struct got_error *
2606 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2607 struct commit_queue_entry *entry, const char *path,
2608 const char *head_ref_name, struct got_repository *repo)
2610 const struct got_error *err = NULL;
2611 struct tog_tree_view_state *s;
2612 struct tog_view *tree_view;
2614 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2615 if (tree_view == NULL)
2616 return got_error_from_errno("view_open");
2618 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2621 s = &tree_view->state.tree;
2623 *new_view = tree_view;
2625 if (got_path_is_root_dir(path))
2628 return tree_view_walk_path(s, entry->commit, path);
2631 static const struct got_error *
2632 block_signals_used_by_main_thread(void)
2637 if (sigemptyset(&sigset) == -1)
2638 return got_error_from_errno("sigemptyset");
2640 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2641 if (sigaddset(&sigset, SIGWINCH) == -1)
2642 return got_error_from_errno("sigaddset");
2643 if (sigaddset(&sigset, SIGCONT) == -1)
2644 return got_error_from_errno("sigaddset");
2645 if (sigaddset(&sigset, SIGINT) == -1)
2646 return got_error_from_errno("sigaddset");
2647 if (sigaddset(&sigset, SIGTERM) == -1)
2648 return got_error_from_errno("sigaddset");
2650 /* ncurses handles SIGTSTP */
2651 if (sigaddset(&sigset, SIGTSTP) == -1)
2652 return got_error_from_errno("sigaddset");
2654 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2656 return got_error_set_errno(errcode, "pthread_sigmask");
2662 log_thread(void *arg)
2664 const struct got_error *err = NULL;
2666 struct tog_log_thread_args *a = arg;
2670 * Sync startup with main thread such that we begin our
2671 * work once view_input() has released the mutex.
2673 errcode = pthread_mutex_lock(&tog_mutex);
2675 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2679 err = block_signals_used_by_main_thread();
2681 pthread_mutex_unlock(&tog_mutex);
2685 while (!done && !err && !tog_fatal_signal_received()) {
2686 errcode = pthread_mutex_unlock(&tog_mutex);
2688 err = got_error_set_errno(errcode,
2689 "pthread_mutex_unlock");
2692 err = queue_commits(a);
2694 if (err->code != GOT_ERR_ITER_COMPLETED)
2698 } else if (a->commits_needed > 0 && !a->load_all)
2699 a->commits_needed--;
2701 errcode = pthread_mutex_lock(&tog_mutex);
2703 err = got_error_set_errno(errcode,
2704 "pthread_mutex_lock");
2706 } else if (*a->quit)
2708 else if (*a->first_displayed_entry == NULL) {
2709 *a->first_displayed_entry =
2710 TAILQ_FIRST(&a->commits->head);
2711 *a->selected_entry = *a->first_displayed_entry;
2714 errcode = pthread_cond_signal(&a->commit_loaded);
2716 err = got_error_set_errno(errcode,
2717 "pthread_cond_signal");
2718 pthread_mutex_unlock(&tog_mutex);
2723 a->commits_needed = 0;
2725 if (a->commits_needed == 0 && !a->load_all) {
2726 errcode = pthread_cond_wait(&a->need_commits,
2729 err = got_error_set_errno(errcode,
2730 "pthread_cond_wait");
2731 pthread_mutex_unlock(&tog_mutex);
2739 a->log_complete = 1;
2740 errcode = pthread_mutex_unlock(&tog_mutex);
2742 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2745 tog_thread_error = 1;
2746 pthread_cond_signal(&a->commit_loaded);
2751 static const struct got_error *
2752 stop_log_thread(struct tog_log_view_state *s)
2754 const struct got_error *err = NULL, *thread_err = NULL;
2759 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2761 return got_error_set_errno(errcode,
2762 "pthread_cond_signal");
2763 errcode = pthread_mutex_unlock(&tog_mutex);
2765 return got_error_set_errno(errcode,
2766 "pthread_mutex_unlock");
2767 errcode = pthread_join(s->thread, (void **)&thread_err);
2769 return got_error_set_errno(errcode, "pthread_join");
2770 errcode = pthread_mutex_lock(&tog_mutex);
2772 return got_error_set_errno(errcode,
2773 "pthread_mutex_lock");
2774 s->thread = 0; //NULL;
2777 if (s->thread_args.repo) {
2778 err = got_repo_close(s->thread_args.repo);
2779 s->thread_args.repo = NULL;
2782 if (s->thread_args.pack_fds) {
2783 const struct got_error *pack_err =
2784 got_repo_pack_fds_close(s->thread_args.pack_fds);
2787 s->thread_args.pack_fds = NULL;
2790 if (s->thread_args.graph) {
2791 got_commit_graph_close(s->thread_args.graph);
2792 s->thread_args.graph = NULL;
2795 return err ? err : thread_err;
2798 static const struct got_error *
2799 close_log_view(struct tog_view *view)
2801 const struct got_error *err = NULL;
2802 struct tog_log_view_state *s = &view->state.log;
2805 err = stop_log_thread(s);
2807 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2808 if (errcode && err == NULL)
2809 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2811 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2812 if (errcode && err == NULL)
2813 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2815 free_commits(&s->commits);
2816 free(s->in_repo_path);
2817 s->in_repo_path = NULL;
2820 free(s->head_ref_name);
2821 s->head_ref_name = NULL;
2825 static const struct got_error *
2826 search_start_log_view(struct tog_view *view)
2828 struct tog_log_view_state *s = &view->state.log;
2830 s->matched_entry = NULL;
2831 s->search_entry = NULL;
2835 static const struct got_error *
2836 search_next_log_view(struct tog_view *view)
2838 const struct got_error *err = NULL;
2839 struct tog_log_view_state *s = &view->state.log;
2840 struct commit_queue_entry *entry;
2842 /* Display progress update in log view. */
2843 show_log_view(view);
2847 if (s->search_entry) {
2849 errcode = pthread_mutex_unlock(&tog_mutex);
2851 return got_error_set_errno(errcode,
2852 "pthread_mutex_unlock");
2853 ch = wgetch(view->window);
2854 errcode = pthread_mutex_lock(&tog_mutex);
2856 return got_error_set_errno(errcode,
2857 "pthread_mutex_lock");
2858 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2859 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2862 if (view->searching == TOG_SEARCH_FORWARD)
2863 entry = TAILQ_NEXT(s->search_entry, entry);
2865 entry = TAILQ_PREV(s->search_entry,
2866 commit_queue_head, entry);
2867 } else if (s->matched_entry) {
2868 int matched_idx = s->matched_entry->idx;
2869 int selected_idx = s->selected_entry->idx;
2872 * If the user has moved the cursor after we hit a match,
2873 * the position from where we should continue searching
2874 * might have changed.
2876 if (view->searching == TOG_SEARCH_FORWARD) {
2877 if (matched_idx > selected_idx)
2878 entry = TAILQ_NEXT(s->selected_entry, entry);
2880 entry = TAILQ_NEXT(s->matched_entry, entry);
2882 if (matched_idx < selected_idx)
2883 entry = TAILQ_PREV(s->selected_entry,
2884 commit_queue_head, entry);
2886 entry = TAILQ_PREV(s->matched_entry,
2887 commit_queue_head, entry);
2890 entry = s->selected_entry;
2896 if (entry == NULL) {
2897 if (s->thread_args.log_complete ||
2898 view->searching == TOG_SEARCH_BACKWARD) {
2899 view->search_next_done =
2900 (s->matched_entry == NULL ?
2901 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2902 s->search_entry = NULL;
2906 * Poke the log thread for more commits and return,
2907 * allowing the main loop to make progress. Search
2908 * will resume at s->search_entry once we come back.
2910 s->thread_args.commits_needed++;
2911 return trigger_log_thread(view, 0);
2914 err = match_commit(&have_match, entry->id, entry->commit,
2919 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2920 s->matched_entry = entry;
2924 s->search_entry = entry;
2925 if (view->searching == TOG_SEARCH_FORWARD)
2926 entry = TAILQ_NEXT(entry, entry);
2928 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2931 if (s->matched_entry) {
2932 int cur = s->selected_entry->idx;
2933 while (cur < s->matched_entry->idx) {
2934 err = input_log_view(NULL, view, KEY_DOWN);
2939 while (cur > s->matched_entry->idx) {
2940 err = input_log_view(NULL, view, KEY_UP);
2947 s->search_entry = NULL;
2952 static const struct got_error *
2953 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2954 struct got_repository *repo, const char *head_ref_name,
2955 const char *in_repo_path, int log_branches)
2957 const struct got_error *err = NULL;
2958 struct tog_log_view_state *s = &view->state.log;
2959 struct got_repository *thread_repo = NULL;
2960 struct got_commit_graph *thread_graph = NULL;
2963 if (in_repo_path != s->in_repo_path) {
2964 free(s->in_repo_path);
2965 s->in_repo_path = strdup(in_repo_path);
2966 if (s->in_repo_path == NULL)
2967 return got_error_from_errno("strdup");
2970 /* The commit queue only contains commits being displayed. */
2971 TAILQ_INIT(&s->commits.head);
2972 s->commits.ncommits = 0;
2975 if (head_ref_name) {
2976 s->head_ref_name = strdup(head_ref_name);
2977 if (s->head_ref_name == NULL) {
2978 err = got_error_from_errno("strdup");
2982 s->start_id = got_object_id_dup(start_id);
2983 if (s->start_id == NULL) {
2984 err = got_error_from_errno("got_object_id_dup");
2987 s->log_branches = log_branches;
2989 STAILQ_INIT(&s->colors);
2990 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2991 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2992 get_color_value("TOG_COLOR_COMMIT"));
2995 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2996 get_color_value("TOG_COLOR_AUTHOR"));
2998 free_colors(&s->colors);
3001 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3002 get_color_value("TOG_COLOR_DATE"));
3004 free_colors(&s->colors);
3009 view->show = show_log_view;
3010 view->input = input_log_view;
3011 view->resize = resize_log_view;
3012 view->close = close_log_view;
3013 view->search_start = search_start_log_view;
3014 view->search_next = search_next_log_view;
3016 if (s->thread_args.pack_fds == NULL) {
3017 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3021 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3022 s->thread_args.pack_fds);
3025 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3029 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3030 s->repo, NULL, NULL);
3034 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3036 err = got_error_set_errno(errcode, "pthread_cond_init");
3039 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3041 err = got_error_set_errno(errcode, "pthread_cond_init");
3045 s->thread_args.commits_needed = view->nlines;
3046 s->thread_args.graph = thread_graph;
3047 s->thread_args.commits = &s->commits;
3048 s->thread_args.in_repo_path = s->in_repo_path;
3049 s->thread_args.start_id = s->start_id;
3050 s->thread_args.repo = thread_repo;
3051 s->thread_args.log_complete = 0;
3052 s->thread_args.quit = &s->quit;
3053 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3054 s->thread_args.selected_entry = &s->selected_entry;
3055 s->thread_args.searching = &view->searching;
3056 s->thread_args.search_next_done = &view->search_next_done;
3057 s->thread_args.regex = &view->regex;
3060 close_log_view(view);
3064 static const struct got_error *
3065 show_log_view(struct tog_view *view)
3067 const struct got_error *err;
3068 struct tog_log_view_state *s = &view->state.log;
3070 if (s->thread == 0) { //NULL) {
3071 int errcode = pthread_create(&s->thread, NULL, log_thread,
3074 return got_error_set_errno(errcode, "pthread_create");
3075 if (s->thread_args.commits_needed > 0) {
3076 err = trigger_log_thread(view, 1);
3082 return draw_commits(view);
3086 log_move_cursor_up(struct tog_view *view, int page, int home)
3088 struct tog_log_view_state *s = &view->state.log;
3090 if (s->selected_entry->idx == 0)
3092 if (s->first_displayed_entry == NULL)
3095 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3097 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3099 if (!page && !home && s->selected > 0)
3102 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3108 static const struct got_error *
3109 log_move_cursor_down(struct tog_view *view, int page)
3111 struct tog_log_view_state *s = &view->state.log;
3112 struct commit_queue_entry *first;
3113 const struct got_error *err = NULL;
3115 first = s->first_displayed_entry;
3116 if (first == NULL) {
3121 if (s->thread_args.log_complete &&
3122 s->selected_entry->idx >= s->commits.ncommits - 1)
3126 int eos = view->nlines - 2;
3128 if (view_is_hsplit_top(view))
3129 --eos; /* border consumes the last line */
3130 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3133 err = log_scroll_down(view, 1);
3134 } else if (s->thread_args.load_all) {
3135 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3136 s->selected += MIN(s->last_displayed_entry->idx -
3137 s->selected_entry->idx, page + 1);
3139 err = log_scroll_down(view, MIN(page,
3140 s->commits.ncommits - s->selected_entry->idx - 1));
3141 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3143 err = log_scroll_down(view, page);
3146 if (first == s->first_displayed_entry && s->selected <
3147 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3148 s->selected = MIN(s->commits.ncommits - 1, page);
3155 * We might necessarily overshoot in horizontal
3156 * splits; if so, select the last displayed commit.
3158 s->selected = MIN(s->selected,
3159 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3163 if (s->thread_args.log_complete &&
3164 s->selected_entry->idx == s->commits.ncommits - 1)
3171 view_get_split(struct tog_view *view, int *y, int *x)
3176 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3177 if (view->child && view->child->resized_y)
3178 *y = view->child->resized_y;
3179 else if (view->resized_y)
3180 *y = view->resized_y;
3182 *y = view_split_begin_y(view->lines);
3183 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3184 if (view->child && view->child->resized_x)
3185 *x = view->child->resized_x;
3186 else if (view->resized_x)
3187 *x = view->resized_x;
3189 *x = view_split_begin_x(view->begin_x);
3193 /* Split view horizontally at y and offset view->state->selected line. */
3194 static const struct got_error *
3195 view_init_hsplit(struct tog_view *view, int y)
3197 const struct got_error *err = NULL;
3201 err = view_resize(view);
3205 err = offset_selection_down(view);
3210 static const struct got_error *
3211 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3213 const struct got_error *err = NULL;
3214 struct tog_log_view_state *s = &view->state.log;
3215 struct tog_view *diff_view = NULL, *tree_view = NULL;
3216 struct tog_view *ref_view = NULL;
3217 struct commit_queue_entry *entry;
3218 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3220 if (s->thread_args.load_all) {
3221 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3222 s->thread_args.load_all = 0;
3223 else if (s->thread_args.log_complete) {
3224 err = log_move_cursor_down(view, s->commits.ncommits);
3225 s->thread_args.load_all = 0;
3230 eos = nscroll = view->nlines - 1;
3231 if (view_is_hsplit_top(view))
3242 view->x = MAX(view->maxx - view->ncols / 2, 0);
3247 if (view->x + view->ncols / 2 < view->maxx)
3248 view->x += 2; /* move two columns right */
3254 view->x -= MIN(view->x, 2); /* move two columns back */
3263 log_move_cursor_up(view, 0, 0);
3267 log_move_cursor_up(view, 0, 1);
3277 log_move_cursor_up(view, nscroll, 0);
3284 err = log_move_cursor_down(view, 0);
3287 s->use_committer = !s->use_committer;
3291 /* We don't know yet how many commits, so we're forced to
3292 * traverse them all. */
3294 if (!s->thread_args.log_complete) {
3295 s->thread_args.load_all = 1;
3296 return trigger_log_thread(view, 0);
3300 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3301 for (n = 0; n < eos; n++) {
3304 s->first_displayed_entry = entry;
3305 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3308 s->selected = n - 1;
3320 err = log_move_cursor_down(view, nscroll);
3323 if (s->selected > view->nlines - 2)
3324 s->selected = view->nlines - 2;
3325 if (s->selected > s->commits.ncommits - 1)
3326 s->selected = s->commits.ncommits - 1;
3328 if (s->commits.ncommits < view->nlines - 1 &&
3329 !s->thread_args.log_complete) {
3330 s->thread_args.commits_needed += (view->nlines - 1) -
3331 s->commits.ncommits;
3332 err = trigger_log_thread(view, 1);
3338 if (s->selected_entry == NULL)
3341 /* get dimensions--don't split till initialisation succeeds */
3342 if (view_is_parent_view(view))
3343 view_get_split(view, &begin_y, &begin_x);
3345 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3346 s->selected_entry->commit, s->selected_entry->id,
3351 if (view_is_parent_view(view) &&
3352 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3353 err = view_init_hsplit(view, begin_y);
3359 diff_view->focussed = 1;
3360 diff_view->mode = view->mode;
3361 diff_view->nlines = view->lines - begin_y;
3363 if (view_is_parent_view(view)) {
3364 view_transfer_size(diff_view, view);
3365 err = view_close_child(view);
3368 err = view_set_child(view, diff_view);
3371 view->focus_child = 1;
3373 *new_view = diff_view;
3377 if (s->selected_entry == NULL)
3379 if (view_is_parent_view(view))
3380 view_get_split(view, &begin_y, &begin_x);
3381 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3382 s->selected_entry, s->in_repo_path, s->head_ref_name,
3386 if (view_is_parent_view(view) &&
3387 view->mode == TOG_VIEW_SPLIT_HRZN) {
3388 err = view_init_hsplit(view, begin_y);
3393 tree_view->focussed = 1;
3394 tree_view->mode = view->mode;
3395 tree_view->nlines = view->lines - begin_y;
3396 if (view_is_parent_view(view)) {
3397 view_transfer_size(tree_view, view);
3398 err = view_close_child(view);
3401 err = view_set_child(view, tree_view);
3404 view->focus_child = 1;
3406 *new_view = tree_view;
3412 if (ch == KEY_BACKSPACE &&
3413 got_path_is_root_dir(s->in_repo_path))
3415 err = stop_log_thread(s);
3418 if (ch == KEY_BACKSPACE) {
3420 err = got_path_dirname(&parent_path, s->in_repo_path);
3423 free(s->in_repo_path);
3424 s->in_repo_path = parent_path;
3425 s->thread_args.in_repo_path = s->in_repo_path;
3426 } else if (ch == CTRL('l')) {
3427 struct got_object_id *start_id;
3428 err = got_repo_match_object_id(&start_id, NULL,
3429 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3430 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3434 s->start_id = start_id;
3435 s->thread_args.start_id = s->start_id;
3437 s->log_branches = !s->log_branches;
3439 if (s->thread_args.pack_fds == NULL) {
3440 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3444 err = got_repo_open(&s->thread_args.repo,
3445 got_repo_get_path(s->repo), NULL,
3446 s->thread_args.pack_fds);
3450 err = tog_load_refs(s->repo, 0);
3453 err = got_commit_graph_open(&s->thread_args.graph,
3454 s->in_repo_path, !s->log_branches);
3457 err = got_commit_graph_iter_start(s->thread_args.graph,
3458 s->start_id, s->repo, NULL, NULL);
3461 free_commits(&s->commits);
3462 s->first_displayed_entry = NULL;
3463 s->last_displayed_entry = NULL;
3464 s->selected_entry = NULL;
3466 s->thread_args.log_complete = 0;
3468 s->thread_args.commits_needed = view->lines;
3469 s->matched_entry = NULL;
3470 s->search_entry = NULL;
3474 if (view_is_parent_view(view))
3475 view_get_split(view, &begin_y, &begin_x);
3476 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3477 if (ref_view == NULL)
3478 return got_error_from_errno("view_open");
3479 err = open_ref_view(ref_view, s->repo);
3481 view_close(ref_view);
3484 if (view_is_parent_view(view) &&
3485 view->mode == TOG_VIEW_SPLIT_HRZN) {
3486 err = view_init_hsplit(view, begin_y);
3491 ref_view->focussed = 1;
3492 ref_view->mode = view->mode;
3493 ref_view->nlines = view->lines - begin_y;
3494 if (view_is_parent_view(view)) {
3495 view_transfer_size(ref_view, view);
3496 err = view_close_child(view);
3499 err = view_set_child(view, ref_view);
3502 view->focus_child = 1;
3504 *new_view = ref_view;
3514 static const struct got_error *
3515 apply_unveil(const char *repo_path, const char *worktree_path)
3517 const struct got_error *error;
3520 if (unveil("gmon.out", "rwc") != 0)
3521 return got_error_from_errno2("unveil", "gmon.out");
3523 if (repo_path && unveil(repo_path, "r") != 0)
3524 return got_error_from_errno2("unveil", repo_path);
3526 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3527 return got_error_from_errno2("unveil", worktree_path);
3529 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3530 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3532 error = got_privsep_unveil_exec_helpers();
3536 if (unveil(NULL, NULL) != 0)
3537 return got_error_from_errno("unveil");
3546 * Override default signal handlers before starting ncurses.
3547 * This should prevent ncurses from installing its own
3548 * broken cleanup() signal handler.
3550 signal(SIGWINCH, tog_sigwinch);
3551 signal(SIGPIPE, tog_sigpipe);
3552 signal(SIGCONT, tog_sigcont);
3553 signal(SIGINT, tog_sigint);
3554 signal(SIGTERM, tog_sigterm);
3558 halfdelay(1); /* Do fast refresh while initial view is loading. */
3561 intrflush(stdscr, FALSE);
3562 keypad(stdscr, TRUE);
3564 if (getenv("TOG_COLORS") != NULL) {
3566 use_default_colors();
3570 static const struct got_error *
3571 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3572 struct got_repository *repo, struct got_worktree *worktree)
3574 const struct got_error *err = NULL;
3577 *in_repo_path = strdup("/");
3578 if (*in_repo_path == NULL)
3579 return got_error_from_errno("strdup");
3584 const char *prefix = got_worktree_get_path_prefix(worktree);
3587 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3590 if (asprintf(in_repo_path, "%s%s%s", prefix,
3591 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3593 err = got_error_from_errno("asprintf");
3594 *in_repo_path = NULL;
3598 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3603 static const struct got_error *
3604 cmd_log(int argc, char *argv[])
3606 const struct got_error *error;
3607 struct got_repository *repo = NULL;
3608 struct got_worktree *worktree = NULL;
3609 struct got_object_id *start_id = NULL;
3610 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3611 char *start_commit = NULL, *label = NULL;
3612 struct got_reference *ref = NULL;
3613 const char *head_ref_name = NULL;
3614 int ch, log_branches = 0;
3615 struct tog_view *view;
3616 int *pack_fds = NULL;
3618 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3624 start_commit = optarg;
3627 repo_path = realpath(optarg, NULL);
3628 if (repo_path == NULL)
3629 return got_error_from_errno2("realpath",
3644 error = got_repo_pack_fds_open(&pack_fds);
3648 if (repo_path == NULL) {
3649 cwd = getcwd(NULL, 0);
3651 return got_error_from_errno("getcwd");
3652 error = got_worktree_open(&worktree, cwd);
3653 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3657 strdup(got_worktree_get_repo_path(worktree));
3659 repo_path = strdup(cwd);
3660 if (repo_path == NULL) {
3661 error = got_error_from_errno("strdup");
3666 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3670 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3677 error = apply_unveil(got_repo_get_path(repo),
3678 worktree ? got_worktree_get_root_path(worktree) : NULL);
3682 /* already loaded by tog_log_with_path()? */
3683 if (TAILQ_EMPTY(&tog_refs)) {
3684 error = tog_load_refs(repo, 0);
3689 if (start_commit == NULL) {
3690 error = got_repo_match_object_id(&start_id, &label,
3691 worktree ? got_worktree_get_head_ref_name(worktree) :
3692 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3695 head_ref_name = label;
3697 error = got_ref_open(&ref, repo, start_commit, 0);
3699 head_ref_name = got_ref_get_name(ref);
3700 else if (error->code != GOT_ERR_NOT_REF)
3702 error = got_repo_match_object_id(&start_id, NULL,
3703 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3708 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3710 error = got_error_from_errno("view_open");
3713 error = open_log_view(view, start_id, repo, head_ref_name,
3714 in_repo_path, log_branches);
3718 /* Release work tree lock. */
3719 got_worktree_close(worktree);
3722 error = view_loop(view);
3732 const struct got_error *close_err = got_repo_close(repo);
3737 got_worktree_close(worktree);
3739 const struct got_error *pack_err =
3740 got_repo_pack_fds_close(pack_fds);
3752 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3753 "[-w] object1 object2\n", getprogname());
3758 match_line(const char *line, regex_t *regex, size_t nmatch,
3759 regmatch_t *regmatch)
3761 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3764 static struct tog_color *
3765 match_color(struct tog_colors *colors, const char *line)
3767 struct tog_color *tc = NULL;
3769 STAILQ_FOREACH(tc, colors, entry) {
3770 if (match_line(line, &tc->regex, 0, NULL))
3777 static const struct got_error *
3778 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3779 WINDOW *window, int skipcol, regmatch_t *regmatch)
3781 const struct got_error *err = NULL;
3783 wchar_t *wline = NULL;
3784 int rme, rms, n, width, scrollx;
3785 int width0 = 0, width1 = 0, width2 = 0;
3786 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3790 rms = regmatch->rm_so;
3791 rme = regmatch->rm_eo;
3793 err = expand_tab(&exstr, line);
3797 /* Split the line into 3 segments, according to match offsets. */
3798 seg0 = strndup(exstr, rms);
3800 err = got_error_from_errno("strndup");
3803 seg1 = strndup(exstr + rms, rme - rms);
3805 err = got_error_from_errno("strndup");
3808 seg2 = strdup(exstr + rme);
3810 err = got_error_from_errno("strndup");
3814 /* draw up to matched token if we haven't scrolled past it */
3815 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3819 n = MAX(width0 - skipcol, 0);
3822 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3823 wlimit, col_tab_align, 1);
3826 waddwstr(window, &wline[scrollx]);
3836 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3840 wlen = wcslen(wline);
3842 width = wcwidth(wline[i]);
3844 /* should not happen, tabs are expanded */
3845 err = got_error(GOT_ERR_RANGE);
3848 if (width0 + w + width > skipcol)
3853 /* draw (visible part of) matched token (if scrolled into it) */
3854 if (width1 - w > 0) {
3855 wattron(window, A_STANDOUT);
3856 waddwstr(window, &wline[i]);
3857 wattroff(window, A_STANDOUT);
3858 wlimit -= (width1 - w);
3859 *wtotal += (width1 - w);
3863 if (wlimit > 0) { /* draw rest of line */
3865 if (skipcol > width0 + width1) {
3866 err = format_line(&wline, &width2, &scrollx, seg2,
3867 skipcol - (width0 + width1), wlimit,
3871 waddwstr(window, &wline[scrollx]);
3873 err = format_line(&wline, &width2, NULL, seg2, 0,
3874 wlimit, col_tab_align, 1);
3877 waddwstr(window, wline);
3890 static const struct got_error *
3891 draw_file(struct tog_view *view, const char *header)
3893 struct tog_diff_view_state *s = &view->state.diff;
3894 regmatch_t *regmatch = &view->regmatch;
3895 const struct got_error *err;
3898 size_t linesize = 0;
3900 struct tog_color *tc;
3903 int max_lines = view->nlines;
3904 int nlines = s->nlines;
3907 line_offset = s->line_offsets[s->first_displayed_line - 1];
3908 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3909 return got_error_from_errno("fseek");
3911 werase(view->window);
3914 if (asprintf(&line, "[%d/%d] %s",
3915 s->first_displayed_line - 1 + s->selected_line, nlines,
3917 return got_error_from_errno("asprintf");
3918 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3924 if (view_needs_focus_indication(view))
3925 wstandout(view->window);
3926 waddwstr(view->window, wline);
3929 if (view_needs_focus_indication(view))
3930 wstandend(view->window);
3931 if (width <= view->ncols - 1)
3932 waddch(view->window, '\n');
3942 while (max_lines > 0 && nprinted < max_lines) {
3943 linelen = getline(&line, &linesize, s->f);
3944 if (linelen == -1) {
3950 return got_ferror(s->f, GOT_ERR_IO);
3953 /* Set view->maxx based on full line length. */
3954 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3960 view->maxx = MAX(view->maxx, width);
3964 tc = match_color(&s->colors, line);
3966 wattr_on(view->window,
3967 COLOR_PAIR(tc->colorpair), NULL);
3968 if (s->first_displayed_line + nprinted == s->matched_line &&
3969 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3970 err = add_matched_line(&width, line, view->ncols, 0,
3971 view->window, view->x, regmatch);
3978 err = format_line(&wline, &width, &skip, line,
3979 view->x, view->ncols, 0, view->x ? 1 : 0);
3984 waddwstr(view->window, &wline[skip]);
3989 wattr_off(view->window,
3990 COLOR_PAIR(tc->colorpair), NULL);
3991 if (width <= view->ncols - 1)
3992 waddch(view->window, '\n');
3997 s->last_displayed_line = s->first_displayed_line +
4000 s->last_displayed_line = s->first_displayed_line;
4005 while (nprinted < view->nlines) {
4006 waddch(view->window, '\n');
4010 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4016 wstandout(view->window);
4017 waddwstr(view->window, wline);
4020 wstandend(view->window);
4027 get_datestr(time_t *time, char *datebuf)
4029 struct tm mytm, *tm;
4032 tm = gmtime_r(time, &mytm);
4035 s = asctime_r(tm, datebuf);
4038 p = strchr(s, '\n');
4044 static const struct got_error *
4045 get_changed_paths(struct got_pathlist_head *paths,
4046 struct got_commit_object *commit, struct got_repository *repo)
4048 const struct got_error *err = NULL;
4049 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4050 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4051 struct got_object_qid *qid;
4053 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4055 struct got_commit_object *pcommit;
4056 err = got_object_open_as_commit(&pcommit, repo,
4061 tree_id1 = got_object_id_dup(
4062 got_object_commit_get_tree_id(pcommit));
4063 if (tree_id1 == NULL) {
4064 got_object_commit_close(pcommit);
4065 return got_error_from_errno("got_object_id_dup");
4067 got_object_commit_close(pcommit);
4072 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4077 tree_id2 = got_object_commit_get_tree_id(commit);
4078 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4082 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4083 got_diff_tree_collect_changed_paths, paths, 0);
4086 got_object_tree_close(tree1);
4088 got_object_tree_close(tree2);
4093 static const struct got_error *
4094 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4098 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4100 return got_error_from_errno("reallocarray");
4102 (*line_offsets)[*nlines] = off;
4107 static const struct got_error *
4108 write_commit_info(off_t **line_offsets, size_t *nlines,
4109 struct got_object_id *commit_id, struct got_reflist_head *refs,
4110 struct got_repository *repo, FILE *outfile)
4112 const struct got_error *err = NULL;
4113 char datebuf[26], *datestr;
4114 struct got_commit_object *commit;
4115 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4116 time_t committer_time;
4117 const char *author, *committer;
4118 char *refs_str = NULL;
4119 struct got_pathlist_head changed_paths;
4120 struct got_pathlist_entry *pe;
4124 TAILQ_INIT(&changed_paths);
4127 err = build_refs_str(&refs_str, refs, commit_id, repo);
4132 err = got_object_open_as_commit(&commit, repo, commit_id);
4136 err = got_object_id_str(&id_str, commit_id);
4138 err = got_error_from_errno("got_object_id_str");
4142 err = add_line_offset(line_offsets, nlines, 0);
4146 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4147 refs_str ? refs_str : "", refs_str ? ")" : "");
4149 err = got_error_from_errno("fprintf");
4153 err = add_line_offset(line_offsets, nlines, outoff);
4157 n = fprintf(outfile, "from: %s\n",
4158 got_object_commit_get_author(commit));
4160 err = got_error_from_errno("fprintf");
4164 err = add_line_offset(line_offsets, nlines, outoff);
4168 committer_time = got_object_commit_get_committer_time(commit);
4169 datestr = get_datestr(&committer_time, datebuf);
4171 n = fprintf(outfile, "date: %s UTC\n", datestr);
4173 err = got_error_from_errno("fprintf");
4177 err = add_line_offset(line_offsets, nlines, outoff);
4181 author = got_object_commit_get_author(commit);
4182 committer = got_object_commit_get_committer(commit);
4183 if (strcmp(author, committer) != 0) {
4184 n = fprintf(outfile, "via: %s\n", committer);
4186 err = got_error_from_errno("fprintf");
4190 err = add_line_offset(line_offsets, nlines, outoff);
4194 if (got_object_commit_get_nparents(commit) > 1) {
4195 const struct got_object_id_queue *parent_ids;
4196 struct got_object_qid *qid;
4198 parent_ids = got_object_commit_get_parent_ids(commit);
4199 STAILQ_FOREACH(qid, parent_ids, entry) {
4200 err = got_object_id_str(&id_str, &qid->id);
4203 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4205 err = got_error_from_errno("fprintf");
4209 err = add_line_offset(line_offsets, nlines, outoff);
4217 err = got_object_commit_get_logmsg(&logmsg, commit);
4221 while ((line = strsep(&s, "\n")) != NULL) {
4222 n = fprintf(outfile, "%s\n", line);
4224 err = got_error_from_errno("fprintf");
4228 err = add_line_offset(line_offsets, nlines, outoff);
4233 err = get_changed_paths(&changed_paths, commit, repo);
4236 TAILQ_FOREACH(pe, &changed_paths, entry) {
4237 struct got_diff_changed_path *cp = pe->data;
4238 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4240 err = got_error_from_errno("fprintf");
4244 err = add_line_offset(line_offsets, nlines, outoff);
4247 free((char *)pe->path);
4251 fputc('\n', outfile);
4253 err = add_line_offset(line_offsets, nlines, outoff);
4255 got_pathlist_free(&changed_paths);
4259 got_object_commit_close(commit);
4261 free(*line_offsets);
4262 *line_offsets = NULL;
4268 static const struct got_error *
4269 create_diff(struct tog_diff_view_state *s)
4271 const struct got_error *err = NULL;
4275 free(s->line_offsets);
4276 s->line_offsets = malloc(sizeof(off_t));
4277 if (s->line_offsets == NULL)
4278 return got_error_from_errno("malloc");
4283 err = got_error_from_errno("got_opentemp");
4286 if (s->f && fclose(s->f) == EOF) {
4287 err = got_error_from_errno("fclose");
4293 err = got_object_get_type(&obj_type, s->repo, s->id1);
4295 err = got_object_get_type(&obj_type, s->repo, s->id2);
4300 case GOT_OBJ_TYPE_BLOB:
4301 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4302 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4303 s->label1, s->label2, tog_diff_algo, s->diff_context,
4304 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4306 case GOT_OBJ_TYPE_TREE:
4307 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4308 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4309 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4310 s->force_text_diff, s->repo, s->f);
4312 case GOT_OBJ_TYPE_COMMIT: {
4313 const struct got_object_id_queue *parent_ids;
4314 struct got_object_qid *pid;
4315 struct got_commit_object *commit2;
4316 struct got_reflist_head *refs;
4318 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4321 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4322 /* Show commit info if we're diffing to a parent/root commit. */
4323 if (s->id1 == NULL) {
4324 err = write_commit_info(&s->line_offsets, &s->nlines,
4325 s->id2, refs, s->repo, s->f);
4329 parent_ids = got_object_commit_get_parent_ids(commit2);
4330 STAILQ_FOREACH(pid, parent_ids, entry) {
4331 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4332 err = write_commit_info(
4333 &s->line_offsets, &s->nlines,
4334 s->id2, refs, s->repo, s->f);
4341 got_object_commit_close(commit2);
4343 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4344 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4345 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4346 s->force_text_diff, s->repo, s->f);
4350 err = got_error(GOT_ERR_OBJ_TYPE);
4356 if (s->f && fflush(s->f) != 0 && err == NULL)
4357 err = got_error_from_errno("fflush");
4362 diff_view_indicate_progress(struct tog_view *view)
4364 mvwaddstr(view->window, 0, 0, "diffing...");
4369 static const struct got_error *
4370 search_start_diff_view(struct tog_view *view)
4372 struct tog_diff_view_state *s = &view->state.diff;
4374 s->matched_line = 0;
4378 static const struct got_error *
4379 search_next_diff_view(struct tog_view *view)
4381 struct tog_diff_view_state *s = &view->state.diff;
4382 const struct got_error *err = NULL;
4385 size_t linesize = 0;
4388 if (!view->searching) {
4389 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4393 if (s->matched_line) {
4394 if (view->searching == TOG_SEARCH_FORWARD)
4395 lineno = s->matched_line + 1;
4397 lineno = s->matched_line - 1;
4399 lineno = s->first_displayed_line;
4404 if (lineno <= 0 || lineno > s->nlines) {
4405 if (s->matched_line == 0) {
4406 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4410 if (view->searching == TOG_SEARCH_FORWARD)
4416 offset = s->line_offsets[lineno - 1];
4417 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4419 return got_error_from_errno("fseeko");
4421 linelen = getline(&line, &linesize, s->f);
4422 if (linelen != -1) {
4424 err = expand_tab(&exstr, line);
4427 if (match_line(exstr, &view->regex, 1,
4429 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4430 s->matched_line = lineno;
4436 if (view->searching == TOG_SEARCH_FORWARD)
4443 if (s->matched_line) {
4444 s->first_displayed_line = s->matched_line;
4445 s->selected_line = 1;
4451 static const struct got_error *
4452 close_diff_view(struct tog_view *view)
4454 const struct got_error *err = NULL;
4455 struct tog_diff_view_state *s = &view->state.diff;
4461 if (s->f && fclose(s->f) == EOF)
4462 err = got_error_from_errno("fclose");
4464 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4465 err = got_error_from_errno("fclose");
4467 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4468 err = got_error_from_errno("fclose");
4470 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4471 err = got_error_from_errno("close");
4473 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4474 err = got_error_from_errno("close");
4476 free_colors(&s->colors);
4477 free(s->line_offsets);
4478 s->line_offsets = NULL;
4483 static const struct got_error *
4484 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4485 struct got_object_id *id2, const char *label1, const char *label2,
4486 int diff_context, int ignore_whitespace, int force_text_diff,
4487 struct tog_view *parent_view, struct got_repository *repo)
4489 const struct got_error *err;
4490 struct tog_diff_view_state *s = &view->state.diff;
4492 memset(s, 0, sizeof(*s));
4496 if (id1 != NULL && id2 != NULL) {
4498 err = got_object_get_type(&type1, repo, id1);
4501 err = got_object_get_type(&type2, repo, id2);
4506 return got_error(GOT_ERR_OBJ_TYPE);
4508 s->first_displayed_line = 1;
4509 s->last_displayed_line = view->nlines;
4510 s->selected_line = 1;
4518 s->id1 = got_object_id_dup(id1);
4520 return got_error_from_errno("got_object_id_dup");
4524 s->id2 = got_object_id_dup(id2);
4525 if (s->id2 == NULL) {
4526 err = got_error_from_errno("got_object_id_dup");
4530 s->f1 = got_opentemp();
4531 if (s->f1 == NULL) {
4532 err = got_error_from_errno("got_opentemp");
4536 s->f2 = got_opentemp();
4537 if (s->f2 == NULL) {
4538 err = got_error_from_errno("got_opentemp");
4542 s->fd1 = got_opentempfd();
4544 err = got_error_from_errno("got_opentempfd");
4548 s->fd2 = got_opentempfd();
4550 err = got_error_from_errno("got_opentempfd");
4554 s->first_displayed_line = 1;
4555 s->last_displayed_line = view->nlines;
4556 s->diff_context = diff_context;
4557 s->ignore_whitespace = ignore_whitespace;
4558 s->force_text_diff = force_text_diff;
4559 s->parent_view = parent_view;
4562 STAILQ_INIT(&s->colors);
4563 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4564 err = add_color(&s->colors,
4565 "^-", TOG_COLOR_DIFF_MINUS,
4566 get_color_value("TOG_COLOR_DIFF_MINUS"));
4569 err = add_color(&s->colors, "^\\+",
4570 TOG_COLOR_DIFF_PLUS,
4571 get_color_value("TOG_COLOR_DIFF_PLUS"));
4574 err = add_color(&s->colors,
4575 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4576 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4580 err = add_color(&s->colors,
4581 "^(commit [0-9a-f]|parent [0-9]|"
4582 "(blob|file|tree|commit) [-+] |"
4583 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4584 get_color_value("TOG_COLOR_DIFF_META"));
4588 err = add_color(&s->colors,
4589 "^(from|via): ", TOG_COLOR_AUTHOR,
4590 get_color_value("TOG_COLOR_AUTHOR"));
4594 err = add_color(&s->colors,
4595 "^date: ", TOG_COLOR_DATE,
4596 get_color_value("TOG_COLOR_DATE"));
4601 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4602 view_is_splitscreen(view))
4603 show_log_view(parent_view); /* draw border */
4604 diff_view_indicate_progress(view);
4606 err = create_diff(s);
4608 view->show = show_diff_view;
4609 view->input = input_diff_view;
4610 view->reset = reset_diff_view;
4611 view->close = close_diff_view;
4612 view->search_start = search_start_diff_view;
4613 view->search_next = search_next_diff_view;
4616 close_diff_view(view);
4620 static const struct got_error *
4621 show_diff_view(struct tog_view *view)
4623 const struct got_error *err;
4624 struct tog_diff_view_state *s = &view->state.diff;
4625 char *id_str1 = NULL, *id_str2, *header;
4626 const char *label1, *label2;
4629 err = got_object_id_str(&id_str1, s->id1);
4632 label1 = s->label1 ? : id_str1;
4634 label1 = "/dev/null";
4636 err = got_object_id_str(&id_str2, s->id2);
4639 label2 = s->label2 ? : id_str2;
4641 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4642 err = got_error_from_errno("asprintf");
4650 err = draw_file(view, header);
4655 static const struct got_error *
4656 set_selected_commit(struct tog_diff_view_state *s,
4657 struct commit_queue_entry *entry)
4659 const struct got_error *err;
4660 const struct got_object_id_queue *parent_ids;
4661 struct got_commit_object *selected_commit;
4662 struct got_object_qid *pid;
4665 s->id2 = got_object_id_dup(entry->id);
4667 return got_error_from_errno("got_object_id_dup");
4669 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4672 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4674 pid = STAILQ_FIRST(parent_ids);
4675 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4676 got_object_commit_close(selected_commit);
4680 static const struct got_error *
4681 reset_diff_view(struct tog_view *view)
4683 struct tog_diff_view_state *s = &view->state.diff;
4686 wclear(view->window);
4687 s->first_displayed_line = 1;
4688 s->last_displayed_line = view->nlines;
4689 s->matched_line = 0;
4690 diff_view_indicate_progress(view);
4691 return create_diff(s);
4694 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4696 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4699 static const struct got_error *
4700 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4702 const struct got_error *err = NULL;
4703 struct tog_diff_view_state *s = &view->state.diff;
4704 struct tog_log_view_state *ls;
4705 struct commit_queue_entry *old_selected_entry;
4707 size_t linesize = 0;
4709 int i, nscroll = view->nlines - 1, up = 0;
4716 view->x = MAX(view->maxx - view->ncols / 3, 0);
4721 if (view->x + view->ncols / 3 < view->maxx)
4722 view->x += 2; /* move two columns right */
4728 view->x -= MIN(view->x, 2); /* move two columns back */
4735 s->force_text_diff = !s->force_text_diff;
4737 s->ignore_whitespace = !s->ignore_whitespace;
4738 err = reset_diff_view(view);
4742 s->first_displayed_line = 1;
4751 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4757 if (s->first_displayed_line > 1)
4758 s->first_displayed_line--;
4769 if (s->first_displayed_line == 1) {
4774 while (i++ < nscroll && s->first_displayed_line > 1)
4775 s->first_displayed_line--;
4781 s->first_displayed_line++;
4798 while (!s->eof && i++ < nscroll) {
4799 linelen = getline(&line, &linesize, s->f);
4800 s->first_displayed_line++;
4801 if (linelen == -1) {
4805 err = got_ferror(s->f, GOT_ERR_IO);
4812 if (s->diff_context > 0) {
4814 s->matched_line = 0;
4815 diff_view_indicate_progress(view);
4816 err = create_diff(s);
4817 if (s->first_displayed_line + view->nlines - 1 >
4819 s->first_displayed_line = 1;
4820 s->last_displayed_line = view->nlines;
4826 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4828 s->matched_line = 0;
4829 diff_view_indicate_progress(view);
4830 err = create_diff(s);
4842 if (s->parent_view == NULL) {
4846 s->parent_view->count = view->count;
4848 if (s->parent_view->type == TOG_VIEW_LOG) {
4849 ls = &s->parent_view->state.log;
4850 old_selected_entry = ls->selected_entry;
4852 err = input_log_view(NULL, s->parent_view,
4853 up ? KEY_UP : KEY_DOWN);
4856 view->count = s->parent_view->count;
4858 if (old_selected_entry == ls->selected_entry)
4861 err = set_selected_commit(s, ls->selected_entry);
4864 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4865 struct tog_blame_view_state *bs;
4866 struct got_object_id *id, *prev_id;
4868 bs = &s->parent_view->state.blame;
4869 prev_id = get_annotation_for_line(bs->blame.lines,
4870 bs->blame.nlines, bs->last_diffed_line);
4872 err = input_blame_view(&view, s->parent_view,
4873 up ? KEY_UP : KEY_DOWN);
4876 view->count = s->parent_view->count;
4878 if (prev_id == NULL)
4880 id = get_selected_commit_id(bs->blame.lines,
4881 bs->blame.nlines, bs->first_displayed_line,
4886 if (!got_object_id_cmp(prev_id, id))
4889 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4893 s->first_displayed_line = 1;
4894 s->last_displayed_line = view->nlines;
4895 s->matched_line = 0;
4898 diff_view_indicate_progress(view);
4899 err = create_diff(s);
4909 static const struct got_error *
4910 cmd_diff(int argc, char *argv[])
4912 const struct got_error *error = NULL;
4913 struct got_repository *repo = NULL;
4914 struct got_worktree *worktree = NULL;
4915 struct got_object_id *id1 = NULL, *id2 = NULL;
4916 char *repo_path = NULL, *cwd = NULL;
4917 char *id_str1 = NULL, *id_str2 = NULL;
4918 char *label1 = NULL, *label2 = NULL;
4919 int diff_context = 3, ignore_whitespace = 0;
4920 int ch, force_text_diff = 0;
4922 struct tog_view *view;
4923 int *pack_fds = NULL;
4925 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4928 force_text_diff = 1;
4931 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4934 errx(1, "number of context lines is %s: %s",
4938 repo_path = realpath(optarg, NULL);
4939 if (repo_path == NULL)
4940 return got_error_from_errno2("realpath",
4942 got_path_strip_trailing_slashes(repo_path);
4945 ignore_whitespace = 1;
4957 usage_diff(); /* TODO show local worktree changes */
4958 } else if (argc == 2) {
4964 error = got_repo_pack_fds_open(&pack_fds);
4968 if (repo_path == NULL) {
4969 cwd = getcwd(NULL, 0);
4971 return got_error_from_errno("getcwd");
4972 error = got_worktree_open(&worktree, cwd);
4973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4977 strdup(got_worktree_get_repo_path(worktree));
4979 repo_path = strdup(cwd);
4980 if (repo_path == NULL) {
4981 error = got_error_from_errno("strdup");
4986 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4992 error = apply_unveil(got_repo_get_path(repo), NULL);
4996 error = tog_load_refs(repo, 0);
5000 error = got_repo_match_object_id(&id1, &label1, id_str1,
5001 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5005 error = got_repo_match_object_id(&id2, &label2, id_str2,
5006 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5010 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5012 error = got_error_from_errno("view_open");
5015 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5016 ignore_whitespace, force_text_diff, NULL, repo);
5019 error = view_loop(view);
5026 const struct got_error *close_err = got_repo_close(repo);
5031 got_worktree_close(worktree);
5033 const struct got_error *pack_err =
5034 got_repo_pack_fds_close(pack_fds);
5047 "usage: %s blame [-c commit] [-r repository-path] path\n",
5052 struct tog_blame_line {
5054 struct got_object_id *id;
5057 static const struct got_error *
5058 draw_blame(struct tog_view *view)
5060 struct tog_blame_view_state *s = &view->state.blame;
5061 struct tog_blame *blame = &s->blame;
5062 regmatch_t *regmatch = &view->regmatch;
5063 const struct got_error *err;
5064 int lineno = 0, nprinted = 0;
5066 size_t linesize = 0;
5070 struct tog_blame_line *blame_line;
5071 struct got_object_id *prev_id = NULL;
5073 struct tog_color *tc;
5075 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5080 werase(view->window);
5082 if (asprintf(&line, "commit %s", id_str) == -1) {
5083 err = got_error_from_errno("asprintf");
5088 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5093 if (view_needs_focus_indication(view))
5094 wstandout(view->window);
5095 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5097 wattr_on(view->window,
5098 COLOR_PAIR(tc->colorpair), NULL);
5099 waddwstr(view->window, wline);
5101 wattr_off(view->window,
5102 COLOR_PAIR(tc->colorpair), NULL);
5103 if (view_needs_focus_indication(view))
5104 wstandend(view->window);
5107 if (width < view->ncols - 1)
5108 waddch(view->window, '\n');
5110 if (asprintf(&line, "[%d/%d] %s%s",
5111 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5112 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5114 return got_error_from_errno("asprintf");
5117 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5122 waddwstr(view->window, wline);
5125 if (width < view->ncols - 1)
5126 waddch(view->window, '\n');
5130 while (nprinted < view->nlines - 2) {
5131 linelen = getline(&line, &linesize, blame->f);
5132 if (linelen == -1) {
5133 if (feof(blame->f)) {
5138 return got_ferror(blame->f, GOT_ERR_IO);
5140 if (++lineno < s->first_displayed_line)
5143 /* Set view->maxx based on full line length. */
5144 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5151 view->maxx = MAX(view->maxx, width);
5153 if (nprinted == s->selected_line - 1)
5154 wstandout(view->window);
5156 if (blame->nlines > 0) {
5157 blame_line = &blame->lines[lineno - 1];
5158 if (blame_line->annotated && prev_id &&
5159 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5160 !(nprinted == s->selected_line - 1)) {
5161 waddstr(view->window, " ");
5162 } else if (blame_line->annotated) {
5164 err = got_object_id_str(&id_str,
5170 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5172 wattr_on(view->window,
5173 COLOR_PAIR(tc->colorpair), NULL);
5174 wprintw(view->window, "%.8s", id_str);
5176 wattr_off(view->window,
5177 COLOR_PAIR(tc->colorpair), NULL);
5179 prev_id = blame_line->id;
5181 waddstr(view->window, "........");
5185 waddstr(view->window, "........");
5189 if (nprinted == s->selected_line - 1)
5190 wstandend(view->window);
5191 waddstr(view->window, " ");
5193 if (view->ncols <= 9) {
5195 } else if (s->first_displayed_line + nprinted ==
5197 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5198 err = add_matched_line(&width, line, view->ncols - 9, 9,
5199 view->window, view->x, regmatch);
5207 err = format_line(&wline, &width, &skip, line,
5208 view->x, view->ncols - 9, 9, 1);
5213 waddwstr(view->window, &wline[skip]);
5219 if (width <= view->ncols - 1)
5220 waddch(view->window, '\n');
5221 if (++nprinted == 1)
5222 s->first_displayed_line = lineno;
5225 s->last_displayed_line = lineno;
5232 static const struct got_error *
5233 blame_cb(void *arg, int nlines, int lineno,
5234 struct got_commit_object *commit, struct got_object_id *id)
5236 const struct got_error *err = NULL;
5237 struct tog_blame_cb_args *a = arg;
5238 struct tog_blame_line *line;
5241 if (nlines != a->nlines ||
5242 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5243 return got_error(GOT_ERR_RANGE);
5245 errcode = pthread_mutex_lock(&tog_mutex);
5247 return got_error_set_errno(errcode, "pthread_mutex_lock");
5249 if (*a->quit) { /* user has quit the blame view */
5250 err = got_error(GOT_ERR_ITER_COMPLETED);
5255 goto done; /* no change in this commit */
5257 line = &a->lines[lineno - 1];
5258 if (line->annotated)
5261 line->id = got_object_id_dup(id);
5262 if (line->id == NULL) {
5263 err = got_error_from_errno("got_object_id_dup");
5266 line->annotated = 1;
5268 errcode = pthread_mutex_unlock(&tog_mutex);
5270 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5275 blame_thread(void *arg)
5277 const struct got_error *err, *close_err;
5278 struct tog_blame_thread_args *ta = arg;
5279 struct tog_blame_cb_args *a = ta->cb_args;
5280 int errcode, fd1 = -1, fd2 = -1;
5281 FILE *f1 = NULL, *f2 = NULL;
5283 fd1 = got_opentempfd();
5285 return (void *)got_error_from_errno("got_opentempfd");
5287 fd2 = got_opentempfd();
5289 err = got_error_from_errno("got_opentempfd");
5293 f1 = got_opentemp();
5295 err = (void *)got_error_from_errno("got_opentemp");
5298 f2 = got_opentemp();
5300 err = (void *)got_error_from_errno("got_opentemp");
5304 err = block_signals_used_by_main_thread();
5308 err = got_blame(ta->path, a->commit_id, ta->repo,
5309 tog_diff_algo, blame_cb, ta->cb_args,
5310 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5311 if (err && err->code == GOT_ERR_CANCELLED)
5314 errcode = pthread_mutex_lock(&tog_mutex);
5316 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5320 close_err = got_repo_close(ta->repo);
5326 errcode = pthread_mutex_unlock(&tog_mutex);
5327 if (errcode && err == NULL)
5328 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5331 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5332 err = got_error_from_errno("close");
5333 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5334 err = got_error_from_errno("close");
5335 if (f1 && fclose(f1) == EOF && err == NULL)
5336 err = got_error_from_errno("fclose");
5337 if (f2 && fclose(f2) == EOF && err == NULL)
5338 err = got_error_from_errno("fclose");
5343 static struct got_object_id *
5344 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5345 int first_displayed_line, int selected_line)
5347 struct tog_blame_line *line;
5352 line = &lines[first_displayed_line - 1 + selected_line - 1];
5353 if (!line->annotated)
5359 static struct got_object_id *
5360 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5363 struct tog_blame_line *line;
5365 if (nlines <= 0 || lineno >= nlines)
5368 line = &lines[lineno - 1];
5369 if (!line->annotated)
5375 static const struct got_error *
5376 stop_blame(struct tog_blame *blame)
5378 const struct got_error *err = NULL;
5381 if (blame->thread) {
5383 errcode = pthread_mutex_unlock(&tog_mutex);
5385 return got_error_set_errno(errcode,
5386 "pthread_mutex_unlock");
5387 errcode = pthread_join(blame->thread, (void **)&err);
5389 return got_error_set_errno(errcode, "pthread_join");
5390 errcode = pthread_mutex_lock(&tog_mutex);
5392 return got_error_set_errno(errcode,
5393 "pthread_mutex_lock");
5394 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5396 blame->thread = 0; //NULL;
5398 if (blame->thread_args.repo) {
5399 const struct got_error *close_err;
5400 close_err = got_repo_close(blame->thread_args.repo);
5403 blame->thread_args.repo = NULL;
5406 if (fclose(blame->f) == EOF && err == NULL)
5407 err = got_error_from_errno("fclose");
5411 for (i = 0; i < blame->nlines; i++)
5412 free(blame->lines[i].id);
5414 blame->lines = NULL;
5416 free(blame->cb_args.commit_id);
5417 blame->cb_args.commit_id = NULL;
5418 if (blame->pack_fds) {
5419 const struct got_error *pack_err =
5420 got_repo_pack_fds_close(blame->pack_fds);
5423 blame->pack_fds = NULL;
5428 static const struct got_error *
5429 cancel_blame_view(void *arg)
5431 const struct got_error *err = NULL;
5435 errcode = pthread_mutex_lock(&tog_mutex);
5437 return got_error_set_errno(errcode,
5438 "pthread_mutex_unlock");
5441 err = got_error(GOT_ERR_CANCELLED);
5443 errcode = pthread_mutex_unlock(&tog_mutex);
5445 return got_error_set_errno(errcode,
5446 "pthread_mutex_lock");
5451 static const struct got_error *
5452 run_blame(struct tog_view *view)
5454 struct tog_blame_view_state *s = &view->state.blame;
5455 struct tog_blame *blame = &s->blame;
5456 const struct got_error *err = NULL;
5457 struct got_commit_object *commit = NULL;
5458 struct got_blob_object *blob = NULL;
5459 struct got_repository *thread_repo = NULL;
5460 struct got_object_id *obj_id = NULL;
5461 int obj_type, fd = -1;
5462 int *pack_fds = NULL;
5464 err = got_object_open_as_commit(&commit, s->repo,
5465 &s->blamed_commit->id);
5469 fd = got_opentempfd();
5471 err = got_error_from_errno("got_opentempfd");
5475 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5479 err = got_object_get_type(&obj_type, s->repo, obj_id);
5483 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5484 err = got_error(GOT_ERR_OBJ_TYPE);
5488 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5491 blame->f = got_opentemp();
5492 if (blame->f == NULL) {
5493 err = got_error_from_errno("got_opentemp");
5496 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5497 &blame->line_offsets, blame->f, blob);
5500 if (blame->nlines == 0) {
5501 s->blame_complete = 1;
5505 /* Don't include \n at EOF in the blame line count. */
5506 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5509 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5510 if (blame->lines == NULL) {
5511 err = got_error_from_errno("calloc");
5515 err = got_repo_pack_fds_open(&pack_fds);
5518 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5523 blame->pack_fds = pack_fds;
5524 blame->cb_args.view = view;
5525 blame->cb_args.lines = blame->lines;
5526 blame->cb_args.nlines = blame->nlines;
5527 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5528 if (blame->cb_args.commit_id == NULL) {
5529 err = got_error_from_errno("got_object_id_dup");
5532 blame->cb_args.quit = &s->done;
5534 blame->thread_args.path = s->path;
5535 blame->thread_args.repo = thread_repo;
5536 blame->thread_args.cb_args = &blame->cb_args;
5537 blame->thread_args.complete = &s->blame_complete;
5538 blame->thread_args.cancel_cb = cancel_blame_view;
5539 blame->thread_args.cancel_arg = &s->done;
5540 s->blame_complete = 0;
5542 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5543 s->first_displayed_line = 1;
5544 s->last_displayed_line = view->nlines;
5545 s->selected_line = 1;
5547 s->matched_line = 0;
5551 got_object_commit_close(commit);
5552 if (fd != -1 && close(fd) == -1 && err == NULL)
5553 err = got_error_from_errno("close");
5555 got_object_blob_close(blob);
5562 static const struct got_error *
5563 open_blame_view(struct tog_view *view, char *path,
5564 struct got_object_id *commit_id, struct got_repository *repo)
5566 const struct got_error *err = NULL;
5567 struct tog_blame_view_state *s = &view->state.blame;
5569 STAILQ_INIT(&s->blamed_commits);
5571 s->path = strdup(path);
5572 if (s->path == NULL)
5573 return got_error_from_errno("strdup");
5575 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5581 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5582 s->first_displayed_line = 1;
5583 s->last_displayed_line = view->nlines;
5584 s->selected_line = 1;
5585 s->blame_complete = 0;
5587 s->commit_id = commit_id;
5588 memset(&s->blame, 0, sizeof(s->blame));
5590 STAILQ_INIT(&s->colors);
5591 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5592 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5593 get_color_value("TOG_COLOR_COMMIT"));
5598 view->show = show_blame_view;
5599 view->input = input_blame_view;
5600 view->reset = reset_blame_view;
5601 view->close = close_blame_view;
5602 view->search_start = search_start_blame_view;
5603 view->search_next = search_next_blame_view;
5605 return run_blame(view);
5608 static const struct got_error *
5609 close_blame_view(struct tog_view *view)
5611 const struct got_error *err = NULL;
5612 struct tog_blame_view_state *s = &view->state.blame;
5614 if (s->blame.thread)
5615 err = stop_blame(&s->blame);
5617 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5618 struct got_object_qid *blamed_commit;
5619 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5620 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5621 got_object_qid_free(blamed_commit);
5625 free_colors(&s->colors);
5629 static const struct got_error *
5630 search_start_blame_view(struct tog_view *view)
5632 struct tog_blame_view_state *s = &view->state.blame;
5634 s->matched_line = 0;
5638 static const struct got_error *
5639 search_next_blame_view(struct tog_view *view)
5641 struct tog_blame_view_state *s = &view->state.blame;
5642 const struct got_error *err = NULL;
5645 size_t linesize = 0;
5648 if (!view->searching) {
5649 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5653 if (s->matched_line) {
5654 if (view->searching == TOG_SEARCH_FORWARD)
5655 lineno = s->matched_line + 1;
5657 lineno = s->matched_line - 1;
5659 lineno = s->first_displayed_line - 1 + s->selected_line;
5664 if (lineno <= 0 || lineno > s->blame.nlines) {
5665 if (s->matched_line == 0) {
5666 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5670 if (view->searching == TOG_SEARCH_FORWARD)
5673 lineno = s->blame.nlines;
5676 offset = s->blame.line_offsets[lineno - 1];
5677 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5679 return got_error_from_errno("fseeko");
5681 linelen = getline(&line, &linesize, s->blame.f);
5682 if (linelen != -1) {
5684 err = expand_tab(&exstr, line);
5687 if (match_line(exstr, &view->regex, 1,
5689 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5690 s->matched_line = lineno;
5696 if (view->searching == TOG_SEARCH_FORWARD)
5703 if (s->matched_line) {
5704 s->first_displayed_line = s->matched_line;
5705 s->selected_line = 1;
5711 static const struct got_error *
5712 show_blame_view(struct tog_view *view)
5714 const struct got_error *err = NULL;
5715 struct tog_blame_view_state *s = &view->state.blame;
5718 if (s->blame.thread == 0 && !s->blame_complete) {
5719 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5720 &s->blame.thread_args);
5722 return got_error_set_errno(errcode, "pthread_create");
5724 halfdelay(1); /* fast refresh while annotating */
5727 if (s->blame_complete)
5728 halfdelay(10); /* disable fast refresh */
5730 err = draw_blame(view);
5736 static const struct got_error *
5737 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5738 struct got_repository *repo, struct got_object_id *id)
5740 struct tog_view *log_view;
5741 const struct got_error *err = NULL;
5745 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5746 if (log_view == NULL)
5747 return got_error_from_errno("view_open");
5749 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5751 view_close(log_view);
5753 *new_view = log_view;
5758 static const struct got_error *
5759 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5761 const struct got_error *err = NULL, *thread_err = NULL;
5762 struct tog_view *diff_view, *log_view;
5763 struct tog_blame_view_state *s = &view->state.blame;
5764 int eos, nscroll, begin_y = 0, begin_x = 0;
5766 eos = nscroll = view->nlines - 2;
5767 if (view_is_hsplit_top(view))
5775 view->x = MAX(view->maxx - view->ncols / 3, 0);
5780 if (view->x + view->ncols / 3 < view->maxx)
5781 view->x += 2; /* move two columns right */
5787 view->x -= MIN(view->x, 2); /* move two columns back */
5796 s->selected_line = 1;
5797 s->first_displayed_line = 1;
5802 if (s->blame.nlines < eos) {
5803 s->selected_line = s->blame.nlines;
5804 s->first_displayed_line = 1;
5806 s->selected_line = eos;
5807 s->first_displayed_line = s->blame.nlines - (eos - 1);
5814 if (s->selected_line > 1)
5816 else if (s->selected_line == 1 &&
5817 s->first_displayed_line > 1)
5818 s->first_displayed_line--;
5829 if (s->first_displayed_line == 1) {
5830 if (view->count > 1)
5832 s->selected_line = MAX(1, s->selected_line - nscroll);
5836 if (s->first_displayed_line > nscroll)
5837 s->first_displayed_line -= nscroll;
5839 s->first_displayed_line = 1;
5844 if (s->selected_line < eos && s->first_displayed_line +
5845 s->selected_line <= s->blame.nlines)
5847 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5848 s->first_displayed_line++;
5854 struct got_object_id *id = NULL;
5857 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5858 s->first_displayed_line, s->selected_line);
5862 struct got_commit_object *commit, *pcommit;
5863 struct got_object_qid *pid;
5864 struct got_object_id *blob_id = NULL;
5866 err = got_object_open_as_commit(&commit,
5871 got_object_commit_get_parent_ids(commit));
5873 got_object_commit_close(commit);
5876 /* Check if path history ends here. */
5877 err = got_object_open_as_commit(&pcommit,
5881 err = got_object_id_by_path(&blob_id, s->repo,
5883 got_object_commit_close(pcommit);
5885 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5887 got_object_commit_close(commit);
5890 err = got_object_get_type(&obj_type, s->repo,
5893 /* Can't blame non-blob type objects. */
5894 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5895 got_object_commit_close(commit);
5898 err = got_object_qid_alloc(&s->blamed_commit,
5900 got_object_commit_close(commit);
5902 if (got_object_id_cmp(id,
5903 &s->blamed_commit->id) == 0)
5905 err = got_object_qid_alloc(&s->blamed_commit,
5911 thread_err = stop_blame(&s->blame);
5915 STAILQ_INSERT_HEAD(&s->blamed_commits,
5916 s->blamed_commit, entry);
5917 err = run_blame(view);
5923 struct got_object_qid *first;
5926 first = STAILQ_FIRST(&s->blamed_commits);
5927 if (!got_object_id_cmp(&first->id, s->commit_id))
5930 thread_err = stop_blame(&s->blame);
5934 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5935 got_object_qid_free(s->blamed_commit);
5937 STAILQ_FIRST(&s->blamed_commits);
5938 err = run_blame(view);
5944 struct got_object_id *id = NULL;
5947 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5948 s->first_displayed_line, s->selected_line);
5952 if (view_is_parent_view(view))
5953 view_get_split(view, &begin_y, &begin_x);
5954 err = log_annotated_line(&log_view, begin_y, begin_x,
5958 if (view_is_parent_view(view) &&
5959 view->mode == TOG_VIEW_SPLIT_HRZN) {
5960 err = view_init_hsplit(view, begin_y);
5966 log_view->focussed = 1;
5967 log_view->mode = view->mode;
5968 log_view->nlines = view->lines - begin_y;
5969 if (view_is_parent_view(view)) {
5970 view_transfer_size(log_view, view);
5971 err = view_close_child(view);
5974 err = view_set_child(view, log_view);
5977 view->focus_child = 1;
5979 *new_view = log_view;
5984 struct got_object_id *id = NULL;
5985 struct got_object_qid *pid;
5986 struct got_commit_object *commit = NULL;
5989 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5990 s->first_displayed_line, s->selected_line);
5993 err = got_object_open_as_commit(&commit, s->repo, id);
5996 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5998 /* traversed from diff view, release diff resources */
5999 err = close_diff_view(*new_view);
6002 diff_view = *new_view;
6004 if (view_is_parent_view(view))
6005 view_get_split(view, &begin_y, &begin_x);
6007 diff_view = view_open(0, 0, begin_y, begin_x,
6009 if (diff_view == NULL) {
6010 got_object_commit_close(commit);
6011 err = got_error_from_errno("view_open");
6015 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6016 id, NULL, NULL, 3, 0, 0, view, s->repo);
6017 got_object_commit_close(commit);
6019 view_close(diff_view);
6022 s->last_diffed_line = s->first_displayed_line - 1 +
6025 break; /* still open from active diff view */
6026 if (view_is_parent_view(view) &&
6027 view->mode == TOG_VIEW_SPLIT_HRZN) {
6028 err = view_init_hsplit(view, begin_y);
6034 diff_view->focussed = 1;
6035 diff_view->mode = view->mode;
6036 diff_view->nlines = view->lines - begin_y;
6037 if (view_is_parent_view(view)) {
6038 view_transfer_size(diff_view, view);
6039 err = view_close_child(view);
6042 err = view_set_child(view, diff_view);
6045 view->focus_child = 1;
6047 *new_view = diff_view;
6060 if (s->last_displayed_line >= s->blame.nlines &&
6061 s->selected_line >= MIN(s->blame.nlines,
6062 view->nlines - 2)) {
6066 if (s->last_displayed_line >= s->blame.nlines &&
6067 s->selected_line < view->nlines - 2) {
6069 MIN(nscroll, s->last_displayed_line -
6070 s->first_displayed_line - s->selected_line + 1);
6072 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6073 s->first_displayed_line += nscroll;
6075 s->first_displayed_line =
6076 s->blame.nlines - (view->nlines - 3);
6079 if (s->selected_line > view->nlines - 2) {
6080 s->selected_line = MIN(s->blame.nlines,
6088 return thread_err ? thread_err : err;
6091 static const struct got_error *
6092 reset_blame_view(struct tog_view *view)
6094 const struct got_error *err;
6095 struct tog_blame_view_state *s = &view->state.blame;
6099 err = stop_blame(&s->blame);
6103 return run_blame(view);
6106 static const struct got_error *
6107 cmd_blame(int argc, char *argv[])
6109 const struct got_error *error;
6110 struct got_repository *repo = NULL;
6111 struct got_worktree *worktree = NULL;
6112 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6113 char *link_target = NULL;
6114 struct got_object_id *commit_id = NULL;
6115 struct got_commit_object *commit = NULL;
6116 char *commit_id_str = NULL;
6118 struct tog_view *view;
6119 int *pack_fds = NULL;
6121 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6124 commit_id_str = optarg;
6127 repo_path = realpath(optarg, NULL);
6128 if (repo_path == NULL)
6129 return got_error_from_errno2("realpath",
6144 error = got_repo_pack_fds_open(&pack_fds);
6148 if (repo_path == NULL) {
6149 cwd = getcwd(NULL, 0);
6151 return got_error_from_errno("getcwd");
6152 error = got_worktree_open(&worktree, cwd);
6153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6157 strdup(got_worktree_get_repo_path(worktree));
6159 repo_path = strdup(cwd);
6160 if (repo_path == NULL) {
6161 error = got_error_from_errno("strdup");
6166 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6170 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6177 error = apply_unveil(got_repo_get_path(repo), NULL);
6181 error = tog_load_refs(repo, 0);
6185 if (commit_id_str == NULL) {
6186 struct got_reference *head_ref;
6187 error = got_ref_open(&head_ref, repo, worktree ?
6188 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6191 error = got_ref_resolve(&commit_id, repo, head_ref);
6192 got_ref_close(head_ref);
6194 error = got_repo_match_object_id(&commit_id, NULL,
6195 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6200 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6202 error = got_error_from_errno("view_open");
6206 error = got_object_open_as_commit(&commit, repo, commit_id);
6210 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6215 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6220 /* Release work tree lock. */
6221 got_worktree_close(worktree);
6224 error = view_loop(view);
6232 got_object_commit_close(commit);
6234 got_worktree_close(worktree);
6236 const struct got_error *close_err = got_repo_close(repo);
6241 const struct got_error *pack_err =
6242 got_repo_pack_fds_close(pack_fds);
6250 static const struct got_error *
6251 draw_tree_entries(struct tog_view *view, const char *parent_path)
6253 struct tog_tree_view_state *s = &view->state.tree;
6254 const struct got_error *err = NULL;
6255 struct got_tree_entry *te;
6257 struct tog_color *tc;
6258 int width, n, i, nentries;
6259 int limit = view->nlines;
6262 if (view_is_hsplit_top(view))
6263 --limit; /* border */
6265 werase(view->window);
6270 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6274 if (view_needs_focus_indication(view))
6275 wstandout(view->window);
6276 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6278 wattr_on(view->window,
6279 COLOR_PAIR(tc->colorpair), NULL);
6280 waddwstr(view->window, wline);
6282 wattr_off(view->window,
6283 COLOR_PAIR(tc->colorpair), NULL);
6284 if (view_needs_focus_indication(view))
6285 wstandend(view->window);
6288 if (width < view->ncols - 1)
6289 waddch(view->window, '\n');
6292 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6296 waddwstr(view->window, wline);
6299 if (width < view->ncols - 1)
6300 waddch(view->window, '\n');
6303 waddch(view->window, '\n');
6307 if (s->first_displayed_entry == NULL) {
6308 te = got_object_tree_get_first_entry(s->tree);
6309 if (s->selected == 0) {
6311 wstandout(view->window);
6312 s->selected_entry = NULL;
6314 waddstr(view->window, " ..\n"); /* parent directory */
6315 if (s->selected == 0 && view->focussed)
6316 wstandend(view->window);
6323 te = s->first_displayed_entry;
6326 nentries = got_object_tree_get_nentries(s->tree);
6327 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6328 char *line = NULL, *id_str = NULL, *link_target = NULL;
6329 const char *modestr = "";
6332 te = got_object_tree_get_entry(s->tree, i);
6333 mode = got_tree_entry_get_mode(te);
6336 err = got_object_id_str(&id_str,
6337 got_tree_entry_get_id(te));
6339 return got_error_from_errno(
6340 "got_object_id_str");
6342 if (got_object_tree_entry_is_submodule(te))
6344 else if (S_ISLNK(mode)) {
6347 err = got_tree_entry_get_symlink_target(&link_target,
6353 for (i = 0; i < strlen(link_target); i++) {
6354 if (!isprint((unsigned char)link_target[i]))
6355 link_target[i] = '?';
6359 else if (S_ISDIR(mode))
6361 else if (mode & S_IXUSR)
6363 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6364 got_tree_entry_get_name(te), modestr,
6365 link_target ? " -> ": "",
6366 link_target ? link_target : "") == -1) {
6369 return got_error_from_errno("asprintf");
6373 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6379 if (n == s->selected) {
6381 wstandout(view->window);
6382 s->selected_entry = te;
6384 tc = match_color(&s->colors, line);
6386 wattr_on(view->window,
6387 COLOR_PAIR(tc->colorpair), NULL);
6388 waddwstr(view->window, wline);
6390 wattr_off(view->window,
6391 COLOR_PAIR(tc->colorpair), NULL);
6392 if (width < view->ncols - 1)
6393 waddch(view->window, '\n');
6394 if (n == s->selected && view->focussed)
6395 wstandend(view->window);
6401 s->last_displayed_entry = te;
6410 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6412 struct got_tree_entry *te;
6413 int isroot = s->tree == s->root;
6416 if (s->first_displayed_entry == NULL)
6419 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6420 while (i++ < maxscroll) {
6423 s->first_displayed_entry = NULL;
6426 s->first_displayed_entry = te;
6427 te = got_tree_entry_get_prev(s->tree, te);
6431 static const struct got_error *
6432 tree_scroll_down(struct tog_view *view, int maxscroll)
6434 struct tog_tree_view_state *s = &view->state.tree;
6435 struct got_tree_entry *next, *last;
6438 if (s->first_displayed_entry)
6439 next = got_tree_entry_get_next(s->tree,
6440 s->first_displayed_entry);
6442 next = got_object_tree_get_first_entry(s->tree);
6444 last = s->last_displayed_entry;
6445 while (next && n++ < maxscroll) {
6447 last = got_tree_entry_get_next(s->tree, last);
6448 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6449 s->first_displayed_entry = next;
6450 next = got_tree_entry_get_next(s->tree, next);
6457 static const struct got_error *
6458 tree_entry_path(char **path, struct tog_parent_trees *parents,
6459 struct got_tree_entry *te)
6461 const struct got_error *err = NULL;
6462 struct tog_parent_tree *pt;
6463 size_t len = 2; /* for leading slash and NUL */
6465 TAILQ_FOREACH(pt, parents, entry)
6466 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6469 len += strlen(got_tree_entry_get_name(te));
6471 *path = calloc(1, len);
6473 return got_error_from_errno("calloc");
6476 pt = TAILQ_LAST(parents, tog_parent_trees);
6478 const char *name = got_tree_entry_get_name(pt->selected_entry);
6479 if (strlcat(*path, name, len) >= len) {
6480 err = got_error(GOT_ERR_NO_SPACE);
6483 if (strlcat(*path, "/", len) >= len) {
6484 err = got_error(GOT_ERR_NO_SPACE);
6487 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6490 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6491 err = got_error(GOT_ERR_NO_SPACE);
6503 static const struct got_error *
6504 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6505 struct got_tree_entry *te, struct tog_parent_trees *parents,
6506 struct got_object_id *commit_id, struct got_repository *repo)
6508 const struct got_error *err = NULL;
6510 struct tog_view *blame_view;
6514 err = tree_entry_path(&path, parents, te);
6518 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6519 if (blame_view == NULL) {
6520 err = got_error_from_errno("view_open");
6524 err = open_blame_view(blame_view, path, commit_id, repo);
6526 if (err->code == GOT_ERR_CANCELLED)
6528 view_close(blame_view);
6530 *new_view = blame_view;
6536 static const struct got_error *
6537 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6538 struct tog_tree_view_state *s)
6540 struct tog_view *log_view;
6541 const struct got_error *err = NULL;
6546 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6547 if (log_view == NULL)
6548 return got_error_from_errno("view_open");
6550 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6554 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6557 view_close(log_view);
6559 *new_view = log_view;
6564 static const struct got_error *
6565 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6566 const char *head_ref_name, struct got_repository *repo)
6568 const struct got_error *err = NULL;
6569 char *commit_id_str = NULL;
6570 struct tog_tree_view_state *s = &view->state.tree;
6571 struct got_commit_object *commit = NULL;
6573 TAILQ_INIT(&s->parents);
6574 STAILQ_INIT(&s->colors);
6576 s->commit_id = got_object_id_dup(commit_id);
6577 if (s->commit_id == NULL)
6578 return got_error_from_errno("got_object_id_dup");
6580 err = got_object_open_as_commit(&commit, repo, commit_id);
6585 * The root is opened here and will be closed when the view is closed.
6586 * Any visited subtrees and their path-wise parents are opened and
6589 err = got_object_open_as_tree(&s->root, repo,
6590 got_object_commit_get_tree_id(commit));
6595 err = got_object_id_str(&commit_id_str, commit_id);
6599 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6600 err = got_error_from_errno("asprintf");
6604 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6605 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6606 if (head_ref_name) {
6607 s->head_ref_name = strdup(head_ref_name);
6608 if (s->head_ref_name == NULL) {
6609 err = got_error_from_errno("strdup");
6615 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6616 err = add_color(&s->colors, "\\$$",
6617 TOG_COLOR_TREE_SUBMODULE,
6618 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6621 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6622 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6625 err = add_color(&s->colors, "/$",
6626 TOG_COLOR_TREE_DIRECTORY,
6627 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6631 err = add_color(&s->colors, "\\*$",
6632 TOG_COLOR_TREE_EXECUTABLE,
6633 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6637 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6638 get_color_value("TOG_COLOR_COMMIT"));
6643 view->show = show_tree_view;
6644 view->input = input_tree_view;
6645 view->close = close_tree_view;
6646 view->search_start = search_start_tree_view;
6647 view->search_next = search_next_tree_view;
6649 free(commit_id_str);
6651 got_object_commit_close(commit);
6653 close_tree_view(view);
6657 static const struct got_error *
6658 close_tree_view(struct tog_view *view)
6660 struct tog_tree_view_state *s = &view->state.tree;
6662 free_colors(&s->colors);
6663 free(s->tree_label);
6664 s->tree_label = NULL;
6666 s->commit_id = NULL;
6667 free(s->head_ref_name);
6668 s->head_ref_name = NULL;
6669 while (!TAILQ_EMPTY(&s->parents)) {
6670 struct tog_parent_tree *parent;
6671 parent = TAILQ_FIRST(&s->parents);
6672 TAILQ_REMOVE(&s->parents, parent, entry);
6673 if (parent->tree != s->root)
6674 got_object_tree_close(parent->tree);
6678 if (s->tree != NULL && s->tree != s->root)
6679 got_object_tree_close(s->tree);
6681 got_object_tree_close(s->root);
6685 static const struct got_error *
6686 search_start_tree_view(struct tog_view *view)
6688 struct tog_tree_view_state *s = &view->state.tree;
6690 s->matched_entry = NULL;
6695 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6697 regmatch_t regmatch;
6699 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6703 static const struct got_error *
6704 search_next_tree_view(struct tog_view *view)
6706 struct tog_tree_view_state *s = &view->state.tree;
6707 struct got_tree_entry *te = NULL;
6709 if (!view->searching) {
6710 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6714 if (s->matched_entry) {
6715 if (view->searching == TOG_SEARCH_FORWARD) {
6716 if (s->selected_entry)
6717 te = got_tree_entry_get_next(s->tree,
6720 te = got_object_tree_get_first_entry(s->tree);
6722 if (s->selected_entry == NULL)
6723 te = got_object_tree_get_last_entry(s->tree);
6725 te = got_tree_entry_get_prev(s->tree,
6729 if (s->selected_entry)
6730 te = s->selected_entry;
6731 else if (view->searching == TOG_SEARCH_FORWARD)
6732 te = got_object_tree_get_first_entry(s->tree);
6734 te = got_object_tree_get_last_entry(s->tree);
6739 if (s->matched_entry == NULL) {
6740 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6743 if (view->searching == TOG_SEARCH_FORWARD)
6744 te = got_object_tree_get_first_entry(s->tree);
6746 te = got_object_tree_get_last_entry(s->tree);
6749 if (match_tree_entry(te, &view->regex)) {
6750 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6751 s->matched_entry = te;
6755 if (view->searching == TOG_SEARCH_FORWARD)
6756 te = got_tree_entry_get_next(s->tree, te);
6758 te = got_tree_entry_get_prev(s->tree, te);
6761 if (s->matched_entry) {
6762 s->first_displayed_entry = s->matched_entry;
6769 static const struct got_error *
6770 show_tree_view(struct tog_view *view)
6772 const struct got_error *err = NULL;
6773 struct tog_tree_view_state *s = &view->state.tree;
6776 err = tree_entry_path(&parent_path, &s->parents, NULL);
6780 err = draw_tree_entries(view, parent_path);
6787 static const struct got_error *
6788 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6790 const struct got_error *err = NULL;
6791 struct tog_tree_view_state *s = &view->state.tree;
6792 struct tog_view *log_view, *ref_view;
6793 struct got_tree_entry *te;
6794 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6798 s->show_ids = !s->show_ids;
6803 if (!s->selected_entry)
6805 if (view_is_parent_view(view))
6806 view_get_split(view, &begin_y, &begin_x);
6807 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6808 if (view_is_parent_view(view) &&
6809 view->mode == TOG_VIEW_SPLIT_HRZN) {
6810 err = view_init_hsplit(view, begin_y);
6815 log_view->focussed = 1;
6816 log_view->mode = view->mode;
6817 log_view->nlines = view->lines - begin_y;
6818 if (view_is_parent_view(view)) {
6819 view_transfer_size(log_view, view);
6820 err = view_close_child(view);
6823 err = view_set_child(view, log_view);
6826 view->focus_child = 1;
6828 *new_view = log_view;
6832 if (view_is_parent_view(view))
6833 view_get_split(view, &begin_y, &begin_x);
6834 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6835 if (ref_view == NULL)
6836 return got_error_from_errno("view_open");
6837 err = open_ref_view(ref_view, s->repo);
6839 view_close(ref_view);
6842 if (view_is_parent_view(view) &&
6843 view->mode == TOG_VIEW_SPLIT_HRZN) {
6844 err = view_init_hsplit(view, begin_y);
6849 ref_view->focussed = 1;
6850 ref_view->mode = view->mode;
6851 ref_view->nlines = view->lines - begin_y;
6852 if (view_is_parent_view(view)) {
6853 view_transfer_size(ref_view, view);
6854 err = view_close_child(view);
6857 err = view_set_child(view, ref_view);
6860 view->focus_child = 1;
6862 *new_view = ref_view;
6868 if (s->tree == s->root)
6869 s->first_displayed_entry =
6870 got_object_tree_get_first_entry(s->tree);
6872 s->first_displayed_entry = NULL;
6876 int eos = view->nlines - 3;
6878 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6882 te = got_object_tree_get_last_entry(s->tree);
6883 for (n = 0; n < eos; n++) {
6885 if (s->tree != s->root) {
6886 s->first_displayed_entry = NULL;
6891 s->first_displayed_entry = te;
6892 te = got_tree_entry_get_prev(s->tree, te);
6895 s->selected = n - 1;
6901 if (s->selected > 0) {
6905 tree_scroll_up(s, 1);
6906 if (s->selected_entry == NULL ||
6907 (s->tree == s->root && s->selected_entry ==
6908 got_object_tree_get_first_entry(s->tree)))
6918 if (s->tree == s->root) {
6919 if (got_object_tree_get_first_entry(s->tree) ==
6920 s->first_displayed_entry)
6921 s->selected -= MIN(s->selected, nscroll);
6923 if (s->first_displayed_entry == NULL)
6924 s->selected -= MIN(s->selected, nscroll);
6926 tree_scroll_up(s, MAX(0, nscroll));
6927 if (s->selected_entry == NULL ||
6928 (s->tree == s->root && s->selected_entry ==
6929 got_object_tree_get_first_entry(s->tree)))
6935 if (s->selected < s->ndisplayed - 1) {
6939 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6941 /* can't scroll any further */
6945 tree_scroll_down(view, 1);
6955 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6957 /* can't scroll any further; move cursor down */
6958 if (s->selected < s->ndisplayed - 1)
6959 s->selected += MIN(nscroll,
6960 s->ndisplayed - s->selected - 1);
6965 tree_scroll_down(view, nscroll);
6970 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6971 struct tog_parent_tree *parent;
6972 /* user selected '..' */
6973 if (s->tree == s->root) {
6977 parent = TAILQ_FIRST(&s->parents);
6978 TAILQ_REMOVE(&s->parents, parent,
6980 got_object_tree_close(s->tree);
6981 s->tree = parent->tree;
6982 s->first_displayed_entry =
6983 parent->first_displayed_entry;
6985 parent->selected_entry;
6986 s->selected = parent->selected;
6987 if (s->selected > view->nlines - 3) {
6988 err = offset_selection_down(view);
6993 } else if (S_ISDIR(got_tree_entry_get_mode(
6994 s->selected_entry))) {
6995 struct got_tree_object *subtree;
6997 err = got_object_open_as_tree(&subtree, s->repo,
6998 got_tree_entry_get_id(s->selected_entry));
7001 err = tree_view_visit_subtree(s, subtree);
7003 got_object_tree_close(subtree);
7006 } else if (S_ISREG(got_tree_entry_get_mode(
7007 s->selected_entry))) {
7008 struct tog_view *blame_view;
7009 int begin_x = 0, begin_y = 0;
7011 if (view_is_parent_view(view))
7012 view_get_split(view, &begin_y, &begin_x);
7014 err = blame_tree_entry(&blame_view, begin_y, begin_x,
7015 s->selected_entry, &s->parents,
7016 s->commit_id, s->repo);
7020 if (view_is_parent_view(view) &&
7021 view->mode == TOG_VIEW_SPLIT_HRZN) {
7022 err = view_init_hsplit(view, begin_y);
7029 blame_view->focussed = 1;
7030 blame_view->mode = view->mode;
7031 blame_view->nlines = view->lines - begin_y;
7032 if (view_is_parent_view(view)) {
7033 view_transfer_size(blame_view, view);
7034 err = view_close_child(view);
7037 err = view_set_child(view, blame_view);
7040 view->focus_child = 1;
7042 *new_view = blame_view;
7046 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7047 s->selected = view->nlines - 4;
7063 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7068 static const struct got_error *
7069 cmd_tree(int argc, char *argv[])
7071 const struct got_error *error;
7072 struct got_repository *repo = NULL;
7073 struct got_worktree *worktree = NULL;
7074 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7075 struct got_object_id *commit_id = NULL;
7076 struct got_commit_object *commit = NULL;
7077 const char *commit_id_arg = NULL;
7079 struct got_reference *ref = NULL;
7080 const char *head_ref_name = NULL;
7082 struct tog_view *view;
7083 int *pack_fds = NULL;
7085 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7088 commit_id_arg = optarg;
7091 repo_path = realpath(optarg, NULL);
7092 if (repo_path == NULL)
7093 return got_error_from_errno2("realpath",
7108 error = got_repo_pack_fds_open(&pack_fds);
7112 if (repo_path == NULL) {
7113 cwd = getcwd(NULL, 0);
7115 return got_error_from_errno("getcwd");
7116 error = got_worktree_open(&worktree, cwd);
7117 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7121 strdup(got_worktree_get_repo_path(worktree));
7123 repo_path = strdup(cwd);
7124 if (repo_path == NULL) {
7125 error = got_error_from_errno("strdup");
7130 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7134 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7141 error = apply_unveil(got_repo_get_path(repo), NULL);
7145 error = tog_load_refs(repo, 0);
7149 if (commit_id_arg == NULL) {
7150 error = got_repo_match_object_id(&commit_id, &label,
7151 worktree ? got_worktree_get_head_ref_name(worktree) :
7152 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7155 head_ref_name = label;
7157 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7159 head_ref_name = got_ref_get_name(ref);
7160 else if (error->code != GOT_ERR_NOT_REF)
7162 error = got_repo_match_object_id(&commit_id, NULL,
7163 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7168 error = got_object_open_as_commit(&commit, repo, commit_id);
7172 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7174 error = got_error_from_errno("view_open");
7177 error = open_tree_view(view, commit_id, head_ref_name, repo);
7180 if (!got_path_is_root_dir(in_repo_path)) {
7181 error = tree_view_walk_path(&view->state.tree, commit,
7188 /* Release work tree lock. */
7189 got_worktree_close(worktree);
7192 error = view_loop(view);
7201 const struct got_error *close_err = got_repo_close(repo);
7206 const struct got_error *pack_err =
7207 got_repo_pack_fds_close(pack_fds);
7215 static const struct got_error *
7216 ref_view_load_refs(struct tog_ref_view_state *s)
7218 struct got_reflist_entry *sre;
7219 struct tog_reflist_entry *re;
7222 TAILQ_FOREACH(sre, &tog_refs, entry) {
7223 if (strncmp(got_ref_get_name(sre->ref),
7224 "refs/got/", 9) == 0 &&
7225 strncmp(got_ref_get_name(sre->ref),
7226 "refs/got/backup/", 16) != 0)
7229 re = malloc(sizeof(*re));
7231 return got_error_from_errno("malloc");
7233 re->ref = got_ref_dup(sre->ref);
7234 if (re->ref == NULL)
7235 return got_error_from_errno("got_ref_dup");
7236 re->idx = s->nrefs++;
7237 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7240 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7245 ref_view_free_refs(struct tog_ref_view_state *s)
7247 struct tog_reflist_entry *re;
7249 while (!TAILQ_EMPTY(&s->refs)) {
7250 re = TAILQ_FIRST(&s->refs);
7251 TAILQ_REMOVE(&s->refs, re, entry);
7252 got_ref_close(re->ref);
7257 static const struct got_error *
7258 open_ref_view(struct tog_view *view, struct got_repository *repo)
7260 const struct got_error *err = NULL;
7261 struct tog_ref_view_state *s = &view->state.ref;
7263 s->selected_entry = 0;
7266 TAILQ_INIT(&s->refs);
7267 STAILQ_INIT(&s->colors);
7269 err = ref_view_load_refs(s);
7273 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7274 err = add_color(&s->colors, "^refs/heads/",
7275 TOG_COLOR_REFS_HEADS,
7276 get_color_value("TOG_COLOR_REFS_HEADS"));
7280 err = add_color(&s->colors, "^refs/tags/",
7281 TOG_COLOR_REFS_TAGS,
7282 get_color_value("TOG_COLOR_REFS_TAGS"));
7286 err = add_color(&s->colors, "^refs/remotes/",
7287 TOG_COLOR_REFS_REMOTES,
7288 get_color_value("TOG_COLOR_REFS_REMOTES"));
7292 err = add_color(&s->colors, "^refs/got/backup/",
7293 TOG_COLOR_REFS_BACKUP,
7294 get_color_value("TOG_COLOR_REFS_BACKUP"));
7299 view->show = show_ref_view;
7300 view->input = input_ref_view;
7301 view->close = close_ref_view;
7302 view->search_start = search_start_ref_view;
7303 view->search_next = search_next_ref_view;
7306 free_colors(&s->colors);
7310 static const struct got_error *
7311 close_ref_view(struct tog_view *view)
7313 struct tog_ref_view_state *s = &view->state.ref;
7315 ref_view_free_refs(s);
7316 free_colors(&s->colors);
7321 static const struct got_error *
7322 resolve_reflist_entry(struct got_object_id **commit_id,
7323 struct tog_reflist_entry *re, struct got_repository *repo)
7325 const struct got_error *err = NULL;
7326 struct got_object_id *obj_id;
7327 struct got_tag_object *tag = NULL;
7332 err = got_ref_resolve(&obj_id, repo, re->ref);
7336 err = got_object_get_type(&obj_type, repo, obj_id);
7341 case GOT_OBJ_TYPE_COMMIT:
7342 *commit_id = obj_id;
7344 case GOT_OBJ_TYPE_TAG:
7345 err = got_object_open_as_tag(&tag, repo, obj_id);
7349 err = got_object_get_type(&obj_type, repo,
7350 got_object_tag_get_object_id(tag));
7353 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7354 err = got_error(GOT_ERR_OBJ_TYPE);
7357 *commit_id = got_object_id_dup(
7358 got_object_tag_get_object_id(tag));
7359 if (*commit_id == NULL) {
7360 err = got_error_from_errno("got_object_id_dup");
7365 err = got_error(GOT_ERR_OBJ_TYPE);
7371 got_object_tag_close(tag);
7379 static const struct got_error *
7380 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7381 struct tog_reflist_entry *re, struct got_repository *repo)
7383 struct tog_view *log_view;
7384 const struct got_error *err = NULL;
7385 struct got_object_id *commit_id = NULL;
7389 err = resolve_reflist_entry(&commit_id, re, repo);
7391 if (err->code != GOT_ERR_OBJ_TYPE)
7397 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7398 if (log_view == NULL) {
7399 err = got_error_from_errno("view_open");
7403 err = open_log_view(log_view, commit_id, repo,
7404 got_ref_get_name(re->ref), "", 0);
7407 view_close(log_view);
7409 *new_view = log_view;
7415 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7417 struct tog_reflist_entry *re;
7420 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7423 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7424 while (i++ < maxscroll) {
7427 s->first_displayed_entry = re;
7428 re = TAILQ_PREV(re, tog_reflist_head, entry);
7432 static const struct got_error *
7433 ref_scroll_down(struct tog_view *view, int maxscroll)
7435 struct tog_ref_view_state *s = &view->state.ref;
7436 struct tog_reflist_entry *next, *last;
7439 if (s->first_displayed_entry)
7440 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7442 next = TAILQ_FIRST(&s->refs);
7444 last = s->last_displayed_entry;
7445 while (next && n++ < maxscroll) {
7447 last = TAILQ_NEXT(last, entry);
7448 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7449 s->first_displayed_entry = next;
7450 next = TAILQ_NEXT(next, entry);
7457 static const struct got_error *
7458 search_start_ref_view(struct tog_view *view)
7460 struct tog_ref_view_state *s = &view->state.ref;
7462 s->matched_entry = NULL;
7467 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7469 regmatch_t regmatch;
7471 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7475 static const struct got_error *
7476 search_next_ref_view(struct tog_view *view)
7478 struct tog_ref_view_state *s = &view->state.ref;
7479 struct tog_reflist_entry *re = NULL;
7481 if (!view->searching) {
7482 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7486 if (s->matched_entry) {
7487 if (view->searching == TOG_SEARCH_FORWARD) {
7488 if (s->selected_entry)
7489 re = TAILQ_NEXT(s->selected_entry, entry);
7491 re = TAILQ_PREV(s->selected_entry,
7492 tog_reflist_head, entry);
7494 if (s->selected_entry == NULL)
7495 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7497 re = TAILQ_PREV(s->selected_entry,
7498 tog_reflist_head, entry);
7501 if (s->selected_entry)
7502 re = s->selected_entry;
7503 else if (view->searching == TOG_SEARCH_FORWARD)
7504 re = TAILQ_FIRST(&s->refs);
7506 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7511 if (s->matched_entry == NULL) {
7512 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7515 if (view->searching == TOG_SEARCH_FORWARD)
7516 re = TAILQ_FIRST(&s->refs);
7518 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7521 if (match_reflist_entry(re, &view->regex)) {
7522 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7523 s->matched_entry = re;
7527 if (view->searching == TOG_SEARCH_FORWARD)
7528 re = TAILQ_NEXT(re, entry);
7530 re = TAILQ_PREV(re, tog_reflist_head, entry);
7533 if (s->matched_entry) {
7534 s->first_displayed_entry = s->matched_entry;
7541 static const struct got_error *
7542 show_ref_view(struct tog_view *view)
7544 const struct got_error *err = NULL;
7545 struct tog_ref_view_state *s = &view->state.ref;
7546 struct tog_reflist_entry *re;
7549 struct tog_color *tc;
7551 int limit = view->nlines;
7553 werase(view->window);
7556 if (view_is_hsplit_top(view))
7557 --limit; /* border */
7562 re = s->first_displayed_entry;
7564 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7566 return got_error_from_errno("asprintf");
7568 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7573 if (view_needs_focus_indication(view))
7574 wstandout(view->window);
7575 waddwstr(view->window, wline);
7576 if (view_needs_focus_indication(view))
7577 wstandend(view->window);
7582 if (width < view->ncols - 1)
7583 waddch(view->window, '\n');
7588 while (re && limit > 0) {
7590 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7593 struct got_commit_object *ci;
7594 struct got_tag_object *tag;
7595 struct got_object_id *id;
7599 err = got_ref_resolve(&id, s->repo, re->ref);
7602 err = got_object_open_as_tag(&tag, s->repo, id);
7604 if (err->code != GOT_ERR_OBJ_TYPE) {
7608 err = got_object_open_as_commit(&ci, s->repo,
7614 t = got_object_commit_get_committer_time(ci);
7615 got_object_commit_close(ci);
7617 t = got_object_tag_get_tagger_time(tag);
7618 got_object_tag_close(tag);
7621 if (gmtime_r(&t, &tm) == NULL)
7622 return got_error_from_errno("gmtime_r");
7623 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7624 return got_error(GOT_ERR_NO_SPACE);
7626 if (got_ref_is_symbolic(re->ref)) {
7627 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7628 ymd : "", got_ref_get_name(re->ref),
7629 got_ref_get_symref_target(re->ref)) == -1)
7630 return got_error_from_errno("asprintf");
7631 } else if (s->show_ids) {
7632 struct got_object_id *id;
7634 err = got_ref_resolve(&id, s->repo, re->ref);
7637 err = got_object_id_str(&id_str, id);
7642 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7643 got_ref_get_name(re->ref), id_str) == -1) {
7644 err = got_error_from_errno("asprintf");
7651 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7652 got_ref_get_name(re->ref)) == -1)
7653 return got_error_from_errno("asprintf");
7655 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7661 if (n == s->selected) {
7663 wstandout(view->window);
7664 s->selected_entry = re;
7666 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7668 wattr_on(view->window,
7669 COLOR_PAIR(tc->colorpair), NULL);
7670 waddwstr(view->window, wline);
7672 wattr_off(view->window,
7673 COLOR_PAIR(tc->colorpair), NULL);
7674 if (width < view->ncols - 1)
7675 waddch(view->window, '\n');
7676 if (n == s->selected && view->focussed)
7677 wstandend(view->window);
7683 s->last_displayed_entry = re;
7686 re = TAILQ_NEXT(re, entry);
7693 static const struct got_error *
7694 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7695 struct tog_reflist_entry *re, struct got_repository *repo)
7697 const struct got_error *err = NULL;
7698 struct got_object_id *commit_id = NULL;
7699 struct tog_view *tree_view;
7703 err = resolve_reflist_entry(&commit_id, re, repo);
7705 if (err->code != GOT_ERR_OBJ_TYPE)
7712 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7713 if (tree_view == NULL) {
7714 err = got_error_from_errno("view_open");
7718 err = open_tree_view(tree_view, commit_id,
7719 got_ref_get_name(re->ref), repo);
7723 *new_view = tree_view;
7728 static const struct got_error *
7729 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7731 const struct got_error *err = NULL;
7732 struct tog_ref_view_state *s = &view->state.ref;
7733 struct tog_view *log_view, *tree_view;
7734 struct tog_reflist_entry *re;
7735 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7739 s->show_ids = !s->show_ids;
7743 s->show_date = !s->show_date;
7747 s->sort_by_date = !s->sort_by_date;
7749 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7750 got_ref_cmp_by_commit_timestamp_descending :
7751 tog_ref_cmp_by_name, s->repo);
7754 got_reflist_object_id_map_free(tog_refs_idmap);
7755 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7756 &tog_refs, s->repo);
7759 ref_view_free_refs(s);
7760 err = ref_view_load_refs(s);
7765 if (!s->selected_entry)
7767 if (view_is_parent_view(view))
7768 view_get_split(view, &begin_y, &begin_x);
7770 err = log_ref_entry(&log_view, begin_y, begin_x,
7771 s->selected_entry, s->repo);
7775 if (view_is_parent_view(view) &&
7776 view->mode == TOG_VIEW_SPLIT_HRZN) {
7777 err = view_init_hsplit(view, begin_y);
7783 log_view->focussed = 1;
7784 log_view->mode = view->mode;
7785 log_view->nlines = view->lines - begin_y;
7786 if (view_is_parent_view(view)) {
7787 view_transfer_size(log_view, view);
7788 err = view_close_child(view);
7791 err = view_set_child(view, log_view);
7794 view->focus_child = 1;
7796 *new_view = log_view;
7800 if (!s->selected_entry)
7802 if (view_is_parent_view(view))
7803 view_get_split(view, &begin_y, &begin_x);
7804 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7805 s->selected_entry, s->repo);
7806 if (err || tree_view == NULL)
7808 if (view_is_parent_view(view) &&
7809 view->mode == TOG_VIEW_SPLIT_HRZN) {
7810 err = view_init_hsplit(view, begin_y);
7815 tree_view->focussed = 1;
7816 tree_view->mode = view->mode;
7817 tree_view->nlines = view->lines - begin_y;
7818 if (view_is_parent_view(view)) {
7819 view_transfer_size(tree_view, view);
7820 err = view_close_child(view);
7823 err = view_set_child(view, tree_view);
7826 view->focus_child = 1;
7828 *new_view = tree_view;
7834 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7838 int eos = view->nlines - 1;
7840 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7844 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7845 for (n = 0; n < eos; n++) {
7848 s->first_displayed_entry = re;
7849 re = TAILQ_PREV(re, tog_reflist_head, entry);
7852 s->selected = n - 1;
7858 if (s->selected > 0) {
7862 ref_scroll_up(s, 1);
7863 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7873 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7874 s->selected -= MIN(nscroll, s->selected);
7875 ref_scroll_up(s, MAX(0, nscroll));
7876 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7882 if (s->selected < s->ndisplayed - 1) {
7886 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7887 /* can't scroll any further */
7891 ref_scroll_down(view, 1);
7901 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7902 /* can't scroll any further; move cursor down */
7903 if (s->selected < s->ndisplayed - 1)
7904 s->selected += MIN(nscroll,
7905 s->ndisplayed - s->selected - 1);
7906 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7907 s->selected += s->ndisplayed - s->selected - 1;
7911 ref_scroll_down(view, nscroll);
7916 err = tog_load_refs(s->repo, s->sort_by_date);
7919 ref_view_free_refs(s);
7920 err = ref_view_load_refs(s);
7923 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7924 s->selected = view->nlines - 2;
7938 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7943 static const struct got_error *
7944 cmd_ref(int argc, char *argv[])
7946 const struct got_error *error;
7947 struct got_repository *repo = NULL;
7948 struct got_worktree *worktree = NULL;
7949 char *cwd = NULL, *repo_path = NULL;
7951 struct tog_view *view;
7952 int *pack_fds = NULL;
7954 while ((ch = getopt(argc, argv, "r:")) != -1) {
7957 repo_path = realpath(optarg, NULL);
7958 if (repo_path == NULL)
7959 return got_error_from_errno2("realpath",
7974 error = got_repo_pack_fds_open(&pack_fds);
7978 if (repo_path == NULL) {
7979 cwd = getcwd(NULL, 0);
7981 return got_error_from_errno("getcwd");
7982 error = got_worktree_open(&worktree, cwd);
7983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7987 strdup(got_worktree_get_repo_path(worktree));
7989 repo_path = strdup(cwd);
7990 if (repo_path == NULL) {
7991 error = got_error_from_errno("strdup");
7996 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8002 error = apply_unveil(got_repo_get_path(repo), NULL);
8006 error = tog_load_refs(repo, 0);
8010 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8012 error = got_error_from_errno("view_open");
8016 error = open_ref_view(view, repo);
8021 /* Release work tree lock. */
8022 got_worktree_close(worktree);
8025 error = view_loop(view);
8030 const struct got_error *close_err = got_repo_close(repo);
8035 const struct got_error *pack_err =
8036 got_repo_pack_fds_close(pack_fds);
8045 * If view was scrolled down to move the selected line into view when opening a
8046 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8049 offset_selection_up(struct tog_view *view)
8051 switch (view->type) {
8052 case TOG_VIEW_BLAME: {
8053 struct tog_blame_view_state *s = &view->state.blame;
8054 if (s->first_displayed_line == 1) {
8055 s->selected_line = MAX(s->selected_line - view->offset,
8059 if (s->first_displayed_line > view->offset)
8060 s->first_displayed_line -= view->offset;
8062 s->first_displayed_line = 1;
8063 s->selected_line += view->offset;
8067 log_scroll_up(&view->state.log, view->offset);
8068 view->state.log.selected += view->offset;
8071 ref_scroll_up(&view->state.ref, view->offset);
8072 view->state.ref.selected += view->offset;
8075 tree_scroll_up(&view->state.tree, view->offset);
8076 view->state.tree.selected += view->offset;
8086 * If the selected line is in the section of screen covered by the bottom split,
8087 * scroll down offset lines to move it into view and index its new position.
8089 static const struct got_error *
8090 offset_selection_down(struct tog_view *view)
8092 const struct got_error *err = NULL;
8093 const struct got_error *(*scrolld)(struct tog_view *, int);
8094 int *selected = NULL;
8097 switch (view->type) {
8098 case TOG_VIEW_BLAME: {
8099 struct tog_blame_view_state *s = &view->state.blame;
8102 if (s->selected_line > view->nlines - header) {
8103 offset = abs(view->nlines - s->selected_line - header);
8104 s->first_displayed_line += offset;
8105 s->selected_line -= offset;
8106 view->offset = offset;
8110 case TOG_VIEW_LOG: {
8111 struct tog_log_view_state *s = &view->state.log;
8112 scrolld = &log_scroll_down;
8113 header = view_is_parent_view(view) ? 3 : 2;
8114 selected = &s->selected;
8117 case TOG_VIEW_REF: {
8118 struct tog_ref_view_state *s = &view->state.ref;
8119 scrolld = &ref_scroll_down;
8121 selected = &s->selected;
8124 case TOG_VIEW_TREE: {
8125 struct tog_tree_view_state *s = &view->state.tree;
8126 scrolld = &tree_scroll_down;
8128 selected = &s->selected;
8138 if (selected && *selected > view->nlines - header) {
8139 offset = abs(view->nlines - *selected - header);
8140 view->offset = offset;
8141 if (scrolld && offset) {
8142 err = scrolld(view, offset);
8143 *selected -= offset;
8151 list_commands(FILE *fp)
8155 fprintf(fp, "commands:");
8156 for (i = 0; i < nitems(tog_commands); i++) {
8157 const struct tog_cmd *cmd = &tog_commands[i];
8158 fprintf(fp, " %s", cmd->name);
8164 usage(int hflag, int status)
8166 FILE *fp = (status == 0) ? stdout : stderr;
8168 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8171 fprintf(fp, "lazy usage: %s path\n", getprogname());
8178 make_argv(int argc, ...)
8186 argv = calloc(argc, sizeof(char *));
8189 for (i = 0; i < argc; i++) {
8190 argv[i] = strdup(va_arg(ap, char *));
8191 if (argv[i] == NULL)
8200 * Try to convert 'tog path' into a 'tog log path' command.
8201 * The user could simply have mistyped the command rather than knowingly
8202 * provided a path. So check whether argv[0] can in fact be resolved
8203 * to a path in the HEAD commit and print a special error if not.
8204 * This hack is for mpi@ <3
8206 static const struct got_error *
8207 tog_log_with_path(int argc, char *argv[])
8209 const struct got_error *error = NULL, *close_err;
8210 const struct tog_cmd *cmd = NULL;
8211 struct got_repository *repo = NULL;
8212 struct got_worktree *worktree = NULL;
8213 struct got_object_id *commit_id = NULL, *id = NULL;
8214 struct got_commit_object *commit = NULL;
8215 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8216 char *commit_id_str = NULL, **cmd_argv = NULL;
8217 int *pack_fds = NULL;
8219 cwd = getcwd(NULL, 0);
8221 return got_error_from_errno("getcwd");
8223 error = got_repo_pack_fds_open(&pack_fds);
8227 error = got_worktree_open(&worktree, cwd);
8228 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8232 repo_path = strdup(got_worktree_get_repo_path(worktree));
8234 repo_path = strdup(cwd);
8235 if (repo_path == NULL) {
8236 error = got_error_from_errno("strdup");
8240 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8244 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8249 error = tog_load_refs(repo, 0);
8252 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8253 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8254 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8259 got_worktree_close(worktree);
8263 error = got_object_open_as_commit(&commit, repo, commit_id);
8267 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8269 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8271 fprintf(stderr, "%s: '%s' is no known command or path\n",
8272 getprogname(), argv[0]);
8277 close_err = got_repo_close(repo);
8282 error = got_object_id_str(&commit_id_str, commit_id);
8286 cmd = &tog_commands[0]; /* log */
8288 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8289 error = cmd->cmd_main(argc, cmd_argv);
8292 close_err = got_repo_close(repo);
8297 got_object_commit_close(commit);
8299 got_worktree_close(worktree);
8301 const struct got_error *pack_err =
8302 got_repo_pack_fds_close(pack_fds);
8307 free(commit_id_str);
8314 for (i = 0; i < argc; i++)
8323 main(int argc, char *argv[])
8325 const struct got_error *error = NULL;
8326 const struct tog_cmd *cmd = NULL;
8327 int ch, hflag = 0, Vflag = 0;
8328 char **cmd_argv = NULL;
8329 static const struct option longopts[] = {
8330 { "version", no_argument, NULL, 'V' },
8333 char *diff_algo_str = NULL;
8335 setlocale(LC_CTYPE, "");
8337 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8357 got_version_print_str();
8362 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8370 /* Build an argument vector which runs a default command. */
8371 cmd = &tog_commands[0];
8373 cmd_argv = make_argv(argc, cmd->name);
8377 /* Did the user specify a command? */
8378 for (i = 0; i < nitems(tog_commands); i++) {
8379 if (strncmp(tog_commands[i].name, argv[0],
8380 strlen(argv[0])) == 0) {
8381 cmd = &tog_commands[i];
8387 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8388 if (diff_algo_str) {
8389 if (strcasecmp(diff_algo_str, "patience") == 0)
8390 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8391 if (strcasecmp(diff_algo_str, "myers") == 0)
8392 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8398 /* No command specified; try log with a path */
8399 error = tog_log_with_path(argc, argv);
8404 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8411 for (i = 0; i < argc; i++)
8416 if (error && error->code != GOT_ERR_CANCELLED)
8417 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);