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;
122 /* passed from log view; may be NULL */
123 struct tog_view *log_view;
126 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
128 struct tog_log_thread_args {
129 pthread_cond_t need_commits;
131 struct got_commit_graph *graph;
132 struct commit_queue *commits;
133 const char *in_repo_path;
134 struct got_object_id *start_id;
135 struct got_repository *repo;
138 struct tog_view *view;
139 struct commit_queue_entry **first_displayed_entry;
140 struct commit_queue_entry **selected_entry;
143 struct tog_log_view_state {
144 struct commit_queue commits;
145 struct commit_queue_entry *first_displayed_entry;
146 struct commit_queue_entry *last_displayed_entry;
147 struct commit_queue_entry *selected_entry;
150 struct got_repository *repo;
151 struct got_object_id *start_id;
154 struct tog_log_thread_args thread_args;
157 struct tog_blame_cb_args {
158 struct tog_blame_line *lines; /* one per line */
161 struct tog_view *view;
162 struct got_object_id *commit_id;
166 struct tog_blame_thread_args {
168 struct got_repository *repo;
169 struct tog_blame_cb_args *cb_args;
176 struct tog_blame_line *lines;
179 struct tog_blame_thread_args thread_args;
180 struct tog_blame_cb_args cb_args;
184 struct tog_blame_view_state {
185 int first_displayed_line;
186 int last_displayed_line;
191 struct got_object_id_queue blamed_commits;
192 struct got_object_qid *blamed_commit;
194 struct got_repository *repo;
195 struct got_object_id *commit_id;
196 struct tog_blame blame;
199 struct tog_parent_tree {
200 TAILQ_ENTRY(tog_parent_tree) entry;
201 struct got_tree_object *tree;
202 struct got_tree_entry *first_displayed_entry;
203 struct got_tree_entry *selected_entry;
207 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
209 struct tog_tree_view_state {
211 struct got_tree_object *root;
212 struct got_tree_object *tree;
213 const struct got_tree_entries *entries;
214 struct got_tree_entry *first_displayed_entry;
215 struct got_tree_entry *last_displayed_entry;
216 struct got_tree_entry *selected_entry;
217 int ndisplayed, selected, show_ids;
218 struct tog_parent_trees parents;
219 struct got_object_id *commit_id;
220 struct got_repository *repo;
224 * We implement two types of views: parent views and child views.
226 * The 'Tab' key switches between a parent view and its child view.
227 * Child views are shown side-by-side to their parent view, provided
228 * there is enough screen estate.
230 * When a new view is opened from within a parent view, this new view
231 * becomes a child view of the parent view, replacing any existing child.
233 * When a new view is opened from within a child view, this new view
234 * becomes a parent view which will obscure the views below until the
235 * user quits the new parent view by typing 'q'.
237 * This list of views contains parent views only.
238 * Child views are only pointed to by their parent view.
240 TAILQ_HEAD(tog_view_list_head, tog_view);
243 TAILQ_ENTRY(tog_view) entry;
246 int nlines, ncols, begin_y, begin_x;
247 int lines, cols; /* copies of LINES and COLS */
249 struct tog_view *parent;
250 struct tog_view *child;
253 /* type-specific state */
254 enum tog_view_type type;
256 struct tog_diff_view_state diff;
257 struct tog_log_view_state log;
258 struct tog_blame_view_state blame;
259 struct tog_tree_view_state tree;
262 const struct got_error *(*show)(struct tog_view *);
263 const struct got_error *(*input)(struct tog_view **,
264 struct tog_view **, struct tog_view**, struct tog_view *, int);
265 const struct got_error *(*close)(struct tog_view *);
268 static const struct got_error *open_diff_view(struct tog_view *,
269 struct got_object_id *, struct got_object_id *, struct tog_view *,
270 struct got_repository *);
271 static const struct got_error *show_diff_view(struct tog_view *);
272 static const struct got_error *input_diff_view(struct tog_view **,
273 struct tog_view **, struct tog_view **, struct tog_view *, int);
274 static const struct got_error* close_diff_view(struct tog_view *);
276 static const struct got_error *open_log_view(struct tog_view *,
277 struct got_object_id *, struct got_repository *, const char *, int);
278 static const struct got_error * show_log_view(struct tog_view *);
279 static const struct got_error *input_log_view(struct tog_view **,
280 struct tog_view **, struct tog_view **, struct tog_view *, int);
281 static const struct got_error *close_log_view(struct tog_view *);
283 static const struct got_error *open_blame_view(struct tog_view *, char *,
284 struct got_object_id *, struct got_repository *);
285 static const struct got_error *show_blame_view(struct tog_view *);
286 static const struct got_error *input_blame_view(struct tog_view **,
287 struct tog_view **, struct tog_view **, struct tog_view *, int);
288 static const struct got_error *close_blame_view(struct tog_view *);
290 static const struct got_error *open_tree_view(struct tog_view *,
291 struct got_tree_object *, struct got_object_id *, struct got_repository *);
292 static const struct got_error *show_tree_view(struct tog_view *);
293 static const struct got_error *input_tree_view(struct tog_view **,
294 struct tog_view **, struct tog_view **, struct tog_view *, int);
295 static const struct got_error *close_tree_view(struct tog_view *);
297 static volatile sig_atomic_t tog_sigwinch_received;
300 tog_sigwinch(int signo)
302 tog_sigwinch_received = 1;
305 static const struct got_error *
306 view_close(struct tog_view *view)
308 const struct got_error *err = NULL;
311 view_close(view->child);
315 err = view->close(view);
317 del_panel(view->panel);
319 delwin(view->window);
324 static struct tog_view *
325 view_open(int nlines, int ncols, int begin_y, int begin_x,
326 enum tog_view_type type)
328 struct tog_view *view = calloc(1, sizeof(*view));
336 view->nlines = nlines ? nlines : LINES - begin_y;
337 view->ncols = ncols ? ncols : COLS - begin_x;
338 view->begin_y = begin_y;
339 view->begin_x = begin_x;
340 view->window = newwin(nlines, ncols, begin_y, begin_x);
341 if (view->window == NULL) {
345 view->panel = new_panel(view->window);
346 if (view->panel == NULL ||
347 set_panel_userptr(view->panel, view) != OK) {
352 keypad(view->window, TRUE);
357 view_split_begin_x(int begin_x)
359 if (begin_x > 0 || COLS < 120)
361 return (COLS - MAX(COLS / 2, 80));
364 static const struct got_error *view_resize(struct tog_view *);
366 static const struct got_error *
367 view_splitscreen(struct tog_view *view)
369 const struct got_error *err = NULL;
372 view->begin_x = view_split_begin_x(0);
373 view->nlines = LINES;
374 view->ncols = COLS - view->begin_x;
377 err = view_resize(view);
381 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
382 return got_error_from_errno();
387 static const struct got_error *
388 view_fullscreen(struct tog_view *view)
390 const struct got_error *err = NULL;
394 view->nlines = LINES;
398 err = view_resize(view);
402 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
403 return got_error_from_errno();
409 view_is_parent_view(struct tog_view *view)
411 return view->parent == NULL;
414 static const struct got_error *
415 view_resize(struct tog_view *view)
419 if (view->lines > LINES)
420 nlines = view->nlines - (view->lines - LINES);
422 nlines = view->nlines + (LINES - view->lines);
424 if (view->cols > COLS)
425 ncols = view->ncols - (view->cols - COLS);
427 ncols = view->ncols + (COLS - view->cols);
429 if (wresize(view->window, nlines, ncols) == ERR)
430 return got_error_from_errno();
431 if (replace_panel(view->panel, view->window) == ERR)
432 return got_error_from_errno();
433 wclear(view->window);
435 view->nlines = nlines;
441 view->child->begin_x = view_split_begin_x(view->begin_x);
442 if (view->child->begin_x == 0) {
443 view_fullscreen(view->child);
444 if (view->child->focussed)
445 show_panel(view->child->panel);
447 show_panel(view->panel);
449 view_splitscreen(view->child);
450 show_panel(view->child->panel);
457 static const struct got_error *
458 view_close_child(struct tog_view *view)
460 const struct got_error *err = NULL;
462 if (view->child == NULL)
465 err = view_close(view->child);
470 static const struct got_error *
471 view_set_child(struct tog_view *view, struct tog_view *child)
473 const struct got_error *err = NULL;
476 child->parent = view;
481 view_is_splitscreen(struct tog_view *view)
483 return !view_is_parent_view(view) && view->begin_x > 0;
492 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
493 cols = 80; /* Default */
499 resize_term(lines, cols);
502 static const struct got_error *
503 view_input(struct tog_view **new, struct tog_view **dead,
504 struct tog_view **focus, int *done, struct tog_view *view,
505 struct tog_view_list_head *views)
507 const struct got_error *err = NULL;
515 nodelay(stdscr, FALSE);
516 /* Allow threads to make progress while we are waiting for input. */
517 errcode = pthread_mutex_unlock(&tog_mutex);
519 return got_error_set_errno(errcode);
520 ch = wgetch(view->window);
521 errcode = pthread_mutex_lock(&tog_mutex);
523 return got_error_set_errno(errcode);
524 nodelay(stdscr, TRUE);
526 if (tog_sigwinch_received) {
528 tog_sigwinch_received = 0;
529 TAILQ_FOREACH(v, views, entry) {
530 err = view_resize(v);
533 err = v->input(new, dead, focus, v, KEY_RESIZE);
544 *focus = view->child;
545 view->child_focussed = 1;
546 } else if (view->parent) {
547 *focus = view->parent;
548 view->parent->child_focussed = 0;
552 err = view->input(new, dead, focus, view, ch);
559 if (view_is_parent_view(view)) {
560 if (view->child == NULL)
562 if (view_is_splitscreen(view->child)) {
563 *focus = view->child;
564 view->child_focussed = 1;
565 err = view_fullscreen(view->child);
567 err = view_splitscreen(view->child);
570 err = view->child->input(new, dead, focus,
571 view->child, KEY_RESIZE);
573 if (view_is_splitscreen(view)) {
575 view->parent->child_focussed = 1;
576 err = view_fullscreen(view);
578 err = view_splitscreen(view);
582 err = view->input(new, dead, focus, view,
589 err = view->input(new, dead, focus, view, ch);
597 view_vborder(struct tog_view *view)
600 struct tog_view *view_above;
603 return view_vborder(view->parent);
605 panel = panel_above(view->panel);
609 view_above = panel_userptr(panel);
610 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
611 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
615 view_needs_focus_indication(struct tog_view *view)
617 if (view_is_parent_view(view)) {
618 if (view->child == NULL || view->child_focussed)
620 if (!view_is_splitscreen(view->child))
622 } else if (!view_is_splitscreen(view))
625 return view->focussed;
628 static const struct got_error *
629 view_loop(struct tog_view *view)
631 const struct got_error *err = NULL;
632 struct tog_view_list_head views;
633 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
634 int fast_refresh = 10;
635 int done = 0, errcode;
637 errcode = pthread_mutex_lock(&tog_mutex);
639 return got_error_set_errno(errcode);
642 TAILQ_INSERT_HEAD(&views, view, entry);
646 err = view->show(view);
651 while (!TAILQ_EMPTY(&views) && !done) {
652 /* Refresh fast during initialization, then become slower. */
653 if (fast_refresh && fast_refresh-- == 0)
654 halfdelay(10); /* switch to once per second */
656 err = view_input(&new_view, &dead_view, &focus_view, &done,
661 struct tog_view *prev = NULL;
663 if (view_is_parent_view(dead_view))
664 prev = TAILQ_PREV(dead_view,
665 tog_view_list_head, entry);
666 else if (view->parent != dead_view)
669 if (dead_view->parent)
670 dead_view->parent->child = NULL;
672 TAILQ_REMOVE(&views, dead_view, entry);
674 err = view_close(dead_view);
675 if (err || dead_view == main_view)
678 if (view == dead_view) {
683 else if (!TAILQ_EMPTY(&views))
684 view = TAILQ_LAST(&views,
689 if (view->child && view->child_focussed)
690 focus_view = view->child;
697 struct tog_view *v, *t;
698 /* Only allow one parent view per type. */
699 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
700 if (v->type != new_view->type)
702 TAILQ_REMOVE(&views, v, entry);
708 TAILQ_INSERT_TAIL(&views, new_view, entry);
710 if (focus_view == NULL)
711 focus_view = new_view;
714 show_panel(focus_view->panel);
717 focus_view->focussed = 1;
720 show_panel(new_view->panel);
721 if (view->child && view_is_splitscreen(view->child))
722 show_panel(view->child->panel);
725 if (focus_view == NULL) {
727 show_panel(view->panel);
728 if (view->child && view_is_splitscreen(view->child))
729 show_panel(view->child->panel);
733 err = view->parent->show(view->parent);
737 err = view->show(view);
741 err = view->child->show(view->child);
750 while (!TAILQ_EMPTY(&views)) {
751 view = TAILQ_FIRST(&views);
752 TAILQ_REMOVE(&views, view, entry);
756 errcode = pthread_mutex_unlock(&tog_mutex);
758 return got_error_set_errno(errcode);
768 "usage: %s log [-c commit] [-r repository-path] [path]\n",
773 /* Create newly allocated wide-character string equivalent to a byte string. */
774 static const struct got_error *
775 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
778 const struct got_error *err = NULL;
781 *wlen = mbstowcs(NULL, s, 0);
782 if (*wlen == (size_t)-1) {
785 return got_error_from_errno();
787 /* byte string invalid in current encoding; try to "fix" it */
788 err = got_mbsavis(&vis, &vislen, s);
791 *wlen = mbstowcs(NULL, vis, 0);
792 if (*wlen == (size_t)-1) {
793 err = got_error_from_errno(); /* give up */
798 *ws = calloc(*wlen + 1, sizeof(*ws));
800 err = got_error_from_errno();
804 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
805 err = got_error_from_errno();
816 /* Format a line for display, ensuring that it won't overflow a width limit. */
817 static const struct got_error *
818 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
820 const struct got_error *err = NULL;
822 wchar_t *wline = NULL;
829 err = mbs2ws(&wline, &wlen, line);
834 while (i < wlen && cols < wlimit) {
835 int width = wcwidth(wline[i]);
842 if (cols + width <= wlimit)
847 if (wline[i] == L'\t')
848 cols += TABSIZE - ((cols + 1) % TABSIZE);
852 err = got_error_from_errno();
867 static const struct got_error *
868 draw_commit(struct tog_view *view, struct got_commit_object *commit,
869 struct got_object_id *id)
871 const struct got_error *err = NULL;
872 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
873 char *logmsg0 = NULL, *logmsg = NULL;
874 char *author0 = NULL, *author = NULL;
875 wchar_t *wlogmsg = NULL, *wauthor = NULL;
876 int author_width, logmsg_width;
877 char *newline, *smallerthan;
880 static const size_t date_display_cols = 9;
881 static const size_t author_display_cols = 16;
882 const int avail = view->ncols;
884 time_t committer_time;
886 committer_time = got_object_commit_get_committer_time(commit);
887 if (localtime_r(&committer_time, &tm) == NULL)
888 return got_error_from_errno();
889 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
891 return got_error(GOT_ERR_NO_SPACE);
893 if (avail < date_display_cols)
894 limit = MIN(sizeof(datebuf) - 1, avail);
896 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
897 waddnstr(view->window, datebuf, limit);
902 author0 = strdup(got_object_commit_get_author(commit));
903 if (author0 == NULL) {
904 err = got_error_from_errno();
908 smallerthan = strchr(author, '<');
912 char *at = strchr(author, '@');
917 err = format_line(&wauthor, &author_width, author, limit);
920 waddwstr(view->window, wauthor);
922 while (col <= avail && author_width < author_display_cols + 1) {
923 waddch(view->window, ' ');
930 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
931 if (logmsg0 == NULL) {
932 err = got_error_from_errno();
936 while (*logmsg == '\n')
938 newline = strchr(logmsg, '\n');
942 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
945 waddwstr(view->window, wlogmsg);
947 while (col <= avail) {
948 waddch(view->window, ' ');
960 static struct commit_queue_entry *
961 alloc_commit_queue_entry(struct got_commit_object *commit,
962 struct got_object_id *id)
964 struct commit_queue_entry *entry;
966 entry = calloc(1, sizeof(*entry));
971 entry->commit = commit;
976 pop_commit(struct commit_queue *commits)
978 struct commit_queue_entry *entry;
980 entry = TAILQ_FIRST(&commits->head);
981 TAILQ_REMOVE(&commits->head, entry, entry);
982 got_object_commit_close(entry->commit);
984 /* Don't free entry->id! It is owned by the commit graph. */
989 free_commits(struct commit_queue *commits)
991 while (!TAILQ_EMPTY(&commits->head))
995 static const struct got_error *
996 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
997 int minqueue, struct got_repository *repo, const char *path)
999 const struct got_error *err = NULL;
1003 * We keep all commits open throughout the lifetime of the log
1004 * view in order to avoid having to re-fetch commits from disk
1005 * while updating the display.
1007 while (nqueued < minqueue) {
1008 struct got_object_id *id;
1009 struct got_commit_object *commit;
1010 struct commit_queue_entry *entry;
1013 err = got_commit_graph_iter_next(&id, graph);
1015 if (err->code != GOT_ERR_ITER_NEED_MORE)
1017 err = got_commit_graph_fetch_commits(graph,
1027 err = got_object_open_as_commit(&commit, repo, id);
1030 entry = alloc_commit_queue_entry(commit, id);
1031 if (entry == NULL) {
1032 err = got_error_from_errno();
1036 errcode = pthread_mutex_lock(&tog_mutex);
1038 err = got_error_set_errno(errcode);
1042 entry->idx = commits->ncommits;
1043 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1045 commits->ncommits++;
1047 errcode = pthread_mutex_unlock(&tog_mutex);
1048 if (errcode && err == NULL)
1049 err = got_error_set_errno(errcode);
1055 static const struct got_error *
1056 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1058 const struct got_error *err = NULL;
1059 struct got_reference *head_ref;
1063 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1067 err = got_ref_resolve(head_id, repo, head_ref);
1068 got_ref_close(head_ref);
1077 static const struct got_error *
1078 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1079 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1080 struct commit_queue *commits, int selected_idx, int limit,
1081 const char *path, int commits_needed)
1083 const struct got_error *err = NULL;
1084 struct commit_queue_entry *entry;
1085 int ncommits, width;
1086 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1092 if (ncommits == selected_idx) {
1096 entry = TAILQ_NEXT(entry, entry);
1101 err = got_object_id_str(&id_str, (*selected)->id);
1106 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1107 entry ? entry->idx + 1 : 0, commits->ncommits,
1108 commits_needed == 0 ? "" : " loading...") == -1)
1109 return got_error_from_errno();
1111 if (path && strcmp(path, "/") != 0) {
1112 if (asprintf(&header, "commit %s %s%s",
1113 id_str ? id_str : "........................................",
1114 path, ncommits_str) == -1) {
1115 err = got_error_from_errno();
1119 } else if (asprintf(&header, "commit %s%s",
1120 id_str ? id_str : "........................................",
1121 ncommits_str) == -1) {
1122 err = got_error_from_errno();
1126 err = format_line(&wline, &width, header, view->ncols);
1130 werase(view->window);
1132 if (view_needs_focus_indication(view))
1133 wstandout(view->window);
1134 waddwstr(view->window, wline);
1135 while (width < view->ncols) {
1136 waddch(view->window, ' ');
1139 if (view_needs_focus_indication(view))
1140 wstandend(view->window);
1149 if (ncommits >= limit - 1)
1151 if (view->focussed && ncommits == selected_idx)
1152 wstandout(view->window);
1153 err = draw_commit(view, entry->commit, entry->id);
1154 if (view->focussed && ncommits == selected_idx)
1155 wstandend(view->window);
1160 entry = TAILQ_NEXT(entry, entry);
1172 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1173 struct commit_queue *commits)
1175 struct commit_queue_entry *entry;
1178 entry = TAILQ_FIRST(&commits->head);
1179 if (*first_displayed_entry == entry)
1182 entry = *first_displayed_entry;
1183 while (entry && nscrolled < maxscroll) {
1184 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1186 *first_displayed_entry = entry;
1192 static const struct got_error *
1193 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1194 struct commit_queue_entry **last_displayed_entry,
1195 struct commit_queue *commits, int *log_complete, int *commits_needed,
1196 pthread_cond_t *need_commits)
1198 const struct got_error *err = NULL;
1199 struct commit_queue_entry *pentry;
1202 if (*last_displayed_entry == NULL)
1206 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1207 if (pentry == NULL) {
1208 *commits_needed = maxscroll + 20;
1209 while (*commits_needed > 0) {
1211 errcode = pthread_cond_signal(need_commits);
1213 return got_error_set_errno(errcode);
1214 errcode = pthread_mutex_unlock(&tog_mutex);
1216 return got_error_set_errno(errcode);
1218 errcode = pthread_mutex_lock(&tog_mutex);
1220 return got_error_set_errno(errcode);
1223 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1227 *last_displayed_entry = pentry;
1229 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1232 *first_displayed_entry = pentry;
1233 } while (++nscrolled < maxscroll);
1238 static const struct got_error *
1239 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1240 struct got_commit_object *commit, struct got_object_id *commit_id,
1241 struct tog_view *log_view, struct got_repository *repo)
1243 const struct got_error *err;
1244 struct got_object_qid *parent_id;
1245 struct tog_view *diff_view;
1247 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1248 if (diff_view == NULL)
1249 return got_error_from_errno();
1251 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1252 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1253 commit_id, log_view, repo);
1255 *new_view = diff_view;
1259 static const struct got_error *
1260 browse_commit(struct tog_view **new_view, int begin_x,
1261 struct commit_queue_entry *entry, struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 struct got_tree_object *tree;
1265 struct tog_view *tree_view;
1267 err = got_object_open_as_tree(&tree, repo,
1268 got_object_commit_get_tree_id(entry->commit));
1272 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1273 if (tree_view == NULL)
1274 return got_error_from_errno();
1276 err = open_tree_view(tree_view, tree, entry->id, repo);
1278 got_object_tree_close(tree);
1280 *new_view = tree_view;
1285 log_thread(void *arg)
1287 const struct got_error *err = NULL;
1289 struct tog_log_thread_args *a = arg;
1292 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1296 while (!done && !err) {
1297 err = queue_commits(a->graph, a->commits, 1, a->repo,
1300 if (err->code != GOT_ERR_ITER_COMPLETED)
1304 } else if (a->commits_needed > 0)
1305 a->commits_needed--;
1307 errcode = pthread_mutex_lock(&tog_mutex);
1309 return (void *)got_error_set_errno(errcode);
1312 a->log_complete = 1;
1313 else if (*a->quit) {
1315 a->log_complete = 1;
1316 } else if (*a->first_displayed_entry == NULL) {
1317 *a->first_displayed_entry =
1318 TAILQ_FIRST(&a->commits->head);
1319 *a->selected_entry = *a->first_displayed_entry;
1323 a->commits_needed = 0;
1324 else if (a->commits_needed == 0) {
1325 errcode = pthread_cond_wait(&a->need_commits,
1328 err = got_error_set_errno(errcode);
1331 errcode = pthread_mutex_unlock(&tog_mutex);
1332 if (errcode && err == NULL)
1333 err = got_error_set_errno(errcode);
1338 static const struct got_error *
1339 stop_log_thread(struct tog_log_view_state *s)
1341 const struct got_error *err = NULL;
1346 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1348 return got_error_set_errno(errcode);
1349 errcode = pthread_mutex_unlock(&tog_mutex);
1351 return got_error_set_errno(errcode);
1352 errcode = pthread_join(s->thread, (void **)&err);
1354 return got_error_set_errno(errcode);
1355 errcode = pthread_mutex_lock(&tog_mutex);
1357 return got_error_set_errno(errcode);
1361 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1362 if (errcode && err == NULL)
1363 err = got_error_set_errno(errcode);
1365 if (s->thread_args.repo) {
1366 got_repo_close(s->thread_args.repo);
1367 s->thread_args.repo = NULL;
1370 if (s->thread_args.graph) {
1371 got_commit_graph_close(s->thread_args.graph);
1372 s->thread_args.graph = NULL;
1378 static const struct got_error *
1379 close_log_view(struct tog_view *view)
1381 const struct got_error *err = NULL;
1382 struct tog_log_view_state *s = &view->state.log;
1384 err = stop_log_thread(s);
1385 free_commits(&s->commits);
1386 free(s->in_repo_path);
1387 s->in_repo_path = NULL;
1393 static const struct got_error *
1394 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1395 struct got_repository *repo, const char *path, int check_disk)
1397 const struct got_error *err = NULL;
1398 struct tog_log_view_state *s = &view->state.log;
1399 struct got_repository *thread_repo = NULL;
1400 struct got_commit_graph *thread_graph = NULL;
1403 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1407 /* The commit queue only contains commits being displayed. */
1408 TAILQ_INIT(&s->commits.head);
1409 s->commits.ncommits = 0;
1412 s->start_id = got_object_id_dup(start_id);
1413 if (s->start_id == NULL) {
1414 err = got_error_from_errno();
1418 view->show = show_log_view;
1419 view->input = input_log_view;
1420 view->close = close_log_view;
1422 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1425 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1430 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1432 err = got_error_set_errno(errcode);
1436 s->thread_args.commits_needed = view->nlines;
1437 s->thread_args.graph = thread_graph;
1438 s->thread_args.commits = &s->commits;
1439 s->thread_args.in_repo_path = s->in_repo_path;
1440 s->thread_args.start_id = s->start_id;
1441 s->thread_args.repo = thread_repo;
1442 s->thread_args.log_complete = 0;
1443 s->thread_args.quit = &s->quit;
1444 s->thread_args.view = view;
1445 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1446 s->thread_args.selected_entry = &s->selected_entry;
1449 close_log_view(view);
1453 static const struct got_error *
1454 show_log_view(struct tog_view *view)
1456 struct tog_log_view_state *s = &view->state.log;
1458 if (s->thread == NULL) {
1459 int errcode = pthread_create(&s->thread, NULL, log_thread,
1462 return got_error_set_errno(errcode);
1465 return draw_commits(view, &s->last_displayed_entry,
1466 &s->selected_entry, s->first_displayed_entry,
1467 &s->commits, s->selected, view->nlines,
1468 s->in_repo_path, s->thread_args.commits_needed);
1471 static const struct got_error *
1472 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1473 struct tog_view **focus_view, struct tog_view *view, int ch)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1478 struct tog_view *diff_view = NULL, *tree_view = NULL;
1489 if (s->first_displayed_entry == NULL)
1491 if (s->selected > 0)
1493 if (s->selected > 0)
1495 scroll_up(&s->first_displayed_entry, 1,
1499 if (s->first_displayed_entry == NULL)
1501 if (TAILQ_FIRST(&s->commits.head) ==
1502 s->first_displayed_entry) {
1506 scroll_up(&s->first_displayed_entry,
1507 view->nlines, &s->commits);
1513 if (s->first_displayed_entry == NULL)
1515 if (s->selected < MIN(view->nlines - 2,
1516 s->commits.ncommits - 1)) {
1520 err = scroll_down(&s->first_displayed_entry, 1,
1521 &s->last_displayed_entry, &s->commits,
1522 &s->thread_args.log_complete,
1523 &s->thread_args.commits_needed,
1524 &s->thread_args.need_commits);
1527 struct commit_queue_entry *first;
1528 first = s->first_displayed_entry;
1531 err = scroll_down(&s->first_displayed_entry,
1532 view->nlines, &s->last_displayed_entry,
1533 &s->commits, &s->thread_args.log_complete,
1534 &s->thread_args.commits_needed,
1535 &s->thread_args.need_commits);
1536 if (first == s->first_displayed_entry &&
1537 s->selected < MIN(view->nlines - 2,
1538 s->commits.ncommits - 1)) {
1539 /* can't scroll further down */
1540 s->selected = MIN(view->nlines - 2,
1541 s->commits.ncommits - 1);
1547 if (s->selected > view->nlines - 2)
1548 s->selected = view->nlines - 2;
1549 if (s->selected > s->commits.ncommits - 1)
1550 s->selected = s->commits.ncommits - 1;
1554 if (s->selected_entry == NULL)
1556 if (view_is_parent_view(view))
1557 begin_x = view_split_begin_x(view->begin_x);
1558 err = open_diff_view_for_commit(&diff_view, begin_x,
1559 s->selected_entry->commit, s->selected_entry->id,
1563 if (view_is_parent_view(view)) {
1564 err = view_close_child(view);
1567 err = view_set_child(view, diff_view);
1569 view_close(diff_view);
1572 *focus_view = diff_view;
1573 view->child_focussed = 1;
1575 *new_view = diff_view;
1578 if (s->selected_entry == NULL)
1580 if (view_is_parent_view(view))
1581 begin_x = view_split_begin_x(view->begin_x);
1582 err = browse_commit(&tree_view, begin_x,
1583 s->selected_entry, s->repo);
1586 if (view_is_parent_view(view)) {
1587 err = view_close_child(view);
1590 err = view_set_child(view, tree_view);
1592 view_close(tree_view);
1595 *focus_view = tree_view;
1596 view->child_focussed = 1;
1598 *new_view = tree_view;
1601 if (strcmp(s->in_repo_path, "/") == 0)
1603 parent_path = dirname(s->in_repo_path);
1604 if (parent_path && strcmp(parent_path, ".") != 0) {
1605 struct tog_view *lv;
1606 err = stop_log_thread(s);
1609 lv = view_open(view->nlines, view->ncols,
1610 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1612 return got_error_from_errno();
1613 err = open_log_view(lv, s->start_id, s->repo,
1617 if (view_is_parent_view(view))
1620 view_set_child(view->parent, lv);
1633 static const struct got_error *
1634 apply_unveil(const char *repo_path, const char *worktree_path)
1636 const struct got_error *error;
1638 if (repo_path && unveil(repo_path, "r") != 0)
1639 return got_error_from_errno();
1641 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1642 return got_error_from_errno();
1644 if (unveil("/tmp", "rwc") != 0)
1645 return got_error_from_errno();
1647 error = got_privsep_unveil_exec_helpers();
1651 if (unveil(NULL, NULL) != 0)
1652 return got_error_from_errno();
1662 halfdelay(1); /* Do fast refresh while initial view is loading. */
1665 intrflush(stdscr, FALSE);
1666 keypad(stdscr, TRUE);
1668 signal(SIGWINCH, tog_sigwinch);
1671 static const struct got_error *
1672 cmd_log(int argc, char *argv[])
1674 const struct got_error *error;
1675 struct got_repository *repo = NULL;
1676 struct got_object_id *start_id = NULL;
1677 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1678 char *start_commit = NULL;
1680 struct tog_view *view;
1683 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1688 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1691 start_commit = optarg;
1694 repo_path = realpath(optarg, NULL);
1695 if (repo_path == NULL)
1696 err(1, "-r option");
1710 path = strdup(argv[0]);
1714 return got_error_from_errno();
1716 cwd = getcwd(NULL, 0);
1718 error = got_error_from_errno();
1721 if (repo_path == NULL) {
1722 struct got_worktree *worktree;
1723 error = got_worktree_open(&worktree, cwd);
1724 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1728 strdup(got_worktree_get_repo_path(worktree));
1729 got_worktree_close(worktree);
1731 repo_path = strdup(cwd);
1732 if (repo_path == NULL) {
1733 error = got_error_from_errno();
1740 error = apply_unveil(repo_path, NULL);
1744 error = got_repo_open(&repo, repo_path);
1748 if (start_commit == NULL)
1749 error = get_head_commit_id(&start_id, repo);
1751 error = got_object_resolve_id_str(&start_id, repo,
1756 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1758 error = got_error_from_errno();
1761 error = open_log_view(view, start_id, repo, path, 1);
1764 error = view_loop(view);
1771 got_repo_close(repo);
1779 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1785 parse_next_line(FILE *f, size_t *len)
1790 const char delim[3] = { '\0', '\0', '\0'};
1792 line = fparseln(f, &linelen, &lineno, delim, 0);
1798 static const struct got_error *
1799 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1800 int *last_displayed_line, int *eof, int max_lines,
1803 const struct got_error *err;
1804 int nlines = 0, nprinted = 0;
1811 werase(view->window);
1814 err = format_line(&wline, &width, header, view->ncols);
1819 if (view_needs_focus_indication(view))
1820 wstandout(view->window);
1821 waddwstr(view->window, wline);
1822 if (view_needs_focus_indication(view))
1823 wstandend(view->window);
1824 if (width < view->ncols)
1825 waddch(view->window, '\n');
1833 while (nprinted < max_lines) {
1834 line = parse_next_line(f, &len);
1839 if (++nlines < *first_displayed_line) {
1844 err = format_line(&wline, &width, line, view->ncols);
1849 waddwstr(view->window, wline);
1850 if (width < view->ncols)
1851 waddch(view->window, '\n');
1852 if (++nprinted == 1)
1853 *first_displayed_line = nlines;
1858 *last_displayed_line = nlines;
1866 get_datestr(time_t *time, char *datebuf)
1868 char *p, *s = ctime_r(time, datebuf);
1869 p = strchr(s, '\n');
1875 static const struct got_error *
1876 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1879 const struct got_error *err = NULL;
1881 struct got_commit_object *commit;
1882 char *id_str = NULL;
1883 time_t committer_time;
1884 const char *author, *committer;
1886 err = got_object_open_as_commit(&commit, repo, commit_id);
1890 err = got_object_id_str(&id_str, commit_id);
1892 err = got_error_from_errno();
1896 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1897 err = got_error_from_errno();
1900 if (fprintf(outfile, "from: %s\n",
1901 got_object_commit_get_author(commit)) < 0) {
1902 err = got_error_from_errno();
1905 committer_time = got_object_commit_get_committer_time(commit);
1906 if (fprintf(outfile, "date: %s UTC\n",
1907 get_datestr(&committer_time, datebuf)) < 0) {
1908 err = got_error_from_errno();
1911 author = got_object_commit_get_author(commit);
1912 committer = got_object_commit_get_committer(commit);
1913 if (strcmp(author, committer) != 0 &&
1914 fprintf(outfile, "via: %s\n", committer) < 0) {
1915 err = got_error_from_errno();
1918 if (fprintf(outfile, "%s\n",
1919 got_object_commit_get_logmsg(commit)) < 0) {
1920 err = got_error_from_errno();
1925 got_object_commit_close(commit);
1929 static const struct got_error *
1930 create_diff(struct tog_diff_view_state *s)
1932 const struct got_error *err = NULL;
1938 err = got_error_from_errno();
1941 if (s->f && fclose(s->f) != 0) {
1942 err = got_error_from_errno();
1948 err = got_object_get_type(&obj_type, s->repo, s->id1);
1950 err = got_object_get_type(&obj_type, s->repo, s->id2);
1955 case GOT_OBJ_TYPE_BLOB:
1956 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1957 s->diff_context, s->repo, f);
1959 case GOT_OBJ_TYPE_TREE:
1960 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1961 s->diff_context, s->repo, f);
1963 case GOT_OBJ_TYPE_COMMIT: {
1964 const struct got_object_id_queue *parent_ids;
1965 struct got_object_qid *pid;
1966 struct got_commit_object *commit2;
1968 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1971 /* Show commit info if we're diffing to a parent/root commit. */
1973 write_commit_info(s->id2, s->repo, f);
1975 parent_ids = got_object_commit_get_parent_ids(commit2);
1976 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1977 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1978 write_commit_info(s->id2, s->repo, f);
1983 got_object_commit_close(commit2);
1985 err = got_diff_objects_as_commits(s->id1, s->id2,
1986 s->diff_context, s->repo, f);
1990 err = got_error(GOT_ERR_OBJ_TYPE);
1994 if (f && fflush(f) != 0 && err == NULL)
1995 err = got_error_from_errno();
1999 static const struct got_error *
2000 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2001 struct got_object_id *id2, struct tog_view *log_view,
2002 struct got_repository *repo)
2004 const struct got_error *err;
2006 if (id1 != NULL && id2 != NULL) {
2008 err = got_object_get_type(&type1, repo, id1);
2011 err = got_object_get_type(&type2, repo, id2);
2016 return got_error(GOT_ERR_OBJ_TYPE);
2020 view->state.diff.id1 = got_object_id_dup(id1);
2021 if (view->state.diff.id1 == NULL)
2022 return got_error_from_errno();
2024 view->state.diff.id1 = NULL;
2026 view->state.diff.id2 = got_object_id_dup(id2);
2027 if (view->state.diff.id2 == NULL) {
2028 free(view->state.diff.id1);
2029 view->state.diff.id1 = NULL;
2030 return got_error_from_errno();
2032 view->state.diff.f = NULL;
2033 view->state.diff.first_displayed_line = 1;
2034 view->state.diff.last_displayed_line = view->nlines;
2035 view->state.diff.diff_context = 3;
2036 view->state.diff.log_view = log_view;
2037 view->state.diff.repo = repo;
2039 err = create_diff(&view->state.diff);
2041 free(view->state.diff.id1);
2042 view->state.diff.id1 = NULL;
2043 free(view->state.diff.id2);
2044 view->state.diff.id2 = NULL;
2048 view->show = show_diff_view;
2049 view->input = input_diff_view;
2050 view->close = close_diff_view;
2055 static const struct got_error *
2056 close_diff_view(struct tog_view *view)
2058 const struct got_error *err = NULL;
2060 free(view->state.diff.id1);
2061 view->state.diff.id1 = NULL;
2062 free(view->state.diff.id2);
2063 view->state.diff.id2 = NULL;
2064 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2065 err = got_error_from_errno();
2069 static const struct got_error *
2070 show_diff_view(struct tog_view *view)
2072 const struct got_error *err;
2073 struct tog_diff_view_state *s = &view->state.diff;
2074 char *id_str1 = NULL, *id_str2, *header;
2077 err = got_object_id_str(&id_str1, s->id1);
2081 err = got_object_id_str(&id_str2, s->id2);
2085 if (asprintf(&header, "diff %s %s",
2086 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2087 err = got_error_from_errno();
2095 return draw_file(view, s->f, &s->first_displayed_line,
2096 &s->last_displayed_line, &s->eof, view->nlines,
2100 static const struct got_error *
2101 set_selected_commit(struct tog_diff_view_state *s,
2102 struct commit_queue_entry *entry)
2104 const struct got_error *err;
2105 const struct got_object_id_queue *parent_ids;
2106 struct got_commit_object *selected_commit;
2107 struct got_object_qid *pid;
2110 s->id2 = got_object_id_dup(entry->id);
2112 return got_error_from_errno();
2114 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2117 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2119 pid = SIMPLEQ_FIRST(parent_ids);
2120 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2125 static const struct got_error *
2126 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2127 struct tog_view **focus_view, struct tog_view *view, int ch)
2129 const struct got_error *err = NULL;
2130 struct tog_diff_view_state *s = &view->state.diff;
2131 struct tog_log_view_state *ls;
2132 struct commit_queue_entry *entry;
2138 if (s->first_displayed_line > 1)
2139 s->first_displayed_line--;
2143 while (i++ < view->nlines - 1 &&
2144 s->first_displayed_line > 1)
2145 s->first_displayed_line--;
2150 s->first_displayed_line++;
2155 while (!s->eof && i++ < view->nlines - 1) {
2157 line = parse_next_line(s->f, NULL);
2158 s->first_displayed_line++;
2164 if (s->diff_context > 0) {
2166 err = create_diff(s);
2170 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2172 err = create_diff(s);
2177 if (s->log_view == NULL)
2179 ls = &s->log_view->state.log;
2180 entry = TAILQ_PREV(ls->selected_entry,
2181 commit_queue_head, entry);
2185 err = input_log_view(NULL, NULL, NULL, s->log_view,
2190 err = set_selected_commit(s, entry);
2194 s->first_displayed_line = 1;
2195 s->last_displayed_line = view->nlines;
2197 err = create_diff(s);
2201 if (s->log_view == NULL)
2203 ls = &s->log_view->state.log;
2204 err = input_log_view(NULL, NULL, NULL, s->log_view,
2209 /* Hack: Ensure two commits get loaded. */
2210 if (!ls->thread_args.log_complete) {
2211 err = input_log_view(NULL, NULL, NULL,
2212 s->log_view, KEY_DOWN);
2215 err = input_log_view(NULL, NULL, NULL,
2216 s->log_view, KEY_UP);
2221 entry = TAILQ_NEXT(ls->selected_entry, entry);
2225 err = set_selected_commit(s, entry);
2229 s->first_displayed_line = 1;
2230 s->last_displayed_line = view->nlines;
2232 err = create_diff(s);
2241 static const struct got_error *
2242 cmd_diff(int argc, char *argv[])
2244 const struct got_error *error = NULL;
2245 struct got_repository *repo = NULL;
2246 struct got_object_id *id1 = NULL, *id2 = NULL;
2247 char *repo_path = NULL;
2248 char *id_str1 = NULL, *id_str2 = NULL;
2250 struct tog_view *view;
2253 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2258 while ((ch = getopt(argc, argv, "")) != -1) {
2270 usage_diff(); /* TODO show local worktree changes */
2271 } else if (argc == 2) {
2272 repo_path = getcwd(NULL, 0);
2273 if (repo_path == NULL)
2274 return got_error_from_errno();
2277 } else if (argc == 3) {
2278 repo_path = realpath(argv[0], NULL);
2279 if (repo_path == NULL)
2280 return got_error_from_errno();
2288 error = apply_unveil(repo_path, NULL);
2292 error = got_repo_open(&repo, repo_path);
2297 error = got_object_resolve_id_str(&id1, repo, id_str1);
2301 error = got_object_resolve_id_str(&id2, repo, id_str2);
2305 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2307 error = got_error_from_errno();
2310 error = open_diff_view(view, id1, id2, NULL, repo);
2313 error = view_loop(view);
2315 got_repo_close(repo);
2323 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2328 struct tog_blame_line {
2330 struct got_object_id *id;
2333 static const struct got_error *
2334 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2335 const char *path, struct tog_blame_line *lines, int nlines,
2336 int blame_complete, int selected_line, int *first_displayed_line,
2337 int *last_displayed_line, int *eof, int max_lines)
2339 const struct got_error *err;
2340 int lineno = 0, nprinted = 0;
2345 struct tog_blame_line *blame_line;
2346 struct got_object_id *prev_id = NULL;
2349 err = got_object_id_str(&id_str, id);
2354 werase(view->window);
2356 if (asprintf(&line, "commit %s", id_str) == -1) {
2357 err = got_error_from_errno();
2362 err = format_line(&wline, &width, line, view->ncols);
2365 if (view_needs_focus_indication(view))
2366 wstandout(view->window);
2367 waddwstr(view->window, wline);
2368 if (view_needs_focus_indication(view))
2369 wstandend(view->window);
2372 if (width < view->ncols)
2373 waddch(view->window, '\n');
2375 if (asprintf(&line, "[%d/%d] %s%s",
2376 *first_displayed_line - 1 + selected_line, nlines,
2377 blame_complete ? "" : "annotating ", path) == -1) {
2379 return got_error_from_errno();
2382 err = format_line(&wline, &width, line, view->ncols);
2387 waddwstr(view->window, wline);
2390 if (width < view->ncols)
2391 waddch(view->window, '\n');
2394 while (nprinted < max_lines - 2) {
2395 line = parse_next_line(f, &len);
2400 if (++lineno < *first_displayed_line) {
2405 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2406 err = format_line(&wline, &width, line, wlimit);
2412 if (view->focussed && nprinted == selected_line - 1)
2413 wstandout(view->window);
2415 blame_line = &lines[lineno - 1];
2416 if (blame_line->annotated && prev_id &&
2417 got_object_id_cmp(prev_id, blame_line->id) == 0)
2418 waddstr(view->window, " ");
2419 else if (blame_line->annotated) {
2421 err = got_object_id_str(&id_str, blame_line->id);
2427 wprintw(view->window, "%.8s ", id_str);
2429 prev_id = blame_line->id;
2431 waddstr(view->window, "........ ");
2435 waddwstr(view->window, wline);
2436 while (width < wlimit) {
2437 waddch(view->window, ' ');
2440 if (view->focussed && nprinted == selected_line - 1)
2441 wstandend(view->window);
2442 if (++nprinted == 1)
2443 *first_displayed_line = lineno;
2448 *last_displayed_line = lineno;
2455 static const struct got_error *
2456 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2458 const struct got_error *err = NULL;
2459 struct tog_blame_cb_args *a = arg;
2460 struct tog_blame_line *line;
2463 if (nlines != a->nlines ||
2464 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2465 return got_error(GOT_ERR_RANGE);
2467 errcode = pthread_mutex_lock(&tog_mutex);
2469 return got_error_set_errno(errcode);
2471 if (*a->quit) { /* user has quit the blame view */
2472 err = got_error(GOT_ERR_ITER_COMPLETED);
2477 goto done; /* no change in this commit */
2479 line = &a->lines[lineno - 1];
2480 if (line->annotated)
2483 line->id = got_object_id_dup(id);
2484 if (line->id == NULL) {
2485 err = got_error_from_errno();
2488 line->annotated = 1;
2490 errcode = pthread_mutex_unlock(&tog_mutex);
2492 err = got_error_set_errno(errcode);
2497 blame_thread(void *arg)
2499 const struct got_error *err;
2500 struct tog_blame_thread_args *ta = arg;
2501 struct tog_blame_cb_args *a = ta->cb_args;
2504 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2505 blame_cb, ta->cb_args);
2507 errcode = pthread_mutex_lock(&tog_mutex);
2509 return (void *)got_error_set_errno(errcode);
2511 got_repo_close(ta->repo);
2515 errcode = pthread_mutex_unlock(&tog_mutex);
2516 if (errcode && err == NULL)
2517 err = got_error_set_errno(errcode);
2522 static struct got_object_id *
2523 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2526 struct tog_blame_line *line;
2528 line = &lines[first_displayed_line - 1 + selected_line - 1];
2529 if (!line->annotated)
2535 static const struct got_error *
2536 stop_blame(struct tog_blame *blame)
2538 const struct got_error *err = NULL;
2541 if (blame->thread) {
2543 errcode = pthread_mutex_unlock(&tog_mutex);
2545 return got_error_set_errno(errcode);
2546 errcode = pthread_join(blame->thread, (void **)&err);
2548 return got_error_set_errno(errcode);
2549 errcode = pthread_mutex_lock(&tog_mutex);
2551 return got_error_set_errno(errcode);
2552 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2554 blame->thread = NULL;
2556 if (blame->thread_args.repo) {
2557 got_repo_close(blame->thread_args.repo);
2558 blame->thread_args.repo = NULL;
2561 if (fclose(blame->f) != 0 && err == NULL)
2562 err = got_error_from_errno();
2566 for (i = 0; i < blame->nlines; i++)
2567 free(blame->lines[i].id);
2569 blame->lines = NULL;
2571 free(blame->cb_args.commit_id);
2572 blame->cb_args.commit_id = NULL;
2577 static const struct got_error *
2578 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2579 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2580 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2581 struct got_repository *repo)
2583 const struct got_error *err = NULL;
2584 struct got_blob_object *blob = NULL;
2585 struct got_repository *thread_repo = NULL;
2586 struct got_object_id *obj_id = NULL;
2589 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2593 return got_error(GOT_ERR_NO_OBJ);
2595 err = got_object_get_type(&obj_type, repo, obj_id);
2599 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2600 err = got_error(GOT_ERR_OBJ_TYPE);
2604 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2607 blame->f = got_opentemp();
2608 if (blame->f == NULL) {
2609 err = got_error_from_errno();
2612 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2617 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2618 if (blame->lines == NULL) {
2619 err = got_error_from_errno();
2623 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2627 blame->cb_args.view = view;
2628 blame->cb_args.lines = blame->lines;
2629 blame->cb_args.nlines = blame->nlines;
2630 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2631 if (blame->cb_args.commit_id == NULL) {
2632 err = got_error_from_errno();
2635 blame->cb_args.quit = done;
2637 blame->thread_args.path = path;
2638 blame->thread_args.repo = thread_repo;
2639 blame->thread_args.cb_args = &blame->cb_args;
2640 blame->thread_args.complete = blame_complete;
2641 *blame_complete = 0;
2645 got_object_blob_close(blob);
2652 static const struct got_error *
2653 open_blame_view(struct tog_view *view, char *path,
2654 struct got_object_id *commit_id, struct got_repository *repo)
2656 const struct got_error *err = NULL;
2657 struct tog_blame_view_state *s = &view->state.blame;
2659 SIMPLEQ_INIT(&s->blamed_commits);
2661 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2665 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2666 s->first_displayed_line = 1;
2667 s->last_displayed_line = view->nlines;
2668 s->selected_line = 1;
2669 s->blame_complete = 0;
2671 if (s->path == NULL)
2672 return got_error_from_errno();
2674 s->commit_id = commit_id;
2675 memset(&s->blame, 0, sizeof(s->blame));
2677 view->show = show_blame_view;
2678 view->input = input_blame_view;
2679 view->close = close_blame_view;
2681 return run_blame(&s->blame, view, &s->blame_complete,
2682 &s->first_displayed_line, &s->last_displayed_line,
2683 &s->selected_line, &s->done, &s->eof, s->path,
2684 s->blamed_commit->id, s->repo);
2687 static const struct got_error *
2688 close_blame_view(struct tog_view *view)
2690 const struct got_error *err = NULL;
2691 struct tog_blame_view_state *s = &view->state.blame;
2693 if (s->blame.thread)
2694 err = stop_blame(&s->blame);
2696 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2697 struct got_object_qid *blamed_commit;
2698 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2699 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2700 got_object_qid_free(blamed_commit);
2708 static const struct got_error *
2709 show_blame_view(struct tog_view *view)
2711 const struct got_error *err = NULL;
2712 struct tog_blame_view_state *s = &view->state.blame;
2715 if (s->blame.thread == NULL) {
2716 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2717 &s->blame.thread_args);
2719 return got_error_set_errno(errcode);
2722 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2723 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2724 s->selected_line, &s->first_displayed_line,
2725 &s->last_displayed_line, &s->eof, view->nlines);
2731 static const struct got_error *
2732 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2733 struct tog_view **focus_view, struct tog_view *view, int ch)
2735 const struct got_error *err = NULL, *thread_err = NULL;
2736 struct tog_view *diff_view;
2737 struct tog_blame_view_state *s = &view->state.blame;
2746 if (s->selected_line > 1)
2748 else if (s->selected_line == 1 &&
2749 s->first_displayed_line > 1)
2750 s->first_displayed_line--;
2753 if (s->first_displayed_line == 1) {
2754 s->selected_line = 1;
2757 if (s->first_displayed_line > view->nlines - 2)
2758 s->first_displayed_line -=
2761 s->first_displayed_line = 1;
2765 if (s->selected_line < view->nlines - 2 &&
2766 s->first_displayed_line +
2767 s->selected_line <= s->blame.nlines)
2769 else if (s->last_displayed_line <
2771 s->first_displayed_line++;
2775 struct got_object_id *id = NULL;
2776 id = get_selected_commit_id(s->blame.lines,
2777 s->first_displayed_line, s->selected_line);
2781 struct got_commit_object *commit;
2782 struct got_object_qid *pid;
2783 struct got_object_id *blob_id = NULL;
2785 err = got_object_open_as_commit(&commit,
2789 pid = SIMPLEQ_FIRST(
2790 got_object_commit_get_parent_ids(commit));
2792 got_object_commit_close(commit);
2795 /* Check if path history ends here. */
2796 err = got_object_id_by_path(&blob_id, s->repo,
2799 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2801 got_object_commit_close(commit);
2804 err = got_object_get_type(&obj_type, s->repo,
2807 /* Can't blame non-blob type objects. */
2808 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2809 got_object_commit_close(commit);
2812 err = got_object_qid_alloc(&s->blamed_commit,
2814 got_object_commit_close(commit);
2816 if (got_object_id_cmp(id,
2817 s->blamed_commit->id) == 0)
2819 err = got_object_qid_alloc(&s->blamed_commit,
2825 thread_err = stop_blame(&s->blame);
2829 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2830 s->blamed_commit, entry);
2831 err = run_blame(&s->blame, view, &s->blame_complete,
2832 &s->first_displayed_line, &s->last_displayed_line,
2833 &s->selected_line, &s->done, &s->eof,
2834 s->path, s->blamed_commit->id, s->repo);
2840 struct got_object_qid *first;
2841 first = SIMPLEQ_FIRST(&s->blamed_commits);
2842 if (!got_object_id_cmp(first->id, s->commit_id))
2845 thread_err = stop_blame(&s->blame);
2849 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2850 got_object_qid_free(s->blamed_commit);
2852 SIMPLEQ_FIRST(&s->blamed_commits);
2853 err = run_blame(&s->blame, view, &s->blame_complete,
2854 &s->first_displayed_line, &s->last_displayed_line,
2855 &s->selected_line, &s->done, &s->eof, s->path,
2856 s->blamed_commit->id, s->repo);
2863 struct got_object_id *id = NULL;
2864 struct got_object_qid *pid;
2865 struct got_commit_object *commit = NULL;
2866 id = get_selected_commit_id(s->blame.lines,
2867 s->first_displayed_line, s->selected_line);
2870 err = got_object_open_as_commit(&commit, s->repo, id);
2873 pid = SIMPLEQ_FIRST(
2874 got_object_commit_get_parent_ids(commit));
2875 if (view_is_parent_view(view))
2876 begin_x = view_split_begin_x(view->begin_x);
2877 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2878 if (diff_view == NULL) {
2879 got_object_commit_close(commit);
2880 err = got_error_from_errno();
2883 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2885 got_object_commit_close(commit);
2887 view_close(diff_view);
2890 if (view_is_parent_view(view)) {
2891 err = view_close_child(view);
2894 err = view_set_child(view, diff_view);
2896 view_close(diff_view);
2899 *focus_view = diff_view;
2900 view->child_focussed = 1;
2902 *new_view = diff_view;
2909 if (s->last_displayed_line >= s->blame.nlines &&
2910 s->selected_line < view->nlines - 2) {
2911 s->selected_line = MIN(s->blame.nlines,
2915 if (s->last_displayed_line + view->nlines - 2
2917 s->first_displayed_line +=
2920 s->first_displayed_line =
2925 if (s->selected_line > view->nlines - 2) {
2926 s->selected_line = MIN(s->blame.nlines,
2933 return thread_err ? thread_err : err;
2936 static const struct got_error *
2937 cmd_blame(int argc, char *argv[])
2939 const struct got_error *error;
2940 struct got_repository *repo = NULL;
2941 struct got_worktree *worktree = NULL;
2942 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2943 struct got_object_id *commit_id = NULL;
2944 char *commit_id_str = NULL;
2946 struct tog_view *view;
2949 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2954 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2957 commit_id_str = optarg;
2960 repo_path = realpath(optarg, NULL);
2961 if (repo_path == NULL)
2962 err(1, "-r option");
2978 cwd = getcwd(NULL, 0);
2980 error = got_error_from_errno();
2983 if (repo_path == NULL) {
2984 error = got_worktree_open(&worktree, cwd);
2985 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2991 strdup(got_worktree_get_repo_path(worktree));
2992 if (repo_path == NULL)
2993 error = got_error_from_errno();
2997 repo_path = strdup(cwd);
2998 if (repo_path == NULL) {
2999 error = got_error_from_errno();
3007 error = apply_unveil(repo_path, NULL);
3011 error = got_repo_open(&repo, repo_path);
3016 const char *prefix = got_worktree_get_path_prefix(worktree);
3017 char *p, *worktree_subdir = cwd +
3018 strlen(got_worktree_get_root_path(worktree));
3019 if (asprintf(&p, "%s%s%s%s%s",
3020 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3021 worktree_subdir, worktree_subdir[0] ? "/" : "",
3023 error = got_error_from_errno();
3026 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3029 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3034 if (commit_id_str == NULL) {
3035 struct got_reference *head_ref;
3036 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3039 error = got_ref_resolve(&commit_id, repo, head_ref);
3040 got_ref_close(head_ref);
3042 error = got_object_resolve_id_str(&commit_id, repo,
3048 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3050 error = got_error_from_errno();
3053 error = open_blame_view(view, in_repo_path, commit_id, repo);
3056 error = view_loop(view);
3062 got_worktree_close(worktree);
3064 got_repo_close(repo);
3068 static const struct got_error *
3069 draw_tree_entries(struct tog_view *view,
3070 struct got_tree_entry **first_displayed_entry,
3071 struct got_tree_entry **last_displayed_entry,
3072 struct got_tree_entry **selected_entry, int *ndisplayed,
3073 const char *label, int show_ids, const char *parent_path,
3074 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3076 const struct got_error *err = NULL;
3077 struct got_tree_entry *te;
3083 werase(view->window);
3088 err = format_line(&wline, &width, label, view->ncols);
3091 if (view_needs_focus_indication(view))
3092 wstandout(view->window);
3093 waddwstr(view->window, wline);
3094 if (view_needs_focus_indication(view))
3095 wstandend(view->window);
3098 if (width < view->ncols)
3099 waddch(view->window, '\n');
3102 err = format_line(&wline, &width, parent_path, view->ncols);
3105 waddwstr(view->window, wline);
3108 if (width < view->ncols)
3109 waddch(view->window, '\n');
3112 waddch(view->window, '\n');
3116 te = SIMPLEQ_FIRST(&entries->head);
3117 if (*first_displayed_entry == NULL) {
3118 if (selected == 0) {
3120 wstandout(view->window);
3121 *selected_entry = NULL;
3123 waddstr(view->window, " ..\n"); /* parent directory */
3124 if (selected == 0 && view->focussed)
3125 wstandend(view->window);
3132 while (te != *first_displayed_entry)
3133 te = SIMPLEQ_NEXT(te, entry);
3137 char *line = NULL, *id_str = NULL;
3140 err = got_object_id_str(&id_str, te->id);
3142 return got_error_from_errno();
3144 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3145 te->name, S_ISDIR(te->mode) ? "/" :
3146 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3148 return got_error_from_errno();
3151 err = format_line(&wline, &width, line, view->ncols);
3156 if (n == selected) {
3158 wstandout(view->window);
3159 *selected_entry = te;
3161 waddwstr(view->window, wline);
3162 if (width < view->ncols)
3163 waddch(view->window, '\n');
3164 if (n == selected && view->focussed)
3165 wstandend(view->window);
3171 *last_displayed_entry = te;
3174 te = SIMPLEQ_NEXT(te, entry);
3181 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3182 const struct got_tree_entries *entries, int isroot)
3184 struct got_tree_entry *te, *prev;
3187 if (*first_displayed_entry == NULL)
3190 te = SIMPLEQ_FIRST(&entries->head);
3191 if (*first_displayed_entry == te) {
3193 *first_displayed_entry = NULL;
3197 /* XXX this is stupid... switch to TAILQ? */
3198 for (i = 0; i < maxscroll; i++) {
3199 while (te != *first_displayed_entry) {
3201 te = SIMPLEQ_NEXT(te, entry);
3203 *first_displayed_entry = prev;
3204 te = SIMPLEQ_FIRST(&entries->head);
3206 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3207 *first_displayed_entry = NULL;
3211 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3212 struct got_tree_entry *last_displayed_entry,
3213 const struct got_tree_entries *entries)
3215 struct got_tree_entry *next, *last;
3218 if (*first_displayed_entry)
3219 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3221 next = SIMPLEQ_FIRST(&entries->head);
3222 last = last_displayed_entry;
3223 while (next && last && n++ < maxscroll) {
3224 last = SIMPLEQ_NEXT(last, entry);
3226 *first_displayed_entry = next;
3227 next = SIMPLEQ_NEXT(next, entry);
3233 static const struct got_error *
3234 tree_entry_path(char **path, struct tog_parent_trees *parents,
3235 struct got_tree_entry *te)
3237 const struct got_error *err = NULL;
3238 struct tog_parent_tree *pt;
3239 size_t len = 2; /* for leading slash and NUL */
3241 TAILQ_FOREACH(pt, parents, entry)
3242 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3244 len += strlen(te->name);
3246 *path = calloc(1, len);
3248 return got_error_from_errno();
3251 pt = TAILQ_LAST(parents, tog_parent_trees);
3253 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3254 err = got_error(GOT_ERR_NO_SPACE);
3257 if (strlcat(*path, "/", len) >= len) {
3258 err = got_error(GOT_ERR_NO_SPACE);
3261 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3264 if (strlcat(*path, te->name, len) >= len) {
3265 err = got_error(GOT_ERR_NO_SPACE);
3277 static const struct got_error *
3278 blame_tree_entry(struct tog_view **new_view, int begin_x,
3279 struct got_tree_entry *te, struct tog_parent_trees *parents,
3280 struct got_object_id *commit_id, struct got_repository *repo)
3282 const struct got_error *err = NULL;
3284 struct tog_view *blame_view;
3286 err = tree_entry_path(&path, parents, te);
3290 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3291 if (blame_view == NULL)
3292 return got_error_from_errno();
3294 err = open_blame_view(blame_view, path, commit_id, repo);
3296 view_close(blame_view);
3299 *new_view = blame_view;
3303 static const struct got_error *
3304 log_tree_entry(struct tog_view **new_view, int begin_x,
3305 struct got_tree_entry *te, struct tog_parent_trees *parents,
3306 struct got_object_id *commit_id, struct got_repository *repo)
3308 struct tog_view *log_view;
3309 const struct got_error *err = NULL;
3312 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3313 if (log_view == NULL)
3314 return got_error_from_errno();
3316 err = tree_entry_path(&path, parents, te);
3320 err = open_log_view(log_view, commit_id, repo, path, 0);
3322 view_close(log_view);
3324 *new_view = log_view;
3329 static const struct got_error *
3330 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3331 struct got_object_id *commit_id, struct got_repository *repo)
3333 const struct got_error *err = NULL;
3334 char *commit_id_str = NULL;
3335 struct tog_tree_view_state *s = &view->state.tree;
3337 TAILQ_INIT(&s->parents);
3339 err = got_object_id_str(&commit_id_str, commit_id);
3343 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3344 err = got_error_from_errno();
3348 s->root = s->tree = root;
3349 s->entries = got_object_tree_get_entries(root);
3350 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3351 s->commit_id = got_object_id_dup(commit_id);
3352 if (s->commit_id == NULL) {
3353 err = got_error_from_errno();
3358 view->show = show_tree_view;
3359 view->input = input_tree_view;
3360 view->close = close_tree_view;
3362 free(commit_id_str);
3364 free(s->tree_label);
3365 s->tree_label = NULL;
3370 static const struct got_error *
3371 close_tree_view(struct tog_view *view)
3373 struct tog_tree_view_state *s = &view->state.tree;
3375 free(s->tree_label);
3376 s->tree_label = NULL;
3378 s->commit_id = NULL;
3379 while (!TAILQ_EMPTY(&s->parents)) {
3380 struct tog_parent_tree *parent;
3381 parent = TAILQ_FIRST(&s->parents);
3382 TAILQ_REMOVE(&s->parents, parent, entry);
3386 if (s->tree != s->root)
3387 got_object_tree_close(s->tree);
3388 got_object_tree_close(s->root);
3393 static const struct got_error *
3394 show_tree_view(struct tog_view *view)
3396 const struct got_error *err = NULL;
3397 struct tog_tree_view_state *s = &view->state.tree;
3400 err = tree_entry_path(&parent_path, &s->parents, NULL);
3404 err = draw_tree_entries(view, &s->first_displayed_entry,
3405 &s->last_displayed_entry, &s->selected_entry,
3406 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3407 s->entries, s->selected, view->nlines, s->tree == s->root);
3414 static const struct got_error *
3415 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3416 struct tog_view **focus_view, struct tog_view *view, int ch)
3418 const struct got_error *err = NULL;
3419 struct tog_tree_view_state *s = &view->state.tree;
3420 struct tog_view *log_view;
3421 int begin_x = 0, nscrolled;
3425 s->show_ids = !s->show_ids;
3428 if (!s->selected_entry)
3430 if (view_is_parent_view(view))
3431 begin_x = view_split_begin_x(view->begin_x);
3432 err = log_tree_entry(&log_view, begin_x,
3433 s->selected_entry, &s->parents,
3434 s->commit_id, s->repo);
3435 if (view_is_parent_view(view)) {
3436 err = view_close_child(view);
3439 err = view_set_child(view, log_view);
3441 view_close(log_view);
3444 *focus_view = log_view;
3445 view->child_focussed = 1;
3447 *new_view = log_view;
3451 if (s->selected > 0) {
3453 if (s->selected == 0)
3456 if (s->selected > 0)
3458 tree_scroll_up(&s->first_displayed_entry, 1,
3459 s->entries, s->tree == s->root);
3462 tree_scroll_up(&s->first_displayed_entry,
3463 MAX(0, view->nlines - 4 - s->selected), s->entries,
3464 s->tree == s->root);
3466 if (SIMPLEQ_FIRST(&s->entries->head) ==
3467 s->first_displayed_entry && s->tree != s->root)
3468 s->first_displayed_entry = NULL;
3472 if (s->selected < s->ndisplayed - 1) {
3476 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3478 /* can't scroll any further */
3481 tree_scroll_down(&s->first_displayed_entry, 1,
3482 s->last_displayed_entry, s->entries);
3485 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3487 /* can't scroll any further; move cursor down */
3488 if (s->selected < s->ndisplayed - 1)
3489 s->selected = s->ndisplayed - 1;
3492 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3493 view->nlines, s->last_displayed_entry, s->entries);
3494 if (nscrolled < view->nlines) {
3496 struct got_tree_entry *te;
3497 te = s->first_displayed_entry;
3500 te = SIMPLEQ_NEXT(te, entry);
3502 s->selected = ndisplayed - 1;
3507 if (s->selected_entry == NULL) {
3508 struct tog_parent_tree *parent;
3510 /* user selected '..' */
3511 if (s->tree == s->root)
3513 parent = TAILQ_FIRST(&s->parents);
3514 TAILQ_REMOVE(&s->parents, parent,
3516 got_object_tree_close(s->tree);
3517 s->tree = parent->tree;
3519 got_object_tree_get_entries(s->tree);
3520 s->first_displayed_entry =
3521 parent->first_displayed_entry;
3523 parent->selected_entry;
3524 s->selected = parent->selected;
3526 } else if (S_ISDIR(s->selected_entry->mode)) {
3527 struct tog_parent_tree *parent;
3528 struct got_tree_object *child;
3529 err = got_object_open_as_tree(&child,
3530 s->repo, s->selected_entry->id);
3533 parent = calloc(1, sizeof(*parent));
3534 if (parent == NULL) {
3535 err = got_error_from_errno();
3538 parent->tree = s->tree;
3539 parent->first_displayed_entry =
3540 s->first_displayed_entry;
3541 parent->selected_entry = s->selected_entry;
3542 parent->selected = s->selected;
3543 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3546 got_object_tree_get_entries(s->tree);
3548 s->first_displayed_entry = NULL;
3549 } else if (S_ISREG(s->selected_entry->mode)) {
3550 struct tog_view *blame_view;
3551 int begin_x = view_is_parent_view(view) ?
3552 view_split_begin_x(view->begin_x) : 0;
3554 err = blame_tree_entry(&blame_view, begin_x,
3555 s->selected_entry, &s->parents, s->commit_id,
3559 if (view_is_parent_view(view)) {
3560 err = view_close_child(view);
3563 err = view_set_child(view, blame_view);
3565 view_close(blame_view);
3568 *focus_view = blame_view;
3569 view->child_focussed = 1;
3571 *new_view = blame_view;
3575 if (s->selected > view->nlines)
3576 s->selected = s->ndisplayed - 1;
3589 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3594 static const struct got_error *
3595 cmd_tree(int argc, char *argv[])
3597 const struct got_error *error;
3598 struct got_repository *repo = NULL;
3599 char *repo_path = NULL;
3600 struct got_object_id *commit_id = NULL;
3601 char *commit_id_arg = NULL;
3602 struct got_commit_object *commit = NULL;
3603 struct got_tree_object *tree = NULL;
3605 struct tog_view *view;
3608 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3613 while ((ch = getopt(argc, argv, "c:")) != -1) {
3616 commit_id_arg = optarg;
3628 struct got_worktree *worktree;
3629 char *cwd = getcwd(NULL, 0);
3631 return got_error_from_errno();
3632 error = got_worktree_open(&worktree, cwd);
3633 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3638 strdup(got_worktree_get_repo_path(worktree));
3639 got_worktree_close(worktree);
3642 if (repo_path == NULL) {
3643 error = got_error_from_errno();
3646 } else if (argc == 1) {
3647 repo_path = realpath(argv[0], NULL);
3648 if (repo_path == NULL)
3649 return got_error_from_errno();
3655 error = apply_unveil(repo_path, NULL);
3659 error = got_repo_open(&repo, repo_path);
3663 if (commit_id_arg == NULL)
3664 error = get_head_commit_id(&commit_id, repo);
3666 error = got_object_resolve_id_str(&commit_id, repo,
3671 error = got_object_open_as_commit(&commit, repo, commit_id);
3675 error = got_object_open_as_tree(&tree, repo,
3676 got_object_commit_get_tree_id(commit));
3680 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3682 error = got_error_from_errno();
3685 error = open_tree_view(view, tree, commit_id, repo);
3688 error = view_loop(view);
3693 got_object_commit_close(commit);
3695 got_object_tree_close(tree);
3697 got_repo_close(repo);
3706 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3707 "Available commands:\n", getprogname());
3708 for (i = 0; i < nitems(tog_commands); i++) {
3709 struct tog_cmd *cmd = &tog_commands[i];
3710 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3716 make_argv(const char *arg0, const char *arg1)
3719 int argc = (arg1 == NULL ? 1 : 2);
3721 argv = calloc(argc, sizeof(char *));
3724 argv[0] = strdup(arg0);
3725 if (argv[0] == NULL)
3728 argv[1] = strdup(arg1);
3729 if (argv[1] == NULL)
3737 main(int argc, char *argv[])
3739 const struct got_error *error = NULL;
3740 struct tog_cmd *cmd = NULL;
3742 char **cmd_argv = NULL;
3744 setlocale(LC_CTYPE, "");
3746 while ((ch = getopt(argc, argv, "h")) != -1) {
3765 /* Build an argument vector which runs a default command. */
3766 cmd = &tog_commands[0];
3767 cmd_argv = make_argv(cmd->name, NULL);
3772 /* Did the user specific a command? */
3773 for (i = 0; i < nitems(tog_commands); i++) {
3774 if (strncmp(tog_commands[i].name, argv[0],
3775 strlen(argv[0])) == 0) {
3776 cmd = &tog_commands[i];
3778 tog_commands[i].cmd_usage();
3783 /* Did the user specify a repository? */
3784 char *repo_path = realpath(argv[0], NULL);
3786 struct got_repository *repo;
3787 error = got_repo_open(&repo, repo_path);
3789 got_repo_close(repo);
3791 error = got_error_from_errno();
3794 fprintf(stderr, "%s: '%s' is not a "
3795 "known command\n", getprogname(),
3799 fprintf(stderr, "%s: '%s' is neither a known "
3800 "command nor a path to a repository\n",
3801 getprogname(), argv[0]);
3805 cmd = &tog_commands[0];
3806 cmd_argv = make_argv(cmd->name, repo_path);
3812 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3819 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);