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;
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);
2484 if (s->first_displayed_entry == NULL)
2486 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2489 log_scroll_up(s, view->nlines - 1);
2497 if (s->first_displayed_entry == NULL)
2499 if (s->selected < MIN(view->nlines - 2,
2500 s->commits.ncommits - 1))
2503 err = log_scroll_down(view, 1);
2511 /* We don't know yet how many commits, so we're forced to
2512 * traverse them all. */
2513 if (!s->thread_args.log_complete) {
2514 s->thread_args.load_all = 1;
2515 return trigger_log_thread(view, 0);
2519 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2520 for (n = 0; n < view->nlines - 1; n++) {
2523 s->first_displayed_entry = entry;
2524 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2527 s->selected = n - 1;
2533 struct commit_queue_entry *first;
2534 first = s->first_displayed_entry;
2537 err = log_scroll_down(view, view->nlines - 1);
2540 if (first == s->first_displayed_entry &&
2541 s->selected < MIN(view->nlines - 2,
2542 s->commits.ncommits - 1)) {
2543 /* can't scroll further down */
2544 s->selected = MIN(view->nlines - 2,
2545 s->commits.ncommits - 1);
2551 if (s->selected > view->nlines - 2)
2552 s->selected = view->nlines - 2;
2553 if (s->selected > s->commits.ncommits - 1)
2554 s->selected = s->commits.ncommits - 1;
2556 if (s->commits.ncommits < view->nlines - 1 &&
2557 !s->thread_args.log_complete) {
2558 s->thread_args.commits_needed += (view->nlines - 1) -
2559 s->commits.ncommits;
2560 err = trigger_log_thread(view, 1);
2566 if (s->selected_entry == NULL)
2568 if (view_is_parent_view(view))
2569 begin_x = view_split_begin_x(view->begin_x);
2570 err = open_diff_view_for_commit(&diff_view, begin_x,
2571 s->selected_entry->commit, s->selected_entry->id,
2576 diff_view->focussed = 1;
2577 if (view_is_parent_view(view)) {
2578 err = view_close_child(view);
2581 view_set_child(view, diff_view);
2582 view->focus_child = 1;
2584 *new_view = diff_view;
2587 if (s->selected_entry == NULL)
2589 if (view_is_parent_view(view))
2590 begin_x = view_split_begin_x(view->begin_x);
2591 err = browse_commit_tree(&tree_view, begin_x,
2592 s->selected_entry, s->in_repo_path, s->head_ref_name,
2597 tree_view->focussed = 1;
2598 if (view_is_parent_view(view)) {
2599 err = view_close_child(view);
2602 view_set_child(view, tree_view);
2603 view->focus_child = 1;
2605 *new_view = tree_view;
2610 if (ch == KEY_BACKSPACE &&
2611 got_path_is_root_dir(s->in_repo_path))
2613 err = stop_log_thread(s);
2616 if (ch == KEY_BACKSPACE) {
2618 err = got_path_dirname(&parent_path, s->in_repo_path);
2621 free(s->in_repo_path);
2622 s->in_repo_path = parent_path;
2623 s->thread_args.in_repo_path = s->in_repo_path;
2624 } else if (ch == CTRL('l')) {
2625 struct got_object_id *start_id;
2626 err = got_repo_match_object_id(&start_id, NULL,
2627 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2628 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2632 s->start_id = start_id;
2633 s->thread_args.start_id = s->start_id;
2635 s->log_branches = !s->log_branches;
2637 err = got_repo_open(&s->thread_args.repo,
2638 got_repo_get_path(s->repo), NULL);
2642 err = tog_load_refs(s->repo, 0);
2645 err = got_commit_graph_open(&s->thread_args.graph,
2646 s->in_repo_path, !s->log_branches);
2649 err = got_commit_graph_iter_start(s->thread_args.graph,
2650 s->start_id, s->repo, NULL, NULL);
2653 free_commits(&s->commits);
2654 s->first_displayed_entry = NULL;
2655 s->last_displayed_entry = NULL;
2656 s->selected_entry = NULL;
2658 s->thread_args.log_complete = 0;
2660 s->thread_args.commits_needed = view->nlines;
2663 if (view_is_parent_view(view))
2664 begin_x = view_split_begin_x(view->begin_x);
2665 ref_view = view_open(view->nlines, view->ncols,
2666 view->begin_y, begin_x, TOG_VIEW_REF);
2667 if (ref_view == NULL)
2668 return got_error_from_errno("view_open");
2669 err = open_ref_view(ref_view, s->repo);
2671 view_close(ref_view);
2675 ref_view->focussed = 1;
2676 if (view_is_parent_view(view)) {
2677 err = view_close_child(view);
2680 view_set_child(view, ref_view);
2681 view->focus_child = 1;
2683 *new_view = ref_view;
2692 static const struct got_error *
2693 apply_unveil(const char *repo_path, const char *worktree_path)
2695 const struct got_error *error;
2698 if (unveil("gmon.out", "rwc") != 0)
2699 return got_error_from_errno2("unveil", "gmon.out");
2701 if (repo_path && unveil(repo_path, "r") != 0)
2702 return got_error_from_errno2("unveil", repo_path);
2704 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2705 return got_error_from_errno2("unveil", worktree_path);
2707 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2708 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2710 error = got_privsep_unveil_exec_helpers();
2714 if (unveil(NULL, NULL) != 0)
2715 return got_error_from_errno("unveil");
2724 * Override default signal handlers before starting ncurses.
2725 * This should prevent ncurses from installing its own
2726 * broken cleanup() signal handler.
2728 signal(SIGWINCH, tog_sigwinch);
2729 signal(SIGPIPE, tog_sigpipe);
2730 signal(SIGCONT, tog_sigcont);
2731 signal(SIGINT, tog_sigint);
2732 signal(SIGTERM, tog_sigterm);
2736 halfdelay(1); /* Do fast refresh while initial view is loading. */
2739 intrflush(stdscr, FALSE);
2740 keypad(stdscr, TRUE);
2742 if (getenv("TOG_COLORS") != NULL) {
2744 use_default_colors();
2748 static const struct got_error *
2749 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2750 struct got_repository *repo, struct got_worktree *worktree)
2752 const struct got_error *err = NULL;
2755 *in_repo_path = strdup("/");
2756 if (*in_repo_path == NULL)
2757 return got_error_from_errno("strdup");
2762 const char *prefix = got_worktree_get_path_prefix(worktree);
2765 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2768 if (asprintf(in_repo_path, "%s%s%s", prefix,
2769 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2771 err = got_error_from_errno("asprintf");
2772 *in_repo_path = NULL;
2776 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2781 static const struct got_error *
2782 cmd_log(int argc, char *argv[])
2784 const struct got_error *error;
2785 struct got_repository *repo = NULL;
2786 struct got_worktree *worktree = NULL;
2787 struct got_object_id *start_id = NULL;
2788 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2789 char *start_commit = NULL, *label = NULL;
2790 struct got_reference *ref = NULL;
2791 const char *head_ref_name = NULL;
2792 int ch, log_branches = 0;
2793 struct tog_view *view;
2795 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2801 start_commit = optarg;
2804 repo_path = realpath(optarg, NULL);
2805 if (repo_path == NULL)
2806 return got_error_from_errno2("realpath",
2821 if (repo_path == NULL) {
2822 cwd = getcwd(NULL, 0);
2824 return got_error_from_errno("getcwd");
2825 error = got_worktree_open(&worktree, cwd);
2826 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2830 strdup(got_worktree_get_repo_path(worktree));
2832 repo_path = strdup(cwd);
2833 if (repo_path == NULL) {
2834 error = got_error_from_errno("strdup");
2839 error = got_repo_open(&repo, repo_path, NULL);
2843 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2850 error = apply_unveil(got_repo_get_path(repo),
2851 worktree ? got_worktree_get_root_path(worktree) : NULL);
2855 /* already loaded by tog_log_with_path()? */
2856 if (TAILQ_EMPTY(&tog_refs)) {
2857 error = tog_load_refs(repo, 0);
2862 if (start_commit == NULL) {
2863 error = got_repo_match_object_id(&start_id, &label,
2864 worktree ? got_worktree_get_head_ref_name(worktree) :
2865 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2868 head_ref_name = label;
2870 error = got_ref_open(&ref, repo, start_commit, 0);
2872 head_ref_name = got_ref_get_name(ref);
2873 else if (error->code != GOT_ERR_NOT_REF)
2875 error = got_repo_match_object_id(&start_id, NULL,
2876 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2881 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2883 error = got_error_from_errno("view_open");
2886 error = open_log_view(view, start_id, repo, head_ref_name,
2887 in_repo_path, log_branches);
2891 /* Release work tree lock. */
2892 got_worktree_close(worktree);
2895 error = view_loop(view);
2905 const struct got_error *close_err = got_repo_close(repo);
2910 got_worktree_close(worktree);
2919 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2920 "[-w] object1 object2\n", getprogname());
2925 match_line(const char *line, regex_t *regex, size_t nmatch,
2926 regmatch_t *regmatch)
2928 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2932 match_color(struct tog_colors *colors, const char *line)
2934 struct tog_color *tc = NULL;
2936 STAILQ_FOREACH(tc, colors, entry) {
2937 if (match_line(line, &tc->regex, 0, NULL))
2944 static const struct got_error *
2945 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2946 WINDOW *window, regmatch_t *regmatch)
2948 const struct got_error *err = NULL;
2955 s = strndup(line, regmatch->rm_so);
2957 return got_error_from_errno("strndup");
2959 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2964 waddwstr(window, wline);
2971 s = strndup(line + regmatch->rm_so,
2972 regmatch->rm_eo - regmatch->rm_so);
2974 err = got_error_from_errno("strndup");
2978 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2983 wattr_on(window, A_STANDOUT, NULL);
2984 waddwstr(window, wline);
2985 wattr_off(window, A_STANDOUT, NULL);
2992 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2993 err = format_line(&wline, &width,
2994 line + regmatch->rm_eo, wlimit, col_tab_align);
2997 waddwstr(window, wline);
3005 static const struct got_error *
3006 draw_file(struct tog_view *view, const char *header)
3008 struct tog_diff_view_state *s = &view->state.diff;
3009 regmatch_t *regmatch = &view->regmatch;
3010 const struct got_error *err;
3013 size_t linesize = 0;
3015 struct tog_color *tc;
3018 int max_lines = view->nlines;
3019 int nlines = s->nlines;
3022 line_offset = s->line_offsets[s->first_displayed_line - 1];
3023 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3024 return got_error_from_errno("fseek");
3026 werase(view->window);
3029 if (asprintf(&line, "[%d/%d] %s",
3030 s->first_displayed_line - 1 + s->selected_line, nlines,
3032 return got_error_from_errno("asprintf");
3033 err = format_line(&wline, &width, line, view->ncols, 0);
3038 if (view_needs_focus_indication(view))
3039 wstandout(view->window);
3040 waddwstr(view->window, wline);
3043 if (view_needs_focus_indication(view))
3044 wstandend(view->window);
3045 if (width <= view->ncols - 1)
3046 waddch(view->window, '\n');
3055 while (max_lines > 0 && nprinted < max_lines) {
3056 linelen = getline(&line, &linesize, s->f);
3057 if (linelen == -1) {
3063 return got_ferror(s->f, GOT_ERR_IO);
3066 tc = match_color(&s->colors, line);
3068 wattr_on(view->window,
3069 COLOR_PAIR(tc->colorpair), NULL);
3070 if (s->first_displayed_line + nprinted == s->matched_line &&
3071 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3072 err = add_matched_line(&width, line, view->ncols, 0,
3073 view->window, regmatch);
3079 err = format_line(&wline, &width, line, view->ncols, 0);
3084 waddwstr(view->window, wline);
3089 wattr_off(view->window,
3090 COLOR_PAIR(tc->colorpair), NULL);
3091 if (width <= view->ncols - 1)
3092 waddch(view->window, '\n');
3097 s->last_displayed_line = s->first_displayed_line +
3100 s->last_displayed_line = s->first_displayed_line;
3105 while (nprinted < view->nlines) {
3106 waddch(view->window, '\n');
3110 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3115 wstandout(view->window);
3116 waddwstr(view->window, wline);
3119 wstandend(view->window);
3126 get_datestr(time_t *time, char *datebuf)
3128 struct tm mytm, *tm;
3131 tm = gmtime_r(time, &mytm);
3134 s = asctime_r(tm, datebuf);
3137 p = strchr(s, '\n');
3143 static const struct got_error *
3144 get_changed_paths(struct got_pathlist_head *paths,
3145 struct got_commit_object *commit, struct got_repository *repo)
3147 const struct got_error *err = NULL;
3148 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3149 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3150 struct got_object_qid *qid;
3152 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3154 struct got_commit_object *pcommit;
3155 err = got_object_open_as_commit(&pcommit, repo,
3160 tree_id1 = got_object_id_dup(
3161 got_object_commit_get_tree_id(pcommit));
3162 if (tree_id1 == NULL) {
3163 got_object_commit_close(pcommit);
3164 return got_error_from_errno("got_object_id_dup");
3166 got_object_commit_close(pcommit);
3171 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3176 tree_id2 = got_object_commit_get_tree_id(commit);
3177 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3181 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3182 got_diff_tree_collect_changed_paths, paths, 0);
3185 got_object_tree_close(tree1);
3187 got_object_tree_close(tree2);
3192 static const struct got_error *
3193 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3197 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3199 return got_error_from_errno("reallocarray");
3201 (*line_offsets)[*nlines] = off;
3206 static const struct got_error *
3207 write_commit_info(off_t **line_offsets, size_t *nlines,
3208 struct got_object_id *commit_id, struct got_reflist_head *refs,
3209 struct got_repository *repo, FILE *outfile)
3211 const struct got_error *err = NULL;
3212 char datebuf[26], *datestr;
3213 struct got_commit_object *commit;
3214 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3215 time_t committer_time;
3216 const char *author, *committer;
3217 char *refs_str = NULL;
3218 struct got_pathlist_head changed_paths;
3219 struct got_pathlist_entry *pe;
3223 TAILQ_INIT(&changed_paths);
3226 err = build_refs_str(&refs_str, refs, commit_id, repo);
3231 err = got_object_open_as_commit(&commit, repo, commit_id);
3235 err = got_object_id_str(&id_str, commit_id);
3237 err = got_error_from_errno("got_object_id_str");
3241 err = add_line_offset(line_offsets, nlines, 0);
3245 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3246 refs_str ? refs_str : "", refs_str ? ")" : "");
3248 err = got_error_from_errno("fprintf");
3252 err = add_line_offset(line_offsets, nlines, outoff);
3256 n = fprintf(outfile, "from: %s\n",
3257 got_object_commit_get_author(commit));
3259 err = got_error_from_errno("fprintf");
3263 err = add_line_offset(line_offsets, nlines, outoff);
3267 committer_time = got_object_commit_get_committer_time(commit);
3268 datestr = get_datestr(&committer_time, datebuf);
3270 n = fprintf(outfile, "date: %s UTC\n", datestr);
3272 err = got_error_from_errno("fprintf");
3276 err = add_line_offset(line_offsets, nlines, outoff);
3280 author = got_object_commit_get_author(commit);
3281 committer = got_object_commit_get_committer(commit);
3282 if (strcmp(author, committer) != 0) {
3283 n = fprintf(outfile, "via: %s\n", committer);
3285 err = got_error_from_errno("fprintf");
3289 err = add_line_offset(line_offsets, nlines, outoff);
3293 if (got_object_commit_get_nparents(commit) > 1) {
3294 const struct got_object_id_queue *parent_ids;
3295 struct got_object_qid *qid;
3297 parent_ids = got_object_commit_get_parent_ids(commit);
3298 STAILQ_FOREACH(qid, parent_ids, entry) {
3299 err = got_object_id_str(&id_str, &qid->id);
3302 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3304 err = got_error_from_errno("fprintf");
3308 err = add_line_offset(line_offsets, nlines, outoff);
3316 err = got_object_commit_get_logmsg(&logmsg, commit);
3320 while ((line = strsep(&s, "\n")) != NULL) {
3321 n = fprintf(outfile, "%s\n", line);
3323 err = got_error_from_errno("fprintf");
3327 err = add_line_offset(line_offsets, nlines, outoff);
3332 err = get_changed_paths(&changed_paths, commit, repo);
3335 TAILQ_FOREACH(pe, &changed_paths, entry) {
3336 struct got_diff_changed_path *cp = pe->data;
3337 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3339 err = got_error_from_errno("fprintf");
3343 err = add_line_offset(line_offsets, nlines, outoff);
3346 free((char *)pe->path);
3350 fputc('\n', outfile);
3352 err = add_line_offset(line_offsets, nlines, outoff);
3354 got_pathlist_free(&changed_paths);
3358 got_object_commit_close(commit);
3360 free(*line_offsets);
3361 *line_offsets = NULL;
3367 static const struct got_error *
3368 create_diff(struct tog_diff_view_state *s)
3370 const struct got_error *err = NULL;
3374 free(s->line_offsets);
3375 s->line_offsets = malloc(sizeof(off_t));
3376 if (s->line_offsets == NULL)
3377 return got_error_from_errno("malloc");
3382 err = got_error_from_errno("got_opentemp");
3385 if (s->f && fclose(s->f) == EOF) {
3386 err = got_error_from_errno("fclose");
3392 err = got_object_get_type(&obj_type, s->repo, s->id1);
3394 err = got_object_get_type(&obj_type, s->repo, s->id2);
3399 case GOT_OBJ_TYPE_BLOB:
3400 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3401 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3402 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3405 case GOT_OBJ_TYPE_TREE:
3406 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3407 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3408 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3410 case GOT_OBJ_TYPE_COMMIT: {
3411 const struct got_object_id_queue *parent_ids;
3412 struct got_object_qid *pid;
3413 struct got_commit_object *commit2;
3414 struct got_reflist_head *refs;
3416 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3419 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3420 /* Show commit info if we're diffing to a parent/root commit. */
3421 if (s->id1 == NULL) {
3422 err = write_commit_info(&s->line_offsets, &s->nlines,
3423 s->id2, refs, s->repo, s->f);
3427 parent_ids = got_object_commit_get_parent_ids(commit2);
3428 STAILQ_FOREACH(pid, parent_ids, entry) {
3429 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3430 err = write_commit_info(
3431 &s->line_offsets, &s->nlines,
3432 s->id2, refs, s->repo, s->f);
3439 got_object_commit_close(commit2);
3441 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3442 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3443 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3447 err = got_error(GOT_ERR_OBJ_TYPE);
3453 if (s->f && fflush(s->f) != 0 && err == NULL)
3454 err = got_error_from_errno("fflush");
3459 diff_view_indicate_progress(struct tog_view *view)
3461 mvwaddstr(view->window, 0, 0, "diffing...");
3466 static const struct got_error *
3467 search_start_diff_view(struct tog_view *view)
3469 struct tog_diff_view_state *s = &view->state.diff;
3471 s->matched_line = 0;
3475 static const struct got_error *
3476 search_next_diff_view(struct tog_view *view)
3478 struct tog_diff_view_state *s = &view->state.diff;
3481 size_t linesize = 0;
3484 if (!view->searching) {
3485 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3489 if (s->matched_line) {
3490 if (view->searching == TOG_SEARCH_FORWARD)
3491 lineno = s->matched_line + 1;
3493 lineno = s->matched_line - 1;
3495 lineno = s->first_displayed_line;
3500 if (lineno <= 0 || lineno > s->nlines) {
3501 if (s->matched_line == 0) {
3502 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3506 if (view->searching == TOG_SEARCH_FORWARD)
3512 offset = s->line_offsets[lineno - 1];
3513 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3515 return got_error_from_errno("fseeko");
3517 linelen = getline(&line, &linesize, s->f);
3518 if (linelen != -1 &&
3519 match_line(line, &view->regex, 1, &view->regmatch)) {
3520 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3521 s->matched_line = lineno;
3524 if (view->searching == TOG_SEARCH_FORWARD)
3531 if (s->matched_line) {
3532 s->first_displayed_line = s->matched_line;
3533 s->selected_line = 1;
3539 static const struct got_error *
3540 close_diff_view(struct tog_view *view)
3542 const struct got_error *err = NULL;
3543 struct tog_diff_view_state *s = &view->state.diff;
3549 if (s->f && fclose(s->f) == EOF)
3550 err = got_error_from_errno("fclose");
3552 if (s->f1 && fclose(s->f1) == EOF)
3553 err = got_error_from_errno("fclose");
3555 if (s->f2 && fclose(s->f2) == EOF)
3556 err = got_error_from_errno("fclose");
3558 free_colors(&s->colors);
3559 free(s->line_offsets);
3560 s->line_offsets = NULL;
3565 static const struct got_error *
3566 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3567 struct got_object_id *id2, const char *label1, const char *label2,
3568 int diff_context, int ignore_whitespace, int force_text_diff,
3569 struct tog_view *log_view, struct got_repository *repo)
3571 const struct got_error *err;
3572 struct tog_diff_view_state *s = &view->state.diff;
3574 memset(s, 0, sizeof(*s));
3576 if (id1 != NULL && id2 != NULL) {
3578 err = got_object_get_type(&type1, repo, id1);
3581 err = got_object_get_type(&type2, repo, id2);
3586 return got_error(GOT_ERR_OBJ_TYPE);
3588 s->first_displayed_line = 1;
3589 s->last_displayed_line = view->nlines;
3590 s->selected_line = 1;
3598 s->id1 = got_object_id_dup(id1);
3600 return got_error_from_errno("got_object_id_dup");
3601 s->f1 = got_opentemp();
3602 if (s->f1 == NULL) {
3603 err = got_error_from_errno("got_opentemp");
3609 s->id2 = got_object_id_dup(id2);
3610 if (s->id2 == NULL) {
3611 err = got_error_from_errno("got_object_id_dup");
3615 s->f2 = got_opentemp();
3616 if (s->f2 == NULL) {
3617 err = got_error_from_errno("got_opentemp");
3621 s->first_displayed_line = 1;
3622 s->last_displayed_line = view->nlines;
3623 s->diff_context = diff_context;
3624 s->ignore_whitespace = ignore_whitespace;
3625 s->force_text_diff = force_text_diff;
3626 s->log_view = log_view;
3629 STAILQ_INIT(&s->colors);
3630 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3631 err = add_color(&s->colors,
3632 "^-", TOG_COLOR_DIFF_MINUS,
3633 get_color_value("TOG_COLOR_DIFF_MINUS"));
3636 err = add_color(&s->colors, "^\\+",
3637 TOG_COLOR_DIFF_PLUS,
3638 get_color_value("TOG_COLOR_DIFF_PLUS"));
3641 err = add_color(&s->colors,
3642 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3643 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3647 err = add_color(&s->colors,
3648 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3649 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3650 get_color_value("TOG_COLOR_DIFF_META"));
3654 err = add_color(&s->colors,
3655 "^(from|via): ", TOG_COLOR_AUTHOR,
3656 get_color_value("TOG_COLOR_AUTHOR"));
3660 err = add_color(&s->colors,
3661 "^date: ", TOG_COLOR_DATE,
3662 get_color_value("TOG_COLOR_DATE"));
3667 if (log_view && view_is_splitscreen(view))
3668 show_log_view(log_view); /* draw vborder */
3669 diff_view_indicate_progress(view);
3671 err = create_diff(s);
3673 view->show = show_diff_view;
3674 view->input = input_diff_view;
3675 view->close = close_diff_view;
3676 view->search_start = search_start_diff_view;
3677 view->search_next = search_next_diff_view;
3680 close_diff_view(view);
3684 static const struct got_error *
3685 show_diff_view(struct tog_view *view)
3687 const struct got_error *err;
3688 struct tog_diff_view_state *s = &view->state.diff;
3689 char *id_str1 = NULL, *id_str2, *header;
3690 const char *label1, *label2;
3693 err = got_object_id_str(&id_str1, s->id1);
3696 label1 = s->label1 ? : id_str1;
3698 label1 = "/dev/null";
3700 err = got_object_id_str(&id_str2, s->id2);
3703 label2 = s->label2 ? : id_str2;
3705 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3706 err = got_error_from_errno("asprintf");
3714 err = draw_file(view, header);
3719 static const struct got_error *
3720 set_selected_commit(struct tog_diff_view_state *s,
3721 struct commit_queue_entry *entry)
3723 const struct got_error *err;
3724 const struct got_object_id_queue *parent_ids;
3725 struct got_commit_object *selected_commit;
3726 struct got_object_qid *pid;
3729 s->id2 = got_object_id_dup(entry->id);
3731 return got_error_from_errno("got_object_id_dup");
3733 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3736 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3738 pid = STAILQ_FIRST(parent_ids);
3739 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3740 got_object_commit_close(selected_commit);
3744 static const struct got_error *
3745 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3747 const struct got_error *err = NULL;
3748 struct tog_diff_view_state *s = &view->state.diff;
3749 struct tog_log_view_state *ls;
3750 struct commit_queue_entry *old_selected_entry;
3752 size_t linesize = 0;
3760 s->force_text_diff = !s->force_text_diff;
3762 s->ignore_whitespace = !s->ignore_whitespace;
3763 wclear(view->window);
3764 s->first_displayed_line = 1;
3765 s->last_displayed_line = view->nlines;
3766 s->matched_line = 0;
3767 diff_view_indicate_progress(view);
3768 err = create_diff(s);
3772 s->first_displayed_line = 1;
3779 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3785 if (s->first_displayed_line > 1)
3786 s->first_displayed_line--;
3790 if (s->first_displayed_line == 1)
3793 while (i++ < view->nlines - 1 &&
3794 s->first_displayed_line > 1)
3795 s->first_displayed_line--;
3801 s->first_displayed_line++;
3809 while (!s->eof && i++ < view->nlines - 1) {
3810 linelen = getline(&line, &linesize, s->f);
3811 s->first_displayed_line++;
3812 if (linelen == -1) {
3816 err = got_ferror(s->f, GOT_ERR_IO);
3823 if (s->diff_context > 0) {
3825 s->matched_line = 0;
3826 diff_view_indicate_progress(view);
3827 err = create_diff(s);
3828 if (s->first_displayed_line + view->nlines - 1 >
3830 s->first_displayed_line = 1;
3831 s->last_displayed_line = view->nlines;
3836 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3838 s->matched_line = 0;
3839 diff_view_indicate_progress(view);
3840 err = create_diff(s);
3845 if (s->log_view == NULL)
3847 ls = &s->log_view->state.log;
3848 old_selected_entry = ls->selected_entry;
3850 err = input_log_view(NULL, s->log_view, KEY_UP);
3854 if (old_selected_entry == ls->selected_entry)
3857 err = set_selected_commit(s, ls->selected_entry);
3861 s->first_displayed_line = 1;
3862 s->last_displayed_line = view->nlines;
3863 s->matched_line = 0;
3865 diff_view_indicate_progress(view);
3866 err = create_diff(s);
3870 if (s->log_view == NULL)
3872 ls = &s->log_view->state.log;
3873 old_selected_entry = ls->selected_entry;
3875 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3879 if (old_selected_entry == ls->selected_entry)
3882 err = set_selected_commit(s, ls->selected_entry);
3886 s->first_displayed_line = 1;
3887 s->last_displayed_line = view->nlines;
3888 s->matched_line = 0;
3890 diff_view_indicate_progress(view);
3891 err = create_diff(s);
3900 static const struct got_error *
3901 cmd_diff(int argc, char *argv[])
3903 const struct got_error *error = NULL;
3904 struct got_repository *repo = NULL;
3905 struct got_worktree *worktree = NULL;
3906 struct got_object_id *id1 = NULL, *id2 = NULL;
3907 char *repo_path = NULL, *cwd = NULL;
3908 char *id_str1 = NULL, *id_str2 = NULL;
3909 char *label1 = NULL, *label2 = NULL;
3910 int diff_context = 3, ignore_whitespace = 0;
3911 int ch, force_text_diff = 0;
3913 struct tog_view *view;
3915 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3918 force_text_diff = 1;
3921 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3924 errx(1, "number of context lines is %s: %s",
3928 repo_path = realpath(optarg, NULL);
3929 if (repo_path == NULL)
3930 return got_error_from_errno2("realpath",
3932 got_path_strip_trailing_slashes(repo_path);
3935 ignore_whitespace = 1;
3947 usage_diff(); /* TODO show local worktree changes */
3948 } else if (argc == 2) {
3954 if (repo_path == NULL) {
3955 cwd = getcwd(NULL, 0);
3957 return got_error_from_errno("getcwd");
3958 error = got_worktree_open(&worktree, cwd);
3959 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3963 strdup(got_worktree_get_repo_path(worktree));
3965 repo_path = strdup(cwd);
3966 if (repo_path == NULL) {
3967 error = got_error_from_errno("strdup");
3972 error = got_repo_open(&repo, repo_path, NULL);
3978 error = apply_unveil(got_repo_get_path(repo), NULL);
3982 error = tog_load_refs(repo, 0);
3986 error = got_repo_match_object_id(&id1, &label1, id_str1,
3987 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3991 error = got_repo_match_object_id(&id2, &label2, id_str2,
3992 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3996 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3998 error = got_error_from_errno("view_open");
4001 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4002 ignore_whitespace, force_text_diff, NULL, repo);
4005 error = view_loop(view);
4012 const struct got_error *close_err = got_repo_close(repo);
4017 got_worktree_close(worktree);
4026 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4031 struct tog_blame_line {
4033 struct got_object_id *id;
4036 static const struct got_error *
4037 draw_blame(struct tog_view *view)
4039 struct tog_blame_view_state *s = &view->state.blame;
4040 struct tog_blame *blame = &s->blame;
4041 regmatch_t *regmatch = &view->regmatch;
4042 const struct got_error *err;
4043 int lineno = 0, nprinted = 0;
4045 size_t linesize = 0;
4049 struct tog_blame_line *blame_line;
4050 struct got_object_id *prev_id = NULL;
4052 struct tog_color *tc;
4054 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4059 werase(view->window);
4061 if (asprintf(&line, "commit %s", id_str) == -1) {
4062 err = got_error_from_errno("asprintf");
4067 err = format_line(&wline, &width, line, view->ncols, 0);
4072 if (view_needs_focus_indication(view))
4073 wstandout(view->window);
4074 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4076 wattr_on(view->window,
4077 COLOR_PAIR(tc->colorpair), NULL);
4078 waddwstr(view->window, wline);
4080 wattr_off(view->window,
4081 COLOR_PAIR(tc->colorpair), NULL);
4082 if (view_needs_focus_indication(view))
4083 wstandend(view->window);
4086 if (width < view->ncols - 1)
4087 waddch(view->window, '\n');
4089 if (asprintf(&line, "[%d/%d] %s%s",
4090 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4091 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4093 return got_error_from_errno("asprintf");
4096 err = format_line(&wline, &width, line, view->ncols, 0);
4101 waddwstr(view->window, wline);
4104 if (width < view->ncols - 1)
4105 waddch(view->window, '\n');
4108 while (nprinted < view->nlines - 2) {
4109 linelen = getline(&line, &linesize, blame->f);
4110 if (linelen == -1) {
4111 if (feof(blame->f)) {
4116 return got_ferror(blame->f, GOT_ERR_IO);
4118 if (++lineno < s->first_displayed_line)
4121 if (view->focussed && nprinted == s->selected_line - 1)
4122 wstandout(view->window);
4124 if (blame->nlines > 0) {
4125 blame_line = &blame->lines[lineno - 1];
4126 if (blame_line->annotated && prev_id &&
4127 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4129 nprinted == s->selected_line - 1)) {
4130 waddstr(view->window, " ");
4131 } else if (blame_line->annotated) {
4133 err = got_object_id_str(&id_str, blame_line->id);
4138 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4140 wattr_on(view->window,
4141 COLOR_PAIR(tc->colorpair), NULL);
4142 wprintw(view->window, "%.8s", id_str);
4144 wattr_off(view->window,
4145 COLOR_PAIR(tc->colorpair), NULL);
4147 prev_id = blame_line->id;
4149 waddstr(view->window, "........");
4153 waddstr(view->window, "........");
4157 if (view->focussed && nprinted == s->selected_line - 1)
4158 wstandend(view->window);
4159 waddstr(view->window, " ");
4161 if (view->ncols <= 9) {
4163 wline = wcsdup(L"");
4164 if (wline == NULL) {
4165 err = got_error_from_errno("wcsdup");
4169 } else if (s->first_displayed_line + nprinted ==
4171 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4172 err = add_matched_line(&width, line, view->ncols - 9, 9,
4173 view->window, regmatch);
4180 err = format_line(&wline, &width, line,
4181 view->ncols - 9, 9);
4182 waddwstr(view->window, wline);
4188 if (width <= view->ncols - 1)
4189 waddch(view->window, '\n');
4190 if (++nprinted == 1)
4191 s->first_displayed_line = lineno;
4194 s->last_displayed_line = lineno;
4201 static const struct got_error *
4202 blame_cb(void *arg, int nlines, int lineno,
4203 struct got_commit_object *commit, struct got_object_id *id)
4205 const struct got_error *err = NULL;
4206 struct tog_blame_cb_args *a = arg;
4207 struct tog_blame_line *line;
4210 if (nlines != a->nlines ||
4211 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4212 return got_error(GOT_ERR_RANGE);
4214 errcode = pthread_mutex_lock(&tog_mutex);
4216 return got_error_set_errno(errcode, "pthread_mutex_lock");
4218 if (*a->quit) { /* user has quit the blame view */
4219 err = got_error(GOT_ERR_ITER_COMPLETED);
4224 goto done; /* no change in this commit */
4226 line = &a->lines[lineno - 1];
4227 if (line->annotated)
4230 line->id = got_object_id_dup(id);
4231 if (line->id == NULL) {
4232 err = got_error_from_errno("got_object_id_dup");
4235 line->annotated = 1;
4237 errcode = pthread_mutex_unlock(&tog_mutex);
4239 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4244 blame_thread(void *arg)
4246 const struct got_error *err, *close_err;
4247 struct tog_blame_thread_args *ta = arg;
4248 struct tog_blame_cb_args *a = ta->cb_args;
4251 err = block_signals_used_by_main_thread();
4255 err = got_blame(ta->path, a->commit_id, ta->repo,
4256 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4257 if (err && err->code == GOT_ERR_CANCELLED)
4260 errcode = pthread_mutex_lock(&tog_mutex);
4262 return (void *)got_error_set_errno(errcode,
4263 "pthread_mutex_lock");
4265 close_err = got_repo_close(ta->repo);
4271 errcode = pthread_mutex_unlock(&tog_mutex);
4272 if (errcode && err == NULL)
4273 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4278 static struct got_object_id *
4279 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4280 int first_displayed_line, int selected_line)
4282 struct tog_blame_line *line;
4287 line = &lines[first_displayed_line - 1 + selected_line - 1];
4288 if (!line->annotated)
4294 static const struct got_error *
4295 stop_blame(struct tog_blame *blame)
4297 const struct got_error *err = NULL;
4300 if (blame->thread) {
4302 errcode = pthread_mutex_unlock(&tog_mutex);
4304 return got_error_set_errno(errcode,
4305 "pthread_mutex_unlock");
4306 errcode = pthread_join(blame->thread, (void **)&err);
4308 return got_error_set_errno(errcode, "pthread_join");
4309 errcode = pthread_mutex_lock(&tog_mutex);
4311 return got_error_set_errno(errcode,
4312 "pthread_mutex_lock");
4313 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4315 blame->thread = NULL;
4317 if (blame->thread_args.repo) {
4318 const struct got_error *close_err;
4319 close_err = got_repo_close(blame->thread_args.repo);
4322 blame->thread_args.repo = NULL;
4325 if (fclose(blame->f) == EOF && err == NULL)
4326 err = got_error_from_errno("fclose");
4330 for (i = 0; i < blame->nlines; i++)
4331 free(blame->lines[i].id);
4333 blame->lines = NULL;
4335 free(blame->cb_args.commit_id);
4336 blame->cb_args.commit_id = NULL;
4341 static const struct got_error *
4342 cancel_blame_view(void *arg)
4344 const struct got_error *err = NULL;
4348 errcode = pthread_mutex_lock(&tog_mutex);
4350 return got_error_set_errno(errcode,
4351 "pthread_mutex_unlock");
4354 err = got_error(GOT_ERR_CANCELLED);
4356 errcode = pthread_mutex_unlock(&tog_mutex);
4358 return got_error_set_errno(errcode,
4359 "pthread_mutex_lock");
4364 static const struct got_error *
4365 run_blame(struct tog_view *view)
4367 struct tog_blame_view_state *s = &view->state.blame;
4368 struct tog_blame *blame = &s->blame;
4369 const struct got_error *err = NULL;
4370 struct got_commit_object *commit = NULL;
4371 struct got_blob_object *blob = NULL;
4372 struct got_repository *thread_repo = NULL;
4373 struct got_object_id *obj_id = NULL;
4376 err = got_object_open_as_commit(&commit, s->repo,
4377 &s->blamed_commit->id);
4381 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4385 err = got_object_get_type(&obj_type, s->repo, obj_id);
4389 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4390 err = got_error(GOT_ERR_OBJ_TYPE);
4394 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4397 blame->f = got_opentemp();
4398 if (blame->f == NULL) {
4399 err = got_error_from_errno("got_opentemp");
4402 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4403 &blame->line_offsets, blame->f, blob);
4406 if (blame->nlines == 0) {
4407 s->blame_complete = 1;
4411 /* Don't include \n at EOF in the blame line count. */
4412 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4415 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4416 if (blame->lines == NULL) {
4417 err = got_error_from_errno("calloc");
4421 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4425 blame->cb_args.view = view;
4426 blame->cb_args.lines = blame->lines;
4427 blame->cb_args.nlines = blame->nlines;
4428 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4429 if (blame->cb_args.commit_id == NULL) {
4430 err = got_error_from_errno("got_object_id_dup");
4433 blame->cb_args.quit = &s->done;
4435 blame->thread_args.path = s->path;
4436 blame->thread_args.repo = thread_repo;
4437 blame->thread_args.cb_args = &blame->cb_args;
4438 blame->thread_args.complete = &s->blame_complete;
4439 blame->thread_args.cancel_cb = cancel_blame_view;
4440 blame->thread_args.cancel_arg = &s->done;
4441 s->blame_complete = 0;
4443 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4444 s->first_displayed_line = 1;
4445 s->last_displayed_line = view->nlines;
4446 s->selected_line = 1;
4448 s->matched_line = 0;
4452 got_object_commit_close(commit);
4454 got_object_blob_close(blob);
4461 static const struct got_error *
4462 open_blame_view(struct tog_view *view, char *path,
4463 struct got_object_id *commit_id, struct got_repository *repo)
4465 const struct got_error *err = NULL;
4466 struct tog_blame_view_state *s = &view->state.blame;
4468 STAILQ_INIT(&s->blamed_commits);
4470 s->path = strdup(path);
4471 if (s->path == NULL)
4472 return got_error_from_errno("strdup");
4474 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4480 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4481 s->first_displayed_line = 1;
4482 s->last_displayed_line = view->nlines;
4483 s->selected_line = 1;
4484 s->blame_complete = 0;
4486 s->commit_id = commit_id;
4487 memset(&s->blame, 0, sizeof(s->blame));
4489 STAILQ_INIT(&s->colors);
4490 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4491 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4492 get_color_value("TOG_COLOR_COMMIT"));
4497 view->show = show_blame_view;
4498 view->input = input_blame_view;
4499 view->close = close_blame_view;
4500 view->search_start = search_start_blame_view;
4501 view->search_next = search_next_blame_view;
4503 return run_blame(view);
4506 static const struct got_error *
4507 close_blame_view(struct tog_view *view)
4509 const struct got_error *err = NULL;
4510 struct tog_blame_view_state *s = &view->state.blame;
4512 if (s->blame.thread)
4513 err = stop_blame(&s->blame);
4515 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4516 struct got_object_qid *blamed_commit;
4517 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4518 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4519 got_object_qid_free(blamed_commit);
4523 free_colors(&s->colors);
4528 static const struct got_error *
4529 search_start_blame_view(struct tog_view *view)
4531 struct tog_blame_view_state *s = &view->state.blame;
4533 s->matched_line = 0;
4537 static const struct got_error *
4538 search_next_blame_view(struct tog_view *view)
4540 struct tog_blame_view_state *s = &view->state.blame;
4543 size_t linesize = 0;
4546 if (!view->searching) {
4547 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4551 if (s->matched_line) {
4552 if (view->searching == TOG_SEARCH_FORWARD)
4553 lineno = s->matched_line + 1;
4555 lineno = s->matched_line - 1;
4557 lineno = s->first_displayed_line - 1 + s->selected_line;
4562 if (lineno <= 0 || lineno > s->blame.nlines) {
4563 if (s->matched_line == 0) {
4564 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4568 if (view->searching == TOG_SEARCH_FORWARD)
4571 lineno = s->blame.nlines;
4574 offset = s->blame.line_offsets[lineno - 1];
4575 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4577 return got_error_from_errno("fseeko");
4579 linelen = getline(&line, &linesize, s->blame.f);
4580 if (linelen != -1 &&
4581 match_line(line, &view->regex, 1, &view->regmatch)) {
4582 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4583 s->matched_line = lineno;
4586 if (view->searching == TOG_SEARCH_FORWARD)
4593 if (s->matched_line) {
4594 s->first_displayed_line = s->matched_line;
4595 s->selected_line = 1;
4601 static const struct got_error *
4602 show_blame_view(struct tog_view *view)
4604 const struct got_error *err = NULL;
4605 struct tog_blame_view_state *s = &view->state.blame;
4608 if (s->blame.thread == NULL && !s->blame_complete) {
4609 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4610 &s->blame.thread_args);
4612 return got_error_set_errno(errcode, "pthread_create");
4614 halfdelay(1); /* fast refresh while annotating */
4617 if (s->blame_complete)
4618 halfdelay(10); /* disable fast refresh */
4620 err = draw_blame(view);
4626 static const struct got_error *
4627 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4629 const struct got_error *err = NULL, *thread_err = NULL;
4630 struct tog_view *diff_view;
4631 struct tog_blame_view_state *s = &view->state.blame;
4640 s->selected_line = 1;
4641 s->first_displayed_line = 1;
4645 if (s->blame.nlines < view->nlines - 2) {
4646 s->selected_line = s->blame.nlines;
4647 s->first_displayed_line = 1;
4649 s->selected_line = view->nlines - 2;
4650 s->first_displayed_line = s->blame.nlines -
4657 if (s->selected_line > 1)
4659 else if (s->selected_line == 1 &&
4660 s->first_displayed_line > 1)
4661 s->first_displayed_line--;
4665 if (s->first_displayed_line == 1) {
4666 s->selected_line = 1;
4669 if (s->first_displayed_line > view->nlines - 2)
4670 s->first_displayed_line -=
4673 s->first_displayed_line = 1;
4678 if (s->selected_line < view->nlines - 2 &&
4679 s->first_displayed_line +
4680 s->selected_line <= s->blame.nlines)
4682 else if (s->last_displayed_line <
4684 s->first_displayed_line++;
4688 struct got_object_id *id = NULL;
4689 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4690 s->first_displayed_line, s->selected_line);
4694 struct got_commit_object *commit, *pcommit;
4695 struct got_object_qid *pid;
4696 struct got_object_id *blob_id = NULL;
4698 err = got_object_open_as_commit(&commit,
4703 got_object_commit_get_parent_ids(commit));
4705 got_object_commit_close(commit);
4708 /* Check if path history ends here. */
4709 err = got_object_open_as_commit(&pcommit,
4713 err = got_object_id_by_path(&blob_id, s->repo,
4715 got_object_commit_close(pcommit);
4717 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4719 got_object_commit_close(commit);
4722 err = got_object_get_type(&obj_type, s->repo,
4725 /* Can't blame non-blob type objects. */
4726 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4727 got_object_commit_close(commit);
4730 err = got_object_qid_alloc(&s->blamed_commit,
4732 got_object_commit_close(commit);
4734 if (got_object_id_cmp(id,
4735 &s->blamed_commit->id) == 0)
4737 err = got_object_qid_alloc(&s->blamed_commit,
4743 thread_err = stop_blame(&s->blame);
4747 STAILQ_INSERT_HEAD(&s->blamed_commits,
4748 s->blamed_commit, entry);
4749 err = run_blame(view);
4755 struct got_object_qid *first;
4756 first = STAILQ_FIRST(&s->blamed_commits);
4757 if (!got_object_id_cmp(&first->id, s->commit_id))
4760 thread_err = stop_blame(&s->blame);
4764 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4765 got_object_qid_free(s->blamed_commit);
4767 STAILQ_FIRST(&s->blamed_commits);
4768 err = run_blame(view);
4775 struct got_object_id *id = NULL;
4776 struct got_object_qid *pid;
4777 struct got_commit_object *commit = NULL;
4778 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4779 s->first_displayed_line, s->selected_line);
4782 err = got_object_open_as_commit(&commit, s->repo, id);
4786 got_object_commit_get_parent_ids(commit));
4787 if (view_is_parent_view(view))
4788 begin_x = view_split_begin_x(view->begin_x);
4789 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4790 if (diff_view == NULL) {
4791 got_object_commit_close(commit);
4792 err = got_error_from_errno("view_open");
4795 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4796 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4797 got_object_commit_close(commit);
4799 view_close(diff_view);
4803 diff_view->focussed = 1;
4804 if (view_is_parent_view(view)) {
4805 err = view_close_child(view);
4808 view_set_child(view, diff_view);
4809 view->focus_child = 1;
4811 *new_view = diff_view;
4819 if (s->last_displayed_line >= s->blame.nlines &&
4820 s->selected_line >= MIN(s->blame.nlines,
4821 view->nlines - 2)) {
4824 if (s->last_displayed_line >= s->blame.nlines &&
4825 s->selected_line < view->nlines - 2) {
4826 s->selected_line = MIN(s->blame.nlines,
4830 if (s->last_displayed_line + view->nlines - 2
4832 s->first_displayed_line +=
4835 s->first_displayed_line =
4840 if (s->selected_line > view->nlines - 2) {
4841 s->selected_line = MIN(s->blame.nlines,
4848 return thread_err ? thread_err : err;
4851 static const struct got_error *
4852 cmd_blame(int argc, char *argv[])
4854 const struct got_error *error;
4855 struct got_repository *repo = NULL;
4856 struct got_worktree *worktree = NULL;
4857 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4858 char *link_target = NULL;
4859 struct got_object_id *commit_id = NULL;
4860 struct got_commit_object *commit = NULL;
4861 char *commit_id_str = NULL;
4863 struct tog_view *view;
4865 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4868 commit_id_str = optarg;
4871 repo_path = realpath(optarg, NULL);
4872 if (repo_path == NULL)
4873 return got_error_from_errno2("realpath",
4888 if (repo_path == NULL) {
4889 cwd = getcwd(NULL, 0);
4891 return got_error_from_errno("getcwd");
4892 error = got_worktree_open(&worktree, cwd);
4893 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4897 strdup(got_worktree_get_repo_path(worktree));
4899 repo_path = strdup(cwd);
4900 if (repo_path == NULL) {
4901 error = got_error_from_errno("strdup");
4906 error = got_repo_open(&repo, repo_path, NULL);
4910 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4917 error = apply_unveil(got_repo_get_path(repo), NULL);
4921 error = tog_load_refs(repo, 0);
4925 if (commit_id_str == NULL) {
4926 struct got_reference *head_ref;
4927 error = got_ref_open(&head_ref, repo, worktree ?
4928 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4931 error = got_ref_resolve(&commit_id, repo, head_ref);
4932 got_ref_close(head_ref);
4934 error = got_repo_match_object_id(&commit_id, NULL,
4935 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4940 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4942 error = got_error_from_errno("view_open");
4946 error = got_object_open_as_commit(&commit, repo, commit_id);
4950 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4955 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4960 /* Release work tree lock. */
4961 got_worktree_close(worktree);
4964 error = view_loop(view);
4972 got_object_commit_close(commit);
4974 got_worktree_close(worktree);
4976 const struct got_error *close_err = got_repo_close(repo);
4984 static const struct got_error *
4985 draw_tree_entries(struct tog_view *view, const char *parent_path)
4987 struct tog_tree_view_state *s = &view->state.tree;
4988 const struct got_error *err = NULL;
4989 struct got_tree_entry *te;
4991 struct tog_color *tc;
4992 int width, n, i, nentries;
4993 int limit = view->nlines;
4997 werase(view->window);
5002 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5005 if (view_needs_focus_indication(view))
5006 wstandout(view->window);
5007 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5009 wattr_on(view->window,
5010 COLOR_PAIR(tc->colorpair), NULL);
5011 waddwstr(view->window, wline);
5013 wattr_off(view->window,
5014 COLOR_PAIR(tc->colorpair), NULL);
5015 if (view_needs_focus_indication(view))
5016 wstandend(view->window);
5019 if (width < view->ncols - 1)
5020 waddch(view->window, '\n');
5023 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5026 waddwstr(view->window, wline);
5029 if (width < view->ncols - 1)
5030 waddch(view->window, '\n');
5033 waddch(view->window, '\n');
5037 if (s->first_displayed_entry == NULL) {
5038 te = got_object_tree_get_first_entry(s->tree);
5039 if (s->selected == 0) {
5041 wstandout(view->window);
5042 s->selected_entry = NULL;
5044 waddstr(view->window, " ..\n"); /* parent directory */
5045 if (s->selected == 0 && view->focussed)
5046 wstandend(view->window);
5053 te = s->first_displayed_entry;
5056 nentries = got_object_tree_get_nentries(s->tree);
5057 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5058 char *line = NULL, *id_str = NULL, *link_target = NULL;
5059 const char *modestr = "";
5062 te = got_object_tree_get_entry(s->tree, i);
5063 mode = got_tree_entry_get_mode(te);
5066 err = got_object_id_str(&id_str,
5067 got_tree_entry_get_id(te));
5069 return got_error_from_errno(
5070 "got_object_id_str");
5072 if (got_object_tree_entry_is_submodule(te))
5074 else if (S_ISLNK(mode)) {
5077 err = got_tree_entry_get_symlink_target(&link_target,
5083 for (i = 0; i < strlen(link_target); i++) {
5084 if (!isprint((unsigned char)link_target[i]))
5085 link_target[i] = '?';
5089 else if (S_ISDIR(mode))
5091 else if (mode & S_IXUSR)
5093 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5094 got_tree_entry_get_name(te), modestr,
5095 link_target ? " -> ": "",
5096 link_target ? link_target : "") == -1) {
5099 return got_error_from_errno("asprintf");
5103 err = format_line(&wline, &width, line, view->ncols, 0);
5108 if (n == s->selected) {
5110 wstandout(view->window);
5111 s->selected_entry = te;
5113 tc = match_color(&s->colors, line);
5115 wattr_on(view->window,
5116 COLOR_PAIR(tc->colorpair), NULL);
5117 waddwstr(view->window, wline);
5119 wattr_off(view->window,
5120 COLOR_PAIR(tc->colorpair), NULL);
5121 if (width < view->ncols - 1)
5122 waddch(view->window, '\n');
5123 if (n == s->selected && view->focussed)
5124 wstandend(view->window);
5130 s->last_displayed_entry = te;
5139 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5141 struct got_tree_entry *te;
5142 int isroot = s->tree == s->root;
5145 if (s->first_displayed_entry == NULL)
5148 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5149 while (i++ < maxscroll) {
5152 s->first_displayed_entry = NULL;
5155 s->first_displayed_entry = te;
5156 te = got_tree_entry_get_prev(s->tree, te);
5161 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5163 struct got_tree_entry *next, *last;
5166 if (s->first_displayed_entry)
5167 next = got_tree_entry_get_next(s->tree,
5168 s->first_displayed_entry);
5170 next = got_object_tree_get_first_entry(s->tree);
5172 last = s->last_displayed_entry;
5173 while (next && last && n++ < maxscroll) {
5174 last = got_tree_entry_get_next(s->tree, last);
5176 s->first_displayed_entry = next;
5177 next = got_tree_entry_get_next(s->tree, next);
5182 static const struct got_error *
5183 tree_entry_path(char **path, struct tog_parent_trees *parents,
5184 struct got_tree_entry *te)
5186 const struct got_error *err = NULL;
5187 struct tog_parent_tree *pt;
5188 size_t len = 2; /* for leading slash and NUL */
5190 TAILQ_FOREACH(pt, parents, entry)
5191 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5194 len += strlen(got_tree_entry_get_name(te));
5196 *path = calloc(1, len);
5198 return got_error_from_errno("calloc");
5201 pt = TAILQ_LAST(parents, tog_parent_trees);
5203 const char *name = got_tree_entry_get_name(pt->selected_entry);
5204 if (strlcat(*path, name, len) >= len) {
5205 err = got_error(GOT_ERR_NO_SPACE);
5208 if (strlcat(*path, "/", len) >= len) {
5209 err = got_error(GOT_ERR_NO_SPACE);
5212 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5215 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5216 err = got_error(GOT_ERR_NO_SPACE);
5228 static const struct got_error *
5229 blame_tree_entry(struct tog_view **new_view, int begin_x,
5230 struct got_tree_entry *te, struct tog_parent_trees *parents,
5231 struct got_object_id *commit_id, struct got_repository *repo)
5233 const struct got_error *err = NULL;
5235 struct tog_view *blame_view;
5239 err = tree_entry_path(&path, parents, te);
5243 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5244 if (blame_view == NULL) {
5245 err = got_error_from_errno("view_open");
5249 err = open_blame_view(blame_view, path, commit_id, repo);
5251 if (err->code == GOT_ERR_CANCELLED)
5253 view_close(blame_view);
5255 *new_view = blame_view;
5261 static const struct got_error *
5262 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5263 struct tog_tree_view_state *s)
5265 struct tog_view *log_view;
5266 const struct got_error *err = NULL;
5271 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5272 if (log_view == NULL)
5273 return got_error_from_errno("view_open");
5275 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5279 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5282 view_close(log_view);
5284 *new_view = log_view;
5289 static const struct got_error *
5290 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5291 const char *head_ref_name, struct got_repository *repo)
5293 const struct got_error *err = NULL;
5294 char *commit_id_str = NULL;
5295 struct tog_tree_view_state *s = &view->state.tree;
5296 struct got_commit_object *commit = NULL;
5298 TAILQ_INIT(&s->parents);
5299 STAILQ_INIT(&s->colors);
5301 s->commit_id = got_object_id_dup(commit_id);
5302 if (s->commit_id == NULL)
5303 return got_error_from_errno("got_object_id_dup");
5305 err = got_object_open_as_commit(&commit, repo, commit_id);
5310 * The root is opened here and will be closed when the view is closed.
5311 * Any visited subtrees and their path-wise parents are opened and
5314 err = got_object_open_as_tree(&s->root, repo,
5315 got_object_commit_get_tree_id(commit));
5320 err = got_object_id_str(&commit_id_str, commit_id);
5324 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5325 err = got_error_from_errno("asprintf");
5329 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5330 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5331 if (head_ref_name) {
5332 s->head_ref_name = strdup(head_ref_name);
5333 if (s->head_ref_name == NULL) {
5334 err = got_error_from_errno("strdup");
5340 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5341 err = add_color(&s->colors, "\\$$",
5342 TOG_COLOR_TREE_SUBMODULE,
5343 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5346 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5347 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5350 err = add_color(&s->colors, "/$",
5351 TOG_COLOR_TREE_DIRECTORY,
5352 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5356 err = add_color(&s->colors, "\\*$",
5357 TOG_COLOR_TREE_EXECUTABLE,
5358 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5362 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5363 get_color_value("TOG_COLOR_COMMIT"));
5368 view->show = show_tree_view;
5369 view->input = input_tree_view;
5370 view->close = close_tree_view;
5371 view->search_start = search_start_tree_view;
5372 view->search_next = search_next_tree_view;
5374 free(commit_id_str);
5376 got_object_commit_close(commit);
5378 close_tree_view(view);
5382 static const struct got_error *
5383 close_tree_view(struct tog_view *view)
5385 struct tog_tree_view_state *s = &view->state.tree;
5387 free_colors(&s->colors);
5388 free(s->tree_label);
5389 s->tree_label = NULL;
5391 s->commit_id = NULL;
5392 free(s->head_ref_name);
5393 s->head_ref_name = NULL;
5394 while (!TAILQ_EMPTY(&s->parents)) {
5395 struct tog_parent_tree *parent;
5396 parent = TAILQ_FIRST(&s->parents);
5397 TAILQ_REMOVE(&s->parents, parent, entry);
5398 if (parent->tree != s->root)
5399 got_object_tree_close(parent->tree);
5403 if (s->tree != NULL && s->tree != s->root)
5404 got_object_tree_close(s->tree);
5406 got_object_tree_close(s->root);
5410 static const struct got_error *
5411 search_start_tree_view(struct tog_view *view)
5413 struct tog_tree_view_state *s = &view->state.tree;
5415 s->matched_entry = NULL;
5420 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5422 regmatch_t regmatch;
5424 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5428 static const struct got_error *
5429 search_next_tree_view(struct tog_view *view)
5431 struct tog_tree_view_state *s = &view->state.tree;
5432 struct got_tree_entry *te = NULL;
5434 if (!view->searching) {
5435 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5439 if (s->matched_entry) {
5440 if (view->searching == TOG_SEARCH_FORWARD) {
5441 if (s->selected_entry)
5442 te = got_tree_entry_get_next(s->tree,
5445 te = got_object_tree_get_first_entry(s->tree);
5447 if (s->selected_entry == NULL)
5448 te = got_object_tree_get_last_entry(s->tree);
5450 te = got_tree_entry_get_prev(s->tree,
5454 if (s->selected_entry)
5455 te = s->selected_entry;
5456 else if (view->searching == TOG_SEARCH_FORWARD)
5457 te = got_object_tree_get_first_entry(s->tree);
5459 te = got_object_tree_get_last_entry(s->tree);
5464 if (s->matched_entry == NULL) {
5465 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5468 if (view->searching == TOG_SEARCH_FORWARD)
5469 te = got_object_tree_get_first_entry(s->tree);
5471 te = got_object_tree_get_last_entry(s->tree);
5474 if (match_tree_entry(te, &view->regex)) {
5475 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5476 s->matched_entry = te;
5480 if (view->searching == TOG_SEARCH_FORWARD)
5481 te = got_tree_entry_get_next(s->tree, te);
5483 te = got_tree_entry_get_prev(s->tree, te);
5486 if (s->matched_entry) {
5487 s->first_displayed_entry = s->matched_entry;
5494 static const struct got_error *
5495 show_tree_view(struct tog_view *view)
5497 const struct got_error *err = NULL;
5498 struct tog_tree_view_state *s = &view->state.tree;
5501 err = tree_entry_path(&parent_path, &s->parents, NULL);
5505 err = draw_tree_entries(view, parent_path);
5512 static const struct got_error *
5513 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5515 const struct got_error *err = NULL;
5516 struct tog_tree_view_state *s = &view->state.tree;
5517 struct tog_view *log_view, *ref_view;
5518 struct got_tree_entry *te;
5523 s->show_ids = !s->show_ids;
5526 if (!s->selected_entry)
5528 if (view_is_parent_view(view))
5529 begin_x = view_split_begin_x(view->begin_x);
5530 err = log_selected_tree_entry(&log_view, begin_x, s);
5532 log_view->focussed = 1;
5533 if (view_is_parent_view(view)) {
5534 err = view_close_child(view);
5537 view_set_child(view, log_view);
5538 view->focus_child = 1;
5540 *new_view = log_view;
5543 if (view_is_parent_view(view))
5544 begin_x = view_split_begin_x(view->begin_x);
5545 ref_view = view_open(view->nlines, view->ncols,
5546 view->begin_y, begin_x, TOG_VIEW_REF);
5547 if (ref_view == NULL)
5548 return got_error_from_errno("view_open");
5549 err = open_ref_view(ref_view, s->repo);
5551 view_close(ref_view);
5555 ref_view->focussed = 1;
5556 if (view_is_parent_view(view)) {
5557 err = view_close_child(view);
5560 view_set_child(view, ref_view);
5561 view->focus_child = 1;
5563 *new_view = ref_view;
5568 if (s->tree == s->root)
5569 s->first_displayed_entry =
5570 got_object_tree_get_first_entry(s->tree);
5572 s->first_displayed_entry = NULL;
5577 te = got_object_tree_get_last_entry(s->tree);
5578 for (n = 0; n < view->nlines - 3; n++) {
5580 if(s->tree != s->root) {
5581 s->first_displayed_entry = NULL;
5586 s->first_displayed_entry = te;
5587 te = got_tree_entry_get_prev(s->tree, te);
5590 s->selected = n - 1;
5595 if (s->selected > 0) {
5599 tree_scroll_up(s, 1);
5603 if (s->tree == s->root) {
5604 if (got_object_tree_get_first_entry(s->tree) ==
5605 s->first_displayed_entry)
5608 if (s->first_displayed_entry == NULL)
5611 tree_scroll_up(s, MAX(0, view->nlines - 3));
5616 if (s->selected < s->ndisplayed - 1) {
5620 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5622 /* can't scroll any further */
5624 tree_scroll_down(s, 1);
5628 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5630 /* can't scroll any further; move cursor down */
5631 if (s->selected < s->ndisplayed - 1)
5632 s->selected = s->ndisplayed - 1;
5635 tree_scroll_down(s, view->nlines - 3);
5640 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5641 struct tog_parent_tree *parent;
5642 /* user selected '..' */
5643 if (s->tree == s->root)
5645 parent = TAILQ_FIRST(&s->parents);
5646 TAILQ_REMOVE(&s->parents, parent,
5648 got_object_tree_close(s->tree);
5649 s->tree = parent->tree;
5650 s->first_displayed_entry =
5651 parent->first_displayed_entry;
5653 parent->selected_entry;
5654 s->selected = parent->selected;
5656 } else if (S_ISDIR(got_tree_entry_get_mode(
5657 s->selected_entry))) {
5658 struct got_tree_object *subtree;
5659 err = got_object_open_as_tree(&subtree, s->repo,
5660 got_tree_entry_get_id(s->selected_entry));
5663 err = tree_view_visit_subtree(s, subtree);
5665 got_object_tree_close(subtree);
5668 } else if (S_ISREG(got_tree_entry_get_mode(
5669 s->selected_entry))) {
5670 struct tog_view *blame_view;
5671 int begin_x = view_is_parent_view(view) ?
5672 view_split_begin_x(view->begin_x) : 0;
5674 err = blame_tree_entry(&blame_view, begin_x,
5675 s->selected_entry, &s->parents,
5676 s->commit_id, s->repo);
5680 blame_view->focussed = 1;
5681 if (view_is_parent_view(view)) {
5682 err = view_close_child(view);
5685 view_set_child(view, blame_view);
5686 view->focus_child = 1;
5688 *new_view = blame_view;
5692 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5693 s->selected = view->nlines - 4;
5706 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5711 static const struct got_error *
5712 cmd_tree(int argc, char *argv[])
5714 const struct got_error *error;
5715 struct got_repository *repo = NULL;
5716 struct got_worktree *worktree = NULL;
5717 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5718 struct got_object_id *commit_id = NULL;
5719 struct got_commit_object *commit = NULL;
5720 const char *commit_id_arg = NULL;
5722 struct got_reference *ref = NULL;
5723 const char *head_ref_name = NULL;
5725 struct tog_view *view;
5727 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5730 commit_id_arg = optarg;
5733 repo_path = realpath(optarg, NULL);
5734 if (repo_path == NULL)
5735 return got_error_from_errno2("realpath",
5750 if (repo_path == NULL) {
5751 cwd = getcwd(NULL, 0);
5753 return got_error_from_errno("getcwd");
5754 error = got_worktree_open(&worktree, cwd);
5755 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5759 strdup(got_worktree_get_repo_path(worktree));
5761 repo_path = strdup(cwd);
5762 if (repo_path == NULL) {
5763 error = got_error_from_errno("strdup");
5768 error = got_repo_open(&repo, repo_path, NULL);
5772 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5779 error = apply_unveil(got_repo_get_path(repo), NULL);
5783 error = tog_load_refs(repo, 0);
5787 if (commit_id_arg == NULL) {
5788 error = got_repo_match_object_id(&commit_id, &label,
5789 worktree ? got_worktree_get_head_ref_name(worktree) :
5790 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5793 head_ref_name = label;
5795 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5797 head_ref_name = got_ref_get_name(ref);
5798 else if (error->code != GOT_ERR_NOT_REF)
5800 error = got_repo_match_object_id(&commit_id, NULL,
5801 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5806 error = got_object_open_as_commit(&commit, repo, commit_id);
5810 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5812 error = got_error_from_errno("view_open");
5815 error = open_tree_view(view, commit_id, head_ref_name, repo);
5818 if (!got_path_is_root_dir(in_repo_path)) {
5819 error = tree_view_walk_path(&view->state.tree, commit,
5826 /* Release work tree lock. */
5827 got_worktree_close(worktree);
5830 error = view_loop(view);
5839 const struct got_error *close_err = got_repo_close(repo);
5847 static const struct got_error *
5848 ref_view_load_refs(struct tog_ref_view_state *s)
5850 struct got_reflist_entry *sre;
5851 struct tog_reflist_entry *re;
5854 TAILQ_FOREACH(sre, &tog_refs, entry) {
5855 if (strncmp(got_ref_get_name(sre->ref),
5856 "refs/got/", 9) == 0 &&
5857 strncmp(got_ref_get_name(sre->ref),
5858 "refs/got/backup/", 16) != 0)
5861 re = malloc(sizeof(*re));
5863 return got_error_from_errno("malloc");
5865 re->ref = got_ref_dup(sre->ref);
5866 if (re->ref == NULL)
5867 return got_error_from_errno("got_ref_dup");
5868 re->idx = s->nrefs++;
5869 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5872 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5877 ref_view_free_refs(struct tog_ref_view_state *s)
5879 struct tog_reflist_entry *re;
5881 while (!TAILQ_EMPTY(&s->refs)) {
5882 re = TAILQ_FIRST(&s->refs);
5883 TAILQ_REMOVE(&s->refs, re, entry);
5884 got_ref_close(re->ref);
5889 static const struct got_error *
5890 open_ref_view(struct tog_view *view, struct got_repository *repo)
5892 const struct got_error *err = NULL;
5893 struct tog_ref_view_state *s = &view->state.ref;
5895 s->selected_entry = 0;
5898 TAILQ_INIT(&s->refs);
5899 STAILQ_INIT(&s->colors);
5901 err = ref_view_load_refs(s);
5905 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5906 err = add_color(&s->colors, "^refs/heads/",
5907 TOG_COLOR_REFS_HEADS,
5908 get_color_value("TOG_COLOR_REFS_HEADS"));
5912 err = add_color(&s->colors, "^refs/tags/",
5913 TOG_COLOR_REFS_TAGS,
5914 get_color_value("TOG_COLOR_REFS_TAGS"));
5918 err = add_color(&s->colors, "^refs/remotes/",
5919 TOG_COLOR_REFS_REMOTES,
5920 get_color_value("TOG_COLOR_REFS_REMOTES"));
5924 err = add_color(&s->colors, "^refs/got/backup/",
5925 TOG_COLOR_REFS_BACKUP,
5926 get_color_value("TOG_COLOR_REFS_BACKUP"));
5931 view->show = show_ref_view;
5932 view->input = input_ref_view;
5933 view->close = close_ref_view;
5934 view->search_start = search_start_ref_view;
5935 view->search_next = search_next_ref_view;
5938 free_colors(&s->colors);
5942 static const struct got_error *
5943 close_ref_view(struct tog_view *view)
5945 struct tog_ref_view_state *s = &view->state.ref;
5947 ref_view_free_refs(s);
5948 free_colors(&s->colors);
5953 static const struct got_error *
5954 resolve_reflist_entry(struct got_object_id **commit_id,
5955 struct tog_reflist_entry *re, struct got_repository *repo)
5957 const struct got_error *err = NULL;
5958 struct got_object_id *obj_id;
5959 struct got_tag_object *tag = NULL;
5964 err = got_ref_resolve(&obj_id, repo, re->ref);
5968 err = got_object_get_type(&obj_type, repo, obj_id);
5973 case GOT_OBJ_TYPE_COMMIT:
5974 *commit_id = obj_id;
5976 case GOT_OBJ_TYPE_TAG:
5977 err = got_object_open_as_tag(&tag, repo, obj_id);
5981 err = got_object_get_type(&obj_type, repo,
5982 got_object_tag_get_object_id(tag));
5985 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5986 err = got_error(GOT_ERR_OBJ_TYPE);
5989 *commit_id = got_object_id_dup(
5990 got_object_tag_get_object_id(tag));
5991 if (*commit_id == NULL) {
5992 err = got_error_from_errno("got_object_id_dup");
5997 err = got_error(GOT_ERR_OBJ_TYPE);
6003 got_object_tag_close(tag);
6011 static const struct got_error *
6012 log_ref_entry(struct tog_view **new_view, int begin_x,
6013 struct tog_reflist_entry *re, struct got_repository *repo)
6015 struct tog_view *log_view;
6016 const struct got_error *err = NULL;
6017 struct got_object_id *commit_id = NULL;
6021 err = resolve_reflist_entry(&commit_id, re, repo);
6023 if (err->code != GOT_ERR_OBJ_TYPE)
6029 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6030 if (log_view == NULL) {
6031 err = got_error_from_errno("view_open");
6035 err = open_log_view(log_view, commit_id, repo,
6036 got_ref_get_name(re->ref), "", 0);
6039 view_close(log_view);
6041 *new_view = log_view;
6047 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6049 struct tog_reflist_entry *re;
6052 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6055 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6056 while (i++ < maxscroll) {
6059 s->first_displayed_entry = re;
6060 re = TAILQ_PREV(re, tog_reflist_head, entry);
6065 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6067 struct tog_reflist_entry *next, *last;
6070 if (s->first_displayed_entry)
6071 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6073 next = TAILQ_FIRST(&s->refs);
6075 last = s->last_displayed_entry;
6076 while (next && last && n++ < maxscroll) {
6077 last = TAILQ_NEXT(last, entry);
6079 s->first_displayed_entry = next;
6080 next = TAILQ_NEXT(next, entry);
6085 static const struct got_error *
6086 search_start_ref_view(struct tog_view *view)
6088 struct tog_ref_view_state *s = &view->state.ref;
6090 s->matched_entry = NULL;
6095 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6097 regmatch_t regmatch;
6099 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6103 static const struct got_error *
6104 search_next_ref_view(struct tog_view *view)
6106 struct tog_ref_view_state *s = &view->state.ref;
6107 struct tog_reflist_entry *re = NULL;
6109 if (!view->searching) {
6110 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6114 if (s->matched_entry) {
6115 if (view->searching == TOG_SEARCH_FORWARD) {
6116 if (s->selected_entry)
6117 re = TAILQ_NEXT(s->selected_entry, entry);
6119 re = TAILQ_PREV(s->selected_entry,
6120 tog_reflist_head, entry);
6122 if (s->selected_entry == NULL)
6123 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6125 re = TAILQ_PREV(s->selected_entry,
6126 tog_reflist_head, entry);
6129 if (s->selected_entry)
6130 re = s->selected_entry;
6131 else if (view->searching == TOG_SEARCH_FORWARD)
6132 re = TAILQ_FIRST(&s->refs);
6134 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6139 if (s->matched_entry == NULL) {
6140 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6143 if (view->searching == TOG_SEARCH_FORWARD)
6144 re = TAILQ_FIRST(&s->refs);
6146 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6149 if (match_reflist_entry(re, &view->regex)) {
6150 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6151 s->matched_entry = re;
6155 if (view->searching == TOG_SEARCH_FORWARD)
6156 re = TAILQ_NEXT(re, entry);
6158 re = TAILQ_PREV(re, tog_reflist_head, entry);
6161 if (s->matched_entry) {
6162 s->first_displayed_entry = s->matched_entry;
6169 static const struct got_error *
6170 show_ref_view(struct tog_view *view)
6172 const struct got_error *err = NULL;
6173 struct tog_ref_view_state *s = &view->state.ref;
6174 struct tog_reflist_entry *re;
6177 struct tog_color *tc;
6179 int limit = view->nlines;
6181 werase(view->window);
6188 re = s->first_displayed_entry;
6190 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6192 return got_error_from_errno("asprintf");
6194 err = format_line(&wline, &width, line, view->ncols, 0);
6199 if (view_needs_focus_indication(view))
6200 wstandout(view->window);
6201 waddwstr(view->window, wline);
6202 if (view_needs_focus_indication(view))
6203 wstandend(view->window);
6208 if (width < view->ncols - 1)
6209 waddch(view->window, '\n');
6214 while (re && limit > 0) {
6217 if (got_ref_is_symbolic(re->ref)) {
6218 if (asprintf(&line, "%s -> %s",
6219 got_ref_get_name(re->ref),
6220 got_ref_get_symref_target(re->ref)) == -1)
6221 return got_error_from_errno("asprintf");
6222 } else if (s->show_ids) {
6223 struct got_object_id *id;
6225 err = got_ref_resolve(&id, s->repo, re->ref);
6228 err = got_object_id_str(&id_str, id);
6233 if (asprintf(&line, "%s: %s",
6234 got_ref_get_name(re->ref), id_str) == -1) {
6235 err = got_error_from_errno("asprintf");
6243 line = strdup(got_ref_get_name(re->ref));
6245 return got_error_from_errno("strdup");
6248 err = format_line(&wline, &width, line, view->ncols, 0);
6253 if (n == s->selected) {
6255 wstandout(view->window);
6256 s->selected_entry = re;
6258 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6260 wattr_on(view->window,
6261 COLOR_PAIR(tc->colorpair), NULL);
6262 waddwstr(view->window, wline);
6264 wattr_off(view->window,
6265 COLOR_PAIR(tc->colorpair), NULL);
6266 if (width < view->ncols - 1)
6267 waddch(view->window, '\n');
6268 if (n == s->selected && view->focussed)
6269 wstandend(view->window);
6275 s->last_displayed_entry = re;
6278 re = TAILQ_NEXT(re, entry);
6285 static const struct got_error *
6286 browse_ref_tree(struct tog_view **new_view, int begin_x,
6287 struct tog_reflist_entry *re, struct got_repository *repo)
6289 const struct got_error *err = NULL;
6290 struct got_object_id *commit_id = NULL;
6291 struct tog_view *tree_view;
6295 err = resolve_reflist_entry(&commit_id, re, repo);
6297 if (err->code != GOT_ERR_OBJ_TYPE)
6304 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6305 if (tree_view == NULL) {
6306 err = got_error_from_errno("view_open");
6310 err = open_tree_view(tree_view, commit_id,
6311 got_ref_get_name(re->ref), repo);
6315 *new_view = tree_view;
6320 static const struct got_error *
6321 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6323 const struct got_error *err = NULL;
6324 struct tog_ref_view_state *s = &view->state.ref;
6325 struct tog_view *log_view, *tree_view;
6326 struct tog_reflist_entry *re;
6331 s->show_ids = !s->show_ids;
6334 s->sort_by_date = !s->sort_by_date;
6335 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6336 got_ref_cmp_by_commit_timestamp_descending :
6337 tog_ref_cmp_by_name, s->repo);
6340 got_reflist_object_id_map_free(tog_refs_idmap);
6341 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6342 &tog_refs, s->repo);
6345 ref_view_free_refs(s);
6346 err = ref_view_load_refs(s);
6350 if (!s->selected_entry)
6352 if (view_is_parent_view(view))
6353 begin_x = view_split_begin_x(view->begin_x);
6354 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6357 log_view->focussed = 1;
6358 if (view_is_parent_view(view)) {
6359 err = view_close_child(view);
6362 view_set_child(view, log_view);
6363 view->focus_child = 1;
6365 *new_view = log_view;
6368 if (!s->selected_entry)
6370 if (view_is_parent_view(view))
6371 begin_x = view_split_begin_x(view->begin_x);
6372 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6374 if (err || tree_view == NULL)
6377 tree_view->focussed = 1;
6378 if (view_is_parent_view(view)) {
6379 err = view_close_child(view);
6382 view_set_child(view, tree_view);
6383 view->focus_child = 1;
6385 *new_view = tree_view;
6390 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6395 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6396 for (n = 0; n < view->nlines - 1; n++) {
6399 s->first_displayed_entry = re;
6400 re = TAILQ_PREV(re, tog_reflist_head, entry);
6403 s->selected = n - 1;
6408 if (s->selected > 0) {
6412 ref_scroll_up(s, 1);
6416 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6418 ref_scroll_up(s, MAX(0, view->nlines - 1));
6423 if (s->selected < s->ndisplayed - 1) {
6427 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6428 /* can't scroll any further */
6430 ref_scroll_down(s, 1);
6434 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6435 /* can't scroll any further; move cursor down */
6436 if (s->selected < s->ndisplayed - 1)
6437 s->selected = s->ndisplayed - 1;
6440 ref_scroll_down(s, view->nlines - 1);
6444 err = tog_load_refs(s->repo, s->sort_by_date);
6447 ref_view_free_refs(s);
6448 err = ref_view_load_refs(s);
6451 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6452 s->selected = view->nlines - 2;
6465 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6470 static const struct got_error *
6471 cmd_ref(int argc, char *argv[])
6473 const struct got_error *error;
6474 struct got_repository *repo = NULL;
6475 struct got_worktree *worktree = NULL;
6476 char *cwd = NULL, *repo_path = NULL;
6478 struct tog_view *view;
6480 while ((ch = getopt(argc, argv, "r:")) != -1) {
6483 repo_path = realpath(optarg, NULL);
6484 if (repo_path == NULL)
6485 return got_error_from_errno2("realpath",
6500 if (repo_path == NULL) {
6501 cwd = getcwd(NULL, 0);
6503 return got_error_from_errno("getcwd");
6504 error = got_worktree_open(&worktree, cwd);
6505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6509 strdup(got_worktree_get_repo_path(worktree));
6511 repo_path = strdup(cwd);
6512 if (repo_path == NULL) {
6513 error = got_error_from_errno("strdup");
6518 error = got_repo_open(&repo, repo_path, NULL);
6524 error = apply_unveil(got_repo_get_path(repo), NULL);
6528 error = tog_load_refs(repo, 0);
6532 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6534 error = got_error_from_errno("view_open");
6538 error = open_ref_view(view, repo);
6543 /* Release work tree lock. */
6544 got_worktree_close(worktree);
6547 error = view_loop(view);
6552 const struct got_error *close_err = got_repo_close(repo);
6561 list_commands(FILE *fp)
6565 fprintf(fp, "commands:");
6566 for (i = 0; i < nitems(tog_commands); i++) {
6567 const struct tog_cmd *cmd = &tog_commands[i];
6568 fprintf(fp, " %s", cmd->name);
6574 usage(int hflag, int status)
6576 FILE *fp = (status == 0) ? stdout : stderr;
6578 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6581 fprintf(fp, "lazy usage: %s path\n", getprogname());
6588 make_argv(int argc, ...)
6596 argv = calloc(argc, sizeof(char *));
6599 for (i = 0; i < argc; i++) {
6600 argv[i] = strdup(va_arg(ap, char *));
6601 if (argv[i] == NULL)
6610 * Try to convert 'tog path' into a 'tog log path' command.
6611 * The user could simply have mistyped the command rather than knowingly
6612 * provided a path. So check whether argv[0] can in fact be resolved
6613 * to a path in the HEAD commit and print a special error if not.
6614 * This hack is for mpi@ <3
6616 static const struct got_error *
6617 tog_log_with_path(int argc, char *argv[])
6619 const struct got_error *error = NULL, *close_err;
6620 const struct tog_cmd *cmd = NULL;
6621 struct got_repository *repo = NULL;
6622 struct got_worktree *worktree = NULL;
6623 struct got_object_id *commit_id = NULL, *id = NULL;
6624 struct got_commit_object *commit = NULL;
6625 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6626 char *commit_id_str = NULL, **cmd_argv = NULL;
6628 cwd = getcwd(NULL, 0);
6630 return got_error_from_errno("getcwd");
6632 error = got_worktree_open(&worktree, cwd);
6633 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6637 repo_path = strdup(got_worktree_get_repo_path(worktree));
6639 repo_path = strdup(cwd);
6640 if (repo_path == NULL) {
6641 error = got_error_from_errno("strdup");
6645 error = got_repo_open(&repo, repo_path, NULL);
6649 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6654 error = tog_load_refs(repo, 0);
6657 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6658 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6659 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6664 got_worktree_close(worktree);
6668 error = got_object_open_as_commit(&commit, repo, commit_id);
6672 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6674 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6676 fprintf(stderr, "%s: '%s' is no known command or path\n",
6677 getprogname(), argv[0]);
6682 close_err = got_repo_close(repo);
6687 error = got_object_id_str(&commit_id_str, commit_id);
6691 cmd = &tog_commands[0]; /* log */
6693 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6694 error = cmd->cmd_main(argc, cmd_argv);
6697 close_err = got_repo_close(repo);
6702 got_object_commit_close(commit);
6704 got_worktree_close(worktree);
6706 free(commit_id_str);
6713 for (i = 0; i < argc; i++)
6722 main(int argc, char *argv[])
6724 const struct got_error *error = NULL;
6725 const struct tog_cmd *cmd = NULL;
6726 int ch, hflag = 0, Vflag = 0;
6727 char **cmd_argv = NULL;
6728 static const struct option longopts[] = {
6729 { "version", no_argument, NULL, 'V' },
6733 setlocale(LC_CTYPE, "");
6735 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6755 got_version_print_str();
6760 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6768 /* Build an argument vector which runs a default command. */
6769 cmd = &tog_commands[0];
6771 cmd_argv = make_argv(argc, cmd->name);
6775 /* Did the user specify a command? */
6776 for (i = 0; i < nitems(tog_commands); i++) {
6777 if (strncmp(tog_commands[i].name, argv[0],
6778 strlen(argv[0])) == 0) {
6779 cmd = &tog_commands[i];
6788 /* No command specified; try log with a path */
6789 error = tog_log_with_path(argc, argv);
6794 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6801 for (i = 0; i < argc; i++)
6806 if (error && error->code != GOT_ERR_CANCELLED)
6807 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);