2 * Copyright (c) 2018, 2019 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
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_opentemp.h"
47 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
52 #include "got_worktree.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #define CTRL(x) ((x) & 0x1f)
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
74 __dead static void usage(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log },
87 { "diff", cmd_diff, usage_diff },
88 { "blame", cmd_blame, usage_blame },
89 { "tree", cmd_tree, usage_tree },
99 #define TOG_EOF_STRING "(END)"
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
110 struct commit_queue_head head;
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
116 int first_displayed_line;
117 int last_displayed_line;
120 struct got_repository *repo;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
151 const char *head_ref_name;
152 struct got_repository *repo;
153 struct got_reflist_head *refs;
154 struct got_object_id *start_id;
157 struct tog_log_thread_args thread_args;
158 struct commit_queue_entry *matched_entry;
161 struct tog_blame_cb_args {
162 struct tog_blame_line *lines; /* one per line */
165 struct tog_view *view;
166 struct got_object_id *commit_id;
170 struct tog_blame_thread_args {
172 struct got_repository *repo;
173 struct tog_blame_cb_args *cb_args;
180 struct tog_blame_line *lines;
184 struct tog_blame_thread_args thread_args;
185 struct tog_blame_cb_args cb_args;
189 struct tog_blame_view_state {
190 int first_displayed_line;
191 int last_displayed_line;
196 struct got_object_id_queue blamed_commits;
197 struct got_object_qid *blamed_commit;
199 struct got_repository *repo;
200 struct got_reflist_head *refs;
201 struct got_object_id *commit_id;
202 struct tog_blame blame;
206 struct tog_parent_tree {
207 TAILQ_ENTRY(tog_parent_tree) entry;
208 struct got_tree_object *tree;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *selected_entry;
214 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
216 struct tog_tree_view_state {
218 struct got_tree_object *root;
219 struct got_tree_object *tree;
220 const struct got_tree_entries *entries;
221 struct got_tree_entry *first_displayed_entry;
222 struct got_tree_entry *last_displayed_entry;
223 struct got_tree_entry *selected_entry;
224 int ndisplayed, selected, show_ids;
225 struct tog_parent_trees parents;
226 struct got_object_id *commit_id;
227 struct got_repository *repo;
228 struct got_reflist_head *refs;
229 struct got_tree_entry *matched_entry;
233 * We implement two types of views: parent views and child views.
235 * The 'Tab' key switches between a parent view and its child view.
236 * Child views are shown side-by-side to their parent view, provided
237 * there is enough screen estate.
239 * When a new view is opened from within a parent view, this new view
240 * becomes a child view of the parent view, replacing any existing child.
242 * When a new view is opened from within a child view, this new view
243 * becomes a parent view which will obscure the views below until the
244 * user quits the new parent view by typing 'q'.
246 * This list of views contains parent views only.
247 * Child views are only pointed to by their parent view.
249 TAILQ_HEAD(tog_view_list_head, tog_view);
252 TAILQ_ENTRY(tog_view) entry;
255 int nlines, ncols, begin_y, begin_x;
256 int lines, cols; /* copies of LINES and COLS */
258 struct tog_view *parent;
259 struct tog_view *child;
262 /* type-specific state */
263 enum tog_view_type type;
265 struct tog_diff_view_state diff;
266 struct tog_log_view_state log;
267 struct tog_blame_view_state blame;
268 struct tog_tree_view_state tree;
271 const struct got_error *(*show)(struct tog_view *);
272 const struct got_error *(*input)(struct tog_view **,
273 struct tog_view **, struct tog_view**, struct tog_view *, int);
274 const struct got_error *(*close)(struct tog_view *);
276 const struct got_error *(*search_start)(struct tog_view *);
277 const struct got_error *(*search_next)(struct tog_view *);
279 #define TOG_SEARCH_FORWARD 1
280 #define TOG_SEARCH_BACKWARD 2
281 int search_next_done;
285 static const struct got_error *open_diff_view(struct tog_view *,
286 struct got_object_id *, struct got_object_id *, struct tog_view *,
287 struct got_reflist_head *, struct got_repository *);
288 static const struct got_error *show_diff_view(struct tog_view *);
289 static const struct got_error *input_diff_view(struct tog_view **,
290 struct tog_view **, struct tog_view **, struct tog_view *, int);
291 static const struct got_error* close_diff_view(struct tog_view *);
293 static const struct got_error *open_log_view(struct tog_view *,
294 struct got_object_id *, struct got_reflist_head *,
295 struct got_repository *, const char *, const char *, int);
296 static const struct got_error * show_log_view(struct tog_view *);
297 static const struct got_error *input_log_view(struct tog_view **,
298 struct tog_view **, struct tog_view **, struct tog_view *, int);
299 static const struct got_error *close_log_view(struct tog_view *);
300 static const struct got_error *search_start_log_view(struct tog_view *);
301 static const struct got_error *search_next_log_view(struct tog_view *);
303 static const struct got_error *open_blame_view(struct tog_view *, char *,
304 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
305 static const struct got_error *show_blame_view(struct tog_view *);
306 static const struct got_error *input_blame_view(struct tog_view **,
307 struct tog_view **, struct tog_view **, struct tog_view *, int);
308 static const struct got_error *close_blame_view(struct tog_view *);
309 static const struct got_error *search_start_blame_view(struct tog_view *);
310 static const struct got_error *search_next_blame_view(struct tog_view *);
312 static const struct got_error *open_tree_view(struct tog_view *,
313 struct got_tree_object *, struct got_object_id *,
314 struct got_reflist_head *, struct got_repository *);
315 static const struct got_error *show_tree_view(struct tog_view *);
316 static const struct got_error *input_tree_view(struct tog_view **,
317 struct tog_view **, struct tog_view **, struct tog_view *, int);
318 static const struct got_error *close_tree_view(struct tog_view *);
319 static const struct got_error *search_start_tree_view(struct tog_view *);
320 static const struct got_error *search_next_tree_view(struct tog_view *);
322 static volatile sig_atomic_t tog_sigwinch_received;
325 tog_sigwinch(int signo)
327 tog_sigwinch_received = 1;
330 static const struct got_error *
331 view_close(struct tog_view *view)
333 const struct got_error *err = NULL;
336 view_close(view->child);
340 err = view->close(view);
342 del_panel(view->panel);
344 delwin(view->window);
349 static struct tog_view *
350 view_open(int nlines, int ncols, int begin_y, int begin_x,
351 enum tog_view_type type)
353 struct tog_view *view = calloc(1, sizeof(*view));
361 view->nlines = nlines ? nlines : LINES - begin_y;
362 view->ncols = ncols ? ncols : COLS - begin_x;
363 view->begin_y = begin_y;
364 view->begin_x = begin_x;
365 view->window = newwin(nlines, ncols, begin_y, begin_x);
366 if (view->window == NULL) {
370 view->panel = new_panel(view->window);
371 if (view->panel == NULL ||
372 set_panel_userptr(view->panel, view) != OK) {
377 keypad(view->window, TRUE);
382 view_split_begin_x(int begin_x)
384 if (begin_x > 0 || COLS < 120)
386 return (COLS - MAX(COLS / 2, 80));
389 static const struct got_error *view_resize(struct tog_view *);
391 static const struct got_error *
392 view_splitscreen(struct tog_view *view)
394 const struct got_error *err = NULL;
397 view->begin_x = view_split_begin_x(0);
398 view->nlines = LINES;
399 view->ncols = COLS - view->begin_x;
402 err = view_resize(view);
406 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
407 return got_error_from_errno("mvwin");
412 static const struct got_error *
413 view_fullscreen(struct tog_view *view)
415 const struct got_error *err = NULL;
419 view->nlines = LINES;
423 err = view_resize(view);
427 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
428 return got_error_from_errno("mvwin");
434 view_is_parent_view(struct tog_view *view)
436 return view->parent == NULL;
439 static const struct got_error *
440 view_resize(struct tog_view *view)
444 if (view->lines > LINES)
445 nlines = view->nlines - (view->lines - LINES);
447 nlines = view->nlines + (LINES - view->lines);
449 if (view->cols > COLS)
450 ncols = view->ncols - (view->cols - COLS);
452 ncols = view->ncols + (COLS - view->cols);
454 if (wresize(view->window, nlines, ncols) == ERR)
455 return got_error_from_errno("wresize");
456 if (replace_panel(view->panel, view->window) == ERR)
457 return got_error_from_errno("replace_panel");
458 wclear(view->window);
460 view->nlines = nlines;
466 view->child->begin_x = view_split_begin_x(view->begin_x);
467 if (view->child->begin_x == 0) {
468 view_fullscreen(view->child);
469 if (view->child->focussed)
470 show_panel(view->child->panel);
472 show_panel(view->panel);
474 view_splitscreen(view->child);
475 show_panel(view->child->panel);
482 static const struct got_error *
483 view_close_child(struct tog_view *view)
485 const struct got_error *err = NULL;
487 if (view->child == NULL)
490 err = view_close(view->child);
495 static const struct got_error *
496 view_set_child(struct tog_view *view, struct tog_view *child)
498 const struct got_error *err = NULL;
501 child->parent = view;
506 view_is_splitscreen(struct tog_view *view)
508 return view->begin_x > 0;
517 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
518 cols = 80; /* Default */
524 resize_term(lines, cols);
527 static const struct got_error *
528 view_search_start(struct tog_view *view)
530 const struct got_error *err = NULL;
534 if (view->nlines < 1)
537 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
539 wclrtoeol(view->window);
543 ret = wgetnstr(view->window, pattern, sizeof(pattern));
549 if (view->searching) {
550 regfree(&view->regex);
554 if (regcomp(&view->regex, pattern,
555 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
556 err = view->search_start(view);
558 regfree(&view->regex);
561 view->searching = TOG_SEARCH_FORWARD;
562 view->search_next_done = 0;
563 view->search_next(view);
569 static const struct got_error *
570 view_input(struct tog_view **new, struct tog_view **dead,
571 struct tog_view **focus, int *done, struct tog_view *view,
572 struct tog_view_list_head *views)
574 const struct got_error *err = NULL;
582 if (view->searching && !view->search_next_done) {
583 errcode = pthread_mutex_unlock(&tog_mutex);
585 return got_error_set_errno(errcode,
586 "pthread_mutex_unlock");
588 errcode = pthread_mutex_lock(&tog_mutex);
590 return got_error_set_errno(errcode,
591 "pthread_mutex_lock");
592 view->search_next(view);
596 nodelay(stdscr, FALSE);
597 /* Allow threads to make progress while we are waiting for input. */
598 errcode = pthread_mutex_unlock(&tog_mutex);
600 return got_error_set_errno(errcode, "pthread_mutex_unlock");
601 ch = wgetch(view->window);
602 errcode = pthread_mutex_lock(&tog_mutex);
604 return got_error_set_errno(errcode, "pthread_mutex_lock");
605 nodelay(stdscr, TRUE);
607 if (tog_sigwinch_received) {
609 tog_sigwinch_received = 0;
610 TAILQ_FOREACH(v, views, entry) {
611 err = view_resize(v);
614 err = v->input(new, dead, focus, v, KEY_RESIZE);
625 *focus = view->child;
626 view->child_focussed = 1;
627 } else if (view->parent) {
628 *focus = view->parent;
629 view->parent->child_focussed = 0;
633 err = view->input(new, dead, focus, view, ch);
640 if (view_is_parent_view(view)) {
641 if (view->child == NULL)
643 if (view_is_splitscreen(view->child)) {
644 *focus = view->child;
645 view->child_focussed = 1;
646 err = view_fullscreen(view->child);
648 err = view_splitscreen(view->child);
651 err = view->child->input(new, dead, focus,
652 view->child, KEY_RESIZE);
654 if (view_is_splitscreen(view)) {
656 view->parent->child_focussed = 1;
657 err = view_fullscreen(view);
659 err = view_splitscreen(view);
663 err = view->input(new, dead, focus, view,
670 if (view->search_start)
671 view_search_start(view);
673 err = view->input(new, dead, focus, view, ch);
677 if (view->search_next && view->searching) {
678 view->searching = (ch == 'n' ?
679 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
680 view->search_next_done = 0;
681 view->search_next(view);
683 err = view->input(new, dead, focus, view, ch);
686 err = view->input(new, dead, focus, view, ch);
694 view_vborder(struct tog_view *view)
697 struct tog_view *view_above;
700 return view_vborder(view->parent);
702 panel = panel_above(view->panel);
706 view_above = panel_userptr(panel);
707 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
708 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
712 view_needs_focus_indication(struct tog_view *view)
714 if (view_is_parent_view(view)) {
715 if (view->child == NULL || view->child_focussed)
717 if (!view_is_splitscreen(view->child))
719 } else if (!view_is_splitscreen(view))
722 return view->focussed;
725 static const struct got_error *
726 view_loop(struct tog_view *view)
728 const struct got_error *err = NULL;
729 struct tog_view_list_head views;
730 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
731 int fast_refresh = 10;
732 int done = 0, errcode;
734 errcode = pthread_mutex_lock(&tog_mutex);
736 return got_error_set_errno(errcode, "pthread_mutex_lock");
739 TAILQ_INSERT_HEAD(&views, view, entry);
743 err = view->show(view);
748 while (!TAILQ_EMPTY(&views) && !done) {
749 /* Refresh fast during initialization, then become slower. */
750 if (fast_refresh && fast_refresh-- == 0)
751 halfdelay(10); /* switch to once per second */
753 err = view_input(&new_view, &dead_view, &focus_view, &done,
758 struct tog_view *prev = NULL;
760 if (view_is_parent_view(dead_view))
761 prev = TAILQ_PREV(dead_view,
762 tog_view_list_head, entry);
763 else if (view->parent != dead_view)
766 if (dead_view->parent)
767 dead_view->parent->child = NULL;
769 TAILQ_REMOVE(&views, dead_view, entry);
771 err = view_close(dead_view);
772 if (err || (dead_view == main_view && new_view == NULL))
775 if (view == dead_view) {
780 else if (!TAILQ_EMPTY(&views))
781 view = TAILQ_LAST(&views,
786 if (view->child && view->child_focussed)
787 focus_view = view->child;
794 struct tog_view *v, *t;
795 /* Only allow one parent view per type. */
796 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
797 if (v->type != new_view->type)
799 TAILQ_REMOVE(&views, v, entry);
805 TAILQ_INSERT_TAIL(&views, new_view, entry);
807 if (focus_view == NULL)
808 focus_view = new_view;
811 show_panel(focus_view->panel);
814 focus_view->focussed = 1;
817 show_panel(new_view->panel);
818 if (view->child && view_is_splitscreen(view->child))
819 show_panel(view->child->panel);
822 if (focus_view == NULL) {
824 show_panel(view->panel);
825 if (view->child && view_is_splitscreen(view->child))
826 show_panel(view->child->panel);
830 err = view->parent->show(view->parent);
834 err = view->show(view);
838 err = view->child->show(view->child);
847 while (!TAILQ_EMPTY(&views)) {
848 view = TAILQ_FIRST(&views);
849 TAILQ_REMOVE(&views, view, entry);
853 errcode = pthread_mutex_unlock(&tog_mutex);
855 return got_error_set_errno(errcode, "pthread_mutex_unlock");
865 "usage: %s log [-c commit] [-r repository-path] [path]\n",
870 /* Create newly allocated wide-character string equivalent to a byte string. */
871 static const struct got_error *
872 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
875 const struct got_error *err = NULL;
878 *wlen = mbstowcs(NULL, s, 0);
879 if (*wlen == (size_t)-1) {
882 return got_error_from_errno("mbstowcs");
884 /* byte string invalid in current encoding; try to "fix" it */
885 err = got_mbsavis(&vis, &vislen, s);
888 *wlen = mbstowcs(NULL, vis, 0);
889 if (*wlen == (size_t)-1) {
890 err = got_error_from_errno("mbstowcs"); /* give up */
895 *ws = calloc(*wlen + 1, sizeof(*ws));
897 err = got_error_from_errno("calloc");
901 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
902 err = got_error_from_errno("mbstowcs");
913 /* Format a line for display, ensuring that it won't overflow a width limit. */
914 static const struct got_error *
915 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
917 const struct got_error *err = NULL;
919 wchar_t *wline = NULL;
926 err = mbs2ws(&wline, &wlen, line);
931 while (i < wlen && cols < wlimit) {
932 int width = wcwidth(wline[i]);
939 if (cols + width <= wlimit)
944 if (wline[i] == L'\t')
945 cols += TABSIZE - ((cols + 1) % TABSIZE);
949 err = got_error_from_errno("wcwidth");
964 static const struct got_error*
965 build_refs_str(char **refs_str, struct got_reflist_head *refs,
966 struct got_object_id *id)
968 static const struct got_error *err = NULL;
969 struct got_reflist_entry *re;
975 SIMPLEQ_FOREACH(re, refs, entry) {
976 if (got_object_id_cmp(re->id, id) != 0)
978 name = got_ref_get_name(re->ref);
979 if (strcmp(name, GOT_REF_HEAD) == 0)
981 if (strncmp(name, "refs/", 5) == 0)
983 if (strncmp(name, "got/", 4) == 0)
985 if (strncmp(name, "heads/", 6) == 0)
987 if (strncmp(name, "remotes/", 8) == 0)
990 if (asprintf(refs_str, "%s%s%s", s ? s : "",
991 s ? ", " : "", name) == -1) {
992 err = got_error_from_errno("asprintf");
1003 static const struct got_error *
1004 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1006 char *smallerthan, *at;
1008 smallerthan = strchr(author, '<');
1009 if (smallerthan && smallerthan[1] != '\0')
1010 author = smallerthan + 1;
1011 at = strchr(author, '@');
1014 return format_line(wauthor, author_width, author, limit);
1017 static const struct got_error *
1018 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1019 struct got_object_id *id, struct got_reflist_head *refs,
1020 int author_display_cols)
1022 const struct got_error *err = NULL;
1023 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1024 char *logmsg0 = NULL, *logmsg = NULL;
1025 char *author = NULL;
1026 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1027 int author_width, logmsg_width;
1028 char *newline, *line = NULL;
1030 static const size_t date_display_cols = 9;
1031 const int avail = view->ncols;
1033 time_t committer_time;
1035 committer_time = got_object_commit_get_committer_time(commit);
1036 if (localtime_r(&committer_time, &tm) == NULL)
1037 return got_error_from_errno("localtime_r");
1038 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1040 return got_error(GOT_ERR_NO_SPACE);
1042 if (avail < date_display_cols)
1043 limit = MIN(sizeof(datebuf) - 1, avail);
1045 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1046 waddnstr(view->window, datebuf, limit);
1051 author = strdup(got_object_commit_get_author(commit));
1052 if (author == NULL) {
1053 err = got_error_from_errno("strdup");
1056 err = format_author(&wauthor, &author_width, author, avail - col);
1059 waddwstr(view->window, wauthor);
1060 col += author_width;
1061 while (col <= avail && author_width < author_display_cols + 2) {
1062 waddch(view->window, ' ');
1069 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1070 if (logmsg0 == NULL) {
1071 err = got_error_from_errno("strdup");
1075 while (*logmsg == '\n')
1077 newline = strchr(logmsg, '\n');
1080 limit = avail - col;
1081 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1084 waddwstr(view->window, wlogmsg);
1085 col += logmsg_width;
1086 while (col <= avail) {
1087 waddch(view->window, ' ');
1099 static struct commit_queue_entry *
1100 alloc_commit_queue_entry(struct got_commit_object *commit,
1101 struct got_object_id *id)
1103 struct commit_queue_entry *entry;
1105 entry = calloc(1, sizeof(*entry));
1110 entry->commit = commit;
1115 pop_commit(struct commit_queue *commits)
1117 struct commit_queue_entry *entry;
1119 entry = TAILQ_FIRST(&commits->head);
1120 TAILQ_REMOVE(&commits->head, entry, entry);
1121 got_object_commit_close(entry->commit);
1122 commits->ncommits--;
1123 /* Don't free entry->id! It is owned by the commit graph. */
1128 free_commits(struct commit_queue *commits)
1130 while (!TAILQ_EMPTY(&commits->head))
1131 pop_commit(commits);
1134 static const struct got_error *
1135 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1136 int minqueue, struct got_repository *repo, const char *path)
1138 const struct got_error *err = NULL;
1142 * We keep all commits open throughout the lifetime of the log
1143 * view in order to avoid having to re-fetch commits from disk
1144 * while updating the display.
1146 while (nqueued < minqueue) {
1147 struct got_object_id *id;
1148 struct got_commit_object *commit;
1149 struct commit_queue_entry *entry;
1152 err = got_commit_graph_iter_next(&id, graph);
1154 if (err->code != GOT_ERR_ITER_NEED_MORE)
1156 err = got_commit_graph_fetch_commits(graph,
1166 err = got_object_open_as_commit(&commit, repo, id);
1169 entry = alloc_commit_queue_entry(commit, id);
1170 if (entry == NULL) {
1171 err = got_error_from_errno("alloc_commit_queue_entry");
1175 errcode = pthread_mutex_lock(&tog_mutex);
1177 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1181 entry->idx = commits->ncommits;
1182 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1184 commits->ncommits++;
1186 errcode = pthread_mutex_unlock(&tog_mutex);
1187 if (errcode && err == NULL)
1188 err = got_error_set_errno(errcode,
1189 "pthread_mutex_unlock");
1195 static const struct got_error *
1196 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1197 struct got_repository *repo)
1199 const struct got_error *err = NULL;
1200 struct got_reference *head_ref;
1204 err = got_ref_open(&head_ref, repo, branch_name, 0);
1208 err = got_ref_resolve(head_id, repo, head_ref);
1209 got_ref_close(head_ref);
1218 static const struct got_error *
1219 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1220 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1221 struct commit_queue *commits, int selected_idx, int limit,
1222 struct got_reflist_head *refs, const char *path, int commits_needed)
1224 const struct got_error *err = NULL;
1225 struct commit_queue_entry *entry;
1227 int ncommits, author_cols = 10;
1228 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1229 char *refs_str = NULL;
1235 if (ncommits == selected_idx) {
1239 entry = TAILQ_NEXT(entry, entry);
1243 if (*selected && !(view->searching && view->search_next_done == 0)) {
1244 err = got_object_id_str(&id_str, (*selected)->id);
1248 err = build_refs_str(&refs_str, refs, (*selected)->id);
1254 if (commits_needed == 0)
1255 halfdelay(10); /* disable fast refresh */
1257 if (asprintf(&ncommits_str, " [%d/%d] %s",
1258 entry ? entry->idx + 1 : 0, commits->ncommits,
1259 commits_needed > 0 ?
1260 (view->searching && view->search_next_done == 0
1261 ? "searching..." : "loading... ") :
1262 (refs_str ? refs_str : "")) == -1) {
1263 err = got_error_from_errno("asprintf");
1267 if (path && strcmp(path, "/") != 0) {
1268 if (asprintf(&header, "commit %s %s%s",
1269 id_str ? id_str : "........................................",
1270 path, ncommits_str) == -1) {
1271 err = got_error_from_errno("asprintf");
1275 } else if (asprintf(&header, "commit %s%s",
1276 id_str ? id_str : "........................................",
1277 ncommits_str) == -1) {
1278 err = got_error_from_errno("asprintf");
1282 err = format_line(&wline, &width, header, view->ncols);
1286 werase(view->window);
1288 if (view_needs_focus_indication(view))
1289 wstandout(view->window);
1290 waddwstr(view->window, wline);
1291 while (width < view->ncols) {
1292 waddch(view->window, ' ');
1295 if (view_needs_focus_indication(view))
1296 wstandend(view->window);
1301 /* Grow author column size if necessary. */
1308 if (ncommits >= limit - 1)
1310 author = strdup(got_object_commit_get_author(entry->commit));
1311 if (author == NULL) {
1312 err = got_error_from_errno("strdup");
1315 err = format_author(&wauthor, &width, author, COLS);
1316 if (author_cols < width)
1317 author_cols = width;
1320 entry = TAILQ_NEXT(entry, entry);
1327 if (ncommits >= limit - 1)
1329 if (ncommits == selected_idx)
1330 wstandout(view->window);
1331 err = draw_commit(view, entry->commit, entry->id, refs,
1333 if (ncommits == selected_idx)
1334 wstandend(view->window);
1339 entry = TAILQ_NEXT(entry, entry);
1352 scroll_up(struct tog_view *view,
1353 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1354 struct commit_queue *commits)
1356 struct commit_queue_entry *entry;
1359 entry = TAILQ_FIRST(&commits->head);
1360 if (*first_displayed_entry == entry)
1363 entry = *first_displayed_entry;
1364 while (entry && nscrolled < maxscroll) {
1365 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1367 *first_displayed_entry = entry;
1373 static const struct got_error *
1374 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1375 pthread_cond_t *need_commits)
1380 halfdelay(1); /* fast refresh while loading commits */
1382 while (*commits_needed > 0) {
1386 /* Wake the log thread. */
1387 errcode = pthread_cond_signal(need_commits);
1389 return got_error_set_errno(errcode,
1390 "pthread_cond_signal");
1391 errcode = pthread_mutex_unlock(&tog_mutex);
1393 return got_error_set_errno(errcode,
1394 "pthread_mutex_unlock");
1396 errcode = pthread_mutex_lock(&tog_mutex);
1398 return got_error_set_errno(errcode,
1399 "pthread_mutex_lock");
1401 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1403 * Thread is not done yet; lose a key press
1404 * and let the user retry... this way the GUI
1405 * remains interactive while logging deep paths
1406 * with few commits in history.
1415 static const struct got_error *
1416 scroll_down(struct tog_view *view,
1417 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1418 struct commit_queue_entry **last_displayed_entry,
1419 struct commit_queue *commits, int *log_complete, int *commits_needed,
1420 pthread_cond_t *need_commits)
1422 const struct got_error *err = NULL;
1423 struct commit_queue_entry *pentry;
1426 if (*last_displayed_entry == NULL)
1429 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1430 if (pentry == NULL && !*log_complete) {
1432 * Ask the log thread for required amount of commits
1433 * plus some amount of pre-fetching.
1435 (*commits_needed) += maxscroll + 20;
1436 err = trigger_log_thread(0, commits_needed, log_complete,
1443 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1447 *last_displayed_entry = pentry;
1449 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1452 *first_displayed_entry = pentry;
1453 } while (++nscrolled < maxscroll);
1458 static const struct got_error *
1459 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1460 struct got_commit_object *commit, struct got_object_id *commit_id,
1461 struct tog_view *log_view, struct got_reflist_head *refs,
1462 struct got_repository *repo)
1464 const struct got_error *err;
1465 struct got_object_qid *parent_id;
1466 struct tog_view *diff_view;
1468 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1469 if (diff_view == NULL)
1470 return got_error_from_errno("view_open");
1472 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1473 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1474 commit_id, log_view, refs, repo);
1476 *new_view = diff_view;
1480 static const struct got_error *
1481 tree_view_visit_subtree(struct got_tree_object *subtree,
1482 struct tog_tree_view_state *s)
1484 struct tog_parent_tree *parent;
1486 parent = calloc(1, sizeof(*parent));
1488 return got_error_from_errno("calloc");
1490 parent->tree = s->tree;
1491 parent->first_displayed_entry = s->first_displayed_entry;
1492 parent->selected_entry = s->selected_entry;
1493 parent->selected = s->selected;
1494 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1496 s->entries = got_object_tree_get_entries(s->tree);
1498 s->first_displayed_entry = NULL;
1503 static const struct got_error *
1504 browse_commit_tree(struct tog_view **new_view, int begin_x,
1505 struct commit_queue_entry *entry, const char *path,
1506 struct got_reflist_head *refs, struct got_repository *repo)
1508 const struct got_error *err = NULL;
1509 struct got_tree_object *tree;
1510 struct got_tree_entry *te;
1511 struct tog_tree_view_state *s;
1512 struct tog_view *tree_view;
1513 char *slash, *subpath = NULL;
1516 err = got_object_open_as_tree(&tree, repo,
1517 got_object_commit_get_tree_id(entry->commit));
1521 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1522 if (tree_view == NULL)
1523 return got_error_from_errno("view_open");
1525 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1527 got_object_tree_close(tree);
1530 s = &tree_view->state.tree;
1532 *new_view = tree_view;
1534 /* Walk the path and open corresponding tree objects. */
1537 struct got_object_id *tree_id;
1542 /* Ensure the correct subtree entry is selected. */
1543 slash = strchr(p, '/');
1545 slash = strchr(p, '\0');
1546 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1547 if (strncmp(p, te->name, slash - p) == 0) {
1548 s->selected_entry = te;
1553 if (s->selected_entry == NULL) {
1554 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1557 if (s->tree != s->root)
1558 s->selected++; /* skip '..' */
1560 if (!S_ISDIR(s->selected_entry->mode)) {
1561 /* Jump to this file's entry. */
1562 s->first_displayed_entry = s->selected_entry;
1567 slash = strchr(p, '/');
1569 subpath = strndup(path, slash - path);
1571 subpath = strdup(path);
1572 if (subpath == NULL) {
1573 err = got_error_from_errno("strdup");
1577 err = got_object_id_by_path(&tree_id, repo, entry->id,
1582 err = got_object_open_as_tree(&tree, repo, tree_id);
1587 err = tree_view_visit_subtree(tree, s);
1589 got_object_tree_close(tree);
1604 log_thread(void *arg)
1606 const struct got_error *err = NULL;
1608 struct tog_log_thread_args *a = arg;
1611 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1615 while (!done && !err) {
1616 err = queue_commits(a->graph, a->commits, 1, a->repo,
1619 if (err->code != GOT_ERR_ITER_COMPLETED)
1623 } else if (a->commits_needed > 0)
1624 a->commits_needed--;
1626 errcode = pthread_mutex_lock(&tog_mutex);
1628 err = got_error_set_errno(errcode,
1629 "pthread_mutex_lock");
1631 } else if (*a->quit)
1633 else if (*a->first_displayed_entry == NULL) {
1634 *a->first_displayed_entry =
1635 TAILQ_FIRST(&a->commits->head);
1636 *a->selected_entry = *a->first_displayed_entry;
1640 a->commits_needed = 0;
1641 else if (a->commits_needed == 0) {
1642 errcode = pthread_cond_wait(&a->need_commits,
1645 err = got_error_set_errno(errcode,
1646 "pthread_cond_wait");
1649 errcode = pthread_mutex_unlock(&tog_mutex);
1650 if (errcode && err == NULL)
1651 err = got_error_set_errno(errcode,
1652 "pthread_mutex_unlock");
1654 a->log_complete = 1;
1658 static const struct got_error *
1659 stop_log_thread(struct tog_log_view_state *s)
1661 const struct got_error *err = NULL;
1666 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1668 return got_error_set_errno(errcode,
1669 "pthread_cond_signal");
1670 errcode = pthread_mutex_unlock(&tog_mutex);
1672 return got_error_set_errno(errcode,
1673 "pthread_mutex_unlock");
1674 errcode = pthread_join(s->thread, (void **)&err);
1676 return got_error_set_errno(errcode, "pthread_join");
1677 errcode = pthread_mutex_lock(&tog_mutex);
1679 return got_error_set_errno(errcode,
1680 "pthread_mutex_lock");
1684 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1685 if (errcode && err == NULL)
1686 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1688 if (s->thread_args.repo) {
1689 got_repo_close(s->thread_args.repo);
1690 s->thread_args.repo = NULL;
1693 if (s->thread_args.graph) {
1694 got_commit_graph_close(s->thread_args.graph);
1695 s->thread_args.graph = NULL;
1701 static const struct got_error *
1702 close_log_view(struct tog_view *view)
1704 const struct got_error *err = NULL;
1705 struct tog_log_view_state *s = &view->state.log;
1707 err = stop_log_thread(s);
1708 free_commits(&s->commits);
1709 free(s->in_repo_path);
1710 s->in_repo_path = NULL;
1716 static const struct got_error *
1717 search_start_log_view(struct tog_view *view)
1719 struct tog_log_view_state *s = &view->state.log;
1721 s->matched_entry = NULL;
1726 match_commit(struct got_commit_object *commit, regex_t *regex)
1728 regmatch_t regmatch;
1730 if (regexec(regex, got_object_commit_get_author(commit), 1,
1731 ®match, 0) == 0 ||
1732 regexec(regex, got_object_commit_get_committer(commit), 1,
1733 ®match, 0) == 0 ||
1734 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1741 static const struct got_error *
1742 search_next_log_view(struct tog_view *view)
1744 const struct got_error *err = NULL;
1745 struct tog_log_view_state *s = &view->state.log;
1746 struct commit_queue_entry *entry;
1748 if (!view->searching) {
1749 view->search_next_done = 1;
1753 if (s->matched_entry) {
1754 if (view->searching == TOG_SEARCH_FORWARD)
1755 entry = TAILQ_NEXT(s->selected_entry, entry);
1757 entry = TAILQ_PREV(s->selected_entry,
1758 commit_queue_head, entry);
1760 if (view->searching == TOG_SEARCH_FORWARD)
1761 entry = TAILQ_FIRST(&s->commits.head);
1763 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1767 if (entry == NULL) {
1768 if (s->thread_args.log_complete ||
1769 view->searching == TOG_SEARCH_BACKWARD) {
1770 view->search_next_done = 1;
1773 s->thread_args.commits_needed = 1;
1774 return trigger_log_thread(0,
1775 &s->thread_args.commits_needed,
1776 &s->thread_args.log_complete,
1777 &s->thread_args.need_commits);
1780 if (match_commit(entry->commit, &view->regex)) {
1781 view->search_next_done = 1;
1782 s->matched_entry = entry;
1785 if (view->searching == TOG_SEARCH_FORWARD)
1786 entry = TAILQ_NEXT(entry, entry);
1788 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1791 if (s->matched_entry) {
1792 int cur = s->selected_entry->idx;
1793 while (cur < s->matched_entry->idx) {
1794 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1799 while (cur > s->matched_entry->idx) {
1800 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1810 static const struct got_error *
1811 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1812 struct got_reflist_head *refs, struct got_repository *repo,
1813 const char *head_ref_name, const char *path, int check_disk)
1815 const struct got_error *err = NULL;
1816 struct tog_log_view_state *s = &view->state.log;
1817 struct got_repository *thread_repo = NULL;
1818 struct got_commit_graph *thread_graph = NULL;
1821 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1825 /* The commit queue only contains commits being displayed. */
1826 TAILQ_INIT(&s->commits.head);
1827 s->commits.ncommits = 0;
1831 s->head_ref_name = head_ref_name;
1832 s->start_id = got_object_id_dup(start_id);
1833 if (s->start_id == NULL) {
1834 err = got_error_from_errno("got_object_id_dup");
1838 view->show = show_log_view;
1839 view->input = input_log_view;
1840 view->close = close_log_view;
1841 view->search_start = search_start_log_view;
1842 view->search_next = search_next_log_view;
1844 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1847 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1852 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1854 err = got_error_set_errno(errcode, "pthread_cond_init");
1858 s->thread_args.commits_needed = view->nlines;
1859 s->thread_args.graph = thread_graph;
1860 s->thread_args.commits = &s->commits;
1861 s->thread_args.in_repo_path = s->in_repo_path;
1862 s->thread_args.start_id = s->start_id;
1863 s->thread_args.repo = thread_repo;
1864 s->thread_args.log_complete = 0;
1865 s->thread_args.quit = &s->quit;
1866 s->thread_args.view = view;
1867 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1868 s->thread_args.selected_entry = &s->selected_entry;
1871 close_log_view(view);
1875 static const struct got_error *
1876 show_log_view(struct tog_view *view)
1878 struct tog_log_view_state *s = &view->state.log;
1880 if (s->thread == NULL) {
1881 int errcode = pthread_create(&s->thread, NULL, log_thread,
1884 return got_error_set_errno(errcode, "pthread_create");
1887 return draw_commits(view, &s->last_displayed_entry,
1888 &s->selected_entry, s->first_displayed_entry,
1889 &s->commits, s->selected, view->nlines, s->refs,
1890 s->in_repo_path, s->thread_args.commits_needed);
1893 static const struct got_error *
1894 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1895 struct tog_view **focus_view, struct tog_view *view, int ch)
1897 const struct got_error *err = NULL;
1898 struct tog_log_view_state *s = &view->state.log;
1899 char *parent_path, *in_repo_path = NULL;
1900 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1902 struct got_object_id *start_id;
1912 if (s->first_displayed_entry == NULL)
1914 if (s->selected > 0)
1917 scroll_up(view, &s->first_displayed_entry, 1,
1922 if (s->first_displayed_entry == NULL)
1924 if (TAILQ_FIRST(&s->commits.head) ==
1925 s->first_displayed_entry) {
1929 scroll_up(view, &s->first_displayed_entry,
1930 view->nlines, &s->commits);
1936 if (s->first_displayed_entry == NULL)
1938 if (s->selected < MIN(view->nlines - 2,
1939 s->commits.ncommits - 1)) {
1943 err = scroll_down(view, &s->first_displayed_entry, 1,
1944 &s->last_displayed_entry, &s->commits,
1945 &s->thread_args.log_complete,
1946 &s->thread_args.commits_needed,
1947 &s->thread_args.need_commits);
1951 struct commit_queue_entry *first;
1952 first = s->first_displayed_entry;
1955 err = scroll_down(view, &s->first_displayed_entry,
1956 view->nlines, &s->last_displayed_entry,
1957 &s->commits, &s->thread_args.log_complete,
1958 &s->thread_args.commits_needed,
1959 &s->thread_args.need_commits);
1960 if (first == s->first_displayed_entry &&
1961 s->selected < MIN(view->nlines - 2,
1962 s->commits.ncommits - 1)) {
1963 /* can't scroll further down */
1964 s->selected = MIN(view->nlines - 2,
1965 s->commits.ncommits - 1);
1971 if (s->selected > view->nlines - 2)
1972 s->selected = view->nlines - 2;
1973 if (s->selected > s->commits.ncommits - 1)
1974 s->selected = s->commits.ncommits - 1;
1979 if (s->selected_entry == NULL)
1981 if (view_is_parent_view(view))
1982 begin_x = view_split_begin_x(view->begin_x);
1983 err = open_diff_view_for_commit(&diff_view, begin_x,
1984 s->selected_entry->commit, s->selected_entry->id,
1985 view, s->refs, s->repo);
1988 if (view_is_parent_view(view)) {
1989 err = view_close_child(view);
1992 err = view_set_child(view, diff_view);
1994 view_close(diff_view);
1997 *focus_view = diff_view;
1998 view->child_focussed = 1;
2000 *new_view = diff_view;
2003 if (s->selected_entry == NULL)
2005 if (view_is_parent_view(view))
2006 begin_x = view_split_begin_x(view->begin_x);
2007 err = browse_commit_tree(&tree_view, begin_x,
2008 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2011 if (view_is_parent_view(view)) {
2012 err = view_close_child(view);
2015 err = view_set_child(view, tree_view);
2017 view_close(tree_view);
2020 *focus_view = tree_view;
2021 view->child_focussed = 1;
2023 *new_view = tree_view;
2026 if (strcmp(s->in_repo_path, "/") == 0)
2028 parent_path = dirname(s->in_repo_path);
2029 if (parent_path && strcmp(parent_path, ".") != 0) {
2030 err = stop_log_thread(s);
2033 lv = view_open(view->nlines, view->ncols,
2034 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2036 return got_error_from_errno(
2038 err = open_log_view(lv, s->start_id, s->refs,
2039 s->repo, s->head_ref_name, parent_path, 0);
2042 if (view_is_parent_view(view))
2045 view_set_child(view->parent, lv);
2052 err = stop_log_thread(s);
2055 lv = view_open(view->nlines, view->ncols,
2056 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2058 return got_error_from_errno("view_open");
2059 err = get_head_commit_id(&start_id, s->head_ref_name ?
2060 s->head_ref_name : GOT_REF_HEAD, s->repo);
2063 in_repo_path = strdup(s->in_repo_path);
2064 if (in_repo_path == NULL) {
2066 return got_error_from_errno("strdup");
2068 err = open_log_view(lv, start_id, s->refs, s->repo,
2069 s->head_ref_name, in_repo_path, 0);
2082 static const struct got_error *
2083 apply_unveil(const char *repo_path, const char *worktree_path)
2085 const struct got_error *error;
2087 if (repo_path && unveil(repo_path, "r") != 0)
2088 return got_error_from_errno2("unveil", repo_path);
2090 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2091 return got_error_from_errno2("unveil", worktree_path);
2093 if (unveil("/tmp", "rwc") != 0)
2094 return got_error_from_errno2("unveil", "/tmp");
2096 error = got_privsep_unveil_exec_helpers();
2100 if (unveil(NULL, NULL) != 0)
2101 return got_error_from_errno("unveil");
2111 halfdelay(1); /* Do fast refresh while initial view is loading. */
2114 intrflush(stdscr, FALSE);
2115 keypad(stdscr, TRUE);
2117 signal(SIGWINCH, tog_sigwinch);
2120 static const struct got_error *
2121 cmd_log(int argc, char *argv[])
2123 const struct got_error *error;
2124 struct got_repository *repo = NULL;
2125 struct got_worktree *worktree = NULL;
2126 struct got_reflist_head refs;
2127 struct got_object_id *start_id = NULL;
2128 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2129 char *start_commit = NULL;
2131 struct tog_view *view;
2133 SIMPLEQ_INIT(&refs);
2136 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2141 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2144 start_commit = optarg;
2147 repo_path = realpath(optarg, NULL);
2148 if (repo_path == NULL)
2149 err(1, "-r option");
2160 cwd = getcwd(NULL, 0);
2162 error = got_error_from_errno("getcwd");
2165 error = got_worktree_open(&worktree, cwd);
2166 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2173 error = got_error_from_errno("strdup");
2176 } else if (argc == 1) {
2178 error = got_worktree_resolve_path(&path, worktree,
2183 path = strdup(argv[0]);
2185 error = got_error_from_errno("strdup");
2192 repo_path = worktree ?
2193 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2194 if (repo_path == NULL) {
2195 error = got_error_from_errno("strdup");
2201 error = got_repo_open(&repo, repo_path);
2205 error = apply_unveil(got_repo_get_path(repo),
2206 worktree ? got_worktree_get_root_path(worktree) : NULL);
2210 if (start_commit == NULL)
2211 error = get_head_commit_id(&start_id, worktree ?
2212 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2215 error = got_object_resolve_id_str(&start_id, repo,
2220 error = got_ref_list(&refs, repo);
2224 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2226 error = got_error_from_errno("view_open");
2229 error = open_log_view(view, start_id, &refs, repo, worktree ?
2230 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2233 error = view_loop(view);
2240 got_repo_close(repo);
2242 got_worktree_close(worktree);
2243 got_ref_list_free(&refs);
2251 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2257 parse_next_line(FILE *f, size_t *len)
2262 const char delim[3] = { '\0', '\0', '\0'};
2264 line = fparseln(f, &linelen, &lineno, delim, 0);
2270 static const struct got_error *
2271 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2272 int *last_displayed_line, int *eof, int max_lines,
2275 const struct got_error *err;
2276 int nlines = 0, nprinted = 0;
2283 werase(view->window);
2286 err = format_line(&wline, &width, header, view->ncols);
2291 if (view_needs_focus_indication(view))
2292 wstandout(view->window);
2293 waddwstr(view->window, wline);
2294 if (view_needs_focus_indication(view))
2295 wstandend(view->window);
2296 if (width < view->ncols - 1)
2297 waddch(view->window, '\n');
2305 while (nprinted < max_lines) {
2306 line = parse_next_line(f, &len);
2311 if (++nlines < *first_displayed_line) {
2316 err = format_line(&wline, &width, line, view->ncols);
2321 waddwstr(view->window, wline);
2322 if (width < view->ncols - 1)
2323 waddch(view->window, '\n');
2324 if (++nprinted == 1)
2325 *first_displayed_line = nlines;
2330 *last_displayed_line = nlines;
2335 while (nprinted < view->nlines) {
2336 waddch(view->window, '\n');
2340 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2345 wstandout(view->window);
2346 waddwstr(view->window, wline);
2347 wstandend(view->window);
2354 get_datestr(time_t *time, char *datebuf)
2356 char *p, *s = ctime_r(time, datebuf);
2357 p = strchr(s, '\n');
2363 static const struct got_error *
2364 write_commit_info(struct got_object_id *commit_id,
2365 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2367 const struct got_error *err = NULL;
2369 struct got_commit_object *commit;
2370 char *id_str = NULL;
2371 time_t committer_time;
2372 const char *author, *committer;
2373 char *refs_str = NULL;
2376 err = build_refs_str(&refs_str, refs, commit_id);
2381 err = got_object_open_as_commit(&commit, repo, commit_id);
2385 err = got_object_id_str(&id_str, commit_id);
2387 err = got_error_from_errno("got_object_id_str");
2391 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2392 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2393 err = got_error_from_errno("fprintf");
2396 if (fprintf(outfile, "from: %s\n",
2397 got_object_commit_get_author(commit)) < 0) {
2398 err = got_error_from_errno("fprintf");
2401 committer_time = got_object_commit_get_committer_time(commit);
2402 if (fprintf(outfile, "date: %s UTC\n",
2403 get_datestr(&committer_time, datebuf)) < 0) {
2404 err = got_error_from_errno("fprintf");
2407 author = got_object_commit_get_author(commit);
2408 committer = got_object_commit_get_committer(commit);
2409 if (strcmp(author, committer) != 0 &&
2410 fprintf(outfile, "via: %s\n", committer) < 0) {
2411 err = got_error_from_errno("fprintf");
2414 if (fprintf(outfile, "%s\n",
2415 got_object_commit_get_logmsg(commit)) < 0) {
2416 err = got_error_from_errno("fprintf");
2422 got_object_commit_close(commit);
2426 static const struct got_error *
2427 create_diff(struct tog_diff_view_state *s)
2429 const struct got_error *err = NULL;
2435 err = got_error_from_errno("got_opentemp");
2438 if (s->f && fclose(s->f) != 0) {
2439 err = got_error_from_errno("fclose");
2445 err = got_object_get_type(&obj_type, s->repo, s->id1);
2447 err = got_object_get_type(&obj_type, s->repo, s->id2);
2452 case GOT_OBJ_TYPE_BLOB:
2453 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2454 s->diff_context, s->repo, f);
2456 case GOT_OBJ_TYPE_TREE:
2457 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2458 s->diff_context, s->repo, f);
2460 case GOT_OBJ_TYPE_COMMIT: {
2461 const struct got_object_id_queue *parent_ids;
2462 struct got_object_qid *pid;
2463 struct got_commit_object *commit2;
2465 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2468 /* Show commit info if we're diffing to a parent/root commit. */
2470 write_commit_info(s->id2, s->refs, s->repo, f);
2472 parent_ids = got_object_commit_get_parent_ids(commit2);
2473 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2474 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2475 write_commit_info(s->id2, s->refs,
2481 got_object_commit_close(commit2);
2483 err = got_diff_objects_as_commits(s->id1, s->id2,
2484 s->diff_context, s->repo, f);
2488 err = got_error(GOT_ERR_OBJ_TYPE);
2492 if (f && fflush(f) != 0 && err == NULL)
2493 err = got_error_from_errno("fflush");
2498 diff_view_indicate_progress(struct tog_view *view)
2500 mvwaddstr(view->window, 0, 0, "diffing...");
2505 static const struct got_error *
2506 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2507 struct got_object_id *id2, struct tog_view *log_view,
2508 struct got_reflist_head *refs, struct got_repository *repo)
2510 const struct got_error *err;
2512 if (id1 != NULL && id2 != NULL) {
2514 err = got_object_get_type(&type1, repo, id1);
2517 err = got_object_get_type(&type2, repo, id2);
2522 return got_error(GOT_ERR_OBJ_TYPE);
2526 view->state.diff.id1 = got_object_id_dup(id1);
2527 if (view->state.diff.id1 == NULL)
2528 return got_error_from_errno("got_object_id_dup");
2530 view->state.diff.id1 = NULL;
2532 view->state.diff.id2 = got_object_id_dup(id2);
2533 if (view->state.diff.id2 == NULL) {
2534 free(view->state.diff.id1);
2535 view->state.diff.id1 = NULL;
2536 return got_error_from_errno("got_object_id_dup");
2538 view->state.diff.f = NULL;
2539 view->state.diff.first_displayed_line = 1;
2540 view->state.diff.last_displayed_line = view->nlines;
2541 view->state.diff.diff_context = 3;
2542 view->state.diff.log_view = log_view;
2543 view->state.diff.repo = repo;
2544 view->state.diff.refs = refs;
2546 if (log_view && view_is_splitscreen(view))
2547 show_log_view(log_view); /* draw vborder */
2548 diff_view_indicate_progress(view);
2550 err = create_diff(&view->state.diff);
2552 free(view->state.diff.id1);
2553 view->state.diff.id1 = NULL;
2554 free(view->state.diff.id2);
2555 view->state.diff.id2 = NULL;
2559 view->show = show_diff_view;
2560 view->input = input_diff_view;
2561 view->close = close_diff_view;
2566 static const struct got_error *
2567 close_diff_view(struct tog_view *view)
2569 const struct got_error *err = NULL;
2571 free(view->state.diff.id1);
2572 view->state.diff.id1 = NULL;
2573 free(view->state.diff.id2);
2574 view->state.diff.id2 = NULL;
2575 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2576 err = got_error_from_errno("fclose");
2580 static const struct got_error *
2581 show_diff_view(struct tog_view *view)
2583 const struct got_error *err;
2584 struct tog_diff_view_state *s = &view->state.diff;
2585 char *id_str1 = NULL, *id_str2, *header;
2588 err = got_object_id_str(&id_str1, s->id1);
2592 err = got_object_id_str(&id_str2, s->id2);
2596 if (asprintf(&header, "diff %s %s",
2597 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2598 err = got_error_from_errno("asprintf");
2606 return draw_file(view, s->f, &s->first_displayed_line,
2607 &s->last_displayed_line, &s->eof, view->nlines,
2611 static const struct got_error *
2612 set_selected_commit(struct tog_diff_view_state *s,
2613 struct commit_queue_entry *entry)
2615 const struct got_error *err;
2616 const struct got_object_id_queue *parent_ids;
2617 struct got_commit_object *selected_commit;
2618 struct got_object_qid *pid;
2621 s->id2 = got_object_id_dup(entry->id);
2623 return got_error_from_errno("got_object_id_dup");
2625 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2628 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2630 pid = SIMPLEQ_FIRST(parent_ids);
2631 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2632 got_object_commit_close(selected_commit);
2636 static const struct got_error *
2637 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2638 struct tog_view **focus_view, struct tog_view *view, int ch)
2640 const struct got_error *err = NULL;
2641 struct tog_diff_view_state *s = &view->state.diff;
2642 struct tog_log_view_state *ls;
2643 struct commit_queue_entry *entry;
2649 if (s->first_displayed_line > 1)
2650 s->first_displayed_line--;
2654 if (s->first_displayed_line == 1)
2657 while (i++ < view->nlines - 1 &&
2658 s->first_displayed_line > 1)
2659 s->first_displayed_line--;
2664 s->first_displayed_line++;
2672 while (!s->eof && i++ < view->nlines - 1) {
2674 line = parse_next_line(s->f, NULL);
2675 s->first_displayed_line++;
2681 if (s->diff_context > 0) {
2683 diff_view_indicate_progress(view);
2684 err = create_diff(s);
2688 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2690 diff_view_indicate_progress(view);
2691 err = create_diff(s);
2696 if (s->log_view == NULL)
2698 ls = &s->log_view->state.log;
2699 entry = TAILQ_PREV(ls->selected_entry,
2700 commit_queue_head, entry);
2704 err = input_log_view(NULL, NULL, NULL, s->log_view,
2709 err = set_selected_commit(s, entry);
2713 s->first_displayed_line = 1;
2714 s->last_displayed_line = view->nlines;
2716 diff_view_indicate_progress(view);
2717 err = create_diff(s);
2721 if (s->log_view == NULL)
2723 ls = &s->log_view->state.log;
2725 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2726 ls->thread_args.commits_needed++;
2728 /* Display "loading..." in log view. */
2729 show_log_view(s->log_view);
2733 err = trigger_log_thread(1 /* load_all */,
2734 &ls->thread_args.commits_needed,
2735 &ls->thread_args.log_complete,
2736 &ls->thread_args.need_commits);
2740 err = input_log_view(NULL, NULL, NULL, s->log_view,
2745 entry = TAILQ_NEXT(ls->selected_entry, entry);
2749 err = set_selected_commit(s, entry);
2753 s->first_displayed_line = 1;
2754 s->last_displayed_line = view->nlines;
2756 diff_view_indicate_progress(view);
2757 err = create_diff(s);
2766 static const struct got_error *
2767 cmd_diff(int argc, char *argv[])
2769 const struct got_error *error = NULL;
2770 struct got_repository *repo = NULL;
2771 struct got_reflist_head refs;
2772 struct got_object_id *id1 = NULL, *id2 = NULL;
2773 char *repo_path = NULL;
2774 char *id_str1 = NULL, *id_str2 = NULL;
2776 struct tog_view *view;
2778 SIMPLEQ_INIT(&refs);
2781 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2786 while ((ch = getopt(argc, argv, "")) != -1) {
2798 usage_diff(); /* TODO show local worktree changes */
2799 } else if (argc == 2) {
2800 repo_path = getcwd(NULL, 0);
2801 if (repo_path == NULL)
2802 return got_error_from_errno("getcwd");
2805 } else if (argc == 3) {
2806 repo_path = realpath(argv[0], NULL);
2807 if (repo_path == NULL)
2808 return got_error_from_errno2("realpath", argv[0]);
2816 error = got_repo_open(&repo, repo_path);
2820 error = apply_unveil(got_repo_get_path(repo), NULL);
2824 error = got_object_resolve_id_str(&id1, repo, id_str1);
2828 error = got_object_resolve_id_str(&id2, repo, id_str2);
2832 error = got_ref_list(&refs, repo);
2836 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2838 error = got_error_from_errno("view_open");
2841 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2844 error = view_loop(view);
2847 got_repo_close(repo);
2848 got_ref_list_free(&refs);
2856 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2861 struct tog_blame_line {
2863 struct got_object_id *id;
2866 static const struct got_error *
2867 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2868 const char *path, struct tog_blame_line *lines, int nlines,
2869 int blame_complete, int selected_line, int *first_displayed_line,
2870 int *last_displayed_line, int *eof, int max_lines)
2872 const struct got_error *err;
2873 int lineno = 0, nprinted = 0;
2878 struct tog_blame_line *blame_line;
2879 struct got_object_id *prev_id = NULL;
2882 err = got_object_id_str(&id_str, id);
2887 werase(view->window);
2889 if (asprintf(&line, "commit %s", id_str) == -1) {
2890 err = got_error_from_errno("asprintf");
2895 err = format_line(&wline, &width, line, view->ncols);
2898 if (view_needs_focus_indication(view))
2899 wstandout(view->window);
2900 waddwstr(view->window, wline);
2901 if (view_needs_focus_indication(view))
2902 wstandend(view->window);
2905 if (width < view->ncols - 1)
2906 waddch(view->window, '\n');
2908 if (asprintf(&line, "[%d/%d] %s%s",
2909 *first_displayed_line - 1 + selected_line, nlines,
2910 blame_complete ? "" : "annotating... ", path) == -1) {
2912 return got_error_from_errno("asprintf");
2915 err = format_line(&wline, &width, line, view->ncols);
2920 waddwstr(view->window, wline);
2923 if (width < view->ncols - 1)
2924 waddch(view->window, '\n');
2927 while (nprinted < max_lines - 2) {
2928 line = parse_next_line(f, &len);
2933 if (++lineno < *first_displayed_line) {
2938 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2939 err = format_line(&wline, &width, line, wlimit);
2945 if (view->focussed && nprinted == selected_line - 1)
2946 wstandout(view->window);
2948 blame_line = &lines[lineno - 1];
2949 if (blame_line->annotated && prev_id &&
2950 got_object_id_cmp(prev_id, blame_line->id) == 0)
2951 waddstr(view->window, " ");
2952 else if (blame_line->annotated) {
2954 err = got_object_id_str(&id_str, blame_line->id);
2960 wprintw(view->window, "%.8s ", id_str);
2962 prev_id = blame_line->id;
2964 waddstr(view->window, "........ ");
2968 waddwstr(view->window, wline);
2969 while (width < wlimit) {
2970 waddch(view->window, ' ');
2973 if (view->focussed && nprinted == selected_line - 1)
2974 wstandend(view->window);
2975 if (++nprinted == 1)
2976 *first_displayed_line = lineno;
2981 *last_displayed_line = lineno;
2988 static const struct got_error *
2989 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2991 const struct got_error *err = NULL;
2992 struct tog_blame_cb_args *a = arg;
2993 struct tog_blame_line *line;
2996 if (nlines != a->nlines ||
2997 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2998 return got_error(GOT_ERR_RANGE);
3000 errcode = pthread_mutex_lock(&tog_mutex);
3002 return got_error_set_errno(errcode, "pthread_mutex_lock");
3004 if (*a->quit) { /* user has quit the blame view */
3005 err = got_error(GOT_ERR_ITER_COMPLETED);
3010 goto done; /* no change in this commit */
3012 line = &a->lines[lineno - 1];
3013 if (line->annotated)
3016 line->id = got_object_id_dup(id);
3017 if (line->id == NULL) {
3018 err = got_error_from_errno("got_object_id_dup");
3021 line->annotated = 1;
3023 errcode = pthread_mutex_unlock(&tog_mutex);
3025 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3030 blame_thread(void *arg)
3032 const struct got_error *err;
3033 struct tog_blame_thread_args *ta = arg;
3034 struct tog_blame_cb_args *a = ta->cb_args;
3037 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3038 blame_cb, ta->cb_args);
3040 errcode = pthread_mutex_lock(&tog_mutex);
3042 return (void *)got_error_set_errno(errcode,
3043 "pthread_mutex_lock");
3045 got_repo_close(ta->repo);
3049 errcode = pthread_mutex_unlock(&tog_mutex);
3050 if (errcode && err == NULL)
3051 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3056 static struct got_object_id *
3057 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3060 struct tog_blame_line *line;
3062 line = &lines[first_displayed_line - 1 + selected_line - 1];
3063 if (!line->annotated)
3069 static const struct got_error *
3070 stop_blame(struct tog_blame *blame)
3072 const struct got_error *err = NULL;
3075 if (blame->thread) {
3077 errcode = pthread_mutex_unlock(&tog_mutex);
3079 return got_error_set_errno(errcode,
3080 "pthread_mutex_unlock");
3081 errcode = pthread_join(blame->thread, (void **)&err);
3083 return got_error_set_errno(errcode, "pthread_join");
3084 errcode = pthread_mutex_lock(&tog_mutex);
3086 return got_error_set_errno(errcode,
3087 "pthread_mutex_lock");
3088 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3090 blame->thread = NULL;
3092 if (blame->thread_args.repo) {
3093 got_repo_close(blame->thread_args.repo);
3094 blame->thread_args.repo = NULL;
3097 if (fclose(blame->f) != 0 && err == NULL)
3098 err = got_error_from_errno("fclose");
3102 for (i = 0; i < blame->nlines; i++)
3103 free(blame->lines[i].id);
3105 blame->lines = NULL;
3107 free(blame->cb_args.commit_id);
3108 blame->cb_args.commit_id = NULL;
3113 static const struct got_error *
3114 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3115 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3116 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3117 struct got_repository *repo)
3119 const struct got_error *err = NULL;
3120 struct got_blob_object *blob = NULL;
3121 struct got_repository *thread_repo = NULL;
3122 struct got_object_id *obj_id = NULL;
3125 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3129 return got_error(GOT_ERR_NO_OBJ);
3131 err = got_object_get_type(&obj_type, repo, obj_id);
3135 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3136 err = got_error(GOT_ERR_OBJ_TYPE);
3140 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3143 blame->f = got_opentemp();
3144 if (blame->f == NULL) {
3145 err = got_error_from_errno("got_opentemp");
3148 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3149 &blame->line_offsets, blame->f, blob);
3153 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3154 if (blame->lines == NULL) {
3155 err = got_error_from_errno("calloc");
3159 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3163 blame->cb_args.view = view;
3164 blame->cb_args.lines = blame->lines;
3165 blame->cb_args.nlines = blame->nlines;
3166 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3167 if (blame->cb_args.commit_id == NULL) {
3168 err = got_error_from_errno("got_object_id_dup");
3171 blame->cb_args.quit = done;
3173 blame->thread_args.path = path;
3174 blame->thread_args.repo = thread_repo;
3175 blame->thread_args.cb_args = &blame->cb_args;
3176 blame->thread_args.complete = blame_complete;
3177 *blame_complete = 0;
3181 got_object_blob_close(blob);
3188 static const struct got_error *
3189 open_blame_view(struct tog_view *view, char *path,
3190 struct got_object_id *commit_id, struct got_reflist_head *refs,
3191 struct got_repository *repo)
3193 const struct got_error *err = NULL;
3194 struct tog_blame_view_state *s = &view->state.blame;
3196 SIMPLEQ_INIT(&s->blamed_commits);
3198 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3202 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3203 s->first_displayed_line = 1;
3204 s->last_displayed_line = view->nlines;
3205 s->selected_line = 1;
3206 s->blame_complete = 0;
3208 if (s->path == NULL)
3209 return got_error_from_errno("open_blame_view");
3212 s->commit_id = commit_id;
3213 memset(&s->blame, 0, sizeof(s->blame));
3215 view->show = show_blame_view;
3216 view->input = input_blame_view;
3217 view->close = close_blame_view;
3218 view->search_start = search_start_blame_view;
3219 view->search_next = search_next_blame_view;
3221 return run_blame(&s->blame, view, &s->blame_complete,
3222 &s->first_displayed_line, &s->last_displayed_line,
3223 &s->selected_line, &s->done, &s->eof, s->path,
3224 s->blamed_commit->id, s->repo);
3227 static const struct got_error *
3228 close_blame_view(struct tog_view *view)
3230 const struct got_error *err = NULL;
3231 struct tog_blame_view_state *s = &view->state.blame;
3233 if (s->blame.thread)
3234 err = stop_blame(&s->blame);
3236 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3237 struct got_object_qid *blamed_commit;
3238 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3239 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3240 got_object_qid_free(blamed_commit);
3248 static const struct got_error *
3249 search_start_blame_view(struct tog_view *view)
3251 struct tog_blame_view_state *s = &view->state.blame;
3253 s->matched_line = 0;
3258 match_line(const char *line, regex_t *regex)
3260 regmatch_t regmatch;
3262 return regexec(regex, line, 1, ®match, 0) == 0;
3266 static const struct got_error *
3267 search_next_blame_view(struct tog_view *view)
3269 struct tog_blame_view_state *s = &view->state.blame;
3272 if (!view->searching) {
3273 view->search_next_done = 1;
3277 if (s->matched_line) {
3278 if (view->searching == TOG_SEARCH_FORWARD)
3279 lineno = s->matched_line + 1;
3281 lineno = s->matched_line - 1;
3283 if (view->searching == TOG_SEARCH_FORWARD)
3286 lineno = s->blame.nlines;
3294 if (lineno <= 0 || lineno > s->blame.nlines) {
3295 if (s->matched_line == 0) {
3296 view->search_next_done = 1;
3301 if (view->searching == TOG_SEARCH_FORWARD)
3304 lineno = s->blame.nlines;
3307 offset = s->blame.line_offsets[lineno - 1];
3308 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3310 return got_error_from_errno("fseeko");
3313 line = parse_next_line(s->blame.f, &len);
3314 if (line && match_line(line, &view->regex)) {
3315 view->search_next_done = 1;
3316 s->matched_line = lineno;
3321 if (view->searching == TOG_SEARCH_FORWARD)
3327 if (s->matched_line) {
3328 s->first_displayed_line = s->matched_line;
3329 s->selected_line = 1;
3335 static const struct got_error *
3336 show_blame_view(struct tog_view *view)
3338 const struct got_error *err = NULL;
3339 struct tog_blame_view_state *s = &view->state.blame;
3342 if (s->blame.thread == NULL) {
3343 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3344 &s->blame.thread_args);
3346 return got_error_set_errno(errcode, "pthread_create");
3349 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3350 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3351 s->selected_line, &s->first_displayed_line,
3352 &s->last_displayed_line, &s->eof, view->nlines);
3358 static const struct got_error *
3359 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3360 struct tog_view **focus_view, struct tog_view *view, int ch)
3362 const struct got_error *err = NULL, *thread_err = NULL;
3363 struct tog_view *diff_view;
3364 struct tog_blame_view_state *s = &view->state.blame;
3373 if (s->selected_line > 1)
3375 else if (s->selected_line == 1 &&
3376 s->first_displayed_line > 1)
3377 s->first_displayed_line--;
3380 if (s->first_displayed_line == 1) {
3381 s->selected_line = 1;
3384 if (s->first_displayed_line > view->nlines - 2)
3385 s->first_displayed_line -=
3388 s->first_displayed_line = 1;
3392 if (s->selected_line < view->nlines - 2 &&
3393 s->first_displayed_line +
3394 s->selected_line <= s->blame.nlines)
3396 else if (s->last_displayed_line <
3398 s->first_displayed_line++;
3402 struct got_object_id *id = NULL;
3403 id = get_selected_commit_id(s->blame.lines,
3404 s->first_displayed_line, s->selected_line);
3408 struct got_commit_object *commit;
3409 struct got_object_qid *pid;
3410 struct got_object_id *blob_id = NULL;
3412 err = got_object_open_as_commit(&commit,
3416 pid = SIMPLEQ_FIRST(
3417 got_object_commit_get_parent_ids(commit));
3419 got_object_commit_close(commit);
3422 /* Check if path history ends here. */
3423 err = got_object_id_by_path(&blob_id, s->repo,
3426 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3428 got_object_commit_close(commit);
3431 err = got_object_get_type(&obj_type, s->repo,
3434 /* Can't blame non-blob type objects. */
3435 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3436 got_object_commit_close(commit);
3439 err = got_object_qid_alloc(&s->blamed_commit,
3441 got_object_commit_close(commit);
3443 if (got_object_id_cmp(id,
3444 s->blamed_commit->id) == 0)
3446 err = got_object_qid_alloc(&s->blamed_commit,
3452 thread_err = stop_blame(&s->blame);
3456 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3457 s->blamed_commit, entry);
3458 err = run_blame(&s->blame, view, &s->blame_complete,
3459 &s->first_displayed_line, &s->last_displayed_line,
3460 &s->selected_line, &s->done, &s->eof,
3461 s->path, s->blamed_commit->id, s->repo);
3467 struct got_object_qid *first;
3468 first = SIMPLEQ_FIRST(&s->blamed_commits);
3469 if (!got_object_id_cmp(first->id, s->commit_id))
3472 thread_err = stop_blame(&s->blame);
3476 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3477 got_object_qid_free(s->blamed_commit);
3479 SIMPLEQ_FIRST(&s->blamed_commits);
3480 err = run_blame(&s->blame, view, &s->blame_complete,
3481 &s->first_displayed_line, &s->last_displayed_line,
3482 &s->selected_line, &s->done, &s->eof, s->path,
3483 s->blamed_commit->id, s->repo);
3490 struct got_object_id *id = NULL;
3491 struct got_object_qid *pid;
3492 struct got_commit_object *commit = NULL;
3493 id = get_selected_commit_id(s->blame.lines,
3494 s->first_displayed_line, s->selected_line);
3497 err = got_object_open_as_commit(&commit, s->repo, id);
3500 pid = SIMPLEQ_FIRST(
3501 got_object_commit_get_parent_ids(commit));
3502 if (view_is_parent_view(view))
3503 begin_x = view_split_begin_x(view->begin_x);
3504 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3505 if (diff_view == NULL) {
3506 got_object_commit_close(commit);
3507 err = got_error_from_errno("view_open");
3510 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3511 id, NULL, s->refs, s->repo);
3512 got_object_commit_close(commit);
3514 view_close(diff_view);
3517 if (view_is_parent_view(view)) {
3518 err = view_close_child(view);
3521 err = view_set_child(view, diff_view);
3523 view_close(diff_view);
3526 *focus_view = diff_view;
3527 view->child_focussed = 1;
3529 *new_view = diff_view;
3536 if (s->last_displayed_line >= s->blame.nlines &&
3537 s->selected_line >= MIN(s->blame.nlines,
3538 view->nlines - 2)) {
3541 if (s->last_displayed_line >= s->blame.nlines &&
3542 s->selected_line < view->nlines - 2) {
3543 s->selected_line = MIN(s->blame.nlines,
3547 if (s->last_displayed_line + view->nlines - 2
3549 s->first_displayed_line +=
3552 s->first_displayed_line =
3557 if (s->selected_line > view->nlines - 2) {
3558 s->selected_line = MIN(s->blame.nlines,
3565 return thread_err ? thread_err : err;
3568 static const struct got_error *
3569 cmd_blame(int argc, char *argv[])
3571 const struct got_error *error;
3572 struct got_repository *repo = NULL;
3573 struct got_reflist_head refs;
3574 struct got_worktree *worktree = NULL;
3575 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3576 struct got_object_id *commit_id = NULL;
3577 char *commit_id_str = NULL;
3579 struct tog_view *view;
3581 SIMPLEQ_INIT(&refs);
3584 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3589 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3592 commit_id_str = optarg;
3595 repo_path = realpath(optarg, NULL);
3596 if (repo_path == NULL)
3597 err(1, "-r option");
3613 cwd = getcwd(NULL, 0);
3615 error = got_error_from_errno("getcwd");
3618 if (repo_path == NULL) {
3619 error = got_worktree_open(&worktree, cwd);
3620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3626 strdup(got_worktree_get_repo_path(worktree));
3627 if (repo_path == NULL)
3628 error = got_error_from_errno("strdup");
3632 repo_path = strdup(cwd);
3633 if (repo_path == NULL) {
3634 error = got_error_from_errno("strdup");
3642 error = got_repo_open(&repo, repo_path);
3646 error = apply_unveil(got_repo_get_path(repo), NULL);
3651 const char *prefix = got_worktree_get_path_prefix(worktree);
3652 char *p, *worktree_subdir = cwd +
3653 strlen(got_worktree_get_root_path(worktree));
3654 if (asprintf(&p, "%s%s%s%s%s",
3655 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3656 worktree_subdir, worktree_subdir[0] ? "/" : "",
3658 error = got_error_from_errno("asprintf");
3661 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3664 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3669 if (commit_id_str == NULL) {
3670 struct got_reference *head_ref;
3671 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3674 error = got_ref_resolve(&commit_id, repo, head_ref);
3675 got_ref_close(head_ref);
3677 error = got_object_resolve_id_str(&commit_id, repo,
3683 error = got_ref_list(&refs, repo);
3687 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3689 error = got_error_from_errno("view_open");
3692 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3695 error = view_loop(view);
3701 got_worktree_close(worktree);
3703 got_repo_close(repo);
3704 got_ref_list_free(&refs);
3708 static const struct got_error *
3709 draw_tree_entries(struct tog_view *view,
3710 struct got_tree_entry **first_displayed_entry,
3711 struct got_tree_entry **last_displayed_entry,
3712 struct got_tree_entry **selected_entry, int *ndisplayed,
3713 const char *label, int show_ids, const char *parent_path,
3714 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3716 const struct got_error *err = NULL;
3717 struct got_tree_entry *te;
3723 werase(view->window);
3728 err = format_line(&wline, &width, label, view->ncols);
3731 if (view_needs_focus_indication(view))
3732 wstandout(view->window);
3733 waddwstr(view->window, wline);
3734 if (view_needs_focus_indication(view))
3735 wstandend(view->window);
3738 if (width < view->ncols - 1)
3739 waddch(view->window, '\n');
3742 err = format_line(&wline, &width, parent_path, view->ncols);
3745 waddwstr(view->window, wline);
3748 if (width < view->ncols - 1)
3749 waddch(view->window, '\n');
3752 waddch(view->window, '\n');
3756 te = SIMPLEQ_FIRST(&entries->head);
3757 if (*first_displayed_entry == NULL) {
3758 if (selected == 0) {
3760 wstandout(view->window);
3761 *selected_entry = NULL;
3763 waddstr(view->window, " ..\n"); /* parent directory */
3764 if (selected == 0 && view->focussed)
3765 wstandend(view->window);
3772 while (te != *first_displayed_entry)
3773 te = SIMPLEQ_NEXT(te, entry);
3777 char *line = NULL, *id_str = NULL;
3780 err = got_object_id_str(&id_str, te->id);
3782 return got_error_from_errno(
3783 "got_object_id_str");
3785 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3786 te->name, S_ISDIR(te->mode) ? "/" :
3787 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3789 return got_error_from_errno("asprintf");
3792 err = format_line(&wline, &width, line, view->ncols);
3797 if (n == selected) {
3799 wstandout(view->window);
3800 *selected_entry = te;
3802 waddwstr(view->window, wline);
3803 if (width < view->ncols - 1)
3804 waddch(view->window, '\n');
3805 if (n == selected && view->focussed)
3806 wstandend(view->window);
3812 *last_displayed_entry = te;
3815 te = SIMPLEQ_NEXT(te, entry);
3822 tree_scroll_up(struct tog_view *view,
3823 struct got_tree_entry **first_displayed_entry, int maxscroll,
3824 const struct got_tree_entries *entries, int isroot)
3826 struct got_tree_entry *te, *prev;
3829 if (*first_displayed_entry == NULL)
3832 te = SIMPLEQ_FIRST(&entries->head);
3833 if (*first_displayed_entry == te) {
3835 *first_displayed_entry = NULL;
3839 /* XXX this is stupid... switch to TAILQ? */
3840 for (i = 0; i < maxscroll; i++) {
3841 while (te != *first_displayed_entry) {
3843 te = SIMPLEQ_NEXT(te, entry);
3845 *first_displayed_entry = prev;
3846 te = SIMPLEQ_FIRST(&entries->head);
3848 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3849 *first_displayed_entry = NULL;
3853 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3854 struct got_tree_entry *last_displayed_entry,
3855 const struct got_tree_entries *entries)
3857 struct got_tree_entry *next, *last;
3860 if (*first_displayed_entry)
3861 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3863 next = SIMPLEQ_FIRST(&entries->head);
3864 last = last_displayed_entry;
3865 while (next && last && n++ < maxscroll) {
3866 last = SIMPLEQ_NEXT(last, entry);
3868 *first_displayed_entry = next;
3869 next = SIMPLEQ_NEXT(next, entry);
3875 static const struct got_error *
3876 tree_entry_path(char **path, struct tog_parent_trees *parents,
3877 struct got_tree_entry *te)
3879 const struct got_error *err = NULL;
3880 struct tog_parent_tree *pt;
3881 size_t len = 2; /* for leading slash and NUL */
3883 TAILQ_FOREACH(pt, parents, entry)
3884 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3886 len += strlen(te->name);
3888 *path = calloc(1, len);
3890 return got_error_from_errno("calloc");
3893 pt = TAILQ_LAST(parents, tog_parent_trees);
3895 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3896 err = got_error(GOT_ERR_NO_SPACE);
3899 if (strlcat(*path, "/", len) >= len) {
3900 err = got_error(GOT_ERR_NO_SPACE);
3903 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3906 if (strlcat(*path, te->name, len) >= len) {
3907 err = got_error(GOT_ERR_NO_SPACE);
3919 static const struct got_error *
3920 blame_tree_entry(struct tog_view **new_view, int begin_x,
3921 struct got_tree_entry *te, struct tog_parent_trees *parents,
3922 struct got_object_id *commit_id, struct got_reflist_head *refs,
3923 struct got_repository *repo)
3925 const struct got_error *err = NULL;
3927 struct tog_view *blame_view;
3929 err = tree_entry_path(&path, parents, te);
3933 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3934 if (blame_view == NULL)
3935 return got_error_from_errno("view_open");
3937 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3939 view_close(blame_view);
3942 *new_view = blame_view;
3946 static const struct got_error *
3947 log_tree_entry(struct tog_view **new_view, int begin_x,
3948 struct got_tree_entry *te, struct tog_parent_trees *parents,
3949 struct got_object_id *commit_id, struct got_reflist_head *refs,
3950 struct got_repository *repo)
3952 struct tog_view *log_view;
3953 const struct got_error *err = NULL;
3956 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3957 if (log_view == NULL)
3958 return got_error_from_errno("view_open");
3960 err = tree_entry_path(&path, parents, te);
3964 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
3966 view_close(log_view);
3968 *new_view = log_view;
3973 static const struct got_error *
3974 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3975 struct got_object_id *commit_id, struct got_reflist_head *refs,
3976 struct got_repository *repo)
3978 const struct got_error *err = NULL;
3979 char *commit_id_str = NULL;
3980 struct tog_tree_view_state *s = &view->state.tree;
3982 TAILQ_INIT(&s->parents);
3984 err = got_object_id_str(&commit_id_str, commit_id);
3988 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3989 err = got_error_from_errno("asprintf");
3993 s->root = s->tree = root;
3994 s->entries = got_object_tree_get_entries(root);
3995 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3996 s->commit_id = got_object_id_dup(commit_id);
3997 if (s->commit_id == NULL) {
3998 err = got_error_from_errno("got_object_id_dup");
4004 view->show = show_tree_view;
4005 view->input = input_tree_view;
4006 view->close = close_tree_view;
4007 view->search_start = search_start_tree_view;
4008 view->search_next = search_next_tree_view;
4010 free(commit_id_str);
4012 free(s->tree_label);
4013 s->tree_label = NULL;
4018 static const struct got_error *
4019 close_tree_view(struct tog_view *view)
4021 struct tog_tree_view_state *s = &view->state.tree;
4023 free(s->tree_label);
4024 s->tree_label = NULL;
4026 s->commit_id = NULL;
4027 while (!TAILQ_EMPTY(&s->parents)) {
4028 struct tog_parent_tree *parent;
4029 parent = TAILQ_FIRST(&s->parents);
4030 TAILQ_REMOVE(&s->parents, parent, entry);
4034 if (s->tree != s->root)
4035 got_object_tree_close(s->tree);
4036 got_object_tree_close(s->root);
4041 static const struct got_error *
4042 search_start_tree_view(struct tog_view *view)
4044 struct tog_tree_view_state *s = &view->state.tree;
4046 s->matched_entry = NULL;
4051 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4053 regmatch_t regmatch;
4055 return regexec(regex, te->name, 1, ®match, 0) == 0;
4058 static const struct got_error *
4059 search_next_tree_view(struct tog_view *view)
4061 struct tog_tree_view_state *s = &view->state.tree;
4062 struct got_tree_entry *entry, *te;
4064 if (!view->searching) {
4065 view->search_next_done = 1;
4069 if (s->matched_entry) {
4070 if (view->searching == TOG_SEARCH_FORWARD) {
4071 if (s->selected_entry)
4072 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4074 entry = SIMPLEQ_FIRST(&s->entries->head);
4077 if (s->selected_entry == NULL) {
4078 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4081 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4083 if (SIMPLEQ_NEXT(te, entry) ==
4090 if (view->searching == TOG_SEARCH_FORWARD)
4091 entry = SIMPLEQ_FIRST(&s->entries->head);
4093 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4099 if (entry == NULL) {
4100 if (s->matched_entry == NULL) {
4101 view->search_next_done = 1;
4104 if (view->searching == TOG_SEARCH_FORWARD)
4105 entry = SIMPLEQ_FIRST(&s->entries->head);
4107 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4112 if (match_tree_entry(entry, &view->regex)) {
4113 view->search_next_done = 1;
4114 s->matched_entry = entry;
4118 if (view->searching == TOG_SEARCH_FORWARD)
4119 entry = SIMPLEQ_NEXT(entry, entry);
4121 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4124 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4125 if (SIMPLEQ_NEXT(te, entry) == entry) {
4134 if (s->matched_entry) {
4135 s->first_displayed_entry = s->matched_entry;
4142 static const struct got_error *
4143 show_tree_view(struct tog_view *view)
4145 const struct got_error *err = NULL;
4146 struct tog_tree_view_state *s = &view->state.tree;
4149 err = tree_entry_path(&parent_path, &s->parents, NULL);
4153 err = draw_tree_entries(view, &s->first_displayed_entry,
4154 &s->last_displayed_entry, &s->selected_entry,
4155 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4156 s->entries, s->selected, view->nlines, s->tree == s->root);
4163 static const struct got_error *
4164 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4165 struct tog_view **focus_view, struct tog_view *view, int ch)
4167 const struct got_error *err = NULL;
4168 struct tog_tree_view_state *s = &view->state.tree;
4169 struct tog_view *log_view;
4170 int begin_x = 0, nscrolled;
4174 s->show_ids = !s->show_ids;
4177 if (!s->selected_entry)
4179 if (view_is_parent_view(view))
4180 begin_x = view_split_begin_x(view->begin_x);
4181 err = log_tree_entry(&log_view, begin_x,
4182 s->selected_entry, &s->parents,
4183 s->commit_id, s->refs, s->repo);
4184 if (view_is_parent_view(view)) {
4185 err = view_close_child(view);
4188 err = view_set_child(view, log_view);
4190 view_close(log_view);
4193 *focus_view = log_view;
4194 view->child_focussed = 1;
4196 *new_view = log_view;
4200 if (s->selected > 0) {
4202 if (s->selected == 0)
4205 if (s->selected > 0)
4207 tree_scroll_up(view, &s->first_displayed_entry, 1,
4208 s->entries, s->tree == s->root);
4211 tree_scroll_up(view, &s->first_displayed_entry,
4212 MAX(0, view->nlines - 4 - s->selected), s->entries,
4213 s->tree == s->root);
4215 if (SIMPLEQ_FIRST(&s->entries->head) ==
4216 s->first_displayed_entry && s->tree != s->root)
4217 s->first_displayed_entry = NULL;
4221 if (s->selected < s->ndisplayed - 1) {
4225 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4226 /* can't scroll any further */
4228 tree_scroll_down(&s->first_displayed_entry, 1,
4229 s->last_displayed_entry, s->entries);
4232 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4234 /* can't scroll any further; move cursor down */
4235 if (s->selected < s->ndisplayed - 1)
4236 s->selected = s->ndisplayed - 1;
4239 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4240 view->nlines, s->last_displayed_entry, s->entries);
4241 if (nscrolled < view->nlines) {
4243 struct got_tree_entry *te;
4244 te = s->first_displayed_entry;
4247 te = SIMPLEQ_NEXT(te, entry);
4249 s->selected = ndisplayed - 1;
4255 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4256 struct tog_parent_tree *parent;
4257 /* user selected '..' */
4258 if (s->tree == s->root)
4260 parent = TAILQ_FIRST(&s->parents);
4261 TAILQ_REMOVE(&s->parents, parent,
4263 got_object_tree_close(s->tree);
4264 s->tree = parent->tree;
4266 got_object_tree_get_entries(s->tree);
4267 s->first_displayed_entry =
4268 parent->first_displayed_entry;
4270 parent->selected_entry;
4271 s->selected = parent->selected;
4273 } else if (S_ISDIR(s->selected_entry->mode)) {
4274 struct got_tree_object *subtree;
4275 err = got_object_open_as_tree(&subtree,
4276 s->repo, s->selected_entry->id);
4279 err = tree_view_visit_subtree(subtree, s);
4281 got_object_tree_close(subtree);
4284 } else if (S_ISREG(s->selected_entry->mode)) {
4285 struct tog_view *blame_view;
4286 int begin_x = view_is_parent_view(view) ?
4287 view_split_begin_x(view->begin_x) : 0;
4289 err = blame_tree_entry(&blame_view, begin_x,
4290 s->selected_entry, &s->parents,
4291 s->commit_id, s->refs, s->repo);
4294 if (view_is_parent_view(view)) {
4295 err = view_close_child(view);
4298 err = view_set_child(view, blame_view);
4300 view_close(blame_view);
4303 *focus_view = blame_view;
4304 view->child_focussed = 1;
4306 *new_view = blame_view;
4310 if (s->selected > view->nlines)
4311 s->selected = s->ndisplayed - 1;
4324 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4329 static const struct got_error *
4330 cmd_tree(int argc, char *argv[])
4332 const struct got_error *error;
4333 struct got_repository *repo = NULL;
4334 struct got_reflist_head refs;
4335 char *repo_path = NULL;
4336 struct got_object_id *commit_id = NULL;
4337 char *commit_id_arg = NULL;
4338 struct got_commit_object *commit = NULL;
4339 struct got_tree_object *tree = NULL;
4341 struct tog_view *view;
4343 SIMPLEQ_INIT(&refs);
4346 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4351 while ((ch = getopt(argc, argv, "c:")) != -1) {
4354 commit_id_arg = optarg;
4366 struct got_worktree *worktree;
4367 char *cwd = getcwd(NULL, 0);
4369 return got_error_from_errno("getcwd");
4370 error = got_worktree_open(&worktree, cwd);
4371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4376 strdup(got_worktree_get_repo_path(worktree));
4377 got_worktree_close(worktree);
4380 if (repo_path == NULL) {
4381 error = got_error_from_errno("strdup");
4384 } else if (argc == 1) {
4385 repo_path = realpath(argv[0], NULL);
4386 if (repo_path == NULL)
4387 return got_error_from_errno2("realpath", argv[0]);
4393 error = got_repo_open(&repo, repo_path);
4397 error = apply_unveil(got_repo_get_path(repo), NULL);
4401 if (commit_id_arg == NULL)
4402 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4404 error = got_object_resolve_id_str(&commit_id, repo,
4409 error = got_object_open_as_commit(&commit, repo, commit_id);
4413 error = got_object_open_as_tree(&tree, repo,
4414 got_object_commit_get_tree_id(commit));
4418 error = got_ref_list(&refs, repo);
4422 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4424 error = got_error_from_errno("view_open");
4427 error = open_tree_view(view, tree, commit_id, &refs, repo);
4430 error = view_loop(view);
4435 got_object_commit_close(commit);
4437 got_object_tree_close(tree);
4439 got_repo_close(repo);
4440 got_ref_list_free(&refs);
4447 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4452 make_argv(const char *arg0, const char *arg1)
4455 int argc = (arg1 == NULL ? 1 : 2);
4457 argv = calloc(argc, sizeof(char *));
4460 argv[0] = strdup(arg0);
4461 if (argv[0] == NULL)
4464 argv[1] = strdup(arg1);
4465 if (argv[1] == NULL)
4473 main(int argc, char *argv[])
4475 const struct got_error *error = NULL;
4476 struct tog_cmd *cmd = NULL;
4478 char **cmd_argv = NULL;
4480 setlocale(LC_CTYPE, "");
4482 while ((ch = getopt(argc, argv, "h")) != -1) {
4501 /* Build an argument vector which runs a default command. */
4502 cmd = &tog_commands[0];
4503 cmd_argv = make_argv(cmd->name, NULL);
4508 /* Did the user specific a command? */
4509 for (i = 0; i < nitems(tog_commands); i++) {
4510 if (strncmp(tog_commands[i].name, argv[0],
4511 strlen(argv[0])) == 0) {
4512 cmd = &tog_commands[i];
4514 tog_commands[i].cmd_usage();
4519 /* Did the user specify a repository? */
4520 char *repo_path = realpath(argv[0], NULL);
4522 struct got_repository *repo;
4523 error = got_repo_open(&repo, repo_path);
4525 got_repo_close(repo);
4527 error = got_error_from_errno2("realpath",
4531 fprintf(stderr, "%s: '%s' is not a "
4532 "known command\n", getprogname(),
4536 fprintf(stderr, "%s: '%s' is neither a known "
4537 "command nor a path to a repository\n",
4538 getprogname(), argv[0]);
4542 cmd = &tog_commands[0];
4543 cmd_argv = make_argv(cmd->name, repo_path);
4549 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4556 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);