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);
147 const struct got_error *
148 view_resize(struct tog_view *view)
153 if (view->lines > LINES)
154 nlines = view->nlines - (view->lines - LINES);
156 nlines = view->nlines + (LINES - view->lines);
158 if (view->cols > COLS)
159 ncols = view->ncols - (view->cols - COLS);
161 ncols = view->ncols + (COLS - view->cols);
163 if (wresize(view->window, nlines, ncols) == ERR)
164 return got_error_from_errno();
166 view->nlines = nlines;
181 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
186 /* Create newly allocated wide-character string equivalent to a byte string. */
187 static const struct got_error *
188 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
191 const struct got_error *err = NULL;
194 *wlen = mbstowcs(NULL, s, 0);
195 if (*wlen == (size_t)-1) {
198 return got_error_from_errno();
200 /* byte string invalid in current encoding; try to "fix" it */
201 err = got_mbsavis(&vis, &vislen, s);
204 *wlen = mbstowcs(NULL, vis, 0);
205 if (*wlen == (size_t)-1) {
206 err = got_error_from_errno(); /* give up */
211 *ws = calloc(*wlen + 1, sizeof(*ws));
213 err = got_error_from_errno();
217 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
218 err = got_error_from_errno();
229 /* Format a line for display, ensuring that it won't overflow a width limit. */
230 static const struct got_error *
231 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
233 const struct got_error *err = NULL;
235 wchar_t *wline = NULL;
242 err = mbs2ws(&wline, &wlen, line);
247 while (i < wlen && cols < wlimit) {
248 int width = wcwidth(wline[i]);
255 if (cols + width <= wlimit) {
261 if (wline[i] == L'\t')
262 cols += TABSIZE - ((cols + 1) % TABSIZE);
266 err = got_error_from_errno();
281 static const struct got_error *
282 draw_commit(struct tog_view *view, struct got_commit_object *commit,
283 struct got_object_id *id)
285 const struct got_error *err = NULL;
286 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
287 char *logmsg0 = NULL, *logmsg = NULL;
288 char *author0 = NULL, *author = NULL;
289 wchar_t *wlogmsg = NULL, *wauthor = NULL;
290 int author_width, logmsg_width;
291 char *newline, *smallerthan;
294 static const size_t date_display_cols = 9;
295 static const size_t author_display_cols = 16;
296 const int avail = view->ncols;
298 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
300 return got_error(GOT_ERR_NO_SPACE);
302 if (avail < date_display_cols)
303 limit = MIN(sizeof(datebuf) - 1, avail);
305 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
306 waddnstr(view->window, datebuf, limit);
311 author0 = strdup(commit->author);
312 if (author0 == NULL) {
313 err = got_error_from_errno();
317 smallerthan = strchr(author, '<');
321 char *at = strchr(author, '@');
326 err = format_line(&wauthor, &author_width, author, limit);
329 waddwstr(view->window, wauthor);
331 while (col <= avail && author_width < author_display_cols + 1) {
332 waddch(view->window, ' ');
339 logmsg0 = strdup(commit->logmsg);
340 if (logmsg0 == NULL) {
341 err = got_error_from_errno();
345 while (*logmsg == '\n')
347 newline = strchr(logmsg, '\n');
351 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
354 waddwstr(view->window, wlogmsg);
356 while (col <= avail) {
357 waddch(view->window, ' ');
369 struct commit_queue_entry {
370 TAILQ_ENTRY(commit_queue_entry) entry;
371 struct got_object_id *id;
372 struct got_commit_object *commit;
374 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
375 struct commit_queue {
377 struct commit_queue_head head;
380 static struct commit_queue_entry *
381 alloc_commit_queue_entry(struct got_commit_object *commit,
382 struct got_object_id *id)
384 struct commit_queue_entry *entry;
386 entry = calloc(1, sizeof(*entry));
391 entry->commit = commit;
396 pop_commit(struct commit_queue *commits)
398 struct commit_queue_entry *entry;
400 entry = TAILQ_FIRST(&commits->head);
401 TAILQ_REMOVE(&commits->head, entry, entry);
402 got_object_commit_close(entry->commit);
404 /* Don't free entry->id! It is owned by the commit graph. */
409 free_commits(struct commit_queue *commits)
411 while (!TAILQ_EMPTY(&commits->head))
415 static const struct got_error *
416 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
417 struct got_object_id *start_id, int minqueue, int init,
418 struct got_repository *repo, const char *path)
420 const struct got_error *err = NULL;
421 struct got_object_id *id;
422 struct commit_queue_entry *entry;
423 int nfetched, nqueued = 0, found_obj = 0;
424 int is_root_path = strcmp(path, "/") == 0;
426 err = got_commit_graph_iter_start(graph, start_id);
430 entry = TAILQ_LAST(&commits->head, commit_queue_head);
431 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
434 /* Start ID's commit is already on the queue; skip over it. */
435 err = got_commit_graph_iter_next(&id, graph);
436 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
439 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
445 struct got_commit_object *commit;
447 err = got_commit_graph_iter_next(&id, graph);
449 if (err->code != GOT_ERR_ITER_NEED_MORE)
451 if (nqueued >= minqueue) {
455 err = got_commit_graph_fetch_commits(&nfetched,
464 err = got_object_open_as_commit(&commit, repo, id);
469 struct got_object *obj;
470 struct got_object_qid *pid;
473 err = got_object_open_by_path(&obj, repo, id, path);
475 got_object_commit_close(commit);
476 if (err->code == GOT_ERR_NO_OBJ &&
477 (found_obj || !init)) {
478 /* History stops here. */
479 err = got_error(GOT_ERR_ITER_COMPLETED);
485 pid = SIMPLEQ_FIRST(&commit->parent_ids);
487 struct got_object *pobj;
488 err = got_object_open_by_path(&pobj, repo,
491 if (err->code != GOT_ERR_NO_OBJ) {
492 got_object_close(obj);
493 got_object_commit_close(commit);
499 struct got_object_id *id, *pid;
500 id = got_object_get_id(obj);
502 err = got_error_from_errno();
503 got_object_close(obj);
504 got_object_close(pobj);
507 pid = got_object_get_id(pobj);
509 err = got_error_from_errno();
511 got_object_close(obj);
512 got_object_close(pobj);
516 (got_object_id_cmp(id, pid) != 0);
517 got_object_close(pobj);
522 got_object_close(obj);
524 got_object_commit_close(commit);
529 entry = alloc_commit_queue_entry(commit, id);
531 err = got_error_from_errno();
534 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
542 static const struct got_error *
543 fetch_next_commit(struct commit_queue_entry **pentry,
544 struct commit_queue_entry *entry, struct commit_queue *commits,
545 struct got_commit_graph *graph, struct got_repository *repo,
548 const struct got_error *err = NULL;
552 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
556 /* Next entry to display should now be available. */
557 *pentry = TAILQ_NEXT(entry, entry);
559 return got_error(GOT_ERR_NO_OBJ);
564 static const struct got_error *
565 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
567 const struct got_error *err = NULL;
568 struct got_reference *head_ref;
572 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
576 err = got_ref_resolve(head_id, repo, head_ref);
577 got_ref_close(head_ref);
586 static const struct got_error *
587 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
588 struct commit_queue_entry **selected, struct commit_queue_entry *first,
589 struct commit_queue *commits, int selected_idx, int limit,
590 struct got_commit_graph *graph, struct got_repository *repo,
593 const struct got_error *err = NULL;
594 struct commit_queue_entry *entry;
596 char *id_str, *header;
602 if (ncommits == selected_idx) {
606 entry = TAILQ_NEXT(entry, entry);
610 err = got_object_id_str(&id_str, (*selected)->id);
615 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
616 err = got_error_from_errno();
620 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
621 err = got_error_from_errno();
626 err = format_line(&wline, &width, header, view->ncols);
633 werase(view->window);
635 waddwstr(view->window, wline);
636 if (width < view->ncols)
637 waddch(view->window, '\n');
646 if (ncommits >= limit - 1)
648 if (ncommits == selected_idx)
649 wstandout(view->window);
650 err = draw_commit(view, entry->commit, entry->id);
651 if (ncommits == selected_idx)
652 wstandend(view->window);
657 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
658 err = queue_commits(graph, commits, entry->id, 1,
661 if (err->code != GOT_ERR_ITER_COMPLETED)
666 entry = TAILQ_NEXT(entry, entry);
676 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
677 struct commit_queue *commits)
679 struct commit_queue_entry *entry;
682 entry = TAILQ_FIRST(&commits->head);
683 if (*first_displayed_entry == entry)
686 entry = *first_displayed_entry;
687 while (entry && nscrolled < maxscroll) {
688 entry = TAILQ_PREV(entry, commit_queue_head, entry);
690 *first_displayed_entry = entry;
696 static const struct got_error *
697 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
698 struct commit_queue_entry *last_displayed_entry,
699 struct commit_queue *commits, struct got_commit_graph *graph,
700 struct got_repository *repo, const char *path)
702 const struct got_error *err = NULL;
703 struct commit_queue_entry *pentry;
707 pentry = TAILQ_NEXT(last_displayed_entry, entry);
708 if (pentry == NULL) {
709 err = fetch_next_commit(&pentry, last_displayed_entry,
710 commits, graph, repo, path);
711 if (err || pentry == NULL)
714 last_displayed_entry = pentry;
716 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
719 *first_displayed_entry = pentry;
720 } while (++nscrolled < maxscroll);
725 static const struct got_error *
726 show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
727 struct got_repository *repo)
729 const struct got_error *err;
730 struct got_object *obj1 = NULL, *obj2 = NULL;
731 struct got_object_qid *parent_id;
732 struct tog_view *view;
734 err = got_object_open(&obj2, repo, entry->id);
738 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
740 err = got_object_open(&obj1, repo, parent_id->id);
745 view = open_view(0, 0, 0, 0, parent_view);
747 err = got_error_from_errno();
751 err = show_diff_view(view, obj1, obj2, repo);
755 got_object_close(obj1);
757 got_object_close(obj2);
761 static const struct got_error *
762 browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
763 struct got_repository *repo)
765 const struct got_error *err = NULL;
766 struct got_tree_object *tree;
767 struct tog_view *view;
769 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
773 view = open_view(0, 0, 0, 0, parent_view);
775 err = got_error_from_errno();
778 err = show_tree_view(view, tree, entry->id, repo);
781 got_object_tree_close(tree);
785 static const struct got_error *
786 show_log_view(struct tog_view *view, struct got_object_id *start_id,
787 struct got_repository *repo, const char *path)
789 const struct got_error *err = NULL;
790 struct got_object_id *head_id = NULL;
791 int ch, done = 0, selected = 0, nfetched;
792 struct got_commit_graph *graph = NULL;
793 struct commit_queue commits;
794 struct commit_queue_entry *first_displayed_entry = NULL;
795 struct commit_queue_entry *last_displayed_entry = NULL;
796 struct commit_queue_entry *selected_entry = NULL;
797 char *in_repo_path = NULL;
799 err = got_repo_map_path(&in_repo_path, repo, path);
803 err = get_head_commit_id(&head_id, repo);
807 /* The graph contains all commits. */
808 err = got_commit_graph_open(&graph, head_id, 0, repo);
811 /* The commit queue contains a subset of commits filtered by path. */
812 TAILQ_INIT(&commits.head);
813 commits.ncommits = 0;
815 /* Populate commit graph with a sufficient number of commits. */
816 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
822 * Open the initial batch of commits, sorted in commit graph order.
823 * We keep all commits open throughout the lifetime of the log view
824 * in order to avoid having to re-fetch commits from disk while
825 * updating the display.
827 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
830 if (err->code != GOT_ERR_ITER_COMPLETED)
835 show_panel(view->panel);
837 first_displayed_entry = TAILQ_FIRST(&commits.head);
838 selected_entry = first_displayed_entry;
840 err = draw_commits(view, &last_displayed_entry, &selected_entry,
841 first_displayed_entry, &commits, selected, view->nlines,
842 graph, repo, in_repo_path);
846 nodelay(stdscr, FALSE);
847 ch = wgetch(view->window);
848 nodelay(stdscr, TRUE);
861 scroll_up(&first_displayed_entry, 1, &commits);
864 if (TAILQ_FIRST(&commits.head) ==
865 first_displayed_entry) {
869 scroll_up(&first_displayed_entry, view->nlines,
874 if (selected < MIN(view->nlines - 2,
875 commits.ncommits - 1)) {
879 err = scroll_down(&first_displayed_entry, 1,
880 last_displayed_entry, &commits, graph,
883 if (err->code != GOT_ERR_ITER_COMPLETED)
889 struct commit_queue_entry *first = first_displayed_entry;
890 err = scroll_down(&first_displayed_entry, view->nlines,
891 last_displayed_entry, &commits, graph,
894 if (err->code != GOT_ERR_ITER_COMPLETED)
896 /* can't scroll any further; move cursor down */
897 if (first == first_displayed_entry && selected <
898 MIN(view->nlines - 2, commits.ncommits - 1)) {
899 selected = MIN(view->nlines - 2,
900 commits.ncommits - 1);
907 err = view_resize(view);
910 if (selected > view->nlines - 2)
911 selected = view->nlines - 2;
912 if (selected > commits.ncommits - 1)
913 selected = commits.ncommits - 1;
917 err = show_commit(view, selected_entry, repo);
920 show_panel(view->panel);
923 err = browse_commit(view, selected_entry, repo);
926 show_panel(view->panel);
935 got_commit_graph_close(graph);
936 free_commits(&commits);
941 static const struct got_error *
942 cmd_log(int argc, char *argv[])
944 const struct got_error *error;
945 struct got_repository *repo = NULL;
946 struct got_object_id *start_id = NULL;
947 char *path = NULL, *repo_path = NULL, *cwd = NULL;
948 char *start_commit = NULL;
950 struct tog_view *view;
953 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
957 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
960 start_commit = optarg;
963 repo_path = realpath(optarg, NULL);
964 if (repo_path == NULL)
979 path = strdup(argv[0]);
983 return got_error_from_errno();
985 cwd = getcwd(NULL, 0);
987 error = got_error_from_errno();
990 if (repo_path == NULL) {
991 repo_path = strdup(cwd);
992 if (repo_path == NULL) {
993 error = got_error_from_errno();
998 error = got_repo_open(&repo, repo_path);
1002 if (start_commit == NULL) {
1003 error = get_head_commit_id(&start_id, repo);
1007 struct got_object *obj;
1008 error = got_object_open_by_id_str(&obj, repo, start_commit);
1009 if (error == NULL) {
1010 start_id = got_object_get_id(obj);
1011 if (start_id == NULL)
1012 error = got_error_from_errno();
1019 view = open_view(0, 0, 0, 0, NULL);
1021 error = got_error_from_errno();
1024 error = show_log_view(view, start_id, repo, path);
1032 got_repo_close(repo);
1040 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1046 parse_next_line(FILE *f, size_t *len)
1051 const char delim[3] = { '\0', '\0', '\0'};
1053 line = fparseln(f, &linelen, &lineno, delim, 0);
1059 static const struct got_error *
1060 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1061 int *last_displayed_line, int *eof, int max_lines)
1063 const struct got_error *err;
1064 int nlines = 0, nprinted = 0;
1071 werase(view->window);
1074 while (nprinted < max_lines) {
1075 line = parse_next_line(f, &len);
1080 if (++nlines < *first_displayed_line) {
1085 err = format_line(&wline, &width, line, view->ncols);
1091 waddwstr(view->window, wline);
1092 if (width < view->ncols)
1093 waddch(view->window, '\n');
1094 if (++nprinted == 1)
1095 *first_displayed_line = nlines;
1100 *last_displayed_line = nlines;
1108 static const struct got_error *
1109 show_diff_view(struct tog_view *view, struct got_object *obj1,
1110 struct got_object *obj2, struct got_repository *repo)
1112 const struct got_error *err;
1115 int first_displayed_line = 1, last_displayed_line = view->nlines;
1118 if (obj1 != NULL && obj2 != NULL &&
1119 got_object_get_type(obj1) != got_object_get_type(obj2))
1120 return got_error(GOT_ERR_OBJ_TYPE);
1124 return got_error_from_errno();
1126 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1127 case GOT_OBJ_TYPE_BLOB:
1128 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1130 case GOT_OBJ_TYPE_TREE:
1131 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1133 case GOT_OBJ_TYPE_COMMIT:
1134 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1137 return got_error(GOT_ERR_OBJ_TYPE);
1142 show_panel(view->panel);
1145 err = draw_file(view, f, &first_displayed_line,
1146 &last_displayed_line, &eof, view->nlines);
1149 nodelay(stdscr, FALSE);
1150 ch = wgetch(view->window);
1151 nodelay(stdscr, TRUE);
1158 if (first_displayed_line > 1)
1159 first_displayed_line--;
1164 while (i++ < view->nlines - 1 &&
1165 first_displayed_line > 1)
1166 first_displayed_line--;
1171 first_displayed_line++;
1176 while (!eof && i++ < view->nlines - 1) {
1177 char *line = parse_next_line(f, NULL);
1178 first_displayed_line++;
1184 err = view_resize(view);
1197 static const struct got_error *
1198 cmd_diff(int argc, char *argv[])
1200 const struct got_error *error = NULL;
1201 struct got_repository *repo = NULL;
1202 struct got_object *obj1 = NULL, *obj2 = NULL;
1203 char *repo_path = NULL;
1204 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1206 struct tog_view *view;
1209 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1213 while ((ch = getopt(argc, argv, "")) != -1) {
1225 usage_diff(); /* TODO show local worktree changes */
1226 } else if (argc == 2) {
1227 repo_path = getcwd(NULL, 0);
1228 if (repo_path == NULL)
1229 return got_error_from_errno();
1230 obj_id_str1 = argv[0];
1231 obj_id_str2 = argv[1];
1232 } else if (argc == 3) {
1233 repo_path = realpath(argv[0], NULL);
1234 if (repo_path == NULL)
1235 return got_error_from_errno();
1236 obj_id_str1 = argv[1];
1237 obj_id_str2 = argv[2];
1241 error = got_repo_open(&repo, repo_path);
1246 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1250 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1254 view = open_view(0, 0, 0, 0, NULL);
1256 error = got_error_from_errno();
1259 error = show_diff_view(view, obj1, obj2, repo);
1262 got_repo_close(repo);
1264 got_object_close(obj1);
1266 got_object_close(obj2);
1274 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1279 struct tog_blame_line {
1281 struct got_object_id *id;
1284 static const struct got_error *
1285 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1286 const char *path, struct tog_blame_line *lines, int nlines,
1287 int blame_complete, int selected_line, int *first_displayed_line,
1288 int *last_displayed_line, int *eof, int max_lines)
1290 const struct got_error *err;
1291 int lineno = 0, nprinted = 0;
1296 struct tog_blame_line *blame_line;
1297 struct got_object_id *prev_id = NULL;
1300 err = got_object_id_str(&id_str, id);
1305 werase(view->window);
1307 if (asprintf(&line, "commit: %s", id_str) == -1) {
1308 err = got_error_from_errno();
1313 err = format_line(&wline, &width, line, view->ncols);
1316 waddwstr(view->window, wline);
1319 if (width < view->ncols)
1320 waddch(view->window, '\n');
1322 if (asprintf(&line, "[%d/%d] %s%s",
1323 *first_displayed_line - 1 + selected_line, nlines,
1324 blame_complete ? "" : "annotating ", path) == -1) {
1326 return got_error_from_errno();
1329 err = format_line(&wline, &width, line, view->ncols);
1334 waddwstr(view->window, wline);
1337 if (width < view->ncols)
1338 waddch(view->window, '\n');
1341 while (nprinted < max_lines - 2) {
1342 line = parse_next_line(f, &len);
1347 if (++lineno < *first_displayed_line) {
1352 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1353 err = format_line(&wline, &width, line, wlimit);
1359 if (nprinted == selected_line - 1)
1360 wstandout(view->window);
1362 blame_line = &lines[lineno - 1];
1363 if (blame_line->annotated && prev_id &&
1364 got_object_id_cmp(prev_id, blame_line->id) == 0)
1365 waddstr(view->window, " ");
1366 else if (blame_line->annotated) {
1368 err = got_object_id_str(&id_str, blame_line->id);
1374 wprintw(view->window, "%.8s ", id_str);
1376 prev_id = blame_line->id;
1378 waddstr(view->window, "........ ");
1382 waddwstr(view->window, wline);
1383 while (width < wlimit) {
1384 waddch(view->window, ' ');
1387 if (nprinted == selected_line - 1)
1388 wstandend(view->window);
1389 if (++nprinted == 1)
1390 *first_displayed_line = lineno;
1395 *last_displayed_line = lineno;
1403 struct tog_blame_cb_args {
1404 pthread_mutex_t *mutex;
1405 struct tog_blame_line *lines; /* one per line */
1408 struct tog_view *view;
1409 struct got_object_id *commit_id;
1412 int *first_displayed_line;
1413 int *last_displayed_line;
1418 static const struct got_error *
1419 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1421 const struct got_error *err = NULL;
1422 struct tog_blame_cb_args *a = arg;
1423 struct tog_blame_line *line;
1426 if (nlines != a->nlines ||
1427 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1428 return got_error(GOT_ERR_RANGE);
1430 if (pthread_mutex_lock(a->mutex) != 0)
1431 return got_error_from_errno();
1433 if (*a->quit) { /* user has quit the blame view */
1434 err = got_error(GOT_ERR_ITER_COMPLETED);
1439 goto done; /* no change in this commit */
1441 line = &a->lines[lineno - 1];
1442 if (line->annotated)
1445 line->id = got_object_id_dup(id);
1446 if (line->id == NULL) {
1447 err = got_error_from_errno();
1450 line->annotated = 1;
1452 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1453 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1454 a->last_displayed_line, &eof, a->view->nlines);
1456 if (pthread_mutex_unlock(a->mutex) != 0)
1457 return got_error_from_errno();
1461 struct tog_blame_thread_args {
1463 struct got_repository *repo;
1464 struct tog_blame_cb_args *cb_args;
1469 blame_thread(void *arg)
1471 const struct got_error *err;
1472 struct tog_blame_thread_args *ta = arg;
1473 struct tog_blame_cb_args *a = ta->cb_args;
1476 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1477 blame_cb, ta->cb_args);
1479 if (pthread_mutex_lock(a->mutex) != 0)
1480 return (void *)got_error_from_errno();
1482 got_repo_close(ta->repo);
1486 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1487 a->lines, a->nlines, 1, *a->selected_line,
1488 a->first_displayed_line, a->last_displayed_line, &eof,
1491 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1492 err = got_error_from_errno();
1497 static struct got_object_id *
1498 get_selected_commit_id(struct tog_blame_line *lines,
1499 int first_displayed_line, int selected_line)
1501 struct tog_blame_line *line;
1503 line = &lines[first_displayed_line - 1 + selected_line - 1];
1504 if (!line->annotated)
1510 static const struct got_error *
1511 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1512 struct tog_blame_line *lines, int first_displayed_line,
1513 int selected_line, struct got_repository *repo)
1515 const struct got_error *err = NULL;
1516 struct got_commit_object *commit = NULL;
1517 struct got_object_id *selected_id;
1518 struct got_object_qid *pid;
1523 selected_id = get_selected_commit_id(lines,
1524 first_displayed_line, selected_line);
1525 if (selected_id == NULL)
1528 err = got_object_open(obj, repo, selected_id);
1532 err = got_object_commit_open(&commit, repo, *obj);
1536 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1538 err = got_object_open(pobj, repo, pid->id);
1544 got_object_commit_close(commit);
1551 struct tog_blame_line *lines;
1554 struct tog_blame_thread_args thread_args;
1555 struct tog_blame_cb_args cb_args;
1559 static const struct got_error *
1560 stop_blame(struct tog_blame *blame)
1562 const struct got_error *err = NULL;
1565 if (blame->thread) {
1566 if (pthread_join(blame->thread, (void **)&err) != 0)
1567 err = got_error_from_errno();
1568 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1570 blame->thread = NULL;
1572 if (blame->thread_args.repo) {
1573 got_repo_close(blame->thread_args.repo);
1574 blame->thread_args.repo = NULL;
1580 for (i = 0; i < blame->nlines; i++)
1581 free(blame->lines[i].id);
1583 blame->lines = NULL;
1584 free(blame->cb_args.commit_id);
1585 blame->cb_args.commit_id = NULL;
1590 static const struct got_error *
1591 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1592 struct tog_view *view, int *blame_complete,
1593 int *first_displayed_line, int *last_displayed_line,
1594 int *selected_line, int *done, const char *path,
1595 struct got_object_id *commit_id,
1596 struct got_repository *repo)
1598 const struct got_error *err = NULL;
1599 struct got_blob_object *blob = NULL;
1600 struct got_repository *thread_repo = NULL;
1601 struct got_object *obj;
1603 err = got_object_open_by_path(&obj, repo, commit_id, path);
1606 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1607 err = got_error(GOT_ERR_OBJ_TYPE);
1611 err = got_object_blob_open(&blob, repo, obj, 8192);
1614 blame->f = got_opentemp();
1615 if (blame->f == NULL) {
1616 err = got_error_from_errno();
1619 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1624 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1625 if (blame->lines == NULL) {
1626 err = got_error_from_errno();
1630 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1634 blame->cb_args.view = view;
1635 blame->cb_args.lines = blame->lines;
1636 blame->cb_args.nlines = blame->nlines;
1637 blame->cb_args.mutex = mutex;
1638 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1639 if (blame->cb_args.commit_id == NULL) {
1640 err = got_error_from_errno();
1643 blame->cb_args.f = blame->f;
1644 blame->cb_args.path = path;
1645 blame->cb_args.first_displayed_line = first_displayed_line;
1646 blame->cb_args.selected_line = selected_line;
1647 blame->cb_args.last_displayed_line = last_displayed_line;
1648 blame->cb_args.quit = done;
1650 blame->thread_args.path = path;
1651 blame->thread_args.repo = thread_repo;
1652 blame->thread_args.cb_args = &blame->cb_args;
1653 blame->thread_args.complete = blame_complete;
1654 *blame_complete = 0;
1656 if (pthread_create(&blame->thread, NULL, blame_thread,
1657 &blame->thread_args) != 0) {
1658 err = got_error_from_errno();
1664 got_object_blob_close(blob);
1666 got_object_close(obj);
1672 static const struct got_error *
1673 show_blame_view(struct tog_view *view, const char *path,
1674 struct got_object_id *commit_id, struct got_repository *repo)
1676 const struct got_error *err = NULL, *thread_err = NULL;
1677 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1678 int selected_line = first_displayed_line;
1679 int eof, blame_complete = 0;
1680 struct got_object *obj = NULL, *pobj = NULL;
1681 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1682 struct tog_blame blame;
1683 struct got_object_id_queue blamed_commits;
1684 struct got_object_qid *blamed_commit = NULL;
1685 struct tog_view *diff_view;
1687 SIMPLEQ_INIT(&blamed_commits);
1689 if (pthread_mutex_init(&mutex, NULL) != 0) {
1690 err = got_error_from_errno();
1694 err = got_object_qid_alloc(&blamed_commit, commit_id);
1697 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1699 show_panel(view->panel);
1700 last_displayed_line = view->nlines;
1702 memset(&blame, 0, sizeof(blame));
1703 err = run_blame(&blame, &mutex, view, &blame_complete,
1704 &first_displayed_line, &last_displayed_line,
1705 &selected_line, &done, path, blamed_commit->id, repo);
1710 if (pthread_mutex_lock(&mutex) != 0) {
1711 err = got_error_from_errno();
1714 err = draw_blame(view, blamed_commit->id, blame.f, path,
1715 blame.lines, blame.nlines, blame_complete, selected_line,
1716 &first_displayed_line, &last_displayed_line, &eof,
1718 if (pthread_mutex_unlock(&mutex) != 0) {
1719 err = got_error_from_errno();
1724 nodelay(stdscr, FALSE);
1725 ch = wgetch(view->window);
1726 nodelay(stdscr, TRUE);
1727 if (pthread_mutex_lock(&mutex) != 0) {
1728 err = got_error_from_errno();
1737 if (selected_line > 1)
1739 else if (selected_line == 1 &&
1740 first_displayed_line > 1)
1741 first_displayed_line--;
1745 if (first_displayed_line == 1) {
1749 if (first_displayed_line > view->nlines - 2)
1750 first_displayed_line -=
1753 first_displayed_line = 1;
1757 if (selected_line < view->nlines - 2 &&
1758 first_displayed_line + selected_line <=
1761 else if (last_displayed_line < blame.nlines)
1762 first_displayed_line++;
1766 struct got_object_id *id;
1767 id = get_selected_commit_id(blame.lines,
1768 first_displayed_line, selected_line);
1769 if (id == NULL || got_object_id_cmp(id,
1770 blamed_commit->id) == 0)
1772 err = open_selected_commit(&pobj, &obj,
1773 blame.lines, first_displayed_line,
1774 selected_line, repo);
1777 if (pobj == NULL && obj == NULL)
1779 if (ch == 'p' && pobj == NULL)
1782 if (pthread_mutex_unlock(&mutex) != 0) {
1783 err = got_error_from_errno();
1786 thread_err = stop_blame(&blame);
1788 if (pthread_mutex_lock(&mutex) != 0) {
1789 err = got_error_from_errno();
1794 id = got_object_get_id(ch == 'b' ? obj : pobj);
1795 got_object_close(obj);
1798 got_object_close(pobj);
1802 err = got_error_from_errno();
1805 err = got_object_qid_alloc(&blamed_commit, id);
1809 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1810 blamed_commit, entry);
1811 err = run_blame(&blame, &mutex, view,
1812 &blame_complete, &first_displayed_line,
1813 &last_displayed_line, &selected_line,
1814 &done, path, blamed_commit->id, repo);
1820 struct got_object_qid *first;
1821 first = SIMPLEQ_FIRST(&blamed_commits);
1822 if (!got_object_id_cmp(first->id, commit_id))
1825 if (pthread_mutex_unlock(&mutex) != 0) {
1826 err = got_error_from_errno();
1829 thread_err = stop_blame(&blame);
1831 if (pthread_mutex_lock(&mutex) != 0) {
1832 err = got_error_from_errno();
1837 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1838 got_object_qid_free(blamed_commit);
1839 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1840 err = run_blame(&blame, &mutex, view,
1841 &blame_complete, &first_displayed_line,
1842 &last_displayed_line, &selected_line,
1843 &done, path, blamed_commit->id, repo);
1850 err = open_selected_commit(&pobj, &obj,
1851 blame.lines, first_displayed_line,
1852 selected_line, repo);
1855 if (pobj == NULL && obj == NULL)
1857 diff_view = open_view(0, 0, 0, 0, view);
1858 if (diff_view == NULL) {
1859 err = got_error_from_errno();
1862 err = show_diff_view(diff_view, pobj, obj, repo);
1863 close_view(diff_view);
1865 got_object_close(pobj);
1868 got_object_close(obj);
1870 show_panel(view->panel);
1876 if (last_displayed_line >= blame.nlines &&
1877 selected_line < view->nlines - 2) {
1878 selected_line = MIN(blame.nlines,
1882 if (last_displayed_line + view->nlines - 2 <=
1884 first_displayed_line +=
1887 first_displayed_line =
1888 blame.nlines - (view->nlines - 3);
1891 err = view_resize(view);
1894 if (selected_line > view->nlines - 2) {
1895 selected_line = MIN(blame.nlines,
1902 if (pthread_mutex_unlock(&mutex) != 0)
1903 err = got_error_from_errno();
1904 if (err || thread_err)
1909 got_object_close(pobj);
1911 thread_err = stop_blame(&blame);
1912 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1913 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1914 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1915 got_object_qid_free(blamed_commit);
1917 return thread_err ? thread_err : err;
1920 static const struct got_error *
1921 cmd_blame(int argc, char *argv[])
1923 const struct got_error *error;
1924 struct got_repository *repo = NULL;
1925 char *repo_path = NULL;
1927 struct got_object_id *commit_id = NULL;
1928 char *commit_id_str = NULL;
1930 struct tog_view *view;
1933 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1937 while ((ch = getopt(argc, argv, "c:")) != -1) {
1940 commit_id_str = optarg;
1953 } else if (argc == 1) {
1954 repo_path = getcwd(NULL, 0);
1955 if (repo_path == NULL)
1956 return got_error_from_errno();
1958 } else if (argc == 2) {
1959 repo_path = realpath(argv[0], NULL);
1960 if (repo_path == NULL)
1961 return got_error_from_errno();
1966 error = got_repo_open(&repo, repo_path);
1971 if (commit_id_str == NULL) {
1972 struct got_reference *head_ref;
1973 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1976 error = got_ref_resolve(&commit_id, repo, head_ref);
1977 got_ref_close(head_ref);
1979 struct got_object *obj;
1980 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1983 commit_id = got_object_get_id(obj);
1984 if (commit_id == NULL)
1985 error = got_error_from_errno();
1986 got_object_close(obj);
1991 view = open_view(0, 0, 0, 0, NULL);
1993 error = got_error_from_errno();
1996 error = show_blame_view(view, path, commit_id, repo);
2001 got_repo_close(repo);
2005 static const struct got_error *
2006 draw_tree_entries(struct tog_view *view,
2007 struct got_tree_entry **first_displayed_entry,
2008 struct got_tree_entry **last_displayed_entry,
2009 struct got_tree_entry **selected_entry, int *ndisplayed,
2010 const char *label, int show_ids, const char *parent_path,
2011 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2013 const struct got_error *err = NULL;
2014 struct got_tree_entry *te;
2020 werase(view->window);
2025 err = format_line(&wline, &width, label, view->ncols);
2028 waddwstr(view->window, wline);
2031 if (width < view->ncols)
2032 waddch(view->window, '\n');
2035 err = format_line(&wline, &width, parent_path, view->ncols);
2038 waddwstr(view->window, wline);
2041 if (width < view->ncols)
2042 waddch(view->window, '\n');
2045 waddch(view->window, '\n');
2049 te = SIMPLEQ_FIRST(&entries->head);
2050 if (*first_displayed_entry == NULL) {
2051 if (selected == 0) {
2052 wstandout(view->window);
2053 *selected_entry = NULL;
2055 waddstr(view->window, " ..\n"); /* parent directory */
2057 wstandend(view->window);
2064 while (te != *first_displayed_entry)
2065 te = SIMPLEQ_NEXT(te, entry);
2069 char *line = NULL, *id_str = NULL;
2072 err = got_object_id_str(&id_str, te->id);
2074 return got_error_from_errno();
2076 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2077 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2079 return got_error_from_errno();
2082 err = format_line(&wline, &width, line, view->ncols);
2087 if (n == selected) {
2088 wstandout(view->window);
2089 *selected_entry = te;
2091 waddwstr(view->window, wline);
2092 if (width < view->ncols)
2093 waddch(view->window, '\n');
2095 wstandend(view->window);
2101 *last_displayed_entry = te;
2104 te = SIMPLEQ_NEXT(te, entry);
2111 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2112 const struct got_tree_entries *entries, int isroot)
2114 struct got_tree_entry *te, *prev;
2117 if (*first_displayed_entry == NULL)
2120 te = SIMPLEQ_FIRST(&entries->head);
2121 if (*first_displayed_entry == te) {
2123 *first_displayed_entry = NULL;
2127 /* XXX this is stupid... switch to TAILQ? */
2128 for (i = 0; i < maxscroll; i++) {
2129 while (te != *first_displayed_entry) {
2131 te = SIMPLEQ_NEXT(te, entry);
2133 *first_displayed_entry = prev;
2134 te = SIMPLEQ_FIRST(&entries->head);
2136 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2137 *first_displayed_entry = NULL;
2141 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2142 struct got_tree_entry *last_displayed_entry,
2143 const struct got_tree_entries *entries)
2145 struct got_tree_entry *next;
2148 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2151 if (*first_displayed_entry)
2152 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2154 next = SIMPLEQ_FIRST(&entries->head);
2156 *first_displayed_entry = next;
2157 if (++n >= maxscroll)
2159 next = SIMPLEQ_NEXT(next, entry);
2163 struct tog_parent_tree {
2164 TAILQ_ENTRY(tog_parent_tree) entry;
2165 struct got_tree_object *tree;
2166 struct got_tree_entry *first_displayed_entry;
2167 struct got_tree_entry *selected_entry;
2171 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2173 static const struct got_error *
2174 tree_entry_path(char **path, struct tog_parent_trees *parents,
2175 struct got_tree_entry *te)
2177 const struct got_error *err = NULL;
2178 struct tog_parent_tree *pt;
2179 size_t len = 2; /* for leading slash and NUL */
2181 TAILQ_FOREACH(pt, parents, entry)
2182 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2184 len += strlen(te->name);
2186 *path = calloc(1, len);
2188 return got_error_from_errno();
2191 pt = TAILQ_LAST(parents, tog_parent_trees);
2193 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2194 err = got_error(GOT_ERR_NO_SPACE);
2197 if (strlcat(*path, "/", len) >= len) {
2198 err = got_error(GOT_ERR_NO_SPACE);
2201 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2204 if (strlcat(*path, te->name, len) >= len) {
2205 err = got_error(GOT_ERR_NO_SPACE);
2217 static const struct got_error *
2218 blame_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2219 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2220 struct got_repository *repo)
2222 const struct got_error *err = NULL;
2225 err = tree_entry_path(&path, parents, te);
2229 err = show_blame_view(view, path, commit_id, repo);
2234 static const struct got_error *
2235 log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2236 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2237 struct got_repository *repo)
2239 const struct got_error *err = NULL;
2242 err = tree_entry_path(&path, parents, te);
2246 err = show_log_view(view, commit_id, repo, path);
2251 static const struct got_error *
2252 show_tree_view(struct tog_view *view, struct got_tree_object *root,
2253 struct got_object_id *commit_id, struct got_repository *repo)
2255 const struct got_error *err = NULL;
2256 int ch, done = 0, selected = 0, show_ids = 0;
2257 struct got_tree_object *tree = root;
2258 const struct got_tree_entries *entries;
2259 struct got_tree_entry *first_displayed_entry = NULL;
2260 struct got_tree_entry *last_displayed_entry = NULL;
2261 struct got_tree_entry *selected_entry = NULL;
2262 char *commit_id_str = NULL, *tree_label = NULL;
2263 int nentries, ndisplayed;
2264 struct tog_parent_trees parents;
2266 TAILQ_INIT(&parents);
2268 err = got_object_id_str(&commit_id_str, commit_id);
2272 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2273 err = got_error_from_errno();
2277 show_panel(view->panel);
2279 entries = got_object_tree_get_entries(root);
2280 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2283 entries = got_object_tree_get_entries(tree);
2284 nentries = entries->nentries;
2286 nentries++; /* '..' directory */
2288 err = tree_entry_path(&parent_path, &parents, NULL);
2292 err = draw_tree_entries(view, &first_displayed_entry,
2293 &last_displayed_entry, &selected_entry, &ndisplayed,
2294 tree_label, show_ids, parent_path, entries, selected,
2295 view->nlines, tree == root);
2300 nodelay(stdscr, FALSE);
2301 ch = wgetch(view->window);
2302 nodelay(stdscr, TRUE);
2308 show_ids = !show_ids;
2311 if (selected_entry) {
2312 struct tog_view *log_view;
2313 log_view = open_view(0, 0, 0, 0, view);
2314 if (log_view == NULL) {
2315 err = got_error_from_errno();
2318 err = log_tree_entry(log_view,
2319 selected_entry, &parents,
2321 close_view(log_view);
2332 tree_scroll_up(&first_displayed_entry, 1,
2333 entries, tree == root);
2336 if (SIMPLEQ_FIRST(&entries->head) ==
2337 first_displayed_entry) {
2339 first_displayed_entry = NULL;
2343 tree_scroll_up(&first_displayed_entry,
2344 view->nlines, entries, tree == root);
2348 if (selected < ndisplayed - 1) {
2352 tree_scroll_down(&first_displayed_entry, 1,
2353 last_displayed_entry, entries);
2356 tree_scroll_down(&first_displayed_entry,
2357 view->nlines, last_displayed_entry,
2359 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2361 /* can't scroll any further; move cursor down */
2362 if (selected < ndisplayed - 1)
2363 selected = ndisplayed - 1;
2367 if (selected_entry == NULL) {
2368 struct tog_parent_tree *parent;
2370 /* user selected '..' */
2373 parent = TAILQ_FIRST(&parents);
2374 TAILQ_REMOVE(&parents, parent, entry);
2375 got_object_tree_close(tree);
2376 tree = parent->tree;
2377 first_displayed_entry =
2378 parent->first_displayed_entry;
2379 selected_entry = parent->selected_entry;
2380 selected = parent->selected;
2382 } else if (S_ISDIR(selected_entry->mode)) {
2383 struct tog_parent_tree *parent;
2384 struct got_tree_object *child;
2385 err = got_object_open_as_tree(
2386 &child, repo, selected_entry->id);
2389 parent = calloc(1, sizeof(*parent));
2390 if (parent == NULL) {
2391 err = got_error_from_errno();
2394 parent->tree = tree;
2395 parent->first_displayed_entry =
2396 first_displayed_entry;
2397 parent->selected_entry = selected_entry;
2398 parent->selected = selected;
2399 TAILQ_INSERT_HEAD(&parents, parent,
2403 first_displayed_entry = NULL;
2404 } else if (S_ISREG(selected_entry->mode)) {
2405 struct tog_view *blame_view =
2406 open_view(0, 0, 0, 0, view);
2407 if (blame_view == NULL) {
2408 err = got_error_from_errno();
2411 err = blame_tree_entry(blame_view,
2412 selected_entry, &parents,
2414 close_view(blame_view);
2420 err = view_resize(view);
2423 if (selected > view->nlines)
2424 selected = ndisplayed - 1;
2432 free(commit_id_str);
2433 while (!TAILQ_EMPTY(&parents)) {
2434 struct tog_parent_tree *parent;
2435 parent = TAILQ_FIRST(&parents);
2436 TAILQ_REMOVE(&parents, parent, entry);
2441 got_object_tree_close(tree);
2449 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2454 static const struct got_error *
2455 cmd_tree(int argc, char *argv[])
2457 const struct got_error *error;
2458 struct got_repository *repo = NULL;
2459 char *repo_path = NULL;
2460 struct got_object_id *commit_id = NULL;
2461 char *commit_id_arg = NULL;
2462 struct got_commit_object *commit = NULL;
2463 struct got_tree_object *tree = NULL;
2465 struct tog_view *view;
2468 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2472 while ((ch = getopt(argc, argv, "c:")) != -1) {
2475 commit_id_arg = optarg;
2487 repo_path = getcwd(NULL, 0);
2488 if (repo_path == NULL)
2489 return got_error_from_errno();
2490 } else if (argc == 1) {
2491 repo_path = realpath(argv[0], NULL);
2492 if (repo_path == NULL)
2493 return got_error_from_errno();
2497 error = got_repo_open(&repo, repo_path);
2502 if (commit_id_arg == NULL) {
2503 error = get_head_commit_id(&commit_id, repo);
2507 struct got_object *obj;
2508 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2509 if (error == NULL) {
2510 commit_id = got_object_get_id(obj);
2511 if (commit_id == NULL)
2512 error = got_error_from_errno();
2518 error = got_object_open_as_commit(&commit, repo, commit_id);
2522 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2526 view = open_view(0, 0, 0, 0, NULL);
2528 error = got_error_from_errno();
2531 error = show_tree_view(view, tree, commit_id, repo);
2536 got_object_commit_close(commit);
2538 got_object_tree_close(tree);
2540 got_repo_close(repo);
2550 intrflush(stdscr, FALSE);
2551 keypad(stdscr, TRUE);
2560 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2561 "Available commands:\n", getprogname());
2562 for (i = 0; i < nitems(tog_commands); i++) {
2563 struct tog_cmd *cmd = &tog_commands[i];
2564 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2570 make_argv(const char *arg0, const char *arg1)
2573 int argc = (arg1 == NULL ? 1 : 2);
2575 argv = calloc(argc, sizeof(char *));
2578 argv[0] = strdup(arg0);
2579 if (argv[0] == NULL)
2582 argv[1] = strdup(arg1);
2583 if (argv[1] == NULL)
2591 main(int argc, char *argv[])
2593 const struct got_error *error = NULL;
2594 struct tog_cmd *cmd = NULL;
2596 char **cmd_argv = NULL;
2598 setlocale(LC_ALL, "");
2600 while ((ch = getopt(argc, argv, "h")) != -1) {
2619 /* Build an argument vector which runs a default command. */
2620 cmd = &tog_commands[0];
2621 cmd_argv = make_argv(cmd->name, NULL);
2626 /* Did the user specific a command? */
2627 for (i = 0; i < nitems(tog_commands); i++) {
2628 if (strncmp(tog_commands[i].name, argv[0],
2629 strlen(argv[0])) == 0) {
2630 cmd = &tog_commands[i];
2632 tog_commands[i].cmd_usage();
2637 /* Did the user specify a repository? */
2638 char *repo_path = realpath(argv[0], NULL);
2640 struct got_repository *repo;
2641 error = got_repo_open(&repo, repo_path);
2643 got_repo_close(repo);
2645 error = got_error_from_errno();
2648 fprintf(stderr, "%s: '%s' is not a "
2649 "known command\n", getprogname(),
2653 fprintf(stderr, "%s: '%s' is neither a known "
2654 "command nor a path to a repository\n",
2655 getprogname(), argv[0]);
2659 cmd = &tog_commands[0];
2660 cmd_argv = make_argv(cmd->name, repo_path);
2668 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2675 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);