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);
438 static const struct got_error *
439 view_loop(struct tog_view *view)
441 const struct got_error *err = NULL;
442 struct tog_view_list_head views;
443 struct tog_view *new_view, *dead_view;
447 TAILQ_INSERT_HEAD(&views, view, entry);
449 while (!TAILQ_EMPTY(&views) && !done) {
450 err = view_show(view);
453 err = view_input(&new_view, &dead_view, &view, &done,
458 TAILQ_REMOVE(&views, dead_view, entry);
459 if (dead_view->parent)
460 view = dead_view->parent;
462 view = TAILQ_LAST(&views, tog_view_list_head);
463 if (dead_view->child) {
464 TAILQ_REMOVE(&views, dead_view->child, entry);
465 err = view_close(dead_view->child);
469 err = view_close(dead_view);
474 /* TODO: de-duplicate! */
475 TAILQ_INSERT_TAIL(&views, new_view, entry);
476 if (new_view->parent) {
477 err = view_set_child(new_view->parent, new_view);
485 while (!TAILQ_EMPTY(&views)) {
486 view = TAILQ_FIRST(&views);
487 TAILQ_REMOVE(&views, view, entry);
498 "usage: %s log [-c commit] [-r repository-path] [path]\n",
503 /* Create newly allocated wide-character string equivalent to a byte string. */
504 static const struct got_error *
505 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
508 const struct got_error *err = NULL;
511 *wlen = mbstowcs(NULL, s, 0);
512 if (*wlen == (size_t)-1) {
515 return got_error_from_errno();
517 /* byte string invalid in current encoding; try to "fix" it */
518 err = got_mbsavis(&vis, &vislen, s);
521 *wlen = mbstowcs(NULL, vis, 0);
522 if (*wlen == (size_t)-1) {
523 err = got_error_from_errno(); /* give up */
528 *ws = calloc(*wlen + 1, sizeof(*ws));
530 err = got_error_from_errno();
534 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
535 err = got_error_from_errno();
546 /* Format a line for display, ensuring that it won't overflow a width limit. */
547 static const struct got_error *
548 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
550 const struct got_error *err = NULL;
552 wchar_t *wline = NULL;
559 err = mbs2ws(&wline, &wlen, line);
564 while (i < wlen && cols < wlimit) {
565 int width = wcwidth(wline[i]);
572 if (cols + width <= wlimit) {
578 if (wline[i] == L'\t')
579 cols += TABSIZE - ((cols + 1) % TABSIZE);
583 err = got_error_from_errno();
598 static const struct got_error *
599 draw_commit(struct tog_view *view, struct got_commit_object *commit,
600 struct got_object_id *id)
602 const struct got_error *err = NULL;
603 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
604 char *logmsg0 = NULL, *logmsg = NULL;
605 char *author0 = NULL, *author = NULL;
606 wchar_t *wlogmsg = NULL, *wauthor = NULL;
607 int author_width, logmsg_width;
608 char *newline, *smallerthan;
611 static const size_t date_display_cols = 9;
612 static const size_t author_display_cols = 16;
613 const int avail = view->ncols;
615 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
616 &commit->tm_committer) >= sizeof(datebuf))
617 return got_error(GOT_ERR_NO_SPACE);
619 if (avail < date_display_cols)
620 limit = MIN(sizeof(datebuf) - 1, avail);
622 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
623 waddnstr(view->window, datebuf, limit);
628 author0 = strdup(commit->author);
629 if (author0 == NULL) {
630 err = got_error_from_errno();
634 smallerthan = strchr(author, '<');
638 char *at = strchr(author, '@');
643 err = format_line(&wauthor, &author_width, author, limit);
646 waddwstr(view->window, wauthor);
648 while (col <= avail && author_width < author_display_cols + 1) {
649 waddch(view->window, ' ');
656 logmsg0 = strdup(commit->logmsg);
657 if (logmsg0 == NULL) {
658 err = got_error_from_errno();
662 while (*logmsg == '\n')
664 newline = strchr(logmsg, '\n');
668 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
671 waddwstr(view->window, wlogmsg);
673 while (col <= avail) {
674 waddch(view->window, ' ');
686 static struct commit_queue_entry *
687 alloc_commit_queue_entry(struct got_commit_object *commit,
688 struct got_object_id *id)
690 struct commit_queue_entry *entry;
692 entry = calloc(1, sizeof(*entry));
697 entry->commit = commit;
702 pop_commit(struct commit_queue *commits)
704 struct commit_queue_entry *entry;
706 entry = TAILQ_FIRST(&commits->head);
707 TAILQ_REMOVE(&commits->head, entry, entry);
708 got_object_commit_close(entry->commit);
710 /* Don't free entry->id! It is owned by the commit graph. */
715 free_commits(struct commit_queue *commits)
717 while (!TAILQ_EMPTY(&commits->head))
721 static const struct got_error *
722 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
723 struct got_object_id *start_id, int minqueue, int init,
724 struct got_repository *repo, const char *path)
726 const struct got_error *err = NULL;
727 struct got_object_id *id;
728 struct commit_queue_entry *entry;
729 int nfetched, nqueued = 0, found_obj = 0;
730 int is_root_path = strcmp(path, "/") == 0;
732 err = got_commit_graph_iter_start(graph, start_id);
736 entry = TAILQ_LAST(&commits->head, commit_queue_head);
737 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
740 /* Start ID's commit is already on the queue; skip over it. */
741 err = got_commit_graph_iter_next(&id, graph);
742 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
745 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
751 struct got_commit_object *commit;
753 err = got_commit_graph_iter_next(&id, graph);
755 if (err->code != GOT_ERR_ITER_NEED_MORE)
757 if (nqueued >= minqueue) {
761 err = got_commit_graph_fetch_commits(&nfetched,
770 err = got_object_open_as_commit(&commit, repo, id);
775 struct got_object *obj;
776 struct got_object_qid *pid;
779 err = got_object_open_by_path(&obj, repo, id, path);
781 got_object_commit_close(commit);
782 if (err->code == GOT_ERR_NO_OBJ &&
783 (found_obj || !init)) {
784 /* History stops here. */
785 err = got_error(GOT_ERR_ITER_COMPLETED);
791 pid = SIMPLEQ_FIRST(&commit->parent_ids);
793 struct got_object *pobj;
794 err = got_object_open_by_path(&pobj, repo,
797 if (err->code != GOT_ERR_NO_OBJ) {
798 got_object_close(obj);
799 got_object_commit_close(commit);
805 struct got_object_id *id, *pid;
806 id = got_object_get_id(obj);
808 err = got_error_from_errno();
809 got_object_close(obj);
810 got_object_close(pobj);
813 pid = got_object_get_id(pobj);
815 err = got_error_from_errno();
817 got_object_close(obj);
818 got_object_close(pobj);
822 (got_object_id_cmp(id, pid) != 0);
823 got_object_close(pobj);
828 got_object_close(obj);
830 got_object_commit_close(commit);
835 entry = alloc_commit_queue_entry(commit, id);
837 err = got_error_from_errno();
840 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
848 static const struct got_error *
849 fetch_next_commit(struct commit_queue_entry **pentry,
850 struct commit_queue_entry *entry, struct commit_queue *commits,
851 struct got_commit_graph *graph, struct got_repository *repo,
854 const struct got_error *err = NULL;
858 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
862 /* Next entry to display should now be available. */
863 *pentry = TAILQ_NEXT(entry, entry);
865 return got_error(GOT_ERR_NO_OBJ);
870 static const struct got_error *
871 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
873 const struct got_error *err = NULL;
874 struct got_reference *head_ref;
878 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
882 err = got_ref_resolve(head_id, repo, head_ref);
883 got_ref_close(head_ref);
892 static const struct got_error *
893 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
894 struct commit_queue_entry **selected, struct commit_queue_entry *first,
895 struct commit_queue *commits, int selected_idx, int limit,
896 struct got_commit_graph *graph, struct got_repository *repo,
899 const struct got_error *err = NULL;
900 struct commit_queue_entry *entry;
902 char *id_str, *header;
908 if (ncommits == selected_idx) {
912 entry = TAILQ_NEXT(entry, entry);
916 err = got_object_id_str(&id_str, (*selected)->id);
920 if (path && strcmp(path, "/") != 0) {
921 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
922 err = got_error_from_errno();
926 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
927 err = got_error_from_errno();
932 err = format_line(&wline, &width, header, view->ncols);
939 werase(view->window);
941 waddwstr(view->window, wline);
942 if (width < view->ncols)
943 waddch(view->window, '\n');
952 if (ncommits >= limit - 1)
954 if (ncommits == selected_idx)
955 wstandout(view->window);
956 err = draw_commit(view, entry->commit, entry->id);
957 if (ncommits == selected_idx)
958 wstandend(view->window);
963 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
964 err = queue_commits(graph, commits, entry->id, 1,
967 if (err->code != GOT_ERR_ITER_COMPLETED)
972 entry = TAILQ_NEXT(entry, entry);
981 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
982 struct commit_queue *commits)
984 struct commit_queue_entry *entry;
987 entry = TAILQ_FIRST(&commits->head);
988 if (*first_displayed_entry == entry)
991 entry = *first_displayed_entry;
992 while (entry && nscrolled < maxscroll) {
993 entry = TAILQ_PREV(entry, commit_queue_head, entry);
995 *first_displayed_entry = entry;
1001 static const struct got_error *
1002 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1003 struct commit_queue_entry *last_displayed_entry,
1004 struct commit_queue *commits, struct got_commit_graph *graph,
1005 struct got_repository *repo, const char *path)
1007 const struct got_error *err = NULL;
1008 struct commit_queue_entry *pentry;
1012 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1013 if (pentry == NULL) {
1014 err = fetch_next_commit(&pentry, last_displayed_entry,
1015 commits, graph, repo, path);
1016 if (err || pentry == NULL)
1019 last_displayed_entry = pentry;
1021 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1024 *first_displayed_entry = pentry;
1025 } while (++nscrolled < maxscroll);
1030 static const struct got_error *
1031 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1032 struct commit_queue_entry *entry, struct got_repository *repo)
1034 const struct got_error *err;
1035 struct got_object *obj1 = NULL, *obj2 = NULL;
1036 struct got_object_qid *parent_id;
1037 struct tog_view *diff_view;
1039 err = got_object_open(&obj2, repo, entry->id);
1043 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1045 err = got_object_open(&obj1, repo, parent_id->id);
1050 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1051 if (diff_view == NULL) {
1052 err = got_error_from_errno();
1056 err = open_diff_view(diff_view, obj1, obj2, repo);
1058 *new_view = diff_view;
1061 got_object_close(obj1);
1063 got_object_close(obj2);
1067 static const struct got_error *
1068 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1069 struct commit_queue_entry *entry, struct got_repository *repo)
1071 const struct got_error *err = NULL;
1072 struct got_tree_object *tree;
1073 struct tog_view *tree_view;
1075 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1079 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1080 if (tree_view == NULL)
1081 return got_error_from_errno();
1083 err = open_tree_view(tree_view, tree, entry->id, repo);
1085 got_object_tree_close(tree);
1087 *new_view = tree_view;
1091 static const struct got_error *
1092 set_child_log_view(struct tog_view *view, struct tog_view *child)
1094 struct tog_log_view_state *s = &view->state.log;
1095 struct tog_diff_view_state *ds;
1096 struct commit_queue_entry *commit, *child_entry = NULL;
1097 int selected_idx = 0;
1099 if (child->type != TOG_VIEW_DIFF)
1101 ds = &child->state.diff;
1103 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1104 if (got_object_id_cmp(commit->id, ds->id) == 0) {
1105 child_entry = commit;
1109 if (child_entry == NULL)
1112 commit = s->first_displayed_entry;
1114 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1115 s->selected_entry = child_entry;
1116 s->selected = selected_idx;
1119 if (commit == s->last_displayed_entry)
1122 commit = TAILQ_NEXT(commit, entry);
1125 return show_log_view(view);
1128 static const struct got_error *
1129 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1130 struct got_repository *repo, const char *path)
1132 const struct got_error *err = NULL;
1133 struct got_object_id *head_id = NULL;
1135 struct tog_log_view_state *s = &view->state.log;
1137 err = got_repo_map_path(&s->in_repo_path, repo, path);
1141 err = get_head_commit_id(&head_id, repo);
1145 /* The graph contains all commits. */
1146 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1149 /* The commit queue contains a subset of commits filtered by path. */
1150 TAILQ_INIT(&s->commits.head);
1151 s->commits.ncommits = 0;
1153 /* Populate commit graph with a sufficient number of commits. */
1154 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1160 * Open the initial batch of commits, sorted in commit graph order.
1161 * We keep all commits open throughout the lifetime of the log view
1162 * in order to avoid having to re-fetch commits from disk while
1163 * updating the display.
1165 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1166 repo, s->in_repo_path);
1168 if (err->code != GOT_ERR_ITER_COMPLETED)
1173 s->first_displayed_entry =
1174 TAILQ_FIRST(&s->commits.head);
1175 s->selected_entry = s->first_displayed_entry;
1178 view->show = show_log_view;
1179 view->input = input_log_view;
1180 view->close = close_log_view;
1181 view->set_child = set_child_log_view;
1187 static const struct got_error *
1188 close_log_view(struct tog_view *view)
1190 struct tog_log_view_state *s = &view->state.log;
1193 got_commit_graph_close(s->graph);
1194 free_commits(&s->commits);
1195 free(s->in_repo_path);
1199 static const struct got_error *
1200 update_diff_child_view(struct tog_view *parent,
1201 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1203 const struct got_error *err = NULL;
1204 struct tog_diff_view_state *ds;
1205 struct got_object *obj1 = NULL, *obj2 = NULL;
1206 struct got_object_qid *parent_id;
1207 struct tog_view *child_view = parent->child;
1209 if (child_view == NULL)
1211 if (child_view->type != TOG_VIEW_DIFF)
1213 ds = &child_view->state.diff;
1214 if (got_object_id_cmp(ds->id, selected_entry->id) == 0)
1217 err = got_object_open(&obj2, repo, selected_entry->id);
1221 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1223 err = got_object_open(&obj1, repo, parent_id->id);
1228 err = close_diff_view(child_view);
1232 err = open_diff_view(child_view, obj1, obj2, repo);
1237 got_object_close(obj1);
1239 got_object_close(obj2);
1243 static const struct got_error *
1244 show_log_view(struct tog_view *view)
1246 const struct got_error *err = NULL;
1247 struct tog_log_view_state *s = &view->state.log;
1249 err = draw_commits(view, &s->last_displayed_entry,
1250 &s->selected_entry, s->first_displayed_entry,
1251 &s->commits, s->selected, view->nlines, s->graph,
1252 s->repo, s->in_repo_path);
1256 return update_diff_child_view(view, s->selected_entry, s->repo);
1259 static const struct got_error *
1260 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1261 struct tog_view *view, int ch)
1263 const struct got_error *err = NULL;
1264 struct tog_log_view_state *s = &view->state.log;
1270 if (s->selected > 0)
1272 if (s->selected > 0)
1274 scroll_up(&s->first_displayed_entry, 1,
1278 if (TAILQ_FIRST(&s->commits.head) ==
1279 s->first_displayed_entry) {
1283 scroll_up(&s->first_displayed_entry,
1284 view->nlines, &s->commits);
1289 if (s->selected < MIN(view->nlines - 2,
1290 s->commits.ncommits - 1)) {
1294 err = scroll_down(&s->first_displayed_entry, 1,
1295 s->last_displayed_entry, &s->commits,
1296 s->graph, s->repo, s->in_repo_path);
1298 if (err->code != GOT_ERR_ITER_COMPLETED)
1304 struct commit_queue_entry *first;
1305 first = s->first_displayed_entry;
1306 err = scroll_down(&s->first_displayed_entry,
1307 view->nlines, s->last_displayed_entry,
1308 &s->commits, s->graph, s->repo,
1312 if (err->code != GOT_ERR_ITER_COMPLETED)
1314 if (first == s->first_displayed_entry &&
1315 s->selected < MIN(view->nlines - 2,
1316 s->commits.ncommits - 1)) {
1317 /* can't scroll further down */
1318 s->selected = MIN(view->nlines - 2,
1319 s->commits.ncommits - 1);
1325 if (s->selected > view->nlines - 2)
1326 s->selected = view->nlines - 2;
1327 if (s->selected > s->commits.ncommits - 1)
1328 s->selected = s->commits.ncommits - 1;
1332 err = show_commit(new_view, view, s->selected_entry,
1336 err = browse_commit(new_view, view, s->selected_entry,
1346 static const struct got_error *
1347 cmd_log(int argc, char *argv[])
1349 const struct got_error *error;
1350 struct got_repository *repo = NULL;
1351 struct got_object_id *start_id = NULL;
1352 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1353 char *start_commit = NULL;
1355 struct tog_view *view;
1358 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1362 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1365 start_commit = optarg;
1368 repo_path = realpath(optarg, NULL);
1369 if (repo_path == NULL)
1370 err(1, "-r option");
1384 path = strdup(argv[0]);
1388 return got_error_from_errno();
1390 cwd = getcwd(NULL, 0);
1392 error = got_error_from_errno();
1395 if (repo_path == NULL) {
1396 repo_path = strdup(cwd);
1397 if (repo_path == NULL) {
1398 error = got_error_from_errno();
1403 error = got_repo_open(&repo, repo_path);
1407 if (start_commit == NULL) {
1408 error = get_head_commit_id(&start_id, repo);
1412 struct got_object *obj;
1413 error = got_object_open_by_id_str(&obj, repo, start_commit);
1414 if (error == NULL) {
1415 start_id = got_object_get_id(obj);
1416 if (start_id == NULL)
1417 error = got_error_from_errno();
1424 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1426 error = got_error_from_errno();
1429 error = open_log_view(view, start_id, repo, path);
1432 error = view_loop(view);
1439 got_repo_close(repo);
1447 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1453 parse_next_line(FILE *f, size_t *len)
1458 const char delim[3] = { '\0', '\0', '\0'};
1460 line = fparseln(f, &linelen, &lineno, delim, 0);
1466 static const struct got_error *
1467 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1468 int *last_displayed_line, int *eof, int max_lines)
1470 const struct got_error *err;
1471 int nlines = 0, nprinted = 0;
1478 werase(view->window);
1481 while (nprinted < max_lines) {
1482 line = parse_next_line(f, &len);
1487 if (++nlines < *first_displayed_line) {
1492 err = format_line(&wline, &width, line, view->ncols);
1498 waddwstr(view->window, wline);
1499 if (width < view->ncols)
1500 waddch(view->window, '\n');
1501 if (++nprinted == 1)
1502 *first_displayed_line = nlines;
1507 *last_displayed_line = nlines;
1514 static const struct got_error *
1515 open_diff_view(struct tog_view *view, struct got_object *obj1,
1516 struct got_object *obj2, struct got_repository *repo)
1518 const struct got_error *err;
1521 if (obj1 != NULL && obj2 != NULL &&
1522 got_object_get_type(obj1) != got_object_get_type(obj2))
1523 return got_error(GOT_ERR_OBJ_TYPE);
1527 return got_error_from_errno();
1529 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1530 case GOT_OBJ_TYPE_BLOB:
1531 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1533 case GOT_OBJ_TYPE_TREE:
1534 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1536 case GOT_OBJ_TYPE_COMMIT:
1537 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1540 return got_error(GOT_ERR_OBJ_TYPE);
1545 view->state.diff.id = got_object_get_id(obj2);
1546 if (view->state.diff.id == NULL)
1547 return got_error_from_errno();
1548 view->state.diff.f = f;
1549 view->state.diff.first_displayed_line = 1;
1550 view->state.diff.last_displayed_line = view->nlines;
1552 view->show = show_diff_view;
1553 view->input = input_diff_view;
1554 view->close = close_diff_view;
1559 static const struct got_error *
1560 close_diff_view(struct tog_view *view)
1562 const struct got_error *err = NULL;
1564 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1565 err = got_error_from_errno();
1566 free(view->state.diff.id);
1570 static const struct got_error *
1571 show_diff_view(struct tog_view *view)
1573 struct tog_diff_view_state *s = &view->state.diff;
1575 return draw_file(view, s->f, &s->first_displayed_line,
1576 &s->last_displayed_line, &s->eof, view->nlines);
1579 static const struct got_error *
1580 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1581 struct tog_view *view, int ch)
1583 const struct got_error *err = NULL;
1584 struct tog_diff_view_state *s = &view->state.diff;
1590 if (s->first_displayed_line > 1)
1591 s->first_displayed_line--;
1595 while (i++ < view->nlines - 1 &&
1596 s->first_displayed_line > 1)
1597 s->first_displayed_line--;
1602 s->first_displayed_line++;
1607 while (!s->eof && i++ < view->nlines - 1) {
1609 line = parse_next_line(s->f, NULL);
1610 s->first_displayed_line++;
1617 struct tog_log_view_state *ls;
1618 struct commit_queue_entry *entry;
1619 struct tog_view *diff_view;
1621 if (view->parent == NULL)
1623 if (view->parent->type != TOG_VIEW_LOG)
1625 ls = &view->parent->state.log;
1628 entry = TAILQ_PREV(ls->selected_entry,
1629 commit_queue_head, entry);
1631 entry = TAILQ_NEXT(ls->selected_entry, entry);
1632 if (entry == NULL) {
1633 err = fetch_next_commit(&entry,
1635 &ls->commits, ls->graph,
1636 ls->repo, ls->in_repo_path);
1643 err = show_commit(&diff_view, view->parent,
1647 *new_view = diff_view;
1658 static const struct got_error *
1659 cmd_diff(int argc, char *argv[])
1661 const struct got_error *error = NULL;
1662 struct got_repository *repo = NULL;
1663 struct got_object *obj1 = NULL, *obj2 = NULL;
1664 char *repo_path = NULL;
1665 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1667 struct tog_view *view;
1670 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1674 while ((ch = getopt(argc, argv, "")) != -1) {
1686 usage_diff(); /* TODO show local worktree changes */
1687 } else if (argc == 2) {
1688 repo_path = getcwd(NULL, 0);
1689 if (repo_path == NULL)
1690 return got_error_from_errno();
1691 obj_id_str1 = argv[0];
1692 obj_id_str2 = argv[1];
1693 } else if (argc == 3) {
1694 repo_path = realpath(argv[0], NULL);
1695 if (repo_path == NULL)
1696 return got_error_from_errno();
1697 obj_id_str1 = argv[1];
1698 obj_id_str2 = argv[2];
1702 error = got_repo_open(&repo, repo_path);
1707 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1711 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1715 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1717 error = got_error_from_errno();
1720 error = open_diff_view(view, obj1, obj2, repo);
1723 error = view_loop(view);
1725 got_repo_close(repo);
1727 got_object_close(obj1);
1729 got_object_close(obj2);
1737 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1742 struct tog_blame_line {
1744 struct got_object_id *id;
1747 static const struct got_error *
1748 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1749 const char *path, struct tog_blame_line *lines, int nlines,
1750 int blame_complete, int selected_line, int *first_displayed_line,
1751 int *last_displayed_line, int *eof, int max_lines)
1753 const struct got_error *err;
1754 int lineno = 0, nprinted = 0;
1759 struct tog_blame_line *blame_line;
1760 struct got_object_id *prev_id = NULL;
1763 err = got_object_id_str(&id_str, id);
1768 werase(view->window);
1770 if (asprintf(&line, "commit: %s", id_str) == -1) {
1771 err = got_error_from_errno();
1776 err = format_line(&wline, &width, line, view->ncols);
1779 waddwstr(view->window, wline);
1782 if (width < view->ncols)
1783 waddch(view->window, '\n');
1785 if (asprintf(&line, "[%d/%d] %s%s",
1786 *first_displayed_line - 1 + selected_line, nlines,
1787 blame_complete ? "" : "annotating ", path) == -1) {
1789 return got_error_from_errno();
1792 err = format_line(&wline, &width, line, view->ncols);
1797 waddwstr(view->window, wline);
1800 if (width < view->ncols)
1801 waddch(view->window, '\n');
1804 while (nprinted < max_lines - 2) {
1805 line = parse_next_line(f, &len);
1810 if (++lineno < *first_displayed_line) {
1815 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1816 err = format_line(&wline, &width, line, wlimit);
1822 if (nprinted == selected_line - 1)
1823 wstandout(view->window);
1825 blame_line = &lines[lineno - 1];
1826 if (blame_line->annotated && prev_id &&
1827 got_object_id_cmp(prev_id, blame_line->id) == 0)
1828 waddstr(view->window, " ");
1829 else if (blame_line->annotated) {
1831 err = got_object_id_str(&id_str, blame_line->id);
1837 wprintw(view->window, "%.8s ", id_str);
1839 prev_id = blame_line->id;
1841 waddstr(view->window, "........ ");
1845 waddwstr(view->window, wline);
1846 while (width < wlimit) {
1847 waddch(view->window, ' ');
1850 if (nprinted == selected_line - 1)
1851 wstandend(view->window);
1852 if (++nprinted == 1)
1853 *first_displayed_line = lineno;
1858 *last_displayed_line = lineno;
1865 static const struct got_error *
1866 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1868 const struct got_error *err = NULL;
1869 struct tog_blame_cb_args *a = arg;
1870 struct tog_blame_line *line;
1872 if (nlines != a->nlines ||
1873 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1874 return got_error(GOT_ERR_RANGE);
1876 if (pthread_mutex_lock(a->mutex) != 0)
1877 return got_error_from_errno();
1879 if (*a->quit) { /* user has quit the blame view */
1880 err = got_error(GOT_ERR_ITER_COMPLETED);
1885 goto done; /* no change in this commit */
1887 line = &a->lines[lineno - 1];
1888 if (line->annotated)
1891 line->id = got_object_id_dup(id);
1892 if (line->id == NULL) {
1893 err = got_error_from_errno();
1896 line->annotated = 1;
1898 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1899 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1900 a->last_displayed_line, a->eof, a->view->nlines);
1902 if (pthread_mutex_unlock(a->mutex) != 0)
1903 return got_error_from_errno();
1908 blame_thread(void *arg)
1910 const struct got_error *err;
1911 struct tog_blame_thread_args *ta = arg;
1912 struct tog_blame_cb_args *a = ta->cb_args;
1914 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1915 blame_cb, ta->cb_args);
1917 if (pthread_mutex_lock(a->mutex) != 0)
1918 return (void *)got_error_from_errno();
1920 got_repo_close(ta->repo);
1924 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1925 a->lines, a->nlines, 1, *a->selected_line,
1926 a->first_displayed_line, a->last_displayed_line, a->eof,
1929 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1930 err = got_error_from_errno();
1935 static struct got_object_id *
1936 get_selected_commit_id(struct tog_blame_line *lines,
1937 int first_displayed_line, int selected_line)
1939 struct tog_blame_line *line;
1941 line = &lines[first_displayed_line - 1 + selected_line - 1];
1942 if (!line->annotated)
1948 static const struct got_error *
1949 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1950 struct tog_blame_line *lines, int first_displayed_line,
1951 int selected_line, struct got_repository *repo)
1953 const struct got_error *err = NULL;
1954 struct got_commit_object *commit = NULL;
1955 struct got_object_id *selected_id;
1956 struct got_object_qid *pid;
1961 selected_id = get_selected_commit_id(lines,
1962 first_displayed_line, selected_line);
1963 if (selected_id == NULL)
1966 err = got_object_open(obj, repo, selected_id);
1970 err = got_object_commit_open(&commit, repo, *obj);
1974 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1976 err = got_object_open(pobj, repo, pid->id);
1982 got_object_commit_close(commit);
1986 static const struct got_error *
1987 stop_blame(struct tog_blame *blame)
1989 const struct got_error *err = NULL;
1992 if (blame->thread) {
1993 if (pthread_join(blame->thread, (void **)&err) != 0)
1994 err = got_error_from_errno();
1995 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1997 blame->thread = NULL;
1999 if (blame->thread_args.repo) {
2000 got_repo_close(blame->thread_args.repo);
2001 blame->thread_args.repo = NULL;
2007 for (i = 0; i < blame->nlines; i++)
2008 free(blame->lines[i].id);
2010 blame->lines = NULL;
2011 free(blame->cb_args.commit_id);
2012 blame->cb_args.commit_id = NULL;
2017 static const struct got_error *
2018 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2019 struct tog_view *view, int *blame_complete,
2020 int *first_displayed_line, int *last_displayed_line,
2021 int *selected_line, int *done, int *eof, const char *path,
2022 struct got_object_id *commit_id,
2023 struct got_repository *repo)
2025 const struct got_error *err = NULL;
2026 struct got_blob_object *blob = NULL;
2027 struct got_repository *thread_repo = NULL;
2028 struct got_object *obj;
2030 err = got_object_open_by_path(&obj, repo, commit_id, path);
2033 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2034 err = got_error(GOT_ERR_OBJ_TYPE);
2038 err = got_object_blob_open(&blob, repo, obj, 8192);
2041 blame->f = got_opentemp();
2042 if (blame->f == NULL) {
2043 err = got_error_from_errno();
2046 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2051 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2052 if (blame->lines == NULL) {
2053 err = got_error_from_errno();
2057 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2061 blame->cb_args.view = view;
2062 blame->cb_args.lines = blame->lines;
2063 blame->cb_args.nlines = blame->nlines;
2064 blame->cb_args.mutex = mutex;
2065 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2066 if (blame->cb_args.commit_id == NULL) {
2067 err = got_error_from_errno();
2070 blame->cb_args.f = blame->f;
2071 blame->cb_args.path = path;
2072 blame->cb_args.first_displayed_line = first_displayed_line;
2073 blame->cb_args.selected_line = selected_line;
2074 blame->cb_args.last_displayed_line = last_displayed_line;
2075 blame->cb_args.quit = done;
2076 blame->cb_args.eof = eof;
2078 blame->thread_args.path = path;
2079 blame->thread_args.repo = thread_repo;
2080 blame->thread_args.cb_args = &blame->cb_args;
2081 blame->thread_args.complete = blame_complete;
2082 *blame_complete = 0;
2084 if (pthread_create(&blame->thread, NULL, blame_thread,
2085 &blame->thread_args) != 0) {
2086 err = got_error_from_errno();
2092 got_object_blob_close(blob);
2094 got_object_close(obj);
2100 static const struct got_error *
2101 open_blame_view(struct tog_view *view, char *path,
2102 struct got_object_id *commit_id, struct got_repository *repo)
2104 const struct got_error *err = NULL;
2105 struct tog_blame_view_state *s = &view->state.blame;
2107 SIMPLEQ_INIT(&s->blamed_commits);
2109 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2110 return got_error_from_errno();
2112 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2116 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2117 s->first_displayed_line = 1;
2118 s->last_displayed_line = view->nlines;
2119 s->selected_line = 1;
2120 s->blame_complete = 0;
2122 if (s->path == NULL)
2123 return got_error_from_errno();
2125 s->commit_id = commit_id;
2126 memset(&s->blame, 0, sizeof(s->blame));
2128 view->show = show_blame_view;
2129 view->input = input_blame_view;
2130 view->close = close_blame_view;
2132 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2133 &s->first_displayed_line, &s->last_displayed_line,
2134 &s->selected_line, &s->done, &s->eof, s->path,
2135 s->blamed_commit->id, s->repo);
2138 static const struct got_error *
2139 close_blame_view(struct tog_view *view)
2141 const struct got_error *err = NULL;
2142 struct tog_blame_view_state *s = &view->state.blame;
2144 if (s->blame.thread)
2145 err = stop_blame(&s->blame);
2147 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2148 struct got_object_qid *blamed_commit;
2149 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2150 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2151 got_object_qid_free(blamed_commit);
2159 static const struct got_error *
2160 show_blame_view(struct tog_view *view)
2162 const struct got_error *err = NULL;
2163 struct tog_blame_view_state *s = &view->state.blame;
2165 if (pthread_mutex_lock(&s->mutex) != 0)
2166 return got_error_from_errno();
2168 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2169 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2170 s->selected_line, &s->first_displayed_line,
2171 &s->last_displayed_line, &s->eof, view->nlines);
2173 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2174 err = got_error_from_errno();
2179 static const struct got_error *
2180 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2181 struct tog_view *view, int ch)
2183 const struct got_error *err = NULL, *thread_err = NULL;
2184 struct got_object *obj = NULL, *pobj = NULL;
2185 struct tog_view *diff_view;
2186 struct tog_blame_view_state *s = &view->state.blame;
2188 if (pthread_mutex_lock(&s->mutex) != 0) {
2189 err = got_error_from_errno();
2196 if (pthread_mutex_unlock(&s->mutex) != 0) {
2197 err = got_error_from_errno();
2200 return stop_blame(&s->blame);
2203 if (s->selected_line > 1)
2205 else if (s->selected_line == 1 &&
2206 s->first_displayed_line > 1)
2207 s->first_displayed_line--;
2210 if (s->first_displayed_line == 1) {
2211 s->selected_line = 1;
2214 if (s->first_displayed_line > view->nlines - 2)
2215 s->first_displayed_line -=
2218 s->first_displayed_line = 1;
2222 if (s->selected_line < view->nlines - 2 &&
2223 s->first_displayed_line +
2224 s->selected_line <= s->blame.nlines)
2226 else if (s->last_displayed_line <
2228 s->first_displayed_line++;
2232 struct got_object_id *id;
2233 id = get_selected_commit_id(s->blame.lines,
2234 s->first_displayed_line, s->selected_line);
2235 if (id == NULL || got_object_id_cmp(id,
2236 s->blamed_commit->id) == 0)
2238 err = open_selected_commit(&pobj, &obj,
2239 s->blame.lines, s->first_displayed_line,
2240 s->selected_line, s->repo);
2243 if (pobj == NULL && obj == NULL)
2245 if (ch == 'p' && pobj == NULL)
2248 if (pthread_mutex_unlock(&s->mutex) != 0) {
2249 err = got_error_from_errno();
2252 thread_err = stop_blame(&s->blame);
2254 if (pthread_mutex_lock(&s->mutex) != 0) {
2255 err = got_error_from_errno();
2260 id = got_object_get_id(ch == 'b' ? obj : pobj);
2261 got_object_close(obj);
2264 got_object_close(pobj);
2268 err = got_error_from_errno();
2271 err = got_object_qid_alloc(
2272 &s->blamed_commit, id);
2276 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2277 s->blamed_commit, entry);
2278 err = run_blame(&s->blame, &s->mutex, view,
2280 &s->first_displayed_line,
2281 &s->last_displayed_line,
2282 &s->selected_line, &s->done, &s->eof,
2283 s->path, s->blamed_commit->id, s->repo);
2289 struct got_object_qid *first;
2290 first = SIMPLEQ_FIRST(&s->blamed_commits);
2291 if (!got_object_id_cmp(first->id, s->commit_id))
2294 if (pthread_mutex_unlock(&s->mutex) != 0) {
2295 err = got_error_from_errno();
2298 thread_err = stop_blame(&s->blame);
2300 if (pthread_mutex_lock(&s->mutex) != 0) {
2301 err = got_error_from_errno();
2306 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2307 got_object_qid_free(s->blamed_commit);
2309 SIMPLEQ_FIRST(&s->blamed_commits);
2310 err = run_blame(&s->blame, &s->mutex, view,
2312 &s->first_displayed_line,
2313 &s->last_displayed_line,
2314 &s->selected_line, &s->done, &s->eof, s->path,
2315 s->blamed_commit->id, s->repo);
2322 err = open_selected_commit(&pobj, &obj,
2323 s->blame.lines, s->first_displayed_line,
2324 s->selected_line, s->repo);
2327 if (pobj == NULL && obj == NULL)
2329 diff_view = view_open(0, 0, 0, 0, view,
2331 if (diff_view == NULL) {
2332 err = got_error_from_errno();
2335 err = open_diff_view(diff_view, pobj, obj,
2338 view_close(diff_view);
2341 *new_view = diff_view;
2343 got_object_close(pobj);
2346 got_object_close(obj);
2353 if (s->last_displayed_line >= s->blame.nlines &&
2354 s->selected_line < view->nlines - 2) {
2355 s->selected_line = MIN(s->blame.nlines,
2359 if (s->last_displayed_line + view->nlines - 2
2361 s->first_displayed_line +=
2364 s->first_displayed_line =
2369 if (s->selected_line > view->nlines - 2) {
2370 s->selected_line = MIN(s->blame.nlines,
2378 if (pthread_mutex_unlock(&s->mutex) != 0)
2379 err = got_error_from_errno();
2382 got_object_close(pobj);
2383 return thread_err ? thread_err : err;
2386 static const struct got_error *
2387 cmd_blame(int argc, char *argv[])
2389 const struct got_error *error;
2390 struct got_repository *repo = NULL;
2391 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2392 struct got_object_id *commit_id = NULL;
2393 char *commit_id_str = NULL;
2395 struct tog_view *view;
2398 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2402 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2405 commit_id_str = optarg;
2408 repo_path = realpath(optarg, NULL);
2409 if (repo_path == NULL)
2410 err(1, "-r option");
2426 cwd = getcwd(NULL, 0);
2428 error = got_error_from_errno();
2431 if (repo_path == NULL) {
2432 repo_path = strdup(cwd);
2433 if (repo_path == NULL) {
2434 error = got_error_from_errno();
2440 error = got_repo_open(&repo, repo_path);
2444 error = got_repo_map_path(&in_repo_path, repo, path);
2448 if (commit_id_str == NULL) {
2449 struct got_reference *head_ref;
2450 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2453 error = got_ref_resolve(&commit_id, repo, head_ref);
2454 got_ref_close(head_ref);
2456 struct got_object *obj;
2457 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2460 commit_id = got_object_get_id(obj);
2461 if (commit_id == NULL)
2462 error = got_error_from_errno();
2463 got_object_close(obj);
2468 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2470 error = got_error_from_errno();
2473 error = open_blame_view(view, in_repo_path, commit_id, repo);
2476 error = view_loop(view);
2482 got_repo_close(repo);
2486 static const struct got_error *
2487 draw_tree_entries(struct tog_view *view,
2488 struct got_tree_entry **first_displayed_entry,
2489 struct got_tree_entry **last_displayed_entry,
2490 struct got_tree_entry **selected_entry, int *ndisplayed,
2491 const char *label, int show_ids, const char *parent_path,
2492 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2494 const struct got_error *err = NULL;
2495 struct got_tree_entry *te;
2501 werase(view->window);
2506 err = format_line(&wline, &width, label, view->ncols);
2509 waddwstr(view->window, wline);
2512 if (width < view->ncols)
2513 waddch(view->window, '\n');
2516 err = format_line(&wline, &width, parent_path, view->ncols);
2519 waddwstr(view->window, wline);
2522 if (width < view->ncols)
2523 waddch(view->window, '\n');
2526 waddch(view->window, '\n');
2530 te = SIMPLEQ_FIRST(&entries->head);
2531 if (*first_displayed_entry == NULL) {
2532 if (selected == 0) {
2533 wstandout(view->window);
2534 *selected_entry = NULL;
2536 waddstr(view->window, " ..\n"); /* parent directory */
2538 wstandend(view->window);
2545 while (te != *first_displayed_entry)
2546 te = SIMPLEQ_NEXT(te, entry);
2550 char *line = NULL, *id_str = NULL;
2553 err = got_object_id_str(&id_str, te->id);
2555 return got_error_from_errno();
2557 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2558 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2560 return got_error_from_errno();
2563 err = format_line(&wline, &width, line, view->ncols);
2568 if (n == selected) {
2569 wstandout(view->window);
2570 *selected_entry = te;
2572 waddwstr(view->window, wline);
2573 if (width < view->ncols)
2574 waddch(view->window, '\n');
2576 wstandend(view->window);
2582 *last_displayed_entry = te;
2585 te = SIMPLEQ_NEXT(te, entry);
2592 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2593 const struct got_tree_entries *entries, int isroot)
2595 struct got_tree_entry *te, *prev;
2598 if (*first_displayed_entry == NULL)
2601 te = SIMPLEQ_FIRST(&entries->head);
2602 if (*first_displayed_entry == te) {
2604 *first_displayed_entry = NULL;
2608 /* XXX this is stupid... switch to TAILQ? */
2609 for (i = 0; i < maxscroll; i++) {
2610 while (te != *first_displayed_entry) {
2612 te = SIMPLEQ_NEXT(te, entry);
2614 *first_displayed_entry = prev;
2615 te = SIMPLEQ_FIRST(&entries->head);
2617 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2618 *first_displayed_entry = NULL;
2622 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2623 struct got_tree_entry *last_displayed_entry,
2624 const struct got_tree_entries *entries)
2626 struct got_tree_entry *next;
2629 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2632 if (*first_displayed_entry)
2633 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2635 next = SIMPLEQ_FIRST(&entries->head);
2637 *first_displayed_entry = next;
2638 if (++n >= maxscroll)
2640 next = SIMPLEQ_NEXT(next, entry);
2644 static const struct got_error *
2645 tree_entry_path(char **path, struct tog_parent_trees *parents,
2646 struct got_tree_entry *te)
2648 const struct got_error *err = NULL;
2649 struct tog_parent_tree *pt;
2650 size_t len = 2; /* for leading slash and NUL */
2652 TAILQ_FOREACH(pt, parents, entry)
2653 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2655 len += strlen(te->name);
2657 *path = calloc(1, len);
2659 return got_error_from_errno();
2662 pt = TAILQ_LAST(parents, tog_parent_trees);
2664 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2665 err = got_error(GOT_ERR_NO_SPACE);
2668 if (strlcat(*path, "/", len) >= len) {
2669 err = got_error(GOT_ERR_NO_SPACE);
2672 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2675 if (strlcat(*path, te->name, len) >= len) {
2676 err = got_error(GOT_ERR_NO_SPACE);
2688 static const struct got_error *
2689 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2690 struct got_tree_entry *te, struct tog_parent_trees *parents,
2691 struct got_object_id *commit_id, struct got_repository *repo)
2693 const struct got_error *err = NULL;
2695 struct tog_view *blame_view;
2697 err = tree_entry_path(&path, parents, te);
2701 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2702 if (blame_view == NULL)
2703 return got_error_from_errno();
2705 err = open_blame_view(blame_view, path, commit_id, repo);
2707 view_close(blame_view);
2710 *new_view = blame_view;
2714 static const struct got_error *
2715 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2716 struct got_tree_entry *te, struct tog_parent_trees *parents,
2717 struct got_object_id *commit_id, struct got_repository *repo)
2719 struct tog_view *log_view;
2720 const struct got_error *err = NULL;
2723 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2724 if (log_view == NULL)
2725 return got_error_from_errno();
2727 err = tree_entry_path(&path, parents, te);
2731 err = open_log_view(log_view, commit_id, repo, path);
2733 view_close(log_view);
2735 *new_view = log_view;
2740 static const struct got_error *
2741 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2742 struct got_object_id *commit_id, struct got_repository *repo)
2744 const struct got_error *err = NULL;
2745 char *commit_id_str = NULL;
2746 struct tog_tree_view_state *s = &view->state.tree;
2748 TAILQ_INIT(&s->parents);
2750 err = got_object_id_str(&commit_id_str, commit_id);
2754 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2755 err = got_error_from_errno();
2759 s->root = s->tree = root;
2760 s->entries = got_object_tree_get_entries(root);
2761 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2762 s->commit_id = commit_id;
2765 view->show = show_tree_view;
2766 view->input = input_tree_view;
2767 view->close = close_tree_view;
2769 free(commit_id_str);
2771 free(s->tree_label);
2775 static const struct got_error *
2776 close_tree_view(struct tog_view *view)
2778 struct tog_tree_view_state *s = &view->state.tree;
2780 free(s->tree_label);
2781 while (!TAILQ_EMPTY(&s->parents)) {
2782 struct tog_parent_tree *parent;
2783 parent = TAILQ_FIRST(&s->parents);
2784 TAILQ_REMOVE(&s->parents, parent, entry);
2788 if (s->tree != s->root)
2789 got_object_tree_close(s->tree);
2790 got_object_tree_close(s->root);
2795 static const struct got_error *
2796 show_tree_view(struct tog_view *view)
2798 const struct got_error *err = NULL;
2799 struct tog_tree_view_state *s = &view->state.tree;
2802 err = tree_entry_path(&parent_path, &s->parents, NULL);
2806 err = draw_tree_entries(view, &s->first_displayed_entry,
2807 &s->last_displayed_entry, &s->selected_entry,
2808 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2809 s->entries, s->selected, view->nlines, s->tree == s->root);
2814 static const struct got_error *
2815 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2816 struct tog_view *view, int ch)
2818 const struct got_error *err = NULL;
2819 struct tog_tree_view_state *s = &view->state.tree;
2823 s->show_ids = !s->show_ids;
2826 if (s->selected_entry) {
2827 err = log_tree_entry(new_view, view,
2828 s->selected_entry, &s->parents,
2829 s->commit_id, s->repo);
2834 if (s->selected > 0)
2836 if (s->selected > 0)
2838 tree_scroll_up(&s->first_displayed_entry, 1,
2839 s->entries, s->tree == s->root);
2842 if (SIMPLEQ_FIRST(&s->entries->head) ==
2843 s->first_displayed_entry) {
2844 if (s->tree != s->root)
2845 s->first_displayed_entry = NULL;
2849 tree_scroll_up(&s->first_displayed_entry,
2850 view->nlines, s->entries,
2851 s->tree == s->root);
2855 if (s->selected < s->ndisplayed - 1) {
2859 tree_scroll_down(&s->first_displayed_entry, 1,
2860 s->last_displayed_entry, s->entries);
2863 tree_scroll_down(&s->first_displayed_entry,
2864 view->nlines, s->last_displayed_entry,
2866 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2869 /* can't scroll any further; move cursor down */
2870 if (s->selected < s->ndisplayed - 1)
2871 s->selected = s->ndisplayed - 1;
2875 if (s->selected_entry == NULL) {
2876 struct tog_parent_tree *parent;
2878 /* user selected '..' */
2879 if (s->tree == s->root)
2881 parent = TAILQ_FIRST(&s->parents);
2882 TAILQ_REMOVE(&s->parents, parent,
2884 got_object_tree_close(s->tree);
2885 s->tree = parent->tree;
2887 got_object_tree_get_entries(s->tree);
2888 s->first_displayed_entry =
2889 parent->first_displayed_entry;
2891 parent->selected_entry;
2892 s->selected = parent->selected;
2894 } else if (S_ISDIR(s->selected_entry->mode)) {
2895 struct tog_parent_tree *parent;
2896 struct got_tree_object *child;
2897 err = got_object_open_as_tree(&child,
2898 s->repo, s->selected_entry->id);
2901 parent = calloc(1, sizeof(*parent));
2902 if (parent == NULL) {
2903 err = got_error_from_errno();
2906 parent->tree = s->tree;
2907 parent->first_displayed_entry =
2908 s->first_displayed_entry;
2909 parent->selected_entry = s->selected_entry;
2910 parent->selected = s->selected;
2911 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2914 got_object_tree_get_entries(s->tree);
2916 s->first_displayed_entry = NULL;
2917 } else if (S_ISREG(s->selected_entry->mode)) {
2918 err = blame_tree_entry(new_view, view,
2919 s->selected_entry, &s->parents,
2920 s->commit_id, s->repo);
2926 if (s->selected > view->nlines)
2927 s->selected = s->ndisplayed - 1;
2940 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2945 static const struct got_error *
2946 cmd_tree(int argc, char *argv[])
2948 const struct got_error *error;
2949 struct got_repository *repo = NULL;
2950 char *repo_path = NULL;
2951 struct got_object_id *commit_id = NULL;
2952 char *commit_id_arg = NULL;
2953 struct got_commit_object *commit = NULL;
2954 struct got_tree_object *tree = NULL;
2956 struct tog_view *view;
2959 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2963 while ((ch = getopt(argc, argv, "c:")) != -1) {
2966 commit_id_arg = optarg;
2978 repo_path = getcwd(NULL, 0);
2979 if (repo_path == NULL)
2980 return got_error_from_errno();
2981 } else if (argc == 1) {
2982 repo_path = realpath(argv[0], NULL);
2983 if (repo_path == NULL)
2984 return got_error_from_errno();
2988 error = got_repo_open(&repo, repo_path);
2993 if (commit_id_arg == NULL) {
2994 error = get_head_commit_id(&commit_id, repo);
2998 struct got_object *obj;
2999 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3000 if (error == NULL) {
3001 commit_id = got_object_get_id(obj);
3002 if (commit_id == NULL)
3003 error = got_error_from_errno();
3009 error = got_object_open_as_commit(&commit, repo, commit_id);
3013 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3017 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3019 error = got_error_from_errno();
3022 error = open_tree_view(view, tree, commit_id, repo);
3025 error = view_loop(view);
3029 got_object_commit_close(commit);
3031 got_object_tree_close(tree);
3033 got_repo_close(repo);
3043 intrflush(stdscr, FALSE);
3044 keypad(stdscr, TRUE);
3053 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3054 "Available commands:\n", getprogname());
3055 for (i = 0; i < nitems(tog_commands); i++) {
3056 struct tog_cmd *cmd = &tog_commands[i];
3057 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3063 make_argv(const char *arg0, const char *arg1)
3066 int argc = (arg1 == NULL ? 1 : 2);
3068 argv = calloc(argc, sizeof(char *));
3071 argv[0] = strdup(arg0);
3072 if (argv[0] == NULL)
3075 argv[1] = strdup(arg1);
3076 if (argv[1] == NULL)
3084 main(int argc, char *argv[])
3086 const struct got_error *error = NULL;
3087 struct tog_cmd *cmd = NULL;
3089 char **cmd_argv = NULL;
3091 setlocale(LC_ALL, "");
3093 while ((ch = getopt(argc, argv, "h")) != -1) {
3112 /* Build an argument vector which runs a default command. */
3113 cmd = &tog_commands[0];
3114 cmd_argv = make_argv(cmd->name, NULL);
3119 /* Did the user specific a command? */
3120 for (i = 0; i < nitems(tog_commands); i++) {
3121 if (strncmp(tog_commands[i].name, argv[0],
3122 strlen(argv[0])) == 0) {
3123 cmd = &tog_commands[i];
3125 tog_commands[i].cmd_usage();
3130 /* Did the user specify a repository? */
3131 char *repo_path = realpath(argv[0], NULL);
3133 struct got_repository *repo;
3134 error = got_repo_open(&repo, repo_path);
3136 got_repo_close(repo);
3138 error = got_error_from_errno();
3141 fprintf(stderr, "%s: '%s' is not a "
3142 "known command\n", getprogname(),
3146 fprintf(stderr, "%s: '%s' is neither a known "
3147 "command nor a path to a repository\n",
3148 getprogname(), argv[0]);
3152 cmd = &tog_commands[0];
3153 cmd_argv = make_argv(cmd->name, repo_path);
3161 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3168 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);