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
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
42 #include "got_opentemp.h"
43 #include "got_commit_graph.h"
45 #include "got_blame.h"
48 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 const struct got_error *(*cmd_main)(int, char *[]);
58 void (*cmd_usage)(void);
62 __dead static void usage(void);
63 __dead static void usage_log(void);
64 __dead static void usage_diff(void);
65 __dead static void usage_blame(void);
66 __dead static void usage_tree(void);
68 static const struct got_error* cmd_log(int, char *[]);
69 static const struct got_error* cmd_diff(int, char *[]);
70 static const struct got_error* cmd_blame(int, char *[]);
71 static const struct got_error* cmd_tree(int, char *[]);
73 static struct tog_cmd tog_commands[] = {
74 { "log", cmd_log, usage_log,
75 "show repository history" },
76 { "diff", cmd_diff, usage_diff,
77 "compare files and directories" },
78 { "blame", cmd_blame, usage_blame,
79 "show line-by-line file history" },
80 { "tree", cmd_tree, usage_tree,
81 "browse trees in repository" },
84 static struct tog_view {
87 } tog_log_view, tog_diff_view, tog_blame_view, tog_tree_view;
89 static const struct got_error *
90 show_diff_view(struct got_object *, struct got_object *,
91 struct got_repository *);
92 static const struct got_error *
93 show_log_view(struct got_object_id *, struct got_repository *);
94 static const struct got_error *
95 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
96 static const struct got_error *
97 show_tree_view(struct got_tree_object *, struct got_object_id *,
98 struct got_repository *);
104 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
109 /* Create newly allocated wide-character string equivalent to a byte string. */
110 static const struct got_error *
111 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
114 const struct got_error *err = NULL;
117 *wlen = mbstowcs(NULL, s, 0);
118 if (*wlen == (size_t)-1) {
121 return got_error_from_errno();
123 /* byte string invalid in current encoding; try to "fix" it */
124 err = got_mbsavis(&vis, &vislen, s);
127 *wlen = mbstowcs(NULL, vis, 0);
128 if (*wlen == (size_t)-1) {
129 err = got_error_from_errno(); /* give up */
134 *ws = calloc(*wlen + 1, sizeof(*ws));
136 err = got_error_from_errno();
140 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
141 err = got_error_from_errno();
152 /* Format a line for display, ensuring that it won't overflow a width limit. */
153 static const struct got_error *
154 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
156 const struct got_error *err = NULL;
158 wchar_t *wline = NULL;
164 err = mbs2ws(&wline, &wlen, line);
169 while (i < wlen && cols <= wlimit) {
170 int width = wcwidth(wline[i]);
179 if (wline[i] == L'\t')
183 err = got_error_from_errno();
201 static const struct got_error *
202 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
204 const struct got_error *err = NULL;
205 char *logmsg0 = NULL, *logmsg = NULL;
206 char *author0 = NULL, *author = NULL;
207 wchar_t *wlogmsg = NULL, *wauthor = NULL;
208 int author_width, logmsg_width;
209 char *newline, *smallerthan;
214 static const size_t id_display_cols = 8;
215 static const size_t author_display_cols = 16;
216 const int avail = COLS;
218 err = got_object_id_str(&id_str, id);
221 id_len = strlen(id_str);
222 if (avail < id_display_cols) {
223 limit = MIN(id_len, avail);
224 waddnstr(tog_log_view.window, id_str, limit);
226 limit = MIN(id_display_cols, id_len);
227 waddnstr(tog_log_view.window, id_str, limit);
230 while (col <= avail && col < id_display_cols + 2) {
231 waddch(tog_log_view.window, ' ');
237 author0 = strdup(commit->author);
238 if (author0 == NULL) {
239 err = got_error_from_errno();
243 smallerthan = strchr(author, '<');
247 char *at = strchr(author, '@');
251 limit = MIN(avail, author_display_cols);
252 err = format_line(&wauthor, &author_width, author, limit);
255 waddwstr(tog_log_view.window, wauthor);
257 while (col <= avail && author_width < author_display_cols + 1) {
258 waddch(tog_log_view.window, ' ');
265 logmsg0 = strdup(commit->logmsg);
266 if (logmsg0 == NULL) {
267 err = got_error_from_errno();
271 while (*logmsg == '\n')
273 newline = strchr(logmsg, '\n');
277 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
280 waddwstr(tog_log_view.window, wlogmsg);
282 while (col <= avail) {
283 waddch(tog_log_view.window, ' ');
296 struct commit_queue_entry {
297 TAILQ_ENTRY(commit_queue_entry) entry;
298 struct got_object_id *id;
299 struct got_commit_object *commit;
301 TAILQ_HEAD(commit_queue, commit_queue_entry);
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);
324 TAILQ_REMOVE(commits, entry, entry);
325 got_object_commit_close(entry->commit);
326 /* Don't free entry->id! It is owned by the commit graph. */
331 free_commits(struct commit_queue *commits)
333 while (!TAILQ_EMPTY(commits))
337 static const struct got_error *
338 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
339 struct got_object_id *start_id, struct got_repository *repo)
341 const struct got_error *err = NULL;
342 struct got_object_id *id;
343 struct commit_queue_entry *entry;
345 err = got_commit_graph_iter_start(graph, start_id);
349 entry = TAILQ_LAST(commits, commit_queue);
350 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
353 /* Start ID's commit is already on the queue; skip over it. */
354 err = got_commit_graph_iter_next(&id, graph);
355 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
358 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
364 struct got_commit_object *commit;
366 err = got_commit_graph_iter_next(&id, graph);
368 if (err->code == GOT_ERR_ITER_NEED_MORE)
373 err = got_object_open_as_commit(&commit, repo, id);
377 entry = alloc_commit_queue_entry(commit, id);
379 err = got_error_from_errno();
383 TAILQ_INSERT_TAIL(commits, entry, entry);
389 static const struct got_error *
390 fetch_next_commit(struct commit_queue_entry **pentry,
391 struct commit_queue_entry *entry, struct commit_queue *commits,
392 struct got_commit_graph *graph, struct got_repository *repo)
394 const struct got_error *err = NULL;
395 struct got_object_qid *qid;
399 /* Populate commit graph with entry's parent commits. */
400 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
402 err = got_commit_graph_fetch_commits_up_to(&nfetched,
403 graph, qid->id, repo);
408 /* Append outstanding commits to queue in graph sort order. */
409 err = queue_commits(graph, commits, entry->id, repo);
411 if (err->code == GOT_ERR_ITER_COMPLETED)
416 /* Next entry to display should now be available. */
417 *pentry = TAILQ_NEXT(entry, entry);
419 return got_error(GOT_ERR_NO_OBJ);
424 static const struct got_error *
425 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
427 const struct got_error *err = NULL;
428 struct got_reference *head_ref;
432 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
436 err = got_ref_resolve(head_id, repo, head_ref);
437 got_ref_close(head_ref);
446 static const struct got_error *
447 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
448 struct commit_queue_entry *first, int selected_idx, int limit)
450 const struct got_error *err = NULL;
451 struct commit_queue_entry *entry;
454 werase(tog_log_view.window);
459 if (ncommits == limit)
461 if (ncommits == selected_idx) {
462 wstandout(tog_log_view.window);
465 err = draw_commit(entry->commit, entry->id);
466 if (ncommits == selected_idx)
467 wstandend(tog_log_view.window);
472 entry = TAILQ_NEXT(entry, entry);
482 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
483 struct commit_queue *commits)
485 struct commit_queue_entry *entry;
488 entry = TAILQ_FIRST(commits);
489 if (*first_displayed_entry == entry)
492 entry = *first_displayed_entry;
493 while (entry && nscrolled < maxscroll) {
494 entry = TAILQ_PREV(entry, commit_queue, entry);
496 *first_displayed_entry = entry;
502 static const struct got_error *
503 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
504 struct commit_queue_entry *last_displayed_entry,
505 struct commit_queue *commits, struct got_commit_graph *graph,
506 struct got_repository *repo)
508 const struct got_error *err = NULL;
509 struct commit_queue_entry *pentry;
513 pentry = TAILQ_NEXT(last_displayed_entry, entry);
514 if (pentry == NULL) {
515 err = fetch_next_commit(&pentry, last_displayed_entry,
516 commits, graph, repo);
517 if (err || pentry == NULL)
520 last_displayed_entry = pentry;
522 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
525 *first_displayed_entry = pentry;
526 } while (++nscrolled < maxscroll);
532 num_parents(struct commit_queue_entry *entry)
537 entry = TAILQ_NEXT(entry, entry);
544 static const struct got_error *
545 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
547 const struct got_error *err;
548 struct got_object *obj1 = NULL, *obj2 = NULL;
549 struct got_object_qid *parent_id;
551 err = got_object_open(&obj2, repo, entry->id);
555 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
557 err = got_object_open(&obj1, repo, parent_id->id);
562 err = show_diff_view(obj1, obj2, repo);
565 got_object_close(obj1);
567 got_object_close(obj2);
571 static const struct got_error *
572 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
574 const struct got_error *err = NULL;
575 struct got_object_id *head_id = NULL;
576 int ch, done = 0, selected = 0, nparents, nfetched;
577 struct got_commit_graph *graph;
578 struct commit_queue commits;
579 struct commit_queue_entry *entry = NULL;
580 struct commit_queue_entry *first_displayed_entry = NULL;
581 struct commit_queue_entry *last_displayed_entry = NULL;
582 struct commit_queue_entry *selected_entry = NULL;
584 if (tog_log_view.window == NULL) {
585 tog_log_view.window = newwin(0, 0, 0, 0);
586 if (tog_log_view.window == NULL)
587 return got_error_from_errno();
588 keypad(tog_log_view.window, TRUE);
590 if (tog_log_view.panel == NULL) {
591 tog_log_view.panel = new_panel(tog_log_view.window);
592 if (tog_log_view.panel == NULL)
593 return got_error_from_errno();
595 show_panel(tog_log_view.panel);
597 err = get_head_commit_id(&head_id, repo);
601 TAILQ_INIT(&commits);
603 err = got_commit_graph_open(&graph, head_id, 0, repo);
607 /* Populate commit graph with a sufficient number of commits. */
608 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
612 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
617 * Open the initial batch of commits, sorted in commit graph order.
618 * We keep all commits open throughout the lifetime of the log view
619 * in order to avoid having to re-fetch commits from disk while
620 * updating the display.
622 err = queue_commits(graph, &commits, head_id, repo);
623 if (err && err->code != GOT_ERR_ITER_COMPLETED)
626 /* Find entry corresponding to the first commit to display. */
627 TAILQ_FOREACH(entry, &commits, entry) {
628 if (got_object_id_cmp(entry->id, start_id) == 0) {
629 first_displayed_entry = entry;
633 if (first_displayed_entry == NULL) {
634 err = got_error(GOT_ERR_NO_OBJ);
639 err = draw_commits(&last_displayed_entry, &selected_entry,
640 first_displayed_entry, selected, LINES);
644 nodelay(stdscr, FALSE);
645 ch = wgetch(tog_log_view.window);
646 nodelay(stdscr, TRUE);
650 err = got_error_from_errno();
663 scroll_up(&first_displayed_entry, 1, &commits);
666 if (TAILQ_FIRST(&commits) ==
667 first_displayed_entry) {
671 scroll_up(&first_displayed_entry, LINES,
676 nparents = num_parents(first_displayed_entry);
677 if (selected < LINES - 1 &&
678 selected < nparents - 1) {
682 err = scroll_down(&first_displayed_entry, 1,
683 last_displayed_entry, &commits, graph,
689 err = scroll_down(&first_displayed_entry, LINES,
690 last_displayed_entry, &commits, graph,
694 if (last_displayed_entry->commit->nparents > 0)
696 /* can't scroll any further; move cursor down */
697 nparents = num_parents(first_displayed_entry);
698 if (selected < LINES - 1 ||
699 selected < nparents - 1)
700 selected = MIN(LINES - 1, nparents - 1);
703 if (selected > LINES)
704 selected = LINES - 1;
708 err = show_commit(selected_entry, repo);
711 show_panel(tog_log_view.panel);
720 got_commit_graph_close(graph);
721 free_commits(&commits);
725 static const struct got_error *
726 cmd_log(int argc, char *argv[])
728 const struct got_error *error;
729 struct got_repository *repo;
730 struct got_object_id *start_id = NULL;
731 char *repo_path = NULL;
732 char *start_commit = NULL;
736 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
740 while ((ch = getopt(argc, argv, "c:")) != -1) {
743 start_commit = optarg;
755 repo_path = getcwd(NULL, 0);
756 if (repo_path == NULL)
757 return got_error_from_errno();
758 } else if (argc == 1) {
759 repo_path = realpath(argv[0], NULL);
760 if (repo_path == NULL)
761 return got_error_from_errno();
765 error = got_repo_open(&repo, repo_path);
770 if (start_commit == NULL) {
771 error = get_head_commit_id(&start_id, repo);
775 struct got_object *obj;
776 error = got_object_open_by_id_str(&obj, repo, start_commit);
778 start_id = got_object_get_id(obj);
779 if (start_id == NULL)
780 error = got_error_from_errno();
785 error = show_log_view(start_id, repo);
787 got_repo_close(repo);
795 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
801 parse_next_line(FILE *f, size_t *len)
806 const char delim[3] = { '\0', '\0', '\0'};
808 line = fparseln(f, &linelen, &lineno, delim, 0);
814 static const struct got_error *
815 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
816 int *last_displayed_line, int *eof, int max_lines)
818 const struct got_error *err;
819 int nlines = 0, nprinted = 0;
829 while (nprinted < max_lines) {
830 line = parse_next_line(f, &len);
835 if (++nlines < *first_displayed_line) {
840 err = format_line(&wline, &width, line, COLS);
845 waddwstr(window, wline);
847 waddch(window, '\n');
849 *first_displayed_line = nlines;
852 *last_displayed_line = nlines;
860 static const struct got_error *
861 show_diff_view(struct got_object *obj1, struct got_object *obj2,
862 struct got_repository *repo)
864 const struct got_error *err;
866 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
869 if (obj1 != NULL && obj2 != NULL &&
870 got_object_get_type(obj1) != got_object_get_type(obj2))
871 return got_error(GOT_ERR_OBJ_TYPE);
875 return got_error_from_errno();
877 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
878 case GOT_OBJ_TYPE_BLOB:
879 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
881 case GOT_OBJ_TYPE_TREE:
882 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
884 case GOT_OBJ_TYPE_COMMIT:
885 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
888 return got_error(GOT_ERR_OBJ_TYPE);
893 if (tog_diff_view.window == NULL) {
894 tog_diff_view.window = newwin(0, 0, 0, 0);
895 if (tog_diff_view.window == NULL)
896 return got_error_from_errno();
897 keypad(tog_diff_view.window, TRUE);
899 if (tog_diff_view.panel == NULL) {
900 tog_diff_view.panel = new_panel(tog_diff_view.window);
901 if (tog_diff_view.panel == NULL)
902 return got_error_from_errno();
904 show_panel(tog_diff_view.panel);
907 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
908 &last_displayed_line, &eof, LINES);
911 nodelay(stdscr, FALSE);
912 ch = wgetch(tog_diff_view.window);
913 nodelay(stdscr, TRUE);
921 if (first_displayed_line > 1)
922 first_displayed_line--;
926 while (i++ < LINES - 1 &&
927 first_displayed_line > 1)
928 first_displayed_line--;
935 first_displayed_line++;
940 while (!eof && i++ < LINES - 1) {
941 char *line = parse_next_line(f, NULL);
942 first_displayed_line++;
955 static const struct got_error *
956 cmd_diff(int argc, char *argv[])
958 const struct got_error *error = NULL;
959 struct got_repository *repo = NULL;
960 struct got_object *obj1 = NULL, *obj2 = NULL;
961 char *repo_path = NULL;
962 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
966 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
970 while ((ch = getopt(argc, argv, "")) != -1) {
982 usage_diff(); /* TODO show local worktree changes */
983 } else if (argc == 2) {
984 repo_path = getcwd(NULL, 0);
985 if (repo_path == NULL)
986 return got_error_from_errno();
987 obj_id_str1 = argv[0];
988 obj_id_str2 = argv[1];
989 } else if (argc == 3) {
990 repo_path = realpath(argv[0], NULL);
991 if (repo_path == NULL)
992 return got_error_from_errno();
993 obj_id_str1 = argv[1];
994 obj_id_str2 = argv[2];
998 error = got_repo_open(&repo, repo_path);
1003 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1007 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1011 error = show_diff_view(obj1, obj2, repo);
1013 got_repo_close(repo);
1015 got_object_close(obj1);
1017 got_object_close(obj2);
1025 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1030 static const struct got_error *
1031 show_blame_view(const char *path, struct got_object_id *commit_id,
1032 struct got_repository *repo)
1034 const struct got_error *err = NULL;
1036 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1041 return got_error_from_errno();
1043 err = got_blame(path, commit_id, repo, f);
1049 if (tog_blame_view.window == NULL) {
1050 tog_blame_view.window = newwin(0, 0, 0, 0);
1051 if (tog_blame_view.window == NULL)
1052 return got_error_from_errno();
1053 keypad(tog_blame_view.window, TRUE);
1055 if (tog_blame_view.panel == NULL) {
1056 tog_blame_view.panel = new_panel(tog_blame_view.window);
1057 if (tog_blame_view.panel == NULL)
1058 return got_error_from_errno();
1060 show_panel(tog_blame_view.panel);
1063 err = draw_file(tog_blame_view.window, f, &first_displayed_line,
1064 &last_displayed_line, &eof, LINES);
1067 nodelay(stdscr, FALSE);
1068 ch = wgetch(tog_blame_view.window);
1069 nodelay(stdscr, TRUE);
1077 if (first_displayed_line > 1)
1078 first_displayed_line--;
1082 while (i++ < LINES - 1 &&
1083 first_displayed_line > 1)
1084 first_displayed_line--;
1091 first_displayed_line++;
1096 while (!eof && i++ < LINES - 1) {
1097 char *line = parse_next_line(f, NULL);
1098 first_displayed_line++;
1112 static const struct got_error *
1113 cmd_blame(int argc, char *argv[])
1115 const struct got_error *error;
1116 struct got_repository *repo = NULL;
1117 char *repo_path = NULL;
1119 struct got_object_id *commit_id = NULL;
1120 char *commit_id_str = NULL;
1124 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1128 while ((ch = getopt(argc, argv, "c:")) != -1) {
1131 commit_id_str = optarg;
1144 } else if (argc == 1) {
1145 repo_path = getcwd(NULL, 0);
1146 if (repo_path == NULL)
1147 return got_error_from_errno();
1149 } else if (argc == 2) {
1150 repo_path = realpath(argv[0], NULL);
1151 if (repo_path == NULL)
1152 return got_error_from_errno();
1157 error = got_repo_open(&repo, repo_path);
1162 if (commit_id_str == NULL) {
1163 struct got_reference *head_ref;
1164 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1167 error = got_ref_resolve(&commit_id, repo, head_ref);
1168 got_ref_close(head_ref);
1170 struct got_object *obj;
1171 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1174 commit_id = got_object_get_id(obj);
1175 if (commit_id == NULL)
1176 error = got_error_from_errno();
1177 got_object_close(obj);
1182 error = show_blame_view(path, commit_id, repo);
1186 got_repo_close(repo);
1190 static const struct got_error *
1191 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1192 struct got_tree_entry **last_displayed_entry,
1193 struct got_tree_entry **selected_entry, int *ndisplayed,
1194 WINDOW *window, const char *label, const struct got_tree_entries *entries,
1195 int selected, int limit, int isroot)
1197 const struct got_error *err = NULL;
1198 struct got_tree_entry *te;
1210 err = format_line(&wline, &width, label, COLS);
1213 waddwstr(window, wline);
1215 waddch(window, '\n');
1218 waddch(window, '\n');
1222 te = SIMPLEQ_FIRST(&entries->head);
1223 if (*first_displayed_entry == NULL) {
1224 if (selected == 0) {
1226 *selected_entry = NULL;
1228 waddstr(window, " ..\n"); /* parent directory */
1237 while (te != *first_displayed_entry)
1238 te = SIMPLEQ_NEXT(te, entry);
1242 if (asprintf(&line, " %s%s",
1243 te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1244 return got_error_from_errno();
1245 err = format_line(&wline, &width, line, COLS);
1250 if (n == selected) {
1252 *selected_entry = te;
1254 waddwstr(window, wline);
1256 waddch(window, '\n');
1262 *last_displayed_entry = te;
1265 te = SIMPLEQ_NEXT(te, entry);
1272 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1273 const struct got_tree_entries *entries, int isroot)
1275 struct got_tree_entry *te, *prev;
1278 if (*first_displayed_entry == NULL)
1281 te = SIMPLEQ_FIRST(&entries->head);
1282 if (*first_displayed_entry == te) {
1284 *first_displayed_entry = NULL;
1288 /* XXX this is stupid... switch to TAILQ? */
1289 for (i = 0; i < maxscroll; i++) {
1290 while (te != *first_displayed_entry) {
1292 te = SIMPLEQ_NEXT(te, entry);
1294 *first_displayed_entry = prev;
1295 te = SIMPLEQ_FIRST(&entries->head);
1297 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1298 *first_displayed_entry = NULL;
1302 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1303 struct got_tree_entry *last_displayed_entry,
1304 const struct got_tree_entries *entries)
1306 struct got_tree_entry *next;
1309 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1312 if (*first_displayed_entry)
1313 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1315 next = SIMPLEQ_FIRST(&entries->head);
1317 *first_displayed_entry = next;
1318 if (++n >= maxscroll)
1320 next = SIMPLEQ_NEXT(next, entry);
1324 struct tog_parent_tree {
1325 SLIST_ENTRY(tog_parent_tree) entry;
1326 struct got_tree_object *tree;
1327 struct got_tree_entry *first_displayed_entry;
1328 struct got_tree_entry *selected_entry;
1332 SLIST_HEAD(tog_parent_trees, tog_parent_tree);
1334 static const struct got_error *
1335 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1336 struct got_object_id *commit_id, struct got_repository *repo)
1338 struct tog_parent_tree *pt;
1340 size_t len = 2; /* for leading slash and NUL */
1342 SLIST_FOREACH(pt, parents, entry)
1343 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1344 len += strlen(te->name);
1345 path = calloc(1, len);
1347 return got_error_from_errno();
1350 SLIST_FOREACH(pt, parents, entry) {
1351 if (strlcat(path, pt->selected_entry->name, len) >= len)
1352 return got_error(GOT_ERR_NO_SPACE);
1353 if (strlcat(path, "/", len) >= len)
1354 return got_error(GOT_ERR_NO_SPACE);
1356 if (strlcat(path, te->name, len) >= len)
1357 return got_error(GOT_ERR_NO_SPACE);
1359 return show_blame_view(path, commit_id, repo);
1362 static const struct got_error *
1363 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1364 struct got_repository *repo)
1366 const struct got_error *err = NULL;
1367 int ch, done = 0, selected = 0;
1368 struct got_tree_object *tree = root;
1369 const struct got_tree_entries *entries;
1370 struct got_tree_entry *first_displayed_entry = NULL;
1371 struct got_tree_entry *last_displayed_entry = NULL;
1372 struct got_tree_entry *selected_entry = NULL;
1373 char *commit_id_str = NULL, *tree_label = NULL;
1374 int nentries, ndisplayed;
1375 struct tog_parent_trees parents;
1377 SLIST_INIT(&parents);
1379 err = got_object_id_str(&commit_id_str, commit_id);
1383 if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1384 err = got_error_from_errno();
1388 if (tog_tree_view.window == NULL) {
1389 tog_tree_view.window = newwin(0, 0, 0, 0);
1390 if (tog_tree_view.window == NULL)
1391 return got_error_from_errno();
1392 keypad(tog_tree_view.window, TRUE);
1394 if (tog_tree_view.panel == NULL) {
1395 tog_tree_view.panel = new_panel(tog_tree_view.window);
1396 if (tog_tree_view.panel == NULL)
1397 return got_error_from_errno();
1399 show_panel(tog_tree_view.panel);
1401 entries = got_object_tree_get_entries(root);
1402 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1404 entries = got_object_tree_get_entries(tree);
1405 nentries = entries->nentries;
1407 nentries++; /* '..' directory */
1409 err = draw_tree_entries(&first_displayed_entry,
1410 &last_displayed_entry, &selected_entry, &ndisplayed,
1411 tog_tree_view.window, tree_label, entries, selected,
1412 LINES, tree == root);
1416 nodelay(stdscr, FALSE);
1417 ch = wgetch(tog_tree_view.window);
1418 nodelay(stdscr, TRUE);
1429 tree_scroll_up(&first_displayed_entry, 1,
1430 entries, tree == root);
1433 if (SIMPLEQ_FIRST(&entries->head) ==
1434 first_displayed_entry) {
1438 tree_scroll_up(&first_displayed_entry, LINES,
1439 entries, tree == root);
1443 if (selected < ndisplayed - 1) {
1447 tree_scroll_down(&first_displayed_entry, 1,
1448 last_displayed_entry, entries);
1451 tree_scroll_down(&first_displayed_entry, LINES,
1452 last_displayed_entry, entries);
1453 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1455 /* can't scroll any further; move cursor down */
1456 if (selected < ndisplayed - 1)
1457 selected = ndisplayed - 1;
1461 if (selected_entry == NULL) {
1462 struct tog_parent_tree *parent;
1464 /* user selected '..' */
1467 parent = SLIST_FIRST(&parents);
1468 SLIST_REMOVE_HEAD(&parents, entry);
1469 got_object_tree_close(tree);
1470 tree = parent->tree;
1471 first_displayed_entry =
1472 parent->first_displayed_entry;
1473 selected_entry = parent->selected_entry;
1474 selected = parent->selected;
1476 } else if (S_ISDIR(selected_entry->mode)) {
1477 struct tog_parent_tree *parent;
1478 struct got_tree_object *child;
1479 err = got_object_open_as_tree(
1480 &child, repo, selected_entry->id);
1483 parent = calloc(1, sizeof(*parent));
1484 if (parent == NULL) {
1485 err = got_error_from_errno();
1488 parent->tree = tree;
1489 parent->first_displayed_entry =
1490 first_displayed_entry;
1491 parent->selected_entry = selected_entry;
1492 parent->selected = selected;
1493 SLIST_INSERT_HEAD(&parents, parent,
1497 first_displayed_entry = NULL;
1498 } else if (S_ISREG(selected_entry->mode)) {
1499 err = blame_tree_entry(selected_entry,
1500 &parents, commit_id, repo);
1506 if (selected > LINES)
1507 selected = ndisplayed - 1;
1515 free(commit_id_str);
1516 while (!SLIST_EMPTY(&parents)) {
1517 struct tog_parent_tree *parent;
1518 parent = SLIST_FIRST(&parents);
1519 SLIST_REMOVE_HEAD(&parents, entry);
1524 got_object_tree_close(tree);
1532 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1537 static const struct got_error *
1538 cmd_tree(int argc, char *argv[])
1540 const struct got_error *error;
1541 struct got_repository *repo = NULL;
1542 char *repo_path = NULL;
1543 struct got_object_id *commit_id = NULL;
1544 char *commit_id_arg = NULL;
1545 struct got_commit_object *commit = NULL;
1546 struct got_tree_object *tree = NULL;
1550 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1554 while ((ch = getopt(argc, argv, "c:")) != -1) {
1557 commit_id_arg = optarg;
1569 repo_path = getcwd(NULL, 0);
1570 if (repo_path == NULL)
1571 return got_error_from_errno();
1572 } else if (argc == 1) {
1573 repo_path = realpath(argv[0], NULL);
1574 if (repo_path == NULL)
1575 return got_error_from_errno();
1579 error = got_repo_open(&repo, repo_path);
1584 if (commit_id_arg == NULL) {
1585 error = get_head_commit_id(&commit_id, repo);
1589 struct got_object *obj;
1590 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1591 if (error == NULL) {
1592 commit_id = got_object_get_id(obj);
1593 if (commit_id == NULL)
1594 error = got_error_from_errno();
1600 error = got_object_open_as_commit(&commit, repo, commit_id);
1604 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1608 error = show_tree_view(tree, commit_id, repo);
1612 got_object_commit_close(commit);
1614 got_object_tree_close(tree);
1616 got_repo_close(repo);
1626 intrflush(stdscr, FALSE);
1627 keypad(stdscr, TRUE);
1636 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1637 "Available commands:\n", getprogname());
1638 for (i = 0; i < nitems(tog_commands); i++) {
1639 struct tog_cmd *cmd = &tog_commands[i];
1640 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1646 make_argv(const char *arg0, const char *arg1)
1649 int argc = (arg1 == NULL ? 1 : 2);
1651 argv = calloc(argc, sizeof(char *));
1654 argv[0] = strdup(arg0);
1655 if (argv[0] == NULL)
1658 argv[1] = strdup(arg1);
1659 if (argv[1] == NULL)
1667 main(int argc, char *argv[])
1669 const struct got_error *error = NULL;
1670 struct tog_cmd *cmd = NULL;
1672 char **cmd_argv = NULL;
1674 setlocale(LC_ALL, "");
1676 while ((ch = getopt(argc, argv, "h")) != -1) {
1695 /* Build an argument vector which runs a default command. */
1696 cmd = &tog_commands[0];
1697 cmd_argv = make_argv(cmd->name, NULL);
1702 /* Did the user specific a command? */
1703 for (i = 0; i < nitems(tog_commands); i++) {
1704 if (strncmp(tog_commands[i].name, argv[0],
1705 strlen(argv[0])) == 0) {
1706 cmd = &tog_commands[i];
1708 tog_commands[i].cmd_usage();
1713 /* Did the user specify a repository? */
1714 char *repo_path = realpath(argv[0], NULL);
1716 struct got_repository *repo;
1717 error = got_repo_open(&repo, repo_path);
1719 got_repo_close(repo);
1721 error = got_error_from_errno();
1724 fprintf(stderr, "%s: '%s' is not a "
1725 "known command\n", getprogname(),
1729 fprintf(stderr, "%s: '%s' is neither a known "
1730 "command nor a path to a repository\n",
1731 getprogname(), argv[0]);
1735 cmd = &tog_commands[0];
1736 cmd_argv = make_argv(cmd->name, repo_path);
1744 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1751 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);