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 *logmsg0 = NULL, *logmsg = NULL;
207 char *author0 = NULL, *author = NULL;
208 wchar_t *wlogmsg = NULL, *wauthor = NULL;
209 int author_width, logmsg_width;
210 char *newline, *smallerthan;
215 static const size_t id_display_cols = 8;
216 static const size_t author_display_cols = 16;
217 const int avail = COLS;
219 err = got_object_id_str(&id_str, id);
222 id_len = strlen(id_str);
223 if (avail < id_display_cols) {
224 limit = MIN(id_len, avail);
225 waddnstr(tog_log_view.window, id_str, limit);
227 limit = MIN(id_display_cols, id_len);
228 waddnstr(tog_log_view.window, id_str, limit);
231 while (col <= avail && col < id_display_cols + 2) {
232 waddch(tog_log_view.window, ' ');
238 author0 = strdup(commit->author);
239 if (author0 == NULL) {
240 err = got_error_from_errno();
244 smallerthan = strchr(author, '<');
248 char *at = strchr(author, '@');
252 limit = MIN(avail, author_display_cols);
253 err = format_line(&wauthor, &author_width, author, limit);
256 waddwstr(tog_log_view.window, wauthor);
258 while (col <= avail && author_width < author_display_cols + 1) {
259 waddch(tog_log_view.window, ' ');
266 logmsg0 = strdup(commit->logmsg);
267 if (logmsg0 == NULL) {
268 err = got_error_from_errno();
272 while (*logmsg == '\n')
274 newline = strchr(logmsg, '\n');
278 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
281 waddwstr(tog_log_view.window, wlogmsg);
283 while (col <= avail) {
284 waddch(tog_log_view.window, ' ');
297 struct commit_queue_entry {
298 TAILQ_ENTRY(commit_queue_entry) entry;
299 struct got_object_id *id;
300 struct got_commit_object *commit;
302 TAILQ_HEAD(commit_queue, commit_queue_entry);
304 static struct commit_queue_entry *
305 alloc_commit_queue_entry(struct got_commit_object *commit,
306 struct got_object_id *id)
308 struct commit_queue_entry *entry;
310 entry = calloc(1, sizeof(*entry));
315 entry->commit = commit;
320 pop_commit(struct commit_queue *commits)
322 struct commit_queue_entry *entry;
324 entry = TAILQ_FIRST(commits);
325 TAILQ_REMOVE(commits, entry, entry);
326 got_object_commit_close(entry->commit);
327 /* Don't free entry->id! It is owned by the commit graph. */
332 free_commits(struct commit_queue *commits)
334 while (!TAILQ_EMPTY(commits))
338 static const struct got_error *
339 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
340 struct got_object_id *start_id, struct got_repository *repo)
342 const struct got_error *err = NULL;
343 struct got_object_id *id;
344 struct commit_queue_entry *entry;
346 err = got_commit_graph_iter_start(graph, start_id);
350 entry = TAILQ_LAST(commits, commit_queue);
351 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
354 /* Start ID's commit is already on the queue; skip over it. */
355 err = got_commit_graph_iter_next(&id, graph);
356 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
359 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
365 struct got_commit_object *commit;
367 err = got_commit_graph_iter_next(&id, graph);
369 if (err->code == GOT_ERR_ITER_NEED_MORE)
374 err = got_object_open_as_commit(&commit, repo, id);
378 entry = alloc_commit_queue_entry(commit, id);
380 err = got_error_from_errno();
384 TAILQ_INSERT_TAIL(commits, entry, entry);
390 static const struct got_error *
391 fetch_next_commit(struct commit_queue_entry **pentry,
392 struct commit_queue_entry *entry, struct commit_queue *commits,
393 struct got_commit_graph *graph, struct got_repository *repo)
395 const struct got_error *err = NULL;
396 struct got_object_qid *qid;
400 /* Populate commit graph with entry's parent commits. */
401 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
403 err = got_commit_graph_fetch_commits_up_to(&nfetched,
404 graph, qid->id, repo);
409 /* Append outstanding commits to queue in graph sort order. */
410 err = queue_commits(graph, commits, entry->id, repo);
412 if (err->code == GOT_ERR_ITER_COMPLETED)
417 /* Next entry to display should now be available. */
418 *pentry = TAILQ_NEXT(entry, entry);
420 return got_error(GOT_ERR_NO_OBJ);
425 static const struct got_error *
426 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
428 const struct got_error *err = NULL;
429 struct got_reference *head_ref;
433 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
437 err = got_ref_resolve(head_id, repo, head_ref);
438 got_ref_close(head_ref);
447 static const struct got_error *
448 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
449 struct commit_queue_entry *first, int selected_idx, int limit)
451 const struct got_error *err = NULL;
452 struct commit_queue_entry *entry;
455 werase(tog_log_view.window);
460 if (ncommits == limit)
462 if (ncommits == selected_idx) {
463 wstandout(tog_log_view.window);
466 err = draw_commit(entry->commit, entry->id);
467 if (ncommits == selected_idx)
468 wstandend(tog_log_view.window);
473 entry = TAILQ_NEXT(entry, entry);
483 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
484 struct commit_queue *commits)
486 struct commit_queue_entry *entry;
489 entry = TAILQ_FIRST(commits);
490 if (*first_displayed_entry == entry)
493 entry = *first_displayed_entry;
494 while (entry && nscrolled < maxscroll) {
495 entry = TAILQ_PREV(entry, commit_queue, entry);
497 *first_displayed_entry = entry;
503 static const struct got_error *
504 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
505 struct commit_queue_entry *last_displayed_entry,
506 struct commit_queue *commits, struct got_commit_graph *graph,
507 struct got_repository *repo)
509 const struct got_error *err = NULL;
510 struct commit_queue_entry *pentry;
514 pentry = TAILQ_NEXT(last_displayed_entry, entry);
515 if (pentry == NULL) {
516 err = fetch_next_commit(&pentry, last_displayed_entry,
517 commits, graph, repo);
518 if (err || pentry == NULL)
521 last_displayed_entry = pentry;
523 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
526 *first_displayed_entry = pentry;
527 } while (++nscrolled < maxscroll);
533 num_parents(struct commit_queue_entry *entry)
538 entry = TAILQ_NEXT(entry, entry);
545 static const struct got_error *
546 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
548 const struct got_error *err;
549 struct got_object *obj1 = NULL, *obj2 = NULL;
550 struct got_object_qid *parent_id;
552 err = got_object_open(&obj2, repo, entry->id);
556 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
558 err = got_object_open(&obj1, repo, parent_id->id);
563 err = show_diff_view(obj1, obj2, repo);
566 got_object_close(obj1);
568 got_object_close(obj2);
572 static const struct got_error *
573 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
575 const struct got_error *err = NULL;
576 struct got_tree_object *tree;
578 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
582 err = show_tree_view(tree, entry->id, repo);
583 got_object_tree_close(tree);
587 static const struct got_error *
588 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
590 const struct got_error *err = NULL;
591 struct got_object_id *head_id = NULL;
592 int ch, done = 0, selected = 0, nparents, nfetched;
593 struct got_commit_graph *graph;
594 struct commit_queue commits;
595 struct commit_queue_entry *entry = NULL;
596 struct commit_queue_entry *first_displayed_entry = NULL;
597 struct commit_queue_entry *last_displayed_entry = NULL;
598 struct commit_queue_entry *selected_entry = NULL;
600 if (tog_log_view.window == NULL) {
601 tog_log_view.window = newwin(0, 0, 0, 0);
602 if (tog_log_view.window == NULL)
603 return got_error_from_errno();
604 keypad(tog_log_view.window, TRUE);
606 if (tog_log_view.panel == NULL) {
607 tog_log_view.panel = new_panel(tog_log_view.window);
608 if (tog_log_view.panel == NULL)
609 return got_error_from_errno();
611 show_panel(tog_log_view.panel);
613 err = get_head_commit_id(&head_id, repo);
617 TAILQ_INIT(&commits);
619 err = got_commit_graph_open(&graph, head_id, 0, repo);
623 /* Populate commit graph with a sufficient number of commits. */
624 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
628 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
633 * Open the initial batch of commits, sorted in commit graph order.
634 * We keep all commits open throughout the lifetime of the log view
635 * in order to avoid having to re-fetch commits from disk while
636 * updating the display.
638 err = queue_commits(graph, &commits, head_id, repo);
639 if (err && err->code != GOT_ERR_ITER_COMPLETED)
642 /* Find entry corresponding to the first commit to display. */
643 TAILQ_FOREACH(entry, &commits, entry) {
644 if (got_object_id_cmp(entry->id, start_id) == 0) {
645 first_displayed_entry = entry;
649 if (first_displayed_entry == NULL) {
650 err = got_error(GOT_ERR_NO_OBJ);
655 err = draw_commits(&last_displayed_entry, &selected_entry,
656 first_displayed_entry, selected, LINES);
660 nodelay(stdscr, FALSE);
661 ch = wgetch(tog_log_view.window);
662 nodelay(stdscr, TRUE);
666 err = got_error_from_errno();
679 scroll_up(&first_displayed_entry, 1, &commits);
682 if (TAILQ_FIRST(&commits) ==
683 first_displayed_entry) {
687 scroll_up(&first_displayed_entry, LINES,
692 nparents = num_parents(first_displayed_entry);
693 if (selected < LINES - 1 &&
694 selected < nparents - 1) {
698 err = scroll_down(&first_displayed_entry, 1,
699 last_displayed_entry, &commits, graph,
705 err = scroll_down(&first_displayed_entry, LINES,
706 last_displayed_entry, &commits, graph,
710 if (last_displayed_entry->commit->nparents > 0)
712 /* can't scroll any further; move cursor down */
713 nparents = num_parents(first_displayed_entry);
714 if (selected < LINES - 1 ||
715 selected < nparents - 1)
716 selected = MIN(LINES - 1, nparents - 1);
719 if (selected > LINES)
720 selected = LINES - 1;
724 err = show_commit(selected_entry, repo);
727 show_panel(tog_log_view.panel);
730 err = browse_commit(selected_entry, repo);
733 show_panel(tog_log_view.panel);
742 got_commit_graph_close(graph);
743 free_commits(&commits);
747 static const struct got_error *
748 cmd_log(int argc, char *argv[])
750 const struct got_error *error;
751 struct got_repository *repo;
752 struct got_object_id *start_id = NULL;
753 char *repo_path = NULL;
754 char *start_commit = NULL;
758 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
762 while ((ch = getopt(argc, argv, "c:")) != -1) {
765 start_commit = optarg;
777 repo_path = getcwd(NULL, 0);
778 if (repo_path == NULL)
779 return got_error_from_errno();
780 } else if (argc == 1) {
781 repo_path = realpath(argv[0], NULL);
782 if (repo_path == NULL)
783 return got_error_from_errno();
787 error = got_repo_open(&repo, repo_path);
792 if (start_commit == NULL) {
793 error = get_head_commit_id(&start_id, repo);
797 struct got_object *obj;
798 error = got_object_open_by_id_str(&obj, repo, start_commit);
800 start_id = got_object_get_id(obj);
801 if (start_id == NULL)
802 error = got_error_from_errno();
807 error = show_log_view(start_id, repo);
809 got_repo_close(repo);
817 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
823 parse_next_line(FILE *f, size_t *len)
828 const char delim[3] = { '\0', '\0', '\0'};
830 line = fparseln(f, &linelen, &lineno, delim, 0);
836 static const struct got_error *
837 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
838 int *last_displayed_line, int *eof, int max_lines)
840 const struct got_error *err;
841 int nlines = 0, nprinted = 0;
851 while (nprinted < max_lines) {
852 line = parse_next_line(f, &len);
857 if (++nlines < *first_displayed_line) {
862 err = format_line(&wline, &width, line, COLS);
867 waddwstr(window, wline);
869 waddch(window, '\n');
871 *first_displayed_line = nlines;
874 *last_displayed_line = nlines;
882 static const struct got_error *
883 show_diff_view(struct got_object *obj1, struct got_object *obj2,
884 struct got_repository *repo)
886 const struct got_error *err;
888 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
891 if (obj1 != NULL && obj2 != NULL &&
892 got_object_get_type(obj1) != got_object_get_type(obj2))
893 return got_error(GOT_ERR_OBJ_TYPE);
897 return got_error_from_errno();
899 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
900 case GOT_OBJ_TYPE_BLOB:
901 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
903 case GOT_OBJ_TYPE_TREE:
904 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
906 case GOT_OBJ_TYPE_COMMIT:
907 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
910 return got_error(GOT_ERR_OBJ_TYPE);
915 if (tog_diff_view.window == NULL) {
916 tog_diff_view.window = newwin(0, 0, 0, 0);
917 if (tog_diff_view.window == NULL)
918 return got_error_from_errno();
919 keypad(tog_diff_view.window, TRUE);
921 if (tog_diff_view.panel == NULL) {
922 tog_diff_view.panel = new_panel(tog_diff_view.window);
923 if (tog_diff_view.panel == NULL)
924 return got_error_from_errno();
926 show_panel(tog_diff_view.panel);
929 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
930 &last_displayed_line, &eof, LINES);
933 nodelay(stdscr, FALSE);
934 ch = wgetch(tog_diff_view.window);
935 nodelay(stdscr, TRUE);
943 if (first_displayed_line > 1)
944 first_displayed_line--;
948 while (i++ < LINES - 1 &&
949 first_displayed_line > 1)
950 first_displayed_line--;
957 first_displayed_line++;
962 while (!eof && i++ < LINES - 1) {
963 char *line = parse_next_line(f, NULL);
964 first_displayed_line++;
977 static const struct got_error *
978 cmd_diff(int argc, char *argv[])
980 const struct got_error *error = NULL;
981 struct got_repository *repo = NULL;
982 struct got_object *obj1 = NULL, *obj2 = NULL;
983 char *repo_path = NULL;
984 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
988 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
992 while ((ch = getopt(argc, argv, "")) != -1) {
1004 usage_diff(); /* TODO show local worktree changes */
1005 } else if (argc == 2) {
1006 repo_path = getcwd(NULL, 0);
1007 if (repo_path == NULL)
1008 return got_error_from_errno();
1009 obj_id_str1 = argv[0];
1010 obj_id_str2 = argv[1];
1011 } else if (argc == 3) {
1012 repo_path = realpath(argv[0], NULL);
1013 if (repo_path == NULL)
1014 return got_error_from_errno();
1015 obj_id_str1 = argv[1];
1016 obj_id_str2 = argv[2];
1020 error = got_repo_open(&repo, repo_path);
1025 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1029 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1033 error = show_diff_view(obj1, obj2, repo);
1035 got_repo_close(repo);
1037 got_object_close(obj1);
1039 got_object_close(obj2);
1047 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1052 struct tog_blame_line {
1054 struct got_object_id *id;
1057 static const struct got_error *
1058 draw_blame(WINDOW *window, FILE *f, struct tog_blame_line *lines, int nlines,
1059 int *first_displayed_line, int *last_displayed_line, int *eof,
1062 const struct got_error *err;
1063 int lineno = 0, nprinted = 0;
1068 struct tog_blame_line *blame_line;
1074 while (nprinted < max_lines) {
1075 line = parse_next_line(f, &len);
1080 if (++lineno < *first_displayed_line) {
1085 err = format_line(&wline, &width, line, COLS - 9);
1091 blame_line = &lines[lineno - 1];
1092 if (blame_line->annotated) {
1094 err = got_object_id_str(&id_str, blame_line->id);
1099 wprintw(window, "%.8s ", id_str);
1102 waddstr(window, " ");
1104 waddwstr(window, wline);
1105 if (width < COLS - 9)
1106 waddch(window, '\n');
1107 if (++nprinted == 1)
1108 *first_displayed_line = lineno;
1111 *last_displayed_line = lineno;
1119 struct tog_blame_cb_args {
1120 pthread_mutex_t *mutex;
1121 struct tog_blame_line *lines; /* one per line */
1126 int *first_displayed_line;
1127 int *last_displayed_line;
1131 static const struct got_error *
1132 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1134 const struct got_error *err = NULL;
1135 struct tog_blame_cb_args *a = arg;
1136 struct tog_blame_line *line;
1140 return got_error(GOT_ERR_ITER_COMPLETED);
1142 if (nlines != a->nlines || lineno < 1 || lineno > a->nlines)
1143 return got_error(GOT_ERR_RANGE);
1145 if (pthread_mutex_lock(a->mutex) != 0)
1146 return got_error_from_errno();
1148 line = &a->lines[lineno - 1];
1149 line->id = got_object_id_dup(id);
1150 if (line->id == NULL) {
1151 err = got_error_from_errno();
1154 line->annotated = 1;
1156 err = draw_blame(a->window, a->f, a->lines, a->nlines,
1157 a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1159 if (pthread_mutex_unlock(a->mutex) != 0)
1160 return got_error_from_errno();
1164 struct tog_blame_thread_args {
1166 struct got_object_id *commit_id;
1167 struct got_repository *repo;
1168 void *blame_cb_args;
1172 blame_thread(void *arg)
1174 struct tog_blame_thread_args *a = arg;
1175 return (void *)got_blame_incremental(a->path, a->commit_id, a->repo,
1176 blame_cb, a->blame_cb_args);
1179 static const struct got_error *
1180 show_blame_view(const char *path, struct got_object_id *commit_id,
1181 struct got_repository *repo)
1183 const struct got_error *err = NULL;
1184 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1186 struct got_object *obj = NULL;
1187 struct got_blob_object *blob = NULL;
1189 size_t filesize, nlines = 0;
1190 struct tog_blame_line *lines = NULL;
1191 pthread_t thread = NULL;
1192 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1193 struct tog_blame_cb_args blame_cb_args;
1194 struct tog_blame_thread_args blame_thread_args;
1196 err = got_object_open_by_path(&obj, repo, commit_id, path);
1199 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1200 err = got_error(GOT_ERR_OBJ_TYPE);
1201 got_object_close(obj);
1205 err = got_object_blob_open(&blob, repo, obj, 8192);
1206 got_object_close(obj);
1211 err = got_error_from_errno();
1214 err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1218 lines = calloc(nlines, sizeof(*lines));
1219 if (lines == NULL) {
1220 err = got_error_from_errno();
1224 if (tog_blame_view.window == NULL) {
1225 tog_blame_view.window = newwin(0, 0, 0, 0);
1226 if (tog_blame_view.window == NULL)
1227 return got_error_from_errno();
1228 keypad(tog_blame_view.window, TRUE);
1230 if (tog_blame_view.panel == NULL) {
1231 tog_blame_view.panel = new_panel(tog_blame_view.window);
1232 if (tog_blame_view.panel == NULL)
1233 return got_error_from_errno();
1235 show_panel(tog_blame_view.panel);
1237 if (pthread_mutex_init(&mutex, NULL) != 0) {
1238 err = got_error_from_errno();
1241 blame_cb_args.lines = lines;
1242 blame_cb_args.nlines = nlines;
1243 blame_cb_args.mutex = &mutex;
1244 blame_cb_args.f = f;
1245 blame_cb_args.window = tog_blame_view.window;
1246 blame_cb_args.first_displayed_line = &first_displayed_line;
1247 blame_cb_args.last_displayed_line = &last_displayed_line;
1248 blame_cb_args.done = &done;
1250 blame_thread_args.path = path;
1251 blame_thread_args.commit_id = commit_id;
1252 blame_thread_args.repo = repo;
1253 blame_thread_args.blame_cb_args = &blame_cb_args;
1255 if (pthread_create(&thread, NULL, blame_thread,
1256 &blame_thread_args) != 0) {
1257 err = got_error_from_errno();
1262 if (pthread_mutex_lock(&mutex) != 0) {
1263 err = got_error_from_errno();
1266 err = draw_blame(tog_blame_view.window, f, lines, nlines,
1267 &first_displayed_line, &last_displayed_line, &eof, LINES);
1268 if (pthread_mutex_unlock(&mutex) != 0) {
1269 err = got_error_from_errno();
1274 nodelay(stdscr, FALSE);
1275 ch = wgetch(tog_blame_view.window);
1276 nodelay(stdscr, TRUE);
1277 if (pthread_mutex_lock(&mutex) != 0) {
1278 err = got_error_from_errno();
1288 if (first_displayed_line > 1)
1289 first_displayed_line--;
1293 while (i++ < LINES - 1 &&
1294 first_displayed_line > 1)
1295 first_displayed_line--;
1302 first_displayed_line++;
1307 while (!eof && i++ < LINES - 1) {
1308 char *line = parse_next_line(f, NULL);
1309 first_displayed_line++;
1317 if (pthread_mutex_unlock(&mutex) != 0) {
1318 err = got_error_from_errno();
1324 if (pthread_join(thread, (void **)&err) != 0)
1325 err = got_error_from_errno();
1326 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1330 got_object_blob_close(blob);
1333 for (i = 0; i < nlines; i++)
1339 static const struct got_error *
1340 cmd_blame(int argc, char *argv[])
1342 const struct got_error *error;
1343 struct got_repository *repo = NULL;
1344 char *repo_path = NULL;
1346 struct got_object_id *commit_id = NULL;
1347 char *commit_id_str = NULL;
1351 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1355 while ((ch = getopt(argc, argv, "c:")) != -1) {
1358 commit_id_str = optarg;
1371 } else if (argc == 1) {
1372 repo_path = getcwd(NULL, 0);
1373 if (repo_path == NULL)
1374 return got_error_from_errno();
1376 } else if (argc == 2) {
1377 repo_path = realpath(argv[0], NULL);
1378 if (repo_path == NULL)
1379 return got_error_from_errno();
1384 error = got_repo_open(&repo, repo_path);
1389 if (commit_id_str == NULL) {
1390 struct got_reference *head_ref;
1391 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1394 error = got_ref_resolve(&commit_id, repo, head_ref);
1395 got_ref_close(head_ref);
1397 struct got_object *obj;
1398 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1401 commit_id = got_object_get_id(obj);
1402 if (commit_id == NULL)
1403 error = got_error_from_errno();
1404 got_object_close(obj);
1409 error = show_blame_view(path, commit_id, repo);
1413 got_repo_close(repo);
1417 static const struct got_error *
1418 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1419 struct got_tree_entry **last_displayed_entry,
1420 struct got_tree_entry **selected_entry, int *ndisplayed,
1421 WINDOW *window, const char *label, const char *parent_path,
1422 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1424 const struct got_error *err = NULL;
1425 struct got_tree_entry *te;
1436 err = format_line(&wline, &width, label, COLS);
1439 waddwstr(window, wline);
1441 waddch(window, '\n');
1444 err = format_line(&wline, &width, parent_path, COLS);
1447 waddwstr(window, wline);
1449 waddch(window, '\n');
1452 waddch(window, '\n');
1456 te = SIMPLEQ_FIRST(&entries->head);
1457 if (*first_displayed_entry == NULL) {
1458 if (selected == 0) {
1460 *selected_entry = NULL;
1462 waddstr(window, " ..\n"); /* parent directory */
1471 while (te != *first_displayed_entry)
1472 te = SIMPLEQ_NEXT(te, entry);
1477 if (asprintf(&line, " %s%s",
1478 te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1479 return got_error_from_errno();
1480 err = format_line(&wline, &width, line, COLS);
1485 if (n == selected) {
1487 *selected_entry = te;
1489 waddwstr(window, wline);
1491 waddch(window, '\n');
1497 *last_displayed_entry = te;
1500 te = SIMPLEQ_NEXT(te, entry);
1507 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1508 const struct got_tree_entries *entries, int isroot)
1510 struct got_tree_entry *te, *prev;
1513 if (*first_displayed_entry == NULL)
1516 te = SIMPLEQ_FIRST(&entries->head);
1517 if (*first_displayed_entry == te) {
1519 *first_displayed_entry = NULL;
1523 /* XXX this is stupid... switch to TAILQ? */
1524 for (i = 0; i < maxscroll; i++) {
1525 while (te != *first_displayed_entry) {
1527 te = SIMPLEQ_NEXT(te, entry);
1529 *first_displayed_entry = prev;
1530 te = SIMPLEQ_FIRST(&entries->head);
1532 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1533 *first_displayed_entry = NULL;
1537 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1538 struct got_tree_entry *last_displayed_entry,
1539 const struct got_tree_entries *entries)
1541 struct got_tree_entry *next;
1544 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1547 if (*first_displayed_entry)
1548 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1550 next = SIMPLEQ_FIRST(&entries->head);
1552 *first_displayed_entry = next;
1553 if (++n >= maxscroll)
1555 next = SIMPLEQ_NEXT(next, entry);
1559 struct tog_parent_tree {
1560 TAILQ_ENTRY(tog_parent_tree) entry;
1561 struct got_tree_object *tree;
1562 struct got_tree_entry *first_displayed_entry;
1563 struct got_tree_entry *selected_entry;
1567 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1569 static const struct got_error *
1570 tree_entry_path(char **path, struct tog_parent_trees *parents,
1571 struct got_tree_entry *te)
1573 const struct got_error *err = NULL;
1574 struct tog_parent_tree *pt;
1575 size_t len = 2; /* for leading slash and NUL */
1577 TAILQ_FOREACH(pt, parents, entry)
1578 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1580 len += strlen(te->name);
1582 *path = calloc(1, len);
1584 return got_error_from_errno();
1587 pt = TAILQ_LAST(parents, tog_parent_trees);
1589 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1590 err = got_error(GOT_ERR_NO_SPACE);
1593 if (strlcat(*path, "/", len) >= len) {
1594 err = got_error(GOT_ERR_NO_SPACE);
1597 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1600 if (strlcat(*path, te->name, len) >= len) {
1601 err = got_error(GOT_ERR_NO_SPACE);
1613 static const struct got_error *
1614 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1615 struct got_object_id *commit_id, struct got_repository *repo)
1617 const struct got_error *err = NULL;
1620 err = tree_entry_path(&path, parents, te);
1624 err = show_blame_view(path, commit_id, repo);
1629 static const struct got_error *
1630 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1631 struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 int ch, done = 0, selected = 0;
1635 struct got_tree_object *tree = root;
1636 const struct got_tree_entries *entries;
1637 struct got_tree_entry *first_displayed_entry = NULL;
1638 struct got_tree_entry *last_displayed_entry = NULL;
1639 struct got_tree_entry *selected_entry = NULL;
1640 char *commit_id_str = NULL, *tree_label = NULL;
1641 int nentries, ndisplayed;
1642 struct tog_parent_trees parents;
1644 TAILQ_INIT(&parents);
1646 err = got_object_id_str(&commit_id_str, commit_id);
1650 if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1651 err = got_error_from_errno();
1655 if (tog_tree_view.window == NULL) {
1656 tog_tree_view.window = newwin(0, 0, 0, 0);
1657 if (tog_tree_view.window == NULL)
1658 return got_error_from_errno();
1659 keypad(tog_tree_view.window, TRUE);
1661 if (tog_tree_view.panel == NULL) {
1662 tog_tree_view.panel = new_panel(tog_tree_view.window);
1663 if (tog_tree_view.panel == NULL)
1664 return got_error_from_errno();
1666 show_panel(tog_tree_view.panel);
1668 entries = got_object_tree_get_entries(root);
1669 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1672 entries = got_object_tree_get_entries(tree);
1673 nentries = entries->nentries;
1675 nentries++; /* '..' directory */
1677 err = tree_entry_path(&parent_path, &parents, NULL);
1681 err = draw_tree_entries(&first_displayed_entry,
1682 &last_displayed_entry, &selected_entry, &ndisplayed,
1683 tog_tree_view.window, tree_label, parent_path, entries,
1684 selected, LINES, tree == root);
1689 nodelay(stdscr, FALSE);
1690 ch = wgetch(tog_tree_view.window);
1691 nodelay(stdscr, TRUE);
1702 tree_scroll_up(&first_displayed_entry, 1,
1703 entries, tree == root);
1706 if (SIMPLEQ_FIRST(&entries->head) ==
1707 first_displayed_entry) {
1709 first_displayed_entry = NULL;
1713 tree_scroll_up(&first_displayed_entry, LINES,
1714 entries, tree == root);
1718 if (selected < ndisplayed - 1) {
1722 tree_scroll_down(&first_displayed_entry, 1,
1723 last_displayed_entry, entries);
1726 tree_scroll_down(&first_displayed_entry, LINES,
1727 last_displayed_entry, entries);
1728 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1730 /* can't scroll any further; move cursor down */
1731 if (selected < ndisplayed - 1)
1732 selected = ndisplayed - 1;
1736 if (selected_entry == NULL) {
1737 struct tog_parent_tree *parent;
1739 /* user selected '..' */
1742 parent = TAILQ_FIRST(&parents);
1743 TAILQ_REMOVE(&parents, parent, entry);
1744 got_object_tree_close(tree);
1745 tree = parent->tree;
1746 first_displayed_entry =
1747 parent->first_displayed_entry;
1748 selected_entry = parent->selected_entry;
1749 selected = parent->selected;
1751 } else if (S_ISDIR(selected_entry->mode)) {
1752 struct tog_parent_tree *parent;
1753 struct got_tree_object *child;
1754 err = got_object_open_as_tree(
1755 &child, repo, selected_entry->id);
1758 parent = calloc(1, sizeof(*parent));
1759 if (parent == NULL) {
1760 err = got_error_from_errno();
1763 parent->tree = tree;
1764 parent->first_displayed_entry =
1765 first_displayed_entry;
1766 parent->selected_entry = selected_entry;
1767 parent->selected = selected;
1768 TAILQ_INSERT_HEAD(&parents, parent,
1772 first_displayed_entry = NULL;
1773 } else if (S_ISREG(selected_entry->mode)) {
1774 err = blame_tree_entry(selected_entry,
1775 &parents, commit_id, repo);
1781 if (selected > LINES)
1782 selected = ndisplayed - 1;
1790 free(commit_id_str);
1791 while (!TAILQ_EMPTY(&parents)) {
1792 struct tog_parent_tree *parent;
1793 parent = TAILQ_FIRST(&parents);
1794 TAILQ_REMOVE(&parents, parent, entry);
1799 got_object_tree_close(tree);
1807 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1812 static const struct got_error *
1813 cmd_tree(int argc, char *argv[])
1815 const struct got_error *error;
1816 struct got_repository *repo = NULL;
1817 char *repo_path = NULL;
1818 struct got_object_id *commit_id = NULL;
1819 char *commit_id_arg = NULL;
1820 struct got_commit_object *commit = NULL;
1821 struct got_tree_object *tree = NULL;
1825 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1829 while ((ch = getopt(argc, argv, "c:")) != -1) {
1832 commit_id_arg = optarg;
1844 repo_path = getcwd(NULL, 0);
1845 if (repo_path == NULL)
1846 return got_error_from_errno();
1847 } else if (argc == 1) {
1848 repo_path = realpath(argv[0], NULL);
1849 if (repo_path == NULL)
1850 return got_error_from_errno();
1854 error = got_repo_open(&repo, repo_path);
1859 if (commit_id_arg == NULL) {
1860 error = get_head_commit_id(&commit_id, repo);
1864 struct got_object *obj;
1865 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1866 if (error == NULL) {
1867 commit_id = got_object_get_id(obj);
1868 if (commit_id == NULL)
1869 error = got_error_from_errno();
1875 error = got_object_open_as_commit(&commit, repo, commit_id);
1879 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1883 error = show_tree_view(tree, commit_id, repo);
1887 got_object_commit_close(commit);
1889 got_object_tree_close(tree);
1891 got_repo_close(repo);
1901 intrflush(stdscr, FALSE);
1902 keypad(stdscr, TRUE);
1911 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1912 "Available commands:\n", getprogname());
1913 for (i = 0; i < nitems(tog_commands); i++) {
1914 struct tog_cmd *cmd = &tog_commands[i];
1915 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1921 make_argv(const char *arg0, const char *arg1)
1924 int argc = (arg1 == NULL ? 1 : 2);
1926 argv = calloc(argc, sizeof(char *));
1929 argv[0] = strdup(arg0);
1930 if (argv[0] == NULL)
1933 argv[1] = strdup(arg1);
1934 if (argv[1] == NULL)
1942 main(int argc, char *argv[])
1944 const struct got_error *error = NULL;
1945 struct tog_cmd *cmd = NULL;
1947 char **cmd_argv = NULL;
1949 setlocale(LC_ALL, "");
1951 while ((ch = getopt(argc, argv, "h")) != -1) {
1970 /* Build an argument vector which runs a default command. */
1971 cmd = &tog_commands[0];
1972 cmd_argv = make_argv(cmd->name, NULL);
1977 /* Did the user specific a command? */
1978 for (i = 0; i < nitems(tog_commands); i++) {
1979 if (strncmp(tog_commands[i].name, argv[0],
1980 strlen(argv[0])) == 0) {
1981 cmd = &tog_commands[i];
1983 tog_commands[i].cmd_usage();
1988 /* Did the user specify a repository? */
1989 char *repo_path = realpath(argv[0], NULL);
1991 struct got_repository *repo;
1992 error = got_repo_open(&repo, repo_path);
1994 got_repo_close(repo);
1996 error = got_error_from_errno();
1999 fprintf(stderr, "%s: '%s' is not a "
2000 "known command\n", getprogname(),
2004 fprintf(stderr, "%s: '%s' is neither a known "
2005 "command nor a path to a repository\n",
2006 getprogname(), argv[0]);
2010 cmd = &tog_commands[0];
2011 cmd_argv = make_argv(cmd->name, repo_path);
2019 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2026 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);