2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
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"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define CTRL(x) ((x) & 0x1f)
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
78 __dead static void usage(int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
84 static const struct got_error* cmd_log(int, char *[]);
85 static const struct got_error* cmd_diff(int, char *[]);
86 static const struct got_error* cmd_blame(int, char *[]);
87 static const struct got_error* cmd_tree(int, char *[]);
89 static struct tog_cmd tog_commands[] = {
90 { "log", cmd_log, usage_log },
91 { "diff", cmd_diff, usage_diff },
92 { "blame", cmd_blame, usage_blame },
93 { "tree", cmd_tree, usage_tree },
103 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
114 struct commit_queue_head head;
118 SIMPLEQ_ENTRY(tog_color) entry;
122 SIMPLEQ_HEAD(tog_colors, tog_color);
124 static const struct got_error *
125 add_color(struct tog_colors *colors, const char *pattern,
126 int idx, short color)
128 const struct got_error *err = NULL;
129 struct tog_color *tc;
132 if (idx < 1 || idx > COLOR_PAIRS - 1)
135 init_pair(idx, color, -1);
137 tc = calloc(1, sizeof(*tc));
139 return got_error_from_errno("calloc");
140 regerr = regcomp(&tc->regex, pattern,
141 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
143 static char regerr_msg[512];
144 static char err_msg[512];
145 regerror(regerr, &tc->regex, regerr_msg,
147 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
149 err = got_error_msg(GOT_ERR_REGEX, err_msg);
154 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
159 free_colors(struct tog_colors *colors)
161 struct tog_color *tc;
163 while (!SIMPLEQ_EMPTY(colors)) {
164 tc = SIMPLEQ_FIRST(colors);
165 SIMPLEQ_REMOVE_HEAD(colors, entry);
172 get_color(struct tog_colors *colors, int colorpair)
174 struct tog_color *tc = NULL;
176 SIMPLEQ_FOREACH(tc, colors, entry) {
177 if (tc->colorpair == colorpair)
185 default_color_value(const char *envvar)
187 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
188 return COLOR_MAGENTA;
189 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
193 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
195 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
196 return COLOR_MAGENTA;
197 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
198 return COLOR_MAGENTA;
199 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
201 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
203 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
205 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
207 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 get_color_value(const char *envvar)
216 const char *val = getenv(envvar);
219 return default_color_value(envvar);
221 if (strcasecmp(val, "black") == 0)
223 if (strcasecmp(val, "red") == 0)
225 if (strcasecmp(val, "green") == 0)
227 if (strcasecmp(val, "yellow") == 0)
229 if (strcasecmp(val, "blue") == 0)
231 if (strcasecmp(val, "magenta") == 0)
232 return COLOR_MAGENTA;
233 if (strcasecmp(val, "cyan") == 0)
235 if (strcasecmp(val, "white") == 0)
237 if (strcasecmp(val, "default") == 0)
240 return default_color_value(envvar);
244 struct tog_diff_view_state {
245 struct got_object_id *id1, *id2;
247 int first_displayed_line;
248 int last_displayed_line;
251 struct got_repository *repo;
252 struct got_reflist_head *refs;
253 struct tog_colors colors;
260 /* passed from log view; may be NULL */
261 struct tog_view *log_view;
264 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 struct tog_log_thread_args {
267 pthread_cond_t need_commits;
268 pthread_cond_t commit_loaded;
270 struct got_commit_graph *graph;
271 struct commit_queue *commits;
272 const char *in_repo_path;
273 struct got_object_id *start_id;
274 struct got_repository *repo;
277 struct commit_queue_entry **first_displayed_entry;
278 struct commit_queue_entry **selected_entry;
280 int *search_next_done;
284 struct tog_log_view_state {
285 struct commit_queue commits;
286 struct commit_queue_entry *first_displayed_entry;
287 struct commit_queue_entry *last_displayed_entry;
288 struct commit_queue_entry *selected_entry;
291 const char *head_ref_name;
293 struct got_repository *repo;
294 struct got_reflist_head *refs;
295 struct got_object_id *start_id;
298 struct tog_log_thread_args thread_args;
299 struct commit_queue_entry *matched_entry;
300 struct commit_queue_entry *search_entry;
301 struct tog_colors colors;
304 #define TOG_COLOR_DIFF_MINUS 1
305 #define TOG_COLOR_DIFF_PLUS 2
306 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
307 #define TOG_COLOR_DIFF_META 4
308 #define TOG_COLOR_TREE_SUBMODULE 5
309 #define TOG_COLOR_TREE_SYMLINK 6
310 #define TOG_COLOR_TREE_DIRECTORY 7
311 #define TOG_COLOR_TREE_EXECUTABLE 8
312 #define TOG_COLOR_COMMIT 9
313 #define TOG_COLOR_AUTHOR 10
314 #define TOG_COLOR_DATE 11
316 struct tog_blame_cb_args {
317 struct tog_blame_line *lines; /* one per line */
320 struct tog_view *view;
321 struct got_object_id *commit_id;
325 struct tog_blame_thread_args {
327 struct got_repository *repo;
328 struct tog_blame_cb_args *cb_args;
330 got_cancel_cb cancel_cb;
337 struct tog_blame_line *lines;
341 struct tog_blame_thread_args thread_args;
342 struct tog_blame_cb_args cb_args;
346 struct tog_blame_view_state {
347 int first_displayed_line;
348 int last_displayed_line;
353 struct got_object_id_queue blamed_commits;
354 struct got_object_qid *blamed_commit;
356 struct got_repository *repo;
357 struct got_reflist_head *refs;
358 struct got_object_id *commit_id;
359 struct tog_blame blame;
361 struct tog_colors colors;
364 struct tog_parent_tree {
365 TAILQ_ENTRY(tog_parent_tree) entry;
366 struct got_tree_object *tree;
367 struct got_tree_entry *first_displayed_entry;
368 struct got_tree_entry *selected_entry;
372 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
374 struct tog_tree_view_state {
376 struct got_tree_object *root;
377 struct got_tree_object *tree;
378 struct got_tree_entry *first_displayed_entry;
379 struct got_tree_entry *last_displayed_entry;
380 struct got_tree_entry *selected_entry;
381 int ndisplayed, selected, show_ids;
382 struct tog_parent_trees parents;
383 struct got_object_id *commit_id;
384 struct got_repository *repo;
385 struct got_reflist_head *refs;
386 struct got_tree_entry *matched_entry;
387 struct tog_colors colors;
391 * We implement two types of views: parent views and child views.
393 * The 'Tab' key switches between a parent view and its child view.
394 * Child views are shown side-by-side to their parent view, provided
395 * there is enough screen estate.
397 * When a new view is opened from within a parent view, this new view
398 * becomes a child view of the parent view, replacing any existing child.
400 * When a new view is opened from within a child view, this new view
401 * becomes a parent view which will obscure the views below until the
402 * user quits the new parent view by typing 'q'.
404 * This list of views contains parent views only.
405 * Child views are only pointed to by their parent view.
407 TAILQ_HEAD(tog_view_list_head, tog_view);
410 TAILQ_ENTRY(tog_view) entry;
413 int nlines, ncols, begin_y, begin_x;
414 int lines, cols; /* copies of LINES and COLS */
416 struct tog_view *parent;
417 struct tog_view *child;
420 /* type-specific state */
421 enum tog_view_type type;
423 struct tog_diff_view_state diff;
424 struct tog_log_view_state log;
425 struct tog_blame_view_state blame;
426 struct tog_tree_view_state tree;
429 const struct got_error *(*show)(struct tog_view *);
430 const struct got_error *(*input)(struct tog_view **,
431 struct tog_view **, struct tog_view**, struct tog_view *, int);
432 const struct got_error *(*close)(struct tog_view *);
434 const struct got_error *(*search_start)(struct tog_view *);
435 const struct got_error *(*search_next)(struct tog_view *);
437 #define TOG_SEARCH_FORWARD 1
438 #define TOG_SEARCH_BACKWARD 2
439 int search_next_done;
440 #define TOG_SEARCH_HAVE_MORE 1
441 #define TOG_SEARCH_NO_MORE 2
442 #define TOG_SEARCH_HAVE_NONE 3
446 static const struct got_error *open_diff_view(struct tog_view *,
447 struct got_object_id *, struct got_object_id *, struct tog_view *,
448 struct got_reflist_head *, struct got_repository *);
449 static const struct got_error *show_diff_view(struct tog_view *);
450 static const struct got_error *input_diff_view(struct tog_view **,
451 struct tog_view **, struct tog_view **, struct tog_view *, int);
452 static const struct got_error* close_diff_view(struct tog_view *);
453 static const struct got_error *search_start_diff_view(struct tog_view *);
454 static const struct got_error *search_next_diff_view(struct tog_view *);
456 static const struct got_error *open_log_view(struct tog_view *,
457 struct got_object_id *, struct got_reflist_head *,
458 struct got_repository *, const char *, const char *, int);
459 static const struct got_error * show_log_view(struct tog_view *);
460 static const struct got_error *input_log_view(struct tog_view **,
461 struct tog_view **, struct tog_view **, struct tog_view *, int);
462 static const struct got_error *close_log_view(struct tog_view *);
463 static const struct got_error *search_start_log_view(struct tog_view *);
464 static const struct got_error *search_next_log_view(struct tog_view *);
466 static const struct got_error *open_blame_view(struct tog_view *, char *,
467 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
468 static const struct got_error *show_blame_view(struct tog_view *);
469 static const struct got_error *input_blame_view(struct tog_view **,
470 struct tog_view **, struct tog_view **, struct tog_view *, int);
471 static const struct got_error *close_blame_view(struct tog_view *);
472 static const struct got_error *search_start_blame_view(struct tog_view *);
473 static const struct got_error *search_next_blame_view(struct tog_view *);
475 static const struct got_error *open_tree_view(struct tog_view *,
476 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
477 struct got_repository *);
478 static const struct got_error *show_tree_view(struct tog_view *);
479 static const struct got_error *input_tree_view(struct tog_view **,
480 struct tog_view **, struct tog_view **, struct tog_view *, int);
481 static const struct got_error *close_tree_view(struct tog_view *);
482 static const struct got_error *search_start_tree_view(struct tog_view *);
483 static const struct got_error *search_next_tree_view(struct tog_view *);
485 static volatile sig_atomic_t tog_sigwinch_received;
486 static volatile sig_atomic_t tog_sigpipe_received;
487 static volatile sig_atomic_t tog_sigcont_received;
490 tog_sigwinch(int signo)
492 tog_sigwinch_received = 1;
496 tog_sigpipe(int signo)
498 tog_sigpipe_received = 1;
502 tog_sigcont(int signo)
504 tog_sigcont_received = 1;
507 static const struct got_error *
508 view_close(struct tog_view *view)
510 const struct got_error *err = NULL;
513 view_close(view->child);
517 err = view->close(view);
519 del_panel(view->panel);
521 delwin(view->window);
526 static struct tog_view *
527 view_open(int nlines, int ncols, int begin_y, int begin_x,
528 enum tog_view_type type)
530 struct tog_view *view = calloc(1, sizeof(*view));
538 view->nlines = nlines ? nlines : LINES - begin_y;
539 view->ncols = ncols ? ncols : COLS - begin_x;
540 view->begin_y = begin_y;
541 view->begin_x = begin_x;
542 view->window = newwin(nlines, ncols, begin_y, begin_x);
543 if (view->window == NULL) {
547 view->panel = new_panel(view->window);
548 if (view->panel == NULL ||
549 set_panel_userptr(view->panel, view) != OK) {
554 keypad(view->window, TRUE);
559 view_split_begin_x(int begin_x)
561 if (begin_x > 0 || COLS < 120)
563 return (COLS - MAX(COLS / 2, 80));
566 static const struct got_error *view_resize(struct tog_view *);
568 static const struct got_error *
569 view_splitscreen(struct tog_view *view)
571 const struct got_error *err = NULL;
574 view->begin_x = view_split_begin_x(0);
575 view->nlines = LINES;
576 view->ncols = COLS - view->begin_x;
579 err = view_resize(view);
583 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
584 return got_error_from_errno("mvwin");
589 static const struct got_error *
590 view_fullscreen(struct tog_view *view)
592 const struct got_error *err = NULL;
596 view->nlines = LINES;
600 err = view_resize(view);
604 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
605 return got_error_from_errno("mvwin");
611 view_is_parent_view(struct tog_view *view)
613 return view->parent == NULL;
616 static const struct got_error *
617 view_resize(struct tog_view *view)
621 if (view->lines > LINES)
622 nlines = view->nlines - (view->lines - LINES);
624 nlines = view->nlines + (LINES - view->lines);
626 if (view->cols > COLS)
627 ncols = view->ncols - (view->cols - COLS);
629 ncols = view->ncols + (COLS - view->cols);
631 if (wresize(view->window, nlines, ncols) == ERR)
632 return got_error_from_errno("wresize");
633 if (replace_panel(view->panel, view->window) == ERR)
634 return got_error_from_errno("replace_panel");
635 wclear(view->window);
637 view->nlines = nlines;
643 view->child->begin_x = view_split_begin_x(view->begin_x);
644 if (view->child->begin_x == 0) {
645 view_fullscreen(view->child);
646 if (view->child->focussed)
647 show_panel(view->child->panel);
649 show_panel(view->panel);
651 view_splitscreen(view->child);
652 show_panel(view->child->panel);
659 static const struct got_error *
660 view_close_child(struct tog_view *view)
662 const struct got_error *err = NULL;
664 if (view->child == NULL)
667 err = view_close(view->child);
672 static const struct got_error *
673 view_set_child(struct tog_view *view, struct tog_view *child)
675 const struct got_error *err = NULL;
678 child->parent = view;
683 view_is_splitscreen(struct tog_view *view)
685 return view->begin_x > 0;
694 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
695 cols = 80; /* Default */
701 resize_term(lines, cols);
704 static const struct got_error *
705 view_search_start(struct tog_view *view)
707 const struct got_error *err = NULL;
711 if (view->nlines < 1)
714 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
715 wclrtoeol(view->window);
719 ret = wgetnstr(view->window, pattern, sizeof(pattern));
725 if (view->searching) {
726 regfree(&view->regex);
730 if (regcomp(&view->regex, pattern,
731 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
732 err = view->search_start(view);
734 regfree(&view->regex);
737 view->searching = TOG_SEARCH_FORWARD;
738 view->search_next_done = 0;
739 view->search_next(view);
745 static const struct got_error *
746 view_input(struct tog_view **new, struct tog_view **dead,
747 struct tog_view **focus, int *done, struct tog_view *view,
748 struct tog_view_list_head *views)
750 const struct got_error *err = NULL;
758 /* Clear "no matches" indicator. */
759 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
760 view->search_next_done == TOG_SEARCH_HAVE_NONE)
761 view->search_next_done = TOG_SEARCH_HAVE_MORE;
763 if (view->searching && !view->search_next_done) {
764 errcode = pthread_mutex_unlock(&tog_mutex);
766 return got_error_set_errno(errcode,
767 "pthread_mutex_unlock");
769 errcode = pthread_mutex_lock(&tog_mutex);
771 return got_error_set_errno(errcode,
772 "pthread_mutex_lock");
773 view->search_next(view);
777 nodelay(stdscr, FALSE);
778 /* Allow threads to make progress while we are waiting for input. */
779 errcode = pthread_mutex_unlock(&tog_mutex);
781 return got_error_set_errno(errcode, "pthread_mutex_unlock");
782 ch = wgetch(view->window);
783 errcode = pthread_mutex_lock(&tog_mutex);
785 return got_error_set_errno(errcode, "pthread_mutex_lock");
786 nodelay(stdscr, TRUE);
788 if (tog_sigwinch_received || tog_sigcont_received) {
790 tog_sigwinch_received = 0;
791 tog_sigcont_received = 0;
792 TAILQ_FOREACH(v, views, entry) {
793 err = view_resize(v);
796 err = v->input(new, dead, focus, v, KEY_RESIZE);
807 *focus = view->child;
808 view->child_focussed = 1;
809 } else if (view->parent) {
810 *focus = view->parent;
811 view->parent->child_focussed = 0;
815 err = view->input(new, dead, focus, view, ch);
822 if (view_is_parent_view(view)) {
823 if (view->child == NULL)
825 if (view_is_splitscreen(view->child)) {
826 *focus = view->child;
827 view->child_focussed = 1;
828 err = view_fullscreen(view->child);
830 err = view_splitscreen(view->child);
833 err = view->child->input(new, dead, focus,
834 view->child, KEY_RESIZE);
836 if (view_is_splitscreen(view)) {
838 view->parent->child_focussed = 1;
839 err = view_fullscreen(view);
841 err = view_splitscreen(view);
845 err = view->input(new, dead, focus, view,
852 if (view->search_start)
853 view_search_start(view);
855 err = view->input(new, dead, focus, view, ch);
859 if (view->search_next) {
860 view->searching = (ch == 'n' ?
861 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
862 view->search_next_done = 0;
863 view->search_next(view);
865 err = view->input(new, dead, focus, view, ch);
868 err = view->input(new, dead, focus, view, ch);
876 view_vborder(struct tog_view *view)
879 struct tog_view *view_above;
882 return view_vborder(view->parent);
884 panel = panel_above(view->panel);
888 view_above = panel_userptr(panel);
889 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
890 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
894 view_needs_focus_indication(struct tog_view *view)
896 if (view_is_parent_view(view)) {
897 if (view->child == NULL || view->child_focussed)
899 if (!view_is_splitscreen(view->child))
901 } else if (!view_is_splitscreen(view))
904 return view->focussed;
907 static const struct got_error *
908 view_loop(struct tog_view *view)
910 const struct got_error *err = NULL;
911 struct tog_view_list_head views;
912 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
913 int fast_refresh = 10;
914 int done = 0, errcode;
916 errcode = pthread_mutex_lock(&tog_mutex);
918 return got_error_set_errno(errcode, "pthread_mutex_lock");
921 TAILQ_INSERT_HEAD(&views, view, entry);
925 err = view->show(view);
930 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
931 /* Refresh fast during initialization, then become slower. */
932 if (fast_refresh && fast_refresh-- == 0)
933 halfdelay(10); /* switch to once per second */
935 err = view_input(&new_view, &dead_view, &focus_view, &done,
940 struct tog_view *prev = NULL;
942 if (view_is_parent_view(dead_view))
943 prev = TAILQ_PREV(dead_view,
944 tog_view_list_head, entry);
945 else if (view->parent != dead_view)
948 if (dead_view->parent)
949 dead_view->parent->child = NULL;
951 TAILQ_REMOVE(&views, dead_view, entry);
953 err = view_close(dead_view);
954 if (err || (dead_view == main_view && new_view == NULL))
957 if (view == dead_view) {
962 else if (!TAILQ_EMPTY(&views))
963 view = TAILQ_LAST(&views,
968 if (view->child && view->child_focussed)
969 focus_view = view->child;
976 struct tog_view *v, *t;
977 /* Only allow one parent view per type. */
978 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
979 if (v->type != new_view->type)
981 TAILQ_REMOVE(&views, v, entry);
987 TAILQ_INSERT_TAIL(&views, new_view, entry);
989 if (focus_view == NULL)
990 focus_view = new_view;
993 show_panel(focus_view->panel);
996 focus_view->focussed = 1;
999 show_panel(new_view->panel);
1000 if (view->child && view_is_splitscreen(view->child))
1001 show_panel(view->child->panel);
1004 if (focus_view == NULL) {
1006 show_panel(view->panel);
1007 if (view->child && view_is_splitscreen(view->child))
1008 show_panel(view->child->panel);
1012 err = view->parent->show(view->parent);
1016 err = view->show(view);
1020 err = view->child->show(view->child);
1029 while (!TAILQ_EMPTY(&views)) {
1030 view = TAILQ_FIRST(&views);
1031 TAILQ_REMOVE(&views, view, entry);
1035 errcode = pthread_mutex_unlock(&tog_mutex);
1036 if (errcode && err == NULL)
1037 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1047 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1052 /* Create newly allocated wide-character string equivalent to a byte string. */
1053 static const struct got_error *
1054 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1057 const struct got_error *err = NULL;
1060 *wlen = mbstowcs(NULL, s, 0);
1061 if (*wlen == (size_t)-1) {
1063 if (errno != EILSEQ)
1064 return got_error_from_errno("mbstowcs");
1066 /* byte string invalid in current encoding; try to "fix" it */
1067 err = got_mbsavis(&vis, &vislen, s);
1070 *wlen = mbstowcs(NULL, vis, 0);
1071 if (*wlen == (size_t)-1) {
1072 err = got_error_from_errno("mbstowcs"); /* give up */
1077 *ws = calloc(*wlen + 1, sizeof(**ws));
1079 err = got_error_from_errno("calloc");
1083 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1084 err = got_error_from_errno("mbstowcs");
1095 /* Format a line for display, ensuring that it won't overflow a width limit. */
1096 static const struct got_error *
1097 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1100 const struct got_error *err = NULL;
1102 wchar_t *wline = NULL;
1109 err = mbs2ws(&wline, &wlen, line);
1115 int width = wcwidth(wline[i]);
1122 if (width == 1 || width == 2) {
1123 if (cols + width > wlimit)
1127 } else if (width == -1) {
1128 if (wline[i] == L'\t') {
1130 ((cols + col_tab_align) % TABSIZE);
1131 if (cols + width > wlimit)
1137 err = got_error_from_errno("wcwidth");
1152 static const struct got_error*
1153 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1154 struct got_object_id *id, struct got_repository *repo)
1156 static const struct got_error *err = NULL;
1157 struct got_reflist_entry *re;
1163 SIMPLEQ_FOREACH(re, refs, entry) {
1164 struct got_tag_object *tag = NULL;
1167 name = got_ref_get_name(re->ref);
1168 if (strcmp(name, GOT_REF_HEAD) == 0)
1170 if (strncmp(name, "refs/", 5) == 0)
1172 if (strncmp(name, "got/", 4) == 0)
1174 if (strncmp(name, "heads/", 6) == 0)
1176 if (strncmp(name, "remotes/", 8) == 0)
1178 if (strncmp(name, "tags/", 5) == 0) {
1179 err = got_object_open_as_tag(&tag, repo, re->id);
1181 if (err->code != GOT_ERR_OBJ_TYPE)
1183 /* Ref points at something other than a tag. */
1188 cmp = got_object_id_cmp(tag ?
1189 got_object_tag_get_object_id(tag) : re->id, id);
1191 got_object_tag_close(tag);
1195 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1196 s ? ", " : "", name) == -1) {
1197 err = got_error_from_errno("asprintf");
1208 static const struct got_error *
1209 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1212 char *smallerthan, *at;
1214 smallerthan = strchr(author, '<');
1215 if (smallerthan && smallerthan[1] != '\0')
1216 author = smallerthan + 1;
1217 at = strchr(author, '@');
1220 return format_line(wauthor, author_width, author, limit, col_tab_align);
1223 static const struct got_error *
1224 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1225 struct got_object_id *id, struct got_reflist_head *refs,
1226 const size_t date_display_cols, int author_display_cols,
1227 struct tog_colors *colors)
1229 const struct got_error *err = NULL;
1230 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1231 char *logmsg0 = NULL, *logmsg = NULL;
1232 char *author = NULL;
1233 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1234 int author_width, logmsg_width;
1235 char *newline, *line = NULL;
1237 const int avail = view->ncols;
1239 time_t committer_time;
1240 struct tog_color *tc;
1242 committer_time = got_object_commit_get_committer_time(commit);
1243 if (localtime_r(&committer_time, &tm) == NULL)
1244 return got_error_from_errno("localtime_r");
1245 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1247 return got_error(GOT_ERR_NO_SPACE);
1249 if (avail <= date_display_cols)
1250 limit = MIN(sizeof(datebuf) - 1, avail);
1252 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1253 tc = get_color(colors, TOG_COLOR_DATE);
1255 wattr_on(view->window,
1256 COLOR_PAIR(tc->colorpair), NULL);
1257 waddnstr(view->window, datebuf, limit);
1259 wattr_off(view->window,
1260 COLOR_PAIR(tc->colorpair), NULL);
1267 err = got_object_id_str(&id_str, id);
1270 tc = get_color(colors, TOG_COLOR_COMMIT);
1272 wattr_on(view->window,
1273 COLOR_PAIR(tc->colorpair), NULL);
1274 wprintw(view->window, "%.8s ", id_str);
1276 wattr_off(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1284 author = strdup(got_object_commit_get_author(commit));
1285 if (author == NULL) {
1286 err = got_error_from_errno("strdup");
1289 err = format_author(&wauthor, &author_width, author, avail - col, col);
1292 tc = get_color(colors, TOG_COLOR_AUTHOR);
1294 wattr_on(view->window,
1295 COLOR_PAIR(tc->colorpair), NULL);
1296 waddwstr(view->window, wauthor);
1298 wattr_off(view->window,
1299 COLOR_PAIR(tc->colorpair), NULL);
1300 col += author_width;
1301 while (col < avail && author_width < author_display_cols + 2) {
1302 waddch(view->window, ' ');
1309 err = got_object_commit_get_logmsg(&logmsg0, commit);
1313 while (*logmsg == '\n')
1315 newline = strchr(logmsg, '\n');
1318 limit = avail - col;
1319 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1322 waddwstr(view->window, wlogmsg);
1323 col += logmsg_width;
1324 while (col < avail) {
1325 waddch(view->window, ' ');
1337 static struct commit_queue_entry *
1338 alloc_commit_queue_entry(struct got_commit_object *commit,
1339 struct got_object_id *id)
1341 struct commit_queue_entry *entry;
1343 entry = calloc(1, sizeof(*entry));
1348 entry->commit = commit;
1353 pop_commit(struct commit_queue *commits)
1355 struct commit_queue_entry *entry;
1357 entry = TAILQ_FIRST(&commits->head);
1358 TAILQ_REMOVE(&commits->head, entry, entry);
1359 got_object_commit_close(entry->commit);
1360 commits->ncommits--;
1361 /* Don't free entry->id! It is owned by the commit graph. */
1366 free_commits(struct commit_queue *commits)
1368 while (!TAILQ_EMPTY(&commits->head))
1369 pop_commit(commits);
1372 static const struct got_error *
1373 match_commit(int *have_match, struct got_object_id *id,
1374 struct got_commit_object *commit, regex_t *regex)
1376 const struct got_error *err = NULL;
1377 regmatch_t regmatch;
1378 char *id_str = NULL, *logmsg = NULL;
1382 err = got_object_id_str(&id_str, id);
1386 err = got_object_commit_get_logmsg(&logmsg, commit);
1390 if (regexec(regex, got_object_commit_get_author(commit), 1,
1391 ®match, 0) == 0 ||
1392 regexec(regex, got_object_commit_get_committer(commit), 1,
1393 ®match, 0) == 0 ||
1394 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1395 regexec(regex, logmsg, 1, ®match, 0) == 0)
1403 static const struct got_error *
1404 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1405 int minqueue, struct got_repository *repo, const char *path,
1406 int *searching, int *search_next_done, regex_t *regex)
1408 const struct got_error *err = NULL;
1412 * We keep all commits open throughout the lifetime of the log
1413 * view in order to avoid having to re-fetch commits from disk
1414 * while updating the display.
1416 while (nqueued < minqueue ||
1417 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1418 struct got_object_id *id;
1419 struct got_commit_object *commit;
1420 struct commit_queue_entry *entry;
1423 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1424 if (err || id == NULL)
1427 err = got_object_open_as_commit(&commit, repo, id);
1430 entry = alloc_commit_queue_entry(commit, id);
1431 if (entry == NULL) {
1432 err = got_error_from_errno("alloc_commit_queue_entry");
1436 errcode = pthread_mutex_lock(&tog_mutex);
1438 err = got_error_set_errno(errcode,
1439 "pthread_mutex_lock");
1443 entry->idx = commits->ncommits;
1444 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1446 commits->ncommits++;
1448 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1450 err = match_commit(&have_match, id, commit, regex);
1454 *search_next_done = TOG_SEARCH_HAVE_MORE;
1457 errcode = pthread_mutex_unlock(&tog_mutex);
1458 if (errcode && err == NULL)
1459 err = got_error_set_errno(errcode,
1460 "pthread_mutex_unlock");
1468 static const struct got_error *
1469 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1470 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1471 struct commit_queue *commits, int selected_idx, int limit,
1472 struct got_reflist_head *refs, const char *path, int commits_needed,
1473 struct tog_colors *colors)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1477 struct commit_queue_entry *entry;
1479 int ncommits, author_cols = 4;
1480 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1481 char *refs_str = NULL;
1483 struct tog_color *tc;
1484 static const size_t date_display_cols = 12;
1489 if (ncommits == selected_idx) {
1493 entry = TAILQ_NEXT(entry, entry);
1497 if (*selected && !(view->searching && view->search_next_done == 0)) {
1498 err = got_object_id_str(&id_str, (*selected)->id);
1502 err = build_refs_str(&refs_str, refs, (*selected)->id,
1509 if (commits_needed == 0)
1510 halfdelay(10); /* disable fast refresh */
1512 if (commits_needed > 0) {
1513 if (asprintf(&ncommits_str, " [%d/%d] %s",
1514 entry ? entry->idx + 1 : 0, commits->ncommits,
1515 view->searching ? "searching..." : "loading...") == -1) {
1516 err = got_error_from_errno("asprintf");
1520 const char *search_str = NULL;
1522 if (view->searching) {
1523 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1524 search_str = "no more matches";
1525 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1526 search_str = "no matches found";
1527 else if (!view->search_next_done)
1528 search_str = "searching...";
1531 if (asprintf(&ncommits_str, " [%d/%d] %s",
1532 entry ? entry->idx + 1 : 0, commits->ncommits,
1533 search_str ? search_str :
1534 (refs_str ? refs_str : "")) == -1) {
1535 err = got_error_from_errno("asprintf");
1540 if (path && strcmp(path, "/") != 0) {
1541 if (asprintf(&header, "commit %s %s%s",
1542 id_str ? id_str : "........................................",
1543 path, ncommits_str) == -1) {
1544 err = got_error_from_errno("asprintf");
1548 } else if (asprintf(&header, "commit %s%s",
1549 id_str ? id_str : "........................................",
1550 ncommits_str) == -1) {
1551 err = got_error_from_errno("asprintf");
1555 err = format_line(&wline, &width, header, view->ncols, 0);
1559 werase(view->window);
1561 if (view_needs_focus_indication(view))
1562 wstandout(view->window);
1563 tc = get_color(colors, TOG_COLOR_COMMIT);
1565 wattr_on(view->window,
1566 COLOR_PAIR(tc->colorpair), NULL);
1567 waddwstr(view->window, wline);
1569 wattr_off(view->window,
1570 COLOR_PAIR(tc->colorpair), NULL);
1571 while (width < view->ncols) {
1572 waddch(view->window, ' ');
1575 if (view_needs_focus_indication(view))
1576 wstandend(view->window);
1581 /* Grow author column size if necessary. */
1588 if (ncommits >= limit - 1)
1590 author = strdup(got_object_commit_get_author(entry->commit));
1591 if (author == NULL) {
1592 err = got_error_from_errno("strdup");
1595 err = format_author(&wauthor, &width, author, COLS,
1597 if (author_cols < width)
1598 author_cols = width;
1602 entry = TAILQ_NEXT(entry, entry);
1609 if (ncommits >= limit - 1)
1611 if (ncommits == selected_idx)
1612 wstandout(view->window);
1613 err = draw_commit(view, entry->commit, entry->id, refs,
1614 date_display_cols, author_cols, colors);
1615 if (ncommits == selected_idx)
1616 wstandend(view->window);
1621 entry = TAILQ_NEXT(entry, entry);
1634 scroll_up(struct tog_view *view,
1635 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1636 struct commit_queue *commits)
1638 struct commit_queue_entry *entry;
1641 entry = TAILQ_FIRST(&commits->head);
1642 if (*first_displayed_entry == entry)
1645 entry = *first_displayed_entry;
1646 while (entry && nscrolled < maxscroll) {
1647 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1649 *first_displayed_entry = entry;
1655 static const struct got_error *
1656 trigger_log_thread(struct tog_view *log_view, int wait,
1657 int *commits_needed, int *log_complete,
1658 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1662 halfdelay(1); /* fast refresh while loading commits */
1664 while (*commits_needed > 0) {
1668 /* Wake the log thread. */
1669 errcode = pthread_cond_signal(need_commits);
1671 return got_error_set_errno(errcode,
1672 "pthread_cond_signal");
1675 * The mutex will be released while the view loop waits
1676 * in wgetch(), at which time the log thread will run.
1681 /* Display progress update in log view. */
1682 show_log_view(log_view);
1686 /* Wait right here while next commit is being loaded. */
1687 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1689 return got_error_set_errno(errcode,
1690 "pthread_cond_wait");
1692 /* Display progress update in log view. */
1693 show_log_view(log_view);
1701 static const struct got_error *
1702 scroll_down(struct tog_view *log_view,
1703 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1704 struct commit_queue_entry **last_displayed_entry,
1705 struct commit_queue *commits, int *log_complete, int *commits_needed,
1706 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1708 const struct got_error *err = NULL;
1709 struct commit_queue_entry *pentry;
1710 int nscrolled = 0, ncommits_needed;
1712 if (*last_displayed_entry == NULL)
1715 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1716 if (commits->ncommits < ncommits_needed && !*log_complete) {
1718 * Ask the log thread for required amount of commits.
1720 (*commits_needed) += maxscroll;
1721 err = trigger_log_thread(log_view, 1, commits_needed,
1722 log_complete, need_commits, commit_loaded);
1728 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1732 *last_displayed_entry = pentry;
1734 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1737 *first_displayed_entry = pentry;
1738 } while (++nscrolled < maxscroll);
1743 static const struct got_error *
1744 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1745 struct got_commit_object *commit, struct got_object_id *commit_id,
1746 struct tog_view *log_view, struct got_reflist_head *refs,
1747 struct got_repository *repo)
1749 const struct got_error *err;
1750 struct got_object_qid *parent_id;
1751 struct tog_view *diff_view;
1753 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1754 if (diff_view == NULL)
1755 return got_error_from_errno("view_open");
1757 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1758 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1759 commit_id, log_view, refs, repo);
1761 *new_view = diff_view;
1765 static const struct got_error *
1766 tree_view_visit_subtree(struct got_tree_object *subtree,
1767 struct tog_tree_view_state *s)
1769 struct tog_parent_tree *parent;
1771 parent = calloc(1, sizeof(*parent));
1773 return got_error_from_errno("calloc");
1775 parent->tree = s->tree;
1776 parent->first_displayed_entry = s->first_displayed_entry;
1777 parent->selected_entry = s->selected_entry;
1778 parent->selected = s->selected;
1779 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1782 s->first_displayed_entry = NULL;
1786 static const struct got_error *
1787 tree_view_walk_path(struct tog_tree_view_state *s,
1788 struct got_object_id *commit_id,
1789 const char *path, struct got_repository *repo)
1791 const struct got_error *err = NULL;
1792 struct got_tree_object *tree = NULL;
1794 char *slash, *subpath = NULL;
1796 /* Walk the path and open corresponding tree objects. */
1799 struct got_tree_entry *te;
1800 struct got_object_id *tree_id;
1806 /* Ensure the correct subtree entry is selected. */
1807 slash = strchr(p, '/');
1809 te_name = strdup(p);
1811 te_name = strndup(p, slash - p);
1812 if (te_name == NULL) {
1813 err = got_error_from_errno("strndup");
1816 te = got_object_tree_find_entry(s->tree, te_name);
1818 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1823 s->selected_entry = te;
1824 s->selected = got_tree_entry_get_index(te);
1825 if (s->tree != s->root)
1826 s->selected++; /* skip '..' */
1828 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1829 /* Jump to this file's entry. */
1830 s->first_displayed_entry = s->selected_entry;
1835 slash = strchr(p, '/');
1837 subpath = strndup(path, slash - path);
1839 subpath = strdup(path);
1840 if (subpath == NULL) {
1841 err = got_error_from_errno("strdup");
1845 err = got_object_id_by_path(&tree_id, repo, commit_id,
1850 err = got_object_open_as_tree(&tree, repo, tree_id);
1855 err = tree_view_visit_subtree(tree, s);
1857 got_object_tree_close(tree);
1871 static const struct got_error *
1872 browse_commit_tree(struct tog_view **new_view, int begin_x,
1873 struct commit_queue_entry *entry, const char *path,
1874 struct got_reflist_head *refs, struct got_repository *repo)
1876 const struct got_error *err = NULL;
1877 struct got_tree_object *tree;
1878 struct tog_tree_view_state *s;
1879 struct tog_view *tree_view;
1881 err = got_object_open_as_tree(&tree, repo,
1882 got_object_commit_get_tree_id(entry->commit));
1886 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1887 if (tree_view == NULL)
1888 return got_error_from_errno("view_open");
1890 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1892 got_object_tree_close(tree);
1895 s = &tree_view->state.tree;
1897 *new_view = tree_view;
1899 if (got_path_is_root_dir(path))
1902 return tree_view_walk_path(s, entry->id, path, repo);
1905 static const struct got_error *
1906 block_signals_used_by_main_thread(void)
1911 if (sigemptyset(&sigset) == -1)
1912 return got_error_from_errno("sigemptyset");
1914 /* tog handles SIGWINCH and SIGCONT */
1915 if (sigaddset(&sigset, SIGWINCH) == -1)
1916 return got_error_from_errno("sigaddset");
1917 if (sigaddset(&sigset, SIGCONT) == -1)
1918 return got_error_from_errno("sigaddset");
1920 /* ncurses handles SIGTSTP */
1921 if (sigaddset(&sigset, SIGTSTP) == -1)
1922 return got_error_from_errno("sigaddset");
1924 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1926 return got_error_set_errno(errcode, "pthread_sigmask");
1932 log_thread(void *arg)
1934 const struct got_error *err = NULL;
1936 struct tog_log_thread_args *a = arg;
1939 err = block_signals_used_by_main_thread();
1943 while (!done && !err && !tog_sigpipe_received) {
1944 err = queue_commits(a->graph, a->commits, 1, a->repo,
1945 a->in_repo_path, a->searching, a->search_next_done,
1948 if (err->code != GOT_ERR_ITER_COMPLETED)
1952 } else if (a->commits_needed > 0)
1953 a->commits_needed--;
1955 errcode = pthread_mutex_lock(&tog_mutex);
1957 err = got_error_set_errno(errcode,
1958 "pthread_mutex_lock");
1960 } else if (*a->quit)
1962 else if (*a->first_displayed_entry == NULL) {
1963 *a->first_displayed_entry =
1964 TAILQ_FIRST(&a->commits->head);
1965 *a->selected_entry = *a->first_displayed_entry;
1968 errcode = pthread_cond_signal(&a->commit_loaded);
1970 err = got_error_set_errno(errcode,
1971 "pthread_cond_signal");
1972 pthread_mutex_unlock(&tog_mutex);
1977 a->commits_needed = 0;
1979 if (a->commits_needed == 0) {
1980 errcode = pthread_cond_wait(&a->need_commits,
1983 err = got_error_set_errno(errcode,
1984 "pthread_cond_wait");
1988 errcode = pthread_mutex_unlock(&tog_mutex);
1989 if (errcode && err == NULL)
1990 err = got_error_set_errno(errcode,
1991 "pthread_mutex_unlock");
1993 a->log_complete = 1;
1997 static const struct got_error *
1998 stop_log_thread(struct tog_log_view_state *s)
2000 const struct got_error *err = NULL;
2005 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2007 return got_error_set_errno(errcode,
2008 "pthread_cond_signal");
2009 errcode = pthread_mutex_unlock(&tog_mutex);
2011 return got_error_set_errno(errcode,
2012 "pthread_mutex_unlock");
2013 errcode = pthread_join(s->thread, (void **)&err);
2015 return got_error_set_errno(errcode, "pthread_join");
2016 errcode = pthread_mutex_lock(&tog_mutex);
2018 return got_error_set_errno(errcode,
2019 "pthread_mutex_lock");
2023 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2024 if (errcode && err == NULL)
2025 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2027 if (s->thread_args.repo) {
2028 got_repo_close(s->thread_args.repo);
2029 s->thread_args.repo = NULL;
2032 if (s->thread_args.graph) {
2033 got_commit_graph_close(s->thread_args.graph);
2034 s->thread_args.graph = NULL;
2040 static const struct got_error *
2041 close_log_view(struct tog_view *view)
2043 const struct got_error *err = NULL;
2044 struct tog_log_view_state *s = &view->state.log;
2046 err = stop_log_thread(s);
2047 free_commits(&s->commits);
2048 free(s->in_repo_path);
2049 s->in_repo_path = NULL;
2055 static const struct got_error *
2056 search_start_log_view(struct tog_view *view)
2058 struct tog_log_view_state *s = &view->state.log;
2060 s->matched_entry = NULL;
2061 s->search_entry = NULL;
2065 static const struct got_error *
2066 search_next_log_view(struct tog_view *view)
2068 const struct got_error *err = NULL;
2069 struct tog_log_view_state *s = &view->state.log;
2070 struct commit_queue_entry *entry;
2072 /* Display progress update in log view. */
2073 show_log_view(view);
2077 if (s->search_entry) {
2079 errcode = pthread_mutex_unlock(&tog_mutex);
2081 return got_error_set_errno(errcode,
2082 "pthread_mutex_unlock");
2083 ch = wgetch(view->window);
2084 errcode = pthread_mutex_lock(&tog_mutex);
2086 return got_error_set_errno(errcode,
2087 "pthread_mutex_lock");
2088 if (ch == KEY_BACKSPACE) {
2089 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2092 if (view->searching == TOG_SEARCH_FORWARD)
2093 entry = TAILQ_NEXT(s->search_entry, entry);
2095 entry = TAILQ_PREV(s->search_entry,
2096 commit_queue_head, entry);
2097 } else if (s->matched_entry) {
2098 if (view->searching == TOG_SEARCH_FORWARD)
2099 entry = TAILQ_NEXT(s->matched_entry, entry);
2101 entry = TAILQ_PREV(s->matched_entry,
2102 commit_queue_head, entry);
2104 if (view->searching == TOG_SEARCH_FORWARD)
2105 entry = TAILQ_FIRST(&s->commits.head);
2107 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2113 if (entry == NULL) {
2114 if (s->thread_args.log_complete ||
2115 view->searching == TOG_SEARCH_BACKWARD) {
2116 view->search_next_done =
2117 (s->matched_entry == NULL ?
2118 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2119 s->search_entry = NULL;
2123 * Poke the log thread for more commits and return,
2124 * allowing the main loop to make progress. Search
2125 * will resume at s->search_entry once we come back.
2127 s->thread_args.commits_needed++;
2128 return trigger_log_thread(view, 0,
2129 &s->thread_args.commits_needed,
2130 &s->thread_args.log_complete,
2131 &s->thread_args.need_commits,
2132 &s->thread_args.commit_loaded);
2135 err = match_commit(&have_match, entry->id, entry->commit,
2140 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2141 s->matched_entry = entry;
2145 s->search_entry = entry;
2146 if (view->searching == TOG_SEARCH_FORWARD)
2147 entry = TAILQ_NEXT(entry, entry);
2149 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2152 if (s->matched_entry) {
2153 int cur = s->selected_entry->idx;
2154 while (cur < s->matched_entry->idx) {
2155 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2160 while (cur > s->matched_entry->idx) {
2161 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2168 s->search_entry = NULL;
2173 static const struct got_error *
2174 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2175 struct got_reflist_head *refs, struct got_repository *repo,
2176 const char *head_ref_name, const char *in_repo_path,
2179 const struct got_error *err = NULL;
2180 struct tog_log_view_state *s = &view->state.log;
2181 struct got_repository *thread_repo = NULL;
2182 struct got_commit_graph *thread_graph = NULL;
2185 if (in_repo_path != s->in_repo_path) {
2186 free(s->in_repo_path);
2187 s->in_repo_path = strdup(in_repo_path);
2188 if (s->in_repo_path == NULL)
2189 return got_error_from_errno("strdup");
2192 /* The commit queue only contains commits being displayed. */
2193 TAILQ_INIT(&s->commits.head);
2194 s->commits.ncommits = 0;
2198 s->head_ref_name = head_ref_name;
2199 s->start_id = got_object_id_dup(start_id);
2200 if (s->start_id == NULL) {
2201 err = got_error_from_errno("got_object_id_dup");
2204 s->log_branches = log_branches;
2206 SIMPLEQ_INIT(&s->colors);
2207 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2208 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2209 get_color_value("TOG_COLOR_COMMIT"));
2212 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2213 get_color_value("TOG_COLOR_AUTHOR"));
2215 free_colors(&s->colors);
2218 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2219 get_color_value("TOG_COLOR_DATE"));
2221 free_colors(&s->colors);
2226 view->show = show_log_view;
2227 view->input = input_log_view;
2228 view->close = close_log_view;
2229 view->search_start = search_start_log_view;
2230 view->search_next = search_next_log_view;
2232 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2235 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2239 err = got_commit_graph_iter_start(thread_graph,
2240 s->start_id, s->repo, NULL, NULL);
2244 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2246 err = got_error_set_errno(errcode, "pthread_cond_init");
2249 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2251 err = got_error_set_errno(errcode, "pthread_cond_init");
2255 s->thread_args.commits_needed = view->nlines;
2256 s->thread_args.graph = thread_graph;
2257 s->thread_args.commits = &s->commits;
2258 s->thread_args.in_repo_path = s->in_repo_path;
2259 s->thread_args.start_id = s->start_id;
2260 s->thread_args.repo = thread_repo;
2261 s->thread_args.log_complete = 0;
2262 s->thread_args.quit = &s->quit;
2263 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2264 s->thread_args.selected_entry = &s->selected_entry;
2265 s->thread_args.searching = &view->searching;
2266 s->thread_args.search_next_done = &view->search_next_done;
2267 s->thread_args.regex = &view->regex;
2270 close_log_view(view);
2274 static const struct got_error *
2275 show_log_view(struct tog_view *view)
2277 struct tog_log_view_state *s = &view->state.log;
2279 if (s->thread == NULL) {
2280 int errcode = pthread_create(&s->thread, NULL, log_thread,
2283 return got_error_set_errno(errcode, "pthread_create");
2286 return draw_commits(view, &s->last_displayed_entry,
2287 &s->selected_entry, s->first_displayed_entry,
2288 &s->commits, s->selected, view->nlines, s->refs,
2289 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2292 static const struct got_error *
2293 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2294 struct tog_view **focus_view, struct tog_view *view, int ch)
2296 const struct got_error *err = NULL;
2297 struct tog_log_view_state *s = &view->state.log;
2298 char *parent_path, *in_repo_path = NULL;
2299 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2301 struct got_object_id *start_id;
2311 if (s->first_displayed_entry == NULL)
2313 if (s->selected > 0)
2316 scroll_up(view, &s->first_displayed_entry, 1,
2321 if (s->first_displayed_entry == NULL)
2323 if (TAILQ_FIRST(&s->commits.head) ==
2324 s->first_displayed_entry) {
2328 scroll_up(view, &s->first_displayed_entry,
2329 view->nlines - 1, &s->commits);
2335 if (s->first_displayed_entry == NULL)
2337 if (s->selected < MIN(view->nlines - 2,
2338 s->commits.ncommits - 1)) {
2342 err = scroll_down(view, &s->first_displayed_entry, 1,
2343 &s->last_displayed_entry, &s->commits,
2344 &s->thread_args.log_complete,
2345 &s->thread_args.commits_needed,
2346 &s->thread_args.need_commits,
2347 &s->thread_args.commit_loaded);
2351 struct commit_queue_entry *first;
2352 first = s->first_displayed_entry;
2355 err = scroll_down(view, &s->first_displayed_entry,
2356 view->nlines - 1, &s->last_displayed_entry,
2357 &s->commits, &s->thread_args.log_complete,
2358 &s->thread_args.commits_needed,
2359 &s->thread_args.need_commits,
2360 &s->thread_args.commit_loaded);
2363 if (first == s->first_displayed_entry &&
2364 s->selected < MIN(view->nlines - 2,
2365 s->commits.ncommits - 1)) {
2366 /* can't scroll further down */
2367 s->selected = MIN(view->nlines - 2,
2368 s->commits.ncommits - 1);
2374 if (s->selected > view->nlines - 2)
2375 s->selected = view->nlines - 2;
2376 if (s->selected > s->commits.ncommits - 1)
2377 s->selected = s->commits.ncommits - 1;
2382 if (s->selected_entry == NULL)
2384 if (view_is_parent_view(view))
2385 begin_x = view_split_begin_x(view->begin_x);
2386 err = open_diff_view_for_commit(&diff_view, begin_x,
2387 s->selected_entry->commit, s->selected_entry->id,
2388 view, s->refs, s->repo);
2391 if (view_is_parent_view(view)) {
2392 err = view_close_child(view);
2395 err = view_set_child(view, diff_view);
2397 view_close(diff_view);
2400 *focus_view = diff_view;
2401 view->child_focussed = 1;
2403 *new_view = diff_view;
2406 if (s->selected_entry == NULL)
2408 if (view_is_parent_view(view))
2409 begin_x = view_split_begin_x(view->begin_x);
2410 err = browse_commit_tree(&tree_view, begin_x,
2411 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2414 if (view_is_parent_view(view)) {
2415 err = view_close_child(view);
2418 err = view_set_child(view, tree_view);
2420 view_close(tree_view);
2423 *focus_view = tree_view;
2424 view->child_focussed = 1;
2426 *new_view = tree_view;
2429 if (strcmp(s->in_repo_path, "/") == 0)
2431 parent_path = dirname(s->in_repo_path);
2432 if (parent_path && strcmp(parent_path, ".") != 0) {
2433 err = stop_log_thread(s);
2436 lv = view_open(view->nlines, view->ncols,
2437 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2439 return got_error_from_errno(
2441 err = open_log_view(lv, s->start_id, s->refs,
2442 s->repo, s->head_ref_name, parent_path,
2446 if (view_is_parent_view(view))
2449 view_set_child(view->parent, lv);
2456 err = stop_log_thread(s);
2459 lv = view_open(view->nlines, view->ncols,
2460 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2462 return got_error_from_errno("view_open");
2463 err = got_repo_match_object_id(&start_id, NULL,
2464 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2465 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2470 in_repo_path = strdup(s->in_repo_path);
2471 if (in_repo_path == NULL) {
2474 return got_error_from_errno("strdup");
2476 got_ref_list_free(s->refs);
2477 err = got_ref_list(s->refs, s->repo, NULL,
2478 got_ref_cmp_by_name, NULL);
2484 err = open_log_view(lv, start_id, s->refs, s->repo,
2485 s->head_ref_name, in_repo_path, s->log_branches);
2495 s->log_branches = !s->log_branches;
2496 err = stop_log_thread(s);
2499 lv = view_open(view->nlines, view->ncols,
2500 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2502 return got_error_from_errno("view_open");
2503 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2504 s->head_ref_name, s->in_repo_path, s->log_branches);
2519 static const struct got_error *
2520 apply_unveil(const char *repo_path, const char *worktree_path)
2522 const struct got_error *error;
2525 if (unveil("gmon.out", "rwc") != 0)
2526 return got_error_from_errno2("unveil", "gmon.out");
2528 if (repo_path && unveil(repo_path, "r") != 0)
2529 return got_error_from_errno2("unveil", repo_path);
2531 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2532 return got_error_from_errno2("unveil", worktree_path);
2534 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2535 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2537 error = got_privsep_unveil_exec_helpers();
2541 if (unveil(NULL, NULL) != 0)
2542 return got_error_from_errno("unveil");
2552 halfdelay(1); /* Do fast refresh while initial view is loading. */
2555 intrflush(stdscr, FALSE);
2556 keypad(stdscr, TRUE);
2558 if (getenv("TOG_COLORS") != NULL) {
2560 use_default_colors();
2562 signal(SIGWINCH, tog_sigwinch);
2563 signal(SIGPIPE, tog_sigpipe);
2564 signal(SIGCONT, tog_sigcont);
2567 static const struct got_error *
2568 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2569 struct got_repository *repo, struct got_worktree *worktree)
2571 const struct got_error *err = NULL;
2574 *in_repo_path = strdup("/");
2575 if (*in_repo_path == NULL)
2576 return got_error_from_errno("strdup");
2581 const char *prefix = got_worktree_get_path_prefix(worktree);
2584 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2588 if (asprintf(&p, "%s%s%s", prefix,
2589 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2590 err = got_error_from_errno("asprintf");
2594 err = got_repo_map_path(in_repo_path, repo, p, 0);
2598 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2603 static const struct got_error *
2604 cmd_log(int argc, char *argv[])
2606 const struct got_error *error;
2607 struct got_repository *repo = NULL;
2608 struct got_worktree *worktree = NULL;
2609 struct got_reflist_head refs;
2610 struct got_object_id *start_id = NULL;
2611 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2612 char *start_commit = NULL, *head_ref_name = NULL;
2613 int ch, log_branches = 0;
2614 struct tog_view *view;
2616 SIMPLEQ_INIT(&refs);
2619 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2624 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2630 start_commit = optarg;
2633 repo_path = realpath(optarg, NULL);
2634 if (repo_path == NULL)
2635 return got_error_from_errno2("realpath",
2650 cwd = getcwd(NULL, 0);
2652 return got_error_from_errno("getcwd");
2654 error = got_worktree_open(&worktree, cwd);
2655 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2658 if (repo_path == NULL) {
2661 strdup(got_worktree_get_repo_path(worktree));
2663 repo_path = strdup(cwd);
2665 if (repo_path == NULL) {
2666 error = got_error_from_errno("strdup");
2670 error = got_repo_open(&repo, repo_path, NULL);
2674 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2681 error = apply_unveil(got_repo_get_path(repo),
2682 worktree ? got_worktree_get_root_path(worktree) : NULL);
2686 if (start_commit == NULL)
2687 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2688 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2689 GOT_OBJ_TYPE_COMMIT, 1, repo);
2691 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2692 GOT_OBJ_TYPE_COMMIT, 1, repo);
2696 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2700 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2702 error = got_error_from_errno("view_open");
2706 head_ref_name = strdup(
2707 got_worktree_get_head_ref_name(worktree));
2708 if (head_ref_name == NULL) {
2709 error = got_error_from_errno("strdup");
2713 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2714 in_repo_path, log_branches);
2718 /* Release work tree lock. */
2719 got_worktree_close(worktree);
2722 error = view_loop(view);
2728 free(head_ref_name);
2730 got_repo_close(repo);
2732 got_worktree_close(worktree);
2733 got_ref_list_free(&refs);
2741 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2747 parse_next_line(FILE *f, size_t *len)
2752 const char delim[3] = { '\0', '\0', '\0'};
2754 line = fparseln(f, &linelen, &lineno, delim, 0);
2761 match_line(const char *line, regex_t *regex)
2763 regmatch_t regmatch;
2765 return regexec(regex, line, 1, ®match, 0) == 0;
2769 match_color(struct tog_colors *colors, const char *line)
2771 struct tog_color *tc = NULL;
2773 SIMPLEQ_FOREACH(tc, colors, entry) {
2774 if (match_line(line, &tc->regex))
2781 static const struct got_error *
2782 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2783 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2784 char *header, struct tog_colors *colors)
2786 const struct got_error *err;
2787 int lineno = 0, nprinted = 0;
2789 struct tog_color *tc;
2795 werase(view->window);
2798 err = format_line(&wline, &width, header, view->ncols, 0);
2803 if (view_needs_focus_indication(view))
2804 wstandout(view->window);
2805 waddwstr(view->window, wline);
2806 if (view_needs_focus_indication(view))
2807 wstandend(view->window);
2808 if (width <= view->ncols - 1)
2809 waddch(view->window, '\n');
2817 while (nprinted < max_lines) {
2818 line = parse_next_line(f, &len);
2823 if (++lineno < *first_displayed_line) {
2828 err = format_line(&wline, &width, line, view->ncols, 0);
2834 tc = match_color(colors, line);
2836 wattr_on(view->window,
2837 COLOR_PAIR(tc->colorpair), NULL);
2838 waddwstr(view->window, wline);
2840 wattr_off(view->window,
2841 COLOR_PAIR(tc->colorpair), NULL);
2842 if (width <= view->ncols - 1)
2843 waddch(view->window, '\n');
2844 if (++nprinted == 1)
2845 *first_displayed_line = lineno;
2850 *last_displayed_line = lineno;
2855 while (nprinted < view->nlines) {
2856 waddch(view->window, '\n');
2860 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2865 wstandout(view->window);
2866 waddwstr(view->window, wline);
2867 wstandend(view->window);
2874 get_datestr(time_t *time, char *datebuf)
2876 struct tm mytm, *tm;
2879 tm = gmtime_r(time, &mytm);
2882 s = asctime_r(tm, datebuf);
2885 p = strchr(s, '\n');
2891 static const struct got_error *
2892 write_commit_info(struct got_object_id *commit_id,
2893 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2895 const struct got_error *err = NULL;
2896 char datebuf[26], *datestr;
2897 struct got_commit_object *commit;
2898 char *id_str = NULL, *logmsg = NULL;
2899 time_t committer_time;
2900 const char *author, *committer;
2901 char *refs_str = NULL;
2904 err = build_refs_str(&refs_str, refs, commit_id, repo);
2909 err = got_object_open_as_commit(&commit, repo, commit_id);
2913 err = got_object_id_str(&id_str, commit_id);
2915 err = got_error_from_errno("got_object_id_str");
2919 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2920 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2921 err = got_error_from_errno("fprintf");
2924 if (fprintf(outfile, "from: %s\n",
2925 got_object_commit_get_author(commit)) < 0) {
2926 err = got_error_from_errno("fprintf");
2929 committer_time = got_object_commit_get_committer_time(commit);
2930 datestr = get_datestr(&committer_time, datebuf);
2931 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2932 err = got_error_from_errno("fprintf");
2935 author = got_object_commit_get_author(commit);
2936 committer = got_object_commit_get_committer(commit);
2937 if (strcmp(author, committer) != 0 &&
2938 fprintf(outfile, "via: %s\n", committer) < 0) {
2939 err = got_error_from_errno("fprintf");
2942 err = got_object_commit_get_logmsg(&logmsg, commit);
2945 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2946 err = got_error_from_errno("fprintf");
2953 got_object_commit_close(commit);
2957 const struct got_error *
2958 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2964 size_t noffsets = 0;
2968 *line_offsets = NULL;
2974 if (fseek(infile, 0, SEEK_END) == -1)
2975 return got_error_from_errno("fseek");
2976 len = ftell(infile) + 1;
2978 return got_error_from_errno("ftell");
2979 if (fseek(infile, 0, SEEK_SET) == -1)
2980 return got_error_from_errno("fseek");
2984 if ((buf = calloc(len, sizeof(char *))) == NULL)
2985 return got_error_from_errno("calloc");
2987 fread(buf, 1, len, infile);
2989 return got_error_from_errno("fread");
2992 if (line_offsets && nlines) {
2993 if (*line_offsets == NULL) {
2994 /* Have some data but perhaps no '\n'. */
2997 *line_offsets = calloc(1, sizeof(**line_offsets));
2998 if (*line_offsets == NULL)
2999 return got_error_from_errno("calloc");
3000 /* Skip forward over end of first line. */
3007 /* Scan '\n' offsets in remaining chunk of data. */
3009 if (buf[i] != '\n') {
3014 if (noffsets < *nlines) {
3015 off_t *o = recallocarray(*line_offsets,
3017 sizeof(**line_offsets));
3019 free(*line_offsets);
3020 *line_offsets = NULL;
3021 return got_error_from_errno(
3028 (*line_offsets)[*nlines - 1] = off;
3033 if (fflush(infile) != 0)
3034 return got_error_from_errno("fflush");
3043 static const struct got_error *
3044 create_diff(struct tog_diff_view_state *s)
3046 const struct got_error *err = NULL;
3052 err = got_error_from_errno("got_opentemp");
3055 if (s->f && fclose(s->f) != 0) {
3056 err = got_error_from_errno("fclose");
3062 err = got_object_get_type(&obj_type, s->repo, s->id1);
3064 err = got_object_get_type(&obj_type, s->repo, s->id2);
3069 case GOT_OBJ_TYPE_BLOB:
3070 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3071 s->diff_context, 0, s->repo, s->f);
3073 case GOT_OBJ_TYPE_TREE:
3074 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3075 s->diff_context, 0, s->repo, s->f);
3077 case GOT_OBJ_TYPE_COMMIT: {
3078 const struct got_object_id_queue *parent_ids;
3079 struct got_object_qid *pid;
3080 struct got_commit_object *commit2;
3082 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3085 /* Show commit info if we're diffing to a parent/root commit. */
3086 if (s->id1 == NULL) {
3087 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3091 parent_ids = got_object_commit_get_parent_ids(commit2);
3092 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3093 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3094 err = write_commit_info(s->id2, s->refs,
3102 got_object_commit_close(commit2);
3104 err = got_diff_objects_as_commits(s->id1, s->id2,
3105 s->diff_context, 0, s->repo, s->f);
3109 err = got_error(GOT_ERR_OBJ_TYPE);
3114 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3117 if (s->f && fflush(s->f) != 0 && err == NULL)
3118 err = got_error_from_errno("fflush");
3123 diff_view_indicate_progress(struct tog_view *view)
3125 mvwaddstr(view->window, 0, 0, "diffing...");
3130 static const struct got_error *
3131 search_start_diff_view(struct tog_view *view)
3133 struct tog_diff_view_state *s = &view->state.diff;
3135 s->matched_line = 0;
3139 static const struct got_error *
3140 search_next_diff_view(struct tog_view *view)
3142 struct tog_diff_view_state *s = &view->state.diff;
3145 if (!view->searching) {
3146 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3150 if (s->matched_line) {
3151 if (view->searching == TOG_SEARCH_FORWARD)
3152 lineno = s->matched_line + 1;
3154 lineno = s->matched_line - 1;
3156 if (view->searching == TOG_SEARCH_FORWARD)
3167 if (lineno <= 0 || lineno > s->nlines) {
3168 if (s->matched_line == 0) {
3169 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3174 if (view->searching == TOG_SEARCH_FORWARD)
3180 offset = s->line_offsets[lineno - 1];
3181 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3183 return got_error_from_errno("fseeko");
3186 line = parse_next_line(s->f, &len);
3187 if (line && match_line(line, &view->regex)) {
3188 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3189 s->matched_line = lineno;
3194 if (view->searching == TOG_SEARCH_FORWARD)
3200 if (s->matched_line) {
3201 s->first_displayed_line = s->matched_line;
3202 s->selected_line = 1;
3208 static const struct got_error *
3209 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3210 struct got_object_id *id2, struct tog_view *log_view,
3211 struct got_reflist_head *refs, struct got_repository *repo)
3213 const struct got_error *err;
3214 struct tog_diff_view_state *s = &view->state.diff;
3216 if (id1 != NULL && id2 != NULL) {
3218 err = got_object_get_type(&type1, repo, id1);
3221 err = got_object_get_type(&type2, repo, id2);
3226 return got_error(GOT_ERR_OBJ_TYPE);
3228 s->first_displayed_line = 1;
3229 s->last_displayed_line = view->nlines;
3230 s->selected_line = 1;
3237 s->id1 = got_object_id_dup(id1);
3239 return got_error_from_errno("got_object_id_dup");
3243 s->id2 = got_object_id_dup(id2);
3244 if (s->id2 == NULL) {
3247 return got_error_from_errno("got_object_id_dup");
3250 s->first_displayed_line = 1;
3251 s->last_displayed_line = view->nlines;
3252 s->diff_context = 3;
3253 s->log_view = log_view;
3257 SIMPLEQ_INIT(&s->colors);
3258 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3259 err = add_color(&s->colors,
3260 "^-", TOG_COLOR_DIFF_MINUS,
3261 get_color_value("TOG_COLOR_DIFF_MINUS"));
3264 err = add_color(&s->colors, "^\\+",
3265 TOG_COLOR_DIFF_PLUS,
3266 get_color_value("TOG_COLOR_DIFF_PLUS"));
3268 free_colors(&s->colors);
3271 err = add_color(&s->colors,
3272 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3273 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3275 free_colors(&s->colors);
3279 err = add_color(&s->colors,
3280 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3281 get_color_value("TOG_COLOR_DIFF_META"));
3283 free_colors(&s->colors);
3287 err = add_color(&s->colors,
3288 "^(from|via): ", TOG_COLOR_AUTHOR,
3289 get_color_value("TOG_COLOR_AUTHOR"));
3291 free_colors(&s->colors);
3295 err = add_color(&s->colors,
3296 "^date: ", TOG_COLOR_DATE,
3297 get_color_value("TOG_COLOR_DATE"));
3299 free_colors(&s->colors);
3304 if (log_view && view_is_splitscreen(view))
3305 show_log_view(log_view); /* draw vborder */
3306 diff_view_indicate_progress(view);
3308 err = create_diff(s);
3317 view->show = show_diff_view;
3318 view->input = input_diff_view;
3319 view->close = close_diff_view;
3320 view->search_start = search_start_diff_view;
3321 view->search_next = search_next_diff_view;
3326 static const struct got_error *
3327 close_diff_view(struct tog_view *view)
3329 const struct got_error *err = NULL;
3330 struct tog_diff_view_state *s = &view->state.diff;
3336 if (s->f && fclose(s->f) == EOF)
3337 err = got_error_from_errno("fclose");
3338 free_colors(&s->colors);
3339 free(s->line_offsets);
3343 static const struct got_error *
3344 show_diff_view(struct tog_view *view)
3346 const struct got_error *err;
3347 struct tog_diff_view_state *s = &view->state.diff;
3348 char *id_str1 = NULL, *id_str2, *header;
3351 err = got_object_id_str(&id_str1, s->id1);
3355 err = got_object_id_str(&id_str2, s->id2);
3359 if (asprintf(&header, "diff %s %s",
3360 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3361 err = got_error_from_errno("asprintf");
3369 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3370 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3371 header, &s->colors);
3374 static const struct got_error *
3375 set_selected_commit(struct tog_diff_view_state *s,
3376 struct commit_queue_entry *entry)
3378 const struct got_error *err;
3379 const struct got_object_id_queue *parent_ids;
3380 struct got_commit_object *selected_commit;
3381 struct got_object_qid *pid;
3384 s->id2 = got_object_id_dup(entry->id);
3386 return got_error_from_errno("got_object_id_dup");
3388 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3391 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3393 pid = SIMPLEQ_FIRST(parent_ids);
3394 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3395 got_object_commit_close(selected_commit);
3399 static const struct got_error *
3400 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3401 struct tog_view **focus_view, struct tog_view *view, int ch)
3403 const struct got_error *err = NULL;
3404 struct tog_diff_view_state *s = &view->state.diff;
3405 struct tog_log_view_state *ls;
3406 struct commit_queue_entry *entry;
3412 if (s->first_displayed_line > 1)
3413 s->first_displayed_line--;
3417 if (s->first_displayed_line == 1)
3420 while (i++ < view->nlines - 1 &&
3421 s->first_displayed_line > 1)
3422 s->first_displayed_line--;
3427 s->first_displayed_line++;
3435 while (!s->eof && i++ < view->nlines - 1) {
3437 line = parse_next_line(s->f, NULL);
3438 s->first_displayed_line++;
3444 if (s->diff_context > 0) {
3446 diff_view_indicate_progress(view);
3447 err = create_diff(s);
3451 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3453 diff_view_indicate_progress(view);
3454 err = create_diff(s);
3459 if (s->log_view == NULL)
3461 ls = &s->log_view->state.log;
3462 entry = TAILQ_PREV(ls->selected_entry,
3463 commit_queue_head, entry);
3467 err = input_log_view(NULL, NULL, NULL, s->log_view,
3472 err = set_selected_commit(s, entry);
3476 s->first_displayed_line = 1;
3477 s->last_displayed_line = view->nlines;
3479 diff_view_indicate_progress(view);
3480 err = create_diff(s);
3484 if (s->log_view == NULL)
3486 ls = &s->log_view->state.log;
3488 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3489 ls->thread_args.commits_needed++;
3490 err = trigger_log_thread(s->log_view, 1,
3491 &ls->thread_args.commits_needed,
3492 &ls->thread_args.log_complete,
3493 &ls->thread_args.need_commits,
3494 &ls->thread_args.commit_loaded);
3498 err = input_log_view(NULL, NULL, NULL, s->log_view,
3503 entry = TAILQ_NEXT(ls->selected_entry, entry);
3507 err = set_selected_commit(s, entry);
3511 s->first_displayed_line = 1;
3512 s->last_displayed_line = view->nlines;
3514 diff_view_indicate_progress(view);
3515 err = create_diff(s);
3524 static const struct got_error *
3525 cmd_diff(int argc, char *argv[])
3527 const struct got_error *error = NULL;
3528 struct got_repository *repo = NULL;
3529 struct got_worktree *worktree = NULL;
3530 struct got_reflist_head refs;
3531 struct got_object_id *id1 = NULL, *id2 = NULL;
3532 char *repo_path = NULL, *cwd = NULL;
3533 char *id_str1 = NULL, *id_str2 = NULL;
3535 struct tog_view *view;
3537 SIMPLEQ_INIT(&refs);
3540 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3545 while ((ch = getopt(argc, argv, "r:")) != -1) {
3548 repo_path = realpath(optarg, NULL);
3549 if (repo_path == NULL)
3550 return got_error_from_errno2("realpath",
3563 usage_diff(); /* TODO show local worktree changes */
3564 } else if (argc == 2) {
3570 cwd = getcwd(NULL, 0);
3572 return got_error_from_errno("getcwd");
3574 error = got_worktree_open(&worktree, cwd);
3575 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3578 if (repo_path == NULL) {
3581 strdup(got_worktree_get_repo_path(worktree));
3583 repo_path = strdup(cwd);
3585 if (repo_path == NULL) {
3586 error = got_error_from_errno("strdup");
3590 error = got_repo_open(&repo, repo_path, NULL);
3596 error = apply_unveil(got_repo_get_path(repo), NULL);
3600 error = got_repo_match_object_id_prefix(&id1, id_str1,
3601 GOT_OBJ_TYPE_ANY, repo);
3605 error = got_repo_match_object_id_prefix(&id2, id_str2,
3606 GOT_OBJ_TYPE_ANY, repo);
3610 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3614 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3616 error = got_error_from_errno("view_open");
3619 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3622 error = view_loop(view);
3627 got_repo_close(repo);
3629 got_worktree_close(worktree);
3630 got_ref_list_free(&refs);
3638 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3643 struct tog_blame_line {
3645 struct got_object_id *id;
3648 static const struct got_error *
3649 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3650 const char *path, struct tog_blame_line *lines, int nlines,
3651 int blame_complete, int selected_line, int *first_displayed_line,
3652 int *last_displayed_line, int *eof, int max_lines,
3653 struct tog_colors *colors)
3655 const struct got_error *err;
3656 int lineno = 0, nprinted = 0;
3661 struct tog_blame_line *blame_line;
3662 struct got_object_id *prev_id = NULL;
3664 struct tog_color *tc;
3666 err = got_object_id_str(&id_str, id);
3671 werase(view->window);
3673 if (asprintf(&line, "commit %s", id_str) == -1) {
3674 err = got_error_from_errno("asprintf");
3679 err = format_line(&wline, &width, line, view->ncols, 0);
3684 if (view_needs_focus_indication(view))
3685 wstandout(view->window);
3686 tc = get_color(colors, TOG_COLOR_COMMIT);
3688 wattr_on(view->window,
3689 COLOR_PAIR(tc->colorpair), NULL);
3690 waddwstr(view->window, wline);
3692 wattr_off(view->window,
3693 COLOR_PAIR(tc->colorpair), NULL);
3694 if (view_needs_focus_indication(view))
3695 wstandend(view->window);
3698 if (width < view->ncols - 1)
3699 waddch(view->window, '\n');
3701 if (asprintf(&line, "[%d/%d] %s%s",
3702 *first_displayed_line - 1 + selected_line, nlines,
3703 blame_complete ? "" : "annotating... ", path) == -1) {
3705 return got_error_from_errno("asprintf");
3708 err = format_line(&wline, &width, line, view->ncols, 0);
3713 waddwstr(view->window, wline);
3716 if (width < view->ncols - 1)
3717 waddch(view->window, '\n');
3720 while (nprinted < max_lines - 2) {
3721 line = parse_next_line(f, &len);
3726 if (++lineno < *first_displayed_line) {
3731 if (view->ncols <= 9) {
3733 wline = wcsdup(L"");
3735 err = got_error_from_errno("wcsdup");
3737 err = format_line(&wline, &width, line,
3738 view->ncols - 9, 9);
3746 if (view->focussed && nprinted == selected_line - 1)
3747 wstandout(view->window);
3750 blame_line = &lines[lineno - 1];
3751 if (blame_line->annotated && prev_id &&
3752 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3754 nprinted == selected_line - 1)) {
3755 waddstr(view->window, " ");
3756 } else if (blame_line->annotated) {
3758 err = got_object_id_str(&id_str, blame_line->id);
3764 tc = get_color(colors, TOG_COLOR_COMMIT);
3766 wattr_on(view->window,
3767 COLOR_PAIR(tc->colorpair), NULL);
3768 wprintw(view->window, "%.8s", id_str);
3770 wattr_off(view->window,
3771 COLOR_PAIR(tc->colorpair), NULL);
3773 prev_id = blame_line->id;
3775 waddstr(view->window, "........");
3779 waddstr(view->window, "........");
3783 if (view->focussed && nprinted == selected_line - 1)
3784 wstandend(view->window);
3785 waddstr(view->window, " ");
3787 waddwstr(view->window, wline);
3788 if (width <= view->ncols - 1)
3789 waddch(view->window, '\n');
3790 if (++nprinted == 1)
3791 *first_displayed_line = lineno;
3796 *last_displayed_line = lineno;
3803 static const struct got_error *
3804 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3806 const struct got_error *err = NULL;
3807 struct tog_blame_cb_args *a = arg;
3808 struct tog_blame_line *line;
3811 if (nlines != a->nlines ||
3812 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3813 return got_error(GOT_ERR_RANGE);
3815 errcode = pthread_mutex_lock(&tog_mutex);
3817 return got_error_set_errno(errcode, "pthread_mutex_lock");
3819 if (*a->quit) { /* user has quit the blame view */
3820 err = got_error(GOT_ERR_ITER_COMPLETED);
3825 goto done; /* no change in this commit */
3827 line = &a->lines[lineno - 1];
3828 if (line->annotated)
3831 line->id = got_object_id_dup(id);
3832 if (line->id == NULL) {
3833 err = got_error_from_errno("got_object_id_dup");
3836 line->annotated = 1;
3838 errcode = pthread_mutex_unlock(&tog_mutex);
3840 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3845 blame_thread(void *arg)
3847 const struct got_error *err;
3848 struct tog_blame_thread_args *ta = arg;
3849 struct tog_blame_cb_args *a = ta->cb_args;
3852 err = block_signals_used_by_main_thread();
3856 err = got_blame(ta->path, a->commit_id, ta->repo,
3857 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3858 if (err && err->code == GOT_ERR_CANCELLED)
3861 errcode = pthread_mutex_lock(&tog_mutex);
3863 return (void *)got_error_set_errno(errcode,
3864 "pthread_mutex_lock");
3866 got_repo_close(ta->repo);
3870 errcode = pthread_mutex_unlock(&tog_mutex);
3871 if (errcode && err == NULL)
3872 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3877 static struct got_object_id *
3878 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3879 int first_displayed_line, int selected_line)
3881 struct tog_blame_line *line;
3886 line = &lines[first_displayed_line - 1 + selected_line - 1];
3887 if (!line->annotated)
3893 static const struct got_error *
3894 stop_blame(struct tog_blame *blame)
3896 const struct got_error *err = NULL;
3899 if (blame->thread) {
3901 errcode = pthread_mutex_unlock(&tog_mutex);
3903 return got_error_set_errno(errcode,
3904 "pthread_mutex_unlock");
3905 errcode = pthread_join(blame->thread, (void **)&err);
3907 return got_error_set_errno(errcode, "pthread_join");
3908 errcode = pthread_mutex_lock(&tog_mutex);
3910 return got_error_set_errno(errcode,
3911 "pthread_mutex_lock");
3912 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3914 blame->thread = NULL;
3916 if (blame->thread_args.repo) {
3917 got_repo_close(blame->thread_args.repo);
3918 blame->thread_args.repo = NULL;
3921 if (fclose(blame->f) != 0 && err == NULL)
3922 err = got_error_from_errno("fclose");
3926 for (i = 0; i < blame->nlines; i++)
3927 free(blame->lines[i].id);
3929 blame->lines = NULL;
3931 free(blame->cb_args.commit_id);
3932 blame->cb_args.commit_id = NULL;
3937 static const struct got_error *
3938 cancel_blame_view(void *arg)
3940 const struct got_error *err = NULL;
3944 errcode = pthread_mutex_lock(&tog_mutex);
3946 return got_error_set_errno(errcode,
3947 "pthread_mutex_unlock");
3950 err = got_error(GOT_ERR_CANCELLED);
3952 errcode = pthread_mutex_unlock(&tog_mutex);
3954 return got_error_set_errno(errcode,
3955 "pthread_mutex_lock");
3960 static const struct got_error *
3961 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3962 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3963 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3964 struct got_repository *repo)
3966 const struct got_error *err = NULL;
3967 struct got_blob_object *blob = NULL;
3968 struct got_repository *thread_repo = NULL;
3969 struct got_object_id *obj_id = NULL;
3972 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3976 err = got_object_get_type(&obj_type, repo, obj_id);
3980 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3981 err = got_error(GOT_ERR_OBJ_TYPE);
3985 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3988 blame->f = got_opentemp();
3989 if (blame->f == NULL) {
3990 err = got_error_from_errno("got_opentemp");
3993 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3994 &blame->line_offsets, blame->f, blob);
3995 if (err || blame->nlines == 0)
3998 /* Don't include \n at EOF in the blame line count. */
3999 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4002 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4003 if (blame->lines == NULL) {
4004 err = got_error_from_errno("calloc");
4008 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4012 blame->cb_args.view = view;
4013 blame->cb_args.lines = blame->lines;
4014 blame->cb_args.nlines = blame->nlines;
4015 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4016 if (blame->cb_args.commit_id == NULL) {
4017 err = got_error_from_errno("got_object_id_dup");
4020 blame->cb_args.quit = done;
4022 blame->thread_args.path = path;
4023 blame->thread_args.repo = thread_repo;
4024 blame->thread_args.cb_args = &blame->cb_args;
4025 blame->thread_args.complete = blame_complete;
4026 blame->thread_args.cancel_cb = cancel_blame_view;
4027 blame->thread_args.cancel_arg = done;
4028 *blame_complete = 0;
4032 got_object_blob_close(blob);
4039 static const struct got_error *
4040 open_blame_view(struct tog_view *view, char *path,
4041 struct got_object_id *commit_id, struct got_reflist_head *refs,
4042 struct got_repository *repo)
4044 const struct got_error *err = NULL;
4045 struct tog_blame_view_state *s = &view->state.blame;
4047 SIMPLEQ_INIT(&s->blamed_commits);
4049 s->path = strdup(path);
4050 if (s->path == NULL)
4051 return got_error_from_errno("strdup");
4053 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4059 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4060 s->first_displayed_line = 1;
4061 s->last_displayed_line = view->nlines;
4062 s->selected_line = 1;
4063 s->blame_complete = 0;
4066 s->commit_id = commit_id;
4067 memset(&s->blame, 0, sizeof(s->blame));
4069 SIMPLEQ_INIT(&s->colors);
4070 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4071 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4072 get_color_value("TOG_COLOR_COMMIT"));
4077 view->show = show_blame_view;
4078 view->input = input_blame_view;
4079 view->close = close_blame_view;
4080 view->search_start = search_start_blame_view;
4081 view->search_next = search_next_blame_view;
4083 return run_blame(&s->blame, view, &s->blame_complete,
4084 &s->first_displayed_line, &s->last_displayed_line,
4085 &s->selected_line, &s->done, &s->eof, s->path,
4086 s->blamed_commit->id, s->repo);
4089 static const struct got_error *
4090 close_blame_view(struct tog_view *view)
4092 const struct got_error *err = NULL;
4093 struct tog_blame_view_state *s = &view->state.blame;
4095 if (s->blame.thread)
4096 err = stop_blame(&s->blame);
4098 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4099 struct got_object_qid *blamed_commit;
4100 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4101 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4102 got_object_qid_free(blamed_commit);
4106 free_colors(&s->colors);
4111 static const struct got_error *
4112 search_start_blame_view(struct tog_view *view)
4114 struct tog_blame_view_state *s = &view->state.blame;
4116 s->matched_line = 0;
4120 static const struct got_error *
4121 search_next_blame_view(struct tog_view *view)
4123 struct tog_blame_view_state *s = &view->state.blame;
4126 if (!view->searching) {
4127 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4131 if (s->matched_line) {
4132 if (view->searching == TOG_SEARCH_FORWARD)
4133 lineno = s->matched_line + 1;
4135 lineno = s->matched_line - 1;
4137 if (view->searching == TOG_SEARCH_FORWARD)
4140 lineno = s->blame.nlines;
4148 if (lineno <= 0 || lineno > s->blame.nlines) {
4149 if (s->matched_line == 0) {
4150 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4155 if (view->searching == TOG_SEARCH_FORWARD)
4158 lineno = s->blame.nlines;
4161 offset = s->blame.line_offsets[lineno - 1];
4162 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4164 return got_error_from_errno("fseeko");
4167 line = parse_next_line(s->blame.f, &len);
4168 if (line && match_line(line, &view->regex)) {
4169 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4170 s->matched_line = lineno;
4175 if (view->searching == TOG_SEARCH_FORWARD)
4181 if (s->matched_line) {
4182 s->first_displayed_line = s->matched_line;
4183 s->selected_line = 1;
4189 static const struct got_error *
4190 show_blame_view(struct tog_view *view)
4192 const struct got_error *err = NULL;
4193 struct tog_blame_view_state *s = &view->state.blame;
4196 if (s->blame.thread == NULL) {
4197 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4198 &s->blame.thread_args);
4200 return got_error_set_errno(errcode, "pthread_create");
4202 halfdelay(1); /* fast refresh while annotating */
4205 if (s->blame_complete)
4206 halfdelay(10); /* disable fast refresh */
4208 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4209 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4210 s->selected_line, &s->first_displayed_line,
4211 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4217 static const struct got_error *
4218 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4219 struct tog_view **focus_view, struct tog_view *view, int ch)
4221 const struct got_error *err = NULL, *thread_err = NULL;
4222 struct tog_view *diff_view;
4223 struct tog_blame_view_state *s = &view->state.blame;
4232 if (s->selected_line > 1)
4234 else if (s->selected_line == 1 &&
4235 s->first_displayed_line > 1)
4236 s->first_displayed_line--;
4240 if (s->first_displayed_line == 1) {
4241 s->selected_line = 1;
4244 if (s->first_displayed_line > view->nlines - 2)
4245 s->first_displayed_line -=
4248 s->first_displayed_line = 1;
4252 if (s->selected_line < view->nlines - 2 &&
4253 s->first_displayed_line +
4254 s->selected_line <= s->blame.nlines)
4256 else if (s->last_displayed_line <
4258 s->first_displayed_line++;
4262 struct got_object_id *id = NULL;
4263 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4264 s->first_displayed_line, s->selected_line);
4268 struct got_commit_object *commit;
4269 struct got_object_qid *pid;
4270 struct got_object_id *blob_id = NULL;
4272 err = got_object_open_as_commit(&commit,
4276 pid = SIMPLEQ_FIRST(
4277 got_object_commit_get_parent_ids(commit));
4279 got_object_commit_close(commit);
4282 /* Check if path history ends here. */
4283 err = got_object_id_by_path(&blob_id, s->repo,
4286 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4288 got_object_commit_close(commit);
4291 err = got_object_get_type(&obj_type, s->repo,
4294 /* Can't blame non-blob type objects. */
4295 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4296 got_object_commit_close(commit);
4299 err = got_object_qid_alloc(&s->blamed_commit,
4301 got_object_commit_close(commit);
4303 if (got_object_id_cmp(id,
4304 s->blamed_commit->id) == 0)
4306 err = got_object_qid_alloc(&s->blamed_commit,
4312 thread_err = stop_blame(&s->blame);
4316 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4317 s->blamed_commit, entry);
4318 err = run_blame(&s->blame, view, &s->blame_complete,
4319 &s->first_displayed_line, &s->last_displayed_line,
4320 &s->selected_line, &s->done, &s->eof,
4321 s->path, s->blamed_commit->id, s->repo);
4327 struct got_object_qid *first;
4328 first = SIMPLEQ_FIRST(&s->blamed_commits);
4329 if (!got_object_id_cmp(first->id, s->commit_id))
4332 thread_err = stop_blame(&s->blame);
4336 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4337 got_object_qid_free(s->blamed_commit);
4339 SIMPLEQ_FIRST(&s->blamed_commits);
4340 err = run_blame(&s->blame, view, &s->blame_complete,
4341 &s->first_displayed_line, &s->last_displayed_line,
4342 &s->selected_line, &s->done, &s->eof, s->path,
4343 s->blamed_commit->id, s->repo);
4350 struct got_object_id *id = NULL;
4351 struct got_object_qid *pid;
4352 struct got_commit_object *commit = NULL;
4353 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4354 s->first_displayed_line, s->selected_line);
4357 err = got_object_open_as_commit(&commit, s->repo, id);
4360 pid = SIMPLEQ_FIRST(
4361 got_object_commit_get_parent_ids(commit));
4362 if (view_is_parent_view(view))
4363 begin_x = view_split_begin_x(view->begin_x);
4364 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4365 if (diff_view == NULL) {
4366 got_object_commit_close(commit);
4367 err = got_error_from_errno("view_open");
4370 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4371 id, NULL, s->refs, s->repo);
4372 got_object_commit_close(commit);
4374 view_close(diff_view);
4377 if (view_is_parent_view(view)) {
4378 err = view_close_child(view);
4381 err = view_set_child(view, diff_view);
4383 view_close(diff_view);
4386 *focus_view = diff_view;
4387 view->child_focussed = 1;
4389 *new_view = diff_view;
4397 if (s->last_displayed_line >= s->blame.nlines &&
4398 s->selected_line >= MIN(s->blame.nlines,
4399 view->nlines - 2)) {
4402 if (s->last_displayed_line >= s->blame.nlines &&
4403 s->selected_line < view->nlines - 2) {
4404 s->selected_line = MIN(s->blame.nlines,
4408 if (s->last_displayed_line + view->nlines - 2
4410 s->first_displayed_line +=
4413 s->first_displayed_line =
4418 if (s->selected_line > view->nlines - 2) {
4419 s->selected_line = MIN(s->blame.nlines,
4426 return thread_err ? thread_err : err;
4429 static const struct got_error *
4430 cmd_blame(int argc, char *argv[])
4432 const struct got_error *error;
4433 struct got_repository *repo = NULL;
4434 struct got_reflist_head refs;
4435 struct got_worktree *worktree = NULL;
4436 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4437 struct got_object_id *commit_id = NULL;
4438 char *commit_id_str = NULL;
4440 struct tog_view *view;
4442 SIMPLEQ_INIT(&refs);
4445 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4450 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4453 commit_id_str = optarg;
4456 repo_path = realpath(optarg, NULL);
4457 if (repo_path == NULL)
4458 return got_error_from_errno2("realpath",
4473 cwd = getcwd(NULL, 0);
4475 return got_error_from_errno("getcwd");
4477 error = got_worktree_open(&worktree, cwd);
4478 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4481 if (repo_path == NULL) {
4484 strdup(got_worktree_get_repo_path(worktree));
4486 repo_path = strdup(cwd);
4488 if (repo_path == NULL) {
4489 error = got_error_from_errno("strdup");
4493 error = got_repo_open(&repo, repo_path, NULL);
4497 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4504 error = apply_unveil(got_repo_get_path(repo), NULL);
4508 if (commit_id_str == NULL) {
4509 struct got_reference *head_ref;
4510 error = got_ref_open(&head_ref, repo, worktree ?
4511 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4514 error = got_ref_resolve(&commit_id, repo, head_ref);
4515 got_ref_close(head_ref);
4517 error = got_repo_match_object_id(&commit_id, NULL,
4518 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4527 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4529 error = got_error_from_errno("view_open");
4532 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4536 /* Release work tree lock. */
4537 got_worktree_close(worktree);
4540 error = view_loop(view);
4547 got_worktree_close(worktree);
4549 got_repo_close(repo);
4550 got_ref_list_free(&refs);
4554 static const struct got_error *
4555 draw_tree_entries(struct tog_view *view,
4556 struct got_tree_entry **first_displayed_entry,
4557 struct got_tree_entry **last_displayed_entry,
4558 struct got_tree_entry **selected_entry, int *ndisplayed,
4559 const char *label, int show_ids, const char *parent_path,
4560 struct got_tree_object *tree, int selected, int limit,
4561 int isroot, struct tog_colors *colors)
4563 const struct got_error *err = NULL;
4564 struct got_tree_entry *te;
4566 struct tog_color *tc;
4567 int width, n, i, nentries;
4571 werase(view->window);
4576 err = format_line(&wline, &width, label, view->ncols, 0);
4579 if (view_needs_focus_indication(view))
4580 wstandout(view->window);
4581 tc = get_color(colors, TOG_COLOR_COMMIT);
4583 wattr_on(view->window,
4584 COLOR_PAIR(tc->colorpair), NULL);
4585 waddwstr(view->window, wline);
4587 wattr_off(view->window,
4588 COLOR_PAIR(tc->colorpair), NULL);
4589 if (view_needs_focus_indication(view))
4590 wstandend(view->window);
4593 if (width < view->ncols - 1)
4594 waddch(view->window, '\n');
4597 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4600 waddwstr(view->window, wline);
4603 if (width < view->ncols - 1)
4604 waddch(view->window, '\n');
4607 waddch(view->window, '\n');
4611 if (*first_displayed_entry == NULL) {
4612 te = got_object_tree_get_first_entry(tree);
4613 if (selected == 0) {
4615 wstandout(view->window);
4616 *selected_entry = NULL;
4618 waddstr(view->window, " ..\n"); /* parent directory */
4619 if (selected == 0 && view->focussed)
4620 wstandend(view->window);
4627 te = *first_displayed_entry;
4630 nentries = got_object_tree_get_nentries(tree);
4631 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4632 char *line = NULL, *id_str = NULL;
4633 const char *modestr = "";
4636 te = got_object_tree_get_entry(tree, i);
4637 mode = got_tree_entry_get_mode(te);
4640 err = got_object_id_str(&id_str,
4641 got_tree_entry_get_id(te));
4643 return got_error_from_errno(
4644 "got_object_id_str");
4646 if (got_object_tree_entry_is_submodule(te))
4648 else if (S_ISLNK(mode))
4650 else if (S_ISDIR(mode))
4652 else if (mode & S_IXUSR)
4654 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4655 got_tree_entry_get_name(te), modestr) == -1) {
4657 return got_error_from_errno("asprintf");
4660 err = format_line(&wline, &width, line, view->ncols, 0);
4665 if (n == selected) {
4667 wstandout(view->window);
4668 *selected_entry = te;
4670 tc = match_color(colors, line);
4672 wattr_on(view->window,
4673 COLOR_PAIR(tc->colorpair), NULL);
4674 waddwstr(view->window, wline);
4676 wattr_off(view->window,
4677 COLOR_PAIR(tc->colorpair), NULL);
4678 if (width < view->ncols - 1)
4679 waddch(view->window, '\n');
4680 if (n == selected && view->focussed)
4681 wstandend(view->window);
4687 *last_displayed_entry = te;
4696 tree_scroll_up(struct tog_view *view,
4697 struct got_tree_entry **first_displayed_entry, int maxscroll,
4698 struct got_tree_object *tree, int isroot)
4700 struct got_tree_entry *te;
4703 if (*first_displayed_entry == NULL)
4706 te = got_object_tree_get_entry(tree, 0);
4707 if (*first_displayed_entry == te) {
4709 *first_displayed_entry = NULL;
4714 while (*first_displayed_entry && i < maxscroll) {
4715 *first_displayed_entry = got_tree_entry_get_prev(tree,
4716 *first_displayed_entry);
4719 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4720 *first_displayed_entry = NULL;
4724 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4725 struct got_tree_entry *last_displayed_entry,
4726 struct got_tree_object *tree)
4728 struct got_tree_entry *next, *last;
4731 if (*first_displayed_entry)
4732 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4734 next = got_object_tree_get_first_entry(tree);
4736 last = last_displayed_entry;
4737 while (next && last && n++ < maxscroll) {
4738 last = got_tree_entry_get_next(tree, last);
4740 *first_displayed_entry = next;
4741 next = got_tree_entry_get_next(tree, next);
4747 static const struct got_error *
4748 tree_entry_path(char **path, struct tog_parent_trees *parents,
4749 struct got_tree_entry *te)
4751 const struct got_error *err = NULL;
4752 struct tog_parent_tree *pt;
4753 size_t len = 2; /* for leading slash and NUL */
4755 TAILQ_FOREACH(pt, parents, entry)
4756 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4759 len += strlen(got_tree_entry_get_name(te));
4761 *path = calloc(1, len);
4763 return got_error_from_errno("calloc");
4766 pt = TAILQ_LAST(parents, tog_parent_trees);
4768 const char *name = got_tree_entry_get_name(pt->selected_entry);
4769 if (strlcat(*path, name, len) >= len) {
4770 err = got_error(GOT_ERR_NO_SPACE);
4773 if (strlcat(*path, "/", len) >= len) {
4774 err = got_error(GOT_ERR_NO_SPACE);
4777 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4780 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4781 err = got_error(GOT_ERR_NO_SPACE);
4793 static const struct got_error *
4794 blame_tree_entry(struct tog_view **new_view, int begin_x,
4795 struct got_tree_entry *te, struct tog_parent_trees *parents,
4796 struct got_object_id *commit_id, struct got_reflist_head *refs,
4797 struct got_repository *repo)
4799 const struct got_error *err = NULL;
4801 struct tog_view *blame_view;
4805 err = tree_entry_path(&path, parents, te);
4809 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4810 if (blame_view == NULL) {
4811 err = got_error_from_errno("view_open");
4815 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4817 if (err->code == GOT_ERR_CANCELLED)
4819 view_close(blame_view);
4821 *new_view = blame_view;
4827 static const struct got_error *
4828 log_tree_entry(struct tog_view **new_view, int begin_x,
4829 struct got_tree_entry *te, struct tog_parent_trees *parents,
4830 struct got_object_id *commit_id, struct got_reflist_head *refs,
4831 struct got_repository *repo)
4833 struct tog_view *log_view;
4834 const struct got_error *err = NULL;
4839 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4840 if (log_view == NULL)
4841 return got_error_from_errno("view_open");
4843 err = tree_entry_path(&path, parents, te);
4847 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4849 view_close(log_view);
4851 *new_view = log_view;
4856 static const struct got_error *
4857 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4858 struct got_object_id *commit_id, struct got_reflist_head *refs,
4859 struct got_repository *repo)
4861 const struct got_error *err = NULL;
4862 char *commit_id_str = NULL;
4863 struct tog_tree_view_state *s = &view->state.tree;
4865 TAILQ_INIT(&s->parents);
4867 err = got_object_id_str(&commit_id_str, commit_id);
4871 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4872 err = got_error_from_errno("asprintf");
4876 s->root = s->tree = root;
4877 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4878 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4879 s->commit_id = got_object_id_dup(commit_id);
4880 if (s->commit_id == NULL) {
4881 err = got_error_from_errno("got_object_id_dup");
4887 SIMPLEQ_INIT(&s->colors);
4889 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4890 err = add_color(&s->colors, "\\$$",
4891 TOG_COLOR_TREE_SUBMODULE,
4892 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4895 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4896 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4898 free_colors(&s->colors);
4901 err = add_color(&s->colors, "/$",
4902 TOG_COLOR_TREE_DIRECTORY,
4903 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4905 free_colors(&s->colors);
4909 err = add_color(&s->colors, "\\*$",
4910 TOG_COLOR_TREE_EXECUTABLE,
4911 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4913 free_colors(&s->colors);
4917 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4918 get_color_value("TOG_COLOR_COMMIT"));
4920 free_colors(&s->colors);
4925 view->show = show_tree_view;
4926 view->input = input_tree_view;
4927 view->close = close_tree_view;
4928 view->search_start = search_start_tree_view;
4929 view->search_next = search_next_tree_view;
4931 free(commit_id_str);
4933 free(s->tree_label);
4934 s->tree_label = NULL;
4939 static const struct got_error *
4940 close_tree_view(struct tog_view *view)
4942 struct tog_tree_view_state *s = &view->state.tree;
4944 free_colors(&s->colors);
4945 free(s->tree_label);
4946 s->tree_label = NULL;
4948 s->commit_id = NULL;
4949 while (!TAILQ_EMPTY(&s->parents)) {
4950 struct tog_parent_tree *parent;
4951 parent = TAILQ_FIRST(&s->parents);
4952 TAILQ_REMOVE(&s->parents, parent, entry);
4956 if (s->tree != s->root)
4957 got_object_tree_close(s->tree);
4958 got_object_tree_close(s->root);
4963 static const struct got_error *
4964 search_start_tree_view(struct tog_view *view)
4966 struct tog_tree_view_state *s = &view->state.tree;
4968 s->matched_entry = NULL;
4973 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4975 regmatch_t regmatch;
4977 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4981 static const struct got_error *
4982 search_next_tree_view(struct tog_view *view)
4984 struct tog_tree_view_state *s = &view->state.tree;
4985 struct got_tree_entry *te = NULL;
4987 if (!view->searching) {
4988 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4992 if (s->matched_entry) {
4993 if (view->searching == TOG_SEARCH_FORWARD) {
4994 if (s->selected_entry)
4995 te = got_tree_entry_get_next(s->tree,
4998 te = got_object_tree_get_first_entry(s->tree);
5000 if (s->selected_entry == NULL)
5001 te = got_object_tree_get_last_entry(s->tree);
5003 te = got_tree_entry_get_prev(s->tree,
5007 if (view->searching == TOG_SEARCH_FORWARD)
5008 te = got_object_tree_get_first_entry(s->tree);
5010 te = got_object_tree_get_last_entry(s->tree);
5015 if (s->matched_entry == NULL) {
5016 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5019 if (view->searching == TOG_SEARCH_FORWARD)
5020 te = got_object_tree_get_first_entry(s->tree);
5022 te = got_object_tree_get_last_entry(s->tree);
5025 if (match_tree_entry(te, &view->regex)) {
5026 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5027 s->matched_entry = te;
5031 if (view->searching == TOG_SEARCH_FORWARD)
5032 te = got_tree_entry_get_next(s->tree, te);
5034 te = got_tree_entry_get_prev(s->tree, te);
5037 if (s->matched_entry) {
5038 s->first_displayed_entry = s->matched_entry;
5045 static const struct got_error *
5046 show_tree_view(struct tog_view *view)
5048 const struct got_error *err = NULL;
5049 struct tog_tree_view_state *s = &view->state.tree;
5052 err = tree_entry_path(&parent_path, &s->parents, NULL);
5056 err = draw_tree_entries(view, &s->first_displayed_entry,
5057 &s->last_displayed_entry, &s->selected_entry,
5058 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5059 s->tree, s->selected, view->nlines, s->tree == s->root,
5067 static const struct got_error *
5068 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5069 struct tog_view **focus_view, struct tog_view *view, int ch)
5071 const struct got_error *err = NULL;
5072 struct tog_tree_view_state *s = &view->state.tree;
5073 struct tog_view *log_view;
5074 int begin_x = 0, nscrolled;
5078 s->show_ids = !s->show_ids;
5081 if (!s->selected_entry)
5083 if (view_is_parent_view(view))
5084 begin_x = view_split_begin_x(view->begin_x);
5085 err = log_tree_entry(&log_view, begin_x,
5086 s->selected_entry, &s->parents,
5087 s->commit_id, s->refs, s->repo);
5088 if (view_is_parent_view(view)) {
5089 err = view_close_child(view);
5092 err = view_set_child(view, log_view);
5094 view_close(log_view);
5097 *focus_view = log_view;
5098 view->child_focussed = 1;
5100 *new_view = log_view;
5104 if (s->selected > 0) {
5106 if (s->selected == 0)
5109 if (s->selected > 0)
5111 tree_scroll_up(view, &s->first_displayed_entry, 1,
5112 s->tree, s->tree == s->root);
5116 tree_scroll_up(view, &s->first_displayed_entry,
5117 MAX(0, view->nlines - 4 - s->selected), s->tree,
5118 s->tree == s->root);
5120 if (got_object_tree_get_first_entry(s->tree) ==
5121 s->first_displayed_entry && s->tree != s->root)
5122 s->first_displayed_entry = NULL;
5126 if (s->selected < s->ndisplayed - 1) {
5130 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5132 /* can't scroll any further */
5134 tree_scroll_down(&s->first_displayed_entry, 1,
5135 s->last_displayed_entry, s->tree);
5139 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5141 /* can't scroll any further; move cursor down */
5142 if (s->selected < s->ndisplayed - 1)
5143 s->selected = s->ndisplayed - 1;
5146 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5147 view->nlines, s->last_displayed_entry, s->tree);
5148 if (nscrolled < view->nlines) {
5150 struct got_tree_entry *te;
5151 te = s->first_displayed_entry;
5154 te = got_tree_entry_get_next(s->tree, te);
5156 s->selected = ndisplayed - 1;
5162 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5163 struct tog_parent_tree *parent;
5164 /* user selected '..' */
5165 if (s->tree == s->root)
5167 parent = TAILQ_FIRST(&s->parents);
5168 TAILQ_REMOVE(&s->parents, parent,
5170 got_object_tree_close(s->tree);
5171 s->tree = parent->tree;
5172 s->first_displayed_entry =
5173 parent->first_displayed_entry;
5175 parent->selected_entry;
5176 s->selected = parent->selected;
5178 } else if (S_ISDIR(got_tree_entry_get_mode(
5179 s->selected_entry))) {
5180 struct got_tree_object *subtree;
5181 err = got_object_open_as_tree(&subtree, s->repo,
5182 got_tree_entry_get_id(s->selected_entry));
5185 err = tree_view_visit_subtree(subtree, s);
5187 got_object_tree_close(subtree);
5190 } else if (S_ISREG(got_tree_entry_get_mode(
5191 s->selected_entry))) {
5192 struct tog_view *blame_view;
5193 int begin_x = view_is_parent_view(view) ?
5194 view_split_begin_x(view->begin_x) : 0;
5196 err = blame_tree_entry(&blame_view, begin_x,
5197 s->selected_entry, &s->parents,
5198 s->commit_id, s->refs, s->repo);
5201 if (view_is_parent_view(view)) {
5202 err = view_close_child(view);
5205 err = view_set_child(view, blame_view);
5207 view_close(blame_view);
5210 *focus_view = blame_view;
5211 view->child_focussed = 1;
5213 *new_view = blame_view;
5217 if (s->selected > view->nlines)
5218 s->selected = s->ndisplayed - 1;
5231 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5236 static const struct got_error *
5237 cmd_tree(int argc, char *argv[])
5239 const struct got_error *error;
5240 struct got_repository *repo = NULL;
5241 struct got_worktree *worktree = NULL;
5242 struct got_reflist_head refs;
5243 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5244 struct got_object_id *commit_id = NULL;
5245 char *commit_id_arg = NULL;
5246 struct got_commit_object *commit = NULL;
5247 struct got_tree_object *tree = NULL;
5249 struct tog_view *view;
5251 SIMPLEQ_INIT(&refs);
5254 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5259 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5262 commit_id_arg = optarg;
5265 repo_path = realpath(optarg, NULL);
5266 if (repo_path == NULL)
5267 return got_error_from_errno2("realpath",
5282 cwd = getcwd(NULL, 0);
5284 return got_error_from_errno("getcwd");
5286 error = got_worktree_open(&worktree, cwd);
5287 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5290 if (repo_path == NULL) {
5293 strdup(got_worktree_get_repo_path(worktree));
5295 repo_path = strdup(cwd);
5297 if (repo_path == NULL) {
5298 error = got_error_from_errno("strdup");
5302 error = got_repo_open(&repo, repo_path, NULL);
5306 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5313 error = apply_unveil(got_repo_get_path(repo), NULL);
5317 error = got_repo_match_object_id(&commit_id, NULL,
5318 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5319 GOT_OBJ_TYPE_COMMIT, 1, repo);
5323 error = got_object_open_as_commit(&commit, repo, commit_id);
5327 error = got_object_open_as_tree(&tree, repo,
5328 got_object_commit_get_tree_id(commit));
5332 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5336 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5338 error = got_error_from_errno("view_open");
5341 error = open_tree_view(view, tree, commit_id, &refs, repo);
5344 if (!got_path_is_root_dir(in_repo_path)) {
5345 error = tree_view_walk_path(&view->state.tree, commit_id,
5346 in_repo_path, repo);
5352 /* Release work tree lock. */
5353 got_worktree_close(worktree);
5356 error = view_loop(view);
5362 got_object_commit_close(commit);
5364 got_object_tree_close(tree);
5366 got_repo_close(repo);
5367 got_ref_list_free(&refs);
5376 fprintf(stderr, "commands:");
5377 for (i = 0; i < nitems(tog_commands); i++) {
5378 struct tog_cmd *cmd = &tog_commands[i];
5379 fprintf(stderr, " %s", cmd->name);
5381 fputc('\n', stderr);
5387 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5388 "[arg ...]\n", getprogname());
5390 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5397 make_argv(int argc, ...)
5405 argv = calloc(argc, sizeof(char *));
5408 for (i = 0; i < argc; i++) {
5409 argv[i] = strdup(va_arg(ap, char *));
5410 if (argv[i] == NULL)
5419 * Try to convert 'tog path' into a 'tog log path' command.
5420 * The user could simply have mistyped the command rather than knowingly
5421 * provided a path. So check whether argv[0] can in fact be resolved
5422 * to a path in the HEAD commit and print a special error if not.
5423 * This hack is for mpi@ <3
5425 static const struct got_error *
5426 tog_log_with_path(int argc, char *argv[])
5428 const struct got_error *error = NULL;
5429 struct tog_cmd *cmd = NULL;
5430 struct got_repository *repo = NULL;
5431 struct got_worktree *worktree = NULL;
5432 struct got_object_id *commit_id = NULL, *id = NULL;
5433 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5434 char *commit_id_str = NULL, **cmd_argv = NULL;
5436 cwd = getcwd(NULL, 0);
5438 return got_error_from_errno("getcwd");
5440 error = got_worktree_open(&worktree, cwd);
5441 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5445 repo_path = strdup(got_worktree_get_repo_path(worktree));
5447 repo_path = strdup(cwd);
5448 if (repo_path == NULL) {
5449 error = got_error_from_errno("strdup");
5453 error = got_repo_open(&repo, repo_path, NULL);
5457 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5462 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5463 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5464 GOT_OBJ_TYPE_COMMIT, 1, repo);
5469 got_worktree_close(worktree);
5473 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5475 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5477 fprintf(stderr, "%s: '%s' is no known command or path\n",
5478 getprogname(), argv[0]);
5483 got_repo_close(repo);
5486 error = got_object_id_str(&commit_id_str, commit_id);
5490 cmd = &tog_commands[0]; /* log */
5492 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5493 error = cmd->cmd_main(argc, cmd_argv);
5496 got_repo_close(repo);
5498 got_worktree_close(worktree);
5500 free(commit_id_str);
5507 for (i = 0; i < argc; i++)
5515 main(int argc, char *argv[])
5517 const struct got_error *error = NULL;
5518 struct tog_cmd *cmd = NULL;
5519 int ch, hflag = 0, Vflag = 0;
5520 char **cmd_argv = NULL;
5521 static struct option longopts[] = {
5522 { "version", no_argument, NULL, 'V' },
5526 setlocale(LC_CTYPE, "");
5528 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5548 got_version_print_str();
5555 /* Build an argument vector which runs a default command. */
5556 cmd = &tog_commands[0];
5558 cmd_argv = make_argv(argc, cmd->name);
5562 /* Did the user specify a command? */
5563 for (i = 0; i < nitems(tog_commands); i++) {
5564 if (strncmp(tog_commands[i].name, argv[0],
5565 strlen(argv[0])) == 0) {
5566 cmd = &tog_commands[i];
5575 /* No command specified; try log with a path */
5576 error = tog_log_with_path(argc, argv);
5581 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5587 for (i = 0; i < argc; i++)
5592 if (error && error->code != GOT_ERR_CANCELLED)
5593 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);