2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_opentemp.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
56 #include "got_worktree.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define CTRL(x) ((x) & 0x1f)
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
78 __dead static void usage(int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
84 static const struct got_error* cmd_log(int, char *[]);
85 static const struct got_error* cmd_diff(int, char *[]);
86 static const struct got_error* cmd_blame(int, char *[]);
87 static const struct got_error* cmd_tree(int, char *[]);
89 static struct tog_cmd tog_commands[] = {
90 { "log", cmd_log, usage_log },
91 { "diff", cmd_diff, usage_diff },
92 { "blame", cmd_blame, usage_blame },
93 { "tree", cmd_tree, usage_tree },
103 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
114 struct commit_queue_head head;
118 SIMPLEQ_ENTRY(tog_color) entry;
122 SIMPLEQ_HEAD(tog_colors, tog_color);
124 static const struct got_error *
125 add_color(struct tog_colors *colors, const char *pattern,
126 int idx, short color)
128 const struct got_error *err = NULL;
129 struct tog_color *tc;
132 if (idx < 1 || idx > COLOR_PAIRS - 1)
135 init_pair(idx, color, -1);
137 tc = calloc(1, sizeof(*tc));
139 return got_error_from_errno("calloc");
140 regerr = regcomp(&tc->regex, pattern,
141 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
143 static char regerr_msg[512];
144 static char err_msg[512];
145 regerror(regerr, &tc->regex, regerr_msg,
147 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
149 err = got_error_msg(GOT_ERR_REGEX, err_msg);
154 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
159 free_colors(struct tog_colors *colors)
161 struct tog_color *tc;
163 while (!SIMPLEQ_EMPTY(colors)) {
164 tc = SIMPLEQ_FIRST(colors);
165 SIMPLEQ_REMOVE_HEAD(colors, entry);
172 get_color(struct tog_colors *colors, int colorpair)
174 struct tog_color *tc = NULL;
176 SIMPLEQ_FOREACH(tc, colors, entry) {
177 if (tc->colorpair == colorpair)
185 default_color_value(const char *envvar)
187 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
188 return COLOR_MAGENTA;
189 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
193 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
195 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
196 return COLOR_MAGENTA;
197 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
198 return COLOR_MAGENTA;
199 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
201 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
203 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
205 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
207 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
214 get_color_value(const char *envvar)
216 const char *val = getenv(envvar);
219 return default_color_value(envvar);
221 if (strcasecmp(val, "black") == 0)
223 if (strcasecmp(val, "red") == 0)
225 if (strcasecmp(val, "green") == 0)
227 if (strcasecmp(val, "yellow") == 0)
229 if (strcasecmp(val, "blue") == 0)
231 if (strcasecmp(val, "magenta") == 0)
232 return COLOR_MAGENTA;
233 if (strcasecmp(val, "cyan") == 0)
235 if (strcasecmp(val, "white") == 0)
237 if (strcasecmp(val, "default") == 0)
240 return default_color_value(envvar);
244 struct tog_diff_view_state {
245 struct got_object_id *id1, *id2;
247 int first_displayed_line;
248 int last_displayed_line;
251 struct got_repository *repo;
252 struct got_reflist_head *refs;
253 struct tog_colors colors;
260 /* passed from log view; may be NULL */
261 struct tog_view *log_view;
264 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 struct tog_log_thread_args {
267 pthread_cond_t need_commits;
268 pthread_cond_t commit_loaded;
270 struct got_commit_graph *graph;
271 struct commit_queue *commits;
272 const char *in_repo_path;
273 struct got_object_id *start_id;
274 struct got_repository *repo;
277 struct commit_queue_entry **first_displayed_entry;
278 struct commit_queue_entry **selected_entry;
280 int *search_next_done;
284 struct tog_log_view_state {
285 struct commit_queue commits;
286 struct commit_queue_entry *first_displayed_entry;
287 struct commit_queue_entry *last_displayed_entry;
288 struct commit_queue_entry *selected_entry;
291 const char *head_ref_name;
293 struct got_repository *repo;
294 struct got_reflist_head *refs;
295 struct got_object_id *start_id;
298 struct tog_log_thread_args thread_args;
299 struct commit_queue_entry *matched_entry;
300 struct commit_queue_entry *search_entry;
301 struct tog_colors colors;
304 #define TOG_COLOR_DIFF_MINUS 1
305 #define TOG_COLOR_DIFF_PLUS 2
306 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
307 #define TOG_COLOR_DIFF_META 4
308 #define TOG_COLOR_TREE_SUBMODULE 5
309 #define TOG_COLOR_TREE_SYMLINK 6
310 #define TOG_COLOR_TREE_DIRECTORY 7
311 #define TOG_COLOR_TREE_EXECUTABLE 8
312 #define TOG_COLOR_COMMIT 9
313 #define TOG_COLOR_AUTHOR 10
314 #define TOG_COLOR_DATE 11
316 struct tog_blame_cb_args {
317 struct tog_blame_line *lines; /* one per line */
320 struct tog_view *view;
321 struct got_object_id *commit_id;
325 struct tog_blame_thread_args {
327 struct got_repository *repo;
328 struct tog_blame_cb_args *cb_args;
330 got_cancel_cb cancel_cb;
337 struct tog_blame_line *lines;
341 struct tog_blame_thread_args thread_args;
342 struct tog_blame_cb_args cb_args;
346 struct tog_blame_view_state {
347 int first_displayed_line;
348 int last_displayed_line;
353 struct got_object_id_queue blamed_commits;
354 struct got_object_qid *blamed_commit;
356 struct got_repository *repo;
357 struct got_reflist_head *refs;
358 struct got_object_id *commit_id;
359 struct tog_blame blame;
361 struct tog_colors colors;
364 struct tog_parent_tree {
365 TAILQ_ENTRY(tog_parent_tree) entry;
366 struct got_tree_object *tree;
367 struct got_tree_entry *first_displayed_entry;
368 struct got_tree_entry *selected_entry;
372 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
374 struct tog_tree_view_state {
376 struct got_tree_object *root;
377 struct got_tree_object *tree;
378 struct got_tree_entry *first_displayed_entry;
379 struct got_tree_entry *last_displayed_entry;
380 struct got_tree_entry *selected_entry;
381 int ndisplayed, selected, show_ids;
382 struct tog_parent_trees parents;
383 struct got_object_id *commit_id;
384 struct got_repository *repo;
385 struct got_reflist_head *refs;
386 struct got_tree_entry *matched_entry;
387 struct tog_colors colors;
391 * We implement two types of views: parent views and child views.
393 * The 'Tab' key switches between a parent view and its child view.
394 * Child views are shown side-by-side to their parent view, provided
395 * there is enough screen estate.
397 * When a new view is opened from within a parent view, this new view
398 * becomes a child view of the parent view, replacing any existing child.
400 * When a new view is opened from within a child view, this new view
401 * becomes a parent view which will obscure the views below until the
402 * user quits the new parent view by typing 'q'.
404 * This list of views contains parent views only.
405 * Child views are only pointed to by their parent view.
407 TAILQ_HEAD(tog_view_list_head, tog_view);
410 TAILQ_ENTRY(tog_view) entry;
413 int nlines, ncols, begin_y, begin_x;
414 int lines, cols; /* copies of LINES and COLS */
416 struct tog_view *parent;
417 struct tog_view *child;
420 /* type-specific state */
421 enum tog_view_type type;
423 struct tog_diff_view_state diff;
424 struct tog_log_view_state log;
425 struct tog_blame_view_state blame;
426 struct tog_tree_view_state tree;
429 const struct got_error *(*show)(struct tog_view *);
430 const struct got_error *(*input)(struct tog_view **,
431 struct tog_view **, struct tog_view**, struct tog_view *, int);
432 const struct got_error *(*close)(struct tog_view *);
434 const struct got_error *(*search_start)(struct tog_view *);
435 const struct got_error *(*search_next)(struct tog_view *);
437 #define TOG_SEARCH_FORWARD 1
438 #define TOG_SEARCH_BACKWARD 2
439 int search_next_done;
440 #define TOG_SEARCH_HAVE_MORE 1
441 #define TOG_SEARCH_NO_MORE 2
442 #define TOG_SEARCH_HAVE_NONE 3
446 static const struct got_error *open_diff_view(struct tog_view *,
447 struct got_object_id *, struct got_object_id *, struct tog_view *,
448 struct got_reflist_head *, struct got_repository *);
449 static const struct got_error *show_diff_view(struct tog_view *);
450 static const struct got_error *input_diff_view(struct tog_view **,
451 struct tog_view **, struct tog_view **, struct tog_view *, int);
452 static const struct got_error* close_diff_view(struct tog_view *);
453 static const struct got_error *search_start_diff_view(struct tog_view *);
454 static const struct got_error *search_next_diff_view(struct tog_view *);
456 static const struct got_error *open_log_view(struct tog_view *,
457 struct got_object_id *, struct got_reflist_head *,
458 struct got_repository *, const char *, const char *, int);
459 static const struct got_error * show_log_view(struct tog_view *);
460 static const struct got_error *input_log_view(struct tog_view **,
461 struct tog_view **, struct tog_view **, struct tog_view *, int);
462 static const struct got_error *close_log_view(struct tog_view *);
463 static const struct got_error *search_start_log_view(struct tog_view *);
464 static const struct got_error *search_next_log_view(struct tog_view *);
466 static const struct got_error *open_blame_view(struct tog_view *, char *,
467 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
468 static const struct got_error *show_blame_view(struct tog_view *);
469 static const struct got_error *input_blame_view(struct tog_view **,
470 struct tog_view **, struct tog_view **, struct tog_view *, int);
471 static const struct got_error *close_blame_view(struct tog_view *);
472 static const struct got_error *search_start_blame_view(struct tog_view *);
473 static const struct got_error *search_next_blame_view(struct tog_view *);
475 static const struct got_error *open_tree_view(struct tog_view *,
476 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
477 struct got_repository *);
478 static const struct got_error *show_tree_view(struct tog_view *);
479 static const struct got_error *input_tree_view(struct tog_view **,
480 struct tog_view **, struct tog_view **, struct tog_view *, int);
481 static const struct got_error *close_tree_view(struct tog_view *);
482 static const struct got_error *search_start_tree_view(struct tog_view *);
483 static const struct got_error *search_next_tree_view(struct tog_view *);
485 static volatile sig_atomic_t tog_sigwinch_received;
486 static volatile sig_atomic_t tog_sigpipe_received;
487 static volatile sig_atomic_t tog_sigcont_received;
490 tog_sigwinch(int signo)
492 tog_sigwinch_received = 1;
496 tog_sigpipe(int signo)
498 tog_sigpipe_received = 1;
502 tog_sigcont(int signo)
504 tog_sigcont_received = 1;
507 static const struct got_error *
508 view_close(struct tog_view *view)
510 const struct got_error *err = NULL;
513 view_close(view->child);
517 err = view->close(view);
519 del_panel(view->panel);
521 delwin(view->window);
526 static struct tog_view *
527 view_open(int nlines, int ncols, int begin_y, int begin_x,
528 enum tog_view_type type)
530 struct tog_view *view = calloc(1, sizeof(*view));
538 view->nlines = nlines ? nlines : LINES - begin_y;
539 view->ncols = ncols ? ncols : COLS - begin_x;
540 view->begin_y = begin_y;
541 view->begin_x = begin_x;
542 view->window = newwin(nlines, ncols, begin_y, begin_x);
543 if (view->window == NULL) {
547 view->panel = new_panel(view->window);
548 if (view->panel == NULL ||
549 set_panel_userptr(view->panel, view) != OK) {
554 keypad(view->window, TRUE);
559 view_split_begin_x(int begin_x)
561 if (begin_x > 0 || COLS < 120)
563 return (COLS - MAX(COLS / 2, 80));
566 static const struct got_error *view_resize(struct tog_view *);
568 static const struct got_error *
569 view_splitscreen(struct tog_view *view)
571 const struct got_error *err = NULL;
574 view->begin_x = view_split_begin_x(0);
575 view->nlines = LINES;
576 view->ncols = COLS - view->begin_x;
579 err = view_resize(view);
583 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
584 return got_error_from_errno("mvwin");
589 static const struct got_error *
590 view_fullscreen(struct tog_view *view)
592 const struct got_error *err = NULL;
596 view->nlines = LINES;
600 err = view_resize(view);
604 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
605 return got_error_from_errno("mvwin");
611 view_is_parent_view(struct tog_view *view)
613 return view->parent == NULL;
616 static const struct got_error *
617 view_resize(struct tog_view *view)
621 if (view->lines > LINES)
622 nlines = view->nlines - (view->lines - LINES);
624 nlines = view->nlines + (LINES - view->lines);
626 if (view->cols > COLS)
627 ncols = view->ncols - (view->cols - COLS);
629 ncols = view->ncols + (COLS - view->cols);
631 if (wresize(view->window, nlines, ncols) == ERR)
632 return got_error_from_errno("wresize");
633 if (replace_panel(view->panel, view->window) == ERR)
634 return got_error_from_errno("replace_panel");
635 wclear(view->window);
637 view->nlines = nlines;
643 view->child->begin_x = view_split_begin_x(view->begin_x);
644 if (view->child->begin_x == 0) {
645 view_fullscreen(view->child);
646 if (view->child->focussed)
647 show_panel(view->child->panel);
649 show_panel(view->panel);
651 view_splitscreen(view->child);
652 show_panel(view->child->panel);
659 static const struct got_error *
660 view_close_child(struct tog_view *view)
662 const struct got_error *err = NULL;
664 if (view->child == NULL)
667 err = view_close(view->child);
672 static const struct got_error *
673 view_set_child(struct tog_view *view, struct tog_view *child)
675 const struct got_error *err = NULL;
678 child->parent = view;
683 view_is_splitscreen(struct tog_view *view)
685 return view->begin_x > 0;
694 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
695 cols = 80; /* Default */
701 resize_term(lines, cols);
704 static const struct got_error *
705 view_search_start(struct tog_view *view)
707 const struct got_error *err = NULL;
711 if (view->nlines < 1)
714 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
715 wclrtoeol(view->window);
719 ret = wgetnstr(view->window, pattern, sizeof(pattern));
725 if (view->searching) {
726 regfree(&view->regex);
730 if (regcomp(&view->regex, pattern,
731 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
732 err = view->search_start(view);
734 regfree(&view->regex);
737 view->searching = TOG_SEARCH_FORWARD;
738 view->search_next_done = 0;
739 view->search_next(view);
745 static const struct got_error *
746 view_input(struct tog_view **new, struct tog_view **dead,
747 struct tog_view **focus, int *done, struct tog_view *view,
748 struct tog_view_list_head *views)
750 const struct got_error *err = NULL;
758 /* Clear "no matches" indicator. */
759 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
760 view->search_next_done == TOG_SEARCH_HAVE_NONE)
761 view->search_next_done = TOG_SEARCH_HAVE_MORE;
763 if (view->searching && !view->search_next_done) {
764 errcode = pthread_mutex_unlock(&tog_mutex);
766 return got_error_set_errno(errcode,
767 "pthread_mutex_unlock");
769 errcode = pthread_mutex_lock(&tog_mutex);
771 return got_error_set_errno(errcode,
772 "pthread_mutex_lock");
773 view->search_next(view);
777 nodelay(stdscr, FALSE);
778 /* Allow threads to make progress while we are waiting for input. */
779 errcode = pthread_mutex_unlock(&tog_mutex);
781 return got_error_set_errno(errcode, "pthread_mutex_unlock");
782 ch = wgetch(view->window);
783 errcode = pthread_mutex_lock(&tog_mutex);
785 return got_error_set_errno(errcode, "pthread_mutex_lock");
786 nodelay(stdscr, TRUE);
788 if (tog_sigwinch_received || tog_sigcont_received) {
790 tog_sigwinch_received = 0;
791 tog_sigcont_received = 0;
792 TAILQ_FOREACH(v, views, entry) {
793 err = view_resize(v);
796 err = v->input(new, dead, focus, v, KEY_RESIZE);
807 *focus = view->child;
808 view->child_focussed = 1;
809 } else if (view->parent) {
810 *focus = view->parent;
811 view->parent->child_focussed = 0;
815 err = view->input(new, dead, focus, view, ch);
822 if (view_is_parent_view(view)) {
823 if (view->child == NULL)
825 if (view_is_splitscreen(view->child)) {
826 *focus = view->child;
827 view->child_focussed = 1;
828 err = view_fullscreen(view->child);
830 err = view_splitscreen(view->child);
833 err = view->child->input(new, dead, focus,
834 view->child, KEY_RESIZE);
836 if (view_is_splitscreen(view)) {
838 view->parent->child_focussed = 1;
839 err = view_fullscreen(view);
841 err = view_splitscreen(view);
845 err = view->input(new, dead, focus, view,
852 if (view->search_start)
853 view_search_start(view);
855 err = view->input(new, dead, focus, view, ch);
859 if (view->search_next) {
860 view->searching = (ch == 'n' ?
861 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
862 view->search_next_done = 0;
863 view->search_next(view);
865 err = view->input(new, dead, focus, view, ch);
868 err = view->input(new, dead, focus, view, ch);
876 view_vborder(struct tog_view *view)
879 struct tog_view *view_above;
882 return view_vborder(view->parent);
884 panel = panel_above(view->panel);
888 view_above = panel_userptr(panel);
889 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
890 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
894 view_needs_focus_indication(struct tog_view *view)
896 if (view_is_parent_view(view)) {
897 if (view->child == NULL || view->child_focussed)
899 if (!view_is_splitscreen(view->child))
901 } else if (!view_is_splitscreen(view))
904 return view->focussed;
907 static const struct got_error *
908 view_loop(struct tog_view *view)
910 const struct got_error *err = NULL;
911 struct tog_view_list_head views;
912 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
913 int fast_refresh = 10;
914 int done = 0, errcode;
916 errcode = pthread_mutex_lock(&tog_mutex);
918 return got_error_set_errno(errcode, "pthread_mutex_lock");
921 TAILQ_INSERT_HEAD(&views, view, entry);
925 err = view->show(view);
930 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
931 /* Refresh fast during initialization, then become slower. */
932 if (fast_refresh && fast_refresh-- == 0)
933 halfdelay(10); /* switch to once per second */
935 err = view_input(&new_view, &dead_view, &focus_view, &done,
940 struct tog_view *prev = NULL;
942 if (view_is_parent_view(dead_view))
943 prev = TAILQ_PREV(dead_view,
944 tog_view_list_head, entry);
945 else if (view->parent != dead_view)
948 if (dead_view->parent)
949 dead_view->parent->child = NULL;
951 TAILQ_REMOVE(&views, dead_view, entry);
953 err = view_close(dead_view);
954 if (err || (dead_view == main_view && new_view == NULL))
957 if (view == dead_view) {
962 else if (!TAILQ_EMPTY(&views))
963 view = TAILQ_LAST(&views,
968 if (view->child && view->child_focussed)
969 focus_view = view->child;
976 struct tog_view *v, *t;
977 /* Only allow one parent view per type. */
978 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
979 if (v->type != new_view->type)
981 TAILQ_REMOVE(&views, v, entry);
987 TAILQ_INSERT_TAIL(&views, new_view, entry);
989 if (focus_view == NULL)
990 focus_view = new_view;
993 show_panel(focus_view->panel);
996 focus_view->focussed = 1;
999 show_panel(new_view->panel);
1000 if (view->child && view_is_splitscreen(view->child))
1001 show_panel(view->child->panel);
1004 if (focus_view == NULL) {
1006 show_panel(view->panel);
1007 if (view->child && view_is_splitscreen(view->child))
1008 show_panel(view->child->panel);
1012 err = view->parent->show(view->parent);
1016 err = view->show(view);
1020 err = view->child->show(view->child);
1029 while (!TAILQ_EMPTY(&views)) {
1030 view = TAILQ_FIRST(&views);
1031 TAILQ_REMOVE(&views, view, entry);
1035 errcode = pthread_mutex_unlock(&tog_mutex);
1036 if (errcode && err == NULL)
1037 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1047 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1052 /* Create newly allocated wide-character string equivalent to a byte string. */
1053 static const struct got_error *
1054 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1057 const struct got_error *err = NULL;
1060 *wlen = mbstowcs(NULL, s, 0);
1061 if (*wlen == (size_t)-1) {
1063 if (errno != EILSEQ)
1064 return got_error_from_errno("mbstowcs");
1066 /* byte string invalid in current encoding; try to "fix" it */
1067 err = got_mbsavis(&vis, &vislen, s);
1070 *wlen = mbstowcs(NULL, vis, 0);
1071 if (*wlen == (size_t)-1) {
1072 err = got_error_from_errno("mbstowcs"); /* give up */
1077 *ws = calloc(*wlen + 1, sizeof(**ws));
1079 err = got_error_from_errno("calloc");
1083 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1084 err = got_error_from_errno("mbstowcs");
1095 /* Format a line for display, ensuring that it won't overflow a width limit. */
1096 static const struct got_error *
1097 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1100 const struct got_error *err = NULL;
1102 wchar_t *wline = NULL;
1109 err = mbs2ws(&wline, &wlen, line);
1115 int width = wcwidth(wline[i]);
1122 if (width == 1 || width == 2) {
1123 if (cols + width > wlimit)
1127 } else if (width == -1) {
1128 if (wline[i] == L'\t') {
1130 ((cols + col_tab_align) % TABSIZE);
1131 if (cols + width > wlimit)
1137 err = got_error_from_errno("wcwidth");
1152 static const struct got_error*
1153 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1154 struct got_object_id *id, struct got_repository *repo)
1156 static const struct got_error *err = NULL;
1157 struct got_reflist_entry *re;
1163 SIMPLEQ_FOREACH(re, refs, entry) {
1164 struct got_tag_object *tag = NULL;
1167 name = got_ref_get_name(re->ref);
1168 if (strcmp(name, GOT_REF_HEAD) == 0)
1170 if (strncmp(name, "refs/", 5) == 0)
1172 if (strncmp(name, "got/", 4) == 0)
1174 if (strncmp(name, "heads/", 6) == 0)
1176 if (strncmp(name, "remotes/", 8) == 0) {
1178 s = strstr(name, "/" GOT_REF_HEAD);
1179 if (s != NULL && s[strlen(s)] == '\0')
1182 if (strncmp(name, "tags/", 5) == 0) {
1183 err = got_object_open_as_tag(&tag, repo, re->id);
1185 if (err->code != GOT_ERR_OBJ_TYPE)
1187 /* Ref points at something other than a tag. */
1192 cmp = got_object_id_cmp(tag ?
1193 got_object_tag_get_object_id(tag) : re->id, id);
1195 got_object_tag_close(tag);
1199 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1200 s ? ", " : "", name) == -1) {
1201 err = got_error_from_errno("asprintf");
1212 static const struct got_error *
1213 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1216 char *smallerthan, *at;
1218 smallerthan = strchr(author, '<');
1219 if (smallerthan && smallerthan[1] != '\0')
1220 author = smallerthan + 1;
1221 at = strchr(author, '@');
1224 return format_line(wauthor, author_width, author, limit, col_tab_align);
1227 static const struct got_error *
1228 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1229 struct got_object_id *id, struct got_reflist_head *refs,
1230 const size_t date_display_cols, int author_display_cols,
1231 struct tog_colors *colors)
1233 const struct got_error *err = NULL;
1234 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1235 char *logmsg0 = NULL, *logmsg = NULL;
1236 char *author = NULL;
1237 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1238 int author_width, logmsg_width;
1239 char *newline, *line = NULL;
1241 const int avail = view->ncols;
1243 time_t committer_time;
1244 struct tog_color *tc;
1246 committer_time = got_object_commit_get_committer_time(commit);
1247 if (localtime_r(&committer_time, &tm) == NULL)
1248 return got_error_from_errno("localtime_r");
1249 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1251 return got_error(GOT_ERR_NO_SPACE);
1253 if (avail <= date_display_cols)
1254 limit = MIN(sizeof(datebuf) - 1, avail);
1256 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1257 tc = get_color(colors, TOG_COLOR_DATE);
1259 wattr_on(view->window,
1260 COLOR_PAIR(tc->colorpair), NULL);
1261 waddnstr(view->window, datebuf, limit);
1263 wattr_off(view->window,
1264 COLOR_PAIR(tc->colorpair), NULL);
1271 err = got_object_id_str(&id_str, id);
1274 tc = get_color(colors, TOG_COLOR_COMMIT);
1276 wattr_on(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1278 wprintw(view->window, "%.8s ", id_str);
1280 wattr_off(view->window,
1281 COLOR_PAIR(tc->colorpair), NULL);
1288 author = strdup(got_object_commit_get_author(commit));
1289 if (author == NULL) {
1290 err = got_error_from_errno("strdup");
1293 err = format_author(&wauthor, &author_width, author, avail - col, col);
1296 tc = get_color(colors, TOG_COLOR_AUTHOR);
1298 wattr_on(view->window,
1299 COLOR_PAIR(tc->colorpair), NULL);
1300 waddwstr(view->window, wauthor);
1302 wattr_off(view->window,
1303 COLOR_PAIR(tc->colorpair), NULL);
1304 col += author_width;
1305 while (col < avail && author_width < author_display_cols + 2) {
1306 waddch(view->window, ' ');
1313 err = got_object_commit_get_logmsg(&logmsg0, commit);
1317 while (*logmsg == '\n')
1319 newline = strchr(logmsg, '\n');
1322 limit = avail - col;
1323 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1326 waddwstr(view->window, wlogmsg);
1327 col += logmsg_width;
1328 while (col < avail) {
1329 waddch(view->window, ' ');
1341 static struct commit_queue_entry *
1342 alloc_commit_queue_entry(struct got_commit_object *commit,
1343 struct got_object_id *id)
1345 struct commit_queue_entry *entry;
1347 entry = calloc(1, sizeof(*entry));
1352 entry->commit = commit;
1357 pop_commit(struct commit_queue *commits)
1359 struct commit_queue_entry *entry;
1361 entry = TAILQ_FIRST(&commits->head);
1362 TAILQ_REMOVE(&commits->head, entry, entry);
1363 got_object_commit_close(entry->commit);
1364 commits->ncommits--;
1365 /* Don't free entry->id! It is owned by the commit graph. */
1370 free_commits(struct commit_queue *commits)
1372 while (!TAILQ_EMPTY(&commits->head))
1373 pop_commit(commits);
1376 static const struct got_error *
1377 match_commit(int *have_match, struct got_object_id *id,
1378 struct got_commit_object *commit, regex_t *regex)
1380 const struct got_error *err = NULL;
1381 regmatch_t regmatch;
1382 char *id_str = NULL, *logmsg = NULL;
1386 err = got_object_id_str(&id_str, id);
1390 err = got_object_commit_get_logmsg(&logmsg, commit);
1394 if (regexec(regex, got_object_commit_get_author(commit), 1,
1395 ®match, 0) == 0 ||
1396 regexec(regex, got_object_commit_get_committer(commit), 1,
1397 ®match, 0) == 0 ||
1398 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1399 regexec(regex, logmsg, 1, ®match, 0) == 0)
1407 static const struct got_error *
1408 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1409 int minqueue, struct got_repository *repo, const char *path,
1410 int *searching, int *search_next_done, regex_t *regex)
1412 const struct got_error *err = NULL;
1416 * We keep all commits open throughout the lifetime of the log
1417 * view in order to avoid having to re-fetch commits from disk
1418 * while updating the display.
1420 while (nqueued < minqueue ||
1421 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1422 struct got_object_id *id;
1423 struct got_commit_object *commit;
1424 struct commit_queue_entry *entry;
1427 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1428 if (err || id == NULL)
1431 err = got_object_open_as_commit(&commit, repo, id);
1434 entry = alloc_commit_queue_entry(commit, id);
1435 if (entry == NULL) {
1436 err = got_error_from_errno("alloc_commit_queue_entry");
1440 errcode = pthread_mutex_lock(&tog_mutex);
1442 err = got_error_set_errno(errcode,
1443 "pthread_mutex_lock");
1447 entry->idx = commits->ncommits;
1448 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1450 commits->ncommits++;
1452 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1454 err = match_commit(&have_match, id, commit, regex);
1458 *search_next_done = TOG_SEARCH_HAVE_MORE;
1461 errcode = pthread_mutex_unlock(&tog_mutex);
1462 if (errcode && err == NULL)
1463 err = got_error_set_errno(errcode,
1464 "pthread_mutex_unlock");
1472 static const struct got_error *
1473 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1474 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1475 struct commit_queue *commits, int selected_idx, int limit,
1476 struct got_reflist_head *refs, const char *path, int commits_needed,
1477 struct tog_colors *colors)
1479 const struct got_error *err = NULL;
1480 struct tog_log_view_state *s = &view->state.log;
1481 struct commit_queue_entry *entry;
1483 int ncommits, author_cols = 4;
1484 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1485 char *refs_str = NULL;
1487 struct tog_color *tc;
1488 static const size_t date_display_cols = 12;
1493 if (ncommits == selected_idx) {
1497 entry = TAILQ_NEXT(entry, entry);
1501 if (*selected && !(view->searching && view->search_next_done == 0)) {
1502 err = got_object_id_str(&id_str, (*selected)->id);
1506 err = build_refs_str(&refs_str, refs, (*selected)->id,
1513 if (commits_needed == 0)
1514 halfdelay(10); /* disable fast refresh */
1516 if (commits_needed > 0) {
1517 if (asprintf(&ncommits_str, " [%d/%d] %s",
1518 entry ? entry->idx + 1 : 0, commits->ncommits,
1519 (view->searching && !view->search_next_done) ?
1520 "searching..." : "loading...") == -1) {
1521 err = got_error_from_errno("asprintf");
1525 const char *search_str = NULL;
1527 if (view->searching) {
1528 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1529 search_str = "no more matches";
1530 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1531 search_str = "no matches found";
1532 else if (!view->search_next_done)
1533 search_str = "searching...";
1536 if (asprintf(&ncommits_str, " [%d/%d] %s",
1537 entry ? entry->idx + 1 : 0, commits->ncommits,
1538 search_str ? search_str :
1539 (refs_str ? refs_str : "")) == -1) {
1540 err = got_error_from_errno("asprintf");
1545 if (path && strcmp(path, "/") != 0) {
1546 if (asprintf(&header, "commit %s %s%s",
1547 id_str ? id_str : "........................................",
1548 path, ncommits_str) == -1) {
1549 err = got_error_from_errno("asprintf");
1553 } else if (asprintf(&header, "commit %s%s",
1554 id_str ? id_str : "........................................",
1555 ncommits_str) == -1) {
1556 err = got_error_from_errno("asprintf");
1560 err = format_line(&wline, &width, header, view->ncols, 0);
1564 werase(view->window);
1566 if (view_needs_focus_indication(view))
1567 wstandout(view->window);
1568 tc = get_color(colors, TOG_COLOR_COMMIT);
1570 wattr_on(view->window,
1571 COLOR_PAIR(tc->colorpair), NULL);
1572 waddwstr(view->window, wline);
1574 wattr_off(view->window,
1575 COLOR_PAIR(tc->colorpair), NULL);
1576 while (width < view->ncols) {
1577 waddch(view->window, ' ');
1580 if (view_needs_focus_indication(view))
1581 wstandend(view->window);
1586 /* Grow author column size if necessary. */
1593 if (ncommits >= limit - 1)
1595 author = strdup(got_object_commit_get_author(entry->commit));
1596 if (author == NULL) {
1597 err = got_error_from_errno("strdup");
1600 err = format_author(&wauthor, &width, author, COLS,
1602 if (author_cols < width)
1603 author_cols = width;
1607 entry = TAILQ_NEXT(entry, entry);
1614 if (ncommits >= limit - 1)
1616 if (ncommits == selected_idx)
1617 wstandout(view->window);
1618 err = draw_commit(view, entry->commit, entry->id, refs,
1619 date_display_cols, author_cols, colors);
1620 if (ncommits == selected_idx)
1621 wstandend(view->window);
1626 entry = TAILQ_NEXT(entry, entry);
1639 scroll_up(struct tog_view *view,
1640 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1641 struct commit_queue *commits)
1643 struct commit_queue_entry *entry;
1646 entry = TAILQ_FIRST(&commits->head);
1647 if (*first_displayed_entry == entry)
1650 entry = *first_displayed_entry;
1651 while (entry && nscrolled < maxscroll) {
1652 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1654 *first_displayed_entry = entry;
1660 static const struct got_error *
1661 trigger_log_thread(struct tog_view *log_view, int wait,
1662 int *commits_needed, int *log_complete,
1663 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1667 halfdelay(1); /* fast refresh while loading commits */
1669 while (*commits_needed > 0) {
1673 /* Wake the log thread. */
1674 errcode = pthread_cond_signal(need_commits);
1676 return got_error_set_errno(errcode,
1677 "pthread_cond_signal");
1680 * The mutex will be released while the view loop waits
1681 * in wgetch(), at which time the log thread will run.
1686 /* Display progress update in log view. */
1687 show_log_view(log_view);
1691 /* Wait right here while next commit is being loaded. */
1692 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1694 return got_error_set_errno(errcode,
1695 "pthread_cond_wait");
1697 /* Display progress update in log view. */
1698 show_log_view(log_view);
1706 static const struct got_error *
1707 scroll_down(struct tog_view *log_view,
1708 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1709 struct commit_queue_entry **last_displayed_entry,
1710 struct commit_queue *commits, int *log_complete, int *commits_needed,
1711 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1713 const struct got_error *err = NULL;
1714 struct commit_queue_entry *pentry;
1715 int nscrolled = 0, ncommits_needed;
1717 if (*last_displayed_entry == NULL)
1720 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1721 if (commits->ncommits < ncommits_needed && !*log_complete) {
1723 * Ask the log thread for required amount of commits.
1725 (*commits_needed) += maxscroll;
1726 err = trigger_log_thread(log_view, 1, commits_needed,
1727 log_complete, need_commits, commit_loaded);
1733 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1737 *last_displayed_entry = pentry;
1739 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1742 *first_displayed_entry = pentry;
1743 } while (++nscrolled < maxscroll);
1748 static const struct got_error *
1749 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1750 struct got_commit_object *commit, struct got_object_id *commit_id,
1751 struct tog_view *log_view, struct got_reflist_head *refs,
1752 struct got_repository *repo)
1754 const struct got_error *err;
1755 struct got_object_qid *parent_id;
1756 struct tog_view *diff_view;
1758 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1759 if (diff_view == NULL)
1760 return got_error_from_errno("view_open");
1762 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1763 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1764 commit_id, log_view, refs, repo);
1766 *new_view = diff_view;
1770 static const struct got_error *
1771 tree_view_visit_subtree(struct got_tree_object *subtree,
1772 struct tog_tree_view_state *s)
1774 struct tog_parent_tree *parent;
1776 parent = calloc(1, sizeof(*parent));
1778 return got_error_from_errno("calloc");
1780 parent->tree = s->tree;
1781 parent->first_displayed_entry = s->first_displayed_entry;
1782 parent->selected_entry = s->selected_entry;
1783 parent->selected = s->selected;
1784 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1787 s->first_displayed_entry = NULL;
1791 static const struct got_error *
1792 tree_view_walk_path(struct tog_tree_view_state *s,
1793 struct got_object_id *commit_id,
1794 const char *path, struct got_repository *repo)
1796 const struct got_error *err = NULL;
1797 struct got_tree_object *tree = NULL;
1799 char *slash, *subpath = NULL;
1801 /* Walk the path and open corresponding tree objects. */
1804 struct got_tree_entry *te;
1805 struct got_object_id *tree_id;
1811 /* Ensure the correct subtree entry is selected. */
1812 slash = strchr(p, '/');
1814 te_name = strdup(p);
1816 te_name = strndup(p, slash - p);
1817 if (te_name == NULL) {
1818 err = got_error_from_errno("strndup");
1821 te = got_object_tree_find_entry(s->tree, te_name);
1823 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1828 s->selected_entry = te;
1829 s->selected = got_tree_entry_get_index(te);
1830 if (s->tree != s->root)
1831 s->selected++; /* skip '..' */
1833 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1834 /* Jump to this file's entry. */
1835 s->first_displayed_entry = s->selected_entry;
1840 slash = strchr(p, '/');
1842 subpath = strndup(path, slash - path);
1844 subpath = strdup(path);
1845 if (subpath == NULL) {
1846 err = got_error_from_errno("strdup");
1850 err = got_object_id_by_path(&tree_id, repo, commit_id,
1855 err = got_object_open_as_tree(&tree, repo, tree_id);
1860 err = tree_view_visit_subtree(tree, s);
1862 got_object_tree_close(tree);
1876 static const struct got_error *
1877 browse_commit_tree(struct tog_view **new_view, int begin_x,
1878 struct commit_queue_entry *entry, const char *path,
1879 struct got_reflist_head *refs, struct got_repository *repo)
1881 const struct got_error *err = NULL;
1882 struct got_tree_object *tree;
1883 struct tog_tree_view_state *s;
1884 struct tog_view *tree_view;
1886 err = got_object_open_as_tree(&tree, repo,
1887 got_object_commit_get_tree_id(entry->commit));
1891 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1892 if (tree_view == NULL)
1893 return got_error_from_errno("view_open");
1895 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1897 got_object_tree_close(tree);
1900 s = &tree_view->state.tree;
1902 *new_view = tree_view;
1904 if (got_path_is_root_dir(path))
1907 return tree_view_walk_path(s, entry->id, path, repo);
1910 static const struct got_error *
1911 block_signals_used_by_main_thread(void)
1916 if (sigemptyset(&sigset) == -1)
1917 return got_error_from_errno("sigemptyset");
1919 /* tog handles SIGWINCH and SIGCONT */
1920 if (sigaddset(&sigset, SIGWINCH) == -1)
1921 return got_error_from_errno("sigaddset");
1922 if (sigaddset(&sigset, SIGCONT) == -1)
1923 return got_error_from_errno("sigaddset");
1925 /* ncurses handles SIGTSTP */
1926 if (sigaddset(&sigset, SIGTSTP) == -1)
1927 return got_error_from_errno("sigaddset");
1929 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1931 return got_error_set_errno(errcode, "pthread_sigmask");
1937 log_thread(void *arg)
1939 const struct got_error *err = NULL;
1941 struct tog_log_thread_args *a = arg;
1944 err = block_signals_used_by_main_thread();
1948 while (!done && !err && !tog_sigpipe_received) {
1949 err = queue_commits(a->graph, a->commits, 1, a->repo,
1950 a->in_repo_path, a->searching, a->search_next_done,
1953 if (err->code != GOT_ERR_ITER_COMPLETED)
1957 } else if (a->commits_needed > 0)
1958 a->commits_needed--;
1960 errcode = pthread_mutex_lock(&tog_mutex);
1962 err = got_error_set_errno(errcode,
1963 "pthread_mutex_lock");
1965 } else if (*a->quit)
1967 else if (*a->first_displayed_entry == NULL) {
1968 *a->first_displayed_entry =
1969 TAILQ_FIRST(&a->commits->head);
1970 *a->selected_entry = *a->first_displayed_entry;
1973 errcode = pthread_cond_signal(&a->commit_loaded);
1975 err = got_error_set_errno(errcode,
1976 "pthread_cond_signal");
1977 pthread_mutex_unlock(&tog_mutex);
1982 a->commits_needed = 0;
1984 if (a->commits_needed == 0) {
1985 errcode = pthread_cond_wait(&a->need_commits,
1988 err = got_error_set_errno(errcode,
1989 "pthread_cond_wait");
1993 errcode = pthread_mutex_unlock(&tog_mutex);
1994 if (errcode && err == NULL)
1995 err = got_error_set_errno(errcode,
1996 "pthread_mutex_unlock");
1998 a->log_complete = 1;
2002 static const struct got_error *
2003 stop_log_thread(struct tog_log_view_state *s)
2005 const struct got_error *err = NULL;
2010 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2012 return got_error_set_errno(errcode,
2013 "pthread_cond_signal");
2014 errcode = pthread_mutex_unlock(&tog_mutex);
2016 return got_error_set_errno(errcode,
2017 "pthread_mutex_unlock");
2018 errcode = pthread_join(s->thread, (void **)&err);
2020 return got_error_set_errno(errcode, "pthread_join");
2021 errcode = pthread_mutex_lock(&tog_mutex);
2023 return got_error_set_errno(errcode,
2024 "pthread_mutex_lock");
2028 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2029 if (errcode && err == NULL)
2030 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2032 if (s->thread_args.repo) {
2033 got_repo_close(s->thread_args.repo);
2034 s->thread_args.repo = NULL;
2037 if (s->thread_args.graph) {
2038 got_commit_graph_close(s->thread_args.graph);
2039 s->thread_args.graph = NULL;
2045 static const struct got_error *
2046 close_log_view(struct tog_view *view)
2048 const struct got_error *err = NULL;
2049 struct tog_log_view_state *s = &view->state.log;
2051 err = stop_log_thread(s);
2052 free_commits(&s->commits);
2053 free(s->in_repo_path);
2054 s->in_repo_path = NULL;
2060 static const struct got_error *
2061 search_start_log_view(struct tog_view *view)
2063 struct tog_log_view_state *s = &view->state.log;
2065 s->matched_entry = NULL;
2066 s->search_entry = NULL;
2070 static const struct got_error *
2071 search_next_log_view(struct tog_view *view)
2073 const struct got_error *err = NULL;
2074 struct tog_log_view_state *s = &view->state.log;
2075 struct commit_queue_entry *entry;
2077 /* Display progress update in log view. */
2078 show_log_view(view);
2082 if (s->search_entry) {
2084 errcode = pthread_mutex_unlock(&tog_mutex);
2086 return got_error_set_errno(errcode,
2087 "pthread_mutex_unlock");
2088 ch = wgetch(view->window);
2089 errcode = pthread_mutex_lock(&tog_mutex);
2091 return got_error_set_errno(errcode,
2092 "pthread_mutex_lock");
2093 if (ch == KEY_BACKSPACE) {
2094 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2097 if (view->searching == TOG_SEARCH_FORWARD)
2098 entry = TAILQ_NEXT(s->search_entry, entry);
2100 entry = TAILQ_PREV(s->search_entry,
2101 commit_queue_head, entry);
2102 } else if (s->matched_entry) {
2103 if (view->searching == TOG_SEARCH_FORWARD)
2104 entry = TAILQ_NEXT(s->matched_entry, entry);
2106 entry = TAILQ_PREV(s->matched_entry,
2107 commit_queue_head, entry);
2109 if (view->searching == TOG_SEARCH_FORWARD)
2110 entry = TAILQ_FIRST(&s->commits.head);
2112 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2118 if (entry == NULL) {
2119 if (s->thread_args.log_complete ||
2120 view->searching == TOG_SEARCH_BACKWARD) {
2121 view->search_next_done =
2122 (s->matched_entry == NULL ?
2123 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2124 s->search_entry = NULL;
2128 * Poke the log thread for more commits and return,
2129 * allowing the main loop to make progress. Search
2130 * will resume at s->search_entry once we come back.
2132 s->thread_args.commits_needed++;
2133 return trigger_log_thread(view, 0,
2134 &s->thread_args.commits_needed,
2135 &s->thread_args.log_complete,
2136 &s->thread_args.need_commits,
2137 &s->thread_args.commit_loaded);
2140 err = match_commit(&have_match, entry->id, entry->commit,
2145 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2146 s->matched_entry = entry;
2150 s->search_entry = entry;
2151 if (view->searching == TOG_SEARCH_FORWARD)
2152 entry = TAILQ_NEXT(entry, entry);
2154 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2157 if (s->matched_entry) {
2158 int cur = s->selected_entry->idx;
2159 while (cur < s->matched_entry->idx) {
2160 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2165 while (cur > s->matched_entry->idx) {
2166 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2173 s->search_entry = NULL;
2178 static const struct got_error *
2179 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2180 struct got_reflist_head *refs, struct got_repository *repo,
2181 const char *head_ref_name, const char *in_repo_path,
2184 const struct got_error *err = NULL;
2185 struct tog_log_view_state *s = &view->state.log;
2186 struct got_repository *thread_repo = NULL;
2187 struct got_commit_graph *thread_graph = NULL;
2190 if (in_repo_path != s->in_repo_path) {
2191 free(s->in_repo_path);
2192 s->in_repo_path = strdup(in_repo_path);
2193 if (s->in_repo_path == NULL)
2194 return got_error_from_errno("strdup");
2197 /* The commit queue only contains commits being displayed. */
2198 TAILQ_INIT(&s->commits.head);
2199 s->commits.ncommits = 0;
2203 s->head_ref_name = head_ref_name;
2204 s->start_id = got_object_id_dup(start_id);
2205 if (s->start_id == NULL) {
2206 err = got_error_from_errno("got_object_id_dup");
2209 s->log_branches = log_branches;
2211 SIMPLEQ_INIT(&s->colors);
2212 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2213 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2214 get_color_value("TOG_COLOR_COMMIT"));
2217 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2218 get_color_value("TOG_COLOR_AUTHOR"));
2220 free_colors(&s->colors);
2223 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2224 get_color_value("TOG_COLOR_DATE"));
2226 free_colors(&s->colors);
2231 view->show = show_log_view;
2232 view->input = input_log_view;
2233 view->close = close_log_view;
2234 view->search_start = search_start_log_view;
2235 view->search_next = search_next_log_view;
2237 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2240 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2244 err = got_commit_graph_iter_start(thread_graph,
2245 s->start_id, s->repo, NULL, NULL);
2249 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2251 err = got_error_set_errno(errcode, "pthread_cond_init");
2254 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2256 err = got_error_set_errno(errcode, "pthread_cond_init");
2260 s->thread_args.commits_needed = view->nlines;
2261 s->thread_args.graph = thread_graph;
2262 s->thread_args.commits = &s->commits;
2263 s->thread_args.in_repo_path = s->in_repo_path;
2264 s->thread_args.start_id = s->start_id;
2265 s->thread_args.repo = thread_repo;
2266 s->thread_args.log_complete = 0;
2267 s->thread_args.quit = &s->quit;
2268 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2269 s->thread_args.selected_entry = &s->selected_entry;
2270 s->thread_args.searching = &view->searching;
2271 s->thread_args.search_next_done = &view->search_next_done;
2272 s->thread_args.regex = &view->regex;
2275 close_log_view(view);
2279 static const struct got_error *
2280 show_log_view(struct tog_view *view)
2282 struct tog_log_view_state *s = &view->state.log;
2284 if (s->thread == NULL) {
2285 int errcode = pthread_create(&s->thread, NULL, log_thread,
2288 return got_error_set_errno(errcode, "pthread_create");
2291 return draw_commits(view, &s->last_displayed_entry,
2292 &s->selected_entry, s->first_displayed_entry,
2293 &s->commits, s->selected, view->nlines, s->refs,
2294 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2297 static const struct got_error *
2298 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2299 struct tog_view **focus_view, struct tog_view *view, int ch)
2301 const struct got_error *err = NULL;
2302 struct tog_log_view_state *s = &view->state.log;
2303 char *parent_path, *in_repo_path = NULL;
2304 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2306 struct got_object_id *start_id;
2316 if (s->first_displayed_entry == NULL)
2318 if (s->selected > 0)
2321 scroll_up(view, &s->first_displayed_entry, 1,
2326 if (s->first_displayed_entry == NULL)
2328 if (TAILQ_FIRST(&s->commits.head) ==
2329 s->first_displayed_entry) {
2333 scroll_up(view, &s->first_displayed_entry,
2334 view->nlines - 1, &s->commits);
2340 if (s->first_displayed_entry == NULL)
2342 if (s->selected < MIN(view->nlines - 2,
2343 s->commits.ncommits - 1)) {
2347 err = scroll_down(view, &s->first_displayed_entry, 1,
2348 &s->last_displayed_entry, &s->commits,
2349 &s->thread_args.log_complete,
2350 &s->thread_args.commits_needed,
2351 &s->thread_args.need_commits,
2352 &s->thread_args.commit_loaded);
2356 struct commit_queue_entry *first;
2357 first = s->first_displayed_entry;
2360 err = scroll_down(view, &s->first_displayed_entry,
2361 view->nlines - 1, &s->last_displayed_entry,
2362 &s->commits, &s->thread_args.log_complete,
2363 &s->thread_args.commits_needed,
2364 &s->thread_args.need_commits,
2365 &s->thread_args.commit_loaded);
2368 if (first == s->first_displayed_entry &&
2369 s->selected < MIN(view->nlines - 2,
2370 s->commits.ncommits - 1)) {
2371 /* can't scroll further down */
2372 s->selected = MIN(view->nlines - 2,
2373 s->commits.ncommits - 1);
2379 if (s->selected > view->nlines - 2)
2380 s->selected = view->nlines - 2;
2381 if (s->selected > s->commits.ncommits - 1)
2382 s->selected = s->commits.ncommits - 1;
2387 if (s->selected_entry == NULL)
2389 if (view_is_parent_view(view))
2390 begin_x = view_split_begin_x(view->begin_x);
2391 err = open_diff_view_for_commit(&diff_view, begin_x,
2392 s->selected_entry->commit, s->selected_entry->id,
2393 view, s->refs, s->repo);
2396 if (view_is_parent_view(view)) {
2397 err = view_close_child(view);
2400 err = view_set_child(view, diff_view);
2402 view_close(diff_view);
2405 *focus_view = diff_view;
2406 view->child_focussed = 1;
2408 *new_view = diff_view;
2411 if (s->selected_entry == NULL)
2413 if (view_is_parent_view(view))
2414 begin_x = view_split_begin_x(view->begin_x);
2415 err = browse_commit_tree(&tree_view, begin_x,
2416 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2419 if (view_is_parent_view(view)) {
2420 err = view_close_child(view);
2423 err = view_set_child(view, tree_view);
2425 view_close(tree_view);
2428 *focus_view = tree_view;
2429 view->child_focussed = 1;
2431 *new_view = tree_view;
2434 if (strcmp(s->in_repo_path, "/") == 0)
2436 parent_path = dirname(s->in_repo_path);
2437 if (parent_path && strcmp(parent_path, ".") != 0) {
2438 err = stop_log_thread(s);
2441 lv = view_open(view->nlines, view->ncols,
2442 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2444 return got_error_from_errno(
2446 err = open_log_view(lv, s->start_id, s->refs,
2447 s->repo, s->head_ref_name, parent_path,
2451 if (view_is_parent_view(view))
2454 view_set_child(view->parent, lv);
2461 err = stop_log_thread(s);
2464 lv = view_open(view->nlines, view->ncols,
2465 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2467 return got_error_from_errno("view_open");
2468 err = got_repo_match_object_id(&start_id, NULL,
2469 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2470 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2475 in_repo_path = strdup(s->in_repo_path);
2476 if (in_repo_path == NULL) {
2479 return got_error_from_errno("strdup");
2481 got_ref_list_free(s->refs);
2482 err = got_ref_list(s->refs, s->repo, NULL,
2483 got_ref_cmp_by_name, NULL);
2489 err = open_log_view(lv, start_id, s->refs, s->repo,
2490 s->head_ref_name, in_repo_path, s->log_branches);
2500 s->log_branches = !s->log_branches;
2501 err = stop_log_thread(s);
2504 lv = view_open(view->nlines, view->ncols,
2505 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2507 return got_error_from_errno("view_open");
2508 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2509 s->head_ref_name, s->in_repo_path, s->log_branches);
2524 static const struct got_error *
2525 apply_unveil(const char *repo_path, const char *worktree_path)
2527 const struct got_error *error;
2530 if (unveil("gmon.out", "rwc") != 0)
2531 return got_error_from_errno2("unveil", "gmon.out");
2533 if (repo_path && unveil(repo_path, "r") != 0)
2534 return got_error_from_errno2("unveil", repo_path);
2536 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2537 return got_error_from_errno2("unveil", worktree_path);
2539 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2540 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2542 error = got_privsep_unveil_exec_helpers();
2546 if (unveil(NULL, NULL) != 0)
2547 return got_error_from_errno("unveil");
2557 halfdelay(1); /* Do fast refresh while initial view is loading. */
2560 intrflush(stdscr, FALSE);
2561 keypad(stdscr, TRUE);
2563 if (getenv("TOG_COLORS") != NULL) {
2565 use_default_colors();
2567 signal(SIGWINCH, tog_sigwinch);
2568 signal(SIGPIPE, tog_sigpipe);
2569 signal(SIGCONT, tog_sigcont);
2572 static const struct got_error *
2573 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2574 struct got_repository *repo, struct got_worktree *worktree)
2576 const struct got_error *err = NULL;
2579 *in_repo_path = strdup("/");
2580 if (*in_repo_path == NULL)
2581 return got_error_from_errno("strdup");
2586 const char *prefix = got_worktree_get_path_prefix(worktree);
2589 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2593 if (asprintf(&p, "%s%s%s", prefix,
2594 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2595 err = got_error_from_errno("asprintf");
2599 err = got_repo_map_path(in_repo_path, repo, p, 0);
2603 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2608 static const struct got_error *
2609 cmd_log(int argc, char *argv[])
2611 const struct got_error *error;
2612 struct got_repository *repo = NULL;
2613 struct got_worktree *worktree = NULL;
2614 struct got_reflist_head refs;
2615 struct got_object_id *start_id = NULL;
2616 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2617 char *start_commit = NULL, *head_ref_name = NULL;
2618 int ch, log_branches = 0;
2619 struct tog_view *view;
2621 SIMPLEQ_INIT(&refs);
2624 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2629 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2635 start_commit = optarg;
2638 repo_path = realpath(optarg, NULL);
2639 if (repo_path == NULL)
2640 return got_error_from_errno2("realpath",
2655 cwd = getcwd(NULL, 0);
2657 return got_error_from_errno("getcwd");
2659 error = got_worktree_open(&worktree, cwd);
2660 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2663 if (repo_path == NULL) {
2666 strdup(got_worktree_get_repo_path(worktree));
2668 repo_path = strdup(cwd);
2670 if (repo_path == NULL) {
2671 error = got_error_from_errno("strdup");
2675 error = got_repo_open(&repo, repo_path, NULL);
2679 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2686 error = apply_unveil(got_repo_get_path(repo),
2687 worktree ? got_worktree_get_root_path(worktree) : NULL);
2691 if (start_commit == NULL)
2692 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2693 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2694 GOT_OBJ_TYPE_COMMIT, 1, repo);
2696 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2697 GOT_OBJ_TYPE_COMMIT, 1, repo);
2701 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2705 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2707 error = got_error_from_errno("view_open");
2711 head_ref_name = strdup(
2712 got_worktree_get_head_ref_name(worktree));
2713 if (head_ref_name == NULL) {
2714 error = got_error_from_errno("strdup");
2718 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2719 in_repo_path, log_branches);
2723 /* Release work tree lock. */
2724 got_worktree_close(worktree);
2727 error = view_loop(view);
2733 free(head_ref_name);
2735 got_repo_close(repo);
2737 got_worktree_close(worktree);
2738 got_ref_list_free(&refs);
2746 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2752 parse_next_line(FILE *f, size_t *len)
2757 const char delim[3] = { '\0', '\0', '\0'};
2759 line = fparseln(f, &linelen, &lineno, delim, 0);
2766 match_line(const char *line, regex_t *regex)
2768 regmatch_t regmatch;
2770 return regexec(regex, line, 1, ®match, 0) == 0;
2774 match_color(struct tog_colors *colors, const char *line)
2776 struct tog_color *tc = NULL;
2778 SIMPLEQ_FOREACH(tc, colors, entry) {
2779 if (match_line(line, &tc->regex))
2786 static const struct got_error *
2787 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2788 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2789 char *header, struct tog_colors *colors)
2791 const struct got_error *err;
2792 int lineno = 0, nprinted = 0;
2794 struct tog_color *tc;
2800 werase(view->window);
2803 err = format_line(&wline, &width, header, view->ncols, 0);
2808 if (view_needs_focus_indication(view))
2809 wstandout(view->window);
2810 waddwstr(view->window, wline);
2811 if (view_needs_focus_indication(view))
2812 wstandend(view->window);
2813 if (width <= view->ncols - 1)
2814 waddch(view->window, '\n');
2822 while (nprinted < max_lines) {
2823 line = parse_next_line(f, &len);
2828 if (++lineno < *first_displayed_line) {
2833 err = format_line(&wline, &width, line, view->ncols, 0);
2839 tc = match_color(colors, line);
2841 wattr_on(view->window,
2842 COLOR_PAIR(tc->colorpair), NULL);
2843 waddwstr(view->window, wline);
2845 wattr_off(view->window,
2846 COLOR_PAIR(tc->colorpair), NULL);
2847 if (width <= view->ncols - 1)
2848 waddch(view->window, '\n');
2849 if (++nprinted == 1)
2850 *first_displayed_line = lineno;
2855 *last_displayed_line = lineno;
2860 while (nprinted < view->nlines) {
2861 waddch(view->window, '\n');
2865 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2870 wstandout(view->window);
2871 waddwstr(view->window, wline);
2872 wstandend(view->window);
2879 get_datestr(time_t *time, char *datebuf)
2881 struct tm mytm, *tm;
2884 tm = gmtime_r(time, &mytm);
2887 s = asctime_r(tm, datebuf);
2890 p = strchr(s, '\n');
2896 static const struct got_error *
2897 get_changed_paths(struct got_pathlist_head *paths,
2898 struct got_commit_object *commit, struct got_repository *repo)
2900 const struct got_error *err = NULL;
2901 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2902 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2903 struct got_object_qid *qid;
2905 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2907 struct got_commit_object *pcommit;
2908 err = got_object_open_as_commit(&pcommit, repo,
2913 tree_id1 = got_object_commit_get_tree_id(pcommit);
2914 got_object_commit_close(pcommit);
2919 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2924 tree_id2 = got_object_commit_get_tree_id(commit);
2925 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2929 err = got_diff_tree(tree1, tree2, "", "", repo,
2930 got_diff_tree_collect_changed_paths, paths, 0);
2933 got_object_tree_close(tree1);
2935 got_object_tree_close(tree2);
2939 static const struct got_error *
2940 write_commit_info(struct got_object_id *commit_id,
2941 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2943 const struct got_error *err = NULL;
2944 char datebuf[26], *datestr;
2945 struct got_commit_object *commit;
2946 char *id_str = NULL, *logmsg = NULL;
2947 time_t committer_time;
2948 const char *author, *committer;
2949 char *refs_str = NULL;
2950 struct got_pathlist_head changed_paths;
2951 struct got_pathlist_entry *pe;
2953 TAILQ_INIT(&changed_paths);
2956 err = build_refs_str(&refs_str, refs, commit_id, repo);
2961 err = got_object_open_as_commit(&commit, repo, commit_id);
2965 err = got_object_id_str(&id_str, commit_id);
2967 err = got_error_from_errno("got_object_id_str");
2971 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2972 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2973 err = got_error_from_errno("fprintf");
2976 if (fprintf(outfile, "from: %s\n",
2977 got_object_commit_get_author(commit)) < 0) {
2978 err = got_error_from_errno("fprintf");
2981 committer_time = got_object_commit_get_committer_time(commit);
2982 datestr = get_datestr(&committer_time, datebuf);
2983 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2984 err = got_error_from_errno("fprintf");
2987 author = got_object_commit_get_author(commit);
2988 committer = got_object_commit_get_committer(commit);
2989 if (strcmp(author, committer) != 0 &&
2990 fprintf(outfile, "via: %s\n", committer) < 0) {
2991 err = got_error_from_errno("fprintf");
2994 err = got_object_commit_get_logmsg(&logmsg, commit);
2997 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2998 err = got_error_from_errno("fprintf");
3001 err = get_changed_paths(&changed_paths, commit, repo);
3004 TAILQ_FOREACH(pe, &changed_paths, entry) {
3005 struct got_diff_changed_path *cp = pe->data;
3006 fprintf(outfile, "%c %s\n", cp->status, pe->path);
3007 free((char *)pe->path);
3010 fputc('\n', outfile);
3012 got_pathlist_free(&changed_paths);
3016 got_object_commit_close(commit);
3020 const struct got_error *
3021 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
3027 size_t noffsets = 0;
3031 *line_offsets = NULL;
3037 if (fseek(infile, 0, SEEK_END) == -1)
3038 return got_error_from_errno("fseek");
3039 len = ftell(infile) + 1;
3041 return got_error_from_errno("ftell");
3042 if (fseek(infile, 0, SEEK_SET) == -1)
3043 return got_error_from_errno("fseek");
3047 if ((buf = calloc(len, sizeof(char *))) == NULL)
3048 return got_error_from_errno("calloc");
3050 fread(buf, 1, len, infile);
3052 return got_error_from_errno("fread");
3055 if (line_offsets && nlines) {
3056 if (*line_offsets == NULL) {
3057 /* Have some data but perhaps no '\n'. */
3060 *line_offsets = calloc(1, sizeof(**line_offsets));
3061 if (*line_offsets == NULL)
3062 return got_error_from_errno("calloc");
3063 /* Skip forward over end of first line. */
3070 /* Scan '\n' offsets in remaining chunk of data. */
3072 if (buf[i] != '\n') {
3077 if (noffsets < *nlines) {
3078 off_t *o = recallocarray(*line_offsets,
3080 sizeof(**line_offsets));
3082 free(*line_offsets);
3083 *line_offsets = NULL;
3084 return got_error_from_errno(
3091 (*line_offsets)[*nlines - 1] = off;
3096 if (fflush(infile) != 0)
3097 return got_error_from_errno("fflush");
3106 static const struct got_error *
3107 create_diff(struct tog_diff_view_state *s)
3109 const struct got_error *err = NULL;
3115 err = got_error_from_errno("got_opentemp");
3118 if (s->f && fclose(s->f) != 0) {
3119 err = got_error_from_errno("fclose");
3125 err = got_object_get_type(&obj_type, s->repo, s->id1);
3127 err = got_object_get_type(&obj_type, s->repo, s->id2);
3132 case GOT_OBJ_TYPE_BLOB:
3133 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3134 s->diff_context, 0, s->repo, s->f);
3136 case GOT_OBJ_TYPE_TREE:
3137 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3138 s->diff_context, 0, s->repo, s->f);
3140 case GOT_OBJ_TYPE_COMMIT: {
3141 const struct got_object_id_queue *parent_ids;
3142 struct got_object_qid *pid;
3143 struct got_commit_object *commit2;
3145 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3148 /* Show commit info if we're diffing to a parent/root commit. */
3149 if (s->id1 == NULL) {
3150 err = write_commit_info(s->id2, s->refs, s->repo, s->f);
3154 parent_ids = got_object_commit_get_parent_ids(commit2);
3155 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3156 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3157 err = write_commit_info(s->id2, s->refs,
3165 got_object_commit_close(commit2);
3167 err = got_diff_objects_as_commits(s->id1, s->id2,
3168 s->diff_context, 0, s->repo, s->f);
3172 err = got_error(GOT_ERR_OBJ_TYPE);
3177 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3180 if (s->f && fflush(s->f) != 0 && err == NULL)
3181 err = got_error_from_errno("fflush");
3186 diff_view_indicate_progress(struct tog_view *view)
3188 mvwaddstr(view->window, 0, 0, "diffing...");
3193 static const struct got_error *
3194 search_start_diff_view(struct tog_view *view)
3196 struct tog_diff_view_state *s = &view->state.diff;
3198 s->matched_line = 0;
3202 static const struct got_error *
3203 search_next_diff_view(struct tog_view *view)
3205 struct tog_diff_view_state *s = &view->state.diff;
3208 if (!view->searching) {
3209 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3213 if (s->matched_line) {
3214 if (view->searching == TOG_SEARCH_FORWARD)
3215 lineno = s->matched_line + 1;
3217 lineno = s->matched_line - 1;
3219 if (view->searching == TOG_SEARCH_FORWARD)
3230 if (lineno <= 0 || lineno > s->nlines) {
3231 if (s->matched_line == 0) {
3232 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3237 if (view->searching == TOG_SEARCH_FORWARD)
3243 offset = s->line_offsets[lineno - 1];
3244 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3246 return got_error_from_errno("fseeko");
3249 line = parse_next_line(s->f, &len);
3250 if (line && match_line(line, &view->regex)) {
3251 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3252 s->matched_line = lineno;
3257 if (view->searching == TOG_SEARCH_FORWARD)
3263 if (s->matched_line) {
3264 s->first_displayed_line = s->matched_line;
3265 s->selected_line = 1;
3271 static const struct got_error *
3272 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3273 struct got_object_id *id2, struct tog_view *log_view,
3274 struct got_reflist_head *refs, struct got_repository *repo)
3276 const struct got_error *err;
3277 struct tog_diff_view_state *s = &view->state.diff;
3279 if (id1 != NULL && id2 != NULL) {
3281 err = got_object_get_type(&type1, repo, id1);
3284 err = got_object_get_type(&type2, repo, id2);
3289 return got_error(GOT_ERR_OBJ_TYPE);
3291 s->first_displayed_line = 1;
3292 s->last_displayed_line = view->nlines;
3293 s->selected_line = 1;
3300 s->id1 = got_object_id_dup(id1);
3302 return got_error_from_errno("got_object_id_dup");
3306 s->id2 = got_object_id_dup(id2);
3307 if (s->id2 == NULL) {
3310 return got_error_from_errno("got_object_id_dup");
3313 s->first_displayed_line = 1;
3314 s->last_displayed_line = view->nlines;
3315 s->diff_context = 3;
3316 s->log_view = log_view;
3320 SIMPLEQ_INIT(&s->colors);
3321 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3322 err = add_color(&s->colors,
3323 "^-", TOG_COLOR_DIFF_MINUS,
3324 get_color_value("TOG_COLOR_DIFF_MINUS"));
3327 err = add_color(&s->colors, "^\\+",
3328 TOG_COLOR_DIFF_PLUS,
3329 get_color_value("TOG_COLOR_DIFF_PLUS"));
3331 free_colors(&s->colors);
3334 err = add_color(&s->colors,
3335 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3336 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3338 free_colors(&s->colors);
3342 err = add_color(&s->colors,
3343 "^(commit|(blob|file) [-+] |[MDmA] [^ ])",
3344 TOG_COLOR_DIFF_META,
3345 get_color_value("TOG_COLOR_DIFF_META"));
3347 free_colors(&s->colors);
3351 err = add_color(&s->colors,
3352 "^(from|via): ", TOG_COLOR_AUTHOR,
3353 get_color_value("TOG_COLOR_AUTHOR"));
3355 free_colors(&s->colors);
3359 err = add_color(&s->colors,
3360 "^date: ", TOG_COLOR_DATE,
3361 get_color_value("TOG_COLOR_DATE"));
3363 free_colors(&s->colors);
3368 if (log_view && view_is_splitscreen(view))
3369 show_log_view(log_view); /* draw vborder */
3370 diff_view_indicate_progress(view);
3372 err = create_diff(s);
3381 view->show = show_diff_view;
3382 view->input = input_diff_view;
3383 view->close = close_diff_view;
3384 view->search_start = search_start_diff_view;
3385 view->search_next = search_next_diff_view;
3390 static const struct got_error *
3391 close_diff_view(struct tog_view *view)
3393 const struct got_error *err = NULL;
3394 struct tog_diff_view_state *s = &view->state.diff;
3400 if (s->f && fclose(s->f) == EOF)
3401 err = got_error_from_errno("fclose");
3402 free_colors(&s->colors);
3403 free(s->line_offsets);
3407 static const struct got_error *
3408 show_diff_view(struct tog_view *view)
3410 const struct got_error *err;
3411 struct tog_diff_view_state *s = &view->state.diff;
3412 char *id_str1 = NULL, *id_str2, *header;
3415 err = got_object_id_str(&id_str1, s->id1);
3419 err = got_object_id_str(&id_str2, s->id2);
3423 if (asprintf(&header, "diff %s %s",
3424 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3425 err = got_error_from_errno("asprintf");
3433 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3434 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3435 header, &s->colors);
3438 static const struct got_error *
3439 set_selected_commit(struct tog_diff_view_state *s,
3440 struct commit_queue_entry *entry)
3442 const struct got_error *err;
3443 const struct got_object_id_queue *parent_ids;
3444 struct got_commit_object *selected_commit;
3445 struct got_object_qid *pid;
3448 s->id2 = got_object_id_dup(entry->id);
3450 return got_error_from_errno("got_object_id_dup");
3452 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3455 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3457 pid = SIMPLEQ_FIRST(parent_ids);
3458 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3459 got_object_commit_close(selected_commit);
3463 static const struct got_error *
3464 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3465 struct tog_view **focus_view, struct tog_view *view, int ch)
3467 const struct got_error *err = NULL;
3468 struct tog_diff_view_state *s = &view->state.diff;
3469 struct tog_log_view_state *ls;
3470 struct commit_queue_entry *entry;
3476 if (s->first_displayed_line > 1)
3477 s->first_displayed_line--;
3481 if (s->first_displayed_line == 1)
3484 while (i++ < view->nlines - 1 &&
3485 s->first_displayed_line > 1)
3486 s->first_displayed_line--;
3491 s->first_displayed_line++;
3499 while (!s->eof && i++ < view->nlines - 1) {
3501 line = parse_next_line(s->f, NULL);
3502 s->first_displayed_line++;
3508 if (s->diff_context > 0) {
3510 diff_view_indicate_progress(view);
3511 err = create_diff(s);
3515 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3517 diff_view_indicate_progress(view);
3518 err = create_diff(s);
3523 if (s->log_view == NULL)
3525 ls = &s->log_view->state.log;
3526 entry = TAILQ_PREV(ls->selected_entry,
3527 commit_queue_head, entry);
3531 err = input_log_view(NULL, NULL, NULL, s->log_view,
3536 err = set_selected_commit(s, entry);
3540 s->first_displayed_line = 1;
3541 s->last_displayed_line = view->nlines;
3543 diff_view_indicate_progress(view);
3544 err = create_diff(s);
3548 if (s->log_view == NULL)
3550 ls = &s->log_view->state.log;
3552 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3553 ls->thread_args.commits_needed++;
3554 err = trigger_log_thread(s->log_view, 1,
3555 &ls->thread_args.commits_needed,
3556 &ls->thread_args.log_complete,
3557 &ls->thread_args.need_commits,
3558 &ls->thread_args.commit_loaded);
3562 err = input_log_view(NULL, NULL, NULL, s->log_view,
3567 entry = TAILQ_NEXT(ls->selected_entry, entry);
3571 err = set_selected_commit(s, entry);
3575 s->first_displayed_line = 1;
3576 s->last_displayed_line = view->nlines;
3578 diff_view_indicate_progress(view);
3579 err = create_diff(s);
3588 static const struct got_error *
3589 cmd_diff(int argc, char *argv[])
3591 const struct got_error *error = NULL;
3592 struct got_repository *repo = NULL;
3593 struct got_worktree *worktree = NULL;
3594 struct got_reflist_head refs;
3595 struct got_object_id *id1 = NULL, *id2 = NULL;
3596 char *repo_path = NULL, *cwd = NULL;
3597 char *id_str1 = NULL, *id_str2 = NULL;
3599 struct tog_view *view;
3601 SIMPLEQ_INIT(&refs);
3604 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3609 while ((ch = getopt(argc, argv, "r:")) != -1) {
3612 repo_path = realpath(optarg, NULL);
3613 if (repo_path == NULL)
3614 return got_error_from_errno2("realpath",
3627 usage_diff(); /* TODO show local worktree changes */
3628 } else if (argc == 2) {
3634 cwd = getcwd(NULL, 0);
3636 return got_error_from_errno("getcwd");
3638 error = got_worktree_open(&worktree, cwd);
3639 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3642 if (repo_path == NULL) {
3645 strdup(got_worktree_get_repo_path(worktree));
3647 repo_path = strdup(cwd);
3649 if (repo_path == NULL) {
3650 error = got_error_from_errno("strdup");
3654 error = got_repo_open(&repo, repo_path, NULL);
3660 error = apply_unveil(got_repo_get_path(repo), NULL);
3664 error = got_repo_match_object_id_prefix(&id1, id_str1,
3665 GOT_OBJ_TYPE_ANY, repo);
3669 error = got_repo_match_object_id_prefix(&id2, id_str2,
3670 GOT_OBJ_TYPE_ANY, repo);
3674 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3678 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3680 error = got_error_from_errno("view_open");
3683 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3686 error = view_loop(view);
3691 got_repo_close(repo);
3693 got_worktree_close(worktree);
3694 got_ref_list_free(&refs);
3702 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3707 struct tog_blame_line {
3709 struct got_object_id *id;
3712 static const struct got_error *
3713 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3714 const char *path, struct tog_blame_line *lines, int nlines,
3715 int blame_complete, int selected_line, int *first_displayed_line,
3716 int *last_displayed_line, int *eof, int max_lines,
3717 struct tog_colors *colors)
3719 const struct got_error *err;
3720 int lineno = 0, nprinted = 0;
3725 struct tog_blame_line *blame_line;
3726 struct got_object_id *prev_id = NULL;
3728 struct tog_color *tc;
3730 err = got_object_id_str(&id_str, id);
3735 werase(view->window);
3737 if (asprintf(&line, "commit %s", id_str) == -1) {
3738 err = got_error_from_errno("asprintf");
3743 err = format_line(&wline, &width, line, view->ncols, 0);
3748 if (view_needs_focus_indication(view))
3749 wstandout(view->window);
3750 tc = get_color(colors, TOG_COLOR_COMMIT);
3752 wattr_on(view->window,
3753 COLOR_PAIR(tc->colorpair), NULL);
3754 waddwstr(view->window, wline);
3756 wattr_off(view->window,
3757 COLOR_PAIR(tc->colorpair), NULL);
3758 if (view_needs_focus_indication(view))
3759 wstandend(view->window);
3762 if (width < view->ncols - 1)
3763 waddch(view->window, '\n');
3765 if (asprintf(&line, "[%d/%d] %s%s",
3766 *first_displayed_line - 1 + selected_line, nlines,
3767 blame_complete ? "" : "annotating... ", path) == -1) {
3769 return got_error_from_errno("asprintf");
3772 err = format_line(&wline, &width, line, view->ncols, 0);
3777 waddwstr(view->window, wline);
3780 if (width < view->ncols - 1)
3781 waddch(view->window, '\n');
3784 while (nprinted < max_lines - 2) {
3785 line = parse_next_line(f, &len);
3790 if (++lineno < *first_displayed_line) {
3795 if (view->ncols <= 9) {
3797 wline = wcsdup(L"");
3799 err = got_error_from_errno("wcsdup");
3801 err = format_line(&wline, &width, line,
3802 view->ncols - 9, 9);
3810 if (view->focussed && nprinted == selected_line - 1)
3811 wstandout(view->window);
3814 blame_line = &lines[lineno - 1];
3815 if (blame_line->annotated && prev_id &&
3816 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3818 nprinted == selected_line - 1)) {
3819 waddstr(view->window, " ");
3820 } else if (blame_line->annotated) {
3822 err = got_object_id_str(&id_str, blame_line->id);
3828 tc = get_color(colors, TOG_COLOR_COMMIT);
3830 wattr_on(view->window,
3831 COLOR_PAIR(tc->colorpair), NULL);
3832 wprintw(view->window, "%.8s", id_str);
3834 wattr_off(view->window,
3835 COLOR_PAIR(tc->colorpair), NULL);
3837 prev_id = blame_line->id;
3839 waddstr(view->window, "........");
3843 waddstr(view->window, "........");
3847 if (view->focussed && nprinted == selected_line - 1)
3848 wstandend(view->window);
3849 waddstr(view->window, " ");
3851 waddwstr(view->window, wline);
3852 if (width <= view->ncols - 1)
3853 waddch(view->window, '\n');
3854 if (++nprinted == 1)
3855 *first_displayed_line = lineno;
3860 *last_displayed_line = lineno;
3867 static const struct got_error *
3868 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3870 const struct got_error *err = NULL;
3871 struct tog_blame_cb_args *a = arg;
3872 struct tog_blame_line *line;
3875 if (nlines != a->nlines ||
3876 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3877 return got_error(GOT_ERR_RANGE);
3879 errcode = pthread_mutex_lock(&tog_mutex);
3881 return got_error_set_errno(errcode, "pthread_mutex_lock");
3883 if (*a->quit) { /* user has quit the blame view */
3884 err = got_error(GOT_ERR_ITER_COMPLETED);
3889 goto done; /* no change in this commit */
3891 line = &a->lines[lineno - 1];
3892 if (line->annotated)
3895 line->id = got_object_id_dup(id);
3896 if (line->id == NULL) {
3897 err = got_error_from_errno("got_object_id_dup");
3900 line->annotated = 1;
3902 errcode = pthread_mutex_unlock(&tog_mutex);
3904 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3909 blame_thread(void *arg)
3911 const struct got_error *err;
3912 struct tog_blame_thread_args *ta = arg;
3913 struct tog_blame_cb_args *a = ta->cb_args;
3916 err = block_signals_used_by_main_thread();
3920 err = got_blame(ta->path, a->commit_id, ta->repo,
3921 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3922 if (err && err->code == GOT_ERR_CANCELLED)
3925 errcode = pthread_mutex_lock(&tog_mutex);
3927 return (void *)got_error_set_errno(errcode,
3928 "pthread_mutex_lock");
3930 got_repo_close(ta->repo);
3934 errcode = pthread_mutex_unlock(&tog_mutex);
3935 if (errcode && err == NULL)
3936 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3941 static struct got_object_id *
3942 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3943 int first_displayed_line, int selected_line)
3945 struct tog_blame_line *line;
3950 line = &lines[first_displayed_line - 1 + selected_line - 1];
3951 if (!line->annotated)
3957 static const struct got_error *
3958 stop_blame(struct tog_blame *blame)
3960 const struct got_error *err = NULL;
3963 if (blame->thread) {
3965 errcode = pthread_mutex_unlock(&tog_mutex);
3967 return got_error_set_errno(errcode,
3968 "pthread_mutex_unlock");
3969 errcode = pthread_join(blame->thread, (void **)&err);
3971 return got_error_set_errno(errcode, "pthread_join");
3972 errcode = pthread_mutex_lock(&tog_mutex);
3974 return got_error_set_errno(errcode,
3975 "pthread_mutex_lock");
3976 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3978 blame->thread = NULL;
3980 if (blame->thread_args.repo) {
3981 got_repo_close(blame->thread_args.repo);
3982 blame->thread_args.repo = NULL;
3985 if (fclose(blame->f) != 0 && err == NULL)
3986 err = got_error_from_errno("fclose");
3990 for (i = 0; i < blame->nlines; i++)
3991 free(blame->lines[i].id);
3993 blame->lines = NULL;
3995 free(blame->cb_args.commit_id);
3996 blame->cb_args.commit_id = NULL;
4001 static const struct got_error *
4002 cancel_blame_view(void *arg)
4004 const struct got_error *err = NULL;
4008 errcode = pthread_mutex_lock(&tog_mutex);
4010 return got_error_set_errno(errcode,
4011 "pthread_mutex_unlock");
4014 err = got_error(GOT_ERR_CANCELLED);
4016 errcode = pthread_mutex_unlock(&tog_mutex);
4018 return got_error_set_errno(errcode,
4019 "pthread_mutex_lock");
4024 static const struct got_error *
4025 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4026 int *first_displayed_line, int *last_displayed_line, int *selected_line,
4027 int *done, int *eof, const char *path, struct got_object_id *commit_id,
4028 struct got_repository *repo)
4030 const struct got_error *err = NULL;
4031 struct got_blob_object *blob = NULL;
4032 struct got_repository *thread_repo = NULL;
4033 struct got_object_id *obj_id = NULL;
4036 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4040 err = got_object_get_type(&obj_type, repo, obj_id);
4044 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4045 err = got_error(GOT_ERR_OBJ_TYPE);
4049 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4052 blame->f = got_opentemp();
4053 if (blame->f == NULL) {
4054 err = got_error_from_errno("got_opentemp");
4057 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4058 &blame->line_offsets, blame->f, blob);
4059 if (err || blame->nlines == 0)
4062 /* Don't include \n at EOF in the blame line count. */
4063 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4066 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4067 if (blame->lines == NULL) {
4068 err = got_error_from_errno("calloc");
4072 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4076 blame->cb_args.view = view;
4077 blame->cb_args.lines = blame->lines;
4078 blame->cb_args.nlines = blame->nlines;
4079 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4080 if (blame->cb_args.commit_id == NULL) {
4081 err = got_error_from_errno("got_object_id_dup");
4084 blame->cb_args.quit = done;
4086 blame->thread_args.path = path;
4087 blame->thread_args.repo = thread_repo;
4088 blame->thread_args.cb_args = &blame->cb_args;
4089 blame->thread_args.complete = blame_complete;
4090 blame->thread_args.cancel_cb = cancel_blame_view;
4091 blame->thread_args.cancel_arg = done;
4092 *blame_complete = 0;
4096 got_object_blob_close(blob);
4103 static const struct got_error *
4104 open_blame_view(struct tog_view *view, char *path,
4105 struct got_object_id *commit_id, struct got_reflist_head *refs,
4106 struct got_repository *repo)
4108 const struct got_error *err = NULL;
4109 struct tog_blame_view_state *s = &view->state.blame;
4111 SIMPLEQ_INIT(&s->blamed_commits);
4113 s->path = strdup(path);
4114 if (s->path == NULL)
4115 return got_error_from_errno("strdup");
4117 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4123 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4124 s->first_displayed_line = 1;
4125 s->last_displayed_line = view->nlines;
4126 s->selected_line = 1;
4127 s->blame_complete = 0;
4130 s->commit_id = commit_id;
4131 memset(&s->blame, 0, sizeof(s->blame));
4133 SIMPLEQ_INIT(&s->colors);
4134 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4135 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4136 get_color_value("TOG_COLOR_COMMIT"));
4141 view->show = show_blame_view;
4142 view->input = input_blame_view;
4143 view->close = close_blame_view;
4144 view->search_start = search_start_blame_view;
4145 view->search_next = search_next_blame_view;
4147 return run_blame(&s->blame, view, &s->blame_complete,
4148 &s->first_displayed_line, &s->last_displayed_line,
4149 &s->selected_line, &s->done, &s->eof, s->path,
4150 s->blamed_commit->id, s->repo);
4153 static const struct got_error *
4154 close_blame_view(struct tog_view *view)
4156 const struct got_error *err = NULL;
4157 struct tog_blame_view_state *s = &view->state.blame;
4159 if (s->blame.thread)
4160 err = stop_blame(&s->blame);
4162 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4163 struct got_object_qid *blamed_commit;
4164 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4165 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4166 got_object_qid_free(blamed_commit);
4170 free_colors(&s->colors);
4175 static const struct got_error *
4176 search_start_blame_view(struct tog_view *view)
4178 struct tog_blame_view_state *s = &view->state.blame;
4180 s->matched_line = 0;
4184 static const struct got_error *
4185 search_next_blame_view(struct tog_view *view)
4187 struct tog_blame_view_state *s = &view->state.blame;
4190 if (!view->searching) {
4191 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4195 if (s->matched_line) {
4196 if (view->searching == TOG_SEARCH_FORWARD)
4197 lineno = s->matched_line + 1;
4199 lineno = s->matched_line - 1;
4201 if (view->searching == TOG_SEARCH_FORWARD)
4204 lineno = s->blame.nlines;
4212 if (lineno <= 0 || lineno > s->blame.nlines) {
4213 if (s->matched_line == 0) {
4214 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4219 if (view->searching == TOG_SEARCH_FORWARD)
4222 lineno = s->blame.nlines;
4225 offset = s->blame.line_offsets[lineno - 1];
4226 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4228 return got_error_from_errno("fseeko");
4231 line = parse_next_line(s->blame.f, &len);
4232 if (line && match_line(line, &view->regex)) {
4233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4234 s->matched_line = lineno;
4239 if (view->searching == TOG_SEARCH_FORWARD)
4245 if (s->matched_line) {
4246 s->first_displayed_line = s->matched_line;
4247 s->selected_line = 1;
4253 static const struct got_error *
4254 show_blame_view(struct tog_view *view)
4256 const struct got_error *err = NULL;
4257 struct tog_blame_view_state *s = &view->state.blame;
4260 if (s->blame.thread == NULL) {
4261 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4262 &s->blame.thread_args);
4264 return got_error_set_errno(errcode, "pthread_create");
4266 halfdelay(1); /* fast refresh while annotating */
4269 if (s->blame_complete)
4270 halfdelay(10); /* disable fast refresh */
4272 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4273 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4274 s->selected_line, &s->first_displayed_line,
4275 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4281 static const struct got_error *
4282 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4283 struct tog_view **focus_view, struct tog_view *view, int ch)
4285 const struct got_error *err = NULL, *thread_err = NULL;
4286 struct tog_view *diff_view;
4287 struct tog_blame_view_state *s = &view->state.blame;
4296 if (s->selected_line > 1)
4298 else if (s->selected_line == 1 &&
4299 s->first_displayed_line > 1)
4300 s->first_displayed_line--;
4304 if (s->first_displayed_line == 1) {
4305 s->selected_line = 1;
4308 if (s->first_displayed_line > view->nlines - 2)
4309 s->first_displayed_line -=
4312 s->first_displayed_line = 1;
4316 if (s->selected_line < view->nlines - 2 &&
4317 s->first_displayed_line +
4318 s->selected_line <= s->blame.nlines)
4320 else if (s->last_displayed_line <
4322 s->first_displayed_line++;
4326 struct got_object_id *id = NULL;
4327 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4328 s->first_displayed_line, s->selected_line);
4332 struct got_commit_object *commit;
4333 struct got_object_qid *pid;
4334 struct got_object_id *blob_id = NULL;
4336 err = got_object_open_as_commit(&commit,
4340 pid = SIMPLEQ_FIRST(
4341 got_object_commit_get_parent_ids(commit));
4343 got_object_commit_close(commit);
4346 /* Check if path history ends here. */
4347 err = got_object_id_by_path(&blob_id, s->repo,
4350 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4352 got_object_commit_close(commit);
4355 err = got_object_get_type(&obj_type, s->repo,
4358 /* Can't blame non-blob type objects. */
4359 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4360 got_object_commit_close(commit);
4363 err = got_object_qid_alloc(&s->blamed_commit,
4365 got_object_commit_close(commit);
4367 if (got_object_id_cmp(id,
4368 s->blamed_commit->id) == 0)
4370 err = got_object_qid_alloc(&s->blamed_commit,
4376 thread_err = stop_blame(&s->blame);
4380 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4381 s->blamed_commit, entry);
4382 err = run_blame(&s->blame, view, &s->blame_complete,
4383 &s->first_displayed_line, &s->last_displayed_line,
4384 &s->selected_line, &s->done, &s->eof,
4385 s->path, s->blamed_commit->id, s->repo);
4391 struct got_object_qid *first;
4392 first = SIMPLEQ_FIRST(&s->blamed_commits);
4393 if (!got_object_id_cmp(first->id, s->commit_id))
4396 thread_err = stop_blame(&s->blame);
4400 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4401 got_object_qid_free(s->blamed_commit);
4403 SIMPLEQ_FIRST(&s->blamed_commits);
4404 err = run_blame(&s->blame, view, &s->blame_complete,
4405 &s->first_displayed_line, &s->last_displayed_line,
4406 &s->selected_line, &s->done, &s->eof, s->path,
4407 s->blamed_commit->id, s->repo);
4414 struct got_object_id *id = NULL;
4415 struct got_object_qid *pid;
4416 struct got_commit_object *commit = NULL;
4417 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4418 s->first_displayed_line, s->selected_line);
4421 err = got_object_open_as_commit(&commit, s->repo, id);
4424 pid = SIMPLEQ_FIRST(
4425 got_object_commit_get_parent_ids(commit));
4426 if (view_is_parent_view(view))
4427 begin_x = view_split_begin_x(view->begin_x);
4428 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4429 if (diff_view == NULL) {
4430 got_object_commit_close(commit);
4431 err = got_error_from_errno("view_open");
4434 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4435 id, NULL, s->refs, s->repo);
4436 got_object_commit_close(commit);
4438 view_close(diff_view);
4441 if (view_is_parent_view(view)) {
4442 err = view_close_child(view);
4445 err = view_set_child(view, diff_view);
4447 view_close(diff_view);
4450 *focus_view = diff_view;
4451 view->child_focussed = 1;
4453 *new_view = diff_view;
4461 if (s->last_displayed_line >= s->blame.nlines &&
4462 s->selected_line >= MIN(s->blame.nlines,
4463 view->nlines - 2)) {
4466 if (s->last_displayed_line >= s->blame.nlines &&
4467 s->selected_line < view->nlines - 2) {
4468 s->selected_line = MIN(s->blame.nlines,
4472 if (s->last_displayed_line + view->nlines - 2
4474 s->first_displayed_line +=
4477 s->first_displayed_line =
4482 if (s->selected_line > view->nlines - 2) {
4483 s->selected_line = MIN(s->blame.nlines,
4490 return thread_err ? thread_err : err;
4493 static const struct got_error *
4494 cmd_blame(int argc, char *argv[])
4496 const struct got_error *error;
4497 struct got_repository *repo = NULL;
4498 struct got_reflist_head refs;
4499 struct got_worktree *worktree = NULL;
4500 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4501 struct got_object_id *commit_id = NULL;
4502 char *commit_id_str = NULL;
4504 struct tog_view *view;
4506 SIMPLEQ_INIT(&refs);
4509 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4514 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4517 commit_id_str = optarg;
4520 repo_path = realpath(optarg, NULL);
4521 if (repo_path == NULL)
4522 return got_error_from_errno2("realpath",
4537 cwd = getcwd(NULL, 0);
4539 return got_error_from_errno("getcwd");
4541 error = got_worktree_open(&worktree, cwd);
4542 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4545 if (repo_path == NULL) {
4548 strdup(got_worktree_get_repo_path(worktree));
4550 repo_path = strdup(cwd);
4552 if (repo_path == NULL) {
4553 error = got_error_from_errno("strdup");
4557 error = got_repo_open(&repo, repo_path, NULL);
4561 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4568 error = apply_unveil(got_repo_get_path(repo), NULL);
4572 if (commit_id_str == NULL) {
4573 struct got_reference *head_ref;
4574 error = got_ref_open(&head_ref, repo, worktree ?
4575 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4578 error = got_ref_resolve(&commit_id, repo, head_ref);
4579 got_ref_close(head_ref);
4581 error = got_repo_match_object_id(&commit_id, NULL,
4582 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4587 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4591 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4593 error = got_error_from_errno("view_open");
4596 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4600 /* Release work tree lock. */
4601 got_worktree_close(worktree);
4604 error = view_loop(view);
4611 got_worktree_close(worktree);
4613 got_repo_close(repo);
4614 got_ref_list_free(&refs);
4618 static const struct got_error *
4619 draw_tree_entries(struct tog_view *view,
4620 struct got_tree_entry **first_displayed_entry,
4621 struct got_tree_entry **last_displayed_entry,
4622 struct got_tree_entry **selected_entry, int *ndisplayed,
4623 const char *label, int show_ids, const char *parent_path,
4624 struct got_tree_object *tree, int selected, int limit,
4625 int isroot, struct tog_colors *colors)
4627 const struct got_error *err = NULL;
4628 struct got_tree_entry *te;
4630 struct tog_color *tc;
4631 int width, n, i, nentries;
4635 werase(view->window);
4640 err = format_line(&wline, &width, label, view->ncols, 0);
4643 if (view_needs_focus_indication(view))
4644 wstandout(view->window);
4645 tc = get_color(colors, TOG_COLOR_COMMIT);
4647 wattr_on(view->window,
4648 COLOR_PAIR(tc->colorpair), NULL);
4649 waddwstr(view->window, wline);
4651 wattr_off(view->window,
4652 COLOR_PAIR(tc->colorpair), NULL);
4653 if (view_needs_focus_indication(view))
4654 wstandend(view->window);
4657 if (width < view->ncols - 1)
4658 waddch(view->window, '\n');
4661 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4664 waddwstr(view->window, wline);
4667 if (width < view->ncols - 1)
4668 waddch(view->window, '\n');
4671 waddch(view->window, '\n');
4675 if (*first_displayed_entry == NULL) {
4676 te = got_object_tree_get_first_entry(tree);
4677 if (selected == 0) {
4679 wstandout(view->window);
4680 *selected_entry = NULL;
4682 waddstr(view->window, " ..\n"); /* parent directory */
4683 if (selected == 0 && view->focussed)
4684 wstandend(view->window);
4691 te = *first_displayed_entry;
4694 nentries = got_object_tree_get_nentries(tree);
4695 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4696 char *line = NULL, *id_str = NULL;
4697 const char *modestr = "";
4700 te = got_object_tree_get_entry(tree, i);
4701 mode = got_tree_entry_get_mode(te);
4704 err = got_object_id_str(&id_str,
4705 got_tree_entry_get_id(te));
4707 return got_error_from_errno(
4708 "got_object_id_str");
4710 if (got_object_tree_entry_is_submodule(te))
4712 else if (S_ISLNK(mode))
4714 else if (S_ISDIR(mode))
4716 else if (mode & S_IXUSR)
4718 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4719 got_tree_entry_get_name(te), modestr) == -1) {
4721 return got_error_from_errno("asprintf");
4724 err = format_line(&wline, &width, line, view->ncols, 0);
4729 if (n == selected) {
4731 wstandout(view->window);
4732 *selected_entry = te;
4734 tc = match_color(colors, line);
4736 wattr_on(view->window,
4737 COLOR_PAIR(tc->colorpair), NULL);
4738 waddwstr(view->window, wline);
4740 wattr_off(view->window,
4741 COLOR_PAIR(tc->colorpair), NULL);
4742 if (width < view->ncols - 1)
4743 waddch(view->window, '\n');
4744 if (n == selected && view->focussed)
4745 wstandend(view->window);
4751 *last_displayed_entry = te;
4760 tree_scroll_up(struct tog_view *view,
4761 struct got_tree_entry **first_displayed_entry, int maxscroll,
4762 struct got_tree_object *tree, int isroot)
4764 struct got_tree_entry *te;
4767 if (*first_displayed_entry == NULL)
4770 te = got_object_tree_get_entry(tree, 0);
4771 if (*first_displayed_entry == te) {
4773 *first_displayed_entry = NULL;
4778 while (*first_displayed_entry && i < maxscroll) {
4779 *first_displayed_entry = got_tree_entry_get_prev(tree,
4780 *first_displayed_entry);
4783 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4784 *first_displayed_entry = NULL;
4788 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4789 struct got_tree_entry *last_displayed_entry,
4790 struct got_tree_object *tree)
4792 struct got_tree_entry *next, *last;
4795 if (*first_displayed_entry)
4796 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4798 next = got_object_tree_get_first_entry(tree);
4800 last = last_displayed_entry;
4801 while (next && last && n++ < maxscroll) {
4802 last = got_tree_entry_get_next(tree, last);
4804 *first_displayed_entry = next;
4805 next = got_tree_entry_get_next(tree, next);
4811 static const struct got_error *
4812 tree_entry_path(char **path, struct tog_parent_trees *parents,
4813 struct got_tree_entry *te)
4815 const struct got_error *err = NULL;
4816 struct tog_parent_tree *pt;
4817 size_t len = 2; /* for leading slash and NUL */
4819 TAILQ_FOREACH(pt, parents, entry)
4820 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4823 len += strlen(got_tree_entry_get_name(te));
4825 *path = calloc(1, len);
4827 return got_error_from_errno("calloc");
4830 pt = TAILQ_LAST(parents, tog_parent_trees);
4832 const char *name = got_tree_entry_get_name(pt->selected_entry);
4833 if (strlcat(*path, name, len) >= len) {
4834 err = got_error(GOT_ERR_NO_SPACE);
4837 if (strlcat(*path, "/", len) >= len) {
4838 err = got_error(GOT_ERR_NO_SPACE);
4841 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4844 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4845 err = got_error(GOT_ERR_NO_SPACE);
4857 static const struct got_error *
4858 blame_tree_entry(struct tog_view **new_view, int begin_x,
4859 struct got_tree_entry *te, struct tog_parent_trees *parents,
4860 struct got_object_id *commit_id, struct got_reflist_head *refs,
4861 struct got_repository *repo)
4863 const struct got_error *err = NULL;
4865 struct tog_view *blame_view;
4869 err = tree_entry_path(&path, parents, te);
4873 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4874 if (blame_view == NULL) {
4875 err = got_error_from_errno("view_open");
4879 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4881 if (err->code == GOT_ERR_CANCELLED)
4883 view_close(blame_view);
4885 *new_view = blame_view;
4891 static const struct got_error *
4892 log_tree_entry(struct tog_view **new_view, int begin_x,
4893 struct got_tree_entry *te, struct tog_parent_trees *parents,
4894 struct got_object_id *commit_id, struct got_reflist_head *refs,
4895 struct got_repository *repo)
4897 struct tog_view *log_view;
4898 const struct got_error *err = NULL;
4903 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4904 if (log_view == NULL)
4905 return got_error_from_errno("view_open");
4907 err = tree_entry_path(&path, parents, te);
4911 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4913 view_close(log_view);
4915 *new_view = log_view;
4920 static const struct got_error *
4921 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4922 struct got_object_id *commit_id, struct got_reflist_head *refs,
4923 struct got_repository *repo)
4925 const struct got_error *err = NULL;
4926 char *commit_id_str = NULL;
4927 struct tog_tree_view_state *s = &view->state.tree;
4929 TAILQ_INIT(&s->parents);
4931 err = got_object_id_str(&commit_id_str, commit_id);
4935 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4936 err = got_error_from_errno("asprintf");
4940 s->root = s->tree = root;
4941 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4942 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4943 s->commit_id = got_object_id_dup(commit_id);
4944 if (s->commit_id == NULL) {
4945 err = got_error_from_errno("got_object_id_dup");
4951 SIMPLEQ_INIT(&s->colors);
4953 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4954 err = add_color(&s->colors, "\\$$",
4955 TOG_COLOR_TREE_SUBMODULE,
4956 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4959 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4960 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4962 free_colors(&s->colors);
4965 err = add_color(&s->colors, "/$",
4966 TOG_COLOR_TREE_DIRECTORY,
4967 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4969 free_colors(&s->colors);
4973 err = add_color(&s->colors, "\\*$",
4974 TOG_COLOR_TREE_EXECUTABLE,
4975 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4977 free_colors(&s->colors);
4981 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4982 get_color_value("TOG_COLOR_COMMIT"));
4984 free_colors(&s->colors);
4989 view->show = show_tree_view;
4990 view->input = input_tree_view;
4991 view->close = close_tree_view;
4992 view->search_start = search_start_tree_view;
4993 view->search_next = search_next_tree_view;
4995 free(commit_id_str);
4997 free(s->tree_label);
4998 s->tree_label = NULL;
5003 static const struct got_error *
5004 close_tree_view(struct tog_view *view)
5006 struct tog_tree_view_state *s = &view->state.tree;
5008 free_colors(&s->colors);
5009 free(s->tree_label);
5010 s->tree_label = NULL;
5012 s->commit_id = NULL;
5013 while (!TAILQ_EMPTY(&s->parents)) {
5014 struct tog_parent_tree *parent;
5015 parent = TAILQ_FIRST(&s->parents);
5016 TAILQ_REMOVE(&s->parents, parent, entry);
5020 if (s->tree != s->root)
5021 got_object_tree_close(s->tree);
5022 got_object_tree_close(s->root);
5027 static const struct got_error *
5028 search_start_tree_view(struct tog_view *view)
5030 struct tog_tree_view_state *s = &view->state.tree;
5032 s->matched_entry = NULL;
5037 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5039 regmatch_t regmatch;
5041 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5045 static const struct got_error *
5046 search_next_tree_view(struct tog_view *view)
5048 struct tog_tree_view_state *s = &view->state.tree;
5049 struct got_tree_entry *te = NULL;
5051 if (!view->searching) {
5052 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5056 if (s->matched_entry) {
5057 if (view->searching == TOG_SEARCH_FORWARD) {
5058 if (s->selected_entry)
5059 te = got_tree_entry_get_next(s->tree,
5062 te = got_object_tree_get_first_entry(s->tree);
5064 if (s->selected_entry == NULL)
5065 te = got_object_tree_get_last_entry(s->tree);
5067 te = got_tree_entry_get_prev(s->tree,
5071 if (view->searching == TOG_SEARCH_FORWARD)
5072 te = got_object_tree_get_first_entry(s->tree);
5074 te = got_object_tree_get_last_entry(s->tree);
5079 if (s->matched_entry == NULL) {
5080 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5083 if (view->searching == TOG_SEARCH_FORWARD)
5084 te = got_object_tree_get_first_entry(s->tree);
5086 te = got_object_tree_get_last_entry(s->tree);
5089 if (match_tree_entry(te, &view->regex)) {
5090 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5091 s->matched_entry = te;
5095 if (view->searching == TOG_SEARCH_FORWARD)
5096 te = got_tree_entry_get_next(s->tree, te);
5098 te = got_tree_entry_get_prev(s->tree, te);
5101 if (s->matched_entry) {
5102 s->first_displayed_entry = s->matched_entry;
5109 static const struct got_error *
5110 show_tree_view(struct tog_view *view)
5112 const struct got_error *err = NULL;
5113 struct tog_tree_view_state *s = &view->state.tree;
5116 err = tree_entry_path(&parent_path, &s->parents, NULL);
5120 err = draw_tree_entries(view, &s->first_displayed_entry,
5121 &s->last_displayed_entry, &s->selected_entry,
5122 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5123 s->tree, s->selected, view->nlines, s->tree == s->root,
5131 static const struct got_error *
5132 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5133 struct tog_view **focus_view, struct tog_view *view, int ch)
5135 const struct got_error *err = NULL;
5136 struct tog_tree_view_state *s = &view->state.tree;
5137 struct tog_view *log_view;
5138 int begin_x = 0, nscrolled;
5142 s->show_ids = !s->show_ids;
5145 if (!s->selected_entry)
5147 if (view_is_parent_view(view))
5148 begin_x = view_split_begin_x(view->begin_x);
5149 err = log_tree_entry(&log_view, begin_x,
5150 s->selected_entry, &s->parents,
5151 s->commit_id, s->refs, s->repo);
5152 if (view_is_parent_view(view)) {
5153 err = view_close_child(view);
5156 err = view_set_child(view, log_view);
5158 view_close(log_view);
5161 *focus_view = log_view;
5162 view->child_focussed = 1;
5164 *new_view = log_view;
5168 if (s->selected > 0) {
5170 if (s->selected == 0)
5173 if (s->selected > 0)
5175 tree_scroll_up(view, &s->first_displayed_entry, 1,
5176 s->tree, s->tree == s->root);
5180 tree_scroll_up(view, &s->first_displayed_entry,
5181 MAX(0, view->nlines - 4 - s->selected), s->tree,
5182 s->tree == s->root);
5184 if (got_object_tree_get_first_entry(s->tree) ==
5185 s->first_displayed_entry && s->tree != s->root)
5186 s->first_displayed_entry = NULL;
5190 if (s->selected < s->ndisplayed - 1) {
5194 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5196 /* can't scroll any further */
5198 tree_scroll_down(&s->first_displayed_entry, 1,
5199 s->last_displayed_entry, s->tree);
5203 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5205 /* can't scroll any further; move cursor down */
5206 if (s->selected < s->ndisplayed - 1)
5207 s->selected = s->ndisplayed - 1;
5210 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5211 view->nlines, s->last_displayed_entry, s->tree);
5212 if (nscrolled < view->nlines) {
5214 struct got_tree_entry *te;
5215 te = s->first_displayed_entry;
5218 te = got_tree_entry_get_next(s->tree, te);
5220 s->selected = ndisplayed - 1;
5226 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5227 struct tog_parent_tree *parent;
5228 /* user selected '..' */
5229 if (s->tree == s->root)
5231 parent = TAILQ_FIRST(&s->parents);
5232 TAILQ_REMOVE(&s->parents, parent,
5234 got_object_tree_close(s->tree);
5235 s->tree = parent->tree;
5236 s->first_displayed_entry =
5237 parent->first_displayed_entry;
5239 parent->selected_entry;
5240 s->selected = parent->selected;
5242 } else if (S_ISDIR(got_tree_entry_get_mode(
5243 s->selected_entry))) {
5244 struct got_tree_object *subtree;
5245 err = got_object_open_as_tree(&subtree, s->repo,
5246 got_tree_entry_get_id(s->selected_entry));
5249 err = tree_view_visit_subtree(subtree, s);
5251 got_object_tree_close(subtree);
5254 } else if (S_ISREG(got_tree_entry_get_mode(
5255 s->selected_entry))) {
5256 struct tog_view *blame_view;
5257 int begin_x = view_is_parent_view(view) ?
5258 view_split_begin_x(view->begin_x) : 0;
5260 err = blame_tree_entry(&blame_view, begin_x,
5261 s->selected_entry, &s->parents,
5262 s->commit_id, s->refs, s->repo);
5265 if (view_is_parent_view(view)) {
5266 err = view_close_child(view);
5269 err = view_set_child(view, blame_view);
5271 view_close(blame_view);
5274 *focus_view = blame_view;
5275 view->child_focussed = 1;
5277 *new_view = blame_view;
5281 if (s->selected > view->nlines)
5282 s->selected = s->ndisplayed - 1;
5295 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5300 static const struct got_error *
5301 cmd_tree(int argc, char *argv[])
5303 const struct got_error *error;
5304 struct got_repository *repo = NULL;
5305 struct got_worktree *worktree = NULL;
5306 struct got_reflist_head refs;
5307 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5308 struct got_object_id *commit_id = NULL;
5309 char *commit_id_arg = NULL;
5310 struct got_commit_object *commit = NULL;
5311 struct got_tree_object *tree = NULL;
5313 struct tog_view *view;
5315 SIMPLEQ_INIT(&refs);
5318 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5323 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5326 commit_id_arg = optarg;
5329 repo_path = realpath(optarg, NULL);
5330 if (repo_path == NULL)
5331 return got_error_from_errno2("realpath",
5346 cwd = getcwd(NULL, 0);
5348 return got_error_from_errno("getcwd");
5350 error = got_worktree_open(&worktree, cwd);
5351 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5354 if (repo_path == NULL) {
5357 strdup(got_worktree_get_repo_path(worktree));
5359 repo_path = strdup(cwd);
5361 if (repo_path == NULL) {
5362 error = got_error_from_errno("strdup");
5366 error = got_repo_open(&repo, repo_path, NULL);
5370 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5377 error = apply_unveil(got_repo_get_path(repo), NULL);
5381 error = got_repo_match_object_id(&commit_id, NULL,
5382 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5383 GOT_OBJ_TYPE_COMMIT, 1, repo);
5387 error = got_object_open_as_commit(&commit, repo, commit_id);
5391 error = got_object_open_as_tree(&tree, repo,
5392 got_object_commit_get_tree_id(commit));
5396 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5400 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5402 error = got_error_from_errno("view_open");
5405 error = open_tree_view(view, tree, commit_id, &refs, repo);
5408 if (!got_path_is_root_dir(in_repo_path)) {
5409 error = tree_view_walk_path(&view->state.tree, commit_id,
5410 in_repo_path, repo);
5416 /* Release work tree lock. */
5417 got_worktree_close(worktree);
5420 error = view_loop(view);
5426 got_object_commit_close(commit);
5428 got_object_tree_close(tree);
5430 got_repo_close(repo);
5431 got_ref_list_free(&refs);
5440 fprintf(stderr, "commands:");
5441 for (i = 0; i < nitems(tog_commands); i++) {
5442 struct tog_cmd *cmd = &tog_commands[i];
5443 fprintf(stderr, " %s", cmd->name);
5445 fputc('\n', stderr);
5451 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5452 "[arg ...]\n", getprogname());
5454 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5461 make_argv(int argc, ...)
5469 argv = calloc(argc, sizeof(char *));
5472 for (i = 0; i < argc; i++) {
5473 argv[i] = strdup(va_arg(ap, char *));
5474 if (argv[i] == NULL)
5483 * Try to convert 'tog path' into a 'tog log path' command.
5484 * The user could simply have mistyped the command rather than knowingly
5485 * provided a path. So check whether argv[0] can in fact be resolved
5486 * to a path in the HEAD commit and print a special error if not.
5487 * This hack is for mpi@ <3
5489 static const struct got_error *
5490 tog_log_with_path(int argc, char *argv[])
5492 const struct got_error *error = NULL;
5493 struct tog_cmd *cmd = NULL;
5494 struct got_repository *repo = NULL;
5495 struct got_worktree *worktree = NULL;
5496 struct got_object_id *commit_id = NULL, *id = NULL;
5497 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5498 char *commit_id_str = NULL, **cmd_argv = NULL;
5500 cwd = getcwd(NULL, 0);
5502 return got_error_from_errno("getcwd");
5504 error = got_worktree_open(&worktree, cwd);
5505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5509 repo_path = strdup(got_worktree_get_repo_path(worktree));
5511 repo_path = strdup(cwd);
5512 if (repo_path == NULL) {
5513 error = got_error_from_errno("strdup");
5517 error = got_repo_open(&repo, repo_path, NULL);
5521 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5526 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5527 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5528 GOT_OBJ_TYPE_COMMIT, 1, repo);
5533 got_worktree_close(worktree);
5537 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5539 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5541 fprintf(stderr, "%s: '%s' is no known command or path\n",
5542 getprogname(), argv[0]);
5547 got_repo_close(repo);
5550 error = got_object_id_str(&commit_id_str, commit_id);
5554 cmd = &tog_commands[0]; /* log */
5556 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5557 error = cmd->cmd_main(argc, cmd_argv);
5560 got_repo_close(repo);
5562 got_worktree_close(worktree);
5564 free(commit_id_str);
5571 for (i = 0; i < argc; i++)
5579 main(int argc, char *argv[])
5581 const struct got_error *error = NULL;
5582 struct tog_cmd *cmd = NULL;
5583 int ch, hflag = 0, Vflag = 0;
5584 char **cmd_argv = NULL;
5585 static struct option longopts[] = {
5586 { "version", no_argument, NULL, 'V' },
5590 setlocale(LC_CTYPE, "");
5592 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5612 got_version_print_str();
5619 /* Build an argument vector which runs a default command. */
5620 cmd = &tog_commands[0];
5622 cmd_argv = make_argv(argc, cmd->name);
5626 /* Did the user specify a command? */
5627 for (i = 0; i < nitems(tog_commands); i++) {
5628 if (strncmp(tog_commands[i].name, argv[0],
5629 strlen(argv[0])) == 0) {
5630 cmd = &tog_commands[i];
5639 /* No command specified; try log with a path */
5640 error = tog_log_with_path(argc, argv);
5645 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5651 for (i = 0; i < argc; i++)
5656 if (error && error->code != GOT_ERR_CANCELLED)
5657 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);