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;
751 static const struct got_error *
752 view_resize(struct tog_view *view)
756 if (view->lines > LINES)
757 nlines = view->nlines - (view->lines - LINES);
759 nlines = view->nlines + (LINES - view->lines);
761 if (view->cols > COLS)
762 ncols = view->ncols - (view->cols - COLS);
764 ncols = view->ncols + (COLS - view->cols);
767 view->child->begin_x = view_split_begin_x(view->begin_x);
768 if (view->child->begin_x == 0) {
771 view_fullscreen(view->child);
772 if (view->child->focussed)
773 show_panel(view->child->panel);
775 show_panel(view->panel);
777 ncols = view->child->begin_x;
779 view_splitscreen(view->child);
780 show_panel(view->child->panel);
782 } else if (view->parent == NULL)
785 if (wresize(view->window, nlines, ncols) == ERR)
786 return got_error_from_errno("wresize");
787 if (replace_panel(view->panel, view->window) == ERR)
788 return got_error_from_errno("replace_panel");
789 wclear(view->window);
791 view->nlines = nlines;
799 static const struct got_error *
800 view_close_child(struct tog_view *view)
802 const struct got_error *err = NULL;
804 if (view->child == NULL)
807 err = view_close(view->child);
812 static const struct got_error *
813 view_set_child(struct tog_view *view, struct tog_view *child)
816 child->parent = view;
818 return view_resize(view);
822 view_is_splitscreen(struct tog_view *view)
824 return view->begin_x > 0;
833 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 cols = 80; /* Default */
840 resize_term(lines, cols);
843 static const struct got_error *
844 view_search_start(struct tog_view *view)
846 const struct got_error *err = NULL;
850 if (view->search_started) {
851 regfree(&view->regex);
853 memset(&view->regmatch, 0, sizeof(view->regmatch));
855 view->search_started = 0;
857 if (view->nlines < 1)
860 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 wclrtoeol(view->window);
865 ret = wgetnstr(view->window, pattern, sizeof(pattern));
871 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 err = view->search_start(view);
874 regfree(&view->regex);
877 view->search_started = 1;
878 view->searching = TOG_SEARCH_FORWARD;
879 view->search_next_done = 0;
880 view->search_next(view);
886 static const struct got_error *
887 view_input(struct tog_view **new, int *done, struct tog_view *view,
888 struct tog_view_list_head *views)
890 const struct got_error *err = NULL;
896 /* Clear "no matches" indicator. */
897 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 view->search_next_done = TOG_SEARCH_HAVE_MORE;
901 if (view->searching && !view->search_next_done) {
902 errcode = pthread_mutex_unlock(&tog_mutex);
904 return got_error_set_errno(errcode,
905 "pthread_mutex_unlock");
907 errcode = pthread_mutex_lock(&tog_mutex);
909 return got_error_set_errno(errcode,
910 "pthread_mutex_lock");
911 view->search_next(view);
915 nodelay(stdscr, FALSE);
916 /* Allow threads to make progress while we are waiting for input. */
917 errcode = pthread_mutex_unlock(&tog_mutex);
919 return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 ch = wgetch(view->window);
921 errcode = pthread_mutex_lock(&tog_mutex);
923 return got_error_set_errno(errcode, "pthread_mutex_lock");
924 nodelay(stdscr, TRUE);
926 if (tog_sigwinch_received || tog_sigcont_received) {
928 tog_sigwinch_received = 0;
929 tog_sigcont_received = 0;
930 TAILQ_FOREACH(v, views, entry) {
931 err = view_resize(v);
934 err = v->input(new, v, KEY_RESIZE);
938 err = view_resize(v->child);
941 err = v->child->input(new, v->child,
953 view->child->focussed = 1;
954 view->focus_child = 1;
955 } else if (view->parent) {
957 view->parent->focussed = 1;
958 view->parent->focus_child = 0;
962 err = view->input(new, view, ch);
969 if (view_is_parent_view(view)) {
970 if (view->child == NULL)
972 if (view_is_splitscreen(view->child)) {
974 view->child->focussed = 1;
975 err = view_fullscreen(view->child);
977 err = view_splitscreen(view->child);
980 err = view->child->input(new, view->child,
983 if (view_is_splitscreen(view)) {
984 view->parent->focussed = 0;
986 err = view_fullscreen(view);
988 err = view_splitscreen(view);
992 err = view->input(new, view, KEY_RESIZE);
998 if (view->search_start)
999 view_search_start(view);
1001 err = view->input(new, view, ch);
1005 if (view->search_started && view->search_next) {
1006 view->searching = (ch == 'n' ?
1007 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 view->search_next_done = 0;
1009 view->search_next(view);
1011 err = view->input(new, view, ch);
1014 err = view->input(new, view, ch);
1022 view_vborder(struct tog_view *view)
1025 const struct tog_view *view_above;
1028 return view_vborder(view->parent);
1030 panel = panel_above(view->panel);
1034 view_above = panel_userptr(panel);
1035 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1040 view_needs_focus_indication(struct tog_view *view)
1042 if (view_is_parent_view(view)) {
1043 if (view->child == NULL || view->child->focussed)
1045 if (!view_is_splitscreen(view->child))
1047 } else if (!view_is_splitscreen(view))
1050 return view->focussed;
1053 static const struct got_error *
1054 view_loop(struct tog_view *view)
1056 const struct got_error *err = NULL;
1057 struct tog_view_list_head views;
1058 struct tog_view *new_view;
1059 int fast_refresh = 10;
1060 int done = 0, errcode;
1062 errcode = pthread_mutex_lock(&tog_mutex);
1064 return got_error_set_errno(errcode, "pthread_mutex_lock");
1067 TAILQ_INSERT_HEAD(&views, view, entry);
1070 err = view->show(view);
1075 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 /* Refresh fast during initialization, then become slower. */
1077 if (fast_refresh && fast_refresh-- == 0)
1078 halfdelay(10); /* switch to once per second */
1080 err = view_input(&new_view, &done, view, &views);
1084 struct tog_view *v, *prev = NULL;
1086 if (view_is_parent_view(view))
1087 prev = TAILQ_PREV(view, tog_view_list_head,
1089 else if (view->parent)
1090 prev = view->parent;
1093 view->parent->child = NULL;
1094 view->parent->focus_child = 0;
1096 err = view_resize(view->parent);
1100 TAILQ_REMOVE(&views, view, entry);
1102 err = view_close(view);
1107 TAILQ_FOREACH(v, &views, entry) {
1111 if (view == NULL && new_view == NULL) {
1112 /* No view has focus. Try to pick one. */
1115 else if (!TAILQ_EMPTY(&views)) {
1116 view = TAILQ_LAST(&views,
1117 tog_view_list_head);
1120 if (view->focus_child) {
1121 view->child->focussed = 1;
1129 struct tog_view *v, *t;
1130 /* Only allow one parent view per type. */
1131 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1132 if (v->type != new_view->type)
1134 TAILQ_REMOVE(&views, v, entry);
1135 err = view_close(v);
1140 TAILQ_INSERT_TAIL(&views, new_view, entry);
1144 if (view_is_parent_view(view)) {
1145 if (view->child && view->child->focussed)
1148 if (view->parent && view->parent->focussed)
1149 view = view->parent;
1151 show_panel(view->panel);
1152 if (view->child && view_is_splitscreen(view->child))
1153 show_panel(view->child->panel);
1154 if (view->parent && view_is_splitscreen(view)) {
1155 err = view->parent->show(view->parent);
1159 err = view->show(view);
1163 err = view->child->show(view->child);
1172 while (!TAILQ_EMPTY(&views)) {
1173 view = TAILQ_FIRST(&views);
1174 TAILQ_REMOVE(&views, view, entry);
1178 errcode = pthread_mutex_unlock(&tog_mutex);
1179 if (errcode && err == NULL)
1180 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1190 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1195 /* Create newly allocated wide-character string equivalent to a byte string. */
1196 static const struct got_error *
1197 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1200 const struct got_error *err = NULL;
1203 *wlen = mbstowcs(NULL, s, 0);
1204 if (*wlen == (size_t)-1) {
1206 if (errno != EILSEQ)
1207 return got_error_from_errno("mbstowcs");
1209 /* byte string invalid in current encoding; try to "fix" it */
1210 err = got_mbsavis(&vis, &vislen, s);
1213 *wlen = mbstowcs(NULL, vis, 0);
1214 if (*wlen == (size_t)-1) {
1215 err = got_error_from_errno("mbstowcs"); /* give up */
1220 *ws = calloc(*wlen + 1, sizeof(**ws));
1222 err = got_error_from_errno("calloc");
1226 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1227 err = got_error_from_errno("mbstowcs");
1238 static const struct got_error *
1239 expand_tab(char **ptr, const char *src)
1242 size_t len, n, idx = 0, sz = 0;
1245 n = len = strlen(src);
1246 dst = malloc(n + 1);
1248 return got_error_from_errno("malloc");
1250 while (idx < len && src[idx]) {
1251 const char c = src[idx];
1254 size_t nb = TABSIZE - sz % TABSIZE;
1257 p = realloc(dst, n + nb);
1260 return got_error_from_errno("realloc");
1265 memset(dst + sz, ' ', nb);
1268 dst[sz++] = src[idx];
1278 * Advance at most n columns from wline starting at offset off.
1279 * Return the index to the first character after the span operation.
1280 * Return the combined column width of all spanned wide character in
1284 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1286 int width, i, cols = 0;
1293 for (i = off; wline[i] != L'\0'; ++i) {
1294 if (wline[i] == L'\t')
1295 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1297 width = wcwidth(wline[i]);
1304 if (cols + width > n)
1314 * Format a line for display, ensuring that it won't overflow a width limit.
1315 * With scrolling, the width returned refers to the scrolled version of the
1316 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1318 static const struct got_error *
1319 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1320 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1322 const struct got_error *err = NULL;
1324 wchar_t *wline = NULL;
1333 err = expand_tab(&exstr, line);
1338 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1343 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1345 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1346 wline[wlen - 1] = L'\0';
1349 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1350 wline[wlen - 1] = L'\0';
1354 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1360 *scrollxp = scrollx;
1368 static const struct got_error*
1369 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1370 struct got_object_id *id, struct got_repository *repo)
1372 static const struct got_error *err = NULL;
1373 struct got_reflist_entry *re;
1379 TAILQ_FOREACH(re, refs, entry) {
1380 struct got_tag_object *tag = NULL;
1381 struct got_object_id *ref_id;
1384 name = got_ref_get_name(re->ref);
1385 if (strcmp(name, GOT_REF_HEAD) == 0)
1387 if (strncmp(name, "refs/", 5) == 0)
1389 if (strncmp(name, "got/", 4) == 0 &&
1390 strncmp(name, "got/backup/", 11) != 0)
1392 if (strncmp(name, "heads/", 6) == 0)
1394 if (strncmp(name, "remotes/", 8) == 0) {
1396 s = strstr(name, "/" GOT_REF_HEAD);
1397 if (s != NULL && s[strlen(s)] == '\0')
1400 err = got_ref_resolve(&ref_id, repo, re->ref);
1403 if (strncmp(name, "tags/", 5) == 0) {
1404 err = got_object_open_as_tag(&tag, repo, ref_id);
1406 if (err->code != GOT_ERR_OBJ_TYPE) {
1410 /* Ref points at something other than a tag. */
1415 cmp = got_object_id_cmp(tag ?
1416 got_object_tag_get_object_id(tag) : ref_id, id);
1419 got_object_tag_close(tag);
1423 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1424 s ? ", " : "", name) == -1) {
1425 err = got_error_from_errno("asprintf");
1436 static const struct got_error *
1437 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1442 smallerthan = strchr(author, '<');
1443 if (smallerthan && smallerthan[1] != '\0')
1444 author = smallerthan + 1;
1445 author[strcspn(author, "@>")] = '\0';
1446 return format_line(wauthor, author_width, NULL, author, 0, limit,
1450 static const struct got_error *
1451 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1452 struct got_object_id *id, const size_t date_display_cols,
1453 int author_display_cols)
1455 struct tog_log_view_state *s = &view->state.log;
1456 const struct got_error *err = NULL;
1457 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1458 char *logmsg0 = NULL, *logmsg = NULL;
1459 char *author = NULL;
1460 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1461 int author_width, logmsg_width;
1462 char *newline, *line = NULL;
1463 int col, limit, scrollx;
1464 const int avail = view->ncols;
1466 time_t committer_time;
1467 struct tog_color *tc;
1469 committer_time = got_object_commit_get_committer_time(commit);
1470 if (gmtime_r(&committer_time, &tm) == NULL)
1471 return got_error_from_errno("gmtime_r");
1472 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1473 return got_error(GOT_ERR_NO_SPACE);
1475 if (avail <= date_display_cols)
1476 limit = MIN(sizeof(datebuf) - 1, avail);
1478 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1479 tc = get_color(&s->colors, TOG_COLOR_DATE);
1481 wattr_on(view->window,
1482 COLOR_PAIR(tc->colorpair), NULL);
1483 waddnstr(view->window, datebuf, limit);
1485 wattr_off(view->window,
1486 COLOR_PAIR(tc->colorpair), NULL);
1493 err = got_object_id_str(&id_str, id);
1496 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1498 wattr_on(view->window,
1499 COLOR_PAIR(tc->colorpair), NULL);
1500 wprintw(view->window, "%.8s ", id_str);
1502 wattr_off(view->window,
1503 COLOR_PAIR(tc->colorpair), NULL);
1510 author = strdup(got_object_commit_get_author(commit));
1511 if (author == NULL) {
1512 err = got_error_from_errno("strdup");
1515 err = format_author(&wauthor, &author_width, author, avail - col, col);
1518 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1520 wattr_on(view->window,
1521 COLOR_PAIR(tc->colorpair), NULL);
1522 waddwstr(view->window, wauthor);
1524 wattr_off(view->window,
1525 COLOR_PAIR(tc->colorpair), NULL);
1526 col += author_width;
1527 while (col < avail && author_width < author_display_cols + 2) {
1528 waddch(view->window, ' ');
1535 err = got_object_commit_get_logmsg(&logmsg0, commit);
1539 while (*logmsg == '\n')
1541 newline = strchr(logmsg, '\n');
1544 limit = avail - col;
1545 if (view->child && limit > 0)
1546 limit--; /* for the border */
1547 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1551 waddwstr(view->window, &wlogmsg[scrollx]);
1552 col += MAX(logmsg_width, 0);
1553 while (col < avail) {
1554 waddch(view->window, ' ');
1566 static struct commit_queue_entry *
1567 alloc_commit_queue_entry(struct got_commit_object *commit,
1568 struct got_object_id *id)
1570 struct commit_queue_entry *entry;
1572 entry = calloc(1, sizeof(*entry));
1577 entry->commit = commit;
1582 pop_commit(struct commit_queue *commits)
1584 struct commit_queue_entry *entry;
1586 entry = TAILQ_FIRST(&commits->head);
1587 TAILQ_REMOVE(&commits->head, entry, entry);
1588 got_object_commit_close(entry->commit);
1589 commits->ncommits--;
1590 /* Don't free entry->id! It is owned by the commit graph. */
1595 free_commits(struct commit_queue *commits)
1597 while (!TAILQ_EMPTY(&commits->head))
1598 pop_commit(commits);
1601 static const struct got_error *
1602 match_commit(int *have_match, struct got_object_id *id,
1603 struct got_commit_object *commit, regex_t *regex)
1605 const struct got_error *err = NULL;
1606 regmatch_t regmatch;
1607 char *id_str = NULL, *logmsg = NULL;
1611 err = got_object_id_str(&id_str, id);
1615 err = got_object_commit_get_logmsg(&logmsg, commit);
1619 if (regexec(regex, got_object_commit_get_author(commit), 1,
1620 ®match, 0) == 0 ||
1621 regexec(regex, got_object_commit_get_committer(commit), 1,
1622 ®match, 0) == 0 ||
1623 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1624 regexec(regex, logmsg, 1, ®match, 0) == 0)
1632 static const struct got_error *
1633 queue_commits(struct tog_log_thread_args *a)
1635 const struct got_error *err = NULL;
1638 * We keep all commits open throughout the lifetime of the log
1639 * view in order to avoid having to re-fetch commits from disk
1640 * while updating the display.
1643 struct got_object_id *id;
1644 struct got_commit_object *commit;
1645 struct commit_queue_entry *entry;
1648 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1650 if (err || id == NULL)
1653 err = got_object_open_as_commit(&commit, a->repo, id);
1656 entry = alloc_commit_queue_entry(commit, id);
1657 if (entry == NULL) {
1658 err = got_error_from_errno("alloc_commit_queue_entry");
1662 errcode = pthread_mutex_lock(&tog_mutex);
1664 err = got_error_set_errno(errcode,
1665 "pthread_mutex_lock");
1669 entry->idx = a->commits->ncommits;
1670 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1671 a->commits->ncommits++;
1673 if (*a->searching == TOG_SEARCH_FORWARD &&
1674 !*a->search_next_done) {
1676 err = match_commit(&have_match, id, commit, a->regex);
1680 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1683 errcode = pthread_mutex_unlock(&tog_mutex);
1684 if (errcode && err == NULL)
1685 err = got_error_set_errno(errcode,
1686 "pthread_mutex_unlock");
1689 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1695 select_commit(struct tog_log_view_state *s)
1697 struct commit_queue_entry *entry;
1700 entry = s->first_displayed_entry;
1702 if (ncommits == s->selected) {
1703 s->selected_entry = entry;
1706 entry = TAILQ_NEXT(entry, entry);
1711 static const struct got_error *
1712 draw_commits(struct tog_view *view)
1714 const struct got_error *err = NULL;
1715 struct tog_log_view_state *s = &view->state.log;
1716 struct commit_queue_entry *entry = s->selected_entry;
1717 const int limit = view->nlines;
1719 int ncommits, author_cols = 4;
1720 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1721 char *refs_str = NULL;
1723 struct tog_color *tc;
1724 static const size_t date_display_cols = 12;
1726 if (s->selected_entry &&
1727 !(view->searching && view->search_next_done == 0)) {
1728 struct got_reflist_head *refs;
1729 err = got_object_id_str(&id_str, s->selected_entry->id);
1732 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1733 s->selected_entry->id);
1735 err = build_refs_str(&refs_str, refs,
1736 s->selected_entry->id, s->repo);
1742 if (s->thread_args.commits_needed == 0)
1743 halfdelay(10); /* disable fast refresh */
1745 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1746 if (asprintf(&ncommits_str, " [%d/%d] %s",
1747 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1748 (view->searching && !view->search_next_done) ?
1749 "searching..." : "loading...") == -1) {
1750 err = got_error_from_errno("asprintf");
1754 const char *search_str = NULL;
1756 if (view->searching) {
1757 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1758 search_str = "no more matches";
1759 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1760 search_str = "no matches found";
1761 else if (!view->search_next_done)
1762 search_str = "searching...";
1765 if (asprintf(&ncommits_str, " [%d/%d] %s",
1766 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1767 search_str ? search_str :
1768 (refs_str ? refs_str : "")) == -1) {
1769 err = got_error_from_errno("asprintf");
1774 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1775 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1776 "........................................",
1777 s->in_repo_path, ncommits_str) == -1) {
1778 err = got_error_from_errno("asprintf");
1782 } else if (asprintf(&header, "commit %s%s",
1783 id_str ? id_str : "........................................",
1784 ncommits_str) == -1) {
1785 err = got_error_from_errno("asprintf");
1789 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1793 werase(view->window);
1795 if (view_needs_focus_indication(view))
1796 wstandout(view->window);
1797 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1799 wattr_on(view->window,
1800 COLOR_PAIR(tc->colorpair), NULL);
1801 waddwstr(view->window, wline);
1803 wattr_off(view->window,
1804 COLOR_PAIR(tc->colorpair), NULL);
1805 while (width < view->ncols) {
1806 waddch(view->window, ' ');
1809 if (view_needs_focus_indication(view))
1810 wstandend(view->window);
1815 /* Grow author column size if necessary, and set view->maxx. */
1816 entry = s->first_displayed_entry;
1820 char *author, *eol, *msg, *msg0;
1821 wchar_t *wauthor, *wmsg;
1823 if (ncommits >= limit - 1)
1825 author = strdup(got_object_commit_get_author(entry->commit));
1826 if (author == NULL) {
1827 err = got_error_from_errno("strdup");
1830 err = format_author(&wauthor, &width, author, COLS,
1832 if (author_cols < width)
1833 author_cols = width;
1836 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1840 while (*msg == '\n')
1842 if ((eol = strchr(msg, '\n')))
1844 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1845 date_display_cols + author_cols, 0);
1848 view->maxx = MAX(view->maxx, width);
1852 entry = TAILQ_NEXT(entry, entry);
1855 entry = s->first_displayed_entry;
1856 s->last_displayed_entry = s->first_displayed_entry;
1859 if (ncommits >= limit - 1)
1861 if (ncommits == s->selected)
1862 wstandout(view->window);
1863 err = draw_commit(view, entry->commit, entry->id,
1864 date_display_cols, author_cols);
1865 if (ncommits == s->selected)
1866 wstandend(view->window);
1870 s->last_displayed_entry = entry;
1871 entry = TAILQ_NEXT(entry, entry);
1884 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1886 struct commit_queue_entry *entry;
1889 entry = TAILQ_FIRST(&s->commits.head);
1890 if (s->first_displayed_entry == entry)
1893 entry = s->first_displayed_entry;
1894 while (entry && nscrolled < maxscroll) {
1895 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1897 s->first_displayed_entry = entry;
1903 static const struct got_error *
1904 trigger_log_thread(struct tog_view *view, int wait)
1906 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1909 halfdelay(1); /* fast refresh while loading commits */
1911 while (ta->commits_needed > 0 || ta->load_all) {
1912 if (ta->log_complete)
1915 /* Wake the log thread. */
1916 errcode = pthread_cond_signal(&ta->need_commits);
1918 return got_error_set_errno(errcode,
1919 "pthread_cond_signal");
1922 * The mutex will be released while the view loop waits
1923 * in wgetch(), at which time the log thread will run.
1928 /* Display progress update in log view. */
1929 show_log_view(view);
1933 /* Wait right here while next commit is being loaded. */
1934 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1936 return got_error_set_errno(errcode,
1937 "pthread_cond_wait");
1939 /* Display progress update in log view. */
1940 show_log_view(view);
1948 static const struct got_error *
1949 log_scroll_down(struct tog_view *view, int maxscroll)
1951 struct tog_log_view_state *s = &view->state.log;
1952 const struct got_error *err = NULL;
1953 struct commit_queue_entry *pentry;
1954 int nscrolled = 0, ncommits_needed;
1956 if (s->last_displayed_entry == NULL)
1959 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1960 if (s->commits.ncommits < ncommits_needed &&
1961 !s->thread_args.log_complete) {
1963 * Ask the log thread for required amount of commits.
1965 s->thread_args.commits_needed += maxscroll;
1966 err = trigger_log_thread(view, 1);
1972 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1976 s->last_displayed_entry = pentry;
1978 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1981 s->first_displayed_entry = pentry;
1982 } while (++nscrolled < maxscroll);
1987 static const struct got_error *
1988 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1989 struct got_commit_object *commit, struct got_object_id *commit_id,
1990 struct tog_view *log_view, struct got_repository *repo)
1992 const struct got_error *err;
1993 struct got_object_qid *parent_id;
1994 struct tog_view *diff_view;
1996 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1997 if (diff_view == NULL)
1998 return got_error_from_errno("view_open");
2000 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2001 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2002 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2004 *new_view = diff_view;
2008 static const struct got_error *
2009 tree_view_visit_subtree(struct tog_tree_view_state *s,
2010 struct got_tree_object *subtree)
2012 struct tog_parent_tree *parent;
2014 parent = calloc(1, sizeof(*parent));
2016 return got_error_from_errno("calloc");
2018 parent->tree = s->tree;
2019 parent->first_displayed_entry = s->first_displayed_entry;
2020 parent->selected_entry = s->selected_entry;
2021 parent->selected = s->selected;
2022 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2025 s->first_displayed_entry = NULL;
2029 static const struct got_error *
2030 tree_view_walk_path(struct tog_tree_view_state *s,
2031 struct got_commit_object *commit, const char *path)
2033 const struct got_error *err = NULL;
2034 struct got_tree_object *tree = NULL;
2036 char *slash, *subpath = NULL;
2038 /* Walk the path and open corresponding tree objects. */
2041 struct got_tree_entry *te;
2042 struct got_object_id *tree_id;
2048 /* Ensure the correct subtree entry is selected. */
2049 slash = strchr(p, '/');
2051 te_name = strdup(p);
2053 te_name = strndup(p, slash - p);
2054 if (te_name == NULL) {
2055 err = got_error_from_errno("strndup");
2058 te = got_object_tree_find_entry(s->tree, te_name);
2060 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2065 s->first_displayed_entry = s->selected_entry = te;
2067 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2068 break; /* jump to this file's entry */
2070 slash = strchr(p, '/');
2072 subpath = strndup(path, slash - path);
2074 subpath = strdup(path);
2075 if (subpath == NULL) {
2076 err = got_error_from_errno("strdup");
2080 err = got_object_id_by_path(&tree_id, s->repo, commit,
2085 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2090 err = tree_view_visit_subtree(s, tree);
2092 got_object_tree_close(tree);
2106 static const struct got_error *
2107 browse_commit_tree(struct tog_view **new_view, int begin_x,
2108 struct commit_queue_entry *entry, const char *path,
2109 const char *head_ref_name, struct got_repository *repo)
2111 const struct got_error *err = NULL;
2112 struct tog_tree_view_state *s;
2113 struct tog_view *tree_view;
2115 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2116 if (tree_view == NULL)
2117 return got_error_from_errno("view_open");
2119 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2122 s = &tree_view->state.tree;
2124 *new_view = tree_view;
2126 if (got_path_is_root_dir(path))
2129 return tree_view_walk_path(s, entry->commit, path);
2132 static const struct got_error *
2133 block_signals_used_by_main_thread(void)
2138 if (sigemptyset(&sigset) == -1)
2139 return got_error_from_errno("sigemptyset");
2141 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2142 if (sigaddset(&sigset, SIGWINCH) == -1)
2143 return got_error_from_errno("sigaddset");
2144 if (sigaddset(&sigset, SIGCONT) == -1)
2145 return got_error_from_errno("sigaddset");
2146 if (sigaddset(&sigset, SIGINT) == -1)
2147 return got_error_from_errno("sigaddset");
2148 if (sigaddset(&sigset, SIGTERM) == -1)
2149 return got_error_from_errno("sigaddset");
2151 /* ncurses handles SIGTSTP */
2152 if (sigaddset(&sigset, SIGTSTP) == -1)
2153 return got_error_from_errno("sigaddset");
2155 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2157 return got_error_set_errno(errcode, "pthread_sigmask");
2163 log_thread(void *arg)
2165 const struct got_error *err = NULL;
2167 struct tog_log_thread_args *a = arg;
2170 err = block_signals_used_by_main_thread();
2174 while (!done && !err && !tog_fatal_signal_received()) {
2175 err = queue_commits(a);
2177 if (err->code != GOT_ERR_ITER_COMPLETED)
2181 } else if (a->commits_needed > 0 && !a->load_all)
2182 a->commits_needed--;
2184 errcode = pthread_mutex_lock(&tog_mutex);
2186 err = got_error_set_errno(errcode,
2187 "pthread_mutex_lock");
2189 } else if (*a->quit)
2191 else if (*a->first_displayed_entry == NULL) {
2192 *a->first_displayed_entry =
2193 TAILQ_FIRST(&a->commits->head);
2194 *a->selected_entry = *a->first_displayed_entry;
2197 errcode = pthread_cond_signal(&a->commit_loaded);
2199 err = got_error_set_errno(errcode,
2200 "pthread_cond_signal");
2201 pthread_mutex_unlock(&tog_mutex);
2206 a->commits_needed = 0;
2208 if (a->commits_needed == 0 && !a->load_all) {
2209 errcode = pthread_cond_wait(&a->need_commits,
2212 err = got_error_set_errno(errcode,
2213 "pthread_cond_wait");
2219 errcode = pthread_mutex_unlock(&tog_mutex);
2220 if (errcode && err == NULL)
2221 err = got_error_set_errno(errcode,
2222 "pthread_mutex_unlock");
2224 a->log_complete = 1;
2228 static const struct got_error *
2229 stop_log_thread(struct tog_log_view_state *s)
2231 const struct got_error *err = NULL;
2236 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2238 return got_error_set_errno(errcode,
2239 "pthread_cond_signal");
2240 errcode = pthread_mutex_unlock(&tog_mutex);
2242 return got_error_set_errno(errcode,
2243 "pthread_mutex_unlock");
2244 errcode = pthread_join(s->thread, (void **)&err);
2246 return got_error_set_errno(errcode, "pthread_join");
2247 errcode = pthread_mutex_lock(&tog_mutex);
2249 return got_error_set_errno(errcode,
2250 "pthread_mutex_lock");
2254 if (s->thread_args.repo) {
2255 err = got_repo_close(s->thread_args.repo);
2256 s->thread_args.repo = NULL;
2259 if (s->thread_args.pack_fds) {
2260 const struct got_error *pack_err =
2261 got_repo_pack_fds_close(s->thread_args.pack_fds);
2264 s->thread_args.pack_fds = NULL;
2267 if (s->thread_args.graph) {
2268 got_commit_graph_close(s->thread_args.graph);
2269 s->thread_args.graph = NULL;
2275 static const struct got_error *
2276 close_log_view(struct tog_view *view)
2278 const struct got_error *err = NULL;
2279 struct tog_log_view_state *s = &view->state.log;
2282 err = stop_log_thread(s);
2284 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2285 if (errcode && err == NULL)
2286 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2288 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2289 if (errcode && err == NULL)
2290 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2292 free_commits(&s->commits);
2293 free(s->in_repo_path);
2294 s->in_repo_path = NULL;
2297 free(s->head_ref_name);
2298 s->head_ref_name = NULL;
2302 static const struct got_error *
2303 search_start_log_view(struct tog_view *view)
2305 struct tog_log_view_state *s = &view->state.log;
2307 s->matched_entry = NULL;
2308 s->search_entry = NULL;
2312 static const struct got_error *
2313 search_next_log_view(struct tog_view *view)
2315 const struct got_error *err = NULL;
2316 struct tog_log_view_state *s = &view->state.log;
2317 struct commit_queue_entry *entry;
2319 /* Display progress update in log view. */
2320 show_log_view(view);
2324 if (s->search_entry) {
2326 errcode = pthread_mutex_unlock(&tog_mutex);
2328 return got_error_set_errno(errcode,
2329 "pthread_mutex_unlock");
2330 ch = wgetch(view->window);
2331 errcode = pthread_mutex_lock(&tog_mutex);
2333 return got_error_set_errno(errcode,
2334 "pthread_mutex_lock");
2335 if (ch == KEY_BACKSPACE) {
2336 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2339 if (view->searching == TOG_SEARCH_FORWARD)
2340 entry = TAILQ_NEXT(s->search_entry, entry);
2342 entry = TAILQ_PREV(s->search_entry,
2343 commit_queue_head, entry);
2344 } else if (s->matched_entry) {
2345 if (view->searching == TOG_SEARCH_FORWARD)
2346 entry = TAILQ_NEXT(s->matched_entry, entry);
2348 entry = TAILQ_PREV(s->matched_entry,
2349 commit_queue_head, entry);
2351 entry = s->selected_entry;
2357 if (entry == NULL) {
2358 if (s->thread_args.log_complete ||
2359 view->searching == TOG_SEARCH_BACKWARD) {
2360 view->search_next_done =
2361 (s->matched_entry == NULL ?
2362 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2363 s->search_entry = NULL;
2367 * Poke the log thread for more commits and return,
2368 * allowing the main loop to make progress. Search
2369 * will resume at s->search_entry once we come back.
2371 s->thread_args.commits_needed++;
2372 return trigger_log_thread(view, 0);
2375 err = match_commit(&have_match, entry->id, entry->commit,
2380 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2381 s->matched_entry = entry;
2385 s->search_entry = entry;
2386 if (view->searching == TOG_SEARCH_FORWARD)
2387 entry = TAILQ_NEXT(entry, entry);
2389 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2392 if (s->matched_entry) {
2393 int cur = s->selected_entry->idx;
2394 while (cur < s->matched_entry->idx) {
2395 err = input_log_view(NULL, view, KEY_DOWN);
2400 while (cur > s->matched_entry->idx) {
2401 err = input_log_view(NULL, view, KEY_UP);
2408 s->search_entry = NULL;
2413 static const struct got_error *
2414 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2415 struct got_repository *repo, const char *head_ref_name,
2416 const char *in_repo_path, int log_branches)
2418 const struct got_error *err = NULL;
2419 struct tog_log_view_state *s = &view->state.log;
2420 struct got_repository *thread_repo = NULL;
2421 struct got_commit_graph *thread_graph = NULL;
2424 if (in_repo_path != s->in_repo_path) {
2425 free(s->in_repo_path);
2426 s->in_repo_path = strdup(in_repo_path);
2427 if (s->in_repo_path == NULL)
2428 return got_error_from_errno("strdup");
2431 /* The commit queue only contains commits being displayed. */
2432 TAILQ_INIT(&s->commits.head);
2433 s->commits.ncommits = 0;
2436 if (head_ref_name) {
2437 s->head_ref_name = strdup(head_ref_name);
2438 if (s->head_ref_name == NULL) {
2439 err = got_error_from_errno("strdup");
2443 s->start_id = got_object_id_dup(start_id);
2444 if (s->start_id == NULL) {
2445 err = got_error_from_errno("got_object_id_dup");
2448 s->log_branches = log_branches;
2450 STAILQ_INIT(&s->colors);
2451 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2452 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2453 get_color_value("TOG_COLOR_COMMIT"));
2456 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2457 get_color_value("TOG_COLOR_AUTHOR"));
2459 free_colors(&s->colors);
2462 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2463 get_color_value("TOG_COLOR_DATE"));
2465 free_colors(&s->colors);
2470 view->show = show_log_view;
2471 view->input = input_log_view;
2472 view->close = close_log_view;
2473 view->search_start = search_start_log_view;
2474 view->search_next = search_next_log_view;
2476 if (s->thread_args.pack_fds == NULL) {
2477 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2481 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2482 s->thread_args.pack_fds);
2485 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2489 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2490 s->repo, NULL, NULL);
2494 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2496 err = got_error_set_errno(errcode, "pthread_cond_init");
2499 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2501 err = got_error_set_errno(errcode, "pthread_cond_init");
2505 s->thread_args.commits_needed = view->nlines;
2506 s->thread_args.graph = thread_graph;
2507 s->thread_args.commits = &s->commits;
2508 s->thread_args.in_repo_path = s->in_repo_path;
2509 s->thread_args.start_id = s->start_id;
2510 s->thread_args.repo = thread_repo;
2511 s->thread_args.log_complete = 0;
2512 s->thread_args.quit = &s->quit;
2513 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2514 s->thread_args.selected_entry = &s->selected_entry;
2515 s->thread_args.searching = &view->searching;
2516 s->thread_args.search_next_done = &view->search_next_done;
2517 s->thread_args.regex = &view->regex;
2520 close_log_view(view);
2524 static const struct got_error *
2525 show_log_view(struct tog_view *view)
2527 const struct got_error *err;
2528 struct tog_log_view_state *s = &view->state.log;
2530 if (s->thread == NULL) {
2531 int errcode = pthread_create(&s->thread, NULL, log_thread,
2534 return got_error_set_errno(errcode, "pthread_create");
2535 if (s->thread_args.commits_needed > 0) {
2536 err = trigger_log_thread(view, 1);
2542 return draw_commits(view);
2545 static const struct got_error *
2546 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2548 const struct got_error *err = NULL;
2549 struct tog_log_view_state *s = &view->state.log;
2550 struct tog_view *diff_view = NULL, *tree_view = NULL;
2551 struct tog_view *ref_view = NULL;
2552 struct commit_queue_entry *entry;
2553 int begin_x = 0, n, nscroll = view->nlines - 1;
2555 if (s->thread_args.load_all) {
2556 if (ch == KEY_BACKSPACE)
2557 s->thread_args.load_all = 0;
2558 else if (s->thread_args.log_complete) {
2559 s->thread_args.load_all = 0;
2560 log_scroll_down(view, s->commits.ncommits);
2561 s->selected = MIN(view->nlines - 2,
2562 s->commits.ncommits - 1);
2576 view->x = MAX(view->maxx - view->ncols / 2, 0);
2580 if (view->x + view->ncols / 2 < view->maxx)
2581 view->x += 2; /* move two columns right */
2585 view->x -= MIN(view->x, 2); /* move two columns back */
2592 if (s->first_displayed_entry == NULL)
2594 if (s->selected > 0)
2597 log_scroll_up(s, 1);
2603 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2612 if (s->first_displayed_entry == NULL)
2614 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2615 s->selected = MAX(0, s->selected - nscroll - 1);
2617 log_scroll_up(s, nscroll);
2625 if (s->first_displayed_entry == NULL)
2627 if (s->selected < MIN(view->nlines - 2,
2628 s->commits.ncommits - 1))
2631 err = log_scroll_down(view, 1);
2639 /* We don't know yet how many commits, so we're forced to
2640 * traverse them all. */
2641 if (!s->thread_args.log_complete) {
2642 s->thread_args.load_all = 1;
2643 return trigger_log_thread(view, 0);
2647 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2648 for (n = 0; n < view->nlines - 1; n++) {
2651 s->first_displayed_entry = entry;
2652 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2655 s->selected = n - 1;
2665 struct commit_queue_entry *first;
2666 first = s->first_displayed_entry;
2669 err = log_scroll_down(view, nscroll);
2672 if (first == s->first_displayed_entry &&
2673 s->selected < MIN(view->nlines - 2,
2674 s->commits.ncommits - 1)) {
2675 /* can't scroll further down */
2676 s->selected += MIN(s->last_displayed_entry->idx -
2677 s->selected_entry->idx, nscroll + 1);
2683 if (s->selected > view->nlines - 2)
2684 s->selected = view->nlines - 2;
2685 if (s->selected > s->commits.ncommits - 1)
2686 s->selected = s->commits.ncommits - 1;
2688 if (s->commits.ncommits < view->nlines - 1 &&
2689 !s->thread_args.log_complete) {
2690 s->thread_args.commits_needed += (view->nlines - 1) -
2691 s->commits.ncommits;
2692 err = trigger_log_thread(view, 1);
2698 if (s->selected_entry == NULL)
2700 if (view_is_parent_view(view))
2701 begin_x = view_split_begin_x(view->begin_x);
2702 err = open_diff_view_for_commit(&diff_view, begin_x,
2703 s->selected_entry->commit, s->selected_entry->id,
2708 diff_view->focussed = 1;
2709 if (view_is_parent_view(view)) {
2710 err = view_close_child(view);
2713 err = view_set_child(view, diff_view);
2716 view->focus_child = 1;
2718 *new_view = diff_view;
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 = browse_commit_tree(&tree_view, begin_x,
2726 s->selected_entry, s->in_repo_path, s->head_ref_name,
2731 tree_view->focussed = 1;
2732 if (view_is_parent_view(view)) {
2733 err = view_close_child(view);
2736 err = view_set_child(view, tree_view);
2739 view->focus_child = 1;
2741 *new_view = tree_view;
2746 if (ch == KEY_BACKSPACE &&
2747 got_path_is_root_dir(s->in_repo_path))
2749 err = stop_log_thread(s);
2752 if (ch == KEY_BACKSPACE) {
2754 err = got_path_dirname(&parent_path, s->in_repo_path);
2757 free(s->in_repo_path);
2758 s->in_repo_path = parent_path;
2759 s->thread_args.in_repo_path = s->in_repo_path;
2760 } else if (ch == CTRL('l')) {
2761 struct got_object_id *start_id;
2762 err = got_repo_match_object_id(&start_id, NULL,
2763 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2764 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2768 s->start_id = start_id;
2769 s->thread_args.start_id = s->start_id;
2771 s->log_branches = !s->log_branches;
2773 if (s->thread_args.pack_fds == NULL) {
2774 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2778 err = got_repo_open(&s->thread_args.repo,
2779 got_repo_get_path(s->repo), NULL,
2780 s->thread_args.pack_fds);
2784 err = tog_load_refs(s->repo, 0);
2787 err = got_commit_graph_open(&s->thread_args.graph,
2788 s->in_repo_path, !s->log_branches);
2791 err = got_commit_graph_iter_start(s->thread_args.graph,
2792 s->start_id, s->repo, NULL, NULL);
2795 free_commits(&s->commits);
2796 s->first_displayed_entry = NULL;
2797 s->last_displayed_entry = NULL;
2798 s->selected_entry = NULL;
2800 s->thread_args.log_complete = 0;
2802 s->thread_args.commits_needed = view->nlines;
2805 if (view_is_parent_view(view))
2806 begin_x = view_split_begin_x(view->begin_x);
2807 ref_view = view_open(view->nlines, view->ncols,
2808 view->begin_y, begin_x, TOG_VIEW_REF);
2809 if (ref_view == NULL)
2810 return got_error_from_errno("view_open");
2811 err = open_ref_view(ref_view, s->repo);
2813 view_close(ref_view);
2817 ref_view->focussed = 1;
2818 if (view_is_parent_view(view)) {
2819 err = view_close_child(view);
2822 err = view_set_child(view, ref_view);
2825 view->focus_child = 1;
2827 *new_view = ref_view;
2836 static const struct got_error *
2837 apply_unveil(const char *repo_path, const char *worktree_path)
2839 const struct got_error *error;
2842 if (unveil("gmon.out", "rwc") != 0)
2843 return got_error_from_errno2("unveil", "gmon.out");
2845 if (repo_path && unveil(repo_path, "r") != 0)
2846 return got_error_from_errno2("unveil", repo_path);
2848 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2849 return got_error_from_errno2("unveil", worktree_path);
2851 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2852 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2854 error = got_privsep_unveil_exec_helpers();
2858 if (unveil(NULL, NULL) != 0)
2859 return got_error_from_errno("unveil");
2868 * Override default signal handlers before starting ncurses.
2869 * This should prevent ncurses from installing its own
2870 * broken cleanup() signal handler.
2872 signal(SIGWINCH, tog_sigwinch);
2873 signal(SIGPIPE, tog_sigpipe);
2874 signal(SIGCONT, tog_sigcont);
2875 signal(SIGINT, tog_sigint);
2876 signal(SIGTERM, tog_sigterm);
2880 halfdelay(1); /* Do fast refresh while initial view is loading. */
2883 intrflush(stdscr, FALSE);
2884 keypad(stdscr, TRUE);
2886 if (getenv("TOG_COLORS") != NULL) {
2888 use_default_colors();
2892 static const struct got_error *
2893 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2894 struct got_repository *repo, struct got_worktree *worktree)
2896 const struct got_error *err = NULL;
2899 *in_repo_path = strdup("/");
2900 if (*in_repo_path == NULL)
2901 return got_error_from_errno("strdup");
2906 const char *prefix = got_worktree_get_path_prefix(worktree);
2909 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2912 if (asprintf(in_repo_path, "%s%s%s", prefix,
2913 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2915 err = got_error_from_errno("asprintf");
2916 *in_repo_path = NULL;
2920 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2925 static const struct got_error *
2926 cmd_log(int argc, char *argv[])
2928 const struct got_error *error;
2929 struct got_repository *repo = NULL;
2930 struct got_worktree *worktree = NULL;
2931 struct got_object_id *start_id = NULL;
2932 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2933 char *start_commit = NULL, *label = NULL;
2934 struct got_reference *ref = NULL;
2935 const char *head_ref_name = NULL;
2936 int ch, log_branches = 0;
2937 struct tog_view *view;
2938 int *pack_fds = NULL;
2940 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2946 start_commit = optarg;
2949 repo_path = realpath(optarg, NULL);
2950 if (repo_path == NULL)
2951 return got_error_from_errno2("realpath",
2966 error = got_repo_pack_fds_open(&pack_fds);
2970 if (repo_path == NULL) {
2971 cwd = getcwd(NULL, 0);
2973 return got_error_from_errno("getcwd");
2974 error = got_worktree_open(&worktree, cwd);
2975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2979 strdup(got_worktree_get_repo_path(worktree));
2981 repo_path = strdup(cwd);
2982 if (repo_path == NULL) {
2983 error = got_error_from_errno("strdup");
2988 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2992 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2999 error = apply_unveil(got_repo_get_path(repo),
3000 worktree ? got_worktree_get_root_path(worktree) : NULL);
3004 /* already loaded by tog_log_with_path()? */
3005 if (TAILQ_EMPTY(&tog_refs)) {
3006 error = tog_load_refs(repo, 0);
3011 if (start_commit == NULL) {
3012 error = got_repo_match_object_id(&start_id, &label,
3013 worktree ? got_worktree_get_head_ref_name(worktree) :
3014 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3017 head_ref_name = label;
3019 error = got_ref_open(&ref, repo, start_commit, 0);
3021 head_ref_name = got_ref_get_name(ref);
3022 else if (error->code != GOT_ERR_NOT_REF)
3024 error = got_repo_match_object_id(&start_id, NULL,
3025 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3030 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3032 error = got_error_from_errno("view_open");
3035 error = open_log_view(view, start_id, repo, head_ref_name,
3036 in_repo_path, log_branches);
3040 /* Release work tree lock. */
3041 got_worktree_close(worktree);
3044 error = view_loop(view);
3054 const struct got_error *close_err = got_repo_close(repo);
3059 got_worktree_close(worktree);
3061 const struct got_error *pack_err =
3062 got_repo_pack_fds_close(pack_fds);
3074 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3075 "[-w] object1 object2\n", getprogname());
3080 match_line(const char *line, regex_t *regex, size_t nmatch,
3081 regmatch_t *regmatch)
3083 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3087 match_color(struct tog_colors *colors, const char *line)
3089 struct tog_color *tc = NULL;
3091 STAILQ_FOREACH(tc, colors, entry) {
3092 if (match_line(line, &tc->regex, 0, NULL))
3099 static const struct got_error *
3100 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3101 WINDOW *window, int skipcol, regmatch_t *regmatch)
3103 const struct got_error *err = NULL;
3105 wchar_t *wline = NULL;
3106 int rme, rms, n, width, scrollx;
3107 int width0 = 0, width1 = 0, width2 = 0;
3108 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3112 rms = regmatch->rm_so;
3113 rme = regmatch->rm_eo;
3115 err = expand_tab(&exstr, line);
3119 /* Split the line into 3 segments, according to match offsets. */
3120 seg0 = strndup(exstr, rms);
3122 err = got_error_from_errno("strndup");
3125 seg1 = strndup(exstr + rms, rme - rms);
3127 err = got_error_from_errno("strndup");
3130 seg2 = strdup(exstr + rme);
3132 err = got_error_from_errno("strndup");
3136 /* draw up to matched token if we haven't scrolled past it */
3137 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3141 n = MAX(width0 - skipcol, 0);
3144 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3145 wlimit, col_tab_align, 1);
3148 waddwstr(window, &wline[scrollx]);
3158 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3162 wlen = wcslen(wline);
3164 width = wcwidth(wline[i]);
3166 /* should not happen, tabs are expanded */
3167 err = got_error(GOT_ERR_RANGE);
3170 if (width0 + w + width > skipcol)
3175 /* draw (visible part of) matched token (if scrolled into it) */
3176 if (width1 - w > 0) {
3177 wattron(window, A_STANDOUT);
3178 waddwstr(window, &wline[i]);
3179 wattroff(window, A_STANDOUT);
3180 wlimit -= (width1 - w);
3181 *wtotal += (width1 - w);
3185 if (wlimit > 0) { /* draw rest of line */
3187 if (skipcol > width0 + width1) {
3188 err = format_line(&wline, &width2, &scrollx, seg2,
3189 skipcol - (width0 + width1), wlimit,
3193 waddwstr(window, &wline[scrollx]);
3195 err = format_line(&wline, &width2, NULL, seg2, 0,
3196 wlimit, col_tab_align, 1);
3199 waddwstr(window, wline);
3212 static const struct got_error *
3213 draw_file(struct tog_view *view, const char *header)
3215 struct tog_diff_view_state *s = &view->state.diff;
3216 regmatch_t *regmatch = &view->regmatch;
3217 const struct got_error *err;
3220 size_t linesize = 0;
3222 struct tog_color *tc;
3225 int max_lines = view->nlines;
3226 int nlines = s->nlines;
3229 line_offset = s->line_offsets[s->first_displayed_line - 1];
3230 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3231 return got_error_from_errno("fseek");
3233 werase(view->window);
3236 if (asprintf(&line, "[%d/%d] %s",
3237 s->first_displayed_line - 1 + s->selected_line, nlines,
3239 return got_error_from_errno("asprintf");
3240 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3246 if (view_needs_focus_indication(view))
3247 wstandout(view->window);
3248 waddwstr(view->window, wline);
3251 if (view_needs_focus_indication(view))
3252 wstandend(view->window);
3253 if (width <= view->ncols - 1)
3254 waddch(view->window, '\n');
3264 while (max_lines > 0 && nprinted < max_lines) {
3265 linelen = getline(&line, &linesize, s->f);
3266 if (linelen == -1) {
3272 return got_ferror(s->f, GOT_ERR_IO);
3275 /* Set view->maxx based on full line length. */
3276 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3282 view->maxx = MAX(view->maxx, width);
3286 tc = match_color(&s->colors, line);
3288 wattr_on(view->window,
3289 COLOR_PAIR(tc->colorpair), NULL);
3290 if (s->first_displayed_line + nprinted == s->matched_line &&
3291 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3292 err = add_matched_line(&width, line, view->ncols, 0,
3293 view->window, view->x, regmatch);
3300 err = format_line(&wline, &width, &skip, line,
3301 view->x, view->ncols, 0, view->x ? 1 : 0);
3306 waddwstr(view->window, &wline[skip]);
3311 wattr_off(view->window,
3312 COLOR_PAIR(tc->colorpair), NULL);
3313 if (width <= view->ncols - 1)
3314 waddch(view->window, '\n');
3319 s->last_displayed_line = s->first_displayed_line +
3322 s->last_displayed_line = s->first_displayed_line;
3327 while (nprinted < view->nlines) {
3328 waddch(view->window, '\n');
3332 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3338 wstandout(view->window);
3339 waddwstr(view->window, wline);
3342 wstandend(view->window);
3349 get_datestr(time_t *time, char *datebuf)
3351 struct tm mytm, *tm;
3354 tm = gmtime_r(time, &mytm);
3357 s = asctime_r(tm, datebuf);
3360 p = strchr(s, '\n');
3366 static const struct got_error *
3367 get_changed_paths(struct got_pathlist_head *paths,
3368 struct got_commit_object *commit, struct got_repository *repo)
3370 const struct got_error *err = NULL;
3371 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3372 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3373 struct got_object_qid *qid;
3375 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3377 struct got_commit_object *pcommit;
3378 err = got_object_open_as_commit(&pcommit, repo,
3383 tree_id1 = got_object_id_dup(
3384 got_object_commit_get_tree_id(pcommit));
3385 if (tree_id1 == NULL) {
3386 got_object_commit_close(pcommit);
3387 return got_error_from_errno("got_object_id_dup");
3389 got_object_commit_close(pcommit);
3394 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3399 tree_id2 = got_object_commit_get_tree_id(commit);
3400 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3404 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3405 got_diff_tree_collect_changed_paths, paths, 0);
3408 got_object_tree_close(tree1);
3410 got_object_tree_close(tree2);
3415 static const struct got_error *
3416 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3420 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3422 return got_error_from_errno("reallocarray");
3424 (*line_offsets)[*nlines] = off;
3429 static const struct got_error *
3430 write_commit_info(off_t **line_offsets, size_t *nlines,
3431 struct got_object_id *commit_id, struct got_reflist_head *refs,
3432 struct got_repository *repo, FILE *outfile)
3434 const struct got_error *err = NULL;
3435 char datebuf[26], *datestr;
3436 struct got_commit_object *commit;
3437 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3438 time_t committer_time;
3439 const char *author, *committer;
3440 char *refs_str = NULL;
3441 struct got_pathlist_head changed_paths;
3442 struct got_pathlist_entry *pe;
3446 TAILQ_INIT(&changed_paths);
3449 err = build_refs_str(&refs_str, refs, commit_id, repo);
3454 err = got_object_open_as_commit(&commit, repo, commit_id);
3458 err = got_object_id_str(&id_str, commit_id);
3460 err = got_error_from_errno("got_object_id_str");
3464 err = add_line_offset(line_offsets, nlines, 0);
3468 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3469 refs_str ? refs_str : "", refs_str ? ")" : "");
3471 err = got_error_from_errno("fprintf");
3475 err = add_line_offset(line_offsets, nlines, outoff);
3479 n = fprintf(outfile, "from: %s\n",
3480 got_object_commit_get_author(commit));
3482 err = got_error_from_errno("fprintf");
3486 err = add_line_offset(line_offsets, nlines, outoff);
3490 committer_time = got_object_commit_get_committer_time(commit);
3491 datestr = get_datestr(&committer_time, datebuf);
3493 n = fprintf(outfile, "date: %s UTC\n", datestr);
3495 err = got_error_from_errno("fprintf");
3499 err = add_line_offset(line_offsets, nlines, outoff);
3503 author = got_object_commit_get_author(commit);
3504 committer = got_object_commit_get_committer(commit);
3505 if (strcmp(author, committer) != 0) {
3506 n = fprintf(outfile, "via: %s\n", committer);
3508 err = got_error_from_errno("fprintf");
3512 err = add_line_offset(line_offsets, nlines, outoff);
3516 if (got_object_commit_get_nparents(commit) > 1) {
3517 const struct got_object_id_queue *parent_ids;
3518 struct got_object_qid *qid;
3520 parent_ids = got_object_commit_get_parent_ids(commit);
3521 STAILQ_FOREACH(qid, parent_ids, entry) {
3522 err = got_object_id_str(&id_str, &qid->id);
3525 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3527 err = got_error_from_errno("fprintf");
3531 err = add_line_offset(line_offsets, nlines, outoff);
3539 err = got_object_commit_get_logmsg(&logmsg, commit);
3543 while ((line = strsep(&s, "\n")) != NULL) {
3544 n = fprintf(outfile, "%s\n", line);
3546 err = got_error_from_errno("fprintf");
3550 err = add_line_offset(line_offsets, nlines, outoff);
3555 err = get_changed_paths(&changed_paths, commit, repo);
3558 TAILQ_FOREACH(pe, &changed_paths, entry) {
3559 struct got_diff_changed_path *cp = pe->data;
3560 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3562 err = got_error_from_errno("fprintf");
3566 err = add_line_offset(line_offsets, nlines, outoff);
3569 free((char *)pe->path);
3573 fputc('\n', outfile);
3575 err = add_line_offset(line_offsets, nlines, outoff);
3577 got_pathlist_free(&changed_paths);
3581 got_object_commit_close(commit);
3583 free(*line_offsets);
3584 *line_offsets = NULL;
3590 static const struct got_error *
3591 create_diff(struct tog_diff_view_state *s)
3593 const struct got_error *err = NULL;
3597 free(s->line_offsets);
3598 s->line_offsets = malloc(sizeof(off_t));
3599 if (s->line_offsets == NULL)
3600 return got_error_from_errno("malloc");
3605 err = got_error_from_errno("got_opentemp");
3608 if (s->f && fclose(s->f) == EOF) {
3609 err = got_error_from_errno("fclose");
3615 err = got_object_get_type(&obj_type, s->repo, s->id1);
3617 err = got_object_get_type(&obj_type, s->repo, s->id2);
3622 case GOT_OBJ_TYPE_BLOB:
3623 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3624 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3625 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3628 case GOT_OBJ_TYPE_TREE:
3629 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3630 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3631 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3633 case GOT_OBJ_TYPE_COMMIT: {
3634 const struct got_object_id_queue *parent_ids;
3635 struct got_object_qid *pid;
3636 struct got_commit_object *commit2;
3637 struct got_reflist_head *refs;
3639 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3642 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3643 /* Show commit info if we're diffing to a parent/root commit. */
3644 if (s->id1 == NULL) {
3645 err = write_commit_info(&s->line_offsets, &s->nlines,
3646 s->id2, refs, s->repo, s->f);
3650 parent_ids = got_object_commit_get_parent_ids(commit2);
3651 STAILQ_FOREACH(pid, parent_ids, entry) {
3652 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3653 err = write_commit_info(
3654 &s->line_offsets, &s->nlines,
3655 s->id2, refs, s->repo, s->f);
3662 got_object_commit_close(commit2);
3664 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3665 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3666 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3670 err = got_error(GOT_ERR_OBJ_TYPE);
3676 if (s->f && fflush(s->f) != 0 && err == NULL)
3677 err = got_error_from_errno("fflush");
3682 diff_view_indicate_progress(struct tog_view *view)
3684 mvwaddstr(view->window, 0, 0, "diffing...");
3689 static const struct got_error *
3690 search_start_diff_view(struct tog_view *view)
3692 struct tog_diff_view_state *s = &view->state.diff;
3694 s->matched_line = 0;
3698 static const struct got_error *
3699 search_next_diff_view(struct tog_view *view)
3701 struct tog_diff_view_state *s = &view->state.diff;
3702 const struct got_error *err = NULL;
3705 size_t linesize = 0;
3708 if (!view->searching) {
3709 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3713 if (s->matched_line) {
3714 if (view->searching == TOG_SEARCH_FORWARD)
3715 lineno = s->matched_line + 1;
3717 lineno = s->matched_line - 1;
3719 lineno = s->first_displayed_line;
3724 if (lineno <= 0 || lineno > s->nlines) {
3725 if (s->matched_line == 0) {
3726 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3730 if (view->searching == TOG_SEARCH_FORWARD)
3736 offset = s->line_offsets[lineno - 1];
3737 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3739 return got_error_from_errno("fseeko");
3741 linelen = getline(&line, &linesize, s->f);
3742 if (linelen != -1) {
3744 err = expand_tab(&exstr, line);
3747 if (match_line(exstr, &view->regex, 1,
3749 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3750 s->matched_line = lineno;
3756 if (view->searching == TOG_SEARCH_FORWARD)
3763 if (s->matched_line) {
3764 s->first_displayed_line = s->matched_line;
3765 s->selected_line = 1;
3771 static const struct got_error *
3772 close_diff_view(struct tog_view *view)
3774 const struct got_error *err = NULL;
3775 struct tog_diff_view_state *s = &view->state.diff;
3781 if (s->f && fclose(s->f) == EOF)
3782 err = got_error_from_errno("fclose");
3784 if (s->f1 && fclose(s->f1) == EOF)
3785 err = got_error_from_errno("fclose");
3787 if (s->f2 && fclose(s->f2) == EOF)
3788 err = got_error_from_errno("fclose");
3790 free_colors(&s->colors);
3791 free(s->line_offsets);
3792 s->line_offsets = NULL;
3797 static const struct got_error *
3798 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3799 struct got_object_id *id2, const char *label1, const char *label2,
3800 int diff_context, int ignore_whitespace, int force_text_diff,
3801 struct tog_view *log_view, struct got_repository *repo)
3803 const struct got_error *err;
3804 struct tog_diff_view_state *s = &view->state.diff;
3806 memset(s, 0, sizeof(*s));
3808 if (id1 != NULL && id2 != NULL) {
3810 err = got_object_get_type(&type1, repo, id1);
3813 err = got_object_get_type(&type2, repo, id2);
3818 return got_error(GOT_ERR_OBJ_TYPE);
3820 s->first_displayed_line = 1;
3821 s->last_displayed_line = view->nlines;
3822 s->selected_line = 1;
3830 s->id1 = got_object_id_dup(id1);
3832 return got_error_from_errno("got_object_id_dup");
3833 s->f1 = got_opentemp();
3834 if (s->f1 == NULL) {
3835 err = got_error_from_errno("got_opentemp");
3841 s->id2 = got_object_id_dup(id2);
3842 if (s->id2 == NULL) {
3843 err = got_error_from_errno("got_object_id_dup");
3847 s->f2 = got_opentemp();
3848 if (s->f2 == NULL) {
3849 err = got_error_from_errno("got_opentemp");
3853 s->first_displayed_line = 1;
3854 s->last_displayed_line = view->nlines;
3855 s->diff_context = diff_context;
3856 s->ignore_whitespace = ignore_whitespace;
3857 s->force_text_diff = force_text_diff;
3858 s->log_view = log_view;
3861 STAILQ_INIT(&s->colors);
3862 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3863 err = add_color(&s->colors,
3864 "^-", TOG_COLOR_DIFF_MINUS,
3865 get_color_value("TOG_COLOR_DIFF_MINUS"));
3868 err = add_color(&s->colors, "^\\+",
3869 TOG_COLOR_DIFF_PLUS,
3870 get_color_value("TOG_COLOR_DIFF_PLUS"));
3873 err = add_color(&s->colors,
3874 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3875 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3879 err = add_color(&s->colors,
3880 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3881 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3882 get_color_value("TOG_COLOR_DIFF_META"));
3886 err = add_color(&s->colors,
3887 "^(from|via): ", TOG_COLOR_AUTHOR,
3888 get_color_value("TOG_COLOR_AUTHOR"));
3892 err = add_color(&s->colors,
3893 "^date: ", TOG_COLOR_DATE,
3894 get_color_value("TOG_COLOR_DATE"));
3899 if (log_view && view_is_splitscreen(view))
3900 show_log_view(log_view); /* draw vborder */
3901 diff_view_indicate_progress(view);
3903 err = create_diff(s);
3905 view->show = show_diff_view;
3906 view->input = input_diff_view;
3907 view->close = close_diff_view;
3908 view->search_start = search_start_diff_view;
3909 view->search_next = search_next_diff_view;
3912 close_diff_view(view);
3916 static const struct got_error *
3917 show_diff_view(struct tog_view *view)
3919 const struct got_error *err;
3920 struct tog_diff_view_state *s = &view->state.diff;
3921 char *id_str1 = NULL, *id_str2, *header;
3922 const char *label1, *label2;
3925 err = got_object_id_str(&id_str1, s->id1);
3928 label1 = s->label1 ? : id_str1;
3930 label1 = "/dev/null";
3932 err = got_object_id_str(&id_str2, s->id2);
3935 label2 = s->label2 ? : id_str2;
3937 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3938 err = got_error_from_errno("asprintf");
3946 err = draw_file(view, header);
3951 static const struct got_error *
3952 set_selected_commit(struct tog_diff_view_state *s,
3953 struct commit_queue_entry *entry)
3955 const struct got_error *err;
3956 const struct got_object_id_queue *parent_ids;
3957 struct got_commit_object *selected_commit;
3958 struct got_object_qid *pid;
3961 s->id2 = got_object_id_dup(entry->id);
3963 return got_error_from_errno("got_object_id_dup");
3965 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3968 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3970 pid = STAILQ_FIRST(parent_ids);
3971 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3972 got_object_commit_close(selected_commit);
3976 static const struct got_error *
3977 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3979 const struct got_error *err = NULL;
3980 struct tog_diff_view_state *s = &view->state.diff;
3981 struct tog_log_view_state *ls;
3982 struct commit_queue_entry *old_selected_entry;
3984 size_t linesize = 0;
3986 int i, nscroll = view->nlines - 1;
3993 view->x = MAX(view->maxx - view->ncols / 3, 0);
3997 if (view->x + view->ncols / 3 < view->maxx)
3998 view->x += 2; /* move two columns right */
4002 view->x -= MIN(view->x, 2); /* move two columns back */
4007 s->force_text_diff = !s->force_text_diff;
4009 s->ignore_whitespace = !s->ignore_whitespace;
4010 wclear(view->window);
4011 s->first_displayed_line = 1;
4012 s->last_displayed_line = view->nlines;
4013 s->matched_line = 0;
4014 diff_view_indicate_progress(view);
4015 err = create_diff(s);
4019 s->first_displayed_line = 1;
4026 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4032 if (s->first_displayed_line > 1)
4033 s->first_displayed_line--;
4041 if (s->first_displayed_line == 1)
4044 while (i++ < nscroll && s->first_displayed_line > 1)
4045 s->first_displayed_line--;
4051 s->first_displayed_line++;
4063 while (!s->eof && i++ < nscroll) {
4064 linelen = getline(&line, &linesize, s->f);
4065 s->first_displayed_line++;
4066 if (linelen == -1) {
4070 err = got_ferror(s->f, GOT_ERR_IO);
4077 if (s->diff_context > 0) {
4079 s->matched_line = 0;
4080 diff_view_indicate_progress(view);
4081 err = create_diff(s);
4082 if (s->first_displayed_line + view->nlines - 1 >
4084 s->first_displayed_line = 1;
4085 s->last_displayed_line = view->nlines;
4090 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4092 s->matched_line = 0;
4093 diff_view_indicate_progress(view);
4094 err = create_diff(s);
4099 if (s->log_view == NULL)
4101 ls = &s->log_view->state.log;
4102 old_selected_entry = ls->selected_entry;
4104 err = input_log_view(NULL, s->log_view, KEY_UP);
4108 if (old_selected_entry == ls->selected_entry)
4111 err = set_selected_commit(s, ls->selected_entry);
4115 s->first_displayed_line = 1;
4116 s->last_displayed_line = view->nlines;
4117 s->matched_line = 0;
4120 diff_view_indicate_progress(view);
4121 err = create_diff(s);
4125 if (s->log_view == NULL)
4127 ls = &s->log_view->state.log;
4128 old_selected_entry = ls->selected_entry;
4130 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4134 if (old_selected_entry == ls->selected_entry)
4137 err = set_selected_commit(s, ls->selected_entry);
4141 s->first_displayed_line = 1;
4142 s->last_displayed_line = view->nlines;
4143 s->matched_line = 0;
4146 diff_view_indicate_progress(view);
4147 err = create_diff(s);
4156 static const struct got_error *
4157 cmd_diff(int argc, char *argv[])
4159 const struct got_error *error = NULL;
4160 struct got_repository *repo = NULL;
4161 struct got_worktree *worktree = NULL;
4162 struct got_object_id *id1 = NULL, *id2 = NULL;
4163 char *repo_path = NULL, *cwd = NULL;
4164 char *id_str1 = NULL, *id_str2 = NULL;
4165 char *label1 = NULL, *label2 = NULL;
4166 int diff_context = 3, ignore_whitespace = 0;
4167 int ch, force_text_diff = 0;
4169 struct tog_view *view;
4170 int *pack_fds = NULL;
4172 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4175 force_text_diff = 1;
4178 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4181 errx(1, "number of context lines is %s: %s",
4185 repo_path = realpath(optarg, NULL);
4186 if (repo_path == NULL)
4187 return got_error_from_errno2("realpath",
4189 got_path_strip_trailing_slashes(repo_path);
4192 ignore_whitespace = 1;
4204 usage_diff(); /* TODO show local worktree changes */
4205 } else if (argc == 2) {
4211 error = got_repo_pack_fds_open(&pack_fds);
4215 if (repo_path == NULL) {
4216 cwd = getcwd(NULL, 0);
4218 return got_error_from_errno("getcwd");
4219 error = got_worktree_open(&worktree, cwd);
4220 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4224 strdup(got_worktree_get_repo_path(worktree));
4226 repo_path = strdup(cwd);
4227 if (repo_path == NULL) {
4228 error = got_error_from_errno("strdup");
4233 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4239 error = apply_unveil(got_repo_get_path(repo), NULL);
4243 error = tog_load_refs(repo, 0);
4247 error = got_repo_match_object_id(&id1, &label1, id_str1,
4248 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4252 error = got_repo_match_object_id(&id2, &label2, id_str2,
4253 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4257 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4259 error = got_error_from_errno("view_open");
4262 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4263 ignore_whitespace, force_text_diff, NULL, repo);
4266 error = view_loop(view);
4273 const struct got_error *close_err = got_repo_close(repo);
4278 got_worktree_close(worktree);
4280 const struct got_error *pack_err =
4281 got_repo_pack_fds_close(pack_fds);
4294 "usage: %s blame [-c commit] [-r repository-path] path\n",
4299 struct tog_blame_line {
4301 struct got_object_id *id;
4304 static const struct got_error *
4305 draw_blame(struct tog_view *view)
4307 struct tog_blame_view_state *s = &view->state.blame;
4308 struct tog_blame *blame = &s->blame;
4309 regmatch_t *regmatch = &view->regmatch;
4310 const struct got_error *err;
4311 int lineno = 0, nprinted = 0;
4313 size_t linesize = 0;
4317 struct tog_blame_line *blame_line;
4318 struct got_object_id *prev_id = NULL;
4320 struct tog_color *tc;
4322 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4327 werase(view->window);
4329 if (asprintf(&line, "commit %s", id_str) == -1) {
4330 err = got_error_from_errno("asprintf");
4335 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4340 if (view_needs_focus_indication(view))
4341 wstandout(view->window);
4342 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4344 wattr_on(view->window,
4345 COLOR_PAIR(tc->colorpair), NULL);
4346 waddwstr(view->window, wline);
4348 wattr_off(view->window,
4349 COLOR_PAIR(tc->colorpair), NULL);
4350 if (view_needs_focus_indication(view))
4351 wstandend(view->window);
4354 if (width < view->ncols - 1)
4355 waddch(view->window, '\n');
4357 if (asprintf(&line, "[%d/%d] %s%s",
4358 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4359 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4361 return got_error_from_errno("asprintf");
4364 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4369 waddwstr(view->window, wline);
4372 if (width < view->ncols - 1)
4373 waddch(view->window, '\n');
4377 while (nprinted < view->nlines - 2) {
4378 linelen = getline(&line, &linesize, blame->f);
4379 if (linelen == -1) {
4380 if (feof(blame->f)) {
4385 return got_ferror(blame->f, GOT_ERR_IO);
4387 if (++lineno < s->first_displayed_line)
4390 /* Set view->maxx based on full line length. */
4391 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4398 view->maxx = MAX(view->maxx, width);
4400 if (view->focussed && nprinted == s->selected_line - 1)
4401 wstandout(view->window);
4403 if (blame->nlines > 0) {
4404 blame_line = &blame->lines[lineno - 1];
4405 if (blame_line->annotated && prev_id &&
4406 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4408 nprinted == s->selected_line - 1)) {
4409 waddstr(view->window, " ");
4410 } else if (blame_line->annotated) {
4412 err = got_object_id_str(&id_str,
4418 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4420 wattr_on(view->window,
4421 COLOR_PAIR(tc->colorpair), NULL);
4422 wprintw(view->window, "%.8s", id_str);
4424 wattr_off(view->window,
4425 COLOR_PAIR(tc->colorpair), NULL);
4427 prev_id = blame_line->id;
4429 waddstr(view->window, "........");
4433 waddstr(view->window, "........");
4437 if (view->focussed && nprinted == s->selected_line - 1)
4438 wstandend(view->window);
4439 waddstr(view->window, " ");
4441 if (view->ncols <= 9) {
4443 } else if (s->first_displayed_line + nprinted ==
4445 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4446 err = add_matched_line(&width, line, view->ncols - 9, 9,
4447 view->window, view->x, regmatch);
4455 err = format_line(&wline, &width, &skip, line,
4456 view->x, view->ncols - 9, 9, 1);
4461 waddwstr(view->window, &wline[skip]);
4467 if (width <= view->ncols - 1)
4468 waddch(view->window, '\n');
4469 if (++nprinted == 1)
4470 s->first_displayed_line = lineno;
4473 s->last_displayed_line = lineno;
4480 static const struct got_error *
4481 blame_cb(void *arg, int nlines, int lineno,
4482 struct got_commit_object *commit, struct got_object_id *id)
4484 const struct got_error *err = NULL;
4485 struct tog_blame_cb_args *a = arg;
4486 struct tog_blame_line *line;
4489 if (nlines != a->nlines ||
4490 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4491 return got_error(GOT_ERR_RANGE);
4493 errcode = pthread_mutex_lock(&tog_mutex);
4495 return got_error_set_errno(errcode, "pthread_mutex_lock");
4497 if (*a->quit) { /* user has quit the blame view */
4498 err = got_error(GOT_ERR_ITER_COMPLETED);
4503 goto done; /* no change in this commit */
4505 line = &a->lines[lineno - 1];
4506 if (line->annotated)
4509 line->id = got_object_id_dup(id);
4510 if (line->id == NULL) {
4511 err = got_error_from_errno("got_object_id_dup");
4514 line->annotated = 1;
4516 errcode = pthread_mutex_unlock(&tog_mutex);
4518 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4523 blame_thread(void *arg)
4525 const struct got_error *err, *close_err;
4526 struct tog_blame_thread_args *ta = arg;
4527 struct tog_blame_cb_args *a = ta->cb_args;
4530 err = block_signals_used_by_main_thread();
4534 err = got_blame(ta->path, a->commit_id, ta->repo,
4535 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4536 if (err && err->code == GOT_ERR_CANCELLED)
4539 errcode = pthread_mutex_lock(&tog_mutex);
4541 return (void *)got_error_set_errno(errcode,
4542 "pthread_mutex_lock");
4544 close_err = got_repo_close(ta->repo);
4550 errcode = pthread_mutex_unlock(&tog_mutex);
4551 if (errcode && err == NULL)
4552 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4557 static struct got_object_id *
4558 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4559 int first_displayed_line, int selected_line)
4561 struct tog_blame_line *line;
4566 line = &lines[first_displayed_line - 1 + selected_line - 1];
4567 if (!line->annotated)
4573 static const struct got_error *
4574 stop_blame(struct tog_blame *blame)
4576 const struct got_error *err = NULL;
4579 if (blame->thread) {
4581 errcode = pthread_mutex_unlock(&tog_mutex);
4583 return got_error_set_errno(errcode,
4584 "pthread_mutex_unlock");
4585 errcode = pthread_join(blame->thread, (void **)&err);
4587 return got_error_set_errno(errcode, "pthread_join");
4588 errcode = pthread_mutex_lock(&tog_mutex);
4590 return got_error_set_errno(errcode,
4591 "pthread_mutex_lock");
4592 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4594 blame->thread = NULL;
4596 if (blame->thread_args.repo) {
4597 const struct got_error *close_err;
4598 close_err = got_repo_close(blame->thread_args.repo);
4601 blame->thread_args.repo = NULL;
4604 if (fclose(blame->f) == EOF && err == NULL)
4605 err = got_error_from_errno("fclose");
4609 for (i = 0; i < blame->nlines; i++)
4610 free(blame->lines[i].id);
4612 blame->lines = NULL;
4614 free(blame->cb_args.commit_id);
4615 blame->cb_args.commit_id = NULL;
4616 if (blame->pack_fds) {
4617 const struct got_error *pack_err =
4618 got_repo_pack_fds_close(blame->pack_fds);
4621 blame->pack_fds = NULL;
4626 static const struct got_error *
4627 cancel_blame_view(void *arg)
4629 const struct got_error *err = NULL;
4633 errcode = pthread_mutex_lock(&tog_mutex);
4635 return got_error_set_errno(errcode,
4636 "pthread_mutex_unlock");
4639 err = got_error(GOT_ERR_CANCELLED);
4641 errcode = pthread_mutex_unlock(&tog_mutex);
4643 return got_error_set_errno(errcode,
4644 "pthread_mutex_lock");
4649 static const struct got_error *
4650 run_blame(struct tog_view *view)
4652 struct tog_blame_view_state *s = &view->state.blame;
4653 struct tog_blame *blame = &s->blame;
4654 const struct got_error *err = NULL;
4655 struct got_commit_object *commit = NULL;
4656 struct got_blob_object *blob = NULL;
4657 struct got_repository *thread_repo = NULL;
4658 struct got_object_id *obj_id = NULL;
4660 int *pack_fds = NULL;
4662 err = got_object_open_as_commit(&commit, s->repo,
4663 &s->blamed_commit->id);
4667 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4671 err = got_object_get_type(&obj_type, s->repo, obj_id);
4675 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4676 err = got_error(GOT_ERR_OBJ_TYPE);
4680 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4683 blame->f = got_opentemp();
4684 if (blame->f == NULL) {
4685 err = got_error_from_errno("got_opentemp");
4688 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4689 &blame->line_offsets, blame->f, blob);
4692 if (blame->nlines == 0) {
4693 s->blame_complete = 1;
4697 /* Don't include \n at EOF in the blame line count. */
4698 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4701 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4702 if (blame->lines == NULL) {
4703 err = got_error_from_errno("calloc");
4707 err = got_repo_pack_fds_open(&pack_fds);
4710 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4715 blame->pack_fds = pack_fds;
4716 blame->cb_args.view = view;
4717 blame->cb_args.lines = blame->lines;
4718 blame->cb_args.nlines = blame->nlines;
4719 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4720 if (blame->cb_args.commit_id == NULL) {
4721 err = got_error_from_errno("got_object_id_dup");
4724 blame->cb_args.quit = &s->done;
4726 blame->thread_args.path = s->path;
4727 blame->thread_args.repo = thread_repo;
4728 blame->thread_args.cb_args = &blame->cb_args;
4729 blame->thread_args.complete = &s->blame_complete;
4730 blame->thread_args.cancel_cb = cancel_blame_view;
4731 blame->thread_args.cancel_arg = &s->done;
4732 s->blame_complete = 0;
4734 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4735 s->first_displayed_line = 1;
4736 s->last_displayed_line = view->nlines;
4737 s->selected_line = 1;
4739 s->matched_line = 0;
4743 got_object_commit_close(commit);
4745 got_object_blob_close(blob);
4752 static const struct got_error *
4753 open_blame_view(struct tog_view *view, char *path,
4754 struct got_object_id *commit_id, struct got_repository *repo)
4756 const struct got_error *err = NULL;
4757 struct tog_blame_view_state *s = &view->state.blame;
4759 STAILQ_INIT(&s->blamed_commits);
4761 s->path = strdup(path);
4762 if (s->path == NULL)
4763 return got_error_from_errno("strdup");
4765 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4771 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4772 s->first_displayed_line = 1;
4773 s->last_displayed_line = view->nlines;
4774 s->selected_line = 1;
4775 s->blame_complete = 0;
4777 s->commit_id = commit_id;
4778 memset(&s->blame, 0, sizeof(s->blame));
4780 STAILQ_INIT(&s->colors);
4781 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4782 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4783 get_color_value("TOG_COLOR_COMMIT"));
4788 view->show = show_blame_view;
4789 view->input = input_blame_view;
4790 view->close = close_blame_view;
4791 view->search_start = search_start_blame_view;
4792 view->search_next = search_next_blame_view;
4794 return run_blame(view);
4797 static const struct got_error *
4798 close_blame_view(struct tog_view *view)
4800 const struct got_error *err = NULL;
4801 struct tog_blame_view_state *s = &view->state.blame;
4803 if (s->blame.thread)
4804 err = stop_blame(&s->blame);
4806 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4807 struct got_object_qid *blamed_commit;
4808 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4809 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4810 got_object_qid_free(blamed_commit);
4814 free_colors(&s->colors);
4818 static const struct got_error *
4819 search_start_blame_view(struct tog_view *view)
4821 struct tog_blame_view_state *s = &view->state.blame;
4823 s->matched_line = 0;
4827 static const struct got_error *
4828 search_next_blame_view(struct tog_view *view)
4830 struct tog_blame_view_state *s = &view->state.blame;
4831 const struct got_error *err = NULL;
4834 size_t linesize = 0;
4837 if (!view->searching) {
4838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4842 if (s->matched_line) {
4843 if (view->searching == TOG_SEARCH_FORWARD)
4844 lineno = s->matched_line + 1;
4846 lineno = s->matched_line - 1;
4848 lineno = s->first_displayed_line - 1 + s->selected_line;
4853 if (lineno <= 0 || lineno > s->blame.nlines) {
4854 if (s->matched_line == 0) {
4855 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4859 if (view->searching == TOG_SEARCH_FORWARD)
4862 lineno = s->blame.nlines;
4865 offset = s->blame.line_offsets[lineno - 1];
4866 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4868 return got_error_from_errno("fseeko");
4870 linelen = getline(&line, &linesize, s->blame.f);
4871 if (linelen != -1) {
4873 err = expand_tab(&exstr, line);
4876 if (match_line(exstr, &view->regex, 1,
4878 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4879 s->matched_line = lineno;
4885 if (view->searching == TOG_SEARCH_FORWARD)
4892 if (s->matched_line) {
4893 s->first_displayed_line = s->matched_line;
4894 s->selected_line = 1;
4900 static const struct got_error *
4901 show_blame_view(struct tog_view *view)
4903 const struct got_error *err = NULL;
4904 struct tog_blame_view_state *s = &view->state.blame;
4907 if (s->blame.thread == NULL && !s->blame_complete) {
4908 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4909 &s->blame.thread_args);
4911 return got_error_set_errno(errcode, "pthread_create");
4913 halfdelay(1); /* fast refresh while annotating */
4916 if (s->blame_complete)
4917 halfdelay(10); /* disable fast refresh */
4919 err = draw_blame(view);
4925 static const struct got_error *
4926 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4928 const struct got_error *err = NULL, *thread_err = NULL;
4929 struct tog_view *diff_view;
4930 struct tog_blame_view_state *s = &view->state.blame;
4931 int begin_x = 0, nscroll = view->nlines - 2;
4938 view->x = MAX(view->maxx - view->ncols / 3, 0);
4942 if (view->x + view->ncols / 3 < view->maxx)
4943 view->x += 2; /* move two columns right */
4947 view->x -= MIN(view->x, 2); /* move two columns back */
4954 s->selected_line = 1;
4955 s->first_displayed_line = 1;
4959 if (s->blame.nlines < view->nlines - 2) {
4960 s->selected_line = s->blame.nlines;
4961 s->first_displayed_line = 1;
4963 s->selected_line = view->nlines - 2;
4964 s->first_displayed_line = s->blame.nlines -
4971 if (s->selected_line > 1)
4973 else if (s->selected_line == 1 &&
4974 s->first_displayed_line > 1)
4975 s->first_displayed_line--;
4983 if (s->first_displayed_line == 1) {
4984 s->selected_line = MAX(1, s->selected_line - nscroll);
4987 if (s->first_displayed_line > nscroll)
4988 s->first_displayed_line -= nscroll;
4990 s->first_displayed_line = 1;
4995 if (s->selected_line < view->nlines - 2 &&
4996 s->first_displayed_line +
4997 s->selected_line <= s->blame.nlines)
4999 else if (s->last_displayed_line <
5001 s->first_displayed_line++;
5005 struct got_object_id *id = NULL;
5006 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5007 s->first_displayed_line, s->selected_line);
5011 struct got_commit_object *commit, *pcommit;
5012 struct got_object_qid *pid;
5013 struct got_object_id *blob_id = NULL;
5015 err = got_object_open_as_commit(&commit,
5020 got_object_commit_get_parent_ids(commit));
5022 got_object_commit_close(commit);
5025 /* Check if path history ends here. */
5026 err = got_object_open_as_commit(&pcommit,
5030 err = got_object_id_by_path(&blob_id, s->repo,
5032 got_object_commit_close(pcommit);
5034 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5036 got_object_commit_close(commit);
5039 err = got_object_get_type(&obj_type, s->repo,
5042 /* Can't blame non-blob type objects. */
5043 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5044 got_object_commit_close(commit);
5047 err = got_object_qid_alloc(&s->blamed_commit,
5049 got_object_commit_close(commit);
5051 if (got_object_id_cmp(id,
5052 &s->blamed_commit->id) == 0)
5054 err = got_object_qid_alloc(&s->blamed_commit,
5060 thread_err = stop_blame(&s->blame);
5064 STAILQ_INSERT_HEAD(&s->blamed_commits,
5065 s->blamed_commit, entry);
5066 err = run_blame(view);
5072 struct got_object_qid *first;
5073 first = STAILQ_FIRST(&s->blamed_commits);
5074 if (!got_object_id_cmp(&first->id, s->commit_id))
5077 thread_err = stop_blame(&s->blame);
5081 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5082 got_object_qid_free(s->blamed_commit);
5084 STAILQ_FIRST(&s->blamed_commits);
5085 err = run_blame(view);
5092 struct got_object_id *id = NULL;
5093 struct got_object_qid *pid;
5094 struct got_commit_object *commit = NULL;
5095 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5096 s->first_displayed_line, s->selected_line);
5099 err = got_object_open_as_commit(&commit, s->repo, id);
5103 got_object_commit_get_parent_ids(commit));
5104 if (view_is_parent_view(view))
5105 begin_x = view_split_begin_x(view->begin_x);
5106 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5107 if (diff_view == NULL) {
5108 got_object_commit_close(commit);
5109 err = got_error_from_errno("view_open");
5112 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5113 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5114 got_object_commit_close(commit);
5116 view_close(diff_view);
5120 diff_view->focussed = 1;
5121 if (view_is_parent_view(view)) {
5122 err = view_close_child(view);
5125 err = view_set_child(view, diff_view);
5128 view->focus_child = 1;
5130 *new_view = diff_view;
5142 if (s->last_displayed_line >= s->blame.nlines &&
5143 s->selected_line >= MIN(s->blame.nlines,
5144 view->nlines - 2)) {
5147 if (s->last_displayed_line >= s->blame.nlines &&
5148 s->selected_line < view->nlines - 2) {
5150 MIN(nscroll, s->last_displayed_line -
5151 s->first_displayed_line - s->selected_line + 1);
5153 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5154 s->first_displayed_line += nscroll;
5156 s->first_displayed_line =
5157 s->blame.nlines - (view->nlines - 3);
5160 if (s->selected_line > view->nlines - 2) {
5161 s->selected_line = MIN(s->blame.nlines,
5168 return thread_err ? thread_err : err;
5171 static const struct got_error *
5172 cmd_blame(int argc, char *argv[])
5174 const struct got_error *error;
5175 struct got_repository *repo = NULL;
5176 struct got_worktree *worktree = NULL;
5177 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5178 char *link_target = NULL;
5179 struct got_object_id *commit_id = NULL;
5180 struct got_commit_object *commit = NULL;
5181 char *commit_id_str = NULL;
5183 struct tog_view *view;
5184 int *pack_fds = NULL;
5186 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5189 commit_id_str = optarg;
5192 repo_path = realpath(optarg, NULL);
5193 if (repo_path == NULL)
5194 return got_error_from_errno2("realpath",
5209 error = got_repo_pack_fds_open(&pack_fds);
5213 if (repo_path == NULL) {
5214 cwd = getcwd(NULL, 0);
5216 return got_error_from_errno("getcwd");
5217 error = got_worktree_open(&worktree, cwd);
5218 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5222 strdup(got_worktree_get_repo_path(worktree));
5224 repo_path = strdup(cwd);
5225 if (repo_path == NULL) {
5226 error = got_error_from_errno("strdup");
5231 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5235 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5242 error = apply_unveil(got_repo_get_path(repo), NULL);
5246 error = tog_load_refs(repo, 0);
5250 if (commit_id_str == NULL) {
5251 struct got_reference *head_ref;
5252 error = got_ref_open(&head_ref, repo, worktree ?
5253 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5256 error = got_ref_resolve(&commit_id, repo, head_ref);
5257 got_ref_close(head_ref);
5259 error = got_repo_match_object_id(&commit_id, NULL,
5260 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5265 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5267 error = got_error_from_errno("view_open");
5271 error = got_object_open_as_commit(&commit, repo, commit_id);
5275 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5280 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5285 /* Release work tree lock. */
5286 got_worktree_close(worktree);
5289 error = view_loop(view);
5297 got_object_commit_close(commit);
5299 got_worktree_close(worktree);
5301 const struct got_error *close_err = got_repo_close(repo);
5306 const struct got_error *pack_err =
5307 got_repo_pack_fds_close(pack_fds);
5315 static const struct got_error *
5316 draw_tree_entries(struct tog_view *view, const char *parent_path)
5318 struct tog_tree_view_state *s = &view->state.tree;
5319 const struct got_error *err = NULL;
5320 struct got_tree_entry *te;
5322 struct tog_color *tc;
5323 int width, n, i, nentries;
5324 int limit = view->nlines;
5328 werase(view->window);
5333 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5337 if (view_needs_focus_indication(view))
5338 wstandout(view->window);
5339 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5341 wattr_on(view->window,
5342 COLOR_PAIR(tc->colorpair), NULL);
5343 waddwstr(view->window, wline);
5345 wattr_off(view->window,
5346 COLOR_PAIR(tc->colorpair), NULL);
5347 if (view_needs_focus_indication(view))
5348 wstandend(view->window);
5351 if (width < view->ncols - 1)
5352 waddch(view->window, '\n');
5355 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5359 waddwstr(view->window, wline);
5362 if (width < view->ncols - 1)
5363 waddch(view->window, '\n');
5366 waddch(view->window, '\n');
5370 if (s->first_displayed_entry == NULL) {
5371 te = got_object_tree_get_first_entry(s->tree);
5372 if (s->selected == 0) {
5374 wstandout(view->window);
5375 s->selected_entry = NULL;
5377 waddstr(view->window, " ..\n"); /* parent directory */
5378 if (s->selected == 0 && view->focussed)
5379 wstandend(view->window);
5386 te = s->first_displayed_entry;
5389 nentries = got_object_tree_get_nentries(s->tree);
5390 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5391 char *line = NULL, *id_str = NULL, *link_target = NULL;
5392 const char *modestr = "";
5395 te = got_object_tree_get_entry(s->tree, i);
5396 mode = got_tree_entry_get_mode(te);
5399 err = got_object_id_str(&id_str,
5400 got_tree_entry_get_id(te));
5402 return got_error_from_errno(
5403 "got_object_id_str");
5405 if (got_object_tree_entry_is_submodule(te))
5407 else if (S_ISLNK(mode)) {
5410 err = got_tree_entry_get_symlink_target(&link_target,
5416 for (i = 0; i < strlen(link_target); i++) {
5417 if (!isprint((unsigned char)link_target[i]))
5418 link_target[i] = '?';
5422 else if (S_ISDIR(mode))
5424 else if (mode & S_IXUSR)
5426 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5427 got_tree_entry_get_name(te), modestr,
5428 link_target ? " -> ": "",
5429 link_target ? link_target : "") == -1) {
5432 return got_error_from_errno("asprintf");
5436 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5442 if (n == s->selected) {
5444 wstandout(view->window);
5445 s->selected_entry = te;
5447 tc = match_color(&s->colors, line);
5449 wattr_on(view->window,
5450 COLOR_PAIR(tc->colorpair), NULL);
5451 waddwstr(view->window, wline);
5453 wattr_off(view->window,
5454 COLOR_PAIR(tc->colorpair), NULL);
5455 if (width < view->ncols - 1)
5456 waddch(view->window, '\n');
5457 if (n == s->selected && view->focussed)
5458 wstandend(view->window);
5464 s->last_displayed_entry = te;
5473 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5475 struct got_tree_entry *te;
5476 int isroot = s->tree == s->root;
5479 if (s->first_displayed_entry == NULL)
5482 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5483 while (i++ < maxscroll) {
5486 s->first_displayed_entry = NULL;
5489 s->first_displayed_entry = te;
5490 te = got_tree_entry_get_prev(s->tree, te);
5495 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5497 struct got_tree_entry *next, *last;
5500 if (s->first_displayed_entry)
5501 next = got_tree_entry_get_next(s->tree,
5502 s->first_displayed_entry);
5504 next = got_object_tree_get_first_entry(s->tree);
5506 last = s->last_displayed_entry;
5507 while (next && last && n++ < maxscroll) {
5508 last = got_tree_entry_get_next(s->tree, last);
5510 s->first_displayed_entry = next;
5511 next = got_tree_entry_get_next(s->tree, next);
5516 static const struct got_error *
5517 tree_entry_path(char **path, struct tog_parent_trees *parents,
5518 struct got_tree_entry *te)
5520 const struct got_error *err = NULL;
5521 struct tog_parent_tree *pt;
5522 size_t len = 2; /* for leading slash and NUL */
5524 TAILQ_FOREACH(pt, parents, entry)
5525 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5528 len += strlen(got_tree_entry_get_name(te));
5530 *path = calloc(1, len);
5532 return got_error_from_errno("calloc");
5535 pt = TAILQ_LAST(parents, tog_parent_trees);
5537 const char *name = got_tree_entry_get_name(pt->selected_entry);
5538 if (strlcat(*path, name, len) >= len) {
5539 err = got_error(GOT_ERR_NO_SPACE);
5542 if (strlcat(*path, "/", len) >= len) {
5543 err = got_error(GOT_ERR_NO_SPACE);
5546 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5549 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5550 err = got_error(GOT_ERR_NO_SPACE);
5562 static const struct got_error *
5563 blame_tree_entry(struct tog_view **new_view, int begin_x,
5564 struct got_tree_entry *te, struct tog_parent_trees *parents,
5565 struct got_object_id *commit_id, struct got_repository *repo)
5567 const struct got_error *err = NULL;
5569 struct tog_view *blame_view;
5573 err = tree_entry_path(&path, parents, te);
5577 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5578 if (blame_view == NULL) {
5579 err = got_error_from_errno("view_open");
5583 err = open_blame_view(blame_view, path, commit_id, repo);
5585 if (err->code == GOT_ERR_CANCELLED)
5587 view_close(blame_view);
5589 *new_view = blame_view;
5595 static const struct got_error *
5596 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5597 struct tog_tree_view_state *s)
5599 struct tog_view *log_view;
5600 const struct got_error *err = NULL;
5605 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5606 if (log_view == NULL)
5607 return got_error_from_errno("view_open");
5609 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5613 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5616 view_close(log_view);
5618 *new_view = log_view;
5623 static const struct got_error *
5624 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5625 const char *head_ref_name, struct got_repository *repo)
5627 const struct got_error *err = NULL;
5628 char *commit_id_str = NULL;
5629 struct tog_tree_view_state *s = &view->state.tree;
5630 struct got_commit_object *commit = NULL;
5632 TAILQ_INIT(&s->parents);
5633 STAILQ_INIT(&s->colors);
5635 s->commit_id = got_object_id_dup(commit_id);
5636 if (s->commit_id == NULL)
5637 return got_error_from_errno("got_object_id_dup");
5639 err = got_object_open_as_commit(&commit, repo, commit_id);
5644 * The root is opened here and will be closed when the view is closed.
5645 * Any visited subtrees and their path-wise parents are opened and
5648 err = got_object_open_as_tree(&s->root, repo,
5649 got_object_commit_get_tree_id(commit));
5654 err = got_object_id_str(&commit_id_str, commit_id);
5658 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5659 err = got_error_from_errno("asprintf");
5663 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5664 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5665 if (head_ref_name) {
5666 s->head_ref_name = strdup(head_ref_name);
5667 if (s->head_ref_name == NULL) {
5668 err = got_error_from_errno("strdup");
5674 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5675 err = add_color(&s->colors, "\\$$",
5676 TOG_COLOR_TREE_SUBMODULE,
5677 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5680 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5681 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5684 err = add_color(&s->colors, "/$",
5685 TOG_COLOR_TREE_DIRECTORY,
5686 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5690 err = add_color(&s->colors, "\\*$",
5691 TOG_COLOR_TREE_EXECUTABLE,
5692 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5696 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5697 get_color_value("TOG_COLOR_COMMIT"));
5702 view->show = show_tree_view;
5703 view->input = input_tree_view;
5704 view->close = close_tree_view;
5705 view->search_start = search_start_tree_view;
5706 view->search_next = search_next_tree_view;
5708 free(commit_id_str);
5710 got_object_commit_close(commit);
5712 close_tree_view(view);
5716 static const struct got_error *
5717 close_tree_view(struct tog_view *view)
5719 struct tog_tree_view_state *s = &view->state.tree;
5721 free_colors(&s->colors);
5722 free(s->tree_label);
5723 s->tree_label = NULL;
5725 s->commit_id = NULL;
5726 free(s->head_ref_name);
5727 s->head_ref_name = NULL;
5728 while (!TAILQ_EMPTY(&s->parents)) {
5729 struct tog_parent_tree *parent;
5730 parent = TAILQ_FIRST(&s->parents);
5731 TAILQ_REMOVE(&s->parents, parent, entry);
5732 if (parent->tree != s->root)
5733 got_object_tree_close(parent->tree);
5737 if (s->tree != NULL && s->tree != s->root)
5738 got_object_tree_close(s->tree);
5740 got_object_tree_close(s->root);
5744 static const struct got_error *
5745 search_start_tree_view(struct tog_view *view)
5747 struct tog_tree_view_state *s = &view->state.tree;
5749 s->matched_entry = NULL;
5754 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5756 regmatch_t regmatch;
5758 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5762 static const struct got_error *
5763 search_next_tree_view(struct tog_view *view)
5765 struct tog_tree_view_state *s = &view->state.tree;
5766 struct got_tree_entry *te = NULL;
5768 if (!view->searching) {
5769 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5773 if (s->matched_entry) {
5774 if (view->searching == TOG_SEARCH_FORWARD) {
5775 if (s->selected_entry)
5776 te = got_tree_entry_get_next(s->tree,
5779 te = got_object_tree_get_first_entry(s->tree);
5781 if (s->selected_entry == NULL)
5782 te = got_object_tree_get_last_entry(s->tree);
5784 te = got_tree_entry_get_prev(s->tree,
5788 if (s->selected_entry)
5789 te = s->selected_entry;
5790 else if (view->searching == TOG_SEARCH_FORWARD)
5791 te = got_object_tree_get_first_entry(s->tree);
5793 te = got_object_tree_get_last_entry(s->tree);
5798 if (s->matched_entry == NULL) {
5799 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5802 if (view->searching == TOG_SEARCH_FORWARD)
5803 te = got_object_tree_get_first_entry(s->tree);
5805 te = got_object_tree_get_last_entry(s->tree);
5808 if (match_tree_entry(te, &view->regex)) {
5809 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5810 s->matched_entry = te;
5814 if (view->searching == TOG_SEARCH_FORWARD)
5815 te = got_tree_entry_get_next(s->tree, te);
5817 te = got_tree_entry_get_prev(s->tree, te);
5820 if (s->matched_entry) {
5821 s->first_displayed_entry = s->matched_entry;
5828 static const struct got_error *
5829 show_tree_view(struct tog_view *view)
5831 const struct got_error *err = NULL;
5832 struct tog_tree_view_state *s = &view->state.tree;
5835 err = tree_entry_path(&parent_path, &s->parents, NULL);
5839 err = draw_tree_entries(view, parent_path);
5846 static const struct got_error *
5847 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5849 const struct got_error *err = NULL;
5850 struct tog_tree_view_state *s = &view->state.tree;
5851 struct tog_view *log_view, *ref_view;
5852 struct got_tree_entry *te;
5853 int begin_x = 0, n, nscroll = view->nlines - 3;
5857 s->show_ids = !s->show_ids;
5860 if (!s->selected_entry)
5862 if (view_is_parent_view(view))
5863 begin_x = view_split_begin_x(view->begin_x);
5864 err = log_selected_tree_entry(&log_view, begin_x, s);
5866 log_view->focussed = 1;
5867 if (view_is_parent_view(view)) {
5868 err = view_close_child(view);
5871 err = view_set_child(view, log_view);
5874 view->focus_child = 1;
5876 *new_view = log_view;
5879 if (view_is_parent_view(view))
5880 begin_x = view_split_begin_x(view->begin_x);
5881 ref_view = view_open(view->nlines, view->ncols,
5882 view->begin_y, begin_x, TOG_VIEW_REF);
5883 if (ref_view == NULL)
5884 return got_error_from_errno("view_open");
5885 err = open_ref_view(ref_view, s->repo);
5887 view_close(ref_view);
5891 ref_view->focussed = 1;
5892 if (view_is_parent_view(view)) {
5893 err = view_close_child(view);
5896 err = view_set_child(view, ref_view);
5899 view->focus_child = 1;
5901 *new_view = ref_view;
5906 if (s->tree == s->root)
5907 s->first_displayed_entry =
5908 got_object_tree_get_first_entry(s->tree);
5910 s->first_displayed_entry = NULL;
5915 te = got_object_tree_get_last_entry(s->tree);
5916 for (n = 0; n < view->nlines - 3; n++) {
5918 if(s->tree != s->root) {
5919 s->first_displayed_entry = NULL;
5924 s->first_displayed_entry = te;
5925 te = got_tree_entry_get_prev(s->tree, te);
5928 s->selected = n - 1;
5933 if (s->selected > 0) {
5937 tree_scroll_up(s, 1);
5945 if (s->tree == s->root) {
5946 if (got_object_tree_get_first_entry(s->tree) ==
5947 s->first_displayed_entry)
5948 s->selected -= MIN(s->selected, nscroll);
5950 if (s->first_displayed_entry == NULL)
5951 s->selected -= MIN(s->selected, nscroll);
5953 tree_scroll_up(s, MAX(0, nscroll));
5958 if (s->selected < s->ndisplayed - 1) {
5962 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5964 /* can't scroll any further */
5966 tree_scroll_down(s, 1);
5974 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5976 /* can't scroll any further; move cursor down */
5977 if (s->selected < s->ndisplayed - 1)
5978 s->selected += MIN(nscroll,
5979 s->ndisplayed - s->selected - 1);
5982 tree_scroll_down(s, nscroll);
5987 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5988 struct tog_parent_tree *parent;
5989 /* user selected '..' */
5990 if (s->tree == s->root)
5992 parent = TAILQ_FIRST(&s->parents);
5993 TAILQ_REMOVE(&s->parents, parent,
5995 got_object_tree_close(s->tree);
5996 s->tree = parent->tree;
5997 s->first_displayed_entry =
5998 parent->first_displayed_entry;
6000 parent->selected_entry;
6001 s->selected = parent->selected;
6003 } else if (S_ISDIR(got_tree_entry_get_mode(
6004 s->selected_entry))) {
6005 struct got_tree_object *subtree;
6006 err = got_object_open_as_tree(&subtree, s->repo,
6007 got_tree_entry_get_id(s->selected_entry));
6010 err = tree_view_visit_subtree(s, subtree);
6012 got_object_tree_close(subtree);
6015 } else if (S_ISREG(got_tree_entry_get_mode(
6016 s->selected_entry))) {
6017 struct tog_view *blame_view;
6018 int begin_x = view_is_parent_view(view) ?
6019 view_split_begin_x(view->begin_x) : 0;
6021 err = blame_tree_entry(&blame_view, begin_x,
6022 s->selected_entry, &s->parents,
6023 s->commit_id, s->repo);
6027 blame_view->focussed = 1;
6028 if (view_is_parent_view(view)) {
6029 err = view_close_child(view);
6032 err = view_set_child(view, blame_view);
6035 view->focus_child = 1;
6037 *new_view = blame_view;
6041 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6042 s->selected = view->nlines - 4;
6056 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6061 static const struct got_error *
6062 cmd_tree(int argc, char *argv[])
6064 const struct got_error *error;
6065 struct got_repository *repo = NULL;
6066 struct got_worktree *worktree = NULL;
6067 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6068 struct got_object_id *commit_id = NULL;
6069 struct got_commit_object *commit = NULL;
6070 const char *commit_id_arg = NULL;
6072 struct got_reference *ref = NULL;
6073 const char *head_ref_name = NULL;
6075 struct tog_view *view;
6076 int *pack_fds = NULL;
6078 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6081 commit_id_arg = optarg;
6084 repo_path = realpath(optarg, NULL);
6085 if (repo_path == NULL)
6086 return got_error_from_errno2("realpath",
6101 error = got_repo_pack_fds_open(&pack_fds);
6105 if (repo_path == NULL) {
6106 cwd = getcwd(NULL, 0);
6108 return got_error_from_errno("getcwd");
6109 error = got_worktree_open(&worktree, cwd);
6110 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6114 strdup(got_worktree_get_repo_path(worktree));
6116 repo_path = strdup(cwd);
6117 if (repo_path == NULL) {
6118 error = got_error_from_errno("strdup");
6123 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6127 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6134 error = apply_unveil(got_repo_get_path(repo), NULL);
6138 error = tog_load_refs(repo, 0);
6142 if (commit_id_arg == NULL) {
6143 error = got_repo_match_object_id(&commit_id, &label,
6144 worktree ? got_worktree_get_head_ref_name(worktree) :
6145 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6148 head_ref_name = label;
6150 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6152 head_ref_name = got_ref_get_name(ref);
6153 else if (error->code != GOT_ERR_NOT_REF)
6155 error = got_repo_match_object_id(&commit_id, NULL,
6156 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6161 error = got_object_open_as_commit(&commit, repo, commit_id);
6165 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6167 error = got_error_from_errno("view_open");
6170 error = open_tree_view(view, commit_id, head_ref_name, repo);
6173 if (!got_path_is_root_dir(in_repo_path)) {
6174 error = tree_view_walk_path(&view->state.tree, commit,
6181 /* Release work tree lock. */
6182 got_worktree_close(worktree);
6185 error = view_loop(view);
6194 const struct got_error *close_err = got_repo_close(repo);
6199 const struct got_error *pack_err =
6200 got_repo_pack_fds_close(pack_fds);
6208 static const struct got_error *
6209 ref_view_load_refs(struct tog_ref_view_state *s)
6211 struct got_reflist_entry *sre;
6212 struct tog_reflist_entry *re;
6215 TAILQ_FOREACH(sre, &tog_refs, entry) {
6216 if (strncmp(got_ref_get_name(sre->ref),
6217 "refs/got/", 9) == 0 &&
6218 strncmp(got_ref_get_name(sre->ref),
6219 "refs/got/backup/", 16) != 0)
6222 re = malloc(sizeof(*re));
6224 return got_error_from_errno("malloc");
6226 re->ref = got_ref_dup(sre->ref);
6227 if (re->ref == NULL)
6228 return got_error_from_errno("got_ref_dup");
6229 re->idx = s->nrefs++;
6230 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6233 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6238 ref_view_free_refs(struct tog_ref_view_state *s)
6240 struct tog_reflist_entry *re;
6242 while (!TAILQ_EMPTY(&s->refs)) {
6243 re = TAILQ_FIRST(&s->refs);
6244 TAILQ_REMOVE(&s->refs, re, entry);
6245 got_ref_close(re->ref);
6250 static const struct got_error *
6251 open_ref_view(struct tog_view *view, struct got_repository *repo)
6253 const struct got_error *err = NULL;
6254 struct tog_ref_view_state *s = &view->state.ref;
6256 s->selected_entry = 0;
6259 TAILQ_INIT(&s->refs);
6260 STAILQ_INIT(&s->colors);
6262 err = ref_view_load_refs(s);
6266 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6267 err = add_color(&s->colors, "^refs/heads/",
6268 TOG_COLOR_REFS_HEADS,
6269 get_color_value("TOG_COLOR_REFS_HEADS"));
6273 err = add_color(&s->colors, "^refs/tags/",
6274 TOG_COLOR_REFS_TAGS,
6275 get_color_value("TOG_COLOR_REFS_TAGS"));
6279 err = add_color(&s->colors, "^refs/remotes/",
6280 TOG_COLOR_REFS_REMOTES,
6281 get_color_value("TOG_COLOR_REFS_REMOTES"));
6285 err = add_color(&s->colors, "^refs/got/backup/",
6286 TOG_COLOR_REFS_BACKUP,
6287 get_color_value("TOG_COLOR_REFS_BACKUP"));
6292 view->show = show_ref_view;
6293 view->input = input_ref_view;
6294 view->close = close_ref_view;
6295 view->search_start = search_start_ref_view;
6296 view->search_next = search_next_ref_view;
6299 free_colors(&s->colors);
6303 static const struct got_error *
6304 close_ref_view(struct tog_view *view)
6306 struct tog_ref_view_state *s = &view->state.ref;
6308 ref_view_free_refs(s);
6309 free_colors(&s->colors);
6314 static const struct got_error *
6315 resolve_reflist_entry(struct got_object_id **commit_id,
6316 struct tog_reflist_entry *re, struct got_repository *repo)
6318 const struct got_error *err = NULL;
6319 struct got_object_id *obj_id;
6320 struct got_tag_object *tag = NULL;
6325 err = got_ref_resolve(&obj_id, repo, re->ref);
6329 err = got_object_get_type(&obj_type, repo, obj_id);
6334 case GOT_OBJ_TYPE_COMMIT:
6335 *commit_id = obj_id;
6337 case GOT_OBJ_TYPE_TAG:
6338 err = got_object_open_as_tag(&tag, repo, obj_id);
6342 err = got_object_get_type(&obj_type, repo,
6343 got_object_tag_get_object_id(tag));
6346 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6347 err = got_error(GOT_ERR_OBJ_TYPE);
6350 *commit_id = got_object_id_dup(
6351 got_object_tag_get_object_id(tag));
6352 if (*commit_id == NULL) {
6353 err = got_error_from_errno("got_object_id_dup");
6358 err = got_error(GOT_ERR_OBJ_TYPE);
6364 got_object_tag_close(tag);
6372 static const struct got_error *
6373 log_ref_entry(struct tog_view **new_view, int begin_x,
6374 struct tog_reflist_entry *re, struct got_repository *repo)
6376 struct tog_view *log_view;
6377 const struct got_error *err = NULL;
6378 struct got_object_id *commit_id = NULL;
6382 err = resolve_reflist_entry(&commit_id, re, repo);
6384 if (err->code != GOT_ERR_OBJ_TYPE)
6390 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6391 if (log_view == NULL) {
6392 err = got_error_from_errno("view_open");
6396 err = open_log_view(log_view, commit_id, repo,
6397 got_ref_get_name(re->ref), "", 0);
6400 view_close(log_view);
6402 *new_view = log_view;
6408 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6410 struct tog_reflist_entry *re;
6413 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6416 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6417 while (i++ < maxscroll) {
6420 s->first_displayed_entry = re;
6421 re = TAILQ_PREV(re, tog_reflist_head, entry);
6426 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6428 struct tog_reflist_entry *next, *last;
6431 if (s->first_displayed_entry)
6432 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6434 next = TAILQ_FIRST(&s->refs);
6436 last = s->last_displayed_entry;
6437 while (next && last && n++ < maxscroll) {
6438 last = TAILQ_NEXT(last, entry);
6440 s->first_displayed_entry = next;
6441 next = TAILQ_NEXT(next, entry);
6446 static const struct got_error *
6447 search_start_ref_view(struct tog_view *view)
6449 struct tog_ref_view_state *s = &view->state.ref;
6451 s->matched_entry = NULL;
6456 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6458 regmatch_t regmatch;
6460 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6464 static const struct got_error *
6465 search_next_ref_view(struct tog_view *view)
6467 struct tog_ref_view_state *s = &view->state.ref;
6468 struct tog_reflist_entry *re = NULL;
6470 if (!view->searching) {
6471 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6475 if (s->matched_entry) {
6476 if (view->searching == TOG_SEARCH_FORWARD) {
6477 if (s->selected_entry)
6478 re = TAILQ_NEXT(s->selected_entry, entry);
6480 re = TAILQ_PREV(s->selected_entry,
6481 tog_reflist_head, entry);
6483 if (s->selected_entry == NULL)
6484 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6486 re = TAILQ_PREV(s->selected_entry,
6487 tog_reflist_head, entry);
6490 if (s->selected_entry)
6491 re = s->selected_entry;
6492 else if (view->searching == TOG_SEARCH_FORWARD)
6493 re = TAILQ_FIRST(&s->refs);
6495 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6500 if (s->matched_entry == NULL) {
6501 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6504 if (view->searching == TOG_SEARCH_FORWARD)
6505 re = TAILQ_FIRST(&s->refs);
6507 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6510 if (match_reflist_entry(re, &view->regex)) {
6511 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6512 s->matched_entry = re;
6516 if (view->searching == TOG_SEARCH_FORWARD)
6517 re = TAILQ_NEXT(re, entry);
6519 re = TAILQ_PREV(re, tog_reflist_head, entry);
6522 if (s->matched_entry) {
6523 s->first_displayed_entry = s->matched_entry;
6530 static const struct got_error *
6531 show_ref_view(struct tog_view *view)
6533 const struct got_error *err = NULL;
6534 struct tog_ref_view_state *s = &view->state.ref;
6535 struct tog_reflist_entry *re;
6538 struct tog_color *tc;
6540 int limit = view->nlines;
6542 werase(view->window);
6549 re = s->first_displayed_entry;
6551 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6553 return got_error_from_errno("asprintf");
6555 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6560 if (view_needs_focus_indication(view))
6561 wstandout(view->window);
6562 waddwstr(view->window, wline);
6563 if (view_needs_focus_indication(view))
6564 wstandend(view->window);
6569 if (width < view->ncols - 1)
6570 waddch(view->window, '\n');
6575 while (re && limit > 0) {
6577 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6580 struct got_commit_object *ci;
6581 struct got_tag_object *tag;
6582 struct got_object_id *id;
6586 err = got_ref_resolve(&id, s->repo, re->ref);
6589 err = got_object_open_as_tag(&tag, s->repo, id);
6591 if (err->code != GOT_ERR_OBJ_TYPE) {
6595 err = got_object_open_as_commit(&ci, s->repo,
6601 t = got_object_commit_get_committer_time(ci);
6602 got_object_commit_close(ci);
6604 t = got_object_tag_get_tagger_time(tag);
6605 got_object_tag_close(tag);
6608 if (gmtime_r(&t, &tm) == NULL)
6609 return got_error_from_errno("gmtime_r");
6610 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6611 return got_error(GOT_ERR_NO_SPACE);
6613 if (got_ref_is_symbolic(re->ref)) {
6614 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6615 ymd : "", got_ref_get_name(re->ref),
6616 got_ref_get_symref_target(re->ref)) == -1)
6617 return got_error_from_errno("asprintf");
6618 } else if (s->show_ids) {
6619 struct got_object_id *id;
6621 err = got_ref_resolve(&id, s->repo, re->ref);
6624 err = got_object_id_str(&id_str, id);
6629 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6630 got_ref_get_name(re->ref), id_str) == -1) {
6631 err = got_error_from_errno("asprintf");
6638 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6639 got_ref_get_name(re->ref)) == -1)
6640 return got_error_from_errno("asprintf");
6642 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6648 if (n == s->selected) {
6650 wstandout(view->window);
6651 s->selected_entry = re;
6653 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6655 wattr_on(view->window,
6656 COLOR_PAIR(tc->colorpair), NULL);
6657 waddwstr(view->window, wline);
6659 wattr_off(view->window,
6660 COLOR_PAIR(tc->colorpair), NULL);
6661 if (width < view->ncols - 1)
6662 waddch(view->window, '\n');
6663 if (n == s->selected && view->focussed)
6664 wstandend(view->window);
6670 s->last_displayed_entry = re;
6673 re = TAILQ_NEXT(re, entry);
6680 static const struct got_error *
6681 browse_ref_tree(struct tog_view **new_view, int begin_x,
6682 struct tog_reflist_entry *re, struct got_repository *repo)
6684 const struct got_error *err = NULL;
6685 struct got_object_id *commit_id = NULL;
6686 struct tog_view *tree_view;
6690 err = resolve_reflist_entry(&commit_id, re, repo);
6692 if (err->code != GOT_ERR_OBJ_TYPE)
6699 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6700 if (tree_view == NULL) {
6701 err = got_error_from_errno("view_open");
6705 err = open_tree_view(tree_view, commit_id,
6706 got_ref_get_name(re->ref), repo);
6710 *new_view = tree_view;
6715 static const struct got_error *
6716 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6718 const struct got_error *err = NULL;
6719 struct tog_ref_view_state *s = &view->state.ref;
6720 struct tog_view *log_view, *tree_view;
6721 struct tog_reflist_entry *re;
6722 int begin_x = 0, n, nscroll = view->nlines - 1;
6726 s->show_ids = !s->show_ids;
6729 s->show_date = !s->show_date;
6732 s->sort_by_date = !s->sort_by_date;
6733 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6734 got_ref_cmp_by_commit_timestamp_descending :
6735 tog_ref_cmp_by_name, s->repo);
6738 got_reflist_object_id_map_free(tog_refs_idmap);
6739 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6740 &tog_refs, s->repo);
6743 ref_view_free_refs(s);
6744 err = ref_view_load_refs(s);
6748 if (!s->selected_entry)
6750 if (view_is_parent_view(view))
6751 begin_x = view_split_begin_x(view->begin_x);
6752 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6755 log_view->focussed = 1;
6756 if (view_is_parent_view(view)) {
6757 err = view_close_child(view);
6760 err = view_set_child(view, log_view);
6763 view->focus_child = 1;
6765 *new_view = log_view;
6768 if (!s->selected_entry)
6770 if (view_is_parent_view(view))
6771 begin_x = view_split_begin_x(view->begin_x);
6772 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6774 if (err || tree_view == NULL)
6777 tree_view->focussed = 1;
6778 if (view_is_parent_view(view)) {
6779 err = view_close_child(view);
6782 err = view_set_child(view, tree_view);
6785 view->focus_child = 1;
6787 *new_view = tree_view;
6792 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6797 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6798 for (n = 0; n < view->nlines - 1; n++) {
6801 s->first_displayed_entry = re;
6802 re = TAILQ_PREV(re, tog_reflist_head, entry);
6805 s->selected = n - 1;
6810 if (s->selected > 0) {
6814 ref_scroll_up(s, 1);
6822 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6823 s->selected -= MIN(nscroll, s->selected);
6824 ref_scroll_up(s, MAX(0, nscroll));
6829 if (s->selected < s->ndisplayed - 1) {
6833 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6834 /* can't scroll any further */
6836 ref_scroll_down(s, 1);
6844 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6845 /* can't scroll any further; move cursor down */
6846 if (s->selected < s->ndisplayed - 1)
6847 s->selected += MIN(nscroll,
6848 s->ndisplayed - s->selected - 1);
6851 ref_scroll_down(s, nscroll);
6855 err = tog_load_refs(s->repo, s->sort_by_date);
6858 ref_view_free_refs(s);
6859 err = ref_view_load_refs(s);
6862 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6863 s->selected = view->nlines - 2;
6876 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6881 static const struct got_error *
6882 cmd_ref(int argc, char *argv[])
6884 const struct got_error *error;
6885 struct got_repository *repo = NULL;
6886 struct got_worktree *worktree = NULL;
6887 char *cwd = NULL, *repo_path = NULL;
6889 struct tog_view *view;
6890 int *pack_fds = NULL;
6892 while ((ch = getopt(argc, argv, "r:")) != -1) {
6895 repo_path = realpath(optarg, NULL);
6896 if (repo_path == NULL)
6897 return got_error_from_errno2("realpath",
6912 error = got_repo_pack_fds_open(&pack_fds);
6916 if (repo_path == NULL) {
6917 cwd = getcwd(NULL, 0);
6919 return got_error_from_errno("getcwd");
6920 error = got_worktree_open(&worktree, cwd);
6921 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6925 strdup(got_worktree_get_repo_path(worktree));
6927 repo_path = strdup(cwd);
6928 if (repo_path == NULL) {
6929 error = got_error_from_errno("strdup");
6934 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6940 error = apply_unveil(got_repo_get_path(repo), NULL);
6944 error = tog_load_refs(repo, 0);
6948 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6950 error = got_error_from_errno("view_open");
6954 error = open_ref_view(view, repo);
6959 /* Release work tree lock. */
6960 got_worktree_close(worktree);
6963 error = view_loop(view);
6968 const struct got_error *close_err = got_repo_close(repo);
6973 const struct got_error *pack_err =
6974 got_repo_pack_fds_close(pack_fds);
6983 list_commands(FILE *fp)
6987 fprintf(fp, "commands:");
6988 for (i = 0; i < nitems(tog_commands); i++) {
6989 const struct tog_cmd *cmd = &tog_commands[i];
6990 fprintf(fp, " %s", cmd->name);
6996 usage(int hflag, int status)
6998 FILE *fp = (status == 0) ? stdout : stderr;
7000 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7003 fprintf(fp, "lazy usage: %s path\n", getprogname());
7010 make_argv(int argc, ...)
7018 argv = calloc(argc, sizeof(char *));
7021 for (i = 0; i < argc; i++) {
7022 argv[i] = strdup(va_arg(ap, char *));
7023 if (argv[i] == NULL)
7032 * Try to convert 'tog path' into a 'tog log path' command.
7033 * The user could simply have mistyped the command rather than knowingly
7034 * provided a path. So check whether argv[0] can in fact be resolved
7035 * to a path in the HEAD commit and print a special error if not.
7036 * This hack is for mpi@ <3
7038 static const struct got_error *
7039 tog_log_with_path(int argc, char *argv[])
7041 const struct got_error *error = NULL, *close_err;
7042 const struct tog_cmd *cmd = NULL;
7043 struct got_repository *repo = NULL;
7044 struct got_worktree *worktree = NULL;
7045 struct got_object_id *commit_id = NULL, *id = NULL;
7046 struct got_commit_object *commit = NULL;
7047 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7048 char *commit_id_str = NULL, **cmd_argv = NULL;
7049 int *pack_fds = NULL;
7051 cwd = getcwd(NULL, 0);
7053 return got_error_from_errno("getcwd");
7055 error = got_repo_pack_fds_open(&pack_fds);
7059 error = got_worktree_open(&worktree, cwd);
7060 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7064 repo_path = strdup(got_worktree_get_repo_path(worktree));
7066 repo_path = strdup(cwd);
7067 if (repo_path == NULL) {
7068 error = got_error_from_errno("strdup");
7072 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7076 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7081 error = tog_load_refs(repo, 0);
7084 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7085 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7086 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7091 got_worktree_close(worktree);
7095 error = got_object_open_as_commit(&commit, repo, commit_id);
7099 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7101 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7103 fprintf(stderr, "%s: '%s' is no known command or path\n",
7104 getprogname(), argv[0]);
7109 close_err = got_repo_close(repo);
7114 error = got_object_id_str(&commit_id_str, commit_id);
7118 cmd = &tog_commands[0]; /* log */
7120 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7121 error = cmd->cmd_main(argc, cmd_argv);
7124 close_err = got_repo_close(repo);
7129 got_object_commit_close(commit);
7131 got_worktree_close(worktree);
7133 const struct got_error *pack_err =
7134 got_repo_pack_fds_close(pack_fds);
7139 free(commit_id_str);
7146 for (i = 0; i < argc; i++)
7155 main(int argc, char *argv[])
7157 const struct got_error *error = NULL;
7158 const struct tog_cmd *cmd = NULL;
7159 int ch, hflag = 0, Vflag = 0;
7160 char **cmd_argv = NULL;
7161 static const struct option longopts[] = {
7162 { "version", no_argument, NULL, 'V' },
7166 setlocale(LC_CTYPE, "");
7168 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7188 got_version_print_str();
7193 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7201 /* Build an argument vector which runs a default command. */
7202 cmd = &tog_commands[0];
7204 cmd_argv = make_argv(argc, cmd->name);
7208 /* Did the user specify a command? */
7209 for (i = 0; i < nitems(tog_commands); i++) {
7210 if (strncmp(tog_commands[i].name, argv[0],
7211 strlen(argv[0])) == 0) {
7212 cmd = &tog_commands[i];
7221 /* No command specified; try log with a path */
7222 error = tog_log_with_path(argc, argv);
7227 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7234 for (i = 0; i < argc; i++)
7239 if (error && error->code != GOT_ERR_CANCELLED)
7240 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);