2 * Copyright (c) 2018 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 nentries, 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);
2727 if (id == NULL || got_object_id_cmp(id,
2728 s->blamed_commit->id) == 0)
2730 err = got_object_open_as_commit(&commit, s->repo, id);
2733 pid = SIMPLEQ_FIRST(
2734 got_object_commit_get_parent_ids(commit));
2735 if (view_is_parent_view(view))
2736 begin_x = view_split_begin_x(view->begin_x);
2737 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2738 if (diff_view == NULL) {
2739 got_object_commit_close(commit);
2740 err = got_error_from_errno();
2743 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2745 got_object_commit_close(commit);
2747 view_close(diff_view);
2750 if (view_is_parent_view(view)) {
2751 err = view_close_child(view);
2754 err = view_set_child(view, diff_view);
2756 view_close(diff_view);
2759 *focus_view = diff_view;
2760 view->child_focussed = 1;
2762 *new_view = diff_view;
2769 if (s->last_displayed_line >= s->blame.nlines &&
2770 s->selected_line < view->nlines - 2) {
2771 s->selected_line = MIN(s->blame.nlines,
2775 if (s->last_displayed_line + view->nlines - 2
2777 s->first_displayed_line +=
2780 s->first_displayed_line =
2785 if (s->selected_line > view->nlines - 2) {
2786 s->selected_line = MIN(s->blame.nlines,
2793 return thread_err ? thread_err : err;
2796 static const struct got_error *
2797 cmd_blame(int argc, char *argv[])
2799 const struct got_error *error;
2800 struct got_repository *repo = NULL;
2801 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2802 struct got_object_id *commit_id = NULL;
2803 char *commit_id_str = NULL;
2805 struct tog_view *view;
2808 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2813 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2816 commit_id_str = optarg;
2819 repo_path = realpath(optarg, NULL);
2820 if (repo_path == NULL)
2821 err(1, "-r option");
2837 cwd = getcwd(NULL, 0);
2839 error = got_error_from_errno();
2842 if (repo_path == NULL) {
2843 repo_path = strdup(cwd);
2844 if (repo_path == NULL) {
2845 error = got_error_from_errno();
2850 error = apply_unveil(repo_path, NULL);
2854 error = got_repo_open(&repo, repo_path);
2858 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2862 if (commit_id_str == NULL) {
2863 struct got_reference *head_ref;
2864 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2867 error = got_ref_resolve(&commit_id, repo, head_ref);
2868 got_ref_close(head_ref);
2870 error = got_object_resolve_id_str(&commit_id, repo,
2876 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2878 error = got_error_from_errno();
2881 error = open_blame_view(view, in_repo_path, commit_id, repo);
2884 error = view_loop(view);
2890 got_repo_close(repo);
2894 static const struct got_error *
2895 draw_tree_entries(struct tog_view *view,
2896 struct got_tree_entry **first_displayed_entry,
2897 struct got_tree_entry **last_displayed_entry,
2898 struct got_tree_entry **selected_entry, int *ndisplayed,
2899 const char *label, int show_ids, const char *parent_path,
2900 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2902 const struct got_error *err = NULL;
2903 struct got_tree_entry *te;
2909 werase(view->window);
2914 err = format_line(&wline, &width, label, view->ncols);
2917 if (view_needs_focus_indication(view))
2918 wstandout(view->window);
2919 waddwstr(view->window, wline);
2920 if (view_needs_focus_indication(view))
2921 wstandend(view->window);
2924 if (width < view->ncols)
2925 waddch(view->window, '\n');
2928 err = format_line(&wline, &width, parent_path, view->ncols);
2931 waddwstr(view->window, wline);
2934 if (width < view->ncols)
2935 waddch(view->window, '\n');
2938 waddch(view->window, '\n');
2942 te = SIMPLEQ_FIRST(&entries->head);
2943 if (*first_displayed_entry == NULL) {
2944 if (selected == 0) {
2946 wstandout(view->window);
2947 *selected_entry = NULL;
2949 waddstr(view->window, " ..\n"); /* parent directory */
2950 if (selected == 0 && view->focussed)
2951 wstandend(view->window);
2958 while (te != *first_displayed_entry)
2959 te = SIMPLEQ_NEXT(te, entry);
2963 char *line = NULL, *id_str = NULL;
2966 err = got_object_id_str(&id_str, te->id);
2968 return got_error_from_errno();
2970 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2971 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2973 return got_error_from_errno();
2976 err = format_line(&wline, &width, line, view->ncols);
2981 if (n == selected) {
2983 wstandout(view->window);
2984 *selected_entry = te;
2986 waddwstr(view->window, wline);
2987 if (width < view->ncols)
2988 waddch(view->window, '\n');
2989 if (n == selected && view->focussed)
2990 wstandend(view->window);
2996 *last_displayed_entry = te;
2999 te = SIMPLEQ_NEXT(te, entry);
3006 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3007 const struct got_tree_entries *entries, int isroot)
3009 struct got_tree_entry *te, *prev;
3012 if (*first_displayed_entry == NULL)
3015 te = SIMPLEQ_FIRST(&entries->head);
3016 if (*first_displayed_entry == te) {
3018 *first_displayed_entry = NULL;
3022 /* XXX this is stupid... switch to TAILQ? */
3023 for (i = 0; i < maxscroll; i++) {
3024 while (te != *first_displayed_entry) {
3026 te = SIMPLEQ_NEXT(te, entry);
3028 *first_displayed_entry = prev;
3029 te = SIMPLEQ_FIRST(&entries->head);
3031 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3032 *first_displayed_entry = NULL;
3036 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3037 struct got_tree_entry *last_displayed_entry,
3038 const struct got_tree_entries *entries)
3040 struct got_tree_entry *next;
3043 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
3046 if (*first_displayed_entry)
3047 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3049 next = SIMPLEQ_FIRST(&entries->head);
3051 *first_displayed_entry = next;
3052 if (++n >= maxscroll)
3054 next = SIMPLEQ_NEXT(next, entry);
3058 static const struct got_error *
3059 tree_entry_path(char **path, struct tog_parent_trees *parents,
3060 struct got_tree_entry *te)
3062 const struct got_error *err = NULL;
3063 struct tog_parent_tree *pt;
3064 size_t len = 2; /* for leading slash and NUL */
3066 TAILQ_FOREACH(pt, parents, entry)
3067 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3069 len += strlen(te->name);
3071 *path = calloc(1, len);
3073 return got_error_from_errno();
3076 pt = TAILQ_LAST(parents, tog_parent_trees);
3078 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3079 err = got_error(GOT_ERR_NO_SPACE);
3082 if (strlcat(*path, "/", len) >= len) {
3083 err = got_error(GOT_ERR_NO_SPACE);
3086 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3089 if (strlcat(*path, te->name, len) >= len) {
3090 err = got_error(GOT_ERR_NO_SPACE);
3102 static const struct got_error *
3103 blame_tree_entry(struct tog_view **new_view, int begin_x,
3104 struct got_tree_entry *te, struct tog_parent_trees *parents,
3105 struct got_object_id *commit_id, struct got_repository *repo)
3107 const struct got_error *err = NULL;
3109 struct tog_view *blame_view;
3111 err = tree_entry_path(&path, parents, te);
3115 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3116 if (blame_view == NULL)
3117 return got_error_from_errno();
3119 err = open_blame_view(blame_view, path, commit_id, repo);
3121 view_close(blame_view);
3124 *new_view = blame_view;
3128 static const struct got_error *
3129 log_tree_entry(struct tog_view **new_view, int begin_x,
3130 struct got_tree_entry *te, struct tog_parent_trees *parents,
3131 struct got_object_id *commit_id, struct got_repository *repo)
3133 struct tog_view *log_view;
3134 const struct got_error *err = NULL;
3137 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3138 if (log_view == NULL)
3139 return got_error_from_errno();
3141 err = tree_entry_path(&path, parents, te);
3145 err = open_log_view(log_view, commit_id, repo, path, 0);
3147 view_close(log_view);
3149 *new_view = log_view;
3154 static const struct got_error *
3155 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3156 struct got_object_id *commit_id, struct got_repository *repo)
3158 const struct got_error *err = NULL;
3159 char *commit_id_str = NULL;
3160 struct tog_tree_view_state *s = &view->state.tree;
3162 TAILQ_INIT(&s->parents);
3164 err = got_object_id_str(&commit_id_str, commit_id);
3168 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3169 err = got_error_from_errno();
3173 s->root = s->tree = root;
3174 s->entries = got_object_tree_get_entries(root);
3175 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3176 s->commit_id = got_object_id_dup(commit_id);
3177 if (s->commit_id == NULL) {
3178 err = got_error_from_errno();
3183 view->show = show_tree_view;
3184 view->input = input_tree_view;
3185 view->close = close_tree_view;
3187 free(commit_id_str);
3189 free(s->tree_label);
3190 s->tree_label = NULL;
3195 static const struct got_error *
3196 close_tree_view(struct tog_view *view)
3198 struct tog_tree_view_state *s = &view->state.tree;
3200 free(s->tree_label);
3201 s->tree_label = NULL;
3203 s->commit_id = NULL;
3204 while (!TAILQ_EMPTY(&s->parents)) {
3205 struct tog_parent_tree *parent;
3206 parent = TAILQ_FIRST(&s->parents);
3207 TAILQ_REMOVE(&s->parents, parent, entry);
3211 if (s->tree != s->root)
3212 got_object_tree_close(s->tree);
3213 got_object_tree_close(s->root);
3218 static const struct got_error *
3219 show_tree_view(struct tog_view *view)
3221 const struct got_error *err = NULL;
3222 struct tog_tree_view_state *s = &view->state.tree;
3225 err = tree_entry_path(&parent_path, &s->parents, NULL);
3229 err = draw_tree_entries(view, &s->first_displayed_entry,
3230 &s->last_displayed_entry, &s->selected_entry,
3231 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3232 s->entries, s->selected, view->nlines, s->tree == s->root);
3239 static const struct got_error *
3240 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3241 struct tog_view **focus_view, struct tog_view *view, int ch)
3243 const struct got_error *err = NULL;
3244 struct tog_tree_view_state *s = &view->state.tree;
3245 struct tog_view *log_view;
3250 s->show_ids = !s->show_ids;
3253 if (!s->selected_entry)
3255 if (view_is_parent_view(view))
3256 begin_x = view_split_begin_x(view->begin_x);
3257 err = log_tree_entry(&log_view, begin_x,
3258 s->selected_entry, &s->parents,
3259 s->commit_id, s->repo);
3260 if (view_is_parent_view(view)) {
3261 err = view_close_child(view);
3264 err = view_set_child(view, log_view);
3266 view_close(log_view);
3269 *focus_view = log_view;
3270 view->child_focussed = 1;
3272 *new_view = log_view;
3276 if (s->selected > 0)
3278 if (s->selected > 0)
3280 tree_scroll_up(&s->first_displayed_entry, 1,
3281 s->entries, s->tree == s->root);
3285 if (SIMPLEQ_FIRST(&s->entries->head) ==
3286 s->first_displayed_entry) {
3287 if (s->tree != s->root)
3288 s->first_displayed_entry = NULL;
3291 tree_scroll_up(&s->first_displayed_entry,
3292 view->nlines, s->entries,
3293 s->tree == s->root);
3297 if (s->selected < s->ndisplayed - 1) {
3301 tree_scroll_down(&s->first_displayed_entry, 1,
3302 s->last_displayed_entry, s->entries);
3305 tree_scroll_down(&s->first_displayed_entry,
3306 view->nlines, s->last_displayed_entry,
3308 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3311 /* can't scroll any further; move cursor down */
3312 if (s->selected < s->ndisplayed - 1)
3313 s->selected = s->ndisplayed - 1;
3317 if (s->selected_entry == NULL) {
3318 struct tog_parent_tree *parent;
3320 /* user selected '..' */
3321 if (s->tree == s->root)
3323 parent = TAILQ_FIRST(&s->parents);
3324 TAILQ_REMOVE(&s->parents, parent,
3326 got_object_tree_close(s->tree);
3327 s->tree = parent->tree;
3329 got_object_tree_get_entries(s->tree);
3330 s->first_displayed_entry =
3331 parent->first_displayed_entry;
3333 parent->selected_entry;
3334 s->selected = parent->selected;
3336 } else if (S_ISDIR(s->selected_entry->mode)) {
3337 struct tog_parent_tree *parent;
3338 struct got_tree_object *child;
3339 err = got_object_open_as_tree(&child,
3340 s->repo, s->selected_entry->id);
3343 parent = calloc(1, sizeof(*parent));
3344 if (parent == NULL) {
3345 err = got_error_from_errno();
3348 parent->tree = s->tree;
3349 parent->first_displayed_entry =
3350 s->first_displayed_entry;
3351 parent->selected_entry = s->selected_entry;
3352 parent->selected = s->selected;
3353 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3356 got_object_tree_get_entries(s->tree);
3358 s->first_displayed_entry = NULL;
3359 } else if (S_ISREG(s->selected_entry->mode)) {
3360 struct tog_view *blame_view;
3361 int begin_x = view_is_parent_view(view) ?
3362 view_split_begin_x(view->begin_x) : 0;
3364 err = blame_tree_entry(&blame_view, begin_x,
3365 s->selected_entry, &s->parents, s->commit_id,
3369 if (view_is_parent_view(view)) {
3370 err = view_close_child(view);
3373 err = view_set_child(view, blame_view);
3375 view_close(blame_view);
3378 *focus_view = blame_view;
3379 view->child_focussed = 1;
3381 *new_view = blame_view;
3385 if (s->selected > view->nlines)
3386 s->selected = s->ndisplayed - 1;
3399 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3404 static const struct got_error *
3405 cmd_tree(int argc, char *argv[])
3407 const struct got_error *error;
3408 struct got_repository *repo = NULL;
3409 char *repo_path = NULL;
3410 struct got_object_id *commit_id = NULL;
3411 char *commit_id_arg = NULL;
3412 struct got_commit_object *commit = NULL;
3413 struct got_tree_object *tree = NULL;
3415 struct tog_view *view;
3418 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3423 while ((ch = getopt(argc, argv, "c:")) != -1) {
3426 commit_id_arg = optarg;
3438 repo_path = getcwd(NULL, 0);
3439 if (repo_path == NULL)
3440 return got_error_from_errno();
3441 } else if (argc == 1) {
3442 repo_path = realpath(argv[0], NULL);
3443 if (repo_path == NULL)
3444 return got_error_from_errno();
3448 error = apply_unveil(repo_path, NULL);
3452 error = got_repo_open(&repo, repo_path);
3457 if (commit_id_arg == NULL)
3458 error = get_head_commit_id(&commit_id, repo);
3460 error = got_object_resolve_id_str(&commit_id, repo,
3465 error = got_object_open_as_commit(&commit, repo, commit_id);
3469 error = got_object_open_as_tree(&tree, repo,
3470 got_object_commit_get_tree_id(commit));
3474 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3476 error = got_error_from_errno();
3479 error = open_tree_view(view, tree, commit_id, repo);
3482 error = view_loop(view);
3486 got_object_commit_close(commit);
3488 got_object_tree_close(tree);
3490 got_repo_close(repo);
3499 halfdelay(1); /* Do fast refresh while initial view is loading. */
3502 intrflush(stdscr, FALSE);
3503 keypad(stdscr, TRUE);
3505 signal(SIGWINCH, tog_sigwinch);
3513 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3514 "Available commands:\n", getprogname());
3515 for (i = 0; i < nitems(tog_commands); i++) {
3516 struct tog_cmd *cmd = &tog_commands[i];
3517 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3523 make_argv(const char *arg0, const char *arg1)
3526 int argc = (arg1 == NULL ? 1 : 2);
3528 argv = calloc(argc, sizeof(char *));
3531 argv[0] = strdup(arg0);
3532 if (argv[0] == NULL)
3535 argv[1] = strdup(arg1);
3536 if (argv[1] == NULL)
3544 main(int argc, char *argv[])
3546 const struct got_error *error = NULL;
3547 struct tog_cmd *cmd = NULL;
3549 char **cmd_argv = NULL;
3551 setlocale(LC_ALL, "");
3553 while ((ch = getopt(argc, argv, "h")) != -1) {
3572 /* Build an argument vector which runs a default command. */
3573 cmd = &tog_commands[0];
3574 cmd_argv = make_argv(cmd->name, NULL);
3579 /* Did the user specific a command? */
3580 for (i = 0; i < nitems(tog_commands); i++) {
3581 if (strncmp(tog_commands[i].name, argv[0],
3582 strlen(argv[0])) == 0) {
3583 cmd = &tog_commands[i];
3585 tog_commands[i].cmd_usage();
3590 /* Did the user specify a repository? */
3591 char *repo_path = realpath(argv[0], NULL);
3593 struct got_repository *repo;
3594 error = got_repo_open(&repo, repo_path);
3596 got_repo_close(repo);
3598 error = got_error_from_errno();
3601 fprintf(stderr, "%s: '%s' is not a "
3602 "known command\n", getprogname(),
3606 fprintf(stderr, "%s: '%s' is neither a known "
3607 "command nor a path to a repository\n",
3608 getprogname(), argv[0]);
3612 cmd = &tog_commands[0];
3613 cmd_argv = make_argv(cmd->name, repo_path);
3621 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3628 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);