2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_worktree.h"
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
72 __dead static void usage(void);
73 __dead static void usage_log(void);
74 __dead static void usage_diff(void);
75 __dead static void usage_blame(void);
76 __dead static void usage_tree(void);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
83 static struct tog_cmd tog_commands[] = {
84 { "log", cmd_log, usage_log,
85 "show repository history" },
86 { "diff", cmd_diff, usage_diff,
87 "compare files and directories" },
88 { "blame", cmd_blame, usage_blame,
89 "show line-by-line file history" },
90 { "tree", cmd_tree, usage_tree,
91 "browse trees in repository" },
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
110 struct commit_queue_head head;
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
116 int first_displayed_line;
117 int last_displayed_line;
120 struct got_repository *repo;
122 /* passed from log view; may be NULL */
123 struct tog_view *log_view;
126 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
128 struct tog_log_thread_args {
129 pthread_cond_t need_commits;
131 struct got_commit_graph *graph;
132 struct commit_queue *commits;
133 const char *in_repo_path;
134 struct got_object_id *start_id;
135 struct got_repository *repo;
138 struct tog_view *view;
139 struct commit_queue_entry **first_displayed_entry;
140 struct commit_queue_entry **selected_entry;
143 struct tog_log_view_state {
144 struct commit_queue commits;
145 struct commit_queue_entry *first_displayed_entry;
146 struct commit_queue_entry *last_displayed_entry;
147 struct commit_queue_entry *selected_entry;
150 struct got_repository *repo;
151 struct got_object_id *start_id;
154 struct tog_log_thread_args thread_args;
157 struct tog_blame_cb_args {
158 struct tog_blame_line *lines; /* one per line */
161 struct tog_view *view;
162 struct got_object_id *commit_id;
166 struct tog_blame_thread_args {
168 struct got_repository *repo;
169 struct tog_blame_cb_args *cb_args;
176 struct tog_blame_line *lines;
179 struct tog_blame_thread_args thread_args;
180 struct tog_blame_cb_args cb_args;
184 struct tog_blame_view_state {
185 int first_displayed_line;
186 int last_displayed_line;
191 struct got_object_id_queue blamed_commits;
192 struct got_object_qid *blamed_commit;
194 struct got_repository *repo;
195 struct got_object_id *commit_id;
196 struct tog_blame blame;
199 struct tog_parent_tree {
200 TAILQ_ENTRY(tog_parent_tree) entry;
201 struct got_tree_object *tree;
202 struct got_tree_entry *first_displayed_entry;
203 struct got_tree_entry *selected_entry;
207 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
209 struct tog_tree_view_state {
211 struct got_tree_object *root;
212 struct got_tree_object *tree;
213 const struct got_tree_entries *entries;
214 struct got_tree_entry *first_displayed_entry;
215 struct got_tree_entry *last_displayed_entry;
216 struct got_tree_entry *selected_entry;
217 int ndisplayed, selected, show_ids;
218 struct tog_parent_trees parents;
219 struct got_object_id *commit_id;
220 struct got_repository *repo;
224 * We implement two types of views: parent views and child views.
226 * The 'Tab' key switches between a parent view and its child view.
227 * Child views are shown side-by-side to their parent view, provided
228 * there is enough screen estate.
230 * When a new view is opened from within a parent view, this new view
231 * becomes a child view of the parent view, replacing any existing child.
233 * When a new view is opened from within a child view, this new view
234 * becomes a parent view which will obscure the views below until the
235 * user quits the new parent view by typing 'q'.
237 * This list of views contains parent views only.
238 * Child views are only pointed to by their parent view.
240 TAILQ_HEAD(tog_view_list_head, tog_view);
243 TAILQ_ENTRY(tog_view) entry;
246 int nlines, ncols, begin_y, begin_x;
247 int lines, cols; /* copies of LINES and COLS */
249 struct tog_view *parent;
250 struct tog_view *child;
253 /* type-specific state */
254 enum tog_view_type type;
256 struct tog_diff_view_state diff;
257 struct tog_log_view_state log;
258 struct tog_blame_view_state blame;
259 struct tog_tree_view_state tree;
262 const struct got_error *(*show)(struct tog_view *);
263 const struct got_error *(*input)(struct tog_view **,
264 struct tog_view **, struct tog_view**, struct tog_view *, int);
265 const struct got_error *(*close)(struct tog_view *);
268 static const struct got_error *open_diff_view(struct tog_view *,
269 struct got_object_id *, struct got_object_id *, struct tog_view *,
270 struct got_repository *);
271 static const struct got_error *show_diff_view(struct tog_view *);
272 static const struct got_error *input_diff_view(struct tog_view **,
273 struct tog_view **, struct tog_view **, struct tog_view *, int);
274 static const struct got_error* close_diff_view(struct tog_view *);
276 static const struct got_error *open_log_view(struct tog_view *,
277 struct got_object_id *, struct got_repository *, const char *, int);
278 static const struct got_error * show_log_view(struct tog_view *);
279 static const struct got_error *input_log_view(struct tog_view **,
280 struct tog_view **, struct tog_view **, struct tog_view *, int);
281 static const struct got_error *close_log_view(struct tog_view *);
283 static const struct got_error *open_blame_view(struct tog_view *, char *,
284 struct got_object_id *, struct got_repository *);
285 static const struct got_error *show_blame_view(struct tog_view *);
286 static const struct got_error *input_blame_view(struct tog_view **,
287 struct tog_view **, struct tog_view **, struct tog_view *, int);
288 static const struct got_error *close_blame_view(struct tog_view *);
290 static const struct got_error *open_tree_view(struct tog_view *,
291 struct got_tree_object *, struct got_object_id *, struct got_repository *);
292 static const struct got_error *show_tree_view(struct tog_view *);
293 static const struct got_error *input_tree_view(struct tog_view **,
294 struct tog_view **, struct tog_view **, struct tog_view *, int);
295 static const struct got_error *close_tree_view(struct tog_view *);
297 static volatile sig_atomic_t tog_sigwinch_received;
300 tog_sigwinch(int signo)
302 tog_sigwinch_received = 1;
305 static const struct got_error *
306 view_close(struct tog_view *view)
308 const struct got_error *err = NULL;
311 view_close(view->child);
315 err = view->close(view);
317 del_panel(view->panel);
319 delwin(view->window);
324 static struct tog_view *
325 view_open(int nlines, int ncols, int begin_y, int begin_x,
326 enum tog_view_type type)
328 struct tog_view *view = calloc(1, sizeof(*view));
336 view->nlines = nlines ? nlines : LINES - begin_y;
337 view->ncols = ncols ? ncols : COLS - begin_x;
338 view->begin_y = begin_y;
339 view->begin_x = begin_x;
340 view->window = newwin(nlines, ncols, begin_y, begin_x);
341 if (view->window == NULL) {
345 view->panel = new_panel(view->window);
346 if (view->panel == NULL ||
347 set_panel_userptr(view->panel, view) != OK) {
352 keypad(view->window, TRUE);
357 view_split_begin_x(int begin_x)
359 if (begin_x > 0 || COLS < 120)
361 return (COLS - MAX(COLS / 2, 80));
364 static const struct got_error *view_resize(struct tog_view *);
366 static const struct got_error *
367 view_splitscreen(struct tog_view *view)
369 const struct got_error *err = NULL;
372 view->begin_x = view_split_begin_x(0);
373 view->nlines = LINES;
374 view->ncols = COLS - view->begin_x;
377 err = view_resize(view);
381 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
382 return got_error_from_errno();
387 static const struct got_error *
388 view_fullscreen(struct tog_view *view)
390 const struct got_error *err = NULL;
394 view->nlines = LINES;
398 err = view_resize(view);
402 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
403 return got_error_from_errno();
409 view_is_parent_view(struct tog_view *view)
411 return view->parent == NULL;
414 static const struct got_error *
415 view_resize(struct tog_view *view)
419 if (view->lines > LINES)
420 nlines = view->nlines - (view->lines - LINES);
422 nlines = view->nlines + (LINES - view->lines);
424 if (view->cols > COLS)
425 ncols = view->ncols - (view->cols - COLS);
427 ncols = view->ncols + (COLS - view->cols);
429 if (wresize(view->window, nlines, ncols) == ERR)
430 return got_error_from_errno();
431 if (replace_panel(view->panel, view->window) == ERR)
432 return got_error_from_errno();
433 wclear(view->window);
435 view->nlines = nlines;
441 view->child->begin_x = view_split_begin_x(view->begin_x);
442 if (view->child->begin_x == 0) {
443 view_fullscreen(view->child);
444 if (view->child->focussed)
445 show_panel(view->child->panel);
447 show_panel(view->panel);
449 view_splitscreen(view->child);
450 show_panel(view->child->panel);
457 static const struct got_error *
458 view_close_child(struct tog_view *view)
460 const struct got_error *err = NULL;
462 if (view->child == NULL)
465 err = view_close(view->child);
470 static const struct got_error *
471 view_set_child(struct tog_view *view, struct tog_view *child)
473 const struct got_error *err = NULL;
476 child->parent = view;
481 view_is_splitscreen(struct tog_view *view)
483 return !view_is_parent_view(view) && view->begin_x > 0;
492 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
493 cols = 80; /* Default */
499 resize_term(lines, cols);
502 static const struct got_error *
503 view_input(struct tog_view **new, struct tog_view **dead,
504 struct tog_view **focus, int *done, struct tog_view *view,
505 struct tog_view_list_head *views)
507 const struct got_error *err = NULL;
515 nodelay(stdscr, FALSE);
516 /* Allow threads to make progress while we are waiting for input. */
517 errcode = pthread_mutex_unlock(&tog_mutex);
519 return got_error_set_errno(errcode);
520 ch = wgetch(view->window);
521 errcode = pthread_mutex_lock(&tog_mutex);
523 return got_error_set_errno(errcode);
524 nodelay(stdscr, TRUE);
526 if (tog_sigwinch_received) {
528 tog_sigwinch_received = 0;
529 TAILQ_FOREACH(v, views, entry) {
530 err = view_resize(v);
533 err = v->input(new, dead, focus, v, KEY_RESIZE);
544 *focus = view->child;
545 view->child_focussed = 1;
546 } else if (view->parent) {
547 *focus = view->parent;
548 view->parent->child_focussed = 0;
552 err = view->input(new, dead, focus, view, ch);
559 if (view_is_parent_view(view)) {
560 if (view->child == NULL)
562 if (view_is_splitscreen(view->child)) {
563 *focus = view->child;
564 view->child_focussed = 1;
565 err = view_fullscreen(view->child);
567 err = view_splitscreen(view->child);
570 err = view->child->input(new, dead, focus,
571 view->child, KEY_RESIZE);
573 if (view_is_splitscreen(view)) {
575 view->parent->child_focussed = 1;
576 err = view_fullscreen(view);
578 err = view_splitscreen(view);
582 err = view->input(new, dead, focus, view,
589 err = view->input(new, dead, focus, view, ch);
597 view_vborder(struct tog_view *view)
600 struct tog_view *view_above;
603 return view_vborder(view->parent);
605 panel = panel_above(view->panel);
609 view_above = panel_userptr(panel);
610 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
611 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
615 view_needs_focus_indication(struct tog_view *view)
617 if (view_is_parent_view(view)) {
618 if (view->child == NULL || view->child_focussed)
620 if (!view_is_splitscreen(view->child))
622 } else if (!view_is_splitscreen(view))
625 return view->focussed;
628 static const struct got_error *
629 view_loop(struct tog_view *view)
631 const struct got_error *err = NULL;
632 struct tog_view_list_head views;
633 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
634 int fast_refresh = 10;
635 int done = 0, errcode;
637 errcode = pthread_mutex_lock(&tog_mutex);
639 return got_error_set_errno(errcode);
642 TAILQ_INSERT_HEAD(&views, view, entry);
646 err = view->show(view);
651 while (!TAILQ_EMPTY(&views) && !done) {
652 /* Refresh fast during initialization, then become slower. */
653 if (fast_refresh && fast_refresh-- == 0)
654 halfdelay(10); /* switch to once per second */
656 err = view_input(&new_view, &dead_view, &focus_view, &done,
661 struct tog_view *prev = NULL;
663 if (view_is_parent_view(dead_view))
664 prev = TAILQ_PREV(dead_view,
665 tog_view_list_head, entry);
666 else if (view->parent != dead_view)
669 if (dead_view->parent)
670 dead_view->parent->child = NULL;
672 TAILQ_REMOVE(&views, dead_view, entry);
674 err = view_close(dead_view);
675 if (err || dead_view == main_view)
678 if (view == dead_view) {
683 else if (!TAILQ_EMPTY(&views))
684 view = TAILQ_LAST(&views,
689 if (view->child && view->child_focussed)
690 focus_view = view->child;
697 struct tog_view *v, *t;
698 /* Only allow one parent view per type. */
699 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
700 if (v->type != new_view->type)
702 TAILQ_REMOVE(&views, v, entry);
708 TAILQ_INSERT_TAIL(&views, new_view, entry);
710 if (focus_view == NULL)
711 focus_view = new_view;
714 show_panel(focus_view->panel);
717 focus_view->focussed = 1;
720 show_panel(new_view->panel);
721 if (view->child && view_is_splitscreen(view->child))
722 show_panel(view->child->panel);
725 if (focus_view == NULL) {
727 show_panel(view->panel);
728 if (view->child && view_is_splitscreen(view->child))
729 show_panel(view->child->panel);
733 err = view->parent->show(view->parent);
737 err = view->show(view);
741 err = view->child->show(view->child);
750 while (!TAILQ_EMPTY(&views)) {
751 view = TAILQ_FIRST(&views);
752 TAILQ_REMOVE(&views, view, entry);
756 errcode = pthread_mutex_unlock(&tog_mutex);
758 return got_error_set_errno(errcode);
768 "usage: %s log [-c commit] [-r repository-path] [path]\n",
773 /* Create newly allocated wide-character string equivalent to a byte string. */
774 static const struct got_error *
775 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
778 const struct got_error *err = NULL;
781 *wlen = mbstowcs(NULL, s, 0);
782 if (*wlen == (size_t)-1) {
785 return got_error_from_errno();
787 /* byte string invalid in current encoding; try to "fix" it */
788 err = got_mbsavis(&vis, &vislen, s);
791 *wlen = mbstowcs(NULL, vis, 0);
792 if (*wlen == (size_t)-1) {
793 err = got_error_from_errno(); /* give up */
798 *ws = calloc(*wlen + 1, sizeof(*ws));
800 err = got_error_from_errno();
804 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
805 err = got_error_from_errno();
816 /* Format a line for display, ensuring that it won't overflow a width limit. */
817 static const struct got_error *
818 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
820 const struct got_error *err = NULL;
822 wchar_t *wline = NULL;
829 err = mbs2ws(&wline, &wlen, line);
834 while (i < wlen && cols < wlimit) {
835 int width = wcwidth(wline[i]);
842 if (cols + width <= wlimit)
847 if (wline[i] == L'\t')
848 cols += TABSIZE - ((cols + 1) % TABSIZE);
852 err = got_error_from_errno();
867 static const struct got_error *
868 draw_commit(struct tog_view *view, struct got_commit_object *commit,
869 struct got_object_id *id)
871 const struct got_error *err = NULL;
872 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
873 char *logmsg0 = NULL, *logmsg = NULL;
874 char *author0 = NULL, *author = NULL;
875 wchar_t *wlogmsg = NULL, *wauthor = NULL;
876 int author_width, logmsg_width;
877 char *newline, *smallerthan;
880 static const size_t date_display_cols = 9;
881 static const size_t author_display_cols = 16;
882 const int avail = view->ncols;
884 time_t committer_time;
886 committer_time = got_object_commit_get_committer_time(commit);
887 if (localtime_r(&committer_time, &tm) == NULL)
888 return got_error_from_errno();
889 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
891 return got_error(GOT_ERR_NO_SPACE);
893 if (avail < date_display_cols)
894 limit = MIN(sizeof(datebuf) - 1, avail);
896 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
897 waddnstr(view->window, datebuf, limit);
902 author0 = strdup(got_object_commit_get_author(commit));
903 if (author0 == NULL) {
904 err = got_error_from_errno();
908 smallerthan = strchr(author, '<');
912 char *at = strchr(author, '@');
917 err = format_line(&wauthor, &author_width, author, limit);
920 waddwstr(view->window, wauthor);
922 while (col <= avail && author_width < author_display_cols + 1) {
923 waddch(view->window, ' ');
930 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
931 if (logmsg0 == NULL) {
932 err = got_error_from_errno();
936 while (*logmsg == '\n')
938 newline = strchr(logmsg, '\n');
942 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
945 waddwstr(view->window, wlogmsg);
947 while (col <= avail) {
948 waddch(view->window, ' ');
960 static struct commit_queue_entry *
961 alloc_commit_queue_entry(struct got_commit_object *commit,
962 struct got_object_id *id)
964 struct commit_queue_entry *entry;
966 entry = calloc(1, sizeof(*entry));
971 entry->commit = commit;
976 pop_commit(struct commit_queue *commits)
978 struct commit_queue_entry *entry;
980 entry = TAILQ_FIRST(&commits->head);
981 TAILQ_REMOVE(&commits->head, entry, entry);
982 got_object_commit_close(entry->commit);
984 /* Don't free entry->id! It is owned by the commit graph. */
989 free_commits(struct commit_queue *commits)
991 while (!TAILQ_EMPTY(&commits->head))
995 static const struct got_error *
996 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
997 int minqueue, struct got_repository *repo, const char *path)
999 const struct got_error *err = NULL;
1003 * We keep all commits open throughout the lifetime of the log
1004 * view in order to avoid having to re-fetch commits from disk
1005 * while updating the display.
1007 while (nqueued < minqueue) {
1008 struct got_object_id *id;
1009 struct got_commit_object *commit;
1010 struct commit_queue_entry *entry;
1013 err = got_commit_graph_iter_next(&id, graph);
1015 if (err->code != GOT_ERR_ITER_NEED_MORE)
1017 err = got_commit_graph_fetch_commits(graph,
1027 err = got_object_open_as_commit(&commit, repo, id);
1030 entry = alloc_commit_queue_entry(commit, id);
1031 if (entry == NULL) {
1032 err = got_error_from_errno();
1036 errcode = pthread_mutex_lock(&tog_mutex);
1038 err = got_error_set_errno(errcode);
1042 entry->idx = commits->ncommits;
1043 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1045 commits->ncommits++;
1047 errcode = pthread_mutex_unlock(&tog_mutex);
1048 if (errcode && err == NULL)
1049 err = got_error_set_errno(errcode);
1055 static const struct got_error *
1056 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1058 const struct got_error *err = NULL;
1059 struct got_reference *head_ref;
1063 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1067 err = got_ref_resolve(head_id, repo, head_ref);
1068 got_ref_close(head_ref);
1077 static const struct got_error *
1078 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1079 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1080 struct commit_queue *commits, int selected_idx, int limit,
1081 const char *path, int commits_needed)
1083 const struct got_error *err = NULL;
1084 struct commit_queue_entry *entry;
1085 int ncommits, width;
1086 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1092 if (ncommits == selected_idx) {
1096 entry = TAILQ_NEXT(entry, entry);
1101 err = got_object_id_str(&id_str, (*selected)->id);
1106 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1107 entry ? entry->idx + 1 : 0, commits->ncommits,
1108 commits_needed == 0 ? "" : " loading...") == -1)
1109 return got_error_from_errno();
1111 if (path && strcmp(path, "/") != 0) {
1112 if (asprintf(&header, "commit %s %s%s",
1113 id_str ? id_str : "........................................",
1114 path, ncommits_str) == -1) {
1115 err = got_error_from_errno();
1119 } else if (asprintf(&header, "commit %s%s",
1120 id_str ? id_str : "........................................",
1121 ncommits_str) == -1) {
1122 err = got_error_from_errno();
1126 err = format_line(&wline, &width, header, view->ncols);
1130 werase(view->window);
1132 if (view_needs_focus_indication(view))
1133 wstandout(view->window);
1134 waddwstr(view->window, wline);
1135 while (width < view->ncols) {
1136 waddch(view->window, ' ');
1139 if (view_needs_focus_indication(view))
1140 wstandend(view->window);
1149 if (ncommits >= limit - 1)
1151 if (view->focussed && ncommits == selected_idx)
1152 wstandout(view->window);
1153 err = draw_commit(view, entry->commit, entry->id);
1154 if (view->focussed && ncommits == selected_idx)
1155 wstandend(view->window);
1160 entry = TAILQ_NEXT(entry, entry);
1172 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1173 struct commit_queue *commits)
1175 struct commit_queue_entry *entry;
1178 entry = TAILQ_FIRST(&commits->head);
1179 if (*first_displayed_entry == entry)
1182 entry = *first_displayed_entry;
1183 while (entry && nscrolled < maxscroll) {
1184 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1186 *first_displayed_entry = entry;
1192 static const struct got_error *
1193 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1194 struct commit_queue_entry **last_displayed_entry,
1195 struct commit_queue *commits, int *log_complete, int *commits_needed,
1196 pthread_cond_t *need_commits)
1198 const struct got_error *err = NULL;
1199 struct commit_queue_entry *pentry;
1202 if (*last_displayed_entry == NULL)
1206 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1207 while (pentry == NULL) {
1211 *commits_needed = maxscroll + 20;
1212 errcode = pthread_cond_signal(need_commits);
1214 return got_error_set_errno(errcode);
1215 errcode = pthread_mutex_unlock(&tog_mutex);
1217 return got_error_set_errno(errcode);
1219 errcode = pthread_mutex_lock(&tog_mutex);
1221 return got_error_set_errno(errcode);
1222 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1227 *last_displayed_entry = pentry;
1229 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1232 *first_displayed_entry = pentry;
1233 } while (++nscrolled < maxscroll);
1238 static const struct got_error *
1239 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1240 struct got_commit_object *commit, struct got_object_id *commit_id,
1241 struct tog_view *log_view, struct got_repository *repo)
1243 const struct got_error *err;
1244 struct got_object_qid *parent_id;
1245 struct tog_view *diff_view;
1247 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1248 if (diff_view == NULL)
1249 return got_error_from_errno();
1251 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1252 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1253 commit_id, log_view, repo);
1255 *new_view = diff_view;
1259 static const struct got_error *
1260 browse_commit(struct tog_view **new_view, int begin_x,
1261 struct commit_queue_entry *entry, struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 struct got_tree_object *tree;
1265 struct tog_view *tree_view;
1267 err = got_object_open_as_tree(&tree, repo,
1268 got_object_commit_get_tree_id(entry->commit));
1272 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1273 if (tree_view == NULL)
1274 return got_error_from_errno();
1276 err = open_tree_view(tree_view, tree, entry->id, repo);
1278 got_object_tree_close(tree);
1280 *new_view = tree_view;
1285 log_thread(void *arg)
1287 const struct got_error *err = NULL;
1289 struct tog_log_thread_args *a = arg;
1292 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1296 while (!done && !err) {
1297 err = queue_commits(a->graph, a->commits, 1, a->repo,
1300 if (err->code != GOT_ERR_ITER_COMPLETED)
1304 } else if (a->commits_needed > 0)
1305 a->commits_needed--;
1307 errcode = pthread_mutex_lock(&tog_mutex);
1309 return (void *)got_error_set_errno(errcode);
1312 a->log_complete = 1;
1313 else if (*a->quit) {
1315 a->log_complete = 1;
1316 } else if (*a->first_displayed_entry == NULL) {
1317 *a->first_displayed_entry =
1318 TAILQ_FIRST(&a->commits->head);
1319 *a->selected_entry = *a->first_displayed_entry;
1323 a->commits_needed = 0;
1324 else if (a->commits_needed == 0) {
1325 errcode = pthread_cond_wait(&a->need_commits,
1328 err = got_error_set_errno(errcode);
1331 errcode = pthread_mutex_unlock(&tog_mutex);
1332 if (errcode && err == NULL)
1333 err = got_error_set_errno(errcode);
1338 static const struct got_error *
1339 stop_log_thread(struct tog_log_view_state *s)
1341 const struct got_error *err = NULL;
1346 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1348 return got_error_set_errno(errcode);
1349 errcode = pthread_mutex_unlock(&tog_mutex);
1351 return got_error_set_errno(errcode);
1352 errcode = pthread_join(s->thread, (void **)&err);
1354 return got_error_set_errno(errcode);
1355 errcode = pthread_mutex_lock(&tog_mutex);
1357 return got_error_set_errno(errcode);
1361 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1362 if (errcode && err == NULL)
1363 err = got_error_set_errno(errcode);
1365 if (s->thread_args.repo) {
1366 got_repo_close(s->thread_args.repo);
1367 s->thread_args.repo = NULL;
1370 if (s->thread_args.graph) {
1371 got_commit_graph_close(s->thread_args.graph);
1372 s->thread_args.graph = NULL;
1378 static const struct got_error *
1379 close_log_view(struct tog_view *view)
1381 const struct got_error *err = NULL;
1382 struct tog_log_view_state *s = &view->state.log;
1384 err = stop_log_thread(s);
1385 free_commits(&s->commits);
1386 free(s->in_repo_path);
1387 s->in_repo_path = NULL;
1393 static const struct got_error *
1394 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1395 struct got_repository *repo, const char *path, int check_disk)
1397 const struct got_error *err = NULL;
1398 struct tog_log_view_state *s = &view->state.log;
1399 struct got_repository *thread_repo = NULL;
1400 struct got_commit_graph *thread_graph = NULL;
1403 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1407 /* The commit queue only contains commits being displayed. */
1408 TAILQ_INIT(&s->commits.head);
1409 s->commits.ncommits = 0;
1412 s->start_id = got_object_id_dup(start_id);
1413 if (s->start_id == NULL) {
1414 err = got_error_from_errno();
1418 view->show = show_log_view;
1419 view->input = input_log_view;
1420 view->close = close_log_view;
1422 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1425 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1430 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1432 err = got_error_set_errno(errcode);
1436 s->thread_args.commits_needed = view->nlines;
1437 s->thread_args.graph = thread_graph;
1438 s->thread_args.commits = &s->commits;
1439 s->thread_args.in_repo_path = s->in_repo_path;
1440 s->thread_args.start_id = s->start_id;
1441 s->thread_args.repo = thread_repo;
1442 s->thread_args.log_complete = 0;
1443 s->thread_args.quit = &s->quit;
1444 s->thread_args.view = view;
1445 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1446 s->thread_args.selected_entry = &s->selected_entry;
1449 close_log_view(view);
1453 static const struct got_error *
1454 show_log_view(struct tog_view *view)
1456 struct tog_log_view_state *s = &view->state.log;
1458 if (s->thread == NULL) {
1459 int errcode = pthread_create(&s->thread, NULL, log_thread,
1462 return got_error_set_errno(errcode);
1465 return draw_commits(view, &s->last_displayed_entry,
1466 &s->selected_entry, s->first_displayed_entry,
1467 &s->commits, s->selected, view->nlines,
1468 s->in_repo_path, s->thread_args.commits_needed);
1471 static const struct got_error *
1472 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1473 struct tog_view **focus_view, struct tog_view *view, int ch)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1478 struct tog_view *diff_view = NULL, *tree_view = NULL;
1489 if (s->first_displayed_entry == NULL)
1491 if (s->selected > 0)
1493 if (s->selected > 0)
1495 scroll_up(&s->first_displayed_entry, 1,
1499 if (s->first_displayed_entry == NULL)
1501 if (TAILQ_FIRST(&s->commits.head) ==
1502 s->first_displayed_entry) {
1506 scroll_up(&s->first_displayed_entry,
1507 view->nlines, &s->commits);
1513 if (s->first_displayed_entry == NULL)
1515 if (s->selected < MIN(view->nlines - 2,
1516 s->commits.ncommits - 1)) {
1520 err = scroll_down(&s->first_displayed_entry, 1,
1521 &s->last_displayed_entry, &s->commits,
1522 &s->thread_args.log_complete,
1523 &s->thread_args.commits_needed,
1524 &s->thread_args.need_commits);
1527 struct commit_queue_entry *first;
1528 first = s->first_displayed_entry;
1531 err = scroll_down(&s->first_displayed_entry,
1532 view->nlines, &s->last_displayed_entry,
1533 &s->commits, &s->thread_args.log_complete,
1534 &s->thread_args.commits_needed,
1535 &s->thread_args.need_commits);
1536 if (first == s->first_displayed_entry &&
1537 s->selected < MIN(view->nlines - 2,
1538 s->commits.ncommits - 1)) {
1539 /* can't scroll further down */
1540 s->selected = MIN(view->nlines - 2,
1541 s->commits.ncommits - 1);
1547 if (s->selected > view->nlines - 2)
1548 s->selected = view->nlines - 2;
1549 if (s->selected > s->commits.ncommits - 1)
1550 s->selected = s->commits.ncommits - 1;
1554 if (s->selected_entry == NULL)
1556 if (view_is_parent_view(view))
1557 begin_x = view_split_begin_x(view->begin_x);
1558 err = open_diff_view_for_commit(&diff_view, begin_x,
1559 s->selected_entry->commit, s->selected_entry->id,
1563 if (view_is_parent_view(view)) {
1564 err = view_close_child(view);
1567 err = view_set_child(view, diff_view);
1569 view_close(diff_view);
1572 *focus_view = diff_view;
1573 view->child_focussed = 1;
1575 *new_view = diff_view;
1578 if (s->selected_entry == NULL)
1580 if (view_is_parent_view(view))
1581 begin_x = view_split_begin_x(view->begin_x);
1582 err = browse_commit(&tree_view, begin_x,
1583 s->selected_entry, s->repo);
1586 if (view_is_parent_view(view)) {
1587 err = view_close_child(view);
1590 err = view_set_child(view, tree_view);
1592 view_close(tree_view);
1595 *focus_view = tree_view;
1596 view->child_focussed = 1;
1598 *new_view = tree_view;
1601 if (strcmp(s->in_repo_path, "/") == 0)
1603 parent_path = dirname(s->in_repo_path);
1604 if (parent_path && strcmp(parent_path, ".") != 0) {
1605 struct tog_view *lv;
1606 err = stop_log_thread(s);
1609 lv = view_open(view->nlines, view->ncols,
1610 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1612 return got_error_from_errno();
1613 err = open_log_view(lv, s->start_id, s->repo,
1617 if (view_is_parent_view(view))
1620 view_set_child(view->parent, lv);
1633 static const struct got_error *
1634 apply_unveil(const char *repo_path, const char *worktree_path)
1636 const struct got_error *error;
1638 if (repo_path && unveil(repo_path, "r") != 0)
1639 return got_error_from_errno();
1641 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1642 return got_error_from_errno();
1644 if (unveil("/tmp", "rwc") != 0)
1645 return got_error_from_errno();
1647 error = got_privsep_unveil_exec_helpers();
1651 if (unveil(NULL, NULL) != 0)
1652 return got_error_from_errno();
1662 halfdelay(1); /* Do fast refresh while initial view is loading. */
1665 intrflush(stdscr, FALSE);
1666 keypad(stdscr, TRUE);
1668 signal(SIGWINCH, tog_sigwinch);
1671 static const struct got_error *
1672 cmd_log(int argc, char *argv[])
1674 const struct got_error *error;
1675 struct got_repository *repo = NULL;
1676 struct got_object_id *start_id = NULL;
1677 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1678 char *start_commit = NULL;
1680 struct tog_view *view;
1683 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1688 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1691 start_commit = optarg;
1694 repo_path = realpath(optarg, NULL);
1695 if (repo_path == NULL)
1696 err(1, "-r option");
1710 path = strdup(argv[0]);
1714 return got_error_from_errno();
1716 cwd = getcwd(NULL, 0);
1718 error = got_error_from_errno();
1721 if (repo_path == NULL) {
1722 struct got_worktree *worktree;
1723 error = got_worktree_open(&worktree, cwd);
1724 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1728 strdup(got_worktree_get_repo_path(worktree));
1729 got_worktree_close(worktree);
1731 repo_path = strdup(cwd);
1732 if (repo_path == NULL) {
1733 error = got_error_from_errno();
1740 error = apply_unveil(repo_path, NULL);
1744 error = got_repo_open(&repo, repo_path);
1748 if (start_commit == NULL)
1749 error = get_head_commit_id(&start_id, repo);
1751 error = got_object_resolve_id_str(&start_id, repo,
1756 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1758 error = got_error_from_errno();
1761 error = open_log_view(view, start_id, repo, path, 1);
1764 error = view_loop(view);
1771 got_repo_close(repo);
1779 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1785 parse_next_line(FILE *f, size_t *len)
1790 const char delim[3] = { '\0', '\0', '\0'};
1792 line = fparseln(f, &linelen, &lineno, delim, 0);
1798 static const struct got_error *
1799 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1800 int *last_displayed_line, int *eof, int max_lines,
1803 const struct got_error *err;
1804 int nlines = 0, nprinted = 0;
1811 werase(view->window);
1814 err = format_line(&wline, &width, header, view->ncols);
1819 if (view_needs_focus_indication(view))
1820 wstandout(view->window);
1821 waddwstr(view->window, wline);
1822 if (view_needs_focus_indication(view))
1823 wstandend(view->window);
1824 if (width < view->ncols)
1825 waddch(view->window, '\n');
1833 while (nprinted < max_lines) {
1834 line = parse_next_line(f, &len);
1839 if (++nlines < *first_displayed_line) {
1844 err = format_line(&wline, &width, line, view->ncols);
1849 waddwstr(view->window, wline);
1850 if (width < view->ncols)
1851 waddch(view->window, '\n');
1852 if (++nprinted == 1)
1853 *first_displayed_line = nlines;
1858 *last_displayed_line = nlines;
1866 get_datestr(time_t *time, char *datebuf)
1868 char *p, *s = ctime_r(time, datebuf);
1869 p = strchr(s, '\n');
1875 static const struct got_error *
1876 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1879 const struct got_error *err = NULL;
1881 struct got_commit_object *commit;
1882 char *id_str = NULL;
1883 time_t committer_time;
1884 const char *author, *committer;
1886 err = got_object_open_as_commit(&commit, repo, commit_id);
1890 err = got_object_id_str(&id_str, commit_id);
1892 err = got_error_from_errno();
1896 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1897 err = got_error_from_errno();
1900 if (fprintf(outfile, "from: %s\n",
1901 got_object_commit_get_author(commit)) < 0) {
1902 err = got_error_from_errno();
1905 committer_time = got_object_commit_get_committer_time(commit);
1906 if (fprintf(outfile, "date: %s UTC\n",
1907 get_datestr(&committer_time, datebuf)) < 0) {
1908 err = got_error_from_errno();
1911 author = got_object_commit_get_author(commit);
1912 committer = got_object_commit_get_committer(commit);
1913 if (strcmp(author, committer) != 0 &&
1914 fprintf(outfile, "via: %s\n", committer) < 0) {
1915 err = got_error_from_errno();
1918 if (fprintf(outfile, "%s\n",
1919 got_object_commit_get_logmsg(commit)) < 0) {
1920 err = got_error_from_errno();
1925 got_object_commit_close(commit);
1929 static const struct got_error *
1930 create_diff(struct tog_diff_view_state *s)
1932 const struct got_error *err = NULL;
1938 err = got_error_from_errno();
1941 if (s->f && fclose(s->f) != 0) {
1942 err = got_error_from_errno();
1948 err = got_object_get_type(&obj_type, s->repo, s->id1);
1950 err = got_object_get_type(&obj_type, s->repo, s->id2);
1955 case GOT_OBJ_TYPE_BLOB:
1956 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1957 s->diff_context, s->repo, f);
1959 case GOT_OBJ_TYPE_TREE:
1960 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1961 s->diff_context, s->repo, f);
1963 case GOT_OBJ_TYPE_COMMIT: {
1964 const struct got_object_id_queue *parent_ids;
1965 struct got_object_qid *pid;
1966 struct got_commit_object *commit2;
1968 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1971 /* Show commit info if we're diffing to a parent/root commit. */
1973 write_commit_info(s->id2, s->repo, f);
1975 parent_ids = got_object_commit_get_parent_ids(commit2);
1976 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1977 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1978 write_commit_info(s->id2, s->repo, f);
1983 got_object_commit_close(commit2);
1985 err = got_diff_objects_as_commits(s->id1, s->id2,
1986 s->diff_context, s->repo, f);
1990 err = got_error(GOT_ERR_OBJ_TYPE);
1994 if (f && fflush(f) != 0 && err == NULL)
1995 err = got_error_from_errno();
1999 static const struct got_error *
2000 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2001 struct got_object_id *id2, struct tog_view *log_view,
2002 struct got_repository *repo)
2004 const struct got_error *err;
2006 if (id1 != NULL && id2 != NULL) {
2008 err = got_object_get_type(&type1, repo, id1);
2011 err = got_object_get_type(&type2, repo, id2);
2016 return got_error(GOT_ERR_OBJ_TYPE);
2020 view->state.diff.id1 = got_object_id_dup(id1);
2021 if (view->state.diff.id1 == NULL)
2022 return got_error_from_errno();
2024 view->state.diff.id1 = NULL;
2026 view->state.diff.id2 = got_object_id_dup(id2);
2027 if (view->state.diff.id2 == NULL) {
2028 free(view->state.diff.id1);
2029 view->state.diff.id1 = NULL;
2030 return got_error_from_errno();
2032 view->state.diff.f = NULL;
2033 view->state.diff.first_displayed_line = 1;
2034 view->state.diff.last_displayed_line = view->nlines;
2035 view->state.diff.diff_context = 3;
2036 view->state.diff.log_view = log_view;
2037 view->state.diff.repo = repo;
2039 err = create_diff(&view->state.diff);
2041 free(view->state.diff.id1);
2042 view->state.diff.id1 = NULL;
2043 free(view->state.diff.id2);
2044 view->state.diff.id2 = NULL;
2048 view->show = show_diff_view;
2049 view->input = input_diff_view;
2050 view->close = close_diff_view;
2055 static const struct got_error *
2056 close_diff_view(struct tog_view *view)
2058 const struct got_error *err = NULL;
2060 free(view->state.diff.id1);
2061 view->state.diff.id1 = NULL;
2062 free(view->state.diff.id2);
2063 view->state.diff.id2 = NULL;
2064 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2065 err = got_error_from_errno();
2069 static const struct got_error *
2070 show_diff_view(struct tog_view *view)
2072 const struct got_error *err;
2073 struct tog_diff_view_state *s = &view->state.diff;
2074 char *id_str1 = NULL, *id_str2, *header;
2077 err = got_object_id_str(&id_str1, s->id1);
2081 err = got_object_id_str(&id_str2, s->id2);
2085 if (asprintf(&header, "diff %s %s",
2086 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2087 err = got_error_from_errno();
2095 return draw_file(view, s->f, &s->first_displayed_line,
2096 &s->last_displayed_line, &s->eof, view->nlines,
2100 static const struct got_error *
2101 set_selected_commit(struct tog_diff_view_state *s,
2102 struct commit_queue_entry *entry)
2104 struct commit_queue_entry *pentry;
2107 s->id2 = got_object_id_dup(entry->id);
2109 return got_error_from_errno();
2113 pentry = TAILQ_NEXT(entry, entry);
2115 s->id1 = got_object_id_dup(pentry->id);
2117 return got_error_from_errno();
2123 static const struct got_error *
2124 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2125 struct tog_view **focus_view, struct tog_view *view, int ch)
2127 const struct got_error *err = NULL;
2128 struct tog_diff_view_state *s = &view->state.diff;
2129 struct tog_log_view_state *ls;
2130 struct commit_queue_entry *entry;
2136 if (s->first_displayed_line > 1)
2137 s->first_displayed_line--;
2141 while (i++ < view->nlines - 1 &&
2142 s->first_displayed_line > 1)
2143 s->first_displayed_line--;
2148 s->first_displayed_line++;
2153 while (!s->eof && i++ < view->nlines - 1) {
2155 line = parse_next_line(s->f, NULL);
2156 s->first_displayed_line++;
2162 if (s->diff_context > 0) {
2164 err = create_diff(s);
2168 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2170 err = create_diff(s);
2175 if (s->log_view == NULL)
2177 err = input_log_view(NULL, NULL, NULL, s->log_view,
2181 ls = &s->log_view->state.log;
2182 entry = TAILQ_PREV(ls->selected_entry,
2183 commit_queue_head, entry);
2187 err = set_selected_commit(s, entry);
2191 s->first_displayed_line = 1;
2192 s->last_displayed_line = view->nlines;
2194 err = create_diff(s);
2198 if (s->log_view == NULL)
2201 ls = &s->log_view->state.log;
2202 err = input_log_view(NULL, NULL, NULL, s->log_view,
2207 /* Hack: Ensure two commits get loaded. */
2208 if (!ls->thread_args.log_complete) {
2209 err = input_log_view(NULL, NULL, NULL,
2210 s->log_view, KEY_DOWN);
2213 err = input_log_view(NULL, NULL, NULL,
2214 s->log_view, KEY_UP);
2219 entry = TAILQ_NEXT(ls->selected_entry, entry);
2223 err = set_selected_commit(s, entry);
2227 s->first_displayed_line = 1;
2228 s->last_displayed_line = view->nlines;
2230 err = create_diff(s);
2239 static const struct got_error *
2240 cmd_diff(int argc, char *argv[])
2242 const struct got_error *error = NULL;
2243 struct got_repository *repo = NULL;
2244 struct got_object_id *id1 = NULL, *id2 = NULL;
2245 char *repo_path = NULL;
2246 char *id_str1 = NULL, *id_str2 = NULL;
2248 struct tog_view *view;
2251 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2256 while ((ch = getopt(argc, argv, "")) != -1) {
2268 usage_diff(); /* TODO show local worktree changes */
2269 } else if (argc == 2) {
2270 repo_path = getcwd(NULL, 0);
2271 if (repo_path == NULL)
2272 return got_error_from_errno();
2275 } else if (argc == 3) {
2276 repo_path = realpath(argv[0], NULL);
2277 if (repo_path == NULL)
2278 return got_error_from_errno();
2286 error = apply_unveil(repo_path, NULL);
2290 error = got_repo_open(&repo, repo_path);
2295 error = got_object_resolve_id_str(&id1, repo, id_str1);
2299 error = got_object_resolve_id_str(&id2, repo, id_str2);
2303 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2305 error = got_error_from_errno();
2308 error = open_diff_view(view, id1, id2, NULL, repo);
2311 error = view_loop(view);
2313 got_repo_close(repo);
2321 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2326 struct tog_blame_line {
2328 struct got_object_id *id;
2331 static const struct got_error *
2332 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2333 const char *path, struct tog_blame_line *lines, int nlines,
2334 int blame_complete, int selected_line, int *first_displayed_line,
2335 int *last_displayed_line, int *eof, int max_lines)
2337 const struct got_error *err;
2338 int lineno = 0, nprinted = 0;
2343 struct tog_blame_line *blame_line;
2344 struct got_object_id *prev_id = NULL;
2347 err = got_object_id_str(&id_str, id);
2352 werase(view->window);
2354 if (asprintf(&line, "commit %s", id_str) == -1) {
2355 err = got_error_from_errno();
2360 err = format_line(&wline, &width, line, view->ncols);
2363 if (view_needs_focus_indication(view))
2364 wstandout(view->window);
2365 waddwstr(view->window, wline);
2366 if (view_needs_focus_indication(view))
2367 wstandend(view->window);
2370 if (width < view->ncols)
2371 waddch(view->window, '\n');
2373 if (asprintf(&line, "[%d/%d] %s%s",
2374 *first_displayed_line - 1 + selected_line, nlines,
2375 blame_complete ? "" : "annotating ", path) == -1) {
2377 return got_error_from_errno();
2380 err = format_line(&wline, &width, line, view->ncols);
2385 waddwstr(view->window, wline);
2388 if (width < view->ncols)
2389 waddch(view->window, '\n');
2392 while (nprinted < max_lines - 2) {
2393 line = parse_next_line(f, &len);
2398 if (++lineno < *first_displayed_line) {
2403 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2404 err = format_line(&wline, &width, line, wlimit);
2410 if (view->focussed && nprinted == selected_line - 1)
2411 wstandout(view->window);
2413 blame_line = &lines[lineno - 1];
2414 if (blame_line->annotated && prev_id &&
2415 got_object_id_cmp(prev_id, blame_line->id) == 0)
2416 waddstr(view->window, " ");
2417 else if (blame_line->annotated) {
2419 err = got_object_id_str(&id_str, blame_line->id);
2425 wprintw(view->window, "%.8s ", id_str);
2427 prev_id = blame_line->id;
2429 waddstr(view->window, "........ ");
2433 waddwstr(view->window, wline);
2434 while (width < wlimit) {
2435 waddch(view->window, ' ');
2438 if (view->focussed && nprinted == selected_line - 1)
2439 wstandend(view->window);
2440 if (++nprinted == 1)
2441 *first_displayed_line = lineno;
2446 *last_displayed_line = lineno;
2453 static const struct got_error *
2454 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2456 const struct got_error *err = NULL;
2457 struct tog_blame_cb_args *a = arg;
2458 struct tog_blame_line *line;
2461 if (nlines != a->nlines ||
2462 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2463 return got_error(GOT_ERR_RANGE);
2465 errcode = pthread_mutex_lock(&tog_mutex);
2467 return got_error_set_errno(errcode);
2469 if (*a->quit) { /* user has quit the blame view */
2470 err = got_error(GOT_ERR_ITER_COMPLETED);
2475 goto done; /* no change in this commit */
2477 line = &a->lines[lineno - 1];
2478 if (line->annotated)
2481 line->id = got_object_id_dup(id);
2482 if (line->id == NULL) {
2483 err = got_error_from_errno();
2486 line->annotated = 1;
2488 errcode = pthread_mutex_unlock(&tog_mutex);
2490 err = got_error_set_errno(errcode);
2495 blame_thread(void *arg)
2497 const struct got_error *err;
2498 struct tog_blame_thread_args *ta = arg;
2499 struct tog_blame_cb_args *a = ta->cb_args;
2502 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2503 blame_cb, ta->cb_args);
2505 errcode = pthread_mutex_lock(&tog_mutex);
2507 return (void *)got_error_set_errno(errcode);
2509 got_repo_close(ta->repo);
2513 errcode = pthread_mutex_unlock(&tog_mutex);
2514 if (errcode && err == NULL)
2515 err = got_error_set_errno(errcode);
2520 static struct got_object_id *
2521 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2524 struct tog_blame_line *line;
2526 line = &lines[first_displayed_line - 1 + selected_line - 1];
2527 if (!line->annotated)
2533 static const struct got_error *
2534 stop_blame(struct tog_blame *blame)
2536 const struct got_error *err = NULL;
2539 if (blame->thread) {
2541 errcode = pthread_mutex_unlock(&tog_mutex);
2543 return got_error_set_errno(errcode);
2544 errcode = pthread_join(blame->thread, (void **)&err);
2546 return got_error_set_errno(errcode);
2547 errcode = pthread_mutex_lock(&tog_mutex);
2549 return got_error_set_errno(errcode);
2550 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2552 blame->thread = NULL;
2554 if (blame->thread_args.repo) {
2555 got_repo_close(blame->thread_args.repo);
2556 blame->thread_args.repo = NULL;
2559 if (fclose(blame->f) != 0 && err == NULL)
2560 err = got_error_from_errno();
2564 for (i = 0; i < blame->nlines; i++)
2565 free(blame->lines[i].id);
2567 blame->lines = NULL;
2569 free(blame->cb_args.commit_id);
2570 blame->cb_args.commit_id = NULL;
2575 static const struct got_error *
2576 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2577 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2578 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2579 struct got_repository *repo)
2581 const struct got_error *err = NULL;
2582 struct got_blob_object *blob = NULL;
2583 struct got_repository *thread_repo = NULL;
2584 struct got_object_id *obj_id = NULL;
2587 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2591 return got_error(GOT_ERR_NO_OBJ);
2593 err = got_object_get_type(&obj_type, repo, obj_id);
2597 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2598 err = got_error(GOT_ERR_OBJ_TYPE);
2602 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2605 blame->f = got_opentemp();
2606 if (blame->f == NULL) {
2607 err = got_error_from_errno();
2610 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2615 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2616 if (blame->lines == NULL) {
2617 err = got_error_from_errno();
2621 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2625 blame->cb_args.view = view;
2626 blame->cb_args.lines = blame->lines;
2627 blame->cb_args.nlines = blame->nlines;
2628 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2629 if (blame->cb_args.commit_id == NULL) {
2630 err = got_error_from_errno();
2633 blame->cb_args.quit = done;
2635 blame->thread_args.path = path;
2636 blame->thread_args.repo = thread_repo;
2637 blame->thread_args.cb_args = &blame->cb_args;
2638 blame->thread_args.complete = blame_complete;
2639 *blame_complete = 0;
2643 got_object_blob_close(blob);
2650 static const struct got_error *
2651 open_blame_view(struct tog_view *view, char *path,
2652 struct got_object_id *commit_id, struct got_repository *repo)
2654 const struct got_error *err = NULL;
2655 struct tog_blame_view_state *s = &view->state.blame;
2657 SIMPLEQ_INIT(&s->blamed_commits);
2659 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2663 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2664 s->first_displayed_line = 1;
2665 s->last_displayed_line = view->nlines;
2666 s->selected_line = 1;
2667 s->blame_complete = 0;
2669 if (s->path == NULL)
2670 return got_error_from_errno();
2672 s->commit_id = commit_id;
2673 memset(&s->blame, 0, sizeof(s->blame));
2675 view->show = show_blame_view;
2676 view->input = input_blame_view;
2677 view->close = close_blame_view;
2679 return run_blame(&s->blame, view, &s->blame_complete,
2680 &s->first_displayed_line, &s->last_displayed_line,
2681 &s->selected_line, &s->done, &s->eof, s->path,
2682 s->blamed_commit->id, s->repo);
2685 static const struct got_error *
2686 close_blame_view(struct tog_view *view)
2688 const struct got_error *err = NULL;
2689 struct tog_blame_view_state *s = &view->state.blame;
2691 if (s->blame.thread)
2692 err = stop_blame(&s->blame);
2694 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2695 struct got_object_qid *blamed_commit;
2696 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2697 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2698 got_object_qid_free(blamed_commit);
2706 static const struct got_error *
2707 show_blame_view(struct tog_view *view)
2709 const struct got_error *err = NULL;
2710 struct tog_blame_view_state *s = &view->state.blame;
2713 if (s->blame.thread == NULL) {
2714 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2715 &s->blame.thread_args);
2717 return got_error_set_errno(errcode);
2720 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2721 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2722 s->selected_line, &s->first_displayed_line,
2723 &s->last_displayed_line, &s->eof, view->nlines);
2729 static const struct got_error *
2730 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2731 struct tog_view **focus_view, struct tog_view *view, int ch)
2733 const struct got_error *err = NULL, *thread_err = NULL;
2734 struct tog_view *diff_view;
2735 struct tog_blame_view_state *s = &view->state.blame;
2744 if (s->selected_line > 1)
2746 else if (s->selected_line == 1 &&
2747 s->first_displayed_line > 1)
2748 s->first_displayed_line--;
2751 if (s->first_displayed_line == 1) {
2752 s->selected_line = 1;
2755 if (s->first_displayed_line > view->nlines - 2)
2756 s->first_displayed_line -=
2759 s->first_displayed_line = 1;
2763 if (s->selected_line < view->nlines - 2 &&
2764 s->first_displayed_line +
2765 s->selected_line <= s->blame.nlines)
2767 else if (s->last_displayed_line <
2769 s->first_displayed_line++;
2773 struct got_object_id *id = NULL;
2774 id = get_selected_commit_id(s->blame.lines,
2775 s->first_displayed_line, s->selected_line);
2779 struct got_commit_object *commit;
2780 struct got_object_qid *pid;
2781 struct got_object_id *blob_id = NULL;
2783 err = got_object_open_as_commit(&commit,
2787 pid = SIMPLEQ_FIRST(
2788 got_object_commit_get_parent_ids(commit));
2790 got_object_commit_close(commit);
2793 /* Check if path history ends here. */
2794 err = got_object_id_by_path(&blob_id, s->repo,
2797 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2799 got_object_commit_close(commit);
2802 err = got_object_get_type(&obj_type, s->repo,
2805 /* Can't blame non-blob type objects. */
2806 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2807 got_object_commit_close(commit);
2810 err = got_object_qid_alloc(&s->blamed_commit,
2812 got_object_commit_close(commit);
2814 if (got_object_id_cmp(id,
2815 s->blamed_commit->id) == 0)
2817 err = got_object_qid_alloc(&s->blamed_commit,
2823 thread_err = stop_blame(&s->blame);
2827 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2828 s->blamed_commit, entry);
2829 err = run_blame(&s->blame, view, &s->blame_complete,
2830 &s->first_displayed_line, &s->last_displayed_line,
2831 &s->selected_line, &s->done, &s->eof,
2832 s->path, s->blamed_commit->id, s->repo);
2838 struct got_object_qid *first;
2839 first = SIMPLEQ_FIRST(&s->blamed_commits);
2840 if (!got_object_id_cmp(first->id, s->commit_id))
2843 thread_err = stop_blame(&s->blame);
2847 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2848 got_object_qid_free(s->blamed_commit);
2850 SIMPLEQ_FIRST(&s->blamed_commits);
2851 err = run_blame(&s->blame, view, &s->blame_complete,
2852 &s->first_displayed_line, &s->last_displayed_line,
2853 &s->selected_line, &s->done, &s->eof, s->path,
2854 s->blamed_commit->id, s->repo);
2861 struct got_object_id *id = NULL;
2862 struct got_object_qid *pid;
2863 struct got_commit_object *commit = NULL;
2864 id = get_selected_commit_id(s->blame.lines,
2865 s->first_displayed_line, s->selected_line);
2868 err = got_object_open_as_commit(&commit, s->repo, id);
2871 pid = SIMPLEQ_FIRST(
2872 got_object_commit_get_parent_ids(commit));
2873 if (view_is_parent_view(view))
2874 begin_x = view_split_begin_x(view->begin_x);
2875 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2876 if (diff_view == NULL) {
2877 got_object_commit_close(commit);
2878 err = got_error_from_errno();
2881 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2883 got_object_commit_close(commit);
2885 view_close(diff_view);
2888 if (view_is_parent_view(view)) {
2889 err = view_close_child(view);
2892 err = view_set_child(view, diff_view);
2894 view_close(diff_view);
2897 *focus_view = diff_view;
2898 view->child_focussed = 1;
2900 *new_view = diff_view;
2907 if (s->last_displayed_line >= s->blame.nlines &&
2908 s->selected_line < view->nlines - 2) {
2909 s->selected_line = MIN(s->blame.nlines,
2913 if (s->last_displayed_line + view->nlines - 2
2915 s->first_displayed_line +=
2918 s->first_displayed_line =
2923 if (s->selected_line > view->nlines - 2) {
2924 s->selected_line = MIN(s->blame.nlines,
2931 return thread_err ? thread_err : err;
2934 static const struct got_error *
2935 cmd_blame(int argc, char *argv[])
2937 const struct got_error *error;
2938 struct got_repository *repo = NULL;
2939 struct got_worktree *worktree = NULL;
2940 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2941 struct got_object_id *commit_id = NULL;
2942 char *commit_id_str = NULL;
2944 struct tog_view *view;
2947 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2952 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2955 commit_id_str = optarg;
2958 repo_path = realpath(optarg, NULL);
2959 if (repo_path == NULL)
2960 err(1, "-r option");
2976 cwd = getcwd(NULL, 0);
2978 error = got_error_from_errno();
2981 if (repo_path == NULL) {
2982 error = got_worktree_open(&worktree, cwd);
2983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2989 strdup(got_worktree_get_repo_path(worktree));
2990 if (repo_path == NULL)
2991 error = got_error_from_errno();
2995 repo_path = strdup(cwd);
2996 if (repo_path == NULL) {
2997 error = got_error_from_errno();
3005 error = apply_unveil(repo_path, NULL);
3009 error = got_repo_open(&repo, repo_path);
3014 const char *prefix = got_worktree_get_path_prefix(worktree);
3015 char *p, *worktree_subdir = cwd +
3016 strlen(got_worktree_get_root_path(worktree));
3017 if (asprintf(&p, "%s%s%s%s%s",
3018 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3019 worktree_subdir, worktree_subdir[0] ? "/" : "",
3021 error = got_error_from_errno();
3024 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3027 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3032 if (commit_id_str == NULL) {
3033 struct got_reference *head_ref;
3034 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3037 error = got_ref_resolve(&commit_id, repo, head_ref);
3038 got_ref_close(head_ref);
3040 error = got_object_resolve_id_str(&commit_id, repo,
3046 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3048 error = got_error_from_errno();
3051 error = open_blame_view(view, in_repo_path, commit_id, repo);
3054 error = view_loop(view);
3060 got_worktree_close(worktree);
3062 got_repo_close(repo);
3066 static const struct got_error *
3067 draw_tree_entries(struct tog_view *view,
3068 struct got_tree_entry **first_displayed_entry,
3069 struct got_tree_entry **last_displayed_entry,
3070 struct got_tree_entry **selected_entry, int *ndisplayed,
3071 const char *label, int show_ids, const char *parent_path,
3072 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3074 const struct got_error *err = NULL;
3075 struct got_tree_entry *te;
3081 werase(view->window);
3086 err = format_line(&wline, &width, label, view->ncols);
3089 if (view_needs_focus_indication(view))
3090 wstandout(view->window);
3091 waddwstr(view->window, wline);
3092 if (view_needs_focus_indication(view))
3093 wstandend(view->window);
3096 if (width < view->ncols)
3097 waddch(view->window, '\n');
3100 err = format_line(&wline, &width, parent_path, view->ncols);
3103 waddwstr(view->window, wline);
3106 if (width < view->ncols)
3107 waddch(view->window, '\n');
3110 waddch(view->window, '\n');
3114 te = SIMPLEQ_FIRST(&entries->head);
3115 if (*first_displayed_entry == NULL) {
3116 if (selected == 0) {
3118 wstandout(view->window);
3119 *selected_entry = NULL;
3121 waddstr(view->window, " ..\n"); /* parent directory */
3122 if (selected == 0 && view->focussed)
3123 wstandend(view->window);
3130 while (te != *first_displayed_entry)
3131 te = SIMPLEQ_NEXT(te, entry);
3135 char *line = NULL, *id_str = NULL;
3138 err = got_object_id_str(&id_str, te->id);
3140 return got_error_from_errno();
3142 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3143 te->name, S_ISDIR(te->mode) ? "/" :
3144 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3146 return got_error_from_errno();
3149 err = format_line(&wline, &width, line, view->ncols);
3154 if (n == selected) {
3156 wstandout(view->window);
3157 *selected_entry = te;
3159 waddwstr(view->window, wline);
3160 if (width < view->ncols)
3161 waddch(view->window, '\n');
3162 if (n == selected && view->focussed)
3163 wstandend(view->window);
3169 *last_displayed_entry = te;
3172 te = SIMPLEQ_NEXT(te, entry);
3179 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3180 const struct got_tree_entries *entries, int isroot)
3182 struct got_tree_entry *te, *prev;
3185 if (*first_displayed_entry == NULL)
3188 te = SIMPLEQ_FIRST(&entries->head);
3189 if (*first_displayed_entry == te) {
3191 *first_displayed_entry = NULL;
3195 /* XXX this is stupid... switch to TAILQ? */
3196 for (i = 0; i < maxscroll; i++) {
3197 while (te != *first_displayed_entry) {
3199 te = SIMPLEQ_NEXT(te, entry);
3201 *first_displayed_entry = prev;
3202 te = SIMPLEQ_FIRST(&entries->head);
3204 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3205 *first_displayed_entry = NULL;
3209 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3210 struct got_tree_entry *last_displayed_entry,
3211 const struct got_tree_entries *entries)
3213 struct got_tree_entry *next, *last;
3216 if (*first_displayed_entry)
3217 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3219 next = SIMPLEQ_FIRST(&entries->head);
3220 last = last_displayed_entry;
3221 while (next && last && n++ < maxscroll) {
3222 last = SIMPLEQ_NEXT(last, entry);
3224 *first_displayed_entry = next;
3225 next = SIMPLEQ_NEXT(next, entry);
3231 static const struct got_error *
3232 tree_entry_path(char **path, struct tog_parent_trees *parents,
3233 struct got_tree_entry *te)
3235 const struct got_error *err = NULL;
3236 struct tog_parent_tree *pt;
3237 size_t len = 2; /* for leading slash and NUL */
3239 TAILQ_FOREACH(pt, parents, entry)
3240 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3242 len += strlen(te->name);
3244 *path = calloc(1, len);
3246 return got_error_from_errno();
3249 pt = TAILQ_LAST(parents, tog_parent_trees);
3251 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3252 err = got_error(GOT_ERR_NO_SPACE);
3255 if (strlcat(*path, "/", len) >= len) {
3256 err = got_error(GOT_ERR_NO_SPACE);
3259 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3262 if (strlcat(*path, te->name, len) >= len) {
3263 err = got_error(GOT_ERR_NO_SPACE);
3275 static const struct got_error *
3276 blame_tree_entry(struct tog_view **new_view, int begin_x,
3277 struct got_tree_entry *te, struct tog_parent_trees *parents,
3278 struct got_object_id *commit_id, struct got_repository *repo)
3280 const struct got_error *err = NULL;
3282 struct tog_view *blame_view;
3284 err = tree_entry_path(&path, parents, te);
3288 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3289 if (blame_view == NULL)
3290 return got_error_from_errno();
3292 err = open_blame_view(blame_view, path, commit_id, repo);
3294 view_close(blame_view);
3297 *new_view = blame_view;
3301 static const struct got_error *
3302 log_tree_entry(struct tog_view **new_view, int begin_x,
3303 struct got_tree_entry *te, struct tog_parent_trees *parents,
3304 struct got_object_id *commit_id, struct got_repository *repo)
3306 struct tog_view *log_view;
3307 const struct got_error *err = NULL;
3310 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3311 if (log_view == NULL)
3312 return got_error_from_errno();
3314 err = tree_entry_path(&path, parents, te);
3318 err = open_log_view(log_view, commit_id, repo, path, 0);
3320 view_close(log_view);
3322 *new_view = log_view;
3327 static const struct got_error *
3328 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3329 struct got_object_id *commit_id, struct got_repository *repo)
3331 const struct got_error *err = NULL;
3332 char *commit_id_str = NULL;
3333 struct tog_tree_view_state *s = &view->state.tree;
3335 TAILQ_INIT(&s->parents);
3337 err = got_object_id_str(&commit_id_str, commit_id);
3341 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3342 err = got_error_from_errno();
3346 s->root = s->tree = root;
3347 s->entries = got_object_tree_get_entries(root);
3348 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3349 s->commit_id = got_object_id_dup(commit_id);
3350 if (s->commit_id == NULL) {
3351 err = got_error_from_errno();
3356 view->show = show_tree_view;
3357 view->input = input_tree_view;
3358 view->close = close_tree_view;
3360 free(commit_id_str);
3362 free(s->tree_label);
3363 s->tree_label = NULL;
3368 static const struct got_error *
3369 close_tree_view(struct tog_view *view)
3371 struct tog_tree_view_state *s = &view->state.tree;
3373 free(s->tree_label);
3374 s->tree_label = NULL;
3376 s->commit_id = NULL;
3377 while (!TAILQ_EMPTY(&s->parents)) {
3378 struct tog_parent_tree *parent;
3379 parent = TAILQ_FIRST(&s->parents);
3380 TAILQ_REMOVE(&s->parents, parent, entry);
3384 if (s->tree != s->root)
3385 got_object_tree_close(s->tree);
3386 got_object_tree_close(s->root);
3391 static const struct got_error *
3392 show_tree_view(struct tog_view *view)
3394 const struct got_error *err = NULL;
3395 struct tog_tree_view_state *s = &view->state.tree;
3398 err = tree_entry_path(&parent_path, &s->parents, NULL);
3402 err = draw_tree_entries(view, &s->first_displayed_entry,
3403 &s->last_displayed_entry, &s->selected_entry,
3404 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3405 s->entries, s->selected, view->nlines, s->tree == s->root);
3412 static const struct got_error *
3413 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3414 struct tog_view **focus_view, struct tog_view *view, int ch)
3416 const struct got_error *err = NULL;
3417 struct tog_tree_view_state *s = &view->state.tree;
3418 struct tog_view *log_view;
3419 int begin_x = 0, nscrolled;
3423 s->show_ids = !s->show_ids;
3426 if (!s->selected_entry)
3428 if (view_is_parent_view(view))
3429 begin_x = view_split_begin_x(view->begin_x);
3430 err = log_tree_entry(&log_view, begin_x,
3431 s->selected_entry, &s->parents,
3432 s->commit_id, s->repo);
3433 if (view_is_parent_view(view)) {
3434 err = view_close_child(view);
3437 err = view_set_child(view, log_view);
3439 view_close(log_view);
3442 *focus_view = log_view;
3443 view->child_focussed = 1;
3445 *new_view = log_view;
3449 if (s->selected > 0) {
3451 if (s->selected == 0)
3454 if (s->selected > 0)
3456 tree_scroll_up(&s->first_displayed_entry, 1,
3457 s->entries, s->tree == s->root);
3460 tree_scroll_up(&s->first_displayed_entry,
3461 MAX(0, view->nlines - 4 - s->selected), s->entries,
3462 s->tree == s->root);
3464 if (SIMPLEQ_FIRST(&s->entries->head) ==
3465 s->first_displayed_entry && s->tree != s->root)
3466 s->first_displayed_entry = NULL;
3470 if (s->selected < s->ndisplayed - 1) {
3474 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3476 /* can't scroll any further */
3479 tree_scroll_down(&s->first_displayed_entry, 1,
3480 s->last_displayed_entry, s->entries);
3483 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3485 /* can't scroll any further; move cursor down */
3486 if (s->selected < s->ndisplayed - 1)
3487 s->selected = s->ndisplayed - 1;
3490 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3491 view->nlines, s->last_displayed_entry, s->entries);
3492 if (nscrolled < view->nlines) {
3494 struct got_tree_entry *te;
3495 te = s->first_displayed_entry;
3498 te = SIMPLEQ_NEXT(te, entry);
3500 s->selected = ndisplayed - 1;
3505 if (s->selected_entry == NULL) {
3506 struct tog_parent_tree *parent;
3508 /* user selected '..' */
3509 if (s->tree == s->root)
3511 parent = TAILQ_FIRST(&s->parents);
3512 TAILQ_REMOVE(&s->parents, parent,
3514 got_object_tree_close(s->tree);
3515 s->tree = parent->tree;
3517 got_object_tree_get_entries(s->tree);
3518 s->first_displayed_entry =
3519 parent->first_displayed_entry;
3521 parent->selected_entry;
3522 s->selected = parent->selected;
3524 } else if (S_ISDIR(s->selected_entry->mode)) {
3525 struct tog_parent_tree *parent;
3526 struct got_tree_object *child;
3527 err = got_object_open_as_tree(&child,
3528 s->repo, s->selected_entry->id);
3531 parent = calloc(1, sizeof(*parent));
3532 if (parent == NULL) {
3533 err = got_error_from_errno();
3536 parent->tree = s->tree;
3537 parent->first_displayed_entry =
3538 s->first_displayed_entry;
3539 parent->selected_entry = s->selected_entry;
3540 parent->selected = s->selected;
3541 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3544 got_object_tree_get_entries(s->tree);
3546 s->first_displayed_entry = NULL;
3547 } else if (S_ISREG(s->selected_entry->mode)) {
3548 struct tog_view *blame_view;
3549 int begin_x = view_is_parent_view(view) ?
3550 view_split_begin_x(view->begin_x) : 0;
3552 err = blame_tree_entry(&blame_view, begin_x,
3553 s->selected_entry, &s->parents, s->commit_id,
3557 if (view_is_parent_view(view)) {
3558 err = view_close_child(view);
3561 err = view_set_child(view, blame_view);
3563 view_close(blame_view);
3566 *focus_view = blame_view;
3567 view->child_focussed = 1;
3569 *new_view = blame_view;
3573 if (s->selected > view->nlines)
3574 s->selected = s->ndisplayed - 1;
3587 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3592 static const struct got_error *
3593 cmd_tree(int argc, char *argv[])
3595 const struct got_error *error;
3596 struct got_repository *repo = NULL;
3597 char *repo_path = NULL;
3598 struct got_object_id *commit_id = NULL;
3599 char *commit_id_arg = NULL;
3600 struct got_commit_object *commit = NULL;
3601 struct got_tree_object *tree = NULL;
3603 struct tog_view *view;
3606 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3611 while ((ch = getopt(argc, argv, "c:")) != -1) {
3614 commit_id_arg = optarg;
3626 struct got_worktree *worktree;
3627 char *cwd = getcwd(NULL, 0);
3629 return got_error_from_errno();
3630 error = got_worktree_open(&worktree, cwd);
3631 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3636 strdup(got_worktree_get_repo_path(worktree));
3637 got_worktree_close(worktree);
3640 if (repo_path == NULL) {
3641 error = got_error_from_errno();
3644 } else if (argc == 1) {
3645 repo_path = realpath(argv[0], NULL);
3646 if (repo_path == NULL)
3647 return got_error_from_errno();
3653 error = apply_unveil(repo_path, NULL);
3657 error = got_repo_open(&repo, repo_path);
3661 if (commit_id_arg == NULL)
3662 error = get_head_commit_id(&commit_id, repo);
3664 error = got_object_resolve_id_str(&commit_id, repo,
3669 error = got_object_open_as_commit(&commit, repo, commit_id);
3673 error = got_object_open_as_tree(&tree, repo,
3674 got_object_commit_get_tree_id(commit));
3678 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3680 error = got_error_from_errno();
3683 error = open_tree_view(view, tree, commit_id, repo);
3686 error = view_loop(view);
3691 got_object_commit_close(commit);
3693 got_object_tree_close(tree);
3695 got_repo_close(repo);
3704 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3705 "Available commands:\n", getprogname());
3706 for (i = 0; i < nitems(tog_commands); i++) {
3707 struct tog_cmd *cmd = &tog_commands[i];
3708 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3714 make_argv(const char *arg0, const char *arg1)
3717 int argc = (arg1 == NULL ? 1 : 2);
3719 argv = calloc(argc, sizeof(char *));
3722 argv[0] = strdup(arg0);
3723 if (argv[0] == NULL)
3726 argv[1] = strdup(arg1);
3727 if (argv[1] == NULL)
3735 main(int argc, char *argv[])
3737 const struct got_error *error = NULL;
3738 struct tog_cmd *cmd = NULL;
3740 char **cmd_argv = NULL;
3742 setlocale(LC_CTYPE, "");
3744 while ((ch = getopt(argc, argv, "h")) != -1) {
3763 /* Build an argument vector which runs a default command. */
3764 cmd = &tog_commands[0];
3765 cmd_argv = make_argv(cmd->name, NULL);
3770 /* Did the user specific a command? */
3771 for (i = 0; i < nitems(tog_commands); i++) {
3772 if (strncmp(tog_commands[i].name, argv[0],
3773 strlen(argv[0])) == 0) {
3774 cmd = &tog_commands[i];
3776 tog_commands[i].cmd_usage();
3781 /* Did the user specify a repository? */
3782 char *repo_path = realpath(argv[0], NULL);
3784 struct got_repository *repo;
3785 error = got_repo_open(&repo, repo_path);
3787 got_repo_close(repo);
3789 error = got_error_from_errno();
3792 fprintf(stderr, "%s: '%s' is not a "
3793 "known command\n", getprogname(),
3797 fprintf(stderr, "%s: '%s' is neither a known "
3798 "command nor a path to a repository\n",
3799 getprogname(), argv[0]);
3803 cmd = &tog_commands[0];
3804 cmd_argv = make_argv(cmd->name, repo_path);
3810 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3817 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);