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_is_parent_view(view) && view->begin_x > 0;
498 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
499 cols = 80; /* Default */
505 resize_term(lines, cols);
508 static const struct got_error *
509 view_input(struct tog_view **new, struct tog_view **dead,
510 struct tog_view **focus, int *done, struct tog_view *view,
511 struct tog_view_list_head *views)
513 const struct got_error *err = NULL;
521 nodelay(stdscr, FALSE);
522 /* Allow threads to make progress while we are waiting for input. */
523 errcode = pthread_mutex_unlock(&tog_mutex);
525 return got_error_set_errno(errcode);
526 ch = wgetch(view->window);
527 errcode = pthread_mutex_lock(&tog_mutex);
529 return got_error_set_errno(errcode);
530 nodelay(stdscr, TRUE);
532 if (tog_sigwinch_received) {
534 tog_sigwinch_received = 0;
535 TAILQ_FOREACH(v, views, entry) {
536 err = view_resize(v);
539 err = v->input(new, dead, focus, v, KEY_RESIZE);
550 *focus = view->child;
551 view->child_focussed = 1;
552 } else if (view->parent) {
553 *focus = view->parent;
554 view->parent->child_focussed = 0;
558 err = view->input(new, dead, focus, view, ch);
565 if (view_is_parent_view(view)) {
566 if (view->child == NULL)
568 if (view_is_splitscreen(view->child)) {
569 *focus = view->child;
570 view->child_focussed = 1;
571 err = view_fullscreen(view->child);
573 err = view_splitscreen(view->child);
576 err = view->child->input(new, dead, focus,
577 view->child, KEY_RESIZE);
579 if (view_is_splitscreen(view)) {
581 view->parent->child_focussed = 1;
582 err = view_fullscreen(view);
584 err = view_splitscreen(view);
588 err = view->input(new, dead, focus, view,
595 err = view->input(new, dead, focus, view, ch);
603 view_vborder(struct tog_view *view)
606 struct tog_view *view_above;
609 return view_vborder(view->parent);
611 panel = panel_above(view->panel);
615 view_above = panel_userptr(panel);
616 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
617 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
621 view_needs_focus_indication(struct tog_view *view)
623 if (view_is_parent_view(view)) {
624 if (view->child == NULL || view->child_focussed)
626 if (!view_is_splitscreen(view->child))
628 } else if (!view_is_splitscreen(view))
631 return view->focussed;
634 static const struct got_error *
635 view_loop(struct tog_view *view)
637 const struct got_error *err = NULL;
638 struct tog_view_list_head views;
639 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
640 int fast_refresh = 10;
641 int done = 0, errcode;
643 errcode = pthread_mutex_lock(&tog_mutex);
645 return got_error_set_errno(errcode);
648 TAILQ_INSERT_HEAD(&views, view, entry);
652 err = view->show(view);
657 while (!TAILQ_EMPTY(&views) && !done) {
658 /* Refresh fast during initialization, then become slower. */
659 if (fast_refresh && fast_refresh-- == 0)
660 halfdelay(10); /* switch to once per second */
662 err = view_input(&new_view, &dead_view, &focus_view, &done,
667 struct tog_view *prev = NULL;
669 if (view_is_parent_view(dead_view))
670 prev = TAILQ_PREV(dead_view,
671 tog_view_list_head, entry);
672 else if (view->parent != dead_view)
675 if (dead_view->parent)
676 dead_view->parent->child = NULL;
678 TAILQ_REMOVE(&views, dead_view, entry);
680 err = view_close(dead_view);
681 if (err || dead_view == main_view)
684 if (view == dead_view) {
689 else if (!TAILQ_EMPTY(&views))
690 view = TAILQ_LAST(&views,
695 if (view->child && view->child_focussed)
696 focus_view = view->child;
703 struct tog_view *v, *t;
704 /* Only allow one parent view per type. */
705 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
706 if (v->type != new_view->type)
708 TAILQ_REMOVE(&views, v, entry);
714 TAILQ_INSERT_TAIL(&views, new_view, entry);
716 if (focus_view == NULL)
717 focus_view = new_view;
720 show_panel(focus_view->panel);
723 focus_view->focussed = 1;
726 show_panel(new_view->panel);
727 if (view->child && view_is_splitscreen(view->child))
728 show_panel(view->child->panel);
731 if (focus_view == NULL) {
733 show_panel(view->panel);
734 if (view->child && view_is_splitscreen(view->child))
735 show_panel(view->child->panel);
739 err = view->parent->show(view->parent);
743 err = view->show(view);
747 err = view->child->show(view->child);
756 while (!TAILQ_EMPTY(&views)) {
757 view = TAILQ_FIRST(&views);
758 TAILQ_REMOVE(&views, view, entry);
762 errcode = pthread_mutex_unlock(&tog_mutex);
764 return got_error_set_errno(errcode);
774 "usage: %s log [-c commit] [-r repository-path] [path]\n",
779 /* Create newly allocated wide-character string equivalent to a byte string. */
780 static const struct got_error *
781 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
784 const struct got_error *err = NULL;
787 *wlen = mbstowcs(NULL, s, 0);
788 if (*wlen == (size_t)-1) {
791 return got_error_from_errno();
793 /* byte string invalid in current encoding; try to "fix" it */
794 err = got_mbsavis(&vis, &vislen, s);
797 *wlen = mbstowcs(NULL, vis, 0);
798 if (*wlen == (size_t)-1) {
799 err = got_error_from_errno(); /* give up */
804 *ws = calloc(*wlen + 1, sizeof(*ws));
806 err = got_error_from_errno();
810 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
811 err = got_error_from_errno();
822 /* Format a line for display, ensuring that it won't overflow a width limit. */
823 static const struct got_error *
824 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
826 const struct got_error *err = NULL;
828 wchar_t *wline = NULL;
835 err = mbs2ws(&wline, &wlen, line);
840 while (i < wlen && cols < wlimit) {
841 int width = wcwidth(wline[i]);
848 if (cols + width <= wlimit)
853 if (wline[i] == L'\t')
854 cols += TABSIZE - ((cols + 1) % TABSIZE);
858 err = got_error_from_errno();
873 static const struct got_error*
874 build_refs_str(char **refs_str, struct got_reflist_head *refs,
875 struct got_object_id *id)
877 static const struct got_error *err = NULL;
878 struct got_reflist_entry *re;
884 SIMPLEQ_FOREACH(re, refs, entry) {
885 if (got_object_id_cmp(re->id, id) != 0)
887 name = got_ref_get_name(re->ref);
888 if (strcmp(name, GOT_REF_HEAD) == 0)
890 if (strncmp(name, "refs/", 5) == 0)
892 if (strncmp(name, "heads/", 6) == 0)
894 if (strncmp(name, "remotes/", 8) == 0)
897 if (asprintf(refs_str, "%s%s%s", s ? s : "",
898 s ? ", " : "", name) == -1) {
899 err = got_error_from_errno();
910 static const struct got_error *
911 draw_commit(struct tog_view *view, struct got_commit_object *commit,
912 struct got_object_id *id, struct got_reflist_head *refs)
914 const struct got_error *err = NULL;
915 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
916 char *logmsg0 = NULL, *logmsg = NULL;
917 char *author0 = NULL, *author = NULL;
918 wchar_t *wlogmsg = NULL, *wauthor = NULL;
919 int author_width, logmsg_width;
920 char *newline, *smallerthan;
923 static const size_t date_display_cols = 9;
924 static const size_t author_display_cols = 16;
925 const int avail = view->ncols;
927 time_t committer_time;
929 committer_time = got_object_commit_get_committer_time(commit);
930 if (localtime_r(&committer_time, &tm) == NULL)
931 return got_error_from_errno();
932 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
934 return got_error(GOT_ERR_NO_SPACE);
936 if (avail < date_display_cols)
937 limit = MIN(sizeof(datebuf) - 1, avail);
939 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
940 waddnstr(view->window, datebuf, limit);
945 author0 = strdup(got_object_commit_get_author(commit));
946 if (author0 == NULL) {
947 err = got_error_from_errno();
951 smallerthan = strchr(author, '<');
955 char *at = strchr(author, '@');
960 err = format_line(&wauthor, &author_width, author, limit);
963 waddwstr(view->window, wauthor);
965 while (col <= avail && author_width < author_display_cols + 1) {
966 waddch(view->window, ' ');
973 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
974 if (logmsg0 == NULL) {
975 err = got_error_from_errno();
979 while (*logmsg == '\n')
981 newline = strchr(logmsg, '\n');
985 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
988 waddwstr(view->window, wlogmsg);
990 while (col <= avail) {
991 waddch(view->window, ' ');
1003 static struct commit_queue_entry *
1004 alloc_commit_queue_entry(struct got_commit_object *commit,
1005 struct got_object_id *id)
1007 struct commit_queue_entry *entry;
1009 entry = calloc(1, sizeof(*entry));
1014 entry->commit = commit;
1019 pop_commit(struct commit_queue *commits)
1021 struct commit_queue_entry *entry;
1023 entry = TAILQ_FIRST(&commits->head);
1024 TAILQ_REMOVE(&commits->head, entry, entry);
1025 got_object_commit_close(entry->commit);
1026 commits->ncommits--;
1027 /* Don't free entry->id! It is owned by the commit graph. */
1032 free_commits(struct commit_queue *commits)
1034 while (!TAILQ_EMPTY(&commits->head))
1035 pop_commit(commits);
1038 static const struct got_error *
1039 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1040 int minqueue, struct got_repository *repo, const char *path)
1042 const struct got_error *err = NULL;
1046 * We keep all commits open throughout the lifetime of the log
1047 * view in order to avoid having to re-fetch commits from disk
1048 * while updating the display.
1050 while (nqueued < minqueue) {
1051 struct got_object_id *id;
1052 struct got_commit_object *commit;
1053 struct commit_queue_entry *entry;
1056 err = got_commit_graph_iter_next(&id, graph);
1058 if (err->code != GOT_ERR_ITER_NEED_MORE)
1060 err = got_commit_graph_fetch_commits(graph,
1070 err = got_object_open_as_commit(&commit, repo, id);
1073 entry = alloc_commit_queue_entry(commit, id);
1074 if (entry == NULL) {
1075 err = got_error_from_errno();
1079 errcode = pthread_mutex_lock(&tog_mutex);
1081 err = got_error_set_errno(errcode);
1085 entry->idx = commits->ncommits;
1086 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1088 commits->ncommits++;
1090 errcode = pthread_mutex_unlock(&tog_mutex);
1091 if (errcode && err == NULL)
1092 err = got_error_set_errno(errcode);
1098 static const struct got_error *
1099 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1101 const struct got_error *err = NULL;
1102 struct got_reference *head_ref;
1106 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1110 err = got_ref_resolve(head_id, repo, head_ref);
1111 got_ref_close(head_ref);
1120 static const struct got_error *
1121 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1122 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1123 struct commit_queue *commits, int selected_idx, int limit,
1124 struct got_reflist_head *refs, const char *path, int commits_needed)
1126 const struct got_error *err = NULL;
1127 struct commit_queue_entry *entry;
1128 int ncommits, width;
1129 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1130 char *refs_str = NULL;
1136 if (ncommits == selected_idx) {
1140 entry = TAILQ_NEXT(entry, entry);
1145 err = got_object_id_str(&id_str, (*selected)->id);
1149 err = build_refs_str(&refs_str, refs, (*selected)->id);
1155 if (commits_needed == 0)
1156 halfdelay(10); /* disable fast refresh */
1158 if (asprintf(&ncommits_str, " [%d/%d] %s",
1159 entry ? entry->idx + 1 : 0, commits->ncommits,
1160 commits_needed > 0 ? "loading... " :
1161 (refs_str ? refs_str : "")) == -1) {
1162 err = got_error_from_errno();
1166 if (path && strcmp(path, "/") != 0) {
1167 if (asprintf(&header, "commit %s %s%s",
1168 id_str ? id_str : "........................................",
1169 path, ncommits_str) == -1) {
1170 err = got_error_from_errno();
1174 } else if (asprintf(&header, "commit %s%s",
1175 id_str ? id_str : "........................................",
1176 ncommits_str) == -1) {
1177 err = got_error_from_errno();
1181 err = format_line(&wline, &width, header, view->ncols);
1185 werase(view->window);
1187 if (view_needs_focus_indication(view))
1188 wstandout(view->window);
1189 waddwstr(view->window, wline);
1190 while (width < view->ncols) {
1191 waddch(view->window, ' ');
1194 if (view_needs_focus_indication(view))
1195 wstandend(view->window);
1204 if (ncommits >= limit - 1)
1206 if (view->focussed && ncommits == selected_idx)
1207 wstandout(view->window);
1208 err = draw_commit(view, entry->commit, entry->id, refs);
1209 if (view->focussed && ncommits == selected_idx)
1210 wstandend(view->window);
1215 entry = TAILQ_NEXT(entry, entry);
1228 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1229 struct commit_queue *commits)
1231 struct commit_queue_entry *entry;
1234 entry = TAILQ_FIRST(&commits->head);
1235 if (*first_displayed_entry == entry)
1238 entry = *first_displayed_entry;
1239 while (entry && nscrolled < maxscroll) {
1240 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1242 *first_displayed_entry = entry;
1248 static const struct got_error *
1249 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1250 pthread_cond_t *need_commits)
1254 while (*commits_needed > 0) {
1258 /* Wake the log thread. */
1259 errcode = pthread_cond_signal(need_commits);
1261 return got_error_set_errno(errcode);
1262 errcode = pthread_mutex_unlock(&tog_mutex);
1264 return got_error_set_errno(errcode);
1266 errcode = pthread_mutex_lock(&tog_mutex);
1268 return got_error_set_errno(errcode);
1270 if (*commits_needed > 0 && !load_all) {
1272 * Thread is not done yet; lose a key press
1273 * and let the user retry... this way the GUI
1274 * remains interactive while logging deep paths
1275 * with few commits in history.
1277 halfdelay(1); /* fast refresh while loading */
1285 static const struct got_error *
1286 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1287 struct commit_queue_entry **last_displayed_entry,
1288 struct commit_queue *commits, int *log_complete, int *commits_needed,
1289 pthread_cond_t *need_commits)
1291 const struct got_error *err = NULL;
1292 struct commit_queue_entry *pentry;
1295 if (*last_displayed_entry == NULL)
1298 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1299 if (pentry == NULL && !*log_complete) {
1301 * Ask the log thread for required amount of commits
1302 * plus some amount of pre-fetching.
1304 (*commits_needed) += maxscroll + 20;
1305 err = trigger_log_thread(0, commits_needed, log_complete,
1312 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1316 *last_displayed_entry = pentry;
1318 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1321 *first_displayed_entry = pentry;
1322 } while (++nscrolled < maxscroll);
1327 static const struct got_error *
1328 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1329 struct got_commit_object *commit, struct got_object_id *commit_id,
1330 struct tog_view *log_view, struct got_reflist_head *refs,
1331 struct got_repository *repo)
1333 const struct got_error *err;
1334 struct got_object_qid *parent_id;
1335 struct tog_view *diff_view;
1337 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1338 if (diff_view == NULL)
1339 return got_error_from_errno();
1341 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1342 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1343 commit_id, log_view, refs, repo);
1345 *new_view = diff_view;
1349 static const struct got_error *
1350 browse_commit(struct tog_view **new_view, int begin_x,
1351 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1352 struct got_repository *repo)
1354 const struct got_error *err = NULL;
1355 struct got_tree_object *tree;
1356 struct tog_view *tree_view;
1358 err = got_object_open_as_tree(&tree, repo,
1359 got_object_commit_get_tree_id(entry->commit));
1363 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1364 if (tree_view == NULL)
1365 return got_error_from_errno();
1367 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1369 got_object_tree_close(tree);
1371 *new_view = tree_view;
1376 log_thread(void *arg)
1378 const struct got_error *err = NULL;
1380 struct tog_log_thread_args *a = arg;
1383 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1387 while (!done && !err) {
1388 err = queue_commits(a->graph, a->commits, 1, a->repo,
1391 if (err->code != GOT_ERR_ITER_COMPLETED)
1395 } else if (a->commits_needed > 0)
1396 a->commits_needed--;
1398 errcode = pthread_mutex_lock(&tog_mutex);
1400 return (void *)got_error_set_errno(errcode);
1403 a->log_complete = 1;
1404 else if (*a->quit) {
1406 a->log_complete = 1;
1407 } else if (*a->first_displayed_entry == NULL) {
1408 *a->first_displayed_entry =
1409 TAILQ_FIRST(&a->commits->head);
1410 *a->selected_entry = *a->first_displayed_entry;
1414 a->commits_needed = 0;
1415 else if (a->commits_needed == 0) {
1416 errcode = pthread_cond_wait(&a->need_commits,
1419 err = got_error_set_errno(errcode);
1422 errcode = pthread_mutex_unlock(&tog_mutex);
1423 if (errcode && err == NULL)
1424 err = got_error_set_errno(errcode);
1429 static const struct got_error *
1430 stop_log_thread(struct tog_log_view_state *s)
1432 const struct got_error *err = NULL;
1437 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1439 return got_error_set_errno(errcode);
1440 errcode = pthread_mutex_unlock(&tog_mutex);
1442 return got_error_set_errno(errcode);
1443 errcode = pthread_join(s->thread, (void **)&err);
1445 return got_error_set_errno(errcode);
1446 errcode = pthread_mutex_lock(&tog_mutex);
1448 return got_error_set_errno(errcode);
1452 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1453 if (errcode && err == NULL)
1454 err = got_error_set_errno(errcode);
1456 if (s->thread_args.repo) {
1457 got_repo_close(s->thread_args.repo);
1458 s->thread_args.repo = NULL;
1461 if (s->thread_args.graph) {
1462 got_commit_graph_close(s->thread_args.graph);
1463 s->thread_args.graph = NULL;
1469 static const struct got_error *
1470 close_log_view(struct tog_view *view)
1472 const struct got_error *err = NULL;
1473 struct tog_log_view_state *s = &view->state.log;
1475 err = stop_log_thread(s);
1476 free_commits(&s->commits);
1477 free(s->in_repo_path);
1478 s->in_repo_path = NULL;
1484 static const struct got_error *
1485 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1486 struct got_reflist_head *refs, struct got_repository *repo,
1487 const char *path, int check_disk)
1489 const struct got_error *err = NULL;
1490 struct tog_log_view_state *s = &view->state.log;
1491 struct got_repository *thread_repo = NULL;
1492 struct got_commit_graph *thread_graph = NULL;
1495 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1499 /* The commit queue only contains commits being displayed. */
1500 TAILQ_INIT(&s->commits.head);
1501 s->commits.ncommits = 0;
1505 s->start_id = got_object_id_dup(start_id);
1506 if (s->start_id == NULL) {
1507 err = got_error_from_errno();
1511 view->show = show_log_view;
1512 view->input = input_log_view;
1513 view->close = close_log_view;
1515 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1518 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1523 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1525 err = got_error_set_errno(errcode);
1529 s->thread_args.commits_needed = view->nlines;
1530 s->thread_args.graph = thread_graph;
1531 s->thread_args.commits = &s->commits;
1532 s->thread_args.in_repo_path = s->in_repo_path;
1533 s->thread_args.start_id = s->start_id;
1534 s->thread_args.repo = thread_repo;
1535 s->thread_args.log_complete = 0;
1536 s->thread_args.quit = &s->quit;
1537 s->thread_args.view = view;
1538 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1539 s->thread_args.selected_entry = &s->selected_entry;
1542 close_log_view(view);
1546 static const struct got_error *
1547 show_log_view(struct tog_view *view)
1549 struct tog_log_view_state *s = &view->state.log;
1551 if (s->thread == NULL) {
1552 int errcode = pthread_create(&s->thread, NULL, log_thread,
1555 return got_error_set_errno(errcode);
1558 return draw_commits(view, &s->last_displayed_entry,
1559 &s->selected_entry, s->first_displayed_entry,
1560 &s->commits, s->selected, view->nlines, s->refs,
1561 s->in_repo_path, s->thread_args.commits_needed);
1564 static const struct got_error *
1565 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1566 struct tog_view **focus_view, struct tog_view *view, int ch)
1568 const struct got_error *err = NULL;
1569 struct tog_log_view_state *s = &view->state.log;
1571 struct tog_view *diff_view = NULL, *tree_view = NULL;
1582 if (s->first_displayed_entry == NULL)
1584 if (s->selected > 0)
1586 if (s->selected > 0)
1588 scroll_up(&s->first_displayed_entry, 1,
1592 if (s->first_displayed_entry == NULL)
1594 if (TAILQ_FIRST(&s->commits.head) ==
1595 s->first_displayed_entry) {
1599 scroll_up(&s->first_displayed_entry,
1600 view->nlines, &s->commits);
1606 if (s->first_displayed_entry == NULL)
1608 if (s->selected < MIN(view->nlines - 2,
1609 s->commits.ncommits - 1)) {
1613 err = scroll_down(&s->first_displayed_entry, 1,
1614 &s->last_displayed_entry, &s->commits,
1615 &s->thread_args.log_complete,
1616 &s->thread_args.commits_needed,
1617 &s->thread_args.need_commits);
1620 struct commit_queue_entry *first;
1621 first = s->first_displayed_entry;
1624 err = scroll_down(&s->first_displayed_entry,
1625 view->nlines, &s->last_displayed_entry,
1626 &s->commits, &s->thread_args.log_complete,
1627 &s->thread_args.commits_needed,
1628 &s->thread_args.need_commits);
1629 if (first == s->first_displayed_entry &&
1630 s->selected < MIN(view->nlines - 2,
1631 s->commits.ncommits - 1)) {
1632 /* can't scroll further down */
1633 s->selected = MIN(view->nlines - 2,
1634 s->commits.ncommits - 1);
1640 if (s->selected > view->nlines - 2)
1641 s->selected = view->nlines - 2;
1642 if (s->selected > s->commits.ncommits - 1)
1643 s->selected = s->commits.ncommits - 1;
1647 if (s->selected_entry == NULL)
1649 if (view_is_parent_view(view))
1650 begin_x = view_split_begin_x(view->begin_x);
1651 err = open_diff_view_for_commit(&diff_view, begin_x,
1652 s->selected_entry->commit, s->selected_entry->id,
1653 view, s->refs, s->repo);
1656 if (view_is_parent_view(view)) {
1657 err = view_close_child(view);
1660 err = view_set_child(view, diff_view);
1662 view_close(diff_view);
1665 *focus_view = diff_view;
1666 view->child_focussed = 1;
1668 *new_view = diff_view;
1671 if (s->selected_entry == NULL)
1673 if (view_is_parent_view(view))
1674 begin_x = view_split_begin_x(view->begin_x);
1675 err = browse_commit(&tree_view, begin_x,
1676 s->selected_entry, s->refs, s->repo);
1679 if (view_is_parent_view(view)) {
1680 err = view_close_child(view);
1683 err = view_set_child(view, tree_view);
1685 view_close(tree_view);
1688 *focus_view = tree_view;
1689 view->child_focussed = 1;
1691 *new_view = tree_view;
1694 if (strcmp(s->in_repo_path, "/") == 0)
1696 parent_path = dirname(s->in_repo_path);
1697 if (parent_path && strcmp(parent_path, ".") != 0) {
1698 struct tog_view *lv;
1699 err = stop_log_thread(s);
1702 lv = view_open(view->nlines, view->ncols,
1703 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1705 return got_error_from_errno();
1706 err = open_log_view(lv, s->start_id, s->refs,
1707 s->repo, parent_path, 0);
1710 if (view_is_parent_view(view))
1713 view_set_child(view->parent, lv);
1726 static const struct got_error *
1727 apply_unveil(const char *repo_path, const char *worktree_path)
1729 const struct got_error *error;
1731 if (repo_path && unveil(repo_path, "r") != 0)
1732 return got_error_from_errno();
1734 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1735 return got_error_from_errno();
1737 if (unveil("/tmp", "rwc") != 0)
1738 return got_error_from_errno();
1740 error = got_privsep_unveil_exec_helpers();
1744 if (unveil(NULL, NULL) != 0)
1745 return got_error_from_errno();
1755 halfdelay(1); /* Do fast refresh while initial view is loading. */
1758 intrflush(stdscr, FALSE);
1759 keypad(stdscr, TRUE);
1761 signal(SIGWINCH, tog_sigwinch);
1764 static const struct got_error *
1765 cmd_log(int argc, char *argv[])
1767 const struct got_error *error;
1768 struct got_repository *repo = NULL;
1769 struct got_reflist_head refs;
1770 struct got_object_id *start_id = NULL;
1771 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1772 char *start_commit = NULL;
1774 struct tog_view *view;
1777 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1782 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1785 start_commit = optarg;
1788 repo_path = realpath(optarg, NULL);
1789 if (repo_path == NULL)
1790 err(1, "-r option");
1804 path = strdup(argv[0]);
1808 return got_error_from_errno();
1810 cwd = getcwd(NULL, 0);
1812 error = got_error_from_errno();
1815 if (repo_path == NULL) {
1816 struct got_worktree *worktree;
1817 error = got_worktree_open(&worktree, cwd);
1818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1822 strdup(got_worktree_get_repo_path(worktree));
1823 got_worktree_close(worktree);
1825 repo_path = strdup(cwd);
1826 if (repo_path == NULL) {
1827 error = got_error_from_errno();
1834 error = apply_unveil(repo_path, NULL);
1838 error = got_repo_open(&repo, repo_path);
1842 if (start_commit == NULL)
1843 error = get_head_commit_id(&start_id, repo);
1845 error = got_object_resolve_id_str(&start_id, repo,
1850 SIMPLEQ_INIT(&refs);
1851 error = got_ref_list(&refs, repo);
1855 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1857 error = got_error_from_errno();
1860 error = open_log_view(view, start_id, &refs, repo, path, 1);
1863 error = view_loop(view);
1870 got_repo_close(repo);
1878 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1884 parse_next_line(FILE *f, size_t *len)
1889 const char delim[3] = { '\0', '\0', '\0'};
1891 line = fparseln(f, &linelen, &lineno, delim, 0);
1897 static const struct got_error *
1898 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1899 int *last_displayed_line, int *eof, int max_lines,
1902 const struct got_error *err;
1903 int nlines = 0, nprinted = 0;
1910 werase(view->window);
1913 err = format_line(&wline, &width, header, view->ncols);
1918 if (view_needs_focus_indication(view))
1919 wstandout(view->window);
1920 waddwstr(view->window, wline);
1921 if (view_needs_focus_indication(view))
1922 wstandend(view->window);
1923 if (width < view->ncols)
1924 waddch(view->window, '\n');
1932 while (nprinted < max_lines) {
1933 line = parse_next_line(f, &len);
1938 if (++nlines < *first_displayed_line) {
1943 err = format_line(&wline, &width, line, view->ncols);
1948 waddwstr(view->window, wline);
1949 if (width < view->ncols)
1950 waddch(view->window, '\n');
1951 if (++nprinted == 1)
1952 *first_displayed_line = nlines;
1957 *last_displayed_line = nlines;
1965 get_datestr(time_t *time, char *datebuf)
1967 char *p, *s = ctime_r(time, datebuf);
1968 p = strchr(s, '\n');
1974 static const struct got_error *
1975 write_commit_info(struct got_object_id *commit_id,
1976 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
1978 const struct got_error *err = NULL;
1980 struct got_commit_object *commit;
1981 char *id_str = NULL;
1982 time_t committer_time;
1983 const char *author, *committer;
1984 char *refs_str = NULL;
1987 err = build_refs_str(&refs_str, refs, commit_id);
1992 err = got_object_open_as_commit(&commit, repo, commit_id);
1996 err = got_object_id_str(&id_str, commit_id);
1998 err = got_error_from_errno();
2002 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2003 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2004 err = got_error_from_errno();
2007 if (fprintf(outfile, "from: %s\n",
2008 got_object_commit_get_author(commit)) < 0) {
2009 err = got_error_from_errno();
2012 committer_time = got_object_commit_get_committer_time(commit);
2013 if (fprintf(outfile, "date: %s UTC\n",
2014 get_datestr(&committer_time, datebuf)) < 0) {
2015 err = got_error_from_errno();
2018 author = got_object_commit_get_author(commit);
2019 committer = got_object_commit_get_committer(commit);
2020 if (strcmp(author, committer) != 0 &&
2021 fprintf(outfile, "via: %s\n", committer) < 0) {
2022 err = got_error_from_errno();
2025 if (fprintf(outfile, "%s\n",
2026 got_object_commit_get_logmsg(commit)) < 0) {
2027 err = got_error_from_errno();
2033 got_object_commit_close(commit);
2037 static const struct got_error *
2038 create_diff(struct tog_diff_view_state *s)
2040 const struct got_error *err = NULL;
2046 err = got_error_from_errno();
2049 if (s->f && fclose(s->f) != 0) {
2050 err = got_error_from_errno();
2056 err = got_object_get_type(&obj_type, s->repo, s->id1);
2058 err = got_object_get_type(&obj_type, s->repo, s->id2);
2063 case GOT_OBJ_TYPE_BLOB:
2064 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2065 s->diff_context, s->repo, f);
2067 case GOT_OBJ_TYPE_TREE:
2068 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2069 s->diff_context, s->repo, f);
2071 case GOT_OBJ_TYPE_COMMIT: {
2072 const struct got_object_id_queue *parent_ids;
2073 struct got_object_qid *pid;
2074 struct got_commit_object *commit2;
2076 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2079 /* Show commit info if we're diffing to a parent/root commit. */
2081 write_commit_info(s->id2, s->refs, s->repo, f);
2083 parent_ids = got_object_commit_get_parent_ids(commit2);
2084 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2085 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2086 write_commit_info(s->id2, s->refs,
2092 got_object_commit_close(commit2);
2094 err = got_diff_objects_as_commits(s->id1, s->id2,
2095 s->diff_context, s->repo, f);
2099 err = got_error(GOT_ERR_OBJ_TYPE);
2103 if (f && fflush(f) != 0 && err == NULL)
2104 err = got_error_from_errno();
2108 static const struct got_error *
2109 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2110 struct got_object_id *id2, struct tog_view *log_view,
2111 struct got_reflist_head *refs, struct got_repository *repo)
2113 const struct got_error *err;
2115 if (id1 != NULL && id2 != NULL) {
2117 err = got_object_get_type(&type1, repo, id1);
2120 err = got_object_get_type(&type2, repo, id2);
2125 return got_error(GOT_ERR_OBJ_TYPE);
2129 view->state.diff.id1 = got_object_id_dup(id1);
2130 if (view->state.diff.id1 == NULL)
2131 return got_error_from_errno();
2133 view->state.diff.id1 = NULL;
2135 view->state.diff.id2 = got_object_id_dup(id2);
2136 if (view->state.diff.id2 == NULL) {
2137 free(view->state.diff.id1);
2138 view->state.diff.id1 = NULL;
2139 return got_error_from_errno();
2141 view->state.diff.f = NULL;
2142 view->state.diff.first_displayed_line = 1;
2143 view->state.diff.last_displayed_line = view->nlines;
2144 view->state.diff.diff_context = 3;
2145 view->state.diff.log_view = log_view;
2146 view->state.diff.repo = repo;
2147 view->state.diff.refs = refs;
2149 err = create_diff(&view->state.diff);
2151 free(view->state.diff.id1);
2152 view->state.diff.id1 = NULL;
2153 free(view->state.diff.id2);
2154 view->state.diff.id2 = NULL;
2158 view->show = show_diff_view;
2159 view->input = input_diff_view;
2160 view->close = close_diff_view;
2165 static const struct got_error *
2166 close_diff_view(struct tog_view *view)
2168 const struct got_error *err = NULL;
2170 free(view->state.diff.id1);
2171 view->state.diff.id1 = NULL;
2172 free(view->state.diff.id2);
2173 view->state.diff.id2 = NULL;
2174 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2175 err = got_error_from_errno();
2179 static const struct got_error *
2180 show_diff_view(struct tog_view *view)
2182 const struct got_error *err;
2183 struct tog_diff_view_state *s = &view->state.diff;
2184 char *id_str1 = NULL, *id_str2, *header;
2187 err = got_object_id_str(&id_str1, s->id1);
2191 err = got_object_id_str(&id_str2, s->id2);
2195 if (asprintf(&header, "diff %s %s",
2196 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2197 err = got_error_from_errno();
2205 return draw_file(view, s->f, &s->first_displayed_line,
2206 &s->last_displayed_line, &s->eof, view->nlines,
2210 static const struct got_error *
2211 set_selected_commit(struct tog_diff_view_state *s,
2212 struct commit_queue_entry *entry)
2214 const struct got_error *err;
2215 const struct got_object_id_queue *parent_ids;
2216 struct got_commit_object *selected_commit;
2217 struct got_object_qid *pid;
2220 s->id2 = got_object_id_dup(entry->id);
2222 return got_error_from_errno();
2224 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2227 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2229 pid = SIMPLEQ_FIRST(parent_ids);
2230 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2231 got_object_commit_close(selected_commit);
2235 static const struct got_error *
2236 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2237 struct tog_view **focus_view, struct tog_view *view, int ch)
2239 const struct got_error *err = NULL;
2240 struct tog_diff_view_state *s = &view->state.diff;
2241 struct tog_log_view_state *ls;
2242 struct commit_queue_entry *entry;
2248 if (s->first_displayed_line > 1)
2249 s->first_displayed_line--;
2253 while (i++ < view->nlines - 1 &&
2254 s->first_displayed_line > 1)
2255 s->first_displayed_line--;
2260 s->first_displayed_line++;
2265 while (!s->eof && i++ < view->nlines - 1) {
2267 line = parse_next_line(s->f, NULL);
2268 s->first_displayed_line++;
2274 if (s->diff_context > 0) {
2276 err = create_diff(s);
2280 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2282 err = create_diff(s);
2287 if (s->log_view == NULL)
2289 ls = &s->log_view->state.log;
2290 entry = TAILQ_PREV(ls->selected_entry,
2291 commit_queue_head, entry);
2295 err = input_log_view(NULL, NULL, NULL, s->log_view,
2300 err = set_selected_commit(s, entry);
2304 s->first_displayed_line = 1;
2305 s->last_displayed_line = view->nlines;
2307 err = create_diff(s);
2311 if (s->log_view == NULL)
2313 ls = &s->log_view->state.log;
2315 if (ls->thread_args.commits_needed == 0) {
2316 ls->thread_args.commits_needed++;
2318 /* Display "loading..." in log view. */
2319 show_log_view(s->log_view);
2323 err = trigger_log_thread(1 /* load_all */,
2324 &ls->thread_args.commits_needed,
2325 &ls->thread_args.log_complete,
2326 &ls->thread_args.need_commits);
2330 err = input_log_view(NULL, NULL, NULL, s->log_view,
2335 entry = TAILQ_NEXT(ls->selected_entry, entry);
2339 err = set_selected_commit(s, entry);
2343 s->first_displayed_line = 1;
2344 s->last_displayed_line = view->nlines;
2346 err = create_diff(s);
2355 static const struct got_error *
2356 cmd_diff(int argc, char *argv[])
2358 const struct got_error *error = NULL;
2359 struct got_repository *repo = NULL;
2360 struct got_reflist_head refs;
2361 struct got_object_id *id1 = NULL, *id2 = NULL;
2362 char *repo_path = NULL;
2363 char *id_str1 = NULL, *id_str2 = NULL;
2365 struct tog_view *view;
2368 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2373 while ((ch = getopt(argc, argv, "")) != -1) {
2385 usage_diff(); /* TODO show local worktree changes */
2386 } else if (argc == 2) {
2387 repo_path = getcwd(NULL, 0);
2388 if (repo_path == NULL)
2389 return got_error_from_errno();
2392 } else if (argc == 3) {
2393 repo_path = realpath(argv[0], NULL);
2394 if (repo_path == NULL)
2395 return got_error_from_errno();
2403 error = apply_unveil(repo_path, NULL);
2407 error = got_repo_open(&repo, repo_path);
2412 error = got_object_resolve_id_str(&id1, repo, id_str1);
2416 error = got_object_resolve_id_str(&id2, repo, id_str2);
2420 SIMPLEQ_INIT(&refs);
2421 error = got_ref_list(&refs, repo);
2425 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2427 error = got_error_from_errno();
2430 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2433 error = view_loop(view);
2435 got_repo_close(repo);
2443 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2448 struct tog_blame_line {
2450 struct got_object_id *id;
2453 static const struct got_error *
2454 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2455 const char *path, struct tog_blame_line *lines, int nlines,
2456 int blame_complete, int selected_line, int *first_displayed_line,
2457 int *last_displayed_line, int *eof, int max_lines)
2459 const struct got_error *err;
2460 int lineno = 0, nprinted = 0;
2465 struct tog_blame_line *blame_line;
2466 struct got_object_id *prev_id = NULL;
2469 err = got_object_id_str(&id_str, id);
2474 werase(view->window);
2476 if (asprintf(&line, "commit %s", id_str) == -1) {
2477 err = got_error_from_errno();
2482 err = format_line(&wline, &width, line, view->ncols);
2485 if (view_needs_focus_indication(view))
2486 wstandout(view->window);
2487 waddwstr(view->window, wline);
2488 if (view_needs_focus_indication(view))
2489 wstandend(view->window);
2492 if (width < view->ncols)
2493 waddch(view->window, '\n');
2495 if (asprintf(&line, "[%d/%d] %s%s",
2496 *first_displayed_line - 1 + selected_line, nlines,
2497 blame_complete ? "" : "annotating... ", path) == -1) {
2499 return got_error_from_errno();
2502 err = format_line(&wline, &width, line, view->ncols);
2507 waddwstr(view->window, wline);
2510 if (width < view->ncols)
2511 waddch(view->window, '\n');
2514 while (nprinted < max_lines - 2) {
2515 line = parse_next_line(f, &len);
2520 if (++lineno < *first_displayed_line) {
2525 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2526 err = format_line(&wline, &width, line, wlimit);
2532 if (view->focussed && nprinted == selected_line - 1)
2533 wstandout(view->window);
2535 blame_line = &lines[lineno - 1];
2536 if (blame_line->annotated && prev_id &&
2537 got_object_id_cmp(prev_id, blame_line->id) == 0)
2538 waddstr(view->window, " ");
2539 else if (blame_line->annotated) {
2541 err = got_object_id_str(&id_str, blame_line->id);
2547 wprintw(view->window, "%.8s ", id_str);
2549 prev_id = blame_line->id;
2551 waddstr(view->window, "........ ");
2555 waddwstr(view->window, wline);
2556 while (width < wlimit) {
2557 waddch(view->window, ' ');
2560 if (view->focussed && nprinted == selected_line - 1)
2561 wstandend(view->window);
2562 if (++nprinted == 1)
2563 *first_displayed_line = lineno;
2568 *last_displayed_line = lineno;
2575 static const struct got_error *
2576 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2578 const struct got_error *err = NULL;
2579 struct tog_blame_cb_args *a = arg;
2580 struct tog_blame_line *line;
2583 if (nlines != a->nlines ||
2584 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2585 return got_error(GOT_ERR_RANGE);
2587 errcode = pthread_mutex_lock(&tog_mutex);
2589 return got_error_set_errno(errcode);
2591 if (*a->quit) { /* user has quit the blame view */
2592 err = got_error(GOT_ERR_ITER_COMPLETED);
2597 goto done; /* no change in this commit */
2599 line = &a->lines[lineno - 1];
2600 if (line->annotated)
2603 line->id = got_object_id_dup(id);
2604 if (line->id == NULL) {
2605 err = got_error_from_errno();
2608 line->annotated = 1;
2610 errcode = pthread_mutex_unlock(&tog_mutex);
2612 err = got_error_set_errno(errcode);
2617 blame_thread(void *arg)
2619 const struct got_error *err;
2620 struct tog_blame_thread_args *ta = arg;
2621 struct tog_blame_cb_args *a = ta->cb_args;
2624 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2625 blame_cb, ta->cb_args);
2627 errcode = pthread_mutex_lock(&tog_mutex);
2629 return (void *)got_error_set_errno(errcode);
2631 got_repo_close(ta->repo);
2635 errcode = pthread_mutex_unlock(&tog_mutex);
2636 if (errcode && err == NULL)
2637 err = got_error_set_errno(errcode);
2642 static struct got_object_id *
2643 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2646 struct tog_blame_line *line;
2648 line = &lines[first_displayed_line - 1 + selected_line - 1];
2649 if (!line->annotated)
2655 static const struct got_error *
2656 stop_blame(struct tog_blame *blame)
2658 const struct got_error *err = NULL;
2661 if (blame->thread) {
2663 errcode = pthread_mutex_unlock(&tog_mutex);
2665 return got_error_set_errno(errcode);
2666 errcode = pthread_join(blame->thread, (void **)&err);
2668 return got_error_set_errno(errcode);
2669 errcode = pthread_mutex_lock(&tog_mutex);
2671 return got_error_set_errno(errcode);
2672 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2674 blame->thread = NULL;
2676 if (blame->thread_args.repo) {
2677 got_repo_close(blame->thread_args.repo);
2678 blame->thread_args.repo = NULL;
2681 if (fclose(blame->f) != 0 && err == NULL)
2682 err = got_error_from_errno();
2686 for (i = 0; i < blame->nlines; i++)
2687 free(blame->lines[i].id);
2689 blame->lines = NULL;
2691 free(blame->cb_args.commit_id);
2692 blame->cb_args.commit_id = NULL;
2697 static const struct got_error *
2698 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2699 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2700 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2701 struct got_repository *repo)
2703 const struct got_error *err = NULL;
2704 struct got_blob_object *blob = NULL;
2705 struct got_repository *thread_repo = NULL;
2706 struct got_object_id *obj_id = NULL;
2709 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2713 return got_error(GOT_ERR_NO_OBJ);
2715 err = got_object_get_type(&obj_type, repo, obj_id);
2719 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2720 err = got_error(GOT_ERR_OBJ_TYPE);
2724 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2727 blame->f = got_opentemp();
2728 if (blame->f == NULL) {
2729 err = got_error_from_errno();
2732 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2737 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2738 if (blame->lines == NULL) {
2739 err = got_error_from_errno();
2743 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2747 blame->cb_args.view = view;
2748 blame->cb_args.lines = blame->lines;
2749 blame->cb_args.nlines = blame->nlines;
2750 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2751 if (blame->cb_args.commit_id == NULL) {
2752 err = got_error_from_errno();
2755 blame->cb_args.quit = done;
2757 blame->thread_args.path = path;
2758 blame->thread_args.repo = thread_repo;
2759 blame->thread_args.cb_args = &blame->cb_args;
2760 blame->thread_args.complete = blame_complete;
2761 *blame_complete = 0;
2765 got_object_blob_close(blob);
2772 static const struct got_error *
2773 open_blame_view(struct tog_view *view, char *path,
2774 struct got_object_id *commit_id, struct got_reflist_head *refs,
2775 struct got_repository *repo)
2777 const struct got_error *err = NULL;
2778 struct tog_blame_view_state *s = &view->state.blame;
2780 SIMPLEQ_INIT(&s->blamed_commits);
2782 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2786 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2787 s->first_displayed_line = 1;
2788 s->last_displayed_line = view->nlines;
2789 s->selected_line = 1;
2790 s->blame_complete = 0;
2792 if (s->path == NULL)
2793 return got_error_from_errno();
2796 s->commit_id = commit_id;
2797 memset(&s->blame, 0, sizeof(s->blame));
2799 view->show = show_blame_view;
2800 view->input = input_blame_view;
2801 view->close = close_blame_view;
2803 return run_blame(&s->blame, view, &s->blame_complete,
2804 &s->first_displayed_line, &s->last_displayed_line,
2805 &s->selected_line, &s->done, &s->eof, s->path,
2806 s->blamed_commit->id, s->repo);
2809 static const struct got_error *
2810 close_blame_view(struct tog_view *view)
2812 const struct got_error *err = NULL;
2813 struct tog_blame_view_state *s = &view->state.blame;
2815 if (s->blame.thread)
2816 err = stop_blame(&s->blame);
2818 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2819 struct got_object_qid *blamed_commit;
2820 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2821 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2822 got_object_qid_free(blamed_commit);
2830 static const struct got_error *
2831 show_blame_view(struct tog_view *view)
2833 const struct got_error *err = NULL;
2834 struct tog_blame_view_state *s = &view->state.blame;
2837 if (s->blame.thread == NULL) {
2838 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2839 &s->blame.thread_args);
2841 return got_error_set_errno(errcode);
2844 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2845 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2846 s->selected_line, &s->first_displayed_line,
2847 &s->last_displayed_line, &s->eof, view->nlines);
2853 static const struct got_error *
2854 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2855 struct tog_view **focus_view, struct tog_view *view, int ch)
2857 const struct got_error *err = NULL, *thread_err = NULL;
2858 struct tog_view *diff_view;
2859 struct tog_blame_view_state *s = &view->state.blame;
2868 if (s->selected_line > 1)
2870 else if (s->selected_line == 1 &&
2871 s->first_displayed_line > 1)
2872 s->first_displayed_line--;
2875 if (s->first_displayed_line == 1) {
2876 s->selected_line = 1;
2879 if (s->first_displayed_line > view->nlines - 2)
2880 s->first_displayed_line -=
2883 s->first_displayed_line = 1;
2887 if (s->selected_line < view->nlines - 2 &&
2888 s->first_displayed_line +
2889 s->selected_line <= s->blame.nlines)
2891 else if (s->last_displayed_line <
2893 s->first_displayed_line++;
2897 struct got_object_id *id = NULL;
2898 id = get_selected_commit_id(s->blame.lines,
2899 s->first_displayed_line, s->selected_line);
2903 struct got_commit_object *commit;
2904 struct got_object_qid *pid;
2905 struct got_object_id *blob_id = NULL;
2907 err = got_object_open_as_commit(&commit,
2911 pid = SIMPLEQ_FIRST(
2912 got_object_commit_get_parent_ids(commit));
2914 got_object_commit_close(commit);
2917 /* Check if path history ends here. */
2918 err = got_object_id_by_path(&blob_id, s->repo,
2921 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2923 got_object_commit_close(commit);
2926 err = got_object_get_type(&obj_type, s->repo,
2929 /* Can't blame non-blob type objects. */
2930 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2931 got_object_commit_close(commit);
2934 err = got_object_qid_alloc(&s->blamed_commit,
2936 got_object_commit_close(commit);
2938 if (got_object_id_cmp(id,
2939 s->blamed_commit->id) == 0)
2941 err = got_object_qid_alloc(&s->blamed_commit,
2947 thread_err = stop_blame(&s->blame);
2951 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2952 s->blamed_commit, entry);
2953 err = run_blame(&s->blame, view, &s->blame_complete,
2954 &s->first_displayed_line, &s->last_displayed_line,
2955 &s->selected_line, &s->done, &s->eof,
2956 s->path, s->blamed_commit->id, s->repo);
2962 struct got_object_qid *first;
2963 first = SIMPLEQ_FIRST(&s->blamed_commits);
2964 if (!got_object_id_cmp(first->id, s->commit_id))
2967 thread_err = stop_blame(&s->blame);
2971 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2972 got_object_qid_free(s->blamed_commit);
2974 SIMPLEQ_FIRST(&s->blamed_commits);
2975 err = run_blame(&s->blame, view, &s->blame_complete,
2976 &s->first_displayed_line, &s->last_displayed_line,
2977 &s->selected_line, &s->done, &s->eof, s->path,
2978 s->blamed_commit->id, s->repo);
2985 struct got_object_id *id = NULL;
2986 struct got_object_qid *pid;
2987 struct got_commit_object *commit = NULL;
2988 id = get_selected_commit_id(s->blame.lines,
2989 s->first_displayed_line, s->selected_line);
2992 err = got_object_open_as_commit(&commit, s->repo, id);
2995 pid = SIMPLEQ_FIRST(
2996 got_object_commit_get_parent_ids(commit));
2997 if (view_is_parent_view(view))
2998 begin_x = view_split_begin_x(view->begin_x);
2999 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3000 if (diff_view == NULL) {
3001 got_object_commit_close(commit);
3002 err = got_error_from_errno();
3005 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3006 id, NULL, s->refs, s->repo);
3007 got_object_commit_close(commit);
3009 view_close(diff_view);
3012 if (view_is_parent_view(view)) {
3013 err = view_close_child(view);
3016 err = view_set_child(view, diff_view);
3018 view_close(diff_view);
3021 *focus_view = diff_view;
3022 view->child_focussed = 1;
3024 *new_view = diff_view;
3031 if (s->last_displayed_line >= s->blame.nlines &&
3032 s->selected_line < view->nlines - 2) {
3033 s->selected_line = MIN(s->blame.nlines,
3037 if (s->last_displayed_line + view->nlines - 2
3039 s->first_displayed_line +=
3042 s->first_displayed_line =
3047 if (s->selected_line > view->nlines - 2) {
3048 s->selected_line = MIN(s->blame.nlines,
3055 return thread_err ? thread_err : err;
3058 static const struct got_error *
3059 cmd_blame(int argc, char *argv[])
3061 const struct got_error *error;
3062 struct got_repository *repo = NULL;
3063 struct got_reflist_head refs;
3064 struct got_worktree *worktree = NULL;
3065 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3066 struct got_object_id *commit_id = NULL;
3067 char *commit_id_str = NULL;
3069 struct tog_view *view;
3072 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3077 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3080 commit_id_str = optarg;
3083 repo_path = realpath(optarg, NULL);
3084 if (repo_path == NULL)
3085 err(1, "-r option");
3101 cwd = getcwd(NULL, 0);
3103 error = got_error_from_errno();
3106 if (repo_path == NULL) {
3107 error = got_worktree_open(&worktree, cwd);
3108 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3114 strdup(got_worktree_get_repo_path(worktree));
3115 if (repo_path == NULL)
3116 error = got_error_from_errno();
3120 repo_path = strdup(cwd);
3121 if (repo_path == NULL) {
3122 error = got_error_from_errno();
3130 error = apply_unveil(repo_path, NULL);
3134 error = got_repo_open(&repo, repo_path);
3139 const char *prefix = got_worktree_get_path_prefix(worktree);
3140 char *p, *worktree_subdir = cwd +
3141 strlen(got_worktree_get_root_path(worktree));
3142 if (asprintf(&p, "%s%s%s%s%s",
3143 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3144 worktree_subdir, worktree_subdir[0] ? "/" : "",
3146 error = got_error_from_errno();
3149 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3152 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3157 if (commit_id_str == NULL) {
3158 struct got_reference *head_ref;
3159 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3162 error = got_ref_resolve(&commit_id, repo, head_ref);
3163 got_ref_close(head_ref);
3165 error = got_object_resolve_id_str(&commit_id, repo,
3171 SIMPLEQ_INIT(&refs);
3172 error = got_ref_list(&refs, repo);
3176 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3178 error = got_error_from_errno();
3181 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3184 error = view_loop(view);
3190 got_worktree_close(worktree);
3192 got_repo_close(repo);
3196 static const struct got_error *
3197 draw_tree_entries(struct tog_view *view,
3198 struct got_tree_entry **first_displayed_entry,
3199 struct got_tree_entry **last_displayed_entry,
3200 struct got_tree_entry **selected_entry, int *ndisplayed,
3201 const char *label, int show_ids, const char *parent_path,
3202 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3204 const struct got_error *err = NULL;
3205 struct got_tree_entry *te;
3211 werase(view->window);
3216 err = format_line(&wline, &width, label, view->ncols);
3219 if (view_needs_focus_indication(view))
3220 wstandout(view->window);
3221 waddwstr(view->window, wline);
3222 if (view_needs_focus_indication(view))
3223 wstandend(view->window);
3226 if (width < view->ncols)
3227 waddch(view->window, '\n');
3230 err = format_line(&wline, &width, parent_path, view->ncols);
3233 waddwstr(view->window, wline);
3236 if (width < view->ncols)
3237 waddch(view->window, '\n');
3240 waddch(view->window, '\n');
3244 te = SIMPLEQ_FIRST(&entries->head);
3245 if (*first_displayed_entry == NULL) {
3246 if (selected == 0) {
3248 wstandout(view->window);
3249 *selected_entry = NULL;
3251 waddstr(view->window, " ..\n"); /* parent directory */
3252 if (selected == 0 && view->focussed)
3253 wstandend(view->window);
3260 while (te != *first_displayed_entry)
3261 te = SIMPLEQ_NEXT(te, entry);
3265 char *line = NULL, *id_str = NULL;
3268 err = got_object_id_str(&id_str, te->id);
3270 return got_error_from_errno();
3272 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3273 te->name, S_ISDIR(te->mode) ? "/" :
3274 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3276 return got_error_from_errno();
3279 err = format_line(&wline, &width, line, view->ncols);
3284 if (n == selected) {
3286 wstandout(view->window);
3287 *selected_entry = te;
3289 waddwstr(view->window, wline);
3290 if (width < view->ncols)
3291 waddch(view->window, '\n');
3292 if (n == selected && view->focussed)
3293 wstandend(view->window);
3299 *last_displayed_entry = te;
3302 te = SIMPLEQ_NEXT(te, entry);
3309 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3310 const struct got_tree_entries *entries, int isroot)
3312 struct got_tree_entry *te, *prev;
3315 if (*first_displayed_entry == NULL)
3318 te = SIMPLEQ_FIRST(&entries->head);
3319 if (*first_displayed_entry == te) {
3321 *first_displayed_entry = NULL;
3325 /* XXX this is stupid... switch to TAILQ? */
3326 for (i = 0; i < maxscroll; i++) {
3327 while (te != *first_displayed_entry) {
3329 te = SIMPLEQ_NEXT(te, entry);
3331 *first_displayed_entry = prev;
3332 te = SIMPLEQ_FIRST(&entries->head);
3334 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3335 *first_displayed_entry = NULL;
3339 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3340 struct got_tree_entry *last_displayed_entry,
3341 const struct got_tree_entries *entries)
3343 struct got_tree_entry *next, *last;
3346 if (*first_displayed_entry)
3347 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3349 next = SIMPLEQ_FIRST(&entries->head);
3350 last = last_displayed_entry;
3351 while (next && last && n++ < maxscroll) {
3352 last = SIMPLEQ_NEXT(last, entry);
3354 *first_displayed_entry = next;
3355 next = SIMPLEQ_NEXT(next, entry);
3361 static const struct got_error *
3362 tree_entry_path(char **path, struct tog_parent_trees *parents,
3363 struct got_tree_entry *te)
3365 const struct got_error *err = NULL;
3366 struct tog_parent_tree *pt;
3367 size_t len = 2; /* for leading slash and NUL */
3369 TAILQ_FOREACH(pt, parents, entry)
3370 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3372 len += strlen(te->name);
3374 *path = calloc(1, len);
3376 return got_error_from_errno();
3379 pt = TAILQ_LAST(parents, tog_parent_trees);
3381 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3382 err = got_error(GOT_ERR_NO_SPACE);
3385 if (strlcat(*path, "/", len) >= len) {
3386 err = got_error(GOT_ERR_NO_SPACE);
3389 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3392 if (strlcat(*path, te->name, len) >= len) {
3393 err = got_error(GOT_ERR_NO_SPACE);
3405 static const struct got_error *
3406 blame_tree_entry(struct tog_view **new_view, int begin_x,
3407 struct got_tree_entry *te, struct tog_parent_trees *parents,
3408 struct got_object_id *commit_id, struct got_reflist_head *refs,
3409 struct got_repository *repo)
3411 const struct got_error *err = NULL;
3413 struct tog_view *blame_view;
3415 err = tree_entry_path(&path, parents, te);
3419 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3420 if (blame_view == NULL)
3421 return got_error_from_errno();
3423 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3425 view_close(blame_view);
3428 *new_view = blame_view;
3432 static const struct got_error *
3433 log_tree_entry(struct tog_view **new_view, int begin_x,
3434 struct got_tree_entry *te, struct tog_parent_trees *parents,
3435 struct got_object_id *commit_id, struct got_reflist_head *refs,
3436 struct got_repository *repo)
3438 struct tog_view *log_view;
3439 const struct got_error *err = NULL;
3442 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3443 if (log_view == NULL)
3444 return got_error_from_errno();
3446 err = tree_entry_path(&path, parents, te);
3450 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3452 view_close(log_view);
3454 *new_view = log_view;
3459 static const struct got_error *
3460 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3461 struct got_object_id *commit_id, struct got_reflist_head *refs,
3462 struct got_repository *repo)
3464 const struct got_error *err = NULL;
3465 char *commit_id_str = NULL;
3466 struct tog_tree_view_state *s = &view->state.tree;
3468 TAILQ_INIT(&s->parents);
3470 err = got_object_id_str(&commit_id_str, commit_id);
3474 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3475 err = got_error_from_errno();
3479 s->root = s->tree = root;
3480 s->entries = got_object_tree_get_entries(root);
3481 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3482 s->commit_id = got_object_id_dup(commit_id);
3483 if (s->commit_id == NULL) {
3484 err = got_error_from_errno();
3490 view->show = show_tree_view;
3491 view->input = input_tree_view;
3492 view->close = close_tree_view;
3494 free(commit_id_str);
3496 free(s->tree_label);
3497 s->tree_label = NULL;
3502 static const struct got_error *
3503 close_tree_view(struct tog_view *view)
3505 struct tog_tree_view_state *s = &view->state.tree;
3507 free(s->tree_label);
3508 s->tree_label = NULL;
3510 s->commit_id = NULL;
3511 while (!TAILQ_EMPTY(&s->parents)) {
3512 struct tog_parent_tree *parent;
3513 parent = TAILQ_FIRST(&s->parents);
3514 TAILQ_REMOVE(&s->parents, parent, entry);
3518 if (s->tree != s->root)
3519 got_object_tree_close(s->tree);
3520 got_object_tree_close(s->root);
3525 static const struct got_error *
3526 show_tree_view(struct tog_view *view)
3528 const struct got_error *err = NULL;
3529 struct tog_tree_view_state *s = &view->state.tree;
3532 err = tree_entry_path(&parent_path, &s->parents, NULL);
3536 err = draw_tree_entries(view, &s->first_displayed_entry,
3537 &s->last_displayed_entry, &s->selected_entry,
3538 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3539 s->entries, s->selected, view->nlines, s->tree == s->root);
3546 static const struct got_error *
3547 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3548 struct tog_view **focus_view, struct tog_view *view, int ch)
3550 const struct got_error *err = NULL;
3551 struct tog_tree_view_state *s = &view->state.tree;
3552 struct tog_view *log_view;
3553 int begin_x = 0, nscrolled;
3557 s->show_ids = !s->show_ids;
3560 if (!s->selected_entry)
3562 if (view_is_parent_view(view))
3563 begin_x = view_split_begin_x(view->begin_x);
3564 err = log_tree_entry(&log_view, begin_x,
3565 s->selected_entry, &s->parents,
3566 s->commit_id, s->refs, s->repo);
3567 if (view_is_parent_view(view)) {
3568 err = view_close_child(view);
3571 err = view_set_child(view, log_view);
3573 view_close(log_view);
3576 *focus_view = log_view;
3577 view->child_focussed = 1;
3579 *new_view = log_view;
3583 if (s->selected > 0) {
3585 if (s->selected == 0)
3588 if (s->selected > 0)
3590 tree_scroll_up(&s->first_displayed_entry, 1,
3591 s->entries, s->tree == s->root);
3594 tree_scroll_up(&s->first_displayed_entry,
3595 MAX(0, view->nlines - 4 - s->selected), s->entries,
3596 s->tree == s->root);
3598 if (SIMPLEQ_FIRST(&s->entries->head) ==
3599 s->first_displayed_entry && s->tree != s->root)
3600 s->first_displayed_entry = NULL;
3604 if (s->selected < s->ndisplayed - 1) {
3608 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3610 /* can't scroll any further */
3613 tree_scroll_down(&s->first_displayed_entry, 1,
3614 s->last_displayed_entry, s->entries);
3617 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3619 /* can't scroll any further; move cursor down */
3620 if (s->selected < s->ndisplayed - 1)
3621 s->selected = s->ndisplayed - 1;
3624 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3625 view->nlines, s->last_displayed_entry, s->entries);
3626 if (nscrolled < view->nlines) {
3628 struct got_tree_entry *te;
3629 te = s->first_displayed_entry;
3632 te = SIMPLEQ_NEXT(te, entry);
3634 s->selected = ndisplayed - 1;
3639 if (s->selected_entry == NULL) {
3640 struct tog_parent_tree *parent;
3642 /* user selected '..' */
3643 if (s->tree == s->root)
3645 parent = TAILQ_FIRST(&s->parents);
3646 TAILQ_REMOVE(&s->parents, parent,
3648 got_object_tree_close(s->tree);
3649 s->tree = parent->tree;
3651 got_object_tree_get_entries(s->tree);
3652 s->first_displayed_entry =
3653 parent->first_displayed_entry;
3655 parent->selected_entry;
3656 s->selected = parent->selected;
3658 } else if (S_ISDIR(s->selected_entry->mode)) {
3659 struct tog_parent_tree *parent;
3660 struct got_tree_object *child;
3661 err = got_object_open_as_tree(&child,
3662 s->repo, s->selected_entry->id);
3665 parent = calloc(1, sizeof(*parent));
3666 if (parent == NULL) {
3667 err = got_error_from_errno();
3670 parent->tree = s->tree;
3671 parent->first_displayed_entry =
3672 s->first_displayed_entry;
3673 parent->selected_entry = s->selected_entry;
3674 parent->selected = s->selected;
3675 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3678 got_object_tree_get_entries(s->tree);
3680 s->first_displayed_entry = NULL;
3681 } else if (S_ISREG(s->selected_entry->mode)) {
3682 struct tog_view *blame_view;
3683 int begin_x = view_is_parent_view(view) ?
3684 view_split_begin_x(view->begin_x) : 0;
3686 err = blame_tree_entry(&blame_view, begin_x,
3687 s->selected_entry, &s->parents,
3688 s->commit_id, s->refs, s->repo);
3691 if (view_is_parent_view(view)) {
3692 err = view_close_child(view);
3695 err = view_set_child(view, blame_view);
3697 view_close(blame_view);
3700 *focus_view = blame_view;
3701 view->child_focussed = 1;
3703 *new_view = blame_view;
3707 if (s->selected > view->nlines)
3708 s->selected = s->ndisplayed - 1;
3721 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3726 static const struct got_error *
3727 cmd_tree(int argc, char *argv[])
3729 const struct got_error *error;
3730 struct got_repository *repo = NULL;
3731 struct got_reflist_head refs;
3732 char *repo_path = NULL;
3733 struct got_object_id *commit_id = NULL;
3734 char *commit_id_arg = NULL;
3735 struct got_commit_object *commit = NULL;
3736 struct got_tree_object *tree = NULL;
3738 struct tog_view *view;
3741 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3746 while ((ch = getopt(argc, argv, "c:")) != -1) {
3749 commit_id_arg = optarg;
3761 struct got_worktree *worktree;
3762 char *cwd = getcwd(NULL, 0);
3764 return got_error_from_errno();
3765 error = got_worktree_open(&worktree, cwd);
3766 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3771 strdup(got_worktree_get_repo_path(worktree));
3772 got_worktree_close(worktree);
3775 if (repo_path == NULL) {
3776 error = got_error_from_errno();
3779 } else if (argc == 1) {
3780 repo_path = realpath(argv[0], NULL);
3781 if (repo_path == NULL)
3782 return got_error_from_errno();
3788 error = apply_unveil(repo_path, NULL);
3792 error = got_repo_open(&repo, repo_path);
3796 if (commit_id_arg == NULL)
3797 error = get_head_commit_id(&commit_id, repo);
3799 error = got_object_resolve_id_str(&commit_id, repo,
3804 error = got_object_open_as_commit(&commit, repo, commit_id);
3808 error = got_object_open_as_tree(&tree, repo,
3809 got_object_commit_get_tree_id(commit));
3813 SIMPLEQ_INIT(&refs);
3814 error = got_ref_list(&refs, repo);
3818 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3820 error = got_error_from_errno();
3823 error = open_tree_view(view, tree, commit_id, &refs, repo);
3826 error = view_loop(view);
3831 got_object_commit_close(commit);
3833 got_object_tree_close(tree);
3835 got_repo_close(repo);
3844 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3845 "Available commands:\n", getprogname());
3846 for (i = 0; i < nitems(tog_commands); i++) {
3847 struct tog_cmd *cmd = &tog_commands[i];
3848 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3854 make_argv(const char *arg0, const char *arg1)
3857 int argc = (arg1 == NULL ? 1 : 2);
3859 argv = calloc(argc, sizeof(char *));
3862 argv[0] = strdup(arg0);
3863 if (argv[0] == NULL)
3866 argv[1] = strdup(arg1);
3867 if (argv[1] == NULL)
3875 main(int argc, char *argv[])
3877 const struct got_error *error = NULL;
3878 struct tog_cmd *cmd = NULL;
3880 char **cmd_argv = NULL;
3882 setlocale(LC_CTYPE, "");
3884 while ((ch = getopt(argc, argv, "h")) != -1) {
3903 /* Build an argument vector which runs a default command. */
3904 cmd = &tog_commands[0];
3905 cmd_argv = make_argv(cmd->name, NULL);
3910 /* Did the user specific a command? */
3911 for (i = 0; i < nitems(tog_commands); i++) {
3912 if (strncmp(tog_commands[i].name, argv[0],
3913 strlen(argv[0])) == 0) {
3914 cmd = &tog_commands[i];
3916 tog_commands[i].cmd_usage();
3921 /* Did the user specify a repository? */
3922 char *repo_path = realpath(argv[0], NULL);
3924 struct got_repository *repo;
3925 error = got_repo_open(&repo, repo_path);
3927 got_repo_close(repo);
3929 error = got_error_from_errno();
3932 fprintf(stderr, "%s: '%s' is not a "
3933 "known command\n", getprogname(),
3937 fprintf(stderr, "%s: '%s' is neither a known "
3938 "command nor a path to a repository\n",
3939 getprogname(), argv[0]);
3943 cmd = &tog_commands[0];
3944 cmd_argv = make_argv(cmd->name, repo_path);
3950 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3957 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);