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 struct got_repository *repo;
152 struct got_reflist_head *refs;
153 struct got_object_id *start_id;
156 struct tog_log_thread_args thread_args;
157 struct commit_queue_entry *matched_entry;
160 struct tog_blame_cb_args {
161 struct tog_blame_line *lines; /* one per line */
164 struct tog_view *view;
165 struct got_object_id *commit_id;
169 struct tog_blame_thread_args {
171 struct got_repository *repo;
172 struct tog_blame_cb_args *cb_args;
179 struct tog_blame_line *lines;
183 struct tog_blame_thread_args thread_args;
184 struct tog_blame_cb_args cb_args;
188 struct tog_blame_view_state {
189 int first_displayed_line;
190 int last_displayed_line;
195 struct got_object_id_queue blamed_commits;
196 struct got_object_qid *blamed_commit;
198 struct got_repository *repo;
199 struct got_reflist_head *refs;
200 struct got_object_id *commit_id;
201 struct tog_blame blame;
205 struct tog_parent_tree {
206 TAILQ_ENTRY(tog_parent_tree) entry;
207 struct got_tree_object *tree;
208 struct got_tree_entry *first_displayed_entry;
209 struct got_tree_entry *selected_entry;
213 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
215 struct tog_tree_view_state {
217 struct got_tree_object *root;
218 struct got_tree_object *tree;
219 const struct got_tree_entries *entries;
220 struct got_tree_entry *first_displayed_entry;
221 struct got_tree_entry *last_displayed_entry;
222 struct got_tree_entry *selected_entry;
223 int ndisplayed, selected, show_ids;
224 struct tog_parent_trees parents;
225 struct got_object_id *commit_id;
226 struct got_repository *repo;
227 struct got_reflist_head *refs;
228 struct got_tree_entry *matched_entry;
232 * We implement two types of views: parent views and child views.
234 * The 'Tab' key switches between a parent view and its child view.
235 * Child views are shown side-by-side to their parent view, provided
236 * there is enough screen estate.
238 * When a new view is opened from within a parent view, this new view
239 * becomes a child view of the parent view, replacing any existing child.
241 * When a new view is opened from within a child view, this new view
242 * becomes a parent view which will obscure the views below until the
243 * user quits the new parent view by typing 'q'.
245 * This list of views contains parent views only.
246 * Child views are only pointed to by their parent view.
248 TAILQ_HEAD(tog_view_list_head, tog_view);
251 TAILQ_ENTRY(tog_view) entry;
254 int nlines, ncols, begin_y, begin_x;
255 int lines, cols; /* copies of LINES and COLS */
257 struct tog_view *parent;
258 struct tog_view *child;
261 /* type-specific state */
262 enum tog_view_type type;
264 struct tog_diff_view_state diff;
265 struct tog_log_view_state log;
266 struct tog_blame_view_state blame;
267 struct tog_tree_view_state tree;
270 const struct got_error *(*show)(struct tog_view *);
271 const struct got_error *(*input)(struct tog_view **,
272 struct tog_view **, struct tog_view**, struct tog_view *, int);
273 const struct got_error *(*close)(struct tog_view *);
275 const struct got_error *(*search_start)(struct tog_view *);
276 const struct got_error *(*search_next)(struct tog_view *);
278 #define TOG_SEARCH_FORWARD 1
279 #define TOG_SEARCH_BACKWARD 2
280 int search_next_done;
284 static const struct got_error *open_diff_view(struct tog_view *,
285 struct got_object_id *, struct got_object_id *, struct tog_view *,
286 struct got_reflist_head *, struct got_repository *);
287 static const struct got_error *show_diff_view(struct tog_view *);
288 static const struct got_error *input_diff_view(struct tog_view **,
289 struct tog_view **, struct tog_view **, struct tog_view *, int);
290 static const struct got_error* close_diff_view(struct tog_view *);
292 static const struct got_error *open_log_view(struct tog_view *,
293 struct got_object_id *, struct got_reflist_head *,
294 struct got_repository *, const char *, int);
295 static const struct got_error * show_log_view(struct tog_view *);
296 static const struct got_error *input_log_view(struct tog_view **,
297 struct tog_view **, struct tog_view **, struct tog_view *, int);
298 static const struct got_error *close_log_view(struct tog_view *);
299 static const struct got_error *search_start_log_view(struct tog_view *);
300 static const struct got_error *search_next_log_view(struct tog_view *);
302 static const struct got_error *open_blame_view(struct tog_view *, char *,
303 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
304 static const struct got_error *show_blame_view(struct tog_view *);
305 static const struct got_error *input_blame_view(struct tog_view **,
306 struct tog_view **, struct tog_view **, struct tog_view *, int);
307 static const struct got_error *close_blame_view(struct tog_view *);
308 static const struct got_error *search_start_blame_view(struct tog_view *);
309 static const struct got_error *search_next_blame_view(struct tog_view *);
311 static const struct got_error *open_tree_view(struct tog_view *,
312 struct got_tree_object *, struct got_object_id *,
313 struct got_reflist_head *, struct got_repository *);
314 static const struct got_error *show_tree_view(struct tog_view *);
315 static const struct got_error *input_tree_view(struct tog_view **,
316 struct tog_view **, struct tog_view **, struct tog_view *, int);
317 static const struct got_error *close_tree_view(struct tog_view *);
318 static const struct got_error *search_start_tree_view(struct tog_view *);
319 static const struct got_error *search_next_tree_view(struct tog_view *);
321 static volatile sig_atomic_t tog_sigwinch_received;
324 tog_sigwinch(int signo)
326 tog_sigwinch_received = 1;
329 static const struct got_error *
330 view_close(struct tog_view *view)
332 const struct got_error *err = NULL;
335 view_close(view->child);
339 err = view->close(view);
341 del_panel(view->panel);
343 delwin(view->window);
348 static struct tog_view *
349 view_open(int nlines, int ncols, int begin_y, int begin_x,
350 enum tog_view_type type)
352 struct tog_view *view = calloc(1, sizeof(*view));
360 view->nlines = nlines ? nlines : LINES - begin_y;
361 view->ncols = ncols ? ncols : COLS - begin_x;
362 view->begin_y = begin_y;
363 view->begin_x = begin_x;
364 view->window = newwin(nlines, ncols, begin_y, begin_x);
365 if (view->window == NULL) {
369 view->panel = new_panel(view->window);
370 if (view->panel == NULL ||
371 set_panel_userptr(view->panel, view) != OK) {
376 keypad(view->window, TRUE);
381 view_split_begin_x(int begin_x)
383 if (begin_x > 0 || COLS < 120)
385 return (COLS - MAX(COLS / 2, 80));
388 static const struct got_error *view_resize(struct tog_view *);
390 static const struct got_error *
391 view_splitscreen(struct tog_view *view)
393 const struct got_error *err = NULL;
396 view->begin_x = view_split_begin_x(0);
397 view->nlines = LINES;
398 view->ncols = COLS - view->begin_x;
401 err = view_resize(view);
405 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
406 return got_error_from_errno("mvwin");
411 static const struct got_error *
412 view_fullscreen(struct tog_view *view)
414 const struct got_error *err = NULL;
418 view->nlines = LINES;
422 err = view_resize(view);
426 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
427 return got_error_from_errno("mvwin");
433 view_is_parent_view(struct tog_view *view)
435 return view->parent == NULL;
438 static const struct got_error *
439 view_resize(struct tog_view *view)
443 if (view->lines > LINES)
444 nlines = view->nlines - (view->lines - LINES);
446 nlines = view->nlines + (LINES - view->lines);
448 if (view->cols > COLS)
449 ncols = view->ncols - (view->cols - COLS);
451 ncols = view->ncols + (COLS - view->cols);
453 if (wresize(view->window, nlines, ncols) == ERR)
454 return got_error_from_errno("wresize");
455 if (replace_panel(view->panel, view->window) == ERR)
456 return got_error_from_errno("replace_panel");
457 wclear(view->window);
459 view->nlines = nlines;
465 view->child->begin_x = view_split_begin_x(view->begin_x);
466 if (view->child->begin_x == 0) {
467 view_fullscreen(view->child);
468 if (view->child->focussed)
469 show_panel(view->child->panel);
471 show_panel(view->panel);
473 view_splitscreen(view->child);
474 show_panel(view->child->panel);
481 static const struct got_error *
482 view_close_child(struct tog_view *view)
484 const struct got_error *err = NULL;
486 if (view->child == NULL)
489 err = view_close(view->child);
494 static const struct got_error *
495 view_set_child(struct tog_view *view, struct tog_view *child)
497 const struct got_error *err = NULL;
500 child->parent = view;
505 view_is_splitscreen(struct tog_view *view)
507 return view->begin_x > 0;
516 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
517 cols = 80; /* Default */
523 resize_term(lines, cols);
526 static const struct got_error *
527 view_search_start(struct tog_view *view)
529 const struct got_error *err = NULL;
533 if (view->nlines < 1)
536 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
538 wclrtoeol(view->window);
542 ret = wgetnstr(view->window, pattern, sizeof(pattern));
548 if (view->searching) {
549 regfree(&view->regex);
553 if (regcomp(&view->regex, pattern,
554 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
555 err = view->search_start(view);
557 regfree(&view->regex);
560 view->searching = TOG_SEARCH_FORWARD;
561 view->search_next_done = 0;
562 view->search_next(view);
568 static const struct got_error *
569 view_input(struct tog_view **new, struct tog_view **dead,
570 struct tog_view **focus, int *done, struct tog_view *view,
571 struct tog_view_list_head *views)
573 const struct got_error *err = NULL;
581 if (view->searching && !view->search_next_done) {
582 errcode = pthread_mutex_unlock(&tog_mutex);
584 return got_error_set_errno(errcode,
585 "pthread_mutex_unlock");
587 errcode = pthread_mutex_lock(&tog_mutex);
589 return got_error_set_errno(errcode,
590 "pthread_mutex_lock");
591 view->search_next(view);
595 nodelay(stdscr, FALSE);
596 /* Allow threads to make progress while we are waiting for input. */
597 errcode = pthread_mutex_unlock(&tog_mutex);
599 return got_error_set_errno(errcode, "pthread_mutex_unlock");
600 ch = wgetch(view->window);
601 errcode = pthread_mutex_lock(&tog_mutex);
603 return got_error_set_errno(errcode, "pthread_mutex_lock");
604 nodelay(stdscr, TRUE);
606 if (tog_sigwinch_received) {
608 tog_sigwinch_received = 0;
609 TAILQ_FOREACH(v, views, entry) {
610 err = view_resize(v);
613 err = v->input(new, dead, focus, v, KEY_RESIZE);
624 *focus = view->child;
625 view->child_focussed = 1;
626 } else if (view->parent) {
627 *focus = view->parent;
628 view->parent->child_focussed = 0;
632 err = view->input(new, dead, focus, view, ch);
639 if (view_is_parent_view(view)) {
640 if (view->child == NULL)
642 if (view_is_splitscreen(view->child)) {
643 *focus = view->child;
644 view->child_focussed = 1;
645 err = view_fullscreen(view->child);
647 err = view_splitscreen(view->child);
650 err = view->child->input(new, dead, focus,
651 view->child, KEY_RESIZE);
653 if (view_is_splitscreen(view)) {
655 view->parent->child_focussed = 1;
656 err = view_fullscreen(view);
658 err = view_splitscreen(view);
662 err = view->input(new, dead, focus, view,
669 if (view->search_start)
670 view_search_start(view);
672 err = view->input(new, dead, focus, view, ch);
676 if (view->search_next && view->searching) {
677 view->searching = (ch == 'n' ?
678 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
679 view->search_next_done = 0;
680 view->search_next(view);
682 err = view->input(new, dead, focus, view, ch);
685 err = view->input(new, dead, focus, view, ch);
693 view_vborder(struct tog_view *view)
696 struct tog_view *view_above;
699 return view_vborder(view->parent);
701 panel = panel_above(view->panel);
705 view_above = panel_userptr(panel);
706 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
707 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
711 view_needs_focus_indication(struct tog_view *view)
713 if (view_is_parent_view(view)) {
714 if (view->child == NULL || view->child_focussed)
716 if (!view_is_splitscreen(view->child))
718 } else if (!view_is_splitscreen(view))
721 return view->focussed;
724 static const struct got_error *
725 view_loop(struct tog_view *view)
727 const struct got_error *err = NULL;
728 struct tog_view_list_head views;
729 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
730 int fast_refresh = 10;
731 int done = 0, errcode;
733 errcode = pthread_mutex_lock(&tog_mutex);
735 return got_error_set_errno(errcode, "pthread_mutex_lock");
738 TAILQ_INSERT_HEAD(&views, view, entry);
742 err = view->show(view);
747 while (!TAILQ_EMPTY(&views) && !done) {
748 /* Refresh fast during initialization, then become slower. */
749 if (fast_refresh && fast_refresh-- == 0)
750 halfdelay(10); /* switch to once per second */
752 err = view_input(&new_view, &dead_view, &focus_view, &done,
757 struct tog_view *prev = NULL;
759 if (view_is_parent_view(dead_view))
760 prev = TAILQ_PREV(dead_view,
761 tog_view_list_head, entry);
762 else if (view->parent != dead_view)
765 if (dead_view->parent)
766 dead_view->parent->child = NULL;
768 TAILQ_REMOVE(&views, dead_view, entry);
770 err = view_close(dead_view);
771 if (err || dead_view == main_view)
774 if (view == dead_view) {
779 else if (!TAILQ_EMPTY(&views))
780 view = TAILQ_LAST(&views,
785 if (view->child && view->child_focussed)
786 focus_view = view->child;
793 struct tog_view *v, *t;
794 /* Only allow one parent view per type. */
795 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
796 if (v->type != new_view->type)
798 TAILQ_REMOVE(&views, v, entry);
804 TAILQ_INSERT_TAIL(&views, new_view, entry);
806 if (focus_view == NULL)
807 focus_view = new_view;
810 show_panel(focus_view->panel);
813 focus_view->focussed = 1;
816 show_panel(new_view->panel);
817 if (view->child && view_is_splitscreen(view->child))
818 show_panel(view->child->panel);
821 if (focus_view == NULL) {
823 show_panel(view->panel);
824 if (view->child && view_is_splitscreen(view->child))
825 show_panel(view->child->panel);
829 err = view->parent->show(view->parent);
833 err = view->show(view);
837 err = view->child->show(view->child);
846 while (!TAILQ_EMPTY(&views)) {
847 view = TAILQ_FIRST(&views);
848 TAILQ_REMOVE(&views, view, entry);
852 errcode = pthread_mutex_unlock(&tog_mutex);
854 return got_error_set_errno(errcode, "pthread_mutex_unlock");
864 "usage: %s log [-c commit] [-r repository-path] [path]\n",
869 /* Create newly allocated wide-character string equivalent to a byte string. */
870 static const struct got_error *
871 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
874 const struct got_error *err = NULL;
877 *wlen = mbstowcs(NULL, s, 0);
878 if (*wlen == (size_t)-1) {
881 return got_error_from_errno("mbstowcs");
883 /* byte string invalid in current encoding; try to "fix" it */
884 err = got_mbsavis(&vis, &vislen, s);
887 *wlen = mbstowcs(NULL, vis, 0);
888 if (*wlen == (size_t)-1) {
889 err = got_error_from_errno("mbstowcs"); /* give up */
894 *ws = calloc(*wlen + 1, sizeof(*ws));
896 err = got_error_from_errno("calloc");
900 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
901 err = got_error_from_errno("mbstowcs");
912 /* Format a line for display, ensuring that it won't overflow a width limit. */
913 static const struct got_error *
914 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
916 const struct got_error *err = NULL;
918 wchar_t *wline = NULL;
925 err = mbs2ws(&wline, &wlen, line);
930 while (i < wlen && cols < wlimit) {
931 int width = wcwidth(wline[i]);
938 if (cols + width <= wlimit)
943 if (wline[i] == L'\t')
944 cols += TABSIZE - ((cols + 1) % TABSIZE);
948 err = got_error_from_errno("wcwidth");
963 static const struct got_error*
964 build_refs_str(char **refs_str, struct got_reflist_head *refs,
965 struct got_object_id *id)
967 static const struct got_error *err = NULL;
968 struct got_reflist_entry *re;
974 SIMPLEQ_FOREACH(re, refs, entry) {
975 if (got_object_id_cmp(re->id, id) != 0)
977 name = got_ref_get_name(re->ref);
978 if (strcmp(name, GOT_REF_HEAD) == 0)
980 if (strncmp(name, "refs/", 5) == 0)
982 if (strncmp(name, "got/", 4) == 0)
984 if (strncmp(name, "heads/", 6) == 0)
986 if (strncmp(name, "remotes/", 8) == 0)
989 if (asprintf(refs_str, "%s%s%s", s ? s : "",
990 s ? ", " : "", name) == -1) {
991 err = got_error_from_errno("asprintf");
1002 static const struct got_error *
1003 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1005 char *smallerthan, *at;
1007 smallerthan = strchr(author, '<');
1008 if (smallerthan && smallerthan[1] != '\0')
1009 author = smallerthan + 1;
1010 at = strchr(author, '@');
1013 return format_line(wauthor, author_width, author, limit);
1016 static const struct got_error *
1017 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1018 struct got_object_id *id, struct got_reflist_head *refs,
1019 int author_display_cols)
1021 const struct got_error *err = NULL;
1022 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1023 char *logmsg0 = NULL, *logmsg = NULL;
1024 char *author = NULL;
1025 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1026 int author_width, logmsg_width;
1027 char *newline, *line = NULL;
1029 static const size_t date_display_cols = 9;
1030 const int avail = view->ncols;
1032 time_t committer_time;
1034 committer_time = got_object_commit_get_committer_time(commit);
1035 if (localtime_r(&committer_time, &tm) == NULL)
1036 return got_error_from_errno("localtime_r");
1037 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1039 return got_error(GOT_ERR_NO_SPACE);
1041 if (avail < date_display_cols)
1042 limit = MIN(sizeof(datebuf) - 1, avail);
1044 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1045 waddnstr(view->window, datebuf, limit);
1050 author = strdup(got_object_commit_get_author(commit));
1051 if (author == NULL) {
1052 err = got_error_from_errno("strdup");
1055 err = format_author(&wauthor, &author_width, author, avail - col);
1058 waddwstr(view->window, wauthor);
1059 col += author_width;
1060 while (col <= avail && author_width < author_display_cols + 2) {
1061 waddch(view->window, ' ');
1068 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1069 if (logmsg0 == NULL) {
1070 err = got_error_from_errno("strdup");
1074 while (*logmsg == '\n')
1076 newline = strchr(logmsg, '\n');
1079 limit = avail - col;
1080 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1083 waddwstr(view->window, wlogmsg);
1084 col += logmsg_width;
1085 while (col <= avail) {
1086 waddch(view->window, ' ');
1098 static struct commit_queue_entry *
1099 alloc_commit_queue_entry(struct got_commit_object *commit,
1100 struct got_object_id *id)
1102 struct commit_queue_entry *entry;
1104 entry = calloc(1, sizeof(*entry));
1109 entry->commit = commit;
1114 pop_commit(struct commit_queue *commits)
1116 struct commit_queue_entry *entry;
1118 entry = TAILQ_FIRST(&commits->head);
1119 TAILQ_REMOVE(&commits->head, entry, entry);
1120 got_object_commit_close(entry->commit);
1121 commits->ncommits--;
1122 /* Don't free entry->id! It is owned by the commit graph. */
1127 free_commits(struct commit_queue *commits)
1129 while (!TAILQ_EMPTY(&commits->head))
1130 pop_commit(commits);
1133 static const struct got_error *
1134 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1135 int minqueue, struct got_repository *repo, const char *path)
1137 const struct got_error *err = NULL;
1141 * We keep all commits open throughout the lifetime of the log
1142 * view in order to avoid having to re-fetch commits from disk
1143 * while updating the display.
1145 while (nqueued < minqueue) {
1146 struct got_object_id *id;
1147 struct got_commit_object *commit;
1148 struct commit_queue_entry *entry;
1151 err = got_commit_graph_iter_next(&id, graph);
1153 if (err->code != GOT_ERR_ITER_NEED_MORE)
1155 err = got_commit_graph_fetch_commits(graph,
1165 err = got_object_open_as_commit(&commit, repo, id);
1168 entry = alloc_commit_queue_entry(commit, id);
1169 if (entry == NULL) {
1170 err = got_error_from_errno("alloc_commit_queue_entry");
1174 errcode = pthread_mutex_lock(&tog_mutex);
1176 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1180 entry->idx = commits->ncommits;
1181 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1183 commits->ncommits++;
1185 errcode = pthread_mutex_unlock(&tog_mutex);
1186 if (errcode && err == NULL)
1187 err = got_error_set_errno(errcode,
1188 "pthread_mutex_unlock");
1194 static const struct got_error *
1195 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1196 struct got_repository *repo)
1198 const struct got_error *err = NULL;
1199 struct got_reference *head_ref;
1203 err = got_ref_open(&head_ref, repo, branch_name, 0);
1207 err = got_ref_resolve(head_id, repo, head_ref);
1208 got_ref_close(head_ref);
1217 static const struct got_error *
1218 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1219 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1220 struct commit_queue *commits, int selected_idx, int limit,
1221 struct got_reflist_head *refs, const char *path, int commits_needed)
1223 const struct got_error *err = NULL;
1224 struct commit_queue_entry *entry;
1226 int ncommits, author_cols = 10;
1227 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1228 char *refs_str = NULL;
1234 if (ncommits == selected_idx) {
1238 entry = TAILQ_NEXT(entry, entry);
1242 if (*selected && !(view->searching && view->search_next_done == 0)) {
1243 err = got_object_id_str(&id_str, (*selected)->id);
1247 err = build_refs_str(&refs_str, refs, (*selected)->id);
1253 if (commits_needed == 0)
1254 halfdelay(10); /* disable fast refresh */
1256 if (asprintf(&ncommits_str, " [%d/%d] %s",
1257 entry ? entry->idx + 1 : 0, commits->ncommits,
1258 commits_needed > 0 ?
1259 (view->searching && view->search_next_done == 0
1260 ? "searching..." : "loading... ") :
1261 (refs_str ? refs_str : "")) == -1) {
1262 err = got_error_from_errno("asprintf");
1266 if (path && strcmp(path, "/") != 0) {
1267 if (asprintf(&header, "commit %s %s%s",
1268 id_str ? id_str : "........................................",
1269 path, ncommits_str) == -1) {
1270 err = got_error_from_errno("asprintf");
1274 } else if (asprintf(&header, "commit %s%s",
1275 id_str ? id_str : "........................................",
1276 ncommits_str) == -1) {
1277 err = got_error_from_errno("asprintf");
1281 err = format_line(&wline, &width, header, view->ncols);
1285 werase(view->window);
1287 if (view_needs_focus_indication(view))
1288 wstandout(view->window);
1289 waddwstr(view->window, wline);
1290 while (width < view->ncols) {
1291 waddch(view->window, ' ');
1294 if (view_needs_focus_indication(view))
1295 wstandend(view->window);
1300 /* Grow author column size if necessary. */
1307 if (ncommits >= limit - 1)
1309 author = strdup(got_object_commit_get_author(entry->commit));
1310 if (author == NULL) {
1311 err = got_error_from_errno("strdup");
1314 err = format_author(&wauthor, &width, author, COLS);
1315 if (author_cols < width)
1316 author_cols = width;
1319 entry = TAILQ_NEXT(entry, entry);
1326 if (ncommits >= limit - 1)
1328 if (ncommits == selected_idx)
1329 wstandout(view->window);
1330 err = draw_commit(view, entry->commit, entry->id, refs,
1332 if (ncommits == selected_idx)
1333 wstandend(view->window);
1338 entry = TAILQ_NEXT(entry, entry);
1351 scroll_up(struct tog_view *view,
1352 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1353 struct commit_queue *commits)
1355 struct commit_queue_entry *entry;
1358 entry = TAILQ_FIRST(&commits->head);
1359 if (*first_displayed_entry == entry)
1362 entry = *first_displayed_entry;
1363 while (entry && nscrolled < maxscroll) {
1364 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1366 *first_displayed_entry = entry;
1372 static const struct got_error *
1373 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1374 pthread_cond_t *need_commits)
1379 halfdelay(1); /* fast refresh while loading commits */
1381 while (*commits_needed > 0) {
1385 /* Wake the log thread. */
1386 errcode = pthread_cond_signal(need_commits);
1388 return got_error_set_errno(errcode,
1389 "pthread_cond_signal");
1390 errcode = pthread_mutex_unlock(&tog_mutex);
1392 return got_error_set_errno(errcode,
1393 "pthread_mutex_unlock");
1395 errcode = pthread_mutex_lock(&tog_mutex);
1397 return got_error_set_errno(errcode,
1398 "pthread_mutex_lock");
1400 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1402 * Thread is not done yet; lose a key press
1403 * and let the user retry... this way the GUI
1404 * remains interactive while logging deep paths
1405 * with few commits in history.
1414 static const struct got_error *
1415 scroll_down(struct tog_view *view,
1416 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1417 struct commit_queue_entry **last_displayed_entry,
1418 struct commit_queue *commits, int *log_complete, int *commits_needed,
1419 pthread_cond_t *need_commits)
1421 const struct got_error *err = NULL;
1422 struct commit_queue_entry *pentry;
1425 if (*last_displayed_entry == NULL)
1428 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1429 if (pentry == NULL && !*log_complete) {
1431 * Ask the log thread for required amount of commits
1432 * plus some amount of pre-fetching.
1434 (*commits_needed) += maxscroll + 20;
1435 err = trigger_log_thread(0, commits_needed, log_complete,
1442 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1446 *last_displayed_entry = pentry;
1448 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1451 *first_displayed_entry = pentry;
1452 } while (++nscrolled < maxscroll);
1457 static const struct got_error *
1458 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1459 struct got_commit_object *commit, struct got_object_id *commit_id,
1460 struct tog_view *log_view, struct got_reflist_head *refs,
1461 struct got_repository *repo)
1463 const struct got_error *err;
1464 struct got_object_qid *parent_id;
1465 struct tog_view *diff_view;
1467 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1468 if (diff_view == NULL)
1469 return got_error_from_errno("view_open");
1471 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1472 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1473 commit_id, log_view, refs, repo);
1475 *new_view = diff_view;
1479 static const struct got_error *
1480 tree_view_visit_subtree(struct got_tree_object *subtree,
1481 struct tog_tree_view_state *s)
1483 struct tog_parent_tree *parent;
1485 parent = calloc(1, sizeof(*parent));
1487 return got_error_from_errno("calloc");
1489 parent->tree = s->tree;
1490 parent->first_displayed_entry = s->first_displayed_entry;
1491 parent->selected_entry = s->selected_entry;
1492 parent->selected = s->selected;
1493 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1495 s->entries = got_object_tree_get_entries(s->tree);
1497 s->first_displayed_entry = NULL;
1502 static const struct got_error *
1503 browse_commit_tree(struct tog_view **new_view, int begin_x,
1504 struct commit_queue_entry *entry, const char *path,
1505 struct got_reflist_head *refs, struct got_repository *repo)
1507 const struct got_error *err = NULL;
1508 struct got_tree_object *tree;
1509 struct got_tree_entry *te;
1510 struct tog_tree_view_state *s;
1511 struct tog_view *tree_view;
1512 char *slash, *subpath = NULL;
1515 err = got_object_open_as_tree(&tree, repo,
1516 got_object_commit_get_tree_id(entry->commit));
1520 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1521 if (tree_view == NULL)
1522 return got_error_from_errno("view_open");
1524 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1526 got_object_tree_close(tree);
1529 s = &tree_view->state.tree;
1531 *new_view = tree_view;
1533 /* Walk the path and open corresponding tree objects. */
1536 struct got_object_id *tree_id;
1541 /* Ensure the correct subtree entry is selected. */
1542 slash = strchr(p, '/');
1544 slash = strchr(p, '\0');
1545 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1546 if (strncmp(p, te->name, slash - p) == 0) {
1547 s->selected_entry = te;
1552 if (s->selected_entry == NULL) {
1553 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1556 if (s->tree != s->root)
1557 s->selected++; /* skip '..' */
1559 if (!S_ISDIR(s->selected_entry->mode)) {
1560 /* Jump to this file's entry. */
1561 s->first_displayed_entry = s->selected_entry;
1566 slash = strchr(p, '/');
1568 subpath = strndup(path, slash - path);
1570 subpath = strdup(path);
1571 if (subpath == NULL) {
1572 err = got_error_from_errno("strdup");
1576 err = got_object_id_by_path(&tree_id, repo, entry->id,
1581 err = got_object_open_as_tree(&tree, repo, tree_id);
1586 err = tree_view_visit_subtree(tree, s);
1588 got_object_tree_close(tree);
1603 log_thread(void *arg)
1605 const struct got_error *err = NULL;
1607 struct tog_log_thread_args *a = arg;
1610 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1614 while (!done && !err) {
1615 err = queue_commits(a->graph, a->commits, 1, a->repo,
1618 if (err->code != GOT_ERR_ITER_COMPLETED)
1622 } else if (a->commits_needed > 0)
1623 a->commits_needed--;
1625 errcode = pthread_mutex_lock(&tog_mutex);
1627 err = got_error_set_errno(errcode,
1628 "pthread_mutex_lock");
1630 } else if (*a->quit)
1632 else if (*a->first_displayed_entry == NULL) {
1633 *a->first_displayed_entry =
1634 TAILQ_FIRST(&a->commits->head);
1635 *a->selected_entry = *a->first_displayed_entry;
1639 a->commits_needed = 0;
1640 else if (a->commits_needed == 0) {
1641 errcode = pthread_cond_wait(&a->need_commits,
1644 err = got_error_set_errno(errcode,
1645 "pthread_cond_wait");
1648 errcode = pthread_mutex_unlock(&tog_mutex);
1649 if (errcode && err == NULL)
1650 err = got_error_set_errno(errcode,
1651 "pthread_mutex_unlock");
1653 a->log_complete = 1;
1657 static const struct got_error *
1658 stop_log_thread(struct tog_log_view_state *s)
1660 const struct got_error *err = NULL;
1665 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1667 return got_error_set_errno(errcode,
1668 "pthread_cond_signal");
1669 errcode = pthread_mutex_unlock(&tog_mutex);
1671 return got_error_set_errno(errcode,
1672 "pthread_mutex_unlock");
1673 errcode = pthread_join(s->thread, (void **)&err);
1675 return got_error_set_errno(errcode, "pthread_join");
1676 errcode = pthread_mutex_lock(&tog_mutex);
1678 return got_error_set_errno(errcode,
1679 "pthread_mutex_lock");
1683 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1684 if (errcode && err == NULL)
1685 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1687 if (s->thread_args.repo) {
1688 got_repo_close(s->thread_args.repo);
1689 s->thread_args.repo = NULL;
1692 if (s->thread_args.graph) {
1693 got_commit_graph_close(s->thread_args.graph);
1694 s->thread_args.graph = NULL;
1700 static const struct got_error *
1701 close_log_view(struct tog_view *view)
1703 const struct got_error *err = NULL;
1704 struct tog_log_view_state *s = &view->state.log;
1706 err = stop_log_thread(s);
1707 free_commits(&s->commits);
1708 free(s->in_repo_path);
1709 s->in_repo_path = NULL;
1715 static const struct got_error *
1716 search_start_log_view(struct tog_view *view)
1718 struct tog_log_view_state *s = &view->state.log;
1720 s->matched_entry = NULL;
1725 match_commit(struct got_commit_object *commit, regex_t *regex)
1727 regmatch_t regmatch;
1729 if (regexec(regex, got_object_commit_get_author(commit), 1,
1730 ®match, 0) == 0 ||
1731 regexec(regex, got_object_commit_get_committer(commit), 1,
1732 ®match, 0) == 0 ||
1733 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1740 static const struct got_error *
1741 search_next_log_view(struct tog_view *view)
1743 const struct got_error *err = NULL;
1744 struct tog_log_view_state *s = &view->state.log;
1745 struct commit_queue_entry *entry;
1747 if (!view->searching) {
1748 view->search_next_done = 1;
1752 if (s->matched_entry) {
1753 if (view->searching == TOG_SEARCH_FORWARD)
1754 entry = TAILQ_NEXT(s->selected_entry, entry);
1756 entry = TAILQ_PREV(s->selected_entry,
1757 commit_queue_head, entry);
1759 if (view->searching == TOG_SEARCH_FORWARD)
1760 entry = TAILQ_FIRST(&s->commits.head);
1762 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1766 if (entry == NULL) {
1767 if (s->thread_args.log_complete ||
1768 view->searching == TOG_SEARCH_BACKWARD) {
1769 view->search_next_done = 1;
1772 s->thread_args.commits_needed = 1;
1773 return trigger_log_thread(0,
1774 &s->thread_args.commits_needed,
1775 &s->thread_args.log_complete,
1776 &s->thread_args.need_commits);
1779 if (match_commit(entry->commit, &view->regex)) {
1780 view->search_next_done = 1;
1781 s->matched_entry = entry;
1784 if (view->searching == TOG_SEARCH_FORWARD)
1785 entry = TAILQ_NEXT(entry, entry);
1787 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1790 if (s->matched_entry) {
1791 int cur = s->selected_entry->idx;
1792 while (cur < s->matched_entry->idx) {
1793 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1798 while (cur > s->matched_entry->idx) {
1799 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1809 static const struct got_error *
1810 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1811 struct got_reflist_head *refs, struct got_repository *repo,
1812 const char *path, int check_disk)
1814 const struct got_error *err = NULL;
1815 struct tog_log_view_state *s = &view->state.log;
1816 struct got_repository *thread_repo = NULL;
1817 struct got_commit_graph *thread_graph = NULL;
1820 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1824 /* The commit queue only contains commits being displayed. */
1825 TAILQ_INIT(&s->commits.head);
1826 s->commits.ncommits = 0;
1830 s->start_id = got_object_id_dup(start_id);
1831 if (s->start_id == NULL) {
1832 err = got_error_from_errno("got_object_id_dup");
1836 view->show = show_log_view;
1837 view->input = input_log_view;
1838 view->close = close_log_view;
1839 view->search_start = search_start_log_view;
1840 view->search_next = search_next_log_view;
1842 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1845 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1850 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1852 err = got_error_set_errno(errcode, "pthread_cond_init");
1856 s->thread_args.commits_needed = view->nlines;
1857 s->thread_args.graph = thread_graph;
1858 s->thread_args.commits = &s->commits;
1859 s->thread_args.in_repo_path = s->in_repo_path;
1860 s->thread_args.start_id = s->start_id;
1861 s->thread_args.repo = thread_repo;
1862 s->thread_args.log_complete = 0;
1863 s->thread_args.quit = &s->quit;
1864 s->thread_args.view = view;
1865 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1866 s->thread_args.selected_entry = &s->selected_entry;
1869 close_log_view(view);
1873 static const struct got_error *
1874 show_log_view(struct tog_view *view)
1876 struct tog_log_view_state *s = &view->state.log;
1878 if (s->thread == NULL) {
1879 int errcode = pthread_create(&s->thread, NULL, log_thread,
1882 return got_error_set_errno(errcode, "pthread_create");
1885 return draw_commits(view, &s->last_displayed_entry,
1886 &s->selected_entry, s->first_displayed_entry,
1887 &s->commits, s->selected, view->nlines, s->refs,
1888 s->in_repo_path, s->thread_args.commits_needed);
1891 static const struct got_error *
1892 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1893 struct tog_view **focus_view, struct tog_view *view, int ch)
1895 const struct got_error *err = NULL;
1896 struct tog_log_view_state *s = &view->state.log;
1898 struct tog_view *diff_view = NULL, *tree_view = NULL;
1909 if (s->first_displayed_entry == NULL)
1911 if (s->selected > 0)
1914 scroll_up(view, &s->first_displayed_entry, 1,
1919 if (s->first_displayed_entry == NULL)
1921 if (TAILQ_FIRST(&s->commits.head) ==
1922 s->first_displayed_entry) {
1926 scroll_up(view, &s->first_displayed_entry,
1927 view->nlines, &s->commits);
1933 if (s->first_displayed_entry == NULL)
1935 if (s->selected < MIN(view->nlines - 2,
1936 s->commits.ncommits - 1)) {
1940 err = scroll_down(view, &s->first_displayed_entry, 1,
1941 &s->last_displayed_entry, &s->commits,
1942 &s->thread_args.log_complete,
1943 &s->thread_args.commits_needed,
1944 &s->thread_args.need_commits);
1948 struct commit_queue_entry *first;
1949 first = s->first_displayed_entry;
1952 err = scroll_down(view, &s->first_displayed_entry,
1953 view->nlines, &s->last_displayed_entry,
1954 &s->commits, &s->thread_args.log_complete,
1955 &s->thread_args.commits_needed,
1956 &s->thread_args.need_commits);
1957 if (first == s->first_displayed_entry &&
1958 s->selected < MIN(view->nlines - 2,
1959 s->commits.ncommits - 1)) {
1960 /* can't scroll further down */
1961 s->selected = MIN(view->nlines - 2,
1962 s->commits.ncommits - 1);
1968 if (s->selected > view->nlines - 2)
1969 s->selected = view->nlines - 2;
1970 if (s->selected > s->commits.ncommits - 1)
1971 s->selected = s->commits.ncommits - 1;
1976 if (s->selected_entry == NULL)
1978 if (view_is_parent_view(view))
1979 begin_x = view_split_begin_x(view->begin_x);
1980 err = open_diff_view_for_commit(&diff_view, begin_x,
1981 s->selected_entry->commit, s->selected_entry->id,
1982 view, s->refs, s->repo);
1985 if (view_is_parent_view(view)) {
1986 err = view_close_child(view);
1989 err = view_set_child(view, diff_view);
1991 view_close(diff_view);
1994 *focus_view = diff_view;
1995 view->child_focussed = 1;
1997 *new_view = diff_view;
2000 if (s->selected_entry == NULL)
2002 if (view_is_parent_view(view))
2003 begin_x = view_split_begin_x(view->begin_x);
2004 err = browse_commit_tree(&tree_view, begin_x,
2005 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2008 if (view_is_parent_view(view)) {
2009 err = view_close_child(view);
2012 err = view_set_child(view, tree_view);
2014 view_close(tree_view);
2017 *focus_view = tree_view;
2018 view->child_focussed = 1;
2020 *new_view = tree_view;
2023 if (strcmp(s->in_repo_path, "/") == 0)
2025 parent_path = dirname(s->in_repo_path);
2026 if (parent_path && strcmp(parent_path, ".") != 0) {
2027 struct tog_view *lv;
2028 err = stop_log_thread(s);
2031 lv = view_open(view->nlines, view->ncols,
2032 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2034 return got_error_from_errno(
2036 err = open_log_view(lv, s->start_id, s->refs,
2037 s->repo, parent_path, 0);
2040 if (view_is_parent_view(view))
2043 view_set_child(view->parent, lv);
2056 static const struct got_error *
2057 apply_unveil(const char *repo_path, const char *worktree_path)
2059 const struct got_error *error;
2061 if (repo_path && unveil(repo_path, "r") != 0)
2062 return got_error_from_errno2("unveil", repo_path);
2064 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2065 return got_error_from_errno2("unveil", worktree_path);
2067 if (unveil("/tmp", "rwc") != 0)
2068 return got_error_from_errno2("unveil", "/tmp");
2070 error = got_privsep_unveil_exec_helpers();
2074 if (unveil(NULL, NULL) != 0)
2075 return got_error_from_errno("unveil");
2085 halfdelay(1); /* Do fast refresh while initial view is loading. */
2088 intrflush(stdscr, FALSE);
2089 keypad(stdscr, TRUE);
2091 signal(SIGWINCH, tog_sigwinch);
2094 static const struct got_error *
2095 cmd_log(int argc, char *argv[])
2097 const struct got_error *error;
2098 struct got_repository *repo = NULL;
2099 struct got_worktree *worktree = NULL;
2100 struct got_reflist_head refs;
2101 struct got_object_id *start_id = NULL;
2102 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2103 char *start_commit = NULL;
2105 struct tog_view *view;
2107 SIMPLEQ_INIT(&refs);
2110 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2115 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2118 start_commit = optarg;
2121 repo_path = realpath(optarg, NULL);
2122 if (repo_path == NULL)
2123 err(1, "-r option");
2134 cwd = getcwd(NULL, 0);
2136 error = got_error_from_errno("getcwd");
2139 error = got_worktree_open(&worktree, cwd);
2140 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2147 error = got_error_from_errno("strdup");
2150 } else if (argc == 1) {
2152 error = got_worktree_resolve_path(&path, worktree,
2157 path = strdup(argv[0]);
2159 error = got_error_from_errno("strdup");
2166 repo_path = worktree ?
2167 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2168 if (repo_path == NULL) {
2169 error = got_error_from_errno("strdup");
2175 error = got_repo_open(&repo, repo_path);
2179 error = apply_unveil(got_repo_get_path(repo),
2180 worktree ? got_worktree_get_root_path(worktree) : NULL);
2184 if (start_commit == NULL)
2185 error = get_head_commit_id(&start_id, worktree ?
2186 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2189 error = got_object_resolve_id_str(&start_id, repo,
2194 error = got_ref_list(&refs, repo);
2198 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2200 error = got_error_from_errno("view_open");
2203 error = open_log_view(view, start_id, &refs, repo, path, 1);
2206 error = view_loop(view);
2213 got_repo_close(repo);
2215 got_worktree_close(worktree);
2216 got_ref_list_free(&refs);
2224 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2230 parse_next_line(FILE *f, size_t *len)
2235 const char delim[3] = { '\0', '\0', '\0'};
2237 line = fparseln(f, &linelen, &lineno, delim, 0);
2243 static const struct got_error *
2244 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2245 int *last_displayed_line, int *eof, int max_lines,
2248 const struct got_error *err;
2249 int nlines = 0, nprinted = 0;
2256 werase(view->window);
2259 err = format_line(&wline, &width, header, view->ncols);
2264 if (view_needs_focus_indication(view))
2265 wstandout(view->window);
2266 waddwstr(view->window, wline);
2267 if (view_needs_focus_indication(view))
2268 wstandend(view->window);
2269 if (width < view->ncols - 1)
2270 waddch(view->window, '\n');
2278 while (nprinted < max_lines) {
2279 line = parse_next_line(f, &len);
2284 if (++nlines < *first_displayed_line) {
2289 err = format_line(&wline, &width, line, view->ncols);
2294 waddwstr(view->window, wline);
2295 if (width < view->ncols - 1)
2296 waddch(view->window, '\n');
2297 if (++nprinted == 1)
2298 *first_displayed_line = nlines;
2303 *last_displayed_line = nlines;
2308 while (nprinted < view->nlines) {
2309 waddch(view->window, '\n');
2313 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2318 wstandout(view->window);
2319 waddwstr(view->window, wline);
2320 wstandend(view->window);
2327 get_datestr(time_t *time, char *datebuf)
2329 char *p, *s = ctime_r(time, datebuf);
2330 p = strchr(s, '\n');
2336 static const struct got_error *
2337 write_commit_info(struct got_object_id *commit_id,
2338 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2340 const struct got_error *err = NULL;
2342 struct got_commit_object *commit;
2343 char *id_str = NULL;
2344 time_t committer_time;
2345 const char *author, *committer;
2346 char *refs_str = NULL;
2349 err = build_refs_str(&refs_str, refs, commit_id);
2354 err = got_object_open_as_commit(&commit, repo, commit_id);
2358 err = got_object_id_str(&id_str, commit_id);
2360 err = got_error_from_errno("got_object_id_str");
2364 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2365 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2366 err = got_error_from_errno("fprintf");
2369 if (fprintf(outfile, "from: %s\n",
2370 got_object_commit_get_author(commit)) < 0) {
2371 err = got_error_from_errno("fprintf");
2374 committer_time = got_object_commit_get_committer_time(commit);
2375 if (fprintf(outfile, "date: %s UTC\n",
2376 get_datestr(&committer_time, datebuf)) < 0) {
2377 err = got_error_from_errno("fprintf");
2380 author = got_object_commit_get_author(commit);
2381 committer = got_object_commit_get_committer(commit);
2382 if (strcmp(author, committer) != 0 &&
2383 fprintf(outfile, "via: %s\n", committer) < 0) {
2384 err = got_error_from_errno("fprintf");
2387 if (fprintf(outfile, "%s\n",
2388 got_object_commit_get_logmsg(commit)) < 0) {
2389 err = got_error_from_errno("fprintf");
2395 got_object_commit_close(commit);
2399 static const struct got_error *
2400 create_diff(struct tog_diff_view_state *s)
2402 const struct got_error *err = NULL;
2408 err = got_error_from_errno("got_opentemp");
2411 if (s->f && fclose(s->f) != 0) {
2412 err = got_error_from_errno("fclose");
2418 err = got_object_get_type(&obj_type, s->repo, s->id1);
2420 err = got_object_get_type(&obj_type, s->repo, s->id2);
2425 case GOT_OBJ_TYPE_BLOB:
2426 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2427 s->diff_context, s->repo, f);
2429 case GOT_OBJ_TYPE_TREE:
2430 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2431 s->diff_context, s->repo, f);
2433 case GOT_OBJ_TYPE_COMMIT: {
2434 const struct got_object_id_queue *parent_ids;
2435 struct got_object_qid *pid;
2436 struct got_commit_object *commit2;
2438 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2441 /* Show commit info if we're diffing to a parent/root commit. */
2443 write_commit_info(s->id2, s->refs, s->repo, f);
2445 parent_ids = got_object_commit_get_parent_ids(commit2);
2446 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2447 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2448 write_commit_info(s->id2, s->refs,
2454 got_object_commit_close(commit2);
2456 err = got_diff_objects_as_commits(s->id1, s->id2,
2457 s->diff_context, s->repo, f);
2461 err = got_error(GOT_ERR_OBJ_TYPE);
2465 if (f && fflush(f) != 0 && err == NULL)
2466 err = got_error_from_errno("fflush");
2471 diff_view_indicate_progress(struct tog_view *view)
2473 mvwaddstr(view->window, 0, 0, "diffing...");
2478 static const struct got_error *
2479 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2480 struct got_object_id *id2, struct tog_view *log_view,
2481 struct got_reflist_head *refs, struct got_repository *repo)
2483 const struct got_error *err;
2485 if (id1 != NULL && id2 != NULL) {
2487 err = got_object_get_type(&type1, repo, id1);
2490 err = got_object_get_type(&type2, repo, id2);
2495 return got_error(GOT_ERR_OBJ_TYPE);
2499 view->state.diff.id1 = got_object_id_dup(id1);
2500 if (view->state.diff.id1 == NULL)
2501 return got_error_from_errno("got_object_id_dup");
2503 view->state.diff.id1 = NULL;
2505 view->state.diff.id2 = got_object_id_dup(id2);
2506 if (view->state.diff.id2 == NULL) {
2507 free(view->state.diff.id1);
2508 view->state.diff.id1 = NULL;
2509 return got_error_from_errno("got_object_id_dup");
2511 view->state.diff.f = NULL;
2512 view->state.diff.first_displayed_line = 1;
2513 view->state.diff.last_displayed_line = view->nlines;
2514 view->state.diff.diff_context = 3;
2515 view->state.diff.log_view = log_view;
2516 view->state.diff.repo = repo;
2517 view->state.diff.refs = refs;
2519 if (log_view && view_is_splitscreen(view))
2520 show_log_view(log_view); /* draw vborder */
2521 diff_view_indicate_progress(view);
2523 err = create_diff(&view->state.diff);
2525 free(view->state.diff.id1);
2526 view->state.diff.id1 = NULL;
2527 free(view->state.diff.id2);
2528 view->state.diff.id2 = NULL;
2532 view->show = show_diff_view;
2533 view->input = input_diff_view;
2534 view->close = close_diff_view;
2539 static const struct got_error *
2540 close_diff_view(struct tog_view *view)
2542 const struct got_error *err = NULL;
2544 free(view->state.diff.id1);
2545 view->state.diff.id1 = NULL;
2546 free(view->state.diff.id2);
2547 view->state.diff.id2 = NULL;
2548 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2549 err = got_error_from_errno("fclose");
2553 static const struct got_error *
2554 show_diff_view(struct tog_view *view)
2556 const struct got_error *err;
2557 struct tog_diff_view_state *s = &view->state.diff;
2558 char *id_str1 = NULL, *id_str2, *header;
2561 err = got_object_id_str(&id_str1, s->id1);
2565 err = got_object_id_str(&id_str2, s->id2);
2569 if (asprintf(&header, "diff %s %s",
2570 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2571 err = got_error_from_errno("asprintf");
2579 return draw_file(view, s->f, &s->first_displayed_line,
2580 &s->last_displayed_line, &s->eof, view->nlines,
2584 static const struct got_error *
2585 set_selected_commit(struct tog_diff_view_state *s,
2586 struct commit_queue_entry *entry)
2588 const struct got_error *err;
2589 const struct got_object_id_queue *parent_ids;
2590 struct got_commit_object *selected_commit;
2591 struct got_object_qid *pid;
2594 s->id2 = got_object_id_dup(entry->id);
2596 return got_error_from_errno("got_object_id_dup");
2598 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2601 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2603 pid = SIMPLEQ_FIRST(parent_ids);
2604 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2605 got_object_commit_close(selected_commit);
2609 static const struct got_error *
2610 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2611 struct tog_view **focus_view, struct tog_view *view, int ch)
2613 const struct got_error *err = NULL;
2614 struct tog_diff_view_state *s = &view->state.diff;
2615 struct tog_log_view_state *ls;
2616 struct commit_queue_entry *entry;
2622 if (s->first_displayed_line > 1)
2623 s->first_displayed_line--;
2627 if (s->first_displayed_line == 1)
2630 while (i++ < view->nlines - 1 &&
2631 s->first_displayed_line > 1)
2632 s->first_displayed_line--;
2637 s->first_displayed_line++;
2645 while (!s->eof && i++ < view->nlines - 1) {
2647 line = parse_next_line(s->f, NULL);
2648 s->first_displayed_line++;
2654 if (s->diff_context > 0) {
2656 diff_view_indicate_progress(view);
2657 err = create_diff(s);
2661 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2663 diff_view_indicate_progress(view);
2664 err = create_diff(s);
2669 if (s->log_view == NULL)
2671 ls = &s->log_view->state.log;
2672 entry = TAILQ_PREV(ls->selected_entry,
2673 commit_queue_head, entry);
2677 err = input_log_view(NULL, NULL, NULL, s->log_view,
2682 err = set_selected_commit(s, entry);
2686 s->first_displayed_line = 1;
2687 s->last_displayed_line = view->nlines;
2689 diff_view_indicate_progress(view);
2690 err = create_diff(s);
2694 if (s->log_view == NULL)
2696 ls = &s->log_view->state.log;
2698 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2699 ls->thread_args.commits_needed++;
2701 /* Display "loading..." in log view. */
2702 show_log_view(s->log_view);
2706 err = trigger_log_thread(1 /* load_all */,
2707 &ls->thread_args.commits_needed,
2708 &ls->thread_args.log_complete,
2709 &ls->thread_args.need_commits);
2713 err = input_log_view(NULL, NULL, NULL, s->log_view,
2718 entry = TAILQ_NEXT(ls->selected_entry, entry);
2722 err = set_selected_commit(s, entry);
2726 s->first_displayed_line = 1;
2727 s->last_displayed_line = view->nlines;
2729 diff_view_indicate_progress(view);
2730 err = create_diff(s);
2739 static const struct got_error *
2740 cmd_diff(int argc, char *argv[])
2742 const struct got_error *error = NULL;
2743 struct got_repository *repo = NULL;
2744 struct got_reflist_head refs;
2745 struct got_object_id *id1 = NULL, *id2 = NULL;
2746 char *repo_path = NULL;
2747 char *id_str1 = NULL, *id_str2 = NULL;
2749 struct tog_view *view;
2751 SIMPLEQ_INIT(&refs);
2754 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2759 while ((ch = getopt(argc, argv, "")) != -1) {
2771 usage_diff(); /* TODO show local worktree changes */
2772 } else if (argc == 2) {
2773 repo_path = getcwd(NULL, 0);
2774 if (repo_path == NULL)
2775 return got_error_from_errno("getcwd");
2778 } else if (argc == 3) {
2779 repo_path = realpath(argv[0], NULL);
2780 if (repo_path == NULL)
2781 return got_error_from_errno2("realpath", argv[0]);
2789 error = got_repo_open(&repo, repo_path);
2793 error = apply_unveil(got_repo_get_path(repo), NULL);
2797 error = got_object_resolve_id_str(&id1, repo, id_str1);
2801 error = got_object_resolve_id_str(&id2, repo, id_str2);
2805 error = got_ref_list(&refs, repo);
2809 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2811 error = got_error_from_errno("view_open");
2814 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2817 error = view_loop(view);
2820 got_repo_close(repo);
2821 got_ref_list_free(&refs);
2829 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2834 struct tog_blame_line {
2836 struct got_object_id *id;
2839 static const struct got_error *
2840 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2841 const char *path, struct tog_blame_line *lines, int nlines,
2842 int blame_complete, int selected_line, int *first_displayed_line,
2843 int *last_displayed_line, int *eof, int max_lines)
2845 const struct got_error *err;
2846 int lineno = 0, nprinted = 0;
2851 struct tog_blame_line *blame_line;
2852 struct got_object_id *prev_id = NULL;
2855 err = got_object_id_str(&id_str, id);
2860 werase(view->window);
2862 if (asprintf(&line, "commit %s", id_str) == -1) {
2863 err = got_error_from_errno("asprintf");
2868 err = format_line(&wline, &width, line, view->ncols);
2871 if (view_needs_focus_indication(view))
2872 wstandout(view->window);
2873 waddwstr(view->window, wline);
2874 if (view_needs_focus_indication(view))
2875 wstandend(view->window);
2878 if (width < view->ncols - 1)
2879 waddch(view->window, '\n');
2881 if (asprintf(&line, "[%d/%d] %s%s",
2882 *first_displayed_line - 1 + selected_line, nlines,
2883 blame_complete ? "" : "annotating... ", path) == -1) {
2885 return got_error_from_errno("asprintf");
2888 err = format_line(&wline, &width, line, view->ncols);
2893 waddwstr(view->window, wline);
2896 if (width < view->ncols - 1)
2897 waddch(view->window, '\n');
2900 while (nprinted < max_lines - 2) {
2901 line = parse_next_line(f, &len);
2906 if (++lineno < *first_displayed_line) {
2911 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2912 err = format_line(&wline, &width, line, wlimit);
2918 if (view->focussed && nprinted == selected_line - 1)
2919 wstandout(view->window);
2921 blame_line = &lines[lineno - 1];
2922 if (blame_line->annotated && prev_id &&
2923 got_object_id_cmp(prev_id, blame_line->id) == 0)
2924 waddstr(view->window, " ");
2925 else if (blame_line->annotated) {
2927 err = got_object_id_str(&id_str, blame_line->id);
2933 wprintw(view->window, "%.8s ", id_str);
2935 prev_id = blame_line->id;
2937 waddstr(view->window, "........ ");
2941 waddwstr(view->window, wline);
2942 while (width < wlimit) {
2943 waddch(view->window, ' ');
2946 if (view->focussed && nprinted == selected_line - 1)
2947 wstandend(view->window);
2948 if (++nprinted == 1)
2949 *first_displayed_line = lineno;
2954 *last_displayed_line = lineno;
2961 static const struct got_error *
2962 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2964 const struct got_error *err = NULL;
2965 struct tog_blame_cb_args *a = arg;
2966 struct tog_blame_line *line;
2969 if (nlines != a->nlines ||
2970 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2971 return got_error(GOT_ERR_RANGE);
2973 errcode = pthread_mutex_lock(&tog_mutex);
2975 return got_error_set_errno(errcode, "pthread_mutex_lock");
2977 if (*a->quit) { /* user has quit the blame view */
2978 err = got_error(GOT_ERR_ITER_COMPLETED);
2983 goto done; /* no change in this commit */
2985 line = &a->lines[lineno - 1];
2986 if (line->annotated)
2989 line->id = got_object_id_dup(id);
2990 if (line->id == NULL) {
2991 err = got_error_from_errno("got_object_id_dup");
2994 line->annotated = 1;
2996 errcode = pthread_mutex_unlock(&tog_mutex);
2998 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3003 blame_thread(void *arg)
3005 const struct got_error *err;
3006 struct tog_blame_thread_args *ta = arg;
3007 struct tog_blame_cb_args *a = ta->cb_args;
3010 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3011 blame_cb, ta->cb_args);
3013 errcode = pthread_mutex_lock(&tog_mutex);
3015 return (void *)got_error_set_errno(errcode,
3016 "pthread_mutex_lock");
3018 got_repo_close(ta->repo);
3022 errcode = pthread_mutex_unlock(&tog_mutex);
3023 if (errcode && err == NULL)
3024 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3029 static struct got_object_id *
3030 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3033 struct tog_blame_line *line;
3035 line = &lines[first_displayed_line - 1 + selected_line - 1];
3036 if (!line->annotated)
3042 static const struct got_error *
3043 stop_blame(struct tog_blame *blame)
3045 const struct got_error *err = NULL;
3048 if (blame->thread) {
3050 errcode = pthread_mutex_unlock(&tog_mutex);
3052 return got_error_set_errno(errcode,
3053 "pthread_mutex_unlock");
3054 errcode = pthread_join(blame->thread, (void **)&err);
3056 return got_error_set_errno(errcode, "pthread_join");
3057 errcode = pthread_mutex_lock(&tog_mutex);
3059 return got_error_set_errno(errcode,
3060 "pthread_mutex_lock");
3061 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3063 blame->thread = NULL;
3065 if (blame->thread_args.repo) {
3066 got_repo_close(blame->thread_args.repo);
3067 blame->thread_args.repo = NULL;
3070 if (fclose(blame->f) != 0 && err == NULL)
3071 err = got_error_from_errno("fclose");
3075 for (i = 0; i < blame->nlines; i++)
3076 free(blame->lines[i].id);
3078 blame->lines = NULL;
3080 free(blame->cb_args.commit_id);
3081 blame->cb_args.commit_id = NULL;
3086 static const struct got_error *
3087 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3088 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3089 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3090 struct got_repository *repo)
3092 const struct got_error *err = NULL;
3093 struct got_blob_object *blob = NULL;
3094 struct got_repository *thread_repo = NULL;
3095 struct got_object_id *obj_id = NULL;
3098 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3102 return got_error(GOT_ERR_NO_OBJ);
3104 err = got_object_get_type(&obj_type, repo, obj_id);
3108 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3109 err = got_error(GOT_ERR_OBJ_TYPE);
3113 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3116 blame->f = got_opentemp();
3117 if (blame->f == NULL) {
3118 err = got_error_from_errno("got_opentemp");
3121 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3122 &blame->line_offsets, blame->f, blob);
3126 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3127 if (blame->lines == NULL) {
3128 err = got_error_from_errno("calloc");
3132 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3136 blame->cb_args.view = view;
3137 blame->cb_args.lines = blame->lines;
3138 blame->cb_args.nlines = blame->nlines;
3139 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3140 if (blame->cb_args.commit_id == NULL) {
3141 err = got_error_from_errno("got_object_id_dup");
3144 blame->cb_args.quit = done;
3146 blame->thread_args.path = path;
3147 blame->thread_args.repo = thread_repo;
3148 blame->thread_args.cb_args = &blame->cb_args;
3149 blame->thread_args.complete = blame_complete;
3150 *blame_complete = 0;
3154 got_object_blob_close(blob);
3161 static const struct got_error *
3162 open_blame_view(struct tog_view *view, char *path,
3163 struct got_object_id *commit_id, struct got_reflist_head *refs,
3164 struct got_repository *repo)
3166 const struct got_error *err = NULL;
3167 struct tog_blame_view_state *s = &view->state.blame;
3169 SIMPLEQ_INIT(&s->blamed_commits);
3171 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3175 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3176 s->first_displayed_line = 1;
3177 s->last_displayed_line = view->nlines;
3178 s->selected_line = 1;
3179 s->blame_complete = 0;
3181 if (s->path == NULL)
3182 return got_error_from_errno("open_blame_view");
3185 s->commit_id = commit_id;
3186 memset(&s->blame, 0, sizeof(s->blame));
3188 view->show = show_blame_view;
3189 view->input = input_blame_view;
3190 view->close = close_blame_view;
3191 view->search_start = search_start_blame_view;
3192 view->search_next = search_next_blame_view;
3194 return run_blame(&s->blame, view, &s->blame_complete,
3195 &s->first_displayed_line, &s->last_displayed_line,
3196 &s->selected_line, &s->done, &s->eof, s->path,
3197 s->blamed_commit->id, s->repo);
3200 static const struct got_error *
3201 close_blame_view(struct tog_view *view)
3203 const struct got_error *err = NULL;
3204 struct tog_blame_view_state *s = &view->state.blame;
3206 if (s->blame.thread)
3207 err = stop_blame(&s->blame);
3209 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3210 struct got_object_qid *blamed_commit;
3211 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3212 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3213 got_object_qid_free(blamed_commit);
3221 static const struct got_error *
3222 search_start_blame_view(struct tog_view *view)
3224 struct tog_blame_view_state *s = &view->state.blame;
3226 s->matched_line = 0;
3231 match_line(const char *line, regex_t *regex)
3233 regmatch_t regmatch;
3235 return regexec(regex, line, 1, ®match, 0) == 0;
3239 static const struct got_error *
3240 search_next_blame_view(struct tog_view *view)
3242 struct tog_blame_view_state *s = &view->state.blame;
3245 if (!view->searching) {
3246 view->search_next_done = 1;
3250 if (s->matched_line) {
3251 if (view->searching == TOG_SEARCH_FORWARD)
3252 lineno = s->matched_line + 1;
3254 lineno = s->matched_line - 1;
3256 if (view->searching == TOG_SEARCH_FORWARD)
3259 lineno = s->blame.nlines;
3267 if (lineno <= 0 || lineno > s->blame.nlines) {
3268 if (s->matched_line == 0) {
3269 view->search_next_done = 1;
3274 if (view->searching == TOG_SEARCH_FORWARD)
3277 lineno = s->blame.nlines;
3280 offset = s->blame.line_offsets[lineno - 1];
3281 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3283 return got_error_from_errno("fseeko");
3286 line = parse_next_line(s->blame.f, &len);
3287 if (line && match_line(line, &view->regex)) {
3288 view->search_next_done = 1;
3289 s->matched_line = lineno;
3294 if (view->searching == TOG_SEARCH_FORWARD)
3300 if (s->matched_line) {
3301 s->first_displayed_line = s->matched_line;
3302 s->selected_line = 1;
3308 static const struct got_error *
3309 show_blame_view(struct tog_view *view)
3311 const struct got_error *err = NULL;
3312 struct tog_blame_view_state *s = &view->state.blame;
3315 if (s->blame.thread == NULL) {
3316 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3317 &s->blame.thread_args);
3319 return got_error_set_errno(errcode, "pthread_create");
3322 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3323 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3324 s->selected_line, &s->first_displayed_line,
3325 &s->last_displayed_line, &s->eof, view->nlines);
3331 static const struct got_error *
3332 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3333 struct tog_view **focus_view, struct tog_view *view, int ch)
3335 const struct got_error *err = NULL, *thread_err = NULL;
3336 struct tog_view *diff_view;
3337 struct tog_blame_view_state *s = &view->state.blame;
3346 if (s->selected_line > 1)
3348 else if (s->selected_line == 1 &&
3349 s->first_displayed_line > 1)
3350 s->first_displayed_line--;
3353 if (s->first_displayed_line == 1) {
3354 s->selected_line = 1;
3357 if (s->first_displayed_line > view->nlines - 2)
3358 s->first_displayed_line -=
3361 s->first_displayed_line = 1;
3365 if (s->selected_line < view->nlines - 2 &&
3366 s->first_displayed_line +
3367 s->selected_line <= s->blame.nlines)
3369 else if (s->last_displayed_line <
3371 s->first_displayed_line++;
3375 struct got_object_id *id = NULL;
3376 id = get_selected_commit_id(s->blame.lines,
3377 s->first_displayed_line, s->selected_line);
3381 struct got_commit_object *commit;
3382 struct got_object_qid *pid;
3383 struct got_object_id *blob_id = NULL;
3385 err = got_object_open_as_commit(&commit,
3389 pid = SIMPLEQ_FIRST(
3390 got_object_commit_get_parent_ids(commit));
3392 got_object_commit_close(commit);
3395 /* Check if path history ends here. */
3396 err = got_object_id_by_path(&blob_id, s->repo,
3399 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3401 got_object_commit_close(commit);
3404 err = got_object_get_type(&obj_type, s->repo,
3407 /* Can't blame non-blob type objects. */
3408 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3409 got_object_commit_close(commit);
3412 err = got_object_qid_alloc(&s->blamed_commit,
3414 got_object_commit_close(commit);
3416 if (got_object_id_cmp(id,
3417 s->blamed_commit->id) == 0)
3419 err = got_object_qid_alloc(&s->blamed_commit,
3425 thread_err = stop_blame(&s->blame);
3429 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3430 s->blamed_commit, entry);
3431 err = run_blame(&s->blame, view, &s->blame_complete,
3432 &s->first_displayed_line, &s->last_displayed_line,
3433 &s->selected_line, &s->done, &s->eof,
3434 s->path, s->blamed_commit->id, s->repo);
3440 struct got_object_qid *first;
3441 first = SIMPLEQ_FIRST(&s->blamed_commits);
3442 if (!got_object_id_cmp(first->id, s->commit_id))
3445 thread_err = stop_blame(&s->blame);
3449 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3450 got_object_qid_free(s->blamed_commit);
3452 SIMPLEQ_FIRST(&s->blamed_commits);
3453 err = run_blame(&s->blame, view, &s->blame_complete,
3454 &s->first_displayed_line, &s->last_displayed_line,
3455 &s->selected_line, &s->done, &s->eof, s->path,
3456 s->blamed_commit->id, s->repo);
3463 struct got_object_id *id = NULL;
3464 struct got_object_qid *pid;
3465 struct got_commit_object *commit = NULL;
3466 id = get_selected_commit_id(s->blame.lines,
3467 s->first_displayed_line, s->selected_line);
3470 err = got_object_open_as_commit(&commit, s->repo, id);
3473 pid = SIMPLEQ_FIRST(
3474 got_object_commit_get_parent_ids(commit));
3475 if (view_is_parent_view(view))
3476 begin_x = view_split_begin_x(view->begin_x);
3477 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3478 if (diff_view == NULL) {
3479 got_object_commit_close(commit);
3480 err = got_error_from_errno("view_open");
3483 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3484 id, NULL, s->refs, s->repo);
3485 got_object_commit_close(commit);
3487 view_close(diff_view);
3490 if (view_is_parent_view(view)) {
3491 err = view_close_child(view);
3494 err = view_set_child(view, diff_view);
3496 view_close(diff_view);
3499 *focus_view = diff_view;
3500 view->child_focussed = 1;
3502 *new_view = diff_view;
3509 if (s->last_displayed_line >= s->blame.nlines &&
3510 s->selected_line >= MIN(s->blame.nlines,
3511 view->nlines - 2)) {
3514 if (s->last_displayed_line >= s->blame.nlines &&
3515 s->selected_line < view->nlines - 2) {
3516 s->selected_line = MIN(s->blame.nlines,
3520 if (s->last_displayed_line + view->nlines - 2
3522 s->first_displayed_line +=
3525 s->first_displayed_line =
3530 if (s->selected_line > view->nlines - 2) {
3531 s->selected_line = MIN(s->blame.nlines,
3538 return thread_err ? thread_err : err;
3541 static const struct got_error *
3542 cmd_blame(int argc, char *argv[])
3544 const struct got_error *error;
3545 struct got_repository *repo = NULL;
3546 struct got_reflist_head refs;
3547 struct got_worktree *worktree = NULL;
3548 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3549 struct got_object_id *commit_id = NULL;
3550 char *commit_id_str = NULL;
3552 struct tog_view *view;
3554 SIMPLEQ_INIT(&refs);
3557 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3562 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3565 commit_id_str = optarg;
3568 repo_path = realpath(optarg, NULL);
3569 if (repo_path == NULL)
3570 err(1, "-r option");
3586 cwd = getcwd(NULL, 0);
3588 error = got_error_from_errno("getcwd");
3591 if (repo_path == NULL) {
3592 error = got_worktree_open(&worktree, cwd);
3593 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3599 strdup(got_worktree_get_repo_path(worktree));
3600 if (repo_path == NULL)
3601 error = got_error_from_errno("strdup");
3605 repo_path = strdup(cwd);
3606 if (repo_path == NULL) {
3607 error = got_error_from_errno("strdup");
3615 error = got_repo_open(&repo, repo_path);
3619 error = apply_unveil(got_repo_get_path(repo), NULL);
3624 const char *prefix = got_worktree_get_path_prefix(worktree);
3625 char *p, *worktree_subdir = cwd +
3626 strlen(got_worktree_get_root_path(worktree));
3627 if (asprintf(&p, "%s%s%s%s%s",
3628 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3629 worktree_subdir, worktree_subdir[0] ? "/" : "",
3631 error = got_error_from_errno("asprintf");
3634 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3637 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3642 if (commit_id_str == NULL) {
3643 struct got_reference *head_ref;
3644 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3647 error = got_ref_resolve(&commit_id, repo, head_ref);
3648 got_ref_close(head_ref);
3650 error = got_object_resolve_id_str(&commit_id, repo,
3656 error = got_ref_list(&refs, repo);
3660 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3662 error = got_error_from_errno("view_open");
3665 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3668 error = view_loop(view);
3674 got_worktree_close(worktree);
3676 got_repo_close(repo);
3677 got_ref_list_free(&refs);
3681 static const struct got_error *
3682 draw_tree_entries(struct tog_view *view,
3683 struct got_tree_entry **first_displayed_entry,
3684 struct got_tree_entry **last_displayed_entry,
3685 struct got_tree_entry **selected_entry, int *ndisplayed,
3686 const char *label, int show_ids, const char *parent_path,
3687 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3689 const struct got_error *err = NULL;
3690 struct got_tree_entry *te;
3696 werase(view->window);
3701 err = format_line(&wline, &width, label, view->ncols);
3704 if (view_needs_focus_indication(view))
3705 wstandout(view->window);
3706 waddwstr(view->window, wline);
3707 if (view_needs_focus_indication(view))
3708 wstandend(view->window);
3711 if (width < view->ncols - 1)
3712 waddch(view->window, '\n');
3715 err = format_line(&wline, &width, parent_path, view->ncols);
3718 waddwstr(view->window, wline);
3721 if (width < view->ncols - 1)
3722 waddch(view->window, '\n');
3725 waddch(view->window, '\n');
3729 te = SIMPLEQ_FIRST(&entries->head);
3730 if (*first_displayed_entry == NULL) {
3731 if (selected == 0) {
3733 wstandout(view->window);
3734 *selected_entry = NULL;
3736 waddstr(view->window, " ..\n"); /* parent directory */
3737 if (selected == 0 && view->focussed)
3738 wstandend(view->window);
3745 while (te != *first_displayed_entry)
3746 te = SIMPLEQ_NEXT(te, entry);
3750 char *line = NULL, *id_str = NULL;
3753 err = got_object_id_str(&id_str, te->id);
3755 return got_error_from_errno(
3756 "got_object_id_str");
3758 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3759 te->name, S_ISDIR(te->mode) ? "/" :
3760 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3762 return got_error_from_errno("asprintf");
3765 err = format_line(&wline, &width, line, view->ncols);
3770 if (n == selected) {
3772 wstandout(view->window);
3773 *selected_entry = te;
3775 waddwstr(view->window, wline);
3776 if (width < view->ncols - 1)
3777 waddch(view->window, '\n');
3778 if (n == selected && view->focussed)
3779 wstandend(view->window);
3785 *last_displayed_entry = te;
3788 te = SIMPLEQ_NEXT(te, entry);
3795 tree_scroll_up(struct tog_view *view,
3796 struct got_tree_entry **first_displayed_entry, int maxscroll,
3797 const struct got_tree_entries *entries, int isroot)
3799 struct got_tree_entry *te, *prev;
3802 if (*first_displayed_entry == NULL)
3805 te = SIMPLEQ_FIRST(&entries->head);
3806 if (*first_displayed_entry == te) {
3808 *first_displayed_entry = NULL;
3812 /* XXX this is stupid... switch to TAILQ? */
3813 for (i = 0; i < maxscroll; i++) {
3814 while (te != *first_displayed_entry) {
3816 te = SIMPLEQ_NEXT(te, entry);
3818 *first_displayed_entry = prev;
3819 te = SIMPLEQ_FIRST(&entries->head);
3821 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3822 *first_displayed_entry = NULL;
3826 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3827 struct got_tree_entry *last_displayed_entry,
3828 const struct got_tree_entries *entries)
3830 struct got_tree_entry *next, *last;
3833 if (*first_displayed_entry)
3834 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3836 next = SIMPLEQ_FIRST(&entries->head);
3837 last = last_displayed_entry;
3838 while (next && last && n++ < maxscroll) {
3839 last = SIMPLEQ_NEXT(last, entry);
3841 *first_displayed_entry = next;
3842 next = SIMPLEQ_NEXT(next, entry);
3848 static const struct got_error *
3849 tree_entry_path(char **path, struct tog_parent_trees *parents,
3850 struct got_tree_entry *te)
3852 const struct got_error *err = NULL;
3853 struct tog_parent_tree *pt;
3854 size_t len = 2; /* for leading slash and NUL */
3856 TAILQ_FOREACH(pt, parents, entry)
3857 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3859 len += strlen(te->name);
3861 *path = calloc(1, len);
3863 return got_error_from_errno("calloc");
3866 pt = TAILQ_LAST(parents, tog_parent_trees);
3868 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3869 err = got_error(GOT_ERR_NO_SPACE);
3872 if (strlcat(*path, "/", len) >= len) {
3873 err = got_error(GOT_ERR_NO_SPACE);
3876 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3879 if (strlcat(*path, te->name, len) >= len) {
3880 err = got_error(GOT_ERR_NO_SPACE);
3892 static const struct got_error *
3893 blame_tree_entry(struct tog_view **new_view, int begin_x,
3894 struct got_tree_entry *te, struct tog_parent_trees *parents,
3895 struct got_object_id *commit_id, struct got_reflist_head *refs,
3896 struct got_repository *repo)
3898 const struct got_error *err = NULL;
3900 struct tog_view *blame_view;
3902 err = tree_entry_path(&path, parents, te);
3906 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3907 if (blame_view == NULL)
3908 return got_error_from_errno("view_open");
3910 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3912 view_close(blame_view);
3915 *new_view = blame_view;
3919 static const struct got_error *
3920 log_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 struct tog_view *log_view;
3926 const struct got_error *err = NULL;
3929 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3930 if (log_view == NULL)
3931 return got_error_from_errno("view_open");
3933 err = tree_entry_path(&path, parents, te);
3937 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3939 view_close(log_view);
3941 *new_view = log_view;
3946 static const struct got_error *
3947 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3948 struct got_object_id *commit_id, struct got_reflist_head *refs,
3949 struct got_repository *repo)
3951 const struct got_error *err = NULL;
3952 char *commit_id_str = NULL;
3953 struct tog_tree_view_state *s = &view->state.tree;
3955 TAILQ_INIT(&s->parents);
3957 err = got_object_id_str(&commit_id_str, commit_id);
3961 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3962 err = got_error_from_errno("asprintf");
3966 s->root = s->tree = root;
3967 s->entries = got_object_tree_get_entries(root);
3968 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3969 s->commit_id = got_object_id_dup(commit_id);
3970 if (s->commit_id == NULL) {
3971 err = got_error_from_errno("got_object_id_dup");
3977 view->show = show_tree_view;
3978 view->input = input_tree_view;
3979 view->close = close_tree_view;
3980 view->search_start = search_start_tree_view;
3981 view->search_next = search_next_tree_view;
3983 free(commit_id_str);
3985 free(s->tree_label);
3986 s->tree_label = NULL;
3991 static const struct got_error *
3992 close_tree_view(struct tog_view *view)
3994 struct tog_tree_view_state *s = &view->state.tree;
3996 free(s->tree_label);
3997 s->tree_label = NULL;
3999 s->commit_id = NULL;
4000 while (!TAILQ_EMPTY(&s->parents)) {
4001 struct tog_parent_tree *parent;
4002 parent = TAILQ_FIRST(&s->parents);
4003 TAILQ_REMOVE(&s->parents, parent, entry);
4007 if (s->tree != s->root)
4008 got_object_tree_close(s->tree);
4009 got_object_tree_close(s->root);
4014 static const struct got_error *
4015 search_start_tree_view(struct tog_view *view)
4017 struct tog_tree_view_state *s = &view->state.tree;
4019 s->matched_entry = NULL;
4024 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4026 regmatch_t regmatch;
4028 return regexec(regex, te->name, 1, ®match, 0) == 0;
4031 static const struct got_error *
4032 search_next_tree_view(struct tog_view *view)
4034 struct tog_tree_view_state *s = &view->state.tree;
4035 struct got_tree_entry *entry, *te;
4037 if (!view->searching) {
4038 view->search_next_done = 1;
4042 if (s->matched_entry) {
4043 if (view->searching == TOG_SEARCH_FORWARD) {
4044 if (s->selected_entry)
4045 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4047 entry = SIMPLEQ_FIRST(&s->entries->head);
4050 if (s->selected_entry == NULL) {
4051 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4054 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4056 if (SIMPLEQ_NEXT(te, entry) ==
4063 if (view->searching == TOG_SEARCH_FORWARD)
4064 entry = SIMPLEQ_FIRST(&s->entries->head);
4066 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4072 if (entry == NULL) {
4073 if (s->matched_entry == NULL) {
4074 view->search_next_done = 1;
4077 if (view->searching == TOG_SEARCH_FORWARD)
4078 entry = SIMPLEQ_FIRST(&s->entries->head);
4080 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4085 if (match_tree_entry(entry, &view->regex)) {
4086 view->search_next_done = 1;
4087 s->matched_entry = entry;
4091 if (view->searching == TOG_SEARCH_FORWARD)
4092 entry = SIMPLEQ_NEXT(entry, entry);
4094 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4097 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4098 if (SIMPLEQ_NEXT(te, entry) == entry) {
4107 if (s->matched_entry) {
4108 s->first_displayed_entry = s->matched_entry;
4115 static const struct got_error *
4116 show_tree_view(struct tog_view *view)
4118 const struct got_error *err = NULL;
4119 struct tog_tree_view_state *s = &view->state.tree;
4122 err = tree_entry_path(&parent_path, &s->parents, NULL);
4126 err = draw_tree_entries(view, &s->first_displayed_entry,
4127 &s->last_displayed_entry, &s->selected_entry,
4128 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4129 s->entries, s->selected, view->nlines, s->tree == s->root);
4136 static const struct got_error *
4137 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4138 struct tog_view **focus_view, struct tog_view *view, int ch)
4140 const struct got_error *err = NULL;
4141 struct tog_tree_view_state *s = &view->state.tree;
4142 struct tog_view *log_view;
4143 int begin_x = 0, nscrolled;
4147 s->show_ids = !s->show_ids;
4150 if (!s->selected_entry)
4152 if (view_is_parent_view(view))
4153 begin_x = view_split_begin_x(view->begin_x);
4154 err = log_tree_entry(&log_view, begin_x,
4155 s->selected_entry, &s->parents,
4156 s->commit_id, s->refs, s->repo);
4157 if (view_is_parent_view(view)) {
4158 err = view_close_child(view);
4161 err = view_set_child(view, log_view);
4163 view_close(log_view);
4166 *focus_view = log_view;
4167 view->child_focussed = 1;
4169 *new_view = log_view;
4173 if (s->selected > 0) {
4175 if (s->selected == 0)
4178 if (s->selected > 0)
4180 tree_scroll_up(view, &s->first_displayed_entry, 1,
4181 s->entries, s->tree == s->root);
4184 tree_scroll_up(view, &s->first_displayed_entry,
4185 MAX(0, view->nlines - 4 - s->selected), s->entries,
4186 s->tree == s->root);
4188 if (SIMPLEQ_FIRST(&s->entries->head) ==
4189 s->first_displayed_entry && s->tree != s->root)
4190 s->first_displayed_entry = NULL;
4194 if (s->selected < s->ndisplayed - 1) {
4198 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4199 /* can't scroll any further */
4201 tree_scroll_down(&s->first_displayed_entry, 1,
4202 s->last_displayed_entry, s->entries);
4205 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4207 /* can't scroll any further; move cursor down */
4208 if (s->selected < s->ndisplayed - 1)
4209 s->selected = s->ndisplayed - 1;
4212 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4213 view->nlines, s->last_displayed_entry, s->entries);
4214 if (nscrolled < view->nlines) {
4216 struct got_tree_entry *te;
4217 te = s->first_displayed_entry;
4220 te = SIMPLEQ_NEXT(te, entry);
4222 s->selected = ndisplayed - 1;
4228 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4229 struct tog_parent_tree *parent;
4230 /* user selected '..' */
4231 if (s->tree == s->root)
4233 parent = TAILQ_FIRST(&s->parents);
4234 TAILQ_REMOVE(&s->parents, parent,
4236 got_object_tree_close(s->tree);
4237 s->tree = parent->tree;
4239 got_object_tree_get_entries(s->tree);
4240 s->first_displayed_entry =
4241 parent->first_displayed_entry;
4243 parent->selected_entry;
4244 s->selected = parent->selected;
4246 } else if (S_ISDIR(s->selected_entry->mode)) {
4247 struct got_tree_object *subtree;
4248 err = got_object_open_as_tree(&subtree,
4249 s->repo, s->selected_entry->id);
4252 err = tree_view_visit_subtree(subtree, s);
4254 got_object_tree_close(subtree);
4257 } else if (S_ISREG(s->selected_entry->mode)) {
4258 struct tog_view *blame_view;
4259 int begin_x = view_is_parent_view(view) ?
4260 view_split_begin_x(view->begin_x) : 0;
4262 err = blame_tree_entry(&blame_view, begin_x,
4263 s->selected_entry, &s->parents,
4264 s->commit_id, s->refs, s->repo);
4267 if (view_is_parent_view(view)) {
4268 err = view_close_child(view);
4271 err = view_set_child(view, blame_view);
4273 view_close(blame_view);
4276 *focus_view = blame_view;
4277 view->child_focussed = 1;
4279 *new_view = blame_view;
4283 if (s->selected > view->nlines)
4284 s->selected = s->ndisplayed - 1;
4297 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4302 static const struct got_error *
4303 cmd_tree(int argc, char *argv[])
4305 const struct got_error *error;
4306 struct got_repository *repo = NULL;
4307 struct got_reflist_head refs;
4308 char *repo_path = NULL;
4309 struct got_object_id *commit_id = NULL;
4310 char *commit_id_arg = NULL;
4311 struct got_commit_object *commit = NULL;
4312 struct got_tree_object *tree = NULL;
4314 struct tog_view *view;
4316 SIMPLEQ_INIT(&refs);
4319 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4324 while ((ch = getopt(argc, argv, "c:")) != -1) {
4327 commit_id_arg = optarg;
4339 struct got_worktree *worktree;
4340 char *cwd = getcwd(NULL, 0);
4342 return got_error_from_errno("getcwd");
4343 error = got_worktree_open(&worktree, cwd);
4344 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4349 strdup(got_worktree_get_repo_path(worktree));
4350 got_worktree_close(worktree);
4353 if (repo_path == NULL) {
4354 error = got_error_from_errno("strdup");
4357 } else if (argc == 1) {
4358 repo_path = realpath(argv[0], NULL);
4359 if (repo_path == NULL)
4360 return got_error_from_errno2("realpath", argv[0]);
4366 error = got_repo_open(&repo, repo_path);
4370 error = apply_unveil(got_repo_get_path(repo), NULL);
4374 if (commit_id_arg == NULL)
4375 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4377 error = got_object_resolve_id_str(&commit_id, repo,
4382 error = got_object_open_as_commit(&commit, repo, commit_id);
4386 error = got_object_open_as_tree(&tree, repo,
4387 got_object_commit_get_tree_id(commit));
4391 error = got_ref_list(&refs, repo);
4395 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4397 error = got_error_from_errno("view_open");
4400 error = open_tree_view(view, tree, commit_id, &refs, repo);
4403 error = view_loop(view);
4408 got_object_commit_close(commit);
4410 got_object_tree_close(tree);
4412 got_repo_close(repo);
4413 got_ref_list_free(&refs);
4420 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4425 make_argv(const char *arg0, const char *arg1)
4428 int argc = (arg1 == NULL ? 1 : 2);
4430 argv = calloc(argc, sizeof(char *));
4433 argv[0] = strdup(arg0);
4434 if (argv[0] == NULL)
4437 argv[1] = strdup(arg1);
4438 if (argv[1] == NULL)
4446 main(int argc, char *argv[])
4448 const struct got_error *error = NULL;
4449 struct tog_cmd *cmd = NULL;
4451 char **cmd_argv = NULL;
4453 setlocale(LC_CTYPE, "");
4455 while ((ch = getopt(argc, argv, "h")) != -1) {
4474 /* Build an argument vector which runs a default command. */
4475 cmd = &tog_commands[0];
4476 cmd_argv = make_argv(cmd->name, NULL);
4481 /* Did the user specific a command? */
4482 for (i = 0; i < nitems(tog_commands); i++) {
4483 if (strncmp(tog_commands[i].name, argv[0],
4484 strlen(argv[0])) == 0) {
4485 cmd = &tog_commands[i];
4487 tog_commands[i].cmd_usage();
4492 /* Did the user specify a repository? */
4493 char *repo_path = realpath(argv[0], NULL);
4495 struct got_repository *repo;
4496 error = got_repo_open(&repo, repo_path);
4498 got_repo_close(repo);
4500 error = got_error_from_errno2("realpath",
4504 fprintf(stderr, "%s: '%s' is not a "
4505 "known command\n", getprogname(),
4509 fprintf(stderr, "%s: '%s' is neither a known "
4510 "command nor a path to a repository\n",
4511 getprogname(), argv[0]);
4515 cmd = &tog_commands[0];
4516 cmd_argv = make_argv(cmd->name, repo_path);
4522 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4529 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);