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" },
88 int nlines, ncols, begin_y, begin_x;
89 int lines, cols; /* copies of LINES and COLS */
92 static const struct got_error *
93 show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
94 struct got_repository *);
95 static const struct got_error *
96 show_log_view(struct tog_view *, struct got_object_id *,
97 struct got_repository *, const char *);
98 static const struct got_error *
99 show_blame_view(struct tog_view *, const char *, struct got_object_id *,
100 struct got_repository *);
101 static const struct got_error *
102 show_tree_view(struct got_tree_object *, struct got_object_id *,
103 struct got_repository *);
106 close_view(struct tog_view *view)
109 del_panel(view->panel);
111 delwin(view->window);
115 static struct tog_view *
116 open_view(int nlines, int ncols, int begin_y, int begin_x)
118 struct tog_view *view = malloc(sizeof(*view));
125 view->nlines = nlines ? nlines : LINES - begin_y;
126 view->ncols = ncols ? ncols : COLS - begin_x;
127 view->begin_y = begin_y;
128 view->begin_x = begin_x;
129 view->window = newwin(nlines, ncols, begin_y, begin_x);
130 if (view->window == NULL) {
134 view->panel = new_panel(view->window);
135 if (view->panel == NULL) {
140 keypad(view->window, TRUE);
144 const struct got_error *
145 view_resize(struct tog_view *view)
149 if (view->lines > LINES)
150 nlines = view->nlines - (view->lines - LINES);
152 nlines = view->nlines + (LINES - view->lines);
154 if (view->cols > COLS)
155 ncols = view->ncols - (view->cols - COLS);
157 ncols = view->ncols + (COLS - view->cols);
159 if (wresize(view->window, nlines, ncols) == ERR)
160 return got_error_from_errno();
162 view->nlines = nlines;
173 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
178 /* Create newly allocated wide-character string equivalent to a byte string. */
179 static const struct got_error *
180 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
183 const struct got_error *err = NULL;
186 *wlen = mbstowcs(NULL, s, 0);
187 if (*wlen == (size_t)-1) {
190 return got_error_from_errno();
192 /* byte string invalid in current encoding; try to "fix" it */
193 err = got_mbsavis(&vis, &vislen, s);
196 *wlen = mbstowcs(NULL, vis, 0);
197 if (*wlen == (size_t)-1) {
198 err = got_error_from_errno(); /* give up */
203 *ws = calloc(*wlen + 1, sizeof(*ws));
205 err = got_error_from_errno();
209 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
210 err = got_error_from_errno();
221 /* Format a line for display, ensuring that it won't overflow a width limit. */
222 static const struct got_error *
223 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
225 const struct got_error *err = NULL;
227 wchar_t *wline = NULL;
234 err = mbs2ws(&wline, &wlen, line);
239 while (i < wlen && cols < wlimit) {
240 int width = wcwidth(wline[i]);
247 if (cols + width <= wlimit) {
253 if (wline[i] == L'\t')
254 cols += TABSIZE - ((cols + 1) % TABSIZE);
258 err = got_error_from_errno();
273 static const struct got_error *
274 draw_commit(struct tog_view *view, struct got_commit_object *commit,
275 struct got_object_id *id)
277 const struct got_error *err = NULL;
278 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
279 char *logmsg0 = NULL, *logmsg = NULL;
280 char *author0 = NULL, *author = NULL;
281 wchar_t *wlogmsg = NULL, *wauthor = NULL;
282 int author_width, logmsg_width;
283 char *newline, *smallerthan;
286 static const size_t date_display_cols = 9;
287 static const size_t author_display_cols = 16;
288 const int avail = view->ncols;
290 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
292 return got_error(GOT_ERR_NO_SPACE);
294 if (avail < date_display_cols)
295 limit = MIN(sizeof(datebuf) - 1, avail);
297 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
298 waddnstr(view->window, datebuf, limit);
303 author0 = strdup(commit->author);
304 if (author0 == NULL) {
305 err = got_error_from_errno();
309 smallerthan = strchr(author, '<');
313 char *at = strchr(author, '@');
318 err = format_line(&wauthor, &author_width, author, limit);
321 waddwstr(view->window, wauthor);
323 while (col <= avail && author_width < author_display_cols + 1) {
324 waddch(view->window, ' ');
331 logmsg0 = strdup(commit->logmsg);
332 if (logmsg0 == NULL) {
333 err = got_error_from_errno();
337 while (*logmsg == '\n')
339 newline = strchr(logmsg, '\n');
343 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
346 waddwstr(view->window, wlogmsg);
348 while (col <= avail) {
349 waddch(view->window, ' ');
361 struct commit_queue_entry {
362 TAILQ_ENTRY(commit_queue_entry) entry;
363 struct got_object_id *id;
364 struct got_commit_object *commit;
366 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
367 struct commit_queue {
369 struct commit_queue_head head;
372 static struct commit_queue_entry *
373 alloc_commit_queue_entry(struct got_commit_object *commit,
374 struct got_object_id *id)
376 struct commit_queue_entry *entry;
378 entry = calloc(1, sizeof(*entry));
383 entry->commit = commit;
388 pop_commit(struct commit_queue *commits)
390 struct commit_queue_entry *entry;
392 entry = TAILQ_FIRST(&commits->head);
393 TAILQ_REMOVE(&commits->head, entry, entry);
394 got_object_commit_close(entry->commit);
396 /* Don't free entry->id! It is owned by the commit graph. */
401 free_commits(struct commit_queue *commits)
403 while (!TAILQ_EMPTY(&commits->head))
407 static const struct got_error *
408 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
409 struct got_object_id *start_id, int minqueue, int init,
410 struct got_repository *repo, const char *path)
412 const struct got_error *err = NULL;
413 struct got_object_id *id;
414 struct commit_queue_entry *entry;
415 int nfetched, nqueued = 0, found_obj = 0;
416 int is_root_path = strcmp(path, "/") == 0;
418 err = got_commit_graph_iter_start(graph, start_id);
422 entry = TAILQ_LAST(&commits->head, commit_queue_head);
423 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
426 /* Start ID's commit is already on the queue; skip over it. */
427 err = got_commit_graph_iter_next(&id, graph);
428 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
431 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
437 struct got_commit_object *commit;
439 err = got_commit_graph_iter_next(&id, graph);
441 if (err->code != GOT_ERR_ITER_NEED_MORE)
443 if (nqueued >= minqueue) {
447 err = got_commit_graph_fetch_commits(&nfetched,
456 err = got_object_open_as_commit(&commit, repo, id);
461 struct got_object *obj;
462 struct got_object_qid *pid;
465 err = got_object_open_by_path(&obj, repo, id, path);
467 got_object_commit_close(commit);
468 if (err->code == GOT_ERR_NO_OBJ &&
469 (found_obj || !init)) {
470 /* History stops here. */
471 err = got_error(GOT_ERR_ITER_COMPLETED);
477 pid = SIMPLEQ_FIRST(&commit->parent_ids);
479 struct got_object *pobj;
480 err = got_object_open_by_path(&pobj, repo,
483 if (err->code != GOT_ERR_NO_OBJ) {
484 got_object_close(obj);
485 got_object_commit_close(commit);
491 struct got_object_id *id, *pid;
492 id = got_object_get_id(obj);
494 err = got_error_from_errno();
495 got_object_close(obj);
496 got_object_close(pobj);
499 pid = got_object_get_id(pobj);
501 err = got_error_from_errno();
503 got_object_close(obj);
504 got_object_close(pobj);
508 (got_object_id_cmp(id, pid) != 0);
509 got_object_close(pobj);
514 got_object_close(obj);
516 got_object_commit_close(commit);
521 entry = alloc_commit_queue_entry(commit, id);
523 err = got_error_from_errno();
526 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
534 static const struct got_error *
535 fetch_next_commit(struct commit_queue_entry **pentry,
536 struct commit_queue_entry *entry, struct commit_queue *commits,
537 struct got_commit_graph *graph, struct got_repository *repo,
540 const struct got_error *err = NULL;
544 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
548 /* Next entry to display should now be available. */
549 *pentry = TAILQ_NEXT(entry, entry);
551 return got_error(GOT_ERR_NO_OBJ);
556 static const struct got_error *
557 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
559 const struct got_error *err = NULL;
560 struct got_reference *head_ref;
564 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
568 err = got_ref_resolve(head_id, repo, head_ref);
569 got_ref_close(head_ref);
578 static const struct got_error *
579 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
580 struct commit_queue_entry **selected, struct commit_queue_entry *first,
581 struct commit_queue *commits, int selected_idx, int limit,
582 struct got_commit_graph *graph, struct got_repository *repo,
585 const struct got_error *err = NULL;
586 struct commit_queue_entry *entry;
588 char *id_str, *header;
594 if (ncommits == selected_idx) {
598 entry = TAILQ_NEXT(entry, entry);
602 err = got_object_id_str(&id_str, (*selected)->id);
607 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
608 err = got_error_from_errno();
612 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
613 err = got_error_from_errno();
618 err = format_line(&wline, &width, header, view->ncols);
625 werase(view->window);
627 waddwstr(view->window, wline);
628 if (width < view->ncols)
629 waddch(view->window, '\n');
638 if (ncommits >= limit - 1)
640 if (ncommits == selected_idx)
641 wstandout(view->window);
642 err = draw_commit(view, entry->commit, entry->id);
643 if (ncommits == selected_idx)
644 wstandend(view->window);
649 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
650 err = queue_commits(graph, commits, entry->id, 1,
653 if (err->code != GOT_ERR_ITER_COMPLETED)
658 entry = TAILQ_NEXT(entry, entry);
668 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
669 struct commit_queue *commits)
671 struct commit_queue_entry *entry;
674 entry = TAILQ_FIRST(&commits->head);
675 if (*first_displayed_entry == entry)
678 entry = *first_displayed_entry;
679 while (entry && nscrolled < maxscroll) {
680 entry = TAILQ_PREV(entry, commit_queue_head, entry);
682 *first_displayed_entry = entry;
688 static const struct got_error *
689 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
690 struct commit_queue_entry *last_displayed_entry,
691 struct commit_queue *commits, struct got_commit_graph *graph,
692 struct got_repository *repo, const char *path)
694 const struct got_error *err = NULL;
695 struct commit_queue_entry *pentry;
699 pentry = TAILQ_NEXT(last_displayed_entry, entry);
700 if (pentry == NULL) {
701 err = fetch_next_commit(&pentry, last_displayed_entry,
702 commits, graph, repo, path);
703 if (err || pentry == NULL)
706 last_displayed_entry = pentry;
708 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
711 *first_displayed_entry = pentry;
712 } while (++nscrolled < maxscroll);
717 static const struct got_error *
718 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
720 const struct got_error *err;
721 struct got_object *obj1 = NULL, *obj2 = NULL;
722 struct got_object_qid *parent_id;
723 struct tog_view *view;
725 err = got_object_open(&obj2, repo, entry->id);
729 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
731 err = got_object_open(&obj1, repo, parent_id->id);
736 view = open_view(0, 0, 0, 0);
738 err = got_error_from_errno();
742 err = show_diff_view(view, obj1, obj2, repo);
746 got_object_close(obj1);
748 got_object_close(obj2);
752 static const struct got_error *
753 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
755 const struct got_error *err = NULL;
756 struct got_tree_object *tree;
758 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
762 err = show_tree_view(tree, entry->id, repo);
763 got_object_tree_close(tree);
767 static const struct got_error *
768 show_log_view(struct tog_view *view, struct got_object_id *start_id,
769 struct got_repository *repo, const char *path)
771 const struct got_error *err = NULL;
772 struct got_object_id *head_id = NULL;
773 int ch, done = 0, selected = 0, nfetched;
774 struct got_commit_graph *graph = NULL;
775 struct commit_queue commits;
776 struct commit_queue_entry *first_displayed_entry = NULL;
777 struct commit_queue_entry *last_displayed_entry = NULL;
778 struct commit_queue_entry *selected_entry = NULL;
779 char *in_repo_path = NULL;
781 err = got_repo_map_path(&in_repo_path, repo, path);
785 err = get_head_commit_id(&head_id, repo);
789 /* The graph contains all commits. */
790 err = got_commit_graph_open(&graph, head_id, 0, repo);
793 /* The commit queue contains a subset of commits filtered by path. */
794 TAILQ_INIT(&commits.head);
795 commits.ncommits = 0;
797 /* Populate commit graph with a sufficient number of commits. */
798 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
804 * Open the initial batch of commits, sorted in commit graph order.
805 * We keep all commits open throughout the lifetime of the log view
806 * in order to avoid having to re-fetch commits from disk while
807 * updating the display.
809 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
812 if (err->code != GOT_ERR_ITER_COMPLETED)
817 show_panel(view->panel);
819 first_displayed_entry = TAILQ_FIRST(&commits.head);
820 selected_entry = first_displayed_entry;
822 err = draw_commits(view, &last_displayed_entry, &selected_entry,
823 first_displayed_entry, &commits, selected, view->nlines,
824 graph, repo, in_repo_path);
828 nodelay(stdscr, FALSE);
829 ch = wgetch(view->window);
830 nodelay(stdscr, TRUE);
843 scroll_up(&first_displayed_entry, 1, &commits);
846 if (TAILQ_FIRST(&commits.head) ==
847 first_displayed_entry) {
851 scroll_up(&first_displayed_entry, view->nlines,
856 if (selected < MIN(view->nlines - 2,
857 commits.ncommits - 1)) {
861 err = scroll_down(&first_displayed_entry, 1,
862 last_displayed_entry, &commits, graph,
865 if (err->code != GOT_ERR_ITER_COMPLETED)
871 struct commit_queue_entry *first = first_displayed_entry;
872 err = scroll_down(&first_displayed_entry, view->nlines,
873 last_displayed_entry, &commits, graph,
876 if (err->code != GOT_ERR_ITER_COMPLETED)
878 /* can't scroll any further; move cursor down */
879 if (first == first_displayed_entry && selected <
880 MIN(view->nlines - 2, commits.ncommits - 1)) {
881 selected = MIN(view->nlines - 2,
882 commits.ncommits - 1);
889 err = view_resize(view);
892 if (selected > view->nlines - 2)
893 selected = view->nlines - 2;
894 if (selected > commits.ncommits - 1)
895 selected = commits.ncommits - 1;
899 err = show_commit(selected_entry, repo);
902 show_panel(view->panel);
905 err = browse_commit(selected_entry, repo);
908 show_panel(view->panel);
917 got_commit_graph_close(graph);
918 free_commits(&commits);
923 static const struct got_error *
924 cmd_log(int argc, char *argv[])
926 const struct got_error *error;
927 struct got_repository *repo = NULL;
928 struct got_object_id *start_id = NULL;
929 char *path = NULL, *repo_path = NULL, *cwd = NULL;
930 char *start_commit = NULL;
932 struct tog_view *view;
935 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
939 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
942 start_commit = optarg;
945 repo_path = realpath(optarg, NULL);
946 if (repo_path == NULL)
961 path = strdup(argv[0]);
965 return got_error_from_errno();
967 cwd = getcwd(NULL, 0);
969 error = got_error_from_errno();
972 if (repo_path == NULL) {
973 repo_path = strdup(cwd);
974 if (repo_path == NULL) {
975 error = got_error_from_errno();
980 error = got_repo_open(&repo, repo_path);
984 if (start_commit == NULL) {
985 error = get_head_commit_id(&start_id, repo);
989 struct got_object *obj;
990 error = got_object_open_by_id_str(&obj, repo, start_commit);
992 start_id = got_object_get_id(obj);
993 if (start_id == NULL)
994 error = got_error_from_errno();
1001 view = open_view(0, 0, 0, 0);
1003 error = got_error_from_errno();
1006 error = show_log_view(view, start_id, repo, path);
1014 got_repo_close(repo);
1022 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1028 parse_next_line(FILE *f, size_t *len)
1033 const char delim[3] = { '\0', '\0', '\0'};
1035 line = fparseln(f, &linelen, &lineno, delim, 0);
1041 static const struct got_error *
1042 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1043 int *last_displayed_line, int *eof, int max_lines)
1045 const struct got_error *err;
1046 int nlines = 0, nprinted = 0;
1053 werase(view->window);
1056 while (nprinted < max_lines) {
1057 line = parse_next_line(f, &len);
1062 if (++nlines < *first_displayed_line) {
1067 err = format_line(&wline, &width, line, view->ncols);
1073 waddwstr(view->window, wline);
1074 if (width < view->ncols)
1075 waddch(view->window, '\n');
1076 if (++nprinted == 1)
1077 *first_displayed_line = nlines;
1082 *last_displayed_line = nlines;
1090 static const struct got_error *
1091 show_diff_view(struct tog_view *view, struct got_object *obj1,
1092 struct got_object *obj2, struct got_repository *repo)
1094 const struct got_error *err;
1097 int first_displayed_line = 1, last_displayed_line = view->nlines;
1100 if (obj1 != NULL && obj2 != NULL &&
1101 got_object_get_type(obj1) != got_object_get_type(obj2))
1102 return got_error(GOT_ERR_OBJ_TYPE);
1106 return got_error_from_errno();
1108 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1109 case GOT_OBJ_TYPE_BLOB:
1110 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1112 case GOT_OBJ_TYPE_TREE:
1113 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1115 case GOT_OBJ_TYPE_COMMIT:
1116 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1119 return got_error(GOT_ERR_OBJ_TYPE);
1124 show_panel(view->panel);
1127 err = draw_file(view, f, &first_displayed_line,
1128 &last_displayed_line, &eof, view->nlines);
1131 nodelay(stdscr, FALSE);
1132 ch = wgetch(view->window);
1133 nodelay(stdscr, TRUE);
1140 if (first_displayed_line > 1)
1141 first_displayed_line--;
1146 while (i++ < view->nlines - 1 &&
1147 first_displayed_line > 1)
1148 first_displayed_line--;
1153 first_displayed_line++;
1158 while (!eof && i++ < view->nlines - 1) {
1159 char *line = parse_next_line(f, NULL);
1160 first_displayed_line++;
1166 err = view_resize(view);
1179 static const struct got_error *
1180 cmd_diff(int argc, char *argv[])
1182 const struct got_error *error = NULL;
1183 struct got_repository *repo = NULL;
1184 struct got_object *obj1 = NULL, *obj2 = NULL;
1185 char *repo_path = NULL;
1186 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1188 struct tog_view *view;
1191 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1195 while ((ch = getopt(argc, argv, "")) != -1) {
1207 usage_diff(); /* TODO show local worktree changes */
1208 } else if (argc == 2) {
1209 repo_path = getcwd(NULL, 0);
1210 if (repo_path == NULL)
1211 return got_error_from_errno();
1212 obj_id_str1 = argv[0];
1213 obj_id_str2 = argv[1];
1214 } else if (argc == 3) {
1215 repo_path = realpath(argv[0], NULL);
1216 if (repo_path == NULL)
1217 return got_error_from_errno();
1218 obj_id_str1 = argv[1];
1219 obj_id_str2 = argv[2];
1223 error = got_repo_open(&repo, repo_path);
1228 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1232 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1236 view = open_view(0, 0, 0, 0);
1238 error = got_error_from_errno();
1241 error = show_diff_view(view, obj1, obj2, repo);
1244 got_repo_close(repo);
1246 got_object_close(obj1);
1248 got_object_close(obj2);
1256 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1261 struct tog_blame_line {
1263 struct got_object_id *id;
1266 static const struct got_error *
1267 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1268 const char *path, struct tog_blame_line *lines, int nlines,
1269 int blame_complete, int selected_line, int *first_displayed_line,
1270 int *last_displayed_line, int *eof, int max_lines)
1272 const struct got_error *err;
1273 int lineno = 0, nprinted = 0;
1278 struct tog_blame_line *blame_line;
1279 struct got_object_id *prev_id = NULL;
1282 err = got_object_id_str(&id_str, id);
1287 werase(view->window);
1289 if (asprintf(&line, "commit: %s", id_str) == -1) {
1290 err = got_error_from_errno();
1295 err = format_line(&wline, &width, line, view->ncols);
1298 waddwstr(view->window, wline);
1301 if (width < view->ncols)
1302 waddch(view->window, '\n');
1304 if (asprintf(&line, "[%d/%d] %s%s",
1305 *first_displayed_line - 1 + selected_line, nlines,
1306 blame_complete ? "" : "annotating ", path) == -1) {
1308 return got_error_from_errno();
1311 err = format_line(&wline, &width, line, view->ncols);
1316 waddwstr(view->window, wline);
1319 if (width < view->ncols)
1320 waddch(view->window, '\n');
1323 while (nprinted < max_lines - 2) {
1324 line = parse_next_line(f, &len);
1329 if (++lineno < *first_displayed_line) {
1334 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1335 err = format_line(&wline, &width, line, wlimit);
1341 if (nprinted == selected_line - 1)
1342 wstandout(view->window);
1344 blame_line = &lines[lineno - 1];
1345 if (blame_line->annotated && prev_id &&
1346 got_object_id_cmp(prev_id, blame_line->id) == 0)
1347 waddstr(view->window, " ");
1348 else if (blame_line->annotated) {
1350 err = got_object_id_str(&id_str, blame_line->id);
1356 wprintw(view->window, "%.8s ", id_str);
1358 prev_id = blame_line->id;
1360 waddstr(view->window, "........ ");
1364 waddwstr(view->window, wline);
1365 while (width < wlimit) {
1366 waddch(view->window, ' ');
1369 if (nprinted == selected_line - 1)
1370 wstandend(view->window);
1371 if (++nprinted == 1)
1372 *first_displayed_line = lineno;
1377 *last_displayed_line = lineno;
1385 struct tog_blame_cb_args {
1386 pthread_mutex_t *mutex;
1387 struct tog_blame_line *lines; /* one per line */
1390 struct tog_view *view;
1391 struct got_object_id *commit_id;
1394 int *first_displayed_line;
1395 int *last_displayed_line;
1400 static const struct got_error *
1401 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1403 const struct got_error *err = NULL;
1404 struct tog_blame_cb_args *a = arg;
1405 struct tog_blame_line *line;
1408 if (nlines != a->nlines ||
1409 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1410 return got_error(GOT_ERR_RANGE);
1412 if (pthread_mutex_lock(a->mutex) != 0)
1413 return got_error_from_errno();
1415 if (*a->quit) { /* user has quit the blame view */
1416 err = got_error(GOT_ERR_ITER_COMPLETED);
1421 goto done; /* no change in this commit */
1423 line = &a->lines[lineno - 1];
1424 if (line->annotated)
1427 line->id = got_object_id_dup(id);
1428 if (line->id == NULL) {
1429 err = got_error_from_errno();
1432 line->annotated = 1;
1434 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1435 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1436 a->last_displayed_line, &eof, a->view->nlines);
1438 if (pthread_mutex_unlock(a->mutex) != 0)
1439 return got_error_from_errno();
1443 struct tog_blame_thread_args {
1445 struct got_repository *repo;
1446 struct tog_blame_cb_args *cb_args;
1451 blame_thread(void *arg)
1453 const struct got_error *err;
1454 struct tog_blame_thread_args *ta = arg;
1455 struct tog_blame_cb_args *a = ta->cb_args;
1458 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1459 blame_cb, ta->cb_args);
1461 if (pthread_mutex_lock(a->mutex) != 0)
1462 return (void *)got_error_from_errno();
1464 got_repo_close(ta->repo);
1468 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1469 a->lines, a->nlines, 1, *a->selected_line,
1470 a->first_displayed_line, a->last_displayed_line, &eof,
1473 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1474 err = got_error_from_errno();
1479 static struct got_object_id *
1480 get_selected_commit_id(struct tog_blame_line *lines,
1481 int first_displayed_line, int selected_line)
1483 struct tog_blame_line *line;
1485 line = &lines[first_displayed_line - 1 + selected_line - 1];
1486 if (!line->annotated)
1492 static const struct got_error *
1493 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1494 struct tog_blame_line *lines, int first_displayed_line,
1495 int selected_line, struct got_repository *repo)
1497 const struct got_error *err = NULL;
1498 struct got_commit_object *commit = NULL;
1499 struct got_object_id *selected_id;
1500 struct got_object_qid *pid;
1505 selected_id = get_selected_commit_id(lines,
1506 first_displayed_line, selected_line);
1507 if (selected_id == NULL)
1510 err = got_object_open(obj, repo, selected_id);
1514 err = got_object_commit_open(&commit, repo, *obj);
1518 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1520 err = got_object_open(pobj, repo, pid->id);
1526 got_object_commit_close(commit);
1533 struct tog_blame_line *lines;
1536 struct tog_blame_thread_args thread_args;
1537 struct tog_blame_cb_args cb_args;
1541 static const struct got_error *
1542 stop_blame(struct tog_blame *blame)
1544 const struct got_error *err = NULL;
1547 if (blame->thread) {
1548 if (pthread_join(blame->thread, (void **)&err) != 0)
1549 err = got_error_from_errno();
1550 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1552 blame->thread = NULL;
1554 if (blame->thread_args.repo) {
1555 got_repo_close(blame->thread_args.repo);
1556 blame->thread_args.repo = NULL;
1562 for (i = 0; i < blame->nlines; i++)
1563 free(blame->lines[i].id);
1565 blame->lines = NULL;
1566 free(blame->cb_args.commit_id);
1567 blame->cb_args.commit_id = NULL;
1572 static const struct got_error *
1573 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1574 struct tog_view *view, int *blame_complete,
1575 int *first_displayed_line, int *last_displayed_line,
1576 int *selected_line, int *done, const char *path,
1577 struct got_object_id *commit_id,
1578 struct got_repository *repo)
1580 const struct got_error *err = NULL;
1581 struct got_blob_object *blob = NULL;
1582 struct got_repository *thread_repo = NULL;
1583 struct got_object *obj;
1585 err = got_object_open_by_path(&obj, repo, commit_id, path);
1588 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1589 err = got_error(GOT_ERR_OBJ_TYPE);
1593 err = got_object_blob_open(&blob, repo, obj, 8192);
1596 blame->f = got_opentemp();
1597 if (blame->f == NULL) {
1598 err = got_error_from_errno();
1601 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1606 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1607 if (blame->lines == NULL) {
1608 err = got_error_from_errno();
1612 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1616 blame->cb_args.view = view;
1617 blame->cb_args.lines = blame->lines;
1618 blame->cb_args.nlines = blame->nlines;
1619 blame->cb_args.mutex = mutex;
1620 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1621 if (blame->cb_args.commit_id == NULL) {
1622 err = got_error_from_errno();
1625 blame->cb_args.f = blame->f;
1626 blame->cb_args.path = path;
1627 blame->cb_args.first_displayed_line = first_displayed_line;
1628 blame->cb_args.selected_line = selected_line;
1629 blame->cb_args.last_displayed_line = last_displayed_line;
1630 blame->cb_args.quit = done;
1632 blame->thread_args.path = path;
1633 blame->thread_args.repo = thread_repo;
1634 blame->thread_args.cb_args = &blame->cb_args;
1635 blame->thread_args.complete = blame_complete;
1636 *blame_complete = 0;
1638 if (pthread_create(&blame->thread, NULL, blame_thread,
1639 &blame->thread_args) != 0) {
1640 err = got_error_from_errno();
1646 got_object_blob_close(blob);
1648 got_object_close(obj);
1654 static const struct got_error *
1655 show_blame_view(struct tog_view *view, const char *path,
1656 struct got_object_id *commit_id, struct got_repository *repo)
1658 const struct got_error *err = NULL, *thread_err = NULL;
1659 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1660 int selected_line = first_displayed_line;
1661 int eof, blame_complete = 0;
1662 struct got_object *obj = NULL, *pobj = NULL;
1663 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1664 struct tog_blame blame;
1665 struct got_object_id_queue blamed_commits;
1666 struct got_object_qid *blamed_commit = NULL;
1667 struct tog_view *diff_view;
1669 SIMPLEQ_INIT(&blamed_commits);
1671 if (pthread_mutex_init(&mutex, NULL) != 0) {
1672 err = got_error_from_errno();
1676 err = got_object_qid_alloc(&blamed_commit, commit_id);
1679 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1681 show_panel(view->panel);
1682 last_displayed_line = view->nlines;
1684 memset(&blame, 0, sizeof(blame));
1685 err = run_blame(&blame, &mutex, view, &blame_complete,
1686 &first_displayed_line, &last_displayed_line,
1687 &selected_line, &done, path, blamed_commit->id, repo);
1692 if (pthread_mutex_lock(&mutex) != 0) {
1693 err = got_error_from_errno();
1696 err = draw_blame(view, blamed_commit->id, blame.f, path,
1697 blame.lines, blame.nlines, blame_complete, selected_line,
1698 &first_displayed_line, &last_displayed_line, &eof,
1700 if (pthread_mutex_unlock(&mutex) != 0) {
1701 err = got_error_from_errno();
1706 nodelay(stdscr, FALSE);
1707 ch = wgetch(view->window);
1708 nodelay(stdscr, TRUE);
1709 if (pthread_mutex_lock(&mutex) != 0) {
1710 err = got_error_from_errno();
1719 if (selected_line > 1)
1721 else if (selected_line == 1 &&
1722 first_displayed_line > 1)
1723 first_displayed_line--;
1727 if (first_displayed_line == 1) {
1731 if (first_displayed_line > view->nlines - 2)
1732 first_displayed_line -=
1735 first_displayed_line = 1;
1739 if (selected_line < view->nlines - 2 &&
1740 first_displayed_line + selected_line <=
1743 else if (last_displayed_line < blame.nlines)
1744 first_displayed_line++;
1748 struct got_object_id *id;
1749 id = get_selected_commit_id(blame.lines,
1750 first_displayed_line, selected_line);
1751 if (id == NULL || got_object_id_cmp(id,
1752 blamed_commit->id) == 0)
1754 err = open_selected_commit(&pobj, &obj,
1755 blame.lines, first_displayed_line,
1756 selected_line, repo);
1759 if (pobj == NULL && obj == NULL)
1761 if (ch == 'p' && pobj == NULL)
1764 if (pthread_mutex_unlock(&mutex) != 0) {
1765 err = got_error_from_errno();
1768 thread_err = stop_blame(&blame);
1770 if (pthread_mutex_lock(&mutex) != 0) {
1771 err = got_error_from_errno();
1776 id = got_object_get_id(ch == 'b' ? obj : pobj);
1777 got_object_close(obj);
1780 got_object_close(pobj);
1784 err = got_error_from_errno();
1787 err = got_object_qid_alloc(&blamed_commit, id);
1791 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1792 blamed_commit, entry);
1793 err = run_blame(&blame, &mutex, view,
1794 &blame_complete, &first_displayed_line,
1795 &last_displayed_line, &selected_line,
1796 &done, path, blamed_commit->id, repo);
1802 struct got_object_qid *first;
1803 first = SIMPLEQ_FIRST(&blamed_commits);
1804 if (!got_object_id_cmp(first->id, commit_id))
1807 if (pthread_mutex_unlock(&mutex) != 0) {
1808 err = got_error_from_errno();
1811 thread_err = stop_blame(&blame);
1813 if (pthread_mutex_lock(&mutex) != 0) {
1814 err = got_error_from_errno();
1819 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1820 got_object_qid_free(blamed_commit);
1821 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1822 err = run_blame(&blame, &mutex, view,
1823 &blame_complete, &first_displayed_line,
1824 &last_displayed_line, &selected_line,
1825 &done, path, blamed_commit->id, repo);
1832 err = open_selected_commit(&pobj, &obj,
1833 blame.lines, first_displayed_line,
1834 selected_line, repo);
1837 if (pobj == NULL && obj == NULL)
1839 diff_view = open_view(0, 0, 0, 0);
1840 if (diff_view == NULL) {
1841 err = got_error_from_errno();
1844 err = show_diff_view(diff_view, pobj, obj, repo);
1845 close_view(diff_view);
1847 got_object_close(pobj);
1850 got_object_close(obj);
1852 show_panel(view->panel);
1858 if (last_displayed_line >= blame.nlines &&
1859 selected_line < view->nlines - 2) {
1860 selected_line = MIN(blame.nlines,
1864 if (last_displayed_line + view->nlines - 2 <=
1866 first_displayed_line +=
1869 first_displayed_line =
1870 blame.nlines - (view->nlines - 3);
1873 err = view_resize(view);
1876 if (selected_line > view->nlines - 2) {
1877 selected_line = MIN(blame.nlines,
1884 if (pthread_mutex_unlock(&mutex) != 0)
1885 err = got_error_from_errno();
1886 if (err || thread_err)
1891 got_object_close(pobj);
1893 thread_err = stop_blame(&blame);
1894 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1895 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1896 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1897 got_object_qid_free(blamed_commit);
1899 return thread_err ? thread_err : err;
1902 static const struct got_error *
1903 cmd_blame(int argc, char *argv[])
1905 const struct got_error *error;
1906 struct got_repository *repo = NULL;
1907 char *repo_path = NULL;
1909 struct got_object_id *commit_id = NULL;
1910 char *commit_id_str = NULL;
1912 struct tog_view *view;
1915 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1919 while ((ch = getopt(argc, argv, "c:")) != -1) {
1922 commit_id_str = optarg;
1935 } else if (argc == 1) {
1936 repo_path = getcwd(NULL, 0);
1937 if (repo_path == NULL)
1938 return got_error_from_errno();
1940 } else if (argc == 2) {
1941 repo_path = realpath(argv[0], NULL);
1942 if (repo_path == NULL)
1943 return got_error_from_errno();
1948 error = got_repo_open(&repo, repo_path);
1953 if (commit_id_str == NULL) {
1954 struct got_reference *head_ref;
1955 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1958 error = got_ref_resolve(&commit_id, repo, head_ref);
1959 got_ref_close(head_ref);
1961 struct got_object *obj;
1962 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1965 commit_id = got_object_get_id(obj);
1966 if (commit_id == NULL)
1967 error = got_error_from_errno();
1968 got_object_close(obj);
1973 view = open_view(0, 0, 0, 0);
1975 error = got_error_from_errno();
1978 error = show_blame_view(view, path, commit_id, repo);
1983 got_repo_close(repo);
1987 static const struct got_error *
1988 draw_tree_entries(struct tog_view *view,
1989 struct got_tree_entry **first_displayed_entry,
1990 struct got_tree_entry **last_displayed_entry,
1991 struct got_tree_entry **selected_entry, int *ndisplayed,
1992 const char *label, int show_ids, const char *parent_path,
1993 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1995 const struct got_error *err = NULL;
1996 struct got_tree_entry *te;
2002 werase(view->window);
2007 err = format_line(&wline, &width, label, view->ncols);
2010 waddwstr(view->window, wline);
2013 if (width < view->ncols)
2014 waddch(view->window, '\n');
2017 err = format_line(&wline, &width, parent_path, view->ncols);
2020 waddwstr(view->window, wline);
2023 if (width < view->ncols)
2024 waddch(view->window, '\n');
2027 waddch(view->window, '\n');
2031 te = SIMPLEQ_FIRST(&entries->head);
2032 if (*first_displayed_entry == NULL) {
2033 if (selected == 0) {
2034 wstandout(view->window);
2035 *selected_entry = NULL;
2037 waddstr(view->window, " ..\n"); /* parent directory */
2039 wstandend(view->window);
2046 while (te != *first_displayed_entry)
2047 te = SIMPLEQ_NEXT(te, entry);
2051 char *line = NULL, *id_str = NULL;
2054 err = got_object_id_str(&id_str, te->id);
2056 return got_error_from_errno();
2058 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2059 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2061 return got_error_from_errno();
2064 err = format_line(&wline, &width, line, view->ncols);
2069 if (n == selected) {
2070 wstandout(view->window);
2071 *selected_entry = te;
2073 waddwstr(view->window, wline);
2074 if (width < view->ncols)
2075 waddch(view->window, '\n');
2077 wstandend(view->window);
2083 *last_displayed_entry = te;
2086 te = SIMPLEQ_NEXT(te, entry);
2093 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2094 const struct got_tree_entries *entries, int isroot)
2096 struct got_tree_entry *te, *prev;
2099 if (*first_displayed_entry == NULL)
2102 te = SIMPLEQ_FIRST(&entries->head);
2103 if (*first_displayed_entry == te) {
2105 *first_displayed_entry = NULL;
2109 /* XXX this is stupid... switch to TAILQ? */
2110 for (i = 0; i < maxscroll; i++) {
2111 while (te != *first_displayed_entry) {
2113 te = SIMPLEQ_NEXT(te, entry);
2115 *first_displayed_entry = prev;
2116 te = SIMPLEQ_FIRST(&entries->head);
2118 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2119 *first_displayed_entry = NULL;
2123 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2124 struct got_tree_entry *last_displayed_entry,
2125 const struct got_tree_entries *entries)
2127 struct got_tree_entry *next;
2130 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2133 if (*first_displayed_entry)
2134 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2136 next = SIMPLEQ_FIRST(&entries->head);
2138 *first_displayed_entry = next;
2139 if (++n >= maxscroll)
2141 next = SIMPLEQ_NEXT(next, entry);
2145 struct tog_parent_tree {
2146 TAILQ_ENTRY(tog_parent_tree) entry;
2147 struct got_tree_object *tree;
2148 struct got_tree_entry *first_displayed_entry;
2149 struct got_tree_entry *selected_entry;
2153 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2155 static const struct got_error *
2156 tree_entry_path(char **path, struct tog_parent_trees *parents,
2157 struct got_tree_entry *te)
2159 const struct got_error *err = NULL;
2160 struct tog_parent_tree *pt;
2161 size_t len = 2; /* for leading slash and NUL */
2163 TAILQ_FOREACH(pt, parents, entry)
2164 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2166 len += strlen(te->name);
2168 *path = calloc(1, len);
2170 return got_error_from_errno();
2173 pt = TAILQ_LAST(parents, tog_parent_trees);
2175 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2176 err = got_error(GOT_ERR_NO_SPACE);
2179 if (strlcat(*path, "/", len) >= len) {
2180 err = got_error(GOT_ERR_NO_SPACE);
2183 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2186 if (strlcat(*path, te->name, len) >= len) {
2187 err = got_error(GOT_ERR_NO_SPACE);
2199 static const struct got_error *
2200 blame_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2201 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2202 struct got_repository *repo)
2204 const struct got_error *err = NULL;
2207 err = tree_entry_path(&path, parents, te);
2211 err = show_blame_view(view, path, commit_id, repo);
2216 static const struct got_error *
2217 log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2218 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2219 struct got_repository *repo)
2221 const struct got_error *err = NULL;
2224 err = tree_entry_path(&path, parents, te);
2228 err = show_log_view(view, commit_id, repo, path);
2233 static const struct got_error *
2234 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2235 struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 int ch, done = 0, selected = 0, show_ids = 0;
2239 struct got_tree_object *tree = root;
2240 const struct got_tree_entries *entries;
2241 struct got_tree_entry *first_displayed_entry = NULL;
2242 struct got_tree_entry *last_displayed_entry = NULL;
2243 struct got_tree_entry *selected_entry = NULL;
2244 char *commit_id_str = NULL, *tree_label = NULL;
2245 int nentries, ndisplayed;
2246 struct tog_parent_trees parents;
2247 struct tog_view *view = NULL;
2249 TAILQ_INIT(&parents);
2251 err = got_object_id_str(&commit_id_str, commit_id);
2255 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2256 err = got_error_from_errno();
2260 view = open_view(0, 0, 0, 0);
2262 err = got_error_from_errno();
2265 show_panel(view->panel);
2267 entries = got_object_tree_get_entries(root);
2268 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2271 entries = got_object_tree_get_entries(tree);
2272 nentries = entries->nentries;
2274 nentries++; /* '..' directory */
2276 err = tree_entry_path(&parent_path, &parents, NULL);
2280 err = draw_tree_entries(view, &first_displayed_entry,
2281 &last_displayed_entry, &selected_entry, &ndisplayed,
2282 tree_label, show_ids, parent_path, entries, selected,
2283 view->nlines, tree == root);
2288 nodelay(stdscr, FALSE);
2289 ch = wgetch(view->window);
2290 nodelay(stdscr, TRUE);
2296 show_ids = !show_ids;
2299 if (selected_entry) {
2300 struct tog_view *log_view;
2301 log_view = open_view(0, 0, 0, 0);
2302 if (log_view == NULL) {
2303 err = got_error_from_errno();
2306 err = log_tree_entry(log_view,
2307 selected_entry, &parents,
2309 close_view(log_view);
2320 tree_scroll_up(&first_displayed_entry, 1,
2321 entries, tree == root);
2324 if (SIMPLEQ_FIRST(&entries->head) ==
2325 first_displayed_entry) {
2327 first_displayed_entry = NULL;
2331 tree_scroll_up(&first_displayed_entry,
2332 view->nlines, entries, tree == root);
2336 if (selected < ndisplayed - 1) {
2340 tree_scroll_down(&first_displayed_entry, 1,
2341 last_displayed_entry, entries);
2344 tree_scroll_down(&first_displayed_entry,
2345 view->nlines, last_displayed_entry,
2347 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2349 /* can't scroll any further; move cursor down */
2350 if (selected < ndisplayed - 1)
2351 selected = ndisplayed - 1;
2355 if (selected_entry == NULL) {
2356 struct tog_parent_tree *parent;
2358 /* user selected '..' */
2361 parent = TAILQ_FIRST(&parents);
2362 TAILQ_REMOVE(&parents, parent, entry);
2363 got_object_tree_close(tree);
2364 tree = parent->tree;
2365 first_displayed_entry =
2366 parent->first_displayed_entry;
2367 selected_entry = parent->selected_entry;
2368 selected = parent->selected;
2370 } else if (S_ISDIR(selected_entry->mode)) {
2371 struct tog_parent_tree *parent;
2372 struct got_tree_object *child;
2373 err = got_object_open_as_tree(
2374 &child, repo, selected_entry->id);
2377 parent = calloc(1, sizeof(*parent));
2378 if (parent == NULL) {
2379 err = got_error_from_errno();
2382 parent->tree = tree;
2383 parent->first_displayed_entry =
2384 first_displayed_entry;
2385 parent->selected_entry = selected_entry;
2386 parent->selected = selected;
2387 TAILQ_INSERT_HEAD(&parents, parent,
2391 first_displayed_entry = NULL;
2392 } else if (S_ISREG(selected_entry->mode)) {
2393 struct tog_view *blame_view;
2394 blame_view = open_view(0, 0, 0, 0);
2395 if (blame_view == NULL) {
2396 err = got_error_from_errno();
2399 err = blame_tree_entry(blame_view,
2400 selected_entry, &parents,
2402 close_view(blame_view);
2408 err = view_resize(view);
2411 if (selected > view->nlines)
2412 selected = ndisplayed - 1;
2422 free(commit_id_str);
2423 while (!TAILQ_EMPTY(&parents)) {
2424 struct tog_parent_tree *parent;
2425 parent = TAILQ_FIRST(&parents);
2426 TAILQ_REMOVE(&parents, parent, entry);
2431 got_object_tree_close(tree);
2439 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2444 static const struct got_error *
2445 cmd_tree(int argc, char *argv[])
2447 const struct got_error *error;
2448 struct got_repository *repo = NULL;
2449 char *repo_path = NULL;
2450 struct got_object_id *commit_id = NULL;
2451 char *commit_id_arg = NULL;
2452 struct got_commit_object *commit = NULL;
2453 struct got_tree_object *tree = NULL;
2457 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2461 while ((ch = getopt(argc, argv, "c:")) != -1) {
2464 commit_id_arg = optarg;
2476 repo_path = getcwd(NULL, 0);
2477 if (repo_path == NULL)
2478 return got_error_from_errno();
2479 } else if (argc == 1) {
2480 repo_path = realpath(argv[0], NULL);
2481 if (repo_path == NULL)
2482 return got_error_from_errno();
2486 error = got_repo_open(&repo, repo_path);
2491 if (commit_id_arg == NULL) {
2492 error = get_head_commit_id(&commit_id, repo);
2496 struct got_object *obj;
2497 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2498 if (error == NULL) {
2499 commit_id = got_object_get_id(obj);
2500 if (commit_id == NULL)
2501 error = got_error_from_errno();
2507 error = got_object_open_as_commit(&commit, repo, commit_id);
2511 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2515 error = show_tree_view(tree, commit_id, repo);
2519 got_object_commit_close(commit);
2521 got_object_tree_close(tree);
2523 got_repo_close(repo);
2533 intrflush(stdscr, FALSE);
2534 keypad(stdscr, TRUE);
2543 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2544 "Available commands:\n", getprogname());
2545 for (i = 0; i < nitems(tog_commands); i++) {
2546 struct tog_cmd *cmd = &tog_commands[i];
2547 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2553 make_argv(const char *arg0, const char *arg1)
2556 int argc = (arg1 == NULL ? 1 : 2);
2558 argv = calloc(argc, sizeof(char *));
2561 argv[0] = strdup(arg0);
2562 if (argv[0] == NULL)
2565 argv[1] = strdup(arg1);
2566 if (argv[1] == NULL)
2574 main(int argc, char *argv[])
2576 const struct got_error *error = NULL;
2577 struct tog_cmd *cmd = NULL;
2579 char **cmd_argv = NULL;
2581 setlocale(LC_ALL, "");
2583 while ((ch = getopt(argc, argv, "h")) != -1) {
2602 /* Build an argument vector which runs a default command. */
2603 cmd = &tog_commands[0];
2604 cmd_argv = make_argv(cmd->name, NULL);
2609 /* Did the user specific a command? */
2610 for (i = 0; i < nitems(tog_commands); i++) {
2611 if (strncmp(tog_commands[i].name, argv[0],
2612 strlen(argv[0])) == 0) {
2613 cmd = &tog_commands[i];
2615 tog_commands[i].cmd_usage();
2620 /* Did the user specify a repository? */
2621 char *repo_path = realpath(argv[0], NULL);
2623 struct got_repository *repo;
2624 error = got_repo_open(&repo, repo_path);
2626 got_repo_close(repo);
2628 error = got_error_from_errno();
2631 fprintf(stderr, "%s: '%s' is not a "
2632 "known command\n", getprogname(),
2636 fprintf(stderr, "%s: '%s' is neither a known "
2637 "command nor a path to a repository\n",
2638 getprogname(), argv[0]);
2642 cmd = &tog_commands[0];
2643 cmd_argv = make_argv(cmd->name, repo_path);
2651 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2658 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);