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>
41 #include "got_compat.h"
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
58 //#define update_panels() (0)
59 //#define doupdate() (0)
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #define CTRL(x) ((x) & 0x1f)
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
112 #define TOG_EOF_STRING "(END)"
114 struct commit_queue_entry {
115 TAILQ_ENTRY(commit_queue_entry) entry;
116 struct got_object_id *id;
117 struct got_commit_object *commit;
120 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
121 struct commit_queue {
123 struct commit_queue_head head;
127 STAILQ_ENTRY(tog_color) entry;
131 STAILQ_HEAD(tog_colors, tog_color);
133 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
134 static struct got_reflist_object_id_map *tog_refs_idmap;
136 static const struct got_error *
137 tog_load_refs(struct got_repository *repo)
139 const struct got_error *err;
141 err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
145 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
152 if (tog_refs_idmap) {
153 got_reflist_object_id_map_free(tog_refs_idmap);
154 tog_refs_idmap = NULL;
156 got_ref_list_free(&tog_refs);
159 static const struct got_error *
160 add_color(struct tog_colors *colors, const char *pattern,
161 int idx, short color)
163 const struct got_error *err = NULL;
164 struct tog_color *tc;
167 if (idx < 1 || idx > COLOR_PAIRS - 1)
170 init_pair(idx, color, -1);
172 tc = calloc(1, sizeof(*tc));
174 return got_error_from_errno("calloc");
175 regerr = regcomp(&tc->regex, pattern,
176 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
178 static char regerr_msg[512];
179 static char err_msg[512];
180 regerror(regerr, &tc->regex, regerr_msg,
182 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
184 err = got_error_msg(GOT_ERR_REGEX, err_msg);
189 STAILQ_INSERT_HEAD(colors, tc, entry);
194 free_colors(struct tog_colors *colors)
196 struct tog_color *tc;
198 while (!STAILQ_EMPTY(colors)) {
199 tc = STAILQ_FIRST(colors);
200 STAILQ_REMOVE_HEAD(colors, entry);
207 get_color(struct tog_colors *colors, int colorpair)
209 struct tog_color *tc = NULL;
211 STAILQ_FOREACH(tc, colors, entry) {
212 if (tc->colorpair == colorpair)
220 default_color_value(const char *envvar)
222 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
223 return COLOR_MAGENTA;
224 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
226 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
228 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
230 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
231 return COLOR_MAGENTA;
232 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
233 return COLOR_MAGENTA;
234 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
236 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
238 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
240 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
242 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
244 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
246 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
247 return COLOR_MAGENTA;
248 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
255 get_color_value(const char *envvar)
257 const char *val = getenv(envvar);
260 return default_color_value(envvar);
262 if (strcasecmp(val, "black") == 0)
264 if (strcasecmp(val, "red") == 0)
266 if (strcasecmp(val, "green") == 0)
268 if (strcasecmp(val, "yellow") == 0)
270 if (strcasecmp(val, "blue") == 0)
272 if (strcasecmp(val, "magenta") == 0)
273 return COLOR_MAGENTA;
274 if (strcasecmp(val, "cyan") == 0)
276 if (strcasecmp(val, "white") == 0)
278 if (strcasecmp(val, "default") == 0)
281 return default_color_value(envvar);
285 struct tog_diff_view_state {
286 struct got_object_id *id1, *id2;
287 const char *label1, *label2;
289 int first_displayed_line;
290 int last_displayed_line;
293 int ignore_whitespace;
295 struct got_repository *repo;
296 struct tog_colors colors;
302 /* passed from log view; may be NULL */
303 struct tog_view *log_view;
306 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
308 struct tog_log_thread_args {
309 pthread_cond_t need_commits;
310 pthread_cond_t commit_loaded;
313 struct got_commit_graph *graph;
314 struct commit_queue *commits;
315 const char *in_repo_path;
316 struct got_object_id *start_id;
317 struct got_repository *repo;
320 struct commit_queue_entry **first_displayed_entry;
321 struct commit_queue_entry **selected_entry;
323 int *search_next_done;
327 struct tog_log_view_state {
328 struct commit_queue commits;
329 struct commit_queue_entry *first_displayed_entry;
330 struct commit_queue_entry *last_displayed_entry;
331 struct commit_queue_entry *selected_entry;
336 struct got_repository *repo;
337 struct got_object_id *start_id;
340 struct tog_log_thread_args thread_args;
341 struct commit_queue_entry *matched_entry;
342 struct commit_queue_entry *search_entry;
343 struct tog_colors colors;
346 #define TOG_COLOR_DIFF_MINUS 1
347 #define TOG_COLOR_DIFF_PLUS 2
348 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
349 #define TOG_COLOR_DIFF_META 4
350 #define TOG_COLOR_TREE_SUBMODULE 5
351 #define TOG_COLOR_TREE_SYMLINK 6
352 #define TOG_COLOR_TREE_DIRECTORY 7
353 #define TOG_COLOR_TREE_EXECUTABLE 8
354 #define TOG_COLOR_COMMIT 9
355 #define TOG_COLOR_AUTHOR 10
356 #define TOG_COLOR_DATE 11
357 #define TOG_COLOR_REFS_HEADS 12
358 #define TOG_COLOR_REFS_TAGS 13
359 #define TOG_COLOR_REFS_REMOTES 14
361 struct tog_blame_cb_args {
362 struct tog_blame_line *lines; /* one per line */
365 struct tog_view *view;
366 struct got_object_id *commit_id;
370 struct tog_blame_thread_args {
372 struct got_repository *repo;
373 struct tog_blame_cb_args *cb_args;
375 got_cancel_cb cancel_cb;
382 struct tog_blame_line *lines;
386 struct tog_blame_thread_args thread_args;
387 struct tog_blame_cb_args cb_args;
391 struct tog_blame_view_state {
392 int first_displayed_line;
393 int last_displayed_line;
398 struct got_object_id_queue blamed_commits;
399 struct got_object_qid *blamed_commit;
401 struct got_repository *repo;
402 struct got_object_id *commit_id;
403 struct tog_blame blame;
405 struct tog_colors colors;
408 struct tog_parent_tree {
409 TAILQ_ENTRY(tog_parent_tree) entry;
410 struct got_tree_object *tree;
411 struct got_tree_entry *first_displayed_entry;
412 struct got_tree_entry *selected_entry;
416 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
418 struct tog_tree_view_state {
420 struct got_object_id *commit_id;/* commit which this tree belongs to */
421 struct got_tree_object *root; /* the commit's root tree entry */
422 struct got_tree_object *tree; /* currently displayed (sub-)tree */
423 struct got_tree_entry *first_displayed_entry;
424 struct got_tree_entry *last_displayed_entry;
425 struct got_tree_entry *selected_entry;
426 int ndisplayed, selected, show_ids;
427 struct tog_parent_trees parents; /* parent trees of current sub-tree */
429 struct got_repository *repo;
430 struct got_tree_entry *matched_entry;
431 struct tog_colors colors;
434 struct tog_reflist_entry {
435 TAILQ_ENTRY(tog_reflist_entry) entry;
436 struct got_reference *ref;
440 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
442 struct tog_ref_view_state {
443 struct tog_reflist_head refs;
444 struct tog_reflist_entry *first_displayed_entry;
445 struct tog_reflist_entry *last_displayed_entry;
446 struct tog_reflist_entry *selected_entry;
447 int nrefs, ndisplayed, selected, show_ids;
448 struct got_repository *repo;
449 struct tog_reflist_entry *matched_entry;
450 struct tog_colors colors;
454 * We implement two types of views: parent views and child views.
456 * The 'Tab' key switches focus between a parent view and its child view.
457 * Child views are shown side-by-side to their parent view, provided
458 * there is enough screen estate.
460 * When a new view is opened from within a parent view, this new view
461 * becomes a child view of the parent view, replacing any existing child.
463 * When a new view is opened from within a child view, this new view
464 * becomes a parent view which will obscure the views below until the
465 * user quits the new parent view by typing 'q'.
467 * This list of views contains parent views only.
468 * Child views are only pointed to by their parent view.
470 TAILQ_HEAD(tog_view_list_head, tog_view);
473 TAILQ_ENTRY(tog_view) entry;
476 int nlines, ncols, begin_y, begin_x;
477 int lines, cols; /* copies of LINES and COLS */
478 int focussed; /* Only set on one parent or child view at a time. */
480 struct tog_view *parent;
481 struct tog_view *child;
484 * This flag is initially set on parent views when a new child view
485 * is created. It gets toggled when the 'Tab' key switches focus
486 * between parent and child.
487 * The flag indicates whether focus should be passed on to our child
488 * view if this parent view gets picked for focus after another parent
489 * view was closed. This prevents child views from losing focus in such
494 /* type-specific state */
495 enum tog_view_type type;
497 struct tog_diff_view_state diff;
498 struct tog_log_view_state log;
499 struct tog_blame_view_state blame;
500 struct tog_tree_view_state tree;
501 struct tog_ref_view_state ref;
504 const struct got_error *(*show)(struct tog_view *);
505 const struct got_error *(*input)(struct tog_view **,
506 struct tog_view *, int);
507 const struct got_error *(*close)(struct tog_view *);
509 const struct got_error *(*search_start)(struct tog_view *);
510 const struct got_error *(*search_next)(struct tog_view *);
513 #define TOG_SEARCH_FORWARD 1
514 #define TOG_SEARCH_BACKWARD 2
515 int search_next_done;
516 #define TOG_SEARCH_HAVE_MORE 1
517 #define TOG_SEARCH_NO_MORE 2
518 #define TOG_SEARCH_HAVE_NONE 3
523 static const struct got_error *open_diff_view(struct tog_view *,
524 struct got_object_id *, struct got_object_id *,
525 const char *, const char *, int, int, int, struct tog_view *,
526 struct got_repository *);
527 static const struct got_error *show_diff_view(struct tog_view *);
528 static const struct got_error *input_diff_view(struct tog_view **,
529 struct tog_view *, int);
530 static const struct got_error* close_diff_view(struct tog_view *);
531 static const struct got_error *search_start_diff_view(struct tog_view *);
532 static const struct got_error *search_next_diff_view(struct tog_view *);
534 static const struct got_error *open_log_view(struct tog_view *,
535 struct got_object_id *, struct got_repository *,
536 const char *, const char *, int);
537 static const struct got_error * show_log_view(struct tog_view *);
538 static const struct got_error *input_log_view(struct tog_view **,
539 struct tog_view *, int);
540 static const struct got_error *close_log_view(struct tog_view *);
541 static const struct got_error *search_start_log_view(struct tog_view *);
542 static const struct got_error *search_next_log_view(struct tog_view *);
544 static const struct got_error *open_blame_view(struct tog_view *, char *,
545 struct got_object_id *, struct got_repository *);
546 static const struct got_error *show_blame_view(struct tog_view *);
547 static const struct got_error *input_blame_view(struct tog_view **,
548 struct tog_view *, int);
549 static const struct got_error *close_blame_view(struct tog_view *);
550 static const struct got_error *search_start_blame_view(struct tog_view *);
551 static const struct got_error *search_next_blame_view(struct tog_view *);
553 static const struct got_error *open_tree_view(struct tog_view *,
554 struct got_object_id *, const char *, struct got_repository *);
555 static const struct got_error *show_tree_view(struct tog_view *);
556 static const struct got_error *input_tree_view(struct tog_view **,
557 struct tog_view *, int);
558 static const struct got_error *close_tree_view(struct tog_view *);
559 static const struct got_error *search_start_tree_view(struct tog_view *);
560 static const struct got_error *search_next_tree_view(struct tog_view *);
562 static const struct got_error *open_ref_view(struct tog_view *,
563 struct got_repository *);
564 static const struct got_error *show_ref_view(struct tog_view *);
565 static const struct got_error *input_ref_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_ref_view(struct tog_view *);
568 static const struct got_error *search_start_ref_view(struct tog_view *);
569 static const struct got_error *search_next_ref_view(struct tog_view *);
571 static volatile sig_atomic_t tog_sigwinch_received;
572 static volatile sig_atomic_t tog_sigpipe_received;
573 static volatile sig_atomic_t tog_sigcont_received;
576 tog_sigwinch(int signo)
578 tog_sigwinch_received = 1;
582 tog_sigpipe(int signo)
584 tog_sigpipe_received = 1;
588 tog_sigcont(int signo)
590 tog_sigcont_received = 1;
593 static const struct got_error *
594 view_close(struct tog_view *view)
596 const struct got_error *err = NULL;
599 view_close(view->child);
603 err = view->close(view);
605 del_panel(view->panel);
607 delwin(view->window);
612 static struct tog_view *
613 view_open(int nlines, int ncols, int begin_y, int begin_x,
614 enum tog_view_type type)
616 struct tog_view *view = calloc(1, sizeof(*view));
624 view->nlines = nlines ? nlines : LINES - begin_y;
625 view->ncols = ncols ? ncols : COLS - begin_x;
626 view->begin_y = begin_y;
627 view->begin_x = begin_x;
628 view->window = newwin(nlines, ncols, begin_y, begin_x);
629 if (view->window == NULL) {
633 view->panel = new_panel(view->window);
634 if (view->panel == NULL ||
635 set_panel_userptr(view->panel, view) != OK) {
640 keypad(view->window, TRUE);
645 view_split_begin_x(int begin_x)
647 if (begin_x > 0 || COLS < 120)
649 return (COLS - MAX(COLS / 2, 80));
652 static const struct got_error *view_resize(struct tog_view *);
654 static const struct got_error *
655 view_splitscreen(struct tog_view *view)
657 const struct got_error *err = NULL;
660 view->begin_x = view_split_begin_x(0);
661 view->nlines = LINES;
662 view->ncols = COLS - view->begin_x;
665 err = view_resize(view);
669 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
670 return got_error_from_errno("mvwin");
675 static const struct got_error *
676 view_fullscreen(struct tog_view *view)
678 const struct got_error *err = NULL;
682 view->nlines = LINES;
686 err = view_resize(view);
690 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
691 return got_error_from_errno("mvwin");
697 view_is_parent_view(struct tog_view *view)
699 return view->parent == NULL;
702 static const struct got_error *
703 view_resize(struct tog_view *view)
707 if (view->lines > LINES)
708 nlines = view->nlines - (view->lines - LINES);
710 nlines = view->nlines + (LINES - view->lines);
712 if (view->cols > COLS)
713 ncols = view->ncols - (view->cols - COLS);
715 ncols = view->ncols + (COLS - view->cols);
717 if (wresize(view->window, nlines, ncols) == ERR)
718 return got_error_from_errno("wresize");
719 if (replace_panel(view->panel, view->window) == ERR)
720 return got_error_from_errno("replace_panel");
721 wclear(view->window);
723 view->nlines = nlines;
729 view->child->begin_x = view_split_begin_x(view->begin_x);
730 if (view->child->begin_x == 0) {
731 view_fullscreen(view->child);
732 if (view->child->focussed)
733 show_panel(view->child->panel);
735 show_panel(view->panel);
737 view_splitscreen(view->child);
738 show_panel(view->child->panel);
745 static const struct got_error *
746 view_close_child(struct tog_view *view)
748 const struct got_error *err = NULL;
750 if (view->child == NULL)
753 err = view_close(view->child);
759 view_set_child(struct tog_view *view, struct tog_view *child)
762 child->parent = view;
766 view_is_splitscreen(struct tog_view *view)
768 return view->begin_x > 0;
777 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
778 cols = 80; /* Default */
784 resize_term(lines, cols);
787 static const struct got_error *
788 view_search_start(struct tog_view *view)
790 const struct got_error *err = NULL;
794 if (view->search_started) {
795 regfree(&view->regex);
797 memset(&view->regmatch, 0, sizeof(view->regmatch));
799 view->search_started = 0;
801 if (view->nlines < 1)
804 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
805 wclrtoeol(view->window);
809 ret = wgetnstr(view->window, pattern, sizeof(pattern));
815 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
816 err = view->search_start(view);
818 regfree(&view->regex);
821 view->search_started = 1;
822 view->searching = TOG_SEARCH_FORWARD;
823 view->search_next_done = 0;
824 view->search_next(view);
830 static const struct got_error *
831 view_input(struct tog_view **new, int *done, struct tog_view *view,
832 struct tog_view_list_head *views)
834 const struct got_error *err = NULL;
840 /* Clear "no matches" indicator. */
841 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
842 view->search_next_done == TOG_SEARCH_HAVE_NONE)
843 view->search_next_done = TOG_SEARCH_HAVE_MORE;
845 if (view->searching && !view->search_next_done) {
846 errcode = pthread_mutex_unlock(&tog_mutex);
848 return got_error_set_errno(errcode,
849 "pthread_mutex_unlock");
851 errcode = pthread_mutex_lock(&tog_mutex);
853 return got_error_set_errno(errcode,
854 "pthread_mutex_lock");
855 view->search_next(view);
859 nodelay(stdscr, FALSE);
860 /* Allow threads to make progress while we are waiting for input. */
861 errcode = pthread_mutex_unlock(&tog_mutex);
863 return got_error_set_errno(errcode, "pthread_mutex_unlock");
864 ch = wgetch(view->window);
865 errcode = pthread_mutex_lock(&tog_mutex);
867 return got_error_set_errno(errcode, "pthread_mutex_lock");
868 nodelay(stdscr, TRUE);
870 if (tog_sigwinch_received || tog_sigcont_received) {
872 tog_sigwinch_received = 0;
873 tog_sigcont_received = 0;
874 TAILQ_FOREACH(v, views, entry) {
875 err = view_resize(v);
878 err = v->input(new, v, KEY_RESIZE);
882 err = view_resize(v->child);
885 err = v->child->input(new, v->child,
897 view->child->focussed = 1;
898 view->focus_child = 1;
899 } else if (view->parent) {
901 view->parent->focussed = 1;
902 view->parent->focus_child = 0;
906 err = view->input(new, view, ch);
913 if (view_is_parent_view(view)) {
914 if (view->child == NULL)
916 if (view_is_splitscreen(view->child)) {
918 view->child->focussed = 1;
919 err = view_fullscreen(view->child);
921 err = view_splitscreen(view->child);
924 err = view->child->input(new, view->child,
927 if (view_is_splitscreen(view)) {
928 view->parent->focussed = 0;
930 err = view_fullscreen(view);
932 err = view_splitscreen(view);
936 err = view->input(new, view, KEY_RESIZE);
942 if (view->search_start)
943 view_search_start(view);
945 err = view->input(new, view, ch);
949 if (view->search_started && view->search_next) {
950 view->searching = (ch == 'n' ?
951 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
952 view->search_next_done = 0;
953 view->search_next(view);
955 err = view->input(new, view, ch);
958 err = view->input(new, view, ch);
966 view_vborder(struct tog_view *view)
969 const struct tog_view *view_above;
972 return view_vborder(view->parent);
974 panel = panel_above(view->panel);
978 view_above = panel_userptr(panel);
979 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
980 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
984 view_needs_focus_indication(struct tog_view *view)
986 if (view_is_parent_view(view)) {
987 if (view->child == NULL || view->child->focussed)
989 if (!view_is_splitscreen(view->child))
991 } else if (!view_is_splitscreen(view))
994 return view->focussed;
997 static const struct got_error *
998 view_loop(struct tog_view *view)
1000 const struct got_error *err = NULL;
1001 struct tog_view_list_head views;
1002 struct tog_view *new_view;
1003 int fast_refresh = 10;
1004 int done = 0, errcode;
1006 errcode = pthread_mutex_lock(&tog_mutex);
1008 return got_error_set_errno(errcode, "pthread_mutex_lock");
1011 TAILQ_INSERT_HEAD(&views, view, entry);
1014 err = view->show(view);
1019 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1020 /* Refresh fast during initialization, then become slower. */
1021 if (fast_refresh && fast_refresh-- == 0)
1022 halfdelay(10); /* switch to once per second */
1024 err = view_input(&new_view, &done, view, &views);
1028 struct tog_view *v, *prev = NULL;
1030 if (view_is_parent_view(view))
1031 prev = TAILQ_PREV(view, tog_view_list_head,
1033 else if (view->parent)
1034 prev = view->parent;
1037 view->parent->child = NULL;
1038 view->parent->focus_child = 0;
1040 TAILQ_REMOVE(&views, view, entry);
1042 err = view_close(view);
1047 TAILQ_FOREACH(v, &views, entry) {
1051 if (view == NULL && new_view == NULL) {
1052 /* No view has focus. Try to pick one. */
1055 else if (!TAILQ_EMPTY(&views)) {
1056 view = TAILQ_LAST(&views,
1057 tog_view_list_head);
1060 if (view->focus_child) {
1061 view->child->focussed = 1;
1069 struct tog_view *v, *t;
1070 /* Only allow one parent view per type. */
1071 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1072 if (v->type != new_view->type)
1074 TAILQ_REMOVE(&views, v, entry);
1075 err = view_close(v);
1080 TAILQ_INSERT_TAIL(&views, new_view, entry);
1084 if (view_is_parent_view(view)) {
1085 if (view->child && view->child->focussed)
1088 if (view->parent && view->parent->focussed)
1089 view = view->parent;
1091 show_panel(view->panel);
1092 if (view->child && view_is_splitscreen(view->child))
1093 show_panel(view->child->panel);
1094 if (view->parent && view_is_splitscreen(view)) {
1095 err = view->parent->show(view->parent);
1099 err = view->show(view);
1103 err = view->child->show(view->child);
1112 while (!TAILQ_EMPTY(&views)) {
1113 view = TAILQ_FIRST(&views);
1114 TAILQ_REMOVE(&views, view, entry);
1118 errcode = pthread_mutex_unlock(&tog_mutex);
1119 if (errcode && err == NULL)
1120 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1130 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1135 /* Create newly allocated wide-character string equivalent to a byte string. */
1136 static const struct got_error *
1137 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1140 const struct got_error *err = NULL;
1143 *wlen = mbstowcs(NULL, s, 0);
1144 if (*wlen == (size_t)-1) {
1146 if (errno != EILSEQ)
1147 return got_error_from_errno("mbstowcs");
1149 /* byte string invalid in current encoding; try to "fix" it */
1150 err = got_mbsavis(&vis, &vislen, s);
1153 *wlen = mbstowcs(NULL, vis, 0);
1154 if (*wlen == (size_t)-1) {
1155 err = got_error_from_errno("mbstowcs"); /* give up */
1160 *ws = calloc(*wlen + 1, sizeof(**ws));
1162 err = got_error_from_errno("calloc");
1166 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1167 err = got_error_from_errno("mbstowcs");
1178 /* Format a line for display, ensuring that it won't overflow a width limit. */
1179 static const struct got_error *
1180 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1183 const struct got_error *err = NULL;
1185 wchar_t *wline = NULL;
1192 err = mbs2ws(&wline, &wlen, line);
1196 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1197 wline[wlen - 1] = L'\0';
1200 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1201 wline[wlen - 1] = L'\0';
1207 int width = wcwidth(wline[i]);
1214 if (width == 1 || width == 2) {
1215 if (cols + width > wlimit)
1219 } else if (width == -1) {
1220 if (wline[i] == L'\t') {
1222 ((cols + col_tab_align) % TABSIZE);
1227 if (cols + width > wlimit)
1232 err = got_error_from_errno("wcwidth");
1247 static const struct got_error*
1248 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1249 struct got_object_id *id, struct got_repository *repo)
1251 static const struct got_error *err = NULL;
1252 struct got_reflist_entry *re;
1258 TAILQ_FOREACH(re, refs, entry) {
1259 struct got_tag_object *tag = NULL;
1260 struct got_object_id *ref_id;
1263 name = got_ref_get_name(re->ref);
1264 if (strcmp(name, GOT_REF_HEAD) == 0)
1266 if (strncmp(name, "refs/", 5) == 0)
1268 if (strncmp(name, "got/", 4) == 0)
1270 if (strncmp(name, "heads/", 6) == 0)
1272 if (strncmp(name, "remotes/", 8) == 0) {
1274 s = strstr(name, "/" GOT_REF_HEAD);
1275 if (s != NULL && s[strlen(s)] == '\0')
1278 err = got_ref_resolve(&ref_id, repo, re->ref);
1281 if (strncmp(name, "tags/", 5) == 0) {
1282 err = got_object_open_as_tag(&tag, repo, ref_id);
1284 if (err->code != GOT_ERR_OBJ_TYPE) {
1288 /* Ref points at something other than a tag. */
1293 cmp = got_object_id_cmp(tag ?
1294 got_object_tag_get_object_id(tag) : ref_id, id);
1297 got_object_tag_close(tag);
1301 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1302 s ? ", " : "", name) == -1) {
1303 err = got_error_from_errno("asprintf");
1314 static const struct got_error *
1315 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1320 smallerthan = strchr(author, '<');
1321 if (smallerthan && smallerthan[1] != '\0')
1322 author = smallerthan + 1;
1323 author[strcspn(author, "@>")] = '\0';
1324 return format_line(wauthor, author_width, author, limit, col_tab_align);
1327 static const struct got_error *
1328 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1329 struct got_object_id *id, const size_t date_display_cols,
1330 int author_display_cols)
1332 struct tog_log_view_state *s = &view->state.log;
1333 const struct got_error *err = NULL;
1334 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1335 char *logmsg0 = NULL, *logmsg = NULL;
1336 char *author = NULL;
1337 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1338 int author_width, logmsg_width;
1339 char *newline, *line = NULL;
1341 const int avail = view->ncols;
1343 time_t committer_time;
1344 struct tog_color *tc;
1346 committer_time = got_object_commit_get_committer_time(commit);
1347 if (gmtime_r(&committer_time, &tm) == NULL)
1348 return got_error_from_errno("gmtime_r");
1349 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1350 return got_error(GOT_ERR_NO_SPACE);
1352 if (avail <= date_display_cols)
1353 limit = MIN(sizeof(datebuf) - 1, avail);
1355 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1356 tc = get_color(&s->colors, TOG_COLOR_DATE);
1358 wattr_on(view->window,
1359 COLOR_PAIR(tc->colorpair), NULL);
1360 waddnstr(view->window, datebuf, limit);
1362 wattr_off(view->window,
1363 COLOR_PAIR(tc->colorpair), NULL);
1370 err = got_object_id_str(&id_str, id);
1373 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1375 wattr_on(view->window,
1376 COLOR_PAIR(tc->colorpair), NULL);
1377 wprintw(view->window, "%.8s ", id_str);
1379 wattr_off(view->window,
1380 COLOR_PAIR(tc->colorpair), NULL);
1387 author = strdup(got_object_commit_get_author(commit));
1388 if (author == NULL) {
1389 err = got_error_from_errno("strdup");
1392 err = format_author(&wauthor, &author_width, author, avail - col, col);
1395 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1397 wattr_on(view->window,
1398 COLOR_PAIR(tc->colorpair), NULL);
1399 waddwstr(view->window, wauthor);
1401 wattr_off(view->window,
1402 COLOR_PAIR(tc->colorpair), NULL);
1403 col += author_width;
1404 while (col < avail && author_width < author_display_cols + 2) {
1405 waddch(view->window, ' ');
1412 err = got_object_commit_get_logmsg(&logmsg0, commit);
1416 while (*logmsg == '\n')
1418 newline = strchr(logmsg, '\n');
1421 limit = avail - col;
1422 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1425 waddwstr(view->window, wlogmsg);
1426 col += logmsg_width;
1427 while (col < avail) {
1428 waddch(view->window, ' ');
1440 static struct commit_queue_entry *
1441 alloc_commit_queue_entry(struct got_commit_object *commit,
1442 struct got_object_id *id)
1444 struct commit_queue_entry *entry;
1446 entry = calloc(1, sizeof(*entry));
1451 entry->commit = commit;
1456 pop_commit(struct commit_queue *commits)
1458 struct commit_queue_entry *entry;
1460 entry = TAILQ_FIRST(&commits->head);
1461 TAILQ_REMOVE(&commits->head, entry, entry);
1462 got_object_commit_close(entry->commit);
1463 commits->ncommits--;
1464 /* Don't free entry->id! It is owned by the commit graph. */
1469 free_commits(struct commit_queue *commits)
1471 while (!TAILQ_EMPTY(&commits->head))
1472 pop_commit(commits);
1475 static const struct got_error *
1476 match_commit(int *have_match, struct got_object_id *id,
1477 struct got_commit_object *commit, regex_t *regex)
1479 const struct got_error *err = NULL;
1480 regmatch_t regmatch;
1481 char *id_str = NULL, *logmsg = NULL;
1485 err = got_object_id_str(&id_str, id);
1489 err = got_object_commit_get_logmsg(&logmsg, commit);
1493 if (regexec(regex, got_object_commit_get_author(commit), 1,
1494 ®match, 0) == 0 ||
1495 regexec(regex, got_object_commit_get_committer(commit), 1,
1496 ®match, 0) == 0 ||
1497 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1498 regexec(regex, logmsg, 1, ®match, 0) == 0)
1506 static const struct got_error *
1507 queue_commits(struct tog_log_thread_args *a)
1509 const struct got_error *err = NULL;
1512 * We keep all commits open throughout the lifetime of the log
1513 * view in order to avoid having to re-fetch commits from disk
1514 * while updating the display.
1517 struct got_object_id *id;
1518 struct got_commit_object *commit;
1519 struct commit_queue_entry *entry;
1522 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1524 if (err || id == NULL)
1527 err = got_object_open_as_commit(&commit, a->repo, id);
1530 entry = alloc_commit_queue_entry(commit, id);
1531 if (entry == NULL) {
1532 err = got_error_from_errno("alloc_commit_queue_entry");
1536 errcode = pthread_mutex_lock(&tog_mutex);
1538 err = got_error_set_errno(errcode,
1539 "pthread_mutex_lock");
1543 entry->idx = a->commits->ncommits;
1544 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1545 a->commits->ncommits++;
1547 if (*a->searching == TOG_SEARCH_FORWARD &&
1548 !*a->search_next_done) {
1550 err = match_commit(&have_match, id, commit, a->regex);
1554 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1557 errcode = pthread_mutex_unlock(&tog_mutex);
1558 if (errcode && err == NULL)
1559 err = got_error_set_errno(errcode,
1560 "pthread_mutex_unlock");
1563 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1569 select_commit(struct tog_log_view_state *s)
1571 struct commit_queue_entry *entry;
1574 entry = s->first_displayed_entry;
1576 if (ncommits == s->selected) {
1577 s->selected_entry = entry;
1580 entry = TAILQ_NEXT(entry, entry);
1585 static const struct got_error *
1586 draw_commits(struct tog_view *view)
1588 const struct got_error *err = NULL;
1589 struct tog_log_view_state *s = &view->state.log;
1590 struct commit_queue_entry *entry = s->selected_entry;
1591 const int limit = view->nlines;
1593 int ncommits, author_cols = 4;
1594 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1595 char *refs_str = NULL;
1597 struct tog_color *tc;
1598 static const size_t date_display_cols = 12;
1600 if (s->selected_entry &&
1601 !(view->searching && view->search_next_done == 0)) {
1602 struct got_reflist_head *refs;
1603 err = got_object_id_str(&id_str, s->selected_entry->id);
1606 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1607 s->selected_entry->id);
1609 err = build_refs_str(&refs_str, refs,
1610 s->selected_entry->id, s->repo);
1616 if (s->thread_args.commits_needed == 0)
1617 halfdelay(10); /* disable fast refresh */
1619 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1620 if (asprintf(&ncommits_str, " [%d/%d] %s",
1621 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1622 (view->searching && !view->search_next_done) ?
1623 "searching..." : "loading...") == -1) {
1624 err = got_error_from_errno("asprintf");
1628 const char *search_str = NULL;
1630 if (view->searching) {
1631 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1632 search_str = "no more matches";
1633 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1634 search_str = "no matches found";
1635 else if (!view->search_next_done)
1636 search_str = "searching...";
1639 if (asprintf(&ncommits_str, " [%d/%d] %s",
1640 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1641 search_str ? search_str :
1642 (refs_str ? refs_str : "")) == -1) {
1643 err = got_error_from_errno("asprintf");
1648 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1649 if (asprintf(&header, "commit %s %s%s",
1650 id_str ? id_str : "........................................",
1651 s->in_repo_path, ncommits_str) == -1) {
1652 err = got_error_from_errno("asprintf");
1656 } else if (asprintf(&header, "commit %s%s",
1657 id_str ? id_str : "........................................",
1658 ncommits_str) == -1) {
1659 err = got_error_from_errno("asprintf");
1663 err = format_line(&wline, &width, header, view->ncols, 0);
1667 werase(view->window);
1669 if (view_needs_focus_indication(view))
1670 wstandout(view->window);
1671 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1673 wattr_on(view->window,
1674 COLOR_PAIR(tc->colorpair), NULL);
1675 waddwstr(view->window, wline);
1677 wattr_off(view->window,
1678 COLOR_PAIR(tc->colorpair), NULL);
1679 while (width < view->ncols) {
1680 waddch(view->window, ' ');
1683 if (view_needs_focus_indication(view))
1684 wstandend(view->window);
1689 /* Grow author column size if necessary. */
1690 entry = s->first_displayed_entry;
1696 if (ncommits >= limit - 1)
1698 author = strdup(got_object_commit_get_author(entry->commit));
1699 if (author == NULL) {
1700 err = got_error_from_errno("strdup");
1703 err = format_author(&wauthor, &width, author, COLS,
1705 if (author_cols < width)
1706 author_cols = width;
1710 entry = TAILQ_NEXT(entry, entry);
1713 entry = s->first_displayed_entry;
1714 s->last_displayed_entry = s->first_displayed_entry;
1717 if (ncommits >= limit - 1)
1719 if (ncommits == s->selected)
1720 wstandout(view->window);
1721 err = draw_commit(view, entry->commit, entry->id,
1722 date_display_cols, author_cols);
1723 if (ncommits == s->selected)
1724 wstandend(view->window);
1728 s->last_displayed_entry = entry;
1729 entry = TAILQ_NEXT(entry, entry);
1744 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1746 struct commit_queue_entry *entry;
1749 entry = TAILQ_FIRST(&s->commits.head);
1750 if (s->first_displayed_entry == entry)
1753 entry = s->first_displayed_entry;
1754 while (entry && nscrolled < maxscroll) {
1755 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1757 s->first_displayed_entry = entry;
1763 static const struct got_error *
1764 trigger_log_thread(struct tog_view *view, int wait)
1766 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1769 halfdelay(1); /* fast refresh while loading commits */
1771 while (ta->commits_needed > 0 || ta->load_all) {
1772 if (ta->log_complete)
1775 /* Wake the log thread. */
1776 errcode = pthread_cond_signal(&ta->need_commits);
1778 return got_error_set_errno(errcode,
1779 "pthread_cond_signal");
1782 * The mutex will be released while the view loop waits
1783 * in wgetch(), at which time the log thread will run.
1788 /* Display progress update in log view. */
1789 show_log_view(view);
1793 /* Wait right here while next commit is being loaded. */
1794 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1796 return got_error_set_errno(errcode,
1797 "pthread_cond_wait");
1799 /* Display progress update in log view. */
1800 show_log_view(view);
1808 static const struct got_error *
1809 log_scroll_down(struct tog_view *view, int maxscroll)
1811 struct tog_log_view_state *s = &view->state.log;
1812 const struct got_error *err = NULL;
1813 struct commit_queue_entry *pentry;
1814 int nscrolled = 0, ncommits_needed;
1816 if (s->last_displayed_entry == NULL)
1819 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1820 if (s->commits.ncommits < ncommits_needed &&
1821 !s->thread_args.log_complete) {
1823 * Ask the log thread for required amount of commits.
1825 s->thread_args.commits_needed += maxscroll;
1826 err = trigger_log_thread(view, 1);
1832 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1836 s->last_displayed_entry = pentry;
1838 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1841 s->first_displayed_entry = pentry;
1842 } while (++nscrolled < maxscroll);
1847 static const struct got_error *
1848 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1849 struct got_commit_object *commit, struct got_object_id *commit_id,
1850 struct tog_view *log_view, struct got_repository *repo)
1852 const struct got_error *err;
1853 struct got_object_qid *parent_id;
1854 struct tog_view *diff_view;
1856 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1857 if (diff_view == NULL)
1858 return got_error_from_errno("view_open");
1860 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1861 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1862 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1864 *new_view = diff_view;
1868 static const struct got_error *
1869 tree_view_visit_subtree(struct tog_tree_view_state *s,
1870 struct got_tree_object *subtree)
1872 struct tog_parent_tree *parent;
1874 parent = calloc(1, sizeof(*parent));
1876 return got_error_from_errno("calloc");
1878 parent->tree = s->tree;
1879 parent->first_displayed_entry = s->first_displayed_entry;
1880 parent->selected_entry = s->selected_entry;
1881 parent->selected = s->selected;
1882 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1885 s->first_displayed_entry = NULL;
1889 static const struct got_error *
1890 tree_view_walk_path(struct tog_tree_view_state *s,
1891 struct got_object_id *commit_id, const char *path)
1893 const struct got_error *err = NULL;
1894 struct got_tree_object *tree = NULL;
1896 char *slash, *subpath = NULL;
1898 /* Walk the path and open corresponding tree objects. */
1901 struct got_tree_entry *te;
1902 struct got_object_id *tree_id;
1908 /* Ensure the correct subtree entry is selected. */
1909 slash = strchr(p, '/');
1911 te_name = strdup(p);
1913 te_name = strndup(p, slash - p);
1914 if (te_name == NULL) {
1915 err = got_error_from_errno("strndup");
1918 te = got_object_tree_find_entry(s->tree, te_name);
1920 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1925 s->first_displayed_entry = s->selected_entry = te;
1927 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1928 break; /* jump to this file's entry */
1930 slash = strchr(p, '/');
1932 subpath = strndup(path, slash - path);
1934 subpath = strdup(path);
1935 if (subpath == NULL) {
1936 err = got_error_from_errno("strdup");
1940 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1945 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1950 err = tree_view_visit_subtree(s, tree);
1952 got_object_tree_close(tree);
1966 static const struct got_error *
1967 browse_commit_tree(struct tog_view **new_view, int begin_x,
1968 struct commit_queue_entry *entry, const char *path,
1969 const char *head_ref_name, struct got_repository *repo)
1971 const struct got_error *err = NULL;
1972 struct tog_tree_view_state *s;
1973 struct tog_view *tree_view;
1975 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1976 if (tree_view == NULL)
1977 return got_error_from_errno("view_open");
1979 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
1982 s = &tree_view->state.tree;
1984 *new_view = tree_view;
1986 if (got_path_is_root_dir(path))
1989 return tree_view_walk_path(s, entry->id, path);
1992 static const struct got_error *
1993 block_signals_used_by_main_thread(void)
1998 if (sigemptyset(&sigset) == -1)
1999 return got_error_from_errno("sigemptyset");
2001 /* tog handles SIGWINCH and SIGCONT */
2002 if (sigaddset(&sigset, SIGWINCH) == -1)
2003 return got_error_from_errno("sigaddset");
2004 if (sigaddset(&sigset, SIGCONT) == -1)
2005 return got_error_from_errno("sigaddset");
2007 /* ncurses handles SIGTSTP */
2008 if (sigaddset(&sigset, SIGTSTP) == -1)
2009 return got_error_from_errno("sigaddset");
2011 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2013 return got_error_set_errno(errcode, "pthread_sigmask");
2019 log_thread(void *arg)
2021 const struct got_error *err = NULL;
2023 struct tog_log_thread_args *a = arg;
2026 err = block_signals_used_by_main_thread();
2030 while (!done && !err && !tog_sigpipe_received) {
2031 err = queue_commits(a);
2033 if (err->code != GOT_ERR_ITER_COMPLETED)
2037 } else if (a->commits_needed > 0 && !a->load_all)
2038 a->commits_needed--;
2040 errcode = pthread_mutex_lock(&tog_mutex);
2042 err = got_error_set_errno(errcode,
2043 "pthread_mutex_lock");
2045 } else if (*a->quit)
2047 else if (*a->first_displayed_entry == NULL) {
2048 *a->first_displayed_entry =
2049 TAILQ_FIRST(&a->commits->head);
2050 *a->selected_entry = *a->first_displayed_entry;
2053 errcode = pthread_cond_signal(&a->commit_loaded);
2055 err = got_error_set_errno(errcode,
2056 "pthread_cond_signal");
2057 pthread_mutex_unlock(&tog_mutex);
2062 a->commits_needed = 0;
2064 if (a->commits_needed == 0 && !a->load_all) {
2065 errcode = pthread_cond_wait(&a->need_commits,
2068 err = got_error_set_errno(errcode,
2069 "pthread_cond_wait");
2075 errcode = pthread_mutex_unlock(&tog_mutex);
2076 if (errcode && err == NULL)
2077 err = got_error_set_errno(errcode,
2078 "pthread_mutex_unlock");
2080 a->log_complete = 1;
2084 static const struct got_error *
2085 stop_log_thread(struct tog_log_view_state *s)
2087 const struct got_error *err = NULL;
2092 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2094 return got_error_set_errno(errcode,
2095 "pthread_cond_signal");
2096 errcode = pthread_mutex_unlock(&tog_mutex);
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_unlock");
2100 errcode = pthread_join(s->thread, (void **)&err);
2102 return got_error_set_errno(errcode, "pthread_join");
2103 errcode = pthread_mutex_lock(&tog_mutex);
2105 return got_error_set_errno(errcode,
2106 "pthread_mutex_lock");
2107 s->thread = 0; //NULL;
2110 if (s->thread_args.repo) {
2111 err = got_repo_close(s->thread_args.repo);
2112 s->thread_args.repo = NULL;
2115 if (s->thread_args.graph) {
2116 got_commit_graph_close(s->thread_args.graph);
2117 s->thread_args.graph = NULL;
2123 static const struct got_error *
2124 close_log_view(struct tog_view *view)
2126 const struct got_error *err = NULL;
2127 struct tog_log_view_state *s = &view->state.log;
2130 err = stop_log_thread(s);
2132 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2133 if (errcode && err == NULL)
2134 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2136 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2140 free_commits(&s->commits);
2141 free(s->in_repo_path);
2142 s->in_repo_path = NULL;
2145 free(s->head_ref_name);
2146 s->head_ref_name = NULL;
2150 static const struct got_error *
2151 search_start_log_view(struct tog_view *view)
2153 struct tog_log_view_state *s = &view->state.log;
2155 s->matched_entry = NULL;
2156 s->search_entry = NULL;
2160 static const struct got_error *
2161 search_next_log_view(struct tog_view *view)
2163 const struct got_error *err = NULL;
2164 struct tog_log_view_state *s = &view->state.log;
2165 struct commit_queue_entry *entry;
2167 /* Display progress update in log view. */
2168 show_log_view(view);
2172 if (s->search_entry) {
2174 errcode = pthread_mutex_unlock(&tog_mutex);
2176 return got_error_set_errno(errcode,
2177 "pthread_mutex_unlock");
2178 ch = wgetch(view->window);
2179 errcode = pthread_mutex_lock(&tog_mutex);
2181 return got_error_set_errno(errcode,
2182 "pthread_mutex_lock");
2183 if (ch == KEY_BACKSPACE) {
2184 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 if (view->searching == TOG_SEARCH_FORWARD)
2188 entry = TAILQ_NEXT(s->search_entry, entry);
2190 entry = TAILQ_PREV(s->search_entry,
2191 commit_queue_head, entry);
2192 } else if (s->matched_entry) {
2193 if (view->searching == TOG_SEARCH_FORWARD)
2194 entry = TAILQ_NEXT(s->matched_entry, entry);
2196 entry = TAILQ_PREV(s->matched_entry,
2197 commit_queue_head, entry);
2199 if (view->searching == TOG_SEARCH_FORWARD)
2200 entry = TAILQ_FIRST(&s->commits.head);
2202 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2208 if (entry == NULL) {
2209 if (s->thread_args.log_complete ||
2210 view->searching == TOG_SEARCH_BACKWARD) {
2211 view->search_next_done =
2212 (s->matched_entry == NULL ?
2213 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2214 s->search_entry = NULL;
2218 * Poke the log thread for more commits and return,
2219 * allowing the main loop to make progress. Search
2220 * will resume at s->search_entry once we come back.
2222 s->thread_args.commits_needed++;
2223 return trigger_log_thread(view, 0);
2226 err = match_commit(&have_match, entry->id, entry->commit,
2231 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2232 s->matched_entry = entry;
2236 s->search_entry = entry;
2237 if (view->searching == TOG_SEARCH_FORWARD)
2238 entry = TAILQ_NEXT(entry, entry);
2240 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2243 if (s->matched_entry) {
2244 int cur = s->selected_entry->idx;
2245 while (cur < s->matched_entry->idx) {
2246 err = input_log_view(NULL, view, KEY_DOWN);
2251 while (cur > s->matched_entry->idx) {
2252 err = input_log_view(NULL, view, KEY_UP);
2259 s->search_entry = NULL;
2264 static const struct got_error *
2265 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2266 struct got_repository *repo, const char *head_ref_name,
2267 const char *in_repo_path, int log_branches)
2269 const struct got_error *err = NULL;
2270 struct tog_log_view_state *s = &view->state.log;
2271 struct got_repository *thread_repo = NULL;
2272 struct got_commit_graph *thread_graph = NULL;
2275 if (in_repo_path != s->in_repo_path) {
2276 free(s->in_repo_path);
2277 s->in_repo_path = strdup(in_repo_path);
2278 if (s->in_repo_path == NULL)
2279 return got_error_from_errno("strdup");
2282 /* The commit queue only contains commits being displayed. */
2283 TAILQ_INIT(&s->commits.head);
2284 s->commits.ncommits = 0;
2287 if (head_ref_name) {
2288 s->head_ref_name = strdup(head_ref_name);
2289 if (s->head_ref_name == NULL) {
2290 err = got_error_from_errno("strdup");
2294 s->start_id = got_object_id_dup(start_id);
2295 if (s->start_id == NULL) {
2296 err = got_error_from_errno("got_object_id_dup");
2299 s->log_branches = log_branches;
2301 STAILQ_INIT(&s->colors);
2302 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2303 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2304 get_color_value("TOG_COLOR_COMMIT"));
2307 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2308 get_color_value("TOG_COLOR_AUTHOR"));
2310 free_colors(&s->colors);
2313 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2314 get_color_value("TOG_COLOR_DATE"));
2316 free_colors(&s->colors);
2321 view->show = show_log_view;
2322 view->input = input_log_view;
2323 view->close = close_log_view;
2324 view->search_start = search_start_log_view;
2325 view->search_next = search_next_log_view;
2327 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2330 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2334 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2335 s->repo, NULL, NULL);
2339 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2341 err = got_error_set_errno(errcode, "pthread_cond_init");
2344 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2346 err = got_error_set_errno(errcode, "pthread_cond_init");
2350 s->thread_args.commits_needed = view->nlines;
2351 s->thread_args.graph = thread_graph;
2352 s->thread_args.commits = &s->commits;
2353 s->thread_args.in_repo_path = s->in_repo_path;
2354 s->thread_args.start_id = s->start_id;
2355 s->thread_args.repo = thread_repo;
2356 s->thread_args.log_complete = 0;
2357 s->thread_args.quit = &s->quit;
2358 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2359 s->thread_args.selected_entry = &s->selected_entry;
2360 s->thread_args.searching = &view->searching;
2361 s->thread_args.search_next_done = &view->search_next_done;
2362 s->thread_args.regex = &view->regex;
2365 close_log_view(view);
2369 static const struct got_error *
2370 show_log_view(struct tog_view *view)
2372 const struct got_error *err;
2373 struct tog_log_view_state *s = &view->state.log;
2375 if (s->thread == 0) { //NULL) {
2376 int errcode = pthread_create(&s->thread, NULL, log_thread,
2379 return got_error_set_errno(errcode, "pthread_create");
2380 if (s->thread_args.commits_needed > 0) {
2381 err = trigger_log_thread(view, 1);
2387 return draw_commits(view);
2390 static const struct got_error *
2391 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2393 const struct got_error *err = NULL;
2394 struct tog_log_view_state *s = &view->state.log;
2395 struct tog_view *diff_view = NULL, *tree_view = NULL;
2396 struct tog_view *ref_view = NULL;
2397 struct commit_queue_entry *entry;
2400 if (s->thread_args.load_all) {
2401 if (ch == KEY_BACKSPACE)
2402 s->thread_args.load_all = 0;
2403 else if (s->thread_args.log_complete) {
2404 s->thread_args.load_all = 0;
2405 log_scroll_down(view, s->commits.ncommits);
2406 s->selected = MIN(view->nlines - 2,
2407 s->commits.ncommits - 1);
2421 if (s->first_displayed_entry == NULL)
2423 if (s->selected > 0)
2426 log_scroll_up(s, 1);
2432 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2437 if (s->first_displayed_entry == NULL)
2439 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2442 log_scroll_up(s, view->nlines - 1);
2449 if (s->first_displayed_entry == NULL)
2451 if (s->selected < MIN(view->nlines - 2,
2452 s->commits.ncommits - 1))
2455 err = log_scroll_down(view, 1);
2463 /* We don't know yet how many commits, so we're forced to
2464 * traverse them all. */
2465 if (!s->thread_args.log_complete) {
2466 s->thread_args.load_all = 1;
2467 return trigger_log_thread(view, 0);
2471 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2472 for (n = 0; n < view->nlines - 1; n++) {
2475 s->first_displayed_entry = entry;
2476 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2479 s->selected = n - 1;
2485 struct commit_queue_entry *first;
2486 first = s->first_displayed_entry;
2489 err = log_scroll_down(view, view->nlines - 1);
2492 if (first == s->first_displayed_entry &&
2493 s->selected < MIN(view->nlines - 2,
2494 s->commits.ncommits - 1)) {
2495 /* can't scroll further down */
2496 s->selected = MIN(view->nlines - 2,
2497 s->commits.ncommits - 1);
2503 if (s->selected > view->nlines - 2)
2504 s->selected = view->nlines - 2;
2505 if (s->selected > s->commits.ncommits - 1)
2506 s->selected = s->commits.ncommits - 1;
2508 if (s->commits.ncommits < view->nlines - 1 &&
2509 !s->thread_args.log_complete) {
2510 s->thread_args.commits_needed += (view->nlines - 1) -
2511 s->commits.ncommits;
2512 err = trigger_log_thread(view, 1);
2518 if (s->selected_entry == NULL)
2520 if (view_is_parent_view(view))
2521 begin_x = view_split_begin_x(view->begin_x);
2522 err = open_diff_view_for_commit(&diff_view, begin_x,
2523 s->selected_entry->commit, s->selected_entry->id,
2528 diff_view->focussed = 1;
2529 if (view_is_parent_view(view)) {
2530 err = view_close_child(view);
2533 view_set_child(view, diff_view);
2534 view->focus_child = 1;
2536 *new_view = diff_view;
2539 if (s->selected_entry == NULL)
2541 if (view_is_parent_view(view))
2542 begin_x = view_split_begin_x(view->begin_x);
2543 err = browse_commit_tree(&tree_view, begin_x,
2544 s->selected_entry, s->in_repo_path, s->head_ref_name,
2549 tree_view->focussed = 1;
2550 if (view_is_parent_view(view)) {
2551 err = view_close_child(view);
2554 view_set_child(view, tree_view);
2555 view->focus_child = 1;
2557 *new_view = tree_view;
2562 if (ch == KEY_BACKSPACE &&
2563 got_path_is_root_dir(s->in_repo_path))
2565 err = stop_log_thread(s);
2568 if (ch == KEY_BACKSPACE) {
2570 err = got_path_dirname(&parent_path, s->in_repo_path);
2573 free(s->in_repo_path);
2574 s->in_repo_path = parent_path;
2575 s->thread_args.in_repo_path = s->in_repo_path;
2576 } else if (ch == CTRL('l')) {
2577 struct got_object_id *start_id;
2578 err = got_repo_match_object_id(&start_id, NULL,
2579 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2580 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2584 s->start_id = start_id;
2585 s->thread_args.start_id = s->start_id;
2587 s->log_branches = !s->log_branches;
2589 err = got_repo_open(&s->thread_args.repo,
2590 got_repo_get_path(s->repo), NULL);
2594 err = tog_load_refs(s->repo);
2597 err = got_commit_graph_open(&s->thread_args.graph,
2598 s->in_repo_path, !s->log_branches);
2601 err = got_commit_graph_iter_start(s->thread_args.graph,
2602 s->start_id, s->repo, NULL, NULL);
2605 free_commits(&s->commits);
2606 s->first_displayed_entry = NULL;
2607 s->last_displayed_entry = NULL;
2608 s->selected_entry = NULL;
2610 s->thread_args.log_complete = 0;
2612 s->thread_args.commits_needed = view->nlines;
2615 if (view_is_parent_view(view))
2616 begin_x = view_split_begin_x(view->begin_x);
2617 ref_view = view_open(view->nlines, view->ncols,
2618 view->begin_y, begin_x, TOG_VIEW_REF);
2619 if (ref_view == NULL)
2620 return got_error_from_errno("view_open");
2621 err = open_ref_view(ref_view, s->repo);
2623 view_close(ref_view);
2627 ref_view->focussed = 1;
2628 if (view_is_parent_view(view)) {
2629 err = view_close_child(view);
2632 view_set_child(view, ref_view);
2633 view->focus_child = 1;
2635 *new_view = ref_view;
2644 static const struct got_error *
2645 apply_unveil(const char *repo_path, const char *worktree_path)
2647 const struct got_error *error;
2650 if (unveil("gmon.out", "rwc") != 0)
2651 return got_error_from_errno2("unveil", "gmon.out");
2653 if (repo_path && unveil(repo_path, "r") != 0)
2654 return got_error_from_errno2("unveil", repo_path);
2656 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2657 return got_error_from_errno2("unveil", worktree_path);
2659 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2660 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2662 error = got_privsep_unveil_exec_helpers();
2666 if (unveil(NULL, NULL) != 0)
2667 return got_error_from_errno("unveil");
2677 halfdelay(1); /* Do fast refresh while initial view is loading. */
2680 intrflush(stdscr, FALSE);
2681 keypad(stdscr, TRUE);
2683 if (getenv("TOG_COLORS") != NULL) {
2685 use_default_colors();
2687 signal(SIGWINCH, tog_sigwinch);
2688 signal(SIGPIPE, tog_sigpipe);
2689 signal(SIGCONT, tog_sigcont);
2692 static const struct got_error *
2693 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2694 struct got_repository *repo, struct got_worktree *worktree)
2696 const struct got_error *err = NULL;
2699 *in_repo_path = strdup("/");
2700 if (*in_repo_path == NULL)
2701 return got_error_from_errno("strdup");
2706 const char *prefix = got_worktree_get_path_prefix(worktree);
2709 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2712 if (asprintf(in_repo_path, "%s%s%s", prefix,
2713 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2715 err = got_error_from_errno("asprintf");
2716 *in_repo_path = NULL;
2720 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2725 static const struct got_error *
2726 cmd_log(int argc, char *argv[])
2728 const struct got_error *error;
2729 struct got_repository *repo = NULL;
2730 struct got_worktree *worktree = NULL;
2731 struct got_object_id *start_id = NULL;
2732 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2733 char *start_commit = NULL, *label = NULL;
2734 struct got_reference *ref = NULL;
2735 const char *head_ref_name = NULL;
2736 int ch, log_branches = 0;
2737 struct tog_view *view;
2739 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2745 start_commit = optarg;
2748 repo_path = realpath(optarg, NULL);
2749 if (repo_path == NULL)
2750 return got_error_from_errno2("realpath",
2765 if (repo_path == NULL) {
2766 cwd = getcwd(NULL, 0);
2768 return got_error_from_errno("getcwd");
2769 error = got_worktree_open(&worktree, cwd);
2770 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2774 strdup(got_worktree_get_repo_path(worktree));
2776 repo_path = strdup(cwd);
2777 if (repo_path == NULL) {
2778 error = got_error_from_errno("strdup");
2783 error = got_repo_open(&repo, repo_path, NULL);
2787 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2794 error = apply_unveil(got_repo_get_path(repo),
2795 worktree ? got_worktree_get_root_path(worktree) : NULL);
2799 /* already loaded by tog_log_with_path()? */
2800 if (TAILQ_EMPTY(&tog_refs)) {
2801 error = tog_load_refs(repo);
2806 if (start_commit == NULL) {
2807 error = got_repo_match_object_id(&start_id, &label,
2808 worktree ? got_worktree_get_head_ref_name(worktree) :
2809 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2812 head_ref_name = label;
2814 error = got_ref_open(&ref, repo, start_commit, 0);
2816 head_ref_name = got_ref_get_name(ref);
2817 else if (error->code != GOT_ERR_NOT_REF)
2819 error = got_repo_match_object_id(&start_id, NULL,
2820 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2825 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2827 error = got_error_from_errno("view_open");
2830 error = open_log_view(view, start_id, repo, head_ref_name,
2831 in_repo_path, log_branches);
2835 /* Release work tree lock. */
2836 got_worktree_close(worktree);
2839 error = view_loop(view);
2849 const struct got_error *close_err = got_repo_close(repo);
2854 got_worktree_close(worktree);
2863 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2864 "[-w] object1 object2\n", getprogname());
2869 match_line(const char *line, regex_t *regex, size_t nmatch,
2870 regmatch_t *regmatch)
2872 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2876 match_color(struct tog_colors *colors, const char *line)
2878 struct tog_color *tc = NULL;
2880 STAILQ_FOREACH(tc, colors, entry) {
2881 if (match_line(line, &tc->regex, 0, NULL))
2888 static const struct got_error *
2889 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2890 WINDOW *window, regmatch_t *regmatch)
2892 const struct got_error *err = NULL;
2899 s = strndup(line, regmatch->rm_so);
2901 return got_error_from_errno("strndup");
2903 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2908 waddwstr(window, wline);
2915 s = strndup(line + regmatch->rm_so,
2916 regmatch->rm_eo - regmatch->rm_so);
2918 err = got_error_from_errno("strndup");
2922 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2927 wattr_on(window, A_STANDOUT, NULL);
2928 waddwstr(window, wline);
2929 wattr_off(window, A_STANDOUT, NULL);
2936 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2937 err = format_line(&wline, &width,
2938 line + regmatch->rm_eo, wlimit, col_tab_align);
2941 waddwstr(window, wline);
2949 static const struct got_error *
2950 draw_file(struct tog_view *view, const char *header)
2952 struct tog_diff_view_state *s = &view->state.diff;
2953 regmatch_t *regmatch = &view->regmatch;
2954 const struct got_error *err;
2957 size_t linesize = 0;
2959 struct tog_color *tc;
2962 int max_lines = view->nlines;
2963 int nlines = s->nlines;
2966 line_offset = s->line_offsets[s->first_displayed_line - 1];
2967 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2968 return got_error_from_errno("fseek");
2970 werase(view->window);
2973 if (asprintf(&line, "[%d/%d] %s",
2974 s->first_displayed_line - 1 + s->selected_line, nlines,
2976 return got_error_from_errno("asprintf");
2977 err = format_line(&wline, &width, line, view->ncols, 0);
2982 if (view_needs_focus_indication(view))
2983 wstandout(view->window);
2984 waddwstr(view->window, wline);
2987 if (view_needs_focus_indication(view))
2988 wstandend(view->window);
2989 if (width <= view->ncols - 1)
2990 waddch(view->window, '\n');
2999 while (max_lines > 0 && nprinted < max_lines) {
3000 linelen = getline(&line, &linesize, s->f);
3001 if (linelen == -1) {
3007 return got_ferror(s->f, GOT_ERR_IO);
3010 tc = match_color(&s->colors, line);
3012 wattr_on(view->window,
3013 COLOR_PAIR(tc->colorpair), NULL);
3014 if (s->first_displayed_line + nprinted == s->matched_line &&
3015 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3016 err = add_matched_line(&width, line, view->ncols, 0,
3017 view->window, regmatch);
3023 err = format_line(&wline, &width, line, view->ncols, 0);
3028 waddwstr(view->window, wline);
3033 wattr_off(view->window,
3034 COLOR_PAIR(tc->colorpair), NULL);
3035 if (width <= view->ncols - 1)
3036 waddch(view->window, '\n');
3041 s->last_displayed_line = s->first_displayed_line +
3044 s->last_displayed_line = s->first_displayed_line;
3049 while (nprinted < view->nlines) {
3050 waddch(view->window, '\n');
3054 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3059 wstandout(view->window);
3060 waddwstr(view->window, wline);
3063 wstandend(view->window);
3070 get_datestr(time_t *time, char *datebuf)
3072 struct tm mytm, *tm;
3075 tm = gmtime_r(time, &mytm);
3078 s = asctime_r(tm, datebuf);
3081 p = strchr(s, '\n');
3087 static const struct got_error *
3088 get_changed_paths(struct got_pathlist_head *paths,
3089 struct got_commit_object *commit, struct got_repository *repo)
3091 const struct got_error *err = NULL;
3092 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3093 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3094 struct got_object_qid *qid;
3096 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3098 struct got_commit_object *pcommit;
3099 err = got_object_open_as_commit(&pcommit, repo,
3104 tree_id1 = got_object_id_dup(
3105 got_object_commit_get_tree_id(pcommit));
3106 if (tree_id1 == NULL) {
3107 got_object_commit_close(pcommit);
3108 return got_error_from_errno("got_object_id_dup");
3110 got_object_commit_close(pcommit);
3115 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3120 tree_id2 = got_object_commit_get_tree_id(commit);
3121 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3125 err = got_diff_tree(tree1, tree2, "", "", repo,
3126 got_diff_tree_collect_changed_paths, paths, 0);
3129 got_object_tree_close(tree1);
3131 got_object_tree_close(tree2);
3136 static const struct got_error *
3137 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3141 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3143 return got_error_from_errno("reallocarray");
3145 (*line_offsets)[*nlines] = off;
3150 static const struct got_error *
3151 write_commit_info(off_t **line_offsets, size_t *nlines,
3152 struct got_object_id *commit_id, struct got_reflist_head *refs,
3153 struct got_repository *repo, FILE *outfile)
3155 const struct got_error *err = NULL;
3156 char datebuf[26], *datestr;
3157 struct got_commit_object *commit;
3158 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3159 time_t committer_time;
3160 const char *author, *committer;
3161 char *refs_str = NULL;
3162 struct got_pathlist_head changed_paths;
3163 struct got_pathlist_entry *pe;
3167 TAILQ_INIT(&changed_paths);
3170 err = build_refs_str(&refs_str, refs, commit_id, repo);
3175 err = got_object_open_as_commit(&commit, repo, commit_id);
3179 err = got_object_id_str(&id_str, commit_id);
3181 err = got_error_from_errno("got_object_id_str");
3185 err = add_line_offset(line_offsets, nlines, 0);
3189 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3190 refs_str ? refs_str : "", refs_str ? ")" : "");
3192 err = got_error_from_errno("fprintf");
3196 err = add_line_offset(line_offsets, nlines, outoff);
3200 n = fprintf(outfile, "from: %s\n",
3201 got_object_commit_get_author(commit));
3203 err = got_error_from_errno("fprintf");
3207 err = add_line_offset(line_offsets, nlines, outoff);
3211 committer_time = got_object_commit_get_committer_time(commit);
3212 datestr = get_datestr(&committer_time, datebuf);
3214 n = fprintf(outfile, "date: %s UTC\n", datestr);
3216 err = got_error_from_errno("fprintf");
3220 err = add_line_offset(line_offsets, nlines, outoff);
3224 author = got_object_commit_get_author(commit);
3225 committer = got_object_commit_get_committer(commit);
3226 if (strcmp(author, committer) != 0) {
3227 n = fprintf(outfile, "via: %s\n", committer);
3229 err = got_error_from_errno("fprintf");
3233 err = add_line_offset(line_offsets, nlines, outoff);
3237 if (got_object_commit_get_nparents(commit) > 1) {
3238 const struct got_object_id_queue *parent_ids;
3239 struct got_object_qid *qid;
3241 parent_ids = got_object_commit_get_parent_ids(commit);
3242 STAILQ_FOREACH(qid, parent_ids, entry) {
3243 err = got_object_id_str(&id_str, qid->id);
3246 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3248 err = got_error_from_errno("fprintf");
3252 err = add_line_offset(line_offsets, nlines, outoff);
3260 err = got_object_commit_get_logmsg(&logmsg, commit);
3264 while ((line = strsep(&s, "\n")) != NULL) {
3265 n = fprintf(outfile, "%s\n", line);
3267 err = got_error_from_errno("fprintf");
3271 err = add_line_offset(line_offsets, nlines, outoff);
3276 err = get_changed_paths(&changed_paths, commit, repo);
3279 TAILQ_FOREACH(pe, &changed_paths, entry) {
3280 struct got_diff_changed_path *cp = pe->data;
3281 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3283 err = got_error_from_errno("fprintf");
3287 err = add_line_offset(line_offsets, nlines, outoff);
3290 free((char *)pe->path);
3294 fputc('\n', outfile);
3296 err = add_line_offset(line_offsets, nlines, outoff);
3298 got_pathlist_free(&changed_paths);
3302 got_object_commit_close(commit);
3304 free(*line_offsets);
3305 *line_offsets = NULL;
3311 static const struct got_error *
3312 create_diff(struct tog_diff_view_state *s)
3314 const struct got_error *err = NULL;
3318 free(s->line_offsets);
3319 s->line_offsets = malloc(sizeof(off_t));
3320 if (s->line_offsets == NULL)
3321 return got_error_from_errno("malloc");
3326 err = got_error_from_errno("got_opentemp");
3329 if (s->f && fclose(s->f) == EOF) {
3330 err = got_error_from_errno("fclose");
3336 err = got_object_get_type(&obj_type, s->repo, s->id1);
3338 err = got_object_get_type(&obj_type, s->repo, s->id2);
3343 case GOT_OBJ_TYPE_BLOB:
3344 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3345 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3346 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3348 case GOT_OBJ_TYPE_TREE:
3349 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3350 s->id1, s->id2, "", "", s->diff_context,
3351 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3353 case GOT_OBJ_TYPE_COMMIT: {
3354 const struct got_object_id_queue *parent_ids;
3355 struct got_object_qid *pid;
3356 struct got_commit_object *commit2;
3357 struct got_reflist_head *refs;
3359 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3362 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3363 /* Show commit info if we're diffing to a parent/root commit. */
3364 if (s->id1 == NULL) {
3365 err = write_commit_info(&s->line_offsets, &s->nlines,
3366 s->id2, refs, s->repo, s->f);
3370 parent_ids = got_object_commit_get_parent_ids(commit2);
3371 STAILQ_FOREACH(pid, parent_ids, entry) {
3372 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3373 err = write_commit_info(
3374 &s->line_offsets, &s->nlines,
3375 s->id2, refs, s->repo, s->f);
3382 got_object_commit_close(commit2);
3384 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3385 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3386 s->force_text_diff, s->repo, s->f);
3390 err = got_error(GOT_ERR_OBJ_TYPE);
3396 if (s->f && fflush(s->f) != 0 && err == NULL)
3397 err = got_error_from_errno("fflush");
3402 diff_view_indicate_progress(struct tog_view *view)
3404 mvwaddstr(view->window, 0, 0, "diffing...");
3409 static const struct got_error *
3410 search_start_diff_view(struct tog_view *view)
3412 struct tog_diff_view_state *s = &view->state.diff;
3414 s->matched_line = 0;
3418 static const struct got_error *
3419 search_next_diff_view(struct tog_view *view)
3421 struct tog_diff_view_state *s = &view->state.diff;
3424 size_t linesize = 0;
3427 if (!view->searching) {
3428 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3432 if (s->matched_line) {
3433 if (view->searching == TOG_SEARCH_FORWARD)
3434 lineno = s->matched_line + 1;
3436 lineno = s->matched_line - 1;
3438 if (view->searching == TOG_SEARCH_FORWARD)
3447 if (lineno <= 0 || lineno > s->nlines) {
3448 if (s->matched_line == 0) {
3449 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3453 if (view->searching == TOG_SEARCH_FORWARD)
3459 offset = s->line_offsets[lineno - 1];
3460 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3462 return got_error_from_errno("fseeko");
3464 linelen = getline(&line, &linesize, s->f);
3465 if (linelen != -1 &&
3466 match_line(line, &view->regex, 1, &view->regmatch)) {
3467 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3468 s->matched_line = lineno;
3471 if (view->searching == TOG_SEARCH_FORWARD)
3478 if (s->matched_line) {
3479 s->first_displayed_line = s->matched_line;
3480 s->selected_line = 1;
3486 static const struct got_error *
3487 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3488 struct got_object_id *id2, const char *label1, const char *label2,
3489 int diff_context, int ignore_whitespace, int force_text_diff,
3490 struct tog_view *log_view, struct got_repository *repo)
3492 const struct got_error *err;
3493 struct tog_diff_view_state *s = &view->state.diff;
3495 if (id1 != NULL && id2 != NULL) {
3497 err = got_object_get_type(&type1, repo, id1);
3500 err = got_object_get_type(&type2, repo, id2);
3505 return got_error(GOT_ERR_OBJ_TYPE);
3507 s->first_displayed_line = 1;
3508 s->last_displayed_line = view->nlines;
3509 s->selected_line = 1;
3517 s->id1 = got_object_id_dup(id1);
3519 return got_error_from_errno("got_object_id_dup");
3523 s->id2 = got_object_id_dup(id2);
3524 if (s->id2 == NULL) {
3527 return got_error_from_errno("got_object_id_dup");
3530 s->first_displayed_line = 1;
3531 s->last_displayed_line = view->nlines;
3532 s->diff_context = diff_context;
3533 s->ignore_whitespace = ignore_whitespace;
3534 s->force_text_diff = force_text_diff;
3535 s->log_view = log_view;
3538 STAILQ_INIT(&s->colors);
3539 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3540 err = add_color(&s->colors,
3541 "^-", TOG_COLOR_DIFF_MINUS,
3542 get_color_value("TOG_COLOR_DIFF_MINUS"));
3545 err = add_color(&s->colors, "^\\+",
3546 TOG_COLOR_DIFF_PLUS,
3547 get_color_value("TOG_COLOR_DIFF_PLUS"));
3549 free_colors(&s->colors);
3552 err = add_color(&s->colors,
3553 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3554 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3556 free_colors(&s->colors);
3560 err = add_color(&s->colors,
3561 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3562 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3563 get_color_value("TOG_COLOR_DIFF_META"));
3565 free_colors(&s->colors);
3569 err = add_color(&s->colors,
3570 "^(from|via): ", TOG_COLOR_AUTHOR,
3571 get_color_value("TOG_COLOR_AUTHOR"));
3573 free_colors(&s->colors);
3577 err = add_color(&s->colors,
3578 "^date: ", TOG_COLOR_DATE,
3579 get_color_value("TOG_COLOR_DATE"));
3581 free_colors(&s->colors);
3586 if (log_view && view_is_splitscreen(view))
3587 show_log_view(log_view); /* draw vborder */
3588 diff_view_indicate_progress(view);
3590 s->line_offsets = NULL;
3592 err = create_diff(s);
3598 free_colors(&s->colors);
3602 view->show = show_diff_view;
3603 view->input = input_diff_view;
3604 view->close = close_diff_view;
3605 view->search_start = search_start_diff_view;
3606 view->search_next = search_next_diff_view;
3611 static const struct got_error *
3612 close_diff_view(struct tog_view *view)
3614 const struct got_error *err = NULL;
3615 struct tog_diff_view_state *s = &view->state.diff;
3621 if (s->f && fclose(s->f) == EOF)
3622 err = got_error_from_errno("fclose");
3623 free_colors(&s->colors);
3624 free(s->line_offsets);
3625 s->line_offsets = NULL;
3630 static const struct got_error *
3631 show_diff_view(struct tog_view *view)
3633 const struct got_error *err;
3634 struct tog_diff_view_state *s = &view->state.diff;
3635 char *id_str1 = NULL, *id_str2, *header;
3636 const char *label1, *label2;
3639 err = got_object_id_str(&id_str1, s->id1);
3642 label1 = s->label1 ? : id_str1;
3644 label1 = "/dev/null";
3646 err = got_object_id_str(&id_str2, s->id2);
3649 label2 = s->label2 ? : id_str2;
3651 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3652 err = got_error_from_errno("asprintf");
3660 err = draw_file(view, header);
3665 static const struct got_error *
3666 set_selected_commit(struct tog_diff_view_state *s,
3667 struct commit_queue_entry *entry)
3669 const struct got_error *err;
3670 const struct got_object_id_queue *parent_ids;
3671 struct got_commit_object *selected_commit;
3672 struct got_object_qid *pid;
3675 s->id2 = got_object_id_dup(entry->id);
3677 return got_error_from_errno("got_object_id_dup");
3679 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3682 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3684 pid = STAILQ_FIRST(parent_ids);
3685 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3686 got_object_commit_close(selected_commit);
3690 static const struct got_error *
3691 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3693 const struct got_error *err = NULL;
3694 struct tog_diff_view_state *s = &view->state.diff;
3695 struct tog_log_view_state *ls;
3696 struct commit_queue_entry *old_selected_entry;
3698 size_t linesize = 0;
3706 s->force_text_diff = !s->force_text_diff;
3708 s->ignore_whitespace = !s->ignore_whitespace;
3709 wclear(view->window);
3710 s->first_displayed_line = 1;
3711 s->last_displayed_line = view->nlines;
3712 diff_view_indicate_progress(view);
3713 err = create_diff(s);
3717 s->first_displayed_line = 1;
3724 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3729 if (s->first_displayed_line > 1)
3730 s->first_displayed_line--;
3734 if (s->first_displayed_line == 1)
3737 while (i++ < view->nlines - 1 &&
3738 s->first_displayed_line > 1)
3739 s->first_displayed_line--;
3744 s->first_displayed_line++;
3752 while (!s->eof && i++ < view->nlines - 1) {
3753 linelen = getline(&line, &linesize, s->f);
3754 s->first_displayed_line++;
3755 if (linelen == -1) {
3759 err = got_ferror(s->f, GOT_ERR_IO);
3766 if (s->diff_context > 0) {
3768 diff_view_indicate_progress(view);
3769 err = create_diff(s);
3770 if (s->first_displayed_line + view->nlines - 1 >
3772 s->first_displayed_line = 1;
3773 s->last_displayed_line = view->nlines;
3778 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3780 diff_view_indicate_progress(view);
3781 err = create_diff(s);
3786 if (s->log_view == NULL)
3788 ls = &s->log_view->state.log;
3789 old_selected_entry = ls->selected_entry;
3791 err = input_log_view(NULL, s->log_view, KEY_UP);
3795 if (old_selected_entry == ls->selected_entry)
3798 err = set_selected_commit(s, ls->selected_entry);
3802 s->first_displayed_line = 1;
3803 s->last_displayed_line = view->nlines;
3805 diff_view_indicate_progress(view);
3806 err = create_diff(s);
3810 if (s->log_view == NULL)
3812 ls = &s->log_view->state.log;
3813 old_selected_entry = ls->selected_entry;
3815 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3819 if (old_selected_entry == ls->selected_entry)
3822 err = set_selected_commit(s, ls->selected_entry);
3826 s->first_displayed_line = 1;
3827 s->last_displayed_line = view->nlines;
3829 diff_view_indicate_progress(view);
3830 err = create_diff(s);
3839 static const struct got_error *
3840 cmd_diff(int argc, char *argv[])
3842 const struct got_error *error = NULL;
3843 struct got_repository *repo = NULL;
3844 struct got_worktree *worktree = NULL;
3845 struct got_object_id *id1 = NULL, *id2 = NULL;
3846 char *repo_path = NULL, *cwd = NULL;
3847 char *id_str1 = NULL, *id_str2 = NULL;
3848 char *label1 = NULL, *label2 = NULL;
3849 int diff_context = 3, ignore_whitespace = 0;
3850 int ch, force_text_diff = 0;
3852 struct tog_view *view;
3854 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3857 force_text_diff = 1;
3860 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3863 err(1, "-C option %s", errstr);
3866 repo_path = realpath(optarg, NULL);
3867 if (repo_path == NULL)
3868 return got_error_from_errno2("realpath",
3870 got_path_strip_trailing_slashes(repo_path);
3873 ignore_whitespace = 1;
3885 usage_diff(); /* TODO show local worktree changes */
3886 } else if (argc == 2) {
3892 if (repo_path == NULL) {
3893 cwd = getcwd(NULL, 0);
3895 return got_error_from_errno("getcwd");
3896 error = got_worktree_open(&worktree, cwd);
3897 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3901 strdup(got_worktree_get_repo_path(worktree));
3903 repo_path = strdup(cwd);
3904 if (repo_path == NULL) {
3905 error = got_error_from_errno("strdup");
3910 error = got_repo_open(&repo, repo_path, NULL);
3916 error = apply_unveil(got_repo_get_path(repo), NULL);
3920 error = tog_load_refs(repo);
3924 error = got_repo_match_object_id(&id1, &label1, id_str1,
3925 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3929 error = got_repo_match_object_id(&id2, &label2, id_str2,
3930 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3934 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3936 error = got_error_from_errno("view_open");
3939 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3940 ignore_whitespace, force_text_diff, NULL, repo);
3943 error = view_loop(view);
3950 const struct got_error *close_err = got_repo_close(repo);
3955 got_worktree_close(worktree);
3964 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3969 struct tog_blame_line {
3971 struct got_object_id *id;
3974 static const struct got_error *
3975 draw_blame(struct tog_view *view)
3977 struct tog_blame_view_state *s = &view->state.blame;
3978 struct tog_blame *blame = &s->blame;
3979 regmatch_t *regmatch = &view->regmatch;
3980 const struct got_error *err;
3981 int lineno = 0, nprinted = 0;
3983 size_t linesize = 0;
3987 struct tog_blame_line *blame_line;
3988 struct got_object_id *prev_id = NULL;
3990 struct tog_color *tc;
3992 err = got_object_id_str(&id_str, s->blamed_commit->id);
3997 werase(view->window);
3999 if (asprintf(&line, "commit %s", id_str) == -1) {
4000 err = got_error_from_errno("asprintf");
4005 err = format_line(&wline, &width, line, view->ncols, 0);
4010 if (view_needs_focus_indication(view))
4011 wstandout(view->window);
4012 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4014 wattr_on(view->window,
4015 COLOR_PAIR(tc->colorpair), NULL);
4016 waddwstr(view->window, wline);
4018 wattr_off(view->window,
4019 COLOR_PAIR(tc->colorpair), NULL);
4020 if (view_needs_focus_indication(view))
4021 wstandend(view->window);
4024 if (width < view->ncols - 1)
4025 waddch(view->window, '\n');
4027 if (asprintf(&line, "[%d/%d] %s%s",
4028 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4029 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4031 return got_error_from_errno("asprintf");
4034 err = format_line(&wline, &width, line, view->ncols, 0);
4039 waddwstr(view->window, wline);
4042 if (width < view->ncols - 1)
4043 waddch(view->window, '\n');
4046 while (nprinted < view->nlines - 2) {
4047 linelen = getline(&line, &linesize, blame->f);
4048 if (linelen == -1) {
4049 if (feof(blame->f)) {
4054 return got_ferror(blame->f, GOT_ERR_IO);
4056 if (++lineno < s->first_displayed_line)
4059 if (view->focussed && nprinted == s->selected_line - 1)
4060 wstandout(view->window);
4062 if (blame->nlines > 0) {
4063 blame_line = &blame->lines[lineno - 1];
4064 if (blame_line->annotated && prev_id &&
4065 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4067 nprinted == s->selected_line - 1)) {
4068 waddstr(view->window, " ");
4069 } else if (blame_line->annotated) {
4071 err = got_object_id_str(&id_str, blame_line->id);
4076 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4078 wattr_on(view->window,
4079 COLOR_PAIR(tc->colorpair), NULL);
4080 wprintw(view->window, "%.8s", id_str);
4082 wattr_off(view->window,
4083 COLOR_PAIR(tc->colorpair), NULL);
4085 prev_id = blame_line->id;
4087 waddstr(view->window, "........");
4091 waddstr(view->window, "........");
4095 if (view->focussed && nprinted == s->selected_line - 1)
4096 wstandend(view->window);
4097 waddstr(view->window, " ");
4099 if (view->ncols <= 9) {
4101 wline = wcsdup(L"");
4102 if (wline == NULL) {
4103 err = got_error_from_errno("wcsdup");
4107 } else if (s->first_displayed_line + nprinted ==
4109 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4110 err = add_matched_line(&width, line, view->ncols - 9, 9,
4111 view->window, regmatch);
4118 err = format_line(&wline, &width, line,
4119 view->ncols - 9, 9);
4120 waddwstr(view->window, wline);
4126 if (width <= view->ncols - 1)
4127 waddch(view->window, '\n');
4128 if (++nprinted == 1)
4129 s->first_displayed_line = lineno;
4132 s->last_displayed_line = lineno;
4139 static const struct got_error *
4140 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4142 const struct got_error *err = NULL;
4143 struct tog_blame_cb_args *a = arg;
4144 struct tog_blame_line *line;
4147 if (nlines != a->nlines ||
4148 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4149 return got_error(GOT_ERR_RANGE);
4151 errcode = pthread_mutex_lock(&tog_mutex);
4153 return got_error_set_errno(errcode, "pthread_mutex_lock");
4155 if (*a->quit) { /* user has quit the blame view */
4156 err = got_error(GOT_ERR_ITER_COMPLETED);
4161 goto done; /* no change in this commit */
4163 line = &a->lines[lineno - 1];
4164 if (line->annotated)
4167 line->id = got_object_id_dup(id);
4168 if (line->id == NULL) {
4169 err = got_error_from_errno("got_object_id_dup");
4172 line->annotated = 1;
4174 errcode = pthread_mutex_unlock(&tog_mutex);
4176 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4181 blame_thread(void *arg)
4183 const struct got_error *err, *close_err;
4184 struct tog_blame_thread_args *ta = arg;
4185 struct tog_blame_cb_args *a = ta->cb_args;
4188 err = block_signals_used_by_main_thread();
4192 err = got_blame(ta->path, a->commit_id, ta->repo,
4193 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4194 if (err && err->code == GOT_ERR_CANCELLED)
4197 errcode = pthread_mutex_lock(&tog_mutex);
4199 return (void *)got_error_set_errno(errcode,
4200 "pthread_mutex_lock");
4202 close_err = got_repo_close(ta->repo);
4208 errcode = pthread_mutex_unlock(&tog_mutex);
4209 if (errcode && err == NULL)
4210 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4215 static struct got_object_id *
4216 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4217 int first_displayed_line, int selected_line)
4219 struct tog_blame_line *line;
4224 line = &lines[first_displayed_line - 1 + selected_line - 1];
4225 if (!line->annotated)
4231 static const struct got_error *
4232 stop_blame(struct tog_blame *blame)
4234 const struct got_error *err = NULL;
4237 if (blame->thread) {
4239 errcode = pthread_mutex_unlock(&tog_mutex);
4241 return got_error_set_errno(errcode,
4242 "pthread_mutex_unlock");
4243 errcode = pthread_join(blame->thread, (void **)&err);
4245 return got_error_set_errno(errcode, "pthread_join");
4246 errcode = pthread_mutex_lock(&tog_mutex);
4248 return got_error_set_errno(errcode,
4249 "pthread_mutex_lock");
4250 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4252 blame->thread = 0; //NULL;
4254 if (blame->thread_args.repo) {
4255 const struct got_error *close_err;
4256 close_err = got_repo_close(blame->thread_args.repo);
4259 blame->thread_args.repo = NULL;
4262 if (fclose(blame->f) == EOF && err == NULL)
4263 err = got_error_from_errno("fclose");
4267 for (i = 0; i < blame->nlines; i++)
4268 free(blame->lines[i].id);
4270 blame->lines = NULL;
4272 free(blame->cb_args.commit_id);
4273 blame->cb_args.commit_id = NULL;
4278 static const struct got_error *
4279 cancel_blame_view(void *arg)
4281 const struct got_error *err = NULL;
4285 errcode = pthread_mutex_lock(&tog_mutex);
4287 return got_error_set_errno(errcode,
4288 "pthread_mutex_unlock");
4291 err = got_error(GOT_ERR_CANCELLED);
4293 errcode = pthread_mutex_unlock(&tog_mutex);
4295 return got_error_set_errno(errcode,
4296 "pthread_mutex_lock");
4301 static const struct got_error *
4302 run_blame(struct tog_view *view)
4304 struct tog_blame_view_state *s = &view->state.blame;
4305 struct tog_blame *blame = &s->blame;
4306 const struct got_error *err = NULL;
4307 struct got_blob_object *blob = NULL;
4308 struct got_repository *thread_repo = NULL;
4309 struct got_object_id *obj_id = NULL;
4312 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4317 err = got_object_get_type(&obj_type, s->repo, obj_id);
4321 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4322 err = got_error(GOT_ERR_OBJ_TYPE);
4326 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4329 blame->f = got_opentemp();
4330 if (blame->f == NULL) {
4331 err = got_error_from_errno("got_opentemp");
4334 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4335 &blame->line_offsets, blame->f, blob);
4338 if (blame->nlines == 0) {
4339 s->blame_complete = 1;
4343 /* Don't include \n at EOF in the blame line count. */
4344 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4347 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4348 if (blame->lines == NULL) {
4349 err = got_error_from_errno("calloc");
4353 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4357 blame->cb_args.view = view;
4358 blame->cb_args.lines = blame->lines;
4359 blame->cb_args.nlines = blame->nlines;
4360 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4361 if (blame->cb_args.commit_id == NULL) {
4362 err = got_error_from_errno("got_object_id_dup");
4365 blame->cb_args.quit = &s->done;
4367 blame->thread_args.path = s->path;
4368 blame->thread_args.repo = thread_repo;
4369 blame->thread_args.cb_args = &blame->cb_args;
4370 blame->thread_args.complete = &s->blame_complete;
4371 blame->thread_args.cancel_cb = cancel_blame_view;
4372 blame->thread_args.cancel_arg = &s->done;
4373 s->blame_complete = 0;
4375 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4376 s->first_displayed_line = 1;
4377 s->last_displayed_line = view->nlines;
4378 s->selected_line = 1;
4383 got_object_blob_close(blob);
4390 static const struct got_error *
4391 open_blame_view(struct tog_view *view, char *path,
4392 struct got_object_id *commit_id, struct got_repository *repo)
4394 const struct got_error *err = NULL;
4395 struct tog_blame_view_state *s = &view->state.blame;
4397 STAILQ_INIT(&s->blamed_commits);
4399 s->path = strdup(path);
4400 if (s->path == NULL)
4401 return got_error_from_errno("strdup");
4403 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4409 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4410 s->first_displayed_line = 1;
4411 s->last_displayed_line = view->nlines;
4412 s->selected_line = 1;
4413 s->blame_complete = 0;
4415 s->commit_id = commit_id;
4416 memset(&s->blame, 0, sizeof(s->blame));
4418 STAILQ_INIT(&s->colors);
4419 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4420 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4421 get_color_value("TOG_COLOR_COMMIT"));
4426 view->show = show_blame_view;
4427 view->input = input_blame_view;
4428 view->close = close_blame_view;
4429 view->search_start = search_start_blame_view;
4430 view->search_next = search_next_blame_view;
4432 return run_blame(view);
4435 static const struct got_error *
4436 close_blame_view(struct tog_view *view)
4438 const struct got_error *err = NULL;
4439 struct tog_blame_view_state *s = &view->state.blame;
4441 if (s->blame.thread)
4442 err = stop_blame(&s->blame);
4444 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4445 struct got_object_qid *blamed_commit;
4446 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4447 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4448 got_object_qid_free(blamed_commit);
4452 free_colors(&s->colors);
4457 static const struct got_error *
4458 search_start_blame_view(struct tog_view *view)
4460 struct tog_blame_view_state *s = &view->state.blame;
4462 s->matched_line = 0;
4466 static const struct got_error *
4467 search_next_blame_view(struct tog_view *view)
4469 struct tog_blame_view_state *s = &view->state.blame;
4472 size_t linesize = 0;
4475 if (!view->searching) {
4476 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4480 if (s->matched_line) {
4481 if (view->searching == TOG_SEARCH_FORWARD)
4482 lineno = s->matched_line + 1;
4484 lineno = s->matched_line - 1;
4486 if (view->searching == TOG_SEARCH_FORWARD)
4489 lineno = s->blame.nlines;
4495 if (lineno <= 0 || lineno > s->blame.nlines) {
4496 if (s->matched_line == 0) {
4497 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4501 if (view->searching == TOG_SEARCH_FORWARD)
4504 lineno = s->blame.nlines;
4507 offset = s->blame.line_offsets[lineno - 1];
4508 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4510 return got_error_from_errno("fseeko");
4512 linelen = getline(&line, &linesize, s->blame.f);
4513 if (linelen != -1 &&
4514 match_line(line, &view->regex, 1, &view->regmatch)) {
4515 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4516 s->matched_line = lineno;
4519 if (view->searching == TOG_SEARCH_FORWARD)
4526 if (s->matched_line) {
4527 s->first_displayed_line = s->matched_line;
4528 s->selected_line = 1;
4534 static const struct got_error *
4535 show_blame_view(struct tog_view *view)
4537 const struct got_error *err = NULL;
4538 struct tog_blame_view_state *s = &view->state.blame;
4541 if (s->blame.thread == 0 && !s->blame_complete) {
4542 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4543 &s->blame.thread_args);
4545 return got_error_set_errno(errcode, "pthread_create");
4547 halfdelay(1); /* fast refresh while annotating */
4550 if (s->blame_complete)
4551 halfdelay(10); /* disable fast refresh */
4553 err = draw_blame(view);
4559 static const struct got_error *
4560 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4562 const struct got_error *err = NULL, *thread_err = NULL;
4563 struct tog_view *diff_view;
4564 struct tog_blame_view_state *s = &view->state.blame;
4573 s->selected_line = 1;
4574 s->first_displayed_line = 1;
4578 if (s->blame.nlines < view->nlines - 2) {
4579 s->selected_line = s->blame.nlines;
4580 s->first_displayed_line = 1;
4582 s->selected_line = view->nlines - 2;
4583 s->first_displayed_line = s->blame.nlines -
4589 if (s->selected_line > 1)
4591 else if (s->selected_line == 1 &&
4592 s->first_displayed_line > 1)
4593 s->first_displayed_line--;
4597 if (s->first_displayed_line == 1) {
4598 s->selected_line = 1;
4601 if (s->first_displayed_line > view->nlines - 2)
4602 s->first_displayed_line -=
4605 s->first_displayed_line = 1;
4609 if (s->selected_line < view->nlines - 2 &&
4610 s->first_displayed_line +
4611 s->selected_line <= s->blame.nlines)
4613 else if (s->last_displayed_line <
4615 s->first_displayed_line++;
4619 struct got_object_id *id = NULL;
4620 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4621 s->first_displayed_line, s->selected_line);
4625 struct got_commit_object *commit;
4626 struct got_object_qid *pid;
4627 struct got_object_id *blob_id = NULL;
4629 err = got_object_open_as_commit(&commit,
4634 got_object_commit_get_parent_ids(commit));
4636 got_object_commit_close(commit);
4639 /* Check if path history ends here. */
4640 err = got_object_id_by_path(&blob_id, s->repo,
4643 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4645 got_object_commit_close(commit);
4648 err = got_object_get_type(&obj_type, s->repo,
4651 /* Can't blame non-blob type objects. */
4652 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4653 got_object_commit_close(commit);
4656 err = got_object_qid_alloc(&s->blamed_commit,
4658 got_object_commit_close(commit);
4660 if (got_object_id_cmp(id,
4661 s->blamed_commit->id) == 0)
4663 err = got_object_qid_alloc(&s->blamed_commit,
4669 thread_err = stop_blame(&s->blame);
4673 STAILQ_INSERT_HEAD(&s->blamed_commits,
4674 s->blamed_commit, entry);
4675 err = run_blame(view);
4681 struct got_object_qid *first;
4682 first = STAILQ_FIRST(&s->blamed_commits);
4683 if (!got_object_id_cmp(first->id, s->commit_id))
4686 thread_err = stop_blame(&s->blame);
4690 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4691 got_object_qid_free(s->blamed_commit);
4693 STAILQ_FIRST(&s->blamed_commits);
4694 err = run_blame(view);
4701 struct got_object_id *id = NULL;
4702 struct got_object_qid *pid;
4703 struct got_commit_object *commit = NULL;
4704 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4705 s->first_displayed_line, s->selected_line);
4708 err = got_object_open_as_commit(&commit, s->repo, id);
4712 got_object_commit_get_parent_ids(commit));
4713 if (view_is_parent_view(view))
4714 begin_x = view_split_begin_x(view->begin_x);
4715 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4716 if (diff_view == NULL) {
4717 got_object_commit_close(commit);
4718 err = got_error_from_errno("view_open");
4721 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4722 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4723 got_object_commit_close(commit);
4725 view_close(diff_view);
4729 diff_view->focussed = 1;
4730 if (view_is_parent_view(view)) {
4731 err = view_close_child(view);
4734 view_set_child(view, diff_view);
4735 view->focus_child = 1;
4737 *new_view = diff_view;
4745 if (s->last_displayed_line >= s->blame.nlines &&
4746 s->selected_line >= MIN(s->blame.nlines,
4747 view->nlines - 2)) {
4750 if (s->last_displayed_line >= s->blame.nlines &&
4751 s->selected_line < view->nlines - 2) {
4752 s->selected_line = MIN(s->blame.nlines,
4756 if (s->last_displayed_line + view->nlines - 2
4758 s->first_displayed_line +=
4761 s->first_displayed_line =
4766 if (s->selected_line > view->nlines - 2) {
4767 s->selected_line = MIN(s->blame.nlines,
4774 return thread_err ? thread_err : err;
4777 static const struct got_error *
4778 cmd_blame(int argc, char *argv[])
4780 const struct got_error *error;
4781 struct got_repository *repo = NULL;
4782 struct got_worktree *worktree = NULL;
4783 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4784 char *link_target = NULL;
4785 struct got_object_id *commit_id = NULL;
4786 char *commit_id_str = NULL;
4788 struct tog_view *view;
4790 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4793 commit_id_str = optarg;
4796 repo_path = realpath(optarg, NULL);
4797 if (repo_path == NULL)
4798 return got_error_from_errno2("realpath",
4813 if (repo_path == NULL) {
4814 cwd = getcwd(NULL, 0);
4816 return got_error_from_errno("getcwd");
4817 error = got_worktree_open(&worktree, cwd);
4818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4822 strdup(got_worktree_get_repo_path(worktree));
4824 repo_path = strdup(cwd);
4825 if (repo_path == NULL) {
4826 error = got_error_from_errno("strdup");
4831 error = got_repo_open(&repo, repo_path, NULL);
4835 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4842 error = apply_unveil(got_repo_get_path(repo), NULL);
4846 error = tog_load_refs(repo);
4850 if (commit_id_str == NULL) {
4851 struct got_reference *head_ref;
4852 error = got_ref_open(&head_ref, repo, worktree ?
4853 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4856 error = got_ref_resolve(&commit_id, repo, head_ref);
4857 got_ref_close(head_ref);
4859 error = got_repo_match_object_id(&commit_id, NULL,
4860 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4865 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4867 error = got_error_from_errno("view_open");
4871 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4876 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4881 /* Release work tree lock. */
4882 got_worktree_close(worktree);
4885 error = view_loop(view);
4893 got_worktree_close(worktree);
4895 const struct got_error *close_err = got_repo_close(repo);
4903 static const struct got_error *
4904 draw_tree_entries(struct tog_view *view, const char *parent_path)
4906 struct tog_tree_view_state *s = &view->state.tree;
4907 const struct got_error *err = NULL;
4908 struct got_tree_entry *te;
4910 struct tog_color *tc;
4911 int width, n, i, nentries;
4912 int limit = view->nlines;
4916 werase(view->window);
4921 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4924 if (view_needs_focus_indication(view))
4925 wstandout(view->window);
4926 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4928 wattr_on(view->window,
4929 COLOR_PAIR(tc->colorpair), NULL);
4930 waddwstr(view->window, wline);
4932 wattr_off(view->window,
4933 COLOR_PAIR(tc->colorpair), NULL);
4934 if (view_needs_focus_indication(view))
4935 wstandend(view->window);
4938 if (width < view->ncols - 1)
4939 waddch(view->window, '\n');
4942 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4945 waddwstr(view->window, wline);
4948 if (width < view->ncols - 1)
4949 waddch(view->window, '\n');
4952 waddch(view->window, '\n');
4956 if (s->first_displayed_entry == NULL) {
4957 te = got_object_tree_get_first_entry(s->tree);
4958 if (s->selected == 0) {
4960 wstandout(view->window);
4961 s->selected_entry = NULL;
4963 waddstr(view->window, " ..\n"); /* parent directory */
4964 if (s->selected == 0 && view->focussed)
4965 wstandend(view->window);
4972 te = s->first_displayed_entry;
4975 nentries = got_object_tree_get_nentries(s->tree);
4976 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4977 char *line = NULL, *id_str = NULL, *link_target = NULL;
4978 const char *modestr = "";
4981 te = got_object_tree_get_entry(s->tree, i);
4982 mode = got_tree_entry_get_mode(te);
4985 err = got_object_id_str(&id_str,
4986 got_tree_entry_get_id(te));
4988 return got_error_from_errno(
4989 "got_object_id_str");
4991 if (got_object_tree_entry_is_submodule(te))
4993 else if (S_ISLNK(mode)) {
4996 err = got_tree_entry_get_symlink_target(&link_target,
5002 for (i = 0; i < strlen(link_target); i++) {
5003 if (!isprint((unsigned char)link_target[i]))
5004 link_target[i] = '?';
5008 else if (S_ISDIR(mode))
5010 else if (mode & S_IXUSR)
5012 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5013 got_tree_entry_get_name(te), modestr,
5014 link_target ? " -> ": "",
5015 link_target ? link_target : "") == -1) {
5018 return got_error_from_errno("asprintf");
5022 err = format_line(&wline, &width, line, view->ncols, 0);
5027 if (n == s->selected) {
5029 wstandout(view->window);
5030 s->selected_entry = te;
5032 tc = match_color(&s->colors, line);
5034 wattr_on(view->window,
5035 COLOR_PAIR(tc->colorpair), NULL);
5036 waddwstr(view->window, wline);
5038 wattr_off(view->window,
5039 COLOR_PAIR(tc->colorpair), NULL);
5040 if (width < view->ncols - 1)
5041 waddch(view->window, '\n');
5042 if (n == s->selected && view->focussed)
5043 wstandend(view->window);
5049 s->last_displayed_entry = te;
5058 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5060 struct got_tree_entry *te;
5061 int isroot = s->tree == s->root;
5064 if (s->first_displayed_entry == NULL)
5067 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5068 while (i++ < maxscroll) {
5071 s->first_displayed_entry = NULL;
5074 s->first_displayed_entry = te;
5075 te = got_tree_entry_get_prev(s->tree, te);
5080 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5082 struct got_tree_entry *next, *last;
5085 if (s->first_displayed_entry)
5086 next = got_tree_entry_get_next(s->tree,
5087 s->first_displayed_entry);
5089 next = got_object_tree_get_first_entry(s->tree);
5091 last = s->last_displayed_entry;
5092 while (next && last && n++ < maxscroll) {
5093 last = got_tree_entry_get_next(s->tree, last);
5095 s->first_displayed_entry = next;
5096 next = got_tree_entry_get_next(s->tree, next);
5101 static const struct got_error *
5102 tree_entry_path(char **path, struct tog_parent_trees *parents,
5103 struct got_tree_entry *te)
5105 const struct got_error *err = NULL;
5106 struct tog_parent_tree *pt;
5107 size_t len = 2; /* for leading slash and NUL */
5109 TAILQ_FOREACH(pt, parents, entry)
5110 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5113 len += strlen(got_tree_entry_get_name(te));
5115 *path = calloc(1, len);
5117 return got_error_from_errno("calloc");
5120 pt = TAILQ_LAST(parents, tog_parent_trees);
5122 const char *name = got_tree_entry_get_name(pt->selected_entry);
5123 if (strlcat(*path, name, len) >= len) {
5124 err = got_error(GOT_ERR_NO_SPACE);
5127 if (strlcat(*path, "/", len) >= len) {
5128 err = got_error(GOT_ERR_NO_SPACE);
5131 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5134 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5135 err = got_error(GOT_ERR_NO_SPACE);
5147 static const struct got_error *
5148 blame_tree_entry(struct tog_view **new_view, int begin_x,
5149 struct got_tree_entry *te, struct tog_parent_trees *parents,
5150 struct got_object_id *commit_id, struct got_repository *repo)
5152 const struct got_error *err = NULL;
5154 struct tog_view *blame_view;
5158 err = tree_entry_path(&path, parents, te);
5162 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5163 if (blame_view == NULL) {
5164 err = got_error_from_errno("view_open");
5168 err = open_blame_view(blame_view, path, commit_id, repo);
5170 if (err->code == GOT_ERR_CANCELLED)
5172 view_close(blame_view);
5174 *new_view = blame_view;
5180 static const struct got_error *
5181 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5182 struct tog_tree_view_state *s)
5184 struct tog_view *log_view;
5185 const struct got_error *err = NULL;
5190 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5191 if (log_view == NULL)
5192 return got_error_from_errno("view_open");
5194 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5198 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5201 view_close(log_view);
5203 *new_view = log_view;
5208 static const struct got_error *
5209 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5210 const char *head_ref_name, struct got_repository *repo)
5212 const struct got_error *err = NULL;
5213 char *commit_id_str = NULL;
5214 struct tog_tree_view_state *s = &view->state.tree;
5215 struct got_commit_object *commit = NULL;
5217 TAILQ_INIT(&s->parents);
5218 STAILQ_INIT(&s->colors);
5220 s->commit_id = got_object_id_dup(commit_id);
5221 if (s->commit_id == NULL)
5222 return got_error_from_errno("got_object_id_dup");
5224 err = got_object_open_as_commit(&commit, repo, commit_id);
5229 * The root is opened here and will be closed when the view is closed.
5230 * Any visited subtrees and their path-wise parents are opened and
5233 err = got_object_open_as_tree(&s->root, repo,
5234 got_object_commit_get_tree_id(commit));
5239 err = got_object_id_str(&commit_id_str, commit_id);
5243 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5244 err = got_error_from_errno("asprintf");
5248 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5249 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5250 if (head_ref_name) {
5251 s->head_ref_name = strdup(head_ref_name);
5252 if (s->head_ref_name == NULL) {
5253 err = got_error_from_errno("strdup");
5259 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5260 err = add_color(&s->colors, "\\$$",
5261 TOG_COLOR_TREE_SUBMODULE,
5262 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5265 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5266 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5269 err = add_color(&s->colors, "/$",
5270 TOG_COLOR_TREE_DIRECTORY,
5271 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5275 err = add_color(&s->colors, "\\*$",
5276 TOG_COLOR_TREE_EXECUTABLE,
5277 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5281 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5282 get_color_value("TOG_COLOR_COMMIT"));
5287 view->show = show_tree_view;
5288 view->input = input_tree_view;
5289 view->close = close_tree_view;
5290 view->search_start = search_start_tree_view;
5291 view->search_next = search_next_tree_view;
5293 free(commit_id_str);
5295 got_object_commit_close(commit);
5297 close_tree_view(view);
5301 static const struct got_error *
5302 close_tree_view(struct tog_view *view)
5304 struct tog_tree_view_state *s = &view->state.tree;
5306 free_colors(&s->colors);
5307 free(s->tree_label);
5308 s->tree_label = NULL;
5310 s->commit_id = NULL;
5311 free(s->head_ref_name);
5312 s->head_ref_name = NULL;
5313 while (!TAILQ_EMPTY(&s->parents)) {
5314 struct tog_parent_tree *parent;
5315 parent = TAILQ_FIRST(&s->parents);
5316 TAILQ_REMOVE(&s->parents, parent, entry);
5317 if (parent->tree != s->root)
5318 got_object_tree_close(parent->tree);
5322 if (s->tree != NULL && s->tree != s->root)
5323 got_object_tree_close(s->tree);
5325 got_object_tree_close(s->root);
5329 static const struct got_error *
5330 search_start_tree_view(struct tog_view *view)
5332 struct tog_tree_view_state *s = &view->state.tree;
5334 s->matched_entry = NULL;
5339 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5341 regmatch_t regmatch;
5343 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5347 static const struct got_error *
5348 search_next_tree_view(struct tog_view *view)
5350 struct tog_tree_view_state *s = &view->state.tree;
5351 struct got_tree_entry *te = NULL;
5353 if (!view->searching) {
5354 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5358 if (s->matched_entry) {
5359 if (view->searching == TOG_SEARCH_FORWARD) {
5360 if (s->selected_entry)
5361 te = got_tree_entry_get_next(s->tree,
5364 te = got_object_tree_get_first_entry(s->tree);
5366 if (s->selected_entry == NULL)
5367 te = got_object_tree_get_last_entry(s->tree);
5369 te = got_tree_entry_get_prev(s->tree,
5373 if (view->searching == TOG_SEARCH_FORWARD)
5374 te = got_object_tree_get_first_entry(s->tree);
5376 te = got_object_tree_get_last_entry(s->tree);
5381 if (s->matched_entry == NULL) {
5382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5385 if (view->searching == TOG_SEARCH_FORWARD)
5386 te = got_object_tree_get_first_entry(s->tree);
5388 te = got_object_tree_get_last_entry(s->tree);
5391 if (match_tree_entry(te, &view->regex)) {
5392 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5393 s->matched_entry = te;
5397 if (view->searching == TOG_SEARCH_FORWARD)
5398 te = got_tree_entry_get_next(s->tree, te);
5400 te = got_tree_entry_get_prev(s->tree, te);
5403 if (s->matched_entry) {
5404 s->first_displayed_entry = s->matched_entry;
5411 static const struct got_error *
5412 show_tree_view(struct tog_view *view)
5414 const struct got_error *err = NULL;
5415 struct tog_tree_view_state *s = &view->state.tree;
5418 err = tree_entry_path(&parent_path, &s->parents, NULL);
5422 err = draw_tree_entries(view, parent_path);
5429 static const struct got_error *
5430 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5432 const struct got_error *err = NULL;
5433 struct tog_tree_view_state *s = &view->state.tree;
5434 struct tog_view *log_view, *ref_view;
5435 struct got_tree_entry *te;
5440 s->show_ids = !s->show_ids;
5443 if (!s->selected_entry)
5445 if (view_is_parent_view(view))
5446 begin_x = view_split_begin_x(view->begin_x);
5447 err = log_selected_tree_entry(&log_view, begin_x, s);
5449 log_view->focussed = 1;
5450 if (view_is_parent_view(view)) {
5451 err = view_close_child(view);
5454 view_set_child(view, log_view);
5455 view->focus_child = 1;
5457 *new_view = log_view;
5460 if (view_is_parent_view(view))
5461 begin_x = view_split_begin_x(view->begin_x);
5462 ref_view = view_open(view->nlines, view->ncols,
5463 view->begin_y, begin_x, TOG_VIEW_REF);
5464 if (ref_view == NULL)
5465 return got_error_from_errno("view_open");
5466 err = open_ref_view(ref_view, s->repo);
5468 view_close(ref_view);
5472 ref_view->focussed = 1;
5473 if (view_is_parent_view(view)) {
5474 err = view_close_child(view);
5477 view_set_child(view, ref_view);
5478 view->focus_child = 1;
5480 *new_view = ref_view;
5485 if (s->tree == s->root)
5486 s->first_displayed_entry =
5487 got_object_tree_get_first_entry(s->tree);
5489 s->first_displayed_entry = NULL;
5494 te = got_object_tree_get_last_entry(s->tree);
5495 for (n = 0; n < view->nlines - 3; n++) {
5497 if(s->tree != s->root) {
5498 s->first_displayed_entry = NULL;
5503 s->first_displayed_entry = te;
5504 te = got_tree_entry_get_prev(s->tree, te);
5507 s->selected = n - 1;
5511 if (s->selected > 0) {
5515 tree_scroll_up(s, 1);
5519 if (s->tree == s->root) {
5520 if (got_object_tree_get_first_entry(s->tree) ==
5521 s->first_displayed_entry)
5524 if (s->first_displayed_entry == NULL)
5527 tree_scroll_up(s, MAX(0, view->nlines - 3));
5531 if (s->selected < s->ndisplayed - 1) {
5535 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5537 /* can't scroll any further */
5539 tree_scroll_down(s, 1);
5543 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5545 /* can't scroll any further; move cursor down */
5546 if (s->selected < s->ndisplayed - 1)
5547 s->selected = s->ndisplayed - 1;
5550 tree_scroll_down(s, view->nlines - 3);
5555 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5556 struct tog_parent_tree *parent;
5557 /* user selected '..' */
5558 if (s->tree == s->root)
5560 parent = TAILQ_FIRST(&s->parents);
5561 TAILQ_REMOVE(&s->parents, parent,
5563 got_object_tree_close(s->tree);
5564 s->tree = parent->tree;
5565 s->first_displayed_entry =
5566 parent->first_displayed_entry;
5568 parent->selected_entry;
5569 s->selected = parent->selected;
5571 } else if (S_ISDIR(got_tree_entry_get_mode(
5572 s->selected_entry))) {
5573 struct got_tree_object *subtree;
5574 err = got_object_open_as_tree(&subtree, s->repo,
5575 got_tree_entry_get_id(s->selected_entry));
5578 err = tree_view_visit_subtree(s, subtree);
5580 got_object_tree_close(subtree);
5583 } else if (S_ISREG(got_tree_entry_get_mode(
5584 s->selected_entry))) {
5585 struct tog_view *blame_view;
5586 int begin_x = view_is_parent_view(view) ?
5587 view_split_begin_x(view->begin_x) : 0;
5589 err = blame_tree_entry(&blame_view, begin_x,
5590 s->selected_entry, &s->parents,
5591 s->commit_id, s->repo);
5595 blame_view->focussed = 1;
5596 if (view_is_parent_view(view)) {
5597 err = view_close_child(view);
5600 view_set_child(view, blame_view);
5601 view->focus_child = 1;
5603 *new_view = blame_view;
5607 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5608 s->selected = view->nlines - 4;
5621 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5626 static const struct got_error *
5627 cmd_tree(int argc, char *argv[])
5629 const struct got_error *error;
5630 struct got_repository *repo = NULL;
5631 struct got_worktree *worktree = NULL;
5632 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5633 struct got_object_id *commit_id = NULL;
5634 const char *commit_id_arg = NULL;
5636 struct got_reference *ref = NULL;
5637 const char *head_ref_name = NULL;
5639 struct tog_view *view;
5641 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5644 commit_id_arg = optarg;
5647 repo_path = realpath(optarg, NULL);
5648 if (repo_path == NULL)
5649 return got_error_from_errno2("realpath",
5664 if (repo_path == NULL) {
5665 cwd = getcwd(NULL, 0);
5667 return got_error_from_errno("getcwd");
5668 error = got_worktree_open(&worktree, cwd);
5669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5673 strdup(got_worktree_get_repo_path(worktree));
5675 repo_path = strdup(cwd);
5676 if (repo_path == NULL) {
5677 error = got_error_from_errno("strdup");
5682 error = got_repo_open(&repo, repo_path, NULL);
5686 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5693 error = apply_unveil(got_repo_get_path(repo), NULL);
5697 error = tog_load_refs(repo);
5701 if (commit_id_arg == NULL) {
5702 error = got_repo_match_object_id(&commit_id, &label,
5703 worktree ? got_worktree_get_head_ref_name(worktree) :
5704 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5707 head_ref_name = label;
5709 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5711 head_ref_name = got_ref_get_name(ref);
5712 else if (error->code != GOT_ERR_NOT_REF)
5714 error = got_repo_match_object_id(&commit_id, NULL,
5715 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5720 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5722 error = got_error_from_errno("view_open");
5725 error = open_tree_view(view, commit_id, head_ref_name, repo);
5728 if (!got_path_is_root_dir(in_repo_path)) {
5729 error = tree_view_walk_path(&view->state.tree, commit_id,
5736 /* Release work tree lock. */
5737 got_worktree_close(worktree);
5740 error = view_loop(view);
5749 const struct got_error *close_err = got_repo_close(repo);
5757 static const struct got_error *
5758 ref_view_load_refs(struct tog_ref_view_state *s)
5760 struct got_reflist_entry *sre;
5761 struct tog_reflist_entry *re;
5764 TAILQ_FOREACH(sre, &tog_refs, entry) {
5765 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5768 re = malloc(sizeof(*re));
5770 return got_error_from_errno("malloc");
5772 re->ref = got_ref_dup(sre->ref);
5773 if (re->ref == NULL)
5774 return got_error_from_errno("got_ref_dup");
5775 re->idx = s->nrefs++;
5776 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5779 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5784 ref_view_free_refs(struct tog_ref_view_state *s)
5786 struct tog_reflist_entry *re;
5788 while (!TAILQ_EMPTY(&s->refs)) {
5789 re = TAILQ_FIRST(&s->refs);
5790 TAILQ_REMOVE(&s->refs, re, entry);
5791 got_ref_close(re->ref);
5796 static const struct got_error *
5797 open_ref_view(struct tog_view *view, struct got_repository *repo)
5799 const struct got_error *err = NULL;
5800 struct tog_ref_view_state *s = &view->state.ref;
5802 s->selected_entry = 0;
5805 TAILQ_INIT(&s->refs);
5806 STAILQ_INIT(&s->colors);
5808 err = ref_view_load_refs(s);
5812 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5813 err = add_color(&s->colors, "^refs/heads/",
5814 TOG_COLOR_REFS_HEADS,
5815 get_color_value("TOG_COLOR_REFS_HEADS"));
5819 err = add_color(&s->colors, "^refs/tags/",
5820 TOG_COLOR_REFS_TAGS,
5821 get_color_value("TOG_COLOR_REFS_TAGS"));
5825 err = add_color(&s->colors, "^refs/remotes/",
5826 TOG_COLOR_REFS_REMOTES,
5827 get_color_value("TOG_COLOR_REFS_REMOTES"));
5832 view->show = show_ref_view;
5833 view->input = input_ref_view;
5834 view->close = close_ref_view;
5835 view->search_start = search_start_ref_view;
5836 view->search_next = search_next_ref_view;
5839 free_colors(&s->colors);
5843 static const struct got_error *
5844 close_ref_view(struct tog_view *view)
5846 struct tog_ref_view_state *s = &view->state.ref;
5848 ref_view_free_refs(s);
5849 free_colors(&s->colors);
5854 static const struct got_error *
5855 resolve_reflist_entry(struct got_object_id **commit_id,
5856 struct tog_reflist_entry *re, struct got_repository *repo)
5858 const struct got_error *err = NULL;
5859 struct got_object_id *obj_id;
5860 struct got_tag_object *tag = NULL;
5865 err = got_ref_resolve(&obj_id, repo, re->ref);
5869 err = got_object_get_type(&obj_type, repo, obj_id);
5874 case GOT_OBJ_TYPE_COMMIT:
5875 *commit_id = obj_id;
5877 case GOT_OBJ_TYPE_TAG:
5878 err = got_object_open_as_tag(&tag, repo, obj_id);
5882 err = got_object_get_type(&obj_type, repo,
5883 got_object_tag_get_object_id(tag));
5886 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5887 err = got_error(GOT_ERR_OBJ_TYPE);
5890 *commit_id = got_object_id_dup(
5891 got_object_tag_get_object_id(tag));
5892 if (*commit_id == NULL) {
5893 err = got_error_from_errno("got_object_id_dup");
5898 err = got_error(GOT_ERR_OBJ_TYPE);
5904 got_object_tag_close(tag);
5912 static const struct got_error *
5913 log_ref_entry(struct tog_view **new_view, int begin_x,
5914 struct tog_reflist_entry *re, struct got_repository *repo)
5916 struct tog_view *log_view;
5917 const struct got_error *err = NULL;
5918 struct got_object_id *commit_id = NULL;
5922 err = resolve_reflist_entry(&commit_id, re, repo);
5924 if (err->code != GOT_ERR_OBJ_TYPE)
5930 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5931 if (log_view == NULL) {
5932 err = got_error_from_errno("view_open");
5936 err = open_log_view(log_view, commit_id, repo,
5937 got_ref_get_name(re->ref), "", 0);
5940 view_close(log_view);
5942 *new_view = log_view;
5948 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5950 struct tog_reflist_entry *re;
5953 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5956 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5957 while (i++ < maxscroll) {
5960 s->first_displayed_entry = re;
5961 re = TAILQ_PREV(re, tog_reflist_head, entry);
5966 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5968 struct tog_reflist_entry *next, *last;
5971 if (s->first_displayed_entry)
5972 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5974 next = TAILQ_FIRST(&s->refs);
5976 last = s->last_displayed_entry;
5977 while (next && last && n++ < maxscroll) {
5978 last = TAILQ_NEXT(last, entry);
5980 s->first_displayed_entry = next;
5981 next = TAILQ_NEXT(next, entry);
5986 static const struct got_error *
5987 search_start_ref_view(struct tog_view *view)
5989 struct tog_ref_view_state *s = &view->state.ref;
5991 s->matched_entry = NULL;
5996 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5998 regmatch_t regmatch;
6000 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6004 static const struct got_error *
6005 search_next_ref_view(struct tog_view *view)
6007 struct tog_ref_view_state *s = &view->state.ref;
6008 struct tog_reflist_entry *re = NULL;
6010 if (!view->searching) {
6011 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6015 if (s->matched_entry) {
6016 if (view->searching == TOG_SEARCH_FORWARD) {
6017 if (s->selected_entry)
6018 re = TAILQ_NEXT(s->selected_entry, entry);
6020 re = TAILQ_PREV(s->selected_entry,
6021 tog_reflist_head, entry);
6023 if (s->selected_entry == NULL)
6024 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6026 re = TAILQ_PREV(s->selected_entry,
6027 tog_reflist_head, entry);
6030 if (view->searching == TOG_SEARCH_FORWARD)
6031 re = TAILQ_FIRST(&s->refs);
6033 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6038 if (s->matched_entry == NULL) {
6039 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6042 if (view->searching == TOG_SEARCH_FORWARD)
6043 re = TAILQ_FIRST(&s->refs);
6045 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6048 if (match_reflist_entry(re, &view->regex)) {
6049 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6050 s->matched_entry = re;
6054 if (view->searching == TOG_SEARCH_FORWARD)
6055 re = TAILQ_NEXT(re, entry);
6057 re = TAILQ_PREV(re, tog_reflist_head, entry);
6060 if (s->matched_entry) {
6061 s->first_displayed_entry = s->matched_entry;
6068 static const struct got_error *
6069 show_ref_view(struct tog_view *view)
6071 const struct got_error *err = NULL;
6072 struct tog_ref_view_state *s = &view->state.ref;
6073 struct tog_reflist_entry *re;
6076 struct tog_color *tc;
6078 int limit = view->nlines;
6080 werase(view->window);
6087 re = s->first_displayed_entry;
6089 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6091 return got_error_from_errno("asprintf");
6093 err = format_line(&wline, &width, line, view->ncols, 0);
6098 if (view_needs_focus_indication(view))
6099 wstandout(view->window);
6100 waddwstr(view->window, wline);
6101 if (view_needs_focus_indication(view))
6102 wstandend(view->window);
6107 if (width < view->ncols - 1)
6108 waddch(view->window, '\n');
6113 while (re && limit > 0) {
6116 if (got_ref_is_symbolic(re->ref)) {
6117 if (asprintf(&line, "%s -> %s",
6118 got_ref_get_name(re->ref),
6119 got_ref_get_symref_target(re->ref)) == -1)
6120 return got_error_from_errno("asprintf");
6121 } else if (s->show_ids) {
6122 struct got_object_id *id;
6124 err = got_ref_resolve(&id, s->repo, re->ref);
6127 err = got_object_id_str(&id_str, id);
6132 if (asprintf(&line, "%s: %s",
6133 got_ref_get_name(re->ref), id_str) == -1) {
6134 err = got_error_from_errno("asprintf");
6142 line = strdup(got_ref_get_name(re->ref));
6144 return got_error_from_errno("strdup");
6147 err = format_line(&wline, &width, line, view->ncols, 0);
6152 if (n == s->selected) {
6154 wstandout(view->window);
6155 s->selected_entry = re;
6157 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6159 wattr_on(view->window,
6160 COLOR_PAIR(tc->colorpair), NULL);
6161 waddwstr(view->window, wline);
6163 wattr_off(view->window,
6164 COLOR_PAIR(tc->colorpair), NULL);
6165 if (width < view->ncols - 1)
6166 waddch(view->window, '\n');
6167 if (n == s->selected && view->focussed)
6168 wstandend(view->window);
6174 s->last_displayed_entry = re;
6177 re = TAILQ_NEXT(re, entry);
6184 static const struct got_error *
6185 browse_ref_tree(struct tog_view **new_view, int begin_x,
6186 struct tog_reflist_entry *re, struct got_repository *repo)
6188 const struct got_error *err = NULL;
6189 struct got_object_id *commit_id = NULL;
6190 struct tog_view *tree_view;
6194 err = resolve_reflist_entry(&commit_id, re, repo);
6196 if (err->code != GOT_ERR_OBJ_TYPE)
6203 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6204 if (tree_view == NULL) {
6205 err = got_error_from_errno("view_open");
6209 err = open_tree_view(tree_view, commit_id,
6210 got_ref_get_name(re->ref), repo);
6214 *new_view = tree_view;
6219 static const struct got_error *
6220 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6222 const struct got_error *err = NULL;
6223 struct tog_ref_view_state *s = &view->state.ref;
6224 struct tog_view *log_view, *tree_view;
6225 struct tog_reflist_entry *re;
6230 s->show_ids = !s->show_ids;
6234 if (!s->selected_entry)
6236 if (view_is_parent_view(view))
6237 begin_x = view_split_begin_x(view->begin_x);
6238 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6241 log_view->focussed = 1;
6242 if (view_is_parent_view(view)) {
6243 err = view_close_child(view);
6246 view_set_child(view, log_view);
6247 view->focus_child = 1;
6249 *new_view = log_view;
6252 if (!s->selected_entry)
6254 if (view_is_parent_view(view))
6255 begin_x = view_split_begin_x(view->begin_x);
6256 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6258 if (err || tree_view == NULL)
6261 tree_view->focussed = 1;
6262 if (view_is_parent_view(view)) {
6263 err = view_close_child(view);
6266 view_set_child(view, tree_view);
6267 view->focus_child = 1;
6269 *new_view = tree_view;
6274 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6279 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6280 for (n = 0; n < view->nlines - 1; n++) {
6283 s->first_displayed_entry = re;
6284 re = TAILQ_PREV(re, tog_reflist_head, entry);
6287 s->selected = n - 1;
6291 if (s->selected > 0) {
6295 ref_scroll_up(s, 1);
6299 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6301 ref_scroll_up(s, MAX(0, view->nlines - 1));
6305 if (s->selected < s->ndisplayed - 1) {
6309 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6310 /* can't scroll any further */
6312 ref_scroll_down(s, 1);
6316 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6317 /* can't scroll any further; move cursor down */
6318 if (s->selected < s->ndisplayed - 1)
6319 s->selected = s->ndisplayed - 1;
6322 ref_scroll_down(s, view->nlines - 1);
6326 err = tog_load_refs(s->repo);
6329 ref_view_free_refs(s);
6330 err = ref_view_load_refs(s);
6333 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6334 s->selected = view->nlines - 2;
6347 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6352 static const struct got_error *
6353 cmd_ref(int argc, char *argv[])
6355 const struct got_error *error;
6356 struct got_repository *repo = NULL;
6357 struct got_worktree *worktree = NULL;
6358 char *cwd = NULL, *repo_path = NULL;
6360 struct tog_view *view;
6362 while ((ch = getopt(argc, argv, "r:")) != -1) {
6365 repo_path = realpath(optarg, NULL);
6366 if (repo_path == NULL)
6367 return got_error_from_errno2("realpath",
6382 if (repo_path == NULL) {
6383 cwd = getcwd(NULL, 0);
6385 return got_error_from_errno("getcwd");
6386 error = got_worktree_open(&worktree, cwd);
6387 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6391 strdup(got_worktree_get_repo_path(worktree));
6393 repo_path = strdup(cwd);
6394 if (repo_path == NULL) {
6395 error = got_error_from_errno("strdup");
6400 error = got_repo_open(&repo, repo_path, NULL);
6406 error = apply_unveil(got_repo_get_path(repo), NULL);
6410 error = tog_load_refs(repo);
6414 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6416 error = got_error_from_errno("view_open");
6420 error = open_ref_view(view, repo);
6425 /* Release work tree lock. */
6426 got_worktree_close(worktree);
6429 error = view_loop(view);
6434 const struct got_error *close_err = got_repo_close(repo);
6443 list_commands(FILE *fp)
6447 fprintf(fp, "commands:");
6448 for (i = 0; i < nitems(tog_commands); i++) {
6449 struct tog_cmd *cmd = &tog_commands[i];
6450 fprintf(fp, " %s", cmd->name);
6456 usage(int hflag, int status)
6458 FILE *fp = (status == 0) ? stdout : stderr;
6460 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6463 fprintf(fp, "lazy usage: %s path\n", getprogname());
6470 make_argv(int argc, ...)
6478 argv = calloc(argc, sizeof(char *));
6481 for (i = 0; i < argc; i++) {
6482 argv[i] = strdup(va_arg(ap, char *));
6483 if (argv[i] == NULL)
6492 * Try to convert 'tog path' into a 'tog log path' command.
6493 * The user could simply have mistyped the command rather than knowingly
6494 * provided a path. So check whether argv[0] can in fact be resolved
6495 * to a path in the HEAD commit and print a special error if not.
6496 * This hack is for mpi@ <3
6498 static const struct got_error *
6499 tog_log_with_path(int argc, char *argv[])
6501 const struct got_error *error = NULL, *close_err;
6502 struct tog_cmd *cmd = NULL;
6503 struct got_repository *repo = NULL;
6504 struct got_worktree *worktree = NULL;
6505 struct got_object_id *commit_id = NULL, *id = NULL;
6506 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6507 char *commit_id_str = NULL, **cmd_argv = NULL;
6509 cwd = getcwd(NULL, 0);
6511 return got_error_from_errno("getcwd");
6513 error = got_worktree_open(&worktree, cwd);
6514 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6518 repo_path = strdup(got_worktree_get_repo_path(worktree));
6520 repo_path = strdup(cwd);
6521 if (repo_path == NULL) {
6522 error = got_error_from_errno("strdup");
6526 error = got_repo_open(&repo, repo_path, NULL);
6530 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6535 error = tog_load_refs(repo);
6538 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6539 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6540 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6545 got_worktree_close(worktree);
6549 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6551 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6553 fprintf(stderr, "%s: '%s' is no known command or path\n",
6554 getprogname(), argv[0]);
6559 close_err = got_repo_close(repo);
6564 error = got_object_id_str(&commit_id_str, commit_id);
6568 cmd = &tog_commands[0]; /* log */
6570 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6571 error = cmd->cmd_main(argc, cmd_argv);
6574 close_err = got_repo_close(repo);
6579 got_worktree_close(worktree);
6581 free(commit_id_str);
6588 for (i = 0; i < argc; i++)
6597 main(int argc, char *argv[])
6599 const struct got_error *error = NULL;
6600 struct tog_cmd *cmd = NULL;
6601 int ch, hflag = 0, Vflag = 0;
6602 char **cmd_argv = NULL;
6603 static struct option longopts[] = {
6604 { "version", no_argument, NULL, 'V' },
6608 setlocale(LC_CTYPE, "");
6610 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6630 got_version_print_str();
6635 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6643 /* Build an argument vector which runs a default command. */
6644 cmd = &tog_commands[0];
6646 cmd_argv = make_argv(argc, cmd->name);
6650 /* Did the user specify a command? */
6651 for (i = 0; i < nitems(tog_commands); i++) {
6652 if (strncmp(tog_commands[i].name, argv[0],
6653 strlen(argv[0])) == 0) {
6654 cmd = &tog_commands[i];
6663 /* No command specified; try log with a path */
6664 error = tog_log_with_path(argc, argv);
6669 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6676 for (i = 0; i < argc; i++)
6681 if (error && error->code != GOT_ERR_CANCELLED)
6682 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);