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 struct tog_view *parent;
91 };
93 static const struct got_error *
94 show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
95 struct got_repository *);
96 static const struct got_error *
97 show_log_view(struct tog_view *, struct got_object_id *,
98 struct got_repository *, const char *);
99 static const struct got_error *
100 show_blame_view(struct tog_view *, const char *, struct got_object_id *,
101 struct got_repository *);
102 static const struct got_error *
103 show_tree_view(struct tog_view *, struct got_tree_object *,
104 struct got_object_id *, struct got_repository *);
106 static void
107 close_view(struct tog_view *view)
109 if (view->panel)
110 del_panel(view->panel);
111 if (view->window)
112 delwin(view->window);
113 free(view);
116 static struct tog_view *
117 open_view(int nlines, int ncols, int begin_y, int begin_x,
118 struct tog_view *parent)
120 struct tog_view *view = malloc(sizeof(*view));
122 if (view == NULL)
123 return NULL;
125 view->parent = parent;
126 view->lines = LINES;
127 view->cols = COLS;
128 view->nlines = nlines ? nlines : LINES - begin_y;
129 view->ncols = ncols ? ncols : COLS - begin_x;
130 view->begin_y = begin_y;
131 view->begin_x = begin_x;
132 view->window = newwin(nlines, ncols, begin_y, begin_x);
133 if (view->window == NULL) {
134 close_view(view);
135 return NULL;
137 view->panel = new_panel(view->window);
138 if (view->panel == NULL) {
139 close_view(view);
140 return NULL;
143 keypad(view->window, TRUE);
144 return view;
147 const struct got_error *
148 view_resize(struct tog_view *view)
150 int nlines, ncols;
152 while (view) {
153 if (view->lines > LINES)
154 nlines = view->nlines - (view->lines - LINES);
155 else
156 nlines = view->nlines + (LINES - view->lines);
158 if (view->cols > COLS)
159 ncols = view->ncols - (view->cols - COLS);
160 else
161 ncols = view->ncols + (COLS - view->cols);
163 if (wresize(view->window, nlines, ncols) == ERR)
164 return got_error_from_errno();
166 view->nlines = nlines;
167 view->ncols = ncols;
168 view->lines = LINES;
169 view->cols = COLS;
171 view = view->parent;
174 return NULL;
177 __dead static void
178 usage_log(void)
180 endwin();
181 fprintf(stderr, "usage: %s log [-c commit] [-r repository-path] [path]\n",
182 getprogname());
183 exit(1);
186 /* Create newly allocated wide-character string equivalent to a byte string. */
187 static const struct got_error *
188 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
190 char *vis = NULL;
191 const struct got_error *err = NULL;
193 *ws = NULL;
194 *wlen = mbstowcs(NULL, s, 0);
195 if (*wlen == (size_t)-1) {
196 int vislen;
197 if (errno != EILSEQ)
198 return got_error_from_errno();
200 /* byte string invalid in current encoding; try to "fix" it */
201 err = got_mbsavis(&vis, &vislen, s);
202 if (err)
203 return err;
204 *wlen = mbstowcs(NULL, vis, 0);
205 if (*wlen == (size_t)-1) {
206 err = got_error_from_errno(); /* give up */
207 goto done;
211 *ws = calloc(*wlen + 1, sizeof(*ws));
212 if (*ws == NULL) {
213 err = got_error_from_errno();
214 goto done;
217 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
218 err = got_error_from_errno();
219 done:
220 free(vis);
221 if (err) {
222 free(*ws);
223 *ws = NULL;
224 *wlen = 0;
226 return err;
229 /* Format a line for display, ensuring that it won't overflow a width limit. */
230 static const struct got_error *
231 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
233 const struct got_error *err = NULL;
234 int cols = 0;
235 wchar_t *wline = NULL;
236 size_t wlen;
237 int i;
239 *wlinep = NULL;
240 *widthp = 0;
242 err = mbs2ws(&wline, &wlen, line);
243 if (err)
244 return err;
246 i = 0;
247 while (i < wlen && cols < wlimit) {
248 int width = wcwidth(wline[i]);
249 switch (width) {
250 case 0:
251 i++;
252 break;
253 case 1:
254 case 2:
255 if (cols + width <= wlimit) {
256 cols += width;
257 i++;
259 break;
260 case -1:
261 if (wline[i] == L'\t')
262 cols += TABSIZE - ((cols + 1) % TABSIZE);
263 i++;
264 break;
265 default:
266 err = got_error_from_errno();
267 goto done;
270 wline[i] = L'\0';
271 if (widthp)
272 *widthp = cols;
273 done:
274 if (err)
275 free(wline);
276 else
277 *wlinep = wline;
278 return err;
281 static const struct got_error *
282 draw_commit(struct tog_view *view, struct got_commit_object *commit,
283 struct got_object_id *id)
285 const struct got_error *err = NULL;
286 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
287 char *logmsg0 = NULL, *logmsg = NULL;
288 char *author0 = NULL, *author = NULL;
289 wchar_t *wlogmsg = NULL, *wauthor = NULL;
290 int author_width, logmsg_width;
291 char *newline, *smallerthan;
292 char *line = NULL;
293 int col, limit;
294 static const size_t date_display_cols = 9;
295 static const size_t author_display_cols = 16;
296 const int avail = view->ncols;
298 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
299 >= sizeof(datebuf))
300 return got_error(GOT_ERR_NO_SPACE);
302 if (avail < date_display_cols)
303 limit = MIN(sizeof(datebuf) - 1, avail);
304 else
305 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
306 waddnstr(view->window, datebuf, limit);
307 col = limit + 1;
308 if (col > avail)
309 goto done;
311 author0 = strdup(commit->author);
312 if (author0 == NULL) {
313 err = got_error_from_errno();
314 goto done;
316 author = author0;
317 smallerthan = strchr(author, '<');
318 if (smallerthan)
319 *smallerthan = '\0';
320 else {
321 char *at = strchr(author, '@');
322 if (at)
323 *at = '\0';
325 limit = avail - col;
326 err = format_line(&wauthor, &author_width, author, limit);
327 if (err)
328 goto done;
329 waddwstr(view->window, wauthor);
330 col += author_width;
331 while (col <= avail && author_width < author_display_cols + 1) {
332 waddch(view->window, ' ');
333 col++;
334 author_width++;
336 if (col > avail)
337 goto done;
339 logmsg0 = strdup(commit->logmsg);
340 if (logmsg0 == NULL) {
341 err = got_error_from_errno();
342 goto done;
344 logmsg = logmsg0;
345 while (*logmsg == '\n')
346 logmsg++;
347 newline = strchr(logmsg, '\n');
348 if (newline)
349 *newline = '\0';
350 limit = avail - col;
351 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
352 if (err)
353 goto done;
354 waddwstr(view->window, wlogmsg);
355 col += logmsg_width;
356 while (col <= avail) {
357 waddch(view->window, ' ');
358 col++;
360 done:
361 free(logmsg0);
362 free(wlogmsg);
363 free(author0);
364 free(wauthor);
365 free(line);
366 return err;
369 struct commit_queue_entry {
370 TAILQ_ENTRY(commit_queue_entry) entry;
371 struct got_object_id *id;
372 struct got_commit_object *commit;
373 };
374 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
375 struct commit_queue {
376 int ncommits;
377 struct commit_queue_head head;
378 };
380 static struct commit_queue_entry *
381 alloc_commit_queue_entry(struct got_commit_object *commit,
382 struct got_object_id *id)
384 struct commit_queue_entry *entry;
386 entry = calloc(1, sizeof(*entry));
387 if (entry == NULL)
388 return NULL;
390 entry->id = id;
391 entry->commit = commit;
392 return entry;
395 static void
396 pop_commit(struct commit_queue *commits)
398 struct commit_queue_entry *entry;
400 entry = TAILQ_FIRST(&commits->head);
401 TAILQ_REMOVE(&commits->head, entry, entry);
402 got_object_commit_close(entry->commit);
403 commits->ncommits--;
404 /* Don't free entry->id! It is owned by the commit graph. */
405 free(entry);
408 static void
409 free_commits(struct commit_queue *commits)
411 while (!TAILQ_EMPTY(&commits->head))
412 pop_commit(commits);
415 static const struct got_error *
416 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
417 struct got_object_id *start_id, int minqueue, int init,
418 struct got_repository *repo, const char *path)
420 const struct got_error *err = NULL;
421 struct got_object_id *id;
422 struct commit_queue_entry *entry;
423 int nfetched, nqueued = 0, found_obj = 0;
424 int is_root_path = strcmp(path, "/") == 0;
426 err = got_commit_graph_iter_start(graph, start_id);
427 if (err)
428 return err;
430 entry = TAILQ_LAST(&commits->head, commit_queue_head);
431 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
432 int nfetched;
434 /* Start ID's commit is already on the queue; skip over it. */
435 err = got_commit_graph_iter_next(&id, graph);
436 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
437 return err;
439 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
440 if (err)
441 return err;
444 while (1) {
445 struct got_commit_object *commit;
447 err = got_commit_graph_iter_next(&id, graph);
448 if (err) {
449 if (err->code != GOT_ERR_ITER_NEED_MORE)
450 break;
451 if (nqueued >= minqueue) {
452 err = NULL;
453 break;
455 err = got_commit_graph_fetch_commits(&nfetched,
456 graph, 1, repo);
457 if (err)
458 return err;
459 continue;
461 if (id == NULL)
462 break;
464 err = got_object_open_as_commit(&commit, repo, id);
465 if (err)
466 break;
468 if (!is_root_path) {
469 struct got_object *obj;
470 struct got_object_qid *pid;
471 int changed = 0;
473 err = got_object_open_by_path(&obj, repo, id, path);
474 if (err) {
475 got_object_commit_close(commit);
476 if (err->code == GOT_ERR_NO_OBJ &&
477 (found_obj || !init)) {
478 /* History stops here. */
479 err = got_error(GOT_ERR_ITER_COMPLETED);
481 break;
483 found_obj = 1;
485 pid = SIMPLEQ_FIRST(&commit->parent_ids);
486 if (pid != NULL) {
487 struct got_object *pobj;
488 err = got_object_open_by_path(&pobj, repo,
489 pid->id, path);
490 if (err) {
491 if (err->code != GOT_ERR_NO_OBJ) {
492 got_object_close(obj);
493 got_object_commit_close(commit);
494 break;
496 err = NULL;
497 changed = 1;
498 } else {
499 struct got_object_id *id, *pid;
500 id = got_object_get_id(obj);
501 if (id == NULL) {
502 err = got_error_from_errno();
503 got_object_close(obj);
504 got_object_close(pobj);
505 break;
507 pid = got_object_get_id(pobj);
508 if (pid == NULL) {
509 err = got_error_from_errno();
510 free(id);
511 got_object_close(obj);
512 got_object_close(pobj);
513 break;
515 changed =
516 (got_object_id_cmp(id, pid) != 0);
517 got_object_close(pobj);
518 free(id);
519 free(pid);
522 got_object_close(obj);
523 if (!changed) {
524 got_object_commit_close(commit);
525 continue;
529 entry = alloc_commit_queue_entry(commit, id);
530 if (entry == NULL) {
531 err = got_error_from_errno();
532 break;
534 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
535 nqueued++;
536 commits->ncommits++;
539 return err;
542 static const struct got_error *
543 fetch_next_commit(struct commit_queue_entry **pentry,
544 struct commit_queue_entry *entry, struct commit_queue *commits,
545 struct got_commit_graph *graph, struct got_repository *repo,
546 const char *path)
548 const struct got_error *err = NULL;
550 *pentry = NULL;
552 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
553 if (err)
554 return err;
556 /* Next entry to display should now be available. */
557 *pentry = TAILQ_NEXT(entry, entry);
558 if (*pentry == NULL)
559 return got_error(GOT_ERR_NO_OBJ);
561 return NULL;
564 static const struct got_error *
565 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
567 const struct got_error *err = NULL;
568 struct got_reference *head_ref;
570 *head_id = NULL;
572 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
573 if (err)
574 return err;
576 err = got_ref_resolve(head_id, repo, head_ref);
577 got_ref_close(head_ref);
578 if (err) {
579 *head_id = NULL;
580 return err;
583 return NULL;
586 static const struct got_error *
587 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
588 struct commit_queue_entry **selected, struct commit_queue_entry *first,
589 struct commit_queue *commits, int selected_idx, int limit,
590 struct got_commit_graph *graph, struct got_repository *repo,
591 const char *path)
593 const struct got_error *err = NULL;
594 struct commit_queue_entry *entry;
595 int ncommits, width;
596 char *id_str, *header;
597 wchar_t *wline;
599 entry = first;
600 ncommits = 0;
601 while (entry) {
602 if (ncommits == selected_idx) {
603 *selected = entry;
604 break;
606 entry = TAILQ_NEXT(entry, entry);
607 ncommits++;
610 err = got_object_id_str(&id_str, (*selected)->id);
611 if (err)
612 return err;
614 if (path) {
615 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
616 err = got_error_from_errno();
617 free(id_str);
618 return err;
620 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
621 err = got_error_from_errno();
622 free(id_str);
623 return err;
625 free(id_str);
626 err = format_line(&wline, &width, header, view->ncols);
627 if (err) {
628 free(header);
629 return err;
631 free(header);
633 werase(view->window);
635 waddwstr(view->window, wline);
636 if (width < view->ncols)
637 waddch(view->window, '\n');
638 free(wline);
639 if (limit <= 1)
640 return NULL;
642 entry = first;
643 *last = first;
644 ncommits = 0;
645 while (entry) {
646 if (ncommits >= limit - 1)
647 break;
648 if (ncommits == selected_idx)
649 wstandout(view->window);
650 err = draw_commit(view, entry->commit, entry->id);
651 if (ncommits == selected_idx)
652 wstandend(view->window);
653 if (err)
654 break;
655 ncommits++;
656 *last = entry;
657 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
658 err = queue_commits(graph, commits, entry->id, 1,
659 0, repo, path);
660 if (err) {
661 if (err->code != GOT_ERR_ITER_COMPLETED)
662 return err;
663 err = NULL;
666 entry = TAILQ_NEXT(entry, entry);
669 update_panels();
670 doupdate();
672 return err;
675 static void
676 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
677 struct commit_queue *commits)
679 struct commit_queue_entry *entry;
680 int nscrolled = 0;
682 entry = TAILQ_FIRST(&commits->head);
683 if (*first_displayed_entry == entry)
684 return;
686 entry = *first_displayed_entry;
687 while (entry && nscrolled < maxscroll) {
688 entry = TAILQ_PREV(entry, commit_queue_head, entry);
689 if (entry) {
690 *first_displayed_entry = entry;
691 nscrolled++;
696 static const struct got_error *
697 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
698 struct commit_queue_entry *last_displayed_entry,
699 struct commit_queue *commits, struct got_commit_graph *graph,
700 struct got_repository *repo, const char *path)
702 const struct got_error *err = NULL;
703 struct commit_queue_entry *pentry;
704 int nscrolled = 0;
706 do {
707 pentry = TAILQ_NEXT(last_displayed_entry, entry);
708 if (pentry == NULL) {
709 err = fetch_next_commit(&pentry, last_displayed_entry,
710 commits, graph, repo, path);
711 if (err || pentry == NULL)
712 break;
714 last_displayed_entry = pentry;
716 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
717 if (pentry == NULL)
718 break;
719 *first_displayed_entry = pentry;
720 } while (++nscrolled < maxscroll);
722 return err;
725 static const struct got_error *
726 show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
727 struct got_repository *repo)
729 const struct got_error *err;
730 struct got_object *obj1 = NULL, *obj2 = NULL;
731 struct got_object_qid *parent_id;
732 struct tog_view *view;
734 err = got_object_open(&obj2, repo, entry->id);
735 if (err)
736 return err;
738 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
739 if (parent_id) {
740 err = got_object_open(&obj1, repo, parent_id->id);
741 if (err)
742 goto done;
745 view = open_view(0, 0, 0, 0, parent_view);
746 if (view == NULL) {
747 err = got_error_from_errno();
748 goto done;
751 err = show_diff_view(view, obj1, obj2, repo);
752 close_view(view);
753 done:
754 if (obj1)
755 got_object_close(obj1);
756 if (obj2)
757 got_object_close(obj2);
758 return err;
761 static const struct got_error *
762 browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
763 struct got_repository *repo)
765 const struct got_error *err = NULL;
766 struct got_tree_object *tree;
767 struct tog_view *view;
769 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
770 if (err)
771 return err;
773 view = open_view(0, 0, 0, 0, parent_view);
774 if (view == NULL) {
775 err = got_error_from_errno();
776 goto done;
778 err = show_tree_view(view, tree, entry->id, repo);
779 close_view(view);
780 done:
781 got_object_tree_close(tree);
782 return err;
785 static const struct got_error *
786 show_log_view(struct tog_view *view, struct got_object_id *start_id,
787 struct got_repository *repo, const char *path)
789 const struct got_error *err = NULL;
790 struct got_object_id *head_id = NULL;
791 int ch, done = 0, selected = 0, nfetched;
792 struct got_commit_graph *graph = NULL;
793 struct commit_queue commits;
794 struct commit_queue_entry *first_displayed_entry = NULL;
795 struct commit_queue_entry *last_displayed_entry = NULL;
796 struct commit_queue_entry *selected_entry = NULL;
797 char *in_repo_path = NULL;
799 err = got_repo_map_path(&in_repo_path, repo, path);
800 if (err != NULL)
801 goto done;
803 err = get_head_commit_id(&head_id, repo);
804 if (err)
805 return err;
807 /* The graph contains all commits. */
808 err = got_commit_graph_open(&graph, head_id, 0, repo);
809 if (err)
810 goto done;
811 /* The commit queue contains a subset of commits filtered by path. */
812 TAILQ_INIT(&commits.head);
813 commits.ncommits = 0;
815 /* Populate commit graph with a sufficient number of commits. */
816 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
817 repo);
818 if (err)
819 goto done;
821 /*
822 * Open the initial batch of commits, sorted in commit graph order.
823 * We keep all commits open throughout the lifetime of the log view
824 * in order to avoid having to re-fetch commits from disk while
825 * updating the display.
826 */
827 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
828 in_repo_path);
829 if (err) {
830 if (err->code != GOT_ERR_ITER_COMPLETED)
831 goto done;
832 err = NULL;
835 show_panel(view->panel);
837 first_displayed_entry = TAILQ_FIRST(&commits.head);
838 selected_entry = first_displayed_entry;
839 while (!done) {
840 err = draw_commits(view, &last_displayed_entry, &selected_entry,
841 first_displayed_entry, &commits, selected, view->nlines,
842 graph, repo, in_repo_path);
843 if (err)
844 goto done;
846 nodelay(stdscr, FALSE);
847 ch = wgetch(view->window);
848 nodelay(stdscr, TRUE);
849 switch (ch) {
850 case ERR:
851 break;
852 case 'q':
853 done = 1;
854 break;
855 case 'k':
856 case KEY_UP:
857 if (selected > 0)
858 selected--;
859 if (selected > 0)
860 break;
861 scroll_up(&first_displayed_entry, 1, &commits);
862 break;
863 case KEY_PPAGE:
864 if (TAILQ_FIRST(&commits.head) ==
865 first_displayed_entry) {
866 selected = 0;
867 break;
869 scroll_up(&first_displayed_entry, view->nlines,
870 &commits);
871 break;
872 case 'j':
873 case KEY_DOWN:
874 if (selected < MIN(view->nlines - 2,
875 commits.ncommits - 1)) {
876 selected++;
877 break;
879 err = scroll_down(&first_displayed_entry, 1,
880 last_displayed_entry, &commits, graph,
881 repo, in_repo_path);
882 if (err) {
883 if (err->code != GOT_ERR_ITER_COMPLETED)
884 goto done;
885 err = NULL;
887 break;
888 case KEY_NPAGE: {
889 struct commit_queue_entry *first = first_displayed_entry;
890 err = scroll_down(&first_displayed_entry, view->nlines,
891 last_displayed_entry, &commits, graph,
892 repo, in_repo_path);
893 if (err) {
894 if (err->code != GOT_ERR_ITER_COMPLETED)
895 goto done;
896 /* can't scroll any further; move cursor down */
897 if (first == first_displayed_entry && selected <
898 MIN(view->nlines - 2, commits.ncommits - 1)) {
899 selected = MIN(view->nlines - 2,
900 commits.ncommits - 1);
902 err = NULL;
904 break;
906 case KEY_RESIZE:
907 err = view_resize(view);
908 if (err)
909 goto done;
910 if (selected > view->nlines - 2)
911 selected = view->nlines - 2;
912 if (selected > commits.ncommits - 1)
913 selected = commits.ncommits - 1;
914 break;
915 case KEY_ENTER:
916 case '\r':
917 err = show_commit(view, selected_entry, repo);
918 if (err)
919 goto done;
920 show_panel(view->panel);
921 break;
922 case 't':
923 err = browse_commit(view, selected_entry, repo);
924 if (err)
925 goto done;
926 show_panel(view->panel);
927 break;
928 default:
929 break;
932 done:
933 free(head_id);
934 if (graph)
935 got_commit_graph_close(graph);
936 free_commits(&commits);
937 free(in_repo_path);
938 return err;
941 static const struct got_error *
942 cmd_log(int argc, char *argv[])
944 const struct got_error *error;
945 struct got_repository *repo = NULL;
946 struct got_object_id *start_id = NULL;
947 char *path = NULL, *repo_path = NULL, *cwd = NULL;
948 char *start_commit = NULL;
949 int ch;
950 struct tog_view *view;
952 #ifndef PROFILE
953 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
954 err(1, "pledge");
955 #endif
957 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
958 switch (ch) {
959 case 'c':
960 start_commit = optarg;
961 break;
962 case 'r':
963 repo_path = realpath(optarg, NULL);
964 if (repo_path == NULL)
965 err(1, "-r option");
966 break;
967 default:
968 usage();
969 /* NOTREACHED */
973 argc -= optind;
974 argv += optind;
976 if (argc == 0)
977 path = strdup("");
978 else if (argc == 1)
979 path = strdup(argv[0]);
980 else
981 usage_log();
982 if (path == NULL)
983 return got_error_from_errno();
985 cwd = getcwd(NULL, 0);
986 if (cwd == NULL) {
987 error = got_error_from_errno();
988 goto done;
990 if (repo_path == NULL) {
991 repo_path = strdup(cwd);
992 if (repo_path == NULL) {
993 error = got_error_from_errno();
994 goto done;
998 error = got_repo_open(&repo, repo_path);
999 if (error != NULL)
1000 goto done;
1002 if (start_commit == NULL) {
1003 error = get_head_commit_id(&start_id, repo);
1004 if (error != NULL)
1005 goto done;
1006 } else {
1007 struct got_object *obj;
1008 error = got_object_open_by_id_str(&obj, repo, start_commit);
1009 if (error == NULL) {
1010 start_id = got_object_get_id(obj);
1011 if (start_id == NULL)
1012 error = got_error_from_errno();
1013 goto done;
1016 if (error != NULL)
1017 goto done;
1019 view = open_view(0, 0, 0, 0, NULL);
1020 if (view == NULL) {
1021 error = got_error_from_errno();
1022 goto done;
1024 error = show_log_view(view, start_id, repo, path);
1025 close_view(view);
1026 done:
1027 free(repo_path);
1028 free(cwd);
1029 free(path);
1030 free(start_id);
1031 if (repo)
1032 got_repo_close(repo);
1033 return error;
1036 __dead static void
1037 usage_diff(void)
1039 endwin();
1040 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1041 getprogname());
1042 exit(1);
1045 static char *
1046 parse_next_line(FILE *f, size_t *len)
1048 char *line;
1049 size_t linelen;
1050 size_t lineno;
1051 const char delim[3] = { '\0', '\0', '\0'};
1053 line = fparseln(f, &linelen, &lineno, delim, 0);
1054 if (len)
1055 *len = linelen;
1056 return line;
1059 static const struct got_error *
1060 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1061 int *last_displayed_line, int *eof, int max_lines)
1063 const struct got_error *err;
1064 int nlines = 0, nprinted = 0;
1065 char *line;
1066 size_t len;
1067 wchar_t *wline;
1068 int width;
1070 rewind(f);
1071 werase(view->window);
1073 *eof = 0;
1074 while (nprinted < max_lines) {
1075 line = parse_next_line(f, &len);
1076 if (line == NULL) {
1077 *eof = 1;
1078 break;
1080 if (++nlines < *first_displayed_line) {
1081 free(line);
1082 continue;
1085 err = format_line(&wline, &width, line, view->ncols);
1086 if (err) {
1087 free(line);
1088 free(wline);
1089 return err;
1091 waddwstr(view->window, wline);
1092 if (width < view->ncols)
1093 waddch(view->window, '\n');
1094 if (++nprinted == 1)
1095 *first_displayed_line = nlines;
1096 free(line);
1097 free(wline);
1098 wline = NULL;
1100 *last_displayed_line = nlines;
1102 update_panels();
1103 doupdate();
1105 return NULL;
1108 static const struct got_error *
1109 show_diff_view(struct tog_view *view, struct got_object *obj1,
1110 struct got_object *obj2, struct got_repository *repo)
1112 const struct got_error *err;
1113 FILE *f;
1114 int ch, done = 0;
1115 int first_displayed_line = 1, last_displayed_line = view->nlines;
1116 int eof, i;
1118 if (obj1 != NULL && obj2 != NULL &&
1119 got_object_get_type(obj1) != got_object_get_type(obj2))
1120 return got_error(GOT_ERR_OBJ_TYPE);
1122 f = got_opentemp();
1123 if (f == NULL)
1124 return got_error_from_errno();
1126 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1127 case GOT_OBJ_TYPE_BLOB:
1128 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1129 break;
1130 case GOT_OBJ_TYPE_TREE:
1131 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1132 break;
1133 case GOT_OBJ_TYPE_COMMIT:
1134 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1135 break;
1136 default:
1137 return got_error(GOT_ERR_OBJ_TYPE);
1140 fflush(f);
1142 show_panel(view->panel);
1144 while (!done) {
1145 err = draw_file(view, f, &first_displayed_line,
1146 &last_displayed_line, &eof, view->nlines);
1147 if (err)
1148 break;
1149 nodelay(stdscr, FALSE);
1150 ch = wgetch(view->window);
1151 nodelay(stdscr, TRUE);
1152 switch (ch) {
1153 case 'q':
1154 done = 1;
1155 break;
1156 case 'k':
1157 case KEY_UP:
1158 if (first_displayed_line > 1)
1159 first_displayed_line--;
1160 break;
1161 case KEY_PPAGE:
1162 case KEY_BACKSPACE:
1163 i = 0;
1164 while (i++ < view->nlines - 1 &&
1165 first_displayed_line > 1)
1166 first_displayed_line--;
1167 break;
1168 case 'j':
1169 case KEY_DOWN:
1170 if (!eof)
1171 first_displayed_line++;
1172 break;
1173 case KEY_NPAGE:
1174 case ' ':
1175 i = 0;
1176 while (!eof && i++ < view->nlines - 1) {
1177 char *line = parse_next_line(f, NULL);
1178 first_displayed_line++;
1179 if (line == NULL)
1180 break;
1182 break;
1183 case KEY_RESIZE:
1184 err = view_resize(view);
1185 if (err)
1186 goto done;
1187 break;
1188 default:
1189 break;
1192 done:
1193 fclose(f);
1194 return err;
1197 static const struct got_error *
1198 cmd_diff(int argc, char *argv[])
1200 const struct got_error *error = NULL;
1201 struct got_repository *repo = NULL;
1202 struct got_object *obj1 = NULL, *obj2 = NULL;
1203 char *repo_path = NULL;
1204 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1205 int ch;
1206 struct tog_view *view;
1208 #ifndef PROFILE
1209 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1210 err(1, "pledge");
1211 #endif
1213 while ((ch = getopt(argc, argv, "")) != -1) {
1214 switch (ch) {
1215 default:
1216 usage();
1217 /* NOTREACHED */
1221 argc -= optind;
1222 argv += optind;
1224 if (argc == 0) {
1225 usage_diff(); /* TODO show local worktree changes */
1226 } else if (argc == 2) {
1227 repo_path = getcwd(NULL, 0);
1228 if (repo_path == NULL)
1229 return got_error_from_errno();
1230 obj_id_str1 = argv[0];
1231 obj_id_str2 = argv[1];
1232 } else if (argc == 3) {
1233 repo_path = realpath(argv[0], NULL);
1234 if (repo_path == NULL)
1235 return got_error_from_errno();
1236 obj_id_str1 = argv[1];
1237 obj_id_str2 = argv[2];
1238 } else
1239 usage_diff();
1241 error = got_repo_open(&repo, repo_path);
1242 free(repo_path);
1243 if (error)
1244 goto done;
1246 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1247 if (error)
1248 goto done;
1250 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1251 if (error)
1252 goto done;
1254 view = open_view(0, 0, 0, 0, NULL);
1255 if (view == NULL) {
1256 error = got_error_from_errno();
1257 goto done;
1259 error = show_diff_view(view, obj1, obj2, repo);
1260 close_view(view);
1261 done:
1262 got_repo_close(repo);
1263 if (obj1)
1264 got_object_close(obj1);
1265 if (obj2)
1266 got_object_close(obj2);
1267 return error;
1270 __dead static void
1271 usage_blame(void)
1273 endwin();
1274 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1275 getprogname());
1276 exit(1);
1279 struct tog_blame_line {
1280 int annotated;
1281 struct got_object_id *id;
1284 static const struct got_error *
1285 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1286 const char *path, struct tog_blame_line *lines, int nlines,
1287 int blame_complete, int selected_line, int *first_displayed_line,
1288 int *last_displayed_line, int *eof, int max_lines)
1290 const struct got_error *err;
1291 int lineno = 0, nprinted = 0;
1292 char *line;
1293 size_t len;
1294 wchar_t *wline;
1295 int width, wlimit;
1296 struct tog_blame_line *blame_line;
1297 struct got_object_id *prev_id = NULL;
1298 char *id_str;
1300 err = got_object_id_str(&id_str, id);
1301 if (err)
1302 return err;
1304 rewind(f);
1305 werase(view->window);
1307 if (asprintf(&line, "commit: %s", id_str) == -1) {
1308 err = got_error_from_errno();
1309 free(id_str);
1310 return err;
1313 err = format_line(&wline, &width, line, view->ncols);
1314 free(line);
1315 line = NULL;
1316 waddwstr(view->window, wline);
1317 free(wline);
1318 wline = NULL;
1319 if (width < view->ncols)
1320 waddch(view->window, '\n');
1322 if (asprintf(&line, "[%d/%d] %s%s",
1323 *first_displayed_line - 1 + selected_line, nlines,
1324 blame_complete ? "" : "annotating ", path) == -1) {
1325 free(id_str);
1326 return got_error_from_errno();
1328 free(id_str);
1329 err = format_line(&wline, &width, line, view->ncols);
1330 free(line);
1331 line = NULL;
1332 if (err)
1333 return err;
1334 waddwstr(view->window, wline);
1335 free(wline);
1336 wline = NULL;
1337 if (width < view->ncols)
1338 waddch(view->window, '\n');
1340 *eof = 0;
1341 while (nprinted < max_lines - 2) {
1342 line = parse_next_line(f, &len);
1343 if (line == NULL) {
1344 *eof = 1;
1345 break;
1347 if (++lineno < *first_displayed_line) {
1348 free(line);
1349 continue;
1352 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1353 err = format_line(&wline, &width, line, wlimit);
1354 if (err) {
1355 free(line);
1356 return err;
1359 if (nprinted == selected_line - 1)
1360 wstandout(view->window);
1362 blame_line = &lines[lineno - 1];
1363 if (blame_line->annotated && prev_id &&
1364 got_object_id_cmp(prev_id, blame_line->id) == 0)
1365 waddstr(view->window, " ");
1366 else if (blame_line->annotated) {
1367 char *id_str;
1368 err = got_object_id_str(&id_str, blame_line->id);
1369 if (err) {
1370 free(line);
1371 free(wline);
1372 return err;
1374 wprintw(view->window, "%.8s ", id_str);
1375 free(id_str);
1376 prev_id = blame_line->id;
1377 } else {
1378 waddstr(view->window, "........ ");
1379 prev_id = NULL;
1382 waddwstr(view->window, wline);
1383 while (width < wlimit) {
1384 waddch(view->window, ' ');
1385 width++;
1387 if (nprinted == selected_line - 1)
1388 wstandend(view->window);
1389 if (++nprinted == 1)
1390 *first_displayed_line = lineno;
1391 free(line);
1392 free(wline);
1393 wline = NULL;
1395 *last_displayed_line = lineno;
1397 update_panels();
1398 doupdate();
1400 return NULL;
1403 struct tog_blame_cb_args {
1404 pthread_mutex_t *mutex;
1405 struct tog_blame_line *lines; /* one per line */
1406 int nlines;
1408 struct tog_view *view;
1409 struct got_object_id *commit_id;
1410 FILE *f;
1411 const char *path;
1412 int *first_displayed_line;
1413 int *last_displayed_line;
1414 int *selected_line;
1415 int *quit;
1418 static const struct got_error *
1419 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1421 const struct got_error *err = NULL;
1422 struct tog_blame_cb_args *a = arg;
1423 struct tog_blame_line *line;
1424 int eof;
1426 if (nlines != a->nlines ||
1427 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1428 return got_error(GOT_ERR_RANGE);
1430 if (pthread_mutex_lock(a->mutex) != 0)
1431 return got_error_from_errno();
1433 if (*a->quit) { /* user has quit the blame view */
1434 err = got_error(GOT_ERR_ITER_COMPLETED);
1435 goto done;
1438 if (lineno == -1)
1439 goto done; /* no change in this commit */
1441 line = &a->lines[lineno - 1];
1442 if (line->annotated)
1443 goto done;
1445 line->id = got_object_id_dup(id);
1446 if (line->id == NULL) {
1447 err = got_error_from_errno();
1448 goto done;
1450 line->annotated = 1;
1452 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1453 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1454 a->last_displayed_line, &eof, a->view->nlines);
1455 done:
1456 if (pthread_mutex_unlock(a->mutex) != 0)
1457 return got_error_from_errno();
1458 return err;
1461 struct tog_blame_thread_args {
1462 const char *path;
1463 struct got_repository *repo;
1464 struct tog_blame_cb_args *cb_args;
1465 int *complete;
1468 static void *
1469 blame_thread(void *arg)
1471 const struct got_error *err;
1472 struct tog_blame_thread_args *ta = arg;
1473 struct tog_blame_cb_args *a = ta->cb_args;
1474 int eof;
1476 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1477 blame_cb, ta->cb_args);
1479 if (pthread_mutex_lock(a->mutex) != 0)
1480 return (void *)got_error_from_errno();
1482 got_repo_close(ta->repo);
1483 ta->repo = NULL;
1484 *ta->complete = 1;
1485 if (!err)
1486 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1487 a->lines, a->nlines, 1, *a->selected_line,
1488 a->first_displayed_line, a->last_displayed_line, &eof,
1489 a->view->nlines);
1491 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1492 err = got_error_from_errno();
1494 return (void *)err;
1497 static struct got_object_id *
1498 get_selected_commit_id(struct tog_blame_line *lines,
1499 int first_displayed_line, int selected_line)
1501 struct tog_blame_line *line;
1503 line = &lines[first_displayed_line - 1 + selected_line - 1];
1504 if (!line->annotated)
1505 return NULL;
1507 return line->id;
1510 static const struct got_error *
1511 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1512 struct tog_blame_line *lines, int first_displayed_line,
1513 int selected_line, struct got_repository *repo)
1515 const struct got_error *err = NULL;
1516 struct got_commit_object *commit = NULL;
1517 struct got_object_id *selected_id;
1518 struct got_object_qid *pid;
1520 *pobj = NULL;
1521 *obj = NULL;
1523 selected_id = get_selected_commit_id(lines,
1524 first_displayed_line, selected_line);
1525 if (selected_id == NULL)
1526 return NULL;
1528 err = got_object_open(obj, repo, selected_id);
1529 if (err)
1530 goto done;
1532 err = got_object_commit_open(&commit, repo, *obj);
1533 if (err)
1534 goto done;
1536 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1537 if (pid) {
1538 err = got_object_open(pobj, repo, pid->id);
1539 if (err)
1540 goto done;
1542 done:
1543 if (commit)
1544 got_object_commit_close(commit);
1545 return err;
1548 struct tog_blame {
1549 FILE *f;
1550 size_t filesize;
1551 struct tog_blame_line *lines;
1552 size_t nlines;
1553 pthread_t thread;
1554 struct tog_blame_thread_args thread_args;
1555 struct tog_blame_cb_args cb_args;
1556 const char *path;
1559 static const struct got_error *
1560 stop_blame(struct tog_blame *blame)
1562 const struct got_error *err = NULL;
1563 int i;
1565 if (blame->thread) {
1566 if (pthread_join(blame->thread, (void **)&err) != 0)
1567 err = got_error_from_errno();
1568 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1569 err = NULL;
1570 blame->thread = NULL;
1572 if (blame->thread_args.repo) {
1573 got_repo_close(blame->thread_args.repo);
1574 blame->thread_args.repo = NULL;
1576 if (blame->f) {
1577 fclose(blame->f);
1578 blame->f = NULL;
1580 for (i = 0; i < blame->nlines; i++)
1581 free(blame->lines[i].id);
1582 free(blame->lines);
1583 blame->lines = NULL;
1584 free(blame->cb_args.commit_id);
1585 blame->cb_args.commit_id = NULL;
1587 return err;
1590 static const struct got_error *
1591 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1592 struct tog_view *view, int *blame_complete,
1593 int *first_displayed_line, int *last_displayed_line,
1594 int *selected_line, int *done, const char *path,
1595 struct got_object_id *commit_id,
1596 struct got_repository *repo)
1598 const struct got_error *err = NULL;
1599 struct got_blob_object *blob = NULL;
1600 struct got_repository *thread_repo = NULL;
1601 struct got_object *obj;
1603 err = got_object_open_by_path(&obj, repo, commit_id, path);
1604 if (err)
1605 goto done;
1606 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1607 err = got_error(GOT_ERR_OBJ_TYPE);
1608 goto done;
1611 err = got_object_blob_open(&blob, repo, obj, 8192);
1612 if (err)
1613 goto done;
1614 blame->f = got_opentemp();
1615 if (blame->f == NULL) {
1616 err = got_error_from_errno();
1617 goto done;
1619 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1620 blame->f, blob);
1621 if (err)
1622 goto done;
1624 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1625 if (blame->lines == NULL) {
1626 err = got_error_from_errno();
1627 goto done;
1630 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1631 if (err)
1632 goto done;
1634 blame->cb_args.view = view;
1635 blame->cb_args.lines = blame->lines;
1636 blame->cb_args.nlines = blame->nlines;
1637 blame->cb_args.mutex = mutex;
1638 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1639 if (blame->cb_args.commit_id == NULL) {
1640 err = got_error_from_errno();
1641 goto done;
1643 blame->cb_args.f = blame->f;
1644 blame->cb_args.path = path;
1645 blame->cb_args.first_displayed_line = first_displayed_line;
1646 blame->cb_args.selected_line = selected_line;
1647 blame->cb_args.last_displayed_line = last_displayed_line;
1648 blame->cb_args.quit = done;
1650 blame->thread_args.path = path;
1651 blame->thread_args.repo = thread_repo;
1652 blame->thread_args.cb_args = &blame->cb_args;
1653 blame->thread_args.complete = blame_complete;
1654 *blame_complete = 0;
1656 if (pthread_create(&blame->thread, NULL, blame_thread,
1657 &blame->thread_args) != 0) {
1658 err = got_error_from_errno();
1659 goto done;
1662 done:
1663 if (blob)
1664 got_object_blob_close(blob);
1665 if (obj)
1666 got_object_close(obj);
1667 if (err)
1668 stop_blame(blame);
1669 return err;
1672 static const struct got_error *
1673 show_blame_view(struct tog_view *view, const char *path,
1674 struct got_object_id *commit_id, struct got_repository *repo)
1676 const struct got_error *err = NULL, *thread_err = NULL;
1677 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1678 int selected_line = first_displayed_line;
1679 int eof, blame_complete = 0;
1680 struct got_object *obj = NULL, *pobj = NULL;
1681 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1682 struct tog_blame blame;
1683 struct got_object_id_queue blamed_commits;
1684 struct got_object_qid *blamed_commit = NULL;
1685 struct tog_view *diff_view;
1687 SIMPLEQ_INIT(&blamed_commits);
1689 if (pthread_mutex_init(&mutex, NULL) != 0) {
1690 err = got_error_from_errno();
1691 goto done;
1694 err = got_object_qid_alloc(&blamed_commit, commit_id);
1695 if (err)
1696 goto done;
1697 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1699 show_panel(view->panel);
1700 last_displayed_line = view->nlines;
1702 memset(&blame, 0, sizeof(blame));
1703 err = run_blame(&blame, &mutex, view, &blame_complete,
1704 &first_displayed_line, &last_displayed_line,
1705 &selected_line, &done, path, blamed_commit->id, repo);
1706 if (err)
1707 return err;
1709 while (!done) {
1710 if (pthread_mutex_lock(&mutex) != 0) {
1711 err = got_error_from_errno();
1712 goto done;
1714 err = draw_blame(view, blamed_commit->id, blame.f, path,
1715 blame.lines, blame.nlines, blame_complete, selected_line,
1716 &first_displayed_line, &last_displayed_line, &eof,
1717 view->nlines);
1718 if (pthread_mutex_unlock(&mutex) != 0) {
1719 err = got_error_from_errno();
1720 goto done;
1722 if (err)
1723 break;
1724 nodelay(stdscr, FALSE);
1725 ch = wgetch(view->window);
1726 nodelay(stdscr, TRUE);
1727 if (pthread_mutex_lock(&mutex) != 0) {
1728 err = got_error_from_errno();
1729 goto done;
1731 switch (ch) {
1732 case 'q':
1733 done = 1;
1734 break;
1735 case 'k':
1736 case KEY_UP:
1737 if (selected_line > 1)
1738 selected_line--;
1739 else if (selected_line == 1 &&
1740 first_displayed_line > 1)
1741 first_displayed_line--;
1742 break;
1743 case KEY_PPAGE:
1744 case KEY_BACKSPACE:
1745 if (first_displayed_line == 1) {
1746 selected_line = 1;
1747 break;
1749 if (first_displayed_line > view->nlines - 2)
1750 first_displayed_line -=
1751 (view->nlines - 2);
1752 else
1753 first_displayed_line = 1;
1754 break;
1755 case 'j':
1756 case KEY_DOWN:
1757 if (selected_line < view->nlines - 2 &&
1758 first_displayed_line + selected_line <=
1759 blame.nlines)
1760 selected_line++;
1761 else if (last_displayed_line < blame.nlines)
1762 first_displayed_line++;
1763 break;
1764 case 'b':
1765 case 'p': {
1766 struct got_object_id *id;
1767 id = get_selected_commit_id(blame.lines,
1768 first_displayed_line, selected_line);
1769 if (id == NULL || got_object_id_cmp(id,
1770 blamed_commit->id) == 0)
1771 break;
1772 err = open_selected_commit(&pobj, &obj,
1773 blame.lines, first_displayed_line,
1774 selected_line, repo);
1775 if (err)
1776 break;
1777 if (pobj == NULL && obj == NULL)
1778 break;
1779 if (ch == 'p' && pobj == NULL)
1780 break;
1781 done = 1;
1782 if (pthread_mutex_unlock(&mutex) != 0) {
1783 err = got_error_from_errno();
1784 goto done;
1786 thread_err = stop_blame(&blame);
1787 done = 0;
1788 if (pthread_mutex_lock(&mutex) != 0) {
1789 err = got_error_from_errno();
1790 goto done;
1792 if (thread_err)
1793 break;
1794 id = got_object_get_id(ch == 'b' ? obj : pobj);
1795 got_object_close(obj);
1796 obj = NULL;
1797 if (pobj) {
1798 got_object_close(pobj);
1799 pobj = NULL;
1801 if (id == NULL) {
1802 err = got_error_from_errno();
1803 break;
1805 err = got_object_qid_alloc(&blamed_commit, id);
1806 free(id);
1807 if (err)
1808 goto done;
1809 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1810 blamed_commit, entry);
1811 err = run_blame(&blame, &mutex, view,
1812 &blame_complete, &first_displayed_line,
1813 &last_displayed_line, &selected_line,
1814 &done, path, blamed_commit->id, repo);
1815 if (err)
1816 break;
1817 break;
1819 case 'B': {
1820 struct got_object_qid *first;
1821 first = SIMPLEQ_FIRST(&blamed_commits);
1822 if (!got_object_id_cmp(first->id, commit_id))
1823 break;
1824 done = 1;
1825 if (pthread_mutex_unlock(&mutex) != 0) {
1826 err = got_error_from_errno();
1827 goto done;
1829 thread_err = stop_blame(&blame);
1830 done = 0;
1831 if (pthread_mutex_lock(&mutex) != 0) {
1832 err = got_error_from_errno();
1833 goto done;
1835 if (thread_err)
1836 break;
1837 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1838 got_object_qid_free(blamed_commit);
1839 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1840 err = run_blame(&blame, &mutex, view,
1841 &blame_complete, &first_displayed_line,
1842 &last_displayed_line, &selected_line,
1843 &done, path, blamed_commit->id, repo);
1844 if (err)
1845 break;
1846 break;
1848 case KEY_ENTER:
1849 case '\r':
1850 err = open_selected_commit(&pobj, &obj,
1851 blame.lines, first_displayed_line,
1852 selected_line, repo);
1853 if (err)
1854 break;
1855 if (pobj == NULL && obj == NULL)
1856 break;
1857 diff_view = open_view(0, 0, 0, 0, view);
1858 if (diff_view == NULL) {
1859 err = got_error_from_errno();
1860 break;
1862 err = show_diff_view(diff_view, pobj, obj, repo);
1863 close_view(diff_view);
1864 if (pobj) {
1865 got_object_close(pobj);
1866 pobj = NULL;
1868 got_object_close(obj);
1869 obj = NULL;
1870 show_panel(view->panel);
1871 if (err)
1872 break;
1873 break;
1874 case KEY_NPAGE:
1875 case ' ':
1876 if (last_displayed_line >= blame.nlines &&
1877 selected_line < view->nlines - 2) {
1878 selected_line = MIN(blame.nlines,
1879 view->nlines - 2);
1880 break;
1882 if (last_displayed_line + view->nlines - 2 <=
1883 blame.nlines)
1884 first_displayed_line +=
1885 view->nlines - 2;
1886 else
1887 first_displayed_line =
1888 blame.nlines - (view->nlines - 3);
1889 break;
1890 case KEY_RESIZE:
1891 err = view_resize(view);
1892 if (err)
1893 break;
1894 if (selected_line > view->nlines - 2) {
1895 selected_line = MIN(blame.nlines,
1896 view->nlines - 2);
1898 break;
1899 default:
1900 break;
1902 if (pthread_mutex_unlock(&mutex) != 0)
1903 err = got_error_from_errno();
1904 if (err || thread_err)
1905 break;
1907 done:
1908 if (pobj)
1909 got_object_close(pobj);
1910 if (blame.thread)
1911 thread_err = stop_blame(&blame);
1912 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1913 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1914 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1915 got_object_qid_free(blamed_commit);
1917 return thread_err ? thread_err : err;
1920 static const struct got_error *
1921 cmd_blame(int argc, char *argv[])
1923 const struct got_error *error;
1924 struct got_repository *repo = NULL;
1925 char *repo_path = NULL;
1926 char *path = NULL;
1927 struct got_object_id *commit_id = NULL;
1928 char *commit_id_str = NULL;
1929 int ch;
1930 struct tog_view *view;
1932 #ifndef PROFILE
1933 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1934 err(1, "pledge");
1935 #endif
1937 while ((ch = getopt(argc, argv, "c:")) != -1) {
1938 switch (ch) {
1939 case 'c':
1940 commit_id_str = optarg;
1941 break;
1942 default:
1943 usage();
1944 /* NOTREACHED */
1948 argc -= optind;
1949 argv += optind;
1951 if (argc == 0) {
1952 usage_blame();
1953 } else if (argc == 1) {
1954 repo_path = getcwd(NULL, 0);
1955 if (repo_path == NULL)
1956 return got_error_from_errno();
1957 path = argv[0];
1958 } else if (argc == 2) {
1959 repo_path = realpath(argv[0], NULL);
1960 if (repo_path == NULL)
1961 return got_error_from_errno();
1962 path = argv[1];
1963 } else
1964 usage_blame();
1966 error = got_repo_open(&repo, repo_path);
1967 free(repo_path);
1968 if (error != NULL)
1969 return error;
1971 if (commit_id_str == NULL) {
1972 struct got_reference *head_ref;
1973 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1974 if (error != NULL)
1975 goto done;
1976 error = got_ref_resolve(&commit_id, repo, head_ref);
1977 got_ref_close(head_ref);
1978 } else {
1979 struct got_object *obj;
1980 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1981 if (error != NULL)
1982 goto done;
1983 commit_id = got_object_get_id(obj);
1984 if (commit_id == NULL)
1985 error = got_error_from_errno();
1986 got_object_close(obj);
1988 if (error != NULL)
1989 goto done;
1991 view = open_view(0, 0, 0, 0, NULL);
1992 if (view == NULL) {
1993 error = got_error_from_errno();
1994 goto done;
1996 error = show_blame_view(view, path, commit_id, repo);
1997 close_view(view);
1998 done:
1999 free(commit_id);
2000 if (repo)
2001 got_repo_close(repo);
2002 return error;
2005 static const struct got_error *
2006 draw_tree_entries(struct tog_view *view,
2007 struct got_tree_entry **first_displayed_entry,
2008 struct got_tree_entry **last_displayed_entry,
2009 struct got_tree_entry **selected_entry, int *ndisplayed,
2010 const char *label, int show_ids, const char *parent_path,
2011 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2013 const struct got_error *err = NULL;
2014 struct got_tree_entry *te;
2015 wchar_t *wline;
2016 int width, n;
2018 *ndisplayed = 0;
2020 werase(view->window);
2022 if (limit == 0)
2023 return NULL;
2025 err = format_line(&wline, &width, label, view->ncols);
2026 if (err)
2027 return err;
2028 waddwstr(view->window, wline);
2029 free(wline);
2030 wline = NULL;
2031 if (width < view->ncols)
2032 waddch(view->window, '\n');
2033 if (--limit <= 0)
2034 return NULL;
2035 err = format_line(&wline, &width, parent_path, view->ncols);
2036 if (err)
2037 return err;
2038 waddwstr(view->window, wline);
2039 free(wline);
2040 wline = NULL;
2041 if (width < view->ncols)
2042 waddch(view->window, '\n');
2043 if (--limit <= 0)
2044 return NULL;
2045 waddch(view->window, '\n');
2046 if (--limit <= 0)
2047 return NULL;
2049 te = SIMPLEQ_FIRST(&entries->head);
2050 if (*first_displayed_entry == NULL) {
2051 if (selected == 0) {
2052 wstandout(view->window);
2053 *selected_entry = NULL;
2055 waddstr(view->window, " ..\n"); /* parent directory */
2056 if (selected == 0)
2057 wstandend(view->window);
2058 (*ndisplayed)++;
2059 if (--limit <= 0)
2060 return NULL;
2061 n = 1;
2062 } else {
2063 n = 0;
2064 while (te != *first_displayed_entry)
2065 te = SIMPLEQ_NEXT(te, entry);
2068 while (te) {
2069 char *line = NULL, *id_str = NULL;
2071 if (show_ids) {
2072 err = got_object_id_str(&id_str, te->id);
2073 if (err)
2074 return got_error_from_errno();
2076 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2077 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2078 free(id_str);
2079 return got_error_from_errno();
2081 free(id_str);
2082 err = format_line(&wline, &width, line, view->ncols);
2083 if (err) {
2084 free(line);
2085 break;
2087 if (n == selected) {
2088 wstandout(view->window);
2089 *selected_entry = te;
2091 waddwstr(view->window, wline);
2092 if (width < view->ncols)
2093 waddch(view->window, '\n');
2094 if (n == selected)
2095 wstandend(view->window);
2096 free(line);
2097 free(wline);
2098 wline = NULL;
2099 n++;
2100 (*ndisplayed)++;
2101 *last_displayed_entry = te;
2102 if (--limit <= 0)
2103 break;
2104 te = SIMPLEQ_NEXT(te, entry);
2107 return err;
2110 static void
2111 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2112 const struct got_tree_entries *entries, int isroot)
2114 struct got_tree_entry *te, *prev;
2115 int i;
2117 if (*first_displayed_entry == NULL)
2118 return;
2120 te = SIMPLEQ_FIRST(&entries->head);
2121 if (*first_displayed_entry == te) {
2122 if (!isroot)
2123 *first_displayed_entry = NULL;
2124 return;
2127 /* XXX this is stupid... switch to TAILQ? */
2128 for (i = 0; i < maxscroll; i++) {
2129 while (te != *first_displayed_entry) {
2130 prev = te;
2131 te = SIMPLEQ_NEXT(te, entry);
2133 *first_displayed_entry = prev;
2134 te = SIMPLEQ_FIRST(&entries->head);
2136 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2137 *first_displayed_entry = NULL;
2140 static void
2141 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2142 struct got_tree_entry *last_displayed_entry,
2143 const struct got_tree_entries *entries)
2145 struct got_tree_entry *next;
2146 int n = 0;
2148 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2149 return;
2151 if (*first_displayed_entry)
2152 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2153 else
2154 next = SIMPLEQ_FIRST(&entries->head);
2155 while (next) {
2156 *first_displayed_entry = next;
2157 if (++n >= maxscroll)
2158 break;
2159 next = SIMPLEQ_NEXT(next, entry);
2163 struct tog_parent_tree {
2164 TAILQ_ENTRY(tog_parent_tree) entry;
2165 struct got_tree_object *tree;
2166 struct got_tree_entry *first_displayed_entry;
2167 struct got_tree_entry *selected_entry;
2168 int selected;
2171 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2173 static const struct got_error *
2174 tree_entry_path(char **path, struct tog_parent_trees *parents,
2175 struct got_tree_entry *te)
2177 const struct got_error *err = NULL;
2178 struct tog_parent_tree *pt;
2179 size_t len = 2; /* for leading slash and NUL */
2181 TAILQ_FOREACH(pt, parents, entry)
2182 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2183 if (te)
2184 len += strlen(te->name);
2186 *path = calloc(1, len);
2187 if (path == NULL)
2188 return got_error_from_errno();
2190 (*path)[0] = '/';
2191 pt = TAILQ_LAST(parents, tog_parent_trees);
2192 while (pt) {
2193 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2194 err = got_error(GOT_ERR_NO_SPACE);
2195 goto done;
2197 if (strlcat(*path, "/", len) >= len) {
2198 err = got_error(GOT_ERR_NO_SPACE);
2199 goto done;
2201 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2203 if (te) {
2204 if (strlcat(*path, te->name, len) >= len) {
2205 err = got_error(GOT_ERR_NO_SPACE);
2206 goto done;
2209 done:
2210 if (err) {
2211 free(*path);
2212 *path = NULL;
2214 return err;
2217 static const struct got_error *
2218 blame_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2219 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2220 struct got_repository *repo)
2222 const struct got_error *err = NULL;
2223 char *path;
2225 err = tree_entry_path(&path, parents, te);
2226 if (err)
2227 return err;
2229 err = show_blame_view(view, path, commit_id, repo);
2230 free(path);
2231 return err;
2234 static const struct got_error *
2235 log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2236 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2237 struct got_repository *repo)
2239 const struct got_error *err = NULL;
2240 char *path;
2242 err = tree_entry_path(&path, parents, te);
2243 if (err)
2244 return err;
2246 err = show_log_view(view, commit_id, repo, path);
2247 free(path);
2248 return err;
2251 static const struct got_error *
2252 show_tree_view(struct tog_view *view, struct got_tree_object *root,
2253 struct got_object_id *commit_id, struct got_repository *repo)
2255 const struct got_error *err = NULL;
2256 int ch, done = 0, selected = 0, show_ids = 0;
2257 struct got_tree_object *tree = root;
2258 const struct got_tree_entries *entries;
2259 struct got_tree_entry *first_displayed_entry = NULL;
2260 struct got_tree_entry *last_displayed_entry = NULL;
2261 struct got_tree_entry *selected_entry = NULL;
2262 char *commit_id_str = NULL, *tree_label = NULL;
2263 int nentries, ndisplayed;
2264 struct tog_parent_trees parents;
2266 TAILQ_INIT(&parents);
2268 err = got_object_id_str(&commit_id_str, commit_id);
2269 if (err != NULL)
2270 goto done;
2272 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2273 err = got_error_from_errno();
2274 goto done;
2277 show_panel(view->panel);
2279 entries = got_object_tree_get_entries(root);
2280 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2281 while (!done) {
2282 char *parent_path;
2283 entries = got_object_tree_get_entries(tree);
2284 nentries = entries->nentries;
2285 if (tree != root)
2286 nentries++; /* '..' directory */
2288 err = tree_entry_path(&parent_path, &parents, NULL);
2289 if (err)
2290 goto done;
2292 err = draw_tree_entries(view, &first_displayed_entry,
2293 &last_displayed_entry, &selected_entry, &ndisplayed,
2294 tree_label, show_ids, parent_path, entries, selected,
2295 view->nlines, tree == root);
2296 free(parent_path);
2297 if (err)
2298 break;
2300 nodelay(stdscr, FALSE);
2301 ch = wgetch(view->window);
2302 nodelay(stdscr, TRUE);
2303 switch (ch) {
2304 case 'q':
2305 done = 1;
2306 break;
2307 case 'i':
2308 show_ids = !show_ids;
2309 break;
2310 case 'l':
2311 if (selected_entry) {
2312 struct tog_view *log_view;
2313 log_view = open_view(0, 0, 0, 0, view);
2314 if (log_view == NULL) {
2315 err = got_error_from_errno();
2316 goto done;
2318 err = log_tree_entry(log_view,
2319 selected_entry, &parents,
2320 commit_id, repo);
2321 close_view(log_view);
2322 if (err)
2323 goto done;
2325 break;
2326 case 'k':
2327 case KEY_UP:
2328 if (selected > 0)
2329 selected--;
2330 if (selected > 0)
2331 break;
2332 tree_scroll_up(&first_displayed_entry, 1,
2333 entries, tree == root);
2334 break;
2335 case KEY_PPAGE:
2336 if (SIMPLEQ_FIRST(&entries->head) ==
2337 first_displayed_entry) {
2338 if (tree != root)
2339 first_displayed_entry = NULL;
2340 selected = 0;
2341 break;
2343 tree_scroll_up(&first_displayed_entry,
2344 view->nlines, entries, tree == root);
2345 break;
2346 case 'j':
2347 case KEY_DOWN:
2348 if (selected < ndisplayed - 1) {
2349 selected++;
2350 break;
2352 tree_scroll_down(&first_displayed_entry, 1,
2353 last_displayed_entry, entries);
2354 break;
2355 case KEY_NPAGE:
2356 tree_scroll_down(&first_displayed_entry,
2357 view->nlines, last_displayed_entry,
2358 entries);
2359 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2360 break;
2361 /* can't scroll any further; move cursor down */
2362 if (selected < ndisplayed - 1)
2363 selected = ndisplayed - 1;
2364 break;
2365 case KEY_ENTER:
2366 case '\r':
2367 if (selected_entry == NULL) {
2368 struct tog_parent_tree *parent;
2369 case KEY_BACKSPACE:
2370 /* user selected '..' */
2371 if (tree == root)
2372 break;
2373 parent = TAILQ_FIRST(&parents);
2374 TAILQ_REMOVE(&parents, parent, entry);
2375 got_object_tree_close(tree);
2376 tree = parent->tree;
2377 first_displayed_entry =
2378 parent->first_displayed_entry;
2379 selected_entry = parent->selected_entry;
2380 selected = parent->selected;
2381 free(parent);
2382 } else if (S_ISDIR(selected_entry->mode)) {
2383 struct tog_parent_tree *parent;
2384 struct got_tree_object *child;
2385 err = got_object_open_as_tree(
2386 &child, repo, selected_entry->id);
2387 if (err)
2388 goto done;
2389 parent = calloc(1, sizeof(*parent));
2390 if (parent == NULL) {
2391 err = got_error_from_errno();
2392 goto done;
2394 parent->tree = tree;
2395 parent->first_displayed_entry =
2396 first_displayed_entry;
2397 parent->selected_entry = selected_entry;
2398 parent->selected = selected;
2399 TAILQ_INSERT_HEAD(&parents, parent,
2400 entry);
2401 tree = child;
2402 selected = 0;
2403 first_displayed_entry = NULL;
2404 } else if (S_ISREG(selected_entry->mode)) {
2405 struct tog_view *blame_view =
2406 open_view(0, 0, 0, 0, view);
2407 if (blame_view == NULL) {
2408 err = got_error_from_errno();
2409 goto done;
2411 err = blame_tree_entry(blame_view,
2412 selected_entry, &parents,
2413 commit_id, repo);
2414 close_view(blame_view);
2415 if (err)
2416 goto done;
2418 break;
2419 case KEY_RESIZE:
2420 err = view_resize(view);
2421 if (err)
2422 goto done;
2423 if (selected > view->nlines)
2424 selected = ndisplayed - 1;
2425 break;
2426 default:
2427 break;
2430 done:
2431 free(tree_label);
2432 free(commit_id_str);
2433 while (!TAILQ_EMPTY(&parents)) {
2434 struct tog_parent_tree *parent;
2435 parent = TAILQ_FIRST(&parents);
2436 TAILQ_REMOVE(&parents, parent, entry);
2437 free(parent);
2440 if (tree != root)
2441 got_object_tree_close(tree);
2442 return err;
2445 __dead static void
2446 usage_tree(void)
2448 endwin();
2449 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2450 getprogname());
2451 exit(1);
2454 static const struct got_error *
2455 cmd_tree(int argc, char *argv[])
2457 const struct got_error *error;
2458 struct got_repository *repo = NULL;
2459 char *repo_path = NULL;
2460 struct got_object_id *commit_id = NULL;
2461 char *commit_id_arg = NULL;
2462 struct got_commit_object *commit = NULL;
2463 struct got_tree_object *tree = NULL;
2464 int ch;
2465 struct tog_view *view;
2467 #ifndef PROFILE
2468 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2469 err(1, "pledge");
2470 #endif
2472 while ((ch = getopt(argc, argv, "c:")) != -1) {
2473 switch (ch) {
2474 case 'c':
2475 commit_id_arg = optarg;
2476 break;
2477 default:
2478 usage();
2479 /* NOTREACHED */
2483 argc -= optind;
2484 argv += optind;
2486 if (argc == 0) {
2487 repo_path = getcwd(NULL, 0);
2488 if (repo_path == NULL)
2489 return got_error_from_errno();
2490 } else if (argc == 1) {
2491 repo_path = realpath(argv[0], NULL);
2492 if (repo_path == NULL)
2493 return got_error_from_errno();
2494 } else
2495 usage_log();
2497 error = got_repo_open(&repo, repo_path);
2498 free(repo_path);
2499 if (error != NULL)
2500 return error;
2502 if (commit_id_arg == NULL) {
2503 error = get_head_commit_id(&commit_id, repo);
2504 if (error != NULL)
2505 goto done;
2506 } else {
2507 struct got_object *obj;
2508 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2509 if (error == NULL) {
2510 commit_id = got_object_get_id(obj);
2511 if (commit_id == NULL)
2512 error = got_error_from_errno();
2515 if (error != NULL)
2516 goto done;
2518 error = got_object_open_as_commit(&commit, repo, commit_id);
2519 if (error != NULL)
2520 goto done;
2522 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2523 if (error != NULL)
2524 goto done;
2526 view = open_view(0, 0, 0, 0, NULL);
2527 if (view == NULL) {
2528 error = got_error_from_errno();
2529 goto done;
2531 error = show_tree_view(view, tree, commit_id, repo);
2532 close_view(view);
2533 done:
2534 free(commit_id);
2535 if (commit)
2536 got_object_commit_close(commit);
2537 if (tree)
2538 got_object_tree_close(tree);
2539 if (repo)
2540 got_repo_close(repo);
2541 return error;
2543 static void
2544 init_curses(void)
2546 initscr();
2547 cbreak();
2548 noecho();
2549 nonl();
2550 intrflush(stdscr, FALSE);
2551 keypad(stdscr, TRUE);
2552 curs_set(0);
2555 __dead static void
2556 usage(void)
2558 int i;
2560 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2561 "Available commands:\n", getprogname());
2562 for (i = 0; i < nitems(tog_commands); i++) {
2563 struct tog_cmd *cmd = &tog_commands[i];
2564 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2566 exit(1);
2569 static char **
2570 make_argv(const char *arg0, const char *arg1)
2572 char **argv;
2573 int argc = (arg1 == NULL ? 1 : 2);
2575 argv = calloc(argc, sizeof(char *));
2576 if (argv == NULL)
2577 err(1, "calloc");
2578 argv[0] = strdup(arg0);
2579 if (argv[0] == NULL)
2580 err(1, "calloc");
2581 if (arg1) {
2582 argv[1] = strdup(arg1);
2583 if (argv[1] == NULL)
2584 err(1, "calloc");
2587 return argv;
2590 int
2591 main(int argc, char *argv[])
2593 const struct got_error *error = NULL;
2594 struct tog_cmd *cmd = NULL;
2595 int ch, hflag = 0;
2596 char **cmd_argv = NULL;
2598 setlocale(LC_ALL, "");
2600 while ((ch = getopt(argc, argv, "h")) != -1) {
2601 switch (ch) {
2602 case 'h':
2603 hflag = 1;
2604 break;
2605 default:
2606 usage();
2607 /* NOTREACHED */
2611 argc -= optind;
2612 argv += optind;
2613 optind = 0;
2614 optreset = 1;
2616 if (argc == 0) {
2617 if (hflag)
2618 usage();
2619 /* Build an argument vector which runs a default command. */
2620 cmd = &tog_commands[0];
2621 cmd_argv = make_argv(cmd->name, NULL);
2622 argc = 1;
2623 } else {
2624 int i;
2626 /* Did the user specific a command? */
2627 for (i = 0; i < nitems(tog_commands); i++) {
2628 if (strncmp(tog_commands[i].name, argv[0],
2629 strlen(argv[0])) == 0) {
2630 cmd = &tog_commands[i];
2631 if (hflag)
2632 tog_commands[i].cmd_usage();
2633 break;
2636 if (cmd == NULL) {
2637 /* Did the user specify a repository? */
2638 char *repo_path = realpath(argv[0], NULL);
2639 if (repo_path) {
2640 struct got_repository *repo;
2641 error = got_repo_open(&repo, repo_path);
2642 if (error == NULL)
2643 got_repo_close(repo);
2644 } else
2645 error = got_error_from_errno();
2646 if (error) {
2647 if (hflag) {
2648 fprintf(stderr, "%s: '%s' is not a "
2649 "known command\n", getprogname(),
2650 argv[0]);
2651 usage();
2653 fprintf(stderr, "%s: '%s' is neither a known "
2654 "command nor a path to a repository\n",
2655 getprogname(), argv[0]);
2656 free(repo_path);
2657 return 1;
2659 cmd = &tog_commands[0];
2660 cmd_argv = make_argv(cmd->name, repo_path);
2661 argc = 2;
2662 free(repo_path);
2666 init_curses();
2668 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2669 if (error)
2670 goto done;
2671 done:
2672 endwin();
2673 free(cmd_argv);
2674 if (error)
2675 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2676 return 0;