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>
21 #define _XOPEN_SOURCE_EXTENDED
23 #undef _XOPEN_SOURCE_EXTENDED
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_commit_graph.h"
46 #include "got_blame.h"
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 const struct got_error *(*cmd_main)(int, char *[]);
59 void (*cmd_usage)(void);
63 __dead static void usage(void);
64 __dead static void usage_log(void);
65 __dead static void usage_diff(void);
66 __dead static void usage_blame(void);
67 __dead static void usage_tree(void);
69 static const struct got_error* cmd_log(int, char *[]);
70 static const struct got_error* cmd_diff(int, char *[]);
71 static const struct got_error* cmd_blame(int, char *[]);
72 static const struct got_error* cmd_tree(int, char *[]);
74 static struct tog_cmd tog_commands[] = {
75 { "log", cmd_log, usage_log,
76 "show repository history" },
77 { "diff", cmd_diff, usage_diff,
78 "compare files and directories" },
79 { "blame", cmd_blame, usage_blame,
80 "show line-by-line file history" },
81 { "tree", cmd_tree, usage_tree,
82 "browse trees in repository" },
92 struct tog_diff_view_state {
93 struct got_object_id *id1, *id2;
95 int first_displayed_line;
96 int last_displayed_line;
100 struct commit_queue_entry {
101 TAILQ_ENTRY(commit_queue_entry) entry;
102 struct got_object_id *id;
103 struct got_commit_object *commit;
105 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 struct commit_queue {
108 struct commit_queue_head head;
111 struct tog_log_view_state {
112 struct got_commit_graph *graph;
113 struct commit_queue commits;
114 struct commit_queue_entry *first_displayed_entry;
115 struct commit_queue_entry *last_displayed_entry;
116 struct commit_queue_entry *selected_entry;
119 struct got_repository *repo;
122 struct tog_blame_cb_args {
123 pthread_mutex_t *mutex;
124 struct tog_blame_line *lines; /* one per line */
127 struct tog_view *view;
128 struct got_object_id *commit_id;
131 int *first_displayed_line;
132 int *last_displayed_line;
138 struct tog_blame_thread_args {
140 struct got_repository *repo;
141 struct tog_blame_cb_args *cb_args;
148 struct tog_blame_line *lines;
151 struct tog_blame_thread_args thread_args;
152 struct tog_blame_cb_args cb_args;
156 struct tog_blame_view_state {
157 int first_displayed_line;
158 int last_displayed_line;
163 pthread_mutex_t mutex;
164 struct got_object_id_queue blamed_commits;
165 struct got_object_qid *blamed_commit;
167 struct got_repository *repo;
168 struct got_object_id *commit_id;
169 struct tog_blame blame;
172 struct tog_parent_tree {
173 TAILQ_ENTRY(tog_parent_tree) entry;
174 struct got_tree_object *tree;
175 struct got_tree_entry *first_displayed_entry;
176 struct got_tree_entry *selected_entry;
180 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 struct tog_tree_view_state {
184 struct got_tree_object *root;
185 struct got_tree_object *tree;
186 const struct got_tree_entries *entries;
187 struct got_tree_entry *first_displayed_entry;
188 struct got_tree_entry *last_displayed_entry;
189 struct got_tree_entry *selected_entry;
190 int nentries, ndisplayed, selected, show_ids;
191 struct tog_parent_trees parents;
192 struct got_object_id *commit_id;
193 struct got_repository *repo;
196 TAILQ_HEAD(tog_view_list_head, tog_view);
198 TAILQ_ENTRY(tog_view) entry;
201 int nlines, ncols, begin_y, begin_x;
202 int lines, cols; /* copies of LINES and COLS */
204 struct tog_view *parent;
205 struct tog_view *child;
207 /* type-specific state */
208 enum tog_view_type type;
210 struct tog_diff_view_state diff;
211 struct tog_log_view_state log;
212 struct tog_blame_view_state blame;
213 struct tog_tree_view_state tree;
216 const struct got_error *(*show)(struct tog_view *);
217 const struct got_error *(*input)(struct tog_view **,
218 struct tog_view **, struct tog_view *, int);
219 const struct got_error *(*set_child)(struct tog_view *,
221 const struct got_error *(*close)(struct tog_view *);
224 static const struct got_error *open_diff_view(struct tog_view *,
225 struct got_object *, struct got_object *, struct got_repository *);
226 static const struct got_error *show_diff_view(struct tog_view *);
227 static const struct got_error *input_diff_view(struct tog_view **,
228 struct tog_view **, struct tog_view *, int);
229 static const struct got_error* close_diff_view(struct tog_view *);
231 static const struct got_error *open_log_view(struct tog_view *,
232 struct got_object_id *, struct got_repository *, const char *);
233 static const struct got_error * show_log_view(struct tog_view *);
234 static const struct got_error *input_log_view(struct tog_view **,
235 struct tog_view **, struct tog_view *, int);
236 static const struct got_error *close_log_view(struct tog_view *);
237 static const struct got_error* set_child_log_view(struct tog_view *,
240 static const struct got_error *open_blame_view(struct tog_view *, char *,
241 struct got_object_id *, struct got_repository *);
242 static const struct got_error *show_blame_view(struct tog_view *);
243 static const struct got_error *input_blame_view(struct tog_view **,
244 struct tog_view **, struct tog_view *, int);
245 static const struct got_error *close_blame_view(struct tog_view *);
247 static const struct got_error *open_tree_view(struct tog_view *,
248 struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 static const struct got_error *show_tree_view(struct tog_view *);
250 static const struct got_error *input_tree_view(struct tog_view **,
251 struct tog_view **, struct tog_view *, int);
252 static const struct got_error *close_tree_view(struct tog_view *);
254 static const struct got_error *
255 view_close(struct tog_view *view)
257 const struct got_error *err = NULL;
260 view->child->parent = NULL;
262 view->parent->child = NULL;
264 err = view->close(view);
266 del_panel(view->panel);
268 delwin(view->window);
273 static struct tog_view *
274 view_open(int nlines, int ncols, int begin_y, int begin_x,
275 struct tog_view *parent, enum tog_view_type type)
277 struct tog_view *view = calloc(1, sizeof(*view));
282 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 begin_x = parent->ncols - 80;
285 view->parent = parent;
287 parent->child = view;
291 view->nlines = nlines ? nlines : LINES - begin_y;
292 view->ncols = ncols ? ncols : COLS - begin_x;
293 view->begin_y = begin_y;
294 view->begin_x = begin_x;
295 view->window = newwin(nlines, ncols, begin_y, begin_x);
296 if (view->window == NULL) {
300 view->panel = new_panel(view->window);
301 if (view->panel == NULL) {
306 keypad(view->window, TRUE);
310 static const struct got_error *
311 view_show(struct tog_view *view)
313 const struct got_error *err;
316 err = view->parent->show(view->parent);
319 show_panel(view->parent->panel);
322 err = view->show(view);
325 show_panel(view->panel);
327 if (view->child && view->child->begin_x > view->begin_x) {
328 err = view->child->show(view->child);
331 show_panel(view->child->panel);
340 static const struct got_error *
341 view_resize(struct tog_view *view)
346 if (view->lines > LINES)
347 nlines = view->nlines - (view->lines - LINES);
349 nlines = view->nlines + (LINES - view->lines);
351 if (view->cols > COLS)
352 ncols = view->ncols - (view->cols - COLS);
354 ncols = view->ncols + (COLS - view->cols);
356 if (wresize(view->window, nlines, ncols) == ERR)
357 return got_error_from_errno();
358 replace_panel(view->panel, view->window);
360 view->nlines = nlines;
371 static const struct got_error *
372 view_input(struct tog_view **new, struct tog_view **dead,
373 struct tog_view **focus, int *done, struct tog_view *view,
374 struct tog_view_list_head *views)
376 const struct got_error *err = NULL;
377 struct tog_view *next, *prev;
383 nodelay(stdscr, FALSE);
384 ch = wgetch(view->window);
385 nodelay(stdscr, TRUE);
390 next = TAILQ_NEXT(view, entry);
394 *focus = TAILQ_FIRST(views);
396 (*focus)->focussed = 1;
399 prev = TAILQ_PREV(view, tog_view_list_head, entry);
403 *focus = TAILQ_LAST(views, tog_view_list_head);
405 (*focus)->focussed = 1;
408 err = view->input(new, dead, view, ch);
415 err = view_resize(view);
418 err = view->input(new, dead, view, ch);
421 err = view->input(new, dead, view, ch);
428 static const struct got_error *
429 view_set_child(struct tog_view *view, struct tog_view *child)
431 const struct got_error *err;
433 if (view->set_child) {
434 err = view->set_child(view, child);
444 view_vborder(struct tog_view *view)
446 if (view->child == NULL)
449 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
450 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
454 view_needs_focus_indication(struct tog_view *view)
459 if (view->child && view->child->begin_x > view->begin_x)
462 if (view->parent && view->begin_x > view->parent->begin_x)
468 static const struct got_error *
469 view_loop(struct tog_view *view)
471 const struct got_error *err = NULL;
472 struct tog_view_list_head views;
473 struct tog_view *new_view, *dead_view;
477 TAILQ_INSERT_HEAD(&views, view, entry);
480 while (!TAILQ_EMPTY(&views) && !done) {
481 err = view_show(view);
484 err = view_input(&new_view, &dead_view, &view, &done,
489 struct tog_view *v, *t;
490 TAILQ_REMOVE(&views, dead_view, entry);
491 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
492 if (v->parent == dead_view) {
493 TAILQ_REMOVE(&views, v, entry);
499 if (dead_view->parent)
500 view = dead_view->parent;
502 view = TAILQ_LAST(&views, tog_view_list_head);
505 err = view_close(dead_view);
511 /* TODO: de-duplicate! */
512 TAILQ_INSERT_TAIL(&views, new_view, entry);
513 if (new_view->parent) {
514 err = view_set_child(new_view->parent, new_view);
517 new_view->parent->focussed = 0;
524 while (!TAILQ_EMPTY(&views)) {
525 view = TAILQ_FIRST(&views);
526 TAILQ_REMOVE(&views, view, entry);
537 "usage: %s log [-c commit] [-r repository-path] [path]\n",
542 /* Create newly allocated wide-character string equivalent to a byte string. */
543 static const struct got_error *
544 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
547 const struct got_error *err = NULL;
550 *wlen = mbstowcs(NULL, s, 0);
551 if (*wlen == (size_t)-1) {
554 return got_error_from_errno();
556 /* byte string invalid in current encoding; try to "fix" it */
557 err = got_mbsavis(&vis, &vislen, s);
560 *wlen = mbstowcs(NULL, vis, 0);
561 if (*wlen == (size_t)-1) {
562 err = got_error_from_errno(); /* give up */
567 *ws = calloc(*wlen + 1, sizeof(*ws));
569 err = got_error_from_errno();
573 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
574 err = got_error_from_errno();
585 /* Format a line for display, ensuring that it won't overflow a width limit. */
586 static const struct got_error *
587 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
589 const struct got_error *err = NULL;
591 wchar_t *wline = NULL;
598 err = mbs2ws(&wline, &wlen, line);
603 while (i < wlen && cols < wlimit) {
604 int width = wcwidth(wline[i]);
611 if (cols + width <= wlimit)
616 if (wline[i] == L'\t')
617 cols += TABSIZE - ((cols + 1) % TABSIZE);
621 err = got_error_from_errno();
636 static const struct got_error *
637 draw_commit(struct tog_view *view, struct got_commit_object *commit,
638 struct got_object_id *id)
640 const struct got_error *err = NULL;
641 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
642 char *logmsg0 = NULL, *logmsg = NULL;
643 char *author0 = NULL, *author = NULL;
644 wchar_t *wlogmsg = NULL, *wauthor = NULL;
645 int author_width, logmsg_width;
646 char *newline, *smallerthan;
649 static const size_t date_display_cols = 9;
650 static const size_t author_display_cols = 16;
651 const int avail = view->ncols;
653 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
654 &commit->tm_committer) >= sizeof(datebuf))
655 return got_error(GOT_ERR_NO_SPACE);
657 if (avail < date_display_cols)
658 limit = MIN(sizeof(datebuf) - 1, avail);
660 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
661 waddnstr(view->window, datebuf, limit);
666 author0 = strdup(commit->author);
667 if (author0 == NULL) {
668 err = got_error_from_errno();
672 smallerthan = strchr(author, '<');
676 char *at = strchr(author, '@');
681 err = format_line(&wauthor, &author_width, author, limit);
684 waddwstr(view->window, wauthor);
686 while (col <= avail && author_width < author_display_cols + 1) {
687 waddch(view->window, ' ');
694 logmsg0 = strdup(commit->logmsg);
695 if (logmsg0 == NULL) {
696 err = got_error_from_errno();
700 while (*logmsg == '\n')
702 newline = strchr(logmsg, '\n');
706 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
709 waddwstr(view->window, wlogmsg);
711 while (col <= avail) {
712 waddch(view->window, ' ');
724 static struct commit_queue_entry *
725 alloc_commit_queue_entry(struct got_commit_object *commit,
726 struct got_object_id *id)
728 struct commit_queue_entry *entry;
730 entry = calloc(1, sizeof(*entry));
735 entry->commit = commit;
740 pop_commit(struct commit_queue *commits)
742 struct commit_queue_entry *entry;
744 entry = TAILQ_FIRST(&commits->head);
745 TAILQ_REMOVE(&commits->head, entry, entry);
746 got_object_commit_close(entry->commit);
748 /* Don't free entry->id! It is owned by the commit graph. */
753 free_commits(struct commit_queue *commits)
755 while (!TAILQ_EMPTY(&commits->head))
759 static const struct got_error *
760 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
761 struct got_object_id *start_id, int minqueue, int init,
762 struct got_repository *repo, const char *path)
764 const struct got_error *err = NULL;
765 struct got_object_id *id;
766 struct commit_queue_entry *entry;
767 int nfetched, nqueued = 0, found_obj = 0;
768 int is_root_path = strcmp(path, "/") == 0;
770 err = got_commit_graph_iter_start(graph, start_id);
774 entry = TAILQ_LAST(&commits->head, commit_queue_head);
775 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
778 /* Start ID's commit is already on the queue; skip over it. */
779 err = got_commit_graph_iter_next(&id, graph);
780 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
783 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
789 struct got_commit_object *commit;
791 err = got_commit_graph_iter_next(&id, graph);
793 if (err->code != GOT_ERR_ITER_NEED_MORE)
795 if (nqueued >= minqueue) {
799 err = got_commit_graph_fetch_commits(&nfetched,
808 err = got_object_open_as_commit(&commit, repo, id);
813 struct got_object *obj;
814 struct got_object_qid *pid;
817 err = got_object_open_by_path(&obj, repo, id, path);
819 got_object_commit_close(commit);
820 if (err->code == GOT_ERR_NO_OBJ &&
821 (found_obj || !init)) {
822 /* History stops here. */
823 err = got_error(GOT_ERR_ITER_COMPLETED);
829 pid = SIMPLEQ_FIRST(&commit->parent_ids);
831 struct got_object *pobj;
832 err = got_object_open_by_path(&pobj, repo,
835 if (err->code != GOT_ERR_NO_OBJ) {
836 got_object_close(obj);
837 got_object_commit_close(commit);
843 struct got_object_id *id, *pid;
844 id = got_object_get_id(obj);
846 err = got_error_from_errno();
847 got_object_close(obj);
848 got_object_close(pobj);
851 pid = got_object_get_id(pobj);
853 err = got_error_from_errno();
855 got_object_close(obj);
856 got_object_close(pobj);
860 (got_object_id_cmp(id, pid) != 0);
861 got_object_close(pobj);
866 got_object_close(obj);
868 got_object_commit_close(commit);
873 entry = alloc_commit_queue_entry(commit, id);
875 err = got_error_from_errno();
878 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
886 static const struct got_error *
887 fetch_next_commit(struct commit_queue_entry **pentry,
888 struct commit_queue_entry *entry, struct commit_queue *commits,
889 struct got_commit_graph *graph, struct got_repository *repo,
892 const struct got_error *err = NULL;
896 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
900 /* Next entry to display should now be available. */
901 *pentry = TAILQ_NEXT(entry, entry);
903 return got_error(GOT_ERR_NO_OBJ);
908 static const struct got_error *
909 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
911 const struct got_error *err = NULL;
912 struct got_reference *head_ref;
916 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
920 err = got_ref_resolve(head_id, repo, head_ref);
921 got_ref_close(head_ref);
930 static const struct got_error *
931 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
932 struct commit_queue_entry **selected, struct commit_queue_entry *first,
933 struct commit_queue *commits, int selected_idx, int limit,
934 struct got_commit_graph *graph, struct got_repository *repo,
937 const struct got_error *err = NULL;
938 struct commit_queue_entry *entry;
940 char *id_str, *header;
946 if (ncommits == selected_idx) {
950 entry = TAILQ_NEXT(entry, entry);
954 err = got_object_id_str(&id_str, (*selected)->id);
958 if (path && strcmp(path, "/") != 0) {
959 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
960 err = got_error_from_errno();
964 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
965 err = got_error_from_errno();
970 err = format_line(&wline, &width, header, view->ncols);
977 werase(view->window);
979 if (view_needs_focus_indication(view))
980 wstandout(view->window);
981 waddwstr(view->window, wline);
982 if (view_needs_focus_indication(view))
983 wstandend(view->window);
984 if (width < view->ncols)
985 waddch(view->window, '\n');
994 if (ncommits >= limit - 1)
996 if (ncommits == selected_idx)
997 wstandout(view->window);
998 err = draw_commit(view, entry->commit, entry->id);
999 if (ncommits == selected_idx)
1000 wstandend(view->window);
1005 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1006 err = queue_commits(graph, commits, entry->id, 1,
1009 if (err->code != GOT_ERR_ITER_COMPLETED)
1014 entry = TAILQ_NEXT(entry, entry);
1023 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1024 struct commit_queue *commits)
1026 struct commit_queue_entry *entry;
1029 entry = TAILQ_FIRST(&commits->head);
1030 if (*first_displayed_entry == entry)
1033 entry = *first_displayed_entry;
1034 while (entry && nscrolled < maxscroll) {
1035 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1037 *first_displayed_entry = entry;
1043 static const struct got_error *
1044 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1045 struct commit_queue_entry *last_displayed_entry,
1046 struct commit_queue *commits, struct got_commit_graph *graph,
1047 struct got_repository *repo, const char *path)
1049 const struct got_error *err = NULL;
1050 struct commit_queue_entry *pentry;
1054 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1055 if (pentry == NULL) {
1056 err = fetch_next_commit(&pentry, last_displayed_entry,
1057 commits, graph, repo, path);
1058 if (err || pentry == NULL)
1061 last_displayed_entry = pentry;
1063 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1066 *first_displayed_entry = pentry;
1067 } while (++nscrolled < maxscroll);
1072 static const struct got_error *
1073 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1074 struct commit_queue_entry *entry, struct got_repository *repo)
1076 const struct got_error *err;
1077 struct got_object *obj1 = NULL, *obj2 = NULL;
1078 struct got_object_qid *parent_id;
1079 struct tog_view *diff_view;
1081 err = got_object_open(&obj2, repo, entry->id);
1085 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1087 err = got_object_open(&obj1, repo, parent_id->id);
1092 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1093 if (diff_view == NULL) {
1094 err = got_error_from_errno();
1098 err = open_diff_view(diff_view, obj1, obj2, repo);
1100 *new_view = diff_view;
1103 got_object_close(obj1);
1105 got_object_close(obj2);
1109 static const struct got_error *
1110 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1111 struct commit_queue_entry *entry, struct got_repository *repo)
1113 const struct got_error *err = NULL;
1114 struct got_tree_object *tree;
1115 struct tog_view *tree_view;
1117 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1121 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1122 if (tree_view == NULL)
1123 return got_error_from_errno();
1125 err = open_tree_view(tree_view, tree, entry->id, repo);
1127 got_object_tree_close(tree);
1129 *new_view = tree_view;
1133 static const struct got_error *
1134 set_child_log_view(struct tog_view *view, struct tog_view *child)
1136 struct tog_log_view_state *s = &view->state.log;
1137 struct tog_diff_view_state *ds;
1138 struct commit_queue_entry *commit, *child_entry = NULL;
1139 int selected_idx = 0;
1141 if (child->type != TOG_VIEW_DIFF)
1143 ds = &child->state.diff;
1145 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1146 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1147 child_entry = commit;
1151 if (child_entry == NULL)
1154 commit = s->first_displayed_entry;
1156 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1157 s->selected_entry = child_entry;
1158 s->selected = selected_idx;
1161 if (commit == s->last_displayed_entry)
1164 commit = TAILQ_NEXT(commit, entry);
1167 return show_log_view(view);
1170 static const struct got_error *
1171 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1172 struct got_repository *repo, const char *path)
1174 const struct got_error *err = NULL;
1175 struct got_object_id *head_id = NULL;
1177 struct tog_log_view_state *s = &view->state.log;
1179 err = got_repo_map_path(&s->in_repo_path, repo, path);
1183 err = get_head_commit_id(&head_id, repo);
1187 /* The graph contains all commits. */
1188 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1191 /* The commit queue contains a subset of commits filtered by path. */
1192 TAILQ_INIT(&s->commits.head);
1193 s->commits.ncommits = 0;
1195 /* Populate commit graph with a sufficient number of commits. */
1196 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1202 * Open the initial batch of commits, sorted in commit graph order.
1203 * We keep all commits open throughout the lifetime of the log view
1204 * in order to avoid having to re-fetch commits from disk while
1205 * updating the display.
1207 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1208 repo, s->in_repo_path);
1210 if (err->code != GOT_ERR_ITER_COMPLETED)
1215 s->first_displayed_entry =
1216 TAILQ_FIRST(&s->commits.head);
1217 s->selected_entry = s->first_displayed_entry;
1220 view->show = show_log_view;
1221 view->input = input_log_view;
1222 view->close = close_log_view;
1223 view->set_child = set_child_log_view;
1229 static const struct got_error *
1230 close_log_view(struct tog_view *view)
1232 struct tog_log_view_state *s = &view->state.log;
1235 got_commit_graph_close(s->graph);
1236 free_commits(&s->commits);
1237 free(s->in_repo_path);
1241 static const struct got_error *
1242 update_diff_child_view(struct tog_view *parent,
1243 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1245 const struct got_error *err = NULL;
1246 struct tog_diff_view_state *ds;
1247 struct got_object *obj1 = NULL, *obj2 = NULL;
1248 struct got_object_qid *parent_id;
1249 struct tog_view *child_view = parent->child;
1251 if (child_view == NULL)
1253 if (child_view->type != TOG_VIEW_DIFF)
1255 ds = &child_view->state.diff;
1256 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1259 err = got_object_open(&obj2, repo, selected_entry->id);
1263 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1265 err = got_object_open(&obj1, repo, parent_id->id);
1270 err = close_diff_view(child_view);
1274 err = open_diff_view(child_view, obj1, obj2, repo);
1279 got_object_close(obj1);
1281 got_object_close(obj2);
1285 static const struct got_error *
1286 show_log_view(struct tog_view *view)
1288 const struct got_error *err = NULL;
1289 struct tog_log_view_state *s = &view->state.log;
1291 err = draw_commits(view, &s->last_displayed_entry,
1292 &s->selected_entry, s->first_displayed_entry,
1293 &s->commits, s->selected, view->nlines, s->graph,
1294 s->repo, s->in_repo_path);
1298 return update_diff_child_view(view, s->selected_entry, s->repo);
1301 static const struct got_error *
1302 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1303 struct tog_view *view, int ch)
1305 const struct got_error *err = NULL;
1306 struct tog_log_view_state *s = &view->state.log;
1312 if (s->selected > 0)
1314 if (s->selected > 0)
1316 scroll_up(&s->first_displayed_entry, 1,
1320 if (TAILQ_FIRST(&s->commits.head) ==
1321 s->first_displayed_entry) {
1325 scroll_up(&s->first_displayed_entry,
1326 view->nlines, &s->commits);
1331 if (s->selected < MIN(view->nlines - 2,
1332 s->commits.ncommits - 1)) {
1336 err = scroll_down(&s->first_displayed_entry, 1,
1337 s->last_displayed_entry, &s->commits,
1338 s->graph, s->repo, s->in_repo_path);
1340 if (err->code != GOT_ERR_ITER_COMPLETED)
1346 struct commit_queue_entry *first;
1347 first = s->first_displayed_entry;
1348 err = scroll_down(&s->first_displayed_entry,
1349 view->nlines, s->last_displayed_entry,
1350 &s->commits, s->graph, s->repo,
1354 if (err->code != GOT_ERR_ITER_COMPLETED)
1356 if (first == s->first_displayed_entry &&
1357 s->selected < MIN(view->nlines - 2,
1358 s->commits.ncommits - 1)) {
1359 /* can't scroll further down */
1360 s->selected = MIN(view->nlines - 2,
1361 s->commits.ncommits - 1);
1367 if (s->selected > view->nlines - 2)
1368 s->selected = view->nlines - 2;
1369 if (s->selected > s->commits.ncommits - 1)
1370 s->selected = s->commits.ncommits - 1;
1374 err = show_commit(new_view, view, s->selected_entry,
1378 err = browse_commit(new_view, view, s->selected_entry,
1388 static const struct got_error *
1389 cmd_log(int argc, char *argv[])
1391 const struct got_error *error;
1392 struct got_repository *repo = NULL;
1393 struct got_object_id *start_id = NULL;
1394 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1395 char *start_commit = NULL;
1397 struct tog_view *view;
1400 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1405 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1408 start_commit = optarg;
1411 repo_path = realpath(optarg, NULL);
1412 if (repo_path == NULL)
1413 err(1, "-r option");
1427 path = strdup(argv[0]);
1431 return got_error_from_errno();
1433 cwd = getcwd(NULL, 0);
1435 error = got_error_from_errno();
1438 if (repo_path == NULL) {
1439 repo_path = strdup(cwd);
1440 if (repo_path == NULL) {
1441 error = got_error_from_errno();
1446 error = got_repo_open(&repo, repo_path);
1450 if (start_commit == NULL) {
1451 error = get_head_commit_id(&start_id, repo);
1455 struct got_object *obj;
1456 error = got_object_open_by_id_str(&obj, repo, start_commit);
1457 if (error == NULL) {
1458 start_id = got_object_get_id(obj);
1459 if (start_id == NULL)
1460 error = got_error_from_errno();
1467 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1469 error = got_error_from_errno();
1472 error = open_log_view(view, start_id, repo, path);
1475 error = view_loop(view);
1482 got_repo_close(repo);
1490 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1496 parse_next_line(FILE *f, size_t *len)
1501 const char delim[3] = { '\0', '\0', '\0'};
1503 line = fparseln(f, &linelen, &lineno, delim, 0);
1509 static const struct got_error *
1510 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1511 int *last_displayed_line, int *eof, int max_lines,
1514 const struct got_error *err;
1515 int nlines = 0, nprinted = 0;
1522 werase(view->window);
1525 err = format_line(&wline, &width, header, view->ncols);
1530 if (view_needs_focus_indication(view))
1531 wstandout(view->window);
1532 waddwstr(view->window, wline);
1533 if (view_needs_focus_indication(view))
1534 wstandend(view->window);
1535 if (width < view->ncols)
1536 waddch(view->window, '\n');
1544 while (nprinted < max_lines) {
1545 line = parse_next_line(f, &len);
1550 if (++nlines < *first_displayed_line) {
1555 err = format_line(&wline, &width, line, view->ncols);
1560 waddwstr(view->window, wline);
1561 if (width < view->ncols)
1562 waddch(view->window, '\n');
1563 if (++nprinted == 1)
1564 *first_displayed_line = nlines;
1569 *last_displayed_line = nlines;
1576 static const struct got_error *
1577 open_diff_view(struct tog_view *view, struct got_object *obj1,
1578 struct got_object *obj2, struct got_repository *repo)
1580 const struct got_error *err;
1583 if (obj1 != NULL && obj2 != NULL &&
1584 got_object_get_type(obj1) != got_object_get_type(obj2))
1585 return got_error(GOT_ERR_OBJ_TYPE);
1589 return got_error_from_errno();
1591 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1592 case GOT_OBJ_TYPE_BLOB:
1593 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1596 case GOT_OBJ_TYPE_TREE:
1597 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1599 case GOT_OBJ_TYPE_COMMIT:
1600 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1603 return got_error(GOT_ERR_OBJ_TYPE);
1608 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1609 view->state.diff.id2 = got_object_get_id(obj2);
1610 if (view->state.diff.id2 == NULL)
1611 return got_error_from_errno();
1612 view->state.diff.f = f;
1613 view->state.diff.first_displayed_line = 1;
1614 view->state.diff.last_displayed_line = view->nlines;
1616 view->show = show_diff_view;
1617 view->input = input_diff_view;
1618 view->close = close_diff_view;
1623 static const struct got_error *
1624 close_diff_view(struct tog_view *view)
1626 const struct got_error *err = NULL;
1628 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1629 err = got_error_from_errno();
1630 free(view->state.diff.id1);
1631 free(view->state.diff.id2);
1635 static const struct got_error *
1636 show_diff_view(struct tog_view *view)
1638 const struct got_error *err;
1639 struct tog_diff_view_state *s = &view->state.diff;
1640 char *id_str1 = NULL, *id_str2, *header;
1643 err = got_object_id_str(&id_str1, s->id1);
1647 err = got_object_id_str(&id_str2, s->id2);
1651 if (asprintf(&header, "diff: %s %s",
1652 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1653 err = got_error_from_errno();
1661 return draw_file(view, s->f, &s->first_displayed_line,
1662 &s->last_displayed_line, &s->eof, view->nlines,
1666 static const struct got_error *
1667 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1668 struct tog_view *view, int ch)
1670 const struct got_error *err = NULL;
1671 struct tog_diff_view_state *s = &view->state.diff;
1677 if (s->first_displayed_line > 1)
1678 s->first_displayed_line--;
1682 while (i++ < view->nlines - 1 &&
1683 s->first_displayed_line > 1)
1684 s->first_displayed_line--;
1689 s->first_displayed_line++;
1694 while (!s->eof && i++ < view->nlines - 1) {
1696 line = parse_next_line(s->f, NULL);
1697 s->first_displayed_line++;
1704 struct tog_log_view_state *ls;
1705 struct commit_queue_entry *entry;
1706 struct tog_view *diff_view;
1708 if (view->parent == NULL)
1710 if (view->parent->type != TOG_VIEW_LOG)
1712 ls = &view->parent->state.log;
1715 entry = TAILQ_PREV(ls->selected_entry,
1716 commit_queue_head, entry);
1718 entry = TAILQ_NEXT(ls->selected_entry, entry);
1719 if (entry == NULL) {
1720 err = fetch_next_commit(&entry,
1722 &ls->commits, ls->graph,
1723 ls->repo, ls->in_repo_path);
1730 err = show_commit(&diff_view, view->parent,
1734 *new_view = diff_view;
1745 static const struct got_error *
1746 cmd_diff(int argc, char *argv[])
1748 const struct got_error *error = NULL;
1749 struct got_repository *repo = NULL;
1750 struct got_object *obj1 = NULL, *obj2 = NULL;
1751 char *repo_path = NULL;
1752 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1754 struct tog_view *view;
1757 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1762 while ((ch = getopt(argc, argv, "")) != -1) {
1774 usage_diff(); /* TODO show local worktree changes */
1775 } else if (argc == 2) {
1776 repo_path = getcwd(NULL, 0);
1777 if (repo_path == NULL)
1778 return got_error_from_errno();
1779 obj_id_str1 = argv[0];
1780 obj_id_str2 = argv[1];
1781 } else if (argc == 3) {
1782 repo_path = realpath(argv[0], NULL);
1783 if (repo_path == NULL)
1784 return got_error_from_errno();
1785 obj_id_str1 = argv[1];
1786 obj_id_str2 = argv[2];
1790 error = got_repo_open(&repo, repo_path);
1795 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1799 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1803 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1805 error = got_error_from_errno();
1808 error = open_diff_view(view, obj1, obj2, repo);
1811 error = view_loop(view);
1813 got_repo_close(repo);
1815 got_object_close(obj1);
1817 got_object_close(obj2);
1825 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1830 struct tog_blame_line {
1832 struct got_object_id *id;
1835 static const struct got_error *
1836 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1837 const char *path, struct tog_blame_line *lines, int nlines,
1838 int blame_complete, int selected_line, int *first_displayed_line,
1839 int *last_displayed_line, int *eof, int max_lines)
1841 const struct got_error *err;
1842 int lineno = 0, nprinted = 0;
1847 struct tog_blame_line *blame_line;
1848 struct got_object_id *prev_id = NULL;
1851 err = got_object_id_str(&id_str, id);
1856 werase(view->window);
1858 if (asprintf(&line, "commit: %s", id_str) == -1) {
1859 err = got_error_from_errno();
1864 err = format_line(&wline, &width, line, view->ncols);
1867 if (view_needs_focus_indication(view))
1868 wstandout(view->window);
1869 waddwstr(view->window, wline);
1870 if (view_needs_focus_indication(view))
1871 wstandend(view->window);
1874 if (width < view->ncols)
1875 waddch(view->window, '\n');
1877 if (asprintf(&line, "[%d/%d] %s%s",
1878 *first_displayed_line - 1 + selected_line, nlines,
1879 blame_complete ? "" : "annotating ", path) == -1) {
1881 return got_error_from_errno();
1884 err = format_line(&wline, &width, line, view->ncols);
1889 waddwstr(view->window, wline);
1892 if (width < view->ncols)
1893 waddch(view->window, '\n');
1896 while (nprinted < max_lines - 2) {
1897 line = parse_next_line(f, &len);
1902 if (++lineno < *first_displayed_line) {
1907 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1908 err = format_line(&wline, &width, line, wlimit);
1914 if (nprinted == selected_line - 1)
1915 wstandout(view->window);
1917 blame_line = &lines[lineno - 1];
1918 if (blame_line->annotated && prev_id &&
1919 got_object_id_cmp(prev_id, blame_line->id) == 0)
1920 waddstr(view->window, " ");
1921 else if (blame_line->annotated) {
1923 err = got_object_id_str(&id_str, blame_line->id);
1929 wprintw(view->window, "%.8s ", id_str);
1931 prev_id = blame_line->id;
1933 waddstr(view->window, "........ ");
1937 waddwstr(view->window, wline);
1938 while (width < wlimit) {
1939 waddch(view->window, ' ');
1942 if (nprinted == selected_line - 1)
1943 wstandend(view->window);
1944 if (++nprinted == 1)
1945 *first_displayed_line = lineno;
1950 *last_displayed_line = lineno;
1957 static const struct got_error *
1958 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1960 const struct got_error *err = NULL;
1961 struct tog_blame_cb_args *a = arg;
1962 struct tog_blame_line *line;
1964 if (nlines != a->nlines ||
1965 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1966 return got_error(GOT_ERR_RANGE);
1968 if (pthread_mutex_lock(a->mutex) != 0)
1969 return got_error_from_errno();
1971 if (*a->quit) { /* user has quit the blame view */
1972 err = got_error(GOT_ERR_ITER_COMPLETED);
1977 goto done; /* no change in this commit */
1979 line = &a->lines[lineno - 1];
1980 if (line->annotated)
1983 line->id = got_object_id_dup(id);
1984 if (line->id == NULL) {
1985 err = got_error_from_errno();
1988 line->annotated = 1;
1990 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1991 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1992 a->last_displayed_line, a->eof, a->view->nlines);
1994 if (pthread_mutex_unlock(a->mutex) != 0)
1995 return got_error_from_errno();
2000 blame_thread(void *arg)
2002 const struct got_error *err;
2003 struct tog_blame_thread_args *ta = arg;
2004 struct tog_blame_cb_args *a = ta->cb_args;
2006 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2007 blame_cb, ta->cb_args);
2009 if (pthread_mutex_lock(a->mutex) != 0)
2010 return (void *)got_error_from_errno();
2012 got_repo_close(ta->repo);
2016 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2017 a->lines, a->nlines, 1, *a->selected_line,
2018 a->first_displayed_line, a->last_displayed_line, a->eof,
2021 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2022 err = got_error_from_errno();
2027 static struct got_object_id *
2028 get_selected_commit_id(struct tog_blame_line *lines,
2029 int first_displayed_line, int selected_line)
2031 struct tog_blame_line *line;
2033 line = &lines[first_displayed_line - 1 + selected_line - 1];
2034 if (!line->annotated)
2040 static const struct got_error *
2041 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2042 struct tog_blame_line *lines, int first_displayed_line,
2043 int selected_line, struct got_repository *repo)
2045 const struct got_error *err = NULL;
2046 struct got_commit_object *commit = NULL;
2047 struct got_object_id *selected_id;
2048 struct got_object_qid *pid;
2053 selected_id = get_selected_commit_id(lines,
2054 first_displayed_line, selected_line);
2055 if (selected_id == NULL)
2058 err = got_object_open(obj, repo, selected_id);
2062 err = got_object_commit_open(&commit, repo, *obj);
2066 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2068 err = got_object_open(pobj, repo, pid->id);
2074 got_object_commit_close(commit);
2078 static const struct got_error *
2079 stop_blame(struct tog_blame *blame)
2081 const struct got_error *err = NULL;
2084 if (blame->thread) {
2085 if (pthread_join(blame->thread, (void **)&err) != 0)
2086 err = got_error_from_errno();
2087 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2089 blame->thread = NULL;
2091 if (blame->thread_args.repo) {
2092 got_repo_close(blame->thread_args.repo);
2093 blame->thread_args.repo = NULL;
2099 for (i = 0; i < blame->nlines; i++)
2100 free(blame->lines[i].id);
2102 blame->lines = NULL;
2103 free(blame->cb_args.commit_id);
2104 blame->cb_args.commit_id = NULL;
2109 static const struct got_error *
2110 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2111 struct tog_view *view, int *blame_complete,
2112 int *first_displayed_line, int *last_displayed_line,
2113 int *selected_line, int *done, int *eof, const char *path,
2114 struct got_object_id *commit_id,
2115 struct got_repository *repo)
2117 const struct got_error *err = NULL;
2118 struct got_blob_object *blob = NULL;
2119 struct got_repository *thread_repo = NULL;
2120 struct got_object *obj;
2122 err = got_object_open_by_path(&obj, repo, commit_id, path);
2125 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2126 err = got_error(GOT_ERR_OBJ_TYPE);
2130 err = got_object_blob_open(&blob, repo, obj, 8192);
2133 blame->f = got_opentemp();
2134 if (blame->f == NULL) {
2135 err = got_error_from_errno();
2138 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2143 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2144 if (blame->lines == NULL) {
2145 err = got_error_from_errno();
2149 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2153 blame->cb_args.view = view;
2154 blame->cb_args.lines = blame->lines;
2155 blame->cb_args.nlines = blame->nlines;
2156 blame->cb_args.mutex = mutex;
2157 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2158 if (blame->cb_args.commit_id == NULL) {
2159 err = got_error_from_errno();
2162 blame->cb_args.f = blame->f;
2163 blame->cb_args.path = path;
2164 blame->cb_args.first_displayed_line = first_displayed_line;
2165 blame->cb_args.selected_line = selected_line;
2166 blame->cb_args.last_displayed_line = last_displayed_line;
2167 blame->cb_args.quit = done;
2168 blame->cb_args.eof = eof;
2170 blame->thread_args.path = path;
2171 blame->thread_args.repo = thread_repo;
2172 blame->thread_args.cb_args = &blame->cb_args;
2173 blame->thread_args.complete = blame_complete;
2174 *blame_complete = 0;
2176 if (pthread_create(&blame->thread, NULL, blame_thread,
2177 &blame->thread_args) != 0) {
2178 err = got_error_from_errno();
2184 got_object_blob_close(blob);
2186 got_object_close(obj);
2192 static const struct got_error *
2193 open_blame_view(struct tog_view *view, char *path,
2194 struct got_object_id *commit_id, struct got_repository *repo)
2196 const struct got_error *err = NULL;
2197 struct tog_blame_view_state *s = &view->state.blame;
2199 SIMPLEQ_INIT(&s->blamed_commits);
2201 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2202 return got_error_from_errno();
2204 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2208 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2209 s->first_displayed_line = 1;
2210 s->last_displayed_line = view->nlines;
2211 s->selected_line = 1;
2212 s->blame_complete = 0;
2214 if (s->path == NULL)
2215 return got_error_from_errno();
2217 s->commit_id = commit_id;
2218 memset(&s->blame, 0, sizeof(s->blame));
2220 view->show = show_blame_view;
2221 view->input = input_blame_view;
2222 view->close = close_blame_view;
2224 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2225 &s->first_displayed_line, &s->last_displayed_line,
2226 &s->selected_line, &s->done, &s->eof, s->path,
2227 s->blamed_commit->id, s->repo);
2230 static const struct got_error *
2231 close_blame_view(struct tog_view *view)
2233 const struct got_error *err = NULL;
2234 struct tog_blame_view_state *s = &view->state.blame;
2236 if (s->blame.thread)
2237 err = stop_blame(&s->blame);
2239 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2240 struct got_object_qid *blamed_commit;
2241 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2242 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2243 got_object_qid_free(blamed_commit);
2251 static const struct got_error *
2252 show_blame_view(struct tog_view *view)
2254 const struct got_error *err = NULL;
2255 struct tog_blame_view_state *s = &view->state.blame;
2257 if (pthread_mutex_lock(&s->mutex) != 0)
2258 return got_error_from_errno();
2260 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2261 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2262 s->selected_line, &s->first_displayed_line,
2263 &s->last_displayed_line, &s->eof, view->nlines);
2265 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2266 err = got_error_from_errno();
2271 static const struct got_error *
2272 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2273 struct tog_view *view, int ch)
2275 const struct got_error *err = NULL, *thread_err = NULL;
2276 struct got_object *obj = NULL, *pobj = NULL;
2277 struct tog_view *diff_view;
2278 struct tog_blame_view_state *s = &view->state.blame;
2280 if (pthread_mutex_lock(&s->mutex) != 0) {
2281 err = got_error_from_errno();
2288 if (pthread_mutex_unlock(&s->mutex) != 0) {
2289 err = got_error_from_errno();
2292 return stop_blame(&s->blame);
2295 if (s->selected_line > 1)
2297 else if (s->selected_line == 1 &&
2298 s->first_displayed_line > 1)
2299 s->first_displayed_line--;
2302 if (s->first_displayed_line == 1) {
2303 s->selected_line = 1;
2306 if (s->first_displayed_line > view->nlines - 2)
2307 s->first_displayed_line -=
2310 s->first_displayed_line = 1;
2314 if (s->selected_line < view->nlines - 2 &&
2315 s->first_displayed_line +
2316 s->selected_line <= s->blame.nlines)
2318 else if (s->last_displayed_line <
2320 s->first_displayed_line++;
2324 struct got_object_id *id;
2325 id = get_selected_commit_id(s->blame.lines,
2326 s->first_displayed_line, s->selected_line);
2327 if (id == NULL || got_object_id_cmp(id,
2328 s->blamed_commit->id) == 0)
2330 err = open_selected_commit(&pobj, &obj,
2331 s->blame.lines, s->first_displayed_line,
2332 s->selected_line, s->repo);
2335 if (pobj == NULL && obj == NULL)
2337 if (ch == 'p' && pobj == NULL)
2340 if (pthread_mutex_unlock(&s->mutex) != 0) {
2341 err = got_error_from_errno();
2344 thread_err = stop_blame(&s->blame);
2346 if (pthread_mutex_lock(&s->mutex) != 0) {
2347 err = got_error_from_errno();
2352 id = got_object_get_id(ch == 'b' ? obj : pobj);
2353 got_object_close(obj);
2356 got_object_close(pobj);
2360 err = got_error_from_errno();
2363 err = got_object_qid_alloc(
2364 &s->blamed_commit, id);
2368 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2369 s->blamed_commit, entry);
2370 err = run_blame(&s->blame, &s->mutex, view,
2372 &s->first_displayed_line,
2373 &s->last_displayed_line,
2374 &s->selected_line, &s->done, &s->eof,
2375 s->path, s->blamed_commit->id, s->repo);
2381 struct got_object_qid *first;
2382 first = SIMPLEQ_FIRST(&s->blamed_commits);
2383 if (!got_object_id_cmp(first->id, s->commit_id))
2386 if (pthread_mutex_unlock(&s->mutex) != 0) {
2387 err = got_error_from_errno();
2390 thread_err = stop_blame(&s->blame);
2392 if (pthread_mutex_lock(&s->mutex) != 0) {
2393 err = got_error_from_errno();
2398 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2399 got_object_qid_free(s->blamed_commit);
2401 SIMPLEQ_FIRST(&s->blamed_commits);
2402 err = run_blame(&s->blame, &s->mutex, view,
2404 &s->first_displayed_line,
2405 &s->last_displayed_line,
2406 &s->selected_line, &s->done, &s->eof, s->path,
2407 s->blamed_commit->id, s->repo);
2414 err = open_selected_commit(&pobj, &obj,
2415 s->blame.lines, s->first_displayed_line,
2416 s->selected_line, s->repo);
2419 if (pobj == NULL && obj == NULL)
2421 diff_view = view_open(0, 0, 0, 0, view,
2423 if (diff_view == NULL) {
2424 err = got_error_from_errno();
2427 err = open_diff_view(diff_view, pobj, obj,
2430 view_close(diff_view);
2433 *new_view = diff_view;
2435 got_object_close(pobj);
2438 got_object_close(obj);
2445 if (s->last_displayed_line >= s->blame.nlines &&
2446 s->selected_line < view->nlines - 2) {
2447 s->selected_line = MIN(s->blame.nlines,
2451 if (s->last_displayed_line + view->nlines - 2
2453 s->first_displayed_line +=
2456 s->first_displayed_line =
2461 if (s->selected_line > view->nlines - 2) {
2462 s->selected_line = MIN(s->blame.nlines,
2470 if (pthread_mutex_unlock(&s->mutex) != 0)
2471 err = got_error_from_errno();
2474 got_object_close(pobj);
2475 return thread_err ? thread_err : err;
2478 static const struct got_error *
2479 cmd_blame(int argc, char *argv[])
2481 const struct got_error *error;
2482 struct got_repository *repo = NULL;
2483 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2484 struct got_object_id *commit_id = NULL;
2485 char *commit_id_str = NULL;
2487 struct tog_view *view;
2490 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2495 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2498 commit_id_str = optarg;
2501 repo_path = realpath(optarg, NULL);
2502 if (repo_path == NULL)
2503 err(1, "-r option");
2519 cwd = getcwd(NULL, 0);
2521 error = got_error_from_errno();
2524 if (repo_path == NULL) {
2525 repo_path = strdup(cwd);
2526 if (repo_path == NULL) {
2527 error = got_error_from_errno();
2533 error = got_repo_open(&repo, repo_path);
2537 error = got_repo_map_path(&in_repo_path, repo, path);
2541 if (commit_id_str == NULL) {
2542 struct got_reference *head_ref;
2543 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2546 error = got_ref_resolve(&commit_id, repo, head_ref);
2547 got_ref_close(head_ref);
2549 struct got_object *obj;
2550 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2553 commit_id = got_object_get_id(obj);
2554 if (commit_id == NULL)
2555 error = got_error_from_errno();
2556 got_object_close(obj);
2561 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2563 error = got_error_from_errno();
2566 error = open_blame_view(view, in_repo_path, commit_id, repo);
2569 error = view_loop(view);
2575 got_repo_close(repo);
2579 static const struct got_error *
2580 draw_tree_entries(struct tog_view *view,
2581 struct got_tree_entry **first_displayed_entry,
2582 struct got_tree_entry **last_displayed_entry,
2583 struct got_tree_entry **selected_entry, int *ndisplayed,
2584 const char *label, int show_ids, const char *parent_path,
2585 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2587 const struct got_error *err = NULL;
2588 struct got_tree_entry *te;
2594 werase(view->window);
2599 err = format_line(&wline, &width, label, view->ncols);
2602 if (view_needs_focus_indication(view))
2603 wstandout(view->window);
2604 waddwstr(view->window, wline);
2605 if (view_needs_focus_indication(view))
2606 wstandend(view->window);
2609 if (width < view->ncols)
2610 waddch(view->window, '\n');
2613 err = format_line(&wline, &width, parent_path, view->ncols);
2616 waddwstr(view->window, wline);
2619 if (width < view->ncols)
2620 waddch(view->window, '\n');
2623 waddch(view->window, '\n');
2627 te = SIMPLEQ_FIRST(&entries->head);
2628 if (*first_displayed_entry == NULL) {
2629 if (selected == 0) {
2630 wstandout(view->window);
2631 *selected_entry = NULL;
2633 waddstr(view->window, " ..\n"); /* parent directory */
2635 wstandend(view->window);
2642 while (te != *first_displayed_entry)
2643 te = SIMPLEQ_NEXT(te, entry);
2647 char *line = NULL, *id_str = NULL;
2650 err = got_object_id_str(&id_str, te->id);
2652 return got_error_from_errno();
2654 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2655 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2657 return got_error_from_errno();
2660 err = format_line(&wline, &width, line, view->ncols);
2665 if (n == selected) {
2666 wstandout(view->window);
2667 *selected_entry = te;
2669 waddwstr(view->window, wline);
2670 if (width < view->ncols)
2671 waddch(view->window, '\n');
2673 wstandend(view->window);
2679 *last_displayed_entry = te;
2682 te = SIMPLEQ_NEXT(te, entry);
2690 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2691 const struct got_tree_entries *entries, int isroot)
2693 struct got_tree_entry *te, *prev;
2696 if (*first_displayed_entry == NULL)
2699 te = SIMPLEQ_FIRST(&entries->head);
2700 if (*first_displayed_entry == te) {
2702 *first_displayed_entry = NULL;
2706 /* XXX this is stupid... switch to TAILQ? */
2707 for (i = 0; i < maxscroll; i++) {
2708 while (te != *first_displayed_entry) {
2710 te = SIMPLEQ_NEXT(te, entry);
2712 *first_displayed_entry = prev;
2713 te = SIMPLEQ_FIRST(&entries->head);
2715 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2716 *first_displayed_entry = NULL;
2720 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2721 struct got_tree_entry *last_displayed_entry,
2722 const struct got_tree_entries *entries)
2724 struct got_tree_entry *next;
2727 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2730 if (*first_displayed_entry)
2731 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2733 next = SIMPLEQ_FIRST(&entries->head);
2735 *first_displayed_entry = next;
2736 if (++n >= maxscroll)
2738 next = SIMPLEQ_NEXT(next, entry);
2742 static const struct got_error *
2743 tree_entry_path(char **path, struct tog_parent_trees *parents,
2744 struct got_tree_entry *te)
2746 const struct got_error *err = NULL;
2747 struct tog_parent_tree *pt;
2748 size_t len = 2; /* for leading slash and NUL */
2750 TAILQ_FOREACH(pt, parents, entry)
2751 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2753 len += strlen(te->name);
2755 *path = calloc(1, len);
2757 return got_error_from_errno();
2760 pt = TAILQ_LAST(parents, tog_parent_trees);
2762 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2763 err = got_error(GOT_ERR_NO_SPACE);
2766 if (strlcat(*path, "/", len) >= len) {
2767 err = got_error(GOT_ERR_NO_SPACE);
2770 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2773 if (strlcat(*path, te->name, len) >= len) {
2774 err = got_error(GOT_ERR_NO_SPACE);
2786 static const struct got_error *
2787 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2788 struct got_tree_entry *te, struct tog_parent_trees *parents,
2789 struct got_object_id *commit_id, struct got_repository *repo)
2791 const struct got_error *err = NULL;
2793 struct tog_view *blame_view;
2795 err = tree_entry_path(&path, parents, te);
2799 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2800 if (blame_view == NULL)
2801 return got_error_from_errno();
2803 err = open_blame_view(blame_view, path, commit_id, repo);
2805 view_close(blame_view);
2808 *new_view = blame_view;
2812 static const struct got_error *
2813 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2814 struct got_tree_entry *te, struct tog_parent_trees *parents,
2815 struct got_object_id *commit_id, struct got_repository *repo)
2817 struct tog_view *log_view;
2818 const struct got_error *err = NULL;
2821 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2822 if (log_view == NULL)
2823 return got_error_from_errno();
2825 err = tree_entry_path(&path, parents, te);
2829 err = open_log_view(log_view, commit_id, repo, path);
2831 view_close(log_view);
2833 *new_view = log_view;
2838 static const struct got_error *
2839 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2840 struct got_object_id *commit_id, struct got_repository *repo)
2842 const struct got_error *err = NULL;
2843 char *commit_id_str = NULL;
2844 struct tog_tree_view_state *s = &view->state.tree;
2846 TAILQ_INIT(&s->parents);
2848 err = got_object_id_str(&commit_id_str, commit_id);
2852 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2853 err = got_error_from_errno();
2857 s->root = s->tree = root;
2858 s->entries = got_object_tree_get_entries(root);
2859 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2860 s->commit_id = commit_id;
2863 view->show = show_tree_view;
2864 view->input = input_tree_view;
2865 view->close = close_tree_view;
2867 free(commit_id_str);
2869 free(s->tree_label);
2873 static const struct got_error *
2874 close_tree_view(struct tog_view *view)
2876 struct tog_tree_view_state *s = &view->state.tree;
2878 free(s->tree_label);
2879 while (!TAILQ_EMPTY(&s->parents)) {
2880 struct tog_parent_tree *parent;
2881 parent = TAILQ_FIRST(&s->parents);
2882 TAILQ_REMOVE(&s->parents, parent, entry);
2886 if (s->tree != s->root)
2887 got_object_tree_close(s->tree);
2888 got_object_tree_close(s->root);
2893 static const struct got_error *
2894 show_tree_view(struct tog_view *view)
2896 const struct got_error *err = NULL;
2897 struct tog_tree_view_state *s = &view->state.tree;
2900 err = tree_entry_path(&parent_path, &s->parents, NULL);
2904 err = draw_tree_entries(view, &s->first_displayed_entry,
2905 &s->last_displayed_entry, &s->selected_entry,
2906 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2907 s->entries, s->selected, view->nlines, s->tree == s->root);
2912 static const struct got_error *
2913 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2914 struct tog_view *view, int ch)
2916 const struct got_error *err = NULL;
2917 struct tog_tree_view_state *s = &view->state.tree;
2921 s->show_ids = !s->show_ids;
2924 if (s->selected_entry) {
2925 err = log_tree_entry(new_view, view,
2926 s->selected_entry, &s->parents,
2927 s->commit_id, s->repo);
2932 if (s->selected > 0)
2934 if (s->selected > 0)
2936 tree_scroll_up(&s->first_displayed_entry, 1,
2937 s->entries, s->tree == s->root);
2940 if (SIMPLEQ_FIRST(&s->entries->head) ==
2941 s->first_displayed_entry) {
2942 if (s->tree != s->root)
2943 s->first_displayed_entry = NULL;
2947 tree_scroll_up(&s->first_displayed_entry,
2948 view->nlines, s->entries,
2949 s->tree == s->root);
2953 if (s->selected < s->ndisplayed - 1) {
2957 tree_scroll_down(&s->first_displayed_entry, 1,
2958 s->last_displayed_entry, s->entries);
2961 tree_scroll_down(&s->first_displayed_entry,
2962 view->nlines, s->last_displayed_entry,
2964 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2967 /* can't scroll any further; move cursor down */
2968 if (s->selected < s->ndisplayed - 1)
2969 s->selected = s->ndisplayed - 1;
2973 if (s->selected_entry == NULL) {
2974 struct tog_parent_tree *parent;
2976 /* user selected '..' */
2977 if (s->tree == s->root)
2979 parent = TAILQ_FIRST(&s->parents);
2980 TAILQ_REMOVE(&s->parents, parent,
2982 got_object_tree_close(s->tree);
2983 s->tree = parent->tree;
2985 got_object_tree_get_entries(s->tree);
2986 s->first_displayed_entry =
2987 parent->first_displayed_entry;
2989 parent->selected_entry;
2990 s->selected = parent->selected;
2992 } else if (S_ISDIR(s->selected_entry->mode)) {
2993 struct tog_parent_tree *parent;
2994 struct got_tree_object *child;
2995 err = got_object_open_as_tree(&child,
2996 s->repo, s->selected_entry->id);
2999 parent = calloc(1, sizeof(*parent));
3000 if (parent == NULL) {
3001 err = got_error_from_errno();
3004 parent->tree = s->tree;
3005 parent->first_displayed_entry =
3006 s->first_displayed_entry;
3007 parent->selected_entry = s->selected_entry;
3008 parent->selected = s->selected;
3009 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3012 got_object_tree_get_entries(s->tree);
3014 s->first_displayed_entry = NULL;
3015 } else if (S_ISREG(s->selected_entry->mode)) {
3016 err = blame_tree_entry(new_view, view,
3017 s->selected_entry, &s->parents,
3018 s->commit_id, s->repo);
3024 if (s->selected > view->nlines)
3025 s->selected = s->ndisplayed - 1;
3038 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3043 static const struct got_error *
3044 cmd_tree(int argc, char *argv[])
3046 const struct got_error *error;
3047 struct got_repository *repo = NULL;
3048 char *repo_path = NULL;
3049 struct got_object_id *commit_id = NULL;
3050 char *commit_id_arg = NULL;
3051 struct got_commit_object *commit = NULL;
3052 struct got_tree_object *tree = NULL;
3054 struct tog_view *view;
3057 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3062 while ((ch = getopt(argc, argv, "c:")) != -1) {
3065 commit_id_arg = optarg;
3077 repo_path = getcwd(NULL, 0);
3078 if (repo_path == NULL)
3079 return got_error_from_errno();
3080 } else if (argc == 1) {
3081 repo_path = realpath(argv[0], NULL);
3082 if (repo_path == NULL)
3083 return got_error_from_errno();
3087 error = got_repo_open(&repo, repo_path);
3092 if (commit_id_arg == NULL) {
3093 error = get_head_commit_id(&commit_id, repo);
3097 struct got_object *obj;
3098 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3099 if (error == NULL) {
3100 commit_id = got_object_get_id(obj);
3101 if (commit_id == NULL)
3102 error = got_error_from_errno();
3108 error = got_object_open_as_commit(&commit, repo, commit_id);
3112 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3116 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3118 error = got_error_from_errno();
3121 error = open_tree_view(view, tree, commit_id, repo);
3124 error = view_loop(view);
3128 got_object_commit_close(commit);
3130 got_object_tree_close(tree);
3132 got_repo_close(repo);
3142 intrflush(stdscr, FALSE);
3143 keypad(stdscr, TRUE);
3152 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3153 "Available commands:\n", getprogname());
3154 for (i = 0; i < nitems(tog_commands); i++) {
3155 struct tog_cmd *cmd = &tog_commands[i];
3156 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3162 make_argv(const char *arg0, const char *arg1)
3165 int argc = (arg1 == NULL ? 1 : 2);
3167 argv = calloc(argc, sizeof(char *));
3170 argv[0] = strdup(arg0);
3171 if (argv[0] == NULL)
3174 argv[1] = strdup(arg1);
3175 if (argv[1] == NULL)
3183 main(int argc, char *argv[])
3185 const struct got_error *error = NULL;
3186 struct tog_cmd *cmd = NULL;
3188 char **cmd_argv = NULL;
3190 setlocale(LC_ALL, "");
3192 while ((ch = getopt(argc, argv, "h")) != -1) {
3211 /* Build an argument vector which runs a default command. */
3212 cmd = &tog_commands[0];
3213 cmd_argv = make_argv(cmd->name, NULL);
3218 /* Did the user specific a command? */
3219 for (i = 0; i < nitems(tog_commands); i++) {
3220 if (strncmp(tog_commands[i].name, argv[0],
3221 strlen(argv[0])) == 0) {
3222 cmd = &tog_commands[i];
3224 tog_commands[i].cmd_usage();
3229 /* Did the user specify a repository? */
3230 char *repo_path = realpath(argv[0], NULL);
3232 struct got_repository *repo;
3233 error = got_repo_open(&repo, repo_path);
3235 got_repo_close(repo);
3237 error = got_error_from_errno();
3240 fprintf(stderr, "%s: '%s' is not a "
3241 "known command\n", getprogname(),
3245 fprintf(stderr, "%s: '%s' is neither a known "
3246 "command nor a path to a repository\n",
3247 getprogname(), argv[0]);
3251 cmd = &tog_commands[0];
3252 cmd_argv = make_argv(cmd->name, repo_path);
3260 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);