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 *, struct got_object *, 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 *obj1 = NULL, *obj2 = NULL;
1229 struct got_object_qid *parent_id;
1230 struct tog_view *diff_view;
1232 err = got_object_open(&obj2, repo, commit_id);
1236 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1238 err = got_object_open(&obj1, repo, parent_id->id);
1243 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1244 if (diff_view == NULL) {
1245 err = got_error_from_errno();
1249 err = open_diff_view(diff_view, obj1, obj2, repo);
1251 *new_view = diff_view;
1254 got_object_close(obj1);
1256 got_object_close(obj2);
1260 static const struct got_error *
1261 browse_commit(struct tog_view **new_view, int begin_x,
1262 struct commit_queue_entry *entry, struct got_repository *repo)
1264 const struct got_error *err = NULL;
1265 struct got_tree_object *tree;
1266 struct tog_view *tree_view;
1268 err = got_object_open_as_tree(&tree, repo,
1269 got_object_commit_get_tree_id(entry->commit));
1273 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1274 if (tree_view == NULL)
1275 return got_error_from_errno();
1277 err = open_tree_view(tree_view, tree, entry->id, repo);
1279 got_object_tree_close(tree);
1281 *new_view = tree_view;
1286 log_thread(void *arg)
1288 const struct got_error *err = NULL;
1290 struct tog_log_thread_args *a = arg;
1293 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1297 while (!done && !err) {
1298 err = queue_commits(a->graph, a->commits, 1, a->repo,
1301 if (err->code != GOT_ERR_ITER_COMPLETED)
1305 } else if (a->commits_needed > 0)
1306 a->commits_needed--;
1308 errcode = pthread_mutex_lock(&tog_mutex);
1310 return (void *)got_error_set_errno(errcode);
1313 a->log_complete = 1;
1314 else if (*a->quit) {
1316 a->log_complete = 1;
1317 } else if (*a->first_displayed_entry == NULL) {
1318 *a->first_displayed_entry =
1319 TAILQ_FIRST(&a->commits->head);
1320 *a->selected_entry = *a->first_displayed_entry;
1324 a->commits_needed = 0;
1325 else if (a->commits_needed == 0) {
1326 errcode = pthread_cond_wait(&a->need_commits,
1329 err = got_error_set_errno(errcode);
1332 errcode = pthread_mutex_unlock(&tog_mutex);
1333 if (errcode && err == NULL)
1334 err = got_error_set_errno(errcode);
1339 static const struct got_error *
1340 stop_log_thread(struct tog_log_view_state *s)
1342 const struct got_error *err = NULL;
1347 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1349 return got_error_set_errno(errcode);
1350 errcode = pthread_mutex_unlock(&tog_mutex);
1352 return got_error_set_errno(errcode);
1353 errcode = pthread_join(s->thread, (void **)&err);
1355 return got_error_set_errno(errcode);
1356 errcode = pthread_mutex_lock(&tog_mutex);
1358 return got_error_set_errno(errcode);
1362 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1363 if (errcode && err == NULL)
1364 err = got_error_set_errno(errcode);
1366 if (s->thread_args.repo) {
1367 got_repo_close(s->thread_args.repo);
1368 s->thread_args.repo = NULL;
1371 if (s->thread_args.graph) {
1372 got_commit_graph_close(s->thread_args.graph);
1373 s->thread_args.graph = NULL;
1379 static const struct got_error *
1380 close_log_view(struct tog_view *view)
1382 const struct got_error *err = NULL;
1383 struct tog_log_view_state *s = &view->state.log;
1385 err = stop_log_thread(s);
1386 free_commits(&s->commits);
1387 free(s->in_repo_path);
1388 s->in_repo_path = NULL;
1394 static const struct got_error *
1395 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1396 struct got_repository *repo, const char *path, int check_disk)
1398 const struct got_error *err = NULL;
1399 struct tog_log_view_state *s = &view->state.log;
1400 struct got_repository *thread_repo = NULL;
1401 struct got_commit_graph *thread_graph = NULL;
1404 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1408 /* The commit queue only contains commits being displayed. */
1409 TAILQ_INIT(&s->commits.head);
1410 s->commits.ncommits = 0;
1413 s->start_id = got_object_id_dup(start_id);
1414 if (s->start_id == NULL) {
1415 err = got_error_from_errno();
1419 view->show = show_log_view;
1420 view->input = input_log_view;
1421 view->close = close_log_view;
1423 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1426 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1431 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1433 err = got_error_set_errno(errcode);
1437 s->thread_args.commits_needed = view->nlines;
1438 s->thread_args.graph = thread_graph;
1439 s->thread_args.commits = &s->commits;
1440 s->thread_args.in_repo_path = s->in_repo_path;
1441 s->thread_args.start_id = s->start_id;
1442 s->thread_args.repo = thread_repo;
1443 s->thread_args.log_complete = 0;
1444 s->thread_args.quit = &s->quit;
1445 s->thread_args.view = view;
1446 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1447 s->thread_args.selected_entry = &s->selected_entry;
1450 close_log_view(view);
1454 static const struct got_error *
1455 show_log_view(struct tog_view *view)
1457 struct tog_log_view_state *s = &view->state.log;
1459 if (s->thread == NULL) {
1460 int errcode = pthread_create(&s->thread, NULL, log_thread,
1463 return got_error_set_errno(errcode);
1466 return draw_commits(view, &s->last_displayed_entry,
1467 &s->selected_entry, s->first_displayed_entry,
1468 &s->commits, s->selected, view->nlines,
1469 s->in_repo_path, s->thread_args.commits_needed);
1472 static const struct got_error *
1473 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1474 struct tog_view **focus_view, struct tog_view *view, int ch)
1476 const struct got_error *err = NULL;
1477 struct tog_log_view_state *s = &view->state.log;
1479 struct tog_view *diff_view = NULL, *tree_view = NULL;
1488 if (s->first_displayed_entry == NULL)
1490 if (s->selected > 0)
1492 if (s->selected > 0)
1494 scroll_up(&s->first_displayed_entry, 1,
1498 if (s->first_displayed_entry == NULL)
1500 if (TAILQ_FIRST(&s->commits.head) ==
1501 s->first_displayed_entry) {
1505 scroll_up(&s->first_displayed_entry,
1506 view->nlines, &s->commits);
1510 if (s->first_displayed_entry == NULL)
1512 if (s->selected < MIN(view->nlines - 2,
1513 s->commits.ncommits - 1)) {
1517 err = scroll_down(&s->first_displayed_entry, 1,
1518 &s->last_displayed_entry, &s->commits,
1519 &s->thread_args.log_complete,
1520 &s->thread_args.commits_needed,
1521 &s->thread_args.need_commits);
1524 struct commit_queue_entry *first;
1525 first = s->first_displayed_entry;
1528 err = scroll_down(&s->first_displayed_entry,
1529 view->nlines, &s->last_displayed_entry,
1530 &s->commits, &s->thread_args.log_complete,
1531 &s->thread_args.commits_needed,
1532 &s->thread_args.need_commits);
1533 if (first == s->first_displayed_entry &&
1534 s->selected < MIN(view->nlines - 2,
1535 s->commits.ncommits - 1)) {
1536 /* can't scroll further down */
1537 s->selected = MIN(view->nlines - 2,
1538 s->commits.ncommits - 1);
1544 if (s->selected > view->nlines - 2)
1545 s->selected = view->nlines - 2;
1546 if (s->selected > s->commits.ncommits - 1)
1547 s->selected = s->commits.ncommits - 1;
1551 if (s->selected_entry == NULL)
1553 if (view_is_parent_view(view))
1554 begin_x = view_split_begin_x(view->begin_x);
1555 err = open_diff_view_for_commit(&diff_view, begin_x,
1556 s->selected_entry->id, s->selected_entry->commit,
1560 if (view_is_parent_view(view)) {
1561 err = view_close_child(view);
1564 err = view_set_child(view, diff_view);
1566 view_close(diff_view);
1569 *focus_view = diff_view;
1570 view->child_focussed = 1;
1572 *new_view = diff_view;
1575 if (s->selected_entry == NULL)
1577 if (view_is_parent_view(view))
1578 begin_x = view_split_begin_x(view->begin_x);
1579 err = browse_commit(&tree_view, begin_x,
1580 s->selected_entry, s->repo);
1583 if (view_is_parent_view(view)) {
1584 err = view_close_child(view);
1587 err = view_set_child(view, tree_view);
1589 view_close(tree_view);
1592 *focus_view = tree_view;
1593 view->child_focussed = 1;
1595 *new_view = tree_view;
1598 if (strcmp(s->in_repo_path, "/") == 0)
1600 parent_path = dirname(s->in_repo_path);
1601 if (parent_path && strcmp(parent_path, ".") != 0) {
1602 struct tog_view *lv;
1603 err = stop_log_thread(s);
1606 lv = view_open(view->nlines, view->ncols,
1607 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1609 return got_error_from_errno();
1610 err = open_log_view(lv, s->start_id, s->repo,
1614 if (view_is_parent_view(view))
1617 view_set_child(view->parent, lv);
1630 static const struct got_error *
1631 cmd_log(int argc, char *argv[])
1633 const struct got_error *error;
1634 struct got_repository *repo = NULL;
1635 struct got_object_id *start_id = NULL;
1636 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1637 char *start_commit = NULL;
1639 struct tog_view *view;
1642 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1647 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1650 start_commit = optarg;
1653 repo_path = realpath(optarg, NULL);
1654 if (repo_path == NULL)
1655 err(1, "-r option");
1669 path = strdup(argv[0]);
1673 return got_error_from_errno();
1675 cwd = getcwd(NULL, 0);
1677 error = got_error_from_errno();
1680 if (repo_path == NULL) {
1681 repo_path = strdup(cwd);
1682 if (repo_path == NULL) {
1683 error = got_error_from_errno();
1688 error = got_repo_open(&repo, repo_path);
1692 if (start_commit == NULL) {
1693 error = get_head_commit_id(&start_id, repo);
1697 struct got_object *obj;
1698 error = got_object_open_by_id_str(&obj, repo, start_commit);
1699 if (error == NULL) {
1700 start_id = got_object_id_dup(got_object_get_id(obj));
1701 if (start_id == NULL)
1702 error = got_error_from_errno();
1709 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1711 error = got_error_from_errno();
1714 error = open_log_view(view, start_id, repo, path, 1);
1717 error = view_loop(view);
1724 got_repo_close(repo);
1732 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1738 parse_next_line(FILE *f, size_t *len)
1743 const char delim[3] = { '\0', '\0', '\0'};
1745 line = fparseln(f, &linelen, &lineno, delim, 0);
1751 static const struct got_error *
1752 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1753 int *last_displayed_line, int *eof, int max_lines,
1756 const struct got_error *err;
1757 int nlines = 0, nprinted = 0;
1764 werase(view->window);
1767 err = format_line(&wline, &width, header, view->ncols);
1772 if (view_needs_focus_indication(view))
1773 wstandout(view->window);
1774 waddwstr(view->window, wline);
1775 if (view_needs_focus_indication(view))
1776 wstandend(view->window);
1777 if (width < view->ncols)
1778 waddch(view->window, '\n');
1786 while (nprinted < max_lines) {
1787 line = parse_next_line(f, &len);
1792 if (++nlines < *first_displayed_line) {
1797 err = format_line(&wline, &width, line, view->ncols);
1802 waddwstr(view->window, wline);
1803 if (width < view->ncols)
1804 waddch(view->window, '\n');
1805 if (++nprinted == 1)
1806 *first_displayed_line = nlines;
1811 *last_displayed_line = nlines;
1819 get_datestr(time_t *time, char *datebuf)
1821 char *p, *s = ctime_r(time, datebuf);
1822 p = strchr(s, '\n');
1828 static const struct got_error *
1829 write_commit_info(struct got_object *obj, struct got_repository *repo,
1832 const struct got_error *err = NULL;
1835 struct got_commit_object *commit = NULL;
1836 time_t committer_time;
1837 const char *author, *committer;
1839 err = got_object_id_str(&id_str, got_object_get_id(obj));
1843 err = got_object_commit_open(&commit, repo, obj);
1845 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
1846 err = got_error_from_errno();
1849 if (fprintf(outfile, "from: %s\n",
1850 got_object_commit_get_author(commit)) < 0) {
1851 err = got_error_from_errno();
1854 committer_time = got_object_commit_get_committer_time(commit);
1855 if (fprintf(outfile, "date: %s UTC\n",
1856 get_datestr(&committer_time, datebuf)) < 0) {
1857 err = got_error_from_errno();
1860 author = got_object_commit_get_author(commit);
1861 committer = got_object_commit_get_committer(commit);
1862 if (strcmp(author, committer) != 0 &&
1863 fprintf(outfile, "via: %s\n", committer) < 0) {
1864 err = got_error_from_errno();
1867 if (fprintf(outfile, "%s\n",
1868 got_object_commit_get_logmsg(commit)) < 0) {
1869 err = got_error_from_errno();
1875 got_object_commit_close(commit);
1879 static const struct got_error *
1880 create_diff(struct tog_diff_view_state *s)
1882 const struct got_error *err = NULL;
1883 struct got_object *obj1 = NULL, *obj2 = NULL;
1887 err = got_object_open(&obj1, s->repo, s->id1);
1892 err = got_object_open(&obj2, s->repo, s->id2);
1898 err = got_error_from_errno();
1905 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1906 case GOT_OBJ_TYPE_BLOB:
1907 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1908 s->diff_context, s->repo, f);
1910 case GOT_OBJ_TYPE_TREE:
1911 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1912 s->diff_context, s->repo, f);
1914 case GOT_OBJ_TYPE_COMMIT: {
1915 const struct got_object_id_queue *parent_ids;
1916 struct got_object_qid *pid;
1917 struct got_commit_object *commit2;
1919 err = got_object_commit_open(&commit2, s->repo, obj2);
1922 /* Show commit info if we're diffing to a parent commit. */
1923 parent_ids = got_object_commit_get_parent_ids(commit2);
1924 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1925 struct got_object_id *id1 = got_object_get_id(obj1);
1926 if (got_object_id_cmp(id1, pid->id) == 0) {
1927 write_commit_info(obj2, s->repo, f);
1931 got_object_commit_close(commit2);
1933 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1938 err = got_error(GOT_ERR_OBJ_TYPE);
1943 got_object_close(obj1);
1944 got_object_close(obj2);
1950 static const struct got_error *
1951 open_diff_view(struct tog_view *view, struct got_object *obj1,
1952 struct got_object *obj2, struct got_repository *repo)
1954 const struct got_error *err;
1956 if (obj1 != NULL && obj2 != NULL &&
1957 got_object_get_type(obj1) != got_object_get_type(obj2))
1958 return got_error(GOT_ERR_OBJ_TYPE);
1961 struct got_object_id *id1;
1962 id1 = got_object_id_dup(got_object_get_id(obj1));
1964 return got_error_from_errno();
1965 view->state.diff.id1 = id1;
1967 view->state.diff.id1 = NULL;
1969 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1970 if (view->state.diff.id2 == NULL) {
1971 free(view->state.diff.id1);
1972 view->state.diff.id1 = NULL;
1973 return got_error_from_errno();
1975 view->state.diff.f = NULL;
1976 view->state.diff.first_displayed_line = 1;
1977 view->state.diff.last_displayed_line = view->nlines;
1978 view->state.diff.diff_context = 3;
1979 view->state.diff.repo = repo;
1981 err = create_diff(&view->state.diff);
1983 free(view->state.diff.id1);
1984 view->state.diff.id1 = NULL;
1985 free(view->state.diff.id2);
1986 view->state.diff.id2 = NULL;
1990 view->show = show_diff_view;
1991 view->input = input_diff_view;
1992 view->close = close_diff_view;
1997 static const struct got_error *
1998 close_diff_view(struct tog_view *view)
2000 const struct got_error *err = NULL;
2002 free(view->state.diff.id1);
2003 view->state.diff.id1 = NULL;
2004 free(view->state.diff.id2);
2005 view->state.diff.id2 = NULL;
2006 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2007 err = got_error_from_errno();
2011 static const struct got_error *
2012 show_diff_view(struct tog_view *view)
2014 const struct got_error *err;
2015 struct tog_diff_view_state *s = &view->state.diff;
2016 char *id_str1 = NULL, *id_str2, *header;
2019 err = got_object_id_str(&id_str1, s->id1);
2023 err = got_object_id_str(&id_str2, s->id2);
2027 if (asprintf(&header, "diff %s %s",
2028 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2029 err = got_error_from_errno();
2037 return draw_file(view, s->f, &s->first_displayed_line,
2038 &s->last_displayed_line, &s->eof, view->nlines,
2042 static const struct got_error *
2043 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2044 struct tog_view **focus_view, struct tog_view *view, int ch)
2046 const struct got_error *err = NULL;
2047 struct tog_diff_view_state *s = &view->state.diff;
2053 if (s->first_displayed_line > 1)
2054 s->first_displayed_line--;
2058 while (i++ < view->nlines - 1 &&
2059 s->first_displayed_line > 1)
2060 s->first_displayed_line--;
2065 s->first_displayed_line++;
2070 while (!s->eof && i++ < view->nlines - 1) {
2072 line = parse_next_line(s->f, NULL);
2073 s->first_displayed_line++;
2079 if (s->diff_context > 0) {
2081 err = create_diff(s);
2085 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2087 err = create_diff(s);
2097 static const struct got_error *
2098 cmd_diff(int argc, char *argv[])
2100 const struct got_error *error = NULL;
2101 struct got_repository *repo = NULL;
2102 struct got_object *obj1 = NULL, *obj2 = NULL;
2103 char *repo_path = NULL;
2104 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
2106 struct tog_view *view;
2109 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2114 while ((ch = getopt(argc, argv, "")) != -1) {
2126 usage_diff(); /* TODO show local worktree changes */
2127 } else if (argc == 2) {
2128 repo_path = getcwd(NULL, 0);
2129 if (repo_path == NULL)
2130 return got_error_from_errno();
2131 obj_id_str1 = argv[0];
2132 obj_id_str2 = argv[1];
2133 } else if (argc == 3) {
2134 repo_path = realpath(argv[0], NULL);
2135 if (repo_path == NULL)
2136 return got_error_from_errno();
2137 obj_id_str1 = argv[1];
2138 obj_id_str2 = argv[2];
2142 error = got_repo_open(&repo, repo_path);
2147 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2151 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2155 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2157 error = got_error_from_errno();
2160 error = open_diff_view(view, obj1, obj2, repo);
2163 error = view_loop(view);
2165 got_repo_close(repo);
2167 got_object_close(obj1);
2169 got_object_close(obj2);
2177 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2182 struct tog_blame_line {
2184 struct got_object_id *id;
2187 static const struct got_error *
2188 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2189 const char *path, struct tog_blame_line *lines, int nlines,
2190 int blame_complete, int selected_line, int *first_displayed_line,
2191 int *last_displayed_line, int *eof, int max_lines)
2193 const struct got_error *err;
2194 int lineno = 0, nprinted = 0;
2199 struct tog_blame_line *blame_line;
2200 struct got_object_id *prev_id = NULL;
2203 err = got_object_id_str(&id_str, id);
2208 werase(view->window);
2210 if (asprintf(&line, "commit: %s", id_str) == -1) {
2211 err = got_error_from_errno();
2216 err = format_line(&wline, &width, line, view->ncols);
2219 if (view_needs_focus_indication(view))
2220 wstandout(view->window);
2221 waddwstr(view->window, wline);
2222 if (view_needs_focus_indication(view))
2223 wstandend(view->window);
2226 if (width < view->ncols)
2227 waddch(view->window, '\n');
2229 if (asprintf(&line, "[%d/%d] %s%s",
2230 *first_displayed_line - 1 + selected_line, nlines,
2231 blame_complete ? "" : "annotating ", path) == -1) {
2233 return got_error_from_errno();
2236 err = format_line(&wline, &width, line, view->ncols);
2241 waddwstr(view->window, wline);
2244 if (width < view->ncols)
2245 waddch(view->window, '\n');
2248 while (nprinted < max_lines - 2) {
2249 line = parse_next_line(f, &len);
2254 if (++lineno < *first_displayed_line) {
2259 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2260 err = format_line(&wline, &width, line, wlimit);
2266 if (view->focussed && nprinted == selected_line - 1)
2267 wstandout(view->window);
2269 blame_line = &lines[lineno - 1];
2270 if (blame_line->annotated && prev_id &&
2271 got_object_id_cmp(prev_id, blame_line->id) == 0)
2272 waddstr(view->window, " ");
2273 else if (blame_line->annotated) {
2275 err = got_object_id_str(&id_str, blame_line->id);
2281 wprintw(view->window, "%.8s ", id_str);
2283 prev_id = blame_line->id;
2285 waddstr(view->window, "........ ");
2289 waddwstr(view->window, wline);
2290 while (width < wlimit) {
2291 waddch(view->window, ' ');
2294 if (view->focussed && nprinted == selected_line - 1)
2295 wstandend(view->window);
2296 if (++nprinted == 1)
2297 *first_displayed_line = lineno;
2302 *last_displayed_line = lineno;
2309 static const struct got_error *
2310 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2312 const struct got_error *err = NULL;
2313 struct tog_blame_cb_args *a = arg;
2314 struct tog_blame_line *line;
2317 if (nlines != a->nlines ||
2318 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2319 return got_error(GOT_ERR_RANGE);
2321 errcode = pthread_mutex_lock(&tog_mutex);
2323 return got_error_set_errno(errcode);
2325 if (*a->quit) { /* user has quit the blame view */
2326 err = got_error(GOT_ERR_ITER_COMPLETED);
2331 goto done; /* no change in this commit */
2333 line = &a->lines[lineno - 1];
2334 if (line->annotated)
2337 line->id = got_object_id_dup(id);
2338 if (line->id == NULL) {
2339 err = got_error_from_errno();
2342 line->annotated = 1;
2344 errcode = pthread_mutex_unlock(&tog_mutex);
2346 err = got_error_set_errno(errcode);
2351 blame_thread(void *arg)
2353 const struct got_error *err;
2354 struct tog_blame_thread_args *ta = arg;
2355 struct tog_blame_cb_args *a = ta->cb_args;
2358 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2359 blame_cb, ta->cb_args);
2361 errcode = pthread_mutex_lock(&tog_mutex);
2363 return (void *)got_error_set_errno(errcode);
2365 got_repo_close(ta->repo);
2369 errcode = pthread_mutex_unlock(&tog_mutex);
2370 if (errcode && err == NULL)
2371 err = got_error_set_errno(errcode);
2376 static struct got_object_id *
2377 get_selected_commit_id(struct tog_blame_line *lines,
2378 int first_displayed_line, int selected_line)
2380 struct tog_blame_line *line;
2382 line = &lines[first_displayed_line - 1 + selected_line - 1];
2383 if (!line->annotated)
2389 static const struct got_error *
2390 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2391 struct tog_blame_line *lines, int first_displayed_line,
2392 int selected_line, struct got_repository *repo)
2394 const struct got_error *err = NULL;
2395 struct got_commit_object *commit = NULL;
2396 struct got_object_id *selected_id;
2397 struct got_object_qid *pid;
2402 selected_id = get_selected_commit_id(lines,
2403 first_displayed_line, selected_line);
2404 if (selected_id == NULL)
2407 err = got_object_open(obj, repo, selected_id);
2411 err = got_object_commit_open(&commit, repo, *obj);
2415 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2417 err = got_object_open(pobj, repo, pid->id);
2423 got_object_commit_close(commit);
2427 static const struct got_error *
2428 stop_blame(struct tog_blame *blame)
2430 const struct got_error *err = NULL;
2433 if (blame->thread) {
2435 errcode = pthread_mutex_unlock(&tog_mutex);
2437 return got_error_set_errno(errcode);
2438 errcode = pthread_join(blame->thread, (void **)&err);
2440 return got_error_set_errno(errcode);
2441 errcode = pthread_mutex_lock(&tog_mutex);
2443 return got_error_set_errno(errcode);
2444 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2446 blame->thread = NULL;
2448 if (blame->thread_args.repo) {
2449 got_repo_close(blame->thread_args.repo);
2450 blame->thread_args.repo = NULL;
2456 for (i = 0; i < blame->nlines; i++)
2457 free(blame->lines[i].id);
2459 blame->lines = NULL;
2460 free(blame->cb_args.commit_id);
2461 blame->cb_args.commit_id = NULL;
2466 static const struct got_error *
2467 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2468 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2469 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2470 struct got_repository *repo)
2472 const struct got_error *err = NULL;
2473 struct got_blob_object *blob = NULL;
2474 struct got_repository *thread_repo = NULL;
2475 struct got_object_id *obj_id = NULL;
2476 struct got_object *obj = NULL;
2478 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2482 err = got_object_open(&obj, repo, obj_id);
2486 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2487 err = got_error(GOT_ERR_OBJ_TYPE);
2491 err = got_object_blob_open(&blob, repo, obj, 8192);
2494 blame->f = got_opentemp();
2495 if (blame->f == NULL) {
2496 err = got_error_from_errno();
2499 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2504 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2505 if (blame->lines == NULL) {
2506 err = got_error_from_errno();
2510 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2514 blame->cb_args.view = view;
2515 blame->cb_args.lines = blame->lines;
2516 blame->cb_args.nlines = blame->nlines;
2517 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2518 if (blame->cb_args.commit_id == NULL) {
2519 err = got_error_from_errno();
2522 blame->cb_args.quit = done;
2524 blame->thread_args.path = path;
2525 blame->thread_args.repo = thread_repo;
2526 blame->thread_args.cb_args = &blame->cb_args;
2527 blame->thread_args.complete = blame_complete;
2528 *blame_complete = 0;
2532 got_object_blob_close(blob);
2535 got_object_close(obj);
2541 static const struct got_error *
2542 open_blame_view(struct tog_view *view, char *path,
2543 struct got_object_id *commit_id, struct got_repository *repo)
2545 const struct got_error *err = NULL;
2546 struct tog_blame_view_state *s = &view->state.blame;
2548 SIMPLEQ_INIT(&s->blamed_commits);
2550 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2554 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2555 s->first_displayed_line = 1;
2556 s->last_displayed_line = view->nlines;
2557 s->selected_line = 1;
2558 s->blame_complete = 0;
2560 if (s->path == NULL)
2561 return got_error_from_errno();
2563 s->commit_id = commit_id;
2564 memset(&s->blame, 0, sizeof(s->blame));
2566 view->show = show_blame_view;
2567 view->input = input_blame_view;
2568 view->close = close_blame_view;
2570 return run_blame(&s->blame, view, &s->blame_complete,
2571 &s->first_displayed_line, &s->last_displayed_line,
2572 &s->selected_line, &s->done, &s->eof, s->path,
2573 s->blamed_commit->id, s->repo);
2576 static const struct got_error *
2577 close_blame_view(struct tog_view *view)
2579 const struct got_error *err = NULL;
2580 struct tog_blame_view_state *s = &view->state.blame;
2582 if (s->blame.thread)
2583 err = stop_blame(&s->blame);
2585 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2586 struct got_object_qid *blamed_commit;
2587 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2588 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2589 got_object_qid_free(blamed_commit);
2597 static const struct got_error *
2598 show_blame_view(struct tog_view *view)
2600 const struct got_error *err = NULL;
2601 struct tog_blame_view_state *s = &view->state.blame;
2604 if (s->blame.thread == NULL) {
2605 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2606 &s->blame.thread_args);
2608 return got_error_set_errno(errcode);
2611 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2612 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2613 s->selected_line, &s->first_displayed_line,
2614 &s->last_displayed_line, &s->eof, view->nlines);
2620 static const struct got_error *
2621 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2622 struct tog_view **focus_view, struct tog_view *view, int ch)
2624 const struct got_error *err = NULL, *thread_err = NULL;
2625 struct got_object *obj = NULL, *pobj = NULL;
2626 struct tog_view *diff_view;
2627 struct tog_blame_view_state *s = &view->state.blame;
2636 if (s->selected_line > 1)
2638 else if (s->selected_line == 1 &&
2639 s->first_displayed_line > 1)
2640 s->first_displayed_line--;
2643 if (s->first_displayed_line == 1) {
2644 s->selected_line = 1;
2647 if (s->first_displayed_line > view->nlines - 2)
2648 s->first_displayed_line -=
2651 s->first_displayed_line = 1;
2655 if (s->selected_line < view->nlines - 2 &&
2656 s->first_displayed_line +
2657 s->selected_line <= s->blame.nlines)
2659 else if (s->last_displayed_line <
2661 s->first_displayed_line++;
2665 struct got_object_id *id;
2666 id = get_selected_commit_id(s->blame.lines,
2667 s->first_displayed_line, s->selected_line);
2668 if (id == NULL || got_object_id_cmp(id,
2669 s->blamed_commit->id) == 0)
2671 err = open_selected_commit(&pobj, &obj,
2672 s->blame.lines, s->first_displayed_line,
2673 s->selected_line, s->repo);
2676 if (pobj == NULL && obj == NULL)
2678 if (ch == 'p' && pobj == NULL)
2681 thread_err = stop_blame(&s->blame);
2685 id = got_object_get_id(ch == 'b' ? obj : pobj);
2686 got_object_close(obj);
2689 got_object_close(pobj);
2692 err = got_object_qid_alloc(&s->blamed_commit, id);
2695 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2696 s->blamed_commit, entry);
2697 err = run_blame(&s->blame, view, &s->blame_complete,
2698 &s->first_displayed_line, &s->last_displayed_line,
2699 &s->selected_line, &s->done, &s->eof,
2700 s->path, s->blamed_commit->id, s->repo);
2706 struct got_object_qid *first;
2707 first = SIMPLEQ_FIRST(&s->blamed_commits);
2708 if (!got_object_id_cmp(first->id, s->commit_id))
2711 thread_err = stop_blame(&s->blame);
2715 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2716 got_object_qid_free(s->blamed_commit);
2718 SIMPLEQ_FIRST(&s->blamed_commits);
2719 err = run_blame(&s->blame, view, &s->blame_complete,
2720 &s->first_displayed_line, &s->last_displayed_line,
2721 &s->selected_line, &s->done, &s->eof, s->path,
2722 s->blamed_commit->id, s->repo);
2729 err = open_selected_commit(&pobj, &obj,
2730 s->blame.lines, s->first_displayed_line,
2731 s->selected_line, s->repo);
2734 if (pobj == NULL && obj == NULL)
2737 if (view_is_parent_view(view))
2738 begin_x = view_split_begin_x(view->begin_x);
2739 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2740 if (diff_view == NULL) {
2741 err = got_error_from_errno();
2744 err = open_diff_view(diff_view, pobj, obj, s->repo);
2746 view_close(diff_view);
2749 if (view_is_parent_view(view)) {
2750 err = view_close_child(view);
2753 err = view_set_child(view, diff_view);
2755 view_close(diff_view);
2758 *focus_view = diff_view;
2759 view->child_focussed = 1;
2761 *new_view = diff_view;
2763 got_object_close(pobj);
2766 got_object_close(obj);
2773 if (s->last_displayed_line >= s->blame.nlines &&
2774 s->selected_line < view->nlines - 2) {
2775 s->selected_line = MIN(s->blame.nlines,
2779 if (s->last_displayed_line + view->nlines - 2
2781 s->first_displayed_line +=
2784 s->first_displayed_line =
2789 if (s->selected_line > view->nlines - 2) {
2790 s->selected_line = MIN(s->blame.nlines,
2799 got_object_close(pobj);
2800 return thread_err ? thread_err : err;
2803 static const struct got_error *
2804 cmd_blame(int argc, char *argv[])
2806 const struct got_error *error;
2807 struct got_repository *repo = NULL;
2808 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2809 struct got_object_id *commit_id = NULL;
2810 char *commit_id_str = NULL;
2812 struct tog_view *view;
2815 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2820 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2823 commit_id_str = optarg;
2826 repo_path = realpath(optarg, NULL);
2827 if (repo_path == NULL)
2828 err(1, "-r option");
2844 cwd = getcwd(NULL, 0);
2846 error = got_error_from_errno();
2849 if (repo_path == NULL) {
2850 repo_path = strdup(cwd);
2851 if (repo_path == NULL) {
2852 error = got_error_from_errno();
2858 error = got_repo_open(&repo, repo_path);
2862 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2866 if (commit_id_str == NULL) {
2867 struct got_reference *head_ref;
2868 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2871 error = got_ref_resolve(&commit_id, repo, head_ref);
2872 got_ref_close(head_ref);
2874 struct got_object *obj;
2875 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2878 commit_id = got_object_id_dup(got_object_get_id(obj));
2879 if (commit_id == NULL)
2880 error = got_error_from_errno();
2881 got_object_close(obj);
2886 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2888 error = got_error_from_errno();
2891 error = open_blame_view(view, in_repo_path, commit_id, repo);
2894 error = view_loop(view);
2900 got_repo_close(repo);
2904 static const struct got_error *
2905 draw_tree_entries(struct tog_view *view,
2906 struct got_tree_entry **first_displayed_entry,
2907 struct got_tree_entry **last_displayed_entry,
2908 struct got_tree_entry **selected_entry, int *ndisplayed,
2909 const char *label, int show_ids, const char *parent_path,
2910 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2912 const struct got_error *err = NULL;
2913 struct got_tree_entry *te;
2919 werase(view->window);
2924 err = format_line(&wline, &width, label, view->ncols);
2927 if (view_needs_focus_indication(view))
2928 wstandout(view->window);
2929 waddwstr(view->window, wline);
2930 if (view_needs_focus_indication(view))
2931 wstandend(view->window);
2934 if (width < view->ncols)
2935 waddch(view->window, '\n');
2938 err = format_line(&wline, &width, parent_path, view->ncols);
2941 waddwstr(view->window, wline);
2944 if (width < view->ncols)
2945 waddch(view->window, '\n');
2948 waddch(view->window, '\n');
2952 te = SIMPLEQ_FIRST(&entries->head);
2953 if (*first_displayed_entry == NULL) {
2954 if (selected == 0) {
2956 wstandout(view->window);
2957 *selected_entry = NULL;
2959 waddstr(view->window, " ..\n"); /* parent directory */
2960 if (selected == 0 && view->focussed)
2961 wstandend(view->window);
2968 while (te != *first_displayed_entry)
2969 te = SIMPLEQ_NEXT(te, entry);
2973 char *line = NULL, *id_str = NULL;
2976 err = got_object_id_str(&id_str, te->id);
2978 return got_error_from_errno();
2980 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2981 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2983 return got_error_from_errno();
2986 err = format_line(&wline, &width, line, view->ncols);
2991 if (n == selected) {
2993 wstandout(view->window);
2994 *selected_entry = te;
2996 waddwstr(view->window, wline);
2997 if (width < view->ncols)
2998 waddch(view->window, '\n');
2999 if (n == selected && view->focussed)
3000 wstandend(view->window);
3006 *last_displayed_entry = te;
3009 te = SIMPLEQ_NEXT(te, entry);
3016 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3017 const struct got_tree_entries *entries, int isroot)
3019 struct got_tree_entry *te, *prev;
3022 if (*first_displayed_entry == NULL)
3025 te = SIMPLEQ_FIRST(&entries->head);
3026 if (*first_displayed_entry == te) {
3028 *first_displayed_entry = NULL;
3032 /* XXX this is stupid... switch to TAILQ? */
3033 for (i = 0; i < maxscroll; i++) {
3034 while (te != *first_displayed_entry) {
3036 te = SIMPLEQ_NEXT(te, entry);
3038 *first_displayed_entry = prev;
3039 te = SIMPLEQ_FIRST(&entries->head);
3041 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3042 *first_displayed_entry = NULL;
3046 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3047 struct got_tree_entry *last_displayed_entry,
3048 const struct got_tree_entries *entries)
3050 struct got_tree_entry *next;
3053 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
3056 if (*first_displayed_entry)
3057 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3059 next = SIMPLEQ_FIRST(&entries->head);
3061 *first_displayed_entry = next;
3062 if (++n >= maxscroll)
3064 next = SIMPLEQ_NEXT(next, entry);
3068 static const struct got_error *
3069 tree_entry_path(char **path, struct tog_parent_trees *parents,
3070 struct got_tree_entry *te)
3072 const struct got_error *err = NULL;
3073 struct tog_parent_tree *pt;
3074 size_t len = 2; /* for leading slash and NUL */
3076 TAILQ_FOREACH(pt, parents, entry)
3077 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3079 len += strlen(te->name);
3081 *path = calloc(1, len);
3083 return got_error_from_errno();
3086 pt = TAILQ_LAST(parents, tog_parent_trees);
3088 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3089 err = got_error(GOT_ERR_NO_SPACE);
3092 if (strlcat(*path, "/", len) >= len) {
3093 err = got_error(GOT_ERR_NO_SPACE);
3096 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3099 if (strlcat(*path, te->name, len) >= len) {
3100 err = got_error(GOT_ERR_NO_SPACE);
3112 static const struct got_error *
3113 blame_tree_entry(struct tog_view **new_view, int begin_x,
3114 struct got_tree_entry *te, struct tog_parent_trees *parents,
3115 struct got_object_id *commit_id, struct got_repository *repo)
3117 const struct got_error *err = NULL;
3119 struct tog_view *blame_view;
3121 err = tree_entry_path(&path, parents, te);
3125 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3126 if (blame_view == NULL)
3127 return got_error_from_errno();
3129 err = open_blame_view(blame_view, path, commit_id, repo);
3131 view_close(blame_view);
3134 *new_view = blame_view;
3138 static const struct got_error *
3139 log_tree_entry(struct tog_view **new_view, int begin_x,
3140 struct got_tree_entry *te, struct tog_parent_trees *parents,
3141 struct got_object_id *commit_id, struct got_repository *repo)
3143 struct tog_view *log_view;
3144 const struct got_error *err = NULL;
3147 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3148 if (log_view == NULL)
3149 return got_error_from_errno();
3151 err = tree_entry_path(&path, parents, te);
3155 err = open_log_view(log_view, commit_id, repo, path, 0);
3157 view_close(log_view);
3159 *new_view = log_view;
3164 static const struct got_error *
3165 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3166 struct got_object_id *commit_id, struct got_repository *repo)
3168 const struct got_error *err = NULL;
3169 char *commit_id_str = NULL;
3170 struct tog_tree_view_state *s = &view->state.tree;
3172 TAILQ_INIT(&s->parents);
3174 err = got_object_id_str(&commit_id_str, commit_id);
3178 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3179 err = got_error_from_errno();
3183 s->root = s->tree = root;
3184 s->entries = got_object_tree_get_entries(root);
3185 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3186 s->commit_id = got_object_id_dup(commit_id);
3187 if (s->commit_id == NULL) {
3188 err = got_error_from_errno();
3193 view->show = show_tree_view;
3194 view->input = input_tree_view;
3195 view->close = close_tree_view;
3197 free(commit_id_str);
3199 free(s->tree_label);
3200 s->tree_label = NULL;
3205 static const struct got_error *
3206 close_tree_view(struct tog_view *view)
3208 struct tog_tree_view_state *s = &view->state.tree;
3210 free(s->tree_label);
3211 s->tree_label = NULL;
3213 s->commit_id = NULL;
3214 while (!TAILQ_EMPTY(&s->parents)) {
3215 struct tog_parent_tree *parent;
3216 parent = TAILQ_FIRST(&s->parents);
3217 TAILQ_REMOVE(&s->parents, parent, entry);
3221 if (s->tree != s->root)
3222 got_object_tree_close(s->tree);
3223 got_object_tree_close(s->root);
3228 static const struct got_error *
3229 show_tree_view(struct tog_view *view)
3231 const struct got_error *err = NULL;
3232 struct tog_tree_view_state *s = &view->state.tree;
3235 err = tree_entry_path(&parent_path, &s->parents, NULL);
3239 err = draw_tree_entries(view, &s->first_displayed_entry,
3240 &s->last_displayed_entry, &s->selected_entry,
3241 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3242 s->entries, s->selected, view->nlines, s->tree == s->root);
3249 static const struct got_error *
3250 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3251 struct tog_view **focus_view, struct tog_view *view, int ch)
3253 const struct got_error *err = NULL;
3254 struct tog_tree_view_state *s = &view->state.tree;
3255 struct tog_view *log_view;
3260 s->show_ids = !s->show_ids;
3263 if (!s->selected_entry)
3265 if (view_is_parent_view(view))
3266 begin_x = view_split_begin_x(view->begin_x);
3267 err = log_tree_entry(&log_view, begin_x,
3268 s->selected_entry, &s->parents,
3269 s->commit_id, s->repo);
3270 if (view_is_parent_view(view)) {
3271 err = view_close_child(view);
3274 err = view_set_child(view, log_view);
3276 view_close(log_view);
3279 *focus_view = log_view;
3280 view->child_focussed = 1;
3282 *new_view = log_view;
3286 if (s->selected > 0)
3288 if (s->selected > 0)
3290 tree_scroll_up(&s->first_displayed_entry, 1,
3291 s->entries, s->tree == s->root);
3295 if (SIMPLEQ_FIRST(&s->entries->head) ==
3296 s->first_displayed_entry) {
3297 if (s->tree != s->root)
3298 s->first_displayed_entry = NULL;
3301 tree_scroll_up(&s->first_displayed_entry,
3302 view->nlines, s->entries,
3303 s->tree == s->root);
3307 if (s->selected < s->ndisplayed - 1) {
3311 tree_scroll_down(&s->first_displayed_entry, 1,
3312 s->last_displayed_entry, s->entries);
3315 tree_scroll_down(&s->first_displayed_entry,
3316 view->nlines, s->last_displayed_entry,
3318 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3321 /* can't scroll any further; move cursor down */
3322 if (s->selected < s->ndisplayed - 1)
3323 s->selected = s->ndisplayed - 1;
3327 if (s->selected_entry == NULL) {
3328 struct tog_parent_tree *parent;
3330 /* user selected '..' */
3331 if (s->tree == s->root)
3333 parent = TAILQ_FIRST(&s->parents);
3334 TAILQ_REMOVE(&s->parents, parent,
3336 got_object_tree_close(s->tree);
3337 s->tree = parent->tree;
3339 got_object_tree_get_entries(s->tree);
3340 s->first_displayed_entry =
3341 parent->first_displayed_entry;
3343 parent->selected_entry;
3344 s->selected = parent->selected;
3346 } else if (S_ISDIR(s->selected_entry->mode)) {
3347 struct tog_parent_tree *parent;
3348 struct got_tree_object *child;
3349 err = got_object_open_as_tree(&child,
3350 s->repo, s->selected_entry->id);
3353 parent = calloc(1, sizeof(*parent));
3354 if (parent == NULL) {
3355 err = got_error_from_errno();
3358 parent->tree = s->tree;
3359 parent->first_displayed_entry =
3360 s->first_displayed_entry;
3361 parent->selected_entry = s->selected_entry;
3362 parent->selected = s->selected;
3363 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3366 got_object_tree_get_entries(s->tree);
3368 s->first_displayed_entry = NULL;
3369 } else if (S_ISREG(s->selected_entry->mode)) {
3370 struct tog_view *blame_view;
3371 int begin_x = view_is_parent_view(view) ?
3372 view_split_begin_x(view->begin_x) : 0;
3374 err = blame_tree_entry(&blame_view, begin_x,
3375 s->selected_entry, &s->parents, s->commit_id,
3379 if (view_is_parent_view(view)) {
3380 err = view_close_child(view);
3383 err = view_set_child(view, blame_view);
3385 view_close(blame_view);
3388 *focus_view = blame_view;
3389 view->child_focussed = 1;
3391 *new_view = blame_view;
3395 if (s->selected > view->nlines)
3396 s->selected = s->ndisplayed - 1;
3409 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3414 static const struct got_error *
3415 cmd_tree(int argc, char *argv[])
3417 const struct got_error *error;
3418 struct got_repository *repo = NULL;
3419 char *repo_path = NULL;
3420 struct got_object_id *commit_id = NULL;
3421 char *commit_id_arg = NULL;
3422 struct got_commit_object *commit = NULL;
3423 struct got_tree_object *tree = NULL;
3425 struct tog_view *view;
3428 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3433 while ((ch = getopt(argc, argv, "c:")) != -1) {
3436 commit_id_arg = optarg;
3448 repo_path = getcwd(NULL, 0);
3449 if (repo_path == NULL)
3450 return got_error_from_errno();
3451 } else if (argc == 1) {
3452 repo_path = realpath(argv[0], NULL);
3453 if (repo_path == NULL)
3454 return got_error_from_errno();
3458 error = got_repo_open(&repo, repo_path);
3463 if (commit_id_arg == NULL) {
3464 error = get_head_commit_id(&commit_id, repo);
3468 struct got_object *obj;
3469 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3470 if (error == NULL) {
3471 commit_id = got_object_id_dup(got_object_get_id(obj));
3472 if (commit_id == NULL)
3473 error = got_error_from_errno();
3479 error = got_object_open_as_commit(&commit, repo, commit_id);
3483 error = got_object_open_as_tree(&tree, repo,
3484 got_object_commit_get_tree_id(commit));
3488 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3490 error = got_error_from_errno();
3493 error = open_tree_view(view, tree, commit_id, repo);
3496 error = view_loop(view);
3500 got_object_commit_close(commit);
3502 got_object_tree_close(tree);
3504 got_repo_close(repo);
3513 halfdelay(1); /* Do fast refresh while initial view is loading. */
3516 intrflush(stdscr, FALSE);
3517 keypad(stdscr, TRUE);
3519 signal(SIGWINCH, tog_sigwinch);
3527 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3528 "Available commands:\n", getprogname());
3529 for (i = 0; i < nitems(tog_commands); i++) {
3530 struct tog_cmd *cmd = &tog_commands[i];
3531 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3537 make_argv(const char *arg0, const char *arg1)
3540 int argc = (arg1 == NULL ? 1 : 2);
3542 argv = calloc(argc, sizeof(char *));
3545 argv[0] = strdup(arg0);
3546 if (argv[0] == NULL)
3549 argv[1] = strdup(arg1);
3550 if (argv[1] == NULL)
3558 main(int argc, char *argv[])
3560 const struct got_error *error = NULL;
3561 struct tog_cmd *cmd = NULL;
3563 char **cmd_argv = NULL;
3565 setlocale(LC_ALL, "");
3567 while ((ch = getopt(argc, argv, "h")) != -1) {
3586 /* Build an argument vector which runs a default command. */
3587 cmd = &tog_commands[0];
3588 cmd_argv = make_argv(cmd->name, NULL);
3593 /* Did the user specific a command? */
3594 for (i = 0; i < nitems(tog_commands); i++) {
3595 if (strncmp(tog_commands[i].name, argv[0],
3596 strlen(argv[0])) == 0) {
3597 cmd = &tog_commands[i];
3599 tog_commands[i].cmd_usage();
3604 /* Did the user specify a repository? */
3605 char *repo_path = realpath(argv[0], NULL);
3607 struct got_repository *repo;
3608 error = got_repo_open(&repo, repo_path);
3610 got_repo_close(repo);
3612 error = got_error_from_errno();
3615 fprintf(stderr, "%s: '%s' is not a "
3616 "known command\n", getprogname(),
3620 fprintf(stderr, "%s: '%s' is neither a known "
3621 "command nor a path to a repository\n",
3622 getprogname(), argv[0]);
3626 cmd = &tog_commands[0];
3627 cmd_argv = make_argv(cmd->name, repo_path);
3635 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3642 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);