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 */
90 struct tog_view *parent;
93 static const struct got_error *
94 show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
95 struct got_repository *);
96 static const struct got_error *
97 show_log_view(struct tog_view *, struct got_object_id *,
98 struct got_repository *, const char *);
99 static const struct got_error *
100 show_blame_view(struct tog_view *, const char *, struct got_object_id *,
101 struct got_repository *);
102 static const struct got_error *
103 show_tree_view(struct tog_view *, struct got_tree_object *,
104 struct got_object_id *, struct got_repository *);
107 close_view(struct tog_view *view)
110 del_panel(view->panel);
112 delwin(view->window);
116 static struct tog_view *
117 open_view(int nlines, int ncols, int begin_y, int begin_x,
118 struct tog_view *parent)
120 struct tog_view *view = malloc(sizeof(*view));
125 view->parent = parent;
128 view->nlines = nlines ? nlines : LINES - begin_y;
129 view->ncols = ncols ? ncols : COLS - begin_x;
130 view->begin_y = begin_y;
131 view->begin_x = begin_x;
132 view->window = newwin(nlines, ncols, begin_y, begin_x);
133 if (view->window == NULL) {
137 view->panel = new_panel(view->window);
138 if (view->panel == NULL) {
143 keypad(view->window, TRUE);
148 show_view(struct tog_view *view)
150 show_panel(view->panel);
154 const struct got_error *
155 view_resize(struct tog_view *view)
160 if (view->lines > LINES)
161 nlines = view->nlines - (view->lines - LINES);
163 nlines = view->nlines + (LINES - view->lines);
165 if (view->cols > COLS)
166 ncols = view->ncols - (view->cols - COLS);
168 ncols = view->ncols + (COLS - view->cols);
170 if (wresize(view->window, nlines, ncols) == ERR)
171 return got_error_from_errno();
172 replace_panel(view->panel, view->window);
174 view->nlines = nlines;
190 "usage: %s log [-c commit] [-r repository-path] [path]\n",
195 /* Create newly allocated wide-character string equivalent to a byte string. */
196 static const struct got_error *
197 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
200 const struct got_error *err = NULL;
203 *wlen = mbstowcs(NULL, s, 0);
204 if (*wlen == (size_t)-1) {
207 return got_error_from_errno();
209 /* byte string invalid in current encoding; try to "fix" it */
210 err = got_mbsavis(&vis, &vislen, s);
213 *wlen = mbstowcs(NULL, vis, 0);
214 if (*wlen == (size_t)-1) {
215 err = got_error_from_errno(); /* give up */
220 *ws = calloc(*wlen + 1, sizeof(*ws));
222 err = got_error_from_errno();
226 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
227 err = got_error_from_errno();
238 /* Format a line for display, ensuring that it won't overflow a width limit. */
239 static const struct got_error *
240 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
242 const struct got_error *err = NULL;
244 wchar_t *wline = NULL;
251 err = mbs2ws(&wline, &wlen, line);
256 while (i < wlen && cols < wlimit) {
257 int width = wcwidth(wline[i]);
264 if (cols + width <= wlimit) {
270 if (wline[i] == L'\t')
271 cols += TABSIZE - ((cols + 1) % TABSIZE);
275 err = got_error_from_errno();
290 static const struct got_error *
291 draw_commit(struct tog_view *view, struct got_commit_object *commit,
292 struct got_object_id *id)
294 const struct got_error *err = NULL;
295 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
296 char *logmsg0 = NULL, *logmsg = NULL;
297 char *author0 = NULL, *author = NULL;
298 wchar_t *wlogmsg = NULL, *wauthor = NULL;
299 int author_width, logmsg_width;
300 char *newline, *smallerthan;
303 static const size_t date_display_cols = 9;
304 static const size_t author_display_cols = 16;
305 const int avail = view->ncols;
307 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
308 &commit->tm_committer) >= sizeof(datebuf))
309 return got_error(GOT_ERR_NO_SPACE);
311 if (avail < date_display_cols)
312 limit = MIN(sizeof(datebuf) - 1, avail);
314 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
315 waddnstr(view->window, datebuf, limit);
320 author0 = strdup(commit->author);
321 if (author0 == NULL) {
322 err = got_error_from_errno();
326 smallerthan = strchr(author, '<');
330 char *at = strchr(author, '@');
335 err = format_line(&wauthor, &author_width, author, limit);
338 waddwstr(view->window, wauthor);
340 while (col <= avail && author_width < author_display_cols + 1) {
341 waddch(view->window, ' ');
348 logmsg0 = strdup(commit->logmsg);
349 if (logmsg0 == NULL) {
350 err = got_error_from_errno();
354 while (*logmsg == '\n')
356 newline = strchr(logmsg, '\n');
360 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
363 waddwstr(view->window, wlogmsg);
365 while (col <= avail) {
366 waddch(view->window, ' ');
378 struct commit_queue_entry {
379 TAILQ_ENTRY(commit_queue_entry) entry;
380 struct got_object_id *id;
381 struct got_commit_object *commit;
383 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
384 struct commit_queue {
386 struct commit_queue_head head;
389 static struct commit_queue_entry *
390 alloc_commit_queue_entry(struct got_commit_object *commit,
391 struct got_object_id *id)
393 struct commit_queue_entry *entry;
395 entry = calloc(1, sizeof(*entry));
400 entry->commit = commit;
405 pop_commit(struct commit_queue *commits)
407 struct commit_queue_entry *entry;
409 entry = TAILQ_FIRST(&commits->head);
410 TAILQ_REMOVE(&commits->head, entry, entry);
411 got_object_commit_close(entry->commit);
413 /* Don't free entry->id! It is owned by the commit graph. */
418 free_commits(struct commit_queue *commits)
420 while (!TAILQ_EMPTY(&commits->head))
424 static const struct got_error *
425 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
426 struct got_object_id *start_id, int minqueue, int init,
427 struct got_repository *repo, const char *path)
429 const struct got_error *err = NULL;
430 struct got_object_id *id;
431 struct commit_queue_entry *entry;
432 int nfetched, nqueued = 0, found_obj = 0;
433 int is_root_path = strcmp(path, "/") == 0;
435 err = got_commit_graph_iter_start(graph, start_id);
439 entry = TAILQ_LAST(&commits->head, commit_queue_head);
440 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
443 /* Start ID's commit is already on the queue; skip over it. */
444 err = got_commit_graph_iter_next(&id, graph);
445 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
448 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
454 struct got_commit_object *commit;
456 err = got_commit_graph_iter_next(&id, graph);
458 if (err->code != GOT_ERR_ITER_NEED_MORE)
460 if (nqueued >= minqueue) {
464 err = got_commit_graph_fetch_commits(&nfetched,
473 err = got_object_open_as_commit(&commit, repo, id);
478 struct got_object *obj;
479 struct got_object_qid *pid;
482 err = got_object_open_by_path(&obj, repo, id, path);
484 got_object_commit_close(commit);
485 if (err->code == GOT_ERR_NO_OBJ &&
486 (found_obj || !init)) {
487 /* History stops here. */
488 err = got_error(GOT_ERR_ITER_COMPLETED);
494 pid = SIMPLEQ_FIRST(&commit->parent_ids);
496 struct got_object *pobj;
497 err = got_object_open_by_path(&pobj, repo,
500 if (err->code != GOT_ERR_NO_OBJ) {
501 got_object_close(obj);
502 got_object_commit_close(commit);
508 struct got_object_id *id, *pid;
509 id = got_object_get_id(obj);
511 err = got_error_from_errno();
512 got_object_close(obj);
513 got_object_close(pobj);
516 pid = got_object_get_id(pobj);
518 err = got_error_from_errno();
520 got_object_close(obj);
521 got_object_close(pobj);
525 (got_object_id_cmp(id, pid) != 0);
526 got_object_close(pobj);
531 got_object_close(obj);
533 got_object_commit_close(commit);
538 entry = alloc_commit_queue_entry(commit, id);
540 err = got_error_from_errno();
543 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
551 static const struct got_error *
552 fetch_next_commit(struct commit_queue_entry **pentry,
553 struct commit_queue_entry *entry, struct commit_queue *commits,
554 struct got_commit_graph *graph, struct got_repository *repo,
557 const struct got_error *err = NULL;
561 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
565 /* Next entry to display should now be available. */
566 *pentry = TAILQ_NEXT(entry, entry);
568 return got_error(GOT_ERR_NO_OBJ);
573 static const struct got_error *
574 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
576 const struct got_error *err = NULL;
577 struct got_reference *head_ref;
581 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
585 err = got_ref_resolve(head_id, repo, head_ref);
586 got_ref_close(head_ref);
595 static const struct got_error *
596 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
597 struct commit_queue_entry **selected, struct commit_queue_entry *first,
598 struct commit_queue *commits, int selected_idx, int limit,
599 struct got_commit_graph *graph, struct got_repository *repo,
602 const struct got_error *err = NULL;
603 struct commit_queue_entry *entry;
605 char *id_str, *header;
611 if (ncommits == selected_idx) {
615 entry = TAILQ_NEXT(entry, entry);
619 err = got_object_id_str(&id_str, (*selected)->id);
623 if (path && strcmp(path, "/") != 0) {
624 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
625 err = got_error_from_errno();
629 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
630 err = got_error_from_errno();
635 err = format_line(&wline, &width, header, view->ncols);
642 werase(view->window);
644 waddwstr(view->window, wline);
645 if (width < view->ncols)
646 waddch(view->window, '\n');
655 if (ncommits >= limit - 1)
657 if (ncommits == selected_idx)
658 wstandout(view->window);
659 err = draw_commit(view, entry->commit, entry->id);
660 if (ncommits == selected_idx)
661 wstandend(view->window);
666 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
667 err = queue_commits(graph, commits, entry->id, 1,
670 if (err->code != GOT_ERR_ITER_COMPLETED)
675 entry = TAILQ_NEXT(entry, entry);
685 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
686 struct commit_queue *commits)
688 struct commit_queue_entry *entry;
691 entry = TAILQ_FIRST(&commits->head);
692 if (*first_displayed_entry == entry)
695 entry = *first_displayed_entry;
696 while (entry && nscrolled < maxscroll) {
697 entry = TAILQ_PREV(entry, commit_queue_head, entry);
699 *first_displayed_entry = entry;
705 static const struct got_error *
706 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
707 struct commit_queue_entry *last_displayed_entry,
708 struct commit_queue *commits, struct got_commit_graph *graph,
709 struct got_repository *repo, const char *path)
711 const struct got_error *err = NULL;
712 struct commit_queue_entry *pentry;
716 pentry = TAILQ_NEXT(last_displayed_entry, entry);
717 if (pentry == NULL) {
718 err = fetch_next_commit(&pentry, last_displayed_entry,
719 commits, graph, repo, path);
720 if (err || pentry == NULL)
723 last_displayed_entry = pentry;
725 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
728 *first_displayed_entry = pentry;
729 } while (++nscrolled < maxscroll);
734 static const struct got_error *
735 show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
736 struct got_repository *repo)
738 const struct got_error *err;
739 struct got_object *obj1 = NULL, *obj2 = NULL;
740 struct got_object_qid *parent_id;
741 struct tog_view *view;
743 err = got_object_open(&obj2, repo, entry->id);
747 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
749 err = got_object_open(&obj1, repo, parent_id->id);
754 view = open_view(0, 0, 0, 0, parent_view);
756 err = got_error_from_errno();
760 err = show_diff_view(view, obj1, obj2, repo);
762 show_view(parent_view);
765 got_object_close(obj1);
767 got_object_close(obj2);
771 static const struct got_error *
772 browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
773 struct got_repository *repo)
775 const struct got_error *err = NULL;
776 struct got_tree_object *tree;
777 struct tog_view *view;
779 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
783 view = open_view(0, 0, 0, 0, parent_view);
785 err = got_error_from_errno();
788 err = show_tree_view(view, tree, entry->id, repo);
790 show_view(parent_view);
792 got_object_tree_close(tree);
796 static const struct got_error *
797 show_log_view(struct tog_view *view, struct got_object_id *start_id,
798 struct got_repository *repo, const char *path)
800 const struct got_error *err = NULL;
801 struct got_object_id *head_id = NULL;
802 int ch, done = 0, selected = 0, nfetched;
803 struct got_commit_graph *graph = NULL;
804 struct commit_queue commits;
805 struct commit_queue_entry *first_displayed_entry = NULL;
806 struct commit_queue_entry *last_displayed_entry = NULL;
807 struct commit_queue_entry *selected_entry = NULL;
808 char *in_repo_path = NULL;
810 err = got_repo_map_path(&in_repo_path, repo, path);
814 err = get_head_commit_id(&head_id, repo);
818 /* The graph contains all commits. */
819 err = got_commit_graph_open(&graph, head_id, 0, repo);
822 /* The commit queue contains a subset of commits filtered by path. */
823 TAILQ_INIT(&commits.head);
824 commits.ncommits = 0;
826 /* Populate commit graph with a sufficient number of commits. */
827 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
833 * Open the initial batch of commits, sorted in commit graph order.
834 * We keep all commits open throughout the lifetime of the log view
835 * in order to avoid having to re-fetch commits from disk while
836 * updating the display.
838 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
841 if (err->code != GOT_ERR_ITER_COMPLETED)
848 first_displayed_entry = TAILQ_FIRST(&commits.head);
849 selected_entry = first_displayed_entry;
851 err = draw_commits(view, &last_displayed_entry, &selected_entry,
852 first_displayed_entry, &commits, selected, view->nlines,
853 graph, repo, in_repo_path);
857 nodelay(stdscr, FALSE);
858 ch = wgetch(view->window);
859 nodelay(stdscr, TRUE);
872 scroll_up(&first_displayed_entry, 1, &commits);
875 if (TAILQ_FIRST(&commits.head) ==
876 first_displayed_entry) {
880 scroll_up(&first_displayed_entry, view->nlines,
885 if (selected < MIN(view->nlines - 2,
886 commits.ncommits - 1)) {
890 err = scroll_down(&first_displayed_entry, 1,
891 last_displayed_entry, &commits, graph,
894 if (err->code != GOT_ERR_ITER_COMPLETED)
900 struct commit_queue_entry *first;
901 first = first_displayed_entry;
902 err = scroll_down(&first_displayed_entry,
903 view->nlines, last_displayed_entry,
904 &commits, graph, repo, in_repo_path);
906 if (err->code != GOT_ERR_ITER_COMPLETED)
908 if (first == first_displayed_entry &&
909 selected < MIN(view->nlines - 2,
910 commits.ncommits - 1)) {
911 /* can't scroll further down */
912 selected = MIN(view->nlines - 2,
913 commits.ncommits - 1);
920 err = view_resize(view);
923 if (selected > view->nlines - 2)
924 selected = view->nlines - 2;
925 if (selected > commits.ncommits - 1)
926 selected = commits.ncommits - 1;
930 err = show_commit(view, selected_entry, repo);
936 err = browse_commit(view, selected_entry, repo);
948 got_commit_graph_close(graph);
949 free_commits(&commits);
954 static const struct got_error *
955 cmd_log(int argc, char *argv[])
957 const struct got_error *error;
958 struct got_repository *repo = NULL;
959 struct got_object_id *start_id = NULL;
960 char *path = NULL, *repo_path = NULL, *cwd = NULL;
961 char *start_commit = NULL;
963 struct tog_view *view;
966 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
970 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
973 start_commit = optarg;
976 repo_path = realpath(optarg, NULL);
977 if (repo_path == NULL)
992 path = strdup(argv[0]);
996 return got_error_from_errno();
998 cwd = getcwd(NULL, 0);
1000 error = got_error_from_errno();
1003 if (repo_path == NULL) {
1004 repo_path = strdup(cwd);
1005 if (repo_path == NULL) {
1006 error = got_error_from_errno();
1011 error = got_repo_open(&repo, repo_path);
1015 if (start_commit == NULL) {
1016 error = get_head_commit_id(&start_id, repo);
1020 struct got_object *obj;
1021 error = got_object_open_by_id_str(&obj, repo, start_commit);
1022 if (error == NULL) {
1023 start_id = got_object_get_id(obj);
1024 if (start_id == NULL)
1025 error = got_error_from_errno();
1032 view = open_view(0, 0, 0, 0, NULL);
1034 error = got_error_from_errno();
1037 error = show_log_view(view, start_id, repo, path);
1045 got_repo_close(repo);
1053 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1059 parse_next_line(FILE *f, size_t *len)
1064 const char delim[3] = { '\0', '\0', '\0'};
1066 line = fparseln(f, &linelen, &lineno, delim, 0);
1072 static const struct got_error *
1073 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1074 int *last_displayed_line, int *eof, int max_lines)
1076 const struct got_error *err;
1077 int nlines = 0, nprinted = 0;
1084 werase(view->window);
1087 while (nprinted < max_lines) {
1088 line = parse_next_line(f, &len);
1093 if (++nlines < *first_displayed_line) {
1098 err = format_line(&wline, &width, line, view->ncols);
1104 waddwstr(view->window, wline);
1105 if (width < view->ncols)
1106 waddch(view->window, '\n');
1107 if (++nprinted == 1)
1108 *first_displayed_line = nlines;
1113 *last_displayed_line = nlines;
1121 static const struct got_error *
1122 show_diff_view(struct tog_view *view, struct got_object *obj1,
1123 struct got_object *obj2, struct got_repository *repo)
1125 const struct got_error *err;
1128 int first_displayed_line = 1, last_displayed_line = view->nlines;
1131 if (obj1 != NULL && obj2 != NULL &&
1132 got_object_get_type(obj1) != got_object_get_type(obj2))
1133 return got_error(GOT_ERR_OBJ_TYPE);
1137 return got_error_from_errno();
1139 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1140 case GOT_OBJ_TYPE_BLOB:
1141 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1143 case GOT_OBJ_TYPE_TREE:
1144 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1146 case GOT_OBJ_TYPE_COMMIT:
1147 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1150 return got_error(GOT_ERR_OBJ_TYPE);
1158 err = draw_file(view, f, &first_displayed_line,
1159 &last_displayed_line, &eof, view->nlines);
1162 nodelay(stdscr, FALSE);
1163 ch = wgetch(view->window);
1164 nodelay(stdscr, TRUE);
1171 if (first_displayed_line > 1)
1172 first_displayed_line--;
1177 while (i++ < view->nlines - 1 &&
1178 first_displayed_line > 1)
1179 first_displayed_line--;
1184 first_displayed_line++;
1189 while (!eof && i++ < view->nlines - 1) {
1190 char *line = parse_next_line(f, NULL);
1191 first_displayed_line++;
1197 err = view_resize(view);
1210 static const struct got_error *
1211 cmd_diff(int argc, char *argv[])
1213 const struct got_error *error = NULL;
1214 struct got_repository *repo = NULL;
1215 struct got_object *obj1 = NULL, *obj2 = NULL;
1216 char *repo_path = NULL;
1217 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1219 struct tog_view *view;
1222 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1226 while ((ch = getopt(argc, argv, "")) != -1) {
1238 usage_diff(); /* TODO show local worktree changes */
1239 } else if (argc == 2) {
1240 repo_path = getcwd(NULL, 0);
1241 if (repo_path == NULL)
1242 return got_error_from_errno();
1243 obj_id_str1 = argv[0];
1244 obj_id_str2 = argv[1];
1245 } else if (argc == 3) {
1246 repo_path = realpath(argv[0], NULL);
1247 if (repo_path == NULL)
1248 return got_error_from_errno();
1249 obj_id_str1 = argv[1];
1250 obj_id_str2 = argv[2];
1254 error = got_repo_open(&repo, repo_path);
1259 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1263 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1267 view = open_view(0, 0, 0, 0, NULL);
1269 error = got_error_from_errno();
1272 error = show_diff_view(view, obj1, obj2, repo);
1275 got_repo_close(repo);
1277 got_object_close(obj1);
1279 got_object_close(obj2);
1287 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1292 struct tog_blame_line {
1294 struct got_object_id *id;
1297 static const struct got_error *
1298 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1299 const char *path, struct tog_blame_line *lines, int nlines,
1300 int blame_complete, int selected_line, int *first_displayed_line,
1301 int *last_displayed_line, int *eof, int max_lines)
1303 const struct got_error *err;
1304 int lineno = 0, nprinted = 0;
1309 struct tog_blame_line *blame_line;
1310 struct got_object_id *prev_id = NULL;
1313 err = got_object_id_str(&id_str, id);
1318 werase(view->window);
1320 if (asprintf(&line, "commit: %s", id_str) == -1) {
1321 err = got_error_from_errno();
1326 err = format_line(&wline, &width, line, view->ncols);
1329 waddwstr(view->window, wline);
1332 if (width < view->ncols)
1333 waddch(view->window, '\n');
1335 if (asprintf(&line, "[%d/%d] %s%s",
1336 *first_displayed_line - 1 + selected_line, nlines,
1337 blame_complete ? "" : "annotating ", path) == -1) {
1339 return got_error_from_errno();
1342 err = format_line(&wline, &width, line, view->ncols);
1347 waddwstr(view->window, wline);
1350 if (width < view->ncols)
1351 waddch(view->window, '\n');
1354 while (nprinted < max_lines - 2) {
1355 line = parse_next_line(f, &len);
1360 if (++lineno < *first_displayed_line) {
1365 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1366 err = format_line(&wline, &width, line, wlimit);
1372 if (nprinted == selected_line - 1)
1373 wstandout(view->window);
1375 blame_line = &lines[lineno - 1];
1376 if (blame_line->annotated && prev_id &&
1377 got_object_id_cmp(prev_id, blame_line->id) == 0)
1378 waddstr(view->window, " ");
1379 else if (blame_line->annotated) {
1381 err = got_object_id_str(&id_str, blame_line->id);
1387 wprintw(view->window, "%.8s ", id_str);
1389 prev_id = blame_line->id;
1391 waddstr(view->window, "........ ");
1395 waddwstr(view->window, wline);
1396 while (width < wlimit) {
1397 waddch(view->window, ' ');
1400 if (nprinted == selected_line - 1)
1401 wstandend(view->window);
1402 if (++nprinted == 1)
1403 *first_displayed_line = lineno;
1408 *last_displayed_line = lineno;
1416 struct tog_blame_cb_args {
1417 pthread_mutex_t *mutex;
1418 struct tog_blame_line *lines; /* one per line */
1421 struct tog_view *view;
1422 struct got_object_id *commit_id;
1425 int *first_displayed_line;
1426 int *last_displayed_line;
1431 static const struct got_error *
1432 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1434 const struct got_error *err = NULL;
1435 struct tog_blame_cb_args *a = arg;
1436 struct tog_blame_line *line;
1439 if (nlines != a->nlines ||
1440 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1441 return got_error(GOT_ERR_RANGE);
1443 if (pthread_mutex_lock(a->mutex) != 0)
1444 return got_error_from_errno();
1446 if (*a->quit) { /* user has quit the blame view */
1447 err = got_error(GOT_ERR_ITER_COMPLETED);
1452 goto done; /* no change in this commit */
1454 line = &a->lines[lineno - 1];
1455 if (line->annotated)
1458 line->id = got_object_id_dup(id);
1459 if (line->id == NULL) {
1460 err = got_error_from_errno();
1463 line->annotated = 1;
1465 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1466 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1467 a->last_displayed_line, &eof, a->view->nlines);
1469 if (pthread_mutex_unlock(a->mutex) != 0)
1470 return got_error_from_errno();
1474 struct tog_blame_thread_args {
1476 struct got_repository *repo;
1477 struct tog_blame_cb_args *cb_args;
1482 blame_thread(void *arg)
1484 const struct got_error *err;
1485 struct tog_blame_thread_args *ta = arg;
1486 struct tog_blame_cb_args *a = ta->cb_args;
1489 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1490 blame_cb, ta->cb_args);
1492 if (pthread_mutex_lock(a->mutex) != 0)
1493 return (void *)got_error_from_errno();
1495 got_repo_close(ta->repo);
1499 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1500 a->lines, a->nlines, 1, *a->selected_line,
1501 a->first_displayed_line, a->last_displayed_line, &eof,
1504 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1505 err = got_error_from_errno();
1510 static struct got_object_id *
1511 get_selected_commit_id(struct tog_blame_line *lines,
1512 int first_displayed_line, int selected_line)
1514 struct tog_blame_line *line;
1516 line = &lines[first_displayed_line - 1 + selected_line - 1];
1517 if (!line->annotated)
1523 static const struct got_error *
1524 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1525 struct tog_blame_line *lines, int first_displayed_line,
1526 int selected_line, struct got_repository *repo)
1528 const struct got_error *err = NULL;
1529 struct got_commit_object *commit = NULL;
1530 struct got_object_id *selected_id;
1531 struct got_object_qid *pid;
1536 selected_id = get_selected_commit_id(lines,
1537 first_displayed_line, selected_line);
1538 if (selected_id == NULL)
1541 err = got_object_open(obj, repo, selected_id);
1545 err = got_object_commit_open(&commit, repo, *obj);
1549 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1551 err = got_object_open(pobj, repo, pid->id);
1557 got_object_commit_close(commit);
1564 struct tog_blame_line *lines;
1567 struct tog_blame_thread_args thread_args;
1568 struct tog_blame_cb_args cb_args;
1572 static const struct got_error *
1573 stop_blame(struct tog_blame *blame)
1575 const struct got_error *err = NULL;
1578 if (blame->thread) {
1579 if (pthread_join(blame->thread, (void **)&err) != 0)
1580 err = got_error_from_errno();
1581 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1583 blame->thread = NULL;
1585 if (blame->thread_args.repo) {
1586 got_repo_close(blame->thread_args.repo);
1587 blame->thread_args.repo = NULL;
1593 for (i = 0; i < blame->nlines; i++)
1594 free(blame->lines[i].id);
1596 blame->lines = NULL;
1597 free(blame->cb_args.commit_id);
1598 blame->cb_args.commit_id = NULL;
1603 static const struct got_error *
1604 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1605 struct tog_view *view, int *blame_complete,
1606 int *first_displayed_line, int *last_displayed_line,
1607 int *selected_line, int *done, const char *path,
1608 struct got_object_id *commit_id,
1609 struct got_repository *repo)
1611 const struct got_error *err = NULL;
1612 struct got_blob_object *blob = NULL;
1613 struct got_repository *thread_repo = NULL;
1614 struct got_object *obj;
1616 err = got_object_open_by_path(&obj, repo, commit_id, path);
1619 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1620 err = got_error(GOT_ERR_OBJ_TYPE);
1624 err = got_object_blob_open(&blob, repo, obj, 8192);
1627 blame->f = got_opentemp();
1628 if (blame->f == NULL) {
1629 err = got_error_from_errno();
1632 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1637 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1638 if (blame->lines == NULL) {
1639 err = got_error_from_errno();
1643 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1647 blame->cb_args.view = view;
1648 blame->cb_args.lines = blame->lines;
1649 blame->cb_args.nlines = blame->nlines;
1650 blame->cb_args.mutex = mutex;
1651 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1652 if (blame->cb_args.commit_id == NULL) {
1653 err = got_error_from_errno();
1656 blame->cb_args.f = blame->f;
1657 blame->cb_args.path = path;
1658 blame->cb_args.first_displayed_line = first_displayed_line;
1659 blame->cb_args.selected_line = selected_line;
1660 blame->cb_args.last_displayed_line = last_displayed_line;
1661 blame->cb_args.quit = done;
1663 blame->thread_args.path = path;
1664 blame->thread_args.repo = thread_repo;
1665 blame->thread_args.cb_args = &blame->cb_args;
1666 blame->thread_args.complete = blame_complete;
1667 *blame_complete = 0;
1669 if (pthread_create(&blame->thread, NULL, blame_thread,
1670 &blame->thread_args) != 0) {
1671 err = got_error_from_errno();
1677 got_object_blob_close(blob);
1679 got_object_close(obj);
1685 static const struct got_error *
1686 show_blame_view(struct tog_view *view, const char *path,
1687 struct got_object_id *commit_id, struct got_repository *repo)
1689 const struct got_error *err = NULL, *thread_err = NULL;
1690 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1691 int selected_line = first_displayed_line;
1692 int eof, blame_complete = 0;
1693 struct got_object *obj = NULL, *pobj = NULL;
1694 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1695 struct tog_blame blame;
1696 struct got_object_id_queue blamed_commits;
1697 struct got_object_qid *blamed_commit = NULL;
1698 struct tog_view *diff_view;
1700 SIMPLEQ_INIT(&blamed_commits);
1702 if (pthread_mutex_init(&mutex, NULL) != 0) {
1703 err = got_error_from_errno();
1707 err = got_object_qid_alloc(&blamed_commit, commit_id);
1710 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1713 last_displayed_line = view->nlines;
1715 memset(&blame, 0, sizeof(blame));
1716 err = run_blame(&blame, &mutex, view, &blame_complete,
1717 &first_displayed_line, &last_displayed_line,
1718 &selected_line, &done, path, blamed_commit->id, repo);
1723 if (pthread_mutex_lock(&mutex) != 0) {
1724 err = got_error_from_errno();
1727 err = draw_blame(view, blamed_commit->id, blame.f, path,
1728 blame.lines, blame.nlines, blame_complete, selected_line,
1729 &first_displayed_line, &last_displayed_line, &eof,
1731 if (pthread_mutex_unlock(&mutex) != 0) {
1732 err = got_error_from_errno();
1737 nodelay(stdscr, FALSE);
1738 ch = wgetch(view->window);
1739 nodelay(stdscr, TRUE);
1740 if (pthread_mutex_lock(&mutex) != 0) {
1741 err = got_error_from_errno();
1750 if (selected_line > 1)
1752 else if (selected_line == 1 &&
1753 first_displayed_line > 1)
1754 first_displayed_line--;
1758 if (first_displayed_line == 1) {
1762 if (first_displayed_line > view->nlines - 2)
1763 first_displayed_line -=
1766 first_displayed_line = 1;
1770 if (selected_line < view->nlines - 2 &&
1771 first_displayed_line + selected_line <=
1774 else if (last_displayed_line < blame.nlines)
1775 first_displayed_line++;
1779 struct got_object_id *id;
1780 id = get_selected_commit_id(blame.lines,
1781 first_displayed_line, selected_line);
1782 if (id == NULL || got_object_id_cmp(id,
1783 blamed_commit->id) == 0)
1785 err = open_selected_commit(&pobj, &obj,
1786 blame.lines, first_displayed_line,
1787 selected_line, repo);
1790 if (pobj == NULL && obj == NULL)
1792 if (ch == 'p' && pobj == NULL)
1795 if (pthread_mutex_unlock(&mutex) != 0) {
1796 err = got_error_from_errno();
1799 thread_err = stop_blame(&blame);
1801 if (pthread_mutex_lock(&mutex) != 0) {
1802 err = got_error_from_errno();
1807 id = got_object_get_id(ch == 'b' ? obj : pobj);
1808 got_object_close(obj);
1811 got_object_close(pobj);
1815 err = got_error_from_errno();
1818 err = got_object_qid_alloc(&blamed_commit, id);
1822 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1823 blamed_commit, entry);
1824 err = run_blame(&blame, &mutex, view,
1825 &blame_complete, &first_displayed_line,
1826 &last_displayed_line, &selected_line,
1827 &done, path, blamed_commit->id, repo);
1833 struct got_object_qid *first;
1834 first = SIMPLEQ_FIRST(&blamed_commits);
1835 if (!got_object_id_cmp(first->id, commit_id))
1838 if (pthread_mutex_unlock(&mutex) != 0) {
1839 err = got_error_from_errno();
1842 thread_err = stop_blame(&blame);
1844 if (pthread_mutex_lock(&mutex) != 0) {
1845 err = got_error_from_errno();
1850 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1851 got_object_qid_free(blamed_commit);
1852 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1853 err = run_blame(&blame, &mutex, view,
1854 &blame_complete, &first_displayed_line,
1855 &last_displayed_line, &selected_line,
1856 &done, path, blamed_commit->id, repo);
1863 err = open_selected_commit(&pobj, &obj,
1864 blame.lines, first_displayed_line,
1865 selected_line, repo);
1868 if (pobj == NULL && obj == NULL)
1870 diff_view = open_view(0, 0, 0, 0, view);
1871 if (diff_view == NULL) {
1872 err = got_error_from_errno();
1875 err = show_diff_view(diff_view, pobj, obj, repo);
1876 close_view(diff_view);
1879 got_object_close(pobj);
1882 got_object_close(obj);
1889 if (last_displayed_line >= blame.nlines &&
1890 selected_line < view->nlines - 2) {
1891 selected_line = MIN(blame.nlines,
1895 if (last_displayed_line + view->nlines - 2 <=
1897 first_displayed_line +=
1900 first_displayed_line =
1901 blame.nlines - (view->nlines - 3);
1904 err = view_resize(view);
1907 if (selected_line > view->nlines - 2) {
1908 selected_line = MIN(blame.nlines,
1915 if (pthread_mutex_unlock(&mutex) != 0)
1916 err = got_error_from_errno();
1917 if (err || thread_err)
1922 got_object_close(pobj);
1924 thread_err = stop_blame(&blame);
1925 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1926 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1927 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1928 got_object_qid_free(blamed_commit);
1930 return thread_err ? thread_err : err;
1933 static const struct got_error *
1934 cmd_blame(int argc, char *argv[])
1936 const struct got_error *error;
1937 struct got_repository *repo = NULL;
1938 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1939 struct got_object_id *commit_id = NULL;
1940 char *commit_id_str = NULL;
1942 struct tog_view *view;
1945 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1949 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1952 commit_id_str = optarg;
1955 repo_path = realpath(optarg, NULL);
1956 if (repo_path == NULL)
1957 err(1, "-r option");
1973 cwd = getcwd(NULL, 0);
1975 error = got_error_from_errno();
1978 if (repo_path == NULL) {
1979 repo_path = strdup(cwd);
1980 if (repo_path == NULL) {
1981 error = got_error_from_errno();
1987 error = got_repo_open(&repo, repo_path);
1991 error = got_repo_map_path(&in_repo_path, repo, path);
1995 if (commit_id_str == NULL) {
1996 struct got_reference *head_ref;
1997 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2000 error = got_ref_resolve(&commit_id, repo, head_ref);
2001 got_ref_close(head_ref);
2003 struct got_object *obj;
2004 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2007 commit_id = got_object_get_id(obj);
2008 if (commit_id == NULL)
2009 error = got_error_from_errno();
2010 got_object_close(obj);
2015 view = open_view(0, 0, 0, 0, NULL);
2017 error = got_error_from_errno();
2020 error = show_blame_view(view, in_repo_path, commit_id, repo);
2028 got_repo_close(repo);
2032 static const struct got_error *
2033 draw_tree_entries(struct tog_view *view,
2034 struct got_tree_entry **first_displayed_entry,
2035 struct got_tree_entry **last_displayed_entry,
2036 struct got_tree_entry **selected_entry, int *ndisplayed,
2037 const char *label, int show_ids, const char *parent_path,
2038 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2040 const struct got_error *err = NULL;
2041 struct got_tree_entry *te;
2047 werase(view->window);
2052 err = format_line(&wline, &width, label, view->ncols);
2055 waddwstr(view->window, wline);
2058 if (width < view->ncols)
2059 waddch(view->window, '\n');
2062 err = format_line(&wline, &width, parent_path, view->ncols);
2065 waddwstr(view->window, wline);
2068 if (width < view->ncols)
2069 waddch(view->window, '\n');
2072 waddch(view->window, '\n');
2076 te = SIMPLEQ_FIRST(&entries->head);
2077 if (*first_displayed_entry == NULL) {
2078 if (selected == 0) {
2079 wstandout(view->window);
2080 *selected_entry = NULL;
2082 waddstr(view->window, " ..\n"); /* parent directory */
2084 wstandend(view->window);
2091 while (te != *first_displayed_entry)
2092 te = SIMPLEQ_NEXT(te, entry);
2096 char *line = NULL, *id_str = NULL;
2099 err = got_object_id_str(&id_str, te->id);
2101 return got_error_from_errno();
2103 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2104 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2106 return got_error_from_errno();
2109 err = format_line(&wline, &width, line, view->ncols);
2114 if (n == selected) {
2115 wstandout(view->window);
2116 *selected_entry = te;
2118 waddwstr(view->window, wline);
2119 if (width < view->ncols)
2120 waddch(view->window, '\n');
2122 wstandend(view->window);
2128 *last_displayed_entry = te;
2131 te = SIMPLEQ_NEXT(te, entry);
2138 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2139 const struct got_tree_entries *entries, int isroot)
2141 struct got_tree_entry *te, *prev;
2144 if (*first_displayed_entry == NULL)
2147 te = SIMPLEQ_FIRST(&entries->head);
2148 if (*first_displayed_entry == te) {
2150 *first_displayed_entry = NULL;
2154 /* XXX this is stupid... switch to TAILQ? */
2155 for (i = 0; i < maxscroll; i++) {
2156 while (te != *first_displayed_entry) {
2158 te = SIMPLEQ_NEXT(te, entry);
2160 *first_displayed_entry = prev;
2161 te = SIMPLEQ_FIRST(&entries->head);
2163 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2164 *first_displayed_entry = NULL;
2168 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2169 struct got_tree_entry *last_displayed_entry,
2170 const struct got_tree_entries *entries)
2172 struct got_tree_entry *next;
2175 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2178 if (*first_displayed_entry)
2179 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2181 next = SIMPLEQ_FIRST(&entries->head);
2183 *first_displayed_entry = next;
2184 if (++n >= maxscroll)
2186 next = SIMPLEQ_NEXT(next, entry);
2190 struct tog_parent_tree {
2191 TAILQ_ENTRY(tog_parent_tree) entry;
2192 struct got_tree_object *tree;
2193 struct got_tree_entry *first_displayed_entry;
2194 struct got_tree_entry *selected_entry;
2198 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2200 static const struct got_error *
2201 tree_entry_path(char **path, struct tog_parent_trees *parents,
2202 struct got_tree_entry *te)
2204 const struct got_error *err = NULL;
2205 struct tog_parent_tree *pt;
2206 size_t len = 2; /* for leading slash and NUL */
2208 TAILQ_FOREACH(pt, parents, entry)
2209 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2211 len += strlen(te->name);
2213 *path = calloc(1, len);
2215 return got_error_from_errno();
2218 pt = TAILQ_LAST(parents, tog_parent_trees);
2220 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2221 err = got_error(GOT_ERR_NO_SPACE);
2224 if (strlcat(*path, "/", len) >= len) {
2225 err = got_error(GOT_ERR_NO_SPACE);
2228 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2231 if (strlcat(*path, te->name, len) >= len) {
2232 err = got_error(GOT_ERR_NO_SPACE);
2244 static const struct got_error *
2245 blame_tree_entry(struct tog_view *parent_view, struct got_tree_entry *te,
2246 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2247 struct got_repository *repo)
2249 const struct got_error *err = NULL;
2251 struct tog_view *view;
2253 err = tree_entry_path(&path, parents, te);
2257 view = open_view(0, 0, 0, 0, parent_view);
2259 err = show_blame_view(view, path, commit_id, repo);
2262 err = got_error_from_errno();
2264 show_view(parent_view);
2269 static const struct got_error *
2270 log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2271 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2272 struct got_repository *repo)
2274 const struct got_error *err = NULL;
2277 err = tree_entry_path(&path, parents, te);
2281 err = show_log_view(view, commit_id, repo, path);
2286 static const struct got_error *
2287 show_tree_view(struct tog_view *view, struct got_tree_object *root,
2288 struct got_object_id *commit_id, struct got_repository *repo)
2290 const struct got_error *err = NULL;
2291 int ch, done = 0, selected = 0, show_ids = 0;
2292 struct got_tree_object *tree = root;
2293 const struct got_tree_entries *entries;
2294 struct got_tree_entry *first_displayed_entry = NULL;
2295 struct got_tree_entry *last_displayed_entry = NULL;
2296 struct got_tree_entry *selected_entry = NULL;
2297 char *commit_id_str = NULL, *tree_label = NULL;
2298 int nentries, ndisplayed;
2299 struct tog_parent_trees parents;
2301 TAILQ_INIT(&parents);
2303 err = got_object_id_str(&commit_id_str, commit_id);
2307 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2308 err = got_error_from_errno();
2314 entries = got_object_tree_get_entries(root);
2315 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2318 entries = got_object_tree_get_entries(tree);
2319 nentries = entries->nentries;
2321 nentries++; /* '..' directory */
2323 err = tree_entry_path(&parent_path, &parents, NULL);
2327 err = draw_tree_entries(view, &first_displayed_entry,
2328 &last_displayed_entry, &selected_entry, &ndisplayed,
2329 tree_label, show_ids, parent_path, entries, selected,
2330 view->nlines, tree == root);
2335 nodelay(stdscr, FALSE);
2336 ch = wgetch(view->window);
2337 nodelay(stdscr, TRUE);
2343 show_ids = !show_ids;
2346 if (selected_entry) {
2347 struct tog_view *log_view;
2348 log_view = open_view(0, 0, 0, 0, view);
2349 if (log_view == NULL) {
2350 err = got_error_from_errno();
2353 err = log_tree_entry(log_view,
2354 selected_entry, &parents,
2356 close_view(log_view);
2368 tree_scroll_up(&first_displayed_entry, 1,
2369 entries, tree == root);
2372 if (SIMPLEQ_FIRST(&entries->head) ==
2373 first_displayed_entry) {
2375 first_displayed_entry = NULL;
2379 tree_scroll_up(&first_displayed_entry,
2380 view->nlines, entries, tree == root);
2384 if (selected < ndisplayed - 1) {
2388 tree_scroll_down(&first_displayed_entry, 1,
2389 last_displayed_entry, entries);
2392 tree_scroll_down(&first_displayed_entry,
2393 view->nlines, last_displayed_entry,
2395 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2397 /* can't scroll any further; move cursor down */
2398 if (selected < ndisplayed - 1)
2399 selected = ndisplayed - 1;
2403 if (selected_entry == NULL) {
2404 struct tog_parent_tree *parent;
2406 /* user selected '..' */
2409 parent = TAILQ_FIRST(&parents);
2410 TAILQ_REMOVE(&parents, parent, entry);
2411 got_object_tree_close(tree);
2412 tree = parent->tree;
2413 first_displayed_entry =
2414 parent->first_displayed_entry;
2415 selected_entry = parent->selected_entry;
2416 selected = parent->selected;
2418 } else if (S_ISDIR(selected_entry->mode)) {
2419 struct tog_parent_tree *parent;
2420 struct got_tree_object *child;
2421 err = got_object_open_as_tree(
2422 &child, repo, selected_entry->id);
2425 parent = calloc(1, sizeof(*parent));
2426 if (parent == NULL) {
2427 err = got_error_from_errno();
2430 parent->tree = tree;
2431 parent->first_displayed_entry =
2432 first_displayed_entry;
2433 parent->selected_entry = selected_entry;
2434 parent->selected = selected;
2435 TAILQ_INSERT_HEAD(&parents, parent,
2439 first_displayed_entry = NULL;
2440 } else if (S_ISREG(selected_entry->mode)) {
2441 err = blame_tree_entry(view,
2442 selected_entry, &parents,
2449 err = view_resize(view);
2452 if (selected > view->nlines)
2453 selected = ndisplayed - 1;
2461 free(commit_id_str);
2462 while (!TAILQ_EMPTY(&parents)) {
2463 struct tog_parent_tree *parent;
2464 parent = TAILQ_FIRST(&parents);
2465 TAILQ_REMOVE(&parents, parent, entry);
2470 got_object_tree_close(tree);
2478 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2483 static const struct got_error *
2484 cmd_tree(int argc, char *argv[])
2486 const struct got_error *error;
2487 struct got_repository *repo = NULL;
2488 char *repo_path = NULL;
2489 struct got_object_id *commit_id = NULL;
2490 char *commit_id_arg = NULL;
2491 struct got_commit_object *commit = NULL;
2492 struct got_tree_object *tree = NULL;
2494 struct tog_view *view;
2497 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2501 while ((ch = getopt(argc, argv, "c:")) != -1) {
2504 commit_id_arg = optarg;
2516 repo_path = getcwd(NULL, 0);
2517 if (repo_path == NULL)
2518 return got_error_from_errno();
2519 } else if (argc == 1) {
2520 repo_path = realpath(argv[0], NULL);
2521 if (repo_path == NULL)
2522 return got_error_from_errno();
2526 error = got_repo_open(&repo, repo_path);
2531 if (commit_id_arg == NULL) {
2532 error = get_head_commit_id(&commit_id, repo);
2536 struct got_object *obj;
2537 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2538 if (error == NULL) {
2539 commit_id = got_object_get_id(obj);
2540 if (commit_id == NULL)
2541 error = got_error_from_errno();
2547 error = got_object_open_as_commit(&commit, repo, commit_id);
2551 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2555 view = open_view(0, 0, 0, 0, NULL);
2557 error = got_error_from_errno();
2560 error = show_tree_view(view, tree, commit_id, repo);
2565 got_object_commit_close(commit);
2567 got_object_tree_close(tree);
2569 got_repo_close(repo);
2579 intrflush(stdscr, FALSE);
2580 keypad(stdscr, TRUE);
2589 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2590 "Available commands:\n", getprogname());
2591 for (i = 0; i < nitems(tog_commands); i++) {
2592 struct tog_cmd *cmd = &tog_commands[i];
2593 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2599 make_argv(const char *arg0, const char *arg1)
2602 int argc = (arg1 == NULL ? 1 : 2);
2604 argv = calloc(argc, sizeof(char *));
2607 argv[0] = strdup(arg0);
2608 if (argv[0] == NULL)
2611 argv[1] = strdup(arg1);
2612 if (argv[1] == NULL)
2620 main(int argc, char *argv[])
2622 const struct got_error *error = NULL;
2623 struct tog_cmd *cmd = NULL;
2625 char **cmd_argv = NULL;
2627 setlocale(LC_ALL, "");
2629 while ((ch = getopt(argc, argv, "h")) != -1) {
2648 /* Build an argument vector which runs a default command. */
2649 cmd = &tog_commands[0];
2650 cmd_argv = make_argv(cmd->name, NULL);
2655 /* Did the user specific a command? */
2656 for (i = 0; i < nitems(tog_commands); i++) {
2657 if (strncmp(tog_commands[i].name, argv[0],
2658 strlen(argv[0])) == 0) {
2659 cmd = &tog_commands[i];
2661 tog_commands[i].cmd_usage();
2666 /* Did the user specify a repository? */
2667 char *repo_path = realpath(argv[0], NULL);
2669 struct got_repository *repo;
2670 error = got_repo_open(&repo, repo_path);
2672 got_repo_close(repo);
2674 error = got_error_from_errno();
2677 fprintf(stderr, "%s: '%s' is not a "
2678 "known command\n", getprogname(),
2682 fprintf(stderr, "%s: '%s' is neither a known "
2683 "command nor a path to a repository\n",
2684 getprogname(), argv[0]);
2688 cmd = &tog_commands[0];
2689 cmd_argv = make_argv(cmd->name, repo_path);
2697 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2704 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);