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>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_diff.h"
42 #include "got_opentemp.h"
43 #include "got_commit_graph.h"
44 #include "got_utf8.h"
45 #include "got_blame.h"
47 #ifndef MIN
48 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 #endif
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 struct tog_cmd {
56 const char *name;
57 const struct got_error *(*cmd_main)(int, char *[]);
58 void (*cmd_usage)(void);
59 const char *descr;
60 };
62 __dead static void usage(void);
63 __dead static void usage_log(void);
64 __dead static void usage_diff(void);
65 __dead static void usage_blame(void);
66 __dead static void usage_tree(void);
68 static const struct got_error* cmd_log(int, char *[]);
69 static const struct got_error* cmd_diff(int, char *[]);
70 static const struct got_error* cmd_blame(int, char *[]);
71 static const struct got_error* cmd_tree(int, char *[]);
73 static struct tog_cmd tog_commands[] = {
74 { "log", cmd_log, usage_log,
75 "show repository history" },
76 { "diff", cmd_diff, usage_diff,
77 "compare files and directories" },
78 { "blame", cmd_blame, usage_blame,
79 "show line-by-line file history" },
80 { "tree", cmd_tree, usage_tree,
81 "browse trees in repository" },
82 };
84 static struct tog_view {
85 WINDOW *window;
86 PANEL *panel;
87 } tog_log_view, tog_diff_view, tog_blame_view, tog_tree_view;
89 static const struct got_error *
90 show_diff_view(struct got_object *, struct got_object *,
91 struct got_repository *);
92 static const struct got_error *
93 show_log_view(struct got_object_id *, struct got_repository *);
94 static const struct got_error *
95 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
96 static const struct got_error *
97 show_tree_view(struct got_tree_object *, struct got_object_id *,
98 struct got_repository *);
100 __dead static void
101 usage_log(void)
103 endwin();
104 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
105 getprogname());
106 exit(1);
109 /* Create newly allocated wide-character string equivalent to a byte string. */
110 static const struct got_error *
111 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
113 char *vis = NULL;
114 const struct got_error *err = NULL;
116 *ws = NULL;
117 *wlen = mbstowcs(NULL, s, 0);
118 if (*wlen == (size_t)-1) {
119 int vislen;
120 if (errno != EILSEQ)
121 return got_error_from_errno();
123 /* byte string invalid in current encoding; try to "fix" it */
124 err = got_mbsavis(&vis, &vislen, s);
125 if (err)
126 return err;
127 *wlen = mbstowcs(NULL, vis, 0);
128 if (*wlen == (size_t)-1) {
129 err = got_error_from_errno(); /* give up */
130 goto done;
134 *ws = calloc(*wlen + 1, sizeof(*ws));
135 if (*ws == NULL) {
136 err = got_error_from_errno();
137 goto done;
140 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
141 err = got_error_from_errno();
142 done:
143 free(vis);
144 if (err) {
145 free(*ws);
146 *ws = NULL;
147 *wlen = 0;
149 return err;
152 /* Format a line for display, ensuring that it won't overflow a width limit. */
153 static const struct got_error *
154 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
156 const struct got_error *err = NULL;
157 int cols = 0;
158 wchar_t *wline = NULL;
159 size_t wlen;
160 int i;
162 *wlinep = NULL;
164 err = mbs2ws(&wline, &wlen, line);
165 if (err)
166 return err;
168 i = 0;
169 while (i < wlen && cols <= wlimit) {
170 int width = wcwidth(wline[i]);
171 switch (width) {
172 case 0:
173 break;
174 case 1:
175 case 2:
176 cols += width;
177 break;
178 case -1:
179 if (wline[i] == L'\t')
180 cols += TABSIZE;
181 break;
182 default:
183 err = got_error_from_errno();
184 goto done;
186 if (cols <= COLS) {
187 i++;
188 if (widthp)
189 *widthp = cols;
192 wline[i] = L'\0';
193 done:
194 if (err)
195 free(wline);
196 else
197 *wlinep = wline;
198 return err;
201 static const struct got_error *
202 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
204 const struct got_error *err = NULL;
205 char *logmsg0 = NULL, *logmsg = NULL;
206 char *author0 = NULL, *author = NULL;
207 wchar_t *wlogmsg = NULL, *wauthor = NULL;
208 int author_width, logmsg_width;
209 char *newline, *smallerthan;
210 char *line = NULL;
211 char *id_str = NULL;
212 size_t id_len;
213 int col, limit;
214 static const size_t id_display_cols = 8;
215 static const size_t author_display_cols = 16;
216 const int avail = COLS;
218 err = got_object_id_str(&id_str, id);
219 if (err)
220 return err;
221 id_len = strlen(id_str);
222 if (avail < id_display_cols) {
223 limit = MIN(id_len, avail);
224 waddnstr(tog_log_view.window, id_str, limit);
225 } else {
226 limit = MIN(id_display_cols, id_len);
227 waddnstr(tog_log_view.window, id_str, limit);
229 col = limit + 1;
230 while (col <= avail && col < id_display_cols + 2) {
231 waddch(tog_log_view.window, ' ');
232 col++;
234 if (col > avail)
235 goto done;
237 author0 = strdup(commit->author);
238 if (author0 == NULL) {
239 err = got_error_from_errno();
240 goto done;
242 author = author0;
243 smallerthan = strchr(author, '<');
244 if (smallerthan)
245 *smallerthan = '\0';
246 else {
247 char *at = strchr(author, '@');
248 if (at)
249 *at = '\0';
251 limit = MIN(avail, author_display_cols);
252 err = format_line(&wauthor, &author_width, author, limit);
253 if (err)
254 goto done;
255 waddwstr(tog_log_view.window, wauthor);
256 col += author_width;
257 while (col <= avail && author_width < author_display_cols + 1) {
258 waddch(tog_log_view.window, ' ');
259 col++;
260 author_width++;
262 if (col > avail)
263 goto done;
265 logmsg0 = strdup(commit->logmsg);
266 if (logmsg0 == NULL) {
267 err = got_error_from_errno();
268 goto done;
270 logmsg = logmsg0;
271 while (*logmsg == '\n')
272 logmsg++;
273 newline = strchr(logmsg, '\n');
274 if (newline)
275 *newline = '\0';
276 limit = avail - col;
277 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
278 if (err)
279 goto done;
280 waddwstr(tog_log_view.window, wlogmsg);
281 col += logmsg_width;
282 while (col <= avail) {
283 waddch(tog_log_view.window, ' ');
284 col++;
286 done:
287 free(logmsg0);
288 free(wlogmsg);
289 free(author0);
290 free(wauthor);
291 free(line);
292 free(id_str);
293 return err;
296 struct commit_queue_entry {
297 TAILQ_ENTRY(commit_queue_entry) entry;
298 struct got_object_id *id;
299 struct got_commit_object *commit;
300 };
301 TAILQ_HEAD(commit_queue, commit_queue_entry);
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);
324 TAILQ_REMOVE(commits, entry, entry);
325 got_object_commit_close(entry->commit);
326 /* Don't free entry->id! It is owned by the commit graph. */
327 free(entry);
330 static void
331 free_commits(struct commit_queue *commits)
333 while (!TAILQ_EMPTY(commits))
334 pop_commit(commits);
337 static const struct got_error *
338 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
339 struct got_object_id *start_id, struct got_repository *repo)
341 const struct got_error *err = NULL;
342 struct got_object_id *id;
343 struct commit_queue_entry *entry;
345 err = got_commit_graph_iter_start(graph, start_id);
346 if (err)
347 return err;
349 entry = TAILQ_LAST(commits, commit_queue);
350 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
351 int nfetched;
353 /* Start ID's commit is already on the queue; skip over it. */
354 err = got_commit_graph_iter_next(&id, graph);
355 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
356 return err;
358 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
359 if (err)
360 return err;
363 while (1) {
364 struct got_commit_object *commit;
366 err = got_commit_graph_iter_next(&id, graph);
367 if (err) {
368 if (err->code == GOT_ERR_ITER_NEED_MORE)
369 err = NULL;
370 break;
373 err = got_object_open_as_commit(&commit, repo, id);
374 if (err)
375 break;
377 entry = alloc_commit_queue_entry(commit, id);
378 if (entry == NULL) {
379 err = got_error_from_errno();
380 break;
383 TAILQ_INSERT_TAIL(commits, entry, entry);
386 return err;
389 static const struct got_error *
390 fetch_next_commit(struct commit_queue_entry **pentry,
391 struct commit_queue_entry *entry, struct commit_queue *commits,
392 struct got_commit_graph *graph, struct got_repository *repo)
394 const struct got_error *err = NULL;
395 struct got_object_qid *qid;
397 *pentry = NULL;
399 /* Populate commit graph with entry's parent commits. */
400 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
401 int nfetched;
402 err = got_commit_graph_fetch_commits_up_to(&nfetched,
403 graph, qid->id, repo);
404 if (err)
405 return err;
408 /* Append outstanding commits to queue in graph sort order. */
409 err = queue_commits(graph, commits, entry->id, repo);
410 if (err) {
411 if (err->code == GOT_ERR_ITER_COMPLETED)
412 err = NULL;
413 return err;
416 /* Next entry to display should now be available. */
417 *pentry = TAILQ_NEXT(entry, entry);
418 if (*pentry == NULL)
419 return got_error(GOT_ERR_NO_OBJ);
421 return NULL;
424 static const struct got_error *
425 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
427 const struct got_error *err = NULL;
428 struct got_reference *head_ref;
430 *head_id = NULL;
432 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
433 if (err)
434 return err;
436 err = got_ref_resolve(head_id, repo, head_ref);
437 got_ref_close(head_ref);
438 if (err) {
439 *head_id = NULL;
440 return err;
443 return NULL;
446 static const struct got_error *
447 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
448 struct commit_queue_entry *first, int selected_idx, int limit)
450 const struct got_error *err = NULL;
451 struct commit_queue_entry *entry;
452 int ncommits = 0;
454 werase(tog_log_view.window);
456 entry = first;
457 *last = first;
458 while (entry) {
459 if (ncommits == limit)
460 break;
461 if (ncommits == selected_idx) {
462 wstandout(tog_log_view.window);
463 *selected = entry;
465 err = draw_commit(entry->commit, entry->id);
466 if (ncommits == selected_idx)
467 wstandend(tog_log_view.window);
468 if (err)
469 break;
470 ncommits++;
471 *last = entry;
472 entry = TAILQ_NEXT(entry, entry);
475 update_panels();
476 doupdate();
478 return err;
481 static void
482 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
483 struct commit_queue *commits)
485 struct commit_queue_entry *entry;
486 int nscrolled = 0;
488 entry = TAILQ_FIRST(commits);
489 if (*first_displayed_entry == entry)
490 return;
492 entry = *first_displayed_entry;
493 while (entry && nscrolled < maxscroll) {
494 entry = TAILQ_PREV(entry, commit_queue, entry);
495 if (entry) {
496 *first_displayed_entry = entry;
497 nscrolled++;
502 static const struct got_error *
503 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
504 struct commit_queue_entry *last_displayed_entry,
505 struct commit_queue *commits, struct got_commit_graph *graph,
506 struct got_repository *repo)
508 const struct got_error *err = NULL;
509 struct commit_queue_entry *pentry;
510 int nscrolled = 0;
512 do {
513 pentry = TAILQ_NEXT(last_displayed_entry, entry);
514 if (pentry == NULL) {
515 err = fetch_next_commit(&pentry, last_displayed_entry,
516 commits, graph, repo);
517 if (err || pentry == NULL)
518 break;
520 last_displayed_entry = pentry;
522 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
523 if (pentry == NULL)
524 break;
525 *first_displayed_entry = pentry;
526 } while (++nscrolled < maxscroll);
528 return err;
531 static int
532 num_parents(struct commit_queue_entry *entry)
534 int nparents = 0;
536 while (entry) {
537 entry = TAILQ_NEXT(entry, entry);
538 nparents++;
541 return nparents;
544 static const struct got_error *
545 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
547 const struct got_error *err;
548 struct got_object *obj1 = NULL, *obj2 = NULL;
549 struct got_object_qid *parent_id;
551 err = got_object_open(&obj2, repo, entry->id);
552 if (err)
553 return err;
555 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
556 if (parent_id) {
557 err = got_object_open(&obj1, repo, parent_id->id);
558 if (err)
559 goto done;
562 err = show_diff_view(obj1, obj2, repo);
563 done:
564 if (obj1)
565 got_object_close(obj1);
566 if (obj2)
567 got_object_close(obj2);
568 return err;
571 static const struct got_error *
572 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
574 const struct got_error *err = NULL;
575 struct got_object_id *head_id = NULL;
576 int ch, done = 0, selected = 0, nparents, nfetched;
577 struct got_commit_graph *graph;
578 struct commit_queue commits;
579 struct commit_queue_entry *entry = NULL;
580 struct commit_queue_entry *first_displayed_entry = NULL;
581 struct commit_queue_entry *last_displayed_entry = NULL;
582 struct commit_queue_entry *selected_entry = NULL;
584 if (tog_log_view.window == NULL) {
585 tog_log_view.window = newwin(0, 0, 0, 0);
586 if (tog_log_view.window == NULL)
587 return got_error_from_errno();
588 keypad(tog_log_view.window, TRUE);
590 if (tog_log_view.panel == NULL) {
591 tog_log_view.panel = new_panel(tog_log_view.window);
592 if (tog_log_view.panel == NULL)
593 return got_error_from_errno();
594 } else
595 show_panel(tog_log_view.panel);
597 err = get_head_commit_id(&head_id, repo);
598 if (err)
599 return err;
601 TAILQ_INIT(&commits);
603 err = got_commit_graph_open(&graph, head_id, 0, repo);
604 if (err)
605 goto done;
607 /* Populate commit graph with a sufficient number of commits. */
608 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
609 repo);
610 if (err)
611 goto done;
612 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
613 if (err)
614 goto done;
616 /*
617 * Open the initial batch of commits, sorted in commit graph order.
618 * We keep all commits open throughout the lifetime of the log view
619 * in order to avoid having to re-fetch commits from disk while
620 * updating the display.
621 */
622 err = queue_commits(graph, &commits, head_id, repo);
623 if (err && err->code != GOT_ERR_ITER_COMPLETED)
624 goto done;
626 /* Find entry corresponding to the first commit to display. */
627 TAILQ_FOREACH(entry, &commits, entry) {
628 if (got_object_id_cmp(entry->id, start_id) == 0) {
629 first_displayed_entry = entry;
630 break;
633 if (first_displayed_entry == NULL) {
634 err = got_error(GOT_ERR_NO_OBJ);
635 goto done;
638 while (!done) {
639 err = draw_commits(&last_displayed_entry, &selected_entry,
640 first_displayed_entry, selected, LINES);
641 if (err)
642 goto done;
644 nodelay(stdscr, FALSE);
645 ch = wgetch(tog_log_view.window);
646 nodelay(stdscr, TRUE);
647 switch (ch) {
648 case ERR:
649 if (errno) {
650 err = got_error_from_errno();
651 goto done;
653 break;
654 case 'q':
655 done = 1;
656 break;
657 case 'k':
658 case KEY_UP:
659 if (selected > 0)
660 selected--;
661 if (selected > 0)
662 break;
663 scroll_up(&first_displayed_entry, 1, &commits);
664 break;
665 case KEY_PPAGE:
666 if (TAILQ_FIRST(&commits) ==
667 first_displayed_entry) {
668 selected = 0;
669 break;
671 scroll_up(&first_displayed_entry, LINES,
672 &commits);
673 break;
674 case 'j':
675 case KEY_DOWN:
676 nparents = num_parents(first_displayed_entry);
677 if (selected < LINES - 1 &&
678 selected < nparents - 1) {
679 selected++;
680 break;
682 err = scroll_down(&first_displayed_entry, 1,
683 last_displayed_entry, &commits, graph,
684 repo);
685 if (err)
686 goto done;
687 break;
688 case KEY_NPAGE:
689 err = scroll_down(&first_displayed_entry, LINES,
690 last_displayed_entry, &commits, graph,
691 repo);
692 if (err)
693 goto done;
694 if (last_displayed_entry->commit->nparents > 0)
695 break;
696 /* can't scroll any further; move cursor down */
697 nparents = num_parents(first_displayed_entry);
698 if (selected < LINES - 1 ||
699 selected < nparents - 1)
700 selected = MIN(LINES - 1, nparents - 1);
701 break;
702 case KEY_RESIZE:
703 if (selected > LINES)
704 selected = LINES - 1;
705 break;
706 case KEY_ENTER:
707 case '\r':
708 err = show_commit(selected_entry, repo);
709 if (err)
710 break;
711 show_panel(tog_log_view.panel);
712 break;
713 default:
714 break;
717 done:
718 free(head_id);
719 if (graph)
720 got_commit_graph_close(graph);
721 free_commits(&commits);
722 return err;
725 static const struct got_error *
726 cmd_log(int argc, char *argv[])
728 const struct got_error *error;
729 struct got_repository *repo;
730 struct got_object_id *start_id = NULL;
731 char *repo_path = NULL;
732 char *start_commit = NULL;
733 int ch;
735 #ifndef PROFILE
736 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
737 err(1, "pledge");
738 #endif
740 while ((ch = getopt(argc, argv, "c:")) != -1) {
741 switch (ch) {
742 case 'c':
743 start_commit = optarg;
744 break;
745 default:
746 usage();
747 /* NOTREACHED */
751 argc -= optind;
752 argv += optind;
754 if (argc == 0) {
755 repo_path = getcwd(NULL, 0);
756 if (repo_path == NULL)
757 return got_error_from_errno();
758 } else if (argc == 1) {
759 repo_path = realpath(argv[0], NULL);
760 if (repo_path == NULL)
761 return got_error_from_errno();
762 } else
763 usage_log();
765 error = got_repo_open(&repo, repo_path);
766 free(repo_path);
767 if (error != NULL)
768 return error;
770 if (start_commit == NULL) {
771 error = get_head_commit_id(&start_id, repo);
772 if (error != NULL)
773 return error;
774 } else {
775 struct got_object *obj;
776 error = got_object_open_by_id_str(&obj, repo, start_commit);
777 if (error == NULL) {
778 start_id = got_object_get_id(obj);
779 if (start_id == NULL)
780 error = got_error_from_errno();
783 if (error != NULL)
784 return error;
785 error = show_log_view(start_id, repo);
786 free(start_id);
787 got_repo_close(repo);
788 return error;
791 __dead static void
792 usage_diff(void)
794 endwin();
795 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
796 getprogname());
797 exit(1);
800 static char *
801 parse_next_line(FILE *f, size_t *len)
803 char *line;
804 size_t linelen;
805 size_t lineno;
806 const char delim[3] = { '\0', '\0', '\0'};
808 line = fparseln(f, &linelen, &lineno, delim, 0);
809 if (len)
810 *len = linelen;
811 return line;
814 static const struct got_error *
815 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
816 int *last_displayed_line, int *eof, int max_lines)
818 const struct got_error *err;
819 int nlines = 0, nprinted = 0;
820 char *line;
821 size_t len;
822 wchar_t *wline;
823 int width;
825 rewind(f);
826 werase(window);
828 *eof = 0;
829 while (nprinted < max_lines) {
830 line = parse_next_line(f, &len);
831 if (line == NULL) {
832 *eof = 1;
833 break;
835 if (++nlines < *first_displayed_line) {
836 free(line);
837 continue;
840 err = format_line(&wline, &width, line, COLS);
841 if (err) {
842 free(line);
843 return err;
845 waddwstr(window, wline);
846 if (width < COLS)
847 waddch(window, '\n');
848 if (++nprinted == 1)
849 *first_displayed_line = nlines;
850 free(line);
852 *last_displayed_line = nlines;
854 update_panels();
855 doupdate();
857 return NULL;
860 static const struct got_error *
861 show_diff_view(struct got_object *obj1, struct got_object *obj2,
862 struct got_repository *repo)
864 const struct got_error *err;
865 FILE *f;
866 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
867 int eof, i;
869 if (obj1 != NULL && obj2 != NULL &&
870 got_object_get_type(obj1) != got_object_get_type(obj2))
871 return got_error(GOT_ERR_OBJ_TYPE);
873 f = got_opentemp();
874 if (f == NULL)
875 return got_error_from_errno();
877 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
878 case GOT_OBJ_TYPE_BLOB:
879 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
880 break;
881 case GOT_OBJ_TYPE_TREE:
882 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
883 break;
884 case GOT_OBJ_TYPE_COMMIT:
885 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
886 break;
887 default:
888 return got_error(GOT_ERR_OBJ_TYPE);
891 fflush(f);
893 if (tog_diff_view.window == NULL) {
894 tog_diff_view.window = newwin(0, 0, 0, 0);
895 if (tog_diff_view.window == NULL)
896 return got_error_from_errno();
897 keypad(tog_diff_view.window, TRUE);
899 if (tog_diff_view.panel == NULL) {
900 tog_diff_view.panel = new_panel(tog_diff_view.window);
901 if (tog_diff_view.panel == NULL)
902 return got_error_from_errno();
903 } else
904 show_panel(tog_diff_view.panel);
906 while (!done) {
907 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
908 &last_displayed_line, &eof, LINES);
909 if (err)
910 break;
911 nodelay(stdscr, FALSE);
912 ch = wgetch(tog_diff_view.window);
913 nodelay(stdscr, TRUE);
914 switch (ch) {
915 case 'q':
916 done = 1;
917 break;
918 case 'k':
919 case KEY_UP:
920 case KEY_BACKSPACE:
921 if (first_displayed_line > 1)
922 first_displayed_line--;
923 break;
924 case KEY_PPAGE:
925 i = 0;
926 while (i++ < LINES - 1 &&
927 first_displayed_line > 1)
928 first_displayed_line--;
929 break;
930 case 'j':
931 case KEY_DOWN:
932 case KEY_ENTER:
933 case '\r':
934 if (!eof)
935 first_displayed_line++;
936 break;
937 case KEY_NPAGE:
938 case ' ':
939 i = 0;
940 while (!eof && i++ < LINES - 1) {
941 char *line = parse_next_line(f, NULL);
942 first_displayed_line++;
943 if (line == NULL)
944 break;
946 break;
947 default:
948 break;
951 fclose(f);
952 return err;
955 static const struct got_error *
956 cmd_diff(int argc, char *argv[])
958 const struct got_error *error = NULL;
959 struct got_repository *repo = NULL;
960 struct got_object *obj1 = NULL, *obj2 = NULL;
961 char *repo_path = NULL;
962 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
963 int ch;
965 #ifndef PROFILE
966 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
967 err(1, "pledge");
968 #endif
970 while ((ch = getopt(argc, argv, "")) != -1) {
971 switch (ch) {
972 default:
973 usage();
974 /* NOTREACHED */
978 argc -= optind;
979 argv += optind;
981 if (argc == 0) {
982 usage_diff(); /* TODO show local worktree changes */
983 } else if (argc == 2) {
984 repo_path = getcwd(NULL, 0);
985 if (repo_path == NULL)
986 return got_error_from_errno();
987 obj_id_str1 = argv[0];
988 obj_id_str2 = argv[1];
989 } else if (argc == 3) {
990 repo_path = realpath(argv[0], NULL);
991 if (repo_path == NULL)
992 return got_error_from_errno();
993 obj_id_str1 = argv[1];
994 obj_id_str2 = argv[2];
995 } else
996 usage_diff();
998 error = got_repo_open(&repo, repo_path);
999 free(repo_path);
1000 if (error)
1001 goto done;
1003 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1004 if (error)
1005 goto done;
1007 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1008 if (error)
1009 goto done;
1011 error = show_diff_view(obj1, obj2, repo);
1012 done:
1013 got_repo_close(repo);
1014 if (obj1)
1015 got_object_close(obj1);
1016 if (obj2)
1017 got_object_close(obj2);
1018 return error;
1021 __dead static void
1022 usage_blame(void)
1024 endwin();
1025 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1026 getprogname());
1027 exit(1);
1030 static const struct got_error *
1031 show_blame_view(const char *path, struct got_object_id *commit_id,
1032 struct got_repository *repo)
1034 const struct got_error *err = NULL;
1035 FILE *f;
1036 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1037 int eof, i;
1039 f = got_opentemp();
1040 if (f == NULL)
1041 return got_error_from_errno();
1043 err = got_blame(path, commit_id, repo, f);
1044 if (err)
1045 goto done;
1047 fflush(f);
1049 if (tog_blame_view.window == NULL) {
1050 tog_blame_view.window = newwin(0, 0, 0, 0);
1051 if (tog_blame_view.window == NULL)
1052 return got_error_from_errno();
1053 keypad(tog_blame_view.window, TRUE);
1055 if (tog_blame_view.panel == NULL) {
1056 tog_blame_view.panel = new_panel(tog_blame_view.window);
1057 if (tog_blame_view.panel == NULL)
1058 return got_error_from_errno();
1059 } else
1060 show_panel(tog_blame_view.panel);
1062 while (!done) {
1063 err = draw_file(tog_blame_view.window, f, &first_displayed_line,
1064 &last_displayed_line, &eof, LINES);
1065 if (err)
1066 break;
1067 nodelay(stdscr, FALSE);
1068 ch = wgetch(tog_blame_view.window);
1069 nodelay(stdscr, TRUE);
1070 switch (ch) {
1071 case 'q':
1072 done = 1;
1073 break;
1074 case 'k':
1075 case KEY_UP:
1076 case KEY_BACKSPACE:
1077 if (first_displayed_line > 1)
1078 first_displayed_line--;
1079 break;
1080 case KEY_PPAGE:
1081 i = 0;
1082 while (i++ < LINES - 1 &&
1083 first_displayed_line > 1)
1084 first_displayed_line--;
1085 break;
1086 case 'j':
1087 case KEY_DOWN:
1088 case KEY_ENTER:
1089 case '\r':
1090 if (!eof)
1091 first_displayed_line++;
1092 break;
1093 case KEY_NPAGE:
1094 case ' ':
1095 i = 0;
1096 while (!eof && i++ < LINES - 1) {
1097 char *line = parse_next_line(f, NULL);
1098 first_displayed_line++;
1099 if (line == NULL)
1100 break;
1102 break;
1103 default:
1104 break;
1107 done:
1108 fclose(f);
1109 return err;
1112 static const struct got_error *
1113 cmd_blame(int argc, char *argv[])
1115 const struct got_error *error;
1116 struct got_repository *repo = NULL;
1117 char *repo_path = NULL;
1118 char *path = NULL;
1119 struct got_object_id *commit_id = NULL;
1120 char *commit_id_str = NULL;
1121 int ch;
1123 #ifndef PROFILE
1124 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1125 err(1, "pledge");
1126 #endif
1128 while ((ch = getopt(argc, argv, "c:")) != -1) {
1129 switch (ch) {
1130 case 'c':
1131 commit_id_str = optarg;
1132 break;
1133 default:
1134 usage();
1135 /* NOTREACHED */
1139 argc -= optind;
1140 argv += optind;
1142 if (argc == 0) {
1143 usage_blame();
1144 } else if (argc == 1) {
1145 repo_path = getcwd(NULL, 0);
1146 if (repo_path == NULL)
1147 return got_error_from_errno();
1148 path = argv[0];
1149 } else if (argc == 2) {
1150 repo_path = realpath(argv[0], NULL);
1151 if (repo_path == NULL)
1152 return got_error_from_errno();
1153 path = argv[1];
1154 } else
1155 usage_blame();
1157 error = got_repo_open(&repo, repo_path);
1158 free(repo_path);
1159 if (error != NULL)
1160 return error;
1162 if (commit_id_str == NULL) {
1163 struct got_reference *head_ref;
1164 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1165 if (error != NULL)
1166 goto done;
1167 error = got_ref_resolve(&commit_id, repo, head_ref);
1168 got_ref_close(head_ref);
1169 } else {
1170 struct got_object *obj;
1171 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1172 if (error != NULL)
1173 goto done;
1174 commit_id = got_object_get_id(obj);
1175 if (commit_id == NULL)
1176 error = got_error_from_errno();
1177 got_object_close(obj);
1179 if (error != NULL)
1180 goto done;
1182 error = show_blame_view(path, commit_id, repo);
1183 done:
1184 free(commit_id);
1185 if (repo)
1186 got_repo_close(repo);
1187 return error;
1190 static const struct got_error *
1191 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1192 struct got_tree_entry **last_displayed_entry,
1193 struct got_tree_entry **selected_entry, int *ndisplayed,
1194 WINDOW *window, const char *label, const struct got_tree_entries *entries,
1195 int selected, int limit, int isroot)
1197 const struct got_error *err = NULL;
1198 struct got_tree_entry *te;
1199 char *line;
1200 wchar_t *wline;
1201 int width, n;
1203 *ndisplayed = 0;
1205 werase(window);
1207 if (limit == 0)
1208 return NULL;
1210 err = format_line(&wline, &width, label, COLS);
1211 if (err)
1212 return err;
1213 waddwstr(window, wline);
1214 if (width < COLS)
1215 waddch(window, '\n');
1216 if (--limit <= 0)
1217 return NULL;
1218 waddch(window, '\n');
1219 if (--limit <= 0)
1220 return NULL;
1222 te = SIMPLEQ_FIRST(&entries->head);
1223 if (*first_displayed_entry == NULL) {
1224 if (selected == 0) {
1225 wstandout(window);
1226 *selected_entry = NULL;
1228 waddstr(window, " ..\n"); /* parent directory */
1229 if (selected == 0)
1230 wstandend(window);
1231 (*ndisplayed)++;
1232 if (--limit <= 0)
1233 return NULL;
1234 n = 1;
1235 } else {
1236 n = 0;
1237 while (te != *first_displayed_entry)
1238 te = SIMPLEQ_NEXT(te, entry);
1241 while (te) {
1242 if (asprintf(&line, " %s%s",
1243 te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1244 return got_error_from_errno();
1245 err = format_line(&wline, &width, line, COLS);
1246 if (err) {
1247 free(line);
1248 break;
1250 if (n == selected) {
1251 wstandout(window);
1252 *selected_entry = te;
1254 waddwstr(window, wline);
1255 if (width < COLS)
1256 waddch(window, '\n');
1257 if (n == selected)
1258 wstandend(window);
1259 free(line);
1260 n++;
1261 (*ndisplayed)++;
1262 *last_displayed_entry = te;
1263 if (--limit <= 0)
1264 break;
1265 te = SIMPLEQ_NEXT(te, entry);
1268 return err;
1271 static void
1272 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1273 const struct got_tree_entries *entries, int isroot)
1275 struct got_tree_entry *te, *prev;
1276 int i;
1278 if (*first_displayed_entry == NULL)
1279 return;
1281 te = SIMPLEQ_FIRST(&entries->head);
1282 if (*first_displayed_entry == te) {
1283 if (!isroot)
1284 *first_displayed_entry = NULL;
1285 return;
1288 /* XXX this is stupid... switch to TAILQ? */
1289 for (i = 0; i < maxscroll; i++) {
1290 while (te != *first_displayed_entry) {
1291 prev = te;
1292 te = SIMPLEQ_NEXT(te, entry);
1294 *first_displayed_entry = prev;
1295 te = SIMPLEQ_FIRST(&entries->head);
1297 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1298 *first_displayed_entry = NULL;
1301 static void
1302 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1303 struct got_tree_entry *last_displayed_entry,
1304 const struct got_tree_entries *entries)
1306 struct got_tree_entry *next;
1307 int n = 0;
1309 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1310 return;
1312 if (*first_displayed_entry)
1313 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1314 else
1315 next = SIMPLEQ_FIRST(&entries->head);
1316 while (next) {
1317 *first_displayed_entry = next;
1318 if (++n >= maxscroll)
1319 break;
1320 next = SIMPLEQ_NEXT(next, entry);
1324 struct tog_parent_tree {
1325 SLIST_ENTRY(tog_parent_tree) entry;
1326 struct got_tree_object *tree;
1327 struct got_tree_entry *first_displayed_entry;
1328 struct got_tree_entry *selected_entry;
1329 int selected;
1332 SLIST_HEAD(tog_parent_trees, tog_parent_tree);
1334 static const struct got_error *
1335 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1336 struct got_object_id *commit_id, struct got_repository *repo)
1338 struct tog_parent_tree *pt;
1339 char *path;
1340 size_t len = 2; /* for leading slash and NUL */
1342 SLIST_FOREACH(pt, parents, entry)
1343 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1344 len += strlen(te->name);
1345 path = calloc(1, len);
1346 if (path == NULL)
1347 return got_error_from_errno();
1349 path[0] = '/';
1350 SLIST_FOREACH(pt, parents, entry) {
1351 if (strlcat(path, pt->selected_entry->name, len) >= len)
1352 return got_error(GOT_ERR_NO_SPACE);
1353 if (strlcat(path, "/", len) >= len)
1354 return got_error(GOT_ERR_NO_SPACE);
1356 if (strlcat(path, te->name, len) >= len)
1357 return got_error(GOT_ERR_NO_SPACE);
1359 return show_blame_view(path, commit_id, repo);
1362 static const struct got_error *
1363 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1364 struct got_repository *repo)
1366 const struct got_error *err = NULL;
1367 int ch, done = 0, selected = 0;
1368 struct got_tree_object *tree = root;
1369 const struct got_tree_entries *entries;
1370 struct got_tree_entry *first_displayed_entry = NULL;
1371 struct got_tree_entry *last_displayed_entry = NULL;
1372 struct got_tree_entry *selected_entry = NULL;
1373 char *commit_id_str = NULL, *tree_label = NULL;
1374 int nentries, ndisplayed;
1375 struct tog_parent_trees parents;
1377 SLIST_INIT(&parents);
1379 err = got_object_id_str(&commit_id_str, commit_id);
1380 if (err != NULL)
1381 goto done;
1383 if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1384 err = got_error_from_errno();
1385 goto done;
1388 if (tog_tree_view.window == NULL) {
1389 tog_tree_view.window = newwin(0, 0, 0, 0);
1390 if (tog_tree_view.window == NULL)
1391 return got_error_from_errno();
1392 keypad(tog_tree_view.window, TRUE);
1394 if (tog_tree_view.panel == NULL) {
1395 tog_tree_view.panel = new_panel(tog_tree_view.window);
1396 if (tog_tree_view.panel == NULL)
1397 return got_error_from_errno();
1398 } else
1399 show_panel(tog_tree_view.panel);
1401 entries = got_object_tree_get_entries(root);
1402 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1403 while (!done) {
1404 entries = got_object_tree_get_entries(tree);
1405 nentries = entries->nentries;
1406 if (tree != root)
1407 nentries++; /* '..' directory */
1409 err = draw_tree_entries(&first_displayed_entry,
1410 &last_displayed_entry, &selected_entry, &ndisplayed,
1411 tog_tree_view.window, tree_label, entries, selected,
1412 LINES, tree == root);
1413 if (err)
1414 break;
1416 nodelay(stdscr, FALSE);
1417 ch = wgetch(tog_tree_view.window);
1418 nodelay(stdscr, TRUE);
1419 switch (ch) {
1420 case 'q':
1421 done = 1;
1422 break;
1423 case 'k':
1424 case KEY_UP:
1425 if (selected > 0)
1426 selected--;
1427 if (selected > 0)
1428 break;
1429 tree_scroll_up(&first_displayed_entry, 1,
1430 entries, tree == root);
1431 break;
1432 case KEY_PPAGE:
1433 if (SIMPLEQ_FIRST(&entries->head) ==
1434 first_displayed_entry) {
1435 selected = 0;
1436 break;
1438 tree_scroll_up(&first_displayed_entry, LINES,
1439 entries, tree == root);
1440 break;
1441 case 'j':
1442 case KEY_DOWN:
1443 if (selected < ndisplayed - 1) {
1444 selected++;
1445 break;
1447 tree_scroll_down(&first_displayed_entry, 1,
1448 last_displayed_entry, entries);
1449 break;
1450 case KEY_NPAGE:
1451 tree_scroll_down(&first_displayed_entry, LINES,
1452 last_displayed_entry, entries);
1453 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1454 break;
1455 /* can't scroll any further; move cursor down */
1456 if (selected < ndisplayed - 1)
1457 selected = ndisplayed - 1;
1458 break;
1459 case KEY_ENTER:
1460 case '\r':
1461 if (selected_entry == NULL) {
1462 struct tog_parent_tree *parent;
1463 case KEY_BACKSPACE:
1464 /* user selected '..' */
1465 if (tree == root)
1466 break;
1467 parent = SLIST_FIRST(&parents);
1468 SLIST_REMOVE_HEAD(&parents, entry);
1469 got_object_tree_close(tree);
1470 tree = parent->tree;
1471 first_displayed_entry =
1472 parent->first_displayed_entry;
1473 selected_entry = parent->selected_entry;
1474 selected = parent->selected;
1475 free(parent);
1476 } else if (S_ISDIR(selected_entry->mode)) {
1477 struct tog_parent_tree *parent;
1478 struct got_tree_object *child;
1479 err = got_object_open_as_tree(
1480 &child, repo, selected_entry->id);
1481 if (err)
1482 goto done;
1483 parent = calloc(1, sizeof(*parent));
1484 if (parent == NULL) {
1485 err = got_error_from_errno();
1486 goto done;
1488 parent->tree = tree;
1489 parent->first_displayed_entry =
1490 first_displayed_entry;
1491 parent->selected_entry = selected_entry;
1492 parent->selected = selected;
1493 SLIST_INSERT_HEAD(&parents, parent,
1494 entry);
1495 tree = child;
1496 selected = 0;
1497 first_displayed_entry = NULL;
1498 } else if (S_ISREG(selected_entry->mode)) {
1499 err = blame_tree_entry(selected_entry,
1500 &parents, commit_id, repo);
1501 if (err)
1502 goto done;
1504 break;
1505 case KEY_RESIZE:
1506 if (selected > LINES)
1507 selected = ndisplayed - 1;
1508 break;
1509 default:
1510 break;
1513 done:
1514 free(tree_label);
1515 free(commit_id_str);
1516 while (!SLIST_EMPTY(&parents)) {
1517 struct tog_parent_tree *parent;
1518 parent = SLIST_FIRST(&parents);
1519 SLIST_REMOVE_HEAD(&parents, entry);
1520 free(parent);
1523 if (tree != root)
1524 got_object_tree_close(tree);
1525 return err;
1528 __dead static void
1529 usage_tree(void)
1531 endwin();
1532 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1533 getprogname());
1534 exit(1);
1537 static const struct got_error *
1538 cmd_tree(int argc, char *argv[])
1540 const struct got_error *error;
1541 struct got_repository *repo = NULL;
1542 char *repo_path = NULL;
1543 struct got_object_id *commit_id = NULL;
1544 char *commit_id_arg = NULL;
1545 struct got_commit_object *commit = NULL;
1546 struct got_tree_object *tree = NULL;
1547 int ch;
1549 #ifndef PROFILE
1550 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1551 err(1, "pledge");
1552 #endif
1554 while ((ch = getopt(argc, argv, "c:")) != -1) {
1555 switch (ch) {
1556 case 'c':
1557 commit_id_arg = optarg;
1558 break;
1559 default:
1560 usage();
1561 /* NOTREACHED */
1565 argc -= optind;
1566 argv += optind;
1568 if (argc == 0) {
1569 repo_path = getcwd(NULL, 0);
1570 if (repo_path == NULL)
1571 return got_error_from_errno();
1572 } else if (argc == 1) {
1573 repo_path = realpath(argv[0], NULL);
1574 if (repo_path == NULL)
1575 return got_error_from_errno();
1576 } else
1577 usage_log();
1579 error = got_repo_open(&repo, repo_path);
1580 free(repo_path);
1581 if (error != NULL)
1582 return error;
1584 if (commit_id_arg == NULL) {
1585 error = get_head_commit_id(&commit_id, repo);
1586 if (error != NULL)
1587 goto done;
1588 } else {
1589 struct got_object *obj;
1590 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1591 if (error == NULL) {
1592 commit_id = got_object_get_id(obj);
1593 if (commit_id == NULL)
1594 error = got_error_from_errno();
1597 if (error != NULL)
1598 goto done;
1600 error = got_object_open_as_commit(&commit, repo, commit_id);
1601 if (error != NULL)
1602 goto done;
1604 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1605 if (error != NULL)
1606 goto done;
1608 error = show_tree_view(tree, commit_id, repo);
1609 done:
1610 free(commit_id);
1611 if (commit)
1612 got_object_commit_close(commit);
1613 if (tree)
1614 got_object_tree_close(tree);
1615 if (repo)
1616 got_repo_close(repo);
1617 return error;
1619 static void
1620 init_curses(void)
1622 initscr();
1623 cbreak();
1624 noecho();
1625 nonl();
1626 intrflush(stdscr, FALSE);
1627 keypad(stdscr, TRUE);
1628 curs_set(0);
1631 __dead static void
1632 usage(void)
1634 int i;
1636 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1637 "Available commands:\n", getprogname());
1638 for (i = 0; i < nitems(tog_commands); i++) {
1639 struct tog_cmd *cmd = &tog_commands[i];
1640 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1642 exit(1);
1645 static char **
1646 make_argv(const char *arg0, const char *arg1)
1648 char **argv;
1649 int argc = (arg1 == NULL ? 1 : 2);
1651 argv = calloc(argc, sizeof(char *));
1652 if (argv == NULL)
1653 err(1, "calloc");
1654 argv[0] = strdup(arg0);
1655 if (argv[0] == NULL)
1656 err(1, "calloc");
1657 if (arg1) {
1658 argv[1] = strdup(arg1);
1659 if (argv[1] == NULL)
1660 err(1, "calloc");
1663 return argv;
1666 int
1667 main(int argc, char *argv[])
1669 const struct got_error *error = NULL;
1670 struct tog_cmd *cmd = NULL;
1671 int ch, hflag = 0;
1672 char **cmd_argv = NULL;
1674 setlocale(LC_ALL, "");
1676 while ((ch = getopt(argc, argv, "h")) != -1) {
1677 switch (ch) {
1678 case 'h':
1679 hflag = 1;
1680 break;
1681 default:
1682 usage();
1683 /* NOTREACHED */
1687 argc -= optind;
1688 argv += optind;
1689 optind = 0;
1690 optreset = 1;
1692 if (argc == 0) {
1693 if (hflag)
1694 usage();
1695 /* Build an argument vector which runs a default command. */
1696 cmd = &tog_commands[0];
1697 cmd_argv = make_argv(cmd->name, NULL);
1698 argc = 1;
1699 } else {
1700 int i;
1702 /* Did the user specific a command? */
1703 for (i = 0; i < nitems(tog_commands); i++) {
1704 if (strncmp(tog_commands[i].name, argv[0],
1705 strlen(argv[0])) == 0) {
1706 cmd = &tog_commands[i];
1707 if (hflag)
1708 tog_commands[i].cmd_usage();
1709 break;
1712 if (cmd == NULL) {
1713 /* Did the user specify a repository? */
1714 char *repo_path = realpath(argv[0], NULL);
1715 if (repo_path) {
1716 struct got_repository *repo;
1717 error = got_repo_open(&repo, repo_path);
1718 if (error == NULL)
1719 got_repo_close(repo);
1720 } else
1721 error = got_error_from_errno();
1722 if (error) {
1723 if (hflag) {
1724 fprintf(stderr, "%s: '%s' is not a "
1725 "known command\n", getprogname(),
1726 argv[0]);
1727 usage();
1729 fprintf(stderr, "%s: '%s' is neither a known "
1730 "command nor a path to a repository\n",
1731 getprogname(), argv[0]);
1732 free(repo_path);
1733 return 1;
1735 cmd = &tog_commands[0];
1736 cmd_argv = make_argv(cmd->name, repo_path);
1737 argc = 2;
1738 free(repo_path);
1742 init_curses();
1744 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1745 if (error)
1746 goto done;
1747 done:
1748 endwin();
1749 free(cmd_argv);
1750 if (error)
1751 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1752 return 0;