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"
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 const struct got_error *(*cmd_main)(int, char *[]);
67 void (*cmd_usage)(void);
71 __dead static void usage(void);
72 __dead static void usage_log(void);
73 __dead static void usage_diff(void);
74 __dead static void usage_blame(void);
75 __dead static void usage_tree(void);
77 static const struct got_error* cmd_log(int, char *[]);
78 static const struct got_error* cmd_diff(int, char *[]);
79 static const struct got_error* cmd_blame(int, char *[]);
80 static const struct got_error* cmd_tree(int, char *[]);
82 static struct tog_cmd tog_commands[] = {
83 { "log", cmd_log, usage_log,
84 "show repository history" },
85 { "diff", cmd_diff, usage_diff,
86 "compare files and directories" },
87 { "blame", cmd_blame, usage_blame,
88 "show line-by-line file history" },
89 { "tree", cmd_tree, usage_tree,
90 "browse trees in repository" },
100 struct tog_diff_view_state {
101 struct got_object_id *id1, *id2;
103 int first_displayed_line;
104 int last_displayed_line;
107 struct got_repository *repo;
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
119 struct commit_queue_head head;
122 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
124 struct tog_log_thread_args {
125 pthread_cond_t need_commits;
127 struct got_commit_graph *graph;
128 struct commit_queue *commits;
129 const char *in_repo_path;
130 struct got_object_id *start_id;
131 struct got_repository *repo;
134 struct tog_view *view;
135 struct commit_queue_entry **first_displayed_entry;
136 struct commit_queue_entry **selected_entry;
139 struct tog_log_view_state {
140 struct commit_queue commits;
141 struct commit_queue_entry *first_displayed_entry;
142 struct commit_queue_entry *last_displayed_entry;
143 struct commit_queue_entry *selected_entry;
146 struct got_repository *repo;
147 struct got_object_id *start_id;
150 struct tog_log_thread_args thread_args;
153 struct tog_blame_cb_args {
154 struct tog_blame_line *lines; /* one per line */
157 struct tog_view *view;
158 struct got_object_id *commit_id;
162 struct tog_blame_thread_args {
164 struct got_repository *repo;
165 struct tog_blame_cb_args *cb_args;
172 struct tog_blame_line *lines;
175 struct tog_blame_thread_args thread_args;
176 struct tog_blame_cb_args cb_args;
180 struct tog_blame_view_state {
181 int first_displayed_line;
182 int last_displayed_line;
187 struct got_object_id_queue blamed_commits;
188 struct got_object_qid *blamed_commit;
190 struct got_repository *repo;
191 struct got_object_id *commit_id;
192 struct tog_blame blame;
195 struct tog_parent_tree {
196 TAILQ_ENTRY(tog_parent_tree) entry;
197 struct got_tree_object *tree;
198 struct got_tree_entry *first_displayed_entry;
199 struct got_tree_entry *selected_entry;
203 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
205 struct tog_tree_view_state {
207 struct got_tree_object *root;
208 struct got_tree_object *tree;
209 const struct got_tree_entries *entries;
210 struct got_tree_entry *first_displayed_entry;
211 struct got_tree_entry *last_displayed_entry;
212 struct got_tree_entry *selected_entry;
213 int ndisplayed, selected, show_ids;
214 struct tog_parent_trees parents;
215 struct got_object_id *commit_id;
216 struct got_repository *repo;
220 * We implement two types of views: parent views and child views.
222 * The 'Tab' key switches between a parent view and its child view.
223 * Child views are shown side-by-side to their parent view, provided
224 * there is enough screen estate.
226 * When a new view is opened from within a parent view, this new view
227 * becomes a child view of the parent view, replacing any existing child.
229 * When a new view is opened from within a child view, this new view
230 * becomes a parent view which will obscure the views below until the
231 * user quits the new parent view by typing 'q'.
233 * This list of views contains parent views only.
234 * Child views are only pointed to by their parent view.
236 TAILQ_HEAD(tog_view_list_head, tog_view);
239 TAILQ_ENTRY(tog_view) entry;
242 int nlines, ncols, begin_y, begin_x;
243 int lines, cols; /* copies of LINES and COLS */
245 struct tog_view *parent;
246 struct tog_view *child;
249 /* type-specific state */
250 enum tog_view_type type;
252 struct tog_diff_view_state diff;
253 struct tog_log_view_state log;
254 struct tog_blame_view_state blame;
255 struct tog_tree_view_state tree;
258 const struct got_error *(*show)(struct tog_view *);
259 const struct got_error *(*input)(struct tog_view **,
260 struct tog_view **, struct tog_view**, struct tog_view *, int);
261 const struct got_error *(*close)(struct tog_view *);
264 static const struct got_error *open_diff_view(struct tog_view *,
265 struct got_object_id *, struct got_object_id *, struct got_repository *);
266 static const struct got_error *show_diff_view(struct tog_view *);
267 static const struct got_error *input_diff_view(struct tog_view **,
268 struct tog_view **, struct tog_view **, struct tog_view *, int);
269 static const struct got_error* close_diff_view(struct tog_view *);
271 static const struct got_error *open_log_view(struct tog_view *,
272 struct got_object_id *, struct got_repository *, const char *, int);
273 static const struct got_error * show_log_view(struct tog_view *);
274 static const struct got_error *input_log_view(struct tog_view **,
275 struct tog_view **, struct tog_view **, struct tog_view *, int);
276 static const struct got_error *close_log_view(struct tog_view *);
278 static const struct got_error *open_blame_view(struct tog_view *, char *,
279 struct got_object_id *, struct got_repository *);
280 static const struct got_error *show_blame_view(struct tog_view *);
281 static const struct got_error *input_blame_view(struct tog_view **,
282 struct tog_view **, struct tog_view **, struct tog_view *, int);
283 static const struct got_error *close_blame_view(struct tog_view *);
285 static const struct got_error *open_tree_view(struct tog_view *,
286 struct got_tree_object *, struct got_object_id *, struct got_repository *);
287 static const struct got_error *show_tree_view(struct tog_view *);
288 static const struct got_error *input_tree_view(struct tog_view **,
289 struct tog_view **, struct tog_view **, struct tog_view *, int);
290 static const struct got_error *close_tree_view(struct tog_view *);
292 static volatile sig_atomic_t tog_sigwinch_received;
295 tog_sigwinch(int signo)
297 tog_sigwinch_received = 1;
300 static const struct got_error *
301 view_close(struct tog_view *view)
303 const struct got_error *err = NULL;
306 view_close(view->child);
310 err = view->close(view);
312 del_panel(view->panel);
314 delwin(view->window);
319 static struct tog_view *
320 view_open(int nlines, int ncols, int begin_y, int begin_x,
321 enum tog_view_type type)
323 struct tog_view *view = calloc(1, sizeof(*view));
331 view->nlines = nlines ? nlines : LINES - begin_y;
332 view->ncols = ncols ? ncols : COLS - begin_x;
333 view->begin_y = begin_y;
334 view->begin_x = begin_x;
335 view->window = newwin(nlines, ncols, begin_y, begin_x);
336 if (view->window == NULL) {
340 view->panel = new_panel(view->window);
341 if (view->panel == NULL ||
342 set_panel_userptr(view->panel, view) != OK) {
347 keypad(view->window, TRUE);
352 view_split_begin_x(int begin_x)
354 if (begin_x > 0 || COLS < 120)
356 return (COLS - MAX(COLS / 2, 80));
359 static const struct got_error *view_resize(struct tog_view *);
361 static const struct got_error *
362 view_splitscreen(struct tog_view *view)
364 const struct got_error *err = NULL;
367 view->begin_x = view_split_begin_x(0);
368 view->nlines = LINES;
369 view->ncols = COLS - view->begin_x;
372 err = view_resize(view);
376 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
377 return got_error_from_errno();
382 static const struct got_error *
383 view_fullscreen(struct tog_view *view)
385 const struct got_error *err = NULL;
389 view->nlines = LINES;
393 err = view_resize(view);
397 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
398 return got_error_from_errno();
404 view_is_parent_view(struct tog_view *view)
406 return view->parent == NULL;
409 static const struct got_error *
410 view_resize(struct tog_view *view)
414 if (view->lines > LINES)
415 nlines = view->nlines - (view->lines - LINES);
417 nlines = view->nlines + (LINES - view->lines);
419 if (view->cols > COLS)
420 ncols = view->ncols - (view->cols - COLS);
422 ncols = view->ncols + (COLS - view->cols);
424 if (wresize(view->window, nlines, ncols) == ERR)
425 return got_error_from_errno();
426 if (replace_panel(view->panel, view->window) == ERR)
427 return got_error_from_errno();
428 wclear(view->window);
430 view->nlines = nlines;
436 view->child->begin_x = view_split_begin_x(view->begin_x);
437 if (view->child->begin_x == 0) {
438 view_fullscreen(view->child);
439 if (view->child->focussed)
440 show_panel(view->child->panel);
442 show_panel(view->panel);
444 view_splitscreen(view->child);
445 show_panel(view->child->panel);
452 static const struct got_error *
453 view_close_child(struct tog_view *view)
455 const struct got_error *err = NULL;
457 if (view->child == NULL)
460 err = view_close(view->child);
465 static const struct got_error *
466 view_set_child(struct tog_view *view, struct tog_view *child)
468 const struct got_error *err = NULL;
471 child->parent = view;
476 view_is_splitscreen(struct tog_view *view)
478 return !view_is_parent_view(view) && view->begin_x > 0;
487 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
488 cols = 80; /* Default */
494 resize_term(lines, cols);
497 static const struct got_error *
498 view_input(struct tog_view **new, struct tog_view **dead,
499 struct tog_view **focus, int *done, struct tog_view *view,
500 struct tog_view_list_head *views)
502 const struct got_error *err = NULL;
510 nodelay(stdscr, FALSE);
511 /* Allow threads to make progress while we are waiting for input. */
512 errcode = pthread_mutex_unlock(&tog_mutex);
514 return got_error_set_errno(errcode);
515 ch = wgetch(view->window);
516 errcode = pthread_mutex_lock(&tog_mutex);
518 return got_error_set_errno(errcode);
519 nodelay(stdscr, TRUE);
521 if (tog_sigwinch_received) {
523 tog_sigwinch_received = 0;
524 TAILQ_FOREACH(v, views, entry) {
525 err = view_resize(v);
528 err = v->input(new, dead, focus, v, KEY_RESIZE);
539 *focus = view->child;
540 view->child_focussed = 1;
541 } else if (view->parent) {
542 *focus = view->parent;
543 view->parent->child_focussed = 0;
547 err = view->input(new, dead, focus, view, ch);
554 if (view_is_parent_view(view)) {
555 if (view->child == NULL)
557 if (view_is_splitscreen(view->child)) {
558 *focus = view->child;
559 view->child_focussed = 1;
560 err = view_fullscreen(view->child);
562 err = view_splitscreen(view->child);
565 err = view->child->input(new, dead, focus,
566 view->child, KEY_RESIZE);
568 if (view_is_splitscreen(view)) {
570 view->parent->child_focussed = 1;
571 err = view_fullscreen(view);
573 err = view_splitscreen(view);
577 err = view->input(new, dead, focus, view,
584 err = view->input(new, dead, focus, view, ch);
592 view_vborder(struct tog_view *view)
595 struct tog_view *view_above;
598 return view_vborder(view->parent);
600 panel = panel_above(view->panel);
604 view_above = panel_userptr(panel);
605 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
606 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
610 view_needs_focus_indication(struct tog_view *view)
612 if (view_is_parent_view(view)) {
613 if (view->child == NULL || view->child_focussed)
615 if (!view_is_splitscreen(view->child))
617 } else if (!view_is_splitscreen(view))
620 return view->focussed;
623 static const struct got_error *
624 view_loop(struct tog_view *view)
626 const struct got_error *err = NULL;
627 struct tog_view_list_head views;
628 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
629 int fast_refresh = 10;
630 int done = 0, errcode;
632 errcode = pthread_mutex_lock(&tog_mutex);
634 return got_error_set_errno(errcode);
637 TAILQ_INSERT_HEAD(&views, view, entry);
641 err = view->show(view);
646 while (!TAILQ_EMPTY(&views) && !done) {
647 /* Refresh fast during initialization, then become slower. */
648 if (fast_refresh && fast_refresh-- == 0)
649 halfdelay(10); /* switch to once per second */
651 err = view_input(&new_view, &dead_view, &focus_view, &done,
656 struct tog_view *prev = NULL;
658 if (view_is_parent_view(dead_view))
659 prev = TAILQ_PREV(dead_view,
660 tog_view_list_head, entry);
661 else if (view->parent != dead_view)
664 if (dead_view->parent)
665 dead_view->parent->child = NULL;
667 TAILQ_REMOVE(&views, dead_view, entry);
669 err = view_close(dead_view);
670 if (err || dead_view == main_view)
673 if (view == dead_view) {
678 else if (!TAILQ_EMPTY(&views))
679 view = TAILQ_LAST(&views,
684 if (view->child && view->child_focussed)
685 focus_view = view->child;
692 struct tog_view *v, *t;
693 /* Only allow one parent view per type. */
694 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
695 if (v->type != new_view->type)
697 TAILQ_REMOVE(&views, v, entry);
703 TAILQ_INSERT_TAIL(&views, new_view, entry);
705 if (focus_view == NULL)
706 focus_view = new_view;
709 show_panel(focus_view->panel);
712 focus_view->focussed = 1;
715 show_panel(new_view->panel);
716 if (view->child && view_is_splitscreen(view->child))
717 show_panel(view->child->panel);
720 if (focus_view == NULL) {
722 show_panel(view->panel);
723 if (view->child && view_is_splitscreen(view->child))
724 show_panel(view->child->panel);
728 err = view->parent->show(view->parent);
732 err = view->show(view);
736 err = view->child->show(view->child);
745 while (!TAILQ_EMPTY(&views)) {
746 view = TAILQ_FIRST(&views);
747 TAILQ_REMOVE(&views, view, entry);
751 errcode = pthread_mutex_unlock(&tog_mutex);
753 return got_error_set_errno(errcode);
763 "usage: %s log [-c commit] [-r repository-path] [path]\n",
768 /* Create newly allocated wide-character string equivalent to a byte string. */
769 static const struct got_error *
770 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
773 const struct got_error *err = NULL;
776 *wlen = mbstowcs(NULL, s, 0);
777 if (*wlen == (size_t)-1) {
780 return got_error_from_errno();
782 /* byte string invalid in current encoding; try to "fix" it */
783 err = got_mbsavis(&vis, &vislen, s);
786 *wlen = mbstowcs(NULL, vis, 0);
787 if (*wlen == (size_t)-1) {
788 err = got_error_from_errno(); /* give up */
793 *ws = calloc(*wlen + 1, sizeof(*ws));
795 err = got_error_from_errno();
799 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
800 err = got_error_from_errno();
811 /* Format a line for display, ensuring that it won't overflow a width limit. */
812 static const struct got_error *
813 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
815 const struct got_error *err = NULL;
817 wchar_t *wline = NULL;
824 err = mbs2ws(&wline, &wlen, line);
829 while (i < wlen && cols < wlimit) {
830 int width = wcwidth(wline[i]);
837 if (cols + width <= wlimit)
842 if (wline[i] == L'\t')
843 cols += TABSIZE - ((cols + 1) % TABSIZE);
847 err = got_error_from_errno();
862 static const struct got_error *
863 draw_commit(struct tog_view *view, struct got_commit_object *commit,
864 struct got_object_id *id)
866 const struct got_error *err = NULL;
867 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
868 char *logmsg0 = NULL, *logmsg = NULL;
869 char *author0 = NULL, *author = NULL;
870 wchar_t *wlogmsg = NULL, *wauthor = NULL;
871 int author_width, logmsg_width;
872 char *newline, *smallerthan;
875 static const size_t date_display_cols = 9;
876 static const size_t author_display_cols = 16;
877 const int avail = view->ncols;
879 time_t committer_time;
881 committer_time = got_object_commit_get_committer_time(commit);
882 if (localtime_r(&committer_time, &tm) == NULL)
883 return got_error_from_errno();
884 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
886 return got_error(GOT_ERR_NO_SPACE);
888 if (avail < date_display_cols)
889 limit = MIN(sizeof(datebuf) - 1, avail);
891 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
892 waddnstr(view->window, datebuf, limit);
897 author0 = strdup(got_object_commit_get_author(commit));
898 if (author0 == NULL) {
899 err = got_error_from_errno();
903 smallerthan = strchr(author, '<');
907 char *at = strchr(author, '@');
912 err = format_line(&wauthor, &author_width, author, limit);
915 waddwstr(view->window, wauthor);
917 while (col <= avail && author_width < author_display_cols + 1) {
918 waddch(view->window, ' ');
925 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
926 if (logmsg0 == NULL) {
927 err = got_error_from_errno();
931 while (*logmsg == '\n')
933 newline = strchr(logmsg, '\n');
937 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
940 waddwstr(view->window, wlogmsg);
942 while (col <= avail) {
943 waddch(view->window, ' ');
955 static struct commit_queue_entry *
956 alloc_commit_queue_entry(struct got_commit_object *commit,
957 struct got_object_id *id)
959 struct commit_queue_entry *entry;
961 entry = calloc(1, sizeof(*entry));
966 entry->commit = commit;
971 pop_commit(struct commit_queue *commits)
973 struct commit_queue_entry *entry;
975 entry = TAILQ_FIRST(&commits->head);
976 TAILQ_REMOVE(&commits->head, entry, entry);
977 got_object_commit_close(entry->commit);
979 /* Don't free entry->id! It is owned by the commit graph. */
984 free_commits(struct commit_queue *commits)
986 while (!TAILQ_EMPTY(&commits->head))
990 static const struct got_error *
991 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
992 int minqueue, struct got_repository *repo, const char *path)
994 const struct got_error *err = NULL;
998 * We keep all commits open throughout the lifetime of the log
999 * view in order to avoid having to re-fetch commits from disk
1000 * while updating the display.
1002 while (nqueued < minqueue) {
1003 struct got_object_id *id;
1004 struct got_commit_object *commit;
1005 struct commit_queue_entry *entry;
1008 err = got_commit_graph_iter_next(&id, graph);
1010 if (err->code != GOT_ERR_ITER_NEED_MORE)
1012 err = got_commit_graph_fetch_commits(graph,
1022 err = got_object_open_as_commit(&commit, repo, id);
1025 entry = alloc_commit_queue_entry(commit, id);
1026 if (entry == NULL) {
1027 err = got_error_from_errno();
1031 errcode = pthread_mutex_lock(&tog_mutex);
1033 err = got_error_set_errno(errcode);
1037 entry->idx = commits->ncommits;
1038 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1040 commits->ncommits++;
1042 errcode = pthread_mutex_unlock(&tog_mutex);
1043 if (errcode && err == NULL)
1044 err = got_error_set_errno(errcode);
1050 static const struct got_error *
1051 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1053 const struct got_error *err = NULL;
1054 struct got_reference *head_ref;
1058 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1062 err = got_ref_resolve(head_id, repo, head_ref);
1063 got_ref_close(head_ref);
1072 static const struct got_error *
1073 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1074 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1075 struct commit_queue *commits, int selected_idx, int limit,
1076 const char *path, int commits_needed)
1078 const struct got_error *err = NULL;
1079 struct commit_queue_entry *entry;
1080 int ncommits, width;
1081 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1087 if (ncommits == selected_idx) {
1091 entry = TAILQ_NEXT(entry, entry);
1096 err = got_object_id_str(&id_str, (*selected)->id);
1101 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1102 entry ? entry->idx + 1 : 0, commits->ncommits,
1103 commits_needed == 0 ? "" : " loading...") == -1)
1104 return got_error_from_errno();
1106 if (path && strcmp(path, "/") != 0) {
1107 if (asprintf(&header, "commit %s %s%s",
1108 id_str ? id_str : "........................................",
1109 path, ncommits_str) == -1) {
1110 err = got_error_from_errno();
1114 } else if (asprintf(&header, "commit %s%s",
1115 id_str ? id_str : "........................................",
1116 ncommits_str) == -1) {
1117 err = got_error_from_errno();
1121 err = format_line(&wline, &width, header, view->ncols);
1125 werase(view->window);
1127 if (view_needs_focus_indication(view))
1128 wstandout(view->window);
1129 waddwstr(view->window, wline);
1130 while (width < view->ncols) {
1131 waddch(view->window, ' ');
1134 if (view_needs_focus_indication(view))
1135 wstandend(view->window);
1144 if (ncommits >= limit - 1)
1146 if (view->focussed && ncommits == selected_idx)
1147 wstandout(view->window);
1148 err = draw_commit(view, entry->commit, entry->id);
1149 if (view->focussed && ncommits == selected_idx)
1150 wstandend(view->window);
1155 entry = TAILQ_NEXT(entry, entry);
1167 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1168 struct commit_queue *commits)
1170 struct commit_queue_entry *entry;
1173 entry = TAILQ_FIRST(&commits->head);
1174 if (*first_displayed_entry == entry)
1177 entry = *first_displayed_entry;
1178 while (entry && nscrolled < maxscroll) {
1179 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1181 *first_displayed_entry = entry;
1187 static const struct got_error *
1188 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1189 struct commit_queue_entry **last_displayed_entry,
1190 struct commit_queue *commits, int *log_complete, int *commits_needed,
1191 pthread_cond_t *need_commits)
1193 const struct got_error *err = NULL;
1194 struct commit_queue_entry *pentry;
1197 if (*last_displayed_entry == NULL)
1201 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1202 if (pentry == NULL) {
1206 *commits_needed = maxscroll + 20;
1207 errcode = pthread_cond_signal(need_commits);
1209 return got_error_set_errno(errcode);
1212 *last_displayed_entry = pentry;
1214 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1217 *first_displayed_entry = pentry;
1218 } while (++nscrolled < maxscroll);
1223 static const struct got_error *
1224 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1225 struct got_object_id *commit_id, struct got_commit_object *commit,
1226 struct got_repository *repo)
1228 const struct got_error *err;
1229 struct got_object_qid *parent_id;
1230 struct tog_view *diff_view;
1232 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1233 if (diff_view == NULL)
1234 return got_error_from_errno();
1236 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1237 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1240 *new_view = diff_view;
1244 static const struct got_error *
1245 browse_commit(struct tog_view **new_view, int begin_x,
1246 struct commit_queue_entry *entry, struct got_repository *repo)
1248 const struct got_error *err = NULL;
1249 struct got_tree_object *tree;
1250 struct tog_view *tree_view;
1252 err = got_object_open_as_tree(&tree, repo,
1253 got_object_commit_get_tree_id(entry->commit));
1257 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1258 if (tree_view == NULL)
1259 return got_error_from_errno();
1261 err = open_tree_view(tree_view, tree, entry->id, repo);
1263 got_object_tree_close(tree);
1265 *new_view = tree_view;
1270 log_thread(void *arg)
1272 const struct got_error *err = NULL;
1274 struct tog_log_thread_args *a = arg;
1277 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1281 while (!done && !err) {
1282 err = queue_commits(a->graph, a->commits, 1, a->repo,
1285 if (err->code != GOT_ERR_ITER_COMPLETED)
1289 } else if (a->commits_needed > 0)
1290 a->commits_needed--;
1292 errcode = pthread_mutex_lock(&tog_mutex);
1294 return (void *)got_error_set_errno(errcode);
1297 a->log_complete = 1;
1298 else if (*a->quit) {
1300 a->log_complete = 1;
1301 } else if (*a->first_displayed_entry == NULL) {
1302 *a->first_displayed_entry =
1303 TAILQ_FIRST(&a->commits->head);
1304 *a->selected_entry = *a->first_displayed_entry;
1308 a->commits_needed = 0;
1309 else if (a->commits_needed == 0) {
1310 errcode = pthread_cond_wait(&a->need_commits,
1313 err = got_error_set_errno(errcode);
1316 errcode = pthread_mutex_unlock(&tog_mutex);
1317 if (errcode && err == NULL)
1318 err = got_error_set_errno(errcode);
1323 static const struct got_error *
1324 stop_log_thread(struct tog_log_view_state *s)
1326 const struct got_error *err = NULL;
1331 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1333 return got_error_set_errno(errcode);
1334 errcode = pthread_mutex_unlock(&tog_mutex);
1336 return got_error_set_errno(errcode);
1337 errcode = pthread_join(s->thread, (void **)&err);
1339 return got_error_set_errno(errcode);
1340 errcode = pthread_mutex_lock(&tog_mutex);
1342 return got_error_set_errno(errcode);
1346 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1347 if (errcode && err == NULL)
1348 err = got_error_set_errno(errcode);
1350 if (s->thread_args.repo) {
1351 got_repo_close(s->thread_args.repo);
1352 s->thread_args.repo = NULL;
1355 if (s->thread_args.graph) {
1356 got_commit_graph_close(s->thread_args.graph);
1357 s->thread_args.graph = NULL;
1363 static const struct got_error *
1364 close_log_view(struct tog_view *view)
1366 const struct got_error *err = NULL;
1367 struct tog_log_view_state *s = &view->state.log;
1369 err = stop_log_thread(s);
1370 free_commits(&s->commits);
1371 free(s->in_repo_path);
1372 s->in_repo_path = NULL;
1378 static const struct got_error *
1379 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1380 struct got_repository *repo, const char *path, int check_disk)
1382 const struct got_error *err = NULL;
1383 struct tog_log_view_state *s = &view->state.log;
1384 struct got_repository *thread_repo = NULL;
1385 struct got_commit_graph *thread_graph = NULL;
1388 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1392 /* The commit queue only contains commits being displayed. */
1393 TAILQ_INIT(&s->commits.head);
1394 s->commits.ncommits = 0;
1397 s->start_id = got_object_id_dup(start_id);
1398 if (s->start_id == NULL) {
1399 err = got_error_from_errno();
1403 view->show = show_log_view;
1404 view->input = input_log_view;
1405 view->close = close_log_view;
1407 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1410 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1415 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1417 err = got_error_set_errno(errcode);
1421 s->thread_args.commits_needed = view->nlines;
1422 s->thread_args.graph = thread_graph;
1423 s->thread_args.commits = &s->commits;
1424 s->thread_args.in_repo_path = s->in_repo_path;
1425 s->thread_args.start_id = s->start_id;
1426 s->thread_args.repo = thread_repo;
1427 s->thread_args.log_complete = 0;
1428 s->thread_args.quit = &s->quit;
1429 s->thread_args.view = view;
1430 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1431 s->thread_args.selected_entry = &s->selected_entry;
1434 close_log_view(view);
1438 static const struct got_error *
1439 show_log_view(struct tog_view *view)
1441 struct tog_log_view_state *s = &view->state.log;
1443 if (s->thread == NULL) {
1444 int errcode = pthread_create(&s->thread, NULL, log_thread,
1447 return got_error_set_errno(errcode);
1450 return draw_commits(view, &s->last_displayed_entry,
1451 &s->selected_entry, s->first_displayed_entry,
1452 &s->commits, s->selected, view->nlines,
1453 s->in_repo_path, s->thread_args.commits_needed);
1456 static const struct got_error *
1457 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1458 struct tog_view **focus_view, struct tog_view *view, int ch)
1460 const struct got_error *err = NULL;
1461 struct tog_log_view_state *s = &view->state.log;
1463 struct tog_view *diff_view = NULL, *tree_view = NULL;
1472 if (s->first_displayed_entry == NULL)
1474 if (s->selected > 0)
1476 if (s->selected > 0)
1478 scroll_up(&s->first_displayed_entry, 1,
1482 if (s->first_displayed_entry == NULL)
1484 if (TAILQ_FIRST(&s->commits.head) ==
1485 s->first_displayed_entry) {
1489 scroll_up(&s->first_displayed_entry,
1490 view->nlines, &s->commits);
1494 if (s->first_displayed_entry == NULL)
1496 if (s->selected < MIN(view->nlines - 2,
1497 s->commits.ncommits - 1)) {
1501 err = scroll_down(&s->first_displayed_entry, 1,
1502 &s->last_displayed_entry, &s->commits,
1503 &s->thread_args.log_complete,
1504 &s->thread_args.commits_needed,
1505 &s->thread_args.need_commits);
1508 struct commit_queue_entry *first;
1509 first = s->first_displayed_entry;
1512 err = scroll_down(&s->first_displayed_entry,
1513 view->nlines, &s->last_displayed_entry,
1514 &s->commits, &s->thread_args.log_complete,
1515 &s->thread_args.commits_needed,
1516 &s->thread_args.need_commits);
1517 if (first == s->first_displayed_entry &&
1518 s->selected < MIN(view->nlines - 2,
1519 s->commits.ncommits - 1)) {
1520 /* can't scroll further down */
1521 s->selected = MIN(view->nlines - 2,
1522 s->commits.ncommits - 1);
1528 if (s->selected > view->nlines - 2)
1529 s->selected = view->nlines - 2;
1530 if (s->selected > s->commits.ncommits - 1)
1531 s->selected = s->commits.ncommits - 1;
1535 if (s->selected_entry == NULL)
1537 if (view_is_parent_view(view))
1538 begin_x = view_split_begin_x(view->begin_x);
1539 err = open_diff_view_for_commit(&diff_view, begin_x,
1540 s->selected_entry->id, s->selected_entry->commit,
1544 if (view_is_parent_view(view)) {
1545 err = view_close_child(view);
1548 err = view_set_child(view, diff_view);
1550 view_close(diff_view);
1553 *focus_view = diff_view;
1554 view->child_focussed = 1;
1556 *new_view = diff_view;
1559 if (s->selected_entry == NULL)
1561 if (view_is_parent_view(view))
1562 begin_x = view_split_begin_x(view->begin_x);
1563 err = browse_commit(&tree_view, begin_x,
1564 s->selected_entry, s->repo);
1567 if (view_is_parent_view(view)) {
1568 err = view_close_child(view);
1571 err = view_set_child(view, tree_view);
1573 view_close(tree_view);
1576 *focus_view = tree_view;
1577 view->child_focussed = 1;
1579 *new_view = tree_view;
1582 if (strcmp(s->in_repo_path, "/") == 0)
1584 parent_path = dirname(s->in_repo_path);
1585 if (parent_path && strcmp(parent_path, ".") != 0) {
1586 struct tog_view *lv;
1587 err = stop_log_thread(s);
1590 lv = view_open(view->nlines, view->ncols,
1591 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1593 return got_error_from_errno();
1594 err = open_log_view(lv, s->start_id, s->repo,
1598 if (view_is_parent_view(view))
1601 view_set_child(view->parent, lv);
1614 static const struct got_error *
1615 apply_unveil(const char *repo_path, const char *worktree_path)
1617 const struct got_error *error;
1619 if (repo_path && unveil(repo_path, "r") != 0)
1620 return got_error_from_errno();
1622 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1623 return got_error_from_errno();
1625 if (unveil("/tmp", "rwc") != 0)
1626 return got_error_from_errno();
1628 error = got_privsep_unveil_exec_helpers();
1632 if (unveil(NULL, NULL) != 0)
1633 return got_error_from_errno();
1638 static const struct got_error *
1639 cmd_log(int argc, char *argv[])
1641 const struct got_error *error;
1642 struct got_repository *repo = NULL;
1643 struct got_object_id *start_id = NULL;
1644 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1645 char *start_commit = NULL;
1647 struct tog_view *view;
1650 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1655 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1658 start_commit = optarg;
1661 repo_path = realpath(optarg, NULL);
1662 if (repo_path == NULL)
1663 err(1, "-r option");
1677 path = strdup(argv[0]);
1681 return got_error_from_errno();
1683 cwd = getcwd(NULL, 0);
1685 error = got_error_from_errno();
1688 if (repo_path == NULL) {
1689 repo_path = strdup(cwd);
1690 if (repo_path == NULL) {
1691 error = got_error_from_errno();
1696 error = apply_unveil(repo_path, NULL);
1700 error = got_repo_open(&repo, repo_path);
1704 if (start_commit == NULL)
1705 error = get_head_commit_id(&start_id, repo);
1707 error = got_object_resolve_id_str(&start_id, repo,
1712 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1714 error = got_error_from_errno();
1717 error = open_log_view(view, start_id, repo, path, 1);
1720 error = view_loop(view);
1727 got_repo_close(repo);
1735 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1741 parse_next_line(FILE *f, size_t *len)
1746 const char delim[3] = { '\0', '\0', '\0'};
1748 line = fparseln(f, &linelen, &lineno, delim, 0);
1754 static const struct got_error *
1755 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1756 int *last_displayed_line, int *eof, int max_lines,
1759 const struct got_error *err;
1760 int nlines = 0, nprinted = 0;
1767 werase(view->window);
1770 err = format_line(&wline, &width, header, view->ncols);
1775 if (view_needs_focus_indication(view))
1776 wstandout(view->window);
1777 waddwstr(view->window, wline);
1778 if (view_needs_focus_indication(view))
1779 wstandend(view->window);
1780 if (width < view->ncols)
1781 waddch(view->window, '\n');
1789 while (nprinted < max_lines) {
1790 line = parse_next_line(f, &len);
1795 if (++nlines < *first_displayed_line) {
1800 err = format_line(&wline, &width, line, view->ncols);
1805 waddwstr(view->window, wline);
1806 if (width < view->ncols)
1807 waddch(view->window, '\n');
1808 if (++nprinted == 1)
1809 *first_displayed_line = nlines;
1814 *last_displayed_line = nlines;
1822 get_datestr(time_t *time, char *datebuf)
1824 char *p, *s = ctime_r(time, datebuf);
1825 p = strchr(s, '\n');
1831 static const struct got_error *
1832 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1835 const struct got_error *err = NULL;
1837 struct got_commit_object *commit;
1838 char *id_str = NULL;
1839 time_t committer_time;
1840 const char *author, *committer;
1842 err = got_object_open_as_commit(&commit, repo, commit_id);
1846 err = got_object_id_str(&id_str, commit_id);
1848 err = got_error_from_errno();
1852 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1853 err = got_error_from_errno();
1856 if (fprintf(outfile, "from: %s\n",
1857 got_object_commit_get_author(commit)) < 0) {
1858 err = got_error_from_errno();
1861 committer_time = got_object_commit_get_committer_time(commit);
1862 if (fprintf(outfile, "date: %s UTC\n",
1863 get_datestr(&committer_time, datebuf)) < 0) {
1864 err = got_error_from_errno();
1867 author = got_object_commit_get_author(commit);
1868 committer = got_object_commit_get_committer(commit);
1869 if (strcmp(author, committer) != 0 &&
1870 fprintf(outfile, "via: %s\n", committer) < 0) {
1871 err = got_error_from_errno();
1874 if (fprintf(outfile, "%s\n",
1875 got_object_commit_get_logmsg(commit)) < 0) {
1876 err = got_error_from_errno();
1881 got_object_commit_close(commit);
1885 static const struct got_error *
1886 create_diff(struct tog_diff_view_state *s)
1888 const struct got_error *err = NULL;
1894 err = got_error_from_errno();
1902 err = got_object_get_type(&obj_type, s->repo, s->id1);
1904 err = got_object_get_type(&obj_type, s->repo, s->id2);
1909 case GOT_OBJ_TYPE_BLOB:
1910 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1911 s->diff_context, s->repo, f);
1913 case GOT_OBJ_TYPE_TREE:
1914 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1915 s->diff_context, s->repo, f);
1917 case GOT_OBJ_TYPE_COMMIT: {
1918 const struct got_object_id_queue *parent_ids;
1919 struct got_object_qid *pid;
1920 struct got_commit_object *commit2;
1922 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1925 /* Show commit info if we're diffing to a parent commit. */
1926 parent_ids = got_object_commit_get_parent_ids(commit2);
1927 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1928 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1929 write_commit_info(s->id2, s->repo, f);
1933 got_object_commit_close(commit2);
1935 err = got_diff_objects_as_commits(s->id1, s->id2,
1936 s->diff_context, s->repo, f);
1940 err = got_error(GOT_ERR_OBJ_TYPE);
1949 static const struct got_error *
1950 open_diff_view(struct tog_view *view, struct got_object_id *id1,
1951 struct got_object_id *id2, struct got_repository *repo)
1953 const struct got_error *err;
1955 if (id1 != NULL && id2 != NULL) {
1957 err = got_object_get_type(&type1, repo, id1);
1960 err = got_object_get_type(&type2, repo, id2);
1965 return got_error(GOT_ERR_OBJ_TYPE);
1969 view->state.diff.id1 = got_object_id_dup(id1);
1970 if (view->state.diff.id1 == NULL)
1971 return got_error_from_errno();
1973 view->state.diff.id1 = NULL;
1975 view->state.diff.id2 = got_object_id_dup(id2);
1976 if (view->state.diff.id2 == NULL) {
1977 free(view->state.diff.id1);
1978 view->state.diff.id1 = NULL;
1979 return got_error_from_errno();
1981 view->state.diff.f = NULL;
1982 view->state.diff.first_displayed_line = 1;
1983 view->state.diff.last_displayed_line = view->nlines;
1984 view->state.diff.diff_context = 3;
1985 view->state.diff.repo = repo;
1987 err = create_diff(&view->state.diff);
1989 free(view->state.diff.id1);
1990 view->state.diff.id1 = NULL;
1991 free(view->state.diff.id2);
1992 view->state.diff.id2 = NULL;
1996 view->show = show_diff_view;
1997 view->input = input_diff_view;
1998 view->close = close_diff_view;
2003 static const struct got_error *
2004 close_diff_view(struct tog_view *view)
2006 const struct got_error *err = NULL;
2008 free(view->state.diff.id1);
2009 view->state.diff.id1 = NULL;
2010 free(view->state.diff.id2);
2011 view->state.diff.id2 = NULL;
2012 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2013 err = got_error_from_errno();
2017 static const struct got_error *
2018 show_diff_view(struct tog_view *view)
2020 const struct got_error *err;
2021 struct tog_diff_view_state *s = &view->state.diff;
2022 char *id_str1 = NULL, *id_str2, *header;
2025 err = got_object_id_str(&id_str1, s->id1);
2029 err = got_object_id_str(&id_str2, s->id2);
2033 if (asprintf(&header, "diff %s %s",
2034 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2035 err = got_error_from_errno();
2043 return draw_file(view, s->f, &s->first_displayed_line,
2044 &s->last_displayed_line, &s->eof, view->nlines,
2048 static const struct got_error *
2049 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2050 struct tog_view **focus_view, struct tog_view *view, int ch)
2052 const struct got_error *err = NULL;
2053 struct tog_diff_view_state *s = &view->state.diff;
2059 if (s->first_displayed_line > 1)
2060 s->first_displayed_line--;
2064 while (i++ < view->nlines - 1 &&
2065 s->first_displayed_line > 1)
2066 s->first_displayed_line--;
2071 s->first_displayed_line++;
2076 while (!s->eof && i++ < view->nlines - 1) {
2078 line = parse_next_line(s->f, NULL);
2079 s->first_displayed_line++;
2085 if (s->diff_context > 0) {
2087 err = create_diff(s);
2091 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2093 err = create_diff(s);
2103 static const struct got_error *
2104 cmd_diff(int argc, char *argv[])
2106 const struct got_error *error = NULL;
2107 struct got_repository *repo = NULL;
2108 struct got_object_id *id1 = NULL, *id2 = NULL;
2109 char *repo_path = NULL;
2110 char *id_str1 = NULL, *id_str2 = NULL;
2112 struct tog_view *view;
2115 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2120 while ((ch = getopt(argc, argv, "")) != -1) {
2132 usage_diff(); /* TODO show local worktree changes */
2133 } else if (argc == 2) {
2134 repo_path = getcwd(NULL, 0);
2135 if (repo_path == NULL)
2136 return got_error_from_errno();
2139 } else if (argc == 3) {
2140 repo_path = realpath(argv[0], NULL);
2141 if (repo_path == NULL)
2142 return got_error_from_errno();
2148 error = apply_unveil(repo_path, NULL);
2152 error = got_repo_open(&repo, repo_path);
2157 error = got_object_resolve_id_str(&id1, repo, id_str1);
2161 error = got_object_resolve_id_str(&id2, repo, id_str2);
2165 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2167 error = got_error_from_errno();
2170 error = open_diff_view(view, id1, id2, repo);
2173 error = view_loop(view);
2175 got_repo_close(repo);
2183 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2188 struct tog_blame_line {
2190 struct got_object_id *id;
2193 static const struct got_error *
2194 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2195 const char *path, struct tog_blame_line *lines, int nlines,
2196 int blame_complete, int selected_line, int *first_displayed_line,
2197 int *last_displayed_line, int *eof, int max_lines)
2199 const struct got_error *err;
2200 int lineno = 0, nprinted = 0;
2205 struct tog_blame_line *blame_line;
2206 struct got_object_id *prev_id = NULL;
2209 err = got_object_id_str(&id_str, id);
2214 werase(view->window);
2216 if (asprintf(&line, "commit %s", id_str) == -1) {
2217 err = got_error_from_errno();
2222 err = format_line(&wline, &width, line, view->ncols);
2225 if (view_needs_focus_indication(view))
2226 wstandout(view->window);
2227 waddwstr(view->window, wline);
2228 if (view_needs_focus_indication(view))
2229 wstandend(view->window);
2232 if (width < view->ncols)
2233 waddch(view->window, '\n');
2235 if (asprintf(&line, "[%d/%d] %s%s",
2236 *first_displayed_line - 1 + selected_line, nlines,
2237 blame_complete ? "" : "annotating ", path) == -1) {
2239 return got_error_from_errno();
2242 err = format_line(&wline, &width, line, view->ncols);
2247 waddwstr(view->window, wline);
2250 if (width < view->ncols)
2251 waddch(view->window, '\n');
2254 while (nprinted < max_lines - 2) {
2255 line = parse_next_line(f, &len);
2260 if (++lineno < *first_displayed_line) {
2265 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2266 err = format_line(&wline, &width, line, wlimit);
2272 if (view->focussed && nprinted == selected_line - 1)
2273 wstandout(view->window);
2275 blame_line = &lines[lineno - 1];
2276 if (blame_line->annotated && prev_id &&
2277 got_object_id_cmp(prev_id, blame_line->id) == 0)
2278 waddstr(view->window, " ");
2279 else if (blame_line->annotated) {
2281 err = got_object_id_str(&id_str, blame_line->id);
2287 wprintw(view->window, "%.8s ", id_str);
2289 prev_id = blame_line->id;
2291 waddstr(view->window, "........ ");
2295 waddwstr(view->window, wline);
2296 while (width < wlimit) {
2297 waddch(view->window, ' ');
2300 if (view->focussed && nprinted == selected_line - 1)
2301 wstandend(view->window);
2302 if (++nprinted == 1)
2303 *first_displayed_line = lineno;
2308 *last_displayed_line = lineno;
2315 static const struct got_error *
2316 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2318 const struct got_error *err = NULL;
2319 struct tog_blame_cb_args *a = arg;
2320 struct tog_blame_line *line;
2323 if (nlines != a->nlines ||
2324 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2325 return got_error(GOT_ERR_RANGE);
2327 errcode = pthread_mutex_lock(&tog_mutex);
2329 return got_error_set_errno(errcode);
2331 if (*a->quit) { /* user has quit the blame view */
2332 err = got_error(GOT_ERR_ITER_COMPLETED);
2337 goto done; /* no change in this commit */
2339 line = &a->lines[lineno - 1];
2340 if (line->annotated)
2343 line->id = got_object_id_dup(id);
2344 if (line->id == NULL) {
2345 err = got_error_from_errno();
2348 line->annotated = 1;
2350 errcode = pthread_mutex_unlock(&tog_mutex);
2352 err = got_error_set_errno(errcode);
2357 blame_thread(void *arg)
2359 const struct got_error *err;
2360 struct tog_blame_thread_args *ta = arg;
2361 struct tog_blame_cb_args *a = ta->cb_args;
2364 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2365 blame_cb, ta->cb_args);
2367 errcode = pthread_mutex_lock(&tog_mutex);
2369 return (void *)got_error_set_errno(errcode);
2371 got_repo_close(ta->repo);
2375 errcode = pthread_mutex_unlock(&tog_mutex);
2376 if (errcode && err == NULL)
2377 err = got_error_set_errno(errcode);
2382 static struct got_object_id *
2383 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2386 struct tog_blame_line *line;
2388 line = &lines[first_displayed_line - 1 + selected_line - 1];
2389 if (!line->annotated)
2395 static const struct got_error *
2396 stop_blame(struct tog_blame *blame)
2398 const struct got_error *err = NULL;
2401 if (blame->thread) {
2403 errcode = pthread_mutex_unlock(&tog_mutex);
2405 return got_error_set_errno(errcode);
2406 errcode = pthread_join(blame->thread, (void **)&err);
2408 return got_error_set_errno(errcode);
2409 errcode = pthread_mutex_lock(&tog_mutex);
2411 return got_error_set_errno(errcode);
2412 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2414 blame->thread = NULL;
2416 if (blame->thread_args.repo) {
2417 got_repo_close(blame->thread_args.repo);
2418 blame->thread_args.repo = NULL;
2425 for (i = 0; i < blame->nlines; i++)
2426 free(blame->lines[i].id);
2428 blame->lines = NULL;
2430 free(blame->cb_args.commit_id);
2431 blame->cb_args.commit_id = NULL;
2436 static const struct got_error *
2437 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2438 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2439 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2440 struct got_repository *repo)
2442 const struct got_error *err = NULL;
2443 struct got_blob_object *blob = NULL;
2444 struct got_repository *thread_repo = NULL;
2445 struct got_object_id *obj_id = NULL;
2448 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2452 return got_error(GOT_ERR_NO_OBJ);
2454 err = got_object_get_type(&obj_type, repo, obj_id);
2458 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2459 err = got_error(GOT_ERR_OBJ_TYPE);
2463 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2466 blame->f = got_opentemp();
2467 if (blame->f == NULL) {
2468 err = got_error_from_errno();
2471 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2476 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2477 if (blame->lines == NULL) {
2478 err = got_error_from_errno();
2482 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2486 blame->cb_args.view = view;
2487 blame->cb_args.lines = blame->lines;
2488 blame->cb_args.nlines = blame->nlines;
2489 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2490 if (blame->cb_args.commit_id == NULL) {
2491 err = got_error_from_errno();
2494 blame->cb_args.quit = done;
2496 blame->thread_args.path = path;
2497 blame->thread_args.repo = thread_repo;
2498 blame->thread_args.cb_args = &blame->cb_args;
2499 blame->thread_args.complete = blame_complete;
2500 *blame_complete = 0;
2504 got_object_blob_close(blob);
2511 static const struct got_error *
2512 open_blame_view(struct tog_view *view, char *path,
2513 struct got_object_id *commit_id, struct got_repository *repo)
2515 const struct got_error *err = NULL;
2516 struct tog_blame_view_state *s = &view->state.blame;
2518 SIMPLEQ_INIT(&s->blamed_commits);
2520 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2524 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2525 s->first_displayed_line = 1;
2526 s->last_displayed_line = view->nlines;
2527 s->selected_line = 1;
2528 s->blame_complete = 0;
2530 if (s->path == NULL)
2531 return got_error_from_errno();
2533 s->commit_id = commit_id;
2534 memset(&s->blame, 0, sizeof(s->blame));
2536 view->show = show_blame_view;
2537 view->input = input_blame_view;
2538 view->close = close_blame_view;
2540 return run_blame(&s->blame, view, &s->blame_complete,
2541 &s->first_displayed_line, &s->last_displayed_line,
2542 &s->selected_line, &s->done, &s->eof, s->path,
2543 s->blamed_commit->id, s->repo);
2546 static const struct got_error *
2547 close_blame_view(struct tog_view *view)
2549 const struct got_error *err = NULL;
2550 struct tog_blame_view_state *s = &view->state.blame;
2552 if (s->blame.thread)
2553 err = stop_blame(&s->blame);
2555 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2556 struct got_object_qid *blamed_commit;
2557 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2558 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2559 got_object_qid_free(blamed_commit);
2567 static const struct got_error *
2568 show_blame_view(struct tog_view *view)
2570 const struct got_error *err = NULL;
2571 struct tog_blame_view_state *s = &view->state.blame;
2574 if (s->blame.thread == NULL) {
2575 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2576 &s->blame.thread_args);
2578 return got_error_set_errno(errcode);
2581 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2582 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2583 s->selected_line, &s->first_displayed_line,
2584 &s->last_displayed_line, &s->eof, view->nlines);
2590 static const struct got_error *
2591 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2592 struct tog_view **focus_view, struct tog_view *view, int ch)
2594 const struct got_error *err = NULL, *thread_err = NULL;
2595 struct tog_view *diff_view;
2596 struct tog_blame_view_state *s = &view->state.blame;
2605 if (s->selected_line > 1)
2607 else if (s->selected_line == 1 &&
2608 s->first_displayed_line > 1)
2609 s->first_displayed_line--;
2612 if (s->first_displayed_line == 1) {
2613 s->selected_line = 1;
2616 if (s->first_displayed_line > view->nlines - 2)
2617 s->first_displayed_line -=
2620 s->first_displayed_line = 1;
2624 if (s->selected_line < view->nlines - 2 &&
2625 s->first_displayed_line +
2626 s->selected_line <= s->blame.nlines)
2628 else if (s->last_displayed_line <
2630 s->first_displayed_line++;
2634 struct got_object_id *id = NULL;
2635 id = get_selected_commit_id(s->blame.lines,
2636 s->first_displayed_line, s->selected_line);
2640 struct got_commit_object *commit;
2641 struct got_object_qid *pid;
2642 struct got_object_id *blob_id = NULL;
2644 err = got_object_open_as_commit(&commit,
2648 pid = SIMPLEQ_FIRST(
2649 got_object_commit_get_parent_ids(commit));
2651 got_object_commit_close(commit);
2654 /* Check if path history ends here. */
2655 err = got_object_id_by_path(&blob_id, s->repo,
2658 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2660 got_object_commit_close(commit);
2663 err = got_object_get_type(&obj_type, s->repo,
2666 /* Can't blame non-blob type objects. */
2667 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2668 got_object_commit_close(commit);
2671 err = got_object_qid_alloc(&s->blamed_commit,
2673 got_object_commit_close(commit);
2675 if (got_object_id_cmp(id,
2676 s->blamed_commit->id) == 0)
2678 err = got_object_qid_alloc(&s->blamed_commit,
2684 thread_err = stop_blame(&s->blame);
2688 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2689 s->blamed_commit, entry);
2690 err = run_blame(&s->blame, view, &s->blame_complete,
2691 &s->first_displayed_line, &s->last_displayed_line,
2692 &s->selected_line, &s->done, &s->eof,
2693 s->path, s->blamed_commit->id, s->repo);
2699 struct got_object_qid *first;
2700 first = SIMPLEQ_FIRST(&s->blamed_commits);
2701 if (!got_object_id_cmp(first->id, s->commit_id))
2704 thread_err = stop_blame(&s->blame);
2708 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2709 got_object_qid_free(s->blamed_commit);
2711 SIMPLEQ_FIRST(&s->blamed_commits);
2712 err = run_blame(&s->blame, view, &s->blame_complete,
2713 &s->first_displayed_line, &s->last_displayed_line,
2714 &s->selected_line, &s->done, &s->eof, s->path,
2715 s->blamed_commit->id, s->repo);
2722 struct got_object_id *id = NULL;
2723 struct got_object_qid *pid;
2724 struct got_commit_object *commit = NULL;
2725 id = get_selected_commit_id(s->blame.lines,
2726 s->first_displayed_line, s->selected_line);
2729 err = got_object_open_as_commit(&commit, s->repo, id);
2732 pid = SIMPLEQ_FIRST(
2733 got_object_commit_get_parent_ids(commit));
2734 if (view_is_parent_view(view))
2735 begin_x = view_split_begin_x(view->begin_x);
2736 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2737 if (diff_view == NULL) {
2738 got_object_commit_close(commit);
2739 err = got_error_from_errno();
2742 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2744 got_object_commit_close(commit);
2746 view_close(diff_view);
2749 if (view_is_parent_view(view)) {
2750 err = view_close_child(view);
2753 err = view_set_child(view, diff_view);
2755 view_close(diff_view);
2758 *focus_view = diff_view;
2759 view->child_focussed = 1;
2761 *new_view = diff_view;
2768 if (s->last_displayed_line >= s->blame.nlines &&
2769 s->selected_line < view->nlines - 2) {
2770 s->selected_line = MIN(s->blame.nlines,
2774 if (s->last_displayed_line + view->nlines - 2
2776 s->first_displayed_line +=
2779 s->first_displayed_line =
2784 if (s->selected_line > view->nlines - 2) {
2785 s->selected_line = MIN(s->blame.nlines,
2792 return thread_err ? thread_err : err;
2795 static const struct got_error *
2796 cmd_blame(int argc, char *argv[])
2798 const struct got_error *error;
2799 struct got_repository *repo = NULL;
2800 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2801 struct got_object_id *commit_id = NULL;
2802 char *commit_id_str = NULL;
2804 struct tog_view *view;
2807 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2812 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2815 commit_id_str = optarg;
2818 repo_path = realpath(optarg, NULL);
2819 if (repo_path == NULL)
2820 err(1, "-r option");
2836 cwd = getcwd(NULL, 0);
2838 error = got_error_from_errno();
2841 if (repo_path == NULL) {
2842 repo_path = strdup(cwd);
2843 if (repo_path == NULL) {
2844 error = got_error_from_errno();
2849 error = apply_unveil(repo_path, NULL);
2853 error = got_repo_open(&repo, repo_path);
2857 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2861 if (commit_id_str == NULL) {
2862 struct got_reference *head_ref;
2863 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2866 error = got_ref_resolve(&commit_id, repo, head_ref);
2867 got_ref_close(head_ref);
2869 error = got_object_resolve_id_str(&commit_id, repo,
2875 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2877 error = got_error_from_errno();
2880 error = open_blame_view(view, in_repo_path, commit_id, repo);
2883 error = view_loop(view);
2889 got_repo_close(repo);
2893 static const struct got_error *
2894 draw_tree_entries(struct tog_view *view,
2895 struct got_tree_entry **first_displayed_entry,
2896 struct got_tree_entry **last_displayed_entry,
2897 struct got_tree_entry **selected_entry, int *ndisplayed,
2898 const char *label, int show_ids, const char *parent_path,
2899 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2901 const struct got_error *err = NULL;
2902 struct got_tree_entry *te;
2908 werase(view->window);
2913 err = format_line(&wline, &width, label, view->ncols);
2916 if (view_needs_focus_indication(view))
2917 wstandout(view->window);
2918 waddwstr(view->window, wline);
2919 if (view_needs_focus_indication(view))
2920 wstandend(view->window);
2923 if (width < view->ncols)
2924 waddch(view->window, '\n');
2927 err = format_line(&wline, &width, parent_path, view->ncols);
2930 waddwstr(view->window, wline);
2933 if (width < view->ncols)
2934 waddch(view->window, '\n');
2937 waddch(view->window, '\n');
2941 te = SIMPLEQ_FIRST(&entries->head);
2942 if (*first_displayed_entry == NULL) {
2943 if (selected == 0) {
2945 wstandout(view->window);
2946 *selected_entry = NULL;
2948 waddstr(view->window, " ..\n"); /* parent directory */
2949 if (selected == 0 && view->focussed)
2950 wstandend(view->window);
2957 while (te != *first_displayed_entry)
2958 te = SIMPLEQ_NEXT(te, entry);
2962 char *line = NULL, *id_str = NULL;
2965 err = got_object_id_str(&id_str, te->id);
2967 return got_error_from_errno();
2969 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2970 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2972 return got_error_from_errno();
2975 err = format_line(&wline, &width, line, view->ncols);
2980 if (n == selected) {
2982 wstandout(view->window);
2983 *selected_entry = te;
2985 waddwstr(view->window, wline);
2986 if (width < view->ncols)
2987 waddch(view->window, '\n');
2988 if (n == selected && view->focussed)
2989 wstandend(view->window);
2995 *last_displayed_entry = te;
2998 te = SIMPLEQ_NEXT(te, entry);
3005 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3006 const struct got_tree_entries *entries, int isroot)
3008 struct got_tree_entry *te, *prev;
3011 if (*first_displayed_entry == NULL)
3014 te = SIMPLEQ_FIRST(&entries->head);
3015 if (*first_displayed_entry == te) {
3017 *first_displayed_entry = NULL;
3021 /* XXX this is stupid... switch to TAILQ? */
3022 for (i = 0; i < maxscroll; i++) {
3023 while (te != *first_displayed_entry) {
3025 te = SIMPLEQ_NEXT(te, entry);
3027 *first_displayed_entry = prev;
3028 te = SIMPLEQ_FIRST(&entries->head);
3030 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3031 *first_displayed_entry = NULL;
3035 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3036 struct got_tree_entry *last_displayed_entry,
3037 const struct got_tree_entries *entries)
3039 struct got_tree_entry *next, *last;
3042 if (*first_displayed_entry)
3043 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3045 next = SIMPLEQ_FIRST(&entries->head);
3046 last = last_displayed_entry;
3047 while (next && last && n++ < maxscroll) {
3048 last = SIMPLEQ_NEXT(last, entry);
3050 *first_displayed_entry = next;
3051 next = SIMPLEQ_NEXT(next, entry);
3057 static const struct got_error *
3058 tree_entry_path(char **path, struct tog_parent_trees *parents,
3059 struct got_tree_entry *te)
3061 const struct got_error *err = NULL;
3062 struct tog_parent_tree *pt;
3063 size_t len = 2; /* for leading slash and NUL */
3065 TAILQ_FOREACH(pt, parents, entry)
3066 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3068 len += strlen(te->name);
3070 *path = calloc(1, len);
3072 return got_error_from_errno();
3075 pt = TAILQ_LAST(parents, tog_parent_trees);
3077 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3078 err = got_error(GOT_ERR_NO_SPACE);
3081 if (strlcat(*path, "/", len) >= len) {
3082 err = got_error(GOT_ERR_NO_SPACE);
3085 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3088 if (strlcat(*path, te->name, len) >= len) {
3089 err = got_error(GOT_ERR_NO_SPACE);
3101 static const struct got_error *
3102 blame_tree_entry(struct tog_view **new_view, int begin_x,
3103 struct got_tree_entry *te, struct tog_parent_trees *parents,
3104 struct got_object_id *commit_id, struct got_repository *repo)
3106 const struct got_error *err = NULL;
3108 struct tog_view *blame_view;
3110 err = tree_entry_path(&path, parents, te);
3114 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3115 if (blame_view == NULL)
3116 return got_error_from_errno();
3118 err = open_blame_view(blame_view, path, commit_id, repo);
3120 view_close(blame_view);
3123 *new_view = blame_view;
3127 static const struct got_error *
3128 log_tree_entry(struct tog_view **new_view, int begin_x,
3129 struct got_tree_entry *te, struct tog_parent_trees *parents,
3130 struct got_object_id *commit_id, struct got_repository *repo)
3132 struct tog_view *log_view;
3133 const struct got_error *err = NULL;
3136 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3137 if (log_view == NULL)
3138 return got_error_from_errno();
3140 err = tree_entry_path(&path, parents, te);
3144 err = open_log_view(log_view, commit_id, repo, path, 0);
3146 view_close(log_view);
3148 *new_view = log_view;
3153 static const struct got_error *
3154 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3155 struct got_object_id *commit_id, struct got_repository *repo)
3157 const struct got_error *err = NULL;
3158 char *commit_id_str = NULL;
3159 struct tog_tree_view_state *s = &view->state.tree;
3161 TAILQ_INIT(&s->parents);
3163 err = got_object_id_str(&commit_id_str, commit_id);
3167 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3168 err = got_error_from_errno();
3172 s->root = s->tree = root;
3173 s->entries = got_object_tree_get_entries(root);
3174 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3175 s->commit_id = got_object_id_dup(commit_id);
3176 if (s->commit_id == NULL) {
3177 err = got_error_from_errno();
3182 view->show = show_tree_view;
3183 view->input = input_tree_view;
3184 view->close = close_tree_view;
3186 free(commit_id_str);
3188 free(s->tree_label);
3189 s->tree_label = NULL;
3194 static const struct got_error *
3195 close_tree_view(struct tog_view *view)
3197 struct tog_tree_view_state *s = &view->state.tree;
3199 free(s->tree_label);
3200 s->tree_label = NULL;
3202 s->commit_id = NULL;
3203 while (!TAILQ_EMPTY(&s->parents)) {
3204 struct tog_parent_tree *parent;
3205 parent = TAILQ_FIRST(&s->parents);
3206 TAILQ_REMOVE(&s->parents, parent, entry);
3210 if (s->tree != s->root)
3211 got_object_tree_close(s->tree);
3212 got_object_tree_close(s->root);
3217 static const struct got_error *
3218 show_tree_view(struct tog_view *view)
3220 const struct got_error *err = NULL;
3221 struct tog_tree_view_state *s = &view->state.tree;
3224 err = tree_entry_path(&parent_path, &s->parents, NULL);
3228 err = draw_tree_entries(view, &s->first_displayed_entry,
3229 &s->last_displayed_entry, &s->selected_entry,
3230 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3231 s->entries, s->selected, view->nlines, s->tree == s->root);
3238 static const struct got_error *
3239 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3240 struct tog_view **focus_view, struct tog_view *view, int ch)
3242 const struct got_error *err = NULL;
3243 struct tog_tree_view_state *s = &view->state.tree;
3244 struct tog_view *log_view;
3245 int begin_x = 0, nscrolled;
3249 s->show_ids = !s->show_ids;
3252 if (!s->selected_entry)
3254 if (view_is_parent_view(view))
3255 begin_x = view_split_begin_x(view->begin_x);
3256 err = log_tree_entry(&log_view, begin_x,
3257 s->selected_entry, &s->parents,
3258 s->commit_id, s->repo);
3259 if (view_is_parent_view(view)) {
3260 err = view_close_child(view);
3263 err = view_set_child(view, log_view);
3265 view_close(log_view);
3268 *focus_view = log_view;
3269 view->child_focussed = 1;
3271 *new_view = log_view;
3275 if (s->selected > 0) {
3277 if (s->selected == 0)
3280 if (s->selected > 0)
3282 tree_scroll_up(&s->first_displayed_entry, 1,
3283 s->entries, s->tree == s->root);
3286 tree_scroll_up(&s->first_displayed_entry,
3287 MAX(0, view->nlines - 4 - s->selected), s->entries,
3288 s->tree == s->root);
3290 if (SIMPLEQ_FIRST(&s->entries->head) ==
3291 s->first_displayed_entry && s->tree != s->root)
3292 s->first_displayed_entry = NULL;
3296 if (s->selected < s->ndisplayed - 1) {
3300 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3302 /* can't scroll any further */
3305 tree_scroll_down(&s->first_displayed_entry, 1,
3306 s->last_displayed_entry, s->entries);
3309 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3311 /* can't scroll any further; move cursor down */
3312 if (s->selected < s->ndisplayed - 1)
3313 s->selected = s->ndisplayed - 1;
3316 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3317 view->nlines, s->last_displayed_entry, s->entries);
3318 if (nscrolled < view->nlines) {
3320 struct got_tree_entry *te;
3321 te = s->first_displayed_entry;
3324 te = SIMPLEQ_NEXT(te, entry);
3326 s->selected = ndisplayed - 1;
3331 if (s->selected_entry == NULL) {
3332 struct tog_parent_tree *parent;
3334 /* user selected '..' */
3335 if (s->tree == s->root)
3337 parent = TAILQ_FIRST(&s->parents);
3338 TAILQ_REMOVE(&s->parents, parent,
3340 got_object_tree_close(s->tree);
3341 s->tree = parent->tree;
3343 got_object_tree_get_entries(s->tree);
3344 s->first_displayed_entry =
3345 parent->first_displayed_entry;
3347 parent->selected_entry;
3348 s->selected = parent->selected;
3350 } else if (S_ISDIR(s->selected_entry->mode)) {
3351 struct tog_parent_tree *parent;
3352 struct got_tree_object *child;
3353 err = got_object_open_as_tree(&child,
3354 s->repo, s->selected_entry->id);
3357 parent = calloc(1, sizeof(*parent));
3358 if (parent == NULL) {
3359 err = got_error_from_errno();
3362 parent->tree = s->tree;
3363 parent->first_displayed_entry =
3364 s->first_displayed_entry;
3365 parent->selected_entry = s->selected_entry;
3366 parent->selected = s->selected;
3367 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3370 got_object_tree_get_entries(s->tree);
3372 s->first_displayed_entry = NULL;
3373 } else if (S_ISREG(s->selected_entry->mode)) {
3374 struct tog_view *blame_view;
3375 int begin_x = view_is_parent_view(view) ?
3376 view_split_begin_x(view->begin_x) : 0;
3378 err = blame_tree_entry(&blame_view, begin_x,
3379 s->selected_entry, &s->parents, s->commit_id,
3383 if (view_is_parent_view(view)) {
3384 err = view_close_child(view);
3387 err = view_set_child(view, blame_view);
3389 view_close(blame_view);
3392 *focus_view = blame_view;
3393 view->child_focussed = 1;
3395 *new_view = blame_view;
3399 if (s->selected > view->nlines)
3400 s->selected = s->ndisplayed - 1;
3413 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3418 static const struct got_error *
3419 cmd_tree(int argc, char *argv[])
3421 const struct got_error *error;
3422 struct got_repository *repo = NULL;
3423 char *repo_path = NULL;
3424 struct got_object_id *commit_id = NULL;
3425 char *commit_id_arg = NULL;
3426 struct got_commit_object *commit = NULL;
3427 struct got_tree_object *tree = NULL;
3429 struct tog_view *view;
3432 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3437 while ((ch = getopt(argc, argv, "c:")) != -1) {
3440 commit_id_arg = optarg;
3452 repo_path = getcwd(NULL, 0);
3453 if (repo_path == NULL)
3454 return got_error_from_errno();
3455 } else if (argc == 1) {
3456 repo_path = realpath(argv[0], NULL);
3457 if (repo_path == NULL)
3458 return got_error_from_errno();
3462 error = apply_unveil(repo_path, NULL);
3466 error = got_repo_open(&repo, repo_path);
3471 if (commit_id_arg == NULL)
3472 error = get_head_commit_id(&commit_id, repo);
3474 error = got_object_resolve_id_str(&commit_id, repo,
3479 error = got_object_open_as_commit(&commit, repo, commit_id);
3483 error = got_object_open_as_tree(&tree, repo,
3484 got_object_commit_get_tree_id(commit));
3488 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3490 error = got_error_from_errno();
3493 error = open_tree_view(view, tree, commit_id, repo);
3496 error = view_loop(view);
3500 got_object_commit_close(commit);
3502 got_object_tree_close(tree);
3504 got_repo_close(repo);
3513 halfdelay(1); /* Do fast refresh while initial view is loading. */
3516 intrflush(stdscr, FALSE);
3517 keypad(stdscr, TRUE);
3519 signal(SIGWINCH, tog_sigwinch);
3527 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3528 "Available commands:\n", getprogname());
3529 for (i = 0; i < nitems(tog_commands); i++) {
3530 struct tog_cmd *cmd = &tog_commands[i];
3531 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3537 make_argv(const char *arg0, const char *arg1)
3540 int argc = (arg1 == NULL ? 1 : 2);
3542 argv = calloc(argc, sizeof(char *));
3545 argv[0] = strdup(arg0);
3546 if (argv[0] == NULL)
3549 argv[1] = strdup(arg1);
3550 if (argv[1] == NULL)
3558 main(int argc, char *argv[])
3560 const struct got_error *error = NULL;
3561 struct tog_cmd *cmd = NULL;
3563 char **cmd_argv = NULL;
3565 setlocale(LC_CTYPE, "");
3567 while ((ch = getopt(argc, argv, "h")) != -1) {
3586 /* Build an argument vector which runs a default command. */
3587 cmd = &tog_commands[0];
3588 cmd_argv = make_argv(cmd->name, NULL);
3593 /* Did the user specific a command? */
3594 for (i = 0; i < nitems(tog_commands); i++) {
3595 if (strncmp(tog_commands[i].name, argv[0],
3596 strlen(argv[0])) == 0) {
3597 cmd = &tog_commands[i];
3599 tog_commands[i].cmd_usage();
3604 /* Did the user specify a repository? */
3605 char *repo_path = realpath(argv[0], NULL);
3607 struct got_repository *repo;
3608 error = got_repo_open(&repo, repo_path);
3610 got_repo_close(repo);
3612 error = got_error_from_errno();
3615 fprintf(stderr, "%s: '%s' is not a "
3616 "known command\n", getprogname(),
3620 fprintf(stderr, "%s: '%s' is neither a known "
3621 "command nor a path to a repository\n",
3622 getprogname(), argv[0]);
3626 cmd = &tog_commands[0];
3627 cmd_argv = make_argv(cmd->name, repo_path);
3635 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3642 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);