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 view_resize(view);
894 if (selected > view->nlines - 2)
895 selected = view->nlines - 2;
896 if (selected > commits.ncommits - 1)
897 selected = commits.ncommits - 1;
898 break;
899 case KEY_ENTER:
900 case '\r':
901 err = show_commit(selected_entry, repo);
902 if (err)
903 goto done;
904 show_panel(view->panel);
905 break;
906 case 't':
907 err = browse_commit(selected_entry, repo);
908 if (err)
909 goto done;
910 show_panel(view->panel);
911 break;
912 default:
913 break;
916 done:
917 if (view)
918 close_view(view);
919 free(head_id);
920 if (graph)
921 got_commit_graph_close(graph);
922 free_commits(&commits);
923 free(in_repo_path);
924 return err;
927 static const struct got_error *
928 cmd_log(int argc, char *argv[])
930 const struct got_error *error;
931 struct got_repository *repo = NULL;
932 struct got_object_id *start_id = NULL;
933 char *path = NULL, *repo_path = NULL, *cwd = NULL;
934 char *start_commit = NULL;
935 int ch;
937 #ifndef PROFILE
938 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
939 err(1, "pledge");
940 #endif
942 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
943 switch (ch) {
944 case 'c':
945 start_commit = optarg;
946 break;
947 case 'r':
948 repo_path = realpath(optarg, NULL);
949 if (repo_path == NULL)
950 err(1, "-r option");
951 break;
952 default:
953 usage();
954 /* NOTREACHED */
958 argc -= optind;
959 argv += optind;
961 if (argc == 0)
962 path = strdup("");
963 else if (argc == 1)
964 path = strdup(argv[0]);
965 else
966 usage_log();
967 if (path == NULL)
968 return got_error_from_errno();
970 cwd = getcwd(NULL, 0);
971 if (cwd == NULL) {
972 error = got_error_from_errno();
973 goto done;
975 if (repo_path == NULL) {
976 repo_path = strdup(cwd);
977 if (repo_path == NULL) {
978 error = got_error_from_errno();
979 goto done;
983 error = got_repo_open(&repo, repo_path);
984 if (error != NULL)
985 goto done;
987 if (start_commit == NULL) {
988 error = get_head_commit_id(&start_id, repo);
989 if (error != NULL)
990 goto done;
991 } else {
992 struct got_object *obj;
993 error = got_object_open_by_id_str(&obj, repo, start_commit);
994 if (error == NULL) {
995 start_id = got_object_get_id(obj);
996 if (start_id == NULL)
997 error = got_error_from_errno();
998 goto done;
1001 if (error != NULL)
1002 goto done;
1004 error = show_log_view(start_id, repo, path);
1005 done:
1006 free(repo_path);
1007 free(cwd);
1008 free(path);
1009 free(start_id);
1010 if (repo)
1011 got_repo_close(repo);
1012 return error;
1015 __dead static void
1016 usage_diff(void)
1018 endwin();
1019 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1020 getprogname());
1021 exit(1);
1024 static char *
1025 parse_next_line(FILE *f, size_t *len)
1027 char *line;
1028 size_t linelen;
1029 size_t lineno;
1030 const char delim[3] = { '\0', '\0', '\0'};
1032 line = fparseln(f, &linelen, &lineno, delim, 0);
1033 if (len)
1034 *len = linelen;
1035 return line;
1038 static const struct got_error *
1039 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1040 int *last_displayed_line, int *eof, int max_lines)
1042 const struct got_error *err;
1043 int nlines = 0, nprinted = 0;
1044 char *line;
1045 size_t len;
1046 wchar_t *wline;
1047 int width;
1049 rewind(f);
1050 werase(view->window);
1052 *eof = 0;
1053 while (nprinted < max_lines) {
1054 line = parse_next_line(f, &len);
1055 if (line == NULL) {
1056 *eof = 1;
1057 break;
1059 if (++nlines < *first_displayed_line) {
1060 free(line);
1061 continue;
1064 err = format_line(&wline, &width, line, view->ncols);
1065 if (err) {
1066 free(line);
1067 free(wline);
1068 return err;
1070 waddwstr(view->window, wline);
1071 if (width < view->ncols)
1072 waddch(view->window, '\n');
1073 if (++nprinted == 1)
1074 *first_displayed_line = nlines;
1075 free(line);
1076 free(wline);
1077 wline = NULL;
1079 *last_displayed_line = nlines;
1081 update_panels();
1082 doupdate();
1084 return NULL;
1087 static const struct got_error *
1088 show_diff_view(struct tog_view *view, struct got_object *obj1,
1089 struct got_object *obj2, struct got_repository *repo)
1091 const struct got_error *err;
1092 FILE *f;
1093 int ch, done = 0;
1094 int first_displayed_line = 1, last_displayed_line = view->nlines;
1095 int eof, i;
1097 if (obj1 != NULL && obj2 != NULL &&
1098 got_object_get_type(obj1) != got_object_get_type(obj2))
1099 return got_error(GOT_ERR_OBJ_TYPE);
1101 f = got_opentemp();
1102 if (f == NULL)
1103 return got_error_from_errno();
1105 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1106 case GOT_OBJ_TYPE_BLOB:
1107 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1108 break;
1109 case GOT_OBJ_TYPE_TREE:
1110 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1111 break;
1112 case GOT_OBJ_TYPE_COMMIT:
1113 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1114 break;
1115 default:
1116 return got_error(GOT_ERR_OBJ_TYPE);
1119 fflush(f);
1121 show_panel(view->panel);
1123 while (!done) {
1124 err = draw_file(view, f, &first_displayed_line,
1125 &last_displayed_line, &eof, view->nlines);
1126 if (err)
1127 break;
1128 nodelay(stdscr, FALSE);
1129 ch = wgetch(view->window);
1130 nodelay(stdscr, TRUE);
1131 switch (ch) {
1132 case 'q':
1133 done = 1;
1134 break;
1135 case 'k':
1136 case KEY_UP:
1137 if (first_displayed_line > 1)
1138 first_displayed_line--;
1139 break;
1140 case KEY_PPAGE:
1141 case KEY_BACKSPACE:
1142 i = 0;
1143 while (i++ < view->nlines - 1 &&
1144 first_displayed_line > 1)
1145 first_displayed_line--;
1146 break;
1147 case 'j':
1148 case KEY_DOWN:
1149 if (!eof)
1150 first_displayed_line++;
1151 break;
1152 case KEY_NPAGE:
1153 case ' ':
1154 i = 0;
1155 while (!eof && i++ < view->nlines - 1) {
1156 char *line = parse_next_line(f, NULL);
1157 first_displayed_line++;
1158 if (line == NULL)
1159 break;
1161 break;
1162 case KEY_RESIZE:
1163 view_resize(view);
1164 break;
1165 default:
1166 break;
1169 fclose(f);
1170 return err;
1173 static const struct got_error *
1174 cmd_diff(int argc, char *argv[])
1176 const struct got_error *error = NULL;
1177 struct got_repository *repo = NULL;
1178 struct got_object *obj1 = NULL, *obj2 = NULL;
1179 char *repo_path = NULL;
1180 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1181 int ch;
1182 struct tog_view *view;
1184 #ifndef PROFILE
1185 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1186 err(1, "pledge");
1187 #endif
1189 while ((ch = getopt(argc, argv, "")) != -1) {
1190 switch (ch) {
1191 default:
1192 usage();
1193 /* NOTREACHED */
1197 argc -= optind;
1198 argv += optind;
1200 if (argc == 0) {
1201 usage_diff(); /* TODO show local worktree changes */
1202 } else if (argc == 2) {
1203 repo_path = getcwd(NULL, 0);
1204 if (repo_path == NULL)
1205 return got_error_from_errno();
1206 obj_id_str1 = argv[0];
1207 obj_id_str2 = argv[1];
1208 } else if (argc == 3) {
1209 repo_path = realpath(argv[0], NULL);
1210 if (repo_path == NULL)
1211 return got_error_from_errno();
1212 obj_id_str1 = argv[1];
1213 obj_id_str2 = argv[2];
1214 } else
1215 usage_diff();
1217 error = got_repo_open(&repo, repo_path);
1218 free(repo_path);
1219 if (error)
1220 goto done;
1222 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1223 if (error)
1224 goto done;
1226 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1227 if (error)
1228 goto done;
1230 view = open_view(0, 0, 0, 0);
1231 if (view == NULL) {
1232 error = got_error_from_errno();
1233 goto done;
1235 error = show_diff_view(view, obj1, obj2, repo);
1236 close_view(view);
1237 done:
1238 got_repo_close(repo);
1239 if (obj1)
1240 got_object_close(obj1);
1241 if (obj2)
1242 got_object_close(obj2);
1243 return error;
1246 __dead static void
1247 usage_blame(void)
1249 endwin();
1250 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1251 getprogname());
1252 exit(1);
1255 struct tog_blame_line {
1256 int annotated;
1257 struct got_object_id *id;
1260 static const struct got_error *
1261 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1262 const char *path, struct tog_blame_line *lines, int nlines,
1263 int blame_complete, int selected_line, int *first_displayed_line,
1264 int *last_displayed_line, int *eof, int max_lines)
1266 const struct got_error *err;
1267 int lineno = 0, nprinted = 0;
1268 char *line;
1269 size_t len;
1270 wchar_t *wline;
1271 int width, wlimit;
1272 struct tog_blame_line *blame_line;
1273 struct got_object_id *prev_id = NULL;
1274 char *id_str;
1276 err = got_object_id_str(&id_str, id);
1277 if (err)
1278 return err;
1280 rewind(f);
1281 werase(view->window);
1283 if (asprintf(&line, "commit: %s", id_str) == -1) {
1284 err = got_error_from_errno();
1285 free(id_str);
1286 return err;
1289 err = format_line(&wline, &width, line, view->ncols);
1290 free(line);
1291 line = NULL;
1292 waddwstr(view->window, wline);
1293 free(wline);
1294 wline = NULL;
1295 if (width < view->ncols)
1296 waddch(view->window, '\n');
1298 if (asprintf(&line, "[%d/%d] %s%s",
1299 *first_displayed_line - 1 + selected_line, nlines,
1300 blame_complete ? "" : "annotating ", path) == -1) {
1301 free(id_str);
1302 return got_error_from_errno();
1304 free(id_str);
1305 err = format_line(&wline, &width, line, view->ncols);
1306 free(line);
1307 line = NULL;
1308 if (err)
1309 return err;
1310 waddwstr(view->window, wline);
1311 free(wline);
1312 wline = NULL;
1313 if (width < view->ncols)
1314 waddch(view->window, '\n');
1316 *eof = 0;
1317 while (nprinted < max_lines - 2) {
1318 line = parse_next_line(f, &len);
1319 if (line == NULL) {
1320 *eof = 1;
1321 break;
1323 if (++lineno < *first_displayed_line) {
1324 free(line);
1325 continue;
1328 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1329 err = format_line(&wline, &width, line, wlimit);
1330 if (err) {
1331 free(line);
1332 return err;
1335 if (nprinted == selected_line - 1)
1336 wstandout(view->window);
1338 blame_line = &lines[lineno - 1];
1339 if (blame_line->annotated && prev_id &&
1340 got_object_id_cmp(prev_id, blame_line->id) == 0)
1341 waddstr(view->window, " ");
1342 else if (blame_line->annotated) {
1343 char *id_str;
1344 err = got_object_id_str(&id_str, blame_line->id);
1345 if (err) {
1346 free(line);
1347 free(wline);
1348 return err;
1350 wprintw(view->window, "%.8s ", id_str);
1351 free(id_str);
1352 prev_id = blame_line->id;
1353 } else {
1354 waddstr(view->window, "........ ");
1355 prev_id = NULL;
1358 waddwstr(view->window, wline);
1359 while (width < wlimit) {
1360 waddch(view->window, ' ');
1361 width++;
1363 if (nprinted == selected_line - 1)
1364 wstandend(view->window);
1365 if (++nprinted == 1)
1366 *first_displayed_line = lineno;
1367 free(line);
1368 free(wline);
1369 wline = NULL;
1371 *last_displayed_line = lineno;
1373 update_panels();
1374 doupdate();
1376 return NULL;
1379 struct tog_blame_cb_args {
1380 pthread_mutex_t *mutex;
1381 struct tog_blame_line *lines; /* one per line */
1382 int nlines;
1384 struct tog_view *view;
1385 struct got_object_id *commit_id;
1386 FILE *f;
1387 const char *path;
1388 int *first_displayed_line;
1389 int *last_displayed_line;
1390 int *selected_line;
1391 int *quit;
1394 static const struct got_error *
1395 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1397 const struct got_error *err = NULL;
1398 struct tog_blame_cb_args *a = arg;
1399 struct tog_blame_line *line;
1400 int eof;
1402 if (nlines != a->nlines ||
1403 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1404 return got_error(GOT_ERR_RANGE);
1406 if (pthread_mutex_lock(a->mutex) != 0)
1407 return got_error_from_errno();
1409 if (*a->quit) { /* user has quit the blame view */
1410 err = got_error(GOT_ERR_ITER_COMPLETED);
1411 goto done;
1414 if (lineno == -1)
1415 goto done; /* no change in this commit */
1417 line = &a->lines[lineno - 1];
1418 if (line->annotated)
1419 goto done;
1421 line->id = got_object_id_dup(id);
1422 if (line->id == NULL) {
1423 err = got_error_from_errno();
1424 goto done;
1426 line->annotated = 1;
1428 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1429 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1430 a->last_displayed_line, &eof, a->view->nlines);
1431 done:
1432 if (pthread_mutex_unlock(a->mutex) != 0)
1433 return got_error_from_errno();
1434 return err;
1437 struct tog_blame_thread_args {
1438 const char *path;
1439 struct got_repository *repo;
1440 struct tog_blame_cb_args *cb_args;
1441 int *complete;
1444 static void *
1445 blame_thread(void *arg)
1447 const struct got_error *err;
1448 struct tog_blame_thread_args *ta = arg;
1449 struct tog_blame_cb_args *a = ta->cb_args;
1450 int eof;
1452 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1453 blame_cb, ta->cb_args);
1455 if (pthread_mutex_lock(a->mutex) != 0)
1456 return (void *)got_error_from_errno();
1458 got_repo_close(ta->repo);
1459 ta->repo = NULL;
1460 *ta->complete = 1;
1461 if (!err)
1462 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1463 a->lines, a->nlines, 1, *a->selected_line,
1464 a->first_displayed_line, a->last_displayed_line, &eof,
1465 a->view->nlines);
1467 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1468 err = got_error_from_errno();
1470 return (void *)err;
1473 static struct got_object_id *
1474 get_selected_commit_id(struct tog_blame_line *lines,
1475 int first_displayed_line, int selected_line)
1477 struct tog_blame_line *line;
1479 line = &lines[first_displayed_line - 1 + selected_line - 1];
1480 if (!line->annotated)
1481 return NULL;
1483 return line->id;
1486 static const struct got_error *
1487 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1488 struct tog_blame_line *lines, int first_displayed_line,
1489 int selected_line, struct got_repository *repo)
1491 const struct got_error *err = NULL;
1492 struct got_commit_object *commit = NULL;
1493 struct got_object_id *selected_id;
1494 struct got_object_qid *pid;
1496 *pobj = NULL;
1497 *obj = NULL;
1499 selected_id = get_selected_commit_id(lines,
1500 first_displayed_line, selected_line);
1501 if (selected_id == NULL)
1502 return NULL;
1504 err = got_object_open(obj, repo, selected_id);
1505 if (err)
1506 goto done;
1508 err = got_object_commit_open(&commit, repo, *obj);
1509 if (err)
1510 goto done;
1512 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1513 if (pid) {
1514 err = got_object_open(pobj, repo, pid->id);
1515 if (err)
1516 goto done;
1518 done:
1519 if (commit)
1520 got_object_commit_close(commit);
1521 return err;
1524 struct tog_blame {
1525 FILE *f;
1526 size_t filesize;
1527 struct tog_blame_line *lines;
1528 size_t nlines;
1529 pthread_t thread;
1530 struct tog_blame_thread_args thread_args;
1531 struct tog_blame_cb_args cb_args;
1532 const char *path;
1535 static const struct got_error *
1536 stop_blame(struct tog_blame *blame)
1538 const struct got_error *err = NULL;
1539 int i;
1541 if (blame->thread) {
1542 if (pthread_join(blame->thread, (void **)&err) != 0)
1543 err = got_error_from_errno();
1544 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1545 err = NULL;
1546 blame->thread = NULL;
1548 if (blame->thread_args.repo) {
1549 got_repo_close(blame->thread_args.repo);
1550 blame->thread_args.repo = NULL;
1552 if (blame->f) {
1553 fclose(blame->f);
1554 blame->f = NULL;
1556 for (i = 0; i < blame->nlines; i++)
1557 free(blame->lines[i].id);
1558 free(blame->lines);
1559 blame->lines = NULL;
1560 free(blame->cb_args.commit_id);
1561 blame->cb_args.commit_id = NULL;
1563 return err;
1566 static const struct got_error *
1567 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1568 struct tog_view *view, int *blame_complete,
1569 int *first_displayed_line, int *last_displayed_line,
1570 int *selected_line, int *done, const char *path,
1571 struct got_object_id *commit_id,
1572 struct got_repository *repo)
1574 const struct got_error *err = NULL;
1575 struct got_blob_object *blob = NULL;
1576 struct got_repository *thread_repo = NULL;
1577 struct got_object *obj;
1579 err = got_object_open_by_path(&obj, repo, commit_id, path);
1580 if (err)
1581 goto done;
1582 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1583 err = got_error(GOT_ERR_OBJ_TYPE);
1584 goto done;
1587 err = got_object_blob_open(&blob, repo, obj, 8192);
1588 if (err)
1589 goto done;
1590 blame->f = got_opentemp();
1591 if (blame->f == NULL) {
1592 err = got_error_from_errno();
1593 goto done;
1595 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1596 blame->f, blob);
1597 if (err)
1598 goto done;
1600 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1601 if (blame->lines == NULL) {
1602 err = got_error_from_errno();
1603 goto done;
1606 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1607 if (err)
1608 goto done;
1610 blame->cb_args.view = view;
1611 blame->cb_args.lines = blame->lines;
1612 blame->cb_args.nlines = blame->nlines;
1613 blame->cb_args.mutex = mutex;
1614 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1615 if (blame->cb_args.commit_id == NULL) {
1616 err = got_error_from_errno();
1617 goto done;
1619 blame->cb_args.f = blame->f;
1620 blame->cb_args.path = path;
1621 blame->cb_args.first_displayed_line = first_displayed_line;
1622 blame->cb_args.selected_line = selected_line;
1623 blame->cb_args.last_displayed_line = last_displayed_line;
1624 blame->cb_args.quit = done;
1626 blame->thread_args.path = path;
1627 blame->thread_args.repo = thread_repo;
1628 blame->thread_args.cb_args = &blame->cb_args;
1629 blame->thread_args.complete = blame_complete;
1630 *blame_complete = 0;
1632 if (pthread_create(&blame->thread, NULL, blame_thread,
1633 &blame->thread_args) != 0) {
1634 err = got_error_from_errno();
1635 goto done;
1638 done:
1639 if (blob)
1640 got_object_blob_close(blob);
1641 if (obj)
1642 got_object_close(obj);
1643 if (err)
1644 stop_blame(blame);
1645 return err;
1648 static const struct got_error *
1649 show_blame_view(const char *path, struct got_object_id *commit_id,
1650 struct got_repository *repo)
1652 const struct got_error *err = NULL, *thread_err = NULL;
1653 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1654 int selected_line = first_displayed_line;
1655 int eof, blame_complete = 0;
1656 struct got_object *obj = NULL, *pobj = NULL;
1657 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1658 struct tog_blame blame;
1659 struct got_object_id_queue blamed_commits;
1660 struct got_object_qid *blamed_commit = NULL;
1661 struct tog_view *view = NULL, *diff_view;
1663 SIMPLEQ_INIT(&blamed_commits);
1665 if (pthread_mutex_init(&mutex, NULL) != 0) {
1666 err = got_error_from_errno();
1667 goto done;
1670 err = got_object_qid_alloc(&blamed_commit, commit_id);
1671 if (err)
1672 goto done;
1673 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1675 view = open_view(0, 0, 0, 0);
1676 if (view == NULL) {
1677 err = got_error_from_errno();
1678 goto done;
1680 show_panel(view->panel);
1681 last_displayed_line = view->nlines;
1683 memset(&blame, 0, sizeof(blame));
1684 err = run_blame(&blame, &mutex, view, &blame_complete,
1685 &first_displayed_line, &last_displayed_line,
1686 &selected_line, &done, path, blamed_commit->id, repo);
1687 if (err)
1688 return err;
1690 while (!done) {
1691 if (pthread_mutex_lock(&mutex) != 0) {
1692 err = got_error_from_errno();
1693 goto done;
1695 err = draw_blame(view, blamed_commit->id, blame.f, path,
1696 blame.lines, blame.nlines, blame_complete, selected_line,
1697 &first_displayed_line, &last_displayed_line, &eof,
1698 view->nlines);
1699 if (pthread_mutex_unlock(&mutex) != 0) {
1700 err = got_error_from_errno();
1701 goto done;
1703 if (err)
1704 break;
1705 nodelay(stdscr, FALSE);
1706 ch = wgetch(view->window);
1707 nodelay(stdscr, TRUE);
1708 if (pthread_mutex_lock(&mutex) != 0) {
1709 err = got_error_from_errno();
1710 goto done;
1712 switch (ch) {
1713 case 'q':
1714 done = 1;
1715 break;
1716 case 'k':
1717 case KEY_UP:
1718 if (selected_line > 1)
1719 selected_line--;
1720 else if (selected_line == 1 &&
1721 first_displayed_line > 1)
1722 first_displayed_line--;
1723 break;
1724 case KEY_PPAGE:
1725 case KEY_BACKSPACE:
1726 if (first_displayed_line == 1) {
1727 selected_line = 1;
1728 break;
1730 if (first_displayed_line > view->nlines - 2)
1731 first_displayed_line -=
1732 (view->nlines - 2);
1733 else
1734 first_displayed_line = 1;
1735 break;
1736 case 'j':
1737 case KEY_DOWN:
1738 if (selected_line < view->nlines - 2 &&
1739 first_displayed_line + selected_line <=
1740 blame.nlines)
1741 selected_line++;
1742 else if (last_displayed_line < blame.nlines)
1743 first_displayed_line++;
1744 break;
1745 case 'b':
1746 case 'p': {
1747 struct got_object_id *id;
1748 id = get_selected_commit_id(blame.lines,
1749 first_displayed_line, selected_line);
1750 if (id == NULL || got_object_id_cmp(id,
1751 blamed_commit->id) == 0)
1752 break;
1753 err = open_selected_commit(&pobj, &obj,
1754 blame.lines, first_displayed_line,
1755 selected_line, repo);
1756 if (err)
1757 break;
1758 if (pobj == NULL && obj == NULL)
1759 break;
1760 if (ch == 'p' && pobj == NULL)
1761 break;
1762 done = 1;
1763 if (pthread_mutex_unlock(&mutex) != 0) {
1764 err = got_error_from_errno();
1765 goto done;
1767 thread_err = stop_blame(&blame);
1768 done = 0;
1769 if (pthread_mutex_lock(&mutex) != 0) {
1770 err = got_error_from_errno();
1771 goto done;
1773 if (thread_err)
1774 break;
1775 id = got_object_get_id(ch == 'b' ? obj : pobj);
1776 got_object_close(obj);
1777 obj = NULL;
1778 if (pobj) {
1779 got_object_close(pobj);
1780 pobj = NULL;
1782 if (id == NULL) {
1783 err = got_error_from_errno();
1784 break;
1786 err = got_object_qid_alloc(&blamed_commit, id);
1787 free(id);
1788 if (err)
1789 goto done;
1790 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1791 blamed_commit, entry);
1792 err = run_blame(&blame, &mutex, view,
1793 &blame_complete, &first_displayed_line,
1794 &last_displayed_line, &selected_line,
1795 &done, path, blamed_commit->id, repo);
1796 if (err)
1797 break;
1798 break;
1800 case 'B': {
1801 struct got_object_qid *first;
1802 first = SIMPLEQ_FIRST(&blamed_commits);
1803 if (!got_object_id_cmp(first->id, commit_id))
1804 break;
1805 done = 1;
1806 if (pthread_mutex_unlock(&mutex) != 0) {
1807 err = got_error_from_errno();
1808 goto done;
1810 thread_err = stop_blame(&blame);
1811 done = 0;
1812 if (pthread_mutex_lock(&mutex) != 0) {
1813 err = got_error_from_errno();
1814 goto done;
1816 if (thread_err)
1817 break;
1818 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1819 got_object_qid_free(blamed_commit);
1820 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1821 err = run_blame(&blame, &mutex, view,
1822 &blame_complete, &first_displayed_line,
1823 &last_displayed_line, &selected_line,
1824 &done, path, blamed_commit->id, repo);
1825 if (err)
1826 break;
1827 break;
1829 case KEY_ENTER:
1830 case '\r':
1831 err = open_selected_commit(&pobj, &obj,
1832 blame.lines, first_displayed_line,
1833 selected_line, repo);
1834 if (err)
1835 break;
1836 if (pobj == NULL && obj == NULL)
1837 break;
1838 diff_view = open_view(0, 0, 0, 0);
1839 if (diff_view == NULL) {
1840 err = got_error_from_errno();
1841 break;
1843 err = show_diff_view(diff_view, pobj, obj, repo);
1844 close_view(diff_view);
1845 if (pobj) {
1846 got_object_close(pobj);
1847 pobj = NULL;
1849 got_object_close(obj);
1850 obj = NULL;
1851 show_panel(view->panel);
1852 if (err)
1853 break;
1854 break;
1855 case KEY_NPAGE:
1856 case ' ':
1857 if (last_displayed_line >= blame.nlines &&
1858 selected_line < view->nlines - 2) {
1859 selected_line = MIN(blame.nlines,
1860 view->nlines - 2);
1861 break;
1863 if (last_displayed_line + view->nlines - 2 <=
1864 blame.nlines)
1865 first_displayed_line +=
1866 view->nlines - 2;
1867 else
1868 first_displayed_line =
1869 blame.nlines - (view->nlines - 3);
1870 break;
1871 case KEY_RESIZE:
1872 view_resize(view);
1873 if (selected_line > view->nlines - 2) {
1874 selected_line = MIN(blame.nlines,
1875 view->nlines - 2);
1877 break;
1878 default:
1879 break;
1881 if (pthread_mutex_unlock(&mutex) != 0)
1882 err = got_error_from_errno();
1883 if (err || thread_err)
1884 break;
1886 done:
1887 if (pobj)
1888 got_object_close(pobj);
1889 if (blame.thread)
1890 thread_err = stop_blame(&blame);
1891 if (view)
1892 close_view(view);
1893 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1894 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1895 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1896 got_object_qid_free(blamed_commit);
1898 return thread_err ? thread_err : err;
1901 static const struct got_error *
1902 cmd_blame(int argc, char *argv[])
1904 const struct got_error *error;
1905 struct got_repository *repo = NULL;
1906 char *repo_path = NULL;
1907 char *path = NULL;
1908 struct got_object_id *commit_id = NULL;
1909 char *commit_id_str = NULL;
1910 int ch;
1912 #ifndef PROFILE
1913 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1914 err(1, "pledge");
1915 #endif
1917 while ((ch = getopt(argc, argv, "c:")) != -1) {
1918 switch (ch) {
1919 case 'c':
1920 commit_id_str = optarg;
1921 break;
1922 default:
1923 usage();
1924 /* NOTREACHED */
1928 argc -= optind;
1929 argv += optind;
1931 if (argc == 0) {
1932 usage_blame();
1933 } else if (argc == 1) {
1934 repo_path = getcwd(NULL, 0);
1935 if (repo_path == NULL)
1936 return got_error_from_errno();
1937 path = argv[0];
1938 } else if (argc == 2) {
1939 repo_path = realpath(argv[0], NULL);
1940 if (repo_path == NULL)
1941 return got_error_from_errno();
1942 path = argv[1];
1943 } else
1944 usage_blame();
1946 error = got_repo_open(&repo, repo_path);
1947 free(repo_path);
1948 if (error != NULL)
1949 return error;
1951 if (commit_id_str == NULL) {
1952 struct got_reference *head_ref;
1953 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1954 if (error != NULL)
1955 goto done;
1956 error = got_ref_resolve(&commit_id, repo, head_ref);
1957 got_ref_close(head_ref);
1958 } else {
1959 struct got_object *obj;
1960 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1961 if (error != NULL)
1962 goto done;
1963 commit_id = got_object_get_id(obj);
1964 if (commit_id == NULL)
1965 error = got_error_from_errno();
1966 got_object_close(obj);
1968 if (error != NULL)
1969 goto done;
1971 error = show_blame_view(path, commit_id, repo);
1972 done:
1973 free(commit_id);
1974 if (repo)
1975 got_repo_close(repo);
1976 return error;
1979 static const struct got_error *
1980 draw_tree_entries(struct tog_view *view,
1981 struct got_tree_entry **first_displayed_entry,
1982 struct got_tree_entry **last_displayed_entry,
1983 struct got_tree_entry **selected_entry, int *ndisplayed,
1984 const char *label, int show_ids, const char *parent_path,
1985 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1987 const struct got_error *err = NULL;
1988 struct got_tree_entry *te;
1989 wchar_t *wline;
1990 int width, n;
1992 *ndisplayed = 0;
1994 werase(view->window);
1996 if (limit == 0)
1997 return NULL;
1999 err = format_line(&wline, &width, label, view->ncols);
2000 if (err)
2001 return err;
2002 waddwstr(view->window, wline);
2003 free(wline);
2004 wline = NULL;
2005 if (width < view->ncols)
2006 waddch(view->window, '\n');
2007 if (--limit <= 0)
2008 return NULL;
2009 err = format_line(&wline, &width, parent_path, view->ncols);
2010 if (err)
2011 return err;
2012 waddwstr(view->window, wline);
2013 free(wline);
2014 wline = NULL;
2015 if (width < view->ncols)
2016 waddch(view->window, '\n');
2017 if (--limit <= 0)
2018 return NULL;
2019 waddch(view->window, '\n');
2020 if (--limit <= 0)
2021 return NULL;
2023 te = SIMPLEQ_FIRST(&entries->head);
2024 if (*first_displayed_entry == NULL) {
2025 if (selected == 0) {
2026 wstandout(view->window);
2027 *selected_entry = NULL;
2029 waddstr(view->window, " ..\n"); /* parent directory */
2030 if (selected == 0)
2031 wstandend(view->window);
2032 (*ndisplayed)++;
2033 if (--limit <= 0)
2034 return NULL;
2035 n = 1;
2036 } else {
2037 n = 0;
2038 while (te != *first_displayed_entry)
2039 te = SIMPLEQ_NEXT(te, entry);
2042 while (te) {
2043 char *line = NULL, *id_str = NULL;
2045 if (show_ids) {
2046 err = got_object_id_str(&id_str, te->id);
2047 if (err)
2048 return got_error_from_errno();
2050 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2051 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2052 free(id_str);
2053 return got_error_from_errno();
2055 free(id_str);
2056 err = format_line(&wline, &width, line, view->ncols);
2057 if (err) {
2058 free(line);
2059 break;
2061 if (n == selected) {
2062 wstandout(view->window);
2063 *selected_entry = te;
2065 waddwstr(view->window, wline);
2066 if (width < view->ncols)
2067 waddch(view->window, '\n');
2068 if (n == selected)
2069 wstandend(view->window);
2070 free(line);
2071 free(wline);
2072 wline = NULL;
2073 n++;
2074 (*ndisplayed)++;
2075 *last_displayed_entry = te;
2076 if (--limit <= 0)
2077 break;
2078 te = SIMPLEQ_NEXT(te, entry);
2081 return err;
2084 static void
2085 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2086 const struct got_tree_entries *entries, int isroot)
2088 struct got_tree_entry *te, *prev;
2089 int i;
2091 if (*first_displayed_entry == NULL)
2092 return;
2094 te = SIMPLEQ_FIRST(&entries->head);
2095 if (*first_displayed_entry == te) {
2096 if (!isroot)
2097 *first_displayed_entry = NULL;
2098 return;
2101 /* XXX this is stupid... switch to TAILQ? */
2102 for (i = 0; i < maxscroll; i++) {
2103 while (te != *first_displayed_entry) {
2104 prev = te;
2105 te = SIMPLEQ_NEXT(te, entry);
2107 *first_displayed_entry = prev;
2108 te = SIMPLEQ_FIRST(&entries->head);
2110 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2111 *first_displayed_entry = NULL;
2114 static void
2115 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2116 struct got_tree_entry *last_displayed_entry,
2117 const struct got_tree_entries *entries)
2119 struct got_tree_entry *next;
2120 int n = 0;
2122 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2123 return;
2125 if (*first_displayed_entry)
2126 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2127 else
2128 next = SIMPLEQ_FIRST(&entries->head);
2129 while (next) {
2130 *first_displayed_entry = next;
2131 if (++n >= maxscroll)
2132 break;
2133 next = SIMPLEQ_NEXT(next, entry);
2137 struct tog_parent_tree {
2138 TAILQ_ENTRY(tog_parent_tree) entry;
2139 struct got_tree_object *tree;
2140 struct got_tree_entry *first_displayed_entry;
2141 struct got_tree_entry *selected_entry;
2142 int selected;
2145 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2147 static const struct got_error *
2148 tree_entry_path(char **path, struct tog_parent_trees *parents,
2149 struct got_tree_entry *te)
2151 const struct got_error *err = NULL;
2152 struct tog_parent_tree *pt;
2153 size_t len = 2; /* for leading slash and NUL */
2155 TAILQ_FOREACH(pt, parents, entry)
2156 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2157 if (te)
2158 len += strlen(te->name);
2160 *path = calloc(1, len);
2161 if (path == NULL)
2162 return got_error_from_errno();
2164 (*path)[0] = '/';
2165 pt = TAILQ_LAST(parents, tog_parent_trees);
2166 while (pt) {
2167 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2168 err = got_error(GOT_ERR_NO_SPACE);
2169 goto done;
2171 if (strlcat(*path, "/", len) >= len) {
2172 err = got_error(GOT_ERR_NO_SPACE);
2173 goto done;
2175 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2177 if (te) {
2178 if (strlcat(*path, te->name, len) >= len) {
2179 err = got_error(GOT_ERR_NO_SPACE);
2180 goto done;
2183 done:
2184 if (err) {
2185 free(*path);
2186 *path = NULL;
2188 return err;
2191 static const struct got_error *
2192 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2193 struct got_object_id *commit_id, struct got_repository *repo)
2195 const struct got_error *err = NULL;
2196 char *path;
2198 err = tree_entry_path(&path, parents, te);
2199 if (err)
2200 return err;
2202 err = show_blame_view(path, commit_id, repo);
2203 free(path);
2204 return err;
2207 static const struct got_error *
2208 log_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
2209 struct got_object_id *commit_id, struct got_repository *repo)
2211 const struct got_error *err = NULL;
2212 char *path;
2214 err = tree_entry_path(&path, parents, te);
2215 if (err)
2216 return err;
2218 err = show_log_view(commit_id, repo, path);
2219 free(path);
2220 return err;
2223 static const struct got_error *
2224 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2225 struct got_repository *repo)
2227 const struct got_error *err = NULL;
2228 int ch, done = 0, selected = 0, show_ids = 0;
2229 struct got_tree_object *tree = root;
2230 const struct got_tree_entries *entries;
2231 struct got_tree_entry *first_displayed_entry = NULL;
2232 struct got_tree_entry *last_displayed_entry = NULL;
2233 struct got_tree_entry *selected_entry = NULL;
2234 char *commit_id_str = NULL, *tree_label = NULL;
2235 int nentries, ndisplayed;
2236 struct tog_parent_trees parents;
2237 struct tog_view *view = NULL;
2239 TAILQ_INIT(&parents);
2241 err = got_object_id_str(&commit_id_str, commit_id);
2242 if (err != NULL)
2243 goto done;
2245 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2246 err = got_error_from_errno();
2247 goto done;
2250 view = open_view(0, 0, 0, 0);
2251 if (view == NULL) {
2252 err = got_error_from_errno();
2253 goto done;
2255 show_panel(view->panel);
2257 entries = got_object_tree_get_entries(root);
2258 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2259 while (!done) {
2260 char *parent_path;
2261 entries = got_object_tree_get_entries(tree);
2262 nentries = entries->nentries;
2263 if (tree != root)
2264 nentries++; /* '..' directory */
2266 err = tree_entry_path(&parent_path, &parents, NULL);
2267 if (err)
2268 goto done;
2270 err = draw_tree_entries(view, &first_displayed_entry,
2271 &last_displayed_entry, &selected_entry, &ndisplayed,
2272 tree_label, show_ids, parent_path, entries, selected,
2273 view->nlines, tree == root);
2274 free(parent_path);
2275 if (err)
2276 break;
2278 nodelay(stdscr, FALSE);
2279 ch = wgetch(view->window);
2280 nodelay(stdscr, TRUE);
2281 switch (ch) {
2282 case 'q':
2283 done = 1;
2284 break;
2285 case 'i':
2286 show_ids = !show_ids;
2287 break;
2288 case 'l':
2289 if (selected_entry) {
2290 err = log_tree_entry(selected_entry,
2291 &parents, commit_id, repo);
2292 if (err)
2293 goto done;
2295 break;
2296 case 'k':
2297 case KEY_UP:
2298 if (selected > 0)
2299 selected--;
2300 if (selected > 0)
2301 break;
2302 tree_scroll_up(&first_displayed_entry, 1,
2303 entries, tree == root);
2304 break;
2305 case KEY_PPAGE:
2306 if (SIMPLEQ_FIRST(&entries->head) ==
2307 first_displayed_entry) {
2308 if (tree != root)
2309 first_displayed_entry = NULL;
2310 selected = 0;
2311 break;
2313 tree_scroll_up(&first_displayed_entry,
2314 view->nlines, entries, tree == root);
2315 break;
2316 case 'j':
2317 case KEY_DOWN:
2318 if (selected < ndisplayed - 1) {
2319 selected++;
2320 break;
2322 tree_scroll_down(&first_displayed_entry, 1,
2323 last_displayed_entry, entries);
2324 break;
2325 case KEY_NPAGE:
2326 tree_scroll_down(&first_displayed_entry,
2327 view->nlines, last_displayed_entry,
2328 entries);
2329 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2330 break;
2331 /* can't scroll any further; move cursor down */
2332 if (selected < ndisplayed - 1)
2333 selected = ndisplayed - 1;
2334 break;
2335 case KEY_ENTER:
2336 case '\r':
2337 if (selected_entry == NULL) {
2338 struct tog_parent_tree *parent;
2339 case KEY_BACKSPACE:
2340 /* user selected '..' */
2341 if (tree == root)
2342 break;
2343 parent = TAILQ_FIRST(&parents);
2344 TAILQ_REMOVE(&parents, parent, entry);
2345 got_object_tree_close(tree);
2346 tree = parent->tree;
2347 first_displayed_entry =
2348 parent->first_displayed_entry;
2349 selected_entry = parent->selected_entry;
2350 selected = parent->selected;
2351 free(parent);
2352 } else if (S_ISDIR(selected_entry->mode)) {
2353 struct tog_parent_tree *parent;
2354 struct got_tree_object *child;
2355 err = got_object_open_as_tree(
2356 &child, repo, selected_entry->id);
2357 if (err)
2358 goto done;
2359 parent = calloc(1, sizeof(*parent));
2360 if (parent == NULL) {
2361 err = got_error_from_errno();
2362 goto done;
2364 parent->tree = tree;
2365 parent->first_displayed_entry =
2366 first_displayed_entry;
2367 parent->selected_entry = selected_entry;
2368 parent->selected = selected;
2369 TAILQ_INSERT_HEAD(&parents, parent,
2370 entry);
2371 tree = child;
2372 selected = 0;
2373 first_displayed_entry = NULL;
2374 } else if (S_ISREG(selected_entry->mode)) {
2375 err = blame_tree_entry(selected_entry,
2376 &parents, commit_id, repo);
2377 if (err)
2378 goto done;
2380 break;
2381 case KEY_RESIZE:
2382 view_resize(view);
2383 if (selected > view->nlines)
2384 selected = ndisplayed - 1;
2385 break;
2386 default:
2387 break;
2390 done:
2391 if (view)
2392 close_view(view);
2393 free(tree_label);
2394 free(commit_id_str);
2395 while (!TAILQ_EMPTY(&parents)) {
2396 struct tog_parent_tree *parent;
2397 parent = TAILQ_FIRST(&parents);
2398 TAILQ_REMOVE(&parents, parent, entry);
2399 free(parent);
2402 if (tree != root)
2403 got_object_tree_close(tree);
2404 return err;
2407 __dead static void
2408 usage_tree(void)
2410 endwin();
2411 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2412 getprogname());
2413 exit(1);
2416 static const struct got_error *
2417 cmd_tree(int argc, char *argv[])
2419 const struct got_error *error;
2420 struct got_repository *repo = NULL;
2421 char *repo_path = NULL;
2422 struct got_object_id *commit_id = NULL;
2423 char *commit_id_arg = NULL;
2424 struct got_commit_object *commit = NULL;
2425 struct got_tree_object *tree = NULL;
2426 int ch;
2428 #ifndef PROFILE
2429 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2430 err(1, "pledge");
2431 #endif
2433 while ((ch = getopt(argc, argv, "c:")) != -1) {
2434 switch (ch) {
2435 case 'c':
2436 commit_id_arg = optarg;
2437 break;
2438 default:
2439 usage();
2440 /* NOTREACHED */
2444 argc -= optind;
2445 argv += optind;
2447 if (argc == 0) {
2448 repo_path = getcwd(NULL, 0);
2449 if (repo_path == NULL)
2450 return got_error_from_errno();
2451 } else if (argc == 1) {
2452 repo_path = realpath(argv[0], NULL);
2453 if (repo_path == NULL)
2454 return got_error_from_errno();
2455 } else
2456 usage_log();
2458 error = got_repo_open(&repo, repo_path);
2459 free(repo_path);
2460 if (error != NULL)
2461 return error;
2463 if (commit_id_arg == NULL) {
2464 error = get_head_commit_id(&commit_id, repo);
2465 if (error != NULL)
2466 goto done;
2467 } else {
2468 struct got_object *obj;
2469 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2470 if (error == NULL) {
2471 commit_id = got_object_get_id(obj);
2472 if (commit_id == NULL)
2473 error = got_error_from_errno();
2476 if (error != NULL)
2477 goto done;
2479 error = got_object_open_as_commit(&commit, repo, commit_id);
2480 if (error != NULL)
2481 goto done;
2483 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2484 if (error != NULL)
2485 goto done;
2487 error = show_tree_view(tree, commit_id, repo);
2488 done:
2489 free(commit_id);
2490 if (commit)
2491 got_object_commit_close(commit);
2492 if (tree)
2493 got_object_tree_close(tree);
2494 if (repo)
2495 got_repo_close(repo);
2496 return error;
2498 static void
2499 init_curses(void)
2501 initscr();
2502 cbreak();
2503 noecho();
2504 nonl();
2505 intrflush(stdscr, FALSE);
2506 keypad(stdscr, TRUE);
2507 curs_set(0);
2510 __dead static void
2511 usage(void)
2513 int i;
2515 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2516 "Available commands:\n", getprogname());
2517 for (i = 0; i < nitems(tog_commands); i++) {
2518 struct tog_cmd *cmd = &tog_commands[i];
2519 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2521 exit(1);
2524 static char **
2525 make_argv(const char *arg0, const char *arg1)
2527 char **argv;
2528 int argc = (arg1 == NULL ? 1 : 2);
2530 argv = calloc(argc, sizeof(char *));
2531 if (argv == NULL)
2532 err(1, "calloc");
2533 argv[0] = strdup(arg0);
2534 if (argv[0] == NULL)
2535 err(1, "calloc");
2536 if (arg1) {
2537 argv[1] = strdup(arg1);
2538 if (argv[1] == NULL)
2539 err(1, "calloc");
2542 return argv;
2545 int
2546 main(int argc, char *argv[])
2548 const struct got_error *error = NULL;
2549 struct tog_cmd *cmd = NULL;
2550 int ch, hflag = 0;
2551 char **cmd_argv = NULL;
2553 setlocale(LC_ALL, "");
2555 while ((ch = getopt(argc, argv, "h")) != -1) {
2556 switch (ch) {
2557 case 'h':
2558 hflag = 1;
2559 break;
2560 default:
2561 usage();
2562 /* NOTREACHED */
2566 argc -= optind;
2567 argv += optind;
2568 optind = 0;
2569 optreset = 1;
2571 if (argc == 0) {
2572 if (hflag)
2573 usage();
2574 /* Build an argument vector which runs a default command. */
2575 cmd = &tog_commands[0];
2576 cmd_argv = make_argv(cmd->name, NULL);
2577 argc = 1;
2578 } else {
2579 int i;
2581 /* Did the user specific a command? */
2582 for (i = 0; i < nitems(tog_commands); i++) {
2583 if (strncmp(tog_commands[i].name, argv[0],
2584 strlen(argv[0])) == 0) {
2585 cmd = &tog_commands[i];
2586 if (hflag)
2587 tog_commands[i].cmd_usage();
2588 break;
2591 if (cmd == NULL) {
2592 /* Did the user specify a repository? */
2593 char *repo_path = realpath(argv[0], NULL);
2594 if (repo_path) {
2595 struct got_repository *repo;
2596 error = got_repo_open(&repo, repo_path);
2597 if (error == NULL)
2598 got_repo_close(repo);
2599 } else
2600 error = got_error_from_errno();
2601 if (error) {
2602 if (hflag) {
2603 fprintf(stderr, "%s: '%s' is not a "
2604 "known command\n", getprogname(),
2605 argv[0]);
2606 usage();
2608 fprintf(stderr, "%s: '%s' is neither a known "
2609 "command nor a path to a repository\n",
2610 getprogname(), argv[0]);
2611 free(repo_path);
2612 return 1;
2614 cmd = &tog_commands[0];
2615 cmd_argv = make_argv(cmd->name, repo_path);
2616 argc = 2;
2617 free(repo_path);
2621 init_curses();
2623 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2624 if (error)
2625 goto done;
2626 done:
2627 endwin();
2628 free(cmd_argv);
2629 if (error)
2630 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2631 return 0;