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"
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 const struct got_error *(*cmd_main)(int, char *[]);
66 void (*cmd_usage)(void);
70 __dead static void usage(void);
71 __dead static void usage_log(void);
72 __dead static void usage_diff(void);
73 __dead static void usage_blame(void);
74 __dead static void usage_tree(void);
76 static const struct got_error* cmd_log(int, char *[]);
77 static const struct got_error* cmd_diff(int, char *[]);
78 static const struct got_error* cmd_blame(int, char *[]);
79 static const struct got_error* cmd_tree(int, char *[]);
81 static struct tog_cmd tog_commands[] = {
82 { "log", cmd_log, usage_log,
83 "show repository history" },
84 { "diff", cmd_diff, usage_diff,
85 "compare files and directories" },
86 { "blame", cmd_blame, usage_blame,
87 "show line-by-line file history" },
88 { "tree", cmd_tree, usage_tree,
89 "browse trees in repository" },
99 struct tog_diff_view_state {
100 struct got_object_id *id1, *id2;
102 int first_displayed_line;
103 int last_displayed_line;
106 struct got_repository *repo;
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
118 struct commit_queue_head head;
121 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
123 struct tog_log_thread_args {
124 pthread_cond_t need_commits;
126 struct got_commit_graph *graph;
127 struct commit_queue *commits;
128 const char *in_repo_path;
129 struct got_object_id *start_id;
130 struct got_repository *repo;
133 struct tog_view *view;
134 struct commit_queue_entry **first_displayed_entry;
135 struct commit_queue_entry **selected_entry;
138 struct tog_log_view_state {
139 struct commit_queue commits;
140 struct commit_queue_entry *first_displayed_entry;
141 struct commit_queue_entry *last_displayed_entry;
142 struct commit_queue_entry *selected_entry;
145 struct got_repository *repo;
146 struct got_object_id *start_id;
149 struct tog_log_thread_args thread_args;
152 struct tog_blame_cb_args {
153 struct tog_blame_line *lines; /* one per line */
156 struct tog_view *view;
157 struct got_object_id *commit_id;
161 struct tog_blame_thread_args {
163 struct got_repository *repo;
164 struct tog_blame_cb_args *cb_args;
171 struct tog_blame_line *lines;
174 struct tog_blame_thread_args thread_args;
175 struct tog_blame_cb_args cb_args;
179 struct tog_blame_view_state {
180 int first_displayed_line;
181 int last_displayed_line;
186 struct got_object_id_queue blamed_commits;
187 struct got_object_qid *blamed_commit;
189 struct got_repository *repo;
190 struct got_object_id *commit_id;
191 struct tog_blame blame;
194 struct tog_parent_tree {
195 TAILQ_ENTRY(tog_parent_tree) entry;
196 struct got_tree_object *tree;
197 struct got_tree_entry *first_displayed_entry;
198 struct got_tree_entry *selected_entry;
202 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
204 struct tog_tree_view_state {
206 struct got_tree_object *root;
207 struct got_tree_object *tree;
208 const struct got_tree_entries *entries;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *last_displayed_entry;
211 struct got_tree_entry *selected_entry;
212 int nentries, ndisplayed, selected, show_ids;
213 struct tog_parent_trees parents;
214 struct got_object_id *commit_id;
215 struct got_repository *repo;
219 * We implement two types of views: parent views and child views.
221 * The 'Tab' key switches between a parent view and its child view.
222 * Child views are shown side-by-side to their parent view, provided
223 * there is enough screen estate.
225 * When a new view is opened from within a parent view, this new view
226 * becomes a child view of the parent view, replacing any existing child.
228 * When a new view is opened from within a child view, this new view
229 * becomes a parent view which will obscure the views below until the
230 * user quits the new parent view by typing 'q'.
232 * This list of views contains parent views only.
233 * Child views are only pointed to by their parent view.
235 TAILQ_HEAD(tog_view_list_head, tog_view);
238 TAILQ_ENTRY(tog_view) entry;
241 int nlines, ncols, begin_y, begin_x;
242 int lines, cols; /* copies of LINES and COLS */
244 struct tog_view *parent;
245 struct tog_view *child;
248 /* type-specific state */
249 enum tog_view_type type;
251 struct tog_diff_view_state diff;
252 struct tog_log_view_state log;
253 struct tog_blame_view_state blame;
254 struct tog_tree_view_state tree;
257 const struct got_error *(*show)(struct tog_view *);
258 const struct got_error *(*input)(struct tog_view **,
259 struct tog_view **, struct tog_view**, struct tog_view *, int);
260 const struct got_error *(*close)(struct tog_view *);
263 static const struct got_error *open_diff_view(struct tog_view *,
264 struct got_object_id *, struct got_object_id *, struct got_repository *);
265 static const struct got_error *show_diff_view(struct tog_view *);
266 static const struct got_error *input_diff_view(struct tog_view **,
267 struct tog_view **, struct tog_view **, struct tog_view *, int);
268 static const struct got_error* close_diff_view(struct tog_view *);
270 static const struct got_error *open_log_view(struct tog_view *,
271 struct got_object_id *, struct got_repository *, const char *, int);
272 static const struct got_error * show_log_view(struct tog_view *);
273 static const struct got_error *input_log_view(struct tog_view **,
274 struct tog_view **, struct tog_view **, struct tog_view *, int);
275 static const struct got_error *close_log_view(struct tog_view *);
277 static const struct got_error *open_blame_view(struct tog_view *, char *,
278 struct got_object_id *, struct got_repository *);
279 static const struct got_error *show_blame_view(struct tog_view *);
280 static const struct got_error *input_blame_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error *close_blame_view(struct tog_view *);
284 static const struct got_error *open_tree_view(struct tog_view *,
285 struct got_tree_object *, struct got_object_id *, struct got_repository *);
286 static const struct got_error *show_tree_view(struct tog_view *);
287 static const struct got_error *input_tree_view(struct tog_view **,
288 struct tog_view **, struct tog_view **, struct tog_view *, int);
289 static const struct got_error *close_tree_view(struct tog_view *);
291 static volatile sig_atomic_t tog_sigwinch_received;
294 tog_sigwinch(int signo)
296 tog_sigwinch_received = 1;
299 static const struct got_error *
300 view_close(struct tog_view *view)
302 const struct got_error *err = NULL;
305 view_close(view->child);
309 err = view->close(view);
311 del_panel(view->panel);
313 delwin(view->window);
318 static struct tog_view *
319 view_open(int nlines, int ncols, int begin_y, int begin_x,
320 enum tog_view_type type)
322 struct tog_view *view = calloc(1, sizeof(*view));
330 view->nlines = nlines ? nlines : LINES - begin_y;
331 view->ncols = ncols ? ncols : COLS - begin_x;
332 view->begin_y = begin_y;
333 view->begin_x = begin_x;
334 view->window = newwin(nlines, ncols, begin_y, begin_x);
335 if (view->window == NULL) {
339 view->panel = new_panel(view->window);
340 if (view->panel == NULL ||
341 set_panel_userptr(view->panel, view) != OK) {
346 keypad(view->window, TRUE);
351 view_split_begin_x(int begin_x)
353 if (begin_x > 0 || COLS < 120)
355 return (COLS - MAX(COLS / 2, 80));
358 static const struct got_error *view_resize(struct tog_view *);
360 static const struct got_error *
361 view_splitscreen(struct tog_view *view)
363 const struct got_error *err = NULL;
366 view->begin_x = view_split_begin_x(0);
367 view->nlines = LINES;
368 view->ncols = COLS - view->begin_x;
371 err = view_resize(view);
375 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
376 return got_error_from_errno();
381 static const struct got_error *
382 view_fullscreen(struct tog_view *view)
384 const struct got_error *err = NULL;
388 view->nlines = LINES;
392 err = view_resize(view);
396 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
397 return got_error_from_errno();
403 view_is_parent_view(struct tog_view *view)
405 return view->parent == NULL;
408 static const struct got_error *
409 view_resize(struct tog_view *view)
413 if (view->lines > LINES)
414 nlines = view->nlines - (view->lines - LINES);
416 nlines = view->nlines + (LINES - view->lines);
418 if (view->cols > COLS)
419 ncols = view->ncols - (view->cols - COLS);
421 ncols = view->ncols + (COLS - view->cols);
423 if (wresize(view->window, nlines, ncols) == ERR)
424 return got_error_from_errno();
425 if (replace_panel(view->panel, view->window) == ERR)
426 return got_error_from_errno();
427 wclear(view->window);
429 view->nlines = nlines;
435 view->child->begin_x = view_split_begin_x(view->begin_x);
436 if (view->child->begin_x == 0) {
437 view_fullscreen(view->child);
438 if (view->child->focussed)
439 show_panel(view->child->panel);
441 show_panel(view->panel);
443 view_splitscreen(view->child);
444 show_panel(view->child->panel);
451 static const struct got_error *
452 view_close_child(struct tog_view *view)
454 const struct got_error *err = NULL;
456 if (view->child == NULL)
459 err = view_close(view->child);
464 static const struct got_error *
465 view_set_child(struct tog_view *view, struct tog_view *child)
467 const struct got_error *err = NULL;
470 child->parent = view;
475 view_is_splitscreen(struct tog_view *view)
477 return !view_is_parent_view(view) && view->begin_x > 0;
486 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
487 cols = 80; /* Default */
493 resize_term(lines, cols);
496 static const struct got_error *
497 view_input(struct tog_view **new, struct tog_view **dead,
498 struct tog_view **focus, int *done, struct tog_view *view,
499 struct tog_view_list_head *views)
501 const struct got_error *err = NULL;
509 nodelay(stdscr, FALSE);
510 /* Allow threads to make progress while we are waiting for input. */
511 errcode = pthread_mutex_unlock(&tog_mutex);
513 return got_error_set_errno(errcode);
514 ch = wgetch(view->window);
515 errcode = pthread_mutex_lock(&tog_mutex);
517 return got_error_set_errno(errcode);
518 nodelay(stdscr, TRUE);
520 if (tog_sigwinch_received) {
522 tog_sigwinch_received = 0;
523 TAILQ_FOREACH(v, views, entry) {
524 err = view_resize(v);
527 err = v->input(new, dead, focus, v, KEY_RESIZE);
538 *focus = view->child;
539 view->child_focussed = 1;
540 } else if (view->parent) {
541 *focus = view->parent;
542 view->parent->child_focussed = 0;
546 err = view->input(new, dead, focus, view, ch);
553 if (view_is_parent_view(view)) {
554 if (view->child == NULL)
556 if (view_is_splitscreen(view->child)) {
557 *focus = view->child;
558 view->child_focussed = 1;
559 err = view_fullscreen(view->child);
561 err = view_splitscreen(view->child);
564 err = view->child->input(new, dead, focus,
565 view->child, KEY_RESIZE);
567 if (view_is_splitscreen(view)) {
569 view->parent->child_focussed = 1;
570 err = view_fullscreen(view);
572 err = view_splitscreen(view);
576 err = view->input(new, dead, focus, view,
583 err = view->input(new, dead, focus, view, ch);
591 view_vborder(struct tog_view *view)
594 struct tog_view *view_above;
597 return view_vborder(view->parent);
599 panel = panel_above(view->panel);
603 view_above = panel_userptr(panel);
604 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
605 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
609 view_needs_focus_indication(struct tog_view *view)
611 if (view_is_parent_view(view)) {
612 if (view->child == NULL || view->child_focussed)
614 if (!view_is_splitscreen(view->child))
616 } else if (!view_is_splitscreen(view))
619 return view->focussed;
622 static const struct got_error *
623 view_loop(struct tog_view *view)
625 const struct got_error *err = NULL;
626 struct tog_view_list_head views;
627 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
628 int fast_refresh = 10;
629 int done = 0, errcode;
631 errcode = pthread_mutex_lock(&tog_mutex);
633 return got_error_set_errno(errcode);
636 TAILQ_INSERT_HEAD(&views, view, entry);
640 err = view->show(view);
645 while (!TAILQ_EMPTY(&views) && !done) {
646 /* Refresh fast during initialization, then become slower. */
647 if (fast_refresh && fast_refresh-- == 0)
648 halfdelay(10); /* switch to once per second */
650 err = view_input(&new_view, &dead_view, &focus_view, &done,
655 struct tog_view *prev = NULL;
657 if (view_is_parent_view(dead_view))
658 prev = TAILQ_PREV(dead_view,
659 tog_view_list_head, entry);
660 else if (view->parent != dead_view)
663 if (dead_view->parent)
664 dead_view->parent->child = NULL;
666 TAILQ_REMOVE(&views, dead_view, entry);
668 err = view_close(dead_view);
669 if (err || dead_view == main_view)
672 if (view == dead_view) {
677 else if (!TAILQ_EMPTY(&views))
678 view = TAILQ_LAST(&views,
683 if (view->child && view->child_focussed)
684 focus_view = view->child;
691 struct tog_view *v, *t;
692 /* Only allow one parent view per type. */
693 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
694 if (v->type != new_view->type)
696 TAILQ_REMOVE(&views, v, entry);
702 TAILQ_INSERT_TAIL(&views, new_view, entry);
704 if (focus_view == NULL)
705 focus_view = new_view;
708 show_panel(focus_view->panel);
711 focus_view->focussed = 1;
714 show_panel(new_view->panel);
715 if (view->child && view_is_splitscreen(view->child))
716 show_panel(view->child->panel);
719 if (focus_view == NULL) {
721 show_panel(view->panel);
722 if (view->child && view_is_splitscreen(view->child))
723 show_panel(view->child->panel);
727 err = view->parent->show(view->parent);
731 err = view->show(view);
735 err = view->child->show(view->child);
744 while (!TAILQ_EMPTY(&views)) {
745 view = TAILQ_FIRST(&views);
746 TAILQ_REMOVE(&views, view, entry);
750 errcode = pthread_mutex_unlock(&tog_mutex);
752 return got_error_set_errno(errcode);
762 "usage: %s log [-c commit] [-r repository-path] [path]\n",
767 /* Create newly allocated wide-character string equivalent to a byte string. */
768 static const struct got_error *
769 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
772 const struct got_error *err = NULL;
775 *wlen = mbstowcs(NULL, s, 0);
776 if (*wlen == (size_t)-1) {
779 return got_error_from_errno();
781 /* byte string invalid in current encoding; try to "fix" it */
782 err = got_mbsavis(&vis, &vislen, s);
785 *wlen = mbstowcs(NULL, vis, 0);
786 if (*wlen == (size_t)-1) {
787 err = got_error_from_errno(); /* give up */
792 *ws = calloc(*wlen + 1, sizeof(*ws));
794 err = got_error_from_errno();
798 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
799 err = got_error_from_errno();
810 /* Format a line for display, ensuring that it won't overflow a width limit. */
811 static const struct got_error *
812 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
814 const struct got_error *err = NULL;
816 wchar_t *wline = NULL;
823 err = mbs2ws(&wline, &wlen, line);
828 while (i < wlen && cols < wlimit) {
829 int width = wcwidth(wline[i]);
836 if (cols + width <= wlimit)
841 if (wline[i] == L'\t')
842 cols += TABSIZE - ((cols + 1) % TABSIZE);
846 err = got_error_from_errno();
861 static const struct got_error *
862 draw_commit(struct tog_view *view, struct got_commit_object *commit,
863 struct got_object_id *id)
865 const struct got_error *err = NULL;
866 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
867 char *logmsg0 = NULL, *logmsg = NULL;
868 char *author0 = NULL, *author = NULL;
869 wchar_t *wlogmsg = NULL, *wauthor = NULL;
870 int author_width, logmsg_width;
871 char *newline, *smallerthan;
874 static const size_t date_display_cols = 9;
875 static const size_t author_display_cols = 16;
876 const int avail = view->ncols;
878 time_t committer_time;
880 committer_time = got_object_commit_get_committer_time(commit);
881 if (localtime_r(&committer_time, &tm) == NULL)
882 return got_error_from_errno();
883 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
885 return got_error(GOT_ERR_NO_SPACE);
887 if (avail < date_display_cols)
888 limit = MIN(sizeof(datebuf) - 1, avail);
890 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
891 waddnstr(view->window, datebuf, limit);
896 author0 = strdup(got_object_commit_get_author(commit));
897 if (author0 == NULL) {
898 err = got_error_from_errno();
902 smallerthan = strchr(author, '<');
906 char *at = strchr(author, '@');
911 err = format_line(&wauthor, &author_width, author, limit);
914 waddwstr(view->window, wauthor);
916 while (col <= avail && author_width < author_display_cols + 1) {
917 waddch(view->window, ' ');
924 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
925 if (logmsg0 == NULL) {
926 err = got_error_from_errno();
930 while (*logmsg == '\n')
932 newline = strchr(logmsg, '\n');
936 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
939 waddwstr(view->window, wlogmsg);
941 while (col <= avail) {
942 waddch(view->window, ' ');
954 static struct commit_queue_entry *
955 alloc_commit_queue_entry(struct got_commit_object *commit,
956 struct got_object_id *id)
958 struct commit_queue_entry *entry;
960 entry = calloc(1, sizeof(*entry));
965 entry->commit = commit;
970 pop_commit(struct commit_queue *commits)
972 struct commit_queue_entry *entry;
974 entry = TAILQ_FIRST(&commits->head);
975 TAILQ_REMOVE(&commits->head, entry, entry);
976 got_object_commit_close(entry->commit);
978 /* Don't free entry->id! It is owned by the commit graph. */
983 free_commits(struct commit_queue *commits)
985 while (!TAILQ_EMPTY(&commits->head))
989 static const struct got_error *
990 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
991 int minqueue, struct got_repository *repo, const char *path)
993 const struct got_error *err = NULL;
997 * We keep all commits open throughout the lifetime of the log
998 * view in order to avoid having to re-fetch commits from disk
999 * while updating the display.
1001 while (nqueued < minqueue) {
1002 struct got_object_id *id;
1003 struct got_commit_object *commit;
1004 struct commit_queue_entry *entry;
1007 err = got_commit_graph_iter_next(&id, graph);
1009 if (err->code != GOT_ERR_ITER_NEED_MORE)
1011 err = got_commit_graph_fetch_commits(graph,
1021 err = got_object_open_as_commit(&commit, repo, id);
1024 entry = alloc_commit_queue_entry(commit, id);
1025 if (entry == NULL) {
1026 err = got_error_from_errno();
1030 errcode = pthread_mutex_lock(&tog_mutex);
1032 err = got_error_set_errno(errcode);
1036 entry->idx = commits->ncommits;
1037 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1039 commits->ncommits++;
1041 errcode = pthread_mutex_unlock(&tog_mutex);
1042 if (errcode && err == NULL)
1043 err = got_error_set_errno(errcode);
1049 static const struct got_error *
1050 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1052 const struct got_error *err = NULL;
1053 struct got_reference *head_ref;
1057 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1061 err = got_ref_resolve(head_id, repo, head_ref);
1062 got_ref_close(head_ref);
1071 static const struct got_error *
1072 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1073 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1074 struct commit_queue *commits, int selected_idx, int limit,
1075 const char *path, int commits_needed)
1077 const struct got_error *err = NULL;
1078 struct commit_queue_entry *entry;
1079 int ncommits, width;
1080 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1086 if (ncommits == selected_idx) {
1090 entry = TAILQ_NEXT(entry, entry);
1095 err = got_object_id_str(&id_str, (*selected)->id);
1100 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1101 entry ? entry->idx + 1 : 0, commits->ncommits,
1102 commits_needed == 0 ? "" : " loading...") == -1)
1103 return got_error_from_errno();
1105 if (path && strcmp(path, "/") != 0) {
1106 if (asprintf(&header, "commit %s %s%s",
1107 id_str ? id_str : "........................................",
1108 path, ncommits_str) == -1) {
1109 err = got_error_from_errno();
1113 } else if (asprintf(&header, "commit %s%s",
1114 id_str ? id_str : "........................................",
1115 ncommits_str) == -1) {
1116 err = got_error_from_errno();
1120 err = format_line(&wline, &width, header, view->ncols);
1124 werase(view->window);
1126 if (view_needs_focus_indication(view))
1127 wstandout(view->window);
1128 waddwstr(view->window, wline);
1129 while (width < view->ncols) {
1130 waddch(view->window, ' ');
1133 if (view_needs_focus_indication(view))
1134 wstandend(view->window);
1143 if (ncommits >= limit - 1)
1145 if (view->focussed && ncommits == selected_idx)
1146 wstandout(view->window);
1147 err = draw_commit(view, entry->commit, entry->id);
1148 if (view->focussed && ncommits == selected_idx)
1149 wstandend(view->window);
1154 entry = TAILQ_NEXT(entry, entry);
1166 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1167 struct commit_queue *commits)
1169 struct commit_queue_entry *entry;
1172 entry = TAILQ_FIRST(&commits->head);
1173 if (*first_displayed_entry == entry)
1176 entry = *first_displayed_entry;
1177 while (entry && nscrolled < maxscroll) {
1178 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1180 *first_displayed_entry = entry;
1186 static const struct got_error *
1187 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1188 struct commit_queue_entry **last_displayed_entry,
1189 struct commit_queue *commits, int *log_complete, int *commits_needed,
1190 pthread_cond_t *need_commits)
1192 const struct got_error *err = NULL;
1193 struct commit_queue_entry *pentry;
1196 if (*last_displayed_entry == NULL)
1200 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1201 if (pentry == NULL) {
1205 *commits_needed = maxscroll + 20;
1206 errcode = pthread_cond_signal(need_commits);
1208 return got_error_set_errno(errcode);
1211 *last_displayed_entry = pentry;
1213 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1216 *first_displayed_entry = pentry;
1217 } while (++nscrolled < maxscroll);
1222 static const struct got_error *
1223 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1224 struct got_object_id *commit_id, struct got_commit_object *commit,
1225 struct got_repository *repo)
1227 const struct got_error *err;
1228 struct got_object_qid *parent_id;
1229 struct tog_view *diff_view;
1231 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1232 if (diff_view == NULL)
1233 return got_error_from_errno();
1235 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1236 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1239 *new_view = diff_view;
1243 static const struct got_error *
1244 browse_commit(struct tog_view **new_view, int begin_x,
1245 struct commit_queue_entry *entry, struct got_repository *repo)
1247 const struct got_error *err = NULL;
1248 struct got_tree_object *tree;
1249 struct tog_view *tree_view;
1251 err = got_object_open_as_tree(&tree, repo,
1252 got_object_commit_get_tree_id(entry->commit));
1256 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1257 if (tree_view == NULL)
1258 return got_error_from_errno();
1260 err = open_tree_view(tree_view, tree, entry->id, repo);
1262 got_object_tree_close(tree);
1264 *new_view = tree_view;
1269 log_thread(void *arg)
1271 const struct got_error *err = NULL;
1273 struct tog_log_thread_args *a = arg;
1276 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1280 while (!done && !err) {
1281 err = queue_commits(a->graph, a->commits, 1, a->repo,
1284 if (err->code != GOT_ERR_ITER_COMPLETED)
1288 } else if (a->commits_needed > 0)
1289 a->commits_needed--;
1291 errcode = pthread_mutex_lock(&tog_mutex);
1293 return (void *)got_error_set_errno(errcode);
1296 a->log_complete = 1;
1297 else if (*a->quit) {
1299 a->log_complete = 1;
1300 } else if (*a->first_displayed_entry == NULL) {
1301 *a->first_displayed_entry =
1302 TAILQ_FIRST(&a->commits->head);
1303 *a->selected_entry = *a->first_displayed_entry;
1307 a->commits_needed = 0;
1308 else if (a->commits_needed == 0) {
1309 errcode = pthread_cond_wait(&a->need_commits,
1312 err = got_error_set_errno(errcode);
1315 errcode = pthread_mutex_unlock(&tog_mutex);
1316 if (errcode && err == NULL)
1317 err = got_error_set_errno(errcode);
1322 static const struct got_error *
1323 stop_log_thread(struct tog_log_view_state *s)
1325 const struct got_error *err = NULL;
1330 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1332 return got_error_set_errno(errcode);
1333 errcode = pthread_mutex_unlock(&tog_mutex);
1335 return got_error_set_errno(errcode);
1336 errcode = pthread_join(s->thread, (void **)&err);
1338 return got_error_set_errno(errcode);
1339 errcode = pthread_mutex_lock(&tog_mutex);
1341 return got_error_set_errno(errcode);
1345 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1346 if (errcode && err == NULL)
1347 err = got_error_set_errno(errcode);
1349 if (s->thread_args.repo) {
1350 got_repo_close(s->thread_args.repo);
1351 s->thread_args.repo = NULL;
1354 if (s->thread_args.graph) {
1355 got_commit_graph_close(s->thread_args.graph);
1356 s->thread_args.graph = NULL;
1362 static const struct got_error *
1363 close_log_view(struct tog_view *view)
1365 const struct got_error *err = NULL;
1366 struct tog_log_view_state *s = &view->state.log;
1368 err = stop_log_thread(s);
1369 free_commits(&s->commits);
1370 free(s->in_repo_path);
1371 s->in_repo_path = NULL;
1377 static const struct got_error *
1378 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1379 struct got_repository *repo, const char *path, int check_disk)
1381 const struct got_error *err = NULL;
1382 struct tog_log_view_state *s = &view->state.log;
1383 struct got_repository *thread_repo = NULL;
1384 struct got_commit_graph *thread_graph = NULL;
1387 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1391 /* The commit queue only contains commits being displayed. */
1392 TAILQ_INIT(&s->commits.head);
1393 s->commits.ncommits = 0;
1396 s->start_id = got_object_id_dup(start_id);
1397 if (s->start_id == NULL) {
1398 err = got_error_from_errno();
1402 view->show = show_log_view;
1403 view->input = input_log_view;
1404 view->close = close_log_view;
1406 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1409 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1414 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1416 err = got_error_set_errno(errcode);
1420 s->thread_args.commits_needed = view->nlines;
1421 s->thread_args.graph = thread_graph;
1422 s->thread_args.commits = &s->commits;
1423 s->thread_args.in_repo_path = s->in_repo_path;
1424 s->thread_args.start_id = s->start_id;
1425 s->thread_args.repo = thread_repo;
1426 s->thread_args.log_complete = 0;
1427 s->thread_args.quit = &s->quit;
1428 s->thread_args.view = view;
1429 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1430 s->thread_args.selected_entry = &s->selected_entry;
1433 close_log_view(view);
1437 static const struct got_error *
1438 show_log_view(struct tog_view *view)
1440 struct tog_log_view_state *s = &view->state.log;
1442 if (s->thread == NULL) {
1443 int errcode = pthread_create(&s->thread, NULL, log_thread,
1446 return got_error_set_errno(errcode);
1449 return draw_commits(view, &s->last_displayed_entry,
1450 &s->selected_entry, s->first_displayed_entry,
1451 &s->commits, s->selected, view->nlines,
1452 s->in_repo_path, s->thread_args.commits_needed);
1455 static const struct got_error *
1456 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1457 struct tog_view **focus_view, struct tog_view *view, int ch)
1459 const struct got_error *err = NULL;
1460 struct tog_log_view_state *s = &view->state.log;
1462 struct tog_view *diff_view = NULL, *tree_view = NULL;
1471 if (s->first_displayed_entry == NULL)
1473 if (s->selected > 0)
1475 if (s->selected > 0)
1477 scroll_up(&s->first_displayed_entry, 1,
1481 if (s->first_displayed_entry == NULL)
1483 if (TAILQ_FIRST(&s->commits.head) ==
1484 s->first_displayed_entry) {
1488 scroll_up(&s->first_displayed_entry,
1489 view->nlines, &s->commits);
1493 if (s->first_displayed_entry == NULL)
1495 if (s->selected < MIN(view->nlines - 2,
1496 s->commits.ncommits - 1)) {
1500 err = scroll_down(&s->first_displayed_entry, 1,
1501 &s->last_displayed_entry, &s->commits,
1502 &s->thread_args.log_complete,
1503 &s->thread_args.commits_needed,
1504 &s->thread_args.need_commits);
1507 struct commit_queue_entry *first;
1508 first = s->first_displayed_entry;
1511 err = scroll_down(&s->first_displayed_entry,
1512 view->nlines, &s->last_displayed_entry,
1513 &s->commits, &s->thread_args.log_complete,
1514 &s->thread_args.commits_needed,
1515 &s->thread_args.need_commits);
1516 if (first == s->first_displayed_entry &&
1517 s->selected < MIN(view->nlines - 2,
1518 s->commits.ncommits - 1)) {
1519 /* can't scroll further down */
1520 s->selected = MIN(view->nlines - 2,
1521 s->commits.ncommits - 1);
1527 if (s->selected > view->nlines - 2)
1528 s->selected = view->nlines - 2;
1529 if (s->selected > s->commits.ncommits - 1)
1530 s->selected = s->commits.ncommits - 1;
1534 if (s->selected_entry == NULL)
1536 if (view_is_parent_view(view))
1537 begin_x = view_split_begin_x(view->begin_x);
1538 err = open_diff_view_for_commit(&diff_view, begin_x,
1539 s->selected_entry->id, s->selected_entry->commit,
1543 if (view_is_parent_view(view)) {
1544 err = view_close_child(view);
1547 err = view_set_child(view, diff_view);
1549 view_close(diff_view);
1552 *focus_view = diff_view;
1553 view->child_focussed = 1;
1555 *new_view = diff_view;
1558 if (s->selected_entry == NULL)
1560 if (view_is_parent_view(view))
1561 begin_x = view_split_begin_x(view->begin_x);
1562 err = browse_commit(&tree_view, begin_x,
1563 s->selected_entry, s->repo);
1566 if (view_is_parent_view(view)) {
1567 err = view_close_child(view);
1570 err = view_set_child(view, tree_view);
1572 view_close(tree_view);
1575 *focus_view = tree_view;
1576 view->child_focussed = 1;
1578 *new_view = tree_view;
1581 if (strcmp(s->in_repo_path, "/") == 0)
1583 parent_path = dirname(s->in_repo_path);
1584 if (parent_path && strcmp(parent_path, ".") != 0) {
1585 struct tog_view *lv;
1586 err = stop_log_thread(s);
1589 lv = view_open(view->nlines, view->ncols,
1590 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1592 return got_error_from_errno();
1593 err = open_log_view(lv, s->start_id, s->repo,
1597 if (view_is_parent_view(view))
1600 view_set_child(view->parent, lv);
1613 static const struct got_error *
1614 cmd_log(int argc, char *argv[])
1616 const struct got_error *error;
1617 struct got_repository *repo = NULL;
1618 struct got_object_id *start_id = NULL;
1619 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1620 char *start_commit = NULL;
1622 struct tog_view *view;
1625 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1630 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1633 start_commit = optarg;
1636 repo_path = realpath(optarg, NULL);
1637 if (repo_path == NULL)
1638 err(1, "-r option");
1652 path = strdup(argv[0]);
1656 return got_error_from_errno();
1658 cwd = getcwd(NULL, 0);
1660 error = got_error_from_errno();
1663 if (repo_path == NULL) {
1664 repo_path = strdup(cwd);
1665 if (repo_path == NULL) {
1666 error = got_error_from_errno();
1671 error = got_repo_open(&repo, repo_path);
1675 if (start_commit == NULL)
1676 error = get_head_commit_id(&start_id, repo);
1678 error = got_object_resolve_id_str(&start_id, repo,
1683 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1685 error = got_error_from_errno();
1688 error = open_log_view(view, start_id, repo, path, 1);
1691 error = view_loop(view);
1698 got_repo_close(repo);
1706 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1712 parse_next_line(FILE *f, size_t *len)
1717 const char delim[3] = { '\0', '\0', '\0'};
1719 line = fparseln(f, &linelen, &lineno, delim, 0);
1725 static const struct got_error *
1726 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1727 int *last_displayed_line, int *eof, int max_lines,
1730 const struct got_error *err;
1731 int nlines = 0, nprinted = 0;
1738 werase(view->window);
1741 err = format_line(&wline, &width, header, view->ncols);
1746 if (view_needs_focus_indication(view))
1747 wstandout(view->window);
1748 waddwstr(view->window, wline);
1749 if (view_needs_focus_indication(view))
1750 wstandend(view->window);
1751 if (width < view->ncols)
1752 waddch(view->window, '\n');
1760 while (nprinted < max_lines) {
1761 line = parse_next_line(f, &len);
1766 if (++nlines < *first_displayed_line) {
1771 err = format_line(&wline, &width, line, view->ncols);
1776 waddwstr(view->window, wline);
1777 if (width < view->ncols)
1778 waddch(view->window, '\n');
1779 if (++nprinted == 1)
1780 *first_displayed_line = nlines;
1785 *last_displayed_line = nlines;
1793 get_datestr(time_t *time, char *datebuf)
1795 char *p, *s = ctime_r(time, datebuf);
1796 p = strchr(s, '\n');
1802 static const struct got_error *
1803 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1806 const struct got_error *err = NULL;
1808 struct got_commit_object *commit;
1809 char *id_str = NULL;
1810 time_t committer_time;
1811 const char *author, *committer;
1813 err = got_object_open_as_commit(&commit, repo, commit_id);
1817 err = got_object_id_str(&id_str, commit_id);
1819 err = got_error_from_errno();
1823 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1824 err = got_error_from_errno();
1827 if (fprintf(outfile, "from: %s\n",
1828 got_object_commit_get_author(commit)) < 0) {
1829 err = got_error_from_errno();
1832 committer_time = got_object_commit_get_committer_time(commit);
1833 if (fprintf(outfile, "date: %s UTC\n",
1834 get_datestr(&committer_time, datebuf)) < 0) {
1835 err = got_error_from_errno();
1838 author = got_object_commit_get_author(commit);
1839 committer = got_object_commit_get_committer(commit);
1840 if (strcmp(author, committer) != 0 &&
1841 fprintf(outfile, "via: %s\n", committer) < 0) {
1842 err = got_error_from_errno();
1845 if (fprintf(outfile, "%s\n",
1846 got_object_commit_get_logmsg(commit)) < 0) {
1847 err = got_error_from_errno();
1852 got_object_commit_close(commit);
1856 static const struct got_error *
1857 create_diff(struct tog_diff_view_state *s)
1859 const struct got_error *err = NULL;
1865 err = got_error_from_errno();
1873 err = got_object_get_type(&obj_type, s->repo, s->id1);
1875 err = got_object_get_type(&obj_type, s->repo, s->id2);
1880 case GOT_OBJ_TYPE_BLOB:
1881 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1882 s->diff_context, s->repo, f);
1884 case GOT_OBJ_TYPE_TREE:
1885 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1886 s->diff_context, s->repo, f);
1888 case GOT_OBJ_TYPE_COMMIT: {
1889 const struct got_object_id_queue *parent_ids;
1890 struct got_object_qid *pid;
1891 struct got_commit_object *commit2;
1893 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1896 /* Show commit info if we're diffing to a parent commit. */
1897 parent_ids = got_object_commit_get_parent_ids(commit2);
1898 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1899 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1900 write_commit_info(s->id2, s->repo, f);
1904 got_object_commit_close(commit2);
1906 err = got_diff_objects_as_commits(s->id1, s->id2,
1907 s->diff_context, s->repo, f);
1911 err = got_error(GOT_ERR_OBJ_TYPE);
1920 static const struct got_error *
1921 open_diff_view(struct tog_view *view, struct got_object_id *id1,
1922 struct got_object_id *id2, struct got_repository *repo)
1924 const struct got_error *err;
1926 if (id1 != NULL && id2 != NULL) {
1928 err = got_object_get_type(&type1, repo, id1);
1931 err = got_object_get_type(&type2, repo, id2);
1936 return got_error(GOT_ERR_OBJ_TYPE);
1940 view->state.diff.id1 = got_object_id_dup(id1);
1941 if (view->state.diff.id1 == NULL)
1942 return got_error_from_errno();
1944 view->state.diff.id1 = NULL;
1946 view->state.diff.id2 = got_object_id_dup(id2);
1947 if (view->state.diff.id2 == NULL) {
1948 free(view->state.diff.id1);
1949 view->state.diff.id1 = NULL;
1950 return got_error_from_errno();
1952 view->state.diff.f = NULL;
1953 view->state.diff.first_displayed_line = 1;
1954 view->state.diff.last_displayed_line = view->nlines;
1955 view->state.diff.diff_context = 3;
1956 view->state.diff.repo = repo;
1958 err = create_diff(&view->state.diff);
1960 free(view->state.diff.id1);
1961 view->state.diff.id1 = NULL;
1962 free(view->state.diff.id2);
1963 view->state.diff.id2 = NULL;
1967 view->show = show_diff_view;
1968 view->input = input_diff_view;
1969 view->close = close_diff_view;
1974 static const struct got_error *
1975 close_diff_view(struct tog_view *view)
1977 const struct got_error *err = NULL;
1979 free(view->state.diff.id1);
1980 view->state.diff.id1 = NULL;
1981 free(view->state.diff.id2);
1982 view->state.diff.id2 = NULL;
1983 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1984 err = got_error_from_errno();
1988 static const struct got_error *
1989 show_diff_view(struct tog_view *view)
1991 const struct got_error *err;
1992 struct tog_diff_view_state *s = &view->state.diff;
1993 char *id_str1 = NULL, *id_str2, *header;
1996 err = got_object_id_str(&id_str1, s->id1);
2000 err = got_object_id_str(&id_str2, s->id2);
2004 if (asprintf(&header, "diff %s %s",
2005 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2006 err = got_error_from_errno();
2014 return draw_file(view, s->f, &s->first_displayed_line,
2015 &s->last_displayed_line, &s->eof, view->nlines,
2019 static const struct got_error *
2020 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2021 struct tog_view **focus_view, struct tog_view *view, int ch)
2023 const struct got_error *err = NULL;
2024 struct tog_diff_view_state *s = &view->state.diff;
2030 if (s->first_displayed_line > 1)
2031 s->first_displayed_line--;
2035 while (i++ < view->nlines - 1 &&
2036 s->first_displayed_line > 1)
2037 s->first_displayed_line--;
2042 s->first_displayed_line++;
2047 while (!s->eof && i++ < view->nlines - 1) {
2049 line = parse_next_line(s->f, NULL);
2050 s->first_displayed_line++;
2056 if (s->diff_context > 0) {
2058 err = create_diff(s);
2062 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2064 err = create_diff(s);
2074 static const struct got_error *
2075 cmd_diff(int argc, char *argv[])
2077 const struct got_error *error = NULL;
2078 struct got_repository *repo = NULL;
2079 struct got_object_id *id1 = NULL, *id2 = NULL;
2080 char *repo_path = NULL;
2081 char *id_str1 = NULL, *id_str2 = NULL;
2083 struct tog_view *view;
2086 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2091 while ((ch = getopt(argc, argv, "")) != -1) {
2103 usage_diff(); /* TODO show local worktree changes */
2104 } else if (argc == 2) {
2105 repo_path = getcwd(NULL, 0);
2106 if (repo_path == NULL)
2107 return got_error_from_errno();
2110 } else if (argc == 3) {
2111 repo_path = realpath(argv[0], NULL);
2112 if (repo_path == NULL)
2113 return got_error_from_errno();
2119 error = got_repo_open(&repo, repo_path);
2124 error = got_object_resolve_id_str(&id1, repo, id_str1);
2128 error = got_object_resolve_id_str(&id2, repo, id_str2);
2132 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2134 error = got_error_from_errno();
2137 error = open_diff_view(view, id1, id2, repo);
2140 error = view_loop(view);
2142 got_repo_close(repo);
2150 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2155 struct tog_blame_line {
2157 struct got_object_id *id;
2160 static const struct got_error *
2161 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2162 const char *path, struct tog_blame_line *lines, int nlines,
2163 int blame_complete, int selected_line, int *first_displayed_line,
2164 int *last_displayed_line, int *eof, int max_lines)
2166 const struct got_error *err;
2167 int lineno = 0, nprinted = 0;
2172 struct tog_blame_line *blame_line;
2173 struct got_object_id *prev_id = NULL;
2176 err = got_object_id_str(&id_str, id);
2181 werase(view->window);
2183 if (asprintf(&line, "commit %s", id_str) == -1) {
2184 err = got_error_from_errno();
2189 err = format_line(&wline, &width, line, view->ncols);
2192 if (view_needs_focus_indication(view))
2193 wstandout(view->window);
2194 waddwstr(view->window, wline);
2195 if (view_needs_focus_indication(view))
2196 wstandend(view->window);
2199 if (width < view->ncols)
2200 waddch(view->window, '\n');
2202 if (asprintf(&line, "[%d/%d] %s%s",
2203 *first_displayed_line - 1 + selected_line, nlines,
2204 blame_complete ? "" : "annotating ", path) == -1) {
2206 return got_error_from_errno();
2209 err = format_line(&wline, &width, line, view->ncols);
2214 waddwstr(view->window, wline);
2217 if (width < view->ncols)
2218 waddch(view->window, '\n');
2221 while (nprinted < max_lines - 2) {
2222 line = parse_next_line(f, &len);
2227 if (++lineno < *first_displayed_line) {
2232 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2233 err = format_line(&wline, &width, line, wlimit);
2239 if (view->focussed && nprinted == selected_line - 1)
2240 wstandout(view->window);
2242 blame_line = &lines[lineno - 1];
2243 if (blame_line->annotated && prev_id &&
2244 got_object_id_cmp(prev_id, blame_line->id) == 0)
2245 waddstr(view->window, " ");
2246 else if (blame_line->annotated) {
2248 err = got_object_id_str(&id_str, blame_line->id);
2254 wprintw(view->window, "%.8s ", id_str);
2256 prev_id = blame_line->id;
2258 waddstr(view->window, "........ ");
2262 waddwstr(view->window, wline);
2263 while (width < wlimit) {
2264 waddch(view->window, ' ');
2267 if (view->focussed && nprinted == selected_line - 1)
2268 wstandend(view->window);
2269 if (++nprinted == 1)
2270 *first_displayed_line = lineno;
2275 *last_displayed_line = lineno;
2282 static const struct got_error *
2283 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2285 const struct got_error *err = NULL;
2286 struct tog_blame_cb_args *a = arg;
2287 struct tog_blame_line *line;
2290 if (nlines != a->nlines ||
2291 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2292 return got_error(GOT_ERR_RANGE);
2294 errcode = pthread_mutex_lock(&tog_mutex);
2296 return got_error_set_errno(errcode);
2298 if (*a->quit) { /* user has quit the blame view */
2299 err = got_error(GOT_ERR_ITER_COMPLETED);
2304 goto done; /* no change in this commit */
2306 line = &a->lines[lineno - 1];
2307 if (line->annotated)
2310 line->id = got_object_id_dup(id);
2311 if (line->id == NULL) {
2312 err = got_error_from_errno();
2315 line->annotated = 1;
2317 errcode = pthread_mutex_unlock(&tog_mutex);
2319 err = got_error_set_errno(errcode);
2324 blame_thread(void *arg)
2326 const struct got_error *err;
2327 struct tog_blame_thread_args *ta = arg;
2328 struct tog_blame_cb_args *a = ta->cb_args;
2331 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2332 blame_cb, ta->cb_args);
2334 errcode = pthread_mutex_lock(&tog_mutex);
2336 return (void *)got_error_set_errno(errcode);
2338 got_repo_close(ta->repo);
2342 errcode = pthread_mutex_unlock(&tog_mutex);
2343 if (errcode && err == NULL)
2344 err = got_error_set_errno(errcode);
2349 static struct got_object_id *
2350 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2353 struct tog_blame_line *line;
2355 line = &lines[first_displayed_line - 1 + selected_line - 1];
2356 if (!line->annotated)
2362 static const struct got_error *
2363 stop_blame(struct tog_blame *blame)
2365 const struct got_error *err = NULL;
2368 if (blame->thread) {
2370 errcode = pthread_mutex_unlock(&tog_mutex);
2372 return got_error_set_errno(errcode);
2373 errcode = pthread_join(blame->thread, (void **)&err);
2375 return got_error_set_errno(errcode);
2376 errcode = pthread_mutex_lock(&tog_mutex);
2378 return got_error_set_errno(errcode);
2379 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2381 blame->thread = NULL;
2383 if (blame->thread_args.repo) {
2384 got_repo_close(blame->thread_args.repo);
2385 blame->thread_args.repo = NULL;
2392 for (i = 0; i < blame->nlines; i++)
2393 free(blame->lines[i].id);
2395 blame->lines = NULL;
2397 free(blame->cb_args.commit_id);
2398 blame->cb_args.commit_id = NULL;
2403 static const struct got_error *
2404 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2405 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2406 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2407 struct got_repository *repo)
2409 const struct got_error *err = NULL;
2410 struct got_blob_object *blob = NULL;
2411 struct got_repository *thread_repo = NULL;
2412 struct got_object_id *obj_id = NULL;
2415 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2419 return got_error(GOT_ERR_NO_OBJ);
2421 err = got_object_get_type(&obj_type, repo, obj_id);
2425 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2426 err = got_error(GOT_ERR_OBJ_TYPE);
2430 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2433 blame->f = got_opentemp();
2434 if (blame->f == NULL) {
2435 err = got_error_from_errno();
2438 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2443 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2444 if (blame->lines == NULL) {
2445 err = got_error_from_errno();
2449 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2453 blame->cb_args.view = view;
2454 blame->cb_args.lines = blame->lines;
2455 blame->cb_args.nlines = blame->nlines;
2456 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2457 if (blame->cb_args.commit_id == NULL) {
2458 err = got_error_from_errno();
2461 blame->cb_args.quit = done;
2463 blame->thread_args.path = path;
2464 blame->thread_args.repo = thread_repo;
2465 blame->thread_args.cb_args = &blame->cb_args;
2466 blame->thread_args.complete = blame_complete;
2467 *blame_complete = 0;
2471 got_object_blob_close(blob);
2478 static const struct got_error *
2479 open_blame_view(struct tog_view *view, char *path,
2480 struct got_object_id *commit_id, struct got_repository *repo)
2482 const struct got_error *err = NULL;
2483 struct tog_blame_view_state *s = &view->state.blame;
2485 SIMPLEQ_INIT(&s->blamed_commits);
2487 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2491 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2492 s->first_displayed_line = 1;
2493 s->last_displayed_line = view->nlines;
2494 s->selected_line = 1;
2495 s->blame_complete = 0;
2497 if (s->path == NULL)
2498 return got_error_from_errno();
2500 s->commit_id = commit_id;
2501 memset(&s->blame, 0, sizeof(s->blame));
2503 view->show = show_blame_view;
2504 view->input = input_blame_view;
2505 view->close = close_blame_view;
2507 return run_blame(&s->blame, view, &s->blame_complete,
2508 &s->first_displayed_line, &s->last_displayed_line,
2509 &s->selected_line, &s->done, &s->eof, s->path,
2510 s->blamed_commit->id, s->repo);
2513 static const struct got_error *
2514 close_blame_view(struct tog_view *view)
2516 const struct got_error *err = NULL;
2517 struct tog_blame_view_state *s = &view->state.blame;
2519 if (s->blame.thread)
2520 err = stop_blame(&s->blame);
2522 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2523 struct got_object_qid *blamed_commit;
2524 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2525 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2526 got_object_qid_free(blamed_commit);
2534 static const struct got_error *
2535 show_blame_view(struct tog_view *view)
2537 const struct got_error *err = NULL;
2538 struct tog_blame_view_state *s = &view->state.blame;
2541 if (s->blame.thread == NULL) {
2542 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2543 &s->blame.thread_args);
2545 return got_error_set_errno(errcode);
2548 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2549 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2550 s->selected_line, &s->first_displayed_line,
2551 &s->last_displayed_line, &s->eof, view->nlines);
2557 static const struct got_error *
2558 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2559 struct tog_view **focus_view, struct tog_view *view, int ch)
2561 const struct got_error *err = NULL, *thread_err = NULL;
2562 struct tog_view *diff_view;
2563 struct tog_blame_view_state *s = &view->state.blame;
2572 if (s->selected_line > 1)
2574 else if (s->selected_line == 1 &&
2575 s->first_displayed_line > 1)
2576 s->first_displayed_line--;
2579 if (s->first_displayed_line == 1) {
2580 s->selected_line = 1;
2583 if (s->first_displayed_line > view->nlines - 2)
2584 s->first_displayed_line -=
2587 s->first_displayed_line = 1;
2591 if (s->selected_line < view->nlines - 2 &&
2592 s->first_displayed_line +
2593 s->selected_line <= s->blame.nlines)
2595 else if (s->last_displayed_line <
2597 s->first_displayed_line++;
2601 struct got_object_id *id = NULL;
2602 id = get_selected_commit_id(s->blame.lines,
2603 s->first_displayed_line, s->selected_line);
2607 struct got_commit_object *commit;
2608 struct got_object_qid *pid;
2609 struct got_object_id *blob_id = NULL;
2611 err = got_object_open_as_commit(&commit,
2615 pid = SIMPLEQ_FIRST(
2616 got_object_commit_get_parent_ids(commit));
2618 got_object_commit_close(commit);
2621 /* Check if path history ends here. */
2622 err = got_object_id_by_path(&blob_id, s->repo,
2625 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2627 got_object_commit_close(commit);
2630 err = got_object_get_type(&obj_type, s->repo,
2633 /* Can't blame non-blob type objects. */
2634 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2635 got_object_commit_close(commit);
2638 err = got_object_qid_alloc(&s->blamed_commit,
2640 got_object_commit_close(commit);
2642 if (got_object_id_cmp(id,
2643 s->blamed_commit->id) == 0)
2645 err = got_object_qid_alloc(&s->blamed_commit,
2651 thread_err = stop_blame(&s->blame);
2655 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2656 s->blamed_commit, entry);
2657 err = run_blame(&s->blame, view, &s->blame_complete,
2658 &s->first_displayed_line, &s->last_displayed_line,
2659 &s->selected_line, &s->done, &s->eof,
2660 s->path, s->blamed_commit->id, s->repo);
2666 struct got_object_qid *first;
2667 first = SIMPLEQ_FIRST(&s->blamed_commits);
2668 if (!got_object_id_cmp(first->id, s->commit_id))
2671 thread_err = stop_blame(&s->blame);
2675 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2676 got_object_qid_free(s->blamed_commit);
2678 SIMPLEQ_FIRST(&s->blamed_commits);
2679 err = run_blame(&s->blame, view, &s->blame_complete,
2680 &s->first_displayed_line, &s->last_displayed_line,
2681 &s->selected_line, &s->done, &s->eof, s->path,
2682 s->blamed_commit->id, s->repo);
2689 struct got_object_id *id = NULL;
2690 struct got_object_qid *pid;
2691 struct got_commit_object *commit = NULL;
2692 id = get_selected_commit_id(s->blame.lines,
2693 s->first_displayed_line, s->selected_line);
2694 if (id == NULL || got_object_id_cmp(id,
2695 s->blamed_commit->id) == 0)
2697 err = got_object_open_as_commit(&commit, s->repo, id);
2700 pid = SIMPLEQ_FIRST(
2701 got_object_commit_get_parent_ids(commit));
2702 if (view_is_parent_view(view))
2703 begin_x = view_split_begin_x(view->begin_x);
2704 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2705 if (diff_view == NULL) {
2706 got_object_commit_close(commit);
2707 err = got_error_from_errno();
2710 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2712 got_object_commit_close(commit);
2714 view_close(diff_view);
2717 if (view_is_parent_view(view)) {
2718 err = view_close_child(view);
2721 err = view_set_child(view, diff_view);
2723 view_close(diff_view);
2726 *focus_view = diff_view;
2727 view->child_focussed = 1;
2729 *new_view = diff_view;
2736 if (s->last_displayed_line >= s->blame.nlines &&
2737 s->selected_line < view->nlines - 2) {
2738 s->selected_line = MIN(s->blame.nlines,
2742 if (s->last_displayed_line + view->nlines - 2
2744 s->first_displayed_line +=
2747 s->first_displayed_line =
2752 if (s->selected_line > view->nlines - 2) {
2753 s->selected_line = MIN(s->blame.nlines,
2760 return thread_err ? thread_err : err;
2763 static const struct got_error *
2764 cmd_blame(int argc, char *argv[])
2766 const struct got_error *error;
2767 struct got_repository *repo = NULL;
2768 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 char *commit_id_str = NULL;
2772 struct tog_view *view;
2775 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2780 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2783 commit_id_str = optarg;
2786 repo_path = realpath(optarg, NULL);
2787 if (repo_path == NULL)
2788 err(1, "-r option");
2804 cwd = getcwd(NULL, 0);
2806 error = got_error_from_errno();
2809 if (repo_path == NULL) {
2810 repo_path = strdup(cwd);
2811 if (repo_path == NULL) {
2812 error = got_error_from_errno();
2818 error = got_repo_open(&repo, repo_path);
2822 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2826 if (commit_id_str == NULL) {
2827 struct got_reference *head_ref;
2828 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2831 error = got_ref_resolve(&commit_id, repo, head_ref);
2832 got_ref_close(head_ref);
2834 error = got_object_resolve_id_str(&commit_id, repo,
2840 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2842 error = got_error_from_errno();
2845 error = open_blame_view(view, in_repo_path, commit_id, repo);
2848 error = view_loop(view);
2854 got_repo_close(repo);
2858 static const struct got_error *
2859 draw_tree_entries(struct tog_view *view,
2860 struct got_tree_entry **first_displayed_entry,
2861 struct got_tree_entry **last_displayed_entry,
2862 struct got_tree_entry **selected_entry, int *ndisplayed,
2863 const char *label, int show_ids, const char *parent_path,
2864 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2866 const struct got_error *err = NULL;
2867 struct got_tree_entry *te;
2873 werase(view->window);
2878 err = format_line(&wline, &width, label, view->ncols);
2881 if (view_needs_focus_indication(view))
2882 wstandout(view->window);
2883 waddwstr(view->window, wline);
2884 if (view_needs_focus_indication(view))
2885 wstandend(view->window);
2888 if (width < view->ncols)
2889 waddch(view->window, '\n');
2892 err = format_line(&wline, &width, parent_path, view->ncols);
2895 waddwstr(view->window, wline);
2898 if (width < view->ncols)
2899 waddch(view->window, '\n');
2902 waddch(view->window, '\n');
2906 te = SIMPLEQ_FIRST(&entries->head);
2907 if (*first_displayed_entry == NULL) {
2908 if (selected == 0) {
2910 wstandout(view->window);
2911 *selected_entry = NULL;
2913 waddstr(view->window, " ..\n"); /* parent directory */
2914 if (selected == 0 && view->focussed)
2915 wstandend(view->window);
2922 while (te != *first_displayed_entry)
2923 te = SIMPLEQ_NEXT(te, entry);
2927 char *line = NULL, *id_str = NULL;
2930 err = got_object_id_str(&id_str, te->id);
2932 return got_error_from_errno();
2934 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2935 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2937 return got_error_from_errno();
2940 err = format_line(&wline, &width, line, view->ncols);
2945 if (n == selected) {
2947 wstandout(view->window);
2948 *selected_entry = te;
2950 waddwstr(view->window, wline);
2951 if (width < view->ncols)
2952 waddch(view->window, '\n');
2953 if (n == selected && view->focussed)
2954 wstandend(view->window);
2960 *last_displayed_entry = te;
2963 te = SIMPLEQ_NEXT(te, entry);
2970 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2971 const struct got_tree_entries *entries, int isroot)
2973 struct got_tree_entry *te, *prev;
2976 if (*first_displayed_entry == NULL)
2979 te = SIMPLEQ_FIRST(&entries->head);
2980 if (*first_displayed_entry == te) {
2982 *first_displayed_entry = NULL;
2986 /* XXX this is stupid... switch to TAILQ? */
2987 for (i = 0; i < maxscroll; i++) {
2988 while (te != *first_displayed_entry) {
2990 te = SIMPLEQ_NEXT(te, entry);
2992 *first_displayed_entry = prev;
2993 te = SIMPLEQ_FIRST(&entries->head);
2995 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2996 *first_displayed_entry = NULL;
3000 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3001 struct got_tree_entry *last_displayed_entry,
3002 const struct got_tree_entries *entries)
3004 struct got_tree_entry *next;
3007 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
3010 if (*first_displayed_entry)
3011 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3013 next = SIMPLEQ_FIRST(&entries->head);
3015 *first_displayed_entry = next;
3016 if (++n >= maxscroll)
3018 next = SIMPLEQ_NEXT(next, entry);
3022 static const struct got_error *
3023 tree_entry_path(char **path, struct tog_parent_trees *parents,
3024 struct got_tree_entry *te)
3026 const struct got_error *err = NULL;
3027 struct tog_parent_tree *pt;
3028 size_t len = 2; /* for leading slash and NUL */
3030 TAILQ_FOREACH(pt, parents, entry)
3031 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3033 len += strlen(te->name);
3035 *path = calloc(1, len);
3037 return got_error_from_errno();
3040 pt = TAILQ_LAST(parents, tog_parent_trees);
3042 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3043 err = got_error(GOT_ERR_NO_SPACE);
3046 if (strlcat(*path, "/", len) >= len) {
3047 err = got_error(GOT_ERR_NO_SPACE);
3050 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3053 if (strlcat(*path, te->name, len) >= len) {
3054 err = got_error(GOT_ERR_NO_SPACE);
3066 static const struct got_error *
3067 blame_tree_entry(struct tog_view **new_view, int begin_x,
3068 struct got_tree_entry *te, struct tog_parent_trees *parents,
3069 struct got_object_id *commit_id, struct got_repository *repo)
3071 const struct got_error *err = NULL;
3073 struct tog_view *blame_view;
3075 err = tree_entry_path(&path, parents, te);
3079 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3080 if (blame_view == NULL)
3081 return got_error_from_errno();
3083 err = open_blame_view(blame_view, path, commit_id, repo);
3085 view_close(blame_view);
3088 *new_view = blame_view;
3092 static const struct got_error *
3093 log_tree_entry(struct tog_view **new_view, int begin_x,
3094 struct got_tree_entry *te, struct tog_parent_trees *parents,
3095 struct got_object_id *commit_id, struct got_repository *repo)
3097 struct tog_view *log_view;
3098 const struct got_error *err = NULL;
3101 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3102 if (log_view == NULL)
3103 return got_error_from_errno();
3105 err = tree_entry_path(&path, parents, te);
3109 err = open_log_view(log_view, commit_id, repo, path, 0);
3111 view_close(log_view);
3113 *new_view = log_view;
3118 static const struct got_error *
3119 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3120 struct got_object_id *commit_id, struct got_repository *repo)
3122 const struct got_error *err = NULL;
3123 char *commit_id_str = NULL;
3124 struct tog_tree_view_state *s = &view->state.tree;
3126 TAILQ_INIT(&s->parents);
3128 err = got_object_id_str(&commit_id_str, commit_id);
3132 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3133 err = got_error_from_errno();
3137 s->root = s->tree = root;
3138 s->entries = got_object_tree_get_entries(root);
3139 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3140 s->commit_id = got_object_id_dup(commit_id);
3141 if (s->commit_id == NULL) {
3142 err = got_error_from_errno();
3147 view->show = show_tree_view;
3148 view->input = input_tree_view;
3149 view->close = close_tree_view;
3151 free(commit_id_str);
3153 free(s->tree_label);
3154 s->tree_label = NULL;
3159 static const struct got_error *
3160 close_tree_view(struct tog_view *view)
3162 struct tog_tree_view_state *s = &view->state.tree;
3164 free(s->tree_label);
3165 s->tree_label = NULL;
3167 s->commit_id = NULL;
3168 while (!TAILQ_EMPTY(&s->parents)) {
3169 struct tog_parent_tree *parent;
3170 parent = TAILQ_FIRST(&s->parents);
3171 TAILQ_REMOVE(&s->parents, parent, entry);
3175 if (s->tree != s->root)
3176 got_object_tree_close(s->tree);
3177 got_object_tree_close(s->root);
3182 static const struct got_error *
3183 show_tree_view(struct tog_view *view)
3185 const struct got_error *err = NULL;
3186 struct tog_tree_view_state *s = &view->state.tree;
3189 err = tree_entry_path(&parent_path, &s->parents, NULL);
3193 err = draw_tree_entries(view, &s->first_displayed_entry,
3194 &s->last_displayed_entry, &s->selected_entry,
3195 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3196 s->entries, s->selected, view->nlines, s->tree == s->root);
3203 static const struct got_error *
3204 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3205 struct tog_view **focus_view, struct tog_view *view, int ch)
3207 const struct got_error *err = NULL;
3208 struct tog_tree_view_state *s = &view->state.tree;
3209 struct tog_view *log_view;
3214 s->show_ids = !s->show_ids;
3217 if (!s->selected_entry)
3219 if (view_is_parent_view(view))
3220 begin_x = view_split_begin_x(view->begin_x);
3221 err = log_tree_entry(&log_view, begin_x,
3222 s->selected_entry, &s->parents,
3223 s->commit_id, s->repo);
3224 if (view_is_parent_view(view)) {
3225 err = view_close_child(view);
3228 err = view_set_child(view, log_view);
3230 view_close(log_view);
3233 *focus_view = log_view;
3234 view->child_focussed = 1;
3236 *new_view = log_view;
3240 if (s->selected > 0)
3242 if (s->selected > 0)
3244 tree_scroll_up(&s->first_displayed_entry, 1,
3245 s->entries, s->tree == s->root);
3249 if (SIMPLEQ_FIRST(&s->entries->head) ==
3250 s->first_displayed_entry) {
3251 if (s->tree != s->root)
3252 s->first_displayed_entry = NULL;
3255 tree_scroll_up(&s->first_displayed_entry,
3256 view->nlines, s->entries,
3257 s->tree == s->root);
3261 if (s->selected < s->ndisplayed - 1) {
3265 tree_scroll_down(&s->first_displayed_entry, 1,
3266 s->last_displayed_entry, s->entries);
3269 tree_scroll_down(&s->first_displayed_entry,
3270 view->nlines, s->last_displayed_entry,
3272 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3275 /* can't scroll any further; move cursor down */
3276 if (s->selected < s->ndisplayed - 1)
3277 s->selected = s->ndisplayed - 1;
3281 if (s->selected_entry == NULL) {
3282 struct tog_parent_tree *parent;
3284 /* user selected '..' */
3285 if (s->tree == s->root)
3287 parent = TAILQ_FIRST(&s->parents);
3288 TAILQ_REMOVE(&s->parents, parent,
3290 got_object_tree_close(s->tree);
3291 s->tree = parent->tree;
3293 got_object_tree_get_entries(s->tree);
3294 s->first_displayed_entry =
3295 parent->first_displayed_entry;
3297 parent->selected_entry;
3298 s->selected = parent->selected;
3300 } else if (S_ISDIR(s->selected_entry->mode)) {
3301 struct tog_parent_tree *parent;
3302 struct got_tree_object *child;
3303 err = got_object_open_as_tree(&child,
3304 s->repo, s->selected_entry->id);
3307 parent = calloc(1, sizeof(*parent));
3308 if (parent == NULL) {
3309 err = got_error_from_errno();
3312 parent->tree = s->tree;
3313 parent->first_displayed_entry =
3314 s->first_displayed_entry;
3315 parent->selected_entry = s->selected_entry;
3316 parent->selected = s->selected;
3317 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3320 got_object_tree_get_entries(s->tree);
3322 s->first_displayed_entry = NULL;
3323 } else if (S_ISREG(s->selected_entry->mode)) {
3324 struct tog_view *blame_view;
3325 int begin_x = view_is_parent_view(view) ?
3326 view_split_begin_x(view->begin_x) : 0;
3328 err = blame_tree_entry(&blame_view, begin_x,
3329 s->selected_entry, &s->parents, s->commit_id,
3333 if (view_is_parent_view(view)) {
3334 err = view_close_child(view);
3337 err = view_set_child(view, blame_view);
3339 view_close(blame_view);
3342 *focus_view = blame_view;
3343 view->child_focussed = 1;
3345 *new_view = blame_view;
3349 if (s->selected > view->nlines)
3350 s->selected = s->ndisplayed - 1;
3363 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3368 static const struct got_error *
3369 cmd_tree(int argc, char *argv[])
3371 const struct got_error *error;
3372 struct got_repository *repo = NULL;
3373 char *repo_path = NULL;
3374 struct got_object_id *commit_id = NULL;
3375 char *commit_id_arg = NULL;
3376 struct got_commit_object *commit = NULL;
3377 struct got_tree_object *tree = NULL;
3379 struct tog_view *view;
3382 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3387 while ((ch = getopt(argc, argv, "c:")) != -1) {
3390 commit_id_arg = optarg;
3402 repo_path = getcwd(NULL, 0);
3403 if (repo_path == NULL)
3404 return got_error_from_errno();
3405 } else if (argc == 1) {
3406 repo_path = realpath(argv[0], NULL);
3407 if (repo_path == NULL)
3408 return got_error_from_errno();
3412 error = got_repo_open(&repo, repo_path);
3417 if (commit_id_arg == NULL)
3418 error = get_head_commit_id(&commit_id, repo);
3420 error = got_object_resolve_id_str(&commit_id, repo,
3425 error = got_object_open_as_commit(&commit, repo, commit_id);
3429 error = got_object_open_as_tree(&tree, repo,
3430 got_object_commit_get_tree_id(commit));
3434 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3436 error = got_error_from_errno();
3439 error = open_tree_view(view, tree, commit_id, repo);
3442 error = view_loop(view);
3446 got_object_commit_close(commit);
3448 got_object_tree_close(tree);
3450 got_repo_close(repo);
3459 halfdelay(1); /* Do fast refresh while initial view is loading. */
3462 intrflush(stdscr, FALSE);
3463 keypad(stdscr, TRUE);
3465 signal(SIGWINCH, tog_sigwinch);
3473 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3474 "Available commands:\n", getprogname());
3475 for (i = 0; i < nitems(tog_commands); i++) {
3476 struct tog_cmd *cmd = &tog_commands[i];
3477 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3483 make_argv(const char *arg0, const char *arg1)
3486 int argc = (arg1 == NULL ? 1 : 2);
3488 argv = calloc(argc, sizeof(char *));
3491 argv[0] = strdup(arg0);
3492 if (argv[0] == NULL)
3495 argv[1] = strdup(arg1);
3496 if (argv[1] == NULL)
3504 main(int argc, char *argv[])
3506 const struct got_error *error = NULL;
3507 struct tog_cmd *cmd = NULL;
3509 char **cmd_argv = NULL;
3511 setlocale(LC_ALL, "");
3513 while ((ch = getopt(argc, argv, "h")) != -1) {
3532 /* Build an argument vector which runs a default command. */
3533 cmd = &tog_commands[0];
3534 cmd_argv = make_argv(cmd->name, NULL);
3539 /* Did the user specific a command? */
3540 for (i = 0; i < nitems(tog_commands); i++) {
3541 if (strncmp(tog_commands[i].name, argv[0],
3542 strlen(argv[0])) == 0) {
3543 cmd = &tog_commands[i];
3545 tog_commands[i].cmd_usage();
3550 /* Did the user specify a repository? */
3551 char *repo_path = realpath(argv[0], NULL);
3553 struct got_repository *repo;
3554 error = got_repo_open(&repo, repo_path);
3556 got_repo_close(repo);
3558 error = got_error_from_errno();
3561 fprintf(stderr, "%s: '%s' is not a "
3562 "known command\n", getprogname(),
3566 fprintf(stderr, "%s: '%s' is neither a known "
3567 "command nor a path to a repository\n",
3568 getprogname(), argv[0]);
3572 cmd = &tog_commands[0];
3573 cmd_argv = make_argv(cmd->name, repo_path);
3581 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3588 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);