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 if (s->matched_entry == NULL) {
1769 view->search_next_done = 1;
1772 if (view->searching == TOG_SEARCH_FORWARD)
1773 entry = TAILQ_FIRST(&s->commits.head);
1775 entry = TAILQ_LAST(&s->commits.head,
1778 s->thread_args.commits_needed = 1;
1779 return trigger_log_thread(0,
1780 &s->thread_args.commits_needed,
1781 &s->thread_args.log_complete,
1782 &s->thread_args.need_commits);
1786 if (match_commit(entry->commit, &view->regex)) {
1787 view->search_next_done = 1;
1788 s->matched_entry = entry;
1791 if (view->searching == TOG_SEARCH_FORWARD)
1792 entry = TAILQ_NEXT(entry, entry);
1794 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1797 if (s->matched_entry) {
1798 int cur = s->selected_entry->idx;
1799 while (cur < s->matched_entry->idx) {
1800 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1805 while (cur > s->matched_entry->idx) {
1806 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1816 static const struct got_error *
1817 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1818 struct got_reflist_head *refs, struct got_repository *repo,
1819 const char *path, int check_disk)
1821 const struct got_error *err = NULL;
1822 struct tog_log_view_state *s = &view->state.log;
1823 struct got_repository *thread_repo = NULL;
1824 struct got_commit_graph *thread_graph = NULL;
1827 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1831 /* The commit queue only contains commits being displayed. */
1832 TAILQ_INIT(&s->commits.head);
1833 s->commits.ncommits = 0;
1837 s->start_id = got_object_id_dup(start_id);
1838 if (s->start_id == NULL) {
1839 err = got_error_from_errno("got_object_id_dup");
1843 view->show = show_log_view;
1844 view->input = input_log_view;
1845 view->close = close_log_view;
1846 view->search_start = search_start_log_view;
1847 view->search_next = search_next_log_view;
1849 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1852 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1857 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1859 err = got_error_set_errno(errcode, "pthread_cond_init");
1863 s->thread_args.commits_needed = view->nlines;
1864 s->thread_args.graph = thread_graph;
1865 s->thread_args.commits = &s->commits;
1866 s->thread_args.in_repo_path = s->in_repo_path;
1867 s->thread_args.start_id = s->start_id;
1868 s->thread_args.repo = thread_repo;
1869 s->thread_args.log_complete = 0;
1870 s->thread_args.quit = &s->quit;
1871 s->thread_args.view = view;
1872 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1873 s->thread_args.selected_entry = &s->selected_entry;
1876 close_log_view(view);
1880 static const struct got_error *
1881 show_log_view(struct tog_view *view)
1883 struct tog_log_view_state *s = &view->state.log;
1885 if (s->thread == NULL) {
1886 int errcode = pthread_create(&s->thread, NULL, log_thread,
1889 return got_error_set_errno(errcode, "pthread_create");
1892 return draw_commits(view, &s->last_displayed_entry,
1893 &s->selected_entry, s->first_displayed_entry,
1894 &s->commits, s->selected, view->nlines, s->refs,
1895 s->in_repo_path, s->thread_args.commits_needed);
1898 static const struct got_error *
1899 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1900 struct tog_view **focus_view, struct tog_view *view, int ch)
1902 const struct got_error *err = NULL;
1903 struct tog_log_view_state *s = &view->state.log;
1905 struct tog_view *diff_view = NULL, *tree_view = NULL;
1916 if (s->first_displayed_entry == NULL)
1918 if (s->selected > 0)
1921 scroll_up(view, &s->first_displayed_entry, 1,
1926 if (s->first_displayed_entry == NULL)
1928 if (TAILQ_FIRST(&s->commits.head) ==
1929 s->first_displayed_entry) {
1933 scroll_up(view, &s->first_displayed_entry,
1934 view->nlines, &s->commits);
1940 if (s->first_displayed_entry == NULL)
1942 if (s->selected < MIN(view->nlines - 2,
1943 s->commits.ncommits - 1)) {
1947 err = scroll_down(view, &s->first_displayed_entry, 1,
1948 &s->last_displayed_entry, &s->commits,
1949 &s->thread_args.log_complete,
1950 &s->thread_args.commits_needed,
1951 &s->thread_args.need_commits);
1955 struct commit_queue_entry *first;
1956 first = s->first_displayed_entry;
1959 err = scroll_down(view, &s->first_displayed_entry,
1960 view->nlines, &s->last_displayed_entry,
1961 &s->commits, &s->thread_args.log_complete,
1962 &s->thread_args.commits_needed,
1963 &s->thread_args.need_commits);
1964 if (first == s->first_displayed_entry &&
1965 s->selected < MIN(view->nlines - 2,
1966 s->commits.ncommits - 1)) {
1967 /* can't scroll further down */
1968 s->selected = MIN(view->nlines - 2,
1969 s->commits.ncommits - 1);
1975 if (s->selected > view->nlines - 2)
1976 s->selected = view->nlines - 2;
1977 if (s->selected > s->commits.ncommits - 1)
1978 s->selected = s->commits.ncommits - 1;
1983 if (s->selected_entry == NULL)
1985 if (view_is_parent_view(view))
1986 begin_x = view_split_begin_x(view->begin_x);
1987 err = open_diff_view_for_commit(&diff_view, begin_x,
1988 s->selected_entry->commit, s->selected_entry->id,
1989 view, s->refs, s->repo);
1992 if (view_is_parent_view(view)) {
1993 err = view_close_child(view);
1996 err = view_set_child(view, diff_view);
1998 view_close(diff_view);
2001 *focus_view = diff_view;
2002 view->child_focussed = 1;
2004 *new_view = diff_view;
2007 if (s->selected_entry == NULL)
2009 if (view_is_parent_view(view))
2010 begin_x = view_split_begin_x(view->begin_x);
2011 err = browse_commit_tree(&tree_view, begin_x,
2012 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2015 if (view_is_parent_view(view)) {
2016 err = view_close_child(view);
2019 err = view_set_child(view, tree_view);
2021 view_close(tree_view);
2024 *focus_view = tree_view;
2025 view->child_focussed = 1;
2027 *new_view = tree_view;
2030 if (strcmp(s->in_repo_path, "/") == 0)
2032 parent_path = dirname(s->in_repo_path);
2033 if (parent_path && strcmp(parent_path, ".") != 0) {
2034 struct tog_view *lv;
2035 err = stop_log_thread(s);
2038 lv = view_open(view->nlines, view->ncols,
2039 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2041 return got_error_from_errno(
2043 err = open_log_view(lv, s->start_id, s->refs,
2044 s->repo, parent_path, 0);
2047 if (view_is_parent_view(view))
2050 view_set_child(view->parent, lv);
2063 static const struct got_error *
2064 apply_unveil(const char *repo_path, const char *worktree_path)
2066 const struct got_error *error;
2068 if (repo_path && unveil(repo_path, "r") != 0)
2069 return got_error_from_errno2("unveil", repo_path);
2071 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2072 return got_error_from_errno2("unveil", worktree_path);
2074 if (unveil("/tmp", "rwc") != 0)
2075 return got_error_from_errno2("unveil", "/tmp");
2077 error = got_privsep_unveil_exec_helpers();
2081 if (unveil(NULL, NULL) != 0)
2082 return got_error_from_errno("unveil");
2092 halfdelay(1); /* Do fast refresh while initial view is loading. */
2095 intrflush(stdscr, FALSE);
2096 keypad(stdscr, TRUE);
2098 signal(SIGWINCH, tog_sigwinch);
2101 static const struct got_error *
2102 cmd_log(int argc, char *argv[])
2104 const struct got_error *error;
2105 struct got_repository *repo = NULL;
2106 struct got_worktree *worktree = NULL;
2107 struct got_reflist_head refs;
2108 struct got_object_id *start_id = NULL;
2109 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2110 char *start_commit = NULL;
2112 struct tog_view *view;
2114 SIMPLEQ_INIT(&refs);
2117 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2122 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2125 start_commit = optarg;
2128 repo_path = realpath(optarg, NULL);
2129 if (repo_path == NULL)
2130 err(1, "-r option");
2141 cwd = getcwd(NULL, 0);
2143 error = got_error_from_errno("getcwd");
2146 error = got_worktree_open(&worktree, cwd);
2147 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2154 error = got_error_from_errno("strdup");
2157 } else if (argc == 1) {
2159 error = got_worktree_resolve_path(&path, worktree,
2164 path = strdup(argv[0]);
2166 error = got_error_from_errno("strdup");
2173 repo_path = worktree ?
2174 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2175 if (repo_path == NULL) {
2176 error = got_error_from_errno("strdup");
2182 error = got_repo_open(&repo, repo_path);
2186 error = apply_unveil(got_repo_get_path(repo),
2187 worktree ? got_worktree_get_root_path(worktree) : NULL);
2191 if (start_commit == NULL)
2192 error = get_head_commit_id(&start_id, worktree ?
2193 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2196 error = got_object_resolve_id_str(&start_id, repo,
2201 error = got_ref_list(&refs, repo);
2205 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2207 error = got_error_from_errno("view_open");
2210 error = open_log_view(view, start_id, &refs, repo, path, 1);
2213 error = view_loop(view);
2220 got_repo_close(repo);
2222 got_worktree_close(worktree);
2223 got_ref_list_free(&refs);
2231 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2237 parse_next_line(FILE *f, size_t *len)
2242 const char delim[3] = { '\0', '\0', '\0'};
2244 line = fparseln(f, &linelen, &lineno, delim, 0);
2250 static const struct got_error *
2251 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2252 int *last_displayed_line, int *eof, int max_lines,
2255 const struct got_error *err;
2256 int nlines = 0, nprinted = 0;
2263 werase(view->window);
2266 err = format_line(&wline, &width, header, view->ncols);
2271 if (view_needs_focus_indication(view))
2272 wstandout(view->window);
2273 waddwstr(view->window, wline);
2274 if (view_needs_focus_indication(view))
2275 wstandend(view->window);
2276 if (width < view->ncols - 1)
2277 waddch(view->window, '\n');
2285 while (nprinted < max_lines) {
2286 line = parse_next_line(f, &len);
2291 if (++nlines < *first_displayed_line) {
2296 err = format_line(&wline, &width, line, view->ncols);
2301 waddwstr(view->window, wline);
2302 if (width < view->ncols - 1)
2303 waddch(view->window, '\n');
2304 if (++nprinted == 1)
2305 *first_displayed_line = nlines;
2310 *last_displayed_line = nlines;
2315 while (nprinted < view->nlines) {
2316 waddch(view->window, '\n');
2320 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2325 wstandout(view->window);
2326 waddwstr(view->window, wline);
2327 wstandend(view->window);
2334 get_datestr(time_t *time, char *datebuf)
2336 char *p, *s = ctime_r(time, datebuf);
2337 p = strchr(s, '\n');
2343 static const struct got_error *
2344 write_commit_info(struct got_object_id *commit_id,
2345 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2347 const struct got_error *err = NULL;
2349 struct got_commit_object *commit;
2350 char *id_str = NULL;
2351 time_t committer_time;
2352 const char *author, *committer;
2353 char *refs_str = NULL;
2356 err = build_refs_str(&refs_str, refs, commit_id);
2361 err = got_object_open_as_commit(&commit, repo, commit_id);
2365 err = got_object_id_str(&id_str, commit_id);
2367 err = got_error_from_errno("got_object_id_str");
2371 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2372 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2373 err = got_error_from_errno("fprintf");
2376 if (fprintf(outfile, "from: %s\n",
2377 got_object_commit_get_author(commit)) < 0) {
2378 err = got_error_from_errno("fprintf");
2381 committer_time = got_object_commit_get_committer_time(commit);
2382 if (fprintf(outfile, "date: %s UTC\n",
2383 get_datestr(&committer_time, datebuf)) < 0) {
2384 err = got_error_from_errno("fprintf");
2387 author = got_object_commit_get_author(commit);
2388 committer = got_object_commit_get_committer(commit);
2389 if (strcmp(author, committer) != 0 &&
2390 fprintf(outfile, "via: %s\n", committer) < 0) {
2391 err = got_error_from_errno("fprintf");
2394 if (fprintf(outfile, "%s\n",
2395 got_object_commit_get_logmsg(commit)) < 0) {
2396 err = got_error_from_errno("fprintf");
2402 got_object_commit_close(commit);
2406 static const struct got_error *
2407 create_diff(struct tog_diff_view_state *s)
2409 const struct got_error *err = NULL;
2415 err = got_error_from_errno("got_opentemp");
2418 if (s->f && fclose(s->f) != 0) {
2419 err = got_error_from_errno("fclose");
2425 err = got_object_get_type(&obj_type, s->repo, s->id1);
2427 err = got_object_get_type(&obj_type, s->repo, s->id2);
2432 case GOT_OBJ_TYPE_BLOB:
2433 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2434 s->diff_context, s->repo, f);
2436 case GOT_OBJ_TYPE_TREE:
2437 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2438 s->diff_context, s->repo, f);
2440 case GOT_OBJ_TYPE_COMMIT: {
2441 const struct got_object_id_queue *parent_ids;
2442 struct got_object_qid *pid;
2443 struct got_commit_object *commit2;
2445 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2448 /* Show commit info if we're diffing to a parent/root commit. */
2450 write_commit_info(s->id2, s->refs, s->repo, f);
2452 parent_ids = got_object_commit_get_parent_ids(commit2);
2453 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2454 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2455 write_commit_info(s->id2, s->refs,
2461 got_object_commit_close(commit2);
2463 err = got_diff_objects_as_commits(s->id1, s->id2,
2464 s->diff_context, s->repo, f);
2468 err = got_error(GOT_ERR_OBJ_TYPE);
2472 if (f && fflush(f) != 0 && err == NULL)
2473 err = got_error_from_errno("fflush");
2478 diff_view_indicate_progress(struct tog_view *view)
2480 mvwaddstr(view->window, 0, 0, "diffing...");
2485 static const struct got_error *
2486 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2487 struct got_object_id *id2, struct tog_view *log_view,
2488 struct got_reflist_head *refs, struct got_repository *repo)
2490 const struct got_error *err;
2492 if (id1 != NULL && id2 != NULL) {
2494 err = got_object_get_type(&type1, repo, id1);
2497 err = got_object_get_type(&type2, repo, id2);
2502 return got_error(GOT_ERR_OBJ_TYPE);
2506 view->state.diff.id1 = got_object_id_dup(id1);
2507 if (view->state.diff.id1 == NULL)
2508 return got_error_from_errno("got_object_id_dup");
2510 view->state.diff.id1 = NULL;
2512 view->state.diff.id2 = got_object_id_dup(id2);
2513 if (view->state.diff.id2 == NULL) {
2514 free(view->state.diff.id1);
2515 view->state.diff.id1 = NULL;
2516 return got_error_from_errno("got_object_id_dup");
2518 view->state.diff.f = NULL;
2519 view->state.diff.first_displayed_line = 1;
2520 view->state.diff.last_displayed_line = view->nlines;
2521 view->state.diff.diff_context = 3;
2522 view->state.diff.log_view = log_view;
2523 view->state.diff.repo = repo;
2524 view->state.diff.refs = refs;
2526 if (log_view && view_is_splitscreen(view))
2527 show_log_view(log_view); /* draw vborder */
2528 diff_view_indicate_progress(view);
2530 err = create_diff(&view->state.diff);
2532 free(view->state.diff.id1);
2533 view->state.diff.id1 = NULL;
2534 free(view->state.diff.id2);
2535 view->state.diff.id2 = NULL;
2539 view->show = show_diff_view;
2540 view->input = input_diff_view;
2541 view->close = close_diff_view;
2546 static const struct got_error *
2547 close_diff_view(struct tog_view *view)
2549 const struct got_error *err = NULL;
2551 free(view->state.diff.id1);
2552 view->state.diff.id1 = NULL;
2553 free(view->state.diff.id2);
2554 view->state.diff.id2 = NULL;
2555 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2556 err = got_error_from_errno("fclose");
2560 static const struct got_error *
2561 show_diff_view(struct tog_view *view)
2563 const struct got_error *err;
2564 struct tog_diff_view_state *s = &view->state.diff;
2565 char *id_str1 = NULL, *id_str2, *header;
2568 err = got_object_id_str(&id_str1, s->id1);
2572 err = got_object_id_str(&id_str2, s->id2);
2576 if (asprintf(&header, "diff %s %s",
2577 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2578 err = got_error_from_errno("asprintf");
2586 return draw_file(view, s->f, &s->first_displayed_line,
2587 &s->last_displayed_line, &s->eof, view->nlines,
2591 static const struct got_error *
2592 set_selected_commit(struct tog_diff_view_state *s,
2593 struct commit_queue_entry *entry)
2595 const struct got_error *err;
2596 const struct got_object_id_queue *parent_ids;
2597 struct got_commit_object *selected_commit;
2598 struct got_object_qid *pid;
2601 s->id2 = got_object_id_dup(entry->id);
2603 return got_error_from_errno("got_object_id_dup");
2605 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2608 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2610 pid = SIMPLEQ_FIRST(parent_ids);
2611 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2612 got_object_commit_close(selected_commit);
2616 static const struct got_error *
2617 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2618 struct tog_view **focus_view, struct tog_view *view, int ch)
2620 const struct got_error *err = NULL;
2621 struct tog_diff_view_state *s = &view->state.diff;
2622 struct tog_log_view_state *ls;
2623 struct commit_queue_entry *entry;
2629 if (s->first_displayed_line > 1)
2630 s->first_displayed_line--;
2634 if (s->first_displayed_line == 1)
2637 while (i++ < view->nlines - 1 &&
2638 s->first_displayed_line > 1)
2639 s->first_displayed_line--;
2644 s->first_displayed_line++;
2652 while (!s->eof && i++ < view->nlines - 1) {
2654 line = parse_next_line(s->f, NULL);
2655 s->first_displayed_line++;
2661 if (s->diff_context > 0) {
2663 diff_view_indicate_progress(view);
2664 err = create_diff(s);
2668 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2670 diff_view_indicate_progress(view);
2671 err = create_diff(s);
2676 if (s->log_view == NULL)
2678 ls = &s->log_view->state.log;
2679 entry = TAILQ_PREV(ls->selected_entry,
2680 commit_queue_head, entry);
2684 err = input_log_view(NULL, NULL, NULL, s->log_view,
2689 err = set_selected_commit(s, entry);
2693 s->first_displayed_line = 1;
2694 s->last_displayed_line = view->nlines;
2696 diff_view_indicate_progress(view);
2697 err = create_diff(s);
2701 if (s->log_view == NULL)
2703 ls = &s->log_view->state.log;
2705 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2706 ls->thread_args.commits_needed++;
2708 /* Display "loading..." in log view. */
2709 show_log_view(s->log_view);
2713 err = trigger_log_thread(1 /* load_all */,
2714 &ls->thread_args.commits_needed,
2715 &ls->thread_args.log_complete,
2716 &ls->thread_args.need_commits);
2720 err = input_log_view(NULL, NULL, NULL, s->log_view,
2725 entry = TAILQ_NEXT(ls->selected_entry, entry);
2729 err = set_selected_commit(s, entry);
2733 s->first_displayed_line = 1;
2734 s->last_displayed_line = view->nlines;
2736 diff_view_indicate_progress(view);
2737 err = create_diff(s);
2746 static const struct got_error *
2747 cmd_diff(int argc, char *argv[])
2749 const struct got_error *error = NULL;
2750 struct got_repository *repo = NULL;
2751 struct got_reflist_head refs;
2752 struct got_object_id *id1 = NULL, *id2 = NULL;
2753 char *repo_path = NULL;
2754 char *id_str1 = NULL, *id_str2 = NULL;
2756 struct tog_view *view;
2758 SIMPLEQ_INIT(&refs);
2761 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2766 while ((ch = getopt(argc, argv, "")) != -1) {
2778 usage_diff(); /* TODO show local worktree changes */
2779 } else if (argc == 2) {
2780 repo_path = getcwd(NULL, 0);
2781 if (repo_path == NULL)
2782 return got_error_from_errno("getcwd");
2785 } else if (argc == 3) {
2786 repo_path = realpath(argv[0], NULL);
2787 if (repo_path == NULL)
2788 return got_error_from_errno2("realpath", argv[0]);
2796 error = got_repo_open(&repo, repo_path);
2800 error = apply_unveil(got_repo_get_path(repo), NULL);
2804 error = got_object_resolve_id_str(&id1, repo, id_str1);
2808 error = got_object_resolve_id_str(&id2, repo, id_str2);
2812 error = got_ref_list(&refs, repo);
2816 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2818 error = got_error_from_errno("view_open");
2821 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2824 error = view_loop(view);
2827 got_repo_close(repo);
2828 got_ref_list_free(&refs);
2836 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2841 struct tog_blame_line {
2843 struct got_object_id *id;
2846 static const struct got_error *
2847 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2848 const char *path, struct tog_blame_line *lines, int nlines,
2849 int blame_complete, int selected_line, int *first_displayed_line,
2850 int *last_displayed_line, int *eof, int max_lines)
2852 const struct got_error *err;
2853 int lineno = 0, nprinted = 0;
2858 struct tog_blame_line *blame_line;
2859 struct got_object_id *prev_id = NULL;
2862 err = got_object_id_str(&id_str, id);
2867 werase(view->window);
2869 if (asprintf(&line, "commit %s", id_str) == -1) {
2870 err = got_error_from_errno("asprintf");
2875 err = format_line(&wline, &width, line, view->ncols);
2878 if (view_needs_focus_indication(view))
2879 wstandout(view->window);
2880 waddwstr(view->window, wline);
2881 if (view_needs_focus_indication(view))
2882 wstandend(view->window);
2885 if (width < view->ncols - 1)
2886 waddch(view->window, '\n');
2888 if (asprintf(&line, "[%d/%d] %s%s",
2889 *first_displayed_line - 1 + selected_line, nlines,
2890 blame_complete ? "" : "annotating... ", path) == -1) {
2892 return got_error_from_errno("asprintf");
2895 err = format_line(&wline, &width, line, view->ncols);
2900 waddwstr(view->window, wline);
2903 if (width < view->ncols - 1)
2904 waddch(view->window, '\n');
2907 while (nprinted < max_lines - 2) {
2908 line = parse_next_line(f, &len);
2913 if (++lineno < *first_displayed_line) {
2918 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2919 err = format_line(&wline, &width, line, wlimit);
2925 if (view->focussed && nprinted == selected_line - 1)
2926 wstandout(view->window);
2928 blame_line = &lines[lineno - 1];
2929 if (blame_line->annotated && prev_id &&
2930 got_object_id_cmp(prev_id, blame_line->id) == 0)
2931 waddstr(view->window, " ");
2932 else if (blame_line->annotated) {
2934 err = got_object_id_str(&id_str, blame_line->id);
2940 wprintw(view->window, "%.8s ", id_str);
2942 prev_id = blame_line->id;
2944 waddstr(view->window, "........ ");
2948 waddwstr(view->window, wline);
2949 while (width < wlimit) {
2950 waddch(view->window, ' ');
2953 if (view->focussed && nprinted == selected_line - 1)
2954 wstandend(view->window);
2955 if (++nprinted == 1)
2956 *first_displayed_line = lineno;
2961 *last_displayed_line = lineno;
2968 static const struct got_error *
2969 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2971 const struct got_error *err = NULL;
2972 struct tog_blame_cb_args *a = arg;
2973 struct tog_blame_line *line;
2976 if (nlines != a->nlines ||
2977 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2978 return got_error(GOT_ERR_RANGE);
2980 errcode = pthread_mutex_lock(&tog_mutex);
2982 return got_error_set_errno(errcode, "pthread_mutex_lock");
2984 if (*a->quit) { /* user has quit the blame view */
2985 err = got_error(GOT_ERR_ITER_COMPLETED);
2990 goto done; /* no change in this commit */
2992 line = &a->lines[lineno - 1];
2993 if (line->annotated)
2996 line->id = got_object_id_dup(id);
2997 if (line->id == NULL) {
2998 err = got_error_from_errno("got_object_id_dup");
3001 line->annotated = 1;
3003 errcode = pthread_mutex_unlock(&tog_mutex);
3005 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3010 blame_thread(void *arg)
3012 const struct got_error *err;
3013 struct tog_blame_thread_args *ta = arg;
3014 struct tog_blame_cb_args *a = ta->cb_args;
3017 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3018 blame_cb, ta->cb_args);
3020 errcode = pthread_mutex_lock(&tog_mutex);
3022 return (void *)got_error_set_errno(errcode,
3023 "pthread_mutex_lock");
3025 got_repo_close(ta->repo);
3029 errcode = pthread_mutex_unlock(&tog_mutex);
3030 if (errcode && err == NULL)
3031 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3036 static struct got_object_id *
3037 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3040 struct tog_blame_line *line;
3042 line = &lines[first_displayed_line - 1 + selected_line - 1];
3043 if (!line->annotated)
3049 static const struct got_error *
3050 stop_blame(struct tog_blame *blame)
3052 const struct got_error *err = NULL;
3055 if (blame->thread) {
3057 errcode = pthread_mutex_unlock(&tog_mutex);
3059 return got_error_set_errno(errcode,
3060 "pthread_mutex_unlock");
3061 errcode = pthread_join(blame->thread, (void **)&err);
3063 return got_error_set_errno(errcode, "pthread_join");
3064 errcode = pthread_mutex_lock(&tog_mutex);
3066 return got_error_set_errno(errcode,
3067 "pthread_mutex_lock");
3068 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3070 blame->thread = NULL;
3072 if (blame->thread_args.repo) {
3073 got_repo_close(blame->thread_args.repo);
3074 blame->thread_args.repo = NULL;
3077 if (fclose(blame->f) != 0 && err == NULL)
3078 err = got_error_from_errno("fclose");
3082 for (i = 0; i < blame->nlines; i++)
3083 free(blame->lines[i].id);
3085 blame->lines = NULL;
3087 free(blame->cb_args.commit_id);
3088 blame->cb_args.commit_id = NULL;
3093 static const struct got_error *
3094 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3095 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3096 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3097 struct got_repository *repo)
3099 const struct got_error *err = NULL;
3100 struct got_blob_object *blob = NULL;
3101 struct got_repository *thread_repo = NULL;
3102 struct got_object_id *obj_id = NULL;
3105 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3109 return got_error(GOT_ERR_NO_OBJ);
3111 err = got_object_get_type(&obj_type, repo, obj_id);
3115 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3116 err = got_error(GOT_ERR_OBJ_TYPE);
3120 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3123 blame->f = got_opentemp();
3124 if (blame->f == NULL) {
3125 err = got_error_from_errno("got_opentemp");
3128 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3129 &blame->line_offsets, blame->f, blob);
3133 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3134 if (blame->lines == NULL) {
3135 err = got_error_from_errno("calloc");
3139 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3143 blame->cb_args.view = view;
3144 blame->cb_args.lines = blame->lines;
3145 blame->cb_args.nlines = blame->nlines;
3146 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3147 if (blame->cb_args.commit_id == NULL) {
3148 err = got_error_from_errno("got_object_id_dup");
3151 blame->cb_args.quit = done;
3153 blame->thread_args.path = path;
3154 blame->thread_args.repo = thread_repo;
3155 blame->thread_args.cb_args = &blame->cb_args;
3156 blame->thread_args.complete = blame_complete;
3157 *blame_complete = 0;
3161 got_object_blob_close(blob);
3168 static const struct got_error *
3169 open_blame_view(struct tog_view *view, char *path,
3170 struct got_object_id *commit_id, struct got_reflist_head *refs,
3171 struct got_repository *repo)
3173 const struct got_error *err = NULL;
3174 struct tog_blame_view_state *s = &view->state.blame;
3176 SIMPLEQ_INIT(&s->blamed_commits);
3178 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3182 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3183 s->first_displayed_line = 1;
3184 s->last_displayed_line = view->nlines;
3185 s->selected_line = 1;
3186 s->blame_complete = 0;
3188 if (s->path == NULL)
3189 return got_error_from_errno("open_blame_view");
3192 s->commit_id = commit_id;
3193 memset(&s->blame, 0, sizeof(s->blame));
3195 view->show = show_blame_view;
3196 view->input = input_blame_view;
3197 view->close = close_blame_view;
3198 view->search_start = search_start_blame_view;
3199 view->search_next = search_next_blame_view;
3201 return run_blame(&s->blame, view, &s->blame_complete,
3202 &s->first_displayed_line, &s->last_displayed_line,
3203 &s->selected_line, &s->done, &s->eof, s->path,
3204 s->blamed_commit->id, s->repo);
3207 static const struct got_error *
3208 close_blame_view(struct tog_view *view)
3210 const struct got_error *err = NULL;
3211 struct tog_blame_view_state *s = &view->state.blame;
3213 if (s->blame.thread)
3214 err = stop_blame(&s->blame);
3216 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3217 struct got_object_qid *blamed_commit;
3218 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3219 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3220 got_object_qid_free(blamed_commit);
3228 static const struct got_error *
3229 search_start_blame_view(struct tog_view *view)
3231 struct tog_blame_view_state *s = &view->state.blame;
3233 s->matched_line = 0;
3238 match_line(const char *line, regex_t *regex)
3240 regmatch_t regmatch;
3242 return regexec(regex, line, 1, ®match, 0) == 0;
3246 static const struct got_error *
3247 search_next_blame_view(struct tog_view *view)
3249 const struct got_error *err = NULL;
3250 struct tog_blame_view_state *s = &view->state.blame;
3253 if (!view->searching) {
3254 view->search_next_done = 1;
3258 if (s->matched_line) {
3259 if (view->searching == TOG_SEARCH_FORWARD)
3260 lineno = s->first_displayed_line - 1 + s->selected_line + 1;
3262 lineno = s->first_displayed_line - 1 + s->selected_line - 1;
3264 if (view->searching == TOG_SEARCH_FORWARD)
3267 lineno = s->blame.nlines;
3275 if (lineno <= 0 || lineno > s->blame.nlines) {
3276 if (s->matched_line == 0) {
3277 view->search_next_done = 1;
3281 if (view->searching == TOG_SEARCH_FORWARD)
3284 lineno = s->blame.nlines;
3287 offset = s->blame.line_offsets[lineno - 1];
3288 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3290 return got_error_from_errno("fseeko");
3293 line = parse_next_line(s->blame.f, &len);
3296 if (match_line(line, &view->regex)) {
3297 view->search_next_done = 1;
3298 s->matched_line = lineno;
3304 if (view->searching == TOG_SEARCH_FORWARD)
3310 if (s->matched_line) {
3311 int cur = s->first_displayed_line - 1 + s->selected_line;
3312 while (cur < s->matched_line) {
3313 err = input_blame_view(NULL, NULL, NULL, view, KEY_DOWN);
3318 while (cur > s->matched_line) {
3319 err = input_blame_view(NULL, NULL, NULL, view, KEY_UP);
3324 s->first_displayed_line = s->matched_line;
3325 s->selected_line = 1;
3331 static const struct got_error *
3332 show_blame_view(struct tog_view *view)
3334 const struct got_error *err = NULL;
3335 struct tog_blame_view_state *s = &view->state.blame;
3338 if (s->blame.thread == NULL) {
3339 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3340 &s->blame.thread_args);
3342 return got_error_set_errno(errcode, "pthread_create");
3345 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3346 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3347 s->selected_line, &s->first_displayed_line,
3348 &s->last_displayed_line, &s->eof, view->nlines);
3354 static const struct got_error *
3355 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3356 struct tog_view **focus_view, struct tog_view *view, int ch)
3358 const struct got_error *err = NULL, *thread_err = NULL;
3359 struct tog_view *diff_view;
3360 struct tog_blame_view_state *s = &view->state.blame;
3369 if (s->selected_line > 1)
3371 else if (s->selected_line == 1 &&
3372 s->first_displayed_line > 1)
3373 s->first_displayed_line--;
3376 if (s->first_displayed_line == 1) {
3377 s->selected_line = 1;
3380 if (s->first_displayed_line > view->nlines - 2)
3381 s->first_displayed_line -=
3384 s->first_displayed_line = 1;
3388 if (s->selected_line < view->nlines - 2 &&
3389 s->first_displayed_line +
3390 s->selected_line <= s->blame.nlines)
3392 else if (s->last_displayed_line <
3394 s->first_displayed_line++;
3398 struct got_object_id *id = NULL;
3399 id = get_selected_commit_id(s->blame.lines,
3400 s->first_displayed_line, s->selected_line);
3404 struct got_commit_object *commit;
3405 struct got_object_qid *pid;
3406 struct got_object_id *blob_id = NULL;
3408 err = got_object_open_as_commit(&commit,
3412 pid = SIMPLEQ_FIRST(
3413 got_object_commit_get_parent_ids(commit));
3415 got_object_commit_close(commit);
3418 /* Check if path history ends here. */
3419 err = got_object_id_by_path(&blob_id, s->repo,
3422 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3424 got_object_commit_close(commit);
3427 err = got_object_get_type(&obj_type, s->repo,
3430 /* Can't blame non-blob type objects. */
3431 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3432 got_object_commit_close(commit);
3435 err = got_object_qid_alloc(&s->blamed_commit,
3437 got_object_commit_close(commit);
3439 if (got_object_id_cmp(id,
3440 s->blamed_commit->id) == 0)
3442 err = got_object_qid_alloc(&s->blamed_commit,
3448 thread_err = stop_blame(&s->blame);
3452 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3453 s->blamed_commit, entry);
3454 err = run_blame(&s->blame, view, &s->blame_complete,
3455 &s->first_displayed_line, &s->last_displayed_line,
3456 &s->selected_line, &s->done, &s->eof,
3457 s->path, s->blamed_commit->id, s->repo);
3463 struct got_object_qid *first;
3464 first = SIMPLEQ_FIRST(&s->blamed_commits);
3465 if (!got_object_id_cmp(first->id, s->commit_id))
3468 thread_err = stop_blame(&s->blame);
3472 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3473 got_object_qid_free(s->blamed_commit);
3475 SIMPLEQ_FIRST(&s->blamed_commits);
3476 err = run_blame(&s->blame, view, &s->blame_complete,
3477 &s->first_displayed_line, &s->last_displayed_line,
3478 &s->selected_line, &s->done, &s->eof, s->path,
3479 s->blamed_commit->id, s->repo);
3486 struct got_object_id *id = NULL;
3487 struct got_object_qid *pid;
3488 struct got_commit_object *commit = NULL;
3489 id = get_selected_commit_id(s->blame.lines,
3490 s->first_displayed_line, s->selected_line);
3493 err = got_object_open_as_commit(&commit, s->repo, id);
3496 pid = SIMPLEQ_FIRST(
3497 got_object_commit_get_parent_ids(commit));
3498 if (view_is_parent_view(view))
3499 begin_x = view_split_begin_x(view->begin_x);
3500 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3501 if (diff_view == NULL) {
3502 got_object_commit_close(commit);
3503 err = got_error_from_errno("view_open");
3506 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3507 id, NULL, s->refs, s->repo);
3508 got_object_commit_close(commit);
3510 view_close(diff_view);
3513 if (view_is_parent_view(view)) {
3514 err = view_close_child(view);
3517 err = view_set_child(view, diff_view);
3519 view_close(diff_view);
3522 *focus_view = diff_view;
3523 view->child_focussed = 1;
3525 *new_view = diff_view;
3532 if (s->last_displayed_line >= s->blame.nlines &&
3533 s->selected_line >= MIN(s->blame.nlines,
3534 view->nlines - 2)) {
3537 if (s->last_displayed_line >= s->blame.nlines &&
3538 s->selected_line < view->nlines - 2) {
3539 s->selected_line = MIN(s->blame.nlines,
3543 if (s->last_displayed_line + view->nlines - 2
3545 s->first_displayed_line +=
3548 s->first_displayed_line =
3553 if (s->selected_line > view->nlines - 2) {
3554 s->selected_line = MIN(s->blame.nlines,
3561 return thread_err ? thread_err : err;
3564 static const struct got_error *
3565 cmd_blame(int argc, char *argv[])
3567 const struct got_error *error;
3568 struct got_repository *repo = NULL;
3569 struct got_reflist_head refs;
3570 struct got_worktree *worktree = NULL;
3571 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3572 struct got_object_id *commit_id = NULL;
3573 char *commit_id_str = NULL;
3575 struct tog_view *view;
3577 SIMPLEQ_INIT(&refs);
3580 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3585 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3588 commit_id_str = optarg;
3591 repo_path = realpath(optarg, NULL);
3592 if (repo_path == NULL)
3593 err(1, "-r option");
3609 cwd = getcwd(NULL, 0);
3611 error = got_error_from_errno("getcwd");
3614 if (repo_path == NULL) {
3615 error = got_worktree_open(&worktree, cwd);
3616 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3622 strdup(got_worktree_get_repo_path(worktree));
3623 if (repo_path == NULL)
3624 error = got_error_from_errno("strdup");
3628 repo_path = strdup(cwd);
3629 if (repo_path == NULL) {
3630 error = got_error_from_errno("strdup");
3638 error = got_repo_open(&repo, repo_path);
3642 error = apply_unveil(got_repo_get_path(repo), NULL);
3647 const char *prefix = got_worktree_get_path_prefix(worktree);
3648 char *p, *worktree_subdir = cwd +
3649 strlen(got_worktree_get_root_path(worktree));
3650 if (asprintf(&p, "%s%s%s%s%s",
3651 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3652 worktree_subdir, worktree_subdir[0] ? "/" : "",
3654 error = got_error_from_errno("asprintf");
3657 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3660 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3665 if (commit_id_str == NULL) {
3666 struct got_reference *head_ref;
3667 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3670 error = got_ref_resolve(&commit_id, repo, head_ref);
3671 got_ref_close(head_ref);
3673 error = got_object_resolve_id_str(&commit_id, repo,
3679 error = got_ref_list(&refs, repo);
3683 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3685 error = got_error_from_errno("view_open");
3688 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3691 error = view_loop(view);
3697 got_worktree_close(worktree);
3699 got_repo_close(repo);
3700 got_ref_list_free(&refs);
3704 static const struct got_error *
3705 draw_tree_entries(struct tog_view *view,
3706 struct got_tree_entry **first_displayed_entry,
3707 struct got_tree_entry **last_displayed_entry,
3708 struct got_tree_entry **selected_entry, int *ndisplayed,
3709 const char *label, int show_ids, const char *parent_path,
3710 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3712 const struct got_error *err = NULL;
3713 struct got_tree_entry *te;
3719 werase(view->window);
3724 err = format_line(&wline, &width, label, view->ncols);
3727 if (view_needs_focus_indication(view))
3728 wstandout(view->window);
3729 waddwstr(view->window, wline);
3730 if (view_needs_focus_indication(view))
3731 wstandend(view->window);
3734 if (width < view->ncols - 1)
3735 waddch(view->window, '\n');
3738 err = format_line(&wline, &width, parent_path, view->ncols);
3741 waddwstr(view->window, wline);
3744 if (width < view->ncols - 1)
3745 waddch(view->window, '\n');
3748 waddch(view->window, '\n');
3752 te = SIMPLEQ_FIRST(&entries->head);
3753 if (*first_displayed_entry == NULL) {
3754 if (selected == 0) {
3756 wstandout(view->window);
3757 *selected_entry = NULL;
3759 waddstr(view->window, " ..\n"); /* parent directory */
3760 if (selected == 0 && view->focussed)
3761 wstandend(view->window);
3768 while (te != *first_displayed_entry)
3769 te = SIMPLEQ_NEXT(te, entry);
3773 char *line = NULL, *id_str = NULL;
3776 err = got_object_id_str(&id_str, te->id);
3778 return got_error_from_errno(
3779 "got_object_id_str");
3781 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3782 te->name, S_ISDIR(te->mode) ? "/" :
3783 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3785 return got_error_from_errno("asprintf");
3788 err = format_line(&wline, &width, line, view->ncols);
3793 if (n == selected) {
3795 wstandout(view->window);
3796 *selected_entry = te;
3798 waddwstr(view->window, wline);
3799 if (width < view->ncols - 1)
3800 waddch(view->window, '\n');
3801 if (n == selected && view->focussed)
3802 wstandend(view->window);
3808 *last_displayed_entry = te;
3811 te = SIMPLEQ_NEXT(te, entry);
3818 tree_scroll_up(struct tog_view *view,
3819 struct got_tree_entry **first_displayed_entry, int maxscroll,
3820 const struct got_tree_entries *entries, int isroot)
3822 struct got_tree_entry *te, *prev;
3825 if (*first_displayed_entry == NULL)
3828 te = SIMPLEQ_FIRST(&entries->head);
3829 if (*first_displayed_entry == te) {
3831 *first_displayed_entry = NULL;
3835 /* XXX this is stupid... switch to TAILQ? */
3836 for (i = 0; i < maxscroll; i++) {
3837 while (te != *first_displayed_entry) {
3839 te = SIMPLEQ_NEXT(te, entry);
3841 *first_displayed_entry = prev;
3842 te = SIMPLEQ_FIRST(&entries->head);
3844 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3845 *first_displayed_entry = NULL;
3849 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3850 struct got_tree_entry *last_displayed_entry,
3851 const struct got_tree_entries *entries)
3853 struct got_tree_entry *next, *last;
3856 if (*first_displayed_entry)
3857 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3859 next = SIMPLEQ_FIRST(&entries->head);
3860 last = last_displayed_entry;
3861 while (next && last && n++ < maxscroll) {
3862 last = SIMPLEQ_NEXT(last, entry);
3864 *first_displayed_entry = next;
3865 next = SIMPLEQ_NEXT(next, entry);
3871 static const struct got_error *
3872 tree_entry_path(char **path, struct tog_parent_trees *parents,
3873 struct got_tree_entry *te)
3875 const struct got_error *err = NULL;
3876 struct tog_parent_tree *pt;
3877 size_t len = 2; /* for leading slash and NUL */
3879 TAILQ_FOREACH(pt, parents, entry)
3880 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3882 len += strlen(te->name);
3884 *path = calloc(1, len);
3886 return got_error_from_errno("calloc");
3889 pt = TAILQ_LAST(parents, tog_parent_trees);
3891 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3892 err = got_error(GOT_ERR_NO_SPACE);
3895 if (strlcat(*path, "/", len) >= len) {
3896 err = got_error(GOT_ERR_NO_SPACE);
3899 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3902 if (strlcat(*path, te->name, len) >= len) {
3903 err = got_error(GOT_ERR_NO_SPACE);
3915 static const struct got_error *
3916 blame_tree_entry(struct tog_view **new_view, int begin_x,
3917 struct got_tree_entry *te, struct tog_parent_trees *parents,
3918 struct got_object_id *commit_id, struct got_reflist_head *refs,
3919 struct got_repository *repo)
3921 const struct got_error *err = NULL;
3923 struct tog_view *blame_view;
3925 err = tree_entry_path(&path, parents, te);
3929 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3930 if (blame_view == NULL)
3931 return got_error_from_errno("view_open");
3933 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3935 view_close(blame_view);
3938 *new_view = blame_view;
3942 static const struct got_error *
3943 log_tree_entry(struct tog_view **new_view, int begin_x,
3944 struct got_tree_entry *te, struct tog_parent_trees *parents,
3945 struct got_object_id *commit_id, struct got_reflist_head *refs,
3946 struct got_repository *repo)
3948 struct tog_view *log_view;
3949 const struct got_error *err = NULL;
3952 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3953 if (log_view == NULL)
3954 return got_error_from_errno("view_open");
3956 err = tree_entry_path(&path, parents, te);
3960 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3962 view_close(log_view);
3964 *new_view = log_view;
3969 static const struct got_error *
3970 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3971 struct got_object_id *commit_id, struct got_reflist_head *refs,
3972 struct got_repository *repo)
3974 const struct got_error *err = NULL;
3975 char *commit_id_str = NULL;
3976 struct tog_tree_view_state *s = &view->state.tree;
3978 TAILQ_INIT(&s->parents);
3980 err = got_object_id_str(&commit_id_str, commit_id);
3984 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3985 err = got_error_from_errno("asprintf");
3989 s->root = s->tree = root;
3990 s->entries = got_object_tree_get_entries(root);
3991 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3992 s->commit_id = got_object_id_dup(commit_id);
3993 if (s->commit_id == NULL) {
3994 err = got_error_from_errno("got_object_id_dup");
4000 view->show = show_tree_view;
4001 view->input = input_tree_view;
4002 view->close = close_tree_view;
4003 view->search_start = search_start_tree_view;
4004 view->search_next = search_next_tree_view;
4006 free(commit_id_str);
4008 free(s->tree_label);
4009 s->tree_label = NULL;
4014 static const struct got_error *
4015 close_tree_view(struct tog_view *view)
4017 struct tog_tree_view_state *s = &view->state.tree;
4019 free(s->tree_label);
4020 s->tree_label = NULL;
4022 s->commit_id = NULL;
4023 while (!TAILQ_EMPTY(&s->parents)) {
4024 struct tog_parent_tree *parent;
4025 parent = TAILQ_FIRST(&s->parents);
4026 TAILQ_REMOVE(&s->parents, parent, entry);
4030 if (s->tree != s->root)
4031 got_object_tree_close(s->tree);
4032 got_object_tree_close(s->root);
4037 static const struct got_error *
4038 search_start_tree_view(struct tog_view *view)
4040 struct tog_tree_view_state *s = &view->state.tree;
4042 s->matched_entry = NULL;
4047 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4049 regmatch_t regmatch;
4051 return regexec(regex, te->name, 1, ®match, 0) == 0;
4054 static const struct got_error *
4055 search_next_tree_view(struct tog_view *view)
4057 struct tog_tree_view_state *s = &view->state.tree;
4058 struct got_tree_entry *entry, *te;
4060 if (!view->searching) {
4061 view->search_next_done = 1;
4065 if (s->matched_entry) {
4066 if (view->searching == TOG_SEARCH_FORWARD) {
4067 if (s->selected_entry)
4068 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4070 entry = SIMPLEQ_FIRST(&s->entries->head);
4073 if (s->selected_entry == NULL) {
4074 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4077 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4079 if (SIMPLEQ_NEXT(te, entry) ==
4086 if (view->searching == TOG_SEARCH_FORWARD)
4087 entry = SIMPLEQ_FIRST(&s->entries->head);
4089 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4095 if (entry == NULL) {
4096 if (view->searching == TOG_SEARCH_FORWARD)
4097 entry = SIMPLEQ_FIRST(&s->entries->head);
4099 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4104 if (match_tree_entry(entry, &view->regex)) {
4105 view->search_next_done = 1;
4106 s->matched_entry = entry;
4110 if (view->searching == TOG_SEARCH_FORWARD)
4111 entry = SIMPLEQ_NEXT(entry, entry);
4113 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4116 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4117 if (SIMPLEQ_NEXT(te, entry) == entry) {
4126 if (s->matched_entry) {
4127 s->first_displayed_entry = s->matched_entry;
4134 static const struct got_error *
4135 show_tree_view(struct tog_view *view)
4137 const struct got_error *err = NULL;
4138 struct tog_tree_view_state *s = &view->state.tree;
4141 err = tree_entry_path(&parent_path, &s->parents, NULL);
4145 err = draw_tree_entries(view, &s->first_displayed_entry,
4146 &s->last_displayed_entry, &s->selected_entry,
4147 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4148 s->entries, s->selected, view->nlines, s->tree == s->root);
4155 static const struct got_error *
4156 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4157 struct tog_view **focus_view, struct tog_view *view, int ch)
4159 const struct got_error *err = NULL;
4160 struct tog_tree_view_state *s = &view->state.tree;
4161 struct tog_view *log_view;
4162 int begin_x = 0, nscrolled;
4166 s->show_ids = !s->show_ids;
4169 if (!s->selected_entry)
4171 if (view_is_parent_view(view))
4172 begin_x = view_split_begin_x(view->begin_x);
4173 err = log_tree_entry(&log_view, begin_x,
4174 s->selected_entry, &s->parents,
4175 s->commit_id, s->refs, s->repo);
4176 if (view_is_parent_view(view)) {
4177 err = view_close_child(view);
4180 err = view_set_child(view, log_view);
4182 view_close(log_view);
4185 *focus_view = log_view;
4186 view->child_focussed = 1;
4188 *new_view = log_view;
4192 if (s->selected > 0) {
4194 if (s->selected == 0)
4197 if (s->selected > 0)
4199 tree_scroll_up(view, &s->first_displayed_entry, 1,
4200 s->entries, s->tree == s->root);
4203 tree_scroll_up(view, &s->first_displayed_entry,
4204 MAX(0, view->nlines - 4 - s->selected), s->entries,
4205 s->tree == s->root);
4207 if (SIMPLEQ_FIRST(&s->entries->head) ==
4208 s->first_displayed_entry && s->tree != s->root)
4209 s->first_displayed_entry = NULL;
4213 if (s->selected < s->ndisplayed - 1) {
4217 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4218 /* can't scroll any further */
4220 tree_scroll_down(&s->first_displayed_entry, 1,
4221 s->last_displayed_entry, s->entries);
4224 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4226 /* can't scroll any further; move cursor down */
4227 if (s->selected < s->ndisplayed - 1)
4228 s->selected = s->ndisplayed - 1;
4231 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4232 view->nlines, s->last_displayed_entry, s->entries);
4233 if (nscrolled < view->nlines) {
4235 struct got_tree_entry *te;
4236 te = s->first_displayed_entry;
4239 te = SIMPLEQ_NEXT(te, entry);
4241 s->selected = ndisplayed - 1;
4247 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4248 struct tog_parent_tree *parent;
4249 /* user selected '..' */
4250 if (s->tree == s->root)
4252 parent = TAILQ_FIRST(&s->parents);
4253 TAILQ_REMOVE(&s->parents, parent,
4255 got_object_tree_close(s->tree);
4256 s->tree = parent->tree;
4258 got_object_tree_get_entries(s->tree);
4259 s->first_displayed_entry =
4260 parent->first_displayed_entry;
4262 parent->selected_entry;
4263 s->selected = parent->selected;
4265 } else if (S_ISDIR(s->selected_entry->mode)) {
4266 struct got_tree_object *subtree;
4267 err = got_object_open_as_tree(&subtree,
4268 s->repo, s->selected_entry->id);
4271 err = tree_view_visit_subtree(subtree, s);
4273 got_object_tree_close(subtree);
4276 } else if (S_ISREG(s->selected_entry->mode)) {
4277 struct tog_view *blame_view;
4278 int begin_x = view_is_parent_view(view) ?
4279 view_split_begin_x(view->begin_x) : 0;
4281 err = blame_tree_entry(&blame_view, begin_x,
4282 s->selected_entry, &s->parents,
4283 s->commit_id, s->refs, s->repo);
4286 if (view_is_parent_view(view)) {
4287 err = view_close_child(view);
4290 err = view_set_child(view, blame_view);
4292 view_close(blame_view);
4295 *focus_view = blame_view;
4296 view->child_focussed = 1;
4298 *new_view = blame_view;
4302 if (s->selected > view->nlines)
4303 s->selected = s->ndisplayed - 1;
4316 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4321 static const struct got_error *
4322 cmd_tree(int argc, char *argv[])
4324 const struct got_error *error;
4325 struct got_repository *repo = NULL;
4326 struct got_reflist_head refs;
4327 char *repo_path = NULL;
4328 struct got_object_id *commit_id = NULL;
4329 char *commit_id_arg = NULL;
4330 struct got_commit_object *commit = NULL;
4331 struct got_tree_object *tree = NULL;
4333 struct tog_view *view;
4335 SIMPLEQ_INIT(&refs);
4338 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4343 while ((ch = getopt(argc, argv, "c:")) != -1) {
4346 commit_id_arg = optarg;
4358 struct got_worktree *worktree;
4359 char *cwd = getcwd(NULL, 0);
4361 return got_error_from_errno("getcwd");
4362 error = got_worktree_open(&worktree, cwd);
4363 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4368 strdup(got_worktree_get_repo_path(worktree));
4369 got_worktree_close(worktree);
4372 if (repo_path == NULL) {
4373 error = got_error_from_errno("strdup");
4376 } else if (argc == 1) {
4377 repo_path = realpath(argv[0], NULL);
4378 if (repo_path == NULL)
4379 return got_error_from_errno2("realpath", argv[0]);
4385 error = got_repo_open(&repo, repo_path);
4389 error = apply_unveil(got_repo_get_path(repo), NULL);
4393 if (commit_id_arg == NULL)
4394 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4396 error = got_object_resolve_id_str(&commit_id, repo,
4401 error = got_object_open_as_commit(&commit, repo, commit_id);
4405 error = got_object_open_as_tree(&tree, repo,
4406 got_object_commit_get_tree_id(commit));
4410 error = got_ref_list(&refs, repo);
4414 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4416 error = got_error_from_errno("view_open");
4419 error = open_tree_view(view, tree, commit_id, &refs, repo);
4422 error = view_loop(view);
4427 got_object_commit_close(commit);
4429 got_object_tree_close(tree);
4431 got_repo_close(repo);
4432 got_ref_list_free(&refs);
4439 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4444 make_argv(const char *arg0, const char *arg1)
4447 int argc = (arg1 == NULL ? 1 : 2);
4449 argv = calloc(argc, sizeof(char *));
4452 argv[0] = strdup(arg0);
4453 if (argv[0] == NULL)
4456 argv[1] = strdup(arg1);
4457 if (argv[1] == NULL)
4465 main(int argc, char *argv[])
4467 const struct got_error *error = NULL;
4468 struct tog_cmd *cmd = NULL;
4470 char **cmd_argv = NULL;
4472 setlocale(LC_CTYPE, "");
4474 while ((ch = getopt(argc, argv, "h")) != -1) {
4493 /* Build an argument vector which runs a default command. */
4494 cmd = &tog_commands[0];
4495 cmd_argv = make_argv(cmd->name, NULL);
4500 /* Did the user specific a command? */
4501 for (i = 0; i < nitems(tog_commands); i++) {
4502 if (strncmp(tog_commands[i].name, argv[0],
4503 strlen(argv[0])) == 0) {
4504 cmd = &tog_commands[i];
4506 tog_commands[i].cmd_usage();
4511 /* Did the user specify a repository? */
4512 char *repo_path = realpath(argv[0], NULL);
4514 struct got_repository *repo;
4515 error = got_repo_open(&repo, repo_path);
4517 got_repo_close(repo);
4519 error = got_error_from_errno2("realpath",
4523 fprintf(stderr, "%s: '%s' is not a "
4524 "known command\n", getprogname(),
4528 fprintf(stderr, "%s: '%s' is neither a known "
4529 "command nor a path to a repository\n",
4530 getprogname(), argv[0]);
4534 cmd = &tog_commands[0];
4535 cmd_argv = make_argv(cmd->name, repo_path);
4541 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4548 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);