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, "got/", 4) == 0)
907 if (strncmp(name, "heads/", 6) == 0)
909 if (strncmp(name, "remotes/", 8) == 0)
912 if (asprintf(refs_str, "%s%s%s", s ? s : "",
913 s ? ", " : "", name) == -1) {
914 err = got_error_from_errno();
925 static const struct got_error *
926 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
928 char *smallerthan, *at;
930 smallerthan = strchr(author, '<');
931 if (smallerthan && smallerthan[1] != '\0')
932 author = smallerthan + 1;
933 at = strchr(author, '@');
936 return format_line(wauthor, author_width, author, limit);
939 static const struct got_error *
940 draw_commit(struct tog_view *view, struct got_commit_object *commit,
941 struct got_object_id *id, struct got_reflist_head *refs,
942 int author_display_cols)
944 const struct got_error *err = NULL;
945 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
946 char *logmsg0 = NULL, *logmsg = NULL;
948 wchar_t *wlogmsg = NULL, *wauthor = NULL;
949 int author_width, logmsg_width;
950 char *newline, *line = NULL;
952 static const size_t date_display_cols = 9;
953 const int avail = view->ncols;
955 time_t committer_time;
957 committer_time = got_object_commit_get_committer_time(commit);
958 if (localtime_r(&committer_time, &tm) == NULL)
959 return got_error_from_errno();
960 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
962 return got_error(GOT_ERR_NO_SPACE);
964 if (avail < date_display_cols)
965 limit = MIN(sizeof(datebuf) - 1, avail);
967 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
968 waddnstr(view->window, datebuf, limit);
973 author = strdup(got_object_commit_get_author(commit));
974 if (author == NULL) {
975 err = got_error_from_errno();
978 err = format_author(&wauthor, &author_width, author, avail - col);
981 waddwstr(view->window, wauthor);
983 while (col <= avail && author_width < author_display_cols + 2) {
984 waddch(view->window, ' ');
991 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
992 if (logmsg0 == NULL) {
993 err = got_error_from_errno();
997 while (*logmsg == '\n')
999 newline = strchr(logmsg, '\n');
1002 limit = avail - col;
1003 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1006 waddwstr(view->window, wlogmsg);
1007 col += logmsg_width;
1008 while (col <= avail) {
1009 waddch(view->window, ' ');
1021 static struct commit_queue_entry *
1022 alloc_commit_queue_entry(struct got_commit_object *commit,
1023 struct got_object_id *id)
1025 struct commit_queue_entry *entry;
1027 entry = calloc(1, sizeof(*entry));
1032 entry->commit = commit;
1037 pop_commit(struct commit_queue *commits)
1039 struct commit_queue_entry *entry;
1041 entry = TAILQ_FIRST(&commits->head);
1042 TAILQ_REMOVE(&commits->head, entry, entry);
1043 got_object_commit_close(entry->commit);
1044 commits->ncommits--;
1045 /* Don't free entry->id! It is owned by the commit graph. */
1050 free_commits(struct commit_queue *commits)
1052 while (!TAILQ_EMPTY(&commits->head))
1053 pop_commit(commits);
1056 static const struct got_error *
1057 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1058 int minqueue, struct got_repository *repo, const char *path)
1060 const struct got_error *err = NULL;
1064 * We keep all commits open throughout the lifetime of the log
1065 * view in order to avoid having to re-fetch commits from disk
1066 * while updating the display.
1068 while (nqueued < minqueue) {
1069 struct got_object_id *id;
1070 struct got_commit_object *commit;
1071 struct commit_queue_entry *entry;
1074 err = got_commit_graph_iter_next(&id, graph);
1076 if (err->code != GOT_ERR_ITER_NEED_MORE)
1078 err = got_commit_graph_fetch_commits(graph,
1088 err = got_object_open_as_commit(&commit, repo, id);
1091 entry = alloc_commit_queue_entry(commit, id);
1092 if (entry == NULL) {
1093 err = got_error_from_errno();
1097 errcode = pthread_mutex_lock(&tog_mutex);
1099 err = got_error_set_errno(errcode);
1103 entry->idx = commits->ncommits;
1104 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1106 commits->ncommits++;
1108 errcode = pthread_mutex_unlock(&tog_mutex);
1109 if (errcode && err == NULL)
1110 err = got_error_set_errno(errcode);
1116 static const struct got_error *
1117 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1119 const struct got_error *err = NULL;
1120 struct got_reference *head_ref;
1124 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1128 err = got_ref_resolve(head_id, repo, head_ref);
1129 got_ref_close(head_ref);
1138 static const struct got_error *
1139 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1140 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1141 struct commit_queue *commits, int selected_idx, int limit,
1142 struct got_reflist_head *refs, const char *path, int commits_needed)
1144 const struct got_error *err = NULL;
1145 struct commit_queue_entry *entry;
1146 int ncommits, width;
1147 int author_cols = 10;
1148 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1149 char *refs_str = NULL;
1155 if (ncommits == selected_idx) {
1159 entry = TAILQ_NEXT(entry, entry);
1164 err = got_object_id_str(&id_str, (*selected)->id);
1168 err = build_refs_str(&refs_str, refs, (*selected)->id);
1174 if (commits_needed == 0)
1175 halfdelay(10); /* disable fast refresh */
1177 if (asprintf(&ncommits_str, " [%d/%d] %s",
1178 entry ? entry->idx + 1 : 0, commits->ncommits,
1179 commits_needed > 0 ? "loading... " :
1180 (refs_str ? refs_str : "")) == -1) {
1181 err = got_error_from_errno();
1185 if (path && strcmp(path, "/") != 0) {
1186 if (asprintf(&header, "commit %s %s%s",
1187 id_str ? id_str : "........................................",
1188 path, ncommits_str) == -1) {
1189 err = got_error_from_errno();
1193 } else if (asprintf(&header, "commit %s%s",
1194 id_str ? id_str : "........................................",
1195 ncommits_str) == -1) {
1196 err = got_error_from_errno();
1200 err = format_line(&wline, &width, header, view->ncols);
1204 werase(view->window);
1206 if (view_needs_focus_indication(view))
1207 wstandout(view->window);
1208 waddwstr(view->window, wline);
1209 while (width < view->ncols) {
1210 waddch(view->window, ' ');
1213 if (view_needs_focus_indication(view))
1214 wstandend(view->window);
1219 /* Grow author column size if necessary. */
1226 if (ncommits >= limit - 1)
1228 author = strdup(got_object_commit_get_author(entry->commit));
1229 if (author == NULL) {
1230 err = got_error_from_errno();
1233 err = format_author(&wauthor, &width, author, COLS);
1234 if (author_cols < width)
1235 author_cols = width;
1238 entry = TAILQ_NEXT(entry, entry);
1245 if (ncommits >= limit - 1)
1247 if (ncommits == selected_idx)
1248 wstandout(view->window);
1249 err = draw_commit(view, entry->commit, entry->id, refs,
1251 if (ncommits == selected_idx)
1252 wstandend(view->window);
1257 entry = TAILQ_NEXT(entry, entry);
1270 scroll_up(struct tog_view *view,
1271 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1272 struct commit_queue *commits)
1274 struct commit_queue_entry *entry;
1277 entry = TAILQ_FIRST(&commits->head);
1278 if (*first_displayed_entry == entry) {
1283 entry = *first_displayed_entry;
1284 while (entry && nscrolled < maxscroll) {
1285 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1287 *first_displayed_entry = entry;
1293 static const struct got_error *
1294 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1295 pthread_cond_t *need_commits)
1300 halfdelay(1); /* fast refresh while loading commits */
1302 while (*commits_needed > 0) {
1306 /* Wake the log thread. */
1307 errcode = pthread_cond_signal(need_commits);
1309 return got_error_set_errno(errcode);
1310 errcode = pthread_mutex_unlock(&tog_mutex);
1312 return got_error_set_errno(errcode);
1314 errcode = pthread_mutex_lock(&tog_mutex);
1316 return got_error_set_errno(errcode);
1318 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1320 * Thread is not done yet; lose a key press
1321 * and let the user retry... this way the GUI
1322 * remains interactive while logging deep paths
1323 * with few commits in history.
1332 static const struct got_error *
1333 scroll_down(struct tog_view *view,
1334 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1335 struct commit_queue_entry **last_displayed_entry,
1336 struct commit_queue *commits, int *log_complete, int *commits_needed,
1337 pthread_cond_t *need_commits)
1339 const struct got_error *err = NULL;
1340 struct commit_queue_entry *pentry;
1343 if (*last_displayed_entry == NULL)
1346 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1347 if (pentry == NULL && !*log_complete) {
1349 * Ask the log thread for required amount of commits
1350 * plus some amount of pre-fetching.
1352 (*commits_needed) += maxscroll + 20;
1353 err = trigger_log_thread(0, commits_needed, log_complete,
1360 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1361 if (pentry == NULL) {
1367 *last_displayed_entry = pentry;
1369 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1372 *first_displayed_entry = pentry;
1373 } while (++nscrolled < maxscroll);
1378 static const struct got_error *
1379 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1380 struct got_commit_object *commit, struct got_object_id *commit_id,
1381 struct tog_view *log_view, struct got_reflist_head *refs,
1382 struct got_repository *repo)
1384 const struct got_error *err;
1385 struct got_object_qid *parent_id;
1386 struct tog_view *diff_view;
1388 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1389 if (diff_view == NULL)
1390 return got_error_from_errno();
1392 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1393 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1394 commit_id, log_view, refs, repo);
1396 *new_view = diff_view;
1400 static const struct got_error *
1401 browse_commit(struct tog_view **new_view, int begin_x,
1402 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1403 struct got_repository *repo)
1405 const struct got_error *err = NULL;
1406 struct got_tree_object *tree;
1407 struct tog_view *tree_view;
1409 err = got_object_open_as_tree(&tree, repo,
1410 got_object_commit_get_tree_id(entry->commit));
1414 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1415 if (tree_view == NULL)
1416 return got_error_from_errno();
1418 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1420 got_object_tree_close(tree);
1422 *new_view = tree_view;
1427 log_thread(void *arg)
1429 const struct got_error *err = NULL;
1431 struct tog_log_thread_args *a = arg;
1434 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1438 while (!done && !err) {
1439 err = queue_commits(a->graph, a->commits, 1, a->repo,
1442 if (err->code != GOT_ERR_ITER_COMPLETED)
1446 } else if (a->commits_needed > 0)
1447 a->commits_needed--;
1449 errcode = pthread_mutex_lock(&tog_mutex);
1451 return (void *)got_error_set_errno(errcode);
1454 a->log_complete = 1;
1455 else if (*a->quit) {
1457 a->log_complete = 1;
1458 } else if (*a->first_displayed_entry == NULL) {
1459 *a->first_displayed_entry =
1460 TAILQ_FIRST(&a->commits->head);
1461 *a->selected_entry = *a->first_displayed_entry;
1465 a->commits_needed = 0;
1466 else if (a->commits_needed == 0) {
1467 errcode = pthread_cond_wait(&a->need_commits,
1470 err = got_error_set_errno(errcode);
1473 errcode = pthread_mutex_unlock(&tog_mutex);
1474 if (errcode && err == NULL)
1475 err = got_error_set_errno(errcode);
1480 static const struct got_error *
1481 stop_log_thread(struct tog_log_view_state *s)
1483 const struct got_error *err = NULL;
1488 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1490 return got_error_set_errno(errcode);
1491 errcode = pthread_mutex_unlock(&tog_mutex);
1493 return got_error_set_errno(errcode);
1494 errcode = pthread_join(s->thread, (void **)&err);
1496 return got_error_set_errno(errcode);
1497 errcode = pthread_mutex_lock(&tog_mutex);
1499 return got_error_set_errno(errcode);
1503 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1504 if (errcode && err == NULL)
1505 err = got_error_set_errno(errcode);
1507 if (s->thread_args.repo) {
1508 got_repo_close(s->thread_args.repo);
1509 s->thread_args.repo = NULL;
1512 if (s->thread_args.graph) {
1513 got_commit_graph_close(s->thread_args.graph);
1514 s->thread_args.graph = NULL;
1520 static const struct got_error *
1521 close_log_view(struct tog_view *view)
1523 const struct got_error *err = NULL;
1524 struct tog_log_view_state *s = &view->state.log;
1526 err = stop_log_thread(s);
1527 free_commits(&s->commits);
1528 free(s->in_repo_path);
1529 s->in_repo_path = NULL;
1535 static const struct got_error *
1536 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1537 struct got_reflist_head *refs, struct got_repository *repo,
1538 const char *path, int check_disk)
1540 const struct got_error *err = NULL;
1541 struct tog_log_view_state *s = &view->state.log;
1542 struct got_repository *thread_repo = NULL;
1543 struct got_commit_graph *thread_graph = NULL;
1546 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1550 /* The commit queue only contains commits being displayed. */
1551 TAILQ_INIT(&s->commits.head);
1552 s->commits.ncommits = 0;
1556 s->start_id = got_object_id_dup(start_id);
1557 if (s->start_id == NULL) {
1558 err = got_error_from_errno();
1562 view->show = show_log_view;
1563 view->input = input_log_view;
1564 view->close = close_log_view;
1566 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1569 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1574 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1576 err = got_error_set_errno(errcode);
1580 s->thread_args.commits_needed = view->nlines;
1581 s->thread_args.graph = thread_graph;
1582 s->thread_args.commits = &s->commits;
1583 s->thread_args.in_repo_path = s->in_repo_path;
1584 s->thread_args.start_id = s->start_id;
1585 s->thread_args.repo = thread_repo;
1586 s->thread_args.log_complete = 0;
1587 s->thread_args.quit = &s->quit;
1588 s->thread_args.view = view;
1589 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1590 s->thread_args.selected_entry = &s->selected_entry;
1593 close_log_view(view);
1597 static const struct got_error *
1598 show_log_view(struct tog_view *view)
1600 struct tog_log_view_state *s = &view->state.log;
1602 if (s->thread == NULL) {
1603 int errcode = pthread_create(&s->thread, NULL, log_thread,
1606 return got_error_set_errno(errcode);
1609 return draw_commits(view, &s->last_displayed_entry,
1610 &s->selected_entry, s->first_displayed_entry,
1611 &s->commits, s->selected, view->nlines, s->refs,
1612 s->in_repo_path, s->thread_args.commits_needed);
1615 static const struct got_error *
1616 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1617 struct tog_view **focus_view, struct tog_view *view, int ch)
1619 const struct got_error *err = NULL;
1620 struct tog_log_view_state *s = &view->state.log;
1622 struct tog_view *diff_view = NULL, *tree_view = NULL;
1633 if (s->first_displayed_entry == NULL)
1635 if (s->selected > 0)
1637 if (s->selected > 0)
1639 scroll_up(view, &s->first_displayed_entry, 1,
1643 if (s->first_displayed_entry == NULL)
1645 if (TAILQ_FIRST(&s->commits.head) ==
1646 s->first_displayed_entry) {
1647 if (s->selected == 0) {
1654 scroll_up(view, &s->first_displayed_entry,
1655 view->nlines, &s->commits);
1661 if (s->first_displayed_entry == NULL)
1663 if (s->selected < MIN(view->nlines - 2,
1664 s->commits.ncommits - 1)) {
1668 err = scroll_down(view, &s->first_displayed_entry, 1,
1669 &s->last_displayed_entry, &s->commits,
1670 &s->thread_args.log_complete,
1671 &s->thread_args.commits_needed,
1672 &s->thread_args.need_commits);
1675 struct commit_queue_entry *first;
1676 first = s->first_displayed_entry;
1679 err = scroll_down(view, &s->first_displayed_entry,
1680 view->nlines, &s->last_displayed_entry,
1681 &s->commits, &s->thread_args.log_complete,
1682 &s->thread_args.commits_needed,
1683 &s->thread_args.need_commits);
1684 if (first == s->first_displayed_entry &&
1685 s->selected < MIN(view->nlines - 2,
1686 s->commits.ncommits - 1)) {
1687 /* can't scroll further down */
1688 s->selected = MIN(view->nlines - 2,
1689 s->commits.ncommits - 1);
1695 if (s->selected > view->nlines - 2)
1696 s->selected = view->nlines - 2;
1697 if (s->selected > s->commits.ncommits - 1)
1698 s->selected = s->commits.ncommits - 1;
1702 if (s->selected_entry == NULL)
1704 if (view_is_parent_view(view))
1705 begin_x = view_split_begin_x(view->begin_x);
1706 err = open_diff_view_for_commit(&diff_view, begin_x,
1707 s->selected_entry->commit, s->selected_entry->id,
1708 view, s->refs, s->repo);
1711 if (view_is_parent_view(view)) {
1712 err = view_close_child(view);
1715 err = view_set_child(view, diff_view);
1717 view_close(diff_view);
1720 *focus_view = diff_view;
1721 view->child_focussed = 1;
1723 *new_view = diff_view;
1726 if (s->selected_entry == NULL)
1728 if (view_is_parent_view(view))
1729 begin_x = view_split_begin_x(view->begin_x);
1730 err = browse_commit(&tree_view, begin_x,
1731 s->selected_entry, s->refs, s->repo);
1734 if (view_is_parent_view(view)) {
1735 err = view_close_child(view);
1738 err = view_set_child(view, tree_view);
1740 view_close(tree_view);
1743 *focus_view = tree_view;
1744 view->child_focussed = 1;
1746 *new_view = tree_view;
1749 if (strcmp(s->in_repo_path, "/") == 0)
1751 parent_path = dirname(s->in_repo_path);
1752 if (parent_path && strcmp(parent_path, ".") != 0) {
1753 struct tog_view *lv;
1754 err = stop_log_thread(s);
1757 lv = view_open(view->nlines, view->ncols,
1758 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1760 return got_error_from_errno();
1761 err = open_log_view(lv, s->start_id, s->refs,
1762 s->repo, parent_path, 0);
1765 if (view_is_parent_view(view))
1768 view_set_child(view->parent, lv);
1781 static const struct got_error *
1782 apply_unveil(const char *repo_path, const char *worktree_path)
1784 const struct got_error *error;
1786 if (repo_path && unveil(repo_path, "r") != 0)
1787 return got_error_from_errno();
1789 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1790 return got_error_from_errno();
1792 if (unveil("/tmp", "rwc") != 0)
1793 return got_error_from_errno();
1795 error = got_privsep_unveil_exec_helpers();
1799 if (unveil(NULL, NULL) != 0)
1800 return got_error_from_errno();
1810 halfdelay(1); /* Do fast refresh while initial view is loading. */
1813 intrflush(stdscr, FALSE);
1814 keypad(stdscr, TRUE);
1816 signal(SIGWINCH, tog_sigwinch);
1819 static const struct got_error *
1820 cmd_log(int argc, char *argv[])
1822 const struct got_error *error;
1823 struct got_repository *repo = NULL;
1824 struct got_worktree *worktree = NULL;
1825 struct got_reflist_head refs;
1826 struct got_object_id *start_id = NULL;
1827 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1828 char *start_commit = NULL;
1830 struct tog_view *view;
1832 SIMPLEQ_INIT(&refs);
1835 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1840 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1843 start_commit = optarg;
1846 repo_path = realpath(optarg, NULL);
1847 if (repo_path == NULL)
1848 err(1, "-r option");
1859 cwd = getcwd(NULL, 0);
1861 error = got_error_from_errno();
1864 error = got_worktree_open(&worktree, cwd);
1865 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1872 error = got_error_from_errno();
1875 } else if (argc == 1) {
1877 error = got_worktree_resolve_path(&path, worktree,
1882 path = strdup(argv[0]);
1884 error = got_error_from_errno();
1891 repo_path = worktree ?
1892 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1893 if (repo_path == NULL) {
1894 error = got_error_from_errno();
1900 error = got_repo_open(&repo, repo_path);
1904 error = apply_unveil(got_repo_get_path(repo),
1905 worktree ? got_worktree_get_root_path(worktree) : NULL);
1909 if (start_commit == NULL)
1910 error = get_head_commit_id(&start_id, repo);
1912 error = got_object_resolve_id_str(&start_id, repo,
1917 error = got_ref_list(&refs, repo);
1921 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1923 error = got_error_from_errno();
1926 error = open_log_view(view, start_id, &refs, repo, path, 1);
1929 error = view_loop(view);
1936 got_repo_close(repo);
1938 got_worktree_close(worktree);
1939 got_ref_list_free(&refs);
1947 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1953 parse_next_line(FILE *f, size_t *len)
1958 const char delim[3] = { '\0', '\0', '\0'};
1960 line = fparseln(f, &linelen, &lineno, delim, 0);
1966 static const struct got_error *
1967 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1968 int *last_displayed_line, int *eof, int max_lines,
1971 const struct got_error *err;
1972 int nlines = 0, nprinted = 0;
1979 werase(view->window);
1982 err = format_line(&wline, &width, header, view->ncols);
1987 if (view_needs_focus_indication(view))
1988 wstandout(view->window);
1989 waddwstr(view->window, wline);
1990 if (view_needs_focus_indication(view))
1991 wstandend(view->window);
1992 if (width < view->ncols)
1993 waddch(view->window, '\n');
2001 while (nprinted < max_lines) {
2002 line = parse_next_line(f, &len);
2007 if (++nlines < *first_displayed_line) {
2012 err = format_line(&wline, &width, line, view->ncols);
2017 waddwstr(view->window, wline);
2018 if (width < view->ncols)
2019 waddch(view->window, '\n');
2020 if (++nprinted == 1)
2021 *first_displayed_line = nlines;
2026 *last_displayed_line = nlines;
2034 get_datestr(time_t *time, char *datebuf)
2036 char *p, *s = ctime_r(time, datebuf);
2037 p = strchr(s, '\n');
2043 static const struct got_error *
2044 write_commit_info(struct got_object_id *commit_id,
2045 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2047 const struct got_error *err = NULL;
2049 struct got_commit_object *commit;
2050 char *id_str = NULL;
2051 time_t committer_time;
2052 const char *author, *committer;
2053 char *refs_str = NULL;
2056 err = build_refs_str(&refs_str, refs, commit_id);
2061 err = got_object_open_as_commit(&commit, repo, commit_id);
2065 err = got_object_id_str(&id_str, commit_id);
2067 err = got_error_from_errno();
2071 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2072 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2073 err = got_error_from_errno();
2076 if (fprintf(outfile, "from: %s\n",
2077 got_object_commit_get_author(commit)) < 0) {
2078 err = got_error_from_errno();
2081 committer_time = got_object_commit_get_committer_time(commit);
2082 if (fprintf(outfile, "date: %s UTC\n",
2083 get_datestr(&committer_time, datebuf)) < 0) {
2084 err = got_error_from_errno();
2087 author = got_object_commit_get_author(commit);
2088 committer = got_object_commit_get_committer(commit);
2089 if (strcmp(author, committer) != 0 &&
2090 fprintf(outfile, "via: %s\n", committer) < 0) {
2091 err = got_error_from_errno();
2094 if (fprintf(outfile, "%s\n",
2095 got_object_commit_get_logmsg(commit)) < 0) {
2096 err = got_error_from_errno();
2102 got_object_commit_close(commit);
2106 static const struct got_error *
2107 create_diff(struct tog_diff_view_state *s)
2109 const struct got_error *err = NULL;
2115 err = got_error_from_errno();
2118 if (s->f && fclose(s->f) != 0) {
2119 err = got_error_from_errno();
2125 err = got_object_get_type(&obj_type, s->repo, s->id1);
2127 err = got_object_get_type(&obj_type, s->repo, s->id2);
2132 case GOT_OBJ_TYPE_BLOB:
2133 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2134 s->diff_context, s->repo, f);
2136 case GOT_OBJ_TYPE_TREE:
2137 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2138 s->diff_context, s->repo, f);
2140 case GOT_OBJ_TYPE_COMMIT: {
2141 const struct got_object_id_queue *parent_ids;
2142 struct got_object_qid *pid;
2143 struct got_commit_object *commit2;
2145 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2148 /* Show commit info if we're diffing to a parent/root commit. */
2150 write_commit_info(s->id2, s->refs, s->repo, f);
2152 parent_ids = got_object_commit_get_parent_ids(commit2);
2153 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2154 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2155 write_commit_info(s->id2, s->refs,
2161 got_object_commit_close(commit2);
2163 err = got_diff_objects_as_commits(s->id1, s->id2,
2164 s->diff_context, s->repo, f);
2168 err = got_error(GOT_ERR_OBJ_TYPE);
2172 if (f && fflush(f) != 0 && err == NULL)
2173 err = got_error_from_errno();
2178 diff_view_indicate_progress(struct tog_view *view)
2180 werase(view->window);
2181 waddstr(view->window, "diffing...");
2186 static const struct got_error *
2187 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2188 struct got_object_id *id2, struct tog_view *log_view,
2189 struct got_reflist_head *refs, struct got_repository *repo)
2191 const struct got_error *err;
2193 if (id1 != NULL && id2 != NULL) {
2195 err = got_object_get_type(&type1, repo, id1);
2198 err = got_object_get_type(&type2, repo, id2);
2203 return got_error(GOT_ERR_OBJ_TYPE);
2207 view->state.diff.id1 = got_object_id_dup(id1);
2208 if (view->state.diff.id1 == NULL)
2209 return got_error_from_errno();
2211 view->state.diff.id1 = NULL;
2213 view->state.diff.id2 = got_object_id_dup(id2);
2214 if (view->state.diff.id2 == NULL) {
2215 free(view->state.diff.id1);
2216 view->state.diff.id1 = NULL;
2217 return got_error_from_errno();
2219 view->state.diff.f = NULL;
2220 view->state.diff.first_displayed_line = 1;
2221 view->state.diff.last_displayed_line = view->nlines;
2222 view->state.diff.diff_context = 3;
2223 view->state.diff.log_view = log_view;
2224 view->state.diff.repo = repo;
2225 view->state.diff.refs = refs;
2227 if (log_view && view_is_splitscreen(view))
2228 show_log_view(log_view); /* draw vborder */
2229 diff_view_indicate_progress(view);
2231 err = create_diff(&view->state.diff);
2233 free(view->state.diff.id1);
2234 view->state.diff.id1 = NULL;
2235 free(view->state.diff.id2);
2236 view->state.diff.id2 = NULL;
2240 view->show = show_diff_view;
2241 view->input = input_diff_view;
2242 view->close = close_diff_view;
2247 static const struct got_error *
2248 close_diff_view(struct tog_view *view)
2250 const struct got_error *err = NULL;
2252 free(view->state.diff.id1);
2253 view->state.diff.id1 = NULL;
2254 free(view->state.diff.id2);
2255 view->state.diff.id2 = NULL;
2256 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2257 err = got_error_from_errno();
2261 static const struct got_error *
2262 show_diff_view(struct tog_view *view)
2264 const struct got_error *err;
2265 struct tog_diff_view_state *s = &view->state.diff;
2266 char *id_str1 = NULL, *id_str2, *header;
2269 err = got_object_id_str(&id_str1, s->id1);
2273 err = got_object_id_str(&id_str2, s->id2);
2277 if (asprintf(&header, "diff %s %s",
2278 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2279 err = got_error_from_errno();
2287 return draw_file(view, s->f, &s->first_displayed_line,
2288 &s->last_displayed_line, &s->eof, view->nlines,
2292 static const struct got_error *
2293 set_selected_commit(struct tog_diff_view_state *s,
2294 struct commit_queue_entry *entry)
2296 const struct got_error *err;
2297 const struct got_object_id_queue *parent_ids;
2298 struct got_commit_object *selected_commit;
2299 struct got_object_qid *pid;
2302 s->id2 = got_object_id_dup(entry->id);
2304 return got_error_from_errno();
2306 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2309 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2311 pid = SIMPLEQ_FIRST(parent_ids);
2312 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2313 got_object_commit_close(selected_commit);
2317 static const struct got_error *
2318 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2319 struct tog_view **focus_view, struct tog_view *view, int ch)
2321 const struct got_error *err = NULL;
2322 struct tog_diff_view_state *s = &view->state.diff;
2323 struct tog_log_view_state *ls;
2324 struct commit_queue_entry *entry;
2330 if (s->first_displayed_line > 1)
2331 s->first_displayed_line--;
2336 if (s->first_displayed_line == 1) {
2341 while (i++ < view->nlines - 1 &&
2342 s->first_displayed_line > 1)
2343 s->first_displayed_line--;
2348 s->first_displayed_line++;
2359 while (!s->eof && i++ < view->nlines - 1) {
2361 line = parse_next_line(s->f, NULL);
2362 s->first_displayed_line++;
2368 if (s->diff_context > 0) {
2370 diff_view_indicate_progress(view);
2371 err = create_diff(s);
2375 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2377 diff_view_indicate_progress(view);
2378 err = create_diff(s);
2383 if (s->log_view == NULL)
2385 ls = &s->log_view->state.log;
2386 entry = TAILQ_PREV(ls->selected_entry,
2387 commit_queue_head, entry);
2391 err = input_log_view(NULL, NULL, NULL, s->log_view,
2396 err = set_selected_commit(s, entry);
2400 s->first_displayed_line = 1;
2401 s->last_displayed_line = view->nlines;
2403 diff_view_indicate_progress(view);
2404 err = create_diff(s);
2408 if (s->log_view == NULL)
2410 ls = &s->log_view->state.log;
2412 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2413 ls->thread_args.commits_needed++;
2415 /* Display "loading..." in log view. */
2416 show_log_view(s->log_view);
2420 err = trigger_log_thread(1 /* load_all */,
2421 &ls->thread_args.commits_needed,
2422 &ls->thread_args.log_complete,
2423 &ls->thread_args.need_commits);
2427 err = input_log_view(NULL, NULL, NULL, s->log_view,
2432 entry = TAILQ_NEXT(ls->selected_entry, entry);
2436 err = set_selected_commit(s, entry);
2440 s->first_displayed_line = 1;
2441 s->last_displayed_line = view->nlines;
2443 diff_view_indicate_progress(view);
2444 err = create_diff(s);
2453 static const struct got_error *
2454 cmd_diff(int argc, char *argv[])
2456 const struct got_error *error = NULL;
2457 struct got_repository *repo = NULL;
2458 struct got_reflist_head refs;
2459 struct got_object_id *id1 = NULL, *id2 = NULL;
2460 char *repo_path = NULL;
2461 char *id_str1 = NULL, *id_str2 = NULL;
2463 struct tog_view *view;
2465 SIMPLEQ_INIT(&refs);
2468 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2473 while ((ch = getopt(argc, argv, "")) != -1) {
2485 usage_diff(); /* TODO show local worktree changes */
2486 } else if (argc == 2) {
2487 repo_path = getcwd(NULL, 0);
2488 if (repo_path == NULL)
2489 return got_error_from_errno();
2492 } else if (argc == 3) {
2493 repo_path = realpath(argv[0], NULL);
2494 if (repo_path == NULL)
2495 return got_error_from_errno();
2503 error = got_repo_open(&repo, repo_path);
2507 error = apply_unveil(got_repo_get_path(repo), NULL);
2511 error = got_object_resolve_id_str(&id1, repo, id_str1);
2515 error = got_object_resolve_id_str(&id2, repo, id_str2);
2519 error = got_ref_list(&refs, repo);
2523 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2525 error = got_error_from_errno();
2528 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2531 error = view_loop(view);
2534 got_repo_close(repo);
2535 got_ref_list_free(&refs);
2543 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2548 struct tog_blame_line {
2550 struct got_object_id *id;
2553 static const struct got_error *
2554 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2555 const char *path, struct tog_blame_line *lines, int nlines,
2556 int blame_complete, int selected_line, int *first_displayed_line,
2557 int *last_displayed_line, int *eof, int max_lines)
2559 const struct got_error *err;
2560 int lineno = 0, nprinted = 0;
2565 struct tog_blame_line *blame_line;
2566 struct got_object_id *prev_id = NULL;
2569 err = got_object_id_str(&id_str, id);
2574 werase(view->window);
2576 if (asprintf(&line, "commit %s", id_str) == -1) {
2577 err = got_error_from_errno();
2582 err = format_line(&wline, &width, line, view->ncols);
2585 if (view_needs_focus_indication(view))
2586 wstandout(view->window);
2587 waddwstr(view->window, wline);
2588 if (view_needs_focus_indication(view))
2589 wstandend(view->window);
2592 if (width < view->ncols)
2593 waddch(view->window, '\n');
2595 if (asprintf(&line, "[%d/%d] %s%s",
2596 *first_displayed_line - 1 + selected_line, nlines,
2597 blame_complete ? "" : "annotating... ", path) == -1) {
2599 return got_error_from_errno();
2602 err = format_line(&wline, &width, line, view->ncols);
2607 waddwstr(view->window, wline);
2610 if (width < view->ncols)
2611 waddch(view->window, '\n');
2614 while (nprinted < max_lines - 2) {
2615 line = parse_next_line(f, &len);
2620 if (++lineno < *first_displayed_line) {
2625 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2626 err = format_line(&wline, &width, line, wlimit);
2632 if (view->focussed && nprinted == selected_line - 1)
2633 wstandout(view->window);
2635 blame_line = &lines[lineno - 1];
2636 if (blame_line->annotated && prev_id &&
2637 got_object_id_cmp(prev_id, blame_line->id) == 0)
2638 waddstr(view->window, " ");
2639 else if (blame_line->annotated) {
2641 err = got_object_id_str(&id_str, blame_line->id);
2647 wprintw(view->window, "%.8s ", id_str);
2649 prev_id = blame_line->id;
2651 waddstr(view->window, "........ ");
2655 waddwstr(view->window, wline);
2656 while (width < wlimit) {
2657 waddch(view->window, ' ');
2660 if (view->focussed && nprinted == selected_line - 1)
2661 wstandend(view->window);
2662 if (++nprinted == 1)
2663 *first_displayed_line = lineno;
2668 *last_displayed_line = lineno;
2675 static const struct got_error *
2676 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2678 const struct got_error *err = NULL;
2679 struct tog_blame_cb_args *a = arg;
2680 struct tog_blame_line *line;
2683 if (nlines != a->nlines ||
2684 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2685 return got_error(GOT_ERR_RANGE);
2687 errcode = pthread_mutex_lock(&tog_mutex);
2689 return got_error_set_errno(errcode);
2691 if (*a->quit) { /* user has quit the blame view */
2692 err = got_error(GOT_ERR_ITER_COMPLETED);
2697 goto done; /* no change in this commit */
2699 line = &a->lines[lineno - 1];
2700 if (line->annotated)
2703 line->id = got_object_id_dup(id);
2704 if (line->id == NULL) {
2705 err = got_error_from_errno();
2708 line->annotated = 1;
2710 errcode = pthread_mutex_unlock(&tog_mutex);
2712 err = got_error_set_errno(errcode);
2717 blame_thread(void *arg)
2719 const struct got_error *err;
2720 struct tog_blame_thread_args *ta = arg;
2721 struct tog_blame_cb_args *a = ta->cb_args;
2724 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2725 blame_cb, ta->cb_args);
2727 errcode = pthread_mutex_lock(&tog_mutex);
2729 return (void *)got_error_set_errno(errcode);
2731 got_repo_close(ta->repo);
2735 errcode = pthread_mutex_unlock(&tog_mutex);
2736 if (errcode && err == NULL)
2737 err = got_error_set_errno(errcode);
2742 static struct got_object_id *
2743 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2746 struct tog_blame_line *line;
2748 line = &lines[first_displayed_line - 1 + selected_line - 1];
2749 if (!line->annotated)
2755 static const struct got_error *
2756 stop_blame(struct tog_blame *blame)
2758 const struct got_error *err = NULL;
2761 if (blame->thread) {
2763 errcode = pthread_mutex_unlock(&tog_mutex);
2765 return got_error_set_errno(errcode);
2766 errcode = pthread_join(blame->thread, (void **)&err);
2768 return got_error_set_errno(errcode);
2769 errcode = pthread_mutex_lock(&tog_mutex);
2771 return got_error_set_errno(errcode);
2772 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2774 blame->thread = NULL;
2776 if (blame->thread_args.repo) {
2777 got_repo_close(blame->thread_args.repo);
2778 blame->thread_args.repo = NULL;
2781 if (fclose(blame->f) != 0 && err == NULL)
2782 err = got_error_from_errno();
2786 for (i = 0; i < blame->nlines; i++)
2787 free(blame->lines[i].id);
2789 blame->lines = NULL;
2791 free(blame->cb_args.commit_id);
2792 blame->cb_args.commit_id = NULL;
2797 static const struct got_error *
2798 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2799 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2800 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2801 struct got_repository *repo)
2803 const struct got_error *err = NULL;
2804 struct got_blob_object *blob = NULL;
2805 struct got_repository *thread_repo = NULL;
2806 struct got_object_id *obj_id = NULL;
2809 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2813 return got_error(GOT_ERR_NO_OBJ);
2815 err = got_object_get_type(&obj_type, repo, obj_id);
2819 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2820 err = got_error(GOT_ERR_OBJ_TYPE);
2824 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2827 blame->f = got_opentemp();
2828 if (blame->f == NULL) {
2829 err = got_error_from_errno();
2832 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2837 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2838 if (blame->lines == NULL) {
2839 err = got_error_from_errno();
2843 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2847 blame->cb_args.view = view;
2848 blame->cb_args.lines = blame->lines;
2849 blame->cb_args.nlines = blame->nlines;
2850 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2851 if (blame->cb_args.commit_id == NULL) {
2852 err = got_error_from_errno();
2855 blame->cb_args.quit = done;
2857 blame->thread_args.path = path;
2858 blame->thread_args.repo = thread_repo;
2859 blame->thread_args.cb_args = &blame->cb_args;
2860 blame->thread_args.complete = blame_complete;
2861 *blame_complete = 0;
2865 got_object_blob_close(blob);
2872 static const struct got_error *
2873 open_blame_view(struct tog_view *view, char *path,
2874 struct got_object_id *commit_id, struct got_reflist_head *refs,
2875 struct got_repository *repo)
2877 const struct got_error *err = NULL;
2878 struct tog_blame_view_state *s = &view->state.blame;
2880 SIMPLEQ_INIT(&s->blamed_commits);
2882 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2886 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2887 s->first_displayed_line = 1;
2888 s->last_displayed_line = view->nlines;
2889 s->selected_line = 1;
2890 s->blame_complete = 0;
2892 if (s->path == NULL)
2893 return got_error_from_errno();
2896 s->commit_id = commit_id;
2897 memset(&s->blame, 0, sizeof(s->blame));
2899 view->show = show_blame_view;
2900 view->input = input_blame_view;
2901 view->close = close_blame_view;
2903 return run_blame(&s->blame, view, &s->blame_complete,
2904 &s->first_displayed_line, &s->last_displayed_line,
2905 &s->selected_line, &s->done, &s->eof, s->path,
2906 s->blamed_commit->id, s->repo);
2909 static const struct got_error *
2910 close_blame_view(struct tog_view *view)
2912 const struct got_error *err = NULL;
2913 struct tog_blame_view_state *s = &view->state.blame;
2915 if (s->blame.thread)
2916 err = stop_blame(&s->blame);
2918 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2919 struct got_object_qid *blamed_commit;
2920 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2921 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2922 got_object_qid_free(blamed_commit);
2930 static const struct got_error *
2931 show_blame_view(struct tog_view *view)
2933 const struct got_error *err = NULL;
2934 struct tog_blame_view_state *s = &view->state.blame;
2937 if (s->blame.thread == NULL) {
2938 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2939 &s->blame.thread_args);
2941 return got_error_set_errno(errcode);
2944 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2945 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2946 s->selected_line, &s->first_displayed_line,
2947 &s->last_displayed_line, &s->eof, view->nlines);
2953 static const struct got_error *
2954 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2955 struct tog_view **focus_view, struct tog_view *view, int ch)
2957 const struct got_error *err = NULL, *thread_err = NULL;
2958 struct tog_view *diff_view;
2959 struct tog_blame_view_state *s = &view->state.blame;
2968 if (s->selected_line > 1)
2970 else if (s->selected_line == 1 &&
2971 s->first_displayed_line > 1)
2972 s->first_displayed_line--;
2977 if (s->first_displayed_line == 1) {
2978 if (s->selected_line == 1) {
2982 s->selected_line = 1;
2985 if (s->first_displayed_line > view->nlines - 2)
2986 s->first_displayed_line -=
2989 s->first_displayed_line = 1;
2993 if (s->selected_line < view->nlines - 2 &&
2994 s->first_displayed_line +
2995 s->selected_line <= s->blame.nlines)
2997 else if (s->last_displayed_line <
2999 s->first_displayed_line++;
3005 struct got_object_id *id = NULL;
3006 id = get_selected_commit_id(s->blame.lines,
3007 s->first_displayed_line, s->selected_line);
3011 struct got_commit_object *commit;
3012 struct got_object_qid *pid;
3013 struct got_object_id *blob_id = NULL;
3015 err = got_object_open_as_commit(&commit,
3019 pid = SIMPLEQ_FIRST(
3020 got_object_commit_get_parent_ids(commit));
3022 got_object_commit_close(commit);
3025 /* Check if path history ends here. */
3026 err = got_object_id_by_path(&blob_id, s->repo,
3029 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3031 got_object_commit_close(commit);
3034 err = got_object_get_type(&obj_type, s->repo,
3037 /* Can't blame non-blob type objects. */
3038 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3039 got_object_commit_close(commit);
3042 err = got_object_qid_alloc(&s->blamed_commit,
3044 got_object_commit_close(commit);
3046 if (got_object_id_cmp(id,
3047 s->blamed_commit->id) == 0)
3049 err = got_object_qid_alloc(&s->blamed_commit,
3055 thread_err = stop_blame(&s->blame);
3059 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3060 s->blamed_commit, entry);
3061 err = run_blame(&s->blame, view, &s->blame_complete,
3062 &s->first_displayed_line, &s->last_displayed_line,
3063 &s->selected_line, &s->done, &s->eof,
3064 s->path, s->blamed_commit->id, s->repo);
3070 struct got_object_qid *first;
3071 first = SIMPLEQ_FIRST(&s->blamed_commits);
3072 if (!got_object_id_cmp(first->id, s->commit_id))
3075 thread_err = stop_blame(&s->blame);
3079 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3080 got_object_qid_free(s->blamed_commit);
3082 SIMPLEQ_FIRST(&s->blamed_commits);
3083 err = run_blame(&s->blame, view, &s->blame_complete,
3084 &s->first_displayed_line, &s->last_displayed_line,
3085 &s->selected_line, &s->done, &s->eof, s->path,
3086 s->blamed_commit->id, s->repo);
3093 struct got_object_id *id = NULL;
3094 struct got_object_qid *pid;
3095 struct got_commit_object *commit = NULL;
3096 id = get_selected_commit_id(s->blame.lines,
3097 s->first_displayed_line, s->selected_line);
3100 err = got_object_open_as_commit(&commit, s->repo, id);
3103 pid = SIMPLEQ_FIRST(
3104 got_object_commit_get_parent_ids(commit));
3105 if (view_is_parent_view(view))
3106 begin_x = view_split_begin_x(view->begin_x);
3107 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3108 if (diff_view == NULL) {
3109 got_object_commit_close(commit);
3110 err = got_error_from_errno();
3113 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3114 id, NULL, s->refs, s->repo);
3115 got_object_commit_close(commit);
3117 view_close(diff_view);
3120 if (view_is_parent_view(view)) {
3121 err = view_close_child(view);
3124 err = view_set_child(view, diff_view);
3126 view_close(diff_view);
3129 *focus_view = diff_view;
3130 view->child_focussed = 1;
3132 *new_view = diff_view;
3139 if (s->last_displayed_line >= s->blame.nlines &&
3140 s->selected_line >= MIN(s->blame.nlines,
3141 view->nlines - 2)) {
3145 if (s->last_displayed_line >= s->blame.nlines &&
3146 s->selected_line < view->nlines - 2) {
3147 s->selected_line = MIN(s->blame.nlines,
3151 if (s->last_displayed_line + view->nlines - 2
3153 s->first_displayed_line +=
3156 s->first_displayed_line =
3161 if (s->selected_line > view->nlines - 2) {
3162 s->selected_line = MIN(s->blame.nlines,
3169 return thread_err ? thread_err : err;
3172 static const struct got_error *
3173 cmd_blame(int argc, char *argv[])
3175 const struct got_error *error;
3176 struct got_repository *repo = NULL;
3177 struct got_reflist_head refs;
3178 struct got_worktree *worktree = NULL;
3179 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3180 struct got_object_id *commit_id = NULL;
3181 char *commit_id_str = NULL;
3183 struct tog_view *view;
3185 SIMPLEQ_INIT(&refs);
3188 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3193 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3196 commit_id_str = optarg;
3199 repo_path = realpath(optarg, NULL);
3200 if (repo_path == NULL)
3201 err(1, "-r option");
3217 cwd = getcwd(NULL, 0);
3219 error = got_error_from_errno();
3222 if (repo_path == NULL) {
3223 error = got_worktree_open(&worktree, cwd);
3224 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3230 strdup(got_worktree_get_repo_path(worktree));
3231 if (repo_path == NULL)
3232 error = got_error_from_errno();
3236 repo_path = strdup(cwd);
3237 if (repo_path == NULL) {
3238 error = got_error_from_errno();
3246 error = got_repo_open(&repo, repo_path);
3250 error = apply_unveil(got_repo_get_path(repo), NULL);
3255 const char *prefix = got_worktree_get_path_prefix(worktree);
3256 char *p, *worktree_subdir = cwd +
3257 strlen(got_worktree_get_root_path(worktree));
3258 if (asprintf(&p, "%s%s%s%s%s",
3259 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3260 worktree_subdir, worktree_subdir[0] ? "/" : "",
3262 error = got_error_from_errno();
3265 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3268 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3273 if (commit_id_str == NULL) {
3274 struct got_reference *head_ref;
3275 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3278 error = got_ref_resolve(&commit_id, repo, head_ref);
3279 got_ref_close(head_ref);
3281 error = got_object_resolve_id_str(&commit_id, repo,
3287 error = got_ref_list(&refs, repo);
3291 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3293 error = got_error_from_errno();
3296 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3299 error = view_loop(view);
3305 got_worktree_close(worktree);
3307 got_repo_close(repo);
3308 got_ref_list_free(&refs);
3312 static const struct got_error *
3313 draw_tree_entries(struct tog_view *view,
3314 struct got_tree_entry **first_displayed_entry,
3315 struct got_tree_entry **last_displayed_entry,
3316 struct got_tree_entry **selected_entry, int *ndisplayed,
3317 const char *label, int show_ids, const char *parent_path,
3318 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3320 const struct got_error *err = NULL;
3321 struct got_tree_entry *te;
3327 werase(view->window);
3332 err = format_line(&wline, &width, label, view->ncols);
3335 if (view_needs_focus_indication(view))
3336 wstandout(view->window);
3337 waddwstr(view->window, wline);
3338 if (view_needs_focus_indication(view))
3339 wstandend(view->window);
3342 if (width < view->ncols)
3343 waddch(view->window, '\n');
3346 err = format_line(&wline, &width, parent_path, view->ncols);
3349 waddwstr(view->window, wline);
3352 if (width < view->ncols)
3353 waddch(view->window, '\n');
3356 waddch(view->window, '\n');
3360 te = SIMPLEQ_FIRST(&entries->head);
3361 if (*first_displayed_entry == NULL) {
3362 if (selected == 0) {
3364 wstandout(view->window);
3365 *selected_entry = NULL;
3367 waddstr(view->window, " ..\n"); /* parent directory */
3368 if (selected == 0 && view->focussed)
3369 wstandend(view->window);
3376 while (te != *first_displayed_entry)
3377 te = SIMPLEQ_NEXT(te, entry);
3381 char *line = NULL, *id_str = NULL;
3384 err = got_object_id_str(&id_str, te->id);
3386 return got_error_from_errno();
3388 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3389 te->name, S_ISDIR(te->mode) ? "/" :
3390 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3392 return got_error_from_errno();
3395 err = format_line(&wline, &width, line, view->ncols);
3400 if (n == selected) {
3402 wstandout(view->window);
3403 *selected_entry = te;
3405 waddwstr(view->window, wline);
3406 if (width < view->ncols)
3407 waddch(view->window, '\n');
3408 if (n == selected && view->focussed)
3409 wstandend(view->window);
3415 *last_displayed_entry = te;
3418 te = SIMPLEQ_NEXT(te, entry);
3425 tree_scroll_up(struct tog_view *view,
3426 struct got_tree_entry **first_displayed_entry, int maxscroll,
3427 const struct got_tree_entries *entries, int isroot)
3429 struct got_tree_entry *te, *prev;
3432 if (*first_displayed_entry == NULL) {
3437 te = SIMPLEQ_FIRST(&entries->head);
3438 if (*first_displayed_entry == te) {
3441 *first_displayed_entry = NULL;
3445 /* XXX this is stupid... switch to TAILQ? */
3446 for (i = 0; i < maxscroll; i++) {
3447 while (te != *first_displayed_entry) {
3449 te = SIMPLEQ_NEXT(te, entry);
3451 *first_displayed_entry = prev;
3452 te = SIMPLEQ_FIRST(&entries->head);
3454 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3455 *first_displayed_entry = NULL;
3459 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3460 struct got_tree_entry *last_displayed_entry,
3461 const struct got_tree_entries *entries)
3463 struct got_tree_entry *next, *last;
3466 if (*first_displayed_entry)
3467 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3469 next = SIMPLEQ_FIRST(&entries->head);
3470 last = last_displayed_entry;
3471 while (next && last && n++ < maxscroll) {
3472 last = SIMPLEQ_NEXT(last, entry);
3474 *first_displayed_entry = next;
3475 next = SIMPLEQ_NEXT(next, entry);
3481 static const struct got_error *
3482 tree_entry_path(char **path, struct tog_parent_trees *parents,
3483 struct got_tree_entry *te)
3485 const struct got_error *err = NULL;
3486 struct tog_parent_tree *pt;
3487 size_t len = 2; /* for leading slash and NUL */
3489 TAILQ_FOREACH(pt, parents, entry)
3490 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3492 len += strlen(te->name);
3494 *path = calloc(1, len);
3496 return got_error_from_errno();
3499 pt = TAILQ_LAST(parents, tog_parent_trees);
3501 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3502 err = got_error(GOT_ERR_NO_SPACE);
3505 if (strlcat(*path, "/", len) >= len) {
3506 err = got_error(GOT_ERR_NO_SPACE);
3509 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3512 if (strlcat(*path, te->name, len) >= len) {
3513 err = got_error(GOT_ERR_NO_SPACE);
3525 static const struct got_error *
3526 blame_tree_entry(struct tog_view **new_view, int begin_x,
3527 struct got_tree_entry *te, struct tog_parent_trees *parents,
3528 struct got_object_id *commit_id, struct got_reflist_head *refs,
3529 struct got_repository *repo)
3531 const struct got_error *err = NULL;
3533 struct tog_view *blame_view;
3535 err = tree_entry_path(&path, parents, te);
3539 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3540 if (blame_view == NULL)
3541 return got_error_from_errno();
3543 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3545 view_close(blame_view);
3548 *new_view = blame_view;
3552 static const struct got_error *
3553 log_tree_entry(struct tog_view **new_view, int begin_x,
3554 struct got_tree_entry *te, struct tog_parent_trees *parents,
3555 struct got_object_id *commit_id, struct got_reflist_head *refs,
3556 struct got_repository *repo)
3558 struct tog_view *log_view;
3559 const struct got_error *err = NULL;
3562 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3563 if (log_view == NULL)
3564 return got_error_from_errno();
3566 err = tree_entry_path(&path, parents, te);
3570 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3572 view_close(log_view);
3574 *new_view = log_view;
3579 static const struct got_error *
3580 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3581 struct got_object_id *commit_id, struct got_reflist_head *refs,
3582 struct got_repository *repo)
3584 const struct got_error *err = NULL;
3585 char *commit_id_str = NULL;
3586 struct tog_tree_view_state *s = &view->state.tree;
3588 TAILQ_INIT(&s->parents);
3590 err = got_object_id_str(&commit_id_str, commit_id);
3594 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3595 err = got_error_from_errno();
3599 s->root = s->tree = root;
3600 s->entries = got_object_tree_get_entries(root);
3601 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3602 s->commit_id = got_object_id_dup(commit_id);
3603 if (s->commit_id == NULL) {
3604 err = got_error_from_errno();
3610 view->show = show_tree_view;
3611 view->input = input_tree_view;
3612 view->close = close_tree_view;
3614 free(commit_id_str);
3616 free(s->tree_label);
3617 s->tree_label = NULL;
3622 static const struct got_error *
3623 close_tree_view(struct tog_view *view)
3625 struct tog_tree_view_state *s = &view->state.tree;
3627 free(s->tree_label);
3628 s->tree_label = NULL;
3630 s->commit_id = NULL;
3631 while (!TAILQ_EMPTY(&s->parents)) {
3632 struct tog_parent_tree *parent;
3633 parent = TAILQ_FIRST(&s->parents);
3634 TAILQ_REMOVE(&s->parents, parent, entry);
3638 if (s->tree != s->root)
3639 got_object_tree_close(s->tree);
3640 got_object_tree_close(s->root);
3645 static const struct got_error *
3646 show_tree_view(struct tog_view *view)
3648 const struct got_error *err = NULL;
3649 struct tog_tree_view_state *s = &view->state.tree;
3652 err = tree_entry_path(&parent_path, &s->parents, NULL);
3656 err = draw_tree_entries(view, &s->first_displayed_entry,
3657 &s->last_displayed_entry, &s->selected_entry,
3658 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3659 s->entries, s->selected, view->nlines, s->tree == s->root);
3666 static const struct got_error *
3667 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3668 struct tog_view **focus_view, struct tog_view *view, int ch)
3670 const struct got_error *err = NULL;
3671 struct tog_tree_view_state *s = &view->state.tree;
3672 struct tog_view *log_view;
3673 int begin_x = 0, nscrolled;
3677 s->show_ids = !s->show_ids;
3680 if (!s->selected_entry)
3682 if (view_is_parent_view(view))
3683 begin_x = view_split_begin_x(view->begin_x);
3684 err = log_tree_entry(&log_view, begin_x,
3685 s->selected_entry, &s->parents,
3686 s->commit_id, s->refs, s->repo);
3687 if (view_is_parent_view(view)) {
3688 err = view_close_child(view);
3691 err = view_set_child(view, log_view);
3693 view_close(log_view);
3696 *focus_view = log_view;
3697 view->child_focussed = 1;
3699 *new_view = log_view;
3703 if (s->selected > 0) {
3705 if (s->selected == 0)
3708 if (s->selected > 0)
3710 tree_scroll_up(view, &s->first_displayed_entry, 1,
3711 s->entries, s->tree == s->root);
3714 tree_scroll_up(view, &s->first_displayed_entry,
3715 MAX(0, view->nlines - 4 - s->selected), s->entries,
3716 s->tree == s->root);
3718 if (SIMPLEQ_FIRST(&s->entries->head) ==
3719 s->first_displayed_entry && s->tree != s->root)
3720 s->first_displayed_entry = NULL;
3724 if (s->selected < s->ndisplayed - 1) {
3728 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3730 /* can't scroll any further */
3734 tree_scroll_down(&s->first_displayed_entry, 1,
3735 s->last_displayed_entry, s->entries);
3738 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3740 /* can't scroll any further; move cursor down */
3741 if (s->selected < s->ndisplayed - 1)
3742 s->selected = s->ndisplayed - 1;
3747 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3748 view->nlines, s->last_displayed_entry, s->entries);
3749 if (nscrolled < view->nlines) {
3751 struct got_tree_entry *te;
3752 te = s->first_displayed_entry;
3755 te = SIMPLEQ_NEXT(te, entry);
3757 s->selected = ndisplayed - 1;
3762 if (s->selected_entry == NULL) {
3763 struct tog_parent_tree *parent;
3765 /* user selected '..' */
3766 if (s->tree == s->root)
3768 parent = TAILQ_FIRST(&s->parents);
3769 TAILQ_REMOVE(&s->parents, parent,
3771 got_object_tree_close(s->tree);
3772 s->tree = parent->tree;
3774 got_object_tree_get_entries(s->tree);
3775 s->first_displayed_entry =
3776 parent->first_displayed_entry;
3778 parent->selected_entry;
3779 s->selected = parent->selected;
3781 } else if (S_ISDIR(s->selected_entry->mode)) {
3782 struct tog_parent_tree *parent;
3783 struct got_tree_object *child;
3784 err = got_object_open_as_tree(&child,
3785 s->repo, s->selected_entry->id);
3788 parent = calloc(1, sizeof(*parent));
3789 if (parent == NULL) {
3790 err = got_error_from_errno();
3793 parent->tree = s->tree;
3794 parent->first_displayed_entry =
3795 s->first_displayed_entry;
3796 parent->selected_entry = s->selected_entry;
3797 parent->selected = s->selected;
3798 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3801 got_object_tree_get_entries(s->tree);
3803 s->first_displayed_entry = NULL;
3804 } else if (S_ISREG(s->selected_entry->mode)) {
3805 struct tog_view *blame_view;
3806 int begin_x = view_is_parent_view(view) ?
3807 view_split_begin_x(view->begin_x) : 0;
3809 err = blame_tree_entry(&blame_view, begin_x,
3810 s->selected_entry, &s->parents,
3811 s->commit_id, s->refs, s->repo);
3814 if (view_is_parent_view(view)) {
3815 err = view_close_child(view);
3818 err = view_set_child(view, blame_view);
3820 view_close(blame_view);
3823 *focus_view = blame_view;
3824 view->child_focussed = 1;
3826 *new_view = blame_view;
3830 if (s->selected > view->nlines)
3831 s->selected = s->ndisplayed - 1;
3844 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3849 static const struct got_error *
3850 cmd_tree(int argc, char *argv[])
3852 const struct got_error *error;
3853 struct got_repository *repo = NULL;
3854 struct got_reflist_head refs;
3855 char *repo_path = NULL;
3856 struct got_object_id *commit_id = NULL;
3857 char *commit_id_arg = NULL;
3858 struct got_commit_object *commit = NULL;
3859 struct got_tree_object *tree = NULL;
3861 struct tog_view *view;
3863 SIMPLEQ_INIT(&refs);
3866 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3871 while ((ch = getopt(argc, argv, "c:")) != -1) {
3874 commit_id_arg = optarg;
3886 struct got_worktree *worktree;
3887 char *cwd = getcwd(NULL, 0);
3889 return got_error_from_errno();
3890 error = got_worktree_open(&worktree, cwd);
3891 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3896 strdup(got_worktree_get_repo_path(worktree));
3897 got_worktree_close(worktree);
3900 if (repo_path == NULL) {
3901 error = got_error_from_errno();
3904 } else if (argc == 1) {
3905 repo_path = realpath(argv[0], NULL);
3906 if (repo_path == NULL)
3907 return got_error_from_errno();
3913 error = got_repo_open(&repo, repo_path);
3917 error = apply_unveil(got_repo_get_path(repo), NULL);
3921 if (commit_id_arg == NULL)
3922 error = get_head_commit_id(&commit_id, repo);
3924 error = got_object_resolve_id_str(&commit_id, repo,
3929 error = got_object_open_as_commit(&commit, repo, commit_id);
3933 error = got_object_open_as_tree(&tree, repo,
3934 got_object_commit_get_tree_id(commit));
3938 error = got_ref_list(&refs, repo);
3942 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3944 error = got_error_from_errno();
3947 error = open_tree_view(view, tree, commit_id, &refs, repo);
3950 error = view_loop(view);
3955 got_object_commit_close(commit);
3957 got_object_tree_close(tree);
3959 got_repo_close(repo);
3960 got_ref_list_free(&refs);
3969 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3970 "Available commands:\n", getprogname());
3971 for (i = 0; i < nitems(tog_commands); i++) {
3972 struct tog_cmd *cmd = &tog_commands[i];
3973 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3979 make_argv(const char *arg0, const char *arg1)
3982 int argc = (arg1 == NULL ? 1 : 2);
3984 argv = calloc(argc, sizeof(char *));
3987 argv[0] = strdup(arg0);
3988 if (argv[0] == NULL)
3991 argv[1] = strdup(arg1);
3992 if (argv[1] == NULL)
4000 main(int argc, char *argv[])
4002 const struct got_error *error = NULL;
4003 struct tog_cmd *cmd = NULL;
4005 char **cmd_argv = NULL;
4007 setlocale(LC_CTYPE, "");
4009 while ((ch = getopt(argc, argv, "h")) != -1) {
4028 /* Build an argument vector which runs a default command. */
4029 cmd = &tog_commands[0];
4030 cmd_argv = make_argv(cmd->name, NULL);
4035 /* Did the user specific a command? */
4036 for (i = 0; i < nitems(tog_commands); i++) {
4037 if (strncmp(tog_commands[i].name, argv[0],
4038 strlen(argv[0])) == 0) {
4039 cmd = &tog_commands[i];
4041 tog_commands[i].cmd_usage();
4046 /* Did the user specify a repository? */
4047 char *repo_path = realpath(argv[0], NULL);
4049 struct got_repository *repo;
4050 error = got_repo_open(&repo, repo_path);
4052 got_repo_close(repo);
4054 error = got_error_from_errno();
4057 fprintf(stderr, "%s: '%s' is not a "
4058 "known command\n", getprogname(),
4062 fprintf(stderr, "%s: '%s' is neither a known "
4063 "command nor a path to a repository\n",
4064 getprogname(), argv[0]);
4068 cmd = &tog_commands[0];
4069 cmd_argv = make_argv(cmd->name, repo_path);
4075 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4082 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);