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 && !view->search_next_done) ?
1516 "searching..." : "loading...") == -1) {
1517 err = got_error_from_errno("asprintf");
1521 const char *search_str = NULL;
1523 if (view->searching) {
1524 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1525 search_str = "no more matches";
1526 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1527 search_str = "no matches found";
1528 else if (!view->search_next_done)
1529 search_str = "searching...";
1532 if (asprintf(&ncommits_str, " [%d/%d] %s",
1533 entry ? entry->idx + 1 : 0, commits->ncommits,
1534 search_str ? search_str :
1535 (refs_str ? refs_str : "")) == -1) {
1536 err = got_error_from_errno("asprintf");
1541 if (path && strcmp(path, "/") != 0) {
1542 if (asprintf(&header, "commit %s %s%s",
1543 id_str ? id_str : "........................................",
1544 path, ncommits_str) == -1) {
1545 err = got_error_from_errno("asprintf");
1549 } else if (asprintf(&header, "commit %s%s",
1550 id_str ? id_str : "........................................",
1551 ncommits_str) == -1) {
1552 err = got_error_from_errno("asprintf");
1556 err = format_line(&wline, &width, header, view->ncols, 0);
1560 werase(view->window);
1562 if (view_needs_focus_indication(view))
1563 wstandout(view->window);
1564 tc = get_color(colors, TOG_COLOR_COMMIT);
1566 wattr_on(view->window,
1567 COLOR_PAIR(tc->colorpair), NULL);
1568 waddwstr(view->window, wline);
1570 wattr_off(view->window,
1571 COLOR_PAIR(tc->colorpair), NULL);
1572 while (width < view->ncols) {
1573 waddch(view->window, ' ');
1576 if (view_needs_focus_indication(view))
1577 wstandend(view->window);
1582 /* Grow author column size if necessary. */
1589 if (ncommits >= limit - 1)
1591 author = strdup(got_object_commit_get_author(entry->commit));
1592 if (author == NULL) {
1593 err = got_error_from_errno("strdup");
1596 err = format_author(&wauthor, &width, author, COLS,
1598 if (author_cols < width)
1599 author_cols = width;
1603 entry = TAILQ_NEXT(entry, entry);
1610 if (ncommits >= limit - 1)
1612 if (ncommits == selected_idx)
1613 wstandout(view->window);
1614 err = draw_commit(view, entry->commit, entry->id, refs,
1615 date_display_cols, author_cols, colors);
1616 if (ncommits == selected_idx)
1617 wstandend(view->window);
1622 entry = TAILQ_NEXT(entry, entry);
1635 scroll_up(struct tog_view *view,
1636 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1637 struct commit_queue *commits)
1639 struct commit_queue_entry *entry;
1642 entry = TAILQ_FIRST(&commits->head);
1643 if (*first_displayed_entry == entry)
1646 entry = *first_displayed_entry;
1647 while (entry && nscrolled < maxscroll) {
1648 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1650 *first_displayed_entry = entry;
1656 static const struct got_error *
1657 trigger_log_thread(struct tog_view *log_view, int wait,
1658 int *commits_needed, int *log_complete,
1659 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1663 halfdelay(1); /* fast refresh while loading commits */
1665 while (*commits_needed > 0) {
1669 /* Wake the log thread. */
1670 errcode = pthread_cond_signal(need_commits);
1672 return got_error_set_errno(errcode,
1673 "pthread_cond_signal");
1676 * The mutex will be released while the view loop waits
1677 * in wgetch(), at which time the log thread will run.
1682 /* Display progress update in log view. */
1683 show_log_view(log_view);
1687 /* Wait right here while next commit is being loaded. */
1688 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1690 return got_error_set_errno(errcode,
1691 "pthread_cond_wait");
1693 /* Display progress update in log view. */
1694 show_log_view(log_view);
1702 static const struct got_error *
1703 scroll_down(struct tog_view *log_view,
1704 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1705 struct commit_queue_entry **last_displayed_entry,
1706 struct commit_queue *commits, int *log_complete, int *commits_needed,
1707 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1709 const struct got_error *err = NULL;
1710 struct commit_queue_entry *pentry;
1711 int nscrolled = 0, ncommits_needed;
1713 if (*last_displayed_entry == NULL)
1716 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1717 if (commits->ncommits < ncommits_needed && !*log_complete) {
1719 * Ask the log thread for required amount of commits.
1721 (*commits_needed) += maxscroll;
1722 err = trigger_log_thread(log_view, 1, commits_needed,
1723 log_complete, need_commits, commit_loaded);
1729 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1733 *last_displayed_entry = pentry;
1735 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1738 *first_displayed_entry = pentry;
1739 } while (++nscrolled < maxscroll);
1744 static const struct got_error *
1745 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1746 struct got_commit_object *commit, struct got_object_id *commit_id,
1747 struct tog_view *log_view, struct got_reflist_head *refs,
1748 struct got_repository *repo)
1750 const struct got_error *err;
1751 struct got_object_qid *parent_id;
1752 struct tog_view *diff_view;
1754 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1755 if (diff_view == NULL)
1756 return got_error_from_errno("view_open");
1758 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1759 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1760 commit_id, log_view, refs, repo);
1762 *new_view = diff_view;
1766 static const struct got_error *
1767 tree_view_visit_subtree(struct got_tree_object *subtree,
1768 struct tog_tree_view_state *s)
1770 struct tog_parent_tree *parent;
1772 parent = calloc(1, sizeof(*parent));
1774 return got_error_from_errno("calloc");
1776 parent->tree = s->tree;
1777 parent->first_displayed_entry = s->first_displayed_entry;
1778 parent->selected_entry = s->selected_entry;
1779 parent->selected = s->selected;
1780 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1783 s->first_displayed_entry = NULL;
1787 static const struct got_error *
1788 tree_view_walk_path(struct tog_tree_view_state *s,
1789 struct got_object_id *commit_id,
1790 const char *path, struct got_repository *repo)
1792 const struct got_error *err = NULL;
1793 struct got_tree_object *tree = NULL;
1795 char *slash, *subpath = NULL;
1797 /* Walk the path and open corresponding tree objects. */
1800 struct got_tree_entry *te;
1801 struct got_object_id *tree_id;
1807 /* Ensure the correct subtree entry is selected. */
1808 slash = strchr(p, '/');
1810 te_name = strdup(p);
1812 te_name = strndup(p, slash - p);
1813 if (te_name == NULL) {
1814 err = got_error_from_errno("strndup");
1817 te = got_object_tree_find_entry(s->tree, te_name);
1819 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1824 s->selected_entry = te;
1825 s->selected = got_tree_entry_get_index(te);
1826 if (s->tree != s->root)
1827 s->selected++; /* skip '..' */
1829 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1830 /* Jump to this file's entry. */
1831 s->first_displayed_entry = s->selected_entry;
1836 slash = strchr(p, '/');
1838 subpath = strndup(path, slash - path);
1840 subpath = strdup(path);
1841 if (subpath == NULL) {
1842 err = got_error_from_errno("strdup");
1846 err = got_object_id_by_path(&tree_id, repo, commit_id,
1851 err = got_object_open_as_tree(&tree, repo, tree_id);
1856 err = tree_view_visit_subtree(tree, s);
1858 got_object_tree_close(tree);
1872 static const struct got_error *
1873 browse_commit_tree(struct tog_view **new_view, int begin_x,
1874 struct commit_queue_entry *entry, const char *path,
1875 struct got_reflist_head *refs, struct got_repository *repo)
1877 const struct got_error *err = NULL;
1878 struct got_tree_object *tree;
1879 struct tog_tree_view_state *s;
1880 struct tog_view *tree_view;
1882 err = got_object_open_as_tree(&tree, repo,
1883 got_object_commit_get_tree_id(entry->commit));
1887 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1888 if (tree_view == NULL)
1889 return got_error_from_errno("view_open");
1891 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1893 got_object_tree_close(tree);
1896 s = &tree_view->state.tree;
1898 *new_view = tree_view;
1900 if (got_path_is_root_dir(path))
1903 return tree_view_walk_path(s, entry->id, path, repo);
1906 static const struct got_error *
1907 block_signals_used_by_main_thread(void)
1912 if (sigemptyset(&sigset) == -1)
1913 return got_error_from_errno("sigemptyset");
1915 /* tog handles SIGWINCH and SIGCONT */
1916 if (sigaddset(&sigset, SIGWINCH) == -1)
1917 return got_error_from_errno("sigaddset");
1918 if (sigaddset(&sigset, SIGCONT) == -1)
1919 return got_error_from_errno("sigaddset");
1921 /* ncurses handles SIGTSTP */
1922 if (sigaddset(&sigset, SIGTSTP) == -1)
1923 return got_error_from_errno("sigaddset");
1925 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1927 return got_error_set_errno(errcode, "pthread_sigmask");
1933 log_thread(void *arg)
1935 const struct got_error *err = NULL;
1937 struct tog_log_thread_args *a = arg;
1940 err = block_signals_used_by_main_thread();
1944 while (!done && !err && !tog_sigpipe_received) {
1945 err = queue_commits(a->graph, a->commits, 1, a->repo,
1946 a->in_repo_path, a->searching, a->search_next_done,
1949 if (err->code != GOT_ERR_ITER_COMPLETED)
1953 } else if (a->commits_needed > 0)
1954 a->commits_needed--;
1956 errcode = pthread_mutex_lock(&tog_mutex);
1958 err = got_error_set_errno(errcode,
1959 "pthread_mutex_lock");
1961 } else if (*a->quit)
1963 else if (*a->first_displayed_entry == NULL) {
1964 *a->first_displayed_entry =
1965 TAILQ_FIRST(&a->commits->head);
1966 *a->selected_entry = *a->first_displayed_entry;
1969 errcode = pthread_cond_signal(&a->commit_loaded);
1971 err = got_error_set_errno(errcode,
1972 "pthread_cond_signal");
1973 pthread_mutex_unlock(&tog_mutex);
1978 a->commits_needed = 0;
1980 if (a->commits_needed == 0) {
1981 errcode = pthread_cond_wait(&a->need_commits,
1984 err = got_error_set_errno(errcode,
1985 "pthread_cond_wait");
1989 errcode = pthread_mutex_unlock(&tog_mutex);
1990 if (errcode && err == NULL)
1991 err = got_error_set_errno(errcode,
1992 "pthread_mutex_unlock");
1994 a->log_complete = 1;
1998 static const struct got_error *
1999 stop_log_thread(struct tog_log_view_state *s)
2001 const struct got_error *err = NULL;
2006 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2008 return got_error_set_errno(errcode,
2009 "pthread_cond_signal");
2010 errcode = pthread_mutex_unlock(&tog_mutex);
2012 return got_error_set_errno(errcode,
2013 "pthread_mutex_unlock");
2014 errcode = pthread_join(s->thread, (void **)&err);
2016 return got_error_set_errno(errcode, "pthread_join");
2017 errcode = pthread_mutex_lock(&tog_mutex);
2019 return got_error_set_errno(errcode,
2020 "pthread_mutex_lock");
2024 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2025 if (errcode && err == NULL)
2026 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2028 if (s->thread_args.repo) {
2029 got_repo_close(s->thread_args.repo);
2030 s->thread_args.repo = NULL;
2033 if (s->thread_args.graph) {
2034 got_commit_graph_close(s->thread_args.graph);
2035 s->thread_args.graph = NULL;
2041 static const struct got_error *
2042 close_log_view(struct tog_view *view)
2044 const struct got_error *err = NULL;
2045 struct tog_log_view_state *s = &view->state.log;
2047 err = stop_log_thread(s);
2048 free_commits(&s->commits);
2049 free(s->in_repo_path);
2050 s->in_repo_path = NULL;
2056 static const struct got_error *
2057 search_start_log_view(struct tog_view *view)
2059 struct tog_log_view_state *s = &view->state.log;
2061 s->matched_entry = NULL;
2062 s->search_entry = NULL;
2066 static const struct got_error *
2067 search_next_log_view(struct tog_view *view)
2069 const struct got_error *err = NULL;
2070 struct tog_log_view_state *s = &view->state.log;
2071 struct commit_queue_entry *entry;
2073 /* Display progress update in log view. */
2074 show_log_view(view);
2078 if (s->search_entry) {
2080 errcode = pthread_mutex_unlock(&tog_mutex);
2082 return got_error_set_errno(errcode,
2083 "pthread_mutex_unlock");
2084 ch = wgetch(view->window);
2085 errcode = pthread_mutex_lock(&tog_mutex);
2087 return got_error_set_errno(errcode,
2088 "pthread_mutex_lock");
2089 if (ch == KEY_BACKSPACE) {
2090 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2093 if (view->searching == TOG_SEARCH_FORWARD)
2094 entry = TAILQ_NEXT(s->search_entry, entry);
2096 entry = TAILQ_PREV(s->search_entry,
2097 commit_queue_head, entry);
2098 } else if (s->matched_entry) {
2099 if (view->searching == TOG_SEARCH_FORWARD)
2100 entry = TAILQ_NEXT(s->matched_entry, entry);
2102 entry = TAILQ_PREV(s->matched_entry,
2103 commit_queue_head, entry);
2105 if (view->searching == TOG_SEARCH_FORWARD)
2106 entry = TAILQ_FIRST(&s->commits.head);
2108 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2114 if (entry == NULL) {
2115 if (s->thread_args.log_complete ||
2116 view->searching == TOG_SEARCH_BACKWARD) {
2117 view->search_next_done =
2118 (s->matched_entry == NULL ?
2119 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2120 s->search_entry = NULL;
2124 * Poke the log thread for more commits and return,
2125 * allowing the main loop to make progress. Search
2126 * will resume at s->search_entry once we come back.
2128 s->thread_args.commits_needed++;
2129 return trigger_log_thread(view, 0,
2130 &s->thread_args.commits_needed,
2131 &s->thread_args.log_complete,
2132 &s->thread_args.need_commits,
2133 &s->thread_args.commit_loaded);
2136 err = match_commit(&have_match, entry->id, entry->commit,
2141 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2142 s->matched_entry = entry;
2146 s->search_entry = entry;
2147 if (view->searching == TOG_SEARCH_FORWARD)
2148 entry = TAILQ_NEXT(entry, entry);
2150 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2153 if (s->matched_entry) {
2154 int cur = s->selected_entry->idx;
2155 while (cur < s->matched_entry->idx) {
2156 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2161 while (cur > s->matched_entry->idx) {
2162 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2169 s->search_entry = NULL;
2174 static const struct got_error *
2175 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2176 struct got_reflist_head *refs, struct got_repository *repo,
2177 const char *head_ref_name, const char *in_repo_path,
2180 const struct got_error *err = NULL;
2181 struct tog_log_view_state *s = &view->state.log;
2182 struct got_repository *thread_repo = NULL;
2183 struct got_commit_graph *thread_graph = NULL;
2186 if (in_repo_path != s->in_repo_path) {
2187 free(s->in_repo_path);
2188 s->in_repo_path = strdup(in_repo_path);
2189 if (s->in_repo_path == NULL)
2190 return got_error_from_errno("strdup");
2193 /* The commit queue only contains commits being displayed. */
2194 TAILQ_INIT(&s->commits.head);
2195 s->commits.ncommits = 0;
2199 s->head_ref_name = head_ref_name;
2200 s->start_id = got_object_id_dup(start_id);
2201 if (s->start_id == NULL) {
2202 err = got_error_from_errno("got_object_id_dup");
2205 s->log_branches = log_branches;
2207 SIMPLEQ_INIT(&s->colors);
2208 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2209 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2210 get_color_value("TOG_COLOR_COMMIT"));
2213 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2214 get_color_value("TOG_COLOR_AUTHOR"));
2216 free_colors(&s->colors);
2219 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2220 get_color_value("TOG_COLOR_DATE"));
2222 free_colors(&s->colors);
2227 view->show = show_log_view;
2228 view->input = input_log_view;
2229 view->close = close_log_view;
2230 view->search_start = search_start_log_view;
2231 view->search_next = search_next_log_view;
2233 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2236 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2240 err = got_commit_graph_iter_start(thread_graph,
2241 s->start_id, s->repo, NULL, NULL);
2245 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2247 err = got_error_set_errno(errcode, "pthread_cond_init");
2250 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2252 err = got_error_set_errno(errcode, "pthread_cond_init");
2256 s->thread_args.commits_needed = view->nlines;
2257 s->thread_args.graph = thread_graph;
2258 s->thread_args.commits = &s->commits;
2259 s->thread_args.in_repo_path = s->in_repo_path;
2260 s->thread_args.start_id = s->start_id;
2261 s->thread_args.repo = thread_repo;
2262 s->thread_args.log_complete = 0;
2263 s->thread_args.quit = &s->quit;
2264 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2265 s->thread_args.selected_entry = &s->selected_entry;
2266 s->thread_args.searching = &view->searching;
2267 s->thread_args.search_next_done = &view->search_next_done;
2268 s->thread_args.regex = &view->regex;
2271 close_log_view(view);
2275 static const struct got_error *
2276 show_log_view(struct tog_view *view)
2278 struct tog_log_view_state *s = &view->state.log;
2280 if (s->thread == NULL) {
2281 int errcode = pthread_create(&s->thread, NULL, log_thread,
2284 return got_error_set_errno(errcode, "pthread_create");
2287 return draw_commits(view, &s->last_displayed_entry,
2288 &s->selected_entry, s->first_displayed_entry,
2289 &s->commits, s->selected, view->nlines, s->refs,
2290 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2293 static const struct got_error *
2294 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2295 struct tog_view **focus_view, struct tog_view *view, int ch)
2297 const struct got_error *err = NULL;
2298 struct tog_log_view_state *s = &view->state.log;
2299 char *parent_path, *in_repo_path = NULL;
2300 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2302 struct got_object_id *start_id;
2312 if (s->first_displayed_entry == NULL)
2314 if (s->selected > 0)
2317 scroll_up(view, &s->first_displayed_entry, 1,
2322 if (s->first_displayed_entry == NULL)
2324 if (TAILQ_FIRST(&s->commits.head) ==
2325 s->first_displayed_entry) {
2329 scroll_up(view, &s->first_displayed_entry,
2330 view->nlines - 1, &s->commits);
2336 if (s->first_displayed_entry == NULL)
2338 if (s->selected < MIN(view->nlines - 2,
2339 s->commits.ncommits - 1)) {
2343 err = scroll_down(view, &s->first_displayed_entry, 1,
2344 &s->last_displayed_entry, &s->commits,
2345 &s->thread_args.log_complete,
2346 &s->thread_args.commits_needed,
2347 &s->thread_args.need_commits,
2348 &s->thread_args.commit_loaded);
2352 struct commit_queue_entry *first;
2353 first = s->first_displayed_entry;
2356 err = scroll_down(view, &s->first_displayed_entry,
2357 view->nlines - 1, &s->last_displayed_entry,
2358 &s->commits, &s->thread_args.log_complete,
2359 &s->thread_args.commits_needed,
2360 &s->thread_args.need_commits,
2361 &s->thread_args.commit_loaded);
2364 if (first == s->first_displayed_entry &&
2365 s->selected < MIN(view->nlines - 2,
2366 s->commits.ncommits - 1)) {
2367 /* can't scroll further down */
2368 s->selected = MIN(view->nlines - 2,
2369 s->commits.ncommits - 1);
2375 if (s->selected > view->nlines - 2)
2376 s->selected = view->nlines - 2;
2377 if (s->selected > s->commits.ncommits - 1)
2378 s->selected = s->commits.ncommits - 1;
2383 if (s->selected_entry == NULL)
2385 if (view_is_parent_view(view))
2386 begin_x = view_split_begin_x(view->begin_x);
2387 err = open_diff_view_for_commit(&diff_view, begin_x,
2388 s->selected_entry->commit, s->selected_entry->id,
2389 view, s->refs, s->repo);
2392 if (view_is_parent_view(view)) {
2393 err = view_close_child(view);
2396 err = view_set_child(view, diff_view);
2398 view_close(diff_view);
2401 *focus_view = diff_view;
2402 view->child_focussed = 1;
2404 *new_view = diff_view;
2407 if (s->selected_entry == NULL)
2409 if (view_is_parent_view(view))
2410 begin_x = view_split_begin_x(view->begin_x);
2411 err = browse_commit_tree(&tree_view, begin_x,
2412 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2415 if (view_is_parent_view(view)) {
2416 err = view_close_child(view);
2419 err = view_set_child(view, tree_view);
2421 view_close(tree_view);
2424 *focus_view = tree_view;
2425 view->child_focussed = 1;
2427 *new_view = tree_view;
2430 if (strcmp(s->in_repo_path, "/") == 0)
2432 parent_path = dirname(s->in_repo_path);
2433 if (parent_path && strcmp(parent_path, ".") != 0) {
2434 err = stop_log_thread(s);
2437 lv = view_open(view->nlines, view->ncols,
2438 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2440 return got_error_from_errno(
2442 err = open_log_view(lv, s->start_id, s->refs,
2443 s->repo, s->head_ref_name, parent_path,
2447 if (view_is_parent_view(view))
2450 view_set_child(view->parent, lv);
2457 err = stop_log_thread(s);
2460 lv = view_open(view->nlines, view->ncols,
2461 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2463 return got_error_from_errno("view_open");
2464 err = got_repo_match_object_id(&start_id, NULL,
2465 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2466 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2471 in_repo_path = strdup(s->in_repo_path);
2472 if (in_repo_path == NULL) {
2475 return got_error_from_errno("strdup");
2477 got_ref_list_free(s->refs);
2478 err = got_ref_list(s->refs, s->repo, NULL,
2479 got_ref_cmp_by_name, NULL);
2485 err = open_log_view(lv, start_id, s->refs, s->repo,
2486 s->head_ref_name, in_repo_path, s->log_branches);
2496 s->log_branches = !s->log_branches;
2497 err = stop_log_thread(s);
2500 lv = view_open(view->nlines, view->ncols,
2501 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2503 return got_error_from_errno("view_open");
2504 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2505 s->head_ref_name, s->in_repo_path, s->log_branches);
2520 static const struct got_error *
2521 apply_unveil(const char *repo_path, const char *worktree_path)
2523 const struct got_error *error;
2526 if (unveil("gmon.out", "rwc") != 0)
2527 return got_error_from_errno2("unveil", "gmon.out");
2529 if (repo_path && unveil(repo_path, "r") != 0)
2530 return got_error_from_errno2("unveil", repo_path);
2532 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2533 return got_error_from_errno2("unveil", worktree_path);
2535 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2536 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2538 error = got_privsep_unveil_exec_helpers();
2542 if (unveil(NULL, NULL) != 0)
2543 return got_error_from_errno("unveil");
2553 halfdelay(1); /* Do fast refresh while initial view is loading. */
2556 intrflush(stdscr, FALSE);
2557 keypad(stdscr, TRUE);
2559 if (getenv("TOG_COLORS") != NULL) {
2561 use_default_colors();
2563 signal(SIGWINCH, tog_sigwinch);
2564 signal(SIGPIPE, tog_sigpipe);
2565 signal(SIGCONT, tog_sigcont);
2568 static const struct got_error *
2569 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2570 struct got_repository *repo, struct got_worktree *worktree)
2572 const struct got_error *err = NULL;
2575 *in_repo_path = strdup("/");
2576 if (*in_repo_path == NULL)
2577 return got_error_from_errno("strdup");
2582 const char *prefix = got_worktree_get_path_prefix(worktree);
2585 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2589 if (asprintf(&p, "%s%s%s", prefix,
2590 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2591 err = got_error_from_errno("asprintf");
2595 err = got_repo_map_path(in_repo_path, repo, p, 0);
2599 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2604 static const struct got_error *
2605 cmd_log(int argc, char *argv[])
2607 const struct got_error *error;
2608 struct got_repository *repo = NULL;
2609 struct got_worktree *worktree = NULL;
2610 struct got_reflist_head refs;
2611 struct got_object_id *start_id = NULL;
2612 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2613 char *start_commit = NULL, *head_ref_name = NULL;
2614 int ch, log_branches = 0;
2615 struct tog_view *view;
2617 SIMPLEQ_INIT(&refs);
2620 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2625 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2631 start_commit = optarg;
2634 repo_path = realpath(optarg, NULL);
2635 if (repo_path == NULL)
2636 return got_error_from_errno2("realpath",
2651 cwd = getcwd(NULL, 0);
2653 return got_error_from_errno("getcwd");
2655 error = got_worktree_open(&worktree, cwd);
2656 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2659 if (repo_path == NULL) {
2662 strdup(got_worktree_get_repo_path(worktree));
2664 repo_path = strdup(cwd);
2666 if (repo_path == NULL) {
2667 error = got_error_from_errno("strdup");
2671 error = got_repo_open(&repo, repo_path, NULL);
2675 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2682 error = apply_unveil(got_repo_get_path(repo),
2683 worktree ? got_worktree_get_root_path(worktree) : NULL);
2687 if (start_commit == NULL)
2688 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2689 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2690 GOT_OBJ_TYPE_COMMIT, 1, repo);
2692 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2693 GOT_OBJ_TYPE_COMMIT, 1, repo);
2697 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2701 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2703 error = got_error_from_errno("view_open");
2707 head_ref_name = strdup(
2708 got_worktree_get_head_ref_name(worktree));
2709 if (head_ref_name == NULL) {
2710 error = got_error_from_errno("strdup");
2714 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2715 in_repo_path, log_branches);
2719 /* Release work tree lock. */
2720 got_worktree_close(worktree);
2723 error = view_loop(view);
2729 free(head_ref_name);
2731 got_repo_close(repo);
2733 got_worktree_close(worktree);
2734 got_ref_list_free(&refs);
2742 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2748 parse_next_line(FILE *f, size_t *len)
2753 const char delim[3] = { '\0', '\0', '\0'};
2755 line = fparseln(f, &linelen, &lineno, delim, 0);
2762 match_line(const char *line, regex_t *regex)
2764 regmatch_t regmatch;
2766 return regexec(regex, line, 1, ®match, 0) == 0;
2770 match_color(struct tog_colors *colors, const char *line)
2772 struct tog_color *tc = NULL;
2774 SIMPLEQ_FOREACH(tc, colors, entry) {
2775 if (match_line(line, &tc->regex))
2782 static const struct got_error *
2783 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2784 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2785 char *header, struct tog_colors *colors)
2787 const struct got_error *err;
2788 int lineno = 0, nprinted = 0;
2790 struct tog_color *tc;
2796 werase(view->window);
2799 err = format_line(&wline, &width, header, view->ncols, 0);
2804 if (view_needs_focus_indication(view))
2805 wstandout(view->window);
2806 waddwstr(view->window, wline);
2807 if (view_needs_focus_indication(view))
2808 wstandend(view->window);
2809 if (width <= view->ncols - 1)
2810 waddch(view->window, '\n');
2818 while (nprinted < max_lines) {
2819 line = parse_next_line(f, &len);
2824 if (++lineno < *first_displayed_line) {
2829 err = format_line(&wline, &width, line, view->ncols, 0);
2835 tc = match_color(colors, line);
2837 wattr_on(view->window,
2838 COLOR_PAIR(tc->colorpair), NULL);
2839 waddwstr(view->window, wline);
2841 wattr_off(view->window,
2842 COLOR_PAIR(tc->colorpair), NULL);
2843 if (width <= view->ncols - 1)
2844 waddch(view->window, '\n');
2845 if (++nprinted == 1)
2846 *first_displayed_line = lineno;
2851 *last_displayed_line = lineno;
2856 while (nprinted < view->nlines) {
2857 waddch(view->window, '\n');
2861 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2866 wstandout(view->window);
2867 waddwstr(view->window, wline);
2868 wstandend(view->window);
2875 get_datestr(time_t *time, char *datebuf)
2877 struct tm mytm, *tm;
2880 tm = gmtime_r(time, &mytm);
2883 s = asctime_r(tm, datebuf);
2886 p = strchr(s, '\n');
2892 static const struct got_error *
2893 write_commit_info(struct got_object_id *commit_id,
2894 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2896 const struct got_error *err = NULL;
2897 char datebuf[26], *datestr;
2898 struct got_commit_object *commit;
2899 char *id_str = NULL, *logmsg = NULL;
2900 time_t committer_time;
2901 const char *author, *committer;
2902 char *refs_str = NULL;
2905 err = build_refs_str(&refs_str, refs, commit_id, repo);
2910 err = got_object_open_as_commit(&commit, repo, commit_id);
2914 err = got_object_id_str(&id_str, commit_id);
2916 err = got_error_from_errno("got_object_id_str");
2920 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2921 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2922 err = got_error_from_errno("fprintf");
2925 if (fprintf(outfile, "from: %s\n",
2926 got_object_commit_get_author(commit)) < 0) {
2927 err = got_error_from_errno("fprintf");
2930 committer_time = got_object_commit_get_committer_time(commit);
2931 datestr = get_datestr(&committer_time, datebuf);
2932 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2933 err = got_error_from_errno("fprintf");
2936 author = got_object_commit_get_author(commit);
2937 committer = got_object_commit_get_committer(commit);
2938 if (strcmp(author, committer) != 0 &&
2939 fprintf(outfile, "via: %s\n", committer) < 0) {
2940 err = got_error_from_errno("fprintf");
2943 err = got_object_commit_get_logmsg(&logmsg, commit);
2946 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2947 err = got_error_from_errno("fprintf");
2954 got_object_commit_close(commit);
2958 const struct got_error *
2959 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2965 size_t noffsets = 0;
2969 *line_offsets = NULL;
2975 if (fseek(infile, 0, SEEK_END) == -1)
2976 return got_error_from_errno("fseek");
2977 len = ftell(infile) + 1;
2979 return got_error_from_errno("ftell");
2980 if (fseek(infile, 0, SEEK_SET) == -1)
2981 return got_error_from_errno("fseek");
2985 if ((buf = calloc(len, sizeof(char *))) == NULL)
2986 return got_error_from_errno("calloc");
2988 fread(buf, 1, len, infile);
2990 return got_error_from_errno("fread");
2993 if (line_offsets && nlines) {
2994 if (*line_offsets == NULL) {
2995 /* Have some data but perhaps no '\n'. */
2998 *line_offsets = calloc(1, sizeof(**line_offsets));
2999 if (*line_offsets == NULL)
3000 return got_error_from_errno("calloc");
3001 /* Skip forward over end of first line. */
3008 /* Scan '\n' offsets in remaining chunk of data. */
3010 if (buf[i] != '\n') {
3015 if (noffsets < *nlines) {
3016 off_t *o = recallocarray(*line_offsets,
3018 sizeof(**line_offsets));
3020 free(*line_offsets);
3021 *line_offsets = NULL;
3022 return got_error_from_errno(
3029 (*line_offsets)[*nlines - 1] = off;
3034 if (fflush(infile) != 0)
3035 return got_error_from_errno("fflush");
3044 static const struct got_error *
3045 create_diff(struct tog_diff_view_state *s)
3047 const struct got_error *err = NULL;
3053 err = got_error_from_errno("got_opentemp");
3056 if (s->f && fclose(s->f) != 0) {
3057 err = got_error_from_errno("fclose");
3063 err = got_object_get_type(&obj_type, s->repo, s->id1);
3065 err = got_object_get_type(&obj_type, s->repo, s->id2);
3070 case GOT_OBJ_TYPE_BLOB:
3071 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3072 s->diff_context, 0, s->repo, s->f);
3074 case GOT_OBJ_TYPE_TREE:
3075 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3076 s->diff_context, 0, s->repo, s->f);
3078 case GOT_OBJ_TYPE_COMMIT: {
3079 const struct got_object_id_queue *parent_ids;
3080 struct got_object_qid *pid;
3081 struct got_commit_object *commit2;
3083 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3086 /* Show commit info if we're diffing to a parent/root commit. */
3087 if (s->id1 == NULL) {
3088 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3092 parent_ids = got_object_commit_get_parent_ids(commit2);
3093 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3094 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3095 err = write_commit_info(s->id2, s->refs,
3103 got_object_commit_close(commit2);
3105 err = got_diff_objects_as_commits(s->id1, s->id2,
3106 s->diff_context, 0, s->repo, s->f);
3110 err = got_error(GOT_ERR_OBJ_TYPE);
3115 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3118 if (s->f && fflush(s->f) != 0 && err == NULL)
3119 err = got_error_from_errno("fflush");
3124 diff_view_indicate_progress(struct tog_view *view)
3126 mvwaddstr(view->window, 0, 0, "diffing...");
3131 static const struct got_error *
3132 search_start_diff_view(struct tog_view *view)
3134 struct tog_diff_view_state *s = &view->state.diff;
3136 s->matched_line = 0;
3140 static const struct got_error *
3141 search_next_diff_view(struct tog_view *view)
3143 struct tog_diff_view_state *s = &view->state.diff;
3146 if (!view->searching) {
3147 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3151 if (s->matched_line) {
3152 if (view->searching == TOG_SEARCH_FORWARD)
3153 lineno = s->matched_line + 1;
3155 lineno = s->matched_line - 1;
3157 if (view->searching == TOG_SEARCH_FORWARD)
3168 if (lineno <= 0 || lineno > s->nlines) {
3169 if (s->matched_line == 0) {
3170 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3175 if (view->searching == TOG_SEARCH_FORWARD)
3181 offset = s->line_offsets[lineno - 1];
3182 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3184 return got_error_from_errno("fseeko");
3187 line = parse_next_line(s->f, &len);
3188 if (line && match_line(line, &view->regex)) {
3189 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3190 s->matched_line = lineno;
3195 if (view->searching == TOG_SEARCH_FORWARD)
3201 if (s->matched_line) {
3202 s->first_displayed_line = s->matched_line;
3203 s->selected_line = 1;
3209 static const struct got_error *
3210 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3211 struct got_object_id *id2, struct tog_view *log_view,
3212 struct got_reflist_head *refs, struct got_repository *repo)
3214 const struct got_error *err;
3215 struct tog_diff_view_state *s = &view->state.diff;
3217 if (id1 != NULL && id2 != NULL) {
3219 err = got_object_get_type(&type1, repo, id1);
3222 err = got_object_get_type(&type2, repo, id2);
3227 return got_error(GOT_ERR_OBJ_TYPE);
3229 s->first_displayed_line = 1;
3230 s->last_displayed_line = view->nlines;
3231 s->selected_line = 1;
3238 s->id1 = got_object_id_dup(id1);
3240 return got_error_from_errno("got_object_id_dup");
3244 s->id2 = got_object_id_dup(id2);
3245 if (s->id2 == NULL) {
3248 return got_error_from_errno("got_object_id_dup");
3251 s->first_displayed_line = 1;
3252 s->last_displayed_line = view->nlines;
3253 s->diff_context = 3;
3254 s->log_view = log_view;
3258 SIMPLEQ_INIT(&s->colors);
3259 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3260 err = add_color(&s->colors,
3261 "^-", TOG_COLOR_DIFF_MINUS,
3262 get_color_value("TOG_COLOR_DIFF_MINUS"));
3265 err = add_color(&s->colors, "^\\+",
3266 TOG_COLOR_DIFF_PLUS,
3267 get_color_value("TOG_COLOR_DIFF_PLUS"));
3269 free_colors(&s->colors);
3272 err = add_color(&s->colors,
3273 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3274 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3276 free_colors(&s->colors);
3280 err = add_color(&s->colors,
3281 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3282 get_color_value("TOG_COLOR_DIFF_META"));
3284 free_colors(&s->colors);
3288 err = add_color(&s->colors,
3289 "^(from|via): ", TOG_COLOR_AUTHOR,
3290 get_color_value("TOG_COLOR_AUTHOR"));
3292 free_colors(&s->colors);
3296 err = add_color(&s->colors,
3297 "^date: ", TOG_COLOR_DATE,
3298 get_color_value("TOG_COLOR_DATE"));
3300 free_colors(&s->colors);
3305 if (log_view && view_is_splitscreen(view))
3306 show_log_view(log_view); /* draw vborder */
3307 diff_view_indicate_progress(view);
3309 err = create_diff(s);
3318 view->show = show_diff_view;
3319 view->input = input_diff_view;
3320 view->close = close_diff_view;
3321 view->search_start = search_start_diff_view;
3322 view->search_next = search_next_diff_view;
3327 static const struct got_error *
3328 close_diff_view(struct tog_view *view)
3330 const struct got_error *err = NULL;
3331 struct tog_diff_view_state *s = &view->state.diff;
3337 if (s->f && fclose(s->f) == EOF)
3338 err = got_error_from_errno("fclose");
3339 free_colors(&s->colors);
3340 free(s->line_offsets);
3344 static const struct got_error *
3345 show_diff_view(struct tog_view *view)
3347 const struct got_error *err;
3348 struct tog_diff_view_state *s = &view->state.diff;
3349 char *id_str1 = NULL, *id_str2, *header;
3352 err = got_object_id_str(&id_str1, s->id1);
3356 err = got_object_id_str(&id_str2, s->id2);
3360 if (asprintf(&header, "diff %s %s",
3361 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3362 err = got_error_from_errno("asprintf");
3370 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3371 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3372 header, &s->colors);
3375 static const struct got_error *
3376 set_selected_commit(struct tog_diff_view_state *s,
3377 struct commit_queue_entry *entry)
3379 const struct got_error *err;
3380 const struct got_object_id_queue *parent_ids;
3381 struct got_commit_object *selected_commit;
3382 struct got_object_qid *pid;
3385 s->id2 = got_object_id_dup(entry->id);
3387 return got_error_from_errno("got_object_id_dup");
3389 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3392 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3394 pid = SIMPLEQ_FIRST(parent_ids);
3395 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3396 got_object_commit_close(selected_commit);
3400 static const struct got_error *
3401 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3402 struct tog_view **focus_view, struct tog_view *view, int ch)
3404 const struct got_error *err = NULL;
3405 struct tog_diff_view_state *s = &view->state.diff;
3406 struct tog_log_view_state *ls;
3407 struct commit_queue_entry *entry;
3413 if (s->first_displayed_line > 1)
3414 s->first_displayed_line--;
3418 if (s->first_displayed_line == 1)
3421 while (i++ < view->nlines - 1 &&
3422 s->first_displayed_line > 1)
3423 s->first_displayed_line--;
3428 s->first_displayed_line++;
3436 while (!s->eof && i++ < view->nlines - 1) {
3438 line = parse_next_line(s->f, NULL);
3439 s->first_displayed_line++;
3445 if (s->diff_context > 0) {
3447 diff_view_indicate_progress(view);
3448 err = create_diff(s);
3452 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3454 diff_view_indicate_progress(view);
3455 err = create_diff(s);
3460 if (s->log_view == NULL)
3462 ls = &s->log_view->state.log;
3463 entry = TAILQ_PREV(ls->selected_entry,
3464 commit_queue_head, entry);
3468 err = input_log_view(NULL, NULL, NULL, s->log_view,
3473 err = set_selected_commit(s, entry);
3477 s->first_displayed_line = 1;
3478 s->last_displayed_line = view->nlines;
3480 diff_view_indicate_progress(view);
3481 err = create_diff(s);
3485 if (s->log_view == NULL)
3487 ls = &s->log_view->state.log;
3489 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3490 ls->thread_args.commits_needed++;
3491 err = trigger_log_thread(s->log_view, 1,
3492 &ls->thread_args.commits_needed,
3493 &ls->thread_args.log_complete,
3494 &ls->thread_args.need_commits,
3495 &ls->thread_args.commit_loaded);
3499 err = input_log_view(NULL, NULL, NULL, s->log_view,
3504 entry = TAILQ_NEXT(ls->selected_entry, entry);
3508 err = set_selected_commit(s, entry);
3512 s->first_displayed_line = 1;
3513 s->last_displayed_line = view->nlines;
3515 diff_view_indicate_progress(view);
3516 err = create_diff(s);
3525 static const struct got_error *
3526 cmd_diff(int argc, char *argv[])
3528 const struct got_error *error = NULL;
3529 struct got_repository *repo = NULL;
3530 struct got_worktree *worktree = NULL;
3531 struct got_reflist_head refs;
3532 struct got_object_id *id1 = NULL, *id2 = NULL;
3533 char *repo_path = NULL, *cwd = NULL;
3534 char *id_str1 = NULL, *id_str2 = NULL;
3536 struct tog_view *view;
3538 SIMPLEQ_INIT(&refs);
3541 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3546 while ((ch = getopt(argc, argv, "r:")) != -1) {
3549 repo_path = realpath(optarg, NULL);
3550 if (repo_path == NULL)
3551 return got_error_from_errno2("realpath",
3564 usage_diff(); /* TODO show local worktree changes */
3565 } else if (argc == 2) {
3571 cwd = getcwd(NULL, 0);
3573 return got_error_from_errno("getcwd");
3575 error = got_worktree_open(&worktree, cwd);
3576 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3579 if (repo_path == NULL) {
3582 strdup(got_worktree_get_repo_path(worktree));
3584 repo_path = strdup(cwd);
3586 if (repo_path == NULL) {
3587 error = got_error_from_errno("strdup");
3591 error = got_repo_open(&repo, repo_path, NULL);
3597 error = apply_unveil(got_repo_get_path(repo), NULL);
3601 error = got_repo_match_object_id_prefix(&id1, id_str1,
3602 GOT_OBJ_TYPE_ANY, repo);
3606 error = got_repo_match_object_id_prefix(&id2, id_str2,
3607 GOT_OBJ_TYPE_ANY, repo);
3611 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3615 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3617 error = got_error_from_errno("view_open");
3620 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3623 error = view_loop(view);
3628 got_repo_close(repo);
3630 got_worktree_close(worktree);
3631 got_ref_list_free(&refs);
3639 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3644 struct tog_blame_line {
3646 struct got_object_id *id;
3649 static const struct got_error *
3650 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3651 const char *path, struct tog_blame_line *lines, int nlines,
3652 int blame_complete, int selected_line, int *first_displayed_line,
3653 int *last_displayed_line, int *eof, int max_lines,
3654 struct tog_colors *colors)
3656 const struct got_error *err;
3657 int lineno = 0, nprinted = 0;
3662 struct tog_blame_line *blame_line;
3663 struct got_object_id *prev_id = NULL;
3665 struct tog_color *tc;
3667 err = got_object_id_str(&id_str, id);
3672 werase(view->window);
3674 if (asprintf(&line, "commit %s", id_str) == -1) {
3675 err = got_error_from_errno("asprintf");
3680 err = format_line(&wline, &width, line, view->ncols, 0);
3685 if (view_needs_focus_indication(view))
3686 wstandout(view->window);
3687 tc = get_color(colors, TOG_COLOR_COMMIT);
3689 wattr_on(view->window,
3690 COLOR_PAIR(tc->colorpair), NULL);
3691 waddwstr(view->window, wline);
3693 wattr_off(view->window,
3694 COLOR_PAIR(tc->colorpair), NULL);
3695 if (view_needs_focus_indication(view))
3696 wstandend(view->window);
3699 if (width < view->ncols - 1)
3700 waddch(view->window, '\n');
3702 if (asprintf(&line, "[%d/%d] %s%s",
3703 *first_displayed_line - 1 + selected_line, nlines,
3704 blame_complete ? "" : "annotating... ", path) == -1) {
3706 return got_error_from_errno("asprintf");
3709 err = format_line(&wline, &width, line, view->ncols, 0);
3714 waddwstr(view->window, wline);
3717 if (width < view->ncols - 1)
3718 waddch(view->window, '\n');
3721 while (nprinted < max_lines - 2) {
3722 line = parse_next_line(f, &len);
3727 if (++lineno < *first_displayed_line) {
3732 if (view->ncols <= 9) {
3734 wline = wcsdup(L"");
3736 err = got_error_from_errno("wcsdup");
3738 err = format_line(&wline, &width, line,
3739 view->ncols - 9, 9);
3747 if (view->focussed && nprinted == selected_line - 1)
3748 wstandout(view->window);
3751 blame_line = &lines[lineno - 1];
3752 if (blame_line->annotated && prev_id &&
3753 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3755 nprinted == selected_line - 1)) {
3756 waddstr(view->window, " ");
3757 } else if (blame_line->annotated) {
3759 err = got_object_id_str(&id_str, blame_line->id);
3765 tc = get_color(colors, TOG_COLOR_COMMIT);
3767 wattr_on(view->window,
3768 COLOR_PAIR(tc->colorpair), NULL);
3769 wprintw(view->window, "%.8s", id_str);
3771 wattr_off(view->window,
3772 COLOR_PAIR(tc->colorpair), NULL);
3774 prev_id = blame_line->id;
3776 waddstr(view->window, "........");
3780 waddstr(view->window, "........");
3784 if (view->focussed && nprinted == selected_line - 1)
3785 wstandend(view->window);
3786 waddstr(view->window, " ");
3788 waddwstr(view->window, wline);
3789 if (width <= view->ncols - 1)
3790 waddch(view->window, '\n');
3791 if (++nprinted == 1)
3792 *first_displayed_line = lineno;
3797 *last_displayed_line = lineno;
3804 static const struct got_error *
3805 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3807 const struct got_error *err = NULL;
3808 struct tog_blame_cb_args *a = arg;
3809 struct tog_blame_line *line;
3812 if (nlines != a->nlines ||
3813 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3814 return got_error(GOT_ERR_RANGE);
3816 errcode = pthread_mutex_lock(&tog_mutex);
3818 return got_error_set_errno(errcode, "pthread_mutex_lock");
3820 if (*a->quit) { /* user has quit the blame view */
3821 err = got_error(GOT_ERR_ITER_COMPLETED);
3826 goto done; /* no change in this commit */
3828 line = &a->lines[lineno - 1];
3829 if (line->annotated)
3832 line->id = got_object_id_dup(id);
3833 if (line->id == NULL) {
3834 err = got_error_from_errno("got_object_id_dup");
3837 line->annotated = 1;
3839 errcode = pthread_mutex_unlock(&tog_mutex);
3841 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3846 blame_thread(void *arg)
3848 const struct got_error *err;
3849 struct tog_blame_thread_args *ta = arg;
3850 struct tog_blame_cb_args *a = ta->cb_args;
3853 err = block_signals_used_by_main_thread();
3857 err = got_blame(ta->path, a->commit_id, ta->repo,
3858 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3859 if (err && err->code == GOT_ERR_CANCELLED)
3862 errcode = pthread_mutex_lock(&tog_mutex);
3864 return (void *)got_error_set_errno(errcode,
3865 "pthread_mutex_lock");
3867 got_repo_close(ta->repo);
3871 errcode = pthread_mutex_unlock(&tog_mutex);
3872 if (errcode && err == NULL)
3873 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3878 static struct got_object_id *
3879 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3880 int first_displayed_line, int selected_line)
3882 struct tog_blame_line *line;
3887 line = &lines[first_displayed_line - 1 + selected_line - 1];
3888 if (!line->annotated)
3894 static const struct got_error *
3895 stop_blame(struct tog_blame *blame)
3897 const struct got_error *err = NULL;
3900 if (blame->thread) {
3902 errcode = pthread_mutex_unlock(&tog_mutex);
3904 return got_error_set_errno(errcode,
3905 "pthread_mutex_unlock");
3906 errcode = pthread_join(blame->thread, (void **)&err);
3908 return got_error_set_errno(errcode, "pthread_join");
3909 errcode = pthread_mutex_lock(&tog_mutex);
3911 return got_error_set_errno(errcode,
3912 "pthread_mutex_lock");
3913 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3915 blame->thread = NULL;
3917 if (blame->thread_args.repo) {
3918 got_repo_close(blame->thread_args.repo);
3919 blame->thread_args.repo = NULL;
3922 if (fclose(blame->f) != 0 && err == NULL)
3923 err = got_error_from_errno("fclose");
3927 for (i = 0; i < blame->nlines; i++)
3928 free(blame->lines[i].id);
3930 blame->lines = NULL;
3932 free(blame->cb_args.commit_id);
3933 blame->cb_args.commit_id = NULL;
3938 static const struct got_error *
3939 cancel_blame_view(void *arg)
3941 const struct got_error *err = NULL;
3945 errcode = pthread_mutex_lock(&tog_mutex);
3947 return got_error_set_errno(errcode,
3948 "pthread_mutex_unlock");
3951 err = got_error(GOT_ERR_CANCELLED);
3953 errcode = pthread_mutex_unlock(&tog_mutex);
3955 return got_error_set_errno(errcode,
3956 "pthread_mutex_lock");
3961 static const struct got_error *
3962 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3963 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3964 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3965 struct got_repository *repo)
3967 const struct got_error *err = NULL;
3968 struct got_blob_object *blob = NULL;
3969 struct got_repository *thread_repo = NULL;
3970 struct got_object_id *obj_id = NULL;
3973 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3977 err = got_object_get_type(&obj_type, repo, obj_id);
3981 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3982 err = got_error(GOT_ERR_OBJ_TYPE);
3986 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3989 blame->f = got_opentemp();
3990 if (blame->f == NULL) {
3991 err = got_error_from_errno("got_opentemp");
3994 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3995 &blame->line_offsets, blame->f, blob);
3996 if (err || blame->nlines == 0)
3999 /* Don't include \n at EOF in the blame line count. */
4000 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4003 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4004 if (blame->lines == NULL) {
4005 err = got_error_from_errno("calloc");
4009 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4013 blame->cb_args.view = view;
4014 blame->cb_args.lines = blame->lines;
4015 blame->cb_args.nlines = blame->nlines;
4016 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4017 if (blame->cb_args.commit_id == NULL) {
4018 err = got_error_from_errno("got_object_id_dup");
4021 blame->cb_args.quit = done;
4023 blame->thread_args.path = path;
4024 blame->thread_args.repo = thread_repo;
4025 blame->thread_args.cb_args = &blame->cb_args;
4026 blame->thread_args.complete = blame_complete;
4027 blame->thread_args.cancel_cb = cancel_blame_view;
4028 blame->thread_args.cancel_arg = done;
4029 *blame_complete = 0;
4033 got_object_blob_close(blob);
4040 static const struct got_error *
4041 open_blame_view(struct tog_view *view, char *path,
4042 struct got_object_id *commit_id, struct got_reflist_head *refs,
4043 struct got_repository *repo)
4045 const struct got_error *err = NULL;
4046 struct tog_blame_view_state *s = &view->state.blame;
4048 SIMPLEQ_INIT(&s->blamed_commits);
4050 s->path = strdup(path);
4051 if (s->path == NULL)
4052 return got_error_from_errno("strdup");
4054 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4060 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4061 s->first_displayed_line = 1;
4062 s->last_displayed_line = view->nlines;
4063 s->selected_line = 1;
4064 s->blame_complete = 0;
4067 s->commit_id = commit_id;
4068 memset(&s->blame, 0, sizeof(s->blame));
4070 SIMPLEQ_INIT(&s->colors);
4071 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4072 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4073 get_color_value("TOG_COLOR_COMMIT"));
4078 view->show = show_blame_view;
4079 view->input = input_blame_view;
4080 view->close = close_blame_view;
4081 view->search_start = search_start_blame_view;
4082 view->search_next = search_next_blame_view;
4084 return run_blame(&s->blame, view, &s->blame_complete,
4085 &s->first_displayed_line, &s->last_displayed_line,
4086 &s->selected_line, &s->done, &s->eof, s->path,
4087 s->blamed_commit->id, s->repo);
4090 static const struct got_error *
4091 close_blame_view(struct tog_view *view)
4093 const struct got_error *err = NULL;
4094 struct tog_blame_view_state *s = &view->state.blame;
4096 if (s->blame.thread)
4097 err = stop_blame(&s->blame);
4099 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4100 struct got_object_qid *blamed_commit;
4101 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4102 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4103 got_object_qid_free(blamed_commit);
4107 free_colors(&s->colors);
4112 static const struct got_error *
4113 search_start_blame_view(struct tog_view *view)
4115 struct tog_blame_view_state *s = &view->state.blame;
4117 s->matched_line = 0;
4121 static const struct got_error *
4122 search_next_blame_view(struct tog_view *view)
4124 struct tog_blame_view_state *s = &view->state.blame;
4127 if (!view->searching) {
4128 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4132 if (s->matched_line) {
4133 if (view->searching == TOG_SEARCH_FORWARD)
4134 lineno = s->matched_line + 1;
4136 lineno = s->matched_line - 1;
4138 if (view->searching == TOG_SEARCH_FORWARD)
4141 lineno = s->blame.nlines;
4149 if (lineno <= 0 || lineno > s->blame.nlines) {
4150 if (s->matched_line == 0) {
4151 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4156 if (view->searching == TOG_SEARCH_FORWARD)
4159 lineno = s->blame.nlines;
4162 offset = s->blame.line_offsets[lineno - 1];
4163 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4165 return got_error_from_errno("fseeko");
4168 line = parse_next_line(s->blame.f, &len);
4169 if (line && match_line(line, &view->regex)) {
4170 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4171 s->matched_line = lineno;
4176 if (view->searching == TOG_SEARCH_FORWARD)
4182 if (s->matched_line) {
4183 s->first_displayed_line = s->matched_line;
4184 s->selected_line = 1;
4190 static const struct got_error *
4191 show_blame_view(struct tog_view *view)
4193 const struct got_error *err = NULL;
4194 struct tog_blame_view_state *s = &view->state.blame;
4197 if (s->blame.thread == NULL) {
4198 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4199 &s->blame.thread_args);
4201 return got_error_set_errno(errcode, "pthread_create");
4203 halfdelay(1); /* fast refresh while annotating */
4206 if (s->blame_complete)
4207 halfdelay(10); /* disable fast refresh */
4209 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4210 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4211 s->selected_line, &s->first_displayed_line,
4212 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4218 static const struct got_error *
4219 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4220 struct tog_view **focus_view, struct tog_view *view, int ch)
4222 const struct got_error *err = NULL, *thread_err = NULL;
4223 struct tog_view *diff_view;
4224 struct tog_blame_view_state *s = &view->state.blame;
4233 if (s->selected_line > 1)
4235 else if (s->selected_line == 1 &&
4236 s->first_displayed_line > 1)
4237 s->first_displayed_line--;
4241 if (s->first_displayed_line == 1) {
4242 s->selected_line = 1;
4245 if (s->first_displayed_line > view->nlines - 2)
4246 s->first_displayed_line -=
4249 s->first_displayed_line = 1;
4253 if (s->selected_line < view->nlines - 2 &&
4254 s->first_displayed_line +
4255 s->selected_line <= s->blame.nlines)
4257 else if (s->last_displayed_line <
4259 s->first_displayed_line++;
4263 struct got_object_id *id = NULL;
4264 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4265 s->first_displayed_line, s->selected_line);
4269 struct got_commit_object *commit;
4270 struct got_object_qid *pid;
4271 struct got_object_id *blob_id = NULL;
4273 err = got_object_open_as_commit(&commit,
4277 pid = SIMPLEQ_FIRST(
4278 got_object_commit_get_parent_ids(commit));
4280 got_object_commit_close(commit);
4283 /* Check if path history ends here. */
4284 err = got_object_id_by_path(&blob_id, s->repo,
4287 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4289 got_object_commit_close(commit);
4292 err = got_object_get_type(&obj_type, s->repo,
4295 /* Can't blame non-blob type objects. */
4296 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4297 got_object_commit_close(commit);
4300 err = got_object_qid_alloc(&s->blamed_commit,
4302 got_object_commit_close(commit);
4304 if (got_object_id_cmp(id,
4305 s->blamed_commit->id) == 0)
4307 err = got_object_qid_alloc(&s->blamed_commit,
4313 thread_err = stop_blame(&s->blame);
4317 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4318 s->blamed_commit, entry);
4319 err = run_blame(&s->blame, view, &s->blame_complete,
4320 &s->first_displayed_line, &s->last_displayed_line,
4321 &s->selected_line, &s->done, &s->eof,
4322 s->path, s->blamed_commit->id, s->repo);
4328 struct got_object_qid *first;
4329 first = SIMPLEQ_FIRST(&s->blamed_commits);
4330 if (!got_object_id_cmp(first->id, s->commit_id))
4333 thread_err = stop_blame(&s->blame);
4337 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4338 got_object_qid_free(s->blamed_commit);
4340 SIMPLEQ_FIRST(&s->blamed_commits);
4341 err = run_blame(&s->blame, view, &s->blame_complete,
4342 &s->first_displayed_line, &s->last_displayed_line,
4343 &s->selected_line, &s->done, &s->eof, s->path,
4344 s->blamed_commit->id, s->repo);
4351 struct got_object_id *id = NULL;
4352 struct got_object_qid *pid;
4353 struct got_commit_object *commit = NULL;
4354 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4355 s->first_displayed_line, s->selected_line);
4358 err = got_object_open_as_commit(&commit, s->repo, id);
4361 pid = SIMPLEQ_FIRST(
4362 got_object_commit_get_parent_ids(commit));
4363 if (view_is_parent_view(view))
4364 begin_x = view_split_begin_x(view->begin_x);
4365 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4366 if (diff_view == NULL) {
4367 got_object_commit_close(commit);
4368 err = got_error_from_errno("view_open");
4371 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4372 id, NULL, s->refs, s->repo);
4373 got_object_commit_close(commit);
4375 view_close(diff_view);
4378 if (view_is_parent_view(view)) {
4379 err = view_close_child(view);
4382 err = view_set_child(view, diff_view);
4384 view_close(diff_view);
4387 *focus_view = diff_view;
4388 view->child_focussed = 1;
4390 *new_view = diff_view;
4398 if (s->last_displayed_line >= s->blame.nlines &&
4399 s->selected_line >= MIN(s->blame.nlines,
4400 view->nlines - 2)) {
4403 if (s->last_displayed_line >= s->blame.nlines &&
4404 s->selected_line < view->nlines - 2) {
4405 s->selected_line = MIN(s->blame.nlines,
4409 if (s->last_displayed_line + view->nlines - 2
4411 s->first_displayed_line +=
4414 s->first_displayed_line =
4419 if (s->selected_line > view->nlines - 2) {
4420 s->selected_line = MIN(s->blame.nlines,
4427 return thread_err ? thread_err : err;
4430 static const struct got_error *
4431 cmd_blame(int argc, char *argv[])
4433 const struct got_error *error;
4434 struct got_repository *repo = NULL;
4435 struct got_reflist_head refs;
4436 struct got_worktree *worktree = NULL;
4437 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4438 struct got_object_id *commit_id = NULL;
4439 char *commit_id_str = NULL;
4441 struct tog_view *view;
4443 SIMPLEQ_INIT(&refs);
4446 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4451 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4454 commit_id_str = optarg;
4457 repo_path = realpath(optarg, NULL);
4458 if (repo_path == NULL)
4459 return got_error_from_errno2("realpath",
4474 cwd = getcwd(NULL, 0);
4476 return got_error_from_errno("getcwd");
4478 error = got_worktree_open(&worktree, cwd);
4479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4482 if (repo_path == NULL) {
4485 strdup(got_worktree_get_repo_path(worktree));
4487 repo_path = strdup(cwd);
4489 if (repo_path == NULL) {
4490 error = got_error_from_errno("strdup");
4494 error = got_repo_open(&repo, repo_path, NULL);
4498 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4505 error = apply_unveil(got_repo_get_path(repo), NULL);
4509 if (commit_id_str == NULL) {
4510 struct got_reference *head_ref;
4511 error = got_ref_open(&head_ref, repo, worktree ?
4512 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4515 error = got_ref_resolve(&commit_id, repo, head_ref);
4516 got_ref_close(head_ref);
4518 error = got_repo_match_object_id(&commit_id, NULL,
4519 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4524 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4528 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4530 error = got_error_from_errno("view_open");
4533 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4537 /* Release work tree lock. */
4538 got_worktree_close(worktree);
4541 error = view_loop(view);
4548 got_worktree_close(worktree);
4550 got_repo_close(repo);
4551 got_ref_list_free(&refs);
4555 static const struct got_error *
4556 draw_tree_entries(struct tog_view *view,
4557 struct got_tree_entry **first_displayed_entry,
4558 struct got_tree_entry **last_displayed_entry,
4559 struct got_tree_entry **selected_entry, int *ndisplayed,
4560 const char *label, int show_ids, const char *parent_path,
4561 struct got_tree_object *tree, int selected, int limit,
4562 int isroot, struct tog_colors *colors)
4564 const struct got_error *err = NULL;
4565 struct got_tree_entry *te;
4567 struct tog_color *tc;
4568 int width, n, i, nentries;
4572 werase(view->window);
4577 err = format_line(&wline, &width, label, view->ncols, 0);
4580 if (view_needs_focus_indication(view))
4581 wstandout(view->window);
4582 tc = get_color(colors, TOG_COLOR_COMMIT);
4584 wattr_on(view->window,
4585 COLOR_PAIR(tc->colorpair), NULL);
4586 waddwstr(view->window, wline);
4588 wattr_off(view->window,
4589 COLOR_PAIR(tc->colorpair), NULL);
4590 if (view_needs_focus_indication(view))
4591 wstandend(view->window);
4594 if (width < view->ncols - 1)
4595 waddch(view->window, '\n');
4598 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4601 waddwstr(view->window, wline);
4604 if (width < view->ncols - 1)
4605 waddch(view->window, '\n');
4608 waddch(view->window, '\n');
4612 if (*first_displayed_entry == NULL) {
4613 te = got_object_tree_get_first_entry(tree);
4614 if (selected == 0) {
4616 wstandout(view->window);
4617 *selected_entry = NULL;
4619 waddstr(view->window, " ..\n"); /* parent directory */
4620 if (selected == 0 && view->focussed)
4621 wstandend(view->window);
4628 te = *first_displayed_entry;
4631 nentries = got_object_tree_get_nentries(tree);
4632 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4633 char *line = NULL, *id_str = NULL;
4634 const char *modestr = "";
4637 te = got_object_tree_get_entry(tree, i);
4638 mode = got_tree_entry_get_mode(te);
4641 err = got_object_id_str(&id_str,
4642 got_tree_entry_get_id(te));
4644 return got_error_from_errno(
4645 "got_object_id_str");
4647 if (got_object_tree_entry_is_submodule(te))
4649 else if (S_ISLNK(mode))
4651 else if (S_ISDIR(mode))
4653 else if (mode & S_IXUSR)
4655 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4656 got_tree_entry_get_name(te), modestr) == -1) {
4658 return got_error_from_errno("asprintf");
4661 err = format_line(&wline, &width, line, view->ncols, 0);
4666 if (n == selected) {
4668 wstandout(view->window);
4669 *selected_entry = te;
4671 tc = match_color(colors, line);
4673 wattr_on(view->window,
4674 COLOR_PAIR(tc->colorpair), NULL);
4675 waddwstr(view->window, wline);
4677 wattr_off(view->window,
4678 COLOR_PAIR(tc->colorpair), NULL);
4679 if (width < view->ncols - 1)
4680 waddch(view->window, '\n');
4681 if (n == selected && view->focussed)
4682 wstandend(view->window);
4688 *last_displayed_entry = te;
4697 tree_scroll_up(struct tog_view *view,
4698 struct got_tree_entry **first_displayed_entry, int maxscroll,
4699 struct got_tree_object *tree, int isroot)
4701 struct got_tree_entry *te;
4704 if (*first_displayed_entry == NULL)
4707 te = got_object_tree_get_entry(tree, 0);
4708 if (*first_displayed_entry == te) {
4710 *first_displayed_entry = NULL;
4715 while (*first_displayed_entry && i < maxscroll) {
4716 *first_displayed_entry = got_tree_entry_get_prev(tree,
4717 *first_displayed_entry);
4720 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4721 *first_displayed_entry = NULL;
4725 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4726 struct got_tree_entry *last_displayed_entry,
4727 struct got_tree_object *tree)
4729 struct got_tree_entry *next, *last;
4732 if (*first_displayed_entry)
4733 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4735 next = got_object_tree_get_first_entry(tree);
4737 last = last_displayed_entry;
4738 while (next && last && n++ < maxscroll) {
4739 last = got_tree_entry_get_next(tree, last);
4741 *first_displayed_entry = next;
4742 next = got_tree_entry_get_next(tree, next);
4748 static const struct got_error *
4749 tree_entry_path(char **path, struct tog_parent_trees *parents,
4750 struct got_tree_entry *te)
4752 const struct got_error *err = NULL;
4753 struct tog_parent_tree *pt;
4754 size_t len = 2; /* for leading slash and NUL */
4756 TAILQ_FOREACH(pt, parents, entry)
4757 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4760 len += strlen(got_tree_entry_get_name(te));
4762 *path = calloc(1, len);
4764 return got_error_from_errno("calloc");
4767 pt = TAILQ_LAST(parents, tog_parent_trees);
4769 const char *name = got_tree_entry_get_name(pt->selected_entry);
4770 if (strlcat(*path, name, len) >= len) {
4771 err = got_error(GOT_ERR_NO_SPACE);
4774 if (strlcat(*path, "/", len) >= len) {
4775 err = got_error(GOT_ERR_NO_SPACE);
4778 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4781 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4782 err = got_error(GOT_ERR_NO_SPACE);
4794 static const struct got_error *
4795 blame_tree_entry(struct tog_view **new_view, int begin_x,
4796 struct got_tree_entry *te, struct tog_parent_trees *parents,
4797 struct got_object_id *commit_id, struct got_reflist_head *refs,
4798 struct got_repository *repo)
4800 const struct got_error *err = NULL;
4802 struct tog_view *blame_view;
4806 err = tree_entry_path(&path, parents, te);
4810 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4811 if (blame_view == NULL) {
4812 err = got_error_from_errno("view_open");
4816 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4818 if (err->code == GOT_ERR_CANCELLED)
4820 view_close(blame_view);
4822 *new_view = blame_view;
4828 static const struct got_error *
4829 log_tree_entry(struct tog_view **new_view, int begin_x,
4830 struct got_tree_entry *te, struct tog_parent_trees *parents,
4831 struct got_object_id *commit_id, struct got_reflist_head *refs,
4832 struct got_repository *repo)
4834 struct tog_view *log_view;
4835 const struct got_error *err = NULL;
4840 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4841 if (log_view == NULL)
4842 return got_error_from_errno("view_open");
4844 err = tree_entry_path(&path, parents, te);
4848 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4850 view_close(log_view);
4852 *new_view = log_view;
4857 static const struct got_error *
4858 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4859 struct got_object_id *commit_id, struct got_reflist_head *refs,
4860 struct got_repository *repo)
4862 const struct got_error *err = NULL;
4863 char *commit_id_str = NULL;
4864 struct tog_tree_view_state *s = &view->state.tree;
4866 TAILQ_INIT(&s->parents);
4868 err = got_object_id_str(&commit_id_str, commit_id);
4872 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4873 err = got_error_from_errno("asprintf");
4877 s->root = s->tree = root;
4878 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4879 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4880 s->commit_id = got_object_id_dup(commit_id);
4881 if (s->commit_id == NULL) {
4882 err = got_error_from_errno("got_object_id_dup");
4888 SIMPLEQ_INIT(&s->colors);
4890 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4891 err = add_color(&s->colors, "\\$$",
4892 TOG_COLOR_TREE_SUBMODULE,
4893 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4896 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4897 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4899 free_colors(&s->colors);
4902 err = add_color(&s->colors, "/$",
4903 TOG_COLOR_TREE_DIRECTORY,
4904 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4906 free_colors(&s->colors);
4910 err = add_color(&s->colors, "\\*$",
4911 TOG_COLOR_TREE_EXECUTABLE,
4912 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4914 free_colors(&s->colors);
4918 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4919 get_color_value("TOG_COLOR_COMMIT"));
4921 free_colors(&s->colors);
4926 view->show = show_tree_view;
4927 view->input = input_tree_view;
4928 view->close = close_tree_view;
4929 view->search_start = search_start_tree_view;
4930 view->search_next = search_next_tree_view;
4932 free(commit_id_str);
4934 free(s->tree_label);
4935 s->tree_label = NULL;
4940 static const struct got_error *
4941 close_tree_view(struct tog_view *view)
4943 struct tog_tree_view_state *s = &view->state.tree;
4945 free_colors(&s->colors);
4946 free(s->tree_label);
4947 s->tree_label = NULL;
4949 s->commit_id = NULL;
4950 while (!TAILQ_EMPTY(&s->parents)) {
4951 struct tog_parent_tree *parent;
4952 parent = TAILQ_FIRST(&s->parents);
4953 TAILQ_REMOVE(&s->parents, parent, entry);
4957 if (s->tree != s->root)
4958 got_object_tree_close(s->tree);
4959 got_object_tree_close(s->root);
4964 static const struct got_error *
4965 search_start_tree_view(struct tog_view *view)
4967 struct tog_tree_view_state *s = &view->state.tree;
4969 s->matched_entry = NULL;
4974 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4976 regmatch_t regmatch;
4978 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4982 static const struct got_error *
4983 search_next_tree_view(struct tog_view *view)
4985 struct tog_tree_view_state *s = &view->state.tree;
4986 struct got_tree_entry *te = NULL;
4988 if (!view->searching) {
4989 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4993 if (s->matched_entry) {
4994 if (view->searching == TOG_SEARCH_FORWARD) {
4995 if (s->selected_entry)
4996 te = got_tree_entry_get_next(s->tree,
4999 te = got_object_tree_get_first_entry(s->tree);
5001 if (s->selected_entry == NULL)
5002 te = got_object_tree_get_last_entry(s->tree);
5004 te = got_tree_entry_get_prev(s->tree,
5008 if (view->searching == TOG_SEARCH_FORWARD)
5009 te = got_object_tree_get_first_entry(s->tree);
5011 te = got_object_tree_get_last_entry(s->tree);
5016 if (s->matched_entry == NULL) {
5017 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5020 if (view->searching == TOG_SEARCH_FORWARD)
5021 te = got_object_tree_get_first_entry(s->tree);
5023 te = got_object_tree_get_last_entry(s->tree);
5026 if (match_tree_entry(te, &view->regex)) {
5027 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5028 s->matched_entry = te;
5032 if (view->searching == TOG_SEARCH_FORWARD)
5033 te = got_tree_entry_get_next(s->tree, te);
5035 te = got_tree_entry_get_prev(s->tree, te);
5038 if (s->matched_entry) {
5039 s->first_displayed_entry = s->matched_entry;
5046 static const struct got_error *
5047 show_tree_view(struct tog_view *view)
5049 const struct got_error *err = NULL;
5050 struct tog_tree_view_state *s = &view->state.tree;
5053 err = tree_entry_path(&parent_path, &s->parents, NULL);
5057 err = draw_tree_entries(view, &s->first_displayed_entry,
5058 &s->last_displayed_entry, &s->selected_entry,
5059 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5060 s->tree, s->selected, view->nlines, s->tree == s->root,
5068 static const struct got_error *
5069 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5070 struct tog_view **focus_view, struct tog_view *view, int ch)
5072 const struct got_error *err = NULL;
5073 struct tog_tree_view_state *s = &view->state.tree;
5074 struct tog_view *log_view;
5075 int begin_x = 0, nscrolled;
5079 s->show_ids = !s->show_ids;
5082 if (!s->selected_entry)
5084 if (view_is_parent_view(view))
5085 begin_x = view_split_begin_x(view->begin_x);
5086 err = log_tree_entry(&log_view, begin_x,
5087 s->selected_entry, &s->parents,
5088 s->commit_id, s->refs, s->repo);
5089 if (view_is_parent_view(view)) {
5090 err = view_close_child(view);
5093 err = view_set_child(view, log_view);
5095 view_close(log_view);
5098 *focus_view = log_view;
5099 view->child_focussed = 1;
5101 *new_view = log_view;
5105 if (s->selected > 0) {
5107 if (s->selected == 0)
5110 if (s->selected > 0)
5112 tree_scroll_up(view, &s->first_displayed_entry, 1,
5113 s->tree, s->tree == s->root);
5117 tree_scroll_up(view, &s->first_displayed_entry,
5118 MAX(0, view->nlines - 4 - s->selected), s->tree,
5119 s->tree == s->root);
5121 if (got_object_tree_get_first_entry(s->tree) ==
5122 s->first_displayed_entry && s->tree != s->root)
5123 s->first_displayed_entry = NULL;
5127 if (s->selected < s->ndisplayed - 1) {
5131 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5133 /* can't scroll any further */
5135 tree_scroll_down(&s->first_displayed_entry, 1,
5136 s->last_displayed_entry, s->tree);
5140 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5142 /* can't scroll any further; move cursor down */
5143 if (s->selected < s->ndisplayed - 1)
5144 s->selected = s->ndisplayed - 1;
5147 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5148 view->nlines, s->last_displayed_entry, s->tree);
5149 if (nscrolled < view->nlines) {
5151 struct got_tree_entry *te;
5152 te = s->first_displayed_entry;
5155 te = got_tree_entry_get_next(s->tree, te);
5157 s->selected = ndisplayed - 1;
5163 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5164 struct tog_parent_tree *parent;
5165 /* user selected '..' */
5166 if (s->tree == s->root)
5168 parent = TAILQ_FIRST(&s->parents);
5169 TAILQ_REMOVE(&s->parents, parent,
5171 got_object_tree_close(s->tree);
5172 s->tree = parent->tree;
5173 s->first_displayed_entry =
5174 parent->first_displayed_entry;
5176 parent->selected_entry;
5177 s->selected = parent->selected;
5179 } else if (S_ISDIR(got_tree_entry_get_mode(
5180 s->selected_entry))) {
5181 struct got_tree_object *subtree;
5182 err = got_object_open_as_tree(&subtree, s->repo,
5183 got_tree_entry_get_id(s->selected_entry));
5186 err = tree_view_visit_subtree(subtree, s);
5188 got_object_tree_close(subtree);
5191 } else if (S_ISREG(got_tree_entry_get_mode(
5192 s->selected_entry))) {
5193 struct tog_view *blame_view;
5194 int begin_x = view_is_parent_view(view) ?
5195 view_split_begin_x(view->begin_x) : 0;
5197 err = blame_tree_entry(&blame_view, begin_x,
5198 s->selected_entry, &s->parents,
5199 s->commit_id, s->refs, s->repo);
5202 if (view_is_parent_view(view)) {
5203 err = view_close_child(view);
5206 err = view_set_child(view, blame_view);
5208 view_close(blame_view);
5211 *focus_view = blame_view;
5212 view->child_focussed = 1;
5214 *new_view = blame_view;
5218 if (s->selected > view->nlines)
5219 s->selected = s->ndisplayed - 1;
5232 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5237 static const struct got_error *
5238 cmd_tree(int argc, char *argv[])
5240 const struct got_error *error;
5241 struct got_repository *repo = NULL;
5242 struct got_worktree *worktree = NULL;
5243 struct got_reflist_head refs;
5244 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5245 struct got_object_id *commit_id = NULL;
5246 char *commit_id_arg = NULL;
5247 struct got_commit_object *commit = NULL;
5248 struct got_tree_object *tree = NULL;
5250 struct tog_view *view;
5252 SIMPLEQ_INIT(&refs);
5255 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5260 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5263 commit_id_arg = optarg;
5266 repo_path = realpath(optarg, NULL);
5267 if (repo_path == NULL)
5268 return got_error_from_errno2("realpath",
5283 cwd = getcwd(NULL, 0);
5285 return got_error_from_errno("getcwd");
5287 error = got_worktree_open(&worktree, cwd);
5288 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5291 if (repo_path == NULL) {
5294 strdup(got_worktree_get_repo_path(worktree));
5296 repo_path = strdup(cwd);
5298 if (repo_path == NULL) {
5299 error = got_error_from_errno("strdup");
5303 error = got_repo_open(&repo, repo_path, NULL);
5307 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5314 error = apply_unveil(got_repo_get_path(repo), NULL);
5318 error = got_repo_match_object_id(&commit_id, NULL,
5319 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5320 GOT_OBJ_TYPE_COMMIT, 1, repo);
5324 error = got_object_open_as_commit(&commit, repo, commit_id);
5328 error = got_object_open_as_tree(&tree, repo,
5329 got_object_commit_get_tree_id(commit));
5333 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5337 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5339 error = got_error_from_errno("view_open");
5342 error = open_tree_view(view, tree, commit_id, &refs, repo);
5345 if (!got_path_is_root_dir(in_repo_path)) {
5346 error = tree_view_walk_path(&view->state.tree, commit_id,
5347 in_repo_path, repo);
5353 /* Release work tree lock. */
5354 got_worktree_close(worktree);
5357 error = view_loop(view);
5363 got_object_commit_close(commit);
5365 got_object_tree_close(tree);
5367 got_repo_close(repo);
5368 got_ref_list_free(&refs);
5377 fprintf(stderr, "commands:");
5378 for (i = 0; i < nitems(tog_commands); i++) {
5379 struct tog_cmd *cmd = &tog_commands[i];
5380 fprintf(stderr, " %s", cmd->name);
5382 fputc('\n', stderr);
5388 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5389 "[arg ...]\n", getprogname());
5391 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5398 make_argv(int argc, ...)
5406 argv = calloc(argc, sizeof(char *));
5409 for (i = 0; i < argc; i++) {
5410 argv[i] = strdup(va_arg(ap, char *));
5411 if (argv[i] == NULL)
5420 * Try to convert 'tog path' into a 'tog log path' command.
5421 * The user could simply have mistyped the command rather than knowingly
5422 * provided a path. So check whether argv[0] can in fact be resolved
5423 * to a path in the HEAD commit and print a special error if not.
5424 * This hack is for mpi@ <3
5426 static const struct got_error *
5427 tog_log_with_path(int argc, char *argv[])
5429 const struct got_error *error = NULL;
5430 struct tog_cmd *cmd = NULL;
5431 struct got_repository *repo = NULL;
5432 struct got_worktree *worktree = NULL;
5433 struct got_object_id *commit_id = NULL, *id = NULL;
5434 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5435 char *commit_id_str = NULL, **cmd_argv = NULL;
5437 cwd = getcwd(NULL, 0);
5439 return got_error_from_errno("getcwd");
5441 error = got_worktree_open(&worktree, cwd);
5442 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5446 repo_path = strdup(got_worktree_get_repo_path(worktree));
5448 repo_path = strdup(cwd);
5449 if (repo_path == NULL) {
5450 error = got_error_from_errno("strdup");
5454 error = got_repo_open(&repo, repo_path, NULL);
5458 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5463 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5464 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5465 GOT_OBJ_TYPE_COMMIT, 1, repo);
5470 got_worktree_close(worktree);
5474 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5476 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5478 fprintf(stderr, "%s: '%s' is no known command or path\n",
5479 getprogname(), argv[0]);
5484 got_repo_close(repo);
5487 error = got_object_id_str(&commit_id_str, commit_id);
5491 cmd = &tog_commands[0]; /* log */
5493 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5494 error = cmd->cmd_main(argc, cmd_argv);
5497 got_repo_close(repo);
5499 got_worktree_close(worktree);
5501 free(commit_id_str);
5508 for (i = 0; i < argc; i++)
5516 main(int argc, char *argv[])
5518 const struct got_error *error = NULL;
5519 struct tog_cmd *cmd = NULL;
5520 int ch, hflag = 0, Vflag = 0;
5521 char **cmd_argv = NULL;
5522 static struct option longopts[] = {
5523 { "version", no_argument, NULL, 'V' },
5527 setlocale(LC_CTYPE, "");
5529 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5549 got_version_print_str();
5556 /* Build an argument vector which runs a default command. */
5557 cmd = &tog_commands[0];
5559 cmd_argv = make_argv(argc, cmd->name);
5563 /* Did the user specify a command? */
5564 for (i = 0; i < nitems(tog_commands); i++) {
5565 if (strncmp(tog_commands[i].name, argv[0],
5566 strlen(argv[0])) == 0) {
5567 cmd = &tog_commands[i];
5576 /* No command specified; try log with a path */
5577 error = tog_log_with_path(argc, argv);
5582 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5588 for (i = 0; i < argc; i++)
5593 if (error && error->code != GOT_ERR_CANCELLED)
5594 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);