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_tree_view;
90 static const struct got_error *
91 show_diff_view(struct tog_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 static void
102 close_view(struct tog_view *view)
104 if (view->panel)
105 del_panel(view->panel);
106 if (view->window)
107 delwin(view->window);
108 free(view);
111 static struct tog_view *
112 open_view(void)
114 struct tog_view *view = malloc(sizeof(*view));
116 if (view == NULL)
117 return NULL;
119 view->window = newwin(0, 0, 0, 0);
120 if (view->window == NULL) {
121 close_view(view);
122 return NULL;
124 view->panel = new_panel(view->window);
125 if (view->panel == NULL) {
126 close_view(view);
127 return NULL;
130 keypad(view->window, TRUE);
131 return view;
134 __dead static void
135 usage_log(void)
137 endwin();
138 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
139 getprogname());
140 exit(1);
143 /* Create newly allocated wide-character string equivalent to a byte string. */
144 static const struct got_error *
145 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
147 char *vis = NULL;
148 const struct got_error *err = NULL;
150 *ws = NULL;
151 *wlen = mbstowcs(NULL, s, 0);
152 if (*wlen == (size_t)-1) {
153 int vislen;
154 if (errno != EILSEQ)
155 return got_error_from_errno();
157 /* byte string invalid in current encoding; try to "fix" it */
158 err = got_mbsavis(&vis, &vislen, s);
159 if (err)
160 return err;
161 *wlen = mbstowcs(NULL, vis, 0);
162 if (*wlen == (size_t)-1) {
163 err = got_error_from_errno(); /* give up */
164 goto done;
168 *ws = calloc(*wlen + 1, sizeof(*ws));
169 if (*ws == NULL) {
170 err = got_error_from_errno();
171 goto done;
174 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
175 err = got_error_from_errno();
176 done:
177 free(vis);
178 if (err) {
179 free(*ws);
180 *ws = NULL;
181 *wlen = 0;
183 return err;
186 /* Format a line for display, ensuring that it won't overflow a width limit. */
187 static const struct got_error *
188 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
190 const struct got_error *err = NULL;
191 int cols = 0;
192 wchar_t *wline = NULL;
193 size_t wlen;
194 int i;
196 *wlinep = NULL;
197 *widthp = 0;
199 err = mbs2ws(&wline, &wlen, line);
200 if (err)
201 return err;
203 i = 0;
204 while (i < wlen && cols < wlimit) {
205 int width = wcwidth(wline[i]);
206 switch (width) {
207 case 0:
208 i++;
209 break;
210 case 1:
211 case 2:
212 if (cols + width <= wlimit) {
213 cols += width;
214 i++;
216 break;
217 case -1:
218 if (wline[i] == L'\t')
219 cols += TABSIZE - ((cols + 1) % TABSIZE);
220 i++;
221 break;
222 default:
223 err = got_error_from_errno();
224 goto done;
227 wline[i] = L'\0';
228 if (widthp)
229 *widthp = cols;
230 done:
231 if (err)
232 free(wline);
233 else
234 *wlinep = wline;
235 return err;
238 static const struct got_error *
239 draw_commit(struct tog_view *view, struct got_commit_object *commit,
240 struct got_object_id *id)
242 const struct got_error *err = NULL;
243 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
244 char *logmsg0 = NULL, *logmsg = NULL;
245 char *author0 = NULL, *author = NULL;
246 wchar_t *wlogmsg = NULL, *wauthor = NULL;
247 int author_width, logmsg_width;
248 char *newline, *smallerthan;
249 char *line = NULL;
250 int col, limit;
251 static const size_t date_display_cols = 9;
252 static const size_t author_display_cols = 16;
253 const int avail = COLS;
255 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
256 >= sizeof(datebuf))
257 return got_error(GOT_ERR_NO_SPACE);
259 if (avail < date_display_cols)
260 limit = MIN(sizeof(datebuf) - 1, avail);
261 else
262 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
263 waddnstr(view->window, datebuf, limit);
264 col = limit + 1;
265 if (col > avail)
266 goto done;
268 author0 = strdup(commit->author);
269 if (author0 == NULL) {
270 err = got_error_from_errno();
271 goto done;
273 author = author0;
274 smallerthan = strchr(author, '<');
275 if (smallerthan)
276 *smallerthan = '\0';
277 else {
278 char *at = strchr(author, '@');
279 if (at)
280 *at = '\0';
282 limit = avail - col;
283 err = format_line(&wauthor, &author_width, author, limit);
284 if (err)
285 goto done;
286 waddwstr(view->window, wauthor);
287 col += author_width;
288 while (col <= avail && author_width < author_display_cols + 1) {
289 waddch(view->window, ' ');
290 col++;
291 author_width++;
293 if (col > avail)
294 goto done;
296 logmsg0 = strdup(commit->logmsg);
297 if (logmsg0 == NULL) {
298 err = got_error_from_errno();
299 goto done;
301 logmsg = logmsg0;
302 while (*logmsg == '\n')
303 logmsg++;
304 newline = strchr(logmsg, '\n');
305 if (newline)
306 *newline = '\0';
307 limit = avail - col;
308 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
309 if (err)
310 goto done;
311 waddwstr(view->window, wlogmsg);
312 col += logmsg_width;
313 while (col <= avail) {
314 waddch(view->window, ' ');
315 col++;
317 done:
318 free(logmsg0);
319 free(wlogmsg);
320 free(author0);
321 free(wauthor);
322 free(line);
323 return err;
326 struct commit_queue_entry {
327 TAILQ_ENTRY(commit_queue_entry) entry;
328 struct got_object_id *id;
329 struct got_commit_object *commit;
330 };
331 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
332 struct commit_queue {
333 int ncommits;
334 struct commit_queue_head head;
335 };
337 static struct commit_queue_entry *
338 alloc_commit_queue_entry(struct got_commit_object *commit,
339 struct got_object_id *id)
341 struct commit_queue_entry *entry;
343 entry = calloc(1, sizeof(*entry));
344 if (entry == NULL)
345 return NULL;
347 entry->id = id;
348 entry->commit = commit;
349 return entry;
352 static void
353 pop_commit(struct commit_queue *commits)
355 struct commit_queue_entry *entry;
357 entry = TAILQ_FIRST(&commits->head);
358 TAILQ_REMOVE(&commits->head, entry, entry);
359 got_object_commit_close(entry->commit);
360 commits->ncommits--;
361 /* Don't free entry->id! It is owned by the commit graph. */
362 free(entry);
365 static void
366 free_commits(struct commit_queue *commits)
368 while (!TAILQ_EMPTY(&commits->head))
369 pop_commit(commits);
372 static const struct got_error *
373 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
374 struct got_object_id *start_id, int minqueue, int init,
375 struct got_repository *repo, const char *path)
377 const struct got_error *err = NULL;
378 struct got_object_id *id;
379 struct commit_queue_entry *entry;
380 int nfetched, nqueued = 0, found_obj = 0;
381 int is_root_path = strcmp(path, "/") == 0;
383 err = got_commit_graph_iter_start(graph, start_id);
384 if (err)
385 return err;
387 entry = TAILQ_LAST(&commits->head, commit_queue_head);
388 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
389 int nfetched;
391 /* Start ID's commit is already on the queue; skip over it. */
392 err = got_commit_graph_iter_next(&id, graph);
393 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
394 return err;
396 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
397 if (err)
398 return err;
401 while (1) {
402 struct got_commit_object *commit;
404 err = got_commit_graph_iter_next(&id, graph);
405 if (err) {
406 if (err->code != GOT_ERR_ITER_NEED_MORE)
407 break;
408 if (nqueued >= minqueue) {
409 err = NULL;
410 break;
412 err = got_commit_graph_fetch_commits(&nfetched,
413 graph, 1, repo);
414 if (err)
415 return err;
416 continue;
418 if (id == NULL)
419 break;
421 err = got_object_open_as_commit(&commit, repo, id);
422 if (err)
423 break;
425 if (!is_root_path) {
426 struct got_object *obj;
427 struct got_object_qid *pid;
428 int changed = 0;
430 err = got_object_open_by_path(&obj, repo, id, path);
431 if (err) {
432 got_object_commit_close(commit);
433 if (err->code == GOT_ERR_NO_OBJ &&
434 (found_obj || !init)) {
435 /* History stops here. */
436 err = got_error(GOT_ERR_ITER_COMPLETED);
438 break;
440 found_obj = 1;
442 pid = SIMPLEQ_FIRST(&commit->parent_ids);
443 if (pid != NULL) {
444 struct got_object *pobj;
445 err = got_object_open_by_path(&pobj, repo,
446 pid->id, path);
447 if (err) {
448 if (err->code != GOT_ERR_NO_OBJ) {
449 got_object_close(obj);
450 got_object_commit_close(commit);
451 break;
453 err = NULL;
454 changed = 1;
455 } else {
456 struct got_object_id *id, *pid;
457 id = got_object_get_id(obj);
458 if (id == NULL) {
459 err = got_error_from_errno();
460 got_object_close(obj);
461 got_object_close(pobj);
462 break;
464 pid = got_object_get_id(pobj);
465 if (pid == NULL) {
466 err = got_error_from_errno();
467 free(id);
468 got_object_close(obj);
469 got_object_close(pobj);
470 break;
472 changed =
473 (got_object_id_cmp(id, pid) != 0);
474 got_object_close(pobj);
475 free(id);
476 free(pid);
479 got_object_close(obj);
480 if (!changed) {
481 got_object_commit_close(commit);
482 continue;
486 entry = alloc_commit_queue_entry(commit, id);
487 if (entry == NULL) {
488 err = got_error_from_errno();
489 break;
491 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
492 nqueued++;
493 commits->ncommits++;
496 return err;
499 static const struct got_error *
500 fetch_next_commit(struct commit_queue_entry **pentry,
501 struct commit_queue_entry *entry, struct commit_queue *commits,
502 struct got_commit_graph *graph, struct got_repository *repo,
503 const char *path)
505 const struct got_error *err = NULL;
507 *pentry = NULL;
509 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
510 if (err)
511 return err;
513 /* Next entry to display should now be available. */
514 *pentry = TAILQ_NEXT(entry, entry);
515 if (*pentry == NULL)
516 return got_error(GOT_ERR_NO_OBJ);
518 return NULL;
521 static const struct got_error *
522 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
524 const struct got_error *err = NULL;
525 struct got_reference *head_ref;
527 *head_id = NULL;
529 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
530 if (err)
531 return err;
533 err = got_ref_resolve(head_id, repo, head_ref);
534 got_ref_close(head_ref);
535 if (err) {
536 *head_id = NULL;
537 return err;
540 return NULL;
543 static const struct got_error *
544 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
545 struct commit_queue_entry **selected, struct commit_queue_entry *first,
546 struct commit_queue *commits, int selected_idx, int limit,
547 struct got_commit_graph *graph, struct got_repository *repo,
548 const char *path)
550 const struct got_error *err = NULL;
551 struct commit_queue_entry *entry;
552 int ncommits, width;
553 char *id_str, *header;
554 wchar_t *wline;
556 entry = first;
557 ncommits = 0;
558 while (entry) {
559 if (ncommits == selected_idx) {
560 *selected = entry;
561 break;
563 entry = TAILQ_NEXT(entry, entry);
564 ncommits++;
567 err = got_object_id_str(&id_str, (*selected)->id);
568 if (err)
569 return err;
571 if (path) {
572 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
573 err = got_error_from_errno();
574 free(id_str);
575 return err;
577 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
578 err = got_error_from_errno();
579 free(id_str);
580 return err;
582 free(id_str);
583 err = format_line(&wline, &width, header, COLS);
584 if (err) {
585 free(header);
586 return err;
588 free(header);
590 werase(view->window);
592 waddwstr(view->window, wline);
593 if (width < COLS)
594 waddch(view->window, '\n');
595 free(wline);
596 if (limit <= 1)
597 return NULL;
599 entry = first;
600 *last = first;
601 ncommits = 0;
602 while (entry) {
603 if (ncommits >= limit - 1)
604 break;
605 if (ncommits == selected_idx)
606 wstandout(view->window);
607 err = draw_commit(view, entry->commit, entry->id);
608 if (ncommits == selected_idx)
609 wstandend(view->window);
610 if (err)
611 break;
612 ncommits++;
613 *last = entry;
614 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
615 err = queue_commits(graph, commits, entry->id, 1,
616 0, repo, path);
617 if (err) {
618 if (err->code != GOT_ERR_ITER_COMPLETED)
619 return err;
620 err = NULL;
623 entry = TAILQ_NEXT(entry, entry);
626 update_panels();
627 doupdate();
629 return err;
632 static void
633 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
634 struct commit_queue *commits)
636 struct commit_queue_entry *entry;
637 int nscrolled = 0;
639 entry = TAILQ_FIRST(&commits->head);
640 if (*first_displayed_entry == entry)
641 return;
643 entry = *first_displayed_entry;
644 while (entry && nscrolled < maxscroll) {
645 entry = TAILQ_PREV(entry, commit_queue_head, entry);
646 if (entry) {
647 *first_displayed_entry = entry;
648 nscrolled++;
653 static const struct got_error *
654 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
655 struct commit_queue_entry *last_displayed_entry,
656 struct commit_queue *commits, struct got_commit_graph *graph,
657 struct got_repository *repo, const char *path)
659 const struct got_error *err = NULL;
660 struct commit_queue_entry *pentry;
661 int nscrolled = 0;
663 do {
664 pentry = TAILQ_NEXT(last_displayed_entry, entry);
665 if (pentry == NULL) {
666 err = fetch_next_commit(&pentry, last_displayed_entry,
667 commits, graph, repo, path);
668 if (err || pentry == NULL)
669 break;
671 last_displayed_entry = pentry;
673 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
674 if (pentry == NULL)
675 break;
676 *first_displayed_entry = pentry;
677 } while (++nscrolled < maxscroll);
679 return err;
682 static const struct got_error *
683 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
685 const struct got_error *err;
686 struct got_object *obj1 = NULL, *obj2 = NULL;
687 struct got_object_qid *parent_id;
688 struct tog_view *view;
690 err = got_object_open(&obj2, repo, entry->id);
691 if (err)
692 return err;
694 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
695 if (parent_id) {
696 err = got_object_open(&obj1, repo, parent_id->id);
697 if (err)
698 goto done;
701 view = open_view();
702 if (view == NULL) {
703 err = got_error_from_errno();
704 goto done;
707 err = show_diff_view(view, obj1, obj2, repo);
708 close_view(view);
709 done:
710 if (obj1)
711 got_object_close(obj1);
712 if (obj2)
713 got_object_close(obj2);
714 return err;
717 static const struct got_error *
718 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
720 const struct got_error *err = NULL;
721 struct got_tree_object *tree;
723 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
724 if (err)
725 return err;
727 err = show_tree_view(tree, entry->id, repo);
728 got_object_tree_close(tree);
729 return err;
732 static const struct got_error *
733 show_log_view(struct got_object_id *start_id, struct got_repository *repo,
734 const char *path)
736 const struct got_error *err = NULL;
737 struct got_object_id *head_id = NULL;
738 int ch, done = 0, selected = 0, nfetched;
739 struct got_commit_graph *graph = NULL;
740 struct commit_queue commits;
741 struct commit_queue_entry *first_displayed_entry = NULL;
742 struct commit_queue_entry *last_displayed_entry = NULL;
743 struct commit_queue_entry *selected_entry = NULL;
744 char *in_repo_path = NULL;
745 struct tog_view *view = NULL;
747 err = got_repo_map_path(&in_repo_path, repo, path);
748 if (err != NULL)
749 goto done;
751 err = get_head_commit_id(&head_id, repo);
752 if (err)
753 return err;
755 /* The graph contains all commits. */
756 err = got_commit_graph_open(&graph, head_id, 0, repo);
757 if (err)
758 goto done;
759 /* The commit queue contains a subset of commits filtered by path. */
760 TAILQ_INIT(&commits.head);
761 commits.ncommits = 0;
763 /* Populate commit graph with a sufficient number of commits. */
764 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
765 repo);
766 if (err)
767 goto done;
769 /*
770 * Open the initial batch of commits, sorted in commit graph order.
771 * We keep all commits open throughout the lifetime of the log view
772 * in order to avoid having to re-fetch commits from disk while
773 * updating the display.
774 */
775 err = queue_commits(graph, &commits, start_id, LINES, 1, repo,
776 in_repo_path);
777 if (err) {
778 if (err->code != GOT_ERR_ITER_COMPLETED)
779 goto done;
780 err = NULL;
783 view = open_view();
784 if (view == NULL) {
785 err = got_error_from_errno();
786 goto done;
789 show_panel(view->panel);
791 first_displayed_entry = TAILQ_FIRST(&commits.head);
792 selected_entry = first_displayed_entry;
793 while (!done) {
794 err = draw_commits(view, &last_displayed_entry, &selected_entry,
795 first_displayed_entry, &commits, selected, LINES,
796 graph, repo, in_repo_path);
797 if (err)
798 goto done;
800 nodelay(stdscr, FALSE);
801 ch = wgetch(view->window);
802 nodelay(stdscr, TRUE);
803 switch (ch) {
804 case ERR:
805 break;
806 case 'q':
807 done = 1;
808 break;
809 case 'k':
810 case KEY_UP:
811 if (selected > 0)
812 selected--;
813 if (selected > 0)
814 break;
815 scroll_up(&first_displayed_entry, 1, &commits);
816 break;
817 case KEY_PPAGE:
818 if (TAILQ_FIRST(&commits.head) ==
819 first_displayed_entry) {
820 selected = 0;
821 break;
823 scroll_up(&first_displayed_entry, LINES,
824 &commits);
825 break;
826 case 'j':
827 case KEY_DOWN:
828 if (selected < MIN(LINES - 2,
829 commits.ncommits - 1)) {
830 selected++;
831 break;
833 err = scroll_down(&first_displayed_entry, 1,
834 last_displayed_entry, &commits, graph,
835 repo, in_repo_path);
836 if (err) {
837 if (err->code != GOT_ERR_ITER_COMPLETED)
838 goto done;
839 err = NULL;
841 break;
842 case KEY_NPAGE: {
843 struct commit_queue_entry *first = first_displayed_entry;
844 err = scroll_down(&first_displayed_entry, LINES,
845 last_displayed_entry, &commits, graph,
846 repo, in_repo_path);
847 if (err) {
848 if (err->code != GOT_ERR_ITER_COMPLETED)
849 goto done;
850 /* can't scroll any further; move cursor down */
851 if (first == first_displayed_entry && selected <
852 MIN(LINES - 2, commits.ncommits - 1)) {
853 selected = MIN(LINES - 2,
854 commits.ncommits - 1);
856 err = NULL;
858 break;
860 case KEY_RESIZE:
861 if (selected > LINES - 2)
862 selected = LINES - 2;
863 if (selected > commits.ncommits - 1)
864 selected = commits.ncommits - 1;
865 break;
866 case KEY_ENTER:
867 case '\r':
868 err = show_commit(selected_entry, repo);
869 if (err)
870 goto done;
871 show_panel(view->panel);
872 break;
873 case 't':
874 err = browse_commit(selected_entry, repo);
875 if (err)
876 goto done;
877 show_panel(view->panel);
878 break;
879 default:
880 break;
883 done:
884 if (view)
885 close_view(view);
886 free(head_id);
887 if (graph)
888 got_commit_graph_close(graph);
889 free_commits(&commits);
890 free(in_repo_path);
891 return err;
894 static const struct got_error *
895 cmd_log(int argc, char *argv[])
897 const struct got_error *error;
898 struct got_repository *repo = NULL;
899 struct got_object_id *start_id = NULL;
900 char *path = NULL, *repo_path = NULL, *cwd = NULL;
901 char *start_commit = NULL;
902 int ch;
904 #ifndef PROFILE
905 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
906 err(1, "pledge");
907 #endif
909 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
910 switch (ch) {
911 case 'c':
912 start_commit = optarg;
913 break;
914 case 'r':
915 repo_path = realpath(optarg, NULL);
916 if (repo_path == NULL)
917 err(1, "-r option");
918 break;
919 default:
920 usage();
921 /* NOTREACHED */
925 argc -= optind;
926 argv += optind;
928 if (argc == 0)
929 path = strdup("");
930 else if (argc == 1)
931 path = strdup(argv[0]);
932 else
933 usage_log();
934 if (path == NULL)
935 return got_error_from_errno();
937 cwd = getcwd(NULL, 0);
938 if (cwd == NULL) {
939 error = got_error_from_errno();
940 goto done;
942 if (repo_path == NULL) {
943 repo_path = strdup(cwd);
944 if (repo_path == NULL) {
945 error = got_error_from_errno();
946 goto done;
950 error = got_repo_open(&repo, repo_path);
951 if (error != NULL)
952 goto done;
954 if (start_commit == NULL) {
955 error = get_head_commit_id(&start_id, repo);
956 if (error != NULL)
957 goto done;
958 } else {
959 struct got_object *obj;
960 error = got_object_open_by_id_str(&obj, repo, start_commit);
961 if (error == NULL) {
962 start_id = got_object_get_id(obj);
963 if (start_id == NULL)
964 error = got_error_from_errno();
965 goto done;
968 if (error != NULL)
969 goto done;
971 error = show_log_view(start_id, repo, path);
972 done:
973 free(repo_path);
974 free(cwd);
975 free(path);
976 free(start_id);
977 if (repo)
978 got_repo_close(repo);
979 return error;
982 __dead static void
983 usage_diff(void)
985 endwin();
986 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
987 getprogname());
988 exit(1);
991 static char *
992 parse_next_line(FILE *f, size_t *len)
994 char *line;
995 size_t linelen;
996 size_t lineno;
997 const char delim[3] = { '\0', '\0', '\0'};
999 line = fparseln(f, &linelen, &lineno, delim, 0);
1000 if (len)
1001 *len = linelen;
1002 return line;
1005 static const struct got_error *
1006 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
1007 int *last_displayed_line, int *eof, int max_lines)
1009 const struct got_error *err;
1010 int nlines = 0, nprinted = 0;
1011 char *line;
1012 size_t len;
1013 wchar_t *wline;
1014 int width;
1016 rewind(f);
1017 werase(window);
1019 *eof = 0;
1020 while (nprinted < max_lines) {
1021 line = parse_next_line(f, &len);
1022 if (line == NULL) {
1023 *eof = 1;
1024 break;
1026 if (++nlines < *first_displayed_line) {
1027 free(line);
1028 continue;
1031 err = format_line(&wline, &width, line, COLS);
1032 if (err) {
1033 free(line);
1034 free(wline);
1035 return err;
1037 waddwstr(window, wline);
1038 if (width < COLS)
1039 waddch(window, '\n');
1040 if (++nprinted == 1)
1041 *first_displayed_line = nlines;
1042 free(line);
1043 free(wline);
1044 wline = NULL;
1046 *last_displayed_line = nlines;
1048 update_panels();
1049 doupdate();
1051 return NULL;
1054 static const struct got_error *
1055 show_diff_view(struct tog_view *view, struct got_object *obj1,
1056 struct got_object *obj2, struct got_repository *repo)
1058 const struct got_error *err;
1059 FILE *f;
1060 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1061 int eof, i;
1063 if (obj1 != NULL && obj2 != NULL &&
1064 got_object_get_type(obj1) != got_object_get_type(obj2))
1065 return got_error(GOT_ERR_OBJ_TYPE);
1067 f = got_opentemp();
1068 if (f == NULL)
1069 return got_error_from_errno();
1071 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1072 case GOT_OBJ_TYPE_BLOB:
1073 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1074 break;
1075 case GOT_OBJ_TYPE_TREE:
1076 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1077 break;
1078 case GOT_OBJ_TYPE_COMMIT:
1079 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1080 break;
1081 default:
1082 return got_error(GOT_ERR_OBJ_TYPE);
1085 fflush(f);
1087 show_panel(view->panel);
1089 while (!done) {
1090 err = draw_file(view->window, f, &first_displayed_line,
1091 &last_displayed_line, &eof, LINES);
1092 if (err)
1093 break;
1094 nodelay(stdscr, FALSE);
1095 ch = wgetch(view->window);
1096 nodelay(stdscr, TRUE);
1097 switch (ch) {
1098 case 'q':
1099 done = 1;
1100 break;
1101 case 'k':
1102 case KEY_UP:
1103 if (first_displayed_line > 1)
1104 first_displayed_line--;
1105 break;
1106 case KEY_PPAGE:
1107 case KEY_BACKSPACE:
1108 i = 0;
1109 while (i++ < LINES - 1 &&
1110 first_displayed_line > 1)
1111 first_displayed_line--;
1112 break;
1113 case 'j':
1114 case KEY_DOWN:
1115 if (!eof)
1116 first_displayed_line++;
1117 break;
1118 case KEY_NPAGE:
1119 case ' ':
1120 i = 0;
1121 while (!eof && i++ < LINES - 1) {
1122 char *line = parse_next_line(f, NULL);
1123 first_displayed_line++;
1124 if (line == NULL)
1125 break;
1127 break;
1128 default:
1129 break;
1132 fclose(f);
1133 return err;
1136 static const struct got_error *
1137 cmd_diff(int argc, char *argv[])
1139 const struct got_error *error = NULL;
1140 struct got_repository *repo = NULL;
1141 struct got_object *obj1 = NULL, *obj2 = NULL;
1142 char *repo_path = NULL;
1143 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1144 int ch;
1145 struct tog_view *view;
1147 #ifndef PROFILE
1148 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1149 err(1, "pledge");
1150 #endif
1152 while ((ch = getopt(argc, argv, "")) != -1) {
1153 switch (ch) {
1154 default:
1155 usage();
1156 /* NOTREACHED */
1160 argc -= optind;
1161 argv += optind;
1163 if (argc == 0) {
1164 usage_diff(); /* TODO show local worktree changes */
1165 } else if (argc == 2) {
1166 repo_path = getcwd(NULL, 0);
1167 if (repo_path == NULL)
1168 return got_error_from_errno();
1169 obj_id_str1 = argv[0];
1170 obj_id_str2 = argv[1];
1171 } else if (argc == 3) {
1172 repo_path = realpath(argv[0], NULL);
1173 if (repo_path == NULL)
1174 return got_error_from_errno();
1175 obj_id_str1 = argv[1];
1176 obj_id_str2 = argv[2];
1177 } else
1178 usage_diff();
1180 error = got_repo_open(&repo, repo_path);
1181 free(repo_path);
1182 if (error)
1183 goto done;
1185 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1186 if (error)
1187 goto done;
1189 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1190 if (error)
1191 goto done;
1193 view = open_view();
1194 if (view == NULL) {
1195 error = got_error_from_errno();
1196 goto done;
1198 error = show_diff_view(view, obj1, obj2, repo);
1199 close_view(view);
1200 done:
1201 got_repo_close(repo);
1202 if (obj1)
1203 got_object_close(obj1);
1204 if (obj2)
1205 got_object_close(obj2);
1206 return error;
1209 __dead static void
1210 usage_blame(void)
1212 endwin();
1213 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1214 getprogname());
1215 exit(1);
1218 struct tog_blame_line {
1219 int annotated;
1220 struct got_object_id *id;
1223 static const struct got_error *
1224 draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1225 struct tog_blame_line *lines, int nlines, int blame_complete,
1226 int selected_line, int *first_displayed_line, int *last_displayed_line,
1227 int *eof, int max_lines)
1229 const struct got_error *err;
1230 int lineno = 0, nprinted = 0;
1231 char *line;
1232 size_t len;
1233 wchar_t *wline;
1234 int width, wlimit;
1235 struct tog_blame_line *blame_line;
1236 struct got_object_id *prev_id = NULL;
1237 char *id_str;
1239 err = got_object_id_str(&id_str, id);
1240 if (err)
1241 return err;
1243 rewind(f);
1244 werase(window);
1246 if (asprintf(&line, "commit: %s", id_str) == -1) {
1247 err = got_error_from_errno();
1248 free(id_str);
1249 return err;
1252 err = format_line(&wline, &width, line, COLS);
1253 free(line);
1254 line = NULL;
1255 waddwstr(window, wline);
1256 free(wline);
1257 wline = NULL;
1258 if (width < COLS)
1259 waddch(window, '\n');
1261 if (asprintf(&line, "[%d/%d] %s%s",
1262 *first_displayed_line - 1 + selected_line, nlines,
1263 blame_complete ? "" : "annotating ", path) == -1) {
1264 free(id_str);
1265 return got_error_from_errno();
1267 free(id_str);
1268 err = format_line(&wline, &width, line, COLS);
1269 free(line);
1270 line = NULL;
1271 if (err)
1272 return err;
1273 waddwstr(window, wline);
1274 free(wline);
1275 wline = NULL;
1276 if (width < COLS)
1277 waddch(window, '\n');
1279 *eof = 0;
1280 while (nprinted < max_lines - 2) {
1281 line = parse_next_line(f, &len);
1282 if (line == NULL) {
1283 *eof = 1;
1284 break;
1286 if (++lineno < *first_displayed_line) {
1287 free(line);
1288 continue;
1291 wlimit = COLS < 9 ? 0 : COLS - 9;
1292 err = format_line(&wline, &width, line, wlimit);
1293 if (err) {
1294 free(line);
1295 return err;
1298 if (nprinted == selected_line - 1)
1299 wstandout(window);
1301 blame_line = &lines[lineno - 1];
1302 if (blame_line->annotated && prev_id &&
1303 got_object_id_cmp(prev_id, blame_line->id) == 0)
1304 waddstr(window, " ");
1305 else if (blame_line->annotated) {
1306 char *id_str;
1307 err = got_object_id_str(&id_str, blame_line->id);
1308 if (err) {
1309 free(line);
1310 free(wline);
1311 return err;
1313 wprintw(window, "%.8s ", id_str);
1314 free(id_str);
1315 prev_id = blame_line->id;
1316 } else {
1317 waddstr(window, "........ ");
1318 prev_id = NULL;
1321 waddwstr(window, wline);
1322 while (width < wlimit) {
1323 waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1324 width++;
1326 if (nprinted == selected_line - 1)
1327 wstandend(window);
1328 if (++nprinted == 1)
1329 *first_displayed_line = lineno;
1330 free(line);
1331 free(wline);
1332 wline = NULL;
1334 *last_displayed_line = lineno;
1336 update_panels();
1337 doupdate();
1339 return NULL;
1342 struct tog_blame_cb_args {
1343 pthread_mutex_t *mutex;
1344 struct tog_blame_line *lines; /* one per line */
1345 int nlines;
1347 struct tog_view *view;
1348 struct got_object_id *commit_id;
1349 FILE *f;
1350 const char *path;
1351 int *first_displayed_line;
1352 int *last_displayed_line;
1353 int *selected_line;
1354 int *quit;
1357 static const struct got_error *
1358 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1360 const struct got_error *err = NULL;
1361 struct tog_blame_cb_args *a = arg;
1362 struct tog_blame_line *line;
1363 int eof;
1365 if (nlines != a->nlines ||
1366 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1367 return got_error(GOT_ERR_RANGE);
1369 if (pthread_mutex_lock(a->mutex) != 0)
1370 return got_error_from_errno();
1372 if (*a->quit) { /* user has quit the blame view */
1373 err = got_error(GOT_ERR_ITER_COMPLETED);
1374 goto done;
1377 if (lineno == -1)
1378 goto done; /* no change in this commit */
1380 line = &a->lines[lineno - 1];
1381 if (line->annotated)
1382 goto done;
1384 line->id = got_object_id_dup(id);
1385 if (line->id == NULL) {
1386 err = got_error_from_errno();
1387 goto done;
1389 line->annotated = 1;
1391 err = draw_blame(a->view->window, a->commit_id, a->f, a->path,
1392 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1393 a->last_displayed_line, &eof, LINES);
1394 done:
1395 if (pthread_mutex_unlock(a->mutex) != 0)
1396 return got_error_from_errno();
1397 return err;
1400 struct tog_blame_thread_args {
1401 const char *path;
1402 struct got_repository *repo;
1403 struct tog_blame_cb_args *cb_args;
1404 int *complete;
1407 static void *
1408 blame_thread(void *arg)
1410 const struct got_error *err;
1411 struct tog_blame_thread_args *ta = arg;
1412 struct tog_blame_cb_args *a = ta->cb_args;
1413 int eof;
1415 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1416 blame_cb, ta->cb_args);
1418 if (pthread_mutex_lock(a->mutex) != 0)
1419 return (void *)got_error_from_errno();
1421 got_repo_close(ta->repo);
1422 ta->repo = NULL;
1423 *ta->complete = 1;
1424 if (!err)
1425 err = draw_blame(a->view->window, a->commit_id, a->f,
1426 a->path, a->lines, a->nlines, 1, *a->selected_line,
1427 a->first_displayed_line, a->last_displayed_line, &eof,
1428 LINES);
1430 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1431 err = got_error_from_errno();
1433 return (void *)err;
1436 static struct got_object_id *
1437 get_selected_commit_id(struct tog_blame_line *lines,
1438 int first_displayed_line, int selected_line)
1440 struct tog_blame_line *line;
1442 line = &lines[first_displayed_line - 1 + selected_line - 1];
1443 if (!line->annotated)
1444 return NULL;
1446 return line->id;
1449 static const struct got_error *
1450 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1451 struct tog_blame_line *lines, int first_displayed_line,
1452 int selected_line, struct got_repository *repo)
1454 const struct got_error *err = NULL;
1455 struct got_commit_object *commit = NULL;
1456 struct got_object_id *selected_id;
1457 struct got_object_qid *pid;
1459 *pobj = NULL;
1460 *obj = NULL;
1462 selected_id = get_selected_commit_id(lines,
1463 first_displayed_line, selected_line);
1464 if (selected_id == NULL)
1465 return NULL;
1467 err = got_object_open(obj, repo, selected_id);
1468 if (err)
1469 goto done;
1471 err = got_object_commit_open(&commit, repo, *obj);
1472 if (err)
1473 goto done;
1475 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1476 if (pid) {
1477 err = got_object_open(pobj, repo, pid->id);
1478 if (err)
1479 goto done;
1481 done:
1482 if (commit)
1483 got_object_commit_close(commit);
1484 return err;
1487 struct tog_blame {
1488 FILE *f;
1489 size_t filesize;
1490 struct tog_blame_line *lines;
1491 size_t nlines;
1492 pthread_t thread;
1493 struct tog_blame_thread_args thread_args;
1494 struct tog_blame_cb_args cb_args;
1495 const char *path;
1498 static const struct got_error *
1499 stop_blame(struct tog_blame *blame)
1501 const struct got_error *err = NULL;
1502 int i;
1504 if (blame->thread) {
1505 if (pthread_join(blame->thread, (void **)&err) != 0)
1506 err = got_error_from_errno();
1507 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1508 err = NULL;
1509 blame->thread = NULL;
1511 if (blame->thread_args.repo) {
1512 got_repo_close(blame->thread_args.repo);
1513 blame->thread_args.repo = NULL;
1515 if (blame->f) {
1516 fclose(blame->f);
1517 blame->f = NULL;
1519 for (i = 0; i < blame->nlines; i++)
1520 free(blame->lines[i].id);
1521 free(blame->lines);
1522 blame->lines = NULL;
1523 free(blame->cb_args.commit_id);
1524 blame->cb_args.commit_id = NULL;
1526 return err;
1529 static const struct got_error *
1530 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1531 struct tog_view *view, int *blame_complete,
1532 int *first_displayed_line, int *last_displayed_line,
1533 int *selected_line, int *done, const char *path,
1534 struct got_object_id *commit_id,
1535 struct got_repository *repo)
1537 const struct got_error *err = NULL;
1538 struct got_blob_object *blob = NULL;
1539 struct got_repository *thread_repo = NULL;
1540 struct got_object *obj;
1542 err = got_object_open_by_path(&obj, repo, commit_id, path);
1543 if (err)
1544 goto done;
1545 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1546 err = got_error(GOT_ERR_OBJ_TYPE);
1547 goto done;
1550 err = got_object_blob_open(&blob, repo, obj, 8192);
1551 if (err)
1552 goto done;
1553 blame->f = got_opentemp();
1554 if (blame->f == NULL) {
1555 err = got_error_from_errno();
1556 goto done;
1558 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1559 blame->f, blob);
1560 if (err)
1561 goto done;
1563 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1564 if (blame->lines == NULL) {
1565 err = got_error_from_errno();
1566 goto done;
1569 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1570 if (err)
1571 goto done;
1573 blame->cb_args.view = view;
1574 blame->cb_args.lines = blame->lines;
1575 blame->cb_args.nlines = blame->nlines;
1576 blame->cb_args.mutex = mutex;
1577 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1578 if (blame->cb_args.commit_id == NULL) {
1579 err = got_error_from_errno();
1580 goto done;
1582 blame->cb_args.f = blame->f;
1583 blame->cb_args.path = path;
1584 blame->cb_args.first_displayed_line = first_displayed_line;
1585 blame->cb_args.selected_line = selected_line;
1586 blame->cb_args.last_displayed_line = last_displayed_line;
1587 blame->cb_args.quit = done;
1589 blame->thread_args.path = path;
1590 blame->thread_args.repo = thread_repo;
1591 blame->thread_args.cb_args = &blame->cb_args;
1592 blame->thread_args.complete = blame_complete;
1593 *blame_complete = 0;
1595 if (pthread_create(&blame->thread, NULL, blame_thread,
1596 &blame->thread_args) != 0) {
1597 err = got_error_from_errno();
1598 goto done;
1601 done:
1602 if (blob)
1603 got_object_blob_close(blob);
1604 if (obj)
1605 got_object_close(obj);
1606 if (err)
1607 stop_blame(blame);
1608 return err;
1611 static const struct got_error *
1612 show_blame_view(const char *path, struct got_object_id *commit_id,
1613 struct got_repository *repo)
1615 const struct got_error *err = NULL, *thread_err = NULL;
1616 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1617 int selected_line = first_displayed_line;
1618 int eof, blame_complete = 0;
1619 struct got_object *obj = NULL, *pobj = NULL;
1620 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1621 struct tog_blame blame;
1622 struct got_object_id_queue blamed_commits;
1623 struct got_object_qid *blamed_commit = NULL;
1624 struct tog_view *view = NULL, *diff_view;
1626 SIMPLEQ_INIT(&blamed_commits);
1628 if (pthread_mutex_init(&mutex, NULL) != 0) {
1629 err = got_error_from_errno();
1630 goto done;
1633 err = got_object_qid_alloc(&blamed_commit, commit_id);
1634 if (err)
1635 goto done;
1636 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1638 view = open_view();
1639 if (view == NULL) {
1640 err = got_error_from_errno();
1641 goto done;
1643 show_panel(view->panel);
1645 memset(&blame, 0, sizeof(blame));
1646 err = run_blame(&blame, &mutex, view, &blame_complete,
1647 &first_displayed_line, &last_displayed_line,
1648 &selected_line, &done, path, blamed_commit->id, repo);
1649 if (err)
1650 return err;
1652 while (!done) {
1653 if (pthread_mutex_lock(&mutex) != 0) {
1654 err = got_error_from_errno();
1655 goto done;
1657 err = draw_blame(view->window, blamed_commit->id,
1658 blame.f, path, blame.lines, blame.nlines, blame_complete,
1659 selected_line, &first_displayed_line, &last_displayed_line,
1660 &eof, LINES);
1661 if (pthread_mutex_unlock(&mutex) != 0) {
1662 err = got_error_from_errno();
1663 goto done;
1665 if (err)
1666 break;
1667 nodelay(stdscr, FALSE);
1668 ch = wgetch(view->window);
1669 nodelay(stdscr, TRUE);
1670 if (pthread_mutex_lock(&mutex) != 0) {
1671 err = got_error_from_errno();
1672 goto done;
1674 switch (ch) {
1675 case 'q':
1676 done = 1;
1677 break;
1678 case 'k':
1679 case KEY_UP:
1680 if (selected_line > 1)
1681 selected_line--;
1682 else if (selected_line == 1 &&
1683 first_displayed_line > 1)
1684 first_displayed_line--;
1685 break;
1686 case KEY_PPAGE:
1687 case KEY_BACKSPACE:
1688 if (first_displayed_line == 1) {
1689 selected_line = 1;
1690 break;
1692 if (first_displayed_line > LINES - 2)
1693 first_displayed_line -= (LINES - 2);
1694 else
1695 first_displayed_line = 1;
1696 break;
1697 case 'j':
1698 case KEY_DOWN:
1699 if (selected_line < LINES - 2 &&
1700 first_displayed_line + selected_line <=
1701 blame.nlines)
1702 selected_line++;
1703 else if (last_displayed_line < blame.nlines)
1704 first_displayed_line++;
1705 break;
1706 case 'b':
1707 case 'p': {
1708 struct got_object_id *id;
1709 id = get_selected_commit_id(blame.lines,
1710 first_displayed_line, selected_line);
1711 if (id == NULL || got_object_id_cmp(id,
1712 blamed_commit->id) == 0)
1713 break;
1714 err = open_selected_commit(&pobj, &obj,
1715 blame.lines, first_displayed_line,
1716 selected_line, repo);
1717 if (err)
1718 break;
1719 if (pobj == NULL && obj == NULL)
1720 break;
1721 if (ch == 'p' && pobj == NULL)
1722 break;
1723 done = 1;
1724 if (pthread_mutex_unlock(&mutex) != 0) {
1725 err = got_error_from_errno();
1726 goto done;
1728 thread_err = stop_blame(&blame);
1729 done = 0;
1730 if (pthread_mutex_lock(&mutex) != 0) {
1731 err = got_error_from_errno();
1732 goto done;
1734 if (thread_err)
1735 break;
1736 id = got_object_get_id(ch == 'b' ? obj : pobj);
1737 got_object_close(obj);
1738 obj = NULL;
1739 if (pobj) {
1740 got_object_close(pobj);
1741 pobj = NULL;
1743 if (id == NULL) {
1744 err = got_error_from_errno();
1745 break;
1747 err = got_object_qid_alloc(&blamed_commit, id);
1748 free(id);
1749 if (err)
1750 goto done;
1751 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1752 blamed_commit, entry);
1753 err = run_blame(&blame, &mutex, view,
1754 &blame_complete, &first_displayed_line,
1755 &last_displayed_line, &selected_line,
1756 &done, path, blamed_commit->id, repo);
1757 if (err)
1758 break;
1759 break;
1761 case 'B': {
1762 struct got_object_qid *first;
1763 first = SIMPLEQ_FIRST(&blamed_commits);
1764 if (!got_object_id_cmp(first->id, commit_id))
1765 break;
1766 done = 1;
1767 if (pthread_mutex_unlock(&mutex) != 0) {
1768 err = got_error_from_errno();
1769 goto done;
1771 thread_err = stop_blame(&blame);
1772 done = 0;
1773 if (pthread_mutex_lock(&mutex) != 0) {
1774 err = got_error_from_errno();
1775 goto done;
1777 if (thread_err)
1778 break;
1779 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1780 got_object_qid_free(blamed_commit);
1781 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1782 err = run_blame(&blame, &mutex, view,
1783 &blame_complete, &first_displayed_line,
1784 &last_displayed_line, &selected_line,
1785 &done, path, blamed_commit->id, repo);
1786 if (err)
1787 break;
1788 break;
1790 case KEY_ENTER:
1791 case '\r':
1792 err = open_selected_commit(&pobj, &obj,
1793 blame.lines, first_displayed_line,
1794 selected_line, repo);
1795 if (err)
1796 break;
1797 if (pobj == NULL && obj == NULL)
1798 break;
1799 diff_view = open_view();
1800 if (diff_view == NULL) {
1801 err = got_error_from_errno();
1802 break;
1804 err = show_diff_view(diff_view, pobj, obj, repo);
1805 close_view(diff_view);
1806 if (pobj) {
1807 got_object_close(pobj);
1808 pobj = NULL;
1810 got_object_close(obj);
1811 obj = NULL;
1812 show_panel(view->panel);
1813 if (err)
1814 break;
1815 break;
1816 case KEY_NPAGE:
1817 case ' ':
1818 if (last_displayed_line >= blame.nlines &&
1819 selected_line < LINES - 2) {
1820 selected_line = MIN(blame.nlines,
1821 LINES - 2);
1822 break;
1824 if (last_displayed_line + LINES - 2 <=
1825 blame.nlines)
1826 first_displayed_line += LINES - 2;
1827 else
1828 first_displayed_line =
1829 blame.nlines - (LINES - 3);
1830 break;
1831 default:
1832 break;
1834 if (pthread_mutex_unlock(&mutex) != 0)
1835 err = got_error_from_errno();
1836 if (err || thread_err)
1837 break;
1839 done:
1840 if (pobj)
1841 got_object_close(pobj);
1842 if (blame.thread)
1843 thread_err = stop_blame(&blame);
1844 if (view)
1845 close_view(view);
1846 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1847 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1848 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1849 got_object_qid_free(blamed_commit);
1851 return thread_err ? thread_err : err;
1854 static const struct got_error *
1855 cmd_blame(int argc, char *argv[])
1857 const struct got_error *error;
1858 struct got_repository *repo = NULL;
1859 char *repo_path = NULL;
1860 char *path = NULL;
1861 struct got_object_id *commit_id = NULL;
1862 char *commit_id_str = NULL;
1863 int ch;
1865 #ifndef PROFILE
1866 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1867 err(1, "pledge");
1868 #endif
1870 while ((ch = getopt(argc, argv, "c:")) != -1) {
1871 switch (ch) {
1872 case 'c':
1873 commit_id_str = optarg;
1874 break;
1875 default:
1876 usage();
1877 /* NOTREACHED */
1881 argc -= optind;
1882 argv += optind;
1884 if (argc == 0) {
1885 usage_blame();
1886 } else if (argc == 1) {
1887 repo_path = getcwd(NULL, 0);
1888 if (repo_path == NULL)
1889 return got_error_from_errno();
1890 path = argv[0];
1891 } else if (argc == 2) {
1892 repo_path = realpath(argv[0], NULL);
1893 if (repo_path == NULL)
1894 return got_error_from_errno();
1895 path = argv[1];
1896 } else
1897 usage_blame();
1899 error = got_repo_open(&repo, repo_path);
1900 free(repo_path);
1901 if (error != NULL)
1902 return error;
1904 if (commit_id_str == NULL) {
1905 struct got_reference *head_ref;
1906 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1907 if (error != NULL)
1908 goto done;
1909 error = got_ref_resolve(&commit_id, repo, head_ref);
1910 got_ref_close(head_ref);
1911 } else {
1912 struct got_object *obj;
1913 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1914 if (error != NULL)
1915 goto done;
1916 commit_id = got_object_get_id(obj);
1917 if (commit_id == NULL)
1918 error = got_error_from_errno();
1919 got_object_close(obj);
1921 if (error != NULL)
1922 goto done;
1924 error = show_blame_view(path, commit_id, repo);
1925 done:
1926 free(commit_id);
1927 if (repo)
1928 got_repo_close(repo);
1929 return error;
1932 static const struct got_error *
1933 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1934 struct got_tree_entry **last_displayed_entry,
1935 struct got_tree_entry **selected_entry, int *ndisplayed,
1936 WINDOW *window, const char *label, int show_ids, const char *parent_path,
1937 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1939 const struct got_error *err = NULL;
1940 struct got_tree_entry *te;
1941 wchar_t *wline;
1942 int width, n;
1944 *ndisplayed = 0;
1946 werase(window);
1948 if (limit == 0)
1949 return NULL;
1951 err = format_line(&wline, &width, label, COLS);
1952 if (err)
1953 return err;
1954 waddwstr(window, wline);
1955 free(wline);
1956 wline = NULL;
1957 if (width < COLS)
1958 waddch(window, '\n');
1959 if (--limit <= 0)
1960 return NULL;
1961 err = format_line(&wline, &width, parent_path, COLS);
1962 if (err)
1963 return err;
1964 waddwstr(window, wline);
1965 free(wline);
1966 wline = NULL;
1967 if (width < COLS)
1968 waddch(window, '\n');
1969 if (--limit <= 0)
1970 return NULL;
1971 waddch(window, '\n');
1972 if (--limit <= 0)
1973 return NULL;
1975 te = SIMPLEQ_FIRST(&entries->head);
1976 if (*first_displayed_entry == NULL) {
1977 if (selected == 0) {
1978 wstandout(window);
1979 *selected_entry = NULL;
1981 waddstr(window, " ..\n"); /* parent directory */
1982 if (selected == 0)
1983 wstandend(window);
1984 (*ndisplayed)++;
1985 if (--limit <= 0)
1986 return NULL;
1987 n = 1;
1988 } else {
1989 n = 0;
1990 while (te != *first_displayed_entry)
1991 te = SIMPLEQ_NEXT(te, entry);
1994 while (te) {
1995 char *line = NULL, *id_str = NULL;
1997 if (show_ids) {
1998 err = got_object_id_str(&id_str, te->id);
1999 if (err)
2000 return got_error_from_errno();
2002 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2003 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2004 free(id_str);
2005 return got_error_from_errno();
2007 free(id_str);
2008 err = format_line(&wline, &width, line, COLS);
2009 if (err) {
2010 free(line);
2011 break;
2013 if (n == selected) {
2014 wstandout(window);
2015 *selected_entry = te;
2017 waddwstr(window, wline);
2018 if (width < COLS)
2019 waddch(window, '\n');
2020 if (n == selected)
2021 wstandend(window);
2022 free(line);
2023 free(wline);
2024 wline = NULL;
2025 n++;
2026 (*ndisplayed)++;
2027 *last_displayed_entry = te;
2028 if (--limit <= 0)
2029 break;
2030 te = SIMPLEQ_NEXT(te, entry);
2033 return err;
2036 static void
2037 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2038 const struct got_tree_entries *entries, int isroot)
2040 struct got_tree_entry *te, *prev;
2041 int i;
2043 if (*first_displayed_entry == NULL)
2044 return;
2046 te = SIMPLEQ_FIRST(&entries->head);
2047 if (*first_displayed_entry == te) {
2048 if (!isroot)
2049 *first_displayed_entry = NULL;
2050 return;
2053 /* XXX this is stupid... switch to TAILQ? */
2054 for (i = 0; i < maxscroll; i++) {
2055 while (te != *first_displayed_entry) {
2056 prev = te;
2057 te = SIMPLEQ_NEXT(te, entry);
2059 *first_displayed_entry = prev;
2060 te = SIMPLEQ_FIRST(&entries->head);
2062 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2063 *first_displayed_entry = NULL;
2066 static void
2067 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2068 struct got_tree_entry *last_displayed_entry,
2069 const struct got_tree_entries *entries)
2071 struct got_tree_entry *next;
2072 int n = 0;
2074 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2075 return;
2077 if (*first_displayed_entry)
2078 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2079 else
2080 next = SIMPLEQ_FIRST(&entries->head);
2081 while (next) {
2082 *first_displayed_entry = next;
2083 if (++n >= maxscroll)
2084 break;
2085 next = SIMPLEQ_NEXT(next, entry);
2089 struct tog_parent_tree {
2090 TAILQ_ENTRY(tog_parent_tree) entry;
2091 struct got_tree_object *tree;
2092 struct got_tree_entry *first_displayed_entry;
2093 struct got_tree_entry *selected_entry;
2094 int selected;
2097 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2099 static const struct got_error *
2100 tree_entry_path(char **path, struct tog_parent_trees *parents,
2101 struct got_tree_entry *te)
2103 const struct got_error *err = NULL;
2104 struct tog_parent_tree *pt;
2105 size_t len = 2; /* for leading slash and NUL */
2107 TAILQ_FOREACH(pt, parents, entry)
2108 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2109 if (te)
2110 len += strlen(te->name);
2112 *path = calloc(1, len);
2113 if (path == NULL)
2114 return got_error_from_errno();
2116 (*path)[0] = '/';
2117 pt = TAILQ_LAST(parents, tog_parent_trees);
2118 while (pt) {
2119 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2120 err = got_error(GOT_ERR_NO_SPACE);
2121 goto done;
2123 if (strlcat(*path, "/", len) >= len) {
2124 err = got_error(GOT_ERR_NO_SPACE);
2125 goto done;
2127 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2129 if (te) {
2130 if (strlcat(*path, te->name, len) >= len) {
2131 err = got_error(GOT_ERR_NO_SPACE);
2132 goto done;
2135 done:
2136 if (err) {
2137 free(*path);
2138 *path = NULL;
2140 return err;
2143 static const struct got_error *
2144 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2145 struct got_object_id *commit_id, struct got_repository *repo)
2147 const struct got_error *err = NULL;
2148 char *path;
2150 err = tree_entry_path(&path, parents, te);
2151 if (err)
2152 return err;
2154 err = show_blame_view(path, commit_id, repo);
2155 free(path);
2156 return err;
2159 static const struct got_error *
2160 log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2161 struct got_object_id *commit_id, struct got_repository *repo)
2163 const struct got_error *err = NULL;
2164 char *path;
2166 err = tree_entry_path(&path, parents, te);
2167 if (err)
2168 return err;
2170 err = show_log_view(commit_id, repo, path);
2171 free(path);
2172 return err;
2175 static const struct got_error *
2176 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2177 struct got_repository *repo)
2179 const struct got_error *err = NULL;
2180 int ch, done = 0, selected = 0, show_ids = 0;
2181 struct got_tree_object *tree = root;
2182 const struct got_tree_entries *entries;
2183 struct got_tree_entry *first_displayed_entry = NULL;
2184 struct got_tree_entry *last_displayed_entry = NULL;
2185 struct got_tree_entry *selected_entry = NULL;
2186 char *commit_id_str = NULL, *tree_label = NULL;
2187 int nentries, ndisplayed;
2188 struct tog_parent_trees parents;
2190 TAILQ_INIT(&parents);
2192 err = got_object_id_str(&commit_id_str, commit_id);
2193 if (err != NULL)
2194 goto done;
2196 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2197 err = got_error_from_errno();
2198 goto done;
2201 if (tog_tree_view.window == NULL) {
2202 tog_tree_view.window = newwin(0, 0, 0, 0);
2203 if (tog_tree_view.window == NULL)
2204 return got_error_from_errno();
2205 keypad(tog_tree_view.window, TRUE);
2207 if (tog_tree_view.panel == NULL) {
2208 tog_tree_view.panel = new_panel(tog_tree_view.window);
2209 if (tog_tree_view.panel == NULL)
2210 return got_error_from_errno();
2211 } else
2212 show_panel(tog_tree_view.panel);
2214 entries = got_object_tree_get_entries(root);
2215 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2216 while (!done) {
2217 char *parent_path;
2218 entries = got_object_tree_get_entries(tree);
2219 nentries = entries->nentries;
2220 if (tree != root)
2221 nentries++; /* '..' directory */
2223 err = tree_entry_path(&parent_path, &parents, NULL);
2224 if (err)
2225 goto done;
2227 err = draw_tree_entries(&first_displayed_entry,
2228 &last_displayed_entry, &selected_entry, &ndisplayed,
2229 tog_tree_view.window, tree_label, show_ids,
2230 parent_path, entries, selected, LINES, tree == root);
2231 free(parent_path);
2232 if (err)
2233 break;
2235 nodelay(stdscr, FALSE);
2236 ch = wgetch(tog_tree_view.window);
2237 nodelay(stdscr, TRUE);
2238 switch (ch) {
2239 case 'q':
2240 done = 1;
2241 break;
2242 case 'i':
2243 show_ids = !show_ids;
2244 break;
2245 case 'l':
2246 if (selected_entry) {
2247 err = log_tree_entry(selected_entry,
2248 &parents, commit_id, repo);
2249 if (err)
2250 goto done;
2252 break;
2253 case 'k':
2254 case KEY_UP:
2255 if (selected > 0)
2256 selected--;
2257 if (selected > 0)
2258 break;
2259 tree_scroll_up(&first_displayed_entry, 1,
2260 entries, tree == root);
2261 break;
2262 case KEY_PPAGE:
2263 if (SIMPLEQ_FIRST(&entries->head) ==
2264 first_displayed_entry) {
2265 if (tree != root)
2266 first_displayed_entry = NULL;
2267 selected = 0;
2268 break;
2270 tree_scroll_up(&first_displayed_entry, LINES,
2271 entries, tree == root);
2272 break;
2273 case 'j':
2274 case KEY_DOWN:
2275 if (selected < ndisplayed - 1) {
2276 selected++;
2277 break;
2279 tree_scroll_down(&first_displayed_entry, 1,
2280 last_displayed_entry, entries);
2281 break;
2282 case KEY_NPAGE:
2283 tree_scroll_down(&first_displayed_entry, LINES,
2284 last_displayed_entry, entries);
2285 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2286 break;
2287 /* can't scroll any further; move cursor down */
2288 if (selected < ndisplayed - 1)
2289 selected = ndisplayed - 1;
2290 break;
2291 case KEY_ENTER:
2292 case '\r':
2293 if (selected_entry == NULL) {
2294 struct tog_parent_tree *parent;
2295 case KEY_BACKSPACE:
2296 /* user selected '..' */
2297 if (tree == root)
2298 break;
2299 parent = TAILQ_FIRST(&parents);
2300 TAILQ_REMOVE(&parents, parent, entry);
2301 got_object_tree_close(tree);
2302 tree = parent->tree;
2303 first_displayed_entry =
2304 parent->first_displayed_entry;
2305 selected_entry = parent->selected_entry;
2306 selected = parent->selected;
2307 free(parent);
2308 } else if (S_ISDIR(selected_entry->mode)) {
2309 struct tog_parent_tree *parent;
2310 struct got_tree_object *child;
2311 err = got_object_open_as_tree(
2312 &child, repo, selected_entry->id);
2313 if (err)
2314 goto done;
2315 parent = calloc(1, sizeof(*parent));
2316 if (parent == NULL) {
2317 err = got_error_from_errno();
2318 goto done;
2320 parent->tree = tree;
2321 parent->first_displayed_entry =
2322 first_displayed_entry;
2323 parent->selected_entry = selected_entry;
2324 parent->selected = selected;
2325 TAILQ_INSERT_HEAD(&parents, parent,
2326 entry);
2327 tree = child;
2328 selected = 0;
2329 first_displayed_entry = NULL;
2330 } else if (S_ISREG(selected_entry->mode)) {
2331 err = blame_tree_entry(selected_entry,
2332 &parents, commit_id, repo);
2333 if (err)
2334 goto done;
2336 break;
2337 case KEY_RESIZE:
2338 if (selected > LINES)
2339 selected = ndisplayed - 1;
2340 break;
2341 default:
2342 break;
2345 done:
2346 free(tree_label);
2347 free(commit_id_str);
2348 while (!TAILQ_EMPTY(&parents)) {
2349 struct tog_parent_tree *parent;
2350 parent = TAILQ_FIRST(&parents);
2351 TAILQ_REMOVE(&parents, parent, entry);
2352 free(parent);
2355 if (tree != root)
2356 got_object_tree_close(tree);
2357 return err;
2360 __dead static void
2361 usage_tree(void)
2363 endwin();
2364 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2365 getprogname());
2366 exit(1);
2369 static const struct got_error *
2370 cmd_tree(int argc, char *argv[])
2372 const struct got_error *error;
2373 struct got_repository *repo = NULL;
2374 char *repo_path = NULL;
2375 struct got_object_id *commit_id = NULL;
2376 char *commit_id_arg = NULL;
2377 struct got_commit_object *commit = NULL;
2378 struct got_tree_object *tree = NULL;
2379 int ch;
2381 #ifndef PROFILE
2382 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2383 err(1, "pledge");
2384 #endif
2386 while ((ch = getopt(argc, argv, "c:")) != -1) {
2387 switch (ch) {
2388 case 'c':
2389 commit_id_arg = optarg;
2390 break;
2391 default:
2392 usage();
2393 /* NOTREACHED */
2397 argc -= optind;
2398 argv += optind;
2400 if (argc == 0) {
2401 repo_path = getcwd(NULL, 0);
2402 if (repo_path == NULL)
2403 return got_error_from_errno();
2404 } else if (argc == 1) {
2405 repo_path = realpath(argv[0], NULL);
2406 if (repo_path == NULL)
2407 return got_error_from_errno();
2408 } else
2409 usage_log();
2411 error = got_repo_open(&repo, repo_path);
2412 free(repo_path);
2413 if (error != NULL)
2414 return error;
2416 if (commit_id_arg == NULL) {
2417 error = get_head_commit_id(&commit_id, repo);
2418 if (error != NULL)
2419 goto done;
2420 } else {
2421 struct got_object *obj;
2422 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2423 if (error == NULL) {
2424 commit_id = got_object_get_id(obj);
2425 if (commit_id == NULL)
2426 error = got_error_from_errno();
2429 if (error != NULL)
2430 goto done;
2432 error = got_object_open_as_commit(&commit, repo, commit_id);
2433 if (error != NULL)
2434 goto done;
2436 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2437 if (error != NULL)
2438 goto done;
2440 error = show_tree_view(tree, commit_id, repo);
2441 done:
2442 free(commit_id);
2443 if (commit)
2444 got_object_commit_close(commit);
2445 if (tree)
2446 got_object_tree_close(tree);
2447 if (repo)
2448 got_repo_close(repo);
2449 return error;
2451 static void
2452 init_curses(void)
2454 initscr();
2455 cbreak();
2456 noecho();
2457 nonl();
2458 intrflush(stdscr, FALSE);
2459 keypad(stdscr, TRUE);
2460 curs_set(0);
2463 __dead static void
2464 usage(void)
2466 int i;
2468 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2469 "Available commands:\n", getprogname());
2470 for (i = 0; i < nitems(tog_commands); i++) {
2471 struct tog_cmd *cmd = &tog_commands[i];
2472 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2474 exit(1);
2477 static char **
2478 make_argv(const char *arg0, const char *arg1)
2480 char **argv;
2481 int argc = (arg1 == NULL ? 1 : 2);
2483 argv = calloc(argc, sizeof(char *));
2484 if (argv == NULL)
2485 err(1, "calloc");
2486 argv[0] = strdup(arg0);
2487 if (argv[0] == NULL)
2488 err(1, "calloc");
2489 if (arg1) {
2490 argv[1] = strdup(arg1);
2491 if (argv[1] == NULL)
2492 err(1, "calloc");
2495 return argv;
2498 int
2499 main(int argc, char *argv[])
2501 const struct got_error *error = NULL;
2502 struct tog_cmd *cmd = NULL;
2503 int ch, hflag = 0;
2504 char **cmd_argv = NULL;
2506 setlocale(LC_ALL, "");
2508 while ((ch = getopt(argc, argv, "h")) != -1) {
2509 switch (ch) {
2510 case 'h':
2511 hflag = 1;
2512 break;
2513 default:
2514 usage();
2515 /* NOTREACHED */
2519 argc -= optind;
2520 argv += optind;
2521 optind = 0;
2522 optreset = 1;
2524 if (argc == 0) {
2525 if (hflag)
2526 usage();
2527 /* Build an argument vector which runs a default command. */
2528 cmd = &tog_commands[0];
2529 cmd_argv = make_argv(cmd->name, NULL);
2530 argc = 1;
2531 } else {
2532 int i;
2534 /* Did the user specific a command? */
2535 for (i = 0; i < nitems(tog_commands); i++) {
2536 if (strncmp(tog_commands[i].name, argv[0],
2537 strlen(argv[0])) == 0) {
2538 cmd = &tog_commands[i];
2539 if (hflag)
2540 tog_commands[i].cmd_usage();
2541 break;
2544 if (cmd == NULL) {
2545 /* Did the user specify a repository? */
2546 char *repo_path = realpath(argv[0], NULL);
2547 if (repo_path) {
2548 struct got_repository *repo;
2549 error = got_repo_open(&repo, repo_path);
2550 if (error == NULL)
2551 got_repo_close(repo);
2552 } else
2553 error = got_error_from_errno();
2554 if (error) {
2555 if (hflag) {
2556 fprintf(stderr, "%s: '%s' is not a "
2557 "known command\n", getprogname(),
2558 argv[0]);
2559 usage();
2561 fprintf(stderr, "%s: '%s' is neither a known "
2562 "command nor a path to a repository\n",
2563 getprogname(), argv[0]);
2564 free(repo_path);
2565 return 1;
2567 cmd = &tog_commands[0];
2568 cmd_argv = make_argv(cmd->name, repo_path);
2569 argc = 2;
2570 free(repo_path);
2574 init_curses();
2576 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2577 if (error)
2578 goto done;
2579 done:
2580 endwin();
2581 free(cmd_argv);
2582 if (error)
2583 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2584 return 0;