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
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_worktree.h"
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
72 __dead static void usage(void);
73 __dead static void usage_log(void);
74 __dead static void usage_diff(void);
75 __dead static void usage_blame(void);
76 __dead static void usage_tree(void);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
83 static struct tog_cmd tog_commands[] = {
84 { "log", cmd_log, usage_log,
85 "show repository history" },
86 { "diff", cmd_diff, usage_diff,
87 "compare files and directories" },
88 { "blame", cmd_blame, usage_blame,
89 "show line-by-line file history" },
90 { "tree", cmd_tree, usage_tree,
91 "browse trees in repository" },
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;
122 /* passed from log view; may be NULL */
123 struct commit_queue_entry *selected_entry;
124 struct commit_queue *commits;
126 pthread_cond_t *need_commits;
130 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
132 struct tog_log_thread_args {
133 pthread_cond_t need_commits;
135 struct got_commit_graph *graph;
136 struct commit_queue *commits;
137 const char *in_repo_path;
138 struct got_object_id *start_id;
139 struct got_repository *repo;
142 struct tog_view *view;
143 struct commit_queue_entry **first_displayed_entry;
144 struct commit_queue_entry **selected_entry;
147 struct tog_log_view_state {
148 struct commit_queue commits;
149 struct commit_queue_entry *first_displayed_entry;
150 struct commit_queue_entry *last_displayed_entry;
151 struct commit_queue_entry *selected_entry;
154 struct got_repository *repo;
155 struct got_object_id *start_id;
158 struct tog_log_thread_args thread_args;
161 struct tog_blame_cb_args {
162 struct tog_blame_line *lines; /* one per line */
165 struct tog_view *view;
166 struct got_object_id *commit_id;
170 struct tog_blame_thread_args {
172 struct got_repository *repo;
173 struct tog_blame_cb_args *cb_args;
180 struct tog_blame_line *lines;
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_object_id *commit_id;
200 struct tog_blame blame;
203 struct tog_parent_tree {
204 TAILQ_ENTRY(tog_parent_tree) entry;
205 struct got_tree_object *tree;
206 struct got_tree_entry *first_displayed_entry;
207 struct got_tree_entry *selected_entry;
211 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
213 struct tog_tree_view_state {
215 struct got_tree_object *root;
216 struct got_tree_object *tree;
217 const struct got_tree_entries *entries;
218 struct got_tree_entry *first_displayed_entry;
219 struct got_tree_entry *last_displayed_entry;
220 struct got_tree_entry *selected_entry;
221 int ndisplayed, selected, show_ids;
222 struct tog_parent_trees parents;
223 struct got_object_id *commit_id;
224 struct got_repository *repo;
228 * We implement two types of views: parent views and child views.
230 * The 'Tab' key switches between a parent view and its child view.
231 * Child views are shown side-by-side to their parent view, provided
232 * there is enough screen estate.
234 * When a new view is opened from within a parent view, this new view
235 * becomes a child view of the parent view, replacing any existing child.
237 * When a new view is opened from within a child view, this new view
238 * becomes a parent view which will obscure the views below until the
239 * user quits the new parent view by typing 'q'.
241 * This list of views contains parent views only.
242 * Child views are only pointed to by their parent view.
244 TAILQ_HEAD(tog_view_list_head, tog_view);
247 TAILQ_ENTRY(tog_view) entry;
250 int nlines, ncols, begin_y, begin_x;
251 int lines, cols; /* copies of LINES and COLS */
253 struct tog_view *parent;
254 struct tog_view *child;
257 /* type-specific state */
258 enum tog_view_type type;
260 struct tog_diff_view_state diff;
261 struct tog_log_view_state log;
262 struct tog_blame_view_state blame;
263 struct tog_tree_view_state tree;
266 const struct got_error *(*show)(struct tog_view *);
267 const struct got_error *(*input)(struct tog_view **,
268 struct tog_view **, struct tog_view**, struct tog_view *, int);
269 const struct got_error *(*close)(struct tog_view *);
272 static const struct got_error *open_diff_view(struct tog_view *,
273 struct got_object_id *, struct got_object_id *, struct commit_queue_entry *,
274 struct commit_queue *, int *, pthread_cond_t *, int *,
275 struct got_repository *);
276 static const struct got_error *show_diff_view(struct tog_view *);
277 static const struct got_error *input_diff_view(struct tog_view **,
278 struct tog_view **, struct tog_view **, struct tog_view *, int);
279 static const struct got_error* close_diff_view(struct tog_view *);
281 static const struct got_error *open_log_view(struct tog_view *,
282 struct got_object_id *, struct got_repository *, const char *, int);
283 static const struct got_error * show_log_view(struct tog_view *);
284 static const struct got_error *input_log_view(struct tog_view **,
285 struct tog_view **, struct tog_view **, struct tog_view *, int);
286 static const struct got_error *close_log_view(struct tog_view *);
288 static const struct got_error *open_blame_view(struct tog_view *, char *,
289 struct got_object_id *, struct got_repository *);
290 static const struct got_error *show_blame_view(struct tog_view *);
291 static const struct got_error *input_blame_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error *close_blame_view(struct tog_view *);
295 static const struct got_error *open_tree_view(struct tog_view *,
296 struct got_tree_object *, struct got_object_id *, struct got_repository *);
297 static const struct got_error *show_tree_view(struct tog_view *);
298 static const struct got_error *input_tree_view(struct tog_view **,
299 struct tog_view **, struct tog_view **, struct tog_view *, int);
300 static const struct got_error *close_tree_view(struct tog_view *);
302 static volatile sig_atomic_t tog_sigwinch_received;
305 tog_sigwinch(int signo)
307 tog_sigwinch_received = 1;
310 static const struct got_error *
311 view_close(struct tog_view *view)
313 const struct got_error *err = NULL;
316 view_close(view->child);
320 err = view->close(view);
322 del_panel(view->panel);
324 delwin(view->window);
329 static struct tog_view *
330 view_open(int nlines, int ncols, int begin_y, int begin_x,
331 enum tog_view_type type)
333 struct tog_view *view = calloc(1, sizeof(*view));
341 view->nlines = nlines ? nlines : LINES - begin_y;
342 view->ncols = ncols ? ncols : COLS - begin_x;
343 view->begin_y = begin_y;
344 view->begin_x = begin_x;
345 view->window = newwin(nlines, ncols, begin_y, begin_x);
346 if (view->window == NULL) {
350 view->panel = new_panel(view->window);
351 if (view->panel == NULL ||
352 set_panel_userptr(view->panel, view) != OK) {
357 keypad(view->window, TRUE);
362 view_split_begin_x(int begin_x)
364 if (begin_x > 0 || COLS < 120)
366 return (COLS - MAX(COLS / 2, 80));
369 static const struct got_error *view_resize(struct tog_view *);
371 static const struct got_error *
372 view_splitscreen(struct tog_view *view)
374 const struct got_error *err = NULL;
377 view->begin_x = view_split_begin_x(0);
378 view->nlines = LINES;
379 view->ncols = COLS - view->begin_x;
382 err = view_resize(view);
386 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
387 return got_error_from_errno();
392 static const struct got_error *
393 view_fullscreen(struct tog_view *view)
395 const struct got_error *err = NULL;
399 view->nlines = LINES;
403 err = view_resize(view);
407 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 return got_error_from_errno();
414 view_is_parent_view(struct tog_view *view)
416 return view->parent == NULL;
419 static const struct got_error *
420 view_resize(struct tog_view *view)
424 if (view->lines > LINES)
425 nlines = view->nlines - (view->lines - LINES);
427 nlines = view->nlines + (LINES - view->lines);
429 if (view->cols > COLS)
430 ncols = view->ncols - (view->cols - COLS);
432 ncols = view->ncols + (COLS - view->cols);
434 if (wresize(view->window, nlines, ncols) == ERR)
435 return got_error_from_errno();
436 if (replace_panel(view->panel, view->window) == ERR)
437 return got_error_from_errno();
438 wclear(view->window);
440 view->nlines = nlines;
446 view->child->begin_x = view_split_begin_x(view->begin_x);
447 if (view->child->begin_x == 0) {
448 view_fullscreen(view->child);
449 if (view->child->focussed)
450 show_panel(view->child->panel);
452 show_panel(view->panel);
454 view_splitscreen(view->child);
455 show_panel(view->child->panel);
462 static const struct got_error *
463 view_close_child(struct tog_view *view)
465 const struct got_error *err = NULL;
467 if (view->child == NULL)
470 err = view_close(view->child);
475 static const struct got_error *
476 view_set_child(struct tog_view *view, struct tog_view *child)
478 const struct got_error *err = NULL;
481 child->parent = view;
486 view_is_splitscreen(struct tog_view *view)
488 return !view_is_parent_view(view) && view->begin_x > 0;
497 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
498 cols = 80; /* Default */
504 resize_term(lines, cols);
507 static const struct got_error *
508 view_input(struct tog_view **new, struct tog_view **dead,
509 struct tog_view **focus, int *done, struct tog_view *view,
510 struct tog_view_list_head *views)
512 const struct got_error *err = NULL;
520 nodelay(stdscr, FALSE);
521 /* Allow threads to make progress while we are waiting for input. */
522 errcode = pthread_mutex_unlock(&tog_mutex);
524 return got_error_set_errno(errcode);
525 ch = wgetch(view->window);
526 errcode = pthread_mutex_lock(&tog_mutex);
528 return got_error_set_errno(errcode);
529 nodelay(stdscr, TRUE);
531 if (tog_sigwinch_received) {
533 tog_sigwinch_received = 0;
534 TAILQ_FOREACH(v, views, entry) {
535 err = view_resize(v);
538 err = v->input(new, dead, focus, v, KEY_RESIZE);
549 *focus = view->child;
550 view->child_focussed = 1;
551 } else if (view->parent) {
552 *focus = view->parent;
553 view->parent->child_focussed = 0;
557 err = view->input(new, dead, focus, view, ch);
564 if (view_is_parent_view(view)) {
565 if (view->child == NULL)
567 if (view_is_splitscreen(view->child)) {
568 *focus = view->child;
569 view->child_focussed = 1;
570 err = view_fullscreen(view->child);
572 err = view_splitscreen(view->child);
575 err = view->child->input(new, dead, focus,
576 view->child, KEY_RESIZE);
578 if (view_is_splitscreen(view)) {
580 view->parent->child_focussed = 1;
581 err = view_fullscreen(view);
583 err = view_splitscreen(view);
587 err = view->input(new, dead, focus, view,
594 err = view->input(new, dead, focus, view, ch);
602 view_vborder(struct tog_view *view)
605 struct tog_view *view_above;
608 return view_vborder(view->parent);
610 panel = panel_above(view->panel);
614 view_above = panel_userptr(panel);
615 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
616 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
620 view_needs_focus_indication(struct tog_view *view)
622 if (view_is_parent_view(view)) {
623 if (view->child == NULL || view->child_focussed)
625 if (!view_is_splitscreen(view->child))
627 } else if (!view_is_splitscreen(view))
630 return view->focussed;
633 static const struct got_error *
634 view_loop(struct tog_view *view)
636 const struct got_error *err = NULL;
637 struct tog_view_list_head views;
638 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
639 int fast_refresh = 10;
640 int done = 0, errcode;
642 errcode = pthread_mutex_lock(&tog_mutex);
644 return got_error_set_errno(errcode);
647 TAILQ_INSERT_HEAD(&views, view, entry);
651 err = view->show(view);
656 while (!TAILQ_EMPTY(&views) && !done) {
657 /* Refresh fast during initialization, then become slower. */
658 if (fast_refresh && fast_refresh-- == 0)
659 halfdelay(10); /* switch to once per second */
661 err = view_input(&new_view, &dead_view, &focus_view, &done,
666 struct tog_view *prev = NULL;
668 if (view_is_parent_view(dead_view))
669 prev = TAILQ_PREV(dead_view,
670 tog_view_list_head, entry);
671 else if (view->parent != dead_view)
674 if (dead_view->parent)
675 dead_view->parent->child = NULL;
677 TAILQ_REMOVE(&views, dead_view, entry);
679 err = view_close(dead_view);
680 if (err || dead_view == main_view)
683 if (view == dead_view) {
688 else if (!TAILQ_EMPTY(&views))
689 view = TAILQ_LAST(&views,
694 if (view->child && view->child_focussed)
695 focus_view = view->child;
702 struct tog_view *v, *t;
703 /* Only allow one parent view per type. */
704 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
705 if (v->type != new_view->type)
707 TAILQ_REMOVE(&views, v, entry);
713 TAILQ_INSERT_TAIL(&views, new_view, entry);
715 if (focus_view == NULL)
716 focus_view = new_view;
719 show_panel(focus_view->panel);
722 focus_view->focussed = 1;
725 show_panel(new_view->panel);
726 if (view->child && view_is_splitscreen(view->child))
727 show_panel(view->child->panel);
730 if (focus_view == NULL) {
732 show_panel(view->panel);
733 if (view->child && view_is_splitscreen(view->child))
734 show_panel(view->child->panel);
738 err = view->parent->show(view->parent);
742 err = view->show(view);
746 err = view->child->show(view->child);
755 while (!TAILQ_EMPTY(&views)) {
756 view = TAILQ_FIRST(&views);
757 TAILQ_REMOVE(&views, view, entry);
761 errcode = pthread_mutex_unlock(&tog_mutex);
763 return got_error_set_errno(errcode);
773 "usage: %s log [-c commit] [-r repository-path] [path]\n",
778 /* Create newly allocated wide-character string equivalent to a byte string. */
779 static const struct got_error *
780 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
783 const struct got_error *err = NULL;
786 *wlen = mbstowcs(NULL, s, 0);
787 if (*wlen == (size_t)-1) {
790 return got_error_from_errno();
792 /* byte string invalid in current encoding; try to "fix" it */
793 err = got_mbsavis(&vis, &vislen, s);
796 *wlen = mbstowcs(NULL, vis, 0);
797 if (*wlen == (size_t)-1) {
798 err = got_error_from_errno(); /* give up */
803 *ws = calloc(*wlen + 1, sizeof(*ws));
805 err = got_error_from_errno();
809 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
810 err = got_error_from_errno();
821 /* Format a line for display, ensuring that it won't overflow a width limit. */
822 static const struct got_error *
823 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
825 const struct got_error *err = NULL;
827 wchar_t *wline = NULL;
834 err = mbs2ws(&wline, &wlen, line);
839 while (i < wlen && cols < wlimit) {
840 int width = wcwidth(wline[i]);
847 if (cols + width <= wlimit)
852 if (wline[i] == L'\t')
853 cols += TABSIZE - ((cols + 1) % TABSIZE);
857 err = got_error_from_errno();
872 static const struct got_error *
873 draw_commit(struct tog_view *view, struct got_commit_object *commit,
874 struct got_object_id *id)
876 const struct got_error *err = NULL;
877 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
878 char *logmsg0 = NULL, *logmsg = NULL;
879 char *author0 = NULL, *author = NULL;
880 wchar_t *wlogmsg = NULL, *wauthor = NULL;
881 int author_width, logmsg_width;
882 char *newline, *smallerthan;
885 static const size_t date_display_cols = 9;
886 static const size_t author_display_cols = 16;
887 const int avail = view->ncols;
889 time_t committer_time;
891 committer_time = got_object_commit_get_committer_time(commit);
892 if (localtime_r(&committer_time, &tm) == NULL)
893 return got_error_from_errno();
894 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
896 return got_error(GOT_ERR_NO_SPACE);
898 if (avail < date_display_cols)
899 limit = MIN(sizeof(datebuf) - 1, avail);
901 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
902 waddnstr(view->window, datebuf, limit);
907 author0 = strdup(got_object_commit_get_author(commit));
908 if (author0 == NULL) {
909 err = got_error_from_errno();
913 smallerthan = strchr(author, '<');
917 char *at = strchr(author, '@');
922 err = format_line(&wauthor, &author_width, author, limit);
925 waddwstr(view->window, wauthor);
927 while (col <= avail && author_width < author_display_cols + 1) {
928 waddch(view->window, ' ');
935 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
936 if (logmsg0 == NULL) {
937 err = got_error_from_errno();
941 while (*logmsg == '\n')
943 newline = strchr(logmsg, '\n');
947 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
950 waddwstr(view->window, wlogmsg);
952 while (col <= avail) {
953 waddch(view->window, ' ');
965 static struct commit_queue_entry *
966 alloc_commit_queue_entry(struct got_commit_object *commit,
967 struct got_object_id *id)
969 struct commit_queue_entry *entry;
971 entry = calloc(1, sizeof(*entry));
976 entry->commit = commit;
981 pop_commit(struct commit_queue *commits)
983 struct commit_queue_entry *entry;
985 entry = TAILQ_FIRST(&commits->head);
986 TAILQ_REMOVE(&commits->head, entry, entry);
987 got_object_commit_close(entry->commit);
989 /* Don't free entry->id! It is owned by the commit graph. */
994 free_commits(struct commit_queue *commits)
996 while (!TAILQ_EMPTY(&commits->head))
1000 static const struct got_error *
1001 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1002 int minqueue, struct got_repository *repo, const char *path)
1004 const struct got_error *err = NULL;
1008 * We keep all commits open throughout the lifetime of the log
1009 * view in order to avoid having to re-fetch commits from disk
1010 * while updating the display.
1012 while (nqueued < minqueue) {
1013 struct got_object_id *id;
1014 struct got_commit_object *commit;
1015 struct commit_queue_entry *entry;
1018 err = got_commit_graph_iter_next(&id, graph);
1020 if (err->code != GOT_ERR_ITER_NEED_MORE)
1022 err = got_commit_graph_fetch_commits(graph,
1032 err = got_object_open_as_commit(&commit, repo, id);
1035 entry = alloc_commit_queue_entry(commit, id);
1036 if (entry == NULL) {
1037 err = got_error_from_errno();
1041 errcode = pthread_mutex_lock(&tog_mutex);
1043 err = got_error_set_errno(errcode);
1047 entry->idx = commits->ncommits;
1048 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1050 commits->ncommits++;
1052 errcode = pthread_mutex_unlock(&tog_mutex);
1053 if (errcode && err == NULL)
1054 err = got_error_set_errno(errcode);
1060 static const struct got_error *
1061 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1063 const struct got_error *err = NULL;
1064 struct got_reference *head_ref;
1068 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1072 err = got_ref_resolve(head_id, repo, head_ref);
1073 got_ref_close(head_ref);
1082 static const struct got_error *
1083 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1084 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1085 struct commit_queue *commits, int selected_idx, int limit,
1086 const char *path, int commits_needed)
1088 const struct got_error *err = NULL;
1089 struct commit_queue_entry *entry;
1090 int ncommits, width;
1091 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1097 if (ncommits == selected_idx) {
1101 entry = TAILQ_NEXT(entry, entry);
1106 err = got_object_id_str(&id_str, (*selected)->id);
1111 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1112 entry ? entry->idx + 1 : 0, commits->ncommits,
1113 commits_needed == 0 ? "" : " loading...") == -1)
1114 return got_error_from_errno();
1116 if (path && strcmp(path, "/") != 0) {
1117 if (asprintf(&header, "commit %s %s%s",
1118 id_str ? id_str : "........................................",
1119 path, ncommits_str) == -1) {
1120 err = got_error_from_errno();
1124 } else if (asprintf(&header, "commit %s%s",
1125 id_str ? id_str : "........................................",
1126 ncommits_str) == -1) {
1127 err = got_error_from_errno();
1131 err = format_line(&wline, &width, header, view->ncols);
1135 werase(view->window);
1137 if (view_needs_focus_indication(view))
1138 wstandout(view->window);
1139 waddwstr(view->window, wline);
1140 while (width < view->ncols) {
1141 waddch(view->window, ' ');
1144 if (view_needs_focus_indication(view))
1145 wstandend(view->window);
1154 if (ncommits >= limit - 1)
1156 if (view->focussed && ncommits == selected_idx)
1157 wstandout(view->window);
1158 err = draw_commit(view, entry->commit, entry->id);
1159 if (view->focussed && ncommits == selected_idx)
1160 wstandend(view->window);
1165 entry = TAILQ_NEXT(entry, entry);
1177 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1178 struct commit_queue *commits)
1180 struct commit_queue_entry *entry;
1183 entry = TAILQ_FIRST(&commits->head);
1184 if (*first_displayed_entry == entry)
1187 entry = *first_displayed_entry;
1188 while (entry && nscrolled < maxscroll) {
1189 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1191 *first_displayed_entry = entry;
1197 static const struct got_error *
1198 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1199 struct commit_queue_entry **last_displayed_entry,
1200 struct commit_queue *commits, int *log_complete, int *commits_needed,
1201 pthread_cond_t *need_commits)
1203 const struct got_error *err = NULL;
1204 struct commit_queue_entry *pentry;
1207 if (*last_displayed_entry == NULL)
1211 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1212 if (pentry == NULL) {
1216 *commits_needed = maxscroll + 20;
1217 errcode = pthread_cond_signal(need_commits);
1219 return got_error_set_errno(errcode);
1222 *last_displayed_entry = pentry;
1224 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1227 *first_displayed_entry = pentry;
1228 } while (++nscrolled < maxscroll);
1233 static const struct got_error *
1234 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1235 struct commit_queue_entry *selected_entry,
1236 struct commit_queue *commits, int *log_complete,
1237 pthread_cond_t *need_commits, int *commits_needed,
1238 struct got_repository *repo)
1240 const struct got_error *err;
1241 struct got_object_qid *parent_id;
1242 struct tog_view *diff_view;
1243 struct got_commit_object *commit = selected_entry->commit;
1244 struct got_object_id *commit_id = selected_entry->id;
1246 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1247 if (diff_view == NULL)
1248 return got_error_from_errno();
1250 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1251 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1252 commit_id, selected_entry, commits, log_complete, need_commits,
1253 commits_needed, repo);
1255 *new_view = diff_view;
1259 static const struct got_error *
1260 browse_commit(struct tog_view **new_view, int begin_x,
1261 struct commit_queue_entry *entry, struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 struct got_tree_object *tree;
1265 struct tog_view *tree_view;
1267 err = got_object_open_as_tree(&tree, repo,
1268 got_object_commit_get_tree_id(entry->commit));
1272 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1273 if (tree_view == NULL)
1274 return got_error_from_errno();
1276 err = open_tree_view(tree_view, tree, entry->id, repo);
1278 got_object_tree_close(tree);
1280 *new_view = tree_view;
1285 log_thread(void *arg)
1287 const struct got_error *err = NULL;
1289 struct tog_log_thread_args *a = arg;
1292 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1296 while (!done && !err) {
1297 err = queue_commits(a->graph, a->commits, 1, a->repo,
1300 if (err->code != GOT_ERR_ITER_COMPLETED)
1304 } else if (a->commits_needed > 0)
1305 a->commits_needed--;
1307 errcode = pthread_mutex_lock(&tog_mutex);
1309 return (void *)got_error_set_errno(errcode);
1312 a->log_complete = 1;
1313 else if (*a->quit) {
1315 a->log_complete = 1;
1316 } else if (*a->first_displayed_entry == NULL) {
1317 *a->first_displayed_entry =
1318 TAILQ_FIRST(&a->commits->head);
1319 *a->selected_entry = *a->first_displayed_entry;
1323 a->commits_needed = 0;
1324 else if (a->commits_needed == 0) {
1325 errcode = pthread_cond_wait(&a->need_commits,
1328 err = got_error_set_errno(errcode);
1331 errcode = pthread_mutex_unlock(&tog_mutex);
1332 if (errcode && err == NULL)
1333 err = got_error_set_errno(errcode);
1338 static const struct got_error *
1339 stop_log_thread(struct tog_log_view_state *s)
1341 const struct got_error *err = NULL;
1346 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1348 return got_error_set_errno(errcode);
1349 errcode = pthread_mutex_unlock(&tog_mutex);
1351 return got_error_set_errno(errcode);
1352 errcode = pthread_join(s->thread, (void **)&err);
1354 return got_error_set_errno(errcode);
1355 errcode = pthread_mutex_lock(&tog_mutex);
1357 return got_error_set_errno(errcode);
1361 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1362 if (errcode && err == NULL)
1363 err = got_error_set_errno(errcode);
1365 if (s->thread_args.repo) {
1366 got_repo_close(s->thread_args.repo);
1367 s->thread_args.repo = NULL;
1370 if (s->thread_args.graph) {
1371 got_commit_graph_close(s->thread_args.graph);
1372 s->thread_args.graph = NULL;
1378 static const struct got_error *
1379 close_log_view(struct tog_view *view)
1381 const struct got_error *err = NULL;
1382 struct tog_log_view_state *s = &view->state.log;
1384 err = stop_log_thread(s);
1385 free_commits(&s->commits);
1386 free(s->in_repo_path);
1387 s->in_repo_path = NULL;
1393 static const struct got_error *
1394 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1395 struct got_repository *repo, const char *path, int check_disk)
1397 const struct got_error *err = NULL;
1398 struct tog_log_view_state *s = &view->state.log;
1399 struct got_repository *thread_repo = NULL;
1400 struct got_commit_graph *thread_graph = NULL;
1403 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1407 /* The commit queue only contains commits being displayed. */
1408 TAILQ_INIT(&s->commits.head);
1409 s->commits.ncommits = 0;
1412 s->start_id = got_object_id_dup(start_id);
1413 if (s->start_id == NULL) {
1414 err = got_error_from_errno();
1418 view->show = show_log_view;
1419 view->input = input_log_view;
1420 view->close = close_log_view;
1422 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1425 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1430 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1432 err = got_error_set_errno(errcode);
1436 s->thread_args.commits_needed = view->nlines;
1437 s->thread_args.graph = thread_graph;
1438 s->thread_args.commits = &s->commits;
1439 s->thread_args.in_repo_path = s->in_repo_path;
1440 s->thread_args.start_id = s->start_id;
1441 s->thread_args.repo = thread_repo;
1442 s->thread_args.log_complete = 0;
1443 s->thread_args.quit = &s->quit;
1444 s->thread_args.view = view;
1445 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1446 s->thread_args.selected_entry = &s->selected_entry;
1449 close_log_view(view);
1453 static const struct got_error *
1454 show_log_view(struct tog_view *view)
1456 struct tog_log_view_state *s = &view->state.log;
1458 if (s->thread == NULL) {
1459 int errcode = pthread_create(&s->thread, NULL, log_thread,
1462 return got_error_set_errno(errcode);
1465 return draw_commits(view, &s->last_displayed_entry,
1466 &s->selected_entry, s->first_displayed_entry,
1467 &s->commits, s->selected, view->nlines,
1468 s->in_repo_path, s->thread_args.commits_needed);
1471 static const struct got_error *
1472 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1473 struct tog_view **focus_view, struct tog_view *view, int ch)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1478 struct tog_view *diff_view = NULL, *tree_view = NULL;
1487 if (s->first_displayed_entry == NULL)
1489 if (s->selected > 0)
1491 if (s->selected > 0)
1493 scroll_up(&s->first_displayed_entry, 1,
1497 if (s->first_displayed_entry == NULL)
1499 if (TAILQ_FIRST(&s->commits.head) ==
1500 s->first_displayed_entry) {
1504 scroll_up(&s->first_displayed_entry,
1505 view->nlines, &s->commits);
1509 if (s->first_displayed_entry == NULL)
1511 if (s->selected < MIN(view->nlines - 2,
1512 s->commits.ncommits - 1)) {
1516 err = scroll_down(&s->first_displayed_entry, 1,
1517 &s->last_displayed_entry, &s->commits,
1518 &s->thread_args.log_complete,
1519 &s->thread_args.commits_needed,
1520 &s->thread_args.need_commits);
1523 struct commit_queue_entry *first;
1524 first = s->first_displayed_entry;
1527 err = scroll_down(&s->first_displayed_entry,
1528 view->nlines, &s->last_displayed_entry,
1529 &s->commits, &s->thread_args.log_complete,
1530 &s->thread_args.commits_needed,
1531 &s->thread_args.need_commits);
1532 if (first == s->first_displayed_entry &&
1533 s->selected < MIN(view->nlines - 2,
1534 s->commits.ncommits - 1)) {
1535 /* can't scroll further down */
1536 s->selected = MIN(view->nlines - 2,
1537 s->commits.ncommits - 1);
1543 if (s->selected > view->nlines - 2)
1544 s->selected = view->nlines - 2;
1545 if (s->selected > s->commits.ncommits - 1)
1546 s->selected = s->commits.ncommits - 1;
1550 if (s->selected_entry == NULL)
1552 if (view_is_parent_view(view))
1553 begin_x = view_split_begin_x(view->begin_x);
1554 err = open_diff_view_for_commit(&diff_view, begin_x,
1555 s->selected_entry, &s->commits,
1556 &s->thread_args.log_complete,
1557 &s->thread_args.need_commits,
1558 &s->thread_args.commits_needed, s->repo);
1561 if (view_is_parent_view(view)) {
1562 err = view_close_child(view);
1565 err = view_set_child(view, diff_view);
1567 view_close(diff_view);
1570 *focus_view = diff_view;
1571 view->child_focussed = 1;
1573 *new_view = diff_view;
1576 if (s->selected_entry == NULL)
1578 if (view_is_parent_view(view))
1579 begin_x = view_split_begin_x(view->begin_x);
1580 err = browse_commit(&tree_view, begin_x,
1581 s->selected_entry, s->repo);
1584 if (view_is_parent_view(view)) {
1585 err = view_close_child(view);
1588 err = view_set_child(view, tree_view);
1590 view_close(tree_view);
1593 *focus_view = tree_view;
1594 view->child_focussed = 1;
1596 *new_view = tree_view;
1599 if (strcmp(s->in_repo_path, "/") == 0)
1601 parent_path = dirname(s->in_repo_path);
1602 if (parent_path && strcmp(parent_path, ".") != 0) {
1603 struct tog_view *lv;
1604 err = stop_log_thread(s);
1607 lv = view_open(view->nlines, view->ncols,
1608 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1610 return got_error_from_errno();
1611 err = open_log_view(lv, s->start_id, s->repo,
1615 if (view_is_parent_view(view))
1618 view_set_child(view->parent, lv);
1631 static const struct got_error *
1632 apply_unveil(const char *repo_path, const char *worktree_path)
1634 const struct got_error *error;
1636 if (repo_path && unveil(repo_path, "r") != 0)
1637 return got_error_from_errno();
1639 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1640 return got_error_from_errno();
1642 if (unveil("/tmp", "rwc") != 0)
1643 return got_error_from_errno();
1645 error = got_privsep_unveil_exec_helpers();
1649 if (unveil(NULL, NULL) != 0)
1650 return got_error_from_errno();
1660 halfdelay(1); /* Do fast refresh while initial view is loading. */
1663 intrflush(stdscr, FALSE);
1664 keypad(stdscr, TRUE);
1666 signal(SIGWINCH, tog_sigwinch);
1669 static const struct got_error *
1670 cmd_log(int argc, char *argv[])
1672 const struct got_error *error;
1673 struct got_repository *repo = NULL;
1674 struct got_object_id *start_id = NULL;
1675 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1676 char *start_commit = NULL;
1678 struct tog_view *view;
1681 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1686 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1689 start_commit = optarg;
1692 repo_path = realpath(optarg, NULL);
1693 if (repo_path == NULL)
1694 err(1, "-r option");
1708 path = strdup(argv[0]);
1712 return got_error_from_errno();
1714 cwd = getcwd(NULL, 0);
1716 error = got_error_from_errno();
1719 if (repo_path == NULL) {
1720 struct got_worktree *worktree;
1721 error = got_worktree_open(&worktree, cwd);
1722 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1726 strdup(got_worktree_get_repo_path(worktree));
1727 got_worktree_close(worktree);
1729 repo_path = strdup(cwd);
1730 if (repo_path == NULL) {
1731 error = got_error_from_errno();
1738 error = apply_unveil(repo_path, NULL);
1742 error = got_repo_open(&repo, repo_path);
1746 if (start_commit == NULL)
1747 error = get_head_commit_id(&start_id, repo);
1749 error = got_object_resolve_id_str(&start_id, repo,
1754 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1756 error = got_error_from_errno();
1759 error = open_log_view(view, start_id, repo, path, 1);
1762 error = view_loop(view);
1769 got_repo_close(repo);
1777 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1783 parse_next_line(FILE *f, size_t *len)
1788 const char delim[3] = { '\0', '\0', '\0'};
1790 line = fparseln(f, &linelen, &lineno, delim, 0);
1796 static const struct got_error *
1797 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1798 int *last_displayed_line, int *eof, int max_lines,
1801 const struct got_error *err;
1802 int nlines = 0, nprinted = 0;
1809 werase(view->window);
1812 err = format_line(&wline, &width, header, view->ncols);
1817 if (view_needs_focus_indication(view))
1818 wstandout(view->window);
1819 waddwstr(view->window, wline);
1820 if (view_needs_focus_indication(view))
1821 wstandend(view->window);
1822 if (width < view->ncols)
1823 waddch(view->window, '\n');
1831 while (nprinted < max_lines) {
1832 line = parse_next_line(f, &len);
1837 if (++nlines < *first_displayed_line) {
1842 err = format_line(&wline, &width, line, view->ncols);
1847 waddwstr(view->window, wline);
1848 if (width < view->ncols)
1849 waddch(view->window, '\n');
1850 if (++nprinted == 1)
1851 *first_displayed_line = nlines;
1856 *last_displayed_line = nlines;
1864 get_datestr(time_t *time, char *datebuf)
1866 char *p, *s = ctime_r(time, datebuf);
1867 p = strchr(s, '\n');
1873 static const struct got_error *
1874 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1877 const struct got_error *err = NULL;
1879 struct got_commit_object *commit;
1880 char *id_str = NULL;
1881 time_t committer_time;
1882 const char *author, *committer;
1884 err = got_object_open_as_commit(&commit, repo, commit_id);
1888 err = got_object_id_str(&id_str, commit_id);
1890 err = got_error_from_errno();
1894 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1895 err = got_error_from_errno();
1898 if (fprintf(outfile, "from: %s\n",
1899 got_object_commit_get_author(commit)) < 0) {
1900 err = got_error_from_errno();
1903 committer_time = got_object_commit_get_committer_time(commit);
1904 if (fprintf(outfile, "date: %s UTC\n",
1905 get_datestr(&committer_time, datebuf)) < 0) {
1906 err = got_error_from_errno();
1909 author = got_object_commit_get_author(commit);
1910 committer = got_object_commit_get_committer(commit);
1911 if (strcmp(author, committer) != 0 &&
1912 fprintf(outfile, "via: %s\n", committer) < 0) {
1913 err = got_error_from_errno();
1916 if (fprintf(outfile, "%s\n",
1917 got_object_commit_get_logmsg(commit)) < 0) {
1918 err = got_error_from_errno();
1923 got_object_commit_close(commit);
1927 static const struct got_error *
1928 create_diff(struct tog_diff_view_state *s)
1930 const struct got_error *err = NULL;
1936 err = got_error_from_errno();
1939 if (s->f && fclose(s->f) != 0) {
1940 err = got_error_from_errno();
1946 err = got_object_get_type(&obj_type, s->repo, s->id1);
1948 err = got_object_get_type(&obj_type, s->repo, s->id2);
1953 case GOT_OBJ_TYPE_BLOB:
1954 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1955 s->diff_context, s->repo, f);
1957 case GOT_OBJ_TYPE_TREE:
1958 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1959 s->diff_context, s->repo, f);
1961 case GOT_OBJ_TYPE_COMMIT: {
1962 const struct got_object_id_queue *parent_ids;
1963 struct got_object_qid *pid;
1964 struct got_commit_object *commit2;
1966 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1969 /* Show commit info if we're diffing to a parent/root commit. */
1971 write_commit_info(s->id2, s->repo, f);
1973 parent_ids = got_object_commit_get_parent_ids(commit2);
1974 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1975 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1976 write_commit_info(s->id2, s->repo, f);
1981 got_object_commit_close(commit2);
1983 err = got_diff_objects_as_commits(s->id1, s->id2,
1984 s->diff_context, s->repo, f);
1988 err = got_error(GOT_ERR_OBJ_TYPE);
1992 if (f && fflush(f) != 0 && err == NULL)
1993 err = got_error_from_errno();
1997 static const struct got_error *
1998 open_diff_view(struct tog_view *view, struct got_object_id *id1,
1999 struct got_object_id *id2, struct commit_queue_entry *selected_entry,
2000 struct commit_queue *commits, int *log_complete,
2001 pthread_cond_t *need_commits, int *commits_needed,
2002 struct got_repository *repo)
2004 const struct got_error *err;
2006 if (id1 != NULL && id2 != NULL) {
2008 err = got_object_get_type(&type1, repo, id1);
2011 err = got_object_get_type(&type2, repo, id2);
2016 return got_error(GOT_ERR_OBJ_TYPE);
2020 view->state.diff.id1 = got_object_id_dup(id1);
2021 if (view->state.diff.id1 == NULL)
2022 return got_error_from_errno();
2024 view->state.diff.id1 = NULL;
2026 view->state.diff.id2 = got_object_id_dup(id2);
2027 if (view->state.diff.id2 == NULL) {
2028 free(view->state.diff.id1);
2029 view->state.diff.id1 = NULL;
2030 return got_error_from_errno();
2032 view->state.diff.f = NULL;
2033 view->state.diff.first_displayed_line = 1;
2034 view->state.diff.last_displayed_line = view->nlines;
2035 view->state.diff.diff_context = 3;
2036 view->state.diff.selected_entry = selected_entry;
2037 view->state.diff.commits = commits;
2038 view->state.diff.log_complete = log_complete;
2039 view->state.diff.need_commits = need_commits;
2040 view->state.diff.commits_needed = commits_needed;
2041 view->state.diff.repo = repo;
2043 err = create_diff(&view->state.diff);
2045 free(view->state.diff.id1);
2046 view->state.diff.id1 = NULL;
2047 free(view->state.diff.id2);
2048 view->state.diff.id2 = NULL;
2052 view->show = show_diff_view;
2053 view->input = input_diff_view;
2054 view->close = close_diff_view;
2059 static const struct got_error *
2060 close_diff_view(struct tog_view *view)
2062 const struct got_error *err = NULL;
2064 free(view->state.diff.id1);
2065 view->state.diff.id1 = NULL;
2066 free(view->state.diff.id2);
2067 view->state.diff.id2 = NULL;
2068 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2069 err = got_error_from_errno();
2073 static const struct got_error *
2074 show_diff_view(struct tog_view *view)
2076 const struct got_error *err;
2077 struct tog_diff_view_state *s = &view->state.diff;
2078 char *id_str1 = NULL, *id_str2, *header;
2081 err = got_object_id_str(&id_str1, s->id1);
2085 err = got_object_id_str(&id_str2, s->id2);
2089 if (asprintf(&header, "diff %s %s",
2090 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2091 err = got_error_from_errno();
2099 return draw_file(view, s->f, &s->first_displayed_line,
2100 &s->last_displayed_line, &s->eof, view->nlines,
2104 static const struct got_error *
2105 set_selected_commit(struct tog_diff_view_state *s,
2106 struct commit_queue_entry *entry)
2108 struct commit_queue_entry *pentry;
2111 s->id2 = got_object_id_dup(entry->id);
2113 return got_error_from_errno();
2117 pentry = TAILQ_NEXT(entry, entry);
2119 s->id1 = got_object_id_dup(pentry->id);
2121 return got_error_from_errno();
2124 s->selected_entry = entry;
2128 static const struct got_error *
2129 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2130 struct tog_view **focus_view, struct tog_view *view, int ch)
2132 const struct got_error *err = NULL;
2133 struct tog_diff_view_state *s = &view->state.diff;
2134 struct commit_queue_entry *entry, *pentry;
2140 if (s->first_displayed_line > 1)
2141 s->first_displayed_line--;
2145 while (i++ < view->nlines - 1 &&
2146 s->first_displayed_line > 1)
2147 s->first_displayed_line--;
2152 s->first_displayed_line++;
2157 while (!s->eof && i++ < view->nlines - 1) {
2159 line = parse_next_line(s->f, NULL);
2160 s->first_displayed_line++;
2166 if (s->diff_context > 0) {
2168 err = create_diff(s);
2172 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2174 err = create_diff(s);
2179 if (s->commits == NULL)
2181 entry = TAILQ_PREV(s->selected_entry,
2182 commit_queue_head, entry);
2186 err = set_selected_commit(s, entry);
2190 s->first_displayed_line = 1;
2191 s->last_displayed_line = view->nlines;
2193 err = create_diff(s);
2197 if (s->commits == NULL)
2199 entry = TAILQ_NEXT(s->selected_entry, entry);
2201 pentry = TAILQ_NEXT(entry, entry);
2202 if (entry == NULL || pentry == NULL) {
2203 if (!*s->log_complete)
2204 *s->commits_needed = 2;
2206 while (entry == NULL || pentry == NULL) {
2208 if (*s->log_complete)
2210 errcode = pthread_cond_signal(s->need_commits);
2212 return got_error_set_errno(errcode);
2213 errcode = pthread_mutex_unlock(&tog_mutex);
2215 return got_error_set_errno(errcode);
2217 errcode = pthread_mutex_lock(&tog_mutex);
2219 return got_error_set_errno(errcode);
2220 entry = TAILQ_NEXT(s->selected_entry, entry);
2222 pentry = TAILQ_NEXT(entry, entry);
2228 err = set_selected_commit(s, entry);
2232 s->first_displayed_line = 1;
2233 s->last_displayed_line = view->nlines;
2235 err = create_diff(s);
2244 static const struct got_error *
2245 cmd_diff(int argc, char *argv[])
2247 const struct got_error *error = NULL;
2248 struct got_repository *repo = NULL;
2249 struct got_object_id *id1 = NULL, *id2 = NULL;
2250 char *repo_path = NULL;
2251 char *id_str1 = NULL, *id_str2 = NULL;
2253 struct tog_view *view;
2256 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2261 while ((ch = getopt(argc, argv, "")) != -1) {
2273 usage_diff(); /* TODO show local worktree changes */
2274 } else if (argc == 2) {
2275 repo_path = getcwd(NULL, 0);
2276 if (repo_path == NULL)
2277 return got_error_from_errno();
2280 } else if (argc == 3) {
2281 repo_path = realpath(argv[0], NULL);
2282 if (repo_path == NULL)
2283 return got_error_from_errno();
2291 error = apply_unveil(repo_path, NULL);
2295 error = got_repo_open(&repo, repo_path);
2300 error = got_object_resolve_id_str(&id1, repo, id_str1);
2304 error = got_object_resolve_id_str(&id2, repo, id_str2);
2308 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2310 error = got_error_from_errno();
2313 error = open_diff_view(view, id1, id2, NULL, NULL, NULL, NULL, NULL,
2317 error = view_loop(view);
2319 got_repo_close(repo);
2327 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2332 struct tog_blame_line {
2334 struct got_object_id *id;
2337 static const struct got_error *
2338 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2339 const char *path, struct tog_blame_line *lines, int nlines,
2340 int blame_complete, int selected_line, int *first_displayed_line,
2341 int *last_displayed_line, int *eof, int max_lines)
2343 const struct got_error *err;
2344 int lineno = 0, nprinted = 0;
2349 struct tog_blame_line *blame_line;
2350 struct got_object_id *prev_id = NULL;
2353 err = got_object_id_str(&id_str, id);
2358 werase(view->window);
2360 if (asprintf(&line, "commit %s", id_str) == -1) {
2361 err = got_error_from_errno();
2366 err = format_line(&wline, &width, line, view->ncols);
2369 if (view_needs_focus_indication(view))
2370 wstandout(view->window);
2371 waddwstr(view->window, wline);
2372 if (view_needs_focus_indication(view))
2373 wstandend(view->window);
2376 if (width < view->ncols)
2377 waddch(view->window, '\n');
2379 if (asprintf(&line, "[%d/%d] %s%s",
2380 *first_displayed_line - 1 + selected_line, nlines,
2381 blame_complete ? "" : "annotating ", path) == -1) {
2383 return got_error_from_errno();
2386 err = format_line(&wline, &width, line, view->ncols);
2391 waddwstr(view->window, wline);
2394 if (width < view->ncols)
2395 waddch(view->window, '\n');
2398 while (nprinted < max_lines - 2) {
2399 line = parse_next_line(f, &len);
2404 if (++lineno < *first_displayed_line) {
2409 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2410 err = format_line(&wline, &width, line, wlimit);
2416 if (view->focussed && nprinted == selected_line - 1)
2417 wstandout(view->window);
2419 blame_line = &lines[lineno - 1];
2420 if (blame_line->annotated && prev_id &&
2421 got_object_id_cmp(prev_id, blame_line->id) == 0)
2422 waddstr(view->window, " ");
2423 else if (blame_line->annotated) {
2425 err = got_object_id_str(&id_str, blame_line->id);
2431 wprintw(view->window, "%.8s ", id_str);
2433 prev_id = blame_line->id;
2435 waddstr(view->window, "........ ");
2439 waddwstr(view->window, wline);
2440 while (width < wlimit) {
2441 waddch(view->window, ' ');
2444 if (view->focussed && nprinted == selected_line - 1)
2445 wstandend(view->window);
2446 if (++nprinted == 1)
2447 *first_displayed_line = lineno;
2452 *last_displayed_line = lineno;
2459 static const struct got_error *
2460 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2462 const struct got_error *err = NULL;
2463 struct tog_blame_cb_args *a = arg;
2464 struct tog_blame_line *line;
2467 if (nlines != a->nlines ||
2468 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2469 return got_error(GOT_ERR_RANGE);
2471 errcode = pthread_mutex_lock(&tog_mutex);
2473 return got_error_set_errno(errcode);
2475 if (*a->quit) { /* user has quit the blame view */
2476 err = got_error(GOT_ERR_ITER_COMPLETED);
2481 goto done; /* no change in this commit */
2483 line = &a->lines[lineno - 1];
2484 if (line->annotated)
2487 line->id = got_object_id_dup(id);
2488 if (line->id == NULL) {
2489 err = got_error_from_errno();
2492 line->annotated = 1;
2494 errcode = pthread_mutex_unlock(&tog_mutex);
2496 err = got_error_set_errno(errcode);
2501 blame_thread(void *arg)
2503 const struct got_error *err;
2504 struct tog_blame_thread_args *ta = arg;
2505 struct tog_blame_cb_args *a = ta->cb_args;
2508 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2509 blame_cb, ta->cb_args);
2511 errcode = pthread_mutex_lock(&tog_mutex);
2513 return (void *)got_error_set_errno(errcode);
2515 got_repo_close(ta->repo);
2519 errcode = pthread_mutex_unlock(&tog_mutex);
2520 if (errcode && err == NULL)
2521 err = got_error_set_errno(errcode);
2526 static struct got_object_id *
2527 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2530 struct tog_blame_line *line;
2532 line = &lines[first_displayed_line - 1 + selected_line - 1];
2533 if (!line->annotated)
2539 static const struct got_error *
2540 stop_blame(struct tog_blame *blame)
2542 const struct got_error *err = NULL;
2545 if (blame->thread) {
2547 errcode = pthread_mutex_unlock(&tog_mutex);
2549 return got_error_set_errno(errcode);
2550 errcode = pthread_join(blame->thread, (void **)&err);
2552 return got_error_set_errno(errcode);
2553 errcode = pthread_mutex_lock(&tog_mutex);
2555 return got_error_set_errno(errcode);
2556 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2558 blame->thread = NULL;
2560 if (blame->thread_args.repo) {
2561 got_repo_close(blame->thread_args.repo);
2562 blame->thread_args.repo = NULL;
2565 if (fclose(blame->f) != 0 && err == NULL)
2566 err = got_error_from_errno();
2570 for (i = 0; i < blame->nlines; i++)
2571 free(blame->lines[i].id);
2573 blame->lines = NULL;
2575 free(blame->cb_args.commit_id);
2576 blame->cb_args.commit_id = NULL;
2581 static const struct got_error *
2582 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2583 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2584 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2585 struct got_repository *repo)
2587 const struct got_error *err = NULL;
2588 struct got_blob_object *blob = NULL;
2589 struct got_repository *thread_repo = NULL;
2590 struct got_object_id *obj_id = NULL;
2593 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2597 return got_error(GOT_ERR_NO_OBJ);
2599 err = got_object_get_type(&obj_type, repo, obj_id);
2603 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2604 err = got_error(GOT_ERR_OBJ_TYPE);
2608 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2611 blame->f = got_opentemp();
2612 if (blame->f == NULL) {
2613 err = got_error_from_errno();
2616 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2621 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2622 if (blame->lines == NULL) {
2623 err = got_error_from_errno();
2627 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2631 blame->cb_args.view = view;
2632 blame->cb_args.lines = blame->lines;
2633 blame->cb_args.nlines = blame->nlines;
2634 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2635 if (blame->cb_args.commit_id == NULL) {
2636 err = got_error_from_errno();
2639 blame->cb_args.quit = done;
2641 blame->thread_args.path = path;
2642 blame->thread_args.repo = thread_repo;
2643 blame->thread_args.cb_args = &blame->cb_args;
2644 blame->thread_args.complete = blame_complete;
2645 *blame_complete = 0;
2649 got_object_blob_close(blob);
2656 static const struct got_error *
2657 open_blame_view(struct tog_view *view, char *path,
2658 struct got_object_id *commit_id, struct got_repository *repo)
2660 const struct got_error *err = NULL;
2661 struct tog_blame_view_state *s = &view->state.blame;
2663 SIMPLEQ_INIT(&s->blamed_commits);
2665 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2669 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2670 s->first_displayed_line = 1;
2671 s->last_displayed_line = view->nlines;
2672 s->selected_line = 1;
2673 s->blame_complete = 0;
2675 if (s->path == NULL)
2676 return got_error_from_errno();
2678 s->commit_id = commit_id;
2679 memset(&s->blame, 0, sizeof(s->blame));
2681 view->show = show_blame_view;
2682 view->input = input_blame_view;
2683 view->close = close_blame_view;
2685 return run_blame(&s->blame, view, &s->blame_complete,
2686 &s->first_displayed_line, &s->last_displayed_line,
2687 &s->selected_line, &s->done, &s->eof, s->path,
2688 s->blamed_commit->id, s->repo);
2691 static const struct got_error *
2692 close_blame_view(struct tog_view *view)
2694 const struct got_error *err = NULL;
2695 struct tog_blame_view_state *s = &view->state.blame;
2697 if (s->blame.thread)
2698 err = stop_blame(&s->blame);
2700 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2701 struct got_object_qid *blamed_commit;
2702 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2703 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2704 got_object_qid_free(blamed_commit);
2712 static const struct got_error *
2713 show_blame_view(struct tog_view *view)
2715 const struct got_error *err = NULL;
2716 struct tog_blame_view_state *s = &view->state.blame;
2719 if (s->blame.thread == NULL) {
2720 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2721 &s->blame.thread_args);
2723 return got_error_set_errno(errcode);
2726 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2727 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2728 s->selected_line, &s->first_displayed_line,
2729 &s->last_displayed_line, &s->eof, view->nlines);
2735 static const struct got_error *
2736 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2737 struct tog_view **focus_view, struct tog_view *view, int ch)
2739 const struct got_error *err = NULL, *thread_err = NULL;
2740 struct tog_view *diff_view;
2741 struct tog_blame_view_state *s = &view->state.blame;
2750 if (s->selected_line > 1)
2752 else if (s->selected_line == 1 &&
2753 s->first_displayed_line > 1)
2754 s->first_displayed_line--;
2757 if (s->first_displayed_line == 1) {
2758 s->selected_line = 1;
2761 if (s->first_displayed_line > view->nlines - 2)
2762 s->first_displayed_line -=
2765 s->first_displayed_line = 1;
2769 if (s->selected_line < view->nlines - 2 &&
2770 s->first_displayed_line +
2771 s->selected_line <= s->blame.nlines)
2773 else if (s->last_displayed_line <
2775 s->first_displayed_line++;
2779 struct got_object_id *id = NULL;
2780 id = get_selected_commit_id(s->blame.lines,
2781 s->first_displayed_line, s->selected_line);
2785 struct got_commit_object *commit;
2786 struct got_object_qid *pid;
2787 struct got_object_id *blob_id = NULL;
2789 err = got_object_open_as_commit(&commit,
2793 pid = SIMPLEQ_FIRST(
2794 got_object_commit_get_parent_ids(commit));
2796 got_object_commit_close(commit);
2799 /* Check if path history ends here. */
2800 err = got_object_id_by_path(&blob_id, s->repo,
2803 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2805 got_object_commit_close(commit);
2808 err = got_object_get_type(&obj_type, s->repo,
2811 /* Can't blame non-blob type objects. */
2812 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2813 got_object_commit_close(commit);
2816 err = got_object_qid_alloc(&s->blamed_commit,
2818 got_object_commit_close(commit);
2820 if (got_object_id_cmp(id,
2821 s->blamed_commit->id) == 0)
2823 err = got_object_qid_alloc(&s->blamed_commit,
2829 thread_err = stop_blame(&s->blame);
2833 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2834 s->blamed_commit, entry);
2835 err = run_blame(&s->blame, view, &s->blame_complete,
2836 &s->first_displayed_line, &s->last_displayed_line,
2837 &s->selected_line, &s->done, &s->eof,
2838 s->path, s->blamed_commit->id, s->repo);
2844 struct got_object_qid *first;
2845 first = SIMPLEQ_FIRST(&s->blamed_commits);
2846 if (!got_object_id_cmp(first->id, s->commit_id))
2849 thread_err = stop_blame(&s->blame);
2853 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2854 got_object_qid_free(s->blamed_commit);
2856 SIMPLEQ_FIRST(&s->blamed_commits);
2857 err = run_blame(&s->blame, view, &s->blame_complete,
2858 &s->first_displayed_line, &s->last_displayed_line,
2859 &s->selected_line, &s->done, &s->eof, s->path,
2860 s->blamed_commit->id, s->repo);
2867 struct got_object_id *id = NULL;
2868 struct got_object_qid *pid;
2869 struct got_commit_object *commit = NULL;
2870 id = get_selected_commit_id(s->blame.lines,
2871 s->first_displayed_line, s->selected_line);
2874 err = got_object_open_as_commit(&commit, s->repo, id);
2877 pid = SIMPLEQ_FIRST(
2878 got_object_commit_get_parent_ids(commit));
2879 if (view_is_parent_view(view))
2880 begin_x = view_split_begin_x(view->begin_x);
2881 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2882 if (diff_view == NULL) {
2883 got_object_commit_close(commit);
2884 err = got_error_from_errno();
2887 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2888 id, NULL, NULL, NULL, NULL, NULL, s->repo);
2889 got_object_commit_close(commit);
2891 view_close(diff_view);
2894 if (view_is_parent_view(view)) {
2895 err = view_close_child(view);
2898 err = view_set_child(view, diff_view);
2900 view_close(diff_view);
2903 *focus_view = diff_view;
2904 view->child_focussed = 1;
2906 *new_view = diff_view;
2913 if (s->last_displayed_line >= s->blame.nlines &&
2914 s->selected_line < view->nlines - 2) {
2915 s->selected_line = MIN(s->blame.nlines,
2919 if (s->last_displayed_line + view->nlines - 2
2921 s->first_displayed_line +=
2924 s->first_displayed_line =
2929 if (s->selected_line > view->nlines - 2) {
2930 s->selected_line = MIN(s->blame.nlines,
2937 return thread_err ? thread_err : err;
2940 static const struct got_error *
2941 cmd_blame(int argc, char *argv[])
2943 const struct got_error *error;
2944 struct got_repository *repo = NULL;
2945 struct got_worktree *worktree = NULL;
2946 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2947 struct got_object_id *commit_id = NULL;
2948 char *commit_id_str = NULL;
2950 struct tog_view *view;
2953 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2958 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2961 commit_id_str = optarg;
2964 repo_path = realpath(optarg, NULL);
2965 if (repo_path == NULL)
2966 err(1, "-r option");
2982 cwd = getcwd(NULL, 0);
2984 error = got_error_from_errno();
2987 if (repo_path == NULL) {
2988 error = got_worktree_open(&worktree, cwd);
2989 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2995 strdup(got_worktree_get_repo_path(worktree));
2996 if (repo_path == NULL)
2997 error = got_error_from_errno();
3001 repo_path = strdup(cwd);
3002 if (repo_path == NULL) {
3003 error = got_error_from_errno();
3011 error = apply_unveil(repo_path, NULL);
3015 error = got_repo_open(&repo, repo_path);
3020 const char *prefix = got_worktree_get_path_prefix(worktree);
3021 char *p, *worktree_subdir = cwd +
3022 strlen(got_worktree_get_root_path(worktree));
3023 if (asprintf(&p, "%s%s%s%s%s",
3024 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3025 worktree_subdir, worktree_subdir[0] ? "/" : "",
3027 error = got_error_from_errno();
3030 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3033 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3038 if (commit_id_str == NULL) {
3039 struct got_reference *head_ref;
3040 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3043 error = got_ref_resolve(&commit_id, repo, head_ref);
3044 got_ref_close(head_ref);
3046 error = got_object_resolve_id_str(&commit_id, repo,
3052 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3054 error = got_error_from_errno();
3057 error = open_blame_view(view, in_repo_path, commit_id, repo);
3060 error = view_loop(view);
3066 got_worktree_close(worktree);
3068 got_repo_close(repo);
3072 static const struct got_error *
3073 draw_tree_entries(struct tog_view *view,
3074 struct got_tree_entry **first_displayed_entry,
3075 struct got_tree_entry **last_displayed_entry,
3076 struct got_tree_entry **selected_entry, int *ndisplayed,
3077 const char *label, int show_ids, const char *parent_path,
3078 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3080 const struct got_error *err = NULL;
3081 struct got_tree_entry *te;
3087 werase(view->window);
3092 err = format_line(&wline, &width, label, view->ncols);
3095 if (view_needs_focus_indication(view))
3096 wstandout(view->window);
3097 waddwstr(view->window, wline);
3098 if (view_needs_focus_indication(view))
3099 wstandend(view->window);
3102 if (width < view->ncols)
3103 waddch(view->window, '\n');
3106 err = format_line(&wline, &width, parent_path, view->ncols);
3109 waddwstr(view->window, wline);
3112 if (width < view->ncols)
3113 waddch(view->window, '\n');
3116 waddch(view->window, '\n');
3120 te = SIMPLEQ_FIRST(&entries->head);
3121 if (*first_displayed_entry == NULL) {
3122 if (selected == 0) {
3124 wstandout(view->window);
3125 *selected_entry = NULL;
3127 waddstr(view->window, " ..\n"); /* parent directory */
3128 if (selected == 0 && view->focussed)
3129 wstandend(view->window);
3136 while (te != *first_displayed_entry)
3137 te = SIMPLEQ_NEXT(te, entry);
3141 char *line = NULL, *id_str = NULL;
3144 err = got_object_id_str(&id_str, te->id);
3146 return got_error_from_errno();
3148 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3149 te->name, S_ISDIR(te->mode) ? "/" :
3150 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3152 return got_error_from_errno();
3155 err = format_line(&wline, &width, line, view->ncols);
3160 if (n == selected) {
3162 wstandout(view->window);
3163 *selected_entry = te;
3165 waddwstr(view->window, wline);
3166 if (width < view->ncols)
3167 waddch(view->window, '\n');
3168 if (n == selected && view->focussed)
3169 wstandend(view->window);
3175 *last_displayed_entry = te;
3178 te = SIMPLEQ_NEXT(te, entry);
3185 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3186 const struct got_tree_entries *entries, int isroot)
3188 struct got_tree_entry *te, *prev;
3191 if (*first_displayed_entry == NULL)
3194 te = SIMPLEQ_FIRST(&entries->head);
3195 if (*first_displayed_entry == te) {
3197 *first_displayed_entry = NULL;
3201 /* XXX this is stupid... switch to TAILQ? */
3202 for (i = 0; i < maxscroll; i++) {
3203 while (te != *first_displayed_entry) {
3205 te = SIMPLEQ_NEXT(te, entry);
3207 *first_displayed_entry = prev;
3208 te = SIMPLEQ_FIRST(&entries->head);
3210 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3211 *first_displayed_entry = NULL;
3215 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3216 struct got_tree_entry *last_displayed_entry,
3217 const struct got_tree_entries *entries)
3219 struct got_tree_entry *next, *last;
3222 if (*first_displayed_entry)
3223 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3225 next = SIMPLEQ_FIRST(&entries->head);
3226 last = last_displayed_entry;
3227 while (next && last && n++ < maxscroll) {
3228 last = SIMPLEQ_NEXT(last, entry);
3230 *first_displayed_entry = next;
3231 next = SIMPLEQ_NEXT(next, entry);
3237 static const struct got_error *
3238 tree_entry_path(char **path, struct tog_parent_trees *parents,
3239 struct got_tree_entry *te)
3241 const struct got_error *err = NULL;
3242 struct tog_parent_tree *pt;
3243 size_t len = 2; /* for leading slash and NUL */
3245 TAILQ_FOREACH(pt, parents, entry)
3246 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3248 len += strlen(te->name);
3250 *path = calloc(1, len);
3252 return got_error_from_errno();
3255 pt = TAILQ_LAST(parents, tog_parent_trees);
3257 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3258 err = got_error(GOT_ERR_NO_SPACE);
3261 if (strlcat(*path, "/", len) >= len) {
3262 err = got_error(GOT_ERR_NO_SPACE);
3265 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3268 if (strlcat(*path, te->name, len) >= len) {
3269 err = got_error(GOT_ERR_NO_SPACE);
3281 static const struct got_error *
3282 blame_tree_entry(struct tog_view **new_view, int begin_x,
3283 struct got_tree_entry *te, struct tog_parent_trees *parents,
3284 struct got_object_id *commit_id, struct got_repository *repo)
3286 const struct got_error *err = NULL;
3288 struct tog_view *blame_view;
3290 err = tree_entry_path(&path, parents, te);
3294 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3295 if (blame_view == NULL)
3296 return got_error_from_errno();
3298 err = open_blame_view(blame_view, path, commit_id, repo);
3300 view_close(blame_view);
3303 *new_view = blame_view;
3307 static const struct got_error *
3308 log_tree_entry(struct tog_view **new_view, int begin_x,
3309 struct got_tree_entry *te, struct tog_parent_trees *parents,
3310 struct got_object_id *commit_id, struct got_repository *repo)
3312 struct tog_view *log_view;
3313 const struct got_error *err = NULL;
3316 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3317 if (log_view == NULL)
3318 return got_error_from_errno();
3320 err = tree_entry_path(&path, parents, te);
3324 err = open_log_view(log_view, commit_id, repo, path, 0);
3326 view_close(log_view);
3328 *new_view = log_view;
3333 static const struct got_error *
3334 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3335 struct got_object_id *commit_id, struct got_repository *repo)
3337 const struct got_error *err = NULL;
3338 char *commit_id_str = NULL;
3339 struct tog_tree_view_state *s = &view->state.tree;
3341 TAILQ_INIT(&s->parents);
3343 err = got_object_id_str(&commit_id_str, commit_id);
3347 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3348 err = got_error_from_errno();
3352 s->root = s->tree = root;
3353 s->entries = got_object_tree_get_entries(root);
3354 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3355 s->commit_id = got_object_id_dup(commit_id);
3356 if (s->commit_id == NULL) {
3357 err = got_error_from_errno();
3362 view->show = show_tree_view;
3363 view->input = input_tree_view;
3364 view->close = close_tree_view;
3366 free(commit_id_str);
3368 free(s->tree_label);
3369 s->tree_label = NULL;
3374 static const struct got_error *
3375 close_tree_view(struct tog_view *view)
3377 struct tog_tree_view_state *s = &view->state.tree;
3379 free(s->tree_label);
3380 s->tree_label = NULL;
3382 s->commit_id = NULL;
3383 while (!TAILQ_EMPTY(&s->parents)) {
3384 struct tog_parent_tree *parent;
3385 parent = TAILQ_FIRST(&s->parents);
3386 TAILQ_REMOVE(&s->parents, parent, entry);
3390 if (s->tree != s->root)
3391 got_object_tree_close(s->tree);
3392 got_object_tree_close(s->root);
3397 static const struct got_error *
3398 show_tree_view(struct tog_view *view)
3400 const struct got_error *err = NULL;
3401 struct tog_tree_view_state *s = &view->state.tree;
3404 err = tree_entry_path(&parent_path, &s->parents, NULL);
3408 err = draw_tree_entries(view, &s->first_displayed_entry,
3409 &s->last_displayed_entry, &s->selected_entry,
3410 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3411 s->entries, s->selected, view->nlines, s->tree == s->root);
3418 static const struct got_error *
3419 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3420 struct tog_view **focus_view, struct tog_view *view, int ch)
3422 const struct got_error *err = NULL;
3423 struct tog_tree_view_state *s = &view->state.tree;
3424 struct tog_view *log_view;
3425 int begin_x = 0, nscrolled;
3429 s->show_ids = !s->show_ids;
3432 if (!s->selected_entry)
3434 if (view_is_parent_view(view))
3435 begin_x = view_split_begin_x(view->begin_x);
3436 err = log_tree_entry(&log_view, begin_x,
3437 s->selected_entry, &s->parents,
3438 s->commit_id, s->repo);
3439 if (view_is_parent_view(view)) {
3440 err = view_close_child(view);
3443 err = view_set_child(view, log_view);
3445 view_close(log_view);
3448 *focus_view = log_view;
3449 view->child_focussed = 1;
3451 *new_view = log_view;
3455 if (s->selected > 0) {
3457 if (s->selected == 0)
3460 if (s->selected > 0)
3462 tree_scroll_up(&s->first_displayed_entry, 1,
3463 s->entries, s->tree == s->root);
3466 tree_scroll_up(&s->first_displayed_entry,
3467 MAX(0, view->nlines - 4 - s->selected), s->entries,
3468 s->tree == s->root);
3470 if (SIMPLEQ_FIRST(&s->entries->head) ==
3471 s->first_displayed_entry && s->tree != s->root)
3472 s->first_displayed_entry = NULL;
3476 if (s->selected < s->ndisplayed - 1) {
3480 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3482 /* can't scroll any further */
3485 tree_scroll_down(&s->first_displayed_entry, 1,
3486 s->last_displayed_entry, s->entries);
3489 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3491 /* can't scroll any further; move cursor down */
3492 if (s->selected < s->ndisplayed - 1)
3493 s->selected = s->ndisplayed - 1;
3496 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3497 view->nlines, s->last_displayed_entry, s->entries);
3498 if (nscrolled < view->nlines) {
3500 struct got_tree_entry *te;
3501 te = s->first_displayed_entry;
3504 te = SIMPLEQ_NEXT(te, entry);
3506 s->selected = ndisplayed - 1;
3511 if (s->selected_entry == NULL) {
3512 struct tog_parent_tree *parent;
3514 /* user selected '..' */
3515 if (s->tree == s->root)
3517 parent = TAILQ_FIRST(&s->parents);
3518 TAILQ_REMOVE(&s->parents, parent,
3520 got_object_tree_close(s->tree);
3521 s->tree = parent->tree;
3523 got_object_tree_get_entries(s->tree);
3524 s->first_displayed_entry =
3525 parent->first_displayed_entry;
3527 parent->selected_entry;
3528 s->selected = parent->selected;
3530 } else if (S_ISDIR(s->selected_entry->mode)) {
3531 struct tog_parent_tree *parent;
3532 struct got_tree_object *child;
3533 err = got_object_open_as_tree(&child,
3534 s->repo, s->selected_entry->id);
3537 parent = calloc(1, sizeof(*parent));
3538 if (parent == NULL) {
3539 err = got_error_from_errno();
3542 parent->tree = s->tree;
3543 parent->first_displayed_entry =
3544 s->first_displayed_entry;
3545 parent->selected_entry = s->selected_entry;
3546 parent->selected = s->selected;
3547 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3550 got_object_tree_get_entries(s->tree);
3552 s->first_displayed_entry = NULL;
3553 } else if (S_ISREG(s->selected_entry->mode)) {
3554 struct tog_view *blame_view;
3555 int begin_x = view_is_parent_view(view) ?
3556 view_split_begin_x(view->begin_x) : 0;
3558 err = blame_tree_entry(&blame_view, begin_x,
3559 s->selected_entry, &s->parents, s->commit_id,
3563 if (view_is_parent_view(view)) {
3564 err = view_close_child(view);
3567 err = view_set_child(view, blame_view);
3569 view_close(blame_view);
3572 *focus_view = blame_view;
3573 view->child_focussed = 1;
3575 *new_view = blame_view;
3579 if (s->selected > view->nlines)
3580 s->selected = s->ndisplayed - 1;
3593 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3598 static const struct got_error *
3599 cmd_tree(int argc, char *argv[])
3601 const struct got_error *error;
3602 struct got_repository *repo = NULL;
3603 char *repo_path = NULL;
3604 struct got_object_id *commit_id = NULL;
3605 char *commit_id_arg = NULL;
3606 struct got_commit_object *commit = NULL;
3607 struct got_tree_object *tree = NULL;
3609 struct tog_view *view;
3612 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3617 while ((ch = getopt(argc, argv, "c:")) != -1) {
3620 commit_id_arg = optarg;
3632 struct got_worktree *worktree;
3633 char *cwd = getcwd(NULL, 0);
3635 return got_error_from_errno();
3636 error = got_worktree_open(&worktree, cwd);
3637 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3642 strdup(got_worktree_get_repo_path(worktree));
3643 got_worktree_close(worktree);
3646 if (repo_path == NULL) {
3647 error = got_error_from_errno();
3650 } else if (argc == 1) {
3651 repo_path = realpath(argv[0], NULL);
3652 if (repo_path == NULL)
3653 return got_error_from_errno();
3659 error = apply_unveil(repo_path, NULL);
3663 error = got_repo_open(&repo, repo_path);
3667 if (commit_id_arg == NULL)
3668 error = get_head_commit_id(&commit_id, repo);
3670 error = got_object_resolve_id_str(&commit_id, repo,
3675 error = got_object_open_as_commit(&commit, repo, commit_id);
3679 error = got_object_open_as_tree(&tree, repo,
3680 got_object_commit_get_tree_id(commit));
3684 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3686 error = got_error_from_errno();
3689 error = open_tree_view(view, tree, commit_id, repo);
3692 error = view_loop(view);
3697 got_object_commit_close(commit);
3699 got_object_tree_close(tree);
3701 got_repo_close(repo);
3710 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3711 "Available commands:\n", getprogname());
3712 for (i = 0; i < nitems(tog_commands); i++) {
3713 struct tog_cmd *cmd = &tog_commands[i];
3714 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3720 make_argv(const char *arg0, const char *arg1)
3723 int argc = (arg1 == NULL ? 1 : 2);
3725 argv = calloc(argc, sizeof(char *));
3728 argv[0] = strdup(arg0);
3729 if (argv[0] == NULL)
3732 argv[1] = strdup(arg1);
3733 if (argv[1] == NULL)
3741 main(int argc, char *argv[])
3743 const struct got_error *error = NULL;
3744 struct tog_cmd *cmd = NULL;
3746 char **cmd_argv = NULL;
3748 setlocale(LC_CTYPE, "");
3750 while ((ch = getopt(argc, argv, "h")) != -1) {
3769 /* Build an argument vector which runs a default command. */
3770 cmd = &tog_commands[0];
3771 cmd_argv = make_argv(cmd->name, NULL);
3776 /* Did the user specific a command? */
3777 for (i = 0; i < nitems(tog_commands); i++) {
3778 if (strncmp(tog_commands[i].name, argv[0],
3779 strlen(argv[0])) == 0) {
3780 cmd = &tog_commands[i];
3782 tog_commands[i].cmd_usage();
3787 /* Did the user specify a repository? */
3788 char *repo_path = realpath(argv[0], NULL);
3790 struct got_repository *repo;
3791 error = got_repo_open(&repo, repo_path);
3793 got_repo_close(repo);
3795 error = got_error_from_errno();
3798 fprintf(stderr, "%s: '%s' is not a "
3799 "known command\n", getprogname(),
3803 fprintf(stderr, "%s: '%s' is neither a known "
3804 "command nor a path to a repository\n",
3805 getprogname(), argv[0]);
3809 cmd = &tog_commands[0];
3810 cmd_argv = make_argv(cmd->name, repo_path);
3816 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3823 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);