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;
415 struct tog_blame_view_state {
416 int first_displayed_line;
417 int last_displayed_line;
422 struct got_object_id_queue blamed_commits;
423 struct got_object_qid *blamed_commit;
425 struct got_repository *repo;
426 struct got_object_id *commit_id;
427 struct tog_blame blame;
429 struct tog_colors colors;
432 struct tog_parent_tree {
433 TAILQ_ENTRY(tog_parent_tree) entry;
434 struct got_tree_object *tree;
435 struct got_tree_entry *first_displayed_entry;
436 struct got_tree_entry *selected_entry;
440 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
442 struct tog_tree_view_state {
444 struct got_object_id *commit_id;/* commit which this tree belongs to */
445 struct got_tree_object *root; /* the commit's root tree entry */
446 struct got_tree_object *tree; /* currently displayed (sub-)tree */
447 struct got_tree_entry *first_displayed_entry;
448 struct got_tree_entry *last_displayed_entry;
449 struct got_tree_entry *selected_entry;
450 int ndisplayed, selected, show_ids;
451 struct tog_parent_trees parents; /* parent trees of current sub-tree */
453 struct got_repository *repo;
454 struct got_tree_entry *matched_entry;
455 struct tog_colors colors;
458 struct tog_reflist_entry {
459 TAILQ_ENTRY(tog_reflist_entry) entry;
460 struct got_reference *ref;
464 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
466 struct tog_ref_view_state {
467 struct tog_reflist_head refs;
468 struct tog_reflist_entry *first_displayed_entry;
469 struct tog_reflist_entry *last_displayed_entry;
470 struct tog_reflist_entry *selected_entry;
471 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
472 struct got_repository *repo;
473 struct tog_reflist_entry *matched_entry;
474 struct tog_colors colors;
478 * We implement two types of views: parent views and child views.
480 * The 'Tab' key switches focus between a parent view and its child view.
481 * Child views are shown side-by-side to their parent view, provided
482 * there is enough screen estate.
484 * When a new view is opened from within a parent view, this new view
485 * becomes a child view of the parent view, replacing any existing child.
487 * When a new view is opened from within a child view, this new view
488 * becomes a parent view which will obscure the views below until the
489 * user quits the new parent view by typing 'q'.
491 * This list of views contains parent views only.
492 * Child views are only pointed to by their parent view.
494 TAILQ_HEAD(tog_view_list_head, tog_view);
497 TAILQ_ENTRY(tog_view) entry;
500 int nlines, ncols, begin_y, begin_x;
501 int lines, cols; /* copies of LINES and COLS */
502 int focussed; /* Only set on one parent or child view at a time. */
504 struct tog_view *parent;
505 struct tog_view *child;
508 * This flag is initially set on parent views when a new child view
509 * is created. It gets toggled when the 'Tab' key switches focus
510 * between parent and child.
511 * The flag indicates whether focus should be passed on to our child
512 * view if this parent view gets picked for focus after another parent
513 * view was closed. This prevents child views from losing focus in such
518 /* type-specific state */
519 enum tog_view_type type;
521 struct tog_diff_view_state diff;
522 struct tog_log_view_state log;
523 struct tog_blame_view_state blame;
524 struct tog_tree_view_state tree;
525 struct tog_ref_view_state ref;
528 const struct got_error *(*show)(struct tog_view *);
529 const struct got_error *(*input)(struct tog_view **,
530 struct tog_view *, int);
531 const struct got_error *(*close)(struct tog_view *);
533 const struct got_error *(*search_start)(struct tog_view *);
534 const struct got_error *(*search_next)(struct tog_view *);
537 #define TOG_SEARCH_FORWARD 1
538 #define TOG_SEARCH_BACKWARD 2
539 int search_next_done;
540 #define TOG_SEARCH_HAVE_MORE 1
541 #define TOG_SEARCH_NO_MORE 2
542 #define TOG_SEARCH_HAVE_NONE 3
547 static const struct got_error *open_diff_view(struct tog_view *,
548 struct got_object_id *, struct got_object_id *,
549 const char *, const char *, int, int, int, struct tog_view *,
550 struct got_repository *);
551 static const struct got_error *show_diff_view(struct tog_view *);
552 static const struct got_error *input_diff_view(struct tog_view **,
553 struct tog_view *, int);
554 static const struct got_error* close_diff_view(struct tog_view *);
555 static const struct got_error *search_start_diff_view(struct tog_view *);
556 static const struct got_error *search_next_diff_view(struct tog_view *);
558 static const struct got_error *open_log_view(struct tog_view *,
559 struct got_object_id *, struct got_repository *,
560 const char *, const char *, int);
561 static const struct got_error * show_log_view(struct tog_view *);
562 static const struct got_error *input_log_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error *close_log_view(struct tog_view *);
565 static const struct got_error *search_start_log_view(struct tog_view *);
566 static const struct got_error *search_next_log_view(struct tog_view *);
568 static const struct got_error *open_blame_view(struct tog_view *, char *,
569 struct got_object_id *, struct got_repository *);
570 static const struct got_error *show_blame_view(struct tog_view *);
571 static const struct got_error *input_blame_view(struct tog_view **,
572 struct tog_view *, int);
573 static const struct got_error *close_blame_view(struct tog_view *);
574 static const struct got_error *search_start_blame_view(struct tog_view *);
575 static const struct got_error *search_next_blame_view(struct tog_view *);
577 static const struct got_error *open_tree_view(struct tog_view *,
578 struct got_object_id *, const char *, struct got_repository *);
579 static const struct got_error *show_tree_view(struct tog_view *);
580 static const struct got_error *input_tree_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *close_tree_view(struct tog_view *);
583 static const struct got_error *search_start_tree_view(struct tog_view *);
584 static const struct got_error *search_next_tree_view(struct tog_view *);
586 static const struct got_error *open_ref_view(struct tog_view *,
587 struct got_repository *);
588 static const struct got_error *show_ref_view(struct tog_view *);
589 static const struct got_error *input_ref_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *close_ref_view(struct tog_view *);
592 static const struct got_error *search_start_ref_view(struct tog_view *);
593 static const struct got_error *search_next_ref_view(struct tog_view *);
595 static volatile sig_atomic_t tog_sigwinch_received;
596 static volatile sig_atomic_t tog_sigpipe_received;
597 static volatile sig_atomic_t tog_sigcont_received;
598 static volatile sig_atomic_t tog_sigint_received;
599 static volatile sig_atomic_t tog_sigterm_received;
602 tog_sigwinch(int signo)
604 tog_sigwinch_received = 1;
608 tog_sigpipe(int signo)
610 tog_sigpipe_received = 1;
614 tog_sigcont(int signo)
616 tog_sigcont_received = 1;
620 tog_sigint(int signo)
622 tog_sigint_received = 1;
626 tog_sigterm(int signo)
628 tog_sigterm_received = 1;
632 tog_fatal_signal_received()
634 return (tog_sigpipe_received ||
635 tog_sigint_received || tog_sigint_received);
639 static const struct got_error *
640 view_close(struct tog_view *view)
642 const struct got_error *err = NULL;
645 view_close(view->child);
649 err = view->close(view);
651 del_panel(view->panel);
653 delwin(view->window);
658 static struct tog_view *
659 view_open(int nlines, int ncols, int begin_y, int begin_x,
660 enum tog_view_type type)
662 struct tog_view *view = calloc(1, sizeof(*view));
670 view->nlines = nlines ? nlines : LINES - begin_y;
671 view->ncols = ncols ? ncols : COLS - begin_x;
672 view->begin_y = begin_y;
673 view->begin_x = begin_x;
674 view->window = newwin(nlines, ncols, begin_y, begin_x);
675 if (view->window == NULL) {
679 view->panel = new_panel(view->window);
680 if (view->panel == NULL ||
681 set_panel_userptr(view->panel, view) != OK) {
686 keypad(view->window, TRUE);
691 view_split_begin_x(int begin_x)
693 if (begin_x > 0 || COLS < 120)
695 return (COLS - MAX(COLS / 2, 80));
698 static const struct got_error *view_resize(struct tog_view *);
700 static const struct got_error *
701 view_splitscreen(struct tog_view *view)
703 const struct got_error *err = NULL;
706 view->begin_x = view_split_begin_x(0);
707 view->nlines = LINES;
708 view->ncols = COLS - view->begin_x;
711 err = view_resize(view);
715 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
716 return got_error_from_errno("mvwin");
721 static const struct got_error *
722 view_fullscreen(struct tog_view *view)
724 const struct got_error *err = NULL;
728 view->nlines = LINES;
732 err = view_resize(view);
736 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
737 return got_error_from_errno("mvwin");
743 view_is_parent_view(struct tog_view *view)
745 return view->parent == NULL;
748 static const struct got_error *
749 view_resize(struct tog_view *view)
753 if (view->lines > LINES)
754 nlines = view->nlines - (view->lines - LINES);
756 nlines = view->nlines + (LINES - view->lines);
758 if (view->cols > COLS)
759 ncols = view->ncols - (view->cols - COLS);
761 ncols = view->ncols + (COLS - view->cols);
763 if (wresize(view->window, nlines, ncols) == ERR)
764 return got_error_from_errno("wresize");
765 if (replace_panel(view->panel, view->window) == ERR)
766 return got_error_from_errno("replace_panel");
767 wclear(view->window);
769 view->nlines = nlines;
775 view->child->begin_x = view_split_begin_x(view->begin_x);
776 if (view->child->begin_x == 0) {
777 view_fullscreen(view->child);
778 if (view->child->focussed)
779 show_panel(view->child->panel);
781 show_panel(view->panel);
783 view_splitscreen(view->child);
784 show_panel(view->child->panel);
791 static const struct got_error *
792 view_close_child(struct tog_view *view)
794 const struct got_error *err = NULL;
796 if (view->child == NULL)
799 err = view_close(view->child);
805 view_set_child(struct tog_view *view, struct tog_view *child)
808 child->parent = view;
812 view_is_splitscreen(struct tog_view *view)
814 return view->begin_x > 0;
823 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
824 cols = 80; /* Default */
830 resize_term(lines, cols);
833 static const struct got_error *
834 view_search_start(struct tog_view *view)
836 const struct got_error *err = NULL;
840 if (view->search_started) {
841 regfree(&view->regex);
843 memset(&view->regmatch, 0, sizeof(view->regmatch));
845 view->search_started = 0;
847 if (view->nlines < 1)
850 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
851 wclrtoeol(view->window);
855 ret = wgetnstr(view->window, pattern, sizeof(pattern));
861 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
862 err = view->search_start(view);
864 regfree(&view->regex);
867 view->search_started = 1;
868 view->searching = TOG_SEARCH_FORWARD;
869 view->search_next_done = 0;
870 view->search_next(view);
876 static const struct got_error *
877 view_input(struct tog_view **new, int *done, struct tog_view *view,
878 struct tog_view_list_head *views)
880 const struct got_error *err = NULL;
886 /* Clear "no matches" indicator. */
887 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
888 view->search_next_done == TOG_SEARCH_HAVE_NONE)
889 view->search_next_done = TOG_SEARCH_HAVE_MORE;
891 if (view->searching && !view->search_next_done) {
892 errcode = pthread_mutex_unlock(&tog_mutex);
894 return got_error_set_errno(errcode,
895 "pthread_mutex_unlock");
897 errcode = pthread_mutex_lock(&tog_mutex);
899 return got_error_set_errno(errcode,
900 "pthread_mutex_lock");
901 view->search_next(view);
905 nodelay(stdscr, FALSE);
906 /* Allow threads to make progress while we are waiting for input. */
907 errcode = pthread_mutex_unlock(&tog_mutex);
909 return got_error_set_errno(errcode, "pthread_mutex_unlock");
910 ch = wgetch(view->window);
911 errcode = pthread_mutex_lock(&tog_mutex);
913 return got_error_set_errno(errcode, "pthread_mutex_lock");
914 nodelay(stdscr, TRUE);
916 if (tog_sigwinch_received || tog_sigcont_received) {
918 tog_sigwinch_received = 0;
919 tog_sigcont_received = 0;
920 TAILQ_FOREACH(v, views, entry) {
921 err = view_resize(v);
924 err = v->input(new, v, KEY_RESIZE);
928 err = view_resize(v->child);
931 err = v->child->input(new, v->child,
943 view->child->focussed = 1;
944 view->focus_child = 1;
945 } else if (view->parent) {
947 view->parent->focussed = 1;
948 view->parent->focus_child = 0;
952 err = view->input(new, view, ch);
959 if (view_is_parent_view(view)) {
960 if (view->child == NULL)
962 if (view_is_splitscreen(view->child)) {
964 view->child->focussed = 1;
965 err = view_fullscreen(view->child);
967 err = view_splitscreen(view->child);
970 err = view->child->input(new, view->child,
973 if (view_is_splitscreen(view)) {
974 view->parent->focussed = 0;
976 err = view_fullscreen(view);
978 err = view_splitscreen(view);
982 err = view->input(new, view, KEY_RESIZE);
988 if (view->search_start)
989 view_search_start(view);
991 err = view->input(new, view, ch);
995 if (view->search_started && view->search_next) {
996 view->searching = (ch == 'n' ?
997 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
998 view->search_next_done = 0;
999 view->search_next(view);
1001 err = view->input(new, view, ch);
1004 err = view->input(new, view, ch);
1012 view_vborder(struct tog_view *view)
1015 const struct tog_view *view_above;
1018 return view_vborder(view->parent);
1020 panel = panel_above(view->panel);
1024 view_above = panel_userptr(panel);
1025 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1026 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1030 view_needs_focus_indication(struct tog_view *view)
1032 if (view_is_parent_view(view)) {
1033 if (view->child == NULL || view->child->focussed)
1035 if (!view_is_splitscreen(view->child))
1037 } else if (!view_is_splitscreen(view))
1040 return view->focussed;
1043 static const struct got_error *
1044 view_loop(struct tog_view *view)
1046 const struct got_error *err = NULL;
1047 struct tog_view_list_head views;
1048 struct tog_view *new_view;
1049 int fast_refresh = 10;
1050 int done = 0, errcode;
1052 errcode = pthread_mutex_lock(&tog_mutex);
1054 return got_error_set_errno(errcode, "pthread_mutex_lock");
1057 TAILQ_INSERT_HEAD(&views, view, entry);
1060 err = view->show(view);
1065 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1066 /* Refresh fast during initialization, then become slower. */
1067 if (fast_refresh && fast_refresh-- == 0)
1068 halfdelay(10); /* switch to once per second */
1070 err = view_input(&new_view, &done, view, &views);
1074 struct tog_view *v, *prev = NULL;
1076 if (view_is_parent_view(view))
1077 prev = TAILQ_PREV(view, tog_view_list_head,
1079 else if (view->parent)
1080 prev = view->parent;
1083 view->parent->child = NULL;
1084 view->parent->focus_child = 0;
1086 TAILQ_REMOVE(&views, view, entry);
1088 err = view_close(view);
1093 TAILQ_FOREACH(v, &views, entry) {
1097 if (view == NULL && new_view == NULL) {
1098 /* No view has focus. Try to pick one. */
1101 else if (!TAILQ_EMPTY(&views)) {
1102 view = TAILQ_LAST(&views,
1103 tog_view_list_head);
1106 if (view->focus_child) {
1107 view->child->focussed = 1;
1115 struct tog_view *v, *t;
1116 /* Only allow one parent view per type. */
1117 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1118 if (v->type != new_view->type)
1120 TAILQ_REMOVE(&views, v, entry);
1121 err = view_close(v);
1126 TAILQ_INSERT_TAIL(&views, new_view, entry);
1130 if (view_is_parent_view(view)) {
1131 if (view->child && view->child->focussed)
1134 if (view->parent && view->parent->focussed)
1135 view = view->parent;
1137 show_panel(view->panel);
1138 if (view->child && view_is_splitscreen(view->child))
1139 show_panel(view->child->panel);
1140 if (view->parent && view_is_splitscreen(view)) {
1141 err = view->parent->show(view->parent);
1145 err = view->show(view);
1149 err = view->child->show(view->child);
1158 while (!TAILQ_EMPTY(&views)) {
1159 view = TAILQ_FIRST(&views);
1160 TAILQ_REMOVE(&views, view, entry);
1164 errcode = pthread_mutex_unlock(&tog_mutex);
1165 if (errcode && err == NULL)
1166 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1176 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1181 /* Create newly allocated wide-character string equivalent to a byte string. */
1182 static const struct got_error *
1183 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1186 const struct got_error *err = NULL;
1189 *wlen = mbstowcs(NULL, s, 0);
1190 if (*wlen == (size_t)-1) {
1192 if (errno != EILSEQ)
1193 return got_error_from_errno("mbstowcs");
1195 /* byte string invalid in current encoding; try to "fix" it */
1196 err = got_mbsavis(&vis, &vislen, s);
1199 *wlen = mbstowcs(NULL, vis, 0);
1200 if (*wlen == (size_t)-1) {
1201 err = got_error_from_errno("mbstowcs"); /* give up */
1206 *ws = calloc(*wlen + 1, sizeof(**ws));
1208 err = got_error_from_errno("calloc");
1212 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1213 err = got_error_from_errno("mbstowcs");
1224 /* Format a line for display, ensuring that it won't overflow a width limit. */
1225 static const struct got_error *
1226 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1229 const struct got_error *err = NULL;
1231 wchar_t *wline = NULL;
1238 err = mbs2ws(&wline, &wlen, line);
1242 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1243 wline[wlen - 1] = L'\0';
1246 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1247 wline[wlen - 1] = L'\0';
1253 int width = wcwidth(wline[i]);
1260 if (width == 1 || width == 2) {
1261 if (cols + width > wlimit)
1265 } else if (width == -1) {
1266 if (wline[i] == L'\t') {
1268 ((cols + col_tab_align) % TABSIZE);
1273 if (cols + width > wlimit)
1278 err = got_error_from_errno("wcwidth");
1293 static const struct got_error*
1294 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1295 struct got_object_id *id, struct got_repository *repo)
1297 static const struct got_error *err = NULL;
1298 struct got_reflist_entry *re;
1304 TAILQ_FOREACH(re, refs, entry) {
1305 struct got_tag_object *tag = NULL;
1306 struct got_object_id *ref_id;
1309 name = got_ref_get_name(re->ref);
1310 if (strcmp(name, GOT_REF_HEAD) == 0)
1312 if (strncmp(name, "refs/", 5) == 0)
1314 if (strncmp(name, "got/", 4) == 0 &&
1315 strncmp(name, "got/backup/", 11) != 0)
1317 if (strncmp(name, "heads/", 6) == 0)
1319 if (strncmp(name, "remotes/", 8) == 0) {
1321 s = strstr(name, "/" GOT_REF_HEAD);
1322 if (s != NULL && s[strlen(s)] == '\0')
1325 err = got_ref_resolve(&ref_id, repo, re->ref);
1328 if (strncmp(name, "tags/", 5) == 0) {
1329 err = got_object_open_as_tag(&tag, repo, ref_id);
1331 if (err->code != GOT_ERR_OBJ_TYPE) {
1335 /* Ref points at something other than a tag. */
1340 cmp = got_object_id_cmp(tag ?
1341 got_object_tag_get_object_id(tag) : ref_id, id);
1344 got_object_tag_close(tag);
1348 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1349 s ? ", " : "", name) == -1) {
1350 err = got_error_from_errno("asprintf");
1361 static const struct got_error *
1362 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1367 smallerthan = strchr(author, '<');
1368 if (smallerthan && smallerthan[1] != '\0')
1369 author = smallerthan + 1;
1370 author[strcspn(author, "@>")] = '\0';
1371 return format_line(wauthor, author_width, author, limit, col_tab_align);
1374 static const struct got_error *
1375 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1376 struct got_object_id *id, const size_t date_display_cols,
1377 int author_display_cols)
1379 struct tog_log_view_state *s = &view->state.log;
1380 const struct got_error *err = NULL;
1381 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1382 char *logmsg0 = NULL, *logmsg = NULL;
1383 char *author = NULL;
1384 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1385 int author_width, logmsg_width;
1386 char *newline, *line = NULL;
1388 const int avail = view->ncols;
1390 time_t committer_time;
1391 struct tog_color *tc;
1393 committer_time = got_object_commit_get_committer_time(commit);
1394 if (gmtime_r(&committer_time, &tm) == NULL)
1395 return got_error_from_errno("gmtime_r");
1396 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1397 return got_error(GOT_ERR_NO_SPACE);
1399 if (avail <= date_display_cols)
1400 limit = MIN(sizeof(datebuf) - 1, avail);
1402 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1403 tc = get_color(&s->colors, TOG_COLOR_DATE);
1405 wattr_on(view->window,
1406 COLOR_PAIR(tc->colorpair), NULL);
1407 waddnstr(view->window, datebuf, limit);
1409 wattr_off(view->window,
1410 COLOR_PAIR(tc->colorpair), NULL);
1417 err = got_object_id_str(&id_str, id);
1420 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1422 wattr_on(view->window,
1423 COLOR_PAIR(tc->colorpair), NULL);
1424 wprintw(view->window, "%.8s ", id_str);
1426 wattr_off(view->window,
1427 COLOR_PAIR(tc->colorpair), NULL);
1434 author = strdup(got_object_commit_get_author(commit));
1435 if (author == NULL) {
1436 err = got_error_from_errno("strdup");
1439 err = format_author(&wauthor, &author_width, author, avail - col, col);
1442 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1444 wattr_on(view->window,
1445 COLOR_PAIR(tc->colorpair), NULL);
1446 waddwstr(view->window, wauthor);
1448 wattr_off(view->window,
1449 COLOR_PAIR(tc->colorpair), NULL);
1450 col += author_width;
1451 while (col < avail && author_width < author_display_cols + 2) {
1452 waddch(view->window, ' ');
1459 err = got_object_commit_get_logmsg(&logmsg0, commit);
1463 while (*logmsg == '\n')
1465 newline = strchr(logmsg, '\n');
1468 limit = avail - col;
1469 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1472 waddwstr(view->window, wlogmsg);
1473 col += logmsg_width;
1474 while (col < avail) {
1475 waddch(view->window, ' ');
1487 static struct commit_queue_entry *
1488 alloc_commit_queue_entry(struct got_commit_object *commit,
1489 struct got_object_id *id)
1491 struct commit_queue_entry *entry;
1493 entry = calloc(1, sizeof(*entry));
1498 entry->commit = commit;
1503 pop_commit(struct commit_queue *commits)
1505 struct commit_queue_entry *entry;
1507 entry = TAILQ_FIRST(&commits->head);
1508 TAILQ_REMOVE(&commits->head, entry, entry);
1509 got_object_commit_close(entry->commit);
1510 commits->ncommits--;
1511 /* Don't free entry->id! It is owned by the commit graph. */
1516 free_commits(struct commit_queue *commits)
1518 while (!TAILQ_EMPTY(&commits->head))
1519 pop_commit(commits);
1522 static const struct got_error *
1523 match_commit(int *have_match, struct got_object_id *id,
1524 struct got_commit_object *commit, regex_t *regex)
1526 const struct got_error *err = NULL;
1527 regmatch_t regmatch;
1528 char *id_str = NULL, *logmsg = NULL;
1532 err = got_object_id_str(&id_str, id);
1536 err = got_object_commit_get_logmsg(&logmsg, commit);
1540 if (regexec(regex, got_object_commit_get_author(commit), 1,
1541 ®match, 0) == 0 ||
1542 regexec(regex, got_object_commit_get_committer(commit), 1,
1543 ®match, 0) == 0 ||
1544 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1545 regexec(regex, logmsg, 1, ®match, 0) == 0)
1553 static const struct got_error *
1554 queue_commits(struct tog_log_thread_args *a)
1556 const struct got_error *err = NULL;
1559 * We keep all commits open throughout the lifetime of the log
1560 * view in order to avoid having to re-fetch commits from disk
1561 * while updating the display.
1564 struct got_object_id *id;
1565 struct got_commit_object *commit;
1566 struct commit_queue_entry *entry;
1569 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1571 if (err || id == NULL)
1574 err = got_object_open_as_commit(&commit, a->repo, id);
1577 entry = alloc_commit_queue_entry(commit, id);
1578 if (entry == NULL) {
1579 err = got_error_from_errno("alloc_commit_queue_entry");
1583 errcode = pthread_mutex_lock(&tog_mutex);
1585 err = got_error_set_errno(errcode,
1586 "pthread_mutex_lock");
1590 entry->idx = a->commits->ncommits;
1591 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1592 a->commits->ncommits++;
1594 if (*a->searching == TOG_SEARCH_FORWARD &&
1595 !*a->search_next_done) {
1597 err = match_commit(&have_match, id, commit, a->regex);
1601 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1604 errcode = pthread_mutex_unlock(&tog_mutex);
1605 if (errcode && err == NULL)
1606 err = got_error_set_errno(errcode,
1607 "pthread_mutex_unlock");
1610 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1616 select_commit(struct tog_log_view_state *s)
1618 struct commit_queue_entry *entry;
1621 entry = s->first_displayed_entry;
1623 if (ncommits == s->selected) {
1624 s->selected_entry = entry;
1627 entry = TAILQ_NEXT(entry, entry);
1632 static const struct got_error *
1633 draw_commits(struct tog_view *view)
1635 const struct got_error *err = NULL;
1636 struct tog_log_view_state *s = &view->state.log;
1637 struct commit_queue_entry *entry = s->selected_entry;
1638 const int limit = view->nlines;
1640 int ncommits, author_cols = 4;
1641 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1642 char *refs_str = NULL;
1644 struct tog_color *tc;
1645 static const size_t date_display_cols = 12;
1647 if (s->selected_entry &&
1648 !(view->searching && view->search_next_done == 0)) {
1649 struct got_reflist_head *refs;
1650 err = got_object_id_str(&id_str, s->selected_entry->id);
1653 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1654 s->selected_entry->id);
1656 err = build_refs_str(&refs_str, refs,
1657 s->selected_entry->id, s->repo);
1663 if (s->thread_args.commits_needed == 0)
1664 halfdelay(10); /* disable fast refresh */
1666 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1667 if (asprintf(&ncommits_str, " [%d/%d] %s",
1668 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1669 (view->searching && !view->search_next_done) ?
1670 "searching..." : "loading...") == -1) {
1671 err = got_error_from_errno("asprintf");
1675 const char *search_str = NULL;
1677 if (view->searching) {
1678 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1679 search_str = "no more matches";
1680 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1681 search_str = "no matches found";
1682 else if (!view->search_next_done)
1683 search_str = "searching...";
1686 if (asprintf(&ncommits_str, " [%d/%d] %s",
1687 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1688 search_str ? search_str :
1689 (refs_str ? refs_str : "")) == -1) {
1690 err = got_error_from_errno("asprintf");
1695 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1696 if (asprintf(&header, "commit %s %s%s",
1697 id_str ? id_str : "........................................",
1698 s->in_repo_path, ncommits_str) == -1) {
1699 err = got_error_from_errno("asprintf");
1703 } else if (asprintf(&header, "commit %s%s",
1704 id_str ? id_str : "........................................",
1705 ncommits_str) == -1) {
1706 err = got_error_from_errno("asprintf");
1710 err = format_line(&wline, &width, header, view->ncols, 0);
1714 werase(view->window);
1716 if (view_needs_focus_indication(view))
1717 wstandout(view->window);
1718 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1720 wattr_on(view->window,
1721 COLOR_PAIR(tc->colorpair), NULL);
1722 waddwstr(view->window, wline);
1724 wattr_off(view->window,
1725 COLOR_PAIR(tc->colorpair), NULL);
1726 while (width < view->ncols) {
1727 waddch(view->window, ' ');
1730 if (view_needs_focus_indication(view))
1731 wstandend(view->window);
1736 /* Grow author column size if necessary. */
1737 entry = s->first_displayed_entry;
1743 if (ncommits >= limit - 1)
1745 author = strdup(got_object_commit_get_author(entry->commit));
1746 if (author == NULL) {
1747 err = got_error_from_errno("strdup");
1750 err = format_author(&wauthor, &width, author, COLS,
1752 if (author_cols < width)
1753 author_cols = width;
1757 entry = TAILQ_NEXT(entry, entry);
1760 entry = s->first_displayed_entry;
1761 s->last_displayed_entry = s->first_displayed_entry;
1764 if (ncommits >= limit - 1)
1766 if (ncommits == s->selected)
1767 wstandout(view->window);
1768 err = draw_commit(view, entry->commit, entry->id,
1769 date_display_cols, author_cols);
1770 if (ncommits == s->selected)
1771 wstandend(view->window);
1775 s->last_displayed_entry = entry;
1776 entry = TAILQ_NEXT(entry, entry);
1789 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1791 struct commit_queue_entry *entry;
1794 entry = TAILQ_FIRST(&s->commits.head);
1795 if (s->first_displayed_entry == entry)
1798 entry = s->first_displayed_entry;
1799 while (entry && nscrolled < maxscroll) {
1800 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1802 s->first_displayed_entry = entry;
1808 static const struct got_error *
1809 trigger_log_thread(struct tog_view *view, int wait)
1811 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1814 halfdelay(1); /* fast refresh while loading commits */
1816 while (ta->commits_needed > 0 || ta->load_all) {
1817 if (ta->log_complete)
1820 /* Wake the log thread. */
1821 errcode = pthread_cond_signal(&ta->need_commits);
1823 return got_error_set_errno(errcode,
1824 "pthread_cond_signal");
1827 * The mutex will be released while the view loop waits
1828 * in wgetch(), at which time the log thread will run.
1833 /* Display progress update in log view. */
1834 show_log_view(view);
1838 /* Wait right here while next commit is being loaded. */
1839 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1841 return got_error_set_errno(errcode,
1842 "pthread_cond_wait");
1844 /* Display progress update in log view. */
1845 show_log_view(view);
1853 static const struct got_error *
1854 log_scroll_down(struct tog_view *view, int maxscroll)
1856 struct tog_log_view_state *s = &view->state.log;
1857 const struct got_error *err = NULL;
1858 struct commit_queue_entry *pentry;
1859 int nscrolled = 0, ncommits_needed;
1861 if (s->last_displayed_entry == NULL)
1864 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1865 if (s->commits.ncommits < ncommits_needed &&
1866 !s->thread_args.log_complete) {
1868 * Ask the log thread for required amount of commits.
1870 s->thread_args.commits_needed += maxscroll;
1871 err = trigger_log_thread(view, 1);
1877 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1881 s->last_displayed_entry = pentry;
1883 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1886 s->first_displayed_entry = pentry;
1887 } while (++nscrolled < maxscroll);
1892 static const struct got_error *
1893 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1894 struct got_commit_object *commit, struct got_object_id *commit_id,
1895 struct tog_view *log_view, struct got_repository *repo)
1897 const struct got_error *err;
1898 struct got_object_qid *parent_id;
1899 struct tog_view *diff_view;
1901 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1902 if (diff_view == NULL)
1903 return got_error_from_errno("view_open");
1905 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1906 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1907 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1909 *new_view = diff_view;
1913 static const struct got_error *
1914 tree_view_visit_subtree(struct tog_tree_view_state *s,
1915 struct got_tree_object *subtree)
1917 struct tog_parent_tree *parent;
1919 parent = calloc(1, sizeof(*parent));
1921 return got_error_from_errno("calloc");
1923 parent->tree = s->tree;
1924 parent->first_displayed_entry = s->first_displayed_entry;
1925 parent->selected_entry = s->selected_entry;
1926 parent->selected = s->selected;
1927 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1930 s->first_displayed_entry = NULL;
1934 static const struct got_error *
1935 tree_view_walk_path(struct tog_tree_view_state *s,
1936 struct got_commit_object *commit, const char *path)
1938 const struct got_error *err = NULL;
1939 struct got_tree_object *tree = NULL;
1941 char *slash, *subpath = NULL;
1943 /* Walk the path and open corresponding tree objects. */
1946 struct got_tree_entry *te;
1947 struct got_object_id *tree_id;
1953 /* Ensure the correct subtree entry is selected. */
1954 slash = strchr(p, '/');
1956 te_name = strdup(p);
1958 te_name = strndup(p, slash - p);
1959 if (te_name == NULL) {
1960 err = got_error_from_errno("strndup");
1963 te = got_object_tree_find_entry(s->tree, te_name);
1965 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1970 s->first_displayed_entry = s->selected_entry = te;
1972 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1973 break; /* jump to this file's entry */
1975 slash = strchr(p, '/');
1977 subpath = strndup(path, slash - path);
1979 subpath = strdup(path);
1980 if (subpath == NULL) {
1981 err = got_error_from_errno("strdup");
1985 err = got_object_id_by_path(&tree_id, s->repo, commit,
1990 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1995 err = tree_view_visit_subtree(s, tree);
1997 got_object_tree_close(tree);
2011 static const struct got_error *
2012 browse_commit_tree(struct tog_view **new_view, int begin_x,
2013 struct commit_queue_entry *entry, const char *path,
2014 const char *head_ref_name, struct got_repository *repo)
2016 const struct got_error *err = NULL;
2017 struct tog_tree_view_state *s;
2018 struct tog_view *tree_view;
2020 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2021 if (tree_view == NULL)
2022 return got_error_from_errno("view_open");
2024 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2027 s = &tree_view->state.tree;
2029 *new_view = tree_view;
2031 if (got_path_is_root_dir(path))
2034 return tree_view_walk_path(s, entry->commit, path);
2037 static const struct got_error *
2038 block_signals_used_by_main_thread(void)
2043 if (sigemptyset(&sigset) == -1)
2044 return got_error_from_errno("sigemptyset");
2046 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2047 if (sigaddset(&sigset, SIGWINCH) == -1)
2048 return got_error_from_errno("sigaddset");
2049 if (sigaddset(&sigset, SIGCONT) == -1)
2050 return got_error_from_errno("sigaddset");
2051 if (sigaddset(&sigset, SIGINT) == -1)
2052 return got_error_from_errno("sigaddset");
2053 if (sigaddset(&sigset, SIGTERM) == -1)
2054 return got_error_from_errno("sigaddset");
2056 /* ncurses handles SIGTSTP */
2057 if (sigaddset(&sigset, SIGTSTP) == -1)
2058 return got_error_from_errno("sigaddset");
2060 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2062 return got_error_set_errno(errcode, "pthread_sigmask");
2068 log_thread(void *arg)
2070 const struct got_error *err = NULL;
2072 struct tog_log_thread_args *a = arg;
2075 err = block_signals_used_by_main_thread();
2079 while (!done && !err && !tog_fatal_signal_received()) {
2080 err = queue_commits(a);
2082 if (err->code != GOT_ERR_ITER_COMPLETED)
2086 } else if (a->commits_needed > 0 && !a->load_all)
2087 a->commits_needed--;
2089 errcode = pthread_mutex_lock(&tog_mutex);
2091 err = got_error_set_errno(errcode,
2092 "pthread_mutex_lock");
2094 } else if (*a->quit)
2096 else if (*a->first_displayed_entry == NULL) {
2097 *a->first_displayed_entry =
2098 TAILQ_FIRST(&a->commits->head);
2099 *a->selected_entry = *a->first_displayed_entry;
2102 errcode = pthread_cond_signal(&a->commit_loaded);
2104 err = got_error_set_errno(errcode,
2105 "pthread_cond_signal");
2106 pthread_mutex_unlock(&tog_mutex);
2111 a->commits_needed = 0;
2113 if (a->commits_needed == 0 && !a->load_all) {
2114 errcode = pthread_cond_wait(&a->need_commits,
2117 err = got_error_set_errno(errcode,
2118 "pthread_cond_wait");
2124 errcode = pthread_mutex_unlock(&tog_mutex);
2125 if (errcode && err == NULL)
2126 err = got_error_set_errno(errcode,
2127 "pthread_mutex_unlock");
2129 a->log_complete = 1;
2133 static const struct got_error *
2134 stop_log_thread(struct tog_log_view_state *s)
2136 const struct got_error *err = NULL;
2141 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2143 return got_error_set_errno(errcode,
2144 "pthread_cond_signal");
2145 errcode = pthread_mutex_unlock(&tog_mutex);
2147 return got_error_set_errno(errcode,
2148 "pthread_mutex_unlock");
2149 errcode = pthread_join(s->thread, (void **)&err);
2151 return got_error_set_errno(errcode, "pthread_join");
2152 errcode = pthread_mutex_lock(&tog_mutex);
2154 return got_error_set_errno(errcode,
2155 "pthread_mutex_lock");
2159 if (s->thread_args.repo) {
2160 err = got_repo_close(s->thread_args.repo);
2161 s->thread_args.repo = NULL;
2164 if (s->thread_args.graph) {
2165 got_commit_graph_close(s->thread_args.graph);
2166 s->thread_args.graph = NULL;
2172 static const struct got_error *
2173 close_log_view(struct tog_view *view)
2175 const struct got_error *err = NULL;
2176 struct tog_log_view_state *s = &view->state.log;
2179 err = stop_log_thread(s);
2181 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2182 if (errcode && err == NULL)
2183 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2185 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2186 if (errcode && err == NULL)
2187 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2189 free_commits(&s->commits);
2190 free(s->in_repo_path);
2191 s->in_repo_path = NULL;
2194 free(s->head_ref_name);
2195 s->head_ref_name = NULL;
2199 static const struct got_error *
2200 search_start_log_view(struct tog_view *view)
2202 struct tog_log_view_state *s = &view->state.log;
2204 s->matched_entry = NULL;
2205 s->search_entry = NULL;
2209 static const struct got_error *
2210 search_next_log_view(struct tog_view *view)
2212 const struct got_error *err = NULL;
2213 struct tog_log_view_state *s = &view->state.log;
2214 struct commit_queue_entry *entry;
2216 /* Display progress update in log view. */
2217 show_log_view(view);
2221 if (s->search_entry) {
2223 errcode = pthread_mutex_unlock(&tog_mutex);
2225 return got_error_set_errno(errcode,
2226 "pthread_mutex_unlock");
2227 ch = wgetch(view->window);
2228 errcode = pthread_mutex_lock(&tog_mutex);
2230 return got_error_set_errno(errcode,
2231 "pthread_mutex_lock");
2232 if (ch == KEY_BACKSPACE) {
2233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2236 if (view->searching == TOG_SEARCH_FORWARD)
2237 entry = TAILQ_NEXT(s->search_entry, entry);
2239 entry = TAILQ_PREV(s->search_entry,
2240 commit_queue_head, entry);
2241 } else if (s->matched_entry) {
2242 if (view->searching == TOG_SEARCH_FORWARD)
2243 entry = TAILQ_NEXT(s->matched_entry, entry);
2245 entry = TAILQ_PREV(s->matched_entry,
2246 commit_queue_head, entry);
2248 entry = s->selected_entry;
2254 if (entry == NULL) {
2255 if (s->thread_args.log_complete ||
2256 view->searching == TOG_SEARCH_BACKWARD) {
2257 view->search_next_done =
2258 (s->matched_entry == NULL ?
2259 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2260 s->search_entry = NULL;
2264 * Poke the log thread for more commits and return,
2265 * allowing the main loop to make progress. Search
2266 * will resume at s->search_entry once we come back.
2268 s->thread_args.commits_needed++;
2269 return trigger_log_thread(view, 0);
2272 err = match_commit(&have_match, entry->id, entry->commit,
2277 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2278 s->matched_entry = entry;
2282 s->search_entry = entry;
2283 if (view->searching == TOG_SEARCH_FORWARD)
2284 entry = TAILQ_NEXT(entry, entry);
2286 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2289 if (s->matched_entry) {
2290 int cur = s->selected_entry->idx;
2291 while (cur < s->matched_entry->idx) {
2292 err = input_log_view(NULL, view, KEY_DOWN);
2297 while (cur > s->matched_entry->idx) {
2298 err = input_log_view(NULL, view, KEY_UP);
2305 s->search_entry = NULL;
2310 static const struct got_error *
2311 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2312 struct got_repository *repo, const char *head_ref_name,
2313 const char *in_repo_path, int log_branches)
2315 const struct got_error *err = NULL;
2316 struct tog_log_view_state *s = &view->state.log;
2317 struct got_repository *thread_repo = NULL;
2318 struct got_commit_graph *thread_graph = NULL;
2321 if (in_repo_path != s->in_repo_path) {
2322 free(s->in_repo_path);
2323 s->in_repo_path = strdup(in_repo_path);
2324 if (s->in_repo_path == NULL)
2325 return got_error_from_errno("strdup");
2328 /* The commit queue only contains commits being displayed. */
2329 TAILQ_INIT(&s->commits.head);
2330 s->commits.ncommits = 0;
2333 if (head_ref_name) {
2334 s->head_ref_name = strdup(head_ref_name);
2335 if (s->head_ref_name == NULL) {
2336 err = got_error_from_errno("strdup");
2340 s->start_id = got_object_id_dup(start_id);
2341 if (s->start_id == NULL) {
2342 err = got_error_from_errno("got_object_id_dup");
2345 s->log_branches = log_branches;
2347 STAILQ_INIT(&s->colors);
2348 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2349 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2350 get_color_value("TOG_COLOR_COMMIT"));
2353 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2354 get_color_value("TOG_COLOR_AUTHOR"));
2356 free_colors(&s->colors);
2359 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2360 get_color_value("TOG_COLOR_DATE"));
2362 free_colors(&s->colors);
2367 view->show = show_log_view;
2368 view->input = input_log_view;
2369 view->close = close_log_view;
2370 view->search_start = search_start_log_view;
2371 view->search_next = search_next_log_view;
2373 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2376 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2380 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2381 s->repo, NULL, NULL);
2385 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2387 err = got_error_set_errno(errcode, "pthread_cond_init");
2390 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2392 err = got_error_set_errno(errcode, "pthread_cond_init");
2396 s->thread_args.commits_needed = view->nlines;
2397 s->thread_args.graph = thread_graph;
2398 s->thread_args.commits = &s->commits;
2399 s->thread_args.in_repo_path = s->in_repo_path;
2400 s->thread_args.start_id = s->start_id;
2401 s->thread_args.repo = thread_repo;
2402 s->thread_args.log_complete = 0;
2403 s->thread_args.quit = &s->quit;
2404 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2405 s->thread_args.selected_entry = &s->selected_entry;
2406 s->thread_args.searching = &view->searching;
2407 s->thread_args.search_next_done = &view->search_next_done;
2408 s->thread_args.regex = &view->regex;
2411 close_log_view(view);
2415 static const struct got_error *
2416 show_log_view(struct tog_view *view)
2418 const struct got_error *err;
2419 struct tog_log_view_state *s = &view->state.log;
2421 if (s->thread == NULL) {
2422 int errcode = pthread_create(&s->thread, NULL, log_thread,
2425 return got_error_set_errno(errcode, "pthread_create");
2426 if (s->thread_args.commits_needed > 0) {
2427 err = trigger_log_thread(view, 1);
2433 return draw_commits(view);
2436 static const struct got_error *
2437 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2439 const struct got_error *err = NULL;
2440 struct tog_log_view_state *s = &view->state.log;
2441 struct tog_view *diff_view = NULL, *tree_view = NULL;
2442 struct tog_view *ref_view = NULL;
2443 struct commit_queue_entry *entry;
2444 int begin_x = 0, n, nscroll = view->nlines - 1;
2446 if (s->thread_args.load_all) {
2447 if (ch == KEY_BACKSPACE)
2448 s->thread_args.load_all = 0;
2449 else if (s->thread_args.log_complete) {
2450 s->thread_args.load_all = 0;
2451 log_scroll_down(view, s->commits.ncommits);
2452 s->selected = MIN(view->nlines - 2,
2453 s->commits.ncommits - 1);
2468 if (s->first_displayed_entry == NULL)
2470 if (s->selected > 0)
2473 log_scroll_up(s, 1);
2479 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2487 if (s->first_displayed_entry == NULL)
2489 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2490 s->selected = MAX(0, s->selected - nscroll - 1);
2492 log_scroll_up(s, nscroll);
2500 if (s->first_displayed_entry == NULL)
2502 if (s->selected < MIN(view->nlines - 2,
2503 s->commits.ncommits - 1))
2506 err = log_scroll_down(view, 1);
2514 /* We don't know yet how many commits, so we're forced to
2515 * traverse them all. */
2516 if (!s->thread_args.log_complete) {
2517 s->thread_args.load_all = 1;
2518 return trigger_log_thread(view, 0);
2522 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2523 for (n = 0; n < view->nlines - 1; n++) {
2526 s->first_displayed_entry = entry;
2527 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2530 s->selected = n - 1;
2539 struct commit_queue_entry *first;
2540 first = s->first_displayed_entry;
2543 err = log_scroll_down(view, nscroll);
2546 if (first == s->first_displayed_entry &&
2547 s->selected < MIN(view->nlines - 2,
2548 s->commits.ncommits - 1)) {
2549 /* can't scroll further down */
2550 s->selected += MIN(s->last_displayed_entry->idx -
2551 s->selected_entry->idx, nscroll + 1);
2557 if (s->selected > view->nlines - 2)
2558 s->selected = view->nlines - 2;
2559 if (s->selected > s->commits.ncommits - 1)
2560 s->selected = s->commits.ncommits - 1;
2562 if (s->commits.ncommits < view->nlines - 1 &&
2563 !s->thread_args.log_complete) {
2564 s->thread_args.commits_needed += (view->nlines - 1) -
2565 s->commits.ncommits;
2566 err = trigger_log_thread(view, 1);
2572 if (s->selected_entry == NULL)
2574 if (view_is_parent_view(view))
2575 begin_x = view_split_begin_x(view->begin_x);
2576 err = open_diff_view_for_commit(&diff_view, begin_x,
2577 s->selected_entry->commit, s->selected_entry->id,
2582 diff_view->focussed = 1;
2583 if (view_is_parent_view(view)) {
2584 err = view_close_child(view);
2587 view_set_child(view, diff_view);
2588 view->focus_child = 1;
2590 *new_view = diff_view;
2593 if (s->selected_entry == NULL)
2595 if (view_is_parent_view(view))
2596 begin_x = view_split_begin_x(view->begin_x);
2597 err = browse_commit_tree(&tree_view, begin_x,
2598 s->selected_entry, s->in_repo_path, s->head_ref_name,
2603 tree_view->focussed = 1;
2604 if (view_is_parent_view(view)) {
2605 err = view_close_child(view);
2608 view_set_child(view, tree_view);
2609 view->focus_child = 1;
2611 *new_view = tree_view;
2616 if (ch == KEY_BACKSPACE &&
2617 got_path_is_root_dir(s->in_repo_path))
2619 err = stop_log_thread(s);
2622 if (ch == KEY_BACKSPACE) {
2624 err = got_path_dirname(&parent_path, s->in_repo_path);
2627 free(s->in_repo_path);
2628 s->in_repo_path = parent_path;
2629 s->thread_args.in_repo_path = s->in_repo_path;
2630 } else if (ch == CTRL('l')) {
2631 struct got_object_id *start_id;
2632 err = got_repo_match_object_id(&start_id, NULL,
2633 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2634 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2638 s->start_id = start_id;
2639 s->thread_args.start_id = s->start_id;
2641 s->log_branches = !s->log_branches;
2643 err = got_repo_open(&s->thread_args.repo,
2644 got_repo_get_path(s->repo), NULL);
2648 err = tog_load_refs(s->repo, 0);
2651 err = got_commit_graph_open(&s->thread_args.graph,
2652 s->in_repo_path, !s->log_branches);
2655 err = got_commit_graph_iter_start(s->thread_args.graph,
2656 s->start_id, s->repo, NULL, NULL);
2659 free_commits(&s->commits);
2660 s->first_displayed_entry = NULL;
2661 s->last_displayed_entry = NULL;
2662 s->selected_entry = NULL;
2664 s->thread_args.log_complete = 0;
2666 s->thread_args.commits_needed = view->nlines;
2669 if (view_is_parent_view(view))
2670 begin_x = view_split_begin_x(view->begin_x);
2671 ref_view = view_open(view->nlines, view->ncols,
2672 view->begin_y, begin_x, TOG_VIEW_REF);
2673 if (ref_view == NULL)
2674 return got_error_from_errno("view_open");
2675 err = open_ref_view(ref_view, s->repo);
2677 view_close(ref_view);
2681 ref_view->focussed = 1;
2682 if (view_is_parent_view(view)) {
2683 err = view_close_child(view);
2686 view_set_child(view, ref_view);
2687 view->focus_child = 1;
2689 *new_view = ref_view;
2698 static const struct got_error *
2699 apply_unveil(const char *repo_path, const char *worktree_path)
2701 const struct got_error *error;
2704 if (unveil("gmon.out", "rwc") != 0)
2705 return got_error_from_errno2("unveil", "gmon.out");
2707 if (repo_path && unveil(repo_path, "r") != 0)
2708 return got_error_from_errno2("unveil", repo_path);
2710 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2711 return got_error_from_errno2("unveil", worktree_path);
2713 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2714 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2716 error = got_privsep_unveil_exec_helpers();
2720 if (unveil(NULL, NULL) != 0)
2721 return got_error_from_errno("unveil");
2730 * Override default signal handlers before starting ncurses.
2731 * This should prevent ncurses from installing its own
2732 * broken cleanup() signal handler.
2734 signal(SIGWINCH, tog_sigwinch);
2735 signal(SIGPIPE, tog_sigpipe);
2736 signal(SIGCONT, tog_sigcont);
2737 signal(SIGINT, tog_sigint);
2738 signal(SIGTERM, tog_sigterm);
2742 halfdelay(1); /* Do fast refresh while initial view is loading. */
2745 intrflush(stdscr, FALSE);
2746 keypad(stdscr, TRUE);
2748 if (getenv("TOG_COLORS") != NULL) {
2750 use_default_colors();
2754 static const struct got_error *
2755 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2756 struct got_repository *repo, struct got_worktree *worktree)
2758 const struct got_error *err = NULL;
2761 *in_repo_path = strdup("/");
2762 if (*in_repo_path == NULL)
2763 return got_error_from_errno("strdup");
2768 const char *prefix = got_worktree_get_path_prefix(worktree);
2771 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2774 if (asprintf(in_repo_path, "%s%s%s", prefix,
2775 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2777 err = got_error_from_errno("asprintf");
2778 *in_repo_path = NULL;
2782 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2787 static const struct got_error *
2788 cmd_log(int argc, char *argv[])
2790 const struct got_error *error;
2791 struct got_repository *repo = NULL;
2792 struct got_worktree *worktree = NULL;
2793 struct got_object_id *start_id = NULL;
2794 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2795 char *start_commit = NULL, *label = NULL;
2796 struct got_reference *ref = NULL;
2797 const char *head_ref_name = NULL;
2798 int ch, log_branches = 0;
2799 struct tog_view *view;
2801 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2807 start_commit = optarg;
2810 repo_path = realpath(optarg, NULL);
2811 if (repo_path == NULL)
2812 return got_error_from_errno2("realpath",
2827 if (repo_path == NULL) {
2828 cwd = getcwd(NULL, 0);
2830 return got_error_from_errno("getcwd");
2831 error = got_worktree_open(&worktree, cwd);
2832 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2836 strdup(got_worktree_get_repo_path(worktree));
2838 repo_path = strdup(cwd);
2839 if (repo_path == NULL) {
2840 error = got_error_from_errno("strdup");
2845 error = got_repo_open(&repo, repo_path, NULL);
2849 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2856 error = apply_unveil(got_repo_get_path(repo),
2857 worktree ? got_worktree_get_root_path(worktree) : NULL);
2861 /* already loaded by tog_log_with_path()? */
2862 if (TAILQ_EMPTY(&tog_refs)) {
2863 error = tog_load_refs(repo, 0);
2868 if (start_commit == NULL) {
2869 error = got_repo_match_object_id(&start_id, &label,
2870 worktree ? got_worktree_get_head_ref_name(worktree) :
2871 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2874 head_ref_name = label;
2876 error = got_ref_open(&ref, repo, start_commit, 0);
2878 head_ref_name = got_ref_get_name(ref);
2879 else if (error->code != GOT_ERR_NOT_REF)
2881 error = got_repo_match_object_id(&start_id, NULL,
2882 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2887 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2889 error = got_error_from_errno("view_open");
2892 error = open_log_view(view, start_id, repo, head_ref_name,
2893 in_repo_path, log_branches);
2897 /* Release work tree lock. */
2898 got_worktree_close(worktree);
2901 error = view_loop(view);
2911 const struct got_error *close_err = got_repo_close(repo);
2916 got_worktree_close(worktree);
2925 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2926 "[-w] object1 object2\n", getprogname());
2931 match_line(const char *line, regex_t *regex, size_t nmatch,
2932 regmatch_t *regmatch)
2934 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2938 match_color(struct tog_colors *colors, const char *line)
2940 struct tog_color *tc = NULL;
2942 STAILQ_FOREACH(tc, colors, entry) {
2943 if (match_line(line, &tc->regex, 0, NULL))
2950 static const struct got_error *
2951 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2952 WINDOW *window, regmatch_t *regmatch)
2954 const struct got_error *err = NULL;
2961 s = strndup(line, regmatch->rm_so);
2963 return got_error_from_errno("strndup");
2965 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2970 waddwstr(window, wline);
2977 s = strndup(line + regmatch->rm_so,
2978 regmatch->rm_eo - regmatch->rm_so);
2980 err = got_error_from_errno("strndup");
2984 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2989 wattr_on(window, A_STANDOUT, NULL);
2990 waddwstr(window, wline);
2991 wattr_off(window, A_STANDOUT, NULL);
2998 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2999 err = format_line(&wline, &width,
3000 line + regmatch->rm_eo, wlimit, col_tab_align);
3003 waddwstr(window, wline);
3011 static const struct got_error *
3012 draw_file(struct tog_view *view, const char *header)
3014 struct tog_diff_view_state *s = &view->state.diff;
3015 regmatch_t *regmatch = &view->regmatch;
3016 const struct got_error *err;
3019 size_t linesize = 0;
3021 struct tog_color *tc;
3024 int max_lines = view->nlines;
3025 int nlines = s->nlines;
3028 line_offset = s->line_offsets[s->first_displayed_line - 1];
3029 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3030 return got_error_from_errno("fseek");
3032 werase(view->window);
3035 if (asprintf(&line, "[%d/%d] %s",
3036 s->first_displayed_line - 1 + s->selected_line, nlines,
3038 return got_error_from_errno("asprintf");
3039 err = format_line(&wline, &width, line, view->ncols, 0);
3044 if (view_needs_focus_indication(view))
3045 wstandout(view->window);
3046 waddwstr(view->window, wline);
3049 if (view_needs_focus_indication(view))
3050 wstandend(view->window);
3051 if (width <= view->ncols - 1)
3052 waddch(view->window, '\n');
3061 while (max_lines > 0 && nprinted < max_lines) {
3062 linelen = getline(&line, &linesize, s->f);
3063 if (linelen == -1) {
3069 return got_ferror(s->f, GOT_ERR_IO);
3072 tc = match_color(&s->colors, line);
3074 wattr_on(view->window,
3075 COLOR_PAIR(tc->colorpair), NULL);
3076 if (s->first_displayed_line + nprinted == s->matched_line &&
3077 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3078 err = add_matched_line(&width, line, view->ncols, 0,
3079 view->window, regmatch);
3085 err = format_line(&wline, &width, line, view->ncols, 0);
3090 waddwstr(view->window, wline);
3095 wattr_off(view->window,
3096 COLOR_PAIR(tc->colorpair), NULL);
3097 if (width <= view->ncols - 1)
3098 waddch(view->window, '\n');
3103 s->last_displayed_line = s->first_displayed_line +
3106 s->last_displayed_line = s->first_displayed_line;
3111 while (nprinted < view->nlines) {
3112 waddch(view->window, '\n');
3116 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3121 wstandout(view->window);
3122 waddwstr(view->window, wline);
3125 wstandend(view->window);
3132 get_datestr(time_t *time, char *datebuf)
3134 struct tm mytm, *tm;
3137 tm = gmtime_r(time, &mytm);
3140 s = asctime_r(tm, datebuf);
3143 p = strchr(s, '\n');
3149 static const struct got_error *
3150 get_changed_paths(struct got_pathlist_head *paths,
3151 struct got_commit_object *commit, struct got_repository *repo)
3153 const struct got_error *err = NULL;
3154 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3155 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3156 struct got_object_qid *qid;
3158 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3160 struct got_commit_object *pcommit;
3161 err = got_object_open_as_commit(&pcommit, repo,
3166 tree_id1 = got_object_id_dup(
3167 got_object_commit_get_tree_id(pcommit));
3168 if (tree_id1 == NULL) {
3169 got_object_commit_close(pcommit);
3170 return got_error_from_errno("got_object_id_dup");
3172 got_object_commit_close(pcommit);
3177 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3182 tree_id2 = got_object_commit_get_tree_id(commit);
3183 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3187 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3188 got_diff_tree_collect_changed_paths, paths, 0);
3191 got_object_tree_close(tree1);
3193 got_object_tree_close(tree2);
3198 static const struct got_error *
3199 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3203 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3205 return got_error_from_errno("reallocarray");
3207 (*line_offsets)[*nlines] = off;
3212 static const struct got_error *
3213 write_commit_info(off_t **line_offsets, size_t *nlines,
3214 struct got_object_id *commit_id, struct got_reflist_head *refs,
3215 struct got_repository *repo, FILE *outfile)
3217 const struct got_error *err = NULL;
3218 char datebuf[26], *datestr;
3219 struct got_commit_object *commit;
3220 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3221 time_t committer_time;
3222 const char *author, *committer;
3223 char *refs_str = NULL;
3224 struct got_pathlist_head changed_paths;
3225 struct got_pathlist_entry *pe;
3229 TAILQ_INIT(&changed_paths);
3232 err = build_refs_str(&refs_str, refs, commit_id, repo);
3237 err = got_object_open_as_commit(&commit, repo, commit_id);
3241 err = got_object_id_str(&id_str, commit_id);
3243 err = got_error_from_errno("got_object_id_str");
3247 err = add_line_offset(line_offsets, nlines, 0);
3251 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3252 refs_str ? refs_str : "", refs_str ? ")" : "");
3254 err = got_error_from_errno("fprintf");
3258 err = add_line_offset(line_offsets, nlines, outoff);
3262 n = fprintf(outfile, "from: %s\n",
3263 got_object_commit_get_author(commit));
3265 err = got_error_from_errno("fprintf");
3269 err = add_line_offset(line_offsets, nlines, outoff);
3273 committer_time = got_object_commit_get_committer_time(commit);
3274 datestr = get_datestr(&committer_time, datebuf);
3276 n = fprintf(outfile, "date: %s UTC\n", datestr);
3278 err = got_error_from_errno("fprintf");
3282 err = add_line_offset(line_offsets, nlines, outoff);
3286 author = got_object_commit_get_author(commit);
3287 committer = got_object_commit_get_committer(commit);
3288 if (strcmp(author, committer) != 0) {
3289 n = fprintf(outfile, "via: %s\n", committer);
3291 err = got_error_from_errno("fprintf");
3295 err = add_line_offset(line_offsets, nlines, outoff);
3299 if (got_object_commit_get_nparents(commit) > 1) {
3300 const struct got_object_id_queue *parent_ids;
3301 struct got_object_qid *qid;
3303 parent_ids = got_object_commit_get_parent_ids(commit);
3304 STAILQ_FOREACH(qid, parent_ids, entry) {
3305 err = got_object_id_str(&id_str, &qid->id);
3308 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3310 err = got_error_from_errno("fprintf");
3314 err = add_line_offset(line_offsets, nlines, outoff);
3322 err = got_object_commit_get_logmsg(&logmsg, commit);
3326 while ((line = strsep(&s, "\n")) != NULL) {
3327 n = fprintf(outfile, "%s\n", line);
3329 err = got_error_from_errno("fprintf");
3333 err = add_line_offset(line_offsets, nlines, outoff);
3338 err = get_changed_paths(&changed_paths, commit, repo);
3341 TAILQ_FOREACH(pe, &changed_paths, entry) {
3342 struct got_diff_changed_path *cp = pe->data;
3343 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3345 err = got_error_from_errno("fprintf");
3349 err = add_line_offset(line_offsets, nlines, outoff);
3352 free((char *)pe->path);
3356 fputc('\n', outfile);
3358 err = add_line_offset(line_offsets, nlines, outoff);
3360 got_pathlist_free(&changed_paths);
3364 got_object_commit_close(commit);
3366 free(*line_offsets);
3367 *line_offsets = NULL;
3373 static const struct got_error *
3374 create_diff(struct tog_diff_view_state *s)
3376 const struct got_error *err = NULL;
3380 free(s->line_offsets);
3381 s->line_offsets = malloc(sizeof(off_t));
3382 if (s->line_offsets == NULL)
3383 return got_error_from_errno("malloc");
3388 err = got_error_from_errno("got_opentemp");
3391 if (s->f && fclose(s->f) == EOF) {
3392 err = got_error_from_errno("fclose");
3398 err = got_object_get_type(&obj_type, s->repo, s->id1);
3400 err = got_object_get_type(&obj_type, s->repo, s->id2);
3405 case GOT_OBJ_TYPE_BLOB:
3406 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3407 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3408 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3411 case GOT_OBJ_TYPE_TREE:
3412 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3413 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3414 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3416 case GOT_OBJ_TYPE_COMMIT: {
3417 const struct got_object_id_queue *parent_ids;
3418 struct got_object_qid *pid;
3419 struct got_commit_object *commit2;
3420 struct got_reflist_head *refs;
3422 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3425 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3426 /* Show commit info if we're diffing to a parent/root commit. */
3427 if (s->id1 == NULL) {
3428 err = write_commit_info(&s->line_offsets, &s->nlines,
3429 s->id2, refs, s->repo, s->f);
3433 parent_ids = got_object_commit_get_parent_ids(commit2);
3434 STAILQ_FOREACH(pid, parent_ids, entry) {
3435 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3436 err = write_commit_info(
3437 &s->line_offsets, &s->nlines,
3438 s->id2, refs, s->repo, s->f);
3445 got_object_commit_close(commit2);
3447 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3448 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3449 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3453 err = got_error(GOT_ERR_OBJ_TYPE);
3459 if (s->f && fflush(s->f) != 0 && err == NULL)
3460 err = got_error_from_errno("fflush");
3465 diff_view_indicate_progress(struct tog_view *view)
3467 mvwaddstr(view->window, 0, 0, "diffing...");
3472 static const struct got_error *
3473 search_start_diff_view(struct tog_view *view)
3475 struct tog_diff_view_state *s = &view->state.diff;
3477 s->matched_line = 0;
3481 static const struct got_error *
3482 search_next_diff_view(struct tog_view *view)
3484 struct tog_diff_view_state *s = &view->state.diff;
3487 size_t linesize = 0;
3490 if (!view->searching) {
3491 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3495 if (s->matched_line) {
3496 if (view->searching == TOG_SEARCH_FORWARD)
3497 lineno = s->matched_line + 1;
3499 lineno = s->matched_line - 1;
3501 lineno = s->first_displayed_line;
3506 if (lineno <= 0 || lineno > s->nlines) {
3507 if (s->matched_line == 0) {
3508 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3512 if (view->searching == TOG_SEARCH_FORWARD)
3518 offset = s->line_offsets[lineno - 1];
3519 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3521 return got_error_from_errno("fseeko");
3523 linelen = getline(&line, &linesize, s->f);
3524 if (linelen != -1 &&
3525 match_line(line, &view->regex, 1, &view->regmatch)) {
3526 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3527 s->matched_line = lineno;
3530 if (view->searching == TOG_SEARCH_FORWARD)
3537 if (s->matched_line) {
3538 s->first_displayed_line = s->matched_line;
3539 s->selected_line = 1;
3545 static const struct got_error *
3546 close_diff_view(struct tog_view *view)
3548 const struct got_error *err = NULL;
3549 struct tog_diff_view_state *s = &view->state.diff;
3555 if (s->f && fclose(s->f) == EOF)
3556 err = got_error_from_errno("fclose");
3558 if (s->f1 && fclose(s->f1) == EOF)
3559 err = got_error_from_errno("fclose");
3561 if (s->f2 && fclose(s->f2) == EOF)
3562 err = got_error_from_errno("fclose");
3564 free_colors(&s->colors);
3565 free(s->line_offsets);
3566 s->line_offsets = NULL;
3571 static const struct got_error *
3572 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3573 struct got_object_id *id2, const char *label1, const char *label2,
3574 int diff_context, int ignore_whitespace, int force_text_diff,
3575 struct tog_view *log_view, struct got_repository *repo)
3577 const struct got_error *err;
3578 struct tog_diff_view_state *s = &view->state.diff;
3580 memset(s, 0, sizeof(*s));
3582 if (id1 != NULL && id2 != NULL) {
3584 err = got_object_get_type(&type1, repo, id1);
3587 err = got_object_get_type(&type2, repo, id2);
3592 return got_error(GOT_ERR_OBJ_TYPE);
3594 s->first_displayed_line = 1;
3595 s->last_displayed_line = view->nlines;
3596 s->selected_line = 1;
3604 s->id1 = got_object_id_dup(id1);
3606 return got_error_from_errno("got_object_id_dup");
3607 s->f1 = got_opentemp();
3608 if (s->f1 == NULL) {
3609 err = got_error_from_errno("got_opentemp");
3615 s->id2 = got_object_id_dup(id2);
3616 if (s->id2 == NULL) {
3617 err = got_error_from_errno("got_object_id_dup");
3621 s->f2 = got_opentemp();
3622 if (s->f2 == NULL) {
3623 err = got_error_from_errno("got_opentemp");
3627 s->first_displayed_line = 1;
3628 s->last_displayed_line = view->nlines;
3629 s->diff_context = diff_context;
3630 s->ignore_whitespace = ignore_whitespace;
3631 s->force_text_diff = force_text_diff;
3632 s->log_view = log_view;
3635 STAILQ_INIT(&s->colors);
3636 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3637 err = add_color(&s->colors,
3638 "^-", TOG_COLOR_DIFF_MINUS,
3639 get_color_value("TOG_COLOR_DIFF_MINUS"));
3642 err = add_color(&s->colors, "^\\+",
3643 TOG_COLOR_DIFF_PLUS,
3644 get_color_value("TOG_COLOR_DIFF_PLUS"));
3647 err = add_color(&s->colors,
3648 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3649 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3653 err = add_color(&s->colors,
3654 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3655 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3656 get_color_value("TOG_COLOR_DIFF_META"));
3660 err = add_color(&s->colors,
3661 "^(from|via): ", TOG_COLOR_AUTHOR,
3662 get_color_value("TOG_COLOR_AUTHOR"));
3666 err = add_color(&s->colors,
3667 "^date: ", TOG_COLOR_DATE,
3668 get_color_value("TOG_COLOR_DATE"));
3673 if (log_view && view_is_splitscreen(view))
3674 show_log_view(log_view); /* draw vborder */
3675 diff_view_indicate_progress(view);
3677 err = create_diff(s);
3679 view->show = show_diff_view;
3680 view->input = input_diff_view;
3681 view->close = close_diff_view;
3682 view->search_start = search_start_diff_view;
3683 view->search_next = search_next_diff_view;
3686 close_diff_view(view);
3690 static const struct got_error *
3691 show_diff_view(struct tog_view *view)
3693 const struct got_error *err;
3694 struct tog_diff_view_state *s = &view->state.diff;
3695 char *id_str1 = NULL, *id_str2, *header;
3696 const char *label1, *label2;
3699 err = got_object_id_str(&id_str1, s->id1);
3702 label1 = s->label1 ? : id_str1;
3704 label1 = "/dev/null";
3706 err = got_object_id_str(&id_str2, s->id2);
3709 label2 = s->label2 ? : id_str2;
3711 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3712 err = got_error_from_errno("asprintf");
3720 err = draw_file(view, header);
3725 static const struct got_error *
3726 set_selected_commit(struct tog_diff_view_state *s,
3727 struct commit_queue_entry *entry)
3729 const struct got_error *err;
3730 const struct got_object_id_queue *parent_ids;
3731 struct got_commit_object *selected_commit;
3732 struct got_object_qid *pid;
3735 s->id2 = got_object_id_dup(entry->id);
3737 return got_error_from_errno("got_object_id_dup");
3739 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3742 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3744 pid = STAILQ_FIRST(parent_ids);
3745 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3746 got_object_commit_close(selected_commit);
3750 static const struct got_error *
3751 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3753 const struct got_error *err = NULL;
3754 struct tog_diff_view_state *s = &view->state.diff;
3755 struct tog_log_view_state *ls;
3756 struct commit_queue_entry *old_selected_entry;
3758 size_t linesize = 0;
3760 int i, nscroll = view->nlines - 1;
3766 s->force_text_diff = !s->force_text_diff;
3768 s->ignore_whitespace = !s->ignore_whitespace;
3769 wclear(view->window);
3770 s->first_displayed_line = 1;
3771 s->last_displayed_line = view->nlines;
3772 s->matched_line = 0;
3773 diff_view_indicate_progress(view);
3774 err = create_diff(s);
3778 s->first_displayed_line = 1;
3785 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3791 if (s->first_displayed_line > 1)
3792 s->first_displayed_line--;
3799 if (s->first_displayed_line == 1)
3802 while (i++ < nscroll && s->first_displayed_line > 1)
3803 s->first_displayed_line--;
3809 s->first_displayed_line++;
3820 while (!s->eof && i++ < nscroll) {
3821 linelen = getline(&line, &linesize, s->f);
3822 s->first_displayed_line++;
3823 if (linelen == -1) {
3827 err = got_ferror(s->f, GOT_ERR_IO);
3834 if (s->diff_context > 0) {
3836 s->matched_line = 0;
3837 diff_view_indicate_progress(view);
3838 err = create_diff(s);
3839 if (s->first_displayed_line + view->nlines - 1 >
3841 s->first_displayed_line = 1;
3842 s->last_displayed_line = view->nlines;
3847 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3849 s->matched_line = 0;
3850 diff_view_indicate_progress(view);
3851 err = create_diff(s);
3856 if (s->log_view == NULL)
3858 ls = &s->log_view->state.log;
3859 old_selected_entry = ls->selected_entry;
3861 err = input_log_view(NULL, s->log_view, KEY_UP);
3865 if (old_selected_entry == ls->selected_entry)
3868 err = set_selected_commit(s, ls->selected_entry);
3872 s->first_displayed_line = 1;
3873 s->last_displayed_line = view->nlines;
3874 s->matched_line = 0;
3876 diff_view_indicate_progress(view);
3877 err = create_diff(s);
3881 if (s->log_view == NULL)
3883 ls = &s->log_view->state.log;
3884 old_selected_entry = ls->selected_entry;
3886 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3890 if (old_selected_entry == ls->selected_entry)
3893 err = set_selected_commit(s, ls->selected_entry);
3897 s->first_displayed_line = 1;
3898 s->last_displayed_line = view->nlines;
3899 s->matched_line = 0;
3901 diff_view_indicate_progress(view);
3902 err = create_diff(s);
3911 static const struct got_error *
3912 cmd_diff(int argc, char *argv[])
3914 const struct got_error *error = NULL;
3915 struct got_repository *repo = NULL;
3916 struct got_worktree *worktree = NULL;
3917 struct got_object_id *id1 = NULL, *id2 = NULL;
3918 char *repo_path = NULL, *cwd = NULL;
3919 char *id_str1 = NULL, *id_str2 = NULL;
3920 char *label1 = NULL, *label2 = NULL;
3921 int diff_context = 3, ignore_whitespace = 0;
3922 int ch, force_text_diff = 0;
3924 struct tog_view *view;
3926 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3929 force_text_diff = 1;
3932 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3935 errx(1, "number of context lines is %s: %s",
3939 repo_path = realpath(optarg, NULL);
3940 if (repo_path == NULL)
3941 return got_error_from_errno2("realpath",
3943 got_path_strip_trailing_slashes(repo_path);
3946 ignore_whitespace = 1;
3958 usage_diff(); /* TODO show local worktree changes */
3959 } else if (argc == 2) {
3965 if (repo_path == NULL) {
3966 cwd = getcwd(NULL, 0);
3968 return got_error_from_errno("getcwd");
3969 error = got_worktree_open(&worktree, cwd);
3970 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3974 strdup(got_worktree_get_repo_path(worktree));
3976 repo_path = strdup(cwd);
3977 if (repo_path == NULL) {
3978 error = got_error_from_errno("strdup");
3983 error = got_repo_open(&repo, repo_path, NULL);
3989 error = apply_unveil(got_repo_get_path(repo), NULL);
3993 error = tog_load_refs(repo, 0);
3997 error = got_repo_match_object_id(&id1, &label1, id_str1,
3998 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4002 error = got_repo_match_object_id(&id2, &label2, id_str2,
4003 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4007 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4009 error = got_error_from_errno("view_open");
4012 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4013 ignore_whitespace, force_text_diff, NULL, repo);
4016 error = view_loop(view);
4023 const struct got_error *close_err = got_repo_close(repo);
4028 got_worktree_close(worktree);
4037 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4042 struct tog_blame_line {
4044 struct got_object_id *id;
4047 static const struct got_error *
4048 draw_blame(struct tog_view *view)
4050 struct tog_blame_view_state *s = &view->state.blame;
4051 struct tog_blame *blame = &s->blame;
4052 regmatch_t *regmatch = &view->regmatch;
4053 const struct got_error *err;
4054 int lineno = 0, nprinted = 0;
4056 size_t linesize = 0;
4060 struct tog_blame_line *blame_line;
4061 struct got_object_id *prev_id = NULL;
4063 struct tog_color *tc;
4065 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4070 werase(view->window);
4072 if (asprintf(&line, "commit %s", id_str) == -1) {
4073 err = got_error_from_errno("asprintf");
4078 err = format_line(&wline, &width, line, view->ncols, 0);
4083 if (view_needs_focus_indication(view))
4084 wstandout(view->window);
4085 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4087 wattr_on(view->window,
4088 COLOR_PAIR(tc->colorpair), NULL);
4089 waddwstr(view->window, wline);
4091 wattr_off(view->window,
4092 COLOR_PAIR(tc->colorpair), NULL);
4093 if (view_needs_focus_indication(view))
4094 wstandend(view->window);
4097 if (width < view->ncols - 1)
4098 waddch(view->window, '\n');
4100 if (asprintf(&line, "[%d/%d] %s%s",
4101 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4102 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4104 return got_error_from_errno("asprintf");
4107 err = format_line(&wline, &width, line, view->ncols, 0);
4112 waddwstr(view->window, wline);
4115 if (width < view->ncols - 1)
4116 waddch(view->window, '\n');
4119 while (nprinted < view->nlines - 2) {
4120 linelen = getline(&line, &linesize, blame->f);
4121 if (linelen == -1) {
4122 if (feof(blame->f)) {
4127 return got_ferror(blame->f, GOT_ERR_IO);
4129 if (++lineno < s->first_displayed_line)
4132 if (view->focussed && nprinted == s->selected_line - 1)
4133 wstandout(view->window);
4135 if (blame->nlines > 0) {
4136 blame_line = &blame->lines[lineno - 1];
4137 if (blame_line->annotated && prev_id &&
4138 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4140 nprinted == s->selected_line - 1)) {
4141 waddstr(view->window, " ");
4142 } else if (blame_line->annotated) {
4144 err = got_object_id_str(&id_str, blame_line->id);
4149 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4151 wattr_on(view->window,
4152 COLOR_PAIR(tc->colorpair), NULL);
4153 wprintw(view->window, "%.8s", id_str);
4155 wattr_off(view->window,
4156 COLOR_PAIR(tc->colorpair), NULL);
4158 prev_id = blame_line->id;
4160 waddstr(view->window, "........");
4164 waddstr(view->window, "........");
4168 if (view->focussed && nprinted == s->selected_line - 1)
4169 wstandend(view->window);
4170 waddstr(view->window, " ");
4172 if (view->ncols <= 9) {
4174 wline = wcsdup(L"");
4175 if (wline == NULL) {
4176 err = got_error_from_errno("wcsdup");
4180 } else if (s->first_displayed_line + nprinted ==
4182 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4183 err = add_matched_line(&width, line, view->ncols - 9, 9,
4184 view->window, regmatch);
4191 err = format_line(&wline, &width, line,
4192 view->ncols - 9, 9);
4193 waddwstr(view->window, wline);
4199 if (width <= view->ncols - 1)
4200 waddch(view->window, '\n');
4201 if (++nprinted == 1)
4202 s->first_displayed_line = lineno;
4205 s->last_displayed_line = lineno;
4212 static const struct got_error *
4213 blame_cb(void *arg, int nlines, int lineno,
4214 struct got_commit_object *commit, struct got_object_id *id)
4216 const struct got_error *err = NULL;
4217 struct tog_blame_cb_args *a = arg;
4218 struct tog_blame_line *line;
4221 if (nlines != a->nlines ||
4222 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4223 return got_error(GOT_ERR_RANGE);
4225 errcode = pthread_mutex_lock(&tog_mutex);
4227 return got_error_set_errno(errcode, "pthread_mutex_lock");
4229 if (*a->quit) { /* user has quit the blame view */
4230 err = got_error(GOT_ERR_ITER_COMPLETED);
4235 goto done; /* no change in this commit */
4237 line = &a->lines[lineno - 1];
4238 if (line->annotated)
4241 line->id = got_object_id_dup(id);
4242 if (line->id == NULL) {
4243 err = got_error_from_errno("got_object_id_dup");
4246 line->annotated = 1;
4248 errcode = pthread_mutex_unlock(&tog_mutex);
4250 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4255 blame_thread(void *arg)
4257 const struct got_error *err, *close_err;
4258 struct tog_blame_thread_args *ta = arg;
4259 struct tog_blame_cb_args *a = ta->cb_args;
4262 err = block_signals_used_by_main_thread();
4266 err = got_blame(ta->path, a->commit_id, ta->repo,
4267 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4268 if (err && err->code == GOT_ERR_CANCELLED)
4271 errcode = pthread_mutex_lock(&tog_mutex);
4273 return (void *)got_error_set_errno(errcode,
4274 "pthread_mutex_lock");
4276 close_err = got_repo_close(ta->repo);
4282 errcode = pthread_mutex_unlock(&tog_mutex);
4283 if (errcode && err == NULL)
4284 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4289 static struct got_object_id *
4290 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4291 int first_displayed_line, int selected_line)
4293 struct tog_blame_line *line;
4298 line = &lines[first_displayed_line - 1 + selected_line - 1];
4299 if (!line->annotated)
4305 static const struct got_error *
4306 stop_blame(struct tog_blame *blame)
4308 const struct got_error *err = NULL;
4311 if (blame->thread) {
4313 errcode = pthread_mutex_unlock(&tog_mutex);
4315 return got_error_set_errno(errcode,
4316 "pthread_mutex_unlock");
4317 errcode = pthread_join(blame->thread, (void **)&err);
4319 return got_error_set_errno(errcode, "pthread_join");
4320 errcode = pthread_mutex_lock(&tog_mutex);
4322 return got_error_set_errno(errcode,
4323 "pthread_mutex_lock");
4324 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4326 blame->thread = NULL;
4328 if (blame->thread_args.repo) {
4329 const struct got_error *close_err;
4330 close_err = got_repo_close(blame->thread_args.repo);
4333 blame->thread_args.repo = NULL;
4336 if (fclose(blame->f) == EOF && err == NULL)
4337 err = got_error_from_errno("fclose");
4341 for (i = 0; i < blame->nlines; i++)
4342 free(blame->lines[i].id);
4344 blame->lines = NULL;
4346 free(blame->cb_args.commit_id);
4347 blame->cb_args.commit_id = NULL;
4352 static const struct got_error *
4353 cancel_blame_view(void *arg)
4355 const struct got_error *err = NULL;
4359 errcode = pthread_mutex_lock(&tog_mutex);
4361 return got_error_set_errno(errcode,
4362 "pthread_mutex_unlock");
4365 err = got_error(GOT_ERR_CANCELLED);
4367 errcode = pthread_mutex_unlock(&tog_mutex);
4369 return got_error_set_errno(errcode,
4370 "pthread_mutex_lock");
4375 static const struct got_error *
4376 run_blame(struct tog_view *view)
4378 struct tog_blame_view_state *s = &view->state.blame;
4379 struct tog_blame *blame = &s->blame;
4380 const struct got_error *err = NULL;
4381 struct got_commit_object *commit = NULL;
4382 struct got_blob_object *blob = NULL;
4383 struct got_repository *thread_repo = NULL;
4384 struct got_object_id *obj_id = NULL;
4387 err = got_object_open_as_commit(&commit, s->repo,
4388 &s->blamed_commit->id);
4392 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4396 err = got_object_get_type(&obj_type, s->repo, obj_id);
4400 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4401 err = got_error(GOT_ERR_OBJ_TYPE);
4405 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4408 blame->f = got_opentemp();
4409 if (blame->f == NULL) {
4410 err = got_error_from_errno("got_opentemp");
4413 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4414 &blame->line_offsets, blame->f, blob);
4417 if (blame->nlines == 0) {
4418 s->blame_complete = 1;
4422 /* Don't include \n at EOF in the blame line count. */
4423 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4426 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4427 if (blame->lines == NULL) {
4428 err = got_error_from_errno("calloc");
4432 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4436 blame->cb_args.view = view;
4437 blame->cb_args.lines = blame->lines;
4438 blame->cb_args.nlines = blame->nlines;
4439 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4440 if (blame->cb_args.commit_id == NULL) {
4441 err = got_error_from_errno("got_object_id_dup");
4444 blame->cb_args.quit = &s->done;
4446 blame->thread_args.path = s->path;
4447 blame->thread_args.repo = thread_repo;
4448 blame->thread_args.cb_args = &blame->cb_args;
4449 blame->thread_args.complete = &s->blame_complete;
4450 blame->thread_args.cancel_cb = cancel_blame_view;
4451 blame->thread_args.cancel_arg = &s->done;
4452 s->blame_complete = 0;
4454 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4455 s->first_displayed_line = 1;
4456 s->last_displayed_line = view->nlines;
4457 s->selected_line = 1;
4459 s->matched_line = 0;
4463 got_object_commit_close(commit);
4465 got_object_blob_close(blob);
4472 static const struct got_error *
4473 open_blame_view(struct tog_view *view, char *path,
4474 struct got_object_id *commit_id, struct got_repository *repo)
4476 const struct got_error *err = NULL;
4477 struct tog_blame_view_state *s = &view->state.blame;
4479 STAILQ_INIT(&s->blamed_commits);
4481 s->path = strdup(path);
4482 if (s->path == NULL)
4483 return got_error_from_errno("strdup");
4485 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4491 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4492 s->first_displayed_line = 1;
4493 s->last_displayed_line = view->nlines;
4494 s->selected_line = 1;
4495 s->blame_complete = 0;
4497 s->commit_id = commit_id;
4498 memset(&s->blame, 0, sizeof(s->blame));
4500 STAILQ_INIT(&s->colors);
4501 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4502 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4503 get_color_value("TOG_COLOR_COMMIT"));
4508 view->show = show_blame_view;
4509 view->input = input_blame_view;
4510 view->close = close_blame_view;
4511 view->search_start = search_start_blame_view;
4512 view->search_next = search_next_blame_view;
4514 return run_blame(view);
4517 static const struct got_error *
4518 close_blame_view(struct tog_view *view)
4520 const struct got_error *err = NULL;
4521 struct tog_blame_view_state *s = &view->state.blame;
4523 if (s->blame.thread)
4524 err = stop_blame(&s->blame);
4526 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4527 struct got_object_qid *blamed_commit;
4528 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4529 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4530 got_object_qid_free(blamed_commit);
4534 free_colors(&s->colors);
4539 static const struct got_error *
4540 search_start_blame_view(struct tog_view *view)
4542 struct tog_blame_view_state *s = &view->state.blame;
4544 s->matched_line = 0;
4548 static const struct got_error *
4549 search_next_blame_view(struct tog_view *view)
4551 struct tog_blame_view_state *s = &view->state.blame;
4554 size_t linesize = 0;
4557 if (!view->searching) {
4558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4562 if (s->matched_line) {
4563 if (view->searching == TOG_SEARCH_FORWARD)
4564 lineno = s->matched_line + 1;
4566 lineno = s->matched_line - 1;
4568 lineno = s->first_displayed_line - 1 + s->selected_line;
4573 if (lineno <= 0 || lineno > s->blame.nlines) {
4574 if (s->matched_line == 0) {
4575 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4579 if (view->searching == TOG_SEARCH_FORWARD)
4582 lineno = s->blame.nlines;
4585 offset = s->blame.line_offsets[lineno - 1];
4586 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4588 return got_error_from_errno("fseeko");
4590 linelen = getline(&line, &linesize, s->blame.f);
4591 if (linelen != -1 &&
4592 match_line(line, &view->regex, 1, &view->regmatch)) {
4593 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4594 s->matched_line = lineno;
4597 if (view->searching == TOG_SEARCH_FORWARD)
4604 if (s->matched_line) {
4605 s->first_displayed_line = s->matched_line;
4606 s->selected_line = 1;
4612 static const struct got_error *
4613 show_blame_view(struct tog_view *view)
4615 const struct got_error *err = NULL;
4616 struct tog_blame_view_state *s = &view->state.blame;
4619 if (s->blame.thread == NULL && !s->blame_complete) {
4620 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4621 &s->blame.thread_args);
4623 return got_error_set_errno(errcode, "pthread_create");
4625 halfdelay(1); /* fast refresh while annotating */
4628 if (s->blame_complete)
4629 halfdelay(10); /* disable fast refresh */
4631 err = draw_blame(view);
4637 static const struct got_error *
4638 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4640 const struct got_error *err = NULL, *thread_err = NULL;
4641 struct tog_view *diff_view;
4642 struct tog_blame_view_state *s = &view->state.blame;
4643 int begin_x = 0, nscroll = view->nlines - 2;
4651 s->selected_line = 1;
4652 s->first_displayed_line = 1;
4656 if (s->blame.nlines < view->nlines - 2) {
4657 s->selected_line = s->blame.nlines;
4658 s->first_displayed_line = 1;
4660 s->selected_line = view->nlines - 2;
4661 s->first_displayed_line = s->blame.nlines -
4668 if (s->selected_line > 1)
4670 else if (s->selected_line == 1 &&
4671 s->first_displayed_line > 1)
4672 s->first_displayed_line--;
4679 if (s->first_displayed_line == 1) {
4680 s->selected_line = MAX(1, s->selected_line - nscroll);
4683 if (s->first_displayed_line > nscroll)
4684 s->first_displayed_line -= nscroll;
4686 s->first_displayed_line = 1;
4691 if (s->selected_line < view->nlines - 2 &&
4692 s->first_displayed_line +
4693 s->selected_line <= s->blame.nlines)
4695 else if (s->last_displayed_line <
4697 s->first_displayed_line++;
4701 struct got_object_id *id = NULL;
4702 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4703 s->first_displayed_line, s->selected_line);
4707 struct got_commit_object *commit, *pcommit;
4708 struct got_object_qid *pid;
4709 struct got_object_id *blob_id = NULL;
4711 err = got_object_open_as_commit(&commit,
4716 got_object_commit_get_parent_ids(commit));
4718 got_object_commit_close(commit);
4721 /* Check if path history ends here. */
4722 err = got_object_open_as_commit(&pcommit,
4726 err = got_object_id_by_path(&blob_id, s->repo,
4728 got_object_commit_close(pcommit);
4730 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4732 got_object_commit_close(commit);
4735 err = got_object_get_type(&obj_type, s->repo,
4738 /* Can't blame non-blob type objects. */
4739 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4740 got_object_commit_close(commit);
4743 err = got_object_qid_alloc(&s->blamed_commit,
4745 got_object_commit_close(commit);
4747 if (got_object_id_cmp(id,
4748 &s->blamed_commit->id) == 0)
4750 err = got_object_qid_alloc(&s->blamed_commit,
4756 thread_err = stop_blame(&s->blame);
4760 STAILQ_INSERT_HEAD(&s->blamed_commits,
4761 s->blamed_commit, entry);
4762 err = run_blame(view);
4768 struct got_object_qid *first;
4769 first = STAILQ_FIRST(&s->blamed_commits);
4770 if (!got_object_id_cmp(&first->id, s->commit_id))
4773 thread_err = stop_blame(&s->blame);
4777 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4778 got_object_qid_free(s->blamed_commit);
4780 STAILQ_FIRST(&s->blamed_commits);
4781 err = run_blame(view);
4788 struct got_object_id *id = NULL;
4789 struct got_object_qid *pid;
4790 struct got_commit_object *commit = NULL;
4791 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4792 s->first_displayed_line, s->selected_line);
4795 err = got_object_open_as_commit(&commit, s->repo, id);
4799 got_object_commit_get_parent_ids(commit));
4800 if (view_is_parent_view(view))
4801 begin_x = view_split_begin_x(view->begin_x);
4802 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4803 if (diff_view == NULL) {
4804 got_object_commit_close(commit);
4805 err = got_error_from_errno("view_open");
4808 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4809 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4810 got_object_commit_close(commit);
4812 view_close(diff_view);
4816 diff_view->focussed = 1;
4817 if (view_is_parent_view(view)) {
4818 err = view_close_child(view);
4821 view_set_child(view, diff_view);
4822 view->focus_child = 1;
4824 *new_view = diff_view;
4835 if (s->last_displayed_line >= s->blame.nlines &&
4836 s->selected_line >= MIN(s->blame.nlines,
4837 view->nlines - 2)) {
4840 if (s->last_displayed_line >= s->blame.nlines &&
4841 s->selected_line < view->nlines - 2) {
4843 MIN(nscroll, s->last_displayed_line -
4844 s->first_displayed_line - s->selected_line + 1);
4846 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4847 s->first_displayed_line += nscroll;
4849 s->first_displayed_line =
4850 s->blame.nlines - (view->nlines - 3);
4853 if (s->selected_line > view->nlines - 2) {
4854 s->selected_line = MIN(s->blame.nlines,
4861 return thread_err ? thread_err : err;
4864 static const struct got_error *
4865 cmd_blame(int argc, char *argv[])
4867 const struct got_error *error;
4868 struct got_repository *repo = NULL;
4869 struct got_worktree *worktree = NULL;
4870 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4871 char *link_target = NULL;
4872 struct got_object_id *commit_id = NULL;
4873 struct got_commit_object *commit = NULL;
4874 char *commit_id_str = NULL;
4876 struct tog_view *view;
4878 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4881 commit_id_str = optarg;
4884 repo_path = realpath(optarg, NULL);
4885 if (repo_path == NULL)
4886 return got_error_from_errno2("realpath",
4901 if (repo_path == NULL) {
4902 cwd = getcwd(NULL, 0);
4904 return got_error_from_errno("getcwd");
4905 error = got_worktree_open(&worktree, cwd);
4906 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4910 strdup(got_worktree_get_repo_path(worktree));
4912 repo_path = strdup(cwd);
4913 if (repo_path == NULL) {
4914 error = got_error_from_errno("strdup");
4919 error = got_repo_open(&repo, repo_path, NULL);
4923 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4930 error = apply_unveil(got_repo_get_path(repo), NULL);
4934 error = tog_load_refs(repo, 0);
4938 if (commit_id_str == NULL) {
4939 struct got_reference *head_ref;
4940 error = got_ref_open(&head_ref, repo, worktree ?
4941 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4944 error = got_ref_resolve(&commit_id, repo, head_ref);
4945 got_ref_close(head_ref);
4947 error = got_repo_match_object_id(&commit_id, NULL,
4948 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4953 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4955 error = got_error_from_errno("view_open");
4959 error = got_object_open_as_commit(&commit, repo, commit_id);
4963 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4968 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4973 /* Release work tree lock. */
4974 got_worktree_close(worktree);
4977 error = view_loop(view);
4985 got_object_commit_close(commit);
4987 got_worktree_close(worktree);
4989 const struct got_error *close_err = got_repo_close(repo);
4997 static const struct got_error *
4998 draw_tree_entries(struct tog_view *view, const char *parent_path)
5000 struct tog_tree_view_state *s = &view->state.tree;
5001 const struct got_error *err = NULL;
5002 struct got_tree_entry *te;
5004 struct tog_color *tc;
5005 int width, n, i, nentries;
5006 int limit = view->nlines;
5010 werase(view->window);
5015 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5018 if (view_needs_focus_indication(view))
5019 wstandout(view->window);
5020 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5022 wattr_on(view->window,
5023 COLOR_PAIR(tc->colorpair), NULL);
5024 waddwstr(view->window, wline);
5026 wattr_off(view->window,
5027 COLOR_PAIR(tc->colorpair), NULL);
5028 if (view_needs_focus_indication(view))
5029 wstandend(view->window);
5032 if (width < view->ncols - 1)
5033 waddch(view->window, '\n');
5036 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5039 waddwstr(view->window, wline);
5042 if (width < view->ncols - 1)
5043 waddch(view->window, '\n');
5046 waddch(view->window, '\n');
5050 if (s->first_displayed_entry == NULL) {
5051 te = got_object_tree_get_first_entry(s->tree);
5052 if (s->selected == 0) {
5054 wstandout(view->window);
5055 s->selected_entry = NULL;
5057 waddstr(view->window, " ..\n"); /* parent directory */
5058 if (s->selected == 0 && view->focussed)
5059 wstandend(view->window);
5066 te = s->first_displayed_entry;
5069 nentries = got_object_tree_get_nentries(s->tree);
5070 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5071 char *line = NULL, *id_str = NULL, *link_target = NULL;
5072 const char *modestr = "";
5075 te = got_object_tree_get_entry(s->tree, i);
5076 mode = got_tree_entry_get_mode(te);
5079 err = got_object_id_str(&id_str,
5080 got_tree_entry_get_id(te));
5082 return got_error_from_errno(
5083 "got_object_id_str");
5085 if (got_object_tree_entry_is_submodule(te))
5087 else if (S_ISLNK(mode)) {
5090 err = got_tree_entry_get_symlink_target(&link_target,
5096 for (i = 0; i < strlen(link_target); i++) {
5097 if (!isprint((unsigned char)link_target[i]))
5098 link_target[i] = '?';
5102 else if (S_ISDIR(mode))
5104 else if (mode & S_IXUSR)
5106 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5107 got_tree_entry_get_name(te), modestr,
5108 link_target ? " -> ": "",
5109 link_target ? link_target : "") == -1) {
5112 return got_error_from_errno("asprintf");
5116 err = format_line(&wline, &width, line, view->ncols, 0);
5121 if (n == s->selected) {
5123 wstandout(view->window);
5124 s->selected_entry = te;
5126 tc = match_color(&s->colors, line);
5128 wattr_on(view->window,
5129 COLOR_PAIR(tc->colorpair), NULL);
5130 waddwstr(view->window, wline);
5132 wattr_off(view->window,
5133 COLOR_PAIR(tc->colorpair), NULL);
5134 if (width < view->ncols - 1)
5135 waddch(view->window, '\n');
5136 if (n == s->selected && view->focussed)
5137 wstandend(view->window);
5143 s->last_displayed_entry = te;
5152 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5154 struct got_tree_entry *te;
5155 int isroot = s->tree == s->root;
5158 if (s->first_displayed_entry == NULL)
5161 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5162 while (i++ < maxscroll) {
5165 s->first_displayed_entry = NULL;
5168 s->first_displayed_entry = te;
5169 te = got_tree_entry_get_prev(s->tree, te);
5174 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5176 struct got_tree_entry *next, *last;
5179 if (s->first_displayed_entry)
5180 next = got_tree_entry_get_next(s->tree,
5181 s->first_displayed_entry);
5183 next = got_object_tree_get_first_entry(s->tree);
5185 last = s->last_displayed_entry;
5186 while (next && last && n++ < maxscroll) {
5187 last = got_tree_entry_get_next(s->tree, last);
5189 s->first_displayed_entry = next;
5190 next = got_tree_entry_get_next(s->tree, next);
5195 static const struct got_error *
5196 tree_entry_path(char **path, struct tog_parent_trees *parents,
5197 struct got_tree_entry *te)
5199 const struct got_error *err = NULL;
5200 struct tog_parent_tree *pt;
5201 size_t len = 2; /* for leading slash and NUL */
5203 TAILQ_FOREACH(pt, parents, entry)
5204 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5207 len += strlen(got_tree_entry_get_name(te));
5209 *path = calloc(1, len);
5211 return got_error_from_errno("calloc");
5214 pt = TAILQ_LAST(parents, tog_parent_trees);
5216 const char *name = got_tree_entry_get_name(pt->selected_entry);
5217 if (strlcat(*path, name, len) >= len) {
5218 err = got_error(GOT_ERR_NO_SPACE);
5221 if (strlcat(*path, "/", len) >= len) {
5222 err = got_error(GOT_ERR_NO_SPACE);
5225 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5228 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5229 err = got_error(GOT_ERR_NO_SPACE);
5241 static const struct got_error *
5242 blame_tree_entry(struct tog_view **new_view, int begin_x,
5243 struct got_tree_entry *te, struct tog_parent_trees *parents,
5244 struct got_object_id *commit_id, struct got_repository *repo)
5246 const struct got_error *err = NULL;
5248 struct tog_view *blame_view;
5252 err = tree_entry_path(&path, parents, te);
5256 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5257 if (blame_view == NULL) {
5258 err = got_error_from_errno("view_open");
5262 err = open_blame_view(blame_view, path, commit_id, repo);
5264 if (err->code == GOT_ERR_CANCELLED)
5266 view_close(blame_view);
5268 *new_view = blame_view;
5274 static const struct got_error *
5275 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5276 struct tog_tree_view_state *s)
5278 struct tog_view *log_view;
5279 const struct got_error *err = NULL;
5284 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5285 if (log_view == NULL)
5286 return got_error_from_errno("view_open");
5288 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5292 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5295 view_close(log_view);
5297 *new_view = log_view;
5302 static const struct got_error *
5303 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5304 const char *head_ref_name, struct got_repository *repo)
5306 const struct got_error *err = NULL;
5307 char *commit_id_str = NULL;
5308 struct tog_tree_view_state *s = &view->state.tree;
5309 struct got_commit_object *commit = NULL;
5311 TAILQ_INIT(&s->parents);
5312 STAILQ_INIT(&s->colors);
5314 s->commit_id = got_object_id_dup(commit_id);
5315 if (s->commit_id == NULL)
5316 return got_error_from_errno("got_object_id_dup");
5318 err = got_object_open_as_commit(&commit, repo, commit_id);
5323 * The root is opened here and will be closed when the view is closed.
5324 * Any visited subtrees and their path-wise parents are opened and
5327 err = got_object_open_as_tree(&s->root, repo,
5328 got_object_commit_get_tree_id(commit));
5333 err = got_object_id_str(&commit_id_str, commit_id);
5337 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5338 err = got_error_from_errno("asprintf");
5342 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5343 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5344 if (head_ref_name) {
5345 s->head_ref_name = strdup(head_ref_name);
5346 if (s->head_ref_name == NULL) {
5347 err = got_error_from_errno("strdup");
5353 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5354 err = add_color(&s->colors, "\\$$",
5355 TOG_COLOR_TREE_SUBMODULE,
5356 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5359 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5360 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5363 err = add_color(&s->colors, "/$",
5364 TOG_COLOR_TREE_DIRECTORY,
5365 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5369 err = add_color(&s->colors, "\\*$",
5370 TOG_COLOR_TREE_EXECUTABLE,
5371 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5375 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5376 get_color_value("TOG_COLOR_COMMIT"));
5381 view->show = show_tree_view;
5382 view->input = input_tree_view;
5383 view->close = close_tree_view;
5384 view->search_start = search_start_tree_view;
5385 view->search_next = search_next_tree_view;
5387 free(commit_id_str);
5389 got_object_commit_close(commit);
5391 close_tree_view(view);
5395 static const struct got_error *
5396 close_tree_view(struct tog_view *view)
5398 struct tog_tree_view_state *s = &view->state.tree;
5400 free_colors(&s->colors);
5401 free(s->tree_label);
5402 s->tree_label = NULL;
5404 s->commit_id = NULL;
5405 free(s->head_ref_name);
5406 s->head_ref_name = NULL;
5407 while (!TAILQ_EMPTY(&s->parents)) {
5408 struct tog_parent_tree *parent;
5409 parent = TAILQ_FIRST(&s->parents);
5410 TAILQ_REMOVE(&s->parents, parent, entry);
5411 if (parent->tree != s->root)
5412 got_object_tree_close(parent->tree);
5416 if (s->tree != NULL && s->tree != s->root)
5417 got_object_tree_close(s->tree);
5419 got_object_tree_close(s->root);
5423 static const struct got_error *
5424 search_start_tree_view(struct tog_view *view)
5426 struct tog_tree_view_state *s = &view->state.tree;
5428 s->matched_entry = NULL;
5433 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5435 regmatch_t regmatch;
5437 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5441 static const struct got_error *
5442 search_next_tree_view(struct tog_view *view)
5444 struct tog_tree_view_state *s = &view->state.tree;
5445 struct got_tree_entry *te = NULL;
5447 if (!view->searching) {
5448 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5452 if (s->matched_entry) {
5453 if (view->searching == TOG_SEARCH_FORWARD) {
5454 if (s->selected_entry)
5455 te = got_tree_entry_get_next(s->tree,
5458 te = got_object_tree_get_first_entry(s->tree);
5460 if (s->selected_entry == NULL)
5461 te = got_object_tree_get_last_entry(s->tree);
5463 te = got_tree_entry_get_prev(s->tree,
5467 if (s->selected_entry)
5468 te = s->selected_entry;
5469 else if (view->searching == TOG_SEARCH_FORWARD)
5470 te = got_object_tree_get_first_entry(s->tree);
5472 te = got_object_tree_get_last_entry(s->tree);
5477 if (s->matched_entry == NULL) {
5478 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5481 if (view->searching == TOG_SEARCH_FORWARD)
5482 te = got_object_tree_get_first_entry(s->tree);
5484 te = got_object_tree_get_last_entry(s->tree);
5487 if (match_tree_entry(te, &view->regex)) {
5488 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5489 s->matched_entry = te;
5493 if (view->searching == TOG_SEARCH_FORWARD)
5494 te = got_tree_entry_get_next(s->tree, te);
5496 te = got_tree_entry_get_prev(s->tree, te);
5499 if (s->matched_entry) {
5500 s->first_displayed_entry = s->matched_entry;
5507 static const struct got_error *
5508 show_tree_view(struct tog_view *view)
5510 const struct got_error *err = NULL;
5511 struct tog_tree_view_state *s = &view->state.tree;
5514 err = tree_entry_path(&parent_path, &s->parents, NULL);
5518 err = draw_tree_entries(view, parent_path);
5525 static const struct got_error *
5526 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5528 const struct got_error *err = NULL;
5529 struct tog_tree_view_state *s = &view->state.tree;
5530 struct tog_view *log_view, *ref_view;
5531 struct got_tree_entry *te;
5532 int begin_x = 0, n, nscroll = view->nlines - 3;
5536 s->show_ids = !s->show_ids;
5539 if (!s->selected_entry)
5541 if (view_is_parent_view(view))
5542 begin_x = view_split_begin_x(view->begin_x);
5543 err = log_selected_tree_entry(&log_view, begin_x, s);
5545 log_view->focussed = 1;
5546 if (view_is_parent_view(view)) {
5547 err = view_close_child(view);
5550 view_set_child(view, log_view);
5551 view->focus_child = 1;
5553 *new_view = log_view;
5556 if (view_is_parent_view(view))
5557 begin_x = view_split_begin_x(view->begin_x);
5558 ref_view = view_open(view->nlines, view->ncols,
5559 view->begin_y, begin_x, TOG_VIEW_REF);
5560 if (ref_view == NULL)
5561 return got_error_from_errno("view_open");
5562 err = open_ref_view(ref_view, s->repo);
5564 view_close(ref_view);
5568 ref_view->focussed = 1;
5569 if (view_is_parent_view(view)) {
5570 err = view_close_child(view);
5573 view_set_child(view, ref_view);
5574 view->focus_child = 1;
5576 *new_view = ref_view;
5581 if (s->tree == s->root)
5582 s->first_displayed_entry =
5583 got_object_tree_get_first_entry(s->tree);
5585 s->first_displayed_entry = NULL;
5590 te = got_object_tree_get_last_entry(s->tree);
5591 for (n = 0; n < view->nlines - 3; n++) {
5593 if(s->tree != s->root) {
5594 s->first_displayed_entry = NULL;
5599 s->first_displayed_entry = te;
5600 te = got_tree_entry_get_prev(s->tree, te);
5603 s->selected = n - 1;
5608 if (s->selected > 0) {
5612 tree_scroll_up(s, 1);
5619 if (s->tree == s->root) {
5620 if (got_object_tree_get_first_entry(s->tree) ==
5621 s->first_displayed_entry)
5622 s->selected -= MIN(s->selected, nscroll);
5624 if (s->first_displayed_entry == NULL)
5625 s->selected -= MIN(s->selected, nscroll);
5627 tree_scroll_up(s, MAX(0, nscroll));
5632 if (s->selected < s->ndisplayed - 1) {
5636 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5638 /* can't scroll any further */
5640 tree_scroll_down(s, 1);
5647 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5649 /* can't scroll any further; move cursor down */
5650 if (s->selected < s->ndisplayed - 1)
5651 s->selected += MIN(nscroll,
5652 s->ndisplayed - s->selected - 1);
5655 tree_scroll_down(s, nscroll);
5660 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5661 struct tog_parent_tree *parent;
5662 /* user selected '..' */
5663 if (s->tree == s->root)
5665 parent = TAILQ_FIRST(&s->parents);
5666 TAILQ_REMOVE(&s->parents, parent,
5668 got_object_tree_close(s->tree);
5669 s->tree = parent->tree;
5670 s->first_displayed_entry =
5671 parent->first_displayed_entry;
5673 parent->selected_entry;
5674 s->selected = parent->selected;
5676 } else if (S_ISDIR(got_tree_entry_get_mode(
5677 s->selected_entry))) {
5678 struct got_tree_object *subtree;
5679 err = got_object_open_as_tree(&subtree, s->repo,
5680 got_tree_entry_get_id(s->selected_entry));
5683 err = tree_view_visit_subtree(s, subtree);
5685 got_object_tree_close(subtree);
5688 } else if (S_ISREG(got_tree_entry_get_mode(
5689 s->selected_entry))) {
5690 struct tog_view *blame_view;
5691 int begin_x = view_is_parent_view(view) ?
5692 view_split_begin_x(view->begin_x) : 0;
5694 err = blame_tree_entry(&blame_view, begin_x,
5695 s->selected_entry, &s->parents,
5696 s->commit_id, s->repo);
5700 blame_view->focussed = 1;
5701 if (view_is_parent_view(view)) {
5702 err = view_close_child(view);
5705 view_set_child(view, blame_view);
5706 view->focus_child = 1;
5708 *new_view = blame_view;
5712 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5713 s->selected = view->nlines - 4;
5726 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5731 static const struct got_error *
5732 cmd_tree(int argc, char *argv[])
5734 const struct got_error *error;
5735 struct got_repository *repo = NULL;
5736 struct got_worktree *worktree = NULL;
5737 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5738 struct got_object_id *commit_id = NULL;
5739 struct got_commit_object *commit = NULL;
5740 const char *commit_id_arg = NULL;
5742 struct got_reference *ref = NULL;
5743 const char *head_ref_name = NULL;
5745 struct tog_view *view;
5747 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5750 commit_id_arg = optarg;
5753 repo_path = realpath(optarg, NULL);
5754 if (repo_path == NULL)
5755 return got_error_from_errno2("realpath",
5770 if (repo_path == NULL) {
5771 cwd = getcwd(NULL, 0);
5773 return got_error_from_errno("getcwd");
5774 error = got_worktree_open(&worktree, cwd);
5775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5779 strdup(got_worktree_get_repo_path(worktree));
5781 repo_path = strdup(cwd);
5782 if (repo_path == NULL) {
5783 error = got_error_from_errno("strdup");
5788 error = got_repo_open(&repo, repo_path, NULL);
5792 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5799 error = apply_unveil(got_repo_get_path(repo), NULL);
5803 error = tog_load_refs(repo, 0);
5807 if (commit_id_arg == NULL) {
5808 error = got_repo_match_object_id(&commit_id, &label,
5809 worktree ? got_worktree_get_head_ref_name(worktree) :
5810 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5813 head_ref_name = label;
5815 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5817 head_ref_name = got_ref_get_name(ref);
5818 else if (error->code != GOT_ERR_NOT_REF)
5820 error = got_repo_match_object_id(&commit_id, NULL,
5821 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5826 error = got_object_open_as_commit(&commit, repo, commit_id);
5830 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5832 error = got_error_from_errno("view_open");
5835 error = open_tree_view(view, commit_id, head_ref_name, repo);
5838 if (!got_path_is_root_dir(in_repo_path)) {
5839 error = tree_view_walk_path(&view->state.tree, commit,
5846 /* Release work tree lock. */
5847 got_worktree_close(worktree);
5850 error = view_loop(view);
5859 const struct got_error *close_err = got_repo_close(repo);
5867 static const struct got_error *
5868 ref_view_load_refs(struct tog_ref_view_state *s)
5870 struct got_reflist_entry *sre;
5871 struct tog_reflist_entry *re;
5874 TAILQ_FOREACH(sre, &tog_refs, entry) {
5875 if (strncmp(got_ref_get_name(sre->ref),
5876 "refs/got/", 9) == 0 &&
5877 strncmp(got_ref_get_name(sre->ref),
5878 "refs/got/backup/", 16) != 0)
5881 re = malloc(sizeof(*re));
5883 return got_error_from_errno("malloc");
5885 re->ref = got_ref_dup(sre->ref);
5886 if (re->ref == NULL)
5887 return got_error_from_errno("got_ref_dup");
5888 re->idx = s->nrefs++;
5889 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5892 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5897 ref_view_free_refs(struct tog_ref_view_state *s)
5899 struct tog_reflist_entry *re;
5901 while (!TAILQ_EMPTY(&s->refs)) {
5902 re = TAILQ_FIRST(&s->refs);
5903 TAILQ_REMOVE(&s->refs, re, entry);
5904 got_ref_close(re->ref);
5909 static const struct got_error *
5910 open_ref_view(struct tog_view *view, struct got_repository *repo)
5912 const struct got_error *err = NULL;
5913 struct tog_ref_view_state *s = &view->state.ref;
5915 s->selected_entry = 0;
5918 TAILQ_INIT(&s->refs);
5919 STAILQ_INIT(&s->colors);
5921 err = ref_view_load_refs(s);
5925 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5926 err = add_color(&s->colors, "^refs/heads/",
5927 TOG_COLOR_REFS_HEADS,
5928 get_color_value("TOG_COLOR_REFS_HEADS"));
5932 err = add_color(&s->colors, "^refs/tags/",
5933 TOG_COLOR_REFS_TAGS,
5934 get_color_value("TOG_COLOR_REFS_TAGS"));
5938 err = add_color(&s->colors, "^refs/remotes/",
5939 TOG_COLOR_REFS_REMOTES,
5940 get_color_value("TOG_COLOR_REFS_REMOTES"));
5944 err = add_color(&s->colors, "^refs/got/backup/",
5945 TOG_COLOR_REFS_BACKUP,
5946 get_color_value("TOG_COLOR_REFS_BACKUP"));
5951 view->show = show_ref_view;
5952 view->input = input_ref_view;
5953 view->close = close_ref_view;
5954 view->search_start = search_start_ref_view;
5955 view->search_next = search_next_ref_view;
5958 free_colors(&s->colors);
5962 static const struct got_error *
5963 close_ref_view(struct tog_view *view)
5965 struct tog_ref_view_state *s = &view->state.ref;
5967 ref_view_free_refs(s);
5968 free_colors(&s->colors);
5973 static const struct got_error *
5974 resolve_reflist_entry(struct got_object_id **commit_id,
5975 struct tog_reflist_entry *re, struct got_repository *repo)
5977 const struct got_error *err = NULL;
5978 struct got_object_id *obj_id;
5979 struct got_tag_object *tag = NULL;
5984 err = got_ref_resolve(&obj_id, repo, re->ref);
5988 err = got_object_get_type(&obj_type, repo, obj_id);
5993 case GOT_OBJ_TYPE_COMMIT:
5994 *commit_id = obj_id;
5996 case GOT_OBJ_TYPE_TAG:
5997 err = got_object_open_as_tag(&tag, repo, obj_id);
6001 err = got_object_get_type(&obj_type, repo,
6002 got_object_tag_get_object_id(tag));
6005 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6006 err = got_error(GOT_ERR_OBJ_TYPE);
6009 *commit_id = got_object_id_dup(
6010 got_object_tag_get_object_id(tag));
6011 if (*commit_id == NULL) {
6012 err = got_error_from_errno("got_object_id_dup");
6017 err = got_error(GOT_ERR_OBJ_TYPE);
6023 got_object_tag_close(tag);
6031 static const struct got_error *
6032 log_ref_entry(struct tog_view **new_view, int begin_x,
6033 struct tog_reflist_entry *re, struct got_repository *repo)
6035 struct tog_view *log_view;
6036 const struct got_error *err = NULL;
6037 struct got_object_id *commit_id = NULL;
6041 err = resolve_reflist_entry(&commit_id, re, repo);
6043 if (err->code != GOT_ERR_OBJ_TYPE)
6049 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6050 if (log_view == NULL) {
6051 err = got_error_from_errno("view_open");
6055 err = open_log_view(log_view, commit_id, repo,
6056 got_ref_get_name(re->ref), "", 0);
6059 view_close(log_view);
6061 *new_view = log_view;
6067 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6069 struct tog_reflist_entry *re;
6072 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6075 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6076 while (i++ < maxscroll) {
6079 s->first_displayed_entry = re;
6080 re = TAILQ_PREV(re, tog_reflist_head, entry);
6085 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6087 struct tog_reflist_entry *next, *last;
6090 if (s->first_displayed_entry)
6091 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6093 next = TAILQ_FIRST(&s->refs);
6095 last = s->last_displayed_entry;
6096 while (next && last && n++ < maxscroll) {
6097 last = TAILQ_NEXT(last, entry);
6099 s->first_displayed_entry = next;
6100 next = TAILQ_NEXT(next, entry);
6105 static const struct got_error *
6106 search_start_ref_view(struct tog_view *view)
6108 struct tog_ref_view_state *s = &view->state.ref;
6110 s->matched_entry = NULL;
6115 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6117 regmatch_t regmatch;
6119 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6123 static const struct got_error *
6124 search_next_ref_view(struct tog_view *view)
6126 struct tog_ref_view_state *s = &view->state.ref;
6127 struct tog_reflist_entry *re = NULL;
6129 if (!view->searching) {
6130 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6134 if (s->matched_entry) {
6135 if (view->searching == TOG_SEARCH_FORWARD) {
6136 if (s->selected_entry)
6137 re = TAILQ_NEXT(s->selected_entry, entry);
6139 re = TAILQ_PREV(s->selected_entry,
6140 tog_reflist_head, entry);
6142 if (s->selected_entry == NULL)
6143 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6145 re = TAILQ_PREV(s->selected_entry,
6146 tog_reflist_head, entry);
6149 if (s->selected_entry)
6150 re = s->selected_entry;
6151 else if (view->searching == TOG_SEARCH_FORWARD)
6152 re = TAILQ_FIRST(&s->refs);
6154 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6159 if (s->matched_entry == NULL) {
6160 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6163 if (view->searching == TOG_SEARCH_FORWARD)
6164 re = TAILQ_FIRST(&s->refs);
6166 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6169 if (match_reflist_entry(re, &view->regex)) {
6170 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6171 s->matched_entry = re;
6175 if (view->searching == TOG_SEARCH_FORWARD)
6176 re = TAILQ_NEXT(re, entry);
6178 re = TAILQ_PREV(re, tog_reflist_head, entry);
6181 if (s->matched_entry) {
6182 s->first_displayed_entry = s->matched_entry;
6189 static const struct got_error *
6190 show_ref_view(struct tog_view *view)
6192 const struct got_error *err = NULL;
6193 struct tog_ref_view_state *s = &view->state.ref;
6194 struct tog_reflist_entry *re;
6197 struct tog_color *tc;
6199 int limit = view->nlines;
6201 werase(view->window);
6208 re = s->first_displayed_entry;
6210 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6212 return got_error_from_errno("asprintf");
6214 err = format_line(&wline, &width, line, view->ncols, 0);
6219 if (view_needs_focus_indication(view))
6220 wstandout(view->window);
6221 waddwstr(view->window, wline);
6222 if (view_needs_focus_indication(view))
6223 wstandend(view->window);
6228 if (width < view->ncols - 1)
6229 waddch(view->window, '\n');
6234 while (re && limit > 0) {
6237 if (got_ref_is_symbolic(re->ref)) {
6238 if (asprintf(&line, "%s -> %s",
6239 got_ref_get_name(re->ref),
6240 got_ref_get_symref_target(re->ref)) == -1)
6241 return got_error_from_errno("asprintf");
6242 } else if (s->show_ids) {
6243 struct got_object_id *id;
6245 err = got_ref_resolve(&id, s->repo, re->ref);
6248 err = got_object_id_str(&id_str, id);
6253 if (asprintf(&line, "%s: %s",
6254 got_ref_get_name(re->ref), id_str) == -1) {
6255 err = got_error_from_errno("asprintf");
6263 line = strdup(got_ref_get_name(re->ref));
6265 return got_error_from_errno("strdup");
6268 err = format_line(&wline, &width, line, view->ncols, 0);
6273 if (n == s->selected) {
6275 wstandout(view->window);
6276 s->selected_entry = re;
6278 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6280 wattr_on(view->window,
6281 COLOR_PAIR(tc->colorpair), NULL);
6282 waddwstr(view->window, wline);
6284 wattr_off(view->window,
6285 COLOR_PAIR(tc->colorpair), NULL);
6286 if (width < view->ncols - 1)
6287 waddch(view->window, '\n');
6288 if (n == s->selected && view->focussed)
6289 wstandend(view->window);
6295 s->last_displayed_entry = re;
6298 re = TAILQ_NEXT(re, entry);
6305 static const struct got_error *
6306 browse_ref_tree(struct tog_view **new_view, int begin_x,
6307 struct tog_reflist_entry *re, struct got_repository *repo)
6309 const struct got_error *err = NULL;
6310 struct got_object_id *commit_id = NULL;
6311 struct tog_view *tree_view;
6315 err = resolve_reflist_entry(&commit_id, re, repo);
6317 if (err->code != GOT_ERR_OBJ_TYPE)
6324 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6325 if (tree_view == NULL) {
6326 err = got_error_from_errno("view_open");
6330 err = open_tree_view(tree_view, commit_id,
6331 got_ref_get_name(re->ref), repo);
6335 *new_view = tree_view;
6340 static const struct got_error *
6341 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6343 const struct got_error *err = NULL;
6344 struct tog_ref_view_state *s = &view->state.ref;
6345 struct tog_view *log_view, *tree_view;
6346 struct tog_reflist_entry *re;
6347 int begin_x = 0, n, nscroll = view->nlines - 1;
6351 s->show_ids = !s->show_ids;
6354 s->sort_by_date = !s->sort_by_date;
6355 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6356 got_ref_cmp_by_commit_timestamp_descending :
6357 tog_ref_cmp_by_name, s->repo);
6360 got_reflist_object_id_map_free(tog_refs_idmap);
6361 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6362 &tog_refs, s->repo);
6365 ref_view_free_refs(s);
6366 err = ref_view_load_refs(s);
6370 if (!s->selected_entry)
6372 if (view_is_parent_view(view))
6373 begin_x = view_split_begin_x(view->begin_x);
6374 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6377 log_view->focussed = 1;
6378 if (view_is_parent_view(view)) {
6379 err = view_close_child(view);
6382 view_set_child(view, log_view);
6383 view->focus_child = 1;
6385 *new_view = log_view;
6388 if (!s->selected_entry)
6390 if (view_is_parent_view(view))
6391 begin_x = view_split_begin_x(view->begin_x);
6392 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6394 if (err || tree_view == NULL)
6397 tree_view->focussed = 1;
6398 if (view_is_parent_view(view)) {
6399 err = view_close_child(view);
6402 view_set_child(view, tree_view);
6403 view->focus_child = 1;
6405 *new_view = tree_view;
6410 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6415 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6416 for (n = 0; n < view->nlines - 1; n++) {
6419 s->first_displayed_entry = re;
6420 re = TAILQ_PREV(re, tog_reflist_head, entry);
6423 s->selected = n - 1;
6428 if (s->selected > 0) {
6432 ref_scroll_up(s, 1);
6439 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6440 s->selected -= MIN(nscroll, s->selected);
6441 ref_scroll_up(s, MAX(0, nscroll));
6446 if (s->selected < s->ndisplayed - 1) {
6450 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6451 /* can't scroll any further */
6453 ref_scroll_down(s, 1);
6460 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6461 /* can't scroll any further; move cursor down */
6462 if (s->selected < s->ndisplayed - 1)
6463 s->selected += MIN(nscroll,
6464 s->ndisplayed - s->selected - 1);
6467 ref_scroll_down(s, nscroll);
6471 err = tog_load_refs(s->repo, s->sort_by_date);
6474 ref_view_free_refs(s);
6475 err = ref_view_load_refs(s);
6478 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6479 s->selected = view->nlines - 2;
6492 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6497 static const struct got_error *
6498 cmd_ref(int argc, char *argv[])
6500 const struct got_error *error;
6501 struct got_repository *repo = NULL;
6502 struct got_worktree *worktree = NULL;
6503 char *cwd = NULL, *repo_path = NULL;
6505 struct tog_view *view;
6507 while ((ch = getopt(argc, argv, "r:")) != -1) {
6510 repo_path = realpath(optarg, NULL);
6511 if (repo_path == NULL)
6512 return got_error_from_errno2("realpath",
6527 if (repo_path == NULL) {
6528 cwd = getcwd(NULL, 0);
6530 return got_error_from_errno("getcwd");
6531 error = got_worktree_open(&worktree, cwd);
6532 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6536 strdup(got_worktree_get_repo_path(worktree));
6538 repo_path = strdup(cwd);
6539 if (repo_path == NULL) {
6540 error = got_error_from_errno("strdup");
6545 error = got_repo_open(&repo, repo_path, NULL);
6551 error = apply_unveil(got_repo_get_path(repo), NULL);
6555 error = tog_load_refs(repo, 0);
6559 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6561 error = got_error_from_errno("view_open");
6565 error = open_ref_view(view, repo);
6570 /* Release work tree lock. */
6571 got_worktree_close(worktree);
6574 error = view_loop(view);
6579 const struct got_error *close_err = got_repo_close(repo);
6588 list_commands(FILE *fp)
6592 fprintf(fp, "commands:");
6593 for (i = 0; i < nitems(tog_commands); i++) {
6594 const struct tog_cmd *cmd = &tog_commands[i];
6595 fprintf(fp, " %s", cmd->name);
6601 usage(int hflag, int status)
6603 FILE *fp = (status == 0) ? stdout : stderr;
6605 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6608 fprintf(fp, "lazy usage: %s path\n", getprogname());
6615 make_argv(int argc, ...)
6623 argv = calloc(argc, sizeof(char *));
6626 for (i = 0; i < argc; i++) {
6627 argv[i] = strdup(va_arg(ap, char *));
6628 if (argv[i] == NULL)
6637 * Try to convert 'tog path' into a 'tog log path' command.
6638 * The user could simply have mistyped the command rather than knowingly
6639 * provided a path. So check whether argv[0] can in fact be resolved
6640 * to a path in the HEAD commit and print a special error if not.
6641 * This hack is for mpi@ <3
6643 static const struct got_error *
6644 tog_log_with_path(int argc, char *argv[])
6646 const struct got_error *error = NULL, *close_err;
6647 const struct tog_cmd *cmd = NULL;
6648 struct got_repository *repo = NULL;
6649 struct got_worktree *worktree = NULL;
6650 struct got_object_id *commit_id = NULL, *id = NULL;
6651 struct got_commit_object *commit = NULL;
6652 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6653 char *commit_id_str = NULL, **cmd_argv = NULL;
6655 cwd = getcwd(NULL, 0);
6657 return got_error_from_errno("getcwd");
6659 error = got_worktree_open(&worktree, cwd);
6660 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6664 repo_path = strdup(got_worktree_get_repo_path(worktree));
6666 repo_path = strdup(cwd);
6667 if (repo_path == NULL) {
6668 error = got_error_from_errno("strdup");
6672 error = got_repo_open(&repo, repo_path, NULL);
6676 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6681 error = tog_load_refs(repo, 0);
6684 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6685 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6686 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6691 got_worktree_close(worktree);
6695 error = got_object_open_as_commit(&commit, repo, commit_id);
6699 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6701 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6703 fprintf(stderr, "%s: '%s' is no known command or path\n",
6704 getprogname(), argv[0]);
6709 close_err = got_repo_close(repo);
6714 error = got_object_id_str(&commit_id_str, commit_id);
6718 cmd = &tog_commands[0]; /* log */
6720 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6721 error = cmd->cmd_main(argc, cmd_argv);
6724 close_err = got_repo_close(repo);
6729 got_object_commit_close(commit);
6731 got_worktree_close(worktree);
6733 free(commit_id_str);
6740 for (i = 0; i < argc; i++)
6749 main(int argc, char *argv[])
6751 const struct got_error *error = NULL;
6752 const struct tog_cmd *cmd = NULL;
6753 int ch, hflag = 0, Vflag = 0;
6754 char **cmd_argv = NULL;
6755 static const struct option longopts[] = {
6756 { "version", no_argument, NULL, 'V' },
6760 setlocale(LC_CTYPE, "");
6762 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6782 got_version_print_str();
6787 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6795 /* Build an argument vector which runs a default command. */
6796 cmd = &tog_commands[0];
6798 cmd_argv = make_argv(argc, cmd->name);
6802 /* Did the user specify a command? */
6803 for (i = 0; i < nitems(tog_commands); i++) {
6804 if (strncmp(tog_commands[i].name, argv[0],
6805 strlen(argv[0])) == 0) {
6806 cmd = &tog_commands[i];
6815 /* No command specified; try log with a path */
6816 error = tog_log_with_path(argc, argv);
6821 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6828 for (i = 0; i < argc; i++)
6833 if (error && error->code != GOT_ERR_CANCELLED)
6834 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);