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 {
94 int first_displayed_line;
95 int last_displayed_line;
99 struct commit_queue_entry {
100 TAILQ_ENTRY(commit_queue_entry) entry;
101 struct got_object_id *id;
102 struct got_commit_object *commit;
104 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
105 struct commit_queue {
107 struct commit_queue_head head;
110 struct tog_log_view_state {
111 struct got_commit_graph *graph;
112 struct commit_queue commits;
113 struct commit_queue_entry *first_displayed_entry;
114 struct commit_queue_entry *last_displayed_entry;
115 struct commit_queue_entry *selected_entry;
118 struct got_repository *repo;
121 struct tog_blame_cb_args {
122 pthread_mutex_t *mutex;
123 struct tog_blame_line *lines; /* one per line */
126 struct tog_view *view;
127 struct got_object_id *commit_id;
130 int *first_displayed_line;
131 int *last_displayed_line;
137 struct tog_blame_thread_args {
139 struct got_repository *repo;
140 struct tog_blame_cb_args *cb_args;
147 struct tog_blame_line *lines;
150 struct tog_blame_thread_args thread_args;
151 struct tog_blame_cb_args cb_args;
155 struct tog_blame_view_state {
156 int first_displayed_line;
157 int last_displayed_line;
162 pthread_mutex_t mutex;
163 struct got_object_id_queue blamed_commits;
164 struct got_object_qid *blamed_commit;
166 struct got_repository *repo;
167 struct got_object_id *commit_id;
168 struct tog_blame blame;
171 struct tog_parent_tree {
172 TAILQ_ENTRY(tog_parent_tree) entry;
173 struct got_tree_object *tree;
174 struct got_tree_entry *first_displayed_entry;
175 struct got_tree_entry *selected_entry;
179 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
181 struct tog_tree_view_state {
183 struct got_tree_object *root;
184 struct got_tree_object *tree;
185 const struct got_tree_entries *entries;
186 struct got_tree_entry *first_displayed_entry;
187 struct got_tree_entry *last_displayed_entry;
188 struct got_tree_entry *selected_entry;
189 int nentries, ndisplayed, selected, show_ids;
190 struct tog_parent_trees parents;
191 struct got_object_id *commit_id;
192 struct got_repository *repo;
196 TAILQ_ENTRY(tog_view) entry;
199 int nlines, ncols, begin_y, begin_x;
200 int lines, cols; /* copies of LINES and COLS */
201 struct tog_view *parent;
203 /* type-specific state */
204 enum tog_view_type type;
206 struct tog_diff_view_state diff;
207 struct tog_log_view_state log;
208 struct tog_blame_view_state blame;
209 struct tog_tree_view_state tree;
212 const struct got_error *(*show)(struct tog_view *);
213 const struct got_error *(*input)(struct tog_view **,
214 struct tog_view **, struct tog_view *, int);
215 const struct got_error *(*close)(struct tog_view *);
217 TAILQ_HEAD(tog_view_list_head, tog_view);
219 static const struct got_error *open_diff_view(struct tog_view *,
220 struct got_object *, struct got_object *, struct got_repository *);
221 static const struct got_error *show_diff_view(struct tog_view *);
222 static const struct got_error *input_diff_view(struct tog_view **,
223 struct tog_view **, struct tog_view *, int);
224 static const struct got_error* close_diff_view(struct tog_view *);
226 static const struct got_error *open_log_view(struct tog_view *,
227 struct got_object_id *, struct got_repository *, const char *);
228 static const struct got_error * show_log_view(struct tog_view *);
229 static const struct got_error *input_log_view(struct tog_view **,
230 struct tog_view **, struct tog_view *, int);
231 static const struct got_error *close_log_view(struct tog_view *);
233 static const struct got_error *open_blame_view(struct tog_view *, char *,
234 struct got_object_id *, struct got_repository *);
235 static const struct got_error *show_blame_view(struct tog_view *);
236 static const struct got_error *input_blame_view(struct tog_view **,
237 struct tog_view **, struct tog_view *, int);
238 static const struct got_error *close_blame_view(struct tog_view *);
240 static const struct got_error *open_tree_view(struct tog_view *,
241 struct got_tree_object *, struct got_object_id *, struct got_repository *);
242 static const struct got_error *show_tree_view(struct tog_view *);
243 static const struct got_error *input_tree_view(struct tog_view **,
244 struct tog_view **, struct tog_view *, int);
245 static const struct got_error *close_tree_view(struct tog_view *);
247 static const struct got_error *
248 view_close(struct tog_view *view)
250 const struct got_error *err = NULL;
253 err = view->close(view);
255 del_panel(view->panel);
257 delwin(view->window);
262 static struct tog_view *
263 view_open(int nlines, int ncols, int begin_y, int begin_x,
264 struct tog_view *parent, enum tog_view_type type)
266 struct tog_view *view = calloc(1, sizeof(*view));
271 view->parent = parent;
275 view->nlines = nlines ? nlines : LINES - begin_y;
276 view->ncols = ncols ? ncols : COLS - begin_x;
277 view->begin_y = begin_y;
278 view->begin_x = begin_x;
279 view->window = newwin(nlines, ncols, begin_y, begin_x);
280 if (view->window == NULL) {
284 view->panel = new_panel(view->window);
285 if (view->panel == NULL) {
290 keypad(view->window, TRUE);
294 const struct got_error *
295 view_show(struct tog_view *view)
297 const struct got_error *err;
299 err = view->show(view);
302 show_panel(view->panel);
309 const struct got_error *
310 view_resize(struct tog_view *view)
315 if (view->lines > LINES)
316 nlines = view->nlines - (view->lines - LINES);
318 nlines = view->nlines + (LINES - view->lines);
320 if (view->cols > COLS)
321 ncols = view->ncols - (view->cols - COLS);
323 ncols = view->ncols + (COLS - view->cols);
325 if (wresize(view->window, nlines, ncols) == ERR)
326 return got_error_from_errno();
327 replace_panel(view->panel, view->window);
329 view->nlines = nlines;
340 static const struct got_error *
341 view_input(struct tog_view **new, struct tog_view **dead,
342 struct tog_view **focus, int *done, struct tog_view *view,
343 struct tog_view_list_head *views)
345 const struct got_error *err = NULL;
346 struct tog_view *next, *prev;
352 nodelay(stdscr, FALSE);
353 ch = wgetch(view->window);
354 nodelay(stdscr, TRUE);
359 next = TAILQ_NEXT(view, entry);
363 *focus = TAILQ_FIRST(views);
366 prev = TAILQ_PREV(view, tog_view_list_head, entry);
370 *focus = TAILQ_LAST(views, tog_view_list_head);
373 err = view->input(new, dead, view, ch);
380 err = view_resize(view);
383 err = view->input(new, dead, view, ch);
386 err = view->input(new, dead, view, ch);
393 static const struct got_error *
394 view_loop(struct tog_view *view)
396 const struct got_error *err = NULL;
397 struct tog_view_list_head views;
398 struct tog_view *new_view, *dead_view;
402 TAILQ_INSERT_HEAD(&views, view, entry);
404 while (!TAILQ_EMPTY(&views) && !done) {
405 err = view_show(view);
408 err = view_input(&new_view, &dead_view, &view, &done,
413 TAILQ_INSERT_TAIL(&views, new_view, entry);
417 TAILQ_REMOVE(&views, dead_view, entry);
418 TAILQ_FOREACH(view, &views, entry) {
419 if (view->parent == dead_view)
422 if (dead_view->parent)
423 view = dead_view->parent;
425 view = TAILQ_LAST(&views, tog_view_list_head);
426 err = view_close(dead_view);
432 while (!TAILQ_EMPTY(&views)) {
433 view = TAILQ_FIRST(&views);
434 TAILQ_REMOVE(&views, view, entry);
445 "usage: %s log [-c commit] [-r repository-path] [path]\n",
450 /* Create newly allocated wide-character string equivalent to a byte string. */
451 static const struct got_error *
452 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
455 const struct got_error *err = NULL;
458 *wlen = mbstowcs(NULL, s, 0);
459 if (*wlen == (size_t)-1) {
462 return got_error_from_errno();
464 /* byte string invalid in current encoding; try to "fix" it */
465 err = got_mbsavis(&vis, &vislen, s);
468 *wlen = mbstowcs(NULL, vis, 0);
469 if (*wlen == (size_t)-1) {
470 err = got_error_from_errno(); /* give up */
475 *ws = calloc(*wlen + 1, sizeof(*ws));
477 err = got_error_from_errno();
481 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
482 err = got_error_from_errno();
493 /* Format a line for display, ensuring that it won't overflow a width limit. */
494 static const struct got_error *
495 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
497 const struct got_error *err = NULL;
499 wchar_t *wline = NULL;
506 err = mbs2ws(&wline, &wlen, line);
511 while (i < wlen && cols < wlimit) {
512 int width = wcwidth(wline[i]);
519 if (cols + width <= wlimit) {
525 if (wline[i] == L'\t')
526 cols += TABSIZE - ((cols + 1) % TABSIZE);
530 err = got_error_from_errno();
545 static const struct got_error *
546 draw_commit(struct tog_view *view, struct got_commit_object *commit,
547 struct got_object_id *id)
549 const struct got_error *err = NULL;
550 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
551 char *logmsg0 = NULL, *logmsg = NULL;
552 char *author0 = NULL, *author = NULL;
553 wchar_t *wlogmsg = NULL, *wauthor = NULL;
554 int author_width, logmsg_width;
555 char *newline, *smallerthan;
558 static const size_t date_display_cols = 9;
559 static const size_t author_display_cols = 16;
560 const int avail = view->ncols;
562 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
563 &commit->tm_committer) >= sizeof(datebuf))
564 return got_error(GOT_ERR_NO_SPACE);
566 if (avail < date_display_cols)
567 limit = MIN(sizeof(datebuf) - 1, avail);
569 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
570 waddnstr(view->window, datebuf, limit);
575 author0 = strdup(commit->author);
576 if (author0 == NULL) {
577 err = got_error_from_errno();
581 smallerthan = strchr(author, '<');
585 char *at = strchr(author, '@');
590 err = format_line(&wauthor, &author_width, author, limit);
593 waddwstr(view->window, wauthor);
595 while (col <= avail && author_width < author_display_cols + 1) {
596 waddch(view->window, ' ');
603 logmsg0 = strdup(commit->logmsg);
604 if (logmsg0 == NULL) {
605 err = got_error_from_errno();
609 while (*logmsg == '\n')
611 newline = strchr(logmsg, '\n');
615 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
618 waddwstr(view->window, wlogmsg);
620 while (col <= avail) {
621 waddch(view->window, ' ');
633 static struct commit_queue_entry *
634 alloc_commit_queue_entry(struct got_commit_object *commit,
635 struct got_object_id *id)
637 struct commit_queue_entry *entry;
639 entry = calloc(1, sizeof(*entry));
644 entry->commit = commit;
649 pop_commit(struct commit_queue *commits)
651 struct commit_queue_entry *entry;
653 entry = TAILQ_FIRST(&commits->head);
654 TAILQ_REMOVE(&commits->head, entry, entry);
655 got_object_commit_close(entry->commit);
657 /* Don't free entry->id! It is owned by the commit graph. */
662 free_commits(struct commit_queue *commits)
664 while (!TAILQ_EMPTY(&commits->head))
668 static const struct got_error *
669 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
670 struct got_object_id *start_id, int minqueue, int init,
671 struct got_repository *repo, const char *path)
673 const struct got_error *err = NULL;
674 struct got_object_id *id;
675 struct commit_queue_entry *entry;
676 int nfetched, nqueued = 0, found_obj = 0;
677 int is_root_path = strcmp(path, "/") == 0;
679 err = got_commit_graph_iter_start(graph, start_id);
683 entry = TAILQ_LAST(&commits->head, commit_queue_head);
684 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
687 /* Start ID's commit is already on the queue; skip over it. */
688 err = got_commit_graph_iter_next(&id, graph);
689 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
692 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
698 struct got_commit_object *commit;
700 err = got_commit_graph_iter_next(&id, graph);
702 if (err->code != GOT_ERR_ITER_NEED_MORE)
704 if (nqueued >= minqueue) {
708 err = got_commit_graph_fetch_commits(&nfetched,
717 err = got_object_open_as_commit(&commit, repo, id);
722 struct got_object *obj;
723 struct got_object_qid *pid;
726 err = got_object_open_by_path(&obj, repo, id, path);
728 got_object_commit_close(commit);
729 if (err->code == GOT_ERR_NO_OBJ &&
730 (found_obj || !init)) {
731 /* History stops here. */
732 err = got_error(GOT_ERR_ITER_COMPLETED);
738 pid = SIMPLEQ_FIRST(&commit->parent_ids);
740 struct got_object *pobj;
741 err = got_object_open_by_path(&pobj, repo,
744 if (err->code != GOT_ERR_NO_OBJ) {
745 got_object_close(obj);
746 got_object_commit_close(commit);
752 struct got_object_id *id, *pid;
753 id = got_object_get_id(obj);
755 err = got_error_from_errno();
756 got_object_close(obj);
757 got_object_close(pobj);
760 pid = got_object_get_id(pobj);
762 err = got_error_from_errno();
764 got_object_close(obj);
765 got_object_close(pobj);
769 (got_object_id_cmp(id, pid) != 0);
770 got_object_close(pobj);
775 got_object_close(obj);
777 got_object_commit_close(commit);
782 entry = alloc_commit_queue_entry(commit, id);
784 err = got_error_from_errno();
787 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
795 static const struct got_error *
796 fetch_next_commit(struct commit_queue_entry **pentry,
797 struct commit_queue_entry *entry, struct commit_queue *commits,
798 struct got_commit_graph *graph, struct got_repository *repo,
801 const struct got_error *err = NULL;
805 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
809 /* Next entry to display should now be available. */
810 *pentry = TAILQ_NEXT(entry, entry);
812 return got_error(GOT_ERR_NO_OBJ);
817 static const struct got_error *
818 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
820 const struct got_error *err = NULL;
821 struct got_reference *head_ref;
825 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
829 err = got_ref_resolve(head_id, repo, head_ref);
830 got_ref_close(head_ref);
839 static const struct got_error *
840 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
841 struct commit_queue_entry **selected, struct commit_queue_entry *first,
842 struct commit_queue *commits, int selected_idx, int limit,
843 struct got_commit_graph *graph, struct got_repository *repo,
846 const struct got_error *err = NULL;
847 struct commit_queue_entry *entry;
849 char *id_str, *header;
855 if (ncommits == selected_idx) {
859 entry = TAILQ_NEXT(entry, entry);
863 err = got_object_id_str(&id_str, (*selected)->id);
867 if (path && strcmp(path, "/") != 0) {
868 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
869 err = got_error_from_errno();
873 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
874 err = got_error_from_errno();
879 err = format_line(&wline, &width, header, view->ncols);
886 werase(view->window);
888 waddwstr(view->window, wline);
889 if (width < view->ncols)
890 waddch(view->window, '\n');
899 if (ncommits >= limit - 1)
901 if (ncommits == selected_idx)
902 wstandout(view->window);
903 err = draw_commit(view, entry->commit, entry->id);
904 if (ncommits == selected_idx)
905 wstandend(view->window);
910 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
911 err = queue_commits(graph, commits, entry->id, 1,
914 if (err->code != GOT_ERR_ITER_COMPLETED)
919 entry = TAILQ_NEXT(entry, entry);
928 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
929 struct commit_queue *commits)
931 struct commit_queue_entry *entry;
934 entry = TAILQ_FIRST(&commits->head);
935 if (*first_displayed_entry == entry)
938 entry = *first_displayed_entry;
939 while (entry && nscrolled < maxscroll) {
940 entry = TAILQ_PREV(entry, commit_queue_head, entry);
942 *first_displayed_entry = entry;
948 static const struct got_error *
949 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
950 struct commit_queue_entry *last_displayed_entry,
951 struct commit_queue *commits, struct got_commit_graph *graph,
952 struct got_repository *repo, const char *path)
954 const struct got_error *err = NULL;
955 struct commit_queue_entry *pentry;
959 pentry = TAILQ_NEXT(last_displayed_entry, entry);
960 if (pentry == NULL) {
961 err = fetch_next_commit(&pentry, last_displayed_entry,
962 commits, graph, repo, path);
963 if (err || pentry == NULL)
966 last_displayed_entry = pentry;
968 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
971 *first_displayed_entry = pentry;
972 } while (++nscrolled < maxscroll);
977 static const struct got_error *
978 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
979 struct commit_queue_entry *entry, struct got_repository *repo)
981 const struct got_error *err;
982 struct got_object *obj1 = NULL, *obj2 = NULL;
983 struct got_object_qid *parent_id;
984 struct tog_view *diff_view;
986 err = got_object_open(&obj2, repo, entry->id);
990 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
992 err = got_object_open(&obj1, repo, parent_id->id);
997 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
998 if (diff_view == NULL) {
999 err = got_error_from_errno();
1003 err = open_diff_view(diff_view, obj1, obj2, repo);
1005 *new_view = diff_view;
1008 got_object_close(obj1);
1010 got_object_close(obj2);
1014 static const struct got_error *
1015 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1016 struct commit_queue_entry *entry, struct got_repository *repo)
1018 const struct got_error *err = NULL;
1019 struct got_tree_object *tree;
1020 struct tog_view *tree_view;
1022 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1026 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1027 if (tree_view == NULL)
1028 return got_error_from_errno();
1030 err = open_tree_view(tree_view, tree, entry->id, repo);
1032 got_object_tree_close(tree);
1034 *new_view = tree_view;
1038 static const struct got_error *
1039 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1040 struct got_repository *repo, const char *path)
1042 const struct got_error *err = NULL;
1043 struct got_object_id *head_id = NULL;
1045 struct tog_log_view_state *s = &view->state.log;
1047 err = got_repo_map_path(&s->in_repo_path, repo, path);
1051 err = get_head_commit_id(&head_id, repo);
1055 /* The graph contains all commits. */
1056 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1059 /* The commit queue contains a subset of commits filtered by path. */
1060 TAILQ_INIT(&s->commits.head);
1061 s->commits.ncommits = 0;
1063 /* Populate commit graph with a sufficient number of commits. */
1064 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1070 * Open the initial batch of commits, sorted in commit graph order.
1071 * We keep all commits open throughout the lifetime of the log view
1072 * in order to avoid having to re-fetch commits from disk while
1073 * updating the display.
1075 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1076 repo, s->in_repo_path);
1078 if (err->code != GOT_ERR_ITER_COMPLETED)
1083 s->first_displayed_entry =
1084 TAILQ_FIRST(&s->commits.head);
1085 s->selected_entry = s->first_displayed_entry;
1088 view->show = show_log_view;
1089 view->input = input_log_view;
1090 view->close = close_log_view;
1096 static const struct got_error *
1097 close_log_view(struct tog_view *view)
1099 struct tog_log_view_state *s = &view->state.log;
1102 got_commit_graph_close(s->graph);
1103 free_commits(&s->commits);
1104 free(s->in_repo_path);
1108 static const struct got_error *
1109 show_log_view(struct tog_view *view)
1111 struct tog_log_view_state *s = &view->state.log;
1113 return draw_commits(view, &s->last_displayed_entry,
1114 &s->selected_entry, s->first_displayed_entry,
1115 &s->commits, s->selected, view->nlines, s->graph,
1116 s->repo, s->in_repo_path);
1119 static const struct got_error *
1120 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1121 struct tog_view *view, int ch)
1123 const struct got_error *err = NULL;
1124 struct tog_log_view_state *s = &view->state.log;
1129 if (s->selected > 0)
1131 if (s->selected > 0)
1133 scroll_up(&s->first_displayed_entry, 1,
1137 if (TAILQ_FIRST(&s->commits.head) ==
1138 s->first_displayed_entry) {
1142 scroll_up(&s->first_displayed_entry,
1143 view->nlines, &s->commits);
1147 if (s->selected < MIN(view->nlines - 2,
1148 s->commits.ncommits - 1)) {
1152 err = scroll_down(&s->first_displayed_entry, 1,
1153 s->last_displayed_entry, &s->commits,
1154 s->graph, s->repo, s->in_repo_path);
1156 if (err->code != GOT_ERR_ITER_COMPLETED)
1162 struct commit_queue_entry *first;
1163 first = s->first_displayed_entry;
1164 err = scroll_down(&s->first_displayed_entry,
1165 view->nlines, s->last_displayed_entry,
1166 &s->commits, s->graph, s->repo,
1170 if (err->code != GOT_ERR_ITER_COMPLETED)
1172 if (first == s->first_displayed_entry &&
1173 s->selected < MIN(view->nlines - 2,
1174 s->commits.ncommits - 1)) {
1175 /* can't scroll further down */
1176 s->selected = MIN(view->nlines - 2,
1177 s->commits.ncommits - 1);
1183 if (s->selected > view->nlines - 2)
1184 s->selected = view->nlines - 2;
1185 if (s->selected > s->commits.ncommits - 1)
1186 s->selected = s->commits.ncommits - 1;
1190 err = show_commit(new_view, view, s->selected_entry,
1194 err = browse_commit(new_view, view, s->selected_entry,
1204 static const struct got_error *
1205 cmd_log(int argc, char *argv[])
1207 const struct got_error *error;
1208 struct got_repository *repo = NULL;
1209 struct got_object_id *start_id = NULL;
1210 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1211 char *start_commit = NULL;
1213 struct tog_view *view;
1216 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1220 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1223 start_commit = optarg;
1226 repo_path = realpath(optarg, NULL);
1227 if (repo_path == NULL)
1228 err(1, "-r option");
1242 path = strdup(argv[0]);
1246 return got_error_from_errno();
1248 cwd = getcwd(NULL, 0);
1250 error = got_error_from_errno();
1253 if (repo_path == NULL) {
1254 repo_path = strdup(cwd);
1255 if (repo_path == NULL) {
1256 error = got_error_from_errno();
1261 error = got_repo_open(&repo, repo_path);
1265 if (start_commit == NULL) {
1266 error = get_head_commit_id(&start_id, repo);
1270 struct got_object *obj;
1271 error = got_object_open_by_id_str(&obj, repo, start_commit);
1272 if (error == NULL) {
1273 start_id = got_object_get_id(obj);
1274 if (start_id == NULL)
1275 error = got_error_from_errno();
1282 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1284 error = got_error_from_errno();
1287 error = open_log_view(view, start_id, repo, path);
1290 error = view_loop(view);
1297 got_repo_close(repo);
1305 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1311 parse_next_line(FILE *f, size_t *len)
1316 const char delim[3] = { '\0', '\0', '\0'};
1318 line = fparseln(f, &linelen, &lineno, delim, 0);
1324 static const struct got_error *
1325 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1326 int *last_displayed_line, int *eof, int max_lines)
1328 const struct got_error *err;
1329 int nlines = 0, nprinted = 0;
1336 werase(view->window);
1339 while (nprinted < max_lines) {
1340 line = parse_next_line(f, &len);
1345 if (++nlines < *first_displayed_line) {
1350 err = format_line(&wline, &width, line, view->ncols);
1356 waddwstr(view->window, wline);
1357 if (width < view->ncols)
1358 waddch(view->window, '\n');
1359 if (++nprinted == 1)
1360 *first_displayed_line = nlines;
1365 *last_displayed_line = nlines;
1372 static const struct got_error *
1373 open_diff_view(struct tog_view *view, struct got_object *obj1,
1374 struct got_object *obj2, struct got_repository *repo)
1376 const struct got_error *err;
1379 if (obj1 != NULL && obj2 != NULL &&
1380 got_object_get_type(obj1) != got_object_get_type(obj2))
1381 return got_error(GOT_ERR_OBJ_TYPE);
1385 return got_error_from_errno();
1387 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1388 case GOT_OBJ_TYPE_BLOB:
1389 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1391 case GOT_OBJ_TYPE_TREE:
1392 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1394 case GOT_OBJ_TYPE_COMMIT:
1395 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1398 return got_error(GOT_ERR_OBJ_TYPE);
1403 view->state.diff.f = f;
1404 view->state.diff.first_displayed_line = 1;
1405 view->state.diff.last_displayed_line = view->nlines;
1407 view->show = show_diff_view;
1408 view->input = input_diff_view;
1409 view->close = close_diff_view;
1414 static const struct got_error *
1415 close_diff_view(struct tog_view *view)
1417 const struct got_error *err = NULL;
1419 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1420 err = got_error_from_errno();
1425 static const struct got_error *
1426 show_diff_view(struct tog_view *view)
1428 struct tog_diff_view_state *s = &view->state.diff;
1430 return draw_file(view, s->f, &s->first_displayed_line,
1431 &s->last_displayed_line, &s->eof, view->nlines);
1434 static const struct got_error *
1435 input_diff_view(struct tog_view **new, struct tog_view **dead,
1436 struct tog_view *view, int ch)
1438 struct tog_diff_view_state *s = &view->state.diff;
1444 if (s->first_displayed_line > 1)
1445 s->first_displayed_line--;
1449 while (i++ < view->nlines - 1 &&
1450 s->first_displayed_line > 1)
1451 s->first_displayed_line--;
1456 s->first_displayed_line++;
1461 while (!s->eof && i++ < view->nlines - 1) {
1463 line = parse_next_line(s->f, NULL);
1464 s->first_displayed_line++;
1476 static const struct got_error *
1477 cmd_diff(int argc, char *argv[])
1479 const struct got_error *error = NULL;
1480 struct got_repository *repo = NULL;
1481 struct got_object *obj1 = NULL, *obj2 = NULL;
1482 char *repo_path = NULL;
1483 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1485 struct tog_view *view;
1488 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1492 while ((ch = getopt(argc, argv, "")) != -1) {
1504 usage_diff(); /* TODO show local worktree changes */
1505 } else if (argc == 2) {
1506 repo_path = getcwd(NULL, 0);
1507 if (repo_path == NULL)
1508 return got_error_from_errno();
1509 obj_id_str1 = argv[0];
1510 obj_id_str2 = argv[1];
1511 } else if (argc == 3) {
1512 repo_path = realpath(argv[0], NULL);
1513 if (repo_path == NULL)
1514 return got_error_from_errno();
1515 obj_id_str1 = argv[1];
1516 obj_id_str2 = argv[2];
1520 error = got_repo_open(&repo, repo_path);
1525 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1529 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1533 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1535 error = got_error_from_errno();
1538 error = open_diff_view(view, obj1, obj2, repo);
1541 error = view_loop(view);
1543 got_repo_close(repo);
1545 got_object_close(obj1);
1547 got_object_close(obj2);
1555 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1560 struct tog_blame_line {
1562 struct got_object_id *id;
1565 static const struct got_error *
1566 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1567 const char *path, struct tog_blame_line *lines, int nlines,
1568 int blame_complete, int selected_line, int *first_displayed_line,
1569 int *last_displayed_line, int *eof, int max_lines)
1571 const struct got_error *err;
1572 int lineno = 0, nprinted = 0;
1577 struct tog_blame_line *blame_line;
1578 struct got_object_id *prev_id = NULL;
1581 err = got_object_id_str(&id_str, id);
1586 werase(view->window);
1588 if (asprintf(&line, "commit: %s", id_str) == -1) {
1589 err = got_error_from_errno();
1594 err = format_line(&wline, &width, line, view->ncols);
1597 waddwstr(view->window, wline);
1600 if (width < view->ncols)
1601 waddch(view->window, '\n');
1603 if (asprintf(&line, "[%d/%d] %s%s",
1604 *first_displayed_line - 1 + selected_line, nlines,
1605 blame_complete ? "" : "annotating ", path) == -1) {
1607 return got_error_from_errno();
1610 err = format_line(&wline, &width, line, view->ncols);
1615 waddwstr(view->window, wline);
1618 if (width < view->ncols)
1619 waddch(view->window, '\n');
1622 while (nprinted < max_lines - 2) {
1623 line = parse_next_line(f, &len);
1628 if (++lineno < *first_displayed_line) {
1633 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1634 err = format_line(&wline, &width, line, wlimit);
1640 if (nprinted == selected_line - 1)
1641 wstandout(view->window);
1643 blame_line = &lines[lineno - 1];
1644 if (blame_line->annotated && prev_id &&
1645 got_object_id_cmp(prev_id, blame_line->id) == 0)
1646 waddstr(view->window, " ");
1647 else if (blame_line->annotated) {
1649 err = got_object_id_str(&id_str, blame_line->id);
1655 wprintw(view->window, "%.8s ", id_str);
1657 prev_id = blame_line->id;
1659 waddstr(view->window, "........ ");
1663 waddwstr(view->window, wline);
1664 while (width < wlimit) {
1665 waddch(view->window, ' ');
1668 if (nprinted == selected_line - 1)
1669 wstandend(view->window);
1670 if (++nprinted == 1)
1671 *first_displayed_line = lineno;
1676 *last_displayed_line = lineno;
1683 static const struct got_error *
1684 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1686 const struct got_error *err = NULL;
1687 struct tog_blame_cb_args *a = arg;
1688 struct tog_blame_line *line;
1690 if (nlines != a->nlines ||
1691 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1692 return got_error(GOT_ERR_RANGE);
1694 if (pthread_mutex_lock(a->mutex) != 0)
1695 return got_error_from_errno();
1697 if (*a->quit) { /* user has quit the blame view */
1698 err = got_error(GOT_ERR_ITER_COMPLETED);
1703 goto done; /* no change in this commit */
1705 line = &a->lines[lineno - 1];
1706 if (line->annotated)
1709 line->id = got_object_id_dup(id);
1710 if (line->id == NULL) {
1711 err = got_error_from_errno();
1714 line->annotated = 1;
1716 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1717 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1718 a->last_displayed_line, a->eof, a->view->nlines);
1720 if (pthread_mutex_unlock(a->mutex) != 0)
1721 return got_error_from_errno();
1726 blame_thread(void *arg)
1728 const struct got_error *err;
1729 struct tog_blame_thread_args *ta = arg;
1730 struct tog_blame_cb_args *a = ta->cb_args;
1732 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1733 blame_cb, ta->cb_args);
1735 if (pthread_mutex_lock(a->mutex) != 0)
1736 return (void *)got_error_from_errno();
1738 got_repo_close(ta->repo);
1742 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1743 a->lines, a->nlines, 1, *a->selected_line,
1744 a->first_displayed_line, a->last_displayed_line, a->eof,
1747 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1748 err = got_error_from_errno();
1753 static struct got_object_id *
1754 get_selected_commit_id(struct tog_blame_line *lines,
1755 int first_displayed_line, int selected_line)
1757 struct tog_blame_line *line;
1759 line = &lines[first_displayed_line - 1 + selected_line - 1];
1760 if (!line->annotated)
1766 static const struct got_error *
1767 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1768 struct tog_blame_line *lines, int first_displayed_line,
1769 int selected_line, struct got_repository *repo)
1771 const struct got_error *err = NULL;
1772 struct got_commit_object *commit = NULL;
1773 struct got_object_id *selected_id;
1774 struct got_object_qid *pid;
1779 selected_id = get_selected_commit_id(lines,
1780 first_displayed_line, selected_line);
1781 if (selected_id == NULL)
1784 err = got_object_open(obj, repo, selected_id);
1788 err = got_object_commit_open(&commit, repo, *obj);
1792 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1794 err = got_object_open(pobj, repo, pid->id);
1800 got_object_commit_close(commit);
1804 static const struct got_error *
1805 stop_blame(struct tog_blame *blame)
1807 const struct got_error *err = NULL;
1810 if (blame->thread) {
1811 if (pthread_join(blame->thread, (void **)&err) != 0)
1812 err = got_error_from_errno();
1813 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1815 blame->thread = NULL;
1817 if (blame->thread_args.repo) {
1818 got_repo_close(blame->thread_args.repo);
1819 blame->thread_args.repo = NULL;
1825 for (i = 0; i < blame->nlines; i++)
1826 free(blame->lines[i].id);
1828 blame->lines = NULL;
1829 free(blame->cb_args.commit_id);
1830 blame->cb_args.commit_id = NULL;
1835 static const struct got_error *
1836 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1837 struct tog_view *view, int *blame_complete,
1838 int *first_displayed_line, int *last_displayed_line,
1839 int *selected_line, int *done, int *eof, const char *path,
1840 struct got_object_id *commit_id,
1841 struct got_repository *repo)
1843 const struct got_error *err = NULL;
1844 struct got_blob_object *blob = NULL;
1845 struct got_repository *thread_repo = NULL;
1846 struct got_object *obj;
1848 err = got_object_open_by_path(&obj, repo, commit_id, path);
1851 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1852 err = got_error(GOT_ERR_OBJ_TYPE);
1856 err = got_object_blob_open(&blob, repo, obj, 8192);
1859 blame->f = got_opentemp();
1860 if (blame->f == NULL) {
1861 err = got_error_from_errno();
1864 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1869 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1870 if (blame->lines == NULL) {
1871 err = got_error_from_errno();
1875 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1879 blame->cb_args.view = view;
1880 blame->cb_args.lines = blame->lines;
1881 blame->cb_args.nlines = blame->nlines;
1882 blame->cb_args.mutex = mutex;
1883 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1884 if (blame->cb_args.commit_id == NULL) {
1885 err = got_error_from_errno();
1888 blame->cb_args.f = blame->f;
1889 blame->cb_args.path = path;
1890 blame->cb_args.first_displayed_line = first_displayed_line;
1891 blame->cb_args.selected_line = selected_line;
1892 blame->cb_args.last_displayed_line = last_displayed_line;
1893 blame->cb_args.quit = done;
1894 blame->cb_args.eof = eof;
1896 blame->thread_args.path = path;
1897 blame->thread_args.repo = thread_repo;
1898 blame->thread_args.cb_args = &blame->cb_args;
1899 blame->thread_args.complete = blame_complete;
1900 *blame_complete = 0;
1902 if (pthread_create(&blame->thread, NULL, blame_thread,
1903 &blame->thread_args) != 0) {
1904 err = got_error_from_errno();
1910 got_object_blob_close(blob);
1912 got_object_close(obj);
1918 static const struct got_error *
1919 open_blame_view(struct tog_view *view, char *path,
1920 struct got_object_id *commit_id, struct got_repository *repo)
1922 const struct got_error *err = NULL;
1923 struct tog_blame_view_state *s = &view->state.blame;
1925 SIMPLEQ_INIT(&s->blamed_commits);
1927 if (pthread_mutex_init(&s->mutex, NULL) != 0)
1928 return got_error_from_errno();
1930 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
1934 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
1935 s->first_displayed_line = 1;
1936 s->last_displayed_line = view->nlines;
1937 s->selected_line = 1;
1938 s->blame_complete = 0;
1940 if (s->path == NULL)
1941 return got_error_from_errno();
1943 s->commit_id = commit_id;
1944 memset(&s->blame, 0, sizeof(s->blame));
1946 view->show = show_blame_view;
1947 view->input = input_blame_view;
1948 view->close = close_blame_view;
1950 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
1951 &s->first_displayed_line, &s->last_displayed_line,
1952 &s->selected_line, &s->done, &s->eof, s->path,
1953 s->blamed_commit->id, s->repo);
1956 static const struct got_error *
1957 close_blame_view(struct tog_view *view)
1959 const struct got_error *err = NULL;
1960 struct tog_blame_view_state *s = &view->state.blame;
1962 if (s->blame.thread)
1963 err = stop_blame(&s->blame);
1965 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
1966 struct got_object_qid *blamed_commit;
1967 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
1968 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
1969 got_object_qid_free(blamed_commit);
1977 static const struct got_error *
1978 show_blame_view(struct tog_view *view)
1980 const struct got_error *err = NULL;
1981 struct tog_blame_view_state *s = &view->state.blame;
1983 if (pthread_mutex_lock(&s->mutex) != 0)
1984 return got_error_from_errno();
1986 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
1987 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
1988 s->selected_line, &s->first_displayed_line,
1989 &s->last_displayed_line, &s->eof, view->nlines);
1991 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
1992 err = got_error_from_errno();
1997 static const struct got_error *
1998 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
1999 struct tog_view *view, int ch)
2001 const struct got_error *err = NULL, *thread_err = NULL;
2002 struct got_object *obj = NULL, *pobj = NULL;
2003 struct tog_view *diff_view;
2004 struct tog_blame_view_state *s = &view->state.blame;
2006 if (pthread_mutex_lock(&s->mutex) != 0) {
2007 err = got_error_from_errno();
2014 if (pthread_mutex_unlock(&s->mutex) != 0) {
2015 err = got_error_from_errno();
2018 return stop_blame(&s->blame);
2021 if (s->selected_line > 1)
2023 else if (s->selected_line == 1 &&
2024 s->first_displayed_line > 1)
2025 s->first_displayed_line--;
2028 if (s->first_displayed_line == 1) {
2029 s->selected_line = 1;
2032 if (s->first_displayed_line > view->nlines - 2)
2033 s->first_displayed_line -=
2036 s->first_displayed_line = 1;
2040 if (s->selected_line < view->nlines - 2 &&
2041 s->first_displayed_line +
2042 s->selected_line <= s->blame.nlines)
2044 else if (s->last_displayed_line <
2046 s->first_displayed_line++;
2050 struct got_object_id *id;
2051 id = get_selected_commit_id(s->blame.lines,
2052 s->first_displayed_line, s->selected_line);
2053 if (id == NULL || got_object_id_cmp(id,
2054 s->blamed_commit->id) == 0)
2056 err = open_selected_commit(&pobj, &obj,
2057 s->blame.lines, s->first_displayed_line,
2058 s->selected_line, s->repo);
2061 if (pobj == NULL && obj == NULL)
2063 if (ch == 'p' && pobj == NULL)
2066 if (pthread_mutex_unlock(&s->mutex) != 0) {
2067 err = got_error_from_errno();
2070 thread_err = stop_blame(&s->blame);
2072 if (pthread_mutex_lock(&s->mutex) != 0) {
2073 err = got_error_from_errno();
2078 id = got_object_get_id(ch == 'b' ? obj : pobj);
2079 got_object_close(obj);
2082 got_object_close(pobj);
2086 err = got_error_from_errno();
2089 err = got_object_qid_alloc(
2090 &s->blamed_commit, id);
2094 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2095 s->blamed_commit, entry);
2096 err = run_blame(&s->blame, &s->mutex, view,
2098 &s->first_displayed_line,
2099 &s->last_displayed_line,
2100 &s->selected_line, &s->done, &s->eof,
2101 s->path, s->blamed_commit->id, s->repo);
2107 struct got_object_qid *first;
2108 first = SIMPLEQ_FIRST(&s->blamed_commits);
2109 if (!got_object_id_cmp(first->id, s->commit_id))
2112 if (pthread_mutex_unlock(&s->mutex) != 0) {
2113 err = got_error_from_errno();
2116 thread_err = stop_blame(&s->blame);
2118 if (pthread_mutex_lock(&s->mutex) != 0) {
2119 err = got_error_from_errno();
2124 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2125 got_object_qid_free(s->blamed_commit);
2127 SIMPLEQ_FIRST(&s->blamed_commits);
2128 err = run_blame(&s->blame, &s->mutex, view,
2130 &s->first_displayed_line,
2131 &s->last_displayed_line,
2132 &s->selected_line, &s->done, &s->eof, s->path,
2133 s->blamed_commit->id, s->repo);
2140 err = open_selected_commit(&pobj, &obj,
2141 s->blame.lines, s->first_displayed_line,
2142 s->selected_line, s->repo);
2145 if (pobj == NULL && obj == NULL)
2147 diff_view = view_open(0, 0, 0, 0, view,
2149 if (diff_view == NULL) {
2150 err = got_error_from_errno();
2153 err = open_diff_view(diff_view, pobj, obj,
2156 view_close(diff_view);
2159 *new_view = diff_view;
2161 got_object_close(pobj);
2164 got_object_close(obj);
2171 if (s->last_displayed_line >= s->blame.nlines &&
2172 s->selected_line < view->nlines - 2) {
2173 s->selected_line = MIN(s->blame.nlines,
2177 if (s->last_displayed_line + view->nlines - 2
2179 s->first_displayed_line +=
2182 s->first_displayed_line =
2187 if (s->selected_line > view->nlines - 2) {
2188 s->selected_line = MIN(s->blame.nlines,
2196 if (pthread_mutex_unlock(&s->mutex) != 0)
2197 err = got_error_from_errno();
2200 got_object_close(pobj);
2201 return thread_err ? thread_err : err;
2204 static const struct got_error *
2205 cmd_blame(int argc, char *argv[])
2207 const struct got_error *error;
2208 struct got_repository *repo = NULL;
2209 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2210 struct got_object_id *commit_id = NULL;
2211 char *commit_id_str = NULL;
2213 struct tog_view *view;
2216 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2220 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2223 commit_id_str = optarg;
2226 repo_path = realpath(optarg, NULL);
2227 if (repo_path == NULL)
2228 err(1, "-r option");
2244 cwd = getcwd(NULL, 0);
2246 error = got_error_from_errno();
2249 if (repo_path == NULL) {
2250 repo_path = strdup(cwd);
2251 if (repo_path == NULL) {
2252 error = got_error_from_errno();
2258 error = got_repo_open(&repo, repo_path);
2262 error = got_repo_map_path(&in_repo_path, repo, path);
2266 if (commit_id_str == NULL) {
2267 struct got_reference *head_ref;
2268 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2271 error = got_ref_resolve(&commit_id, repo, head_ref);
2272 got_ref_close(head_ref);
2274 struct got_object *obj;
2275 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2278 commit_id = got_object_get_id(obj);
2279 if (commit_id == NULL)
2280 error = got_error_from_errno();
2281 got_object_close(obj);
2286 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2288 error = got_error_from_errno();
2291 error = open_blame_view(view, in_repo_path, commit_id, repo);
2294 error = view_loop(view);
2300 got_repo_close(repo);
2304 static const struct got_error *
2305 draw_tree_entries(struct tog_view *view,
2306 struct got_tree_entry **first_displayed_entry,
2307 struct got_tree_entry **last_displayed_entry,
2308 struct got_tree_entry **selected_entry, int *ndisplayed,
2309 const char *label, int show_ids, const char *parent_path,
2310 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2312 const struct got_error *err = NULL;
2313 struct got_tree_entry *te;
2319 werase(view->window);
2324 err = format_line(&wline, &width, label, view->ncols);
2327 waddwstr(view->window, wline);
2330 if (width < view->ncols)
2331 waddch(view->window, '\n');
2334 err = format_line(&wline, &width, parent_path, view->ncols);
2337 waddwstr(view->window, wline);
2340 if (width < view->ncols)
2341 waddch(view->window, '\n');
2344 waddch(view->window, '\n');
2348 te = SIMPLEQ_FIRST(&entries->head);
2349 if (*first_displayed_entry == NULL) {
2350 if (selected == 0) {
2351 wstandout(view->window);
2352 *selected_entry = NULL;
2354 waddstr(view->window, " ..\n"); /* parent directory */
2356 wstandend(view->window);
2363 while (te != *first_displayed_entry)
2364 te = SIMPLEQ_NEXT(te, entry);
2368 char *line = NULL, *id_str = NULL;
2371 err = got_object_id_str(&id_str, te->id);
2373 return got_error_from_errno();
2375 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2376 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2378 return got_error_from_errno();
2381 err = format_line(&wline, &width, line, view->ncols);
2386 if (n == selected) {
2387 wstandout(view->window);
2388 *selected_entry = te;
2390 waddwstr(view->window, wline);
2391 if (width < view->ncols)
2392 waddch(view->window, '\n');
2394 wstandend(view->window);
2400 *last_displayed_entry = te;
2403 te = SIMPLEQ_NEXT(te, entry);
2410 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2411 const struct got_tree_entries *entries, int isroot)
2413 struct got_tree_entry *te, *prev;
2416 if (*first_displayed_entry == NULL)
2419 te = SIMPLEQ_FIRST(&entries->head);
2420 if (*first_displayed_entry == te) {
2422 *first_displayed_entry = NULL;
2426 /* XXX this is stupid... switch to TAILQ? */
2427 for (i = 0; i < maxscroll; i++) {
2428 while (te != *first_displayed_entry) {
2430 te = SIMPLEQ_NEXT(te, entry);
2432 *first_displayed_entry = prev;
2433 te = SIMPLEQ_FIRST(&entries->head);
2435 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2436 *first_displayed_entry = NULL;
2440 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2441 struct got_tree_entry *last_displayed_entry,
2442 const struct got_tree_entries *entries)
2444 struct got_tree_entry *next;
2447 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2450 if (*first_displayed_entry)
2451 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2453 next = SIMPLEQ_FIRST(&entries->head);
2455 *first_displayed_entry = next;
2456 if (++n >= maxscroll)
2458 next = SIMPLEQ_NEXT(next, entry);
2462 static const struct got_error *
2463 tree_entry_path(char **path, struct tog_parent_trees *parents,
2464 struct got_tree_entry *te)
2466 const struct got_error *err = NULL;
2467 struct tog_parent_tree *pt;
2468 size_t len = 2; /* for leading slash and NUL */
2470 TAILQ_FOREACH(pt, parents, entry)
2471 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2473 len += strlen(te->name);
2475 *path = calloc(1, len);
2477 return got_error_from_errno();
2480 pt = TAILQ_LAST(parents, tog_parent_trees);
2482 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2483 err = got_error(GOT_ERR_NO_SPACE);
2486 if (strlcat(*path, "/", len) >= len) {
2487 err = got_error(GOT_ERR_NO_SPACE);
2490 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2493 if (strlcat(*path, te->name, len) >= len) {
2494 err = got_error(GOT_ERR_NO_SPACE);
2506 static const struct got_error *
2507 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2508 struct got_tree_entry *te, struct tog_parent_trees *parents,
2509 struct got_object_id *commit_id, struct got_repository *repo)
2511 const struct got_error *err = NULL;
2513 struct tog_view *blame_view;
2515 err = tree_entry_path(&path, parents, te);
2519 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2520 if (blame_view == NULL)
2521 return got_error_from_errno();
2523 err = open_blame_view(blame_view, path, commit_id, repo);
2525 view_close(blame_view);
2528 *new_view = blame_view;
2532 static const struct got_error *
2533 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2534 struct got_tree_entry *te, struct tog_parent_trees *parents,
2535 struct got_object_id *commit_id, struct got_repository *repo)
2537 struct tog_view *log_view;
2538 const struct got_error *err = NULL;
2541 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2542 if (log_view == NULL)
2543 return got_error_from_errno();
2545 err = tree_entry_path(&path, parents, te);
2549 err = open_log_view(log_view, commit_id, repo, path);
2551 view_close(log_view);
2553 *new_view = log_view;
2558 static const struct got_error *
2559 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2560 struct got_object_id *commit_id, struct got_repository *repo)
2562 const struct got_error *err = NULL;
2563 char *commit_id_str = NULL;
2564 struct tog_tree_view_state *s = &view->state.tree;
2566 TAILQ_INIT(&s->parents);
2568 err = got_object_id_str(&commit_id_str, commit_id);
2572 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2573 err = got_error_from_errno();
2577 s->root = s->tree = root;
2578 s->entries = got_object_tree_get_entries(root);
2579 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2580 s->commit_id = commit_id;
2583 view->show = show_tree_view;
2584 view->input = input_tree_view;
2585 view->close = close_tree_view;
2587 free(commit_id_str);
2589 free(s->tree_label);
2593 static const struct got_error *
2594 close_tree_view(struct tog_view *view)
2596 struct tog_tree_view_state *s = &view->state.tree;
2598 free(s->tree_label);
2599 while (!TAILQ_EMPTY(&s->parents)) {
2600 struct tog_parent_tree *parent;
2601 parent = TAILQ_FIRST(&s->parents);
2602 TAILQ_REMOVE(&s->parents, parent, entry);
2606 if (s->tree != s->root)
2607 got_object_tree_close(s->tree);
2608 got_object_tree_close(s->root);
2613 static const struct got_error *
2614 show_tree_view(struct tog_view *view)
2616 const struct got_error *err = NULL;
2617 struct tog_tree_view_state *s = &view->state.tree;
2620 err = tree_entry_path(&parent_path, &s->parents, NULL);
2624 err = draw_tree_entries(view, &s->first_displayed_entry,
2625 &s->last_displayed_entry, &s->selected_entry,
2626 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2627 s->entries, s->selected, view->nlines, s->tree == s->root);
2632 static const struct got_error *
2633 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2634 struct tog_view *view, int ch)
2636 const struct got_error *err = NULL;
2637 struct tog_tree_view_state *s = &view->state.tree;
2641 s->show_ids = !s->show_ids;
2644 if (s->selected_entry) {
2645 err = log_tree_entry(new_view, view,
2646 s->selected_entry, &s->parents,
2647 s->commit_id, s->repo);
2652 if (s->selected > 0)
2654 if (s->selected > 0)
2656 tree_scroll_up(&s->first_displayed_entry, 1,
2657 s->entries, s->tree == s->root);
2660 if (SIMPLEQ_FIRST(&s->entries->head) ==
2661 s->first_displayed_entry) {
2662 if (s->tree != s->root)
2663 s->first_displayed_entry = NULL;
2667 tree_scroll_up(&s->first_displayed_entry,
2668 view->nlines, s->entries,
2669 s->tree == s->root);
2673 if (s->selected < s->ndisplayed - 1) {
2677 tree_scroll_down(&s->first_displayed_entry, 1,
2678 s->last_displayed_entry, s->entries);
2681 tree_scroll_down(&s->first_displayed_entry,
2682 view->nlines, s->last_displayed_entry,
2684 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2687 /* can't scroll any further; move cursor down */
2688 if (s->selected < s->ndisplayed - 1)
2689 s->selected = s->ndisplayed - 1;
2693 if (s->selected_entry == NULL) {
2694 struct tog_parent_tree *parent;
2696 /* user selected '..' */
2697 if (s->tree == s->root)
2699 parent = TAILQ_FIRST(&s->parents);
2700 TAILQ_REMOVE(&s->parents, parent,
2702 got_object_tree_close(s->tree);
2703 s->tree = parent->tree;
2705 got_object_tree_get_entries(s->tree);
2706 s->first_displayed_entry =
2707 parent->first_displayed_entry;
2709 parent->selected_entry;
2710 s->selected = parent->selected;
2712 } else if (S_ISDIR(s->selected_entry->mode)) {
2713 struct tog_parent_tree *parent;
2714 struct got_tree_object *child;
2715 err = got_object_open_as_tree(&child,
2716 s->repo, s->selected_entry->id);
2719 parent = calloc(1, sizeof(*parent));
2720 if (parent == NULL) {
2721 err = got_error_from_errno();
2724 parent->tree = s->tree;
2725 parent->first_displayed_entry =
2726 s->first_displayed_entry;
2727 parent->selected_entry = s->selected_entry;
2728 parent->selected = s->selected;
2729 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2732 got_object_tree_get_entries(s->tree);
2734 s->first_displayed_entry = NULL;
2735 } else if (S_ISREG(s->selected_entry->mode)) {
2736 err = blame_tree_entry(new_view, view,
2737 s->selected_entry, &s->parents,
2738 s->commit_id, s->repo);
2744 if (s->selected > view->nlines)
2745 s->selected = s->ndisplayed - 1;
2758 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2763 static const struct got_error *
2764 cmd_tree(int argc, char *argv[])
2766 const struct got_error *error;
2767 struct got_repository *repo = NULL;
2768 char *repo_path = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 char *commit_id_arg = NULL;
2771 struct got_commit_object *commit = NULL;
2772 struct got_tree_object *tree = NULL;
2774 struct tog_view *view;
2777 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2781 while ((ch = getopt(argc, argv, "c:")) != -1) {
2784 commit_id_arg = optarg;
2796 repo_path = getcwd(NULL, 0);
2797 if (repo_path == NULL)
2798 return got_error_from_errno();
2799 } else if (argc == 1) {
2800 repo_path = realpath(argv[0], NULL);
2801 if (repo_path == NULL)
2802 return got_error_from_errno();
2806 error = got_repo_open(&repo, repo_path);
2811 if (commit_id_arg == NULL) {
2812 error = get_head_commit_id(&commit_id, repo);
2816 struct got_object *obj;
2817 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2818 if (error == NULL) {
2819 commit_id = got_object_get_id(obj);
2820 if (commit_id == NULL)
2821 error = got_error_from_errno();
2827 error = got_object_open_as_commit(&commit, repo, commit_id);
2831 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2835 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2837 error = got_error_from_errno();
2840 error = open_tree_view(view, tree, commit_id, repo);
2843 error = view_loop(view);
2847 got_object_commit_close(commit);
2849 got_object_tree_close(tree);
2851 got_repo_close(repo);
2861 intrflush(stdscr, FALSE);
2862 keypad(stdscr, TRUE);
2871 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2872 "Available commands:\n", getprogname());
2873 for (i = 0; i < nitems(tog_commands); i++) {
2874 struct tog_cmd *cmd = &tog_commands[i];
2875 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2881 make_argv(const char *arg0, const char *arg1)
2884 int argc = (arg1 == NULL ? 1 : 2);
2886 argv = calloc(argc, sizeof(char *));
2889 argv[0] = strdup(arg0);
2890 if (argv[0] == NULL)
2893 argv[1] = strdup(arg1);
2894 if (argv[1] == NULL)
2902 main(int argc, char *argv[])
2904 const struct got_error *error = NULL;
2905 struct tog_cmd *cmd = NULL;
2907 char **cmd_argv = NULL;
2909 setlocale(LC_ALL, "");
2911 while ((ch = getopt(argc, argv, "h")) != -1) {
2930 /* Build an argument vector which runs a default command. */
2931 cmd = &tog_commands[0];
2932 cmd_argv = make_argv(cmd->name, NULL);
2937 /* Did the user specific a command? */
2938 for (i = 0; i < nitems(tog_commands); i++) {
2939 if (strncmp(tog_commands[i].name, argv[0],
2940 strlen(argv[0])) == 0) {
2941 cmd = &tog_commands[i];
2943 tog_commands[i].cmd_usage();
2948 /* Did the user specify a repository? */
2949 char *repo_path = realpath(argv[0], NULL);
2951 struct got_repository *repo;
2952 error = got_repo_open(&repo, repo_path);
2954 got_repo_close(repo);
2956 error = got_error_from_errno();
2959 fprintf(stderr, "%s: '%s' is not a "
2960 "known command\n", getprogname(),
2964 fprintf(stderr, "%s: '%s' is neither a known "
2965 "command nor a path to a repository\n",
2966 getprogname(), argv[0]);
2970 cmd = &tog_commands[0];
2971 cmd_argv = make_argv(cmd->name, repo_path);
2979 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2986 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);