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()
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);
766 if (wresize(view->window, nlines, ncols) == ERR)
767 return got_error_from_errno("wresize");
768 if (replace_panel(view->panel, view->window) == ERR)
769 return got_error_from_errno("replace_panel");
770 wclear(view->window);
772 view->nlines = nlines;
778 view->child->begin_x = view_split_begin_x(view->begin_x);
779 if (view->child->begin_x == 0) {
780 view_fullscreen(view->child);
781 if (view->child->focussed)
782 show_panel(view->child->panel);
784 show_panel(view->panel);
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
794 static const struct got_error *
795 view_close_child(struct tog_view *view)
797 const struct got_error *err = NULL;
799 if (view->child == NULL)
802 err = view_close(view->child);
808 view_set_child(struct tog_view *view, struct tog_view *child)
811 child->parent = view;
815 view_is_splitscreen(struct tog_view *view)
817 return view->begin_x > 0;
826 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
827 cols = 80; /* Default */
833 resize_term(lines, cols);
836 static const struct got_error *
837 view_search_start(struct tog_view *view)
839 const struct got_error *err = NULL;
843 if (view->search_started) {
844 regfree(&view->regex);
846 memset(&view->regmatch, 0, sizeof(view->regmatch));
848 view->search_started = 0;
850 if (view->nlines < 1)
853 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
854 wclrtoeol(view->window);
858 ret = wgetnstr(view->window, pattern, sizeof(pattern));
864 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
865 err = view->search_start(view);
867 regfree(&view->regex);
870 view->search_started = 1;
871 view->searching = TOG_SEARCH_FORWARD;
872 view->search_next_done = 0;
873 view->search_next(view);
879 static const struct got_error *
880 view_input(struct tog_view **new, int *done, struct tog_view *view,
881 struct tog_view_list_head *views)
883 const struct got_error *err = NULL;
889 /* Clear "no matches" indicator. */
890 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
891 view->search_next_done == TOG_SEARCH_HAVE_NONE)
892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
894 if (view->searching && !view->search_next_done) {
895 errcode = pthread_mutex_unlock(&tog_mutex);
897 return got_error_set_errno(errcode,
898 "pthread_mutex_unlock");
900 errcode = pthread_mutex_lock(&tog_mutex);
902 return got_error_set_errno(errcode,
903 "pthread_mutex_lock");
904 view->search_next(view);
908 nodelay(stdscr, FALSE);
909 /* Allow threads to make progress while we are waiting for input. */
910 errcode = pthread_mutex_unlock(&tog_mutex);
912 return got_error_set_errno(errcode, "pthread_mutex_unlock");
913 ch = wgetch(view->window);
914 errcode = pthread_mutex_lock(&tog_mutex);
916 return got_error_set_errno(errcode, "pthread_mutex_lock");
917 nodelay(stdscr, TRUE);
919 if (tog_sigwinch_received || tog_sigcont_received) {
921 tog_sigwinch_received = 0;
922 tog_sigcont_received = 0;
923 TAILQ_FOREACH(v, views, entry) {
924 err = view_resize(v);
927 err = v->input(new, v, KEY_RESIZE);
931 err = view_resize(v->child);
934 err = v->child->input(new, v->child,
946 view->child->focussed = 1;
947 view->focus_child = 1;
948 } else if (view->parent) {
950 view->parent->focussed = 1;
951 view->parent->focus_child = 0;
955 err = view->input(new, view, ch);
962 if (view_is_parent_view(view)) {
963 if (view->child == NULL)
965 if (view_is_splitscreen(view->child)) {
967 view->child->focussed = 1;
968 err = view_fullscreen(view->child);
970 err = view_splitscreen(view->child);
973 err = view->child->input(new, view->child,
976 if (view_is_splitscreen(view)) {
977 view->parent->focussed = 0;
979 err = view_fullscreen(view);
981 err = view_splitscreen(view);
985 err = view->input(new, view, KEY_RESIZE);
991 if (view->search_start)
992 view_search_start(view);
994 err = view->input(new, view, ch);
998 if (view->search_started && view->search_next) {
999 view->searching = (ch == 'n' ?
1000 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1001 view->search_next_done = 0;
1002 view->search_next(view);
1004 err = view->input(new, view, ch);
1007 err = view->input(new, view, ch);
1015 view_vborder(struct tog_view *view)
1018 const struct tog_view *view_above;
1021 return view_vborder(view->parent);
1023 panel = panel_above(view->panel);
1027 view_above = panel_userptr(panel);
1028 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1029 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1033 view_needs_focus_indication(struct tog_view *view)
1035 if (view_is_parent_view(view)) {
1036 if (view->child == NULL || view->child->focussed)
1038 if (!view_is_splitscreen(view->child))
1040 } else if (!view_is_splitscreen(view))
1043 return view->focussed;
1046 static const struct got_error *
1047 view_loop(struct tog_view *view)
1049 const struct got_error *err = NULL;
1050 struct tog_view_list_head views;
1051 struct tog_view *new_view;
1052 int fast_refresh = 10;
1053 int done = 0, errcode;
1055 errcode = pthread_mutex_lock(&tog_mutex);
1057 return got_error_set_errno(errcode, "pthread_mutex_lock");
1060 TAILQ_INSERT_HEAD(&views, view, entry);
1063 err = view->show(view);
1068 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1069 /* Refresh fast during initialization, then become slower. */
1070 if (fast_refresh && fast_refresh-- == 0)
1071 halfdelay(10); /* switch to once per second */
1073 err = view_input(&new_view, &done, view, &views);
1077 struct tog_view *v, *prev = NULL;
1079 if (view_is_parent_view(view))
1080 prev = TAILQ_PREV(view, tog_view_list_head,
1082 else if (view->parent)
1083 prev = view->parent;
1086 view->parent->child = NULL;
1087 view->parent->focus_child = 0;
1089 TAILQ_REMOVE(&views, view, entry);
1091 err = view_close(view);
1096 TAILQ_FOREACH(v, &views, entry) {
1100 if (view == NULL && new_view == NULL) {
1101 /* No view has focus. Try to pick one. */
1104 else if (!TAILQ_EMPTY(&views)) {
1105 view = TAILQ_LAST(&views,
1106 tog_view_list_head);
1109 if (view->focus_child) {
1110 view->child->focussed = 1;
1118 struct tog_view *v, *t;
1119 /* Only allow one parent view per type. */
1120 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1121 if (v->type != new_view->type)
1123 TAILQ_REMOVE(&views, v, entry);
1124 err = view_close(v);
1129 TAILQ_INSERT_TAIL(&views, new_view, entry);
1133 if (view_is_parent_view(view)) {
1134 if (view->child && view->child->focussed)
1137 if (view->parent && view->parent->focussed)
1138 view = view->parent;
1140 show_panel(view->panel);
1141 if (view->child && view_is_splitscreen(view->child))
1142 show_panel(view->child->panel);
1143 if (view->parent && view_is_splitscreen(view)) {
1144 err = view->parent->show(view->parent);
1148 err = view->show(view);
1152 err = view->child->show(view->child);
1161 while (!TAILQ_EMPTY(&views)) {
1162 view = TAILQ_FIRST(&views);
1163 TAILQ_REMOVE(&views, view, entry);
1167 errcode = pthread_mutex_unlock(&tog_mutex);
1168 if (errcode && err == NULL)
1169 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1179 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1184 /* Create newly allocated wide-character string equivalent to a byte string. */
1185 static const struct got_error *
1186 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1189 const struct got_error *err = NULL;
1192 *wlen = mbstowcs(NULL, s, 0);
1193 if (*wlen == (size_t)-1) {
1195 if (errno != EILSEQ)
1196 return got_error_from_errno("mbstowcs");
1198 /* byte string invalid in current encoding; try to "fix" it */
1199 err = got_mbsavis(&vis, &vislen, s);
1202 *wlen = mbstowcs(NULL, vis, 0);
1203 if (*wlen == (size_t)-1) {
1204 err = got_error_from_errno("mbstowcs"); /* give up */
1209 *ws = calloc(*wlen + 1, sizeof(**ws));
1211 err = got_error_from_errno("calloc");
1215 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1216 err = got_error_from_errno("mbstowcs");
1227 static const struct got_error *
1228 expand_tab(char **ptr, const char *src)
1231 size_t len, n, idx = 0, sz = 0;
1234 n = len = strlen(src);
1235 dst = malloc((n + 1) * sizeof(char));
1237 return got_error_from_errno("malloc");
1239 while (idx < len && src[idx]) {
1240 const char c = src[idx];
1243 size_t nb = TABSIZE - sz % TABSIZE;
1245 dst = reallocarray(dst, n, sizeof(char));
1247 return got_error_from_errno("reallocarray");
1248 memcpy(dst + sz, " ", nb);
1251 dst[sz++] = src[idx];
1260 /* Format a line for display, ensuring that it won't overflow a width limit. */
1261 static const struct got_error *
1262 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1263 int col_tab_align, int expand)
1265 const struct got_error *err = NULL;
1267 wchar_t *wline = NULL;
1276 err = expand_tab(&exstr, line);
1281 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1286 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1287 wline[wlen - 1] = L'\0';
1290 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1291 wline[wlen - 1] = L'\0';
1297 int width = wcwidth(wline[i]);
1304 if (width == 1 || width == 2) {
1305 if (cols + width > wlimit)
1309 } else if (width == -1) {
1310 if (wline[i] == L'\t') {
1312 ((cols + col_tab_align) % TABSIZE);
1317 if (cols + width > wlimit)
1322 err = got_error_from_errno("wcwidth");
1337 /* Skip the leading nscroll columns of a wide character string. */
1338 const struct got_error *
1339 scroll_wline(wchar_t **wlinep, wchar_t *wline, int nscroll,
1343 size_t wlen = wcslen(wline);
1348 while (i < wlen && cols < nscroll) {
1349 int width = wcwidth(wline[i]);
1356 if (width == 1 || width == 2) {
1357 if (cols + width > nscroll)
1361 } else if (width == -1) {
1362 if (wline[i] == L'\t') {
1364 ((cols + col_tab_align) % TABSIZE);
1369 if (cols + width > nscroll)
1374 return got_error_from_errno("wcwidth");
1378 *wlinep = &wline[j];
1382 static const struct got_error*
1383 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1384 struct got_object_id *id, struct got_repository *repo)
1386 static const struct got_error *err = NULL;
1387 struct got_reflist_entry *re;
1393 TAILQ_FOREACH(re, refs, entry) {
1394 struct got_tag_object *tag = NULL;
1395 struct got_object_id *ref_id;
1398 name = got_ref_get_name(re->ref);
1399 if (strcmp(name, GOT_REF_HEAD) == 0)
1401 if (strncmp(name, "refs/", 5) == 0)
1403 if (strncmp(name, "got/", 4) == 0 &&
1404 strncmp(name, "got/backup/", 11) != 0)
1406 if (strncmp(name, "heads/", 6) == 0)
1408 if (strncmp(name, "remotes/", 8) == 0) {
1410 s = strstr(name, "/" GOT_REF_HEAD);
1411 if (s != NULL && s[strlen(s)] == '\0')
1414 err = got_ref_resolve(&ref_id, repo, re->ref);
1417 if (strncmp(name, "tags/", 5) == 0) {
1418 err = got_object_open_as_tag(&tag, repo, ref_id);
1420 if (err->code != GOT_ERR_OBJ_TYPE) {
1424 /* Ref points at something other than a tag. */
1429 cmp = got_object_id_cmp(tag ?
1430 got_object_tag_get_object_id(tag) : ref_id, id);
1433 got_object_tag_close(tag);
1437 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1438 s ? ", " : "", name) == -1) {
1439 err = got_error_from_errno("asprintf");
1450 static const struct got_error *
1451 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1456 smallerthan = strchr(author, '<');
1457 if (smallerthan && smallerthan[1] != '\0')
1458 author = smallerthan + 1;
1459 author[strcspn(author, "@>")] = '\0';
1460 return format_line(wauthor, author_width, author, limit, col_tab_align,
1464 static const struct got_error *
1465 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1466 struct got_object_id *id, const size_t date_display_cols,
1467 int author_display_cols)
1469 struct tog_log_view_state *s = &view->state.log;
1470 const struct got_error *err = NULL;
1471 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1472 char *logmsg0 = NULL, *logmsg = NULL;
1473 char *author = NULL;
1474 wchar_t *wlogmsg = NULL, *wauthor = NULL, *scrolled_wline;
1475 int author_width, logmsg_width;
1476 char *newline, *line = NULL;
1478 const int avail = view->ncols;
1480 time_t committer_time;
1481 struct tog_color *tc;
1483 committer_time = got_object_commit_get_committer_time(commit);
1484 if (gmtime_r(&committer_time, &tm) == NULL)
1485 return got_error_from_errno("gmtime_r");
1486 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1487 return got_error(GOT_ERR_NO_SPACE);
1489 if (avail <= date_display_cols)
1490 limit = MIN(sizeof(datebuf) - 1, avail);
1492 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1493 tc = get_color(&s->colors, TOG_COLOR_DATE);
1495 wattr_on(view->window,
1496 COLOR_PAIR(tc->colorpair), NULL);
1497 waddnstr(view->window, datebuf, limit);
1499 wattr_off(view->window,
1500 COLOR_PAIR(tc->colorpair), NULL);
1507 err = got_object_id_str(&id_str, id);
1510 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1512 wattr_on(view->window,
1513 COLOR_PAIR(tc->colorpair), NULL);
1514 wprintw(view->window, "%.8s ", id_str);
1516 wattr_off(view->window,
1517 COLOR_PAIR(tc->colorpair), NULL);
1524 author = strdup(got_object_commit_get_author(commit));
1525 if (author == NULL) {
1526 err = got_error_from_errno("strdup");
1529 err = format_author(&wauthor, &author_width, author, avail - col, col);
1532 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1534 wattr_on(view->window,
1535 COLOR_PAIR(tc->colorpair), NULL);
1536 waddwstr(view->window, wauthor);
1538 wattr_off(view->window,
1539 COLOR_PAIR(tc->colorpair), NULL);
1540 col += author_width;
1541 while (col < avail && author_width < author_display_cols + 2) {
1542 waddch(view->window, ' ');
1549 err = got_object_commit_get_logmsg(&logmsg0, commit);
1553 while (*logmsg == '\n')
1555 newline = strchr(logmsg, '\n');
1558 limit = view->x + avail - col;
1559 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col, 1);
1562 err = scroll_wline(&scrolled_wline, wlogmsg, view->x, col);
1565 waddwstr(view->window, scrolled_wline);
1566 logmsg_width = wcswidth(scrolled_wline, wcslen(scrolled_wline));
1567 col += MAX(logmsg_width, 0);
1568 while (col < avail) {
1569 waddch(view->window, ' ');
1581 static struct commit_queue_entry *
1582 alloc_commit_queue_entry(struct got_commit_object *commit,
1583 struct got_object_id *id)
1585 struct commit_queue_entry *entry;
1587 entry = calloc(1, sizeof(*entry));
1592 entry->commit = commit;
1597 pop_commit(struct commit_queue *commits)
1599 struct commit_queue_entry *entry;
1601 entry = TAILQ_FIRST(&commits->head);
1602 TAILQ_REMOVE(&commits->head, entry, entry);
1603 got_object_commit_close(entry->commit);
1604 commits->ncommits--;
1605 /* Don't free entry->id! It is owned by the commit graph. */
1610 free_commits(struct commit_queue *commits)
1612 while (!TAILQ_EMPTY(&commits->head))
1613 pop_commit(commits);
1616 static const struct got_error *
1617 match_commit(int *have_match, struct got_object_id *id,
1618 struct got_commit_object *commit, regex_t *regex)
1620 const struct got_error *err = NULL;
1621 regmatch_t regmatch;
1622 char *id_str = NULL, *logmsg = NULL;
1626 err = got_object_id_str(&id_str, id);
1630 err = got_object_commit_get_logmsg(&logmsg, commit);
1634 if (regexec(regex, got_object_commit_get_author(commit), 1,
1635 ®match, 0) == 0 ||
1636 regexec(regex, got_object_commit_get_committer(commit), 1,
1637 ®match, 0) == 0 ||
1638 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1639 regexec(regex, logmsg, 1, ®match, 0) == 0)
1647 static const struct got_error *
1648 queue_commits(struct tog_log_thread_args *a)
1650 const struct got_error *err = NULL;
1653 * We keep all commits open throughout the lifetime of the log
1654 * view in order to avoid having to re-fetch commits from disk
1655 * while updating the display.
1658 struct got_object_id *id;
1659 struct got_commit_object *commit;
1660 struct commit_queue_entry *entry;
1663 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1665 if (err || id == NULL)
1668 err = got_object_open_as_commit(&commit, a->repo, id);
1671 entry = alloc_commit_queue_entry(commit, id);
1672 if (entry == NULL) {
1673 err = got_error_from_errno("alloc_commit_queue_entry");
1677 errcode = pthread_mutex_lock(&tog_mutex);
1679 err = got_error_set_errno(errcode,
1680 "pthread_mutex_lock");
1684 entry->idx = a->commits->ncommits;
1685 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1686 a->commits->ncommits++;
1688 if (*a->searching == TOG_SEARCH_FORWARD &&
1689 !*a->search_next_done) {
1691 err = match_commit(&have_match, id, commit, a->regex);
1695 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1698 errcode = pthread_mutex_unlock(&tog_mutex);
1699 if (errcode && err == NULL)
1700 err = got_error_set_errno(errcode,
1701 "pthread_mutex_unlock");
1704 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1710 select_commit(struct tog_log_view_state *s)
1712 struct commit_queue_entry *entry;
1715 entry = s->first_displayed_entry;
1717 if (ncommits == s->selected) {
1718 s->selected_entry = entry;
1721 entry = TAILQ_NEXT(entry, entry);
1726 static const struct got_error *
1727 draw_commits(struct tog_view *view)
1729 const struct got_error *err = NULL;
1730 struct tog_log_view_state *s = &view->state.log;
1731 struct commit_queue_entry *entry = s->selected_entry;
1732 const int limit = view->nlines;
1734 int ncommits, author_cols = 4;
1735 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1736 char *refs_str = NULL;
1738 struct tog_color *tc;
1739 static const size_t date_display_cols = 12;
1741 if (s->selected_entry &&
1742 !(view->searching && view->search_next_done == 0)) {
1743 struct got_reflist_head *refs;
1744 err = got_object_id_str(&id_str, s->selected_entry->id);
1747 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1748 s->selected_entry->id);
1750 err = build_refs_str(&refs_str, refs,
1751 s->selected_entry->id, s->repo);
1757 if (s->thread_args.commits_needed == 0)
1758 halfdelay(10); /* disable fast refresh */
1760 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1761 if (asprintf(&ncommits_str, " [%d/%d] %s",
1762 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1763 (view->searching && !view->search_next_done) ?
1764 "searching..." : "loading...") == -1) {
1765 err = got_error_from_errno("asprintf");
1769 const char *search_str = NULL;
1771 if (view->searching) {
1772 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1773 search_str = "no more matches";
1774 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1775 search_str = "no matches found";
1776 else if (!view->search_next_done)
1777 search_str = "searching...";
1780 if (asprintf(&ncommits_str, " [%d/%d] %s",
1781 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1782 search_str ? search_str :
1783 (refs_str ? refs_str : "")) == -1) {
1784 err = got_error_from_errno("asprintf");
1789 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1790 if (asprintf(&header, "commit %s %s%s",
1791 id_str ? id_str : "........................................",
1792 s->in_repo_path, ncommits_str) == -1) {
1793 err = got_error_from_errno("asprintf");
1797 } else if (asprintf(&header, "commit %s%s",
1798 id_str ? id_str : "........................................",
1799 ncommits_str) == -1) {
1800 err = got_error_from_errno("asprintf");
1804 err = format_line(&wline, &width, header, view->ncols, 0, 0);
1808 werase(view->window);
1810 if (view_needs_focus_indication(view))
1811 wstandout(view->window);
1812 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1814 wattr_on(view->window,
1815 COLOR_PAIR(tc->colorpair), NULL);
1816 waddwstr(view->window, wline);
1818 wattr_off(view->window,
1819 COLOR_PAIR(tc->colorpair), NULL);
1820 while (width < view->ncols) {
1821 waddch(view->window, ' ');
1824 if (view_needs_focus_indication(view))
1825 wstandend(view->window);
1830 /* Grow author column size if necessary, and set view->maxx. */
1831 entry = s->first_displayed_entry;
1835 char *author, *eol, *msg, *msg0;
1836 wchar_t *wauthor, *wmsg;
1838 if (ncommits >= limit - 1)
1840 author = strdup(got_object_commit_get_author(entry->commit));
1841 if (author == NULL) {
1842 err = got_error_from_errno("strdup");
1845 err = format_author(&wauthor, &width, author, COLS,
1847 if (author_cols < width)
1848 author_cols = width;
1851 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1855 while (*msg == '\n')
1857 if ((eol = strchr(msg, '\n')))
1859 err = format_line(&wmsg, &width, msg, INT_MAX,
1860 date_display_cols + author_cols, 0);
1863 view->maxx = MAX(view->maxx, width);
1867 entry = TAILQ_NEXT(entry, entry);
1870 entry = s->first_displayed_entry;
1871 s->last_displayed_entry = s->first_displayed_entry;
1874 if (ncommits >= limit - 1)
1876 if (ncommits == s->selected)
1877 wstandout(view->window);
1878 err = draw_commit(view, entry->commit, entry->id,
1879 date_display_cols, author_cols);
1880 if (ncommits == s->selected)
1881 wstandend(view->window);
1885 s->last_displayed_entry = entry;
1886 entry = TAILQ_NEXT(entry, entry);
1899 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1901 struct commit_queue_entry *entry;
1904 entry = TAILQ_FIRST(&s->commits.head);
1905 if (s->first_displayed_entry == entry)
1908 entry = s->first_displayed_entry;
1909 while (entry && nscrolled < maxscroll) {
1910 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1912 s->first_displayed_entry = entry;
1918 static const struct got_error *
1919 trigger_log_thread(struct tog_view *view, int wait)
1921 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1924 halfdelay(1); /* fast refresh while loading commits */
1926 while (ta->commits_needed > 0 || ta->load_all) {
1927 if (ta->log_complete)
1930 /* Wake the log thread. */
1931 errcode = pthread_cond_signal(&ta->need_commits);
1933 return got_error_set_errno(errcode,
1934 "pthread_cond_signal");
1937 * The mutex will be released while the view loop waits
1938 * in wgetch(), at which time the log thread will run.
1943 /* Display progress update in log view. */
1944 show_log_view(view);
1948 /* Wait right here while next commit is being loaded. */
1949 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1951 return got_error_set_errno(errcode,
1952 "pthread_cond_wait");
1954 /* Display progress update in log view. */
1955 show_log_view(view);
1963 static const struct got_error *
1964 log_scroll_down(struct tog_view *view, int maxscroll)
1966 struct tog_log_view_state *s = &view->state.log;
1967 const struct got_error *err = NULL;
1968 struct commit_queue_entry *pentry;
1969 int nscrolled = 0, ncommits_needed;
1971 if (s->last_displayed_entry == NULL)
1974 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1975 if (s->commits.ncommits < ncommits_needed &&
1976 !s->thread_args.log_complete) {
1978 * Ask the log thread for required amount of commits.
1980 s->thread_args.commits_needed += maxscroll;
1981 err = trigger_log_thread(view, 1);
1987 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1991 s->last_displayed_entry = pentry;
1993 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1996 s->first_displayed_entry = pentry;
1997 } while (++nscrolled < maxscroll);
2002 static const struct got_error *
2003 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2004 struct got_commit_object *commit, struct got_object_id *commit_id,
2005 struct tog_view *log_view, struct got_repository *repo)
2007 const struct got_error *err;
2008 struct got_object_qid *parent_id;
2009 struct tog_view *diff_view;
2011 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2012 if (diff_view == NULL)
2013 return got_error_from_errno("view_open");
2015 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2016 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2017 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2019 *new_view = diff_view;
2023 static const struct got_error *
2024 tree_view_visit_subtree(struct tog_tree_view_state *s,
2025 struct got_tree_object *subtree)
2027 struct tog_parent_tree *parent;
2029 parent = calloc(1, sizeof(*parent));
2031 return got_error_from_errno("calloc");
2033 parent->tree = s->tree;
2034 parent->first_displayed_entry = s->first_displayed_entry;
2035 parent->selected_entry = s->selected_entry;
2036 parent->selected = s->selected;
2037 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2040 s->first_displayed_entry = NULL;
2044 static const struct got_error *
2045 tree_view_walk_path(struct tog_tree_view_state *s,
2046 struct got_commit_object *commit, const char *path)
2048 const struct got_error *err = NULL;
2049 struct got_tree_object *tree = NULL;
2051 char *slash, *subpath = NULL;
2053 /* Walk the path and open corresponding tree objects. */
2056 struct got_tree_entry *te;
2057 struct got_object_id *tree_id;
2063 /* Ensure the correct subtree entry is selected. */
2064 slash = strchr(p, '/');
2066 te_name = strdup(p);
2068 te_name = strndup(p, slash - p);
2069 if (te_name == NULL) {
2070 err = got_error_from_errno("strndup");
2073 te = got_object_tree_find_entry(s->tree, te_name);
2075 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2080 s->first_displayed_entry = s->selected_entry = te;
2082 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2083 break; /* jump to this file's entry */
2085 slash = strchr(p, '/');
2087 subpath = strndup(path, slash - path);
2089 subpath = strdup(path);
2090 if (subpath == NULL) {
2091 err = got_error_from_errno("strdup");
2095 err = got_object_id_by_path(&tree_id, s->repo, commit,
2100 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2105 err = tree_view_visit_subtree(s, tree);
2107 got_object_tree_close(tree);
2121 static const struct got_error *
2122 browse_commit_tree(struct tog_view **new_view, int begin_x,
2123 struct commit_queue_entry *entry, const char *path,
2124 const char *head_ref_name, struct got_repository *repo)
2126 const struct got_error *err = NULL;
2127 struct tog_tree_view_state *s;
2128 struct tog_view *tree_view;
2130 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2131 if (tree_view == NULL)
2132 return got_error_from_errno("view_open");
2134 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2137 s = &tree_view->state.tree;
2139 *new_view = tree_view;
2141 if (got_path_is_root_dir(path))
2144 return tree_view_walk_path(s, entry->commit, path);
2147 static const struct got_error *
2148 block_signals_used_by_main_thread(void)
2153 if (sigemptyset(&sigset) == -1)
2154 return got_error_from_errno("sigemptyset");
2156 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2157 if (sigaddset(&sigset, SIGWINCH) == -1)
2158 return got_error_from_errno("sigaddset");
2159 if (sigaddset(&sigset, SIGCONT) == -1)
2160 return got_error_from_errno("sigaddset");
2161 if (sigaddset(&sigset, SIGINT) == -1)
2162 return got_error_from_errno("sigaddset");
2163 if (sigaddset(&sigset, SIGTERM) == -1)
2164 return got_error_from_errno("sigaddset");
2166 /* ncurses handles SIGTSTP */
2167 if (sigaddset(&sigset, SIGTSTP) == -1)
2168 return got_error_from_errno("sigaddset");
2170 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2172 return got_error_set_errno(errcode, "pthread_sigmask");
2178 log_thread(void *arg)
2180 const struct got_error *err = NULL;
2182 struct tog_log_thread_args *a = arg;
2185 err = block_signals_used_by_main_thread();
2189 while (!done && !err && !tog_fatal_signal_received()) {
2190 err = queue_commits(a);
2192 if (err->code != GOT_ERR_ITER_COMPLETED)
2196 } else if (a->commits_needed > 0 && !a->load_all)
2197 a->commits_needed--;
2199 errcode = pthread_mutex_lock(&tog_mutex);
2201 err = got_error_set_errno(errcode,
2202 "pthread_mutex_lock");
2204 } else if (*a->quit)
2206 else if (*a->first_displayed_entry == NULL) {
2207 *a->first_displayed_entry =
2208 TAILQ_FIRST(&a->commits->head);
2209 *a->selected_entry = *a->first_displayed_entry;
2212 errcode = pthread_cond_signal(&a->commit_loaded);
2214 err = got_error_set_errno(errcode,
2215 "pthread_cond_signal");
2216 pthread_mutex_unlock(&tog_mutex);
2221 a->commits_needed = 0;
2223 if (a->commits_needed == 0 && !a->load_all) {
2224 errcode = pthread_cond_wait(&a->need_commits,
2227 err = got_error_set_errno(errcode,
2228 "pthread_cond_wait");
2234 errcode = pthread_mutex_unlock(&tog_mutex);
2235 if (errcode && err == NULL)
2236 err = got_error_set_errno(errcode,
2237 "pthread_mutex_unlock");
2239 a->log_complete = 1;
2243 static const struct got_error *
2244 stop_log_thread(struct tog_log_view_state *s)
2246 const struct got_error *err = NULL;
2251 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2253 return got_error_set_errno(errcode,
2254 "pthread_cond_signal");
2255 errcode = pthread_mutex_unlock(&tog_mutex);
2257 return got_error_set_errno(errcode,
2258 "pthread_mutex_unlock");
2259 errcode = pthread_join(s->thread, (void **)&err);
2261 return got_error_set_errno(errcode, "pthread_join");
2262 errcode = pthread_mutex_lock(&tog_mutex);
2264 return got_error_set_errno(errcode,
2265 "pthread_mutex_lock");
2269 if (s->thread_args.repo) {
2270 err = got_repo_close(s->thread_args.repo);
2271 s->thread_args.repo = NULL;
2274 if (s->thread_args.pack_fds) {
2275 const struct got_error *pack_err =
2276 got_repo_pack_fds_close(s->thread_args.pack_fds);
2279 s->thread_args.pack_fds = NULL;
2282 if (s->thread_args.graph) {
2283 got_commit_graph_close(s->thread_args.graph);
2284 s->thread_args.graph = NULL;
2290 static const struct got_error *
2291 close_log_view(struct tog_view *view)
2293 const struct got_error *err = NULL;
2294 struct tog_log_view_state *s = &view->state.log;
2297 err = stop_log_thread(s);
2299 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2300 if (errcode && err == NULL)
2301 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2303 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2304 if (errcode && err == NULL)
2305 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2307 free_commits(&s->commits);
2308 free(s->in_repo_path);
2309 s->in_repo_path = NULL;
2312 free(s->head_ref_name);
2313 s->head_ref_name = NULL;
2317 static const struct got_error *
2318 search_start_log_view(struct tog_view *view)
2320 struct tog_log_view_state *s = &view->state.log;
2322 s->matched_entry = NULL;
2323 s->search_entry = NULL;
2327 static const struct got_error *
2328 search_next_log_view(struct tog_view *view)
2330 const struct got_error *err = NULL;
2331 struct tog_log_view_state *s = &view->state.log;
2332 struct commit_queue_entry *entry;
2334 /* Display progress update in log view. */
2335 show_log_view(view);
2339 if (s->search_entry) {
2341 errcode = pthread_mutex_unlock(&tog_mutex);
2343 return got_error_set_errno(errcode,
2344 "pthread_mutex_unlock");
2345 ch = wgetch(view->window);
2346 errcode = pthread_mutex_lock(&tog_mutex);
2348 return got_error_set_errno(errcode,
2349 "pthread_mutex_lock");
2350 if (ch == KEY_BACKSPACE) {
2351 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2354 if (view->searching == TOG_SEARCH_FORWARD)
2355 entry = TAILQ_NEXT(s->search_entry, entry);
2357 entry = TAILQ_PREV(s->search_entry,
2358 commit_queue_head, entry);
2359 } else if (s->matched_entry) {
2360 if (view->searching == TOG_SEARCH_FORWARD)
2361 entry = TAILQ_NEXT(s->matched_entry, entry);
2363 entry = TAILQ_PREV(s->matched_entry,
2364 commit_queue_head, entry);
2366 entry = s->selected_entry;
2372 if (entry == NULL) {
2373 if (s->thread_args.log_complete ||
2374 view->searching == TOG_SEARCH_BACKWARD) {
2375 view->search_next_done =
2376 (s->matched_entry == NULL ?
2377 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2378 s->search_entry = NULL;
2382 * Poke the log thread for more commits and return,
2383 * allowing the main loop to make progress. Search
2384 * will resume at s->search_entry once we come back.
2386 s->thread_args.commits_needed++;
2387 return trigger_log_thread(view, 0);
2390 err = match_commit(&have_match, entry->id, entry->commit,
2395 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2396 s->matched_entry = entry;
2400 s->search_entry = entry;
2401 if (view->searching == TOG_SEARCH_FORWARD)
2402 entry = TAILQ_NEXT(entry, entry);
2404 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2407 if (s->matched_entry) {
2408 int cur = s->selected_entry->idx;
2409 while (cur < s->matched_entry->idx) {
2410 err = input_log_view(NULL, view, KEY_DOWN);
2415 while (cur > s->matched_entry->idx) {
2416 err = input_log_view(NULL, view, KEY_UP);
2423 s->search_entry = NULL;
2428 static const struct got_error *
2429 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2430 struct got_repository *repo, const char *head_ref_name,
2431 const char *in_repo_path, int log_branches)
2433 const struct got_error *err = NULL;
2434 struct tog_log_view_state *s = &view->state.log;
2435 struct got_repository *thread_repo = NULL;
2436 struct got_commit_graph *thread_graph = NULL;
2439 if (in_repo_path != s->in_repo_path) {
2440 free(s->in_repo_path);
2441 s->in_repo_path = strdup(in_repo_path);
2442 if (s->in_repo_path == NULL)
2443 return got_error_from_errno("strdup");
2446 /* The commit queue only contains commits being displayed. */
2447 TAILQ_INIT(&s->commits.head);
2448 s->commits.ncommits = 0;
2451 if (head_ref_name) {
2452 s->head_ref_name = strdup(head_ref_name);
2453 if (s->head_ref_name == NULL) {
2454 err = got_error_from_errno("strdup");
2458 s->start_id = got_object_id_dup(start_id);
2459 if (s->start_id == NULL) {
2460 err = got_error_from_errno("got_object_id_dup");
2463 s->log_branches = log_branches;
2465 STAILQ_INIT(&s->colors);
2466 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2467 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2468 get_color_value("TOG_COLOR_COMMIT"));
2471 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2472 get_color_value("TOG_COLOR_AUTHOR"));
2474 free_colors(&s->colors);
2477 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2478 get_color_value("TOG_COLOR_DATE"));
2480 free_colors(&s->colors);
2485 view->show = show_log_view;
2486 view->input = input_log_view;
2487 view->close = close_log_view;
2488 view->search_start = search_start_log_view;
2489 view->search_next = search_next_log_view;
2491 if (s->thread_args.pack_fds == NULL) {
2492 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2496 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2497 s->thread_args.pack_fds);
2500 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2504 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2505 s->repo, NULL, NULL);
2509 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2511 err = got_error_set_errno(errcode, "pthread_cond_init");
2514 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2516 err = got_error_set_errno(errcode, "pthread_cond_init");
2520 s->thread_args.commits_needed = view->nlines;
2521 s->thread_args.graph = thread_graph;
2522 s->thread_args.commits = &s->commits;
2523 s->thread_args.in_repo_path = s->in_repo_path;
2524 s->thread_args.start_id = s->start_id;
2525 s->thread_args.repo = thread_repo;
2526 s->thread_args.log_complete = 0;
2527 s->thread_args.quit = &s->quit;
2528 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2529 s->thread_args.selected_entry = &s->selected_entry;
2530 s->thread_args.searching = &view->searching;
2531 s->thread_args.search_next_done = &view->search_next_done;
2532 s->thread_args.regex = &view->regex;
2535 close_log_view(view);
2539 static const struct got_error *
2540 show_log_view(struct tog_view *view)
2542 const struct got_error *err;
2543 struct tog_log_view_state *s = &view->state.log;
2545 if (s->thread == NULL) {
2546 int errcode = pthread_create(&s->thread, NULL, log_thread,
2549 return got_error_set_errno(errcode, "pthread_create");
2550 if (s->thread_args.commits_needed > 0) {
2551 err = trigger_log_thread(view, 1);
2557 return draw_commits(view);
2560 static const struct got_error *
2561 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2563 const struct got_error *err = NULL;
2564 struct tog_log_view_state *s = &view->state.log;
2565 struct tog_view *diff_view = NULL, *tree_view = NULL;
2566 struct tog_view *ref_view = NULL;
2567 struct commit_queue_entry *entry;
2568 int begin_x = 0, n, nscroll = view->nlines - 1;
2570 if (s->thread_args.load_all) {
2571 if (ch == KEY_BACKSPACE)
2572 s->thread_args.load_all = 0;
2573 else if (s->thread_args.log_complete) {
2574 s->thread_args.load_all = 0;
2575 log_scroll_down(view, s->commits.ncommits);
2576 s->selected = MIN(view->nlines - 2,
2577 s->commits.ncommits - 1);
2591 view->x = MAX(view->maxx - view->ncols / 2, 0);
2595 if (view->x + view->ncols / 2 < view->maxx)
2596 view->x += 2; /* move two columns right */
2600 view->x -= MIN(view->x, 2); /* move two columns back */
2607 if (s->first_displayed_entry == NULL)
2609 if (s->selected > 0)
2612 log_scroll_up(s, 1);
2618 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2627 if (s->first_displayed_entry == NULL)
2629 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2630 s->selected = MAX(0, s->selected - nscroll - 1);
2632 log_scroll_up(s, nscroll);
2640 if (s->first_displayed_entry == NULL)
2642 if (s->selected < MIN(view->nlines - 2,
2643 s->commits.ncommits - 1))
2646 err = log_scroll_down(view, 1);
2654 /* We don't know yet how many commits, so we're forced to
2655 * traverse them all. */
2656 if (!s->thread_args.log_complete) {
2657 s->thread_args.load_all = 1;
2658 return trigger_log_thread(view, 0);
2662 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2663 for (n = 0; n < view->nlines - 1; n++) {
2666 s->first_displayed_entry = entry;
2667 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2670 s->selected = n - 1;
2680 struct commit_queue_entry *first;
2681 first = s->first_displayed_entry;
2684 err = log_scroll_down(view, nscroll);
2687 if (first == s->first_displayed_entry &&
2688 s->selected < MIN(view->nlines - 2,
2689 s->commits.ncommits - 1)) {
2690 /* can't scroll further down */
2691 s->selected += MIN(s->last_displayed_entry->idx -
2692 s->selected_entry->idx, nscroll + 1);
2698 if (s->selected > view->nlines - 2)
2699 s->selected = view->nlines - 2;
2700 if (s->selected > s->commits.ncommits - 1)
2701 s->selected = s->commits.ncommits - 1;
2703 if (s->commits.ncommits < view->nlines - 1 &&
2704 !s->thread_args.log_complete) {
2705 s->thread_args.commits_needed += (view->nlines - 1) -
2706 s->commits.ncommits;
2707 err = trigger_log_thread(view, 1);
2713 if (s->selected_entry == NULL)
2715 if (view_is_parent_view(view))
2716 begin_x = view_split_begin_x(view->begin_x);
2717 err = open_diff_view_for_commit(&diff_view, begin_x,
2718 s->selected_entry->commit, s->selected_entry->id,
2723 diff_view->focussed = 1;
2724 if (view_is_parent_view(view)) {
2725 err = view_close_child(view);
2728 view_set_child(view, diff_view);
2729 view->focus_child = 1;
2731 *new_view = diff_view;
2734 if (s->selected_entry == NULL)
2736 if (view_is_parent_view(view))
2737 begin_x = view_split_begin_x(view->begin_x);
2738 err = browse_commit_tree(&tree_view, begin_x,
2739 s->selected_entry, s->in_repo_path, s->head_ref_name,
2744 tree_view->focussed = 1;
2745 if (view_is_parent_view(view)) {
2746 err = view_close_child(view);
2749 view_set_child(view, tree_view);
2750 view->focus_child = 1;
2752 *new_view = tree_view;
2757 if (ch == KEY_BACKSPACE &&
2758 got_path_is_root_dir(s->in_repo_path))
2760 err = stop_log_thread(s);
2763 if (ch == KEY_BACKSPACE) {
2765 err = got_path_dirname(&parent_path, s->in_repo_path);
2768 free(s->in_repo_path);
2769 s->in_repo_path = parent_path;
2770 s->thread_args.in_repo_path = s->in_repo_path;
2771 } else if (ch == CTRL('l')) {
2772 struct got_object_id *start_id;
2773 err = got_repo_match_object_id(&start_id, NULL,
2774 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2775 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2779 s->start_id = start_id;
2780 s->thread_args.start_id = s->start_id;
2782 s->log_branches = !s->log_branches;
2784 err = got_repo_open(&s->thread_args.repo,
2785 got_repo_get_path(s->repo), NULL,
2786 s->thread_args.pack_fds);
2790 err = tog_load_refs(s->repo, 0);
2793 err = got_commit_graph_open(&s->thread_args.graph,
2794 s->in_repo_path, !s->log_branches);
2797 err = got_commit_graph_iter_start(s->thread_args.graph,
2798 s->start_id, s->repo, NULL, NULL);
2801 free_commits(&s->commits);
2802 s->first_displayed_entry = NULL;
2803 s->last_displayed_entry = NULL;
2804 s->selected_entry = NULL;
2806 s->thread_args.log_complete = 0;
2808 s->thread_args.commits_needed = view->nlines;
2811 if (view_is_parent_view(view))
2812 begin_x = view_split_begin_x(view->begin_x);
2813 ref_view = view_open(view->nlines, view->ncols,
2814 view->begin_y, begin_x, TOG_VIEW_REF);
2815 if (ref_view == NULL)
2816 return got_error_from_errno("view_open");
2817 err = open_ref_view(ref_view, s->repo);
2819 view_close(ref_view);
2823 ref_view->focussed = 1;
2824 if (view_is_parent_view(view)) {
2825 err = view_close_child(view);
2828 view_set_child(view, ref_view);
2829 view->focus_child = 1;
2831 *new_view = ref_view;
2840 static const struct got_error *
2841 apply_unveil(const char *repo_path, const char *worktree_path)
2843 const struct got_error *error;
2846 if (unveil("gmon.out", "rwc") != 0)
2847 return got_error_from_errno2("unveil", "gmon.out");
2849 if (repo_path && unveil(repo_path, "r") != 0)
2850 return got_error_from_errno2("unveil", repo_path);
2852 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2853 return got_error_from_errno2("unveil", worktree_path);
2855 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2856 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2858 error = got_privsep_unveil_exec_helpers();
2862 if (unveil(NULL, NULL) != 0)
2863 return got_error_from_errno("unveil");
2872 * Override default signal handlers before starting ncurses.
2873 * This should prevent ncurses from installing its own
2874 * broken cleanup() signal handler.
2876 signal(SIGWINCH, tog_sigwinch);
2877 signal(SIGPIPE, tog_sigpipe);
2878 signal(SIGCONT, tog_sigcont);
2879 signal(SIGINT, tog_sigint);
2880 signal(SIGTERM, tog_sigterm);
2884 halfdelay(1); /* Do fast refresh while initial view is loading. */
2887 intrflush(stdscr, FALSE);
2888 keypad(stdscr, TRUE);
2890 if (getenv("TOG_COLORS") != NULL) {
2892 use_default_colors();
2896 static const struct got_error *
2897 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2898 struct got_repository *repo, struct got_worktree *worktree)
2900 const struct got_error *err = NULL;
2903 *in_repo_path = strdup("/");
2904 if (*in_repo_path == NULL)
2905 return got_error_from_errno("strdup");
2910 const char *prefix = got_worktree_get_path_prefix(worktree);
2913 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2916 if (asprintf(in_repo_path, "%s%s%s", prefix,
2917 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2919 err = got_error_from_errno("asprintf");
2920 *in_repo_path = NULL;
2924 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2929 static const struct got_error *
2930 cmd_log(int argc, char *argv[])
2932 const struct got_error *error;
2933 struct got_repository *repo = NULL;
2934 struct got_worktree *worktree = NULL;
2935 struct got_object_id *start_id = NULL;
2936 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2937 char *start_commit = NULL, *label = NULL;
2938 struct got_reference *ref = NULL;
2939 const char *head_ref_name = NULL;
2940 int ch, log_branches = 0;
2941 struct tog_view *view;
2942 int *pack_fds = NULL;
2944 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2950 start_commit = optarg;
2953 repo_path = realpath(optarg, NULL);
2954 if (repo_path == NULL)
2955 return got_error_from_errno2("realpath",
2970 error = got_repo_pack_fds_open(&pack_fds);
2974 if (repo_path == NULL) {
2975 cwd = getcwd(NULL, 0);
2977 return got_error_from_errno("getcwd");
2978 error = got_worktree_open(&worktree, cwd);
2979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2983 strdup(got_worktree_get_repo_path(worktree));
2985 repo_path = strdup(cwd);
2986 if (repo_path == NULL) {
2987 error = got_error_from_errno("strdup");
2992 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2996 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3003 error = apply_unveil(got_repo_get_path(repo),
3004 worktree ? got_worktree_get_root_path(worktree) : NULL);
3008 /* already loaded by tog_log_with_path()? */
3009 if (TAILQ_EMPTY(&tog_refs)) {
3010 error = tog_load_refs(repo, 0);
3015 if (start_commit == NULL) {
3016 error = got_repo_match_object_id(&start_id, &label,
3017 worktree ? got_worktree_get_head_ref_name(worktree) :
3018 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3021 head_ref_name = label;
3023 error = got_ref_open(&ref, repo, start_commit, 0);
3025 head_ref_name = got_ref_get_name(ref);
3026 else if (error->code != GOT_ERR_NOT_REF)
3028 error = got_repo_match_object_id(&start_id, NULL,
3029 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3034 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3036 error = got_error_from_errno("view_open");
3039 error = open_log_view(view, start_id, repo, head_ref_name,
3040 in_repo_path, log_branches);
3044 /* Release work tree lock. */
3045 got_worktree_close(worktree);
3048 error = view_loop(view);
3058 const struct got_error *close_err = got_repo_close(repo);
3063 got_worktree_close(worktree);
3065 const struct got_error *pack_err =
3066 got_repo_pack_fds_close(pack_fds);
3078 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3079 "[-w] object1 object2\n", getprogname());
3084 match_line(const char *line, regex_t *regex, size_t nmatch,
3085 regmatch_t *regmatch)
3087 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3091 match_color(struct tog_colors *colors, const char *line)
3093 struct tog_color *tc = NULL;
3095 STAILQ_FOREACH(tc, colors, entry) {
3096 if (match_line(line, &tc->regex, 0, NULL))
3103 static const struct got_error *
3104 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3105 WINDOW *window, int skip, regmatch_t *regmatch)
3107 const struct got_error *err = NULL;
3109 int rme, rms, n, width;
3112 rms = regmatch->rm_so;
3113 rme = regmatch->rm_eo;
3115 err = format_line(&wline, &width, line, wlimit + skip,
3120 /* draw up to matched token if we haven't scrolled past it */
3121 n = MAX(rms - skip, 0);
3123 waddnwstr(window, wline + skip, n);
3129 int len = rme - rms;
3133 len = MAX(len - n, 0);
3135 /* draw (visible part of) matched token (if scrolled into it) */
3137 wattron(window, A_STANDOUT);
3138 waddnwstr(window, wline + rms + n, len);
3139 wattroff(window, A_STANDOUT);
3145 if (wlimit > 0 && skip < width) { /* draw rest of line */
3148 n = MIN(skip - rme, width - rme);
3149 waddnwstr(window, wline + rme + n, wlimit);
3157 static const struct got_error *
3158 draw_file(struct tog_view *view, const char *header)
3160 struct tog_diff_view_state *s = &view->state.diff;
3161 regmatch_t *regmatch = &view->regmatch;
3162 const struct got_error *err;
3165 size_t linesize = 0;
3167 struct tog_color *tc;
3170 int max_lines = view->nlines;
3171 int nlines = s->nlines;
3174 line_offset = s->line_offsets[s->first_displayed_line - 1];
3175 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3176 return got_error_from_errno("fseek");
3178 werase(view->window);
3181 if (asprintf(&line, "[%d/%d] %s",
3182 s->first_displayed_line - 1 + s->selected_line, nlines,
3184 return got_error_from_errno("asprintf");
3185 err = format_line(&wline, &width, line, view->ncols, 0, 0);
3190 if (view_needs_focus_indication(view))
3191 wstandout(view->window);
3192 waddwstr(view->window, wline);
3195 if (view_needs_focus_indication(view))
3196 wstandend(view->window);
3197 if (width <= view->ncols - 1)
3198 waddch(view->window, '\n');
3208 while (max_lines > 0 && nprinted < max_lines) {
3209 linelen = getline(&line, &linesize, s->f);
3210 if (linelen == -1) {
3216 return got_ferror(s->f, GOT_ERR_IO);
3219 view->maxx = MAX(view->maxx, linelen);
3221 tc = match_color(&s->colors, line);
3223 wattr_on(view->window,
3224 COLOR_PAIR(tc->colorpair), NULL);
3225 if (s->first_displayed_line + nprinted == s->matched_line &&
3226 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3227 err = add_matched_line(&width, line, view->ncols, 0,
3228 view->window, view->x, regmatch);
3234 err = format_line(&wline, &width, line,
3235 view->x + view->ncols, 0, view->x ? 1 : 0);
3240 if (view->x < width - 1)
3241 waddwstr(view->window, wline + view->x);
3246 wattr_off(view->window,
3247 COLOR_PAIR(tc->colorpair), NULL);
3248 if (width - view->x <= view->ncols - 1)
3249 waddch(view->window, '\n');
3254 s->last_displayed_line = s->first_displayed_line +
3257 s->last_displayed_line = s->first_displayed_line;
3262 while (nprinted < view->nlines) {
3263 waddch(view->window, '\n');
3267 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols,
3273 wstandout(view->window);
3274 waddwstr(view->window, wline);
3277 wstandend(view->window);
3284 get_datestr(time_t *time, char *datebuf)
3286 struct tm mytm, *tm;
3289 tm = gmtime_r(time, &mytm);
3292 s = asctime_r(tm, datebuf);
3295 p = strchr(s, '\n');
3301 static const struct got_error *
3302 get_changed_paths(struct got_pathlist_head *paths,
3303 struct got_commit_object *commit, struct got_repository *repo)
3305 const struct got_error *err = NULL;
3306 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3307 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3308 struct got_object_qid *qid;
3310 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3312 struct got_commit_object *pcommit;
3313 err = got_object_open_as_commit(&pcommit, repo,
3318 tree_id1 = got_object_id_dup(
3319 got_object_commit_get_tree_id(pcommit));
3320 if (tree_id1 == NULL) {
3321 got_object_commit_close(pcommit);
3322 return got_error_from_errno("got_object_id_dup");
3324 got_object_commit_close(pcommit);
3329 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3334 tree_id2 = got_object_commit_get_tree_id(commit);
3335 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3339 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3340 got_diff_tree_collect_changed_paths, paths, 0);
3343 got_object_tree_close(tree1);
3345 got_object_tree_close(tree2);
3350 static const struct got_error *
3351 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3355 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3357 return got_error_from_errno("reallocarray");
3359 (*line_offsets)[*nlines] = off;
3364 static const struct got_error *
3365 write_commit_info(off_t **line_offsets, size_t *nlines,
3366 struct got_object_id *commit_id, struct got_reflist_head *refs,
3367 struct got_repository *repo, FILE *outfile)
3369 const struct got_error *err = NULL;
3370 char datebuf[26], *datestr;
3371 struct got_commit_object *commit;
3372 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3373 time_t committer_time;
3374 const char *author, *committer;
3375 char *refs_str = NULL;
3376 struct got_pathlist_head changed_paths;
3377 struct got_pathlist_entry *pe;
3381 TAILQ_INIT(&changed_paths);
3384 err = build_refs_str(&refs_str, refs, commit_id, repo);
3389 err = got_object_open_as_commit(&commit, repo, commit_id);
3393 err = got_object_id_str(&id_str, commit_id);
3395 err = got_error_from_errno("got_object_id_str");
3399 err = add_line_offset(line_offsets, nlines, 0);
3403 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3404 refs_str ? refs_str : "", refs_str ? ")" : "");
3406 err = got_error_from_errno("fprintf");
3410 err = add_line_offset(line_offsets, nlines, outoff);
3414 n = fprintf(outfile, "from: %s\n",
3415 got_object_commit_get_author(commit));
3417 err = got_error_from_errno("fprintf");
3421 err = add_line_offset(line_offsets, nlines, outoff);
3425 committer_time = got_object_commit_get_committer_time(commit);
3426 datestr = get_datestr(&committer_time, datebuf);
3428 n = fprintf(outfile, "date: %s UTC\n", datestr);
3430 err = got_error_from_errno("fprintf");
3434 err = add_line_offset(line_offsets, nlines, outoff);
3438 author = got_object_commit_get_author(commit);
3439 committer = got_object_commit_get_committer(commit);
3440 if (strcmp(author, committer) != 0) {
3441 n = fprintf(outfile, "via: %s\n", committer);
3443 err = got_error_from_errno("fprintf");
3447 err = add_line_offset(line_offsets, nlines, outoff);
3451 if (got_object_commit_get_nparents(commit) > 1) {
3452 const struct got_object_id_queue *parent_ids;
3453 struct got_object_qid *qid;
3455 parent_ids = got_object_commit_get_parent_ids(commit);
3456 STAILQ_FOREACH(qid, parent_ids, entry) {
3457 err = got_object_id_str(&id_str, &qid->id);
3460 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3462 err = got_error_from_errno("fprintf");
3466 err = add_line_offset(line_offsets, nlines, outoff);
3474 err = got_object_commit_get_logmsg(&logmsg, commit);
3478 while ((line = strsep(&s, "\n")) != NULL) {
3479 n = fprintf(outfile, "%s\n", line);
3481 err = got_error_from_errno("fprintf");
3485 err = add_line_offset(line_offsets, nlines, outoff);
3490 err = get_changed_paths(&changed_paths, commit, repo);
3493 TAILQ_FOREACH(pe, &changed_paths, entry) {
3494 struct got_diff_changed_path *cp = pe->data;
3495 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3497 err = got_error_from_errno("fprintf");
3501 err = add_line_offset(line_offsets, nlines, outoff);
3504 free((char *)pe->path);
3508 fputc('\n', outfile);
3510 err = add_line_offset(line_offsets, nlines, outoff);
3512 got_pathlist_free(&changed_paths);
3516 got_object_commit_close(commit);
3518 free(*line_offsets);
3519 *line_offsets = NULL;
3525 static const struct got_error *
3526 create_diff(struct tog_diff_view_state *s)
3528 const struct got_error *err = NULL;
3532 free(s->line_offsets);
3533 s->line_offsets = malloc(sizeof(off_t));
3534 if (s->line_offsets == NULL)
3535 return got_error_from_errno("malloc");
3540 err = got_error_from_errno("got_opentemp");
3543 if (s->f && fclose(s->f) == EOF) {
3544 err = got_error_from_errno("fclose");
3550 err = got_object_get_type(&obj_type, s->repo, s->id1);
3552 err = got_object_get_type(&obj_type, s->repo, s->id2);
3557 case GOT_OBJ_TYPE_BLOB:
3558 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3559 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3560 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3563 case GOT_OBJ_TYPE_TREE:
3564 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3565 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3566 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3568 case GOT_OBJ_TYPE_COMMIT: {
3569 const struct got_object_id_queue *parent_ids;
3570 struct got_object_qid *pid;
3571 struct got_commit_object *commit2;
3572 struct got_reflist_head *refs;
3574 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3577 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3578 /* Show commit info if we're diffing to a parent/root commit. */
3579 if (s->id1 == NULL) {
3580 err = write_commit_info(&s->line_offsets, &s->nlines,
3581 s->id2, refs, s->repo, s->f);
3585 parent_ids = got_object_commit_get_parent_ids(commit2);
3586 STAILQ_FOREACH(pid, parent_ids, entry) {
3587 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3588 err = write_commit_info(
3589 &s->line_offsets, &s->nlines,
3590 s->id2, refs, s->repo, s->f);
3597 got_object_commit_close(commit2);
3599 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3600 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3601 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3605 err = got_error(GOT_ERR_OBJ_TYPE);
3611 if (s->f && fflush(s->f) != 0 && err == NULL)
3612 err = got_error_from_errno("fflush");
3617 diff_view_indicate_progress(struct tog_view *view)
3619 mvwaddstr(view->window, 0, 0, "diffing...");
3624 static const struct got_error *
3625 search_start_diff_view(struct tog_view *view)
3627 struct tog_diff_view_state *s = &view->state.diff;
3629 s->matched_line = 0;
3633 static const struct got_error *
3634 search_next_diff_view(struct tog_view *view)
3636 struct tog_diff_view_state *s = &view->state.diff;
3637 const struct got_error *err = NULL;
3639 char *exstr = NULL, *line = NULL;
3640 size_t linesize = 0;
3643 if (!view->searching) {
3644 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3648 if (s->matched_line) {
3649 if (view->searching == TOG_SEARCH_FORWARD)
3650 lineno = s->matched_line + 1;
3652 lineno = s->matched_line - 1;
3654 lineno = s->first_displayed_line;
3659 if (lineno <= 0 || lineno > s->nlines) {
3660 if (s->matched_line == 0) {
3661 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3665 if (view->searching == TOG_SEARCH_FORWARD)
3671 offset = s->line_offsets[lineno - 1];
3672 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3674 return got_error_from_errno("fseeko");
3676 linelen = getline(&line, &linesize, s->f);
3677 err = expand_tab(&exstr, line);
3680 if (linelen != -1 &&
3681 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3682 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3683 s->matched_line = lineno;
3688 if (view->searching == TOG_SEARCH_FORWARD)
3696 if (s->matched_line) {
3697 s->first_displayed_line = s->matched_line;
3698 s->selected_line = 1;
3704 static const struct got_error *
3705 close_diff_view(struct tog_view *view)
3707 const struct got_error *err = NULL;
3708 struct tog_diff_view_state *s = &view->state.diff;
3714 if (s->f && fclose(s->f) == EOF)
3715 err = got_error_from_errno("fclose");
3717 if (s->f1 && fclose(s->f1) == EOF)
3718 err = got_error_from_errno("fclose");
3720 if (s->f2 && fclose(s->f2) == EOF)
3721 err = got_error_from_errno("fclose");
3723 free_colors(&s->colors);
3724 free(s->line_offsets);
3725 s->line_offsets = NULL;
3730 static const struct got_error *
3731 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3732 struct got_object_id *id2, const char *label1, const char *label2,
3733 int diff_context, int ignore_whitespace, int force_text_diff,
3734 struct tog_view *log_view, struct got_repository *repo)
3736 const struct got_error *err;
3737 struct tog_diff_view_state *s = &view->state.diff;
3739 memset(s, 0, sizeof(*s));
3741 if (id1 != NULL && id2 != NULL) {
3743 err = got_object_get_type(&type1, repo, id1);
3746 err = got_object_get_type(&type2, repo, id2);
3751 return got_error(GOT_ERR_OBJ_TYPE);
3753 s->first_displayed_line = 1;
3754 s->last_displayed_line = view->nlines;
3755 s->selected_line = 1;
3763 s->id1 = got_object_id_dup(id1);
3765 return got_error_from_errno("got_object_id_dup");
3766 s->f1 = got_opentemp();
3767 if (s->f1 == NULL) {
3768 err = got_error_from_errno("got_opentemp");
3774 s->id2 = got_object_id_dup(id2);
3775 if (s->id2 == NULL) {
3776 err = got_error_from_errno("got_object_id_dup");
3780 s->f2 = got_opentemp();
3781 if (s->f2 == NULL) {
3782 err = got_error_from_errno("got_opentemp");
3786 s->first_displayed_line = 1;
3787 s->last_displayed_line = view->nlines;
3788 s->diff_context = diff_context;
3789 s->ignore_whitespace = ignore_whitespace;
3790 s->force_text_diff = force_text_diff;
3791 s->log_view = log_view;
3794 STAILQ_INIT(&s->colors);
3795 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3796 err = add_color(&s->colors,
3797 "^-", TOG_COLOR_DIFF_MINUS,
3798 get_color_value("TOG_COLOR_DIFF_MINUS"));
3801 err = add_color(&s->colors, "^\\+",
3802 TOG_COLOR_DIFF_PLUS,
3803 get_color_value("TOG_COLOR_DIFF_PLUS"));
3806 err = add_color(&s->colors,
3807 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3808 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3812 err = add_color(&s->colors,
3813 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3814 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3815 get_color_value("TOG_COLOR_DIFF_META"));
3819 err = add_color(&s->colors,
3820 "^(from|via): ", TOG_COLOR_AUTHOR,
3821 get_color_value("TOG_COLOR_AUTHOR"));
3825 err = add_color(&s->colors,
3826 "^date: ", TOG_COLOR_DATE,
3827 get_color_value("TOG_COLOR_DATE"));
3832 if (log_view && view_is_splitscreen(view))
3833 show_log_view(log_view); /* draw vborder */
3834 diff_view_indicate_progress(view);
3836 err = create_diff(s);
3838 view->show = show_diff_view;
3839 view->input = input_diff_view;
3840 view->close = close_diff_view;
3841 view->search_start = search_start_diff_view;
3842 view->search_next = search_next_diff_view;
3845 close_diff_view(view);
3849 static const struct got_error *
3850 show_diff_view(struct tog_view *view)
3852 const struct got_error *err;
3853 struct tog_diff_view_state *s = &view->state.diff;
3854 char *id_str1 = NULL, *id_str2, *header;
3855 const char *label1, *label2;
3858 err = got_object_id_str(&id_str1, s->id1);
3861 label1 = s->label1 ? : id_str1;
3863 label1 = "/dev/null";
3865 err = got_object_id_str(&id_str2, s->id2);
3868 label2 = s->label2 ? : id_str2;
3870 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3871 err = got_error_from_errno("asprintf");
3879 err = draw_file(view, header);
3884 static const struct got_error *
3885 set_selected_commit(struct tog_diff_view_state *s,
3886 struct commit_queue_entry *entry)
3888 const struct got_error *err;
3889 const struct got_object_id_queue *parent_ids;
3890 struct got_commit_object *selected_commit;
3891 struct got_object_qid *pid;
3894 s->id2 = got_object_id_dup(entry->id);
3896 return got_error_from_errno("got_object_id_dup");
3898 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3901 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3903 pid = STAILQ_FIRST(parent_ids);
3904 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3905 got_object_commit_close(selected_commit);
3909 static const struct got_error *
3910 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3912 const struct got_error *err = NULL;
3913 struct tog_diff_view_state *s = &view->state.diff;
3914 struct tog_log_view_state *ls;
3915 struct commit_queue_entry *old_selected_entry;
3917 size_t linesize = 0;
3919 int i, nscroll = view->nlines - 1;
3926 view->x = MAX(view->maxx - view->ncols / 3, 0);
3930 if (view->x + view->ncols / 3 < view->maxx)
3931 view->x += 2; /* move two columns right */
3935 view->x -= MIN(view->x, 2); /* move two columns back */
3940 s->force_text_diff = !s->force_text_diff;
3942 s->ignore_whitespace = !s->ignore_whitespace;
3943 wclear(view->window);
3944 s->first_displayed_line = 1;
3945 s->last_displayed_line = view->nlines;
3946 s->matched_line = 0;
3947 diff_view_indicate_progress(view);
3948 err = create_diff(s);
3952 s->first_displayed_line = 1;
3959 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3965 if (s->first_displayed_line > 1)
3966 s->first_displayed_line--;
3974 if (s->first_displayed_line == 1)
3977 while (i++ < nscroll && s->first_displayed_line > 1)
3978 s->first_displayed_line--;
3984 s->first_displayed_line++;
3996 while (!s->eof && i++ < nscroll) {
3997 linelen = getline(&line, &linesize, s->f);
3998 s->first_displayed_line++;
3999 if (linelen == -1) {
4003 err = got_ferror(s->f, GOT_ERR_IO);
4010 if (s->diff_context > 0) {
4012 s->matched_line = 0;
4013 diff_view_indicate_progress(view);
4014 err = create_diff(s);
4015 if (s->first_displayed_line + view->nlines - 1 >
4017 s->first_displayed_line = 1;
4018 s->last_displayed_line = view->nlines;
4023 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4025 s->matched_line = 0;
4026 diff_view_indicate_progress(view);
4027 err = create_diff(s);
4032 if (s->log_view == NULL)
4034 ls = &s->log_view->state.log;
4035 old_selected_entry = ls->selected_entry;
4037 err = input_log_view(NULL, s->log_view, KEY_UP);
4041 if (old_selected_entry == ls->selected_entry)
4044 err = set_selected_commit(s, ls->selected_entry);
4048 s->first_displayed_line = 1;
4049 s->last_displayed_line = view->nlines;
4050 s->matched_line = 0;
4053 diff_view_indicate_progress(view);
4054 err = create_diff(s);
4058 if (s->log_view == NULL)
4060 ls = &s->log_view->state.log;
4061 old_selected_entry = ls->selected_entry;
4063 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4067 if (old_selected_entry == ls->selected_entry)
4070 err = set_selected_commit(s, ls->selected_entry);
4074 s->first_displayed_line = 1;
4075 s->last_displayed_line = view->nlines;
4076 s->matched_line = 0;
4079 diff_view_indicate_progress(view);
4080 err = create_diff(s);
4089 static const struct got_error *
4090 cmd_diff(int argc, char *argv[])
4092 const struct got_error *error = NULL;
4093 struct got_repository *repo = NULL;
4094 struct got_worktree *worktree = NULL;
4095 struct got_object_id *id1 = NULL, *id2 = NULL;
4096 char *repo_path = NULL, *cwd = NULL;
4097 char *id_str1 = NULL, *id_str2 = NULL;
4098 char *label1 = NULL, *label2 = NULL;
4099 int diff_context = 3, ignore_whitespace = 0;
4100 int ch, force_text_diff = 0;
4102 struct tog_view *view;
4103 int *pack_fds = NULL;
4105 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4108 force_text_diff = 1;
4111 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4114 errx(1, "number of context lines is %s: %s",
4118 repo_path = realpath(optarg, NULL);
4119 if (repo_path == NULL)
4120 return got_error_from_errno2("realpath",
4122 got_path_strip_trailing_slashes(repo_path);
4125 ignore_whitespace = 1;
4137 usage_diff(); /* TODO show local worktree changes */
4138 } else if (argc == 2) {
4144 error = got_repo_pack_fds_open(&pack_fds);
4148 if (repo_path == NULL) {
4149 cwd = getcwd(NULL, 0);
4151 return got_error_from_errno("getcwd");
4152 error = got_worktree_open(&worktree, cwd);
4153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4157 strdup(got_worktree_get_repo_path(worktree));
4159 repo_path = strdup(cwd);
4160 if (repo_path == NULL) {
4161 error = got_error_from_errno("strdup");
4166 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4172 error = apply_unveil(got_repo_get_path(repo), NULL);
4176 error = tog_load_refs(repo, 0);
4180 error = got_repo_match_object_id(&id1, &label1, id_str1,
4181 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4185 error = got_repo_match_object_id(&id2, &label2, id_str2,
4186 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4190 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4192 error = got_error_from_errno("view_open");
4195 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4196 ignore_whitespace, force_text_diff, NULL, repo);
4199 error = view_loop(view);
4206 const struct got_error *close_err = got_repo_close(repo);
4211 got_worktree_close(worktree);
4213 const struct got_error *pack_err =
4214 got_repo_pack_fds_close(pack_fds);
4226 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4231 struct tog_blame_line {
4233 struct got_object_id *id;
4236 static const struct got_error *
4237 draw_blame(struct tog_view *view)
4239 struct tog_blame_view_state *s = &view->state.blame;
4240 struct tog_blame *blame = &s->blame;
4241 regmatch_t *regmatch = &view->regmatch;
4242 const struct got_error *err;
4243 int lineno = 0, nprinted = 0, i;
4245 size_t linesize = 0;
4249 struct tog_blame_line *blame_line;
4250 struct got_object_id *prev_id = NULL;
4252 struct tog_color *tc;
4254 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4259 werase(view->window);
4261 if (asprintf(&line, "commit %s", id_str) == -1) {
4262 err = got_error_from_errno("asprintf");
4267 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4272 if (view_needs_focus_indication(view))
4273 wstandout(view->window);
4274 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4276 wattr_on(view->window,
4277 COLOR_PAIR(tc->colorpair), NULL);
4278 waddwstr(view->window, wline);
4280 wattr_off(view->window,
4281 COLOR_PAIR(tc->colorpair), NULL);
4282 if (view_needs_focus_indication(view))
4283 wstandend(view->window);
4286 if (width < view->ncols - 1)
4287 waddch(view->window, '\n');
4289 if (asprintf(&line, "[%d/%d] %s%s",
4290 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4291 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4293 return got_error_from_errno("asprintf");
4296 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4301 waddwstr(view->window, wline);
4304 if (width < view->ncols - 1)
4305 waddch(view->window, '\n');
4309 while (nprinted < view->nlines - 2) {
4310 linelen = getline(&line, &linesize, blame->f);
4311 if (linelen == -1) {
4312 if (feof(blame->f)) {
4317 return got_ferror(blame->f, GOT_ERR_IO);
4319 if (++lineno < s->first_displayed_line)
4322 view->maxx = MAX(view->maxx, linelen);
4324 if (view->focussed && nprinted == s->selected_line - 1)
4325 wstandout(view->window);
4327 if (blame->nlines > 0) {
4328 blame_line = &blame->lines[lineno - 1];
4329 if (blame_line->annotated && prev_id &&
4330 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4332 nprinted == s->selected_line - 1)) {
4333 waddstr(view->window, " ");
4334 } else if (blame_line->annotated) {
4336 err = got_object_id_str(&id_str, blame_line->id);
4341 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4343 wattr_on(view->window,
4344 COLOR_PAIR(tc->colorpair), NULL);
4345 wprintw(view->window, "%.8s", id_str);
4347 wattr_off(view->window,
4348 COLOR_PAIR(tc->colorpair), NULL);
4350 prev_id = blame_line->id;
4352 waddstr(view->window, "........");
4356 waddstr(view->window, "........");
4360 if (view->focussed && nprinted == s->selected_line - 1)
4361 wstandend(view->window);
4362 waddstr(view->window, " ");
4364 if (view->ncols <= 9) {
4366 wline = wcsdup(L"");
4367 if (wline == NULL) {
4368 err = got_error_from_errno("wcsdup");
4372 } else if (s->first_displayed_line + nprinted ==
4374 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4375 err = add_matched_line(&width, line, view->ncols - 9, 9,
4376 view->window, view->x, regmatch);
4383 err = format_line(&wline, &width, line,
4384 view->x + view->ncols - 9, 9, 1);
4389 if (view->x < width) {
4390 waddwstr(view->window, wline + view->x);
4391 for (i = 0; i < view->x; i++)
4392 width -= wcwidth(wline[i]);
4399 if (width <= view->ncols - 1)
4400 waddch(view->window, '\n');
4401 if (++nprinted == 1)
4402 s->first_displayed_line = lineno;
4405 s->last_displayed_line = lineno;
4412 static const struct got_error *
4413 blame_cb(void *arg, int nlines, int lineno,
4414 struct got_commit_object *commit, struct got_object_id *id)
4416 const struct got_error *err = NULL;
4417 struct tog_blame_cb_args *a = arg;
4418 struct tog_blame_line *line;
4421 if (nlines != a->nlines ||
4422 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4423 return got_error(GOT_ERR_RANGE);
4425 errcode = pthread_mutex_lock(&tog_mutex);
4427 return got_error_set_errno(errcode, "pthread_mutex_lock");
4429 if (*a->quit) { /* user has quit the blame view */
4430 err = got_error(GOT_ERR_ITER_COMPLETED);
4435 goto done; /* no change in this commit */
4437 line = &a->lines[lineno - 1];
4438 if (line->annotated)
4441 line->id = got_object_id_dup(id);
4442 if (line->id == NULL) {
4443 err = got_error_from_errno("got_object_id_dup");
4446 line->annotated = 1;
4448 errcode = pthread_mutex_unlock(&tog_mutex);
4450 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4455 blame_thread(void *arg)
4457 const struct got_error *err, *close_err;
4458 struct tog_blame_thread_args *ta = arg;
4459 struct tog_blame_cb_args *a = ta->cb_args;
4462 err = block_signals_used_by_main_thread();
4466 err = got_blame(ta->path, a->commit_id, ta->repo,
4467 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4468 if (err && err->code == GOT_ERR_CANCELLED)
4471 errcode = pthread_mutex_lock(&tog_mutex);
4473 return (void *)got_error_set_errno(errcode,
4474 "pthread_mutex_lock");
4476 close_err = got_repo_close(ta->repo);
4482 errcode = pthread_mutex_unlock(&tog_mutex);
4483 if (errcode && err == NULL)
4484 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4489 static struct got_object_id *
4490 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4491 int first_displayed_line, int selected_line)
4493 struct tog_blame_line *line;
4498 line = &lines[first_displayed_line - 1 + selected_line - 1];
4499 if (!line->annotated)
4505 static const struct got_error *
4506 stop_blame(struct tog_blame *blame)
4508 const struct got_error *err = NULL;
4511 if (blame->thread) {
4513 errcode = pthread_mutex_unlock(&tog_mutex);
4515 return got_error_set_errno(errcode,
4516 "pthread_mutex_unlock");
4517 errcode = pthread_join(blame->thread, (void **)&err);
4519 return got_error_set_errno(errcode, "pthread_join");
4520 errcode = pthread_mutex_lock(&tog_mutex);
4522 return got_error_set_errno(errcode,
4523 "pthread_mutex_lock");
4524 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4526 blame->thread = NULL;
4528 if (blame->thread_args.repo) {
4529 const struct got_error *close_err;
4530 close_err = got_repo_close(blame->thread_args.repo);
4533 blame->thread_args.repo = NULL;
4536 if (fclose(blame->f) == EOF && err == NULL)
4537 err = got_error_from_errno("fclose");
4541 for (i = 0; i < blame->nlines; i++)
4542 free(blame->lines[i].id);
4544 blame->lines = NULL;
4546 free(blame->cb_args.commit_id);
4547 blame->cb_args.commit_id = NULL;
4548 if (blame->pack_fds) {
4549 const struct got_error *pack_err =
4550 got_repo_pack_fds_close(blame->pack_fds);
4553 blame->pack_fds = NULL;
4558 static const struct got_error *
4559 cancel_blame_view(void *arg)
4561 const struct got_error *err = NULL;
4565 errcode = pthread_mutex_lock(&tog_mutex);
4567 return got_error_set_errno(errcode,
4568 "pthread_mutex_unlock");
4571 err = got_error(GOT_ERR_CANCELLED);
4573 errcode = pthread_mutex_unlock(&tog_mutex);
4575 return got_error_set_errno(errcode,
4576 "pthread_mutex_lock");
4581 static const struct got_error *
4582 run_blame(struct tog_view *view)
4584 struct tog_blame_view_state *s = &view->state.blame;
4585 struct tog_blame *blame = &s->blame;
4586 const struct got_error *err = NULL;
4587 struct got_commit_object *commit = NULL;
4588 struct got_blob_object *blob = NULL;
4589 struct got_repository *thread_repo = NULL;
4590 struct got_object_id *obj_id = NULL;
4592 int *pack_fds = NULL;
4594 err = got_object_open_as_commit(&commit, s->repo,
4595 &s->blamed_commit->id);
4599 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4603 err = got_object_get_type(&obj_type, s->repo, obj_id);
4607 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4608 err = got_error(GOT_ERR_OBJ_TYPE);
4612 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4615 blame->f = got_opentemp();
4616 if (blame->f == NULL) {
4617 err = got_error_from_errno("got_opentemp");
4620 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4621 &blame->line_offsets, blame->f, blob);
4624 if (blame->nlines == 0) {
4625 s->blame_complete = 1;
4629 /* Don't include \n at EOF in the blame line count. */
4630 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4633 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4634 if (blame->lines == NULL) {
4635 err = got_error_from_errno("calloc");
4639 err = got_repo_pack_fds_open(&pack_fds);
4642 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4647 blame->pack_fds = pack_fds;
4648 blame->cb_args.view = view;
4649 blame->cb_args.lines = blame->lines;
4650 blame->cb_args.nlines = blame->nlines;
4651 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4652 if (blame->cb_args.commit_id == NULL) {
4653 err = got_error_from_errno("got_object_id_dup");
4656 blame->cb_args.quit = &s->done;
4658 blame->thread_args.path = s->path;
4659 blame->thread_args.repo = thread_repo;
4660 blame->thread_args.cb_args = &blame->cb_args;
4661 blame->thread_args.complete = &s->blame_complete;
4662 blame->thread_args.cancel_cb = cancel_blame_view;
4663 blame->thread_args.cancel_arg = &s->done;
4664 s->blame_complete = 0;
4666 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4667 s->first_displayed_line = 1;
4668 s->last_displayed_line = view->nlines;
4669 s->selected_line = 1;
4671 s->matched_line = 0;
4675 got_object_commit_close(commit);
4677 got_object_blob_close(blob);
4684 static const struct got_error *
4685 open_blame_view(struct tog_view *view, char *path,
4686 struct got_object_id *commit_id, struct got_repository *repo)
4688 const struct got_error *err = NULL;
4689 struct tog_blame_view_state *s = &view->state.blame;
4691 STAILQ_INIT(&s->blamed_commits);
4693 s->path = strdup(path);
4694 if (s->path == NULL)
4695 return got_error_from_errno("strdup");
4697 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4703 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4704 s->first_displayed_line = 1;
4705 s->last_displayed_line = view->nlines;
4706 s->selected_line = 1;
4707 s->blame_complete = 0;
4709 s->commit_id = commit_id;
4710 memset(&s->blame, 0, sizeof(s->blame));
4712 STAILQ_INIT(&s->colors);
4713 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4714 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4715 get_color_value("TOG_COLOR_COMMIT"));
4720 view->show = show_blame_view;
4721 view->input = input_blame_view;
4722 view->close = close_blame_view;
4723 view->search_start = search_start_blame_view;
4724 view->search_next = search_next_blame_view;
4726 return run_blame(view);
4729 static const struct got_error *
4730 close_blame_view(struct tog_view *view)
4732 const struct got_error *err = NULL;
4733 struct tog_blame_view_state *s = &view->state.blame;
4735 if (s->blame.thread)
4736 err = stop_blame(&s->blame);
4738 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4739 struct got_object_qid *blamed_commit;
4740 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4741 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4742 got_object_qid_free(blamed_commit);
4746 free_colors(&s->colors);
4750 static const struct got_error *
4751 search_start_blame_view(struct tog_view *view)
4753 struct tog_blame_view_state *s = &view->state.blame;
4755 s->matched_line = 0;
4759 static const struct got_error *
4760 search_next_blame_view(struct tog_view *view)
4762 struct tog_blame_view_state *s = &view->state.blame;
4763 const struct got_error *err = NULL;
4765 char *exstr = NULL, *line = NULL;
4766 size_t linesize = 0;
4769 if (!view->searching) {
4770 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4774 if (s->matched_line) {
4775 if (view->searching == TOG_SEARCH_FORWARD)
4776 lineno = s->matched_line + 1;
4778 lineno = s->matched_line - 1;
4780 lineno = s->first_displayed_line - 1 + s->selected_line;
4785 if (lineno <= 0 || lineno > s->blame.nlines) {
4786 if (s->matched_line == 0) {
4787 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4791 if (view->searching == TOG_SEARCH_FORWARD)
4794 lineno = s->blame.nlines;
4797 offset = s->blame.line_offsets[lineno - 1];
4798 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4800 return got_error_from_errno("fseeko");
4802 linelen = getline(&line, &linesize, s->blame.f);
4803 err = expand_tab(&exstr, line);
4806 if (linelen != -1 &&
4807 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4808 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4809 s->matched_line = lineno;
4814 if (view->searching == TOG_SEARCH_FORWARD)
4822 if (s->matched_line) {
4823 s->first_displayed_line = s->matched_line;
4824 s->selected_line = 1;
4830 static const struct got_error *
4831 show_blame_view(struct tog_view *view)
4833 const struct got_error *err = NULL;
4834 struct tog_blame_view_state *s = &view->state.blame;
4837 if (s->blame.thread == NULL && !s->blame_complete) {
4838 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4839 &s->blame.thread_args);
4841 return got_error_set_errno(errcode, "pthread_create");
4843 halfdelay(1); /* fast refresh while annotating */
4846 if (s->blame_complete)
4847 halfdelay(10); /* disable fast refresh */
4849 err = draw_blame(view);
4855 static const struct got_error *
4856 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4858 const struct got_error *err = NULL, *thread_err = NULL;
4859 struct tog_view *diff_view;
4860 struct tog_blame_view_state *s = &view->state.blame;
4861 int begin_x = 0, nscroll = view->nlines - 2;
4868 view->x = MAX(view->maxx - view->ncols / 3, 0);
4872 if (view->x + view->ncols / 3 < view->maxx)
4873 view->x += 2; /* move two columns right */
4877 view->x -= MIN(view->x, 2); /* move two columns back */
4884 s->selected_line = 1;
4885 s->first_displayed_line = 1;
4889 if (s->blame.nlines < view->nlines - 2) {
4890 s->selected_line = s->blame.nlines;
4891 s->first_displayed_line = 1;
4893 s->selected_line = view->nlines - 2;
4894 s->first_displayed_line = s->blame.nlines -
4901 if (s->selected_line > 1)
4903 else if (s->selected_line == 1 &&
4904 s->first_displayed_line > 1)
4905 s->first_displayed_line--;
4913 if (s->first_displayed_line == 1) {
4914 s->selected_line = MAX(1, s->selected_line - nscroll);
4917 if (s->first_displayed_line > nscroll)
4918 s->first_displayed_line -= nscroll;
4920 s->first_displayed_line = 1;
4925 if (s->selected_line < view->nlines - 2 &&
4926 s->first_displayed_line +
4927 s->selected_line <= s->blame.nlines)
4929 else if (s->last_displayed_line <
4931 s->first_displayed_line++;
4935 struct got_object_id *id = NULL;
4936 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4937 s->first_displayed_line, s->selected_line);
4941 struct got_commit_object *commit, *pcommit;
4942 struct got_object_qid *pid;
4943 struct got_object_id *blob_id = NULL;
4945 err = got_object_open_as_commit(&commit,
4950 got_object_commit_get_parent_ids(commit));
4952 got_object_commit_close(commit);
4955 /* Check if path history ends here. */
4956 err = got_object_open_as_commit(&pcommit,
4960 err = got_object_id_by_path(&blob_id, s->repo,
4962 got_object_commit_close(pcommit);
4964 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4966 got_object_commit_close(commit);
4969 err = got_object_get_type(&obj_type, s->repo,
4972 /* Can't blame non-blob type objects. */
4973 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4974 got_object_commit_close(commit);
4977 err = got_object_qid_alloc(&s->blamed_commit,
4979 got_object_commit_close(commit);
4981 if (got_object_id_cmp(id,
4982 &s->blamed_commit->id) == 0)
4984 err = got_object_qid_alloc(&s->blamed_commit,
4990 thread_err = stop_blame(&s->blame);
4994 STAILQ_INSERT_HEAD(&s->blamed_commits,
4995 s->blamed_commit, entry);
4996 err = run_blame(view);
5002 struct got_object_qid *first;
5003 first = STAILQ_FIRST(&s->blamed_commits);
5004 if (!got_object_id_cmp(&first->id, s->commit_id))
5007 thread_err = stop_blame(&s->blame);
5011 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5012 got_object_qid_free(s->blamed_commit);
5014 STAILQ_FIRST(&s->blamed_commits);
5015 err = run_blame(view);
5022 struct got_object_id *id = NULL;
5023 struct got_object_qid *pid;
5024 struct got_commit_object *commit = NULL;
5025 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5026 s->first_displayed_line, s->selected_line);
5029 err = got_object_open_as_commit(&commit, s->repo, id);
5033 got_object_commit_get_parent_ids(commit));
5034 if (view_is_parent_view(view))
5035 begin_x = view_split_begin_x(view->begin_x);
5036 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5037 if (diff_view == NULL) {
5038 got_object_commit_close(commit);
5039 err = got_error_from_errno("view_open");
5042 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5043 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5044 got_object_commit_close(commit);
5046 view_close(diff_view);
5050 diff_view->focussed = 1;
5051 if (view_is_parent_view(view)) {
5052 err = view_close_child(view);
5055 view_set_child(view, diff_view);
5056 view->focus_child = 1;
5058 *new_view = diff_view;
5070 if (s->last_displayed_line >= s->blame.nlines &&
5071 s->selected_line >= MIN(s->blame.nlines,
5072 view->nlines - 2)) {
5075 if (s->last_displayed_line >= s->blame.nlines &&
5076 s->selected_line < view->nlines - 2) {
5078 MIN(nscroll, s->last_displayed_line -
5079 s->first_displayed_line - s->selected_line + 1);
5081 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5082 s->first_displayed_line += nscroll;
5084 s->first_displayed_line =
5085 s->blame.nlines - (view->nlines - 3);
5088 if (s->selected_line > view->nlines - 2) {
5089 s->selected_line = MIN(s->blame.nlines,
5096 return thread_err ? thread_err : err;
5099 static const struct got_error *
5100 cmd_blame(int argc, char *argv[])
5102 const struct got_error *error;
5103 struct got_repository *repo = NULL;
5104 struct got_worktree *worktree = NULL;
5105 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5106 char *link_target = NULL;
5107 struct got_object_id *commit_id = NULL;
5108 struct got_commit_object *commit = NULL;
5109 char *commit_id_str = NULL;
5111 struct tog_view *view;
5112 int *pack_fds = NULL;
5114 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5117 commit_id_str = optarg;
5120 repo_path = realpath(optarg, NULL);
5121 if (repo_path == NULL)
5122 return got_error_from_errno2("realpath",
5137 error = got_repo_pack_fds_open(&pack_fds);
5141 if (repo_path == NULL) {
5142 cwd = getcwd(NULL, 0);
5144 return got_error_from_errno("getcwd");
5145 error = got_worktree_open(&worktree, cwd);
5146 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5150 strdup(got_worktree_get_repo_path(worktree));
5152 repo_path = strdup(cwd);
5153 if (repo_path == NULL) {
5154 error = got_error_from_errno("strdup");
5159 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5163 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5170 error = apply_unveil(got_repo_get_path(repo), NULL);
5174 error = tog_load_refs(repo, 0);
5178 if (commit_id_str == NULL) {
5179 struct got_reference *head_ref;
5180 error = got_ref_open(&head_ref, repo, worktree ?
5181 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5184 error = got_ref_resolve(&commit_id, repo, head_ref);
5185 got_ref_close(head_ref);
5187 error = got_repo_match_object_id(&commit_id, NULL,
5188 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5193 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5195 error = got_error_from_errno("view_open");
5199 error = got_object_open_as_commit(&commit, repo, commit_id);
5203 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5208 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5213 /* Release work tree lock. */
5214 got_worktree_close(worktree);
5217 error = view_loop(view);
5225 got_object_commit_close(commit);
5227 got_worktree_close(worktree);
5229 const struct got_error *close_err = got_repo_close(repo);
5234 const struct got_error *pack_err =
5235 got_repo_pack_fds_close(pack_fds);
5243 static const struct got_error *
5244 draw_tree_entries(struct tog_view *view, const char *parent_path)
5246 struct tog_tree_view_state *s = &view->state.tree;
5247 const struct got_error *err = NULL;
5248 struct got_tree_entry *te;
5250 struct tog_color *tc;
5251 int width, n, i, nentries;
5252 int limit = view->nlines;
5256 werase(view->window);
5261 err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5264 if (view_needs_focus_indication(view))
5265 wstandout(view->window);
5266 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5268 wattr_on(view->window,
5269 COLOR_PAIR(tc->colorpair), NULL);
5270 waddwstr(view->window, wline);
5272 wattr_off(view->window,
5273 COLOR_PAIR(tc->colorpair), NULL);
5274 if (view_needs_focus_indication(view))
5275 wstandend(view->window);
5278 if (width < view->ncols - 1)
5279 waddch(view->window, '\n');
5282 err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5285 waddwstr(view->window, wline);
5288 if (width < view->ncols - 1)
5289 waddch(view->window, '\n');
5292 waddch(view->window, '\n');
5296 if (s->first_displayed_entry == NULL) {
5297 te = got_object_tree_get_first_entry(s->tree);
5298 if (s->selected == 0) {
5300 wstandout(view->window);
5301 s->selected_entry = NULL;
5303 waddstr(view->window, " ..\n"); /* parent directory */
5304 if (s->selected == 0 && view->focussed)
5305 wstandend(view->window);
5312 te = s->first_displayed_entry;
5315 nentries = got_object_tree_get_nentries(s->tree);
5316 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5317 char *line = NULL, *id_str = NULL, *link_target = NULL;
5318 const char *modestr = "";
5321 te = got_object_tree_get_entry(s->tree, i);
5322 mode = got_tree_entry_get_mode(te);
5325 err = got_object_id_str(&id_str,
5326 got_tree_entry_get_id(te));
5328 return got_error_from_errno(
5329 "got_object_id_str");
5331 if (got_object_tree_entry_is_submodule(te))
5333 else if (S_ISLNK(mode)) {
5336 err = got_tree_entry_get_symlink_target(&link_target,
5342 for (i = 0; i < strlen(link_target); i++) {
5343 if (!isprint((unsigned char)link_target[i]))
5344 link_target[i] = '?';
5348 else if (S_ISDIR(mode))
5350 else if (mode & S_IXUSR)
5352 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5353 got_tree_entry_get_name(te), modestr,
5354 link_target ? " -> ": "",
5355 link_target ? link_target : "") == -1) {
5358 return got_error_from_errno("asprintf");
5362 err = format_line(&wline, &width, line, view->ncols, 0, 0);
5367 if (n == s->selected) {
5369 wstandout(view->window);
5370 s->selected_entry = te;
5372 tc = match_color(&s->colors, line);
5374 wattr_on(view->window,
5375 COLOR_PAIR(tc->colorpair), NULL);
5376 waddwstr(view->window, wline);
5378 wattr_off(view->window,
5379 COLOR_PAIR(tc->colorpair), NULL);
5380 if (width < view->ncols - 1)
5381 waddch(view->window, '\n');
5382 if (n == s->selected && view->focussed)
5383 wstandend(view->window);
5389 s->last_displayed_entry = te;
5398 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5400 struct got_tree_entry *te;
5401 int isroot = s->tree == s->root;
5404 if (s->first_displayed_entry == NULL)
5407 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5408 while (i++ < maxscroll) {
5411 s->first_displayed_entry = NULL;
5414 s->first_displayed_entry = te;
5415 te = got_tree_entry_get_prev(s->tree, te);
5420 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5422 struct got_tree_entry *next, *last;
5425 if (s->first_displayed_entry)
5426 next = got_tree_entry_get_next(s->tree,
5427 s->first_displayed_entry);
5429 next = got_object_tree_get_first_entry(s->tree);
5431 last = s->last_displayed_entry;
5432 while (next && last && n++ < maxscroll) {
5433 last = got_tree_entry_get_next(s->tree, last);
5435 s->first_displayed_entry = next;
5436 next = got_tree_entry_get_next(s->tree, next);
5441 static const struct got_error *
5442 tree_entry_path(char **path, struct tog_parent_trees *parents,
5443 struct got_tree_entry *te)
5445 const struct got_error *err = NULL;
5446 struct tog_parent_tree *pt;
5447 size_t len = 2; /* for leading slash and NUL */
5449 TAILQ_FOREACH(pt, parents, entry)
5450 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5453 len += strlen(got_tree_entry_get_name(te));
5455 *path = calloc(1, len);
5457 return got_error_from_errno("calloc");
5460 pt = TAILQ_LAST(parents, tog_parent_trees);
5462 const char *name = got_tree_entry_get_name(pt->selected_entry);
5463 if (strlcat(*path, name, len) >= len) {
5464 err = got_error(GOT_ERR_NO_SPACE);
5467 if (strlcat(*path, "/", len) >= len) {
5468 err = got_error(GOT_ERR_NO_SPACE);
5471 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5474 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5475 err = got_error(GOT_ERR_NO_SPACE);
5487 static const struct got_error *
5488 blame_tree_entry(struct tog_view **new_view, int begin_x,
5489 struct got_tree_entry *te, struct tog_parent_trees *parents,
5490 struct got_object_id *commit_id, struct got_repository *repo)
5492 const struct got_error *err = NULL;
5494 struct tog_view *blame_view;
5498 err = tree_entry_path(&path, parents, te);
5502 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5503 if (blame_view == NULL) {
5504 err = got_error_from_errno("view_open");
5508 err = open_blame_view(blame_view, path, commit_id, repo);
5510 if (err->code == GOT_ERR_CANCELLED)
5512 view_close(blame_view);
5514 *new_view = blame_view;
5520 static const struct got_error *
5521 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5522 struct tog_tree_view_state *s)
5524 struct tog_view *log_view;
5525 const struct got_error *err = NULL;
5530 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5531 if (log_view == NULL)
5532 return got_error_from_errno("view_open");
5534 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5538 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5541 view_close(log_view);
5543 *new_view = log_view;
5548 static const struct got_error *
5549 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5550 const char *head_ref_name, struct got_repository *repo)
5552 const struct got_error *err = NULL;
5553 char *commit_id_str = NULL;
5554 struct tog_tree_view_state *s = &view->state.tree;
5555 struct got_commit_object *commit = NULL;
5557 TAILQ_INIT(&s->parents);
5558 STAILQ_INIT(&s->colors);
5560 s->commit_id = got_object_id_dup(commit_id);
5561 if (s->commit_id == NULL)
5562 return got_error_from_errno("got_object_id_dup");
5564 err = got_object_open_as_commit(&commit, repo, commit_id);
5569 * The root is opened here and will be closed when the view is closed.
5570 * Any visited subtrees and their path-wise parents are opened and
5573 err = got_object_open_as_tree(&s->root, repo,
5574 got_object_commit_get_tree_id(commit));
5579 err = got_object_id_str(&commit_id_str, commit_id);
5583 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5584 err = got_error_from_errno("asprintf");
5588 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5589 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5590 if (head_ref_name) {
5591 s->head_ref_name = strdup(head_ref_name);
5592 if (s->head_ref_name == NULL) {
5593 err = got_error_from_errno("strdup");
5599 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5600 err = add_color(&s->colors, "\\$$",
5601 TOG_COLOR_TREE_SUBMODULE,
5602 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5605 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5606 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5609 err = add_color(&s->colors, "/$",
5610 TOG_COLOR_TREE_DIRECTORY,
5611 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5615 err = add_color(&s->colors, "\\*$",
5616 TOG_COLOR_TREE_EXECUTABLE,
5617 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5621 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5622 get_color_value("TOG_COLOR_COMMIT"));
5627 view->show = show_tree_view;
5628 view->input = input_tree_view;
5629 view->close = close_tree_view;
5630 view->search_start = search_start_tree_view;
5631 view->search_next = search_next_tree_view;
5633 free(commit_id_str);
5635 got_object_commit_close(commit);
5637 close_tree_view(view);
5641 static const struct got_error *
5642 close_tree_view(struct tog_view *view)
5644 struct tog_tree_view_state *s = &view->state.tree;
5646 free_colors(&s->colors);
5647 free(s->tree_label);
5648 s->tree_label = NULL;
5650 s->commit_id = NULL;
5651 free(s->head_ref_name);
5652 s->head_ref_name = NULL;
5653 while (!TAILQ_EMPTY(&s->parents)) {
5654 struct tog_parent_tree *parent;
5655 parent = TAILQ_FIRST(&s->parents);
5656 TAILQ_REMOVE(&s->parents, parent, entry);
5657 if (parent->tree != s->root)
5658 got_object_tree_close(parent->tree);
5662 if (s->tree != NULL && s->tree != s->root)
5663 got_object_tree_close(s->tree);
5665 got_object_tree_close(s->root);
5669 static const struct got_error *
5670 search_start_tree_view(struct tog_view *view)
5672 struct tog_tree_view_state *s = &view->state.tree;
5674 s->matched_entry = NULL;
5679 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5681 regmatch_t regmatch;
5683 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5687 static const struct got_error *
5688 search_next_tree_view(struct tog_view *view)
5690 struct tog_tree_view_state *s = &view->state.tree;
5691 struct got_tree_entry *te = NULL;
5693 if (!view->searching) {
5694 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5698 if (s->matched_entry) {
5699 if (view->searching == TOG_SEARCH_FORWARD) {
5700 if (s->selected_entry)
5701 te = got_tree_entry_get_next(s->tree,
5704 te = got_object_tree_get_first_entry(s->tree);
5706 if (s->selected_entry == NULL)
5707 te = got_object_tree_get_last_entry(s->tree);
5709 te = got_tree_entry_get_prev(s->tree,
5713 if (s->selected_entry)
5714 te = s->selected_entry;
5715 else if (view->searching == TOG_SEARCH_FORWARD)
5716 te = got_object_tree_get_first_entry(s->tree);
5718 te = got_object_tree_get_last_entry(s->tree);
5723 if (s->matched_entry == NULL) {
5724 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5727 if (view->searching == TOG_SEARCH_FORWARD)
5728 te = got_object_tree_get_first_entry(s->tree);
5730 te = got_object_tree_get_last_entry(s->tree);
5733 if (match_tree_entry(te, &view->regex)) {
5734 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5735 s->matched_entry = te;
5739 if (view->searching == TOG_SEARCH_FORWARD)
5740 te = got_tree_entry_get_next(s->tree, te);
5742 te = got_tree_entry_get_prev(s->tree, te);
5745 if (s->matched_entry) {
5746 s->first_displayed_entry = s->matched_entry;
5753 static const struct got_error *
5754 show_tree_view(struct tog_view *view)
5756 const struct got_error *err = NULL;
5757 struct tog_tree_view_state *s = &view->state.tree;
5760 err = tree_entry_path(&parent_path, &s->parents, NULL);
5764 err = draw_tree_entries(view, parent_path);
5771 static const struct got_error *
5772 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5774 const struct got_error *err = NULL;
5775 struct tog_tree_view_state *s = &view->state.tree;
5776 struct tog_view *log_view, *ref_view;
5777 struct got_tree_entry *te;
5778 int begin_x = 0, n, nscroll = view->nlines - 3;
5782 s->show_ids = !s->show_ids;
5785 if (!s->selected_entry)
5787 if (view_is_parent_view(view))
5788 begin_x = view_split_begin_x(view->begin_x);
5789 err = log_selected_tree_entry(&log_view, begin_x, s);
5791 log_view->focussed = 1;
5792 if (view_is_parent_view(view)) {
5793 err = view_close_child(view);
5796 view_set_child(view, log_view);
5797 view->focus_child = 1;
5799 *new_view = log_view;
5802 if (view_is_parent_view(view))
5803 begin_x = view_split_begin_x(view->begin_x);
5804 ref_view = view_open(view->nlines, view->ncols,
5805 view->begin_y, begin_x, TOG_VIEW_REF);
5806 if (ref_view == NULL)
5807 return got_error_from_errno("view_open");
5808 err = open_ref_view(ref_view, s->repo);
5810 view_close(ref_view);
5814 ref_view->focussed = 1;
5815 if (view_is_parent_view(view)) {
5816 err = view_close_child(view);
5819 view_set_child(view, ref_view);
5820 view->focus_child = 1;
5822 *new_view = ref_view;
5827 if (s->tree == s->root)
5828 s->first_displayed_entry =
5829 got_object_tree_get_first_entry(s->tree);
5831 s->first_displayed_entry = NULL;
5836 te = got_object_tree_get_last_entry(s->tree);
5837 for (n = 0; n < view->nlines - 3; n++) {
5839 if(s->tree != s->root) {
5840 s->first_displayed_entry = NULL;
5845 s->first_displayed_entry = te;
5846 te = got_tree_entry_get_prev(s->tree, te);
5849 s->selected = n - 1;
5854 if (s->selected > 0) {
5858 tree_scroll_up(s, 1);
5866 if (s->tree == s->root) {
5867 if (got_object_tree_get_first_entry(s->tree) ==
5868 s->first_displayed_entry)
5869 s->selected -= MIN(s->selected, nscroll);
5871 if (s->first_displayed_entry == NULL)
5872 s->selected -= MIN(s->selected, nscroll);
5874 tree_scroll_up(s, MAX(0, nscroll));
5879 if (s->selected < s->ndisplayed - 1) {
5883 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5885 /* can't scroll any further */
5887 tree_scroll_down(s, 1);
5895 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5897 /* can't scroll any further; move cursor down */
5898 if (s->selected < s->ndisplayed - 1)
5899 s->selected += MIN(nscroll,
5900 s->ndisplayed - s->selected - 1);
5903 tree_scroll_down(s, nscroll);
5908 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5909 struct tog_parent_tree *parent;
5910 /* user selected '..' */
5911 if (s->tree == s->root)
5913 parent = TAILQ_FIRST(&s->parents);
5914 TAILQ_REMOVE(&s->parents, parent,
5916 got_object_tree_close(s->tree);
5917 s->tree = parent->tree;
5918 s->first_displayed_entry =
5919 parent->first_displayed_entry;
5921 parent->selected_entry;
5922 s->selected = parent->selected;
5924 } else if (S_ISDIR(got_tree_entry_get_mode(
5925 s->selected_entry))) {
5926 struct got_tree_object *subtree;
5927 err = got_object_open_as_tree(&subtree, s->repo,
5928 got_tree_entry_get_id(s->selected_entry));
5931 err = tree_view_visit_subtree(s, subtree);
5933 got_object_tree_close(subtree);
5936 } else if (S_ISREG(got_tree_entry_get_mode(
5937 s->selected_entry))) {
5938 struct tog_view *blame_view;
5939 int begin_x = view_is_parent_view(view) ?
5940 view_split_begin_x(view->begin_x) : 0;
5942 err = blame_tree_entry(&blame_view, begin_x,
5943 s->selected_entry, &s->parents,
5944 s->commit_id, s->repo);
5948 blame_view->focussed = 1;
5949 if (view_is_parent_view(view)) {
5950 err = view_close_child(view);
5953 view_set_child(view, blame_view);
5954 view->focus_child = 1;
5956 *new_view = blame_view;
5960 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5961 s->selected = view->nlines - 4;
5974 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5979 static const struct got_error *
5980 cmd_tree(int argc, char *argv[])
5982 const struct got_error *error;
5983 struct got_repository *repo = NULL;
5984 struct got_worktree *worktree = NULL;
5985 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5986 struct got_object_id *commit_id = NULL;
5987 struct got_commit_object *commit = NULL;
5988 const char *commit_id_arg = NULL;
5990 struct got_reference *ref = NULL;
5991 const char *head_ref_name = NULL;
5993 struct tog_view *view;
5994 int *pack_fds = NULL;
5996 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5999 commit_id_arg = optarg;
6002 repo_path = realpath(optarg, NULL);
6003 if (repo_path == NULL)
6004 return got_error_from_errno2("realpath",
6019 error = got_repo_pack_fds_open(&pack_fds);
6023 if (repo_path == NULL) {
6024 cwd = getcwd(NULL, 0);
6026 return got_error_from_errno("getcwd");
6027 error = got_worktree_open(&worktree, cwd);
6028 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6032 strdup(got_worktree_get_repo_path(worktree));
6034 repo_path = strdup(cwd);
6035 if (repo_path == NULL) {
6036 error = got_error_from_errno("strdup");
6041 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6045 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6052 error = apply_unveil(got_repo_get_path(repo), NULL);
6056 error = tog_load_refs(repo, 0);
6060 if (commit_id_arg == NULL) {
6061 error = got_repo_match_object_id(&commit_id, &label,
6062 worktree ? got_worktree_get_head_ref_name(worktree) :
6063 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6066 head_ref_name = label;
6068 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6070 head_ref_name = got_ref_get_name(ref);
6071 else if (error->code != GOT_ERR_NOT_REF)
6073 error = got_repo_match_object_id(&commit_id, NULL,
6074 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6079 error = got_object_open_as_commit(&commit, repo, commit_id);
6083 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6085 error = got_error_from_errno("view_open");
6088 error = open_tree_view(view, commit_id, head_ref_name, repo);
6091 if (!got_path_is_root_dir(in_repo_path)) {
6092 error = tree_view_walk_path(&view->state.tree, commit,
6099 /* Release work tree lock. */
6100 got_worktree_close(worktree);
6103 error = view_loop(view);
6112 const struct got_error *close_err = got_repo_close(repo);
6117 const struct got_error *pack_err =
6118 got_repo_pack_fds_close(pack_fds);
6126 static const struct got_error *
6127 ref_view_load_refs(struct tog_ref_view_state *s)
6129 struct got_reflist_entry *sre;
6130 struct tog_reflist_entry *re;
6133 TAILQ_FOREACH(sre, &tog_refs, entry) {
6134 if (strncmp(got_ref_get_name(sre->ref),
6135 "refs/got/", 9) == 0 &&
6136 strncmp(got_ref_get_name(sre->ref),
6137 "refs/got/backup/", 16) != 0)
6140 re = malloc(sizeof(*re));
6142 return got_error_from_errno("malloc");
6144 re->ref = got_ref_dup(sre->ref);
6145 if (re->ref == NULL)
6146 return got_error_from_errno("got_ref_dup");
6147 re->idx = s->nrefs++;
6148 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6151 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6156 ref_view_free_refs(struct tog_ref_view_state *s)
6158 struct tog_reflist_entry *re;
6160 while (!TAILQ_EMPTY(&s->refs)) {
6161 re = TAILQ_FIRST(&s->refs);
6162 TAILQ_REMOVE(&s->refs, re, entry);
6163 got_ref_close(re->ref);
6168 static const struct got_error *
6169 open_ref_view(struct tog_view *view, struct got_repository *repo)
6171 const struct got_error *err = NULL;
6172 struct tog_ref_view_state *s = &view->state.ref;
6174 s->selected_entry = 0;
6177 TAILQ_INIT(&s->refs);
6178 STAILQ_INIT(&s->colors);
6180 err = ref_view_load_refs(s);
6184 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6185 err = add_color(&s->colors, "^refs/heads/",
6186 TOG_COLOR_REFS_HEADS,
6187 get_color_value("TOG_COLOR_REFS_HEADS"));
6191 err = add_color(&s->colors, "^refs/tags/",
6192 TOG_COLOR_REFS_TAGS,
6193 get_color_value("TOG_COLOR_REFS_TAGS"));
6197 err = add_color(&s->colors, "^refs/remotes/",
6198 TOG_COLOR_REFS_REMOTES,
6199 get_color_value("TOG_COLOR_REFS_REMOTES"));
6203 err = add_color(&s->colors, "^refs/got/backup/",
6204 TOG_COLOR_REFS_BACKUP,
6205 get_color_value("TOG_COLOR_REFS_BACKUP"));
6210 view->show = show_ref_view;
6211 view->input = input_ref_view;
6212 view->close = close_ref_view;
6213 view->search_start = search_start_ref_view;
6214 view->search_next = search_next_ref_view;
6217 free_colors(&s->colors);
6221 static const struct got_error *
6222 close_ref_view(struct tog_view *view)
6224 struct tog_ref_view_state *s = &view->state.ref;
6226 ref_view_free_refs(s);
6227 free_colors(&s->colors);
6232 static const struct got_error *
6233 resolve_reflist_entry(struct got_object_id **commit_id,
6234 struct tog_reflist_entry *re, struct got_repository *repo)
6236 const struct got_error *err = NULL;
6237 struct got_object_id *obj_id;
6238 struct got_tag_object *tag = NULL;
6243 err = got_ref_resolve(&obj_id, repo, re->ref);
6247 err = got_object_get_type(&obj_type, repo, obj_id);
6252 case GOT_OBJ_TYPE_COMMIT:
6253 *commit_id = obj_id;
6255 case GOT_OBJ_TYPE_TAG:
6256 err = got_object_open_as_tag(&tag, repo, obj_id);
6260 err = got_object_get_type(&obj_type, repo,
6261 got_object_tag_get_object_id(tag));
6264 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6265 err = got_error(GOT_ERR_OBJ_TYPE);
6268 *commit_id = got_object_id_dup(
6269 got_object_tag_get_object_id(tag));
6270 if (*commit_id == NULL) {
6271 err = got_error_from_errno("got_object_id_dup");
6276 err = got_error(GOT_ERR_OBJ_TYPE);
6282 got_object_tag_close(tag);
6290 static const struct got_error *
6291 log_ref_entry(struct tog_view **new_view, int begin_x,
6292 struct tog_reflist_entry *re, struct got_repository *repo)
6294 struct tog_view *log_view;
6295 const struct got_error *err = NULL;
6296 struct got_object_id *commit_id = NULL;
6300 err = resolve_reflist_entry(&commit_id, re, repo);
6302 if (err->code != GOT_ERR_OBJ_TYPE)
6308 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6309 if (log_view == NULL) {
6310 err = got_error_from_errno("view_open");
6314 err = open_log_view(log_view, commit_id, repo,
6315 got_ref_get_name(re->ref), "", 0);
6318 view_close(log_view);
6320 *new_view = log_view;
6326 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6328 struct tog_reflist_entry *re;
6331 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6334 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6335 while (i++ < maxscroll) {
6338 s->first_displayed_entry = re;
6339 re = TAILQ_PREV(re, tog_reflist_head, entry);
6344 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6346 struct tog_reflist_entry *next, *last;
6349 if (s->first_displayed_entry)
6350 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6352 next = TAILQ_FIRST(&s->refs);
6354 last = s->last_displayed_entry;
6355 while (next && last && n++ < maxscroll) {
6356 last = TAILQ_NEXT(last, entry);
6358 s->first_displayed_entry = next;
6359 next = TAILQ_NEXT(next, entry);
6364 static const struct got_error *
6365 search_start_ref_view(struct tog_view *view)
6367 struct tog_ref_view_state *s = &view->state.ref;
6369 s->matched_entry = NULL;
6374 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6376 regmatch_t regmatch;
6378 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6382 static const struct got_error *
6383 search_next_ref_view(struct tog_view *view)
6385 struct tog_ref_view_state *s = &view->state.ref;
6386 struct tog_reflist_entry *re = NULL;
6388 if (!view->searching) {
6389 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6393 if (s->matched_entry) {
6394 if (view->searching == TOG_SEARCH_FORWARD) {
6395 if (s->selected_entry)
6396 re = TAILQ_NEXT(s->selected_entry, entry);
6398 re = TAILQ_PREV(s->selected_entry,
6399 tog_reflist_head, entry);
6401 if (s->selected_entry == NULL)
6402 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6404 re = TAILQ_PREV(s->selected_entry,
6405 tog_reflist_head, entry);
6408 if (s->selected_entry)
6409 re = s->selected_entry;
6410 else if (view->searching == TOG_SEARCH_FORWARD)
6411 re = TAILQ_FIRST(&s->refs);
6413 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6418 if (s->matched_entry == NULL) {
6419 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6422 if (view->searching == TOG_SEARCH_FORWARD)
6423 re = TAILQ_FIRST(&s->refs);
6425 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6428 if (match_reflist_entry(re, &view->regex)) {
6429 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6430 s->matched_entry = re;
6434 if (view->searching == TOG_SEARCH_FORWARD)
6435 re = TAILQ_NEXT(re, entry);
6437 re = TAILQ_PREV(re, tog_reflist_head, entry);
6440 if (s->matched_entry) {
6441 s->first_displayed_entry = s->matched_entry;
6448 static const struct got_error *
6449 show_ref_view(struct tog_view *view)
6451 const struct got_error *err = NULL;
6452 struct tog_ref_view_state *s = &view->state.ref;
6453 struct tog_reflist_entry *re;
6456 struct tog_color *tc;
6458 int limit = view->nlines;
6460 werase(view->window);
6467 re = s->first_displayed_entry;
6469 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6471 return got_error_from_errno("asprintf");
6473 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6478 if (view_needs_focus_indication(view))
6479 wstandout(view->window);
6480 waddwstr(view->window, wline);
6481 if (view_needs_focus_indication(view))
6482 wstandend(view->window);
6487 if (width < view->ncols - 1)
6488 waddch(view->window, '\n');
6493 while (re && limit > 0) {
6495 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6498 struct got_commit_object *ci;
6499 struct got_tag_object *tag;
6500 struct got_object_id *id;
6504 err = got_ref_resolve(&id, s->repo, re->ref);
6507 err = got_object_open_as_tag(&tag, s->repo, id);
6509 if (err->code != GOT_ERR_OBJ_TYPE) {
6513 err = got_object_open_as_commit(&ci, s->repo,
6519 t = got_object_commit_get_committer_time(ci);
6520 got_object_commit_close(ci);
6522 t = got_object_tag_get_tagger_time(tag);
6523 got_object_tag_close(tag);
6526 if (gmtime_r(&t, &tm) == NULL)
6527 return got_error_from_errno("gmtime_r");
6528 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6529 return got_error(GOT_ERR_NO_SPACE);
6531 if (got_ref_is_symbolic(re->ref)) {
6532 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6533 ymd : "", got_ref_get_name(re->ref),
6534 got_ref_get_symref_target(re->ref)) == -1)
6535 return got_error_from_errno("asprintf");
6536 } else if (s->show_ids) {
6537 struct got_object_id *id;
6539 err = got_ref_resolve(&id, s->repo, re->ref);
6542 err = got_object_id_str(&id_str, id);
6547 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6548 got_ref_get_name(re->ref), id_str) == -1) {
6549 err = got_error_from_errno("asprintf");
6556 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6557 got_ref_get_name(re->ref)) == -1)
6558 return got_error_from_errno("asprintf");
6560 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6565 if (n == s->selected) {
6567 wstandout(view->window);
6568 s->selected_entry = re;
6570 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6572 wattr_on(view->window,
6573 COLOR_PAIR(tc->colorpair), NULL);
6574 waddwstr(view->window, wline);
6576 wattr_off(view->window,
6577 COLOR_PAIR(tc->colorpair), NULL);
6578 if (width < view->ncols - 1)
6579 waddch(view->window, '\n');
6580 if (n == s->selected && view->focussed)
6581 wstandend(view->window);
6587 s->last_displayed_entry = re;
6590 re = TAILQ_NEXT(re, entry);
6597 static const struct got_error *
6598 browse_ref_tree(struct tog_view **new_view, int begin_x,
6599 struct tog_reflist_entry *re, struct got_repository *repo)
6601 const struct got_error *err = NULL;
6602 struct got_object_id *commit_id = NULL;
6603 struct tog_view *tree_view;
6607 err = resolve_reflist_entry(&commit_id, re, repo);
6609 if (err->code != GOT_ERR_OBJ_TYPE)
6616 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6617 if (tree_view == NULL) {
6618 err = got_error_from_errno("view_open");
6622 err = open_tree_view(tree_view, commit_id,
6623 got_ref_get_name(re->ref), repo);
6627 *new_view = tree_view;
6632 static const struct got_error *
6633 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6635 const struct got_error *err = NULL;
6636 struct tog_ref_view_state *s = &view->state.ref;
6637 struct tog_view *log_view, *tree_view;
6638 struct tog_reflist_entry *re;
6639 int begin_x = 0, n, nscroll = view->nlines - 1;
6643 s->show_ids = !s->show_ids;
6646 s->show_date = !s->show_date;
6649 s->sort_by_date = !s->sort_by_date;
6650 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6651 got_ref_cmp_by_commit_timestamp_descending :
6652 tog_ref_cmp_by_name, s->repo);
6655 got_reflist_object_id_map_free(tog_refs_idmap);
6656 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6657 &tog_refs, s->repo);
6660 ref_view_free_refs(s);
6661 err = ref_view_load_refs(s);
6665 if (!s->selected_entry)
6667 if (view_is_parent_view(view))
6668 begin_x = view_split_begin_x(view->begin_x);
6669 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6672 log_view->focussed = 1;
6673 if (view_is_parent_view(view)) {
6674 err = view_close_child(view);
6677 view_set_child(view, log_view);
6678 view->focus_child = 1;
6680 *new_view = log_view;
6683 if (!s->selected_entry)
6685 if (view_is_parent_view(view))
6686 begin_x = view_split_begin_x(view->begin_x);
6687 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6689 if (err || tree_view == NULL)
6692 tree_view->focussed = 1;
6693 if (view_is_parent_view(view)) {
6694 err = view_close_child(view);
6697 view_set_child(view, tree_view);
6698 view->focus_child = 1;
6700 *new_view = tree_view;
6705 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6710 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6711 for (n = 0; n < view->nlines - 1; n++) {
6714 s->first_displayed_entry = re;
6715 re = TAILQ_PREV(re, tog_reflist_head, entry);
6718 s->selected = n - 1;
6723 if (s->selected > 0) {
6727 ref_scroll_up(s, 1);
6735 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6736 s->selected -= MIN(nscroll, s->selected);
6737 ref_scroll_up(s, MAX(0, nscroll));
6742 if (s->selected < s->ndisplayed - 1) {
6746 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6747 /* can't scroll any further */
6749 ref_scroll_down(s, 1);
6757 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6758 /* can't scroll any further; move cursor down */
6759 if (s->selected < s->ndisplayed - 1)
6760 s->selected += MIN(nscroll,
6761 s->ndisplayed - s->selected - 1);
6764 ref_scroll_down(s, nscroll);
6768 err = tog_load_refs(s->repo, s->sort_by_date);
6771 ref_view_free_refs(s);
6772 err = ref_view_load_refs(s);
6775 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6776 s->selected = view->nlines - 2;
6789 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6794 static const struct got_error *
6795 cmd_ref(int argc, char *argv[])
6797 const struct got_error *error;
6798 struct got_repository *repo = NULL;
6799 struct got_worktree *worktree = NULL;
6800 char *cwd = NULL, *repo_path = NULL;
6802 struct tog_view *view;
6803 int *pack_fds = NULL;
6805 while ((ch = getopt(argc, argv, "r:")) != -1) {
6808 repo_path = realpath(optarg, NULL);
6809 if (repo_path == NULL)
6810 return got_error_from_errno2("realpath",
6825 error = got_repo_pack_fds_open(&pack_fds);
6829 if (repo_path == NULL) {
6830 cwd = getcwd(NULL, 0);
6832 return got_error_from_errno("getcwd");
6833 error = got_worktree_open(&worktree, cwd);
6834 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6838 strdup(got_worktree_get_repo_path(worktree));
6840 repo_path = strdup(cwd);
6841 if (repo_path == NULL) {
6842 error = got_error_from_errno("strdup");
6847 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6853 error = apply_unveil(got_repo_get_path(repo), NULL);
6857 error = tog_load_refs(repo, 0);
6861 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6863 error = got_error_from_errno("view_open");
6867 error = open_ref_view(view, repo);
6872 /* Release work tree lock. */
6873 got_worktree_close(worktree);
6876 error = view_loop(view);
6881 const struct got_error *close_err = got_repo_close(repo);
6886 const struct got_error *pack_err =
6887 got_repo_pack_fds_close(pack_fds);
6896 list_commands(FILE *fp)
6900 fprintf(fp, "commands:");
6901 for (i = 0; i < nitems(tog_commands); i++) {
6902 const struct tog_cmd *cmd = &tog_commands[i];
6903 fprintf(fp, " %s", cmd->name);
6909 usage(int hflag, int status)
6911 FILE *fp = (status == 0) ? stdout : stderr;
6913 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6916 fprintf(fp, "lazy usage: %s path\n", getprogname());
6923 make_argv(int argc, ...)
6931 argv = calloc(argc, sizeof(char *));
6934 for (i = 0; i < argc; i++) {
6935 argv[i] = strdup(va_arg(ap, char *));
6936 if (argv[i] == NULL)
6945 * Try to convert 'tog path' into a 'tog log path' command.
6946 * The user could simply have mistyped the command rather than knowingly
6947 * provided a path. So check whether argv[0] can in fact be resolved
6948 * to a path in the HEAD commit and print a special error if not.
6949 * This hack is for mpi@ <3
6951 static const struct got_error *
6952 tog_log_with_path(int argc, char *argv[])
6954 const struct got_error *error = NULL, *close_err;
6955 const struct tog_cmd *cmd = NULL;
6956 struct got_repository *repo = NULL;
6957 struct got_worktree *worktree = NULL;
6958 struct got_object_id *commit_id = NULL, *id = NULL;
6959 struct got_commit_object *commit = NULL;
6960 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6961 char *commit_id_str = NULL, **cmd_argv = NULL;
6962 int *pack_fds = NULL;
6964 cwd = getcwd(NULL, 0);
6966 return got_error_from_errno("getcwd");
6968 error = got_repo_pack_fds_open(&pack_fds);
6972 error = got_worktree_open(&worktree, cwd);
6973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6977 repo_path = strdup(got_worktree_get_repo_path(worktree));
6979 repo_path = strdup(cwd);
6980 if (repo_path == NULL) {
6981 error = got_error_from_errno("strdup");
6985 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6989 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6994 error = tog_load_refs(repo, 0);
6997 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6998 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6999 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7004 got_worktree_close(worktree);
7008 error = got_object_open_as_commit(&commit, repo, commit_id);
7012 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7014 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7016 fprintf(stderr, "%s: '%s' is no known command or path\n",
7017 getprogname(), argv[0]);
7022 close_err = got_repo_close(repo);
7027 error = got_object_id_str(&commit_id_str, commit_id);
7031 cmd = &tog_commands[0]; /* log */
7033 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7034 error = cmd->cmd_main(argc, cmd_argv);
7037 close_err = got_repo_close(repo);
7042 got_object_commit_close(commit);
7044 got_worktree_close(worktree);
7046 const struct got_error *pack_err =
7047 got_repo_pack_fds_close(pack_fds);
7052 free(commit_id_str);
7059 for (i = 0; i < argc; i++)
7068 main(int argc, char *argv[])
7070 const struct got_error *error = NULL;
7071 const struct tog_cmd *cmd = NULL;
7072 int ch, hflag = 0, Vflag = 0;
7073 char **cmd_argv = NULL;
7074 static const struct option longopts[] = {
7075 { "version", no_argument, NULL, 'V' },
7079 setlocale(LC_CTYPE, "");
7081 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7101 got_version_print_str();
7106 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7114 /* Build an argument vector which runs a default command. */
7115 cmd = &tog_commands[0];
7117 cmd_argv = make_argv(argc, cmd->name);
7121 /* Did the user specify a command? */
7122 for (i = 0; i < nitems(tog_commands); i++) {
7123 if (strncmp(tog_commands[i].name, argv[0],
7124 strlen(argv[0])) == 0) {
7125 cmd = &tog_commands[i];
7134 /* No command specified; try log with a path */
7135 error = tog_log_with_path(argc, argv);
7140 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7147 for (i = 0; i < argc; i++)
7152 if (error && error->code != GOT_ERR_CANCELLED)
7153 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);