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.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_opentemp.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
73 #define CTRL(x) ((x) & 0x1f)
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
126 struct commit_queue_head head;
130 STAILQ_ENTRY(tog_color) entry;
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 struct got_reference* re2)
143 const char *name1 = got_ref_get_name(re1);
144 const char *name2 = got_ref_get_name(re2);
145 int isbackup1, isbackup2;
147 /* Sort backup refs towards the bottom of the list. */
148 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 if (!isbackup1 && isbackup2) {
153 } else if (isbackup1 && !isbackup2) {
158 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
162 static const struct got_error *
163 tog_load_refs(struct got_repository *repo, int sort_by_date)
165 const struct got_error *err;
167 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
173 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
180 if (tog_refs_idmap) {
181 got_reflist_object_id_map_free(tog_refs_idmap);
182 tog_refs_idmap = NULL;
184 got_ref_list_free(&tog_refs);
187 static const struct got_error *
188 add_color(struct tog_colors *colors, const char *pattern,
189 int idx, short color)
191 const struct got_error *err = NULL;
192 struct tog_color *tc;
195 if (idx < 1 || idx > COLOR_PAIRS - 1)
198 init_pair(idx, color, -1);
200 tc = calloc(1, sizeof(*tc));
202 return got_error_from_errno("calloc");
203 regerr = regcomp(&tc->regex, pattern,
204 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
206 static char regerr_msg[512];
207 static char err_msg[512];
208 regerror(regerr, &tc->regex, regerr_msg,
210 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
212 err = got_error_msg(GOT_ERR_REGEX, err_msg);
217 STAILQ_INSERT_HEAD(colors, tc, entry);
222 free_colors(struct tog_colors *colors)
224 struct tog_color *tc;
226 while (!STAILQ_EMPTY(colors)) {
227 tc = STAILQ_FIRST(colors);
228 STAILQ_REMOVE_HEAD(colors, entry);
235 get_color(struct tog_colors *colors, int colorpair)
237 struct tog_color *tc = NULL;
239 STAILQ_FOREACH(tc, colors, entry) {
240 if (tc->colorpair == colorpair)
248 default_color_value(const char *envvar)
250 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
254 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
258 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 return COLOR_MAGENTA;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
264 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
266 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
268 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
270 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
272 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
278 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
285 get_color_value(const char *envvar)
287 const char *val = getenv(envvar);
290 return default_color_value(envvar);
292 if (strcasecmp(val, "black") == 0)
294 if (strcasecmp(val, "red") == 0)
296 if (strcasecmp(val, "green") == 0)
298 if (strcasecmp(val, "yellow") == 0)
300 if (strcasecmp(val, "blue") == 0)
302 if (strcasecmp(val, "magenta") == 0)
303 return COLOR_MAGENTA;
304 if (strcasecmp(val, "cyan") == 0)
306 if (strcasecmp(val, "white") == 0)
308 if (strcasecmp(val, "default") == 0)
311 return default_color_value(envvar);
315 struct tog_diff_view_state {
316 struct got_object_id *id1, *id2;
317 const char *label1, *label2;
319 int first_displayed_line;
320 int last_displayed_line;
323 int ignore_whitespace;
325 struct got_repository *repo;
326 struct tog_colors colors;
332 /* passed from log view; may be NULL */
333 struct tog_view *log_view;
336 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 struct tog_log_thread_args {
339 pthread_cond_t need_commits;
340 pthread_cond_t commit_loaded;
343 struct got_commit_graph *graph;
344 struct commit_queue *commits;
345 const char *in_repo_path;
346 struct got_object_id *start_id;
347 struct got_repository *repo;
350 struct commit_queue_entry **first_displayed_entry;
351 struct commit_queue_entry **selected_entry;
353 int *search_next_done;
357 struct tog_log_view_state {
358 struct commit_queue commits;
359 struct commit_queue_entry *first_displayed_entry;
360 struct commit_queue_entry *last_displayed_entry;
361 struct commit_queue_entry *selected_entry;
366 struct got_repository *repo;
367 struct got_object_id *start_id;
370 struct tog_log_thread_args thread_args;
371 struct commit_queue_entry *matched_entry;
372 struct commit_queue_entry *search_entry;
373 struct tog_colors colors;
376 #define TOG_COLOR_DIFF_MINUS 1
377 #define TOG_COLOR_DIFF_PLUS 2
378 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
379 #define TOG_COLOR_DIFF_META 4
380 #define TOG_COLOR_TREE_SUBMODULE 5
381 #define TOG_COLOR_TREE_SYMLINK 6
382 #define TOG_COLOR_TREE_DIRECTORY 7
383 #define TOG_COLOR_TREE_EXECUTABLE 8
384 #define TOG_COLOR_COMMIT 9
385 #define TOG_COLOR_AUTHOR 10
386 #define TOG_COLOR_DATE 11
387 #define TOG_COLOR_REFS_HEADS 12
388 #define TOG_COLOR_REFS_TAGS 13
389 #define TOG_COLOR_REFS_REMOTES 14
390 #define TOG_COLOR_REFS_BACKUP 15
392 struct tog_blame_cb_args {
393 struct tog_blame_line *lines; /* one per line */
396 struct tog_view *view;
397 struct got_object_id *commit_id;
401 struct tog_blame_thread_args {
403 struct got_repository *repo;
404 struct tog_blame_cb_args *cb_args;
406 got_cancel_cb cancel_cb;
413 struct tog_blame_line *lines;
417 struct tog_blame_thread_args thread_args;
418 struct tog_blame_cb_args cb_args;
422 struct tog_blame_view_state {
423 int first_displayed_line;
424 int last_displayed_line;
429 struct got_object_id_queue blamed_commits;
430 struct got_object_qid *blamed_commit;
432 struct got_repository *repo;
433 struct got_object_id *commit_id;
434 struct tog_blame blame;
436 struct tog_colors colors;
439 struct tog_parent_tree {
440 TAILQ_ENTRY(tog_parent_tree) entry;
441 struct got_tree_object *tree;
442 struct got_tree_entry *first_displayed_entry;
443 struct got_tree_entry *selected_entry;
447 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
449 struct tog_tree_view_state {
451 struct got_object_id *commit_id;/* commit which this tree belongs to */
452 struct got_tree_object *root; /* the commit's root tree entry */
453 struct got_tree_object *tree; /* currently displayed (sub-)tree */
454 struct got_tree_entry *first_displayed_entry;
455 struct got_tree_entry *last_displayed_entry;
456 struct got_tree_entry *selected_entry;
457 int ndisplayed, selected, show_ids;
458 struct tog_parent_trees parents; /* parent trees of current sub-tree */
460 struct got_repository *repo;
461 struct got_tree_entry *matched_entry;
462 struct tog_colors colors;
465 struct tog_reflist_entry {
466 TAILQ_ENTRY(tog_reflist_entry) entry;
467 struct got_reference *ref;
471 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
473 struct tog_ref_view_state {
474 struct tog_reflist_head refs;
475 struct tog_reflist_entry *first_displayed_entry;
476 struct tog_reflist_entry *last_displayed_entry;
477 struct tog_reflist_entry *selected_entry;
478 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
479 struct got_repository *repo;
480 struct tog_reflist_entry *matched_entry;
481 struct tog_colors colors;
485 * We implement two types of views: parent views and child views.
487 * The 'Tab' key switches focus between a parent view and its child view.
488 * Child views are shown side-by-side to their parent view, provided
489 * there is enough screen estate.
491 * When a new view is opened from within a parent view, this new view
492 * becomes a child view of the parent view, replacing any existing child.
494 * When a new view is opened from within a child view, this new view
495 * becomes a parent view which will obscure the views below until the
496 * user quits the new parent view by typing 'q'.
498 * This list of views contains parent views only.
499 * Child views are only pointed to by their parent view.
501 TAILQ_HEAD(tog_view_list_head, tog_view);
504 TAILQ_ENTRY(tog_view) entry;
507 int nlines, ncols, begin_y, begin_x;
508 int lines, cols; /* copies of LINES and COLS */
509 int focussed; /* Only set on one parent or child view at a time. */
511 struct tog_view *parent;
512 struct tog_view *child;
515 * This flag is initially set on parent views when a new child view
516 * is created. It gets toggled when the 'Tab' key switches focus
517 * between parent and child.
518 * The flag indicates whether focus should be passed on to our child
519 * view if this parent view gets picked for focus after another parent
520 * view was closed. This prevents child views from losing focus in such
525 /* type-specific state */
526 enum tog_view_type type;
528 struct tog_diff_view_state diff;
529 struct tog_log_view_state log;
530 struct tog_blame_view_state blame;
531 struct tog_tree_view_state tree;
532 struct tog_ref_view_state ref;
535 const struct got_error *(*show)(struct tog_view *);
536 const struct got_error *(*input)(struct tog_view **,
537 struct tog_view *, int);
538 const struct got_error *(*close)(struct tog_view *);
540 const struct got_error *(*search_start)(struct tog_view *);
541 const struct got_error *(*search_next)(struct tog_view *);
544 #define TOG_SEARCH_FORWARD 1
545 #define TOG_SEARCH_BACKWARD 2
546 int search_next_done;
547 #define TOG_SEARCH_HAVE_MORE 1
548 #define TOG_SEARCH_NO_MORE 2
549 #define TOG_SEARCH_HAVE_NONE 3
554 static const struct got_error *open_diff_view(struct tog_view *,
555 struct got_object_id *, struct got_object_id *,
556 const char *, const char *, int, int, int, struct tog_view *,
557 struct got_repository *);
558 static const struct got_error *show_diff_view(struct tog_view *);
559 static const struct got_error *input_diff_view(struct tog_view **,
560 struct tog_view *, int);
561 static const struct got_error* close_diff_view(struct tog_view *);
562 static const struct got_error *search_start_diff_view(struct tog_view *);
563 static const struct got_error *search_next_diff_view(struct tog_view *);
565 static const struct got_error *open_log_view(struct tog_view *,
566 struct got_object_id *, struct got_repository *,
567 const char *, const char *, int);
568 static const struct got_error * show_log_view(struct tog_view *);
569 static const struct got_error *input_log_view(struct tog_view **,
570 struct tog_view *, int);
571 static const struct got_error *close_log_view(struct tog_view *);
572 static const struct got_error *search_start_log_view(struct tog_view *);
573 static const struct got_error *search_next_log_view(struct tog_view *);
575 static const struct got_error *open_blame_view(struct tog_view *, char *,
576 struct got_object_id *, struct got_repository *);
577 static const struct got_error *show_blame_view(struct tog_view *);
578 static const struct got_error *input_blame_view(struct tog_view **,
579 struct tog_view *, int);
580 static const struct got_error *close_blame_view(struct tog_view *);
581 static const struct got_error *search_start_blame_view(struct tog_view *);
582 static const struct got_error *search_next_blame_view(struct tog_view *);
584 static const struct got_error *open_tree_view(struct tog_view *,
585 struct got_object_id *, const char *, struct got_repository *);
586 static const struct got_error *show_tree_view(struct tog_view *);
587 static const struct got_error *input_tree_view(struct tog_view **,
588 struct tog_view *, int);
589 static const struct got_error *close_tree_view(struct tog_view *);
590 static const struct got_error *search_start_tree_view(struct tog_view *);
591 static const struct got_error *search_next_tree_view(struct tog_view *);
593 static const struct got_error *open_ref_view(struct tog_view *,
594 struct got_repository *);
595 static const struct got_error *show_ref_view(struct tog_view *);
596 static const struct got_error *input_ref_view(struct tog_view **,
597 struct tog_view *, int);
598 static const struct got_error *close_ref_view(struct tog_view *);
599 static const struct got_error *search_start_ref_view(struct tog_view *);
600 static const struct got_error *search_next_ref_view(struct tog_view *);
602 static volatile sig_atomic_t tog_sigwinch_received;
603 static volatile sig_atomic_t tog_sigpipe_received;
604 static volatile sig_atomic_t tog_sigcont_received;
605 static volatile sig_atomic_t tog_sigint_received;
606 static volatile sig_atomic_t tog_sigterm_received;
609 tog_sigwinch(int signo)
611 tog_sigwinch_received = 1;
615 tog_sigpipe(int signo)
617 tog_sigpipe_received = 1;
621 tog_sigcont(int signo)
623 tog_sigcont_received = 1;
627 tog_sigint(int signo)
629 tog_sigint_received = 1;
633 tog_sigterm(int signo)
635 tog_sigterm_received = 1;
639 tog_fatal_signal_received()
641 return (tog_sigpipe_received ||
642 tog_sigint_received || tog_sigint_received);
646 static const struct got_error *
647 view_close(struct tog_view *view)
649 const struct got_error *err = NULL;
652 view_close(view->child);
656 err = view->close(view);
658 del_panel(view->panel);
660 delwin(view->window);
665 static struct tog_view *
666 view_open(int nlines, int ncols, int begin_y, int begin_x,
667 enum tog_view_type type)
669 struct tog_view *view = calloc(1, sizeof(*view));
677 view->nlines = nlines ? nlines : LINES - begin_y;
678 view->ncols = ncols ? ncols : COLS - begin_x;
679 view->begin_y = begin_y;
680 view->begin_x = begin_x;
681 view->window = newwin(nlines, ncols, begin_y, begin_x);
682 if (view->window == NULL) {
686 view->panel = new_panel(view->window);
687 if (view->panel == NULL ||
688 set_panel_userptr(view->panel, view) != OK) {
693 keypad(view->window, TRUE);
698 view_split_begin_x(int begin_x)
700 if (begin_x > 0 || COLS < 120)
702 return (COLS - MAX(COLS / 2, 80));
705 static const struct got_error *view_resize(struct tog_view *);
707 static const struct got_error *
708 view_splitscreen(struct tog_view *view)
710 const struct got_error *err = NULL;
713 view->begin_x = view_split_begin_x(0);
714 view->nlines = LINES;
715 view->ncols = COLS - view->begin_x;
718 err = view_resize(view);
722 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
723 return got_error_from_errno("mvwin");
728 static const struct got_error *
729 view_fullscreen(struct tog_view *view)
731 const struct got_error *err = NULL;
735 view->nlines = LINES;
739 err = view_resize(view);
743 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
744 return got_error_from_errno("mvwin");
750 view_is_parent_view(struct tog_view *view)
752 return view->parent == NULL;
755 static const struct got_error *
756 view_resize(struct tog_view *view)
760 if (view->lines > LINES)
761 nlines = view->nlines - (view->lines - LINES);
763 nlines = view->nlines + (LINES - view->lines);
765 if (view->cols > COLS)
766 ncols = view->ncols - (view->cols - COLS);
768 ncols = view->ncols + (COLS - view->cols);
770 if (wresize(view->window, nlines, ncols) == ERR)
771 return got_error_from_errno("wresize");
772 if (replace_panel(view->panel, view->window) == ERR)
773 return got_error_from_errno("replace_panel");
774 wclear(view->window);
776 view->nlines = nlines;
782 view->child->begin_x = view_split_begin_x(view->begin_x);
783 if (view->child->begin_x == 0) {
784 view_fullscreen(view->child);
785 if (view->child->focussed)
786 show_panel(view->child->panel);
788 show_panel(view->panel);
790 view_splitscreen(view->child);
791 show_panel(view->child->panel);
798 static const struct got_error *
799 view_close_child(struct tog_view *view)
801 const struct got_error *err = NULL;
803 if (view->child == NULL)
806 err = view_close(view->child);
812 view_set_child(struct tog_view *view, struct tog_view *child)
815 child->parent = view;
819 view_is_splitscreen(struct tog_view *view)
821 return view->begin_x > 0;
830 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
831 cols = 80; /* Default */
837 resize_term(lines, cols);
840 static const struct got_error *
841 view_search_start(struct tog_view *view)
843 const struct got_error *err = NULL;
847 if (view->search_started) {
848 regfree(&view->regex);
850 memset(&view->regmatch, 0, sizeof(view->regmatch));
852 view->search_started = 0;
854 if (view->nlines < 1)
857 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
858 wclrtoeol(view->window);
862 ret = wgetnstr(view->window, pattern, sizeof(pattern));
868 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
869 err = view->search_start(view);
871 regfree(&view->regex);
874 view->search_started = 1;
875 view->searching = TOG_SEARCH_FORWARD;
876 view->search_next_done = 0;
877 view->search_next(view);
883 static const struct got_error *
884 view_input(struct tog_view **new, int *done, struct tog_view *view,
885 struct tog_view_list_head *views)
887 const struct got_error *err = NULL;
893 /* Clear "no matches" indicator. */
894 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
895 view->search_next_done == TOG_SEARCH_HAVE_NONE)
896 view->search_next_done = TOG_SEARCH_HAVE_MORE;
898 if (view->searching && !view->search_next_done) {
899 errcode = pthread_mutex_unlock(&tog_mutex);
901 return got_error_set_errno(errcode,
902 "pthread_mutex_unlock");
904 errcode = pthread_mutex_lock(&tog_mutex);
906 return got_error_set_errno(errcode,
907 "pthread_mutex_lock");
908 view->search_next(view);
912 nodelay(stdscr, FALSE);
913 /* Allow threads to make progress while we are waiting for input. */
914 errcode = pthread_mutex_unlock(&tog_mutex);
916 return got_error_set_errno(errcode, "pthread_mutex_unlock");
917 ch = wgetch(view->window);
918 errcode = pthread_mutex_lock(&tog_mutex);
920 return got_error_set_errno(errcode, "pthread_mutex_lock");
921 nodelay(stdscr, TRUE);
923 if (tog_sigwinch_received || tog_sigcont_received) {
925 tog_sigwinch_received = 0;
926 tog_sigcont_received = 0;
927 TAILQ_FOREACH(v, views, entry) {
928 err = view_resize(v);
931 err = v->input(new, v, KEY_RESIZE);
935 err = view_resize(v->child);
938 err = v->child->input(new, v->child,
950 view->child->focussed = 1;
951 view->focus_child = 1;
952 } else if (view->parent) {
954 view->parent->focussed = 1;
955 view->parent->focus_child = 0;
959 err = view->input(new, view, ch);
966 if (view_is_parent_view(view)) {
967 if (view->child == NULL)
969 if (view_is_splitscreen(view->child)) {
971 view->child->focussed = 1;
972 err = view_fullscreen(view->child);
974 err = view_splitscreen(view->child);
977 err = view->child->input(new, view->child,
980 if (view_is_splitscreen(view)) {
981 view->parent->focussed = 0;
983 err = view_fullscreen(view);
985 err = view_splitscreen(view);
989 err = view->input(new, view, KEY_RESIZE);
995 if (view->search_start)
996 view_search_start(view);
998 err = view->input(new, view, ch);
1002 if (view->search_started && view->search_next) {
1003 view->searching = (ch == 'n' ?
1004 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1005 view->search_next_done = 0;
1006 view->search_next(view);
1008 err = view->input(new, view, ch);
1011 err = view->input(new, view, ch);
1019 view_vborder(struct tog_view *view)
1022 const struct tog_view *view_above;
1025 return view_vborder(view->parent);
1027 panel = panel_above(view->panel);
1031 view_above = panel_userptr(panel);
1032 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1033 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1037 view_needs_focus_indication(struct tog_view *view)
1039 if (view_is_parent_view(view)) {
1040 if (view->child == NULL || view->child->focussed)
1042 if (!view_is_splitscreen(view->child))
1044 } else if (!view_is_splitscreen(view))
1047 return view->focussed;
1050 static const struct got_error *
1051 view_loop(struct tog_view *view)
1053 const struct got_error *err = NULL;
1054 struct tog_view_list_head views;
1055 struct tog_view *new_view;
1056 int fast_refresh = 10;
1057 int done = 0, errcode;
1059 errcode = pthread_mutex_lock(&tog_mutex);
1061 return got_error_set_errno(errcode, "pthread_mutex_lock");
1064 TAILQ_INSERT_HEAD(&views, view, entry);
1067 err = view->show(view);
1072 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1073 /* Refresh fast during initialization, then become slower. */
1074 if (fast_refresh && fast_refresh-- == 0)
1075 halfdelay(10); /* switch to once per second */
1077 err = view_input(&new_view, &done, view, &views);
1081 struct tog_view *v, *prev = NULL;
1083 if (view_is_parent_view(view))
1084 prev = TAILQ_PREV(view, tog_view_list_head,
1086 else if (view->parent)
1087 prev = view->parent;
1090 view->parent->child = NULL;
1091 view->parent->focus_child = 0;
1093 TAILQ_REMOVE(&views, view, entry);
1095 err = view_close(view);
1100 TAILQ_FOREACH(v, &views, entry) {
1104 if (view == NULL && new_view == NULL) {
1105 /* No view has focus. Try to pick one. */
1108 else if (!TAILQ_EMPTY(&views)) {
1109 view = TAILQ_LAST(&views,
1110 tog_view_list_head);
1113 if (view->focus_child) {
1114 view->child->focussed = 1;
1122 struct tog_view *v, *t;
1123 /* Only allow one parent view per type. */
1124 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1125 if (v->type != new_view->type)
1127 TAILQ_REMOVE(&views, v, entry);
1128 err = view_close(v);
1133 TAILQ_INSERT_TAIL(&views, new_view, entry);
1137 if (view_is_parent_view(view)) {
1138 if (view->child && view->child->focussed)
1141 if (view->parent && view->parent->focussed)
1142 view = view->parent;
1144 show_panel(view->panel);
1145 if (view->child && view_is_splitscreen(view->child))
1146 show_panel(view->child->panel);
1147 if (view->parent && view_is_splitscreen(view)) {
1148 err = view->parent->show(view->parent);
1152 err = view->show(view);
1156 err = view->child->show(view->child);
1165 while (!TAILQ_EMPTY(&views)) {
1166 view = TAILQ_FIRST(&views);
1167 TAILQ_REMOVE(&views, view, entry);
1171 errcode = pthread_mutex_unlock(&tog_mutex);
1172 if (errcode && err == NULL)
1173 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1183 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1188 /* Create newly allocated wide-character string equivalent to a byte string. */
1189 static const struct got_error *
1190 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1193 const struct got_error *err = NULL;
1196 *wlen = mbstowcs(NULL, s, 0);
1197 if (*wlen == (size_t)-1) {
1199 if (errno != EILSEQ)
1200 return got_error_from_errno("mbstowcs");
1202 /* byte string invalid in current encoding; try to "fix" it */
1203 err = got_mbsavis(&vis, &vislen, s);
1206 *wlen = mbstowcs(NULL, vis, 0);
1207 if (*wlen == (size_t)-1) {
1208 err = got_error_from_errno("mbstowcs"); /* give up */
1213 *ws = calloc(*wlen + 1, sizeof(**ws));
1215 err = got_error_from_errno("calloc");
1219 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1220 err = got_error_from_errno("mbstowcs");
1231 /* Format a line for display, ensuring that it won't overflow a width limit. */
1232 static const struct got_error *
1233 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1236 const struct got_error *err = NULL;
1238 wchar_t *wline = NULL;
1245 err = mbs2ws(&wline, &wlen, line);
1249 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1250 wline[wlen - 1] = L'\0';
1253 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1254 wline[wlen - 1] = L'\0';
1260 int width = wcwidth(wline[i]);
1267 if (width == 1 || width == 2) {
1268 if (cols + width > wlimit)
1272 } else if (width == -1) {
1273 if (wline[i] == L'\t') {
1275 ((cols + col_tab_align) % TABSIZE);
1280 if (cols + width > wlimit)
1285 err = got_error_from_errno("wcwidth");
1300 static const struct got_error*
1301 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1302 struct got_object_id *id, struct got_repository *repo)
1304 static const struct got_error *err = NULL;
1305 struct got_reflist_entry *re;
1311 TAILQ_FOREACH(re, refs, entry) {
1312 struct got_tag_object *tag = NULL;
1313 struct got_object_id *ref_id;
1316 name = got_ref_get_name(re->ref);
1317 if (strcmp(name, GOT_REF_HEAD) == 0)
1319 if (strncmp(name, "refs/", 5) == 0)
1321 if (strncmp(name, "got/", 4) == 0 &&
1322 strncmp(name, "got/backup/", 11) != 0)
1324 if (strncmp(name, "heads/", 6) == 0)
1326 if (strncmp(name, "remotes/", 8) == 0) {
1328 s = strstr(name, "/" GOT_REF_HEAD);
1329 if (s != NULL && s[strlen(s)] == '\0')
1332 err = got_ref_resolve(&ref_id, repo, re->ref);
1335 if (strncmp(name, "tags/", 5) == 0) {
1336 err = got_object_open_as_tag(&tag, repo, ref_id);
1338 if (err->code != GOT_ERR_OBJ_TYPE) {
1342 /* Ref points at something other than a tag. */
1347 cmp = got_object_id_cmp(tag ?
1348 got_object_tag_get_object_id(tag) : ref_id, id);
1351 got_object_tag_close(tag);
1355 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1356 s ? ", " : "", name) == -1) {
1357 err = got_error_from_errno("asprintf");
1368 static const struct got_error *
1369 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1374 smallerthan = strchr(author, '<');
1375 if (smallerthan && smallerthan[1] != '\0')
1376 author = smallerthan + 1;
1377 author[strcspn(author, "@>")] = '\0';
1378 return format_line(wauthor, author_width, author, limit, col_tab_align);
1381 static const struct got_error *
1382 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1383 struct got_object_id *id, const size_t date_display_cols,
1384 int author_display_cols)
1386 struct tog_log_view_state *s = &view->state.log;
1387 const struct got_error *err = NULL;
1388 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1389 char *logmsg0 = NULL, *logmsg = NULL;
1390 char *author = NULL;
1391 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1392 int author_width, logmsg_width;
1393 char *newline, *line = NULL;
1395 const int avail = view->ncols;
1397 time_t committer_time;
1398 struct tog_color *tc;
1400 committer_time = got_object_commit_get_committer_time(commit);
1401 if (gmtime_r(&committer_time, &tm) == NULL)
1402 return got_error_from_errno("gmtime_r");
1403 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1404 return got_error(GOT_ERR_NO_SPACE);
1406 if (avail <= date_display_cols)
1407 limit = MIN(sizeof(datebuf) - 1, avail);
1409 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1410 tc = get_color(&s->colors, TOG_COLOR_DATE);
1412 wattr_on(view->window,
1413 COLOR_PAIR(tc->colorpair), NULL);
1414 waddnstr(view->window, datebuf, limit);
1416 wattr_off(view->window,
1417 COLOR_PAIR(tc->colorpair), NULL);
1424 err = got_object_id_str(&id_str, id);
1427 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1429 wattr_on(view->window,
1430 COLOR_PAIR(tc->colorpair), NULL);
1431 wprintw(view->window, "%.8s ", id_str);
1433 wattr_off(view->window,
1434 COLOR_PAIR(tc->colorpair), NULL);
1441 author = strdup(got_object_commit_get_author(commit));
1442 if (author == NULL) {
1443 err = got_error_from_errno("strdup");
1446 err = format_author(&wauthor, &author_width, author, avail - col, col);
1449 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1451 wattr_on(view->window,
1452 COLOR_PAIR(tc->colorpair), NULL);
1453 waddwstr(view->window, wauthor);
1455 wattr_off(view->window,
1456 COLOR_PAIR(tc->colorpair), NULL);
1457 col += author_width;
1458 while (col < avail && author_width < author_display_cols + 2) {
1459 waddch(view->window, ' ');
1466 err = got_object_commit_get_logmsg(&logmsg0, commit);
1470 while (*logmsg == '\n')
1472 newline = strchr(logmsg, '\n');
1475 limit = avail - col;
1476 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1479 waddwstr(view->window, wlogmsg);
1480 col += logmsg_width;
1481 while (col < avail) {
1482 waddch(view->window, ' ');
1494 static struct commit_queue_entry *
1495 alloc_commit_queue_entry(struct got_commit_object *commit,
1496 struct got_object_id *id)
1498 struct commit_queue_entry *entry;
1500 entry = calloc(1, sizeof(*entry));
1505 entry->commit = commit;
1510 pop_commit(struct commit_queue *commits)
1512 struct commit_queue_entry *entry;
1514 entry = TAILQ_FIRST(&commits->head);
1515 TAILQ_REMOVE(&commits->head, entry, entry);
1516 got_object_commit_close(entry->commit);
1517 commits->ncommits--;
1518 /* Don't free entry->id! It is owned by the commit graph. */
1523 free_commits(struct commit_queue *commits)
1525 while (!TAILQ_EMPTY(&commits->head))
1526 pop_commit(commits);
1529 static const struct got_error *
1530 match_commit(int *have_match, struct got_object_id *id,
1531 struct got_commit_object *commit, regex_t *regex)
1533 const struct got_error *err = NULL;
1534 regmatch_t regmatch;
1535 char *id_str = NULL, *logmsg = NULL;
1539 err = got_object_id_str(&id_str, id);
1543 err = got_object_commit_get_logmsg(&logmsg, commit);
1547 if (regexec(regex, got_object_commit_get_author(commit), 1,
1548 ®match, 0) == 0 ||
1549 regexec(regex, got_object_commit_get_committer(commit), 1,
1550 ®match, 0) == 0 ||
1551 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1552 regexec(regex, logmsg, 1, ®match, 0) == 0)
1560 static const struct got_error *
1561 queue_commits(struct tog_log_thread_args *a)
1563 const struct got_error *err = NULL;
1566 * We keep all commits open throughout the lifetime of the log
1567 * view in order to avoid having to re-fetch commits from disk
1568 * while updating the display.
1571 struct got_object_id *id;
1572 struct got_commit_object *commit;
1573 struct commit_queue_entry *entry;
1576 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1578 if (err || id == NULL)
1581 err = got_object_open_as_commit(&commit, a->repo, id);
1584 entry = alloc_commit_queue_entry(commit, id);
1585 if (entry == NULL) {
1586 err = got_error_from_errno("alloc_commit_queue_entry");
1590 errcode = pthread_mutex_lock(&tog_mutex);
1592 err = got_error_set_errno(errcode,
1593 "pthread_mutex_lock");
1597 entry->idx = a->commits->ncommits;
1598 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1599 a->commits->ncommits++;
1601 if (*a->searching == TOG_SEARCH_FORWARD &&
1602 !*a->search_next_done) {
1604 err = match_commit(&have_match, id, commit, a->regex);
1608 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1611 errcode = pthread_mutex_unlock(&tog_mutex);
1612 if (errcode && err == NULL)
1613 err = got_error_set_errno(errcode,
1614 "pthread_mutex_unlock");
1617 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1623 select_commit(struct tog_log_view_state *s)
1625 struct commit_queue_entry *entry;
1628 entry = s->first_displayed_entry;
1630 if (ncommits == s->selected) {
1631 s->selected_entry = entry;
1634 entry = TAILQ_NEXT(entry, entry);
1639 static const struct got_error *
1640 draw_commits(struct tog_view *view)
1642 const struct got_error *err = NULL;
1643 struct tog_log_view_state *s = &view->state.log;
1644 struct commit_queue_entry *entry = s->selected_entry;
1645 const int limit = view->nlines;
1647 int ncommits, author_cols = 4;
1648 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1649 char *refs_str = NULL;
1651 struct tog_color *tc;
1652 static const size_t date_display_cols = 12;
1654 if (s->selected_entry &&
1655 !(view->searching && view->search_next_done == 0)) {
1656 struct got_reflist_head *refs;
1657 err = got_object_id_str(&id_str, s->selected_entry->id);
1660 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1661 s->selected_entry->id);
1663 err = build_refs_str(&refs_str, refs,
1664 s->selected_entry->id, s->repo);
1670 if (s->thread_args.commits_needed == 0)
1671 halfdelay(10); /* disable fast refresh */
1673 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1674 if (asprintf(&ncommits_str, " [%d/%d] %s",
1675 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1676 (view->searching && !view->search_next_done) ?
1677 "searching..." : "loading...") == -1) {
1678 err = got_error_from_errno("asprintf");
1682 const char *search_str = NULL;
1684 if (view->searching) {
1685 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1686 search_str = "no more matches";
1687 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1688 search_str = "no matches found";
1689 else if (!view->search_next_done)
1690 search_str = "searching...";
1693 if (asprintf(&ncommits_str, " [%d/%d] %s",
1694 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1695 search_str ? search_str :
1696 (refs_str ? refs_str : "")) == -1) {
1697 err = got_error_from_errno("asprintf");
1702 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1703 if (asprintf(&header, "commit %s %s%s",
1704 id_str ? id_str : "........................................",
1705 s->in_repo_path, ncommits_str) == -1) {
1706 err = got_error_from_errno("asprintf");
1710 } else if (asprintf(&header, "commit %s%s",
1711 id_str ? id_str : "........................................",
1712 ncommits_str) == -1) {
1713 err = got_error_from_errno("asprintf");
1717 err = format_line(&wline, &width, header, view->ncols, 0);
1721 werase(view->window);
1723 if (view_needs_focus_indication(view))
1724 wstandout(view->window);
1725 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1727 wattr_on(view->window,
1728 COLOR_PAIR(tc->colorpair), NULL);
1729 waddwstr(view->window, wline);
1731 wattr_off(view->window,
1732 COLOR_PAIR(tc->colorpair), NULL);
1733 while (width < view->ncols) {
1734 waddch(view->window, ' ');
1737 if (view_needs_focus_indication(view))
1738 wstandend(view->window);
1743 /* Grow author column size if necessary. */
1744 entry = s->first_displayed_entry;
1750 if (ncommits >= limit - 1)
1752 author = strdup(got_object_commit_get_author(entry->commit));
1753 if (author == NULL) {
1754 err = got_error_from_errno("strdup");
1757 err = format_author(&wauthor, &width, author, COLS,
1759 if (author_cols < width)
1760 author_cols = width;
1764 entry = TAILQ_NEXT(entry, entry);
1767 entry = s->first_displayed_entry;
1768 s->last_displayed_entry = s->first_displayed_entry;
1771 if (ncommits >= limit - 1)
1773 if (ncommits == s->selected)
1774 wstandout(view->window);
1775 err = draw_commit(view, entry->commit, entry->id,
1776 date_display_cols, author_cols);
1777 if (ncommits == s->selected)
1778 wstandend(view->window);
1782 s->last_displayed_entry = entry;
1783 entry = TAILQ_NEXT(entry, entry);
1798 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1800 struct commit_queue_entry *entry;
1803 entry = TAILQ_FIRST(&s->commits.head);
1804 if (s->first_displayed_entry == entry)
1807 entry = s->first_displayed_entry;
1808 while (entry && nscrolled < maxscroll) {
1809 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1811 s->first_displayed_entry = entry;
1817 static const struct got_error *
1818 trigger_log_thread(struct tog_view *view, int wait)
1820 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1823 halfdelay(1); /* fast refresh while loading commits */
1825 while (ta->commits_needed > 0 || ta->load_all) {
1826 if (ta->log_complete)
1829 /* Wake the log thread. */
1830 errcode = pthread_cond_signal(&ta->need_commits);
1832 return got_error_set_errno(errcode,
1833 "pthread_cond_signal");
1836 * The mutex will be released while the view loop waits
1837 * in wgetch(), at which time the log thread will run.
1842 /* Display progress update in log view. */
1843 show_log_view(view);
1847 /* Wait right here while next commit is being loaded. */
1848 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1850 return got_error_set_errno(errcode,
1851 "pthread_cond_wait");
1853 /* Display progress update in log view. */
1854 show_log_view(view);
1862 static const struct got_error *
1863 log_scroll_down(struct tog_view *view, int maxscroll)
1865 struct tog_log_view_state *s = &view->state.log;
1866 const struct got_error *err = NULL;
1867 struct commit_queue_entry *pentry;
1868 int nscrolled = 0, ncommits_needed;
1870 if (s->last_displayed_entry == NULL)
1873 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1874 if (s->commits.ncommits < ncommits_needed &&
1875 !s->thread_args.log_complete) {
1877 * Ask the log thread for required amount of commits.
1879 s->thread_args.commits_needed += maxscroll;
1880 err = trigger_log_thread(view, 1);
1886 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1890 s->last_displayed_entry = pentry;
1892 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1895 s->first_displayed_entry = pentry;
1896 } while (++nscrolled < maxscroll);
1901 static const struct got_error *
1902 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1903 struct got_commit_object *commit, struct got_object_id *commit_id,
1904 struct tog_view *log_view, struct got_repository *repo)
1906 const struct got_error *err;
1907 struct got_object_qid *parent_id;
1908 struct tog_view *diff_view;
1910 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1911 if (diff_view == NULL)
1912 return got_error_from_errno("view_open");
1914 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1915 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1916 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1918 *new_view = diff_view;
1922 static const struct got_error *
1923 tree_view_visit_subtree(struct tog_tree_view_state *s,
1924 struct got_tree_object *subtree)
1926 struct tog_parent_tree *parent;
1928 parent = calloc(1, sizeof(*parent));
1930 return got_error_from_errno("calloc");
1932 parent->tree = s->tree;
1933 parent->first_displayed_entry = s->first_displayed_entry;
1934 parent->selected_entry = s->selected_entry;
1935 parent->selected = s->selected;
1936 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1939 s->first_displayed_entry = NULL;
1943 static const struct got_error *
1944 tree_view_walk_path(struct tog_tree_view_state *s,
1945 struct got_commit_object *commit, const char *path)
1947 const struct got_error *err = NULL;
1948 struct got_tree_object *tree = NULL;
1950 char *slash, *subpath = NULL;
1952 /* Walk the path and open corresponding tree objects. */
1955 struct got_tree_entry *te;
1956 struct got_object_id *tree_id;
1962 /* Ensure the correct subtree entry is selected. */
1963 slash = strchr(p, '/');
1965 te_name = strdup(p);
1967 te_name = strndup(p, slash - p);
1968 if (te_name == NULL) {
1969 err = got_error_from_errno("strndup");
1972 te = got_object_tree_find_entry(s->tree, te_name);
1974 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1979 s->first_displayed_entry = s->selected_entry = te;
1981 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1982 break; /* jump to this file's entry */
1984 slash = strchr(p, '/');
1986 subpath = strndup(path, slash - path);
1988 subpath = strdup(path);
1989 if (subpath == NULL) {
1990 err = got_error_from_errno("strdup");
1994 err = got_object_id_by_path(&tree_id, s->repo, commit,
1999 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2004 err = tree_view_visit_subtree(s, tree);
2006 got_object_tree_close(tree);
2020 static const struct got_error *
2021 browse_commit_tree(struct tog_view **new_view, int begin_x,
2022 struct commit_queue_entry *entry, const char *path,
2023 const char *head_ref_name, struct got_repository *repo)
2025 const struct got_error *err = NULL;
2026 struct tog_tree_view_state *s;
2027 struct tog_view *tree_view;
2029 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2030 if (tree_view == NULL)
2031 return got_error_from_errno("view_open");
2033 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2036 s = &tree_view->state.tree;
2038 *new_view = tree_view;
2040 if (got_path_is_root_dir(path))
2043 return tree_view_walk_path(s, entry->commit, path);
2046 static const struct got_error *
2047 block_signals_used_by_main_thread(void)
2052 if (sigemptyset(&sigset) == -1)
2053 return got_error_from_errno("sigemptyset");
2055 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2056 if (sigaddset(&sigset, SIGWINCH) == -1)
2057 return got_error_from_errno("sigaddset");
2058 if (sigaddset(&sigset, SIGCONT) == -1)
2059 return got_error_from_errno("sigaddset");
2060 if (sigaddset(&sigset, SIGINT) == -1)
2061 return got_error_from_errno("sigaddset");
2062 if (sigaddset(&sigset, SIGTERM) == -1)
2063 return got_error_from_errno("sigaddset");
2065 /* ncurses handles SIGTSTP */
2066 if (sigaddset(&sigset, SIGTSTP) == -1)
2067 return got_error_from_errno("sigaddset");
2069 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2071 return got_error_set_errno(errcode, "pthread_sigmask");
2077 log_thread(void *arg)
2079 const struct got_error *err = NULL;
2081 struct tog_log_thread_args *a = arg;
2084 err = block_signals_used_by_main_thread();
2088 while (!done && !err && !tog_fatal_signal_received()) {
2089 err = queue_commits(a);
2091 if (err->code != GOT_ERR_ITER_COMPLETED)
2095 } else if (a->commits_needed > 0 && !a->load_all)
2096 a->commits_needed--;
2098 errcode = pthread_mutex_lock(&tog_mutex);
2100 err = got_error_set_errno(errcode,
2101 "pthread_mutex_lock");
2103 } else if (*a->quit)
2105 else if (*a->first_displayed_entry == NULL) {
2106 *a->first_displayed_entry =
2107 TAILQ_FIRST(&a->commits->head);
2108 *a->selected_entry = *a->first_displayed_entry;
2111 errcode = pthread_cond_signal(&a->commit_loaded);
2113 err = got_error_set_errno(errcode,
2114 "pthread_cond_signal");
2115 pthread_mutex_unlock(&tog_mutex);
2120 a->commits_needed = 0;
2122 if (a->commits_needed == 0 && !a->load_all) {
2123 errcode = pthread_cond_wait(&a->need_commits,
2126 err = got_error_set_errno(errcode,
2127 "pthread_cond_wait");
2133 errcode = pthread_mutex_unlock(&tog_mutex);
2134 if (errcode && err == NULL)
2135 err = got_error_set_errno(errcode,
2136 "pthread_mutex_unlock");
2138 a->log_complete = 1;
2142 static const struct got_error *
2143 stop_log_thread(struct tog_log_view_state *s)
2145 const struct got_error *err = NULL;
2150 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2152 return got_error_set_errno(errcode,
2153 "pthread_cond_signal");
2154 errcode = pthread_mutex_unlock(&tog_mutex);
2156 return got_error_set_errno(errcode,
2157 "pthread_mutex_unlock");
2158 errcode = pthread_join(s->thread, (void **)&err);
2160 return got_error_set_errno(errcode, "pthread_join");
2161 errcode = pthread_mutex_lock(&tog_mutex);
2163 return got_error_set_errno(errcode,
2164 "pthread_mutex_lock");
2165 s->thread = 0; //NULL;
2168 if (s->thread_args.repo) {
2169 err = got_repo_close(s->thread_args.repo);
2170 s->thread_args.repo = NULL;
2173 if (s->thread_args.graph) {
2174 got_commit_graph_close(s->thread_args.graph);
2175 s->thread_args.graph = NULL;
2181 static const struct got_error *
2182 close_log_view(struct tog_view *view)
2184 const struct got_error *err = NULL;
2185 struct tog_log_view_state *s = &view->state.log;
2188 err = stop_log_thread(s);
2190 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2191 if (errcode && err == NULL)
2192 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2194 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2195 if (errcode && err == NULL)
2196 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2198 free_commits(&s->commits);
2199 free(s->in_repo_path);
2200 s->in_repo_path = NULL;
2203 free(s->head_ref_name);
2204 s->head_ref_name = NULL;
2208 static const struct got_error *
2209 search_start_log_view(struct tog_view *view)
2211 struct tog_log_view_state *s = &view->state.log;
2213 s->matched_entry = NULL;
2214 s->search_entry = NULL;
2218 static const struct got_error *
2219 search_next_log_view(struct tog_view *view)
2221 const struct got_error *err = NULL;
2222 struct tog_log_view_state *s = &view->state.log;
2223 struct commit_queue_entry *entry;
2225 /* Display progress update in log view. */
2226 show_log_view(view);
2230 if (s->search_entry) {
2232 errcode = pthread_mutex_unlock(&tog_mutex);
2234 return got_error_set_errno(errcode,
2235 "pthread_mutex_unlock");
2236 ch = wgetch(view->window);
2237 errcode = pthread_mutex_lock(&tog_mutex);
2239 return got_error_set_errno(errcode,
2240 "pthread_mutex_lock");
2241 if (ch == KEY_BACKSPACE) {
2242 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2245 if (view->searching == TOG_SEARCH_FORWARD)
2246 entry = TAILQ_NEXT(s->search_entry, entry);
2248 entry = TAILQ_PREV(s->search_entry,
2249 commit_queue_head, entry);
2250 } else if (s->matched_entry) {
2251 if (view->searching == TOG_SEARCH_FORWARD)
2252 entry = TAILQ_NEXT(s->matched_entry, entry);
2254 entry = TAILQ_PREV(s->matched_entry,
2255 commit_queue_head, entry);
2257 entry = s->selected_entry;
2263 if (entry == NULL) {
2264 if (s->thread_args.log_complete ||
2265 view->searching == TOG_SEARCH_BACKWARD) {
2266 view->search_next_done =
2267 (s->matched_entry == NULL ?
2268 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2269 s->search_entry = NULL;
2273 * Poke the log thread for more commits and return,
2274 * allowing the main loop to make progress. Search
2275 * will resume at s->search_entry once we come back.
2277 s->thread_args.commits_needed++;
2278 return trigger_log_thread(view, 0);
2281 err = match_commit(&have_match, entry->id, entry->commit,
2286 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2287 s->matched_entry = entry;
2291 s->search_entry = entry;
2292 if (view->searching == TOG_SEARCH_FORWARD)
2293 entry = TAILQ_NEXT(entry, entry);
2295 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2298 if (s->matched_entry) {
2299 int cur = s->selected_entry->idx;
2300 while (cur < s->matched_entry->idx) {
2301 err = input_log_view(NULL, view, KEY_DOWN);
2306 while (cur > s->matched_entry->idx) {
2307 err = input_log_view(NULL, view, KEY_UP);
2314 s->search_entry = NULL;
2319 static const struct got_error *
2320 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2321 struct got_repository *repo, const char *head_ref_name,
2322 const char *in_repo_path, int log_branches)
2324 const struct got_error *err = NULL;
2325 struct tog_log_view_state *s = &view->state.log;
2326 struct got_repository *thread_repo = NULL;
2327 struct got_commit_graph *thread_graph = NULL;
2330 if (in_repo_path != s->in_repo_path) {
2331 free(s->in_repo_path);
2332 s->in_repo_path = strdup(in_repo_path);
2333 if (s->in_repo_path == NULL)
2334 return got_error_from_errno("strdup");
2337 /* The commit queue only contains commits being displayed. */
2338 TAILQ_INIT(&s->commits.head);
2339 s->commits.ncommits = 0;
2342 if (head_ref_name) {
2343 s->head_ref_name = strdup(head_ref_name);
2344 if (s->head_ref_name == NULL) {
2345 err = got_error_from_errno("strdup");
2349 s->start_id = got_object_id_dup(start_id);
2350 if (s->start_id == NULL) {
2351 err = got_error_from_errno("got_object_id_dup");
2354 s->log_branches = log_branches;
2356 STAILQ_INIT(&s->colors);
2357 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2358 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2359 get_color_value("TOG_COLOR_COMMIT"));
2362 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2363 get_color_value("TOG_COLOR_AUTHOR"));
2365 free_colors(&s->colors);
2368 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2369 get_color_value("TOG_COLOR_DATE"));
2371 free_colors(&s->colors);
2376 view->show = show_log_view;
2377 view->input = input_log_view;
2378 view->close = close_log_view;
2379 view->search_start = search_start_log_view;
2380 view->search_next = search_next_log_view;
2382 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2385 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2389 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2390 s->repo, NULL, NULL);
2394 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2396 err = got_error_set_errno(errcode, "pthread_cond_init");
2399 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2401 err = got_error_set_errno(errcode, "pthread_cond_init");
2405 s->thread_args.commits_needed = view->nlines;
2406 s->thread_args.graph = thread_graph;
2407 s->thread_args.commits = &s->commits;
2408 s->thread_args.in_repo_path = s->in_repo_path;
2409 s->thread_args.start_id = s->start_id;
2410 s->thread_args.repo = thread_repo;
2411 s->thread_args.log_complete = 0;
2412 s->thread_args.quit = &s->quit;
2413 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2414 s->thread_args.selected_entry = &s->selected_entry;
2415 s->thread_args.searching = &view->searching;
2416 s->thread_args.search_next_done = &view->search_next_done;
2417 s->thread_args.regex = &view->regex;
2420 close_log_view(view);
2424 static const struct got_error *
2425 show_log_view(struct tog_view *view)
2427 const struct got_error *err;
2428 struct tog_log_view_state *s = &view->state.log;
2430 if (s->thread == 0) { //NULL) {
2431 int errcode = pthread_create(&s->thread, NULL, log_thread,
2434 return got_error_set_errno(errcode, "pthread_create");
2435 if (s->thread_args.commits_needed > 0) {
2436 err = trigger_log_thread(view, 1);
2442 return draw_commits(view);
2445 static const struct got_error *
2446 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2448 const struct got_error *err = NULL;
2449 struct tog_log_view_state *s = &view->state.log;
2450 struct tog_view *diff_view = NULL, *tree_view = NULL;
2451 struct tog_view *ref_view = NULL;
2452 struct commit_queue_entry *entry;
2455 if (s->thread_args.load_all) {
2456 if (ch == KEY_BACKSPACE)
2457 s->thread_args.load_all = 0;
2458 else if (s->thread_args.log_complete) {
2459 s->thread_args.load_all = 0;
2460 log_scroll_down(view, s->commits.ncommits);
2461 s->selected = MIN(view->nlines - 2,
2462 s->commits.ncommits - 1);
2477 if (s->first_displayed_entry == NULL)
2479 if (s->selected > 0)
2482 log_scroll_up(s, 1);
2488 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2493 if (s->first_displayed_entry == NULL)
2495 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2498 log_scroll_up(s, view->nlines - 1);
2506 if (s->first_displayed_entry == NULL)
2508 if (s->selected < MIN(view->nlines - 2,
2509 s->commits.ncommits - 1))
2512 err = log_scroll_down(view, 1);
2520 /* We don't know yet how many commits, so we're forced to
2521 * traverse them all. */
2522 if (!s->thread_args.log_complete) {
2523 s->thread_args.load_all = 1;
2524 return trigger_log_thread(view, 0);
2528 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2529 for (n = 0; n < view->nlines - 1; n++) {
2532 s->first_displayed_entry = entry;
2533 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2536 s->selected = n - 1;
2542 struct commit_queue_entry *first;
2543 first = s->first_displayed_entry;
2546 err = log_scroll_down(view, view->nlines - 1);
2549 if (first == s->first_displayed_entry &&
2550 s->selected < MIN(view->nlines - 2,
2551 s->commits.ncommits - 1)) {
2552 /* can't scroll further down */
2553 s->selected = MIN(view->nlines - 2,
2554 s->commits.ncommits - 1);
2560 if (s->selected > view->nlines - 2)
2561 s->selected = view->nlines - 2;
2562 if (s->selected > s->commits.ncommits - 1)
2563 s->selected = s->commits.ncommits - 1;
2565 if (s->commits.ncommits < view->nlines - 1 &&
2566 !s->thread_args.log_complete) {
2567 s->thread_args.commits_needed += (view->nlines - 1) -
2568 s->commits.ncommits;
2569 err = trigger_log_thread(view, 1);
2575 if (s->selected_entry == NULL)
2577 if (view_is_parent_view(view))
2578 begin_x = view_split_begin_x(view->begin_x);
2579 err = open_diff_view_for_commit(&diff_view, begin_x,
2580 s->selected_entry->commit, s->selected_entry->id,
2585 diff_view->focussed = 1;
2586 if (view_is_parent_view(view)) {
2587 err = view_close_child(view);
2590 view_set_child(view, diff_view);
2591 view->focus_child = 1;
2593 *new_view = diff_view;
2596 if (s->selected_entry == NULL)
2598 if (view_is_parent_view(view))
2599 begin_x = view_split_begin_x(view->begin_x);
2600 err = browse_commit_tree(&tree_view, begin_x,
2601 s->selected_entry, s->in_repo_path, s->head_ref_name,
2606 tree_view->focussed = 1;
2607 if (view_is_parent_view(view)) {
2608 err = view_close_child(view);
2611 view_set_child(view, tree_view);
2612 view->focus_child = 1;
2614 *new_view = tree_view;
2619 if (ch == KEY_BACKSPACE &&
2620 got_path_is_root_dir(s->in_repo_path))
2622 err = stop_log_thread(s);
2625 if (ch == KEY_BACKSPACE) {
2627 err = got_path_dirname(&parent_path, s->in_repo_path);
2630 free(s->in_repo_path);
2631 s->in_repo_path = parent_path;
2632 s->thread_args.in_repo_path = s->in_repo_path;
2633 } else if (ch == CTRL('l')) {
2634 struct got_object_id *start_id;
2635 err = got_repo_match_object_id(&start_id, NULL,
2636 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2637 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2641 s->start_id = start_id;
2642 s->thread_args.start_id = s->start_id;
2644 s->log_branches = !s->log_branches;
2646 err = got_repo_open(&s->thread_args.repo,
2647 got_repo_get_path(s->repo), NULL);
2651 err = tog_load_refs(s->repo, 0);
2654 err = got_commit_graph_open(&s->thread_args.graph,
2655 s->in_repo_path, !s->log_branches);
2658 err = got_commit_graph_iter_start(s->thread_args.graph,
2659 s->start_id, s->repo, NULL, NULL);
2662 free_commits(&s->commits);
2663 s->first_displayed_entry = NULL;
2664 s->last_displayed_entry = NULL;
2665 s->selected_entry = NULL;
2667 s->thread_args.log_complete = 0;
2669 s->thread_args.commits_needed = view->nlines;
2672 if (view_is_parent_view(view))
2673 begin_x = view_split_begin_x(view->begin_x);
2674 ref_view = view_open(view->nlines, view->ncols,
2675 view->begin_y, begin_x, TOG_VIEW_REF);
2676 if (ref_view == NULL)
2677 return got_error_from_errno("view_open");
2678 err = open_ref_view(ref_view, s->repo);
2680 view_close(ref_view);
2684 ref_view->focussed = 1;
2685 if (view_is_parent_view(view)) {
2686 err = view_close_child(view);
2689 view_set_child(view, ref_view);
2690 view->focus_child = 1;
2692 *new_view = ref_view;
2701 static const struct got_error *
2702 apply_unveil(const char *repo_path, const char *worktree_path)
2704 const struct got_error *error;
2707 if (unveil("gmon.out", "rwc") != 0)
2708 return got_error_from_errno2("unveil", "gmon.out");
2710 if (repo_path && unveil(repo_path, "r") != 0)
2711 return got_error_from_errno2("unveil", repo_path);
2713 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2714 return got_error_from_errno2("unveil", worktree_path);
2716 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2717 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2719 error = got_privsep_unveil_exec_helpers();
2723 if (unveil(NULL, NULL) != 0)
2724 return got_error_from_errno("unveil");
2733 * Override default signal handlers before starting ncurses.
2734 * This should prevent ncurses from installing its own
2735 * broken cleanup() signal handler.
2737 signal(SIGWINCH, tog_sigwinch);
2738 signal(SIGPIPE, tog_sigpipe);
2739 signal(SIGCONT, tog_sigcont);
2740 signal(SIGINT, tog_sigint);
2741 signal(SIGTERM, tog_sigterm);
2745 halfdelay(1); /* Do fast refresh while initial view is loading. */
2748 intrflush(stdscr, FALSE);
2749 keypad(stdscr, TRUE);
2751 if (getenv("TOG_COLORS") != NULL) {
2753 use_default_colors();
2757 static const struct got_error *
2758 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2759 struct got_repository *repo, struct got_worktree *worktree)
2761 const struct got_error *err = NULL;
2764 *in_repo_path = strdup("/");
2765 if (*in_repo_path == NULL)
2766 return got_error_from_errno("strdup");
2771 const char *prefix = got_worktree_get_path_prefix(worktree);
2774 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2777 if (asprintf(in_repo_path, "%s%s%s", prefix,
2778 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2780 err = got_error_from_errno("asprintf");
2781 *in_repo_path = NULL;
2785 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2790 static const struct got_error *
2791 cmd_log(int argc, char *argv[])
2793 const struct got_error *error;
2794 struct got_repository *repo = NULL;
2795 struct got_worktree *worktree = NULL;
2796 struct got_object_id *start_id = NULL;
2797 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2798 char *start_commit = NULL, *label = NULL;
2799 struct got_reference *ref = NULL;
2800 const char *head_ref_name = NULL;
2801 int ch, log_branches = 0;
2802 struct tog_view *view;
2804 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2810 start_commit = optarg;
2813 repo_path = realpath(optarg, NULL);
2814 if (repo_path == NULL)
2815 return got_error_from_errno2("realpath",
2830 if (repo_path == NULL) {
2831 cwd = getcwd(NULL, 0);
2833 return got_error_from_errno("getcwd");
2834 error = got_worktree_open(&worktree, cwd);
2835 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2839 strdup(got_worktree_get_repo_path(worktree));
2841 repo_path = strdup(cwd);
2842 if (repo_path == NULL) {
2843 error = got_error_from_errno("strdup");
2848 error = got_repo_open(&repo, repo_path, NULL);
2852 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2859 error = apply_unveil(got_repo_get_path(repo),
2860 worktree ? got_worktree_get_root_path(worktree) : NULL);
2864 /* already loaded by tog_log_with_path()? */
2865 if (TAILQ_EMPTY(&tog_refs)) {
2866 error = tog_load_refs(repo, 0);
2871 if (start_commit == NULL) {
2872 error = got_repo_match_object_id(&start_id, &label,
2873 worktree ? got_worktree_get_head_ref_name(worktree) :
2874 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2877 head_ref_name = label;
2879 error = got_ref_open(&ref, repo, start_commit, 0);
2881 head_ref_name = got_ref_get_name(ref);
2882 else if (error->code != GOT_ERR_NOT_REF)
2884 error = got_repo_match_object_id(&start_id, NULL,
2885 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2890 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2892 error = got_error_from_errno("view_open");
2895 error = open_log_view(view, start_id, repo, head_ref_name,
2896 in_repo_path, log_branches);
2900 /* Release work tree lock. */
2901 got_worktree_close(worktree);
2904 error = view_loop(view);
2914 const struct got_error *close_err = got_repo_close(repo);
2919 got_worktree_close(worktree);
2928 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2929 "[-w] object1 object2\n", getprogname());
2934 match_line(const char *line, regex_t *regex, size_t nmatch,
2935 regmatch_t *regmatch)
2937 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2941 match_color(struct tog_colors *colors, const char *line)
2943 struct tog_color *tc = NULL;
2945 STAILQ_FOREACH(tc, colors, entry) {
2946 if (match_line(line, &tc->regex, 0, NULL))
2953 static const struct got_error *
2954 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2955 WINDOW *window, regmatch_t *regmatch)
2957 const struct got_error *err = NULL;
2964 s = strndup(line, regmatch->rm_so);
2966 return got_error_from_errno("strndup");
2968 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2973 waddwstr(window, wline);
2980 s = strndup(line + regmatch->rm_so,
2981 regmatch->rm_eo - regmatch->rm_so);
2983 err = got_error_from_errno("strndup");
2987 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2992 wattr_on(window, A_STANDOUT, NULL);
2993 waddwstr(window, wline);
2994 wattr_off(window, A_STANDOUT, NULL);
3001 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3002 err = format_line(&wline, &width,
3003 line + regmatch->rm_eo, wlimit, col_tab_align);
3006 waddwstr(window, wline);
3014 static const struct got_error *
3015 draw_file(struct tog_view *view, const char *header)
3017 struct tog_diff_view_state *s = &view->state.diff;
3018 regmatch_t *regmatch = &view->regmatch;
3019 const struct got_error *err;
3022 size_t linesize = 0;
3024 struct tog_color *tc;
3027 int max_lines = view->nlines;
3028 int nlines = s->nlines;
3031 line_offset = s->line_offsets[s->first_displayed_line - 1];
3032 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3033 return got_error_from_errno("fseek");
3035 werase(view->window);
3038 if (asprintf(&line, "[%d/%d] %s",
3039 s->first_displayed_line - 1 + s->selected_line, nlines,
3041 return got_error_from_errno("asprintf");
3042 err = format_line(&wline, &width, line, view->ncols, 0);
3047 if (view_needs_focus_indication(view))
3048 wstandout(view->window);
3049 waddwstr(view->window, wline);
3052 if (view_needs_focus_indication(view))
3053 wstandend(view->window);
3054 if (width <= view->ncols - 1)
3055 waddch(view->window, '\n');
3064 while (max_lines > 0 && nprinted < max_lines) {
3065 linelen = getline(&line, &linesize, s->f);
3066 if (linelen == -1) {
3072 return got_ferror(s->f, GOT_ERR_IO);
3075 tc = match_color(&s->colors, line);
3077 wattr_on(view->window,
3078 COLOR_PAIR(tc->colorpair), NULL);
3079 if (s->first_displayed_line + nprinted == s->matched_line &&
3080 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3081 err = add_matched_line(&width, line, view->ncols, 0,
3082 view->window, regmatch);
3088 err = format_line(&wline, &width, line, view->ncols, 0);
3093 waddwstr(view->window, wline);
3098 wattr_off(view->window,
3099 COLOR_PAIR(tc->colorpair), NULL);
3100 if (width <= view->ncols - 1)
3101 waddch(view->window, '\n');
3106 s->last_displayed_line = s->first_displayed_line +
3109 s->last_displayed_line = s->first_displayed_line;
3114 while (nprinted < view->nlines) {
3115 waddch(view->window, '\n');
3119 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3124 wstandout(view->window);
3125 waddwstr(view->window, wline);
3128 wstandend(view->window);
3135 get_datestr(time_t *time, char *datebuf)
3137 struct tm mytm, *tm;
3140 tm = gmtime_r(time, &mytm);
3143 s = asctime_r(tm, datebuf);
3146 p = strchr(s, '\n');
3152 static const struct got_error *
3153 get_changed_paths(struct got_pathlist_head *paths,
3154 struct got_commit_object *commit, struct got_repository *repo)
3156 const struct got_error *err = NULL;
3157 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3158 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3159 struct got_object_qid *qid;
3161 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3163 struct got_commit_object *pcommit;
3164 err = got_object_open_as_commit(&pcommit, repo,
3169 tree_id1 = got_object_id_dup(
3170 got_object_commit_get_tree_id(pcommit));
3171 if (tree_id1 == NULL) {
3172 got_object_commit_close(pcommit);
3173 return got_error_from_errno("got_object_id_dup");
3175 got_object_commit_close(pcommit);
3180 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3185 tree_id2 = got_object_commit_get_tree_id(commit);
3186 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3190 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3191 got_diff_tree_collect_changed_paths, paths, 0);
3194 got_object_tree_close(tree1);
3196 got_object_tree_close(tree2);
3201 static const struct got_error *
3202 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3206 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3208 return got_error_from_errno("reallocarray");
3210 (*line_offsets)[*nlines] = off;
3215 static const struct got_error *
3216 write_commit_info(off_t **line_offsets, size_t *nlines,
3217 struct got_object_id *commit_id, struct got_reflist_head *refs,
3218 struct got_repository *repo, FILE *outfile)
3220 const struct got_error *err = NULL;
3221 char datebuf[26], *datestr;
3222 struct got_commit_object *commit;
3223 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3224 time_t committer_time;
3225 const char *author, *committer;
3226 char *refs_str = NULL;
3227 struct got_pathlist_head changed_paths;
3228 struct got_pathlist_entry *pe;
3232 TAILQ_INIT(&changed_paths);
3235 err = build_refs_str(&refs_str, refs, commit_id, repo);
3240 err = got_object_open_as_commit(&commit, repo, commit_id);
3244 err = got_object_id_str(&id_str, commit_id);
3246 err = got_error_from_errno("got_object_id_str");
3250 err = add_line_offset(line_offsets, nlines, 0);
3254 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3255 refs_str ? refs_str : "", refs_str ? ")" : "");
3257 err = got_error_from_errno("fprintf");
3261 err = add_line_offset(line_offsets, nlines, outoff);
3265 n = fprintf(outfile, "from: %s\n",
3266 got_object_commit_get_author(commit));
3268 err = got_error_from_errno("fprintf");
3272 err = add_line_offset(line_offsets, nlines, outoff);
3276 committer_time = got_object_commit_get_committer_time(commit);
3277 datestr = get_datestr(&committer_time, datebuf);
3279 n = fprintf(outfile, "date: %s UTC\n", datestr);
3281 err = got_error_from_errno("fprintf");
3285 err = add_line_offset(line_offsets, nlines, outoff);
3289 author = got_object_commit_get_author(commit);
3290 committer = got_object_commit_get_committer(commit);
3291 if (strcmp(author, committer) != 0) {
3292 n = fprintf(outfile, "via: %s\n", committer);
3294 err = got_error_from_errno("fprintf");
3298 err = add_line_offset(line_offsets, nlines, outoff);
3302 if (got_object_commit_get_nparents(commit) > 1) {
3303 const struct got_object_id_queue *parent_ids;
3304 struct got_object_qid *qid;
3306 parent_ids = got_object_commit_get_parent_ids(commit);
3307 STAILQ_FOREACH(qid, parent_ids, entry) {
3308 err = got_object_id_str(&id_str, &qid->id);
3311 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3313 err = got_error_from_errno("fprintf");
3317 err = add_line_offset(line_offsets, nlines, outoff);
3325 err = got_object_commit_get_logmsg(&logmsg, commit);
3329 while ((line = strsep(&s, "\n")) != NULL) {
3330 n = fprintf(outfile, "%s\n", line);
3332 err = got_error_from_errno("fprintf");
3336 err = add_line_offset(line_offsets, nlines, outoff);
3341 err = get_changed_paths(&changed_paths, commit, repo);
3344 TAILQ_FOREACH(pe, &changed_paths, entry) {
3345 struct got_diff_changed_path *cp = pe->data;
3346 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3348 err = got_error_from_errno("fprintf");
3352 err = add_line_offset(line_offsets, nlines, outoff);
3355 free((char *)pe->path);
3359 fputc('\n', outfile);
3361 err = add_line_offset(line_offsets, nlines, outoff);
3363 got_pathlist_free(&changed_paths);
3367 got_object_commit_close(commit);
3369 free(*line_offsets);
3370 *line_offsets = NULL;
3376 static const struct got_error *
3377 create_diff(struct tog_diff_view_state *s)
3379 const struct got_error *err = NULL;
3383 free(s->line_offsets);
3384 s->line_offsets = malloc(sizeof(off_t));
3385 if (s->line_offsets == NULL)
3386 return got_error_from_errno("malloc");
3391 err = got_error_from_errno("got_opentemp");
3394 if (s->f && fclose(s->f) == EOF) {
3395 err = got_error_from_errno("fclose");
3401 err = got_object_get_type(&obj_type, s->repo, s->id1);
3403 err = got_object_get_type(&obj_type, s->repo, s->id2);
3408 case GOT_OBJ_TYPE_BLOB:
3409 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3410 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3411 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3414 case GOT_OBJ_TYPE_TREE:
3415 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3416 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3417 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3419 case GOT_OBJ_TYPE_COMMIT: {
3420 const struct got_object_id_queue *parent_ids;
3421 struct got_object_qid *pid;
3422 struct got_commit_object *commit2;
3423 struct got_reflist_head *refs;
3425 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3428 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3429 /* Show commit info if we're diffing to a parent/root commit. */
3430 if (s->id1 == NULL) {
3431 err = write_commit_info(&s->line_offsets, &s->nlines,
3432 s->id2, refs, s->repo, s->f);
3436 parent_ids = got_object_commit_get_parent_ids(commit2);
3437 STAILQ_FOREACH(pid, parent_ids, entry) {
3438 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3439 err = write_commit_info(
3440 &s->line_offsets, &s->nlines,
3441 s->id2, refs, s->repo, s->f);
3448 got_object_commit_close(commit2);
3450 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3451 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3452 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3456 err = got_error(GOT_ERR_OBJ_TYPE);
3462 if (s->f && fflush(s->f) != 0 && err == NULL)
3463 err = got_error_from_errno("fflush");
3468 diff_view_indicate_progress(struct tog_view *view)
3470 mvwaddstr(view->window, 0, 0, "diffing...");
3475 static const struct got_error *
3476 search_start_diff_view(struct tog_view *view)
3478 struct tog_diff_view_state *s = &view->state.diff;
3480 s->matched_line = 0;
3484 static const struct got_error *
3485 search_next_diff_view(struct tog_view *view)
3487 struct tog_diff_view_state *s = &view->state.diff;
3490 size_t linesize = 0;
3493 if (!view->searching) {
3494 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3498 if (s->matched_line) {
3499 if (view->searching == TOG_SEARCH_FORWARD)
3500 lineno = s->matched_line + 1;
3502 lineno = s->matched_line - 1;
3504 lineno = s->first_displayed_line;
3509 if (lineno <= 0 || lineno > s->nlines) {
3510 if (s->matched_line == 0) {
3511 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3515 if (view->searching == TOG_SEARCH_FORWARD)
3521 offset = s->line_offsets[lineno - 1];
3522 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3524 return got_error_from_errno("fseeko");
3526 linelen = getline(&line, &linesize, s->f);
3527 if (linelen != -1 &&
3528 match_line(line, &view->regex, 1, &view->regmatch)) {
3529 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3530 s->matched_line = lineno;
3533 if (view->searching == TOG_SEARCH_FORWARD)
3540 if (s->matched_line) {
3541 s->first_displayed_line = s->matched_line;
3542 s->selected_line = 1;
3548 static const struct got_error *
3549 close_diff_view(struct tog_view *view)
3551 const struct got_error *err = NULL;
3552 struct tog_diff_view_state *s = &view->state.diff;
3558 if (s->f && fclose(s->f) == EOF)
3559 err = got_error_from_errno("fclose");
3561 if (s->f1 && fclose(s->f1) == EOF)
3562 err = got_error_from_errno("fclose");
3564 if (s->f2 && fclose(s->f2) == EOF)
3565 err = got_error_from_errno("fclose");
3567 free_colors(&s->colors);
3568 free(s->line_offsets);
3569 s->line_offsets = NULL;
3574 static const struct got_error *
3575 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3576 struct got_object_id *id2, const char *label1, const char *label2,
3577 int diff_context, int ignore_whitespace, int force_text_diff,
3578 struct tog_view *log_view, struct got_repository *repo)
3580 const struct got_error *err;
3581 struct tog_diff_view_state *s = &view->state.diff;
3583 memset(s, 0, sizeof(*s));
3585 if (id1 != NULL && id2 != NULL) {
3587 err = got_object_get_type(&type1, repo, id1);
3590 err = got_object_get_type(&type2, repo, id2);
3595 return got_error(GOT_ERR_OBJ_TYPE);
3597 s->first_displayed_line = 1;
3598 s->last_displayed_line = view->nlines;
3599 s->selected_line = 1;
3607 s->id1 = got_object_id_dup(id1);
3609 return got_error_from_errno("got_object_id_dup");
3610 s->f1 = got_opentemp();
3611 if (s->f1 == NULL) {
3612 err = got_error_from_errno("got_opentemp");
3618 s->id2 = got_object_id_dup(id2);
3619 if (s->id2 == NULL) {
3620 err = got_error_from_errno("got_object_id_dup");
3624 s->f2 = got_opentemp();
3625 if (s->f2 == NULL) {
3626 err = got_error_from_errno("got_opentemp");
3630 s->first_displayed_line = 1;
3631 s->last_displayed_line = view->nlines;
3632 s->diff_context = diff_context;
3633 s->ignore_whitespace = ignore_whitespace;
3634 s->force_text_diff = force_text_diff;
3635 s->log_view = log_view;
3638 STAILQ_INIT(&s->colors);
3639 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3640 err = add_color(&s->colors,
3641 "^-", TOG_COLOR_DIFF_MINUS,
3642 get_color_value("TOG_COLOR_DIFF_MINUS"));
3645 err = add_color(&s->colors, "^\\+",
3646 TOG_COLOR_DIFF_PLUS,
3647 get_color_value("TOG_COLOR_DIFF_PLUS"));
3650 err = add_color(&s->colors,
3651 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3652 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3656 err = add_color(&s->colors,
3657 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3658 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3659 get_color_value("TOG_COLOR_DIFF_META"));
3663 err = add_color(&s->colors,
3664 "^(from|via): ", TOG_COLOR_AUTHOR,
3665 get_color_value("TOG_COLOR_AUTHOR"));
3669 err = add_color(&s->colors,
3670 "^date: ", TOG_COLOR_DATE,
3671 get_color_value("TOG_COLOR_DATE"));
3676 if (log_view && view_is_splitscreen(view))
3677 show_log_view(log_view); /* draw vborder */
3678 diff_view_indicate_progress(view);
3680 err = create_diff(s);
3682 view->show = show_diff_view;
3683 view->input = input_diff_view;
3684 view->close = close_diff_view;
3685 view->search_start = search_start_diff_view;
3686 view->search_next = search_next_diff_view;
3689 close_diff_view(view);
3693 static const struct got_error *
3694 show_diff_view(struct tog_view *view)
3696 const struct got_error *err;
3697 struct tog_diff_view_state *s = &view->state.diff;
3698 char *id_str1 = NULL, *id_str2, *header;
3699 const char *label1, *label2;
3702 err = got_object_id_str(&id_str1, s->id1);
3705 label1 = s->label1 ? : id_str1;
3707 label1 = "/dev/null";
3709 err = got_object_id_str(&id_str2, s->id2);
3712 label2 = s->label2 ? : id_str2;
3714 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3715 err = got_error_from_errno("asprintf");
3723 err = draw_file(view, header);
3728 static const struct got_error *
3729 set_selected_commit(struct tog_diff_view_state *s,
3730 struct commit_queue_entry *entry)
3732 const struct got_error *err;
3733 const struct got_object_id_queue *parent_ids;
3734 struct got_commit_object *selected_commit;
3735 struct got_object_qid *pid;
3738 s->id2 = got_object_id_dup(entry->id);
3740 return got_error_from_errno("got_object_id_dup");
3742 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3745 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3747 pid = STAILQ_FIRST(parent_ids);
3748 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3749 got_object_commit_close(selected_commit);
3753 static const struct got_error *
3754 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3756 const struct got_error *err = NULL;
3757 struct tog_diff_view_state *s = &view->state.diff;
3758 struct tog_log_view_state *ls;
3759 struct commit_queue_entry *old_selected_entry;
3761 size_t linesize = 0;
3769 s->force_text_diff = !s->force_text_diff;
3771 s->ignore_whitespace = !s->ignore_whitespace;
3772 wclear(view->window);
3773 s->first_displayed_line = 1;
3774 s->last_displayed_line = view->nlines;
3775 s->matched_line = 0;
3776 diff_view_indicate_progress(view);
3777 err = create_diff(s);
3781 s->first_displayed_line = 1;
3788 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3794 if (s->first_displayed_line > 1)
3795 s->first_displayed_line--;
3799 if (s->first_displayed_line == 1)
3802 while (i++ < view->nlines - 1 &&
3803 s->first_displayed_line > 1)
3804 s->first_displayed_line--;
3810 s->first_displayed_line++;
3818 while (!s->eof && i++ < view->nlines - 1) {
3819 linelen = getline(&line, &linesize, s->f);
3820 s->first_displayed_line++;
3821 if (linelen == -1) {
3825 err = got_ferror(s->f, GOT_ERR_IO);
3832 if (s->diff_context > 0) {
3834 s->matched_line = 0;
3835 diff_view_indicate_progress(view);
3836 err = create_diff(s);
3837 if (s->first_displayed_line + view->nlines - 1 >
3839 s->first_displayed_line = 1;
3840 s->last_displayed_line = view->nlines;
3845 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3847 s->matched_line = 0;
3848 diff_view_indicate_progress(view);
3849 err = create_diff(s);
3854 if (s->log_view == NULL)
3856 ls = &s->log_view->state.log;
3857 old_selected_entry = ls->selected_entry;
3859 err = input_log_view(NULL, s->log_view, KEY_UP);
3863 if (old_selected_entry == ls->selected_entry)
3866 err = set_selected_commit(s, ls->selected_entry);
3870 s->first_displayed_line = 1;
3871 s->last_displayed_line = view->nlines;
3872 s->matched_line = 0;
3874 diff_view_indicate_progress(view);
3875 err = create_diff(s);
3879 if (s->log_view == NULL)
3881 ls = &s->log_view->state.log;
3882 old_selected_entry = ls->selected_entry;
3884 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3888 if (old_selected_entry == ls->selected_entry)
3891 err = set_selected_commit(s, ls->selected_entry);
3895 s->first_displayed_line = 1;
3896 s->last_displayed_line = view->nlines;
3897 s->matched_line = 0;
3899 diff_view_indicate_progress(view);
3900 err = create_diff(s);
3909 static const struct got_error *
3910 cmd_diff(int argc, char *argv[])
3912 const struct got_error *error = NULL;
3913 struct got_repository *repo = NULL;
3914 struct got_worktree *worktree = NULL;
3915 struct got_object_id *id1 = NULL, *id2 = NULL;
3916 char *repo_path = NULL, *cwd = NULL;
3917 char *id_str1 = NULL, *id_str2 = NULL;
3918 char *label1 = NULL, *label2 = NULL;
3919 int diff_context = 3, ignore_whitespace = 0;
3920 int ch, force_text_diff = 0;
3922 struct tog_view *view;
3924 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3927 force_text_diff = 1;
3930 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3933 errx(1, "number of context lines is %s: %s",
3937 repo_path = realpath(optarg, NULL);
3938 if (repo_path == NULL)
3939 return got_error_from_errno2("realpath",
3941 got_path_strip_trailing_slashes(repo_path);
3944 ignore_whitespace = 1;
3956 usage_diff(); /* TODO show local worktree changes */
3957 } else if (argc == 2) {
3963 if (repo_path == NULL) {
3964 cwd = getcwd(NULL, 0);
3966 return got_error_from_errno("getcwd");
3967 error = got_worktree_open(&worktree, cwd);
3968 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3972 strdup(got_worktree_get_repo_path(worktree));
3974 repo_path = strdup(cwd);
3975 if (repo_path == NULL) {
3976 error = got_error_from_errno("strdup");
3981 error = got_repo_open(&repo, repo_path, NULL);
3987 error = apply_unveil(got_repo_get_path(repo), NULL);
3991 error = tog_load_refs(repo, 0);
3995 error = got_repo_match_object_id(&id1, &label1, id_str1,
3996 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4000 error = got_repo_match_object_id(&id2, &label2, id_str2,
4001 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4005 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4007 error = got_error_from_errno("view_open");
4010 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4011 ignore_whitespace, force_text_diff, NULL, repo);
4014 error = view_loop(view);
4021 const struct got_error *close_err = got_repo_close(repo);
4026 got_worktree_close(worktree);
4035 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4040 struct tog_blame_line {
4042 struct got_object_id *id;
4045 static const struct got_error *
4046 draw_blame(struct tog_view *view)
4048 struct tog_blame_view_state *s = &view->state.blame;
4049 struct tog_blame *blame = &s->blame;
4050 regmatch_t *regmatch = &view->regmatch;
4051 const struct got_error *err;
4052 int lineno = 0, nprinted = 0;
4054 size_t linesize = 0;
4058 struct tog_blame_line *blame_line;
4059 struct got_object_id *prev_id = NULL;
4061 struct tog_color *tc;
4063 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4068 werase(view->window);
4070 if (asprintf(&line, "commit %s", id_str) == -1) {
4071 err = got_error_from_errno("asprintf");
4076 err = format_line(&wline, &width, line, view->ncols, 0);
4081 if (view_needs_focus_indication(view))
4082 wstandout(view->window);
4083 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4085 wattr_on(view->window,
4086 COLOR_PAIR(tc->colorpair), NULL);
4087 waddwstr(view->window, wline);
4089 wattr_off(view->window,
4090 COLOR_PAIR(tc->colorpair), NULL);
4091 if (view_needs_focus_indication(view))
4092 wstandend(view->window);
4095 if (width < view->ncols - 1)
4096 waddch(view->window, '\n');
4098 if (asprintf(&line, "[%d/%d] %s%s",
4099 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4100 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4102 return got_error_from_errno("asprintf");
4105 err = format_line(&wline, &width, line, view->ncols, 0);
4110 waddwstr(view->window, wline);
4113 if (width < view->ncols - 1)
4114 waddch(view->window, '\n');
4117 while (nprinted < view->nlines - 2) {
4118 linelen = getline(&line, &linesize, blame->f);
4119 if (linelen == -1) {
4120 if (feof(blame->f)) {
4125 return got_ferror(blame->f, GOT_ERR_IO);
4127 if (++lineno < s->first_displayed_line)
4130 if (view->focussed && nprinted == s->selected_line - 1)
4131 wstandout(view->window);
4133 if (blame->nlines > 0) {
4134 blame_line = &blame->lines[lineno - 1];
4135 if (blame_line->annotated && prev_id &&
4136 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4138 nprinted == s->selected_line - 1)) {
4139 waddstr(view->window, " ");
4140 } else if (blame_line->annotated) {
4142 err = got_object_id_str(&id_str, blame_line->id);
4147 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4149 wattr_on(view->window,
4150 COLOR_PAIR(tc->colorpair), NULL);
4151 wprintw(view->window, "%.8s", id_str);
4153 wattr_off(view->window,
4154 COLOR_PAIR(tc->colorpair), NULL);
4156 prev_id = blame_line->id;
4158 waddstr(view->window, "........");
4162 waddstr(view->window, "........");
4166 if (view->focussed && nprinted == s->selected_line - 1)
4167 wstandend(view->window);
4168 waddstr(view->window, " ");
4170 if (view->ncols <= 9) {
4172 wline = wcsdup(L"");
4173 if (wline == NULL) {
4174 err = got_error_from_errno("wcsdup");
4178 } else if (s->first_displayed_line + nprinted ==
4180 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4181 err = add_matched_line(&width, line, view->ncols - 9, 9,
4182 view->window, regmatch);
4189 err = format_line(&wline, &width, line,
4190 view->ncols - 9, 9);
4191 waddwstr(view->window, wline);
4197 if (width <= view->ncols - 1)
4198 waddch(view->window, '\n');
4199 if (++nprinted == 1)
4200 s->first_displayed_line = lineno;
4203 s->last_displayed_line = lineno;
4210 static const struct got_error *
4211 blame_cb(void *arg, int nlines, int lineno,
4212 struct got_commit_object *commit, struct got_object_id *id)
4214 const struct got_error *err = NULL;
4215 struct tog_blame_cb_args *a = arg;
4216 struct tog_blame_line *line;
4219 if (nlines != a->nlines ||
4220 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4221 return got_error(GOT_ERR_RANGE);
4223 errcode = pthread_mutex_lock(&tog_mutex);
4225 return got_error_set_errno(errcode, "pthread_mutex_lock");
4227 if (*a->quit) { /* user has quit the blame view */
4228 err = got_error(GOT_ERR_ITER_COMPLETED);
4233 goto done; /* no change in this commit */
4235 line = &a->lines[lineno - 1];
4236 if (line->annotated)
4239 line->id = got_object_id_dup(id);
4240 if (line->id == NULL) {
4241 err = got_error_from_errno("got_object_id_dup");
4244 line->annotated = 1;
4246 errcode = pthread_mutex_unlock(&tog_mutex);
4248 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4253 blame_thread(void *arg)
4255 const struct got_error *err, *close_err;
4256 struct tog_blame_thread_args *ta = arg;
4257 struct tog_blame_cb_args *a = ta->cb_args;
4260 err = block_signals_used_by_main_thread();
4264 err = got_blame(ta->path, a->commit_id, ta->repo,
4265 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4266 if (err && err->code == GOT_ERR_CANCELLED)
4269 errcode = pthread_mutex_lock(&tog_mutex);
4271 return (void *)got_error_set_errno(errcode,
4272 "pthread_mutex_lock");
4274 close_err = got_repo_close(ta->repo);
4280 errcode = pthread_mutex_unlock(&tog_mutex);
4281 if (errcode && err == NULL)
4282 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4287 static struct got_object_id *
4288 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4289 int first_displayed_line, int selected_line)
4291 struct tog_blame_line *line;
4296 line = &lines[first_displayed_line - 1 + selected_line - 1];
4297 if (!line->annotated)
4303 static const struct got_error *
4304 stop_blame(struct tog_blame *blame)
4306 const struct got_error *err = NULL;
4309 if (blame->thread) {
4311 errcode = pthread_mutex_unlock(&tog_mutex);
4313 return got_error_set_errno(errcode,
4314 "pthread_mutex_unlock");
4315 errcode = pthread_join(blame->thread, (void **)&err);
4317 return got_error_set_errno(errcode, "pthread_join");
4318 errcode = pthread_mutex_lock(&tog_mutex);
4320 return got_error_set_errno(errcode,
4321 "pthread_mutex_lock");
4322 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4324 blame->thread = 0; //NULL;
4326 if (blame->thread_args.repo) {
4327 const struct got_error *close_err;
4328 close_err = got_repo_close(blame->thread_args.repo);
4331 blame->thread_args.repo = NULL;
4334 if (fclose(blame->f) == EOF && err == NULL)
4335 err = got_error_from_errno("fclose");
4339 for (i = 0; i < blame->nlines; i++)
4340 free(blame->lines[i].id);
4342 blame->lines = NULL;
4344 free(blame->cb_args.commit_id);
4345 blame->cb_args.commit_id = NULL;
4350 static const struct got_error *
4351 cancel_blame_view(void *arg)
4353 const struct got_error *err = NULL;
4357 errcode = pthread_mutex_lock(&tog_mutex);
4359 return got_error_set_errno(errcode,
4360 "pthread_mutex_unlock");
4363 err = got_error(GOT_ERR_CANCELLED);
4365 errcode = pthread_mutex_unlock(&tog_mutex);
4367 return got_error_set_errno(errcode,
4368 "pthread_mutex_lock");
4373 static const struct got_error *
4374 run_blame(struct tog_view *view)
4376 struct tog_blame_view_state *s = &view->state.blame;
4377 struct tog_blame *blame = &s->blame;
4378 const struct got_error *err = NULL;
4379 struct got_commit_object *commit = NULL;
4380 struct got_blob_object *blob = NULL;
4381 struct got_repository *thread_repo = NULL;
4382 struct got_object_id *obj_id = NULL;
4385 err = got_object_open_as_commit(&commit, s->repo,
4386 &s->blamed_commit->id);
4390 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4394 err = got_object_get_type(&obj_type, s->repo, obj_id);
4398 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4399 err = got_error(GOT_ERR_OBJ_TYPE);
4403 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4406 blame->f = got_opentemp();
4407 if (blame->f == NULL) {
4408 err = got_error_from_errno("got_opentemp");
4411 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4412 &blame->line_offsets, blame->f, blob);
4415 if (blame->nlines == 0) {
4416 s->blame_complete = 1;
4420 /* Don't include \n at EOF in the blame line count. */
4421 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4424 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4425 if (blame->lines == NULL) {
4426 err = got_error_from_errno("calloc");
4430 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4434 blame->cb_args.view = view;
4435 blame->cb_args.lines = blame->lines;
4436 blame->cb_args.nlines = blame->nlines;
4437 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4438 if (blame->cb_args.commit_id == NULL) {
4439 err = got_error_from_errno("got_object_id_dup");
4442 blame->cb_args.quit = &s->done;
4444 blame->thread_args.path = s->path;
4445 blame->thread_args.repo = thread_repo;
4446 blame->thread_args.cb_args = &blame->cb_args;
4447 blame->thread_args.complete = &s->blame_complete;
4448 blame->thread_args.cancel_cb = cancel_blame_view;
4449 blame->thread_args.cancel_arg = &s->done;
4450 s->blame_complete = 0;
4452 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4453 s->first_displayed_line = 1;
4454 s->last_displayed_line = view->nlines;
4455 s->selected_line = 1;
4457 s->matched_line = 0;
4461 got_object_commit_close(commit);
4463 got_object_blob_close(blob);
4470 static const struct got_error *
4471 open_blame_view(struct tog_view *view, char *path,
4472 struct got_object_id *commit_id, struct got_repository *repo)
4474 const struct got_error *err = NULL;
4475 struct tog_blame_view_state *s = &view->state.blame;
4477 STAILQ_INIT(&s->blamed_commits);
4479 s->path = strdup(path);
4480 if (s->path == NULL)
4481 return got_error_from_errno("strdup");
4483 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4489 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4490 s->first_displayed_line = 1;
4491 s->last_displayed_line = view->nlines;
4492 s->selected_line = 1;
4493 s->blame_complete = 0;
4495 s->commit_id = commit_id;
4496 memset(&s->blame, 0, sizeof(s->blame));
4498 STAILQ_INIT(&s->colors);
4499 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4500 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4501 get_color_value("TOG_COLOR_COMMIT"));
4506 view->show = show_blame_view;
4507 view->input = input_blame_view;
4508 view->close = close_blame_view;
4509 view->search_start = search_start_blame_view;
4510 view->search_next = search_next_blame_view;
4512 return run_blame(view);
4515 static const struct got_error *
4516 close_blame_view(struct tog_view *view)
4518 const struct got_error *err = NULL;
4519 struct tog_blame_view_state *s = &view->state.blame;
4521 if (s->blame.thread)
4522 err = stop_blame(&s->blame);
4524 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4525 struct got_object_qid *blamed_commit;
4526 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4527 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4528 got_object_qid_free(blamed_commit);
4532 free_colors(&s->colors);
4537 static const struct got_error *
4538 search_start_blame_view(struct tog_view *view)
4540 struct tog_blame_view_state *s = &view->state.blame;
4542 s->matched_line = 0;
4546 static const struct got_error *
4547 search_next_blame_view(struct tog_view *view)
4549 struct tog_blame_view_state *s = &view->state.blame;
4552 size_t linesize = 0;
4555 if (!view->searching) {
4556 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4560 if (s->matched_line) {
4561 if (view->searching == TOG_SEARCH_FORWARD)
4562 lineno = s->matched_line + 1;
4564 lineno = s->matched_line - 1;
4566 lineno = s->first_displayed_line - 1 + s->selected_line;
4571 if (lineno <= 0 || lineno > s->blame.nlines) {
4572 if (s->matched_line == 0) {
4573 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4577 if (view->searching == TOG_SEARCH_FORWARD)
4580 lineno = s->blame.nlines;
4583 offset = s->blame.line_offsets[lineno - 1];
4584 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4586 return got_error_from_errno("fseeko");
4588 linelen = getline(&line, &linesize, s->blame.f);
4589 if (linelen != -1 &&
4590 match_line(line, &view->regex, 1, &view->regmatch)) {
4591 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4592 s->matched_line = lineno;
4595 if (view->searching == TOG_SEARCH_FORWARD)
4602 if (s->matched_line) {
4603 s->first_displayed_line = s->matched_line;
4604 s->selected_line = 1;
4610 static const struct got_error *
4611 show_blame_view(struct tog_view *view)
4613 const struct got_error *err = NULL;
4614 struct tog_blame_view_state *s = &view->state.blame;
4617 if (s->blame.thread == 0 && !s->blame_complete) {
4618 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4619 &s->blame.thread_args);
4621 return got_error_set_errno(errcode, "pthread_create");
4623 halfdelay(1); /* fast refresh while annotating */
4626 if (s->blame_complete)
4627 halfdelay(10); /* disable fast refresh */
4629 err = draw_blame(view);
4635 static const struct got_error *
4636 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4638 const struct got_error *err = NULL, *thread_err = NULL;
4639 struct tog_view *diff_view;
4640 struct tog_blame_view_state *s = &view->state.blame;
4649 s->selected_line = 1;
4650 s->first_displayed_line = 1;
4654 if (s->blame.nlines < view->nlines - 2) {
4655 s->selected_line = s->blame.nlines;
4656 s->first_displayed_line = 1;
4658 s->selected_line = view->nlines - 2;
4659 s->first_displayed_line = s->blame.nlines -
4666 if (s->selected_line > 1)
4668 else if (s->selected_line == 1 &&
4669 s->first_displayed_line > 1)
4670 s->first_displayed_line--;
4674 if (s->first_displayed_line == 1) {
4675 s->selected_line = 1;
4678 if (s->first_displayed_line > view->nlines - 2)
4679 s->first_displayed_line -=
4682 s->first_displayed_line = 1;
4687 if (s->selected_line < view->nlines - 2 &&
4688 s->first_displayed_line +
4689 s->selected_line <= s->blame.nlines)
4691 else if (s->last_displayed_line <
4693 s->first_displayed_line++;
4697 struct got_object_id *id = NULL;
4698 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4699 s->first_displayed_line, s->selected_line);
4703 struct got_commit_object *commit, *pcommit;
4704 struct got_object_qid *pid;
4705 struct got_object_id *blob_id = NULL;
4707 err = got_object_open_as_commit(&commit,
4712 got_object_commit_get_parent_ids(commit));
4714 got_object_commit_close(commit);
4717 /* Check if path history ends here. */
4718 err = got_object_open_as_commit(&pcommit,
4722 err = got_object_id_by_path(&blob_id, s->repo,
4724 got_object_commit_close(pcommit);
4726 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4728 got_object_commit_close(commit);
4731 err = got_object_get_type(&obj_type, s->repo,
4734 /* Can't blame non-blob type objects. */
4735 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4736 got_object_commit_close(commit);
4739 err = got_object_qid_alloc(&s->blamed_commit,
4741 got_object_commit_close(commit);
4743 if (got_object_id_cmp(id,
4744 &s->blamed_commit->id) == 0)
4746 err = got_object_qid_alloc(&s->blamed_commit,
4752 thread_err = stop_blame(&s->blame);
4756 STAILQ_INSERT_HEAD(&s->blamed_commits,
4757 s->blamed_commit, entry);
4758 err = run_blame(view);
4764 struct got_object_qid *first;
4765 first = STAILQ_FIRST(&s->blamed_commits);
4766 if (!got_object_id_cmp(&first->id, s->commit_id))
4769 thread_err = stop_blame(&s->blame);
4773 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4774 got_object_qid_free(s->blamed_commit);
4776 STAILQ_FIRST(&s->blamed_commits);
4777 err = run_blame(view);
4784 struct got_object_id *id = NULL;
4785 struct got_object_qid *pid;
4786 struct got_commit_object *commit = NULL;
4787 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4788 s->first_displayed_line, s->selected_line);
4791 err = got_object_open_as_commit(&commit, s->repo, id);
4795 got_object_commit_get_parent_ids(commit));
4796 if (view_is_parent_view(view))
4797 begin_x = view_split_begin_x(view->begin_x);
4798 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4799 if (diff_view == NULL) {
4800 got_object_commit_close(commit);
4801 err = got_error_from_errno("view_open");
4804 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4805 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4806 got_object_commit_close(commit);
4808 view_close(diff_view);
4812 diff_view->focussed = 1;
4813 if (view_is_parent_view(view)) {
4814 err = view_close_child(view);
4817 view_set_child(view, diff_view);
4818 view->focus_child = 1;
4820 *new_view = diff_view;
4828 if (s->last_displayed_line >= s->blame.nlines &&
4829 s->selected_line >= MIN(s->blame.nlines,
4830 view->nlines - 2)) {
4833 if (s->last_displayed_line >= s->blame.nlines &&
4834 s->selected_line < view->nlines - 2) {
4835 s->selected_line = MIN(s->blame.nlines,
4839 if (s->last_displayed_line + view->nlines - 2
4841 s->first_displayed_line +=
4844 s->first_displayed_line =
4849 if (s->selected_line > view->nlines - 2) {
4850 s->selected_line = MIN(s->blame.nlines,
4857 return thread_err ? thread_err : err;
4860 static const struct got_error *
4861 cmd_blame(int argc, char *argv[])
4863 const struct got_error *error;
4864 struct got_repository *repo = NULL;
4865 struct got_worktree *worktree = NULL;
4866 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4867 char *link_target = NULL;
4868 struct got_object_id *commit_id = NULL;
4869 struct got_commit_object *commit = NULL;
4870 char *commit_id_str = NULL;
4872 struct tog_view *view;
4874 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4877 commit_id_str = optarg;
4880 repo_path = realpath(optarg, NULL);
4881 if (repo_path == NULL)
4882 return got_error_from_errno2("realpath",
4897 if (repo_path == NULL) {
4898 cwd = getcwd(NULL, 0);
4900 return got_error_from_errno("getcwd");
4901 error = got_worktree_open(&worktree, cwd);
4902 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4906 strdup(got_worktree_get_repo_path(worktree));
4908 repo_path = strdup(cwd);
4909 if (repo_path == NULL) {
4910 error = got_error_from_errno("strdup");
4915 error = got_repo_open(&repo, repo_path, NULL);
4919 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4926 error = apply_unveil(got_repo_get_path(repo), NULL);
4930 error = tog_load_refs(repo, 0);
4934 if (commit_id_str == NULL) {
4935 struct got_reference *head_ref;
4936 error = got_ref_open(&head_ref, repo, worktree ?
4937 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4940 error = got_ref_resolve(&commit_id, repo, head_ref);
4941 got_ref_close(head_ref);
4943 error = got_repo_match_object_id(&commit_id, NULL,
4944 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4949 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4951 error = got_error_from_errno("view_open");
4955 error = got_object_open_as_commit(&commit, repo, commit_id);
4959 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4964 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4969 /* Release work tree lock. */
4970 got_worktree_close(worktree);
4973 error = view_loop(view);
4981 got_object_commit_close(commit);
4983 got_worktree_close(worktree);
4985 const struct got_error *close_err = got_repo_close(repo);
4993 static const struct got_error *
4994 draw_tree_entries(struct tog_view *view, const char *parent_path)
4996 struct tog_tree_view_state *s = &view->state.tree;
4997 const struct got_error *err = NULL;
4998 struct got_tree_entry *te;
5000 struct tog_color *tc;
5001 int width, n, i, nentries;
5002 int limit = view->nlines;
5006 werase(view->window);
5011 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5014 if (view_needs_focus_indication(view))
5015 wstandout(view->window);
5016 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5018 wattr_on(view->window,
5019 COLOR_PAIR(tc->colorpair), NULL);
5020 waddwstr(view->window, wline);
5022 wattr_off(view->window,
5023 COLOR_PAIR(tc->colorpair), NULL);
5024 if (view_needs_focus_indication(view))
5025 wstandend(view->window);
5028 if (width < view->ncols - 1)
5029 waddch(view->window, '\n');
5032 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5035 waddwstr(view->window, wline);
5038 if (width < view->ncols - 1)
5039 waddch(view->window, '\n');
5042 waddch(view->window, '\n');
5046 if (s->first_displayed_entry == NULL) {
5047 te = got_object_tree_get_first_entry(s->tree);
5048 if (s->selected == 0) {
5050 wstandout(view->window);
5051 s->selected_entry = NULL;
5053 waddstr(view->window, " ..\n"); /* parent directory */
5054 if (s->selected == 0 && view->focussed)
5055 wstandend(view->window);
5062 te = s->first_displayed_entry;
5065 nentries = got_object_tree_get_nentries(s->tree);
5066 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5067 char *line = NULL, *id_str = NULL, *link_target = NULL;
5068 const char *modestr = "";
5071 te = got_object_tree_get_entry(s->tree, i);
5072 mode = got_tree_entry_get_mode(te);
5075 err = got_object_id_str(&id_str,
5076 got_tree_entry_get_id(te));
5078 return got_error_from_errno(
5079 "got_object_id_str");
5081 if (got_object_tree_entry_is_submodule(te))
5083 else if (S_ISLNK(mode)) {
5086 err = got_tree_entry_get_symlink_target(&link_target,
5092 for (i = 0; i < strlen(link_target); i++) {
5093 if (!isprint((unsigned char)link_target[i]))
5094 link_target[i] = '?';
5098 else if (S_ISDIR(mode))
5100 else if (mode & S_IXUSR)
5102 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5103 got_tree_entry_get_name(te), modestr,
5104 link_target ? " -> ": "",
5105 link_target ? link_target : "") == -1) {
5108 return got_error_from_errno("asprintf");
5112 err = format_line(&wline, &width, line, view->ncols, 0);
5117 if (n == s->selected) {
5119 wstandout(view->window);
5120 s->selected_entry = te;
5122 tc = match_color(&s->colors, line);
5124 wattr_on(view->window,
5125 COLOR_PAIR(tc->colorpair), NULL);
5126 waddwstr(view->window, wline);
5128 wattr_off(view->window,
5129 COLOR_PAIR(tc->colorpair), NULL);
5130 if (width < view->ncols - 1)
5131 waddch(view->window, '\n');
5132 if (n == s->selected && view->focussed)
5133 wstandend(view->window);
5139 s->last_displayed_entry = te;
5148 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5150 struct got_tree_entry *te;
5151 int isroot = s->tree == s->root;
5154 if (s->first_displayed_entry == NULL)
5157 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5158 while (i++ < maxscroll) {
5161 s->first_displayed_entry = NULL;
5164 s->first_displayed_entry = te;
5165 te = got_tree_entry_get_prev(s->tree, te);
5170 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5172 struct got_tree_entry *next, *last;
5175 if (s->first_displayed_entry)
5176 next = got_tree_entry_get_next(s->tree,
5177 s->first_displayed_entry);
5179 next = got_object_tree_get_first_entry(s->tree);
5181 last = s->last_displayed_entry;
5182 while (next && last && n++ < maxscroll) {
5183 last = got_tree_entry_get_next(s->tree, last);
5185 s->first_displayed_entry = next;
5186 next = got_tree_entry_get_next(s->tree, next);
5191 static const struct got_error *
5192 tree_entry_path(char **path, struct tog_parent_trees *parents,
5193 struct got_tree_entry *te)
5195 const struct got_error *err = NULL;
5196 struct tog_parent_tree *pt;
5197 size_t len = 2; /* for leading slash and NUL */
5199 TAILQ_FOREACH(pt, parents, entry)
5200 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5203 len += strlen(got_tree_entry_get_name(te));
5205 *path = calloc(1, len);
5207 return got_error_from_errno("calloc");
5210 pt = TAILQ_LAST(parents, tog_parent_trees);
5212 const char *name = got_tree_entry_get_name(pt->selected_entry);
5213 if (strlcat(*path, name, len) >= len) {
5214 err = got_error(GOT_ERR_NO_SPACE);
5217 if (strlcat(*path, "/", len) >= len) {
5218 err = got_error(GOT_ERR_NO_SPACE);
5221 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5224 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5225 err = got_error(GOT_ERR_NO_SPACE);
5237 static const struct got_error *
5238 blame_tree_entry(struct tog_view **new_view, int begin_x,
5239 struct got_tree_entry *te, struct tog_parent_trees *parents,
5240 struct got_object_id *commit_id, struct got_repository *repo)
5242 const struct got_error *err = NULL;
5244 struct tog_view *blame_view;
5248 err = tree_entry_path(&path, parents, te);
5252 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5253 if (blame_view == NULL) {
5254 err = got_error_from_errno("view_open");
5258 err = open_blame_view(blame_view, path, commit_id, repo);
5260 if (err->code == GOT_ERR_CANCELLED)
5262 view_close(blame_view);
5264 *new_view = blame_view;
5270 static const struct got_error *
5271 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5272 struct tog_tree_view_state *s)
5274 struct tog_view *log_view;
5275 const struct got_error *err = NULL;
5280 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5281 if (log_view == NULL)
5282 return got_error_from_errno("view_open");
5284 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5288 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5291 view_close(log_view);
5293 *new_view = log_view;
5298 static const struct got_error *
5299 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5300 const char *head_ref_name, struct got_repository *repo)
5302 const struct got_error *err = NULL;
5303 char *commit_id_str = NULL;
5304 struct tog_tree_view_state *s = &view->state.tree;
5305 struct got_commit_object *commit = NULL;
5307 TAILQ_INIT(&s->parents);
5308 STAILQ_INIT(&s->colors);
5310 s->commit_id = got_object_id_dup(commit_id);
5311 if (s->commit_id == NULL)
5312 return got_error_from_errno("got_object_id_dup");
5314 err = got_object_open_as_commit(&commit, repo, commit_id);
5319 * The root is opened here and will be closed when the view is closed.
5320 * Any visited subtrees and their path-wise parents are opened and
5323 err = got_object_open_as_tree(&s->root, repo,
5324 got_object_commit_get_tree_id(commit));
5329 err = got_object_id_str(&commit_id_str, commit_id);
5333 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5334 err = got_error_from_errno("asprintf");
5338 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5339 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5340 if (head_ref_name) {
5341 s->head_ref_name = strdup(head_ref_name);
5342 if (s->head_ref_name == NULL) {
5343 err = got_error_from_errno("strdup");
5349 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5350 err = add_color(&s->colors, "\\$$",
5351 TOG_COLOR_TREE_SUBMODULE,
5352 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5355 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5356 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5359 err = add_color(&s->colors, "/$",
5360 TOG_COLOR_TREE_DIRECTORY,
5361 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5365 err = add_color(&s->colors, "\\*$",
5366 TOG_COLOR_TREE_EXECUTABLE,
5367 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5371 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5372 get_color_value("TOG_COLOR_COMMIT"));
5377 view->show = show_tree_view;
5378 view->input = input_tree_view;
5379 view->close = close_tree_view;
5380 view->search_start = search_start_tree_view;
5381 view->search_next = search_next_tree_view;
5383 free(commit_id_str);
5385 got_object_commit_close(commit);
5387 close_tree_view(view);
5391 static const struct got_error *
5392 close_tree_view(struct tog_view *view)
5394 struct tog_tree_view_state *s = &view->state.tree;
5396 free_colors(&s->colors);
5397 free(s->tree_label);
5398 s->tree_label = NULL;
5400 s->commit_id = NULL;
5401 free(s->head_ref_name);
5402 s->head_ref_name = NULL;
5403 while (!TAILQ_EMPTY(&s->parents)) {
5404 struct tog_parent_tree *parent;
5405 parent = TAILQ_FIRST(&s->parents);
5406 TAILQ_REMOVE(&s->parents, parent, entry);
5407 if (parent->tree != s->root)
5408 got_object_tree_close(parent->tree);
5412 if (s->tree != NULL && s->tree != s->root)
5413 got_object_tree_close(s->tree);
5415 got_object_tree_close(s->root);
5419 static const struct got_error *
5420 search_start_tree_view(struct tog_view *view)
5422 struct tog_tree_view_state *s = &view->state.tree;
5424 s->matched_entry = NULL;
5429 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5431 regmatch_t regmatch;
5433 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5437 static const struct got_error *
5438 search_next_tree_view(struct tog_view *view)
5440 struct tog_tree_view_state *s = &view->state.tree;
5441 struct got_tree_entry *te = NULL;
5443 if (!view->searching) {
5444 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5448 if (s->matched_entry) {
5449 if (view->searching == TOG_SEARCH_FORWARD) {
5450 if (s->selected_entry)
5451 te = got_tree_entry_get_next(s->tree,
5454 te = got_object_tree_get_first_entry(s->tree);
5456 if (s->selected_entry == NULL)
5457 te = got_object_tree_get_last_entry(s->tree);
5459 te = got_tree_entry_get_prev(s->tree,
5463 if (s->selected_entry)
5464 te = s->selected_entry;
5465 else if (view->searching == TOG_SEARCH_FORWARD)
5466 te = got_object_tree_get_first_entry(s->tree);
5468 te = got_object_tree_get_last_entry(s->tree);
5473 if (s->matched_entry == NULL) {
5474 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5477 if (view->searching == TOG_SEARCH_FORWARD)
5478 te = got_object_tree_get_first_entry(s->tree);
5480 te = got_object_tree_get_last_entry(s->tree);
5483 if (match_tree_entry(te, &view->regex)) {
5484 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5485 s->matched_entry = te;
5489 if (view->searching == TOG_SEARCH_FORWARD)
5490 te = got_tree_entry_get_next(s->tree, te);
5492 te = got_tree_entry_get_prev(s->tree, te);
5495 if (s->matched_entry) {
5496 s->first_displayed_entry = s->matched_entry;
5503 static const struct got_error *
5504 show_tree_view(struct tog_view *view)
5506 const struct got_error *err = NULL;
5507 struct tog_tree_view_state *s = &view->state.tree;
5510 err = tree_entry_path(&parent_path, &s->parents, NULL);
5514 err = draw_tree_entries(view, parent_path);
5521 static const struct got_error *
5522 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5524 const struct got_error *err = NULL;
5525 struct tog_tree_view_state *s = &view->state.tree;
5526 struct tog_view *log_view, *ref_view;
5527 struct got_tree_entry *te;
5532 s->show_ids = !s->show_ids;
5535 if (!s->selected_entry)
5537 if (view_is_parent_view(view))
5538 begin_x = view_split_begin_x(view->begin_x);
5539 err = log_selected_tree_entry(&log_view, begin_x, s);
5541 log_view->focussed = 1;
5542 if (view_is_parent_view(view)) {
5543 err = view_close_child(view);
5546 view_set_child(view, log_view);
5547 view->focus_child = 1;
5549 *new_view = log_view;
5552 if (view_is_parent_view(view))
5553 begin_x = view_split_begin_x(view->begin_x);
5554 ref_view = view_open(view->nlines, view->ncols,
5555 view->begin_y, begin_x, TOG_VIEW_REF);
5556 if (ref_view == NULL)
5557 return got_error_from_errno("view_open");
5558 err = open_ref_view(ref_view, s->repo);
5560 view_close(ref_view);
5564 ref_view->focussed = 1;
5565 if (view_is_parent_view(view)) {
5566 err = view_close_child(view);
5569 view_set_child(view, ref_view);
5570 view->focus_child = 1;
5572 *new_view = ref_view;
5577 if (s->tree == s->root)
5578 s->first_displayed_entry =
5579 got_object_tree_get_first_entry(s->tree);
5581 s->first_displayed_entry = NULL;
5586 te = got_object_tree_get_last_entry(s->tree);
5587 for (n = 0; n < view->nlines - 3; n++) {
5589 if(s->tree != s->root) {
5590 s->first_displayed_entry = NULL;
5595 s->first_displayed_entry = te;
5596 te = got_tree_entry_get_prev(s->tree, te);
5599 s->selected = n - 1;
5604 if (s->selected > 0) {
5608 tree_scroll_up(s, 1);
5612 if (s->tree == s->root) {
5613 if (got_object_tree_get_first_entry(s->tree) ==
5614 s->first_displayed_entry)
5617 if (s->first_displayed_entry == NULL)
5620 tree_scroll_up(s, MAX(0, view->nlines - 3));
5625 if (s->selected < s->ndisplayed - 1) {
5629 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5631 /* can't scroll any further */
5633 tree_scroll_down(s, 1);
5637 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5639 /* can't scroll any further; move cursor down */
5640 if (s->selected < s->ndisplayed - 1)
5641 s->selected = s->ndisplayed - 1;
5644 tree_scroll_down(s, view->nlines - 3);
5649 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5650 struct tog_parent_tree *parent;
5651 /* user selected '..' */
5652 if (s->tree == s->root)
5654 parent = TAILQ_FIRST(&s->parents);
5655 TAILQ_REMOVE(&s->parents, parent,
5657 got_object_tree_close(s->tree);
5658 s->tree = parent->tree;
5659 s->first_displayed_entry =
5660 parent->first_displayed_entry;
5662 parent->selected_entry;
5663 s->selected = parent->selected;
5665 } else if (S_ISDIR(got_tree_entry_get_mode(
5666 s->selected_entry))) {
5667 struct got_tree_object *subtree;
5668 err = got_object_open_as_tree(&subtree, s->repo,
5669 got_tree_entry_get_id(s->selected_entry));
5672 err = tree_view_visit_subtree(s, subtree);
5674 got_object_tree_close(subtree);
5677 } else if (S_ISREG(got_tree_entry_get_mode(
5678 s->selected_entry))) {
5679 struct tog_view *blame_view;
5680 int begin_x = view_is_parent_view(view) ?
5681 view_split_begin_x(view->begin_x) : 0;
5683 err = blame_tree_entry(&blame_view, begin_x,
5684 s->selected_entry, &s->parents,
5685 s->commit_id, s->repo);
5689 blame_view->focussed = 1;
5690 if (view_is_parent_view(view)) {
5691 err = view_close_child(view);
5694 view_set_child(view, blame_view);
5695 view->focus_child = 1;
5697 *new_view = blame_view;
5701 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5702 s->selected = view->nlines - 4;
5715 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5720 static const struct got_error *
5721 cmd_tree(int argc, char *argv[])
5723 const struct got_error *error;
5724 struct got_repository *repo = NULL;
5725 struct got_worktree *worktree = NULL;
5726 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5727 struct got_object_id *commit_id = NULL;
5728 struct got_commit_object *commit = NULL;
5729 const char *commit_id_arg = NULL;
5731 struct got_reference *ref = NULL;
5732 const char *head_ref_name = NULL;
5734 struct tog_view *view;
5736 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5739 commit_id_arg = optarg;
5742 repo_path = realpath(optarg, NULL);
5743 if (repo_path == NULL)
5744 return got_error_from_errno2("realpath",
5759 if (repo_path == NULL) {
5760 cwd = getcwd(NULL, 0);
5762 return got_error_from_errno("getcwd");
5763 error = got_worktree_open(&worktree, cwd);
5764 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5768 strdup(got_worktree_get_repo_path(worktree));
5770 repo_path = strdup(cwd);
5771 if (repo_path == NULL) {
5772 error = got_error_from_errno("strdup");
5777 error = got_repo_open(&repo, repo_path, NULL);
5781 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5788 error = apply_unveil(got_repo_get_path(repo), NULL);
5792 error = tog_load_refs(repo, 0);
5796 if (commit_id_arg == NULL) {
5797 error = got_repo_match_object_id(&commit_id, &label,
5798 worktree ? got_worktree_get_head_ref_name(worktree) :
5799 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5802 head_ref_name = label;
5804 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5806 head_ref_name = got_ref_get_name(ref);
5807 else if (error->code != GOT_ERR_NOT_REF)
5809 error = got_repo_match_object_id(&commit_id, NULL,
5810 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5815 error = got_object_open_as_commit(&commit, repo, commit_id);
5819 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5821 error = got_error_from_errno("view_open");
5824 error = open_tree_view(view, commit_id, head_ref_name, repo);
5827 if (!got_path_is_root_dir(in_repo_path)) {
5828 error = tree_view_walk_path(&view->state.tree, commit,
5835 /* Release work tree lock. */
5836 got_worktree_close(worktree);
5839 error = view_loop(view);
5848 const struct got_error *close_err = got_repo_close(repo);
5856 static const struct got_error *
5857 ref_view_load_refs(struct tog_ref_view_state *s)
5859 struct got_reflist_entry *sre;
5860 struct tog_reflist_entry *re;
5863 TAILQ_FOREACH(sre, &tog_refs, entry) {
5864 if (strncmp(got_ref_get_name(sre->ref),
5865 "refs/got/", 9) == 0 &&
5866 strncmp(got_ref_get_name(sre->ref),
5867 "refs/got/backup/", 16) != 0)
5870 re = malloc(sizeof(*re));
5872 return got_error_from_errno("malloc");
5874 re->ref = got_ref_dup(sre->ref);
5875 if (re->ref == NULL)
5876 return got_error_from_errno("got_ref_dup");
5877 re->idx = s->nrefs++;
5878 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5881 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5886 ref_view_free_refs(struct tog_ref_view_state *s)
5888 struct tog_reflist_entry *re;
5890 while (!TAILQ_EMPTY(&s->refs)) {
5891 re = TAILQ_FIRST(&s->refs);
5892 TAILQ_REMOVE(&s->refs, re, entry);
5893 got_ref_close(re->ref);
5898 static const struct got_error *
5899 open_ref_view(struct tog_view *view, struct got_repository *repo)
5901 const struct got_error *err = NULL;
5902 struct tog_ref_view_state *s = &view->state.ref;
5904 s->selected_entry = 0;
5907 TAILQ_INIT(&s->refs);
5908 STAILQ_INIT(&s->colors);
5910 err = ref_view_load_refs(s);
5914 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5915 err = add_color(&s->colors, "^refs/heads/",
5916 TOG_COLOR_REFS_HEADS,
5917 get_color_value("TOG_COLOR_REFS_HEADS"));
5921 err = add_color(&s->colors, "^refs/tags/",
5922 TOG_COLOR_REFS_TAGS,
5923 get_color_value("TOG_COLOR_REFS_TAGS"));
5927 err = add_color(&s->colors, "^refs/remotes/",
5928 TOG_COLOR_REFS_REMOTES,
5929 get_color_value("TOG_COLOR_REFS_REMOTES"));
5933 err = add_color(&s->colors, "^refs/got/backup/",
5934 TOG_COLOR_REFS_BACKUP,
5935 get_color_value("TOG_COLOR_REFS_BACKUP"));
5940 view->show = show_ref_view;
5941 view->input = input_ref_view;
5942 view->close = close_ref_view;
5943 view->search_start = search_start_ref_view;
5944 view->search_next = search_next_ref_view;
5947 free_colors(&s->colors);
5951 static const struct got_error *
5952 close_ref_view(struct tog_view *view)
5954 struct tog_ref_view_state *s = &view->state.ref;
5956 ref_view_free_refs(s);
5957 free_colors(&s->colors);
5962 static const struct got_error *
5963 resolve_reflist_entry(struct got_object_id **commit_id,
5964 struct tog_reflist_entry *re, struct got_repository *repo)
5966 const struct got_error *err = NULL;
5967 struct got_object_id *obj_id;
5968 struct got_tag_object *tag = NULL;
5973 err = got_ref_resolve(&obj_id, repo, re->ref);
5977 err = got_object_get_type(&obj_type, repo, obj_id);
5982 case GOT_OBJ_TYPE_COMMIT:
5983 *commit_id = obj_id;
5985 case GOT_OBJ_TYPE_TAG:
5986 err = got_object_open_as_tag(&tag, repo, obj_id);
5990 err = got_object_get_type(&obj_type, repo,
5991 got_object_tag_get_object_id(tag));
5994 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5995 err = got_error(GOT_ERR_OBJ_TYPE);
5998 *commit_id = got_object_id_dup(
5999 got_object_tag_get_object_id(tag));
6000 if (*commit_id == NULL) {
6001 err = got_error_from_errno("got_object_id_dup");
6006 err = got_error(GOT_ERR_OBJ_TYPE);
6012 got_object_tag_close(tag);
6020 static const struct got_error *
6021 log_ref_entry(struct tog_view **new_view, int begin_x,
6022 struct tog_reflist_entry *re, struct got_repository *repo)
6024 struct tog_view *log_view;
6025 const struct got_error *err = NULL;
6026 struct got_object_id *commit_id = NULL;
6030 err = resolve_reflist_entry(&commit_id, re, repo);
6032 if (err->code != GOT_ERR_OBJ_TYPE)
6038 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6039 if (log_view == NULL) {
6040 err = got_error_from_errno("view_open");
6044 err = open_log_view(log_view, commit_id, repo,
6045 got_ref_get_name(re->ref), "", 0);
6048 view_close(log_view);
6050 *new_view = log_view;
6056 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6058 struct tog_reflist_entry *re;
6061 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6064 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6065 while (i++ < maxscroll) {
6068 s->first_displayed_entry = re;
6069 re = TAILQ_PREV(re, tog_reflist_head, entry);
6074 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6076 struct tog_reflist_entry *next, *last;
6079 if (s->first_displayed_entry)
6080 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6082 next = TAILQ_FIRST(&s->refs);
6084 last = s->last_displayed_entry;
6085 while (next && last && n++ < maxscroll) {
6086 last = TAILQ_NEXT(last, entry);
6088 s->first_displayed_entry = next;
6089 next = TAILQ_NEXT(next, entry);
6094 static const struct got_error *
6095 search_start_ref_view(struct tog_view *view)
6097 struct tog_ref_view_state *s = &view->state.ref;
6099 s->matched_entry = NULL;
6104 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6106 regmatch_t regmatch;
6108 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6112 static const struct got_error *
6113 search_next_ref_view(struct tog_view *view)
6115 struct tog_ref_view_state *s = &view->state.ref;
6116 struct tog_reflist_entry *re = NULL;
6118 if (!view->searching) {
6119 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6123 if (s->matched_entry) {
6124 if (view->searching == TOG_SEARCH_FORWARD) {
6125 if (s->selected_entry)
6126 re = TAILQ_NEXT(s->selected_entry, entry);
6128 re = TAILQ_PREV(s->selected_entry,
6129 tog_reflist_head, entry);
6131 if (s->selected_entry == NULL)
6132 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6134 re = TAILQ_PREV(s->selected_entry,
6135 tog_reflist_head, entry);
6138 if (s->selected_entry)
6139 re = s->selected_entry;
6140 else if (view->searching == TOG_SEARCH_FORWARD)
6141 re = TAILQ_FIRST(&s->refs);
6143 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6148 if (s->matched_entry == NULL) {
6149 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6152 if (view->searching == TOG_SEARCH_FORWARD)
6153 re = TAILQ_FIRST(&s->refs);
6155 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6158 if (match_reflist_entry(re, &view->regex)) {
6159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6160 s->matched_entry = re;
6164 if (view->searching == TOG_SEARCH_FORWARD)
6165 re = TAILQ_NEXT(re, entry);
6167 re = TAILQ_PREV(re, tog_reflist_head, entry);
6170 if (s->matched_entry) {
6171 s->first_displayed_entry = s->matched_entry;
6178 static const struct got_error *
6179 show_ref_view(struct tog_view *view)
6181 const struct got_error *err = NULL;
6182 struct tog_ref_view_state *s = &view->state.ref;
6183 struct tog_reflist_entry *re;
6186 struct tog_color *tc;
6188 int limit = view->nlines;
6190 werase(view->window);
6197 re = s->first_displayed_entry;
6199 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6201 return got_error_from_errno("asprintf");
6203 err = format_line(&wline, &width, line, view->ncols, 0);
6208 if (view_needs_focus_indication(view))
6209 wstandout(view->window);
6210 waddwstr(view->window, wline);
6211 if (view_needs_focus_indication(view))
6212 wstandend(view->window);
6217 if (width < view->ncols - 1)
6218 waddch(view->window, '\n');
6223 while (re && limit > 0) {
6226 if (got_ref_is_symbolic(re->ref)) {
6227 if (asprintf(&line, "%s -> %s",
6228 got_ref_get_name(re->ref),
6229 got_ref_get_symref_target(re->ref)) == -1)
6230 return got_error_from_errno("asprintf");
6231 } else if (s->show_ids) {
6232 struct got_object_id *id;
6234 err = got_ref_resolve(&id, s->repo, re->ref);
6237 err = got_object_id_str(&id_str, id);
6242 if (asprintf(&line, "%s: %s",
6243 got_ref_get_name(re->ref), id_str) == -1) {
6244 err = got_error_from_errno("asprintf");
6252 line = strdup(got_ref_get_name(re->ref));
6254 return got_error_from_errno("strdup");
6257 err = format_line(&wline, &width, line, view->ncols, 0);
6262 if (n == s->selected) {
6264 wstandout(view->window);
6265 s->selected_entry = re;
6267 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6269 wattr_on(view->window,
6270 COLOR_PAIR(tc->colorpair), NULL);
6271 waddwstr(view->window, wline);
6273 wattr_off(view->window,
6274 COLOR_PAIR(tc->colorpair), NULL);
6275 if (width < view->ncols - 1)
6276 waddch(view->window, '\n');
6277 if (n == s->selected && view->focussed)
6278 wstandend(view->window);
6284 s->last_displayed_entry = re;
6287 re = TAILQ_NEXT(re, entry);
6294 static const struct got_error *
6295 browse_ref_tree(struct tog_view **new_view, int begin_x,
6296 struct tog_reflist_entry *re, struct got_repository *repo)
6298 const struct got_error *err = NULL;
6299 struct got_object_id *commit_id = NULL;
6300 struct tog_view *tree_view;
6304 err = resolve_reflist_entry(&commit_id, re, repo);
6306 if (err->code != GOT_ERR_OBJ_TYPE)
6313 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6314 if (tree_view == NULL) {
6315 err = got_error_from_errno("view_open");
6319 err = open_tree_view(tree_view, commit_id,
6320 got_ref_get_name(re->ref), repo);
6324 *new_view = tree_view;
6329 static const struct got_error *
6330 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6332 const struct got_error *err = NULL;
6333 struct tog_ref_view_state *s = &view->state.ref;
6334 struct tog_view *log_view, *tree_view;
6335 struct tog_reflist_entry *re;
6340 s->show_ids = !s->show_ids;
6343 s->sort_by_date = !s->sort_by_date;
6344 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6345 got_ref_cmp_by_commit_timestamp_descending :
6346 tog_ref_cmp_by_name, s->repo);
6349 got_reflist_object_id_map_free(tog_refs_idmap);
6350 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6351 &tog_refs, s->repo);
6354 ref_view_free_refs(s);
6355 err = ref_view_load_refs(s);
6359 if (!s->selected_entry)
6361 if (view_is_parent_view(view))
6362 begin_x = view_split_begin_x(view->begin_x);
6363 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6366 log_view->focussed = 1;
6367 if (view_is_parent_view(view)) {
6368 err = view_close_child(view);
6371 view_set_child(view, log_view);
6372 view->focus_child = 1;
6374 *new_view = log_view;
6377 if (!s->selected_entry)
6379 if (view_is_parent_view(view))
6380 begin_x = view_split_begin_x(view->begin_x);
6381 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6383 if (err || tree_view == NULL)
6386 tree_view->focussed = 1;
6387 if (view_is_parent_view(view)) {
6388 err = view_close_child(view);
6391 view_set_child(view, tree_view);
6392 view->focus_child = 1;
6394 *new_view = tree_view;
6399 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6404 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6405 for (n = 0; n < view->nlines - 1; n++) {
6408 s->first_displayed_entry = re;
6409 re = TAILQ_PREV(re, tog_reflist_head, entry);
6412 s->selected = n - 1;
6417 if (s->selected > 0) {
6421 ref_scroll_up(s, 1);
6425 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6427 ref_scroll_up(s, MAX(0, view->nlines - 1));
6432 if (s->selected < s->ndisplayed - 1) {
6436 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6437 /* can't scroll any further */
6439 ref_scroll_down(s, 1);
6443 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6444 /* can't scroll any further; move cursor down */
6445 if (s->selected < s->ndisplayed - 1)
6446 s->selected = s->ndisplayed - 1;
6449 ref_scroll_down(s, view->nlines - 1);
6453 err = tog_load_refs(s->repo, s->sort_by_date);
6456 ref_view_free_refs(s);
6457 err = ref_view_load_refs(s);
6460 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6461 s->selected = view->nlines - 2;
6474 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6479 static const struct got_error *
6480 cmd_ref(int argc, char *argv[])
6482 const struct got_error *error;
6483 struct got_repository *repo = NULL;
6484 struct got_worktree *worktree = NULL;
6485 char *cwd = NULL, *repo_path = NULL;
6487 struct tog_view *view;
6489 while ((ch = getopt(argc, argv, "r:")) != -1) {
6492 repo_path = realpath(optarg, NULL);
6493 if (repo_path == NULL)
6494 return got_error_from_errno2("realpath",
6509 if (repo_path == NULL) {
6510 cwd = getcwd(NULL, 0);
6512 return got_error_from_errno("getcwd");
6513 error = got_worktree_open(&worktree, cwd);
6514 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6518 strdup(got_worktree_get_repo_path(worktree));
6520 repo_path = strdup(cwd);
6521 if (repo_path == NULL) {
6522 error = got_error_from_errno("strdup");
6527 error = got_repo_open(&repo, repo_path, NULL);
6533 error = apply_unveil(got_repo_get_path(repo), NULL);
6537 error = tog_load_refs(repo, 0);
6541 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6543 error = got_error_from_errno("view_open");
6547 error = open_ref_view(view, repo);
6552 /* Release work tree lock. */
6553 got_worktree_close(worktree);
6556 error = view_loop(view);
6561 const struct got_error *close_err = got_repo_close(repo);
6570 list_commands(FILE *fp)
6574 fprintf(fp, "commands:");
6575 for (i = 0; i < nitems(tog_commands); i++) {
6576 const struct tog_cmd *cmd = &tog_commands[i];
6577 fprintf(fp, " %s", cmd->name);
6583 usage(int hflag, int status)
6585 FILE *fp = (status == 0) ? stdout : stderr;
6587 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6590 fprintf(fp, "lazy usage: %s path\n", getprogname());
6597 make_argv(int argc, ...)
6605 argv = calloc(argc, sizeof(char *));
6608 for (i = 0; i < argc; i++) {
6609 argv[i] = strdup(va_arg(ap, char *));
6610 if (argv[i] == NULL)
6619 * Try to convert 'tog path' into a 'tog log path' command.
6620 * The user could simply have mistyped the command rather than knowingly
6621 * provided a path. So check whether argv[0] can in fact be resolved
6622 * to a path in the HEAD commit and print a special error if not.
6623 * This hack is for mpi@ <3
6625 static const struct got_error *
6626 tog_log_with_path(int argc, char *argv[])
6628 const struct got_error *error = NULL, *close_err;
6629 const struct tog_cmd *cmd = NULL;
6630 struct got_repository *repo = NULL;
6631 struct got_worktree *worktree = NULL;
6632 struct got_object_id *commit_id = NULL, *id = NULL;
6633 struct got_commit_object *commit = NULL;
6634 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6635 char *commit_id_str = NULL, **cmd_argv = NULL;
6637 cwd = getcwd(NULL, 0);
6639 return got_error_from_errno("getcwd");
6641 error = got_worktree_open(&worktree, cwd);
6642 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6646 repo_path = strdup(got_worktree_get_repo_path(worktree));
6648 repo_path = strdup(cwd);
6649 if (repo_path == NULL) {
6650 error = got_error_from_errno("strdup");
6654 error = got_repo_open(&repo, repo_path, NULL);
6658 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6663 error = tog_load_refs(repo, 0);
6666 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6667 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6668 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6673 got_worktree_close(worktree);
6677 error = got_object_open_as_commit(&commit, repo, commit_id);
6681 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6683 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6685 fprintf(stderr, "%s: '%s' is no known command or path\n",
6686 getprogname(), argv[0]);
6691 close_err = got_repo_close(repo);
6696 error = got_object_id_str(&commit_id_str, commit_id);
6700 cmd = &tog_commands[0]; /* log */
6702 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6703 error = cmd->cmd_main(argc, cmd_argv);
6706 close_err = got_repo_close(repo);
6711 got_object_commit_close(commit);
6713 got_worktree_close(worktree);
6715 free(commit_id_str);
6722 for (i = 0; i < argc; i++)
6731 main(int argc, char *argv[])
6733 const struct got_error *error = NULL;
6734 const struct tog_cmd *cmd = NULL;
6735 int ch, hflag = 0, Vflag = 0;
6736 char **cmd_argv = NULL;
6737 static const struct option longopts[] = {
6738 { "version", no_argument, NULL, 'V' },
6742 setlocale(LC_CTYPE, "");
6744 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6764 got_version_print_str();
6769 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6777 /* Build an argument vector which runs a default command. */
6778 cmd = &tog_commands[0];
6780 cmd_argv = make_argv(argc, cmd->name);
6784 /* Did the user specify a command? */
6785 for (i = 0; i < nitems(tog_commands); i++) {
6786 if (strncmp(tog_commands[i].name, argv[0],
6787 strlen(argv[0])) == 0) {
6788 cmd = &tog_commands[i];
6797 /* No command specified; try log with a path */
6798 error = tog_log_with_path(argc, argv);
6803 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6810 for (i = 0; i < argc; i++)
6815 if (error && error->code != GOT_ERR_CANCELLED)
6816 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);