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>
23 #define _XOPEN_SOURCE_EXTENDED
25 #undef _XOPEN_SOURCE_EXTENDED
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_opentemp.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
57 #include "got_worktree.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #define CTRL(x) ((x) & 0x1f)
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
85 static const struct got_error* cmd_log(int, char *[]);
86 static const struct got_error* cmd_diff(int, char *[]);
87 static const struct got_error* cmd_blame(int, char *[]);
88 static const struct got_error* cmd_tree(int, char *[]);
90 static struct tog_cmd tog_commands[] = {
91 { "log", cmd_log, usage_log },
92 { "diff", cmd_diff, usage_diff },
93 { "blame", cmd_blame, usage_blame },
94 { "tree", cmd_tree, usage_tree },
104 #define TOG_EOF_STRING "(END)"
106 struct commit_queue_entry {
107 TAILQ_ENTRY(commit_queue_entry) entry;
108 struct got_object_id *id;
109 struct got_commit_object *commit;
112 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 struct commit_queue {
115 struct commit_queue_head head;
119 SIMPLEQ_ENTRY(tog_color) entry;
123 SIMPLEQ_HEAD(tog_colors, tog_color);
125 static const struct got_error *
126 add_color(struct tog_colors *colors, const char *pattern,
127 int idx, short color)
129 const struct got_error *err = NULL;
130 struct tog_color *tc;
133 if (idx < 1 || idx > COLOR_PAIRS - 1)
136 init_pair(idx, color, -1);
138 tc = calloc(1, sizeof(*tc));
140 return got_error_from_errno("calloc");
141 regerr = regcomp(&tc->regex, pattern,
142 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
144 static char regerr_msg[512];
145 static char err_msg[512];
146 regerror(regerr, &tc->regex, regerr_msg,
148 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
150 err = got_error_msg(GOT_ERR_REGEX, err_msg);
155 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 free_colors(struct tog_colors *colors)
162 struct tog_color *tc;
164 while (!SIMPLEQ_EMPTY(colors)) {
165 tc = SIMPLEQ_FIRST(colors);
166 SIMPLEQ_REMOVE_HEAD(colors, entry);
173 get_color(struct tog_colors *colors, int colorpair)
175 struct tog_color *tc = NULL;
177 SIMPLEQ_FOREACH(tc, colors, entry) {
178 if (tc->colorpair == colorpair)
186 default_color_value(const char *envvar)
188 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
189 return COLOR_MAGENTA;
190 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
194 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
196 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
199 return COLOR_MAGENTA;
200 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
202 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
204 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
206 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
208 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
215 get_color_value(const char *envvar)
217 const char *val = getenv(envvar);
220 return default_color_value(envvar);
222 if (strcasecmp(val, "black") == 0)
224 if (strcasecmp(val, "red") == 0)
226 if (strcasecmp(val, "green") == 0)
228 if (strcasecmp(val, "yellow") == 0)
230 if (strcasecmp(val, "blue") == 0)
232 if (strcasecmp(val, "magenta") == 0)
233 return COLOR_MAGENTA;
234 if (strcasecmp(val, "cyan") == 0)
236 if (strcasecmp(val, "white") == 0)
238 if (strcasecmp(val, "default") == 0)
241 return default_color_value(envvar);
245 struct tog_diff_view_state {
246 struct got_object_id *id1, *id2;
248 int first_displayed_line;
249 int last_displayed_line;
252 struct got_repository *repo;
253 struct got_reflist_head *refs;
254 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;
1165 struct got_object_id *ref_id;
1168 name = got_ref_get_name(re->ref);
1169 if (strcmp(name, GOT_REF_HEAD) == 0)
1171 if (strncmp(name, "refs/", 5) == 0)
1173 if (strncmp(name, "got/", 4) == 0)
1175 if (strncmp(name, "heads/", 6) == 0)
1177 if (strncmp(name, "remotes/", 8) == 0) {
1179 s = strstr(name, "/" GOT_REF_HEAD);
1180 if (s != NULL && s[strlen(s)] == '\0')
1183 err = got_ref_resolve(&ref_id, repo, re->ref);
1186 if (strncmp(name, "tags/", 5) == 0) {
1187 err = got_object_open_as_tag(&tag, repo, ref_id);
1189 if (err->code != GOT_ERR_OBJ_TYPE) {
1193 /* Ref points at something other than a tag. */
1198 cmp = got_object_id_cmp(tag ?
1199 got_object_tag_get_object_id(tag) : ref_id, id);
1202 got_object_tag_close(tag);
1206 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1207 s ? ", " : "", name) == -1) {
1208 err = got_error_from_errno("asprintf");
1219 static const struct got_error *
1220 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1223 char *smallerthan, *at;
1225 smallerthan = strchr(author, '<');
1226 if (smallerthan && smallerthan[1] != '\0')
1227 author = smallerthan + 1;
1228 at = strchr(author, '@');
1231 return format_line(wauthor, author_width, author, limit, col_tab_align);
1234 static const struct got_error *
1235 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1236 struct got_object_id *id, struct got_reflist_head *refs,
1237 const size_t date_display_cols, int author_display_cols,
1238 struct tog_colors *colors)
1240 const struct got_error *err = NULL;
1241 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1242 char *logmsg0 = NULL, *logmsg = NULL;
1243 char *author = NULL;
1244 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1245 int author_width, logmsg_width;
1246 char *newline, *line = NULL;
1248 const int avail = view->ncols;
1250 time_t committer_time;
1251 struct tog_color *tc;
1253 committer_time = got_object_commit_get_committer_time(commit);
1254 if (localtime_r(&committer_time, &tm) == NULL)
1255 return got_error_from_errno("localtime_r");
1256 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1258 return got_error(GOT_ERR_NO_SPACE);
1260 if (avail <= date_display_cols)
1261 limit = MIN(sizeof(datebuf) - 1, avail);
1263 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1264 tc = get_color(colors, TOG_COLOR_DATE);
1266 wattr_on(view->window,
1267 COLOR_PAIR(tc->colorpair), NULL);
1268 waddnstr(view->window, datebuf, limit);
1270 wattr_off(view->window,
1271 COLOR_PAIR(tc->colorpair), NULL);
1278 err = got_object_id_str(&id_str, id);
1281 tc = get_color(colors, TOG_COLOR_COMMIT);
1283 wattr_on(view->window,
1284 COLOR_PAIR(tc->colorpair), NULL);
1285 wprintw(view->window, "%.8s ", id_str);
1287 wattr_off(view->window,
1288 COLOR_PAIR(tc->colorpair), NULL);
1295 author = strdup(got_object_commit_get_author(commit));
1296 if (author == NULL) {
1297 err = got_error_from_errno("strdup");
1300 err = format_author(&wauthor, &author_width, author, avail - col, col);
1303 tc = get_color(colors, TOG_COLOR_AUTHOR);
1305 wattr_on(view->window,
1306 COLOR_PAIR(tc->colorpair), NULL);
1307 waddwstr(view->window, wauthor);
1309 wattr_off(view->window,
1310 COLOR_PAIR(tc->colorpair), NULL);
1311 col += author_width;
1312 while (col < avail && author_width < author_display_cols + 2) {
1313 waddch(view->window, ' ');
1320 err = got_object_commit_get_logmsg(&logmsg0, commit);
1324 while (*logmsg == '\n')
1326 newline = strchr(logmsg, '\n');
1329 limit = avail - col;
1330 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1333 waddwstr(view->window, wlogmsg);
1334 col += logmsg_width;
1335 while (col < avail) {
1336 waddch(view->window, ' ');
1348 static struct commit_queue_entry *
1349 alloc_commit_queue_entry(struct got_commit_object *commit,
1350 struct got_object_id *id)
1352 struct commit_queue_entry *entry;
1354 entry = calloc(1, sizeof(*entry));
1359 entry->commit = commit;
1364 pop_commit(struct commit_queue *commits)
1366 struct commit_queue_entry *entry;
1368 entry = TAILQ_FIRST(&commits->head);
1369 TAILQ_REMOVE(&commits->head, entry, entry);
1370 got_object_commit_close(entry->commit);
1371 commits->ncommits--;
1372 /* Don't free entry->id! It is owned by the commit graph. */
1377 free_commits(struct commit_queue *commits)
1379 while (!TAILQ_EMPTY(&commits->head))
1380 pop_commit(commits);
1383 static const struct got_error *
1384 match_commit(int *have_match, struct got_object_id *id,
1385 struct got_commit_object *commit, regex_t *regex)
1387 const struct got_error *err = NULL;
1388 regmatch_t regmatch;
1389 char *id_str = NULL, *logmsg = NULL;
1393 err = got_object_id_str(&id_str, id);
1397 err = got_object_commit_get_logmsg(&logmsg, commit);
1401 if (regexec(regex, got_object_commit_get_author(commit), 1,
1402 ®match, 0) == 0 ||
1403 regexec(regex, got_object_commit_get_committer(commit), 1,
1404 ®match, 0) == 0 ||
1405 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1406 regexec(regex, logmsg, 1, ®match, 0) == 0)
1414 static const struct got_error *
1415 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1416 int minqueue, struct got_repository *repo, const char *path,
1417 int *searching, int *search_next_done, regex_t *regex)
1419 const struct got_error *err = NULL;
1423 * We keep all commits open throughout the lifetime of the log
1424 * view in order to avoid having to re-fetch commits from disk
1425 * while updating the display.
1427 while (nqueued < minqueue ||
1428 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1429 struct got_object_id *id;
1430 struct got_commit_object *commit;
1431 struct commit_queue_entry *entry;
1434 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1435 if (err || id == NULL)
1438 err = got_object_open_as_commit(&commit, repo, id);
1441 entry = alloc_commit_queue_entry(commit, id);
1442 if (entry == NULL) {
1443 err = got_error_from_errno("alloc_commit_queue_entry");
1447 errcode = pthread_mutex_lock(&tog_mutex);
1449 err = got_error_set_errno(errcode,
1450 "pthread_mutex_lock");
1454 entry->idx = commits->ncommits;
1455 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1457 commits->ncommits++;
1459 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1461 err = match_commit(&have_match, id, commit, regex);
1465 *search_next_done = TOG_SEARCH_HAVE_MORE;
1468 errcode = pthread_mutex_unlock(&tog_mutex);
1469 if (errcode && err == NULL)
1470 err = got_error_set_errno(errcode,
1471 "pthread_mutex_unlock");
1479 static const struct got_error *
1480 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1481 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1482 struct commit_queue *commits, int selected_idx, int limit,
1483 struct got_reflist_head *refs, const char *path, int commits_needed,
1484 struct tog_colors *colors)
1486 const struct got_error *err = NULL;
1487 struct tog_log_view_state *s = &view->state.log;
1488 struct commit_queue_entry *entry;
1490 int ncommits, author_cols = 4;
1491 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1492 char *refs_str = NULL;
1494 struct tog_color *tc;
1495 static const size_t date_display_cols = 12;
1500 if (ncommits == selected_idx) {
1504 entry = TAILQ_NEXT(entry, entry);
1508 if (*selected && !(view->searching && view->search_next_done == 0)) {
1509 err = got_object_id_str(&id_str, (*selected)->id);
1513 err = build_refs_str(&refs_str, refs, (*selected)->id,
1520 if (commits_needed == 0)
1521 halfdelay(10); /* disable fast refresh */
1523 if (commits_needed > 0) {
1524 if (asprintf(&ncommits_str, " [%d/%d] %s",
1525 entry ? entry->idx + 1 : 0, commits->ncommits,
1526 (view->searching && !view->search_next_done) ?
1527 "searching..." : "loading...") == -1) {
1528 err = got_error_from_errno("asprintf");
1532 const char *search_str = NULL;
1534 if (view->searching) {
1535 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1536 search_str = "no more matches";
1537 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1538 search_str = "no matches found";
1539 else if (!view->search_next_done)
1540 search_str = "searching...";
1543 if (asprintf(&ncommits_str, " [%d/%d] %s",
1544 entry ? entry->idx + 1 : 0, commits->ncommits,
1545 search_str ? search_str :
1546 (refs_str ? refs_str : "")) == -1) {
1547 err = got_error_from_errno("asprintf");
1552 if (path && strcmp(path, "/") != 0) {
1553 if (asprintf(&header, "commit %s %s%s",
1554 id_str ? id_str : "........................................",
1555 path, ncommits_str) == -1) {
1556 err = got_error_from_errno("asprintf");
1560 } else if (asprintf(&header, "commit %s%s",
1561 id_str ? id_str : "........................................",
1562 ncommits_str) == -1) {
1563 err = got_error_from_errno("asprintf");
1567 err = format_line(&wline, &width, header, view->ncols, 0);
1571 werase(view->window);
1573 if (view_needs_focus_indication(view))
1574 wstandout(view->window);
1575 tc = get_color(colors, TOG_COLOR_COMMIT);
1577 wattr_on(view->window,
1578 COLOR_PAIR(tc->colorpair), NULL);
1579 waddwstr(view->window, wline);
1581 wattr_off(view->window,
1582 COLOR_PAIR(tc->colorpair), NULL);
1583 while (width < view->ncols) {
1584 waddch(view->window, ' ');
1587 if (view_needs_focus_indication(view))
1588 wstandend(view->window);
1593 /* Grow author column size if necessary. */
1600 if (ncommits >= limit - 1)
1602 author = strdup(got_object_commit_get_author(entry->commit));
1603 if (author == NULL) {
1604 err = got_error_from_errno("strdup");
1607 err = format_author(&wauthor, &width, author, COLS,
1609 if (author_cols < width)
1610 author_cols = width;
1614 entry = TAILQ_NEXT(entry, entry);
1621 if (ncommits >= limit - 1)
1623 if (ncommits == selected_idx)
1624 wstandout(view->window);
1625 err = draw_commit(view, entry->commit, entry->id, refs,
1626 date_display_cols, author_cols, colors);
1627 if (ncommits == selected_idx)
1628 wstandend(view->window);
1633 entry = TAILQ_NEXT(entry, entry);
1646 scroll_up(struct tog_view *view,
1647 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1648 struct commit_queue *commits)
1650 struct commit_queue_entry *entry;
1653 entry = TAILQ_FIRST(&commits->head);
1654 if (*first_displayed_entry == entry)
1657 entry = *first_displayed_entry;
1658 while (entry && nscrolled < maxscroll) {
1659 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1661 *first_displayed_entry = entry;
1667 static const struct got_error *
1668 trigger_log_thread(struct tog_view *log_view, int wait,
1669 int *commits_needed, int *log_complete,
1670 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1674 halfdelay(1); /* fast refresh while loading commits */
1676 while (*commits_needed > 0) {
1680 /* Wake the log thread. */
1681 errcode = pthread_cond_signal(need_commits);
1683 return got_error_set_errno(errcode,
1684 "pthread_cond_signal");
1687 * The mutex will be released while the view loop waits
1688 * in wgetch(), at which time the log thread will run.
1693 /* Display progress update in log view. */
1694 show_log_view(log_view);
1698 /* Wait right here while next commit is being loaded. */
1699 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1701 return got_error_set_errno(errcode,
1702 "pthread_cond_wait");
1704 /* Display progress update in log view. */
1705 show_log_view(log_view);
1713 static const struct got_error *
1714 scroll_down(struct tog_view *log_view,
1715 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1716 struct commit_queue_entry **last_displayed_entry,
1717 struct commit_queue *commits, int *log_complete, int *commits_needed,
1718 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1720 const struct got_error *err = NULL;
1721 struct commit_queue_entry *pentry;
1722 int nscrolled = 0, ncommits_needed;
1724 if (*last_displayed_entry == NULL)
1727 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1728 if (commits->ncommits < ncommits_needed && !*log_complete) {
1730 * Ask the log thread for required amount of commits.
1732 (*commits_needed) += maxscroll;
1733 err = trigger_log_thread(log_view, 1, commits_needed,
1734 log_complete, need_commits, commit_loaded);
1740 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1744 *last_displayed_entry = pentry;
1746 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1749 *first_displayed_entry = pentry;
1750 } while (++nscrolled < maxscroll);
1755 static const struct got_error *
1756 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1757 struct got_commit_object *commit, struct got_object_id *commit_id,
1758 struct tog_view *log_view, struct got_reflist_head *refs,
1759 struct got_repository *repo)
1761 const struct got_error *err;
1762 struct got_object_qid *parent_id;
1763 struct tog_view *diff_view;
1765 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1766 if (diff_view == NULL)
1767 return got_error_from_errno("view_open");
1769 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1770 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1771 commit_id, log_view, refs, repo);
1773 *new_view = diff_view;
1777 static const struct got_error *
1778 tree_view_visit_subtree(struct got_tree_object *subtree,
1779 struct tog_tree_view_state *s)
1781 struct tog_parent_tree *parent;
1783 parent = calloc(1, sizeof(*parent));
1785 return got_error_from_errno("calloc");
1787 parent->tree = s->tree;
1788 parent->first_displayed_entry = s->first_displayed_entry;
1789 parent->selected_entry = s->selected_entry;
1790 parent->selected = s->selected;
1791 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1794 s->first_displayed_entry = NULL;
1798 static const struct got_error *
1799 tree_view_walk_path(struct tog_tree_view_state *s,
1800 struct got_object_id *commit_id,
1801 const char *path, struct got_repository *repo)
1803 const struct got_error *err = NULL;
1804 struct got_tree_object *tree = NULL;
1806 char *slash, *subpath = NULL;
1808 /* Walk the path and open corresponding tree objects. */
1811 struct got_tree_entry *te;
1812 struct got_object_id *tree_id;
1818 /* Ensure the correct subtree entry is selected. */
1819 slash = strchr(p, '/');
1821 te_name = strdup(p);
1823 te_name = strndup(p, slash - p);
1824 if (te_name == NULL) {
1825 err = got_error_from_errno("strndup");
1828 te = got_object_tree_find_entry(s->tree, te_name);
1830 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1835 s->selected_entry = te;
1836 s->selected = got_tree_entry_get_index(te);
1837 if (s->tree != s->root)
1838 s->selected++; /* skip '..' */
1840 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1841 /* Jump to this file's entry. */
1842 s->first_displayed_entry = s->selected_entry;
1847 slash = strchr(p, '/');
1849 subpath = strndup(path, slash - path);
1851 subpath = strdup(path);
1852 if (subpath == NULL) {
1853 err = got_error_from_errno("strdup");
1857 err = got_object_id_by_path(&tree_id, repo, commit_id,
1862 err = got_object_open_as_tree(&tree, repo, tree_id);
1867 err = tree_view_visit_subtree(tree, s);
1869 got_object_tree_close(tree);
1883 static const struct got_error *
1884 browse_commit_tree(struct tog_view **new_view, int begin_x,
1885 struct commit_queue_entry *entry, const char *path,
1886 struct got_reflist_head *refs, struct got_repository *repo)
1888 const struct got_error *err = NULL;
1889 struct got_tree_object *tree;
1890 struct tog_tree_view_state *s;
1891 struct tog_view *tree_view;
1893 err = got_object_open_as_tree(&tree, repo,
1894 got_object_commit_get_tree_id(entry->commit));
1898 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1899 if (tree_view == NULL)
1900 return got_error_from_errno("view_open");
1902 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1904 got_object_tree_close(tree);
1907 s = &tree_view->state.tree;
1909 *new_view = tree_view;
1911 if (got_path_is_root_dir(path))
1914 return tree_view_walk_path(s, entry->id, path, repo);
1917 static const struct got_error *
1918 block_signals_used_by_main_thread(void)
1923 if (sigemptyset(&sigset) == -1)
1924 return got_error_from_errno("sigemptyset");
1926 /* tog handles SIGWINCH and SIGCONT */
1927 if (sigaddset(&sigset, SIGWINCH) == -1)
1928 return got_error_from_errno("sigaddset");
1929 if (sigaddset(&sigset, SIGCONT) == -1)
1930 return got_error_from_errno("sigaddset");
1932 /* ncurses handles SIGTSTP */
1933 if (sigaddset(&sigset, SIGTSTP) == -1)
1934 return got_error_from_errno("sigaddset");
1936 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1938 return got_error_set_errno(errcode, "pthread_sigmask");
1944 log_thread(void *arg)
1946 const struct got_error *err = NULL;
1948 struct tog_log_thread_args *a = arg;
1951 err = block_signals_used_by_main_thread();
1955 while (!done && !err && !tog_sigpipe_received) {
1956 err = queue_commits(a->graph, a->commits, 1, a->repo,
1957 a->in_repo_path, a->searching, a->search_next_done,
1960 if (err->code != GOT_ERR_ITER_COMPLETED)
1964 } else if (a->commits_needed > 0)
1965 a->commits_needed--;
1967 errcode = pthread_mutex_lock(&tog_mutex);
1969 err = got_error_set_errno(errcode,
1970 "pthread_mutex_lock");
1972 } else if (*a->quit)
1974 else if (*a->first_displayed_entry == NULL) {
1975 *a->first_displayed_entry =
1976 TAILQ_FIRST(&a->commits->head);
1977 *a->selected_entry = *a->first_displayed_entry;
1980 errcode = pthread_cond_signal(&a->commit_loaded);
1982 err = got_error_set_errno(errcode,
1983 "pthread_cond_signal");
1984 pthread_mutex_unlock(&tog_mutex);
1989 a->commits_needed = 0;
1991 if (a->commits_needed == 0) {
1992 errcode = pthread_cond_wait(&a->need_commits,
1995 err = got_error_set_errno(errcode,
1996 "pthread_cond_wait");
2000 errcode = pthread_mutex_unlock(&tog_mutex);
2001 if (errcode && err == NULL)
2002 err = got_error_set_errno(errcode,
2003 "pthread_mutex_unlock");
2005 a->log_complete = 1;
2009 static const struct got_error *
2010 stop_log_thread(struct tog_log_view_state *s)
2012 const struct got_error *err = NULL;
2017 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2019 return got_error_set_errno(errcode,
2020 "pthread_cond_signal");
2021 errcode = pthread_mutex_unlock(&tog_mutex);
2023 return got_error_set_errno(errcode,
2024 "pthread_mutex_unlock");
2025 errcode = pthread_join(s->thread, (void **)&err);
2027 return got_error_set_errno(errcode, "pthread_join");
2028 errcode = pthread_mutex_lock(&tog_mutex);
2030 return got_error_set_errno(errcode,
2031 "pthread_mutex_lock");
2035 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2036 if (errcode && err == NULL)
2037 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2039 if (s->thread_args.repo) {
2040 got_repo_close(s->thread_args.repo);
2041 s->thread_args.repo = NULL;
2044 if (s->thread_args.graph) {
2045 got_commit_graph_close(s->thread_args.graph);
2046 s->thread_args.graph = NULL;
2052 static const struct got_error *
2053 close_log_view(struct tog_view *view)
2055 const struct got_error *err = NULL;
2056 struct tog_log_view_state *s = &view->state.log;
2058 err = stop_log_thread(s);
2059 free_commits(&s->commits);
2060 free(s->in_repo_path);
2061 s->in_repo_path = NULL;
2067 static const struct got_error *
2068 search_start_log_view(struct tog_view *view)
2070 struct tog_log_view_state *s = &view->state.log;
2072 s->matched_entry = NULL;
2073 s->search_entry = NULL;
2077 static const struct got_error *
2078 search_next_log_view(struct tog_view *view)
2080 const struct got_error *err = NULL;
2081 struct tog_log_view_state *s = &view->state.log;
2082 struct commit_queue_entry *entry;
2084 /* Display progress update in log view. */
2085 show_log_view(view);
2089 if (s->search_entry) {
2091 errcode = pthread_mutex_unlock(&tog_mutex);
2093 return got_error_set_errno(errcode,
2094 "pthread_mutex_unlock");
2095 ch = wgetch(view->window);
2096 errcode = pthread_mutex_lock(&tog_mutex);
2098 return got_error_set_errno(errcode,
2099 "pthread_mutex_lock");
2100 if (ch == KEY_BACKSPACE) {
2101 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2104 if (view->searching == TOG_SEARCH_FORWARD)
2105 entry = TAILQ_NEXT(s->search_entry, entry);
2107 entry = TAILQ_PREV(s->search_entry,
2108 commit_queue_head, entry);
2109 } else if (s->matched_entry) {
2110 if (view->searching == TOG_SEARCH_FORWARD)
2111 entry = TAILQ_NEXT(s->matched_entry, entry);
2113 entry = TAILQ_PREV(s->matched_entry,
2114 commit_queue_head, entry);
2116 if (view->searching == TOG_SEARCH_FORWARD)
2117 entry = TAILQ_FIRST(&s->commits.head);
2119 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2125 if (entry == NULL) {
2126 if (s->thread_args.log_complete ||
2127 view->searching == TOG_SEARCH_BACKWARD) {
2128 view->search_next_done =
2129 (s->matched_entry == NULL ?
2130 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2131 s->search_entry = NULL;
2135 * Poke the log thread for more commits and return,
2136 * allowing the main loop to make progress. Search
2137 * will resume at s->search_entry once we come back.
2139 s->thread_args.commits_needed++;
2140 return trigger_log_thread(view, 0,
2141 &s->thread_args.commits_needed,
2142 &s->thread_args.log_complete,
2143 &s->thread_args.need_commits,
2144 &s->thread_args.commit_loaded);
2147 err = match_commit(&have_match, entry->id, entry->commit,
2152 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2153 s->matched_entry = entry;
2157 s->search_entry = entry;
2158 if (view->searching == TOG_SEARCH_FORWARD)
2159 entry = TAILQ_NEXT(entry, entry);
2161 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2164 if (s->matched_entry) {
2165 int cur = s->selected_entry->idx;
2166 while (cur < s->matched_entry->idx) {
2167 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2172 while (cur > s->matched_entry->idx) {
2173 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2180 s->search_entry = NULL;
2185 static const struct got_error *
2186 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2187 struct got_reflist_head *refs, struct got_repository *repo,
2188 const char *head_ref_name, const char *in_repo_path,
2191 const struct got_error *err = NULL;
2192 struct tog_log_view_state *s = &view->state.log;
2193 struct got_repository *thread_repo = NULL;
2194 struct got_commit_graph *thread_graph = NULL;
2197 if (in_repo_path != s->in_repo_path) {
2198 free(s->in_repo_path);
2199 s->in_repo_path = strdup(in_repo_path);
2200 if (s->in_repo_path == NULL)
2201 return got_error_from_errno("strdup");
2204 /* The commit queue only contains commits being displayed. */
2205 TAILQ_INIT(&s->commits.head);
2206 s->commits.ncommits = 0;
2210 s->head_ref_name = head_ref_name;
2211 s->start_id = got_object_id_dup(start_id);
2212 if (s->start_id == NULL) {
2213 err = got_error_from_errno("got_object_id_dup");
2216 s->log_branches = log_branches;
2218 SIMPLEQ_INIT(&s->colors);
2219 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2220 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2221 get_color_value("TOG_COLOR_COMMIT"));
2224 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2225 get_color_value("TOG_COLOR_AUTHOR"));
2227 free_colors(&s->colors);
2230 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2231 get_color_value("TOG_COLOR_DATE"));
2233 free_colors(&s->colors);
2238 view->show = show_log_view;
2239 view->input = input_log_view;
2240 view->close = close_log_view;
2241 view->search_start = search_start_log_view;
2242 view->search_next = search_next_log_view;
2244 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2247 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2251 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2252 s->repo, NULL, NULL);
2256 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2258 err = got_error_set_errno(errcode, "pthread_cond_init");
2261 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2263 err = got_error_set_errno(errcode, "pthread_cond_init");
2267 s->thread_args.commits_needed = view->nlines;
2268 s->thread_args.graph = thread_graph;
2269 s->thread_args.commits = &s->commits;
2270 s->thread_args.in_repo_path = s->in_repo_path;
2271 s->thread_args.start_id = s->start_id;
2272 s->thread_args.repo = thread_repo;
2273 s->thread_args.log_complete = 0;
2274 s->thread_args.quit = &s->quit;
2275 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2276 s->thread_args.selected_entry = &s->selected_entry;
2277 s->thread_args.searching = &view->searching;
2278 s->thread_args.search_next_done = &view->search_next_done;
2279 s->thread_args.regex = &view->regex;
2282 close_log_view(view);
2286 static const struct got_error *
2287 show_log_view(struct tog_view *view)
2289 struct tog_log_view_state *s = &view->state.log;
2291 if (s->thread == NULL) {
2292 int errcode = pthread_create(&s->thread, NULL, log_thread,
2295 return got_error_set_errno(errcode, "pthread_create");
2298 return draw_commits(view, &s->last_displayed_entry,
2299 &s->selected_entry, s->first_displayed_entry,
2300 &s->commits, s->selected, view->nlines, s->refs,
2301 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2304 static const struct got_error *
2305 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2306 struct tog_view **focus_view, struct tog_view *view, int ch)
2308 const struct got_error *err = NULL;
2309 struct tog_log_view_state *s = &view->state.log;
2310 char *parent_path, *in_repo_path = NULL;
2311 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2313 struct got_object_id *start_id;
2323 if (s->first_displayed_entry == NULL)
2325 if (s->selected > 0)
2328 scroll_up(view, &s->first_displayed_entry, 1,
2333 if (s->first_displayed_entry == NULL)
2335 if (TAILQ_FIRST(&s->commits.head) ==
2336 s->first_displayed_entry) {
2340 scroll_up(view, &s->first_displayed_entry,
2341 view->nlines - 1, &s->commits);
2347 if (s->first_displayed_entry == NULL)
2349 if (s->selected < MIN(view->nlines - 2,
2350 s->commits.ncommits - 1)) {
2354 err = scroll_down(view, &s->first_displayed_entry, 1,
2355 &s->last_displayed_entry, &s->commits,
2356 &s->thread_args.log_complete,
2357 &s->thread_args.commits_needed,
2358 &s->thread_args.need_commits,
2359 &s->thread_args.commit_loaded);
2363 struct commit_queue_entry *first;
2364 first = s->first_displayed_entry;
2367 err = scroll_down(view, &s->first_displayed_entry,
2368 view->nlines - 1, &s->last_displayed_entry,
2369 &s->commits, &s->thread_args.log_complete,
2370 &s->thread_args.commits_needed,
2371 &s->thread_args.need_commits,
2372 &s->thread_args.commit_loaded);
2375 if (first == s->first_displayed_entry &&
2376 s->selected < MIN(view->nlines - 2,
2377 s->commits.ncommits - 1)) {
2378 /* can't scroll further down */
2379 s->selected = MIN(view->nlines - 2,
2380 s->commits.ncommits - 1);
2386 if (s->selected > view->nlines - 2)
2387 s->selected = view->nlines - 2;
2388 if (s->selected > s->commits.ncommits - 1)
2389 s->selected = s->commits.ncommits - 1;
2394 if (s->selected_entry == NULL)
2396 if (view_is_parent_view(view))
2397 begin_x = view_split_begin_x(view->begin_x);
2398 err = open_diff_view_for_commit(&diff_view, begin_x,
2399 s->selected_entry->commit, s->selected_entry->id,
2400 view, s->refs, s->repo);
2403 if (view_is_parent_view(view)) {
2404 err = view_close_child(view);
2407 err = view_set_child(view, diff_view);
2409 view_close(diff_view);
2412 *focus_view = diff_view;
2413 view->child_focussed = 1;
2415 *new_view = diff_view;
2418 if (s->selected_entry == NULL)
2420 if (view_is_parent_view(view))
2421 begin_x = view_split_begin_x(view->begin_x);
2422 err = browse_commit_tree(&tree_view, begin_x,
2423 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2426 if (view_is_parent_view(view)) {
2427 err = view_close_child(view);
2430 err = view_set_child(view, tree_view);
2432 view_close(tree_view);
2435 *focus_view = tree_view;
2436 view->child_focussed = 1;
2438 *new_view = tree_view;
2441 if (got_path_cmp(s->in_repo_path, "/",
2442 strlen(s->in_repo_path), 1) == 0)
2444 err = got_path_dirname(&parent_path, s->in_repo_path);
2447 err = stop_log_thread(s);
2452 lv = view_open(view->nlines, view->ncols,
2453 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2456 return got_error_from_errno("view_open");
2458 err = open_log_view(lv, s->start_id, s->refs,
2459 s->repo, s->head_ref_name, parent_path,
2464 if (view_is_parent_view(view))
2467 view_set_child(view->parent, lv);
2472 err = stop_log_thread(s);
2475 lv = view_open(view->nlines, view->ncols,
2476 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2478 return got_error_from_errno("view_open");
2479 err = got_repo_match_object_id(&start_id, NULL,
2480 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2481 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2486 in_repo_path = strdup(s->in_repo_path);
2487 if (in_repo_path == NULL) {
2490 return got_error_from_errno("strdup");
2492 got_ref_list_free(s->refs);
2493 err = got_ref_list(s->refs, s->repo, NULL,
2494 got_ref_cmp_by_name, NULL);
2500 err = open_log_view(lv, start_id, s->refs, s->repo,
2501 s->head_ref_name, in_repo_path, s->log_branches);
2511 s->log_branches = !s->log_branches;
2512 err = stop_log_thread(s);
2515 lv = view_open(view->nlines, view->ncols,
2516 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2518 return got_error_from_errno("view_open");
2519 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2520 s->head_ref_name, s->in_repo_path, s->log_branches);
2535 static const struct got_error *
2536 apply_unveil(const char *repo_path, const char *worktree_path)
2538 const struct got_error *error;
2541 if (unveil("gmon.out", "rwc") != 0)
2542 return got_error_from_errno2("unveil", "gmon.out");
2544 if (repo_path && unveil(repo_path, "r") != 0)
2545 return got_error_from_errno2("unveil", repo_path);
2547 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2548 return got_error_from_errno2("unveil", worktree_path);
2550 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2551 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2553 error = got_privsep_unveil_exec_helpers();
2557 if (unveil(NULL, NULL) != 0)
2558 return got_error_from_errno("unveil");
2568 halfdelay(1); /* Do fast refresh while initial view is loading. */
2571 intrflush(stdscr, FALSE);
2572 keypad(stdscr, TRUE);
2574 if (getenv("TOG_COLORS") != NULL) {
2576 use_default_colors();
2578 signal(SIGWINCH, tog_sigwinch);
2579 signal(SIGPIPE, tog_sigpipe);
2580 signal(SIGCONT, tog_sigcont);
2583 static const struct got_error *
2584 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2585 struct got_repository *repo, struct got_worktree *worktree)
2587 const struct got_error *err = NULL;
2590 *in_repo_path = strdup("/");
2591 if (*in_repo_path == NULL)
2592 return got_error_from_errno("strdup");
2597 const char *prefix = got_worktree_get_path_prefix(worktree);
2600 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2604 if (asprintf(&p, "%s%s%s", prefix,
2605 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2606 err = got_error_from_errno("asprintf");
2610 err = got_repo_map_path(in_repo_path, repo, p, 0);
2614 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2619 static const struct got_error *
2620 cmd_log(int argc, char *argv[])
2622 const struct got_error *error;
2623 struct got_repository *repo = NULL;
2624 struct got_worktree *worktree = NULL;
2625 struct got_reflist_head refs;
2626 struct got_object_id *start_id = NULL;
2627 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2628 char *start_commit = NULL, *head_ref_name = NULL;
2629 int ch, log_branches = 0;
2630 struct tog_view *view;
2632 SIMPLEQ_INIT(&refs);
2635 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2640 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2646 start_commit = optarg;
2649 repo_path = realpath(optarg, NULL);
2650 if (repo_path == NULL)
2651 return got_error_from_errno2("realpath",
2666 cwd = getcwd(NULL, 0);
2668 return got_error_from_errno("getcwd");
2670 error = got_worktree_open(&worktree, cwd);
2671 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2674 if (repo_path == NULL) {
2677 strdup(got_worktree_get_repo_path(worktree));
2679 repo_path = strdup(cwd);
2681 if (repo_path == NULL) {
2682 error = got_error_from_errno("strdup");
2686 error = got_repo_open(&repo, repo_path, NULL);
2690 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2697 error = apply_unveil(got_repo_get_path(repo),
2698 worktree ? got_worktree_get_root_path(worktree) : NULL);
2702 if (start_commit == NULL)
2703 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2704 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2705 GOT_OBJ_TYPE_COMMIT, 1, repo);
2707 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2708 GOT_OBJ_TYPE_COMMIT, 1, repo);
2712 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2716 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2718 error = got_error_from_errno("view_open");
2722 head_ref_name = strdup(
2723 got_worktree_get_head_ref_name(worktree));
2724 if (head_ref_name == NULL) {
2725 error = got_error_from_errno("strdup");
2729 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2730 in_repo_path, log_branches);
2734 /* Release work tree lock. */
2735 got_worktree_close(worktree);
2738 error = view_loop(view);
2744 free(head_ref_name);
2746 got_repo_close(repo);
2748 got_worktree_close(worktree);
2749 got_ref_list_free(&refs);
2757 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2763 parse_next_line(FILE *f, size_t *len)
2768 const char delim[3] = { '\0', '\0', '\0'};
2770 line = fparseln(f, &linelen, &lineno, delim, 0);
2777 match_line(const char *line, regex_t *regex)
2779 regmatch_t regmatch;
2781 return regexec(regex, line, 1, ®match, 0) == 0;
2785 match_color(struct tog_colors *colors, const char *line)
2787 struct tog_color *tc = NULL;
2789 SIMPLEQ_FOREACH(tc, colors, entry) {
2790 if (match_line(line, &tc->regex))
2797 static const struct got_error *
2798 draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2799 off_t *line_offsets, int selected_line, int max_lines,
2800 int *last_displayed_line, int *eof, char *header, struct tog_colors *colors)
2802 const struct got_error *err;
2805 struct tog_color *tc;
2811 line_offset = line_offsets[first_displayed_line - 1];
2812 if (fseeko(f, line_offset, SEEK_SET) == -1)
2813 return got_error_from_errno("fseek");
2815 werase(view->window);
2818 if (asprintf(&line, "[%d/%d] %s",
2819 first_displayed_line - 1 + selected_line, nlines,
2821 return got_error_from_errno("asprintf");
2822 err = format_line(&wline, &width, line, view->ncols, 0);
2827 if (view_needs_focus_indication(view))
2828 wstandout(view->window);
2829 waddwstr(view->window, wline);
2832 if (view_needs_focus_indication(view))
2833 wstandend(view->window);
2834 if (width <= view->ncols - 1)
2835 waddch(view->window, '\n');
2843 while (max_lines > 0 && nprinted < max_lines) {
2844 line = parse_next_line(f, &len);
2849 err = format_line(&wline, &width, line, view->ncols, 0);
2855 tc = match_color(colors, line);
2857 wattr_on(view->window,
2858 COLOR_PAIR(tc->colorpair), NULL);
2859 waddwstr(view->window, wline);
2861 wattr_off(view->window,
2862 COLOR_PAIR(tc->colorpair), NULL);
2863 if (width <= view->ncols - 1)
2864 waddch(view->window, '\n');
2871 *last_displayed_line = first_displayed_line + (nprinted - 1);
2873 *last_displayed_line = first_displayed_line;
2878 while (nprinted < view->nlines) {
2879 waddch(view->window, '\n');
2883 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2888 wstandout(view->window);
2889 waddwstr(view->window, wline);
2892 wstandend(view->window);
2899 get_datestr(time_t *time, char *datebuf)
2901 struct tm mytm, *tm;
2904 tm = gmtime_r(time, &mytm);
2907 s = asctime_r(tm, datebuf);
2910 p = strchr(s, '\n');
2916 static const struct got_error *
2917 get_changed_paths(struct got_pathlist_head *paths,
2918 struct got_commit_object *commit, struct got_repository *repo)
2920 const struct got_error *err = NULL;
2921 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2922 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2923 struct got_object_qid *qid;
2925 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2927 struct got_commit_object *pcommit;
2928 err = got_object_open_as_commit(&pcommit, repo,
2933 tree_id1 = got_object_commit_get_tree_id(pcommit);
2934 got_object_commit_close(pcommit);
2939 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2944 tree_id2 = got_object_commit_get_tree_id(commit);
2945 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2949 err = got_diff_tree(tree1, tree2, "", "", repo,
2950 got_diff_tree_collect_changed_paths, paths, 0);
2953 got_object_tree_close(tree1);
2955 got_object_tree_close(tree2);
2959 static const struct got_error *
2960 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
2964 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
2966 return got_error_from_errno("reallocarray");
2968 (*line_offsets)[*nlines] = off;
2973 static const struct got_error *
2974 write_commit_info(off_t **line_offsets, size_t *nlines,
2975 struct got_object_id *commit_id, struct got_reflist_head *refs,
2976 struct got_repository *repo, FILE *outfile)
2978 const struct got_error *err = NULL;
2979 char datebuf[26], *datestr;
2980 struct got_commit_object *commit;
2981 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
2982 time_t committer_time;
2983 const char *author, *committer;
2984 char *refs_str = NULL;
2985 struct got_pathlist_head changed_paths;
2986 struct got_pathlist_entry *pe;
2990 TAILQ_INIT(&changed_paths);
2993 err = build_refs_str(&refs_str, refs, commit_id, repo);
2998 err = got_object_open_as_commit(&commit, repo, commit_id);
3002 err = got_object_id_str(&id_str, commit_id);
3004 err = got_error_from_errno("got_object_id_str");
3008 err = add_line_offset(line_offsets, nlines, 0);
3012 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3013 refs_str ? refs_str : "", refs_str ? ")" : "");
3015 err = got_error_from_errno("fprintf");
3019 err = add_line_offset(line_offsets, nlines, outoff);
3023 n = fprintf(outfile, "from: %s\n",
3024 got_object_commit_get_author(commit));
3026 err = got_error_from_errno("fprintf");
3030 err = add_line_offset(line_offsets, nlines, outoff);
3034 committer_time = got_object_commit_get_committer_time(commit);
3035 datestr = get_datestr(&committer_time, datebuf);
3037 n = fprintf(outfile, "date: %s UTC\n", datestr);
3039 err = got_error_from_errno("fprintf");
3043 err = add_line_offset(line_offsets, nlines, outoff);
3047 author = got_object_commit_get_author(commit);
3048 committer = got_object_commit_get_committer(commit);
3049 if (strcmp(author, committer) != 0) {
3050 n = fprintf(outfile, "via: %s\n", committer);
3052 err = got_error_from_errno("fprintf");
3056 err = add_line_offset(line_offsets, nlines, outoff);
3060 err = got_object_commit_get_logmsg(&logmsg, commit);
3064 while ((line = strsep(&s, "\n")) != NULL) {
3065 n = fprintf(outfile, "%s\n", line);
3067 err = got_error_from_errno("fprintf");
3071 err = add_line_offset(line_offsets, nlines, outoff);
3076 err = get_changed_paths(&changed_paths, commit, repo);
3079 TAILQ_FOREACH(pe, &changed_paths, entry) {
3080 struct got_diff_changed_path *cp = pe->data;
3081 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3083 err = got_error_from_errno("fprintf");
3087 err = add_line_offset(line_offsets, nlines, outoff);
3090 free((char *)pe->path);
3094 fputc('\n', outfile);
3096 err = add_line_offset(line_offsets, nlines, outoff);
3098 got_pathlist_free(&changed_paths);
3102 got_object_commit_close(commit);
3104 free(*line_offsets);
3105 *line_offsets = NULL;
3111 static const struct got_error *
3112 create_diff(struct tog_diff_view_state *s)
3114 const struct got_error *err = NULL;
3118 free(s->line_offsets);
3119 s->line_offsets = malloc(sizeof(off_t));
3120 if (s->line_offsets == NULL)
3121 return got_error_from_errno("malloc");
3126 err = got_error_from_errno("got_opentemp");
3129 if (s->f && fclose(s->f) != 0) {
3130 err = got_error_from_errno("fclose");
3136 err = got_object_get_type(&obj_type, s->repo, s->id1);
3138 err = got_object_get_type(&obj_type, s->repo, s->id2);
3143 case GOT_OBJ_TYPE_BLOB:
3144 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3145 s->id1, s->id2, NULL, NULL, s->diff_context, 0,
3148 case GOT_OBJ_TYPE_TREE:
3149 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3150 s->id1, s->id2, "", "", s->diff_context, 0, s->repo, s->f);
3152 case GOT_OBJ_TYPE_COMMIT: {
3153 const struct got_object_id_queue *parent_ids;
3154 struct got_object_qid *pid;
3155 struct got_commit_object *commit2;
3157 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3160 /* Show commit info if we're diffing to a parent/root commit. */
3161 if (s->id1 == NULL) {
3162 err = write_commit_info(&s->line_offsets, &s->nlines,
3163 s->id2, s->refs, s->repo, s->f);
3167 parent_ids = got_object_commit_get_parent_ids(commit2);
3168 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3169 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3170 err = write_commit_info(
3171 &s->line_offsets, &s->nlines,
3172 s->id2, s->refs, s->repo, s->f);
3179 got_object_commit_close(commit2);
3181 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3182 s->id1, s->id2, s->diff_context, 0, s->repo, s->f);
3186 err = got_error(GOT_ERR_OBJ_TYPE);
3192 if (s->f && fflush(s->f) != 0 && err == NULL)
3193 err = got_error_from_errno("fflush");
3198 diff_view_indicate_progress(struct tog_view *view)
3200 mvwaddstr(view->window, 0, 0, "diffing...");
3205 static const struct got_error *
3206 search_start_diff_view(struct tog_view *view)
3208 struct tog_diff_view_state *s = &view->state.diff;
3210 s->matched_line = 0;
3214 static const struct got_error *
3215 search_next_diff_view(struct tog_view *view)
3217 struct tog_diff_view_state *s = &view->state.diff;
3220 if (!view->searching) {
3221 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3225 if (s->matched_line) {
3226 if (view->searching == TOG_SEARCH_FORWARD)
3227 lineno = s->matched_line + 1;
3229 lineno = s->matched_line - 1;
3231 if (view->searching == TOG_SEARCH_FORWARD)
3242 if (lineno <= 0 || lineno > s->nlines) {
3243 if (s->matched_line == 0) {
3244 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3249 if (view->searching == TOG_SEARCH_FORWARD)
3255 offset = s->line_offsets[lineno - 1];
3256 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3258 return got_error_from_errno("fseeko");
3261 line = parse_next_line(s->f, &len);
3262 if (line && match_line(line, &view->regex)) {
3263 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3264 s->matched_line = lineno;
3269 if (view->searching == TOG_SEARCH_FORWARD)
3275 if (s->matched_line) {
3276 s->first_displayed_line = s->matched_line;
3277 s->selected_line = 1;
3283 static const struct got_error *
3284 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3285 struct got_object_id *id2, struct tog_view *log_view,
3286 struct got_reflist_head *refs, struct got_repository *repo)
3288 const struct got_error *err;
3289 struct tog_diff_view_state *s = &view->state.diff;
3291 if (id1 != NULL && id2 != NULL) {
3293 err = got_object_get_type(&type1, repo, id1);
3296 err = got_object_get_type(&type2, repo, id2);
3301 return got_error(GOT_ERR_OBJ_TYPE);
3303 s->first_displayed_line = 1;
3304 s->last_displayed_line = view->nlines;
3305 s->selected_line = 1;
3312 s->id1 = got_object_id_dup(id1);
3314 return got_error_from_errno("got_object_id_dup");
3318 s->id2 = got_object_id_dup(id2);
3319 if (s->id2 == NULL) {
3322 return got_error_from_errno("got_object_id_dup");
3325 s->first_displayed_line = 1;
3326 s->last_displayed_line = view->nlines;
3327 s->diff_context = 3;
3328 s->log_view = log_view;
3332 SIMPLEQ_INIT(&s->colors);
3333 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3334 err = add_color(&s->colors,
3335 "^-", TOG_COLOR_DIFF_MINUS,
3336 get_color_value("TOG_COLOR_DIFF_MINUS"));
3339 err = add_color(&s->colors, "^\\+",
3340 TOG_COLOR_DIFF_PLUS,
3341 get_color_value("TOG_COLOR_DIFF_PLUS"));
3343 free_colors(&s->colors);
3346 err = add_color(&s->colors,
3347 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3348 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3350 free_colors(&s->colors);
3354 err = add_color(&s->colors,
3355 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3356 TOG_COLOR_DIFF_META,
3357 get_color_value("TOG_COLOR_DIFF_META"));
3359 free_colors(&s->colors);
3363 err = add_color(&s->colors,
3364 "^(from|via): ", TOG_COLOR_AUTHOR,
3365 get_color_value("TOG_COLOR_AUTHOR"));
3367 free_colors(&s->colors);
3371 err = add_color(&s->colors,
3372 "^date: ", TOG_COLOR_DATE,
3373 get_color_value("TOG_COLOR_DATE"));
3375 free_colors(&s->colors);
3380 if (log_view && view_is_splitscreen(view))
3381 show_log_view(log_view); /* draw vborder */
3382 diff_view_indicate_progress(view);
3384 s->line_offsets = NULL;
3386 err = create_diff(s);
3395 view->show = show_diff_view;
3396 view->input = input_diff_view;
3397 view->close = close_diff_view;
3398 view->search_start = search_start_diff_view;
3399 view->search_next = search_next_diff_view;
3404 static const struct got_error *
3405 close_diff_view(struct tog_view *view)
3407 const struct got_error *err = NULL;
3408 struct tog_diff_view_state *s = &view->state.diff;
3414 if (s->f && fclose(s->f) == EOF)
3415 err = got_error_from_errno("fclose");
3416 free_colors(&s->colors);
3417 free(s->line_offsets);
3418 s->line_offsets = NULL;
3423 static const struct got_error *
3424 show_diff_view(struct tog_view *view)
3426 const struct got_error *err;
3427 struct tog_diff_view_state *s = &view->state.diff;
3428 char *id_str1 = NULL, *id_str2, *header;
3431 err = got_object_id_str(&id_str1, s->id1);
3435 err = got_object_id_str(&id_str2, s->id2);
3439 if (asprintf(&header, "diff %s %s",
3440 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3441 err = got_error_from_errno("asprintf");
3449 return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3450 s->line_offsets, s->selected_line, view->nlines,
3451 &s->last_displayed_line, &s->eof, header, &s->colors);
3454 static const struct got_error *
3455 set_selected_commit(struct tog_diff_view_state *s,
3456 struct commit_queue_entry *entry)
3458 const struct got_error *err;
3459 const struct got_object_id_queue *parent_ids;
3460 struct got_commit_object *selected_commit;
3461 struct got_object_qid *pid;
3464 s->id2 = got_object_id_dup(entry->id);
3466 return got_error_from_errno("got_object_id_dup");
3468 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3471 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3473 pid = SIMPLEQ_FIRST(parent_ids);
3474 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3475 got_object_commit_close(selected_commit);
3479 static const struct got_error *
3480 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3481 struct tog_view **focus_view, struct tog_view *view, int ch)
3483 const struct got_error *err = NULL;
3484 struct tog_diff_view_state *s = &view->state.diff;
3485 struct tog_log_view_state *ls;
3486 struct commit_queue_entry *entry;
3492 if (s->first_displayed_line > 1)
3493 s->first_displayed_line--;
3497 if (s->first_displayed_line == 1)
3500 while (i++ < view->nlines - 1 &&
3501 s->first_displayed_line > 1)
3502 s->first_displayed_line--;
3507 s->first_displayed_line++;
3515 while (!s->eof && i++ < view->nlines - 1) {
3517 line = parse_next_line(s->f, NULL);
3518 s->first_displayed_line++;
3524 if (s->diff_context > 0) {
3526 diff_view_indicate_progress(view);
3527 err = create_diff(s);
3531 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3533 diff_view_indicate_progress(view);
3534 err = create_diff(s);
3539 if (s->log_view == NULL)
3541 ls = &s->log_view->state.log;
3542 entry = TAILQ_PREV(ls->selected_entry,
3543 commit_queue_head, entry);
3547 err = input_log_view(NULL, NULL, NULL, s->log_view,
3552 err = set_selected_commit(s, entry);
3556 s->first_displayed_line = 1;
3557 s->last_displayed_line = view->nlines;
3559 diff_view_indicate_progress(view);
3560 err = create_diff(s);
3564 if (s->log_view == NULL)
3566 ls = &s->log_view->state.log;
3568 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3569 ls->thread_args.commits_needed++;
3570 err = trigger_log_thread(s->log_view, 1,
3571 &ls->thread_args.commits_needed,
3572 &ls->thread_args.log_complete,
3573 &ls->thread_args.need_commits,
3574 &ls->thread_args.commit_loaded);
3578 err = input_log_view(NULL, NULL, NULL, s->log_view,
3583 entry = TAILQ_NEXT(ls->selected_entry, entry);
3587 err = set_selected_commit(s, entry);
3591 s->first_displayed_line = 1;
3592 s->last_displayed_line = view->nlines;
3594 diff_view_indicate_progress(view);
3595 err = create_diff(s);
3604 static const struct got_error *
3605 cmd_diff(int argc, char *argv[])
3607 const struct got_error *error = NULL;
3608 struct got_repository *repo = NULL;
3609 struct got_worktree *worktree = NULL;
3610 struct got_reflist_head refs;
3611 struct got_object_id *id1 = NULL, *id2 = NULL;
3612 char *repo_path = NULL, *cwd = NULL;
3613 char *id_str1 = NULL, *id_str2 = NULL;
3615 struct tog_view *view;
3617 SIMPLEQ_INIT(&refs);
3620 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3625 while ((ch = getopt(argc, argv, "r:")) != -1) {
3628 repo_path = realpath(optarg, NULL);
3629 if (repo_path == NULL)
3630 return got_error_from_errno2("realpath",
3643 usage_diff(); /* TODO show local worktree changes */
3644 } else if (argc == 2) {
3650 cwd = getcwd(NULL, 0);
3652 return got_error_from_errno("getcwd");
3654 error = got_worktree_open(&worktree, cwd);
3655 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3658 if (repo_path == NULL) {
3661 strdup(got_worktree_get_repo_path(worktree));
3663 repo_path = strdup(cwd);
3665 if (repo_path == NULL) {
3666 error = got_error_from_errno("strdup");
3670 error = got_repo_open(&repo, repo_path, NULL);
3676 error = apply_unveil(got_repo_get_path(repo), NULL);
3680 error = got_repo_match_object_id_prefix(&id1, id_str1,
3681 GOT_OBJ_TYPE_ANY, repo);
3685 error = got_repo_match_object_id_prefix(&id2, id_str2,
3686 GOT_OBJ_TYPE_ANY, repo);
3690 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3694 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3696 error = got_error_from_errno("view_open");
3699 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3702 error = view_loop(view);
3707 got_repo_close(repo);
3709 got_worktree_close(worktree);
3710 got_ref_list_free(&refs);
3718 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3723 struct tog_blame_line {
3725 struct got_object_id *id;
3728 static const struct got_error *
3729 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3730 const char *path, struct tog_blame_line *lines, int nlines,
3731 int blame_complete, int selected_line, int *first_displayed_line,
3732 int *last_displayed_line, int *eof, int max_lines,
3733 struct tog_colors *colors)
3735 const struct got_error *err;
3736 int lineno = 0, nprinted = 0;
3741 struct tog_blame_line *blame_line;
3742 struct got_object_id *prev_id = NULL;
3744 struct tog_color *tc;
3746 err = got_object_id_str(&id_str, id);
3751 werase(view->window);
3753 if (asprintf(&line, "commit %s", id_str) == -1) {
3754 err = got_error_from_errno("asprintf");
3759 err = format_line(&wline, &width, line, view->ncols, 0);
3764 if (view_needs_focus_indication(view))
3765 wstandout(view->window);
3766 tc = get_color(colors, TOG_COLOR_COMMIT);
3768 wattr_on(view->window,
3769 COLOR_PAIR(tc->colorpair), NULL);
3770 waddwstr(view->window, wline);
3772 wattr_off(view->window,
3773 COLOR_PAIR(tc->colorpair), NULL);
3774 if (view_needs_focus_indication(view))
3775 wstandend(view->window);
3778 if (width < view->ncols - 1)
3779 waddch(view->window, '\n');
3781 if (asprintf(&line, "[%d/%d] %s%s",
3782 *first_displayed_line - 1 + selected_line, nlines,
3783 blame_complete ? "" : "annotating... ", path) == -1) {
3785 return got_error_from_errno("asprintf");
3788 err = format_line(&wline, &width, line, view->ncols, 0);
3793 waddwstr(view->window, wline);
3796 if (width < view->ncols - 1)
3797 waddch(view->window, '\n');
3800 while (nprinted < max_lines - 2) {
3801 line = parse_next_line(f, &len);
3806 if (++lineno < *first_displayed_line) {
3811 if (view->ncols <= 9) {
3813 wline = wcsdup(L"");
3815 err = got_error_from_errno("wcsdup");
3817 err = format_line(&wline, &width, line,
3818 view->ncols - 9, 9);
3826 if (view->focussed && nprinted == selected_line - 1)
3827 wstandout(view->window);
3830 blame_line = &lines[lineno - 1];
3831 if (blame_line->annotated && prev_id &&
3832 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3834 nprinted == selected_line - 1)) {
3835 waddstr(view->window, " ");
3836 } else if (blame_line->annotated) {
3838 err = got_object_id_str(&id_str, blame_line->id);
3844 tc = get_color(colors, TOG_COLOR_COMMIT);
3846 wattr_on(view->window,
3847 COLOR_PAIR(tc->colorpair), NULL);
3848 wprintw(view->window, "%.8s", id_str);
3850 wattr_off(view->window,
3851 COLOR_PAIR(tc->colorpair), NULL);
3853 prev_id = blame_line->id;
3855 waddstr(view->window, "........");
3859 waddstr(view->window, "........");
3863 if (view->focussed && nprinted == selected_line - 1)
3864 wstandend(view->window);
3865 waddstr(view->window, " ");
3867 waddwstr(view->window, wline);
3868 if (width <= view->ncols - 1)
3869 waddch(view->window, '\n');
3870 if (++nprinted == 1)
3871 *first_displayed_line = lineno;
3876 *last_displayed_line = lineno;
3883 static const struct got_error *
3884 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3886 const struct got_error *err = NULL;
3887 struct tog_blame_cb_args *a = arg;
3888 struct tog_blame_line *line;
3891 if (nlines != a->nlines ||
3892 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3893 return got_error(GOT_ERR_RANGE);
3895 errcode = pthread_mutex_lock(&tog_mutex);
3897 return got_error_set_errno(errcode, "pthread_mutex_lock");
3899 if (*a->quit) { /* user has quit the blame view */
3900 err = got_error(GOT_ERR_ITER_COMPLETED);
3905 goto done; /* no change in this commit */
3907 line = &a->lines[lineno - 1];
3908 if (line->annotated)
3911 line->id = got_object_id_dup(id);
3912 if (line->id == NULL) {
3913 err = got_error_from_errno("got_object_id_dup");
3916 line->annotated = 1;
3918 errcode = pthread_mutex_unlock(&tog_mutex);
3920 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3925 blame_thread(void *arg)
3927 const struct got_error *err;
3928 struct tog_blame_thread_args *ta = arg;
3929 struct tog_blame_cb_args *a = ta->cb_args;
3932 err = block_signals_used_by_main_thread();
3936 err = got_blame(ta->path, a->commit_id, ta->repo,
3937 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3938 if (err && err->code == GOT_ERR_CANCELLED)
3941 errcode = pthread_mutex_lock(&tog_mutex);
3943 return (void *)got_error_set_errno(errcode,
3944 "pthread_mutex_lock");
3946 got_repo_close(ta->repo);
3950 errcode = pthread_mutex_unlock(&tog_mutex);
3951 if (errcode && err == NULL)
3952 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3957 static struct got_object_id *
3958 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3959 int first_displayed_line, int selected_line)
3961 struct tog_blame_line *line;
3966 line = &lines[first_displayed_line - 1 + selected_line - 1];
3967 if (!line->annotated)
3973 static const struct got_error *
3974 stop_blame(struct tog_blame *blame)
3976 const struct got_error *err = NULL;
3979 if (blame->thread) {
3981 errcode = pthread_mutex_unlock(&tog_mutex);
3983 return got_error_set_errno(errcode,
3984 "pthread_mutex_unlock");
3985 errcode = pthread_join(blame->thread, (void **)&err);
3987 return got_error_set_errno(errcode, "pthread_join");
3988 errcode = pthread_mutex_lock(&tog_mutex);
3990 return got_error_set_errno(errcode,
3991 "pthread_mutex_lock");
3992 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3994 blame->thread = NULL;
3996 if (blame->thread_args.repo) {
3997 got_repo_close(blame->thread_args.repo);
3998 blame->thread_args.repo = NULL;
4001 if (fclose(blame->f) != 0 && err == NULL)
4002 err = got_error_from_errno("fclose");
4006 for (i = 0; i < blame->nlines; i++)
4007 free(blame->lines[i].id);
4009 blame->lines = NULL;
4011 free(blame->cb_args.commit_id);
4012 blame->cb_args.commit_id = NULL;
4017 static const struct got_error *
4018 cancel_blame_view(void *arg)
4020 const struct got_error *err = NULL;
4024 errcode = pthread_mutex_lock(&tog_mutex);
4026 return got_error_set_errno(errcode,
4027 "pthread_mutex_unlock");
4030 err = got_error(GOT_ERR_CANCELLED);
4032 errcode = pthread_mutex_unlock(&tog_mutex);
4034 return got_error_set_errno(errcode,
4035 "pthread_mutex_lock");
4040 static const struct got_error *
4041 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4042 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4043 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4044 struct got_repository *repo)
4046 const struct got_error *err = NULL;
4047 struct got_blob_object *blob = NULL;
4048 struct got_repository *thread_repo = NULL;
4049 struct got_object_id *obj_id = NULL;
4052 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4056 err = got_object_get_type(&obj_type, repo, obj_id);
4060 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4061 err = got_error(GOT_ERR_OBJ_TYPE);
4065 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4068 blame->f = got_opentemp();
4069 if (blame->f == NULL) {
4070 err = got_error_from_errno("got_opentemp");
4073 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4074 &blame->line_offsets, blame->f, blob);
4075 if (err || blame->nlines == 0)
4078 /* Don't include \n at EOF in the blame line count. */
4079 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4082 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4083 if (blame->lines == NULL) {
4084 err = got_error_from_errno("calloc");
4088 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4092 blame->cb_args.view = view;
4093 blame->cb_args.lines = blame->lines;
4094 blame->cb_args.nlines = blame->nlines;
4095 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4096 if (blame->cb_args.commit_id == NULL) {
4097 err = got_error_from_errno("got_object_id_dup");
4100 blame->cb_args.quit = done;
4102 blame->thread_args.path = path;
4103 blame->thread_args.repo = thread_repo;
4104 blame->thread_args.cb_args = &blame->cb_args;
4105 blame->thread_args.complete = blame_complete;
4106 blame->thread_args.cancel_cb = cancel_blame_view;
4107 blame->thread_args.cancel_arg = done;
4108 *blame_complete = 0;
4112 got_object_blob_close(blob);
4119 static const struct got_error *
4120 open_blame_view(struct tog_view *view, char *path,
4121 struct got_object_id *commit_id, struct got_reflist_head *refs,
4122 struct got_repository *repo)
4124 const struct got_error *err = NULL;
4125 struct tog_blame_view_state *s = &view->state.blame;
4127 SIMPLEQ_INIT(&s->blamed_commits);
4129 s->path = strdup(path);
4130 if (s->path == NULL)
4131 return got_error_from_errno("strdup");
4133 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4139 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4140 s->first_displayed_line = 1;
4141 s->last_displayed_line = view->nlines;
4142 s->selected_line = 1;
4143 s->blame_complete = 0;
4146 s->commit_id = commit_id;
4147 memset(&s->blame, 0, sizeof(s->blame));
4149 SIMPLEQ_INIT(&s->colors);
4150 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4151 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4152 get_color_value("TOG_COLOR_COMMIT"));
4157 view->show = show_blame_view;
4158 view->input = input_blame_view;
4159 view->close = close_blame_view;
4160 view->search_start = search_start_blame_view;
4161 view->search_next = search_next_blame_view;
4163 return run_blame(&s->blame, view, &s->blame_complete,
4164 &s->first_displayed_line, &s->last_displayed_line,
4165 &s->selected_line, &s->done, &s->eof, s->path,
4166 s->blamed_commit->id, s->repo);
4169 static const struct got_error *
4170 close_blame_view(struct tog_view *view)
4172 const struct got_error *err = NULL;
4173 struct tog_blame_view_state *s = &view->state.blame;
4175 if (s->blame.thread)
4176 err = stop_blame(&s->blame);
4178 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4179 struct got_object_qid *blamed_commit;
4180 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4181 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4182 got_object_qid_free(blamed_commit);
4186 free_colors(&s->colors);
4191 static const struct got_error *
4192 search_start_blame_view(struct tog_view *view)
4194 struct tog_blame_view_state *s = &view->state.blame;
4196 s->matched_line = 0;
4200 static const struct got_error *
4201 search_next_blame_view(struct tog_view *view)
4203 struct tog_blame_view_state *s = &view->state.blame;
4206 if (!view->searching) {
4207 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4211 if (s->matched_line) {
4212 if (view->searching == TOG_SEARCH_FORWARD)
4213 lineno = s->matched_line + 1;
4215 lineno = s->matched_line - 1;
4217 if (view->searching == TOG_SEARCH_FORWARD)
4220 lineno = s->blame.nlines;
4228 if (lineno <= 0 || lineno > s->blame.nlines) {
4229 if (s->matched_line == 0) {
4230 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4235 if (view->searching == TOG_SEARCH_FORWARD)
4238 lineno = s->blame.nlines;
4241 offset = s->blame.line_offsets[lineno - 1];
4242 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4244 return got_error_from_errno("fseeko");
4247 line = parse_next_line(s->blame.f, &len);
4248 if (line && match_line(line, &view->regex)) {
4249 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4250 s->matched_line = lineno;
4255 if (view->searching == TOG_SEARCH_FORWARD)
4261 if (s->matched_line) {
4262 s->first_displayed_line = s->matched_line;
4263 s->selected_line = 1;
4269 static const struct got_error *
4270 show_blame_view(struct tog_view *view)
4272 const struct got_error *err = NULL;
4273 struct tog_blame_view_state *s = &view->state.blame;
4276 if (s->blame.thread == NULL) {
4277 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4278 &s->blame.thread_args);
4280 return got_error_set_errno(errcode, "pthread_create");
4282 halfdelay(1); /* fast refresh while annotating */
4285 if (s->blame_complete)
4286 halfdelay(10); /* disable fast refresh */
4288 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4289 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4290 s->selected_line, &s->first_displayed_line,
4291 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4297 static const struct got_error *
4298 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4299 struct tog_view **focus_view, struct tog_view *view, int ch)
4301 const struct got_error *err = NULL, *thread_err = NULL;
4302 struct tog_view *diff_view;
4303 struct tog_blame_view_state *s = &view->state.blame;
4312 if (s->selected_line > 1)
4314 else if (s->selected_line == 1 &&
4315 s->first_displayed_line > 1)
4316 s->first_displayed_line--;
4320 if (s->first_displayed_line == 1) {
4321 s->selected_line = 1;
4324 if (s->first_displayed_line > view->nlines - 2)
4325 s->first_displayed_line -=
4328 s->first_displayed_line = 1;
4332 if (s->selected_line < view->nlines - 2 &&
4333 s->first_displayed_line +
4334 s->selected_line <= s->blame.nlines)
4336 else if (s->last_displayed_line <
4338 s->first_displayed_line++;
4342 struct got_object_id *id = NULL;
4343 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4344 s->first_displayed_line, s->selected_line);
4348 struct got_commit_object *commit;
4349 struct got_object_qid *pid;
4350 struct got_object_id *blob_id = NULL;
4352 err = got_object_open_as_commit(&commit,
4356 pid = SIMPLEQ_FIRST(
4357 got_object_commit_get_parent_ids(commit));
4359 got_object_commit_close(commit);
4362 /* Check if path history ends here. */
4363 err = got_object_id_by_path(&blob_id, s->repo,
4366 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4368 got_object_commit_close(commit);
4371 err = got_object_get_type(&obj_type, s->repo,
4374 /* Can't blame non-blob type objects. */
4375 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4376 got_object_commit_close(commit);
4379 err = got_object_qid_alloc(&s->blamed_commit,
4381 got_object_commit_close(commit);
4383 if (got_object_id_cmp(id,
4384 s->blamed_commit->id) == 0)
4386 err = got_object_qid_alloc(&s->blamed_commit,
4392 thread_err = stop_blame(&s->blame);
4396 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4397 s->blamed_commit, entry);
4398 err = run_blame(&s->blame, view, &s->blame_complete,
4399 &s->first_displayed_line, &s->last_displayed_line,
4400 &s->selected_line, &s->done, &s->eof,
4401 s->path, s->blamed_commit->id, s->repo);
4407 struct got_object_qid *first;
4408 first = SIMPLEQ_FIRST(&s->blamed_commits);
4409 if (!got_object_id_cmp(first->id, s->commit_id))
4412 thread_err = stop_blame(&s->blame);
4416 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4417 got_object_qid_free(s->blamed_commit);
4419 SIMPLEQ_FIRST(&s->blamed_commits);
4420 err = run_blame(&s->blame, view, &s->blame_complete,
4421 &s->first_displayed_line, &s->last_displayed_line,
4422 &s->selected_line, &s->done, &s->eof, s->path,
4423 s->blamed_commit->id, s->repo);
4430 struct got_object_id *id = NULL;
4431 struct got_object_qid *pid;
4432 struct got_commit_object *commit = NULL;
4433 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4434 s->first_displayed_line, s->selected_line);
4437 err = got_object_open_as_commit(&commit, s->repo, id);
4440 pid = SIMPLEQ_FIRST(
4441 got_object_commit_get_parent_ids(commit));
4442 if (view_is_parent_view(view))
4443 begin_x = view_split_begin_x(view->begin_x);
4444 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4445 if (diff_view == NULL) {
4446 got_object_commit_close(commit);
4447 err = got_error_from_errno("view_open");
4450 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4451 id, NULL, s->refs, s->repo);
4452 got_object_commit_close(commit);
4454 view_close(diff_view);
4457 if (view_is_parent_view(view)) {
4458 err = view_close_child(view);
4461 err = view_set_child(view, diff_view);
4463 view_close(diff_view);
4466 *focus_view = diff_view;
4467 view->child_focussed = 1;
4469 *new_view = diff_view;
4477 if (s->last_displayed_line >= s->blame.nlines &&
4478 s->selected_line >= MIN(s->blame.nlines,
4479 view->nlines - 2)) {
4482 if (s->last_displayed_line >= s->blame.nlines &&
4483 s->selected_line < view->nlines - 2) {
4484 s->selected_line = MIN(s->blame.nlines,
4488 if (s->last_displayed_line + view->nlines - 2
4490 s->first_displayed_line +=
4493 s->first_displayed_line =
4498 if (s->selected_line > view->nlines - 2) {
4499 s->selected_line = MIN(s->blame.nlines,
4506 return thread_err ? thread_err : err;
4509 static const struct got_error *
4510 cmd_blame(int argc, char *argv[])
4512 const struct got_error *error;
4513 struct got_repository *repo = NULL;
4514 struct got_reflist_head refs;
4515 struct got_worktree *worktree = NULL;
4516 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4517 char *link_target = NULL;
4518 struct got_object_id *commit_id = NULL;
4519 char *commit_id_str = NULL;
4521 struct tog_view *view;
4523 SIMPLEQ_INIT(&refs);
4526 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4531 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4534 commit_id_str = optarg;
4537 repo_path = realpath(optarg, NULL);
4538 if (repo_path == NULL)
4539 return got_error_from_errno2("realpath",
4554 cwd = getcwd(NULL, 0);
4556 return got_error_from_errno("getcwd");
4558 error = got_worktree_open(&worktree, cwd);
4559 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4562 if (repo_path == NULL) {
4565 strdup(got_worktree_get_repo_path(worktree));
4567 repo_path = strdup(cwd);
4569 if (repo_path == NULL) {
4570 error = got_error_from_errno("strdup");
4574 error = got_repo_open(&repo, repo_path, NULL);
4578 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4585 error = apply_unveil(got_repo_get_path(repo), NULL);
4589 if (commit_id_str == NULL) {
4590 struct got_reference *head_ref;
4591 error = got_ref_open(&head_ref, repo, worktree ?
4592 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4595 error = got_ref_resolve(&commit_id, repo, head_ref);
4596 got_ref_close(head_ref);
4598 error = got_repo_match_object_id(&commit_id, NULL,
4599 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4604 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4608 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4610 error = got_error_from_errno("view_open");
4614 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4619 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4620 commit_id, &refs, repo);
4624 /* Release work tree lock. */
4625 got_worktree_close(worktree);
4628 error = view_loop(view);
4636 got_worktree_close(worktree);
4638 got_repo_close(repo);
4639 got_ref_list_free(&refs);
4643 static const struct got_error *
4644 draw_tree_entries(struct tog_view *view,
4645 struct got_tree_entry **first_displayed_entry,
4646 struct got_tree_entry **last_displayed_entry,
4647 struct got_tree_entry **selected_entry, int *ndisplayed,
4648 const char *label, int show_ids, const char *parent_path,
4649 struct got_tree_object *tree, int selected, int limit,
4650 int isroot, struct tog_colors *colors, struct got_repository *repo)
4652 const struct got_error *err = NULL;
4653 struct got_tree_entry *te;
4655 struct tog_color *tc;
4656 int width, n, i, nentries;
4660 werase(view->window);
4665 err = format_line(&wline, &width, label, view->ncols, 0);
4668 if (view_needs_focus_indication(view))
4669 wstandout(view->window);
4670 tc = get_color(colors, TOG_COLOR_COMMIT);
4672 wattr_on(view->window,
4673 COLOR_PAIR(tc->colorpair), NULL);
4674 waddwstr(view->window, wline);
4676 wattr_off(view->window,
4677 COLOR_PAIR(tc->colorpair), NULL);
4678 if (view_needs_focus_indication(view))
4679 wstandend(view->window);
4682 if (width < view->ncols - 1)
4683 waddch(view->window, '\n');
4686 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4689 waddwstr(view->window, wline);
4692 if (width < view->ncols - 1)
4693 waddch(view->window, '\n');
4696 waddch(view->window, '\n');
4700 if (*first_displayed_entry == NULL) {
4701 te = got_object_tree_get_first_entry(tree);
4702 if (selected == 0) {
4704 wstandout(view->window);
4705 *selected_entry = NULL;
4707 waddstr(view->window, " ..\n"); /* parent directory */
4708 if (selected == 0 && view->focussed)
4709 wstandend(view->window);
4716 te = *first_displayed_entry;
4719 nentries = got_object_tree_get_nentries(tree);
4720 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4721 char *line = NULL, *id_str = NULL, *link_target = NULL;
4722 const char *modestr = "";
4725 te = got_object_tree_get_entry(tree, i);
4726 mode = got_tree_entry_get_mode(te);
4729 err = got_object_id_str(&id_str,
4730 got_tree_entry_get_id(te));
4732 return got_error_from_errno(
4733 "got_object_id_str");
4735 if (got_object_tree_entry_is_submodule(te))
4737 else if (S_ISLNK(mode)) {
4740 err = got_tree_entry_get_symlink_target(&link_target,
4746 for (i = 0; i < strlen(link_target); i++) {
4747 if (!isprint((unsigned char)link_target[i]))
4748 link_target[i] = '?';
4752 else if (S_ISDIR(mode))
4754 else if (mode & S_IXUSR)
4756 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4757 got_tree_entry_get_name(te), modestr,
4758 link_target ? " -> ": "",
4759 link_target ? link_target : "") == -1) {
4762 return got_error_from_errno("asprintf");
4766 err = format_line(&wline, &width, line, view->ncols, 0);
4771 if (n == selected) {
4773 wstandout(view->window);
4774 *selected_entry = te;
4776 tc = match_color(colors, line);
4778 wattr_on(view->window,
4779 COLOR_PAIR(tc->colorpair), NULL);
4780 waddwstr(view->window, wline);
4782 wattr_off(view->window,
4783 COLOR_PAIR(tc->colorpair), NULL);
4784 if (width < view->ncols - 1)
4785 waddch(view->window, '\n');
4786 if (n == selected && view->focussed)
4787 wstandend(view->window);
4793 *last_displayed_entry = te;
4802 tree_scroll_up(struct tog_view *view,
4803 struct got_tree_entry **first_displayed_entry, int maxscroll,
4804 struct got_tree_object *tree, int isroot)
4806 struct got_tree_entry *te;
4809 if (*first_displayed_entry == NULL)
4812 te = got_object_tree_get_entry(tree, 0);
4813 if (*first_displayed_entry == te) {
4815 *first_displayed_entry = NULL;
4820 while (*first_displayed_entry && i < maxscroll) {
4821 *first_displayed_entry = got_tree_entry_get_prev(tree,
4822 *first_displayed_entry);
4825 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4826 *first_displayed_entry = NULL;
4830 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4831 struct got_tree_entry *last_displayed_entry,
4832 struct got_tree_object *tree)
4834 struct got_tree_entry *next, *last;
4837 if (*first_displayed_entry)
4838 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4840 next = got_object_tree_get_first_entry(tree);
4842 last = last_displayed_entry;
4843 while (next && last && n++ < maxscroll) {
4844 last = got_tree_entry_get_next(tree, last);
4846 *first_displayed_entry = next;
4847 next = got_tree_entry_get_next(tree, next);
4853 static const struct got_error *
4854 tree_entry_path(char **path, struct tog_parent_trees *parents,
4855 struct got_tree_entry *te)
4857 const struct got_error *err = NULL;
4858 struct tog_parent_tree *pt;
4859 size_t len = 2; /* for leading slash and NUL */
4861 TAILQ_FOREACH(pt, parents, entry)
4862 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4865 len += strlen(got_tree_entry_get_name(te));
4867 *path = calloc(1, len);
4869 return got_error_from_errno("calloc");
4872 pt = TAILQ_LAST(parents, tog_parent_trees);
4874 const char *name = got_tree_entry_get_name(pt->selected_entry);
4875 if (strlcat(*path, name, len) >= len) {
4876 err = got_error(GOT_ERR_NO_SPACE);
4879 if (strlcat(*path, "/", len) >= len) {
4880 err = got_error(GOT_ERR_NO_SPACE);
4883 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4886 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4887 err = got_error(GOT_ERR_NO_SPACE);
4899 static const struct got_error *
4900 blame_tree_entry(struct tog_view **new_view, int begin_x,
4901 struct got_tree_entry *te, struct tog_parent_trees *parents,
4902 struct got_object_id *commit_id, struct got_reflist_head *refs,
4903 struct got_repository *repo)
4905 const struct got_error *err = NULL;
4907 struct tog_view *blame_view;
4911 err = tree_entry_path(&path, parents, te);
4915 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4916 if (blame_view == NULL) {
4917 err = got_error_from_errno("view_open");
4921 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4923 if (err->code == GOT_ERR_CANCELLED)
4925 view_close(blame_view);
4927 *new_view = blame_view;
4933 static const struct got_error *
4934 log_tree_entry(struct tog_view **new_view, int begin_x,
4935 struct got_tree_entry *te, struct tog_parent_trees *parents,
4936 struct got_object_id *commit_id, struct got_reflist_head *refs,
4937 struct got_repository *repo)
4939 struct tog_view *log_view;
4940 const struct got_error *err = NULL;
4945 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4946 if (log_view == NULL)
4947 return got_error_from_errno("view_open");
4949 err = tree_entry_path(&path, parents, te);
4953 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4955 view_close(log_view);
4957 *new_view = log_view;
4962 static const struct got_error *
4963 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4964 struct got_object_id *commit_id, struct got_reflist_head *refs,
4965 struct got_repository *repo)
4967 const struct got_error *err = NULL;
4968 char *commit_id_str = NULL;
4969 struct tog_tree_view_state *s = &view->state.tree;
4971 TAILQ_INIT(&s->parents);
4973 err = got_object_id_str(&commit_id_str, commit_id);
4977 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4978 err = got_error_from_errno("asprintf");
4982 s->root = s->tree = root;
4983 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4984 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4985 s->commit_id = got_object_id_dup(commit_id);
4986 if (s->commit_id == NULL) {
4987 err = got_error_from_errno("got_object_id_dup");
4993 SIMPLEQ_INIT(&s->colors);
4995 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4996 err = add_color(&s->colors, "\\$$",
4997 TOG_COLOR_TREE_SUBMODULE,
4998 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5001 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5002 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5004 free_colors(&s->colors);
5007 err = add_color(&s->colors, "/$",
5008 TOG_COLOR_TREE_DIRECTORY,
5009 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5011 free_colors(&s->colors);
5015 err = add_color(&s->colors, "\\*$",
5016 TOG_COLOR_TREE_EXECUTABLE,
5017 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5019 free_colors(&s->colors);
5023 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5024 get_color_value("TOG_COLOR_COMMIT"));
5026 free_colors(&s->colors);
5031 view->show = show_tree_view;
5032 view->input = input_tree_view;
5033 view->close = close_tree_view;
5034 view->search_start = search_start_tree_view;
5035 view->search_next = search_next_tree_view;
5037 free(commit_id_str);
5039 free(s->tree_label);
5040 s->tree_label = NULL;
5045 static const struct got_error *
5046 close_tree_view(struct tog_view *view)
5048 struct tog_tree_view_state *s = &view->state.tree;
5050 free_colors(&s->colors);
5051 free(s->tree_label);
5052 s->tree_label = NULL;
5054 s->commit_id = NULL;
5055 while (!TAILQ_EMPTY(&s->parents)) {
5056 struct tog_parent_tree *parent;
5057 parent = TAILQ_FIRST(&s->parents);
5058 TAILQ_REMOVE(&s->parents, parent, entry);
5062 if (s->tree != s->root)
5063 got_object_tree_close(s->tree);
5064 got_object_tree_close(s->root);
5069 static const struct got_error *
5070 search_start_tree_view(struct tog_view *view)
5072 struct tog_tree_view_state *s = &view->state.tree;
5074 s->matched_entry = NULL;
5079 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5081 regmatch_t regmatch;
5083 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5087 static const struct got_error *
5088 search_next_tree_view(struct tog_view *view)
5090 struct tog_tree_view_state *s = &view->state.tree;
5091 struct got_tree_entry *te = NULL;
5093 if (!view->searching) {
5094 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5098 if (s->matched_entry) {
5099 if (view->searching == TOG_SEARCH_FORWARD) {
5100 if (s->selected_entry)
5101 te = got_tree_entry_get_next(s->tree,
5104 te = got_object_tree_get_first_entry(s->tree);
5106 if (s->selected_entry == NULL)
5107 te = got_object_tree_get_last_entry(s->tree);
5109 te = got_tree_entry_get_prev(s->tree,
5113 if (view->searching == TOG_SEARCH_FORWARD)
5114 te = got_object_tree_get_first_entry(s->tree);
5116 te = got_object_tree_get_last_entry(s->tree);
5121 if (s->matched_entry == NULL) {
5122 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5125 if (view->searching == TOG_SEARCH_FORWARD)
5126 te = got_object_tree_get_first_entry(s->tree);
5128 te = got_object_tree_get_last_entry(s->tree);
5131 if (match_tree_entry(te, &view->regex)) {
5132 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5133 s->matched_entry = te;
5137 if (view->searching == TOG_SEARCH_FORWARD)
5138 te = got_tree_entry_get_next(s->tree, te);
5140 te = got_tree_entry_get_prev(s->tree, te);
5143 if (s->matched_entry) {
5144 s->first_displayed_entry = s->matched_entry;
5151 static const struct got_error *
5152 show_tree_view(struct tog_view *view)
5154 const struct got_error *err = NULL;
5155 struct tog_tree_view_state *s = &view->state.tree;
5158 err = tree_entry_path(&parent_path, &s->parents, NULL);
5162 err = draw_tree_entries(view, &s->first_displayed_entry,
5163 &s->last_displayed_entry, &s->selected_entry,
5164 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5165 s->tree, s->selected, view->nlines, s->tree == s->root,
5166 &s->colors, s->repo);
5173 static const struct got_error *
5174 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5175 struct tog_view **focus_view, struct tog_view *view, int ch)
5177 const struct got_error *err = NULL;
5178 struct tog_tree_view_state *s = &view->state.tree;
5179 struct tog_view *log_view;
5180 int begin_x = 0, nscrolled;
5184 s->show_ids = !s->show_ids;
5187 if (!s->selected_entry)
5189 if (view_is_parent_view(view))
5190 begin_x = view_split_begin_x(view->begin_x);
5191 err = log_tree_entry(&log_view, begin_x,
5192 s->selected_entry, &s->parents,
5193 s->commit_id, s->refs, s->repo);
5194 if (view_is_parent_view(view)) {
5195 err = view_close_child(view);
5198 err = view_set_child(view, log_view);
5200 view_close(log_view);
5203 *focus_view = log_view;
5204 view->child_focussed = 1;
5206 *new_view = log_view;
5210 if (s->selected > 0) {
5212 if (s->selected == 0)
5215 if (s->selected > 0)
5217 tree_scroll_up(view, &s->first_displayed_entry, 1,
5218 s->tree, s->tree == s->root);
5222 tree_scroll_up(view, &s->first_displayed_entry,
5223 MAX(0, view->nlines - 4 - s->selected), s->tree,
5224 s->tree == s->root);
5226 if (got_object_tree_get_first_entry(s->tree) ==
5227 s->first_displayed_entry && s->tree != s->root)
5228 s->first_displayed_entry = NULL;
5232 if (s->selected < s->ndisplayed - 1) {
5236 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5238 /* can't scroll any further */
5240 tree_scroll_down(&s->first_displayed_entry, 1,
5241 s->last_displayed_entry, s->tree);
5245 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5247 /* can't scroll any further; move cursor down */
5248 if (s->selected < s->ndisplayed - 1)
5249 s->selected = s->ndisplayed - 1;
5252 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5253 view->nlines, s->last_displayed_entry, s->tree);
5254 if (nscrolled < view->nlines) {
5256 struct got_tree_entry *te;
5257 te = s->first_displayed_entry;
5260 te = got_tree_entry_get_next(s->tree, te);
5262 s->selected = ndisplayed - 1;
5268 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5269 struct tog_parent_tree *parent;
5270 /* user selected '..' */
5271 if (s->tree == s->root)
5273 parent = TAILQ_FIRST(&s->parents);
5274 TAILQ_REMOVE(&s->parents, parent,
5276 got_object_tree_close(s->tree);
5277 s->tree = parent->tree;
5278 s->first_displayed_entry =
5279 parent->first_displayed_entry;
5281 parent->selected_entry;
5282 s->selected = parent->selected;
5284 } else if (S_ISDIR(got_tree_entry_get_mode(
5285 s->selected_entry))) {
5286 struct got_tree_object *subtree;
5287 err = got_object_open_as_tree(&subtree, s->repo,
5288 got_tree_entry_get_id(s->selected_entry));
5291 err = tree_view_visit_subtree(subtree, s);
5293 got_object_tree_close(subtree);
5296 } else if (S_ISREG(got_tree_entry_get_mode(
5297 s->selected_entry))) {
5298 struct tog_view *blame_view;
5299 int begin_x = view_is_parent_view(view) ?
5300 view_split_begin_x(view->begin_x) : 0;
5302 err = blame_tree_entry(&blame_view, begin_x,
5303 s->selected_entry, &s->parents,
5304 s->commit_id, s->refs, s->repo);
5307 if (view_is_parent_view(view)) {
5308 err = view_close_child(view);
5311 err = view_set_child(view, blame_view);
5313 view_close(blame_view);
5316 *focus_view = blame_view;
5317 view->child_focussed = 1;
5319 *new_view = blame_view;
5323 if (s->selected > view->nlines)
5324 s->selected = s->ndisplayed - 1;
5337 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5342 static const struct got_error *
5343 cmd_tree(int argc, char *argv[])
5345 const struct got_error *error;
5346 struct got_repository *repo = NULL;
5347 struct got_worktree *worktree = NULL;
5348 struct got_reflist_head refs;
5349 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5350 struct got_object_id *commit_id = NULL;
5351 char *commit_id_arg = NULL;
5352 struct got_commit_object *commit = NULL;
5353 struct got_tree_object *tree = NULL;
5355 struct tog_view *view;
5357 SIMPLEQ_INIT(&refs);
5360 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5365 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5368 commit_id_arg = optarg;
5371 repo_path = realpath(optarg, NULL);
5372 if (repo_path == NULL)
5373 return got_error_from_errno2("realpath",
5388 cwd = getcwd(NULL, 0);
5390 return got_error_from_errno("getcwd");
5392 error = got_worktree_open(&worktree, cwd);
5393 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5396 if (repo_path == NULL) {
5399 strdup(got_worktree_get_repo_path(worktree));
5401 repo_path = strdup(cwd);
5403 if (repo_path == NULL) {
5404 error = got_error_from_errno("strdup");
5408 error = got_repo_open(&repo, repo_path, NULL);
5412 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5419 error = apply_unveil(got_repo_get_path(repo), NULL);
5423 error = got_repo_match_object_id(&commit_id, NULL,
5424 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5425 GOT_OBJ_TYPE_COMMIT, 1, repo);
5429 error = got_object_open_as_commit(&commit, repo, commit_id);
5433 error = got_object_open_as_tree(&tree, repo,
5434 got_object_commit_get_tree_id(commit));
5438 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5442 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5444 error = got_error_from_errno("view_open");
5447 error = open_tree_view(view, tree, commit_id, &refs, repo);
5450 if (!got_path_is_root_dir(in_repo_path)) {
5451 error = tree_view_walk_path(&view->state.tree, commit_id,
5452 in_repo_path, repo);
5458 /* Release work tree lock. */
5459 got_worktree_close(worktree);
5462 error = view_loop(view);
5468 got_object_commit_close(commit);
5470 got_object_tree_close(tree);
5472 got_repo_close(repo);
5473 got_ref_list_free(&refs);
5478 list_commands(FILE *fp)
5482 fprintf(fp, "commands:");
5483 for (i = 0; i < nitems(tog_commands); i++) {
5484 struct tog_cmd *cmd = &tog_commands[i];
5485 fprintf(fp, " %s", cmd->name);
5491 usage(int hflag, int status)
5493 FILE *fp = (status == 0) ? stdout : stderr;
5495 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5498 fprintf(fp, "lazy usage: %s path\n", getprogname());
5505 make_argv(int argc, ...)
5513 argv = calloc(argc, sizeof(char *));
5516 for (i = 0; i < argc; i++) {
5517 argv[i] = strdup(va_arg(ap, char *));
5518 if (argv[i] == NULL)
5527 * Try to convert 'tog path' into a 'tog log path' command.
5528 * The user could simply have mistyped the command rather than knowingly
5529 * provided a path. So check whether argv[0] can in fact be resolved
5530 * to a path in the HEAD commit and print a special error if not.
5531 * This hack is for mpi@ <3
5533 static const struct got_error *
5534 tog_log_with_path(int argc, char *argv[])
5536 const struct got_error *error = NULL;
5537 struct tog_cmd *cmd = NULL;
5538 struct got_repository *repo = NULL;
5539 struct got_worktree *worktree = NULL;
5540 struct got_object_id *commit_id = NULL, *id = NULL;
5541 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5542 char *commit_id_str = NULL, **cmd_argv = NULL;
5544 cwd = getcwd(NULL, 0);
5546 return got_error_from_errno("getcwd");
5548 error = got_worktree_open(&worktree, cwd);
5549 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5553 repo_path = strdup(got_worktree_get_repo_path(worktree));
5555 repo_path = strdup(cwd);
5556 if (repo_path == NULL) {
5557 error = got_error_from_errno("strdup");
5561 error = got_repo_open(&repo, repo_path, NULL);
5565 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5570 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5571 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5572 GOT_OBJ_TYPE_COMMIT, 1, repo);
5577 got_worktree_close(worktree);
5581 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5583 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5585 fprintf(stderr, "%s: '%s' is no known command or path\n",
5586 getprogname(), argv[0]);
5591 got_repo_close(repo);
5594 error = got_object_id_str(&commit_id_str, commit_id);
5598 cmd = &tog_commands[0]; /* log */
5600 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5601 error = cmd->cmd_main(argc, cmd_argv);
5604 got_repo_close(repo);
5606 got_worktree_close(worktree);
5608 free(commit_id_str);
5615 for (i = 0; i < argc; i++)
5623 main(int argc, char *argv[])
5625 const struct got_error *error = NULL;
5626 struct tog_cmd *cmd = NULL;
5627 int ch, hflag = 0, Vflag = 0;
5628 char **cmd_argv = NULL;
5629 static struct option longopts[] = {
5630 { "version", no_argument, NULL, 'V' },
5634 setlocale(LC_CTYPE, "");
5636 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5656 got_version_print_str();
5663 /* Build an argument vector which runs a default command. */
5664 cmd = &tog_commands[0];
5666 cmd_argv = make_argv(argc, cmd->name);
5670 /* Did the user specify a command? */
5671 for (i = 0; i < nitems(tog_commands); i++) {
5672 if (strncmp(tog_commands[i].name, argv[0],
5673 strlen(argv[0])) == 0) {
5674 cmd = &tog_commands[i];
5683 /* No command specified; try log with a path */
5684 error = tog_log_with_path(argc, argv);
5689 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5696 for (i = 0; i < argc; i++)
5701 if (error && error->code != GOT_ERR_CANCELLED)
5702 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);