Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <errno.h>
21 #define _XOPEN_SOURCE_EXTENDED
22 #include <curses.h>
23 #undef _XOPEN_SOURCE_EXTENDED
24 #include <panel.h>
25 #include <locale.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <string.h>
30 #include <err.h>
31 #include <unistd.h>
32 #include <util.h>
33 #include <limits.h>
34 #include <wchar.h>
35 #include <time.h>
36 #include <pthread.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_diff.h"
43 #include "got_opentemp.h"
44 #include "got_commit_graph.h"
45 #include "got_utf8.h"
46 #include "got_blame.h"
48 #ifndef MIN
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #endif
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct tog_cmd {
57 const char *name;
58 const struct got_error *(*cmd_main)(int, char *[]);
59 void (*cmd_usage)(void);
60 const char *descr;
61 };
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" },
83 };
85 static struct tog_view {
86 WINDOW *window;
87 PANEL *panel;
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 *, const char *);
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 *);
101 __dead static void
102 usage_log(void)
104 endwin();
105 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
106 getprogname());
107 exit(1);
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)
114 char *vis = NULL;
115 const struct got_error *err = NULL;
117 *ws = NULL;
118 *wlen = mbstowcs(NULL, s, 0);
119 if (*wlen == (size_t)-1) {
120 int vislen;
121 if (errno != EILSEQ)
122 return got_error_from_errno();
124 /* byte string invalid in current encoding; try to "fix" it */
125 err = got_mbsavis(&vis, &vislen, s);
126 if (err)
127 return err;
128 *wlen = mbstowcs(NULL, vis, 0);
129 if (*wlen == (size_t)-1) {
130 err = got_error_from_errno(); /* give up */
131 goto done;
135 *ws = calloc(*wlen + 1, sizeof(*ws));
136 if (*ws == NULL) {
137 err = got_error_from_errno();
138 goto done;
141 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
142 err = got_error_from_errno();
143 done:
144 free(vis);
145 if (err) {
146 free(*ws);
147 *ws = NULL;
148 *wlen = 0;
150 return err;
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;
158 int cols = 0;
159 wchar_t *wline = NULL;
160 size_t wlen;
161 int i;
163 *wlinep = NULL;
164 *widthp = 0;
166 err = mbs2ws(&wline, &wlen, line);
167 if (err)
168 return err;
170 i = 0;
171 while (i < wlen && cols < wlimit) {
172 int width = wcwidth(wline[i]);
173 switch (width) {
174 case 0:
175 i++;
176 break;
177 case 1:
178 case 2:
179 if (cols + width <= wlimit) {
180 cols += width;
181 i++;
183 break;
184 case -1:
185 if (wline[i] == L'\t')
186 cols += TABSIZE - ((cols + 1) % TABSIZE);
187 i++;
188 break;
189 default:
190 err = got_error_from_errno();
191 goto done;
194 wline[i] = L'\0';
195 if (widthp)
196 *widthp = cols;
197 done:
198 if (err)
199 free(wline);
200 else
201 *wlinep = wline;
202 return err;
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;
215 char *line = NULL;
216 int col, limit;
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)
222 >= sizeof(datebuf))
223 return got_error(GOT_ERR_NO_SPACE);
225 if (avail < date_display_cols)
226 limit = MIN(sizeof(datebuf) - 1, avail);
227 else
228 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
229 waddnstr(tog_log_view.window, datebuf, limit);
230 col = limit + 1;
231 if (col > avail)
232 goto done;
234 author0 = strdup(commit->author);
235 if (author0 == NULL) {
236 err = got_error_from_errno();
237 goto done;
239 author = author0;
240 smallerthan = strchr(author, '<');
241 if (smallerthan)
242 *smallerthan = '\0';
243 else {
244 char *at = strchr(author, '@');
245 if (at)
246 *at = '\0';
248 limit = avail - col;
249 err = format_line(&wauthor, &author_width, author, limit);
250 if (err)
251 goto done;
252 waddwstr(tog_log_view.window, wauthor);
253 col += author_width;
254 while (col <= avail && author_width < author_display_cols + 1) {
255 waddch(tog_log_view.window, ' ');
256 col++;
257 author_width++;
259 if (col > avail)
260 goto done;
262 logmsg0 = strdup(commit->logmsg);
263 if (logmsg0 == NULL) {
264 err = got_error_from_errno();
265 goto done;
267 logmsg = logmsg0;
268 while (*logmsg == '\n')
269 logmsg++;
270 newline = strchr(logmsg, '\n');
271 if (newline)
272 *newline = '\0';
273 limit = avail - col;
274 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
275 if (err)
276 goto done;
277 waddwstr(tog_log_view.window, wlogmsg);
278 col += logmsg_width;
279 while (col <= avail) {
280 waddch(tog_log_view.window, ' ');
281 col++;
283 done:
284 free(logmsg0);
285 free(wlogmsg);
286 free(author0);
287 free(wauthor);
288 free(line);
289 return err;
292 struct commit_queue_entry {
293 TAILQ_ENTRY(commit_queue_entry) entry;
294 struct got_object_id *id;
295 struct got_commit_object *commit;
296 };
297 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
298 struct commit_queue {
299 int ncommits;
300 struct commit_queue_head head;
301 };
303 static struct commit_queue_entry *
304 alloc_commit_queue_entry(struct got_commit_object *commit,
305 struct got_object_id *id)
307 struct commit_queue_entry *entry;
309 entry = calloc(1, sizeof(*entry));
310 if (entry == NULL)
311 return NULL;
313 entry->id = id;
314 entry->commit = commit;
315 return entry;
318 static void
319 pop_commit(struct commit_queue *commits)
321 struct commit_queue_entry *entry;
323 entry = TAILQ_FIRST(&commits->head);
324 TAILQ_REMOVE(&commits->head, entry, entry);
325 got_object_commit_close(entry->commit);
326 commits->ncommits--;
327 /* Don't free entry->id! It is owned by the commit graph. */
328 free(entry);
331 static void
332 free_commits(struct commit_queue *commits)
334 while (!TAILQ_EMPTY(&commits->head))
335 pop_commit(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, int minqueue, int init,
341 struct got_repository *repo, const char *path)
343 const struct got_error *err = NULL;
344 struct got_object_id *id;
345 struct commit_queue_entry *entry;
346 int nfetched, nqueued = 0, found_obj = 0;
348 err = got_commit_graph_iter_start(graph, start_id);
349 if (err)
350 return err;
352 entry = TAILQ_LAST(&commits->head, commit_queue_head);
353 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
354 int nfetched;
356 /* Start ID's commit is already on the queue; skip over it. */
357 err = got_commit_graph_iter_next(&id, graph);
358 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
359 return err;
361 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
362 if (err)
363 return err;
366 while (1) {
367 struct got_commit_object *commit;
369 err = got_commit_graph_iter_next(&id, graph);
370 if (err) {
371 if (err->code != GOT_ERR_ITER_NEED_MORE)
372 break;
373 if (nqueued >= minqueue) {
374 err = NULL;
375 break;
377 err = got_commit_graph_fetch_commits(&nfetched,
378 graph, 1, repo);
379 if (err)
380 return err;
381 continue;
383 if (id == NULL)
384 break;
386 err = got_object_open_as_commit(&commit, repo, id);
387 if (err)
388 break;
390 if (path) {
391 struct got_object *obj;
392 struct got_object_qid *pid;
393 int changed = 0;
395 err = got_object_open_by_path(&obj, repo, id, path);
396 if (err) {
397 if (err->code == GOT_ERR_NO_OBJ &&
398 (found_obj || !init)) {
399 /* History stops here. */
400 err = got_error(GOT_ERR_ITER_COMPLETED);
402 break;
404 found_obj = 1;
406 pid = SIMPLEQ_FIRST(&commit->parent_ids);
407 if (pid != NULL) {
408 struct got_object *pobj;
409 err = got_object_open_by_path(&pobj, repo,
410 pid->id, path);
411 if (err) {
412 if (err->code != GOT_ERR_NO_OBJ) {
413 got_object_close(obj);
414 break;
416 err = NULL;
417 changed = 1;
418 } else {
419 changed = (got_object_id_cmp(
420 got_object_get_id(obj),
421 got_object_get_id(pobj)) != 0);
422 got_object_close(pobj);
425 if (!changed) {
426 got_object_commit_close(commit);
427 continue;
431 entry = alloc_commit_queue_entry(commit, id);
432 if (entry == NULL) {
433 err = got_error_from_errno();
434 break;
436 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
437 nqueued++;
438 commits->ncommits++;
441 return err;
444 static const struct got_error *
445 fetch_next_commit(struct commit_queue_entry **pentry,
446 struct commit_queue_entry *entry, struct commit_queue *commits,
447 struct got_commit_graph *graph, struct got_repository *repo,
448 const char *path)
450 const struct got_error *err = NULL;
452 *pentry = NULL;
454 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
455 if (err)
456 return err;
458 /* Next entry to display should now be available. */
459 *pentry = TAILQ_NEXT(entry, entry);
460 if (*pentry == NULL)
461 return got_error(GOT_ERR_NO_OBJ);
463 return NULL;
466 static const struct got_error *
467 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
469 const struct got_error *err = NULL;
470 struct got_reference *head_ref;
472 *head_id = NULL;
474 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
475 if (err)
476 return err;
478 err = got_ref_resolve(head_id, repo, head_ref);
479 got_ref_close(head_ref);
480 if (err) {
481 *head_id = NULL;
482 return err;
485 return NULL;
488 static const struct got_error *
489 draw_commits(struct commit_queue_entry **last,
490 struct commit_queue_entry **selected, struct commit_queue_entry *first,
491 struct commit_queue *commits, int selected_idx, int limit,
492 struct got_commit_graph *graph, struct got_repository *repo,
493 const char *path)
495 const struct got_error *err = NULL;
496 struct commit_queue_entry *entry;
497 int ncommits, width;
498 char *id_str, *header;
499 wchar_t *wline;
501 entry = first;
502 ncommits = 0;
503 while (entry) {
504 if (ncommits == selected_idx) {
505 *selected = entry;
506 break;
508 entry = TAILQ_NEXT(entry, entry);
509 ncommits++;
512 err = got_object_id_str(&id_str, (*selected)->id);
513 if (err)
514 return err;
516 if (path) {
517 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
518 err = got_error_from_errno();
519 free(id_str);
520 return err;
522 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
523 err = got_error_from_errno();
524 free(id_str);
525 return err;
527 free(id_str);
528 err = format_line(&wline, &width, header, COLS);
529 if (err) {
530 free(header);
531 return err;
533 free(header);
535 werase(tog_log_view.window);
537 waddwstr(tog_log_view.window, wline);
538 if (width < COLS)
539 waddch(tog_log_view.window, '\n');
540 free(wline);
541 if (limit <= 1)
542 return NULL;
544 entry = first;
545 *last = first;
546 ncommits = 0;
547 while (entry) {
548 if (ncommits >= limit - 1)
549 break;
550 if (ncommits == selected_idx)
551 wstandout(tog_log_view.window);
552 err = draw_commit(entry->commit, entry->id);
553 if (ncommits == selected_idx)
554 wstandend(tog_log_view.window);
555 if (err)
556 break;
557 ncommits++;
558 *last = entry;
559 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
560 err = queue_commits(graph, commits, entry->id, 1,
561 0, repo, path);
562 if (err) {
563 if (err->code != GOT_ERR_ITER_COMPLETED)
564 return err;
565 err = NULL;
568 entry = TAILQ_NEXT(entry, entry);
571 update_panels();
572 doupdate();
574 return err;
577 static void
578 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
579 struct commit_queue *commits)
581 struct commit_queue_entry *entry;
582 int nscrolled = 0;
584 entry = TAILQ_FIRST(&commits->head);
585 if (*first_displayed_entry == entry)
586 return;
588 entry = *first_displayed_entry;
589 while (entry && nscrolled < maxscroll) {
590 entry = TAILQ_PREV(entry, commit_queue_head, entry);
591 if (entry) {
592 *first_displayed_entry = entry;
593 nscrolled++;
598 static const struct got_error *
599 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
600 struct commit_queue_entry *last_displayed_entry,
601 struct commit_queue *commits, struct got_commit_graph *graph,
602 struct got_repository *repo, const char *path)
604 const struct got_error *err = NULL;
605 struct commit_queue_entry *pentry;
606 int nscrolled = 0;
608 do {
609 pentry = TAILQ_NEXT(last_displayed_entry, entry);
610 if (pentry == NULL) {
611 err = fetch_next_commit(&pentry, last_displayed_entry,
612 commits, graph, repo, path);
613 if (err || pentry == NULL)
614 break;
616 last_displayed_entry = pentry;
618 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
619 if (pentry == NULL)
620 break;
621 *first_displayed_entry = pentry;
622 } while (++nscrolled < maxscroll);
624 return err;
627 static const struct got_error *
628 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
630 const struct got_error *err;
631 struct got_object *obj1 = NULL, *obj2 = NULL;
632 struct got_object_qid *parent_id;
634 err = got_object_open(&obj2, repo, entry->id);
635 if (err)
636 return err;
638 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
639 if (parent_id) {
640 err = got_object_open(&obj1, repo, parent_id->id);
641 if (err)
642 goto done;
645 err = show_diff_view(obj1, obj2, repo);
646 done:
647 if (obj1)
648 got_object_close(obj1);
649 if (obj2)
650 got_object_close(obj2);
651 return err;
654 static const struct got_error *
655 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
657 const struct got_error *err = NULL;
658 struct got_tree_object *tree;
660 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
661 if (err)
662 return err;
664 err = show_tree_view(tree, entry->id, repo);
665 got_object_tree_close(tree);
666 return err;
669 static const struct got_error *
670 show_log_view(struct got_object_id *start_id, struct got_repository *repo,
671 const char *path)
673 const struct got_error *err = NULL;
674 struct got_object_id *head_id = NULL;
675 int ch, done = 0, selected = 0, nfetched;
676 struct got_commit_graph *graph = NULL;
677 struct commit_queue commits;
678 struct commit_queue_entry *first_displayed_entry = NULL;
679 struct commit_queue_entry *last_displayed_entry = NULL;
680 struct commit_queue_entry *selected_entry = NULL;
681 char *in_repo_path = NULL;
683 err = got_repo_map_path(&in_repo_path, repo, path);
684 if (err != NULL)
685 goto done;
687 if (tog_log_view.window == NULL) {
688 tog_log_view.window = newwin(0, 0, 0, 0);
689 if (tog_log_view.window == NULL)
690 return got_error_from_errno();
691 keypad(tog_log_view.window, TRUE);
693 if (tog_log_view.panel == NULL) {
694 tog_log_view.panel = new_panel(tog_log_view.window);
695 if (tog_log_view.panel == NULL)
696 return got_error_from_errno();
697 } else
698 show_panel(tog_log_view.panel);
700 err = get_head_commit_id(&head_id, repo);
701 if (err)
702 return err;
704 /* The graph contains all commits. */
705 err = got_commit_graph_open(&graph, head_id, 0, repo);
706 if (err)
707 goto done;
708 /* The commit queue contains a subset of commits filtered by path. */
709 TAILQ_INIT(&commits.head);
710 commits.ncommits = 0;
712 /* Populate commit graph with a sufficient number of commits. */
713 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
714 repo);
715 if (err)
716 goto done;
718 /*
719 * Open the initial batch of commits, sorted in commit graph order.
720 * We keep all commits open throughout the lifetime of the log view
721 * in order to avoid having to re-fetch commits from disk while
722 * updating the display.
723 */
724 err = queue_commits(graph, &commits, start_id, LINES, 1, repo,
725 in_repo_path);
726 if (err) {
727 if (err->code != GOT_ERR_ITER_COMPLETED)
728 goto done;
729 err = NULL;
732 first_displayed_entry = TAILQ_FIRST(&commits.head);
733 selected_entry = first_displayed_entry;
734 while (!done) {
735 err = draw_commits(&last_displayed_entry, &selected_entry,
736 first_displayed_entry, &commits, selected, LINES,
737 graph, repo, in_repo_path);
738 if (err)
739 goto done;
741 nodelay(stdscr, FALSE);
742 ch = wgetch(tog_log_view.window);
743 nodelay(stdscr, TRUE);
744 switch (ch) {
745 case ERR:
746 break;
747 case 'q':
748 done = 1;
749 break;
750 case 'k':
751 case KEY_UP:
752 if (selected > 0)
753 selected--;
754 if (selected > 0)
755 break;
756 scroll_up(&first_displayed_entry, 1, &commits);
757 break;
758 case KEY_PPAGE:
759 if (TAILQ_FIRST(&commits.head) ==
760 first_displayed_entry) {
761 selected = 0;
762 break;
764 scroll_up(&first_displayed_entry, LINES,
765 &commits);
766 break;
767 case 'j':
768 case KEY_DOWN:
769 if (selected < MIN(LINES - 2,
770 commits.ncommits - 1)) {
771 selected++;
772 break;
774 err = scroll_down(&first_displayed_entry, 1,
775 last_displayed_entry, &commits, graph,
776 repo, in_repo_path);
777 if (err) {
778 if (err->code != GOT_ERR_ITER_COMPLETED)
779 goto done;
780 err = NULL;
782 break;
783 case KEY_NPAGE: {
784 struct commit_queue_entry *first = first_displayed_entry;
785 err = scroll_down(&first_displayed_entry, LINES,
786 last_displayed_entry, &commits, graph,
787 repo, in_repo_path);
788 if (err) {
789 if (err->code != GOT_ERR_ITER_COMPLETED)
790 goto done;
791 /* can't scroll any further; move cursor down */
792 if (first == first_displayed_entry && selected <
793 MIN(LINES - 2, commits.ncommits - 1)) {
794 selected = MIN(LINES - 2,
795 commits.ncommits - 1);
797 err = NULL;
799 break;
801 case KEY_RESIZE:
802 if (selected > LINES - 2)
803 selected = LINES - 2;
804 if (selected > commits.ncommits - 1)
805 selected = commits.ncommits - 1;
806 break;
807 case KEY_ENTER:
808 case '\r':
809 err = show_commit(selected_entry, repo);
810 if (err)
811 goto done;
812 show_panel(tog_log_view.panel);
813 break;
814 case 't':
815 err = browse_commit(selected_entry, repo);
816 if (err)
817 goto done;
818 show_panel(tog_log_view.panel);
819 break;
820 default:
821 break;
824 done:
825 free(head_id);
826 if (graph)
827 got_commit_graph_close(graph);
828 free_commits(&commits);
829 free(in_repo_path);
830 return err;
833 static const struct got_error *
834 cmd_log(int argc, char *argv[])
836 const struct got_error *error;
837 struct got_repository *repo = NULL;
838 struct got_object_id *start_id = NULL;
839 char *path = NULL, *repo_path = NULL, *cwd = NULL;
840 char *start_commit = NULL;
841 int ch;
843 #ifndef PROFILE
844 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
845 err(1, "pledge");
846 #endif
848 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
849 switch (ch) {
850 case 'c':
851 start_commit = optarg;
852 break;
853 case 'r':
854 repo_path = realpath(optarg, NULL);
855 if (repo_path == NULL)
856 err(1, "-r option");
857 break;
858 default:
859 usage();
860 /* NOTREACHED */
864 argc -= optind;
865 argv += optind;
867 if (argc == 0)
868 path = strdup("");
869 else if (argc == 1)
870 path = strdup(argv[0]);
871 else
872 usage_log();
873 if (path == NULL)
874 return got_error_from_errno();
876 cwd = getcwd(NULL, 0);
877 if (cwd == NULL) {
878 error = got_error_from_errno();
879 goto done;
881 if (repo_path == NULL) {
882 repo_path = strdup(cwd);
883 if (repo_path == NULL) {
884 error = got_error_from_errno();
885 goto done;
889 error = got_repo_open(&repo, repo_path);
890 if (error != NULL)
891 goto done;
893 if (start_commit == NULL) {
894 error = get_head_commit_id(&start_id, repo);
895 if (error != NULL)
896 goto done;
897 } else {
898 struct got_object *obj;
899 error = got_object_open_by_id_str(&obj, repo, start_commit);
900 if (error == NULL) {
901 start_id = got_object_get_id(obj);
902 if (start_id == NULL)
903 error = got_error_from_errno();
904 goto done;
907 if (error != NULL)
908 goto done;
910 error = show_log_view(start_id, repo, path);
911 done:
912 free(repo_path);
913 free(cwd);
914 free(path);
915 free(start_id);
916 if (repo)
917 got_repo_close(repo);
918 return error;
921 __dead static void
922 usage_diff(void)
924 endwin();
925 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
926 getprogname());
927 exit(1);
930 static char *
931 parse_next_line(FILE *f, size_t *len)
933 char *line;
934 size_t linelen;
935 size_t lineno;
936 const char delim[3] = { '\0', '\0', '\0'};
938 line = fparseln(f, &linelen, &lineno, delim, 0);
939 if (len)
940 *len = linelen;
941 return line;
944 static const struct got_error *
945 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
946 int *last_displayed_line, int *eof, int max_lines)
948 const struct got_error *err;
949 int nlines = 0, nprinted = 0;
950 char *line;
951 size_t len;
952 wchar_t *wline;
953 int width;
955 rewind(f);
956 werase(window);
958 *eof = 0;
959 while (nprinted < max_lines) {
960 line = parse_next_line(f, &len);
961 if (line == NULL) {
962 *eof = 1;
963 break;
965 if (++nlines < *first_displayed_line) {
966 free(line);
967 continue;
970 err = format_line(&wline, &width, line, COLS);
971 if (err) {
972 free(line);
973 free(wline);
974 return err;
976 waddwstr(window, wline);
977 if (width < COLS)
978 waddch(window, '\n');
979 if (++nprinted == 1)
980 *first_displayed_line = nlines;
981 free(line);
982 free(wline);
983 wline = NULL;
985 *last_displayed_line = nlines;
987 update_panels();
988 doupdate();
990 return NULL;
993 static const struct got_error *
994 show_diff_view(struct got_object *obj1, struct got_object *obj2,
995 struct got_repository *repo)
997 const struct got_error *err;
998 FILE *f;
999 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1000 int eof, i;
1002 if (obj1 != NULL && obj2 != NULL &&
1003 got_object_get_type(obj1) != got_object_get_type(obj2))
1004 return got_error(GOT_ERR_OBJ_TYPE);
1006 f = got_opentemp();
1007 if (f == NULL)
1008 return got_error_from_errno();
1010 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1011 case GOT_OBJ_TYPE_BLOB:
1012 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1013 break;
1014 case GOT_OBJ_TYPE_TREE:
1015 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1016 break;
1017 case GOT_OBJ_TYPE_COMMIT:
1018 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1019 break;
1020 default:
1021 return got_error(GOT_ERR_OBJ_TYPE);
1024 fflush(f);
1026 if (tog_diff_view.window == NULL) {
1027 tog_diff_view.window = newwin(0, 0, 0, 0);
1028 if (tog_diff_view.window == NULL)
1029 return got_error_from_errno();
1030 keypad(tog_diff_view.window, TRUE);
1032 if (tog_diff_view.panel == NULL) {
1033 tog_diff_view.panel = new_panel(tog_diff_view.window);
1034 if (tog_diff_view.panel == NULL)
1035 return got_error_from_errno();
1036 } else
1037 show_panel(tog_diff_view.panel);
1039 while (!done) {
1040 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
1041 &last_displayed_line, &eof, LINES);
1042 if (err)
1043 break;
1044 nodelay(stdscr, FALSE);
1045 ch = wgetch(tog_diff_view.window);
1046 nodelay(stdscr, TRUE);
1047 switch (ch) {
1048 case 'q':
1049 done = 1;
1050 break;
1051 case 'k':
1052 case KEY_UP:
1053 if (first_displayed_line > 1)
1054 first_displayed_line--;
1055 break;
1056 case KEY_PPAGE:
1057 case KEY_BACKSPACE:
1058 i = 0;
1059 while (i++ < LINES - 1 &&
1060 first_displayed_line > 1)
1061 first_displayed_line--;
1062 break;
1063 case 'j':
1064 case KEY_DOWN:
1065 if (!eof)
1066 first_displayed_line++;
1067 break;
1068 case KEY_NPAGE:
1069 case ' ':
1070 i = 0;
1071 while (!eof && i++ < LINES - 1) {
1072 char *line = parse_next_line(f, NULL);
1073 first_displayed_line++;
1074 if (line == NULL)
1075 break;
1077 break;
1078 default:
1079 break;
1082 fclose(f);
1083 return err;
1086 static const struct got_error *
1087 cmd_diff(int argc, char *argv[])
1089 const struct got_error *error = NULL;
1090 struct got_repository *repo = NULL;
1091 struct got_object *obj1 = NULL, *obj2 = NULL;
1092 char *repo_path = NULL;
1093 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1094 int ch;
1096 #ifndef PROFILE
1097 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1098 err(1, "pledge");
1099 #endif
1101 while ((ch = getopt(argc, argv, "")) != -1) {
1102 switch (ch) {
1103 default:
1104 usage();
1105 /* NOTREACHED */
1109 argc -= optind;
1110 argv += optind;
1112 if (argc == 0) {
1113 usage_diff(); /* TODO show local worktree changes */
1114 } else if (argc == 2) {
1115 repo_path = getcwd(NULL, 0);
1116 if (repo_path == NULL)
1117 return got_error_from_errno();
1118 obj_id_str1 = argv[0];
1119 obj_id_str2 = argv[1];
1120 } else if (argc == 3) {
1121 repo_path = realpath(argv[0], NULL);
1122 if (repo_path == NULL)
1123 return got_error_from_errno();
1124 obj_id_str1 = argv[1];
1125 obj_id_str2 = argv[2];
1126 } else
1127 usage_diff();
1129 error = got_repo_open(&repo, repo_path);
1130 free(repo_path);
1131 if (error)
1132 goto done;
1134 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1135 if (error)
1136 goto done;
1138 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1139 if (error)
1140 goto done;
1142 error = show_diff_view(obj1, obj2, repo);
1143 done:
1144 got_repo_close(repo);
1145 if (obj1)
1146 got_object_close(obj1);
1147 if (obj2)
1148 got_object_close(obj2);
1149 return error;
1152 __dead static void
1153 usage_blame(void)
1155 endwin();
1156 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1157 getprogname());
1158 exit(1);
1161 struct tog_blame_line {
1162 int annotated;
1163 struct got_object_id *id;
1166 static const struct got_error *
1167 draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1168 struct tog_blame_line *lines, int nlines, int blame_complete,
1169 int selected_line, int *first_displayed_line, int *last_displayed_line,
1170 int *eof, int max_lines)
1172 const struct got_error *err;
1173 int lineno = 0, nprinted = 0;
1174 char *line;
1175 size_t len;
1176 wchar_t *wline;
1177 int width, wlimit;
1178 struct tog_blame_line *blame_line;
1179 struct got_object_id *prev_id = NULL;
1180 char *id_str;
1182 err = got_object_id_str(&id_str, id);
1183 if (err)
1184 return err;
1186 rewind(f);
1187 werase(window);
1189 if (asprintf(&line, "commit: %s", id_str) == -1) {
1190 err = got_error_from_errno();
1191 free(id_str);
1192 return err;
1195 err = format_line(&wline, &width, line, COLS);
1196 free(line);
1197 line = NULL;
1198 waddwstr(window, wline);
1199 free(wline);
1200 wline = NULL;
1201 if (width < COLS)
1202 waddch(window, '\n');
1204 if (asprintf(&line, "[%d/%d] %s%s",
1205 *first_displayed_line - 1 + selected_line, nlines,
1206 blame_complete ? "" : "annotating ", path) == -1) {
1207 free(id_str);
1208 return got_error_from_errno();
1210 free(id_str);
1211 err = format_line(&wline, &width, line, COLS);
1212 free(line);
1213 line = NULL;
1214 if (err)
1215 return err;
1216 waddwstr(window, wline);
1217 free(wline);
1218 wline = NULL;
1219 if (width < COLS)
1220 waddch(window, '\n');
1222 *eof = 0;
1223 while (nprinted < max_lines - 2) {
1224 line = parse_next_line(f, &len);
1225 if (line == NULL) {
1226 *eof = 1;
1227 break;
1229 if (++lineno < *first_displayed_line) {
1230 free(line);
1231 continue;
1234 wlimit = COLS < 9 ? 0 : COLS - 9;
1235 err = format_line(&wline, &width, line, wlimit);
1236 if (err) {
1237 free(line);
1238 return err;
1241 if (nprinted == selected_line - 1)
1242 wstandout(window);
1244 blame_line = &lines[lineno - 1];
1245 if (blame_line->annotated && prev_id &&
1246 got_object_id_cmp(prev_id, blame_line->id) == 0)
1247 waddstr(window, " ");
1248 else if (blame_line->annotated) {
1249 char *id_str;
1250 err = got_object_id_str(&id_str, blame_line->id);
1251 if (err) {
1252 free(line);
1253 free(wline);
1254 return err;
1256 wprintw(window, "%.8s ", id_str);
1257 free(id_str);
1258 prev_id = blame_line->id;
1259 } else {
1260 waddstr(window, "........ ");
1261 prev_id = NULL;
1264 waddwstr(window, wline);
1265 while (width < wlimit) {
1266 waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1267 width++;
1269 if (nprinted == selected_line - 1)
1270 wstandend(window);
1271 if (++nprinted == 1)
1272 *first_displayed_line = lineno;
1273 free(line);
1274 free(wline);
1275 wline = NULL;
1277 *last_displayed_line = lineno;
1279 update_panels();
1280 doupdate();
1282 return NULL;
1285 struct tog_blame_cb_args {
1286 pthread_mutex_t *mutex;
1287 struct tog_blame_line *lines; /* one per line */
1288 int nlines;
1290 struct got_object_id *commit_id;
1291 FILE *f;
1292 const char *path;
1293 int *first_displayed_line;
1294 int *last_displayed_line;
1295 int *selected_line;
1296 int *quit;
1299 static const struct got_error *
1300 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1302 const struct got_error *err = NULL;
1303 struct tog_blame_cb_args *a = arg;
1304 struct tog_blame_line *line;
1305 int eof;
1307 if (nlines != a->nlines ||
1308 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1309 return got_error(GOT_ERR_RANGE);
1311 if (pthread_mutex_lock(a->mutex) != 0)
1312 return got_error_from_errno();
1314 if (*a->quit) { /* user has quit the blame view */
1315 err = got_error(GOT_ERR_ITER_COMPLETED);
1316 goto done;
1319 if (lineno == -1)
1320 goto done; /* no change in this commit */
1322 line = &a->lines[lineno - 1];
1323 if (line->annotated)
1324 goto done;
1326 line->id = got_object_id_dup(id);
1327 if (line->id == NULL) {
1328 err = got_error_from_errno();
1329 goto done;
1331 line->annotated = 1;
1333 err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1334 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1335 a->last_displayed_line, &eof, LINES);
1336 done:
1337 if (pthread_mutex_unlock(a->mutex) != 0)
1338 return got_error_from_errno();
1339 return err;
1342 struct tog_blame_thread_args {
1343 const char *path;
1344 struct got_repository *repo;
1345 struct tog_blame_cb_args *cb_args;
1346 int *complete;
1349 static void *
1350 blame_thread(void *arg)
1352 const struct got_error *err;
1353 struct tog_blame_thread_args *ta = arg;
1354 struct tog_blame_cb_args *a = ta->cb_args;
1355 int eof;
1357 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1358 blame_cb, ta->cb_args);
1360 if (pthread_mutex_lock(a->mutex) != 0)
1361 return (void *)got_error_from_errno();
1363 got_repo_close(ta->repo);
1364 ta->repo = NULL;
1365 *ta->complete = 1;
1366 if (!err)
1367 err = draw_blame(tog_blame_view.window, a->commit_id, a->f,
1368 a->path, a->lines, a->nlines, 1, *a->selected_line,
1369 a->first_displayed_line, a->last_displayed_line, &eof,
1370 LINES);
1372 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1373 err = got_error_from_errno();
1375 return (void *)err;
1378 static struct got_object_id *
1379 get_selected_commit_id(struct tog_blame_line *lines,
1380 int first_displayed_line, int selected_line)
1382 struct tog_blame_line *line;
1384 line = &lines[first_displayed_line - 1 + selected_line - 1];
1385 if (!line->annotated)
1386 return NULL;
1388 return line->id;
1391 static const struct got_error *
1392 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1393 struct tog_blame_line *lines, int first_displayed_line,
1394 int selected_line, struct got_repository *repo)
1396 const struct got_error *err = NULL;
1397 struct got_commit_object *commit = NULL;
1398 struct got_object_id *selected_id;
1399 struct got_object_qid *pid;
1401 *pobj = NULL;
1402 *obj = NULL;
1404 selected_id = get_selected_commit_id(lines,
1405 first_displayed_line, selected_line);
1406 if (selected_id == NULL)
1407 return NULL;
1409 err = got_object_open(obj, repo, selected_id);
1410 if (err)
1411 goto done;
1413 err = got_object_commit_open(&commit, repo, *obj);
1414 if (err)
1415 goto done;
1417 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1418 if (pid) {
1419 err = got_object_open(pobj, repo, pid->id);
1420 if (err)
1421 goto done;
1423 done:
1424 if (commit)
1425 got_object_commit_close(commit);
1426 return err;
1429 struct tog_blame {
1430 FILE *f;
1431 size_t filesize;
1432 struct tog_blame_line *lines;
1433 size_t nlines;
1434 pthread_t thread;
1435 struct tog_blame_thread_args thread_args;
1436 struct tog_blame_cb_args cb_args;
1437 const char *path;
1438 struct got_object_id *commit_id;
1441 static const struct got_error *
1442 stop_blame(struct tog_blame *blame)
1444 const struct got_error *err = NULL;
1445 int i;
1447 if (blame->thread) {
1448 if (pthread_join(blame->thread, (void **)&err) != 0)
1449 err = got_error_from_errno();
1450 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1451 err = NULL;
1452 blame->thread = NULL;
1454 if (blame->thread_args.repo) {
1455 got_repo_close(blame->thread_args.repo);
1456 blame->thread_args.repo = NULL;
1458 if (blame->f) {
1459 fclose(blame->f);
1460 blame->f = NULL;
1462 for (i = 0; i < blame->nlines; i++)
1463 free(blame->lines[i].id);
1464 free(blame->lines);
1465 blame->lines = NULL;
1466 free(blame->commit_id);
1467 blame->commit_id = NULL;
1469 return err;
1472 static const struct got_error *
1473 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex, int *blame_complete,
1474 int *first_displayed_line, int *last_displayed_line,
1475 int *selected_line, int *done, const char *path,
1476 struct got_object_id *commit_id,
1477 struct got_repository *repo)
1479 const struct got_error *err = NULL;
1480 struct got_blob_object *blob = NULL;
1481 struct got_repository *thread_repo = NULL;
1482 struct got_object *obj;
1484 err = got_object_open_by_path(&obj, repo, commit_id, path);
1485 if (err)
1486 goto done;
1487 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1488 err = got_error(GOT_ERR_OBJ_TYPE);
1489 goto done;
1492 err = got_object_blob_open(&blob, repo, obj, 8192);
1493 if (err)
1494 goto done;
1495 blame->f = got_opentemp();
1496 if (blame->f == NULL) {
1497 err = got_error_from_errno();
1498 goto done;
1500 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1501 blame->f, blob);
1502 if (err)
1503 goto done;
1505 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1506 if (blame->lines == NULL) {
1507 err = got_error_from_errno();
1508 goto done;
1511 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1512 if (err)
1513 goto done;
1515 blame->cb_args.lines = blame->lines;
1516 blame->cb_args.nlines = blame->nlines;
1517 blame->cb_args.mutex = mutex;
1518 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1519 if (blame->cb_args.commit_id == NULL) {
1520 err = got_error_from_errno();
1521 goto done;
1523 blame->cb_args.f = blame->f;
1524 blame->cb_args.path = path;
1525 blame->cb_args.first_displayed_line = first_displayed_line;
1526 blame->cb_args.selected_line = selected_line;
1527 blame->cb_args.last_displayed_line = last_displayed_line;
1528 blame->cb_args.quit = done;
1530 blame->thread_args.path = path;
1531 blame->thread_args.repo = thread_repo;
1532 blame->thread_args.cb_args = &blame->cb_args;
1533 blame->thread_args.complete = blame_complete;
1534 *blame_complete = 0;
1536 if (pthread_create(&blame->thread, NULL, blame_thread,
1537 &blame->thread_args) != 0) {
1538 err = got_error_from_errno();
1539 goto done;
1542 done:
1543 if (blob)
1544 got_object_blob_close(blob);
1545 if (obj)
1546 got_object_close(obj);
1547 if (err)
1548 stop_blame(blame);
1549 return err;
1552 static const struct got_error *
1553 show_blame_view(const char *path, struct got_object_id *commit_id,
1554 struct got_repository *repo)
1556 const struct got_error *err = NULL, *thread_err = NULL;
1557 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1558 int selected_line = first_displayed_line;
1559 int eof, blame_complete = 0;
1560 struct got_object *obj = NULL, *pobj = NULL;
1561 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1562 struct tog_blame blame;
1563 struct got_object_id_queue blamed_commits;
1564 struct got_object_qid *blamed_commit = NULL;
1566 SIMPLEQ_INIT(&blamed_commits);
1568 if (pthread_mutex_init(&mutex, NULL) != 0) {
1569 err = got_error_from_errno();
1570 goto done;
1573 err = got_object_qid_alloc(&blamed_commit, commit_id);
1574 if (err)
1575 goto done;
1576 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1578 if (tog_blame_view.window == NULL) {
1579 tog_blame_view.window = newwin(0, 0, 0, 0);
1580 if (tog_blame_view.window == NULL)
1581 return got_error_from_errno();
1582 keypad(tog_blame_view.window, TRUE);
1584 if (tog_blame_view.panel == NULL) {
1585 tog_blame_view.panel = new_panel(tog_blame_view.window);
1586 if (tog_blame_view.panel == NULL)
1587 return got_error_from_errno();
1588 } else
1589 show_panel(tog_blame_view.panel);
1591 memset(&blame, 0, sizeof(blame));
1592 err = run_blame(&blame, &mutex, &blame_complete,
1593 &first_displayed_line, &last_displayed_line,
1594 &selected_line, &done, path, blamed_commit->id, repo);
1595 if (err)
1596 return err;
1598 while (!done) {
1599 if (pthread_mutex_lock(&mutex) != 0) {
1600 err = got_error_from_errno();
1601 goto done;
1603 err = draw_blame(tog_blame_view.window, blamed_commit->id,
1604 blame.f, path, blame.lines, blame.nlines, blame_complete,
1605 selected_line, &first_displayed_line, &last_displayed_line,
1606 &eof, LINES);
1607 if (pthread_mutex_unlock(&mutex) != 0) {
1608 err = got_error_from_errno();
1609 goto done;
1611 if (err)
1612 break;
1613 nodelay(stdscr, FALSE);
1614 ch = wgetch(tog_blame_view.window);
1615 nodelay(stdscr, TRUE);
1616 if (pthread_mutex_lock(&mutex) != 0) {
1617 err = got_error_from_errno();
1618 goto done;
1620 switch (ch) {
1621 case 'q':
1622 done = 1;
1623 break;
1624 case 'k':
1625 case KEY_UP:
1626 if (selected_line > 1)
1627 selected_line--;
1628 else if (selected_line == 1 &&
1629 first_displayed_line > 1)
1630 first_displayed_line--;
1631 break;
1632 case KEY_PPAGE:
1633 case KEY_BACKSPACE:
1634 if (first_displayed_line == 1) {
1635 selected_line = 1;
1636 break;
1638 if (first_displayed_line > LINES - 2)
1639 first_displayed_line -= (LINES - 2);
1640 else
1641 first_displayed_line = 1;
1642 break;
1643 case 'j':
1644 case KEY_DOWN:
1645 if (selected_line < LINES - 2 &&
1646 first_displayed_line + selected_line <=
1647 blame.nlines)
1648 selected_line++;
1649 else if (last_displayed_line < blame.nlines)
1650 first_displayed_line++;
1651 break;
1652 case 'b':
1653 case 'p': {
1654 struct got_object_id *id;
1655 id = get_selected_commit_id(blame.lines,
1656 first_displayed_line, selected_line);
1657 if (id == NULL || got_object_id_cmp(id,
1658 blamed_commit->id) == 0)
1659 break;
1660 err = open_selected_commit(&pobj, &obj,
1661 blame.lines, first_displayed_line,
1662 selected_line, repo);
1663 if (err)
1664 break;
1665 if (pobj == NULL && obj == NULL)
1666 break;
1667 if (ch == 'p' && pobj == NULL)
1668 break;
1669 done = 1;
1670 if (pthread_mutex_unlock(&mutex) != 0) {
1671 err = got_error_from_errno();
1672 goto done;
1674 thread_err = stop_blame(&blame);
1675 done = 0;
1676 if (pthread_mutex_lock(&mutex) != 0) {
1677 err = got_error_from_errno();
1678 goto done;
1680 if (thread_err)
1681 break;
1682 id = got_object_get_id(ch == 'b' ? obj : pobj);
1683 got_object_close(obj);
1684 obj = NULL;
1685 if (pobj) {
1686 got_object_close(pobj);
1687 pobj = NULL;
1689 if (id == NULL) {
1690 err = got_error_from_errno();
1691 break;
1693 err = got_object_qid_alloc(&blamed_commit, id);
1694 free(id);
1695 if (err)
1696 goto done;
1697 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1698 blamed_commit, entry);
1699 err = run_blame(&blame, &mutex,
1700 &blame_complete, &first_displayed_line,
1701 &last_displayed_line, &selected_line,
1702 &done, path, blamed_commit->id, repo);
1703 if (err)
1704 break;
1705 break;
1707 case 'B': {
1708 struct got_object_qid *first;
1709 first = SIMPLEQ_FIRST(&blamed_commits);
1710 if (!got_object_id_cmp(first->id, commit_id))
1711 break;
1712 done = 1;
1713 if (pthread_mutex_unlock(&mutex) != 0) {
1714 err = got_error_from_errno();
1715 goto done;
1717 thread_err = stop_blame(&blame);
1718 done = 0;
1719 if (pthread_mutex_lock(&mutex) != 0) {
1720 err = got_error_from_errno();
1721 goto done;
1723 if (thread_err)
1724 break;
1725 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1726 got_object_qid_free(blamed_commit);
1727 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1728 err = run_blame(&blame, &mutex,
1729 &blame_complete, &first_displayed_line,
1730 &last_displayed_line, &selected_line,
1731 &done, path, blamed_commit->id, repo);
1732 if (err)
1733 break;
1734 break;
1736 case KEY_ENTER:
1737 case '\r':
1738 err = open_selected_commit(&pobj, &obj,
1739 blame.lines, first_displayed_line,
1740 selected_line, repo);
1741 if (err)
1742 break;
1743 if (pobj == NULL && obj == NULL)
1744 break;
1745 err = show_diff_view(pobj, obj, repo);
1746 if (pobj) {
1747 got_object_close(pobj);
1748 pobj = NULL;
1750 got_object_close(obj);
1751 obj = NULL;
1752 show_panel(tog_blame_view.panel);
1753 if (err)
1754 break;
1755 break;
1756 case KEY_NPAGE:
1757 case ' ':
1758 if (last_displayed_line >= blame.nlines &&
1759 selected_line < LINES - 2) {
1760 selected_line = MIN(blame.nlines,
1761 LINES - 2);
1762 break;
1764 if (last_displayed_line + LINES - 2 <=
1765 blame.nlines)
1766 first_displayed_line += LINES - 2;
1767 else
1768 first_displayed_line =
1769 blame.nlines - (LINES - 3);
1770 break;
1771 default:
1772 break;
1774 if (pthread_mutex_unlock(&mutex) != 0)
1775 err = got_error_from_errno();
1776 if (err || thread_err)
1777 break;
1779 done:
1780 if (pobj)
1781 got_object_close(pobj);
1782 if (blame.thread)
1783 thread_err = stop_blame(&blame);
1784 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1785 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1786 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1787 got_object_qid_free(blamed_commit);
1789 return thread_err ? thread_err : err;
1792 static const struct got_error *
1793 cmd_blame(int argc, char *argv[])
1795 const struct got_error *error;
1796 struct got_repository *repo = NULL;
1797 char *repo_path = NULL;
1798 char *path = NULL;
1799 struct got_object_id *commit_id = NULL;
1800 char *commit_id_str = NULL;
1801 int ch;
1803 #ifndef PROFILE
1804 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1805 err(1, "pledge");
1806 #endif
1808 while ((ch = getopt(argc, argv, "c:")) != -1) {
1809 switch (ch) {
1810 case 'c':
1811 commit_id_str = optarg;
1812 break;
1813 default:
1814 usage();
1815 /* NOTREACHED */
1819 argc -= optind;
1820 argv += optind;
1822 if (argc == 0) {
1823 usage_blame();
1824 } else if (argc == 1) {
1825 repo_path = getcwd(NULL, 0);
1826 if (repo_path == NULL)
1827 return got_error_from_errno();
1828 path = argv[0];
1829 } else if (argc == 2) {
1830 repo_path = realpath(argv[0], NULL);
1831 if (repo_path == NULL)
1832 return got_error_from_errno();
1833 path = argv[1];
1834 } else
1835 usage_blame();
1837 error = got_repo_open(&repo, repo_path);
1838 free(repo_path);
1839 if (error != NULL)
1840 return error;
1842 if (commit_id_str == NULL) {
1843 struct got_reference *head_ref;
1844 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1845 if (error != NULL)
1846 goto done;
1847 error = got_ref_resolve(&commit_id, repo, head_ref);
1848 got_ref_close(head_ref);
1849 } else {
1850 struct got_object *obj;
1851 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1852 if (error != NULL)
1853 goto done;
1854 commit_id = got_object_get_id(obj);
1855 if (commit_id == NULL)
1856 error = got_error_from_errno();
1857 got_object_close(obj);
1859 if (error != NULL)
1860 goto done;
1862 error = show_blame_view(path, commit_id, repo);
1863 done:
1864 free(commit_id);
1865 if (repo)
1866 got_repo_close(repo);
1867 return error;
1870 static const struct got_error *
1871 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1872 struct got_tree_entry **last_displayed_entry,
1873 struct got_tree_entry **selected_entry, int *ndisplayed,
1874 WINDOW *window, const char *label, int show_ids, const char *parent_path,
1875 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1877 const struct got_error *err = NULL;
1878 struct got_tree_entry *te;
1879 wchar_t *wline;
1880 int width, n;
1882 *ndisplayed = 0;
1884 werase(window);
1886 if (limit == 0)
1887 return NULL;
1889 err = format_line(&wline, &width, label, COLS);
1890 if (err)
1891 return err;
1892 waddwstr(window, wline);
1893 free(wline);
1894 wline = NULL;
1895 if (width < COLS)
1896 waddch(window, '\n');
1897 if (--limit <= 0)
1898 return NULL;
1899 err = format_line(&wline, &width, parent_path, COLS);
1900 if (err)
1901 return err;
1902 waddwstr(window, wline);
1903 free(wline);
1904 wline = NULL;
1905 if (width < COLS)
1906 waddch(window, '\n');
1907 if (--limit <= 0)
1908 return NULL;
1909 waddch(window, '\n');
1910 if (--limit <= 0)
1911 return NULL;
1913 te = SIMPLEQ_FIRST(&entries->head);
1914 if (*first_displayed_entry == NULL) {
1915 if (selected == 0) {
1916 wstandout(window);
1917 *selected_entry = NULL;
1919 waddstr(window, " ..\n"); /* parent directory */
1920 if (selected == 0)
1921 wstandend(window);
1922 (*ndisplayed)++;
1923 if (--limit <= 0)
1924 return NULL;
1925 n = 1;
1926 } else {
1927 n = 0;
1928 while (te != *first_displayed_entry)
1929 te = SIMPLEQ_NEXT(te, entry);
1932 while (te) {
1933 char *line = NULL, *id_str = NULL;
1935 if (show_ids) {
1936 err = got_object_id_str(&id_str, te->id);
1937 if (err)
1938 return got_error_from_errno();
1940 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
1941 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
1942 free(id_str);
1943 return got_error_from_errno();
1945 free(id_str);
1946 err = format_line(&wline, &width, line, COLS);
1947 if (err) {
1948 free(line);
1949 break;
1951 if (n == selected) {
1952 wstandout(window);
1953 *selected_entry = te;
1955 waddwstr(window, wline);
1956 if (width < COLS)
1957 waddch(window, '\n');
1958 if (n == selected)
1959 wstandend(window);
1960 free(line);
1961 free(wline);
1962 wline = NULL;
1963 n++;
1964 (*ndisplayed)++;
1965 *last_displayed_entry = te;
1966 if (--limit <= 0)
1967 break;
1968 te = SIMPLEQ_NEXT(te, entry);
1971 return err;
1974 static void
1975 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1976 const struct got_tree_entries *entries, int isroot)
1978 struct got_tree_entry *te, *prev;
1979 int i;
1981 if (*first_displayed_entry == NULL)
1982 return;
1984 te = SIMPLEQ_FIRST(&entries->head);
1985 if (*first_displayed_entry == te) {
1986 if (!isroot)
1987 *first_displayed_entry = NULL;
1988 return;
1991 /* XXX this is stupid... switch to TAILQ? */
1992 for (i = 0; i < maxscroll; i++) {
1993 while (te != *first_displayed_entry) {
1994 prev = te;
1995 te = SIMPLEQ_NEXT(te, entry);
1997 *first_displayed_entry = prev;
1998 te = SIMPLEQ_FIRST(&entries->head);
2000 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2001 *first_displayed_entry = NULL;
2004 static void
2005 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2006 struct got_tree_entry *last_displayed_entry,
2007 const struct got_tree_entries *entries)
2009 struct got_tree_entry *next;
2010 int n = 0;
2012 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2013 return;
2015 if (*first_displayed_entry)
2016 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2017 else
2018 next = SIMPLEQ_FIRST(&entries->head);
2019 while (next) {
2020 *first_displayed_entry = next;
2021 if (++n >= maxscroll)
2022 break;
2023 next = SIMPLEQ_NEXT(next, entry);
2027 struct tog_parent_tree {
2028 TAILQ_ENTRY(tog_parent_tree) entry;
2029 struct got_tree_object *tree;
2030 struct got_tree_entry *first_displayed_entry;
2031 struct got_tree_entry *selected_entry;
2032 int selected;
2035 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2037 static const struct got_error *
2038 tree_entry_path(char **path, struct tog_parent_trees *parents,
2039 struct got_tree_entry *te)
2041 const struct got_error *err = NULL;
2042 struct tog_parent_tree *pt;
2043 size_t len = 2; /* for leading slash and NUL */
2045 TAILQ_FOREACH(pt, parents, entry)
2046 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2047 if (te)
2048 len += strlen(te->name);
2050 *path = calloc(1, len);
2051 if (path == NULL)
2052 return got_error_from_errno();
2054 (*path)[0] = '/';
2055 pt = TAILQ_LAST(parents, tog_parent_trees);
2056 while (pt) {
2057 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2058 err = got_error(GOT_ERR_NO_SPACE);
2059 goto done;
2061 if (strlcat(*path, "/", len) >= len) {
2062 err = got_error(GOT_ERR_NO_SPACE);
2063 goto done;
2065 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2067 if (te) {
2068 if (strlcat(*path, te->name, len) >= len) {
2069 err = got_error(GOT_ERR_NO_SPACE);
2070 goto done;
2073 done:
2074 if (err) {
2075 free(*path);
2076 *path = NULL;
2078 return err;
2081 static const struct got_error *
2082 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2083 struct got_object_id *commit_id, struct got_repository *repo)
2085 const struct got_error *err = NULL;
2086 char *path;
2088 err = tree_entry_path(&path, parents, te);
2089 if (err)
2090 return err;
2092 err = show_blame_view(path, commit_id, repo);
2093 free(path);
2094 return err;
2097 static const struct got_error *
2098 log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2099 struct got_object_id *commit_id, struct got_repository *repo)
2101 const struct got_error *err = NULL;
2102 char *path;
2104 err = tree_entry_path(&path, parents, te);
2105 if (err)
2106 return err;
2108 err = show_log_view(commit_id, repo, path);
2109 free(path);
2110 return err;
2113 static const struct got_error *
2114 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2115 struct got_repository *repo)
2117 const struct got_error *err = NULL;
2118 int ch, done = 0, selected = 0, show_ids = 0;
2119 struct got_tree_object *tree = root;
2120 const struct got_tree_entries *entries;
2121 struct got_tree_entry *first_displayed_entry = NULL;
2122 struct got_tree_entry *last_displayed_entry = NULL;
2123 struct got_tree_entry *selected_entry = NULL;
2124 char *commit_id_str = NULL, *tree_label = NULL;
2125 int nentries, ndisplayed;
2126 struct tog_parent_trees parents;
2128 TAILQ_INIT(&parents);
2130 err = got_object_id_str(&commit_id_str, commit_id);
2131 if (err != NULL)
2132 goto done;
2134 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2135 err = got_error_from_errno();
2136 goto done;
2139 if (tog_tree_view.window == NULL) {
2140 tog_tree_view.window = newwin(0, 0, 0, 0);
2141 if (tog_tree_view.window == NULL)
2142 return got_error_from_errno();
2143 keypad(tog_tree_view.window, TRUE);
2145 if (tog_tree_view.panel == NULL) {
2146 tog_tree_view.panel = new_panel(tog_tree_view.window);
2147 if (tog_tree_view.panel == NULL)
2148 return got_error_from_errno();
2149 } else
2150 show_panel(tog_tree_view.panel);
2152 entries = got_object_tree_get_entries(root);
2153 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2154 while (!done) {
2155 char *parent_path;
2156 entries = got_object_tree_get_entries(tree);
2157 nentries = entries->nentries;
2158 if (tree != root)
2159 nentries++; /* '..' directory */
2161 err = tree_entry_path(&parent_path, &parents, NULL);
2162 if (err)
2163 goto done;
2165 err = draw_tree_entries(&first_displayed_entry,
2166 &last_displayed_entry, &selected_entry, &ndisplayed,
2167 tog_tree_view.window, tree_label, show_ids,
2168 parent_path, entries, selected, LINES, tree == root);
2169 free(parent_path);
2170 if (err)
2171 break;
2173 nodelay(stdscr, FALSE);
2174 ch = wgetch(tog_tree_view.window);
2175 nodelay(stdscr, TRUE);
2176 switch (ch) {
2177 case 'q':
2178 done = 1;
2179 break;
2180 case 'i':
2181 show_ids = !show_ids;
2182 break;
2183 case 'l':
2184 if (selected_entry) {
2185 err = log_tree_entry(selected_entry,
2186 &parents, commit_id, repo);
2187 if (err)
2188 goto done;
2190 break;
2191 case 'k':
2192 case KEY_UP:
2193 if (selected > 0)
2194 selected--;
2195 if (selected > 0)
2196 break;
2197 tree_scroll_up(&first_displayed_entry, 1,
2198 entries, tree == root);
2199 break;
2200 case KEY_PPAGE:
2201 if (SIMPLEQ_FIRST(&entries->head) ==
2202 first_displayed_entry) {
2203 if (tree != root)
2204 first_displayed_entry = NULL;
2205 selected = 0;
2206 break;
2208 tree_scroll_up(&first_displayed_entry, LINES,
2209 entries, tree == root);
2210 break;
2211 case 'j':
2212 case KEY_DOWN:
2213 if (selected < ndisplayed - 1) {
2214 selected++;
2215 break;
2217 tree_scroll_down(&first_displayed_entry, 1,
2218 last_displayed_entry, entries);
2219 break;
2220 case KEY_NPAGE:
2221 tree_scroll_down(&first_displayed_entry, LINES,
2222 last_displayed_entry, entries);
2223 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2224 break;
2225 /* can't scroll any further; move cursor down */
2226 if (selected < ndisplayed - 1)
2227 selected = ndisplayed - 1;
2228 break;
2229 case KEY_ENTER:
2230 case '\r':
2231 if (selected_entry == NULL) {
2232 struct tog_parent_tree *parent;
2233 case KEY_BACKSPACE:
2234 /* user selected '..' */
2235 if (tree == root)
2236 break;
2237 parent = TAILQ_FIRST(&parents);
2238 TAILQ_REMOVE(&parents, parent, entry);
2239 got_object_tree_close(tree);
2240 tree = parent->tree;
2241 first_displayed_entry =
2242 parent->first_displayed_entry;
2243 selected_entry = parent->selected_entry;
2244 selected = parent->selected;
2245 free(parent);
2246 } else if (S_ISDIR(selected_entry->mode)) {
2247 struct tog_parent_tree *parent;
2248 struct got_tree_object *child;
2249 err = got_object_open_as_tree(
2250 &child, repo, selected_entry->id);
2251 if (err)
2252 goto done;
2253 parent = calloc(1, sizeof(*parent));
2254 if (parent == NULL) {
2255 err = got_error_from_errno();
2256 goto done;
2258 parent->tree = tree;
2259 parent->first_displayed_entry =
2260 first_displayed_entry;
2261 parent->selected_entry = selected_entry;
2262 parent->selected = selected;
2263 TAILQ_INSERT_HEAD(&parents, parent,
2264 entry);
2265 tree = child;
2266 selected = 0;
2267 first_displayed_entry = NULL;
2268 } else if (S_ISREG(selected_entry->mode)) {
2269 err = blame_tree_entry(selected_entry,
2270 &parents, commit_id, repo);
2271 if (err)
2272 goto done;
2274 break;
2275 case KEY_RESIZE:
2276 if (selected > LINES)
2277 selected = ndisplayed - 1;
2278 break;
2279 default:
2280 break;
2283 done:
2284 free(tree_label);
2285 free(commit_id_str);
2286 while (!TAILQ_EMPTY(&parents)) {
2287 struct tog_parent_tree *parent;
2288 parent = TAILQ_FIRST(&parents);
2289 TAILQ_REMOVE(&parents, parent, entry);
2290 free(parent);
2293 if (tree != root)
2294 got_object_tree_close(tree);
2295 return err;
2298 __dead static void
2299 usage_tree(void)
2301 endwin();
2302 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2303 getprogname());
2304 exit(1);
2307 static const struct got_error *
2308 cmd_tree(int argc, char *argv[])
2310 const struct got_error *error;
2311 struct got_repository *repo = NULL;
2312 char *repo_path = NULL;
2313 struct got_object_id *commit_id = NULL;
2314 char *commit_id_arg = NULL;
2315 struct got_commit_object *commit = NULL;
2316 struct got_tree_object *tree = NULL;
2317 int ch;
2319 #ifndef PROFILE
2320 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2321 err(1, "pledge");
2322 #endif
2324 while ((ch = getopt(argc, argv, "c:")) != -1) {
2325 switch (ch) {
2326 case 'c':
2327 commit_id_arg = optarg;
2328 break;
2329 default:
2330 usage();
2331 /* NOTREACHED */
2335 argc -= optind;
2336 argv += optind;
2338 if (argc == 0) {
2339 repo_path = getcwd(NULL, 0);
2340 if (repo_path == NULL)
2341 return got_error_from_errno();
2342 } else if (argc == 1) {
2343 repo_path = realpath(argv[0], NULL);
2344 if (repo_path == NULL)
2345 return got_error_from_errno();
2346 } else
2347 usage_log();
2349 error = got_repo_open(&repo, repo_path);
2350 free(repo_path);
2351 if (error != NULL)
2352 return error;
2354 if (commit_id_arg == NULL) {
2355 error = get_head_commit_id(&commit_id, repo);
2356 if (error != NULL)
2357 goto done;
2358 } else {
2359 struct got_object *obj;
2360 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2361 if (error == NULL) {
2362 commit_id = got_object_get_id(obj);
2363 if (commit_id == NULL)
2364 error = got_error_from_errno();
2367 if (error != NULL)
2368 goto done;
2370 error = got_object_open_as_commit(&commit, repo, commit_id);
2371 if (error != NULL)
2372 goto done;
2374 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2375 if (error != NULL)
2376 goto done;
2378 error = show_tree_view(tree, commit_id, repo);
2379 done:
2380 free(commit_id);
2381 if (commit)
2382 got_object_commit_close(commit);
2383 if (tree)
2384 got_object_tree_close(tree);
2385 if (repo)
2386 got_repo_close(repo);
2387 return error;
2389 static void
2390 init_curses(void)
2392 initscr();
2393 cbreak();
2394 noecho();
2395 nonl();
2396 intrflush(stdscr, FALSE);
2397 keypad(stdscr, TRUE);
2398 curs_set(0);
2401 __dead static void
2402 usage(void)
2404 int i;
2406 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2407 "Available commands:\n", getprogname());
2408 for (i = 0; i < nitems(tog_commands); i++) {
2409 struct tog_cmd *cmd = &tog_commands[i];
2410 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2412 exit(1);
2415 static char **
2416 make_argv(const char *arg0, const char *arg1)
2418 char **argv;
2419 int argc = (arg1 == NULL ? 1 : 2);
2421 argv = calloc(argc, sizeof(char *));
2422 if (argv == NULL)
2423 err(1, "calloc");
2424 argv[0] = strdup(arg0);
2425 if (argv[0] == NULL)
2426 err(1, "calloc");
2427 if (arg1) {
2428 argv[1] = strdup(arg1);
2429 if (argv[1] == NULL)
2430 err(1, "calloc");
2433 return argv;
2436 int
2437 main(int argc, char *argv[])
2439 const struct got_error *error = NULL;
2440 struct tog_cmd *cmd = NULL;
2441 int ch, hflag = 0;
2442 char **cmd_argv = NULL;
2444 setlocale(LC_ALL, "");
2446 while ((ch = getopt(argc, argv, "h")) != -1) {
2447 switch (ch) {
2448 case 'h':
2449 hflag = 1;
2450 break;
2451 default:
2452 usage();
2453 /* NOTREACHED */
2457 argc -= optind;
2458 argv += optind;
2459 optind = 0;
2460 optreset = 1;
2462 if (argc == 0) {
2463 if (hflag)
2464 usage();
2465 /* Build an argument vector which runs a default command. */
2466 cmd = &tog_commands[0];
2467 cmd_argv = make_argv(cmd->name, NULL);
2468 argc = 1;
2469 } else {
2470 int i;
2472 /* Did the user specific a command? */
2473 for (i = 0; i < nitems(tog_commands); i++) {
2474 if (strncmp(tog_commands[i].name, argv[0],
2475 strlen(argv[0])) == 0) {
2476 cmd = &tog_commands[i];
2477 if (hflag)
2478 tog_commands[i].cmd_usage();
2479 break;
2482 if (cmd == NULL) {
2483 /* Did the user specify a repository? */
2484 char *repo_path = realpath(argv[0], NULL);
2485 if (repo_path) {
2486 struct got_repository *repo;
2487 error = got_repo_open(&repo, repo_path);
2488 if (error == NULL)
2489 got_repo_close(repo);
2490 } else
2491 error = got_error_from_errno();
2492 if (error) {
2493 if (hflag) {
2494 fprintf(stderr, "%s: '%s' is not a "
2495 "known command\n", getprogname(),
2496 argv[0]);
2497 usage();
2499 fprintf(stderr, "%s: '%s' is neither a known "
2500 "command nor a path to a repository\n",
2501 getprogname(), argv[0]);
2502 free(repo_path);
2503 return 1;
2505 cmd = &tog_commands[0];
2506 cmd_argv = make_argv(cmd->name, repo_path);
2507 argc = 2;
2508 free(repo_path);
2512 init_curses();
2514 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2515 if (error)
2516 goto done;
2517 done:
2518 endwin();
2519 free(cmd_argv);
2520 if (error)
2521 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2522 return 0;