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 tog_view *, struct got_object_id *,
97 struct got_repository *, const char *);
98 static const struct got_error *
99 show_blame_view(struct tog_view *, const char *, struct got_object_id *,
100 struct got_repository *);
101 static const struct got_error *
102 show_tree_view(struct got_tree_object *, struct got_object_id *,
103 struct got_repository *);
105 static void
106 close_view(struct tog_view *view)
108 if (view->panel)
109 del_panel(view->panel);
110 if (view->window)
111 delwin(view->window);
112 free(view);
115 static struct tog_view *
116 open_view(int nlines, int ncols, int begin_y, int begin_x)
118 struct tog_view *view = malloc(sizeof(*view));
120 if (view == NULL)
121 return NULL;
123 view->lines = LINES;
124 view->cols = COLS;
125 view->nlines = nlines ? nlines : LINES - begin_y;
126 view->ncols = ncols ? ncols : COLS - begin_x;
127 view->begin_y = begin_y;
128 view->begin_x = begin_x;
129 view->window = newwin(nlines, ncols, begin_y, begin_x);
130 if (view->window == NULL) {
131 close_view(view);
132 return NULL;
134 view->panel = new_panel(view->window);
135 if (view->panel == NULL) {
136 close_view(view);
137 return NULL;
140 keypad(view->window, TRUE);
141 return view;
144 const struct got_error *
145 view_resize(struct tog_view *view)
147 int nlines, ncols;
149 if (view->lines > LINES)
150 nlines = view->nlines - (view->lines - LINES);
151 else
152 nlines = view->nlines + (LINES - view->lines);
154 if (view->cols > COLS)
155 ncols = view->ncols - (view->cols - COLS);
156 else
157 ncols = view->ncols + (COLS - view->cols);
159 if (wresize(view->window, nlines, ncols) == ERR)
160 return got_error_from_errno();
162 view->nlines = nlines;
163 view->ncols = ncols;
164 view->lines = LINES;
165 view->cols = COLS;
166 return NULL;
169 __dead static void
170 usage_log(void)
172 endwin();
173 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
174 getprogname());
175 exit(1);
178 /* Create newly allocated wide-character string equivalent to a byte string. */
179 static const struct got_error *
180 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
182 char *vis = NULL;
183 const struct got_error *err = NULL;
185 *ws = NULL;
186 *wlen = mbstowcs(NULL, s, 0);
187 if (*wlen == (size_t)-1) {
188 int vislen;
189 if (errno != EILSEQ)
190 return got_error_from_errno();
192 /* byte string invalid in current encoding; try to "fix" it */
193 err = got_mbsavis(&vis, &vislen, s);
194 if (err)
195 return err;
196 *wlen = mbstowcs(NULL, vis, 0);
197 if (*wlen == (size_t)-1) {
198 err = got_error_from_errno(); /* give up */
199 goto done;
203 *ws = calloc(*wlen + 1, sizeof(*ws));
204 if (*ws == NULL) {
205 err = got_error_from_errno();
206 goto done;
209 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
210 err = got_error_from_errno();
211 done:
212 free(vis);
213 if (err) {
214 free(*ws);
215 *ws = NULL;
216 *wlen = 0;
218 return err;
221 /* Format a line for display, ensuring that it won't overflow a width limit. */
222 static const struct got_error *
223 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
225 const struct got_error *err = NULL;
226 int cols = 0;
227 wchar_t *wline = NULL;
228 size_t wlen;
229 int i;
231 *wlinep = NULL;
232 *widthp = 0;
234 err = mbs2ws(&wline, &wlen, line);
235 if (err)
236 return err;
238 i = 0;
239 while (i < wlen && cols < wlimit) {
240 int width = wcwidth(wline[i]);
241 switch (width) {
242 case 0:
243 i++;
244 break;
245 case 1:
246 case 2:
247 if (cols + width <= wlimit) {
248 cols += width;
249 i++;
251 break;
252 case -1:
253 if (wline[i] == L'\t')
254 cols += TABSIZE - ((cols + 1) % TABSIZE);
255 i++;
256 break;
257 default:
258 err = got_error_from_errno();
259 goto done;
262 wline[i] = L'\0';
263 if (widthp)
264 *widthp = cols;
265 done:
266 if (err)
267 free(wline);
268 else
269 *wlinep = wline;
270 return err;
273 static const struct got_error *
274 draw_commit(struct tog_view *view, struct got_commit_object *commit,
275 struct got_object_id *id)
277 const struct got_error *err = NULL;
278 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
279 char *logmsg0 = NULL, *logmsg = NULL;
280 char *author0 = NULL, *author = NULL;
281 wchar_t *wlogmsg = NULL, *wauthor = NULL;
282 int author_width, logmsg_width;
283 char *newline, *smallerthan;
284 char *line = NULL;
285 int col, limit;
286 static const size_t date_display_cols = 9;
287 static const size_t author_display_cols = 16;
288 const int avail = view->ncols;
290 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
291 >= sizeof(datebuf))
292 return got_error(GOT_ERR_NO_SPACE);
294 if (avail < date_display_cols)
295 limit = MIN(sizeof(datebuf) - 1, avail);
296 else
297 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
298 waddnstr(view->window, datebuf, limit);
299 col = limit + 1;
300 if (col > avail)
301 goto done;
303 author0 = strdup(commit->author);
304 if (author0 == NULL) {
305 err = got_error_from_errno();
306 goto done;
308 author = author0;
309 smallerthan = strchr(author, '<');
310 if (smallerthan)
311 *smallerthan = '\0';
312 else {
313 char *at = strchr(author, '@');
314 if (at)
315 *at = '\0';
317 limit = avail - col;
318 err = format_line(&wauthor, &author_width, author, limit);
319 if (err)
320 goto done;
321 waddwstr(view->window, wauthor);
322 col += author_width;
323 while (col <= avail && author_width < author_display_cols + 1) {
324 waddch(view->window, ' ');
325 col++;
326 author_width++;
328 if (col > avail)
329 goto done;
331 logmsg0 = strdup(commit->logmsg);
332 if (logmsg0 == NULL) {
333 err = got_error_from_errno();
334 goto done;
336 logmsg = logmsg0;
337 while (*logmsg == '\n')
338 logmsg++;
339 newline = strchr(logmsg, '\n');
340 if (newline)
341 *newline = '\0';
342 limit = avail - col;
343 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
344 if (err)
345 goto done;
346 waddwstr(view->window, wlogmsg);
347 col += logmsg_width;
348 while (col <= avail) {
349 waddch(view->window, ' ');
350 col++;
352 done:
353 free(logmsg0);
354 free(wlogmsg);
355 free(author0);
356 free(wauthor);
357 free(line);
358 return err;
361 struct commit_queue_entry {
362 TAILQ_ENTRY(commit_queue_entry) entry;
363 struct got_object_id *id;
364 struct got_commit_object *commit;
365 };
366 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
367 struct commit_queue {
368 int ncommits;
369 struct commit_queue_head head;
370 };
372 static struct commit_queue_entry *
373 alloc_commit_queue_entry(struct got_commit_object *commit,
374 struct got_object_id *id)
376 struct commit_queue_entry *entry;
378 entry = calloc(1, sizeof(*entry));
379 if (entry == NULL)
380 return NULL;
382 entry->id = id;
383 entry->commit = commit;
384 return entry;
387 static void
388 pop_commit(struct commit_queue *commits)
390 struct commit_queue_entry *entry;
392 entry = TAILQ_FIRST(&commits->head);
393 TAILQ_REMOVE(&commits->head, entry, entry);
394 got_object_commit_close(entry->commit);
395 commits->ncommits--;
396 /* Don't free entry->id! It is owned by the commit graph. */
397 free(entry);
400 static void
401 free_commits(struct commit_queue *commits)
403 while (!TAILQ_EMPTY(&commits->head))
404 pop_commit(commits);
407 static const struct got_error *
408 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
409 struct got_object_id *start_id, int minqueue, int init,
410 struct got_repository *repo, const char *path)
412 const struct got_error *err = NULL;
413 struct got_object_id *id;
414 struct commit_queue_entry *entry;
415 int nfetched, nqueued = 0, found_obj = 0;
416 int is_root_path = strcmp(path, "/") == 0;
418 err = got_commit_graph_iter_start(graph, start_id);
419 if (err)
420 return err;
422 entry = TAILQ_LAST(&commits->head, commit_queue_head);
423 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
424 int nfetched;
426 /* Start ID's commit is already on the queue; skip over it. */
427 err = got_commit_graph_iter_next(&id, graph);
428 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
429 return err;
431 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
432 if (err)
433 return err;
436 while (1) {
437 struct got_commit_object *commit;
439 err = got_commit_graph_iter_next(&id, graph);
440 if (err) {
441 if (err->code != GOT_ERR_ITER_NEED_MORE)
442 break;
443 if (nqueued >= minqueue) {
444 err = NULL;
445 break;
447 err = got_commit_graph_fetch_commits(&nfetched,
448 graph, 1, repo);
449 if (err)
450 return err;
451 continue;
453 if (id == NULL)
454 break;
456 err = got_object_open_as_commit(&commit, repo, id);
457 if (err)
458 break;
460 if (!is_root_path) {
461 struct got_object *obj;
462 struct got_object_qid *pid;
463 int changed = 0;
465 err = got_object_open_by_path(&obj, repo, id, path);
466 if (err) {
467 got_object_commit_close(commit);
468 if (err->code == GOT_ERR_NO_OBJ &&
469 (found_obj || !init)) {
470 /* History stops here. */
471 err = got_error(GOT_ERR_ITER_COMPLETED);
473 break;
475 found_obj = 1;
477 pid = SIMPLEQ_FIRST(&commit->parent_ids);
478 if (pid != NULL) {
479 struct got_object *pobj;
480 err = got_object_open_by_path(&pobj, repo,
481 pid->id, path);
482 if (err) {
483 if (err->code != GOT_ERR_NO_OBJ) {
484 got_object_close(obj);
485 got_object_commit_close(commit);
486 break;
488 err = NULL;
489 changed = 1;
490 } else {
491 struct got_object_id *id, *pid;
492 id = got_object_get_id(obj);
493 if (id == NULL) {
494 err = got_error_from_errno();
495 got_object_close(obj);
496 got_object_close(pobj);
497 break;
499 pid = got_object_get_id(pobj);
500 if (pid == NULL) {
501 err = got_error_from_errno();
502 free(id);
503 got_object_close(obj);
504 got_object_close(pobj);
505 break;
507 changed =
508 (got_object_id_cmp(id, pid) != 0);
509 got_object_close(pobj);
510 free(id);
511 free(pid);
514 got_object_close(obj);
515 if (!changed) {
516 got_object_commit_close(commit);
517 continue;
521 entry = alloc_commit_queue_entry(commit, id);
522 if (entry == NULL) {
523 err = got_error_from_errno();
524 break;
526 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
527 nqueued++;
528 commits->ncommits++;
531 return err;
534 static const struct got_error *
535 fetch_next_commit(struct commit_queue_entry **pentry,
536 struct commit_queue_entry *entry, struct commit_queue *commits,
537 struct got_commit_graph *graph, struct got_repository *repo,
538 const char *path)
540 const struct got_error *err = NULL;
542 *pentry = NULL;
544 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
545 if (err)
546 return err;
548 /* Next entry to display should now be available. */
549 *pentry = TAILQ_NEXT(entry, entry);
550 if (*pentry == NULL)
551 return got_error(GOT_ERR_NO_OBJ);
553 return NULL;
556 static const struct got_error *
557 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
559 const struct got_error *err = NULL;
560 struct got_reference *head_ref;
562 *head_id = NULL;
564 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
565 if (err)
566 return err;
568 err = got_ref_resolve(head_id, repo, head_ref);
569 got_ref_close(head_ref);
570 if (err) {
571 *head_id = NULL;
572 return err;
575 return NULL;
578 static const struct got_error *
579 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
580 struct commit_queue_entry **selected, struct commit_queue_entry *first,
581 struct commit_queue *commits, int selected_idx, int limit,
582 struct got_commit_graph *graph, struct got_repository *repo,
583 const char *path)
585 const struct got_error *err = NULL;
586 struct commit_queue_entry *entry;
587 int ncommits, width;
588 char *id_str, *header;
589 wchar_t *wline;
591 entry = first;
592 ncommits = 0;
593 while (entry) {
594 if (ncommits == selected_idx) {
595 *selected = entry;
596 break;
598 entry = TAILQ_NEXT(entry, entry);
599 ncommits++;
602 err = got_object_id_str(&id_str, (*selected)->id);
603 if (err)
604 return err;
606 if (path) {
607 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
608 err = got_error_from_errno();
609 free(id_str);
610 return err;
612 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
613 err = got_error_from_errno();
614 free(id_str);
615 return err;
617 free(id_str);
618 err = format_line(&wline, &width, header, view->ncols);
619 if (err) {
620 free(header);
621 return err;
623 free(header);
625 werase(view->window);
627 waddwstr(view->window, wline);
628 if (width < view->ncols)
629 waddch(view->window, '\n');
630 free(wline);
631 if (limit <= 1)
632 return NULL;
634 entry = first;
635 *last = first;
636 ncommits = 0;
637 while (entry) {
638 if (ncommits >= limit - 1)
639 break;
640 if (ncommits == selected_idx)
641 wstandout(view->window);
642 err = draw_commit(view, entry->commit, entry->id);
643 if (ncommits == selected_idx)
644 wstandend(view->window);
645 if (err)
646 break;
647 ncommits++;
648 *last = entry;
649 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
650 err = queue_commits(graph, commits, entry->id, 1,
651 0, repo, path);
652 if (err) {
653 if (err->code != GOT_ERR_ITER_COMPLETED)
654 return err;
655 err = NULL;
658 entry = TAILQ_NEXT(entry, entry);
661 update_panels();
662 doupdate();
664 return err;
667 static void
668 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
669 struct commit_queue *commits)
671 struct commit_queue_entry *entry;
672 int nscrolled = 0;
674 entry = TAILQ_FIRST(&commits->head);
675 if (*first_displayed_entry == entry)
676 return;
678 entry = *first_displayed_entry;
679 while (entry && nscrolled < maxscroll) {
680 entry = TAILQ_PREV(entry, commit_queue_head, entry);
681 if (entry) {
682 *first_displayed_entry = entry;
683 nscrolled++;
688 static const struct got_error *
689 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
690 struct commit_queue_entry *last_displayed_entry,
691 struct commit_queue *commits, struct got_commit_graph *graph,
692 struct got_repository *repo, const char *path)
694 const struct got_error *err = NULL;
695 struct commit_queue_entry *pentry;
696 int nscrolled = 0;
698 do {
699 pentry = TAILQ_NEXT(last_displayed_entry, entry);
700 if (pentry == NULL) {
701 err = fetch_next_commit(&pentry, last_displayed_entry,
702 commits, graph, repo, path);
703 if (err || pentry == NULL)
704 break;
706 last_displayed_entry = pentry;
708 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
709 if (pentry == NULL)
710 break;
711 *first_displayed_entry = pentry;
712 } while (++nscrolled < maxscroll);
714 return err;
717 static const struct got_error *
718 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
720 const struct got_error *err;
721 struct got_object *obj1 = NULL, *obj2 = NULL;
722 struct got_object_qid *parent_id;
723 struct tog_view *view;
725 err = got_object_open(&obj2, repo, entry->id);
726 if (err)
727 return err;
729 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
730 if (parent_id) {
731 err = got_object_open(&obj1, repo, parent_id->id);
732 if (err)
733 goto done;
736 view = open_view(0, 0, 0, 0);
737 if (view == NULL) {
738 err = got_error_from_errno();
739 goto done;
742 err = show_diff_view(view, obj1, obj2, repo);
743 close_view(view);
744 done:
745 if (obj1)
746 got_object_close(obj1);
747 if (obj2)
748 got_object_close(obj2);
749 return err;
752 static const struct got_error *
753 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
755 const struct got_error *err = NULL;
756 struct got_tree_object *tree;
758 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
759 if (err)
760 return err;
762 err = show_tree_view(tree, entry->id, repo);
763 got_object_tree_close(tree);
764 return err;
767 static const struct got_error *
768 show_log_view(struct tog_view *view, struct got_object_id *start_id,
769 struct got_repository *repo, const char *path)
771 const struct got_error *err = NULL;
772 struct got_object_id *head_id = NULL;
773 int ch, done = 0, selected = 0, nfetched;
774 struct got_commit_graph *graph = NULL;
775 struct commit_queue commits;
776 struct commit_queue_entry *first_displayed_entry = NULL;
777 struct commit_queue_entry *last_displayed_entry = NULL;
778 struct commit_queue_entry *selected_entry = NULL;
779 char *in_repo_path = NULL;
781 err = got_repo_map_path(&in_repo_path, repo, path);
782 if (err != NULL)
783 goto done;
785 err = get_head_commit_id(&head_id, repo);
786 if (err)
787 return err;
789 /* The graph contains all commits. */
790 err = got_commit_graph_open(&graph, head_id, 0, repo);
791 if (err)
792 goto done;
793 /* The commit queue contains a subset of commits filtered by path. */
794 TAILQ_INIT(&commits.head);
795 commits.ncommits = 0;
797 /* Populate commit graph with a sufficient number of commits. */
798 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
799 repo);
800 if (err)
801 goto done;
803 /*
804 * Open the initial batch of commits, sorted in commit graph order.
805 * We keep all commits open throughout the lifetime of the log view
806 * in order to avoid having to re-fetch commits from disk while
807 * updating the display.
808 */
809 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
810 in_repo_path);
811 if (err) {
812 if (err->code != GOT_ERR_ITER_COMPLETED)
813 goto done;
814 err = NULL;
817 show_panel(view->panel);
819 first_displayed_entry = TAILQ_FIRST(&commits.head);
820 selected_entry = first_displayed_entry;
821 while (!done) {
822 err = draw_commits(view, &last_displayed_entry, &selected_entry,
823 first_displayed_entry, &commits, selected, view->nlines,
824 graph, repo, in_repo_path);
825 if (err)
826 goto done;
828 nodelay(stdscr, FALSE);
829 ch = wgetch(view->window);
830 nodelay(stdscr, TRUE);
831 switch (ch) {
832 case ERR:
833 break;
834 case 'q':
835 done = 1;
836 break;
837 case 'k':
838 case KEY_UP:
839 if (selected > 0)
840 selected--;
841 if (selected > 0)
842 break;
843 scroll_up(&first_displayed_entry, 1, &commits);
844 break;
845 case KEY_PPAGE:
846 if (TAILQ_FIRST(&commits.head) ==
847 first_displayed_entry) {
848 selected = 0;
849 break;
851 scroll_up(&first_displayed_entry, view->nlines,
852 &commits);
853 break;
854 case 'j':
855 case KEY_DOWN:
856 if (selected < MIN(view->nlines - 2,
857 commits.ncommits - 1)) {
858 selected++;
859 break;
861 err = scroll_down(&first_displayed_entry, 1,
862 last_displayed_entry, &commits, graph,
863 repo, in_repo_path);
864 if (err) {
865 if (err->code != GOT_ERR_ITER_COMPLETED)
866 goto done;
867 err = NULL;
869 break;
870 case KEY_NPAGE: {
871 struct commit_queue_entry *first = first_displayed_entry;
872 err = scroll_down(&first_displayed_entry, view->nlines,
873 last_displayed_entry, &commits, graph,
874 repo, in_repo_path);
875 if (err) {
876 if (err->code != GOT_ERR_ITER_COMPLETED)
877 goto done;
878 /* can't scroll any further; move cursor down */
879 if (first == first_displayed_entry && selected <
880 MIN(view->nlines - 2, commits.ncommits - 1)) {
881 selected = MIN(view->nlines - 2,
882 commits.ncommits - 1);
884 err = NULL;
886 break;
888 case KEY_RESIZE:
889 err = view_resize(view);
890 if (err)
891 goto done;
892 if (selected > view->nlines - 2)
893 selected = view->nlines - 2;
894 if (selected > commits.ncommits - 1)
895 selected = commits.ncommits - 1;
896 break;
897 case KEY_ENTER:
898 case '\r':
899 err = show_commit(selected_entry, repo);
900 if (err)
901 goto done;
902 show_panel(view->panel);
903 break;
904 case 't':
905 err = browse_commit(selected_entry, repo);
906 if (err)
907 goto done;
908 show_panel(view->panel);
909 break;
910 default:
911 break;
914 done:
915 free(head_id);
916 if (graph)
917 got_commit_graph_close(graph);
918 free_commits(&commits);
919 free(in_repo_path);
920 return err;
923 static const struct got_error *
924 cmd_log(int argc, char *argv[])
926 const struct got_error *error;
927 struct got_repository *repo = NULL;
928 struct got_object_id *start_id = NULL;
929 char *path = NULL, *repo_path = NULL, *cwd = NULL;
930 char *start_commit = NULL;
931 int ch;
932 struct tog_view *view;
934 #ifndef PROFILE
935 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
936 err(1, "pledge");
937 #endif
939 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
940 switch (ch) {
941 case 'c':
942 start_commit = optarg;
943 break;
944 case 'r':
945 repo_path = realpath(optarg, NULL);
946 if (repo_path == NULL)
947 err(1, "-r option");
948 break;
949 default:
950 usage();
951 /* NOTREACHED */
955 argc -= optind;
956 argv += optind;
958 if (argc == 0)
959 path = strdup("");
960 else if (argc == 1)
961 path = strdup(argv[0]);
962 else
963 usage_log();
964 if (path == NULL)
965 return got_error_from_errno();
967 cwd = getcwd(NULL, 0);
968 if (cwd == NULL) {
969 error = got_error_from_errno();
970 goto done;
972 if (repo_path == NULL) {
973 repo_path = strdup(cwd);
974 if (repo_path == NULL) {
975 error = got_error_from_errno();
976 goto done;
980 error = got_repo_open(&repo, repo_path);
981 if (error != NULL)
982 goto done;
984 if (start_commit == NULL) {
985 error = get_head_commit_id(&start_id, repo);
986 if (error != NULL)
987 goto done;
988 } else {
989 struct got_object *obj;
990 error = got_object_open_by_id_str(&obj, repo, start_commit);
991 if (error == NULL) {
992 start_id = got_object_get_id(obj);
993 if (start_id == NULL)
994 error = got_error_from_errno();
995 goto done;
998 if (error != NULL)
999 goto done;
1001 view = open_view(0, 0, 0, 0);
1002 if (view == NULL) {
1003 error = got_error_from_errno();
1004 goto done;
1006 error = show_log_view(view, start_id, repo, path);
1007 close_view(view);
1008 done:
1009 free(repo_path);
1010 free(cwd);
1011 free(path);
1012 free(start_id);
1013 if (repo)
1014 got_repo_close(repo);
1015 return error;
1018 __dead static void
1019 usage_diff(void)
1021 endwin();
1022 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1023 getprogname());
1024 exit(1);
1027 static char *
1028 parse_next_line(FILE *f, size_t *len)
1030 char *line;
1031 size_t linelen;
1032 size_t lineno;
1033 const char delim[3] = { '\0', '\0', '\0'};
1035 line = fparseln(f, &linelen, &lineno, delim, 0);
1036 if (len)
1037 *len = linelen;
1038 return line;
1041 static const struct got_error *
1042 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1043 int *last_displayed_line, int *eof, int max_lines)
1045 const struct got_error *err;
1046 int nlines = 0, nprinted = 0;
1047 char *line;
1048 size_t len;
1049 wchar_t *wline;
1050 int width;
1052 rewind(f);
1053 werase(view->window);
1055 *eof = 0;
1056 while (nprinted < max_lines) {
1057 line = parse_next_line(f, &len);
1058 if (line == NULL) {
1059 *eof = 1;
1060 break;
1062 if (++nlines < *first_displayed_line) {
1063 free(line);
1064 continue;
1067 err = format_line(&wline, &width, line, view->ncols);
1068 if (err) {
1069 free(line);
1070 free(wline);
1071 return err;
1073 waddwstr(view->window, wline);
1074 if (width < view->ncols)
1075 waddch(view->window, '\n');
1076 if (++nprinted == 1)
1077 *first_displayed_line = nlines;
1078 free(line);
1079 free(wline);
1080 wline = NULL;
1082 *last_displayed_line = nlines;
1084 update_panels();
1085 doupdate();
1087 return NULL;
1090 static const struct got_error *
1091 show_diff_view(struct tog_view *view, struct got_object *obj1,
1092 struct got_object *obj2, struct got_repository *repo)
1094 const struct got_error *err;
1095 FILE *f;
1096 int ch, done = 0;
1097 int first_displayed_line = 1, last_displayed_line = view->nlines;
1098 int eof, i;
1100 if (obj1 != NULL && obj2 != NULL &&
1101 got_object_get_type(obj1) != got_object_get_type(obj2))
1102 return got_error(GOT_ERR_OBJ_TYPE);
1104 f = got_opentemp();
1105 if (f == NULL)
1106 return got_error_from_errno();
1108 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1109 case GOT_OBJ_TYPE_BLOB:
1110 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1111 break;
1112 case GOT_OBJ_TYPE_TREE:
1113 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1114 break;
1115 case GOT_OBJ_TYPE_COMMIT:
1116 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1117 break;
1118 default:
1119 return got_error(GOT_ERR_OBJ_TYPE);
1122 fflush(f);
1124 show_panel(view->panel);
1126 while (!done) {
1127 err = draw_file(view, f, &first_displayed_line,
1128 &last_displayed_line, &eof, view->nlines);
1129 if (err)
1130 break;
1131 nodelay(stdscr, FALSE);
1132 ch = wgetch(view->window);
1133 nodelay(stdscr, TRUE);
1134 switch (ch) {
1135 case 'q':
1136 done = 1;
1137 break;
1138 case 'k':
1139 case KEY_UP:
1140 if (first_displayed_line > 1)
1141 first_displayed_line--;
1142 break;
1143 case KEY_PPAGE:
1144 case KEY_BACKSPACE:
1145 i = 0;
1146 while (i++ < view->nlines - 1 &&
1147 first_displayed_line > 1)
1148 first_displayed_line--;
1149 break;
1150 case 'j':
1151 case KEY_DOWN:
1152 if (!eof)
1153 first_displayed_line++;
1154 break;
1155 case KEY_NPAGE:
1156 case ' ':
1157 i = 0;
1158 while (!eof && i++ < view->nlines - 1) {
1159 char *line = parse_next_line(f, NULL);
1160 first_displayed_line++;
1161 if (line == NULL)
1162 break;
1164 break;
1165 case KEY_RESIZE:
1166 err = view_resize(view);
1167 if (err)
1168 goto done;
1169 break;
1170 default:
1171 break;
1174 done:
1175 fclose(f);
1176 return err;
1179 static const struct got_error *
1180 cmd_diff(int argc, char *argv[])
1182 const struct got_error *error = NULL;
1183 struct got_repository *repo = NULL;
1184 struct got_object *obj1 = NULL, *obj2 = NULL;
1185 char *repo_path = NULL;
1186 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1187 int ch;
1188 struct tog_view *view;
1190 #ifndef PROFILE
1191 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1192 err(1, "pledge");
1193 #endif
1195 while ((ch = getopt(argc, argv, "")) != -1) {
1196 switch (ch) {
1197 default:
1198 usage();
1199 /* NOTREACHED */
1203 argc -= optind;
1204 argv += optind;
1206 if (argc == 0) {
1207 usage_diff(); /* TODO show local worktree changes */
1208 } else if (argc == 2) {
1209 repo_path = getcwd(NULL, 0);
1210 if (repo_path == NULL)
1211 return got_error_from_errno();
1212 obj_id_str1 = argv[0];
1213 obj_id_str2 = argv[1];
1214 } else if (argc == 3) {
1215 repo_path = realpath(argv[0], NULL);
1216 if (repo_path == NULL)
1217 return got_error_from_errno();
1218 obj_id_str1 = argv[1];
1219 obj_id_str2 = argv[2];
1220 } else
1221 usage_diff();
1223 error = got_repo_open(&repo, repo_path);
1224 free(repo_path);
1225 if (error)
1226 goto done;
1228 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1229 if (error)
1230 goto done;
1232 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1233 if (error)
1234 goto done;
1236 view = open_view(0, 0, 0, 0);
1237 if (view == NULL) {
1238 error = got_error_from_errno();
1239 goto done;
1241 error = show_diff_view(view, obj1, obj2, repo);
1242 close_view(view);
1243 done:
1244 got_repo_close(repo);
1245 if (obj1)
1246 got_object_close(obj1);
1247 if (obj2)
1248 got_object_close(obj2);
1249 return error;
1252 __dead static void
1253 usage_blame(void)
1255 endwin();
1256 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1257 getprogname());
1258 exit(1);
1261 struct tog_blame_line {
1262 int annotated;
1263 struct got_object_id *id;
1266 static const struct got_error *
1267 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1268 const char *path, struct tog_blame_line *lines, int nlines,
1269 int blame_complete, int selected_line, int *first_displayed_line,
1270 int *last_displayed_line, int *eof, int max_lines)
1272 const struct got_error *err;
1273 int lineno = 0, nprinted = 0;
1274 char *line;
1275 size_t len;
1276 wchar_t *wline;
1277 int width, wlimit;
1278 struct tog_blame_line *blame_line;
1279 struct got_object_id *prev_id = NULL;
1280 char *id_str;
1282 err = got_object_id_str(&id_str, id);
1283 if (err)
1284 return err;
1286 rewind(f);
1287 werase(view->window);
1289 if (asprintf(&line, "commit: %s", id_str) == -1) {
1290 err = got_error_from_errno();
1291 free(id_str);
1292 return err;
1295 err = format_line(&wline, &width, line, view->ncols);
1296 free(line);
1297 line = NULL;
1298 waddwstr(view->window, wline);
1299 free(wline);
1300 wline = NULL;
1301 if (width < view->ncols)
1302 waddch(view->window, '\n');
1304 if (asprintf(&line, "[%d/%d] %s%s",
1305 *first_displayed_line - 1 + selected_line, nlines,
1306 blame_complete ? "" : "annotating ", path) == -1) {
1307 free(id_str);
1308 return got_error_from_errno();
1310 free(id_str);
1311 err = format_line(&wline, &width, line, view->ncols);
1312 free(line);
1313 line = NULL;
1314 if (err)
1315 return err;
1316 waddwstr(view->window, wline);
1317 free(wline);
1318 wline = NULL;
1319 if (width < view->ncols)
1320 waddch(view->window, '\n');
1322 *eof = 0;
1323 while (nprinted < max_lines - 2) {
1324 line = parse_next_line(f, &len);
1325 if (line == NULL) {
1326 *eof = 1;
1327 break;
1329 if (++lineno < *first_displayed_line) {
1330 free(line);
1331 continue;
1334 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1335 err = format_line(&wline, &width, line, wlimit);
1336 if (err) {
1337 free(line);
1338 return err;
1341 if (nprinted == selected_line - 1)
1342 wstandout(view->window);
1344 blame_line = &lines[lineno - 1];
1345 if (blame_line->annotated && prev_id &&
1346 got_object_id_cmp(prev_id, blame_line->id) == 0)
1347 waddstr(view->window, " ");
1348 else if (blame_line->annotated) {
1349 char *id_str;
1350 err = got_object_id_str(&id_str, blame_line->id);
1351 if (err) {
1352 free(line);
1353 free(wline);
1354 return err;
1356 wprintw(view->window, "%.8s ", id_str);
1357 free(id_str);
1358 prev_id = blame_line->id;
1359 } else {
1360 waddstr(view->window, "........ ");
1361 prev_id = NULL;
1364 waddwstr(view->window, wline);
1365 while (width < wlimit) {
1366 waddch(view->window, ' ');
1367 width++;
1369 if (nprinted == selected_line - 1)
1370 wstandend(view->window);
1371 if (++nprinted == 1)
1372 *first_displayed_line = lineno;
1373 free(line);
1374 free(wline);
1375 wline = NULL;
1377 *last_displayed_line = lineno;
1379 update_panels();
1380 doupdate();
1382 return NULL;
1385 struct tog_blame_cb_args {
1386 pthread_mutex_t *mutex;
1387 struct tog_blame_line *lines; /* one per line */
1388 int nlines;
1390 struct tog_view *view;
1391 struct got_object_id *commit_id;
1392 FILE *f;
1393 const char *path;
1394 int *first_displayed_line;
1395 int *last_displayed_line;
1396 int *selected_line;
1397 int *quit;
1400 static const struct got_error *
1401 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1403 const struct got_error *err = NULL;
1404 struct tog_blame_cb_args *a = arg;
1405 struct tog_blame_line *line;
1406 int eof;
1408 if (nlines != a->nlines ||
1409 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1410 return got_error(GOT_ERR_RANGE);
1412 if (pthread_mutex_lock(a->mutex) != 0)
1413 return got_error_from_errno();
1415 if (*a->quit) { /* user has quit the blame view */
1416 err = got_error(GOT_ERR_ITER_COMPLETED);
1417 goto done;
1420 if (lineno == -1)
1421 goto done; /* no change in this commit */
1423 line = &a->lines[lineno - 1];
1424 if (line->annotated)
1425 goto done;
1427 line->id = got_object_id_dup(id);
1428 if (line->id == NULL) {
1429 err = got_error_from_errno();
1430 goto done;
1432 line->annotated = 1;
1434 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1435 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1436 a->last_displayed_line, &eof, a->view->nlines);
1437 done:
1438 if (pthread_mutex_unlock(a->mutex) != 0)
1439 return got_error_from_errno();
1440 return err;
1443 struct tog_blame_thread_args {
1444 const char *path;
1445 struct got_repository *repo;
1446 struct tog_blame_cb_args *cb_args;
1447 int *complete;
1450 static void *
1451 blame_thread(void *arg)
1453 const struct got_error *err;
1454 struct tog_blame_thread_args *ta = arg;
1455 struct tog_blame_cb_args *a = ta->cb_args;
1456 int eof;
1458 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1459 blame_cb, ta->cb_args);
1461 if (pthread_mutex_lock(a->mutex) != 0)
1462 return (void *)got_error_from_errno();
1464 got_repo_close(ta->repo);
1465 ta->repo = NULL;
1466 *ta->complete = 1;
1467 if (!err)
1468 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1469 a->lines, a->nlines, 1, *a->selected_line,
1470 a->first_displayed_line, a->last_displayed_line, &eof,
1471 a->view->nlines);
1473 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1474 err = got_error_from_errno();
1476 return (void *)err;
1479 static struct got_object_id *
1480 get_selected_commit_id(struct tog_blame_line *lines,
1481 int first_displayed_line, int selected_line)
1483 struct tog_blame_line *line;
1485 line = &lines[first_displayed_line - 1 + selected_line - 1];
1486 if (!line->annotated)
1487 return NULL;
1489 return line->id;
1492 static const struct got_error *
1493 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1494 struct tog_blame_line *lines, int first_displayed_line,
1495 int selected_line, struct got_repository *repo)
1497 const struct got_error *err = NULL;
1498 struct got_commit_object *commit = NULL;
1499 struct got_object_id *selected_id;
1500 struct got_object_qid *pid;
1502 *pobj = NULL;
1503 *obj = NULL;
1505 selected_id = get_selected_commit_id(lines,
1506 first_displayed_line, selected_line);
1507 if (selected_id == NULL)
1508 return NULL;
1510 err = got_object_open(obj, repo, selected_id);
1511 if (err)
1512 goto done;
1514 err = got_object_commit_open(&commit, repo, *obj);
1515 if (err)
1516 goto done;
1518 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1519 if (pid) {
1520 err = got_object_open(pobj, repo, pid->id);
1521 if (err)
1522 goto done;
1524 done:
1525 if (commit)
1526 got_object_commit_close(commit);
1527 return err;
1530 struct tog_blame {
1531 FILE *f;
1532 size_t filesize;
1533 struct tog_blame_line *lines;
1534 size_t nlines;
1535 pthread_t thread;
1536 struct tog_blame_thread_args thread_args;
1537 struct tog_blame_cb_args cb_args;
1538 const char *path;
1541 static const struct got_error *
1542 stop_blame(struct tog_blame *blame)
1544 const struct got_error *err = NULL;
1545 int i;
1547 if (blame->thread) {
1548 if (pthread_join(blame->thread, (void **)&err) != 0)
1549 err = got_error_from_errno();
1550 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1551 err = NULL;
1552 blame->thread = NULL;
1554 if (blame->thread_args.repo) {
1555 got_repo_close(blame->thread_args.repo);
1556 blame->thread_args.repo = NULL;
1558 if (blame->f) {
1559 fclose(blame->f);
1560 blame->f = NULL;
1562 for (i = 0; i < blame->nlines; i++)
1563 free(blame->lines[i].id);
1564 free(blame->lines);
1565 blame->lines = NULL;
1566 free(blame->cb_args.commit_id);
1567 blame->cb_args.commit_id = NULL;
1569 return err;
1572 static const struct got_error *
1573 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1574 struct tog_view *view, int *blame_complete,
1575 int *first_displayed_line, int *last_displayed_line,
1576 int *selected_line, int *done, const char *path,
1577 struct got_object_id *commit_id,
1578 struct got_repository *repo)
1580 const struct got_error *err = NULL;
1581 struct got_blob_object *blob = NULL;
1582 struct got_repository *thread_repo = NULL;
1583 struct got_object *obj;
1585 err = got_object_open_by_path(&obj, repo, commit_id, path);
1586 if (err)
1587 goto done;
1588 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1589 err = got_error(GOT_ERR_OBJ_TYPE);
1590 goto done;
1593 err = got_object_blob_open(&blob, repo, obj, 8192);
1594 if (err)
1595 goto done;
1596 blame->f = got_opentemp();
1597 if (blame->f == NULL) {
1598 err = got_error_from_errno();
1599 goto done;
1601 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1602 blame->f, blob);
1603 if (err)
1604 goto done;
1606 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1607 if (blame->lines == NULL) {
1608 err = got_error_from_errno();
1609 goto done;
1612 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1613 if (err)
1614 goto done;
1616 blame->cb_args.view = view;
1617 blame->cb_args.lines = blame->lines;
1618 blame->cb_args.nlines = blame->nlines;
1619 blame->cb_args.mutex = mutex;
1620 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1621 if (blame->cb_args.commit_id == NULL) {
1622 err = got_error_from_errno();
1623 goto done;
1625 blame->cb_args.f = blame->f;
1626 blame->cb_args.path = path;
1627 blame->cb_args.first_displayed_line = first_displayed_line;
1628 blame->cb_args.selected_line = selected_line;
1629 blame->cb_args.last_displayed_line = last_displayed_line;
1630 blame->cb_args.quit = done;
1632 blame->thread_args.path = path;
1633 blame->thread_args.repo = thread_repo;
1634 blame->thread_args.cb_args = &blame->cb_args;
1635 blame->thread_args.complete = blame_complete;
1636 *blame_complete = 0;
1638 if (pthread_create(&blame->thread, NULL, blame_thread,
1639 &blame->thread_args) != 0) {
1640 err = got_error_from_errno();
1641 goto done;
1644 done:
1645 if (blob)
1646 got_object_blob_close(blob);
1647 if (obj)
1648 got_object_close(obj);
1649 if (err)
1650 stop_blame(blame);
1651 return err;
1654 static const struct got_error *
1655 show_blame_view(struct tog_view *view, const char *path,
1656 struct got_object_id *commit_id, struct got_repository *repo)
1658 const struct got_error *err = NULL, *thread_err = NULL;
1659 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1660 int selected_line = first_displayed_line;
1661 int eof, blame_complete = 0;
1662 struct got_object *obj = NULL, *pobj = NULL;
1663 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1664 struct tog_blame blame;
1665 struct got_object_id_queue blamed_commits;
1666 struct got_object_qid *blamed_commit = NULL;
1667 struct tog_view *diff_view;
1669 SIMPLEQ_INIT(&blamed_commits);
1671 if (pthread_mutex_init(&mutex, NULL) != 0) {
1672 err = got_error_from_errno();
1673 goto done;
1676 err = got_object_qid_alloc(&blamed_commit, commit_id);
1677 if (err)
1678 goto done;
1679 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1681 show_panel(view->panel);
1682 last_displayed_line = view->nlines;
1684 memset(&blame, 0, sizeof(blame));
1685 err = run_blame(&blame, &mutex, view, &blame_complete,
1686 &first_displayed_line, &last_displayed_line,
1687 &selected_line, &done, path, blamed_commit->id, repo);
1688 if (err)
1689 return err;
1691 while (!done) {
1692 if (pthread_mutex_lock(&mutex) != 0) {
1693 err = got_error_from_errno();
1694 goto done;
1696 err = draw_blame(view, blamed_commit->id, blame.f, path,
1697 blame.lines, blame.nlines, blame_complete, selected_line,
1698 &first_displayed_line, &last_displayed_line, &eof,
1699 view->nlines);
1700 if (pthread_mutex_unlock(&mutex) != 0) {
1701 err = got_error_from_errno();
1702 goto done;
1704 if (err)
1705 break;
1706 nodelay(stdscr, FALSE);
1707 ch = wgetch(view->window);
1708 nodelay(stdscr, TRUE);
1709 if (pthread_mutex_lock(&mutex) != 0) {
1710 err = got_error_from_errno();
1711 goto done;
1713 switch (ch) {
1714 case 'q':
1715 done = 1;
1716 break;
1717 case 'k':
1718 case KEY_UP:
1719 if (selected_line > 1)
1720 selected_line--;
1721 else if (selected_line == 1 &&
1722 first_displayed_line > 1)
1723 first_displayed_line--;
1724 break;
1725 case KEY_PPAGE:
1726 case KEY_BACKSPACE:
1727 if (first_displayed_line == 1) {
1728 selected_line = 1;
1729 break;
1731 if (first_displayed_line > view->nlines - 2)
1732 first_displayed_line -=
1733 (view->nlines - 2);
1734 else
1735 first_displayed_line = 1;
1736 break;
1737 case 'j':
1738 case KEY_DOWN:
1739 if (selected_line < view->nlines - 2 &&
1740 first_displayed_line + selected_line <=
1741 blame.nlines)
1742 selected_line++;
1743 else if (last_displayed_line < blame.nlines)
1744 first_displayed_line++;
1745 break;
1746 case 'b':
1747 case 'p': {
1748 struct got_object_id *id;
1749 id = get_selected_commit_id(blame.lines,
1750 first_displayed_line, selected_line);
1751 if (id == NULL || got_object_id_cmp(id,
1752 blamed_commit->id) == 0)
1753 break;
1754 err = open_selected_commit(&pobj, &obj,
1755 blame.lines, first_displayed_line,
1756 selected_line, repo);
1757 if (err)
1758 break;
1759 if (pobj == NULL && obj == NULL)
1760 break;
1761 if (ch == 'p' && pobj == NULL)
1762 break;
1763 done = 1;
1764 if (pthread_mutex_unlock(&mutex) != 0) {
1765 err = got_error_from_errno();
1766 goto done;
1768 thread_err = stop_blame(&blame);
1769 done = 0;
1770 if (pthread_mutex_lock(&mutex) != 0) {
1771 err = got_error_from_errno();
1772 goto done;
1774 if (thread_err)
1775 break;
1776 id = got_object_get_id(ch == 'b' ? obj : pobj);
1777 got_object_close(obj);
1778 obj = NULL;
1779 if (pobj) {
1780 got_object_close(pobj);
1781 pobj = NULL;
1783 if (id == NULL) {
1784 err = got_error_from_errno();
1785 break;
1787 err = got_object_qid_alloc(&blamed_commit, id);
1788 free(id);
1789 if (err)
1790 goto done;
1791 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1792 blamed_commit, entry);
1793 err = run_blame(&blame, &mutex, view,
1794 &blame_complete, &first_displayed_line,
1795 &last_displayed_line, &selected_line,
1796 &done, path, blamed_commit->id, repo);
1797 if (err)
1798 break;
1799 break;
1801 case 'B': {
1802 struct got_object_qid *first;
1803 first = SIMPLEQ_FIRST(&blamed_commits);
1804 if (!got_object_id_cmp(first->id, commit_id))
1805 break;
1806 done = 1;
1807 if (pthread_mutex_unlock(&mutex) != 0) {
1808 err = got_error_from_errno();
1809 goto done;
1811 thread_err = stop_blame(&blame);
1812 done = 0;
1813 if (pthread_mutex_lock(&mutex) != 0) {
1814 err = got_error_from_errno();
1815 goto done;
1817 if (thread_err)
1818 break;
1819 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1820 got_object_qid_free(blamed_commit);
1821 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1822 err = run_blame(&blame, &mutex, view,
1823 &blame_complete, &first_displayed_line,
1824 &last_displayed_line, &selected_line,
1825 &done, path, blamed_commit->id, repo);
1826 if (err)
1827 break;
1828 break;
1830 case KEY_ENTER:
1831 case '\r':
1832 err = open_selected_commit(&pobj, &obj,
1833 blame.lines, first_displayed_line,
1834 selected_line, repo);
1835 if (err)
1836 break;
1837 if (pobj == NULL && obj == NULL)
1838 break;
1839 diff_view = open_view(0, 0, 0, 0);
1840 if (diff_view == NULL) {
1841 err = got_error_from_errno();
1842 break;
1844 err = show_diff_view(diff_view, pobj, obj, repo);
1845 close_view(diff_view);
1846 if (pobj) {
1847 got_object_close(pobj);
1848 pobj = NULL;
1850 got_object_close(obj);
1851 obj = NULL;
1852 show_panel(view->panel);
1853 if (err)
1854 break;
1855 break;
1856 case KEY_NPAGE:
1857 case ' ':
1858 if (last_displayed_line >= blame.nlines &&
1859 selected_line < view->nlines - 2) {
1860 selected_line = MIN(blame.nlines,
1861 view->nlines - 2);
1862 break;
1864 if (last_displayed_line + view->nlines - 2 <=
1865 blame.nlines)
1866 first_displayed_line +=
1867 view->nlines - 2;
1868 else
1869 first_displayed_line =
1870 blame.nlines - (view->nlines - 3);
1871 break;
1872 case KEY_RESIZE:
1873 err = view_resize(view);
1874 if (err)
1875 break;
1876 if (selected_line > view->nlines - 2) {
1877 selected_line = MIN(blame.nlines,
1878 view->nlines - 2);
1880 break;
1881 default:
1882 break;
1884 if (pthread_mutex_unlock(&mutex) != 0)
1885 err = got_error_from_errno();
1886 if (err || thread_err)
1887 break;
1889 done:
1890 if (pobj)
1891 got_object_close(pobj);
1892 if (blame.thread)
1893 thread_err = stop_blame(&blame);
1894 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1895 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1896 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1897 got_object_qid_free(blamed_commit);
1899 return thread_err ? thread_err : err;
1902 static const struct got_error *
1903 cmd_blame(int argc, char *argv[])
1905 const struct got_error *error;
1906 struct got_repository *repo = NULL;
1907 char *repo_path = NULL;
1908 char *path = NULL;
1909 struct got_object_id *commit_id = NULL;
1910 char *commit_id_str = NULL;
1911 int ch;
1912 struct tog_view *view;
1914 #ifndef PROFILE
1915 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1916 err(1, "pledge");
1917 #endif
1919 while ((ch = getopt(argc, argv, "c:")) != -1) {
1920 switch (ch) {
1921 case 'c':
1922 commit_id_str = optarg;
1923 break;
1924 default:
1925 usage();
1926 /* NOTREACHED */
1930 argc -= optind;
1931 argv += optind;
1933 if (argc == 0) {
1934 usage_blame();
1935 } else if (argc == 1) {
1936 repo_path = getcwd(NULL, 0);
1937 if (repo_path == NULL)
1938 return got_error_from_errno();
1939 path = argv[0];
1940 } else if (argc == 2) {
1941 repo_path = realpath(argv[0], NULL);
1942 if (repo_path == NULL)
1943 return got_error_from_errno();
1944 path = argv[1];
1945 } else
1946 usage_blame();
1948 error = got_repo_open(&repo, repo_path);
1949 free(repo_path);
1950 if (error != NULL)
1951 return error;
1953 if (commit_id_str == NULL) {
1954 struct got_reference *head_ref;
1955 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1956 if (error != NULL)
1957 goto done;
1958 error = got_ref_resolve(&commit_id, repo, head_ref);
1959 got_ref_close(head_ref);
1960 } else {
1961 struct got_object *obj;
1962 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1963 if (error != NULL)
1964 goto done;
1965 commit_id = got_object_get_id(obj);
1966 if (commit_id == NULL)
1967 error = got_error_from_errno();
1968 got_object_close(obj);
1970 if (error != NULL)
1971 goto done;
1973 view = open_view(0, 0, 0, 0);
1974 if (view == NULL) {
1975 error = got_error_from_errno();
1976 goto done;
1978 error = show_blame_view(view, path, commit_id, repo);
1979 close_view(view);
1980 done:
1981 free(commit_id);
1982 if (repo)
1983 got_repo_close(repo);
1984 return error;
1987 static const struct got_error *
1988 draw_tree_entries(struct tog_view *view,
1989 struct got_tree_entry **first_displayed_entry,
1990 struct got_tree_entry **last_displayed_entry,
1991 struct got_tree_entry **selected_entry, int *ndisplayed,
1992 const char *label, int show_ids, const char *parent_path,
1993 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1995 const struct got_error *err = NULL;
1996 struct got_tree_entry *te;
1997 wchar_t *wline;
1998 int width, n;
2000 *ndisplayed = 0;
2002 werase(view->window);
2004 if (limit == 0)
2005 return NULL;
2007 err = format_line(&wline, &width, label, view->ncols);
2008 if (err)
2009 return err;
2010 waddwstr(view->window, wline);
2011 free(wline);
2012 wline = NULL;
2013 if (width < view->ncols)
2014 waddch(view->window, '\n');
2015 if (--limit <= 0)
2016 return NULL;
2017 err = format_line(&wline, &width, parent_path, view->ncols);
2018 if (err)
2019 return err;
2020 waddwstr(view->window, wline);
2021 free(wline);
2022 wline = NULL;
2023 if (width < view->ncols)
2024 waddch(view->window, '\n');
2025 if (--limit <= 0)
2026 return NULL;
2027 waddch(view->window, '\n');
2028 if (--limit <= 0)
2029 return NULL;
2031 te = SIMPLEQ_FIRST(&entries->head);
2032 if (*first_displayed_entry == NULL) {
2033 if (selected == 0) {
2034 wstandout(view->window);
2035 *selected_entry = NULL;
2037 waddstr(view->window, " ..\n"); /* parent directory */
2038 if (selected == 0)
2039 wstandend(view->window);
2040 (*ndisplayed)++;
2041 if (--limit <= 0)
2042 return NULL;
2043 n = 1;
2044 } else {
2045 n = 0;
2046 while (te != *first_displayed_entry)
2047 te = SIMPLEQ_NEXT(te, entry);
2050 while (te) {
2051 char *line = NULL, *id_str = NULL;
2053 if (show_ids) {
2054 err = got_object_id_str(&id_str, te->id);
2055 if (err)
2056 return got_error_from_errno();
2058 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2059 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2060 free(id_str);
2061 return got_error_from_errno();
2063 free(id_str);
2064 err = format_line(&wline, &width, line, view->ncols);
2065 if (err) {
2066 free(line);
2067 break;
2069 if (n == selected) {
2070 wstandout(view->window);
2071 *selected_entry = te;
2073 waddwstr(view->window, wline);
2074 if (width < view->ncols)
2075 waddch(view->window, '\n');
2076 if (n == selected)
2077 wstandend(view->window);
2078 free(line);
2079 free(wline);
2080 wline = NULL;
2081 n++;
2082 (*ndisplayed)++;
2083 *last_displayed_entry = te;
2084 if (--limit <= 0)
2085 break;
2086 te = SIMPLEQ_NEXT(te, entry);
2089 return err;
2092 static void
2093 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2094 const struct got_tree_entries *entries, int isroot)
2096 struct got_tree_entry *te, *prev;
2097 int i;
2099 if (*first_displayed_entry == NULL)
2100 return;
2102 te = SIMPLEQ_FIRST(&entries->head);
2103 if (*first_displayed_entry == te) {
2104 if (!isroot)
2105 *first_displayed_entry = NULL;
2106 return;
2109 /* XXX this is stupid... switch to TAILQ? */
2110 for (i = 0; i < maxscroll; i++) {
2111 while (te != *first_displayed_entry) {
2112 prev = te;
2113 te = SIMPLEQ_NEXT(te, entry);
2115 *first_displayed_entry = prev;
2116 te = SIMPLEQ_FIRST(&entries->head);
2118 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2119 *first_displayed_entry = NULL;
2122 static void
2123 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2124 struct got_tree_entry *last_displayed_entry,
2125 const struct got_tree_entries *entries)
2127 struct got_tree_entry *next;
2128 int n = 0;
2130 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2131 return;
2133 if (*first_displayed_entry)
2134 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2135 else
2136 next = SIMPLEQ_FIRST(&entries->head);
2137 while (next) {
2138 *first_displayed_entry = next;
2139 if (++n >= maxscroll)
2140 break;
2141 next = SIMPLEQ_NEXT(next, entry);
2145 struct tog_parent_tree {
2146 TAILQ_ENTRY(tog_parent_tree) entry;
2147 struct got_tree_object *tree;
2148 struct got_tree_entry *first_displayed_entry;
2149 struct got_tree_entry *selected_entry;
2150 int selected;
2153 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2155 static const struct got_error *
2156 tree_entry_path(char **path, struct tog_parent_trees *parents,
2157 struct got_tree_entry *te)
2159 const struct got_error *err = NULL;
2160 struct tog_parent_tree *pt;
2161 size_t len = 2; /* for leading slash and NUL */
2163 TAILQ_FOREACH(pt, parents, entry)
2164 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2165 if (te)
2166 len += strlen(te->name);
2168 *path = calloc(1, len);
2169 if (path == NULL)
2170 return got_error_from_errno();
2172 (*path)[0] = '/';
2173 pt = TAILQ_LAST(parents, tog_parent_trees);
2174 while (pt) {
2175 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2176 err = got_error(GOT_ERR_NO_SPACE);
2177 goto done;
2179 if (strlcat(*path, "/", len) >= len) {
2180 err = got_error(GOT_ERR_NO_SPACE);
2181 goto done;
2183 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2185 if (te) {
2186 if (strlcat(*path, te->name, len) >= len) {
2187 err = got_error(GOT_ERR_NO_SPACE);
2188 goto done;
2191 done:
2192 if (err) {
2193 free(*path);
2194 *path = NULL;
2196 return err;
2199 static const struct got_error *
2200 blame_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2201 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2202 struct got_repository *repo)
2204 const struct got_error *err = NULL;
2205 char *path;
2207 err = tree_entry_path(&path, parents, te);
2208 if (err)
2209 return err;
2211 err = show_blame_view(view, path, commit_id, repo);
2212 free(path);
2213 return err;
2216 static const struct got_error *
2217 log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2218 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2219 struct got_repository *repo)
2221 const struct got_error *err = NULL;
2222 char *path;
2224 err = tree_entry_path(&path, parents, te);
2225 if (err)
2226 return err;
2228 err = show_log_view(view, commit_id, repo, path);
2229 free(path);
2230 return err;
2233 static const struct got_error *
2234 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2235 struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 int ch, done = 0, selected = 0, show_ids = 0;
2239 struct got_tree_object *tree = root;
2240 const struct got_tree_entries *entries;
2241 struct got_tree_entry *first_displayed_entry = NULL;
2242 struct got_tree_entry *last_displayed_entry = NULL;
2243 struct got_tree_entry *selected_entry = NULL;
2244 char *commit_id_str = NULL, *tree_label = NULL;
2245 int nentries, ndisplayed;
2246 struct tog_parent_trees parents;
2247 struct tog_view *view = NULL;
2249 TAILQ_INIT(&parents);
2251 err = got_object_id_str(&commit_id_str, commit_id);
2252 if (err != NULL)
2253 goto done;
2255 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2256 err = got_error_from_errno();
2257 goto done;
2260 view = open_view(0, 0, 0, 0);
2261 if (view == NULL) {
2262 err = got_error_from_errno();
2263 goto done;
2265 show_panel(view->panel);
2267 entries = got_object_tree_get_entries(root);
2268 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2269 while (!done) {
2270 char *parent_path;
2271 entries = got_object_tree_get_entries(tree);
2272 nentries = entries->nentries;
2273 if (tree != root)
2274 nentries++; /* '..' directory */
2276 err = tree_entry_path(&parent_path, &parents, NULL);
2277 if (err)
2278 goto done;
2280 err = draw_tree_entries(view, &first_displayed_entry,
2281 &last_displayed_entry, &selected_entry, &ndisplayed,
2282 tree_label, show_ids, parent_path, entries, selected,
2283 view->nlines, tree == root);
2284 free(parent_path);
2285 if (err)
2286 break;
2288 nodelay(stdscr, FALSE);
2289 ch = wgetch(view->window);
2290 nodelay(stdscr, TRUE);
2291 switch (ch) {
2292 case 'q':
2293 done = 1;
2294 break;
2295 case 'i':
2296 show_ids = !show_ids;
2297 break;
2298 case 'l':
2299 if (selected_entry) {
2300 struct tog_view *log_view;
2301 log_view = open_view(0, 0, 0, 0);
2302 if (log_view == NULL) {
2303 err = got_error_from_errno();
2304 goto done;
2306 err = log_tree_entry(log_view,
2307 selected_entry, &parents,
2308 commit_id, repo);
2309 close_view(log_view);
2310 if (err)
2311 goto done;
2313 break;
2314 case 'k':
2315 case KEY_UP:
2316 if (selected > 0)
2317 selected--;
2318 if (selected > 0)
2319 break;
2320 tree_scroll_up(&first_displayed_entry, 1,
2321 entries, tree == root);
2322 break;
2323 case KEY_PPAGE:
2324 if (SIMPLEQ_FIRST(&entries->head) ==
2325 first_displayed_entry) {
2326 if (tree != root)
2327 first_displayed_entry = NULL;
2328 selected = 0;
2329 break;
2331 tree_scroll_up(&first_displayed_entry,
2332 view->nlines, entries, tree == root);
2333 break;
2334 case 'j':
2335 case KEY_DOWN:
2336 if (selected < ndisplayed - 1) {
2337 selected++;
2338 break;
2340 tree_scroll_down(&first_displayed_entry, 1,
2341 last_displayed_entry, entries);
2342 break;
2343 case KEY_NPAGE:
2344 tree_scroll_down(&first_displayed_entry,
2345 view->nlines, last_displayed_entry,
2346 entries);
2347 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2348 break;
2349 /* can't scroll any further; move cursor down */
2350 if (selected < ndisplayed - 1)
2351 selected = ndisplayed - 1;
2352 break;
2353 case KEY_ENTER:
2354 case '\r':
2355 if (selected_entry == NULL) {
2356 struct tog_parent_tree *parent;
2357 case KEY_BACKSPACE:
2358 /* user selected '..' */
2359 if (tree == root)
2360 break;
2361 parent = TAILQ_FIRST(&parents);
2362 TAILQ_REMOVE(&parents, parent, entry);
2363 got_object_tree_close(tree);
2364 tree = parent->tree;
2365 first_displayed_entry =
2366 parent->first_displayed_entry;
2367 selected_entry = parent->selected_entry;
2368 selected = parent->selected;
2369 free(parent);
2370 } else if (S_ISDIR(selected_entry->mode)) {
2371 struct tog_parent_tree *parent;
2372 struct got_tree_object *child;
2373 err = got_object_open_as_tree(
2374 &child, repo, selected_entry->id);
2375 if (err)
2376 goto done;
2377 parent = calloc(1, sizeof(*parent));
2378 if (parent == NULL) {
2379 err = got_error_from_errno();
2380 goto done;
2382 parent->tree = tree;
2383 parent->first_displayed_entry =
2384 first_displayed_entry;
2385 parent->selected_entry = selected_entry;
2386 parent->selected = selected;
2387 TAILQ_INSERT_HEAD(&parents, parent,
2388 entry);
2389 tree = child;
2390 selected = 0;
2391 first_displayed_entry = NULL;
2392 } else if (S_ISREG(selected_entry->mode)) {
2393 struct tog_view *blame_view;
2394 blame_view = open_view(0, 0, 0, 0);
2395 if (blame_view == NULL) {
2396 err = got_error_from_errno();
2397 goto done;
2399 err = blame_tree_entry(blame_view,
2400 selected_entry, &parents,
2401 commit_id, repo);
2402 close_view(blame_view);
2403 if (err)
2404 goto done;
2406 break;
2407 case KEY_RESIZE:
2408 err = view_resize(view);
2409 if (err)
2410 goto done;
2411 if (selected > view->nlines)
2412 selected = ndisplayed - 1;
2413 break;
2414 default:
2415 break;
2418 done:
2419 if (view)
2420 close_view(view);
2421 free(tree_label);
2422 free(commit_id_str);
2423 while (!TAILQ_EMPTY(&parents)) {
2424 struct tog_parent_tree *parent;
2425 parent = TAILQ_FIRST(&parents);
2426 TAILQ_REMOVE(&parents, parent, entry);
2427 free(parent);
2430 if (tree != root)
2431 got_object_tree_close(tree);
2432 return err;
2435 __dead static void
2436 usage_tree(void)
2438 endwin();
2439 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2440 getprogname());
2441 exit(1);
2444 static const struct got_error *
2445 cmd_tree(int argc, char *argv[])
2447 const struct got_error *error;
2448 struct got_repository *repo = NULL;
2449 char *repo_path = NULL;
2450 struct got_object_id *commit_id = NULL;
2451 char *commit_id_arg = NULL;
2452 struct got_commit_object *commit = NULL;
2453 struct got_tree_object *tree = NULL;
2454 int ch;
2456 #ifndef PROFILE
2457 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2458 err(1, "pledge");
2459 #endif
2461 while ((ch = getopt(argc, argv, "c:")) != -1) {
2462 switch (ch) {
2463 case 'c':
2464 commit_id_arg = optarg;
2465 break;
2466 default:
2467 usage();
2468 /* NOTREACHED */
2472 argc -= optind;
2473 argv += optind;
2475 if (argc == 0) {
2476 repo_path = getcwd(NULL, 0);
2477 if (repo_path == NULL)
2478 return got_error_from_errno();
2479 } else if (argc == 1) {
2480 repo_path = realpath(argv[0], NULL);
2481 if (repo_path == NULL)
2482 return got_error_from_errno();
2483 } else
2484 usage_log();
2486 error = got_repo_open(&repo, repo_path);
2487 free(repo_path);
2488 if (error != NULL)
2489 return error;
2491 if (commit_id_arg == NULL) {
2492 error = get_head_commit_id(&commit_id, repo);
2493 if (error != NULL)
2494 goto done;
2495 } else {
2496 struct got_object *obj;
2497 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2498 if (error == NULL) {
2499 commit_id = got_object_get_id(obj);
2500 if (commit_id == NULL)
2501 error = got_error_from_errno();
2504 if (error != NULL)
2505 goto done;
2507 error = got_object_open_as_commit(&commit, repo, commit_id);
2508 if (error != NULL)
2509 goto done;
2511 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2512 if (error != NULL)
2513 goto done;
2515 error = show_tree_view(tree, commit_id, repo);
2516 done:
2517 free(commit_id);
2518 if (commit)
2519 got_object_commit_close(commit);
2520 if (tree)
2521 got_object_tree_close(tree);
2522 if (repo)
2523 got_repo_close(repo);
2524 return error;
2526 static void
2527 init_curses(void)
2529 initscr();
2530 cbreak();
2531 noecho();
2532 nonl();
2533 intrflush(stdscr, FALSE);
2534 keypad(stdscr, TRUE);
2535 curs_set(0);
2538 __dead static void
2539 usage(void)
2541 int i;
2543 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2544 "Available commands:\n", getprogname());
2545 for (i = 0; i < nitems(tog_commands); i++) {
2546 struct tog_cmd *cmd = &tog_commands[i];
2547 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2549 exit(1);
2552 static char **
2553 make_argv(const char *arg0, const char *arg1)
2555 char **argv;
2556 int argc = (arg1 == NULL ? 1 : 2);
2558 argv = calloc(argc, sizeof(char *));
2559 if (argv == NULL)
2560 err(1, "calloc");
2561 argv[0] = strdup(arg0);
2562 if (argv[0] == NULL)
2563 err(1, "calloc");
2564 if (arg1) {
2565 argv[1] = strdup(arg1);
2566 if (argv[1] == NULL)
2567 err(1, "calloc");
2570 return argv;
2573 int
2574 main(int argc, char *argv[])
2576 const struct got_error *error = NULL;
2577 struct tog_cmd *cmd = NULL;
2578 int ch, hflag = 0;
2579 char **cmd_argv = NULL;
2581 setlocale(LC_ALL, "");
2583 while ((ch = getopt(argc, argv, "h")) != -1) {
2584 switch (ch) {
2585 case 'h':
2586 hflag = 1;
2587 break;
2588 default:
2589 usage();
2590 /* NOTREACHED */
2594 argc -= optind;
2595 argv += optind;
2596 optind = 0;
2597 optreset = 1;
2599 if (argc == 0) {
2600 if (hflag)
2601 usage();
2602 /* Build an argument vector which runs a default command. */
2603 cmd = &tog_commands[0];
2604 cmd_argv = make_argv(cmd->name, NULL);
2605 argc = 1;
2606 } else {
2607 int i;
2609 /* Did the user specific a command? */
2610 for (i = 0; i < nitems(tog_commands); i++) {
2611 if (strncmp(tog_commands[i].name, argv[0],
2612 strlen(argv[0])) == 0) {
2613 cmd = &tog_commands[i];
2614 if (hflag)
2615 tog_commands[i].cmd_usage();
2616 break;
2619 if (cmd == NULL) {
2620 /* Did the user specify a repository? */
2621 char *repo_path = realpath(argv[0], NULL);
2622 if (repo_path) {
2623 struct got_repository *repo;
2624 error = got_repo_open(&repo, repo_path);
2625 if (error == NULL)
2626 got_repo_close(repo);
2627 } else
2628 error = got_error_from_errno();
2629 if (error) {
2630 if (hflag) {
2631 fprintf(stderr, "%s: '%s' is not a "
2632 "known command\n", getprogname(),
2633 argv[0]);
2634 usage();
2636 fprintf(stderr, "%s: '%s' is neither a known "
2637 "command nor a path to a repository\n",
2638 getprogname(), argv[0]);
2639 free(repo_path);
2640 return 1;
2642 cmd = &tog_commands[0];
2643 cmd_argv = make_argv(cmd->name, repo_path);
2644 argc = 2;
2645 free(repo_path);
2649 init_curses();
2651 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2652 if (error)
2653 goto done;
2654 done:
2655 endwin();
2656 free(cmd_argv);
2657 if (error)
2658 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2659 return 0;