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;
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 err = view->input(new, dead, view, ch);
373 err = view_resize(view);
376 err = view->input(new, dead, view, ch);
379 err = view->input(new, dead, view, ch);
386 static const struct got_error *
387 view_loop(struct tog_view *view)
389 const struct got_error *err = NULL;
390 struct tog_view_list_head views;
391 struct tog_view *new_view, *dead_view;
395 TAILQ_INSERT_HEAD(&views, view, entry);
397 while (!TAILQ_EMPTY(&views) && !done) {
398 err = view_show(view);
401 err = view_input(&new_view, &dead_view, &view, &done,
406 TAILQ_INSERT_TAIL(&views, new_view, entry);
410 TAILQ_REMOVE(&views, dead_view, entry);
411 TAILQ_FOREACH(view, &views, entry) {
412 if (view->parent == dead_view)
415 if (dead_view->parent)
416 view = dead_view->parent;
418 view = TAILQ_LAST(&views, tog_view_list_head);
419 err = view_close(dead_view);
425 while (!TAILQ_EMPTY(&views)) {
426 view = TAILQ_FIRST(&views);
427 TAILQ_REMOVE(&views, view, entry);
438 "usage: %s log [-c commit] [-r repository-path] [path]\n",
443 /* Create newly allocated wide-character string equivalent to a byte string. */
444 static const struct got_error *
445 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
448 const struct got_error *err = NULL;
451 *wlen = mbstowcs(NULL, s, 0);
452 if (*wlen == (size_t)-1) {
455 return got_error_from_errno();
457 /* byte string invalid in current encoding; try to "fix" it */
458 err = got_mbsavis(&vis, &vislen, s);
461 *wlen = mbstowcs(NULL, vis, 0);
462 if (*wlen == (size_t)-1) {
463 err = got_error_from_errno(); /* give up */
468 *ws = calloc(*wlen + 1, sizeof(*ws));
470 err = got_error_from_errno();
474 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
475 err = got_error_from_errno();
486 /* Format a line for display, ensuring that it won't overflow a width limit. */
487 static const struct got_error *
488 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
490 const struct got_error *err = NULL;
492 wchar_t *wline = NULL;
499 err = mbs2ws(&wline, &wlen, line);
504 while (i < wlen && cols < wlimit) {
505 int width = wcwidth(wline[i]);
512 if (cols + width <= wlimit) {
518 if (wline[i] == L'\t')
519 cols += TABSIZE - ((cols + 1) % TABSIZE);
523 err = got_error_from_errno();
538 static const struct got_error *
539 draw_commit(struct tog_view *view, struct got_commit_object *commit,
540 struct got_object_id *id)
542 const struct got_error *err = NULL;
543 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
544 char *logmsg0 = NULL, *logmsg = NULL;
545 char *author0 = NULL, *author = NULL;
546 wchar_t *wlogmsg = NULL, *wauthor = NULL;
547 int author_width, logmsg_width;
548 char *newline, *smallerthan;
551 static const size_t date_display_cols = 9;
552 static const size_t author_display_cols = 16;
553 const int avail = view->ncols;
555 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
556 &commit->tm_committer) >= sizeof(datebuf))
557 return got_error(GOT_ERR_NO_SPACE);
559 if (avail < date_display_cols)
560 limit = MIN(sizeof(datebuf) - 1, avail);
562 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
563 waddnstr(view->window, datebuf, limit);
568 author0 = strdup(commit->author);
569 if (author0 == NULL) {
570 err = got_error_from_errno();
574 smallerthan = strchr(author, '<');
578 char *at = strchr(author, '@');
583 err = format_line(&wauthor, &author_width, author, limit);
586 waddwstr(view->window, wauthor);
588 while (col <= avail && author_width < author_display_cols + 1) {
589 waddch(view->window, ' ');
596 logmsg0 = strdup(commit->logmsg);
597 if (logmsg0 == NULL) {
598 err = got_error_from_errno();
602 while (*logmsg == '\n')
604 newline = strchr(logmsg, '\n');
608 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
611 waddwstr(view->window, wlogmsg);
613 while (col <= avail) {
614 waddch(view->window, ' ');
626 static struct commit_queue_entry *
627 alloc_commit_queue_entry(struct got_commit_object *commit,
628 struct got_object_id *id)
630 struct commit_queue_entry *entry;
632 entry = calloc(1, sizeof(*entry));
637 entry->commit = commit;
642 pop_commit(struct commit_queue *commits)
644 struct commit_queue_entry *entry;
646 entry = TAILQ_FIRST(&commits->head);
647 TAILQ_REMOVE(&commits->head, entry, entry);
648 got_object_commit_close(entry->commit);
650 /* Don't free entry->id! It is owned by the commit graph. */
655 free_commits(struct commit_queue *commits)
657 while (!TAILQ_EMPTY(&commits->head))
661 static const struct got_error *
662 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
663 struct got_object_id *start_id, int minqueue, int init,
664 struct got_repository *repo, const char *path)
666 const struct got_error *err = NULL;
667 struct got_object_id *id;
668 struct commit_queue_entry *entry;
669 int nfetched, nqueued = 0, found_obj = 0;
670 int is_root_path = strcmp(path, "/") == 0;
672 err = got_commit_graph_iter_start(graph, start_id);
676 entry = TAILQ_LAST(&commits->head, commit_queue_head);
677 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
680 /* Start ID's commit is already on the queue; skip over it. */
681 err = got_commit_graph_iter_next(&id, graph);
682 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
685 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
691 struct got_commit_object *commit;
693 err = got_commit_graph_iter_next(&id, graph);
695 if (err->code != GOT_ERR_ITER_NEED_MORE)
697 if (nqueued >= minqueue) {
701 err = got_commit_graph_fetch_commits(&nfetched,
710 err = got_object_open_as_commit(&commit, repo, id);
715 struct got_object *obj;
716 struct got_object_qid *pid;
719 err = got_object_open_by_path(&obj, repo, id, path);
721 got_object_commit_close(commit);
722 if (err->code == GOT_ERR_NO_OBJ &&
723 (found_obj || !init)) {
724 /* History stops here. */
725 err = got_error(GOT_ERR_ITER_COMPLETED);
731 pid = SIMPLEQ_FIRST(&commit->parent_ids);
733 struct got_object *pobj;
734 err = got_object_open_by_path(&pobj, repo,
737 if (err->code != GOT_ERR_NO_OBJ) {
738 got_object_close(obj);
739 got_object_commit_close(commit);
745 struct got_object_id *id, *pid;
746 id = got_object_get_id(obj);
748 err = got_error_from_errno();
749 got_object_close(obj);
750 got_object_close(pobj);
753 pid = got_object_get_id(pobj);
755 err = got_error_from_errno();
757 got_object_close(obj);
758 got_object_close(pobj);
762 (got_object_id_cmp(id, pid) != 0);
763 got_object_close(pobj);
768 got_object_close(obj);
770 got_object_commit_close(commit);
775 entry = alloc_commit_queue_entry(commit, id);
777 err = got_error_from_errno();
780 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
788 static const struct got_error *
789 fetch_next_commit(struct commit_queue_entry **pentry,
790 struct commit_queue_entry *entry, struct commit_queue *commits,
791 struct got_commit_graph *graph, struct got_repository *repo,
794 const struct got_error *err = NULL;
798 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
802 /* Next entry to display should now be available. */
803 *pentry = TAILQ_NEXT(entry, entry);
805 return got_error(GOT_ERR_NO_OBJ);
810 static const struct got_error *
811 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
813 const struct got_error *err = NULL;
814 struct got_reference *head_ref;
818 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
822 err = got_ref_resolve(head_id, repo, head_ref);
823 got_ref_close(head_ref);
832 static const struct got_error *
833 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
834 struct commit_queue_entry **selected, struct commit_queue_entry *first,
835 struct commit_queue *commits, int selected_idx, int limit,
836 struct got_commit_graph *graph, struct got_repository *repo,
839 const struct got_error *err = NULL;
840 struct commit_queue_entry *entry;
842 char *id_str, *header;
848 if (ncommits == selected_idx) {
852 entry = TAILQ_NEXT(entry, entry);
856 err = got_object_id_str(&id_str, (*selected)->id);
860 if (path && strcmp(path, "/") != 0) {
861 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
862 err = got_error_from_errno();
866 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
867 err = got_error_from_errno();
872 err = format_line(&wline, &width, header, view->ncols);
879 werase(view->window);
881 waddwstr(view->window, wline);
882 if (width < view->ncols)
883 waddch(view->window, '\n');
892 if (ncommits >= limit - 1)
894 if (ncommits == selected_idx)
895 wstandout(view->window);
896 err = draw_commit(view, entry->commit, entry->id);
897 if (ncommits == selected_idx)
898 wstandend(view->window);
903 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
904 err = queue_commits(graph, commits, entry->id, 1,
907 if (err->code != GOT_ERR_ITER_COMPLETED)
912 entry = TAILQ_NEXT(entry, entry);
921 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
922 struct commit_queue *commits)
924 struct commit_queue_entry *entry;
927 entry = TAILQ_FIRST(&commits->head);
928 if (*first_displayed_entry == entry)
931 entry = *first_displayed_entry;
932 while (entry && nscrolled < maxscroll) {
933 entry = TAILQ_PREV(entry, commit_queue_head, entry);
935 *first_displayed_entry = entry;
941 static const struct got_error *
942 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
943 struct commit_queue_entry *last_displayed_entry,
944 struct commit_queue *commits, struct got_commit_graph *graph,
945 struct got_repository *repo, const char *path)
947 const struct got_error *err = NULL;
948 struct commit_queue_entry *pentry;
952 pentry = TAILQ_NEXT(last_displayed_entry, entry);
953 if (pentry == NULL) {
954 err = fetch_next_commit(&pentry, last_displayed_entry,
955 commits, graph, repo, path);
956 if (err || pentry == NULL)
959 last_displayed_entry = pentry;
961 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
964 *first_displayed_entry = pentry;
965 } while (++nscrolled < maxscroll);
970 static const struct got_error *
971 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
972 struct commit_queue_entry *entry, struct got_repository *repo)
974 const struct got_error *err;
975 struct got_object *obj1 = NULL, *obj2 = NULL;
976 struct got_object_qid *parent_id;
977 struct tog_view *diff_view;
979 err = got_object_open(&obj2, repo, entry->id);
983 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
985 err = got_object_open(&obj1, repo, parent_id->id);
990 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
991 if (diff_view == NULL) {
992 err = got_error_from_errno();
996 err = open_diff_view(diff_view, obj1, obj2, repo);
998 *new_view = diff_view;
1001 got_object_close(obj1);
1003 got_object_close(obj2);
1007 static const struct got_error *
1008 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1009 struct commit_queue_entry *entry, struct got_repository *repo)
1011 const struct got_error *err = NULL;
1012 struct got_tree_object *tree;
1013 struct tog_view *tree_view;
1015 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1019 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1020 if (tree_view == NULL)
1021 return got_error_from_errno();
1023 err = open_tree_view(tree_view, tree, entry->id, repo);
1025 got_object_tree_close(tree);
1027 *new_view = tree_view;
1031 static const struct got_error *
1032 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1033 struct got_repository *repo, const char *path)
1035 const struct got_error *err = NULL;
1036 struct got_object_id *head_id = NULL;
1038 struct tog_log_view_state *s = &view->state.log;
1040 err = got_repo_map_path(&s->in_repo_path, repo, path);
1044 err = get_head_commit_id(&head_id, repo);
1048 /* The graph contains all commits. */
1049 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1052 /* The commit queue contains a subset of commits filtered by path. */
1053 TAILQ_INIT(&s->commits.head);
1054 s->commits.ncommits = 0;
1056 /* Populate commit graph with a sufficient number of commits. */
1057 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1063 * Open the initial batch of commits, sorted in commit graph order.
1064 * We keep all commits open throughout the lifetime of the log view
1065 * in order to avoid having to re-fetch commits from disk while
1066 * updating the display.
1068 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1069 repo, s->in_repo_path);
1071 if (err->code != GOT_ERR_ITER_COMPLETED)
1076 s->first_displayed_entry =
1077 TAILQ_FIRST(&s->commits.head);
1078 s->selected_entry = s->first_displayed_entry;
1081 view->show = show_log_view;
1082 view->input = input_log_view;
1083 view->close = close_log_view;
1089 static const struct got_error *
1090 close_log_view(struct tog_view *view)
1092 struct tog_log_view_state *s = &view->state.log;
1095 got_commit_graph_close(s->graph);
1096 free_commits(&s->commits);
1097 free(s->in_repo_path);
1101 static const struct got_error *
1102 show_log_view(struct tog_view *view)
1104 struct tog_log_view_state *s = &view->state.log;
1106 return draw_commits(view, &s->last_displayed_entry,
1107 &s->selected_entry, s->first_displayed_entry,
1108 &s->commits, s->selected, view->nlines, s->graph,
1109 s->repo, s->in_repo_path);
1112 static const struct got_error *
1113 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1114 struct tog_view *view, int ch)
1116 const struct got_error *err = NULL;
1117 struct tog_log_view_state *s = &view->state.log;
1122 if (s->selected > 0)
1124 if (s->selected > 0)
1126 scroll_up(&s->first_displayed_entry, 1,
1130 if (TAILQ_FIRST(&s->commits.head) ==
1131 s->first_displayed_entry) {
1135 scroll_up(&s->first_displayed_entry,
1136 view->nlines, &s->commits);
1140 if (s->selected < MIN(view->nlines - 2,
1141 s->commits.ncommits - 1)) {
1145 err = scroll_down(&s->first_displayed_entry, 1,
1146 s->last_displayed_entry, &s->commits,
1147 s->graph, s->repo, s->in_repo_path);
1149 if (err->code != GOT_ERR_ITER_COMPLETED)
1155 struct commit_queue_entry *first;
1156 first = s->first_displayed_entry;
1157 err = scroll_down(&s->first_displayed_entry,
1158 view->nlines, s->last_displayed_entry,
1159 &s->commits, s->graph, s->repo,
1163 if (err->code != GOT_ERR_ITER_COMPLETED)
1165 if (first == s->first_displayed_entry &&
1166 s->selected < MIN(view->nlines - 2,
1167 s->commits.ncommits - 1)) {
1168 /* can't scroll further down */
1169 s->selected = MIN(view->nlines - 2,
1170 s->commits.ncommits - 1);
1176 if (s->selected > view->nlines - 2)
1177 s->selected = view->nlines - 2;
1178 if (s->selected > s->commits.ncommits - 1)
1179 s->selected = s->commits.ncommits - 1;
1183 err = show_commit(new_view, view, s->selected_entry,
1187 err = browse_commit(new_view, view, s->selected_entry,
1197 static const struct got_error *
1198 cmd_log(int argc, char *argv[])
1200 const struct got_error *error;
1201 struct got_repository *repo = NULL;
1202 struct got_object_id *start_id = NULL;
1203 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1204 char *start_commit = NULL;
1206 struct tog_view *view;
1209 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1213 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1216 start_commit = optarg;
1219 repo_path = realpath(optarg, NULL);
1220 if (repo_path == NULL)
1221 err(1, "-r option");
1235 path = strdup(argv[0]);
1239 return got_error_from_errno();
1241 cwd = getcwd(NULL, 0);
1243 error = got_error_from_errno();
1246 if (repo_path == NULL) {
1247 repo_path = strdup(cwd);
1248 if (repo_path == NULL) {
1249 error = got_error_from_errno();
1254 error = got_repo_open(&repo, repo_path);
1258 if (start_commit == NULL) {
1259 error = get_head_commit_id(&start_id, repo);
1263 struct got_object *obj;
1264 error = got_object_open_by_id_str(&obj, repo, start_commit);
1265 if (error == NULL) {
1266 start_id = got_object_get_id(obj);
1267 if (start_id == NULL)
1268 error = got_error_from_errno();
1275 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1277 error = got_error_from_errno();
1280 error = open_log_view(view, start_id, repo, path);
1283 error = view_loop(view);
1290 got_repo_close(repo);
1298 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1304 parse_next_line(FILE *f, size_t *len)
1309 const char delim[3] = { '\0', '\0', '\0'};
1311 line = fparseln(f, &linelen, &lineno, delim, 0);
1317 static const struct got_error *
1318 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1319 int *last_displayed_line, int *eof, int max_lines)
1321 const struct got_error *err;
1322 int nlines = 0, nprinted = 0;
1329 werase(view->window);
1332 while (nprinted < max_lines) {
1333 line = parse_next_line(f, &len);
1338 if (++nlines < *first_displayed_line) {
1343 err = format_line(&wline, &width, line, view->ncols);
1349 waddwstr(view->window, wline);
1350 if (width < view->ncols)
1351 waddch(view->window, '\n');
1352 if (++nprinted == 1)
1353 *first_displayed_line = nlines;
1358 *last_displayed_line = nlines;
1365 static const struct got_error *
1366 open_diff_view(struct tog_view *view, struct got_object *obj1,
1367 struct got_object *obj2, struct got_repository *repo)
1369 const struct got_error *err;
1372 if (obj1 != NULL && obj2 != NULL &&
1373 got_object_get_type(obj1) != got_object_get_type(obj2))
1374 return got_error(GOT_ERR_OBJ_TYPE);
1378 return got_error_from_errno();
1380 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1381 case GOT_OBJ_TYPE_BLOB:
1382 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1384 case GOT_OBJ_TYPE_TREE:
1385 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1387 case GOT_OBJ_TYPE_COMMIT:
1388 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1391 return got_error(GOT_ERR_OBJ_TYPE);
1396 view->state.diff.f = f;
1397 view->state.diff.first_displayed_line = 1;
1398 view->state.diff.last_displayed_line = view->nlines;
1400 view->show = show_diff_view;
1401 view->input = input_diff_view;
1402 view->close = close_diff_view;
1407 static const struct got_error *
1408 close_diff_view(struct tog_view *view)
1410 const struct got_error *err = NULL;
1412 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1413 err = got_error_from_errno();
1418 static const struct got_error *
1419 show_diff_view(struct tog_view *view)
1421 struct tog_diff_view_state *s = &view->state.diff;
1423 return draw_file(view, s->f, &s->first_displayed_line,
1424 &s->last_displayed_line, &s->eof, view->nlines);
1427 static const struct got_error *
1428 input_diff_view(struct tog_view **new, struct tog_view **dead,
1429 struct tog_view *view, int ch)
1431 struct tog_diff_view_state *s = &view->state.diff;
1437 if (s->first_displayed_line > 1)
1438 s->first_displayed_line--;
1443 while (i++ < view->nlines - 1 &&
1444 s->first_displayed_line > 1)
1445 s->first_displayed_line--;
1450 s->first_displayed_line++;
1455 while (!s->eof && i++ < view->nlines - 1) {
1457 line = parse_next_line(s->f, NULL);
1458 s->first_displayed_line++;
1470 static const struct got_error *
1471 cmd_diff(int argc, char *argv[])
1473 const struct got_error *error = NULL;
1474 struct got_repository *repo = NULL;
1475 struct got_object *obj1 = NULL, *obj2 = NULL;
1476 char *repo_path = NULL;
1477 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1479 struct tog_view *view;
1482 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1486 while ((ch = getopt(argc, argv, "")) != -1) {
1498 usage_diff(); /* TODO show local worktree changes */
1499 } else if (argc == 2) {
1500 repo_path = getcwd(NULL, 0);
1501 if (repo_path == NULL)
1502 return got_error_from_errno();
1503 obj_id_str1 = argv[0];
1504 obj_id_str2 = argv[1];
1505 } else if (argc == 3) {
1506 repo_path = realpath(argv[0], NULL);
1507 if (repo_path == NULL)
1508 return got_error_from_errno();
1509 obj_id_str1 = argv[1];
1510 obj_id_str2 = argv[2];
1514 error = got_repo_open(&repo, repo_path);
1519 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1523 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1527 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1529 error = got_error_from_errno();
1532 error = open_diff_view(view, obj1, obj2, repo);
1535 error = view_loop(view);
1537 got_repo_close(repo);
1539 got_object_close(obj1);
1541 got_object_close(obj2);
1549 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1554 struct tog_blame_line {
1556 struct got_object_id *id;
1559 static const struct got_error *
1560 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1561 const char *path, struct tog_blame_line *lines, int nlines,
1562 int blame_complete, int selected_line, int *first_displayed_line,
1563 int *last_displayed_line, int *eof, int max_lines)
1565 const struct got_error *err;
1566 int lineno = 0, nprinted = 0;
1571 struct tog_blame_line *blame_line;
1572 struct got_object_id *prev_id = NULL;
1575 err = got_object_id_str(&id_str, id);
1580 werase(view->window);
1582 if (asprintf(&line, "commit: %s", id_str) == -1) {
1583 err = got_error_from_errno();
1588 err = format_line(&wline, &width, line, view->ncols);
1591 waddwstr(view->window, wline);
1594 if (width < view->ncols)
1595 waddch(view->window, '\n');
1597 if (asprintf(&line, "[%d/%d] %s%s",
1598 *first_displayed_line - 1 + selected_line, nlines,
1599 blame_complete ? "" : "annotating ", path) == -1) {
1601 return got_error_from_errno();
1604 err = format_line(&wline, &width, line, view->ncols);
1609 waddwstr(view->window, wline);
1612 if (width < view->ncols)
1613 waddch(view->window, '\n');
1616 while (nprinted < max_lines - 2) {
1617 line = parse_next_line(f, &len);
1622 if (++lineno < *first_displayed_line) {
1627 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1628 err = format_line(&wline, &width, line, wlimit);
1634 if (nprinted == selected_line - 1)
1635 wstandout(view->window);
1637 blame_line = &lines[lineno - 1];
1638 if (blame_line->annotated && prev_id &&
1639 got_object_id_cmp(prev_id, blame_line->id) == 0)
1640 waddstr(view->window, " ");
1641 else if (blame_line->annotated) {
1643 err = got_object_id_str(&id_str, blame_line->id);
1649 wprintw(view->window, "%.8s ", id_str);
1651 prev_id = blame_line->id;
1653 waddstr(view->window, "........ ");
1657 waddwstr(view->window, wline);
1658 while (width < wlimit) {
1659 waddch(view->window, ' ');
1662 if (nprinted == selected_line - 1)
1663 wstandend(view->window);
1664 if (++nprinted == 1)
1665 *first_displayed_line = lineno;
1670 *last_displayed_line = lineno;
1677 static const struct got_error *
1678 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1680 const struct got_error *err = NULL;
1681 struct tog_blame_cb_args *a = arg;
1682 struct tog_blame_line *line;
1684 if (nlines != a->nlines ||
1685 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1686 return got_error(GOT_ERR_RANGE);
1688 if (pthread_mutex_lock(a->mutex) != 0)
1689 return got_error_from_errno();
1691 if (*a->quit) { /* user has quit the blame view */
1692 err = got_error(GOT_ERR_ITER_COMPLETED);
1697 goto done; /* no change in this commit */
1699 line = &a->lines[lineno - 1];
1700 if (line->annotated)
1703 line->id = got_object_id_dup(id);
1704 if (line->id == NULL) {
1705 err = got_error_from_errno();
1708 line->annotated = 1;
1710 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1711 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1712 a->last_displayed_line, a->eof, a->view->nlines);
1714 if (pthread_mutex_unlock(a->mutex) != 0)
1715 return got_error_from_errno();
1720 blame_thread(void *arg)
1722 const struct got_error *err;
1723 struct tog_blame_thread_args *ta = arg;
1724 struct tog_blame_cb_args *a = ta->cb_args;
1726 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1727 blame_cb, ta->cb_args);
1729 if (pthread_mutex_lock(a->mutex) != 0)
1730 return (void *)got_error_from_errno();
1732 got_repo_close(ta->repo);
1736 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1737 a->lines, a->nlines, 1, *a->selected_line,
1738 a->first_displayed_line, a->last_displayed_line, a->eof,
1741 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1742 err = got_error_from_errno();
1747 static struct got_object_id *
1748 get_selected_commit_id(struct tog_blame_line *lines,
1749 int first_displayed_line, int selected_line)
1751 struct tog_blame_line *line;
1753 line = &lines[first_displayed_line - 1 + selected_line - 1];
1754 if (!line->annotated)
1760 static const struct got_error *
1761 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1762 struct tog_blame_line *lines, int first_displayed_line,
1763 int selected_line, struct got_repository *repo)
1765 const struct got_error *err = NULL;
1766 struct got_commit_object *commit = NULL;
1767 struct got_object_id *selected_id;
1768 struct got_object_qid *pid;
1773 selected_id = get_selected_commit_id(lines,
1774 first_displayed_line, selected_line);
1775 if (selected_id == NULL)
1778 err = got_object_open(obj, repo, selected_id);
1782 err = got_object_commit_open(&commit, repo, *obj);
1786 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1788 err = got_object_open(pobj, repo, pid->id);
1794 got_object_commit_close(commit);
1798 static const struct got_error *
1799 stop_blame(struct tog_blame *blame)
1801 const struct got_error *err = NULL;
1804 if (blame->thread) {
1805 if (pthread_join(blame->thread, (void **)&err) != 0)
1806 err = got_error_from_errno();
1807 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1809 blame->thread = NULL;
1811 if (blame->thread_args.repo) {
1812 got_repo_close(blame->thread_args.repo);
1813 blame->thread_args.repo = NULL;
1819 for (i = 0; i < blame->nlines; i++)
1820 free(blame->lines[i].id);
1822 blame->lines = NULL;
1823 free(blame->cb_args.commit_id);
1824 blame->cb_args.commit_id = NULL;
1829 static const struct got_error *
1830 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1831 struct tog_view *view, int *blame_complete,
1832 int *first_displayed_line, int *last_displayed_line,
1833 int *selected_line, int *done, int *eof, const char *path,
1834 struct got_object_id *commit_id,
1835 struct got_repository *repo)
1837 const struct got_error *err = NULL;
1838 struct got_blob_object *blob = NULL;
1839 struct got_repository *thread_repo = NULL;
1840 struct got_object *obj;
1842 err = got_object_open_by_path(&obj, repo, commit_id, path);
1845 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1846 err = got_error(GOT_ERR_OBJ_TYPE);
1850 err = got_object_blob_open(&blob, repo, obj, 8192);
1853 blame->f = got_opentemp();
1854 if (blame->f == NULL) {
1855 err = got_error_from_errno();
1858 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1863 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1864 if (blame->lines == NULL) {
1865 err = got_error_from_errno();
1869 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1873 blame->cb_args.view = view;
1874 blame->cb_args.lines = blame->lines;
1875 blame->cb_args.nlines = blame->nlines;
1876 blame->cb_args.mutex = mutex;
1877 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1878 if (blame->cb_args.commit_id == NULL) {
1879 err = got_error_from_errno();
1882 blame->cb_args.f = blame->f;
1883 blame->cb_args.path = path;
1884 blame->cb_args.first_displayed_line = first_displayed_line;
1885 blame->cb_args.selected_line = selected_line;
1886 blame->cb_args.last_displayed_line = last_displayed_line;
1887 blame->cb_args.quit = done;
1888 blame->cb_args.eof = eof;
1890 blame->thread_args.path = path;
1891 blame->thread_args.repo = thread_repo;
1892 blame->thread_args.cb_args = &blame->cb_args;
1893 blame->thread_args.complete = blame_complete;
1894 *blame_complete = 0;
1896 if (pthread_create(&blame->thread, NULL, blame_thread,
1897 &blame->thread_args) != 0) {
1898 err = got_error_from_errno();
1904 got_object_blob_close(blob);
1906 got_object_close(obj);
1912 static const struct got_error *
1913 open_blame_view(struct tog_view *view, char *path,
1914 struct got_object_id *commit_id, struct got_repository *repo)
1916 const struct got_error *err = NULL;
1917 struct tog_blame_view_state *s = &view->state.blame;
1919 SIMPLEQ_INIT(&s->blamed_commits);
1921 if (pthread_mutex_init(&s->mutex, NULL) != 0)
1922 return got_error_from_errno();
1924 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
1928 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
1929 s->first_displayed_line = 1;
1930 s->last_displayed_line = view->nlines;
1931 s->selected_line = 1;
1932 s->blame_complete = 0;
1934 if (s->path == NULL)
1935 return got_error_from_errno();
1937 s->commit_id = commit_id;
1938 memset(&s->blame, 0, sizeof(s->blame));
1940 view->show = show_blame_view;
1941 view->input = input_blame_view;
1942 view->close = close_blame_view;
1944 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
1945 &s->first_displayed_line, &s->last_displayed_line,
1946 &s->selected_line, &s->done, &s->eof, s->path,
1947 s->blamed_commit->id, s->repo);
1950 static const struct got_error *
1951 close_blame_view(struct tog_view *view)
1953 const struct got_error *err = NULL;
1954 struct tog_blame_view_state *s = &view->state.blame;
1956 if (s->blame.thread)
1957 err = stop_blame(&s->blame);
1959 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
1960 struct got_object_qid *blamed_commit;
1961 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
1962 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
1963 got_object_qid_free(blamed_commit);
1971 static const struct got_error *
1972 show_blame_view(struct tog_view *view)
1974 const struct got_error *err = NULL;
1975 struct tog_blame_view_state *s = &view->state.blame;
1977 if (pthread_mutex_lock(&s->mutex) != 0)
1978 return got_error_from_errno();
1980 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
1981 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
1982 s->selected_line, &s->first_displayed_line,
1983 &s->last_displayed_line, &s->eof, view->nlines);
1985 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
1986 err = got_error_from_errno();
1991 static const struct got_error *
1992 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
1993 struct tog_view *view, int ch)
1995 const struct got_error *err = NULL, *thread_err = NULL;
1996 struct got_object *obj = NULL, *pobj = NULL;
1997 struct tog_view *diff_view;
1998 struct tog_blame_view_state *s = &view->state.blame;
2000 if (pthread_mutex_lock(&s->mutex) != 0) {
2001 err = got_error_from_errno();
2008 if (pthread_mutex_unlock(&s->mutex) != 0) {
2009 err = got_error_from_errno();
2012 return stop_blame(&s->blame);
2015 if (s->selected_line > 1)
2017 else if (s->selected_line == 1 &&
2018 s->first_displayed_line > 1)
2019 s->first_displayed_line--;
2023 if (s->first_displayed_line == 1) {
2024 s->selected_line = 1;
2027 if (s->first_displayed_line > view->nlines - 2)
2028 s->first_displayed_line -=
2031 s->first_displayed_line = 1;
2035 if (s->selected_line < view->nlines - 2 &&
2036 s->first_displayed_line +
2037 s->selected_line <= s->blame.nlines)
2039 else if (s->last_displayed_line <
2041 s->first_displayed_line++;
2045 struct got_object_id *id;
2046 id = get_selected_commit_id(s->blame.lines,
2047 s->first_displayed_line, s->selected_line);
2048 if (id == NULL || got_object_id_cmp(id,
2049 s->blamed_commit->id) == 0)
2051 err = open_selected_commit(&pobj, &obj,
2052 s->blame.lines, s->first_displayed_line,
2053 s->selected_line, s->repo);
2056 if (pobj == NULL && obj == NULL)
2058 if (ch == 'p' && pobj == NULL)
2061 if (pthread_mutex_unlock(&s->mutex) != 0) {
2062 err = got_error_from_errno();
2065 thread_err = stop_blame(&s->blame);
2067 if (pthread_mutex_lock(&s->mutex) != 0) {
2068 err = got_error_from_errno();
2073 id = got_object_get_id(ch == 'b' ? obj : pobj);
2074 got_object_close(obj);
2077 got_object_close(pobj);
2081 err = got_error_from_errno();
2084 err = got_object_qid_alloc(
2085 &s->blamed_commit, id);
2089 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2090 s->blamed_commit, entry);
2091 err = run_blame(&s->blame, &s->mutex, view,
2093 &s->first_displayed_line,
2094 &s->last_displayed_line,
2095 &s->selected_line, &s->done, &s->eof,
2096 s->path, s->blamed_commit->id, s->repo);
2102 struct got_object_qid *first;
2103 first = SIMPLEQ_FIRST(&s->blamed_commits);
2104 if (!got_object_id_cmp(first->id, s->commit_id))
2107 if (pthread_mutex_unlock(&s->mutex) != 0) {
2108 err = got_error_from_errno();
2111 thread_err = stop_blame(&s->blame);
2113 if (pthread_mutex_lock(&s->mutex) != 0) {
2114 err = got_error_from_errno();
2119 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2120 got_object_qid_free(s->blamed_commit);
2122 SIMPLEQ_FIRST(&s->blamed_commits);
2123 err = run_blame(&s->blame, &s->mutex, view,
2125 &s->first_displayed_line,
2126 &s->last_displayed_line,
2127 &s->selected_line, &s->done, &s->eof, s->path,
2128 s->blamed_commit->id, s->repo);
2135 err = open_selected_commit(&pobj, &obj,
2136 s->blame.lines, s->first_displayed_line,
2137 s->selected_line, s->repo);
2140 if (pobj == NULL && obj == NULL)
2142 diff_view = view_open(0, 0, 0, 0, view,
2144 if (diff_view == NULL) {
2145 err = got_error_from_errno();
2148 err = open_diff_view(diff_view, pobj, obj,
2151 view_close(diff_view);
2154 *new_view = diff_view;
2156 got_object_close(pobj);
2159 got_object_close(obj);
2166 if (s->last_displayed_line >= s->blame.nlines &&
2167 s->selected_line < view->nlines - 2) {
2168 s->selected_line = MIN(s->blame.nlines,
2172 if (s->last_displayed_line + view->nlines - 2
2174 s->first_displayed_line +=
2177 s->first_displayed_line =
2182 if (s->selected_line > view->nlines - 2) {
2183 s->selected_line = MIN(s->blame.nlines,
2191 if (pthread_mutex_unlock(&s->mutex) != 0)
2192 err = got_error_from_errno();
2195 got_object_close(pobj);
2196 return thread_err ? thread_err : err;
2199 static const struct got_error *
2200 cmd_blame(int argc, char *argv[])
2202 const struct got_error *error;
2203 struct got_repository *repo = NULL;
2204 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2205 struct got_object_id *commit_id = NULL;
2206 char *commit_id_str = NULL;
2208 struct tog_view *view;
2211 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2215 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2218 commit_id_str = optarg;
2221 repo_path = realpath(optarg, NULL);
2222 if (repo_path == NULL)
2223 err(1, "-r option");
2239 cwd = getcwd(NULL, 0);
2241 error = got_error_from_errno();
2244 if (repo_path == NULL) {
2245 repo_path = strdup(cwd);
2246 if (repo_path == NULL) {
2247 error = got_error_from_errno();
2253 error = got_repo_open(&repo, repo_path);
2257 error = got_repo_map_path(&in_repo_path, repo, path);
2261 if (commit_id_str == NULL) {
2262 struct got_reference *head_ref;
2263 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2266 error = got_ref_resolve(&commit_id, repo, head_ref);
2267 got_ref_close(head_ref);
2269 struct got_object *obj;
2270 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2273 commit_id = got_object_get_id(obj);
2274 if (commit_id == NULL)
2275 error = got_error_from_errno();
2276 got_object_close(obj);
2281 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2283 error = got_error_from_errno();
2286 error = open_blame_view(view, in_repo_path, commit_id, repo);
2289 error = view_loop(view);
2295 got_repo_close(repo);
2299 static const struct got_error *
2300 draw_tree_entries(struct tog_view *view,
2301 struct got_tree_entry **first_displayed_entry,
2302 struct got_tree_entry **last_displayed_entry,
2303 struct got_tree_entry **selected_entry, int *ndisplayed,
2304 const char *label, int show_ids, const char *parent_path,
2305 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2307 const struct got_error *err = NULL;
2308 struct got_tree_entry *te;
2314 werase(view->window);
2319 err = format_line(&wline, &width, label, view->ncols);
2322 waddwstr(view->window, wline);
2325 if (width < view->ncols)
2326 waddch(view->window, '\n');
2329 err = format_line(&wline, &width, parent_path, view->ncols);
2332 waddwstr(view->window, wline);
2335 if (width < view->ncols)
2336 waddch(view->window, '\n');
2339 waddch(view->window, '\n');
2343 te = SIMPLEQ_FIRST(&entries->head);
2344 if (*first_displayed_entry == NULL) {
2345 if (selected == 0) {
2346 wstandout(view->window);
2347 *selected_entry = NULL;
2349 waddstr(view->window, " ..\n"); /* parent directory */
2351 wstandend(view->window);
2358 while (te != *first_displayed_entry)
2359 te = SIMPLEQ_NEXT(te, entry);
2363 char *line = NULL, *id_str = NULL;
2366 err = got_object_id_str(&id_str, te->id);
2368 return got_error_from_errno();
2370 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2371 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2373 return got_error_from_errno();
2376 err = format_line(&wline, &width, line, view->ncols);
2381 if (n == selected) {
2382 wstandout(view->window);
2383 *selected_entry = te;
2385 waddwstr(view->window, wline);
2386 if (width < view->ncols)
2387 waddch(view->window, '\n');
2389 wstandend(view->window);
2395 *last_displayed_entry = te;
2398 te = SIMPLEQ_NEXT(te, entry);
2405 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2406 const struct got_tree_entries *entries, int isroot)
2408 struct got_tree_entry *te, *prev;
2411 if (*first_displayed_entry == NULL)
2414 te = SIMPLEQ_FIRST(&entries->head);
2415 if (*first_displayed_entry == te) {
2417 *first_displayed_entry = NULL;
2421 /* XXX this is stupid... switch to TAILQ? */
2422 for (i = 0; i < maxscroll; i++) {
2423 while (te != *first_displayed_entry) {
2425 te = SIMPLEQ_NEXT(te, entry);
2427 *first_displayed_entry = prev;
2428 te = SIMPLEQ_FIRST(&entries->head);
2430 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2431 *first_displayed_entry = NULL;
2435 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2436 struct got_tree_entry *last_displayed_entry,
2437 const struct got_tree_entries *entries)
2439 struct got_tree_entry *next;
2442 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2445 if (*first_displayed_entry)
2446 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2448 next = SIMPLEQ_FIRST(&entries->head);
2450 *first_displayed_entry = next;
2451 if (++n >= maxscroll)
2453 next = SIMPLEQ_NEXT(next, entry);
2457 static const struct got_error *
2458 tree_entry_path(char **path, struct tog_parent_trees *parents,
2459 struct got_tree_entry *te)
2461 const struct got_error *err = NULL;
2462 struct tog_parent_tree *pt;
2463 size_t len = 2; /* for leading slash and NUL */
2465 TAILQ_FOREACH(pt, parents, entry)
2466 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2468 len += strlen(te->name);
2470 *path = calloc(1, len);
2472 return got_error_from_errno();
2475 pt = TAILQ_LAST(parents, tog_parent_trees);
2477 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2478 err = got_error(GOT_ERR_NO_SPACE);
2481 if (strlcat(*path, "/", len) >= len) {
2482 err = got_error(GOT_ERR_NO_SPACE);
2485 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2488 if (strlcat(*path, te->name, len) >= len) {
2489 err = got_error(GOT_ERR_NO_SPACE);
2501 static const struct got_error *
2502 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2503 struct got_tree_entry *te, struct tog_parent_trees *parents,
2504 struct got_object_id *commit_id, struct got_repository *repo)
2506 const struct got_error *err = NULL;
2508 struct tog_view *blame_view;
2510 err = tree_entry_path(&path, parents, te);
2514 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2515 if (blame_view == NULL)
2516 return got_error_from_errno();
2518 err = open_blame_view(blame_view, path, commit_id, repo);
2520 view_close(blame_view);
2523 *new_view = blame_view;
2527 static const struct got_error *
2528 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2529 struct got_tree_entry *te, struct tog_parent_trees *parents,
2530 struct got_object_id *commit_id, struct got_repository *repo)
2532 struct tog_view *log_view;
2533 const struct got_error *err = NULL;
2536 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2537 if (log_view == NULL)
2538 return got_error_from_errno();
2540 err = tree_entry_path(&path, parents, te);
2544 err = open_log_view(log_view, commit_id, repo, path);
2546 view_close(log_view);
2548 *new_view = log_view;
2553 static const struct got_error *
2554 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2555 struct got_object_id *commit_id, struct got_repository *repo)
2557 const struct got_error *err = NULL;
2558 char *commit_id_str = NULL;
2559 struct tog_tree_view_state *s = &view->state.tree;
2561 TAILQ_INIT(&s->parents);
2563 err = got_object_id_str(&commit_id_str, commit_id);
2567 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2568 err = got_error_from_errno();
2572 s->root = s->tree = root;
2573 s->entries = got_object_tree_get_entries(root);
2574 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2575 s->commit_id = commit_id;
2578 view->show = show_tree_view;
2579 view->input = input_tree_view;
2580 view->close = close_tree_view;
2582 free(commit_id_str);
2584 free(s->tree_label);
2588 static const struct got_error *
2589 close_tree_view(struct tog_view *view)
2591 struct tog_tree_view_state *s = &view->state.tree;
2593 free(s->tree_label);
2594 while (!TAILQ_EMPTY(&s->parents)) {
2595 struct tog_parent_tree *parent;
2596 parent = TAILQ_FIRST(&s->parents);
2597 TAILQ_REMOVE(&s->parents, parent, entry);
2601 if (s->tree != s->root)
2602 got_object_tree_close(s->tree);
2603 got_object_tree_close(s->root);
2608 static const struct got_error *
2609 show_tree_view(struct tog_view *view)
2611 const struct got_error *err = NULL;
2612 struct tog_tree_view_state *s = &view->state.tree;
2615 err = tree_entry_path(&parent_path, &s->parents, NULL);
2619 err = draw_tree_entries(view, &s->first_displayed_entry,
2620 &s->last_displayed_entry, &s->selected_entry,
2621 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2622 s->entries, s->selected, view->nlines, s->tree == s->root);
2627 static const struct got_error *
2628 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2629 struct tog_view *view, int ch)
2631 const struct got_error *err = NULL;
2632 struct tog_tree_view_state *s = &view->state.tree;
2636 s->show_ids = !s->show_ids;
2639 if (s->selected_entry) {
2640 err = log_tree_entry(new_view, view,
2641 s->selected_entry, &s->parents,
2642 s->commit_id, s->repo);
2647 if (s->selected > 0)
2649 if (s->selected > 0)
2651 tree_scroll_up(&s->first_displayed_entry, 1,
2652 s->entries, s->tree == s->root);
2655 if (SIMPLEQ_FIRST(&s->entries->head) ==
2656 s->first_displayed_entry) {
2657 if (s->tree != s->root)
2658 s->first_displayed_entry = NULL;
2662 tree_scroll_up(&s->first_displayed_entry,
2663 view->nlines, s->entries,
2664 s->tree == s->root);
2668 if (s->selected < s->ndisplayed - 1) {
2672 tree_scroll_down(&s->first_displayed_entry, 1,
2673 s->last_displayed_entry, s->entries);
2676 tree_scroll_down(&s->first_displayed_entry,
2677 view->nlines, s->last_displayed_entry,
2679 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2682 /* can't scroll any further; move cursor down */
2683 if (s->selected < s->ndisplayed - 1)
2684 s->selected = s->ndisplayed - 1;
2688 if (s->selected_entry == NULL) {
2689 struct tog_parent_tree *parent;
2691 /* user selected '..' */
2692 if (s->tree == s->root)
2694 parent = TAILQ_FIRST(&s->parents);
2695 TAILQ_REMOVE(&s->parents, parent,
2697 got_object_tree_close(s->tree);
2698 s->tree = parent->tree;
2700 got_object_tree_get_entries(s->tree);
2701 s->first_displayed_entry =
2702 parent->first_displayed_entry;
2704 parent->selected_entry;
2705 s->selected = parent->selected;
2707 } else if (S_ISDIR(s->selected_entry->mode)) {
2708 struct tog_parent_tree *parent;
2709 struct got_tree_object *child;
2710 err = got_object_open_as_tree(&child,
2711 s->repo, s->selected_entry->id);
2714 parent = calloc(1, sizeof(*parent));
2715 if (parent == NULL) {
2716 err = got_error_from_errno();
2719 parent->tree = s->tree;
2720 parent->first_displayed_entry =
2721 s->first_displayed_entry;
2722 parent->selected_entry = s->selected_entry;
2723 parent->selected = s->selected;
2724 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2727 got_object_tree_get_entries(s->tree);
2729 s->first_displayed_entry = NULL;
2730 } else if (S_ISREG(s->selected_entry->mode)) {
2731 err = blame_tree_entry(new_view, view,
2732 s->selected_entry, &s->parents,
2733 s->commit_id, s->repo);
2739 if (s->selected > view->nlines)
2740 s->selected = s->ndisplayed - 1;
2753 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2758 static const struct got_error *
2759 cmd_tree(int argc, char *argv[])
2761 const struct got_error *error;
2762 struct got_repository *repo = NULL;
2763 char *repo_path = NULL;
2764 struct got_object_id *commit_id = NULL;
2765 char *commit_id_arg = NULL;
2766 struct got_commit_object *commit = NULL;
2767 struct got_tree_object *tree = NULL;
2769 struct tog_view *view;
2772 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2776 while ((ch = getopt(argc, argv, "c:")) != -1) {
2779 commit_id_arg = optarg;
2791 repo_path = getcwd(NULL, 0);
2792 if (repo_path == NULL)
2793 return got_error_from_errno();
2794 } else if (argc == 1) {
2795 repo_path = realpath(argv[0], NULL);
2796 if (repo_path == NULL)
2797 return got_error_from_errno();
2801 error = got_repo_open(&repo, repo_path);
2806 if (commit_id_arg == NULL) {
2807 error = get_head_commit_id(&commit_id, repo);
2811 struct got_object *obj;
2812 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2813 if (error == NULL) {
2814 commit_id = got_object_get_id(obj);
2815 if (commit_id == NULL)
2816 error = got_error_from_errno();
2822 error = got_object_open_as_commit(&commit, repo, commit_id);
2826 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2830 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
2832 error = got_error_from_errno();
2835 error = open_tree_view(view, tree, commit_id, repo);
2838 error = view_loop(view);
2842 got_object_commit_close(commit);
2844 got_object_tree_close(tree);
2846 got_repo_close(repo);
2856 intrflush(stdscr, FALSE);
2857 keypad(stdscr, TRUE);
2866 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2867 "Available commands:\n", getprogname());
2868 for (i = 0; i < nitems(tog_commands); i++) {
2869 struct tog_cmd *cmd = &tog_commands[i];
2870 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2876 make_argv(const char *arg0, const char *arg1)
2879 int argc = (arg1 == NULL ? 1 : 2);
2881 argv = calloc(argc, sizeof(char *));
2884 argv[0] = strdup(arg0);
2885 if (argv[0] == NULL)
2888 argv[1] = strdup(arg1);
2889 if (argv[1] == NULL)
2897 main(int argc, char *argv[])
2899 const struct got_error *error = NULL;
2900 struct tog_cmd *cmd = NULL;
2902 char **cmd_argv = NULL;
2904 setlocale(LC_ALL, "");
2906 while ((ch = getopt(argc, argv, "h")) != -1) {
2925 /* Build an argument vector which runs a default command. */
2926 cmd = &tog_commands[0];
2927 cmd_argv = make_argv(cmd->name, NULL);
2932 /* Did the user specific a command? */
2933 for (i = 0; i < nitems(tog_commands); i++) {
2934 if (strncmp(tog_commands[i].name, argv[0],
2935 strlen(argv[0])) == 0) {
2936 cmd = &tog_commands[i];
2938 tog_commands[i].cmd_usage();
2943 /* Did the user specify a repository? */
2944 char *repo_path = realpath(argv[0], NULL);
2946 struct got_repository *repo;
2947 error = got_repo_open(&repo, repo_path);
2949 got_repo_close(repo);
2951 error = got_error_from_errno();
2954 fprintf(stderr, "%s: '%s' is not a "
2955 "known command\n", getprogname(),
2959 fprintf(stderr, "%s: '%s' is neither a known "
2960 "command nor a path to a repository\n",
2961 getprogname(), argv[0]);
2965 cmd = &tog_commands[0];
2966 cmd_argv = make_argv(cmd->name, repo_path);
2974 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2981 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);