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;
343 struct commit_queue_entry **first_displayed_entry;
344 struct commit_queue_entry **selected_entry;
346 int *search_next_done;
350 struct tog_log_view_state {
351 struct commit_queue commits;
352 struct commit_queue_entry *first_displayed_entry;
353 struct commit_queue_entry *last_displayed_entry;
354 struct commit_queue_entry *selected_entry;
359 struct got_repository *repo;
360 struct got_object_id *start_id;
363 struct tog_log_thread_args thread_args;
364 struct commit_queue_entry *matched_entry;
365 struct commit_queue_entry *search_entry;
366 struct tog_colors colors;
369 #define TOG_COLOR_DIFF_MINUS 1
370 #define TOG_COLOR_DIFF_PLUS 2
371 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
372 #define TOG_COLOR_DIFF_META 4
373 #define TOG_COLOR_TREE_SUBMODULE 5
374 #define TOG_COLOR_TREE_SYMLINK 6
375 #define TOG_COLOR_TREE_DIRECTORY 7
376 #define TOG_COLOR_TREE_EXECUTABLE 8
377 #define TOG_COLOR_COMMIT 9
378 #define TOG_COLOR_AUTHOR 10
379 #define TOG_COLOR_DATE 11
380 #define TOG_COLOR_REFS_HEADS 12
381 #define TOG_COLOR_REFS_TAGS 13
382 #define TOG_COLOR_REFS_REMOTES 14
383 #define TOG_COLOR_REFS_BACKUP 15
385 struct tog_blame_cb_args {
386 struct tog_blame_line *lines; /* one per line */
389 struct tog_view *view;
390 struct got_object_id *commit_id;
394 struct tog_blame_thread_args {
396 struct got_repository *repo;
397 struct tog_blame_cb_args *cb_args;
399 got_cancel_cb cancel_cb;
406 struct tog_blame_line *lines;
410 struct tog_blame_thread_args thread_args;
411 struct tog_blame_cb_args cb_args;
416 struct tog_blame_view_state {
417 int first_displayed_line;
418 int last_displayed_line;
423 struct got_object_id_queue blamed_commits;
424 struct got_object_qid *blamed_commit;
426 struct got_repository *repo;
427 struct got_object_id *commit_id;
428 struct tog_blame blame;
430 struct tog_colors colors;
433 struct tog_parent_tree {
434 TAILQ_ENTRY(tog_parent_tree) entry;
435 struct got_tree_object *tree;
436 struct got_tree_entry *first_displayed_entry;
437 struct got_tree_entry *selected_entry;
441 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
443 struct tog_tree_view_state {
445 struct got_object_id *commit_id;/* commit which this tree belongs to */
446 struct got_tree_object *root; /* the commit's root tree entry */
447 struct got_tree_object *tree; /* currently displayed (sub-)tree */
448 struct got_tree_entry *first_displayed_entry;
449 struct got_tree_entry *last_displayed_entry;
450 struct got_tree_entry *selected_entry;
451 int ndisplayed, selected, show_ids;
452 struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 struct got_repository *repo;
455 struct got_tree_entry *matched_entry;
456 struct tog_colors colors;
459 struct tog_reflist_entry {
460 TAILQ_ENTRY(tog_reflist_entry) entry;
461 struct got_reference *ref;
465 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
467 struct tog_ref_view_state {
468 struct tog_reflist_head refs;
469 struct tog_reflist_entry *first_displayed_entry;
470 struct tog_reflist_entry *last_displayed_entry;
471 struct tog_reflist_entry *selected_entry;
472 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
473 struct got_repository *repo;
474 struct tog_reflist_entry *matched_entry;
475 struct tog_colors colors;
479 * We implement two types of views: parent views and child views.
481 * The 'Tab' key switches focus between a parent view and its child view.
482 * Child views are shown side-by-side to their parent view, provided
483 * there is enough screen estate.
485 * When a new view is opened from within a parent view, this new view
486 * becomes a child view of the parent view, replacing any existing child.
488 * When a new view is opened from within a child view, this new view
489 * becomes a parent view which will obscure the views below until the
490 * user quits the new parent view by typing 'q'.
492 * This list of views contains parent views only.
493 * Child views are only pointed to by their parent view.
495 TAILQ_HEAD(tog_view_list_head, tog_view);
498 TAILQ_ENTRY(tog_view) entry;
501 int nlines, ncols, begin_y, begin_x;
502 int lines, cols; /* copies of LINES and COLS */
503 int focussed; /* Only set on one parent or child view at a time. */
505 struct tog_view *parent;
506 struct tog_view *child;
509 * This flag is initially set on parent views when a new child view
510 * is created. It gets toggled when the 'Tab' key switches focus
511 * between parent and child.
512 * The flag indicates whether focus should be passed on to our child
513 * view if this parent view gets picked for focus after another parent
514 * view was closed. This prevents child views from losing focus in such
519 /* type-specific state */
520 enum tog_view_type type;
522 struct tog_diff_view_state diff;
523 struct tog_log_view_state log;
524 struct tog_blame_view_state blame;
525 struct tog_tree_view_state tree;
526 struct tog_ref_view_state ref;
529 const struct got_error *(*show)(struct tog_view *);
530 const struct got_error *(*input)(struct tog_view **,
531 struct tog_view *, int);
532 const struct got_error *(*close)(struct tog_view *);
534 const struct got_error *(*search_start)(struct tog_view *);
535 const struct got_error *(*search_next)(struct tog_view *);
538 #define TOG_SEARCH_FORWARD 1
539 #define TOG_SEARCH_BACKWARD 2
540 int search_next_done;
541 #define TOG_SEARCH_HAVE_MORE 1
542 #define TOG_SEARCH_NO_MORE 2
543 #define TOG_SEARCH_HAVE_NONE 3
548 static const struct got_error *open_diff_view(struct tog_view *,
549 struct got_object_id *, struct got_object_id *,
550 const char *, const char *, int, int, int, struct tog_view *,
551 struct got_repository *);
552 static const struct got_error *show_diff_view(struct tog_view *);
553 static const struct got_error *input_diff_view(struct tog_view **,
554 struct tog_view *, int);
555 static const struct got_error* close_diff_view(struct tog_view *);
556 static const struct got_error *search_start_diff_view(struct tog_view *);
557 static const struct got_error *search_next_diff_view(struct tog_view *);
559 static const struct got_error *open_log_view(struct tog_view *,
560 struct got_object_id *, struct got_repository *,
561 const char *, const char *, int);
562 static const struct got_error * show_log_view(struct tog_view *);
563 static const struct got_error *input_log_view(struct tog_view **,
564 struct tog_view *, int);
565 static const struct got_error *close_log_view(struct tog_view *);
566 static const struct got_error *search_start_log_view(struct tog_view *);
567 static const struct got_error *search_next_log_view(struct tog_view *);
569 static const struct got_error *open_blame_view(struct tog_view *, char *,
570 struct got_object_id *, struct got_repository *);
571 static const struct got_error *show_blame_view(struct tog_view *);
572 static const struct got_error *input_blame_view(struct tog_view **,
573 struct tog_view *, int);
574 static const struct got_error *close_blame_view(struct tog_view *);
575 static const struct got_error *search_start_blame_view(struct tog_view *);
576 static const struct got_error *search_next_blame_view(struct tog_view *);
578 static const struct got_error *open_tree_view(struct tog_view *,
579 struct got_object_id *, const char *, struct got_repository *);
580 static const struct got_error *show_tree_view(struct tog_view *);
581 static const struct got_error *input_tree_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *close_tree_view(struct tog_view *);
584 static const struct got_error *search_start_tree_view(struct tog_view *);
585 static const struct got_error *search_next_tree_view(struct tog_view *);
587 static const struct got_error *open_ref_view(struct tog_view *,
588 struct got_repository *);
589 static const struct got_error *show_ref_view(struct tog_view *);
590 static const struct got_error *input_ref_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *close_ref_view(struct tog_view *);
593 static const struct got_error *search_start_ref_view(struct tog_view *);
594 static const struct got_error *search_next_ref_view(struct tog_view *);
596 static volatile sig_atomic_t tog_sigwinch_received;
597 static volatile sig_atomic_t tog_sigpipe_received;
598 static volatile sig_atomic_t tog_sigcont_received;
599 static volatile sig_atomic_t tog_sigint_received;
600 static volatile sig_atomic_t tog_sigterm_received;
603 tog_sigwinch(int signo)
605 tog_sigwinch_received = 1;
609 tog_sigpipe(int signo)
611 tog_sigpipe_received = 1;
615 tog_sigcont(int signo)
617 tog_sigcont_received = 1;
621 tog_sigint(int signo)
623 tog_sigint_received = 1;
627 tog_sigterm(int signo)
629 tog_sigterm_received = 1;
633 tog_fatal_signal_received()
635 return (tog_sigpipe_received ||
636 tog_sigint_received || tog_sigint_received);
640 static const struct got_error *
641 view_close(struct tog_view *view)
643 const struct got_error *err = NULL;
646 view_close(view->child);
650 err = view->close(view);
652 del_panel(view->panel);
654 delwin(view->window);
659 static struct tog_view *
660 view_open(int nlines, int ncols, int begin_y, int begin_x,
661 enum tog_view_type type)
663 struct tog_view *view = calloc(1, sizeof(*view));
671 view->nlines = nlines ? nlines : LINES - begin_y;
672 view->ncols = ncols ? ncols : COLS - begin_x;
673 view->begin_y = begin_y;
674 view->begin_x = begin_x;
675 view->window = newwin(nlines, ncols, begin_y, begin_x);
676 if (view->window == NULL) {
680 view->panel = new_panel(view->window);
681 if (view->panel == NULL ||
682 set_panel_userptr(view->panel, view) != OK) {
687 keypad(view->window, TRUE);
692 view_split_begin_x(int begin_x)
694 if (begin_x > 0 || COLS < 120)
696 return (COLS - MAX(COLS / 2, 80));
699 static const struct got_error *view_resize(struct tog_view *);
701 static const struct got_error *
702 view_splitscreen(struct tog_view *view)
704 const struct got_error *err = NULL;
707 view->begin_x = view_split_begin_x(0);
708 view->nlines = LINES;
709 view->ncols = COLS - view->begin_x;
712 err = view_resize(view);
716 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
717 return got_error_from_errno("mvwin");
722 static const struct got_error *
723 view_fullscreen(struct tog_view *view)
725 const struct got_error *err = NULL;
729 view->nlines = LINES;
733 err = view_resize(view);
737 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
738 return got_error_from_errno("mvwin");
744 view_is_parent_view(struct tog_view *view)
746 return view->parent == NULL;
749 static const struct got_error *
750 view_resize(struct tog_view *view)
754 if (view->lines > LINES)
755 nlines = view->nlines - (view->lines - LINES);
757 nlines = view->nlines + (LINES - view->lines);
759 if (view->cols > COLS)
760 ncols = view->ncols - (view->cols - COLS);
762 ncols = view->ncols + (COLS - view->cols);
764 if (wresize(view->window, nlines, ncols) == ERR)
765 return got_error_from_errno("wresize");
766 if (replace_panel(view->panel, view->window) == ERR)
767 return got_error_from_errno("replace_panel");
768 wclear(view->window);
770 view->nlines = nlines;
776 view->child->begin_x = view_split_begin_x(view->begin_x);
777 if (view->child->begin_x == 0) {
778 view_fullscreen(view->child);
779 if (view->child->focussed)
780 show_panel(view->child->panel);
782 show_panel(view->panel);
784 view_splitscreen(view->child);
785 show_panel(view->child->panel);
792 static const struct got_error *
793 view_close_child(struct tog_view *view)
795 const struct got_error *err = NULL;
797 if (view->child == NULL)
800 err = view_close(view->child);
806 view_set_child(struct tog_view *view, struct tog_view *child)
809 child->parent = view;
813 view_is_splitscreen(struct tog_view *view)
815 return view->begin_x > 0;
824 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
825 cols = 80; /* Default */
831 resize_term(lines, cols);
834 static const struct got_error *
835 view_search_start(struct tog_view *view)
837 const struct got_error *err = NULL;
841 if (view->search_started) {
842 regfree(&view->regex);
844 memset(&view->regmatch, 0, sizeof(view->regmatch));
846 view->search_started = 0;
848 if (view->nlines < 1)
851 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
852 wclrtoeol(view->window);
856 ret = wgetnstr(view->window, pattern, sizeof(pattern));
862 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
863 err = view->search_start(view);
865 regfree(&view->regex);
868 view->search_started = 1;
869 view->searching = TOG_SEARCH_FORWARD;
870 view->search_next_done = 0;
871 view->search_next(view);
877 static const struct got_error *
878 view_input(struct tog_view **new, int *done, struct tog_view *view,
879 struct tog_view_list_head *views)
881 const struct got_error *err = NULL;
887 /* Clear "no matches" indicator. */
888 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
889 view->search_next_done == TOG_SEARCH_HAVE_NONE)
890 view->search_next_done = TOG_SEARCH_HAVE_MORE;
892 if (view->searching && !view->search_next_done) {
893 errcode = pthread_mutex_unlock(&tog_mutex);
895 return got_error_set_errno(errcode,
896 "pthread_mutex_unlock");
898 errcode = pthread_mutex_lock(&tog_mutex);
900 return got_error_set_errno(errcode,
901 "pthread_mutex_lock");
902 view->search_next(view);
906 nodelay(stdscr, FALSE);
907 /* Allow threads to make progress while we are waiting for input. */
908 errcode = pthread_mutex_unlock(&tog_mutex);
910 return got_error_set_errno(errcode, "pthread_mutex_unlock");
911 ch = wgetch(view->window);
912 errcode = pthread_mutex_lock(&tog_mutex);
914 return got_error_set_errno(errcode, "pthread_mutex_lock");
915 nodelay(stdscr, TRUE);
917 if (tog_sigwinch_received || tog_sigcont_received) {
919 tog_sigwinch_received = 0;
920 tog_sigcont_received = 0;
921 TAILQ_FOREACH(v, views, entry) {
922 err = view_resize(v);
925 err = v->input(new, v, KEY_RESIZE);
929 err = view_resize(v->child);
932 err = v->child->input(new, v->child,
944 view->child->focussed = 1;
945 view->focus_child = 1;
946 } else if (view->parent) {
948 view->parent->focussed = 1;
949 view->parent->focus_child = 0;
953 err = view->input(new, view, ch);
960 if (view_is_parent_view(view)) {
961 if (view->child == NULL)
963 if (view_is_splitscreen(view->child)) {
965 view->child->focussed = 1;
966 err = view_fullscreen(view->child);
968 err = view_splitscreen(view->child);
971 err = view->child->input(new, view->child,
974 if (view_is_splitscreen(view)) {
975 view->parent->focussed = 0;
977 err = view_fullscreen(view);
979 err = view_splitscreen(view);
983 err = view->input(new, view, KEY_RESIZE);
989 if (view->search_start)
990 view_search_start(view);
992 err = view->input(new, view, ch);
996 if (view->search_started && view->search_next) {
997 view->searching = (ch == 'n' ?
998 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
999 view->search_next_done = 0;
1000 view->search_next(view);
1002 err = view->input(new, view, ch);
1005 err = view->input(new, view, ch);
1013 view_vborder(struct tog_view *view)
1016 const struct tog_view *view_above;
1019 return view_vborder(view->parent);
1021 panel = panel_above(view->panel);
1025 view_above = panel_userptr(panel);
1026 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1027 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1031 view_needs_focus_indication(struct tog_view *view)
1033 if (view_is_parent_view(view)) {
1034 if (view->child == NULL || view->child->focussed)
1036 if (!view_is_splitscreen(view->child))
1038 } else if (!view_is_splitscreen(view))
1041 return view->focussed;
1044 static const struct got_error *
1045 view_loop(struct tog_view *view)
1047 const struct got_error *err = NULL;
1048 struct tog_view_list_head views;
1049 struct tog_view *new_view;
1050 int fast_refresh = 10;
1051 int done = 0, errcode;
1053 errcode = pthread_mutex_lock(&tog_mutex);
1055 return got_error_set_errno(errcode, "pthread_mutex_lock");
1058 TAILQ_INSERT_HEAD(&views, view, entry);
1061 err = view->show(view);
1066 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1067 /* Refresh fast during initialization, then become slower. */
1068 if (fast_refresh && fast_refresh-- == 0)
1069 halfdelay(10); /* switch to once per second */
1071 err = view_input(&new_view, &done, view, &views);
1075 struct tog_view *v, *prev = NULL;
1077 if (view_is_parent_view(view))
1078 prev = TAILQ_PREV(view, tog_view_list_head,
1080 else if (view->parent)
1081 prev = view->parent;
1084 view->parent->child = NULL;
1085 view->parent->focus_child = 0;
1087 TAILQ_REMOVE(&views, view, entry);
1089 err = view_close(view);
1094 TAILQ_FOREACH(v, &views, entry) {
1098 if (view == NULL && new_view == NULL) {
1099 /* No view has focus. Try to pick one. */
1102 else if (!TAILQ_EMPTY(&views)) {
1103 view = TAILQ_LAST(&views,
1104 tog_view_list_head);
1107 if (view->focus_child) {
1108 view->child->focussed = 1;
1116 struct tog_view *v, *t;
1117 /* Only allow one parent view per type. */
1118 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1119 if (v->type != new_view->type)
1121 TAILQ_REMOVE(&views, v, entry);
1122 err = view_close(v);
1127 TAILQ_INSERT_TAIL(&views, new_view, entry);
1131 if (view_is_parent_view(view)) {
1132 if (view->child && view->child->focussed)
1135 if (view->parent && view->parent->focussed)
1136 view = view->parent;
1138 show_panel(view->panel);
1139 if (view->child && view_is_splitscreen(view->child))
1140 show_panel(view->child->panel);
1141 if (view->parent && view_is_splitscreen(view)) {
1142 err = view->parent->show(view->parent);
1146 err = view->show(view);
1150 err = view->child->show(view->child);
1159 while (!TAILQ_EMPTY(&views)) {
1160 view = TAILQ_FIRST(&views);
1161 TAILQ_REMOVE(&views, view, entry);
1165 errcode = pthread_mutex_unlock(&tog_mutex);
1166 if (errcode && err == NULL)
1167 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1177 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1182 /* Create newly allocated wide-character string equivalent to a byte string. */
1183 static const struct got_error *
1184 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1187 const struct got_error *err = NULL;
1190 *wlen = mbstowcs(NULL, s, 0);
1191 if (*wlen == (size_t)-1) {
1193 if (errno != EILSEQ)
1194 return got_error_from_errno("mbstowcs");
1196 /* byte string invalid in current encoding; try to "fix" it */
1197 err = got_mbsavis(&vis, &vislen, s);
1200 *wlen = mbstowcs(NULL, vis, 0);
1201 if (*wlen == (size_t)-1) {
1202 err = got_error_from_errno("mbstowcs"); /* give up */
1207 *ws = calloc(*wlen + 1, sizeof(**ws));
1209 err = got_error_from_errno("calloc");
1213 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1214 err = got_error_from_errno("mbstowcs");
1225 /* Format a line for display, ensuring that it won't overflow a width limit. */
1226 static const struct got_error *
1227 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1230 const struct got_error *err = NULL;
1232 wchar_t *wline = NULL;
1239 err = mbs2ws(&wline, &wlen, line);
1243 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1244 wline[wlen - 1] = L'\0';
1247 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1248 wline[wlen - 1] = L'\0';
1254 int width = wcwidth(wline[i]);
1261 if (width == 1 || width == 2) {
1262 if (cols + width > wlimit)
1266 } else if (width == -1) {
1267 if (wline[i] == L'\t') {
1269 ((cols + col_tab_align) % TABSIZE);
1274 if (cols + width > wlimit)
1279 err = got_error_from_errno("wcwidth");
1294 static const struct got_error*
1295 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1296 struct got_object_id *id, struct got_repository *repo)
1298 static const struct got_error *err = NULL;
1299 struct got_reflist_entry *re;
1305 TAILQ_FOREACH(re, refs, entry) {
1306 struct got_tag_object *tag = NULL;
1307 struct got_object_id *ref_id;
1310 name = got_ref_get_name(re->ref);
1311 if (strcmp(name, GOT_REF_HEAD) == 0)
1313 if (strncmp(name, "refs/", 5) == 0)
1315 if (strncmp(name, "got/", 4) == 0 &&
1316 strncmp(name, "got/backup/", 11) != 0)
1318 if (strncmp(name, "heads/", 6) == 0)
1320 if (strncmp(name, "remotes/", 8) == 0) {
1322 s = strstr(name, "/" GOT_REF_HEAD);
1323 if (s != NULL && s[strlen(s)] == '\0')
1326 err = got_ref_resolve(&ref_id, repo, re->ref);
1329 if (strncmp(name, "tags/", 5) == 0) {
1330 err = got_object_open_as_tag(&tag, repo, ref_id);
1332 if (err->code != GOT_ERR_OBJ_TYPE) {
1336 /* Ref points at something other than a tag. */
1341 cmp = got_object_id_cmp(tag ?
1342 got_object_tag_get_object_id(tag) : ref_id, id);
1345 got_object_tag_close(tag);
1349 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1350 s ? ", " : "", name) == -1) {
1351 err = got_error_from_errno("asprintf");
1362 static const struct got_error *
1363 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1368 smallerthan = strchr(author, '<');
1369 if (smallerthan && smallerthan[1] != '\0')
1370 author = smallerthan + 1;
1371 author[strcspn(author, "@>")] = '\0';
1372 return format_line(wauthor, author_width, author, limit, col_tab_align);
1375 static const struct got_error *
1376 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1377 struct got_object_id *id, const size_t date_display_cols,
1378 int author_display_cols)
1380 struct tog_log_view_state *s = &view->state.log;
1381 const struct got_error *err = NULL;
1382 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1383 char *logmsg0 = NULL, *logmsg = NULL;
1384 char *author = NULL;
1385 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1386 int author_width, logmsg_width;
1387 char *newline, *line = NULL;
1389 const int avail = view->ncols;
1391 time_t committer_time;
1392 struct tog_color *tc;
1394 committer_time = got_object_commit_get_committer_time(commit);
1395 if (gmtime_r(&committer_time, &tm) == NULL)
1396 return got_error_from_errno("gmtime_r");
1397 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1398 return got_error(GOT_ERR_NO_SPACE);
1400 if (avail <= date_display_cols)
1401 limit = MIN(sizeof(datebuf) - 1, avail);
1403 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1404 tc = get_color(&s->colors, TOG_COLOR_DATE);
1406 wattr_on(view->window,
1407 COLOR_PAIR(tc->colorpair), NULL);
1408 waddnstr(view->window, datebuf, limit);
1410 wattr_off(view->window,
1411 COLOR_PAIR(tc->colorpair), NULL);
1418 err = got_object_id_str(&id_str, id);
1421 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1423 wattr_on(view->window,
1424 COLOR_PAIR(tc->colorpair), NULL);
1425 wprintw(view->window, "%.8s ", id_str);
1427 wattr_off(view->window,
1428 COLOR_PAIR(tc->colorpair), NULL);
1435 author = strdup(got_object_commit_get_author(commit));
1436 if (author == NULL) {
1437 err = got_error_from_errno("strdup");
1440 err = format_author(&wauthor, &author_width, author, avail - col, col);
1443 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1445 wattr_on(view->window,
1446 COLOR_PAIR(tc->colorpair), NULL);
1447 waddwstr(view->window, wauthor);
1449 wattr_off(view->window,
1450 COLOR_PAIR(tc->colorpair), NULL);
1451 col += author_width;
1452 while (col < avail && author_width < author_display_cols + 2) {
1453 waddch(view->window, ' ');
1460 err = got_object_commit_get_logmsg(&logmsg0, commit);
1464 while (*logmsg == '\n')
1466 newline = strchr(logmsg, '\n');
1469 limit = avail - col;
1470 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1473 waddwstr(view->window, wlogmsg);
1474 col += logmsg_width;
1475 while (col < avail) {
1476 waddch(view->window, ' ');
1488 static struct commit_queue_entry *
1489 alloc_commit_queue_entry(struct got_commit_object *commit,
1490 struct got_object_id *id)
1492 struct commit_queue_entry *entry;
1494 entry = calloc(1, sizeof(*entry));
1499 entry->commit = commit;
1504 pop_commit(struct commit_queue *commits)
1506 struct commit_queue_entry *entry;
1508 entry = TAILQ_FIRST(&commits->head);
1509 TAILQ_REMOVE(&commits->head, entry, entry);
1510 got_object_commit_close(entry->commit);
1511 commits->ncommits--;
1512 /* Don't free entry->id! It is owned by the commit graph. */
1517 free_commits(struct commit_queue *commits)
1519 while (!TAILQ_EMPTY(&commits->head))
1520 pop_commit(commits);
1523 static const struct got_error *
1524 match_commit(int *have_match, struct got_object_id *id,
1525 struct got_commit_object *commit, regex_t *regex)
1527 const struct got_error *err = NULL;
1528 regmatch_t regmatch;
1529 char *id_str = NULL, *logmsg = NULL;
1533 err = got_object_id_str(&id_str, id);
1537 err = got_object_commit_get_logmsg(&logmsg, commit);
1541 if (regexec(regex, got_object_commit_get_author(commit), 1,
1542 ®match, 0) == 0 ||
1543 regexec(regex, got_object_commit_get_committer(commit), 1,
1544 ®match, 0) == 0 ||
1545 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1546 regexec(regex, logmsg, 1, ®match, 0) == 0)
1554 static const struct got_error *
1555 queue_commits(struct tog_log_thread_args *a)
1557 const struct got_error *err = NULL;
1560 * We keep all commits open throughout the lifetime of the log
1561 * view in order to avoid having to re-fetch commits from disk
1562 * while updating the display.
1565 struct got_object_id *id;
1566 struct got_commit_object *commit;
1567 struct commit_queue_entry *entry;
1570 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1572 if (err || id == NULL)
1575 err = got_object_open_as_commit(&commit, a->repo, id);
1578 entry = alloc_commit_queue_entry(commit, id);
1579 if (entry == NULL) {
1580 err = got_error_from_errno("alloc_commit_queue_entry");
1584 errcode = pthread_mutex_lock(&tog_mutex);
1586 err = got_error_set_errno(errcode,
1587 "pthread_mutex_lock");
1591 entry->idx = a->commits->ncommits;
1592 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1593 a->commits->ncommits++;
1595 if (*a->searching == TOG_SEARCH_FORWARD &&
1596 !*a->search_next_done) {
1598 err = match_commit(&have_match, id, commit, a->regex);
1602 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1605 errcode = pthread_mutex_unlock(&tog_mutex);
1606 if (errcode && err == NULL)
1607 err = got_error_set_errno(errcode,
1608 "pthread_mutex_unlock");
1611 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1617 select_commit(struct tog_log_view_state *s)
1619 struct commit_queue_entry *entry;
1622 entry = s->first_displayed_entry;
1624 if (ncommits == s->selected) {
1625 s->selected_entry = entry;
1628 entry = TAILQ_NEXT(entry, entry);
1633 static const struct got_error *
1634 draw_commits(struct tog_view *view)
1636 const struct got_error *err = NULL;
1637 struct tog_log_view_state *s = &view->state.log;
1638 struct commit_queue_entry *entry = s->selected_entry;
1639 const int limit = view->nlines;
1641 int ncommits, author_cols = 4;
1642 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1643 char *refs_str = NULL;
1645 struct tog_color *tc;
1646 static const size_t date_display_cols = 12;
1648 if (s->selected_entry &&
1649 !(view->searching && view->search_next_done == 0)) {
1650 struct got_reflist_head *refs;
1651 err = got_object_id_str(&id_str, s->selected_entry->id);
1654 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1655 s->selected_entry->id);
1657 err = build_refs_str(&refs_str, refs,
1658 s->selected_entry->id, s->repo);
1664 if (s->thread_args.commits_needed == 0)
1665 halfdelay(10); /* disable fast refresh */
1667 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1668 if (asprintf(&ncommits_str, " [%d/%d] %s",
1669 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1670 (view->searching && !view->search_next_done) ?
1671 "searching..." : "loading...") == -1) {
1672 err = got_error_from_errno("asprintf");
1676 const char *search_str = NULL;
1678 if (view->searching) {
1679 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1680 search_str = "no more matches";
1681 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1682 search_str = "no matches found";
1683 else if (!view->search_next_done)
1684 search_str = "searching...";
1687 if (asprintf(&ncommits_str, " [%d/%d] %s",
1688 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1689 search_str ? search_str :
1690 (refs_str ? refs_str : "")) == -1) {
1691 err = got_error_from_errno("asprintf");
1696 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1697 if (asprintf(&header, "commit %s %s%s",
1698 id_str ? id_str : "........................................",
1699 s->in_repo_path, ncommits_str) == -1) {
1700 err = got_error_from_errno("asprintf");
1704 } else if (asprintf(&header, "commit %s%s",
1705 id_str ? id_str : "........................................",
1706 ncommits_str) == -1) {
1707 err = got_error_from_errno("asprintf");
1711 err = format_line(&wline, &width, header, view->ncols, 0);
1715 werase(view->window);
1717 if (view_needs_focus_indication(view))
1718 wstandout(view->window);
1719 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1721 wattr_on(view->window,
1722 COLOR_PAIR(tc->colorpair), NULL);
1723 waddwstr(view->window, wline);
1725 wattr_off(view->window,
1726 COLOR_PAIR(tc->colorpair), NULL);
1727 while (width < view->ncols) {
1728 waddch(view->window, ' ');
1731 if (view_needs_focus_indication(view))
1732 wstandend(view->window);
1737 /* Grow author column size if necessary. */
1738 entry = s->first_displayed_entry;
1744 if (ncommits >= limit - 1)
1746 author = strdup(got_object_commit_get_author(entry->commit));
1747 if (author == NULL) {
1748 err = got_error_from_errno("strdup");
1751 err = format_author(&wauthor, &width, author, COLS,
1753 if (author_cols < width)
1754 author_cols = width;
1758 entry = TAILQ_NEXT(entry, entry);
1761 entry = s->first_displayed_entry;
1762 s->last_displayed_entry = s->first_displayed_entry;
1765 if (ncommits >= limit - 1)
1767 if (ncommits == s->selected)
1768 wstandout(view->window);
1769 err = draw_commit(view, entry->commit, entry->id,
1770 date_display_cols, author_cols);
1771 if (ncommits == s->selected)
1772 wstandend(view->window);
1776 s->last_displayed_entry = entry;
1777 entry = TAILQ_NEXT(entry, entry);
1790 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1792 struct commit_queue_entry *entry;
1795 entry = TAILQ_FIRST(&s->commits.head);
1796 if (s->first_displayed_entry == entry)
1799 entry = s->first_displayed_entry;
1800 while (entry && nscrolled < maxscroll) {
1801 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1803 s->first_displayed_entry = entry;
1809 static const struct got_error *
1810 trigger_log_thread(struct tog_view *view, int wait)
1812 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1815 halfdelay(1); /* fast refresh while loading commits */
1817 while (ta->commits_needed > 0 || ta->load_all) {
1818 if (ta->log_complete)
1821 /* Wake the log thread. */
1822 errcode = pthread_cond_signal(&ta->need_commits);
1824 return got_error_set_errno(errcode,
1825 "pthread_cond_signal");
1828 * The mutex will be released while the view loop waits
1829 * in wgetch(), at which time the log thread will run.
1834 /* Display progress update in log view. */
1835 show_log_view(view);
1839 /* Wait right here while next commit is being loaded. */
1840 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1842 return got_error_set_errno(errcode,
1843 "pthread_cond_wait");
1845 /* Display progress update in log view. */
1846 show_log_view(view);
1854 static const struct got_error *
1855 log_scroll_down(struct tog_view *view, int maxscroll)
1857 struct tog_log_view_state *s = &view->state.log;
1858 const struct got_error *err = NULL;
1859 struct commit_queue_entry *pentry;
1860 int nscrolled = 0, ncommits_needed;
1862 if (s->last_displayed_entry == NULL)
1865 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1866 if (s->commits.ncommits < ncommits_needed &&
1867 !s->thread_args.log_complete) {
1869 * Ask the log thread for required amount of commits.
1871 s->thread_args.commits_needed += maxscroll;
1872 err = trigger_log_thread(view, 1);
1878 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1882 s->last_displayed_entry = pentry;
1884 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1887 s->first_displayed_entry = pentry;
1888 } while (++nscrolled < maxscroll);
1893 static const struct got_error *
1894 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1895 struct got_commit_object *commit, struct got_object_id *commit_id,
1896 struct tog_view *log_view, struct got_repository *repo)
1898 const struct got_error *err;
1899 struct got_object_qid *parent_id;
1900 struct tog_view *diff_view;
1902 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1903 if (diff_view == NULL)
1904 return got_error_from_errno("view_open");
1906 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1907 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1908 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1910 *new_view = diff_view;
1914 static const struct got_error *
1915 tree_view_visit_subtree(struct tog_tree_view_state *s,
1916 struct got_tree_object *subtree)
1918 struct tog_parent_tree *parent;
1920 parent = calloc(1, sizeof(*parent));
1922 return got_error_from_errno("calloc");
1924 parent->tree = s->tree;
1925 parent->first_displayed_entry = s->first_displayed_entry;
1926 parent->selected_entry = s->selected_entry;
1927 parent->selected = s->selected;
1928 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1931 s->first_displayed_entry = NULL;
1935 static const struct got_error *
1936 tree_view_walk_path(struct tog_tree_view_state *s,
1937 struct got_commit_object *commit, const char *path)
1939 const struct got_error *err = NULL;
1940 struct got_tree_object *tree = NULL;
1942 char *slash, *subpath = NULL;
1944 /* Walk the path and open corresponding tree objects. */
1947 struct got_tree_entry *te;
1948 struct got_object_id *tree_id;
1954 /* Ensure the correct subtree entry is selected. */
1955 slash = strchr(p, '/');
1957 te_name = strdup(p);
1959 te_name = strndup(p, slash - p);
1960 if (te_name == NULL) {
1961 err = got_error_from_errno("strndup");
1964 te = got_object_tree_find_entry(s->tree, te_name);
1966 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1971 s->first_displayed_entry = s->selected_entry = te;
1973 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1974 break; /* jump to this file's entry */
1976 slash = strchr(p, '/');
1978 subpath = strndup(path, slash - path);
1980 subpath = strdup(path);
1981 if (subpath == NULL) {
1982 err = got_error_from_errno("strdup");
1986 err = got_object_id_by_path(&tree_id, s->repo, commit,
1991 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1996 err = tree_view_visit_subtree(s, tree);
1998 got_object_tree_close(tree);
2012 static const struct got_error *
2013 browse_commit_tree(struct tog_view **new_view, int begin_x,
2014 struct commit_queue_entry *entry, const char *path,
2015 const char *head_ref_name, struct got_repository *repo)
2017 const struct got_error *err = NULL;
2018 struct tog_tree_view_state *s;
2019 struct tog_view *tree_view;
2021 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2022 if (tree_view == NULL)
2023 return got_error_from_errno("view_open");
2025 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2028 s = &tree_view->state.tree;
2030 *new_view = tree_view;
2032 if (got_path_is_root_dir(path))
2035 return tree_view_walk_path(s, entry->commit, path);
2038 static const struct got_error *
2039 block_signals_used_by_main_thread(void)
2044 if (sigemptyset(&sigset) == -1)
2045 return got_error_from_errno("sigemptyset");
2047 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2048 if (sigaddset(&sigset, SIGWINCH) == -1)
2049 return got_error_from_errno("sigaddset");
2050 if (sigaddset(&sigset, SIGCONT) == -1)
2051 return got_error_from_errno("sigaddset");
2052 if (sigaddset(&sigset, SIGINT) == -1)
2053 return got_error_from_errno("sigaddset");
2054 if (sigaddset(&sigset, SIGTERM) == -1)
2055 return got_error_from_errno("sigaddset");
2057 /* ncurses handles SIGTSTP */
2058 if (sigaddset(&sigset, SIGTSTP) == -1)
2059 return got_error_from_errno("sigaddset");
2061 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2063 return got_error_set_errno(errcode, "pthread_sigmask");
2069 log_thread(void *arg)
2071 const struct got_error *err = NULL;
2073 struct tog_log_thread_args *a = arg;
2076 err = block_signals_used_by_main_thread();
2080 while (!done && !err && !tog_fatal_signal_received()) {
2081 err = queue_commits(a);
2083 if (err->code != GOT_ERR_ITER_COMPLETED)
2087 } else if (a->commits_needed > 0 && !a->load_all)
2088 a->commits_needed--;
2090 errcode = pthread_mutex_lock(&tog_mutex);
2092 err = got_error_set_errno(errcode,
2093 "pthread_mutex_lock");
2095 } else if (*a->quit)
2097 else if (*a->first_displayed_entry == NULL) {
2098 *a->first_displayed_entry =
2099 TAILQ_FIRST(&a->commits->head);
2100 *a->selected_entry = *a->first_displayed_entry;
2103 errcode = pthread_cond_signal(&a->commit_loaded);
2105 err = got_error_set_errno(errcode,
2106 "pthread_cond_signal");
2107 pthread_mutex_unlock(&tog_mutex);
2112 a->commits_needed = 0;
2114 if (a->commits_needed == 0 && !a->load_all) {
2115 errcode = pthread_cond_wait(&a->need_commits,
2118 err = got_error_set_errno(errcode,
2119 "pthread_cond_wait");
2125 errcode = pthread_mutex_unlock(&tog_mutex);
2126 if (errcode && err == NULL)
2127 err = got_error_set_errno(errcode,
2128 "pthread_mutex_unlock");
2130 a->log_complete = 1;
2134 static const struct got_error *
2135 stop_log_thread(struct tog_log_view_state *s)
2137 const struct got_error *err = NULL;
2142 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2144 return got_error_set_errno(errcode,
2145 "pthread_cond_signal");
2146 errcode = pthread_mutex_unlock(&tog_mutex);
2148 return got_error_set_errno(errcode,
2149 "pthread_mutex_unlock");
2150 errcode = pthread_join(s->thread, (void **)&err);
2152 return got_error_set_errno(errcode, "pthread_join");
2153 errcode = pthread_mutex_lock(&tog_mutex);
2155 return got_error_set_errno(errcode,
2156 "pthread_mutex_lock");
2160 if (s->thread_args.repo) {
2161 err = got_repo_close(s->thread_args.repo);
2162 s->thread_args.repo = NULL;
2165 if (s->thread_args.graph) {
2166 got_commit_graph_close(s->thread_args.graph);
2167 s->thread_args.graph = NULL;
2173 static const struct got_error *
2174 close_log_view(struct tog_view *view)
2176 const struct got_error *err = NULL;
2177 struct tog_log_view_state *s = &view->state.log;
2180 err = stop_log_thread(s);
2182 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2183 if (errcode && err == NULL)
2184 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2186 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2187 if (errcode && err == NULL)
2188 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2190 free_commits(&s->commits);
2191 free(s->in_repo_path);
2192 s->in_repo_path = NULL;
2195 free(s->head_ref_name);
2196 s->head_ref_name = NULL;
2200 static const struct got_error *
2201 search_start_log_view(struct tog_view *view)
2203 struct tog_log_view_state *s = &view->state.log;
2205 s->matched_entry = NULL;
2206 s->search_entry = NULL;
2210 static const struct got_error *
2211 search_next_log_view(struct tog_view *view)
2213 const struct got_error *err = NULL;
2214 struct tog_log_view_state *s = &view->state.log;
2215 struct commit_queue_entry *entry;
2217 /* Display progress update in log view. */
2218 show_log_view(view);
2222 if (s->search_entry) {
2224 errcode = pthread_mutex_unlock(&tog_mutex);
2226 return got_error_set_errno(errcode,
2227 "pthread_mutex_unlock");
2228 ch = wgetch(view->window);
2229 errcode = pthread_mutex_lock(&tog_mutex);
2231 return got_error_set_errno(errcode,
2232 "pthread_mutex_lock");
2233 if (ch == KEY_BACKSPACE) {
2234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2237 if (view->searching == TOG_SEARCH_FORWARD)
2238 entry = TAILQ_NEXT(s->search_entry, entry);
2240 entry = TAILQ_PREV(s->search_entry,
2241 commit_queue_head, entry);
2242 } else if (s->matched_entry) {
2243 if (view->searching == TOG_SEARCH_FORWARD)
2244 entry = TAILQ_NEXT(s->matched_entry, entry);
2246 entry = TAILQ_PREV(s->matched_entry,
2247 commit_queue_head, entry);
2249 entry = s->selected_entry;
2255 if (entry == NULL) {
2256 if (s->thread_args.log_complete ||
2257 view->searching == TOG_SEARCH_BACKWARD) {
2258 view->search_next_done =
2259 (s->matched_entry == NULL ?
2260 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2261 s->search_entry = NULL;
2265 * Poke the log thread for more commits and return,
2266 * allowing the main loop to make progress. Search
2267 * will resume at s->search_entry once we come back.
2269 s->thread_args.commits_needed++;
2270 return trigger_log_thread(view, 0);
2273 err = match_commit(&have_match, entry->id, entry->commit,
2278 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2279 s->matched_entry = entry;
2283 s->search_entry = entry;
2284 if (view->searching == TOG_SEARCH_FORWARD)
2285 entry = TAILQ_NEXT(entry, entry);
2287 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2290 if (s->matched_entry) {
2291 int cur = s->selected_entry->idx;
2292 while (cur < s->matched_entry->idx) {
2293 err = input_log_view(NULL, view, KEY_DOWN);
2298 while (cur > s->matched_entry->idx) {
2299 err = input_log_view(NULL, view, KEY_UP);
2306 s->search_entry = NULL;
2311 static const struct got_error *
2312 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2313 struct got_repository *repo, const char *head_ref_name,
2314 const char *in_repo_path, int log_branches)
2316 const struct got_error *err = NULL;
2317 struct tog_log_view_state *s = &view->state.log;
2318 struct got_repository *thread_repo = NULL;
2319 struct got_commit_graph *thread_graph = NULL;
2321 int *pack_fds = NULL;
2323 if (in_repo_path != s->in_repo_path) {
2324 free(s->in_repo_path);
2325 s->in_repo_path = strdup(in_repo_path);
2326 if (s->in_repo_path == NULL)
2327 return got_error_from_errno("strdup");
2330 /* The commit queue only contains commits being displayed. */
2331 TAILQ_INIT(&s->commits.head);
2332 s->commits.ncommits = 0;
2335 if (head_ref_name) {
2336 s->head_ref_name = strdup(head_ref_name);
2337 if (s->head_ref_name == NULL) {
2338 err = got_error_from_errno("strdup");
2342 s->start_id = got_object_id_dup(start_id);
2343 if (s->start_id == NULL) {
2344 err = got_error_from_errno("got_object_id_dup");
2347 s->log_branches = log_branches;
2349 STAILQ_INIT(&s->colors);
2350 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2351 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2352 get_color_value("TOG_COLOR_COMMIT"));
2355 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2356 get_color_value("TOG_COLOR_AUTHOR"));
2358 free_colors(&s->colors);
2361 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2362 get_color_value("TOG_COLOR_DATE"));
2364 free_colors(&s->colors);
2369 view->show = show_log_view;
2370 view->input = input_log_view;
2371 view->close = close_log_view;
2372 view->search_start = search_start_log_view;
2373 view->search_next = search_next_log_view;
2375 err = got_repo_pack_fds_open(&pack_fds);
2378 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2382 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2386 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2387 s->repo, NULL, NULL);
2391 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2393 err = got_error_set_errno(errcode, "pthread_cond_init");
2396 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2398 err = got_error_set_errno(errcode, "pthread_cond_init");
2402 s->thread_args.commits_needed = view->nlines;
2403 s->thread_args.graph = thread_graph;
2404 s->thread_args.commits = &s->commits;
2405 s->thread_args.in_repo_path = s->in_repo_path;
2406 s->thread_args.start_id = s->start_id;
2407 s->thread_args.repo = thread_repo;
2408 s->thread_args.log_complete = 0;
2409 s->thread_args.quit = &s->quit;
2410 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2411 s->thread_args.selected_entry = &s->selected_entry;
2412 s->thread_args.searching = &view->searching;
2413 s->thread_args.search_next_done = &view->search_next_done;
2414 s->thread_args.regex = &view->regex;
2417 const struct got_error *pack_err =
2418 got_repo_pack_fds_close(pack_fds);
2423 close_log_view(view);
2427 static const struct got_error *
2428 show_log_view(struct tog_view *view)
2430 const struct got_error *err;
2431 struct tog_log_view_state *s = &view->state.log;
2433 if (s->thread == NULL) {
2434 int errcode = pthread_create(&s->thread, NULL, log_thread,
2437 return got_error_set_errno(errcode, "pthread_create");
2438 if (s->thread_args.commits_needed > 0) {
2439 err = trigger_log_thread(view, 1);
2445 return draw_commits(view);
2448 static const struct got_error *
2449 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2451 const struct got_error *err = NULL;
2452 struct tog_log_view_state *s = &view->state.log;
2453 struct tog_view *diff_view = NULL, *tree_view = NULL;
2454 struct tog_view *ref_view = NULL;
2455 struct commit_queue_entry *entry;
2456 int begin_x = 0, n, nscroll = view->nlines - 1;
2457 int *pack_fds = NULL;
2459 if (s->thread_args.load_all) {
2460 if (ch == KEY_BACKSPACE)
2461 s->thread_args.load_all = 0;
2462 else if (s->thread_args.log_complete) {
2463 s->thread_args.load_all = 0;
2464 log_scroll_down(view, s->commits.ncommits);
2465 s->selected = MIN(view->nlines - 2,
2466 s->commits.ncommits - 1);
2481 if (s->first_displayed_entry == NULL)
2483 if (s->selected > 0)
2486 log_scroll_up(s, 1);
2492 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2501 if (s->first_displayed_entry == NULL)
2503 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2504 s->selected = MAX(0, s->selected - nscroll - 1);
2506 log_scroll_up(s, nscroll);
2514 if (s->first_displayed_entry == NULL)
2516 if (s->selected < MIN(view->nlines - 2,
2517 s->commits.ncommits - 1))
2520 err = log_scroll_down(view, 1);
2528 /* We don't know yet how many commits, so we're forced to
2529 * traverse them all. */
2530 if (!s->thread_args.log_complete) {
2531 s->thread_args.load_all = 1;
2532 return trigger_log_thread(view, 0);
2536 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2537 for (n = 0; n < view->nlines - 1; n++) {
2540 s->first_displayed_entry = entry;
2541 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2544 s->selected = n - 1;
2554 struct commit_queue_entry *first;
2555 first = s->first_displayed_entry;
2558 err = log_scroll_down(view, nscroll);
2561 if (first == s->first_displayed_entry &&
2562 s->selected < MIN(view->nlines - 2,
2563 s->commits.ncommits - 1)) {
2564 /* can't scroll further down */
2565 s->selected += MIN(s->last_displayed_entry->idx -
2566 s->selected_entry->idx, nscroll + 1);
2572 if (s->selected > view->nlines - 2)
2573 s->selected = view->nlines - 2;
2574 if (s->selected > s->commits.ncommits - 1)
2575 s->selected = s->commits.ncommits - 1;
2577 if (s->commits.ncommits < view->nlines - 1 &&
2578 !s->thread_args.log_complete) {
2579 s->thread_args.commits_needed += (view->nlines - 1) -
2580 s->commits.ncommits;
2581 err = trigger_log_thread(view, 1);
2587 if (s->selected_entry == NULL)
2589 if (view_is_parent_view(view))
2590 begin_x = view_split_begin_x(view->begin_x);
2591 err = open_diff_view_for_commit(&diff_view, begin_x,
2592 s->selected_entry->commit, s->selected_entry->id,
2597 diff_view->focussed = 1;
2598 if (view_is_parent_view(view)) {
2599 err = view_close_child(view);
2602 view_set_child(view, diff_view);
2603 view->focus_child = 1;
2605 *new_view = diff_view;
2608 if (s->selected_entry == NULL)
2610 if (view_is_parent_view(view))
2611 begin_x = view_split_begin_x(view->begin_x);
2612 err = browse_commit_tree(&tree_view, begin_x,
2613 s->selected_entry, s->in_repo_path, s->head_ref_name,
2618 tree_view->focussed = 1;
2619 if (view_is_parent_view(view)) {
2620 err = view_close_child(view);
2623 view_set_child(view, tree_view);
2624 view->focus_child = 1;
2626 *new_view = tree_view;
2631 if (ch == KEY_BACKSPACE &&
2632 got_path_is_root_dir(s->in_repo_path))
2634 err = stop_log_thread(s);
2637 if (ch == KEY_BACKSPACE) {
2639 err = got_path_dirname(&parent_path, s->in_repo_path);
2642 free(s->in_repo_path);
2643 s->in_repo_path = parent_path;
2644 s->thread_args.in_repo_path = s->in_repo_path;
2645 } else if (ch == CTRL('l')) {
2646 struct got_object_id *start_id;
2647 err = got_repo_match_object_id(&start_id, NULL,
2648 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2649 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2653 s->start_id = start_id;
2654 s->thread_args.start_id = s->start_id;
2656 s->log_branches = !s->log_branches;
2658 err = got_repo_pack_fds_open(&pack_fds);
2661 err = got_repo_open(&s->thread_args.repo,
2662 got_repo_get_path(s->repo), NULL, pack_fds);
2665 err = got_repo_pack_fds_close(pack_fds);
2669 err = tog_load_refs(s->repo, 0);
2672 err = got_commit_graph_open(&s->thread_args.graph,
2673 s->in_repo_path, !s->log_branches);
2676 err = got_commit_graph_iter_start(s->thread_args.graph,
2677 s->start_id, s->repo, NULL, NULL);
2680 free_commits(&s->commits);
2681 s->first_displayed_entry = NULL;
2682 s->last_displayed_entry = NULL;
2683 s->selected_entry = NULL;
2685 s->thread_args.log_complete = 0;
2687 s->thread_args.commits_needed = view->nlines;
2690 if (view_is_parent_view(view))
2691 begin_x = view_split_begin_x(view->begin_x);
2692 ref_view = view_open(view->nlines, view->ncols,
2693 view->begin_y, begin_x, TOG_VIEW_REF);
2694 if (ref_view == NULL)
2695 return got_error_from_errno("view_open");
2696 err = open_ref_view(ref_view, s->repo);
2698 view_close(ref_view);
2702 ref_view->focussed = 1;
2703 if (view_is_parent_view(view)) {
2704 err = view_close_child(view);
2707 view_set_child(view, ref_view);
2708 view->focus_child = 1;
2710 *new_view = ref_view;
2719 static const struct got_error *
2720 apply_unveil(const char *repo_path, const char *worktree_path)
2722 const struct got_error *error;
2725 if (unveil("gmon.out", "rwc") != 0)
2726 return got_error_from_errno2("unveil", "gmon.out");
2728 if (repo_path && unveil(repo_path, "r") != 0)
2729 return got_error_from_errno2("unveil", repo_path);
2731 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2732 return got_error_from_errno2("unveil", worktree_path);
2734 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2735 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2737 error = got_privsep_unveil_exec_helpers();
2741 if (unveil(NULL, NULL) != 0)
2742 return got_error_from_errno("unveil");
2751 * Override default signal handlers before starting ncurses.
2752 * This should prevent ncurses from installing its own
2753 * broken cleanup() signal handler.
2755 signal(SIGWINCH, tog_sigwinch);
2756 signal(SIGPIPE, tog_sigpipe);
2757 signal(SIGCONT, tog_sigcont);
2758 signal(SIGINT, tog_sigint);
2759 signal(SIGTERM, tog_sigterm);
2763 halfdelay(1); /* Do fast refresh while initial view is loading. */
2766 intrflush(stdscr, FALSE);
2767 keypad(stdscr, TRUE);
2769 if (getenv("TOG_COLORS") != NULL) {
2771 use_default_colors();
2775 static const struct got_error *
2776 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2777 struct got_repository *repo, struct got_worktree *worktree)
2779 const struct got_error *err = NULL;
2782 *in_repo_path = strdup("/");
2783 if (*in_repo_path == NULL)
2784 return got_error_from_errno("strdup");
2789 const char *prefix = got_worktree_get_path_prefix(worktree);
2792 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2795 if (asprintf(in_repo_path, "%s%s%s", prefix,
2796 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2798 err = got_error_from_errno("asprintf");
2799 *in_repo_path = NULL;
2803 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2808 static const struct got_error *
2809 cmd_log(int argc, char *argv[])
2811 const struct got_error *error;
2812 struct got_repository *repo = NULL;
2813 struct got_worktree *worktree = NULL;
2814 struct got_object_id *start_id = NULL;
2815 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2816 char *start_commit = NULL, *label = NULL;
2817 struct got_reference *ref = NULL;
2818 const char *head_ref_name = NULL;
2819 int ch, log_branches = 0;
2820 struct tog_view *view;
2821 int *pack_fds = NULL;
2823 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2829 start_commit = optarg;
2832 repo_path = realpath(optarg, NULL);
2833 if (repo_path == NULL)
2834 return got_error_from_errno2("realpath",
2849 error = got_repo_pack_fds_open(&pack_fds);
2853 if (repo_path == NULL) {
2854 cwd = getcwd(NULL, 0);
2856 return got_error_from_errno("getcwd");
2857 error = got_worktree_open(&worktree, cwd);
2858 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2862 strdup(got_worktree_get_repo_path(worktree));
2864 repo_path = strdup(cwd);
2865 if (repo_path == NULL) {
2866 error = got_error_from_errno("strdup");
2871 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2875 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2882 error = apply_unveil(got_repo_get_path(repo),
2883 worktree ? got_worktree_get_root_path(worktree) : NULL);
2887 /* already loaded by tog_log_with_path()? */
2888 if (TAILQ_EMPTY(&tog_refs)) {
2889 error = tog_load_refs(repo, 0);
2894 if (start_commit == NULL) {
2895 error = got_repo_match_object_id(&start_id, &label,
2896 worktree ? got_worktree_get_head_ref_name(worktree) :
2897 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2900 head_ref_name = label;
2902 error = got_ref_open(&ref, repo, start_commit, 0);
2904 head_ref_name = got_ref_get_name(ref);
2905 else if (error->code != GOT_ERR_NOT_REF)
2907 error = got_repo_match_object_id(&start_id, NULL,
2908 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2913 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2915 error = got_error_from_errno("view_open");
2918 error = open_log_view(view, start_id, repo, head_ref_name,
2919 in_repo_path, log_branches);
2923 /* Release work tree lock. */
2924 got_worktree_close(worktree);
2927 error = view_loop(view);
2937 const struct got_error *close_err = got_repo_close(repo);
2942 got_worktree_close(worktree);
2944 const struct got_error *pack_err =
2945 got_repo_pack_fds_close(pack_fds);
2957 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2958 "[-w] object1 object2\n", getprogname());
2963 match_line(const char *line, regex_t *regex, size_t nmatch,
2964 regmatch_t *regmatch)
2966 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2970 match_color(struct tog_colors *colors, const char *line)
2972 struct tog_color *tc = NULL;
2974 STAILQ_FOREACH(tc, colors, entry) {
2975 if (match_line(line, &tc->regex, 0, NULL))
2982 static const struct got_error *
2983 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2984 WINDOW *window, regmatch_t *regmatch)
2986 const struct got_error *err = NULL;
2993 s = strndup(line, regmatch->rm_so);
2995 return got_error_from_errno("strndup");
2997 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3002 waddwstr(window, wline);
3009 s = strndup(line + regmatch->rm_so,
3010 regmatch->rm_eo - regmatch->rm_so);
3012 err = got_error_from_errno("strndup");
3016 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3021 wattr_on(window, A_STANDOUT, NULL);
3022 waddwstr(window, wline);
3023 wattr_off(window, A_STANDOUT, NULL);
3030 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3031 err = format_line(&wline, &width,
3032 line + regmatch->rm_eo, wlimit, col_tab_align);
3035 waddwstr(window, wline);
3043 static const struct got_error *
3044 draw_file(struct tog_view *view, const char *header)
3046 struct tog_diff_view_state *s = &view->state.diff;
3047 regmatch_t *regmatch = &view->regmatch;
3048 const struct got_error *err;
3051 size_t linesize = 0;
3053 struct tog_color *tc;
3056 int max_lines = view->nlines;
3057 int nlines = s->nlines;
3060 line_offset = s->line_offsets[s->first_displayed_line - 1];
3061 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3062 return got_error_from_errno("fseek");
3064 werase(view->window);
3067 if (asprintf(&line, "[%d/%d] %s",
3068 s->first_displayed_line - 1 + s->selected_line, nlines,
3070 return got_error_from_errno("asprintf");
3071 err = format_line(&wline, &width, line, view->ncols, 0);
3076 if (view_needs_focus_indication(view))
3077 wstandout(view->window);
3078 waddwstr(view->window, wline);
3081 if (view_needs_focus_indication(view))
3082 wstandend(view->window);
3083 if (width <= view->ncols - 1)
3084 waddch(view->window, '\n');
3093 while (max_lines > 0 && nprinted < max_lines) {
3094 linelen = getline(&line, &linesize, s->f);
3095 if (linelen == -1) {
3101 return got_ferror(s->f, GOT_ERR_IO);
3104 tc = match_color(&s->colors, line);
3106 wattr_on(view->window,
3107 COLOR_PAIR(tc->colorpair), NULL);
3108 if (s->first_displayed_line + nprinted == s->matched_line &&
3109 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3110 err = add_matched_line(&width, line, view->ncols, 0,
3111 view->window, regmatch);
3117 err = format_line(&wline, &width, line, view->ncols, 0);
3122 waddwstr(view->window, wline);
3127 wattr_off(view->window,
3128 COLOR_PAIR(tc->colorpair), NULL);
3129 if (width <= view->ncols - 1)
3130 waddch(view->window, '\n');
3135 s->last_displayed_line = s->first_displayed_line +
3138 s->last_displayed_line = s->first_displayed_line;
3143 while (nprinted < view->nlines) {
3144 waddch(view->window, '\n');
3148 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3153 wstandout(view->window);
3154 waddwstr(view->window, wline);
3157 wstandend(view->window);
3164 get_datestr(time_t *time, char *datebuf)
3166 struct tm mytm, *tm;
3169 tm = gmtime_r(time, &mytm);
3172 s = asctime_r(tm, datebuf);
3175 p = strchr(s, '\n');
3181 static const struct got_error *
3182 get_changed_paths(struct got_pathlist_head *paths,
3183 struct got_commit_object *commit, struct got_repository *repo)
3185 const struct got_error *err = NULL;
3186 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3187 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3188 struct got_object_qid *qid;
3190 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3192 struct got_commit_object *pcommit;
3193 err = got_object_open_as_commit(&pcommit, repo,
3198 tree_id1 = got_object_id_dup(
3199 got_object_commit_get_tree_id(pcommit));
3200 if (tree_id1 == NULL) {
3201 got_object_commit_close(pcommit);
3202 return got_error_from_errno("got_object_id_dup");
3204 got_object_commit_close(pcommit);
3209 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3214 tree_id2 = got_object_commit_get_tree_id(commit);
3215 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3219 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3220 got_diff_tree_collect_changed_paths, paths, 0);
3223 got_object_tree_close(tree1);
3225 got_object_tree_close(tree2);
3230 static const struct got_error *
3231 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3235 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3237 return got_error_from_errno("reallocarray");
3239 (*line_offsets)[*nlines] = off;
3244 static const struct got_error *
3245 write_commit_info(off_t **line_offsets, size_t *nlines,
3246 struct got_object_id *commit_id, struct got_reflist_head *refs,
3247 struct got_repository *repo, FILE *outfile)
3249 const struct got_error *err = NULL;
3250 char datebuf[26], *datestr;
3251 struct got_commit_object *commit;
3252 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3253 time_t committer_time;
3254 const char *author, *committer;
3255 char *refs_str = NULL;
3256 struct got_pathlist_head changed_paths;
3257 struct got_pathlist_entry *pe;
3261 TAILQ_INIT(&changed_paths);
3264 err = build_refs_str(&refs_str, refs, commit_id, repo);
3269 err = got_object_open_as_commit(&commit, repo, commit_id);
3273 err = got_object_id_str(&id_str, commit_id);
3275 err = got_error_from_errno("got_object_id_str");
3279 err = add_line_offset(line_offsets, nlines, 0);
3283 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3284 refs_str ? refs_str : "", refs_str ? ")" : "");
3286 err = got_error_from_errno("fprintf");
3290 err = add_line_offset(line_offsets, nlines, outoff);
3294 n = fprintf(outfile, "from: %s\n",
3295 got_object_commit_get_author(commit));
3297 err = got_error_from_errno("fprintf");
3301 err = add_line_offset(line_offsets, nlines, outoff);
3305 committer_time = got_object_commit_get_committer_time(commit);
3306 datestr = get_datestr(&committer_time, datebuf);
3308 n = fprintf(outfile, "date: %s UTC\n", datestr);
3310 err = got_error_from_errno("fprintf");
3314 err = add_line_offset(line_offsets, nlines, outoff);
3318 author = got_object_commit_get_author(commit);
3319 committer = got_object_commit_get_committer(commit);
3320 if (strcmp(author, committer) != 0) {
3321 n = fprintf(outfile, "via: %s\n", committer);
3323 err = got_error_from_errno("fprintf");
3327 err = add_line_offset(line_offsets, nlines, outoff);
3331 if (got_object_commit_get_nparents(commit) > 1) {
3332 const struct got_object_id_queue *parent_ids;
3333 struct got_object_qid *qid;
3335 parent_ids = got_object_commit_get_parent_ids(commit);
3336 STAILQ_FOREACH(qid, parent_ids, entry) {
3337 err = got_object_id_str(&id_str, &qid->id);
3340 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3342 err = got_error_from_errno("fprintf");
3346 err = add_line_offset(line_offsets, nlines, outoff);
3354 err = got_object_commit_get_logmsg(&logmsg, commit);
3358 while ((line = strsep(&s, "\n")) != NULL) {
3359 n = fprintf(outfile, "%s\n", line);
3361 err = got_error_from_errno("fprintf");
3365 err = add_line_offset(line_offsets, nlines, outoff);
3370 err = get_changed_paths(&changed_paths, commit, repo);
3373 TAILQ_FOREACH(pe, &changed_paths, entry) {
3374 struct got_diff_changed_path *cp = pe->data;
3375 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3377 err = got_error_from_errno("fprintf");
3381 err = add_line_offset(line_offsets, nlines, outoff);
3384 free((char *)pe->path);
3388 fputc('\n', outfile);
3390 err = add_line_offset(line_offsets, nlines, outoff);
3392 got_pathlist_free(&changed_paths);
3396 got_object_commit_close(commit);
3398 free(*line_offsets);
3399 *line_offsets = NULL;
3405 static const struct got_error *
3406 create_diff(struct tog_diff_view_state *s)
3408 const struct got_error *err = NULL;
3412 free(s->line_offsets);
3413 s->line_offsets = malloc(sizeof(off_t));
3414 if (s->line_offsets == NULL)
3415 return got_error_from_errno("malloc");
3420 err = got_error_from_errno("got_opentemp");
3423 if (s->f && fclose(s->f) == EOF) {
3424 err = got_error_from_errno("fclose");
3430 err = got_object_get_type(&obj_type, s->repo, s->id1);
3432 err = got_object_get_type(&obj_type, s->repo, s->id2);
3437 case GOT_OBJ_TYPE_BLOB:
3438 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3439 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3440 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3443 case GOT_OBJ_TYPE_TREE:
3444 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3445 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3446 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3448 case GOT_OBJ_TYPE_COMMIT: {
3449 const struct got_object_id_queue *parent_ids;
3450 struct got_object_qid *pid;
3451 struct got_commit_object *commit2;
3452 struct got_reflist_head *refs;
3454 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3457 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3458 /* Show commit info if we're diffing to a parent/root commit. */
3459 if (s->id1 == NULL) {
3460 err = write_commit_info(&s->line_offsets, &s->nlines,
3461 s->id2, refs, s->repo, s->f);
3465 parent_ids = got_object_commit_get_parent_ids(commit2);
3466 STAILQ_FOREACH(pid, parent_ids, entry) {
3467 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3468 err = write_commit_info(
3469 &s->line_offsets, &s->nlines,
3470 s->id2, refs, s->repo, s->f);
3477 got_object_commit_close(commit2);
3479 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3480 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3481 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3485 err = got_error(GOT_ERR_OBJ_TYPE);
3491 if (s->f && fflush(s->f) != 0 && err == NULL)
3492 err = got_error_from_errno("fflush");
3497 diff_view_indicate_progress(struct tog_view *view)
3499 mvwaddstr(view->window, 0, 0, "diffing...");
3504 static const struct got_error *
3505 search_start_diff_view(struct tog_view *view)
3507 struct tog_diff_view_state *s = &view->state.diff;
3509 s->matched_line = 0;
3513 static const struct got_error *
3514 search_next_diff_view(struct tog_view *view)
3516 struct tog_diff_view_state *s = &view->state.diff;
3519 size_t linesize = 0;
3522 if (!view->searching) {
3523 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3527 if (s->matched_line) {
3528 if (view->searching == TOG_SEARCH_FORWARD)
3529 lineno = s->matched_line + 1;
3531 lineno = s->matched_line - 1;
3533 lineno = s->first_displayed_line;
3538 if (lineno <= 0 || lineno > s->nlines) {
3539 if (s->matched_line == 0) {
3540 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3544 if (view->searching == TOG_SEARCH_FORWARD)
3550 offset = s->line_offsets[lineno - 1];
3551 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3553 return got_error_from_errno("fseeko");
3555 linelen = getline(&line, &linesize, s->f);
3556 if (linelen != -1 &&
3557 match_line(line, &view->regex, 1, &view->regmatch)) {
3558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3559 s->matched_line = lineno;
3562 if (view->searching == TOG_SEARCH_FORWARD)
3569 if (s->matched_line) {
3570 s->first_displayed_line = s->matched_line;
3571 s->selected_line = 1;
3577 static const struct got_error *
3578 close_diff_view(struct tog_view *view)
3580 const struct got_error *err = NULL;
3581 struct tog_diff_view_state *s = &view->state.diff;
3587 if (s->f && fclose(s->f) == EOF)
3588 err = got_error_from_errno("fclose");
3590 if (s->f1 && fclose(s->f1) == EOF)
3591 err = got_error_from_errno("fclose");
3593 if (s->f2 && fclose(s->f2) == EOF)
3594 err = got_error_from_errno("fclose");
3596 free_colors(&s->colors);
3597 free(s->line_offsets);
3598 s->line_offsets = NULL;
3603 static const struct got_error *
3604 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3605 struct got_object_id *id2, const char *label1, const char *label2,
3606 int diff_context, int ignore_whitespace, int force_text_diff,
3607 struct tog_view *log_view, struct got_repository *repo)
3609 const struct got_error *err;
3610 struct tog_diff_view_state *s = &view->state.diff;
3612 memset(s, 0, sizeof(*s));
3614 if (id1 != NULL && id2 != NULL) {
3616 err = got_object_get_type(&type1, repo, id1);
3619 err = got_object_get_type(&type2, repo, id2);
3624 return got_error(GOT_ERR_OBJ_TYPE);
3626 s->first_displayed_line = 1;
3627 s->last_displayed_line = view->nlines;
3628 s->selected_line = 1;
3636 s->id1 = got_object_id_dup(id1);
3638 return got_error_from_errno("got_object_id_dup");
3639 s->f1 = got_opentemp();
3640 if (s->f1 == NULL) {
3641 err = got_error_from_errno("got_opentemp");
3647 s->id2 = got_object_id_dup(id2);
3648 if (s->id2 == NULL) {
3649 err = got_error_from_errno("got_object_id_dup");
3653 s->f2 = got_opentemp();
3654 if (s->f2 == NULL) {
3655 err = got_error_from_errno("got_opentemp");
3659 s->first_displayed_line = 1;
3660 s->last_displayed_line = view->nlines;
3661 s->diff_context = diff_context;
3662 s->ignore_whitespace = ignore_whitespace;
3663 s->force_text_diff = force_text_diff;
3664 s->log_view = log_view;
3667 STAILQ_INIT(&s->colors);
3668 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3669 err = add_color(&s->colors,
3670 "^-", TOG_COLOR_DIFF_MINUS,
3671 get_color_value("TOG_COLOR_DIFF_MINUS"));
3674 err = add_color(&s->colors, "^\\+",
3675 TOG_COLOR_DIFF_PLUS,
3676 get_color_value("TOG_COLOR_DIFF_PLUS"));
3679 err = add_color(&s->colors,
3680 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3681 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3685 err = add_color(&s->colors,
3686 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3687 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3688 get_color_value("TOG_COLOR_DIFF_META"));
3692 err = add_color(&s->colors,
3693 "^(from|via): ", TOG_COLOR_AUTHOR,
3694 get_color_value("TOG_COLOR_AUTHOR"));
3698 err = add_color(&s->colors,
3699 "^date: ", TOG_COLOR_DATE,
3700 get_color_value("TOG_COLOR_DATE"));
3705 if (log_view && view_is_splitscreen(view))
3706 show_log_view(log_view); /* draw vborder */
3707 diff_view_indicate_progress(view);
3709 err = create_diff(s);
3711 view->show = show_diff_view;
3712 view->input = input_diff_view;
3713 view->close = close_diff_view;
3714 view->search_start = search_start_diff_view;
3715 view->search_next = search_next_diff_view;
3718 close_diff_view(view);
3722 static const struct got_error *
3723 show_diff_view(struct tog_view *view)
3725 const struct got_error *err;
3726 struct tog_diff_view_state *s = &view->state.diff;
3727 char *id_str1 = NULL, *id_str2, *header;
3728 const char *label1, *label2;
3731 err = got_object_id_str(&id_str1, s->id1);
3734 label1 = s->label1 ? : id_str1;
3736 label1 = "/dev/null";
3738 err = got_object_id_str(&id_str2, s->id2);
3741 label2 = s->label2 ? : id_str2;
3743 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3744 err = got_error_from_errno("asprintf");
3752 err = draw_file(view, header);
3757 static const struct got_error *
3758 set_selected_commit(struct tog_diff_view_state *s,
3759 struct commit_queue_entry *entry)
3761 const struct got_error *err;
3762 const struct got_object_id_queue *parent_ids;
3763 struct got_commit_object *selected_commit;
3764 struct got_object_qid *pid;
3767 s->id2 = got_object_id_dup(entry->id);
3769 return got_error_from_errno("got_object_id_dup");
3771 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3774 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3776 pid = STAILQ_FIRST(parent_ids);
3777 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3778 got_object_commit_close(selected_commit);
3782 static const struct got_error *
3783 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3785 const struct got_error *err = NULL;
3786 struct tog_diff_view_state *s = &view->state.diff;
3787 struct tog_log_view_state *ls;
3788 struct commit_queue_entry *old_selected_entry;
3790 size_t linesize = 0;
3792 int i, nscroll = view->nlines - 1;
3798 s->force_text_diff = !s->force_text_diff;
3800 s->ignore_whitespace = !s->ignore_whitespace;
3801 wclear(view->window);
3802 s->first_displayed_line = 1;
3803 s->last_displayed_line = view->nlines;
3804 s->matched_line = 0;
3805 diff_view_indicate_progress(view);
3806 err = create_diff(s);
3810 s->first_displayed_line = 1;
3817 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3823 if (s->first_displayed_line > 1)
3824 s->first_displayed_line--;
3832 if (s->first_displayed_line == 1)
3835 while (i++ < nscroll && s->first_displayed_line > 1)
3836 s->first_displayed_line--;
3842 s->first_displayed_line++;
3854 while (!s->eof && i++ < nscroll) {
3855 linelen = getline(&line, &linesize, s->f);
3856 s->first_displayed_line++;
3857 if (linelen == -1) {
3861 err = got_ferror(s->f, GOT_ERR_IO);
3868 if (s->diff_context > 0) {
3870 s->matched_line = 0;
3871 diff_view_indicate_progress(view);
3872 err = create_diff(s);
3873 if (s->first_displayed_line + view->nlines - 1 >
3875 s->first_displayed_line = 1;
3876 s->last_displayed_line = view->nlines;
3881 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3883 s->matched_line = 0;
3884 diff_view_indicate_progress(view);
3885 err = create_diff(s);
3890 if (s->log_view == NULL)
3892 ls = &s->log_view->state.log;
3893 old_selected_entry = ls->selected_entry;
3895 err = input_log_view(NULL, s->log_view, KEY_UP);
3899 if (old_selected_entry == ls->selected_entry)
3902 err = set_selected_commit(s, ls->selected_entry);
3906 s->first_displayed_line = 1;
3907 s->last_displayed_line = view->nlines;
3908 s->matched_line = 0;
3910 diff_view_indicate_progress(view);
3911 err = create_diff(s);
3915 if (s->log_view == NULL)
3917 ls = &s->log_view->state.log;
3918 old_selected_entry = ls->selected_entry;
3920 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3924 if (old_selected_entry == ls->selected_entry)
3927 err = set_selected_commit(s, ls->selected_entry);
3931 s->first_displayed_line = 1;
3932 s->last_displayed_line = view->nlines;
3933 s->matched_line = 0;
3935 diff_view_indicate_progress(view);
3936 err = create_diff(s);
3945 static const struct got_error *
3946 cmd_diff(int argc, char *argv[])
3948 const struct got_error *error = NULL;
3949 struct got_repository *repo = NULL;
3950 struct got_worktree *worktree = NULL;
3951 struct got_object_id *id1 = NULL, *id2 = NULL;
3952 char *repo_path = NULL, *cwd = NULL;
3953 char *id_str1 = NULL, *id_str2 = NULL;
3954 char *label1 = NULL, *label2 = NULL;
3955 int diff_context = 3, ignore_whitespace = 0;
3956 int ch, force_text_diff = 0;
3958 struct tog_view *view;
3959 int *pack_fds = NULL;
3961 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3964 force_text_diff = 1;
3967 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3970 errx(1, "number of context lines is %s: %s",
3974 repo_path = realpath(optarg, NULL);
3975 if (repo_path == NULL)
3976 return got_error_from_errno2("realpath",
3978 got_path_strip_trailing_slashes(repo_path);
3981 ignore_whitespace = 1;
3993 usage_diff(); /* TODO show local worktree changes */
3994 } else if (argc == 2) {
4000 error = got_repo_pack_fds_open(&pack_fds);
4004 if (repo_path == NULL) {
4005 cwd = getcwd(NULL, 0);
4007 return got_error_from_errno("getcwd");
4008 error = got_worktree_open(&worktree, cwd);
4009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4013 strdup(got_worktree_get_repo_path(worktree));
4015 repo_path = strdup(cwd);
4016 if (repo_path == NULL) {
4017 error = got_error_from_errno("strdup");
4022 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4028 error = apply_unveil(got_repo_get_path(repo), NULL);
4032 error = tog_load_refs(repo, 0);
4036 error = got_repo_match_object_id(&id1, &label1, id_str1,
4037 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4041 error = got_repo_match_object_id(&id2, &label2, id_str2,
4042 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4046 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4048 error = got_error_from_errno("view_open");
4051 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4052 ignore_whitespace, force_text_diff, NULL, repo);
4055 error = view_loop(view);
4062 const struct got_error *close_err = got_repo_close(repo);
4067 got_worktree_close(worktree);
4069 const struct got_error *pack_err =
4070 got_repo_pack_fds_close(pack_fds);
4082 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4087 struct tog_blame_line {
4089 struct got_object_id *id;
4092 static const struct got_error *
4093 draw_blame(struct tog_view *view)
4095 struct tog_blame_view_state *s = &view->state.blame;
4096 struct tog_blame *blame = &s->blame;
4097 regmatch_t *regmatch = &view->regmatch;
4098 const struct got_error *err;
4099 int lineno = 0, nprinted = 0;
4101 size_t linesize = 0;
4105 struct tog_blame_line *blame_line;
4106 struct got_object_id *prev_id = NULL;
4108 struct tog_color *tc;
4110 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4115 werase(view->window);
4117 if (asprintf(&line, "commit %s", id_str) == -1) {
4118 err = got_error_from_errno("asprintf");
4123 err = format_line(&wline, &width, line, view->ncols, 0);
4128 if (view_needs_focus_indication(view))
4129 wstandout(view->window);
4130 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4132 wattr_on(view->window,
4133 COLOR_PAIR(tc->colorpair), NULL);
4134 waddwstr(view->window, wline);
4136 wattr_off(view->window,
4137 COLOR_PAIR(tc->colorpair), NULL);
4138 if (view_needs_focus_indication(view))
4139 wstandend(view->window);
4142 if (width < view->ncols - 1)
4143 waddch(view->window, '\n');
4145 if (asprintf(&line, "[%d/%d] %s%s",
4146 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4147 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4149 return got_error_from_errno("asprintf");
4152 err = format_line(&wline, &width, line, view->ncols, 0);
4157 waddwstr(view->window, wline);
4160 if (width < view->ncols - 1)
4161 waddch(view->window, '\n');
4164 while (nprinted < view->nlines - 2) {
4165 linelen = getline(&line, &linesize, blame->f);
4166 if (linelen == -1) {
4167 if (feof(blame->f)) {
4172 return got_ferror(blame->f, GOT_ERR_IO);
4174 if (++lineno < s->first_displayed_line)
4177 if (view->focussed && nprinted == s->selected_line - 1)
4178 wstandout(view->window);
4180 if (blame->nlines > 0) {
4181 blame_line = &blame->lines[lineno - 1];
4182 if (blame_line->annotated && prev_id &&
4183 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4185 nprinted == s->selected_line - 1)) {
4186 waddstr(view->window, " ");
4187 } else if (blame_line->annotated) {
4189 err = got_object_id_str(&id_str, blame_line->id);
4194 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4196 wattr_on(view->window,
4197 COLOR_PAIR(tc->colorpair), NULL);
4198 wprintw(view->window, "%.8s", id_str);
4200 wattr_off(view->window,
4201 COLOR_PAIR(tc->colorpair), NULL);
4203 prev_id = blame_line->id;
4205 waddstr(view->window, "........");
4209 waddstr(view->window, "........");
4213 if (view->focussed && nprinted == s->selected_line - 1)
4214 wstandend(view->window);
4215 waddstr(view->window, " ");
4217 if (view->ncols <= 9) {
4219 wline = wcsdup(L"");
4220 if (wline == NULL) {
4221 err = got_error_from_errno("wcsdup");
4225 } else if (s->first_displayed_line + nprinted ==
4227 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4228 err = add_matched_line(&width, line, view->ncols - 9, 9,
4229 view->window, regmatch);
4236 err = format_line(&wline, &width, line,
4237 view->ncols - 9, 9);
4238 waddwstr(view->window, wline);
4244 if (width <= view->ncols - 1)
4245 waddch(view->window, '\n');
4246 if (++nprinted == 1)
4247 s->first_displayed_line = lineno;
4250 s->last_displayed_line = lineno;
4257 static const struct got_error *
4258 blame_cb(void *arg, int nlines, int lineno,
4259 struct got_commit_object *commit, struct got_object_id *id)
4261 const struct got_error *err = NULL;
4262 struct tog_blame_cb_args *a = arg;
4263 struct tog_blame_line *line;
4266 if (nlines != a->nlines ||
4267 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4268 return got_error(GOT_ERR_RANGE);
4270 errcode = pthread_mutex_lock(&tog_mutex);
4272 return got_error_set_errno(errcode, "pthread_mutex_lock");
4274 if (*a->quit) { /* user has quit the blame view */
4275 err = got_error(GOT_ERR_ITER_COMPLETED);
4280 goto done; /* no change in this commit */
4282 line = &a->lines[lineno - 1];
4283 if (line->annotated)
4286 line->id = got_object_id_dup(id);
4287 if (line->id == NULL) {
4288 err = got_error_from_errno("got_object_id_dup");
4291 line->annotated = 1;
4293 errcode = pthread_mutex_unlock(&tog_mutex);
4295 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4300 blame_thread(void *arg)
4302 const struct got_error *err, *close_err;
4303 struct tog_blame_thread_args *ta = arg;
4304 struct tog_blame_cb_args *a = ta->cb_args;
4307 err = block_signals_used_by_main_thread();
4311 err = got_blame(ta->path, a->commit_id, ta->repo,
4312 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4313 if (err && err->code == GOT_ERR_CANCELLED)
4316 errcode = pthread_mutex_lock(&tog_mutex);
4318 return (void *)got_error_set_errno(errcode,
4319 "pthread_mutex_lock");
4321 close_err = got_repo_close(ta->repo);
4327 errcode = pthread_mutex_unlock(&tog_mutex);
4328 if (errcode && err == NULL)
4329 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4334 static struct got_object_id *
4335 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4336 int first_displayed_line, int selected_line)
4338 struct tog_blame_line *line;
4343 line = &lines[first_displayed_line - 1 + selected_line - 1];
4344 if (!line->annotated)
4350 static const struct got_error *
4351 stop_blame(struct tog_blame *blame)
4353 const struct got_error *err = NULL;
4356 if (blame->thread) {
4358 errcode = pthread_mutex_unlock(&tog_mutex);
4360 return got_error_set_errno(errcode,
4361 "pthread_mutex_unlock");
4362 errcode = pthread_join(blame->thread, (void **)&err);
4364 return got_error_set_errno(errcode, "pthread_join");
4365 errcode = pthread_mutex_lock(&tog_mutex);
4367 return got_error_set_errno(errcode,
4368 "pthread_mutex_lock");
4369 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4371 blame->thread = NULL;
4373 if (blame->thread_args.repo) {
4374 const struct got_error *close_err;
4375 close_err = got_repo_close(blame->thread_args.repo);
4378 blame->thread_args.repo = NULL;
4381 if (fclose(blame->f) == EOF && err == NULL)
4382 err = got_error_from_errno("fclose");
4386 for (i = 0; i < blame->nlines; i++)
4387 free(blame->lines[i].id);
4389 blame->lines = NULL;
4391 free(blame->cb_args.commit_id);
4392 blame->cb_args.commit_id = NULL;
4393 if (blame->pack_fds) {
4394 const struct got_error *pack_err =
4395 got_repo_pack_fds_close(blame->pack_fds);
4402 static const struct got_error *
4403 cancel_blame_view(void *arg)
4405 const struct got_error *err = NULL;
4409 errcode = pthread_mutex_lock(&tog_mutex);
4411 return got_error_set_errno(errcode,
4412 "pthread_mutex_unlock");
4415 err = got_error(GOT_ERR_CANCELLED);
4417 errcode = pthread_mutex_unlock(&tog_mutex);
4419 return got_error_set_errno(errcode,
4420 "pthread_mutex_lock");
4425 static const struct got_error *
4426 run_blame(struct tog_view *view)
4428 struct tog_blame_view_state *s = &view->state.blame;
4429 struct tog_blame *blame = &s->blame;
4430 const struct got_error *err = NULL;
4431 struct got_commit_object *commit = NULL;
4432 struct got_blob_object *blob = NULL;
4433 struct got_repository *thread_repo = NULL;
4434 struct got_object_id *obj_id = NULL;
4436 int *pack_fds = NULL;
4438 err = got_object_open_as_commit(&commit, s->repo,
4439 &s->blamed_commit->id);
4443 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4447 err = got_object_get_type(&obj_type, s->repo, obj_id);
4451 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4452 err = got_error(GOT_ERR_OBJ_TYPE);
4456 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4459 blame->f = got_opentemp();
4460 if (blame->f == NULL) {
4461 err = got_error_from_errno("got_opentemp");
4464 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4465 &blame->line_offsets, blame->f, blob);
4468 if (blame->nlines == 0) {
4469 s->blame_complete = 1;
4473 /* Don't include \n at EOF in the blame line count. */
4474 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4477 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4478 if (blame->lines == NULL) {
4479 err = got_error_from_errno("calloc");
4483 err = got_repo_pack_fds_open(&pack_fds);
4486 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4491 blame->pack_fds = pack_fds;
4492 blame->cb_args.view = view;
4493 blame->cb_args.lines = blame->lines;
4494 blame->cb_args.nlines = blame->nlines;
4495 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4496 if (blame->cb_args.commit_id == NULL) {
4497 err = got_error_from_errno("got_object_id_dup");
4500 blame->cb_args.quit = &s->done;
4502 blame->thread_args.path = s->path;
4503 blame->thread_args.repo = thread_repo;
4504 blame->thread_args.cb_args = &blame->cb_args;
4505 blame->thread_args.complete = &s->blame_complete;
4506 blame->thread_args.cancel_cb = cancel_blame_view;
4507 blame->thread_args.cancel_arg = &s->done;
4508 s->blame_complete = 0;
4510 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4511 s->first_displayed_line = 1;
4512 s->last_displayed_line = view->nlines;
4513 s->selected_line = 1;
4515 s->matched_line = 0;
4519 got_object_commit_close(commit);
4521 got_object_blob_close(blob);
4528 static const struct got_error *
4529 open_blame_view(struct tog_view *view, char *path,
4530 struct got_object_id *commit_id, struct got_repository *repo)
4532 const struct got_error *err = NULL;
4533 struct tog_blame_view_state *s = &view->state.blame;
4535 STAILQ_INIT(&s->blamed_commits);
4537 s->path = strdup(path);
4538 if (s->path == NULL)
4539 return got_error_from_errno("strdup");
4541 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4547 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4548 s->first_displayed_line = 1;
4549 s->last_displayed_line = view->nlines;
4550 s->selected_line = 1;
4551 s->blame_complete = 0;
4553 s->commit_id = commit_id;
4554 memset(&s->blame, 0, sizeof(s->blame));
4556 STAILQ_INIT(&s->colors);
4557 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4558 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4559 get_color_value("TOG_COLOR_COMMIT"));
4564 view->show = show_blame_view;
4565 view->input = input_blame_view;
4566 view->close = close_blame_view;
4567 view->search_start = search_start_blame_view;
4568 view->search_next = search_next_blame_view;
4570 return run_blame(view);
4573 static const struct got_error *
4574 close_blame_view(struct tog_view *view)
4576 const struct got_error *err = NULL;
4577 struct tog_blame_view_state *s = &view->state.blame;
4579 if (s->blame.thread)
4580 err = stop_blame(&s->blame);
4582 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4583 struct got_object_qid *blamed_commit;
4584 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4585 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4586 got_object_qid_free(blamed_commit);
4590 free_colors(&s->colors);
4594 static const struct got_error *
4595 search_start_blame_view(struct tog_view *view)
4597 struct tog_blame_view_state *s = &view->state.blame;
4599 s->matched_line = 0;
4603 static const struct got_error *
4604 search_next_blame_view(struct tog_view *view)
4606 struct tog_blame_view_state *s = &view->state.blame;
4609 size_t linesize = 0;
4612 if (!view->searching) {
4613 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4617 if (s->matched_line) {
4618 if (view->searching == TOG_SEARCH_FORWARD)
4619 lineno = s->matched_line + 1;
4621 lineno = s->matched_line - 1;
4623 lineno = s->first_displayed_line - 1 + s->selected_line;
4628 if (lineno <= 0 || lineno > s->blame.nlines) {
4629 if (s->matched_line == 0) {
4630 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4634 if (view->searching == TOG_SEARCH_FORWARD)
4637 lineno = s->blame.nlines;
4640 offset = s->blame.line_offsets[lineno - 1];
4641 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4643 return got_error_from_errno("fseeko");
4645 linelen = getline(&line, &linesize, s->blame.f);
4646 if (linelen != -1 &&
4647 match_line(line, &view->regex, 1, &view->regmatch)) {
4648 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4649 s->matched_line = lineno;
4652 if (view->searching == TOG_SEARCH_FORWARD)
4659 if (s->matched_line) {
4660 s->first_displayed_line = s->matched_line;
4661 s->selected_line = 1;
4667 static const struct got_error *
4668 show_blame_view(struct tog_view *view)
4670 const struct got_error *err = NULL;
4671 struct tog_blame_view_state *s = &view->state.blame;
4674 if (s->blame.thread == NULL && !s->blame_complete) {
4675 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4676 &s->blame.thread_args);
4678 return got_error_set_errno(errcode, "pthread_create");
4680 halfdelay(1); /* fast refresh while annotating */
4683 if (s->blame_complete)
4684 halfdelay(10); /* disable fast refresh */
4686 err = draw_blame(view);
4692 static const struct got_error *
4693 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4695 const struct got_error *err = NULL, *thread_err = NULL;
4696 struct tog_view *diff_view;
4697 struct tog_blame_view_state *s = &view->state.blame;
4698 int begin_x = 0, nscroll = view->nlines - 2;
4706 s->selected_line = 1;
4707 s->first_displayed_line = 1;
4711 if (s->blame.nlines < view->nlines - 2) {
4712 s->selected_line = s->blame.nlines;
4713 s->first_displayed_line = 1;
4715 s->selected_line = view->nlines - 2;
4716 s->first_displayed_line = s->blame.nlines -
4723 if (s->selected_line > 1)
4725 else if (s->selected_line == 1 &&
4726 s->first_displayed_line > 1)
4727 s->first_displayed_line--;
4735 if (s->first_displayed_line == 1) {
4736 s->selected_line = MAX(1, s->selected_line - nscroll);
4739 if (s->first_displayed_line > nscroll)
4740 s->first_displayed_line -= nscroll;
4742 s->first_displayed_line = 1;
4747 if (s->selected_line < view->nlines - 2 &&
4748 s->first_displayed_line +
4749 s->selected_line <= s->blame.nlines)
4751 else if (s->last_displayed_line <
4753 s->first_displayed_line++;
4757 struct got_object_id *id = NULL;
4758 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4759 s->first_displayed_line, s->selected_line);
4763 struct got_commit_object *commit, *pcommit;
4764 struct got_object_qid *pid;
4765 struct got_object_id *blob_id = NULL;
4767 err = got_object_open_as_commit(&commit,
4772 got_object_commit_get_parent_ids(commit));
4774 got_object_commit_close(commit);
4777 /* Check if path history ends here. */
4778 err = got_object_open_as_commit(&pcommit,
4782 err = got_object_id_by_path(&blob_id, s->repo,
4784 got_object_commit_close(pcommit);
4786 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4788 got_object_commit_close(commit);
4791 err = got_object_get_type(&obj_type, s->repo,
4794 /* Can't blame non-blob type objects. */
4795 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4796 got_object_commit_close(commit);
4799 err = got_object_qid_alloc(&s->blamed_commit,
4801 got_object_commit_close(commit);
4803 if (got_object_id_cmp(id,
4804 &s->blamed_commit->id) == 0)
4806 err = got_object_qid_alloc(&s->blamed_commit,
4812 thread_err = stop_blame(&s->blame);
4816 STAILQ_INSERT_HEAD(&s->blamed_commits,
4817 s->blamed_commit, entry);
4818 err = run_blame(view);
4824 struct got_object_qid *first;
4825 first = STAILQ_FIRST(&s->blamed_commits);
4826 if (!got_object_id_cmp(&first->id, s->commit_id))
4829 thread_err = stop_blame(&s->blame);
4833 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4834 got_object_qid_free(s->blamed_commit);
4836 STAILQ_FIRST(&s->blamed_commits);
4837 err = run_blame(view);
4844 struct got_object_id *id = NULL;
4845 struct got_object_qid *pid;
4846 struct got_commit_object *commit = NULL;
4847 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4848 s->first_displayed_line, s->selected_line);
4851 err = got_object_open_as_commit(&commit, s->repo, id);
4855 got_object_commit_get_parent_ids(commit));
4856 if (view_is_parent_view(view))
4857 begin_x = view_split_begin_x(view->begin_x);
4858 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4859 if (diff_view == NULL) {
4860 got_object_commit_close(commit);
4861 err = got_error_from_errno("view_open");
4864 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4865 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4866 got_object_commit_close(commit);
4868 view_close(diff_view);
4872 diff_view->focussed = 1;
4873 if (view_is_parent_view(view)) {
4874 err = view_close_child(view);
4877 view_set_child(view, diff_view);
4878 view->focus_child = 1;
4880 *new_view = diff_view;
4892 if (s->last_displayed_line >= s->blame.nlines &&
4893 s->selected_line >= MIN(s->blame.nlines,
4894 view->nlines - 2)) {
4897 if (s->last_displayed_line >= s->blame.nlines &&
4898 s->selected_line < view->nlines - 2) {
4900 MIN(nscroll, s->last_displayed_line -
4901 s->first_displayed_line - s->selected_line + 1);
4903 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4904 s->first_displayed_line += nscroll;
4906 s->first_displayed_line =
4907 s->blame.nlines - (view->nlines - 3);
4910 if (s->selected_line > view->nlines - 2) {
4911 s->selected_line = MIN(s->blame.nlines,
4918 return thread_err ? thread_err : err;
4921 static const struct got_error *
4922 cmd_blame(int argc, char *argv[])
4924 const struct got_error *error;
4925 struct got_repository *repo = NULL;
4926 struct got_worktree *worktree = NULL;
4927 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4928 char *link_target = NULL;
4929 struct got_object_id *commit_id = NULL;
4930 struct got_commit_object *commit = NULL;
4931 char *commit_id_str = NULL;
4933 struct tog_view *view;
4934 int *pack_fds = NULL;
4936 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4939 commit_id_str = optarg;
4942 repo_path = realpath(optarg, NULL);
4943 if (repo_path == NULL)
4944 return got_error_from_errno2("realpath",
4959 error = got_repo_pack_fds_open(&pack_fds);
4963 if (repo_path == NULL) {
4964 cwd = getcwd(NULL, 0);
4966 return got_error_from_errno("getcwd");
4967 error = got_worktree_open(&worktree, cwd);
4968 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4972 strdup(got_worktree_get_repo_path(worktree));
4974 repo_path = strdup(cwd);
4975 if (repo_path == NULL) {
4976 error = got_error_from_errno("strdup");
4981 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4985 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4992 error = apply_unveil(got_repo_get_path(repo), NULL);
4996 error = tog_load_refs(repo, 0);
5000 if (commit_id_str == NULL) {
5001 struct got_reference *head_ref;
5002 error = got_ref_open(&head_ref, repo, worktree ?
5003 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5006 error = got_ref_resolve(&commit_id, repo, head_ref);
5007 got_ref_close(head_ref);
5009 error = got_repo_match_object_id(&commit_id, NULL,
5010 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5015 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5017 error = got_error_from_errno("view_open");
5021 error = got_object_open_as_commit(&commit, repo, commit_id);
5025 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5030 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5035 /* Release work tree lock. */
5036 got_worktree_close(worktree);
5039 error = view_loop(view);
5047 got_object_commit_close(commit);
5049 got_worktree_close(worktree);
5051 const struct got_error *close_err = got_repo_close(repo);
5056 const struct got_error *pack_err =
5057 got_repo_pack_fds_close(pack_fds);
5065 static const struct got_error *
5066 draw_tree_entries(struct tog_view *view, const char *parent_path)
5068 struct tog_tree_view_state *s = &view->state.tree;
5069 const struct got_error *err = NULL;
5070 struct got_tree_entry *te;
5072 struct tog_color *tc;
5073 int width, n, i, nentries;
5074 int limit = view->nlines;
5078 werase(view->window);
5083 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5086 if (view_needs_focus_indication(view))
5087 wstandout(view->window);
5088 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5090 wattr_on(view->window,
5091 COLOR_PAIR(tc->colorpair), NULL);
5092 waddwstr(view->window, wline);
5094 wattr_off(view->window,
5095 COLOR_PAIR(tc->colorpair), NULL);
5096 if (view_needs_focus_indication(view))
5097 wstandend(view->window);
5100 if (width < view->ncols - 1)
5101 waddch(view->window, '\n');
5104 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5107 waddwstr(view->window, wline);
5110 if (width < view->ncols - 1)
5111 waddch(view->window, '\n');
5114 waddch(view->window, '\n');
5118 if (s->first_displayed_entry == NULL) {
5119 te = got_object_tree_get_first_entry(s->tree);
5120 if (s->selected == 0) {
5122 wstandout(view->window);
5123 s->selected_entry = NULL;
5125 waddstr(view->window, " ..\n"); /* parent directory */
5126 if (s->selected == 0 && view->focussed)
5127 wstandend(view->window);
5134 te = s->first_displayed_entry;
5137 nentries = got_object_tree_get_nentries(s->tree);
5138 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5139 char *line = NULL, *id_str = NULL, *link_target = NULL;
5140 const char *modestr = "";
5143 te = got_object_tree_get_entry(s->tree, i);
5144 mode = got_tree_entry_get_mode(te);
5147 err = got_object_id_str(&id_str,
5148 got_tree_entry_get_id(te));
5150 return got_error_from_errno(
5151 "got_object_id_str");
5153 if (got_object_tree_entry_is_submodule(te))
5155 else if (S_ISLNK(mode)) {
5158 err = got_tree_entry_get_symlink_target(&link_target,
5164 for (i = 0; i < strlen(link_target); i++) {
5165 if (!isprint((unsigned char)link_target[i]))
5166 link_target[i] = '?';
5170 else if (S_ISDIR(mode))
5172 else if (mode & S_IXUSR)
5174 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5175 got_tree_entry_get_name(te), modestr,
5176 link_target ? " -> ": "",
5177 link_target ? link_target : "") == -1) {
5180 return got_error_from_errno("asprintf");
5184 err = format_line(&wline, &width, line, view->ncols, 0);
5189 if (n == s->selected) {
5191 wstandout(view->window);
5192 s->selected_entry = te;
5194 tc = match_color(&s->colors, line);
5196 wattr_on(view->window,
5197 COLOR_PAIR(tc->colorpair), NULL);
5198 waddwstr(view->window, wline);
5200 wattr_off(view->window,
5201 COLOR_PAIR(tc->colorpair), NULL);
5202 if (width < view->ncols - 1)
5203 waddch(view->window, '\n');
5204 if (n == s->selected && view->focussed)
5205 wstandend(view->window);
5211 s->last_displayed_entry = te;
5220 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5222 struct got_tree_entry *te;
5223 int isroot = s->tree == s->root;
5226 if (s->first_displayed_entry == NULL)
5229 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5230 while (i++ < maxscroll) {
5233 s->first_displayed_entry = NULL;
5236 s->first_displayed_entry = te;
5237 te = got_tree_entry_get_prev(s->tree, te);
5242 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5244 struct got_tree_entry *next, *last;
5247 if (s->first_displayed_entry)
5248 next = got_tree_entry_get_next(s->tree,
5249 s->first_displayed_entry);
5251 next = got_object_tree_get_first_entry(s->tree);
5253 last = s->last_displayed_entry;
5254 while (next && last && n++ < maxscroll) {
5255 last = got_tree_entry_get_next(s->tree, last);
5257 s->first_displayed_entry = next;
5258 next = got_tree_entry_get_next(s->tree, next);
5263 static const struct got_error *
5264 tree_entry_path(char **path, struct tog_parent_trees *parents,
5265 struct got_tree_entry *te)
5267 const struct got_error *err = NULL;
5268 struct tog_parent_tree *pt;
5269 size_t len = 2; /* for leading slash and NUL */
5271 TAILQ_FOREACH(pt, parents, entry)
5272 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5275 len += strlen(got_tree_entry_get_name(te));
5277 *path = calloc(1, len);
5279 return got_error_from_errno("calloc");
5282 pt = TAILQ_LAST(parents, tog_parent_trees);
5284 const char *name = got_tree_entry_get_name(pt->selected_entry);
5285 if (strlcat(*path, name, len) >= len) {
5286 err = got_error(GOT_ERR_NO_SPACE);
5289 if (strlcat(*path, "/", len) >= len) {
5290 err = got_error(GOT_ERR_NO_SPACE);
5293 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5296 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5297 err = got_error(GOT_ERR_NO_SPACE);
5309 static const struct got_error *
5310 blame_tree_entry(struct tog_view **new_view, int begin_x,
5311 struct got_tree_entry *te, struct tog_parent_trees *parents,
5312 struct got_object_id *commit_id, struct got_repository *repo)
5314 const struct got_error *err = NULL;
5316 struct tog_view *blame_view;
5320 err = tree_entry_path(&path, parents, te);
5324 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5325 if (blame_view == NULL) {
5326 err = got_error_from_errno("view_open");
5330 err = open_blame_view(blame_view, path, commit_id, repo);
5332 if (err->code == GOT_ERR_CANCELLED)
5334 view_close(blame_view);
5336 *new_view = blame_view;
5342 static const struct got_error *
5343 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5344 struct tog_tree_view_state *s)
5346 struct tog_view *log_view;
5347 const struct got_error *err = NULL;
5352 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5353 if (log_view == NULL)
5354 return got_error_from_errno("view_open");
5356 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5360 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5363 view_close(log_view);
5365 *new_view = log_view;
5370 static const struct got_error *
5371 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5372 const char *head_ref_name, struct got_repository *repo)
5374 const struct got_error *err = NULL;
5375 char *commit_id_str = NULL;
5376 struct tog_tree_view_state *s = &view->state.tree;
5377 struct got_commit_object *commit = NULL;
5379 TAILQ_INIT(&s->parents);
5380 STAILQ_INIT(&s->colors);
5382 s->commit_id = got_object_id_dup(commit_id);
5383 if (s->commit_id == NULL)
5384 return got_error_from_errno("got_object_id_dup");
5386 err = got_object_open_as_commit(&commit, repo, commit_id);
5391 * The root is opened here and will be closed when the view is closed.
5392 * Any visited subtrees and their path-wise parents are opened and
5395 err = got_object_open_as_tree(&s->root, repo,
5396 got_object_commit_get_tree_id(commit));
5401 err = got_object_id_str(&commit_id_str, commit_id);
5405 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5406 err = got_error_from_errno("asprintf");
5410 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5411 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5412 if (head_ref_name) {
5413 s->head_ref_name = strdup(head_ref_name);
5414 if (s->head_ref_name == NULL) {
5415 err = got_error_from_errno("strdup");
5421 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5422 err = add_color(&s->colors, "\\$$",
5423 TOG_COLOR_TREE_SUBMODULE,
5424 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5427 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5428 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5431 err = add_color(&s->colors, "/$",
5432 TOG_COLOR_TREE_DIRECTORY,
5433 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5437 err = add_color(&s->colors, "\\*$",
5438 TOG_COLOR_TREE_EXECUTABLE,
5439 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5443 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5444 get_color_value("TOG_COLOR_COMMIT"));
5449 view->show = show_tree_view;
5450 view->input = input_tree_view;
5451 view->close = close_tree_view;
5452 view->search_start = search_start_tree_view;
5453 view->search_next = search_next_tree_view;
5455 free(commit_id_str);
5457 got_object_commit_close(commit);
5459 close_tree_view(view);
5463 static const struct got_error *
5464 close_tree_view(struct tog_view *view)
5466 struct tog_tree_view_state *s = &view->state.tree;
5468 free_colors(&s->colors);
5469 free(s->tree_label);
5470 s->tree_label = NULL;
5472 s->commit_id = NULL;
5473 free(s->head_ref_name);
5474 s->head_ref_name = NULL;
5475 while (!TAILQ_EMPTY(&s->parents)) {
5476 struct tog_parent_tree *parent;
5477 parent = TAILQ_FIRST(&s->parents);
5478 TAILQ_REMOVE(&s->parents, parent, entry);
5479 if (parent->tree != s->root)
5480 got_object_tree_close(parent->tree);
5484 if (s->tree != NULL && s->tree != s->root)
5485 got_object_tree_close(s->tree);
5487 got_object_tree_close(s->root);
5491 static const struct got_error *
5492 search_start_tree_view(struct tog_view *view)
5494 struct tog_tree_view_state *s = &view->state.tree;
5496 s->matched_entry = NULL;
5501 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5503 regmatch_t regmatch;
5505 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5509 static const struct got_error *
5510 search_next_tree_view(struct tog_view *view)
5512 struct tog_tree_view_state *s = &view->state.tree;
5513 struct got_tree_entry *te = NULL;
5515 if (!view->searching) {
5516 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5520 if (s->matched_entry) {
5521 if (view->searching == TOG_SEARCH_FORWARD) {
5522 if (s->selected_entry)
5523 te = got_tree_entry_get_next(s->tree,
5526 te = got_object_tree_get_first_entry(s->tree);
5528 if (s->selected_entry == NULL)
5529 te = got_object_tree_get_last_entry(s->tree);
5531 te = got_tree_entry_get_prev(s->tree,
5535 if (s->selected_entry)
5536 te = s->selected_entry;
5537 else if (view->searching == TOG_SEARCH_FORWARD)
5538 te = got_object_tree_get_first_entry(s->tree);
5540 te = got_object_tree_get_last_entry(s->tree);
5545 if (s->matched_entry == NULL) {
5546 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5549 if (view->searching == TOG_SEARCH_FORWARD)
5550 te = got_object_tree_get_first_entry(s->tree);
5552 te = got_object_tree_get_last_entry(s->tree);
5555 if (match_tree_entry(te, &view->regex)) {
5556 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5557 s->matched_entry = te;
5561 if (view->searching == TOG_SEARCH_FORWARD)
5562 te = got_tree_entry_get_next(s->tree, te);
5564 te = got_tree_entry_get_prev(s->tree, te);
5567 if (s->matched_entry) {
5568 s->first_displayed_entry = s->matched_entry;
5575 static const struct got_error *
5576 show_tree_view(struct tog_view *view)
5578 const struct got_error *err = NULL;
5579 struct tog_tree_view_state *s = &view->state.tree;
5582 err = tree_entry_path(&parent_path, &s->parents, NULL);
5586 err = draw_tree_entries(view, parent_path);
5593 static const struct got_error *
5594 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5596 const struct got_error *err = NULL;
5597 struct tog_tree_view_state *s = &view->state.tree;
5598 struct tog_view *log_view, *ref_view;
5599 struct got_tree_entry *te;
5600 int begin_x = 0, n, nscroll = view->nlines - 3;
5604 s->show_ids = !s->show_ids;
5607 if (!s->selected_entry)
5609 if (view_is_parent_view(view))
5610 begin_x = view_split_begin_x(view->begin_x);
5611 err = log_selected_tree_entry(&log_view, begin_x, s);
5613 log_view->focussed = 1;
5614 if (view_is_parent_view(view)) {
5615 err = view_close_child(view);
5618 view_set_child(view, log_view);
5619 view->focus_child = 1;
5621 *new_view = log_view;
5624 if (view_is_parent_view(view))
5625 begin_x = view_split_begin_x(view->begin_x);
5626 ref_view = view_open(view->nlines, view->ncols,
5627 view->begin_y, begin_x, TOG_VIEW_REF);
5628 if (ref_view == NULL)
5629 return got_error_from_errno("view_open");
5630 err = open_ref_view(ref_view, s->repo);
5632 view_close(ref_view);
5636 ref_view->focussed = 1;
5637 if (view_is_parent_view(view)) {
5638 err = view_close_child(view);
5641 view_set_child(view, ref_view);
5642 view->focus_child = 1;
5644 *new_view = ref_view;
5649 if (s->tree == s->root)
5650 s->first_displayed_entry =
5651 got_object_tree_get_first_entry(s->tree);
5653 s->first_displayed_entry = NULL;
5658 te = got_object_tree_get_last_entry(s->tree);
5659 for (n = 0; n < view->nlines - 3; n++) {
5661 if(s->tree != s->root) {
5662 s->first_displayed_entry = NULL;
5667 s->first_displayed_entry = te;
5668 te = got_tree_entry_get_prev(s->tree, te);
5671 s->selected = n - 1;
5676 if (s->selected > 0) {
5680 tree_scroll_up(s, 1);
5688 if (s->tree == s->root) {
5689 if (got_object_tree_get_first_entry(s->tree) ==
5690 s->first_displayed_entry)
5691 s->selected -= MIN(s->selected, nscroll);
5693 if (s->first_displayed_entry == NULL)
5694 s->selected -= MIN(s->selected, nscroll);
5696 tree_scroll_up(s, MAX(0, nscroll));
5701 if (s->selected < s->ndisplayed - 1) {
5705 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5707 /* can't scroll any further */
5709 tree_scroll_down(s, 1);
5717 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5719 /* can't scroll any further; move cursor down */
5720 if (s->selected < s->ndisplayed - 1)
5721 s->selected += MIN(nscroll,
5722 s->ndisplayed - s->selected - 1);
5725 tree_scroll_down(s, nscroll);
5730 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5731 struct tog_parent_tree *parent;
5732 /* user selected '..' */
5733 if (s->tree == s->root)
5735 parent = TAILQ_FIRST(&s->parents);
5736 TAILQ_REMOVE(&s->parents, parent,
5738 got_object_tree_close(s->tree);
5739 s->tree = parent->tree;
5740 s->first_displayed_entry =
5741 parent->first_displayed_entry;
5743 parent->selected_entry;
5744 s->selected = parent->selected;
5746 } else if (S_ISDIR(got_tree_entry_get_mode(
5747 s->selected_entry))) {
5748 struct got_tree_object *subtree;
5749 err = got_object_open_as_tree(&subtree, s->repo,
5750 got_tree_entry_get_id(s->selected_entry));
5753 err = tree_view_visit_subtree(s, subtree);
5755 got_object_tree_close(subtree);
5758 } else if (S_ISREG(got_tree_entry_get_mode(
5759 s->selected_entry))) {
5760 struct tog_view *blame_view;
5761 int begin_x = view_is_parent_view(view) ?
5762 view_split_begin_x(view->begin_x) : 0;
5764 err = blame_tree_entry(&blame_view, begin_x,
5765 s->selected_entry, &s->parents,
5766 s->commit_id, s->repo);
5770 blame_view->focussed = 1;
5771 if (view_is_parent_view(view)) {
5772 err = view_close_child(view);
5775 view_set_child(view, blame_view);
5776 view->focus_child = 1;
5778 *new_view = blame_view;
5782 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5783 s->selected = view->nlines - 4;
5796 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5801 static const struct got_error *
5802 cmd_tree(int argc, char *argv[])
5804 const struct got_error *error;
5805 struct got_repository *repo = NULL;
5806 struct got_worktree *worktree = NULL;
5807 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5808 struct got_object_id *commit_id = NULL;
5809 struct got_commit_object *commit = NULL;
5810 const char *commit_id_arg = NULL;
5812 struct got_reference *ref = NULL;
5813 const char *head_ref_name = NULL;
5815 struct tog_view *view;
5816 int *pack_fds = NULL;
5818 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5821 commit_id_arg = optarg;
5824 repo_path = realpath(optarg, NULL);
5825 if (repo_path == NULL)
5826 return got_error_from_errno2("realpath",
5841 error = got_repo_pack_fds_open(&pack_fds);
5845 if (repo_path == NULL) {
5846 cwd = getcwd(NULL, 0);
5848 return got_error_from_errno("getcwd");
5849 error = got_worktree_open(&worktree, cwd);
5850 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5854 strdup(got_worktree_get_repo_path(worktree));
5856 repo_path = strdup(cwd);
5857 if (repo_path == NULL) {
5858 error = got_error_from_errno("strdup");
5863 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5867 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5874 error = apply_unveil(got_repo_get_path(repo), NULL);
5878 error = tog_load_refs(repo, 0);
5882 if (commit_id_arg == NULL) {
5883 error = got_repo_match_object_id(&commit_id, &label,
5884 worktree ? got_worktree_get_head_ref_name(worktree) :
5885 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5888 head_ref_name = label;
5890 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5892 head_ref_name = got_ref_get_name(ref);
5893 else if (error->code != GOT_ERR_NOT_REF)
5895 error = got_repo_match_object_id(&commit_id, NULL,
5896 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5901 error = got_object_open_as_commit(&commit, repo, commit_id);
5905 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5907 error = got_error_from_errno("view_open");
5910 error = open_tree_view(view, commit_id, head_ref_name, repo);
5913 if (!got_path_is_root_dir(in_repo_path)) {
5914 error = tree_view_walk_path(&view->state.tree, commit,
5921 /* Release work tree lock. */
5922 got_worktree_close(worktree);
5925 error = view_loop(view);
5934 const struct got_error *close_err = got_repo_close(repo);
5939 const struct got_error *pack_err =
5940 got_repo_pack_fds_close(pack_fds);
5948 static const struct got_error *
5949 ref_view_load_refs(struct tog_ref_view_state *s)
5951 struct got_reflist_entry *sre;
5952 struct tog_reflist_entry *re;
5955 TAILQ_FOREACH(sre, &tog_refs, entry) {
5956 if (strncmp(got_ref_get_name(sre->ref),
5957 "refs/got/", 9) == 0 &&
5958 strncmp(got_ref_get_name(sre->ref),
5959 "refs/got/backup/", 16) != 0)
5962 re = malloc(sizeof(*re));
5964 return got_error_from_errno("malloc");
5966 re->ref = got_ref_dup(sre->ref);
5967 if (re->ref == NULL)
5968 return got_error_from_errno("got_ref_dup");
5969 re->idx = s->nrefs++;
5970 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5973 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5978 ref_view_free_refs(struct tog_ref_view_state *s)
5980 struct tog_reflist_entry *re;
5982 while (!TAILQ_EMPTY(&s->refs)) {
5983 re = TAILQ_FIRST(&s->refs);
5984 TAILQ_REMOVE(&s->refs, re, entry);
5985 got_ref_close(re->ref);
5990 static const struct got_error *
5991 open_ref_view(struct tog_view *view, struct got_repository *repo)
5993 const struct got_error *err = NULL;
5994 struct tog_ref_view_state *s = &view->state.ref;
5996 s->selected_entry = 0;
5999 TAILQ_INIT(&s->refs);
6000 STAILQ_INIT(&s->colors);
6002 err = ref_view_load_refs(s);
6006 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6007 err = add_color(&s->colors, "^refs/heads/",
6008 TOG_COLOR_REFS_HEADS,
6009 get_color_value("TOG_COLOR_REFS_HEADS"));
6013 err = add_color(&s->colors, "^refs/tags/",
6014 TOG_COLOR_REFS_TAGS,
6015 get_color_value("TOG_COLOR_REFS_TAGS"));
6019 err = add_color(&s->colors, "^refs/remotes/",
6020 TOG_COLOR_REFS_REMOTES,
6021 get_color_value("TOG_COLOR_REFS_REMOTES"));
6025 err = add_color(&s->colors, "^refs/got/backup/",
6026 TOG_COLOR_REFS_BACKUP,
6027 get_color_value("TOG_COLOR_REFS_BACKUP"));
6032 view->show = show_ref_view;
6033 view->input = input_ref_view;
6034 view->close = close_ref_view;
6035 view->search_start = search_start_ref_view;
6036 view->search_next = search_next_ref_view;
6039 free_colors(&s->colors);
6043 static const struct got_error *
6044 close_ref_view(struct tog_view *view)
6046 struct tog_ref_view_state *s = &view->state.ref;
6048 ref_view_free_refs(s);
6049 free_colors(&s->colors);
6054 static const struct got_error *
6055 resolve_reflist_entry(struct got_object_id **commit_id,
6056 struct tog_reflist_entry *re, struct got_repository *repo)
6058 const struct got_error *err = NULL;
6059 struct got_object_id *obj_id;
6060 struct got_tag_object *tag = NULL;
6065 err = got_ref_resolve(&obj_id, repo, re->ref);
6069 err = got_object_get_type(&obj_type, repo, obj_id);
6074 case GOT_OBJ_TYPE_COMMIT:
6075 *commit_id = obj_id;
6077 case GOT_OBJ_TYPE_TAG:
6078 err = got_object_open_as_tag(&tag, repo, obj_id);
6082 err = got_object_get_type(&obj_type, repo,
6083 got_object_tag_get_object_id(tag));
6086 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6087 err = got_error(GOT_ERR_OBJ_TYPE);
6090 *commit_id = got_object_id_dup(
6091 got_object_tag_get_object_id(tag));
6092 if (*commit_id == NULL) {
6093 err = got_error_from_errno("got_object_id_dup");
6098 err = got_error(GOT_ERR_OBJ_TYPE);
6104 got_object_tag_close(tag);
6112 static const struct got_error *
6113 log_ref_entry(struct tog_view **new_view, int begin_x,
6114 struct tog_reflist_entry *re, struct got_repository *repo)
6116 struct tog_view *log_view;
6117 const struct got_error *err = NULL;
6118 struct got_object_id *commit_id = NULL;
6122 err = resolve_reflist_entry(&commit_id, re, repo);
6124 if (err->code != GOT_ERR_OBJ_TYPE)
6130 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6131 if (log_view == NULL) {
6132 err = got_error_from_errno("view_open");
6136 err = open_log_view(log_view, commit_id, repo,
6137 got_ref_get_name(re->ref), "", 0);
6140 view_close(log_view);
6142 *new_view = log_view;
6148 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6150 struct tog_reflist_entry *re;
6153 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6156 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6157 while (i++ < maxscroll) {
6160 s->first_displayed_entry = re;
6161 re = TAILQ_PREV(re, tog_reflist_head, entry);
6166 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6168 struct tog_reflist_entry *next, *last;
6171 if (s->first_displayed_entry)
6172 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6174 next = TAILQ_FIRST(&s->refs);
6176 last = s->last_displayed_entry;
6177 while (next && last && n++ < maxscroll) {
6178 last = TAILQ_NEXT(last, entry);
6180 s->first_displayed_entry = next;
6181 next = TAILQ_NEXT(next, entry);
6186 static const struct got_error *
6187 search_start_ref_view(struct tog_view *view)
6189 struct tog_ref_view_state *s = &view->state.ref;
6191 s->matched_entry = NULL;
6196 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6198 regmatch_t regmatch;
6200 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6204 static const struct got_error *
6205 search_next_ref_view(struct tog_view *view)
6207 struct tog_ref_view_state *s = &view->state.ref;
6208 struct tog_reflist_entry *re = NULL;
6210 if (!view->searching) {
6211 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6215 if (s->matched_entry) {
6216 if (view->searching == TOG_SEARCH_FORWARD) {
6217 if (s->selected_entry)
6218 re = TAILQ_NEXT(s->selected_entry, entry);
6220 re = TAILQ_PREV(s->selected_entry,
6221 tog_reflist_head, entry);
6223 if (s->selected_entry == NULL)
6224 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6226 re = TAILQ_PREV(s->selected_entry,
6227 tog_reflist_head, entry);
6230 if (s->selected_entry)
6231 re = s->selected_entry;
6232 else if (view->searching == TOG_SEARCH_FORWARD)
6233 re = TAILQ_FIRST(&s->refs);
6235 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6240 if (s->matched_entry == NULL) {
6241 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6244 if (view->searching == TOG_SEARCH_FORWARD)
6245 re = TAILQ_FIRST(&s->refs);
6247 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6250 if (match_reflist_entry(re, &view->regex)) {
6251 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6252 s->matched_entry = re;
6256 if (view->searching == TOG_SEARCH_FORWARD)
6257 re = TAILQ_NEXT(re, entry);
6259 re = TAILQ_PREV(re, tog_reflist_head, entry);
6262 if (s->matched_entry) {
6263 s->first_displayed_entry = s->matched_entry;
6270 static const struct got_error *
6271 show_ref_view(struct tog_view *view)
6273 const struct got_error *err = NULL;
6274 struct tog_ref_view_state *s = &view->state.ref;
6275 struct tog_reflist_entry *re;
6278 struct tog_color *tc;
6280 int limit = view->nlines;
6282 werase(view->window);
6289 re = s->first_displayed_entry;
6291 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6293 return got_error_from_errno("asprintf");
6295 err = format_line(&wline, &width, line, view->ncols, 0);
6300 if (view_needs_focus_indication(view))
6301 wstandout(view->window);
6302 waddwstr(view->window, wline);
6303 if (view_needs_focus_indication(view))
6304 wstandend(view->window);
6309 if (width < view->ncols - 1)
6310 waddch(view->window, '\n');
6315 while (re && limit > 0) {
6318 if (got_ref_is_symbolic(re->ref)) {
6319 if (asprintf(&line, "%s -> %s",
6320 got_ref_get_name(re->ref),
6321 got_ref_get_symref_target(re->ref)) == -1)
6322 return got_error_from_errno("asprintf");
6323 } else if (s->show_ids) {
6324 struct got_object_id *id;
6326 err = got_ref_resolve(&id, s->repo, re->ref);
6329 err = got_object_id_str(&id_str, id);
6334 if (asprintf(&line, "%s: %s",
6335 got_ref_get_name(re->ref), id_str) == -1) {
6336 err = got_error_from_errno("asprintf");
6344 line = strdup(got_ref_get_name(re->ref));
6346 return got_error_from_errno("strdup");
6349 err = format_line(&wline, &width, line, view->ncols, 0);
6354 if (n == s->selected) {
6356 wstandout(view->window);
6357 s->selected_entry = re;
6359 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6361 wattr_on(view->window,
6362 COLOR_PAIR(tc->colorpair), NULL);
6363 waddwstr(view->window, wline);
6365 wattr_off(view->window,
6366 COLOR_PAIR(tc->colorpair), NULL);
6367 if (width < view->ncols - 1)
6368 waddch(view->window, '\n');
6369 if (n == s->selected && view->focussed)
6370 wstandend(view->window);
6376 s->last_displayed_entry = re;
6379 re = TAILQ_NEXT(re, entry);
6386 static const struct got_error *
6387 browse_ref_tree(struct tog_view **new_view, int begin_x,
6388 struct tog_reflist_entry *re, struct got_repository *repo)
6390 const struct got_error *err = NULL;
6391 struct got_object_id *commit_id = NULL;
6392 struct tog_view *tree_view;
6396 err = resolve_reflist_entry(&commit_id, re, repo);
6398 if (err->code != GOT_ERR_OBJ_TYPE)
6405 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6406 if (tree_view == NULL) {
6407 err = got_error_from_errno("view_open");
6411 err = open_tree_view(tree_view, commit_id,
6412 got_ref_get_name(re->ref), repo);
6416 *new_view = tree_view;
6421 static const struct got_error *
6422 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6424 const struct got_error *err = NULL;
6425 struct tog_ref_view_state *s = &view->state.ref;
6426 struct tog_view *log_view, *tree_view;
6427 struct tog_reflist_entry *re;
6428 int begin_x = 0, n, nscroll = view->nlines - 1;
6432 s->show_ids = !s->show_ids;
6435 s->sort_by_date = !s->sort_by_date;
6436 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6437 got_ref_cmp_by_commit_timestamp_descending :
6438 tog_ref_cmp_by_name, s->repo);
6441 got_reflist_object_id_map_free(tog_refs_idmap);
6442 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6443 &tog_refs, s->repo);
6446 ref_view_free_refs(s);
6447 err = ref_view_load_refs(s);
6451 if (!s->selected_entry)
6453 if (view_is_parent_view(view))
6454 begin_x = view_split_begin_x(view->begin_x);
6455 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6458 log_view->focussed = 1;
6459 if (view_is_parent_view(view)) {
6460 err = view_close_child(view);
6463 view_set_child(view, log_view);
6464 view->focus_child = 1;
6466 *new_view = log_view;
6469 if (!s->selected_entry)
6471 if (view_is_parent_view(view))
6472 begin_x = view_split_begin_x(view->begin_x);
6473 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6475 if (err || tree_view == NULL)
6478 tree_view->focussed = 1;
6479 if (view_is_parent_view(view)) {
6480 err = view_close_child(view);
6483 view_set_child(view, tree_view);
6484 view->focus_child = 1;
6486 *new_view = tree_view;
6491 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6496 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6497 for (n = 0; n < view->nlines - 1; n++) {
6500 s->first_displayed_entry = re;
6501 re = TAILQ_PREV(re, tog_reflist_head, entry);
6504 s->selected = n - 1;
6509 if (s->selected > 0) {
6513 ref_scroll_up(s, 1);
6521 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6522 s->selected -= MIN(nscroll, s->selected);
6523 ref_scroll_up(s, MAX(0, nscroll));
6528 if (s->selected < s->ndisplayed - 1) {
6532 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6533 /* can't scroll any further */
6535 ref_scroll_down(s, 1);
6543 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6544 /* can't scroll any further; move cursor down */
6545 if (s->selected < s->ndisplayed - 1)
6546 s->selected += MIN(nscroll,
6547 s->ndisplayed - s->selected - 1);
6550 ref_scroll_down(s, nscroll);
6554 err = tog_load_refs(s->repo, s->sort_by_date);
6557 ref_view_free_refs(s);
6558 err = ref_view_load_refs(s);
6561 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6562 s->selected = view->nlines - 2;
6575 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6580 static const struct got_error *
6581 cmd_ref(int argc, char *argv[])
6583 const struct got_error *error;
6584 struct got_repository *repo = NULL;
6585 struct got_worktree *worktree = NULL;
6586 char *cwd = NULL, *repo_path = NULL;
6588 struct tog_view *view;
6589 int *pack_fds = NULL;
6591 while ((ch = getopt(argc, argv, "r:")) != -1) {
6594 repo_path = realpath(optarg, NULL);
6595 if (repo_path == NULL)
6596 return got_error_from_errno2("realpath",
6611 error = got_repo_pack_fds_open(&pack_fds);
6615 if (repo_path == NULL) {
6616 cwd = getcwd(NULL, 0);
6618 return got_error_from_errno("getcwd");
6619 error = got_worktree_open(&worktree, cwd);
6620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6624 strdup(got_worktree_get_repo_path(worktree));
6626 repo_path = strdup(cwd);
6627 if (repo_path == NULL) {
6628 error = got_error_from_errno("strdup");
6633 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6639 error = apply_unveil(got_repo_get_path(repo), NULL);
6643 error = tog_load_refs(repo, 0);
6647 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6649 error = got_error_from_errno("view_open");
6653 error = open_ref_view(view, repo);
6658 /* Release work tree lock. */
6659 got_worktree_close(worktree);
6662 error = view_loop(view);
6667 const struct got_error *close_err = got_repo_close(repo);
6672 const struct got_error *pack_err =
6673 got_repo_pack_fds_close(pack_fds);
6682 list_commands(FILE *fp)
6686 fprintf(fp, "commands:");
6687 for (i = 0; i < nitems(tog_commands); i++) {
6688 const struct tog_cmd *cmd = &tog_commands[i];
6689 fprintf(fp, " %s", cmd->name);
6695 usage(int hflag, int status)
6697 FILE *fp = (status == 0) ? stdout : stderr;
6699 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6702 fprintf(fp, "lazy usage: %s path\n", getprogname());
6709 make_argv(int argc, ...)
6717 argv = calloc(argc, sizeof(char *));
6720 for (i = 0; i < argc; i++) {
6721 argv[i] = strdup(va_arg(ap, char *));
6722 if (argv[i] == NULL)
6731 * Try to convert 'tog path' into a 'tog log path' command.
6732 * The user could simply have mistyped the command rather than knowingly
6733 * provided a path. So check whether argv[0] can in fact be resolved
6734 * to a path in the HEAD commit and print a special error if not.
6735 * This hack is for mpi@ <3
6737 static const struct got_error *
6738 tog_log_with_path(int argc, char *argv[])
6740 const struct got_error *error = NULL, *close_err;
6741 const struct tog_cmd *cmd = NULL;
6742 struct got_repository *repo = NULL;
6743 struct got_worktree *worktree = NULL;
6744 struct got_object_id *commit_id = NULL, *id = NULL;
6745 struct got_commit_object *commit = NULL;
6746 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6747 char *commit_id_str = NULL, **cmd_argv = NULL;
6748 int *pack_fds = NULL;
6750 cwd = getcwd(NULL, 0);
6752 return got_error_from_errno("getcwd");
6754 error = got_repo_pack_fds_open(&pack_fds);
6758 error = got_worktree_open(&worktree, cwd);
6759 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6763 repo_path = strdup(got_worktree_get_repo_path(worktree));
6765 repo_path = strdup(cwd);
6766 if (repo_path == NULL) {
6767 error = got_error_from_errno("strdup");
6771 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6775 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6780 error = tog_load_refs(repo, 0);
6783 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6784 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6785 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6790 got_worktree_close(worktree);
6794 error = got_object_open_as_commit(&commit, repo, commit_id);
6798 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6800 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6802 fprintf(stderr, "%s: '%s' is no known command or path\n",
6803 getprogname(), argv[0]);
6808 close_err = got_repo_close(repo);
6813 error = got_object_id_str(&commit_id_str, commit_id);
6817 cmd = &tog_commands[0]; /* log */
6819 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6820 error = cmd->cmd_main(argc, cmd_argv);
6823 close_err = got_repo_close(repo);
6828 got_object_commit_close(commit);
6830 got_worktree_close(worktree);
6832 const struct got_error *pack_err =
6833 got_repo_pack_fds_close(pack_fds);
6838 free(commit_id_str);
6845 for (i = 0; i < argc; i++)
6854 main(int argc, char *argv[])
6856 const struct got_error *error = NULL;
6857 const struct tog_cmd *cmd = NULL;
6858 int ch, hflag = 0, Vflag = 0;
6859 char **cmd_argv = NULL;
6860 static const struct option longopts[] = {
6861 { "version", no_argument, NULL, 'V' },
6865 setlocale(LC_CTYPE, "");
6867 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6887 got_version_print_str();
6892 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6900 /* Build an argument vector which runs a default command. */
6901 cmd = &tog_commands[0];
6903 cmd_argv = make_argv(argc, cmd->name);
6907 /* Did the user specify a command? */
6908 for (i = 0; i < nitems(tog_commands); i++) {
6909 if (strncmp(tog_commands[i].name, argv[0],
6910 strlen(argv[0])) == 0) {
6911 cmd = &tog_commands[i];
6920 /* No command specified; try log with a path */
6921 error = tog_log_with_path(argc, argv);
6926 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6933 for (i = 0; i < argc; i++)
6938 if (error && error->code != GOT_ERR_CANCELLED)
6939 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);