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 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
926 char *smallerthan, *at;
928 smallerthan = strchr(author, '<');
929 if (smallerthan && smallerthan[1] != '\0')
930 author = smallerthan + 1;
931 at = strchr(author, '@');
934 return format_line(wauthor, author_width, author, limit);
937 static const struct got_error *
938 draw_commit(struct tog_view *view, struct got_commit_object *commit,
939 struct got_object_id *id, struct got_reflist_head *refs,
940 int author_display_cols)
942 const struct got_error *err = NULL;
943 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
944 char *logmsg0 = NULL, *logmsg = NULL;
946 wchar_t *wlogmsg = NULL, *wauthor = NULL;
947 int author_width, logmsg_width;
948 char *newline, *line = NULL;
950 static const size_t date_display_cols = 9;
951 const int avail = view->ncols;
953 time_t committer_time;
955 committer_time = got_object_commit_get_committer_time(commit);
956 if (localtime_r(&committer_time, &tm) == NULL)
957 return got_error_from_errno();
958 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
960 return got_error(GOT_ERR_NO_SPACE);
962 if (avail < date_display_cols)
963 limit = MIN(sizeof(datebuf) - 1, avail);
965 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
966 waddnstr(view->window, datebuf, limit);
971 author = strdup(got_object_commit_get_author(commit));
972 if (author == NULL) {
973 err = got_error_from_errno();
976 err = format_author(&wauthor, &author_width, author, avail - col);
979 waddwstr(view->window, wauthor);
981 while (col <= avail && author_width < author_display_cols + 2) {
982 waddch(view->window, ' ');
989 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
990 if (logmsg0 == NULL) {
991 err = got_error_from_errno();
995 while (*logmsg == '\n')
997 newline = strchr(logmsg, '\n');
1000 limit = avail - col;
1001 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1004 waddwstr(view->window, wlogmsg);
1005 col += logmsg_width;
1006 while (col <= avail) {
1007 waddch(view->window, ' ');
1019 static struct commit_queue_entry *
1020 alloc_commit_queue_entry(struct got_commit_object *commit,
1021 struct got_object_id *id)
1023 struct commit_queue_entry *entry;
1025 entry = calloc(1, sizeof(*entry));
1030 entry->commit = commit;
1035 pop_commit(struct commit_queue *commits)
1037 struct commit_queue_entry *entry;
1039 entry = TAILQ_FIRST(&commits->head);
1040 TAILQ_REMOVE(&commits->head, entry, entry);
1041 got_object_commit_close(entry->commit);
1042 commits->ncommits--;
1043 /* Don't free entry->id! It is owned by the commit graph. */
1048 free_commits(struct commit_queue *commits)
1050 while (!TAILQ_EMPTY(&commits->head))
1051 pop_commit(commits);
1054 static const struct got_error *
1055 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1056 int minqueue, struct got_repository *repo, const char *path)
1058 const struct got_error *err = NULL;
1062 * We keep all commits open throughout the lifetime of the log
1063 * view in order to avoid having to re-fetch commits from disk
1064 * while updating the display.
1066 while (nqueued < minqueue) {
1067 struct got_object_id *id;
1068 struct got_commit_object *commit;
1069 struct commit_queue_entry *entry;
1072 err = got_commit_graph_iter_next(&id, graph);
1074 if (err->code != GOT_ERR_ITER_NEED_MORE)
1076 err = got_commit_graph_fetch_commits(graph,
1086 err = got_object_open_as_commit(&commit, repo, id);
1089 entry = alloc_commit_queue_entry(commit, id);
1090 if (entry == NULL) {
1091 err = got_error_from_errno();
1095 errcode = pthread_mutex_lock(&tog_mutex);
1097 err = got_error_set_errno(errcode);
1101 entry->idx = commits->ncommits;
1102 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1104 commits->ncommits++;
1106 errcode = pthread_mutex_unlock(&tog_mutex);
1107 if (errcode && err == NULL)
1108 err = got_error_set_errno(errcode);
1114 static const struct got_error *
1115 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1117 const struct got_error *err = NULL;
1118 struct got_reference *head_ref;
1122 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1126 err = got_ref_resolve(head_id, repo, head_ref);
1127 got_ref_close(head_ref);
1136 static const struct got_error *
1137 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1138 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1139 struct commit_queue *commits, int selected_idx, int limit,
1140 struct got_reflist_head *refs, const char *path, int commits_needed)
1142 const struct got_error *err = NULL;
1143 struct commit_queue_entry *entry;
1144 int ncommits, width;
1145 int author_cols = 10;
1146 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1147 char *refs_str = NULL;
1153 if (ncommits == selected_idx) {
1157 entry = TAILQ_NEXT(entry, entry);
1162 err = got_object_id_str(&id_str, (*selected)->id);
1166 err = build_refs_str(&refs_str, refs, (*selected)->id);
1172 if (commits_needed == 0)
1173 halfdelay(10); /* disable fast refresh */
1175 if (asprintf(&ncommits_str, " [%d/%d] %s",
1176 entry ? entry->idx + 1 : 0, commits->ncommits,
1177 commits_needed > 0 ? "loading... " :
1178 (refs_str ? refs_str : "")) == -1) {
1179 err = got_error_from_errno();
1183 if (path && strcmp(path, "/") != 0) {
1184 if (asprintf(&header, "commit %s %s%s",
1185 id_str ? id_str : "........................................",
1186 path, ncommits_str) == -1) {
1187 err = got_error_from_errno();
1191 } else if (asprintf(&header, "commit %s%s",
1192 id_str ? id_str : "........................................",
1193 ncommits_str) == -1) {
1194 err = got_error_from_errno();
1198 err = format_line(&wline, &width, header, view->ncols);
1202 werase(view->window);
1204 if (view_needs_focus_indication(view))
1205 wstandout(view->window);
1206 waddwstr(view->window, wline);
1207 while (width < view->ncols) {
1208 waddch(view->window, ' ');
1211 if (view_needs_focus_indication(view))
1212 wstandend(view->window);
1217 /* Grow author column size if necessary. */
1224 if (ncommits >= limit - 1)
1226 author = strdup(got_object_commit_get_author(entry->commit));
1227 if (author == NULL) {
1228 err = got_error_from_errno();
1231 err = format_author(&wauthor, &width, author, COLS);
1232 if (author_cols < width)
1233 author_cols = width;
1236 entry = TAILQ_NEXT(entry, entry);
1243 if (ncommits >= limit - 1)
1245 if (ncommits == selected_idx)
1246 wstandout(view->window);
1247 err = draw_commit(view, entry->commit, entry->id, refs,
1249 if (ncommits == selected_idx)
1250 wstandend(view->window);
1255 entry = TAILQ_NEXT(entry, entry);
1268 scroll_up(struct tog_view *view,
1269 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1270 struct commit_queue *commits)
1272 struct commit_queue_entry *entry;
1275 entry = TAILQ_FIRST(&commits->head);
1276 if (*first_displayed_entry == entry) {
1281 entry = *first_displayed_entry;
1282 while (entry && nscrolled < maxscroll) {
1283 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1285 *first_displayed_entry = entry;
1291 static const struct got_error *
1292 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1293 pthread_cond_t *need_commits)
1298 halfdelay(1); /* fast refresh while loading commits */
1300 while (*commits_needed > 0) {
1304 /* Wake the log thread. */
1305 errcode = pthread_cond_signal(need_commits);
1307 return got_error_set_errno(errcode);
1308 errcode = pthread_mutex_unlock(&tog_mutex);
1310 return got_error_set_errno(errcode);
1312 errcode = pthread_mutex_lock(&tog_mutex);
1314 return got_error_set_errno(errcode);
1316 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1318 * Thread is not done yet; lose a key press
1319 * and let the user retry... this way the GUI
1320 * remains interactive while logging deep paths
1321 * with few commits in history.
1330 static const struct got_error *
1331 scroll_down(struct tog_view *view,
1332 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1333 struct commit_queue_entry **last_displayed_entry,
1334 struct commit_queue *commits, int *log_complete, int *commits_needed,
1335 pthread_cond_t *need_commits)
1337 const struct got_error *err = NULL;
1338 struct commit_queue_entry *pentry;
1341 if (*last_displayed_entry == NULL)
1344 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1345 if (pentry == NULL && !*log_complete) {
1347 * Ask the log thread for required amount of commits
1348 * plus some amount of pre-fetching.
1350 (*commits_needed) += maxscroll + 20;
1351 err = trigger_log_thread(0, commits_needed, log_complete,
1358 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1359 if (pentry == NULL) {
1365 *last_displayed_entry = pentry;
1367 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1370 *first_displayed_entry = pentry;
1371 } while (++nscrolled < maxscroll);
1376 static const struct got_error *
1377 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1378 struct got_commit_object *commit, struct got_object_id *commit_id,
1379 struct tog_view *log_view, struct got_reflist_head *refs,
1380 struct got_repository *repo)
1382 const struct got_error *err;
1383 struct got_object_qid *parent_id;
1384 struct tog_view *diff_view;
1386 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1387 if (diff_view == NULL)
1388 return got_error_from_errno();
1390 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1391 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1392 commit_id, log_view, refs, repo);
1394 *new_view = diff_view;
1398 static const struct got_error *
1399 browse_commit(struct tog_view **new_view, int begin_x,
1400 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1401 struct got_repository *repo)
1403 const struct got_error *err = NULL;
1404 struct got_tree_object *tree;
1405 struct tog_view *tree_view;
1407 err = got_object_open_as_tree(&tree, repo,
1408 got_object_commit_get_tree_id(entry->commit));
1412 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1413 if (tree_view == NULL)
1414 return got_error_from_errno();
1416 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1418 got_object_tree_close(tree);
1420 *new_view = tree_view;
1425 log_thread(void *arg)
1427 const struct got_error *err = NULL;
1429 struct tog_log_thread_args *a = arg;
1432 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1436 while (!done && !err) {
1437 err = queue_commits(a->graph, a->commits, 1, a->repo,
1440 if (err->code != GOT_ERR_ITER_COMPLETED)
1444 } else if (a->commits_needed > 0)
1445 a->commits_needed--;
1447 errcode = pthread_mutex_lock(&tog_mutex);
1449 return (void *)got_error_set_errno(errcode);
1452 a->log_complete = 1;
1453 else if (*a->quit) {
1455 a->log_complete = 1;
1456 } else if (*a->first_displayed_entry == NULL) {
1457 *a->first_displayed_entry =
1458 TAILQ_FIRST(&a->commits->head);
1459 *a->selected_entry = *a->first_displayed_entry;
1463 a->commits_needed = 0;
1464 else if (a->commits_needed == 0) {
1465 errcode = pthread_cond_wait(&a->need_commits,
1468 err = got_error_set_errno(errcode);
1471 errcode = pthread_mutex_unlock(&tog_mutex);
1472 if (errcode && err == NULL)
1473 err = got_error_set_errno(errcode);
1478 static const struct got_error *
1479 stop_log_thread(struct tog_log_view_state *s)
1481 const struct got_error *err = NULL;
1486 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1488 return got_error_set_errno(errcode);
1489 errcode = pthread_mutex_unlock(&tog_mutex);
1491 return got_error_set_errno(errcode);
1492 errcode = pthread_join(s->thread, (void **)&err);
1494 return got_error_set_errno(errcode);
1495 errcode = pthread_mutex_lock(&tog_mutex);
1497 return got_error_set_errno(errcode);
1501 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1502 if (errcode && err == NULL)
1503 err = got_error_set_errno(errcode);
1505 if (s->thread_args.repo) {
1506 got_repo_close(s->thread_args.repo);
1507 s->thread_args.repo = NULL;
1510 if (s->thread_args.graph) {
1511 got_commit_graph_close(s->thread_args.graph);
1512 s->thread_args.graph = NULL;
1518 static const struct got_error *
1519 close_log_view(struct tog_view *view)
1521 const struct got_error *err = NULL;
1522 struct tog_log_view_state *s = &view->state.log;
1524 err = stop_log_thread(s);
1525 free_commits(&s->commits);
1526 free(s->in_repo_path);
1527 s->in_repo_path = NULL;
1533 static const struct got_error *
1534 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1535 struct got_reflist_head *refs, struct got_repository *repo,
1536 const char *path, int check_disk)
1538 const struct got_error *err = NULL;
1539 struct tog_log_view_state *s = &view->state.log;
1540 struct got_repository *thread_repo = NULL;
1541 struct got_commit_graph *thread_graph = NULL;
1544 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1548 /* The commit queue only contains commits being displayed. */
1549 TAILQ_INIT(&s->commits.head);
1550 s->commits.ncommits = 0;
1554 s->start_id = got_object_id_dup(start_id);
1555 if (s->start_id == NULL) {
1556 err = got_error_from_errno();
1560 view->show = show_log_view;
1561 view->input = input_log_view;
1562 view->close = close_log_view;
1564 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1567 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1572 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1574 err = got_error_set_errno(errcode);
1578 s->thread_args.commits_needed = view->nlines;
1579 s->thread_args.graph = thread_graph;
1580 s->thread_args.commits = &s->commits;
1581 s->thread_args.in_repo_path = s->in_repo_path;
1582 s->thread_args.start_id = s->start_id;
1583 s->thread_args.repo = thread_repo;
1584 s->thread_args.log_complete = 0;
1585 s->thread_args.quit = &s->quit;
1586 s->thread_args.view = view;
1587 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1588 s->thread_args.selected_entry = &s->selected_entry;
1591 close_log_view(view);
1595 static const struct got_error *
1596 show_log_view(struct tog_view *view)
1598 struct tog_log_view_state *s = &view->state.log;
1600 if (s->thread == NULL) {
1601 int errcode = pthread_create(&s->thread, NULL, log_thread,
1604 return got_error_set_errno(errcode);
1607 return draw_commits(view, &s->last_displayed_entry,
1608 &s->selected_entry, s->first_displayed_entry,
1609 &s->commits, s->selected, view->nlines, s->refs,
1610 s->in_repo_path, s->thread_args.commits_needed);
1613 static const struct got_error *
1614 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1615 struct tog_view **focus_view, struct tog_view *view, int ch)
1617 const struct got_error *err = NULL;
1618 struct tog_log_view_state *s = &view->state.log;
1620 struct tog_view *diff_view = NULL, *tree_view = NULL;
1631 if (s->first_displayed_entry == NULL)
1633 if (s->selected > 0)
1635 if (s->selected > 0)
1637 scroll_up(view, &s->first_displayed_entry, 1,
1641 if (s->first_displayed_entry == NULL)
1643 if (TAILQ_FIRST(&s->commits.head) ==
1644 s->first_displayed_entry) {
1645 if (s->selected == 0) {
1652 scroll_up(view, &s->first_displayed_entry,
1653 view->nlines, &s->commits);
1659 if (s->first_displayed_entry == NULL)
1661 if (s->selected < MIN(view->nlines - 2,
1662 s->commits.ncommits - 1)) {
1666 err = scroll_down(view, &s->first_displayed_entry, 1,
1667 &s->last_displayed_entry, &s->commits,
1668 &s->thread_args.log_complete,
1669 &s->thread_args.commits_needed,
1670 &s->thread_args.need_commits);
1673 struct commit_queue_entry *first;
1674 first = s->first_displayed_entry;
1677 err = scroll_down(view, &s->first_displayed_entry,
1678 view->nlines, &s->last_displayed_entry,
1679 &s->commits, &s->thread_args.log_complete,
1680 &s->thread_args.commits_needed,
1681 &s->thread_args.need_commits);
1682 if (first == s->first_displayed_entry &&
1683 s->selected < MIN(view->nlines - 2,
1684 s->commits.ncommits - 1)) {
1685 /* can't scroll further down */
1686 s->selected = MIN(view->nlines - 2,
1687 s->commits.ncommits - 1);
1693 if (s->selected > view->nlines - 2)
1694 s->selected = view->nlines - 2;
1695 if (s->selected > s->commits.ncommits - 1)
1696 s->selected = s->commits.ncommits - 1;
1700 if (s->selected_entry == NULL)
1702 if (view_is_parent_view(view))
1703 begin_x = view_split_begin_x(view->begin_x);
1704 err = open_diff_view_for_commit(&diff_view, begin_x,
1705 s->selected_entry->commit, s->selected_entry->id,
1706 view, s->refs, s->repo);
1709 if (view_is_parent_view(view)) {
1710 err = view_close_child(view);
1713 err = view_set_child(view, diff_view);
1715 view_close(diff_view);
1718 *focus_view = diff_view;
1719 view->child_focussed = 1;
1721 *new_view = diff_view;
1724 if (s->selected_entry == NULL)
1726 if (view_is_parent_view(view))
1727 begin_x = view_split_begin_x(view->begin_x);
1728 err = browse_commit(&tree_view, begin_x,
1729 s->selected_entry, s->refs, s->repo);
1732 if (view_is_parent_view(view)) {
1733 err = view_close_child(view);
1736 err = view_set_child(view, tree_view);
1738 view_close(tree_view);
1741 *focus_view = tree_view;
1742 view->child_focussed = 1;
1744 *new_view = tree_view;
1747 if (strcmp(s->in_repo_path, "/") == 0)
1749 parent_path = dirname(s->in_repo_path);
1750 if (parent_path && strcmp(parent_path, ".") != 0) {
1751 struct tog_view *lv;
1752 err = stop_log_thread(s);
1755 lv = view_open(view->nlines, view->ncols,
1756 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1758 return got_error_from_errno();
1759 err = open_log_view(lv, s->start_id, s->refs,
1760 s->repo, parent_path, 0);
1763 if (view_is_parent_view(view))
1766 view_set_child(view->parent, lv);
1779 static const struct got_error *
1780 apply_unveil(const char *repo_path, const char *worktree_path)
1782 const struct got_error *error;
1784 if (repo_path && unveil(repo_path, "r") != 0)
1785 return got_error_from_errno();
1787 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1788 return got_error_from_errno();
1790 if (unveil("/tmp", "rwc") != 0)
1791 return got_error_from_errno();
1793 error = got_privsep_unveil_exec_helpers();
1797 if (unveil(NULL, NULL) != 0)
1798 return got_error_from_errno();
1808 halfdelay(1); /* Do fast refresh while initial view is loading. */
1811 intrflush(stdscr, FALSE);
1812 keypad(stdscr, TRUE);
1814 signal(SIGWINCH, tog_sigwinch);
1817 static const struct got_error *
1818 cmd_log(int argc, char *argv[])
1820 const struct got_error *error;
1821 struct got_repository *repo = NULL;
1822 struct got_worktree *worktree = NULL;
1823 struct got_reflist_head refs;
1824 struct got_object_id *start_id = NULL;
1825 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1826 char *start_commit = NULL;
1828 struct tog_view *view;
1831 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1836 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1839 start_commit = optarg;
1842 repo_path = realpath(optarg, NULL);
1843 if (repo_path == NULL)
1844 err(1, "-r option");
1858 path = strdup(argv[0]);
1862 return got_error_from_errno();
1864 cwd = getcwd(NULL, 0);
1866 error = got_error_from_errno();
1869 if (repo_path == NULL) {
1870 error = got_worktree_open(&worktree, cwd);
1871 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1875 strdup(got_worktree_get_repo_path(worktree));
1877 repo_path = strdup(cwd);
1878 if (repo_path == NULL) {
1879 error = got_error_from_errno();
1886 error = apply_unveil(repo_path,
1887 worktree ? got_worktree_get_root_path(worktree) : NULL);
1891 error = got_repo_open(&repo, repo_path);
1895 if (start_commit == NULL)
1896 error = get_head_commit_id(&start_id, repo);
1898 error = got_object_resolve_id_str(&start_id, repo,
1903 SIMPLEQ_INIT(&refs);
1904 error = got_ref_list(&refs, repo);
1908 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1910 error = got_error_from_errno();
1913 error = open_log_view(view, start_id, &refs, repo, path, 1);
1916 error = view_loop(view);
1923 got_repo_close(repo);
1925 got_worktree_close(worktree);
1933 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1939 parse_next_line(FILE *f, size_t *len)
1944 const char delim[3] = { '\0', '\0', '\0'};
1946 line = fparseln(f, &linelen, &lineno, delim, 0);
1952 static const struct got_error *
1953 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1954 int *last_displayed_line, int *eof, int max_lines,
1957 const struct got_error *err;
1958 int nlines = 0, nprinted = 0;
1965 werase(view->window);
1968 err = format_line(&wline, &width, header, view->ncols);
1973 if (view_needs_focus_indication(view))
1974 wstandout(view->window);
1975 waddwstr(view->window, wline);
1976 if (view_needs_focus_indication(view))
1977 wstandend(view->window);
1978 if (width < view->ncols)
1979 waddch(view->window, '\n');
1987 while (nprinted < max_lines) {
1988 line = parse_next_line(f, &len);
1993 if (++nlines < *first_displayed_line) {
1998 err = format_line(&wline, &width, line, view->ncols);
2003 waddwstr(view->window, wline);
2004 if (width < view->ncols)
2005 waddch(view->window, '\n');
2006 if (++nprinted == 1)
2007 *first_displayed_line = nlines;
2012 *last_displayed_line = nlines;
2020 get_datestr(time_t *time, char *datebuf)
2022 char *p, *s = ctime_r(time, datebuf);
2023 p = strchr(s, '\n');
2029 static const struct got_error *
2030 write_commit_info(struct got_object_id *commit_id,
2031 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2033 const struct got_error *err = NULL;
2035 struct got_commit_object *commit;
2036 char *id_str = NULL;
2037 time_t committer_time;
2038 const char *author, *committer;
2039 char *refs_str = NULL;
2042 err = build_refs_str(&refs_str, refs, commit_id);
2047 err = got_object_open_as_commit(&commit, repo, commit_id);
2051 err = got_object_id_str(&id_str, commit_id);
2053 err = got_error_from_errno();
2057 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2058 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2059 err = got_error_from_errno();
2062 if (fprintf(outfile, "from: %s\n",
2063 got_object_commit_get_author(commit)) < 0) {
2064 err = got_error_from_errno();
2067 committer_time = got_object_commit_get_committer_time(commit);
2068 if (fprintf(outfile, "date: %s UTC\n",
2069 get_datestr(&committer_time, datebuf)) < 0) {
2070 err = got_error_from_errno();
2073 author = got_object_commit_get_author(commit);
2074 committer = got_object_commit_get_committer(commit);
2075 if (strcmp(author, committer) != 0 &&
2076 fprintf(outfile, "via: %s\n", committer) < 0) {
2077 err = got_error_from_errno();
2080 if (fprintf(outfile, "%s\n",
2081 got_object_commit_get_logmsg(commit)) < 0) {
2082 err = got_error_from_errno();
2088 got_object_commit_close(commit);
2092 static const struct got_error *
2093 create_diff(struct tog_diff_view_state *s)
2095 const struct got_error *err = NULL;
2101 err = got_error_from_errno();
2104 if (s->f && fclose(s->f) != 0) {
2105 err = got_error_from_errno();
2111 err = got_object_get_type(&obj_type, s->repo, s->id1);
2113 err = got_object_get_type(&obj_type, s->repo, s->id2);
2118 case GOT_OBJ_TYPE_BLOB:
2119 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2120 s->diff_context, s->repo, f);
2122 case GOT_OBJ_TYPE_TREE:
2123 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2124 s->diff_context, s->repo, f);
2126 case GOT_OBJ_TYPE_COMMIT: {
2127 const struct got_object_id_queue *parent_ids;
2128 struct got_object_qid *pid;
2129 struct got_commit_object *commit2;
2131 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2134 /* Show commit info if we're diffing to a parent/root commit. */
2136 write_commit_info(s->id2, s->refs, s->repo, f);
2138 parent_ids = got_object_commit_get_parent_ids(commit2);
2139 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2140 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2141 write_commit_info(s->id2, s->refs,
2147 got_object_commit_close(commit2);
2149 err = got_diff_objects_as_commits(s->id1, s->id2,
2150 s->diff_context, s->repo, f);
2154 err = got_error(GOT_ERR_OBJ_TYPE);
2158 if (f && fflush(f) != 0 && err == NULL)
2159 err = got_error_from_errno();
2164 diff_view_indicate_progress(struct tog_view *view)
2166 werase(view->window);
2167 waddstr(view->window, "diffing...");
2172 static const struct got_error *
2173 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2174 struct got_object_id *id2, struct tog_view *log_view,
2175 struct got_reflist_head *refs, struct got_repository *repo)
2177 const struct got_error *err;
2179 if (id1 != NULL && id2 != NULL) {
2181 err = got_object_get_type(&type1, repo, id1);
2184 err = got_object_get_type(&type2, repo, id2);
2189 return got_error(GOT_ERR_OBJ_TYPE);
2193 view->state.diff.id1 = got_object_id_dup(id1);
2194 if (view->state.diff.id1 == NULL)
2195 return got_error_from_errno();
2197 view->state.diff.id1 = NULL;
2199 view->state.diff.id2 = got_object_id_dup(id2);
2200 if (view->state.diff.id2 == NULL) {
2201 free(view->state.diff.id1);
2202 view->state.diff.id1 = NULL;
2203 return got_error_from_errno();
2205 view->state.diff.f = NULL;
2206 view->state.diff.first_displayed_line = 1;
2207 view->state.diff.last_displayed_line = view->nlines;
2208 view->state.diff.diff_context = 3;
2209 view->state.diff.log_view = log_view;
2210 view->state.diff.repo = repo;
2211 view->state.diff.refs = refs;
2213 if (log_view && view_is_splitscreen(view))
2214 show_log_view(log_view); /* draw vborder */
2215 diff_view_indicate_progress(view);
2217 err = create_diff(&view->state.diff);
2219 free(view->state.diff.id1);
2220 view->state.diff.id1 = NULL;
2221 free(view->state.diff.id2);
2222 view->state.diff.id2 = NULL;
2226 view->show = show_diff_view;
2227 view->input = input_diff_view;
2228 view->close = close_diff_view;
2233 static const struct got_error *
2234 close_diff_view(struct tog_view *view)
2236 const struct got_error *err = NULL;
2238 free(view->state.diff.id1);
2239 view->state.diff.id1 = NULL;
2240 free(view->state.diff.id2);
2241 view->state.diff.id2 = NULL;
2242 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2243 err = got_error_from_errno();
2247 static const struct got_error *
2248 show_diff_view(struct tog_view *view)
2250 const struct got_error *err;
2251 struct tog_diff_view_state *s = &view->state.diff;
2252 char *id_str1 = NULL, *id_str2, *header;
2255 err = got_object_id_str(&id_str1, s->id1);
2259 err = got_object_id_str(&id_str2, s->id2);
2263 if (asprintf(&header, "diff %s %s",
2264 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2265 err = got_error_from_errno();
2273 return draw_file(view, s->f, &s->first_displayed_line,
2274 &s->last_displayed_line, &s->eof, view->nlines,
2278 static const struct got_error *
2279 set_selected_commit(struct tog_diff_view_state *s,
2280 struct commit_queue_entry *entry)
2282 const struct got_error *err;
2283 const struct got_object_id_queue *parent_ids;
2284 struct got_commit_object *selected_commit;
2285 struct got_object_qid *pid;
2288 s->id2 = got_object_id_dup(entry->id);
2290 return got_error_from_errno();
2292 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2295 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2297 pid = SIMPLEQ_FIRST(parent_ids);
2298 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2299 got_object_commit_close(selected_commit);
2303 static const struct got_error *
2304 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2305 struct tog_view **focus_view, struct tog_view *view, int ch)
2307 const struct got_error *err = NULL;
2308 struct tog_diff_view_state *s = &view->state.diff;
2309 struct tog_log_view_state *ls;
2310 struct commit_queue_entry *entry;
2316 if (s->first_displayed_line > 1)
2317 s->first_displayed_line--;
2322 if (s->first_displayed_line == 1) {
2327 while (i++ < view->nlines - 1 &&
2328 s->first_displayed_line > 1)
2329 s->first_displayed_line--;
2334 s->first_displayed_line++;
2345 while (!s->eof && i++ < view->nlines - 1) {
2347 line = parse_next_line(s->f, NULL);
2348 s->first_displayed_line++;
2354 if (s->diff_context > 0) {
2356 diff_view_indicate_progress(view);
2357 err = create_diff(s);
2361 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2363 diff_view_indicate_progress(view);
2364 err = create_diff(s);
2369 if (s->log_view == NULL)
2371 ls = &s->log_view->state.log;
2372 entry = TAILQ_PREV(ls->selected_entry,
2373 commit_queue_head, entry);
2377 err = input_log_view(NULL, NULL, NULL, s->log_view,
2382 err = set_selected_commit(s, entry);
2386 s->first_displayed_line = 1;
2387 s->last_displayed_line = view->nlines;
2389 diff_view_indicate_progress(view);
2390 err = create_diff(s);
2394 if (s->log_view == NULL)
2396 ls = &s->log_view->state.log;
2398 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2399 ls->thread_args.commits_needed++;
2401 /* Display "loading..." in log view. */
2402 show_log_view(s->log_view);
2406 err = trigger_log_thread(1 /* load_all */,
2407 &ls->thread_args.commits_needed,
2408 &ls->thread_args.log_complete,
2409 &ls->thread_args.need_commits);
2413 err = input_log_view(NULL, NULL, NULL, s->log_view,
2418 entry = TAILQ_NEXT(ls->selected_entry, entry);
2422 err = set_selected_commit(s, entry);
2426 s->first_displayed_line = 1;
2427 s->last_displayed_line = view->nlines;
2429 diff_view_indicate_progress(view);
2430 err = create_diff(s);
2439 static const struct got_error *
2440 cmd_diff(int argc, char *argv[])
2442 const struct got_error *error = NULL;
2443 struct got_repository *repo = NULL;
2444 struct got_reflist_head refs;
2445 struct got_object_id *id1 = NULL, *id2 = NULL;
2446 char *repo_path = NULL;
2447 char *id_str1 = NULL, *id_str2 = NULL;
2449 struct tog_view *view;
2452 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2457 while ((ch = getopt(argc, argv, "")) != -1) {
2469 usage_diff(); /* TODO show local worktree changes */
2470 } else if (argc == 2) {
2471 repo_path = getcwd(NULL, 0);
2472 if (repo_path == NULL)
2473 return got_error_from_errno();
2476 } else if (argc == 3) {
2477 repo_path = realpath(argv[0], NULL);
2478 if (repo_path == NULL)
2479 return got_error_from_errno();
2487 error = apply_unveil(repo_path, NULL);
2491 error = got_repo_open(&repo, repo_path);
2496 error = got_object_resolve_id_str(&id1, repo, id_str1);
2500 error = got_object_resolve_id_str(&id2, repo, id_str2);
2504 SIMPLEQ_INIT(&refs);
2505 error = got_ref_list(&refs, repo);
2509 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2511 error = got_error_from_errno();
2514 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2517 error = view_loop(view);
2519 got_repo_close(repo);
2527 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2532 struct tog_blame_line {
2534 struct got_object_id *id;
2537 static const struct got_error *
2538 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2539 const char *path, struct tog_blame_line *lines, int nlines,
2540 int blame_complete, int selected_line, int *first_displayed_line,
2541 int *last_displayed_line, int *eof, int max_lines)
2543 const struct got_error *err;
2544 int lineno = 0, nprinted = 0;
2549 struct tog_blame_line *blame_line;
2550 struct got_object_id *prev_id = NULL;
2553 err = got_object_id_str(&id_str, id);
2558 werase(view->window);
2560 if (asprintf(&line, "commit %s", id_str) == -1) {
2561 err = got_error_from_errno();
2566 err = format_line(&wline, &width, line, view->ncols);
2569 if (view_needs_focus_indication(view))
2570 wstandout(view->window);
2571 waddwstr(view->window, wline);
2572 if (view_needs_focus_indication(view))
2573 wstandend(view->window);
2576 if (width < view->ncols)
2577 waddch(view->window, '\n');
2579 if (asprintf(&line, "[%d/%d] %s%s",
2580 *first_displayed_line - 1 + selected_line, nlines,
2581 blame_complete ? "" : "annotating... ", path) == -1) {
2583 return got_error_from_errno();
2586 err = format_line(&wline, &width, line, view->ncols);
2591 waddwstr(view->window, wline);
2594 if (width < view->ncols)
2595 waddch(view->window, '\n');
2598 while (nprinted < max_lines - 2) {
2599 line = parse_next_line(f, &len);
2604 if (++lineno < *first_displayed_line) {
2609 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2610 err = format_line(&wline, &width, line, wlimit);
2616 if (view->focussed && nprinted == selected_line - 1)
2617 wstandout(view->window);
2619 blame_line = &lines[lineno - 1];
2620 if (blame_line->annotated && prev_id &&
2621 got_object_id_cmp(prev_id, blame_line->id) == 0)
2622 waddstr(view->window, " ");
2623 else if (blame_line->annotated) {
2625 err = got_object_id_str(&id_str, blame_line->id);
2631 wprintw(view->window, "%.8s ", id_str);
2633 prev_id = blame_line->id;
2635 waddstr(view->window, "........ ");
2639 waddwstr(view->window, wline);
2640 while (width < wlimit) {
2641 waddch(view->window, ' ');
2644 if (view->focussed && nprinted == selected_line - 1)
2645 wstandend(view->window);
2646 if (++nprinted == 1)
2647 *first_displayed_line = lineno;
2652 *last_displayed_line = lineno;
2659 static const struct got_error *
2660 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2662 const struct got_error *err = NULL;
2663 struct tog_blame_cb_args *a = arg;
2664 struct tog_blame_line *line;
2667 if (nlines != a->nlines ||
2668 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2669 return got_error(GOT_ERR_RANGE);
2671 errcode = pthread_mutex_lock(&tog_mutex);
2673 return got_error_set_errno(errcode);
2675 if (*a->quit) { /* user has quit the blame view */
2676 err = got_error(GOT_ERR_ITER_COMPLETED);
2681 goto done; /* no change in this commit */
2683 line = &a->lines[lineno - 1];
2684 if (line->annotated)
2687 line->id = got_object_id_dup(id);
2688 if (line->id == NULL) {
2689 err = got_error_from_errno();
2692 line->annotated = 1;
2694 errcode = pthread_mutex_unlock(&tog_mutex);
2696 err = got_error_set_errno(errcode);
2701 blame_thread(void *arg)
2703 const struct got_error *err;
2704 struct tog_blame_thread_args *ta = arg;
2705 struct tog_blame_cb_args *a = ta->cb_args;
2708 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2709 blame_cb, ta->cb_args);
2711 errcode = pthread_mutex_lock(&tog_mutex);
2713 return (void *)got_error_set_errno(errcode);
2715 got_repo_close(ta->repo);
2719 errcode = pthread_mutex_unlock(&tog_mutex);
2720 if (errcode && err == NULL)
2721 err = got_error_set_errno(errcode);
2726 static struct got_object_id *
2727 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2730 struct tog_blame_line *line;
2732 line = &lines[first_displayed_line - 1 + selected_line - 1];
2733 if (!line->annotated)
2739 static const struct got_error *
2740 stop_blame(struct tog_blame *blame)
2742 const struct got_error *err = NULL;
2745 if (blame->thread) {
2747 errcode = pthread_mutex_unlock(&tog_mutex);
2749 return got_error_set_errno(errcode);
2750 errcode = pthread_join(blame->thread, (void **)&err);
2752 return got_error_set_errno(errcode);
2753 errcode = pthread_mutex_lock(&tog_mutex);
2755 return got_error_set_errno(errcode);
2756 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2758 blame->thread = NULL;
2760 if (blame->thread_args.repo) {
2761 got_repo_close(blame->thread_args.repo);
2762 blame->thread_args.repo = NULL;
2765 if (fclose(blame->f) != 0 && err == NULL)
2766 err = got_error_from_errno();
2770 for (i = 0; i < blame->nlines; i++)
2771 free(blame->lines[i].id);
2773 blame->lines = NULL;
2775 free(blame->cb_args.commit_id);
2776 blame->cb_args.commit_id = NULL;
2781 static const struct got_error *
2782 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2783 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2784 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2785 struct got_repository *repo)
2787 const struct got_error *err = NULL;
2788 struct got_blob_object *blob = NULL;
2789 struct got_repository *thread_repo = NULL;
2790 struct got_object_id *obj_id = NULL;
2793 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2797 return got_error(GOT_ERR_NO_OBJ);
2799 err = got_object_get_type(&obj_type, repo, obj_id);
2803 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2804 err = got_error(GOT_ERR_OBJ_TYPE);
2808 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2811 blame->f = got_opentemp();
2812 if (blame->f == NULL) {
2813 err = got_error_from_errno();
2816 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2821 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2822 if (blame->lines == NULL) {
2823 err = got_error_from_errno();
2827 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2831 blame->cb_args.view = view;
2832 blame->cb_args.lines = blame->lines;
2833 blame->cb_args.nlines = blame->nlines;
2834 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2835 if (blame->cb_args.commit_id == NULL) {
2836 err = got_error_from_errno();
2839 blame->cb_args.quit = done;
2841 blame->thread_args.path = path;
2842 blame->thread_args.repo = thread_repo;
2843 blame->thread_args.cb_args = &blame->cb_args;
2844 blame->thread_args.complete = blame_complete;
2845 *blame_complete = 0;
2849 got_object_blob_close(blob);
2856 static const struct got_error *
2857 open_blame_view(struct tog_view *view, char *path,
2858 struct got_object_id *commit_id, struct got_reflist_head *refs,
2859 struct got_repository *repo)
2861 const struct got_error *err = NULL;
2862 struct tog_blame_view_state *s = &view->state.blame;
2864 SIMPLEQ_INIT(&s->blamed_commits);
2866 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2870 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2871 s->first_displayed_line = 1;
2872 s->last_displayed_line = view->nlines;
2873 s->selected_line = 1;
2874 s->blame_complete = 0;
2876 if (s->path == NULL)
2877 return got_error_from_errno();
2880 s->commit_id = commit_id;
2881 memset(&s->blame, 0, sizeof(s->blame));
2883 view->show = show_blame_view;
2884 view->input = input_blame_view;
2885 view->close = close_blame_view;
2887 return run_blame(&s->blame, view, &s->blame_complete,
2888 &s->first_displayed_line, &s->last_displayed_line,
2889 &s->selected_line, &s->done, &s->eof, s->path,
2890 s->blamed_commit->id, s->repo);
2893 static const struct got_error *
2894 close_blame_view(struct tog_view *view)
2896 const struct got_error *err = NULL;
2897 struct tog_blame_view_state *s = &view->state.blame;
2899 if (s->blame.thread)
2900 err = stop_blame(&s->blame);
2902 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2903 struct got_object_qid *blamed_commit;
2904 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2905 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2906 got_object_qid_free(blamed_commit);
2914 static const struct got_error *
2915 show_blame_view(struct tog_view *view)
2917 const struct got_error *err = NULL;
2918 struct tog_blame_view_state *s = &view->state.blame;
2921 if (s->blame.thread == NULL) {
2922 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2923 &s->blame.thread_args);
2925 return got_error_set_errno(errcode);
2928 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2929 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2930 s->selected_line, &s->first_displayed_line,
2931 &s->last_displayed_line, &s->eof, view->nlines);
2937 static const struct got_error *
2938 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2939 struct tog_view **focus_view, struct tog_view *view, int ch)
2941 const struct got_error *err = NULL, *thread_err = NULL;
2942 struct tog_view *diff_view;
2943 struct tog_blame_view_state *s = &view->state.blame;
2952 if (s->selected_line > 1)
2954 else if (s->selected_line == 1 &&
2955 s->first_displayed_line > 1)
2956 s->first_displayed_line--;
2961 if (s->first_displayed_line == 1) {
2962 if (s->selected_line == 1) {
2966 s->selected_line = 1;
2969 if (s->first_displayed_line > view->nlines - 2)
2970 s->first_displayed_line -=
2973 s->first_displayed_line = 1;
2977 if (s->selected_line < view->nlines - 2 &&
2978 s->first_displayed_line +
2979 s->selected_line <= s->blame.nlines)
2981 else if (s->last_displayed_line <
2983 s->first_displayed_line++;
2989 struct got_object_id *id = NULL;
2990 id = get_selected_commit_id(s->blame.lines,
2991 s->first_displayed_line, s->selected_line);
2995 struct got_commit_object *commit;
2996 struct got_object_qid *pid;
2997 struct got_object_id *blob_id = NULL;
2999 err = got_object_open_as_commit(&commit,
3003 pid = SIMPLEQ_FIRST(
3004 got_object_commit_get_parent_ids(commit));
3006 got_object_commit_close(commit);
3009 /* Check if path history ends here. */
3010 err = got_object_id_by_path(&blob_id, s->repo,
3013 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3015 got_object_commit_close(commit);
3018 err = got_object_get_type(&obj_type, s->repo,
3021 /* Can't blame non-blob type objects. */
3022 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3023 got_object_commit_close(commit);
3026 err = got_object_qid_alloc(&s->blamed_commit,
3028 got_object_commit_close(commit);
3030 if (got_object_id_cmp(id,
3031 s->blamed_commit->id) == 0)
3033 err = got_object_qid_alloc(&s->blamed_commit,
3039 thread_err = stop_blame(&s->blame);
3043 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3044 s->blamed_commit, entry);
3045 err = run_blame(&s->blame, view, &s->blame_complete,
3046 &s->first_displayed_line, &s->last_displayed_line,
3047 &s->selected_line, &s->done, &s->eof,
3048 s->path, s->blamed_commit->id, s->repo);
3054 struct got_object_qid *first;
3055 first = SIMPLEQ_FIRST(&s->blamed_commits);
3056 if (!got_object_id_cmp(first->id, s->commit_id))
3059 thread_err = stop_blame(&s->blame);
3063 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3064 got_object_qid_free(s->blamed_commit);
3066 SIMPLEQ_FIRST(&s->blamed_commits);
3067 err = run_blame(&s->blame, view, &s->blame_complete,
3068 &s->first_displayed_line, &s->last_displayed_line,
3069 &s->selected_line, &s->done, &s->eof, s->path,
3070 s->blamed_commit->id, s->repo);
3077 struct got_object_id *id = NULL;
3078 struct got_object_qid *pid;
3079 struct got_commit_object *commit = NULL;
3080 id = get_selected_commit_id(s->blame.lines,
3081 s->first_displayed_line, s->selected_line);
3084 err = got_object_open_as_commit(&commit, s->repo, id);
3087 pid = SIMPLEQ_FIRST(
3088 got_object_commit_get_parent_ids(commit));
3089 if (view_is_parent_view(view))
3090 begin_x = view_split_begin_x(view->begin_x);
3091 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3092 if (diff_view == NULL) {
3093 got_object_commit_close(commit);
3094 err = got_error_from_errno();
3097 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3098 id, NULL, s->refs, s->repo);
3099 got_object_commit_close(commit);
3101 view_close(diff_view);
3104 if (view_is_parent_view(view)) {
3105 err = view_close_child(view);
3108 err = view_set_child(view, diff_view);
3110 view_close(diff_view);
3113 *focus_view = diff_view;
3114 view->child_focussed = 1;
3116 *new_view = diff_view;
3123 if (s->last_displayed_line >= s->blame.nlines &&
3124 s->selected_line >= MIN(s->blame.nlines,
3125 view->nlines - 2)) {
3129 if (s->last_displayed_line >= s->blame.nlines &&
3130 s->selected_line < view->nlines - 2) {
3131 s->selected_line = MIN(s->blame.nlines,
3135 if (s->last_displayed_line + view->nlines - 2
3137 s->first_displayed_line +=
3140 s->first_displayed_line =
3145 if (s->selected_line > view->nlines - 2) {
3146 s->selected_line = MIN(s->blame.nlines,
3153 return thread_err ? thread_err : err;
3156 static const struct got_error *
3157 cmd_blame(int argc, char *argv[])
3159 const struct got_error *error;
3160 struct got_repository *repo = NULL;
3161 struct got_reflist_head refs;
3162 struct got_worktree *worktree = NULL;
3163 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3164 struct got_object_id *commit_id = NULL;
3165 char *commit_id_str = NULL;
3167 struct tog_view *view;
3170 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3175 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3178 commit_id_str = optarg;
3181 repo_path = realpath(optarg, NULL);
3182 if (repo_path == NULL)
3183 err(1, "-r option");
3199 cwd = getcwd(NULL, 0);
3201 error = got_error_from_errno();
3204 if (repo_path == NULL) {
3205 error = got_worktree_open(&worktree, cwd);
3206 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3212 strdup(got_worktree_get_repo_path(worktree));
3213 if (repo_path == NULL)
3214 error = got_error_from_errno();
3218 repo_path = strdup(cwd);
3219 if (repo_path == NULL) {
3220 error = got_error_from_errno();
3228 error = apply_unveil(repo_path, NULL);
3232 error = got_repo_open(&repo, repo_path);
3237 const char *prefix = got_worktree_get_path_prefix(worktree);
3238 char *p, *worktree_subdir = cwd +
3239 strlen(got_worktree_get_root_path(worktree));
3240 if (asprintf(&p, "%s%s%s%s%s",
3241 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3242 worktree_subdir, worktree_subdir[0] ? "/" : "",
3244 error = got_error_from_errno();
3247 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3250 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3255 if (commit_id_str == NULL) {
3256 struct got_reference *head_ref;
3257 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3260 error = got_ref_resolve(&commit_id, repo, head_ref);
3261 got_ref_close(head_ref);
3263 error = got_object_resolve_id_str(&commit_id, repo,
3269 SIMPLEQ_INIT(&refs);
3270 error = got_ref_list(&refs, repo);
3274 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3276 error = got_error_from_errno();
3279 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3282 error = view_loop(view);
3288 got_worktree_close(worktree);
3290 got_repo_close(repo);
3294 static const struct got_error *
3295 draw_tree_entries(struct tog_view *view,
3296 struct got_tree_entry **first_displayed_entry,
3297 struct got_tree_entry **last_displayed_entry,
3298 struct got_tree_entry **selected_entry, int *ndisplayed,
3299 const char *label, int show_ids, const char *parent_path,
3300 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3302 const struct got_error *err = NULL;
3303 struct got_tree_entry *te;
3309 werase(view->window);
3314 err = format_line(&wline, &width, label, view->ncols);
3317 if (view_needs_focus_indication(view))
3318 wstandout(view->window);
3319 waddwstr(view->window, wline);
3320 if (view_needs_focus_indication(view))
3321 wstandend(view->window);
3324 if (width < view->ncols)
3325 waddch(view->window, '\n');
3328 err = format_line(&wline, &width, parent_path, view->ncols);
3331 waddwstr(view->window, wline);
3334 if (width < view->ncols)
3335 waddch(view->window, '\n');
3338 waddch(view->window, '\n');
3342 te = SIMPLEQ_FIRST(&entries->head);
3343 if (*first_displayed_entry == NULL) {
3344 if (selected == 0) {
3346 wstandout(view->window);
3347 *selected_entry = NULL;
3349 waddstr(view->window, " ..\n"); /* parent directory */
3350 if (selected == 0 && view->focussed)
3351 wstandend(view->window);
3358 while (te != *first_displayed_entry)
3359 te = SIMPLEQ_NEXT(te, entry);
3363 char *line = NULL, *id_str = NULL;
3366 err = got_object_id_str(&id_str, te->id);
3368 return got_error_from_errno();
3370 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3371 te->name, S_ISDIR(te->mode) ? "/" :
3372 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3374 return got_error_from_errno();
3377 err = format_line(&wline, &width, line, view->ncols);
3382 if (n == selected) {
3384 wstandout(view->window);
3385 *selected_entry = te;
3387 waddwstr(view->window, wline);
3388 if (width < view->ncols)
3389 waddch(view->window, '\n');
3390 if (n == selected && view->focussed)
3391 wstandend(view->window);
3397 *last_displayed_entry = te;
3400 te = SIMPLEQ_NEXT(te, entry);
3407 tree_scroll_up(struct tog_view *view,
3408 struct got_tree_entry **first_displayed_entry, int maxscroll,
3409 const struct got_tree_entries *entries, int isroot)
3411 struct got_tree_entry *te, *prev;
3414 if (*first_displayed_entry == NULL) {
3419 te = SIMPLEQ_FIRST(&entries->head);
3420 if (*first_displayed_entry == te) {
3423 *first_displayed_entry = NULL;
3427 /* XXX this is stupid... switch to TAILQ? */
3428 for (i = 0; i < maxscroll; i++) {
3429 while (te != *first_displayed_entry) {
3431 te = SIMPLEQ_NEXT(te, entry);
3433 *first_displayed_entry = prev;
3434 te = SIMPLEQ_FIRST(&entries->head);
3436 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3437 *first_displayed_entry = NULL;
3441 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3442 struct got_tree_entry *last_displayed_entry,
3443 const struct got_tree_entries *entries)
3445 struct got_tree_entry *next, *last;
3448 if (*first_displayed_entry)
3449 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3451 next = SIMPLEQ_FIRST(&entries->head);
3452 last = last_displayed_entry;
3453 while (next && last && n++ < maxscroll) {
3454 last = SIMPLEQ_NEXT(last, entry);
3456 *first_displayed_entry = next;
3457 next = SIMPLEQ_NEXT(next, entry);
3463 static const struct got_error *
3464 tree_entry_path(char **path, struct tog_parent_trees *parents,
3465 struct got_tree_entry *te)
3467 const struct got_error *err = NULL;
3468 struct tog_parent_tree *pt;
3469 size_t len = 2; /* for leading slash and NUL */
3471 TAILQ_FOREACH(pt, parents, entry)
3472 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3474 len += strlen(te->name);
3476 *path = calloc(1, len);
3478 return got_error_from_errno();
3481 pt = TAILQ_LAST(parents, tog_parent_trees);
3483 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3484 err = got_error(GOT_ERR_NO_SPACE);
3487 if (strlcat(*path, "/", len) >= len) {
3488 err = got_error(GOT_ERR_NO_SPACE);
3491 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3494 if (strlcat(*path, te->name, len) >= len) {
3495 err = got_error(GOT_ERR_NO_SPACE);
3507 static const struct got_error *
3508 blame_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 const struct got_error *err = NULL;
3515 struct tog_view *blame_view;
3517 err = tree_entry_path(&path, parents, te);
3521 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3522 if (blame_view == NULL)
3523 return got_error_from_errno();
3525 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3527 view_close(blame_view);
3530 *new_view = blame_view;
3534 static const struct got_error *
3535 log_tree_entry(struct tog_view **new_view, int begin_x,
3536 struct got_tree_entry *te, struct tog_parent_trees *parents,
3537 struct got_object_id *commit_id, struct got_reflist_head *refs,
3538 struct got_repository *repo)
3540 struct tog_view *log_view;
3541 const struct got_error *err = NULL;
3544 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3545 if (log_view == NULL)
3546 return got_error_from_errno();
3548 err = tree_entry_path(&path, parents, te);
3552 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3554 view_close(log_view);
3556 *new_view = log_view;
3561 static const struct got_error *
3562 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3563 struct got_object_id *commit_id, struct got_reflist_head *refs,
3564 struct got_repository *repo)
3566 const struct got_error *err = NULL;
3567 char *commit_id_str = NULL;
3568 struct tog_tree_view_state *s = &view->state.tree;
3570 TAILQ_INIT(&s->parents);
3572 err = got_object_id_str(&commit_id_str, commit_id);
3576 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3577 err = got_error_from_errno();
3581 s->root = s->tree = root;
3582 s->entries = got_object_tree_get_entries(root);
3583 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3584 s->commit_id = got_object_id_dup(commit_id);
3585 if (s->commit_id == NULL) {
3586 err = got_error_from_errno();
3592 view->show = show_tree_view;
3593 view->input = input_tree_view;
3594 view->close = close_tree_view;
3596 free(commit_id_str);
3598 free(s->tree_label);
3599 s->tree_label = NULL;
3604 static const struct got_error *
3605 close_tree_view(struct tog_view *view)
3607 struct tog_tree_view_state *s = &view->state.tree;
3609 free(s->tree_label);
3610 s->tree_label = NULL;
3612 s->commit_id = NULL;
3613 while (!TAILQ_EMPTY(&s->parents)) {
3614 struct tog_parent_tree *parent;
3615 parent = TAILQ_FIRST(&s->parents);
3616 TAILQ_REMOVE(&s->parents, parent, entry);
3620 if (s->tree != s->root)
3621 got_object_tree_close(s->tree);
3622 got_object_tree_close(s->root);
3627 static const struct got_error *
3628 show_tree_view(struct tog_view *view)
3630 const struct got_error *err = NULL;
3631 struct tog_tree_view_state *s = &view->state.tree;
3634 err = tree_entry_path(&parent_path, &s->parents, NULL);
3638 err = draw_tree_entries(view, &s->first_displayed_entry,
3639 &s->last_displayed_entry, &s->selected_entry,
3640 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3641 s->entries, s->selected, view->nlines, s->tree == s->root);
3648 static const struct got_error *
3649 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3650 struct tog_view **focus_view, struct tog_view *view, int ch)
3652 const struct got_error *err = NULL;
3653 struct tog_tree_view_state *s = &view->state.tree;
3654 struct tog_view *log_view;
3655 int begin_x = 0, nscrolled;
3659 s->show_ids = !s->show_ids;
3662 if (!s->selected_entry)
3664 if (view_is_parent_view(view))
3665 begin_x = view_split_begin_x(view->begin_x);
3666 err = log_tree_entry(&log_view, begin_x,
3667 s->selected_entry, &s->parents,
3668 s->commit_id, s->refs, s->repo);
3669 if (view_is_parent_view(view)) {
3670 err = view_close_child(view);
3673 err = view_set_child(view, log_view);
3675 view_close(log_view);
3678 *focus_view = log_view;
3679 view->child_focussed = 1;
3681 *new_view = log_view;
3685 if (s->selected > 0) {
3687 if (s->selected == 0)
3690 if (s->selected > 0)
3692 tree_scroll_up(view, &s->first_displayed_entry, 1,
3693 s->entries, s->tree == s->root);
3696 tree_scroll_up(view, &s->first_displayed_entry,
3697 MAX(0, view->nlines - 4 - s->selected), s->entries,
3698 s->tree == s->root);
3700 if (SIMPLEQ_FIRST(&s->entries->head) ==
3701 s->first_displayed_entry && s->tree != s->root)
3702 s->first_displayed_entry = NULL;
3706 if (s->selected < s->ndisplayed - 1) {
3710 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3712 /* can't scroll any further */
3716 tree_scroll_down(&s->first_displayed_entry, 1,
3717 s->last_displayed_entry, s->entries);
3720 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3722 /* can't scroll any further; move cursor down */
3723 if (s->selected < s->ndisplayed - 1)
3724 s->selected = s->ndisplayed - 1;
3729 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3730 view->nlines, s->last_displayed_entry, s->entries);
3731 if (nscrolled < view->nlines) {
3733 struct got_tree_entry *te;
3734 te = s->first_displayed_entry;
3737 te = SIMPLEQ_NEXT(te, entry);
3739 s->selected = ndisplayed - 1;
3744 if (s->selected_entry == NULL) {
3745 struct tog_parent_tree *parent;
3747 /* user selected '..' */
3748 if (s->tree == s->root)
3750 parent = TAILQ_FIRST(&s->parents);
3751 TAILQ_REMOVE(&s->parents, parent,
3753 got_object_tree_close(s->tree);
3754 s->tree = parent->tree;
3756 got_object_tree_get_entries(s->tree);
3757 s->first_displayed_entry =
3758 parent->first_displayed_entry;
3760 parent->selected_entry;
3761 s->selected = parent->selected;
3763 } else if (S_ISDIR(s->selected_entry->mode)) {
3764 struct tog_parent_tree *parent;
3765 struct got_tree_object *child;
3766 err = got_object_open_as_tree(&child,
3767 s->repo, s->selected_entry->id);
3770 parent = calloc(1, sizeof(*parent));
3771 if (parent == NULL) {
3772 err = got_error_from_errno();
3775 parent->tree = s->tree;
3776 parent->first_displayed_entry =
3777 s->first_displayed_entry;
3778 parent->selected_entry = s->selected_entry;
3779 parent->selected = s->selected;
3780 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3783 got_object_tree_get_entries(s->tree);
3785 s->first_displayed_entry = NULL;
3786 } else if (S_ISREG(s->selected_entry->mode)) {
3787 struct tog_view *blame_view;
3788 int begin_x = view_is_parent_view(view) ?
3789 view_split_begin_x(view->begin_x) : 0;
3791 err = blame_tree_entry(&blame_view, begin_x,
3792 s->selected_entry, &s->parents,
3793 s->commit_id, s->refs, s->repo);
3796 if (view_is_parent_view(view)) {
3797 err = view_close_child(view);
3800 err = view_set_child(view, blame_view);
3802 view_close(blame_view);
3805 *focus_view = blame_view;
3806 view->child_focussed = 1;
3808 *new_view = blame_view;
3812 if (s->selected > view->nlines)
3813 s->selected = s->ndisplayed - 1;
3826 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3831 static const struct got_error *
3832 cmd_tree(int argc, char *argv[])
3834 const struct got_error *error;
3835 struct got_repository *repo = NULL;
3836 struct got_reflist_head refs;
3837 char *repo_path = NULL;
3838 struct got_object_id *commit_id = NULL;
3839 char *commit_id_arg = NULL;
3840 struct got_commit_object *commit = NULL;
3841 struct got_tree_object *tree = NULL;
3843 struct tog_view *view;
3846 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3851 while ((ch = getopt(argc, argv, "c:")) != -1) {
3854 commit_id_arg = optarg;
3866 struct got_worktree *worktree;
3867 char *cwd = getcwd(NULL, 0);
3869 return got_error_from_errno();
3870 error = got_worktree_open(&worktree, cwd);
3871 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3876 strdup(got_worktree_get_repo_path(worktree));
3877 got_worktree_close(worktree);
3880 if (repo_path == NULL) {
3881 error = got_error_from_errno();
3884 } else if (argc == 1) {
3885 repo_path = realpath(argv[0], NULL);
3886 if (repo_path == NULL)
3887 return got_error_from_errno();
3893 error = apply_unveil(repo_path, NULL);
3897 error = got_repo_open(&repo, repo_path);
3901 if (commit_id_arg == NULL)
3902 error = get_head_commit_id(&commit_id, repo);
3904 error = got_object_resolve_id_str(&commit_id, repo,
3909 error = got_object_open_as_commit(&commit, repo, commit_id);
3913 error = got_object_open_as_tree(&tree, repo,
3914 got_object_commit_get_tree_id(commit));
3918 SIMPLEQ_INIT(&refs);
3919 error = got_ref_list(&refs, repo);
3923 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3925 error = got_error_from_errno();
3928 error = open_tree_view(view, tree, commit_id, &refs, repo);
3931 error = view_loop(view);
3936 got_object_commit_close(commit);
3938 got_object_tree_close(tree);
3940 got_repo_close(repo);
3949 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3950 "Available commands:\n", getprogname());
3951 for (i = 0; i < nitems(tog_commands); i++) {
3952 struct tog_cmd *cmd = &tog_commands[i];
3953 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3959 make_argv(const char *arg0, const char *arg1)
3962 int argc = (arg1 == NULL ? 1 : 2);
3964 argv = calloc(argc, sizeof(char *));
3967 argv[0] = strdup(arg0);
3968 if (argv[0] == NULL)
3971 argv[1] = strdup(arg1);
3972 if (argv[1] == NULL)
3980 main(int argc, char *argv[])
3982 const struct got_error *error = NULL;
3983 struct tog_cmd *cmd = NULL;
3985 char **cmd_argv = NULL;
3987 setlocale(LC_CTYPE, "");
3989 while ((ch = getopt(argc, argv, "h")) != -1) {
4008 /* Build an argument vector which runs a default command. */
4009 cmd = &tog_commands[0];
4010 cmd_argv = make_argv(cmd->name, NULL);
4015 /* Did the user specific a command? */
4016 for (i = 0; i < nitems(tog_commands); i++) {
4017 if (strncmp(tog_commands[i].name, argv[0],
4018 strlen(argv[0])) == 0) {
4019 cmd = &tog_commands[i];
4021 tog_commands[i].cmd_usage();
4026 /* Did the user specify a repository? */
4027 char *repo_path = realpath(argv[0], NULL);
4029 struct got_repository *repo;
4030 error = got_repo_open(&repo, repo_path);
4032 got_repo_close(repo);
4034 error = got_error_from_errno();
4037 fprintf(stderr, "%s: '%s' is not a "
4038 "known command\n", getprogname(),
4042 fprintf(stderr, "%s: '%s' is neither a known "
4043 "command nor a path to a repository\n",
4044 getprogname(), argv[0]);
4048 cmd = &tog_commands[0];
4049 cmd_argv = make_argv(cmd->name, repo_path);
4055 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4062 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);