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;
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;
159 struct tog_blame_cb_args {
160 struct tog_blame_line *lines; /* one per line */
163 struct tog_view *view;
164 struct got_object_id *commit_id;
168 struct tog_blame_thread_args {
170 struct got_repository *repo;
171 struct tog_blame_cb_args *cb_args;
178 struct tog_blame_line *lines;
181 struct tog_blame_thread_args thread_args;
182 struct tog_blame_cb_args cb_args;
186 struct tog_blame_view_state {
187 int first_displayed_line;
188 int last_displayed_line;
193 struct got_object_id_queue blamed_commits;
194 struct got_object_qid *blamed_commit;
196 struct got_repository *repo;
197 struct got_reflist_head *refs;
198 struct got_object_id *commit_id;
199 struct tog_blame blame;
202 struct tog_parent_tree {
203 TAILQ_ENTRY(tog_parent_tree) entry;
204 struct got_tree_object *tree;
205 struct got_tree_entry *first_displayed_entry;
206 struct got_tree_entry *selected_entry;
210 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
212 struct tog_tree_view_state {
214 struct got_tree_object *root;
215 struct got_tree_object *tree;
216 const struct got_tree_entries *entries;
217 struct got_tree_entry *first_displayed_entry;
218 struct got_tree_entry *last_displayed_entry;
219 struct got_tree_entry *selected_entry;
220 int ndisplayed, selected, show_ids;
221 struct tog_parent_trees parents;
222 struct got_object_id *commit_id;
223 struct got_repository *repo;
224 struct got_reflist_head *refs;
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 tog_view *,
274 struct got_reflist_head *, struct got_repository *);
275 static const struct got_error *show_diff_view(struct tog_view *);
276 static const struct got_error *input_diff_view(struct tog_view **,
277 struct tog_view **, struct tog_view **, struct tog_view *, int);
278 static const struct got_error* close_diff_view(struct tog_view *);
280 static const struct got_error *open_log_view(struct tog_view *,
281 struct got_object_id *, struct got_reflist_head *,
282 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_reflist_head *, 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 *,
297 struct got_reflist_head *, struct got_repository *);
298 static const struct got_error *show_tree_view(struct tog_view *);
299 static const struct got_error *input_tree_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_tree_view(struct tog_view *);
303 static volatile sig_atomic_t tog_sigwinch_received;
306 tog_sigwinch(int signo)
308 tog_sigwinch_received = 1;
311 static const struct got_error *
312 view_close(struct tog_view *view)
314 const struct got_error *err = NULL;
317 view_close(view->child);
321 err = view->close(view);
323 del_panel(view->panel);
325 delwin(view->window);
330 static struct tog_view *
331 view_open(int nlines, int ncols, int begin_y, int begin_x,
332 enum tog_view_type type)
334 struct tog_view *view = calloc(1, sizeof(*view));
342 view->nlines = nlines ? nlines : LINES - begin_y;
343 view->ncols = ncols ? ncols : COLS - begin_x;
344 view->begin_y = begin_y;
345 view->begin_x = begin_x;
346 view->window = newwin(nlines, ncols, begin_y, begin_x);
347 if (view->window == NULL) {
351 view->panel = new_panel(view->window);
352 if (view->panel == NULL ||
353 set_panel_userptr(view->panel, view) != OK) {
358 keypad(view->window, TRUE);
363 view_split_begin_x(int begin_x)
365 if (begin_x > 0 || COLS < 120)
367 return (COLS - MAX(COLS / 2, 80));
370 static const struct got_error *view_resize(struct tog_view *);
372 static const struct got_error *
373 view_splitscreen(struct tog_view *view)
375 const struct got_error *err = NULL;
378 view->begin_x = view_split_begin_x(0);
379 view->nlines = LINES;
380 view->ncols = COLS - view->begin_x;
383 err = view_resize(view);
387 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 return got_error_from_errno();
393 static const struct got_error *
394 view_fullscreen(struct tog_view *view)
396 const struct got_error *err = NULL;
400 view->nlines = LINES;
404 err = view_resize(view);
408 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 return got_error_from_errno();
415 view_is_parent_view(struct tog_view *view)
417 return view->parent == NULL;
420 static const struct got_error *
421 view_resize(struct tog_view *view)
425 if (view->lines > LINES)
426 nlines = view->nlines - (view->lines - LINES);
428 nlines = view->nlines + (LINES - view->lines);
430 if (view->cols > COLS)
431 ncols = view->ncols - (view->cols - COLS);
433 ncols = view->ncols + (COLS - view->cols);
435 if (wresize(view->window, nlines, ncols) == ERR)
436 return got_error_from_errno();
437 if (replace_panel(view->panel, view->window) == ERR)
438 return got_error_from_errno();
439 wclear(view->window);
441 view->nlines = nlines;
447 view->child->begin_x = view_split_begin_x(view->begin_x);
448 if (view->child->begin_x == 0) {
449 view_fullscreen(view->child);
450 if (view->child->focussed)
451 show_panel(view->child->panel);
453 show_panel(view->panel);
455 view_splitscreen(view->child);
456 show_panel(view->child->panel);
463 static const struct got_error *
464 view_close_child(struct tog_view *view)
466 const struct got_error *err = NULL;
468 if (view->child == NULL)
471 err = view_close(view->child);
476 static const struct got_error *
477 view_set_child(struct tog_view *view, struct tog_view *child)
479 const struct got_error *err = NULL;
482 child->parent = view;
487 view_is_splitscreen(struct tog_view *view)
489 return view->begin_x > 0;
493 * Erase all content of the view. Can be used to "flash" the view because
494 * the view loop will redraw it quickly, providing a more subtle visual
495 * effect than curs_flash(3) would provide.
498 view_flash(struct tog_view *view)
500 werase(view->window);
511 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
512 cols = 80; /* Default */
518 resize_term(lines, cols);
521 static const struct got_error *
522 view_input(struct tog_view **new, struct tog_view **dead,
523 struct tog_view **focus, int *done, struct tog_view *view,
524 struct tog_view_list_head *views)
526 const struct got_error *err = NULL;
534 nodelay(stdscr, FALSE);
535 /* Allow threads to make progress while we are waiting for input. */
536 errcode = pthread_mutex_unlock(&tog_mutex);
538 return got_error_set_errno(errcode);
539 ch = wgetch(view->window);
540 errcode = pthread_mutex_lock(&tog_mutex);
542 return got_error_set_errno(errcode);
543 nodelay(stdscr, TRUE);
545 if (tog_sigwinch_received) {
547 tog_sigwinch_received = 0;
548 TAILQ_FOREACH(v, views, entry) {
549 err = view_resize(v);
552 err = v->input(new, dead, focus, v, KEY_RESIZE);
563 *focus = view->child;
564 view->child_focussed = 1;
565 } else if (view->parent) {
566 *focus = view->parent;
567 view->parent->child_focussed = 0;
571 err = view->input(new, dead, focus, view, ch);
578 if (view_is_parent_view(view)) {
579 if (view->child == NULL)
581 if (view_is_splitscreen(view->child)) {
582 *focus = view->child;
583 view->child_focussed = 1;
584 err = view_fullscreen(view->child);
586 err = view_splitscreen(view->child);
589 err = view->child->input(new, dead, focus,
590 view->child, KEY_RESIZE);
592 if (view_is_splitscreen(view)) {
594 view->parent->child_focussed = 1;
595 err = view_fullscreen(view);
597 err = view_splitscreen(view);
601 err = view->input(new, dead, focus, view,
608 err = view->input(new, dead, focus, view, ch);
616 view_vborder(struct tog_view *view)
619 struct tog_view *view_above;
622 return view_vborder(view->parent);
624 panel = panel_above(view->panel);
628 view_above = panel_userptr(panel);
629 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
630 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
634 view_needs_focus_indication(struct tog_view *view)
636 if (view_is_parent_view(view)) {
637 if (view->child == NULL || view->child_focussed)
639 if (!view_is_splitscreen(view->child))
641 } else if (!view_is_splitscreen(view))
644 return view->focussed;
647 static const struct got_error *
648 view_loop(struct tog_view *view)
650 const struct got_error *err = NULL;
651 struct tog_view_list_head views;
652 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
653 int fast_refresh = 10;
654 int done = 0, errcode;
656 errcode = pthread_mutex_lock(&tog_mutex);
658 return got_error_set_errno(errcode);
661 TAILQ_INSERT_HEAD(&views, view, entry);
665 err = view->show(view);
670 while (!TAILQ_EMPTY(&views) && !done) {
671 /* Refresh fast during initialization, then become slower. */
672 if (fast_refresh && fast_refresh-- == 0)
673 halfdelay(10); /* switch to once per second */
675 err = view_input(&new_view, &dead_view, &focus_view, &done,
680 struct tog_view *prev = NULL;
682 if (view_is_parent_view(dead_view))
683 prev = TAILQ_PREV(dead_view,
684 tog_view_list_head, entry);
685 else if (view->parent != dead_view)
688 if (dead_view->parent)
689 dead_view->parent->child = NULL;
691 TAILQ_REMOVE(&views, dead_view, entry);
693 err = view_close(dead_view);
694 if (err || dead_view == main_view)
697 if (view == dead_view) {
702 else if (!TAILQ_EMPTY(&views))
703 view = TAILQ_LAST(&views,
708 if (view->child && view->child_focussed)
709 focus_view = view->child;
716 struct tog_view *v, *t;
717 /* Only allow one parent view per type. */
718 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
719 if (v->type != new_view->type)
721 TAILQ_REMOVE(&views, v, entry);
727 TAILQ_INSERT_TAIL(&views, new_view, entry);
729 if (focus_view == NULL)
730 focus_view = new_view;
733 show_panel(focus_view->panel);
736 focus_view->focussed = 1;
739 show_panel(new_view->panel);
740 if (view->child && view_is_splitscreen(view->child))
741 show_panel(view->child->panel);
744 if (focus_view == NULL) {
746 show_panel(view->panel);
747 if (view->child && view_is_splitscreen(view->child))
748 show_panel(view->child->panel);
752 err = view->parent->show(view->parent);
756 err = view->show(view);
760 err = view->child->show(view->child);
769 while (!TAILQ_EMPTY(&views)) {
770 view = TAILQ_FIRST(&views);
771 TAILQ_REMOVE(&views, view, entry);
775 errcode = pthread_mutex_unlock(&tog_mutex);
777 return got_error_set_errno(errcode);
787 "usage: %s log [-c commit] [-r repository-path] [path]\n",
792 /* Create newly allocated wide-character string equivalent to a byte string. */
793 static const struct got_error *
794 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
797 const struct got_error *err = NULL;
800 *wlen = mbstowcs(NULL, s, 0);
801 if (*wlen == (size_t)-1) {
804 return got_error_from_errno();
806 /* byte string invalid in current encoding; try to "fix" it */
807 err = got_mbsavis(&vis, &vislen, s);
810 *wlen = mbstowcs(NULL, vis, 0);
811 if (*wlen == (size_t)-1) {
812 err = got_error_from_errno(); /* give up */
817 *ws = calloc(*wlen + 1, sizeof(*ws));
819 err = got_error_from_errno();
823 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
824 err = got_error_from_errno();
835 /* Format a line for display, ensuring that it won't overflow a width limit. */
836 static const struct got_error *
837 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
839 const struct got_error *err = NULL;
841 wchar_t *wline = NULL;
848 err = mbs2ws(&wline, &wlen, line);
853 while (i < wlen && cols < wlimit) {
854 int width = wcwidth(wline[i]);
861 if (cols + width <= wlimit)
866 if (wline[i] == L'\t')
867 cols += TABSIZE - ((cols + 1) % TABSIZE);
871 err = got_error_from_errno();
886 static const struct got_error*
887 build_refs_str(char **refs_str, struct got_reflist_head *refs,
888 struct got_object_id *id)
890 static const struct got_error *err = NULL;
891 struct got_reflist_entry *re;
897 SIMPLEQ_FOREACH(re, refs, entry) {
898 if (got_object_id_cmp(re->id, id) != 0)
900 name = got_ref_get_name(re->ref);
901 if (strcmp(name, GOT_REF_HEAD) == 0)
903 if (strncmp(name, "refs/", 5) == 0)
905 if (strncmp(name, "heads/", 6) == 0)
907 if (strncmp(name, "remotes/", 8) == 0)
910 if (asprintf(refs_str, "%s%s%s", s ? s : "",
911 s ? ", " : "", name) == -1) {
912 err = got_error_from_errno();
923 static const struct got_error *
924 draw_commit(struct tog_view *view, struct got_commit_object *commit,
925 struct got_object_id *id, struct got_reflist_head *refs)
927 const struct got_error *err = NULL;
928 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
929 char *logmsg0 = NULL, *logmsg = NULL;
930 char *author0 = NULL, *author = NULL;
931 wchar_t *wlogmsg = NULL, *wauthor = NULL;
932 int author_width, logmsg_width;
933 char *newline, *smallerthan, *at;
936 static const size_t date_display_cols = 9;
937 static const size_t author_display_cols = 10;
938 const int avail = view->ncols;
940 time_t committer_time;
942 committer_time = got_object_commit_get_committer_time(commit);
943 if (localtime_r(&committer_time, &tm) == NULL)
944 return got_error_from_errno();
945 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
947 return got_error(GOT_ERR_NO_SPACE);
949 if (avail < date_display_cols)
950 limit = MIN(sizeof(datebuf) - 1, avail);
952 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
953 waddnstr(view->window, datebuf, limit);
958 author0 = strdup(got_object_commit_get_author(commit));
959 if (author0 == NULL) {
960 err = got_error_from_errno();
964 smallerthan = strchr(author, '<');
965 if (smallerthan && smallerthan[1] != '\0')
966 author = smallerthan + 1;
967 at = strchr(author, '@');
971 err = format_line(&wauthor, &author_width, author, limit);
974 waddwstr(view->window, wauthor);
976 while (col <= avail && author_width < author_display_cols + 1) {
977 waddch(view->window, ' ');
984 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
985 if (logmsg0 == NULL) {
986 err = got_error_from_errno();
990 while (*logmsg == '\n')
992 newline = strchr(logmsg, '\n');
996 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
999 waddwstr(view->window, wlogmsg);
1000 col += logmsg_width;
1001 while (col <= avail) {
1002 waddch(view->window, ' ');
1014 static struct commit_queue_entry *
1015 alloc_commit_queue_entry(struct got_commit_object *commit,
1016 struct got_object_id *id)
1018 struct commit_queue_entry *entry;
1020 entry = calloc(1, sizeof(*entry));
1025 entry->commit = commit;
1030 pop_commit(struct commit_queue *commits)
1032 struct commit_queue_entry *entry;
1034 entry = TAILQ_FIRST(&commits->head);
1035 TAILQ_REMOVE(&commits->head, entry, entry);
1036 got_object_commit_close(entry->commit);
1037 commits->ncommits--;
1038 /* Don't free entry->id! It is owned by the commit graph. */
1043 free_commits(struct commit_queue *commits)
1045 while (!TAILQ_EMPTY(&commits->head))
1046 pop_commit(commits);
1049 static const struct got_error *
1050 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1051 int minqueue, struct got_repository *repo, const char *path)
1053 const struct got_error *err = NULL;
1057 * We keep all commits open throughout the lifetime of the log
1058 * view in order to avoid having to re-fetch commits from disk
1059 * while updating the display.
1061 while (nqueued < minqueue) {
1062 struct got_object_id *id;
1063 struct got_commit_object *commit;
1064 struct commit_queue_entry *entry;
1067 err = got_commit_graph_iter_next(&id, graph);
1069 if (err->code != GOT_ERR_ITER_NEED_MORE)
1071 err = got_commit_graph_fetch_commits(graph,
1081 err = got_object_open_as_commit(&commit, repo, id);
1084 entry = alloc_commit_queue_entry(commit, id);
1085 if (entry == NULL) {
1086 err = got_error_from_errno();
1090 errcode = pthread_mutex_lock(&tog_mutex);
1092 err = got_error_set_errno(errcode);
1096 entry->idx = commits->ncommits;
1097 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1099 commits->ncommits++;
1101 errcode = pthread_mutex_unlock(&tog_mutex);
1102 if (errcode && err == NULL)
1103 err = got_error_set_errno(errcode);
1109 static const struct got_error *
1110 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_reference *head_ref;
1117 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1121 err = got_ref_resolve(head_id, repo, head_ref);
1122 got_ref_close(head_ref);
1131 static const struct got_error *
1132 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1133 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1134 struct commit_queue *commits, int selected_idx, int limit,
1135 struct got_reflist_head *refs, const char *path, int commits_needed)
1137 const struct got_error *err = NULL;
1138 struct commit_queue_entry *entry;
1139 int ncommits, width;
1140 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1141 char *refs_str = NULL;
1147 if (ncommits == selected_idx) {
1151 entry = TAILQ_NEXT(entry, entry);
1156 err = got_object_id_str(&id_str, (*selected)->id);
1160 err = build_refs_str(&refs_str, refs, (*selected)->id);
1166 if (commits_needed == 0)
1167 halfdelay(10); /* disable fast refresh */
1169 if (asprintf(&ncommits_str, " [%d/%d] %s",
1170 entry ? entry->idx + 1 : 0, commits->ncommits,
1171 commits_needed > 0 ? "loading... " :
1172 (refs_str ? refs_str : "")) == -1) {
1173 err = got_error_from_errno();
1177 if (path && strcmp(path, "/") != 0) {
1178 if (asprintf(&header, "commit %s %s%s",
1179 id_str ? id_str : "........................................",
1180 path, ncommits_str) == -1) {
1181 err = got_error_from_errno();
1185 } else if (asprintf(&header, "commit %s%s",
1186 id_str ? id_str : "........................................",
1187 ncommits_str) == -1) {
1188 err = got_error_from_errno();
1192 err = format_line(&wline, &width, header, view->ncols);
1196 werase(view->window);
1198 if (view_needs_focus_indication(view))
1199 wstandout(view->window);
1200 waddwstr(view->window, wline);
1201 while (width < view->ncols) {
1202 waddch(view->window, ' ');
1205 if (view_needs_focus_indication(view))
1206 wstandend(view->window);
1215 if (ncommits >= limit - 1)
1217 if (ncommits == selected_idx)
1218 wstandout(view->window);
1219 err = draw_commit(view, entry->commit, entry->id, refs);
1220 if (ncommits == selected_idx)
1221 wstandend(view->window);
1226 entry = TAILQ_NEXT(entry, entry);
1239 scroll_up(struct tog_view *view,
1240 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1241 struct commit_queue *commits)
1243 struct commit_queue_entry *entry;
1246 entry = TAILQ_FIRST(&commits->head);
1247 if (*first_displayed_entry == entry) {
1252 entry = *first_displayed_entry;
1253 while (entry && nscrolled < maxscroll) {
1254 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1256 *first_displayed_entry = entry;
1262 static const struct got_error *
1263 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1264 pthread_cond_t *need_commits)
1269 halfdelay(1); /* fast refresh while loading commits */
1271 while (*commits_needed > 0) {
1275 /* Wake the log thread. */
1276 errcode = pthread_cond_signal(need_commits);
1278 return got_error_set_errno(errcode);
1279 errcode = pthread_mutex_unlock(&tog_mutex);
1281 return got_error_set_errno(errcode);
1283 errcode = pthread_mutex_lock(&tog_mutex);
1285 return got_error_set_errno(errcode);
1287 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1289 * Thread is not done yet; lose a key press
1290 * and let the user retry... this way the GUI
1291 * remains interactive while logging deep paths
1292 * with few commits in history.
1301 static const struct got_error *
1302 scroll_down(struct tog_view *view,
1303 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1304 struct commit_queue_entry **last_displayed_entry,
1305 struct commit_queue *commits, int *log_complete, int *commits_needed,
1306 pthread_cond_t *need_commits)
1308 const struct got_error *err = NULL;
1309 struct commit_queue_entry *pentry;
1312 if (*last_displayed_entry == NULL)
1315 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1316 if (pentry == NULL && !*log_complete) {
1318 * Ask the log thread for required amount of commits
1319 * plus some amount of pre-fetching.
1321 (*commits_needed) += maxscroll + 20;
1322 err = trigger_log_thread(0, commits_needed, log_complete,
1329 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1330 if (pentry == NULL) {
1336 *last_displayed_entry = pentry;
1338 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1341 *first_displayed_entry = pentry;
1342 } while (++nscrolled < maxscroll);
1347 static const struct got_error *
1348 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1349 struct got_commit_object *commit, struct got_object_id *commit_id,
1350 struct tog_view *log_view, struct got_reflist_head *refs,
1351 struct got_repository *repo)
1353 const struct got_error *err;
1354 struct got_object_qid *parent_id;
1355 struct tog_view *diff_view;
1357 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1358 if (diff_view == NULL)
1359 return got_error_from_errno();
1361 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1362 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1363 commit_id, log_view, refs, repo);
1365 *new_view = diff_view;
1369 static const struct got_error *
1370 browse_commit(struct tog_view **new_view, int begin_x,
1371 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1372 struct got_repository *repo)
1374 const struct got_error *err = NULL;
1375 struct got_tree_object *tree;
1376 struct tog_view *tree_view;
1378 err = got_object_open_as_tree(&tree, repo,
1379 got_object_commit_get_tree_id(entry->commit));
1383 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1384 if (tree_view == NULL)
1385 return got_error_from_errno();
1387 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1389 got_object_tree_close(tree);
1391 *new_view = tree_view;
1396 log_thread(void *arg)
1398 const struct got_error *err = NULL;
1400 struct tog_log_thread_args *a = arg;
1403 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1407 while (!done && !err) {
1408 err = queue_commits(a->graph, a->commits, 1, a->repo,
1411 if (err->code != GOT_ERR_ITER_COMPLETED)
1415 } else if (a->commits_needed > 0)
1416 a->commits_needed--;
1418 errcode = pthread_mutex_lock(&tog_mutex);
1420 return (void *)got_error_set_errno(errcode);
1423 a->log_complete = 1;
1424 else if (*a->quit) {
1426 a->log_complete = 1;
1427 } else if (*a->first_displayed_entry == NULL) {
1428 *a->first_displayed_entry =
1429 TAILQ_FIRST(&a->commits->head);
1430 *a->selected_entry = *a->first_displayed_entry;
1434 a->commits_needed = 0;
1435 else if (a->commits_needed == 0) {
1436 errcode = pthread_cond_wait(&a->need_commits,
1439 err = got_error_set_errno(errcode);
1442 errcode = pthread_mutex_unlock(&tog_mutex);
1443 if (errcode && err == NULL)
1444 err = got_error_set_errno(errcode);
1449 static const struct got_error *
1450 stop_log_thread(struct tog_log_view_state *s)
1452 const struct got_error *err = NULL;
1457 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1459 return got_error_set_errno(errcode);
1460 errcode = pthread_mutex_unlock(&tog_mutex);
1462 return got_error_set_errno(errcode);
1463 errcode = pthread_join(s->thread, (void **)&err);
1465 return got_error_set_errno(errcode);
1466 errcode = pthread_mutex_lock(&tog_mutex);
1468 return got_error_set_errno(errcode);
1472 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1473 if (errcode && err == NULL)
1474 err = got_error_set_errno(errcode);
1476 if (s->thread_args.repo) {
1477 got_repo_close(s->thread_args.repo);
1478 s->thread_args.repo = NULL;
1481 if (s->thread_args.graph) {
1482 got_commit_graph_close(s->thread_args.graph);
1483 s->thread_args.graph = NULL;
1489 static const struct got_error *
1490 close_log_view(struct tog_view *view)
1492 const struct got_error *err = NULL;
1493 struct tog_log_view_state *s = &view->state.log;
1495 err = stop_log_thread(s);
1496 free_commits(&s->commits);
1497 free(s->in_repo_path);
1498 s->in_repo_path = NULL;
1504 static const struct got_error *
1505 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1506 struct got_reflist_head *refs, struct got_repository *repo,
1507 const char *path, int check_disk)
1509 const struct got_error *err = NULL;
1510 struct tog_log_view_state *s = &view->state.log;
1511 struct got_repository *thread_repo = NULL;
1512 struct got_commit_graph *thread_graph = NULL;
1515 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1519 /* The commit queue only contains commits being displayed. */
1520 TAILQ_INIT(&s->commits.head);
1521 s->commits.ncommits = 0;
1525 s->start_id = got_object_id_dup(start_id);
1526 if (s->start_id == NULL) {
1527 err = got_error_from_errno();
1531 view->show = show_log_view;
1532 view->input = input_log_view;
1533 view->close = close_log_view;
1535 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1538 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1543 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1545 err = got_error_set_errno(errcode);
1549 s->thread_args.commits_needed = view->nlines;
1550 s->thread_args.graph = thread_graph;
1551 s->thread_args.commits = &s->commits;
1552 s->thread_args.in_repo_path = s->in_repo_path;
1553 s->thread_args.start_id = s->start_id;
1554 s->thread_args.repo = thread_repo;
1555 s->thread_args.log_complete = 0;
1556 s->thread_args.quit = &s->quit;
1557 s->thread_args.view = view;
1558 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1559 s->thread_args.selected_entry = &s->selected_entry;
1562 close_log_view(view);
1566 static const struct got_error *
1567 show_log_view(struct tog_view *view)
1569 struct tog_log_view_state *s = &view->state.log;
1571 if (s->thread == NULL) {
1572 int errcode = pthread_create(&s->thread, NULL, log_thread,
1575 return got_error_set_errno(errcode);
1578 return draw_commits(view, &s->last_displayed_entry,
1579 &s->selected_entry, s->first_displayed_entry,
1580 &s->commits, s->selected, view->nlines, s->refs,
1581 s->in_repo_path, s->thread_args.commits_needed);
1584 static const struct got_error *
1585 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1586 struct tog_view **focus_view, struct tog_view *view, int ch)
1588 const struct got_error *err = NULL;
1589 struct tog_log_view_state *s = &view->state.log;
1591 struct tog_view *diff_view = NULL, *tree_view = NULL;
1602 if (s->first_displayed_entry == NULL)
1604 if (s->selected > 0)
1606 if (s->selected > 0)
1608 scroll_up(view, &s->first_displayed_entry, 1,
1612 if (s->first_displayed_entry == NULL)
1614 if (TAILQ_FIRST(&s->commits.head) ==
1615 s->first_displayed_entry) {
1616 if (s->selected == 0) {
1623 scroll_up(view, &s->first_displayed_entry,
1624 view->nlines, &s->commits);
1630 if (s->first_displayed_entry == NULL)
1632 if (s->selected < MIN(view->nlines - 2,
1633 s->commits.ncommits - 1)) {
1637 err = scroll_down(view, &s->first_displayed_entry, 1,
1638 &s->last_displayed_entry, &s->commits,
1639 &s->thread_args.log_complete,
1640 &s->thread_args.commits_needed,
1641 &s->thread_args.need_commits);
1644 struct commit_queue_entry *first;
1645 first = s->first_displayed_entry;
1648 err = scroll_down(view, &s->first_displayed_entry,
1649 view->nlines, &s->last_displayed_entry,
1650 &s->commits, &s->thread_args.log_complete,
1651 &s->thread_args.commits_needed,
1652 &s->thread_args.need_commits);
1653 if (first == s->first_displayed_entry &&
1654 s->selected < MIN(view->nlines - 2,
1655 s->commits.ncommits - 1)) {
1656 /* can't scroll further down */
1657 s->selected = MIN(view->nlines - 2,
1658 s->commits.ncommits - 1);
1664 if (s->selected > view->nlines - 2)
1665 s->selected = view->nlines - 2;
1666 if (s->selected > s->commits.ncommits - 1)
1667 s->selected = s->commits.ncommits - 1;
1671 if (s->selected_entry == NULL)
1673 if (view_is_parent_view(view))
1674 begin_x = view_split_begin_x(view->begin_x);
1675 err = open_diff_view_for_commit(&diff_view, begin_x,
1676 s->selected_entry->commit, s->selected_entry->id,
1677 view, s->refs, s->repo);
1680 if (view_is_parent_view(view)) {
1681 err = view_close_child(view);
1684 err = view_set_child(view, diff_view);
1686 view_close(diff_view);
1689 *focus_view = diff_view;
1690 view->child_focussed = 1;
1692 *new_view = diff_view;
1695 if (s->selected_entry == NULL)
1697 if (view_is_parent_view(view))
1698 begin_x = view_split_begin_x(view->begin_x);
1699 err = browse_commit(&tree_view, begin_x,
1700 s->selected_entry, s->refs, s->repo);
1703 if (view_is_parent_view(view)) {
1704 err = view_close_child(view);
1707 err = view_set_child(view, tree_view);
1709 view_close(tree_view);
1712 *focus_view = tree_view;
1713 view->child_focussed = 1;
1715 *new_view = tree_view;
1718 if (strcmp(s->in_repo_path, "/") == 0)
1720 parent_path = dirname(s->in_repo_path);
1721 if (parent_path && strcmp(parent_path, ".") != 0) {
1722 struct tog_view *lv;
1723 err = stop_log_thread(s);
1726 lv = view_open(view->nlines, view->ncols,
1727 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1729 return got_error_from_errno();
1730 err = open_log_view(lv, s->start_id, s->refs,
1731 s->repo, parent_path, 0);
1734 if (view_is_parent_view(view))
1737 view_set_child(view->parent, lv);
1750 static const struct got_error *
1751 apply_unveil(const char *repo_path, const char *worktree_path)
1753 const struct got_error *error;
1755 if (repo_path && unveil(repo_path, "r") != 0)
1756 return got_error_from_errno();
1758 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1759 return got_error_from_errno();
1761 if (unveil("/tmp", "rwc") != 0)
1762 return got_error_from_errno();
1764 error = got_privsep_unveil_exec_helpers();
1768 if (unveil(NULL, NULL) != 0)
1769 return got_error_from_errno();
1779 halfdelay(1); /* Do fast refresh while initial view is loading. */
1782 intrflush(stdscr, FALSE);
1783 keypad(stdscr, TRUE);
1785 signal(SIGWINCH, tog_sigwinch);
1788 static const struct got_error *
1789 cmd_log(int argc, char *argv[])
1791 const struct got_error *error;
1792 struct got_repository *repo = NULL;
1793 struct got_worktree *worktree = NULL;
1794 struct got_reflist_head refs;
1795 struct got_object_id *start_id = NULL;
1796 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1797 char *start_commit = NULL;
1799 struct tog_view *view;
1802 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1807 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1810 start_commit = optarg;
1813 repo_path = realpath(optarg, NULL);
1814 if (repo_path == NULL)
1815 err(1, "-r option");
1829 path = strdup(argv[0]);
1833 return got_error_from_errno();
1835 cwd = getcwd(NULL, 0);
1837 error = got_error_from_errno();
1840 if (repo_path == NULL) {
1841 error = got_worktree_open(&worktree, cwd);
1842 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1846 strdup(got_worktree_get_repo_path(worktree));
1848 repo_path = strdup(cwd);
1849 if (repo_path == NULL) {
1850 error = got_error_from_errno();
1857 error = apply_unveil(repo_path,
1858 worktree ? got_worktree_get_root_path(worktree) : NULL);
1862 error = got_repo_open(&repo, repo_path);
1866 if (start_commit == NULL)
1867 error = get_head_commit_id(&start_id, repo);
1869 error = got_object_resolve_id_str(&start_id, repo,
1874 SIMPLEQ_INIT(&refs);
1875 error = got_ref_list(&refs, repo);
1879 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1881 error = got_error_from_errno();
1884 error = open_log_view(view, start_id, &refs, repo, path, 1);
1887 error = view_loop(view);
1894 got_repo_close(repo);
1896 got_worktree_close(worktree);
1904 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1910 parse_next_line(FILE *f, size_t *len)
1915 const char delim[3] = { '\0', '\0', '\0'};
1917 line = fparseln(f, &linelen, &lineno, delim, 0);
1923 static const struct got_error *
1924 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1925 int *last_displayed_line, int *eof, int max_lines,
1928 const struct got_error *err;
1929 int nlines = 0, nprinted = 0;
1936 werase(view->window);
1939 err = format_line(&wline, &width, header, view->ncols);
1944 if (view_needs_focus_indication(view))
1945 wstandout(view->window);
1946 waddwstr(view->window, wline);
1947 if (view_needs_focus_indication(view))
1948 wstandend(view->window);
1949 if (width < view->ncols)
1950 waddch(view->window, '\n');
1958 while (nprinted < max_lines) {
1959 line = parse_next_line(f, &len);
1964 if (++nlines < *first_displayed_line) {
1969 err = format_line(&wline, &width, line, view->ncols);
1974 waddwstr(view->window, wline);
1975 if (width < view->ncols)
1976 waddch(view->window, '\n');
1977 if (++nprinted == 1)
1978 *first_displayed_line = nlines;
1983 *last_displayed_line = nlines;
1991 get_datestr(time_t *time, char *datebuf)
1993 char *p, *s = ctime_r(time, datebuf);
1994 p = strchr(s, '\n');
2000 static const struct got_error *
2001 write_commit_info(struct got_object_id *commit_id,
2002 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2004 const struct got_error *err = NULL;
2006 struct got_commit_object *commit;
2007 char *id_str = NULL;
2008 time_t committer_time;
2009 const char *author, *committer;
2010 char *refs_str = NULL;
2013 err = build_refs_str(&refs_str, refs, commit_id);
2018 err = got_object_open_as_commit(&commit, repo, commit_id);
2022 err = got_object_id_str(&id_str, commit_id);
2024 err = got_error_from_errno();
2028 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2029 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2030 err = got_error_from_errno();
2033 if (fprintf(outfile, "from: %s\n",
2034 got_object_commit_get_author(commit)) < 0) {
2035 err = got_error_from_errno();
2038 committer_time = got_object_commit_get_committer_time(commit);
2039 if (fprintf(outfile, "date: %s UTC\n",
2040 get_datestr(&committer_time, datebuf)) < 0) {
2041 err = got_error_from_errno();
2044 author = got_object_commit_get_author(commit);
2045 committer = got_object_commit_get_committer(commit);
2046 if (strcmp(author, committer) != 0 &&
2047 fprintf(outfile, "via: %s\n", committer) < 0) {
2048 err = got_error_from_errno();
2051 if (fprintf(outfile, "%s\n",
2052 got_object_commit_get_logmsg(commit)) < 0) {
2053 err = got_error_from_errno();
2059 got_object_commit_close(commit);
2063 static const struct got_error *
2064 create_diff(struct tog_diff_view_state *s)
2066 const struct got_error *err = NULL;
2072 err = got_error_from_errno();
2075 if (s->f && fclose(s->f) != 0) {
2076 err = got_error_from_errno();
2082 err = got_object_get_type(&obj_type, s->repo, s->id1);
2084 err = got_object_get_type(&obj_type, s->repo, s->id2);
2089 case GOT_OBJ_TYPE_BLOB:
2090 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2091 s->diff_context, s->repo, f);
2093 case GOT_OBJ_TYPE_TREE:
2094 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2095 s->diff_context, s->repo, f);
2097 case GOT_OBJ_TYPE_COMMIT: {
2098 const struct got_object_id_queue *parent_ids;
2099 struct got_object_qid *pid;
2100 struct got_commit_object *commit2;
2102 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2105 /* Show commit info if we're diffing to a parent/root commit. */
2107 write_commit_info(s->id2, s->refs, s->repo, f);
2109 parent_ids = got_object_commit_get_parent_ids(commit2);
2110 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2111 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2112 write_commit_info(s->id2, s->refs,
2118 got_object_commit_close(commit2);
2120 err = got_diff_objects_as_commits(s->id1, s->id2,
2121 s->diff_context, s->repo, f);
2125 err = got_error(GOT_ERR_OBJ_TYPE);
2129 if (f && fflush(f) != 0 && err == NULL)
2130 err = got_error_from_errno();
2135 diff_view_indicate_progress(struct tog_view *view)
2137 werase(view->window);
2138 waddstr(view->window, "diffing...");
2143 static const struct got_error *
2144 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2145 struct got_object_id *id2, struct tog_view *log_view,
2146 struct got_reflist_head *refs, struct got_repository *repo)
2148 const struct got_error *err;
2150 if (id1 != NULL && id2 != NULL) {
2152 err = got_object_get_type(&type1, repo, id1);
2155 err = got_object_get_type(&type2, repo, id2);
2160 return got_error(GOT_ERR_OBJ_TYPE);
2164 view->state.diff.id1 = got_object_id_dup(id1);
2165 if (view->state.diff.id1 == NULL)
2166 return got_error_from_errno();
2168 view->state.diff.id1 = NULL;
2170 view->state.diff.id2 = got_object_id_dup(id2);
2171 if (view->state.diff.id2 == NULL) {
2172 free(view->state.diff.id1);
2173 view->state.diff.id1 = NULL;
2174 return got_error_from_errno();
2176 view->state.diff.f = NULL;
2177 view->state.diff.first_displayed_line = 1;
2178 view->state.diff.last_displayed_line = view->nlines;
2179 view->state.diff.diff_context = 3;
2180 view->state.diff.log_view = log_view;
2181 view->state.diff.repo = repo;
2182 view->state.diff.refs = refs;
2184 if (log_view && view_is_splitscreen(view))
2185 show_log_view(log_view); /* draw vborder */
2186 diff_view_indicate_progress(view);
2188 err = create_diff(&view->state.diff);
2190 free(view->state.diff.id1);
2191 view->state.diff.id1 = NULL;
2192 free(view->state.diff.id2);
2193 view->state.diff.id2 = NULL;
2197 view->show = show_diff_view;
2198 view->input = input_diff_view;
2199 view->close = close_diff_view;
2204 static const struct got_error *
2205 close_diff_view(struct tog_view *view)
2207 const struct got_error *err = NULL;
2209 free(view->state.diff.id1);
2210 view->state.diff.id1 = NULL;
2211 free(view->state.diff.id2);
2212 view->state.diff.id2 = NULL;
2213 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2214 err = got_error_from_errno();
2218 static const struct got_error *
2219 show_diff_view(struct tog_view *view)
2221 const struct got_error *err;
2222 struct tog_diff_view_state *s = &view->state.diff;
2223 char *id_str1 = NULL, *id_str2, *header;
2226 err = got_object_id_str(&id_str1, s->id1);
2230 err = got_object_id_str(&id_str2, s->id2);
2234 if (asprintf(&header, "diff %s %s",
2235 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2236 err = got_error_from_errno();
2244 return draw_file(view, s->f, &s->first_displayed_line,
2245 &s->last_displayed_line, &s->eof, view->nlines,
2249 static const struct got_error *
2250 set_selected_commit(struct tog_diff_view_state *s,
2251 struct commit_queue_entry *entry)
2253 const struct got_error *err;
2254 const struct got_object_id_queue *parent_ids;
2255 struct got_commit_object *selected_commit;
2256 struct got_object_qid *pid;
2259 s->id2 = got_object_id_dup(entry->id);
2261 return got_error_from_errno();
2263 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2266 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2268 pid = SIMPLEQ_FIRST(parent_ids);
2269 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2270 got_object_commit_close(selected_commit);
2274 static const struct got_error *
2275 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2276 struct tog_view **focus_view, struct tog_view *view, int ch)
2278 const struct got_error *err = NULL;
2279 struct tog_diff_view_state *s = &view->state.diff;
2280 struct tog_log_view_state *ls;
2281 struct commit_queue_entry *entry;
2287 if (s->first_displayed_line > 1)
2288 s->first_displayed_line--;
2293 if (s->first_displayed_line == 1) {
2298 while (i++ < view->nlines - 1 &&
2299 s->first_displayed_line > 1)
2300 s->first_displayed_line--;
2305 s->first_displayed_line++;
2316 while (!s->eof && i++ < view->nlines - 1) {
2318 line = parse_next_line(s->f, NULL);
2319 s->first_displayed_line++;
2325 if (s->diff_context > 0) {
2327 diff_view_indicate_progress(view);
2328 err = create_diff(s);
2332 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2334 diff_view_indicate_progress(view);
2335 err = create_diff(s);
2340 if (s->log_view == NULL)
2342 ls = &s->log_view->state.log;
2343 entry = TAILQ_PREV(ls->selected_entry,
2344 commit_queue_head, entry);
2348 err = input_log_view(NULL, NULL, NULL, s->log_view,
2353 err = set_selected_commit(s, entry);
2357 s->first_displayed_line = 1;
2358 s->last_displayed_line = view->nlines;
2360 diff_view_indicate_progress(view);
2361 err = create_diff(s);
2365 if (s->log_view == NULL)
2367 ls = &s->log_view->state.log;
2369 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2370 ls->thread_args.commits_needed++;
2372 /* Display "loading..." in log view. */
2373 show_log_view(s->log_view);
2377 err = trigger_log_thread(1 /* load_all */,
2378 &ls->thread_args.commits_needed,
2379 &ls->thread_args.log_complete,
2380 &ls->thread_args.need_commits);
2384 err = input_log_view(NULL, NULL, NULL, s->log_view,
2389 entry = TAILQ_NEXT(ls->selected_entry, entry);
2393 err = set_selected_commit(s, entry);
2397 s->first_displayed_line = 1;
2398 s->last_displayed_line = view->nlines;
2400 diff_view_indicate_progress(view);
2401 err = create_diff(s);
2410 static const struct got_error *
2411 cmd_diff(int argc, char *argv[])
2413 const struct got_error *error = NULL;
2414 struct got_repository *repo = NULL;
2415 struct got_reflist_head refs;
2416 struct got_object_id *id1 = NULL, *id2 = NULL;
2417 char *repo_path = NULL;
2418 char *id_str1 = NULL, *id_str2 = NULL;
2420 struct tog_view *view;
2423 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2428 while ((ch = getopt(argc, argv, "")) != -1) {
2440 usage_diff(); /* TODO show local worktree changes */
2441 } else if (argc == 2) {
2442 repo_path = getcwd(NULL, 0);
2443 if (repo_path == NULL)
2444 return got_error_from_errno();
2447 } else if (argc == 3) {
2448 repo_path = realpath(argv[0], NULL);
2449 if (repo_path == NULL)
2450 return got_error_from_errno();
2458 error = apply_unveil(repo_path, NULL);
2462 error = got_repo_open(&repo, repo_path);
2467 error = got_object_resolve_id_str(&id1, repo, id_str1);
2471 error = got_object_resolve_id_str(&id2, repo, id_str2);
2475 SIMPLEQ_INIT(&refs);
2476 error = got_ref_list(&refs, repo);
2480 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2482 error = got_error_from_errno();
2485 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2488 error = view_loop(view);
2490 got_repo_close(repo);
2498 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2503 struct tog_blame_line {
2505 struct got_object_id *id;
2508 static const struct got_error *
2509 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2510 const char *path, struct tog_blame_line *lines, int nlines,
2511 int blame_complete, int selected_line, int *first_displayed_line,
2512 int *last_displayed_line, int *eof, int max_lines)
2514 const struct got_error *err;
2515 int lineno = 0, nprinted = 0;
2520 struct tog_blame_line *blame_line;
2521 struct got_object_id *prev_id = NULL;
2524 err = got_object_id_str(&id_str, id);
2529 werase(view->window);
2531 if (asprintf(&line, "commit %s", id_str) == -1) {
2532 err = got_error_from_errno();
2537 err = format_line(&wline, &width, line, view->ncols);
2540 if (view_needs_focus_indication(view))
2541 wstandout(view->window);
2542 waddwstr(view->window, wline);
2543 if (view_needs_focus_indication(view))
2544 wstandend(view->window);
2547 if (width < view->ncols)
2548 waddch(view->window, '\n');
2550 if (asprintf(&line, "[%d/%d] %s%s",
2551 *first_displayed_line - 1 + selected_line, nlines,
2552 blame_complete ? "" : "annotating... ", path) == -1) {
2554 return got_error_from_errno();
2557 err = format_line(&wline, &width, line, view->ncols);
2562 waddwstr(view->window, wline);
2565 if (width < view->ncols)
2566 waddch(view->window, '\n');
2569 while (nprinted < max_lines - 2) {
2570 line = parse_next_line(f, &len);
2575 if (++lineno < *first_displayed_line) {
2580 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2581 err = format_line(&wline, &width, line, wlimit);
2587 if (view->focussed && nprinted == selected_line - 1)
2588 wstandout(view->window);
2590 blame_line = &lines[lineno - 1];
2591 if (blame_line->annotated && prev_id &&
2592 got_object_id_cmp(prev_id, blame_line->id) == 0)
2593 waddstr(view->window, " ");
2594 else if (blame_line->annotated) {
2596 err = got_object_id_str(&id_str, blame_line->id);
2602 wprintw(view->window, "%.8s ", id_str);
2604 prev_id = blame_line->id;
2606 waddstr(view->window, "........ ");
2610 waddwstr(view->window, wline);
2611 while (width < wlimit) {
2612 waddch(view->window, ' ');
2615 if (view->focussed && nprinted == selected_line - 1)
2616 wstandend(view->window);
2617 if (++nprinted == 1)
2618 *first_displayed_line = lineno;
2623 *last_displayed_line = lineno;
2630 static const struct got_error *
2631 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2633 const struct got_error *err = NULL;
2634 struct tog_blame_cb_args *a = arg;
2635 struct tog_blame_line *line;
2638 if (nlines != a->nlines ||
2639 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2640 return got_error(GOT_ERR_RANGE);
2642 errcode = pthread_mutex_lock(&tog_mutex);
2644 return got_error_set_errno(errcode);
2646 if (*a->quit) { /* user has quit the blame view */
2647 err = got_error(GOT_ERR_ITER_COMPLETED);
2652 goto done; /* no change in this commit */
2654 line = &a->lines[lineno - 1];
2655 if (line->annotated)
2658 line->id = got_object_id_dup(id);
2659 if (line->id == NULL) {
2660 err = got_error_from_errno();
2663 line->annotated = 1;
2665 errcode = pthread_mutex_unlock(&tog_mutex);
2667 err = got_error_set_errno(errcode);
2672 blame_thread(void *arg)
2674 const struct got_error *err;
2675 struct tog_blame_thread_args *ta = arg;
2676 struct tog_blame_cb_args *a = ta->cb_args;
2679 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2680 blame_cb, ta->cb_args);
2682 errcode = pthread_mutex_lock(&tog_mutex);
2684 return (void *)got_error_set_errno(errcode);
2686 got_repo_close(ta->repo);
2690 errcode = pthread_mutex_unlock(&tog_mutex);
2691 if (errcode && err == NULL)
2692 err = got_error_set_errno(errcode);
2697 static struct got_object_id *
2698 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2701 struct tog_blame_line *line;
2703 line = &lines[first_displayed_line - 1 + selected_line - 1];
2704 if (!line->annotated)
2710 static const struct got_error *
2711 stop_blame(struct tog_blame *blame)
2713 const struct got_error *err = NULL;
2716 if (blame->thread) {
2718 errcode = pthread_mutex_unlock(&tog_mutex);
2720 return got_error_set_errno(errcode);
2721 errcode = pthread_join(blame->thread, (void **)&err);
2723 return got_error_set_errno(errcode);
2724 errcode = pthread_mutex_lock(&tog_mutex);
2726 return got_error_set_errno(errcode);
2727 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2729 blame->thread = NULL;
2731 if (blame->thread_args.repo) {
2732 got_repo_close(blame->thread_args.repo);
2733 blame->thread_args.repo = NULL;
2736 if (fclose(blame->f) != 0 && err == NULL)
2737 err = got_error_from_errno();
2741 for (i = 0; i < blame->nlines; i++)
2742 free(blame->lines[i].id);
2744 blame->lines = NULL;
2746 free(blame->cb_args.commit_id);
2747 blame->cb_args.commit_id = NULL;
2752 static const struct got_error *
2753 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2754 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2755 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2756 struct got_repository *repo)
2758 const struct got_error *err = NULL;
2759 struct got_blob_object *blob = NULL;
2760 struct got_repository *thread_repo = NULL;
2761 struct got_object_id *obj_id = NULL;
2764 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2768 return got_error(GOT_ERR_NO_OBJ);
2770 err = got_object_get_type(&obj_type, repo, obj_id);
2774 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2775 err = got_error(GOT_ERR_OBJ_TYPE);
2779 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2782 blame->f = got_opentemp();
2783 if (blame->f == NULL) {
2784 err = got_error_from_errno();
2787 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2792 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2793 if (blame->lines == NULL) {
2794 err = got_error_from_errno();
2798 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2802 blame->cb_args.view = view;
2803 blame->cb_args.lines = blame->lines;
2804 blame->cb_args.nlines = blame->nlines;
2805 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2806 if (blame->cb_args.commit_id == NULL) {
2807 err = got_error_from_errno();
2810 blame->cb_args.quit = done;
2812 blame->thread_args.path = path;
2813 blame->thread_args.repo = thread_repo;
2814 blame->thread_args.cb_args = &blame->cb_args;
2815 blame->thread_args.complete = blame_complete;
2816 *blame_complete = 0;
2820 got_object_blob_close(blob);
2827 static const struct got_error *
2828 open_blame_view(struct tog_view *view, char *path,
2829 struct got_object_id *commit_id, struct got_reflist_head *refs,
2830 struct got_repository *repo)
2832 const struct got_error *err = NULL;
2833 struct tog_blame_view_state *s = &view->state.blame;
2835 SIMPLEQ_INIT(&s->blamed_commits);
2837 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2841 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2842 s->first_displayed_line = 1;
2843 s->last_displayed_line = view->nlines;
2844 s->selected_line = 1;
2845 s->blame_complete = 0;
2847 if (s->path == NULL)
2848 return got_error_from_errno();
2851 s->commit_id = commit_id;
2852 memset(&s->blame, 0, sizeof(s->blame));
2854 view->show = show_blame_view;
2855 view->input = input_blame_view;
2856 view->close = close_blame_view;
2858 return run_blame(&s->blame, view, &s->blame_complete,
2859 &s->first_displayed_line, &s->last_displayed_line,
2860 &s->selected_line, &s->done, &s->eof, s->path,
2861 s->blamed_commit->id, s->repo);
2864 static const struct got_error *
2865 close_blame_view(struct tog_view *view)
2867 const struct got_error *err = NULL;
2868 struct tog_blame_view_state *s = &view->state.blame;
2870 if (s->blame.thread)
2871 err = stop_blame(&s->blame);
2873 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2874 struct got_object_qid *blamed_commit;
2875 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2876 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2877 got_object_qid_free(blamed_commit);
2885 static const struct got_error *
2886 show_blame_view(struct tog_view *view)
2888 const struct got_error *err = NULL;
2889 struct tog_blame_view_state *s = &view->state.blame;
2892 if (s->blame.thread == NULL) {
2893 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2894 &s->blame.thread_args);
2896 return got_error_set_errno(errcode);
2899 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2900 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2901 s->selected_line, &s->first_displayed_line,
2902 &s->last_displayed_line, &s->eof, view->nlines);
2908 static const struct got_error *
2909 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2910 struct tog_view **focus_view, struct tog_view *view, int ch)
2912 const struct got_error *err = NULL, *thread_err = NULL;
2913 struct tog_view *diff_view;
2914 struct tog_blame_view_state *s = &view->state.blame;
2923 if (s->selected_line > 1)
2925 else if (s->selected_line == 1 &&
2926 s->first_displayed_line > 1)
2927 s->first_displayed_line--;
2932 if (s->first_displayed_line == 1) {
2933 if (s->selected_line == 1) {
2937 s->selected_line = 1;
2940 if (s->first_displayed_line > view->nlines - 2)
2941 s->first_displayed_line -=
2944 s->first_displayed_line = 1;
2948 if (s->selected_line < view->nlines - 2 &&
2949 s->first_displayed_line +
2950 s->selected_line <= s->blame.nlines)
2952 else if (s->last_displayed_line <
2954 s->first_displayed_line++;
2960 struct got_object_id *id = NULL;
2961 id = get_selected_commit_id(s->blame.lines,
2962 s->first_displayed_line, s->selected_line);
2966 struct got_commit_object *commit;
2967 struct got_object_qid *pid;
2968 struct got_object_id *blob_id = NULL;
2970 err = got_object_open_as_commit(&commit,
2974 pid = SIMPLEQ_FIRST(
2975 got_object_commit_get_parent_ids(commit));
2977 got_object_commit_close(commit);
2980 /* Check if path history ends here. */
2981 err = got_object_id_by_path(&blob_id, s->repo,
2984 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2986 got_object_commit_close(commit);
2989 err = got_object_get_type(&obj_type, s->repo,
2992 /* Can't blame non-blob type objects. */
2993 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2994 got_object_commit_close(commit);
2997 err = got_object_qid_alloc(&s->blamed_commit,
2999 got_object_commit_close(commit);
3001 if (got_object_id_cmp(id,
3002 s->blamed_commit->id) == 0)
3004 err = got_object_qid_alloc(&s->blamed_commit,
3010 thread_err = stop_blame(&s->blame);
3014 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3015 s->blamed_commit, entry);
3016 err = run_blame(&s->blame, view, &s->blame_complete,
3017 &s->first_displayed_line, &s->last_displayed_line,
3018 &s->selected_line, &s->done, &s->eof,
3019 s->path, s->blamed_commit->id, s->repo);
3025 struct got_object_qid *first;
3026 first = SIMPLEQ_FIRST(&s->blamed_commits);
3027 if (!got_object_id_cmp(first->id, s->commit_id))
3030 thread_err = stop_blame(&s->blame);
3034 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3035 got_object_qid_free(s->blamed_commit);
3037 SIMPLEQ_FIRST(&s->blamed_commits);
3038 err = run_blame(&s->blame, view, &s->blame_complete,
3039 &s->first_displayed_line, &s->last_displayed_line,
3040 &s->selected_line, &s->done, &s->eof, s->path,
3041 s->blamed_commit->id, s->repo);
3048 struct got_object_id *id = NULL;
3049 struct got_object_qid *pid;
3050 struct got_commit_object *commit = NULL;
3051 id = get_selected_commit_id(s->blame.lines,
3052 s->first_displayed_line, s->selected_line);
3055 err = got_object_open_as_commit(&commit, s->repo, id);
3058 pid = SIMPLEQ_FIRST(
3059 got_object_commit_get_parent_ids(commit));
3060 if (view_is_parent_view(view))
3061 begin_x = view_split_begin_x(view->begin_x);
3062 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3063 if (diff_view == NULL) {
3064 got_object_commit_close(commit);
3065 err = got_error_from_errno();
3068 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3069 id, NULL, s->refs, s->repo);
3070 got_object_commit_close(commit);
3072 view_close(diff_view);
3075 if (view_is_parent_view(view)) {
3076 err = view_close_child(view);
3079 err = view_set_child(view, diff_view);
3081 view_close(diff_view);
3084 *focus_view = diff_view;
3085 view->child_focussed = 1;
3087 *new_view = diff_view;
3094 if (s->last_displayed_line >= s->blame.nlines &&
3095 s->selected_line >= MIN(s->blame.nlines,
3096 view->nlines - 2)) {
3100 if (s->last_displayed_line >= s->blame.nlines &&
3101 s->selected_line < view->nlines - 2) {
3102 s->selected_line = MIN(s->blame.nlines,
3106 if (s->last_displayed_line + view->nlines - 2
3108 s->first_displayed_line +=
3111 s->first_displayed_line =
3116 if (s->selected_line > view->nlines - 2) {
3117 s->selected_line = MIN(s->blame.nlines,
3124 return thread_err ? thread_err : err;
3127 static const struct got_error *
3128 cmd_blame(int argc, char *argv[])
3130 const struct got_error *error;
3131 struct got_repository *repo = NULL;
3132 struct got_reflist_head refs;
3133 struct got_worktree *worktree = NULL;
3134 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3135 struct got_object_id *commit_id = NULL;
3136 char *commit_id_str = NULL;
3138 struct tog_view *view;
3141 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3146 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3149 commit_id_str = optarg;
3152 repo_path = realpath(optarg, NULL);
3153 if (repo_path == NULL)
3154 err(1, "-r option");
3170 cwd = getcwd(NULL, 0);
3172 error = got_error_from_errno();
3175 if (repo_path == NULL) {
3176 error = got_worktree_open(&worktree, cwd);
3177 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3183 strdup(got_worktree_get_repo_path(worktree));
3184 if (repo_path == NULL)
3185 error = got_error_from_errno();
3189 repo_path = strdup(cwd);
3190 if (repo_path == NULL) {
3191 error = got_error_from_errno();
3199 error = apply_unveil(repo_path, NULL);
3203 error = got_repo_open(&repo, repo_path);
3208 const char *prefix = got_worktree_get_path_prefix(worktree);
3209 char *p, *worktree_subdir = cwd +
3210 strlen(got_worktree_get_root_path(worktree));
3211 if (asprintf(&p, "%s%s%s%s%s",
3212 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3213 worktree_subdir, worktree_subdir[0] ? "/" : "",
3215 error = got_error_from_errno();
3218 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3221 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3226 if (commit_id_str == NULL) {
3227 struct got_reference *head_ref;
3228 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3231 error = got_ref_resolve(&commit_id, repo, head_ref);
3232 got_ref_close(head_ref);
3234 error = got_object_resolve_id_str(&commit_id, repo,
3240 SIMPLEQ_INIT(&refs);
3241 error = got_ref_list(&refs, repo);
3245 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3247 error = got_error_from_errno();
3250 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3253 error = view_loop(view);
3259 got_worktree_close(worktree);
3261 got_repo_close(repo);
3265 static const struct got_error *
3266 draw_tree_entries(struct tog_view *view,
3267 struct got_tree_entry **first_displayed_entry,
3268 struct got_tree_entry **last_displayed_entry,
3269 struct got_tree_entry **selected_entry, int *ndisplayed,
3270 const char *label, int show_ids, const char *parent_path,
3271 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3273 const struct got_error *err = NULL;
3274 struct got_tree_entry *te;
3280 werase(view->window);
3285 err = format_line(&wline, &width, label, view->ncols);
3288 if (view_needs_focus_indication(view))
3289 wstandout(view->window);
3290 waddwstr(view->window, wline);
3291 if (view_needs_focus_indication(view))
3292 wstandend(view->window);
3295 if (width < view->ncols)
3296 waddch(view->window, '\n');
3299 err = format_line(&wline, &width, parent_path, view->ncols);
3302 waddwstr(view->window, wline);
3305 if (width < view->ncols)
3306 waddch(view->window, '\n');
3309 waddch(view->window, '\n');
3313 te = SIMPLEQ_FIRST(&entries->head);
3314 if (*first_displayed_entry == NULL) {
3315 if (selected == 0) {
3317 wstandout(view->window);
3318 *selected_entry = NULL;
3320 waddstr(view->window, " ..\n"); /* parent directory */
3321 if (selected == 0 && view->focussed)
3322 wstandend(view->window);
3329 while (te != *first_displayed_entry)
3330 te = SIMPLEQ_NEXT(te, entry);
3334 char *line = NULL, *id_str = NULL;
3337 err = got_object_id_str(&id_str, te->id);
3339 return got_error_from_errno();
3341 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3342 te->name, S_ISDIR(te->mode) ? "/" :
3343 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3345 return got_error_from_errno();
3348 err = format_line(&wline, &width, line, view->ncols);
3353 if (n == selected) {
3355 wstandout(view->window);
3356 *selected_entry = te;
3358 waddwstr(view->window, wline);
3359 if (width < view->ncols)
3360 waddch(view->window, '\n');
3361 if (n == selected && view->focussed)
3362 wstandend(view->window);
3368 *last_displayed_entry = te;
3371 te = SIMPLEQ_NEXT(te, entry);
3378 tree_scroll_up(struct tog_view *view,
3379 struct got_tree_entry **first_displayed_entry, int maxscroll,
3380 const struct got_tree_entries *entries, int isroot)
3382 struct got_tree_entry *te, *prev;
3385 if (*first_displayed_entry == NULL) {
3390 te = SIMPLEQ_FIRST(&entries->head);
3391 if (*first_displayed_entry == te) {
3394 *first_displayed_entry = NULL;
3398 /* XXX this is stupid... switch to TAILQ? */
3399 for (i = 0; i < maxscroll; i++) {
3400 while (te != *first_displayed_entry) {
3402 te = SIMPLEQ_NEXT(te, entry);
3404 *first_displayed_entry = prev;
3405 te = SIMPLEQ_FIRST(&entries->head);
3407 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3408 *first_displayed_entry = NULL;
3412 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3413 struct got_tree_entry *last_displayed_entry,
3414 const struct got_tree_entries *entries)
3416 struct got_tree_entry *next, *last;
3419 if (*first_displayed_entry)
3420 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3422 next = SIMPLEQ_FIRST(&entries->head);
3423 last = last_displayed_entry;
3424 while (next && last && n++ < maxscroll) {
3425 last = SIMPLEQ_NEXT(last, entry);
3427 *first_displayed_entry = next;
3428 next = SIMPLEQ_NEXT(next, entry);
3434 static const struct got_error *
3435 tree_entry_path(char **path, struct tog_parent_trees *parents,
3436 struct got_tree_entry *te)
3438 const struct got_error *err = NULL;
3439 struct tog_parent_tree *pt;
3440 size_t len = 2; /* for leading slash and NUL */
3442 TAILQ_FOREACH(pt, parents, entry)
3443 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3445 len += strlen(te->name);
3447 *path = calloc(1, len);
3449 return got_error_from_errno();
3452 pt = TAILQ_LAST(parents, tog_parent_trees);
3454 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3455 err = got_error(GOT_ERR_NO_SPACE);
3458 if (strlcat(*path, "/", len) >= len) {
3459 err = got_error(GOT_ERR_NO_SPACE);
3462 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3465 if (strlcat(*path, te->name, len) >= len) {
3466 err = got_error(GOT_ERR_NO_SPACE);
3478 static const struct got_error *
3479 blame_tree_entry(struct tog_view **new_view, int begin_x,
3480 struct got_tree_entry *te, struct tog_parent_trees *parents,
3481 struct got_object_id *commit_id, struct got_reflist_head *refs,
3482 struct got_repository *repo)
3484 const struct got_error *err = NULL;
3486 struct tog_view *blame_view;
3488 err = tree_entry_path(&path, parents, te);
3492 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3493 if (blame_view == NULL)
3494 return got_error_from_errno();
3496 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3498 view_close(blame_view);
3501 *new_view = blame_view;
3505 static const struct got_error *
3506 log_tree_entry(struct tog_view **new_view, int begin_x,
3507 struct got_tree_entry *te, struct tog_parent_trees *parents,
3508 struct got_object_id *commit_id, struct got_reflist_head *refs,
3509 struct got_repository *repo)
3511 struct tog_view *log_view;
3512 const struct got_error *err = NULL;
3515 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3516 if (log_view == NULL)
3517 return got_error_from_errno();
3519 err = tree_entry_path(&path, parents, te);
3523 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3525 view_close(log_view);
3527 *new_view = log_view;
3532 static const struct got_error *
3533 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3534 struct got_object_id *commit_id, struct got_reflist_head *refs,
3535 struct got_repository *repo)
3537 const struct got_error *err = NULL;
3538 char *commit_id_str = NULL;
3539 struct tog_tree_view_state *s = &view->state.tree;
3541 TAILQ_INIT(&s->parents);
3543 err = got_object_id_str(&commit_id_str, commit_id);
3547 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3548 err = got_error_from_errno();
3552 s->root = s->tree = root;
3553 s->entries = got_object_tree_get_entries(root);
3554 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3555 s->commit_id = got_object_id_dup(commit_id);
3556 if (s->commit_id == NULL) {
3557 err = got_error_from_errno();
3563 view->show = show_tree_view;
3564 view->input = input_tree_view;
3565 view->close = close_tree_view;
3567 free(commit_id_str);
3569 free(s->tree_label);
3570 s->tree_label = NULL;
3575 static const struct got_error *
3576 close_tree_view(struct tog_view *view)
3578 struct tog_tree_view_state *s = &view->state.tree;
3580 free(s->tree_label);
3581 s->tree_label = NULL;
3583 s->commit_id = NULL;
3584 while (!TAILQ_EMPTY(&s->parents)) {
3585 struct tog_parent_tree *parent;
3586 parent = TAILQ_FIRST(&s->parents);
3587 TAILQ_REMOVE(&s->parents, parent, entry);
3591 if (s->tree != s->root)
3592 got_object_tree_close(s->tree);
3593 got_object_tree_close(s->root);
3598 static const struct got_error *
3599 show_tree_view(struct tog_view *view)
3601 const struct got_error *err = NULL;
3602 struct tog_tree_view_state *s = &view->state.tree;
3605 err = tree_entry_path(&parent_path, &s->parents, NULL);
3609 err = draw_tree_entries(view, &s->first_displayed_entry,
3610 &s->last_displayed_entry, &s->selected_entry,
3611 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3612 s->entries, s->selected, view->nlines, s->tree == s->root);
3619 static const struct got_error *
3620 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3621 struct tog_view **focus_view, struct tog_view *view, int ch)
3623 const struct got_error *err = NULL;
3624 struct tog_tree_view_state *s = &view->state.tree;
3625 struct tog_view *log_view;
3626 int begin_x = 0, nscrolled;
3630 s->show_ids = !s->show_ids;
3633 if (!s->selected_entry)
3635 if (view_is_parent_view(view))
3636 begin_x = view_split_begin_x(view->begin_x);
3637 err = log_tree_entry(&log_view, begin_x,
3638 s->selected_entry, &s->parents,
3639 s->commit_id, s->refs, s->repo);
3640 if (view_is_parent_view(view)) {
3641 err = view_close_child(view);
3644 err = view_set_child(view, log_view);
3646 view_close(log_view);
3649 *focus_view = log_view;
3650 view->child_focussed = 1;
3652 *new_view = log_view;
3656 if (s->selected > 0) {
3658 if (s->selected == 0)
3661 if (s->selected > 0)
3663 tree_scroll_up(view, &s->first_displayed_entry, 1,
3664 s->entries, s->tree == s->root);
3667 tree_scroll_up(view, &s->first_displayed_entry,
3668 MAX(0, view->nlines - 4 - s->selected), s->entries,
3669 s->tree == s->root);
3671 if (SIMPLEQ_FIRST(&s->entries->head) ==
3672 s->first_displayed_entry && s->tree != s->root)
3673 s->first_displayed_entry = NULL;
3677 if (s->selected < s->ndisplayed - 1) {
3681 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3683 /* can't scroll any further */
3687 tree_scroll_down(&s->first_displayed_entry, 1,
3688 s->last_displayed_entry, s->entries);
3691 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3693 /* can't scroll any further; move cursor down */
3694 if (s->selected < s->ndisplayed - 1)
3695 s->selected = s->ndisplayed - 1;
3700 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3701 view->nlines, s->last_displayed_entry, s->entries);
3702 if (nscrolled < view->nlines) {
3704 struct got_tree_entry *te;
3705 te = s->first_displayed_entry;
3708 te = SIMPLEQ_NEXT(te, entry);
3710 s->selected = ndisplayed - 1;
3715 if (s->selected_entry == NULL) {
3716 struct tog_parent_tree *parent;
3718 /* user selected '..' */
3719 if (s->tree == s->root)
3721 parent = TAILQ_FIRST(&s->parents);
3722 TAILQ_REMOVE(&s->parents, parent,
3724 got_object_tree_close(s->tree);
3725 s->tree = parent->tree;
3727 got_object_tree_get_entries(s->tree);
3728 s->first_displayed_entry =
3729 parent->first_displayed_entry;
3731 parent->selected_entry;
3732 s->selected = parent->selected;
3734 } else if (S_ISDIR(s->selected_entry->mode)) {
3735 struct tog_parent_tree *parent;
3736 struct got_tree_object *child;
3737 err = got_object_open_as_tree(&child,
3738 s->repo, s->selected_entry->id);
3741 parent = calloc(1, sizeof(*parent));
3742 if (parent == NULL) {
3743 err = got_error_from_errno();
3746 parent->tree = s->tree;
3747 parent->first_displayed_entry =
3748 s->first_displayed_entry;
3749 parent->selected_entry = s->selected_entry;
3750 parent->selected = s->selected;
3751 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3754 got_object_tree_get_entries(s->tree);
3756 s->first_displayed_entry = NULL;
3757 } else if (S_ISREG(s->selected_entry->mode)) {
3758 struct tog_view *blame_view;
3759 int begin_x = view_is_parent_view(view) ?
3760 view_split_begin_x(view->begin_x) : 0;
3762 err = blame_tree_entry(&blame_view, begin_x,
3763 s->selected_entry, &s->parents,
3764 s->commit_id, s->refs, s->repo);
3767 if (view_is_parent_view(view)) {
3768 err = view_close_child(view);
3771 err = view_set_child(view, blame_view);
3773 view_close(blame_view);
3776 *focus_view = blame_view;
3777 view->child_focussed = 1;
3779 *new_view = blame_view;
3783 if (s->selected > view->nlines)
3784 s->selected = s->ndisplayed - 1;
3797 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3802 static const struct got_error *
3803 cmd_tree(int argc, char *argv[])
3805 const struct got_error *error;
3806 struct got_repository *repo = NULL;
3807 struct got_reflist_head refs;
3808 char *repo_path = NULL;
3809 struct got_object_id *commit_id = NULL;
3810 char *commit_id_arg = NULL;
3811 struct got_commit_object *commit = NULL;
3812 struct got_tree_object *tree = NULL;
3814 struct tog_view *view;
3817 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3822 while ((ch = getopt(argc, argv, "c:")) != -1) {
3825 commit_id_arg = optarg;
3837 struct got_worktree *worktree;
3838 char *cwd = getcwd(NULL, 0);
3840 return got_error_from_errno();
3841 error = got_worktree_open(&worktree, cwd);
3842 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3847 strdup(got_worktree_get_repo_path(worktree));
3848 got_worktree_close(worktree);
3851 if (repo_path == NULL) {
3852 error = got_error_from_errno();
3855 } else if (argc == 1) {
3856 repo_path = realpath(argv[0], NULL);
3857 if (repo_path == NULL)
3858 return got_error_from_errno();
3864 error = apply_unveil(repo_path, NULL);
3868 error = got_repo_open(&repo, repo_path);
3872 if (commit_id_arg == NULL)
3873 error = get_head_commit_id(&commit_id, repo);
3875 error = got_object_resolve_id_str(&commit_id, repo,
3880 error = got_object_open_as_commit(&commit, repo, commit_id);
3884 error = got_object_open_as_tree(&tree, repo,
3885 got_object_commit_get_tree_id(commit));
3889 SIMPLEQ_INIT(&refs);
3890 error = got_ref_list(&refs, repo);
3894 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3896 error = got_error_from_errno();
3899 error = open_tree_view(view, tree, commit_id, &refs, repo);
3902 error = view_loop(view);
3907 got_object_commit_close(commit);
3909 got_object_tree_close(tree);
3911 got_repo_close(repo);
3920 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3921 "Available commands:\n", getprogname());
3922 for (i = 0; i < nitems(tog_commands); i++) {
3923 struct tog_cmd *cmd = &tog_commands[i];
3924 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3930 make_argv(const char *arg0, const char *arg1)
3933 int argc = (arg1 == NULL ? 1 : 2);
3935 argv = calloc(argc, sizeof(char *));
3938 argv[0] = strdup(arg0);
3939 if (argv[0] == NULL)
3942 argv[1] = strdup(arg1);
3943 if (argv[1] == NULL)
3951 main(int argc, char *argv[])
3953 const struct got_error *error = NULL;
3954 struct tog_cmd *cmd = NULL;
3956 char **cmd_argv = NULL;
3958 setlocale(LC_CTYPE, "");
3960 while ((ch = getopt(argc, argv, "h")) != -1) {
3979 /* Build an argument vector which runs a default command. */
3980 cmd = &tog_commands[0];
3981 cmd_argv = make_argv(cmd->name, NULL);
3986 /* Did the user specific a command? */
3987 for (i = 0; i < nitems(tog_commands); i++) {
3988 if (strncmp(tog_commands[i].name, argv[0],
3989 strlen(argv[0])) == 0) {
3990 cmd = &tog_commands[i];
3992 tog_commands[i].cmd_usage();
3997 /* Did the user specify a repository? */
3998 char *repo_path = realpath(argv[0], NULL);
4000 struct got_repository *repo;
4001 error = got_repo_open(&repo, repo_path);
4003 got_repo_close(repo);
4005 error = got_error_from_errno();
4008 fprintf(stderr, "%s: '%s' is not a "
4009 "known command\n", getprogname(),
4013 fprintf(stderr, "%s: '%s' is neither a known "
4014 "command nor a path to a repository\n",
4015 getprogname(), argv[0]);
4019 cmd = &tog_commands[0];
4020 cmd_argv = make_argv(cmd->name, repo_path);
4026 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4033 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);