2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
123 STAILQ_ENTRY(tog_color) entry;
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
146 } else if (isbackup1 && !isbackup2) {
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
210 STAILQ_INSERT_HEAD(colors, tc, entry);
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
227 static struct tog_color *
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
287 if (strcasecmp(val, "red") == 0)
289 if (strcasecmp(val, "green") == 0)
291 if (strcasecmp(val, "yellow") == 0)
293 if (strcasecmp(val, "blue") == 0)
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
299 if (strcasecmp(val, "white") == 0)
301 if (strcasecmp(val, "default") == 0)
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
313 int first_displayed_line;
314 int last_displayed_line;
317 int ignore_whitespace;
319 struct got_repository *repo;
320 struct tog_colors colors;
326 /* passed from log view; may be NULL */
327 struct tog_view *log_view;
330 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
332 struct tog_log_thread_args {
333 pthread_cond_t need_commits;
334 pthread_cond_t commit_loaded;
337 struct got_commit_graph *graph;
338 struct commit_queue *commits;
339 const char *in_repo_path;
340 struct got_object_id *start_id;
341 struct got_repository *repo;
345 struct commit_queue_entry **first_displayed_entry;
346 struct commit_queue_entry **selected_entry;
348 int *search_next_done;
352 struct tog_log_view_state {
353 struct commit_queue commits;
354 struct commit_queue_entry *first_displayed_entry;
355 struct commit_queue_entry *last_displayed_entry;
356 struct commit_queue_entry *selected_entry;
361 struct got_repository *repo;
362 struct got_object_id *start_id;
365 struct tog_log_thread_args thread_args;
366 struct commit_queue_entry *matched_entry;
367 struct commit_queue_entry *search_entry;
368 struct tog_colors colors;
371 #define TOG_COLOR_DIFF_MINUS 1
372 #define TOG_COLOR_DIFF_PLUS 2
373 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
374 #define TOG_COLOR_DIFF_META 4
375 #define TOG_COLOR_TREE_SUBMODULE 5
376 #define TOG_COLOR_TREE_SYMLINK 6
377 #define TOG_COLOR_TREE_DIRECTORY 7
378 #define TOG_COLOR_TREE_EXECUTABLE 8
379 #define TOG_COLOR_COMMIT 9
380 #define TOG_COLOR_AUTHOR 10
381 #define TOG_COLOR_DATE 11
382 #define TOG_COLOR_REFS_HEADS 12
383 #define TOG_COLOR_REFS_TAGS 13
384 #define TOG_COLOR_REFS_REMOTES 14
385 #define TOG_COLOR_REFS_BACKUP 15
387 struct tog_blame_cb_args {
388 struct tog_blame_line *lines; /* one per line */
391 struct tog_view *view;
392 struct got_object_id *commit_id;
396 struct tog_blame_thread_args {
398 struct got_repository *repo;
399 struct tog_blame_cb_args *cb_args;
401 got_cancel_cb cancel_cb;
408 struct tog_blame_line *lines;
412 struct tog_blame_thread_args thread_args;
413 struct tog_blame_cb_args cb_args;
418 struct tog_blame_view_state {
419 int first_displayed_line;
420 int last_displayed_line;
425 struct got_object_id_queue blamed_commits;
426 struct got_object_qid *blamed_commit;
428 struct got_repository *repo;
429 struct got_object_id *commit_id;
430 struct tog_blame blame;
432 struct tog_colors colors;
435 struct tog_parent_tree {
436 TAILQ_ENTRY(tog_parent_tree) entry;
437 struct got_tree_object *tree;
438 struct got_tree_entry *first_displayed_entry;
439 struct got_tree_entry *selected_entry;
443 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
445 struct tog_tree_view_state {
447 struct got_object_id *commit_id;/* commit which this tree belongs to */
448 struct got_tree_object *root; /* the commit's root tree entry */
449 struct got_tree_object *tree; /* currently displayed (sub-)tree */
450 struct got_tree_entry *first_displayed_entry;
451 struct got_tree_entry *last_displayed_entry;
452 struct got_tree_entry *selected_entry;
453 int ndisplayed, selected, show_ids;
454 struct tog_parent_trees parents; /* parent trees of current sub-tree */
456 struct got_repository *repo;
457 struct got_tree_entry *matched_entry;
458 struct tog_colors colors;
461 struct tog_reflist_entry {
462 TAILQ_ENTRY(tog_reflist_entry) entry;
463 struct got_reference *ref;
467 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
469 struct tog_ref_view_state {
470 struct tog_reflist_head refs;
471 struct tog_reflist_entry *first_displayed_entry;
472 struct tog_reflist_entry *last_displayed_entry;
473 struct tog_reflist_entry *selected_entry;
474 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
475 struct got_repository *repo;
476 struct tog_reflist_entry *matched_entry;
477 struct tog_colors colors;
481 * We implement two types of views: parent views and child views.
483 * The 'Tab' key switches focus between a parent view and its child view.
484 * Child views are shown side-by-side to their parent view, provided
485 * there is enough screen estate.
487 * When a new view is opened from within a parent view, this new view
488 * becomes a child view of the parent view, replacing any existing child.
490 * When a new view is opened from within a child view, this new view
491 * becomes a parent view which will obscure the views below until the
492 * user quits the new parent view by typing 'q'.
494 * This list of views contains parent views only.
495 * Child views are only pointed to by their parent view.
497 TAILQ_HEAD(tog_view_list_head, tog_view);
500 TAILQ_ENTRY(tog_view) entry;
503 int nlines, ncols, begin_y, begin_x;
504 int maxx, x; /* max column and current start column */
505 int lines, cols; /* copies of LINES and COLS */
506 int ch, count; /* current keymap and count prefix */
507 int focussed; /* Only set on one parent or child view at a time. */
509 struct tog_view *parent;
510 struct tog_view *child;
513 * This flag is initially set on parent views when a new child view
514 * is created. It gets toggled when the 'Tab' key switches focus
515 * between parent and child.
516 * The flag indicates whether focus should be passed on to our child
517 * view if this parent view gets picked for focus after another parent
518 * view was closed. This prevents child views from losing focus in such
523 /* type-specific state */
524 enum tog_view_type type;
526 struct tog_diff_view_state diff;
527 struct tog_log_view_state log;
528 struct tog_blame_view_state blame;
529 struct tog_tree_view_state tree;
530 struct tog_ref_view_state ref;
533 const struct got_error *(*show)(struct tog_view *);
534 const struct got_error *(*input)(struct tog_view **,
535 struct tog_view *, int);
536 const struct got_error *(*close)(struct tog_view *);
538 const struct got_error *(*search_start)(struct tog_view *);
539 const struct got_error *(*search_next)(struct tog_view *);
542 #define TOG_SEARCH_FORWARD 1
543 #define TOG_SEARCH_BACKWARD 2
544 int search_next_done;
545 #define TOG_SEARCH_HAVE_MORE 1
546 #define TOG_SEARCH_NO_MORE 2
547 #define TOG_SEARCH_HAVE_NONE 3
552 static const struct got_error *open_diff_view(struct tog_view *,
553 struct got_object_id *, struct got_object_id *,
554 const char *, const char *, int, int, int, struct tog_view *,
555 struct got_repository *);
556 static const struct got_error *show_diff_view(struct tog_view *);
557 static const struct got_error *input_diff_view(struct tog_view **,
558 struct tog_view *, int);
559 static const struct got_error* close_diff_view(struct tog_view *);
560 static const struct got_error *search_start_diff_view(struct tog_view *);
561 static const struct got_error *search_next_diff_view(struct tog_view *);
563 static const struct got_error *open_log_view(struct tog_view *,
564 struct got_object_id *, struct got_repository *,
565 const char *, const char *, int);
566 static const struct got_error * show_log_view(struct tog_view *);
567 static const struct got_error *input_log_view(struct tog_view **,
568 struct tog_view *, int);
569 static const struct got_error *close_log_view(struct tog_view *);
570 static const struct got_error *search_start_log_view(struct tog_view *);
571 static const struct got_error *search_next_log_view(struct tog_view *);
573 static const struct got_error *open_blame_view(struct tog_view *, char *,
574 struct got_object_id *, struct got_repository *);
575 static const struct got_error *show_blame_view(struct tog_view *);
576 static const struct got_error *input_blame_view(struct tog_view **,
577 struct tog_view *, int);
578 static const struct got_error *close_blame_view(struct tog_view *);
579 static const struct got_error *search_start_blame_view(struct tog_view *);
580 static const struct got_error *search_next_blame_view(struct tog_view *);
582 static const struct got_error *open_tree_view(struct tog_view *,
583 struct got_object_id *, const char *, struct got_repository *);
584 static const struct got_error *show_tree_view(struct tog_view *);
585 static const struct got_error *input_tree_view(struct tog_view **,
586 struct tog_view *, int);
587 static const struct got_error *close_tree_view(struct tog_view *);
588 static const struct got_error *search_start_tree_view(struct tog_view *);
589 static const struct got_error *search_next_tree_view(struct tog_view *);
591 static const struct got_error *open_ref_view(struct tog_view *,
592 struct got_repository *);
593 static const struct got_error *show_ref_view(struct tog_view *);
594 static const struct got_error *input_ref_view(struct tog_view **,
595 struct tog_view *, int);
596 static const struct got_error *close_ref_view(struct tog_view *);
597 static const struct got_error *search_start_ref_view(struct tog_view *);
598 static const struct got_error *search_next_ref_view(struct tog_view *);
600 static volatile sig_atomic_t tog_sigwinch_received;
601 static volatile sig_atomic_t tog_sigpipe_received;
602 static volatile sig_atomic_t tog_sigcont_received;
603 static volatile sig_atomic_t tog_sigint_received;
604 static volatile sig_atomic_t tog_sigterm_received;
607 tog_sigwinch(int signo)
609 tog_sigwinch_received = 1;
613 tog_sigpipe(int signo)
615 tog_sigpipe_received = 1;
619 tog_sigcont(int signo)
621 tog_sigcont_received = 1;
625 tog_sigint(int signo)
627 tog_sigint_received = 1;
631 tog_sigterm(int signo)
633 tog_sigterm_received = 1;
637 tog_fatal_signal_received(void)
639 return (tog_sigpipe_received ||
640 tog_sigint_received || tog_sigint_received);
644 static const struct got_error *
645 view_close(struct tog_view *view)
647 const struct got_error *err = NULL;
650 view_close(view->child);
654 err = view->close(view);
656 del_panel(view->panel);
658 delwin(view->window);
663 static struct tog_view *
664 view_open(int nlines, int ncols, int begin_y, int begin_x,
665 enum tog_view_type type)
667 struct tog_view *view = calloc(1, sizeof(*view));
677 view->nlines = nlines ? nlines : LINES - begin_y;
678 view->ncols = ncols ? ncols : COLS - begin_x;
679 view->begin_y = begin_y;
680 view->begin_x = begin_x;
681 view->window = newwin(nlines, ncols, begin_y, begin_x);
682 if (view->window == NULL) {
686 view->panel = new_panel(view->window);
687 if (view->panel == NULL ||
688 set_panel_userptr(view->panel, view) != OK) {
693 keypad(view->window, TRUE);
698 view_split_begin_x(int begin_x)
700 if (begin_x > 0 || COLS < 120)
702 return (COLS - MAX(COLS / 2, 80));
705 static const struct got_error *view_resize(struct tog_view *);
707 static const struct got_error *
708 view_splitscreen(struct tog_view *view)
710 const struct got_error *err = NULL;
713 view->begin_x = view_split_begin_x(0);
714 view->nlines = LINES;
715 view->ncols = COLS - view->begin_x;
718 err = view_resize(view);
722 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
723 return got_error_from_errno("mvwin");
728 static const struct got_error *
729 view_fullscreen(struct tog_view *view)
731 const struct got_error *err = NULL;
735 view->nlines = LINES;
739 err = view_resize(view);
743 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
744 return got_error_from_errno("mvwin");
750 view_is_parent_view(struct tog_view *view)
752 return view->parent == NULL;
756 view_is_splitscreen(struct tog_view *view)
758 return view->begin_x > 0;
762 static const struct got_error *
763 view_resize(struct tog_view *view)
767 if (view->lines > LINES)
768 nlines = view->nlines - (view->lines - LINES);
770 nlines = view->nlines + (LINES - view->lines);
772 if (view->cols > COLS)
773 ncols = view->ncols - (view->cols - COLS);
775 ncols = view->ncols + (COLS - view->cols);
777 if (view->child && view_is_splitscreen(view->child)) {
778 view->child->begin_x = view_split_begin_x(view->begin_x);
779 if (view->child->begin_x == 0) {
782 view_fullscreen(view->child);
783 if (view->child->focussed)
784 show_panel(view->child->panel);
786 show_panel(view->panel);
788 ncols = view->child->begin_x;
790 view_splitscreen(view->child);
791 show_panel(view->child->panel);
793 } else if (view->parent == NULL)
796 if (wresize(view->window, nlines, ncols) == ERR)
797 return got_error_from_errno("wresize");
798 if (replace_panel(view->panel, view->window) == ERR)
799 return got_error_from_errno("replace_panel");
800 wclear(view->window);
802 view->nlines = nlines;
810 static const struct got_error *
811 view_close_child(struct tog_view *view)
813 const struct got_error *err = NULL;
815 if (view->child == NULL)
818 err = view_close(view->child);
823 static const struct got_error *
824 view_set_child(struct tog_view *view, struct tog_view *child)
827 child->parent = view;
829 return view_resize(view);
838 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
839 cols = 80; /* Default */
845 resize_term(lines, cols);
848 static const struct got_error *
849 view_search_start(struct tog_view *view)
851 const struct got_error *err = NULL;
855 if (view->search_started) {
856 regfree(&view->regex);
858 memset(&view->regmatch, 0, sizeof(view->regmatch));
860 view->search_started = 0;
862 if (view->nlines < 1)
865 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
866 wclrtoeol(view->window);
870 ret = wgetnstr(view->window, pattern, sizeof(pattern));
876 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
877 err = view->search_start(view);
879 regfree(&view->regex);
882 view->search_started = 1;
883 view->searching = TOG_SEARCH_FORWARD;
884 view->search_next_done = 0;
885 view->search_next(view);
892 * Compute view->count from numeric user input. User has five-tenths of a
893 * second to follow each numeric keypress with another number to form count.
894 * Return first non-numeric input or ERR and assign total to view->count.
895 * XXX Should we add support for user-defined timeout?
898 get_compound_key(struct tog_view *view, int c)
903 halfdelay(5); /* block for half a second */
904 wattron(view->window, A_BOLD);
905 wmove(view->window, view->nlines - 1, 0);
906 wclrtoeol(view->window);
907 waddch(view->window, ':');
910 x = getcurx(view->window);
911 if (x != ERR && x < view->ncols)
912 waddch(view->window, c);
914 * Don't overflow. Max valid request should be the greatest
915 * between the longest and total lines; cap at 10 million.
920 n = n * 10 + (c - '0');
921 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
923 /* Massage excessive or inapplicable values at the input handler. */
926 wattroff(view->window, A_BOLD);
927 cbreak(); /* return to blocking */
931 static const struct got_error *
932 view_input(struct tog_view **new, int *done, struct tog_view *view,
933 struct tog_view_list_head *views)
935 const struct got_error *err = NULL;
941 /* Clear "no matches" indicator. */
942 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
943 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
944 view->search_next_done = TOG_SEARCH_HAVE_MORE;
948 if (view->searching && !view->search_next_done) {
949 errcode = pthread_mutex_unlock(&tog_mutex);
951 return got_error_set_errno(errcode,
952 "pthread_mutex_unlock");
954 errcode = pthread_mutex_lock(&tog_mutex);
956 return got_error_set_errno(errcode,
957 "pthread_mutex_lock");
958 view->search_next(view);
962 nodelay(stdscr, FALSE);
963 /* Allow threads to make progress while we are waiting for input. */
964 errcode = pthread_mutex_unlock(&tog_mutex);
966 return got_error_set_errno(errcode, "pthread_mutex_unlock");
967 /* If we have an unfinished count, don't get a new key map. */
969 if ((view->count && --view->count == 0) || !view->count) {
970 ch = wgetch(view->window);
971 if (ch >= '1' && ch <= '9')
972 view->ch = ch = get_compound_key(view, ch);
974 errcode = pthread_mutex_lock(&tog_mutex);
976 return got_error_set_errno(errcode, "pthread_mutex_lock");
977 nodelay(stdscr, TRUE);
979 if (tog_sigwinch_received || tog_sigcont_received) {
981 tog_sigwinch_received = 0;
982 tog_sigcont_received = 0;
983 TAILQ_FOREACH(v, views, entry) {
984 err = view_resize(v);
987 err = v->input(new, v, KEY_RESIZE);
991 err = view_resize(v->child);
994 err = v->child->input(new, v->child,
1007 view->child->focussed = 1;
1008 view->focus_child = 1;
1009 } else if (view->parent) {
1011 view->parent->focussed = 1;
1012 view->parent->focus_child = 0;
1013 if (!view_is_splitscreen(view))
1014 err = view_fullscreen(view->parent);
1018 err = view->input(new, view, ch);
1026 if (view_is_parent_view(view)) {
1027 if (view->child == NULL)
1029 if (view_is_splitscreen(view->child)) {
1031 view->child->focussed = 1;
1032 err = view_fullscreen(view->child);
1034 err = view_splitscreen(view->child);
1037 err = view->child->input(new, view->child,
1040 if (view_is_splitscreen(view)) {
1041 view->parent->focussed = 0;
1043 err = view_fullscreen(view);
1045 err = view_splitscreen(view);
1047 err = view_resize(view->parent);
1051 err = view->input(new, view, KEY_RESIZE);
1058 if (view->search_start)
1059 view_search_start(view);
1061 err = view->input(new, view, ch);
1065 if (view->search_started && view->search_next) {
1066 view->searching = (ch == 'n' ?
1067 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1068 view->search_next_done = 0;
1069 view->search_next(view);
1071 err = view->input(new, view, ch);
1074 err = view->input(new, view, ch);
1082 view_vborder(struct tog_view *view)
1085 const struct tog_view *view_above;
1088 return view_vborder(view->parent);
1090 panel = panel_above(view->panel);
1094 view_above = panel_userptr(panel);
1095 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1096 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1100 view_needs_focus_indication(struct tog_view *view)
1102 if (view_is_parent_view(view)) {
1103 if (view->child == NULL || view->child->focussed)
1105 if (!view_is_splitscreen(view->child))
1107 } else if (!view_is_splitscreen(view))
1110 return view->focussed;
1113 static const struct got_error *
1114 view_loop(struct tog_view *view)
1116 const struct got_error *err = NULL;
1117 struct tog_view_list_head views;
1118 struct tog_view *new_view;
1119 int fast_refresh = 10;
1120 int done = 0, errcode;
1122 errcode = pthread_mutex_lock(&tog_mutex);
1124 return got_error_set_errno(errcode, "pthread_mutex_lock");
1127 TAILQ_INSERT_HEAD(&views, view, entry);
1130 err = view->show(view);
1135 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1136 /* Refresh fast during initialization, then become slower. */
1137 if (fast_refresh && fast_refresh-- == 0)
1138 halfdelay(10); /* switch to once per second */
1140 err = view_input(&new_view, &done, view, &views);
1144 struct tog_view *v, *prev = NULL;
1146 if (view_is_parent_view(view))
1147 prev = TAILQ_PREV(view, tog_view_list_head,
1149 else if (view->parent)
1150 prev = view->parent;
1153 view->parent->child = NULL;
1154 view->parent->focus_child = 0;
1156 err = view_resize(view->parent);
1160 TAILQ_REMOVE(&views, view, entry);
1162 err = view_close(view);
1167 TAILQ_FOREACH(v, &views, entry) {
1171 if (view == NULL && new_view == NULL) {
1172 /* No view has focus. Try to pick one. */
1175 else if (!TAILQ_EMPTY(&views)) {
1176 view = TAILQ_LAST(&views,
1177 tog_view_list_head);
1180 if (view->focus_child) {
1181 view->child->focussed = 1;
1189 struct tog_view *v, *t;
1190 /* Only allow one parent view per type. */
1191 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1192 if (v->type != new_view->type)
1194 TAILQ_REMOVE(&views, v, entry);
1195 err = view_close(v);
1200 TAILQ_INSERT_TAIL(&views, new_view, entry);
1204 if (view_is_parent_view(view)) {
1205 if (view->child && view->child->focussed)
1208 if (view->parent && view->parent->focussed)
1209 view = view->parent;
1211 show_panel(view->panel);
1212 if (view->child && view_is_splitscreen(view->child))
1213 show_panel(view->child->panel);
1214 if (view->parent && view_is_splitscreen(view)) {
1215 err = view->parent->show(view->parent);
1219 err = view->show(view);
1223 err = view->child->show(view->child);
1232 while (!TAILQ_EMPTY(&views)) {
1233 view = TAILQ_FIRST(&views);
1234 TAILQ_REMOVE(&views, view, entry);
1238 errcode = pthread_mutex_unlock(&tog_mutex);
1239 if (errcode && err == NULL)
1240 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1250 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1255 /* Create newly allocated wide-character string equivalent to a byte string. */
1256 static const struct got_error *
1257 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1260 const struct got_error *err = NULL;
1263 *wlen = mbstowcs(NULL, s, 0);
1264 if (*wlen == (size_t)-1) {
1266 if (errno != EILSEQ)
1267 return got_error_from_errno("mbstowcs");
1269 /* byte string invalid in current encoding; try to "fix" it */
1270 err = got_mbsavis(&vis, &vislen, s);
1273 *wlen = mbstowcs(NULL, vis, 0);
1274 if (*wlen == (size_t)-1) {
1275 err = got_error_from_errno("mbstowcs"); /* give up */
1280 *ws = calloc(*wlen + 1, sizeof(**ws));
1282 err = got_error_from_errno("calloc");
1286 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1287 err = got_error_from_errno("mbstowcs");
1298 static const struct got_error *
1299 expand_tab(char **ptr, const char *src)
1302 size_t len, n, idx = 0, sz = 0;
1305 n = len = strlen(src);
1306 dst = malloc(n + 1);
1308 return got_error_from_errno("malloc");
1310 while (idx < len && src[idx]) {
1311 const char c = src[idx];
1314 size_t nb = TABSIZE - sz % TABSIZE;
1317 p = realloc(dst, n + nb);
1320 return got_error_from_errno("realloc");
1325 memset(dst + sz, ' ', nb);
1328 dst[sz++] = src[idx];
1338 * Advance at most n columns from wline starting at offset off.
1339 * Return the index to the first character after the span operation.
1340 * Return the combined column width of all spanned wide character in
1344 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1346 int width, i, cols = 0;
1353 for (i = off; wline[i] != L'\0'; ++i) {
1354 if (wline[i] == L'\t')
1355 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1357 width = wcwidth(wline[i]);
1364 if (cols + width > n)
1374 * Format a line for display, ensuring that it won't overflow a width limit.
1375 * With scrolling, the width returned refers to the scrolled version of the
1376 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1378 static const struct got_error *
1379 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1380 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1382 const struct got_error *err = NULL;
1384 wchar_t *wline = NULL;
1393 err = expand_tab(&exstr, line);
1398 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1403 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1405 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1406 wline[wlen - 1] = L'\0';
1409 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1410 wline[wlen - 1] = L'\0';
1414 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1420 *scrollxp = scrollx;
1428 static const struct got_error*
1429 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1430 struct got_object_id *id, struct got_repository *repo)
1432 static const struct got_error *err = NULL;
1433 struct got_reflist_entry *re;
1439 TAILQ_FOREACH(re, refs, entry) {
1440 struct got_tag_object *tag = NULL;
1441 struct got_object_id *ref_id;
1444 name = got_ref_get_name(re->ref);
1445 if (strcmp(name, GOT_REF_HEAD) == 0)
1447 if (strncmp(name, "refs/", 5) == 0)
1449 if (strncmp(name, "got/", 4) == 0 &&
1450 strncmp(name, "got/backup/", 11) != 0)
1452 if (strncmp(name, "heads/", 6) == 0)
1454 if (strncmp(name, "remotes/", 8) == 0) {
1456 s = strstr(name, "/" GOT_REF_HEAD);
1457 if (s != NULL && s[strlen(s)] == '\0')
1460 err = got_ref_resolve(&ref_id, repo, re->ref);
1463 if (strncmp(name, "tags/", 5) == 0) {
1464 err = got_object_open_as_tag(&tag, repo, ref_id);
1466 if (err->code != GOT_ERR_OBJ_TYPE) {
1470 /* Ref points at something other than a tag. */
1475 cmp = got_object_id_cmp(tag ?
1476 got_object_tag_get_object_id(tag) : ref_id, id);
1479 got_object_tag_close(tag);
1483 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1484 s ? ", " : "", name) == -1) {
1485 err = got_error_from_errno("asprintf");
1496 static const struct got_error *
1497 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1502 smallerthan = strchr(author, '<');
1503 if (smallerthan && smallerthan[1] != '\0')
1504 author = smallerthan + 1;
1505 author[strcspn(author, "@>")] = '\0';
1506 return format_line(wauthor, author_width, NULL, author, 0, limit,
1510 static const struct got_error *
1511 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1512 struct got_object_id *id, const size_t date_display_cols,
1513 int author_display_cols)
1515 struct tog_log_view_state *s = &view->state.log;
1516 const struct got_error *err = NULL;
1517 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1518 char *logmsg0 = NULL, *logmsg = NULL;
1519 char *author = NULL;
1520 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1521 int author_width, logmsg_width;
1522 char *newline, *line = NULL;
1523 int col, limit, scrollx;
1524 const int avail = view->ncols;
1526 time_t committer_time;
1527 struct tog_color *tc;
1529 committer_time = got_object_commit_get_committer_time(commit);
1530 if (gmtime_r(&committer_time, &tm) == NULL)
1531 return got_error_from_errno("gmtime_r");
1532 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1533 return got_error(GOT_ERR_NO_SPACE);
1535 if (avail <= date_display_cols)
1536 limit = MIN(sizeof(datebuf) - 1, avail);
1538 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1539 tc = get_color(&s->colors, TOG_COLOR_DATE);
1541 wattr_on(view->window,
1542 COLOR_PAIR(tc->colorpair), NULL);
1543 waddnstr(view->window, datebuf, limit);
1545 wattr_off(view->window,
1546 COLOR_PAIR(tc->colorpair), NULL);
1553 err = got_object_id_str(&id_str, id);
1556 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1558 wattr_on(view->window,
1559 COLOR_PAIR(tc->colorpair), NULL);
1560 wprintw(view->window, "%.8s ", id_str);
1562 wattr_off(view->window,
1563 COLOR_PAIR(tc->colorpair), NULL);
1570 author = strdup(got_object_commit_get_author(commit));
1571 if (author == NULL) {
1572 err = got_error_from_errno("strdup");
1575 err = format_author(&wauthor, &author_width, author, avail - col, col);
1578 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1580 wattr_on(view->window,
1581 COLOR_PAIR(tc->colorpair), NULL);
1582 waddwstr(view->window, wauthor);
1584 wattr_off(view->window,
1585 COLOR_PAIR(tc->colorpair), NULL);
1586 col += author_width;
1587 while (col < avail && author_width < author_display_cols + 2) {
1588 waddch(view->window, ' ');
1595 err = got_object_commit_get_logmsg(&logmsg0, commit);
1599 while (*logmsg == '\n')
1601 newline = strchr(logmsg, '\n');
1604 limit = avail - col;
1605 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1606 limit--; /* for the border */
1607 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1611 waddwstr(view->window, &wlogmsg[scrollx]);
1612 col += MAX(logmsg_width, 0);
1613 while (col < avail) {
1614 waddch(view->window, ' ');
1626 static struct commit_queue_entry *
1627 alloc_commit_queue_entry(struct got_commit_object *commit,
1628 struct got_object_id *id)
1630 struct commit_queue_entry *entry;
1632 entry = calloc(1, sizeof(*entry));
1637 entry->commit = commit;
1642 pop_commit(struct commit_queue *commits)
1644 struct commit_queue_entry *entry;
1646 entry = TAILQ_FIRST(&commits->head);
1647 TAILQ_REMOVE(&commits->head, entry, entry);
1648 got_object_commit_close(entry->commit);
1649 commits->ncommits--;
1650 /* Don't free entry->id! It is owned by the commit graph. */
1655 free_commits(struct commit_queue *commits)
1657 while (!TAILQ_EMPTY(&commits->head))
1658 pop_commit(commits);
1661 static const struct got_error *
1662 match_commit(int *have_match, struct got_object_id *id,
1663 struct got_commit_object *commit, regex_t *regex)
1665 const struct got_error *err = NULL;
1666 regmatch_t regmatch;
1667 char *id_str = NULL, *logmsg = NULL;
1671 err = got_object_id_str(&id_str, id);
1675 err = got_object_commit_get_logmsg(&logmsg, commit);
1679 if (regexec(regex, got_object_commit_get_author(commit), 1,
1680 ®match, 0) == 0 ||
1681 regexec(regex, got_object_commit_get_committer(commit), 1,
1682 ®match, 0) == 0 ||
1683 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1684 regexec(regex, logmsg, 1, ®match, 0) == 0)
1692 static const struct got_error *
1693 queue_commits(struct tog_log_thread_args *a)
1695 const struct got_error *err = NULL;
1698 * We keep all commits open throughout the lifetime of the log
1699 * view in order to avoid having to re-fetch commits from disk
1700 * while updating the display.
1703 struct got_object_id *id;
1704 struct got_commit_object *commit;
1705 struct commit_queue_entry *entry;
1708 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1710 if (err || id == NULL)
1713 err = got_object_open_as_commit(&commit, a->repo, id);
1716 entry = alloc_commit_queue_entry(commit, id);
1717 if (entry == NULL) {
1718 err = got_error_from_errno("alloc_commit_queue_entry");
1722 errcode = pthread_mutex_lock(&tog_mutex);
1724 err = got_error_set_errno(errcode,
1725 "pthread_mutex_lock");
1729 entry->idx = a->commits->ncommits;
1730 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1731 a->commits->ncommits++;
1733 if (*a->searching == TOG_SEARCH_FORWARD &&
1734 !*a->search_next_done) {
1736 err = match_commit(&have_match, id, commit, a->regex);
1740 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1743 errcode = pthread_mutex_unlock(&tog_mutex);
1744 if (errcode && err == NULL)
1745 err = got_error_set_errno(errcode,
1746 "pthread_mutex_unlock");
1749 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1755 select_commit(struct tog_log_view_state *s)
1757 struct commit_queue_entry *entry;
1760 entry = s->first_displayed_entry;
1762 if (ncommits == s->selected) {
1763 s->selected_entry = entry;
1766 entry = TAILQ_NEXT(entry, entry);
1771 static const struct got_error *
1772 draw_commits(struct tog_view *view)
1774 const struct got_error *err = NULL;
1775 struct tog_log_view_state *s = &view->state.log;
1776 struct commit_queue_entry *entry = s->selected_entry;
1777 const int limit = view->nlines;
1779 int ncommits, author_cols = 4;
1780 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1781 char *refs_str = NULL;
1783 struct tog_color *tc;
1784 static const size_t date_display_cols = 12;
1786 if (s->selected_entry &&
1787 !(view->searching && view->search_next_done == 0)) {
1788 struct got_reflist_head *refs;
1789 err = got_object_id_str(&id_str, s->selected_entry->id);
1792 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1793 s->selected_entry->id);
1795 err = build_refs_str(&refs_str, refs,
1796 s->selected_entry->id, s->repo);
1802 if (s->thread_args.commits_needed == 0)
1803 halfdelay(10); /* disable fast refresh */
1805 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1806 if (asprintf(&ncommits_str, " [%d/%d] %s",
1807 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1808 (view->searching && !view->search_next_done) ?
1809 "searching..." : "loading...") == -1) {
1810 err = got_error_from_errno("asprintf");
1814 const char *search_str = NULL;
1816 if (view->searching) {
1817 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1818 search_str = "no more matches";
1819 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1820 search_str = "no matches found";
1821 else if (!view->search_next_done)
1822 search_str = "searching...";
1825 if (asprintf(&ncommits_str, " [%d/%d] %s",
1826 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1827 search_str ? search_str :
1828 (refs_str ? refs_str : "")) == -1) {
1829 err = got_error_from_errno("asprintf");
1834 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1835 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1836 "........................................",
1837 s->in_repo_path, ncommits_str) == -1) {
1838 err = got_error_from_errno("asprintf");
1842 } else if (asprintf(&header, "commit %s%s",
1843 id_str ? id_str : "........................................",
1844 ncommits_str) == -1) {
1845 err = got_error_from_errno("asprintf");
1849 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1853 werase(view->window);
1855 if (view_needs_focus_indication(view))
1856 wstandout(view->window);
1857 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1859 wattr_on(view->window,
1860 COLOR_PAIR(tc->colorpair), NULL);
1861 waddwstr(view->window, wline);
1863 wattr_off(view->window,
1864 COLOR_PAIR(tc->colorpair), NULL);
1865 while (width < view->ncols) {
1866 waddch(view->window, ' ');
1869 if (view_needs_focus_indication(view))
1870 wstandend(view->window);
1875 /* Grow author column size if necessary, and set view->maxx. */
1876 entry = s->first_displayed_entry;
1880 char *author, *eol, *msg, *msg0;
1881 wchar_t *wauthor, *wmsg;
1883 if (ncommits >= limit - 1)
1885 author = strdup(got_object_commit_get_author(entry->commit));
1886 if (author == NULL) {
1887 err = got_error_from_errno("strdup");
1890 err = format_author(&wauthor, &width, author, COLS,
1892 if (author_cols < width)
1893 author_cols = width;
1896 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1900 while (*msg == '\n')
1902 if ((eol = strchr(msg, '\n')))
1904 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1905 date_display_cols + author_cols, 0);
1908 view->maxx = MAX(view->maxx, width);
1912 entry = TAILQ_NEXT(entry, entry);
1915 entry = s->first_displayed_entry;
1916 s->last_displayed_entry = s->first_displayed_entry;
1919 if (ncommits >= limit - 1)
1921 if (ncommits == s->selected)
1922 wstandout(view->window);
1923 err = draw_commit(view, entry->commit, entry->id,
1924 date_display_cols, author_cols);
1925 if (ncommits == s->selected)
1926 wstandend(view->window);
1930 s->last_displayed_entry = entry;
1931 entry = TAILQ_NEXT(entry, entry);
1944 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1946 struct commit_queue_entry *entry;
1949 entry = TAILQ_FIRST(&s->commits.head);
1950 if (s->first_displayed_entry == entry)
1953 entry = s->first_displayed_entry;
1954 while (entry && nscrolled < maxscroll) {
1955 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1957 s->first_displayed_entry = entry;
1963 static const struct got_error *
1964 trigger_log_thread(struct tog_view *view, int wait)
1966 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1969 halfdelay(1); /* fast refresh while loading commits */
1971 while (ta->commits_needed > 0 || ta->load_all) {
1972 if (ta->log_complete)
1975 /* Wake the log thread. */
1976 errcode = pthread_cond_signal(&ta->need_commits);
1978 return got_error_set_errno(errcode,
1979 "pthread_cond_signal");
1982 * The mutex will be released while the view loop waits
1983 * in wgetch(), at which time the log thread will run.
1988 /* Display progress update in log view. */
1989 show_log_view(view);
1993 /* Wait right here while next commit is being loaded. */
1994 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1996 return got_error_set_errno(errcode,
1997 "pthread_cond_wait");
1999 /* Display progress update in log view. */
2000 show_log_view(view);
2008 static const struct got_error *
2009 log_scroll_down(struct tog_view *view, int maxscroll)
2011 struct tog_log_view_state *s = &view->state.log;
2012 const struct got_error *err = NULL;
2013 struct commit_queue_entry *pentry;
2014 int nscrolled = 0, ncommits_needed;
2016 if (s->last_displayed_entry == NULL)
2019 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2020 if (s->commits.ncommits < ncommits_needed &&
2021 !s->thread_args.log_complete) {
2023 * Ask the log thread for required amount of commits.
2025 s->thread_args.commits_needed += maxscroll;
2026 err = trigger_log_thread(view, 1);
2032 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2036 s->last_displayed_entry = pentry;
2038 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2041 s->first_displayed_entry = pentry;
2042 } while (++nscrolled < maxscroll);
2047 static const struct got_error *
2048 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2049 struct got_commit_object *commit, struct got_object_id *commit_id,
2050 struct tog_view *log_view, struct got_repository *repo)
2052 const struct got_error *err;
2053 struct got_object_qid *parent_id;
2054 struct tog_view *diff_view;
2056 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2057 if (diff_view == NULL)
2058 return got_error_from_errno("view_open");
2060 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2061 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2062 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2064 *new_view = diff_view;
2068 static const struct got_error *
2069 tree_view_visit_subtree(struct tog_tree_view_state *s,
2070 struct got_tree_object *subtree)
2072 struct tog_parent_tree *parent;
2074 parent = calloc(1, sizeof(*parent));
2076 return got_error_from_errno("calloc");
2078 parent->tree = s->tree;
2079 parent->first_displayed_entry = s->first_displayed_entry;
2080 parent->selected_entry = s->selected_entry;
2081 parent->selected = s->selected;
2082 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2085 s->first_displayed_entry = NULL;
2089 static const struct got_error *
2090 tree_view_walk_path(struct tog_tree_view_state *s,
2091 struct got_commit_object *commit, const char *path)
2093 const struct got_error *err = NULL;
2094 struct got_tree_object *tree = NULL;
2096 char *slash, *subpath = NULL;
2098 /* Walk the path and open corresponding tree objects. */
2101 struct got_tree_entry *te;
2102 struct got_object_id *tree_id;
2108 /* Ensure the correct subtree entry is selected. */
2109 slash = strchr(p, '/');
2111 te_name = strdup(p);
2113 te_name = strndup(p, slash - p);
2114 if (te_name == NULL) {
2115 err = got_error_from_errno("strndup");
2118 te = got_object_tree_find_entry(s->tree, te_name);
2120 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2125 s->first_displayed_entry = s->selected_entry = te;
2127 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2128 break; /* jump to this file's entry */
2130 slash = strchr(p, '/');
2132 subpath = strndup(path, slash - path);
2134 subpath = strdup(path);
2135 if (subpath == NULL) {
2136 err = got_error_from_errno("strdup");
2140 err = got_object_id_by_path(&tree_id, s->repo, commit,
2145 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2150 err = tree_view_visit_subtree(s, tree);
2152 got_object_tree_close(tree);
2166 static const struct got_error *
2167 browse_commit_tree(struct tog_view **new_view, int begin_x,
2168 struct commit_queue_entry *entry, const char *path,
2169 const char *head_ref_name, struct got_repository *repo)
2171 const struct got_error *err = NULL;
2172 struct tog_tree_view_state *s;
2173 struct tog_view *tree_view;
2175 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2176 if (tree_view == NULL)
2177 return got_error_from_errno("view_open");
2179 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2182 s = &tree_view->state.tree;
2184 *new_view = tree_view;
2186 if (got_path_is_root_dir(path))
2189 return tree_view_walk_path(s, entry->commit, path);
2192 static const struct got_error *
2193 block_signals_used_by_main_thread(void)
2198 if (sigemptyset(&sigset) == -1)
2199 return got_error_from_errno("sigemptyset");
2201 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2202 if (sigaddset(&sigset, SIGWINCH) == -1)
2203 return got_error_from_errno("sigaddset");
2204 if (sigaddset(&sigset, SIGCONT) == -1)
2205 return got_error_from_errno("sigaddset");
2206 if (sigaddset(&sigset, SIGINT) == -1)
2207 return got_error_from_errno("sigaddset");
2208 if (sigaddset(&sigset, SIGTERM) == -1)
2209 return got_error_from_errno("sigaddset");
2211 /* ncurses handles SIGTSTP */
2212 if (sigaddset(&sigset, SIGTSTP) == -1)
2213 return got_error_from_errno("sigaddset");
2215 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2217 return got_error_set_errno(errcode, "pthread_sigmask");
2223 log_thread(void *arg)
2225 const struct got_error *err = NULL;
2227 struct tog_log_thread_args *a = arg;
2230 err = block_signals_used_by_main_thread();
2234 while (!done && !err && !tog_fatal_signal_received()) {
2235 err = queue_commits(a);
2237 if (err->code != GOT_ERR_ITER_COMPLETED)
2241 } else if (a->commits_needed > 0 && !a->load_all)
2242 a->commits_needed--;
2244 errcode = pthread_mutex_lock(&tog_mutex);
2246 err = got_error_set_errno(errcode,
2247 "pthread_mutex_lock");
2249 } else if (*a->quit)
2251 else if (*a->first_displayed_entry == NULL) {
2252 *a->first_displayed_entry =
2253 TAILQ_FIRST(&a->commits->head);
2254 *a->selected_entry = *a->first_displayed_entry;
2257 errcode = pthread_cond_signal(&a->commit_loaded);
2259 err = got_error_set_errno(errcode,
2260 "pthread_cond_signal");
2261 pthread_mutex_unlock(&tog_mutex);
2266 a->commits_needed = 0;
2268 if (a->commits_needed == 0 && !a->load_all) {
2269 errcode = pthread_cond_wait(&a->need_commits,
2272 err = got_error_set_errno(errcode,
2273 "pthread_cond_wait");
2279 errcode = pthread_mutex_unlock(&tog_mutex);
2280 if (errcode && err == NULL)
2281 err = got_error_set_errno(errcode,
2282 "pthread_mutex_unlock");
2284 a->log_complete = 1;
2288 static const struct got_error *
2289 stop_log_thread(struct tog_log_view_state *s)
2291 const struct got_error *err = NULL;
2296 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2298 return got_error_set_errno(errcode,
2299 "pthread_cond_signal");
2300 errcode = pthread_mutex_unlock(&tog_mutex);
2302 return got_error_set_errno(errcode,
2303 "pthread_mutex_unlock");
2304 errcode = pthread_join(s->thread, (void **)&err);
2306 return got_error_set_errno(errcode, "pthread_join");
2307 errcode = pthread_mutex_lock(&tog_mutex);
2309 return got_error_set_errno(errcode,
2310 "pthread_mutex_lock");
2314 if (s->thread_args.repo) {
2315 err = got_repo_close(s->thread_args.repo);
2316 s->thread_args.repo = NULL;
2319 if (s->thread_args.pack_fds) {
2320 const struct got_error *pack_err =
2321 got_repo_pack_fds_close(s->thread_args.pack_fds);
2324 s->thread_args.pack_fds = NULL;
2327 if (s->thread_args.graph) {
2328 got_commit_graph_close(s->thread_args.graph);
2329 s->thread_args.graph = NULL;
2335 static const struct got_error *
2336 close_log_view(struct tog_view *view)
2338 const struct got_error *err = NULL;
2339 struct tog_log_view_state *s = &view->state.log;
2342 err = stop_log_thread(s);
2344 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2345 if (errcode && err == NULL)
2346 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2348 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2349 if (errcode && err == NULL)
2350 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2352 free_commits(&s->commits);
2353 free(s->in_repo_path);
2354 s->in_repo_path = NULL;
2357 free(s->head_ref_name);
2358 s->head_ref_name = NULL;
2362 static const struct got_error *
2363 search_start_log_view(struct tog_view *view)
2365 struct tog_log_view_state *s = &view->state.log;
2367 s->matched_entry = NULL;
2368 s->search_entry = NULL;
2372 static const struct got_error *
2373 search_next_log_view(struct tog_view *view)
2375 const struct got_error *err = NULL;
2376 struct tog_log_view_state *s = &view->state.log;
2377 struct commit_queue_entry *entry;
2379 /* Display progress update in log view. */
2380 show_log_view(view);
2384 if (s->search_entry) {
2386 errcode = pthread_mutex_unlock(&tog_mutex);
2388 return got_error_set_errno(errcode,
2389 "pthread_mutex_unlock");
2390 ch = wgetch(view->window);
2391 errcode = pthread_mutex_lock(&tog_mutex);
2393 return got_error_set_errno(errcode,
2394 "pthread_mutex_lock");
2395 if (ch == KEY_BACKSPACE) {
2396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2399 if (view->searching == TOG_SEARCH_FORWARD)
2400 entry = TAILQ_NEXT(s->search_entry, entry);
2402 entry = TAILQ_PREV(s->search_entry,
2403 commit_queue_head, entry);
2404 } else if (s->matched_entry) {
2405 int matched_idx = s->matched_entry->idx;
2406 int selected_idx = s->selected_entry->idx;
2409 * If the user has moved the cursor after we hit a match,
2410 * the position from where we should continue searching
2411 * might have changed.
2413 if (view->searching == TOG_SEARCH_FORWARD) {
2414 if (matched_idx > selected_idx)
2415 entry = TAILQ_NEXT(s->selected_entry, entry);
2417 entry = TAILQ_NEXT(s->matched_entry, entry);
2419 if (matched_idx < selected_idx)
2420 entry = TAILQ_PREV(s->selected_entry,
2421 commit_queue_head, entry);
2423 entry = TAILQ_PREV(s->matched_entry,
2424 commit_queue_head, entry);
2427 entry = s->selected_entry;
2433 if (entry == NULL) {
2434 if (s->thread_args.log_complete ||
2435 view->searching == TOG_SEARCH_BACKWARD) {
2436 view->search_next_done =
2437 (s->matched_entry == NULL ?
2438 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2439 s->search_entry = NULL;
2443 * Poke the log thread for more commits and return,
2444 * allowing the main loop to make progress. Search
2445 * will resume at s->search_entry once we come back.
2447 s->thread_args.commits_needed++;
2448 return trigger_log_thread(view, 0);
2451 err = match_commit(&have_match, entry->id, entry->commit,
2456 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2457 s->matched_entry = entry;
2461 s->search_entry = entry;
2462 if (view->searching == TOG_SEARCH_FORWARD)
2463 entry = TAILQ_NEXT(entry, entry);
2465 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2468 if (s->matched_entry) {
2469 int cur = s->selected_entry->idx;
2470 while (cur < s->matched_entry->idx) {
2471 err = input_log_view(NULL, view, KEY_DOWN);
2476 while (cur > s->matched_entry->idx) {
2477 err = input_log_view(NULL, view, KEY_UP);
2484 s->search_entry = NULL;
2489 static const struct got_error *
2490 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2491 struct got_repository *repo, const char *head_ref_name,
2492 const char *in_repo_path, int log_branches)
2494 const struct got_error *err = NULL;
2495 struct tog_log_view_state *s = &view->state.log;
2496 struct got_repository *thread_repo = NULL;
2497 struct got_commit_graph *thread_graph = NULL;
2500 if (in_repo_path != s->in_repo_path) {
2501 free(s->in_repo_path);
2502 s->in_repo_path = strdup(in_repo_path);
2503 if (s->in_repo_path == NULL)
2504 return got_error_from_errno("strdup");
2507 /* The commit queue only contains commits being displayed. */
2508 TAILQ_INIT(&s->commits.head);
2509 s->commits.ncommits = 0;
2512 if (head_ref_name) {
2513 s->head_ref_name = strdup(head_ref_name);
2514 if (s->head_ref_name == NULL) {
2515 err = got_error_from_errno("strdup");
2519 s->start_id = got_object_id_dup(start_id);
2520 if (s->start_id == NULL) {
2521 err = got_error_from_errno("got_object_id_dup");
2524 s->log_branches = log_branches;
2526 STAILQ_INIT(&s->colors);
2527 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2528 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2529 get_color_value("TOG_COLOR_COMMIT"));
2532 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2533 get_color_value("TOG_COLOR_AUTHOR"));
2535 free_colors(&s->colors);
2538 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2539 get_color_value("TOG_COLOR_DATE"));
2541 free_colors(&s->colors);
2546 view->show = show_log_view;
2547 view->input = input_log_view;
2548 view->close = close_log_view;
2549 view->search_start = search_start_log_view;
2550 view->search_next = search_next_log_view;
2552 if (s->thread_args.pack_fds == NULL) {
2553 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2557 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2558 s->thread_args.pack_fds);
2561 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2565 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2566 s->repo, NULL, NULL);
2570 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2572 err = got_error_set_errno(errcode, "pthread_cond_init");
2575 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2577 err = got_error_set_errno(errcode, "pthread_cond_init");
2581 s->thread_args.commits_needed = view->nlines;
2582 s->thread_args.graph = thread_graph;
2583 s->thread_args.commits = &s->commits;
2584 s->thread_args.in_repo_path = s->in_repo_path;
2585 s->thread_args.start_id = s->start_id;
2586 s->thread_args.repo = thread_repo;
2587 s->thread_args.log_complete = 0;
2588 s->thread_args.quit = &s->quit;
2589 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2590 s->thread_args.selected_entry = &s->selected_entry;
2591 s->thread_args.searching = &view->searching;
2592 s->thread_args.search_next_done = &view->search_next_done;
2593 s->thread_args.regex = &view->regex;
2596 close_log_view(view);
2600 static const struct got_error *
2601 show_log_view(struct tog_view *view)
2603 const struct got_error *err;
2604 struct tog_log_view_state *s = &view->state.log;
2606 if (s->thread == NULL) {
2607 int errcode = pthread_create(&s->thread, NULL, log_thread,
2610 return got_error_set_errno(errcode, "pthread_create");
2611 if (s->thread_args.commits_needed > 0) {
2612 err = trigger_log_thread(view, 1);
2618 return draw_commits(view);
2621 static const struct got_error *
2622 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2624 const struct got_error *err = NULL;
2625 struct tog_log_view_state *s = &view->state.log;
2626 struct tog_view *diff_view = NULL, *tree_view = NULL;
2627 struct tog_view *ref_view = NULL;
2628 struct commit_queue_entry *entry;
2629 int begin_x = 0, n, nscroll = view->nlines - 1;
2631 if (s->thread_args.load_all) {
2632 if (ch == KEY_BACKSPACE)
2633 s->thread_args.load_all = 0;
2634 else if (s->thread_args.log_complete) {
2635 s->thread_args.load_all = 0;
2636 log_scroll_down(view, s->commits.ncommits);
2637 s->selected = MIN(view->nlines - 2,
2638 s->commits.ncommits - 1);
2652 view->x = MAX(view->maxx - view->ncols / 2, 0);
2657 if (view->x + view->ncols / 2 < view->maxx)
2658 view->x += 2; /* move two columns right */
2664 view->x -= MIN(view->x, 2); /* move two columns back */
2673 if (s->selected_entry->idx == 0)
2675 if (s->first_displayed_entry == NULL)
2677 if (s->selected > 0)
2680 log_scroll_up(s, 1);
2686 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2697 if (s->first_displayed_entry == NULL)
2699 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2700 s->selected = MAX(0, s->selected - nscroll - 1);
2702 log_scroll_up(s, nscroll);
2704 if (s->selected_entry->idx == 0)
2712 if (s->first_displayed_entry == NULL)
2714 if (s->selected < MIN(view->nlines - 2,
2715 s->commits.ncommits - 1))
2718 err = log_scroll_down(view, 1);
2723 if (s->thread_args.log_complete &&
2724 s->selected_entry->idx == s->commits.ncommits - 1)
2729 /* We don't know yet how many commits, so we're forced to
2730 * traverse them all. */
2732 if (!s->thread_args.log_complete) {
2733 s->thread_args.load_all = 1;
2734 return trigger_log_thread(view, 0);
2738 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2739 for (n = 0; n < view->nlines - 1; n++) {
2742 s->first_displayed_entry = entry;
2743 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2746 s->selected = n - 1;
2758 struct commit_queue_entry *first;
2759 first = s->first_displayed_entry;
2760 if (first == NULL) {
2764 err = log_scroll_down(view, nscroll);
2767 if (first == s->first_displayed_entry &&
2768 s->selected < MIN(view->nlines - 2,
2769 s->commits.ncommits - 1)) {
2770 /* can't scroll further down */
2771 s->selected += MIN(s->last_displayed_entry->idx -
2772 s->selected_entry->idx, nscroll + 1);
2775 if (s->thread_args.log_complete &&
2776 s->selected_entry->idx == s->commits.ncommits - 1)
2781 if (s->selected > view->nlines - 2)
2782 s->selected = view->nlines - 2;
2783 if (s->selected > s->commits.ncommits - 1)
2784 s->selected = s->commits.ncommits - 1;
2786 if (s->commits.ncommits < view->nlines - 1 &&
2787 !s->thread_args.log_complete) {
2788 s->thread_args.commits_needed += (view->nlines - 1) -
2789 s->commits.ncommits;
2790 err = trigger_log_thread(view, 1);
2796 if (s->selected_entry == NULL)
2798 if (view_is_parent_view(view))
2799 begin_x = view_split_begin_x(view->begin_x);
2800 err = open_diff_view_for_commit(&diff_view, begin_x,
2801 s->selected_entry->commit, s->selected_entry->id,
2806 diff_view->focussed = 1;
2807 if (view_is_parent_view(view)) {
2808 err = view_close_child(view);
2811 err = view_set_child(view, diff_view);
2814 view->focus_child = 1;
2816 *new_view = diff_view;
2820 if (s->selected_entry == NULL)
2822 if (view_is_parent_view(view))
2823 begin_x = view_split_begin_x(view->begin_x);
2824 err = browse_commit_tree(&tree_view, begin_x,
2825 s->selected_entry, s->in_repo_path, s->head_ref_name,
2830 tree_view->focussed = 1;
2831 if (view_is_parent_view(view)) {
2832 err = view_close_child(view);
2835 err = view_set_child(view, tree_view);
2838 view->focus_child = 1;
2840 *new_view = tree_view;
2846 if (ch == KEY_BACKSPACE &&
2847 got_path_is_root_dir(s->in_repo_path))
2849 err = stop_log_thread(s);
2852 if (ch == KEY_BACKSPACE) {
2854 err = got_path_dirname(&parent_path, s->in_repo_path);
2857 free(s->in_repo_path);
2858 s->in_repo_path = parent_path;
2859 s->thread_args.in_repo_path = s->in_repo_path;
2860 } else if (ch == CTRL('l')) {
2861 struct got_object_id *start_id;
2862 err = got_repo_match_object_id(&start_id, NULL,
2863 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2864 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2868 s->start_id = start_id;
2869 s->thread_args.start_id = s->start_id;
2871 s->log_branches = !s->log_branches;
2873 if (s->thread_args.pack_fds == NULL) {
2874 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2878 err = got_repo_open(&s->thread_args.repo,
2879 got_repo_get_path(s->repo), NULL,
2880 s->thread_args.pack_fds);
2884 err = tog_load_refs(s->repo, 0);
2887 err = got_commit_graph_open(&s->thread_args.graph,
2888 s->in_repo_path, !s->log_branches);
2891 err = got_commit_graph_iter_start(s->thread_args.graph,
2892 s->start_id, s->repo, NULL, NULL);
2895 free_commits(&s->commits);
2896 s->first_displayed_entry = NULL;
2897 s->last_displayed_entry = NULL;
2898 s->selected_entry = NULL;
2900 s->thread_args.log_complete = 0;
2902 s->thread_args.commits_needed = view->nlines;
2903 s->matched_entry = NULL;
2904 s->search_entry = NULL;
2908 if (view_is_parent_view(view))
2909 begin_x = view_split_begin_x(view->begin_x);
2910 ref_view = view_open(view->nlines, view->ncols,
2911 view->begin_y, begin_x, TOG_VIEW_REF);
2912 if (ref_view == NULL)
2913 return got_error_from_errno("view_open");
2914 err = open_ref_view(ref_view, s->repo);
2916 view_close(ref_view);
2920 ref_view->focussed = 1;
2921 if (view_is_parent_view(view)) {
2922 err = view_close_child(view);
2925 err = view_set_child(view, ref_view);
2928 view->focus_child = 1;
2930 *new_view = ref_view;
2940 static const struct got_error *
2941 apply_unveil(const char *repo_path, const char *worktree_path)
2943 const struct got_error *error;
2946 if (unveil("gmon.out", "rwc") != 0)
2947 return got_error_from_errno2("unveil", "gmon.out");
2949 if (repo_path && unveil(repo_path, "r") != 0)
2950 return got_error_from_errno2("unveil", repo_path);
2952 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2953 return got_error_from_errno2("unveil", worktree_path);
2955 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2956 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2958 error = got_privsep_unveil_exec_helpers();
2962 if (unveil(NULL, NULL) != 0)
2963 return got_error_from_errno("unveil");
2972 * Override default signal handlers before starting ncurses.
2973 * This should prevent ncurses from installing its own
2974 * broken cleanup() signal handler.
2976 signal(SIGWINCH, tog_sigwinch);
2977 signal(SIGPIPE, tog_sigpipe);
2978 signal(SIGCONT, tog_sigcont);
2979 signal(SIGINT, tog_sigint);
2980 signal(SIGTERM, tog_sigterm);
2984 halfdelay(1); /* Do fast refresh while initial view is loading. */
2987 intrflush(stdscr, FALSE);
2988 keypad(stdscr, TRUE);
2990 if (getenv("TOG_COLORS") != NULL) {
2992 use_default_colors();
2996 static const struct got_error *
2997 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2998 struct got_repository *repo, struct got_worktree *worktree)
3000 const struct got_error *err = NULL;
3003 *in_repo_path = strdup("/");
3004 if (*in_repo_path == NULL)
3005 return got_error_from_errno("strdup");
3010 const char *prefix = got_worktree_get_path_prefix(worktree);
3013 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3016 if (asprintf(in_repo_path, "%s%s%s", prefix,
3017 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3019 err = got_error_from_errno("asprintf");
3020 *in_repo_path = NULL;
3024 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3029 static const struct got_error *
3030 cmd_log(int argc, char *argv[])
3032 const struct got_error *error;
3033 struct got_repository *repo = NULL;
3034 struct got_worktree *worktree = NULL;
3035 struct got_object_id *start_id = NULL;
3036 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3037 char *start_commit = NULL, *label = NULL;
3038 struct got_reference *ref = NULL;
3039 const char *head_ref_name = NULL;
3040 int ch, log_branches = 0;
3041 struct tog_view *view;
3042 int *pack_fds = NULL;
3044 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3050 start_commit = optarg;
3053 repo_path = realpath(optarg, NULL);
3054 if (repo_path == NULL)
3055 return got_error_from_errno2("realpath",
3070 error = got_repo_pack_fds_open(&pack_fds);
3074 if (repo_path == NULL) {
3075 cwd = getcwd(NULL, 0);
3077 return got_error_from_errno("getcwd");
3078 error = got_worktree_open(&worktree, cwd);
3079 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3083 strdup(got_worktree_get_repo_path(worktree));
3085 repo_path = strdup(cwd);
3086 if (repo_path == NULL) {
3087 error = got_error_from_errno("strdup");
3092 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3096 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3103 error = apply_unveil(got_repo_get_path(repo),
3104 worktree ? got_worktree_get_root_path(worktree) : NULL);
3108 /* already loaded by tog_log_with_path()? */
3109 if (TAILQ_EMPTY(&tog_refs)) {
3110 error = tog_load_refs(repo, 0);
3115 if (start_commit == NULL) {
3116 error = got_repo_match_object_id(&start_id, &label,
3117 worktree ? got_worktree_get_head_ref_name(worktree) :
3118 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3121 head_ref_name = label;
3123 error = got_ref_open(&ref, repo, start_commit, 0);
3125 head_ref_name = got_ref_get_name(ref);
3126 else if (error->code != GOT_ERR_NOT_REF)
3128 error = got_repo_match_object_id(&start_id, NULL,
3129 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3134 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3136 error = got_error_from_errno("view_open");
3139 error = open_log_view(view, start_id, repo, head_ref_name,
3140 in_repo_path, log_branches);
3144 /* Release work tree lock. */
3145 got_worktree_close(worktree);
3148 error = view_loop(view);
3158 const struct got_error *close_err = got_repo_close(repo);
3163 got_worktree_close(worktree);
3165 const struct got_error *pack_err =
3166 got_repo_pack_fds_close(pack_fds);
3178 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3179 "[-w] object1 object2\n", getprogname());
3184 match_line(const char *line, regex_t *regex, size_t nmatch,
3185 regmatch_t *regmatch)
3187 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3190 static struct tog_color *
3191 match_color(struct tog_colors *colors, const char *line)
3193 struct tog_color *tc = NULL;
3195 STAILQ_FOREACH(tc, colors, entry) {
3196 if (match_line(line, &tc->regex, 0, NULL))
3203 static const struct got_error *
3204 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3205 WINDOW *window, int skipcol, regmatch_t *regmatch)
3207 const struct got_error *err = NULL;
3209 wchar_t *wline = NULL;
3210 int rme, rms, n, width, scrollx;
3211 int width0 = 0, width1 = 0, width2 = 0;
3212 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3216 rms = regmatch->rm_so;
3217 rme = regmatch->rm_eo;
3219 err = expand_tab(&exstr, line);
3223 /* Split the line into 3 segments, according to match offsets. */
3224 seg0 = strndup(exstr, rms);
3226 err = got_error_from_errno("strndup");
3229 seg1 = strndup(exstr + rms, rme - rms);
3231 err = got_error_from_errno("strndup");
3234 seg2 = strdup(exstr + rme);
3236 err = got_error_from_errno("strndup");
3240 /* draw up to matched token if we haven't scrolled past it */
3241 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3245 n = MAX(width0 - skipcol, 0);
3248 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3249 wlimit, col_tab_align, 1);
3252 waddwstr(window, &wline[scrollx]);
3262 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3266 wlen = wcslen(wline);
3268 width = wcwidth(wline[i]);
3270 /* should not happen, tabs are expanded */
3271 err = got_error(GOT_ERR_RANGE);
3274 if (width0 + w + width > skipcol)
3279 /* draw (visible part of) matched token (if scrolled into it) */
3280 if (width1 - w > 0) {
3281 wattron(window, A_STANDOUT);
3282 waddwstr(window, &wline[i]);
3283 wattroff(window, A_STANDOUT);
3284 wlimit -= (width1 - w);
3285 *wtotal += (width1 - w);
3289 if (wlimit > 0) { /* draw rest of line */
3291 if (skipcol > width0 + width1) {
3292 err = format_line(&wline, &width2, &scrollx, seg2,
3293 skipcol - (width0 + width1), wlimit,
3297 waddwstr(window, &wline[scrollx]);
3299 err = format_line(&wline, &width2, NULL, seg2, 0,
3300 wlimit, col_tab_align, 1);
3303 waddwstr(window, wline);
3316 static const struct got_error *
3317 draw_file(struct tog_view *view, const char *header)
3319 struct tog_diff_view_state *s = &view->state.diff;
3320 regmatch_t *regmatch = &view->regmatch;
3321 const struct got_error *err;
3324 size_t linesize = 0;
3326 struct tog_color *tc;
3329 int max_lines = view->nlines;
3330 int nlines = s->nlines;
3333 line_offset = s->line_offsets[s->first_displayed_line - 1];
3334 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3335 return got_error_from_errno("fseek");
3337 werase(view->window);
3340 if (asprintf(&line, "[%d/%d] %s",
3341 s->first_displayed_line - 1 + s->selected_line, nlines,
3343 return got_error_from_errno("asprintf");
3344 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3350 if (view_needs_focus_indication(view))
3351 wstandout(view->window);
3352 waddwstr(view->window, wline);
3355 if (view_needs_focus_indication(view))
3356 wstandend(view->window);
3357 if (width <= view->ncols - 1)
3358 waddch(view->window, '\n');
3368 while (max_lines > 0 && nprinted < max_lines) {
3369 linelen = getline(&line, &linesize, s->f);
3370 if (linelen == -1) {
3376 return got_ferror(s->f, GOT_ERR_IO);
3379 /* Set view->maxx based on full line length. */
3380 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3386 view->maxx = MAX(view->maxx, width);
3390 tc = match_color(&s->colors, line);
3392 wattr_on(view->window,
3393 COLOR_PAIR(tc->colorpair), NULL);
3394 if (s->first_displayed_line + nprinted == s->matched_line &&
3395 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3396 err = add_matched_line(&width, line, view->ncols, 0,
3397 view->window, view->x, regmatch);
3404 err = format_line(&wline, &width, &skip, line,
3405 view->x, view->ncols, 0, view->x ? 1 : 0);
3410 waddwstr(view->window, &wline[skip]);
3415 wattr_off(view->window,
3416 COLOR_PAIR(tc->colorpair), NULL);
3417 if (width <= view->ncols - 1)
3418 waddch(view->window, '\n');
3423 s->last_displayed_line = s->first_displayed_line +
3426 s->last_displayed_line = s->first_displayed_line;
3431 while (nprinted < view->nlines) {
3432 waddch(view->window, '\n');
3436 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3442 wstandout(view->window);
3443 waddwstr(view->window, wline);
3446 wstandend(view->window);
3453 get_datestr(time_t *time, char *datebuf)
3455 struct tm mytm, *tm;
3458 tm = gmtime_r(time, &mytm);
3461 s = asctime_r(tm, datebuf);
3464 p = strchr(s, '\n');
3470 static const struct got_error *
3471 get_changed_paths(struct got_pathlist_head *paths,
3472 struct got_commit_object *commit, struct got_repository *repo)
3474 const struct got_error *err = NULL;
3475 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3476 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3477 struct got_object_qid *qid;
3479 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3481 struct got_commit_object *pcommit;
3482 err = got_object_open_as_commit(&pcommit, repo,
3487 tree_id1 = got_object_id_dup(
3488 got_object_commit_get_tree_id(pcommit));
3489 if (tree_id1 == NULL) {
3490 got_object_commit_close(pcommit);
3491 return got_error_from_errno("got_object_id_dup");
3493 got_object_commit_close(pcommit);
3498 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3503 tree_id2 = got_object_commit_get_tree_id(commit);
3504 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3508 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3509 got_diff_tree_collect_changed_paths, paths, 0);
3512 got_object_tree_close(tree1);
3514 got_object_tree_close(tree2);
3519 static const struct got_error *
3520 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3524 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3526 return got_error_from_errno("reallocarray");
3528 (*line_offsets)[*nlines] = off;
3533 static const struct got_error *
3534 write_commit_info(off_t **line_offsets, size_t *nlines,
3535 struct got_object_id *commit_id, struct got_reflist_head *refs,
3536 struct got_repository *repo, FILE *outfile)
3538 const struct got_error *err = NULL;
3539 char datebuf[26], *datestr;
3540 struct got_commit_object *commit;
3541 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3542 time_t committer_time;
3543 const char *author, *committer;
3544 char *refs_str = NULL;
3545 struct got_pathlist_head changed_paths;
3546 struct got_pathlist_entry *pe;
3550 TAILQ_INIT(&changed_paths);
3553 err = build_refs_str(&refs_str, refs, commit_id, repo);
3558 err = got_object_open_as_commit(&commit, repo, commit_id);
3562 err = got_object_id_str(&id_str, commit_id);
3564 err = got_error_from_errno("got_object_id_str");
3568 err = add_line_offset(line_offsets, nlines, 0);
3572 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3573 refs_str ? refs_str : "", refs_str ? ")" : "");
3575 err = got_error_from_errno("fprintf");
3579 err = add_line_offset(line_offsets, nlines, outoff);
3583 n = fprintf(outfile, "from: %s\n",
3584 got_object_commit_get_author(commit));
3586 err = got_error_from_errno("fprintf");
3590 err = add_line_offset(line_offsets, nlines, outoff);
3594 committer_time = got_object_commit_get_committer_time(commit);
3595 datestr = get_datestr(&committer_time, datebuf);
3597 n = fprintf(outfile, "date: %s UTC\n", datestr);
3599 err = got_error_from_errno("fprintf");
3603 err = add_line_offset(line_offsets, nlines, outoff);
3607 author = got_object_commit_get_author(commit);
3608 committer = got_object_commit_get_committer(commit);
3609 if (strcmp(author, committer) != 0) {
3610 n = fprintf(outfile, "via: %s\n", committer);
3612 err = got_error_from_errno("fprintf");
3616 err = add_line_offset(line_offsets, nlines, outoff);
3620 if (got_object_commit_get_nparents(commit) > 1) {
3621 const struct got_object_id_queue *parent_ids;
3622 struct got_object_qid *qid;
3624 parent_ids = got_object_commit_get_parent_ids(commit);
3625 STAILQ_FOREACH(qid, parent_ids, entry) {
3626 err = got_object_id_str(&id_str, &qid->id);
3629 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3631 err = got_error_from_errno("fprintf");
3635 err = add_line_offset(line_offsets, nlines, outoff);
3643 err = got_object_commit_get_logmsg(&logmsg, commit);
3647 while ((line = strsep(&s, "\n")) != NULL) {
3648 n = fprintf(outfile, "%s\n", line);
3650 err = got_error_from_errno("fprintf");
3654 err = add_line_offset(line_offsets, nlines, outoff);
3659 err = get_changed_paths(&changed_paths, commit, repo);
3662 TAILQ_FOREACH(pe, &changed_paths, entry) {
3663 struct got_diff_changed_path *cp = pe->data;
3664 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3666 err = got_error_from_errno("fprintf");
3670 err = add_line_offset(line_offsets, nlines, outoff);
3673 free((char *)pe->path);
3677 fputc('\n', outfile);
3679 err = add_line_offset(line_offsets, nlines, outoff);
3681 got_pathlist_free(&changed_paths);
3685 got_object_commit_close(commit);
3687 free(*line_offsets);
3688 *line_offsets = NULL;
3694 static const struct got_error *
3695 create_diff(struct tog_diff_view_state *s)
3697 const struct got_error *err = NULL;
3701 free(s->line_offsets);
3702 s->line_offsets = malloc(sizeof(off_t));
3703 if (s->line_offsets == NULL)
3704 return got_error_from_errno("malloc");
3709 err = got_error_from_errno("got_opentemp");
3712 if (s->f && fclose(s->f) == EOF) {
3713 err = got_error_from_errno("fclose");
3719 err = got_object_get_type(&obj_type, s->repo, s->id1);
3721 err = got_object_get_type(&obj_type, s->repo, s->id2);
3726 case GOT_OBJ_TYPE_BLOB:
3727 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3728 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3729 s->label1, s->label2, s->diff_context,
3730 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3732 case GOT_OBJ_TYPE_TREE:
3733 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3734 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3735 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3738 case GOT_OBJ_TYPE_COMMIT: {
3739 const struct got_object_id_queue *parent_ids;
3740 struct got_object_qid *pid;
3741 struct got_commit_object *commit2;
3742 struct got_reflist_head *refs;
3744 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3747 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3748 /* Show commit info if we're diffing to a parent/root commit. */
3749 if (s->id1 == NULL) {
3750 err = write_commit_info(&s->line_offsets, &s->nlines,
3751 s->id2, refs, s->repo, s->f);
3755 parent_ids = got_object_commit_get_parent_ids(commit2);
3756 STAILQ_FOREACH(pid, parent_ids, entry) {
3757 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3758 err = write_commit_info(
3759 &s->line_offsets, &s->nlines,
3760 s->id2, refs, s->repo, s->f);
3767 got_object_commit_close(commit2);
3769 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3770 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
3771 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3776 err = got_error(GOT_ERR_OBJ_TYPE);
3782 if (s->f && fflush(s->f) != 0 && err == NULL)
3783 err = got_error_from_errno("fflush");
3788 diff_view_indicate_progress(struct tog_view *view)
3790 mvwaddstr(view->window, 0, 0, "diffing...");
3795 static const struct got_error *
3796 search_start_diff_view(struct tog_view *view)
3798 struct tog_diff_view_state *s = &view->state.diff;
3800 s->matched_line = 0;
3804 static const struct got_error *
3805 search_next_diff_view(struct tog_view *view)
3807 struct tog_diff_view_state *s = &view->state.diff;
3808 const struct got_error *err = NULL;
3811 size_t linesize = 0;
3814 if (!view->searching) {
3815 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3819 if (s->matched_line) {
3820 if (view->searching == TOG_SEARCH_FORWARD)
3821 lineno = s->matched_line + 1;
3823 lineno = s->matched_line - 1;
3825 lineno = s->first_displayed_line;
3830 if (lineno <= 0 || lineno > s->nlines) {
3831 if (s->matched_line == 0) {
3832 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3836 if (view->searching == TOG_SEARCH_FORWARD)
3842 offset = s->line_offsets[lineno - 1];
3843 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3845 return got_error_from_errno("fseeko");
3847 linelen = getline(&line, &linesize, s->f);
3848 if (linelen != -1) {
3850 err = expand_tab(&exstr, line);
3853 if (match_line(exstr, &view->regex, 1,
3855 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3856 s->matched_line = lineno;
3862 if (view->searching == TOG_SEARCH_FORWARD)
3869 if (s->matched_line) {
3870 s->first_displayed_line = s->matched_line;
3871 s->selected_line = 1;
3877 static const struct got_error *
3878 close_diff_view(struct tog_view *view)
3880 const struct got_error *err = NULL;
3881 struct tog_diff_view_state *s = &view->state.diff;
3887 if (s->f && fclose(s->f) == EOF)
3888 err = got_error_from_errno("fclose");
3890 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
3891 err = got_error_from_errno("fclose");
3893 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
3894 err = got_error_from_errno("fclose");
3896 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
3897 err = got_error_from_errno("close");
3899 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
3900 err = got_error_from_errno("close");
3902 free_colors(&s->colors);
3903 free(s->line_offsets);
3904 s->line_offsets = NULL;
3909 static const struct got_error *
3910 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3911 struct got_object_id *id2, const char *label1, const char *label2,
3912 int diff_context, int ignore_whitespace, int force_text_diff,
3913 struct tog_view *log_view, struct got_repository *repo)
3915 const struct got_error *err;
3916 struct tog_diff_view_state *s = &view->state.diff;
3918 memset(s, 0, sizeof(*s));
3922 if (id1 != NULL && id2 != NULL) {
3924 err = got_object_get_type(&type1, repo, id1);
3927 err = got_object_get_type(&type2, repo, id2);
3932 return got_error(GOT_ERR_OBJ_TYPE);
3934 s->first_displayed_line = 1;
3935 s->last_displayed_line = view->nlines;
3936 s->selected_line = 1;
3944 s->id1 = got_object_id_dup(id1);
3946 return got_error_from_errno("got_object_id_dup");
3950 s->id2 = got_object_id_dup(id2);
3951 if (s->id2 == NULL) {
3952 err = got_error_from_errno("got_object_id_dup");
3956 s->f1 = got_opentemp();
3957 if (s->f1 == NULL) {
3958 err = got_error_from_errno("got_opentemp");
3962 s->f2 = got_opentemp();
3963 if (s->f2 == NULL) {
3964 err = got_error_from_errno("got_opentemp");
3968 s->fd1 = got_opentempfd();
3970 err = got_error_from_errno("got_opentempfd");
3974 s->fd2 = got_opentempfd();
3976 err = got_error_from_errno("got_opentempfd");
3980 s->first_displayed_line = 1;
3981 s->last_displayed_line = view->nlines;
3982 s->diff_context = diff_context;
3983 s->ignore_whitespace = ignore_whitespace;
3984 s->force_text_diff = force_text_diff;
3985 s->log_view = log_view;
3988 STAILQ_INIT(&s->colors);
3989 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3990 err = add_color(&s->colors,
3991 "^-", TOG_COLOR_DIFF_MINUS,
3992 get_color_value("TOG_COLOR_DIFF_MINUS"));
3995 err = add_color(&s->colors, "^\\+",
3996 TOG_COLOR_DIFF_PLUS,
3997 get_color_value("TOG_COLOR_DIFF_PLUS"));
4000 err = add_color(&s->colors,
4001 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4002 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4006 err = add_color(&s->colors,
4007 "^(commit [0-9a-f]|parent [0-9]|"
4008 "(blob|file|tree|commit) [-+] |"
4009 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4010 get_color_value("TOG_COLOR_DIFF_META"));
4014 err = add_color(&s->colors,
4015 "^(from|via): ", TOG_COLOR_AUTHOR,
4016 get_color_value("TOG_COLOR_AUTHOR"));
4020 err = add_color(&s->colors,
4021 "^date: ", TOG_COLOR_DATE,
4022 get_color_value("TOG_COLOR_DATE"));
4027 if (log_view && view_is_splitscreen(view))
4028 show_log_view(log_view); /* draw vborder */
4029 diff_view_indicate_progress(view);
4031 err = create_diff(s);
4033 view->show = show_diff_view;
4034 view->input = input_diff_view;
4035 view->close = close_diff_view;
4036 view->search_start = search_start_diff_view;
4037 view->search_next = search_next_diff_view;
4040 close_diff_view(view);
4044 static const struct got_error *
4045 show_diff_view(struct tog_view *view)
4047 const struct got_error *err;
4048 struct tog_diff_view_state *s = &view->state.diff;
4049 char *id_str1 = NULL, *id_str2, *header;
4050 const char *label1, *label2;
4053 err = got_object_id_str(&id_str1, s->id1);
4056 label1 = s->label1 ? : id_str1;
4058 label1 = "/dev/null";
4060 err = got_object_id_str(&id_str2, s->id2);
4063 label2 = s->label2 ? : id_str2;
4065 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4066 err = got_error_from_errno("asprintf");
4074 err = draw_file(view, header);
4079 static const struct got_error *
4080 set_selected_commit(struct tog_diff_view_state *s,
4081 struct commit_queue_entry *entry)
4083 const struct got_error *err;
4084 const struct got_object_id_queue *parent_ids;
4085 struct got_commit_object *selected_commit;
4086 struct got_object_qid *pid;
4089 s->id2 = got_object_id_dup(entry->id);
4091 return got_error_from_errno("got_object_id_dup");
4093 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4096 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4098 pid = STAILQ_FIRST(parent_ids);
4099 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4100 got_object_commit_close(selected_commit);
4104 static const struct got_error *
4105 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4107 const struct got_error *err = NULL;
4108 struct tog_diff_view_state *s = &view->state.diff;
4109 struct tog_log_view_state *ls;
4110 struct commit_queue_entry *old_selected_entry;
4112 size_t linesize = 0;
4114 int i, nscroll = view->nlines - 1;
4121 view->x = MAX(view->maxx - view->ncols / 3, 0);
4126 if (view->x + view->ncols / 3 < view->maxx)
4127 view->x += 2; /* move two columns right */
4133 view->x -= MIN(view->x, 2); /* move two columns back */
4140 s->force_text_diff = !s->force_text_diff;
4142 s->ignore_whitespace = !s->ignore_whitespace;
4143 wclear(view->window);
4144 s->first_displayed_line = 1;
4145 s->last_displayed_line = view->nlines;
4146 s->matched_line = 0;
4147 diff_view_indicate_progress(view);
4148 err = create_diff(s);
4153 s->first_displayed_line = 1;
4162 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4168 if (s->first_displayed_line > 1)
4169 s->first_displayed_line--;
4180 if (s->first_displayed_line == 1) {
4185 while (i++ < nscroll && s->first_displayed_line > 1)
4186 s->first_displayed_line--;
4192 s->first_displayed_line++;
4209 while (!s->eof && i++ < nscroll) {
4210 linelen = getline(&line, &linesize, s->f);
4211 s->first_displayed_line++;
4212 if (linelen == -1) {
4216 err = got_ferror(s->f, GOT_ERR_IO);
4223 if (s->diff_context > 0) {
4225 s->matched_line = 0;
4226 diff_view_indicate_progress(view);
4227 err = create_diff(s);
4228 if (s->first_displayed_line + view->nlines - 1 >
4230 s->first_displayed_line = 1;
4231 s->last_displayed_line = view->nlines;
4237 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4239 s->matched_line = 0;
4240 diff_view_indicate_progress(view);
4241 err = create_diff(s);
4247 if (s->log_view == NULL) {
4251 ls = &s->log_view->state.log;
4252 old_selected_entry = ls->selected_entry;
4254 /* view->count handled in input_log_view() */
4255 err = input_log_view(NULL, s->log_view, KEY_UP);
4259 if (old_selected_entry == ls->selected_entry)
4262 err = set_selected_commit(s, ls->selected_entry);
4266 s->first_displayed_line = 1;
4267 s->last_displayed_line = view->nlines;
4268 s->matched_line = 0;
4271 diff_view_indicate_progress(view);
4272 err = create_diff(s);
4276 if (s->log_view == NULL) {
4280 ls = &s->log_view->state.log;
4281 old_selected_entry = ls->selected_entry;
4283 /* view->count handled in input_log_view() */
4284 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4288 if (old_selected_entry == ls->selected_entry)
4291 err = set_selected_commit(s, ls->selected_entry);
4295 s->first_displayed_line = 1;
4296 s->last_displayed_line = view->nlines;
4297 s->matched_line = 0;
4300 diff_view_indicate_progress(view);
4301 err = create_diff(s);
4311 static const struct got_error *
4312 cmd_diff(int argc, char *argv[])
4314 const struct got_error *error = NULL;
4315 struct got_repository *repo = NULL;
4316 struct got_worktree *worktree = NULL;
4317 struct got_object_id *id1 = NULL, *id2 = NULL;
4318 char *repo_path = NULL, *cwd = NULL;
4319 char *id_str1 = NULL, *id_str2 = NULL;
4320 char *label1 = NULL, *label2 = NULL;
4321 int diff_context = 3, ignore_whitespace = 0;
4322 int ch, force_text_diff = 0;
4324 struct tog_view *view;
4325 int *pack_fds = NULL;
4327 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4330 force_text_diff = 1;
4333 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4336 errx(1, "number of context lines is %s: %s",
4340 repo_path = realpath(optarg, NULL);
4341 if (repo_path == NULL)
4342 return got_error_from_errno2("realpath",
4344 got_path_strip_trailing_slashes(repo_path);
4347 ignore_whitespace = 1;
4359 usage_diff(); /* TODO show local worktree changes */
4360 } else if (argc == 2) {
4366 error = got_repo_pack_fds_open(&pack_fds);
4370 if (repo_path == NULL) {
4371 cwd = getcwd(NULL, 0);
4373 return got_error_from_errno("getcwd");
4374 error = got_worktree_open(&worktree, cwd);
4375 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4379 strdup(got_worktree_get_repo_path(worktree));
4381 repo_path = strdup(cwd);
4382 if (repo_path == NULL) {
4383 error = got_error_from_errno("strdup");
4388 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4394 error = apply_unveil(got_repo_get_path(repo), NULL);
4398 error = tog_load_refs(repo, 0);
4402 error = got_repo_match_object_id(&id1, &label1, id_str1,
4403 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4407 error = got_repo_match_object_id(&id2, &label2, id_str2,
4408 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4412 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4414 error = got_error_from_errno("view_open");
4417 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4418 ignore_whitespace, force_text_diff, NULL, repo);
4421 error = view_loop(view);
4428 const struct got_error *close_err = got_repo_close(repo);
4433 got_worktree_close(worktree);
4435 const struct got_error *pack_err =
4436 got_repo_pack_fds_close(pack_fds);
4449 "usage: %s blame [-c commit] [-r repository-path] path\n",
4454 struct tog_blame_line {
4456 struct got_object_id *id;
4459 static const struct got_error *
4460 draw_blame(struct tog_view *view)
4462 struct tog_blame_view_state *s = &view->state.blame;
4463 struct tog_blame *blame = &s->blame;
4464 regmatch_t *regmatch = &view->regmatch;
4465 const struct got_error *err;
4466 int lineno = 0, nprinted = 0;
4468 size_t linesize = 0;
4472 struct tog_blame_line *blame_line;
4473 struct got_object_id *prev_id = NULL;
4475 struct tog_color *tc;
4477 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4482 werase(view->window);
4484 if (asprintf(&line, "commit %s", id_str) == -1) {
4485 err = got_error_from_errno("asprintf");
4490 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4495 if (view_needs_focus_indication(view))
4496 wstandout(view->window);
4497 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4499 wattr_on(view->window,
4500 COLOR_PAIR(tc->colorpair), NULL);
4501 waddwstr(view->window, wline);
4503 wattr_off(view->window,
4504 COLOR_PAIR(tc->colorpair), NULL);
4505 if (view_needs_focus_indication(view))
4506 wstandend(view->window);
4509 if (width < view->ncols - 1)
4510 waddch(view->window, '\n');
4512 if (asprintf(&line, "[%d/%d] %s%s",
4513 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4514 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4516 return got_error_from_errno("asprintf");
4519 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4524 waddwstr(view->window, wline);
4527 if (width < view->ncols - 1)
4528 waddch(view->window, '\n');
4532 while (nprinted < view->nlines - 2) {
4533 linelen = getline(&line, &linesize, blame->f);
4534 if (linelen == -1) {
4535 if (feof(blame->f)) {
4540 return got_ferror(blame->f, GOT_ERR_IO);
4542 if (++lineno < s->first_displayed_line)
4545 /* Set view->maxx based on full line length. */
4546 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4553 view->maxx = MAX(view->maxx, width);
4555 if (view->focussed && nprinted == s->selected_line - 1)
4556 wstandout(view->window);
4558 if (blame->nlines > 0) {
4559 blame_line = &blame->lines[lineno - 1];
4560 if (blame_line->annotated && prev_id &&
4561 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4563 nprinted == s->selected_line - 1)) {
4564 waddstr(view->window, " ");
4565 } else if (blame_line->annotated) {
4567 err = got_object_id_str(&id_str,
4573 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4575 wattr_on(view->window,
4576 COLOR_PAIR(tc->colorpair), NULL);
4577 wprintw(view->window, "%.8s", id_str);
4579 wattr_off(view->window,
4580 COLOR_PAIR(tc->colorpair), NULL);
4582 prev_id = blame_line->id;
4584 waddstr(view->window, "........");
4588 waddstr(view->window, "........");
4592 if (view->focussed && nprinted == s->selected_line - 1)
4593 wstandend(view->window);
4594 waddstr(view->window, " ");
4596 if (view->ncols <= 9) {
4598 } else if (s->first_displayed_line + nprinted ==
4600 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4601 err = add_matched_line(&width, line, view->ncols - 9, 9,
4602 view->window, view->x, regmatch);
4610 err = format_line(&wline, &width, &skip, line,
4611 view->x, view->ncols - 9, 9, 1);
4616 waddwstr(view->window, &wline[skip]);
4622 if (width <= view->ncols - 1)
4623 waddch(view->window, '\n');
4624 if (++nprinted == 1)
4625 s->first_displayed_line = lineno;
4628 s->last_displayed_line = lineno;
4635 static const struct got_error *
4636 blame_cb(void *arg, int nlines, int lineno,
4637 struct got_commit_object *commit, struct got_object_id *id)
4639 const struct got_error *err = NULL;
4640 struct tog_blame_cb_args *a = arg;
4641 struct tog_blame_line *line;
4644 if (nlines != a->nlines ||
4645 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4646 return got_error(GOT_ERR_RANGE);
4648 errcode = pthread_mutex_lock(&tog_mutex);
4650 return got_error_set_errno(errcode, "pthread_mutex_lock");
4652 if (*a->quit) { /* user has quit the blame view */
4653 err = got_error(GOT_ERR_ITER_COMPLETED);
4658 goto done; /* no change in this commit */
4660 line = &a->lines[lineno - 1];
4661 if (line->annotated)
4664 line->id = got_object_id_dup(id);
4665 if (line->id == NULL) {
4666 err = got_error_from_errno("got_object_id_dup");
4669 line->annotated = 1;
4671 errcode = pthread_mutex_unlock(&tog_mutex);
4673 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4678 blame_thread(void *arg)
4680 const struct got_error *err, *close_err;
4681 struct tog_blame_thread_args *ta = arg;
4682 struct tog_blame_cb_args *a = ta->cb_args;
4683 int errcode, fd = -1;
4685 fd = got_opentempfd();
4687 return (void *)got_error_from_errno("got_opentempfd");
4689 err = block_signals_used_by_main_thread();
4693 err = got_blame(ta->path, a->commit_id, ta->repo,
4694 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd);
4695 if (err && err->code == GOT_ERR_CANCELLED)
4698 errcode = pthread_mutex_lock(&tog_mutex);
4700 return (void *)got_error_set_errno(errcode,
4701 "pthread_mutex_lock");
4703 close_err = got_repo_close(ta->repo);
4709 errcode = pthread_mutex_unlock(&tog_mutex);
4710 if (errcode && err == NULL)
4711 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4713 if (fd != -1 && close(fd) == -1 && err == NULL)
4714 err = got_error_from_errno("close");
4719 static struct got_object_id *
4720 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4721 int first_displayed_line, int selected_line)
4723 struct tog_blame_line *line;
4728 line = &lines[first_displayed_line - 1 + selected_line - 1];
4729 if (!line->annotated)
4735 static const struct got_error *
4736 stop_blame(struct tog_blame *blame)
4738 const struct got_error *err = NULL;
4741 if (blame->thread) {
4743 errcode = pthread_mutex_unlock(&tog_mutex);
4745 return got_error_set_errno(errcode,
4746 "pthread_mutex_unlock");
4747 errcode = pthread_join(blame->thread, (void **)&err);
4749 return got_error_set_errno(errcode, "pthread_join");
4750 errcode = pthread_mutex_lock(&tog_mutex);
4752 return got_error_set_errno(errcode,
4753 "pthread_mutex_lock");
4754 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4756 blame->thread = NULL;
4758 if (blame->thread_args.repo) {
4759 const struct got_error *close_err;
4760 close_err = got_repo_close(blame->thread_args.repo);
4763 blame->thread_args.repo = NULL;
4766 if (fclose(blame->f) == EOF && err == NULL)
4767 err = got_error_from_errno("fclose");
4771 for (i = 0; i < blame->nlines; i++)
4772 free(blame->lines[i].id);
4774 blame->lines = NULL;
4776 free(blame->cb_args.commit_id);
4777 blame->cb_args.commit_id = NULL;
4778 if (blame->pack_fds) {
4779 const struct got_error *pack_err =
4780 got_repo_pack_fds_close(blame->pack_fds);
4783 blame->pack_fds = NULL;
4788 static const struct got_error *
4789 cancel_blame_view(void *arg)
4791 const struct got_error *err = NULL;
4795 errcode = pthread_mutex_lock(&tog_mutex);
4797 return got_error_set_errno(errcode,
4798 "pthread_mutex_unlock");
4801 err = got_error(GOT_ERR_CANCELLED);
4803 errcode = pthread_mutex_unlock(&tog_mutex);
4805 return got_error_set_errno(errcode,
4806 "pthread_mutex_lock");
4811 static const struct got_error *
4812 run_blame(struct tog_view *view)
4814 struct tog_blame_view_state *s = &view->state.blame;
4815 struct tog_blame *blame = &s->blame;
4816 const struct got_error *err = NULL;
4817 struct got_commit_object *commit = NULL;
4818 struct got_blob_object *blob = NULL;
4819 struct got_repository *thread_repo = NULL;
4820 struct got_object_id *obj_id = NULL;
4821 int obj_type, fd = -1;
4822 int *pack_fds = NULL;
4824 err = got_object_open_as_commit(&commit, s->repo,
4825 &s->blamed_commit->id);
4829 fd = got_opentempfd();
4831 err = got_error_from_errno("got_opentempfd");
4835 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4839 err = got_object_get_type(&obj_type, s->repo, obj_id);
4843 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4844 err = got_error(GOT_ERR_OBJ_TYPE);
4848 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
4851 blame->f = got_opentemp();
4852 if (blame->f == NULL) {
4853 err = got_error_from_errno("got_opentemp");
4856 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4857 &blame->line_offsets, blame->f, blob);
4860 if (blame->nlines == 0) {
4861 s->blame_complete = 1;
4865 /* Don't include \n at EOF in the blame line count. */
4866 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4869 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4870 if (blame->lines == NULL) {
4871 err = got_error_from_errno("calloc");
4875 err = got_repo_pack_fds_open(&pack_fds);
4878 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4883 blame->pack_fds = pack_fds;
4884 blame->cb_args.view = view;
4885 blame->cb_args.lines = blame->lines;
4886 blame->cb_args.nlines = blame->nlines;
4887 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4888 if (blame->cb_args.commit_id == NULL) {
4889 err = got_error_from_errno("got_object_id_dup");
4892 blame->cb_args.quit = &s->done;
4894 blame->thread_args.path = s->path;
4895 blame->thread_args.repo = thread_repo;
4896 blame->thread_args.cb_args = &blame->cb_args;
4897 blame->thread_args.complete = &s->blame_complete;
4898 blame->thread_args.cancel_cb = cancel_blame_view;
4899 blame->thread_args.cancel_arg = &s->done;
4900 s->blame_complete = 0;
4902 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4903 s->first_displayed_line = 1;
4904 s->last_displayed_line = view->nlines;
4905 s->selected_line = 1;
4907 s->matched_line = 0;
4911 got_object_commit_close(commit);
4912 if (fd != -1 && close(fd) == -1 && err == NULL)
4913 err = got_error_from_errno("close");
4915 got_object_blob_close(blob);
4922 static const struct got_error *
4923 open_blame_view(struct tog_view *view, char *path,
4924 struct got_object_id *commit_id, struct got_repository *repo)
4926 const struct got_error *err = NULL;
4927 struct tog_blame_view_state *s = &view->state.blame;
4929 STAILQ_INIT(&s->blamed_commits);
4931 s->path = strdup(path);
4932 if (s->path == NULL)
4933 return got_error_from_errno("strdup");
4935 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4941 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4942 s->first_displayed_line = 1;
4943 s->last_displayed_line = view->nlines;
4944 s->selected_line = 1;
4945 s->blame_complete = 0;
4947 s->commit_id = commit_id;
4948 memset(&s->blame, 0, sizeof(s->blame));
4950 STAILQ_INIT(&s->colors);
4951 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4952 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4953 get_color_value("TOG_COLOR_COMMIT"));
4958 view->show = show_blame_view;
4959 view->input = input_blame_view;
4960 view->close = close_blame_view;
4961 view->search_start = search_start_blame_view;
4962 view->search_next = search_next_blame_view;
4964 return run_blame(view);
4967 static const struct got_error *
4968 close_blame_view(struct tog_view *view)
4970 const struct got_error *err = NULL;
4971 struct tog_blame_view_state *s = &view->state.blame;
4973 if (s->blame.thread)
4974 err = stop_blame(&s->blame);
4976 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4977 struct got_object_qid *blamed_commit;
4978 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4979 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4980 got_object_qid_free(blamed_commit);
4984 free_colors(&s->colors);
4988 static const struct got_error *
4989 search_start_blame_view(struct tog_view *view)
4991 struct tog_blame_view_state *s = &view->state.blame;
4993 s->matched_line = 0;
4997 static const struct got_error *
4998 search_next_blame_view(struct tog_view *view)
5000 struct tog_blame_view_state *s = &view->state.blame;
5001 const struct got_error *err = NULL;
5004 size_t linesize = 0;
5007 if (!view->searching) {
5008 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5012 if (s->matched_line) {
5013 if (view->searching == TOG_SEARCH_FORWARD)
5014 lineno = s->matched_line + 1;
5016 lineno = s->matched_line - 1;
5018 lineno = s->first_displayed_line - 1 + s->selected_line;
5023 if (lineno <= 0 || lineno > s->blame.nlines) {
5024 if (s->matched_line == 0) {
5025 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5029 if (view->searching == TOG_SEARCH_FORWARD)
5032 lineno = s->blame.nlines;
5035 offset = s->blame.line_offsets[lineno - 1];
5036 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5038 return got_error_from_errno("fseeko");
5040 linelen = getline(&line, &linesize, s->blame.f);
5041 if (linelen != -1) {
5043 err = expand_tab(&exstr, line);
5046 if (match_line(exstr, &view->regex, 1,
5048 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5049 s->matched_line = lineno;
5055 if (view->searching == TOG_SEARCH_FORWARD)
5062 if (s->matched_line) {
5063 s->first_displayed_line = s->matched_line;
5064 s->selected_line = 1;
5070 static const struct got_error *
5071 show_blame_view(struct tog_view *view)
5073 const struct got_error *err = NULL;
5074 struct tog_blame_view_state *s = &view->state.blame;
5077 if (s->blame.thread == NULL && !s->blame_complete) {
5078 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5079 &s->blame.thread_args);
5081 return got_error_set_errno(errcode, "pthread_create");
5083 halfdelay(1); /* fast refresh while annotating */
5086 if (s->blame_complete)
5087 halfdelay(10); /* disable fast refresh */
5089 err = draw_blame(view);
5095 static const struct got_error *
5096 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5098 const struct got_error *err = NULL, *thread_err = NULL;
5099 struct tog_view *diff_view;
5100 struct tog_blame_view_state *s = &view->state.blame;
5101 int begin_x = 0, nscroll = view->nlines - 2;
5108 view->x = MAX(view->maxx - view->ncols / 3, 0);
5113 if (view->x + view->ncols / 3 < view->maxx)
5114 view->x += 2; /* move two columns right */
5120 view->x -= MIN(view->x, 2); /* move two columns back */
5129 s->selected_line = 1;
5130 s->first_displayed_line = 1;
5135 if (s->blame.nlines < view->nlines - 2) {
5136 s->selected_line = s->blame.nlines;
5137 s->first_displayed_line = 1;
5139 s->selected_line = view->nlines - 2;
5140 s->first_displayed_line = s->blame.nlines -
5148 if (s->selected_line > 1)
5150 else if (s->selected_line == 1 &&
5151 s->first_displayed_line > 1)
5152 s->first_displayed_line--;
5163 if (s->first_displayed_line == 1) {
5164 if (view->count > 1)
5166 s->selected_line = MAX(1, s->selected_line - nscroll);
5170 if (s->first_displayed_line > nscroll)
5171 s->first_displayed_line -= nscroll;
5173 s->first_displayed_line = 1;
5178 if (s->selected_line < view->nlines - 2 &&
5179 s->first_displayed_line +
5180 s->selected_line <= s->blame.nlines)
5182 else if (s->last_displayed_line <
5184 s->first_displayed_line++;
5190 struct got_object_id *id = NULL;
5193 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5194 s->first_displayed_line, s->selected_line);
5198 struct got_commit_object *commit, *pcommit;
5199 struct got_object_qid *pid;
5200 struct got_object_id *blob_id = NULL;
5202 err = got_object_open_as_commit(&commit,
5207 got_object_commit_get_parent_ids(commit));
5209 got_object_commit_close(commit);
5212 /* Check if path history ends here. */
5213 err = got_object_open_as_commit(&pcommit,
5217 err = got_object_id_by_path(&blob_id, s->repo,
5219 got_object_commit_close(pcommit);
5221 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5223 got_object_commit_close(commit);
5226 err = got_object_get_type(&obj_type, s->repo,
5229 /* Can't blame non-blob type objects. */
5230 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5231 got_object_commit_close(commit);
5234 err = got_object_qid_alloc(&s->blamed_commit,
5236 got_object_commit_close(commit);
5238 if (got_object_id_cmp(id,
5239 &s->blamed_commit->id) == 0)
5241 err = got_object_qid_alloc(&s->blamed_commit,
5247 thread_err = stop_blame(&s->blame);
5251 STAILQ_INSERT_HEAD(&s->blamed_commits,
5252 s->blamed_commit, entry);
5253 err = run_blame(view);
5259 struct got_object_qid *first;
5262 first = STAILQ_FIRST(&s->blamed_commits);
5263 if (!got_object_id_cmp(&first->id, s->commit_id))
5266 thread_err = stop_blame(&s->blame);
5270 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5271 got_object_qid_free(s->blamed_commit);
5273 STAILQ_FIRST(&s->blamed_commits);
5274 err = run_blame(view);
5281 struct got_object_id *id = NULL;
5282 struct got_object_qid *pid;
5283 struct got_commit_object *commit = NULL;
5286 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5287 s->first_displayed_line, s->selected_line);
5290 err = got_object_open_as_commit(&commit, s->repo, id);
5294 got_object_commit_get_parent_ids(commit));
5295 if (view_is_parent_view(view))
5296 begin_x = view_split_begin_x(view->begin_x);
5297 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5298 if (diff_view == NULL) {
5299 got_object_commit_close(commit);
5300 err = got_error_from_errno("view_open");
5303 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5304 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5305 got_object_commit_close(commit);
5307 view_close(diff_view);
5311 diff_view->focussed = 1;
5312 if (view_is_parent_view(view)) {
5313 err = view_close_child(view);
5316 err = view_set_child(view, diff_view);
5319 view->focus_child = 1;
5321 *new_view = diff_view;
5334 if (s->last_displayed_line >= s->blame.nlines &&
5335 s->selected_line >= MIN(s->blame.nlines,
5336 view->nlines - 2)) {
5340 if (s->last_displayed_line >= s->blame.nlines &&
5341 s->selected_line < view->nlines - 2) {
5343 MIN(nscroll, s->last_displayed_line -
5344 s->first_displayed_line - s->selected_line + 1);
5346 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5347 s->first_displayed_line += nscroll;
5349 s->first_displayed_line =
5350 s->blame.nlines - (view->nlines - 3);
5353 if (s->selected_line > view->nlines - 2) {
5354 s->selected_line = MIN(s->blame.nlines,
5362 return thread_err ? thread_err : err;
5365 static const struct got_error *
5366 cmd_blame(int argc, char *argv[])
5368 const struct got_error *error;
5369 struct got_repository *repo = NULL;
5370 struct got_worktree *worktree = NULL;
5371 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5372 char *link_target = NULL;
5373 struct got_object_id *commit_id = NULL;
5374 struct got_commit_object *commit = NULL;
5375 char *commit_id_str = NULL;
5377 struct tog_view *view;
5378 int *pack_fds = NULL;
5380 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5383 commit_id_str = optarg;
5386 repo_path = realpath(optarg, NULL);
5387 if (repo_path == NULL)
5388 return got_error_from_errno2("realpath",
5403 error = got_repo_pack_fds_open(&pack_fds);
5407 if (repo_path == NULL) {
5408 cwd = getcwd(NULL, 0);
5410 return got_error_from_errno("getcwd");
5411 error = got_worktree_open(&worktree, cwd);
5412 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5416 strdup(got_worktree_get_repo_path(worktree));
5418 repo_path = strdup(cwd);
5419 if (repo_path == NULL) {
5420 error = got_error_from_errno("strdup");
5425 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5429 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5436 error = apply_unveil(got_repo_get_path(repo), NULL);
5440 error = tog_load_refs(repo, 0);
5444 if (commit_id_str == NULL) {
5445 struct got_reference *head_ref;
5446 error = got_ref_open(&head_ref, repo, worktree ?
5447 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5450 error = got_ref_resolve(&commit_id, repo, head_ref);
5451 got_ref_close(head_ref);
5453 error = got_repo_match_object_id(&commit_id, NULL,
5454 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5459 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5461 error = got_error_from_errno("view_open");
5465 error = got_object_open_as_commit(&commit, repo, commit_id);
5469 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5474 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5479 /* Release work tree lock. */
5480 got_worktree_close(worktree);
5483 error = view_loop(view);
5491 got_object_commit_close(commit);
5493 got_worktree_close(worktree);
5495 const struct got_error *close_err = got_repo_close(repo);
5500 const struct got_error *pack_err =
5501 got_repo_pack_fds_close(pack_fds);
5509 static const struct got_error *
5510 draw_tree_entries(struct tog_view *view, const char *parent_path)
5512 struct tog_tree_view_state *s = &view->state.tree;
5513 const struct got_error *err = NULL;
5514 struct got_tree_entry *te;
5516 struct tog_color *tc;
5517 int width, n, i, nentries;
5518 int limit = view->nlines;
5522 werase(view->window);
5527 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5531 if (view_needs_focus_indication(view))
5532 wstandout(view->window);
5533 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5535 wattr_on(view->window,
5536 COLOR_PAIR(tc->colorpair), NULL);
5537 waddwstr(view->window, wline);
5539 wattr_off(view->window,
5540 COLOR_PAIR(tc->colorpair), NULL);
5541 if (view_needs_focus_indication(view))
5542 wstandend(view->window);
5545 if (width < view->ncols - 1)
5546 waddch(view->window, '\n');
5549 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5553 waddwstr(view->window, wline);
5556 if (width < view->ncols - 1)
5557 waddch(view->window, '\n');
5560 waddch(view->window, '\n');
5564 if (s->first_displayed_entry == NULL) {
5565 te = got_object_tree_get_first_entry(s->tree);
5566 if (s->selected == 0) {
5568 wstandout(view->window);
5569 s->selected_entry = NULL;
5571 waddstr(view->window, " ..\n"); /* parent directory */
5572 if (s->selected == 0 && view->focussed)
5573 wstandend(view->window);
5580 te = s->first_displayed_entry;
5583 nentries = got_object_tree_get_nentries(s->tree);
5584 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5585 char *line = NULL, *id_str = NULL, *link_target = NULL;
5586 const char *modestr = "";
5589 te = got_object_tree_get_entry(s->tree, i);
5590 mode = got_tree_entry_get_mode(te);
5593 err = got_object_id_str(&id_str,
5594 got_tree_entry_get_id(te));
5596 return got_error_from_errno(
5597 "got_object_id_str");
5599 if (got_object_tree_entry_is_submodule(te))
5601 else if (S_ISLNK(mode)) {
5604 err = got_tree_entry_get_symlink_target(&link_target,
5610 for (i = 0; i < strlen(link_target); i++) {
5611 if (!isprint((unsigned char)link_target[i]))
5612 link_target[i] = '?';
5616 else if (S_ISDIR(mode))
5618 else if (mode & S_IXUSR)
5620 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5621 got_tree_entry_get_name(te), modestr,
5622 link_target ? " -> ": "",
5623 link_target ? link_target : "") == -1) {
5626 return got_error_from_errno("asprintf");
5630 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5636 if (n == s->selected) {
5638 wstandout(view->window);
5639 s->selected_entry = te;
5641 tc = match_color(&s->colors, line);
5643 wattr_on(view->window,
5644 COLOR_PAIR(tc->colorpair), NULL);
5645 waddwstr(view->window, wline);
5647 wattr_off(view->window,
5648 COLOR_PAIR(tc->colorpair), NULL);
5649 if (width < view->ncols - 1)
5650 waddch(view->window, '\n');
5651 if (n == s->selected && view->focussed)
5652 wstandend(view->window);
5658 s->last_displayed_entry = te;
5667 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5669 struct got_tree_entry *te;
5670 int isroot = s->tree == s->root;
5673 if (s->first_displayed_entry == NULL)
5676 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5677 while (i++ < maxscroll) {
5680 s->first_displayed_entry = NULL;
5683 s->first_displayed_entry = te;
5684 te = got_tree_entry_get_prev(s->tree, te);
5689 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5691 struct got_tree_entry *next, *last;
5694 if (s->first_displayed_entry)
5695 next = got_tree_entry_get_next(s->tree,
5696 s->first_displayed_entry);
5698 next = got_object_tree_get_first_entry(s->tree);
5700 last = s->last_displayed_entry;
5701 while (next && last && n++ < maxscroll) {
5702 last = got_tree_entry_get_next(s->tree, last);
5704 s->first_displayed_entry = next;
5705 next = got_tree_entry_get_next(s->tree, next);
5710 static const struct got_error *
5711 tree_entry_path(char **path, struct tog_parent_trees *parents,
5712 struct got_tree_entry *te)
5714 const struct got_error *err = NULL;
5715 struct tog_parent_tree *pt;
5716 size_t len = 2; /* for leading slash and NUL */
5718 TAILQ_FOREACH(pt, parents, entry)
5719 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5722 len += strlen(got_tree_entry_get_name(te));
5724 *path = calloc(1, len);
5726 return got_error_from_errno("calloc");
5729 pt = TAILQ_LAST(parents, tog_parent_trees);
5731 const char *name = got_tree_entry_get_name(pt->selected_entry);
5732 if (strlcat(*path, name, len) >= len) {
5733 err = got_error(GOT_ERR_NO_SPACE);
5736 if (strlcat(*path, "/", len) >= len) {
5737 err = got_error(GOT_ERR_NO_SPACE);
5740 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5743 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5744 err = got_error(GOT_ERR_NO_SPACE);
5756 static const struct got_error *
5757 blame_tree_entry(struct tog_view **new_view, int begin_x,
5758 struct got_tree_entry *te, struct tog_parent_trees *parents,
5759 struct got_object_id *commit_id, struct got_repository *repo)
5761 const struct got_error *err = NULL;
5763 struct tog_view *blame_view;
5767 err = tree_entry_path(&path, parents, te);
5771 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5772 if (blame_view == NULL) {
5773 err = got_error_from_errno("view_open");
5777 err = open_blame_view(blame_view, path, commit_id, repo);
5779 if (err->code == GOT_ERR_CANCELLED)
5781 view_close(blame_view);
5783 *new_view = blame_view;
5789 static const struct got_error *
5790 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5791 struct tog_tree_view_state *s)
5793 struct tog_view *log_view;
5794 const struct got_error *err = NULL;
5799 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5800 if (log_view == NULL)
5801 return got_error_from_errno("view_open");
5803 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5807 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5810 view_close(log_view);
5812 *new_view = log_view;
5817 static const struct got_error *
5818 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5819 const char *head_ref_name, struct got_repository *repo)
5821 const struct got_error *err = NULL;
5822 char *commit_id_str = NULL;
5823 struct tog_tree_view_state *s = &view->state.tree;
5824 struct got_commit_object *commit = NULL;
5826 TAILQ_INIT(&s->parents);
5827 STAILQ_INIT(&s->colors);
5829 s->commit_id = got_object_id_dup(commit_id);
5830 if (s->commit_id == NULL)
5831 return got_error_from_errno("got_object_id_dup");
5833 err = got_object_open_as_commit(&commit, repo, commit_id);
5838 * The root is opened here and will be closed when the view is closed.
5839 * Any visited subtrees and their path-wise parents are opened and
5842 err = got_object_open_as_tree(&s->root, repo,
5843 got_object_commit_get_tree_id(commit));
5848 err = got_object_id_str(&commit_id_str, commit_id);
5852 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5853 err = got_error_from_errno("asprintf");
5857 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5858 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5859 if (head_ref_name) {
5860 s->head_ref_name = strdup(head_ref_name);
5861 if (s->head_ref_name == NULL) {
5862 err = got_error_from_errno("strdup");
5868 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5869 err = add_color(&s->colors, "\\$$",
5870 TOG_COLOR_TREE_SUBMODULE,
5871 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5874 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5875 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5878 err = add_color(&s->colors, "/$",
5879 TOG_COLOR_TREE_DIRECTORY,
5880 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5884 err = add_color(&s->colors, "\\*$",
5885 TOG_COLOR_TREE_EXECUTABLE,
5886 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5890 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5891 get_color_value("TOG_COLOR_COMMIT"));
5896 view->show = show_tree_view;
5897 view->input = input_tree_view;
5898 view->close = close_tree_view;
5899 view->search_start = search_start_tree_view;
5900 view->search_next = search_next_tree_view;
5902 free(commit_id_str);
5904 got_object_commit_close(commit);
5906 close_tree_view(view);
5910 static const struct got_error *
5911 close_tree_view(struct tog_view *view)
5913 struct tog_tree_view_state *s = &view->state.tree;
5915 free_colors(&s->colors);
5916 free(s->tree_label);
5917 s->tree_label = NULL;
5919 s->commit_id = NULL;
5920 free(s->head_ref_name);
5921 s->head_ref_name = NULL;
5922 while (!TAILQ_EMPTY(&s->parents)) {
5923 struct tog_parent_tree *parent;
5924 parent = TAILQ_FIRST(&s->parents);
5925 TAILQ_REMOVE(&s->parents, parent, entry);
5926 if (parent->tree != s->root)
5927 got_object_tree_close(parent->tree);
5931 if (s->tree != NULL && s->tree != s->root)
5932 got_object_tree_close(s->tree);
5934 got_object_tree_close(s->root);
5938 static const struct got_error *
5939 search_start_tree_view(struct tog_view *view)
5941 struct tog_tree_view_state *s = &view->state.tree;
5943 s->matched_entry = NULL;
5948 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5950 regmatch_t regmatch;
5952 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5956 static const struct got_error *
5957 search_next_tree_view(struct tog_view *view)
5959 struct tog_tree_view_state *s = &view->state.tree;
5960 struct got_tree_entry *te = NULL;
5962 if (!view->searching) {
5963 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5967 if (s->matched_entry) {
5968 if (view->searching == TOG_SEARCH_FORWARD) {
5969 if (s->selected_entry)
5970 te = got_tree_entry_get_next(s->tree,
5973 te = got_object_tree_get_first_entry(s->tree);
5975 if (s->selected_entry == NULL)
5976 te = got_object_tree_get_last_entry(s->tree);
5978 te = got_tree_entry_get_prev(s->tree,
5982 if (s->selected_entry)
5983 te = s->selected_entry;
5984 else if (view->searching == TOG_SEARCH_FORWARD)
5985 te = got_object_tree_get_first_entry(s->tree);
5987 te = got_object_tree_get_last_entry(s->tree);
5992 if (s->matched_entry == NULL) {
5993 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5996 if (view->searching == TOG_SEARCH_FORWARD)
5997 te = got_object_tree_get_first_entry(s->tree);
5999 te = got_object_tree_get_last_entry(s->tree);
6002 if (match_tree_entry(te, &view->regex)) {
6003 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6004 s->matched_entry = te;
6008 if (view->searching == TOG_SEARCH_FORWARD)
6009 te = got_tree_entry_get_next(s->tree, te);
6011 te = got_tree_entry_get_prev(s->tree, te);
6014 if (s->matched_entry) {
6015 s->first_displayed_entry = s->matched_entry;
6022 static const struct got_error *
6023 show_tree_view(struct tog_view *view)
6025 const struct got_error *err = NULL;
6026 struct tog_tree_view_state *s = &view->state.tree;
6029 err = tree_entry_path(&parent_path, &s->parents, NULL);
6033 err = draw_tree_entries(view, parent_path);
6040 static const struct got_error *
6041 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6043 const struct got_error *err = NULL;
6044 struct tog_tree_view_state *s = &view->state.tree;
6045 struct tog_view *log_view, *ref_view;
6046 struct got_tree_entry *te;
6047 int begin_x = 0, n, nscroll = view->nlines - 3;
6051 s->show_ids = !s->show_ids;
6056 if (!s->selected_entry)
6058 if (view_is_parent_view(view))
6059 begin_x = view_split_begin_x(view->begin_x);
6060 err = log_selected_tree_entry(&log_view, begin_x, s);
6062 log_view->focussed = 1;
6063 if (view_is_parent_view(view)) {
6064 err = view_close_child(view);
6067 err = view_set_child(view, log_view);
6070 view->focus_child = 1;
6072 *new_view = log_view;
6076 if (view_is_parent_view(view))
6077 begin_x = view_split_begin_x(view->begin_x);
6078 ref_view = view_open(view->nlines, view->ncols,
6079 view->begin_y, begin_x, TOG_VIEW_REF);
6080 if (ref_view == NULL)
6081 return got_error_from_errno("view_open");
6082 err = open_ref_view(ref_view, s->repo);
6084 view_close(ref_view);
6088 ref_view->focussed = 1;
6089 if (view_is_parent_view(view)) {
6090 err = view_close_child(view);
6093 err = view_set_child(view, ref_view);
6096 view->focus_child = 1;
6098 *new_view = ref_view;
6104 if (s->tree == s->root)
6105 s->first_displayed_entry =
6106 got_object_tree_get_first_entry(s->tree);
6108 s->first_displayed_entry = NULL;
6114 te = got_object_tree_get_last_entry(s->tree);
6115 for (n = 0; n < view->nlines - 3; n++) {
6117 if(s->tree != s->root) {
6118 s->first_displayed_entry = NULL;
6123 s->first_displayed_entry = te;
6124 te = got_tree_entry_get_prev(s->tree, te);
6127 s->selected = n - 1;
6132 if (s->selected > 0) {
6136 tree_scroll_up(s, 1);
6137 if (s->selected_entry == NULL ||
6138 (s->tree == s->root && s->selected_entry ==
6139 got_object_tree_get_first_entry(s->tree)))
6149 if (s->tree == s->root) {
6150 if (got_object_tree_get_first_entry(s->tree) ==
6151 s->first_displayed_entry)
6152 s->selected -= MIN(s->selected, nscroll);
6154 if (s->first_displayed_entry == NULL)
6155 s->selected -= MIN(s->selected, nscroll);
6157 tree_scroll_up(s, MAX(0, nscroll));
6158 if (s->selected_entry == NULL ||
6159 (s->tree == s->root && s->selected_entry ==
6160 got_object_tree_get_first_entry(s->tree)))
6166 if (s->selected < s->ndisplayed - 1) {
6170 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6172 /* can't scroll any further */
6176 tree_scroll_down(s, 1);
6186 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6188 /* can't scroll any further; move cursor down */
6189 if (s->selected < s->ndisplayed - 1)
6190 s->selected += MIN(nscroll,
6191 s->ndisplayed - s->selected - 1);
6196 tree_scroll_down(s, nscroll);
6201 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6202 struct tog_parent_tree *parent;
6203 /* user selected '..' */
6204 if (s->tree == s->root) {
6208 parent = TAILQ_FIRST(&s->parents);
6209 TAILQ_REMOVE(&s->parents, parent,
6211 got_object_tree_close(s->tree);
6212 s->tree = parent->tree;
6213 s->first_displayed_entry =
6214 parent->first_displayed_entry;
6216 parent->selected_entry;
6217 s->selected = parent->selected;
6219 } else if (S_ISDIR(got_tree_entry_get_mode(
6220 s->selected_entry))) {
6221 struct got_tree_object *subtree;
6223 err = got_object_open_as_tree(&subtree, s->repo,
6224 got_tree_entry_get_id(s->selected_entry));
6227 err = tree_view_visit_subtree(s, subtree);
6229 got_object_tree_close(subtree);
6232 } else if (S_ISREG(got_tree_entry_get_mode(
6233 s->selected_entry))) {
6234 struct tog_view *blame_view;
6235 int begin_x = view_is_parent_view(view) ?
6236 view_split_begin_x(view->begin_x) : 0;
6238 err = blame_tree_entry(&blame_view, begin_x,
6239 s->selected_entry, &s->parents,
6240 s->commit_id, s->repo);
6245 blame_view->focussed = 1;
6246 if (view_is_parent_view(view)) {
6247 err = view_close_child(view);
6250 err = view_set_child(view, blame_view);
6253 view->focus_child = 1;
6255 *new_view = blame_view;
6259 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6260 s->selected = view->nlines - 4;
6276 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6281 static const struct got_error *
6282 cmd_tree(int argc, char *argv[])
6284 const struct got_error *error;
6285 struct got_repository *repo = NULL;
6286 struct got_worktree *worktree = NULL;
6287 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6288 struct got_object_id *commit_id = NULL;
6289 struct got_commit_object *commit = NULL;
6290 const char *commit_id_arg = NULL;
6292 struct got_reference *ref = NULL;
6293 const char *head_ref_name = NULL;
6295 struct tog_view *view;
6296 int *pack_fds = NULL;
6298 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6301 commit_id_arg = optarg;
6304 repo_path = realpath(optarg, NULL);
6305 if (repo_path == NULL)
6306 return got_error_from_errno2("realpath",
6321 error = got_repo_pack_fds_open(&pack_fds);
6325 if (repo_path == NULL) {
6326 cwd = getcwd(NULL, 0);
6328 return got_error_from_errno("getcwd");
6329 error = got_worktree_open(&worktree, cwd);
6330 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6334 strdup(got_worktree_get_repo_path(worktree));
6336 repo_path = strdup(cwd);
6337 if (repo_path == NULL) {
6338 error = got_error_from_errno("strdup");
6343 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6347 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6354 error = apply_unveil(got_repo_get_path(repo), NULL);
6358 error = tog_load_refs(repo, 0);
6362 if (commit_id_arg == NULL) {
6363 error = got_repo_match_object_id(&commit_id, &label,
6364 worktree ? got_worktree_get_head_ref_name(worktree) :
6365 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6368 head_ref_name = label;
6370 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6372 head_ref_name = got_ref_get_name(ref);
6373 else if (error->code != GOT_ERR_NOT_REF)
6375 error = got_repo_match_object_id(&commit_id, NULL,
6376 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6381 error = got_object_open_as_commit(&commit, repo, commit_id);
6385 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6387 error = got_error_from_errno("view_open");
6390 error = open_tree_view(view, commit_id, head_ref_name, repo);
6393 if (!got_path_is_root_dir(in_repo_path)) {
6394 error = tree_view_walk_path(&view->state.tree, commit,
6401 /* Release work tree lock. */
6402 got_worktree_close(worktree);
6405 error = view_loop(view);
6414 const struct got_error *close_err = got_repo_close(repo);
6419 const struct got_error *pack_err =
6420 got_repo_pack_fds_close(pack_fds);
6428 static const struct got_error *
6429 ref_view_load_refs(struct tog_ref_view_state *s)
6431 struct got_reflist_entry *sre;
6432 struct tog_reflist_entry *re;
6435 TAILQ_FOREACH(sre, &tog_refs, entry) {
6436 if (strncmp(got_ref_get_name(sre->ref),
6437 "refs/got/", 9) == 0 &&
6438 strncmp(got_ref_get_name(sre->ref),
6439 "refs/got/backup/", 16) != 0)
6442 re = malloc(sizeof(*re));
6444 return got_error_from_errno("malloc");
6446 re->ref = got_ref_dup(sre->ref);
6447 if (re->ref == NULL)
6448 return got_error_from_errno("got_ref_dup");
6449 re->idx = s->nrefs++;
6450 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6453 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6458 ref_view_free_refs(struct tog_ref_view_state *s)
6460 struct tog_reflist_entry *re;
6462 while (!TAILQ_EMPTY(&s->refs)) {
6463 re = TAILQ_FIRST(&s->refs);
6464 TAILQ_REMOVE(&s->refs, re, entry);
6465 got_ref_close(re->ref);
6470 static const struct got_error *
6471 open_ref_view(struct tog_view *view, struct got_repository *repo)
6473 const struct got_error *err = NULL;
6474 struct tog_ref_view_state *s = &view->state.ref;
6476 s->selected_entry = 0;
6479 TAILQ_INIT(&s->refs);
6480 STAILQ_INIT(&s->colors);
6482 err = ref_view_load_refs(s);
6486 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6487 err = add_color(&s->colors, "^refs/heads/",
6488 TOG_COLOR_REFS_HEADS,
6489 get_color_value("TOG_COLOR_REFS_HEADS"));
6493 err = add_color(&s->colors, "^refs/tags/",
6494 TOG_COLOR_REFS_TAGS,
6495 get_color_value("TOG_COLOR_REFS_TAGS"));
6499 err = add_color(&s->colors, "^refs/remotes/",
6500 TOG_COLOR_REFS_REMOTES,
6501 get_color_value("TOG_COLOR_REFS_REMOTES"));
6505 err = add_color(&s->colors, "^refs/got/backup/",
6506 TOG_COLOR_REFS_BACKUP,
6507 get_color_value("TOG_COLOR_REFS_BACKUP"));
6512 view->show = show_ref_view;
6513 view->input = input_ref_view;
6514 view->close = close_ref_view;
6515 view->search_start = search_start_ref_view;
6516 view->search_next = search_next_ref_view;
6519 free_colors(&s->colors);
6523 static const struct got_error *
6524 close_ref_view(struct tog_view *view)
6526 struct tog_ref_view_state *s = &view->state.ref;
6528 ref_view_free_refs(s);
6529 free_colors(&s->colors);
6534 static const struct got_error *
6535 resolve_reflist_entry(struct got_object_id **commit_id,
6536 struct tog_reflist_entry *re, struct got_repository *repo)
6538 const struct got_error *err = NULL;
6539 struct got_object_id *obj_id;
6540 struct got_tag_object *tag = NULL;
6545 err = got_ref_resolve(&obj_id, repo, re->ref);
6549 err = got_object_get_type(&obj_type, repo, obj_id);
6554 case GOT_OBJ_TYPE_COMMIT:
6555 *commit_id = obj_id;
6557 case GOT_OBJ_TYPE_TAG:
6558 err = got_object_open_as_tag(&tag, repo, obj_id);
6562 err = got_object_get_type(&obj_type, repo,
6563 got_object_tag_get_object_id(tag));
6566 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6567 err = got_error(GOT_ERR_OBJ_TYPE);
6570 *commit_id = got_object_id_dup(
6571 got_object_tag_get_object_id(tag));
6572 if (*commit_id == NULL) {
6573 err = got_error_from_errno("got_object_id_dup");
6578 err = got_error(GOT_ERR_OBJ_TYPE);
6584 got_object_tag_close(tag);
6592 static const struct got_error *
6593 log_ref_entry(struct tog_view **new_view, int begin_x,
6594 struct tog_reflist_entry *re, struct got_repository *repo)
6596 struct tog_view *log_view;
6597 const struct got_error *err = NULL;
6598 struct got_object_id *commit_id = NULL;
6602 err = resolve_reflist_entry(&commit_id, re, repo);
6604 if (err->code != GOT_ERR_OBJ_TYPE)
6610 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6611 if (log_view == NULL) {
6612 err = got_error_from_errno("view_open");
6616 err = open_log_view(log_view, commit_id, repo,
6617 got_ref_get_name(re->ref), "", 0);
6620 view_close(log_view);
6622 *new_view = log_view;
6628 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6630 struct tog_reflist_entry *re;
6633 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6636 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6637 while (i++ < maxscroll) {
6640 s->first_displayed_entry = re;
6641 re = TAILQ_PREV(re, tog_reflist_head, entry);
6646 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6648 struct tog_reflist_entry *next, *last;
6651 if (s->first_displayed_entry)
6652 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6654 next = TAILQ_FIRST(&s->refs);
6656 last = s->last_displayed_entry;
6657 while (next && last && n++ < maxscroll) {
6658 last = TAILQ_NEXT(last, entry);
6660 s->first_displayed_entry = next;
6661 next = TAILQ_NEXT(next, entry);
6666 static const struct got_error *
6667 search_start_ref_view(struct tog_view *view)
6669 struct tog_ref_view_state *s = &view->state.ref;
6671 s->matched_entry = NULL;
6676 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6678 regmatch_t regmatch;
6680 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6684 static const struct got_error *
6685 search_next_ref_view(struct tog_view *view)
6687 struct tog_ref_view_state *s = &view->state.ref;
6688 struct tog_reflist_entry *re = NULL;
6690 if (!view->searching) {
6691 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6695 if (s->matched_entry) {
6696 if (view->searching == TOG_SEARCH_FORWARD) {
6697 if (s->selected_entry)
6698 re = TAILQ_NEXT(s->selected_entry, entry);
6700 re = TAILQ_PREV(s->selected_entry,
6701 tog_reflist_head, entry);
6703 if (s->selected_entry == NULL)
6704 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6706 re = TAILQ_PREV(s->selected_entry,
6707 tog_reflist_head, entry);
6710 if (s->selected_entry)
6711 re = s->selected_entry;
6712 else if (view->searching == TOG_SEARCH_FORWARD)
6713 re = TAILQ_FIRST(&s->refs);
6715 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6720 if (s->matched_entry == NULL) {
6721 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6724 if (view->searching == TOG_SEARCH_FORWARD)
6725 re = TAILQ_FIRST(&s->refs);
6727 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6730 if (match_reflist_entry(re, &view->regex)) {
6731 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6732 s->matched_entry = re;
6736 if (view->searching == TOG_SEARCH_FORWARD)
6737 re = TAILQ_NEXT(re, entry);
6739 re = TAILQ_PREV(re, tog_reflist_head, entry);
6742 if (s->matched_entry) {
6743 s->first_displayed_entry = s->matched_entry;
6750 static const struct got_error *
6751 show_ref_view(struct tog_view *view)
6753 const struct got_error *err = NULL;
6754 struct tog_ref_view_state *s = &view->state.ref;
6755 struct tog_reflist_entry *re;
6758 struct tog_color *tc;
6760 int limit = view->nlines;
6762 werase(view->window);
6769 re = s->first_displayed_entry;
6771 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6773 return got_error_from_errno("asprintf");
6775 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6780 if (view_needs_focus_indication(view))
6781 wstandout(view->window);
6782 waddwstr(view->window, wline);
6783 if (view_needs_focus_indication(view))
6784 wstandend(view->window);
6789 if (width < view->ncols - 1)
6790 waddch(view->window, '\n');
6795 while (re && limit > 0) {
6797 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6800 struct got_commit_object *ci;
6801 struct got_tag_object *tag;
6802 struct got_object_id *id;
6806 err = got_ref_resolve(&id, s->repo, re->ref);
6809 err = got_object_open_as_tag(&tag, s->repo, id);
6811 if (err->code != GOT_ERR_OBJ_TYPE) {
6815 err = got_object_open_as_commit(&ci, s->repo,
6821 t = got_object_commit_get_committer_time(ci);
6822 got_object_commit_close(ci);
6824 t = got_object_tag_get_tagger_time(tag);
6825 got_object_tag_close(tag);
6828 if (gmtime_r(&t, &tm) == NULL)
6829 return got_error_from_errno("gmtime_r");
6830 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6831 return got_error(GOT_ERR_NO_SPACE);
6833 if (got_ref_is_symbolic(re->ref)) {
6834 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6835 ymd : "", got_ref_get_name(re->ref),
6836 got_ref_get_symref_target(re->ref)) == -1)
6837 return got_error_from_errno("asprintf");
6838 } else if (s->show_ids) {
6839 struct got_object_id *id;
6841 err = got_ref_resolve(&id, s->repo, re->ref);
6844 err = got_object_id_str(&id_str, id);
6849 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6850 got_ref_get_name(re->ref), id_str) == -1) {
6851 err = got_error_from_errno("asprintf");
6858 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6859 got_ref_get_name(re->ref)) == -1)
6860 return got_error_from_errno("asprintf");
6862 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6868 if (n == s->selected) {
6870 wstandout(view->window);
6871 s->selected_entry = re;
6873 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6875 wattr_on(view->window,
6876 COLOR_PAIR(tc->colorpair), NULL);
6877 waddwstr(view->window, wline);
6879 wattr_off(view->window,
6880 COLOR_PAIR(tc->colorpair), NULL);
6881 if (width < view->ncols - 1)
6882 waddch(view->window, '\n');
6883 if (n == s->selected && view->focussed)
6884 wstandend(view->window);
6890 s->last_displayed_entry = re;
6893 re = TAILQ_NEXT(re, entry);
6900 static const struct got_error *
6901 browse_ref_tree(struct tog_view **new_view, int begin_x,
6902 struct tog_reflist_entry *re, struct got_repository *repo)
6904 const struct got_error *err = NULL;
6905 struct got_object_id *commit_id = NULL;
6906 struct tog_view *tree_view;
6910 err = resolve_reflist_entry(&commit_id, re, repo);
6912 if (err->code != GOT_ERR_OBJ_TYPE)
6919 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6920 if (tree_view == NULL) {
6921 err = got_error_from_errno("view_open");
6925 err = open_tree_view(tree_view, commit_id,
6926 got_ref_get_name(re->ref), repo);
6930 *new_view = tree_view;
6935 static const struct got_error *
6936 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6938 const struct got_error *err = NULL;
6939 struct tog_ref_view_state *s = &view->state.ref;
6940 struct tog_view *log_view, *tree_view;
6941 struct tog_reflist_entry *re;
6942 int begin_x = 0, n, nscroll = view->nlines - 1;
6946 s->show_ids = !s->show_ids;
6950 s->show_date = !s->show_date;
6954 s->sort_by_date = !s->sort_by_date;
6956 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6957 got_ref_cmp_by_commit_timestamp_descending :
6958 tog_ref_cmp_by_name, s->repo);
6961 got_reflist_object_id_map_free(tog_refs_idmap);
6962 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6963 &tog_refs, s->repo);
6966 ref_view_free_refs(s);
6967 err = ref_view_load_refs(s);
6972 if (!s->selected_entry)
6974 if (view_is_parent_view(view))
6975 begin_x = view_split_begin_x(view->begin_x);
6976 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6979 log_view->focussed = 1;
6980 if (view_is_parent_view(view)) {
6981 err = view_close_child(view);
6984 err = view_set_child(view, log_view);
6987 view->focus_child = 1;
6989 *new_view = log_view;
6993 if (!s->selected_entry)
6995 if (view_is_parent_view(view))
6996 begin_x = view_split_begin_x(view->begin_x);
6997 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6999 if (err || tree_view == NULL)
7002 tree_view->focussed = 1;
7003 if (view_is_parent_view(view)) {
7004 err = view_close_child(view);
7007 err = view_set_child(view, tree_view);
7010 view->focus_child = 1;
7012 *new_view = tree_view;
7018 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7024 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7025 for (n = 0; n < view->nlines - 1; n++) {
7028 s->first_displayed_entry = re;
7029 re = TAILQ_PREV(re, tog_reflist_head, entry);
7032 s->selected = n - 1;
7037 if (s->selected > 0) {
7041 ref_scroll_up(s, 1);
7042 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7052 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7053 s->selected -= MIN(nscroll, s->selected);
7054 ref_scroll_up(s, MAX(0, nscroll));
7055 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7061 if (s->selected < s->ndisplayed - 1) {
7065 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7066 /* can't scroll any further */
7070 ref_scroll_down(s, 1);
7080 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7081 /* can't scroll any further; move cursor down */
7082 if (s->selected < s->ndisplayed - 1)
7083 s->selected += MIN(nscroll,
7084 s->ndisplayed - s->selected - 1);
7085 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7086 s->selected += s->ndisplayed - s->selected - 1;
7090 ref_scroll_down(s, nscroll);
7095 err = tog_load_refs(s->repo, s->sort_by_date);
7098 ref_view_free_refs(s);
7099 err = ref_view_load_refs(s);
7102 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7103 s->selected = view->nlines - 2;
7117 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7122 static const struct got_error *
7123 cmd_ref(int argc, char *argv[])
7125 const struct got_error *error;
7126 struct got_repository *repo = NULL;
7127 struct got_worktree *worktree = NULL;
7128 char *cwd = NULL, *repo_path = NULL;
7130 struct tog_view *view;
7131 int *pack_fds = NULL;
7133 while ((ch = getopt(argc, argv, "r:")) != -1) {
7136 repo_path = realpath(optarg, NULL);
7137 if (repo_path == NULL)
7138 return got_error_from_errno2("realpath",
7153 error = got_repo_pack_fds_open(&pack_fds);
7157 if (repo_path == NULL) {
7158 cwd = getcwd(NULL, 0);
7160 return got_error_from_errno("getcwd");
7161 error = got_worktree_open(&worktree, cwd);
7162 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7166 strdup(got_worktree_get_repo_path(worktree));
7168 repo_path = strdup(cwd);
7169 if (repo_path == NULL) {
7170 error = got_error_from_errno("strdup");
7175 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7181 error = apply_unveil(got_repo_get_path(repo), NULL);
7185 error = tog_load_refs(repo, 0);
7189 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7191 error = got_error_from_errno("view_open");
7195 error = open_ref_view(view, repo);
7200 /* Release work tree lock. */
7201 got_worktree_close(worktree);
7204 error = view_loop(view);
7209 const struct got_error *close_err = got_repo_close(repo);
7214 const struct got_error *pack_err =
7215 got_repo_pack_fds_close(pack_fds);
7224 list_commands(FILE *fp)
7228 fprintf(fp, "commands:");
7229 for (i = 0; i < nitems(tog_commands); i++) {
7230 const struct tog_cmd *cmd = &tog_commands[i];
7231 fprintf(fp, " %s", cmd->name);
7237 usage(int hflag, int status)
7239 FILE *fp = (status == 0) ? stdout : stderr;
7241 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7244 fprintf(fp, "lazy usage: %s path\n", getprogname());
7251 make_argv(int argc, ...)
7259 argv = calloc(argc, sizeof(char *));
7262 for (i = 0; i < argc; i++) {
7263 argv[i] = strdup(va_arg(ap, char *));
7264 if (argv[i] == NULL)
7273 * Try to convert 'tog path' into a 'tog log path' command.
7274 * The user could simply have mistyped the command rather than knowingly
7275 * provided a path. So check whether argv[0] can in fact be resolved
7276 * to a path in the HEAD commit and print a special error if not.
7277 * This hack is for mpi@ <3
7279 static const struct got_error *
7280 tog_log_with_path(int argc, char *argv[])
7282 const struct got_error *error = NULL, *close_err;
7283 const struct tog_cmd *cmd = NULL;
7284 struct got_repository *repo = NULL;
7285 struct got_worktree *worktree = NULL;
7286 struct got_object_id *commit_id = NULL, *id = NULL;
7287 struct got_commit_object *commit = NULL;
7288 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7289 char *commit_id_str = NULL, **cmd_argv = NULL;
7290 int *pack_fds = NULL;
7292 cwd = getcwd(NULL, 0);
7294 return got_error_from_errno("getcwd");
7296 error = got_repo_pack_fds_open(&pack_fds);
7300 error = got_worktree_open(&worktree, cwd);
7301 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7305 repo_path = strdup(got_worktree_get_repo_path(worktree));
7307 repo_path = strdup(cwd);
7308 if (repo_path == NULL) {
7309 error = got_error_from_errno("strdup");
7313 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7317 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7322 error = tog_load_refs(repo, 0);
7325 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7326 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7327 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7332 got_worktree_close(worktree);
7336 error = got_object_open_as_commit(&commit, repo, commit_id);
7340 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7342 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7344 fprintf(stderr, "%s: '%s' is no known command or path\n",
7345 getprogname(), argv[0]);
7350 close_err = got_repo_close(repo);
7355 error = got_object_id_str(&commit_id_str, commit_id);
7359 cmd = &tog_commands[0]; /* log */
7361 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7362 error = cmd->cmd_main(argc, cmd_argv);
7365 close_err = got_repo_close(repo);
7370 got_object_commit_close(commit);
7372 got_worktree_close(worktree);
7374 const struct got_error *pack_err =
7375 got_repo_pack_fds_close(pack_fds);
7380 free(commit_id_str);
7387 for (i = 0; i < argc; i++)
7396 main(int argc, char *argv[])
7398 const struct got_error *error = NULL;
7399 const struct tog_cmd *cmd = NULL;
7400 int ch, hflag = 0, Vflag = 0;
7401 char **cmd_argv = NULL;
7402 static const struct option longopts[] = {
7403 { "version", no_argument, NULL, 'V' },
7407 setlocale(LC_CTYPE, "");
7409 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7429 got_version_print_str();
7434 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7442 /* Build an argument vector which runs a default command. */
7443 cmd = &tog_commands[0];
7445 cmd_argv = make_argv(argc, cmd->name);
7449 /* Did the user specify a command? */
7450 for (i = 0; i < nitems(tog_commands); i++) {
7451 if (strncmp(tog_commands[i].name, argv[0],
7452 strlen(argv[0])) == 0) {
7453 cmd = &tog_commands[i];
7462 /* No command specified; try log with a path */
7463 error = tog_log_with_path(argc, argv);
7468 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7475 for (i = 0; i < argc; i++)
7480 if (error && error->code != GOT_ERR_CANCELLED)
7481 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);