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 struct tog_view {
86 WINDOW *window;
87 PANEL *panel;
88 int nlines, ncols, begin_y, begin_x;
89 int lines, cols; /* copies of LINES and COLS */
90 };
92 static const struct got_error *
93 show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
94 struct got_repository *);
95 static const struct got_error *
96 show_log_view(struct got_object_id *, struct got_repository *, const char *);
97 static const struct got_error *
98 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
99 static const struct got_error *
100 show_tree_view(struct got_tree_object *, struct got_object_id *,
101 struct got_repository *);
103 static void
104 close_view(struct tog_view *view)
106 if (view->panel)
107 del_panel(view->panel);
108 if (view->window)
109 delwin(view->window);
110 free(view);
113 static struct tog_view *
114 open_view(int nlines, int ncols, int begin_y, int begin_x)
116 struct tog_view *view = malloc(sizeof(*view));
118 if (view == NULL)
119 return NULL;
121 view->lines = LINES;
122 view->cols = COLS;
123 view->nlines = nlines ? nlines : LINES - begin_y;
124 view->ncols = ncols ? ncols : COLS - begin_x;
125 view->begin_y = begin_y;
126 view->begin_x = begin_x;
127 view->window = newwin(nlines, ncols, begin_y, begin_x);
128 if (view->window == NULL) {
129 close_view(view);
130 return NULL;
132 view->panel = new_panel(view->window);
133 if (view->panel == NULL) {
134 close_view(view);
135 return NULL;
138 keypad(view->window, TRUE);
139 return view;
142 const struct got_error *
143 view_resize(struct tog_view *view)
145 int nlines, ncols;
147 if (view->lines > LINES)
148 nlines = view->nlines - (view->lines - LINES);
149 else
150 nlines = view->nlines + (LINES - view->lines);
152 if (view->cols > COLS)
153 ncols = view->ncols - (view->cols - COLS);
154 else
155 ncols = view->ncols + (COLS - view->cols);
157 if (wresize(view->window, nlines, ncols) == ERR)
158 return got_error_from_errno();
160 view->nlines = nlines;
161 view->ncols = ncols;
162 view->lines = LINES;
163 view->cols = COLS;
164 return NULL;
167 __dead static void
168 usage_log(void)
170 endwin();
171 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
172 getprogname());
173 exit(1);
176 /* Create newly allocated wide-character string equivalent to a byte string. */
177 static const struct got_error *
178 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
180 char *vis = NULL;
181 const struct got_error *err = NULL;
183 *ws = NULL;
184 *wlen = mbstowcs(NULL, s, 0);
185 if (*wlen == (size_t)-1) {
186 int vislen;
187 if (errno != EILSEQ)
188 return got_error_from_errno();
190 /* byte string invalid in current encoding; try to "fix" it */
191 err = got_mbsavis(&vis, &vislen, s);
192 if (err)
193 return err;
194 *wlen = mbstowcs(NULL, vis, 0);
195 if (*wlen == (size_t)-1) {
196 err = got_error_from_errno(); /* give up */
197 goto done;
201 *ws = calloc(*wlen + 1, sizeof(*ws));
202 if (*ws == NULL) {
203 err = got_error_from_errno();
204 goto done;
207 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
208 err = got_error_from_errno();
209 done:
210 free(vis);
211 if (err) {
212 free(*ws);
213 *ws = NULL;
214 *wlen = 0;
216 return err;
219 /* Format a line for display, ensuring that it won't overflow a width limit. */
220 static const struct got_error *
221 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
223 const struct got_error *err = NULL;
224 int cols = 0;
225 wchar_t *wline = NULL;
226 size_t wlen;
227 int i;
229 *wlinep = NULL;
230 *widthp = 0;
232 err = mbs2ws(&wline, &wlen, line);
233 if (err)
234 return err;
236 i = 0;
237 while (i < wlen && cols < wlimit) {
238 int width = wcwidth(wline[i]);
239 switch (width) {
240 case 0:
241 i++;
242 break;
243 case 1:
244 case 2:
245 if (cols + width <= wlimit) {
246 cols += width;
247 i++;
249 break;
250 case -1:
251 if (wline[i] == L'\t')
252 cols += TABSIZE - ((cols + 1) % TABSIZE);
253 i++;
254 break;
255 default:
256 err = got_error_from_errno();
257 goto done;
260 wline[i] = L'\0';
261 if (widthp)
262 *widthp = cols;
263 done:
264 if (err)
265 free(wline);
266 else
267 *wlinep = wline;
268 return err;
271 static const struct got_error *
272 draw_commit(struct tog_view *view, struct got_commit_object *commit,
273 struct got_object_id *id)
275 const struct got_error *err = NULL;
276 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
277 char *logmsg0 = NULL, *logmsg = NULL;
278 char *author0 = NULL, *author = NULL;
279 wchar_t *wlogmsg = NULL, *wauthor = NULL;
280 int author_width, logmsg_width;
281 char *newline, *smallerthan;
282 char *line = NULL;
283 int col, limit;
284 static const size_t date_display_cols = 9;
285 static const size_t author_display_cols = 16;
286 const int avail = view->ncols;
288 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
289 >= sizeof(datebuf))
290 return got_error(GOT_ERR_NO_SPACE);
292 if (avail < date_display_cols)
293 limit = MIN(sizeof(datebuf) - 1, avail);
294 else
295 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
296 waddnstr(view->window, datebuf, limit);
297 col = limit + 1;
298 if (col > avail)
299 goto done;
301 author0 = strdup(commit->author);
302 if (author0 == NULL) {
303 err = got_error_from_errno();
304 goto done;
306 author = author0;
307 smallerthan = strchr(author, '<');
308 if (smallerthan)
309 *smallerthan = '\0';
310 else {
311 char *at = strchr(author, '@');
312 if (at)
313 *at = '\0';
315 limit = avail - col;
316 err = format_line(&wauthor, &author_width, author, limit);
317 if (err)
318 goto done;
319 waddwstr(view->window, wauthor);
320 col += author_width;
321 while (col <= avail && author_width < author_display_cols + 1) {
322 waddch(view->window, ' ');
323 col++;
324 author_width++;
326 if (col > avail)
327 goto done;
329 logmsg0 = strdup(commit->logmsg);
330 if (logmsg0 == NULL) {
331 err = got_error_from_errno();
332 goto done;
334 logmsg = logmsg0;
335 while (*logmsg == '\n')
336 logmsg++;
337 newline = strchr(logmsg, '\n');
338 if (newline)
339 *newline = '\0';
340 limit = avail - col;
341 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
342 if (err)
343 goto done;
344 waddwstr(view->window, wlogmsg);
345 col += logmsg_width;
346 while (col <= avail) {
347 waddch(view->window, ' ');
348 col++;
350 done:
351 free(logmsg0);
352 free(wlogmsg);
353 free(author0);
354 free(wauthor);
355 free(line);
356 return err;
359 struct commit_queue_entry {
360 TAILQ_ENTRY(commit_queue_entry) entry;
361 struct got_object_id *id;
362 struct got_commit_object *commit;
363 };
364 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
365 struct commit_queue {
366 int ncommits;
367 struct commit_queue_head head;
368 };
370 static struct commit_queue_entry *
371 alloc_commit_queue_entry(struct got_commit_object *commit,
372 struct got_object_id *id)
374 struct commit_queue_entry *entry;
376 entry = calloc(1, sizeof(*entry));
377 if (entry == NULL)
378 return NULL;
380 entry->id = id;
381 entry->commit = commit;
382 return entry;
385 static void
386 pop_commit(struct commit_queue *commits)
388 struct commit_queue_entry *entry;
390 entry = TAILQ_FIRST(&commits->head);
391 TAILQ_REMOVE(&commits->head, entry, entry);
392 got_object_commit_close(entry->commit);
393 commits->ncommits--;
394 /* Don't free entry->id! It is owned by the commit graph. */
395 free(entry);
398 static void
399 free_commits(struct commit_queue *commits)
401 while (!TAILQ_EMPTY(&commits->head))
402 pop_commit(commits);
405 static const struct got_error *
406 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
407 struct got_object_id *start_id, int minqueue, int init,
408 struct got_repository *repo, const char *path)
410 const struct got_error *err = NULL;
411 struct got_object_id *id;
412 struct commit_queue_entry *entry;
413 int nfetched, nqueued = 0, found_obj = 0;
414 int is_root_path = strcmp(path, "/") == 0;
416 err = got_commit_graph_iter_start(graph, start_id);
417 if (err)
418 return err;
420 entry = TAILQ_LAST(&commits->head, commit_queue_head);
421 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
422 int nfetched;
424 /* Start ID's commit is already on the queue; skip over it. */
425 err = got_commit_graph_iter_next(&id, graph);
426 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
427 return err;
429 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
430 if (err)
431 return err;
434 while (1) {
435 struct got_commit_object *commit;
437 err = got_commit_graph_iter_next(&id, graph);
438 if (err) {
439 if (err->code != GOT_ERR_ITER_NEED_MORE)
440 break;
441 if (nqueued >= minqueue) {
442 err = NULL;
443 break;
445 err = got_commit_graph_fetch_commits(&nfetched,
446 graph, 1, repo);
447 if (err)
448 return err;
449 continue;
451 if (id == NULL)
452 break;
454 err = got_object_open_as_commit(&commit, repo, id);
455 if (err)
456 break;
458 if (!is_root_path) {
459 struct got_object *obj;
460 struct got_object_qid *pid;
461 int changed = 0;
463 err = got_object_open_by_path(&obj, repo, id, path);
464 if (err) {
465 got_object_commit_close(commit);
466 if (err->code == GOT_ERR_NO_OBJ &&
467 (found_obj || !init)) {
468 /* History stops here. */
469 err = got_error(GOT_ERR_ITER_COMPLETED);
471 break;
473 found_obj = 1;
475 pid = SIMPLEQ_FIRST(&commit->parent_ids);
476 if (pid != NULL) {
477 struct got_object *pobj;
478 err = got_object_open_by_path(&pobj, repo,
479 pid->id, path);
480 if (err) {
481 if (err->code != GOT_ERR_NO_OBJ) {
482 got_object_close(obj);
483 got_object_commit_close(commit);
484 break;
486 err = NULL;
487 changed = 1;
488 } else {
489 struct got_object_id *id, *pid;
490 id = got_object_get_id(obj);
491 if (id == NULL) {
492 err = got_error_from_errno();
493 got_object_close(obj);
494 got_object_close(pobj);
495 break;
497 pid = got_object_get_id(pobj);
498 if (pid == NULL) {
499 err = got_error_from_errno();
500 free(id);
501 got_object_close(obj);
502 got_object_close(pobj);
503 break;
505 changed =
506 (got_object_id_cmp(id, pid) != 0);
507 got_object_close(pobj);
508 free(id);
509 free(pid);
512 got_object_close(obj);
513 if (!changed) {
514 got_object_commit_close(commit);
515 continue;
519 entry = alloc_commit_queue_entry(commit, id);
520 if (entry == NULL) {
521 err = got_error_from_errno();
522 break;
524 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
525 nqueued++;
526 commits->ncommits++;
529 return err;
532 static const struct got_error *
533 fetch_next_commit(struct commit_queue_entry **pentry,
534 struct commit_queue_entry *entry, struct commit_queue *commits,
535 struct got_commit_graph *graph, struct got_repository *repo,
536 const char *path)
538 const struct got_error *err = NULL;
540 *pentry = NULL;
542 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
543 if (err)
544 return err;
546 /* Next entry to display should now be available. */
547 *pentry = TAILQ_NEXT(entry, entry);
548 if (*pentry == NULL)
549 return got_error(GOT_ERR_NO_OBJ);
551 return NULL;
554 static const struct got_error *
555 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
557 const struct got_error *err = NULL;
558 struct got_reference *head_ref;
560 *head_id = NULL;
562 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
563 if (err)
564 return err;
566 err = got_ref_resolve(head_id, repo, head_ref);
567 got_ref_close(head_ref);
568 if (err) {
569 *head_id = NULL;
570 return err;
573 return NULL;
576 static const struct got_error *
577 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
578 struct commit_queue_entry **selected, struct commit_queue_entry *first,
579 struct commit_queue *commits, int selected_idx, int limit,
580 struct got_commit_graph *graph, struct got_repository *repo,
581 const char *path)
583 const struct got_error *err = NULL;
584 struct commit_queue_entry *entry;
585 int ncommits, width;
586 char *id_str, *header;
587 wchar_t *wline;
589 entry = first;
590 ncommits = 0;
591 while (entry) {
592 if (ncommits == selected_idx) {
593 *selected = entry;
594 break;
596 entry = TAILQ_NEXT(entry, entry);
597 ncommits++;
600 err = got_object_id_str(&id_str, (*selected)->id);
601 if (err)
602 return err;
604 if (path) {
605 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
606 err = got_error_from_errno();
607 free(id_str);
608 return err;
610 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
611 err = got_error_from_errno();
612 free(id_str);
613 return err;
615 free(id_str);
616 err = format_line(&wline, &width, header, view->ncols);
617 if (err) {
618 free(header);
619 return err;
621 free(header);
623 werase(view->window);
625 waddwstr(view->window, wline);
626 if (width < view->ncols)
627 waddch(view->window, '\n');
628 free(wline);
629 if (limit <= 1)
630 return NULL;
632 entry = first;
633 *last = first;
634 ncommits = 0;
635 while (entry) {
636 if (ncommits >= limit - 1)
637 break;
638 if (ncommits == selected_idx)
639 wstandout(view->window);
640 err = draw_commit(view, entry->commit, entry->id);
641 if (ncommits == selected_idx)
642 wstandend(view->window);
643 if (err)
644 break;
645 ncommits++;
646 *last = entry;
647 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
648 err = queue_commits(graph, commits, entry->id, 1,
649 0, repo, path);
650 if (err) {
651 if (err->code != GOT_ERR_ITER_COMPLETED)
652 return err;
653 err = NULL;
656 entry = TAILQ_NEXT(entry, entry);
659 update_panels();
660 doupdate();
662 return err;
665 static void
666 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
667 struct commit_queue *commits)
669 struct commit_queue_entry *entry;
670 int nscrolled = 0;
672 entry = TAILQ_FIRST(&commits->head);
673 if (*first_displayed_entry == entry)
674 return;
676 entry = *first_displayed_entry;
677 while (entry && nscrolled < maxscroll) {
678 entry = TAILQ_PREV(entry, commit_queue_head, entry);
679 if (entry) {
680 *first_displayed_entry = entry;
681 nscrolled++;
686 static const struct got_error *
687 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
688 struct commit_queue_entry *last_displayed_entry,
689 struct commit_queue *commits, struct got_commit_graph *graph,
690 struct got_repository *repo, const char *path)
692 const struct got_error *err = NULL;
693 struct commit_queue_entry *pentry;
694 int nscrolled = 0;
696 do {
697 pentry = TAILQ_NEXT(last_displayed_entry, entry);
698 if (pentry == NULL) {
699 err = fetch_next_commit(&pentry, last_displayed_entry,
700 commits, graph, repo, path);
701 if (err || pentry == NULL)
702 break;
704 last_displayed_entry = pentry;
706 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
707 if (pentry == NULL)
708 break;
709 *first_displayed_entry = pentry;
710 } while (++nscrolled < maxscroll);
712 return err;
715 static const struct got_error *
716 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
718 const struct got_error *err;
719 struct got_object *obj1 = NULL, *obj2 = NULL;
720 struct got_object_qid *parent_id;
721 struct tog_view *view;
723 err = got_object_open(&obj2, repo, entry->id);
724 if (err)
725 return err;
727 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
728 if (parent_id) {
729 err = got_object_open(&obj1, repo, parent_id->id);
730 if (err)
731 goto done;
734 view = open_view(0, 0, 0, 0);
735 if (view == NULL) {
736 err = got_error_from_errno();
737 goto done;
740 err = show_diff_view(view, obj1, obj2, repo);
741 close_view(view);
742 done:
743 if (obj1)
744 got_object_close(obj1);
745 if (obj2)
746 got_object_close(obj2);
747 return err;
750 static const struct got_error *
751 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
753 const struct got_error *err = NULL;
754 struct got_tree_object *tree;
756 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
757 if (err)
758 return err;
760 err = show_tree_view(tree, entry->id, repo);
761 got_object_tree_close(tree);
762 return err;
765 static const struct got_error *
766 show_log_view(struct got_object_id *start_id, struct got_repository *repo,
767 const char *path)
769 const struct got_error *err = NULL;
770 struct got_object_id *head_id = NULL;
771 int ch, done = 0, selected = 0, nfetched;
772 struct got_commit_graph *graph = NULL;
773 struct commit_queue commits;
774 struct commit_queue_entry *first_displayed_entry = NULL;
775 struct commit_queue_entry *last_displayed_entry = NULL;
776 struct commit_queue_entry *selected_entry = NULL;
777 char *in_repo_path = NULL;
778 struct tog_view *view = NULL;
780 err = got_repo_map_path(&in_repo_path, repo, path);
781 if (err != NULL)
782 goto done;
784 err = get_head_commit_id(&head_id, repo);
785 if (err)
786 return err;
788 /* The graph contains all commits. */
789 err = got_commit_graph_open(&graph, head_id, 0, repo);
790 if (err)
791 goto done;
792 /* The commit queue contains a subset of commits filtered by path. */
793 TAILQ_INIT(&commits.head);
794 commits.ncommits = 0;
796 /* Populate commit graph with a sufficient number of commits. */
797 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
798 repo);
799 if (err)
800 goto done;
802 view = open_view(0, 0, 0, 0);
803 if (view == NULL) {
804 err = got_error_from_errno();
805 goto done;
807 show_panel(view->panel);
809 /*
810 * Open the initial batch of commits, sorted in commit graph order.
811 * We keep all commits open throughout the lifetime of the log view
812 * in order to avoid having to re-fetch commits from disk while
813 * updating the display.
814 */
815 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
816 in_repo_path);
817 if (err) {
818 if (err->code != GOT_ERR_ITER_COMPLETED)
819 goto done;
820 err = NULL;
823 first_displayed_entry = TAILQ_FIRST(&commits.head);
824 selected_entry = first_displayed_entry;
825 while (!done) {
826 err = draw_commits(view, &last_displayed_entry, &selected_entry,
827 first_displayed_entry, &commits, selected, view->nlines,
828 graph, repo, in_repo_path);
829 if (err)
830 goto done;
832 nodelay(stdscr, FALSE);
833 ch = wgetch(view->window);
834 nodelay(stdscr, TRUE);
835 switch (ch) {
836 case ERR:
837 break;
838 case 'q':
839 done = 1;
840 break;
841 case 'k':
842 case KEY_UP:
843 if (selected > 0)
844 selected--;
845 if (selected > 0)
846 break;
847 scroll_up(&first_displayed_entry, 1, &commits);
848 break;
849 case KEY_PPAGE:
850 if (TAILQ_FIRST(&commits.head) ==
851 first_displayed_entry) {
852 selected = 0;
853 break;
855 scroll_up(&first_displayed_entry, view->nlines,
856 &commits);
857 break;
858 case 'j':
859 case KEY_DOWN:
860 if (selected < MIN(view->nlines - 2,
861 commits.ncommits - 1)) {
862 selected++;
863 break;
865 err = scroll_down(&first_displayed_entry, 1,
866 last_displayed_entry, &commits, graph,
867 repo, in_repo_path);
868 if (err) {
869 if (err->code != GOT_ERR_ITER_COMPLETED)
870 goto done;
871 err = NULL;
873 break;
874 case KEY_NPAGE: {
875 struct commit_queue_entry *first = first_displayed_entry;
876 err = scroll_down(&first_displayed_entry, view->nlines,
877 last_displayed_entry, &commits, graph,
878 repo, in_repo_path);
879 if (err) {
880 if (err->code != GOT_ERR_ITER_COMPLETED)
881 goto done;
882 /* can't scroll any further; move cursor down */
883 if (first == first_displayed_entry && selected <
884 MIN(view->nlines - 2, commits.ncommits - 1)) {
885 selected = MIN(view->nlines - 2,
886 commits.ncommits - 1);
888 err = NULL;
890 break;
892 case KEY_RESIZE:
893 err = view_resize(view);
894 if (err)
895 goto done;
896 if (selected > view->nlines - 2)
897 selected = view->nlines - 2;
898 if (selected > commits.ncommits - 1)
899 selected = commits.ncommits - 1;
900 break;
901 case KEY_ENTER:
902 case '\r':
903 err = show_commit(selected_entry, repo);
904 if (err)
905 goto done;
906 show_panel(view->panel);
907 break;
908 case 't':
909 err = browse_commit(selected_entry, repo);
910 if (err)
911 goto done;
912 show_panel(view->panel);
913 break;
914 default:
915 break;
918 done:
919 if (view)
920 close_view(view);
921 free(head_id);
922 if (graph)
923 got_commit_graph_close(graph);
924 free_commits(&commits);
925 free(in_repo_path);
926 return err;
929 static const struct got_error *
930 cmd_log(int argc, char *argv[])
932 const struct got_error *error;
933 struct got_repository *repo = NULL;
934 struct got_object_id *start_id = NULL;
935 char *path = NULL, *repo_path = NULL, *cwd = NULL;
936 char *start_commit = NULL;
937 int ch;
939 #ifndef PROFILE
940 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
941 err(1, "pledge");
942 #endif
944 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
945 switch (ch) {
946 case 'c':
947 start_commit = optarg;
948 break;
949 case 'r':
950 repo_path = realpath(optarg, NULL);
951 if (repo_path == NULL)
952 err(1, "-r option");
953 break;
954 default:
955 usage();
956 /* NOTREACHED */
960 argc -= optind;
961 argv += optind;
963 if (argc == 0)
964 path = strdup("");
965 else if (argc == 1)
966 path = strdup(argv[0]);
967 else
968 usage_log();
969 if (path == NULL)
970 return got_error_from_errno();
972 cwd = getcwd(NULL, 0);
973 if (cwd == NULL) {
974 error = got_error_from_errno();
975 goto done;
977 if (repo_path == NULL) {
978 repo_path = strdup(cwd);
979 if (repo_path == NULL) {
980 error = got_error_from_errno();
981 goto done;
985 error = got_repo_open(&repo, repo_path);
986 if (error != NULL)
987 goto done;
989 if (start_commit == NULL) {
990 error = get_head_commit_id(&start_id, repo);
991 if (error != NULL)
992 goto done;
993 } else {
994 struct got_object *obj;
995 error = got_object_open_by_id_str(&obj, repo, start_commit);
996 if (error == NULL) {
997 start_id = got_object_get_id(obj);
998 if (start_id == NULL)
999 error = got_error_from_errno();
1000 goto done;
1003 if (error != NULL)
1004 goto done;
1006 error = show_log_view(start_id, repo, path);
1007 done:
1008 free(repo_path);
1009 free(cwd);
1010 free(path);
1011 free(start_id);
1012 if (repo)
1013 got_repo_close(repo);
1014 return error;
1017 __dead static void
1018 usage_diff(void)
1020 endwin();
1021 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1022 getprogname());
1023 exit(1);
1026 static char *
1027 parse_next_line(FILE *f, size_t *len)
1029 char *line;
1030 size_t linelen;
1031 size_t lineno;
1032 const char delim[3] = { '\0', '\0', '\0'};
1034 line = fparseln(f, &linelen, &lineno, delim, 0);
1035 if (len)
1036 *len = linelen;
1037 return line;
1040 static const struct got_error *
1041 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1042 int *last_displayed_line, int *eof, int max_lines)
1044 const struct got_error *err;
1045 int nlines = 0, nprinted = 0;
1046 char *line;
1047 size_t len;
1048 wchar_t *wline;
1049 int width;
1051 rewind(f);
1052 werase(view->window);
1054 *eof = 0;
1055 while (nprinted < max_lines) {
1056 line = parse_next_line(f, &len);
1057 if (line == NULL) {
1058 *eof = 1;
1059 break;
1061 if (++nlines < *first_displayed_line) {
1062 free(line);
1063 continue;
1066 err = format_line(&wline, &width, line, view->ncols);
1067 if (err) {
1068 free(line);
1069 free(wline);
1070 return err;
1072 waddwstr(view->window, wline);
1073 if (width < view->ncols)
1074 waddch(view->window, '\n');
1075 if (++nprinted == 1)
1076 *first_displayed_line = nlines;
1077 free(line);
1078 free(wline);
1079 wline = NULL;
1081 *last_displayed_line = nlines;
1083 update_panels();
1084 doupdate();
1086 return NULL;
1089 static const struct got_error *
1090 show_diff_view(struct tog_view *view, struct got_object *obj1,
1091 struct got_object *obj2, struct got_repository *repo)
1093 const struct got_error *err;
1094 FILE *f;
1095 int ch, done = 0;
1096 int first_displayed_line = 1, last_displayed_line = view->nlines;
1097 int eof, i;
1099 if (obj1 != NULL && obj2 != NULL &&
1100 got_object_get_type(obj1) != got_object_get_type(obj2))
1101 return got_error(GOT_ERR_OBJ_TYPE);
1103 f = got_opentemp();
1104 if (f == NULL)
1105 return got_error_from_errno();
1107 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1108 case GOT_OBJ_TYPE_BLOB:
1109 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1110 break;
1111 case GOT_OBJ_TYPE_TREE:
1112 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1113 break;
1114 case GOT_OBJ_TYPE_COMMIT:
1115 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1116 break;
1117 default:
1118 return got_error(GOT_ERR_OBJ_TYPE);
1121 fflush(f);
1123 show_panel(view->panel);
1125 while (!done) {
1126 err = draw_file(view, f, &first_displayed_line,
1127 &last_displayed_line, &eof, view->nlines);
1128 if (err)
1129 break;
1130 nodelay(stdscr, FALSE);
1131 ch = wgetch(view->window);
1132 nodelay(stdscr, TRUE);
1133 switch (ch) {
1134 case 'q':
1135 done = 1;
1136 break;
1137 case 'k':
1138 case KEY_UP:
1139 if (first_displayed_line > 1)
1140 first_displayed_line--;
1141 break;
1142 case KEY_PPAGE:
1143 case KEY_BACKSPACE:
1144 i = 0;
1145 while (i++ < view->nlines - 1 &&
1146 first_displayed_line > 1)
1147 first_displayed_line--;
1148 break;
1149 case 'j':
1150 case KEY_DOWN:
1151 if (!eof)
1152 first_displayed_line++;
1153 break;
1154 case KEY_NPAGE:
1155 case ' ':
1156 i = 0;
1157 while (!eof && i++ < view->nlines - 1) {
1158 char *line = parse_next_line(f, NULL);
1159 first_displayed_line++;
1160 if (line == NULL)
1161 break;
1163 break;
1164 case KEY_RESIZE:
1165 err = view_resize(view);
1166 if (err)
1167 goto done;
1168 break;
1169 default:
1170 break;
1173 done:
1174 fclose(f);
1175 return err;
1178 static const struct got_error *
1179 cmd_diff(int argc, char *argv[])
1181 const struct got_error *error = NULL;
1182 struct got_repository *repo = NULL;
1183 struct got_object *obj1 = NULL, *obj2 = NULL;
1184 char *repo_path = NULL;
1185 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1186 int ch;
1187 struct tog_view *view;
1189 #ifndef PROFILE
1190 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1191 err(1, "pledge");
1192 #endif
1194 while ((ch = getopt(argc, argv, "")) != -1) {
1195 switch (ch) {
1196 default:
1197 usage();
1198 /* NOTREACHED */
1202 argc -= optind;
1203 argv += optind;
1205 if (argc == 0) {
1206 usage_diff(); /* TODO show local worktree changes */
1207 } else if (argc == 2) {
1208 repo_path = getcwd(NULL, 0);
1209 if (repo_path == NULL)
1210 return got_error_from_errno();
1211 obj_id_str1 = argv[0];
1212 obj_id_str2 = argv[1];
1213 } else if (argc == 3) {
1214 repo_path = realpath(argv[0], NULL);
1215 if (repo_path == NULL)
1216 return got_error_from_errno();
1217 obj_id_str1 = argv[1];
1218 obj_id_str2 = argv[2];
1219 } else
1220 usage_diff();
1222 error = got_repo_open(&repo, repo_path);
1223 free(repo_path);
1224 if (error)
1225 goto done;
1227 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1228 if (error)
1229 goto done;
1231 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1232 if (error)
1233 goto done;
1235 view = open_view(0, 0, 0, 0);
1236 if (view == NULL) {
1237 error = got_error_from_errno();
1238 goto done;
1240 error = show_diff_view(view, obj1, obj2, repo);
1241 close_view(view);
1242 done:
1243 got_repo_close(repo);
1244 if (obj1)
1245 got_object_close(obj1);
1246 if (obj2)
1247 got_object_close(obj2);
1248 return error;
1251 __dead static void
1252 usage_blame(void)
1254 endwin();
1255 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1256 getprogname());
1257 exit(1);
1260 struct tog_blame_line {
1261 int annotated;
1262 struct got_object_id *id;
1265 static const struct got_error *
1266 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1267 const char *path, struct tog_blame_line *lines, int nlines,
1268 int blame_complete, int selected_line, int *first_displayed_line,
1269 int *last_displayed_line, int *eof, int max_lines)
1271 const struct got_error *err;
1272 int lineno = 0, nprinted = 0;
1273 char *line;
1274 size_t len;
1275 wchar_t *wline;
1276 int width, wlimit;
1277 struct tog_blame_line *blame_line;
1278 struct got_object_id *prev_id = NULL;
1279 char *id_str;
1281 err = got_object_id_str(&id_str, id);
1282 if (err)
1283 return err;
1285 rewind(f);
1286 werase(view->window);
1288 if (asprintf(&line, "commit: %s", id_str) == -1) {
1289 err = got_error_from_errno();
1290 free(id_str);
1291 return err;
1294 err = format_line(&wline, &width, line, view->ncols);
1295 free(line);
1296 line = NULL;
1297 waddwstr(view->window, wline);
1298 free(wline);
1299 wline = NULL;
1300 if (width < view->ncols)
1301 waddch(view->window, '\n');
1303 if (asprintf(&line, "[%d/%d] %s%s",
1304 *first_displayed_line - 1 + selected_line, nlines,
1305 blame_complete ? "" : "annotating ", path) == -1) {
1306 free(id_str);
1307 return got_error_from_errno();
1309 free(id_str);
1310 err = format_line(&wline, &width, line, view->ncols);
1311 free(line);
1312 line = NULL;
1313 if (err)
1314 return err;
1315 waddwstr(view->window, wline);
1316 free(wline);
1317 wline = NULL;
1318 if (width < view->ncols)
1319 waddch(view->window, '\n');
1321 *eof = 0;
1322 while (nprinted < max_lines - 2) {
1323 line = parse_next_line(f, &len);
1324 if (line == NULL) {
1325 *eof = 1;
1326 break;
1328 if (++lineno < *first_displayed_line) {
1329 free(line);
1330 continue;
1333 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1334 err = format_line(&wline, &width, line, wlimit);
1335 if (err) {
1336 free(line);
1337 return err;
1340 if (nprinted == selected_line - 1)
1341 wstandout(view->window);
1343 blame_line = &lines[lineno - 1];
1344 if (blame_line->annotated && prev_id &&
1345 got_object_id_cmp(prev_id, blame_line->id) == 0)
1346 waddstr(view->window, " ");
1347 else if (blame_line->annotated) {
1348 char *id_str;
1349 err = got_object_id_str(&id_str, blame_line->id);
1350 if (err) {
1351 free(line);
1352 free(wline);
1353 return err;
1355 wprintw(view->window, "%.8s ", id_str);
1356 free(id_str);
1357 prev_id = blame_line->id;
1358 } else {
1359 waddstr(view->window, "........ ");
1360 prev_id = NULL;
1363 waddwstr(view->window, wline);
1364 while (width < wlimit) {
1365 waddch(view->window, ' ');
1366 width++;
1368 if (nprinted == selected_line - 1)
1369 wstandend(view->window);
1370 if (++nprinted == 1)
1371 *first_displayed_line = lineno;
1372 free(line);
1373 free(wline);
1374 wline = NULL;
1376 *last_displayed_line = lineno;
1378 update_panels();
1379 doupdate();
1381 return NULL;
1384 struct tog_blame_cb_args {
1385 pthread_mutex_t *mutex;
1386 struct tog_blame_line *lines; /* one per line */
1387 int nlines;
1389 struct tog_view *view;
1390 struct got_object_id *commit_id;
1391 FILE *f;
1392 const char *path;
1393 int *first_displayed_line;
1394 int *last_displayed_line;
1395 int *selected_line;
1396 int *quit;
1399 static const struct got_error *
1400 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1402 const struct got_error *err = NULL;
1403 struct tog_blame_cb_args *a = arg;
1404 struct tog_blame_line *line;
1405 int eof;
1407 if (nlines != a->nlines ||
1408 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1409 return got_error(GOT_ERR_RANGE);
1411 if (pthread_mutex_lock(a->mutex) != 0)
1412 return got_error_from_errno();
1414 if (*a->quit) { /* user has quit the blame view */
1415 err = got_error(GOT_ERR_ITER_COMPLETED);
1416 goto done;
1419 if (lineno == -1)
1420 goto done; /* no change in this commit */
1422 line = &a->lines[lineno - 1];
1423 if (line->annotated)
1424 goto done;
1426 line->id = got_object_id_dup(id);
1427 if (line->id == NULL) {
1428 err = got_error_from_errno();
1429 goto done;
1431 line->annotated = 1;
1433 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1434 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1435 a->last_displayed_line, &eof, a->view->nlines);
1436 done:
1437 if (pthread_mutex_unlock(a->mutex) != 0)
1438 return got_error_from_errno();
1439 return err;
1442 struct tog_blame_thread_args {
1443 const char *path;
1444 struct got_repository *repo;
1445 struct tog_blame_cb_args *cb_args;
1446 int *complete;
1449 static void *
1450 blame_thread(void *arg)
1452 const struct got_error *err;
1453 struct tog_blame_thread_args *ta = arg;
1454 struct tog_blame_cb_args *a = ta->cb_args;
1455 int eof;
1457 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1458 blame_cb, ta->cb_args);
1460 if (pthread_mutex_lock(a->mutex) != 0)
1461 return (void *)got_error_from_errno();
1463 got_repo_close(ta->repo);
1464 ta->repo = NULL;
1465 *ta->complete = 1;
1466 if (!err)
1467 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1468 a->lines, a->nlines, 1, *a->selected_line,
1469 a->first_displayed_line, a->last_displayed_line, &eof,
1470 a->view->nlines);
1472 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1473 err = got_error_from_errno();
1475 return (void *)err;
1478 static struct got_object_id *
1479 get_selected_commit_id(struct tog_blame_line *lines,
1480 int first_displayed_line, int selected_line)
1482 struct tog_blame_line *line;
1484 line = &lines[first_displayed_line - 1 + selected_line - 1];
1485 if (!line->annotated)
1486 return NULL;
1488 return line->id;
1491 static const struct got_error *
1492 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1493 struct tog_blame_line *lines, int first_displayed_line,
1494 int selected_line, struct got_repository *repo)
1496 const struct got_error *err = NULL;
1497 struct got_commit_object *commit = NULL;
1498 struct got_object_id *selected_id;
1499 struct got_object_qid *pid;
1501 *pobj = NULL;
1502 *obj = NULL;
1504 selected_id = get_selected_commit_id(lines,
1505 first_displayed_line, selected_line);
1506 if (selected_id == NULL)
1507 return NULL;
1509 err = got_object_open(obj, repo, selected_id);
1510 if (err)
1511 goto done;
1513 err = got_object_commit_open(&commit, repo, *obj);
1514 if (err)
1515 goto done;
1517 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1518 if (pid) {
1519 err = got_object_open(pobj, repo, pid->id);
1520 if (err)
1521 goto done;
1523 done:
1524 if (commit)
1525 got_object_commit_close(commit);
1526 return err;
1529 struct tog_blame {
1530 FILE *f;
1531 size_t filesize;
1532 struct tog_blame_line *lines;
1533 size_t nlines;
1534 pthread_t thread;
1535 struct tog_blame_thread_args thread_args;
1536 struct tog_blame_cb_args cb_args;
1537 const char *path;
1540 static const struct got_error *
1541 stop_blame(struct tog_blame *blame)
1543 const struct got_error *err = NULL;
1544 int i;
1546 if (blame->thread) {
1547 if (pthread_join(blame->thread, (void **)&err) != 0)
1548 err = got_error_from_errno();
1549 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1550 err = NULL;
1551 blame->thread = NULL;
1553 if (blame->thread_args.repo) {
1554 got_repo_close(blame->thread_args.repo);
1555 blame->thread_args.repo = NULL;
1557 if (blame->f) {
1558 fclose(blame->f);
1559 blame->f = NULL;
1561 for (i = 0; i < blame->nlines; i++)
1562 free(blame->lines[i].id);
1563 free(blame->lines);
1564 blame->lines = NULL;
1565 free(blame->cb_args.commit_id);
1566 blame->cb_args.commit_id = NULL;
1568 return err;
1571 static const struct got_error *
1572 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1573 struct tog_view *view, int *blame_complete,
1574 int *first_displayed_line, int *last_displayed_line,
1575 int *selected_line, int *done, const char *path,
1576 struct got_object_id *commit_id,
1577 struct got_repository *repo)
1579 const struct got_error *err = NULL;
1580 struct got_blob_object *blob = NULL;
1581 struct got_repository *thread_repo = NULL;
1582 struct got_object *obj;
1584 err = got_object_open_by_path(&obj, repo, commit_id, path);
1585 if (err)
1586 goto done;
1587 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1588 err = got_error(GOT_ERR_OBJ_TYPE);
1589 goto done;
1592 err = got_object_blob_open(&blob, repo, obj, 8192);
1593 if (err)
1594 goto done;
1595 blame->f = got_opentemp();
1596 if (blame->f == NULL) {
1597 err = got_error_from_errno();
1598 goto done;
1600 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1601 blame->f, blob);
1602 if (err)
1603 goto done;
1605 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1606 if (blame->lines == NULL) {
1607 err = got_error_from_errno();
1608 goto done;
1611 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1612 if (err)
1613 goto done;
1615 blame->cb_args.view = view;
1616 blame->cb_args.lines = blame->lines;
1617 blame->cb_args.nlines = blame->nlines;
1618 blame->cb_args.mutex = mutex;
1619 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1620 if (blame->cb_args.commit_id == NULL) {
1621 err = got_error_from_errno();
1622 goto done;
1624 blame->cb_args.f = blame->f;
1625 blame->cb_args.path = path;
1626 blame->cb_args.first_displayed_line = first_displayed_line;
1627 blame->cb_args.selected_line = selected_line;
1628 blame->cb_args.last_displayed_line = last_displayed_line;
1629 blame->cb_args.quit = done;
1631 blame->thread_args.path = path;
1632 blame->thread_args.repo = thread_repo;
1633 blame->thread_args.cb_args = &blame->cb_args;
1634 blame->thread_args.complete = blame_complete;
1635 *blame_complete = 0;
1637 if (pthread_create(&blame->thread, NULL, blame_thread,
1638 &blame->thread_args) != 0) {
1639 err = got_error_from_errno();
1640 goto done;
1643 done:
1644 if (blob)
1645 got_object_blob_close(blob);
1646 if (obj)
1647 got_object_close(obj);
1648 if (err)
1649 stop_blame(blame);
1650 return err;
1653 static const struct got_error *
1654 show_blame_view(const char *path, struct got_object_id *commit_id,
1655 struct got_repository *repo)
1657 const struct got_error *err = NULL, *thread_err = NULL;
1658 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1659 int selected_line = first_displayed_line;
1660 int eof, blame_complete = 0;
1661 struct got_object *obj = NULL, *pobj = NULL;
1662 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1663 struct tog_blame blame;
1664 struct got_object_id_queue blamed_commits;
1665 struct got_object_qid *blamed_commit = NULL;
1666 struct tog_view *view = NULL, *diff_view;
1668 SIMPLEQ_INIT(&blamed_commits);
1670 if (pthread_mutex_init(&mutex, NULL) != 0) {
1671 err = got_error_from_errno();
1672 goto done;
1675 err = got_object_qid_alloc(&blamed_commit, commit_id);
1676 if (err)
1677 goto done;
1678 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1680 view = open_view(0, 0, 0, 0);
1681 if (view == NULL) {
1682 err = got_error_from_errno();
1683 goto done;
1685 show_panel(view->panel);
1686 last_displayed_line = view->nlines;
1688 memset(&blame, 0, sizeof(blame));
1689 err = run_blame(&blame, &mutex, view, &blame_complete,
1690 &first_displayed_line, &last_displayed_line,
1691 &selected_line, &done, path, blamed_commit->id, repo);
1692 if (err)
1693 return err;
1695 while (!done) {
1696 if (pthread_mutex_lock(&mutex) != 0) {
1697 err = got_error_from_errno();
1698 goto done;
1700 err = draw_blame(view, blamed_commit->id, blame.f, path,
1701 blame.lines, blame.nlines, blame_complete, selected_line,
1702 &first_displayed_line, &last_displayed_line, &eof,
1703 view->nlines);
1704 if (pthread_mutex_unlock(&mutex) != 0) {
1705 err = got_error_from_errno();
1706 goto done;
1708 if (err)
1709 break;
1710 nodelay(stdscr, FALSE);
1711 ch = wgetch(view->window);
1712 nodelay(stdscr, TRUE);
1713 if (pthread_mutex_lock(&mutex) != 0) {
1714 err = got_error_from_errno();
1715 goto done;
1717 switch (ch) {
1718 case 'q':
1719 done = 1;
1720 break;
1721 case 'k':
1722 case KEY_UP:
1723 if (selected_line > 1)
1724 selected_line--;
1725 else if (selected_line == 1 &&
1726 first_displayed_line > 1)
1727 first_displayed_line--;
1728 break;
1729 case KEY_PPAGE:
1730 case KEY_BACKSPACE:
1731 if (first_displayed_line == 1) {
1732 selected_line = 1;
1733 break;
1735 if (first_displayed_line > view->nlines - 2)
1736 first_displayed_line -=
1737 (view->nlines - 2);
1738 else
1739 first_displayed_line = 1;
1740 break;
1741 case 'j':
1742 case KEY_DOWN:
1743 if (selected_line < view->nlines - 2 &&
1744 first_displayed_line + selected_line <=
1745 blame.nlines)
1746 selected_line++;
1747 else if (last_displayed_line < blame.nlines)
1748 first_displayed_line++;
1749 break;
1750 case 'b':
1751 case 'p': {
1752 struct got_object_id *id;
1753 id = get_selected_commit_id(blame.lines,
1754 first_displayed_line, selected_line);
1755 if (id == NULL || got_object_id_cmp(id,
1756 blamed_commit->id) == 0)
1757 break;
1758 err = open_selected_commit(&pobj, &obj,
1759 blame.lines, first_displayed_line,
1760 selected_line, repo);
1761 if (err)
1762 break;
1763 if (pobj == NULL && obj == NULL)
1764 break;
1765 if (ch == 'p' && pobj == NULL)
1766 break;
1767 done = 1;
1768 if (pthread_mutex_unlock(&mutex) != 0) {
1769 err = got_error_from_errno();
1770 goto done;
1772 thread_err = stop_blame(&blame);
1773 done = 0;
1774 if (pthread_mutex_lock(&mutex) != 0) {
1775 err = got_error_from_errno();
1776 goto done;
1778 if (thread_err)
1779 break;
1780 id = got_object_get_id(ch == 'b' ? obj : pobj);
1781 got_object_close(obj);
1782 obj = NULL;
1783 if (pobj) {
1784 got_object_close(pobj);
1785 pobj = NULL;
1787 if (id == NULL) {
1788 err = got_error_from_errno();
1789 break;
1791 err = got_object_qid_alloc(&blamed_commit, id);
1792 free(id);
1793 if (err)
1794 goto done;
1795 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1796 blamed_commit, entry);
1797 err = run_blame(&blame, &mutex, view,
1798 &blame_complete, &first_displayed_line,
1799 &last_displayed_line, &selected_line,
1800 &done, path, blamed_commit->id, repo);
1801 if (err)
1802 break;
1803 break;
1805 case 'B': {
1806 struct got_object_qid *first;
1807 first = SIMPLEQ_FIRST(&blamed_commits);
1808 if (!got_object_id_cmp(first->id, commit_id))
1809 break;
1810 done = 1;
1811 if (pthread_mutex_unlock(&mutex) != 0) {
1812 err = got_error_from_errno();
1813 goto done;
1815 thread_err = stop_blame(&blame);
1816 done = 0;
1817 if (pthread_mutex_lock(&mutex) != 0) {
1818 err = got_error_from_errno();
1819 goto done;
1821 if (thread_err)
1822 break;
1823 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1824 got_object_qid_free(blamed_commit);
1825 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1826 err = run_blame(&blame, &mutex, view,
1827 &blame_complete, &first_displayed_line,
1828 &last_displayed_line, &selected_line,
1829 &done, path, blamed_commit->id, repo);
1830 if (err)
1831 break;
1832 break;
1834 case KEY_ENTER:
1835 case '\r':
1836 err = open_selected_commit(&pobj, &obj,
1837 blame.lines, first_displayed_line,
1838 selected_line, repo);
1839 if (err)
1840 break;
1841 if (pobj == NULL && obj == NULL)
1842 break;
1843 diff_view = open_view(0, 0, 0, 0);
1844 if (diff_view == NULL) {
1845 err = got_error_from_errno();
1846 break;
1848 err = show_diff_view(diff_view, pobj, obj, repo);
1849 close_view(diff_view);
1850 if (pobj) {
1851 got_object_close(pobj);
1852 pobj = NULL;
1854 got_object_close(obj);
1855 obj = NULL;
1856 show_panel(view->panel);
1857 if (err)
1858 break;
1859 break;
1860 case KEY_NPAGE:
1861 case ' ':
1862 if (last_displayed_line >= blame.nlines &&
1863 selected_line < view->nlines - 2) {
1864 selected_line = MIN(blame.nlines,
1865 view->nlines - 2);
1866 break;
1868 if (last_displayed_line + view->nlines - 2 <=
1869 blame.nlines)
1870 first_displayed_line +=
1871 view->nlines - 2;
1872 else
1873 first_displayed_line =
1874 blame.nlines - (view->nlines - 3);
1875 break;
1876 case KEY_RESIZE:
1877 err = view_resize(view);
1878 if (err)
1879 break;
1880 if (selected_line > view->nlines - 2) {
1881 selected_line = MIN(blame.nlines,
1882 view->nlines - 2);
1884 break;
1885 default:
1886 break;
1888 if (pthread_mutex_unlock(&mutex) != 0)
1889 err = got_error_from_errno();
1890 if (err || thread_err)
1891 break;
1893 done:
1894 if (pobj)
1895 got_object_close(pobj);
1896 if (blame.thread)
1897 thread_err = stop_blame(&blame);
1898 if (view)
1899 close_view(view);
1900 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1901 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1902 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1903 got_object_qid_free(blamed_commit);
1905 return thread_err ? thread_err : err;
1908 static const struct got_error *
1909 cmd_blame(int argc, char *argv[])
1911 const struct got_error *error;
1912 struct got_repository *repo = NULL;
1913 char *repo_path = NULL;
1914 char *path = NULL;
1915 struct got_object_id *commit_id = NULL;
1916 char *commit_id_str = NULL;
1917 int ch;
1919 #ifndef PROFILE
1920 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1921 err(1, "pledge");
1922 #endif
1924 while ((ch = getopt(argc, argv, "c:")) != -1) {
1925 switch (ch) {
1926 case 'c':
1927 commit_id_str = optarg;
1928 break;
1929 default:
1930 usage();
1931 /* NOTREACHED */
1935 argc -= optind;
1936 argv += optind;
1938 if (argc == 0) {
1939 usage_blame();
1940 } else if (argc == 1) {
1941 repo_path = getcwd(NULL, 0);
1942 if (repo_path == NULL)
1943 return got_error_from_errno();
1944 path = argv[0];
1945 } else if (argc == 2) {
1946 repo_path = realpath(argv[0], NULL);
1947 if (repo_path == NULL)
1948 return got_error_from_errno();
1949 path = argv[1];
1950 } else
1951 usage_blame();
1953 error = got_repo_open(&repo, repo_path);
1954 free(repo_path);
1955 if (error != NULL)
1956 return error;
1958 if (commit_id_str == NULL) {
1959 struct got_reference *head_ref;
1960 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1961 if (error != NULL)
1962 goto done;
1963 error = got_ref_resolve(&commit_id, repo, head_ref);
1964 got_ref_close(head_ref);
1965 } else {
1966 struct got_object *obj;
1967 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1968 if (error != NULL)
1969 goto done;
1970 commit_id = got_object_get_id(obj);
1971 if (commit_id == NULL)
1972 error = got_error_from_errno();
1973 got_object_close(obj);
1975 if (error != NULL)
1976 goto done;
1978 error = show_blame_view(path, commit_id, repo);
1979 done:
1980 free(commit_id);
1981 if (repo)
1982 got_repo_close(repo);
1983 return error;
1986 static const struct got_error *
1987 draw_tree_entries(struct tog_view *view,
1988 struct got_tree_entry **first_displayed_entry,
1989 struct got_tree_entry **last_displayed_entry,
1990 struct got_tree_entry **selected_entry, int *ndisplayed,
1991 const char *label, int show_ids, const char *parent_path,
1992 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1994 const struct got_error *err = NULL;
1995 struct got_tree_entry *te;
1996 wchar_t *wline;
1997 int width, n;
1999 *ndisplayed = 0;
2001 werase(view->window);
2003 if (limit == 0)
2004 return NULL;
2006 err = format_line(&wline, &width, label, view->ncols);
2007 if (err)
2008 return err;
2009 waddwstr(view->window, wline);
2010 free(wline);
2011 wline = NULL;
2012 if (width < view->ncols)
2013 waddch(view->window, '\n');
2014 if (--limit <= 0)
2015 return NULL;
2016 err = format_line(&wline, &width, parent_path, view->ncols);
2017 if (err)
2018 return err;
2019 waddwstr(view->window, wline);
2020 free(wline);
2021 wline = NULL;
2022 if (width < view->ncols)
2023 waddch(view->window, '\n');
2024 if (--limit <= 0)
2025 return NULL;
2026 waddch(view->window, '\n');
2027 if (--limit <= 0)
2028 return NULL;
2030 te = SIMPLEQ_FIRST(&entries->head);
2031 if (*first_displayed_entry == NULL) {
2032 if (selected == 0) {
2033 wstandout(view->window);
2034 *selected_entry = NULL;
2036 waddstr(view->window, " ..\n"); /* parent directory */
2037 if (selected == 0)
2038 wstandend(view->window);
2039 (*ndisplayed)++;
2040 if (--limit <= 0)
2041 return NULL;
2042 n = 1;
2043 } else {
2044 n = 0;
2045 while (te != *first_displayed_entry)
2046 te = SIMPLEQ_NEXT(te, entry);
2049 while (te) {
2050 char *line = NULL, *id_str = NULL;
2052 if (show_ids) {
2053 err = got_object_id_str(&id_str, te->id);
2054 if (err)
2055 return got_error_from_errno();
2057 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2058 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2059 free(id_str);
2060 return got_error_from_errno();
2062 free(id_str);
2063 err = format_line(&wline, &width, line, view->ncols);
2064 if (err) {
2065 free(line);
2066 break;
2068 if (n == selected) {
2069 wstandout(view->window);
2070 *selected_entry = te;
2072 waddwstr(view->window, wline);
2073 if (width < view->ncols)
2074 waddch(view->window, '\n');
2075 if (n == selected)
2076 wstandend(view->window);
2077 free(line);
2078 free(wline);
2079 wline = NULL;
2080 n++;
2081 (*ndisplayed)++;
2082 *last_displayed_entry = te;
2083 if (--limit <= 0)
2084 break;
2085 te = SIMPLEQ_NEXT(te, entry);
2088 return err;
2091 static void
2092 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2093 const struct got_tree_entries *entries, int isroot)
2095 struct got_tree_entry *te, *prev;
2096 int i;
2098 if (*first_displayed_entry == NULL)
2099 return;
2101 te = SIMPLEQ_FIRST(&entries->head);
2102 if (*first_displayed_entry == te) {
2103 if (!isroot)
2104 *first_displayed_entry = NULL;
2105 return;
2108 /* XXX this is stupid... switch to TAILQ? */
2109 for (i = 0; i < maxscroll; i++) {
2110 while (te != *first_displayed_entry) {
2111 prev = te;
2112 te = SIMPLEQ_NEXT(te, entry);
2114 *first_displayed_entry = prev;
2115 te = SIMPLEQ_FIRST(&entries->head);
2117 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2118 *first_displayed_entry = NULL;
2121 static void
2122 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2123 struct got_tree_entry *last_displayed_entry,
2124 const struct got_tree_entries *entries)
2126 struct got_tree_entry *next;
2127 int n = 0;
2129 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2130 return;
2132 if (*first_displayed_entry)
2133 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2134 else
2135 next = SIMPLEQ_FIRST(&entries->head);
2136 while (next) {
2137 *first_displayed_entry = next;
2138 if (++n >= maxscroll)
2139 break;
2140 next = SIMPLEQ_NEXT(next, entry);
2144 struct tog_parent_tree {
2145 TAILQ_ENTRY(tog_parent_tree) entry;
2146 struct got_tree_object *tree;
2147 struct got_tree_entry *first_displayed_entry;
2148 struct got_tree_entry *selected_entry;
2149 int selected;
2152 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2154 static const struct got_error *
2155 tree_entry_path(char **path, struct tog_parent_trees *parents,
2156 struct got_tree_entry *te)
2158 const struct got_error *err = NULL;
2159 struct tog_parent_tree *pt;
2160 size_t len = 2; /* for leading slash and NUL */
2162 TAILQ_FOREACH(pt, parents, entry)
2163 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2164 if (te)
2165 len += strlen(te->name);
2167 *path = calloc(1, len);
2168 if (path == NULL)
2169 return got_error_from_errno();
2171 (*path)[0] = '/';
2172 pt = TAILQ_LAST(parents, tog_parent_trees);
2173 while (pt) {
2174 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2175 err = got_error(GOT_ERR_NO_SPACE);
2176 goto done;
2178 if (strlcat(*path, "/", len) >= len) {
2179 err = got_error(GOT_ERR_NO_SPACE);
2180 goto done;
2182 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2184 if (te) {
2185 if (strlcat(*path, te->name, len) >= len) {
2186 err = got_error(GOT_ERR_NO_SPACE);
2187 goto done;
2190 done:
2191 if (err) {
2192 free(*path);
2193 *path = NULL;
2195 return err;
2198 static const struct got_error *
2199 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2200 struct got_object_id *commit_id, struct got_repository *repo)
2202 const struct got_error *err = NULL;
2203 char *path;
2205 err = tree_entry_path(&path, parents, te);
2206 if (err)
2207 return err;
2209 err = show_blame_view(path, commit_id, repo);
2210 free(path);
2211 return err;
2214 static const struct got_error *
2215 log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2216 struct got_object_id *commit_id, struct got_repository *repo)
2218 const struct got_error *err = NULL;
2219 char *path;
2221 err = tree_entry_path(&path, parents, te);
2222 if (err)
2223 return err;
2225 err = show_log_view(commit_id, repo, path);
2226 free(path);
2227 return err;
2230 static const struct got_error *
2231 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2232 struct got_repository *repo)
2234 const struct got_error *err = NULL;
2235 int ch, done = 0, selected = 0, show_ids = 0;
2236 struct got_tree_object *tree = root;
2237 const struct got_tree_entries *entries;
2238 struct got_tree_entry *first_displayed_entry = NULL;
2239 struct got_tree_entry *last_displayed_entry = NULL;
2240 struct got_tree_entry *selected_entry = NULL;
2241 char *commit_id_str = NULL, *tree_label = NULL;
2242 int nentries, ndisplayed;
2243 struct tog_parent_trees parents;
2244 struct tog_view *view = NULL;
2246 TAILQ_INIT(&parents);
2248 err = got_object_id_str(&commit_id_str, commit_id);
2249 if (err != NULL)
2250 goto done;
2252 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2253 err = got_error_from_errno();
2254 goto done;
2257 view = open_view(0, 0, 0, 0);
2258 if (view == NULL) {
2259 err = got_error_from_errno();
2260 goto done;
2262 show_panel(view->panel);
2264 entries = got_object_tree_get_entries(root);
2265 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2266 while (!done) {
2267 char *parent_path;
2268 entries = got_object_tree_get_entries(tree);
2269 nentries = entries->nentries;
2270 if (tree != root)
2271 nentries++; /* '..' directory */
2273 err = tree_entry_path(&parent_path, &parents, NULL);
2274 if (err)
2275 goto done;
2277 err = draw_tree_entries(view, &first_displayed_entry,
2278 &last_displayed_entry, &selected_entry, &ndisplayed,
2279 tree_label, show_ids, parent_path, entries, selected,
2280 view->nlines, tree == root);
2281 free(parent_path);
2282 if (err)
2283 break;
2285 nodelay(stdscr, FALSE);
2286 ch = wgetch(view->window);
2287 nodelay(stdscr, TRUE);
2288 switch (ch) {
2289 case 'q':
2290 done = 1;
2291 break;
2292 case 'i':
2293 show_ids = !show_ids;
2294 break;
2295 case 'l':
2296 if (selected_entry) {
2297 err = log_tree_entry(selected_entry,
2298 &parents, commit_id, repo);
2299 if (err)
2300 goto done;
2302 break;
2303 case 'k':
2304 case KEY_UP:
2305 if (selected > 0)
2306 selected--;
2307 if (selected > 0)
2308 break;
2309 tree_scroll_up(&first_displayed_entry, 1,
2310 entries, tree == root);
2311 break;
2312 case KEY_PPAGE:
2313 if (SIMPLEQ_FIRST(&entries->head) ==
2314 first_displayed_entry) {
2315 if (tree != root)
2316 first_displayed_entry = NULL;
2317 selected = 0;
2318 break;
2320 tree_scroll_up(&first_displayed_entry,
2321 view->nlines, entries, tree == root);
2322 break;
2323 case 'j':
2324 case KEY_DOWN:
2325 if (selected < ndisplayed - 1) {
2326 selected++;
2327 break;
2329 tree_scroll_down(&first_displayed_entry, 1,
2330 last_displayed_entry, entries);
2331 break;
2332 case KEY_NPAGE:
2333 tree_scroll_down(&first_displayed_entry,
2334 view->nlines, last_displayed_entry,
2335 entries);
2336 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2337 break;
2338 /* can't scroll any further; move cursor down */
2339 if (selected < ndisplayed - 1)
2340 selected = ndisplayed - 1;
2341 break;
2342 case KEY_ENTER:
2343 case '\r':
2344 if (selected_entry == NULL) {
2345 struct tog_parent_tree *parent;
2346 case KEY_BACKSPACE:
2347 /* user selected '..' */
2348 if (tree == root)
2349 break;
2350 parent = TAILQ_FIRST(&parents);
2351 TAILQ_REMOVE(&parents, parent, entry);
2352 got_object_tree_close(tree);
2353 tree = parent->tree;
2354 first_displayed_entry =
2355 parent->first_displayed_entry;
2356 selected_entry = parent->selected_entry;
2357 selected = parent->selected;
2358 free(parent);
2359 } else if (S_ISDIR(selected_entry->mode)) {
2360 struct tog_parent_tree *parent;
2361 struct got_tree_object *child;
2362 err = got_object_open_as_tree(
2363 &child, repo, selected_entry->id);
2364 if (err)
2365 goto done;
2366 parent = calloc(1, sizeof(*parent));
2367 if (parent == NULL) {
2368 err = got_error_from_errno();
2369 goto done;
2371 parent->tree = tree;
2372 parent->first_displayed_entry =
2373 first_displayed_entry;
2374 parent->selected_entry = selected_entry;
2375 parent->selected = selected;
2376 TAILQ_INSERT_HEAD(&parents, parent,
2377 entry);
2378 tree = child;
2379 selected = 0;
2380 first_displayed_entry = NULL;
2381 } else if (S_ISREG(selected_entry->mode)) {
2382 err = blame_tree_entry(selected_entry,
2383 &parents, commit_id, repo);
2384 if (err)
2385 goto done;
2387 break;
2388 case KEY_RESIZE:
2389 err = view_resize(view);
2390 if (err)
2391 goto done;
2392 if (selected > view->nlines)
2393 selected = ndisplayed - 1;
2394 break;
2395 default:
2396 break;
2399 done:
2400 if (view)
2401 close_view(view);
2402 free(tree_label);
2403 free(commit_id_str);
2404 while (!TAILQ_EMPTY(&parents)) {
2405 struct tog_parent_tree *parent;
2406 parent = TAILQ_FIRST(&parents);
2407 TAILQ_REMOVE(&parents, parent, entry);
2408 free(parent);
2411 if (tree != root)
2412 got_object_tree_close(tree);
2413 return err;
2416 __dead static void
2417 usage_tree(void)
2419 endwin();
2420 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2421 getprogname());
2422 exit(1);
2425 static const struct got_error *
2426 cmd_tree(int argc, char *argv[])
2428 const struct got_error *error;
2429 struct got_repository *repo = NULL;
2430 char *repo_path = NULL;
2431 struct got_object_id *commit_id = NULL;
2432 char *commit_id_arg = NULL;
2433 struct got_commit_object *commit = NULL;
2434 struct got_tree_object *tree = NULL;
2435 int ch;
2437 #ifndef PROFILE
2438 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2439 err(1, "pledge");
2440 #endif
2442 while ((ch = getopt(argc, argv, "c:")) != -1) {
2443 switch (ch) {
2444 case 'c':
2445 commit_id_arg = optarg;
2446 break;
2447 default:
2448 usage();
2449 /* NOTREACHED */
2453 argc -= optind;
2454 argv += optind;
2456 if (argc == 0) {
2457 repo_path = getcwd(NULL, 0);
2458 if (repo_path == NULL)
2459 return got_error_from_errno();
2460 } else if (argc == 1) {
2461 repo_path = realpath(argv[0], NULL);
2462 if (repo_path == NULL)
2463 return got_error_from_errno();
2464 } else
2465 usage_log();
2467 error = got_repo_open(&repo, repo_path);
2468 free(repo_path);
2469 if (error != NULL)
2470 return error;
2472 if (commit_id_arg == NULL) {
2473 error = get_head_commit_id(&commit_id, repo);
2474 if (error != NULL)
2475 goto done;
2476 } else {
2477 struct got_object *obj;
2478 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2479 if (error == NULL) {
2480 commit_id = got_object_get_id(obj);
2481 if (commit_id == NULL)
2482 error = got_error_from_errno();
2485 if (error != NULL)
2486 goto done;
2488 error = got_object_open_as_commit(&commit, repo, commit_id);
2489 if (error != NULL)
2490 goto done;
2492 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2493 if (error != NULL)
2494 goto done;
2496 error = show_tree_view(tree, commit_id, repo);
2497 done:
2498 free(commit_id);
2499 if (commit)
2500 got_object_commit_close(commit);
2501 if (tree)
2502 got_object_tree_close(tree);
2503 if (repo)
2504 got_repo_close(repo);
2505 return error;
2507 static void
2508 init_curses(void)
2510 initscr();
2511 cbreak();
2512 noecho();
2513 nonl();
2514 intrflush(stdscr, FALSE);
2515 keypad(stdscr, TRUE);
2516 curs_set(0);
2519 __dead static void
2520 usage(void)
2522 int i;
2524 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2525 "Available commands:\n", getprogname());
2526 for (i = 0; i < nitems(tog_commands); i++) {
2527 struct tog_cmd *cmd = &tog_commands[i];
2528 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2530 exit(1);
2533 static char **
2534 make_argv(const char *arg0, const char *arg1)
2536 char **argv;
2537 int argc = (arg1 == NULL ? 1 : 2);
2539 argv = calloc(argc, sizeof(char *));
2540 if (argv == NULL)
2541 err(1, "calloc");
2542 argv[0] = strdup(arg0);
2543 if (argv[0] == NULL)
2544 err(1, "calloc");
2545 if (arg1) {
2546 argv[1] = strdup(arg1);
2547 if (argv[1] == NULL)
2548 err(1, "calloc");
2551 return argv;
2554 int
2555 main(int argc, char *argv[])
2557 const struct got_error *error = NULL;
2558 struct tog_cmd *cmd = NULL;
2559 int ch, hflag = 0;
2560 char **cmd_argv = NULL;
2562 setlocale(LC_ALL, "");
2564 while ((ch = getopt(argc, argv, "h")) != -1) {
2565 switch (ch) {
2566 case 'h':
2567 hflag = 1;
2568 break;
2569 default:
2570 usage();
2571 /* NOTREACHED */
2575 argc -= optind;
2576 argv += optind;
2577 optind = 0;
2578 optreset = 1;
2580 if (argc == 0) {
2581 if (hflag)
2582 usage();
2583 /* Build an argument vector which runs a default command. */
2584 cmd = &tog_commands[0];
2585 cmd_argv = make_argv(cmd->name, NULL);
2586 argc = 1;
2587 } else {
2588 int i;
2590 /* Did the user specific a command? */
2591 for (i = 0; i < nitems(tog_commands); i++) {
2592 if (strncmp(tog_commands[i].name, argv[0],
2593 strlen(argv[0])) == 0) {
2594 cmd = &tog_commands[i];
2595 if (hflag)
2596 tog_commands[i].cmd_usage();
2597 break;
2600 if (cmd == NULL) {
2601 /* Did the user specify a repository? */
2602 char *repo_path = realpath(argv[0], NULL);
2603 if (repo_path) {
2604 struct got_repository *repo;
2605 error = got_repo_open(&repo, repo_path);
2606 if (error == NULL)
2607 got_repo_close(repo);
2608 } else
2609 error = got_error_from_errno();
2610 if (error) {
2611 if (hflag) {
2612 fprintf(stderr, "%s: '%s' is not a "
2613 "known command\n", getprogname(),
2614 argv[0]);
2615 usage();
2617 fprintf(stderr, "%s: '%s' is neither a known "
2618 "command nor a path to a repository\n",
2619 getprogname(), argv[0]);
2620 free(repo_path);
2621 return 1;
2623 cmd = &tog_commands[0];
2624 cmd_argv = make_argv(cmd->name, repo_path);
2625 argc = 2;
2626 free(repo_path);
2630 init_curses();
2632 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2633 if (error)
2634 goto done;
2635 done:
2636 endwin();
2637 free(cmd_argv);
2638 if (error)
2639 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2640 return 0;