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;
936 static const size_t date_display_cols = 9;
937 static const size_t author_display_cols = 16;
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, '<');
968 char *at = strchr(author, '@');
973 err = format_line(&wauthor, &author_width, author, limit);
976 waddwstr(view->window, wauthor);
978 while (col <= avail && author_width < author_display_cols + 1) {
979 waddch(view->window, ' ');
986 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
987 if (logmsg0 == NULL) {
988 err = got_error_from_errno();
992 while (*logmsg == '\n')
994 newline = strchr(logmsg, '\n');
998 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1001 waddwstr(view->window, wlogmsg);
1002 col += logmsg_width;
1003 while (col <= avail) {
1004 waddch(view->window, ' ');
1016 static struct commit_queue_entry *
1017 alloc_commit_queue_entry(struct got_commit_object *commit,
1018 struct got_object_id *id)
1020 struct commit_queue_entry *entry;
1022 entry = calloc(1, sizeof(*entry));
1027 entry->commit = commit;
1032 pop_commit(struct commit_queue *commits)
1034 struct commit_queue_entry *entry;
1036 entry = TAILQ_FIRST(&commits->head);
1037 TAILQ_REMOVE(&commits->head, entry, entry);
1038 got_object_commit_close(entry->commit);
1039 commits->ncommits--;
1040 /* Don't free entry->id! It is owned by the commit graph. */
1045 free_commits(struct commit_queue *commits)
1047 while (!TAILQ_EMPTY(&commits->head))
1048 pop_commit(commits);
1051 static const struct got_error *
1052 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1053 int minqueue, struct got_repository *repo, const char *path)
1055 const struct got_error *err = NULL;
1059 * We keep all commits open throughout the lifetime of the log
1060 * view in order to avoid having to re-fetch commits from disk
1061 * while updating the display.
1063 while (nqueued < minqueue) {
1064 struct got_object_id *id;
1065 struct got_commit_object *commit;
1066 struct commit_queue_entry *entry;
1069 err = got_commit_graph_iter_next(&id, graph);
1071 if (err->code != GOT_ERR_ITER_NEED_MORE)
1073 err = got_commit_graph_fetch_commits(graph,
1083 err = got_object_open_as_commit(&commit, repo, id);
1086 entry = alloc_commit_queue_entry(commit, id);
1087 if (entry == NULL) {
1088 err = got_error_from_errno();
1092 errcode = pthread_mutex_lock(&tog_mutex);
1094 err = got_error_set_errno(errcode);
1098 entry->idx = commits->ncommits;
1099 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1101 commits->ncommits++;
1103 errcode = pthread_mutex_unlock(&tog_mutex);
1104 if (errcode && err == NULL)
1105 err = got_error_set_errno(errcode);
1111 static const struct got_error *
1112 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1114 const struct got_error *err = NULL;
1115 struct got_reference *head_ref;
1119 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1123 err = got_ref_resolve(head_id, repo, head_ref);
1124 got_ref_close(head_ref);
1133 static const struct got_error *
1134 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1135 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1136 struct commit_queue *commits, int selected_idx, int limit,
1137 struct got_reflist_head *refs, const char *path, int commits_needed)
1139 const struct got_error *err = NULL;
1140 struct commit_queue_entry *entry;
1141 int ncommits, width;
1142 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1143 char *refs_str = NULL;
1149 if (ncommits == selected_idx) {
1153 entry = TAILQ_NEXT(entry, entry);
1158 err = got_object_id_str(&id_str, (*selected)->id);
1162 err = build_refs_str(&refs_str, refs, (*selected)->id);
1168 if (commits_needed == 0)
1169 halfdelay(10); /* disable fast refresh */
1171 if (asprintf(&ncommits_str, " [%d/%d] %s",
1172 entry ? entry->idx + 1 : 0, commits->ncommits,
1173 commits_needed > 0 ? "loading... " :
1174 (refs_str ? refs_str : "")) == -1) {
1175 err = got_error_from_errno();
1179 if (path && strcmp(path, "/") != 0) {
1180 if (asprintf(&header, "commit %s %s%s",
1181 id_str ? id_str : "........................................",
1182 path, ncommits_str) == -1) {
1183 err = got_error_from_errno();
1187 } else if (asprintf(&header, "commit %s%s",
1188 id_str ? id_str : "........................................",
1189 ncommits_str) == -1) {
1190 err = got_error_from_errno();
1194 err = format_line(&wline, &width, header, view->ncols);
1198 werase(view->window);
1200 if (view_needs_focus_indication(view))
1201 wstandout(view->window);
1202 waddwstr(view->window, wline);
1203 while (width < view->ncols) {
1204 waddch(view->window, ' ');
1207 if (view_needs_focus_indication(view))
1208 wstandend(view->window);
1217 if (ncommits >= limit - 1)
1219 if (ncommits == selected_idx)
1220 wstandout(view->window);
1221 err = draw_commit(view, entry->commit, entry->id, refs);
1222 if (ncommits == selected_idx)
1223 wstandend(view->window);
1228 entry = TAILQ_NEXT(entry, entry);
1241 scroll_up(struct tog_view *view,
1242 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1243 struct commit_queue *commits)
1245 struct commit_queue_entry *entry;
1248 entry = TAILQ_FIRST(&commits->head);
1249 if (*first_displayed_entry == entry) {
1254 entry = *first_displayed_entry;
1255 while (entry && nscrolled < maxscroll) {
1256 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1258 *first_displayed_entry = entry;
1264 static const struct got_error *
1265 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1266 pthread_cond_t *need_commits)
1271 halfdelay(1); /* fast refresh while loading commits */
1273 while (*commits_needed > 0) {
1277 /* Wake the log thread. */
1278 errcode = pthread_cond_signal(need_commits);
1280 return got_error_set_errno(errcode);
1281 errcode = pthread_mutex_unlock(&tog_mutex);
1283 return got_error_set_errno(errcode);
1285 errcode = pthread_mutex_lock(&tog_mutex);
1287 return got_error_set_errno(errcode);
1289 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1291 * Thread is not done yet; lose a key press
1292 * and let the user retry... this way the GUI
1293 * remains interactive while logging deep paths
1294 * with few commits in history.
1303 static const struct got_error *
1304 scroll_down(struct tog_view *view,
1305 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1306 struct commit_queue_entry **last_displayed_entry,
1307 struct commit_queue *commits, int *log_complete, int *commits_needed,
1308 pthread_cond_t *need_commits)
1310 const struct got_error *err = NULL;
1311 struct commit_queue_entry *pentry;
1314 if (*last_displayed_entry == NULL)
1317 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1318 if (pentry == NULL && !*log_complete) {
1320 * Ask the log thread for required amount of commits
1321 * plus some amount of pre-fetching.
1323 (*commits_needed) += maxscroll + 20;
1324 err = trigger_log_thread(0, commits_needed, log_complete,
1331 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1332 if (pentry == NULL) {
1338 *last_displayed_entry = pentry;
1340 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1343 *first_displayed_entry = pentry;
1344 } while (++nscrolled < maxscroll);
1349 static const struct got_error *
1350 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1351 struct got_commit_object *commit, struct got_object_id *commit_id,
1352 struct tog_view *log_view, struct got_reflist_head *refs,
1353 struct got_repository *repo)
1355 const struct got_error *err;
1356 struct got_object_qid *parent_id;
1357 struct tog_view *diff_view;
1359 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1360 if (diff_view == NULL)
1361 return got_error_from_errno();
1363 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1364 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1365 commit_id, log_view, refs, repo);
1367 *new_view = diff_view;
1371 static const struct got_error *
1372 browse_commit(struct tog_view **new_view, int begin_x,
1373 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1374 struct got_repository *repo)
1376 const struct got_error *err = NULL;
1377 struct got_tree_object *tree;
1378 struct tog_view *tree_view;
1380 err = got_object_open_as_tree(&tree, repo,
1381 got_object_commit_get_tree_id(entry->commit));
1385 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1386 if (tree_view == NULL)
1387 return got_error_from_errno();
1389 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1391 got_object_tree_close(tree);
1393 *new_view = tree_view;
1398 log_thread(void *arg)
1400 const struct got_error *err = NULL;
1402 struct tog_log_thread_args *a = arg;
1405 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1409 while (!done && !err) {
1410 err = queue_commits(a->graph, a->commits, 1, a->repo,
1413 if (err->code != GOT_ERR_ITER_COMPLETED)
1417 } else if (a->commits_needed > 0)
1418 a->commits_needed--;
1420 errcode = pthread_mutex_lock(&tog_mutex);
1422 return (void *)got_error_set_errno(errcode);
1425 a->log_complete = 1;
1426 else if (*a->quit) {
1428 a->log_complete = 1;
1429 } else if (*a->first_displayed_entry == NULL) {
1430 *a->first_displayed_entry =
1431 TAILQ_FIRST(&a->commits->head);
1432 *a->selected_entry = *a->first_displayed_entry;
1436 a->commits_needed = 0;
1437 else if (a->commits_needed == 0) {
1438 errcode = pthread_cond_wait(&a->need_commits,
1441 err = got_error_set_errno(errcode);
1444 errcode = pthread_mutex_unlock(&tog_mutex);
1445 if (errcode && err == NULL)
1446 err = got_error_set_errno(errcode);
1451 static const struct got_error *
1452 stop_log_thread(struct tog_log_view_state *s)
1454 const struct got_error *err = NULL;
1459 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1461 return got_error_set_errno(errcode);
1462 errcode = pthread_mutex_unlock(&tog_mutex);
1464 return got_error_set_errno(errcode);
1465 errcode = pthread_join(s->thread, (void **)&err);
1467 return got_error_set_errno(errcode);
1468 errcode = pthread_mutex_lock(&tog_mutex);
1470 return got_error_set_errno(errcode);
1474 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1475 if (errcode && err == NULL)
1476 err = got_error_set_errno(errcode);
1478 if (s->thread_args.repo) {
1479 got_repo_close(s->thread_args.repo);
1480 s->thread_args.repo = NULL;
1483 if (s->thread_args.graph) {
1484 got_commit_graph_close(s->thread_args.graph);
1485 s->thread_args.graph = NULL;
1491 static const struct got_error *
1492 close_log_view(struct tog_view *view)
1494 const struct got_error *err = NULL;
1495 struct tog_log_view_state *s = &view->state.log;
1497 err = stop_log_thread(s);
1498 free_commits(&s->commits);
1499 free(s->in_repo_path);
1500 s->in_repo_path = NULL;
1506 static const struct got_error *
1507 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1508 struct got_reflist_head *refs, struct got_repository *repo,
1509 const char *path, int check_disk)
1511 const struct got_error *err = NULL;
1512 struct tog_log_view_state *s = &view->state.log;
1513 struct got_repository *thread_repo = NULL;
1514 struct got_commit_graph *thread_graph = NULL;
1517 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1521 /* The commit queue only contains commits being displayed. */
1522 TAILQ_INIT(&s->commits.head);
1523 s->commits.ncommits = 0;
1527 s->start_id = got_object_id_dup(start_id);
1528 if (s->start_id == NULL) {
1529 err = got_error_from_errno();
1533 view->show = show_log_view;
1534 view->input = input_log_view;
1535 view->close = close_log_view;
1537 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1540 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1545 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1547 err = got_error_set_errno(errcode);
1551 s->thread_args.commits_needed = view->nlines;
1552 s->thread_args.graph = thread_graph;
1553 s->thread_args.commits = &s->commits;
1554 s->thread_args.in_repo_path = s->in_repo_path;
1555 s->thread_args.start_id = s->start_id;
1556 s->thread_args.repo = thread_repo;
1557 s->thread_args.log_complete = 0;
1558 s->thread_args.quit = &s->quit;
1559 s->thread_args.view = view;
1560 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1561 s->thread_args.selected_entry = &s->selected_entry;
1564 close_log_view(view);
1568 static const struct got_error *
1569 show_log_view(struct tog_view *view)
1571 struct tog_log_view_state *s = &view->state.log;
1573 if (s->thread == NULL) {
1574 int errcode = pthread_create(&s->thread, NULL, log_thread,
1577 return got_error_set_errno(errcode);
1580 return draw_commits(view, &s->last_displayed_entry,
1581 &s->selected_entry, s->first_displayed_entry,
1582 &s->commits, s->selected, view->nlines, s->refs,
1583 s->in_repo_path, s->thread_args.commits_needed);
1586 static const struct got_error *
1587 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1588 struct tog_view **focus_view, struct tog_view *view, int ch)
1590 const struct got_error *err = NULL;
1591 struct tog_log_view_state *s = &view->state.log;
1593 struct tog_view *diff_view = NULL, *tree_view = NULL;
1604 if (s->first_displayed_entry == NULL)
1606 if (s->selected > 0)
1608 if (s->selected > 0)
1610 scroll_up(view, &s->first_displayed_entry, 1,
1614 if (s->first_displayed_entry == NULL)
1616 if (TAILQ_FIRST(&s->commits.head) ==
1617 s->first_displayed_entry) {
1618 if (s->selected == 0) {
1625 scroll_up(view, &s->first_displayed_entry,
1626 view->nlines, &s->commits);
1632 if (s->first_displayed_entry == NULL)
1634 if (s->selected < MIN(view->nlines - 2,
1635 s->commits.ncommits - 1)) {
1639 err = scroll_down(view, &s->first_displayed_entry, 1,
1640 &s->last_displayed_entry, &s->commits,
1641 &s->thread_args.log_complete,
1642 &s->thread_args.commits_needed,
1643 &s->thread_args.need_commits);
1646 struct commit_queue_entry *first;
1647 first = s->first_displayed_entry;
1650 err = scroll_down(view, &s->first_displayed_entry,
1651 view->nlines, &s->last_displayed_entry,
1652 &s->commits, &s->thread_args.log_complete,
1653 &s->thread_args.commits_needed,
1654 &s->thread_args.need_commits);
1655 if (first == s->first_displayed_entry &&
1656 s->selected < MIN(view->nlines - 2,
1657 s->commits.ncommits - 1)) {
1658 /* can't scroll further down */
1659 s->selected = MIN(view->nlines - 2,
1660 s->commits.ncommits - 1);
1666 if (s->selected > view->nlines - 2)
1667 s->selected = view->nlines - 2;
1668 if (s->selected > s->commits.ncommits - 1)
1669 s->selected = s->commits.ncommits - 1;
1673 if (s->selected_entry == NULL)
1675 if (view_is_parent_view(view))
1676 begin_x = view_split_begin_x(view->begin_x);
1677 err = open_diff_view_for_commit(&diff_view, begin_x,
1678 s->selected_entry->commit, s->selected_entry->id,
1679 view, s->refs, s->repo);
1682 if (view_is_parent_view(view)) {
1683 err = view_close_child(view);
1686 err = view_set_child(view, diff_view);
1688 view_close(diff_view);
1691 *focus_view = diff_view;
1692 view->child_focussed = 1;
1694 *new_view = diff_view;
1697 if (s->selected_entry == NULL)
1699 if (view_is_parent_view(view))
1700 begin_x = view_split_begin_x(view->begin_x);
1701 err = browse_commit(&tree_view, begin_x,
1702 s->selected_entry, s->refs, s->repo);
1705 if (view_is_parent_view(view)) {
1706 err = view_close_child(view);
1709 err = view_set_child(view, tree_view);
1711 view_close(tree_view);
1714 *focus_view = tree_view;
1715 view->child_focussed = 1;
1717 *new_view = tree_view;
1720 if (strcmp(s->in_repo_path, "/") == 0)
1722 parent_path = dirname(s->in_repo_path);
1723 if (parent_path && strcmp(parent_path, ".") != 0) {
1724 struct tog_view *lv;
1725 err = stop_log_thread(s);
1728 lv = view_open(view->nlines, view->ncols,
1729 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1731 return got_error_from_errno();
1732 err = open_log_view(lv, s->start_id, s->refs,
1733 s->repo, parent_path, 0);
1736 if (view_is_parent_view(view))
1739 view_set_child(view->parent, lv);
1752 static const struct got_error *
1753 apply_unveil(const char *repo_path, const char *worktree_path)
1755 const struct got_error *error;
1757 if (repo_path && unveil(repo_path, "r") != 0)
1758 return got_error_from_errno();
1760 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1761 return got_error_from_errno();
1763 if (unveil("/tmp", "rwc") != 0)
1764 return got_error_from_errno();
1766 error = got_privsep_unveil_exec_helpers();
1770 if (unveil(NULL, NULL) != 0)
1771 return got_error_from_errno();
1781 halfdelay(1); /* Do fast refresh while initial view is loading. */
1784 intrflush(stdscr, FALSE);
1785 keypad(stdscr, TRUE);
1787 signal(SIGWINCH, tog_sigwinch);
1790 static const struct got_error *
1791 cmd_log(int argc, char *argv[])
1793 const struct got_error *error;
1794 struct got_repository *repo = NULL;
1795 struct got_worktree *worktree = NULL;
1796 struct got_reflist_head refs;
1797 struct got_object_id *start_id = NULL;
1798 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1799 char *start_commit = NULL;
1801 struct tog_view *view;
1804 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1809 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1812 start_commit = optarg;
1815 repo_path = realpath(optarg, NULL);
1816 if (repo_path == NULL)
1817 err(1, "-r option");
1831 path = strdup(argv[0]);
1835 return got_error_from_errno();
1837 cwd = getcwd(NULL, 0);
1839 error = got_error_from_errno();
1842 if (repo_path == NULL) {
1843 error = got_worktree_open(&worktree, cwd);
1844 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1848 strdup(got_worktree_get_repo_path(worktree));
1850 repo_path = strdup(cwd);
1851 if (repo_path == NULL) {
1852 error = got_error_from_errno();
1859 error = apply_unveil(repo_path,
1860 worktree ? got_worktree_get_root_path(worktree) : NULL);
1864 error = got_repo_open(&repo, repo_path);
1868 if (start_commit == NULL)
1869 error = get_head_commit_id(&start_id, repo);
1871 error = got_object_resolve_id_str(&start_id, repo,
1876 SIMPLEQ_INIT(&refs);
1877 error = got_ref_list(&refs, repo);
1881 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1883 error = got_error_from_errno();
1886 error = open_log_view(view, start_id, &refs, repo, path, 1);
1889 error = view_loop(view);
1896 got_repo_close(repo);
1898 got_worktree_close(worktree);
1906 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1912 parse_next_line(FILE *f, size_t *len)
1917 const char delim[3] = { '\0', '\0', '\0'};
1919 line = fparseln(f, &linelen, &lineno, delim, 0);
1925 static const struct got_error *
1926 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1927 int *last_displayed_line, int *eof, int max_lines,
1930 const struct got_error *err;
1931 int nlines = 0, nprinted = 0;
1938 werase(view->window);
1941 err = format_line(&wline, &width, header, view->ncols);
1946 if (view_needs_focus_indication(view))
1947 wstandout(view->window);
1948 waddwstr(view->window, wline);
1949 if (view_needs_focus_indication(view))
1950 wstandend(view->window);
1951 if (width < view->ncols)
1952 waddch(view->window, '\n');
1960 while (nprinted < max_lines) {
1961 line = parse_next_line(f, &len);
1966 if (++nlines < *first_displayed_line) {
1971 err = format_line(&wline, &width, line, view->ncols);
1976 waddwstr(view->window, wline);
1977 if (width < view->ncols)
1978 waddch(view->window, '\n');
1979 if (++nprinted == 1)
1980 *first_displayed_line = nlines;
1985 *last_displayed_line = nlines;
1993 get_datestr(time_t *time, char *datebuf)
1995 char *p, *s = ctime_r(time, datebuf);
1996 p = strchr(s, '\n');
2002 static const struct got_error *
2003 write_commit_info(struct got_object_id *commit_id,
2004 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2006 const struct got_error *err = NULL;
2008 struct got_commit_object *commit;
2009 char *id_str = NULL;
2010 time_t committer_time;
2011 const char *author, *committer;
2012 char *refs_str = NULL;
2015 err = build_refs_str(&refs_str, refs, commit_id);
2020 err = got_object_open_as_commit(&commit, repo, commit_id);
2024 err = got_object_id_str(&id_str, commit_id);
2026 err = got_error_from_errno();
2030 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2031 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2032 err = got_error_from_errno();
2035 if (fprintf(outfile, "from: %s\n",
2036 got_object_commit_get_author(commit)) < 0) {
2037 err = got_error_from_errno();
2040 committer_time = got_object_commit_get_committer_time(commit);
2041 if (fprintf(outfile, "date: %s UTC\n",
2042 get_datestr(&committer_time, datebuf)) < 0) {
2043 err = got_error_from_errno();
2046 author = got_object_commit_get_author(commit);
2047 committer = got_object_commit_get_committer(commit);
2048 if (strcmp(author, committer) != 0 &&
2049 fprintf(outfile, "via: %s\n", committer) < 0) {
2050 err = got_error_from_errno();
2053 if (fprintf(outfile, "%s\n",
2054 got_object_commit_get_logmsg(commit)) < 0) {
2055 err = got_error_from_errno();
2061 got_object_commit_close(commit);
2065 static const struct got_error *
2066 create_diff(struct tog_diff_view_state *s)
2068 const struct got_error *err = NULL;
2074 err = got_error_from_errno();
2077 if (s->f && fclose(s->f) != 0) {
2078 err = got_error_from_errno();
2084 err = got_object_get_type(&obj_type, s->repo, s->id1);
2086 err = got_object_get_type(&obj_type, s->repo, s->id2);
2091 case GOT_OBJ_TYPE_BLOB:
2092 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2093 s->diff_context, s->repo, f);
2095 case GOT_OBJ_TYPE_TREE:
2096 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2097 s->diff_context, s->repo, f);
2099 case GOT_OBJ_TYPE_COMMIT: {
2100 const struct got_object_id_queue *parent_ids;
2101 struct got_object_qid *pid;
2102 struct got_commit_object *commit2;
2104 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2107 /* Show commit info if we're diffing to a parent/root commit. */
2109 write_commit_info(s->id2, s->refs, s->repo, f);
2111 parent_ids = got_object_commit_get_parent_ids(commit2);
2112 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2113 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2114 write_commit_info(s->id2, s->refs,
2120 got_object_commit_close(commit2);
2122 err = got_diff_objects_as_commits(s->id1, s->id2,
2123 s->diff_context, s->repo, f);
2127 err = got_error(GOT_ERR_OBJ_TYPE);
2131 if (f && fflush(f) != 0 && err == NULL)
2132 err = got_error_from_errno();
2137 diff_view_indicate_progress(struct tog_view *view)
2139 werase(view->window);
2140 waddstr(view->window, "diffing...");
2145 static const struct got_error *
2146 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2147 struct got_object_id *id2, struct tog_view *log_view,
2148 struct got_reflist_head *refs, struct got_repository *repo)
2150 const struct got_error *err;
2152 if (id1 != NULL && id2 != NULL) {
2154 err = got_object_get_type(&type1, repo, id1);
2157 err = got_object_get_type(&type2, repo, id2);
2162 return got_error(GOT_ERR_OBJ_TYPE);
2166 view->state.diff.id1 = got_object_id_dup(id1);
2167 if (view->state.diff.id1 == NULL)
2168 return got_error_from_errno();
2170 view->state.diff.id1 = NULL;
2172 view->state.diff.id2 = got_object_id_dup(id2);
2173 if (view->state.diff.id2 == NULL) {
2174 free(view->state.diff.id1);
2175 view->state.diff.id1 = NULL;
2176 return got_error_from_errno();
2178 view->state.diff.f = NULL;
2179 view->state.diff.first_displayed_line = 1;
2180 view->state.diff.last_displayed_line = view->nlines;
2181 view->state.diff.diff_context = 3;
2182 view->state.diff.log_view = log_view;
2183 view->state.diff.repo = repo;
2184 view->state.diff.refs = refs;
2186 if (log_view && view_is_splitscreen(view))
2187 show_log_view(log_view); /* draw vborder */
2188 diff_view_indicate_progress(view);
2190 err = create_diff(&view->state.diff);
2192 free(view->state.diff.id1);
2193 view->state.diff.id1 = NULL;
2194 free(view->state.diff.id2);
2195 view->state.diff.id2 = NULL;
2199 view->show = show_diff_view;
2200 view->input = input_diff_view;
2201 view->close = close_diff_view;
2206 static const struct got_error *
2207 close_diff_view(struct tog_view *view)
2209 const struct got_error *err = NULL;
2211 free(view->state.diff.id1);
2212 view->state.diff.id1 = NULL;
2213 free(view->state.diff.id2);
2214 view->state.diff.id2 = NULL;
2215 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2216 err = got_error_from_errno();
2220 static const struct got_error *
2221 show_diff_view(struct tog_view *view)
2223 const struct got_error *err;
2224 struct tog_diff_view_state *s = &view->state.diff;
2225 char *id_str1 = NULL, *id_str2, *header;
2228 err = got_object_id_str(&id_str1, s->id1);
2232 err = got_object_id_str(&id_str2, s->id2);
2236 if (asprintf(&header, "diff %s %s",
2237 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2238 err = got_error_from_errno();
2246 return draw_file(view, s->f, &s->first_displayed_line,
2247 &s->last_displayed_line, &s->eof, view->nlines,
2251 static const struct got_error *
2252 set_selected_commit(struct tog_diff_view_state *s,
2253 struct commit_queue_entry *entry)
2255 const struct got_error *err;
2256 const struct got_object_id_queue *parent_ids;
2257 struct got_commit_object *selected_commit;
2258 struct got_object_qid *pid;
2261 s->id2 = got_object_id_dup(entry->id);
2263 return got_error_from_errno();
2265 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2268 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2270 pid = SIMPLEQ_FIRST(parent_ids);
2271 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2272 got_object_commit_close(selected_commit);
2276 static const struct got_error *
2277 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2278 struct tog_view **focus_view, struct tog_view *view, int ch)
2280 const struct got_error *err = NULL;
2281 struct tog_diff_view_state *s = &view->state.diff;
2282 struct tog_log_view_state *ls;
2283 struct commit_queue_entry *entry;
2289 if (s->first_displayed_line > 1)
2290 s->first_displayed_line--;
2295 if (s->first_displayed_line == 1) {
2300 while (i++ < view->nlines - 1 &&
2301 s->first_displayed_line > 1)
2302 s->first_displayed_line--;
2307 s->first_displayed_line++;
2318 while (!s->eof && i++ < view->nlines - 1) {
2320 line = parse_next_line(s->f, NULL);
2321 s->first_displayed_line++;
2327 if (s->diff_context > 0) {
2329 diff_view_indicate_progress(view);
2330 err = create_diff(s);
2334 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2336 diff_view_indicate_progress(view);
2337 err = create_diff(s);
2342 if (s->log_view == NULL)
2344 ls = &s->log_view->state.log;
2345 entry = TAILQ_PREV(ls->selected_entry,
2346 commit_queue_head, entry);
2350 err = input_log_view(NULL, NULL, NULL, s->log_view,
2355 err = set_selected_commit(s, entry);
2359 s->first_displayed_line = 1;
2360 s->last_displayed_line = view->nlines;
2362 diff_view_indicate_progress(view);
2363 err = create_diff(s);
2367 if (s->log_view == NULL)
2369 ls = &s->log_view->state.log;
2371 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2372 ls->thread_args.commits_needed++;
2374 /* Display "loading..." in log view. */
2375 show_log_view(s->log_view);
2379 err = trigger_log_thread(1 /* load_all */,
2380 &ls->thread_args.commits_needed,
2381 &ls->thread_args.log_complete,
2382 &ls->thread_args.need_commits);
2386 err = input_log_view(NULL, NULL, NULL, s->log_view,
2391 entry = TAILQ_NEXT(ls->selected_entry, entry);
2395 err = set_selected_commit(s, entry);
2399 s->first_displayed_line = 1;
2400 s->last_displayed_line = view->nlines;
2402 diff_view_indicate_progress(view);
2403 err = create_diff(s);
2412 static const struct got_error *
2413 cmd_diff(int argc, char *argv[])
2415 const struct got_error *error = NULL;
2416 struct got_repository *repo = NULL;
2417 struct got_reflist_head refs;
2418 struct got_object_id *id1 = NULL, *id2 = NULL;
2419 char *repo_path = NULL;
2420 char *id_str1 = NULL, *id_str2 = NULL;
2422 struct tog_view *view;
2425 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2430 while ((ch = getopt(argc, argv, "")) != -1) {
2442 usage_diff(); /* TODO show local worktree changes */
2443 } else if (argc == 2) {
2444 repo_path = getcwd(NULL, 0);
2445 if (repo_path == NULL)
2446 return got_error_from_errno();
2449 } else if (argc == 3) {
2450 repo_path = realpath(argv[0], NULL);
2451 if (repo_path == NULL)
2452 return got_error_from_errno();
2460 error = apply_unveil(repo_path, NULL);
2464 error = got_repo_open(&repo, repo_path);
2469 error = got_object_resolve_id_str(&id1, repo, id_str1);
2473 error = got_object_resolve_id_str(&id2, repo, id_str2);
2477 SIMPLEQ_INIT(&refs);
2478 error = got_ref_list(&refs, repo);
2482 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2484 error = got_error_from_errno();
2487 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2490 error = view_loop(view);
2492 got_repo_close(repo);
2500 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2505 struct tog_blame_line {
2507 struct got_object_id *id;
2510 static const struct got_error *
2511 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2512 const char *path, struct tog_blame_line *lines, int nlines,
2513 int blame_complete, int selected_line, int *first_displayed_line,
2514 int *last_displayed_line, int *eof, int max_lines)
2516 const struct got_error *err;
2517 int lineno = 0, nprinted = 0;
2522 struct tog_blame_line *blame_line;
2523 struct got_object_id *prev_id = NULL;
2526 err = got_object_id_str(&id_str, id);
2531 werase(view->window);
2533 if (asprintf(&line, "commit %s", id_str) == -1) {
2534 err = got_error_from_errno();
2539 err = format_line(&wline, &width, line, view->ncols);
2542 if (view_needs_focus_indication(view))
2543 wstandout(view->window);
2544 waddwstr(view->window, wline);
2545 if (view_needs_focus_indication(view))
2546 wstandend(view->window);
2549 if (width < view->ncols)
2550 waddch(view->window, '\n');
2552 if (asprintf(&line, "[%d/%d] %s%s",
2553 *first_displayed_line - 1 + selected_line, nlines,
2554 blame_complete ? "" : "annotating... ", path) == -1) {
2556 return got_error_from_errno();
2559 err = format_line(&wline, &width, line, view->ncols);
2564 waddwstr(view->window, wline);
2567 if (width < view->ncols)
2568 waddch(view->window, '\n');
2571 while (nprinted < max_lines - 2) {
2572 line = parse_next_line(f, &len);
2577 if (++lineno < *first_displayed_line) {
2582 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2583 err = format_line(&wline, &width, line, wlimit);
2589 if (view->focussed && nprinted == selected_line - 1)
2590 wstandout(view->window);
2592 blame_line = &lines[lineno - 1];
2593 if (blame_line->annotated && prev_id &&
2594 got_object_id_cmp(prev_id, blame_line->id) == 0)
2595 waddstr(view->window, " ");
2596 else if (blame_line->annotated) {
2598 err = got_object_id_str(&id_str, blame_line->id);
2604 wprintw(view->window, "%.8s ", id_str);
2606 prev_id = blame_line->id;
2608 waddstr(view->window, "........ ");
2612 waddwstr(view->window, wline);
2613 while (width < wlimit) {
2614 waddch(view->window, ' ');
2617 if (view->focussed && nprinted == selected_line - 1)
2618 wstandend(view->window);
2619 if (++nprinted == 1)
2620 *first_displayed_line = lineno;
2625 *last_displayed_line = lineno;
2632 static const struct got_error *
2633 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2635 const struct got_error *err = NULL;
2636 struct tog_blame_cb_args *a = arg;
2637 struct tog_blame_line *line;
2640 if (nlines != a->nlines ||
2641 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2642 return got_error(GOT_ERR_RANGE);
2644 errcode = pthread_mutex_lock(&tog_mutex);
2646 return got_error_set_errno(errcode);
2648 if (*a->quit) { /* user has quit the blame view */
2649 err = got_error(GOT_ERR_ITER_COMPLETED);
2654 goto done; /* no change in this commit */
2656 line = &a->lines[lineno - 1];
2657 if (line->annotated)
2660 line->id = got_object_id_dup(id);
2661 if (line->id == NULL) {
2662 err = got_error_from_errno();
2665 line->annotated = 1;
2667 errcode = pthread_mutex_unlock(&tog_mutex);
2669 err = got_error_set_errno(errcode);
2674 blame_thread(void *arg)
2676 const struct got_error *err;
2677 struct tog_blame_thread_args *ta = arg;
2678 struct tog_blame_cb_args *a = ta->cb_args;
2681 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2682 blame_cb, ta->cb_args);
2684 errcode = pthread_mutex_lock(&tog_mutex);
2686 return (void *)got_error_set_errno(errcode);
2688 got_repo_close(ta->repo);
2692 errcode = pthread_mutex_unlock(&tog_mutex);
2693 if (errcode && err == NULL)
2694 err = got_error_set_errno(errcode);
2699 static struct got_object_id *
2700 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2703 struct tog_blame_line *line;
2705 line = &lines[first_displayed_line - 1 + selected_line - 1];
2706 if (!line->annotated)
2712 static const struct got_error *
2713 stop_blame(struct tog_blame *blame)
2715 const struct got_error *err = NULL;
2718 if (blame->thread) {
2720 errcode = pthread_mutex_unlock(&tog_mutex);
2722 return got_error_set_errno(errcode);
2723 errcode = pthread_join(blame->thread, (void **)&err);
2725 return got_error_set_errno(errcode);
2726 errcode = pthread_mutex_lock(&tog_mutex);
2728 return got_error_set_errno(errcode);
2729 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2731 blame->thread = NULL;
2733 if (blame->thread_args.repo) {
2734 got_repo_close(blame->thread_args.repo);
2735 blame->thread_args.repo = NULL;
2738 if (fclose(blame->f) != 0 && err == NULL)
2739 err = got_error_from_errno();
2743 for (i = 0; i < blame->nlines; i++)
2744 free(blame->lines[i].id);
2746 blame->lines = NULL;
2748 free(blame->cb_args.commit_id);
2749 blame->cb_args.commit_id = NULL;
2754 static const struct got_error *
2755 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2756 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2757 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2758 struct got_repository *repo)
2760 const struct got_error *err = NULL;
2761 struct got_blob_object *blob = NULL;
2762 struct got_repository *thread_repo = NULL;
2763 struct got_object_id *obj_id = NULL;
2766 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2770 return got_error(GOT_ERR_NO_OBJ);
2772 err = got_object_get_type(&obj_type, repo, obj_id);
2776 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2777 err = got_error(GOT_ERR_OBJ_TYPE);
2781 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2784 blame->f = got_opentemp();
2785 if (blame->f == NULL) {
2786 err = got_error_from_errno();
2789 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2794 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2795 if (blame->lines == NULL) {
2796 err = got_error_from_errno();
2800 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2804 blame->cb_args.view = view;
2805 blame->cb_args.lines = blame->lines;
2806 blame->cb_args.nlines = blame->nlines;
2807 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2808 if (blame->cb_args.commit_id == NULL) {
2809 err = got_error_from_errno();
2812 blame->cb_args.quit = done;
2814 blame->thread_args.path = path;
2815 blame->thread_args.repo = thread_repo;
2816 blame->thread_args.cb_args = &blame->cb_args;
2817 blame->thread_args.complete = blame_complete;
2818 *blame_complete = 0;
2822 got_object_blob_close(blob);
2829 static const struct got_error *
2830 open_blame_view(struct tog_view *view, char *path,
2831 struct got_object_id *commit_id, struct got_reflist_head *refs,
2832 struct got_repository *repo)
2834 const struct got_error *err = NULL;
2835 struct tog_blame_view_state *s = &view->state.blame;
2837 SIMPLEQ_INIT(&s->blamed_commits);
2839 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2843 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2844 s->first_displayed_line = 1;
2845 s->last_displayed_line = view->nlines;
2846 s->selected_line = 1;
2847 s->blame_complete = 0;
2849 if (s->path == NULL)
2850 return got_error_from_errno();
2853 s->commit_id = commit_id;
2854 memset(&s->blame, 0, sizeof(s->blame));
2856 view->show = show_blame_view;
2857 view->input = input_blame_view;
2858 view->close = close_blame_view;
2860 return run_blame(&s->blame, view, &s->blame_complete,
2861 &s->first_displayed_line, &s->last_displayed_line,
2862 &s->selected_line, &s->done, &s->eof, s->path,
2863 s->blamed_commit->id, s->repo);
2866 static const struct got_error *
2867 close_blame_view(struct tog_view *view)
2869 const struct got_error *err = NULL;
2870 struct tog_blame_view_state *s = &view->state.blame;
2872 if (s->blame.thread)
2873 err = stop_blame(&s->blame);
2875 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2876 struct got_object_qid *blamed_commit;
2877 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2878 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2879 got_object_qid_free(blamed_commit);
2887 static const struct got_error *
2888 show_blame_view(struct tog_view *view)
2890 const struct got_error *err = NULL;
2891 struct tog_blame_view_state *s = &view->state.blame;
2894 if (s->blame.thread == NULL) {
2895 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2896 &s->blame.thread_args);
2898 return got_error_set_errno(errcode);
2901 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2902 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2903 s->selected_line, &s->first_displayed_line,
2904 &s->last_displayed_line, &s->eof, view->nlines);
2910 static const struct got_error *
2911 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2912 struct tog_view **focus_view, struct tog_view *view, int ch)
2914 const struct got_error *err = NULL, *thread_err = NULL;
2915 struct tog_view *diff_view;
2916 struct tog_blame_view_state *s = &view->state.blame;
2925 if (s->selected_line > 1)
2927 else if (s->selected_line == 1 &&
2928 s->first_displayed_line > 1)
2929 s->first_displayed_line--;
2934 if (s->first_displayed_line == 1) {
2935 if (s->selected_line == 1) {
2939 s->selected_line = 1;
2942 if (s->first_displayed_line > view->nlines - 2)
2943 s->first_displayed_line -=
2946 s->first_displayed_line = 1;
2950 if (s->selected_line < view->nlines - 2 &&
2951 s->first_displayed_line +
2952 s->selected_line <= s->blame.nlines)
2954 else if (s->last_displayed_line <
2956 s->first_displayed_line++;
2962 struct got_object_id *id = NULL;
2963 id = get_selected_commit_id(s->blame.lines,
2964 s->first_displayed_line, s->selected_line);
2968 struct got_commit_object *commit;
2969 struct got_object_qid *pid;
2970 struct got_object_id *blob_id = NULL;
2972 err = got_object_open_as_commit(&commit,
2976 pid = SIMPLEQ_FIRST(
2977 got_object_commit_get_parent_ids(commit));
2979 got_object_commit_close(commit);
2982 /* Check if path history ends here. */
2983 err = got_object_id_by_path(&blob_id, s->repo,
2986 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2988 got_object_commit_close(commit);
2991 err = got_object_get_type(&obj_type, s->repo,
2994 /* Can't blame non-blob type objects. */
2995 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2996 got_object_commit_close(commit);
2999 err = got_object_qid_alloc(&s->blamed_commit,
3001 got_object_commit_close(commit);
3003 if (got_object_id_cmp(id,
3004 s->blamed_commit->id) == 0)
3006 err = got_object_qid_alloc(&s->blamed_commit,
3012 thread_err = stop_blame(&s->blame);
3016 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3017 s->blamed_commit, entry);
3018 err = run_blame(&s->blame, view, &s->blame_complete,
3019 &s->first_displayed_line, &s->last_displayed_line,
3020 &s->selected_line, &s->done, &s->eof,
3021 s->path, s->blamed_commit->id, s->repo);
3027 struct got_object_qid *first;
3028 first = SIMPLEQ_FIRST(&s->blamed_commits);
3029 if (!got_object_id_cmp(first->id, s->commit_id))
3032 thread_err = stop_blame(&s->blame);
3036 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3037 got_object_qid_free(s->blamed_commit);
3039 SIMPLEQ_FIRST(&s->blamed_commits);
3040 err = run_blame(&s->blame, view, &s->blame_complete,
3041 &s->first_displayed_line, &s->last_displayed_line,
3042 &s->selected_line, &s->done, &s->eof, s->path,
3043 s->blamed_commit->id, s->repo);
3050 struct got_object_id *id = NULL;
3051 struct got_object_qid *pid;
3052 struct got_commit_object *commit = NULL;
3053 id = get_selected_commit_id(s->blame.lines,
3054 s->first_displayed_line, s->selected_line);
3057 err = got_object_open_as_commit(&commit, s->repo, id);
3060 pid = SIMPLEQ_FIRST(
3061 got_object_commit_get_parent_ids(commit));
3062 if (view_is_parent_view(view))
3063 begin_x = view_split_begin_x(view->begin_x);
3064 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3065 if (diff_view == NULL) {
3066 got_object_commit_close(commit);
3067 err = got_error_from_errno();
3070 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3071 id, NULL, s->refs, s->repo);
3072 got_object_commit_close(commit);
3074 view_close(diff_view);
3077 if (view_is_parent_view(view)) {
3078 err = view_close_child(view);
3081 err = view_set_child(view, diff_view);
3083 view_close(diff_view);
3086 *focus_view = diff_view;
3087 view->child_focussed = 1;
3089 *new_view = diff_view;
3096 if (s->last_displayed_line >= s->blame.nlines &&
3097 s->selected_line >= MIN(s->blame.nlines,
3098 view->nlines - 2)) {
3102 if (s->last_displayed_line >= s->blame.nlines &&
3103 s->selected_line < view->nlines - 2) {
3104 s->selected_line = MIN(s->blame.nlines,
3108 if (s->last_displayed_line + view->nlines - 2
3110 s->first_displayed_line +=
3113 s->first_displayed_line =
3118 if (s->selected_line > view->nlines - 2) {
3119 s->selected_line = MIN(s->blame.nlines,
3126 return thread_err ? thread_err : err;
3129 static const struct got_error *
3130 cmd_blame(int argc, char *argv[])
3132 const struct got_error *error;
3133 struct got_repository *repo = NULL;
3134 struct got_reflist_head refs;
3135 struct got_worktree *worktree = NULL;
3136 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3137 struct got_object_id *commit_id = NULL;
3138 char *commit_id_str = NULL;
3140 struct tog_view *view;
3143 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3148 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3151 commit_id_str = optarg;
3154 repo_path = realpath(optarg, NULL);
3155 if (repo_path == NULL)
3156 err(1, "-r option");
3172 cwd = getcwd(NULL, 0);
3174 error = got_error_from_errno();
3177 if (repo_path == NULL) {
3178 error = got_worktree_open(&worktree, cwd);
3179 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3185 strdup(got_worktree_get_repo_path(worktree));
3186 if (repo_path == NULL)
3187 error = got_error_from_errno();
3191 repo_path = strdup(cwd);
3192 if (repo_path == NULL) {
3193 error = got_error_from_errno();
3201 error = apply_unveil(repo_path, NULL);
3205 error = got_repo_open(&repo, repo_path);
3210 const char *prefix = got_worktree_get_path_prefix(worktree);
3211 char *p, *worktree_subdir = cwd +
3212 strlen(got_worktree_get_root_path(worktree));
3213 if (asprintf(&p, "%s%s%s%s%s",
3214 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3215 worktree_subdir, worktree_subdir[0] ? "/" : "",
3217 error = got_error_from_errno();
3220 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3223 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3228 if (commit_id_str == NULL) {
3229 struct got_reference *head_ref;
3230 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3233 error = got_ref_resolve(&commit_id, repo, head_ref);
3234 got_ref_close(head_ref);
3236 error = got_object_resolve_id_str(&commit_id, repo,
3242 SIMPLEQ_INIT(&refs);
3243 error = got_ref_list(&refs, repo);
3247 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3249 error = got_error_from_errno();
3252 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3255 error = view_loop(view);
3261 got_worktree_close(worktree);
3263 got_repo_close(repo);
3267 static const struct got_error *
3268 draw_tree_entries(struct tog_view *view,
3269 struct got_tree_entry **first_displayed_entry,
3270 struct got_tree_entry **last_displayed_entry,
3271 struct got_tree_entry **selected_entry, int *ndisplayed,
3272 const char *label, int show_ids, const char *parent_path,
3273 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3275 const struct got_error *err = NULL;
3276 struct got_tree_entry *te;
3282 werase(view->window);
3287 err = format_line(&wline, &width, label, view->ncols);
3290 if (view_needs_focus_indication(view))
3291 wstandout(view->window);
3292 waddwstr(view->window, wline);
3293 if (view_needs_focus_indication(view))
3294 wstandend(view->window);
3297 if (width < view->ncols)
3298 waddch(view->window, '\n');
3301 err = format_line(&wline, &width, parent_path, view->ncols);
3304 waddwstr(view->window, wline);
3307 if (width < view->ncols)
3308 waddch(view->window, '\n');
3311 waddch(view->window, '\n');
3315 te = SIMPLEQ_FIRST(&entries->head);
3316 if (*first_displayed_entry == NULL) {
3317 if (selected == 0) {
3319 wstandout(view->window);
3320 *selected_entry = NULL;
3322 waddstr(view->window, " ..\n"); /* parent directory */
3323 if (selected == 0 && view->focussed)
3324 wstandend(view->window);
3331 while (te != *first_displayed_entry)
3332 te = SIMPLEQ_NEXT(te, entry);
3336 char *line = NULL, *id_str = NULL;
3339 err = got_object_id_str(&id_str, te->id);
3341 return got_error_from_errno();
3343 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3344 te->name, S_ISDIR(te->mode) ? "/" :
3345 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3347 return got_error_from_errno();
3350 err = format_line(&wline, &width, line, view->ncols);
3355 if (n == selected) {
3357 wstandout(view->window);
3358 *selected_entry = te;
3360 waddwstr(view->window, wline);
3361 if (width < view->ncols)
3362 waddch(view->window, '\n');
3363 if (n == selected && view->focussed)
3364 wstandend(view->window);
3370 *last_displayed_entry = te;
3373 te = SIMPLEQ_NEXT(te, entry);
3380 tree_scroll_up(struct tog_view *view,
3381 struct got_tree_entry **first_displayed_entry, int maxscroll,
3382 const struct got_tree_entries *entries, int isroot)
3384 struct got_tree_entry *te, *prev;
3387 if (*first_displayed_entry == NULL) {
3392 te = SIMPLEQ_FIRST(&entries->head);
3393 if (*first_displayed_entry == te) {
3396 *first_displayed_entry = NULL;
3400 /* XXX this is stupid... switch to TAILQ? */
3401 for (i = 0; i < maxscroll; i++) {
3402 while (te != *first_displayed_entry) {
3404 te = SIMPLEQ_NEXT(te, entry);
3406 *first_displayed_entry = prev;
3407 te = SIMPLEQ_FIRST(&entries->head);
3409 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3410 *first_displayed_entry = NULL;
3414 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3415 struct got_tree_entry *last_displayed_entry,
3416 const struct got_tree_entries *entries)
3418 struct got_tree_entry *next, *last;
3421 if (*first_displayed_entry)
3422 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3424 next = SIMPLEQ_FIRST(&entries->head);
3425 last = last_displayed_entry;
3426 while (next && last && n++ < maxscroll) {
3427 last = SIMPLEQ_NEXT(last, entry);
3429 *first_displayed_entry = next;
3430 next = SIMPLEQ_NEXT(next, entry);
3436 static const struct got_error *
3437 tree_entry_path(char **path, struct tog_parent_trees *parents,
3438 struct got_tree_entry *te)
3440 const struct got_error *err = NULL;
3441 struct tog_parent_tree *pt;
3442 size_t len = 2; /* for leading slash and NUL */
3444 TAILQ_FOREACH(pt, parents, entry)
3445 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3447 len += strlen(te->name);
3449 *path = calloc(1, len);
3451 return got_error_from_errno();
3454 pt = TAILQ_LAST(parents, tog_parent_trees);
3456 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3457 err = got_error(GOT_ERR_NO_SPACE);
3460 if (strlcat(*path, "/", len) >= len) {
3461 err = got_error(GOT_ERR_NO_SPACE);
3464 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3467 if (strlcat(*path, te->name, len) >= len) {
3468 err = got_error(GOT_ERR_NO_SPACE);
3480 static const struct got_error *
3481 blame_tree_entry(struct tog_view **new_view, int begin_x,
3482 struct got_tree_entry *te, struct tog_parent_trees *parents,
3483 struct got_object_id *commit_id, struct got_reflist_head *refs,
3484 struct got_repository *repo)
3486 const struct got_error *err = NULL;
3488 struct tog_view *blame_view;
3490 err = tree_entry_path(&path, parents, te);
3494 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3495 if (blame_view == NULL)
3496 return got_error_from_errno();
3498 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3500 view_close(blame_view);
3503 *new_view = blame_view;
3507 static const struct got_error *
3508 log_tree_entry(struct tog_view **new_view, int begin_x,
3509 struct got_tree_entry *te, struct tog_parent_trees *parents,
3510 struct got_object_id *commit_id, struct got_reflist_head *refs,
3511 struct got_repository *repo)
3513 struct tog_view *log_view;
3514 const struct got_error *err = NULL;
3517 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3518 if (log_view == NULL)
3519 return got_error_from_errno();
3521 err = tree_entry_path(&path, parents, te);
3525 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3527 view_close(log_view);
3529 *new_view = log_view;
3534 static const struct got_error *
3535 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3536 struct got_object_id *commit_id, struct got_reflist_head *refs,
3537 struct got_repository *repo)
3539 const struct got_error *err = NULL;
3540 char *commit_id_str = NULL;
3541 struct tog_tree_view_state *s = &view->state.tree;
3543 TAILQ_INIT(&s->parents);
3545 err = got_object_id_str(&commit_id_str, commit_id);
3549 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3550 err = got_error_from_errno();
3554 s->root = s->tree = root;
3555 s->entries = got_object_tree_get_entries(root);
3556 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3557 s->commit_id = got_object_id_dup(commit_id);
3558 if (s->commit_id == NULL) {
3559 err = got_error_from_errno();
3565 view->show = show_tree_view;
3566 view->input = input_tree_view;
3567 view->close = close_tree_view;
3569 free(commit_id_str);
3571 free(s->tree_label);
3572 s->tree_label = NULL;
3577 static const struct got_error *
3578 close_tree_view(struct tog_view *view)
3580 struct tog_tree_view_state *s = &view->state.tree;
3582 free(s->tree_label);
3583 s->tree_label = NULL;
3585 s->commit_id = NULL;
3586 while (!TAILQ_EMPTY(&s->parents)) {
3587 struct tog_parent_tree *parent;
3588 parent = TAILQ_FIRST(&s->parents);
3589 TAILQ_REMOVE(&s->parents, parent, entry);
3593 if (s->tree != s->root)
3594 got_object_tree_close(s->tree);
3595 got_object_tree_close(s->root);
3600 static const struct got_error *
3601 show_tree_view(struct tog_view *view)
3603 const struct got_error *err = NULL;
3604 struct tog_tree_view_state *s = &view->state.tree;
3607 err = tree_entry_path(&parent_path, &s->parents, NULL);
3611 err = draw_tree_entries(view, &s->first_displayed_entry,
3612 &s->last_displayed_entry, &s->selected_entry,
3613 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3614 s->entries, s->selected, view->nlines, s->tree == s->root);
3621 static const struct got_error *
3622 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3623 struct tog_view **focus_view, struct tog_view *view, int ch)
3625 const struct got_error *err = NULL;
3626 struct tog_tree_view_state *s = &view->state.tree;
3627 struct tog_view *log_view;
3628 int begin_x = 0, nscrolled;
3632 s->show_ids = !s->show_ids;
3635 if (!s->selected_entry)
3637 if (view_is_parent_view(view))
3638 begin_x = view_split_begin_x(view->begin_x);
3639 err = log_tree_entry(&log_view, begin_x,
3640 s->selected_entry, &s->parents,
3641 s->commit_id, s->refs, s->repo);
3642 if (view_is_parent_view(view)) {
3643 err = view_close_child(view);
3646 err = view_set_child(view, log_view);
3648 view_close(log_view);
3651 *focus_view = log_view;
3652 view->child_focussed = 1;
3654 *new_view = log_view;
3658 if (s->selected > 0) {
3660 if (s->selected == 0)
3663 if (s->selected > 0)
3665 tree_scroll_up(view, &s->first_displayed_entry, 1,
3666 s->entries, s->tree == s->root);
3669 tree_scroll_up(view, &s->first_displayed_entry,
3670 MAX(0, view->nlines - 4 - s->selected), s->entries,
3671 s->tree == s->root);
3673 if (SIMPLEQ_FIRST(&s->entries->head) ==
3674 s->first_displayed_entry && s->tree != s->root)
3675 s->first_displayed_entry = NULL;
3679 if (s->selected < s->ndisplayed - 1) {
3683 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3685 /* can't scroll any further */
3689 tree_scroll_down(&s->first_displayed_entry, 1,
3690 s->last_displayed_entry, s->entries);
3693 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3695 /* can't scroll any further; move cursor down */
3696 if (s->selected < s->ndisplayed - 1)
3697 s->selected = s->ndisplayed - 1;
3702 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3703 view->nlines, s->last_displayed_entry, s->entries);
3704 if (nscrolled < view->nlines) {
3706 struct got_tree_entry *te;
3707 te = s->first_displayed_entry;
3710 te = SIMPLEQ_NEXT(te, entry);
3712 s->selected = ndisplayed - 1;
3717 if (s->selected_entry == NULL) {
3718 struct tog_parent_tree *parent;
3720 /* user selected '..' */
3721 if (s->tree == s->root)
3723 parent = TAILQ_FIRST(&s->parents);
3724 TAILQ_REMOVE(&s->parents, parent,
3726 got_object_tree_close(s->tree);
3727 s->tree = parent->tree;
3729 got_object_tree_get_entries(s->tree);
3730 s->first_displayed_entry =
3731 parent->first_displayed_entry;
3733 parent->selected_entry;
3734 s->selected = parent->selected;
3736 } else if (S_ISDIR(s->selected_entry->mode)) {
3737 struct tog_parent_tree *parent;
3738 struct got_tree_object *child;
3739 err = got_object_open_as_tree(&child,
3740 s->repo, s->selected_entry->id);
3743 parent = calloc(1, sizeof(*parent));
3744 if (parent == NULL) {
3745 err = got_error_from_errno();
3748 parent->tree = s->tree;
3749 parent->first_displayed_entry =
3750 s->first_displayed_entry;
3751 parent->selected_entry = s->selected_entry;
3752 parent->selected = s->selected;
3753 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3756 got_object_tree_get_entries(s->tree);
3758 s->first_displayed_entry = NULL;
3759 } else if (S_ISREG(s->selected_entry->mode)) {
3760 struct tog_view *blame_view;
3761 int begin_x = view_is_parent_view(view) ?
3762 view_split_begin_x(view->begin_x) : 0;
3764 err = blame_tree_entry(&blame_view, begin_x,
3765 s->selected_entry, &s->parents,
3766 s->commit_id, s->refs, s->repo);
3769 if (view_is_parent_view(view)) {
3770 err = view_close_child(view);
3773 err = view_set_child(view, blame_view);
3775 view_close(blame_view);
3778 *focus_view = blame_view;
3779 view->child_focussed = 1;
3781 *new_view = blame_view;
3785 if (s->selected > view->nlines)
3786 s->selected = s->ndisplayed - 1;
3799 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3804 static const struct got_error *
3805 cmd_tree(int argc, char *argv[])
3807 const struct got_error *error;
3808 struct got_repository *repo = NULL;
3809 struct got_reflist_head refs;
3810 char *repo_path = NULL;
3811 struct got_object_id *commit_id = NULL;
3812 char *commit_id_arg = NULL;
3813 struct got_commit_object *commit = NULL;
3814 struct got_tree_object *tree = NULL;
3816 struct tog_view *view;
3819 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3824 while ((ch = getopt(argc, argv, "c:")) != -1) {
3827 commit_id_arg = optarg;
3839 struct got_worktree *worktree;
3840 char *cwd = getcwd(NULL, 0);
3842 return got_error_from_errno();
3843 error = got_worktree_open(&worktree, cwd);
3844 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3849 strdup(got_worktree_get_repo_path(worktree));
3850 got_worktree_close(worktree);
3853 if (repo_path == NULL) {
3854 error = got_error_from_errno();
3857 } else if (argc == 1) {
3858 repo_path = realpath(argv[0], NULL);
3859 if (repo_path == NULL)
3860 return got_error_from_errno();
3866 error = apply_unveil(repo_path, NULL);
3870 error = got_repo_open(&repo, repo_path);
3874 if (commit_id_arg == NULL)
3875 error = get_head_commit_id(&commit_id, repo);
3877 error = got_object_resolve_id_str(&commit_id, repo,
3882 error = got_object_open_as_commit(&commit, repo, commit_id);
3886 error = got_object_open_as_tree(&tree, repo,
3887 got_object_commit_get_tree_id(commit));
3891 SIMPLEQ_INIT(&refs);
3892 error = got_ref_list(&refs, repo);
3896 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3898 error = got_error_from_errno();
3901 error = open_tree_view(view, tree, commit_id, &refs, repo);
3904 error = view_loop(view);
3909 got_object_commit_close(commit);
3911 got_object_tree_close(tree);
3913 got_repo_close(repo);
3922 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3923 "Available commands:\n", getprogname());
3924 for (i = 0; i < nitems(tog_commands); i++) {
3925 struct tog_cmd *cmd = &tog_commands[i];
3926 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3932 make_argv(const char *arg0, const char *arg1)
3935 int argc = (arg1 == NULL ? 1 : 2);
3937 argv = calloc(argc, sizeof(char *));
3940 argv[0] = strdup(arg0);
3941 if (argv[0] == NULL)
3944 argv[1] = strdup(arg1);
3945 if (argv[1] == NULL)
3953 main(int argc, char *argv[])
3955 const struct got_error *error = NULL;
3956 struct tog_cmd *cmd = NULL;
3958 char **cmd_argv = NULL;
3960 setlocale(LC_CTYPE, "");
3962 while ((ch = getopt(argc, argv, "h")) != -1) {
3981 /* Build an argument vector which runs a default command. */
3982 cmd = &tog_commands[0];
3983 cmd_argv = make_argv(cmd->name, NULL);
3988 /* Did the user specific a command? */
3989 for (i = 0; i < nitems(tog_commands); i++) {
3990 if (strncmp(tog_commands[i].name, argv[0],
3991 strlen(argv[0])) == 0) {
3992 cmd = &tog_commands[i];
3994 tog_commands[i].cmd_usage();
3999 /* Did the user specify a repository? */
4000 char *repo_path = realpath(argv[0], NULL);
4002 struct got_repository *repo;
4003 error = got_repo_open(&repo, repo_path);
4005 got_repo_close(repo);
4007 error = got_error_from_errno();
4010 fprintf(stderr, "%s: '%s' is not a "
4011 "known command\n", getprogname(),
4015 fprintf(stderr, "%s: '%s' is neither a known "
4016 "command nor a path to a repository\n",
4017 getprogname(), argv[0]);
4021 cmd = &tog_commands[0];
4022 cmd_argv = make_argv(cmd->name, repo_path);
4028 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4035 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);