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 s = strstr(name, "/" GOT_REF_HEAD);
1179 if (s != NULL && s[strlen(s)] == '\0')
1182 if (strncmp(name, "tags/", 5) == 0) {
1183 err = got_object_open_as_tag(&tag, repo, re->id);
1185 if (err->code != GOT_ERR_OBJ_TYPE)
1187 /* Ref points at something other than a tag. */
1192 cmp = got_object_id_cmp(tag ?
1193 got_object_tag_get_object_id(tag) : re->id, id);
1195 got_object_tag_close(tag);
1199 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1200 s ? ", " : "", name) == -1) {
1201 err = got_error_from_errno("asprintf");
1212 static const struct got_error *
1213 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1216 char *smallerthan, *at;
1218 smallerthan = strchr(author, '<');
1219 if (smallerthan && smallerthan[1] != '\0')
1220 author = smallerthan + 1;
1221 at = strchr(author, '@');
1224 return format_line(wauthor, author_width, author, limit, col_tab_align);
1227 static const struct got_error *
1228 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1229 struct got_object_id *id, struct got_reflist_head *refs,
1230 const size_t date_display_cols, int author_display_cols,
1231 struct tog_colors *colors)
1233 const struct got_error *err = NULL;
1234 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1235 char *logmsg0 = NULL, *logmsg = NULL;
1236 char *author = NULL;
1237 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1238 int author_width, logmsg_width;
1239 char *newline, *line = NULL;
1241 const int avail = view->ncols;
1243 time_t committer_time;
1244 struct tog_color *tc;
1246 committer_time = got_object_commit_get_committer_time(commit);
1247 if (localtime_r(&committer_time, &tm) == NULL)
1248 return got_error_from_errno("localtime_r");
1249 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1251 return got_error(GOT_ERR_NO_SPACE);
1253 if (avail <= date_display_cols)
1254 limit = MIN(sizeof(datebuf) - 1, avail);
1256 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1257 tc = get_color(colors, TOG_COLOR_DATE);
1259 wattr_on(view->window,
1260 COLOR_PAIR(tc->colorpair), NULL);
1261 waddnstr(view->window, datebuf, limit);
1263 wattr_off(view->window,
1264 COLOR_PAIR(tc->colorpair), NULL);
1271 err = got_object_id_str(&id_str, id);
1274 tc = get_color(colors, TOG_COLOR_COMMIT);
1276 wattr_on(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1278 wprintw(view->window, "%.8s ", id_str);
1280 wattr_off(view->window,
1281 COLOR_PAIR(tc->colorpair), NULL);
1288 author = strdup(got_object_commit_get_author(commit));
1289 if (author == NULL) {
1290 err = got_error_from_errno("strdup");
1293 err = format_author(&wauthor, &author_width, author, avail - col, col);
1296 tc = get_color(colors, TOG_COLOR_AUTHOR);
1298 wattr_on(view->window,
1299 COLOR_PAIR(tc->colorpair), NULL);
1300 waddwstr(view->window, wauthor);
1302 wattr_off(view->window,
1303 COLOR_PAIR(tc->colorpair), NULL);
1304 col += author_width;
1305 while (col < avail && author_width < author_display_cols + 2) {
1306 waddch(view->window, ' ');
1313 err = got_object_commit_get_logmsg(&logmsg0, commit);
1317 while (*logmsg == '\n')
1319 newline = strchr(logmsg, '\n');
1322 limit = avail - col;
1323 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1326 waddwstr(view->window, wlogmsg);
1327 col += logmsg_width;
1328 while (col < avail) {
1329 waddch(view->window, ' ');
1341 static struct commit_queue_entry *
1342 alloc_commit_queue_entry(struct got_commit_object *commit,
1343 struct got_object_id *id)
1345 struct commit_queue_entry *entry;
1347 entry = calloc(1, sizeof(*entry));
1352 entry->commit = commit;
1357 pop_commit(struct commit_queue *commits)
1359 struct commit_queue_entry *entry;
1361 entry = TAILQ_FIRST(&commits->head);
1362 TAILQ_REMOVE(&commits->head, entry, entry);
1363 got_object_commit_close(entry->commit);
1364 commits->ncommits--;
1365 /* Don't free entry->id! It is owned by the commit graph. */
1370 free_commits(struct commit_queue *commits)
1372 while (!TAILQ_EMPTY(&commits->head))
1373 pop_commit(commits);
1376 static const struct got_error *
1377 match_commit(int *have_match, struct got_object_id *id,
1378 struct got_commit_object *commit, regex_t *regex)
1380 const struct got_error *err = NULL;
1381 regmatch_t regmatch;
1382 char *id_str = NULL, *logmsg = NULL;
1386 err = got_object_id_str(&id_str, id);
1390 err = got_object_commit_get_logmsg(&logmsg, commit);
1394 if (regexec(regex, got_object_commit_get_author(commit), 1,
1395 ®match, 0) == 0 ||
1396 regexec(regex, got_object_commit_get_committer(commit), 1,
1397 ®match, 0) == 0 ||
1398 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1399 regexec(regex, logmsg, 1, ®match, 0) == 0)
1407 static const struct got_error *
1408 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1409 int minqueue, struct got_repository *repo, const char *path,
1410 int *searching, int *search_next_done, regex_t *regex)
1412 const struct got_error *err = NULL;
1416 * We keep all commits open throughout the lifetime of the log
1417 * view in order to avoid having to re-fetch commits from disk
1418 * while updating the display.
1420 while (nqueued < minqueue ||
1421 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1422 struct got_object_id *id;
1423 struct got_commit_object *commit;
1424 struct commit_queue_entry *entry;
1427 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1428 if (err || id == NULL)
1431 err = got_object_open_as_commit(&commit, repo, id);
1434 entry = alloc_commit_queue_entry(commit, id);
1435 if (entry == NULL) {
1436 err = got_error_from_errno("alloc_commit_queue_entry");
1440 errcode = pthread_mutex_lock(&tog_mutex);
1442 err = got_error_set_errno(errcode,
1443 "pthread_mutex_lock");
1447 entry->idx = commits->ncommits;
1448 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1450 commits->ncommits++;
1452 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1454 err = match_commit(&have_match, id, commit, regex);
1458 *search_next_done = TOG_SEARCH_HAVE_MORE;
1461 errcode = pthread_mutex_unlock(&tog_mutex);
1462 if (errcode && err == NULL)
1463 err = got_error_set_errno(errcode,
1464 "pthread_mutex_unlock");
1472 static const struct got_error *
1473 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1474 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1475 struct commit_queue *commits, int selected_idx, int limit,
1476 struct got_reflist_head *refs, const char *path, int commits_needed,
1477 struct tog_colors *colors)
1479 const struct got_error *err = NULL;
1480 struct tog_log_view_state *s = &view->state.log;
1481 struct commit_queue_entry *entry;
1483 int ncommits, author_cols = 4;
1484 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1485 char *refs_str = NULL;
1487 struct tog_color *tc;
1488 static const size_t date_display_cols = 12;
1493 if (ncommits == selected_idx) {
1497 entry = TAILQ_NEXT(entry, entry);
1501 if (*selected && !(view->searching && view->search_next_done == 0)) {
1502 err = got_object_id_str(&id_str, (*selected)->id);
1506 err = build_refs_str(&refs_str, refs, (*selected)->id,
1513 if (commits_needed == 0)
1514 halfdelay(10); /* disable fast refresh */
1516 if (commits_needed > 0) {
1517 if (asprintf(&ncommits_str, " [%d/%d] %s",
1518 entry ? entry->idx + 1 : 0, commits->ncommits,
1519 (view->searching && !view->search_next_done) ?
1520 "searching..." : "loading...") == -1) {
1521 err = got_error_from_errno("asprintf");
1525 const char *search_str = NULL;
1527 if (view->searching) {
1528 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1529 search_str = "no more matches";
1530 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1531 search_str = "no matches found";
1532 else if (!view->search_next_done)
1533 search_str = "searching...";
1536 if (asprintf(&ncommits_str, " [%d/%d] %s",
1537 entry ? entry->idx + 1 : 0, commits->ncommits,
1538 search_str ? search_str :
1539 (refs_str ? refs_str : "")) == -1) {
1540 err = got_error_from_errno("asprintf");
1545 if (path && strcmp(path, "/") != 0) {
1546 if (asprintf(&header, "commit %s %s%s",
1547 id_str ? id_str : "........................................",
1548 path, ncommits_str) == -1) {
1549 err = got_error_from_errno("asprintf");
1553 } else if (asprintf(&header, "commit %s%s",
1554 id_str ? id_str : "........................................",
1555 ncommits_str) == -1) {
1556 err = got_error_from_errno("asprintf");
1560 err = format_line(&wline, &width, header, view->ncols, 0);
1564 werase(view->window);
1566 if (view_needs_focus_indication(view))
1567 wstandout(view->window);
1568 tc = get_color(colors, TOG_COLOR_COMMIT);
1570 wattr_on(view->window,
1571 COLOR_PAIR(tc->colorpair), NULL);
1572 waddwstr(view->window, wline);
1574 wattr_off(view->window,
1575 COLOR_PAIR(tc->colorpair), NULL);
1576 while (width < view->ncols) {
1577 waddch(view->window, ' ');
1580 if (view_needs_focus_indication(view))
1581 wstandend(view->window);
1586 /* Grow author column size if necessary. */
1593 if (ncommits >= limit - 1)
1595 author = strdup(got_object_commit_get_author(entry->commit));
1596 if (author == NULL) {
1597 err = got_error_from_errno("strdup");
1600 err = format_author(&wauthor, &width, author, COLS,
1602 if (author_cols < width)
1603 author_cols = width;
1607 entry = TAILQ_NEXT(entry, entry);
1614 if (ncommits >= limit - 1)
1616 if (ncommits == selected_idx)
1617 wstandout(view->window);
1618 err = draw_commit(view, entry->commit, entry->id, refs,
1619 date_display_cols, author_cols, colors);
1620 if (ncommits == selected_idx)
1621 wstandend(view->window);
1626 entry = TAILQ_NEXT(entry, entry);
1639 scroll_up(struct tog_view *view,
1640 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1641 struct commit_queue *commits)
1643 struct commit_queue_entry *entry;
1646 entry = TAILQ_FIRST(&commits->head);
1647 if (*first_displayed_entry == entry)
1650 entry = *first_displayed_entry;
1651 while (entry && nscrolled < maxscroll) {
1652 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1654 *first_displayed_entry = entry;
1660 static const struct got_error *
1661 trigger_log_thread(struct tog_view *log_view, int wait,
1662 int *commits_needed, int *log_complete,
1663 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1667 halfdelay(1); /* fast refresh while loading commits */
1669 while (*commits_needed > 0) {
1673 /* Wake the log thread. */
1674 errcode = pthread_cond_signal(need_commits);
1676 return got_error_set_errno(errcode,
1677 "pthread_cond_signal");
1680 * The mutex will be released while the view loop waits
1681 * in wgetch(), at which time the log thread will run.
1686 /* Display progress update in log view. */
1687 show_log_view(log_view);
1691 /* Wait right here while next commit is being loaded. */
1692 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1694 return got_error_set_errno(errcode,
1695 "pthread_cond_wait");
1697 /* Display progress update in log view. */
1698 show_log_view(log_view);
1706 static const struct got_error *
1707 scroll_down(struct tog_view *log_view,
1708 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1709 struct commit_queue_entry **last_displayed_entry,
1710 struct commit_queue *commits, int *log_complete, int *commits_needed,
1711 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1713 const struct got_error *err = NULL;
1714 struct commit_queue_entry *pentry;
1715 int nscrolled = 0, ncommits_needed;
1717 if (*last_displayed_entry == NULL)
1720 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1721 if (commits->ncommits < ncommits_needed && !*log_complete) {
1723 * Ask the log thread for required amount of commits.
1725 (*commits_needed) += maxscroll;
1726 err = trigger_log_thread(log_view, 1, commits_needed,
1727 log_complete, need_commits, commit_loaded);
1733 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1737 *last_displayed_entry = pentry;
1739 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1742 *first_displayed_entry = pentry;
1743 } while (++nscrolled < maxscroll);
1748 static const struct got_error *
1749 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1750 struct got_commit_object *commit, struct got_object_id *commit_id,
1751 struct tog_view *log_view, struct got_reflist_head *refs,
1752 struct got_repository *repo)
1754 const struct got_error *err;
1755 struct got_object_qid *parent_id;
1756 struct tog_view *diff_view;
1758 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1759 if (diff_view == NULL)
1760 return got_error_from_errno("view_open");
1762 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1763 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1764 commit_id, log_view, refs, repo);
1766 *new_view = diff_view;
1770 static const struct got_error *
1771 tree_view_visit_subtree(struct got_tree_object *subtree,
1772 struct tog_tree_view_state *s)
1774 struct tog_parent_tree *parent;
1776 parent = calloc(1, sizeof(*parent));
1778 return got_error_from_errno("calloc");
1780 parent->tree = s->tree;
1781 parent->first_displayed_entry = s->first_displayed_entry;
1782 parent->selected_entry = s->selected_entry;
1783 parent->selected = s->selected;
1784 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1787 s->first_displayed_entry = NULL;
1791 static const struct got_error *
1792 tree_view_walk_path(struct tog_tree_view_state *s,
1793 struct got_object_id *commit_id,
1794 const char *path, struct got_repository *repo)
1796 const struct got_error *err = NULL;
1797 struct got_tree_object *tree = NULL;
1799 char *slash, *subpath = NULL;
1801 /* Walk the path and open corresponding tree objects. */
1804 struct got_tree_entry *te;
1805 struct got_object_id *tree_id;
1811 /* Ensure the correct subtree entry is selected. */
1812 slash = strchr(p, '/');
1814 te_name = strdup(p);
1816 te_name = strndup(p, slash - p);
1817 if (te_name == NULL) {
1818 err = got_error_from_errno("strndup");
1821 te = got_object_tree_find_entry(s->tree, te_name);
1823 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1828 s->selected_entry = te;
1829 s->selected = got_tree_entry_get_index(te);
1830 if (s->tree != s->root)
1831 s->selected++; /* skip '..' */
1833 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1834 /* Jump to this file's entry. */
1835 s->first_displayed_entry = s->selected_entry;
1840 slash = strchr(p, '/');
1842 subpath = strndup(path, slash - path);
1844 subpath = strdup(path);
1845 if (subpath == NULL) {
1846 err = got_error_from_errno("strdup");
1850 err = got_object_id_by_path(&tree_id, repo, commit_id,
1855 err = got_object_open_as_tree(&tree, repo, tree_id);
1860 err = tree_view_visit_subtree(tree, s);
1862 got_object_tree_close(tree);
1876 static const struct got_error *
1877 browse_commit_tree(struct tog_view **new_view, int begin_x,
1878 struct commit_queue_entry *entry, const char *path,
1879 struct got_reflist_head *refs, struct got_repository *repo)
1881 const struct got_error *err = NULL;
1882 struct got_tree_object *tree;
1883 struct tog_tree_view_state *s;
1884 struct tog_view *tree_view;
1886 err = got_object_open_as_tree(&tree, repo,
1887 got_object_commit_get_tree_id(entry->commit));
1891 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1892 if (tree_view == NULL)
1893 return got_error_from_errno("view_open");
1895 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1897 got_object_tree_close(tree);
1900 s = &tree_view->state.tree;
1902 *new_view = tree_view;
1904 if (got_path_is_root_dir(path))
1907 return tree_view_walk_path(s, entry->id, path, repo);
1910 static const struct got_error *
1911 block_signals_used_by_main_thread(void)
1916 if (sigemptyset(&sigset) == -1)
1917 return got_error_from_errno("sigemptyset");
1919 /* tog handles SIGWINCH and SIGCONT */
1920 if (sigaddset(&sigset, SIGWINCH) == -1)
1921 return got_error_from_errno("sigaddset");
1922 if (sigaddset(&sigset, SIGCONT) == -1)
1923 return got_error_from_errno("sigaddset");
1925 /* ncurses handles SIGTSTP */
1926 if (sigaddset(&sigset, SIGTSTP) == -1)
1927 return got_error_from_errno("sigaddset");
1929 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1931 return got_error_set_errno(errcode, "pthread_sigmask");
1937 log_thread(void *arg)
1939 const struct got_error *err = NULL;
1941 struct tog_log_thread_args *a = arg;
1944 err = block_signals_used_by_main_thread();
1948 while (!done && !err && !tog_sigpipe_received) {
1949 err = queue_commits(a->graph, a->commits, 1, a->repo,
1950 a->in_repo_path, a->searching, a->search_next_done,
1953 if (err->code != GOT_ERR_ITER_COMPLETED)
1957 } else if (a->commits_needed > 0)
1958 a->commits_needed--;
1960 errcode = pthread_mutex_lock(&tog_mutex);
1962 err = got_error_set_errno(errcode,
1963 "pthread_mutex_lock");
1965 } else if (*a->quit)
1967 else if (*a->first_displayed_entry == NULL) {
1968 *a->first_displayed_entry =
1969 TAILQ_FIRST(&a->commits->head);
1970 *a->selected_entry = *a->first_displayed_entry;
1973 errcode = pthread_cond_signal(&a->commit_loaded);
1975 err = got_error_set_errno(errcode,
1976 "pthread_cond_signal");
1977 pthread_mutex_unlock(&tog_mutex);
1982 a->commits_needed = 0;
1984 if (a->commits_needed == 0) {
1985 errcode = pthread_cond_wait(&a->need_commits,
1988 err = got_error_set_errno(errcode,
1989 "pthread_cond_wait");
1993 errcode = pthread_mutex_unlock(&tog_mutex);
1994 if (errcode && err == NULL)
1995 err = got_error_set_errno(errcode,
1996 "pthread_mutex_unlock");
1998 a->log_complete = 1;
2002 static const struct got_error *
2003 stop_log_thread(struct tog_log_view_state *s)
2005 const struct got_error *err = NULL;
2010 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2012 return got_error_set_errno(errcode,
2013 "pthread_cond_signal");
2014 errcode = pthread_mutex_unlock(&tog_mutex);
2016 return got_error_set_errno(errcode,
2017 "pthread_mutex_unlock");
2018 errcode = pthread_join(s->thread, (void **)&err);
2020 return got_error_set_errno(errcode, "pthread_join");
2021 errcode = pthread_mutex_lock(&tog_mutex);
2023 return got_error_set_errno(errcode,
2024 "pthread_mutex_lock");
2028 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2029 if (errcode && err == NULL)
2030 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2032 if (s->thread_args.repo) {
2033 got_repo_close(s->thread_args.repo);
2034 s->thread_args.repo = NULL;
2037 if (s->thread_args.graph) {
2038 got_commit_graph_close(s->thread_args.graph);
2039 s->thread_args.graph = NULL;
2045 static const struct got_error *
2046 close_log_view(struct tog_view *view)
2048 const struct got_error *err = NULL;
2049 struct tog_log_view_state *s = &view->state.log;
2051 err = stop_log_thread(s);
2052 free_commits(&s->commits);
2053 free(s->in_repo_path);
2054 s->in_repo_path = NULL;
2060 static const struct got_error *
2061 search_start_log_view(struct tog_view *view)
2063 struct tog_log_view_state *s = &view->state.log;
2065 s->matched_entry = NULL;
2066 s->search_entry = NULL;
2070 static const struct got_error *
2071 search_next_log_view(struct tog_view *view)
2073 const struct got_error *err = NULL;
2074 struct tog_log_view_state *s = &view->state.log;
2075 struct commit_queue_entry *entry;
2077 /* Display progress update in log view. */
2078 show_log_view(view);
2082 if (s->search_entry) {
2084 errcode = pthread_mutex_unlock(&tog_mutex);
2086 return got_error_set_errno(errcode,
2087 "pthread_mutex_unlock");
2088 ch = wgetch(view->window);
2089 errcode = pthread_mutex_lock(&tog_mutex);
2091 return got_error_set_errno(errcode,
2092 "pthread_mutex_lock");
2093 if (ch == KEY_BACKSPACE) {
2094 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2097 if (view->searching == TOG_SEARCH_FORWARD)
2098 entry = TAILQ_NEXT(s->search_entry, entry);
2100 entry = TAILQ_PREV(s->search_entry,
2101 commit_queue_head, entry);
2102 } else if (s->matched_entry) {
2103 if (view->searching == TOG_SEARCH_FORWARD)
2104 entry = TAILQ_NEXT(s->matched_entry, entry);
2106 entry = TAILQ_PREV(s->matched_entry,
2107 commit_queue_head, entry);
2109 if (view->searching == TOG_SEARCH_FORWARD)
2110 entry = TAILQ_FIRST(&s->commits.head);
2112 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2118 if (entry == NULL) {
2119 if (s->thread_args.log_complete ||
2120 view->searching == TOG_SEARCH_BACKWARD) {
2121 view->search_next_done =
2122 (s->matched_entry == NULL ?
2123 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2124 s->search_entry = NULL;
2128 * Poke the log thread for more commits and return,
2129 * allowing the main loop to make progress. Search
2130 * will resume at s->search_entry once we come back.
2132 s->thread_args.commits_needed++;
2133 return trigger_log_thread(view, 0,
2134 &s->thread_args.commits_needed,
2135 &s->thread_args.log_complete,
2136 &s->thread_args.need_commits,
2137 &s->thread_args.commit_loaded);
2140 err = match_commit(&have_match, entry->id, entry->commit,
2145 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2146 s->matched_entry = entry;
2150 s->search_entry = entry;
2151 if (view->searching == TOG_SEARCH_FORWARD)
2152 entry = TAILQ_NEXT(entry, entry);
2154 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2157 if (s->matched_entry) {
2158 int cur = s->selected_entry->idx;
2159 while (cur < s->matched_entry->idx) {
2160 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2165 while (cur > s->matched_entry->idx) {
2166 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2173 s->search_entry = NULL;
2178 static const struct got_error *
2179 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2180 struct got_reflist_head *refs, struct got_repository *repo,
2181 const char *head_ref_name, const char *in_repo_path,
2184 const struct got_error *err = NULL;
2185 struct tog_log_view_state *s = &view->state.log;
2186 struct got_repository *thread_repo = NULL;
2187 struct got_commit_graph *thread_graph = NULL;
2190 if (in_repo_path != s->in_repo_path) {
2191 free(s->in_repo_path);
2192 s->in_repo_path = strdup(in_repo_path);
2193 if (s->in_repo_path == NULL)
2194 return got_error_from_errno("strdup");
2197 /* The commit queue only contains commits being displayed. */
2198 TAILQ_INIT(&s->commits.head);
2199 s->commits.ncommits = 0;
2203 s->head_ref_name = head_ref_name;
2204 s->start_id = got_object_id_dup(start_id);
2205 if (s->start_id == NULL) {
2206 err = got_error_from_errno("got_object_id_dup");
2209 s->log_branches = log_branches;
2211 SIMPLEQ_INIT(&s->colors);
2212 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2213 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2214 get_color_value("TOG_COLOR_COMMIT"));
2217 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2218 get_color_value("TOG_COLOR_AUTHOR"));
2220 free_colors(&s->colors);
2223 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2224 get_color_value("TOG_COLOR_DATE"));
2226 free_colors(&s->colors);
2231 view->show = show_log_view;
2232 view->input = input_log_view;
2233 view->close = close_log_view;
2234 view->search_start = search_start_log_view;
2235 view->search_next = search_next_log_view;
2237 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2240 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2244 err = got_commit_graph_iter_start(thread_graph,
2245 s->start_id, s->repo, NULL, NULL);
2249 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2251 err = got_error_set_errno(errcode, "pthread_cond_init");
2254 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2256 err = got_error_set_errno(errcode, "pthread_cond_init");
2260 s->thread_args.commits_needed = view->nlines;
2261 s->thread_args.graph = thread_graph;
2262 s->thread_args.commits = &s->commits;
2263 s->thread_args.in_repo_path = s->in_repo_path;
2264 s->thread_args.start_id = s->start_id;
2265 s->thread_args.repo = thread_repo;
2266 s->thread_args.log_complete = 0;
2267 s->thread_args.quit = &s->quit;
2268 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2269 s->thread_args.selected_entry = &s->selected_entry;
2270 s->thread_args.searching = &view->searching;
2271 s->thread_args.search_next_done = &view->search_next_done;
2272 s->thread_args.regex = &view->regex;
2275 close_log_view(view);
2279 static const struct got_error *
2280 show_log_view(struct tog_view *view)
2282 struct tog_log_view_state *s = &view->state.log;
2284 if (s->thread == NULL) {
2285 int errcode = pthread_create(&s->thread, NULL, log_thread,
2288 return got_error_set_errno(errcode, "pthread_create");
2291 return draw_commits(view, &s->last_displayed_entry,
2292 &s->selected_entry, s->first_displayed_entry,
2293 &s->commits, s->selected, view->nlines, s->refs,
2294 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2297 static const struct got_error *
2298 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2299 struct tog_view **focus_view, struct tog_view *view, int ch)
2301 const struct got_error *err = NULL;
2302 struct tog_log_view_state *s = &view->state.log;
2303 char *parent_path, *in_repo_path = NULL;
2304 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2306 struct got_object_id *start_id;
2316 if (s->first_displayed_entry == NULL)
2318 if (s->selected > 0)
2321 scroll_up(view, &s->first_displayed_entry, 1,
2326 if (s->first_displayed_entry == NULL)
2328 if (TAILQ_FIRST(&s->commits.head) ==
2329 s->first_displayed_entry) {
2333 scroll_up(view, &s->first_displayed_entry,
2334 view->nlines - 1, &s->commits);
2340 if (s->first_displayed_entry == NULL)
2342 if (s->selected < MIN(view->nlines - 2,
2343 s->commits.ncommits - 1)) {
2347 err = scroll_down(view, &s->first_displayed_entry, 1,
2348 &s->last_displayed_entry, &s->commits,
2349 &s->thread_args.log_complete,
2350 &s->thread_args.commits_needed,
2351 &s->thread_args.need_commits,
2352 &s->thread_args.commit_loaded);
2356 struct commit_queue_entry *first;
2357 first = s->first_displayed_entry;
2360 err = scroll_down(view, &s->first_displayed_entry,
2361 view->nlines - 1, &s->last_displayed_entry,
2362 &s->commits, &s->thread_args.log_complete,
2363 &s->thread_args.commits_needed,
2364 &s->thread_args.need_commits,
2365 &s->thread_args.commit_loaded);
2368 if (first == s->first_displayed_entry &&
2369 s->selected < MIN(view->nlines - 2,
2370 s->commits.ncommits - 1)) {
2371 /* can't scroll further down */
2372 s->selected = MIN(view->nlines - 2,
2373 s->commits.ncommits - 1);
2379 if (s->selected > view->nlines - 2)
2380 s->selected = view->nlines - 2;
2381 if (s->selected > s->commits.ncommits - 1)
2382 s->selected = s->commits.ncommits - 1;
2387 if (s->selected_entry == NULL)
2389 if (view_is_parent_view(view))
2390 begin_x = view_split_begin_x(view->begin_x);
2391 err = open_diff_view_for_commit(&diff_view, begin_x,
2392 s->selected_entry->commit, s->selected_entry->id,
2393 view, s->refs, s->repo);
2396 if (view_is_parent_view(view)) {
2397 err = view_close_child(view);
2400 err = view_set_child(view, diff_view);
2402 view_close(diff_view);
2405 *focus_view = diff_view;
2406 view->child_focussed = 1;
2408 *new_view = diff_view;
2411 if (s->selected_entry == NULL)
2413 if (view_is_parent_view(view))
2414 begin_x = view_split_begin_x(view->begin_x);
2415 err = browse_commit_tree(&tree_view, begin_x,
2416 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2419 if (view_is_parent_view(view)) {
2420 err = view_close_child(view);
2423 err = view_set_child(view, tree_view);
2425 view_close(tree_view);
2428 *focus_view = tree_view;
2429 view->child_focussed = 1;
2431 *new_view = tree_view;
2434 if (strcmp(s->in_repo_path, "/") == 0)
2436 parent_path = dirname(s->in_repo_path);
2437 if (parent_path && strcmp(parent_path, ".") != 0) {
2438 err = stop_log_thread(s);
2441 lv = view_open(view->nlines, view->ncols,
2442 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2444 return got_error_from_errno(
2446 err = open_log_view(lv, s->start_id, s->refs,
2447 s->repo, s->head_ref_name, parent_path,
2451 if (view_is_parent_view(view))
2454 view_set_child(view->parent, lv);
2461 err = stop_log_thread(s);
2464 lv = view_open(view->nlines, view->ncols,
2465 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2467 return got_error_from_errno("view_open");
2468 err = got_repo_match_object_id(&start_id, NULL,
2469 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2470 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2475 in_repo_path = strdup(s->in_repo_path);
2476 if (in_repo_path == NULL) {
2479 return got_error_from_errno("strdup");
2481 got_ref_list_free(s->refs);
2482 err = got_ref_list(s->refs, s->repo, NULL,
2483 got_ref_cmp_by_name, NULL);
2489 err = open_log_view(lv, start_id, s->refs, s->repo,
2490 s->head_ref_name, in_repo_path, s->log_branches);
2500 s->log_branches = !s->log_branches;
2501 err = stop_log_thread(s);
2504 lv = view_open(view->nlines, view->ncols,
2505 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2507 return got_error_from_errno("view_open");
2508 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2509 s->head_ref_name, s->in_repo_path, s->log_branches);
2524 static const struct got_error *
2525 apply_unveil(const char *repo_path, const char *worktree_path)
2527 const struct got_error *error;
2530 if (unveil("gmon.out", "rwc") != 0)
2531 return got_error_from_errno2("unveil", "gmon.out");
2533 if (repo_path && unveil(repo_path, "r") != 0)
2534 return got_error_from_errno2("unveil", repo_path);
2536 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2537 return got_error_from_errno2("unveil", worktree_path);
2539 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2540 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2542 error = got_privsep_unveil_exec_helpers();
2546 if (unveil(NULL, NULL) != 0)
2547 return got_error_from_errno("unveil");
2557 halfdelay(1); /* Do fast refresh while initial view is loading. */
2560 intrflush(stdscr, FALSE);
2561 keypad(stdscr, TRUE);
2563 if (getenv("TOG_COLORS") != NULL) {
2565 use_default_colors();
2567 signal(SIGWINCH, tog_sigwinch);
2568 signal(SIGPIPE, tog_sigpipe);
2569 signal(SIGCONT, tog_sigcont);
2572 static const struct got_error *
2573 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2574 struct got_repository *repo, struct got_worktree *worktree)
2576 const struct got_error *err = NULL;
2579 *in_repo_path = strdup("/");
2580 if (*in_repo_path == NULL)
2581 return got_error_from_errno("strdup");
2586 const char *prefix = got_worktree_get_path_prefix(worktree);
2589 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2593 if (asprintf(&p, "%s%s%s", prefix,
2594 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2595 err = got_error_from_errno("asprintf");
2599 err = got_repo_map_path(in_repo_path, repo, p, 0);
2603 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2608 static const struct got_error *
2609 cmd_log(int argc, char *argv[])
2611 const struct got_error *error;
2612 struct got_repository *repo = NULL;
2613 struct got_worktree *worktree = NULL;
2614 struct got_reflist_head refs;
2615 struct got_object_id *start_id = NULL;
2616 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2617 char *start_commit = NULL, *head_ref_name = NULL;
2618 int ch, log_branches = 0;
2619 struct tog_view *view;
2621 SIMPLEQ_INIT(&refs);
2624 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2629 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2635 start_commit = optarg;
2638 repo_path = realpath(optarg, NULL);
2639 if (repo_path == NULL)
2640 return got_error_from_errno2("realpath",
2655 cwd = getcwd(NULL, 0);
2657 return got_error_from_errno("getcwd");
2659 error = got_worktree_open(&worktree, cwd);
2660 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2663 if (repo_path == NULL) {
2666 strdup(got_worktree_get_repo_path(worktree));
2668 repo_path = strdup(cwd);
2670 if (repo_path == NULL) {
2671 error = got_error_from_errno("strdup");
2675 error = got_repo_open(&repo, repo_path, NULL);
2679 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2686 error = apply_unveil(got_repo_get_path(repo),
2687 worktree ? got_worktree_get_root_path(worktree) : NULL);
2691 if (start_commit == NULL)
2692 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2693 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2694 GOT_OBJ_TYPE_COMMIT, 1, repo);
2696 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2697 GOT_OBJ_TYPE_COMMIT, 1, repo);
2701 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2705 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2707 error = got_error_from_errno("view_open");
2711 head_ref_name = strdup(
2712 got_worktree_get_head_ref_name(worktree));
2713 if (head_ref_name == NULL) {
2714 error = got_error_from_errno("strdup");
2718 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2719 in_repo_path, log_branches);
2723 /* Release work tree lock. */
2724 got_worktree_close(worktree);
2727 error = view_loop(view);
2733 free(head_ref_name);
2735 got_repo_close(repo);
2737 got_worktree_close(worktree);
2738 got_ref_list_free(&refs);
2746 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2752 parse_next_line(FILE *f, size_t *len)
2757 const char delim[3] = { '\0', '\0', '\0'};
2759 line = fparseln(f, &linelen, &lineno, delim, 0);
2766 match_line(const char *line, regex_t *regex)
2768 regmatch_t regmatch;
2770 return regexec(regex, line, 1, ®match, 0) == 0;
2774 match_color(struct tog_colors *colors, const char *line)
2776 struct tog_color *tc = NULL;
2778 SIMPLEQ_FOREACH(tc, colors, entry) {
2779 if (match_line(line, &tc->regex))
2786 static const struct got_error *
2787 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2788 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2789 char *header, struct tog_colors *colors)
2791 const struct got_error *err;
2792 int lineno = 0, nprinted = 0;
2794 struct tog_color *tc;
2800 werase(view->window);
2803 err = format_line(&wline, &width, header, view->ncols, 0);
2808 if (view_needs_focus_indication(view))
2809 wstandout(view->window);
2810 waddwstr(view->window, wline);
2811 if (view_needs_focus_indication(view))
2812 wstandend(view->window);
2813 if (width <= view->ncols - 1)
2814 waddch(view->window, '\n');
2822 while (nprinted < max_lines) {
2823 line = parse_next_line(f, &len);
2828 if (++lineno < *first_displayed_line) {
2833 err = format_line(&wline, &width, line, view->ncols, 0);
2839 tc = match_color(colors, line);
2841 wattr_on(view->window,
2842 COLOR_PAIR(tc->colorpair), NULL);
2843 waddwstr(view->window, wline);
2845 wattr_off(view->window,
2846 COLOR_PAIR(tc->colorpair), NULL);
2847 if (width <= view->ncols - 1)
2848 waddch(view->window, '\n');
2849 if (++nprinted == 1)
2850 *first_displayed_line = lineno;
2855 *last_displayed_line = lineno;
2860 while (nprinted < view->nlines) {
2861 waddch(view->window, '\n');
2865 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2870 wstandout(view->window);
2871 waddwstr(view->window, wline);
2872 wstandend(view->window);
2879 get_datestr(time_t *time, char *datebuf)
2881 struct tm mytm, *tm;
2884 tm = gmtime_r(time, &mytm);
2887 s = asctime_r(tm, datebuf);
2890 p = strchr(s, '\n');
2896 static const struct got_error *
2897 write_commit_info(struct got_object_id *commit_id,
2898 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2900 const struct got_error *err = NULL;
2901 char datebuf[26], *datestr;
2902 struct got_commit_object *commit;
2903 char *id_str = NULL, *logmsg = NULL;
2904 time_t committer_time;
2905 const char *author, *committer;
2906 char *refs_str = NULL;
2909 err = build_refs_str(&refs_str, refs, commit_id, repo);
2914 err = got_object_open_as_commit(&commit, repo, commit_id);
2918 err = got_object_id_str(&id_str, commit_id);
2920 err = got_error_from_errno("got_object_id_str");
2924 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2925 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2926 err = got_error_from_errno("fprintf");
2929 if (fprintf(outfile, "from: %s\n",
2930 got_object_commit_get_author(commit)) < 0) {
2931 err = got_error_from_errno("fprintf");
2934 committer_time = got_object_commit_get_committer_time(commit);
2935 datestr = get_datestr(&committer_time, datebuf);
2936 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2937 err = got_error_from_errno("fprintf");
2940 author = got_object_commit_get_author(commit);
2941 committer = got_object_commit_get_committer(commit);
2942 if (strcmp(author, committer) != 0 &&
2943 fprintf(outfile, "via: %s\n", committer) < 0) {
2944 err = got_error_from_errno("fprintf");
2947 err = got_object_commit_get_logmsg(&logmsg, commit);
2950 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2951 err = got_error_from_errno("fprintf");
2958 got_object_commit_close(commit);
2962 const struct got_error *
2963 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2969 size_t noffsets = 0;
2973 *line_offsets = NULL;
2979 if (fseek(infile, 0, SEEK_END) == -1)
2980 return got_error_from_errno("fseek");
2981 len = ftell(infile) + 1;
2983 return got_error_from_errno("ftell");
2984 if (fseek(infile, 0, SEEK_SET) == -1)
2985 return got_error_from_errno("fseek");
2989 if ((buf = calloc(len, sizeof(char *))) == NULL)
2990 return got_error_from_errno("calloc");
2992 fread(buf, 1, len, infile);
2994 return got_error_from_errno("fread");
2997 if (line_offsets && nlines) {
2998 if (*line_offsets == NULL) {
2999 /* Have some data but perhaps no '\n'. */
3002 *line_offsets = calloc(1, sizeof(**line_offsets));
3003 if (*line_offsets == NULL)
3004 return got_error_from_errno("calloc");
3005 /* Skip forward over end of first line. */
3012 /* Scan '\n' offsets in remaining chunk of data. */
3014 if (buf[i] != '\n') {
3019 if (noffsets < *nlines) {
3020 off_t *o = recallocarray(*line_offsets,
3022 sizeof(**line_offsets));
3024 free(*line_offsets);
3025 *line_offsets = NULL;
3026 return got_error_from_errno(
3033 (*line_offsets)[*nlines - 1] = off;
3038 if (fflush(infile) != 0)
3039 return got_error_from_errno("fflush");
3048 static const struct got_error *
3049 create_diff(struct tog_diff_view_state *s)
3051 const struct got_error *err = NULL;
3057 err = got_error_from_errno("got_opentemp");
3060 if (s->f && fclose(s->f) != 0) {
3061 err = got_error_from_errno("fclose");
3067 err = got_object_get_type(&obj_type, s->repo, s->id1);
3069 err = got_object_get_type(&obj_type, s->repo, s->id2);
3074 case GOT_OBJ_TYPE_BLOB:
3075 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3076 s->diff_context, 0, s->repo, s->f);
3078 case GOT_OBJ_TYPE_TREE:
3079 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3080 s->diff_context, 0, s->repo, s->f);
3082 case GOT_OBJ_TYPE_COMMIT: {
3083 const struct got_object_id_queue *parent_ids;
3084 struct got_object_qid *pid;
3085 struct got_commit_object *commit2;
3087 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3090 /* Show commit info if we're diffing to a parent/root commit. */
3091 if (s->id1 == NULL) {
3092 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3096 parent_ids = got_object_commit_get_parent_ids(commit2);
3097 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3098 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3099 err = write_commit_info(s->id2, s->refs,
3107 got_object_commit_close(commit2);
3109 err = got_diff_objects_as_commits(s->id1, s->id2,
3110 s->diff_context, 0, s->repo, s->f);
3114 err = got_error(GOT_ERR_OBJ_TYPE);
3119 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3122 if (s->f && fflush(s->f) != 0 && err == NULL)
3123 err = got_error_from_errno("fflush");
3128 diff_view_indicate_progress(struct tog_view *view)
3130 mvwaddstr(view->window, 0, 0, "diffing...");
3135 static const struct got_error *
3136 search_start_diff_view(struct tog_view *view)
3138 struct tog_diff_view_state *s = &view->state.diff;
3140 s->matched_line = 0;
3144 static const struct got_error *
3145 search_next_diff_view(struct tog_view *view)
3147 struct tog_diff_view_state *s = &view->state.diff;
3150 if (!view->searching) {
3151 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3155 if (s->matched_line) {
3156 if (view->searching == TOG_SEARCH_FORWARD)
3157 lineno = s->matched_line + 1;
3159 lineno = s->matched_line - 1;
3161 if (view->searching == TOG_SEARCH_FORWARD)
3172 if (lineno <= 0 || lineno > s->nlines) {
3173 if (s->matched_line == 0) {
3174 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3179 if (view->searching == TOG_SEARCH_FORWARD)
3185 offset = s->line_offsets[lineno - 1];
3186 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3188 return got_error_from_errno("fseeko");
3191 line = parse_next_line(s->f, &len);
3192 if (line && match_line(line, &view->regex)) {
3193 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3194 s->matched_line = lineno;
3199 if (view->searching == TOG_SEARCH_FORWARD)
3205 if (s->matched_line) {
3206 s->first_displayed_line = s->matched_line;
3207 s->selected_line = 1;
3213 static const struct got_error *
3214 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3215 struct got_object_id *id2, struct tog_view *log_view,
3216 struct got_reflist_head *refs, struct got_repository *repo)
3218 const struct got_error *err;
3219 struct tog_diff_view_state *s = &view->state.diff;
3221 if (id1 != NULL && id2 != NULL) {
3223 err = got_object_get_type(&type1, repo, id1);
3226 err = got_object_get_type(&type2, repo, id2);
3231 return got_error(GOT_ERR_OBJ_TYPE);
3233 s->first_displayed_line = 1;
3234 s->last_displayed_line = view->nlines;
3235 s->selected_line = 1;
3242 s->id1 = got_object_id_dup(id1);
3244 return got_error_from_errno("got_object_id_dup");
3248 s->id2 = got_object_id_dup(id2);
3249 if (s->id2 == NULL) {
3252 return got_error_from_errno("got_object_id_dup");
3255 s->first_displayed_line = 1;
3256 s->last_displayed_line = view->nlines;
3257 s->diff_context = 3;
3258 s->log_view = log_view;
3262 SIMPLEQ_INIT(&s->colors);
3263 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3264 err = add_color(&s->colors,
3265 "^-", TOG_COLOR_DIFF_MINUS,
3266 get_color_value("TOG_COLOR_DIFF_MINUS"));
3269 err = add_color(&s->colors, "^\\+",
3270 TOG_COLOR_DIFF_PLUS,
3271 get_color_value("TOG_COLOR_DIFF_PLUS"));
3273 free_colors(&s->colors);
3276 err = add_color(&s->colors,
3277 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3278 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3280 free_colors(&s->colors);
3284 err = add_color(&s->colors,
3285 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3286 get_color_value("TOG_COLOR_DIFF_META"));
3288 free_colors(&s->colors);
3292 err = add_color(&s->colors,
3293 "^(from|via): ", TOG_COLOR_AUTHOR,
3294 get_color_value("TOG_COLOR_AUTHOR"));
3296 free_colors(&s->colors);
3300 err = add_color(&s->colors,
3301 "^date: ", TOG_COLOR_DATE,
3302 get_color_value("TOG_COLOR_DATE"));
3304 free_colors(&s->colors);
3309 if (log_view && view_is_splitscreen(view))
3310 show_log_view(log_view); /* draw vborder */
3311 diff_view_indicate_progress(view);
3313 err = create_diff(s);
3322 view->show = show_diff_view;
3323 view->input = input_diff_view;
3324 view->close = close_diff_view;
3325 view->search_start = search_start_diff_view;
3326 view->search_next = search_next_diff_view;
3331 static const struct got_error *
3332 close_diff_view(struct tog_view *view)
3334 const struct got_error *err = NULL;
3335 struct tog_diff_view_state *s = &view->state.diff;
3341 if (s->f && fclose(s->f) == EOF)
3342 err = got_error_from_errno("fclose");
3343 free_colors(&s->colors);
3344 free(s->line_offsets);
3348 static const struct got_error *
3349 show_diff_view(struct tog_view *view)
3351 const struct got_error *err;
3352 struct tog_diff_view_state *s = &view->state.diff;
3353 char *id_str1 = NULL, *id_str2, *header;
3356 err = got_object_id_str(&id_str1, s->id1);
3360 err = got_object_id_str(&id_str2, s->id2);
3364 if (asprintf(&header, "diff %s %s",
3365 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3366 err = got_error_from_errno("asprintf");
3374 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3375 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3376 header, &s->colors);
3379 static const struct got_error *
3380 set_selected_commit(struct tog_diff_view_state *s,
3381 struct commit_queue_entry *entry)
3383 const struct got_error *err;
3384 const struct got_object_id_queue *parent_ids;
3385 struct got_commit_object *selected_commit;
3386 struct got_object_qid *pid;
3389 s->id2 = got_object_id_dup(entry->id);
3391 return got_error_from_errno("got_object_id_dup");
3393 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3396 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3398 pid = SIMPLEQ_FIRST(parent_ids);
3399 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3400 got_object_commit_close(selected_commit);
3404 static const struct got_error *
3405 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3406 struct tog_view **focus_view, struct tog_view *view, int ch)
3408 const struct got_error *err = NULL;
3409 struct tog_diff_view_state *s = &view->state.diff;
3410 struct tog_log_view_state *ls;
3411 struct commit_queue_entry *entry;
3417 if (s->first_displayed_line > 1)
3418 s->first_displayed_line--;
3422 if (s->first_displayed_line == 1)
3425 while (i++ < view->nlines - 1 &&
3426 s->first_displayed_line > 1)
3427 s->first_displayed_line--;
3432 s->first_displayed_line++;
3440 while (!s->eof && i++ < view->nlines - 1) {
3442 line = parse_next_line(s->f, NULL);
3443 s->first_displayed_line++;
3449 if (s->diff_context > 0) {
3451 diff_view_indicate_progress(view);
3452 err = create_diff(s);
3456 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3458 diff_view_indicate_progress(view);
3459 err = create_diff(s);
3464 if (s->log_view == NULL)
3466 ls = &s->log_view->state.log;
3467 entry = TAILQ_PREV(ls->selected_entry,
3468 commit_queue_head, entry);
3472 err = input_log_view(NULL, NULL, NULL, s->log_view,
3477 err = set_selected_commit(s, entry);
3481 s->first_displayed_line = 1;
3482 s->last_displayed_line = view->nlines;
3484 diff_view_indicate_progress(view);
3485 err = create_diff(s);
3489 if (s->log_view == NULL)
3491 ls = &s->log_view->state.log;
3493 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3494 ls->thread_args.commits_needed++;
3495 err = trigger_log_thread(s->log_view, 1,
3496 &ls->thread_args.commits_needed,
3497 &ls->thread_args.log_complete,
3498 &ls->thread_args.need_commits,
3499 &ls->thread_args.commit_loaded);
3503 err = input_log_view(NULL, NULL, NULL, s->log_view,
3508 entry = TAILQ_NEXT(ls->selected_entry, entry);
3512 err = set_selected_commit(s, entry);
3516 s->first_displayed_line = 1;
3517 s->last_displayed_line = view->nlines;
3519 diff_view_indicate_progress(view);
3520 err = create_diff(s);
3529 static const struct got_error *
3530 cmd_diff(int argc, char *argv[])
3532 const struct got_error *error = NULL;
3533 struct got_repository *repo = NULL;
3534 struct got_worktree *worktree = NULL;
3535 struct got_reflist_head refs;
3536 struct got_object_id *id1 = NULL, *id2 = NULL;
3537 char *repo_path = NULL, *cwd = NULL;
3538 char *id_str1 = NULL, *id_str2 = NULL;
3540 struct tog_view *view;
3542 SIMPLEQ_INIT(&refs);
3545 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3550 while ((ch = getopt(argc, argv, "r:")) != -1) {
3553 repo_path = realpath(optarg, NULL);
3554 if (repo_path == NULL)
3555 return got_error_from_errno2("realpath",
3568 usage_diff(); /* TODO show local worktree changes */
3569 } else if (argc == 2) {
3575 cwd = getcwd(NULL, 0);
3577 return got_error_from_errno("getcwd");
3579 error = got_worktree_open(&worktree, cwd);
3580 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3583 if (repo_path == NULL) {
3586 strdup(got_worktree_get_repo_path(worktree));
3588 repo_path = strdup(cwd);
3590 if (repo_path == NULL) {
3591 error = got_error_from_errno("strdup");
3595 error = got_repo_open(&repo, repo_path, NULL);
3601 error = apply_unveil(got_repo_get_path(repo), NULL);
3605 error = got_repo_match_object_id_prefix(&id1, id_str1,
3606 GOT_OBJ_TYPE_ANY, repo);
3610 error = got_repo_match_object_id_prefix(&id2, id_str2,
3611 GOT_OBJ_TYPE_ANY, repo);
3615 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3619 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3621 error = got_error_from_errno("view_open");
3624 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3627 error = view_loop(view);
3632 got_repo_close(repo);
3634 got_worktree_close(worktree);
3635 got_ref_list_free(&refs);
3643 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3648 struct tog_blame_line {
3650 struct got_object_id *id;
3653 static const struct got_error *
3654 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3655 const char *path, struct tog_blame_line *lines, int nlines,
3656 int blame_complete, int selected_line, int *first_displayed_line,
3657 int *last_displayed_line, int *eof, int max_lines,
3658 struct tog_colors *colors)
3660 const struct got_error *err;
3661 int lineno = 0, nprinted = 0;
3666 struct tog_blame_line *blame_line;
3667 struct got_object_id *prev_id = NULL;
3669 struct tog_color *tc;
3671 err = got_object_id_str(&id_str, id);
3676 werase(view->window);
3678 if (asprintf(&line, "commit %s", id_str) == -1) {
3679 err = got_error_from_errno("asprintf");
3684 err = format_line(&wline, &width, line, view->ncols, 0);
3689 if (view_needs_focus_indication(view))
3690 wstandout(view->window);
3691 tc = get_color(colors, TOG_COLOR_COMMIT);
3693 wattr_on(view->window,
3694 COLOR_PAIR(tc->colorpair), NULL);
3695 waddwstr(view->window, wline);
3697 wattr_off(view->window,
3698 COLOR_PAIR(tc->colorpair), NULL);
3699 if (view_needs_focus_indication(view))
3700 wstandend(view->window);
3703 if (width < view->ncols - 1)
3704 waddch(view->window, '\n');
3706 if (asprintf(&line, "[%d/%d] %s%s",
3707 *first_displayed_line - 1 + selected_line, nlines,
3708 blame_complete ? "" : "annotating... ", path) == -1) {
3710 return got_error_from_errno("asprintf");
3713 err = format_line(&wline, &width, line, view->ncols, 0);
3718 waddwstr(view->window, wline);
3721 if (width < view->ncols - 1)
3722 waddch(view->window, '\n');
3725 while (nprinted < max_lines - 2) {
3726 line = parse_next_line(f, &len);
3731 if (++lineno < *first_displayed_line) {
3736 if (view->ncols <= 9) {
3738 wline = wcsdup(L"");
3740 err = got_error_from_errno("wcsdup");
3742 err = format_line(&wline, &width, line,
3743 view->ncols - 9, 9);
3751 if (view->focussed && nprinted == selected_line - 1)
3752 wstandout(view->window);
3755 blame_line = &lines[lineno - 1];
3756 if (blame_line->annotated && prev_id &&
3757 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3759 nprinted == selected_line - 1)) {
3760 waddstr(view->window, " ");
3761 } else if (blame_line->annotated) {
3763 err = got_object_id_str(&id_str, blame_line->id);
3769 tc = get_color(colors, TOG_COLOR_COMMIT);
3771 wattr_on(view->window,
3772 COLOR_PAIR(tc->colorpair), NULL);
3773 wprintw(view->window, "%.8s", id_str);
3775 wattr_off(view->window,
3776 COLOR_PAIR(tc->colorpair), NULL);
3778 prev_id = blame_line->id;
3780 waddstr(view->window, "........");
3784 waddstr(view->window, "........");
3788 if (view->focussed && nprinted == selected_line - 1)
3789 wstandend(view->window);
3790 waddstr(view->window, " ");
3792 waddwstr(view->window, wline);
3793 if (width <= view->ncols - 1)
3794 waddch(view->window, '\n');
3795 if (++nprinted == 1)
3796 *first_displayed_line = lineno;
3801 *last_displayed_line = lineno;
3808 static const struct got_error *
3809 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3811 const struct got_error *err = NULL;
3812 struct tog_blame_cb_args *a = arg;
3813 struct tog_blame_line *line;
3816 if (nlines != a->nlines ||
3817 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3818 return got_error(GOT_ERR_RANGE);
3820 errcode = pthread_mutex_lock(&tog_mutex);
3822 return got_error_set_errno(errcode, "pthread_mutex_lock");
3824 if (*a->quit) { /* user has quit the blame view */
3825 err = got_error(GOT_ERR_ITER_COMPLETED);
3830 goto done; /* no change in this commit */
3832 line = &a->lines[lineno - 1];
3833 if (line->annotated)
3836 line->id = got_object_id_dup(id);
3837 if (line->id == NULL) {
3838 err = got_error_from_errno("got_object_id_dup");
3841 line->annotated = 1;
3843 errcode = pthread_mutex_unlock(&tog_mutex);
3845 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3850 blame_thread(void *arg)
3852 const struct got_error *err;
3853 struct tog_blame_thread_args *ta = arg;
3854 struct tog_blame_cb_args *a = ta->cb_args;
3857 err = block_signals_used_by_main_thread();
3861 err = got_blame(ta->path, a->commit_id, ta->repo,
3862 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3863 if (err && err->code == GOT_ERR_CANCELLED)
3866 errcode = pthread_mutex_lock(&tog_mutex);
3868 return (void *)got_error_set_errno(errcode,
3869 "pthread_mutex_lock");
3871 got_repo_close(ta->repo);
3875 errcode = pthread_mutex_unlock(&tog_mutex);
3876 if (errcode && err == NULL)
3877 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3882 static struct got_object_id *
3883 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3884 int first_displayed_line, int selected_line)
3886 struct tog_blame_line *line;
3891 line = &lines[first_displayed_line - 1 + selected_line - 1];
3892 if (!line->annotated)
3898 static const struct got_error *
3899 stop_blame(struct tog_blame *blame)
3901 const struct got_error *err = NULL;
3904 if (blame->thread) {
3906 errcode = pthread_mutex_unlock(&tog_mutex);
3908 return got_error_set_errno(errcode,
3909 "pthread_mutex_unlock");
3910 errcode = pthread_join(blame->thread, (void **)&err);
3912 return got_error_set_errno(errcode, "pthread_join");
3913 errcode = pthread_mutex_lock(&tog_mutex);
3915 return got_error_set_errno(errcode,
3916 "pthread_mutex_lock");
3917 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3919 blame->thread = NULL;
3921 if (blame->thread_args.repo) {
3922 got_repo_close(blame->thread_args.repo);
3923 blame->thread_args.repo = NULL;
3926 if (fclose(blame->f) != 0 && err == NULL)
3927 err = got_error_from_errno("fclose");
3931 for (i = 0; i < blame->nlines; i++)
3932 free(blame->lines[i].id);
3934 blame->lines = NULL;
3936 free(blame->cb_args.commit_id);
3937 blame->cb_args.commit_id = NULL;
3942 static const struct got_error *
3943 cancel_blame_view(void *arg)
3945 const struct got_error *err = NULL;
3949 errcode = pthread_mutex_lock(&tog_mutex);
3951 return got_error_set_errno(errcode,
3952 "pthread_mutex_unlock");
3955 err = got_error(GOT_ERR_CANCELLED);
3957 errcode = pthread_mutex_unlock(&tog_mutex);
3959 return got_error_set_errno(errcode,
3960 "pthread_mutex_lock");
3965 static const struct got_error *
3966 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3967 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3968 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3969 struct got_repository *repo)
3971 const struct got_error *err = NULL;
3972 struct got_blob_object *blob = NULL;
3973 struct got_repository *thread_repo = NULL;
3974 struct got_object_id *obj_id = NULL;
3977 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3981 err = got_object_get_type(&obj_type, repo, obj_id);
3985 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3986 err = got_error(GOT_ERR_OBJ_TYPE);
3990 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3993 blame->f = got_opentemp();
3994 if (blame->f == NULL) {
3995 err = got_error_from_errno("got_opentemp");
3998 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3999 &blame->line_offsets, blame->f, blob);
4000 if (err || blame->nlines == 0)
4003 /* Don't include \n at EOF in the blame line count. */
4004 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4007 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4008 if (blame->lines == NULL) {
4009 err = got_error_from_errno("calloc");
4013 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4017 blame->cb_args.view = view;
4018 blame->cb_args.lines = blame->lines;
4019 blame->cb_args.nlines = blame->nlines;
4020 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4021 if (blame->cb_args.commit_id == NULL) {
4022 err = got_error_from_errno("got_object_id_dup");
4025 blame->cb_args.quit = done;
4027 blame->thread_args.path = path;
4028 blame->thread_args.repo = thread_repo;
4029 blame->thread_args.cb_args = &blame->cb_args;
4030 blame->thread_args.complete = blame_complete;
4031 blame->thread_args.cancel_cb = cancel_blame_view;
4032 blame->thread_args.cancel_arg = done;
4033 *blame_complete = 0;
4037 got_object_blob_close(blob);
4044 static const struct got_error *
4045 open_blame_view(struct tog_view *view, char *path,
4046 struct got_object_id *commit_id, struct got_reflist_head *refs,
4047 struct got_repository *repo)
4049 const struct got_error *err = NULL;
4050 struct tog_blame_view_state *s = &view->state.blame;
4052 SIMPLEQ_INIT(&s->blamed_commits);
4054 s->path = strdup(path);
4055 if (s->path == NULL)
4056 return got_error_from_errno("strdup");
4058 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4064 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4065 s->first_displayed_line = 1;
4066 s->last_displayed_line = view->nlines;
4067 s->selected_line = 1;
4068 s->blame_complete = 0;
4071 s->commit_id = commit_id;
4072 memset(&s->blame, 0, sizeof(s->blame));
4074 SIMPLEQ_INIT(&s->colors);
4075 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4076 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4077 get_color_value("TOG_COLOR_COMMIT"));
4082 view->show = show_blame_view;
4083 view->input = input_blame_view;
4084 view->close = close_blame_view;
4085 view->search_start = search_start_blame_view;
4086 view->search_next = search_next_blame_view;
4088 return run_blame(&s->blame, view, &s->blame_complete,
4089 &s->first_displayed_line, &s->last_displayed_line,
4090 &s->selected_line, &s->done, &s->eof, s->path,
4091 s->blamed_commit->id, s->repo);
4094 static const struct got_error *
4095 close_blame_view(struct tog_view *view)
4097 const struct got_error *err = NULL;
4098 struct tog_blame_view_state *s = &view->state.blame;
4100 if (s->blame.thread)
4101 err = stop_blame(&s->blame);
4103 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4104 struct got_object_qid *blamed_commit;
4105 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4106 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4107 got_object_qid_free(blamed_commit);
4111 free_colors(&s->colors);
4116 static const struct got_error *
4117 search_start_blame_view(struct tog_view *view)
4119 struct tog_blame_view_state *s = &view->state.blame;
4121 s->matched_line = 0;
4125 static const struct got_error *
4126 search_next_blame_view(struct tog_view *view)
4128 struct tog_blame_view_state *s = &view->state.blame;
4131 if (!view->searching) {
4132 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4136 if (s->matched_line) {
4137 if (view->searching == TOG_SEARCH_FORWARD)
4138 lineno = s->matched_line + 1;
4140 lineno = s->matched_line - 1;
4142 if (view->searching == TOG_SEARCH_FORWARD)
4145 lineno = s->blame.nlines;
4153 if (lineno <= 0 || lineno > s->blame.nlines) {
4154 if (s->matched_line == 0) {
4155 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4160 if (view->searching == TOG_SEARCH_FORWARD)
4163 lineno = s->blame.nlines;
4166 offset = s->blame.line_offsets[lineno - 1];
4167 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4169 return got_error_from_errno("fseeko");
4172 line = parse_next_line(s->blame.f, &len);
4173 if (line && match_line(line, &view->regex)) {
4174 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4175 s->matched_line = lineno;
4180 if (view->searching == TOG_SEARCH_FORWARD)
4186 if (s->matched_line) {
4187 s->first_displayed_line = s->matched_line;
4188 s->selected_line = 1;
4194 static const struct got_error *
4195 show_blame_view(struct tog_view *view)
4197 const struct got_error *err = NULL;
4198 struct tog_blame_view_state *s = &view->state.blame;
4201 if (s->blame.thread == NULL) {
4202 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4203 &s->blame.thread_args);
4205 return got_error_set_errno(errcode, "pthread_create");
4207 halfdelay(1); /* fast refresh while annotating */
4210 if (s->blame_complete)
4211 halfdelay(10); /* disable fast refresh */
4213 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4214 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4215 s->selected_line, &s->first_displayed_line,
4216 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4222 static const struct got_error *
4223 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4224 struct tog_view **focus_view, struct tog_view *view, int ch)
4226 const struct got_error *err = NULL, *thread_err = NULL;
4227 struct tog_view *diff_view;
4228 struct tog_blame_view_state *s = &view->state.blame;
4237 if (s->selected_line > 1)
4239 else if (s->selected_line == 1 &&
4240 s->first_displayed_line > 1)
4241 s->first_displayed_line--;
4245 if (s->first_displayed_line == 1) {
4246 s->selected_line = 1;
4249 if (s->first_displayed_line > view->nlines - 2)
4250 s->first_displayed_line -=
4253 s->first_displayed_line = 1;
4257 if (s->selected_line < view->nlines - 2 &&
4258 s->first_displayed_line +
4259 s->selected_line <= s->blame.nlines)
4261 else if (s->last_displayed_line <
4263 s->first_displayed_line++;
4267 struct got_object_id *id = NULL;
4268 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4269 s->first_displayed_line, s->selected_line);
4273 struct got_commit_object *commit;
4274 struct got_object_qid *pid;
4275 struct got_object_id *blob_id = NULL;
4277 err = got_object_open_as_commit(&commit,
4281 pid = SIMPLEQ_FIRST(
4282 got_object_commit_get_parent_ids(commit));
4284 got_object_commit_close(commit);
4287 /* Check if path history ends here. */
4288 err = got_object_id_by_path(&blob_id, s->repo,
4291 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4293 got_object_commit_close(commit);
4296 err = got_object_get_type(&obj_type, s->repo,
4299 /* Can't blame non-blob type objects. */
4300 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4301 got_object_commit_close(commit);
4304 err = got_object_qid_alloc(&s->blamed_commit,
4306 got_object_commit_close(commit);
4308 if (got_object_id_cmp(id,
4309 s->blamed_commit->id) == 0)
4311 err = got_object_qid_alloc(&s->blamed_commit,
4317 thread_err = stop_blame(&s->blame);
4321 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4322 s->blamed_commit, entry);
4323 err = run_blame(&s->blame, view, &s->blame_complete,
4324 &s->first_displayed_line, &s->last_displayed_line,
4325 &s->selected_line, &s->done, &s->eof,
4326 s->path, s->blamed_commit->id, s->repo);
4332 struct got_object_qid *first;
4333 first = SIMPLEQ_FIRST(&s->blamed_commits);
4334 if (!got_object_id_cmp(first->id, s->commit_id))
4337 thread_err = stop_blame(&s->blame);
4341 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4342 got_object_qid_free(s->blamed_commit);
4344 SIMPLEQ_FIRST(&s->blamed_commits);
4345 err = run_blame(&s->blame, view, &s->blame_complete,
4346 &s->first_displayed_line, &s->last_displayed_line,
4347 &s->selected_line, &s->done, &s->eof, s->path,
4348 s->blamed_commit->id, s->repo);
4355 struct got_object_id *id = NULL;
4356 struct got_object_qid *pid;
4357 struct got_commit_object *commit = NULL;
4358 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4359 s->first_displayed_line, s->selected_line);
4362 err = got_object_open_as_commit(&commit, s->repo, id);
4365 pid = SIMPLEQ_FIRST(
4366 got_object_commit_get_parent_ids(commit));
4367 if (view_is_parent_view(view))
4368 begin_x = view_split_begin_x(view->begin_x);
4369 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4370 if (diff_view == NULL) {
4371 got_object_commit_close(commit);
4372 err = got_error_from_errno("view_open");
4375 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4376 id, NULL, s->refs, s->repo);
4377 got_object_commit_close(commit);
4379 view_close(diff_view);
4382 if (view_is_parent_view(view)) {
4383 err = view_close_child(view);
4386 err = view_set_child(view, diff_view);
4388 view_close(diff_view);
4391 *focus_view = diff_view;
4392 view->child_focussed = 1;
4394 *new_view = diff_view;
4402 if (s->last_displayed_line >= s->blame.nlines &&
4403 s->selected_line >= MIN(s->blame.nlines,
4404 view->nlines - 2)) {
4407 if (s->last_displayed_line >= s->blame.nlines &&
4408 s->selected_line < view->nlines - 2) {
4409 s->selected_line = MIN(s->blame.nlines,
4413 if (s->last_displayed_line + view->nlines - 2
4415 s->first_displayed_line +=
4418 s->first_displayed_line =
4423 if (s->selected_line > view->nlines - 2) {
4424 s->selected_line = MIN(s->blame.nlines,
4431 return thread_err ? thread_err : err;
4434 static const struct got_error *
4435 cmd_blame(int argc, char *argv[])
4437 const struct got_error *error;
4438 struct got_repository *repo = NULL;
4439 struct got_reflist_head refs;
4440 struct got_worktree *worktree = NULL;
4441 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4442 struct got_object_id *commit_id = NULL;
4443 char *commit_id_str = NULL;
4445 struct tog_view *view;
4447 SIMPLEQ_INIT(&refs);
4450 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4455 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4458 commit_id_str = optarg;
4461 repo_path = realpath(optarg, NULL);
4462 if (repo_path == NULL)
4463 return got_error_from_errno2("realpath",
4478 cwd = getcwd(NULL, 0);
4480 return got_error_from_errno("getcwd");
4482 error = got_worktree_open(&worktree, cwd);
4483 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4486 if (repo_path == NULL) {
4489 strdup(got_worktree_get_repo_path(worktree));
4491 repo_path = strdup(cwd);
4493 if (repo_path == NULL) {
4494 error = got_error_from_errno("strdup");
4498 error = got_repo_open(&repo, repo_path, NULL);
4502 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4509 error = apply_unveil(got_repo_get_path(repo), NULL);
4513 if (commit_id_str == NULL) {
4514 struct got_reference *head_ref;
4515 error = got_ref_open(&head_ref, repo, worktree ?
4516 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4519 error = got_ref_resolve(&commit_id, repo, head_ref);
4520 got_ref_close(head_ref);
4522 error = got_repo_match_object_id(&commit_id, NULL,
4523 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4528 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4532 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4534 error = got_error_from_errno("view_open");
4537 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4541 /* Release work tree lock. */
4542 got_worktree_close(worktree);
4545 error = view_loop(view);
4552 got_worktree_close(worktree);
4554 got_repo_close(repo);
4555 got_ref_list_free(&refs);
4559 static const struct got_error *
4560 draw_tree_entries(struct tog_view *view,
4561 struct got_tree_entry **first_displayed_entry,
4562 struct got_tree_entry **last_displayed_entry,
4563 struct got_tree_entry **selected_entry, int *ndisplayed,
4564 const char *label, int show_ids, const char *parent_path,
4565 struct got_tree_object *tree, int selected, int limit,
4566 int isroot, struct tog_colors *colors)
4568 const struct got_error *err = NULL;
4569 struct got_tree_entry *te;
4571 struct tog_color *tc;
4572 int width, n, i, nentries;
4576 werase(view->window);
4581 err = format_line(&wline, &width, label, view->ncols, 0);
4584 if (view_needs_focus_indication(view))
4585 wstandout(view->window);
4586 tc = get_color(colors, TOG_COLOR_COMMIT);
4588 wattr_on(view->window,
4589 COLOR_PAIR(tc->colorpair), NULL);
4590 waddwstr(view->window, wline);
4592 wattr_off(view->window,
4593 COLOR_PAIR(tc->colorpair), NULL);
4594 if (view_needs_focus_indication(view))
4595 wstandend(view->window);
4598 if (width < view->ncols - 1)
4599 waddch(view->window, '\n');
4602 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4605 waddwstr(view->window, wline);
4608 if (width < view->ncols - 1)
4609 waddch(view->window, '\n');
4612 waddch(view->window, '\n');
4616 if (*first_displayed_entry == NULL) {
4617 te = got_object_tree_get_first_entry(tree);
4618 if (selected == 0) {
4620 wstandout(view->window);
4621 *selected_entry = NULL;
4623 waddstr(view->window, " ..\n"); /* parent directory */
4624 if (selected == 0 && view->focussed)
4625 wstandend(view->window);
4632 te = *first_displayed_entry;
4635 nentries = got_object_tree_get_nentries(tree);
4636 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4637 char *line = NULL, *id_str = NULL;
4638 const char *modestr = "";
4641 te = got_object_tree_get_entry(tree, i);
4642 mode = got_tree_entry_get_mode(te);
4645 err = got_object_id_str(&id_str,
4646 got_tree_entry_get_id(te));
4648 return got_error_from_errno(
4649 "got_object_id_str");
4651 if (got_object_tree_entry_is_submodule(te))
4653 else if (S_ISLNK(mode))
4655 else if (S_ISDIR(mode))
4657 else if (mode & S_IXUSR)
4659 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4660 got_tree_entry_get_name(te), modestr) == -1) {
4662 return got_error_from_errno("asprintf");
4665 err = format_line(&wline, &width, line, view->ncols, 0);
4670 if (n == selected) {
4672 wstandout(view->window);
4673 *selected_entry = te;
4675 tc = match_color(colors, line);
4677 wattr_on(view->window,
4678 COLOR_PAIR(tc->colorpair), NULL);
4679 waddwstr(view->window, wline);
4681 wattr_off(view->window,
4682 COLOR_PAIR(tc->colorpair), NULL);
4683 if (width < view->ncols - 1)
4684 waddch(view->window, '\n');
4685 if (n == selected && view->focussed)
4686 wstandend(view->window);
4692 *last_displayed_entry = te;
4701 tree_scroll_up(struct tog_view *view,
4702 struct got_tree_entry **first_displayed_entry, int maxscroll,
4703 struct got_tree_object *tree, int isroot)
4705 struct got_tree_entry *te;
4708 if (*first_displayed_entry == NULL)
4711 te = got_object_tree_get_entry(tree, 0);
4712 if (*first_displayed_entry == te) {
4714 *first_displayed_entry = NULL;
4719 while (*first_displayed_entry && i < maxscroll) {
4720 *first_displayed_entry = got_tree_entry_get_prev(tree,
4721 *first_displayed_entry);
4724 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4725 *first_displayed_entry = NULL;
4729 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4730 struct got_tree_entry *last_displayed_entry,
4731 struct got_tree_object *tree)
4733 struct got_tree_entry *next, *last;
4736 if (*first_displayed_entry)
4737 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4739 next = got_object_tree_get_first_entry(tree);
4741 last = last_displayed_entry;
4742 while (next && last && n++ < maxscroll) {
4743 last = got_tree_entry_get_next(tree, last);
4745 *first_displayed_entry = next;
4746 next = got_tree_entry_get_next(tree, next);
4752 static const struct got_error *
4753 tree_entry_path(char **path, struct tog_parent_trees *parents,
4754 struct got_tree_entry *te)
4756 const struct got_error *err = NULL;
4757 struct tog_parent_tree *pt;
4758 size_t len = 2; /* for leading slash and NUL */
4760 TAILQ_FOREACH(pt, parents, entry)
4761 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4764 len += strlen(got_tree_entry_get_name(te));
4766 *path = calloc(1, len);
4768 return got_error_from_errno("calloc");
4771 pt = TAILQ_LAST(parents, tog_parent_trees);
4773 const char *name = got_tree_entry_get_name(pt->selected_entry);
4774 if (strlcat(*path, name, len) >= len) {
4775 err = got_error(GOT_ERR_NO_SPACE);
4778 if (strlcat(*path, "/", len) >= len) {
4779 err = got_error(GOT_ERR_NO_SPACE);
4782 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4785 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4786 err = got_error(GOT_ERR_NO_SPACE);
4798 static const struct got_error *
4799 blame_tree_entry(struct tog_view **new_view, int begin_x,
4800 struct got_tree_entry *te, struct tog_parent_trees *parents,
4801 struct got_object_id *commit_id, struct got_reflist_head *refs,
4802 struct got_repository *repo)
4804 const struct got_error *err = NULL;
4806 struct tog_view *blame_view;
4810 err = tree_entry_path(&path, parents, te);
4814 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4815 if (blame_view == NULL) {
4816 err = got_error_from_errno("view_open");
4820 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4822 if (err->code == GOT_ERR_CANCELLED)
4824 view_close(blame_view);
4826 *new_view = blame_view;
4832 static const struct got_error *
4833 log_tree_entry(struct tog_view **new_view, int begin_x,
4834 struct got_tree_entry *te, struct tog_parent_trees *parents,
4835 struct got_object_id *commit_id, struct got_reflist_head *refs,
4836 struct got_repository *repo)
4838 struct tog_view *log_view;
4839 const struct got_error *err = NULL;
4844 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4845 if (log_view == NULL)
4846 return got_error_from_errno("view_open");
4848 err = tree_entry_path(&path, parents, te);
4852 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4854 view_close(log_view);
4856 *new_view = log_view;
4861 static const struct got_error *
4862 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4863 struct got_object_id *commit_id, struct got_reflist_head *refs,
4864 struct got_repository *repo)
4866 const struct got_error *err = NULL;
4867 char *commit_id_str = NULL;
4868 struct tog_tree_view_state *s = &view->state.tree;
4870 TAILQ_INIT(&s->parents);
4872 err = got_object_id_str(&commit_id_str, commit_id);
4876 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4877 err = got_error_from_errno("asprintf");
4881 s->root = s->tree = root;
4882 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4883 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4884 s->commit_id = got_object_id_dup(commit_id);
4885 if (s->commit_id == NULL) {
4886 err = got_error_from_errno("got_object_id_dup");
4892 SIMPLEQ_INIT(&s->colors);
4894 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4895 err = add_color(&s->colors, "\\$$",
4896 TOG_COLOR_TREE_SUBMODULE,
4897 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4900 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4901 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4903 free_colors(&s->colors);
4906 err = add_color(&s->colors, "/$",
4907 TOG_COLOR_TREE_DIRECTORY,
4908 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4910 free_colors(&s->colors);
4914 err = add_color(&s->colors, "\\*$",
4915 TOG_COLOR_TREE_EXECUTABLE,
4916 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4918 free_colors(&s->colors);
4922 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4923 get_color_value("TOG_COLOR_COMMIT"));
4925 free_colors(&s->colors);
4930 view->show = show_tree_view;
4931 view->input = input_tree_view;
4932 view->close = close_tree_view;
4933 view->search_start = search_start_tree_view;
4934 view->search_next = search_next_tree_view;
4936 free(commit_id_str);
4938 free(s->tree_label);
4939 s->tree_label = NULL;
4944 static const struct got_error *
4945 close_tree_view(struct tog_view *view)
4947 struct tog_tree_view_state *s = &view->state.tree;
4949 free_colors(&s->colors);
4950 free(s->tree_label);
4951 s->tree_label = NULL;
4953 s->commit_id = NULL;
4954 while (!TAILQ_EMPTY(&s->parents)) {
4955 struct tog_parent_tree *parent;
4956 parent = TAILQ_FIRST(&s->parents);
4957 TAILQ_REMOVE(&s->parents, parent, entry);
4961 if (s->tree != s->root)
4962 got_object_tree_close(s->tree);
4963 got_object_tree_close(s->root);
4968 static const struct got_error *
4969 search_start_tree_view(struct tog_view *view)
4971 struct tog_tree_view_state *s = &view->state.tree;
4973 s->matched_entry = NULL;
4978 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4980 regmatch_t regmatch;
4982 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4986 static const struct got_error *
4987 search_next_tree_view(struct tog_view *view)
4989 struct tog_tree_view_state *s = &view->state.tree;
4990 struct got_tree_entry *te = NULL;
4992 if (!view->searching) {
4993 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4997 if (s->matched_entry) {
4998 if (view->searching == TOG_SEARCH_FORWARD) {
4999 if (s->selected_entry)
5000 te = got_tree_entry_get_next(s->tree,
5003 te = got_object_tree_get_first_entry(s->tree);
5005 if (s->selected_entry == NULL)
5006 te = got_object_tree_get_last_entry(s->tree);
5008 te = got_tree_entry_get_prev(s->tree,
5012 if (view->searching == TOG_SEARCH_FORWARD)
5013 te = got_object_tree_get_first_entry(s->tree);
5015 te = got_object_tree_get_last_entry(s->tree);
5020 if (s->matched_entry == NULL) {
5021 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5024 if (view->searching == TOG_SEARCH_FORWARD)
5025 te = got_object_tree_get_first_entry(s->tree);
5027 te = got_object_tree_get_last_entry(s->tree);
5030 if (match_tree_entry(te, &view->regex)) {
5031 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5032 s->matched_entry = te;
5036 if (view->searching == TOG_SEARCH_FORWARD)
5037 te = got_tree_entry_get_next(s->tree, te);
5039 te = got_tree_entry_get_prev(s->tree, te);
5042 if (s->matched_entry) {
5043 s->first_displayed_entry = s->matched_entry;
5050 static const struct got_error *
5051 show_tree_view(struct tog_view *view)
5053 const struct got_error *err = NULL;
5054 struct tog_tree_view_state *s = &view->state.tree;
5057 err = tree_entry_path(&parent_path, &s->parents, NULL);
5061 err = draw_tree_entries(view, &s->first_displayed_entry,
5062 &s->last_displayed_entry, &s->selected_entry,
5063 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5064 s->tree, s->selected, view->nlines, s->tree == s->root,
5072 static const struct got_error *
5073 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5074 struct tog_view **focus_view, struct tog_view *view, int ch)
5076 const struct got_error *err = NULL;
5077 struct tog_tree_view_state *s = &view->state.tree;
5078 struct tog_view *log_view;
5079 int begin_x = 0, nscrolled;
5083 s->show_ids = !s->show_ids;
5086 if (!s->selected_entry)
5088 if (view_is_parent_view(view))
5089 begin_x = view_split_begin_x(view->begin_x);
5090 err = log_tree_entry(&log_view, begin_x,
5091 s->selected_entry, &s->parents,
5092 s->commit_id, s->refs, s->repo);
5093 if (view_is_parent_view(view)) {
5094 err = view_close_child(view);
5097 err = view_set_child(view, log_view);
5099 view_close(log_view);
5102 *focus_view = log_view;
5103 view->child_focussed = 1;
5105 *new_view = log_view;
5109 if (s->selected > 0) {
5111 if (s->selected == 0)
5114 if (s->selected > 0)
5116 tree_scroll_up(view, &s->first_displayed_entry, 1,
5117 s->tree, s->tree == s->root);
5121 tree_scroll_up(view, &s->first_displayed_entry,
5122 MAX(0, view->nlines - 4 - s->selected), s->tree,
5123 s->tree == s->root);
5125 if (got_object_tree_get_first_entry(s->tree) ==
5126 s->first_displayed_entry && s->tree != s->root)
5127 s->first_displayed_entry = NULL;
5131 if (s->selected < s->ndisplayed - 1) {
5135 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5137 /* can't scroll any further */
5139 tree_scroll_down(&s->first_displayed_entry, 1,
5140 s->last_displayed_entry, s->tree);
5144 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5146 /* can't scroll any further; move cursor down */
5147 if (s->selected < s->ndisplayed - 1)
5148 s->selected = s->ndisplayed - 1;
5151 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5152 view->nlines, s->last_displayed_entry, s->tree);
5153 if (nscrolled < view->nlines) {
5155 struct got_tree_entry *te;
5156 te = s->first_displayed_entry;
5159 te = got_tree_entry_get_next(s->tree, te);
5161 s->selected = ndisplayed - 1;
5167 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5168 struct tog_parent_tree *parent;
5169 /* user selected '..' */
5170 if (s->tree == s->root)
5172 parent = TAILQ_FIRST(&s->parents);
5173 TAILQ_REMOVE(&s->parents, parent,
5175 got_object_tree_close(s->tree);
5176 s->tree = parent->tree;
5177 s->first_displayed_entry =
5178 parent->first_displayed_entry;
5180 parent->selected_entry;
5181 s->selected = parent->selected;
5183 } else if (S_ISDIR(got_tree_entry_get_mode(
5184 s->selected_entry))) {
5185 struct got_tree_object *subtree;
5186 err = got_object_open_as_tree(&subtree, s->repo,
5187 got_tree_entry_get_id(s->selected_entry));
5190 err = tree_view_visit_subtree(subtree, s);
5192 got_object_tree_close(subtree);
5195 } else if (S_ISREG(got_tree_entry_get_mode(
5196 s->selected_entry))) {
5197 struct tog_view *blame_view;
5198 int begin_x = view_is_parent_view(view) ?
5199 view_split_begin_x(view->begin_x) : 0;
5201 err = blame_tree_entry(&blame_view, begin_x,
5202 s->selected_entry, &s->parents,
5203 s->commit_id, s->refs, s->repo);
5206 if (view_is_parent_view(view)) {
5207 err = view_close_child(view);
5210 err = view_set_child(view, blame_view);
5212 view_close(blame_view);
5215 *focus_view = blame_view;
5216 view->child_focussed = 1;
5218 *new_view = blame_view;
5222 if (s->selected > view->nlines)
5223 s->selected = s->ndisplayed - 1;
5236 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5241 static const struct got_error *
5242 cmd_tree(int argc, char *argv[])
5244 const struct got_error *error;
5245 struct got_repository *repo = NULL;
5246 struct got_worktree *worktree = NULL;
5247 struct got_reflist_head refs;
5248 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5249 struct got_object_id *commit_id = NULL;
5250 char *commit_id_arg = NULL;
5251 struct got_commit_object *commit = NULL;
5252 struct got_tree_object *tree = NULL;
5254 struct tog_view *view;
5256 SIMPLEQ_INIT(&refs);
5259 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5264 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5267 commit_id_arg = optarg;
5270 repo_path = realpath(optarg, NULL);
5271 if (repo_path == NULL)
5272 return got_error_from_errno2("realpath",
5287 cwd = getcwd(NULL, 0);
5289 return got_error_from_errno("getcwd");
5291 error = got_worktree_open(&worktree, cwd);
5292 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5295 if (repo_path == NULL) {
5298 strdup(got_worktree_get_repo_path(worktree));
5300 repo_path = strdup(cwd);
5302 if (repo_path == NULL) {
5303 error = got_error_from_errno("strdup");
5307 error = got_repo_open(&repo, repo_path, NULL);
5311 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5318 error = apply_unveil(got_repo_get_path(repo), NULL);
5322 error = got_repo_match_object_id(&commit_id, NULL,
5323 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5324 GOT_OBJ_TYPE_COMMIT, 1, repo);
5328 error = got_object_open_as_commit(&commit, repo, commit_id);
5332 error = got_object_open_as_tree(&tree, repo,
5333 got_object_commit_get_tree_id(commit));
5337 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5341 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5343 error = got_error_from_errno("view_open");
5346 error = open_tree_view(view, tree, commit_id, &refs, repo);
5349 if (!got_path_is_root_dir(in_repo_path)) {
5350 error = tree_view_walk_path(&view->state.tree, commit_id,
5351 in_repo_path, repo);
5357 /* Release work tree lock. */
5358 got_worktree_close(worktree);
5361 error = view_loop(view);
5367 got_object_commit_close(commit);
5369 got_object_tree_close(tree);
5371 got_repo_close(repo);
5372 got_ref_list_free(&refs);
5381 fprintf(stderr, "commands:");
5382 for (i = 0; i < nitems(tog_commands); i++) {
5383 struct tog_cmd *cmd = &tog_commands[i];
5384 fprintf(stderr, " %s", cmd->name);
5386 fputc('\n', stderr);
5392 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5393 "[arg ...]\n", getprogname());
5395 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5402 make_argv(int argc, ...)
5410 argv = calloc(argc, sizeof(char *));
5413 for (i = 0; i < argc; i++) {
5414 argv[i] = strdup(va_arg(ap, char *));
5415 if (argv[i] == NULL)
5424 * Try to convert 'tog path' into a 'tog log path' command.
5425 * The user could simply have mistyped the command rather than knowingly
5426 * provided a path. So check whether argv[0] can in fact be resolved
5427 * to a path in the HEAD commit and print a special error if not.
5428 * This hack is for mpi@ <3
5430 static const struct got_error *
5431 tog_log_with_path(int argc, char *argv[])
5433 const struct got_error *error = NULL;
5434 struct tog_cmd *cmd = NULL;
5435 struct got_repository *repo = NULL;
5436 struct got_worktree *worktree = NULL;
5437 struct got_object_id *commit_id = NULL, *id = NULL;
5438 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5439 char *commit_id_str = NULL, **cmd_argv = NULL;
5441 cwd = getcwd(NULL, 0);
5443 return got_error_from_errno("getcwd");
5445 error = got_worktree_open(&worktree, cwd);
5446 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5450 repo_path = strdup(got_worktree_get_repo_path(worktree));
5452 repo_path = strdup(cwd);
5453 if (repo_path == NULL) {
5454 error = got_error_from_errno("strdup");
5458 error = got_repo_open(&repo, repo_path, NULL);
5462 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5467 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5468 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5469 GOT_OBJ_TYPE_COMMIT, 1, repo);
5474 got_worktree_close(worktree);
5478 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5480 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5482 fprintf(stderr, "%s: '%s' is no known command or path\n",
5483 getprogname(), argv[0]);
5488 got_repo_close(repo);
5491 error = got_object_id_str(&commit_id_str, commit_id);
5495 cmd = &tog_commands[0]; /* log */
5497 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5498 error = cmd->cmd_main(argc, cmd_argv);
5501 got_repo_close(repo);
5503 got_worktree_close(worktree);
5505 free(commit_id_str);
5512 for (i = 0; i < argc; i++)
5520 main(int argc, char *argv[])
5522 const struct got_error *error = NULL;
5523 struct tog_cmd *cmd = NULL;
5524 int ch, hflag = 0, Vflag = 0;
5525 char **cmd_argv = NULL;
5526 static struct option longopts[] = {
5527 { "version", no_argument, NULL, 'V' },
5531 setlocale(LC_CTYPE, "");
5533 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5553 got_version_print_str();
5560 /* Build an argument vector which runs a default command. */
5561 cmd = &tog_commands[0];
5563 cmd_argv = make_argv(argc, cmd->name);
5567 /* Did the user specify a command? */
5568 for (i = 0; i < nitems(tog_commands); i++) {
5569 if (strncmp(tog_commands[i].name, argv[0],
5570 strlen(argv[0])) == 0) {
5571 cmd = &tog_commands[i];
5580 /* No command specified; try log with a path */
5581 error = tog_log_with_path(argc, argv);
5586 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5592 for (i = 0; i < argc; i++)
5597 if (error && error->code != GOT_ERR_CANCELLED)
5598 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);