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 *id;
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 */
203 struct tog_view *parent;
204 struct tog_view *child;
206 /* type-specific state */
207 enum tog_view_type type;
209 struct tog_diff_view_state diff;
210 struct tog_log_view_state log;
211 struct tog_blame_view_state blame;
212 struct tog_tree_view_state tree;
215 const struct got_error *(*show)(struct tog_view *);
216 const struct got_error *(*input)(struct tog_view **,
217 struct tog_view **, struct tog_view *, int);
218 const struct got_error *(*set_child)(struct tog_view *,
220 const struct got_error *(*close)(struct tog_view *);
223 static const struct got_error *open_diff_view(struct tog_view *,
224 struct got_object *, struct got_object *, struct got_repository *);
225 static const struct got_error *show_diff_view(struct tog_view *);
226 static const struct got_error *input_diff_view(struct tog_view **,
227 struct tog_view **, struct tog_view *, int);
228 static const struct got_error* close_diff_view(struct tog_view *);
230 static const struct got_error *open_log_view(struct tog_view *,
231 struct got_object_id *, struct got_repository *, const char *);
232 static const struct got_error * show_log_view(struct tog_view *);
233 static const struct got_error *input_log_view(struct tog_view **,
234 struct tog_view **, struct tog_view *, int);
235 static const struct got_error *close_log_view(struct tog_view *);
236 static const struct got_error* set_child_log_view(struct tog_view *,
239 static const struct got_error *open_blame_view(struct tog_view *, char *,
240 struct got_object_id *, struct got_repository *);
241 static const struct got_error *show_blame_view(struct tog_view *);
242 static const struct got_error *input_blame_view(struct tog_view **,
243 struct tog_view **, struct tog_view *, int);
244 static const struct got_error *close_blame_view(struct tog_view *);
246 static const struct got_error *open_tree_view(struct tog_view *,
247 struct got_tree_object *, struct got_object_id *, struct got_repository *);
248 static const struct got_error *show_tree_view(struct tog_view *);
249 static const struct got_error *input_tree_view(struct tog_view **,
250 struct tog_view **, struct tog_view *, int);
251 static const struct got_error *close_tree_view(struct tog_view *);
253 static const struct got_error *
254 view_close(struct tog_view *view)
256 const struct got_error *err = NULL;
259 view->child->parent = NULL;
261 view->parent->child = NULL;
263 err = view->close(view);
265 del_panel(view->panel);
267 delwin(view->window);
272 static struct tog_view *
273 view_open(int nlines, int ncols, int begin_y, int begin_x,
274 struct tog_view *parent, enum tog_view_type type)
276 struct tog_view *view = calloc(1, sizeof(*view));
281 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
282 begin_x = parent->ncols - 80;
284 view->parent = parent;
286 parent->child = view;
290 view->nlines = nlines ? nlines : LINES - begin_y;
291 view->ncols = ncols ? ncols : COLS - begin_x;
292 view->begin_y = begin_y;
293 view->begin_x = begin_x;
294 view->window = newwin(nlines, ncols, begin_y, begin_x);
295 if (view->window == NULL) {
299 view->panel = new_panel(view->window);
300 if (view->panel == NULL) {
305 keypad(view->window, TRUE);
309 static const struct got_error *
310 view_show(struct tog_view *view)
312 const struct got_error *err;
315 err = view->parent->show(view->parent);
318 show_panel(view->parent->panel);
321 err = view->show(view);
324 show_panel(view->panel);
327 err = view->child->show(view->child);
330 show_panel(view->child->panel);
339 static const struct got_error *
340 view_resize(struct tog_view *view)
345 if (view->lines > LINES)
346 nlines = view->nlines - (view->lines - LINES);
348 nlines = view->nlines + (LINES - view->lines);
350 if (view->cols > COLS)
351 ncols = view->ncols - (view->cols - COLS);
353 ncols = view->ncols + (COLS - view->cols);
355 if (wresize(view->window, nlines, ncols) == ERR)
356 return got_error_from_errno();
357 replace_panel(view->panel, view->window);
359 view->nlines = nlines;
370 static const struct got_error *
371 view_input(struct tog_view **new, struct tog_view **dead,
372 struct tog_view **focus, int *done, struct tog_view *view,
373 struct tog_view_list_head *views)
375 const struct got_error *err = NULL;
376 struct tog_view *next, *prev;
382 nodelay(stdscr, FALSE);
383 ch = wgetch(view->window);
384 nodelay(stdscr, TRUE);
389 next = TAILQ_NEXT(view, entry);
393 *focus = TAILQ_FIRST(views);
396 prev = TAILQ_PREV(view, tog_view_list_head, entry);
400 *focus = TAILQ_LAST(views, tog_view_list_head);
403 err = view->input(new, dead, view, ch);
410 err = view_resize(view);
413 err = view->input(new, dead, view, ch);
416 err = view->input(new, dead, view, ch);
423 static const struct got_error *
424 view_set_child(struct tog_view *view, struct tog_view *child)
426 const struct got_error *err;
428 if (view->set_child) {
429 err = view->set_child(view, child);
439 view_vborder(struct tog_view *view)
441 if (view->child == NULL)
444 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
445 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
448 static const struct got_error *
449 view_loop(struct tog_view *view)
451 const struct got_error *err = NULL;
452 struct tog_view_list_head views;
453 struct tog_view *new_view, *dead_view;
457 TAILQ_INSERT_HEAD(&views, view, entry);
459 while (!TAILQ_EMPTY(&views) && !done) {
460 err = view_show(view);
463 err = view_input(&new_view, &dead_view, &view, &done,
468 TAILQ_REMOVE(&views, dead_view, entry);
469 TAILQ_FOREACH(view, &views, entry) {
470 if (view->parent == dead_view)
473 if (dead_view->parent)
474 view = dead_view->parent;
476 view = TAILQ_LAST(&views, tog_view_list_head);
477 if (dead_view->child) {
478 TAILQ_REMOVE(&views, dead_view->child, entry);
479 err = view_close(dead_view->child);
483 err = view_close(dead_view);
488 /* TODO: de-duplicate! */
489 TAILQ_INSERT_TAIL(&views, new_view, entry);
490 if (new_view->parent) {
491 err = view_set_child(new_view->parent, new_view);
499 while (!TAILQ_EMPTY(&views)) {
500 view = TAILQ_FIRST(&views);
501 TAILQ_REMOVE(&views, view, entry);
512 "usage: %s log [-c commit] [-r repository-path] [path]\n",
517 /* Create newly allocated wide-character string equivalent to a byte string. */
518 static const struct got_error *
519 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
522 const struct got_error *err = NULL;
525 *wlen = mbstowcs(NULL, s, 0);
526 if (*wlen == (size_t)-1) {
529 return got_error_from_errno();
531 /* byte string invalid in current encoding; try to "fix" it */
532 err = got_mbsavis(&vis, &vislen, s);
535 *wlen = mbstowcs(NULL, vis, 0);
536 if (*wlen == (size_t)-1) {
537 err = got_error_from_errno(); /* give up */
542 *ws = calloc(*wlen + 1, sizeof(*ws));
544 err = got_error_from_errno();
548 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
549 err = got_error_from_errno();
560 /* Format a line for display, ensuring that it won't overflow a width limit. */
561 static const struct got_error *
562 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
564 const struct got_error *err = NULL;
566 wchar_t *wline = NULL;
573 err = mbs2ws(&wline, &wlen, line);
578 while (i < wlen && cols < wlimit) {
579 int width = wcwidth(wline[i]);
586 if (cols + width <= wlimit) {
592 if (wline[i] == L'\t')
593 cols += TABSIZE - ((cols + 1) % TABSIZE);
597 err = got_error_from_errno();
612 static const struct got_error *
613 draw_commit(struct tog_view *view, struct got_commit_object *commit,
614 struct got_object_id *id)
616 const struct got_error *err = NULL;
617 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
618 char *logmsg0 = NULL, *logmsg = NULL;
619 char *author0 = NULL, *author = NULL;
620 wchar_t *wlogmsg = NULL, *wauthor = NULL;
621 int author_width, logmsg_width;
622 char *newline, *smallerthan;
625 static const size_t date_display_cols = 9;
626 static const size_t author_display_cols = 16;
627 const int avail = view->ncols;
629 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
630 &commit->tm_committer) >= sizeof(datebuf))
631 return got_error(GOT_ERR_NO_SPACE);
633 if (avail < date_display_cols)
634 limit = MIN(sizeof(datebuf) - 1, avail);
636 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
637 waddnstr(view->window, datebuf, limit);
642 author0 = strdup(commit->author);
643 if (author0 == NULL) {
644 err = got_error_from_errno();
648 smallerthan = strchr(author, '<');
652 char *at = strchr(author, '@');
657 err = format_line(&wauthor, &author_width, author, limit);
660 waddwstr(view->window, wauthor);
662 while (col <= avail && author_width < author_display_cols + 1) {
663 waddch(view->window, ' ');
670 logmsg0 = strdup(commit->logmsg);
671 if (logmsg0 == NULL) {
672 err = got_error_from_errno();
676 while (*logmsg == '\n')
678 newline = strchr(logmsg, '\n');
682 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
685 waddwstr(view->window, wlogmsg);
687 while (col <= avail) {
688 waddch(view->window, ' ');
700 static struct commit_queue_entry *
701 alloc_commit_queue_entry(struct got_commit_object *commit,
702 struct got_object_id *id)
704 struct commit_queue_entry *entry;
706 entry = calloc(1, sizeof(*entry));
711 entry->commit = commit;
716 pop_commit(struct commit_queue *commits)
718 struct commit_queue_entry *entry;
720 entry = TAILQ_FIRST(&commits->head);
721 TAILQ_REMOVE(&commits->head, entry, entry);
722 got_object_commit_close(entry->commit);
724 /* Don't free entry->id! It is owned by the commit graph. */
729 free_commits(struct commit_queue *commits)
731 while (!TAILQ_EMPTY(&commits->head))
735 static const struct got_error *
736 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
737 struct got_object_id *start_id, int minqueue, int init,
738 struct got_repository *repo, const char *path)
740 const struct got_error *err = NULL;
741 struct got_object_id *id;
742 struct commit_queue_entry *entry;
743 int nfetched, nqueued = 0, found_obj = 0;
744 int is_root_path = strcmp(path, "/") == 0;
746 err = got_commit_graph_iter_start(graph, start_id);
750 entry = TAILQ_LAST(&commits->head, commit_queue_head);
751 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
754 /* Start ID's commit is already on the queue; skip over it. */
755 err = got_commit_graph_iter_next(&id, graph);
756 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
759 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
765 struct got_commit_object *commit;
767 err = got_commit_graph_iter_next(&id, graph);
769 if (err->code != GOT_ERR_ITER_NEED_MORE)
771 if (nqueued >= minqueue) {
775 err = got_commit_graph_fetch_commits(&nfetched,
784 err = got_object_open_as_commit(&commit, repo, id);
789 struct got_object *obj;
790 struct got_object_qid *pid;
793 err = got_object_open_by_path(&obj, repo, id, path);
795 got_object_commit_close(commit);
796 if (err->code == GOT_ERR_NO_OBJ &&
797 (found_obj || !init)) {
798 /* History stops here. */
799 err = got_error(GOT_ERR_ITER_COMPLETED);
805 pid = SIMPLEQ_FIRST(&commit->parent_ids);
807 struct got_object *pobj;
808 err = got_object_open_by_path(&pobj, repo,
811 if (err->code != GOT_ERR_NO_OBJ) {
812 got_object_close(obj);
813 got_object_commit_close(commit);
819 struct got_object_id *id, *pid;
820 id = got_object_get_id(obj);
822 err = got_error_from_errno();
823 got_object_close(obj);
824 got_object_close(pobj);
827 pid = got_object_get_id(pobj);
829 err = got_error_from_errno();
831 got_object_close(obj);
832 got_object_close(pobj);
836 (got_object_id_cmp(id, pid) != 0);
837 got_object_close(pobj);
842 got_object_close(obj);
844 got_object_commit_close(commit);
849 entry = alloc_commit_queue_entry(commit, id);
851 err = got_error_from_errno();
854 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
862 static const struct got_error *
863 fetch_next_commit(struct commit_queue_entry **pentry,
864 struct commit_queue_entry *entry, struct commit_queue *commits,
865 struct got_commit_graph *graph, struct got_repository *repo,
868 const struct got_error *err = NULL;
872 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
876 /* Next entry to display should now be available. */
877 *pentry = TAILQ_NEXT(entry, entry);
879 return got_error(GOT_ERR_NO_OBJ);
884 static const struct got_error *
885 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
887 const struct got_error *err = NULL;
888 struct got_reference *head_ref;
892 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
896 err = got_ref_resolve(head_id, repo, head_ref);
897 got_ref_close(head_ref);
906 static const struct got_error *
907 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
908 struct commit_queue_entry **selected, struct commit_queue_entry *first,
909 struct commit_queue *commits, int selected_idx, int limit,
910 struct got_commit_graph *graph, struct got_repository *repo,
913 const struct got_error *err = NULL;
914 struct commit_queue_entry *entry;
916 char *id_str, *header;
922 if (ncommits == selected_idx) {
926 entry = TAILQ_NEXT(entry, entry);
930 err = got_object_id_str(&id_str, (*selected)->id);
934 if (path && strcmp(path, "/") != 0) {
935 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
936 err = got_error_from_errno();
940 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
941 err = got_error_from_errno();
946 err = format_line(&wline, &width, header, view->ncols);
953 werase(view->window);
955 waddwstr(view->window, wline);
956 if (width < view->ncols)
957 waddch(view->window, '\n');
966 if (ncommits >= limit - 1)
968 if (ncommits == selected_idx)
969 wstandout(view->window);
970 err = draw_commit(view, entry->commit, entry->id);
971 if (ncommits == selected_idx)
972 wstandend(view->window);
977 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
978 err = queue_commits(graph, commits, entry->id, 1,
981 if (err->code != GOT_ERR_ITER_COMPLETED)
986 entry = TAILQ_NEXT(entry, entry);
995 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
996 struct commit_queue *commits)
998 struct commit_queue_entry *entry;
1001 entry = TAILQ_FIRST(&commits->head);
1002 if (*first_displayed_entry == entry)
1005 entry = *first_displayed_entry;
1006 while (entry && nscrolled < maxscroll) {
1007 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1009 *first_displayed_entry = entry;
1015 static const struct got_error *
1016 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1017 struct commit_queue_entry *last_displayed_entry,
1018 struct commit_queue *commits, struct got_commit_graph *graph,
1019 struct got_repository *repo, const char *path)
1021 const struct got_error *err = NULL;
1022 struct commit_queue_entry *pentry;
1026 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1027 if (pentry == NULL) {
1028 err = fetch_next_commit(&pentry, last_displayed_entry,
1029 commits, graph, repo, path);
1030 if (err || pentry == NULL)
1033 last_displayed_entry = pentry;
1035 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1038 *first_displayed_entry = pentry;
1039 } while (++nscrolled < maxscroll);
1044 static const struct got_error *
1045 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1046 struct commit_queue_entry *entry, struct got_repository *repo)
1048 const struct got_error *err;
1049 struct got_object *obj1 = NULL, *obj2 = NULL;
1050 struct got_object_qid *parent_id;
1051 struct tog_view *diff_view;
1053 err = got_object_open(&obj2, repo, entry->id);
1057 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1059 err = got_object_open(&obj1, repo, parent_id->id);
1064 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1065 if (diff_view == NULL) {
1066 err = got_error_from_errno();
1070 err = open_diff_view(diff_view, obj1, obj2, repo);
1072 *new_view = diff_view;
1075 got_object_close(obj1);
1077 got_object_close(obj2);
1081 static const struct got_error *
1082 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1083 struct commit_queue_entry *entry, struct got_repository *repo)
1085 const struct got_error *err = NULL;
1086 struct got_tree_object *tree;
1087 struct tog_view *tree_view;
1089 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1093 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1094 if (tree_view == NULL)
1095 return got_error_from_errno();
1097 err = open_tree_view(tree_view, tree, entry->id, repo);
1099 got_object_tree_close(tree);
1101 *new_view = tree_view;
1105 static const struct got_error *
1106 set_child_log_view(struct tog_view *view, struct tog_view *child)
1108 struct tog_log_view_state *s = &view->state.log;
1109 struct tog_diff_view_state *ds;
1110 struct commit_queue_entry *commit, *child_entry = NULL;
1111 int selected_idx = 0;
1113 if (child->type != TOG_VIEW_DIFF)
1115 ds = &child->state.diff;
1117 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1118 if (got_object_id_cmp(commit->id, ds->id) == 0) {
1119 child_entry = commit;
1123 if (child_entry == NULL)
1126 commit = s->first_displayed_entry;
1128 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1129 s->selected_entry = child_entry;
1130 s->selected = selected_idx;
1133 if (commit == s->last_displayed_entry)
1136 commit = TAILQ_NEXT(commit, entry);
1139 return show_log_view(view);
1142 static const struct got_error *
1143 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1144 struct got_repository *repo, const char *path)
1146 const struct got_error *err = NULL;
1147 struct got_object_id *head_id = NULL;
1149 struct tog_log_view_state *s = &view->state.log;
1151 err = got_repo_map_path(&s->in_repo_path, repo, path);
1155 err = get_head_commit_id(&head_id, repo);
1159 /* The graph contains all commits. */
1160 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1163 /* The commit queue contains a subset of commits filtered by path. */
1164 TAILQ_INIT(&s->commits.head);
1165 s->commits.ncommits = 0;
1167 /* Populate commit graph with a sufficient number of commits. */
1168 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1174 * Open the initial batch of commits, sorted in commit graph order.
1175 * We keep all commits open throughout the lifetime of the log view
1176 * in order to avoid having to re-fetch commits from disk while
1177 * updating the display.
1179 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1180 repo, s->in_repo_path);
1182 if (err->code != GOT_ERR_ITER_COMPLETED)
1187 s->first_displayed_entry =
1188 TAILQ_FIRST(&s->commits.head);
1189 s->selected_entry = s->first_displayed_entry;
1192 view->show = show_log_view;
1193 view->input = input_log_view;
1194 view->close = close_log_view;
1195 view->set_child = set_child_log_view;
1201 static const struct got_error *
1202 close_log_view(struct tog_view *view)
1204 struct tog_log_view_state *s = &view->state.log;
1207 got_commit_graph_close(s->graph);
1208 free_commits(&s->commits);
1209 free(s->in_repo_path);
1213 static const struct got_error *
1214 update_diff_child_view(struct tog_view *parent,
1215 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1217 const struct got_error *err = NULL;
1218 struct tog_diff_view_state *ds;
1219 struct got_object *obj1 = NULL, *obj2 = NULL;
1220 struct got_object_qid *parent_id;
1221 struct tog_view *child_view = parent->child;
1223 if (child_view == NULL)
1225 if (child_view->type != TOG_VIEW_DIFF)
1227 ds = &child_view->state.diff;
1228 if (got_object_id_cmp(ds->id, selected_entry->id) == 0)
1231 err = got_object_open(&obj2, repo, selected_entry->id);
1235 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1237 err = got_object_open(&obj1, repo, parent_id->id);
1242 err = close_diff_view(child_view);
1246 err = open_diff_view(child_view, obj1, obj2, repo);
1251 got_object_close(obj1);
1253 got_object_close(obj2);
1257 static const struct got_error *
1258 show_log_view(struct tog_view *view)
1260 const struct got_error *err = NULL;
1261 struct tog_log_view_state *s = &view->state.log;
1263 err = draw_commits(view, &s->last_displayed_entry,
1264 &s->selected_entry, s->first_displayed_entry,
1265 &s->commits, s->selected, view->nlines, s->graph,
1266 s->repo, s->in_repo_path);
1270 return update_diff_child_view(view, s->selected_entry, s->repo);
1273 static const struct got_error *
1274 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1275 struct tog_view *view, int ch)
1277 const struct got_error *err = NULL;
1278 struct tog_log_view_state *s = &view->state.log;
1284 if (s->selected > 0)
1286 if (s->selected > 0)
1288 scroll_up(&s->first_displayed_entry, 1,
1292 if (TAILQ_FIRST(&s->commits.head) ==
1293 s->first_displayed_entry) {
1297 scroll_up(&s->first_displayed_entry,
1298 view->nlines, &s->commits);
1303 if (s->selected < MIN(view->nlines - 2,
1304 s->commits.ncommits - 1)) {
1308 err = scroll_down(&s->first_displayed_entry, 1,
1309 s->last_displayed_entry, &s->commits,
1310 s->graph, s->repo, s->in_repo_path);
1312 if (err->code != GOT_ERR_ITER_COMPLETED)
1318 struct commit_queue_entry *first;
1319 first = s->first_displayed_entry;
1320 err = scroll_down(&s->first_displayed_entry,
1321 view->nlines, s->last_displayed_entry,
1322 &s->commits, s->graph, s->repo,
1326 if (err->code != GOT_ERR_ITER_COMPLETED)
1328 if (first == s->first_displayed_entry &&
1329 s->selected < MIN(view->nlines - 2,
1330 s->commits.ncommits - 1)) {
1331 /* can't scroll further down */
1332 s->selected = MIN(view->nlines - 2,
1333 s->commits.ncommits - 1);
1339 if (s->selected > view->nlines - 2)
1340 s->selected = view->nlines - 2;
1341 if (s->selected > s->commits.ncommits - 1)
1342 s->selected = s->commits.ncommits - 1;
1346 err = show_commit(new_view, view, s->selected_entry,
1350 err = browse_commit(new_view, view, s->selected_entry,
1360 static const struct got_error *
1361 cmd_log(int argc, char *argv[])
1363 const struct got_error *error;
1364 struct got_repository *repo = NULL;
1365 struct got_object_id *start_id = NULL;
1366 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1367 char *start_commit = NULL;
1369 struct tog_view *view;
1372 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1376 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1379 start_commit = optarg;
1382 repo_path = realpath(optarg, NULL);
1383 if (repo_path == NULL)
1384 err(1, "-r option");
1398 path = strdup(argv[0]);
1402 return got_error_from_errno();
1404 cwd = getcwd(NULL, 0);
1406 error = got_error_from_errno();
1409 if (repo_path == NULL) {
1410 repo_path = strdup(cwd);
1411 if (repo_path == NULL) {
1412 error = got_error_from_errno();
1417 error = got_repo_open(&repo, repo_path);
1421 if (start_commit == NULL) {
1422 error = get_head_commit_id(&start_id, repo);
1426 struct got_object *obj;
1427 error = got_object_open_by_id_str(&obj, repo, start_commit);
1428 if (error == NULL) {
1429 start_id = got_object_get_id(obj);
1430 if (start_id == NULL)
1431 error = got_error_from_errno();
1438 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1440 error = got_error_from_errno();
1443 error = open_log_view(view, start_id, repo, path);
1446 error = view_loop(view);
1453 got_repo_close(repo);
1461 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1467 parse_next_line(FILE *f, size_t *len)
1472 const char delim[3] = { '\0', '\0', '\0'};
1474 line = fparseln(f, &linelen, &lineno, delim, 0);
1480 static const struct got_error *
1481 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1482 int *last_displayed_line, int *eof, int max_lines)
1484 const struct got_error *err;
1485 int nlines = 0, nprinted = 0;
1492 werase(view->window);
1495 while (nprinted < max_lines) {
1496 line = parse_next_line(f, &len);
1501 if (++nlines < *first_displayed_line) {
1506 err = format_line(&wline, &width, line, view->ncols);
1511 waddwstr(view->window, wline);
1512 if (width < view->ncols)
1513 waddch(view->window, '\n');
1514 if (++nprinted == 1)
1515 *first_displayed_line = nlines;
1520 *last_displayed_line = nlines;
1527 static const struct got_error *
1528 open_diff_view(struct tog_view *view, struct got_object *obj1,
1529 struct got_object *obj2, struct got_repository *repo)
1531 const struct got_error *err;
1534 if (obj1 != NULL && obj2 != NULL &&
1535 got_object_get_type(obj1) != got_object_get_type(obj2))
1536 return got_error(GOT_ERR_OBJ_TYPE);
1540 return got_error_from_errno();
1542 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1543 case GOT_OBJ_TYPE_BLOB:
1544 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1546 case GOT_OBJ_TYPE_TREE:
1547 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1549 case GOT_OBJ_TYPE_COMMIT:
1550 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1553 return got_error(GOT_ERR_OBJ_TYPE);
1558 view->state.diff.id = got_object_get_id(obj2);
1559 if (view->state.diff.id == NULL)
1560 return got_error_from_errno();
1561 view->state.diff.f = f;
1562 view->state.diff.first_displayed_line = 1;
1563 view->state.diff.last_displayed_line = view->nlines;
1565 view->show = show_diff_view;
1566 view->input = input_diff_view;
1567 view->close = close_diff_view;
1572 static const struct got_error *
1573 close_diff_view(struct tog_view *view)
1575 const struct got_error *err = NULL;
1577 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1578 err = got_error_from_errno();
1579 free(view->state.diff.id);
1583 static const struct got_error *
1584 show_diff_view(struct tog_view *view)
1586 struct tog_diff_view_state *s = &view->state.diff;
1588 return draw_file(view, s->f, &s->first_displayed_line,
1589 &s->last_displayed_line, &s->eof, view->nlines);
1592 static const struct got_error *
1593 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1594 struct tog_view *view, int ch)
1596 const struct got_error *err = NULL;
1597 struct tog_diff_view_state *s = &view->state.diff;
1603 if (s->first_displayed_line > 1)
1604 s->first_displayed_line--;
1608 while (i++ < view->nlines - 1 &&
1609 s->first_displayed_line > 1)
1610 s->first_displayed_line--;
1615 s->first_displayed_line++;
1620 while (!s->eof && i++ < view->nlines - 1) {
1622 line = parse_next_line(s->f, NULL);
1623 s->first_displayed_line++;
1630 struct tog_log_view_state *ls;
1631 struct commit_queue_entry *entry;
1632 struct tog_view *diff_view;
1634 if (view->parent == NULL)
1636 if (view->parent->type != TOG_VIEW_LOG)
1638 ls = &view->parent->state.log;
1641 entry = TAILQ_PREV(ls->selected_entry,
1642 commit_queue_head, entry);
1644 entry = TAILQ_NEXT(ls->selected_entry, entry);
1645 if (entry == NULL) {
1646 err = fetch_next_commit(&entry,
1648 &ls->commits, ls->graph,
1649 ls->repo, ls->in_repo_path);
1656 err = show_commit(&diff_view, view->parent,
1660 *new_view = diff_view;
1671 static const struct got_error *
1672 cmd_diff(int argc, char *argv[])
1674 const struct got_error *error = NULL;
1675 struct got_repository *repo = NULL;
1676 struct got_object *obj1 = NULL, *obj2 = NULL;
1677 char *repo_path = NULL;
1678 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1680 struct tog_view *view;
1683 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1687 while ((ch = getopt(argc, argv, "")) != -1) {
1699 usage_diff(); /* TODO show local worktree changes */
1700 } else if (argc == 2) {
1701 repo_path = getcwd(NULL, 0);
1702 if (repo_path == NULL)
1703 return got_error_from_errno();
1704 obj_id_str1 = argv[0];
1705 obj_id_str2 = argv[1];
1706 } else if (argc == 3) {
1707 repo_path = realpath(argv[0], NULL);
1708 if (repo_path == NULL)
1709 return got_error_from_errno();
1710 obj_id_str1 = argv[1];
1711 obj_id_str2 = argv[2];
1715 error = got_repo_open(&repo, repo_path);
1720 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1724 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1728 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1730 error = got_error_from_errno();
1733 error = open_diff_view(view, obj1, obj2, repo);
1736 error = view_loop(view);
1738 got_repo_close(repo);
1740 got_object_close(obj1);
1742 got_object_close(obj2);
1750 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1755 struct tog_blame_line {
1757 struct got_object_id *id;
1760 static const struct got_error *
1761 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1762 const char *path, struct tog_blame_line *lines, int nlines,
1763 int blame_complete, int selected_line, int *first_displayed_line,
1764 int *last_displayed_line, int *eof, int max_lines)
1766 const struct got_error *err;
1767 int lineno = 0, nprinted = 0;
1772 struct tog_blame_line *blame_line;
1773 struct got_object_id *prev_id = NULL;
1776 err = got_object_id_str(&id_str, id);
1781 werase(view->window);
1783 if (asprintf(&line, "commit: %s", id_str) == -1) {
1784 err = got_error_from_errno();
1789 err = format_line(&wline, &width, line, view->ncols);
1792 waddwstr(view->window, wline);
1795 if (width < view->ncols)
1796 waddch(view->window, '\n');
1798 if (asprintf(&line, "[%d/%d] %s%s",
1799 *first_displayed_line - 1 + selected_line, nlines,
1800 blame_complete ? "" : "annotating ", path) == -1) {
1802 return got_error_from_errno();
1805 err = format_line(&wline, &width, line, view->ncols);
1810 waddwstr(view->window, wline);
1813 if (width < view->ncols)
1814 waddch(view->window, '\n');
1817 while (nprinted < max_lines - 2) {
1818 line = parse_next_line(f, &len);
1823 if (++lineno < *first_displayed_line) {
1828 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1829 err = format_line(&wline, &width, line, wlimit);
1835 if (nprinted == selected_line - 1)
1836 wstandout(view->window);
1838 blame_line = &lines[lineno - 1];
1839 if (blame_line->annotated && prev_id &&
1840 got_object_id_cmp(prev_id, blame_line->id) == 0)
1841 waddstr(view->window, " ");
1842 else if (blame_line->annotated) {
1844 err = got_object_id_str(&id_str, blame_line->id);
1850 wprintw(view->window, "%.8s ", id_str);
1852 prev_id = blame_line->id;
1854 waddstr(view->window, "........ ");
1858 waddwstr(view->window, wline);
1859 while (width < wlimit) {
1860 waddch(view->window, ' ');
1863 if (nprinted == selected_line - 1)
1864 wstandend(view->window);
1865 if (++nprinted == 1)
1866 *first_displayed_line = lineno;
1871 *last_displayed_line = lineno;
1878 static const struct got_error *
1879 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1881 const struct got_error *err = NULL;
1882 struct tog_blame_cb_args *a = arg;
1883 struct tog_blame_line *line;
1885 if (nlines != a->nlines ||
1886 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1887 return got_error(GOT_ERR_RANGE);
1889 if (pthread_mutex_lock(a->mutex) != 0)
1890 return got_error_from_errno();
1892 if (*a->quit) { /* user has quit the blame view */
1893 err = got_error(GOT_ERR_ITER_COMPLETED);
1898 goto done; /* no change in this commit */
1900 line = &a->lines[lineno - 1];
1901 if (line->annotated)
1904 line->id = got_object_id_dup(id);
1905 if (line->id == NULL) {
1906 err = got_error_from_errno();
1909 line->annotated = 1;
1911 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1912 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1913 a->last_displayed_line, a->eof, a->view->nlines);
1915 if (pthread_mutex_unlock(a->mutex) != 0)
1916 return got_error_from_errno();
1921 blame_thread(void *arg)
1923 const struct got_error *err;
1924 struct tog_blame_thread_args *ta = arg;
1925 struct tog_blame_cb_args *a = ta->cb_args;
1927 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1928 blame_cb, ta->cb_args);
1930 if (pthread_mutex_lock(a->mutex) != 0)
1931 return (void *)got_error_from_errno();
1933 got_repo_close(ta->repo);
1937 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1938 a->lines, a->nlines, 1, *a->selected_line,
1939 a->first_displayed_line, a->last_displayed_line, a->eof,
1942 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1943 err = got_error_from_errno();
1948 static struct got_object_id *
1949 get_selected_commit_id(struct tog_blame_line *lines,
1950 int first_displayed_line, int selected_line)
1952 struct tog_blame_line *line;
1954 line = &lines[first_displayed_line - 1 + selected_line - 1];
1955 if (!line->annotated)
1961 static const struct got_error *
1962 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1963 struct tog_blame_line *lines, int first_displayed_line,
1964 int selected_line, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_commit_object *commit = NULL;
1968 struct got_object_id *selected_id;
1969 struct got_object_qid *pid;
1974 selected_id = get_selected_commit_id(lines,
1975 first_displayed_line, selected_line);
1976 if (selected_id == NULL)
1979 err = got_object_open(obj, repo, selected_id);
1983 err = got_object_commit_open(&commit, repo, *obj);
1987 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1989 err = got_object_open(pobj, repo, pid->id);
1995 got_object_commit_close(commit);
1999 static const struct got_error *
2000 stop_blame(struct tog_blame *blame)
2002 const struct got_error *err = NULL;
2005 if (blame->thread) {
2006 if (pthread_join(blame->thread, (void **)&err) != 0)
2007 err = got_error_from_errno();
2008 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2010 blame->thread = NULL;
2012 if (blame->thread_args.repo) {
2013 got_repo_close(blame->thread_args.repo);
2014 blame->thread_args.repo = NULL;
2020 for (i = 0; i < blame->nlines; i++)
2021 free(blame->lines[i].id);
2023 blame->lines = NULL;
2024 free(blame->cb_args.commit_id);
2025 blame->cb_args.commit_id = NULL;
2030 static const struct got_error *
2031 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2032 struct tog_view *view, int *blame_complete,
2033 int *first_displayed_line, int *last_displayed_line,
2034 int *selected_line, int *done, int *eof, const char *path,
2035 struct got_object_id *commit_id,
2036 struct got_repository *repo)
2038 const struct got_error *err = NULL;
2039 struct got_blob_object *blob = NULL;
2040 struct got_repository *thread_repo = NULL;
2041 struct got_object *obj;
2043 err = got_object_open_by_path(&obj, repo, commit_id, path);
2046 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2047 err = got_error(GOT_ERR_OBJ_TYPE);
2051 err = got_object_blob_open(&blob, repo, obj, 8192);
2054 blame->f = got_opentemp();
2055 if (blame->f == NULL) {
2056 err = got_error_from_errno();
2059 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2064 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2065 if (blame->lines == NULL) {
2066 err = got_error_from_errno();
2070 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2074 blame->cb_args.view = view;
2075 blame->cb_args.lines = blame->lines;
2076 blame->cb_args.nlines = blame->nlines;
2077 blame->cb_args.mutex = mutex;
2078 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2079 if (blame->cb_args.commit_id == NULL) {
2080 err = got_error_from_errno();
2083 blame->cb_args.f = blame->f;
2084 blame->cb_args.path = path;
2085 blame->cb_args.first_displayed_line = first_displayed_line;
2086 blame->cb_args.selected_line = selected_line;
2087 blame->cb_args.last_displayed_line = last_displayed_line;
2088 blame->cb_args.quit = done;
2089 blame->cb_args.eof = eof;
2091 blame->thread_args.path = path;
2092 blame->thread_args.repo = thread_repo;
2093 blame->thread_args.cb_args = &blame->cb_args;
2094 blame->thread_args.complete = blame_complete;
2095 *blame_complete = 0;
2097 if (pthread_create(&blame->thread, NULL, blame_thread,
2098 &blame->thread_args) != 0) {
2099 err = got_error_from_errno();
2105 got_object_blob_close(blob);
2107 got_object_close(obj);
2113 static const struct got_error *
2114 open_blame_view(struct tog_view *view, char *path,
2115 struct got_object_id *commit_id, struct got_repository *repo)
2117 const struct got_error *err = NULL;
2118 struct tog_blame_view_state *s = &view->state.blame;
2120 SIMPLEQ_INIT(&s->blamed_commits);
2122 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2123 return got_error_from_errno();
2125 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2129 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2130 s->first_displayed_line = 1;
2131 s->last_displayed_line = view->nlines;
2132 s->selected_line = 1;
2133 s->blame_complete = 0;
2135 if (s->path == NULL)
2136 return got_error_from_errno();
2138 s->commit_id = commit_id;
2139 memset(&s->blame, 0, sizeof(s->blame));
2141 view->show = show_blame_view;
2142 view->input = input_blame_view;
2143 view->close = close_blame_view;
2145 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2146 &s->first_displayed_line, &s->last_displayed_line,
2147 &s->selected_line, &s->done, &s->eof, s->path,
2148 s->blamed_commit->id, s->repo);
2151 static const struct got_error *
2152 close_blame_view(struct tog_view *view)
2154 const struct got_error *err = NULL;
2155 struct tog_blame_view_state *s = &view->state.blame;
2157 if (s->blame.thread)
2158 err = stop_blame(&s->blame);
2160 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2161 struct got_object_qid *blamed_commit;
2162 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2163 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2164 got_object_qid_free(blamed_commit);
2172 static const struct got_error *
2173 show_blame_view(struct tog_view *view)
2175 const struct got_error *err = NULL;
2176 struct tog_blame_view_state *s = &view->state.blame;
2178 if (pthread_mutex_lock(&s->mutex) != 0)
2179 return got_error_from_errno();
2181 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2182 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2183 s->selected_line, &s->first_displayed_line,
2184 &s->last_displayed_line, &s->eof, view->nlines);
2186 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2187 err = got_error_from_errno();
2192 static const struct got_error *
2193 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2194 struct tog_view *view, int ch)
2196 const struct got_error *err = NULL, *thread_err = NULL;
2197 struct got_object *obj = NULL, *pobj = NULL;
2198 struct tog_view *diff_view;
2199 struct tog_blame_view_state *s = &view->state.blame;
2201 if (pthread_mutex_lock(&s->mutex) != 0) {
2202 err = got_error_from_errno();
2209 if (pthread_mutex_unlock(&s->mutex) != 0) {
2210 err = got_error_from_errno();
2213 return stop_blame(&s->blame);
2216 if (s->selected_line > 1)
2218 else if (s->selected_line == 1 &&
2219 s->first_displayed_line > 1)
2220 s->first_displayed_line--;
2223 if (s->first_displayed_line == 1) {
2224 s->selected_line = 1;
2227 if (s->first_displayed_line > view->nlines - 2)
2228 s->first_displayed_line -=
2231 s->first_displayed_line = 1;
2235 if (s->selected_line < view->nlines - 2 &&
2236 s->first_displayed_line +
2237 s->selected_line <= s->blame.nlines)
2239 else if (s->last_displayed_line <
2241 s->first_displayed_line++;
2245 struct got_object_id *id;
2246 id = get_selected_commit_id(s->blame.lines,
2247 s->first_displayed_line, s->selected_line);
2248 if (id == NULL || got_object_id_cmp(id,
2249 s->blamed_commit->id) == 0)
2251 err = open_selected_commit(&pobj, &obj,
2252 s->blame.lines, s->first_displayed_line,
2253 s->selected_line, s->repo);
2256 if (pobj == NULL && obj == NULL)
2258 if (ch == 'p' && pobj == NULL)
2261 if (pthread_mutex_unlock(&s->mutex) != 0) {
2262 err = got_error_from_errno();
2265 thread_err = stop_blame(&s->blame);
2267 if (pthread_mutex_lock(&s->mutex) != 0) {
2268 err = got_error_from_errno();
2273 id = got_object_get_id(ch == 'b' ? obj : pobj);
2274 got_object_close(obj);
2277 got_object_close(pobj);
2281 err = got_error_from_errno();
2284 err = got_object_qid_alloc(
2285 &s->blamed_commit, id);
2289 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2290 s->blamed_commit, entry);
2291 err = run_blame(&s->blame, &s->mutex, view,
2293 &s->first_displayed_line,
2294 &s->last_displayed_line,
2295 &s->selected_line, &s->done, &s->eof,
2296 s->path, s->blamed_commit->id, s->repo);
2302 struct got_object_qid *first;
2303 first = SIMPLEQ_FIRST(&s->blamed_commits);
2304 if (!got_object_id_cmp(first->id, s->commit_id))
2307 if (pthread_mutex_unlock(&s->mutex) != 0) {
2308 err = got_error_from_errno();
2311 thread_err = stop_blame(&s->blame);
2313 if (pthread_mutex_lock(&s->mutex) != 0) {
2314 err = got_error_from_errno();
2319 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2320 got_object_qid_free(s->blamed_commit);
2322 SIMPLEQ_FIRST(&s->blamed_commits);
2323 err = run_blame(&s->blame, &s->mutex, view,
2325 &s->first_displayed_line,
2326 &s->last_displayed_line,
2327 &s->selected_line, &s->done, &s->eof, s->path,
2328 s->blamed_commit->id, s->repo);
2335 err = open_selected_commit(&pobj, &obj,
2336 s->blame.lines, s->first_displayed_line,
2337 s->selected_line, s->repo);
2340 if (pobj == NULL && obj == NULL)
2342 diff_view = view_open(0, 0, 0, 0, view,
2344 if (diff_view == NULL) {
2345 err = got_error_from_errno();
2348 err = open_diff_view(diff_view, pobj, obj,
2351 view_close(diff_view);
2354 *new_view = diff_view;
2356 got_object_close(pobj);
2359 got_object_close(obj);
2366 if (s->last_displayed_line >= s->blame.nlines &&
2367 s->selected_line < view->nlines - 2) {
2368 s->selected_line = MIN(s->blame.nlines,
2372 if (s->last_displayed_line + view->nlines - 2
2374 s->first_displayed_line +=
2377 s->first_displayed_line =
2382 if (s->selected_line > view->nlines - 2) {
2383 s->selected_line = MIN(s->blame.nlines,
2391 if (pthread_mutex_unlock(&s->mutex) != 0)
2392 err = got_error_from_errno();
2395 got_object_close(pobj);
2396 return thread_err ? thread_err : err;
2399 static const struct got_error *
2400 cmd_blame(int argc, char *argv[])
2402 const struct got_error *error;
2403 struct got_repository *repo = NULL;
2404 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2405 struct got_object_id *commit_id = NULL;
2406 char *commit_id_str = NULL;
2408 struct tog_view *view;
2411 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2415 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2418 commit_id_str = optarg;
2421 repo_path = realpath(optarg, NULL);
2422 if (repo_path == NULL)
2423 err(1, "-r option");
2439 cwd = getcwd(NULL, 0);
2441 error = got_error_from_errno();
2444 if (repo_path == NULL) {
2445 repo_path = strdup(cwd);
2446 if (repo_path == NULL) {
2447 error = got_error_from_errno();
2453 error = got_repo_open(&repo, repo_path);
2457 error = got_repo_map_path(&in_repo_path, repo, path);
2461 if (commit_id_str == NULL) {
2462 struct got_reference *head_ref;
2463 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2466 error = got_ref_resolve(&commit_id, repo, head_ref);
2467 got_ref_close(head_ref);
2469 struct got_object *obj;
2470 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2473 commit_id = got_object_get_id(obj);
2474 if (commit_id == NULL)
2475 error = got_error_from_errno();
2476 got_object_close(obj);
2481 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2483 error = got_error_from_errno();
2486 error = open_blame_view(view, in_repo_path, commit_id, repo);
2489 error = view_loop(view);
2495 got_repo_close(repo);
2499 static const struct got_error *
2500 draw_tree_entries(struct tog_view *view,
2501 struct got_tree_entry **first_displayed_entry,
2502 struct got_tree_entry **last_displayed_entry,
2503 struct got_tree_entry **selected_entry, int *ndisplayed,
2504 const char *label, int show_ids, const char *parent_path,
2505 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2507 const struct got_error *err = NULL;
2508 struct got_tree_entry *te;
2514 werase(view->window);
2519 err = format_line(&wline, &width, label, view->ncols);
2522 waddwstr(view->window, wline);
2525 if (width < view->ncols)
2526 waddch(view->window, '\n');
2529 err = format_line(&wline, &width, parent_path, view->ncols);
2532 waddwstr(view->window, wline);
2535 if (width < view->ncols)
2536 waddch(view->window, '\n');
2539 waddch(view->window, '\n');
2543 te = SIMPLEQ_FIRST(&entries->head);
2544 if (*first_displayed_entry == NULL) {
2545 if (selected == 0) {
2546 wstandout(view->window);
2547 *selected_entry = NULL;
2549 waddstr(view->window, " ..\n"); /* parent directory */
2551 wstandend(view->window);
2558 while (te != *first_displayed_entry)
2559 te = SIMPLEQ_NEXT(te, entry);
2563 char *line = NULL, *id_str = NULL;
2566 err = got_object_id_str(&id_str, te->id);
2568 return got_error_from_errno();
2570 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2571 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2573 return got_error_from_errno();
2576 err = format_line(&wline, &width, line, view->ncols);
2581 if (n == selected) {
2582 wstandout(view->window);
2583 *selected_entry = te;
2585 waddwstr(view->window, wline);
2586 if (width < view->ncols)
2587 waddch(view->window, '\n');
2589 wstandend(view->window);
2595 *last_displayed_entry = te;
2598 te = SIMPLEQ_NEXT(te, entry);
2606 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2607 const struct got_tree_entries *entries, int isroot)
2609 struct got_tree_entry *te, *prev;
2612 if (*first_displayed_entry == NULL)
2615 te = SIMPLEQ_FIRST(&entries->head);
2616 if (*first_displayed_entry == te) {
2618 *first_displayed_entry = NULL;
2622 /* XXX this is stupid... switch to TAILQ? */
2623 for (i = 0; i < maxscroll; i++) {
2624 while (te != *first_displayed_entry) {
2626 te = SIMPLEQ_NEXT(te, entry);
2628 *first_displayed_entry = prev;
2629 te = SIMPLEQ_FIRST(&entries->head);
2631 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2632 *first_displayed_entry = NULL;
2636 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2637 struct got_tree_entry *last_displayed_entry,
2638 const struct got_tree_entries *entries)
2640 struct got_tree_entry *next;
2643 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2646 if (*first_displayed_entry)
2647 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2649 next = SIMPLEQ_FIRST(&entries->head);
2651 *first_displayed_entry = next;
2652 if (++n >= maxscroll)
2654 next = SIMPLEQ_NEXT(next, entry);
2658 static const struct got_error *
2659 tree_entry_path(char **path, struct tog_parent_trees *parents,
2660 struct got_tree_entry *te)
2662 const struct got_error *err = NULL;
2663 struct tog_parent_tree *pt;
2664 size_t len = 2; /* for leading slash and NUL */
2666 TAILQ_FOREACH(pt, parents, entry)
2667 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2669 len += strlen(te->name);
2671 *path = calloc(1, len);
2673 return got_error_from_errno();
2676 pt = TAILQ_LAST(parents, tog_parent_trees);
2678 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2679 err = got_error(GOT_ERR_NO_SPACE);
2682 if (strlcat(*path, "/", len) >= len) {
2683 err = got_error(GOT_ERR_NO_SPACE);
2686 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2689 if (strlcat(*path, te->name, len) >= len) {
2690 err = got_error(GOT_ERR_NO_SPACE);
2702 static const struct got_error *
2703 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2704 struct got_tree_entry *te, struct tog_parent_trees *parents,
2705 struct got_object_id *commit_id, struct got_repository *repo)
2707 const struct got_error *err = NULL;
2709 struct tog_view *blame_view;
2711 err = tree_entry_path(&path, parents, te);
2715 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2716 if (blame_view == NULL)
2717 return got_error_from_errno();
2719 err = open_blame_view(blame_view, path, commit_id, repo);
2721 view_close(blame_view);
2724 *new_view = blame_view;
2728 static const struct got_error *
2729 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2730 struct got_tree_entry *te, struct tog_parent_trees *parents,
2731 struct got_object_id *commit_id, struct got_repository *repo)
2733 struct tog_view *log_view;
2734 const struct got_error *err = NULL;
2737 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2738 if (log_view == NULL)
2739 return got_error_from_errno();
2741 err = tree_entry_path(&path, parents, te);
2745 err = open_log_view(log_view, commit_id, repo, path);
2747 view_close(log_view);
2749 *new_view = log_view;
2754 static const struct got_error *
2755 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2756 struct got_object_id *commit_id, struct got_repository *repo)
2758 const struct got_error *err = NULL;
2759 char *commit_id_str = NULL;
2760 struct tog_tree_view_state *s = &view->state.tree;
2762 TAILQ_INIT(&s->parents);
2764 err = got_object_id_str(&commit_id_str, commit_id);
2768 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2769 err = got_error_from_errno();
2773 s->root = s->tree = root;
2774 s->entries = got_object_tree_get_entries(root);
2775 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2776 s->commit_id = commit_id;
2779 view->show = show_tree_view;
2780 view->input = input_tree_view;
2781 view->close = close_tree_view;
2783 free(commit_id_str);
2785 free(s->tree_label);
2789 static const struct got_error *
2790 close_tree_view(struct tog_view *view)
2792 struct tog_tree_view_state *s = &view->state.tree;
2794 free(s->tree_label);
2795 while (!TAILQ_EMPTY(&s->parents)) {
2796 struct tog_parent_tree *parent;
2797 parent = TAILQ_FIRST(&s->parents);
2798 TAILQ_REMOVE(&s->parents, parent, entry);
2802 if (s->tree != s->root)
2803 got_object_tree_close(s->tree);
2804 got_object_tree_close(s->root);
2809 static const struct got_error *
2810 show_tree_view(struct tog_view *view)
2812 const struct got_error *err = NULL;
2813 struct tog_tree_view_state *s = &view->state.tree;
2816 err = tree_entry_path(&parent_path, &s->parents, NULL);
2820 err = draw_tree_entries(view, &s->first_displayed_entry,
2821 &s->last_displayed_entry, &s->selected_entry,
2822 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2823 s->entries, s->selected, view->nlines, s->tree == s->root);
2828 static const struct got_error *
2829 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2830 struct tog_view *view, int ch)
2832 const struct got_error *err = NULL;
2833 struct tog_tree_view_state *s = &view->state.tree;
2837 s->show_ids = !s->show_ids;
2840 if (s->selected_entry) {
2841 err = log_tree_entry(new_view, view,
2842 s->selected_entry, &s->parents,
2843 s->commit_id, s->repo);
2848 if (s->selected > 0)
2850 if (s->selected > 0)
2852 tree_scroll_up(&s->first_displayed_entry, 1,
2853 s->entries, s->tree == s->root);
2856 if (SIMPLEQ_FIRST(&s->entries->head) ==
2857 s->first_displayed_entry) {
2858 if (s->tree != s->root)
2859 s->first_displayed_entry = NULL;
2863 tree_scroll_up(&s->first_displayed_entry,
2864 view->nlines, s->entries,
2865 s->tree == s->root);
2869 if (s->selected < s->ndisplayed - 1) {
2873 tree_scroll_down(&s->first_displayed_entry, 1,
2874 s->last_displayed_entry, s->entries);
2877 tree_scroll_down(&s->first_displayed_entry,
2878 view->nlines, s->last_displayed_entry,
2880 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2883 /* can't scroll any further; move cursor down */
2884 if (s->selected < s->ndisplayed - 1)
2885 s->selected = s->ndisplayed - 1;
2889 if (s->selected_entry == NULL) {
2890 struct tog_parent_tree *parent;
2892 /* user selected '..' */
2893 if (s->tree == s->root)
2895 parent = TAILQ_FIRST(&s->parents);
2896 TAILQ_REMOVE(&s->parents, parent,
2898 got_object_tree_close(s->tree);
2899 s->tree = parent->tree;
2901 got_object_tree_get_entries(s->tree);
2902 s->first_displayed_entry =
2903 parent->first_displayed_entry;
2905 parent->selected_entry;
2906 s->selected = parent->selected;
2908 } else if (S_ISDIR(s->selected_entry->mode)) {
2909 struct tog_parent_tree *parent;
2910 struct got_tree_object *child;
2911 err = got_object_open_as_tree(&child,
2912 s->repo, s->selected_entry->id);
2915 parent = calloc(1, sizeof(*parent));
2916 if (parent == NULL) {
2917 err = got_error_from_errno();
2920 parent->tree = s->tree;
2921 parent->first_displayed_entry =
2922 s->first_displayed_entry;
2923 parent->selected_entry = s->selected_entry;
2924 parent->selected = s->selected;
2925 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2928 got_object_tree_get_entries(s->tree);
2930 s->first_displayed_entry = NULL;
2931 } else if (S_ISREG(s->selected_entry->mode)) {
2932 err = blame_tree_entry(new_view, view,
2933 s->selected_entry, &s->parents,
2934 s->commit_id, s->repo);
2940 if (s->selected > view->nlines)
2941 s->selected = s->ndisplayed - 1;
2954 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2959 static const struct got_error *
2960 cmd_tree(int argc, char *argv[])
2962 const struct got_error *error;
2963 struct got_repository *repo = NULL;
2964 char *repo_path = NULL;
2965 struct got_object_id *commit_id = NULL;
2966 char *commit_id_arg = NULL;
2967 struct got_commit_object *commit = NULL;
2968 struct got_tree_object *tree = NULL;
2970 struct tog_view *view;
2973 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2977 while ((ch = getopt(argc, argv, "c:")) != -1) {
2980 commit_id_arg = optarg;
2992 repo_path = getcwd(NULL, 0);
2993 if (repo_path == NULL)
2994 return got_error_from_errno();
2995 } else if (argc == 1) {
2996 repo_path = realpath(argv[0], NULL);
2997 if (repo_path == NULL)
2998 return got_error_from_errno();
3002 error = got_repo_open(&repo, repo_path);
3007 if (commit_id_arg == NULL) {
3008 error = get_head_commit_id(&commit_id, repo);
3012 struct got_object *obj;
3013 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3014 if (error == NULL) {
3015 commit_id = got_object_get_id(obj);
3016 if (commit_id == NULL)
3017 error = got_error_from_errno();
3023 error = got_object_open_as_commit(&commit, repo, commit_id);
3027 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3031 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3033 error = got_error_from_errno();
3036 error = open_tree_view(view, tree, commit_id, repo);
3039 error = view_loop(view);
3043 got_object_commit_close(commit);
3045 got_object_tree_close(tree);
3047 got_repo_close(repo);
3057 intrflush(stdscr, FALSE);
3058 keypad(stdscr, TRUE);
3067 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3068 "Available commands:\n", getprogname());
3069 for (i = 0; i < nitems(tog_commands); i++) {
3070 struct tog_cmd *cmd = &tog_commands[i];
3071 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3077 make_argv(const char *arg0, const char *arg1)
3080 int argc = (arg1 == NULL ? 1 : 2);
3082 argv = calloc(argc, sizeof(char *));
3085 argv[0] = strdup(arg0);
3086 if (argv[0] == NULL)
3089 argv[1] = strdup(arg1);
3090 if (argv[1] == NULL)
3098 main(int argc, char *argv[])
3100 const struct got_error *error = NULL;
3101 struct tog_cmd *cmd = NULL;
3103 char **cmd_argv = NULL;
3105 setlocale(LC_ALL, "");
3107 while ((ch = getopt(argc, argv, "h")) != -1) {
3126 /* Build an argument vector which runs a default command. */
3127 cmd = &tog_commands[0];
3128 cmd_argv = make_argv(cmd->name, NULL);
3133 /* Did the user specific a command? */
3134 for (i = 0; i < nitems(tog_commands); i++) {
3135 if (strncmp(tog_commands[i].name, argv[0],
3136 strlen(argv[0])) == 0) {
3137 cmd = &tog_commands[i];
3139 tog_commands[i].cmd_usage();
3144 /* Did the user specify a repository? */
3145 char *repo_path = realpath(argv[0], NULL);
3147 struct got_repository *repo;
3148 error = got_repo_open(&repo, repo_path);
3150 got_repo_close(repo);
3152 error = got_error_from_errno();
3155 fprintf(stderr, "%s: '%s' is not a "
3156 "known command\n", getprogname(),
3160 fprintf(stderr, "%s: '%s' is neither a known "
3161 "command nor a path to a repository\n",
3162 getprogname(), argv[0]);
3166 cmd = &tog_commands[0];
3167 cmd_argv = make_argv(cmd->name, repo_path);
3175 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3182 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);