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" },
85 static struct tog_view {
90 static const struct got_error *
91 show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
92 struct got_repository *);
93 static const struct got_error *
94 show_log_view(struct got_object_id *, struct got_repository *, const char *);
95 static const struct got_error *
96 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
97 static const struct got_error *
98 show_tree_view(struct got_tree_object *, struct got_object_id *,
99 struct got_repository *);
102 close_view(struct tog_view *view)
105 del_panel(view->panel);
107 delwin(view->window);
111 static struct tog_view *
114 struct tog_view *view = malloc(sizeof(*view));
119 view->window = newwin(0, 0, 0, 0);
120 if (view->window == NULL) {
124 view->panel = new_panel(view->window);
125 if (view->panel == NULL) {
130 keypad(view->window, TRUE);
138 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
143 /* Create newly allocated wide-character string equivalent to a byte string. */
144 static const struct got_error *
145 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
148 const struct got_error *err = NULL;
151 *wlen = mbstowcs(NULL, s, 0);
152 if (*wlen == (size_t)-1) {
155 return got_error_from_errno();
157 /* byte string invalid in current encoding; try to "fix" it */
158 err = got_mbsavis(&vis, &vislen, s);
161 *wlen = mbstowcs(NULL, vis, 0);
162 if (*wlen == (size_t)-1) {
163 err = got_error_from_errno(); /* give up */
168 *ws = calloc(*wlen + 1, sizeof(*ws));
170 err = got_error_from_errno();
174 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
175 err = got_error_from_errno();
186 /* Format a line for display, ensuring that it won't overflow a width limit. */
187 static const struct got_error *
188 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
190 const struct got_error *err = NULL;
192 wchar_t *wline = NULL;
199 err = mbs2ws(&wline, &wlen, line);
204 while (i < wlen && cols < wlimit) {
205 int width = wcwidth(wline[i]);
212 if (cols + width <= wlimit) {
218 if (wline[i] == L'\t')
219 cols += TABSIZE - ((cols + 1) % TABSIZE);
223 err = got_error_from_errno();
238 static const struct got_error *
239 draw_commit(struct tog_view *view, struct got_commit_object *commit,
240 struct got_object_id *id)
242 const struct got_error *err = NULL;
243 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
244 char *logmsg0 = NULL, *logmsg = NULL;
245 char *author0 = NULL, *author = NULL;
246 wchar_t *wlogmsg = NULL, *wauthor = NULL;
247 int author_width, logmsg_width;
248 char *newline, *smallerthan;
251 static const size_t date_display_cols = 9;
252 static const size_t author_display_cols = 16;
253 const int avail = COLS;
255 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
257 return got_error(GOT_ERR_NO_SPACE);
259 if (avail < date_display_cols)
260 limit = MIN(sizeof(datebuf) - 1, avail);
262 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
263 waddnstr(view->window, datebuf, limit);
268 author0 = strdup(commit->author);
269 if (author0 == NULL) {
270 err = got_error_from_errno();
274 smallerthan = strchr(author, '<');
278 char *at = strchr(author, '@');
283 err = format_line(&wauthor, &author_width, author, limit);
286 waddwstr(view->window, wauthor);
288 while (col <= avail && author_width < author_display_cols + 1) {
289 waddch(view->window, ' ');
296 logmsg0 = strdup(commit->logmsg);
297 if (logmsg0 == NULL) {
298 err = got_error_from_errno();
302 while (*logmsg == '\n')
304 newline = strchr(logmsg, '\n');
308 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
311 waddwstr(view->window, wlogmsg);
313 while (col <= avail) {
314 waddch(view->window, ' ');
326 struct commit_queue_entry {
327 TAILQ_ENTRY(commit_queue_entry) entry;
328 struct got_object_id *id;
329 struct got_commit_object *commit;
331 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
332 struct commit_queue {
334 struct commit_queue_head head;
337 static struct commit_queue_entry *
338 alloc_commit_queue_entry(struct got_commit_object *commit,
339 struct got_object_id *id)
341 struct commit_queue_entry *entry;
343 entry = calloc(1, sizeof(*entry));
348 entry->commit = commit;
353 pop_commit(struct commit_queue *commits)
355 struct commit_queue_entry *entry;
357 entry = TAILQ_FIRST(&commits->head);
358 TAILQ_REMOVE(&commits->head, entry, entry);
359 got_object_commit_close(entry->commit);
361 /* Don't free entry->id! It is owned by the commit graph. */
366 free_commits(struct commit_queue *commits)
368 while (!TAILQ_EMPTY(&commits->head))
372 static const struct got_error *
373 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
374 struct got_object_id *start_id, int minqueue, int init,
375 struct got_repository *repo, const char *path)
377 const struct got_error *err = NULL;
378 struct got_object_id *id;
379 struct commit_queue_entry *entry;
380 int nfetched, nqueued = 0, found_obj = 0;
381 int is_root_path = strcmp(path, "/") == 0;
383 err = got_commit_graph_iter_start(graph, start_id);
387 entry = TAILQ_LAST(&commits->head, commit_queue_head);
388 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
391 /* Start ID's commit is already on the queue; skip over it. */
392 err = got_commit_graph_iter_next(&id, graph);
393 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
396 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
402 struct got_commit_object *commit;
404 err = got_commit_graph_iter_next(&id, graph);
406 if (err->code != GOT_ERR_ITER_NEED_MORE)
408 if (nqueued >= minqueue) {
412 err = got_commit_graph_fetch_commits(&nfetched,
421 err = got_object_open_as_commit(&commit, repo, id);
426 struct got_object *obj;
427 struct got_object_qid *pid;
430 err = got_object_open_by_path(&obj, repo, id, path);
432 got_object_commit_close(commit);
433 if (err->code == GOT_ERR_NO_OBJ &&
434 (found_obj || !init)) {
435 /* History stops here. */
436 err = got_error(GOT_ERR_ITER_COMPLETED);
442 pid = SIMPLEQ_FIRST(&commit->parent_ids);
444 struct got_object *pobj;
445 err = got_object_open_by_path(&pobj, repo,
448 if (err->code != GOT_ERR_NO_OBJ) {
449 got_object_close(obj);
450 got_object_commit_close(commit);
456 struct got_object_id *id, *pid;
457 id = got_object_get_id(obj);
459 err = got_error_from_errno();
460 got_object_close(obj);
461 got_object_close(pobj);
464 pid = got_object_get_id(pobj);
466 err = got_error_from_errno();
468 got_object_close(obj);
469 got_object_close(pobj);
473 (got_object_id_cmp(id, pid) != 0);
474 got_object_close(pobj);
479 got_object_close(obj);
481 got_object_commit_close(commit);
486 entry = alloc_commit_queue_entry(commit, id);
488 err = got_error_from_errno();
491 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
499 static const struct got_error *
500 fetch_next_commit(struct commit_queue_entry **pentry,
501 struct commit_queue_entry *entry, struct commit_queue *commits,
502 struct got_commit_graph *graph, struct got_repository *repo,
505 const struct got_error *err = NULL;
509 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
513 /* Next entry to display should now be available. */
514 *pentry = TAILQ_NEXT(entry, entry);
516 return got_error(GOT_ERR_NO_OBJ);
521 static const struct got_error *
522 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
524 const struct got_error *err = NULL;
525 struct got_reference *head_ref;
529 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
533 err = got_ref_resolve(head_id, repo, head_ref);
534 got_ref_close(head_ref);
543 static const struct got_error *
544 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
545 struct commit_queue_entry **selected, struct commit_queue_entry *first,
546 struct commit_queue *commits, int selected_idx, int limit,
547 struct got_commit_graph *graph, struct got_repository *repo,
550 const struct got_error *err = NULL;
551 struct commit_queue_entry *entry;
553 char *id_str, *header;
559 if (ncommits == selected_idx) {
563 entry = TAILQ_NEXT(entry, entry);
567 err = got_object_id_str(&id_str, (*selected)->id);
572 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
573 err = got_error_from_errno();
577 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
578 err = got_error_from_errno();
583 err = format_line(&wline, &width, header, COLS);
590 werase(view->window);
592 waddwstr(view->window, wline);
594 waddch(view->window, '\n');
603 if (ncommits >= limit - 1)
605 if (ncommits == selected_idx)
606 wstandout(view->window);
607 err = draw_commit(view, entry->commit, entry->id);
608 if (ncommits == selected_idx)
609 wstandend(view->window);
614 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
615 err = queue_commits(graph, commits, entry->id, 1,
618 if (err->code != GOT_ERR_ITER_COMPLETED)
623 entry = TAILQ_NEXT(entry, entry);
633 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
634 struct commit_queue *commits)
636 struct commit_queue_entry *entry;
639 entry = TAILQ_FIRST(&commits->head);
640 if (*first_displayed_entry == entry)
643 entry = *first_displayed_entry;
644 while (entry && nscrolled < maxscroll) {
645 entry = TAILQ_PREV(entry, commit_queue_head, entry);
647 *first_displayed_entry = entry;
653 static const struct got_error *
654 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
655 struct commit_queue_entry *last_displayed_entry,
656 struct commit_queue *commits, struct got_commit_graph *graph,
657 struct got_repository *repo, const char *path)
659 const struct got_error *err = NULL;
660 struct commit_queue_entry *pentry;
664 pentry = TAILQ_NEXT(last_displayed_entry, entry);
665 if (pentry == NULL) {
666 err = fetch_next_commit(&pentry, last_displayed_entry,
667 commits, graph, repo, path);
668 if (err || pentry == NULL)
671 last_displayed_entry = pentry;
673 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
676 *first_displayed_entry = pentry;
677 } while (++nscrolled < maxscroll);
682 static const struct got_error *
683 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
685 const struct got_error *err;
686 struct got_object *obj1 = NULL, *obj2 = NULL;
687 struct got_object_qid *parent_id;
688 struct tog_view *view;
690 err = got_object_open(&obj2, repo, entry->id);
694 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
696 err = got_object_open(&obj1, repo, parent_id->id);
703 err = got_error_from_errno();
707 err = show_diff_view(view, obj1, obj2, repo);
711 got_object_close(obj1);
713 got_object_close(obj2);
717 static const struct got_error *
718 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
720 const struct got_error *err = NULL;
721 struct got_tree_object *tree;
723 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
727 err = show_tree_view(tree, entry->id, repo);
728 got_object_tree_close(tree);
732 static const struct got_error *
733 show_log_view(struct got_object_id *start_id, struct got_repository *repo,
736 const struct got_error *err = NULL;
737 struct got_object_id *head_id = NULL;
738 int ch, done = 0, selected = 0, nfetched;
739 struct got_commit_graph *graph = NULL;
740 struct commit_queue commits;
741 struct commit_queue_entry *first_displayed_entry = NULL;
742 struct commit_queue_entry *last_displayed_entry = NULL;
743 struct commit_queue_entry *selected_entry = NULL;
744 char *in_repo_path = NULL;
745 struct tog_view *view = NULL;
747 err = got_repo_map_path(&in_repo_path, repo, path);
751 err = get_head_commit_id(&head_id, repo);
755 /* The graph contains all commits. */
756 err = got_commit_graph_open(&graph, head_id, 0, repo);
759 /* The commit queue contains a subset of commits filtered by path. */
760 TAILQ_INIT(&commits.head);
761 commits.ncommits = 0;
763 /* Populate commit graph with a sufficient number of commits. */
764 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
770 * Open the initial batch of commits, sorted in commit graph order.
771 * We keep all commits open throughout the lifetime of the log view
772 * in order to avoid having to re-fetch commits from disk while
773 * updating the display.
775 err = queue_commits(graph, &commits, start_id, LINES, 1, repo,
778 if (err->code != GOT_ERR_ITER_COMPLETED)
785 err = got_error_from_errno();
789 show_panel(view->panel);
791 first_displayed_entry = TAILQ_FIRST(&commits.head);
792 selected_entry = first_displayed_entry;
794 err = draw_commits(view, &last_displayed_entry, &selected_entry,
795 first_displayed_entry, &commits, selected, LINES,
796 graph, repo, in_repo_path);
800 nodelay(stdscr, FALSE);
801 ch = wgetch(view->window);
802 nodelay(stdscr, TRUE);
815 scroll_up(&first_displayed_entry, 1, &commits);
818 if (TAILQ_FIRST(&commits.head) ==
819 first_displayed_entry) {
823 scroll_up(&first_displayed_entry, LINES,
828 if (selected < MIN(LINES - 2,
829 commits.ncommits - 1)) {
833 err = scroll_down(&first_displayed_entry, 1,
834 last_displayed_entry, &commits, graph,
837 if (err->code != GOT_ERR_ITER_COMPLETED)
843 struct commit_queue_entry *first = first_displayed_entry;
844 err = scroll_down(&first_displayed_entry, LINES,
845 last_displayed_entry, &commits, graph,
848 if (err->code != GOT_ERR_ITER_COMPLETED)
850 /* can't scroll any further; move cursor down */
851 if (first == first_displayed_entry && selected <
852 MIN(LINES - 2, commits.ncommits - 1)) {
853 selected = MIN(LINES - 2,
854 commits.ncommits - 1);
861 if (selected > LINES - 2)
862 selected = LINES - 2;
863 if (selected > commits.ncommits - 1)
864 selected = commits.ncommits - 1;
868 err = show_commit(selected_entry, repo);
871 show_panel(view->panel);
874 err = browse_commit(selected_entry, repo);
877 show_panel(view->panel);
888 got_commit_graph_close(graph);
889 free_commits(&commits);
894 static const struct got_error *
895 cmd_log(int argc, char *argv[])
897 const struct got_error *error;
898 struct got_repository *repo = NULL;
899 struct got_object_id *start_id = NULL;
900 char *path = NULL, *repo_path = NULL, *cwd = NULL;
901 char *start_commit = NULL;
905 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
909 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
912 start_commit = optarg;
915 repo_path = realpath(optarg, NULL);
916 if (repo_path == NULL)
931 path = strdup(argv[0]);
935 return got_error_from_errno();
937 cwd = getcwd(NULL, 0);
939 error = got_error_from_errno();
942 if (repo_path == NULL) {
943 repo_path = strdup(cwd);
944 if (repo_path == NULL) {
945 error = got_error_from_errno();
950 error = got_repo_open(&repo, repo_path);
954 if (start_commit == NULL) {
955 error = get_head_commit_id(&start_id, repo);
959 struct got_object *obj;
960 error = got_object_open_by_id_str(&obj, repo, start_commit);
962 start_id = got_object_get_id(obj);
963 if (start_id == NULL)
964 error = got_error_from_errno();
971 error = show_log_view(start_id, repo, path);
978 got_repo_close(repo);
986 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
992 parse_next_line(FILE *f, size_t *len)
997 const char delim[3] = { '\0', '\0', '\0'};
999 line = fparseln(f, &linelen, &lineno, delim, 0);
1005 static const struct got_error *
1006 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
1007 int *last_displayed_line, int *eof, int max_lines)
1009 const struct got_error *err;
1010 int nlines = 0, nprinted = 0;
1020 while (nprinted < max_lines) {
1021 line = parse_next_line(f, &len);
1026 if (++nlines < *first_displayed_line) {
1031 err = format_line(&wline, &width, line, COLS);
1037 waddwstr(window, wline);
1039 waddch(window, '\n');
1040 if (++nprinted == 1)
1041 *first_displayed_line = nlines;
1046 *last_displayed_line = nlines;
1054 static const struct got_error *
1055 show_diff_view(struct tog_view *view, struct got_object *obj1,
1056 struct got_object *obj2, struct got_repository *repo)
1058 const struct got_error *err;
1060 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1063 if (obj1 != NULL && obj2 != NULL &&
1064 got_object_get_type(obj1) != got_object_get_type(obj2))
1065 return got_error(GOT_ERR_OBJ_TYPE);
1069 return got_error_from_errno();
1071 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1072 case GOT_OBJ_TYPE_BLOB:
1073 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1075 case GOT_OBJ_TYPE_TREE:
1076 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1078 case GOT_OBJ_TYPE_COMMIT:
1079 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1082 return got_error(GOT_ERR_OBJ_TYPE);
1087 show_panel(view->panel);
1090 err = draw_file(view->window, f, &first_displayed_line,
1091 &last_displayed_line, &eof, LINES);
1094 nodelay(stdscr, FALSE);
1095 ch = wgetch(view->window);
1096 nodelay(stdscr, TRUE);
1103 if (first_displayed_line > 1)
1104 first_displayed_line--;
1109 while (i++ < LINES - 1 &&
1110 first_displayed_line > 1)
1111 first_displayed_line--;
1116 first_displayed_line++;
1121 while (!eof && i++ < LINES - 1) {
1122 char *line = parse_next_line(f, NULL);
1123 first_displayed_line++;
1136 static const struct got_error *
1137 cmd_diff(int argc, char *argv[])
1139 const struct got_error *error = NULL;
1140 struct got_repository *repo = NULL;
1141 struct got_object *obj1 = NULL, *obj2 = NULL;
1142 char *repo_path = NULL;
1143 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1145 struct tog_view *view;
1148 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1152 while ((ch = getopt(argc, argv, "")) != -1) {
1164 usage_diff(); /* TODO show local worktree changes */
1165 } else if (argc == 2) {
1166 repo_path = getcwd(NULL, 0);
1167 if (repo_path == NULL)
1168 return got_error_from_errno();
1169 obj_id_str1 = argv[0];
1170 obj_id_str2 = argv[1];
1171 } else if (argc == 3) {
1172 repo_path = realpath(argv[0], NULL);
1173 if (repo_path == NULL)
1174 return got_error_from_errno();
1175 obj_id_str1 = argv[1];
1176 obj_id_str2 = argv[2];
1180 error = got_repo_open(&repo, repo_path);
1185 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1189 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1195 error = got_error_from_errno();
1198 error = show_diff_view(view, obj1, obj2, repo);
1201 got_repo_close(repo);
1203 got_object_close(obj1);
1205 got_object_close(obj2);
1213 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1218 struct tog_blame_line {
1220 struct got_object_id *id;
1223 static const struct got_error *
1224 draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1225 struct tog_blame_line *lines, int nlines, int blame_complete,
1226 int selected_line, int *first_displayed_line, int *last_displayed_line,
1227 int *eof, int max_lines)
1229 const struct got_error *err;
1230 int lineno = 0, nprinted = 0;
1235 struct tog_blame_line *blame_line;
1236 struct got_object_id *prev_id = NULL;
1239 err = got_object_id_str(&id_str, id);
1246 if (asprintf(&line, "commit: %s", id_str) == -1) {
1247 err = got_error_from_errno();
1252 err = format_line(&wline, &width, line, COLS);
1255 waddwstr(window, wline);
1259 waddch(window, '\n');
1261 if (asprintf(&line, "[%d/%d] %s%s",
1262 *first_displayed_line - 1 + selected_line, nlines,
1263 blame_complete ? "" : "annotating ", path) == -1) {
1265 return got_error_from_errno();
1268 err = format_line(&wline, &width, line, COLS);
1273 waddwstr(window, wline);
1277 waddch(window, '\n');
1280 while (nprinted < max_lines - 2) {
1281 line = parse_next_line(f, &len);
1286 if (++lineno < *first_displayed_line) {
1291 wlimit = COLS < 9 ? 0 : COLS - 9;
1292 err = format_line(&wline, &width, line, wlimit);
1298 if (nprinted == selected_line - 1)
1301 blame_line = &lines[lineno - 1];
1302 if (blame_line->annotated && prev_id &&
1303 got_object_id_cmp(prev_id, blame_line->id) == 0)
1304 waddstr(window, " ");
1305 else if (blame_line->annotated) {
1307 err = got_object_id_str(&id_str, blame_line->id);
1313 wprintw(window, "%.8s ", id_str);
1315 prev_id = blame_line->id;
1317 waddstr(window, "........ ");
1321 waddwstr(window, wline);
1322 while (width < wlimit) {
1323 waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1326 if (nprinted == selected_line - 1)
1328 if (++nprinted == 1)
1329 *first_displayed_line = lineno;
1334 *last_displayed_line = lineno;
1342 struct tog_blame_cb_args {
1343 pthread_mutex_t *mutex;
1344 struct tog_blame_line *lines; /* one per line */
1347 struct tog_view *view;
1348 struct got_object_id *commit_id;
1351 int *first_displayed_line;
1352 int *last_displayed_line;
1357 static const struct got_error *
1358 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1360 const struct got_error *err = NULL;
1361 struct tog_blame_cb_args *a = arg;
1362 struct tog_blame_line *line;
1365 if (nlines != a->nlines ||
1366 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1367 return got_error(GOT_ERR_RANGE);
1369 if (pthread_mutex_lock(a->mutex) != 0)
1370 return got_error_from_errno();
1372 if (*a->quit) { /* user has quit the blame view */
1373 err = got_error(GOT_ERR_ITER_COMPLETED);
1378 goto done; /* no change in this commit */
1380 line = &a->lines[lineno - 1];
1381 if (line->annotated)
1384 line->id = got_object_id_dup(id);
1385 if (line->id == NULL) {
1386 err = got_error_from_errno();
1389 line->annotated = 1;
1391 err = draw_blame(a->view->window, a->commit_id, a->f, a->path,
1392 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1393 a->last_displayed_line, &eof, LINES);
1395 if (pthread_mutex_unlock(a->mutex) != 0)
1396 return got_error_from_errno();
1400 struct tog_blame_thread_args {
1402 struct got_repository *repo;
1403 struct tog_blame_cb_args *cb_args;
1408 blame_thread(void *arg)
1410 const struct got_error *err;
1411 struct tog_blame_thread_args *ta = arg;
1412 struct tog_blame_cb_args *a = ta->cb_args;
1415 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1416 blame_cb, ta->cb_args);
1418 if (pthread_mutex_lock(a->mutex) != 0)
1419 return (void *)got_error_from_errno();
1421 got_repo_close(ta->repo);
1425 err = draw_blame(a->view->window, a->commit_id, a->f,
1426 a->path, a->lines, a->nlines, 1, *a->selected_line,
1427 a->first_displayed_line, a->last_displayed_line, &eof,
1430 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1431 err = got_error_from_errno();
1436 static struct got_object_id *
1437 get_selected_commit_id(struct tog_blame_line *lines,
1438 int first_displayed_line, int selected_line)
1440 struct tog_blame_line *line;
1442 line = &lines[first_displayed_line - 1 + selected_line - 1];
1443 if (!line->annotated)
1449 static const struct got_error *
1450 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1451 struct tog_blame_line *lines, int first_displayed_line,
1452 int selected_line, struct got_repository *repo)
1454 const struct got_error *err = NULL;
1455 struct got_commit_object *commit = NULL;
1456 struct got_object_id *selected_id;
1457 struct got_object_qid *pid;
1462 selected_id = get_selected_commit_id(lines,
1463 first_displayed_line, selected_line);
1464 if (selected_id == NULL)
1467 err = got_object_open(obj, repo, selected_id);
1471 err = got_object_commit_open(&commit, repo, *obj);
1475 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1477 err = got_object_open(pobj, repo, pid->id);
1483 got_object_commit_close(commit);
1490 struct tog_blame_line *lines;
1493 struct tog_blame_thread_args thread_args;
1494 struct tog_blame_cb_args cb_args;
1498 static const struct got_error *
1499 stop_blame(struct tog_blame *blame)
1501 const struct got_error *err = NULL;
1504 if (blame->thread) {
1505 if (pthread_join(blame->thread, (void **)&err) != 0)
1506 err = got_error_from_errno();
1507 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1509 blame->thread = NULL;
1511 if (blame->thread_args.repo) {
1512 got_repo_close(blame->thread_args.repo);
1513 blame->thread_args.repo = NULL;
1519 for (i = 0; i < blame->nlines; i++)
1520 free(blame->lines[i].id);
1522 blame->lines = NULL;
1523 free(blame->cb_args.commit_id);
1524 blame->cb_args.commit_id = NULL;
1529 static const struct got_error *
1530 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1531 struct tog_view *view, int *blame_complete,
1532 int *first_displayed_line, int *last_displayed_line,
1533 int *selected_line, int *done, const char *path,
1534 struct got_object_id *commit_id,
1535 struct got_repository *repo)
1537 const struct got_error *err = NULL;
1538 struct got_blob_object *blob = NULL;
1539 struct got_repository *thread_repo = NULL;
1540 struct got_object *obj;
1542 err = got_object_open_by_path(&obj, repo, commit_id, path);
1545 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1546 err = got_error(GOT_ERR_OBJ_TYPE);
1550 err = got_object_blob_open(&blob, repo, obj, 8192);
1553 blame->f = got_opentemp();
1554 if (blame->f == NULL) {
1555 err = got_error_from_errno();
1558 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1563 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1564 if (blame->lines == NULL) {
1565 err = got_error_from_errno();
1569 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1573 blame->cb_args.view = view;
1574 blame->cb_args.lines = blame->lines;
1575 blame->cb_args.nlines = blame->nlines;
1576 blame->cb_args.mutex = mutex;
1577 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1578 if (blame->cb_args.commit_id == NULL) {
1579 err = got_error_from_errno();
1582 blame->cb_args.f = blame->f;
1583 blame->cb_args.path = path;
1584 blame->cb_args.first_displayed_line = first_displayed_line;
1585 blame->cb_args.selected_line = selected_line;
1586 blame->cb_args.last_displayed_line = last_displayed_line;
1587 blame->cb_args.quit = done;
1589 blame->thread_args.path = path;
1590 blame->thread_args.repo = thread_repo;
1591 blame->thread_args.cb_args = &blame->cb_args;
1592 blame->thread_args.complete = blame_complete;
1593 *blame_complete = 0;
1595 if (pthread_create(&blame->thread, NULL, blame_thread,
1596 &blame->thread_args) != 0) {
1597 err = got_error_from_errno();
1603 got_object_blob_close(blob);
1605 got_object_close(obj);
1611 static const struct got_error *
1612 show_blame_view(const char *path, struct got_object_id *commit_id,
1613 struct got_repository *repo)
1615 const struct got_error *err = NULL, *thread_err = NULL;
1616 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1617 int selected_line = first_displayed_line;
1618 int eof, blame_complete = 0;
1619 struct got_object *obj = NULL, *pobj = NULL;
1620 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1621 struct tog_blame blame;
1622 struct got_object_id_queue blamed_commits;
1623 struct got_object_qid *blamed_commit = NULL;
1624 struct tog_view *view = NULL, *diff_view;
1626 SIMPLEQ_INIT(&blamed_commits);
1628 if (pthread_mutex_init(&mutex, NULL) != 0) {
1629 err = got_error_from_errno();
1633 err = got_object_qid_alloc(&blamed_commit, commit_id);
1636 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1640 err = got_error_from_errno();
1643 show_panel(view->panel);
1645 memset(&blame, 0, sizeof(blame));
1646 err = run_blame(&blame, &mutex, view, &blame_complete,
1647 &first_displayed_line, &last_displayed_line,
1648 &selected_line, &done, path, blamed_commit->id, repo);
1653 if (pthread_mutex_lock(&mutex) != 0) {
1654 err = got_error_from_errno();
1657 err = draw_blame(view->window, blamed_commit->id,
1658 blame.f, path, blame.lines, blame.nlines, blame_complete,
1659 selected_line, &first_displayed_line, &last_displayed_line,
1661 if (pthread_mutex_unlock(&mutex) != 0) {
1662 err = got_error_from_errno();
1667 nodelay(stdscr, FALSE);
1668 ch = wgetch(view->window);
1669 nodelay(stdscr, TRUE);
1670 if (pthread_mutex_lock(&mutex) != 0) {
1671 err = got_error_from_errno();
1680 if (selected_line > 1)
1682 else if (selected_line == 1 &&
1683 first_displayed_line > 1)
1684 first_displayed_line--;
1688 if (first_displayed_line == 1) {
1692 if (first_displayed_line > LINES - 2)
1693 first_displayed_line -= (LINES - 2);
1695 first_displayed_line = 1;
1699 if (selected_line < LINES - 2 &&
1700 first_displayed_line + selected_line <=
1703 else if (last_displayed_line < blame.nlines)
1704 first_displayed_line++;
1708 struct got_object_id *id;
1709 id = get_selected_commit_id(blame.lines,
1710 first_displayed_line, selected_line);
1711 if (id == NULL || got_object_id_cmp(id,
1712 blamed_commit->id) == 0)
1714 err = open_selected_commit(&pobj, &obj,
1715 blame.lines, first_displayed_line,
1716 selected_line, repo);
1719 if (pobj == NULL && obj == NULL)
1721 if (ch == 'p' && pobj == NULL)
1724 if (pthread_mutex_unlock(&mutex) != 0) {
1725 err = got_error_from_errno();
1728 thread_err = stop_blame(&blame);
1730 if (pthread_mutex_lock(&mutex) != 0) {
1731 err = got_error_from_errno();
1736 id = got_object_get_id(ch == 'b' ? obj : pobj);
1737 got_object_close(obj);
1740 got_object_close(pobj);
1744 err = got_error_from_errno();
1747 err = got_object_qid_alloc(&blamed_commit, id);
1751 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1752 blamed_commit, entry);
1753 err = run_blame(&blame, &mutex, view,
1754 &blame_complete, &first_displayed_line,
1755 &last_displayed_line, &selected_line,
1756 &done, path, blamed_commit->id, repo);
1762 struct got_object_qid *first;
1763 first = SIMPLEQ_FIRST(&blamed_commits);
1764 if (!got_object_id_cmp(first->id, commit_id))
1767 if (pthread_mutex_unlock(&mutex) != 0) {
1768 err = got_error_from_errno();
1771 thread_err = stop_blame(&blame);
1773 if (pthread_mutex_lock(&mutex) != 0) {
1774 err = got_error_from_errno();
1779 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1780 got_object_qid_free(blamed_commit);
1781 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1782 err = run_blame(&blame, &mutex, view,
1783 &blame_complete, &first_displayed_line,
1784 &last_displayed_line, &selected_line,
1785 &done, path, blamed_commit->id, repo);
1792 err = open_selected_commit(&pobj, &obj,
1793 blame.lines, first_displayed_line,
1794 selected_line, repo);
1797 if (pobj == NULL && obj == NULL)
1799 diff_view = open_view();
1800 if (diff_view == NULL) {
1801 err = got_error_from_errno();
1804 err = show_diff_view(diff_view, pobj, obj, repo);
1805 close_view(diff_view);
1807 got_object_close(pobj);
1810 got_object_close(obj);
1812 show_panel(view->panel);
1818 if (last_displayed_line >= blame.nlines &&
1819 selected_line < LINES - 2) {
1820 selected_line = MIN(blame.nlines,
1824 if (last_displayed_line + LINES - 2 <=
1826 first_displayed_line += LINES - 2;
1828 first_displayed_line =
1829 blame.nlines - (LINES - 3);
1834 if (pthread_mutex_unlock(&mutex) != 0)
1835 err = got_error_from_errno();
1836 if (err || thread_err)
1841 got_object_close(pobj);
1843 thread_err = stop_blame(&blame);
1846 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1847 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1848 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1849 got_object_qid_free(blamed_commit);
1851 return thread_err ? thread_err : err;
1854 static const struct got_error *
1855 cmd_blame(int argc, char *argv[])
1857 const struct got_error *error;
1858 struct got_repository *repo = NULL;
1859 char *repo_path = NULL;
1861 struct got_object_id *commit_id = NULL;
1862 char *commit_id_str = NULL;
1866 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1870 while ((ch = getopt(argc, argv, "c:")) != -1) {
1873 commit_id_str = optarg;
1886 } else if (argc == 1) {
1887 repo_path = getcwd(NULL, 0);
1888 if (repo_path == NULL)
1889 return got_error_from_errno();
1891 } else if (argc == 2) {
1892 repo_path = realpath(argv[0], NULL);
1893 if (repo_path == NULL)
1894 return got_error_from_errno();
1899 error = got_repo_open(&repo, repo_path);
1904 if (commit_id_str == NULL) {
1905 struct got_reference *head_ref;
1906 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1909 error = got_ref_resolve(&commit_id, repo, head_ref);
1910 got_ref_close(head_ref);
1912 struct got_object *obj;
1913 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1916 commit_id = got_object_get_id(obj);
1917 if (commit_id == NULL)
1918 error = got_error_from_errno();
1919 got_object_close(obj);
1924 error = show_blame_view(path, commit_id, repo);
1928 got_repo_close(repo);
1932 static const struct got_error *
1933 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1934 struct got_tree_entry **last_displayed_entry,
1935 struct got_tree_entry **selected_entry, int *ndisplayed,
1936 WINDOW *window, const char *label, int show_ids, const char *parent_path,
1937 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1939 const struct got_error *err = NULL;
1940 struct got_tree_entry *te;
1951 err = format_line(&wline, &width, label, COLS);
1954 waddwstr(window, wline);
1958 waddch(window, '\n');
1961 err = format_line(&wline, &width, parent_path, COLS);
1964 waddwstr(window, wline);
1968 waddch(window, '\n');
1971 waddch(window, '\n');
1975 te = SIMPLEQ_FIRST(&entries->head);
1976 if (*first_displayed_entry == NULL) {
1977 if (selected == 0) {
1979 *selected_entry = NULL;
1981 waddstr(window, " ..\n"); /* parent directory */
1990 while (te != *first_displayed_entry)
1991 te = SIMPLEQ_NEXT(te, entry);
1995 char *line = NULL, *id_str = NULL;
1998 err = got_object_id_str(&id_str, te->id);
2000 return got_error_from_errno();
2002 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2003 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2005 return got_error_from_errno();
2008 err = format_line(&wline, &width, line, COLS);
2013 if (n == selected) {
2015 *selected_entry = te;
2017 waddwstr(window, wline);
2019 waddch(window, '\n');
2027 *last_displayed_entry = te;
2030 te = SIMPLEQ_NEXT(te, entry);
2037 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2038 const struct got_tree_entries *entries, int isroot)
2040 struct got_tree_entry *te, *prev;
2043 if (*first_displayed_entry == NULL)
2046 te = SIMPLEQ_FIRST(&entries->head);
2047 if (*first_displayed_entry == te) {
2049 *first_displayed_entry = NULL;
2053 /* XXX this is stupid... switch to TAILQ? */
2054 for (i = 0; i < maxscroll; i++) {
2055 while (te != *first_displayed_entry) {
2057 te = SIMPLEQ_NEXT(te, entry);
2059 *first_displayed_entry = prev;
2060 te = SIMPLEQ_FIRST(&entries->head);
2062 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2063 *first_displayed_entry = NULL;
2067 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2068 struct got_tree_entry *last_displayed_entry,
2069 const struct got_tree_entries *entries)
2071 struct got_tree_entry *next;
2074 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2077 if (*first_displayed_entry)
2078 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2080 next = SIMPLEQ_FIRST(&entries->head);
2082 *first_displayed_entry = next;
2083 if (++n >= maxscroll)
2085 next = SIMPLEQ_NEXT(next, entry);
2089 struct tog_parent_tree {
2090 TAILQ_ENTRY(tog_parent_tree) entry;
2091 struct got_tree_object *tree;
2092 struct got_tree_entry *first_displayed_entry;
2093 struct got_tree_entry *selected_entry;
2097 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2099 static const struct got_error *
2100 tree_entry_path(char **path, struct tog_parent_trees *parents,
2101 struct got_tree_entry *te)
2103 const struct got_error *err = NULL;
2104 struct tog_parent_tree *pt;
2105 size_t len = 2; /* for leading slash and NUL */
2107 TAILQ_FOREACH(pt, parents, entry)
2108 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2110 len += strlen(te->name);
2112 *path = calloc(1, len);
2114 return got_error_from_errno();
2117 pt = TAILQ_LAST(parents, tog_parent_trees);
2119 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2120 err = got_error(GOT_ERR_NO_SPACE);
2123 if (strlcat(*path, "/", len) >= len) {
2124 err = got_error(GOT_ERR_NO_SPACE);
2127 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2130 if (strlcat(*path, te->name, len) >= len) {
2131 err = got_error(GOT_ERR_NO_SPACE);
2143 static const struct got_error *
2144 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2145 struct got_object_id *commit_id, struct got_repository *repo)
2147 const struct got_error *err = NULL;
2150 err = tree_entry_path(&path, parents, te);
2154 err = show_blame_view(path, commit_id, repo);
2159 static const struct got_error *
2160 log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2161 struct got_object_id *commit_id, struct got_repository *repo)
2163 const struct got_error *err = NULL;
2166 err = tree_entry_path(&path, parents, te);
2170 err = show_log_view(commit_id, repo, path);
2175 static const struct got_error *
2176 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2177 struct got_repository *repo)
2179 const struct got_error *err = NULL;
2180 int ch, done = 0, selected = 0, show_ids = 0;
2181 struct got_tree_object *tree = root;
2182 const struct got_tree_entries *entries;
2183 struct got_tree_entry *first_displayed_entry = NULL;
2184 struct got_tree_entry *last_displayed_entry = NULL;
2185 struct got_tree_entry *selected_entry = NULL;
2186 char *commit_id_str = NULL, *tree_label = NULL;
2187 int nentries, ndisplayed;
2188 struct tog_parent_trees parents;
2190 TAILQ_INIT(&parents);
2192 err = got_object_id_str(&commit_id_str, commit_id);
2196 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2197 err = got_error_from_errno();
2201 if (tog_tree_view.window == NULL) {
2202 tog_tree_view.window = newwin(0, 0, 0, 0);
2203 if (tog_tree_view.window == NULL)
2204 return got_error_from_errno();
2205 keypad(tog_tree_view.window, TRUE);
2207 if (tog_tree_view.panel == NULL) {
2208 tog_tree_view.panel = new_panel(tog_tree_view.window);
2209 if (tog_tree_view.panel == NULL)
2210 return got_error_from_errno();
2212 show_panel(tog_tree_view.panel);
2214 entries = got_object_tree_get_entries(root);
2215 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2218 entries = got_object_tree_get_entries(tree);
2219 nentries = entries->nentries;
2221 nentries++; /* '..' directory */
2223 err = tree_entry_path(&parent_path, &parents, NULL);
2227 err = draw_tree_entries(&first_displayed_entry,
2228 &last_displayed_entry, &selected_entry, &ndisplayed,
2229 tog_tree_view.window, tree_label, show_ids,
2230 parent_path, entries, selected, LINES, tree == root);
2235 nodelay(stdscr, FALSE);
2236 ch = wgetch(tog_tree_view.window);
2237 nodelay(stdscr, TRUE);
2243 show_ids = !show_ids;
2246 if (selected_entry) {
2247 err = log_tree_entry(selected_entry,
2248 &parents, commit_id, repo);
2259 tree_scroll_up(&first_displayed_entry, 1,
2260 entries, tree == root);
2263 if (SIMPLEQ_FIRST(&entries->head) ==
2264 first_displayed_entry) {
2266 first_displayed_entry = NULL;
2270 tree_scroll_up(&first_displayed_entry, LINES,
2271 entries, tree == root);
2275 if (selected < ndisplayed - 1) {
2279 tree_scroll_down(&first_displayed_entry, 1,
2280 last_displayed_entry, entries);
2283 tree_scroll_down(&first_displayed_entry, LINES,
2284 last_displayed_entry, entries);
2285 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2287 /* can't scroll any further; move cursor down */
2288 if (selected < ndisplayed - 1)
2289 selected = ndisplayed - 1;
2293 if (selected_entry == NULL) {
2294 struct tog_parent_tree *parent;
2296 /* user selected '..' */
2299 parent = TAILQ_FIRST(&parents);
2300 TAILQ_REMOVE(&parents, parent, entry);
2301 got_object_tree_close(tree);
2302 tree = parent->tree;
2303 first_displayed_entry =
2304 parent->first_displayed_entry;
2305 selected_entry = parent->selected_entry;
2306 selected = parent->selected;
2308 } else if (S_ISDIR(selected_entry->mode)) {
2309 struct tog_parent_tree *parent;
2310 struct got_tree_object *child;
2311 err = got_object_open_as_tree(
2312 &child, repo, selected_entry->id);
2315 parent = calloc(1, sizeof(*parent));
2316 if (parent == NULL) {
2317 err = got_error_from_errno();
2320 parent->tree = tree;
2321 parent->first_displayed_entry =
2322 first_displayed_entry;
2323 parent->selected_entry = selected_entry;
2324 parent->selected = selected;
2325 TAILQ_INSERT_HEAD(&parents, parent,
2329 first_displayed_entry = NULL;
2330 } else if (S_ISREG(selected_entry->mode)) {
2331 err = blame_tree_entry(selected_entry,
2332 &parents, commit_id, repo);
2338 if (selected > LINES)
2339 selected = ndisplayed - 1;
2347 free(commit_id_str);
2348 while (!TAILQ_EMPTY(&parents)) {
2349 struct tog_parent_tree *parent;
2350 parent = TAILQ_FIRST(&parents);
2351 TAILQ_REMOVE(&parents, parent, entry);
2356 got_object_tree_close(tree);
2364 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2369 static const struct got_error *
2370 cmd_tree(int argc, char *argv[])
2372 const struct got_error *error;
2373 struct got_repository *repo = NULL;
2374 char *repo_path = NULL;
2375 struct got_object_id *commit_id = NULL;
2376 char *commit_id_arg = NULL;
2377 struct got_commit_object *commit = NULL;
2378 struct got_tree_object *tree = NULL;
2382 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2386 while ((ch = getopt(argc, argv, "c:")) != -1) {
2389 commit_id_arg = optarg;
2401 repo_path = getcwd(NULL, 0);
2402 if (repo_path == NULL)
2403 return got_error_from_errno();
2404 } else if (argc == 1) {
2405 repo_path = realpath(argv[0], NULL);
2406 if (repo_path == NULL)
2407 return got_error_from_errno();
2411 error = got_repo_open(&repo, repo_path);
2416 if (commit_id_arg == NULL) {
2417 error = get_head_commit_id(&commit_id, repo);
2421 struct got_object *obj;
2422 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2423 if (error == NULL) {
2424 commit_id = got_object_get_id(obj);
2425 if (commit_id == NULL)
2426 error = got_error_from_errno();
2432 error = got_object_open_as_commit(&commit, repo, commit_id);
2436 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2440 error = show_tree_view(tree, commit_id, repo);
2444 got_object_commit_close(commit);
2446 got_object_tree_close(tree);
2448 got_repo_close(repo);
2458 intrflush(stdscr, FALSE);
2459 keypad(stdscr, TRUE);
2468 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2469 "Available commands:\n", getprogname());
2470 for (i = 0; i < nitems(tog_commands); i++) {
2471 struct tog_cmd *cmd = &tog_commands[i];
2472 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2478 make_argv(const char *arg0, const char *arg1)
2481 int argc = (arg1 == NULL ? 1 : 2);
2483 argv = calloc(argc, sizeof(char *));
2486 argv[0] = strdup(arg0);
2487 if (argv[0] == NULL)
2490 argv[1] = strdup(arg1);
2491 if (argv[1] == NULL)
2499 main(int argc, char *argv[])
2501 const struct got_error *error = NULL;
2502 struct tog_cmd *cmd = NULL;
2504 char **cmd_argv = NULL;
2506 setlocale(LC_ALL, "");
2508 while ((ch = getopt(argc, argv, "h")) != -1) {
2527 /* Build an argument vector which runs a default command. */
2528 cmd = &tog_commands[0];
2529 cmd_argv = make_argv(cmd->name, NULL);
2534 /* Did the user specific a command? */
2535 for (i = 0; i < nitems(tog_commands); i++) {
2536 if (strncmp(tog_commands[i].name, argv[0],
2537 strlen(argv[0])) == 0) {
2538 cmd = &tog_commands[i];
2540 tog_commands[i].cmd_usage();
2545 /* Did the user specify a repository? */
2546 char *repo_path = realpath(argv[0], NULL);
2548 struct got_repository *repo;
2549 error = got_repo_open(&repo, repo_path);
2551 got_repo_close(repo);
2553 error = got_error_from_errno();
2556 fprintf(stderr, "%s: '%s' is not a "
2557 "known command\n", getprogname(),
2561 fprintf(stderr, "%s: '%s' is neither a known "
2562 "command nor a path to a repository\n",
2563 getprogname(), argv[0]);
2567 cmd = &tog_commands[0];
2568 cmd_argv = make_argv(cmd->name, repo_path);
2576 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2583 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);