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 tog_diff_view_state {
102 struct got_object_id *id1, *id2;
104 int first_displayed_line;
105 int last_displayed_line;
108 struct got_repository *repo;
111 struct commit_queue_entry {
112 TAILQ_ENTRY(commit_queue_entry) entry;
113 struct got_object_id *id;
114 struct got_commit_object *commit;
117 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
118 struct commit_queue {
120 struct commit_queue_head head;
123 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
125 struct tog_log_thread_args {
126 pthread_cond_t need_commits;
128 struct got_commit_graph *graph;
129 struct commit_queue *commits;
130 const char *in_repo_path;
131 struct got_object_id *start_id;
132 struct got_repository *repo;
135 struct tog_view *view;
136 struct commit_queue_entry **first_displayed_entry;
137 struct commit_queue_entry **selected_entry;
140 struct tog_log_view_state {
141 struct commit_queue commits;
142 struct commit_queue_entry *first_displayed_entry;
143 struct commit_queue_entry *last_displayed_entry;
144 struct commit_queue_entry *selected_entry;
147 struct got_repository *repo;
148 struct got_object_id *start_id;
151 struct tog_log_thread_args thread_args;
154 struct tog_blame_cb_args {
155 struct tog_blame_line *lines; /* one per line */
158 struct tog_view *view;
159 struct got_object_id *commit_id;
163 struct tog_blame_thread_args {
165 struct got_repository *repo;
166 struct tog_blame_cb_args *cb_args;
173 struct tog_blame_line *lines;
176 struct tog_blame_thread_args thread_args;
177 struct tog_blame_cb_args cb_args;
181 struct tog_blame_view_state {
182 int first_displayed_line;
183 int last_displayed_line;
188 struct got_object_id_queue blamed_commits;
189 struct got_object_qid *blamed_commit;
191 struct got_repository *repo;
192 struct got_object_id *commit_id;
193 struct tog_blame blame;
196 struct tog_parent_tree {
197 TAILQ_ENTRY(tog_parent_tree) entry;
198 struct got_tree_object *tree;
199 struct got_tree_entry *first_displayed_entry;
200 struct got_tree_entry *selected_entry;
204 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
206 struct tog_tree_view_state {
208 struct got_tree_object *root;
209 struct got_tree_object *tree;
210 const struct got_tree_entries *entries;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *last_displayed_entry;
213 struct got_tree_entry *selected_entry;
214 int ndisplayed, selected, show_ids;
215 struct tog_parent_trees parents;
216 struct got_object_id *commit_id;
217 struct got_repository *repo;
221 * We implement two types of views: parent views and child views.
223 * The 'Tab' key switches between a parent view and its child view.
224 * Child views are shown side-by-side to their parent view, provided
225 * there is enough screen estate.
227 * When a new view is opened from within a parent view, this new view
228 * becomes a child view of the parent view, replacing any existing child.
230 * When a new view is opened from within a child view, this new view
231 * becomes a parent view which will obscure the views below until the
232 * user quits the new parent view by typing 'q'.
234 * This list of views contains parent views only.
235 * Child views are only pointed to by their parent view.
237 TAILQ_HEAD(tog_view_list_head, tog_view);
240 TAILQ_ENTRY(tog_view) entry;
243 int nlines, ncols, begin_y, begin_x;
244 int lines, cols; /* copies of LINES and COLS */
246 struct tog_view *parent;
247 struct tog_view *child;
250 /* type-specific state */
251 enum tog_view_type type;
253 struct tog_diff_view_state diff;
254 struct tog_log_view_state log;
255 struct tog_blame_view_state blame;
256 struct tog_tree_view_state tree;
259 const struct got_error *(*show)(struct tog_view *);
260 const struct got_error *(*input)(struct tog_view **,
261 struct tog_view **, struct tog_view**, struct tog_view *, int);
262 const struct got_error *(*close)(struct tog_view *);
265 static const struct got_error *open_diff_view(struct tog_view *,
266 struct got_object_id *, struct got_object_id *, struct got_repository *);
267 static const struct got_error *show_diff_view(struct tog_view *);
268 static const struct got_error *input_diff_view(struct tog_view **,
269 struct tog_view **, struct tog_view **, struct tog_view *, int);
270 static const struct got_error* close_diff_view(struct tog_view *);
272 static const struct got_error *open_log_view(struct tog_view *,
273 struct got_object_id *, struct got_repository *, const char *, int);
274 static const struct got_error * show_log_view(struct tog_view *);
275 static const struct got_error *input_log_view(struct tog_view **,
276 struct tog_view **, struct tog_view **, struct tog_view *, int);
277 static const struct got_error *close_log_view(struct tog_view *);
279 static const struct got_error *open_blame_view(struct tog_view *, char *,
280 struct got_object_id *, struct got_repository *);
281 static const struct got_error *show_blame_view(struct tog_view *);
282 static const struct got_error *input_blame_view(struct tog_view **,
283 struct tog_view **, struct tog_view **, struct tog_view *, int);
284 static const struct got_error *close_blame_view(struct tog_view *);
286 static const struct got_error *open_tree_view(struct tog_view *,
287 struct got_tree_object *, struct got_object_id *, struct got_repository *);
288 static const struct got_error *show_tree_view(struct tog_view *);
289 static const struct got_error *input_tree_view(struct tog_view **,
290 struct tog_view **, struct tog_view **, struct tog_view *, int);
291 static const struct got_error *close_tree_view(struct tog_view *);
293 static volatile sig_atomic_t tog_sigwinch_received;
296 tog_sigwinch(int signo)
298 tog_sigwinch_received = 1;
301 static const struct got_error *
302 view_close(struct tog_view *view)
304 const struct got_error *err = NULL;
307 view_close(view->child);
311 err = view->close(view);
313 del_panel(view->panel);
315 delwin(view->window);
320 static struct tog_view *
321 view_open(int nlines, int ncols, int begin_y, int begin_x,
322 enum tog_view_type type)
324 struct tog_view *view = calloc(1, sizeof(*view));
332 view->nlines = nlines ? nlines : LINES - begin_y;
333 view->ncols = ncols ? ncols : COLS - begin_x;
334 view->begin_y = begin_y;
335 view->begin_x = begin_x;
336 view->window = newwin(nlines, ncols, begin_y, begin_x);
337 if (view->window == NULL) {
341 view->panel = new_panel(view->window);
342 if (view->panel == NULL ||
343 set_panel_userptr(view->panel, view) != OK) {
348 keypad(view->window, TRUE);
353 view_split_begin_x(int begin_x)
355 if (begin_x > 0 || COLS < 120)
357 return (COLS - MAX(COLS / 2, 80));
360 static const struct got_error *view_resize(struct tog_view *);
362 static const struct got_error *
363 view_splitscreen(struct tog_view *view)
365 const struct got_error *err = NULL;
368 view->begin_x = view_split_begin_x(0);
369 view->nlines = LINES;
370 view->ncols = COLS - view->begin_x;
373 err = view_resize(view);
377 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
378 return got_error_from_errno();
383 static const struct got_error *
384 view_fullscreen(struct tog_view *view)
386 const struct got_error *err = NULL;
390 view->nlines = LINES;
394 err = view_resize(view);
398 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
399 return got_error_from_errno();
405 view_is_parent_view(struct tog_view *view)
407 return view->parent == NULL;
410 static const struct got_error *
411 view_resize(struct tog_view *view)
415 if (view->lines > LINES)
416 nlines = view->nlines - (view->lines - LINES);
418 nlines = view->nlines + (LINES - view->lines);
420 if (view->cols > COLS)
421 ncols = view->ncols - (view->cols - COLS);
423 ncols = view->ncols + (COLS - view->cols);
425 if (wresize(view->window, nlines, ncols) == ERR)
426 return got_error_from_errno();
427 if (replace_panel(view->panel, view->window) == ERR)
428 return got_error_from_errno();
429 wclear(view->window);
431 view->nlines = nlines;
437 view->child->begin_x = view_split_begin_x(view->begin_x);
438 if (view->child->begin_x == 0) {
439 view_fullscreen(view->child);
440 if (view->child->focussed)
441 show_panel(view->child->panel);
443 show_panel(view->panel);
445 view_splitscreen(view->child);
446 show_panel(view->child->panel);
453 static const struct got_error *
454 view_close_child(struct tog_view *view)
456 const struct got_error *err = NULL;
458 if (view->child == NULL)
461 err = view_close(view->child);
466 static const struct got_error *
467 view_set_child(struct tog_view *view, struct tog_view *child)
469 const struct got_error *err = NULL;
472 child->parent = view;
477 view_is_splitscreen(struct tog_view *view)
479 return !view_is_parent_view(view) && view->begin_x > 0;
488 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
489 cols = 80; /* Default */
495 resize_term(lines, cols);
498 static const struct got_error *
499 view_input(struct tog_view **new, struct tog_view **dead,
500 struct tog_view **focus, int *done, struct tog_view *view,
501 struct tog_view_list_head *views)
503 const struct got_error *err = NULL;
511 nodelay(stdscr, FALSE);
512 /* Allow threads to make progress while we are waiting for input. */
513 errcode = pthread_mutex_unlock(&tog_mutex);
515 return got_error_set_errno(errcode);
516 ch = wgetch(view->window);
517 errcode = pthread_mutex_lock(&tog_mutex);
519 return got_error_set_errno(errcode);
520 nodelay(stdscr, TRUE);
522 if (tog_sigwinch_received) {
524 tog_sigwinch_received = 0;
525 TAILQ_FOREACH(v, views, entry) {
526 err = view_resize(v);
529 err = v->input(new, dead, focus, v, KEY_RESIZE);
540 *focus = view->child;
541 view->child_focussed = 1;
542 } else if (view->parent) {
543 *focus = view->parent;
544 view->parent->child_focussed = 0;
548 err = view->input(new, dead, focus, view, ch);
555 if (view_is_parent_view(view)) {
556 if (view->child == NULL)
558 if (view_is_splitscreen(view->child)) {
559 *focus = view->child;
560 view->child_focussed = 1;
561 err = view_fullscreen(view->child);
563 err = view_splitscreen(view->child);
566 err = view->child->input(new, dead, focus,
567 view->child, KEY_RESIZE);
569 if (view_is_splitscreen(view)) {
571 view->parent->child_focussed = 1;
572 err = view_fullscreen(view);
574 err = view_splitscreen(view);
578 err = view->input(new, dead, focus, view,
585 err = view->input(new, dead, focus, view, ch);
593 view_vborder(struct tog_view *view)
596 struct tog_view *view_above;
599 return view_vborder(view->parent);
601 panel = panel_above(view->panel);
605 view_above = panel_userptr(panel);
606 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
607 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
611 view_needs_focus_indication(struct tog_view *view)
613 if (view_is_parent_view(view)) {
614 if (view->child == NULL || view->child_focussed)
616 if (!view_is_splitscreen(view->child))
618 } else if (!view_is_splitscreen(view))
621 return view->focussed;
624 static const struct got_error *
625 view_loop(struct tog_view *view)
627 const struct got_error *err = NULL;
628 struct tog_view_list_head views;
629 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
630 int fast_refresh = 10;
631 int done = 0, errcode;
633 errcode = pthread_mutex_lock(&tog_mutex);
635 return got_error_set_errno(errcode);
638 TAILQ_INSERT_HEAD(&views, view, entry);
642 err = view->show(view);
647 while (!TAILQ_EMPTY(&views) && !done) {
648 /* Refresh fast during initialization, then become slower. */
649 if (fast_refresh && fast_refresh-- == 0)
650 halfdelay(10); /* switch to once per second */
652 err = view_input(&new_view, &dead_view, &focus_view, &done,
657 struct tog_view *prev = NULL;
659 if (view_is_parent_view(dead_view))
660 prev = TAILQ_PREV(dead_view,
661 tog_view_list_head, entry);
662 else if (view->parent != dead_view)
665 if (dead_view->parent)
666 dead_view->parent->child = NULL;
668 TAILQ_REMOVE(&views, dead_view, entry);
670 err = view_close(dead_view);
671 if (err || dead_view == main_view)
674 if (view == dead_view) {
679 else if (!TAILQ_EMPTY(&views))
680 view = TAILQ_LAST(&views,
685 if (view->child && view->child_focussed)
686 focus_view = view->child;
693 struct tog_view *v, *t;
694 /* Only allow one parent view per type. */
695 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
696 if (v->type != new_view->type)
698 TAILQ_REMOVE(&views, v, entry);
704 TAILQ_INSERT_TAIL(&views, new_view, entry);
706 if (focus_view == NULL)
707 focus_view = new_view;
710 show_panel(focus_view->panel);
713 focus_view->focussed = 1;
716 show_panel(new_view->panel);
717 if (view->child && view_is_splitscreen(view->child))
718 show_panel(view->child->panel);
721 if (focus_view == NULL) {
723 show_panel(view->panel);
724 if (view->child && view_is_splitscreen(view->child))
725 show_panel(view->child->panel);
729 err = view->parent->show(view->parent);
733 err = view->show(view);
737 err = view->child->show(view->child);
746 while (!TAILQ_EMPTY(&views)) {
747 view = TAILQ_FIRST(&views);
748 TAILQ_REMOVE(&views, view, entry);
752 errcode = pthread_mutex_unlock(&tog_mutex);
754 return got_error_set_errno(errcode);
764 "usage: %s log [-c commit] [-r repository-path] [path]\n",
769 /* Create newly allocated wide-character string equivalent to a byte string. */
770 static const struct got_error *
771 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
774 const struct got_error *err = NULL;
777 *wlen = mbstowcs(NULL, s, 0);
778 if (*wlen == (size_t)-1) {
781 return got_error_from_errno();
783 /* byte string invalid in current encoding; try to "fix" it */
784 err = got_mbsavis(&vis, &vislen, s);
787 *wlen = mbstowcs(NULL, vis, 0);
788 if (*wlen == (size_t)-1) {
789 err = got_error_from_errno(); /* give up */
794 *ws = calloc(*wlen + 1, sizeof(*ws));
796 err = got_error_from_errno();
800 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
801 err = got_error_from_errno();
812 /* Format a line for display, ensuring that it won't overflow a width limit. */
813 static const struct got_error *
814 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
816 const struct got_error *err = NULL;
818 wchar_t *wline = NULL;
825 err = mbs2ws(&wline, &wlen, line);
830 while (i < wlen && cols < wlimit) {
831 int width = wcwidth(wline[i]);
838 if (cols + width <= wlimit)
843 if (wline[i] == L'\t')
844 cols += TABSIZE - ((cols + 1) % TABSIZE);
848 err = got_error_from_errno();
863 static const struct got_error *
864 draw_commit(struct tog_view *view, struct got_commit_object *commit,
865 struct got_object_id *id)
867 const struct got_error *err = NULL;
868 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
869 char *logmsg0 = NULL, *logmsg = NULL;
870 char *author0 = NULL, *author = NULL;
871 wchar_t *wlogmsg = NULL, *wauthor = NULL;
872 int author_width, logmsg_width;
873 char *newline, *smallerthan;
876 static const size_t date_display_cols = 9;
877 static const size_t author_display_cols = 16;
878 const int avail = view->ncols;
880 time_t committer_time;
882 committer_time = got_object_commit_get_committer_time(commit);
883 if (localtime_r(&committer_time, &tm) == NULL)
884 return got_error_from_errno();
885 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
887 return got_error(GOT_ERR_NO_SPACE);
889 if (avail < date_display_cols)
890 limit = MIN(sizeof(datebuf) - 1, avail);
892 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
893 waddnstr(view->window, datebuf, limit);
898 author0 = strdup(got_object_commit_get_author(commit));
899 if (author0 == NULL) {
900 err = got_error_from_errno();
904 smallerthan = strchr(author, '<');
908 char *at = strchr(author, '@');
913 err = format_line(&wauthor, &author_width, author, limit);
916 waddwstr(view->window, wauthor);
918 while (col <= avail && author_width < author_display_cols + 1) {
919 waddch(view->window, ' ');
926 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
927 if (logmsg0 == NULL) {
928 err = got_error_from_errno();
932 while (*logmsg == '\n')
934 newline = strchr(logmsg, '\n');
938 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
941 waddwstr(view->window, wlogmsg);
943 while (col <= avail) {
944 waddch(view->window, ' ');
956 static struct commit_queue_entry *
957 alloc_commit_queue_entry(struct got_commit_object *commit,
958 struct got_object_id *id)
960 struct commit_queue_entry *entry;
962 entry = calloc(1, sizeof(*entry));
967 entry->commit = commit;
972 pop_commit(struct commit_queue *commits)
974 struct commit_queue_entry *entry;
976 entry = TAILQ_FIRST(&commits->head);
977 TAILQ_REMOVE(&commits->head, entry, entry);
978 got_object_commit_close(entry->commit);
980 /* Don't free entry->id! It is owned by the commit graph. */
985 free_commits(struct commit_queue *commits)
987 while (!TAILQ_EMPTY(&commits->head))
991 static const struct got_error *
992 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
993 int minqueue, struct got_repository *repo, const char *path)
995 const struct got_error *err = NULL;
999 * We keep all commits open throughout the lifetime of the log
1000 * view in order to avoid having to re-fetch commits from disk
1001 * while updating the display.
1003 while (nqueued < minqueue) {
1004 struct got_object_id *id;
1005 struct got_commit_object *commit;
1006 struct commit_queue_entry *entry;
1009 err = got_commit_graph_iter_next(&id, graph);
1011 if (err->code != GOT_ERR_ITER_NEED_MORE)
1013 err = got_commit_graph_fetch_commits(graph,
1023 err = got_object_open_as_commit(&commit, repo, id);
1026 entry = alloc_commit_queue_entry(commit, id);
1027 if (entry == NULL) {
1028 err = got_error_from_errno();
1032 errcode = pthread_mutex_lock(&tog_mutex);
1034 err = got_error_set_errno(errcode);
1038 entry->idx = commits->ncommits;
1039 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1041 commits->ncommits++;
1043 errcode = pthread_mutex_unlock(&tog_mutex);
1044 if (errcode && err == NULL)
1045 err = got_error_set_errno(errcode);
1051 static const struct got_error *
1052 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1054 const struct got_error *err = NULL;
1055 struct got_reference *head_ref;
1059 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1063 err = got_ref_resolve(head_id, repo, head_ref);
1064 got_ref_close(head_ref);
1073 static const struct got_error *
1074 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1075 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1076 struct commit_queue *commits, int selected_idx, int limit,
1077 const char *path, int commits_needed)
1079 const struct got_error *err = NULL;
1080 struct commit_queue_entry *entry;
1081 int ncommits, width;
1082 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1088 if (ncommits == selected_idx) {
1092 entry = TAILQ_NEXT(entry, entry);
1097 err = got_object_id_str(&id_str, (*selected)->id);
1102 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1103 entry ? entry->idx + 1 : 0, commits->ncommits,
1104 commits_needed == 0 ? "" : " loading...") == -1)
1105 return got_error_from_errno();
1107 if (path && strcmp(path, "/") != 0) {
1108 if (asprintf(&header, "commit %s %s%s",
1109 id_str ? id_str : "........................................",
1110 path, ncommits_str) == -1) {
1111 err = got_error_from_errno();
1115 } else if (asprintf(&header, "commit %s%s",
1116 id_str ? id_str : "........................................",
1117 ncommits_str) == -1) {
1118 err = got_error_from_errno();
1122 err = format_line(&wline, &width, header, view->ncols);
1126 werase(view->window);
1128 if (view_needs_focus_indication(view))
1129 wstandout(view->window);
1130 waddwstr(view->window, wline);
1131 while (width < view->ncols) {
1132 waddch(view->window, ' ');
1135 if (view_needs_focus_indication(view))
1136 wstandend(view->window);
1145 if (ncommits >= limit - 1)
1147 if (view->focussed && ncommits == selected_idx)
1148 wstandout(view->window);
1149 err = draw_commit(view, entry->commit, entry->id);
1150 if (view->focussed && ncommits == selected_idx)
1151 wstandend(view->window);
1156 entry = TAILQ_NEXT(entry, entry);
1168 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1169 struct commit_queue *commits)
1171 struct commit_queue_entry *entry;
1174 entry = TAILQ_FIRST(&commits->head);
1175 if (*first_displayed_entry == entry)
1178 entry = *first_displayed_entry;
1179 while (entry && nscrolled < maxscroll) {
1180 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1182 *first_displayed_entry = entry;
1188 static const struct got_error *
1189 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1190 struct commit_queue_entry **last_displayed_entry,
1191 struct commit_queue *commits, int *log_complete, int *commits_needed,
1192 pthread_cond_t *need_commits)
1194 const struct got_error *err = NULL;
1195 struct commit_queue_entry *pentry;
1198 if (*last_displayed_entry == NULL)
1202 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1203 if (pentry == NULL) {
1207 *commits_needed = maxscroll + 20;
1208 errcode = pthread_cond_signal(need_commits);
1210 return got_error_set_errno(errcode);
1213 *last_displayed_entry = pentry;
1215 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1218 *first_displayed_entry = pentry;
1219 } while (++nscrolled < maxscroll);
1224 static const struct got_error *
1225 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1226 struct got_object_id *commit_id, struct got_commit_object *commit,
1227 struct got_repository *repo)
1229 const struct got_error *err;
1230 struct got_object_qid *parent_id;
1231 struct tog_view *diff_view;
1233 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1234 if (diff_view == NULL)
1235 return got_error_from_errno();
1237 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1238 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1241 *new_view = diff_view;
1245 static const struct got_error *
1246 browse_commit(struct tog_view **new_view, int begin_x,
1247 struct commit_queue_entry *entry, struct got_repository *repo)
1249 const struct got_error *err = NULL;
1250 struct got_tree_object *tree;
1251 struct tog_view *tree_view;
1253 err = got_object_open_as_tree(&tree, repo,
1254 got_object_commit_get_tree_id(entry->commit));
1258 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1259 if (tree_view == NULL)
1260 return got_error_from_errno();
1262 err = open_tree_view(tree_view, tree, entry->id, repo);
1264 got_object_tree_close(tree);
1266 *new_view = tree_view;
1271 log_thread(void *arg)
1273 const struct got_error *err = NULL;
1275 struct tog_log_thread_args *a = arg;
1278 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1282 while (!done && !err) {
1283 err = queue_commits(a->graph, a->commits, 1, a->repo,
1286 if (err->code != GOT_ERR_ITER_COMPLETED)
1290 } else if (a->commits_needed > 0)
1291 a->commits_needed--;
1293 errcode = pthread_mutex_lock(&tog_mutex);
1295 return (void *)got_error_set_errno(errcode);
1298 a->log_complete = 1;
1299 else if (*a->quit) {
1301 a->log_complete = 1;
1302 } else if (*a->first_displayed_entry == NULL) {
1303 *a->first_displayed_entry =
1304 TAILQ_FIRST(&a->commits->head);
1305 *a->selected_entry = *a->first_displayed_entry;
1309 a->commits_needed = 0;
1310 else if (a->commits_needed == 0) {
1311 errcode = pthread_cond_wait(&a->need_commits,
1314 err = got_error_set_errno(errcode);
1317 errcode = pthread_mutex_unlock(&tog_mutex);
1318 if (errcode && err == NULL)
1319 err = got_error_set_errno(errcode);
1324 static const struct got_error *
1325 stop_log_thread(struct tog_log_view_state *s)
1327 const struct got_error *err = NULL;
1332 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1334 return got_error_set_errno(errcode);
1335 errcode = pthread_mutex_unlock(&tog_mutex);
1337 return got_error_set_errno(errcode);
1338 errcode = pthread_join(s->thread, (void **)&err);
1340 return got_error_set_errno(errcode);
1341 errcode = pthread_mutex_lock(&tog_mutex);
1343 return got_error_set_errno(errcode);
1347 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1348 if (errcode && err == NULL)
1349 err = got_error_set_errno(errcode);
1351 if (s->thread_args.repo) {
1352 got_repo_close(s->thread_args.repo);
1353 s->thread_args.repo = NULL;
1356 if (s->thread_args.graph) {
1357 got_commit_graph_close(s->thread_args.graph);
1358 s->thread_args.graph = NULL;
1364 static const struct got_error *
1365 close_log_view(struct tog_view *view)
1367 const struct got_error *err = NULL;
1368 struct tog_log_view_state *s = &view->state.log;
1370 err = stop_log_thread(s);
1371 free_commits(&s->commits);
1372 free(s->in_repo_path);
1373 s->in_repo_path = NULL;
1379 static const struct got_error *
1380 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1381 struct got_repository *repo, const char *path, int check_disk)
1383 const struct got_error *err = NULL;
1384 struct tog_log_view_state *s = &view->state.log;
1385 struct got_repository *thread_repo = NULL;
1386 struct got_commit_graph *thread_graph = NULL;
1389 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1393 /* The commit queue only contains commits being displayed. */
1394 TAILQ_INIT(&s->commits.head);
1395 s->commits.ncommits = 0;
1398 s->start_id = got_object_id_dup(start_id);
1399 if (s->start_id == NULL) {
1400 err = got_error_from_errno();
1404 view->show = show_log_view;
1405 view->input = input_log_view;
1406 view->close = close_log_view;
1408 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1411 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1416 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1418 err = got_error_set_errno(errcode);
1422 s->thread_args.commits_needed = view->nlines;
1423 s->thread_args.graph = thread_graph;
1424 s->thread_args.commits = &s->commits;
1425 s->thread_args.in_repo_path = s->in_repo_path;
1426 s->thread_args.start_id = s->start_id;
1427 s->thread_args.repo = thread_repo;
1428 s->thread_args.log_complete = 0;
1429 s->thread_args.quit = &s->quit;
1430 s->thread_args.view = view;
1431 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1432 s->thread_args.selected_entry = &s->selected_entry;
1435 close_log_view(view);
1439 static const struct got_error *
1440 show_log_view(struct tog_view *view)
1442 struct tog_log_view_state *s = &view->state.log;
1444 if (s->thread == NULL) {
1445 int errcode = pthread_create(&s->thread, NULL, log_thread,
1448 return got_error_set_errno(errcode);
1451 return draw_commits(view, &s->last_displayed_entry,
1452 &s->selected_entry, s->first_displayed_entry,
1453 &s->commits, s->selected, view->nlines,
1454 s->in_repo_path, s->thread_args.commits_needed);
1457 static const struct got_error *
1458 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1459 struct tog_view **focus_view, struct tog_view *view, int ch)
1461 const struct got_error *err = NULL;
1462 struct tog_log_view_state *s = &view->state.log;
1464 struct tog_view *diff_view = NULL, *tree_view = NULL;
1473 if (s->first_displayed_entry == NULL)
1475 if (s->selected > 0)
1477 if (s->selected > 0)
1479 scroll_up(&s->first_displayed_entry, 1,
1483 if (s->first_displayed_entry == NULL)
1485 if (TAILQ_FIRST(&s->commits.head) ==
1486 s->first_displayed_entry) {
1490 scroll_up(&s->first_displayed_entry,
1491 view->nlines, &s->commits);
1495 if (s->first_displayed_entry == NULL)
1497 if (s->selected < MIN(view->nlines - 2,
1498 s->commits.ncommits - 1)) {
1502 err = scroll_down(&s->first_displayed_entry, 1,
1503 &s->last_displayed_entry, &s->commits,
1504 &s->thread_args.log_complete,
1505 &s->thread_args.commits_needed,
1506 &s->thread_args.need_commits);
1509 struct commit_queue_entry *first;
1510 first = s->first_displayed_entry;
1513 err = scroll_down(&s->first_displayed_entry,
1514 view->nlines, &s->last_displayed_entry,
1515 &s->commits, &s->thread_args.log_complete,
1516 &s->thread_args.commits_needed,
1517 &s->thread_args.need_commits);
1518 if (first == s->first_displayed_entry &&
1519 s->selected < MIN(view->nlines - 2,
1520 s->commits.ncommits - 1)) {
1521 /* can't scroll further down */
1522 s->selected = MIN(view->nlines - 2,
1523 s->commits.ncommits - 1);
1529 if (s->selected > view->nlines - 2)
1530 s->selected = view->nlines - 2;
1531 if (s->selected > s->commits.ncommits - 1)
1532 s->selected = s->commits.ncommits - 1;
1536 if (s->selected_entry == NULL)
1538 if (view_is_parent_view(view))
1539 begin_x = view_split_begin_x(view->begin_x);
1540 err = open_diff_view_for_commit(&diff_view, begin_x,
1541 s->selected_entry->id, s->selected_entry->commit,
1545 if (view_is_parent_view(view)) {
1546 err = view_close_child(view);
1549 err = view_set_child(view, diff_view);
1551 view_close(diff_view);
1554 *focus_view = diff_view;
1555 view->child_focussed = 1;
1557 *new_view = diff_view;
1560 if (s->selected_entry == NULL)
1562 if (view_is_parent_view(view))
1563 begin_x = view_split_begin_x(view->begin_x);
1564 err = browse_commit(&tree_view, begin_x,
1565 s->selected_entry, s->repo);
1568 if (view_is_parent_view(view)) {
1569 err = view_close_child(view);
1572 err = view_set_child(view, tree_view);
1574 view_close(tree_view);
1577 *focus_view = tree_view;
1578 view->child_focussed = 1;
1580 *new_view = tree_view;
1583 if (strcmp(s->in_repo_path, "/") == 0)
1585 parent_path = dirname(s->in_repo_path);
1586 if (parent_path && strcmp(parent_path, ".") != 0) {
1587 struct tog_view *lv;
1588 err = stop_log_thread(s);
1591 lv = view_open(view->nlines, view->ncols,
1592 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1594 return got_error_from_errno();
1595 err = open_log_view(lv, s->start_id, s->repo,
1599 if (view_is_parent_view(view))
1602 view_set_child(view->parent, lv);
1615 static const struct got_error *
1616 apply_unveil(const char *repo_path, const char *worktree_path)
1618 const struct got_error *error;
1620 if (repo_path && unveil(repo_path, "r") != 0)
1621 return got_error_from_errno();
1623 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1624 return got_error_from_errno();
1626 if (unveil("/tmp", "rwc") != 0)
1627 return got_error_from_errno();
1629 error = got_privsep_unveil_exec_helpers();
1633 if (unveil(NULL, NULL) != 0)
1634 return got_error_from_errno();
1644 halfdelay(1); /* Do fast refresh while initial view is loading. */
1647 intrflush(stdscr, FALSE);
1648 keypad(stdscr, TRUE);
1650 signal(SIGWINCH, tog_sigwinch);
1653 static const struct got_error *
1654 cmd_log(int argc, char *argv[])
1656 const struct got_error *error;
1657 struct got_repository *repo = NULL;
1658 struct got_object_id *start_id = NULL;
1659 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1660 char *start_commit = NULL;
1662 struct tog_view *view;
1665 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1670 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1673 start_commit = optarg;
1676 repo_path = realpath(optarg, NULL);
1677 if (repo_path == NULL)
1678 err(1, "-r option");
1692 path = strdup(argv[0]);
1696 return got_error_from_errno();
1698 cwd = getcwd(NULL, 0);
1700 error = got_error_from_errno();
1703 if (repo_path == NULL) {
1704 struct got_worktree *worktree;
1705 error = got_worktree_open(&worktree, cwd);
1706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1710 strdup(got_worktree_get_repo_path(worktree));
1711 got_worktree_close(worktree);
1713 repo_path = strdup(cwd);
1714 if (repo_path == NULL) {
1715 error = got_error_from_errno();
1722 error = apply_unveil(repo_path, NULL);
1726 error = got_repo_open(&repo, repo_path);
1730 if (start_commit == NULL)
1731 error = get_head_commit_id(&start_id, repo);
1733 error = got_object_resolve_id_str(&start_id, repo,
1738 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1740 error = got_error_from_errno();
1743 error = open_log_view(view, start_id, repo, path, 1);
1746 error = view_loop(view);
1753 got_repo_close(repo);
1761 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1767 parse_next_line(FILE *f, size_t *len)
1772 const char delim[3] = { '\0', '\0', '\0'};
1774 line = fparseln(f, &linelen, &lineno, delim, 0);
1780 static const struct got_error *
1781 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1782 int *last_displayed_line, int *eof, int max_lines,
1785 const struct got_error *err;
1786 int nlines = 0, nprinted = 0;
1793 werase(view->window);
1796 err = format_line(&wline, &width, header, view->ncols);
1801 if (view_needs_focus_indication(view))
1802 wstandout(view->window);
1803 waddwstr(view->window, wline);
1804 if (view_needs_focus_indication(view))
1805 wstandend(view->window);
1806 if (width < view->ncols)
1807 waddch(view->window, '\n');
1815 while (nprinted < max_lines) {
1816 line = parse_next_line(f, &len);
1821 if (++nlines < *first_displayed_line) {
1826 err = format_line(&wline, &width, line, view->ncols);
1831 waddwstr(view->window, wline);
1832 if (width < view->ncols)
1833 waddch(view->window, '\n');
1834 if (++nprinted == 1)
1835 *first_displayed_line = nlines;
1840 *last_displayed_line = nlines;
1848 get_datestr(time_t *time, char *datebuf)
1850 char *p, *s = ctime_r(time, datebuf);
1851 p = strchr(s, '\n');
1857 static const struct got_error *
1858 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1861 const struct got_error *err = NULL;
1863 struct got_commit_object *commit;
1864 char *id_str = NULL;
1865 time_t committer_time;
1866 const char *author, *committer;
1868 err = got_object_open_as_commit(&commit, repo, commit_id);
1872 err = got_object_id_str(&id_str, commit_id);
1874 err = got_error_from_errno();
1878 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1879 err = got_error_from_errno();
1882 if (fprintf(outfile, "from: %s\n",
1883 got_object_commit_get_author(commit)) < 0) {
1884 err = got_error_from_errno();
1887 committer_time = got_object_commit_get_committer_time(commit);
1888 if (fprintf(outfile, "date: %s UTC\n",
1889 get_datestr(&committer_time, datebuf)) < 0) {
1890 err = got_error_from_errno();
1893 author = got_object_commit_get_author(commit);
1894 committer = got_object_commit_get_committer(commit);
1895 if (strcmp(author, committer) != 0 &&
1896 fprintf(outfile, "via: %s\n", committer) < 0) {
1897 err = got_error_from_errno();
1900 if (fprintf(outfile, "%s\n",
1901 got_object_commit_get_logmsg(commit)) < 0) {
1902 err = got_error_from_errno();
1907 got_object_commit_close(commit);
1911 static const struct got_error *
1912 create_diff(struct tog_diff_view_state *s)
1914 const struct got_error *err = NULL;
1920 err = got_error_from_errno();
1923 if (s->f && fclose(s->f) != 0) {
1924 err = got_error_from_errno();
1930 err = got_object_get_type(&obj_type, s->repo, s->id1);
1932 err = got_object_get_type(&obj_type, s->repo, s->id2);
1937 case GOT_OBJ_TYPE_BLOB:
1938 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1939 s->diff_context, s->repo, f);
1941 case GOT_OBJ_TYPE_TREE:
1942 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1943 s->diff_context, s->repo, f);
1945 case GOT_OBJ_TYPE_COMMIT: {
1946 const struct got_object_id_queue *parent_ids;
1947 struct got_object_qid *pid;
1948 struct got_commit_object *commit2;
1950 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1953 /* Show commit info if we're diffing to a parent commit. */
1954 parent_ids = got_object_commit_get_parent_ids(commit2);
1955 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1956 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1957 write_commit_info(s->id2, s->repo, f);
1961 got_object_commit_close(commit2);
1963 err = got_diff_objects_as_commits(s->id1, s->id2,
1964 s->diff_context, s->repo, f);
1968 err = got_error(GOT_ERR_OBJ_TYPE);
1972 if (f && fflush(f) != 0 && err == NULL)
1973 err = got_error_from_errno();
1977 static const struct got_error *
1978 open_diff_view(struct tog_view *view, struct got_object_id *id1,
1979 struct got_object_id *id2, struct got_repository *repo)
1981 const struct got_error *err;
1983 if (id1 != NULL && id2 != NULL) {
1985 err = got_object_get_type(&type1, repo, id1);
1988 err = got_object_get_type(&type2, repo, id2);
1993 return got_error(GOT_ERR_OBJ_TYPE);
1997 view->state.diff.id1 = got_object_id_dup(id1);
1998 if (view->state.diff.id1 == NULL)
1999 return got_error_from_errno();
2001 view->state.diff.id1 = NULL;
2003 view->state.diff.id2 = got_object_id_dup(id2);
2004 if (view->state.diff.id2 == NULL) {
2005 free(view->state.diff.id1);
2006 view->state.diff.id1 = NULL;
2007 return got_error_from_errno();
2009 view->state.diff.f = NULL;
2010 view->state.diff.first_displayed_line = 1;
2011 view->state.diff.last_displayed_line = view->nlines;
2012 view->state.diff.diff_context = 3;
2013 view->state.diff.repo = repo;
2015 err = create_diff(&view->state.diff);
2017 free(view->state.diff.id1);
2018 view->state.diff.id1 = NULL;
2019 free(view->state.diff.id2);
2020 view->state.diff.id2 = NULL;
2024 view->show = show_diff_view;
2025 view->input = input_diff_view;
2026 view->close = close_diff_view;
2031 static const struct got_error *
2032 close_diff_view(struct tog_view *view)
2034 const struct got_error *err = NULL;
2036 free(view->state.diff.id1);
2037 view->state.diff.id1 = NULL;
2038 free(view->state.diff.id2);
2039 view->state.diff.id2 = NULL;
2040 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2041 err = got_error_from_errno();
2045 static const struct got_error *
2046 show_diff_view(struct tog_view *view)
2048 const struct got_error *err;
2049 struct tog_diff_view_state *s = &view->state.diff;
2050 char *id_str1 = NULL, *id_str2, *header;
2053 err = got_object_id_str(&id_str1, s->id1);
2057 err = got_object_id_str(&id_str2, s->id2);
2061 if (asprintf(&header, "diff %s %s",
2062 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2063 err = got_error_from_errno();
2071 return draw_file(view, s->f, &s->first_displayed_line,
2072 &s->last_displayed_line, &s->eof, view->nlines,
2076 static const struct got_error *
2077 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2078 struct tog_view **focus_view, struct tog_view *view, int ch)
2080 const struct got_error *err = NULL;
2081 struct tog_diff_view_state *s = &view->state.diff;
2087 if (s->first_displayed_line > 1)
2088 s->first_displayed_line--;
2092 while (i++ < view->nlines - 1 &&
2093 s->first_displayed_line > 1)
2094 s->first_displayed_line--;
2099 s->first_displayed_line++;
2104 while (!s->eof && i++ < view->nlines - 1) {
2106 line = parse_next_line(s->f, NULL);
2107 s->first_displayed_line++;
2113 if (s->diff_context > 0) {
2115 err = create_diff(s);
2119 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2121 err = create_diff(s);
2131 static const struct got_error *
2132 cmd_diff(int argc, char *argv[])
2134 const struct got_error *error = NULL;
2135 struct got_repository *repo = NULL;
2136 struct got_object_id *id1 = NULL, *id2 = NULL;
2137 char *repo_path = NULL;
2138 char *id_str1 = NULL, *id_str2 = NULL;
2140 struct tog_view *view;
2143 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2148 while ((ch = getopt(argc, argv, "")) != -1) {
2160 usage_diff(); /* TODO show local worktree changes */
2161 } else if (argc == 2) {
2162 repo_path = getcwd(NULL, 0);
2163 if (repo_path == NULL)
2164 return got_error_from_errno();
2167 } else if (argc == 3) {
2168 repo_path = realpath(argv[0], NULL);
2169 if (repo_path == NULL)
2170 return got_error_from_errno();
2178 error = apply_unveil(repo_path, NULL);
2182 error = got_repo_open(&repo, repo_path);
2187 error = got_object_resolve_id_str(&id1, repo, id_str1);
2191 error = got_object_resolve_id_str(&id2, repo, id_str2);
2195 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2197 error = got_error_from_errno();
2200 error = open_diff_view(view, id1, id2, repo);
2203 error = view_loop(view);
2205 got_repo_close(repo);
2213 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2218 struct tog_blame_line {
2220 struct got_object_id *id;
2223 static const struct got_error *
2224 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2225 const char *path, struct tog_blame_line *lines, int nlines,
2226 int blame_complete, int selected_line, int *first_displayed_line,
2227 int *last_displayed_line, int *eof, int max_lines)
2229 const struct got_error *err;
2230 int lineno = 0, nprinted = 0;
2235 struct tog_blame_line *blame_line;
2236 struct got_object_id *prev_id = NULL;
2239 err = got_object_id_str(&id_str, id);
2244 werase(view->window);
2246 if (asprintf(&line, "commit %s", id_str) == -1) {
2247 err = got_error_from_errno();
2252 err = format_line(&wline, &width, line, view->ncols);
2255 if (view_needs_focus_indication(view))
2256 wstandout(view->window);
2257 waddwstr(view->window, wline);
2258 if (view_needs_focus_indication(view))
2259 wstandend(view->window);
2262 if (width < view->ncols)
2263 waddch(view->window, '\n');
2265 if (asprintf(&line, "[%d/%d] %s%s",
2266 *first_displayed_line - 1 + selected_line, nlines,
2267 blame_complete ? "" : "annotating ", path) == -1) {
2269 return got_error_from_errno();
2272 err = format_line(&wline, &width, line, view->ncols);
2277 waddwstr(view->window, wline);
2280 if (width < view->ncols)
2281 waddch(view->window, '\n');
2284 while (nprinted < max_lines - 2) {
2285 line = parse_next_line(f, &len);
2290 if (++lineno < *first_displayed_line) {
2295 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2296 err = format_line(&wline, &width, line, wlimit);
2302 if (view->focussed && nprinted == selected_line - 1)
2303 wstandout(view->window);
2305 blame_line = &lines[lineno - 1];
2306 if (blame_line->annotated && prev_id &&
2307 got_object_id_cmp(prev_id, blame_line->id) == 0)
2308 waddstr(view->window, " ");
2309 else if (blame_line->annotated) {
2311 err = got_object_id_str(&id_str, blame_line->id);
2317 wprintw(view->window, "%.8s ", id_str);
2319 prev_id = blame_line->id;
2321 waddstr(view->window, "........ ");
2325 waddwstr(view->window, wline);
2326 while (width < wlimit) {
2327 waddch(view->window, ' ');
2330 if (view->focussed && nprinted == selected_line - 1)
2331 wstandend(view->window);
2332 if (++nprinted == 1)
2333 *first_displayed_line = lineno;
2338 *last_displayed_line = lineno;
2345 static const struct got_error *
2346 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2348 const struct got_error *err = NULL;
2349 struct tog_blame_cb_args *a = arg;
2350 struct tog_blame_line *line;
2353 if (nlines != a->nlines ||
2354 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2355 return got_error(GOT_ERR_RANGE);
2357 errcode = pthread_mutex_lock(&tog_mutex);
2359 return got_error_set_errno(errcode);
2361 if (*a->quit) { /* user has quit the blame view */
2362 err = got_error(GOT_ERR_ITER_COMPLETED);
2367 goto done; /* no change in this commit */
2369 line = &a->lines[lineno - 1];
2370 if (line->annotated)
2373 line->id = got_object_id_dup(id);
2374 if (line->id == NULL) {
2375 err = got_error_from_errno();
2378 line->annotated = 1;
2380 errcode = pthread_mutex_unlock(&tog_mutex);
2382 err = got_error_set_errno(errcode);
2387 blame_thread(void *arg)
2389 const struct got_error *err;
2390 struct tog_blame_thread_args *ta = arg;
2391 struct tog_blame_cb_args *a = ta->cb_args;
2394 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2395 blame_cb, ta->cb_args);
2397 errcode = pthread_mutex_lock(&tog_mutex);
2399 return (void *)got_error_set_errno(errcode);
2401 got_repo_close(ta->repo);
2405 errcode = pthread_mutex_unlock(&tog_mutex);
2406 if (errcode && err == NULL)
2407 err = got_error_set_errno(errcode);
2412 static struct got_object_id *
2413 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2416 struct tog_blame_line *line;
2418 line = &lines[first_displayed_line - 1 + selected_line - 1];
2419 if (!line->annotated)
2425 static const struct got_error *
2426 stop_blame(struct tog_blame *blame)
2428 const struct got_error *err = NULL;
2431 if (blame->thread) {
2433 errcode = pthread_mutex_unlock(&tog_mutex);
2435 return got_error_set_errno(errcode);
2436 errcode = pthread_join(blame->thread, (void **)&err);
2438 return got_error_set_errno(errcode);
2439 errcode = pthread_mutex_lock(&tog_mutex);
2441 return got_error_set_errno(errcode);
2442 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2444 blame->thread = NULL;
2446 if (blame->thread_args.repo) {
2447 got_repo_close(blame->thread_args.repo);
2448 blame->thread_args.repo = NULL;
2451 if (fclose(blame->f) != 0 && err == NULL)
2452 err = got_error_from_errno();
2456 for (i = 0; i < blame->nlines; i++)
2457 free(blame->lines[i].id);
2459 blame->lines = NULL;
2461 free(blame->cb_args.commit_id);
2462 blame->cb_args.commit_id = NULL;
2467 static const struct got_error *
2468 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2469 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2470 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2471 struct got_repository *repo)
2473 const struct got_error *err = NULL;
2474 struct got_blob_object *blob = NULL;
2475 struct got_repository *thread_repo = NULL;
2476 struct got_object_id *obj_id = NULL;
2479 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2483 return got_error(GOT_ERR_NO_OBJ);
2485 err = got_object_get_type(&obj_type, repo, obj_id);
2489 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2490 err = got_error(GOT_ERR_OBJ_TYPE);
2494 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2497 blame->f = got_opentemp();
2498 if (blame->f == NULL) {
2499 err = got_error_from_errno();
2502 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2507 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2508 if (blame->lines == NULL) {
2509 err = got_error_from_errno();
2513 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2517 blame->cb_args.view = view;
2518 blame->cb_args.lines = blame->lines;
2519 blame->cb_args.nlines = blame->nlines;
2520 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2521 if (blame->cb_args.commit_id == NULL) {
2522 err = got_error_from_errno();
2525 blame->cb_args.quit = done;
2527 blame->thread_args.path = path;
2528 blame->thread_args.repo = thread_repo;
2529 blame->thread_args.cb_args = &blame->cb_args;
2530 blame->thread_args.complete = blame_complete;
2531 *blame_complete = 0;
2535 got_object_blob_close(blob);
2542 static const struct got_error *
2543 open_blame_view(struct tog_view *view, char *path,
2544 struct got_object_id *commit_id, struct got_repository *repo)
2546 const struct got_error *err = NULL;
2547 struct tog_blame_view_state *s = &view->state.blame;
2549 SIMPLEQ_INIT(&s->blamed_commits);
2551 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2555 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2556 s->first_displayed_line = 1;
2557 s->last_displayed_line = view->nlines;
2558 s->selected_line = 1;
2559 s->blame_complete = 0;
2561 if (s->path == NULL)
2562 return got_error_from_errno();
2564 s->commit_id = commit_id;
2565 memset(&s->blame, 0, sizeof(s->blame));
2567 view->show = show_blame_view;
2568 view->input = input_blame_view;
2569 view->close = close_blame_view;
2571 return run_blame(&s->blame, view, &s->blame_complete,
2572 &s->first_displayed_line, &s->last_displayed_line,
2573 &s->selected_line, &s->done, &s->eof, s->path,
2574 s->blamed_commit->id, s->repo);
2577 static const struct got_error *
2578 close_blame_view(struct tog_view *view)
2580 const struct got_error *err = NULL;
2581 struct tog_blame_view_state *s = &view->state.blame;
2583 if (s->blame.thread)
2584 err = stop_blame(&s->blame);
2586 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2587 struct got_object_qid *blamed_commit;
2588 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2589 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2590 got_object_qid_free(blamed_commit);
2598 static const struct got_error *
2599 show_blame_view(struct tog_view *view)
2601 const struct got_error *err = NULL;
2602 struct tog_blame_view_state *s = &view->state.blame;
2605 if (s->blame.thread == NULL) {
2606 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2607 &s->blame.thread_args);
2609 return got_error_set_errno(errcode);
2612 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2613 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2614 s->selected_line, &s->first_displayed_line,
2615 &s->last_displayed_line, &s->eof, view->nlines);
2621 static const struct got_error *
2622 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2623 struct tog_view **focus_view, struct tog_view *view, int ch)
2625 const struct got_error *err = NULL, *thread_err = NULL;
2626 struct tog_view *diff_view;
2627 struct tog_blame_view_state *s = &view->state.blame;
2636 if (s->selected_line > 1)
2638 else if (s->selected_line == 1 &&
2639 s->first_displayed_line > 1)
2640 s->first_displayed_line--;
2643 if (s->first_displayed_line == 1) {
2644 s->selected_line = 1;
2647 if (s->first_displayed_line > view->nlines - 2)
2648 s->first_displayed_line -=
2651 s->first_displayed_line = 1;
2655 if (s->selected_line < view->nlines - 2 &&
2656 s->first_displayed_line +
2657 s->selected_line <= s->blame.nlines)
2659 else if (s->last_displayed_line <
2661 s->first_displayed_line++;
2665 struct got_object_id *id = NULL;
2666 id = get_selected_commit_id(s->blame.lines,
2667 s->first_displayed_line, s->selected_line);
2671 struct got_commit_object *commit;
2672 struct got_object_qid *pid;
2673 struct got_object_id *blob_id = NULL;
2675 err = got_object_open_as_commit(&commit,
2679 pid = SIMPLEQ_FIRST(
2680 got_object_commit_get_parent_ids(commit));
2682 got_object_commit_close(commit);
2685 /* Check if path history ends here. */
2686 err = got_object_id_by_path(&blob_id, s->repo,
2689 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2691 got_object_commit_close(commit);
2694 err = got_object_get_type(&obj_type, s->repo,
2697 /* Can't blame non-blob type objects. */
2698 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2699 got_object_commit_close(commit);
2702 err = got_object_qid_alloc(&s->blamed_commit,
2704 got_object_commit_close(commit);
2706 if (got_object_id_cmp(id,
2707 s->blamed_commit->id) == 0)
2709 err = got_object_qid_alloc(&s->blamed_commit,
2715 thread_err = stop_blame(&s->blame);
2719 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2720 s->blamed_commit, entry);
2721 err = run_blame(&s->blame, view, &s->blame_complete,
2722 &s->first_displayed_line, &s->last_displayed_line,
2723 &s->selected_line, &s->done, &s->eof,
2724 s->path, s->blamed_commit->id, s->repo);
2730 struct got_object_qid *first;
2731 first = SIMPLEQ_FIRST(&s->blamed_commits);
2732 if (!got_object_id_cmp(first->id, s->commit_id))
2735 thread_err = stop_blame(&s->blame);
2739 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2740 got_object_qid_free(s->blamed_commit);
2742 SIMPLEQ_FIRST(&s->blamed_commits);
2743 err = run_blame(&s->blame, view, &s->blame_complete,
2744 &s->first_displayed_line, &s->last_displayed_line,
2745 &s->selected_line, &s->done, &s->eof, s->path,
2746 s->blamed_commit->id, s->repo);
2753 struct got_object_id *id = NULL;
2754 struct got_object_qid *pid;
2755 struct got_commit_object *commit = NULL;
2756 id = get_selected_commit_id(s->blame.lines,
2757 s->first_displayed_line, s->selected_line);
2760 err = got_object_open_as_commit(&commit, s->repo, id);
2763 pid = SIMPLEQ_FIRST(
2764 got_object_commit_get_parent_ids(commit));
2765 if (view_is_parent_view(view))
2766 begin_x = view_split_begin_x(view->begin_x);
2767 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2768 if (diff_view == NULL) {
2769 got_object_commit_close(commit);
2770 err = got_error_from_errno();
2773 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2775 got_object_commit_close(commit);
2777 view_close(diff_view);
2780 if (view_is_parent_view(view)) {
2781 err = view_close_child(view);
2784 err = view_set_child(view, diff_view);
2786 view_close(diff_view);
2789 *focus_view = diff_view;
2790 view->child_focussed = 1;
2792 *new_view = diff_view;
2799 if (s->last_displayed_line >= s->blame.nlines &&
2800 s->selected_line < view->nlines - 2) {
2801 s->selected_line = MIN(s->blame.nlines,
2805 if (s->last_displayed_line + view->nlines - 2
2807 s->first_displayed_line +=
2810 s->first_displayed_line =
2815 if (s->selected_line > view->nlines - 2) {
2816 s->selected_line = MIN(s->blame.nlines,
2823 return thread_err ? thread_err : err;
2826 static const struct got_error *
2827 cmd_blame(int argc, char *argv[])
2829 const struct got_error *error;
2830 struct got_repository *repo = NULL;
2831 struct got_worktree *worktree = NULL;
2832 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2833 struct got_object_id *commit_id = NULL;
2834 char *commit_id_str = NULL;
2836 struct tog_view *view;
2839 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2844 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2847 commit_id_str = optarg;
2850 repo_path = realpath(optarg, NULL);
2851 if (repo_path == NULL)
2852 err(1, "-r option");
2868 cwd = getcwd(NULL, 0);
2870 error = got_error_from_errno();
2873 if (repo_path == NULL) {
2874 error = got_worktree_open(&worktree, cwd);
2875 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2881 strdup(got_worktree_get_repo_path(worktree));
2882 if (repo_path == NULL)
2883 error = got_error_from_errno();
2887 repo_path = strdup(cwd);
2888 if (repo_path == NULL) {
2889 error = got_error_from_errno();
2897 error = apply_unveil(repo_path, NULL);
2901 error = got_repo_open(&repo, repo_path);
2906 const char *prefix = got_worktree_get_path_prefix(worktree);
2907 char *p, *worktree_subdir = cwd +
2908 strlen(got_worktree_get_root_path(worktree));
2909 if (asprintf(&p, "%s%s%s%s%s",
2910 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2911 worktree_subdir, worktree_subdir[0] ? "/" : "",
2913 error = got_error_from_errno();
2916 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2919 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2924 if (commit_id_str == NULL) {
2925 struct got_reference *head_ref;
2926 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2929 error = got_ref_resolve(&commit_id, repo, head_ref);
2930 got_ref_close(head_ref);
2932 error = got_object_resolve_id_str(&commit_id, repo,
2938 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2940 error = got_error_from_errno();
2943 error = open_blame_view(view, in_repo_path, commit_id, repo);
2946 error = view_loop(view);
2952 got_worktree_close(worktree);
2954 got_repo_close(repo);
2958 static const struct got_error *
2959 draw_tree_entries(struct tog_view *view,
2960 struct got_tree_entry **first_displayed_entry,
2961 struct got_tree_entry **last_displayed_entry,
2962 struct got_tree_entry **selected_entry, int *ndisplayed,
2963 const char *label, int show_ids, const char *parent_path,
2964 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2966 const struct got_error *err = NULL;
2967 struct got_tree_entry *te;
2973 werase(view->window);
2978 err = format_line(&wline, &width, label, view->ncols);
2981 if (view_needs_focus_indication(view))
2982 wstandout(view->window);
2983 waddwstr(view->window, wline);
2984 if (view_needs_focus_indication(view))
2985 wstandend(view->window);
2988 if (width < view->ncols)
2989 waddch(view->window, '\n');
2992 err = format_line(&wline, &width, parent_path, view->ncols);
2995 waddwstr(view->window, wline);
2998 if (width < view->ncols)
2999 waddch(view->window, '\n');
3002 waddch(view->window, '\n');
3006 te = SIMPLEQ_FIRST(&entries->head);
3007 if (*first_displayed_entry == NULL) {
3008 if (selected == 0) {
3010 wstandout(view->window);
3011 *selected_entry = NULL;
3013 waddstr(view->window, " ..\n"); /* parent directory */
3014 if (selected == 0 && view->focussed)
3015 wstandend(view->window);
3022 while (te != *first_displayed_entry)
3023 te = SIMPLEQ_NEXT(te, entry);
3027 char *line = NULL, *id_str = NULL;
3030 err = got_object_id_str(&id_str, te->id);
3032 return got_error_from_errno();
3034 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3035 te->name, S_ISDIR(te->mode) ? "/" :
3036 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3038 return got_error_from_errno();
3041 err = format_line(&wline, &width, line, view->ncols);
3046 if (n == selected) {
3048 wstandout(view->window);
3049 *selected_entry = te;
3051 waddwstr(view->window, wline);
3052 if (width < view->ncols)
3053 waddch(view->window, '\n');
3054 if (n == selected && view->focussed)
3055 wstandend(view->window);
3061 *last_displayed_entry = te;
3064 te = SIMPLEQ_NEXT(te, entry);
3071 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3072 const struct got_tree_entries *entries, int isroot)
3074 struct got_tree_entry *te, *prev;
3077 if (*first_displayed_entry == NULL)
3080 te = SIMPLEQ_FIRST(&entries->head);
3081 if (*first_displayed_entry == te) {
3083 *first_displayed_entry = NULL;
3087 /* XXX this is stupid... switch to TAILQ? */
3088 for (i = 0; i < maxscroll; i++) {
3089 while (te != *first_displayed_entry) {
3091 te = SIMPLEQ_NEXT(te, entry);
3093 *first_displayed_entry = prev;
3094 te = SIMPLEQ_FIRST(&entries->head);
3096 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3097 *first_displayed_entry = NULL;
3101 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3102 struct got_tree_entry *last_displayed_entry,
3103 const struct got_tree_entries *entries)
3105 struct got_tree_entry *next, *last;
3108 if (*first_displayed_entry)
3109 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3111 next = SIMPLEQ_FIRST(&entries->head);
3112 last = last_displayed_entry;
3113 while (next && last && n++ < maxscroll) {
3114 last = SIMPLEQ_NEXT(last, entry);
3116 *first_displayed_entry = next;
3117 next = SIMPLEQ_NEXT(next, entry);
3123 static const struct got_error *
3124 tree_entry_path(char **path, struct tog_parent_trees *parents,
3125 struct got_tree_entry *te)
3127 const struct got_error *err = NULL;
3128 struct tog_parent_tree *pt;
3129 size_t len = 2; /* for leading slash and NUL */
3131 TAILQ_FOREACH(pt, parents, entry)
3132 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3134 len += strlen(te->name);
3136 *path = calloc(1, len);
3138 return got_error_from_errno();
3141 pt = TAILQ_LAST(parents, tog_parent_trees);
3143 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3144 err = got_error(GOT_ERR_NO_SPACE);
3147 if (strlcat(*path, "/", len) >= len) {
3148 err = got_error(GOT_ERR_NO_SPACE);
3151 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3154 if (strlcat(*path, te->name, len) >= len) {
3155 err = got_error(GOT_ERR_NO_SPACE);
3167 static const struct got_error *
3168 blame_tree_entry(struct tog_view **new_view, int begin_x,
3169 struct got_tree_entry *te, struct tog_parent_trees *parents,
3170 struct got_object_id *commit_id, struct got_repository *repo)
3172 const struct got_error *err = NULL;
3174 struct tog_view *blame_view;
3176 err = tree_entry_path(&path, parents, te);
3180 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3181 if (blame_view == NULL)
3182 return got_error_from_errno();
3184 err = open_blame_view(blame_view, path, commit_id, repo);
3186 view_close(blame_view);
3189 *new_view = blame_view;
3193 static const struct got_error *
3194 log_tree_entry(struct tog_view **new_view, int begin_x,
3195 struct got_tree_entry *te, struct tog_parent_trees *parents,
3196 struct got_object_id *commit_id, struct got_repository *repo)
3198 struct tog_view *log_view;
3199 const struct got_error *err = NULL;
3202 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3203 if (log_view == NULL)
3204 return got_error_from_errno();
3206 err = tree_entry_path(&path, parents, te);
3210 err = open_log_view(log_view, commit_id, repo, path, 0);
3212 view_close(log_view);
3214 *new_view = log_view;
3219 static const struct got_error *
3220 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3221 struct got_object_id *commit_id, struct got_repository *repo)
3223 const struct got_error *err = NULL;
3224 char *commit_id_str = NULL;
3225 struct tog_tree_view_state *s = &view->state.tree;
3227 TAILQ_INIT(&s->parents);
3229 err = got_object_id_str(&commit_id_str, commit_id);
3233 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3234 err = got_error_from_errno();
3238 s->root = s->tree = root;
3239 s->entries = got_object_tree_get_entries(root);
3240 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3241 s->commit_id = got_object_id_dup(commit_id);
3242 if (s->commit_id == NULL) {
3243 err = got_error_from_errno();
3248 view->show = show_tree_view;
3249 view->input = input_tree_view;
3250 view->close = close_tree_view;
3252 free(commit_id_str);
3254 free(s->tree_label);
3255 s->tree_label = NULL;
3260 static const struct got_error *
3261 close_tree_view(struct tog_view *view)
3263 struct tog_tree_view_state *s = &view->state.tree;
3265 free(s->tree_label);
3266 s->tree_label = NULL;
3268 s->commit_id = NULL;
3269 while (!TAILQ_EMPTY(&s->parents)) {
3270 struct tog_parent_tree *parent;
3271 parent = TAILQ_FIRST(&s->parents);
3272 TAILQ_REMOVE(&s->parents, parent, entry);
3276 if (s->tree != s->root)
3277 got_object_tree_close(s->tree);
3278 got_object_tree_close(s->root);
3283 static const struct got_error *
3284 show_tree_view(struct tog_view *view)
3286 const struct got_error *err = NULL;
3287 struct tog_tree_view_state *s = &view->state.tree;
3290 err = tree_entry_path(&parent_path, &s->parents, NULL);
3294 err = draw_tree_entries(view, &s->first_displayed_entry,
3295 &s->last_displayed_entry, &s->selected_entry,
3296 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3297 s->entries, s->selected, view->nlines, s->tree == s->root);
3304 static const struct got_error *
3305 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3306 struct tog_view **focus_view, struct tog_view *view, int ch)
3308 const struct got_error *err = NULL;
3309 struct tog_tree_view_state *s = &view->state.tree;
3310 struct tog_view *log_view;
3311 int begin_x = 0, nscrolled;
3315 s->show_ids = !s->show_ids;
3318 if (!s->selected_entry)
3320 if (view_is_parent_view(view))
3321 begin_x = view_split_begin_x(view->begin_x);
3322 err = log_tree_entry(&log_view, begin_x,
3323 s->selected_entry, &s->parents,
3324 s->commit_id, s->repo);
3325 if (view_is_parent_view(view)) {
3326 err = view_close_child(view);
3329 err = view_set_child(view, log_view);
3331 view_close(log_view);
3334 *focus_view = log_view;
3335 view->child_focussed = 1;
3337 *new_view = log_view;
3341 if (s->selected > 0) {
3343 if (s->selected == 0)
3346 if (s->selected > 0)
3348 tree_scroll_up(&s->first_displayed_entry, 1,
3349 s->entries, s->tree == s->root);
3352 tree_scroll_up(&s->first_displayed_entry,
3353 MAX(0, view->nlines - 4 - s->selected), s->entries,
3354 s->tree == s->root);
3356 if (SIMPLEQ_FIRST(&s->entries->head) ==
3357 s->first_displayed_entry && s->tree != s->root)
3358 s->first_displayed_entry = NULL;
3362 if (s->selected < s->ndisplayed - 1) {
3366 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3368 /* can't scroll any further */
3371 tree_scroll_down(&s->first_displayed_entry, 1,
3372 s->last_displayed_entry, s->entries);
3375 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3377 /* can't scroll any further; move cursor down */
3378 if (s->selected < s->ndisplayed - 1)
3379 s->selected = s->ndisplayed - 1;
3382 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3383 view->nlines, s->last_displayed_entry, s->entries);
3384 if (nscrolled < view->nlines) {
3386 struct got_tree_entry *te;
3387 te = s->first_displayed_entry;
3390 te = SIMPLEQ_NEXT(te, entry);
3392 s->selected = ndisplayed - 1;
3397 if (s->selected_entry == NULL) {
3398 struct tog_parent_tree *parent;
3400 /* user selected '..' */
3401 if (s->tree == s->root)
3403 parent = TAILQ_FIRST(&s->parents);
3404 TAILQ_REMOVE(&s->parents, parent,
3406 got_object_tree_close(s->tree);
3407 s->tree = parent->tree;
3409 got_object_tree_get_entries(s->tree);
3410 s->first_displayed_entry =
3411 parent->first_displayed_entry;
3413 parent->selected_entry;
3414 s->selected = parent->selected;
3416 } else if (S_ISDIR(s->selected_entry->mode)) {
3417 struct tog_parent_tree *parent;
3418 struct got_tree_object *child;
3419 err = got_object_open_as_tree(&child,
3420 s->repo, s->selected_entry->id);
3423 parent = calloc(1, sizeof(*parent));
3424 if (parent == NULL) {
3425 err = got_error_from_errno();
3428 parent->tree = s->tree;
3429 parent->first_displayed_entry =
3430 s->first_displayed_entry;
3431 parent->selected_entry = s->selected_entry;
3432 parent->selected = s->selected;
3433 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3436 got_object_tree_get_entries(s->tree);
3438 s->first_displayed_entry = NULL;
3439 } else if (S_ISREG(s->selected_entry->mode)) {
3440 struct tog_view *blame_view;
3441 int begin_x = view_is_parent_view(view) ?
3442 view_split_begin_x(view->begin_x) : 0;
3444 err = blame_tree_entry(&blame_view, begin_x,
3445 s->selected_entry, &s->parents, s->commit_id,
3449 if (view_is_parent_view(view)) {
3450 err = view_close_child(view);
3453 err = view_set_child(view, blame_view);
3455 view_close(blame_view);
3458 *focus_view = blame_view;
3459 view->child_focussed = 1;
3461 *new_view = blame_view;
3465 if (s->selected > view->nlines)
3466 s->selected = s->ndisplayed - 1;
3479 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3484 static const struct got_error *
3485 cmd_tree(int argc, char *argv[])
3487 const struct got_error *error;
3488 struct got_repository *repo = NULL;
3489 char *repo_path = NULL;
3490 struct got_object_id *commit_id = NULL;
3491 char *commit_id_arg = NULL;
3492 struct got_commit_object *commit = NULL;
3493 struct got_tree_object *tree = NULL;
3495 struct tog_view *view;
3498 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3503 while ((ch = getopt(argc, argv, "c:")) != -1) {
3506 commit_id_arg = optarg;
3518 struct got_worktree *worktree;
3519 char *cwd = getcwd(NULL, 0);
3521 return got_error_from_errno();
3522 error = got_worktree_open(&worktree, cwd);
3523 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3528 strdup(got_worktree_get_repo_path(worktree));
3529 got_worktree_close(worktree);
3532 if (repo_path == NULL) {
3533 error = got_error_from_errno();
3536 } else if (argc == 1) {
3537 repo_path = realpath(argv[0], NULL);
3538 if (repo_path == NULL)
3539 return got_error_from_errno();
3545 error = apply_unveil(repo_path, NULL);
3549 error = got_repo_open(&repo, repo_path);
3553 if (commit_id_arg == NULL)
3554 error = get_head_commit_id(&commit_id, repo);
3556 error = got_object_resolve_id_str(&commit_id, repo,
3561 error = got_object_open_as_commit(&commit, repo, commit_id);
3565 error = got_object_open_as_tree(&tree, repo,
3566 got_object_commit_get_tree_id(commit));
3570 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3572 error = got_error_from_errno();
3575 error = open_tree_view(view, tree, commit_id, repo);
3578 error = view_loop(view);
3583 got_object_commit_close(commit);
3585 got_object_tree_close(tree);
3587 got_repo_close(repo);
3596 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3597 "Available commands:\n", getprogname());
3598 for (i = 0; i < nitems(tog_commands); i++) {
3599 struct tog_cmd *cmd = &tog_commands[i];
3600 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3606 make_argv(const char *arg0, const char *arg1)
3609 int argc = (arg1 == NULL ? 1 : 2);
3611 argv = calloc(argc, sizeof(char *));
3614 argv[0] = strdup(arg0);
3615 if (argv[0] == NULL)
3618 argv[1] = strdup(arg1);
3619 if (argv[1] == NULL)
3627 main(int argc, char *argv[])
3629 const struct got_error *error = NULL;
3630 struct tog_cmd *cmd = NULL;
3632 char **cmd_argv = NULL;
3634 setlocale(LC_CTYPE, "");
3636 while ((ch = getopt(argc, argv, "h")) != -1) {
3655 /* Build an argument vector which runs a default command. */
3656 cmd = &tog_commands[0];
3657 cmd_argv = make_argv(cmd->name, NULL);
3662 /* Did the user specific a command? */
3663 for (i = 0; i < nitems(tog_commands); i++) {
3664 if (strncmp(tog_commands[i].name, argv[0],
3665 strlen(argv[0])) == 0) {
3666 cmd = &tog_commands[i];
3668 tog_commands[i].cmd_usage();
3673 /* Did the user specify a repository? */
3674 char *repo_path = realpath(argv[0], NULL);
3676 struct got_repository *repo;
3677 error = got_repo_open(&repo, repo_path);
3679 got_repo_close(repo);
3681 error = got_error_from_errno();
3684 fprintf(stderr, "%s: '%s' is not a "
3685 "known command\n", getprogname(),
3689 fprintf(stderr, "%s: '%s' is neither a known "
3690 "command nor a path to a repository\n",
3691 getprogname(), argv[0]);
3695 cmd = &tog_commands[0];
3696 cmd_argv = make_argv(cmd->name, repo_path);
3702 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3709 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);