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 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
574 const struct got_error *err = NULL;
575 struct got_tree_object *tree;
577 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
578 if (err)
579 return err;
581 err = show_tree_view(tree, entry->id, repo);
582 got_object_tree_close(tree);
583 return err;
586 static const struct got_error *
587 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
589 const struct got_error *err = NULL;
590 struct got_object_id *head_id = NULL;
591 int ch, done = 0, selected = 0, nparents, nfetched;
592 struct got_commit_graph *graph;
593 struct commit_queue commits;
594 struct commit_queue_entry *entry = NULL;
595 struct commit_queue_entry *first_displayed_entry = NULL;
596 struct commit_queue_entry *last_displayed_entry = NULL;
597 struct commit_queue_entry *selected_entry = NULL;
599 if (tog_log_view.window == NULL) {
600 tog_log_view.window = newwin(0, 0, 0, 0);
601 if (tog_log_view.window == NULL)
602 return got_error_from_errno();
603 keypad(tog_log_view.window, TRUE);
605 if (tog_log_view.panel == NULL) {
606 tog_log_view.panel = new_panel(tog_log_view.window);
607 if (tog_log_view.panel == NULL)
608 return got_error_from_errno();
609 } else
610 show_panel(tog_log_view.panel);
612 err = get_head_commit_id(&head_id, repo);
613 if (err)
614 return err;
616 TAILQ_INIT(&commits);
618 err = got_commit_graph_open(&graph, head_id, 0, repo);
619 if (err)
620 goto done;
622 /* Populate commit graph with a sufficient number of commits. */
623 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
624 repo);
625 if (err)
626 goto done;
627 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
628 if (err)
629 goto done;
631 /*
632 * Open the initial batch of commits, sorted in commit graph order.
633 * We keep all commits open throughout the lifetime of the log view
634 * in order to avoid having to re-fetch commits from disk while
635 * updating the display.
636 */
637 err = queue_commits(graph, &commits, head_id, repo);
638 if (err && err->code != GOT_ERR_ITER_COMPLETED)
639 goto done;
641 /* Find entry corresponding to the first commit to display. */
642 TAILQ_FOREACH(entry, &commits, entry) {
643 if (got_object_id_cmp(entry->id, start_id) == 0) {
644 first_displayed_entry = entry;
645 break;
648 if (first_displayed_entry == NULL) {
649 err = got_error(GOT_ERR_NO_OBJ);
650 goto done;
653 while (!done) {
654 err = draw_commits(&last_displayed_entry, &selected_entry,
655 first_displayed_entry, selected, LINES);
656 if (err)
657 goto done;
659 nodelay(stdscr, FALSE);
660 ch = wgetch(tog_log_view.window);
661 nodelay(stdscr, TRUE);
662 switch (ch) {
663 case ERR:
664 if (errno) {
665 err = got_error_from_errno();
666 goto done;
668 break;
669 case 'q':
670 done = 1;
671 break;
672 case 'k':
673 case KEY_UP:
674 if (selected > 0)
675 selected--;
676 if (selected > 0)
677 break;
678 scroll_up(&first_displayed_entry, 1, &commits);
679 break;
680 case KEY_PPAGE:
681 if (TAILQ_FIRST(&commits) ==
682 first_displayed_entry) {
683 selected = 0;
684 break;
686 scroll_up(&first_displayed_entry, LINES,
687 &commits);
688 break;
689 case 'j':
690 case KEY_DOWN:
691 nparents = num_parents(first_displayed_entry);
692 if (selected < LINES - 1 &&
693 selected < nparents - 1) {
694 selected++;
695 break;
697 err = scroll_down(&first_displayed_entry, 1,
698 last_displayed_entry, &commits, graph,
699 repo);
700 if (err)
701 goto done;
702 break;
703 case KEY_NPAGE:
704 err = scroll_down(&first_displayed_entry, LINES,
705 last_displayed_entry, &commits, graph,
706 repo);
707 if (err)
708 goto done;
709 if (last_displayed_entry->commit->nparents > 0)
710 break;
711 /* can't scroll any further; move cursor down */
712 nparents = num_parents(first_displayed_entry);
713 if (selected < LINES - 1 ||
714 selected < nparents - 1)
715 selected = MIN(LINES - 1, nparents - 1);
716 break;
717 case KEY_RESIZE:
718 if (selected > LINES)
719 selected = LINES - 1;
720 break;
721 case KEY_ENTER:
722 case '\r':
723 err = show_commit(selected_entry, repo);
724 if (err)
725 goto done;
726 show_panel(tog_log_view.panel);
727 break;
728 case 't':
729 err = browse_commit(selected_entry, repo);
730 if (err)
731 goto done;
732 show_panel(tog_log_view.panel);
733 break;
734 default:
735 break;
738 done:
739 free(head_id);
740 if (graph)
741 got_commit_graph_close(graph);
742 free_commits(&commits);
743 return err;
746 static const struct got_error *
747 cmd_log(int argc, char *argv[])
749 const struct got_error *error;
750 struct got_repository *repo;
751 struct got_object_id *start_id = NULL;
752 char *repo_path = NULL;
753 char *start_commit = NULL;
754 int ch;
756 #ifndef PROFILE
757 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
758 err(1, "pledge");
759 #endif
761 while ((ch = getopt(argc, argv, "c:")) != -1) {
762 switch (ch) {
763 case 'c':
764 start_commit = optarg;
765 break;
766 default:
767 usage();
768 /* NOTREACHED */
772 argc -= optind;
773 argv += optind;
775 if (argc == 0) {
776 repo_path = getcwd(NULL, 0);
777 if (repo_path == NULL)
778 return got_error_from_errno();
779 } else if (argc == 1) {
780 repo_path = realpath(argv[0], NULL);
781 if (repo_path == NULL)
782 return got_error_from_errno();
783 } else
784 usage_log();
786 error = got_repo_open(&repo, repo_path);
787 free(repo_path);
788 if (error != NULL)
789 return error;
791 if (start_commit == NULL) {
792 error = get_head_commit_id(&start_id, repo);
793 if (error != NULL)
794 return error;
795 } else {
796 struct got_object *obj;
797 error = got_object_open_by_id_str(&obj, repo, start_commit);
798 if (error == NULL) {
799 start_id = got_object_get_id(obj);
800 if (start_id == NULL)
801 error = got_error_from_errno();
804 if (error != NULL)
805 return error;
806 error = show_log_view(start_id, repo);
807 free(start_id);
808 got_repo_close(repo);
809 return error;
812 __dead static void
813 usage_diff(void)
815 endwin();
816 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
817 getprogname());
818 exit(1);
821 static char *
822 parse_next_line(FILE *f, size_t *len)
824 char *line;
825 size_t linelen;
826 size_t lineno;
827 const char delim[3] = { '\0', '\0', '\0'};
829 line = fparseln(f, &linelen, &lineno, delim, 0);
830 if (len)
831 *len = linelen;
832 return line;
835 static const struct got_error *
836 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
837 int *last_displayed_line, int *eof, int max_lines)
839 const struct got_error *err;
840 int nlines = 0, nprinted = 0;
841 char *line;
842 size_t len;
843 wchar_t *wline;
844 int width;
846 rewind(f);
847 werase(window);
849 *eof = 0;
850 while (nprinted < max_lines) {
851 line = parse_next_line(f, &len);
852 if (line == NULL) {
853 *eof = 1;
854 break;
856 if (++nlines < *first_displayed_line) {
857 free(line);
858 continue;
861 err = format_line(&wline, &width, line, COLS);
862 if (err) {
863 free(line);
864 return err;
866 waddwstr(window, wline);
867 if (width < COLS)
868 waddch(window, '\n');
869 if (++nprinted == 1)
870 *first_displayed_line = nlines;
871 free(line);
873 *last_displayed_line = nlines;
875 update_panels();
876 doupdate();
878 return NULL;
881 static const struct got_error *
882 show_diff_view(struct got_object *obj1, struct got_object *obj2,
883 struct got_repository *repo)
885 const struct got_error *err;
886 FILE *f;
887 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
888 int eof, i;
890 if (obj1 != NULL && obj2 != NULL &&
891 got_object_get_type(obj1) != got_object_get_type(obj2))
892 return got_error(GOT_ERR_OBJ_TYPE);
894 f = got_opentemp();
895 if (f == NULL)
896 return got_error_from_errno();
898 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
899 case GOT_OBJ_TYPE_BLOB:
900 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
901 break;
902 case GOT_OBJ_TYPE_TREE:
903 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
904 break;
905 case GOT_OBJ_TYPE_COMMIT:
906 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
907 break;
908 default:
909 return got_error(GOT_ERR_OBJ_TYPE);
912 fflush(f);
914 if (tog_diff_view.window == NULL) {
915 tog_diff_view.window = newwin(0, 0, 0, 0);
916 if (tog_diff_view.window == NULL)
917 return got_error_from_errno();
918 keypad(tog_diff_view.window, TRUE);
920 if (tog_diff_view.panel == NULL) {
921 tog_diff_view.panel = new_panel(tog_diff_view.window);
922 if (tog_diff_view.panel == NULL)
923 return got_error_from_errno();
924 } else
925 show_panel(tog_diff_view.panel);
927 while (!done) {
928 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
929 &last_displayed_line, &eof, LINES);
930 if (err)
931 break;
932 nodelay(stdscr, FALSE);
933 ch = wgetch(tog_diff_view.window);
934 nodelay(stdscr, TRUE);
935 switch (ch) {
936 case 'q':
937 done = 1;
938 break;
939 case 'k':
940 case KEY_UP:
941 case KEY_BACKSPACE:
942 if (first_displayed_line > 1)
943 first_displayed_line--;
944 break;
945 case KEY_PPAGE:
946 i = 0;
947 while (i++ < LINES - 1 &&
948 first_displayed_line > 1)
949 first_displayed_line--;
950 break;
951 case 'j':
952 case KEY_DOWN:
953 case KEY_ENTER:
954 case '\r':
955 if (!eof)
956 first_displayed_line++;
957 break;
958 case KEY_NPAGE:
959 case ' ':
960 i = 0;
961 while (!eof && i++ < LINES - 1) {
962 char *line = parse_next_line(f, NULL);
963 first_displayed_line++;
964 if (line == NULL)
965 break;
967 break;
968 default:
969 break;
972 fclose(f);
973 return err;
976 static const struct got_error *
977 cmd_diff(int argc, char *argv[])
979 const struct got_error *error = NULL;
980 struct got_repository *repo = NULL;
981 struct got_object *obj1 = NULL, *obj2 = NULL;
982 char *repo_path = NULL;
983 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
984 int ch;
986 #ifndef PROFILE
987 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
988 err(1, "pledge");
989 #endif
991 while ((ch = getopt(argc, argv, "")) != -1) {
992 switch (ch) {
993 default:
994 usage();
995 /* NOTREACHED */
999 argc -= optind;
1000 argv += optind;
1002 if (argc == 0) {
1003 usage_diff(); /* TODO show local worktree changes */
1004 } else if (argc == 2) {
1005 repo_path = getcwd(NULL, 0);
1006 if (repo_path == NULL)
1007 return got_error_from_errno();
1008 obj_id_str1 = argv[0];
1009 obj_id_str2 = argv[1];
1010 } else if (argc == 3) {
1011 repo_path = realpath(argv[0], NULL);
1012 if (repo_path == NULL)
1013 return got_error_from_errno();
1014 obj_id_str1 = argv[1];
1015 obj_id_str2 = argv[2];
1016 } else
1017 usage_diff();
1019 error = got_repo_open(&repo, repo_path);
1020 free(repo_path);
1021 if (error)
1022 goto done;
1024 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1025 if (error)
1026 goto done;
1028 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1029 if (error)
1030 goto done;
1032 error = show_diff_view(obj1, obj2, repo);
1033 done:
1034 got_repo_close(repo);
1035 if (obj1)
1036 got_object_close(obj1);
1037 if (obj2)
1038 got_object_close(obj2);
1039 return error;
1042 __dead static void
1043 usage_blame(void)
1045 endwin();
1046 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1047 getprogname());
1048 exit(1);
1051 static const struct got_error *
1052 show_blame_view(const char *path, struct got_object_id *commit_id,
1053 struct got_repository *repo)
1055 const struct got_error *err = NULL;
1056 FILE *f;
1057 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1058 int eof, i;
1060 f = got_opentemp();
1061 if (f == NULL)
1062 return got_error_from_errno();
1064 err = got_blame(path, commit_id, repo, f);
1065 if (err)
1066 goto done;
1068 fflush(f);
1070 if (tog_blame_view.window == NULL) {
1071 tog_blame_view.window = newwin(0, 0, 0, 0);
1072 if (tog_blame_view.window == NULL)
1073 return got_error_from_errno();
1074 keypad(tog_blame_view.window, TRUE);
1076 if (tog_blame_view.panel == NULL) {
1077 tog_blame_view.panel = new_panel(tog_blame_view.window);
1078 if (tog_blame_view.panel == NULL)
1079 return got_error_from_errno();
1080 } else
1081 show_panel(tog_blame_view.panel);
1083 while (!done) {
1084 err = draw_file(tog_blame_view.window, f, &first_displayed_line,
1085 &last_displayed_line, &eof, LINES);
1086 if (err)
1087 break;
1088 nodelay(stdscr, FALSE);
1089 ch = wgetch(tog_blame_view.window);
1090 nodelay(stdscr, TRUE);
1091 switch (ch) {
1092 case 'q':
1093 done = 1;
1094 break;
1095 case 'k':
1096 case KEY_UP:
1097 case KEY_BACKSPACE:
1098 if (first_displayed_line > 1)
1099 first_displayed_line--;
1100 break;
1101 case KEY_PPAGE:
1102 i = 0;
1103 while (i++ < LINES - 1 &&
1104 first_displayed_line > 1)
1105 first_displayed_line--;
1106 break;
1107 case 'j':
1108 case KEY_DOWN:
1109 case KEY_ENTER:
1110 case '\r':
1111 if (!eof)
1112 first_displayed_line++;
1113 break;
1114 case KEY_NPAGE:
1115 case ' ':
1116 i = 0;
1117 while (!eof && i++ < LINES - 1) {
1118 char *line = parse_next_line(f, NULL);
1119 first_displayed_line++;
1120 if (line == NULL)
1121 break;
1123 break;
1124 default:
1125 break;
1128 done:
1129 fclose(f);
1130 return err;
1133 static const struct got_error *
1134 cmd_blame(int argc, char *argv[])
1136 const struct got_error *error;
1137 struct got_repository *repo = NULL;
1138 char *repo_path = NULL;
1139 char *path = NULL;
1140 struct got_object_id *commit_id = NULL;
1141 char *commit_id_str = NULL;
1142 int ch;
1144 #ifndef PROFILE
1145 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1146 err(1, "pledge");
1147 #endif
1149 while ((ch = getopt(argc, argv, "c:")) != -1) {
1150 switch (ch) {
1151 case 'c':
1152 commit_id_str = optarg;
1153 break;
1154 default:
1155 usage();
1156 /* NOTREACHED */
1160 argc -= optind;
1161 argv += optind;
1163 if (argc == 0) {
1164 usage_blame();
1165 } else if (argc == 1) {
1166 repo_path = getcwd(NULL, 0);
1167 if (repo_path == NULL)
1168 return got_error_from_errno();
1169 path = argv[0];
1170 } else if (argc == 2) {
1171 repo_path = realpath(argv[0], NULL);
1172 if (repo_path == NULL)
1173 return got_error_from_errno();
1174 path = argv[1];
1175 } else
1176 usage_blame();
1178 error = got_repo_open(&repo, repo_path);
1179 free(repo_path);
1180 if (error != NULL)
1181 return error;
1183 if (commit_id_str == NULL) {
1184 struct got_reference *head_ref;
1185 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1186 if (error != NULL)
1187 goto done;
1188 error = got_ref_resolve(&commit_id, repo, head_ref);
1189 got_ref_close(head_ref);
1190 } else {
1191 struct got_object *obj;
1192 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1193 if (error != NULL)
1194 goto done;
1195 commit_id = got_object_get_id(obj);
1196 if (commit_id == NULL)
1197 error = got_error_from_errno();
1198 got_object_close(obj);
1200 if (error != NULL)
1201 goto done;
1203 error = show_blame_view(path, commit_id, repo);
1204 done:
1205 free(commit_id);
1206 if (repo)
1207 got_repo_close(repo);
1208 return error;
1211 static const struct got_error *
1212 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1213 struct got_tree_entry **last_displayed_entry,
1214 struct got_tree_entry **selected_entry, int *ndisplayed,
1215 WINDOW *window, const char *label, const char *parent_path,
1216 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1218 const struct got_error *err = NULL;
1219 struct got_tree_entry *te;
1220 wchar_t *wline;
1221 int width, n;
1223 *ndisplayed = 0;
1225 werase(window);
1227 if (limit == 0)
1228 return NULL;
1230 err = format_line(&wline, &width, label, COLS);
1231 if (err)
1232 return err;
1233 waddwstr(window, wline);
1234 if (width < COLS)
1235 waddch(window, '\n');
1236 if (--limit <= 0)
1237 return NULL;
1238 err = format_line(&wline, &width, parent_path, COLS);
1239 if (err)
1240 return err;
1241 waddwstr(window, wline);
1242 if (width < COLS)
1243 waddch(window, '\n');
1244 if (--limit <= 0)
1245 return NULL;
1246 waddch(window, '\n');
1247 if (--limit <= 0)
1248 return NULL;
1250 te = SIMPLEQ_FIRST(&entries->head);
1251 if (*first_displayed_entry == NULL) {
1252 if (selected == 0) {
1253 wstandout(window);
1254 *selected_entry = NULL;
1256 waddstr(window, " ..\n"); /* parent directory */
1257 if (selected == 0)
1258 wstandend(window);
1259 (*ndisplayed)++;
1260 if (--limit <= 0)
1261 return NULL;
1262 n = 1;
1263 } else {
1264 n = 0;
1265 while (te != *first_displayed_entry)
1266 te = SIMPLEQ_NEXT(te, entry);
1269 while (te) {
1270 char *line = NULL;
1271 if (asprintf(&line, " %s%s",
1272 te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1273 return got_error_from_errno();
1274 err = format_line(&wline, &width, line, COLS);
1275 if (err) {
1276 free(line);
1277 break;
1279 if (n == selected) {
1280 wstandout(window);
1281 *selected_entry = te;
1283 waddwstr(window, wline);
1284 if (width < COLS)
1285 waddch(window, '\n');
1286 if (n == selected)
1287 wstandend(window);
1288 free(line);
1289 n++;
1290 (*ndisplayed)++;
1291 *last_displayed_entry = te;
1292 if (--limit <= 0)
1293 break;
1294 te = SIMPLEQ_NEXT(te, entry);
1297 return err;
1300 static void
1301 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1302 const struct got_tree_entries *entries, int isroot)
1304 struct got_tree_entry *te, *prev;
1305 int i;
1307 if (*first_displayed_entry == NULL)
1308 return;
1310 te = SIMPLEQ_FIRST(&entries->head);
1311 if (*first_displayed_entry == te) {
1312 if (!isroot)
1313 *first_displayed_entry = NULL;
1314 return;
1317 /* XXX this is stupid... switch to TAILQ? */
1318 for (i = 0; i < maxscroll; i++) {
1319 while (te != *first_displayed_entry) {
1320 prev = te;
1321 te = SIMPLEQ_NEXT(te, entry);
1323 *first_displayed_entry = prev;
1324 te = SIMPLEQ_FIRST(&entries->head);
1326 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1327 *first_displayed_entry = NULL;
1330 static void
1331 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1332 struct got_tree_entry *last_displayed_entry,
1333 const struct got_tree_entries *entries)
1335 struct got_tree_entry *next;
1336 int n = 0;
1338 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1339 return;
1341 if (*first_displayed_entry)
1342 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1343 else
1344 next = SIMPLEQ_FIRST(&entries->head);
1345 while (next) {
1346 *first_displayed_entry = next;
1347 if (++n >= maxscroll)
1348 break;
1349 next = SIMPLEQ_NEXT(next, entry);
1353 struct tog_parent_tree {
1354 TAILQ_ENTRY(tog_parent_tree) entry;
1355 struct got_tree_object *tree;
1356 struct got_tree_entry *first_displayed_entry;
1357 struct got_tree_entry *selected_entry;
1358 int selected;
1361 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1363 static const struct got_error *
1364 tree_entry_path(char **path, struct tog_parent_trees *parents,
1365 struct got_tree_entry *te)
1367 const struct got_error *err = NULL;
1368 struct tog_parent_tree *pt;
1369 size_t len = 2; /* for leading slash and NUL */
1371 TAILQ_FOREACH(pt, parents, entry)
1372 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1373 if (te)
1374 len += strlen(te->name);
1376 *path = calloc(1, len);
1377 if (path == NULL)
1378 return got_error_from_errno();
1380 (*path)[0] = '/';
1381 pt = TAILQ_LAST(parents, tog_parent_trees);
1382 while (pt) {
1383 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1384 err = got_error(GOT_ERR_NO_SPACE);
1385 goto done;
1387 if (strlcat(*path, "/", len) >= len) {
1388 err = got_error(GOT_ERR_NO_SPACE);
1389 goto done;
1391 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1393 if (te) {
1394 if (strlcat(*path, te->name, len) >= len) {
1395 err = got_error(GOT_ERR_NO_SPACE);
1396 goto done;
1399 done:
1400 if (err) {
1401 free(*path);
1402 *path = NULL;
1404 return err;
1407 static const struct got_error *
1408 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1409 struct got_object_id *commit_id, struct got_repository *repo)
1411 const struct got_error *err = NULL;
1412 char *path;
1414 err = tree_entry_path(&path, parents, te);
1415 if (err)
1416 return err;
1418 err = show_blame_view(path, commit_id, repo);
1419 free(path);
1420 return err;
1423 static const struct got_error *
1424 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1425 struct got_repository *repo)
1427 const struct got_error *err = NULL;
1428 int ch, done = 0, selected = 0;
1429 struct got_tree_object *tree = root;
1430 const struct got_tree_entries *entries;
1431 struct got_tree_entry *first_displayed_entry = NULL;
1432 struct got_tree_entry *last_displayed_entry = NULL;
1433 struct got_tree_entry *selected_entry = NULL;
1434 char *commit_id_str = NULL, *tree_label = NULL;
1435 int nentries, ndisplayed;
1436 struct tog_parent_trees parents;
1438 TAILQ_INIT(&parents);
1440 err = got_object_id_str(&commit_id_str, commit_id);
1441 if (err != NULL)
1442 goto done;
1444 if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1445 err = got_error_from_errno();
1446 goto done;
1449 if (tog_tree_view.window == NULL) {
1450 tog_tree_view.window = newwin(0, 0, 0, 0);
1451 if (tog_tree_view.window == NULL)
1452 return got_error_from_errno();
1453 keypad(tog_tree_view.window, TRUE);
1455 if (tog_tree_view.panel == NULL) {
1456 tog_tree_view.panel = new_panel(tog_tree_view.window);
1457 if (tog_tree_view.panel == NULL)
1458 return got_error_from_errno();
1459 } else
1460 show_panel(tog_tree_view.panel);
1462 entries = got_object_tree_get_entries(root);
1463 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1464 while (!done) {
1465 char *parent_path;
1466 entries = got_object_tree_get_entries(tree);
1467 nentries = entries->nentries;
1468 if (tree != root)
1469 nentries++; /* '..' directory */
1471 err = tree_entry_path(&parent_path, &parents, NULL);
1472 if (err)
1473 goto done;
1475 err = draw_tree_entries(&first_displayed_entry,
1476 &last_displayed_entry, &selected_entry, &ndisplayed,
1477 tog_tree_view.window, tree_label, parent_path, entries,
1478 selected, LINES, tree == root);
1479 free(parent_path);
1480 if (err)
1481 break;
1483 nodelay(stdscr, FALSE);
1484 ch = wgetch(tog_tree_view.window);
1485 nodelay(stdscr, TRUE);
1486 switch (ch) {
1487 case 'q':
1488 done = 1;
1489 break;
1490 case 'k':
1491 case KEY_UP:
1492 if (selected > 0)
1493 selected--;
1494 if (selected > 0)
1495 break;
1496 tree_scroll_up(&first_displayed_entry, 1,
1497 entries, tree == root);
1498 break;
1499 case KEY_PPAGE:
1500 if (SIMPLEQ_FIRST(&entries->head) ==
1501 first_displayed_entry) {
1502 if (tree != root)
1503 first_displayed_entry = NULL;
1504 selected = 0;
1505 break;
1507 tree_scroll_up(&first_displayed_entry, LINES,
1508 entries, tree == root);
1509 break;
1510 case 'j':
1511 case KEY_DOWN:
1512 if (selected < ndisplayed - 1) {
1513 selected++;
1514 break;
1516 tree_scroll_down(&first_displayed_entry, 1,
1517 last_displayed_entry, entries);
1518 break;
1519 case KEY_NPAGE:
1520 tree_scroll_down(&first_displayed_entry, LINES,
1521 last_displayed_entry, entries);
1522 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1523 break;
1524 /* can't scroll any further; move cursor down */
1525 if (selected < ndisplayed - 1)
1526 selected = ndisplayed - 1;
1527 break;
1528 case KEY_ENTER:
1529 case '\r':
1530 if (selected_entry == NULL) {
1531 struct tog_parent_tree *parent;
1532 case KEY_BACKSPACE:
1533 /* user selected '..' */
1534 if (tree == root)
1535 break;
1536 parent = TAILQ_FIRST(&parents);
1537 TAILQ_REMOVE(&parents, parent, entry);
1538 got_object_tree_close(tree);
1539 tree = parent->tree;
1540 first_displayed_entry =
1541 parent->first_displayed_entry;
1542 selected_entry = parent->selected_entry;
1543 selected = parent->selected;
1544 free(parent);
1545 } else if (S_ISDIR(selected_entry->mode)) {
1546 struct tog_parent_tree *parent;
1547 struct got_tree_object *child;
1548 err = got_object_open_as_tree(
1549 &child, repo, selected_entry->id);
1550 if (err)
1551 goto done;
1552 parent = calloc(1, sizeof(*parent));
1553 if (parent == NULL) {
1554 err = got_error_from_errno();
1555 goto done;
1557 parent->tree = tree;
1558 parent->first_displayed_entry =
1559 first_displayed_entry;
1560 parent->selected_entry = selected_entry;
1561 parent->selected = selected;
1562 TAILQ_INSERT_HEAD(&parents, parent,
1563 entry);
1564 tree = child;
1565 selected = 0;
1566 first_displayed_entry = NULL;
1567 } else if (S_ISREG(selected_entry->mode)) {
1568 err = blame_tree_entry(selected_entry,
1569 &parents, commit_id, repo);
1570 if (err)
1571 goto done;
1573 break;
1574 case KEY_RESIZE:
1575 if (selected > LINES)
1576 selected = ndisplayed - 1;
1577 break;
1578 default:
1579 break;
1582 done:
1583 free(tree_label);
1584 free(commit_id_str);
1585 while (!TAILQ_EMPTY(&parents)) {
1586 struct tog_parent_tree *parent;
1587 parent = TAILQ_FIRST(&parents);
1588 TAILQ_REMOVE(&parents, parent, entry);
1589 free(parent);
1592 if (tree != root)
1593 got_object_tree_close(tree);
1594 return err;
1597 __dead static void
1598 usage_tree(void)
1600 endwin();
1601 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1602 getprogname());
1603 exit(1);
1606 static const struct got_error *
1607 cmd_tree(int argc, char *argv[])
1609 const struct got_error *error;
1610 struct got_repository *repo = NULL;
1611 char *repo_path = NULL;
1612 struct got_object_id *commit_id = NULL;
1613 char *commit_id_arg = NULL;
1614 struct got_commit_object *commit = NULL;
1615 struct got_tree_object *tree = NULL;
1616 int ch;
1618 #ifndef PROFILE
1619 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1620 err(1, "pledge");
1621 #endif
1623 while ((ch = getopt(argc, argv, "c:")) != -1) {
1624 switch (ch) {
1625 case 'c':
1626 commit_id_arg = optarg;
1627 break;
1628 default:
1629 usage();
1630 /* NOTREACHED */
1634 argc -= optind;
1635 argv += optind;
1637 if (argc == 0) {
1638 repo_path = getcwd(NULL, 0);
1639 if (repo_path == NULL)
1640 return got_error_from_errno();
1641 } else if (argc == 1) {
1642 repo_path = realpath(argv[0], NULL);
1643 if (repo_path == NULL)
1644 return got_error_from_errno();
1645 } else
1646 usage_log();
1648 error = got_repo_open(&repo, repo_path);
1649 free(repo_path);
1650 if (error != NULL)
1651 return error;
1653 if (commit_id_arg == NULL) {
1654 error = get_head_commit_id(&commit_id, repo);
1655 if (error != NULL)
1656 goto done;
1657 } else {
1658 struct got_object *obj;
1659 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1660 if (error == NULL) {
1661 commit_id = got_object_get_id(obj);
1662 if (commit_id == NULL)
1663 error = got_error_from_errno();
1666 if (error != NULL)
1667 goto done;
1669 error = got_object_open_as_commit(&commit, repo, commit_id);
1670 if (error != NULL)
1671 goto done;
1673 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1674 if (error != NULL)
1675 goto done;
1677 error = show_tree_view(tree, commit_id, repo);
1678 done:
1679 free(commit_id);
1680 if (commit)
1681 got_object_commit_close(commit);
1682 if (tree)
1683 got_object_tree_close(tree);
1684 if (repo)
1685 got_repo_close(repo);
1686 return error;
1688 static void
1689 init_curses(void)
1691 initscr();
1692 cbreak();
1693 noecho();
1694 nonl();
1695 intrflush(stdscr, FALSE);
1696 keypad(stdscr, TRUE);
1697 curs_set(0);
1700 __dead static void
1701 usage(void)
1703 int i;
1705 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1706 "Available commands:\n", getprogname());
1707 for (i = 0; i < nitems(tog_commands); i++) {
1708 struct tog_cmd *cmd = &tog_commands[i];
1709 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1711 exit(1);
1714 static char **
1715 make_argv(const char *arg0, const char *arg1)
1717 char **argv;
1718 int argc = (arg1 == NULL ? 1 : 2);
1720 argv = calloc(argc, sizeof(char *));
1721 if (argv == NULL)
1722 err(1, "calloc");
1723 argv[0] = strdup(arg0);
1724 if (argv[0] == NULL)
1725 err(1, "calloc");
1726 if (arg1) {
1727 argv[1] = strdup(arg1);
1728 if (argv[1] == NULL)
1729 err(1, "calloc");
1732 return argv;
1735 int
1736 main(int argc, char *argv[])
1738 const struct got_error *error = NULL;
1739 struct tog_cmd *cmd = NULL;
1740 int ch, hflag = 0;
1741 char **cmd_argv = NULL;
1743 setlocale(LC_ALL, "");
1745 while ((ch = getopt(argc, argv, "h")) != -1) {
1746 switch (ch) {
1747 case 'h':
1748 hflag = 1;
1749 break;
1750 default:
1751 usage();
1752 /* NOTREACHED */
1756 argc -= optind;
1757 argv += optind;
1758 optind = 0;
1759 optreset = 1;
1761 if (argc == 0) {
1762 if (hflag)
1763 usage();
1764 /* Build an argument vector which runs a default command. */
1765 cmd = &tog_commands[0];
1766 cmd_argv = make_argv(cmd->name, NULL);
1767 argc = 1;
1768 } else {
1769 int i;
1771 /* Did the user specific a command? */
1772 for (i = 0; i < nitems(tog_commands); i++) {
1773 if (strncmp(tog_commands[i].name, argv[0],
1774 strlen(argv[0])) == 0) {
1775 cmd = &tog_commands[i];
1776 if (hflag)
1777 tog_commands[i].cmd_usage();
1778 break;
1781 if (cmd == NULL) {
1782 /* Did the user specify a repository? */
1783 char *repo_path = realpath(argv[0], NULL);
1784 if (repo_path) {
1785 struct got_repository *repo;
1786 error = got_repo_open(&repo, repo_path);
1787 if (error == NULL)
1788 got_repo_close(repo);
1789 } else
1790 error = got_error_from_errno();
1791 if (error) {
1792 if (hflag) {
1793 fprintf(stderr, "%s: '%s' is not a "
1794 "known command\n", getprogname(),
1795 argv[0]);
1796 usage();
1798 fprintf(stderr, "%s: '%s' is neither a known "
1799 "command nor a path to a repository\n",
1800 getprogname(), argv[0]);
1801 free(repo_path);
1802 return 1;
1804 cmd = &tog_commands[0];
1805 cmd_argv = make_argv(cmd->name, repo_path);
1806 argc = 2;
1807 free(repo_path);
1811 init_curses();
1813 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
1814 if (error)
1815 goto done;
1816 done:
1817 endwin();
1818 free(cmd_argv);
1819 if (error)
1820 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
1821 return 0;