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 {
88 } tog_log_view, tog_diff_view, tog_blame_view, tog_tree_view;
90 static const struct got_error *
91 show_diff_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 *);
105 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
110 /* Create newly allocated wide-character string equivalent to a byte string. */
111 static const struct got_error *
112 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
115 const struct got_error *err = NULL;
118 *wlen = mbstowcs(NULL, s, 0);
119 if (*wlen == (size_t)-1) {
122 return got_error_from_errno();
124 /* byte string invalid in current encoding; try to "fix" it */
125 err = got_mbsavis(&vis, &vislen, s);
128 *wlen = mbstowcs(NULL, vis, 0);
129 if (*wlen == (size_t)-1) {
130 err = got_error_from_errno(); /* give up */
135 *ws = calloc(*wlen + 1, sizeof(*ws));
137 err = got_error_from_errno();
141 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
142 err = got_error_from_errno();
153 /* Format a line for display, ensuring that it won't overflow a width limit. */
154 static const struct got_error *
155 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
157 const struct got_error *err = NULL;
159 wchar_t *wline = NULL;
166 err = mbs2ws(&wline, &wlen, line);
171 while (i < wlen && cols < wlimit) {
172 int width = wcwidth(wline[i]);
179 if (cols + width <= wlimit) {
185 if (wline[i] == L'\t')
186 cols += TABSIZE - ((cols + 1) % TABSIZE);
190 err = got_error_from_errno();
205 static const struct got_error *
206 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
208 const struct got_error *err = NULL;
209 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
210 char *logmsg0 = NULL, *logmsg = NULL;
211 char *author0 = NULL, *author = NULL;
212 wchar_t *wlogmsg = NULL, *wauthor = NULL;
213 int author_width, logmsg_width;
214 char *newline, *smallerthan;
217 static const size_t date_display_cols = 9;
218 static const size_t author_display_cols = 16;
219 const int avail = COLS;
221 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
223 return got_error(GOT_ERR_NO_SPACE);
225 if (avail < date_display_cols)
226 limit = MIN(sizeof(datebuf) - 1, avail);
228 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
229 waddnstr(tog_log_view.window, datebuf, limit);
234 author0 = strdup(commit->author);
235 if (author0 == NULL) {
236 err = got_error_from_errno();
240 smallerthan = strchr(author, '<');
244 char *at = strchr(author, '@');
249 err = format_line(&wauthor, &author_width, author, limit);
252 waddwstr(tog_log_view.window, wauthor);
254 while (col <= avail && author_width < author_display_cols + 1) {
255 waddch(tog_log_view.window, ' ');
262 logmsg0 = strdup(commit->logmsg);
263 if (logmsg0 == NULL) {
264 err = got_error_from_errno();
268 while (*logmsg == '\n')
270 newline = strchr(logmsg, '\n');
274 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
277 waddwstr(tog_log_view.window, wlogmsg);
279 while (col <= avail) {
280 waddch(tog_log_view.window, ' ');
292 struct commit_queue_entry {
293 TAILQ_ENTRY(commit_queue_entry) entry;
294 struct got_object_id *id;
295 struct got_commit_object *commit;
297 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
298 struct commit_queue {
300 struct commit_queue_head head;
303 static struct commit_queue_entry *
304 alloc_commit_queue_entry(struct got_commit_object *commit,
305 struct got_object_id *id)
307 struct commit_queue_entry *entry;
309 entry = calloc(1, sizeof(*entry));
314 entry->commit = commit;
319 pop_commit(struct commit_queue *commits)
321 struct commit_queue_entry *entry;
323 entry = TAILQ_FIRST(&commits->head);
324 TAILQ_REMOVE(&commits->head, entry, entry);
325 got_object_commit_close(entry->commit);
327 /* Don't free entry->id! It is owned by the commit graph. */
332 free_commits(struct commit_queue *commits)
334 while (!TAILQ_EMPTY(&commits->head))
338 static const struct got_error *
339 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
340 struct got_object_id *start_id, int minqueue, int init,
341 struct got_repository *repo, const char *path)
343 const struct got_error *err = NULL;
344 struct got_object_id *id;
345 struct commit_queue_entry *entry;
346 int nfetched, nqueued = 0, found_obj = 0;
348 err = got_commit_graph_iter_start(graph, start_id);
352 entry = TAILQ_LAST(&commits->head, commit_queue_head);
353 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
356 /* Start ID's commit is already on the queue; skip over it. */
357 err = got_commit_graph_iter_next(&id, graph);
358 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
361 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
367 struct got_commit_object *commit;
369 err = got_commit_graph_iter_next(&id, graph);
371 if (err->code != GOT_ERR_ITER_NEED_MORE)
373 if (nqueued >= minqueue) {
377 err = got_commit_graph_fetch_commits(&nfetched,
386 err = got_object_open_as_commit(&commit, repo, id);
391 struct got_object *obj;
392 struct got_object_qid *pid;
395 err = got_object_open_by_path(&obj, repo, id, path);
397 if (err->code == GOT_ERR_NO_OBJ &&
398 (found_obj || !init)) {
399 /* History stops here. */
400 err = got_error(GOT_ERR_ITER_COMPLETED);
406 pid = SIMPLEQ_FIRST(&commit->parent_ids);
408 struct got_object *pobj;
409 err = got_object_open_by_path(&pobj, repo,
412 if (err->code != GOT_ERR_NO_OBJ) {
413 got_object_close(obj);
419 changed = (got_object_id_cmp(
420 got_object_get_id(obj),
421 got_object_get_id(pobj)) != 0);
422 got_object_close(pobj);
426 got_object_commit_close(commit);
431 entry = alloc_commit_queue_entry(commit, id);
433 err = got_error_from_errno();
436 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
444 static const struct got_error *
445 fetch_next_commit(struct commit_queue_entry **pentry,
446 struct commit_queue_entry *entry, struct commit_queue *commits,
447 struct got_commit_graph *graph, struct got_repository *repo,
450 const struct got_error *err = NULL;
454 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
458 /* Next entry to display should now be available. */
459 *pentry = TAILQ_NEXT(entry, entry);
461 return got_error(GOT_ERR_NO_OBJ);
466 static const struct got_error *
467 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
469 const struct got_error *err = NULL;
470 struct got_reference *head_ref;
474 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
478 err = got_ref_resolve(head_id, repo, head_ref);
479 got_ref_close(head_ref);
488 static const struct got_error *
489 draw_commits(struct commit_queue_entry **last,
490 struct commit_queue_entry **selected, struct commit_queue_entry *first,
491 struct commit_queue *commits, int selected_idx, int limit,
492 struct got_commit_graph *graph, struct got_repository *repo,
495 const struct got_error *err = NULL;
496 struct commit_queue_entry *entry;
498 char *id_str, *header;
504 if (ncommits == selected_idx) {
508 entry = TAILQ_NEXT(entry, entry);
512 err = got_object_id_str(&id_str, (*selected)->id);
517 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
518 err = got_error_from_errno();
522 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
523 err = got_error_from_errno();
528 err = format_line(&wline, &width, header, COLS);
535 werase(tog_log_view.window);
537 waddwstr(tog_log_view.window, wline);
539 waddch(tog_log_view.window, '\n');
548 if (ncommits >= limit - 1)
550 if (ncommits == selected_idx)
551 wstandout(tog_log_view.window);
552 err = draw_commit(entry->commit, entry->id);
553 if (ncommits == selected_idx)
554 wstandend(tog_log_view.window);
559 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
560 err = queue_commits(graph, commits, entry->id, 1,
563 if (err->code != GOT_ERR_ITER_COMPLETED)
568 entry = TAILQ_NEXT(entry, entry);
578 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
579 struct commit_queue *commits)
581 struct commit_queue_entry *entry;
584 entry = TAILQ_FIRST(&commits->head);
585 if (*first_displayed_entry == entry)
588 entry = *first_displayed_entry;
589 while (entry && nscrolled < maxscroll) {
590 entry = TAILQ_PREV(entry, commit_queue_head, entry);
592 *first_displayed_entry = entry;
598 static const struct got_error *
599 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
600 struct commit_queue_entry *last_displayed_entry,
601 struct commit_queue *commits, struct got_commit_graph *graph,
602 struct got_repository *repo, const char *path)
604 const struct got_error *err = NULL;
605 struct commit_queue_entry *pentry;
609 pentry = TAILQ_NEXT(last_displayed_entry, entry);
610 if (pentry == NULL) {
611 err = fetch_next_commit(&pentry, last_displayed_entry,
612 commits, graph, repo, path);
613 if (err || pentry == NULL)
616 last_displayed_entry = pentry;
618 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
621 *first_displayed_entry = pentry;
622 } while (++nscrolled < maxscroll);
627 static const struct got_error *
628 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
630 const struct got_error *err;
631 struct got_object *obj1 = NULL, *obj2 = NULL;
632 struct got_object_qid *parent_id;
634 err = got_object_open(&obj2, repo, entry->id);
638 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
640 err = got_object_open(&obj1, repo, parent_id->id);
645 err = show_diff_view(obj1, obj2, repo);
648 got_object_close(obj1);
650 got_object_close(obj2);
654 static const struct got_error *
655 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
657 const struct got_error *err = NULL;
658 struct got_tree_object *tree;
660 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
664 err = show_tree_view(tree, entry->id, repo);
665 got_object_tree_close(tree);
669 static const struct got_error *
670 show_log_view(struct got_object_id *start_id, struct got_repository *repo,
673 const struct got_error *err = NULL;
674 struct got_object_id *head_id = NULL;
675 int ch, done = 0, selected = 0, nfetched;
676 struct got_commit_graph *graph = NULL;
677 struct commit_queue commits;
678 struct commit_queue_entry *first_displayed_entry = NULL;
679 struct commit_queue_entry *last_displayed_entry = NULL;
680 struct commit_queue_entry *selected_entry = NULL;
681 char *in_repo_path = NULL;
683 err = got_repo_map_path(&in_repo_path, repo, path);
687 if (tog_log_view.window == NULL) {
688 tog_log_view.window = newwin(0, 0, 0, 0);
689 if (tog_log_view.window == NULL)
690 return got_error_from_errno();
691 keypad(tog_log_view.window, TRUE);
693 if (tog_log_view.panel == NULL) {
694 tog_log_view.panel = new_panel(tog_log_view.window);
695 if (tog_log_view.panel == NULL)
696 return got_error_from_errno();
698 show_panel(tog_log_view.panel);
700 err = get_head_commit_id(&head_id, repo);
704 /* The graph contains all commits. */
705 err = got_commit_graph_open(&graph, head_id, 0, repo);
708 /* The commit queue contains a subset of commits filtered by path. */
709 TAILQ_INIT(&commits.head);
710 commits.ncommits = 0;
712 /* Populate commit graph with a sufficient number of commits. */
713 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
719 * Open the initial batch of commits, sorted in commit graph order.
720 * We keep all commits open throughout the lifetime of the log view
721 * in order to avoid having to re-fetch commits from disk while
722 * updating the display.
724 err = queue_commits(graph, &commits, start_id, LINES, 1, repo,
727 if (err->code != GOT_ERR_ITER_COMPLETED)
732 first_displayed_entry = TAILQ_FIRST(&commits.head);
733 selected_entry = first_displayed_entry;
735 err = draw_commits(&last_displayed_entry, &selected_entry,
736 first_displayed_entry, &commits, selected, LINES,
737 graph, repo, in_repo_path);
741 nodelay(stdscr, FALSE);
742 ch = wgetch(tog_log_view.window);
743 nodelay(stdscr, TRUE);
756 scroll_up(&first_displayed_entry, 1, &commits);
759 if (TAILQ_FIRST(&commits.head) ==
760 first_displayed_entry) {
764 scroll_up(&first_displayed_entry, LINES,
769 if (selected < MIN(LINES - 2,
770 commits.ncommits - 1)) {
774 err = scroll_down(&first_displayed_entry, 1,
775 last_displayed_entry, &commits, graph,
778 if (err->code != GOT_ERR_ITER_COMPLETED)
784 struct commit_queue_entry *first = first_displayed_entry;
785 err = scroll_down(&first_displayed_entry, LINES,
786 last_displayed_entry, &commits, graph,
789 if (err->code != GOT_ERR_ITER_COMPLETED)
791 /* can't scroll any further; move cursor down */
792 if (first == first_displayed_entry && selected <
793 MIN(LINES - 2, commits.ncommits - 1)) {
794 selected = MIN(LINES - 2,
795 commits.ncommits - 1);
802 if (selected > LINES - 2)
803 selected = LINES - 2;
804 if (selected > commits.ncommits - 1)
805 selected = commits.ncommits - 1;
809 err = show_commit(selected_entry, repo);
812 show_panel(tog_log_view.panel);
815 err = browse_commit(selected_entry, repo);
818 show_panel(tog_log_view.panel);
827 got_commit_graph_close(graph);
828 free_commits(&commits);
833 static const struct got_error *
834 cmd_log(int argc, char *argv[])
836 const struct got_error *error;
837 struct got_repository *repo = NULL;
838 struct got_object_id *start_id = NULL;
839 char *path = NULL, *repo_path = NULL, *cwd = NULL;
840 char *start_commit = NULL;
844 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
848 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
851 start_commit = optarg;
854 repo_path = realpath(optarg, NULL);
855 if (repo_path == NULL)
870 path = strdup(argv[0]);
874 return got_error_from_errno();
876 cwd = getcwd(NULL, 0);
878 error = got_error_from_errno();
881 if (repo_path == NULL) {
882 repo_path = strdup(cwd);
883 if (repo_path == NULL) {
884 error = got_error_from_errno();
889 error = got_repo_open(&repo, repo_path);
893 if (start_commit == NULL) {
894 error = get_head_commit_id(&start_id, repo);
898 struct got_object *obj;
899 error = got_object_open_by_id_str(&obj, repo, start_commit);
901 start_id = got_object_get_id(obj);
902 if (start_id == NULL)
903 error = got_error_from_errno();
910 error = show_log_view(start_id, repo, path);
917 got_repo_close(repo);
925 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
931 parse_next_line(FILE *f, size_t *len)
936 const char delim[3] = { '\0', '\0', '\0'};
938 line = fparseln(f, &linelen, &lineno, delim, 0);
944 static const struct got_error *
945 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
946 int *last_displayed_line, int *eof, int max_lines)
948 const struct got_error *err;
949 int nlines = 0, nprinted = 0;
959 while (nprinted < max_lines) {
960 line = parse_next_line(f, &len);
965 if (++nlines < *first_displayed_line) {
970 err = format_line(&wline, &width, line, COLS);
976 waddwstr(window, wline);
978 waddch(window, '\n');
980 *first_displayed_line = nlines;
985 *last_displayed_line = nlines;
993 static const struct got_error *
994 show_diff_view(struct got_object *obj1, struct got_object *obj2,
995 struct got_repository *repo)
997 const struct got_error *err;
999 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1002 if (obj1 != NULL && obj2 != NULL &&
1003 got_object_get_type(obj1) != got_object_get_type(obj2))
1004 return got_error(GOT_ERR_OBJ_TYPE);
1008 return got_error_from_errno();
1010 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1011 case GOT_OBJ_TYPE_BLOB:
1012 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1014 case GOT_OBJ_TYPE_TREE:
1015 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1017 case GOT_OBJ_TYPE_COMMIT:
1018 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1021 return got_error(GOT_ERR_OBJ_TYPE);
1026 if (tog_diff_view.window == NULL) {
1027 tog_diff_view.window = newwin(0, 0, 0, 0);
1028 if (tog_diff_view.window == NULL)
1029 return got_error_from_errno();
1030 keypad(tog_diff_view.window, TRUE);
1032 if (tog_diff_view.panel == NULL) {
1033 tog_diff_view.panel = new_panel(tog_diff_view.window);
1034 if (tog_diff_view.panel == NULL)
1035 return got_error_from_errno();
1037 show_panel(tog_diff_view.panel);
1040 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
1041 &last_displayed_line, &eof, LINES);
1044 nodelay(stdscr, FALSE);
1045 ch = wgetch(tog_diff_view.window);
1046 nodelay(stdscr, TRUE);
1053 if (first_displayed_line > 1)
1054 first_displayed_line--;
1059 while (i++ < LINES - 1 &&
1060 first_displayed_line > 1)
1061 first_displayed_line--;
1066 first_displayed_line++;
1071 while (!eof && i++ < LINES - 1) {
1072 char *line = parse_next_line(f, NULL);
1073 first_displayed_line++;
1086 static const struct got_error *
1087 cmd_diff(int argc, char *argv[])
1089 const struct got_error *error = NULL;
1090 struct got_repository *repo = NULL;
1091 struct got_object *obj1 = NULL, *obj2 = NULL;
1092 char *repo_path = NULL;
1093 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1097 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1101 while ((ch = getopt(argc, argv, "")) != -1) {
1113 usage_diff(); /* TODO show local worktree changes */
1114 } else if (argc == 2) {
1115 repo_path = getcwd(NULL, 0);
1116 if (repo_path == NULL)
1117 return got_error_from_errno();
1118 obj_id_str1 = argv[0];
1119 obj_id_str2 = argv[1];
1120 } else if (argc == 3) {
1121 repo_path = realpath(argv[0], NULL);
1122 if (repo_path == NULL)
1123 return got_error_from_errno();
1124 obj_id_str1 = argv[1];
1125 obj_id_str2 = argv[2];
1129 error = got_repo_open(&repo, repo_path);
1134 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1138 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1142 error = show_diff_view(obj1, obj2, repo);
1144 got_repo_close(repo);
1146 got_object_close(obj1);
1148 got_object_close(obj2);
1156 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1161 struct tog_blame_line {
1163 struct got_object_id *id;
1166 static const struct got_error *
1167 draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1168 struct tog_blame_line *lines, int nlines, int blame_complete,
1169 int selected_line, int *first_displayed_line, int *last_displayed_line,
1170 int *eof, int max_lines)
1172 const struct got_error *err;
1173 int lineno = 0, nprinted = 0;
1178 struct tog_blame_line *blame_line;
1179 struct got_object_id *prev_id = NULL;
1182 err = got_object_id_str(&id_str, id);
1189 if (asprintf(&line, "commit: %s", id_str) == -1) {
1190 err = got_error_from_errno();
1195 err = format_line(&wline, &width, line, COLS);
1198 waddwstr(window, wline);
1202 waddch(window, '\n');
1204 if (asprintf(&line, "[%d/%d] %s%s",
1205 *first_displayed_line - 1 + selected_line, nlines,
1206 blame_complete ? "" : "annotating ", path) == -1) {
1208 return got_error_from_errno();
1211 err = format_line(&wline, &width, line, COLS);
1216 waddwstr(window, wline);
1220 waddch(window, '\n');
1223 while (nprinted < max_lines - 2) {
1224 line = parse_next_line(f, &len);
1229 if (++lineno < *first_displayed_line) {
1234 wlimit = COLS < 9 ? 0 : COLS - 9;
1235 err = format_line(&wline, &width, line, wlimit);
1241 if (nprinted == selected_line - 1)
1244 blame_line = &lines[lineno - 1];
1245 if (blame_line->annotated && prev_id &&
1246 got_object_id_cmp(prev_id, blame_line->id) == 0)
1247 waddstr(window, " ");
1248 else if (blame_line->annotated) {
1250 err = got_object_id_str(&id_str, blame_line->id);
1256 wprintw(window, "%.8s ", id_str);
1258 prev_id = blame_line->id;
1260 waddstr(window, "........ ");
1264 waddwstr(window, wline);
1265 while (width < wlimit) {
1266 waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1269 if (nprinted == selected_line - 1)
1271 if (++nprinted == 1)
1272 *first_displayed_line = lineno;
1277 *last_displayed_line = lineno;
1285 struct tog_blame_cb_args {
1286 pthread_mutex_t *mutex;
1287 struct tog_blame_line *lines; /* one per line */
1290 struct got_object_id *commit_id;
1293 int *first_displayed_line;
1294 int *last_displayed_line;
1299 static const struct got_error *
1300 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1302 const struct got_error *err = NULL;
1303 struct tog_blame_cb_args *a = arg;
1304 struct tog_blame_line *line;
1307 if (nlines != a->nlines ||
1308 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1309 return got_error(GOT_ERR_RANGE);
1311 if (pthread_mutex_lock(a->mutex) != 0)
1312 return got_error_from_errno();
1314 if (*a->quit) { /* user has quit the blame view */
1315 err = got_error(GOT_ERR_ITER_COMPLETED);
1320 goto done; /* no change in this commit */
1322 line = &a->lines[lineno - 1];
1323 if (line->annotated)
1326 line->id = got_object_id_dup(id);
1327 if (line->id == NULL) {
1328 err = got_error_from_errno();
1331 line->annotated = 1;
1333 err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1334 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1335 a->last_displayed_line, &eof, LINES);
1337 if (pthread_mutex_unlock(a->mutex) != 0)
1338 return got_error_from_errno();
1342 struct tog_blame_thread_args {
1344 struct got_repository *repo;
1345 struct tog_blame_cb_args *cb_args;
1350 blame_thread(void *arg)
1352 const struct got_error *err;
1353 struct tog_blame_thread_args *ta = arg;
1354 struct tog_blame_cb_args *a = ta->cb_args;
1357 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1358 blame_cb, ta->cb_args);
1360 if (pthread_mutex_lock(a->mutex) != 0)
1361 return (void *)got_error_from_errno();
1363 got_repo_close(ta->repo);
1367 err = draw_blame(tog_blame_view.window, a->commit_id, a->f,
1368 a->path, a->lines, a->nlines, 1, *a->selected_line,
1369 a->first_displayed_line, a->last_displayed_line, &eof,
1372 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1373 err = got_error_from_errno();
1378 static struct got_object_id *
1379 get_selected_commit_id(struct tog_blame_line *lines,
1380 int first_displayed_line, int selected_line)
1382 struct tog_blame_line *line;
1384 line = &lines[first_displayed_line - 1 + selected_line - 1];
1385 if (!line->annotated)
1391 static const struct got_error *
1392 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1393 struct tog_blame_line *lines, int first_displayed_line,
1394 int selected_line, struct got_repository *repo)
1396 const struct got_error *err = NULL;
1397 struct got_commit_object *commit = NULL;
1398 struct got_object_id *selected_id;
1399 struct got_object_qid *pid;
1404 selected_id = get_selected_commit_id(lines,
1405 first_displayed_line, selected_line);
1406 if (selected_id == NULL)
1409 err = got_object_open(obj, repo, selected_id);
1413 err = got_object_commit_open(&commit, repo, *obj);
1417 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1419 err = got_object_open(pobj, repo, pid->id);
1425 got_object_commit_close(commit);
1432 struct tog_blame_line *lines;
1435 struct tog_blame_thread_args thread_args;
1436 struct tog_blame_cb_args cb_args;
1438 struct got_object_id *commit_id;
1441 static const struct got_error *
1442 stop_blame(struct tog_blame *blame)
1444 const struct got_error *err = NULL;
1447 if (blame->thread) {
1448 if (pthread_join(blame->thread, (void **)&err) != 0)
1449 err = got_error_from_errno();
1450 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1452 blame->thread = NULL;
1454 if (blame->thread_args.repo) {
1455 got_repo_close(blame->thread_args.repo);
1456 blame->thread_args.repo = NULL;
1462 for (i = 0; i < blame->nlines; i++)
1463 free(blame->lines[i].id);
1465 blame->lines = NULL;
1466 free(blame->commit_id);
1467 blame->commit_id = NULL;
1472 static const struct got_error *
1473 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex, int *blame_complete,
1474 int *first_displayed_line, int *last_displayed_line,
1475 int *selected_line, int *done, const char *path,
1476 struct got_object_id *commit_id,
1477 struct got_repository *repo)
1479 const struct got_error *err = NULL;
1480 struct got_blob_object *blob = NULL;
1481 struct got_repository *thread_repo = NULL;
1482 struct got_object *obj;
1484 err = got_object_open_by_path(&obj, repo, commit_id, path);
1487 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1488 err = got_error(GOT_ERR_OBJ_TYPE);
1492 err = got_object_blob_open(&blob, repo, obj, 8192);
1495 blame->f = got_opentemp();
1496 if (blame->f == NULL) {
1497 err = got_error_from_errno();
1500 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1505 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1506 if (blame->lines == NULL) {
1507 err = got_error_from_errno();
1511 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1515 blame->cb_args.lines = blame->lines;
1516 blame->cb_args.nlines = blame->nlines;
1517 blame->cb_args.mutex = mutex;
1518 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1519 if (blame->cb_args.commit_id == NULL) {
1520 err = got_error_from_errno();
1523 blame->cb_args.f = blame->f;
1524 blame->cb_args.path = path;
1525 blame->cb_args.first_displayed_line = first_displayed_line;
1526 blame->cb_args.selected_line = selected_line;
1527 blame->cb_args.last_displayed_line = last_displayed_line;
1528 blame->cb_args.quit = done;
1530 blame->thread_args.path = path;
1531 blame->thread_args.repo = thread_repo;
1532 blame->thread_args.cb_args = &blame->cb_args;
1533 blame->thread_args.complete = blame_complete;
1534 *blame_complete = 0;
1536 if (pthread_create(&blame->thread, NULL, blame_thread,
1537 &blame->thread_args) != 0) {
1538 err = got_error_from_errno();
1544 got_object_blob_close(blob);
1546 got_object_close(obj);
1552 static const struct got_error *
1553 show_blame_view(const char *path, struct got_object_id *commit_id,
1554 struct got_repository *repo)
1556 const struct got_error *err = NULL, *thread_err = NULL;
1557 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1558 int selected_line = first_displayed_line;
1559 int eof, blame_complete = 0;
1560 struct got_object *obj = NULL, *pobj = NULL;
1561 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1562 struct tog_blame blame;
1563 struct got_object_id_queue blamed_commits;
1564 struct got_object_qid *blamed_commit = NULL;
1566 SIMPLEQ_INIT(&blamed_commits);
1568 if (pthread_mutex_init(&mutex, NULL) != 0) {
1569 err = got_error_from_errno();
1573 err = got_object_qid_alloc(&blamed_commit, commit_id);
1576 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1578 if (tog_blame_view.window == NULL) {
1579 tog_blame_view.window = newwin(0, 0, 0, 0);
1580 if (tog_blame_view.window == NULL)
1581 return got_error_from_errno();
1582 keypad(tog_blame_view.window, TRUE);
1584 if (tog_blame_view.panel == NULL) {
1585 tog_blame_view.panel = new_panel(tog_blame_view.window);
1586 if (tog_blame_view.panel == NULL)
1587 return got_error_from_errno();
1589 show_panel(tog_blame_view.panel);
1591 memset(&blame, 0, sizeof(blame));
1592 err = run_blame(&blame, &mutex, &blame_complete,
1593 &first_displayed_line, &last_displayed_line,
1594 &selected_line, &done, path, blamed_commit->id, repo);
1599 if (pthread_mutex_lock(&mutex) != 0) {
1600 err = got_error_from_errno();
1603 err = draw_blame(tog_blame_view.window, blamed_commit->id,
1604 blame.f, path, blame.lines, blame.nlines, blame_complete,
1605 selected_line, &first_displayed_line, &last_displayed_line,
1607 if (pthread_mutex_unlock(&mutex) != 0) {
1608 err = got_error_from_errno();
1613 nodelay(stdscr, FALSE);
1614 ch = wgetch(tog_blame_view.window);
1615 nodelay(stdscr, TRUE);
1616 if (pthread_mutex_lock(&mutex) != 0) {
1617 err = got_error_from_errno();
1626 if (selected_line > 1)
1628 else if (selected_line == 1 &&
1629 first_displayed_line > 1)
1630 first_displayed_line--;
1634 if (first_displayed_line == 1) {
1638 if (first_displayed_line > LINES - 2)
1639 first_displayed_line -= (LINES - 2);
1641 first_displayed_line = 1;
1645 if (selected_line < LINES - 2 &&
1646 first_displayed_line + selected_line <=
1649 else if (last_displayed_line < blame.nlines)
1650 first_displayed_line++;
1654 struct got_object_id *id;
1655 id = get_selected_commit_id(blame.lines,
1656 first_displayed_line, selected_line);
1657 if (id == NULL || got_object_id_cmp(id,
1658 blamed_commit->id) == 0)
1660 err = open_selected_commit(&pobj, &obj,
1661 blame.lines, first_displayed_line,
1662 selected_line, repo);
1665 if (pobj == NULL && obj == NULL)
1667 if (ch == 'p' && pobj == NULL)
1670 if (pthread_mutex_unlock(&mutex) != 0) {
1671 err = got_error_from_errno();
1674 thread_err = stop_blame(&blame);
1676 if (pthread_mutex_lock(&mutex) != 0) {
1677 err = got_error_from_errno();
1682 id = got_object_get_id(ch == 'b' ? obj : pobj);
1683 got_object_close(obj);
1686 got_object_close(pobj);
1690 err = got_error_from_errno();
1693 err = got_object_qid_alloc(&blamed_commit, id);
1697 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1698 blamed_commit, entry);
1699 err = run_blame(&blame, &mutex,
1700 &blame_complete, &first_displayed_line,
1701 &last_displayed_line, &selected_line,
1702 &done, path, blamed_commit->id, repo);
1708 struct got_object_qid *first;
1709 first = SIMPLEQ_FIRST(&blamed_commits);
1710 if (!got_object_id_cmp(first->id, commit_id))
1713 if (pthread_mutex_unlock(&mutex) != 0) {
1714 err = got_error_from_errno();
1717 thread_err = stop_blame(&blame);
1719 if (pthread_mutex_lock(&mutex) != 0) {
1720 err = got_error_from_errno();
1725 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1726 got_object_qid_free(blamed_commit);
1727 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1728 err = run_blame(&blame, &mutex,
1729 &blame_complete, &first_displayed_line,
1730 &last_displayed_line, &selected_line,
1731 &done, path, blamed_commit->id, repo);
1738 err = open_selected_commit(&pobj, &obj,
1739 blame.lines, first_displayed_line,
1740 selected_line, repo);
1743 if (pobj == NULL && obj == NULL)
1745 err = show_diff_view(pobj, obj, repo);
1747 got_object_close(pobj);
1750 got_object_close(obj);
1752 show_panel(tog_blame_view.panel);
1758 if (last_displayed_line >= blame.nlines &&
1759 selected_line < LINES - 2) {
1760 selected_line = MIN(blame.nlines,
1764 if (last_displayed_line + LINES - 2 <=
1766 first_displayed_line += LINES - 2;
1768 first_displayed_line =
1769 blame.nlines - (LINES - 3);
1774 if (pthread_mutex_unlock(&mutex) != 0)
1775 err = got_error_from_errno();
1776 if (err || thread_err)
1781 got_object_close(pobj);
1783 thread_err = stop_blame(&blame);
1784 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1785 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1786 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1787 got_object_qid_free(blamed_commit);
1789 return thread_err ? thread_err : err;
1792 static const struct got_error *
1793 cmd_blame(int argc, char *argv[])
1795 const struct got_error *error;
1796 struct got_repository *repo = NULL;
1797 char *repo_path = NULL;
1799 struct got_object_id *commit_id = NULL;
1800 char *commit_id_str = NULL;
1804 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1808 while ((ch = getopt(argc, argv, "c:")) != -1) {
1811 commit_id_str = optarg;
1824 } else if (argc == 1) {
1825 repo_path = getcwd(NULL, 0);
1826 if (repo_path == NULL)
1827 return got_error_from_errno();
1829 } else if (argc == 2) {
1830 repo_path = realpath(argv[0], NULL);
1831 if (repo_path == NULL)
1832 return got_error_from_errno();
1837 error = got_repo_open(&repo, repo_path);
1842 if (commit_id_str == NULL) {
1843 struct got_reference *head_ref;
1844 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1847 error = got_ref_resolve(&commit_id, repo, head_ref);
1848 got_ref_close(head_ref);
1850 struct got_object *obj;
1851 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1854 commit_id = got_object_get_id(obj);
1855 if (commit_id == NULL)
1856 error = got_error_from_errno();
1857 got_object_close(obj);
1862 error = show_blame_view(path, commit_id, repo);
1866 got_repo_close(repo);
1870 static const struct got_error *
1871 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1872 struct got_tree_entry **last_displayed_entry,
1873 struct got_tree_entry **selected_entry, int *ndisplayed,
1874 WINDOW *window, const char *label, int show_ids, const char *parent_path,
1875 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1877 const struct got_error *err = NULL;
1878 struct got_tree_entry *te;
1889 err = format_line(&wline, &width, label, COLS);
1892 waddwstr(window, wline);
1896 waddch(window, '\n');
1899 err = format_line(&wline, &width, parent_path, COLS);
1902 waddwstr(window, wline);
1906 waddch(window, '\n');
1909 waddch(window, '\n');
1913 te = SIMPLEQ_FIRST(&entries->head);
1914 if (*first_displayed_entry == NULL) {
1915 if (selected == 0) {
1917 *selected_entry = NULL;
1919 waddstr(window, " ..\n"); /* parent directory */
1928 while (te != *first_displayed_entry)
1929 te = SIMPLEQ_NEXT(te, entry);
1933 char *line = NULL, *id_str = NULL;
1936 err = got_object_id_str(&id_str, te->id);
1938 return got_error_from_errno();
1940 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
1941 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
1943 return got_error_from_errno();
1946 err = format_line(&wline, &width, line, COLS);
1951 if (n == selected) {
1953 *selected_entry = te;
1955 waddwstr(window, wline);
1957 waddch(window, '\n');
1965 *last_displayed_entry = te;
1968 te = SIMPLEQ_NEXT(te, entry);
1975 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1976 const struct got_tree_entries *entries, int isroot)
1978 struct got_tree_entry *te, *prev;
1981 if (*first_displayed_entry == NULL)
1984 te = SIMPLEQ_FIRST(&entries->head);
1985 if (*first_displayed_entry == te) {
1987 *first_displayed_entry = NULL;
1991 /* XXX this is stupid... switch to TAILQ? */
1992 for (i = 0; i < maxscroll; i++) {
1993 while (te != *first_displayed_entry) {
1995 te = SIMPLEQ_NEXT(te, entry);
1997 *first_displayed_entry = prev;
1998 te = SIMPLEQ_FIRST(&entries->head);
2000 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2001 *first_displayed_entry = NULL;
2005 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2006 struct got_tree_entry *last_displayed_entry,
2007 const struct got_tree_entries *entries)
2009 struct got_tree_entry *next;
2012 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2015 if (*first_displayed_entry)
2016 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2018 next = SIMPLEQ_FIRST(&entries->head);
2020 *first_displayed_entry = next;
2021 if (++n >= maxscroll)
2023 next = SIMPLEQ_NEXT(next, entry);
2027 struct tog_parent_tree {
2028 TAILQ_ENTRY(tog_parent_tree) entry;
2029 struct got_tree_object *tree;
2030 struct got_tree_entry *first_displayed_entry;
2031 struct got_tree_entry *selected_entry;
2035 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2037 static const struct got_error *
2038 tree_entry_path(char **path, struct tog_parent_trees *parents,
2039 struct got_tree_entry *te)
2041 const struct got_error *err = NULL;
2042 struct tog_parent_tree *pt;
2043 size_t len = 2; /* for leading slash and NUL */
2045 TAILQ_FOREACH(pt, parents, entry)
2046 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2048 len += strlen(te->name);
2050 *path = calloc(1, len);
2052 return got_error_from_errno();
2055 pt = TAILQ_LAST(parents, tog_parent_trees);
2057 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2058 err = got_error(GOT_ERR_NO_SPACE);
2061 if (strlcat(*path, "/", len) >= len) {
2062 err = got_error(GOT_ERR_NO_SPACE);
2065 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2068 if (strlcat(*path, te->name, len) >= len) {
2069 err = got_error(GOT_ERR_NO_SPACE);
2081 static const struct got_error *
2082 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2083 struct got_object_id *commit_id, struct got_repository *repo)
2085 const struct got_error *err = NULL;
2088 err = tree_entry_path(&path, parents, te);
2092 err = show_blame_view(path, commit_id, repo);
2097 static const struct got_error *
2098 log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2099 struct got_object_id *commit_id, struct got_repository *repo)
2101 const struct got_error *err = NULL;
2104 err = tree_entry_path(&path, parents, te);
2108 err = show_log_view(commit_id, repo, path);
2113 static const struct got_error *
2114 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2115 struct got_repository *repo)
2117 const struct got_error *err = NULL;
2118 int ch, done = 0, selected = 0, show_ids = 0;
2119 struct got_tree_object *tree = root;
2120 const struct got_tree_entries *entries;
2121 struct got_tree_entry *first_displayed_entry = NULL;
2122 struct got_tree_entry *last_displayed_entry = NULL;
2123 struct got_tree_entry *selected_entry = NULL;
2124 char *commit_id_str = NULL, *tree_label = NULL;
2125 int nentries, ndisplayed;
2126 struct tog_parent_trees parents;
2128 TAILQ_INIT(&parents);
2130 err = got_object_id_str(&commit_id_str, commit_id);
2134 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2135 err = got_error_from_errno();
2139 if (tog_tree_view.window == NULL) {
2140 tog_tree_view.window = newwin(0, 0, 0, 0);
2141 if (tog_tree_view.window == NULL)
2142 return got_error_from_errno();
2143 keypad(tog_tree_view.window, TRUE);
2145 if (tog_tree_view.panel == NULL) {
2146 tog_tree_view.panel = new_panel(tog_tree_view.window);
2147 if (tog_tree_view.panel == NULL)
2148 return got_error_from_errno();
2150 show_panel(tog_tree_view.panel);
2152 entries = got_object_tree_get_entries(root);
2153 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2156 entries = got_object_tree_get_entries(tree);
2157 nentries = entries->nentries;
2159 nentries++; /* '..' directory */
2161 err = tree_entry_path(&parent_path, &parents, NULL);
2165 err = draw_tree_entries(&first_displayed_entry,
2166 &last_displayed_entry, &selected_entry, &ndisplayed,
2167 tog_tree_view.window, tree_label, show_ids,
2168 parent_path, entries, selected, LINES, tree == root);
2173 nodelay(stdscr, FALSE);
2174 ch = wgetch(tog_tree_view.window);
2175 nodelay(stdscr, TRUE);
2181 show_ids = !show_ids;
2184 if (selected_entry) {
2185 err = log_tree_entry(selected_entry,
2186 &parents, commit_id, repo);
2197 tree_scroll_up(&first_displayed_entry, 1,
2198 entries, tree == root);
2201 if (SIMPLEQ_FIRST(&entries->head) ==
2202 first_displayed_entry) {
2204 first_displayed_entry = NULL;
2208 tree_scroll_up(&first_displayed_entry, LINES,
2209 entries, tree == root);
2213 if (selected < ndisplayed - 1) {
2217 tree_scroll_down(&first_displayed_entry, 1,
2218 last_displayed_entry, entries);
2221 tree_scroll_down(&first_displayed_entry, LINES,
2222 last_displayed_entry, entries);
2223 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2225 /* can't scroll any further; move cursor down */
2226 if (selected < ndisplayed - 1)
2227 selected = ndisplayed - 1;
2231 if (selected_entry == NULL) {
2232 struct tog_parent_tree *parent;
2234 /* user selected '..' */
2237 parent = TAILQ_FIRST(&parents);
2238 TAILQ_REMOVE(&parents, parent, entry);
2239 got_object_tree_close(tree);
2240 tree = parent->tree;
2241 first_displayed_entry =
2242 parent->first_displayed_entry;
2243 selected_entry = parent->selected_entry;
2244 selected = parent->selected;
2246 } else if (S_ISDIR(selected_entry->mode)) {
2247 struct tog_parent_tree *parent;
2248 struct got_tree_object *child;
2249 err = got_object_open_as_tree(
2250 &child, repo, selected_entry->id);
2253 parent = calloc(1, sizeof(*parent));
2254 if (parent == NULL) {
2255 err = got_error_from_errno();
2258 parent->tree = tree;
2259 parent->first_displayed_entry =
2260 first_displayed_entry;
2261 parent->selected_entry = selected_entry;
2262 parent->selected = selected;
2263 TAILQ_INSERT_HEAD(&parents, parent,
2267 first_displayed_entry = NULL;
2268 } else if (S_ISREG(selected_entry->mode)) {
2269 err = blame_tree_entry(selected_entry,
2270 &parents, commit_id, repo);
2276 if (selected > LINES)
2277 selected = ndisplayed - 1;
2285 free(commit_id_str);
2286 while (!TAILQ_EMPTY(&parents)) {
2287 struct tog_parent_tree *parent;
2288 parent = TAILQ_FIRST(&parents);
2289 TAILQ_REMOVE(&parents, parent, entry);
2294 got_object_tree_close(tree);
2302 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2307 static const struct got_error *
2308 cmd_tree(int argc, char *argv[])
2310 const struct got_error *error;
2311 struct got_repository *repo = NULL;
2312 char *repo_path = NULL;
2313 struct got_object_id *commit_id = NULL;
2314 char *commit_id_arg = NULL;
2315 struct got_commit_object *commit = NULL;
2316 struct got_tree_object *tree = NULL;
2320 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2324 while ((ch = getopt(argc, argv, "c:")) != -1) {
2327 commit_id_arg = optarg;
2339 repo_path = getcwd(NULL, 0);
2340 if (repo_path == NULL)
2341 return got_error_from_errno();
2342 } else if (argc == 1) {
2343 repo_path = realpath(argv[0], NULL);
2344 if (repo_path == NULL)
2345 return got_error_from_errno();
2349 error = got_repo_open(&repo, repo_path);
2354 if (commit_id_arg == NULL) {
2355 error = get_head_commit_id(&commit_id, repo);
2359 struct got_object *obj;
2360 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2361 if (error == NULL) {
2362 commit_id = got_object_get_id(obj);
2363 if (commit_id == NULL)
2364 error = got_error_from_errno();
2370 error = got_object_open_as_commit(&commit, repo, commit_id);
2374 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2378 error = show_tree_view(tree, commit_id, repo);
2382 got_object_commit_close(commit);
2384 got_object_tree_close(tree);
2386 got_repo_close(repo);
2396 intrflush(stdscr, FALSE);
2397 keypad(stdscr, TRUE);
2406 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2407 "Available commands:\n", getprogname());
2408 for (i = 0; i < nitems(tog_commands); i++) {
2409 struct tog_cmd *cmd = &tog_commands[i];
2410 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2416 make_argv(const char *arg0, const char *arg1)
2419 int argc = (arg1 == NULL ? 1 : 2);
2421 argv = calloc(argc, sizeof(char *));
2424 argv[0] = strdup(arg0);
2425 if (argv[0] == NULL)
2428 argv[1] = strdup(arg1);
2429 if (argv[1] == NULL)
2437 main(int argc, char *argv[])
2439 const struct got_error *error = NULL;
2440 struct tog_cmd *cmd = NULL;
2442 char **cmd_argv = NULL;
2444 setlocale(LC_ALL, "");
2446 while ((ch = getopt(argc, argv, "h")) != -1) {
2465 /* Build an argument vector which runs a default command. */
2466 cmd = &tog_commands[0];
2467 cmd_argv = make_argv(cmd->name, NULL);
2472 /* Did the user specific a command? */
2473 for (i = 0; i < nitems(tog_commands); i++) {
2474 if (strncmp(tog_commands[i].name, argv[0],
2475 strlen(argv[0])) == 0) {
2476 cmd = &tog_commands[i];
2478 tog_commands[i].cmd_usage();
2483 /* Did the user specify a repository? */
2484 char *repo_path = realpath(argv[0], NULL);
2486 struct got_repository *repo;
2487 error = got_repo_open(&repo, repo_path);
2489 got_repo_close(repo);
2491 error = got_error_from_errno();
2494 fprintf(stderr, "%s: '%s' is not a "
2495 "known command\n", getprogname(),
2499 fprintf(stderr, "%s: '%s' is neither a known "
2500 "command nor a path to a repository\n",
2501 getprogname(), argv[0]);
2505 cmd = &tog_commands[0];
2506 cmd_argv = make_argv(cmd->name, repo_path);
2514 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2521 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);