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) {
617 if (wline[i] == L'\t')
618 cols += TABSIZE - ((cols + 1) % TABSIZE);
622 err = got_error_from_errno();
637 static const struct got_error *
638 draw_commit(struct tog_view *view, struct got_commit_object *commit,
639 struct got_object_id *id)
641 const struct got_error *err = NULL;
642 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
643 char *logmsg0 = NULL, *logmsg = NULL;
644 char *author0 = NULL, *author = NULL;
645 wchar_t *wlogmsg = NULL, *wauthor = NULL;
646 int author_width, logmsg_width;
647 char *newline, *smallerthan;
650 static const size_t date_display_cols = 9;
651 static const size_t author_display_cols = 16;
652 const int avail = view->ncols;
654 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
655 &commit->tm_committer) >= sizeof(datebuf))
656 return got_error(GOT_ERR_NO_SPACE);
658 if (avail < date_display_cols)
659 limit = MIN(sizeof(datebuf) - 1, avail);
661 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
662 waddnstr(view->window, datebuf, limit);
667 author0 = strdup(commit->author);
668 if (author0 == NULL) {
669 err = got_error_from_errno();
673 smallerthan = strchr(author, '<');
677 char *at = strchr(author, '@');
682 err = format_line(&wauthor, &author_width, author, limit);
685 waddwstr(view->window, wauthor);
687 while (col <= avail && author_width < author_display_cols + 1) {
688 waddch(view->window, ' ');
695 logmsg0 = strdup(commit->logmsg);
696 if (logmsg0 == NULL) {
697 err = got_error_from_errno();
701 while (*logmsg == '\n')
703 newline = strchr(logmsg, '\n');
707 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
710 waddwstr(view->window, wlogmsg);
712 while (col <= avail) {
713 waddch(view->window, ' ');
725 static struct commit_queue_entry *
726 alloc_commit_queue_entry(struct got_commit_object *commit,
727 struct got_object_id *id)
729 struct commit_queue_entry *entry;
731 entry = calloc(1, sizeof(*entry));
736 entry->commit = commit;
741 pop_commit(struct commit_queue *commits)
743 struct commit_queue_entry *entry;
745 entry = TAILQ_FIRST(&commits->head);
746 TAILQ_REMOVE(&commits->head, entry, entry);
747 got_object_commit_close(entry->commit);
749 /* Don't free entry->id! It is owned by the commit graph. */
754 free_commits(struct commit_queue *commits)
756 while (!TAILQ_EMPTY(&commits->head))
760 static const struct got_error *
761 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
762 struct got_object_id *start_id, int minqueue, int init,
763 struct got_repository *repo, const char *path)
765 const struct got_error *err = NULL;
766 struct got_object_id *id;
767 struct commit_queue_entry *entry;
768 int nfetched, nqueued = 0, found_obj = 0;
769 int is_root_path = strcmp(path, "/") == 0;
771 err = got_commit_graph_iter_start(graph, start_id);
775 entry = TAILQ_LAST(&commits->head, commit_queue_head);
776 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
779 /* Start ID's commit is already on the queue; skip over it. */
780 err = got_commit_graph_iter_next(&id, graph);
781 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
784 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
790 struct got_commit_object *commit;
792 err = got_commit_graph_iter_next(&id, graph);
794 if (err->code != GOT_ERR_ITER_NEED_MORE)
796 if (nqueued >= minqueue) {
800 err = got_commit_graph_fetch_commits(&nfetched,
809 err = got_object_open_as_commit(&commit, repo, id);
814 struct got_object *obj;
815 struct got_object_qid *pid;
818 err = got_object_open_by_path(&obj, repo, id, path);
820 got_object_commit_close(commit);
821 if (err->code == GOT_ERR_NO_OBJ &&
822 (found_obj || !init)) {
823 /* History stops here. */
824 err = got_error(GOT_ERR_ITER_COMPLETED);
830 pid = SIMPLEQ_FIRST(&commit->parent_ids);
832 struct got_object *pobj;
833 err = got_object_open_by_path(&pobj, repo,
836 if (err->code != GOT_ERR_NO_OBJ) {
837 got_object_close(obj);
838 got_object_commit_close(commit);
844 struct got_object_id *id, *pid;
845 id = got_object_get_id(obj);
847 err = got_error_from_errno();
848 got_object_close(obj);
849 got_object_close(pobj);
852 pid = got_object_get_id(pobj);
854 err = got_error_from_errno();
856 got_object_close(obj);
857 got_object_close(pobj);
861 (got_object_id_cmp(id, pid) != 0);
862 got_object_close(pobj);
867 got_object_close(obj);
869 got_object_commit_close(commit);
874 entry = alloc_commit_queue_entry(commit, id);
876 err = got_error_from_errno();
879 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
887 static const struct got_error *
888 fetch_next_commit(struct commit_queue_entry **pentry,
889 struct commit_queue_entry *entry, struct commit_queue *commits,
890 struct got_commit_graph *graph, struct got_repository *repo,
893 const struct got_error *err = NULL;
897 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
901 /* Next entry to display should now be available. */
902 *pentry = TAILQ_NEXT(entry, entry);
904 return got_error(GOT_ERR_NO_OBJ);
909 static const struct got_error *
910 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
912 const struct got_error *err = NULL;
913 struct got_reference *head_ref;
917 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
921 err = got_ref_resolve(head_id, repo, head_ref);
922 got_ref_close(head_ref);
931 static const struct got_error *
932 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
933 struct commit_queue_entry **selected, struct commit_queue_entry *first,
934 struct commit_queue *commits, int selected_idx, int limit,
935 struct got_commit_graph *graph, struct got_repository *repo,
938 const struct got_error *err = NULL;
939 struct commit_queue_entry *entry;
941 char *id_str, *header;
947 if (ncommits == selected_idx) {
951 entry = TAILQ_NEXT(entry, entry);
955 err = got_object_id_str(&id_str, (*selected)->id);
959 if (path && strcmp(path, "/") != 0) {
960 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
961 err = got_error_from_errno();
965 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
966 err = got_error_from_errno();
971 err = format_line(&wline, &width, header, view->ncols);
978 werase(view->window);
980 if (view_needs_focus_indication(view))
981 wstandout(view->window);
982 waddwstr(view->window, wline);
983 if (view_needs_focus_indication(view))
984 wstandend(view->window);
985 if (width < view->ncols)
986 waddch(view->window, '\n');
995 if (ncommits >= limit - 1)
997 if (ncommits == selected_idx)
998 wstandout(view->window);
999 err = draw_commit(view, entry->commit, entry->id);
1000 if (ncommits == selected_idx)
1001 wstandend(view->window);
1006 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1007 err = queue_commits(graph, commits, entry->id, 1,
1010 if (err->code != GOT_ERR_ITER_COMPLETED)
1015 entry = TAILQ_NEXT(entry, entry);
1024 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1025 struct commit_queue *commits)
1027 struct commit_queue_entry *entry;
1030 entry = TAILQ_FIRST(&commits->head);
1031 if (*first_displayed_entry == entry)
1034 entry = *first_displayed_entry;
1035 while (entry && nscrolled < maxscroll) {
1036 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1038 *first_displayed_entry = entry;
1044 static const struct got_error *
1045 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1046 struct commit_queue_entry *last_displayed_entry,
1047 struct commit_queue *commits, struct got_commit_graph *graph,
1048 struct got_repository *repo, const char *path)
1050 const struct got_error *err = NULL;
1051 struct commit_queue_entry *pentry;
1055 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1056 if (pentry == NULL) {
1057 err = fetch_next_commit(&pentry, last_displayed_entry,
1058 commits, graph, repo, path);
1059 if (err || pentry == NULL)
1062 last_displayed_entry = pentry;
1064 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1067 *first_displayed_entry = pentry;
1068 } while (++nscrolled < maxscroll);
1073 static const struct got_error *
1074 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1075 struct commit_queue_entry *entry, struct got_repository *repo)
1077 const struct got_error *err;
1078 struct got_object *obj1 = NULL, *obj2 = NULL;
1079 struct got_object_qid *parent_id;
1080 struct tog_view *diff_view;
1082 err = got_object_open(&obj2, repo, entry->id);
1086 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1088 err = got_object_open(&obj1, repo, parent_id->id);
1093 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1094 if (diff_view == NULL) {
1095 err = got_error_from_errno();
1099 err = open_diff_view(diff_view, obj1, obj2, repo);
1101 *new_view = diff_view;
1104 got_object_close(obj1);
1106 got_object_close(obj2);
1110 static const struct got_error *
1111 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1112 struct commit_queue_entry *entry, struct got_repository *repo)
1114 const struct got_error *err = NULL;
1115 struct got_tree_object *tree;
1116 struct tog_view *tree_view;
1118 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1122 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1123 if (tree_view == NULL)
1124 return got_error_from_errno();
1126 err = open_tree_view(tree_view, tree, entry->id, repo);
1128 got_object_tree_close(tree);
1130 *new_view = tree_view;
1134 static const struct got_error *
1135 set_child_log_view(struct tog_view *view, struct tog_view *child)
1137 struct tog_log_view_state *s = &view->state.log;
1138 struct tog_diff_view_state *ds;
1139 struct commit_queue_entry *commit, *child_entry = NULL;
1140 int selected_idx = 0;
1142 if (child->type != TOG_VIEW_DIFF)
1144 ds = &child->state.diff;
1146 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1147 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1148 child_entry = commit;
1152 if (child_entry == NULL)
1155 commit = s->first_displayed_entry;
1157 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1158 s->selected_entry = child_entry;
1159 s->selected = selected_idx;
1162 if (commit == s->last_displayed_entry)
1165 commit = TAILQ_NEXT(commit, entry);
1168 return show_log_view(view);
1171 static const struct got_error *
1172 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1173 struct got_repository *repo, const char *path)
1175 const struct got_error *err = NULL;
1176 struct got_object_id *head_id = NULL;
1178 struct tog_log_view_state *s = &view->state.log;
1180 err = got_repo_map_path(&s->in_repo_path, repo, path);
1184 err = get_head_commit_id(&head_id, repo);
1188 /* The graph contains all commits. */
1189 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1192 /* The commit queue contains a subset of commits filtered by path. */
1193 TAILQ_INIT(&s->commits.head);
1194 s->commits.ncommits = 0;
1196 /* Populate commit graph with a sufficient number of commits. */
1197 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1203 * Open the initial batch of commits, sorted in commit graph order.
1204 * We keep all commits open throughout the lifetime of the log view
1205 * in order to avoid having to re-fetch commits from disk while
1206 * updating the display.
1208 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1209 repo, s->in_repo_path);
1211 if (err->code != GOT_ERR_ITER_COMPLETED)
1216 s->first_displayed_entry =
1217 TAILQ_FIRST(&s->commits.head);
1218 s->selected_entry = s->first_displayed_entry;
1221 view->show = show_log_view;
1222 view->input = input_log_view;
1223 view->close = close_log_view;
1224 view->set_child = set_child_log_view;
1230 static const struct got_error *
1231 close_log_view(struct tog_view *view)
1233 struct tog_log_view_state *s = &view->state.log;
1236 got_commit_graph_close(s->graph);
1237 free_commits(&s->commits);
1238 free(s->in_repo_path);
1242 static const struct got_error *
1243 update_diff_child_view(struct tog_view *parent,
1244 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1246 const struct got_error *err = NULL;
1247 struct tog_diff_view_state *ds;
1248 struct got_object *obj1 = NULL, *obj2 = NULL;
1249 struct got_object_qid *parent_id;
1250 struct tog_view *child_view = parent->child;
1252 if (child_view == NULL)
1254 if (child_view->type != TOG_VIEW_DIFF)
1256 ds = &child_view->state.diff;
1257 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1260 err = got_object_open(&obj2, repo, selected_entry->id);
1264 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1266 err = got_object_open(&obj1, repo, parent_id->id);
1271 err = close_diff_view(child_view);
1275 err = open_diff_view(child_view, obj1, obj2, repo);
1280 got_object_close(obj1);
1282 got_object_close(obj2);
1286 static const struct got_error *
1287 show_log_view(struct tog_view *view)
1289 const struct got_error *err = NULL;
1290 struct tog_log_view_state *s = &view->state.log;
1292 err = draw_commits(view, &s->last_displayed_entry,
1293 &s->selected_entry, s->first_displayed_entry,
1294 &s->commits, s->selected, view->nlines, s->graph,
1295 s->repo, s->in_repo_path);
1299 return update_diff_child_view(view, s->selected_entry, s->repo);
1302 static const struct got_error *
1303 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1304 struct tog_view *view, int ch)
1306 const struct got_error *err = NULL;
1307 struct tog_log_view_state *s = &view->state.log;
1313 if (s->selected > 0)
1315 if (s->selected > 0)
1317 scroll_up(&s->first_displayed_entry, 1,
1321 if (TAILQ_FIRST(&s->commits.head) ==
1322 s->first_displayed_entry) {
1326 scroll_up(&s->first_displayed_entry,
1327 view->nlines, &s->commits);
1332 if (s->selected < MIN(view->nlines - 2,
1333 s->commits.ncommits - 1)) {
1337 err = scroll_down(&s->first_displayed_entry, 1,
1338 s->last_displayed_entry, &s->commits,
1339 s->graph, s->repo, s->in_repo_path);
1341 if (err->code != GOT_ERR_ITER_COMPLETED)
1347 struct commit_queue_entry *first;
1348 first = s->first_displayed_entry;
1349 err = scroll_down(&s->first_displayed_entry,
1350 view->nlines, s->last_displayed_entry,
1351 &s->commits, s->graph, s->repo,
1355 if (err->code != GOT_ERR_ITER_COMPLETED)
1357 if (first == s->first_displayed_entry &&
1358 s->selected < MIN(view->nlines - 2,
1359 s->commits.ncommits - 1)) {
1360 /* can't scroll further down */
1361 s->selected = MIN(view->nlines - 2,
1362 s->commits.ncommits - 1);
1368 if (s->selected > view->nlines - 2)
1369 s->selected = view->nlines - 2;
1370 if (s->selected > s->commits.ncommits - 1)
1371 s->selected = s->commits.ncommits - 1;
1375 err = show_commit(new_view, view, s->selected_entry,
1379 err = browse_commit(new_view, view, s->selected_entry,
1389 static const struct got_error *
1390 cmd_log(int argc, char *argv[])
1392 const struct got_error *error;
1393 struct got_repository *repo = NULL;
1394 struct got_object_id *start_id = NULL;
1395 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1396 char *start_commit = NULL;
1398 struct tog_view *view;
1401 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
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, repo, f);
1595 case GOT_OBJ_TYPE_TREE:
1596 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1598 case GOT_OBJ_TYPE_COMMIT:
1599 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1602 return got_error(GOT_ERR_OBJ_TYPE);
1607 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1608 view->state.diff.id2 = got_object_get_id(obj2);
1609 if (view->state.diff.id2 == NULL)
1610 return got_error_from_errno();
1611 view->state.diff.f = f;
1612 view->state.diff.first_displayed_line = 1;
1613 view->state.diff.last_displayed_line = view->nlines;
1615 view->show = show_diff_view;
1616 view->input = input_diff_view;
1617 view->close = close_diff_view;
1622 static const struct got_error *
1623 close_diff_view(struct tog_view *view)
1625 const struct got_error *err = NULL;
1627 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1628 err = got_error_from_errno();
1629 free(view->state.diff.id1);
1630 free(view->state.diff.id2);
1634 static const struct got_error *
1635 show_diff_view(struct tog_view *view)
1637 const struct got_error *err;
1638 struct tog_diff_view_state *s = &view->state.diff;
1639 char *id_str1 = NULL, *id_str2, *header;
1642 err = got_object_id_str(&id_str1, s->id1);
1646 err = got_object_id_str(&id_str2, s->id2);
1650 if (asprintf(&header, "diff: %s %s",
1651 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1652 err = got_error_from_errno();
1660 return draw_file(view, s->f, &s->first_displayed_line,
1661 &s->last_displayed_line, &s->eof, view->nlines,
1665 static const struct got_error *
1666 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1667 struct tog_view *view, int ch)
1669 const struct got_error *err = NULL;
1670 struct tog_diff_view_state *s = &view->state.diff;
1676 if (s->first_displayed_line > 1)
1677 s->first_displayed_line--;
1681 while (i++ < view->nlines - 1 &&
1682 s->first_displayed_line > 1)
1683 s->first_displayed_line--;
1688 s->first_displayed_line++;
1693 while (!s->eof && i++ < view->nlines - 1) {
1695 line = parse_next_line(s->f, NULL);
1696 s->first_displayed_line++;
1703 struct tog_log_view_state *ls;
1704 struct commit_queue_entry *entry;
1705 struct tog_view *diff_view;
1707 if (view->parent == NULL)
1709 if (view->parent->type != TOG_VIEW_LOG)
1711 ls = &view->parent->state.log;
1714 entry = TAILQ_PREV(ls->selected_entry,
1715 commit_queue_head, entry);
1717 entry = TAILQ_NEXT(ls->selected_entry, entry);
1718 if (entry == NULL) {
1719 err = fetch_next_commit(&entry,
1721 &ls->commits, ls->graph,
1722 ls->repo, ls->in_repo_path);
1729 err = show_commit(&diff_view, view->parent,
1733 *new_view = diff_view;
1744 static const struct got_error *
1745 cmd_diff(int argc, char *argv[])
1747 const struct got_error *error = NULL;
1748 struct got_repository *repo = NULL;
1749 struct got_object *obj1 = NULL, *obj2 = NULL;
1750 char *repo_path = NULL;
1751 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1753 struct tog_view *view;
1756 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1760 while ((ch = getopt(argc, argv, "")) != -1) {
1772 usage_diff(); /* TODO show local worktree changes */
1773 } else if (argc == 2) {
1774 repo_path = getcwd(NULL, 0);
1775 if (repo_path == NULL)
1776 return got_error_from_errno();
1777 obj_id_str1 = argv[0];
1778 obj_id_str2 = argv[1];
1779 } else if (argc == 3) {
1780 repo_path = realpath(argv[0], NULL);
1781 if (repo_path == NULL)
1782 return got_error_from_errno();
1783 obj_id_str1 = argv[1];
1784 obj_id_str2 = argv[2];
1788 error = got_repo_open(&repo, repo_path);
1793 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1797 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1801 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1803 error = got_error_from_errno();
1806 error = open_diff_view(view, obj1, obj2, repo);
1809 error = view_loop(view);
1811 got_repo_close(repo);
1813 got_object_close(obj1);
1815 got_object_close(obj2);
1823 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1828 struct tog_blame_line {
1830 struct got_object_id *id;
1833 static const struct got_error *
1834 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1835 const char *path, struct tog_blame_line *lines, int nlines,
1836 int blame_complete, int selected_line, int *first_displayed_line,
1837 int *last_displayed_line, int *eof, int max_lines)
1839 const struct got_error *err;
1840 int lineno = 0, nprinted = 0;
1845 struct tog_blame_line *blame_line;
1846 struct got_object_id *prev_id = NULL;
1849 err = got_object_id_str(&id_str, id);
1854 werase(view->window);
1856 if (asprintf(&line, "commit: %s", id_str) == -1) {
1857 err = got_error_from_errno();
1862 err = format_line(&wline, &width, line, view->ncols);
1865 if (view_needs_focus_indication(view))
1866 wstandout(view->window);
1867 waddwstr(view->window, wline);
1868 if (view_needs_focus_indication(view))
1869 wstandend(view->window);
1872 if (width < view->ncols)
1873 waddch(view->window, '\n');
1875 if (asprintf(&line, "[%d/%d] %s%s",
1876 *first_displayed_line - 1 + selected_line, nlines,
1877 blame_complete ? "" : "annotating ", path) == -1) {
1879 return got_error_from_errno();
1882 err = format_line(&wline, &width, line, view->ncols);
1887 waddwstr(view->window, wline);
1890 if (width < view->ncols)
1891 waddch(view->window, '\n');
1894 while (nprinted < max_lines - 2) {
1895 line = parse_next_line(f, &len);
1900 if (++lineno < *first_displayed_line) {
1905 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1906 err = format_line(&wline, &width, line, wlimit);
1912 if (nprinted == selected_line - 1)
1913 wstandout(view->window);
1915 blame_line = &lines[lineno - 1];
1916 if (blame_line->annotated && prev_id &&
1917 got_object_id_cmp(prev_id, blame_line->id) == 0)
1918 waddstr(view->window, " ");
1919 else if (blame_line->annotated) {
1921 err = got_object_id_str(&id_str, blame_line->id);
1927 wprintw(view->window, "%.8s ", id_str);
1929 prev_id = blame_line->id;
1931 waddstr(view->window, "........ ");
1935 waddwstr(view->window, wline);
1936 while (width < wlimit) {
1937 waddch(view->window, ' ');
1940 if (nprinted == selected_line - 1)
1941 wstandend(view->window);
1942 if (++nprinted == 1)
1943 *first_displayed_line = lineno;
1948 *last_displayed_line = lineno;
1955 static const struct got_error *
1956 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1958 const struct got_error *err = NULL;
1959 struct tog_blame_cb_args *a = arg;
1960 struct tog_blame_line *line;
1962 if (nlines != a->nlines ||
1963 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1964 return got_error(GOT_ERR_RANGE);
1966 if (pthread_mutex_lock(a->mutex) != 0)
1967 return got_error_from_errno();
1969 if (*a->quit) { /* user has quit the blame view */
1970 err = got_error(GOT_ERR_ITER_COMPLETED);
1975 goto done; /* no change in this commit */
1977 line = &a->lines[lineno - 1];
1978 if (line->annotated)
1981 line->id = got_object_id_dup(id);
1982 if (line->id == NULL) {
1983 err = got_error_from_errno();
1986 line->annotated = 1;
1988 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1989 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1990 a->last_displayed_line, a->eof, a->view->nlines);
1992 if (pthread_mutex_unlock(a->mutex) != 0)
1993 return got_error_from_errno();
1998 blame_thread(void *arg)
2000 const struct got_error *err;
2001 struct tog_blame_thread_args *ta = arg;
2002 struct tog_blame_cb_args *a = ta->cb_args;
2004 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2005 blame_cb, ta->cb_args);
2007 if (pthread_mutex_lock(a->mutex) != 0)
2008 return (void *)got_error_from_errno();
2010 got_repo_close(ta->repo);
2014 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2015 a->lines, a->nlines, 1, *a->selected_line,
2016 a->first_displayed_line, a->last_displayed_line, a->eof,
2019 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2020 err = got_error_from_errno();
2025 static struct got_object_id *
2026 get_selected_commit_id(struct tog_blame_line *lines,
2027 int first_displayed_line, int selected_line)
2029 struct tog_blame_line *line;
2031 line = &lines[first_displayed_line - 1 + selected_line - 1];
2032 if (!line->annotated)
2038 static const struct got_error *
2039 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2040 struct tog_blame_line *lines, int first_displayed_line,
2041 int selected_line, struct got_repository *repo)
2043 const struct got_error *err = NULL;
2044 struct got_commit_object *commit = NULL;
2045 struct got_object_id *selected_id;
2046 struct got_object_qid *pid;
2051 selected_id = get_selected_commit_id(lines,
2052 first_displayed_line, selected_line);
2053 if (selected_id == NULL)
2056 err = got_object_open(obj, repo, selected_id);
2060 err = got_object_commit_open(&commit, repo, *obj);
2064 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2066 err = got_object_open(pobj, repo, pid->id);
2072 got_object_commit_close(commit);
2076 static const struct got_error *
2077 stop_blame(struct tog_blame *blame)
2079 const struct got_error *err = NULL;
2082 if (blame->thread) {
2083 if (pthread_join(blame->thread, (void **)&err) != 0)
2084 err = got_error_from_errno();
2085 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2087 blame->thread = NULL;
2089 if (blame->thread_args.repo) {
2090 got_repo_close(blame->thread_args.repo);
2091 blame->thread_args.repo = NULL;
2097 for (i = 0; i < blame->nlines; i++)
2098 free(blame->lines[i].id);
2100 blame->lines = NULL;
2101 free(blame->cb_args.commit_id);
2102 blame->cb_args.commit_id = NULL;
2107 static const struct got_error *
2108 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2109 struct tog_view *view, int *blame_complete,
2110 int *first_displayed_line, int *last_displayed_line,
2111 int *selected_line, int *done, int *eof, const char *path,
2112 struct got_object_id *commit_id,
2113 struct got_repository *repo)
2115 const struct got_error *err = NULL;
2116 struct got_blob_object *blob = NULL;
2117 struct got_repository *thread_repo = NULL;
2118 struct got_object *obj;
2120 err = got_object_open_by_path(&obj, repo, commit_id, path);
2123 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2124 err = got_error(GOT_ERR_OBJ_TYPE);
2128 err = got_object_blob_open(&blob, repo, obj, 8192);
2131 blame->f = got_opentemp();
2132 if (blame->f == NULL) {
2133 err = got_error_from_errno();
2136 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2141 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2142 if (blame->lines == NULL) {
2143 err = got_error_from_errno();
2147 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2151 blame->cb_args.view = view;
2152 blame->cb_args.lines = blame->lines;
2153 blame->cb_args.nlines = blame->nlines;
2154 blame->cb_args.mutex = mutex;
2155 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2156 if (blame->cb_args.commit_id == NULL) {
2157 err = got_error_from_errno();
2160 blame->cb_args.f = blame->f;
2161 blame->cb_args.path = path;
2162 blame->cb_args.first_displayed_line = first_displayed_line;
2163 blame->cb_args.selected_line = selected_line;
2164 blame->cb_args.last_displayed_line = last_displayed_line;
2165 blame->cb_args.quit = done;
2166 blame->cb_args.eof = eof;
2168 blame->thread_args.path = path;
2169 blame->thread_args.repo = thread_repo;
2170 blame->thread_args.cb_args = &blame->cb_args;
2171 blame->thread_args.complete = blame_complete;
2172 *blame_complete = 0;
2174 if (pthread_create(&blame->thread, NULL, blame_thread,
2175 &blame->thread_args) != 0) {
2176 err = got_error_from_errno();
2182 got_object_blob_close(blob);
2184 got_object_close(obj);
2190 static const struct got_error *
2191 open_blame_view(struct tog_view *view, char *path,
2192 struct got_object_id *commit_id, struct got_repository *repo)
2194 const struct got_error *err = NULL;
2195 struct tog_blame_view_state *s = &view->state.blame;
2197 SIMPLEQ_INIT(&s->blamed_commits);
2199 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2200 return got_error_from_errno();
2202 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2206 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2207 s->first_displayed_line = 1;
2208 s->last_displayed_line = view->nlines;
2209 s->selected_line = 1;
2210 s->blame_complete = 0;
2212 if (s->path == NULL)
2213 return got_error_from_errno();
2215 s->commit_id = commit_id;
2216 memset(&s->blame, 0, sizeof(s->blame));
2218 view->show = show_blame_view;
2219 view->input = input_blame_view;
2220 view->close = close_blame_view;
2222 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2223 &s->first_displayed_line, &s->last_displayed_line,
2224 &s->selected_line, &s->done, &s->eof, s->path,
2225 s->blamed_commit->id, s->repo);
2228 static const struct got_error *
2229 close_blame_view(struct tog_view *view)
2231 const struct got_error *err = NULL;
2232 struct tog_blame_view_state *s = &view->state.blame;
2234 if (s->blame.thread)
2235 err = stop_blame(&s->blame);
2237 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2238 struct got_object_qid *blamed_commit;
2239 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2240 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2241 got_object_qid_free(blamed_commit);
2249 static const struct got_error *
2250 show_blame_view(struct tog_view *view)
2252 const struct got_error *err = NULL;
2253 struct tog_blame_view_state *s = &view->state.blame;
2255 if (pthread_mutex_lock(&s->mutex) != 0)
2256 return got_error_from_errno();
2258 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2259 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2260 s->selected_line, &s->first_displayed_line,
2261 &s->last_displayed_line, &s->eof, view->nlines);
2263 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2264 err = got_error_from_errno();
2269 static const struct got_error *
2270 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2271 struct tog_view *view, int ch)
2273 const struct got_error *err = NULL, *thread_err = NULL;
2274 struct got_object *obj = NULL, *pobj = NULL;
2275 struct tog_view *diff_view;
2276 struct tog_blame_view_state *s = &view->state.blame;
2278 if (pthread_mutex_lock(&s->mutex) != 0) {
2279 err = got_error_from_errno();
2286 if (pthread_mutex_unlock(&s->mutex) != 0) {
2287 err = got_error_from_errno();
2290 return stop_blame(&s->blame);
2293 if (s->selected_line > 1)
2295 else if (s->selected_line == 1 &&
2296 s->first_displayed_line > 1)
2297 s->first_displayed_line--;
2300 if (s->first_displayed_line == 1) {
2301 s->selected_line = 1;
2304 if (s->first_displayed_line > view->nlines - 2)
2305 s->first_displayed_line -=
2308 s->first_displayed_line = 1;
2312 if (s->selected_line < view->nlines - 2 &&
2313 s->first_displayed_line +
2314 s->selected_line <= s->blame.nlines)
2316 else if (s->last_displayed_line <
2318 s->first_displayed_line++;
2322 struct got_object_id *id;
2323 id = get_selected_commit_id(s->blame.lines,
2324 s->first_displayed_line, s->selected_line);
2325 if (id == NULL || got_object_id_cmp(id,
2326 s->blamed_commit->id) == 0)
2328 err = open_selected_commit(&pobj, &obj,
2329 s->blame.lines, s->first_displayed_line,
2330 s->selected_line, s->repo);
2333 if (pobj == NULL && obj == NULL)
2335 if (ch == 'p' && pobj == NULL)
2338 if (pthread_mutex_unlock(&s->mutex) != 0) {
2339 err = got_error_from_errno();
2342 thread_err = stop_blame(&s->blame);
2344 if (pthread_mutex_lock(&s->mutex) != 0) {
2345 err = got_error_from_errno();
2350 id = got_object_get_id(ch == 'b' ? obj : pobj);
2351 got_object_close(obj);
2354 got_object_close(pobj);
2358 err = got_error_from_errno();
2361 err = got_object_qid_alloc(
2362 &s->blamed_commit, id);
2366 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2367 s->blamed_commit, entry);
2368 err = run_blame(&s->blame, &s->mutex, view,
2370 &s->first_displayed_line,
2371 &s->last_displayed_line,
2372 &s->selected_line, &s->done, &s->eof,
2373 s->path, s->blamed_commit->id, s->repo);
2379 struct got_object_qid *first;
2380 first = SIMPLEQ_FIRST(&s->blamed_commits);
2381 if (!got_object_id_cmp(first->id, s->commit_id))
2384 if (pthread_mutex_unlock(&s->mutex) != 0) {
2385 err = got_error_from_errno();
2388 thread_err = stop_blame(&s->blame);
2390 if (pthread_mutex_lock(&s->mutex) != 0) {
2391 err = got_error_from_errno();
2396 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2397 got_object_qid_free(s->blamed_commit);
2399 SIMPLEQ_FIRST(&s->blamed_commits);
2400 err = run_blame(&s->blame, &s->mutex, view,
2402 &s->first_displayed_line,
2403 &s->last_displayed_line,
2404 &s->selected_line, &s->done, &s->eof, s->path,
2405 s->blamed_commit->id, s->repo);
2412 err = open_selected_commit(&pobj, &obj,
2413 s->blame.lines, s->first_displayed_line,
2414 s->selected_line, s->repo);
2417 if (pobj == NULL && obj == NULL)
2419 diff_view = view_open(0, 0, 0, 0, view,
2421 if (diff_view == NULL) {
2422 err = got_error_from_errno();
2425 err = open_diff_view(diff_view, pobj, obj,
2428 view_close(diff_view);
2431 *new_view = diff_view;
2433 got_object_close(pobj);
2436 got_object_close(obj);
2443 if (s->last_displayed_line >= s->blame.nlines &&
2444 s->selected_line < view->nlines - 2) {
2445 s->selected_line = MIN(s->blame.nlines,
2449 if (s->last_displayed_line + view->nlines - 2
2451 s->first_displayed_line +=
2454 s->first_displayed_line =
2459 if (s->selected_line > view->nlines - 2) {
2460 s->selected_line = MIN(s->blame.nlines,
2468 if (pthread_mutex_unlock(&s->mutex) != 0)
2469 err = got_error_from_errno();
2472 got_object_close(pobj);
2473 return thread_err ? thread_err : err;
2476 static const struct got_error *
2477 cmd_blame(int argc, char *argv[])
2479 const struct got_error *error;
2480 struct got_repository *repo = NULL;
2481 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2482 struct got_object_id *commit_id = NULL;
2483 char *commit_id_str = NULL;
2485 struct tog_view *view;
2488 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2492 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2495 commit_id_str = optarg;
2498 repo_path = realpath(optarg, NULL);
2499 if (repo_path == NULL)
2500 err(1, "-r option");
2516 cwd = getcwd(NULL, 0);
2518 error = got_error_from_errno();
2521 if (repo_path == NULL) {
2522 repo_path = strdup(cwd);
2523 if (repo_path == NULL) {
2524 error = got_error_from_errno();
2530 error = got_repo_open(&repo, repo_path);
2534 error = got_repo_map_path(&in_repo_path, repo, path);
2538 if (commit_id_str == NULL) {
2539 struct got_reference *head_ref;
2540 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2543 error = got_ref_resolve(&commit_id, repo, head_ref);
2544 got_ref_close(head_ref);
2546 struct got_object *obj;
2547 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2550 commit_id = got_object_get_id(obj);
2551 if (commit_id == NULL)
2552 error = got_error_from_errno();
2553 got_object_close(obj);
2558 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2560 error = got_error_from_errno();
2563 error = open_blame_view(view, in_repo_path, commit_id, repo);
2566 error = view_loop(view);
2572 got_repo_close(repo);
2576 static const struct got_error *
2577 draw_tree_entries(struct tog_view *view,
2578 struct got_tree_entry **first_displayed_entry,
2579 struct got_tree_entry **last_displayed_entry,
2580 struct got_tree_entry **selected_entry, int *ndisplayed,
2581 const char *label, int show_ids, const char *parent_path,
2582 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2584 const struct got_error *err = NULL;
2585 struct got_tree_entry *te;
2591 werase(view->window);
2596 err = format_line(&wline, &width, label, view->ncols);
2599 if (view_needs_focus_indication(view))
2600 wstandout(view->window);
2601 waddwstr(view->window, wline);
2602 if (view_needs_focus_indication(view))
2603 wstandend(view->window);
2606 if (width < view->ncols)
2607 waddch(view->window, '\n');
2610 err = format_line(&wline, &width, parent_path, view->ncols);
2613 waddwstr(view->window, wline);
2616 if (width < view->ncols)
2617 waddch(view->window, '\n');
2620 waddch(view->window, '\n');
2624 te = SIMPLEQ_FIRST(&entries->head);
2625 if (*first_displayed_entry == NULL) {
2626 if (selected == 0) {
2627 wstandout(view->window);
2628 *selected_entry = NULL;
2630 waddstr(view->window, " ..\n"); /* parent directory */
2632 wstandend(view->window);
2639 while (te != *first_displayed_entry)
2640 te = SIMPLEQ_NEXT(te, entry);
2644 char *line = NULL, *id_str = NULL;
2647 err = got_object_id_str(&id_str, te->id);
2649 return got_error_from_errno();
2651 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2652 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2654 return got_error_from_errno();
2657 err = format_line(&wline, &width, line, view->ncols);
2662 if (n == selected) {
2663 wstandout(view->window);
2664 *selected_entry = te;
2666 waddwstr(view->window, wline);
2667 if (width < view->ncols)
2668 waddch(view->window, '\n');
2670 wstandend(view->window);
2676 *last_displayed_entry = te;
2679 te = SIMPLEQ_NEXT(te, entry);
2687 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2688 const struct got_tree_entries *entries, int isroot)
2690 struct got_tree_entry *te, *prev;
2693 if (*first_displayed_entry == NULL)
2696 te = SIMPLEQ_FIRST(&entries->head);
2697 if (*first_displayed_entry == te) {
2699 *first_displayed_entry = NULL;
2703 /* XXX this is stupid... switch to TAILQ? */
2704 for (i = 0; i < maxscroll; i++) {
2705 while (te != *first_displayed_entry) {
2707 te = SIMPLEQ_NEXT(te, entry);
2709 *first_displayed_entry = prev;
2710 te = SIMPLEQ_FIRST(&entries->head);
2712 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2713 *first_displayed_entry = NULL;
2717 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2718 struct got_tree_entry *last_displayed_entry,
2719 const struct got_tree_entries *entries)
2721 struct got_tree_entry *next;
2724 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2727 if (*first_displayed_entry)
2728 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2730 next = SIMPLEQ_FIRST(&entries->head);
2732 *first_displayed_entry = next;
2733 if (++n >= maxscroll)
2735 next = SIMPLEQ_NEXT(next, entry);
2739 static const struct got_error *
2740 tree_entry_path(char **path, struct tog_parent_trees *parents,
2741 struct got_tree_entry *te)
2743 const struct got_error *err = NULL;
2744 struct tog_parent_tree *pt;
2745 size_t len = 2; /* for leading slash and NUL */
2747 TAILQ_FOREACH(pt, parents, entry)
2748 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2750 len += strlen(te->name);
2752 *path = calloc(1, len);
2754 return got_error_from_errno();
2757 pt = TAILQ_LAST(parents, tog_parent_trees);
2759 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2760 err = got_error(GOT_ERR_NO_SPACE);
2763 if (strlcat(*path, "/", len) >= len) {
2764 err = got_error(GOT_ERR_NO_SPACE);
2767 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2770 if (strlcat(*path, te->name, len) >= len) {
2771 err = got_error(GOT_ERR_NO_SPACE);
2783 static const struct got_error *
2784 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2785 struct got_tree_entry *te, struct tog_parent_trees *parents,
2786 struct got_object_id *commit_id, struct got_repository *repo)
2788 const struct got_error *err = NULL;
2790 struct tog_view *blame_view;
2792 err = tree_entry_path(&path, parents, te);
2796 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2797 if (blame_view == NULL)
2798 return got_error_from_errno();
2800 err = open_blame_view(blame_view, path, commit_id, repo);
2802 view_close(blame_view);
2805 *new_view = blame_view;
2809 static const struct got_error *
2810 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2811 struct got_tree_entry *te, struct tog_parent_trees *parents,
2812 struct got_object_id *commit_id, struct got_repository *repo)
2814 struct tog_view *log_view;
2815 const struct got_error *err = NULL;
2818 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2819 if (log_view == NULL)
2820 return got_error_from_errno();
2822 err = tree_entry_path(&path, parents, te);
2826 err = open_log_view(log_view, commit_id, repo, path);
2828 view_close(log_view);
2830 *new_view = log_view;
2835 static const struct got_error *
2836 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2837 struct got_object_id *commit_id, struct got_repository *repo)
2839 const struct got_error *err = NULL;
2840 char *commit_id_str = NULL;
2841 struct tog_tree_view_state *s = &view->state.tree;
2843 TAILQ_INIT(&s->parents);
2845 err = got_object_id_str(&commit_id_str, commit_id);
2849 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2850 err = got_error_from_errno();
2854 s->root = s->tree = root;
2855 s->entries = got_object_tree_get_entries(root);
2856 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2857 s->commit_id = commit_id;
2860 view->show = show_tree_view;
2861 view->input = input_tree_view;
2862 view->close = close_tree_view;
2864 free(commit_id_str);
2866 free(s->tree_label);
2870 static const struct got_error *
2871 close_tree_view(struct tog_view *view)
2873 struct tog_tree_view_state *s = &view->state.tree;
2875 free(s->tree_label);
2876 while (!TAILQ_EMPTY(&s->parents)) {
2877 struct tog_parent_tree *parent;
2878 parent = TAILQ_FIRST(&s->parents);
2879 TAILQ_REMOVE(&s->parents, parent, entry);
2883 if (s->tree != s->root)
2884 got_object_tree_close(s->tree);
2885 got_object_tree_close(s->root);
2890 static const struct got_error *
2891 show_tree_view(struct tog_view *view)
2893 const struct got_error *err = NULL;
2894 struct tog_tree_view_state *s = &view->state.tree;
2897 err = tree_entry_path(&parent_path, &s->parents, NULL);
2901 err = draw_tree_entries(view, &s->first_displayed_entry,
2902 &s->last_displayed_entry, &s->selected_entry,
2903 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2904 s->entries, s->selected, view->nlines, s->tree == s->root);
2909 static const struct got_error *
2910 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2911 struct tog_view *view, int ch)
2913 const struct got_error *err = NULL;
2914 struct tog_tree_view_state *s = &view->state.tree;
2918 s->show_ids = !s->show_ids;
2921 if (s->selected_entry) {
2922 err = log_tree_entry(new_view, view,
2923 s->selected_entry, &s->parents,
2924 s->commit_id, s->repo);
2929 if (s->selected > 0)
2931 if (s->selected > 0)
2933 tree_scroll_up(&s->first_displayed_entry, 1,
2934 s->entries, s->tree == s->root);
2937 if (SIMPLEQ_FIRST(&s->entries->head) ==
2938 s->first_displayed_entry) {
2939 if (s->tree != s->root)
2940 s->first_displayed_entry = NULL;
2944 tree_scroll_up(&s->first_displayed_entry,
2945 view->nlines, s->entries,
2946 s->tree == s->root);
2950 if (s->selected < s->ndisplayed - 1) {
2954 tree_scroll_down(&s->first_displayed_entry, 1,
2955 s->last_displayed_entry, s->entries);
2958 tree_scroll_down(&s->first_displayed_entry,
2959 view->nlines, s->last_displayed_entry,
2961 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2964 /* can't scroll any further; move cursor down */
2965 if (s->selected < s->ndisplayed - 1)
2966 s->selected = s->ndisplayed - 1;
2970 if (s->selected_entry == NULL) {
2971 struct tog_parent_tree *parent;
2973 /* user selected '..' */
2974 if (s->tree == s->root)
2976 parent = TAILQ_FIRST(&s->parents);
2977 TAILQ_REMOVE(&s->parents, parent,
2979 got_object_tree_close(s->tree);
2980 s->tree = parent->tree;
2982 got_object_tree_get_entries(s->tree);
2983 s->first_displayed_entry =
2984 parent->first_displayed_entry;
2986 parent->selected_entry;
2987 s->selected = parent->selected;
2989 } else if (S_ISDIR(s->selected_entry->mode)) {
2990 struct tog_parent_tree *parent;
2991 struct got_tree_object *child;
2992 err = got_object_open_as_tree(&child,
2993 s->repo, s->selected_entry->id);
2996 parent = calloc(1, sizeof(*parent));
2997 if (parent == NULL) {
2998 err = got_error_from_errno();
3001 parent->tree = s->tree;
3002 parent->first_displayed_entry =
3003 s->first_displayed_entry;
3004 parent->selected_entry = s->selected_entry;
3005 parent->selected = s->selected;
3006 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3009 got_object_tree_get_entries(s->tree);
3011 s->first_displayed_entry = NULL;
3012 } else if (S_ISREG(s->selected_entry->mode)) {
3013 err = blame_tree_entry(new_view, view,
3014 s->selected_entry, &s->parents,
3015 s->commit_id, s->repo);
3021 if (s->selected > view->nlines)
3022 s->selected = s->ndisplayed - 1;
3035 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3040 static const struct got_error *
3041 cmd_tree(int argc, char *argv[])
3043 const struct got_error *error;
3044 struct got_repository *repo = NULL;
3045 char *repo_path = NULL;
3046 struct got_object_id *commit_id = NULL;
3047 char *commit_id_arg = NULL;
3048 struct got_commit_object *commit = NULL;
3049 struct got_tree_object *tree = NULL;
3051 struct tog_view *view;
3054 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
3058 while ((ch = getopt(argc, argv, "c:")) != -1) {
3061 commit_id_arg = optarg;
3073 repo_path = getcwd(NULL, 0);
3074 if (repo_path == NULL)
3075 return got_error_from_errno();
3076 } else if (argc == 1) {
3077 repo_path = realpath(argv[0], NULL);
3078 if (repo_path == NULL)
3079 return got_error_from_errno();
3083 error = got_repo_open(&repo, repo_path);
3088 if (commit_id_arg == NULL) {
3089 error = get_head_commit_id(&commit_id, repo);
3093 struct got_object *obj;
3094 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3095 if (error == NULL) {
3096 commit_id = got_object_get_id(obj);
3097 if (commit_id == NULL)
3098 error = got_error_from_errno();
3104 error = got_object_open_as_commit(&commit, repo, commit_id);
3108 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3112 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3114 error = got_error_from_errno();
3117 error = open_tree_view(view, tree, commit_id, repo);
3120 error = view_loop(view);
3124 got_object_commit_close(commit);
3126 got_object_tree_close(tree);
3128 got_repo_close(repo);
3138 intrflush(stdscr, FALSE);
3139 keypad(stdscr, TRUE);
3148 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3149 "Available commands:\n", getprogname());
3150 for (i = 0; i < nitems(tog_commands); i++) {
3151 struct tog_cmd *cmd = &tog_commands[i];
3152 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3158 make_argv(const char *arg0, const char *arg1)
3161 int argc = (arg1 == NULL ? 1 : 2);
3163 argv = calloc(argc, sizeof(char *));
3166 argv[0] = strdup(arg0);
3167 if (argv[0] == NULL)
3170 argv[1] = strdup(arg1);
3171 if (argv[1] == NULL)
3179 main(int argc, char *argv[])
3181 const struct got_error *error = NULL;
3182 struct tog_cmd *cmd = NULL;
3184 char **cmd_argv = NULL;
3186 setlocale(LC_ALL, "");
3188 while ((ch = getopt(argc, argv, "h")) != -1) {
3207 /* Build an argument vector which runs a default command. */
3208 cmd = &tog_commands[0];
3209 cmd_argv = make_argv(cmd->name, NULL);
3214 /* Did the user specific a command? */
3215 for (i = 0; i < nitems(tog_commands); i++) {
3216 if (strncmp(tog_commands[i].name, argv[0],
3217 strlen(argv[0])) == 0) {
3218 cmd = &tog_commands[i];
3220 tog_commands[i].cmd_usage();
3225 /* Did the user specify a repository? */
3226 char *repo_path = realpath(argv[0], NULL);
3228 struct got_repository *repo;
3229 error = got_repo_open(&repo, repo_path);
3231 got_repo_close(repo);
3233 error = got_error_from_errno();
3236 fprintf(stderr, "%s: '%s' is not a "
3237 "known command\n", getprogname(),
3241 fprintf(stderr, "%s: '%s' is neither a known "
3242 "command nor a path to a repository\n",
3243 getprogname(), argv[0]);
3247 cmd = &tog_commands[0];
3248 cmd_argv = make_argv(cmd->name, repo_path);
3256 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3263 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);