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 got_object_id *, struct got_repository *, const char *);
97 static const struct got_error *
98 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
99 static const struct got_error *
100 show_tree_view(struct got_tree_object *, struct got_object_id *,
101 struct got_repository *);
104 close_view(struct tog_view *view)
107 del_panel(view->panel);
109 delwin(view->window);
113 static struct tog_view *
114 open_view(int nlines, int ncols, int begin_y, int begin_x)
116 struct tog_view *view = malloc(sizeof(*view));
123 view->nlines = nlines ? nlines : LINES - begin_y;
124 view->ncols = ncols ? ncols : COLS - begin_x;
125 view->begin_y = begin_y;
126 view->begin_x = begin_x;
127 view->window = newwin(nlines, ncols, begin_y, begin_x);
128 if (view->window == NULL) {
132 view->panel = new_panel(view->window);
133 if (view->panel == NULL) {
138 keypad(view->window, TRUE);
142 const struct got_error *
143 view_resize(struct tog_view *view)
147 if (view->lines > LINES)
148 nlines = view->nlines - (view->lines - LINES);
150 nlines = view->nlines + (LINES - view->lines);
152 if (view->cols > COLS)
153 ncols = view->ncols - (view->cols - COLS);
155 ncols = view->ncols + (COLS - view->cols);
157 if (wresize(view->window, nlines, ncols) == ERR)
158 return got_error_from_errno();
160 view->nlines = nlines;
171 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
176 /* Create newly allocated wide-character string equivalent to a byte string. */
177 static const struct got_error *
178 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
181 const struct got_error *err = NULL;
184 *wlen = mbstowcs(NULL, s, 0);
185 if (*wlen == (size_t)-1) {
188 return got_error_from_errno();
190 /* byte string invalid in current encoding; try to "fix" it */
191 err = got_mbsavis(&vis, &vislen, s);
194 *wlen = mbstowcs(NULL, vis, 0);
195 if (*wlen == (size_t)-1) {
196 err = got_error_from_errno(); /* give up */
201 *ws = calloc(*wlen + 1, sizeof(*ws));
203 err = got_error_from_errno();
207 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
208 err = got_error_from_errno();
219 /* Format a line for display, ensuring that it won't overflow a width limit. */
220 static const struct got_error *
221 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
223 const struct got_error *err = NULL;
225 wchar_t *wline = NULL;
232 err = mbs2ws(&wline, &wlen, line);
237 while (i < wlen && cols < wlimit) {
238 int width = wcwidth(wline[i]);
245 if (cols + width <= wlimit) {
251 if (wline[i] == L'\t')
252 cols += TABSIZE - ((cols + 1) % TABSIZE);
256 err = got_error_from_errno();
271 static const struct got_error *
272 draw_commit(struct tog_view *view, struct got_commit_object *commit,
273 struct got_object_id *id)
275 const struct got_error *err = NULL;
276 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
277 char *logmsg0 = NULL, *logmsg = NULL;
278 char *author0 = NULL, *author = NULL;
279 wchar_t *wlogmsg = NULL, *wauthor = NULL;
280 int author_width, logmsg_width;
281 char *newline, *smallerthan;
284 static const size_t date_display_cols = 9;
285 static const size_t author_display_cols = 16;
286 const int avail = view->ncols;
288 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
290 return got_error(GOT_ERR_NO_SPACE);
292 if (avail < date_display_cols)
293 limit = MIN(sizeof(datebuf) - 1, avail);
295 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
296 waddnstr(view->window, datebuf, limit);
301 author0 = strdup(commit->author);
302 if (author0 == NULL) {
303 err = got_error_from_errno();
307 smallerthan = strchr(author, '<');
311 char *at = strchr(author, '@');
316 err = format_line(&wauthor, &author_width, author, limit);
319 waddwstr(view->window, wauthor);
321 while (col <= avail && author_width < author_display_cols + 1) {
322 waddch(view->window, ' ');
329 logmsg0 = strdup(commit->logmsg);
330 if (logmsg0 == NULL) {
331 err = got_error_from_errno();
335 while (*logmsg == '\n')
337 newline = strchr(logmsg, '\n');
341 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
344 waddwstr(view->window, wlogmsg);
346 while (col <= avail) {
347 waddch(view->window, ' ');
359 struct commit_queue_entry {
360 TAILQ_ENTRY(commit_queue_entry) entry;
361 struct got_object_id *id;
362 struct got_commit_object *commit;
364 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
365 struct commit_queue {
367 struct commit_queue_head head;
370 static struct commit_queue_entry *
371 alloc_commit_queue_entry(struct got_commit_object *commit,
372 struct got_object_id *id)
374 struct commit_queue_entry *entry;
376 entry = calloc(1, sizeof(*entry));
381 entry->commit = commit;
386 pop_commit(struct commit_queue *commits)
388 struct commit_queue_entry *entry;
390 entry = TAILQ_FIRST(&commits->head);
391 TAILQ_REMOVE(&commits->head, entry, entry);
392 got_object_commit_close(entry->commit);
394 /* Don't free entry->id! It is owned by the commit graph. */
399 free_commits(struct commit_queue *commits)
401 while (!TAILQ_EMPTY(&commits->head))
405 static const struct got_error *
406 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
407 struct got_object_id *start_id, int minqueue, int init,
408 struct got_repository *repo, const char *path)
410 const struct got_error *err = NULL;
411 struct got_object_id *id;
412 struct commit_queue_entry *entry;
413 int nfetched, nqueued = 0, found_obj = 0;
414 int is_root_path = strcmp(path, "/") == 0;
416 err = got_commit_graph_iter_start(graph, start_id);
420 entry = TAILQ_LAST(&commits->head, commit_queue_head);
421 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
424 /* Start ID's commit is already on the queue; skip over it. */
425 err = got_commit_graph_iter_next(&id, graph);
426 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
429 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
435 struct got_commit_object *commit;
437 err = got_commit_graph_iter_next(&id, graph);
439 if (err->code != GOT_ERR_ITER_NEED_MORE)
441 if (nqueued >= minqueue) {
445 err = got_commit_graph_fetch_commits(&nfetched,
454 err = got_object_open_as_commit(&commit, repo, id);
459 struct got_object *obj;
460 struct got_object_qid *pid;
463 err = got_object_open_by_path(&obj, repo, id, path);
465 got_object_commit_close(commit);
466 if (err->code == GOT_ERR_NO_OBJ &&
467 (found_obj || !init)) {
468 /* History stops here. */
469 err = got_error(GOT_ERR_ITER_COMPLETED);
475 pid = SIMPLEQ_FIRST(&commit->parent_ids);
477 struct got_object *pobj;
478 err = got_object_open_by_path(&pobj, repo,
481 if (err->code != GOT_ERR_NO_OBJ) {
482 got_object_close(obj);
483 got_object_commit_close(commit);
489 struct got_object_id *id, *pid;
490 id = got_object_get_id(obj);
492 err = got_error_from_errno();
493 got_object_close(obj);
494 got_object_close(pobj);
497 pid = got_object_get_id(pobj);
499 err = got_error_from_errno();
501 got_object_close(obj);
502 got_object_close(pobj);
506 (got_object_id_cmp(id, pid) != 0);
507 got_object_close(pobj);
512 got_object_close(obj);
514 got_object_commit_close(commit);
519 entry = alloc_commit_queue_entry(commit, id);
521 err = got_error_from_errno();
524 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
532 static const struct got_error *
533 fetch_next_commit(struct commit_queue_entry **pentry,
534 struct commit_queue_entry *entry, struct commit_queue *commits,
535 struct got_commit_graph *graph, struct got_repository *repo,
538 const struct got_error *err = NULL;
542 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
546 /* Next entry to display should now be available. */
547 *pentry = TAILQ_NEXT(entry, entry);
549 return got_error(GOT_ERR_NO_OBJ);
554 static const struct got_error *
555 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
557 const struct got_error *err = NULL;
558 struct got_reference *head_ref;
562 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
566 err = got_ref_resolve(head_id, repo, head_ref);
567 got_ref_close(head_ref);
576 static const struct got_error *
577 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
578 struct commit_queue_entry **selected, struct commit_queue_entry *first,
579 struct commit_queue *commits, int selected_idx, int limit,
580 struct got_commit_graph *graph, struct got_repository *repo,
583 const struct got_error *err = NULL;
584 struct commit_queue_entry *entry;
586 char *id_str, *header;
592 if (ncommits == selected_idx) {
596 entry = TAILQ_NEXT(entry, entry);
600 err = got_object_id_str(&id_str, (*selected)->id);
605 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
606 err = got_error_from_errno();
610 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
611 err = got_error_from_errno();
616 err = format_line(&wline, &width, header, view->ncols);
623 werase(view->window);
625 waddwstr(view->window, wline);
626 if (width < view->ncols)
627 waddch(view->window, '\n');
636 if (ncommits >= limit - 1)
638 if (ncommits == selected_idx)
639 wstandout(view->window);
640 err = draw_commit(view, entry->commit, entry->id);
641 if (ncommits == selected_idx)
642 wstandend(view->window);
647 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
648 err = queue_commits(graph, commits, entry->id, 1,
651 if (err->code != GOT_ERR_ITER_COMPLETED)
656 entry = TAILQ_NEXT(entry, entry);
666 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
667 struct commit_queue *commits)
669 struct commit_queue_entry *entry;
672 entry = TAILQ_FIRST(&commits->head);
673 if (*first_displayed_entry == entry)
676 entry = *first_displayed_entry;
677 while (entry && nscrolled < maxscroll) {
678 entry = TAILQ_PREV(entry, commit_queue_head, entry);
680 *first_displayed_entry = entry;
686 static const struct got_error *
687 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
688 struct commit_queue_entry *last_displayed_entry,
689 struct commit_queue *commits, struct got_commit_graph *graph,
690 struct got_repository *repo, const char *path)
692 const struct got_error *err = NULL;
693 struct commit_queue_entry *pentry;
697 pentry = TAILQ_NEXT(last_displayed_entry, entry);
698 if (pentry == NULL) {
699 err = fetch_next_commit(&pentry, last_displayed_entry,
700 commits, graph, repo, path);
701 if (err || pentry == NULL)
704 last_displayed_entry = pentry;
706 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
709 *first_displayed_entry = pentry;
710 } while (++nscrolled < maxscroll);
715 static const struct got_error *
716 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
718 const struct got_error *err;
719 struct got_object *obj1 = NULL, *obj2 = NULL;
720 struct got_object_qid *parent_id;
721 struct tog_view *view;
723 err = got_object_open(&obj2, repo, entry->id);
727 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
729 err = got_object_open(&obj1, repo, parent_id->id);
734 view = open_view(0, 0, 0, 0);
736 err = got_error_from_errno();
740 err = show_diff_view(view, obj1, obj2, repo);
744 got_object_close(obj1);
746 got_object_close(obj2);
750 static const struct got_error *
751 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
753 const struct got_error *err = NULL;
754 struct got_tree_object *tree;
756 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
760 err = show_tree_view(tree, entry->id, repo);
761 got_object_tree_close(tree);
765 static const struct got_error *
766 show_log_view(struct got_object_id *start_id, struct got_repository *repo,
769 const struct got_error *err = NULL;
770 struct got_object_id *head_id = NULL;
771 int ch, done = 0, selected = 0, nfetched;
772 struct got_commit_graph *graph = NULL;
773 struct commit_queue commits;
774 struct commit_queue_entry *first_displayed_entry = NULL;
775 struct commit_queue_entry *last_displayed_entry = NULL;
776 struct commit_queue_entry *selected_entry = NULL;
777 char *in_repo_path = NULL;
778 struct tog_view *view = NULL;
780 err = got_repo_map_path(&in_repo_path, repo, path);
784 err = get_head_commit_id(&head_id, repo);
788 /* The graph contains all commits. */
789 err = got_commit_graph_open(&graph, head_id, 0, repo);
792 /* The commit queue contains a subset of commits filtered by path. */
793 TAILQ_INIT(&commits.head);
794 commits.ncommits = 0;
796 /* Populate commit graph with a sufficient number of commits. */
797 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
802 view = open_view(0, 0, 0, 0);
804 err = got_error_from_errno();
807 show_panel(view->panel);
810 * Open the initial batch of commits, sorted in commit graph order.
811 * We keep all commits open throughout the lifetime of the log view
812 * in order to avoid having to re-fetch commits from disk while
813 * updating the display.
815 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
818 if (err->code != GOT_ERR_ITER_COMPLETED)
823 first_displayed_entry = TAILQ_FIRST(&commits.head);
824 selected_entry = first_displayed_entry;
826 err = draw_commits(view, &last_displayed_entry, &selected_entry,
827 first_displayed_entry, &commits, selected, view->nlines,
828 graph, repo, in_repo_path);
832 nodelay(stdscr, FALSE);
833 ch = wgetch(view->window);
834 nodelay(stdscr, TRUE);
847 scroll_up(&first_displayed_entry, 1, &commits);
850 if (TAILQ_FIRST(&commits.head) ==
851 first_displayed_entry) {
855 scroll_up(&first_displayed_entry, view->nlines,
860 if (selected < MIN(view->nlines - 2,
861 commits.ncommits - 1)) {
865 err = scroll_down(&first_displayed_entry, 1,
866 last_displayed_entry, &commits, graph,
869 if (err->code != GOT_ERR_ITER_COMPLETED)
875 struct commit_queue_entry *first = first_displayed_entry;
876 err = scroll_down(&first_displayed_entry, view->nlines,
877 last_displayed_entry, &commits, graph,
880 if (err->code != GOT_ERR_ITER_COMPLETED)
882 /* can't scroll any further; move cursor down */
883 if (first == first_displayed_entry && selected <
884 MIN(view->nlines - 2, commits.ncommits - 1)) {
885 selected = MIN(view->nlines - 2,
886 commits.ncommits - 1);
894 if (selected > view->nlines - 2)
895 selected = view->nlines - 2;
896 if (selected > commits.ncommits - 1)
897 selected = commits.ncommits - 1;
901 err = show_commit(selected_entry, repo);
904 show_panel(view->panel);
907 err = browse_commit(selected_entry, repo);
910 show_panel(view->panel);
921 got_commit_graph_close(graph);
922 free_commits(&commits);
927 static const struct got_error *
928 cmd_log(int argc, char *argv[])
930 const struct got_error *error;
931 struct got_repository *repo = NULL;
932 struct got_object_id *start_id = NULL;
933 char *path = NULL, *repo_path = NULL, *cwd = NULL;
934 char *start_commit = NULL;
938 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
942 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
945 start_commit = optarg;
948 repo_path = realpath(optarg, NULL);
949 if (repo_path == NULL)
964 path = strdup(argv[0]);
968 return got_error_from_errno();
970 cwd = getcwd(NULL, 0);
972 error = got_error_from_errno();
975 if (repo_path == NULL) {
976 repo_path = strdup(cwd);
977 if (repo_path == NULL) {
978 error = got_error_from_errno();
983 error = got_repo_open(&repo, repo_path);
987 if (start_commit == NULL) {
988 error = get_head_commit_id(&start_id, repo);
992 struct got_object *obj;
993 error = got_object_open_by_id_str(&obj, repo, start_commit);
995 start_id = got_object_get_id(obj);
996 if (start_id == NULL)
997 error = got_error_from_errno();
1004 error = show_log_view(start_id, repo, path);
1011 got_repo_close(repo);
1019 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1025 parse_next_line(FILE *f, size_t *len)
1030 const char delim[3] = { '\0', '\0', '\0'};
1032 line = fparseln(f, &linelen, &lineno, delim, 0);
1038 static const struct got_error *
1039 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1040 int *last_displayed_line, int *eof, int max_lines)
1042 const struct got_error *err;
1043 int nlines = 0, nprinted = 0;
1050 werase(view->window);
1053 while (nprinted < max_lines) {
1054 line = parse_next_line(f, &len);
1059 if (++nlines < *first_displayed_line) {
1064 err = format_line(&wline, &width, line, view->ncols);
1070 waddwstr(view->window, wline);
1071 if (width < view->ncols)
1072 waddch(view->window, '\n');
1073 if (++nprinted == 1)
1074 *first_displayed_line = nlines;
1079 *last_displayed_line = nlines;
1087 static const struct got_error *
1088 show_diff_view(struct tog_view *view, struct got_object *obj1,
1089 struct got_object *obj2, struct got_repository *repo)
1091 const struct got_error *err;
1094 int first_displayed_line = 1, last_displayed_line = view->nlines;
1097 if (obj1 != NULL && obj2 != NULL &&
1098 got_object_get_type(obj1) != got_object_get_type(obj2))
1099 return got_error(GOT_ERR_OBJ_TYPE);
1103 return got_error_from_errno();
1105 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1106 case GOT_OBJ_TYPE_BLOB:
1107 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1109 case GOT_OBJ_TYPE_TREE:
1110 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1112 case GOT_OBJ_TYPE_COMMIT:
1113 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1116 return got_error(GOT_ERR_OBJ_TYPE);
1121 show_panel(view->panel);
1124 err = draw_file(view, f, &first_displayed_line,
1125 &last_displayed_line, &eof, view->nlines);
1128 nodelay(stdscr, FALSE);
1129 ch = wgetch(view->window);
1130 nodelay(stdscr, TRUE);
1137 if (first_displayed_line > 1)
1138 first_displayed_line--;
1143 while (i++ < view->nlines - 1 &&
1144 first_displayed_line > 1)
1145 first_displayed_line--;
1150 first_displayed_line++;
1155 while (!eof && i++ < view->nlines - 1) {
1156 char *line = parse_next_line(f, NULL);
1157 first_displayed_line++;
1173 static const struct got_error *
1174 cmd_diff(int argc, char *argv[])
1176 const struct got_error *error = NULL;
1177 struct got_repository *repo = NULL;
1178 struct got_object *obj1 = NULL, *obj2 = NULL;
1179 char *repo_path = NULL;
1180 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1182 struct tog_view *view;
1185 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1189 while ((ch = getopt(argc, argv, "")) != -1) {
1201 usage_diff(); /* TODO show local worktree changes */
1202 } else if (argc == 2) {
1203 repo_path = getcwd(NULL, 0);
1204 if (repo_path == NULL)
1205 return got_error_from_errno();
1206 obj_id_str1 = argv[0];
1207 obj_id_str2 = argv[1];
1208 } else if (argc == 3) {
1209 repo_path = realpath(argv[0], NULL);
1210 if (repo_path == NULL)
1211 return got_error_from_errno();
1212 obj_id_str1 = argv[1];
1213 obj_id_str2 = argv[2];
1217 error = got_repo_open(&repo, repo_path);
1222 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1226 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1230 view = open_view(0, 0, 0, 0);
1232 error = got_error_from_errno();
1235 error = show_diff_view(view, obj1, obj2, repo);
1238 got_repo_close(repo);
1240 got_object_close(obj1);
1242 got_object_close(obj2);
1250 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1255 struct tog_blame_line {
1257 struct got_object_id *id;
1260 static const struct got_error *
1261 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1262 const char *path, struct tog_blame_line *lines, int nlines,
1263 int blame_complete, int selected_line, int *first_displayed_line,
1264 int *last_displayed_line, int *eof, int max_lines)
1266 const struct got_error *err;
1267 int lineno = 0, nprinted = 0;
1272 struct tog_blame_line *blame_line;
1273 struct got_object_id *prev_id = NULL;
1276 err = got_object_id_str(&id_str, id);
1281 werase(view->window);
1283 if (asprintf(&line, "commit: %s", id_str) == -1) {
1284 err = got_error_from_errno();
1289 err = format_line(&wline, &width, line, view->ncols);
1292 waddwstr(view->window, wline);
1295 if (width < view->ncols)
1296 waddch(view->window, '\n');
1298 if (asprintf(&line, "[%d/%d] %s%s",
1299 *first_displayed_line - 1 + selected_line, nlines,
1300 blame_complete ? "" : "annotating ", path) == -1) {
1302 return got_error_from_errno();
1305 err = format_line(&wline, &width, line, view->ncols);
1310 waddwstr(view->window, wline);
1313 if (width < view->ncols)
1314 waddch(view->window, '\n');
1317 while (nprinted < max_lines - 2) {
1318 line = parse_next_line(f, &len);
1323 if (++lineno < *first_displayed_line) {
1328 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1329 err = format_line(&wline, &width, line, wlimit);
1335 if (nprinted == selected_line - 1)
1336 wstandout(view->window);
1338 blame_line = &lines[lineno - 1];
1339 if (blame_line->annotated && prev_id &&
1340 got_object_id_cmp(prev_id, blame_line->id) == 0)
1341 waddstr(view->window, " ");
1342 else if (blame_line->annotated) {
1344 err = got_object_id_str(&id_str, blame_line->id);
1350 wprintw(view->window, "%.8s ", id_str);
1352 prev_id = blame_line->id;
1354 waddstr(view->window, "........ ");
1358 waddwstr(view->window, wline);
1359 while (width < wlimit) {
1360 waddch(view->window, ' ');
1363 if (nprinted == selected_line - 1)
1364 wstandend(view->window);
1365 if (++nprinted == 1)
1366 *first_displayed_line = lineno;
1371 *last_displayed_line = lineno;
1379 struct tog_blame_cb_args {
1380 pthread_mutex_t *mutex;
1381 struct tog_blame_line *lines; /* one per line */
1384 struct tog_view *view;
1385 struct got_object_id *commit_id;
1388 int *first_displayed_line;
1389 int *last_displayed_line;
1394 static const struct got_error *
1395 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1397 const struct got_error *err = NULL;
1398 struct tog_blame_cb_args *a = arg;
1399 struct tog_blame_line *line;
1402 if (nlines != a->nlines ||
1403 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1404 return got_error(GOT_ERR_RANGE);
1406 if (pthread_mutex_lock(a->mutex) != 0)
1407 return got_error_from_errno();
1409 if (*a->quit) { /* user has quit the blame view */
1410 err = got_error(GOT_ERR_ITER_COMPLETED);
1415 goto done; /* no change in this commit */
1417 line = &a->lines[lineno - 1];
1418 if (line->annotated)
1421 line->id = got_object_id_dup(id);
1422 if (line->id == NULL) {
1423 err = got_error_from_errno();
1426 line->annotated = 1;
1428 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1429 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1430 a->last_displayed_line, &eof, a->view->nlines);
1432 if (pthread_mutex_unlock(a->mutex) != 0)
1433 return got_error_from_errno();
1437 struct tog_blame_thread_args {
1439 struct got_repository *repo;
1440 struct tog_blame_cb_args *cb_args;
1445 blame_thread(void *arg)
1447 const struct got_error *err;
1448 struct tog_blame_thread_args *ta = arg;
1449 struct tog_blame_cb_args *a = ta->cb_args;
1452 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1453 blame_cb, ta->cb_args);
1455 if (pthread_mutex_lock(a->mutex) != 0)
1456 return (void *)got_error_from_errno();
1458 got_repo_close(ta->repo);
1462 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1463 a->lines, a->nlines, 1, *a->selected_line,
1464 a->first_displayed_line, a->last_displayed_line, &eof,
1467 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1468 err = got_error_from_errno();
1473 static struct got_object_id *
1474 get_selected_commit_id(struct tog_blame_line *lines,
1475 int first_displayed_line, int selected_line)
1477 struct tog_blame_line *line;
1479 line = &lines[first_displayed_line - 1 + selected_line - 1];
1480 if (!line->annotated)
1486 static const struct got_error *
1487 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1488 struct tog_blame_line *lines, int first_displayed_line,
1489 int selected_line, struct got_repository *repo)
1491 const struct got_error *err = NULL;
1492 struct got_commit_object *commit = NULL;
1493 struct got_object_id *selected_id;
1494 struct got_object_qid *pid;
1499 selected_id = get_selected_commit_id(lines,
1500 first_displayed_line, selected_line);
1501 if (selected_id == NULL)
1504 err = got_object_open(obj, repo, selected_id);
1508 err = got_object_commit_open(&commit, repo, *obj);
1512 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1514 err = got_object_open(pobj, repo, pid->id);
1520 got_object_commit_close(commit);
1527 struct tog_blame_line *lines;
1530 struct tog_blame_thread_args thread_args;
1531 struct tog_blame_cb_args cb_args;
1535 static const struct got_error *
1536 stop_blame(struct tog_blame *blame)
1538 const struct got_error *err = NULL;
1541 if (blame->thread) {
1542 if (pthread_join(blame->thread, (void **)&err) != 0)
1543 err = got_error_from_errno();
1544 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1546 blame->thread = NULL;
1548 if (blame->thread_args.repo) {
1549 got_repo_close(blame->thread_args.repo);
1550 blame->thread_args.repo = NULL;
1556 for (i = 0; i < blame->nlines; i++)
1557 free(blame->lines[i].id);
1559 blame->lines = NULL;
1560 free(blame->cb_args.commit_id);
1561 blame->cb_args.commit_id = NULL;
1566 static const struct got_error *
1567 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1568 struct tog_view *view, int *blame_complete,
1569 int *first_displayed_line, int *last_displayed_line,
1570 int *selected_line, int *done, const char *path,
1571 struct got_object_id *commit_id,
1572 struct got_repository *repo)
1574 const struct got_error *err = NULL;
1575 struct got_blob_object *blob = NULL;
1576 struct got_repository *thread_repo = NULL;
1577 struct got_object *obj;
1579 err = got_object_open_by_path(&obj, repo, commit_id, path);
1582 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1583 err = got_error(GOT_ERR_OBJ_TYPE);
1587 err = got_object_blob_open(&blob, repo, obj, 8192);
1590 blame->f = got_opentemp();
1591 if (blame->f == NULL) {
1592 err = got_error_from_errno();
1595 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1600 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1601 if (blame->lines == NULL) {
1602 err = got_error_from_errno();
1606 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1610 blame->cb_args.view = view;
1611 blame->cb_args.lines = blame->lines;
1612 blame->cb_args.nlines = blame->nlines;
1613 blame->cb_args.mutex = mutex;
1614 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1615 if (blame->cb_args.commit_id == NULL) {
1616 err = got_error_from_errno();
1619 blame->cb_args.f = blame->f;
1620 blame->cb_args.path = path;
1621 blame->cb_args.first_displayed_line = first_displayed_line;
1622 blame->cb_args.selected_line = selected_line;
1623 blame->cb_args.last_displayed_line = last_displayed_line;
1624 blame->cb_args.quit = done;
1626 blame->thread_args.path = path;
1627 blame->thread_args.repo = thread_repo;
1628 blame->thread_args.cb_args = &blame->cb_args;
1629 blame->thread_args.complete = blame_complete;
1630 *blame_complete = 0;
1632 if (pthread_create(&blame->thread, NULL, blame_thread,
1633 &blame->thread_args) != 0) {
1634 err = got_error_from_errno();
1640 got_object_blob_close(blob);
1642 got_object_close(obj);
1648 static const struct got_error *
1649 show_blame_view(const char *path, struct got_object_id *commit_id,
1650 struct got_repository *repo)
1652 const struct got_error *err = NULL, *thread_err = NULL;
1653 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1654 int selected_line = first_displayed_line;
1655 int eof, blame_complete = 0;
1656 struct got_object *obj = NULL, *pobj = NULL;
1657 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1658 struct tog_blame blame;
1659 struct got_object_id_queue blamed_commits;
1660 struct got_object_qid *blamed_commit = NULL;
1661 struct tog_view *view = NULL, *diff_view;
1663 SIMPLEQ_INIT(&blamed_commits);
1665 if (pthread_mutex_init(&mutex, NULL) != 0) {
1666 err = got_error_from_errno();
1670 err = got_object_qid_alloc(&blamed_commit, commit_id);
1673 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1675 view = open_view(0, 0, 0, 0);
1677 err = got_error_from_errno();
1680 show_panel(view->panel);
1681 last_displayed_line = view->nlines;
1683 memset(&blame, 0, sizeof(blame));
1684 err = run_blame(&blame, &mutex, view, &blame_complete,
1685 &first_displayed_line, &last_displayed_line,
1686 &selected_line, &done, path, blamed_commit->id, repo);
1691 if (pthread_mutex_lock(&mutex) != 0) {
1692 err = got_error_from_errno();
1695 err = draw_blame(view, blamed_commit->id, blame.f, path,
1696 blame.lines, blame.nlines, blame_complete, selected_line,
1697 &first_displayed_line, &last_displayed_line, &eof,
1699 if (pthread_mutex_unlock(&mutex) != 0) {
1700 err = got_error_from_errno();
1705 nodelay(stdscr, FALSE);
1706 ch = wgetch(view->window);
1707 nodelay(stdscr, TRUE);
1708 if (pthread_mutex_lock(&mutex) != 0) {
1709 err = got_error_from_errno();
1718 if (selected_line > 1)
1720 else if (selected_line == 1 &&
1721 first_displayed_line > 1)
1722 first_displayed_line--;
1726 if (first_displayed_line == 1) {
1730 if (first_displayed_line > view->nlines - 2)
1731 first_displayed_line -=
1734 first_displayed_line = 1;
1738 if (selected_line < view->nlines - 2 &&
1739 first_displayed_line + selected_line <=
1742 else if (last_displayed_line < blame.nlines)
1743 first_displayed_line++;
1747 struct got_object_id *id;
1748 id = get_selected_commit_id(blame.lines,
1749 first_displayed_line, selected_line);
1750 if (id == NULL || got_object_id_cmp(id,
1751 blamed_commit->id) == 0)
1753 err = open_selected_commit(&pobj, &obj,
1754 blame.lines, first_displayed_line,
1755 selected_line, repo);
1758 if (pobj == NULL && obj == NULL)
1760 if (ch == 'p' && pobj == NULL)
1763 if (pthread_mutex_unlock(&mutex) != 0) {
1764 err = got_error_from_errno();
1767 thread_err = stop_blame(&blame);
1769 if (pthread_mutex_lock(&mutex) != 0) {
1770 err = got_error_from_errno();
1775 id = got_object_get_id(ch == 'b' ? obj : pobj);
1776 got_object_close(obj);
1779 got_object_close(pobj);
1783 err = got_error_from_errno();
1786 err = got_object_qid_alloc(&blamed_commit, id);
1790 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1791 blamed_commit, entry);
1792 err = run_blame(&blame, &mutex, view,
1793 &blame_complete, &first_displayed_line,
1794 &last_displayed_line, &selected_line,
1795 &done, path, blamed_commit->id, repo);
1801 struct got_object_qid *first;
1802 first = SIMPLEQ_FIRST(&blamed_commits);
1803 if (!got_object_id_cmp(first->id, commit_id))
1806 if (pthread_mutex_unlock(&mutex) != 0) {
1807 err = got_error_from_errno();
1810 thread_err = stop_blame(&blame);
1812 if (pthread_mutex_lock(&mutex) != 0) {
1813 err = got_error_from_errno();
1818 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1819 got_object_qid_free(blamed_commit);
1820 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1821 err = run_blame(&blame, &mutex, view,
1822 &blame_complete, &first_displayed_line,
1823 &last_displayed_line, &selected_line,
1824 &done, path, blamed_commit->id, repo);
1831 err = open_selected_commit(&pobj, &obj,
1832 blame.lines, first_displayed_line,
1833 selected_line, repo);
1836 if (pobj == NULL && obj == NULL)
1838 diff_view = open_view(0, 0, 0, 0);
1839 if (diff_view == NULL) {
1840 err = got_error_from_errno();
1843 err = show_diff_view(diff_view, pobj, obj, repo);
1844 close_view(diff_view);
1846 got_object_close(pobj);
1849 got_object_close(obj);
1851 show_panel(view->panel);
1857 if (last_displayed_line >= blame.nlines &&
1858 selected_line < view->nlines - 2) {
1859 selected_line = MIN(blame.nlines,
1863 if (last_displayed_line + view->nlines - 2 <=
1865 first_displayed_line +=
1868 first_displayed_line =
1869 blame.nlines - (view->nlines - 3);
1873 if (selected_line > view->nlines - 2) {
1874 selected_line = MIN(blame.nlines,
1881 if (pthread_mutex_unlock(&mutex) != 0)
1882 err = got_error_from_errno();
1883 if (err || thread_err)
1888 got_object_close(pobj);
1890 thread_err = stop_blame(&blame);
1893 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1894 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1895 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1896 got_object_qid_free(blamed_commit);
1898 return thread_err ? thread_err : err;
1901 static const struct got_error *
1902 cmd_blame(int argc, char *argv[])
1904 const struct got_error *error;
1905 struct got_repository *repo = NULL;
1906 char *repo_path = NULL;
1908 struct got_object_id *commit_id = NULL;
1909 char *commit_id_str = NULL;
1913 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1917 while ((ch = getopt(argc, argv, "c:")) != -1) {
1920 commit_id_str = optarg;
1933 } else if (argc == 1) {
1934 repo_path = getcwd(NULL, 0);
1935 if (repo_path == NULL)
1936 return got_error_from_errno();
1938 } else if (argc == 2) {
1939 repo_path = realpath(argv[0], NULL);
1940 if (repo_path == NULL)
1941 return got_error_from_errno();
1946 error = got_repo_open(&repo, repo_path);
1951 if (commit_id_str == NULL) {
1952 struct got_reference *head_ref;
1953 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1956 error = got_ref_resolve(&commit_id, repo, head_ref);
1957 got_ref_close(head_ref);
1959 struct got_object *obj;
1960 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1963 commit_id = got_object_get_id(obj);
1964 if (commit_id == NULL)
1965 error = got_error_from_errno();
1966 got_object_close(obj);
1971 error = show_blame_view(path, commit_id, repo);
1975 got_repo_close(repo);
1979 static const struct got_error *
1980 draw_tree_entries(struct tog_view *view,
1981 struct got_tree_entry **first_displayed_entry,
1982 struct got_tree_entry **last_displayed_entry,
1983 struct got_tree_entry **selected_entry, int *ndisplayed,
1984 const char *label, int show_ids, const char *parent_path,
1985 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1987 const struct got_error *err = NULL;
1988 struct got_tree_entry *te;
1994 werase(view->window);
1999 err = format_line(&wline, &width, label, view->ncols);
2002 waddwstr(view->window, wline);
2005 if (width < view->ncols)
2006 waddch(view->window, '\n');
2009 err = format_line(&wline, &width, parent_path, view->ncols);
2012 waddwstr(view->window, wline);
2015 if (width < view->ncols)
2016 waddch(view->window, '\n');
2019 waddch(view->window, '\n');
2023 te = SIMPLEQ_FIRST(&entries->head);
2024 if (*first_displayed_entry == NULL) {
2025 if (selected == 0) {
2026 wstandout(view->window);
2027 *selected_entry = NULL;
2029 waddstr(view->window, " ..\n"); /* parent directory */
2031 wstandend(view->window);
2038 while (te != *first_displayed_entry)
2039 te = SIMPLEQ_NEXT(te, entry);
2043 char *line = NULL, *id_str = NULL;
2046 err = got_object_id_str(&id_str, te->id);
2048 return got_error_from_errno();
2050 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2051 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2053 return got_error_from_errno();
2056 err = format_line(&wline, &width, line, view->ncols);
2061 if (n == selected) {
2062 wstandout(view->window);
2063 *selected_entry = te;
2065 waddwstr(view->window, wline);
2066 if (width < view->ncols)
2067 waddch(view->window, '\n');
2069 wstandend(view->window);
2075 *last_displayed_entry = te;
2078 te = SIMPLEQ_NEXT(te, entry);
2085 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2086 const struct got_tree_entries *entries, int isroot)
2088 struct got_tree_entry *te, *prev;
2091 if (*first_displayed_entry == NULL)
2094 te = SIMPLEQ_FIRST(&entries->head);
2095 if (*first_displayed_entry == te) {
2097 *first_displayed_entry = NULL;
2101 /* XXX this is stupid... switch to TAILQ? */
2102 for (i = 0; i < maxscroll; i++) {
2103 while (te != *first_displayed_entry) {
2105 te = SIMPLEQ_NEXT(te, entry);
2107 *first_displayed_entry = prev;
2108 te = SIMPLEQ_FIRST(&entries->head);
2110 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2111 *first_displayed_entry = NULL;
2115 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2116 struct got_tree_entry *last_displayed_entry,
2117 const struct got_tree_entries *entries)
2119 struct got_tree_entry *next;
2122 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2125 if (*first_displayed_entry)
2126 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2128 next = SIMPLEQ_FIRST(&entries->head);
2130 *first_displayed_entry = next;
2131 if (++n >= maxscroll)
2133 next = SIMPLEQ_NEXT(next, entry);
2137 struct tog_parent_tree {
2138 TAILQ_ENTRY(tog_parent_tree) entry;
2139 struct got_tree_object *tree;
2140 struct got_tree_entry *first_displayed_entry;
2141 struct got_tree_entry *selected_entry;
2145 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2147 static const struct got_error *
2148 tree_entry_path(char **path, struct tog_parent_trees *parents,
2149 struct got_tree_entry *te)
2151 const struct got_error *err = NULL;
2152 struct tog_parent_tree *pt;
2153 size_t len = 2; /* for leading slash and NUL */
2155 TAILQ_FOREACH(pt, parents, entry)
2156 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2158 len += strlen(te->name);
2160 *path = calloc(1, len);
2162 return got_error_from_errno();
2165 pt = TAILQ_LAST(parents, tog_parent_trees);
2167 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2168 err = got_error(GOT_ERR_NO_SPACE);
2171 if (strlcat(*path, "/", len) >= len) {
2172 err = got_error(GOT_ERR_NO_SPACE);
2175 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2178 if (strlcat(*path, te->name, len) >= len) {
2179 err = got_error(GOT_ERR_NO_SPACE);
2191 static const struct got_error *
2192 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2193 struct got_object_id *commit_id, struct got_repository *repo)
2195 const struct got_error *err = NULL;
2198 err = tree_entry_path(&path, parents, te);
2202 err = show_blame_view(path, commit_id, repo);
2207 static const struct got_error *
2208 log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2209 struct got_object_id *commit_id, struct got_repository *repo)
2211 const struct got_error *err = NULL;
2214 err = tree_entry_path(&path, parents, te);
2218 err = show_log_view(commit_id, repo, path);
2223 static const struct got_error *
2224 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2225 struct got_repository *repo)
2227 const struct got_error *err = NULL;
2228 int ch, done = 0, selected = 0, show_ids = 0;
2229 struct got_tree_object *tree = root;
2230 const struct got_tree_entries *entries;
2231 struct got_tree_entry *first_displayed_entry = NULL;
2232 struct got_tree_entry *last_displayed_entry = NULL;
2233 struct got_tree_entry *selected_entry = NULL;
2234 char *commit_id_str = NULL, *tree_label = NULL;
2235 int nentries, ndisplayed;
2236 struct tog_parent_trees parents;
2237 struct tog_view *view = NULL;
2239 TAILQ_INIT(&parents);
2241 err = got_object_id_str(&commit_id_str, commit_id);
2245 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2246 err = got_error_from_errno();
2250 view = open_view(0, 0, 0, 0);
2252 err = got_error_from_errno();
2255 show_panel(view->panel);
2257 entries = got_object_tree_get_entries(root);
2258 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2261 entries = got_object_tree_get_entries(tree);
2262 nentries = entries->nentries;
2264 nentries++; /* '..' directory */
2266 err = tree_entry_path(&parent_path, &parents, NULL);
2270 err = draw_tree_entries(view, &first_displayed_entry,
2271 &last_displayed_entry, &selected_entry, &ndisplayed,
2272 tree_label, show_ids, parent_path, entries, selected,
2273 view->nlines, tree == root);
2278 nodelay(stdscr, FALSE);
2279 ch = wgetch(view->window);
2280 nodelay(stdscr, TRUE);
2286 show_ids = !show_ids;
2289 if (selected_entry) {
2290 err = log_tree_entry(selected_entry,
2291 &parents, commit_id, repo);
2302 tree_scroll_up(&first_displayed_entry, 1,
2303 entries, tree == root);
2306 if (SIMPLEQ_FIRST(&entries->head) ==
2307 first_displayed_entry) {
2309 first_displayed_entry = NULL;
2313 tree_scroll_up(&first_displayed_entry,
2314 view->nlines, entries, tree == root);
2318 if (selected < ndisplayed - 1) {
2322 tree_scroll_down(&first_displayed_entry, 1,
2323 last_displayed_entry, entries);
2326 tree_scroll_down(&first_displayed_entry,
2327 view->nlines, last_displayed_entry,
2329 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2331 /* can't scroll any further; move cursor down */
2332 if (selected < ndisplayed - 1)
2333 selected = ndisplayed - 1;
2337 if (selected_entry == NULL) {
2338 struct tog_parent_tree *parent;
2340 /* user selected '..' */
2343 parent = TAILQ_FIRST(&parents);
2344 TAILQ_REMOVE(&parents, parent, entry);
2345 got_object_tree_close(tree);
2346 tree = parent->tree;
2347 first_displayed_entry =
2348 parent->first_displayed_entry;
2349 selected_entry = parent->selected_entry;
2350 selected = parent->selected;
2352 } else if (S_ISDIR(selected_entry->mode)) {
2353 struct tog_parent_tree *parent;
2354 struct got_tree_object *child;
2355 err = got_object_open_as_tree(
2356 &child, repo, selected_entry->id);
2359 parent = calloc(1, sizeof(*parent));
2360 if (parent == NULL) {
2361 err = got_error_from_errno();
2364 parent->tree = tree;
2365 parent->first_displayed_entry =
2366 first_displayed_entry;
2367 parent->selected_entry = selected_entry;
2368 parent->selected = selected;
2369 TAILQ_INSERT_HEAD(&parents, parent,
2373 first_displayed_entry = NULL;
2374 } else if (S_ISREG(selected_entry->mode)) {
2375 err = blame_tree_entry(selected_entry,
2376 &parents, commit_id, repo);
2383 if (selected > view->nlines)
2384 selected = ndisplayed - 1;
2394 free(commit_id_str);
2395 while (!TAILQ_EMPTY(&parents)) {
2396 struct tog_parent_tree *parent;
2397 parent = TAILQ_FIRST(&parents);
2398 TAILQ_REMOVE(&parents, parent, entry);
2403 got_object_tree_close(tree);
2411 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2416 static const struct got_error *
2417 cmd_tree(int argc, char *argv[])
2419 const struct got_error *error;
2420 struct got_repository *repo = NULL;
2421 char *repo_path = NULL;
2422 struct got_object_id *commit_id = NULL;
2423 char *commit_id_arg = NULL;
2424 struct got_commit_object *commit = NULL;
2425 struct got_tree_object *tree = NULL;
2429 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2433 while ((ch = getopt(argc, argv, "c:")) != -1) {
2436 commit_id_arg = optarg;
2448 repo_path = getcwd(NULL, 0);
2449 if (repo_path == NULL)
2450 return got_error_from_errno();
2451 } else if (argc == 1) {
2452 repo_path = realpath(argv[0], NULL);
2453 if (repo_path == NULL)
2454 return got_error_from_errno();
2458 error = got_repo_open(&repo, repo_path);
2463 if (commit_id_arg == NULL) {
2464 error = get_head_commit_id(&commit_id, repo);
2468 struct got_object *obj;
2469 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2470 if (error == NULL) {
2471 commit_id = got_object_get_id(obj);
2472 if (commit_id == NULL)
2473 error = got_error_from_errno();
2479 error = got_object_open_as_commit(&commit, repo, commit_id);
2483 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2487 error = show_tree_view(tree, commit_id, repo);
2491 got_object_commit_close(commit);
2493 got_object_tree_close(tree);
2495 got_repo_close(repo);
2505 intrflush(stdscr, FALSE);
2506 keypad(stdscr, TRUE);
2515 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2516 "Available commands:\n", getprogname());
2517 for (i = 0; i < nitems(tog_commands); i++) {
2518 struct tog_cmd *cmd = &tog_commands[i];
2519 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2525 make_argv(const char *arg0, const char *arg1)
2528 int argc = (arg1 == NULL ? 1 : 2);
2530 argv = calloc(argc, sizeof(char *));
2533 argv[0] = strdup(arg0);
2534 if (argv[0] == NULL)
2537 argv[1] = strdup(arg1);
2538 if (argv[1] == NULL)
2546 main(int argc, char *argv[])
2548 const struct got_error *error = NULL;
2549 struct tog_cmd *cmd = NULL;
2551 char **cmd_argv = NULL;
2553 setlocale(LC_ALL, "");
2555 while ((ch = getopt(argc, argv, "h")) != -1) {
2574 /* Build an argument vector which runs a default command. */
2575 cmd = &tog_commands[0];
2576 cmd_argv = make_argv(cmd->name, NULL);
2581 /* Did the user specific a command? */
2582 for (i = 0; i < nitems(tog_commands); i++) {
2583 if (strncmp(tog_commands[i].name, argv[0],
2584 strlen(argv[0])) == 0) {
2585 cmd = &tog_commands[i];
2587 tog_commands[i].cmd_usage();
2592 /* Did the user specify a repository? */
2593 char *repo_path = realpath(argv[0], NULL);
2595 struct got_repository *repo;
2596 error = got_repo_open(&repo, repo_path);
2598 got_repo_close(repo);
2600 error = got_error_from_errno();
2603 fprintf(stderr, "%s: '%s' is not a "
2604 "known command\n", getprogname(),
2608 fprintf(stderr, "%s: '%s' is neither a known "
2609 "command nor a path to a repository\n",
2610 getprogname(), argv[0]);
2614 cmd = &tog_commands[0];
2615 cmd_argv = make_argv(cmd->name, repo_path);
2623 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2630 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);