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);
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;
312 int first_displayed_line;
313 int last_displayed_line;
316 int ignore_whitespace;
318 struct got_repository *repo;
319 struct tog_colors colors;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
347 int *search_next_done;
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
390 struct tog_view *view;
391 struct got_object_id *commit_id;
395 struct tog_blame_thread_args {
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
400 got_cancel_cb cancel_cb;
407 struct tog_blame_line *lines;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
431 struct tog_colors colors;
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
496 TAILQ_HEAD(tog_view_list_head, tog_view);
499 TAILQ_ENTRY(tog_view) entry;
502 int nlines, ncols, begin_y, begin_x;
503 int maxx, x; /* max column and current start column */
504 int lines, cols; /* copies of LINES and COLS */
505 int focussed; /* Only set on one parent or child view at a time. */
507 struct tog_view *parent;
508 struct tog_view *child;
511 * This flag is initially set on parent views when a new child view
512 * is created. It gets toggled when the 'Tab' key switches focus
513 * between parent and child.
514 * The flag indicates whether focus should be passed on to our child
515 * view if this parent view gets picked for focus after another parent
516 * view was closed. This prevents child views from losing focus in such
521 /* type-specific state */
522 enum tog_view_type type;
524 struct tog_diff_view_state diff;
525 struct tog_log_view_state log;
526 struct tog_blame_view_state blame;
527 struct tog_tree_view_state tree;
528 struct tog_ref_view_state ref;
531 const struct got_error *(*show)(struct tog_view *);
532 const struct got_error *(*input)(struct tog_view **,
533 struct tog_view *, int);
534 const struct got_error *(*close)(struct tog_view *);
536 const struct got_error *(*search_start)(struct tog_view *);
537 const struct got_error *(*search_next)(struct tog_view *);
540 #define TOG_SEARCH_FORWARD 1
541 #define TOG_SEARCH_BACKWARD 2
542 int search_next_done;
543 #define TOG_SEARCH_HAVE_MORE 1
544 #define TOG_SEARCH_NO_MORE 2
545 #define TOG_SEARCH_HAVE_NONE 3
550 static const struct got_error *open_diff_view(struct tog_view *,
551 struct got_object_id *, struct got_object_id *,
552 const char *, const char *, int, int, int, struct tog_view *,
553 struct got_repository *);
554 static const struct got_error *show_diff_view(struct tog_view *);
555 static const struct got_error *input_diff_view(struct tog_view **,
556 struct tog_view *, int);
557 static const struct got_error* close_diff_view(struct tog_view *);
558 static const struct got_error *search_start_diff_view(struct tog_view *);
559 static const struct got_error *search_next_diff_view(struct tog_view *);
561 static const struct got_error *open_log_view(struct tog_view *,
562 struct got_object_id *, struct got_repository *,
563 const char *, const char *, int);
564 static const struct got_error * show_log_view(struct tog_view *);
565 static const struct got_error *input_log_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_log_view(struct tog_view *);
568 static const struct got_error *search_start_log_view(struct tog_view *);
569 static const struct got_error *search_next_log_view(struct tog_view *);
571 static const struct got_error *open_blame_view(struct tog_view *, char *,
572 struct got_object_id *, struct got_repository *);
573 static const struct got_error *show_blame_view(struct tog_view *);
574 static const struct got_error *input_blame_view(struct tog_view **,
575 struct tog_view *, int);
576 static const struct got_error *close_blame_view(struct tog_view *);
577 static const struct got_error *search_start_blame_view(struct tog_view *);
578 static const struct got_error *search_next_blame_view(struct tog_view *);
580 static const struct got_error *open_tree_view(struct tog_view *,
581 struct got_object_id *, const char *, struct got_repository *);
582 static const struct got_error *show_tree_view(struct tog_view *);
583 static const struct got_error *input_tree_view(struct tog_view **,
584 struct tog_view *, int);
585 static const struct got_error *close_tree_view(struct tog_view *);
586 static const struct got_error *search_start_tree_view(struct tog_view *);
587 static const struct got_error *search_next_tree_view(struct tog_view *);
589 static const struct got_error *open_ref_view(struct tog_view *,
590 struct got_repository *);
591 static const struct got_error *show_ref_view(struct tog_view *);
592 static const struct got_error *input_ref_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *close_ref_view(struct tog_view *);
595 static const struct got_error *search_start_ref_view(struct tog_view *);
596 static const struct got_error *search_next_ref_view(struct tog_view *);
598 static volatile sig_atomic_t tog_sigwinch_received;
599 static volatile sig_atomic_t tog_sigpipe_received;
600 static volatile sig_atomic_t tog_sigcont_received;
601 static volatile sig_atomic_t tog_sigint_received;
602 static volatile sig_atomic_t tog_sigterm_received;
605 tog_sigwinch(int signo)
607 tog_sigwinch_received = 1;
611 tog_sigpipe(int signo)
613 tog_sigpipe_received = 1;
617 tog_sigcont(int signo)
619 tog_sigcont_received = 1;
623 tog_sigint(int signo)
625 tog_sigint_received = 1;
629 tog_sigterm(int signo)
631 tog_sigterm_received = 1;
635 tog_fatal_signal_received(void)
637 return (tog_sigpipe_received ||
638 tog_sigint_received || tog_sigint_received);
642 static const struct got_error *
643 view_close(struct tog_view *view)
645 const struct got_error *err = NULL;
648 view_close(view->child);
652 err = view->close(view);
654 del_panel(view->panel);
656 delwin(view->window);
661 static struct tog_view *
662 view_open(int nlines, int ncols, int begin_y, int begin_x,
663 enum tog_view_type type)
665 struct tog_view *view = calloc(1, sizeof(*view));
673 view->nlines = nlines ? nlines : LINES - begin_y;
674 view->ncols = ncols ? ncols : COLS - begin_x;
675 view->begin_y = begin_y;
676 view->begin_x = begin_x;
677 view->window = newwin(nlines, ncols, begin_y, begin_x);
678 if (view->window == NULL) {
682 view->panel = new_panel(view->window);
683 if (view->panel == NULL ||
684 set_panel_userptr(view->panel, view) != OK) {
689 keypad(view->window, TRUE);
694 view_split_begin_x(int begin_x)
696 if (begin_x > 0 || COLS < 120)
698 return (COLS - MAX(COLS / 2, 80));
701 static const struct got_error *view_resize(struct tog_view *);
703 static const struct got_error *
704 view_splitscreen(struct tog_view *view)
706 const struct got_error *err = NULL;
709 view->begin_x = view_split_begin_x(0);
710 view->nlines = LINES;
711 view->ncols = COLS - view->begin_x;
714 err = view_resize(view);
718 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 return got_error_from_errno("mvwin");
724 static const struct got_error *
725 view_fullscreen(struct tog_view *view)
727 const struct got_error *err = NULL;
731 view->nlines = LINES;
735 err = view_resize(view);
739 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 return got_error_from_errno("mvwin");
746 view_is_parent_view(struct tog_view *view)
748 return view->parent == NULL;
752 view_is_splitscreen(struct tog_view *view)
754 return view->begin_x > 0;
758 static const struct got_error *
759 view_resize(struct tog_view *view)
763 if (view->lines > LINES)
764 nlines = view->nlines - (view->lines - LINES);
766 nlines = view->nlines + (LINES - view->lines);
768 if (view->cols > COLS)
769 ncols = view->ncols - (view->cols - COLS);
771 ncols = view->ncols + (COLS - view->cols);
773 if (view->child && view_is_splitscreen(view->child)) {
774 view->child->begin_x = view_split_begin_x(view->begin_x);
775 if (view->child->begin_x == 0) {
778 view_fullscreen(view->child);
779 if (view->child->focussed)
780 show_panel(view->child->panel);
782 show_panel(view->panel);
784 ncols = view->child->begin_x;
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
789 } else if (view->parent == NULL)
792 if (wresize(view->window, nlines, ncols) == ERR)
793 return got_error_from_errno("wresize");
794 if (replace_panel(view->panel, view->window) == ERR)
795 return got_error_from_errno("replace_panel");
796 wclear(view->window);
798 view->nlines = nlines;
806 static const struct got_error *
807 view_close_child(struct tog_view *view)
809 const struct got_error *err = NULL;
811 if (view->child == NULL)
814 err = view_close(view->child);
819 static const struct got_error *
820 view_set_child(struct tog_view *view, struct tog_view *child)
823 child->parent = view;
825 return view_resize(view);
834 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
835 cols = 80; /* Default */
841 resize_term(lines, cols);
844 static const struct got_error *
845 view_search_start(struct tog_view *view)
847 const struct got_error *err = NULL;
851 if (view->search_started) {
852 regfree(&view->regex);
854 memset(&view->regmatch, 0, sizeof(view->regmatch));
856 view->search_started = 0;
858 if (view->nlines < 1)
861 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
862 wclrtoeol(view->window);
866 ret = wgetnstr(view->window, pattern, sizeof(pattern));
872 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
873 err = view->search_start(view);
875 regfree(&view->regex);
878 view->search_started = 1;
879 view->searching = TOG_SEARCH_FORWARD;
880 view->search_next_done = 0;
881 view->search_next(view);
887 static const struct got_error *
888 view_input(struct tog_view **new, int *done, struct tog_view *view,
889 struct tog_view_list_head *views)
891 const struct got_error *err = NULL;
897 /* Clear "no matches" indicator. */
898 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
899 view->search_next_done == TOG_SEARCH_HAVE_NONE)
900 view->search_next_done = TOG_SEARCH_HAVE_MORE;
902 if (view->searching && !view->search_next_done) {
903 errcode = pthread_mutex_unlock(&tog_mutex);
905 return got_error_set_errno(errcode,
906 "pthread_mutex_unlock");
908 errcode = pthread_mutex_lock(&tog_mutex);
910 return got_error_set_errno(errcode,
911 "pthread_mutex_lock");
912 view->search_next(view);
916 nodelay(stdscr, FALSE);
917 /* Allow threads to make progress while we are waiting for input. */
918 errcode = pthread_mutex_unlock(&tog_mutex);
920 return got_error_set_errno(errcode, "pthread_mutex_unlock");
921 ch = wgetch(view->window);
922 errcode = pthread_mutex_lock(&tog_mutex);
924 return got_error_set_errno(errcode, "pthread_mutex_lock");
925 nodelay(stdscr, TRUE);
927 if (tog_sigwinch_received || tog_sigcont_received) {
929 tog_sigwinch_received = 0;
930 tog_sigcont_received = 0;
931 TAILQ_FOREACH(v, views, entry) {
932 err = view_resize(v);
935 err = v->input(new, v, KEY_RESIZE);
939 err = view_resize(v->child);
942 err = v->child->input(new, v->child,
954 view->child->focussed = 1;
955 view->focus_child = 1;
956 } else if (view->parent) {
958 view->parent->focussed = 1;
959 view->parent->focus_child = 0;
960 if (!view_is_splitscreen(view))
961 err = view_fullscreen(view->parent);
965 err = view->input(new, view, ch);
972 if (view_is_parent_view(view)) {
973 if (view->child == NULL)
975 if (view_is_splitscreen(view->child)) {
977 view->child->focussed = 1;
978 err = view_fullscreen(view->child);
980 err = view_splitscreen(view->child);
983 err = view->child->input(new, view->child,
986 if (view_is_splitscreen(view)) {
987 view->parent->focussed = 0;
989 err = view_fullscreen(view);
991 err = view_splitscreen(view);
993 err = view_resize(view->parent);
997 err = view->input(new, view, KEY_RESIZE);
1003 if (view->search_start)
1004 view_search_start(view);
1006 err = view->input(new, view, ch);
1010 if (view->search_started && view->search_next) {
1011 view->searching = (ch == 'n' ?
1012 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1013 view->search_next_done = 0;
1014 view->search_next(view);
1016 err = view->input(new, view, ch);
1019 err = view->input(new, view, ch);
1027 view_vborder(struct tog_view *view)
1030 const struct tog_view *view_above;
1033 return view_vborder(view->parent);
1035 panel = panel_above(view->panel);
1039 view_above = panel_userptr(panel);
1040 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1041 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1045 view_needs_focus_indication(struct tog_view *view)
1047 if (view_is_parent_view(view)) {
1048 if (view->child == NULL || view->child->focussed)
1050 if (!view_is_splitscreen(view->child))
1052 } else if (!view_is_splitscreen(view))
1055 return view->focussed;
1058 static const struct got_error *
1059 view_loop(struct tog_view *view)
1061 const struct got_error *err = NULL;
1062 struct tog_view_list_head views;
1063 struct tog_view *new_view;
1064 int fast_refresh = 10;
1065 int done = 0, errcode;
1067 errcode = pthread_mutex_lock(&tog_mutex);
1069 return got_error_set_errno(errcode, "pthread_mutex_lock");
1072 TAILQ_INSERT_HEAD(&views, view, entry);
1075 err = view->show(view);
1080 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1081 /* Refresh fast during initialization, then become slower. */
1082 if (fast_refresh && fast_refresh-- == 0)
1083 halfdelay(10); /* switch to once per second */
1085 err = view_input(&new_view, &done, view, &views);
1089 struct tog_view *v, *prev = NULL;
1091 if (view_is_parent_view(view))
1092 prev = TAILQ_PREV(view, tog_view_list_head,
1094 else if (view->parent)
1095 prev = view->parent;
1098 view->parent->child = NULL;
1099 view->parent->focus_child = 0;
1101 err = view_resize(view->parent);
1105 TAILQ_REMOVE(&views, view, entry);
1107 err = view_close(view);
1112 TAILQ_FOREACH(v, &views, entry) {
1116 if (view == NULL && new_view == NULL) {
1117 /* No view has focus. Try to pick one. */
1120 else if (!TAILQ_EMPTY(&views)) {
1121 view = TAILQ_LAST(&views,
1122 tog_view_list_head);
1125 if (view->focus_child) {
1126 view->child->focussed = 1;
1134 struct tog_view *v, *t;
1135 /* Only allow one parent view per type. */
1136 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1137 if (v->type != new_view->type)
1139 TAILQ_REMOVE(&views, v, entry);
1140 err = view_close(v);
1145 TAILQ_INSERT_TAIL(&views, new_view, entry);
1149 if (view_is_parent_view(view)) {
1150 if (view->child && view->child->focussed)
1153 if (view->parent && view->parent->focussed)
1154 view = view->parent;
1156 show_panel(view->panel);
1157 if (view->child && view_is_splitscreen(view->child))
1158 show_panel(view->child->panel);
1159 if (view->parent && view_is_splitscreen(view)) {
1160 err = view->parent->show(view->parent);
1164 err = view->show(view);
1168 err = view->child->show(view->child);
1177 while (!TAILQ_EMPTY(&views)) {
1178 view = TAILQ_FIRST(&views);
1179 TAILQ_REMOVE(&views, view, entry);
1183 errcode = pthread_mutex_unlock(&tog_mutex);
1184 if (errcode && err == NULL)
1185 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1195 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1200 /* Create newly allocated wide-character string equivalent to a byte string. */
1201 static const struct got_error *
1202 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1205 const struct got_error *err = NULL;
1208 *wlen = mbstowcs(NULL, s, 0);
1209 if (*wlen == (size_t)-1) {
1211 if (errno != EILSEQ)
1212 return got_error_from_errno("mbstowcs");
1214 /* byte string invalid in current encoding; try to "fix" it */
1215 err = got_mbsavis(&vis, &vislen, s);
1218 *wlen = mbstowcs(NULL, vis, 0);
1219 if (*wlen == (size_t)-1) {
1220 err = got_error_from_errno("mbstowcs"); /* give up */
1225 *ws = calloc(*wlen + 1, sizeof(**ws));
1227 err = got_error_from_errno("calloc");
1231 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1232 err = got_error_from_errno("mbstowcs");
1243 static const struct got_error *
1244 expand_tab(char **ptr, const char *src)
1247 size_t len, n, idx = 0, sz = 0;
1250 n = len = strlen(src);
1251 dst = malloc(n + 1);
1253 return got_error_from_errno("malloc");
1255 while (idx < len && src[idx]) {
1256 const char c = src[idx];
1259 size_t nb = TABSIZE - sz % TABSIZE;
1262 p = realloc(dst, n + nb);
1265 return got_error_from_errno("realloc");
1270 memset(dst + sz, ' ', nb);
1273 dst[sz++] = src[idx];
1283 * Advance at most n columns from wline starting at offset off.
1284 * Return the index to the first character after the span operation.
1285 * Return the combined column width of all spanned wide character in
1289 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1291 int width, i, cols = 0;
1298 for (i = off; wline[i] != L'\0'; ++i) {
1299 if (wline[i] == L'\t')
1300 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1302 width = wcwidth(wline[i]);
1309 if (cols + width > n)
1319 * Format a line for display, ensuring that it won't overflow a width limit.
1320 * With scrolling, the width returned refers to the scrolled version of the
1321 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1323 static const struct got_error *
1324 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1325 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1327 const struct got_error *err = NULL;
1329 wchar_t *wline = NULL;
1338 err = expand_tab(&exstr, line);
1343 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1348 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1350 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1351 wline[wlen - 1] = L'\0';
1354 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1355 wline[wlen - 1] = L'\0';
1359 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1365 *scrollxp = scrollx;
1373 static const struct got_error*
1374 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1375 struct got_object_id *id, struct got_repository *repo)
1377 static const struct got_error *err = NULL;
1378 struct got_reflist_entry *re;
1384 TAILQ_FOREACH(re, refs, entry) {
1385 struct got_tag_object *tag = NULL;
1386 struct got_object_id *ref_id;
1389 name = got_ref_get_name(re->ref);
1390 if (strcmp(name, GOT_REF_HEAD) == 0)
1392 if (strncmp(name, "refs/", 5) == 0)
1394 if (strncmp(name, "got/", 4) == 0 &&
1395 strncmp(name, "got/backup/", 11) != 0)
1397 if (strncmp(name, "heads/", 6) == 0)
1399 if (strncmp(name, "remotes/", 8) == 0) {
1401 s = strstr(name, "/" GOT_REF_HEAD);
1402 if (s != NULL && s[strlen(s)] == '\0')
1405 err = got_ref_resolve(&ref_id, repo, re->ref);
1408 if (strncmp(name, "tags/", 5) == 0) {
1409 err = got_object_open_as_tag(&tag, repo, ref_id);
1411 if (err->code != GOT_ERR_OBJ_TYPE) {
1415 /* Ref points at something other than a tag. */
1420 cmp = got_object_id_cmp(tag ?
1421 got_object_tag_get_object_id(tag) : ref_id, id);
1424 got_object_tag_close(tag);
1428 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1429 s ? ", " : "", name) == -1) {
1430 err = got_error_from_errno("asprintf");
1441 static const struct got_error *
1442 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1447 smallerthan = strchr(author, '<');
1448 if (smallerthan && smallerthan[1] != '\0')
1449 author = smallerthan + 1;
1450 author[strcspn(author, "@>")] = '\0';
1451 return format_line(wauthor, author_width, NULL, author, 0, limit,
1455 static const struct got_error *
1456 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1457 struct got_object_id *id, const size_t date_display_cols,
1458 int author_display_cols)
1460 struct tog_log_view_state *s = &view->state.log;
1461 const struct got_error *err = NULL;
1462 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1463 char *logmsg0 = NULL, *logmsg = NULL;
1464 char *author = NULL;
1465 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1466 int author_width, logmsg_width;
1467 char *newline, *line = NULL;
1468 int col, limit, scrollx;
1469 const int avail = view->ncols;
1471 time_t committer_time;
1472 struct tog_color *tc;
1474 committer_time = got_object_commit_get_committer_time(commit);
1475 if (gmtime_r(&committer_time, &tm) == NULL)
1476 return got_error_from_errno("gmtime_r");
1477 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1478 return got_error(GOT_ERR_NO_SPACE);
1480 if (avail <= date_display_cols)
1481 limit = MIN(sizeof(datebuf) - 1, avail);
1483 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1484 tc = get_color(&s->colors, TOG_COLOR_DATE);
1486 wattr_on(view->window,
1487 COLOR_PAIR(tc->colorpair), NULL);
1488 waddnstr(view->window, datebuf, limit);
1490 wattr_off(view->window,
1491 COLOR_PAIR(tc->colorpair), NULL);
1498 err = got_object_id_str(&id_str, id);
1501 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1503 wattr_on(view->window,
1504 COLOR_PAIR(tc->colorpair), NULL);
1505 wprintw(view->window, "%.8s ", id_str);
1507 wattr_off(view->window,
1508 COLOR_PAIR(tc->colorpair), NULL);
1515 author = strdup(got_object_commit_get_author(commit));
1516 if (author == NULL) {
1517 err = got_error_from_errno("strdup");
1520 err = format_author(&wauthor, &author_width, author, avail - col, col);
1523 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1525 wattr_on(view->window,
1526 COLOR_PAIR(tc->colorpair), NULL);
1527 waddwstr(view->window, wauthor);
1529 wattr_off(view->window,
1530 COLOR_PAIR(tc->colorpair), NULL);
1531 col += author_width;
1532 while (col < avail && author_width < author_display_cols + 2) {
1533 waddch(view->window, ' ');
1540 err = got_object_commit_get_logmsg(&logmsg0, commit);
1544 while (*logmsg == '\n')
1546 newline = strchr(logmsg, '\n');
1549 limit = avail - col;
1550 if (view->child && limit > 0)
1551 limit--; /* for the border */
1552 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1556 waddwstr(view->window, &wlogmsg[scrollx]);
1557 col += MAX(logmsg_width, 0);
1558 while (col < avail) {
1559 waddch(view->window, ' ');
1571 static struct commit_queue_entry *
1572 alloc_commit_queue_entry(struct got_commit_object *commit,
1573 struct got_object_id *id)
1575 struct commit_queue_entry *entry;
1577 entry = calloc(1, sizeof(*entry));
1582 entry->commit = commit;
1587 pop_commit(struct commit_queue *commits)
1589 struct commit_queue_entry *entry;
1591 entry = TAILQ_FIRST(&commits->head);
1592 TAILQ_REMOVE(&commits->head, entry, entry);
1593 got_object_commit_close(entry->commit);
1594 commits->ncommits--;
1595 /* Don't free entry->id! It is owned by the commit graph. */
1600 free_commits(struct commit_queue *commits)
1602 while (!TAILQ_EMPTY(&commits->head))
1603 pop_commit(commits);
1606 static const struct got_error *
1607 match_commit(int *have_match, struct got_object_id *id,
1608 struct got_commit_object *commit, regex_t *regex)
1610 const struct got_error *err = NULL;
1611 regmatch_t regmatch;
1612 char *id_str = NULL, *logmsg = NULL;
1616 err = got_object_id_str(&id_str, id);
1620 err = got_object_commit_get_logmsg(&logmsg, commit);
1624 if (regexec(regex, got_object_commit_get_author(commit), 1,
1625 ®match, 0) == 0 ||
1626 regexec(regex, got_object_commit_get_committer(commit), 1,
1627 ®match, 0) == 0 ||
1628 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1629 regexec(regex, logmsg, 1, ®match, 0) == 0)
1637 static const struct got_error *
1638 queue_commits(struct tog_log_thread_args *a)
1640 const struct got_error *err = NULL;
1643 * We keep all commits open throughout the lifetime of the log
1644 * view in order to avoid having to re-fetch commits from disk
1645 * while updating the display.
1648 struct got_object_id *id;
1649 struct got_commit_object *commit;
1650 struct commit_queue_entry *entry;
1653 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1655 if (err || id == NULL)
1658 err = got_object_open_as_commit(&commit, a->repo, id);
1661 entry = alloc_commit_queue_entry(commit, id);
1662 if (entry == NULL) {
1663 err = got_error_from_errno("alloc_commit_queue_entry");
1667 errcode = pthread_mutex_lock(&tog_mutex);
1669 err = got_error_set_errno(errcode,
1670 "pthread_mutex_lock");
1674 entry->idx = a->commits->ncommits;
1675 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1676 a->commits->ncommits++;
1678 if (*a->searching == TOG_SEARCH_FORWARD &&
1679 !*a->search_next_done) {
1681 err = match_commit(&have_match, id, commit, a->regex);
1685 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1688 errcode = pthread_mutex_unlock(&tog_mutex);
1689 if (errcode && err == NULL)
1690 err = got_error_set_errno(errcode,
1691 "pthread_mutex_unlock");
1694 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1700 select_commit(struct tog_log_view_state *s)
1702 struct commit_queue_entry *entry;
1705 entry = s->first_displayed_entry;
1707 if (ncommits == s->selected) {
1708 s->selected_entry = entry;
1711 entry = TAILQ_NEXT(entry, entry);
1716 static const struct got_error *
1717 draw_commits(struct tog_view *view)
1719 const struct got_error *err = NULL;
1720 struct tog_log_view_state *s = &view->state.log;
1721 struct commit_queue_entry *entry = s->selected_entry;
1722 const int limit = view->nlines;
1724 int ncommits, author_cols = 4;
1725 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1726 char *refs_str = NULL;
1728 struct tog_color *tc;
1729 static const size_t date_display_cols = 12;
1731 if (s->selected_entry &&
1732 !(view->searching && view->search_next_done == 0)) {
1733 struct got_reflist_head *refs;
1734 err = got_object_id_str(&id_str, s->selected_entry->id);
1737 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1738 s->selected_entry->id);
1740 err = build_refs_str(&refs_str, refs,
1741 s->selected_entry->id, s->repo);
1747 if (s->thread_args.commits_needed == 0)
1748 halfdelay(10); /* disable fast refresh */
1750 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1751 if (asprintf(&ncommits_str, " [%d/%d] %s",
1752 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1753 (view->searching && !view->search_next_done) ?
1754 "searching..." : "loading...") == -1) {
1755 err = got_error_from_errno("asprintf");
1759 const char *search_str = NULL;
1761 if (view->searching) {
1762 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1763 search_str = "no more matches";
1764 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1765 search_str = "no matches found";
1766 else if (!view->search_next_done)
1767 search_str = "searching...";
1770 if (asprintf(&ncommits_str, " [%d/%d] %s",
1771 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1772 search_str ? search_str :
1773 (refs_str ? refs_str : "")) == -1) {
1774 err = got_error_from_errno("asprintf");
1779 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1780 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1781 "........................................",
1782 s->in_repo_path, ncommits_str) == -1) {
1783 err = got_error_from_errno("asprintf");
1787 } else if (asprintf(&header, "commit %s%s",
1788 id_str ? id_str : "........................................",
1789 ncommits_str) == -1) {
1790 err = got_error_from_errno("asprintf");
1794 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1798 werase(view->window);
1800 if (view_needs_focus_indication(view))
1801 wstandout(view->window);
1802 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1804 wattr_on(view->window,
1805 COLOR_PAIR(tc->colorpair), NULL);
1806 waddwstr(view->window, wline);
1808 wattr_off(view->window,
1809 COLOR_PAIR(tc->colorpair), NULL);
1810 while (width < view->ncols) {
1811 waddch(view->window, ' ');
1814 if (view_needs_focus_indication(view))
1815 wstandend(view->window);
1820 /* Grow author column size if necessary, and set view->maxx. */
1821 entry = s->first_displayed_entry;
1825 char *author, *eol, *msg, *msg0;
1826 wchar_t *wauthor, *wmsg;
1828 if (ncommits >= limit - 1)
1830 author = strdup(got_object_commit_get_author(entry->commit));
1831 if (author == NULL) {
1832 err = got_error_from_errno("strdup");
1835 err = format_author(&wauthor, &width, author, COLS,
1837 if (author_cols < width)
1838 author_cols = width;
1841 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1845 while (*msg == '\n')
1847 if ((eol = strchr(msg, '\n')))
1849 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1850 date_display_cols + author_cols, 0);
1853 view->maxx = MAX(view->maxx, width);
1857 entry = TAILQ_NEXT(entry, entry);
1860 entry = s->first_displayed_entry;
1861 s->last_displayed_entry = s->first_displayed_entry;
1864 if (ncommits >= limit - 1)
1866 if (ncommits == s->selected)
1867 wstandout(view->window);
1868 err = draw_commit(view, entry->commit, entry->id,
1869 date_display_cols, author_cols);
1870 if (ncommits == s->selected)
1871 wstandend(view->window);
1875 s->last_displayed_entry = entry;
1876 entry = TAILQ_NEXT(entry, entry);
1889 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1891 struct commit_queue_entry *entry;
1894 entry = TAILQ_FIRST(&s->commits.head);
1895 if (s->first_displayed_entry == entry)
1898 entry = s->first_displayed_entry;
1899 while (entry && nscrolled < maxscroll) {
1900 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1902 s->first_displayed_entry = entry;
1908 static const struct got_error *
1909 trigger_log_thread(struct tog_view *view, int wait)
1911 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1914 halfdelay(1); /* fast refresh while loading commits */
1916 while (ta->commits_needed > 0 || ta->load_all) {
1917 if (ta->log_complete)
1920 /* Wake the log thread. */
1921 errcode = pthread_cond_signal(&ta->need_commits);
1923 return got_error_set_errno(errcode,
1924 "pthread_cond_signal");
1927 * The mutex will be released while the view loop waits
1928 * in wgetch(), at which time the log thread will run.
1933 /* Display progress update in log view. */
1934 show_log_view(view);
1938 /* Wait right here while next commit is being loaded. */
1939 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1941 return got_error_set_errno(errcode,
1942 "pthread_cond_wait");
1944 /* Display progress update in log view. */
1945 show_log_view(view);
1953 static const struct got_error *
1954 log_scroll_down(struct tog_view *view, int maxscroll)
1956 struct tog_log_view_state *s = &view->state.log;
1957 const struct got_error *err = NULL;
1958 struct commit_queue_entry *pentry;
1959 int nscrolled = 0, ncommits_needed;
1961 if (s->last_displayed_entry == NULL)
1964 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1965 if (s->commits.ncommits < ncommits_needed &&
1966 !s->thread_args.log_complete) {
1968 * Ask the log thread for required amount of commits.
1970 s->thread_args.commits_needed += maxscroll;
1971 err = trigger_log_thread(view, 1);
1977 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1981 s->last_displayed_entry = pentry;
1983 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1986 s->first_displayed_entry = pentry;
1987 } while (++nscrolled < maxscroll);
1992 static const struct got_error *
1993 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1994 struct got_commit_object *commit, struct got_object_id *commit_id,
1995 struct tog_view *log_view, struct got_repository *repo)
1997 const struct got_error *err;
1998 struct got_object_qid *parent_id;
1999 struct tog_view *diff_view;
2001 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2002 if (diff_view == NULL)
2003 return got_error_from_errno("view_open");
2005 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2006 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2007 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2009 *new_view = diff_view;
2013 static const struct got_error *
2014 tree_view_visit_subtree(struct tog_tree_view_state *s,
2015 struct got_tree_object *subtree)
2017 struct tog_parent_tree *parent;
2019 parent = calloc(1, sizeof(*parent));
2021 return got_error_from_errno("calloc");
2023 parent->tree = s->tree;
2024 parent->first_displayed_entry = s->first_displayed_entry;
2025 parent->selected_entry = s->selected_entry;
2026 parent->selected = s->selected;
2027 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2030 s->first_displayed_entry = NULL;
2034 static const struct got_error *
2035 tree_view_walk_path(struct tog_tree_view_state *s,
2036 struct got_commit_object *commit, const char *path)
2038 const struct got_error *err = NULL;
2039 struct got_tree_object *tree = NULL;
2041 char *slash, *subpath = NULL;
2043 /* Walk the path and open corresponding tree objects. */
2046 struct got_tree_entry *te;
2047 struct got_object_id *tree_id;
2053 /* Ensure the correct subtree entry is selected. */
2054 slash = strchr(p, '/');
2056 te_name = strdup(p);
2058 te_name = strndup(p, slash - p);
2059 if (te_name == NULL) {
2060 err = got_error_from_errno("strndup");
2063 te = got_object_tree_find_entry(s->tree, te_name);
2065 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2070 s->first_displayed_entry = s->selected_entry = te;
2072 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2073 break; /* jump to this file's entry */
2075 slash = strchr(p, '/');
2077 subpath = strndup(path, slash - path);
2079 subpath = strdup(path);
2080 if (subpath == NULL) {
2081 err = got_error_from_errno("strdup");
2085 err = got_object_id_by_path(&tree_id, s->repo, commit,
2090 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2095 err = tree_view_visit_subtree(s, tree);
2097 got_object_tree_close(tree);
2111 static const struct got_error *
2112 browse_commit_tree(struct tog_view **new_view, int begin_x,
2113 struct commit_queue_entry *entry, const char *path,
2114 const char *head_ref_name, struct got_repository *repo)
2116 const struct got_error *err = NULL;
2117 struct tog_tree_view_state *s;
2118 struct tog_view *tree_view;
2120 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2121 if (tree_view == NULL)
2122 return got_error_from_errno("view_open");
2124 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2127 s = &tree_view->state.tree;
2129 *new_view = tree_view;
2131 if (got_path_is_root_dir(path))
2134 return tree_view_walk_path(s, entry->commit, path);
2137 static const struct got_error *
2138 block_signals_used_by_main_thread(void)
2143 if (sigemptyset(&sigset) == -1)
2144 return got_error_from_errno("sigemptyset");
2146 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2147 if (sigaddset(&sigset, SIGWINCH) == -1)
2148 return got_error_from_errno("sigaddset");
2149 if (sigaddset(&sigset, SIGCONT) == -1)
2150 return got_error_from_errno("sigaddset");
2151 if (sigaddset(&sigset, SIGINT) == -1)
2152 return got_error_from_errno("sigaddset");
2153 if (sigaddset(&sigset, SIGTERM) == -1)
2154 return got_error_from_errno("sigaddset");
2156 /* ncurses handles SIGTSTP */
2157 if (sigaddset(&sigset, SIGTSTP) == -1)
2158 return got_error_from_errno("sigaddset");
2160 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2162 return got_error_set_errno(errcode, "pthread_sigmask");
2168 log_thread(void *arg)
2170 const struct got_error *err = NULL;
2172 struct tog_log_thread_args *a = arg;
2175 err = block_signals_used_by_main_thread();
2179 while (!done && !err && !tog_fatal_signal_received()) {
2180 err = queue_commits(a);
2182 if (err->code != GOT_ERR_ITER_COMPLETED)
2186 } else if (a->commits_needed > 0 && !a->load_all)
2187 a->commits_needed--;
2189 errcode = pthread_mutex_lock(&tog_mutex);
2191 err = got_error_set_errno(errcode,
2192 "pthread_mutex_lock");
2194 } else if (*a->quit)
2196 else if (*a->first_displayed_entry == NULL) {
2197 *a->first_displayed_entry =
2198 TAILQ_FIRST(&a->commits->head);
2199 *a->selected_entry = *a->first_displayed_entry;
2202 errcode = pthread_cond_signal(&a->commit_loaded);
2204 err = got_error_set_errno(errcode,
2205 "pthread_cond_signal");
2206 pthread_mutex_unlock(&tog_mutex);
2211 a->commits_needed = 0;
2213 if (a->commits_needed == 0 && !a->load_all) {
2214 errcode = pthread_cond_wait(&a->need_commits,
2217 err = got_error_set_errno(errcode,
2218 "pthread_cond_wait");
2224 errcode = pthread_mutex_unlock(&tog_mutex);
2225 if (errcode && err == NULL)
2226 err = got_error_set_errno(errcode,
2227 "pthread_mutex_unlock");
2229 a->log_complete = 1;
2233 static const struct got_error *
2234 stop_log_thread(struct tog_log_view_state *s)
2236 const struct got_error *err = NULL;
2241 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2243 return got_error_set_errno(errcode,
2244 "pthread_cond_signal");
2245 errcode = pthread_mutex_unlock(&tog_mutex);
2247 return got_error_set_errno(errcode,
2248 "pthread_mutex_unlock");
2249 errcode = pthread_join(s->thread, (void **)&err);
2251 return got_error_set_errno(errcode, "pthread_join");
2252 errcode = pthread_mutex_lock(&tog_mutex);
2254 return got_error_set_errno(errcode,
2255 "pthread_mutex_lock");
2259 if (s->thread_args.repo) {
2260 err = got_repo_close(s->thread_args.repo);
2261 s->thread_args.repo = NULL;
2264 if (s->thread_args.pack_fds) {
2265 const struct got_error *pack_err =
2266 got_repo_pack_fds_close(s->thread_args.pack_fds);
2269 s->thread_args.pack_fds = NULL;
2272 if (s->thread_args.graph) {
2273 got_commit_graph_close(s->thread_args.graph);
2274 s->thread_args.graph = NULL;
2280 static const struct got_error *
2281 close_log_view(struct tog_view *view)
2283 const struct got_error *err = NULL;
2284 struct tog_log_view_state *s = &view->state.log;
2287 err = stop_log_thread(s);
2289 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2290 if (errcode && err == NULL)
2291 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2293 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2294 if (errcode && err == NULL)
2295 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2297 free_commits(&s->commits);
2298 free(s->in_repo_path);
2299 s->in_repo_path = NULL;
2302 free(s->head_ref_name);
2303 s->head_ref_name = NULL;
2307 static const struct got_error *
2308 search_start_log_view(struct tog_view *view)
2310 struct tog_log_view_state *s = &view->state.log;
2312 s->matched_entry = NULL;
2313 s->search_entry = NULL;
2317 static const struct got_error *
2318 search_next_log_view(struct tog_view *view)
2320 const struct got_error *err = NULL;
2321 struct tog_log_view_state *s = &view->state.log;
2322 struct commit_queue_entry *entry;
2324 /* Display progress update in log view. */
2325 show_log_view(view);
2329 if (s->search_entry) {
2331 errcode = pthread_mutex_unlock(&tog_mutex);
2333 return got_error_set_errno(errcode,
2334 "pthread_mutex_unlock");
2335 ch = wgetch(view->window);
2336 errcode = pthread_mutex_lock(&tog_mutex);
2338 return got_error_set_errno(errcode,
2339 "pthread_mutex_lock");
2340 if (ch == KEY_BACKSPACE) {
2341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2344 if (view->searching == TOG_SEARCH_FORWARD)
2345 entry = TAILQ_NEXT(s->search_entry, entry);
2347 entry = TAILQ_PREV(s->search_entry,
2348 commit_queue_head, entry);
2349 } else if (s->matched_entry) {
2350 int matched_idx = s->matched_entry->idx;
2351 int selected_idx = s->selected_entry->idx;
2354 * If the user has moved the cursor after we hit a match,
2355 * the position from where we should continue searching
2356 * might have changed.
2358 if (view->searching == TOG_SEARCH_FORWARD) {
2359 if (matched_idx > selected_idx)
2360 entry = TAILQ_NEXT(s->selected_entry, entry);
2362 entry = TAILQ_NEXT(s->matched_entry, entry);
2364 if (matched_idx < selected_idx)
2365 entry = TAILQ_PREV(s->selected_entry,
2366 commit_queue_head, entry);
2368 entry = TAILQ_PREV(s->matched_entry,
2369 commit_queue_head, entry);
2372 entry = s->selected_entry;
2378 if (entry == NULL) {
2379 if (s->thread_args.log_complete ||
2380 view->searching == TOG_SEARCH_BACKWARD) {
2381 view->search_next_done =
2382 (s->matched_entry == NULL ?
2383 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2384 s->search_entry = NULL;
2388 * Poke the log thread for more commits and return,
2389 * allowing the main loop to make progress. Search
2390 * will resume at s->search_entry once we come back.
2392 s->thread_args.commits_needed++;
2393 return trigger_log_thread(view, 0);
2396 err = match_commit(&have_match, entry->id, entry->commit,
2401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2402 s->matched_entry = entry;
2406 s->search_entry = entry;
2407 if (view->searching == TOG_SEARCH_FORWARD)
2408 entry = TAILQ_NEXT(entry, entry);
2410 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2413 if (s->matched_entry) {
2414 int cur = s->selected_entry->idx;
2415 while (cur < s->matched_entry->idx) {
2416 err = input_log_view(NULL, view, KEY_DOWN);
2421 while (cur > s->matched_entry->idx) {
2422 err = input_log_view(NULL, view, KEY_UP);
2429 s->search_entry = NULL;
2434 static const struct got_error *
2435 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2436 struct got_repository *repo, const char *head_ref_name,
2437 const char *in_repo_path, int log_branches)
2439 const struct got_error *err = NULL;
2440 struct tog_log_view_state *s = &view->state.log;
2441 struct got_repository *thread_repo = NULL;
2442 struct got_commit_graph *thread_graph = NULL;
2445 if (in_repo_path != s->in_repo_path) {
2446 free(s->in_repo_path);
2447 s->in_repo_path = strdup(in_repo_path);
2448 if (s->in_repo_path == NULL)
2449 return got_error_from_errno("strdup");
2452 /* The commit queue only contains commits being displayed. */
2453 TAILQ_INIT(&s->commits.head);
2454 s->commits.ncommits = 0;
2457 if (head_ref_name) {
2458 s->head_ref_name = strdup(head_ref_name);
2459 if (s->head_ref_name == NULL) {
2460 err = got_error_from_errno("strdup");
2464 s->start_id = got_object_id_dup(start_id);
2465 if (s->start_id == NULL) {
2466 err = got_error_from_errno("got_object_id_dup");
2469 s->log_branches = log_branches;
2471 STAILQ_INIT(&s->colors);
2472 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2473 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2474 get_color_value("TOG_COLOR_COMMIT"));
2477 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2478 get_color_value("TOG_COLOR_AUTHOR"));
2480 free_colors(&s->colors);
2483 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2484 get_color_value("TOG_COLOR_DATE"));
2486 free_colors(&s->colors);
2491 view->show = show_log_view;
2492 view->input = input_log_view;
2493 view->close = close_log_view;
2494 view->search_start = search_start_log_view;
2495 view->search_next = search_next_log_view;
2497 if (s->thread_args.pack_fds == NULL) {
2498 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2502 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2503 s->thread_args.pack_fds);
2506 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2510 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2511 s->repo, NULL, NULL);
2515 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2517 err = got_error_set_errno(errcode, "pthread_cond_init");
2520 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2522 err = got_error_set_errno(errcode, "pthread_cond_init");
2526 s->thread_args.commits_needed = view->nlines;
2527 s->thread_args.graph = thread_graph;
2528 s->thread_args.commits = &s->commits;
2529 s->thread_args.in_repo_path = s->in_repo_path;
2530 s->thread_args.start_id = s->start_id;
2531 s->thread_args.repo = thread_repo;
2532 s->thread_args.log_complete = 0;
2533 s->thread_args.quit = &s->quit;
2534 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2535 s->thread_args.selected_entry = &s->selected_entry;
2536 s->thread_args.searching = &view->searching;
2537 s->thread_args.search_next_done = &view->search_next_done;
2538 s->thread_args.regex = &view->regex;
2541 close_log_view(view);
2545 static const struct got_error *
2546 show_log_view(struct tog_view *view)
2548 const struct got_error *err;
2549 struct tog_log_view_state *s = &view->state.log;
2551 if (s->thread == NULL) {
2552 int errcode = pthread_create(&s->thread, NULL, log_thread,
2555 return got_error_set_errno(errcode, "pthread_create");
2556 if (s->thread_args.commits_needed > 0) {
2557 err = trigger_log_thread(view, 1);
2563 return draw_commits(view);
2566 static const struct got_error *
2567 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2569 const struct got_error *err = NULL;
2570 struct tog_log_view_state *s = &view->state.log;
2571 struct tog_view *diff_view = NULL, *tree_view = NULL;
2572 struct tog_view *ref_view = NULL;
2573 struct commit_queue_entry *entry;
2574 int begin_x = 0, n, nscroll = view->nlines - 1;
2576 if (s->thread_args.load_all) {
2577 if (ch == KEY_BACKSPACE)
2578 s->thread_args.load_all = 0;
2579 else if (s->thread_args.log_complete) {
2580 s->thread_args.load_all = 0;
2581 log_scroll_down(view, s->commits.ncommits);
2582 s->selected = MIN(view->nlines - 2,
2583 s->commits.ncommits - 1);
2597 view->x = MAX(view->maxx - view->ncols / 2, 0);
2601 if (view->x + view->ncols / 2 < view->maxx)
2602 view->x += 2; /* move two columns right */
2606 view->x -= MIN(view->x, 2); /* move two columns back */
2613 if (s->first_displayed_entry == NULL)
2615 if (s->selected > 0)
2618 log_scroll_up(s, 1);
2624 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2634 if (s->first_displayed_entry == NULL)
2636 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2637 s->selected = MAX(0, s->selected - nscroll - 1);
2639 log_scroll_up(s, nscroll);
2647 if (s->first_displayed_entry == NULL)
2649 if (s->selected < MIN(view->nlines - 2,
2650 s->commits.ncommits - 1))
2653 err = log_scroll_down(view, 1);
2661 /* We don't know yet how many commits, so we're forced to
2662 * traverse them all. */
2663 if (!s->thread_args.log_complete) {
2664 s->thread_args.load_all = 1;
2665 return trigger_log_thread(view, 0);
2669 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2670 for (n = 0; n < view->nlines - 1; n++) {
2673 s->first_displayed_entry = entry;
2674 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2677 s->selected = n - 1;
2688 struct commit_queue_entry *first;
2689 first = s->first_displayed_entry;
2692 err = log_scroll_down(view, nscroll);
2695 if (first == s->first_displayed_entry &&
2696 s->selected < MIN(view->nlines - 2,
2697 s->commits.ncommits - 1)) {
2698 /* can't scroll further down */
2699 s->selected += MIN(s->last_displayed_entry->idx -
2700 s->selected_entry->idx, nscroll + 1);
2706 if (s->selected > view->nlines - 2)
2707 s->selected = view->nlines - 2;
2708 if (s->selected > s->commits.ncommits - 1)
2709 s->selected = s->commits.ncommits - 1;
2711 if (s->commits.ncommits < view->nlines - 1 &&
2712 !s->thread_args.log_complete) {
2713 s->thread_args.commits_needed += (view->nlines - 1) -
2714 s->commits.ncommits;
2715 err = trigger_log_thread(view, 1);
2721 if (s->selected_entry == NULL)
2723 if (view_is_parent_view(view))
2724 begin_x = view_split_begin_x(view->begin_x);
2725 err = open_diff_view_for_commit(&diff_view, begin_x,
2726 s->selected_entry->commit, s->selected_entry->id,
2731 diff_view->focussed = 1;
2732 if (view_is_parent_view(view)) {
2733 err = view_close_child(view);
2736 err = view_set_child(view, diff_view);
2739 view->focus_child = 1;
2741 *new_view = diff_view;
2744 if (s->selected_entry == NULL)
2746 if (view_is_parent_view(view))
2747 begin_x = view_split_begin_x(view->begin_x);
2748 err = browse_commit_tree(&tree_view, begin_x,
2749 s->selected_entry, s->in_repo_path, s->head_ref_name,
2754 tree_view->focussed = 1;
2755 if (view_is_parent_view(view)) {
2756 err = view_close_child(view);
2759 err = view_set_child(view, tree_view);
2762 view->focus_child = 1;
2764 *new_view = tree_view;
2769 if (ch == KEY_BACKSPACE &&
2770 got_path_is_root_dir(s->in_repo_path))
2772 err = stop_log_thread(s);
2775 if (ch == KEY_BACKSPACE) {
2777 err = got_path_dirname(&parent_path, s->in_repo_path);
2780 free(s->in_repo_path);
2781 s->in_repo_path = parent_path;
2782 s->thread_args.in_repo_path = s->in_repo_path;
2783 } else if (ch == CTRL('l')) {
2784 struct got_object_id *start_id;
2785 err = got_repo_match_object_id(&start_id, NULL,
2786 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2787 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2791 s->start_id = start_id;
2792 s->thread_args.start_id = s->start_id;
2794 s->log_branches = !s->log_branches;
2796 if (s->thread_args.pack_fds == NULL) {
2797 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2801 err = got_repo_open(&s->thread_args.repo,
2802 got_repo_get_path(s->repo), NULL,
2803 s->thread_args.pack_fds);
2807 err = tog_load_refs(s->repo, 0);
2810 err = got_commit_graph_open(&s->thread_args.graph,
2811 s->in_repo_path, !s->log_branches);
2814 err = got_commit_graph_iter_start(s->thread_args.graph,
2815 s->start_id, s->repo, NULL, NULL);
2818 free_commits(&s->commits);
2819 s->first_displayed_entry = NULL;
2820 s->last_displayed_entry = NULL;
2821 s->selected_entry = NULL;
2823 s->thread_args.log_complete = 0;
2825 s->thread_args.commits_needed = view->nlines;
2826 s->matched_entry = NULL;
2827 s->search_entry = NULL;
2830 if (view_is_parent_view(view))
2831 begin_x = view_split_begin_x(view->begin_x);
2832 ref_view = view_open(view->nlines, view->ncols,
2833 view->begin_y, begin_x, TOG_VIEW_REF);
2834 if (ref_view == NULL)
2835 return got_error_from_errno("view_open");
2836 err = open_ref_view(ref_view, s->repo);
2838 view_close(ref_view);
2842 ref_view->focussed = 1;
2843 if (view_is_parent_view(view)) {
2844 err = view_close_child(view);
2847 err = view_set_child(view, ref_view);
2850 view->focus_child = 1;
2852 *new_view = ref_view;
2861 static const struct got_error *
2862 apply_unveil(const char *repo_path, const char *worktree_path)
2864 const struct got_error *error;
2867 if (unveil("gmon.out", "rwc") != 0)
2868 return got_error_from_errno2("unveil", "gmon.out");
2870 if (repo_path && unveil(repo_path, "r") != 0)
2871 return got_error_from_errno2("unveil", repo_path);
2873 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2874 return got_error_from_errno2("unveil", worktree_path);
2876 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2877 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2879 error = got_privsep_unveil_exec_helpers();
2883 if (unveil(NULL, NULL) != 0)
2884 return got_error_from_errno("unveil");
2893 * Override default signal handlers before starting ncurses.
2894 * This should prevent ncurses from installing its own
2895 * broken cleanup() signal handler.
2897 signal(SIGWINCH, tog_sigwinch);
2898 signal(SIGPIPE, tog_sigpipe);
2899 signal(SIGCONT, tog_sigcont);
2900 signal(SIGINT, tog_sigint);
2901 signal(SIGTERM, tog_sigterm);
2905 halfdelay(1); /* Do fast refresh while initial view is loading. */
2908 intrflush(stdscr, FALSE);
2909 keypad(stdscr, TRUE);
2911 if (getenv("TOG_COLORS") != NULL) {
2913 use_default_colors();
2917 static const struct got_error *
2918 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2919 struct got_repository *repo, struct got_worktree *worktree)
2921 const struct got_error *err = NULL;
2924 *in_repo_path = strdup("/");
2925 if (*in_repo_path == NULL)
2926 return got_error_from_errno("strdup");
2931 const char *prefix = got_worktree_get_path_prefix(worktree);
2934 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2937 if (asprintf(in_repo_path, "%s%s%s", prefix,
2938 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2940 err = got_error_from_errno("asprintf");
2941 *in_repo_path = NULL;
2945 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2950 static const struct got_error *
2951 cmd_log(int argc, char *argv[])
2953 const struct got_error *error;
2954 struct got_repository *repo = NULL;
2955 struct got_worktree *worktree = NULL;
2956 struct got_object_id *start_id = NULL;
2957 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2958 char *start_commit = NULL, *label = NULL;
2959 struct got_reference *ref = NULL;
2960 const char *head_ref_name = NULL;
2961 int ch, log_branches = 0;
2962 struct tog_view *view;
2963 int *pack_fds = NULL;
2965 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2971 start_commit = optarg;
2974 repo_path = realpath(optarg, NULL);
2975 if (repo_path == NULL)
2976 return got_error_from_errno2("realpath",
2991 error = got_repo_pack_fds_open(&pack_fds);
2995 if (repo_path == NULL) {
2996 cwd = getcwd(NULL, 0);
2998 return got_error_from_errno("getcwd");
2999 error = got_worktree_open(&worktree, cwd);
3000 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3004 strdup(got_worktree_get_repo_path(worktree));
3006 repo_path = strdup(cwd);
3007 if (repo_path == NULL) {
3008 error = got_error_from_errno("strdup");
3013 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3017 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3024 error = apply_unveil(got_repo_get_path(repo),
3025 worktree ? got_worktree_get_root_path(worktree) : NULL);
3029 /* already loaded by tog_log_with_path()? */
3030 if (TAILQ_EMPTY(&tog_refs)) {
3031 error = tog_load_refs(repo, 0);
3036 if (start_commit == NULL) {
3037 error = got_repo_match_object_id(&start_id, &label,
3038 worktree ? got_worktree_get_head_ref_name(worktree) :
3039 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3042 head_ref_name = label;
3044 error = got_ref_open(&ref, repo, start_commit, 0);
3046 head_ref_name = got_ref_get_name(ref);
3047 else if (error->code != GOT_ERR_NOT_REF)
3049 error = got_repo_match_object_id(&start_id, NULL,
3050 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3055 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3057 error = got_error_from_errno("view_open");
3060 error = open_log_view(view, start_id, repo, head_ref_name,
3061 in_repo_path, log_branches);
3065 /* Release work tree lock. */
3066 got_worktree_close(worktree);
3069 error = view_loop(view);
3079 const struct got_error *close_err = got_repo_close(repo);
3084 got_worktree_close(worktree);
3086 const struct got_error *pack_err =
3087 got_repo_pack_fds_close(pack_fds);
3099 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3100 "[-w] object1 object2\n", getprogname());
3105 match_line(const char *line, regex_t *regex, size_t nmatch,
3106 regmatch_t *regmatch)
3108 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3112 match_color(struct tog_colors *colors, const char *line)
3114 struct tog_color *tc = NULL;
3116 STAILQ_FOREACH(tc, colors, entry) {
3117 if (match_line(line, &tc->regex, 0, NULL))
3124 static const struct got_error *
3125 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3126 WINDOW *window, int skipcol, regmatch_t *regmatch)
3128 const struct got_error *err = NULL;
3130 wchar_t *wline = NULL;
3131 int rme, rms, n, width, scrollx;
3132 int width0 = 0, width1 = 0, width2 = 0;
3133 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3137 rms = regmatch->rm_so;
3138 rme = regmatch->rm_eo;
3140 err = expand_tab(&exstr, line);
3144 /* Split the line into 3 segments, according to match offsets. */
3145 seg0 = strndup(exstr, rms);
3147 err = got_error_from_errno("strndup");
3150 seg1 = strndup(exstr + rms, rme - rms);
3152 err = got_error_from_errno("strndup");
3155 seg2 = strdup(exstr + rme);
3157 err = got_error_from_errno("strndup");
3161 /* draw up to matched token if we haven't scrolled past it */
3162 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3166 n = MAX(width0 - skipcol, 0);
3169 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3170 wlimit, col_tab_align, 1);
3173 waddwstr(window, &wline[scrollx]);
3183 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3187 wlen = wcslen(wline);
3189 width = wcwidth(wline[i]);
3191 /* should not happen, tabs are expanded */
3192 err = got_error(GOT_ERR_RANGE);
3195 if (width0 + w + width > skipcol)
3200 /* draw (visible part of) matched token (if scrolled into it) */
3201 if (width1 - w > 0) {
3202 wattron(window, A_STANDOUT);
3203 waddwstr(window, &wline[i]);
3204 wattroff(window, A_STANDOUT);
3205 wlimit -= (width1 - w);
3206 *wtotal += (width1 - w);
3210 if (wlimit > 0) { /* draw rest of line */
3212 if (skipcol > width0 + width1) {
3213 err = format_line(&wline, &width2, &scrollx, seg2,
3214 skipcol - (width0 + width1), wlimit,
3218 waddwstr(window, &wline[scrollx]);
3220 err = format_line(&wline, &width2, NULL, seg2, 0,
3221 wlimit, col_tab_align, 1);
3224 waddwstr(window, wline);
3237 static const struct got_error *
3238 draw_file(struct tog_view *view, const char *header)
3240 struct tog_diff_view_state *s = &view->state.diff;
3241 regmatch_t *regmatch = &view->regmatch;
3242 const struct got_error *err;
3245 size_t linesize = 0;
3247 struct tog_color *tc;
3250 int max_lines = view->nlines;
3251 int nlines = s->nlines;
3254 line_offset = s->line_offsets[s->first_displayed_line - 1];
3255 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3256 return got_error_from_errno("fseek");
3258 werase(view->window);
3261 if (asprintf(&line, "[%d/%d] %s",
3262 s->first_displayed_line - 1 + s->selected_line, nlines,
3264 return got_error_from_errno("asprintf");
3265 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3271 if (view_needs_focus_indication(view))
3272 wstandout(view->window);
3273 waddwstr(view->window, wline);
3276 if (view_needs_focus_indication(view))
3277 wstandend(view->window);
3278 if (width <= view->ncols - 1)
3279 waddch(view->window, '\n');
3289 while (max_lines > 0 && nprinted < max_lines) {
3290 linelen = getline(&line, &linesize, s->f);
3291 if (linelen == -1) {
3297 return got_ferror(s->f, GOT_ERR_IO);
3300 /* Set view->maxx based on full line length. */
3301 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3307 view->maxx = MAX(view->maxx, width);
3311 tc = match_color(&s->colors, line);
3313 wattr_on(view->window,
3314 COLOR_PAIR(tc->colorpair), NULL);
3315 if (s->first_displayed_line + nprinted == s->matched_line &&
3316 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3317 err = add_matched_line(&width, line, view->ncols, 0,
3318 view->window, view->x, regmatch);
3325 err = format_line(&wline, &width, &skip, line,
3326 view->x, view->ncols, 0, view->x ? 1 : 0);
3331 waddwstr(view->window, &wline[skip]);
3336 wattr_off(view->window,
3337 COLOR_PAIR(tc->colorpair), NULL);
3338 if (width <= view->ncols - 1)
3339 waddch(view->window, '\n');
3344 s->last_displayed_line = s->first_displayed_line +
3347 s->last_displayed_line = s->first_displayed_line;
3352 while (nprinted < view->nlines) {
3353 waddch(view->window, '\n');
3357 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3363 wstandout(view->window);
3364 waddwstr(view->window, wline);
3367 wstandend(view->window);
3374 get_datestr(time_t *time, char *datebuf)
3376 struct tm mytm, *tm;
3379 tm = gmtime_r(time, &mytm);
3382 s = asctime_r(tm, datebuf);
3385 p = strchr(s, '\n');
3391 static const struct got_error *
3392 get_changed_paths(struct got_pathlist_head *paths,
3393 struct got_commit_object *commit, struct got_repository *repo)
3395 const struct got_error *err = NULL;
3396 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3397 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3398 struct got_object_qid *qid;
3400 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3402 struct got_commit_object *pcommit;
3403 err = got_object_open_as_commit(&pcommit, repo,
3408 tree_id1 = got_object_id_dup(
3409 got_object_commit_get_tree_id(pcommit));
3410 if (tree_id1 == NULL) {
3411 got_object_commit_close(pcommit);
3412 return got_error_from_errno("got_object_id_dup");
3414 got_object_commit_close(pcommit);
3419 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3424 tree_id2 = got_object_commit_get_tree_id(commit);
3425 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3429 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3430 got_diff_tree_collect_changed_paths, paths, 0);
3433 got_object_tree_close(tree1);
3435 got_object_tree_close(tree2);
3440 static const struct got_error *
3441 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3445 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3447 return got_error_from_errno("reallocarray");
3449 (*line_offsets)[*nlines] = off;
3454 static const struct got_error *
3455 write_commit_info(off_t **line_offsets, size_t *nlines,
3456 struct got_object_id *commit_id, struct got_reflist_head *refs,
3457 struct got_repository *repo, FILE *outfile)
3459 const struct got_error *err = NULL;
3460 char datebuf[26], *datestr;
3461 struct got_commit_object *commit;
3462 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3463 time_t committer_time;
3464 const char *author, *committer;
3465 char *refs_str = NULL;
3466 struct got_pathlist_head changed_paths;
3467 struct got_pathlist_entry *pe;
3471 TAILQ_INIT(&changed_paths);
3474 err = build_refs_str(&refs_str, refs, commit_id, repo);
3479 err = got_object_open_as_commit(&commit, repo, commit_id);
3483 err = got_object_id_str(&id_str, commit_id);
3485 err = got_error_from_errno("got_object_id_str");
3489 err = add_line_offset(line_offsets, nlines, 0);
3493 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3494 refs_str ? refs_str : "", refs_str ? ")" : "");
3496 err = got_error_from_errno("fprintf");
3500 err = add_line_offset(line_offsets, nlines, outoff);
3504 n = fprintf(outfile, "from: %s\n",
3505 got_object_commit_get_author(commit));
3507 err = got_error_from_errno("fprintf");
3511 err = add_line_offset(line_offsets, nlines, outoff);
3515 committer_time = got_object_commit_get_committer_time(commit);
3516 datestr = get_datestr(&committer_time, datebuf);
3518 n = fprintf(outfile, "date: %s UTC\n", datestr);
3520 err = got_error_from_errno("fprintf");
3524 err = add_line_offset(line_offsets, nlines, outoff);
3528 author = got_object_commit_get_author(commit);
3529 committer = got_object_commit_get_committer(commit);
3530 if (strcmp(author, committer) != 0) {
3531 n = fprintf(outfile, "via: %s\n", committer);
3533 err = got_error_from_errno("fprintf");
3537 err = add_line_offset(line_offsets, nlines, outoff);
3541 if (got_object_commit_get_nparents(commit) > 1) {
3542 const struct got_object_id_queue *parent_ids;
3543 struct got_object_qid *qid;
3545 parent_ids = got_object_commit_get_parent_ids(commit);
3546 STAILQ_FOREACH(qid, parent_ids, entry) {
3547 err = got_object_id_str(&id_str, &qid->id);
3550 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3552 err = got_error_from_errno("fprintf");
3556 err = add_line_offset(line_offsets, nlines, outoff);
3564 err = got_object_commit_get_logmsg(&logmsg, commit);
3568 while ((line = strsep(&s, "\n")) != NULL) {
3569 n = fprintf(outfile, "%s\n", line);
3571 err = got_error_from_errno("fprintf");
3575 err = add_line_offset(line_offsets, nlines, outoff);
3580 err = get_changed_paths(&changed_paths, commit, repo);
3583 TAILQ_FOREACH(pe, &changed_paths, entry) {
3584 struct got_diff_changed_path *cp = pe->data;
3585 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3587 err = got_error_from_errno("fprintf");
3591 err = add_line_offset(line_offsets, nlines, outoff);
3594 free((char *)pe->path);
3598 fputc('\n', outfile);
3600 err = add_line_offset(line_offsets, nlines, outoff);
3602 got_pathlist_free(&changed_paths);
3606 got_object_commit_close(commit);
3608 free(*line_offsets);
3609 *line_offsets = NULL;
3615 static const struct got_error *
3616 create_diff(struct tog_diff_view_state *s)
3618 const struct got_error *err = NULL;
3622 free(s->line_offsets);
3623 s->line_offsets = malloc(sizeof(off_t));
3624 if (s->line_offsets == NULL)
3625 return got_error_from_errno("malloc");
3630 err = got_error_from_errno("got_opentemp");
3633 if (s->f && fclose(s->f) == EOF) {
3634 err = got_error_from_errno("fclose");
3640 err = got_object_get_type(&obj_type, s->repo, s->id1);
3642 err = got_object_get_type(&obj_type, s->repo, s->id2);
3647 case GOT_OBJ_TYPE_BLOB:
3648 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3649 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3650 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3653 case GOT_OBJ_TYPE_TREE:
3654 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3655 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3656 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3658 case GOT_OBJ_TYPE_COMMIT: {
3659 const struct got_object_id_queue *parent_ids;
3660 struct got_object_qid *pid;
3661 struct got_commit_object *commit2;
3662 struct got_reflist_head *refs;
3664 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3667 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3668 /* Show commit info if we're diffing to a parent/root commit. */
3669 if (s->id1 == NULL) {
3670 err = write_commit_info(&s->line_offsets, &s->nlines,
3671 s->id2, refs, s->repo, s->f);
3675 parent_ids = got_object_commit_get_parent_ids(commit2);
3676 STAILQ_FOREACH(pid, parent_ids, entry) {
3677 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3678 err = write_commit_info(
3679 &s->line_offsets, &s->nlines,
3680 s->id2, refs, s->repo, s->f);
3687 got_object_commit_close(commit2);
3689 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3690 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3691 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3695 err = got_error(GOT_ERR_OBJ_TYPE);
3701 if (s->f && fflush(s->f) != 0 && err == NULL)
3702 err = got_error_from_errno("fflush");
3707 diff_view_indicate_progress(struct tog_view *view)
3709 mvwaddstr(view->window, 0, 0, "diffing...");
3714 static const struct got_error *
3715 search_start_diff_view(struct tog_view *view)
3717 struct tog_diff_view_state *s = &view->state.diff;
3719 s->matched_line = 0;
3723 static const struct got_error *
3724 search_next_diff_view(struct tog_view *view)
3726 struct tog_diff_view_state *s = &view->state.diff;
3727 const struct got_error *err = NULL;
3730 size_t linesize = 0;
3733 if (!view->searching) {
3734 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3738 if (s->matched_line) {
3739 if (view->searching == TOG_SEARCH_FORWARD)
3740 lineno = s->matched_line + 1;
3742 lineno = s->matched_line - 1;
3744 lineno = s->first_displayed_line;
3749 if (lineno <= 0 || lineno > s->nlines) {
3750 if (s->matched_line == 0) {
3751 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3755 if (view->searching == TOG_SEARCH_FORWARD)
3761 offset = s->line_offsets[lineno - 1];
3762 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3764 return got_error_from_errno("fseeko");
3766 linelen = getline(&line, &linesize, s->f);
3767 if (linelen != -1) {
3769 err = expand_tab(&exstr, line);
3772 if (match_line(exstr, &view->regex, 1,
3774 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3775 s->matched_line = lineno;
3781 if (view->searching == TOG_SEARCH_FORWARD)
3788 if (s->matched_line) {
3789 s->first_displayed_line = s->matched_line;
3790 s->selected_line = 1;
3796 static const struct got_error *
3797 close_diff_view(struct tog_view *view)
3799 const struct got_error *err = NULL;
3800 struct tog_diff_view_state *s = &view->state.diff;
3806 if (s->f && fclose(s->f) == EOF)
3807 err = got_error_from_errno("fclose");
3809 if (s->f1 && fclose(s->f1) == EOF)
3810 err = got_error_from_errno("fclose");
3812 if (s->f2 && fclose(s->f2) == EOF)
3813 err = got_error_from_errno("fclose");
3815 free_colors(&s->colors);
3816 free(s->line_offsets);
3817 s->line_offsets = NULL;
3822 static const struct got_error *
3823 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3824 struct got_object_id *id2, const char *label1, const char *label2,
3825 int diff_context, int ignore_whitespace, int force_text_diff,
3826 struct tog_view *log_view, struct got_repository *repo)
3828 const struct got_error *err;
3829 struct tog_diff_view_state *s = &view->state.diff;
3831 memset(s, 0, sizeof(*s));
3833 if (id1 != NULL && id2 != NULL) {
3835 err = got_object_get_type(&type1, repo, id1);
3838 err = got_object_get_type(&type2, repo, id2);
3843 return got_error(GOT_ERR_OBJ_TYPE);
3845 s->first_displayed_line = 1;
3846 s->last_displayed_line = view->nlines;
3847 s->selected_line = 1;
3855 s->id1 = got_object_id_dup(id1);
3857 return got_error_from_errno("got_object_id_dup");
3861 s->id2 = got_object_id_dup(id2);
3862 if (s->id2 == NULL) {
3863 err = got_error_from_errno("got_object_id_dup");
3867 s->f1 = got_opentemp();
3868 if (s->f1 == NULL) {
3869 err = got_error_from_errno("got_opentemp");
3873 s->f2 = got_opentemp();
3874 if (s->f2 == NULL) {
3875 err = got_error_from_errno("got_opentemp");
3879 s->first_displayed_line = 1;
3880 s->last_displayed_line = view->nlines;
3881 s->diff_context = diff_context;
3882 s->ignore_whitespace = ignore_whitespace;
3883 s->force_text_diff = force_text_diff;
3884 s->log_view = log_view;
3887 STAILQ_INIT(&s->colors);
3888 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3889 err = add_color(&s->colors,
3890 "^-", TOG_COLOR_DIFF_MINUS,
3891 get_color_value("TOG_COLOR_DIFF_MINUS"));
3894 err = add_color(&s->colors, "^\\+",
3895 TOG_COLOR_DIFF_PLUS,
3896 get_color_value("TOG_COLOR_DIFF_PLUS"));
3899 err = add_color(&s->colors,
3900 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3901 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3905 err = add_color(&s->colors,
3906 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3907 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3908 get_color_value("TOG_COLOR_DIFF_META"));
3912 err = add_color(&s->colors,
3913 "^(from|via): ", TOG_COLOR_AUTHOR,
3914 get_color_value("TOG_COLOR_AUTHOR"));
3918 err = add_color(&s->colors,
3919 "^date: ", TOG_COLOR_DATE,
3920 get_color_value("TOG_COLOR_DATE"));
3925 if (log_view && view_is_splitscreen(view))
3926 show_log_view(log_view); /* draw vborder */
3927 diff_view_indicate_progress(view);
3929 err = create_diff(s);
3931 view->show = show_diff_view;
3932 view->input = input_diff_view;
3933 view->close = close_diff_view;
3934 view->search_start = search_start_diff_view;
3935 view->search_next = search_next_diff_view;
3938 close_diff_view(view);
3942 static const struct got_error *
3943 show_diff_view(struct tog_view *view)
3945 const struct got_error *err;
3946 struct tog_diff_view_state *s = &view->state.diff;
3947 char *id_str1 = NULL, *id_str2, *header;
3948 const char *label1, *label2;
3951 err = got_object_id_str(&id_str1, s->id1);
3954 label1 = s->label1 ? : id_str1;
3956 label1 = "/dev/null";
3958 err = got_object_id_str(&id_str2, s->id2);
3961 label2 = s->label2 ? : id_str2;
3963 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3964 err = got_error_from_errno("asprintf");
3972 err = draw_file(view, header);
3977 static const struct got_error *
3978 set_selected_commit(struct tog_diff_view_state *s,
3979 struct commit_queue_entry *entry)
3981 const struct got_error *err;
3982 const struct got_object_id_queue *parent_ids;
3983 struct got_commit_object *selected_commit;
3984 struct got_object_qid *pid;
3987 s->id2 = got_object_id_dup(entry->id);
3989 return got_error_from_errno("got_object_id_dup");
3991 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3994 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3996 pid = STAILQ_FIRST(parent_ids);
3997 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3998 got_object_commit_close(selected_commit);
4002 static const struct got_error *
4003 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4005 const struct got_error *err = NULL;
4006 struct tog_diff_view_state *s = &view->state.diff;
4007 struct tog_log_view_state *ls;
4008 struct commit_queue_entry *old_selected_entry;
4010 size_t linesize = 0;
4012 int i, nscroll = view->nlines - 1;
4019 view->x = MAX(view->maxx - view->ncols / 3, 0);
4023 if (view->x + view->ncols / 3 < view->maxx)
4024 view->x += 2; /* move two columns right */
4028 view->x -= MIN(view->x, 2); /* move two columns back */
4033 s->force_text_diff = !s->force_text_diff;
4035 s->ignore_whitespace = !s->ignore_whitespace;
4036 wclear(view->window);
4037 s->first_displayed_line = 1;
4038 s->last_displayed_line = view->nlines;
4039 s->matched_line = 0;
4040 diff_view_indicate_progress(view);
4041 err = create_diff(s);
4045 s->first_displayed_line = 1;
4052 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4058 if (s->first_displayed_line > 1)
4059 s->first_displayed_line--;
4068 if (s->first_displayed_line == 1)
4071 while (i++ < nscroll && s->first_displayed_line > 1)
4072 s->first_displayed_line--;
4078 s->first_displayed_line++;
4091 while (!s->eof && i++ < nscroll) {
4092 linelen = getline(&line, &linesize, s->f);
4093 s->first_displayed_line++;
4094 if (linelen == -1) {
4098 err = got_ferror(s->f, GOT_ERR_IO);
4105 if (s->diff_context > 0) {
4107 s->matched_line = 0;
4108 diff_view_indicate_progress(view);
4109 err = create_diff(s);
4110 if (s->first_displayed_line + view->nlines - 1 >
4112 s->first_displayed_line = 1;
4113 s->last_displayed_line = view->nlines;
4118 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4120 s->matched_line = 0;
4121 diff_view_indicate_progress(view);
4122 err = create_diff(s);
4127 if (s->log_view == NULL)
4129 ls = &s->log_view->state.log;
4130 old_selected_entry = ls->selected_entry;
4132 err = input_log_view(NULL, s->log_view, KEY_UP);
4136 if (old_selected_entry == ls->selected_entry)
4139 err = set_selected_commit(s, ls->selected_entry);
4143 s->first_displayed_line = 1;
4144 s->last_displayed_line = view->nlines;
4145 s->matched_line = 0;
4148 diff_view_indicate_progress(view);
4149 err = create_diff(s);
4153 if (s->log_view == NULL)
4155 ls = &s->log_view->state.log;
4156 old_selected_entry = ls->selected_entry;
4158 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4162 if (old_selected_entry == ls->selected_entry)
4165 err = set_selected_commit(s, ls->selected_entry);
4169 s->first_displayed_line = 1;
4170 s->last_displayed_line = view->nlines;
4171 s->matched_line = 0;
4174 diff_view_indicate_progress(view);
4175 err = create_diff(s);
4184 static const struct got_error *
4185 cmd_diff(int argc, char *argv[])
4187 const struct got_error *error = NULL;
4188 struct got_repository *repo = NULL;
4189 struct got_worktree *worktree = NULL;
4190 struct got_object_id *id1 = NULL, *id2 = NULL;
4191 char *repo_path = NULL, *cwd = NULL;
4192 char *id_str1 = NULL, *id_str2 = NULL;
4193 char *label1 = NULL, *label2 = NULL;
4194 int diff_context = 3, ignore_whitespace = 0;
4195 int ch, force_text_diff = 0;
4197 struct tog_view *view;
4198 int *pack_fds = NULL;
4200 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4203 force_text_diff = 1;
4206 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4209 errx(1, "number of context lines is %s: %s",
4213 repo_path = realpath(optarg, NULL);
4214 if (repo_path == NULL)
4215 return got_error_from_errno2("realpath",
4217 got_path_strip_trailing_slashes(repo_path);
4220 ignore_whitespace = 1;
4232 usage_diff(); /* TODO show local worktree changes */
4233 } else if (argc == 2) {
4239 error = got_repo_pack_fds_open(&pack_fds);
4243 if (repo_path == NULL) {
4244 cwd = getcwd(NULL, 0);
4246 return got_error_from_errno("getcwd");
4247 error = got_worktree_open(&worktree, cwd);
4248 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4252 strdup(got_worktree_get_repo_path(worktree));
4254 repo_path = strdup(cwd);
4255 if (repo_path == NULL) {
4256 error = got_error_from_errno("strdup");
4261 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4267 error = apply_unveil(got_repo_get_path(repo), NULL);
4271 error = tog_load_refs(repo, 0);
4275 error = got_repo_match_object_id(&id1, &label1, id_str1,
4276 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4280 error = got_repo_match_object_id(&id2, &label2, id_str2,
4281 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4285 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4287 error = got_error_from_errno("view_open");
4290 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4291 ignore_whitespace, force_text_diff, NULL, repo);
4294 error = view_loop(view);
4301 const struct got_error *close_err = got_repo_close(repo);
4306 got_worktree_close(worktree);
4308 const struct got_error *pack_err =
4309 got_repo_pack_fds_close(pack_fds);
4322 "usage: %s blame [-c commit] [-r repository-path] path\n",
4327 struct tog_blame_line {
4329 struct got_object_id *id;
4332 static const struct got_error *
4333 draw_blame(struct tog_view *view)
4335 struct tog_blame_view_state *s = &view->state.blame;
4336 struct tog_blame *blame = &s->blame;
4337 regmatch_t *regmatch = &view->regmatch;
4338 const struct got_error *err;
4339 int lineno = 0, nprinted = 0;
4341 size_t linesize = 0;
4345 struct tog_blame_line *blame_line;
4346 struct got_object_id *prev_id = NULL;
4348 struct tog_color *tc;
4350 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4355 werase(view->window);
4357 if (asprintf(&line, "commit %s", id_str) == -1) {
4358 err = got_error_from_errno("asprintf");
4363 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4368 if (view_needs_focus_indication(view))
4369 wstandout(view->window);
4370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4372 wattr_on(view->window,
4373 COLOR_PAIR(tc->colorpair), NULL);
4374 waddwstr(view->window, wline);
4376 wattr_off(view->window,
4377 COLOR_PAIR(tc->colorpair), NULL);
4378 if (view_needs_focus_indication(view))
4379 wstandend(view->window);
4382 if (width < view->ncols - 1)
4383 waddch(view->window, '\n');
4385 if (asprintf(&line, "[%d/%d] %s%s",
4386 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4387 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4389 return got_error_from_errno("asprintf");
4392 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4397 waddwstr(view->window, wline);
4400 if (width < view->ncols - 1)
4401 waddch(view->window, '\n');
4405 while (nprinted < view->nlines - 2) {
4406 linelen = getline(&line, &linesize, blame->f);
4407 if (linelen == -1) {
4408 if (feof(blame->f)) {
4413 return got_ferror(blame->f, GOT_ERR_IO);
4415 if (++lineno < s->first_displayed_line)
4418 /* Set view->maxx based on full line length. */
4419 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4426 view->maxx = MAX(view->maxx, width);
4428 if (view->focussed && nprinted == s->selected_line - 1)
4429 wstandout(view->window);
4431 if (blame->nlines > 0) {
4432 blame_line = &blame->lines[lineno - 1];
4433 if (blame_line->annotated && prev_id &&
4434 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4436 nprinted == s->selected_line - 1)) {
4437 waddstr(view->window, " ");
4438 } else if (blame_line->annotated) {
4440 err = got_object_id_str(&id_str,
4446 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4448 wattr_on(view->window,
4449 COLOR_PAIR(tc->colorpair), NULL);
4450 wprintw(view->window, "%.8s", id_str);
4452 wattr_off(view->window,
4453 COLOR_PAIR(tc->colorpair), NULL);
4455 prev_id = blame_line->id;
4457 waddstr(view->window, "........");
4461 waddstr(view->window, "........");
4465 if (view->focussed && nprinted == s->selected_line - 1)
4466 wstandend(view->window);
4467 waddstr(view->window, " ");
4469 if (view->ncols <= 9) {
4471 } else if (s->first_displayed_line + nprinted ==
4473 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4474 err = add_matched_line(&width, line, view->ncols - 9, 9,
4475 view->window, view->x, regmatch);
4483 err = format_line(&wline, &width, &skip, line,
4484 view->x, view->ncols - 9, 9, 1);
4489 waddwstr(view->window, &wline[skip]);
4495 if (width <= view->ncols - 1)
4496 waddch(view->window, '\n');
4497 if (++nprinted == 1)
4498 s->first_displayed_line = lineno;
4501 s->last_displayed_line = lineno;
4508 static const struct got_error *
4509 blame_cb(void *arg, int nlines, int lineno,
4510 struct got_commit_object *commit, struct got_object_id *id)
4512 const struct got_error *err = NULL;
4513 struct tog_blame_cb_args *a = arg;
4514 struct tog_blame_line *line;
4517 if (nlines != a->nlines ||
4518 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4519 return got_error(GOT_ERR_RANGE);
4521 errcode = pthread_mutex_lock(&tog_mutex);
4523 return got_error_set_errno(errcode, "pthread_mutex_lock");
4525 if (*a->quit) { /* user has quit the blame view */
4526 err = got_error(GOT_ERR_ITER_COMPLETED);
4531 goto done; /* no change in this commit */
4533 line = &a->lines[lineno - 1];
4534 if (line->annotated)
4537 line->id = got_object_id_dup(id);
4538 if (line->id == NULL) {
4539 err = got_error_from_errno("got_object_id_dup");
4542 line->annotated = 1;
4544 errcode = pthread_mutex_unlock(&tog_mutex);
4546 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4551 blame_thread(void *arg)
4553 const struct got_error *err, *close_err;
4554 struct tog_blame_thread_args *ta = arg;
4555 struct tog_blame_cb_args *a = ta->cb_args;
4558 err = block_signals_used_by_main_thread();
4562 err = got_blame(ta->path, a->commit_id, ta->repo,
4563 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4564 if (err && err->code == GOT_ERR_CANCELLED)
4567 errcode = pthread_mutex_lock(&tog_mutex);
4569 return (void *)got_error_set_errno(errcode,
4570 "pthread_mutex_lock");
4572 close_err = got_repo_close(ta->repo);
4578 errcode = pthread_mutex_unlock(&tog_mutex);
4579 if (errcode && err == NULL)
4580 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4585 static struct got_object_id *
4586 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4587 int first_displayed_line, int selected_line)
4589 struct tog_blame_line *line;
4594 line = &lines[first_displayed_line - 1 + selected_line - 1];
4595 if (!line->annotated)
4601 static const struct got_error *
4602 stop_blame(struct tog_blame *blame)
4604 const struct got_error *err = NULL;
4607 if (blame->thread) {
4609 errcode = pthread_mutex_unlock(&tog_mutex);
4611 return got_error_set_errno(errcode,
4612 "pthread_mutex_unlock");
4613 errcode = pthread_join(blame->thread, (void **)&err);
4615 return got_error_set_errno(errcode, "pthread_join");
4616 errcode = pthread_mutex_lock(&tog_mutex);
4618 return got_error_set_errno(errcode,
4619 "pthread_mutex_lock");
4620 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4622 blame->thread = NULL;
4624 if (blame->thread_args.repo) {
4625 const struct got_error *close_err;
4626 close_err = got_repo_close(blame->thread_args.repo);
4629 blame->thread_args.repo = NULL;
4632 if (fclose(blame->f) == EOF && err == NULL)
4633 err = got_error_from_errno("fclose");
4637 for (i = 0; i < blame->nlines; i++)
4638 free(blame->lines[i].id);
4640 blame->lines = NULL;
4642 free(blame->cb_args.commit_id);
4643 blame->cb_args.commit_id = NULL;
4644 if (blame->pack_fds) {
4645 const struct got_error *pack_err =
4646 got_repo_pack_fds_close(blame->pack_fds);
4649 blame->pack_fds = NULL;
4654 static const struct got_error *
4655 cancel_blame_view(void *arg)
4657 const struct got_error *err = NULL;
4661 errcode = pthread_mutex_lock(&tog_mutex);
4663 return got_error_set_errno(errcode,
4664 "pthread_mutex_unlock");
4667 err = got_error(GOT_ERR_CANCELLED);
4669 errcode = pthread_mutex_unlock(&tog_mutex);
4671 return got_error_set_errno(errcode,
4672 "pthread_mutex_lock");
4677 static const struct got_error *
4678 run_blame(struct tog_view *view)
4680 struct tog_blame_view_state *s = &view->state.blame;
4681 struct tog_blame *blame = &s->blame;
4682 const struct got_error *err = NULL;
4683 struct got_commit_object *commit = NULL;
4684 struct got_blob_object *blob = NULL;
4685 struct got_repository *thread_repo = NULL;
4686 struct got_object_id *obj_id = NULL;
4688 int *pack_fds = NULL;
4690 err = got_object_open_as_commit(&commit, s->repo,
4691 &s->blamed_commit->id);
4695 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4699 err = got_object_get_type(&obj_type, s->repo, obj_id);
4703 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4704 err = got_error(GOT_ERR_OBJ_TYPE);
4708 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4711 blame->f = got_opentemp();
4712 if (blame->f == NULL) {
4713 err = got_error_from_errno("got_opentemp");
4716 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4717 &blame->line_offsets, blame->f, blob);
4720 if (blame->nlines == 0) {
4721 s->blame_complete = 1;
4725 /* Don't include \n at EOF in the blame line count. */
4726 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4729 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4730 if (blame->lines == NULL) {
4731 err = got_error_from_errno("calloc");
4735 err = got_repo_pack_fds_open(&pack_fds);
4738 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4743 blame->pack_fds = pack_fds;
4744 blame->cb_args.view = view;
4745 blame->cb_args.lines = blame->lines;
4746 blame->cb_args.nlines = blame->nlines;
4747 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4748 if (blame->cb_args.commit_id == NULL) {
4749 err = got_error_from_errno("got_object_id_dup");
4752 blame->cb_args.quit = &s->done;
4754 blame->thread_args.path = s->path;
4755 blame->thread_args.repo = thread_repo;
4756 blame->thread_args.cb_args = &blame->cb_args;
4757 blame->thread_args.complete = &s->blame_complete;
4758 blame->thread_args.cancel_cb = cancel_blame_view;
4759 blame->thread_args.cancel_arg = &s->done;
4760 s->blame_complete = 0;
4762 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4763 s->first_displayed_line = 1;
4764 s->last_displayed_line = view->nlines;
4765 s->selected_line = 1;
4767 s->matched_line = 0;
4771 got_object_commit_close(commit);
4773 got_object_blob_close(blob);
4780 static const struct got_error *
4781 open_blame_view(struct tog_view *view, char *path,
4782 struct got_object_id *commit_id, struct got_repository *repo)
4784 const struct got_error *err = NULL;
4785 struct tog_blame_view_state *s = &view->state.blame;
4787 STAILQ_INIT(&s->blamed_commits);
4789 s->path = strdup(path);
4790 if (s->path == NULL)
4791 return got_error_from_errno("strdup");
4793 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4799 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4800 s->first_displayed_line = 1;
4801 s->last_displayed_line = view->nlines;
4802 s->selected_line = 1;
4803 s->blame_complete = 0;
4805 s->commit_id = commit_id;
4806 memset(&s->blame, 0, sizeof(s->blame));
4808 STAILQ_INIT(&s->colors);
4809 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4810 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4811 get_color_value("TOG_COLOR_COMMIT"));
4816 view->show = show_blame_view;
4817 view->input = input_blame_view;
4818 view->close = close_blame_view;
4819 view->search_start = search_start_blame_view;
4820 view->search_next = search_next_blame_view;
4822 return run_blame(view);
4825 static const struct got_error *
4826 close_blame_view(struct tog_view *view)
4828 const struct got_error *err = NULL;
4829 struct tog_blame_view_state *s = &view->state.blame;
4831 if (s->blame.thread)
4832 err = stop_blame(&s->blame);
4834 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4835 struct got_object_qid *blamed_commit;
4836 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4837 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4838 got_object_qid_free(blamed_commit);
4842 free_colors(&s->colors);
4846 static const struct got_error *
4847 search_start_blame_view(struct tog_view *view)
4849 struct tog_blame_view_state *s = &view->state.blame;
4851 s->matched_line = 0;
4855 static const struct got_error *
4856 search_next_blame_view(struct tog_view *view)
4858 struct tog_blame_view_state *s = &view->state.blame;
4859 const struct got_error *err = NULL;
4862 size_t linesize = 0;
4865 if (!view->searching) {
4866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4870 if (s->matched_line) {
4871 if (view->searching == TOG_SEARCH_FORWARD)
4872 lineno = s->matched_line + 1;
4874 lineno = s->matched_line - 1;
4876 lineno = s->first_displayed_line - 1 + s->selected_line;
4881 if (lineno <= 0 || lineno > s->blame.nlines) {
4882 if (s->matched_line == 0) {
4883 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4887 if (view->searching == TOG_SEARCH_FORWARD)
4890 lineno = s->blame.nlines;
4893 offset = s->blame.line_offsets[lineno - 1];
4894 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4896 return got_error_from_errno("fseeko");
4898 linelen = getline(&line, &linesize, s->blame.f);
4899 if (linelen != -1) {
4901 err = expand_tab(&exstr, line);
4904 if (match_line(exstr, &view->regex, 1,
4906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4907 s->matched_line = lineno;
4913 if (view->searching == TOG_SEARCH_FORWARD)
4920 if (s->matched_line) {
4921 s->first_displayed_line = s->matched_line;
4922 s->selected_line = 1;
4928 static const struct got_error *
4929 show_blame_view(struct tog_view *view)
4931 const struct got_error *err = NULL;
4932 struct tog_blame_view_state *s = &view->state.blame;
4935 if (s->blame.thread == NULL && !s->blame_complete) {
4936 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4937 &s->blame.thread_args);
4939 return got_error_set_errno(errcode, "pthread_create");
4941 halfdelay(1); /* fast refresh while annotating */
4944 if (s->blame_complete)
4945 halfdelay(10); /* disable fast refresh */
4947 err = draw_blame(view);
4953 static const struct got_error *
4954 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4956 const struct got_error *err = NULL, *thread_err = NULL;
4957 struct tog_view *diff_view;
4958 struct tog_blame_view_state *s = &view->state.blame;
4959 int begin_x = 0, nscroll = view->nlines - 2;
4966 view->x = MAX(view->maxx - view->ncols / 3, 0);
4970 if (view->x + view->ncols / 3 < view->maxx)
4971 view->x += 2; /* move two columns right */
4975 view->x -= MIN(view->x, 2); /* move two columns back */
4982 s->selected_line = 1;
4983 s->first_displayed_line = 1;
4987 if (s->blame.nlines < view->nlines - 2) {
4988 s->selected_line = s->blame.nlines;
4989 s->first_displayed_line = 1;
4991 s->selected_line = view->nlines - 2;
4992 s->first_displayed_line = s->blame.nlines -
4999 if (s->selected_line > 1)
5001 else if (s->selected_line == 1 &&
5002 s->first_displayed_line > 1)
5003 s->first_displayed_line--;
5012 if (s->first_displayed_line == 1) {
5013 s->selected_line = MAX(1, s->selected_line - nscroll);
5016 if (s->first_displayed_line > nscroll)
5017 s->first_displayed_line -= nscroll;
5019 s->first_displayed_line = 1;
5024 if (s->selected_line < view->nlines - 2 &&
5025 s->first_displayed_line +
5026 s->selected_line <= s->blame.nlines)
5028 else if (s->last_displayed_line <
5030 s->first_displayed_line++;
5034 struct got_object_id *id = NULL;
5035 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5036 s->first_displayed_line, s->selected_line);
5040 struct got_commit_object *commit, *pcommit;
5041 struct got_object_qid *pid;
5042 struct got_object_id *blob_id = NULL;
5044 err = got_object_open_as_commit(&commit,
5049 got_object_commit_get_parent_ids(commit));
5051 got_object_commit_close(commit);
5054 /* Check if path history ends here. */
5055 err = got_object_open_as_commit(&pcommit,
5059 err = got_object_id_by_path(&blob_id, s->repo,
5061 got_object_commit_close(pcommit);
5063 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5065 got_object_commit_close(commit);
5068 err = got_object_get_type(&obj_type, s->repo,
5071 /* Can't blame non-blob type objects. */
5072 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5073 got_object_commit_close(commit);
5076 err = got_object_qid_alloc(&s->blamed_commit,
5078 got_object_commit_close(commit);
5080 if (got_object_id_cmp(id,
5081 &s->blamed_commit->id) == 0)
5083 err = got_object_qid_alloc(&s->blamed_commit,
5089 thread_err = stop_blame(&s->blame);
5093 STAILQ_INSERT_HEAD(&s->blamed_commits,
5094 s->blamed_commit, entry);
5095 err = run_blame(view);
5101 struct got_object_qid *first;
5102 first = STAILQ_FIRST(&s->blamed_commits);
5103 if (!got_object_id_cmp(&first->id, s->commit_id))
5106 thread_err = stop_blame(&s->blame);
5110 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5111 got_object_qid_free(s->blamed_commit);
5113 STAILQ_FIRST(&s->blamed_commits);
5114 err = run_blame(view);
5121 struct got_object_id *id = NULL;
5122 struct got_object_qid *pid;
5123 struct got_commit_object *commit = NULL;
5124 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5125 s->first_displayed_line, s->selected_line);
5128 err = got_object_open_as_commit(&commit, s->repo, id);
5132 got_object_commit_get_parent_ids(commit));
5133 if (view_is_parent_view(view))
5134 begin_x = view_split_begin_x(view->begin_x);
5135 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5136 if (diff_view == NULL) {
5137 got_object_commit_close(commit);
5138 err = got_error_from_errno("view_open");
5141 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5142 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5143 got_object_commit_close(commit);
5145 view_close(diff_view);
5149 diff_view->focussed = 1;
5150 if (view_is_parent_view(view)) {
5151 err = view_close_child(view);
5154 err = view_set_child(view, diff_view);
5157 view->focus_child = 1;
5159 *new_view = diff_view;
5172 if (s->last_displayed_line >= s->blame.nlines &&
5173 s->selected_line >= MIN(s->blame.nlines,
5174 view->nlines - 2)) {
5177 if (s->last_displayed_line >= s->blame.nlines &&
5178 s->selected_line < view->nlines - 2) {
5180 MIN(nscroll, s->last_displayed_line -
5181 s->first_displayed_line - s->selected_line + 1);
5183 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5184 s->first_displayed_line += nscroll;
5186 s->first_displayed_line =
5187 s->blame.nlines - (view->nlines - 3);
5190 if (s->selected_line > view->nlines - 2) {
5191 s->selected_line = MIN(s->blame.nlines,
5198 return thread_err ? thread_err : err;
5201 static const struct got_error *
5202 cmd_blame(int argc, char *argv[])
5204 const struct got_error *error;
5205 struct got_repository *repo = NULL;
5206 struct got_worktree *worktree = NULL;
5207 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5208 char *link_target = NULL;
5209 struct got_object_id *commit_id = NULL;
5210 struct got_commit_object *commit = NULL;
5211 char *commit_id_str = NULL;
5213 struct tog_view *view;
5214 int *pack_fds = NULL;
5216 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5219 commit_id_str = optarg;
5222 repo_path = realpath(optarg, NULL);
5223 if (repo_path == NULL)
5224 return got_error_from_errno2("realpath",
5239 error = got_repo_pack_fds_open(&pack_fds);
5243 if (repo_path == NULL) {
5244 cwd = getcwd(NULL, 0);
5246 return got_error_from_errno("getcwd");
5247 error = got_worktree_open(&worktree, cwd);
5248 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5252 strdup(got_worktree_get_repo_path(worktree));
5254 repo_path = strdup(cwd);
5255 if (repo_path == NULL) {
5256 error = got_error_from_errno("strdup");
5261 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5265 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5272 error = apply_unveil(got_repo_get_path(repo), NULL);
5276 error = tog_load_refs(repo, 0);
5280 if (commit_id_str == NULL) {
5281 struct got_reference *head_ref;
5282 error = got_ref_open(&head_ref, repo, worktree ?
5283 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5286 error = got_ref_resolve(&commit_id, repo, head_ref);
5287 got_ref_close(head_ref);
5289 error = got_repo_match_object_id(&commit_id, NULL,
5290 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5295 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5297 error = got_error_from_errno("view_open");
5301 error = got_object_open_as_commit(&commit, repo, commit_id);
5305 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5310 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5315 /* Release work tree lock. */
5316 got_worktree_close(worktree);
5319 error = view_loop(view);
5327 got_object_commit_close(commit);
5329 got_worktree_close(worktree);
5331 const struct got_error *close_err = got_repo_close(repo);
5336 const struct got_error *pack_err =
5337 got_repo_pack_fds_close(pack_fds);
5345 static const struct got_error *
5346 draw_tree_entries(struct tog_view *view, const char *parent_path)
5348 struct tog_tree_view_state *s = &view->state.tree;
5349 const struct got_error *err = NULL;
5350 struct got_tree_entry *te;
5352 struct tog_color *tc;
5353 int width, n, i, nentries;
5354 int limit = view->nlines;
5358 werase(view->window);
5363 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5367 if (view_needs_focus_indication(view))
5368 wstandout(view->window);
5369 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5371 wattr_on(view->window,
5372 COLOR_PAIR(tc->colorpair), NULL);
5373 waddwstr(view->window, wline);
5375 wattr_off(view->window,
5376 COLOR_PAIR(tc->colorpair), NULL);
5377 if (view_needs_focus_indication(view))
5378 wstandend(view->window);
5381 if (width < view->ncols - 1)
5382 waddch(view->window, '\n');
5385 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5389 waddwstr(view->window, wline);
5392 if (width < view->ncols - 1)
5393 waddch(view->window, '\n');
5396 waddch(view->window, '\n');
5400 if (s->first_displayed_entry == NULL) {
5401 te = got_object_tree_get_first_entry(s->tree);
5402 if (s->selected == 0) {
5404 wstandout(view->window);
5405 s->selected_entry = NULL;
5407 waddstr(view->window, " ..\n"); /* parent directory */
5408 if (s->selected == 0 && view->focussed)
5409 wstandend(view->window);
5416 te = s->first_displayed_entry;
5419 nentries = got_object_tree_get_nentries(s->tree);
5420 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5421 char *line = NULL, *id_str = NULL, *link_target = NULL;
5422 const char *modestr = "";
5425 te = got_object_tree_get_entry(s->tree, i);
5426 mode = got_tree_entry_get_mode(te);
5429 err = got_object_id_str(&id_str,
5430 got_tree_entry_get_id(te));
5432 return got_error_from_errno(
5433 "got_object_id_str");
5435 if (got_object_tree_entry_is_submodule(te))
5437 else if (S_ISLNK(mode)) {
5440 err = got_tree_entry_get_symlink_target(&link_target,
5446 for (i = 0; i < strlen(link_target); i++) {
5447 if (!isprint((unsigned char)link_target[i]))
5448 link_target[i] = '?';
5452 else if (S_ISDIR(mode))
5454 else if (mode & S_IXUSR)
5456 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5457 got_tree_entry_get_name(te), modestr,
5458 link_target ? " -> ": "",
5459 link_target ? link_target : "") == -1) {
5462 return got_error_from_errno("asprintf");
5466 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5472 if (n == s->selected) {
5474 wstandout(view->window);
5475 s->selected_entry = te;
5477 tc = match_color(&s->colors, line);
5479 wattr_on(view->window,
5480 COLOR_PAIR(tc->colorpair), NULL);
5481 waddwstr(view->window, wline);
5483 wattr_off(view->window,
5484 COLOR_PAIR(tc->colorpair), NULL);
5485 if (width < view->ncols - 1)
5486 waddch(view->window, '\n');
5487 if (n == s->selected && view->focussed)
5488 wstandend(view->window);
5494 s->last_displayed_entry = te;
5503 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5505 struct got_tree_entry *te;
5506 int isroot = s->tree == s->root;
5509 if (s->first_displayed_entry == NULL)
5512 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5513 while (i++ < maxscroll) {
5516 s->first_displayed_entry = NULL;
5519 s->first_displayed_entry = te;
5520 te = got_tree_entry_get_prev(s->tree, te);
5525 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5527 struct got_tree_entry *next, *last;
5530 if (s->first_displayed_entry)
5531 next = got_tree_entry_get_next(s->tree,
5532 s->first_displayed_entry);
5534 next = got_object_tree_get_first_entry(s->tree);
5536 last = s->last_displayed_entry;
5537 while (next && last && n++ < maxscroll) {
5538 last = got_tree_entry_get_next(s->tree, last);
5540 s->first_displayed_entry = next;
5541 next = got_tree_entry_get_next(s->tree, next);
5546 static const struct got_error *
5547 tree_entry_path(char **path, struct tog_parent_trees *parents,
5548 struct got_tree_entry *te)
5550 const struct got_error *err = NULL;
5551 struct tog_parent_tree *pt;
5552 size_t len = 2; /* for leading slash and NUL */
5554 TAILQ_FOREACH(pt, parents, entry)
5555 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5558 len += strlen(got_tree_entry_get_name(te));
5560 *path = calloc(1, len);
5562 return got_error_from_errno("calloc");
5565 pt = TAILQ_LAST(parents, tog_parent_trees);
5567 const char *name = got_tree_entry_get_name(pt->selected_entry);
5568 if (strlcat(*path, name, len) >= len) {
5569 err = got_error(GOT_ERR_NO_SPACE);
5572 if (strlcat(*path, "/", len) >= len) {
5573 err = got_error(GOT_ERR_NO_SPACE);
5576 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5579 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5580 err = got_error(GOT_ERR_NO_SPACE);
5592 static const struct got_error *
5593 blame_tree_entry(struct tog_view **new_view, int begin_x,
5594 struct got_tree_entry *te, struct tog_parent_trees *parents,
5595 struct got_object_id *commit_id, struct got_repository *repo)
5597 const struct got_error *err = NULL;
5599 struct tog_view *blame_view;
5603 err = tree_entry_path(&path, parents, te);
5607 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5608 if (blame_view == NULL) {
5609 err = got_error_from_errno("view_open");
5613 err = open_blame_view(blame_view, path, commit_id, repo);
5615 if (err->code == GOT_ERR_CANCELLED)
5617 view_close(blame_view);
5619 *new_view = blame_view;
5625 static const struct got_error *
5626 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5627 struct tog_tree_view_state *s)
5629 struct tog_view *log_view;
5630 const struct got_error *err = NULL;
5635 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5636 if (log_view == NULL)
5637 return got_error_from_errno("view_open");
5639 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5643 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5646 view_close(log_view);
5648 *new_view = log_view;
5653 static const struct got_error *
5654 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5655 const char *head_ref_name, struct got_repository *repo)
5657 const struct got_error *err = NULL;
5658 char *commit_id_str = NULL;
5659 struct tog_tree_view_state *s = &view->state.tree;
5660 struct got_commit_object *commit = NULL;
5662 TAILQ_INIT(&s->parents);
5663 STAILQ_INIT(&s->colors);
5665 s->commit_id = got_object_id_dup(commit_id);
5666 if (s->commit_id == NULL)
5667 return got_error_from_errno("got_object_id_dup");
5669 err = got_object_open_as_commit(&commit, repo, commit_id);
5674 * The root is opened here and will be closed when the view is closed.
5675 * Any visited subtrees and their path-wise parents are opened and
5678 err = got_object_open_as_tree(&s->root, repo,
5679 got_object_commit_get_tree_id(commit));
5684 err = got_object_id_str(&commit_id_str, commit_id);
5688 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5689 err = got_error_from_errno("asprintf");
5693 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5694 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5695 if (head_ref_name) {
5696 s->head_ref_name = strdup(head_ref_name);
5697 if (s->head_ref_name == NULL) {
5698 err = got_error_from_errno("strdup");
5704 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5705 err = add_color(&s->colors, "\\$$",
5706 TOG_COLOR_TREE_SUBMODULE,
5707 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5710 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5711 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5714 err = add_color(&s->colors, "/$",
5715 TOG_COLOR_TREE_DIRECTORY,
5716 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5720 err = add_color(&s->colors, "\\*$",
5721 TOG_COLOR_TREE_EXECUTABLE,
5722 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5726 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5727 get_color_value("TOG_COLOR_COMMIT"));
5732 view->show = show_tree_view;
5733 view->input = input_tree_view;
5734 view->close = close_tree_view;
5735 view->search_start = search_start_tree_view;
5736 view->search_next = search_next_tree_view;
5738 free(commit_id_str);
5740 got_object_commit_close(commit);
5742 close_tree_view(view);
5746 static const struct got_error *
5747 close_tree_view(struct tog_view *view)
5749 struct tog_tree_view_state *s = &view->state.tree;
5751 free_colors(&s->colors);
5752 free(s->tree_label);
5753 s->tree_label = NULL;
5755 s->commit_id = NULL;
5756 free(s->head_ref_name);
5757 s->head_ref_name = NULL;
5758 while (!TAILQ_EMPTY(&s->parents)) {
5759 struct tog_parent_tree *parent;
5760 parent = TAILQ_FIRST(&s->parents);
5761 TAILQ_REMOVE(&s->parents, parent, entry);
5762 if (parent->tree != s->root)
5763 got_object_tree_close(parent->tree);
5767 if (s->tree != NULL && s->tree != s->root)
5768 got_object_tree_close(s->tree);
5770 got_object_tree_close(s->root);
5774 static const struct got_error *
5775 search_start_tree_view(struct tog_view *view)
5777 struct tog_tree_view_state *s = &view->state.tree;
5779 s->matched_entry = NULL;
5784 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5786 regmatch_t regmatch;
5788 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5792 static const struct got_error *
5793 search_next_tree_view(struct tog_view *view)
5795 struct tog_tree_view_state *s = &view->state.tree;
5796 struct got_tree_entry *te = NULL;
5798 if (!view->searching) {
5799 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5803 if (s->matched_entry) {
5804 if (view->searching == TOG_SEARCH_FORWARD) {
5805 if (s->selected_entry)
5806 te = got_tree_entry_get_next(s->tree,
5809 te = got_object_tree_get_first_entry(s->tree);
5811 if (s->selected_entry == NULL)
5812 te = got_object_tree_get_last_entry(s->tree);
5814 te = got_tree_entry_get_prev(s->tree,
5818 if (s->selected_entry)
5819 te = s->selected_entry;
5820 else if (view->searching == TOG_SEARCH_FORWARD)
5821 te = got_object_tree_get_first_entry(s->tree);
5823 te = got_object_tree_get_last_entry(s->tree);
5828 if (s->matched_entry == NULL) {
5829 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5832 if (view->searching == TOG_SEARCH_FORWARD)
5833 te = got_object_tree_get_first_entry(s->tree);
5835 te = got_object_tree_get_last_entry(s->tree);
5838 if (match_tree_entry(te, &view->regex)) {
5839 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5840 s->matched_entry = te;
5844 if (view->searching == TOG_SEARCH_FORWARD)
5845 te = got_tree_entry_get_next(s->tree, te);
5847 te = got_tree_entry_get_prev(s->tree, te);
5850 if (s->matched_entry) {
5851 s->first_displayed_entry = s->matched_entry;
5858 static const struct got_error *
5859 show_tree_view(struct tog_view *view)
5861 const struct got_error *err = NULL;
5862 struct tog_tree_view_state *s = &view->state.tree;
5865 err = tree_entry_path(&parent_path, &s->parents, NULL);
5869 err = draw_tree_entries(view, parent_path);
5876 static const struct got_error *
5877 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5879 const struct got_error *err = NULL;
5880 struct tog_tree_view_state *s = &view->state.tree;
5881 struct tog_view *log_view, *ref_view;
5882 struct got_tree_entry *te;
5883 int begin_x = 0, n, nscroll = view->nlines - 3;
5887 s->show_ids = !s->show_ids;
5890 if (!s->selected_entry)
5892 if (view_is_parent_view(view))
5893 begin_x = view_split_begin_x(view->begin_x);
5894 err = log_selected_tree_entry(&log_view, begin_x, s);
5896 log_view->focussed = 1;
5897 if (view_is_parent_view(view)) {
5898 err = view_close_child(view);
5901 err = view_set_child(view, log_view);
5904 view->focus_child = 1;
5906 *new_view = log_view;
5909 if (view_is_parent_view(view))
5910 begin_x = view_split_begin_x(view->begin_x);
5911 ref_view = view_open(view->nlines, view->ncols,
5912 view->begin_y, begin_x, TOG_VIEW_REF);
5913 if (ref_view == NULL)
5914 return got_error_from_errno("view_open");
5915 err = open_ref_view(ref_view, s->repo);
5917 view_close(ref_view);
5921 ref_view->focussed = 1;
5922 if (view_is_parent_view(view)) {
5923 err = view_close_child(view);
5926 err = view_set_child(view, ref_view);
5929 view->focus_child = 1;
5931 *new_view = ref_view;
5936 if (s->tree == s->root)
5937 s->first_displayed_entry =
5938 got_object_tree_get_first_entry(s->tree);
5940 s->first_displayed_entry = NULL;
5945 te = got_object_tree_get_last_entry(s->tree);
5946 for (n = 0; n < view->nlines - 3; n++) {
5948 if(s->tree != s->root) {
5949 s->first_displayed_entry = NULL;
5954 s->first_displayed_entry = te;
5955 te = got_tree_entry_get_prev(s->tree, te);
5958 s->selected = n - 1;
5963 if (s->selected > 0) {
5967 tree_scroll_up(s, 1);
5976 if (s->tree == s->root) {
5977 if (got_object_tree_get_first_entry(s->tree) ==
5978 s->first_displayed_entry)
5979 s->selected -= MIN(s->selected, nscroll);
5981 if (s->first_displayed_entry == NULL)
5982 s->selected -= MIN(s->selected, nscroll);
5984 tree_scroll_up(s, MAX(0, nscroll));
5989 if (s->selected < s->ndisplayed - 1) {
5993 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5995 /* can't scroll any further */
5997 tree_scroll_down(s, 1);
6006 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6008 /* can't scroll any further; move cursor down */
6009 if (s->selected < s->ndisplayed - 1)
6010 s->selected += MIN(nscroll,
6011 s->ndisplayed - s->selected - 1);
6014 tree_scroll_down(s, nscroll);
6019 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6020 struct tog_parent_tree *parent;
6021 /* user selected '..' */
6022 if (s->tree == s->root)
6024 parent = TAILQ_FIRST(&s->parents);
6025 TAILQ_REMOVE(&s->parents, parent,
6027 got_object_tree_close(s->tree);
6028 s->tree = parent->tree;
6029 s->first_displayed_entry =
6030 parent->first_displayed_entry;
6032 parent->selected_entry;
6033 s->selected = parent->selected;
6035 } else if (S_ISDIR(got_tree_entry_get_mode(
6036 s->selected_entry))) {
6037 struct got_tree_object *subtree;
6038 err = got_object_open_as_tree(&subtree, s->repo,
6039 got_tree_entry_get_id(s->selected_entry));
6042 err = tree_view_visit_subtree(s, subtree);
6044 got_object_tree_close(subtree);
6047 } else if (S_ISREG(got_tree_entry_get_mode(
6048 s->selected_entry))) {
6049 struct tog_view *blame_view;
6050 int begin_x = view_is_parent_view(view) ?
6051 view_split_begin_x(view->begin_x) : 0;
6053 err = blame_tree_entry(&blame_view, begin_x,
6054 s->selected_entry, &s->parents,
6055 s->commit_id, s->repo);
6059 blame_view->focussed = 1;
6060 if (view_is_parent_view(view)) {
6061 err = view_close_child(view);
6064 err = view_set_child(view, blame_view);
6067 view->focus_child = 1;
6069 *new_view = blame_view;
6073 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6074 s->selected = view->nlines - 4;
6088 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6093 static const struct got_error *
6094 cmd_tree(int argc, char *argv[])
6096 const struct got_error *error;
6097 struct got_repository *repo = NULL;
6098 struct got_worktree *worktree = NULL;
6099 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6100 struct got_object_id *commit_id = NULL;
6101 struct got_commit_object *commit = NULL;
6102 const char *commit_id_arg = NULL;
6104 struct got_reference *ref = NULL;
6105 const char *head_ref_name = NULL;
6107 struct tog_view *view;
6108 int *pack_fds = NULL;
6110 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6113 commit_id_arg = optarg;
6116 repo_path = realpath(optarg, NULL);
6117 if (repo_path == NULL)
6118 return got_error_from_errno2("realpath",
6133 error = got_repo_pack_fds_open(&pack_fds);
6137 if (repo_path == NULL) {
6138 cwd = getcwd(NULL, 0);
6140 return got_error_from_errno("getcwd");
6141 error = got_worktree_open(&worktree, cwd);
6142 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6146 strdup(got_worktree_get_repo_path(worktree));
6148 repo_path = strdup(cwd);
6149 if (repo_path == NULL) {
6150 error = got_error_from_errno("strdup");
6155 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6159 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6166 error = apply_unveil(got_repo_get_path(repo), NULL);
6170 error = tog_load_refs(repo, 0);
6174 if (commit_id_arg == NULL) {
6175 error = got_repo_match_object_id(&commit_id, &label,
6176 worktree ? got_worktree_get_head_ref_name(worktree) :
6177 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6180 head_ref_name = label;
6182 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6184 head_ref_name = got_ref_get_name(ref);
6185 else if (error->code != GOT_ERR_NOT_REF)
6187 error = got_repo_match_object_id(&commit_id, NULL,
6188 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6193 error = got_object_open_as_commit(&commit, repo, commit_id);
6197 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6199 error = got_error_from_errno("view_open");
6202 error = open_tree_view(view, commit_id, head_ref_name, repo);
6205 if (!got_path_is_root_dir(in_repo_path)) {
6206 error = tree_view_walk_path(&view->state.tree, commit,
6213 /* Release work tree lock. */
6214 got_worktree_close(worktree);
6217 error = view_loop(view);
6226 const struct got_error *close_err = got_repo_close(repo);
6231 const struct got_error *pack_err =
6232 got_repo_pack_fds_close(pack_fds);
6240 static const struct got_error *
6241 ref_view_load_refs(struct tog_ref_view_state *s)
6243 struct got_reflist_entry *sre;
6244 struct tog_reflist_entry *re;
6247 TAILQ_FOREACH(sre, &tog_refs, entry) {
6248 if (strncmp(got_ref_get_name(sre->ref),
6249 "refs/got/", 9) == 0 &&
6250 strncmp(got_ref_get_name(sre->ref),
6251 "refs/got/backup/", 16) != 0)
6254 re = malloc(sizeof(*re));
6256 return got_error_from_errno("malloc");
6258 re->ref = got_ref_dup(sre->ref);
6259 if (re->ref == NULL)
6260 return got_error_from_errno("got_ref_dup");
6261 re->idx = s->nrefs++;
6262 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6265 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6270 ref_view_free_refs(struct tog_ref_view_state *s)
6272 struct tog_reflist_entry *re;
6274 while (!TAILQ_EMPTY(&s->refs)) {
6275 re = TAILQ_FIRST(&s->refs);
6276 TAILQ_REMOVE(&s->refs, re, entry);
6277 got_ref_close(re->ref);
6282 static const struct got_error *
6283 open_ref_view(struct tog_view *view, struct got_repository *repo)
6285 const struct got_error *err = NULL;
6286 struct tog_ref_view_state *s = &view->state.ref;
6288 s->selected_entry = 0;
6291 TAILQ_INIT(&s->refs);
6292 STAILQ_INIT(&s->colors);
6294 err = ref_view_load_refs(s);
6298 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6299 err = add_color(&s->colors, "^refs/heads/",
6300 TOG_COLOR_REFS_HEADS,
6301 get_color_value("TOG_COLOR_REFS_HEADS"));
6305 err = add_color(&s->colors, "^refs/tags/",
6306 TOG_COLOR_REFS_TAGS,
6307 get_color_value("TOG_COLOR_REFS_TAGS"));
6311 err = add_color(&s->colors, "^refs/remotes/",
6312 TOG_COLOR_REFS_REMOTES,
6313 get_color_value("TOG_COLOR_REFS_REMOTES"));
6317 err = add_color(&s->colors, "^refs/got/backup/",
6318 TOG_COLOR_REFS_BACKUP,
6319 get_color_value("TOG_COLOR_REFS_BACKUP"));
6324 view->show = show_ref_view;
6325 view->input = input_ref_view;
6326 view->close = close_ref_view;
6327 view->search_start = search_start_ref_view;
6328 view->search_next = search_next_ref_view;
6331 free_colors(&s->colors);
6335 static const struct got_error *
6336 close_ref_view(struct tog_view *view)
6338 struct tog_ref_view_state *s = &view->state.ref;
6340 ref_view_free_refs(s);
6341 free_colors(&s->colors);
6346 static const struct got_error *
6347 resolve_reflist_entry(struct got_object_id **commit_id,
6348 struct tog_reflist_entry *re, struct got_repository *repo)
6350 const struct got_error *err = NULL;
6351 struct got_object_id *obj_id;
6352 struct got_tag_object *tag = NULL;
6357 err = got_ref_resolve(&obj_id, repo, re->ref);
6361 err = got_object_get_type(&obj_type, repo, obj_id);
6366 case GOT_OBJ_TYPE_COMMIT:
6367 *commit_id = obj_id;
6369 case GOT_OBJ_TYPE_TAG:
6370 err = got_object_open_as_tag(&tag, repo, obj_id);
6374 err = got_object_get_type(&obj_type, repo,
6375 got_object_tag_get_object_id(tag));
6378 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6379 err = got_error(GOT_ERR_OBJ_TYPE);
6382 *commit_id = got_object_id_dup(
6383 got_object_tag_get_object_id(tag));
6384 if (*commit_id == NULL) {
6385 err = got_error_from_errno("got_object_id_dup");
6390 err = got_error(GOT_ERR_OBJ_TYPE);
6396 got_object_tag_close(tag);
6404 static const struct got_error *
6405 log_ref_entry(struct tog_view **new_view, int begin_x,
6406 struct tog_reflist_entry *re, struct got_repository *repo)
6408 struct tog_view *log_view;
6409 const struct got_error *err = NULL;
6410 struct got_object_id *commit_id = NULL;
6414 err = resolve_reflist_entry(&commit_id, re, repo);
6416 if (err->code != GOT_ERR_OBJ_TYPE)
6422 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6423 if (log_view == NULL) {
6424 err = got_error_from_errno("view_open");
6428 err = open_log_view(log_view, commit_id, repo,
6429 got_ref_get_name(re->ref), "", 0);
6432 view_close(log_view);
6434 *new_view = log_view;
6440 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6442 struct tog_reflist_entry *re;
6445 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6448 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6449 while (i++ < maxscroll) {
6452 s->first_displayed_entry = re;
6453 re = TAILQ_PREV(re, tog_reflist_head, entry);
6458 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6460 struct tog_reflist_entry *next, *last;
6463 if (s->first_displayed_entry)
6464 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6466 next = TAILQ_FIRST(&s->refs);
6468 last = s->last_displayed_entry;
6469 while (next && last && n++ < maxscroll) {
6470 last = TAILQ_NEXT(last, entry);
6472 s->first_displayed_entry = next;
6473 next = TAILQ_NEXT(next, entry);
6478 static const struct got_error *
6479 search_start_ref_view(struct tog_view *view)
6481 struct tog_ref_view_state *s = &view->state.ref;
6483 s->matched_entry = NULL;
6488 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6490 regmatch_t regmatch;
6492 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6496 static const struct got_error *
6497 search_next_ref_view(struct tog_view *view)
6499 struct tog_ref_view_state *s = &view->state.ref;
6500 struct tog_reflist_entry *re = NULL;
6502 if (!view->searching) {
6503 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6507 if (s->matched_entry) {
6508 if (view->searching == TOG_SEARCH_FORWARD) {
6509 if (s->selected_entry)
6510 re = TAILQ_NEXT(s->selected_entry, entry);
6512 re = TAILQ_PREV(s->selected_entry,
6513 tog_reflist_head, entry);
6515 if (s->selected_entry == NULL)
6516 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6518 re = TAILQ_PREV(s->selected_entry,
6519 tog_reflist_head, entry);
6522 if (s->selected_entry)
6523 re = s->selected_entry;
6524 else if (view->searching == TOG_SEARCH_FORWARD)
6525 re = TAILQ_FIRST(&s->refs);
6527 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6532 if (s->matched_entry == NULL) {
6533 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6536 if (view->searching == TOG_SEARCH_FORWARD)
6537 re = TAILQ_FIRST(&s->refs);
6539 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6542 if (match_reflist_entry(re, &view->regex)) {
6543 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6544 s->matched_entry = re;
6548 if (view->searching == TOG_SEARCH_FORWARD)
6549 re = TAILQ_NEXT(re, entry);
6551 re = TAILQ_PREV(re, tog_reflist_head, entry);
6554 if (s->matched_entry) {
6555 s->first_displayed_entry = s->matched_entry;
6562 static const struct got_error *
6563 show_ref_view(struct tog_view *view)
6565 const struct got_error *err = NULL;
6566 struct tog_ref_view_state *s = &view->state.ref;
6567 struct tog_reflist_entry *re;
6570 struct tog_color *tc;
6572 int limit = view->nlines;
6574 werase(view->window);
6581 re = s->first_displayed_entry;
6583 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6585 return got_error_from_errno("asprintf");
6587 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6592 if (view_needs_focus_indication(view))
6593 wstandout(view->window);
6594 waddwstr(view->window, wline);
6595 if (view_needs_focus_indication(view))
6596 wstandend(view->window);
6601 if (width < view->ncols - 1)
6602 waddch(view->window, '\n');
6607 while (re && limit > 0) {
6609 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6612 struct got_commit_object *ci;
6613 struct got_tag_object *tag;
6614 struct got_object_id *id;
6618 err = got_ref_resolve(&id, s->repo, re->ref);
6621 err = got_object_open_as_tag(&tag, s->repo, id);
6623 if (err->code != GOT_ERR_OBJ_TYPE) {
6627 err = got_object_open_as_commit(&ci, s->repo,
6633 t = got_object_commit_get_committer_time(ci);
6634 got_object_commit_close(ci);
6636 t = got_object_tag_get_tagger_time(tag);
6637 got_object_tag_close(tag);
6640 if (gmtime_r(&t, &tm) == NULL)
6641 return got_error_from_errno("gmtime_r");
6642 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6643 return got_error(GOT_ERR_NO_SPACE);
6645 if (got_ref_is_symbolic(re->ref)) {
6646 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6647 ymd : "", got_ref_get_name(re->ref),
6648 got_ref_get_symref_target(re->ref)) == -1)
6649 return got_error_from_errno("asprintf");
6650 } else if (s->show_ids) {
6651 struct got_object_id *id;
6653 err = got_ref_resolve(&id, s->repo, re->ref);
6656 err = got_object_id_str(&id_str, id);
6661 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6662 got_ref_get_name(re->ref), id_str) == -1) {
6663 err = got_error_from_errno("asprintf");
6670 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6671 got_ref_get_name(re->ref)) == -1)
6672 return got_error_from_errno("asprintf");
6674 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6680 if (n == s->selected) {
6682 wstandout(view->window);
6683 s->selected_entry = re;
6685 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6687 wattr_on(view->window,
6688 COLOR_PAIR(tc->colorpair), NULL);
6689 waddwstr(view->window, wline);
6691 wattr_off(view->window,
6692 COLOR_PAIR(tc->colorpair), NULL);
6693 if (width < view->ncols - 1)
6694 waddch(view->window, '\n');
6695 if (n == s->selected && view->focussed)
6696 wstandend(view->window);
6702 s->last_displayed_entry = re;
6705 re = TAILQ_NEXT(re, entry);
6712 static const struct got_error *
6713 browse_ref_tree(struct tog_view **new_view, int begin_x,
6714 struct tog_reflist_entry *re, struct got_repository *repo)
6716 const struct got_error *err = NULL;
6717 struct got_object_id *commit_id = NULL;
6718 struct tog_view *tree_view;
6722 err = resolve_reflist_entry(&commit_id, re, repo);
6724 if (err->code != GOT_ERR_OBJ_TYPE)
6731 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6732 if (tree_view == NULL) {
6733 err = got_error_from_errno("view_open");
6737 err = open_tree_view(tree_view, commit_id,
6738 got_ref_get_name(re->ref), repo);
6742 *new_view = tree_view;
6747 static const struct got_error *
6748 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6750 const struct got_error *err = NULL;
6751 struct tog_ref_view_state *s = &view->state.ref;
6752 struct tog_view *log_view, *tree_view;
6753 struct tog_reflist_entry *re;
6754 int begin_x = 0, n, nscroll = view->nlines - 1;
6758 s->show_ids = !s->show_ids;
6761 s->show_date = !s->show_date;
6764 s->sort_by_date = !s->sort_by_date;
6765 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6766 got_ref_cmp_by_commit_timestamp_descending :
6767 tog_ref_cmp_by_name, s->repo);
6770 got_reflist_object_id_map_free(tog_refs_idmap);
6771 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6772 &tog_refs, s->repo);
6775 ref_view_free_refs(s);
6776 err = ref_view_load_refs(s);
6780 if (!s->selected_entry)
6782 if (view_is_parent_view(view))
6783 begin_x = view_split_begin_x(view->begin_x);
6784 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6787 log_view->focussed = 1;
6788 if (view_is_parent_view(view)) {
6789 err = view_close_child(view);
6792 err = view_set_child(view, log_view);
6795 view->focus_child = 1;
6797 *new_view = log_view;
6800 if (!s->selected_entry)
6802 if (view_is_parent_view(view))
6803 begin_x = view_split_begin_x(view->begin_x);
6804 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6806 if (err || tree_view == NULL)
6809 tree_view->focussed = 1;
6810 if (view_is_parent_view(view)) {
6811 err = view_close_child(view);
6814 err = view_set_child(view, tree_view);
6817 view->focus_child = 1;
6819 *new_view = tree_view;
6824 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6829 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6830 for (n = 0; n < view->nlines - 1; n++) {
6833 s->first_displayed_entry = re;
6834 re = TAILQ_PREV(re, tog_reflist_head, entry);
6837 s->selected = n - 1;
6842 if (s->selected > 0) {
6846 ref_scroll_up(s, 1);
6855 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6856 s->selected -= MIN(nscroll, s->selected);
6857 ref_scroll_up(s, MAX(0, nscroll));
6862 if (s->selected < s->ndisplayed - 1) {
6866 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6867 /* can't scroll any further */
6869 ref_scroll_down(s, 1);
6878 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6879 /* can't scroll any further; move cursor down */
6880 if (s->selected < s->ndisplayed - 1)
6881 s->selected += MIN(nscroll,
6882 s->ndisplayed - s->selected - 1);
6885 ref_scroll_down(s, nscroll);
6889 err = tog_load_refs(s->repo, s->sort_by_date);
6892 ref_view_free_refs(s);
6893 err = ref_view_load_refs(s);
6896 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6897 s->selected = view->nlines - 2;
6910 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6915 static const struct got_error *
6916 cmd_ref(int argc, char *argv[])
6918 const struct got_error *error;
6919 struct got_repository *repo = NULL;
6920 struct got_worktree *worktree = NULL;
6921 char *cwd = NULL, *repo_path = NULL;
6923 struct tog_view *view;
6924 int *pack_fds = NULL;
6926 while ((ch = getopt(argc, argv, "r:")) != -1) {
6929 repo_path = realpath(optarg, NULL);
6930 if (repo_path == NULL)
6931 return got_error_from_errno2("realpath",
6946 error = got_repo_pack_fds_open(&pack_fds);
6950 if (repo_path == NULL) {
6951 cwd = getcwd(NULL, 0);
6953 return got_error_from_errno("getcwd");
6954 error = got_worktree_open(&worktree, cwd);
6955 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6959 strdup(got_worktree_get_repo_path(worktree));
6961 repo_path = strdup(cwd);
6962 if (repo_path == NULL) {
6963 error = got_error_from_errno("strdup");
6968 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6974 error = apply_unveil(got_repo_get_path(repo), NULL);
6978 error = tog_load_refs(repo, 0);
6982 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6984 error = got_error_from_errno("view_open");
6988 error = open_ref_view(view, repo);
6993 /* Release work tree lock. */
6994 got_worktree_close(worktree);
6997 error = view_loop(view);
7002 const struct got_error *close_err = got_repo_close(repo);
7007 const struct got_error *pack_err =
7008 got_repo_pack_fds_close(pack_fds);
7017 list_commands(FILE *fp)
7021 fprintf(fp, "commands:");
7022 for (i = 0; i < nitems(tog_commands); i++) {
7023 const struct tog_cmd *cmd = &tog_commands[i];
7024 fprintf(fp, " %s", cmd->name);
7030 usage(int hflag, int status)
7032 FILE *fp = (status == 0) ? stdout : stderr;
7034 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7037 fprintf(fp, "lazy usage: %s path\n", getprogname());
7044 make_argv(int argc, ...)
7052 argv = calloc(argc, sizeof(char *));
7055 for (i = 0; i < argc; i++) {
7056 argv[i] = strdup(va_arg(ap, char *));
7057 if (argv[i] == NULL)
7066 * Try to convert 'tog path' into a 'tog log path' command.
7067 * The user could simply have mistyped the command rather than knowingly
7068 * provided a path. So check whether argv[0] can in fact be resolved
7069 * to a path in the HEAD commit and print a special error if not.
7070 * This hack is for mpi@ <3
7072 static const struct got_error *
7073 tog_log_with_path(int argc, char *argv[])
7075 const struct got_error *error = NULL, *close_err;
7076 const struct tog_cmd *cmd = NULL;
7077 struct got_repository *repo = NULL;
7078 struct got_worktree *worktree = NULL;
7079 struct got_object_id *commit_id = NULL, *id = NULL;
7080 struct got_commit_object *commit = NULL;
7081 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7082 char *commit_id_str = NULL, **cmd_argv = NULL;
7083 int *pack_fds = NULL;
7085 cwd = getcwd(NULL, 0);
7087 return got_error_from_errno("getcwd");
7089 error = got_repo_pack_fds_open(&pack_fds);
7093 error = got_worktree_open(&worktree, cwd);
7094 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7098 repo_path = strdup(got_worktree_get_repo_path(worktree));
7100 repo_path = strdup(cwd);
7101 if (repo_path == NULL) {
7102 error = got_error_from_errno("strdup");
7106 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7110 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7115 error = tog_load_refs(repo, 0);
7118 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7119 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7120 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7125 got_worktree_close(worktree);
7129 error = got_object_open_as_commit(&commit, repo, commit_id);
7133 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7135 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7137 fprintf(stderr, "%s: '%s' is no known command or path\n",
7138 getprogname(), argv[0]);
7143 close_err = got_repo_close(repo);
7148 error = got_object_id_str(&commit_id_str, commit_id);
7152 cmd = &tog_commands[0]; /* log */
7154 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7155 error = cmd->cmd_main(argc, cmd_argv);
7158 close_err = got_repo_close(repo);
7163 got_object_commit_close(commit);
7165 got_worktree_close(worktree);
7167 const struct got_error *pack_err =
7168 got_repo_pack_fds_close(pack_fds);
7173 free(commit_id_str);
7180 for (i = 0; i < argc; i++)
7189 main(int argc, char *argv[])
7191 const struct got_error *error = NULL;
7192 const struct tog_cmd *cmd = NULL;
7193 int ch, hflag = 0, Vflag = 0;
7194 char **cmd_argv = NULL;
7195 static const struct option longopts[] = {
7196 { "version", no_argument, NULL, 'V' },
7200 setlocale(LC_CTYPE, "");
7202 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7222 got_version_print_str();
7227 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7235 /* Build an argument vector which runs a default command. */
7236 cmd = &tog_commands[0];
7238 cmd_argv = make_argv(argc, cmd->name);
7242 /* Did the user specify a command? */
7243 for (i = 0; i < nitems(tog_commands); i++) {
7244 if (strncmp(tog_commands[i].name, argv[0],
7245 strlen(argv[0])) == 0) {
7246 cmd = &tog_commands[i];
7255 /* No command specified; try log with a path */
7256 error = tog_log_with_path(argc, argv);
7261 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7268 for (i = 0; i < argc; i++)
7273 if (error && error->code != GOT_ERR_CANCELLED)
7274 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);