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 *);
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] [repository-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;
165 err = mbs2ws(&wline, &wlen, line);
170 while (i < wlen && cols <= wlimit) {
171 int width = wcwidth(wline[i]);
180 if (wline[i] == L'\t')
181 cols += TABSIZE - (cols % TABSIZE);
184 err = got_error_from_errno();
202 static const struct got_error *
203 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
205 const struct got_error *err = NULL;
206 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
207 char *logmsg0 = NULL, *logmsg = NULL;
208 char *author0 = NULL, *author = NULL;
209 wchar_t *wlogmsg = NULL, *wauthor = NULL;
210 int author_width, logmsg_width;
211 char *newline, *smallerthan;
216 static const size_t date_display_cols = 9;
217 static const size_t id_display_cols = 8;
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 err = got_object_id_str(&id_str, id);
237 id_len = strlen(id_str);
238 if (avail < date_display_cols + id_display_cols) {
239 limit = MIN(id_len, avail - date_display_cols);
240 waddnstr(tog_log_view.window, id_str, limit);
242 limit = MIN(id_display_cols, id_len);
243 waddnstr(tog_log_view.window, id_str, limit);
246 while (col <= avail && col < date_display_cols + id_display_cols + 2) {
247 waddch(tog_log_view.window, ' ');
253 author0 = strdup(commit->author);
254 if (author0 == NULL) {
255 err = got_error_from_errno();
259 smallerthan = strchr(author, '<');
263 char *at = strchr(author, '@');
268 err = format_line(&wauthor, &author_width, author, limit);
271 waddwstr(tog_log_view.window, wauthor);
273 while (col <= avail && author_width < author_display_cols + 1) {
274 waddch(tog_log_view.window, ' ');
281 logmsg0 = strdup(commit->logmsg);
282 if (logmsg0 == NULL) {
283 err = got_error_from_errno();
287 while (*logmsg == '\n')
289 newline = strchr(logmsg, '\n');
293 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
296 waddwstr(tog_log_view.window, wlogmsg);
298 while (col <= avail) {
299 waddch(tog_log_view.window, ' ');
312 struct commit_queue_entry {
313 TAILQ_ENTRY(commit_queue_entry) entry;
314 struct got_object_id *id;
315 struct got_commit_object *commit;
317 TAILQ_HEAD(commit_queue, commit_queue_entry);
319 static struct commit_queue_entry *
320 alloc_commit_queue_entry(struct got_commit_object *commit,
321 struct got_object_id *id)
323 struct commit_queue_entry *entry;
325 entry = calloc(1, sizeof(*entry));
330 entry->commit = commit;
335 pop_commit(struct commit_queue *commits)
337 struct commit_queue_entry *entry;
339 entry = TAILQ_FIRST(commits);
340 TAILQ_REMOVE(commits, entry, entry);
341 got_object_commit_close(entry->commit);
342 /* Don't free entry->id! It is owned by the commit graph. */
347 free_commits(struct commit_queue *commits)
349 while (!TAILQ_EMPTY(commits))
353 static const struct got_error *
354 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
355 struct got_object_id *start_id, struct got_repository *repo)
357 const struct got_error *err = NULL;
358 struct got_object_id *id;
359 struct commit_queue_entry *entry;
361 err = got_commit_graph_iter_start(graph, start_id);
365 entry = TAILQ_LAST(commits, commit_queue);
366 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
369 /* Start ID's commit is already on the queue; skip over it. */
370 err = got_commit_graph_iter_next(&id, graph);
371 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
374 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
380 struct got_commit_object *commit;
382 err = got_commit_graph_iter_next(&id, graph);
384 if (err->code == GOT_ERR_ITER_NEED_MORE)
389 err = got_object_open_as_commit(&commit, repo, id);
393 entry = alloc_commit_queue_entry(commit, id);
395 err = got_error_from_errno();
399 TAILQ_INSERT_TAIL(commits, entry, entry);
405 static const struct got_error *
406 fetch_next_commit(struct commit_queue_entry **pentry,
407 struct commit_queue_entry *entry, struct commit_queue *commits,
408 struct got_commit_graph *graph, struct got_repository *repo)
410 const struct got_error *err = NULL;
411 struct got_object_qid *qid;
415 /* Populate commit graph with entry's parent commits. */
416 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
418 err = got_commit_graph_fetch_commits_up_to(&nfetched,
419 graph, qid->id, repo);
424 /* Append outstanding commits to queue in graph sort order. */
425 err = queue_commits(graph, commits, entry->id, repo);
427 if (err->code == GOT_ERR_ITER_COMPLETED)
432 /* Next entry to display should now be available. */
433 *pentry = TAILQ_NEXT(entry, entry);
435 return got_error(GOT_ERR_NO_OBJ);
440 static const struct got_error *
441 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
443 const struct got_error *err = NULL;
444 struct got_reference *head_ref;
448 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
452 err = got_ref_resolve(head_id, repo, head_ref);
453 got_ref_close(head_ref);
462 static const struct got_error *
463 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
464 struct commit_queue_entry *first, int selected_idx, int limit)
466 const struct got_error *err = NULL;
467 struct commit_queue_entry *entry;
470 werase(tog_log_view.window);
475 if (ncommits == limit)
477 if (ncommits == selected_idx) {
478 wstandout(tog_log_view.window);
481 err = draw_commit(entry->commit, entry->id);
482 if (ncommits == selected_idx)
483 wstandend(tog_log_view.window);
488 entry = TAILQ_NEXT(entry, entry);
498 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
499 struct commit_queue *commits)
501 struct commit_queue_entry *entry;
504 entry = TAILQ_FIRST(commits);
505 if (*first_displayed_entry == entry)
508 entry = *first_displayed_entry;
509 while (entry && nscrolled < maxscroll) {
510 entry = TAILQ_PREV(entry, commit_queue, entry);
512 *first_displayed_entry = entry;
518 static const struct got_error *
519 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
520 struct commit_queue_entry *last_displayed_entry,
521 struct commit_queue *commits, struct got_commit_graph *graph,
522 struct got_repository *repo)
524 const struct got_error *err = NULL;
525 struct commit_queue_entry *pentry;
529 pentry = TAILQ_NEXT(last_displayed_entry, entry);
530 if (pentry == NULL) {
531 err = fetch_next_commit(&pentry, last_displayed_entry,
532 commits, graph, repo);
533 if (err || pentry == NULL)
536 last_displayed_entry = pentry;
538 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
541 *first_displayed_entry = pentry;
542 } while (++nscrolled < maxscroll);
548 num_parents(struct commit_queue_entry *entry)
553 entry = TAILQ_NEXT(entry, entry);
560 static const struct got_error *
561 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
563 const struct got_error *err;
564 struct got_object *obj1 = NULL, *obj2 = NULL;
565 struct got_object_qid *parent_id;
567 err = got_object_open(&obj2, repo, entry->id);
571 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
573 err = got_object_open(&obj1, repo, parent_id->id);
578 err = show_diff_view(obj1, obj2, repo);
581 got_object_close(obj1);
583 got_object_close(obj2);
587 static const struct got_error *
588 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
590 const struct got_error *err = NULL;
591 struct got_tree_object *tree;
593 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
597 err = show_tree_view(tree, entry->id, repo);
598 got_object_tree_close(tree);
602 static const struct got_error *
603 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
605 const struct got_error *err = NULL;
606 struct got_object_id *head_id = NULL;
607 int ch, done = 0, selected = 0, nparents, nfetched;
608 struct got_commit_graph *graph;
609 struct commit_queue commits;
610 struct commit_queue_entry *entry = NULL;
611 struct commit_queue_entry *first_displayed_entry = NULL;
612 struct commit_queue_entry *last_displayed_entry = NULL;
613 struct commit_queue_entry *selected_entry = NULL;
615 if (tog_log_view.window == NULL) {
616 tog_log_view.window = newwin(0, 0, 0, 0);
617 if (tog_log_view.window == NULL)
618 return got_error_from_errno();
619 keypad(tog_log_view.window, TRUE);
621 if (tog_log_view.panel == NULL) {
622 tog_log_view.panel = new_panel(tog_log_view.window);
623 if (tog_log_view.panel == NULL)
624 return got_error_from_errno();
626 show_panel(tog_log_view.panel);
628 err = get_head_commit_id(&head_id, repo);
632 TAILQ_INIT(&commits);
634 err = got_commit_graph_open(&graph, head_id, 0, repo);
638 /* Populate commit graph with a sufficient number of commits. */
639 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
643 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
648 * Open the initial batch of commits, sorted in commit graph order.
649 * We keep all commits open throughout the lifetime of the log view
650 * in order to avoid having to re-fetch commits from disk while
651 * updating the display.
653 err = queue_commits(graph, &commits, head_id, repo);
654 if (err && err->code != GOT_ERR_ITER_COMPLETED)
657 /* Find entry corresponding to the first commit to display. */
658 TAILQ_FOREACH(entry, &commits, entry) {
659 if (got_object_id_cmp(entry->id, start_id) == 0) {
660 first_displayed_entry = entry;
664 if (first_displayed_entry == NULL) {
665 err = got_error(GOT_ERR_NO_OBJ);
670 err = draw_commits(&last_displayed_entry, &selected_entry,
671 first_displayed_entry, selected, LINES);
675 nodelay(stdscr, FALSE);
676 ch = wgetch(tog_log_view.window);
677 nodelay(stdscr, TRUE);
681 err = got_error_from_errno();
694 scroll_up(&first_displayed_entry, 1, &commits);
697 if (TAILQ_FIRST(&commits) ==
698 first_displayed_entry) {
702 scroll_up(&first_displayed_entry, LINES,
707 nparents = num_parents(first_displayed_entry);
708 if (selected < LINES - 1 &&
709 selected < nparents - 1) {
713 err = scroll_down(&first_displayed_entry, 1,
714 last_displayed_entry, &commits, graph,
720 err = scroll_down(&first_displayed_entry, LINES,
721 last_displayed_entry, &commits, graph,
725 if (last_displayed_entry->commit->nparents > 0)
727 /* can't scroll any further; move cursor down */
728 nparents = num_parents(first_displayed_entry);
729 if (selected < LINES - 1 ||
730 selected < nparents - 1)
731 selected = MIN(LINES - 1, nparents - 1);
734 if (selected > LINES)
735 selected = LINES - 1;
739 err = show_commit(selected_entry, repo);
742 show_panel(tog_log_view.panel);
745 err = browse_commit(selected_entry, repo);
748 show_panel(tog_log_view.panel);
757 got_commit_graph_close(graph);
758 free_commits(&commits);
762 static const struct got_error *
763 cmd_log(int argc, char *argv[])
765 const struct got_error *error;
766 struct got_repository *repo;
767 struct got_object_id *start_id = NULL;
768 char *repo_path = NULL;
769 char *start_commit = NULL;
773 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
777 while ((ch = getopt(argc, argv, "c:")) != -1) {
780 start_commit = optarg;
792 repo_path = getcwd(NULL, 0);
793 if (repo_path == NULL)
794 return got_error_from_errno();
795 } else if (argc == 1) {
796 repo_path = realpath(argv[0], NULL);
797 if (repo_path == NULL)
798 return got_error_from_errno();
802 error = got_repo_open(&repo, repo_path);
807 if (start_commit == NULL) {
808 error = get_head_commit_id(&start_id, repo);
812 struct got_object *obj;
813 error = got_object_open_by_id_str(&obj, repo, start_commit);
815 start_id = got_object_get_id(obj);
816 if (start_id == NULL)
817 error = got_error_from_errno();
822 error = show_log_view(start_id, repo);
824 got_repo_close(repo);
832 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
838 parse_next_line(FILE *f, size_t *len)
843 const char delim[3] = { '\0', '\0', '\0'};
845 line = fparseln(f, &linelen, &lineno, delim, 0);
851 static const struct got_error *
852 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
853 int *last_displayed_line, int *eof, int max_lines)
855 const struct got_error *err;
856 int nlines = 0, nprinted = 0;
866 while (nprinted < max_lines) {
867 line = parse_next_line(f, &len);
872 if (++nlines < *first_displayed_line) {
877 err = format_line(&wline, &width, line, COLS);
882 waddwstr(window, wline);
884 waddch(window, '\n');
886 *first_displayed_line = nlines;
889 *last_displayed_line = nlines;
897 static const struct got_error *
898 show_diff_view(struct got_object *obj1, struct got_object *obj2,
899 struct got_repository *repo)
901 const struct got_error *err;
903 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
906 if (obj1 != NULL && obj2 != NULL &&
907 got_object_get_type(obj1) != got_object_get_type(obj2))
908 return got_error(GOT_ERR_OBJ_TYPE);
912 return got_error_from_errno();
914 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
915 case GOT_OBJ_TYPE_BLOB:
916 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
918 case GOT_OBJ_TYPE_TREE:
919 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
921 case GOT_OBJ_TYPE_COMMIT:
922 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
925 return got_error(GOT_ERR_OBJ_TYPE);
930 if (tog_diff_view.window == NULL) {
931 tog_diff_view.window = newwin(0, 0, 0, 0);
932 if (tog_diff_view.window == NULL)
933 return got_error_from_errno();
934 keypad(tog_diff_view.window, TRUE);
936 if (tog_diff_view.panel == NULL) {
937 tog_diff_view.panel = new_panel(tog_diff_view.window);
938 if (tog_diff_view.panel == NULL)
939 return got_error_from_errno();
941 show_panel(tog_diff_view.panel);
944 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
945 &last_displayed_line, &eof, LINES);
948 nodelay(stdscr, FALSE);
949 ch = wgetch(tog_diff_view.window);
950 nodelay(stdscr, TRUE);
958 if (first_displayed_line > 1)
959 first_displayed_line--;
963 while (i++ < LINES - 1 &&
964 first_displayed_line > 1)
965 first_displayed_line--;
972 first_displayed_line++;
977 while (!eof && i++ < LINES - 1) {
978 char *line = parse_next_line(f, NULL);
979 first_displayed_line++;
992 static const struct got_error *
993 cmd_diff(int argc, char *argv[])
995 const struct got_error *error = NULL;
996 struct got_repository *repo = NULL;
997 struct got_object *obj1 = NULL, *obj2 = NULL;
998 char *repo_path = NULL;
999 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1003 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1007 while ((ch = getopt(argc, argv, "")) != -1) {
1019 usage_diff(); /* TODO show local worktree changes */
1020 } else if (argc == 2) {
1021 repo_path = getcwd(NULL, 0);
1022 if (repo_path == NULL)
1023 return got_error_from_errno();
1024 obj_id_str1 = argv[0];
1025 obj_id_str2 = argv[1];
1026 } else if (argc == 3) {
1027 repo_path = realpath(argv[0], NULL);
1028 if (repo_path == NULL)
1029 return got_error_from_errno();
1030 obj_id_str1 = argv[1];
1031 obj_id_str2 = argv[2];
1035 error = got_repo_open(&repo, repo_path);
1040 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1044 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1048 error = show_diff_view(obj1, obj2, repo);
1050 got_repo_close(repo);
1052 got_object_close(obj1);
1054 got_object_close(obj2);
1062 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1067 struct tog_blame_line {
1069 struct got_object_id *id;
1072 static const struct got_error *
1073 draw_blame(WINDOW *window, FILE *f, const char *path,
1074 struct tog_blame_line *lines, int nlines, int blame_complete,
1075 int *first_displayed_line, int *last_displayed_line,
1076 int *eof, int max_lines)
1078 const struct got_error *err;
1079 int lineno = 0, nprinted = 0;
1084 struct tog_blame_line *blame_line;
1089 if (asprintf(&line, "[%d-%d/%d] annotation of %s%s",
1090 *first_displayed_line, *last_displayed_line, nlines,
1091 path, blame_complete ? "" : " in progress...") == -1)
1092 return got_error_from_errno();
1093 err = format_line(&wline, &width, line, COLS);
1097 waddwstr(window, wline);
1099 waddch(window, '\n');
1102 while (nprinted < max_lines - 1) {
1103 line = parse_next_line(f, &len);
1108 if (++lineno < *first_displayed_line) {
1113 err = format_line(&wline, &width, line, COLS - 9);
1119 blame_line = &lines[lineno - 1];
1120 if (blame_line->annotated) {
1122 err = got_object_id_str(&id_str, blame_line->id);
1127 wprintw(window, "%.8s ", id_str);
1130 waddstr(window, " ");
1132 waddwstr(window, wline);
1133 if (width < COLS - 9)
1134 waddch(window, '\n');
1135 if (++nprinted == 1)
1136 *first_displayed_line = lineno;
1139 *last_displayed_line = lineno;
1147 struct tog_blame_cb_args {
1148 pthread_mutex_t *mutex;
1149 struct tog_blame_line *lines; /* one per line */
1155 int *first_displayed_line;
1156 int *last_displayed_line;
1160 static const struct got_error *
1161 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1163 const struct got_error *err = NULL;
1164 struct tog_blame_cb_args *a = arg;
1165 struct tog_blame_line *line;
1168 if (nlines != a->nlines ||
1169 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1170 return got_error(GOT_ERR_RANGE);
1172 if (pthread_mutex_lock(a->mutex) != 0)
1173 return got_error_from_errno();
1175 if (*a->quit) { /* user has quit the blame view */
1176 err = got_error(GOT_ERR_ITER_COMPLETED);
1181 goto done; /* no change in this commit */
1183 line = &a->lines[lineno - 1];
1184 if (line->annotated)
1187 line->id = got_object_id_dup(id);
1188 if (line->id == NULL) {
1189 err = got_error_from_errno();
1192 line->annotated = 1;
1194 err = draw_blame(a->window, a->f, a->path, a->lines, a->nlines, 0,
1195 a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1197 if (pthread_mutex_unlock(a->mutex) != 0)
1198 return got_error_from_errno();
1202 struct tog_blame_thread_args {
1204 struct got_object_id *commit_id;
1205 struct got_repository *repo;
1206 void *blame_cb_args;
1211 blame_thread(void *arg)
1213 const struct got_error *err;
1214 struct tog_blame_thread_args *ta = arg;
1215 struct tog_blame_cb_args *a = ta->blame_cb_args;
1218 err = got_blame_incremental(ta->path, ta->commit_id, ta->repo,
1219 blame_cb, ta->blame_cb_args);
1224 if (pthread_mutex_lock(a->mutex) != 0)
1225 return (void *)got_error_from_errno();
1227 err = draw_blame(a->window, a->f, a->path, a->lines, a->nlines, 1,
1228 a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1230 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1231 err = got_error_from_errno();
1236 static const struct got_error *
1237 show_blame_view(const char *path, struct got_object_id *commit_id,
1238 struct got_repository *repo)
1240 const struct got_error *err = NULL;
1241 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1242 int eof, i, blame_complete = 0;
1243 struct got_object *obj = NULL;
1244 struct got_blob_object *blob = NULL;
1246 size_t filesize, nlines = 0;
1247 struct tog_blame_line *lines = NULL;
1248 pthread_t thread = NULL;
1249 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1250 struct tog_blame_cb_args blame_cb_args;
1251 struct tog_blame_thread_args blame_thread_args;
1253 err = got_object_open_by_path(&obj, repo, commit_id, path);
1256 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1257 err = got_error(GOT_ERR_OBJ_TYPE);
1258 got_object_close(obj);
1262 err = got_object_blob_open(&blob, repo, obj, 8192);
1263 got_object_close(obj);
1268 err = got_error_from_errno();
1271 err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1275 lines = calloc(nlines, sizeof(*lines));
1276 if (lines == NULL) {
1277 err = got_error_from_errno();
1281 if (tog_blame_view.window == NULL) {
1282 tog_blame_view.window = newwin(0, 0, 0, 0);
1283 if (tog_blame_view.window == NULL)
1284 return got_error_from_errno();
1285 keypad(tog_blame_view.window, TRUE);
1287 if (tog_blame_view.panel == NULL) {
1288 tog_blame_view.panel = new_panel(tog_blame_view.window);
1289 if (tog_blame_view.panel == NULL)
1290 return got_error_from_errno();
1292 show_panel(tog_blame_view.panel);
1294 if (pthread_mutex_init(&mutex, NULL) != 0) {
1295 err = got_error_from_errno();
1298 blame_cb_args.lines = lines;
1299 blame_cb_args.nlines = nlines;
1300 blame_cb_args.mutex = &mutex;
1301 blame_cb_args.f = f;
1302 blame_cb_args.path = path;
1303 blame_cb_args.window = tog_blame_view.window;
1304 blame_cb_args.first_displayed_line = &first_displayed_line;
1305 blame_cb_args.last_displayed_line = &last_displayed_line;
1306 blame_cb_args.quit = &done;
1308 blame_thread_args.path = path;
1309 blame_thread_args.commit_id = commit_id;
1310 blame_thread_args.repo = repo;
1311 blame_thread_args.blame_cb_args = &blame_cb_args;
1312 blame_thread_args.complete = &blame_complete;
1314 if (pthread_create(&thread, NULL, blame_thread,
1315 &blame_thread_args) != 0) {
1316 err = got_error_from_errno();
1321 if (pthread_mutex_lock(&mutex) != 0) {
1322 err = got_error_from_errno();
1325 err = draw_blame(tog_blame_view.window, f, path, lines, nlines,
1326 blame_complete, &first_displayed_line, &last_displayed_line,
1328 if (pthread_mutex_unlock(&mutex) != 0) {
1329 err = got_error_from_errno();
1334 nodelay(stdscr, FALSE);
1335 ch = wgetch(tog_blame_view.window);
1336 nodelay(stdscr, TRUE);
1337 if (pthread_mutex_lock(&mutex) != 0) {
1338 err = got_error_from_errno();
1348 if (first_displayed_line > 1)
1349 first_displayed_line--;
1353 while (i++ < LINES - 1 &&
1354 first_displayed_line > 1)
1355 first_displayed_line--;
1362 first_displayed_line++;
1367 while (!eof && i++ < LINES - 1) {
1368 char *line = parse_next_line(f, NULL);
1369 first_displayed_line++;
1377 if (pthread_mutex_unlock(&mutex) != 0) {
1378 err = got_error_from_errno();
1384 if (pthread_join(thread, (void **)&err) != 0)
1385 err = got_error_from_errno();
1386 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1390 got_object_blob_close(blob);
1393 for (i = 0; i < nlines; i++)
1399 static const struct got_error *
1400 cmd_blame(int argc, char *argv[])
1402 const struct got_error *error;
1403 struct got_repository *repo = NULL;
1404 char *repo_path = NULL;
1406 struct got_object_id *commit_id = NULL;
1407 char *commit_id_str = NULL;
1411 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1415 while ((ch = getopt(argc, argv, "c:")) != -1) {
1418 commit_id_str = optarg;
1431 } else if (argc == 1) {
1432 repo_path = getcwd(NULL, 0);
1433 if (repo_path == NULL)
1434 return got_error_from_errno();
1436 } else if (argc == 2) {
1437 repo_path = realpath(argv[0], NULL);
1438 if (repo_path == NULL)
1439 return got_error_from_errno();
1444 error = got_repo_open(&repo, repo_path);
1449 if (commit_id_str == NULL) {
1450 struct got_reference *head_ref;
1451 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1454 error = got_ref_resolve(&commit_id, repo, head_ref);
1455 got_ref_close(head_ref);
1457 struct got_object *obj;
1458 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1461 commit_id = got_object_get_id(obj);
1462 if (commit_id == NULL)
1463 error = got_error_from_errno();
1464 got_object_close(obj);
1469 error = show_blame_view(path, commit_id, repo);
1473 got_repo_close(repo);
1477 static const struct got_error *
1478 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1479 struct got_tree_entry **last_displayed_entry,
1480 struct got_tree_entry **selected_entry, int *ndisplayed,
1481 WINDOW *window, const char *label, const char *parent_path,
1482 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1484 const struct got_error *err = NULL;
1485 struct got_tree_entry *te;
1496 err = format_line(&wline, &width, label, COLS);
1499 waddwstr(window, wline);
1501 waddch(window, '\n');
1504 err = format_line(&wline, &width, parent_path, COLS);
1507 waddwstr(window, wline);
1509 waddch(window, '\n');
1512 waddch(window, '\n');
1516 te = SIMPLEQ_FIRST(&entries->head);
1517 if (*first_displayed_entry == NULL) {
1518 if (selected == 0) {
1520 *selected_entry = NULL;
1522 waddstr(window, " ..\n"); /* parent directory */
1531 while (te != *first_displayed_entry)
1532 te = SIMPLEQ_NEXT(te, entry);
1537 if (asprintf(&line, " %s%s",
1538 te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1539 return got_error_from_errno();
1540 err = format_line(&wline, &width, line, COLS);
1545 if (n == selected) {
1547 *selected_entry = te;
1549 waddwstr(window, wline);
1551 waddch(window, '\n');
1557 *last_displayed_entry = te;
1560 te = SIMPLEQ_NEXT(te, entry);
1567 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1568 const struct got_tree_entries *entries, int isroot)
1570 struct got_tree_entry *te, *prev;
1573 if (*first_displayed_entry == NULL)
1576 te = SIMPLEQ_FIRST(&entries->head);
1577 if (*first_displayed_entry == te) {
1579 *first_displayed_entry = NULL;
1583 /* XXX this is stupid... switch to TAILQ? */
1584 for (i = 0; i < maxscroll; i++) {
1585 while (te != *first_displayed_entry) {
1587 te = SIMPLEQ_NEXT(te, entry);
1589 *first_displayed_entry = prev;
1590 te = SIMPLEQ_FIRST(&entries->head);
1592 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1593 *first_displayed_entry = NULL;
1597 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1598 struct got_tree_entry *last_displayed_entry,
1599 const struct got_tree_entries *entries)
1601 struct got_tree_entry *next;
1604 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1607 if (*first_displayed_entry)
1608 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1610 next = SIMPLEQ_FIRST(&entries->head);
1612 *first_displayed_entry = next;
1613 if (++n >= maxscroll)
1615 next = SIMPLEQ_NEXT(next, entry);
1619 struct tog_parent_tree {
1620 TAILQ_ENTRY(tog_parent_tree) entry;
1621 struct got_tree_object *tree;
1622 struct got_tree_entry *first_displayed_entry;
1623 struct got_tree_entry *selected_entry;
1627 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1629 static const struct got_error *
1630 tree_entry_path(char **path, struct tog_parent_trees *parents,
1631 struct got_tree_entry *te)
1633 const struct got_error *err = NULL;
1634 struct tog_parent_tree *pt;
1635 size_t len = 2; /* for leading slash and NUL */
1637 TAILQ_FOREACH(pt, parents, entry)
1638 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1640 len += strlen(te->name);
1642 *path = calloc(1, len);
1644 return got_error_from_errno();
1647 pt = TAILQ_LAST(parents, tog_parent_trees);
1649 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1650 err = got_error(GOT_ERR_NO_SPACE);
1653 if (strlcat(*path, "/", len) >= len) {
1654 err = got_error(GOT_ERR_NO_SPACE);
1657 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1660 if (strlcat(*path, te->name, len) >= len) {
1661 err = got_error(GOT_ERR_NO_SPACE);
1673 static const struct got_error *
1674 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1675 struct got_object_id *commit_id, struct got_repository *repo)
1677 const struct got_error *err = NULL;
1680 err = tree_entry_path(&path, parents, te);
1684 err = show_blame_view(path, commit_id, repo);
1689 static const struct got_error *
1690 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1691 struct got_repository *repo)
1693 const struct got_error *err = NULL;
1694 int ch, done = 0, selected = 0;
1695 struct got_tree_object *tree = root;
1696 const struct got_tree_entries *entries;
1697 struct got_tree_entry *first_displayed_entry = NULL;
1698 struct got_tree_entry *last_displayed_entry = NULL;
1699 struct got_tree_entry *selected_entry = NULL;
1700 char *commit_id_str = NULL, *tree_label = NULL;
1701 int nentries, ndisplayed;
1702 struct tog_parent_trees parents;
1704 TAILQ_INIT(&parents);
1706 err = got_object_id_str(&commit_id_str, commit_id);
1710 if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1711 err = got_error_from_errno();
1715 if (tog_tree_view.window == NULL) {
1716 tog_tree_view.window = newwin(0, 0, 0, 0);
1717 if (tog_tree_view.window == NULL)
1718 return got_error_from_errno();
1719 keypad(tog_tree_view.window, TRUE);
1721 if (tog_tree_view.panel == NULL) {
1722 tog_tree_view.panel = new_panel(tog_tree_view.window);
1723 if (tog_tree_view.panel == NULL)
1724 return got_error_from_errno();
1726 show_panel(tog_tree_view.panel);
1728 entries = got_object_tree_get_entries(root);
1729 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1732 entries = got_object_tree_get_entries(tree);
1733 nentries = entries->nentries;
1735 nentries++; /* '..' directory */
1737 err = tree_entry_path(&parent_path, &parents, NULL);
1741 err = draw_tree_entries(&first_displayed_entry,
1742 &last_displayed_entry, &selected_entry, &ndisplayed,
1743 tog_tree_view.window, tree_label, parent_path, entries,
1744 selected, LINES, tree == root);
1749 nodelay(stdscr, FALSE);
1750 ch = wgetch(tog_tree_view.window);
1751 nodelay(stdscr, TRUE);
1762 tree_scroll_up(&first_displayed_entry, 1,
1763 entries, tree == root);
1766 if (SIMPLEQ_FIRST(&entries->head) ==
1767 first_displayed_entry) {
1769 first_displayed_entry = NULL;
1773 tree_scroll_up(&first_displayed_entry, LINES,
1774 entries, tree == root);
1778 if (selected < ndisplayed - 1) {
1782 tree_scroll_down(&first_displayed_entry, 1,
1783 last_displayed_entry, entries);
1786 tree_scroll_down(&first_displayed_entry, LINES,
1787 last_displayed_entry, entries);
1788 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1790 /* can't scroll any further; move cursor down */
1791 if (selected < ndisplayed - 1)
1792 selected = ndisplayed - 1;
1796 if (selected_entry == NULL) {
1797 struct tog_parent_tree *parent;
1799 /* user selected '..' */
1802 parent = TAILQ_FIRST(&parents);
1803 TAILQ_REMOVE(&parents, parent, entry);
1804 got_object_tree_close(tree);
1805 tree = parent->tree;
1806 first_displayed_entry =
1807 parent->first_displayed_entry;
1808 selected_entry = parent->selected_entry;
1809 selected = parent->selected;
1811 } else if (S_ISDIR(selected_entry->mode)) {
1812 struct tog_parent_tree *parent;
1813 struct got_tree_object *child;
1814 err = got_object_open_as_tree(
1815 &child, repo, selected_entry->id);
1818 parent = calloc(1, sizeof(*parent));
1819 if (parent == NULL) {
1820 err = got_error_from_errno();
1823 parent->tree = tree;
1824 parent->first_displayed_entry =
1825 first_displayed_entry;
1826 parent->selected_entry = selected_entry;
1827 parent->selected = selected;
1828 TAILQ_INSERT_HEAD(&parents, parent,
1832 first_displayed_entry = NULL;
1833 } else if (S_ISREG(selected_entry->mode)) {
1834 err = blame_tree_entry(selected_entry,
1835 &parents, commit_id, repo);
1841 if (selected > LINES)
1842 selected = ndisplayed - 1;
1850 free(commit_id_str);
1851 while (!TAILQ_EMPTY(&parents)) {
1852 struct tog_parent_tree *parent;
1853 parent = TAILQ_FIRST(&parents);
1854 TAILQ_REMOVE(&parents, parent, entry);
1859 got_object_tree_close(tree);
1867 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1872 static const struct got_error *
1873 cmd_tree(int argc, char *argv[])
1875 const struct got_error *error;
1876 struct got_repository *repo = NULL;
1877 char *repo_path = NULL;
1878 struct got_object_id *commit_id = NULL;
1879 char *commit_id_arg = NULL;
1880 struct got_commit_object *commit = NULL;
1881 struct got_tree_object *tree = NULL;
1885 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1889 while ((ch = getopt(argc, argv, "c:")) != -1) {
1892 commit_id_arg = optarg;
1904 repo_path = getcwd(NULL, 0);
1905 if (repo_path == NULL)
1906 return got_error_from_errno();
1907 } else if (argc == 1) {
1908 repo_path = realpath(argv[0], NULL);
1909 if (repo_path == NULL)
1910 return got_error_from_errno();
1914 error = got_repo_open(&repo, repo_path);
1919 if (commit_id_arg == NULL) {
1920 error = get_head_commit_id(&commit_id, repo);
1924 struct got_object *obj;
1925 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1926 if (error == NULL) {
1927 commit_id = got_object_get_id(obj);
1928 if (commit_id == NULL)
1929 error = got_error_from_errno();
1935 error = got_object_open_as_commit(&commit, repo, commit_id);
1939 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1943 error = show_tree_view(tree, commit_id, repo);
1947 got_object_commit_close(commit);
1949 got_object_tree_close(tree);
1951 got_repo_close(repo);
1961 intrflush(stdscr, FALSE);
1962 keypad(stdscr, TRUE);
1971 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1972 "Available commands:\n", getprogname());
1973 for (i = 0; i < nitems(tog_commands); i++) {
1974 struct tog_cmd *cmd = &tog_commands[i];
1975 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1981 make_argv(const char *arg0, const char *arg1)
1984 int argc = (arg1 == NULL ? 1 : 2);
1986 argv = calloc(argc, sizeof(char *));
1989 argv[0] = strdup(arg0);
1990 if (argv[0] == NULL)
1993 argv[1] = strdup(arg1);
1994 if (argv[1] == NULL)
2002 main(int argc, char *argv[])
2004 const struct got_error *error = NULL;
2005 struct tog_cmd *cmd = NULL;
2007 char **cmd_argv = NULL;
2009 setlocale(LC_ALL, "");
2011 while ((ch = getopt(argc, argv, "h")) != -1) {
2030 /* Build an argument vector which runs a default command. */
2031 cmd = &tog_commands[0];
2032 cmd_argv = make_argv(cmd->name, NULL);
2037 /* Did the user specific a command? */
2038 for (i = 0; i < nitems(tog_commands); i++) {
2039 if (strncmp(tog_commands[i].name, argv[0],
2040 strlen(argv[0])) == 0) {
2041 cmd = &tog_commands[i];
2043 tog_commands[i].cmd_usage();
2048 /* Did the user specify a repository? */
2049 char *repo_path = realpath(argv[0], NULL);
2051 struct got_repository *repo;
2052 error = got_repo_open(&repo, repo_path);
2054 got_repo_close(repo);
2056 error = got_error_from_errno();
2059 fprintf(stderr, "%s: '%s' is not a "
2060 "known command\n", getprogname(),
2064 fprintf(stderr, "%s: '%s' is neither a known "
2065 "command nor a path to a repository\n",
2066 getprogname(), argv[0]);
2070 cmd = &tog_commands[0];
2071 cmd_argv = make_argv(cmd->name, repo_path);
2079 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2086 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);