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;
166 err = mbs2ws(&wline, &wlen, line);
171 while (i < wlen && cols < wlimit) {
172 int width = wcwidth(wline[i]);
179 if (cols + width <= wlimit) {
185 if (wline[i] == L'\t')
186 cols += TABSIZE - ((cols + 1) % TABSIZE);
190 err = got_error_from_errno();
205 static const struct got_error *
206 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
208 const struct got_error *err = NULL;
209 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
210 char *logmsg0 = NULL, *logmsg = NULL;
211 char *author0 = NULL, *author = NULL;
212 wchar_t *wlogmsg = NULL, *wauthor = NULL;
213 int author_width, logmsg_width;
214 char *newline, *smallerthan;
217 static const size_t date_display_cols = 9;
218 static const size_t author_display_cols = 16;
219 const int avail = COLS;
221 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
223 return got_error(GOT_ERR_NO_SPACE);
225 if (avail < date_display_cols)
226 limit = MIN(sizeof(datebuf) - 1, avail);
228 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
229 waddnstr(tog_log_view.window, datebuf, limit);
234 author0 = strdup(commit->author);
235 if (author0 == NULL) {
236 err = got_error_from_errno();
240 smallerthan = strchr(author, '<');
244 char *at = strchr(author, '@');
249 err = format_line(&wauthor, &author_width, author, limit);
252 waddwstr(tog_log_view.window, wauthor);
254 while (col <= avail && author_width < author_display_cols + 1) {
255 waddch(tog_log_view.window, ' ');
262 logmsg0 = strdup(commit->logmsg);
263 if (logmsg0 == NULL) {
264 err = got_error_from_errno();
268 while (*logmsg == '\n')
270 newline = strchr(logmsg, '\n');
274 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
277 waddwstr(tog_log_view.window, wlogmsg);
279 while (col <= avail) {
280 waddch(tog_log_view.window, ' ');
292 struct commit_queue_entry {
293 TAILQ_ENTRY(commit_queue_entry) entry;
294 struct got_object_id *id;
295 struct got_commit_object *commit;
297 TAILQ_HEAD(commit_queue, commit_queue_entry);
299 static struct commit_queue_entry *
300 alloc_commit_queue_entry(struct got_commit_object *commit,
301 struct got_object_id *id)
303 struct commit_queue_entry *entry;
305 entry = calloc(1, sizeof(*entry));
310 entry->commit = commit;
315 pop_commit(struct commit_queue *commits)
317 struct commit_queue_entry *entry;
319 entry = TAILQ_FIRST(commits);
320 TAILQ_REMOVE(commits, entry, entry);
321 got_object_commit_close(entry->commit);
322 /* Don't free entry->id! It is owned by the commit graph. */
327 free_commits(struct commit_queue *commits)
329 while (!TAILQ_EMPTY(commits))
333 static const struct got_error *
334 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
335 struct got_object_id *start_id, struct got_repository *repo)
337 const struct got_error *err = NULL;
338 struct got_object_id *id;
339 struct commit_queue_entry *entry;
341 err = got_commit_graph_iter_start(graph, start_id);
345 entry = TAILQ_LAST(commits, commit_queue);
346 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
349 /* Start ID's commit is already on the queue; skip over it. */
350 err = got_commit_graph_iter_next(&id, graph);
351 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
354 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
360 struct got_commit_object *commit;
362 err = got_commit_graph_iter_next(&id, graph);
364 if (err->code == GOT_ERR_ITER_NEED_MORE)
369 err = got_object_open_as_commit(&commit, repo, id);
373 entry = alloc_commit_queue_entry(commit, id);
375 err = got_error_from_errno();
379 TAILQ_INSERT_TAIL(commits, entry, entry);
385 static const struct got_error *
386 fetch_next_commit(struct commit_queue_entry **pentry,
387 struct commit_queue_entry *entry, struct commit_queue *commits,
388 struct got_commit_graph *graph, struct got_repository *repo)
390 const struct got_error *err = NULL;
391 struct got_object_qid *qid;
395 /* Populate commit graph with entry's parent commits. */
396 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
398 err = got_commit_graph_fetch_commits_up_to(&nfetched,
399 graph, qid->id, repo);
404 /* Append outstanding commits to queue in graph sort order. */
405 err = queue_commits(graph, commits, entry->id, repo);
407 if (err->code == GOT_ERR_ITER_COMPLETED)
412 /* Next entry to display should now be available. */
413 *pentry = TAILQ_NEXT(entry, entry);
415 return got_error(GOT_ERR_NO_OBJ);
420 static const struct got_error *
421 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
423 const struct got_error *err = NULL;
424 struct got_reference *head_ref;
428 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
432 err = got_ref_resolve(head_id, repo, head_ref);
433 got_ref_close(head_ref);
442 static const struct got_error *
443 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
444 struct commit_queue_entry *first, int selected_idx, int limit)
446 const struct got_error *err = NULL;
447 struct commit_queue_entry *entry;
449 char *id_str, *header;
456 if (++ncommits - 1 == selected_idx) {
460 entry = TAILQ_NEXT(entry, entry);
462 if (*selected == NULL)
463 return got_error(GOT_ERR_RANGE);
465 err = got_object_id_str(&id_str, (*selected)->id);
469 if (asprintf(&header, "commit: %s", id_str) == -1) {
470 err = got_error_from_errno();
475 werase(tog_log_view.window);
477 header_len = strlen(header);
478 if (header_len > COLS) {
479 id_str[COLS + 1] = '\0';
482 wprintw(tog_log_view.window, header);
483 while (header_len < COLS) {
484 waddch(tog_log_view.window, ' ');
494 if (ncommits == limit - 1)
496 if (ncommits == selected_idx) {
497 wstandout(tog_log_view.window);
500 err = draw_commit(entry->commit, entry->id);
501 if (ncommits == selected_idx)
502 wstandend(tog_log_view.window);
507 entry = TAILQ_NEXT(entry, entry);
517 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
518 struct commit_queue *commits)
520 struct commit_queue_entry *entry;
523 entry = TAILQ_FIRST(commits);
524 if (*first_displayed_entry == entry)
527 entry = *first_displayed_entry;
528 while (entry && nscrolled < maxscroll) {
529 entry = TAILQ_PREV(entry, commit_queue, entry);
531 *first_displayed_entry = entry;
537 static const struct got_error *
538 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
539 struct commit_queue_entry *last_displayed_entry,
540 struct commit_queue *commits, struct got_commit_graph *graph,
541 struct got_repository *repo)
543 const struct got_error *err = NULL;
544 struct commit_queue_entry *pentry;
548 pentry = TAILQ_NEXT(last_displayed_entry, entry);
549 if (pentry == NULL) {
550 err = fetch_next_commit(&pentry, last_displayed_entry,
551 commits, graph, repo);
552 if (err || pentry == NULL)
555 last_displayed_entry = pentry;
557 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
560 *first_displayed_entry = pentry;
561 } while (++nscrolled < maxscroll);
567 num_parents(struct commit_queue_entry *entry)
572 entry = TAILQ_NEXT(entry, entry);
579 static const struct got_error *
580 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
582 const struct got_error *err;
583 struct got_object *obj1 = NULL, *obj2 = NULL;
584 struct got_object_qid *parent_id;
586 err = got_object_open(&obj2, repo, entry->id);
590 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
592 err = got_object_open(&obj1, repo, parent_id->id);
597 err = show_diff_view(obj1, obj2, repo);
600 got_object_close(obj1);
602 got_object_close(obj2);
606 static const struct got_error *
607 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
609 const struct got_error *err = NULL;
610 struct got_tree_object *tree;
612 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
616 err = show_tree_view(tree, entry->id, repo);
617 got_object_tree_close(tree);
621 static const struct got_error *
622 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
624 const struct got_error *err = NULL;
625 struct got_object_id *head_id = NULL;
626 int ch, done = 0, selected = 0, nparents, nfetched;
627 struct got_commit_graph *graph;
628 struct commit_queue commits;
629 struct commit_queue_entry *entry = NULL;
630 struct commit_queue_entry *first_displayed_entry = NULL;
631 struct commit_queue_entry *last_displayed_entry = NULL;
632 struct commit_queue_entry *selected_entry = NULL;
634 if (tog_log_view.window == NULL) {
635 tog_log_view.window = newwin(0, 0, 0, 0);
636 if (tog_log_view.window == NULL)
637 return got_error_from_errno();
638 keypad(tog_log_view.window, TRUE);
640 if (tog_log_view.panel == NULL) {
641 tog_log_view.panel = new_panel(tog_log_view.window);
642 if (tog_log_view.panel == NULL)
643 return got_error_from_errno();
645 show_panel(tog_log_view.panel);
647 err = get_head_commit_id(&head_id, repo);
651 TAILQ_INIT(&commits);
653 err = got_commit_graph_open(&graph, head_id, 0, repo);
657 /* Populate commit graph with a sufficient number of commits. */
658 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
662 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
667 * Open the initial batch of commits, sorted in commit graph order.
668 * We keep all commits open throughout the lifetime of the log view
669 * in order to avoid having to re-fetch commits from disk while
670 * updating the display.
672 err = queue_commits(graph, &commits, head_id, repo);
673 if (err && err->code != GOT_ERR_ITER_COMPLETED)
676 /* Find entry corresponding to the first commit to display. */
677 TAILQ_FOREACH(entry, &commits, entry) {
678 if (got_object_id_cmp(entry->id, start_id) == 0) {
679 first_displayed_entry = entry;
683 if (first_displayed_entry == NULL) {
684 err = got_error(GOT_ERR_NO_OBJ);
688 selected_entry = first_displayed_entry;
690 err = draw_commits(&last_displayed_entry, &selected_entry,
691 first_displayed_entry, selected, LINES);
695 nodelay(stdscr, FALSE);
696 ch = wgetch(tog_log_view.window);
697 nodelay(stdscr, TRUE);
701 err = got_error_from_errno();
714 scroll_up(&first_displayed_entry, 1, &commits);
717 if (TAILQ_FIRST(&commits) ==
718 first_displayed_entry) {
722 scroll_up(&first_displayed_entry, LINES,
727 nparents = num_parents(first_displayed_entry);
728 if (selected < LINES - 2 &&
729 selected < nparents - 1) {
733 err = scroll_down(&first_displayed_entry, 1,
734 last_displayed_entry, &commits, graph,
740 err = scroll_down(&first_displayed_entry, LINES,
741 last_displayed_entry, &commits, graph,
745 if (last_displayed_entry->commit->nparents > 0)
747 /* can't scroll any further; move cursor down */
748 nparents = num_parents(first_displayed_entry);
749 if (selected < LINES - 2 ||
750 selected < nparents - 1)
751 selected = MIN(LINES - 2, nparents - 1);
754 if (selected > LINES - 1)
755 selected = LINES - 2;
759 err = show_commit(selected_entry, repo);
762 show_panel(tog_log_view.panel);
765 err = browse_commit(selected_entry, repo);
768 show_panel(tog_log_view.panel);
777 got_commit_graph_close(graph);
778 free_commits(&commits);
782 static const struct got_error *
783 cmd_log(int argc, char *argv[])
785 const struct got_error *error;
786 struct got_repository *repo;
787 struct got_object_id *start_id = NULL;
788 char *repo_path = NULL;
789 char *start_commit = NULL;
793 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
797 while ((ch = getopt(argc, argv, "c:")) != -1) {
800 start_commit = optarg;
812 repo_path = getcwd(NULL, 0);
813 if (repo_path == NULL)
814 return got_error_from_errno();
815 } else if (argc == 1) {
816 repo_path = realpath(argv[0], NULL);
817 if (repo_path == NULL)
818 return got_error_from_errno();
822 error = got_repo_open(&repo, repo_path);
827 if (start_commit == NULL) {
828 error = get_head_commit_id(&start_id, repo);
832 struct got_object *obj;
833 error = got_object_open_by_id_str(&obj, repo, start_commit);
835 start_id = got_object_get_id(obj);
836 if (start_id == NULL)
837 error = got_error_from_errno();
842 error = show_log_view(start_id, repo);
844 got_repo_close(repo);
852 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
858 parse_next_line(FILE *f, size_t *len)
863 const char delim[3] = { '\0', '\0', '\0'};
865 line = fparseln(f, &linelen, &lineno, delim, 0);
871 static const struct got_error *
872 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
873 int *last_displayed_line, int *eof, int max_lines)
875 const struct got_error *err;
876 int nlines = 0, nprinted = 0;
886 while (nprinted < max_lines) {
887 line = parse_next_line(f, &len);
892 if (++nlines < *first_displayed_line) {
897 err = format_line(&wline, &width, line, COLS);
902 waddwstr(window, wline);
904 waddch(window, '\n');
906 *first_displayed_line = nlines;
909 *last_displayed_line = nlines;
917 static const struct got_error *
918 show_diff_view(struct got_object *obj1, struct got_object *obj2,
919 struct got_repository *repo)
921 const struct got_error *err;
923 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
926 if (obj1 != NULL && obj2 != NULL &&
927 got_object_get_type(obj1) != got_object_get_type(obj2))
928 return got_error(GOT_ERR_OBJ_TYPE);
932 return got_error_from_errno();
934 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
935 case GOT_OBJ_TYPE_BLOB:
936 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
938 case GOT_OBJ_TYPE_TREE:
939 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
941 case GOT_OBJ_TYPE_COMMIT:
942 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
945 return got_error(GOT_ERR_OBJ_TYPE);
950 if (tog_diff_view.window == NULL) {
951 tog_diff_view.window = newwin(0, 0, 0, 0);
952 if (tog_diff_view.window == NULL)
953 return got_error_from_errno();
954 keypad(tog_diff_view.window, TRUE);
956 if (tog_diff_view.panel == NULL) {
957 tog_diff_view.panel = new_panel(tog_diff_view.window);
958 if (tog_diff_view.panel == NULL)
959 return got_error_from_errno();
961 show_panel(tog_diff_view.panel);
964 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
965 &last_displayed_line, &eof, LINES);
968 nodelay(stdscr, FALSE);
969 ch = wgetch(tog_diff_view.window);
970 nodelay(stdscr, TRUE);
977 if (first_displayed_line > 1)
978 first_displayed_line--;
983 while (i++ < LINES - 1 &&
984 first_displayed_line > 1)
985 first_displayed_line--;
990 first_displayed_line++;
995 while (!eof && i++ < LINES - 1) {
996 char *line = parse_next_line(f, NULL);
997 first_displayed_line++;
1010 static const struct got_error *
1011 cmd_diff(int argc, char *argv[])
1013 const struct got_error *error = NULL;
1014 struct got_repository *repo = NULL;
1015 struct got_object *obj1 = NULL, *obj2 = NULL;
1016 char *repo_path = NULL;
1017 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1021 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1025 while ((ch = getopt(argc, argv, "")) != -1) {
1037 usage_diff(); /* TODO show local worktree changes */
1038 } else if (argc == 2) {
1039 repo_path = getcwd(NULL, 0);
1040 if (repo_path == NULL)
1041 return got_error_from_errno();
1042 obj_id_str1 = argv[0];
1043 obj_id_str2 = argv[1];
1044 } else if (argc == 3) {
1045 repo_path = realpath(argv[0], NULL);
1046 if (repo_path == NULL)
1047 return got_error_from_errno();
1048 obj_id_str1 = argv[1];
1049 obj_id_str2 = argv[2];
1053 error = got_repo_open(&repo, repo_path);
1058 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1062 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1066 error = show_diff_view(obj1, obj2, repo);
1068 got_repo_close(repo);
1070 got_object_close(obj1);
1072 got_object_close(obj2);
1080 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1085 struct tog_blame_line {
1087 struct got_object_id *id;
1090 static const struct got_error *
1091 draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1092 struct tog_blame_line *lines, int nlines, int blame_complete,
1093 int selected_line, int *first_displayed_line, int *last_displayed_line,
1094 int *eof, int max_lines)
1096 const struct got_error *err;
1097 int lineno = 0, nprinted = 0;
1102 struct tog_blame_line *blame_line;
1103 struct got_object_id *prev_id = NULL;
1106 err = got_object_id_str(&id_str, id);
1113 if (asprintf(&line, "commit: %s", id_str) == -1) {
1114 err = got_error_from_errno();
1119 err = format_line(&wline, &width, line, COLS);
1121 waddwstr(window, wline);
1123 waddch(window, '\n');
1125 if (asprintf(&line, "[%d/%d] %s%s",
1126 *first_displayed_line - 1 + selected_line, nlines,
1127 blame_complete ? "" : "annotating ", path) == -1) {
1129 return got_error_from_errno();
1132 err = format_line(&wline, &width, line, COLS);
1136 waddwstr(window, wline);
1138 waddch(window, '\n');
1141 while (nprinted < max_lines - 2) {
1142 line = parse_next_line(f, &len);
1147 if (++lineno < *first_displayed_line) {
1152 wlimit = COLS < 9 ? 0 : COLS - 9;
1153 err = format_line(&wline, &width, line, wlimit);
1159 if (nprinted == selected_line - 1)
1162 blame_line = &lines[lineno - 1];
1163 if (blame_line->annotated && prev_id &&
1164 got_object_id_cmp(prev_id, blame_line->id) == 0)
1165 waddstr(window, " ");
1166 else if (blame_line->annotated) {
1168 err = got_object_id_str(&id_str, blame_line->id);
1173 wprintw(window, "%.8s ", id_str);
1175 prev_id = blame_line->id;
1177 waddstr(window, "........ ");
1181 waddwstr(window, wline);
1182 while (width < wlimit) {
1183 waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1186 if (nprinted == selected_line - 1)
1188 if (++nprinted == 1)
1189 *first_displayed_line = lineno;
1192 *last_displayed_line = lineno;
1200 struct tog_blame_cb_args {
1201 pthread_mutex_t *mutex;
1202 struct tog_blame_line *lines; /* one per line */
1205 struct got_object_id *commit_id;
1208 int *first_displayed_line;
1209 int *last_displayed_line;
1214 static const struct got_error *
1215 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1217 const struct got_error *err = NULL;
1218 struct tog_blame_cb_args *a = arg;
1219 struct tog_blame_line *line;
1222 if (nlines != a->nlines ||
1223 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1224 return got_error(GOT_ERR_RANGE);
1226 if (pthread_mutex_lock(a->mutex) != 0)
1227 return got_error_from_errno();
1229 if (*a->quit) { /* user has quit the blame view */
1230 err = got_error(GOT_ERR_ITER_COMPLETED);
1235 goto done; /* no change in this commit */
1237 line = &a->lines[lineno - 1];
1238 if (line->annotated)
1241 line->id = got_object_id_dup(id);
1242 if (line->id == NULL) {
1243 err = got_error_from_errno();
1246 line->annotated = 1;
1248 err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1249 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1250 a->last_displayed_line, &eof, LINES);
1252 if (pthread_mutex_unlock(a->mutex) != 0)
1253 return got_error_from_errno();
1257 struct tog_blame_thread_args {
1259 struct got_repository *repo;
1260 struct tog_blame_cb_args *cb_args;
1265 blame_thread(void *arg)
1267 const struct got_error *err;
1268 struct tog_blame_thread_args *ta = arg;
1269 struct tog_blame_cb_args *a = ta->cb_args;
1272 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1273 blame_cb, ta->cb_args);
1278 if (pthread_mutex_lock(a->mutex) != 0)
1279 return (void *)got_error_from_errno();
1281 err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1282 a->lines, a->nlines, 1, *a->selected_line, a->first_displayed_line,
1283 a->last_displayed_line, &eof, LINES);
1285 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1286 err = got_error_from_errno();
1291 static struct got_object_id *
1292 get_selected_commit_id(struct tog_blame_line *lines,
1293 int first_displayed_line, int selected_line)
1295 struct tog_blame_line *line;
1297 line = &lines[first_displayed_line - 1 + selected_line - 1];
1298 if (!line->annotated)
1304 static const struct got_error *
1305 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1306 struct tog_blame_line *lines, int first_displayed_line,
1307 int selected_line, struct got_repository *repo)
1309 const struct got_error *err = NULL;
1310 struct got_commit_object *commit = NULL;
1311 struct got_object_id *selected_id;
1312 struct got_object_qid *pid;
1317 selected_id = get_selected_commit_id(lines,
1318 first_displayed_line, selected_line);
1319 if (selected_id == NULL)
1322 err = got_object_open(obj, repo, selected_id);
1326 err = got_object_commit_open(&commit, repo, *obj);
1330 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1332 err = got_object_open(pobj, repo, pid->id);
1338 got_object_commit_close(commit);
1345 struct tog_blame_line *lines;
1348 struct tog_blame_thread_args thread_args;
1349 struct tog_blame_cb_args cb_args;
1351 struct got_object_id *commit_id;
1354 static const struct got_error *
1355 stop_blame(struct tog_blame *blame)
1357 const struct got_error *err = NULL;
1360 if (blame->thread) {
1361 if (pthread_join(blame->thread, (void **)&err) != 0)
1362 err = got_error_from_errno();
1363 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1365 blame->thread = NULL;
1367 if (blame->thread_args.repo) {
1368 got_repo_close(blame->thread_args.repo);
1369 blame->thread_args.repo = NULL;
1375 for (i = 0; i < blame->nlines; i++)
1376 free(blame->lines[i].id);
1378 blame->lines = NULL;
1379 free(blame->commit_id);
1380 blame->commit_id = NULL;
1385 static const struct got_error *
1386 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex, int *blame_complete,
1387 int *first_displayed_line, int *last_displayed_line,
1388 int *selected_line, int *done, const char *path,
1389 struct got_object_id *commit_id,
1390 struct got_repository *repo)
1392 const struct got_error *err = NULL;
1393 struct got_blob_object *blob = NULL;
1394 struct got_repository *thread_repo = NULL;
1395 struct got_object *obj;
1397 err = got_object_open_by_path(&obj, repo, commit_id, path);
1400 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1401 err = got_error(GOT_ERR_OBJ_TYPE);
1405 err = got_object_blob_open(&blob, repo, obj, 8192);
1408 blame->f = got_opentemp();
1409 if (blame->f == NULL) {
1410 err = got_error_from_errno();
1413 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1418 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1419 if (blame->lines == NULL) {
1420 err = got_error_from_errno();
1424 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1428 blame->cb_args.lines = blame->lines;
1429 blame->cb_args.nlines = blame->nlines;
1430 blame->cb_args.mutex = mutex;
1431 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1432 if (blame->cb_args.commit_id == NULL) {
1433 err = got_error_from_errno();
1436 blame->cb_args.f = blame->f;
1437 blame->cb_args.path = path;
1438 blame->cb_args.first_displayed_line = first_displayed_line;
1439 blame->cb_args.selected_line = selected_line;
1440 blame->cb_args.last_displayed_line = last_displayed_line;
1441 blame->cb_args.quit = done;
1443 blame->thread_args.path = path;
1444 blame->thread_args.repo = thread_repo;
1445 blame->thread_args.cb_args = &blame->cb_args;
1446 blame->thread_args.complete = blame_complete;
1447 *blame_complete = 0;
1449 if (pthread_create(&blame->thread, NULL, blame_thread,
1450 &blame->thread_args) != 0) {
1451 err = got_error_from_errno();
1457 got_object_blob_close(blob);
1459 got_object_close(obj);
1465 static const struct got_error *
1466 show_blame_view(const char *path, struct got_object_id *commit_id,
1467 struct got_repository *repo)
1469 const struct got_error *err = NULL, *thread_err = NULL;
1470 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1471 int selected_line = first_displayed_line;
1472 int eof, blame_complete = 0;
1473 struct got_object *obj = NULL, *pobj = NULL;
1474 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1475 struct tog_blame blame;
1476 int blame_running = 0;
1477 struct got_object_id_queue blamed_commits;
1478 struct got_object_qid *blamed_commit = NULL;
1480 SIMPLEQ_INIT(&blamed_commits);
1482 if (pthread_mutex_init(&mutex, NULL) != 0) {
1483 err = got_error_from_errno();
1487 err = got_object_qid_alloc(&blamed_commit, commit_id);
1490 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1492 if (tog_blame_view.window == NULL) {
1493 tog_blame_view.window = newwin(0, 0, 0, 0);
1494 if (tog_blame_view.window == NULL)
1495 return got_error_from_errno();
1496 keypad(tog_blame_view.window, TRUE);
1498 if (tog_blame_view.panel == NULL) {
1499 tog_blame_view.panel = new_panel(tog_blame_view.window);
1500 if (tog_blame_view.panel == NULL)
1501 return got_error_from_errno();
1503 show_panel(tog_blame_view.panel);
1505 memset(&blame, 0, sizeof(blame));
1506 err = run_blame(&blame, &mutex, &blame_complete,
1507 &first_displayed_line, &last_displayed_line,
1508 &selected_line, &done, path, blamed_commit->id, repo);
1513 if (pthread_mutex_lock(&mutex) != 0) {
1514 err = got_error_from_errno();
1517 err = draw_blame(tog_blame_view.window, blamed_commit->id,
1518 blame.f, path, blame.lines, blame.nlines, blame_complete,
1519 selected_line, &first_displayed_line, &last_displayed_line,
1521 if (pthread_mutex_unlock(&mutex) != 0) {
1522 err = got_error_from_errno();
1527 nodelay(stdscr, FALSE);
1528 ch = wgetch(tog_blame_view.window);
1529 nodelay(stdscr, TRUE);
1530 if (pthread_mutex_lock(&mutex) != 0) {
1531 err = got_error_from_errno();
1540 if (selected_line > 1)
1542 else if (selected_line == 1 &&
1543 first_displayed_line > 1)
1544 first_displayed_line--;
1548 if (first_displayed_line == 1) {
1552 if (first_displayed_line > LINES - 2)
1553 first_displayed_line -= (LINES - 2);
1555 first_displayed_line = 1;
1559 if (selected_line < LINES - 2 &&
1560 first_displayed_line + selected_line <=
1563 else if (last_displayed_line < blame.nlines)
1564 first_displayed_line++;
1568 struct got_object_id *id;
1569 id = get_selected_commit_id(blame.lines,
1570 first_displayed_line, selected_line);
1571 if (id == NULL || got_object_id_cmp(id,
1572 blamed_commit->id) == 0)
1574 err = open_selected_commit(&pobj, &obj,
1575 blame.lines, first_displayed_line,
1576 selected_line, repo);
1579 if (pobj == NULL && obj == NULL)
1581 if (ch == 'p' && pobj == NULL)
1584 if (pthread_mutex_unlock(&mutex) != 0) {
1585 err = got_error_from_errno();
1588 thread_err = stop_blame(&blame);
1591 if (pthread_mutex_lock(&mutex) != 0) {
1592 err = got_error_from_errno();
1597 id = got_object_get_id(ch == 'b' ? obj : pobj);
1598 got_object_close(obj);
1601 got_object_close(pobj);
1605 err = got_error_from_errno();
1608 err = got_object_qid_alloc(&blamed_commit, id);
1612 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1613 blamed_commit, entry);
1614 err = run_blame(&blame, &mutex,
1615 &blame_complete, &first_displayed_line,
1616 &last_displayed_line, &selected_line,
1617 &done, path, blamed_commit->id, repo);
1624 struct got_object_qid *first;
1625 first = SIMPLEQ_FIRST(&blamed_commits);
1626 if (!got_object_id_cmp(first->id, commit_id))
1629 if (pthread_mutex_unlock(&mutex) != 0) {
1630 err = got_error_from_errno();
1633 thread_err = stop_blame(&blame);
1636 if (pthread_mutex_lock(&mutex) != 0) {
1637 err = got_error_from_errno();
1642 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1643 got_object_qid_free(blamed_commit);
1644 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1645 err = run_blame(&blame, &mutex,
1646 &blame_complete, &first_displayed_line,
1647 &last_displayed_line, &selected_line,
1648 &done, path, blamed_commit->id, repo);
1656 err = open_selected_commit(&pobj, &obj,
1657 blame.lines, first_displayed_line,
1658 selected_line, repo);
1661 if (pobj == NULL && obj == NULL)
1663 err = show_diff_view(pobj, obj, repo);
1665 got_object_close(pobj);
1668 got_object_close(obj);
1670 show_panel(tog_blame_view.panel);
1676 if (last_displayed_line >= blame.nlines &&
1677 selected_line < LINES - 2) {
1678 selected_line = MIN(blame.nlines,
1682 if (last_displayed_line + LINES - 2 <=
1684 first_displayed_line += LINES - 2;
1686 first_displayed_line =
1687 blame.nlines - (LINES - 3);
1692 if (pthread_mutex_unlock(&mutex) != 0)
1693 err = got_error_from_errno();
1694 if (err || thread_err)
1699 got_object_close(pobj);
1701 thread_err = stop_blame(&blame);
1702 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1703 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1704 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1705 got_object_qid_free(blamed_commit);
1707 return thread_err ? thread_err : err;
1710 static const struct got_error *
1711 cmd_blame(int argc, char *argv[])
1713 const struct got_error *error;
1714 struct got_repository *repo = NULL;
1715 char *repo_path = NULL;
1717 struct got_object_id *commit_id = NULL;
1718 char *commit_id_str = NULL;
1722 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1726 while ((ch = getopt(argc, argv, "c:")) != -1) {
1729 commit_id_str = optarg;
1742 } else if (argc == 1) {
1743 repo_path = getcwd(NULL, 0);
1744 if (repo_path == NULL)
1745 return got_error_from_errno();
1747 } else if (argc == 2) {
1748 repo_path = realpath(argv[0], NULL);
1749 if (repo_path == NULL)
1750 return got_error_from_errno();
1755 error = got_repo_open(&repo, repo_path);
1760 if (commit_id_str == NULL) {
1761 struct got_reference *head_ref;
1762 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1765 error = got_ref_resolve(&commit_id, repo, head_ref);
1766 got_ref_close(head_ref);
1768 struct got_object *obj;
1769 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1772 commit_id = got_object_get_id(obj);
1773 if (commit_id == NULL)
1774 error = got_error_from_errno();
1775 got_object_close(obj);
1780 error = show_blame_view(path, commit_id, repo);
1784 got_repo_close(repo);
1788 static const struct got_error *
1789 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1790 struct got_tree_entry **last_displayed_entry,
1791 struct got_tree_entry **selected_entry, int *ndisplayed,
1792 WINDOW *window, const char *label, int show_ids, const char *parent_path,
1793 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1795 const struct got_error *err = NULL;
1796 struct got_tree_entry *te;
1807 err = format_line(&wline, &width, label, COLS);
1810 waddwstr(window, wline);
1812 waddch(window, '\n');
1815 err = format_line(&wline, &width, parent_path, COLS);
1818 waddwstr(window, wline);
1820 waddch(window, '\n');
1823 waddch(window, '\n');
1827 te = SIMPLEQ_FIRST(&entries->head);
1828 if (*first_displayed_entry == NULL) {
1829 if (selected == 0) {
1831 *selected_entry = NULL;
1833 waddstr(window, " ..\n"); /* parent directory */
1842 while (te != *first_displayed_entry)
1843 te = SIMPLEQ_NEXT(te, entry);
1847 char *line = NULL, *id_str = NULL;
1850 err = got_object_id_str(&id_str, te->id);
1852 return got_error_from_errno();
1854 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
1855 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
1857 return got_error_from_errno();
1860 err = format_line(&wline, &width, line, COLS);
1865 if (n == selected) {
1867 *selected_entry = te;
1869 waddwstr(window, wline);
1871 waddch(window, '\n');
1877 *last_displayed_entry = te;
1880 te = SIMPLEQ_NEXT(te, entry);
1887 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1888 const struct got_tree_entries *entries, int isroot)
1890 struct got_tree_entry *te, *prev;
1893 if (*first_displayed_entry == NULL)
1896 te = SIMPLEQ_FIRST(&entries->head);
1897 if (*first_displayed_entry == te) {
1899 *first_displayed_entry = NULL;
1903 /* XXX this is stupid... switch to TAILQ? */
1904 for (i = 0; i < maxscroll; i++) {
1905 while (te != *first_displayed_entry) {
1907 te = SIMPLEQ_NEXT(te, entry);
1909 *first_displayed_entry = prev;
1910 te = SIMPLEQ_FIRST(&entries->head);
1912 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1913 *first_displayed_entry = NULL;
1917 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1918 struct got_tree_entry *last_displayed_entry,
1919 const struct got_tree_entries *entries)
1921 struct got_tree_entry *next;
1924 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1927 if (*first_displayed_entry)
1928 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1930 next = SIMPLEQ_FIRST(&entries->head);
1932 *first_displayed_entry = next;
1933 if (++n >= maxscroll)
1935 next = SIMPLEQ_NEXT(next, entry);
1939 struct tog_parent_tree {
1940 TAILQ_ENTRY(tog_parent_tree) entry;
1941 struct got_tree_object *tree;
1942 struct got_tree_entry *first_displayed_entry;
1943 struct got_tree_entry *selected_entry;
1947 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1949 static const struct got_error *
1950 tree_entry_path(char **path, struct tog_parent_trees *parents,
1951 struct got_tree_entry *te)
1953 const struct got_error *err = NULL;
1954 struct tog_parent_tree *pt;
1955 size_t len = 2; /* for leading slash and NUL */
1957 TAILQ_FOREACH(pt, parents, entry)
1958 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1960 len += strlen(te->name);
1962 *path = calloc(1, len);
1964 return got_error_from_errno();
1967 pt = TAILQ_LAST(parents, tog_parent_trees);
1969 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1970 err = got_error(GOT_ERR_NO_SPACE);
1973 if (strlcat(*path, "/", len) >= len) {
1974 err = got_error(GOT_ERR_NO_SPACE);
1977 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1980 if (strlcat(*path, te->name, len) >= len) {
1981 err = got_error(GOT_ERR_NO_SPACE);
1993 static const struct got_error *
1994 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1995 struct got_object_id *commit_id, struct got_repository *repo)
1997 const struct got_error *err = NULL;
2000 err = tree_entry_path(&path, parents, te);
2004 err = show_blame_view(path, commit_id, repo);
2009 static const struct got_error *
2010 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2011 struct got_repository *repo)
2013 const struct got_error *err = NULL;
2014 int ch, done = 0, selected = 0, show_ids = 0;
2015 struct got_tree_object *tree = root;
2016 const struct got_tree_entries *entries;
2017 struct got_tree_entry *first_displayed_entry = NULL;
2018 struct got_tree_entry *last_displayed_entry = NULL;
2019 struct got_tree_entry *selected_entry = NULL;
2020 char *commit_id_str = NULL, *tree_label = NULL;
2021 int nentries, ndisplayed;
2022 struct tog_parent_trees parents;
2024 TAILQ_INIT(&parents);
2026 err = got_object_id_str(&commit_id_str, commit_id);
2030 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2031 err = got_error_from_errno();
2035 if (tog_tree_view.window == NULL) {
2036 tog_tree_view.window = newwin(0, 0, 0, 0);
2037 if (tog_tree_view.window == NULL)
2038 return got_error_from_errno();
2039 keypad(tog_tree_view.window, TRUE);
2041 if (tog_tree_view.panel == NULL) {
2042 tog_tree_view.panel = new_panel(tog_tree_view.window);
2043 if (tog_tree_view.panel == NULL)
2044 return got_error_from_errno();
2046 show_panel(tog_tree_view.panel);
2048 entries = got_object_tree_get_entries(root);
2049 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2052 entries = got_object_tree_get_entries(tree);
2053 nentries = entries->nentries;
2055 nentries++; /* '..' directory */
2057 err = tree_entry_path(&parent_path, &parents, NULL);
2061 err = draw_tree_entries(&first_displayed_entry,
2062 &last_displayed_entry, &selected_entry, &ndisplayed,
2063 tog_tree_view.window, tree_label, show_ids,
2064 parent_path, entries, selected, LINES, tree == root);
2069 nodelay(stdscr, FALSE);
2070 ch = wgetch(tog_tree_view.window);
2071 nodelay(stdscr, TRUE);
2077 show_ids = !show_ids;
2085 tree_scroll_up(&first_displayed_entry, 1,
2086 entries, tree == root);
2089 if (SIMPLEQ_FIRST(&entries->head) ==
2090 first_displayed_entry) {
2092 first_displayed_entry = NULL;
2096 tree_scroll_up(&first_displayed_entry, LINES,
2097 entries, tree == root);
2101 if (selected < ndisplayed - 1) {
2105 tree_scroll_down(&first_displayed_entry, 1,
2106 last_displayed_entry, entries);
2109 tree_scroll_down(&first_displayed_entry, LINES,
2110 last_displayed_entry, entries);
2111 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2113 /* can't scroll any further; move cursor down */
2114 if (selected < ndisplayed - 1)
2115 selected = ndisplayed - 1;
2119 if (selected_entry == NULL) {
2120 struct tog_parent_tree *parent;
2122 /* user selected '..' */
2125 parent = TAILQ_FIRST(&parents);
2126 TAILQ_REMOVE(&parents, parent, entry);
2127 got_object_tree_close(tree);
2128 tree = parent->tree;
2129 first_displayed_entry =
2130 parent->first_displayed_entry;
2131 selected_entry = parent->selected_entry;
2132 selected = parent->selected;
2134 } else if (S_ISDIR(selected_entry->mode)) {
2135 struct tog_parent_tree *parent;
2136 struct got_tree_object *child;
2137 err = got_object_open_as_tree(
2138 &child, repo, selected_entry->id);
2141 parent = calloc(1, sizeof(*parent));
2142 if (parent == NULL) {
2143 err = got_error_from_errno();
2146 parent->tree = tree;
2147 parent->first_displayed_entry =
2148 first_displayed_entry;
2149 parent->selected_entry = selected_entry;
2150 parent->selected = selected;
2151 TAILQ_INSERT_HEAD(&parents, parent,
2155 first_displayed_entry = NULL;
2156 } else if (S_ISREG(selected_entry->mode)) {
2157 err = blame_tree_entry(selected_entry,
2158 &parents, commit_id, repo);
2164 if (selected > LINES)
2165 selected = ndisplayed - 1;
2173 free(commit_id_str);
2174 while (!TAILQ_EMPTY(&parents)) {
2175 struct tog_parent_tree *parent;
2176 parent = TAILQ_FIRST(&parents);
2177 TAILQ_REMOVE(&parents, parent, entry);
2182 got_object_tree_close(tree);
2190 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2195 static const struct got_error *
2196 cmd_tree(int argc, char *argv[])
2198 const struct got_error *error;
2199 struct got_repository *repo = NULL;
2200 char *repo_path = NULL;
2201 struct got_object_id *commit_id = NULL;
2202 char *commit_id_arg = NULL;
2203 struct got_commit_object *commit = NULL;
2204 struct got_tree_object *tree = NULL;
2208 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2212 while ((ch = getopt(argc, argv, "c:")) != -1) {
2215 commit_id_arg = optarg;
2227 repo_path = getcwd(NULL, 0);
2228 if (repo_path == NULL)
2229 return got_error_from_errno();
2230 } else if (argc == 1) {
2231 repo_path = realpath(argv[0], NULL);
2232 if (repo_path == NULL)
2233 return got_error_from_errno();
2237 error = got_repo_open(&repo, repo_path);
2242 if (commit_id_arg == NULL) {
2243 error = get_head_commit_id(&commit_id, repo);
2247 struct got_object *obj;
2248 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2249 if (error == NULL) {
2250 commit_id = got_object_get_id(obj);
2251 if (commit_id == NULL)
2252 error = got_error_from_errno();
2258 error = got_object_open_as_commit(&commit, repo, commit_id);
2262 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2266 error = show_tree_view(tree, commit_id, repo);
2270 got_object_commit_close(commit);
2272 got_object_tree_close(tree);
2274 got_repo_close(repo);
2284 intrflush(stdscr, FALSE);
2285 keypad(stdscr, TRUE);
2294 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2295 "Available commands:\n", getprogname());
2296 for (i = 0; i < nitems(tog_commands); i++) {
2297 struct tog_cmd *cmd = &tog_commands[i];
2298 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2304 make_argv(const char *arg0, const char *arg1)
2307 int argc = (arg1 == NULL ? 1 : 2);
2309 argv = calloc(argc, sizeof(char *));
2312 argv[0] = strdup(arg0);
2313 if (argv[0] == NULL)
2316 argv[1] = strdup(arg1);
2317 if (argv[1] == NULL)
2325 main(int argc, char *argv[])
2327 const struct got_error *error = NULL;
2328 struct tog_cmd *cmd = NULL;
2330 char **cmd_argv = NULL;
2332 setlocale(LC_ALL, "");
2334 while ((ch = getopt(argc, argv, "h")) != -1) {
2353 /* Build an argument vector which runs a default command. */
2354 cmd = &tog_commands[0];
2355 cmd_argv = make_argv(cmd->name, NULL);
2360 /* Did the user specific a command? */
2361 for (i = 0; i < nitems(tog_commands); i++) {
2362 if (strncmp(tog_commands[i].name, argv[0],
2363 strlen(argv[0])) == 0) {
2364 cmd = &tog_commands[i];
2366 tog_commands[i].cmd_usage();
2371 /* Did the user specify a repository? */
2372 char *repo_path = realpath(argv[0], NULL);
2374 struct got_repository *repo;
2375 error = got_repo_open(&repo, repo_path);
2377 got_repo_close(repo);
2379 error = got_error_from_errno();
2382 fprintf(stderr, "%s: '%s' is not a "
2383 "known command\n", getprogname(),
2387 fprintf(stderr, "%s: '%s' is neither a known "
2388 "command nor a path to a repository\n",
2389 getprogname(), argv[0]);
2393 cmd = &tog_commands[0];
2394 cmd_argv = make_argv(cmd->name, repo_path);
2402 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2409 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);