2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
21 #define _XOPEN_SOURCE_EXTENDED
23 #undef _XOPEN_SOURCE_EXTENDED
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_commit_graph.h"
46 #include "got_blame.h"
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 const struct got_error *(*cmd_main)(int, char *[]);
59 void (*cmd_usage)(void);
63 __dead static void usage(void);
64 __dead static void usage_log(void);
65 __dead static void usage_diff(void);
66 __dead static void usage_blame(void);
67 __dead static void usage_tree(void);
69 static const struct got_error* cmd_log(int, char *[]);
70 static const struct got_error* cmd_diff(int, char *[]);
71 static const struct got_error* cmd_blame(int, char *[]);
72 static const struct got_error* cmd_tree(int, char *[]);
74 static struct tog_cmd tog_commands[] = {
75 { "log", cmd_log, usage_log,
76 "show repository history" },
77 { "diff", cmd_diff, usage_diff,
78 "compare files and directories" },
79 { "blame", cmd_blame, usage_blame,
80 "show line-by-line file history" },
81 { "tree", cmd_tree, usage_tree,
82 "browse trees in repository" },
88 int nlines, ncols, begin_y, begin_x;
89 int lines, cols; /* copies of LINES and COLS */
92 static const struct got_error *
93 show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
94 struct got_repository *);
95 static const struct got_error *
96 show_log_view(struct got_object_id *, struct got_repository *, const char *);
97 static const struct got_error *
98 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
99 static const struct got_error *
100 show_tree_view(struct got_tree_object *, struct got_object_id *,
101 struct got_repository *);
104 close_view(struct tog_view *view)
107 del_panel(view->panel);
109 delwin(view->window);
113 static struct tog_view *
114 open_view(int nlines, int ncols, int begin_y, int begin_x)
116 struct tog_view *view = malloc(sizeof(*view));
123 view->nlines = nlines ? nlines : LINES - begin_y;
124 view->ncols = ncols ? ncols : COLS - begin_x;
125 view->begin_y = begin_y;
126 view->begin_x = begin_x;
127 view->window = newwin(nlines, ncols, begin_y, begin_x);
128 if (view->window == NULL) {
132 view->panel = new_panel(view->window);
133 if (view->panel == NULL) {
138 keypad(view->window, TRUE);
142 const struct got_error *
143 view_resize(struct tog_view *view)
147 if (view->lines > LINES)
148 nlines = view->nlines - (view->lines - LINES);
150 nlines = view->nlines + (LINES - view->lines);
152 if (view->cols > COLS)
153 ncols = view->ncols - (view->cols - COLS);
155 ncols = view->ncols + (COLS - view->cols);
157 if (wresize(view->window, nlines, ncols) == ERR)
158 return got_error_from_errno();
160 view->nlines = nlines;
171 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
176 /* Create newly allocated wide-character string equivalent to a byte string. */
177 static const struct got_error *
178 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
181 const struct got_error *err = NULL;
184 *wlen = mbstowcs(NULL, s, 0);
185 if (*wlen == (size_t)-1) {
188 return got_error_from_errno();
190 /* byte string invalid in current encoding; try to "fix" it */
191 err = got_mbsavis(&vis, &vislen, s);
194 *wlen = mbstowcs(NULL, vis, 0);
195 if (*wlen == (size_t)-1) {
196 err = got_error_from_errno(); /* give up */
201 *ws = calloc(*wlen + 1, sizeof(*ws));
203 err = got_error_from_errno();
207 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
208 err = got_error_from_errno();
219 /* Format a line for display, ensuring that it won't overflow a width limit. */
220 static const struct got_error *
221 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
223 const struct got_error *err = NULL;
225 wchar_t *wline = NULL;
232 err = mbs2ws(&wline, &wlen, line);
237 while (i < wlen && cols < wlimit) {
238 int width = wcwidth(wline[i]);
245 if (cols + width <= wlimit) {
251 if (wline[i] == L'\t')
252 cols += TABSIZE - ((cols + 1) % TABSIZE);
256 err = got_error_from_errno();
271 static const struct got_error *
272 draw_commit(struct tog_view *view, struct got_commit_object *commit,
273 struct got_object_id *id)
275 const struct got_error *err = NULL;
276 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
277 char *logmsg0 = NULL, *logmsg = NULL;
278 char *author0 = NULL, *author = NULL;
279 wchar_t *wlogmsg = NULL, *wauthor = NULL;
280 int author_width, logmsg_width;
281 char *newline, *smallerthan;
284 static const size_t date_display_cols = 9;
285 static const size_t author_display_cols = 16;
286 const int avail = view->ncols;
288 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
290 return got_error(GOT_ERR_NO_SPACE);
292 if (avail < date_display_cols)
293 limit = MIN(sizeof(datebuf) - 1, avail);
295 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
296 waddnstr(view->window, datebuf, limit);
301 author0 = strdup(commit->author);
302 if (author0 == NULL) {
303 err = got_error_from_errno();
307 smallerthan = strchr(author, '<');
311 char *at = strchr(author, '@');
316 err = format_line(&wauthor, &author_width, author, limit);
319 waddwstr(view->window, wauthor);
321 while (col <= avail && author_width < author_display_cols + 1) {
322 waddch(view->window, ' ');
329 logmsg0 = strdup(commit->logmsg);
330 if (logmsg0 == NULL) {
331 err = got_error_from_errno();
335 while (*logmsg == '\n')
337 newline = strchr(logmsg, '\n');
341 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
344 waddwstr(view->window, wlogmsg);
346 while (col <= avail) {
347 waddch(view->window, ' ');
359 struct commit_queue_entry {
360 TAILQ_ENTRY(commit_queue_entry) entry;
361 struct got_object_id *id;
362 struct got_commit_object *commit;
364 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
365 struct commit_queue {
367 struct commit_queue_head head;
370 static struct commit_queue_entry *
371 alloc_commit_queue_entry(struct got_commit_object *commit,
372 struct got_object_id *id)
374 struct commit_queue_entry *entry;
376 entry = calloc(1, sizeof(*entry));
381 entry->commit = commit;
386 pop_commit(struct commit_queue *commits)
388 struct commit_queue_entry *entry;
390 entry = TAILQ_FIRST(&commits->head);
391 TAILQ_REMOVE(&commits->head, entry, entry);
392 got_object_commit_close(entry->commit);
394 /* Don't free entry->id! It is owned by the commit graph. */
399 free_commits(struct commit_queue *commits)
401 while (!TAILQ_EMPTY(&commits->head))
405 static const struct got_error *
406 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
407 struct got_object_id *start_id, int minqueue, int init,
408 struct got_repository *repo, const char *path)
410 const struct got_error *err = NULL;
411 struct got_object_id *id;
412 struct commit_queue_entry *entry;
413 int nfetched, nqueued = 0, found_obj = 0;
414 int is_root_path = strcmp(path, "/") == 0;
416 err = got_commit_graph_iter_start(graph, start_id);
420 entry = TAILQ_LAST(&commits->head, commit_queue_head);
421 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
424 /* Start ID's commit is already on the queue; skip over it. */
425 err = got_commit_graph_iter_next(&id, graph);
426 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
429 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
435 struct got_commit_object *commit;
437 err = got_commit_graph_iter_next(&id, graph);
439 if (err->code != GOT_ERR_ITER_NEED_MORE)
441 if (nqueued >= minqueue) {
445 err = got_commit_graph_fetch_commits(&nfetched,
454 err = got_object_open_as_commit(&commit, repo, id);
459 struct got_object *obj;
460 struct got_object_qid *pid;
463 err = got_object_open_by_path(&obj, repo, id, path);
465 got_object_commit_close(commit);
466 if (err->code == GOT_ERR_NO_OBJ &&
467 (found_obj || !init)) {
468 /* History stops here. */
469 err = got_error(GOT_ERR_ITER_COMPLETED);
475 pid = SIMPLEQ_FIRST(&commit->parent_ids);
477 struct got_object *pobj;
478 err = got_object_open_by_path(&pobj, repo,
481 if (err->code != GOT_ERR_NO_OBJ) {
482 got_object_close(obj);
483 got_object_commit_close(commit);
489 struct got_object_id *id, *pid;
490 id = got_object_get_id(obj);
492 err = got_error_from_errno();
493 got_object_close(obj);
494 got_object_close(pobj);
497 pid = got_object_get_id(pobj);
499 err = got_error_from_errno();
501 got_object_close(obj);
502 got_object_close(pobj);
506 (got_object_id_cmp(id, pid) != 0);
507 got_object_close(pobj);
512 got_object_close(obj);
514 got_object_commit_close(commit);
519 entry = alloc_commit_queue_entry(commit, id);
521 err = got_error_from_errno();
524 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
532 static const struct got_error *
533 fetch_next_commit(struct commit_queue_entry **pentry,
534 struct commit_queue_entry *entry, struct commit_queue *commits,
535 struct got_commit_graph *graph, struct got_repository *repo,
538 const struct got_error *err = NULL;
542 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
546 /* Next entry to display should now be available. */
547 *pentry = TAILQ_NEXT(entry, entry);
549 return got_error(GOT_ERR_NO_OBJ);
554 static const struct got_error *
555 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
557 const struct got_error *err = NULL;
558 struct got_reference *head_ref;
562 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
566 err = got_ref_resolve(head_id, repo, head_ref);
567 got_ref_close(head_ref);
576 static const struct got_error *
577 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
578 struct commit_queue_entry **selected, struct commit_queue_entry *first,
579 struct commit_queue *commits, int selected_idx, int limit,
580 struct got_commit_graph *graph, struct got_repository *repo,
583 const struct got_error *err = NULL;
584 struct commit_queue_entry *entry;
586 char *id_str, *header;
592 if (ncommits == selected_idx) {
596 entry = TAILQ_NEXT(entry, entry);
600 err = got_object_id_str(&id_str, (*selected)->id);
605 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
606 err = got_error_from_errno();
610 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
611 err = got_error_from_errno();
616 err = format_line(&wline, &width, header, view->ncols);
623 werase(view->window);
625 waddwstr(view->window, wline);
626 if (width < view->ncols)
627 waddch(view->window, '\n');
636 if (ncommits >= limit - 1)
638 if (ncommits == selected_idx)
639 wstandout(view->window);
640 err = draw_commit(view, entry->commit, entry->id);
641 if (ncommits == selected_idx)
642 wstandend(view->window);
647 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
648 err = queue_commits(graph, commits, entry->id, 1,
651 if (err->code != GOT_ERR_ITER_COMPLETED)
656 entry = TAILQ_NEXT(entry, entry);
666 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
667 struct commit_queue *commits)
669 struct commit_queue_entry *entry;
672 entry = TAILQ_FIRST(&commits->head);
673 if (*first_displayed_entry == entry)
676 entry = *first_displayed_entry;
677 while (entry && nscrolled < maxscroll) {
678 entry = TAILQ_PREV(entry, commit_queue_head, entry);
680 *first_displayed_entry = entry;
686 static const struct got_error *
687 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
688 struct commit_queue_entry *last_displayed_entry,
689 struct commit_queue *commits, struct got_commit_graph *graph,
690 struct got_repository *repo, const char *path)
692 const struct got_error *err = NULL;
693 struct commit_queue_entry *pentry;
697 pentry = TAILQ_NEXT(last_displayed_entry, entry);
698 if (pentry == NULL) {
699 err = fetch_next_commit(&pentry, last_displayed_entry,
700 commits, graph, repo, path);
701 if (err || pentry == NULL)
704 last_displayed_entry = pentry;
706 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
709 *first_displayed_entry = pentry;
710 } while (++nscrolled < maxscroll);
715 static const struct got_error *
716 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
718 const struct got_error *err;
719 struct got_object *obj1 = NULL, *obj2 = NULL;
720 struct got_object_qid *parent_id;
721 struct tog_view *view;
723 err = got_object_open(&obj2, repo, entry->id);
727 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
729 err = got_object_open(&obj1, repo, parent_id->id);
734 view = open_view(0, 0, 0, 0);
736 err = got_error_from_errno();
740 err = show_diff_view(view, obj1, obj2, repo);
744 got_object_close(obj1);
746 got_object_close(obj2);
750 static const struct got_error *
751 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
753 const struct got_error *err = NULL;
754 struct got_tree_object *tree;
756 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
760 err = show_tree_view(tree, entry->id, repo);
761 got_object_tree_close(tree);
765 static const struct got_error *
766 show_log_view(struct got_object_id *start_id, struct got_repository *repo,
769 const struct got_error *err = NULL;
770 struct got_object_id *head_id = NULL;
771 int ch, done = 0, selected = 0, nfetched;
772 struct got_commit_graph *graph = NULL;
773 struct commit_queue commits;
774 struct commit_queue_entry *first_displayed_entry = NULL;
775 struct commit_queue_entry *last_displayed_entry = NULL;
776 struct commit_queue_entry *selected_entry = NULL;
777 char *in_repo_path = NULL;
778 struct tog_view *view = NULL;
780 err = got_repo_map_path(&in_repo_path, repo, path);
784 err = get_head_commit_id(&head_id, repo);
788 /* The graph contains all commits. */
789 err = got_commit_graph_open(&graph, head_id, 0, repo);
792 /* The commit queue contains a subset of commits filtered by path. */
793 TAILQ_INIT(&commits.head);
794 commits.ncommits = 0;
796 /* Populate commit graph with a sufficient number of commits. */
797 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
802 view = open_view(0, 0, 0, 0);
804 err = got_error_from_errno();
807 show_panel(view->panel);
810 * Open the initial batch of commits, sorted in commit graph order.
811 * We keep all commits open throughout the lifetime of the log view
812 * in order to avoid having to re-fetch commits from disk while
813 * updating the display.
815 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
818 if (err->code != GOT_ERR_ITER_COMPLETED)
823 first_displayed_entry = TAILQ_FIRST(&commits.head);
824 selected_entry = first_displayed_entry;
826 err = draw_commits(view, &last_displayed_entry, &selected_entry,
827 first_displayed_entry, &commits, selected, view->nlines,
828 graph, repo, in_repo_path);
832 nodelay(stdscr, FALSE);
833 ch = wgetch(view->window);
834 nodelay(stdscr, TRUE);
847 scroll_up(&first_displayed_entry, 1, &commits);
850 if (TAILQ_FIRST(&commits.head) ==
851 first_displayed_entry) {
855 scroll_up(&first_displayed_entry, view->nlines,
860 if (selected < MIN(view->nlines - 2,
861 commits.ncommits - 1)) {
865 err = scroll_down(&first_displayed_entry, 1,
866 last_displayed_entry, &commits, graph,
869 if (err->code != GOT_ERR_ITER_COMPLETED)
875 struct commit_queue_entry *first = first_displayed_entry;
876 err = scroll_down(&first_displayed_entry, view->nlines,
877 last_displayed_entry, &commits, graph,
880 if (err->code != GOT_ERR_ITER_COMPLETED)
882 /* can't scroll any further; move cursor down */
883 if (first == first_displayed_entry && selected <
884 MIN(view->nlines - 2, commits.ncommits - 1)) {
885 selected = MIN(view->nlines - 2,
886 commits.ncommits - 1);
893 err = view_resize(view);
896 if (selected > view->nlines - 2)
897 selected = view->nlines - 2;
898 if (selected > commits.ncommits - 1)
899 selected = commits.ncommits - 1;
903 err = show_commit(selected_entry, repo);
906 show_panel(view->panel);
909 err = browse_commit(selected_entry, repo);
912 show_panel(view->panel);
923 got_commit_graph_close(graph);
924 free_commits(&commits);
929 static const struct got_error *
930 cmd_log(int argc, char *argv[])
932 const struct got_error *error;
933 struct got_repository *repo = NULL;
934 struct got_object_id *start_id = NULL;
935 char *path = NULL, *repo_path = NULL, *cwd = NULL;
936 char *start_commit = NULL;
940 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
944 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
947 start_commit = optarg;
950 repo_path = realpath(optarg, NULL);
951 if (repo_path == NULL)
966 path = strdup(argv[0]);
970 return got_error_from_errno();
972 cwd = getcwd(NULL, 0);
974 error = got_error_from_errno();
977 if (repo_path == NULL) {
978 repo_path = strdup(cwd);
979 if (repo_path == NULL) {
980 error = got_error_from_errno();
985 error = got_repo_open(&repo, repo_path);
989 if (start_commit == NULL) {
990 error = get_head_commit_id(&start_id, repo);
994 struct got_object *obj;
995 error = got_object_open_by_id_str(&obj, repo, start_commit);
997 start_id = got_object_get_id(obj);
998 if (start_id == NULL)
999 error = got_error_from_errno();
1006 error = show_log_view(start_id, repo, path);
1013 got_repo_close(repo);
1021 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1027 parse_next_line(FILE *f, size_t *len)
1032 const char delim[3] = { '\0', '\0', '\0'};
1034 line = fparseln(f, &linelen, &lineno, delim, 0);
1040 static const struct got_error *
1041 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1042 int *last_displayed_line, int *eof, int max_lines)
1044 const struct got_error *err;
1045 int nlines = 0, nprinted = 0;
1052 werase(view->window);
1055 while (nprinted < max_lines) {
1056 line = parse_next_line(f, &len);
1061 if (++nlines < *first_displayed_line) {
1066 err = format_line(&wline, &width, line, view->ncols);
1072 waddwstr(view->window, wline);
1073 if (width < view->ncols)
1074 waddch(view->window, '\n');
1075 if (++nprinted == 1)
1076 *first_displayed_line = nlines;
1081 *last_displayed_line = nlines;
1089 static const struct got_error *
1090 show_diff_view(struct tog_view *view, struct got_object *obj1,
1091 struct got_object *obj2, struct got_repository *repo)
1093 const struct got_error *err;
1096 int first_displayed_line = 1, last_displayed_line = view->nlines;
1099 if (obj1 != NULL && obj2 != NULL &&
1100 got_object_get_type(obj1) != got_object_get_type(obj2))
1101 return got_error(GOT_ERR_OBJ_TYPE);
1105 return got_error_from_errno();
1107 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1108 case GOT_OBJ_TYPE_BLOB:
1109 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1111 case GOT_OBJ_TYPE_TREE:
1112 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1114 case GOT_OBJ_TYPE_COMMIT:
1115 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1118 return got_error(GOT_ERR_OBJ_TYPE);
1123 show_panel(view->panel);
1126 err = draw_file(view, f, &first_displayed_line,
1127 &last_displayed_line, &eof, view->nlines);
1130 nodelay(stdscr, FALSE);
1131 ch = wgetch(view->window);
1132 nodelay(stdscr, TRUE);
1139 if (first_displayed_line > 1)
1140 first_displayed_line--;
1145 while (i++ < view->nlines - 1 &&
1146 first_displayed_line > 1)
1147 first_displayed_line--;
1152 first_displayed_line++;
1157 while (!eof && i++ < view->nlines - 1) {
1158 char *line = parse_next_line(f, NULL);
1159 first_displayed_line++;
1165 err = view_resize(view);
1178 static const struct got_error *
1179 cmd_diff(int argc, char *argv[])
1181 const struct got_error *error = NULL;
1182 struct got_repository *repo = NULL;
1183 struct got_object *obj1 = NULL, *obj2 = NULL;
1184 char *repo_path = NULL;
1185 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1187 struct tog_view *view;
1190 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1194 while ((ch = getopt(argc, argv, "")) != -1) {
1206 usage_diff(); /* TODO show local worktree changes */
1207 } else if (argc == 2) {
1208 repo_path = getcwd(NULL, 0);
1209 if (repo_path == NULL)
1210 return got_error_from_errno();
1211 obj_id_str1 = argv[0];
1212 obj_id_str2 = argv[1];
1213 } else if (argc == 3) {
1214 repo_path = realpath(argv[0], NULL);
1215 if (repo_path == NULL)
1216 return got_error_from_errno();
1217 obj_id_str1 = argv[1];
1218 obj_id_str2 = argv[2];
1222 error = got_repo_open(&repo, repo_path);
1227 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1231 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1235 view = open_view(0, 0, 0, 0);
1237 error = got_error_from_errno();
1240 error = show_diff_view(view, obj1, obj2, repo);
1243 got_repo_close(repo);
1245 got_object_close(obj1);
1247 got_object_close(obj2);
1255 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1260 struct tog_blame_line {
1262 struct got_object_id *id;
1265 static const struct got_error *
1266 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1267 const char *path, struct tog_blame_line *lines, int nlines,
1268 int blame_complete, int selected_line, int *first_displayed_line,
1269 int *last_displayed_line, int *eof, int max_lines)
1271 const struct got_error *err;
1272 int lineno = 0, nprinted = 0;
1277 struct tog_blame_line *blame_line;
1278 struct got_object_id *prev_id = NULL;
1281 err = got_object_id_str(&id_str, id);
1286 werase(view->window);
1288 if (asprintf(&line, "commit: %s", id_str) == -1) {
1289 err = got_error_from_errno();
1294 err = format_line(&wline, &width, line, view->ncols);
1297 waddwstr(view->window, wline);
1300 if (width < view->ncols)
1301 waddch(view->window, '\n');
1303 if (asprintf(&line, "[%d/%d] %s%s",
1304 *first_displayed_line - 1 + selected_line, nlines,
1305 blame_complete ? "" : "annotating ", path) == -1) {
1307 return got_error_from_errno();
1310 err = format_line(&wline, &width, line, view->ncols);
1315 waddwstr(view->window, wline);
1318 if (width < view->ncols)
1319 waddch(view->window, '\n');
1322 while (nprinted < max_lines - 2) {
1323 line = parse_next_line(f, &len);
1328 if (++lineno < *first_displayed_line) {
1333 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1334 err = format_line(&wline, &width, line, wlimit);
1340 if (nprinted == selected_line - 1)
1341 wstandout(view->window);
1343 blame_line = &lines[lineno - 1];
1344 if (blame_line->annotated && prev_id &&
1345 got_object_id_cmp(prev_id, blame_line->id) == 0)
1346 waddstr(view->window, " ");
1347 else if (blame_line->annotated) {
1349 err = got_object_id_str(&id_str, blame_line->id);
1355 wprintw(view->window, "%.8s ", id_str);
1357 prev_id = blame_line->id;
1359 waddstr(view->window, "........ ");
1363 waddwstr(view->window, wline);
1364 while (width < wlimit) {
1365 waddch(view->window, ' ');
1368 if (nprinted == selected_line - 1)
1369 wstandend(view->window);
1370 if (++nprinted == 1)
1371 *first_displayed_line = lineno;
1376 *last_displayed_line = lineno;
1384 struct tog_blame_cb_args {
1385 pthread_mutex_t *mutex;
1386 struct tog_blame_line *lines; /* one per line */
1389 struct tog_view *view;
1390 struct got_object_id *commit_id;
1393 int *first_displayed_line;
1394 int *last_displayed_line;
1399 static const struct got_error *
1400 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1402 const struct got_error *err = NULL;
1403 struct tog_blame_cb_args *a = arg;
1404 struct tog_blame_line *line;
1407 if (nlines != a->nlines ||
1408 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1409 return got_error(GOT_ERR_RANGE);
1411 if (pthread_mutex_lock(a->mutex) != 0)
1412 return got_error_from_errno();
1414 if (*a->quit) { /* user has quit the blame view */
1415 err = got_error(GOT_ERR_ITER_COMPLETED);
1420 goto done; /* no change in this commit */
1422 line = &a->lines[lineno - 1];
1423 if (line->annotated)
1426 line->id = got_object_id_dup(id);
1427 if (line->id == NULL) {
1428 err = got_error_from_errno();
1431 line->annotated = 1;
1433 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1434 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1435 a->last_displayed_line, &eof, a->view->nlines);
1437 if (pthread_mutex_unlock(a->mutex) != 0)
1438 return got_error_from_errno();
1442 struct tog_blame_thread_args {
1444 struct got_repository *repo;
1445 struct tog_blame_cb_args *cb_args;
1450 blame_thread(void *arg)
1452 const struct got_error *err;
1453 struct tog_blame_thread_args *ta = arg;
1454 struct tog_blame_cb_args *a = ta->cb_args;
1457 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1458 blame_cb, ta->cb_args);
1460 if (pthread_mutex_lock(a->mutex) != 0)
1461 return (void *)got_error_from_errno();
1463 got_repo_close(ta->repo);
1467 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1468 a->lines, a->nlines, 1, *a->selected_line,
1469 a->first_displayed_line, a->last_displayed_line, &eof,
1472 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1473 err = got_error_from_errno();
1478 static struct got_object_id *
1479 get_selected_commit_id(struct tog_blame_line *lines,
1480 int first_displayed_line, int selected_line)
1482 struct tog_blame_line *line;
1484 line = &lines[first_displayed_line - 1 + selected_line - 1];
1485 if (!line->annotated)
1491 static const struct got_error *
1492 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1493 struct tog_blame_line *lines, int first_displayed_line,
1494 int selected_line, struct got_repository *repo)
1496 const struct got_error *err = NULL;
1497 struct got_commit_object *commit = NULL;
1498 struct got_object_id *selected_id;
1499 struct got_object_qid *pid;
1504 selected_id = get_selected_commit_id(lines,
1505 first_displayed_line, selected_line);
1506 if (selected_id == NULL)
1509 err = got_object_open(obj, repo, selected_id);
1513 err = got_object_commit_open(&commit, repo, *obj);
1517 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1519 err = got_object_open(pobj, repo, pid->id);
1525 got_object_commit_close(commit);
1532 struct tog_blame_line *lines;
1535 struct tog_blame_thread_args thread_args;
1536 struct tog_blame_cb_args cb_args;
1540 static const struct got_error *
1541 stop_blame(struct tog_blame *blame)
1543 const struct got_error *err = NULL;
1546 if (blame->thread) {
1547 if (pthread_join(blame->thread, (void **)&err) != 0)
1548 err = got_error_from_errno();
1549 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1551 blame->thread = NULL;
1553 if (blame->thread_args.repo) {
1554 got_repo_close(blame->thread_args.repo);
1555 blame->thread_args.repo = NULL;
1561 for (i = 0; i < blame->nlines; i++)
1562 free(blame->lines[i].id);
1564 blame->lines = NULL;
1565 free(blame->cb_args.commit_id);
1566 blame->cb_args.commit_id = NULL;
1571 static const struct got_error *
1572 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1573 struct tog_view *view, int *blame_complete,
1574 int *first_displayed_line, int *last_displayed_line,
1575 int *selected_line, int *done, const char *path,
1576 struct got_object_id *commit_id,
1577 struct got_repository *repo)
1579 const struct got_error *err = NULL;
1580 struct got_blob_object *blob = NULL;
1581 struct got_repository *thread_repo = NULL;
1582 struct got_object *obj;
1584 err = got_object_open_by_path(&obj, repo, commit_id, path);
1587 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1588 err = got_error(GOT_ERR_OBJ_TYPE);
1592 err = got_object_blob_open(&blob, repo, obj, 8192);
1595 blame->f = got_opentemp();
1596 if (blame->f == NULL) {
1597 err = got_error_from_errno();
1600 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1605 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1606 if (blame->lines == NULL) {
1607 err = got_error_from_errno();
1611 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1615 blame->cb_args.view = view;
1616 blame->cb_args.lines = blame->lines;
1617 blame->cb_args.nlines = blame->nlines;
1618 blame->cb_args.mutex = mutex;
1619 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1620 if (blame->cb_args.commit_id == NULL) {
1621 err = got_error_from_errno();
1624 blame->cb_args.f = blame->f;
1625 blame->cb_args.path = path;
1626 blame->cb_args.first_displayed_line = first_displayed_line;
1627 blame->cb_args.selected_line = selected_line;
1628 blame->cb_args.last_displayed_line = last_displayed_line;
1629 blame->cb_args.quit = done;
1631 blame->thread_args.path = path;
1632 blame->thread_args.repo = thread_repo;
1633 blame->thread_args.cb_args = &blame->cb_args;
1634 blame->thread_args.complete = blame_complete;
1635 *blame_complete = 0;
1637 if (pthread_create(&blame->thread, NULL, blame_thread,
1638 &blame->thread_args) != 0) {
1639 err = got_error_from_errno();
1645 got_object_blob_close(blob);
1647 got_object_close(obj);
1653 static const struct got_error *
1654 show_blame_view(const char *path, struct got_object_id *commit_id,
1655 struct got_repository *repo)
1657 const struct got_error *err = NULL, *thread_err = NULL;
1658 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1659 int selected_line = first_displayed_line;
1660 int eof, blame_complete = 0;
1661 struct got_object *obj = NULL, *pobj = NULL;
1662 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1663 struct tog_blame blame;
1664 struct got_object_id_queue blamed_commits;
1665 struct got_object_qid *blamed_commit = NULL;
1666 struct tog_view *view = NULL, *diff_view;
1668 SIMPLEQ_INIT(&blamed_commits);
1670 if (pthread_mutex_init(&mutex, NULL) != 0) {
1671 err = got_error_from_errno();
1675 err = got_object_qid_alloc(&blamed_commit, commit_id);
1678 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1680 view = open_view(0, 0, 0, 0);
1682 err = got_error_from_errno();
1685 show_panel(view->panel);
1686 last_displayed_line = view->nlines;
1688 memset(&blame, 0, sizeof(blame));
1689 err = run_blame(&blame, &mutex, view, &blame_complete,
1690 &first_displayed_line, &last_displayed_line,
1691 &selected_line, &done, path, blamed_commit->id, repo);
1696 if (pthread_mutex_lock(&mutex) != 0) {
1697 err = got_error_from_errno();
1700 err = draw_blame(view, blamed_commit->id, blame.f, path,
1701 blame.lines, blame.nlines, blame_complete, selected_line,
1702 &first_displayed_line, &last_displayed_line, &eof,
1704 if (pthread_mutex_unlock(&mutex) != 0) {
1705 err = got_error_from_errno();
1710 nodelay(stdscr, FALSE);
1711 ch = wgetch(view->window);
1712 nodelay(stdscr, TRUE);
1713 if (pthread_mutex_lock(&mutex) != 0) {
1714 err = got_error_from_errno();
1723 if (selected_line > 1)
1725 else if (selected_line == 1 &&
1726 first_displayed_line > 1)
1727 first_displayed_line--;
1731 if (first_displayed_line == 1) {
1735 if (first_displayed_line > view->nlines - 2)
1736 first_displayed_line -=
1739 first_displayed_line = 1;
1743 if (selected_line < view->nlines - 2 &&
1744 first_displayed_line + selected_line <=
1747 else if (last_displayed_line < blame.nlines)
1748 first_displayed_line++;
1752 struct got_object_id *id;
1753 id = get_selected_commit_id(blame.lines,
1754 first_displayed_line, selected_line);
1755 if (id == NULL || got_object_id_cmp(id,
1756 blamed_commit->id) == 0)
1758 err = open_selected_commit(&pobj, &obj,
1759 blame.lines, first_displayed_line,
1760 selected_line, repo);
1763 if (pobj == NULL && obj == NULL)
1765 if (ch == 'p' && pobj == NULL)
1768 if (pthread_mutex_unlock(&mutex) != 0) {
1769 err = got_error_from_errno();
1772 thread_err = stop_blame(&blame);
1774 if (pthread_mutex_lock(&mutex) != 0) {
1775 err = got_error_from_errno();
1780 id = got_object_get_id(ch == 'b' ? obj : pobj);
1781 got_object_close(obj);
1784 got_object_close(pobj);
1788 err = got_error_from_errno();
1791 err = got_object_qid_alloc(&blamed_commit, id);
1795 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1796 blamed_commit, entry);
1797 err = run_blame(&blame, &mutex, view,
1798 &blame_complete, &first_displayed_line,
1799 &last_displayed_line, &selected_line,
1800 &done, path, blamed_commit->id, repo);
1806 struct got_object_qid *first;
1807 first = SIMPLEQ_FIRST(&blamed_commits);
1808 if (!got_object_id_cmp(first->id, commit_id))
1811 if (pthread_mutex_unlock(&mutex) != 0) {
1812 err = got_error_from_errno();
1815 thread_err = stop_blame(&blame);
1817 if (pthread_mutex_lock(&mutex) != 0) {
1818 err = got_error_from_errno();
1823 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1824 got_object_qid_free(blamed_commit);
1825 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1826 err = run_blame(&blame, &mutex, view,
1827 &blame_complete, &first_displayed_line,
1828 &last_displayed_line, &selected_line,
1829 &done, path, blamed_commit->id, repo);
1836 err = open_selected_commit(&pobj, &obj,
1837 blame.lines, first_displayed_line,
1838 selected_line, repo);
1841 if (pobj == NULL && obj == NULL)
1843 diff_view = open_view(0, 0, 0, 0);
1844 if (diff_view == NULL) {
1845 err = got_error_from_errno();
1848 err = show_diff_view(diff_view, pobj, obj, repo);
1849 close_view(diff_view);
1851 got_object_close(pobj);
1854 got_object_close(obj);
1856 show_panel(view->panel);
1862 if (last_displayed_line >= blame.nlines &&
1863 selected_line < view->nlines - 2) {
1864 selected_line = MIN(blame.nlines,
1868 if (last_displayed_line + view->nlines - 2 <=
1870 first_displayed_line +=
1873 first_displayed_line =
1874 blame.nlines - (view->nlines - 3);
1877 err = view_resize(view);
1880 if (selected_line > view->nlines - 2) {
1881 selected_line = MIN(blame.nlines,
1888 if (pthread_mutex_unlock(&mutex) != 0)
1889 err = got_error_from_errno();
1890 if (err || thread_err)
1895 got_object_close(pobj);
1897 thread_err = stop_blame(&blame);
1900 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1901 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1902 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1903 got_object_qid_free(blamed_commit);
1905 return thread_err ? thread_err : err;
1908 static const struct got_error *
1909 cmd_blame(int argc, char *argv[])
1911 const struct got_error *error;
1912 struct got_repository *repo = NULL;
1913 char *repo_path = NULL;
1915 struct got_object_id *commit_id = NULL;
1916 char *commit_id_str = NULL;
1920 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1924 while ((ch = getopt(argc, argv, "c:")) != -1) {
1927 commit_id_str = optarg;
1940 } else if (argc == 1) {
1941 repo_path = getcwd(NULL, 0);
1942 if (repo_path == NULL)
1943 return got_error_from_errno();
1945 } else if (argc == 2) {
1946 repo_path = realpath(argv[0], NULL);
1947 if (repo_path == NULL)
1948 return got_error_from_errno();
1953 error = got_repo_open(&repo, repo_path);
1958 if (commit_id_str == NULL) {
1959 struct got_reference *head_ref;
1960 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1963 error = got_ref_resolve(&commit_id, repo, head_ref);
1964 got_ref_close(head_ref);
1966 struct got_object *obj;
1967 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1970 commit_id = got_object_get_id(obj);
1971 if (commit_id == NULL)
1972 error = got_error_from_errno();
1973 got_object_close(obj);
1978 error = show_blame_view(path, commit_id, repo);
1982 got_repo_close(repo);
1986 static const struct got_error *
1987 draw_tree_entries(struct tog_view *view,
1988 struct got_tree_entry **first_displayed_entry,
1989 struct got_tree_entry **last_displayed_entry,
1990 struct got_tree_entry **selected_entry, int *ndisplayed,
1991 const char *label, int show_ids, const char *parent_path,
1992 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1994 const struct got_error *err = NULL;
1995 struct got_tree_entry *te;
2001 werase(view->window);
2006 err = format_line(&wline, &width, label, view->ncols);
2009 waddwstr(view->window, wline);
2012 if (width < view->ncols)
2013 waddch(view->window, '\n');
2016 err = format_line(&wline, &width, parent_path, view->ncols);
2019 waddwstr(view->window, wline);
2022 if (width < view->ncols)
2023 waddch(view->window, '\n');
2026 waddch(view->window, '\n');
2030 te = SIMPLEQ_FIRST(&entries->head);
2031 if (*first_displayed_entry == NULL) {
2032 if (selected == 0) {
2033 wstandout(view->window);
2034 *selected_entry = NULL;
2036 waddstr(view->window, " ..\n"); /* parent directory */
2038 wstandend(view->window);
2045 while (te != *first_displayed_entry)
2046 te = SIMPLEQ_NEXT(te, entry);
2050 char *line = NULL, *id_str = NULL;
2053 err = got_object_id_str(&id_str, te->id);
2055 return got_error_from_errno();
2057 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2058 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2060 return got_error_from_errno();
2063 err = format_line(&wline, &width, line, view->ncols);
2068 if (n == selected) {
2069 wstandout(view->window);
2070 *selected_entry = te;
2072 waddwstr(view->window, wline);
2073 if (width < view->ncols)
2074 waddch(view->window, '\n');
2076 wstandend(view->window);
2082 *last_displayed_entry = te;
2085 te = SIMPLEQ_NEXT(te, entry);
2092 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2093 const struct got_tree_entries *entries, int isroot)
2095 struct got_tree_entry *te, *prev;
2098 if (*first_displayed_entry == NULL)
2101 te = SIMPLEQ_FIRST(&entries->head);
2102 if (*first_displayed_entry == te) {
2104 *first_displayed_entry = NULL;
2108 /* XXX this is stupid... switch to TAILQ? */
2109 for (i = 0; i < maxscroll; i++) {
2110 while (te != *first_displayed_entry) {
2112 te = SIMPLEQ_NEXT(te, entry);
2114 *first_displayed_entry = prev;
2115 te = SIMPLEQ_FIRST(&entries->head);
2117 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2118 *first_displayed_entry = NULL;
2122 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2123 struct got_tree_entry *last_displayed_entry,
2124 const struct got_tree_entries *entries)
2126 struct got_tree_entry *next;
2129 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2132 if (*first_displayed_entry)
2133 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2135 next = SIMPLEQ_FIRST(&entries->head);
2137 *first_displayed_entry = next;
2138 if (++n >= maxscroll)
2140 next = SIMPLEQ_NEXT(next, entry);
2144 struct tog_parent_tree {
2145 TAILQ_ENTRY(tog_parent_tree) entry;
2146 struct got_tree_object *tree;
2147 struct got_tree_entry *first_displayed_entry;
2148 struct got_tree_entry *selected_entry;
2152 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2154 static const struct got_error *
2155 tree_entry_path(char **path, struct tog_parent_trees *parents,
2156 struct got_tree_entry *te)
2158 const struct got_error *err = NULL;
2159 struct tog_parent_tree *pt;
2160 size_t len = 2; /* for leading slash and NUL */
2162 TAILQ_FOREACH(pt, parents, entry)
2163 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2165 len += strlen(te->name);
2167 *path = calloc(1, len);
2169 return got_error_from_errno();
2172 pt = TAILQ_LAST(parents, tog_parent_trees);
2174 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2175 err = got_error(GOT_ERR_NO_SPACE);
2178 if (strlcat(*path, "/", len) >= len) {
2179 err = got_error(GOT_ERR_NO_SPACE);
2182 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2185 if (strlcat(*path, te->name, len) >= len) {
2186 err = got_error(GOT_ERR_NO_SPACE);
2198 static const struct got_error *
2199 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2200 struct got_object_id *commit_id, struct got_repository *repo)
2202 const struct got_error *err = NULL;
2205 err = tree_entry_path(&path, parents, te);
2209 err = show_blame_view(path, commit_id, repo);
2214 static const struct got_error *
2215 log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2216 struct got_object_id *commit_id, struct got_repository *repo)
2218 const struct got_error *err = NULL;
2221 err = tree_entry_path(&path, parents, te);
2225 err = show_log_view(commit_id, repo, path);
2230 static const struct got_error *
2231 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2232 struct got_repository *repo)
2234 const struct got_error *err = NULL;
2235 int ch, done = 0, selected = 0, show_ids = 0;
2236 struct got_tree_object *tree = root;
2237 const struct got_tree_entries *entries;
2238 struct got_tree_entry *first_displayed_entry = NULL;
2239 struct got_tree_entry *last_displayed_entry = NULL;
2240 struct got_tree_entry *selected_entry = NULL;
2241 char *commit_id_str = NULL, *tree_label = NULL;
2242 int nentries, ndisplayed;
2243 struct tog_parent_trees parents;
2244 struct tog_view *view = NULL;
2246 TAILQ_INIT(&parents);
2248 err = got_object_id_str(&commit_id_str, commit_id);
2252 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2253 err = got_error_from_errno();
2257 view = open_view(0, 0, 0, 0);
2259 err = got_error_from_errno();
2262 show_panel(view->panel);
2264 entries = got_object_tree_get_entries(root);
2265 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2268 entries = got_object_tree_get_entries(tree);
2269 nentries = entries->nentries;
2271 nentries++; /* '..' directory */
2273 err = tree_entry_path(&parent_path, &parents, NULL);
2277 err = draw_tree_entries(view, &first_displayed_entry,
2278 &last_displayed_entry, &selected_entry, &ndisplayed,
2279 tree_label, show_ids, parent_path, entries, selected,
2280 view->nlines, tree == root);
2285 nodelay(stdscr, FALSE);
2286 ch = wgetch(view->window);
2287 nodelay(stdscr, TRUE);
2293 show_ids = !show_ids;
2296 if (selected_entry) {
2297 err = log_tree_entry(selected_entry,
2298 &parents, commit_id, repo);
2309 tree_scroll_up(&first_displayed_entry, 1,
2310 entries, tree == root);
2313 if (SIMPLEQ_FIRST(&entries->head) ==
2314 first_displayed_entry) {
2316 first_displayed_entry = NULL;
2320 tree_scroll_up(&first_displayed_entry,
2321 view->nlines, entries, tree == root);
2325 if (selected < ndisplayed - 1) {
2329 tree_scroll_down(&first_displayed_entry, 1,
2330 last_displayed_entry, entries);
2333 tree_scroll_down(&first_displayed_entry,
2334 view->nlines, last_displayed_entry,
2336 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2338 /* can't scroll any further; move cursor down */
2339 if (selected < ndisplayed - 1)
2340 selected = ndisplayed - 1;
2344 if (selected_entry == NULL) {
2345 struct tog_parent_tree *parent;
2347 /* user selected '..' */
2350 parent = TAILQ_FIRST(&parents);
2351 TAILQ_REMOVE(&parents, parent, entry);
2352 got_object_tree_close(tree);
2353 tree = parent->tree;
2354 first_displayed_entry =
2355 parent->first_displayed_entry;
2356 selected_entry = parent->selected_entry;
2357 selected = parent->selected;
2359 } else if (S_ISDIR(selected_entry->mode)) {
2360 struct tog_parent_tree *parent;
2361 struct got_tree_object *child;
2362 err = got_object_open_as_tree(
2363 &child, repo, selected_entry->id);
2366 parent = calloc(1, sizeof(*parent));
2367 if (parent == NULL) {
2368 err = got_error_from_errno();
2371 parent->tree = tree;
2372 parent->first_displayed_entry =
2373 first_displayed_entry;
2374 parent->selected_entry = selected_entry;
2375 parent->selected = selected;
2376 TAILQ_INSERT_HEAD(&parents, parent,
2380 first_displayed_entry = NULL;
2381 } else if (S_ISREG(selected_entry->mode)) {
2382 err = blame_tree_entry(selected_entry,
2383 &parents, commit_id, repo);
2389 err = view_resize(view);
2392 if (selected > view->nlines)
2393 selected = ndisplayed - 1;
2403 free(commit_id_str);
2404 while (!TAILQ_EMPTY(&parents)) {
2405 struct tog_parent_tree *parent;
2406 parent = TAILQ_FIRST(&parents);
2407 TAILQ_REMOVE(&parents, parent, entry);
2412 got_object_tree_close(tree);
2420 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2425 static const struct got_error *
2426 cmd_tree(int argc, char *argv[])
2428 const struct got_error *error;
2429 struct got_repository *repo = NULL;
2430 char *repo_path = NULL;
2431 struct got_object_id *commit_id = NULL;
2432 char *commit_id_arg = NULL;
2433 struct got_commit_object *commit = NULL;
2434 struct got_tree_object *tree = NULL;
2438 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2442 while ((ch = getopt(argc, argv, "c:")) != -1) {
2445 commit_id_arg = optarg;
2457 repo_path = getcwd(NULL, 0);
2458 if (repo_path == NULL)
2459 return got_error_from_errno();
2460 } else if (argc == 1) {
2461 repo_path = realpath(argv[0], NULL);
2462 if (repo_path == NULL)
2463 return got_error_from_errno();
2467 error = got_repo_open(&repo, repo_path);
2472 if (commit_id_arg == NULL) {
2473 error = get_head_commit_id(&commit_id, repo);
2477 struct got_object *obj;
2478 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2479 if (error == NULL) {
2480 commit_id = got_object_get_id(obj);
2481 if (commit_id == NULL)
2482 error = got_error_from_errno();
2488 error = got_object_open_as_commit(&commit, repo, commit_id);
2492 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2496 error = show_tree_view(tree, commit_id, repo);
2500 got_object_commit_close(commit);
2502 got_object_tree_close(tree);
2504 got_repo_close(repo);
2514 intrflush(stdscr, FALSE);
2515 keypad(stdscr, TRUE);
2524 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2525 "Available commands:\n", getprogname());
2526 for (i = 0; i < nitems(tog_commands); i++) {
2527 struct tog_cmd *cmd = &tog_commands[i];
2528 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2534 make_argv(const char *arg0, const char *arg1)
2537 int argc = (arg1 == NULL ? 1 : 2);
2539 argv = calloc(argc, sizeof(char *));
2542 argv[0] = strdup(arg0);
2543 if (argv[0] == NULL)
2546 argv[1] = strdup(arg1);
2547 if (argv[1] == NULL)
2555 main(int argc, char *argv[])
2557 const struct got_error *error = NULL;
2558 struct tog_cmd *cmd = NULL;
2560 char **cmd_argv = NULL;
2562 setlocale(LC_ALL, "");
2564 while ((ch = getopt(argc, argv, "h")) != -1) {
2583 /* Build an argument vector which runs a default command. */
2584 cmd = &tog_commands[0];
2585 cmd_argv = make_argv(cmd->name, NULL);
2590 /* Did the user specific a command? */
2591 for (i = 0; i < nitems(tog_commands); i++) {
2592 if (strncmp(tog_commands[i].name, argv[0],
2593 strlen(argv[0])) == 0) {
2594 cmd = &tog_commands[i];
2596 tog_commands[i].cmd_usage();
2601 /* Did the user specify a repository? */
2602 char *repo_path = realpath(argv[0], NULL);
2604 struct got_repository *repo;
2605 error = got_repo_open(&repo, repo_path);
2607 got_repo_close(repo);
2609 error = got_error_from_errno();
2612 fprintf(stderr, "%s: '%s' is not a "
2613 "known command\n", getprogname(),
2617 fprintf(stderr, "%s: '%s' is neither a known "
2618 "command nor a path to a repository\n",
2619 getprogname(), argv[0]);
2623 cmd = &tog_commands[0];
2624 cmd_argv = make_argv(cmd->name, repo_path);
2632 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2639 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);