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", NULL)
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 = got_repo_open(&repo, repo_path);
2153 error = got_object_resolve_id_str(&id1, repo, id_str1);
2157 error = got_object_resolve_id_str(&id2, repo, id_str2);
2161 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2163 error = got_error_from_errno();
2166 error = open_diff_view(view, id1, id2, repo);
2169 error = view_loop(view);
2171 got_repo_close(repo);
2179 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2184 struct tog_blame_line {
2186 struct got_object_id *id;
2189 static const struct got_error *
2190 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2191 const char *path, struct tog_blame_line *lines, int nlines,
2192 int blame_complete, int selected_line, int *first_displayed_line,
2193 int *last_displayed_line, int *eof, int max_lines)
2195 const struct got_error *err;
2196 int lineno = 0, nprinted = 0;
2201 struct tog_blame_line *blame_line;
2202 struct got_object_id *prev_id = NULL;
2205 err = got_object_id_str(&id_str, id);
2210 werase(view->window);
2212 if (asprintf(&line, "commit %s", id_str) == -1) {
2213 err = got_error_from_errno();
2218 err = format_line(&wline, &width, line, view->ncols);
2221 if (view_needs_focus_indication(view))
2222 wstandout(view->window);
2223 waddwstr(view->window, wline);
2224 if (view_needs_focus_indication(view))
2225 wstandend(view->window);
2228 if (width < view->ncols)
2229 waddch(view->window, '\n');
2231 if (asprintf(&line, "[%d/%d] %s%s",
2232 *first_displayed_line - 1 + selected_line, nlines,
2233 blame_complete ? "" : "annotating ", path) == -1) {
2235 return got_error_from_errno();
2238 err = format_line(&wline, &width, line, view->ncols);
2243 waddwstr(view->window, wline);
2246 if (width < view->ncols)
2247 waddch(view->window, '\n');
2250 while (nprinted < max_lines - 2) {
2251 line = parse_next_line(f, &len);
2256 if (++lineno < *first_displayed_line) {
2261 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2262 err = format_line(&wline, &width, line, wlimit);
2268 if (view->focussed && nprinted == selected_line - 1)
2269 wstandout(view->window);
2271 blame_line = &lines[lineno - 1];
2272 if (blame_line->annotated && prev_id &&
2273 got_object_id_cmp(prev_id, blame_line->id) == 0)
2274 waddstr(view->window, " ");
2275 else if (blame_line->annotated) {
2277 err = got_object_id_str(&id_str, blame_line->id);
2283 wprintw(view->window, "%.8s ", id_str);
2285 prev_id = blame_line->id;
2287 waddstr(view->window, "........ ");
2291 waddwstr(view->window, wline);
2292 while (width < wlimit) {
2293 waddch(view->window, ' ');
2296 if (view->focussed && nprinted == selected_line - 1)
2297 wstandend(view->window);
2298 if (++nprinted == 1)
2299 *first_displayed_line = lineno;
2304 *last_displayed_line = lineno;
2311 static const struct got_error *
2312 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2314 const struct got_error *err = NULL;
2315 struct tog_blame_cb_args *a = arg;
2316 struct tog_blame_line *line;
2319 if (nlines != a->nlines ||
2320 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2321 return got_error(GOT_ERR_RANGE);
2323 errcode = pthread_mutex_lock(&tog_mutex);
2325 return got_error_set_errno(errcode);
2327 if (*a->quit) { /* user has quit the blame view */
2328 err = got_error(GOT_ERR_ITER_COMPLETED);
2333 goto done; /* no change in this commit */
2335 line = &a->lines[lineno - 1];
2336 if (line->annotated)
2339 line->id = got_object_id_dup(id);
2340 if (line->id == NULL) {
2341 err = got_error_from_errno();
2344 line->annotated = 1;
2346 errcode = pthread_mutex_unlock(&tog_mutex);
2348 err = got_error_set_errno(errcode);
2353 blame_thread(void *arg)
2355 const struct got_error *err;
2356 struct tog_blame_thread_args *ta = arg;
2357 struct tog_blame_cb_args *a = ta->cb_args;
2360 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2361 blame_cb, ta->cb_args);
2363 errcode = pthread_mutex_lock(&tog_mutex);
2365 return (void *)got_error_set_errno(errcode);
2367 got_repo_close(ta->repo);
2371 errcode = pthread_mutex_unlock(&tog_mutex);
2372 if (errcode && err == NULL)
2373 err = got_error_set_errno(errcode);
2378 static struct got_object_id *
2379 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2382 struct tog_blame_line *line;
2384 line = &lines[first_displayed_line - 1 + selected_line - 1];
2385 if (!line->annotated)
2391 static const struct got_error *
2392 stop_blame(struct tog_blame *blame)
2394 const struct got_error *err = NULL;
2397 if (blame->thread) {
2399 errcode = pthread_mutex_unlock(&tog_mutex);
2401 return got_error_set_errno(errcode);
2402 errcode = pthread_join(blame->thread, (void **)&err);
2404 return got_error_set_errno(errcode);
2405 errcode = pthread_mutex_lock(&tog_mutex);
2407 return got_error_set_errno(errcode);
2408 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2410 blame->thread = NULL;
2412 if (blame->thread_args.repo) {
2413 got_repo_close(blame->thread_args.repo);
2414 blame->thread_args.repo = NULL;
2421 for (i = 0; i < blame->nlines; i++)
2422 free(blame->lines[i].id);
2424 blame->lines = NULL;
2426 free(blame->cb_args.commit_id);
2427 blame->cb_args.commit_id = NULL;
2432 static const struct got_error *
2433 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2434 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2435 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2436 struct got_repository *repo)
2438 const struct got_error *err = NULL;
2439 struct got_blob_object *blob = NULL;
2440 struct got_repository *thread_repo = NULL;
2441 struct got_object_id *obj_id = NULL;
2444 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2448 return got_error(GOT_ERR_NO_OBJ);
2450 err = got_object_get_type(&obj_type, repo, obj_id);
2454 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2455 err = got_error(GOT_ERR_OBJ_TYPE);
2459 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2462 blame->f = got_opentemp();
2463 if (blame->f == NULL) {
2464 err = got_error_from_errno();
2467 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2472 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2473 if (blame->lines == NULL) {
2474 err = got_error_from_errno();
2478 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2482 blame->cb_args.view = view;
2483 blame->cb_args.lines = blame->lines;
2484 blame->cb_args.nlines = blame->nlines;
2485 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2486 if (blame->cb_args.commit_id == NULL) {
2487 err = got_error_from_errno();
2490 blame->cb_args.quit = done;
2492 blame->thread_args.path = path;
2493 blame->thread_args.repo = thread_repo;
2494 blame->thread_args.cb_args = &blame->cb_args;
2495 blame->thread_args.complete = blame_complete;
2496 *blame_complete = 0;
2500 got_object_blob_close(blob);
2507 static const struct got_error *
2508 open_blame_view(struct tog_view *view, char *path,
2509 struct got_object_id *commit_id, struct got_repository *repo)
2511 const struct got_error *err = NULL;
2512 struct tog_blame_view_state *s = &view->state.blame;
2514 SIMPLEQ_INIT(&s->blamed_commits);
2516 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2520 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2521 s->first_displayed_line = 1;
2522 s->last_displayed_line = view->nlines;
2523 s->selected_line = 1;
2524 s->blame_complete = 0;
2526 if (s->path == NULL)
2527 return got_error_from_errno();
2529 s->commit_id = commit_id;
2530 memset(&s->blame, 0, sizeof(s->blame));
2532 view->show = show_blame_view;
2533 view->input = input_blame_view;
2534 view->close = close_blame_view;
2536 return run_blame(&s->blame, view, &s->blame_complete,
2537 &s->first_displayed_line, &s->last_displayed_line,
2538 &s->selected_line, &s->done, &s->eof, s->path,
2539 s->blamed_commit->id, s->repo);
2542 static const struct got_error *
2543 close_blame_view(struct tog_view *view)
2545 const struct got_error *err = NULL;
2546 struct tog_blame_view_state *s = &view->state.blame;
2548 if (s->blame.thread)
2549 err = stop_blame(&s->blame);
2551 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2552 struct got_object_qid *blamed_commit;
2553 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2554 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2555 got_object_qid_free(blamed_commit);
2563 static const struct got_error *
2564 show_blame_view(struct tog_view *view)
2566 const struct got_error *err = NULL;
2567 struct tog_blame_view_state *s = &view->state.blame;
2570 if (s->blame.thread == NULL) {
2571 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2572 &s->blame.thread_args);
2574 return got_error_set_errno(errcode);
2577 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2578 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2579 s->selected_line, &s->first_displayed_line,
2580 &s->last_displayed_line, &s->eof, view->nlines);
2586 static const struct got_error *
2587 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2588 struct tog_view **focus_view, struct tog_view *view, int ch)
2590 const struct got_error *err = NULL, *thread_err = NULL;
2591 struct tog_view *diff_view;
2592 struct tog_blame_view_state *s = &view->state.blame;
2601 if (s->selected_line > 1)
2603 else if (s->selected_line == 1 &&
2604 s->first_displayed_line > 1)
2605 s->first_displayed_line--;
2608 if (s->first_displayed_line == 1) {
2609 s->selected_line = 1;
2612 if (s->first_displayed_line > view->nlines - 2)
2613 s->first_displayed_line -=
2616 s->first_displayed_line = 1;
2620 if (s->selected_line < view->nlines - 2 &&
2621 s->first_displayed_line +
2622 s->selected_line <= s->blame.nlines)
2624 else if (s->last_displayed_line <
2626 s->first_displayed_line++;
2630 struct got_object_id *id = NULL;
2631 id = get_selected_commit_id(s->blame.lines,
2632 s->first_displayed_line, s->selected_line);
2636 struct got_commit_object *commit;
2637 struct got_object_qid *pid;
2638 struct got_object_id *blob_id = NULL;
2640 err = got_object_open_as_commit(&commit,
2644 pid = SIMPLEQ_FIRST(
2645 got_object_commit_get_parent_ids(commit));
2647 got_object_commit_close(commit);
2650 /* Check if path history ends here. */
2651 err = got_object_id_by_path(&blob_id, s->repo,
2654 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2656 got_object_commit_close(commit);
2659 err = got_object_get_type(&obj_type, s->repo,
2662 /* Can't blame non-blob type objects. */
2663 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2664 got_object_commit_close(commit);
2667 err = got_object_qid_alloc(&s->blamed_commit,
2669 got_object_commit_close(commit);
2671 if (got_object_id_cmp(id,
2672 s->blamed_commit->id) == 0)
2674 err = got_object_qid_alloc(&s->blamed_commit,
2680 thread_err = stop_blame(&s->blame);
2684 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2685 s->blamed_commit, entry);
2686 err = run_blame(&s->blame, view, &s->blame_complete,
2687 &s->first_displayed_line, &s->last_displayed_line,
2688 &s->selected_line, &s->done, &s->eof,
2689 s->path, s->blamed_commit->id, s->repo);
2695 struct got_object_qid *first;
2696 first = SIMPLEQ_FIRST(&s->blamed_commits);
2697 if (!got_object_id_cmp(first->id, s->commit_id))
2700 thread_err = stop_blame(&s->blame);
2704 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2705 got_object_qid_free(s->blamed_commit);
2707 SIMPLEQ_FIRST(&s->blamed_commits);
2708 err = run_blame(&s->blame, view, &s->blame_complete,
2709 &s->first_displayed_line, &s->last_displayed_line,
2710 &s->selected_line, &s->done, &s->eof, s->path,
2711 s->blamed_commit->id, s->repo);
2718 struct got_object_id *id = NULL;
2719 struct got_object_qid *pid;
2720 struct got_commit_object *commit = NULL;
2721 id = get_selected_commit_id(s->blame.lines,
2722 s->first_displayed_line, s->selected_line);
2723 if (id == NULL || got_object_id_cmp(id,
2724 s->blamed_commit->id) == 0)
2726 err = got_object_open_as_commit(&commit, s->repo, id);
2729 pid = SIMPLEQ_FIRST(
2730 got_object_commit_get_parent_ids(commit));
2731 if (view_is_parent_view(view))
2732 begin_x = view_split_begin_x(view->begin_x);
2733 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2734 if (diff_view == NULL) {
2735 got_object_commit_close(commit);
2736 err = got_error_from_errno();
2739 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2741 got_object_commit_close(commit);
2743 view_close(diff_view);
2746 if (view_is_parent_view(view)) {
2747 err = view_close_child(view);
2750 err = view_set_child(view, diff_view);
2752 view_close(diff_view);
2755 *focus_view = diff_view;
2756 view->child_focussed = 1;
2758 *new_view = diff_view;
2765 if (s->last_displayed_line >= s->blame.nlines &&
2766 s->selected_line < view->nlines - 2) {
2767 s->selected_line = MIN(s->blame.nlines,
2771 if (s->last_displayed_line + view->nlines - 2
2773 s->first_displayed_line +=
2776 s->first_displayed_line =
2781 if (s->selected_line > view->nlines - 2) {
2782 s->selected_line = MIN(s->blame.nlines,
2789 return thread_err ? thread_err : err;
2792 static const struct got_error *
2793 cmd_blame(int argc, char *argv[])
2795 const struct got_error *error;
2796 struct got_repository *repo = NULL;
2797 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2798 struct got_object_id *commit_id = NULL;
2799 char *commit_id_str = NULL;
2801 struct tog_view *view;
2804 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2809 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2812 commit_id_str = optarg;
2815 repo_path = realpath(optarg, NULL);
2816 if (repo_path == NULL)
2817 err(1, "-r option");
2833 cwd = getcwd(NULL, 0);
2835 error = got_error_from_errno();
2838 if (repo_path == NULL) {
2839 repo_path = strdup(cwd);
2840 if (repo_path == NULL) {
2841 error = got_error_from_errno();
2847 error = got_repo_open(&repo, repo_path);
2851 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2855 if (commit_id_str == NULL) {
2856 struct got_reference *head_ref;
2857 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2860 error = got_ref_resolve(&commit_id, repo, head_ref);
2861 got_ref_close(head_ref);
2863 error = got_object_resolve_id_str(&commit_id, repo,
2869 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2871 error = got_error_from_errno();
2874 error = open_blame_view(view, in_repo_path, commit_id, repo);
2877 error = view_loop(view);
2883 got_repo_close(repo);
2887 static const struct got_error *
2888 draw_tree_entries(struct tog_view *view,
2889 struct got_tree_entry **first_displayed_entry,
2890 struct got_tree_entry **last_displayed_entry,
2891 struct got_tree_entry **selected_entry, int *ndisplayed,
2892 const char *label, int show_ids, const char *parent_path,
2893 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2895 const struct got_error *err = NULL;
2896 struct got_tree_entry *te;
2902 werase(view->window);
2907 err = format_line(&wline, &width, label, view->ncols);
2910 if (view_needs_focus_indication(view))
2911 wstandout(view->window);
2912 waddwstr(view->window, wline);
2913 if (view_needs_focus_indication(view))
2914 wstandend(view->window);
2917 if (width < view->ncols)
2918 waddch(view->window, '\n');
2921 err = format_line(&wline, &width, parent_path, view->ncols);
2924 waddwstr(view->window, wline);
2927 if (width < view->ncols)
2928 waddch(view->window, '\n');
2931 waddch(view->window, '\n');
2935 te = SIMPLEQ_FIRST(&entries->head);
2936 if (*first_displayed_entry == NULL) {
2937 if (selected == 0) {
2939 wstandout(view->window);
2940 *selected_entry = NULL;
2942 waddstr(view->window, " ..\n"); /* parent directory */
2943 if (selected == 0 && view->focussed)
2944 wstandend(view->window);
2951 while (te != *first_displayed_entry)
2952 te = SIMPLEQ_NEXT(te, entry);
2956 char *line = NULL, *id_str = NULL;
2959 err = got_object_id_str(&id_str, te->id);
2961 return got_error_from_errno();
2963 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2964 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2966 return got_error_from_errno();
2969 err = format_line(&wline, &width, line, view->ncols);
2974 if (n == selected) {
2976 wstandout(view->window);
2977 *selected_entry = te;
2979 waddwstr(view->window, wline);
2980 if (width < view->ncols)
2981 waddch(view->window, '\n');
2982 if (n == selected && view->focussed)
2983 wstandend(view->window);
2989 *last_displayed_entry = te;
2992 te = SIMPLEQ_NEXT(te, entry);
2999 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3000 const struct got_tree_entries *entries, int isroot)
3002 struct got_tree_entry *te, *prev;
3005 if (*first_displayed_entry == NULL)
3008 te = SIMPLEQ_FIRST(&entries->head);
3009 if (*first_displayed_entry == te) {
3011 *first_displayed_entry = NULL;
3015 /* XXX this is stupid... switch to TAILQ? */
3016 for (i = 0; i < maxscroll; i++) {
3017 while (te != *first_displayed_entry) {
3019 te = SIMPLEQ_NEXT(te, entry);
3021 *first_displayed_entry = prev;
3022 te = SIMPLEQ_FIRST(&entries->head);
3024 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3025 *first_displayed_entry = NULL;
3029 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3030 struct got_tree_entry *last_displayed_entry,
3031 const struct got_tree_entries *entries)
3033 struct got_tree_entry *next;
3036 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
3039 if (*first_displayed_entry)
3040 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3042 next = SIMPLEQ_FIRST(&entries->head);
3044 *first_displayed_entry = next;
3045 if (++n >= maxscroll)
3047 next = SIMPLEQ_NEXT(next, entry);
3051 static const struct got_error *
3052 tree_entry_path(char **path, struct tog_parent_trees *parents,
3053 struct got_tree_entry *te)
3055 const struct got_error *err = NULL;
3056 struct tog_parent_tree *pt;
3057 size_t len = 2; /* for leading slash and NUL */
3059 TAILQ_FOREACH(pt, parents, entry)
3060 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3062 len += strlen(te->name);
3064 *path = calloc(1, len);
3066 return got_error_from_errno();
3069 pt = TAILQ_LAST(parents, tog_parent_trees);
3071 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3072 err = got_error(GOT_ERR_NO_SPACE);
3075 if (strlcat(*path, "/", len) >= len) {
3076 err = got_error(GOT_ERR_NO_SPACE);
3079 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3082 if (strlcat(*path, te->name, len) >= len) {
3083 err = got_error(GOT_ERR_NO_SPACE);
3095 static const struct got_error *
3096 blame_tree_entry(struct tog_view **new_view, int begin_x,
3097 struct got_tree_entry *te, struct tog_parent_trees *parents,
3098 struct got_object_id *commit_id, struct got_repository *repo)
3100 const struct got_error *err = NULL;
3102 struct tog_view *blame_view;
3104 err = tree_entry_path(&path, parents, te);
3108 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3109 if (blame_view == NULL)
3110 return got_error_from_errno();
3112 err = open_blame_view(blame_view, path, commit_id, repo);
3114 view_close(blame_view);
3117 *new_view = blame_view;
3121 static const struct got_error *
3122 log_tree_entry(struct tog_view **new_view, int begin_x,
3123 struct got_tree_entry *te, struct tog_parent_trees *parents,
3124 struct got_object_id *commit_id, struct got_repository *repo)
3126 struct tog_view *log_view;
3127 const struct got_error *err = NULL;
3130 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3131 if (log_view == NULL)
3132 return got_error_from_errno();
3134 err = tree_entry_path(&path, parents, te);
3138 err = open_log_view(log_view, commit_id, repo, path, 0);
3140 view_close(log_view);
3142 *new_view = log_view;
3147 static const struct got_error *
3148 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3149 struct got_object_id *commit_id, struct got_repository *repo)
3151 const struct got_error *err = NULL;
3152 char *commit_id_str = NULL;
3153 struct tog_tree_view_state *s = &view->state.tree;
3155 TAILQ_INIT(&s->parents);
3157 err = got_object_id_str(&commit_id_str, commit_id);
3161 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3162 err = got_error_from_errno();
3166 s->root = s->tree = root;
3167 s->entries = got_object_tree_get_entries(root);
3168 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3169 s->commit_id = got_object_id_dup(commit_id);
3170 if (s->commit_id == NULL) {
3171 err = got_error_from_errno();
3176 view->show = show_tree_view;
3177 view->input = input_tree_view;
3178 view->close = close_tree_view;
3180 free(commit_id_str);
3182 free(s->tree_label);
3183 s->tree_label = NULL;
3188 static const struct got_error *
3189 close_tree_view(struct tog_view *view)
3191 struct tog_tree_view_state *s = &view->state.tree;
3193 free(s->tree_label);
3194 s->tree_label = NULL;
3196 s->commit_id = NULL;
3197 while (!TAILQ_EMPTY(&s->parents)) {
3198 struct tog_parent_tree *parent;
3199 parent = TAILQ_FIRST(&s->parents);
3200 TAILQ_REMOVE(&s->parents, parent, entry);
3204 if (s->tree != s->root)
3205 got_object_tree_close(s->tree);
3206 got_object_tree_close(s->root);
3211 static const struct got_error *
3212 show_tree_view(struct tog_view *view)
3214 const struct got_error *err = NULL;
3215 struct tog_tree_view_state *s = &view->state.tree;
3218 err = tree_entry_path(&parent_path, &s->parents, NULL);
3222 err = draw_tree_entries(view, &s->first_displayed_entry,
3223 &s->last_displayed_entry, &s->selected_entry,
3224 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3225 s->entries, s->selected, view->nlines, s->tree == s->root);
3232 static const struct got_error *
3233 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3234 struct tog_view **focus_view, struct tog_view *view, int ch)
3236 const struct got_error *err = NULL;
3237 struct tog_tree_view_state *s = &view->state.tree;
3238 struct tog_view *log_view;
3243 s->show_ids = !s->show_ids;
3246 if (!s->selected_entry)
3248 if (view_is_parent_view(view))
3249 begin_x = view_split_begin_x(view->begin_x);
3250 err = log_tree_entry(&log_view, begin_x,
3251 s->selected_entry, &s->parents,
3252 s->commit_id, s->repo);
3253 if (view_is_parent_view(view)) {
3254 err = view_close_child(view);
3257 err = view_set_child(view, log_view);
3259 view_close(log_view);
3262 *focus_view = log_view;
3263 view->child_focussed = 1;
3265 *new_view = log_view;
3269 if (s->selected > 0)
3271 if (s->selected > 0)
3273 tree_scroll_up(&s->first_displayed_entry, 1,
3274 s->entries, s->tree == s->root);
3278 if (SIMPLEQ_FIRST(&s->entries->head) ==
3279 s->first_displayed_entry) {
3280 if (s->tree != s->root)
3281 s->first_displayed_entry = NULL;
3284 tree_scroll_up(&s->first_displayed_entry,
3285 view->nlines, s->entries,
3286 s->tree == s->root);
3290 if (s->selected < s->ndisplayed - 1) {
3294 tree_scroll_down(&s->first_displayed_entry, 1,
3295 s->last_displayed_entry, s->entries);
3298 tree_scroll_down(&s->first_displayed_entry,
3299 view->nlines, s->last_displayed_entry,
3301 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3304 /* can't scroll any further; move cursor down */
3305 if (s->selected < s->ndisplayed - 1)
3306 s->selected = s->ndisplayed - 1;
3310 if (s->selected_entry == NULL) {
3311 struct tog_parent_tree *parent;
3313 /* user selected '..' */
3314 if (s->tree == s->root)
3316 parent = TAILQ_FIRST(&s->parents);
3317 TAILQ_REMOVE(&s->parents, parent,
3319 got_object_tree_close(s->tree);
3320 s->tree = parent->tree;
3322 got_object_tree_get_entries(s->tree);
3323 s->first_displayed_entry =
3324 parent->first_displayed_entry;
3326 parent->selected_entry;
3327 s->selected = parent->selected;
3329 } else if (S_ISDIR(s->selected_entry->mode)) {
3330 struct tog_parent_tree *parent;
3331 struct got_tree_object *child;
3332 err = got_object_open_as_tree(&child,
3333 s->repo, s->selected_entry->id);
3336 parent = calloc(1, sizeof(*parent));
3337 if (parent == NULL) {
3338 err = got_error_from_errno();
3341 parent->tree = s->tree;
3342 parent->first_displayed_entry =
3343 s->first_displayed_entry;
3344 parent->selected_entry = s->selected_entry;
3345 parent->selected = s->selected;
3346 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3349 got_object_tree_get_entries(s->tree);
3351 s->first_displayed_entry = NULL;
3352 } else if (S_ISREG(s->selected_entry->mode)) {
3353 struct tog_view *blame_view;
3354 int begin_x = view_is_parent_view(view) ?
3355 view_split_begin_x(view->begin_x) : 0;
3357 err = blame_tree_entry(&blame_view, begin_x,
3358 s->selected_entry, &s->parents, s->commit_id,
3362 if (view_is_parent_view(view)) {
3363 err = view_close_child(view);
3366 err = view_set_child(view, blame_view);
3368 view_close(blame_view);
3371 *focus_view = blame_view;
3372 view->child_focussed = 1;
3374 *new_view = blame_view;
3378 if (s->selected > view->nlines)
3379 s->selected = s->ndisplayed - 1;
3392 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3397 static const struct got_error *
3398 cmd_tree(int argc, char *argv[])
3400 const struct got_error *error;
3401 struct got_repository *repo = NULL;
3402 char *repo_path = NULL;
3403 struct got_object_id *commit_id = NULL;
3404 char *commit_id_arg = NULL;
3405 struct got_commit_object *commit = NULL;
3406 struct got_tree_object *tree = NULL;
3408 struct tog_view *view;
3411 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3416 while ((ch = getopt(argc, argv, "c:")) != -1) {
3419 commit_id_arg = optarg;
3431 repo_path = getcwd(NULL, 0);
3432 if (repo_path == NULL)
3433 return got_error_from_errno();
3434 } else if (argc == 1) {
3435 repo_path = realpath(argv[0], NULL);
3436 if (repo_path == NULL)
3437 return got_error_from_errno();
3441 error = got_repo_open(&repo, repo_path);
3446 if (commit_id_arg == NULL)
3447 error = get_head_commit_id(&commit_id, repo);
3449 error = got_object_resolve_id_str(&commit_id, repo,
3454 error = got_object_open_as_commit(&commit, repo, commit_id);
3458 error = got_object_open_as_tree(&tree, repo,
3459 got_object_commit_get_tree_id(commit));
3463 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3465 error = got_error_from_errno();
3468 error = open_tree_view(view, tree, commit_id, repo);
3471 error = view_loop(view);
3475 got_object_commit_close(commit);
3477 got_object_tree_close(tree);
3479 got_repo_close(repo);
3488 halfdelay(1); /* Do fast refresh while initial view is loading. */
3491 intrflush(stdscr, FALSE);
3492 keypad(stdscr, TRUE);
3494 signal(SIGWINCH, tog_sigwinch);
3502 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3503 "Available commands:\n", getprogname());
3504 for (i = 0; i < nitems(tog_commands); i++) {
3505 struct tog_cmd *cmd = &tog_commands[i];
3506 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3512 make_argv(const char *arg0, const char *arg1)
3515 int argc = (arg1 == NULL ? 1 : 2);
3517 argv = calloc(argc, sizeof(char *));
3520 argv[0] = strdup(arg0);
3521 if (argv[0] == NULL)
3524 argv[1] = strdup(arg1);
3525 if (argv[1] == NULL)
3533 main(int argc, char *argv[])
3535 const struct got_error *error = NULL;
3536 struct tog_cmd *cmd = NULL;
3538 char **cmd_argv = NULL;
3540 setlocale(LC_ALL, "");
3542 while ((ch = getopt(argc, argv, "h")) != -1) {
3561 /* Build an argument vector which runs a default command. */
3562 cmd = &tog_commands[0];
3563 cmd_argv = make_argv(cmd->name, NULL);
3568 /* Did the user specific a command? */
3569 for (i = 0; i < nitems(tog_commands); i++) {
3570 if (strncmp(tog_commands[i].name, argv[0],
3571 strlen(argv[0])) == 0) {
3572 cmd = &tog_commands[i];
3574 tog_commands[i].cmd_usage();
3579 /* Did the user specify a repository? */
3580 char *repo_path = realpath(argv[0], NULL);
3582 struct got_repository *repo;
3583 error = got_repo_open(&repo, repo_path);
3585 got_repo_close(repo);
3587 error = got_error_from_errno();
3590 fprintf(stderr, "%s: '%s' is not a "
3591 "known command\n", getprogname(),
3595 fprintf(stderr, "%s: '%s' is neither a known "
3596 "command nor a path to a repository\n",
3597 getprogname(), argv[0]);
3601 cmd = &tog_commands[0];
3602 cmd_argv = make_argv(cmd->name, repo_path);
3610 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3617 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);