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 void
148 show_view(struct tog_view *view)
150 show_panel(view->panel);
151 update_panels();
154 const struct got_error *
155 view_resize(struct tog_view *view)
157 int nlines, ncols;
159 while (view) {
160 if (view->lines > LINES)
161 nlines = view->nlines - (view->lines - LINES);
162 else
163 nlines = view->nlines + (LINES - view->lines);
165 if (view->cols > COLS)
166 ncols = view->ncols - (view->cols - COLS);
167 else
168 ncols = view->ncols + (COLS - view->cols);
170 if (wresize(view->window, nlines, ncols) == ERR)
171 return got_error_from_errno();
172 replace_panel(view->panel, view->window);
174 view->nlines = nlines;
175 view->ncols = ncols;
176 view->lines = LINES;
177 view->cols = COLS;
179 view = view->parent;
182 return NULL;
185 __dead static void
186 usage_log(void)
188 endwin();
189 fprintf(stderr,
190 "usage: %s log [-c commit] [-r repository-path] [path]\n",
191 getprogname());
192 exit(1);
195 /* Create newly allocated wide-character string equivalent to a byte string. */
196 static const struct got_error *
197 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
199 char *vis = NULL;
200 const struct got_error *err = NULL;
202 *ws = NULL;
203 *wlen = mbstowcs(NULL, s, 0);
204 if (*wlen == (size_t)-1) {
205 int vislen;
206 if (errno != EILSEQ)
207 return got_error_from_errno();
209 /* byte string invalid in current encoding; try to "fix" it */
210 err = got_mbsavis(&vis, &vislen, s);
211 if (err)
212 return err;
213 *wlen = mbstowcs(NULL, vis, 0);
214 if (*wlen == (size_t)-1) {
215 err = got_error_from_errno(); /* give up */
216 goto done;
220 *ws = calloc(*wlen + 1, sizeof(*ws));
221 if (*ws == NULL) {
222 err = got_error_from_errno();
223 goto done;
226 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
227 err = got_error_from_errno();
228 done:
229 free(vis);
230 if (err) {
231 free(*ws);
232 *ws = NULL;
233 *wlen = 0;
235 return err;
238 /* Format a line for display, ensuring that it won't overflow a width limit. */
239 static const struct got_error *
240 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
242 const struct got_error *err = NULL;
243 int cols = 0;
244 wchar_t *wline = NULL;
245 size_t wlen;
246 int i;
248 *wlinep = NULL;
249 *widthp = 0;
251 err = mbs2ws(&wline, &wlen, line);
252 if (err)
253 return err;
255 i = 0;
256 while (i < wlen && cols < wlimit) {
257 int width = wcwidth(wline[i]);
258 switch (width) {
259 case 0:
260 i++;
261 break;
262 case 1:
263 case 2:
264 if (cols + width <= wlimit) {
265 cols += width;
266 i++;
268 break;
269 case -1:
270 if (wline[i] == L'\t')
271 cols += TABSIZE - ((cols + 1) % TABSIZE);
272 i++;
273 break;
274 default:
275 err = got_error_from_errno();
276 goto done;
279 wline[i] = L'\0';
280 if (widthp)
281 *widthp = cols;
282 done:
283 if (err)
284 free(wline);
285 else
286 *wlinep = wline;
287 return err;
290 static const struct got_error *
291 draw_commit(struct tog_view *view, struct got_commit_object *commit,
292 struct got_object_id *id)
294 const struct got_error *err = NULL;
295 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
296 char *logmsg0 = NULL, *logmsg = NULL;
297 char *author0 = NULL, *author = NULL;
298 wchar_t *wlogmsg = NULL, *wauthor = NULL;
299 int author_width, logmsg_width;
300 char *newline, *smallerthan;
301 char *line = NULL;
302 int col, limit;
303 static const size_t date_display_cols = 9;
304 static const size_t author_display_cols = 16;
305 const int avail = view->ncols;
307 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
308 &commit->tm_committer) >= sizeof(datebuf))
309 return got_error(GOT_ERR_NO_SPACE);
311 if (avail < date_display_cols)
312 limit = MIN(sizeof(datebuf) - 1, avail);
313 else
314 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
315 waddnstr(view->window, datebuf, limit);
316 col = limit + 1;
317 if (col > avail)
318 goto done;
320 author0 = strdup(commit->author);
321 if (author0 == NULL) {
322 err = got_error_from_errno();
323 goto done;
325 author = author0;
326 smallerthan = strchr(author, '<');
327 if (smallerthan)
328 *smallerthan = '\0';
329 else {
330 char *at = strchr(author, '@');
331 if (at)
332 *at = '\0';
334 limit = avail - col;
335 err = format_line(&wauthor, &author_width, author, limit);
336 if (err)
337 goto done;
338 waddwstr(view->window, wauthor);
339 col += author_width;
340 while (col <= avail && author_width < author_display_cols + 1) {
341 waddch(view->window, ' ');
342 col++;
343 author_width++;
345 if (col > avail)
346 goto done;
348 logmsg0 = strdup(commit->logmsg);
349 if (logmsg0 == NULL) {
350 err = got_error_from_errno();
351 goto done;
353 logmsg = logmsg0;
354 while (*logmsg == '\n')
355 logmsg++;
356 newline = strchr(logmsg, '\n');
357 if (newline)
358 *newline = '\0';
359 limit = avail - col;
360 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
361 if (err)
362 goto done;
363 waddwstr(view->window, wlogmsg);
364 col += logmsg_width;
365 while (col <= avail) {
366 waddch(view->window, ' ');
367 col++;
369 done:
370 free(logmsg0);
371 free(wlogmsg);
372 free(author0);
373 free(wauthor);
374 free(line);
375 return err;
378 struct commit_queue_entry {
379 TAILQ_ENTRY(commit_queue_entry) entry;
380 struct got_object_id *id;
381 struct got_commit_object *commit;
382 };
383 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
384 struct commit_queue {
385 int ncommits;
386 struct commit_queue_head head;
387 };
389 static struct commit_queue_entry *
390 alloc_commit_queue_entry(struct got_commit_object *commit,
391 struct got_object_id *id)
393 struct commit_queue_entry *entry;
395 entry = calloc(1, sizeof(*entry));
396 if (entry == NULL)
397 return NULL;
399 entry->id = id;
400 entry->commit = commit;
401 return entry;
404 static void
405 pop_commit(struct commit_queue *commits)
407 struct commit_queue_entry *entry;
409 entry = TAILQ_FIRST(&commits->head);
410 TAILQ_REMOVE(&commits->head, entry, entry);
411 got_object_commit_close(entry->commit);
412 commits->ncommits--;
413 /* Don't free entry->id! It is owned by the commit graph. */
414 free(entry);
417 static void
418 free_commits(struct commit_queue *commits)
420 while (!TAILQ_EMPTY(&commits->head))
421 pop_commit(commits);
424 static const struct got_error *
425 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
426 struct got_object_id *start_id, int minqueue, int init,
427 struct got_repository *repo, const char *path)
429 const struct got_error *err = NULL;
430 struct got_object_id *id;
431 struct commit_queue_entry *entry;
432 int nfetched, nqueued = 0, found_obj = 0;
433 int is_root_path = strcmp(path, "/") == 0;
435 err = got_commit_graph_iter_start(graph, start_id);
436 if (err)
437 return err;
439 entry = TAILQ_LAST(&commits->head, commit_queue_head);
440 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
441 int nfetched;
443 /* Start ID's commit is already on the queue; skip over it. */
444 err = got_commit_graph_iter_next(&id, graph);
445 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
446 return err;
448 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
449 if (err)
450 return err;
453 while (1) {
454 struct got_commit_object *commit;
456 err = got_commit_graph_iter_next(&id, graph);
457 if (err) {
458 if (err->code != GOT_ERR_ITER_NEED_MORE)
459 break;
460 if (nqueued >= minqueue) {
461 err = NULL;
462 break;
464 err = got_commit_graph_fetch_commits(&nfetched,
465 graph, 1, repo);
466 if (err)
467 return err;
468 continue;
470 if (id == NULL)
471 break;
473 err = got_object_open_as_commit(&commit, repo, id);
474 if (err)
475 break;
477 if (!is_root_path) {
478 struct got_object *obj;
479 struct got_object_qid *pid;
480 int changed = 0;
482 err = got_object_open_by_path(&obj, repo, id, path);
483 if (err) {
484 got_object_commit_close(commit);
485 if (err->code == GOT_ERR_NO_OBJ &&
486 (found_obj || !init)) {
487 /* History stops here. */
488 err = got_error(GOT_ERR_ITER_COMPLETED);
490 break;
492 found_obj = 1;
494 pid = SIMPLEQ_FIRST(&commit->parent_ids);
495 if (pid != NULL) {
496 struct got_object *pobj;
497 err = got_object_open_by_path(&pobj, repo,
498 pid->id, path);
499 if (err) {
500 if (err->code != GOT_ERR_NO_OBJ) {
501 got_object_close(obj);
502 got_object_commit_close(commit);
503 break;
505 err = NULL;
506 changed = 1;
507 } else {
508 struct got_object_id *id, *pid;
509 id = got_object_get_id(obj);
510 if (id == NULL) {
511 err = got_error_from_errno();
512 got_object_close(obj);
513 got_object_close(pobj);
514 break;
516 pid = got_object_get_id(pobj);
517 if (pid == NULL) {
518 err = got_error_from_errno();
519 free(id);
520 got_object_close(obj);
521 got_object_close(pobj);
522 break;
524 changed =
525 (got_object_id_cmp(id, pid) != 0);
526 got_object_close(pobj);
527 free(id);
528 free(pid);
531 got_object_close(obj);
532 if (!changed) {
533 got_object_commit_close(commit);
534 continue;
538 entry = alloc_commit_queue_entry(commit, id);
539 if (entry == NULL) {
540 err = got_error_from_errno();
541 break;
543 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
544 nqueued++;
545 commits->ncommits++;
548 return err;
551 static const struct got_error *
552 fetch_next_commit(struct commit_queue_entry **pentry,
553 struct commit_queue_entry *entry, struct commit_queue *commits,
554 struct got_commit_graph *graph, struct got_repository *repo,
555 const char *path)
557 const struct got_error *err = NULL;
559 *pentry = NULL;
561 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
562 if (err)
563 return err;
565 /* Next entry to display should now be available. */
566 *pentry = TAILQ_NEXT(entry, entry);
567 if (*pentry == NULL)
568 return got_error(GOT_ERR_NO_OBJ);
570 return NULL;
573 static const struct got_error *
574 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
576 const struct got_error *err = NULL;
577 struct got_reference *head_ref;
579 *head_id = NULL;
581 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
582 if (err)
583 return err;
585 err = got_ref_resolve(head_id, repo, head_ref);
586 got_ref_close(head_ref);
587 if (err) {
588 *head_id = NULL;
589 return err;
592 return NULL;
595 static const struct got_error *
596 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
597 struct commit_queue_entry **selected, struct commit_queue_entry *first,
598 struct commit_queue *commits, int selected_idx, int limit,
599 struct got_commit_graph *graph, struct got_repository *repo,
600 const char *path)
602 const struct got_error *err = NULL;
603 struct commit_queue_entry *entry;
604 int ncommits, width;
605 char *id_str, *header;
606 wchar_t *wline;
608 entry = first;
609 ncommits = 0;
610 while (entry) {
611 if (ncommits == selected_idx) {
612 *selected = entry;
613 break;
615 entry = TAILQ_NEXT(entry, entry);
616 ncommits++;
619 err = got_object_id_str(&id_str, (*selected)->id);
620 if (err)
621 return err;
623 if (path && strcmp(path, "/") != 0) {
624 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
625 err = got_error_from_errno();
626 free(id_str);
627 return err;
629 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
630 err = got_error_from_errno();
631 free(id_str);
632 return err;
634 free(id_str);
635 err = format_line(&wline, &width, header, view->ncols);
636 if (err) {
637 free(header);
638 return err;
640 free(header);
642 werase(view->window);
644 waddwstr(view->window, wline);
645 if (width < view->ncols)
646 waddch(view->window, '\n');
647 free(wline);
648 if (limit <= 1)
649 return NULL;
651 entry = first;
652 *last = first;
653 ncommits = 0;
654 while (entry) {
655 if (ncommits >= limit - 1)
656 break;
657 if (ncommits == selected_idx)
658 wstandout(view->window);
659 err = draw_commit(view, entry->commit, entry->id);
660 if (ncommits == selected_idx)
661 wstandend(view->window);
662 if (err)
663 break;
664 ncommits++;
665 *last = entry;
666 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
667 err = queue_commits(graph, commits, entry->id, 1,
668 0, repo, path);
669 if (err) {
670 if (err->code != GOT_ERR_ITER_COMPLETED)
671 return err;
672 err = NULL;
675 entry = TAILQ_NEXT(entry, entry);
678 update_panels();
679 doupdate();
681 return err;
684 static void
685 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
686 struct commit_queue *commits)
688 struct commit_queue_entry *entry;
689 int nscrolled = 0;
691 entry = TAILQ_FIRST(&commits->head);
692 if (*first_displayed_entry == entry)
693 return;
695 entry = *first_displayed_entry;
696 while (entry && nscrolled < maxscroll) {
697 entry = TAILQ_PREV(entry, commit_queue_head, entry);
698 if (entry) {
699 *first_displayed_entry = entry;
700 nscrolled++;
705 static const struct got_error *
706 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
707 struct commit_queue_entry *last_displayed_entry,
708 struct commit_queue *commits, struct got_commit_graph *graph,
709 struct got_repository *repo, const char *path)
711 const struct got_error *err = NULL;
712 struct commit_queue_entry *pentry;
713 int nscrolled = 0;
715 do {
716 pentry = TAILQ_NEXT(last_displayed_entry, entry);
717 if (pentry == NULL) {
718 err = fetch_next_commit(&pentry, last_displayed_entry,
719 commits, graph, repo, path);
720 if (err || pentry == NULL)
721 break;
723 last_displayed_entry = pentry;
725 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
726 if (pentry == NULL)
727 break;
728 *first_displayed_entry = pentry;
729 } while (++nscrolled < maxscroll);
731 return err;
734 static const struct got_error *
735 show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
736 struct got_repository *repo)
738 const struct got_error *err;
739 struct got_object *obj1 = NULL, *obj2 = NULL;
740 struct got_object_qid *parent_id;
741 struct tog_view *view;
743 err = got_object_open(&obj2, repo, entry->id);
744 if (err)
745 return err;
747 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
748 if (parent_id) {
749 err = got_object_open(&obj1, repo, parent_id->id);
750 if (err)
751 goto done;
754 view = open_view(0, 0, 0, 0, parent_view);
755 if (view == NULL) {
756 err = got_error_from_errno();
757 goto done;
760 err = show_diff_view(view, obj1, obj2, repo);
761 close_view(view);
762 show_view(parent_view);
763 done:
764 if (obj1)
765 got_object_close(obj1);
766 if (obj2)
767 got_object_close(obj2);
768 return err;
771 static const struct got_error *
772 browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
773 struct got_repository *repo)
775 const struct got_error *err = NULL;
776 struct got_tree_object *tree;
777 struct tog_view *view;
779 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
780 if (err)
781 return err;
783 view = open_view(0, 0, 0, 0, parent_view);
784 if (view == NULL) {
785 err = got_error_from_errno();
786 goto done;
788 err = show_tree_view(view, tree, entry->id, repo);
789 close_view(view);
790 show_view(parent_view);
791 done:
792 got_object_tree_close(tree);
793 return err;
796 static const struct got_error *
797 show_log_view(struct tog_view *view, struct got_object_id *start_id,
798 struct got_repository *repo, const char *path)
800 const struct got_error *err = NULL;
801 struct got_object_id *head_id = NULL;
802 int ch, done = 0, selected = 0, nfetched;
803 struct got_commit_graph *graph = NULL;
804 struct commit_queue commits;
805 struct commit_queue_entry *first_displayed_entry = NULL;
806 struct commit_queue_entry *last_displayed_entry = NULL;
807 struct commit_queue_entry *selected_entry = NULL;
808 char *in_repo_path = NULL;
810 err = got_repo_map_path(&in_repo_path, repo, path);
811 if (err != NULL)
812 goto done;
814 err = get_head_commit_id(&head_id, repo);
815 if (err)
816 return err;
818 /* The graph contains all commits. */
819 err = got_commit_graph_open(&graph, head_id, 0, repo);
820 if (err)
821 goto done;
822 /* The commit queue contains a subset of commits filtered by path. */
823 TAILQ_INIT(&commits.head);
824 commits.ncommits = 0;
826 /* Populate commit graph with a sufficient number of commits. */
827 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
828 repo);
829 if (err)
830 goto done;
832 /*
833 * Open the initial batch of commits, sorted in commit graph order.
834 * We keep all commits open throughout the lifetime of the log view
835 * in order to avoid having to re-fetch commits from disk while
836 * updating the display.
837 */
838 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
839 in_repo_path);
840 if (err) {
841 if (err->code != GOT_ERR_ITER_COMPLETED)
842 goto done;
843 err = NULL;
846 show_view(view);
848 first_displayed_entry = TAILQ_FIRST(&commits.head);
849 selected_entry = first_displayed_entry;
850 while (!done) {
851 err = draw_commits(view, &last_displayed_entry, &selected_entry,
852 first_displayed_entry, &commits, selected, view->nlines,
853 graph, repo, in_repo_path);
854 if (err)
855 goto done;
857 nodelay(stdscr, FALSE);
858 ch = wgetch(view->window);
859 nodelay(stdscr, TRUE);
860 switch (ch) {
861 case ERR:
862 break;
863 case 'q':
864 done = 1;
865 break;
866 case 'k':
867 case KEY_UP:
868 if (selected > 0)
869 selected--;
870 if (selected > 0)
871 break;
872 scroll_up(&first_displayed_entry, 1, &commits);
873 break;
874 case KEY_PPAGE:
875 if (TAILQ_FIRST(&commits.head) ==
876 first_displayed_entry) {
877 selected = 0;
878 break;
880 scroll_up(&first_displayed_entry, view->nlines,
881 &commits);
882 break;
883 case 'j':
884 case KEY_DOWN:
885 if (selected < MIN(view->nlines - 2,
886 commits.ncommits - 1)) {
887 selected++;
888 break;
890 err = scroll_down(&first_displayed_entry, 1,
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 err = NULL;
898 break;
899 case KEY_NPAGE: {
900 struct commit_queue_entry *first;
901 first = first_displayed_entry;
902 err = scroll_down(&first_displayed_entry,
903 view->nlines, last_displayed_entry,
904 &commits, graph, repo, in_repo_path);
905 if (err) {
906 if (err->code != GOT_ERR_ITER_COMPLETED)
907 goto done;
908 if (first == first_displayed_entry &&
909 selected < MIN(view->nlines - 2,
910 commits.ncommits - 1)) {
911 /* can't scroll further down */
912 selected = MIN(view->nlines - 2,
913 commits.ncommits - 1);
915 err = NULL;
917 break;
919 case KEY_RESIZE:
920 err = view_resize(view);
921 if (err)
922 goto done;
923 if (selected > view->nlines - 2)
924 selected = view->nlines - 2;
925 if (selected > commits.ncommits - 1)
926 selected = commits.ncommits - 1;
927 break;
928 case KEY_ENTER:
929 case '\r':
930 err = show_commit(view, selected_entry, repo);
931 if (err)
932 goto done;
933 show_view(view);
934 break;
935 case 't':
936 err = browse_commit(view, selected_entry, repo);
937 if (err)
938 goto done;
939 show_view(view);
940 break;
941 default:
942 break;
945 done:
946 free(head_id);
947 if (graph)
948 got_commit_graph_close(graph);
949 free_commits(&commits);
950 free(in_repo_path);
951 return err;
954 static const struct got_error *
955 cmd_log(int argc, char *argv[])
957 const struct got_error *error;
958 struct got_repository *repo = NULL;
959 struct got_object_id *start_id = NULL;
960 char *path = NULL, *repo_path = NULL, *cwd = NULL;
961 char *start_commit = NULL;
962 int ch;
963 struct tog_view *view;
965 #ifndef PROFILE
966 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
967 err(1, "pledge");
968 #endif
970 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
971 switch (ch) {
972 case 'c':
973 start_commit = optarg;
974 break;
975 case 'r':
976 repo_path = realpath(optarg, NULL);
977 if (repo_path == NULL)
978 err(1, "-r option");
979 break;
980 default:
981 usage();
982 /* NOTREACHED */
986 argc -= optind;
987 argv += optind;
989 if (argc == 0)
990 path = strdup("");
991 else if (argc == 1)
992 path = strdup(argv[0]);
993 else
994 usage_log();
995 if (path == NULL)
996 return got_error_from_errno();
998 cwd = getcwd(NULL, 0);
999 if (cwd == NULL) {
1000 error = got_error_from_errno();
1001 goto done;
1003 if (repo_path == NULL) {
1004 repo_path = strdup(cwd);
1005 if (repo_path == NULL) {
1006 error = got_error_from_errno();
1007 goto done;
1011 error = got_repo_open(&repo, repo_path);
1012 if (error != NULL)
1013 goto done;
1015 if (start_commit == NULL) {
1016 error = get_head_commit_id(&start_id, repo);
1017 if (error != NULL)
1018 goto done;
1019 } else {
1020 struct got_object *obj;
1021 error = got_object_open_by_id_str(&obj, repo, start_commit);
1022 if (error == NULL) {
1023 start_id = got_object_get_id(obj);
1024 if (start_id == NULL)
1025 error = got_error_from_errno();
1026 goto done;
1029 if (error != NULL)
1030 goto done;
1032 view = open_view(0, 0, 0, 0, NULL);
1033 if (view == NULL) {
1034 error = got_error_from_errno();
1035 goto done;
1037 error = show_log_view(view, start_id, repo, path);
1038 close_view(view);
1039 done:
1040 free(repo_path);
1041 free(cwd);
1042 free(path);
1043 free(start_id);
1044 if (repo)
1045 got_repo_close(repo);
1046 return error;
1049 __dead static void
1050 usage_diff(void)
1052 endwin();
1053 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1054 getprogname());
1055 exit(1);
1058 static char *
1059 parse_next_line(FILE *f, size_t *len)
1061 char *line;
1062 size_t linelen;
1063 size_t lineno;
1064 const char delim[3] = { '\0', '\0', '\0'};
1066 line = fparseln(f, &linelen, &lineno, delim, 0);
1067 if (len)
1068 *len = linelen;
1069 return line;
1072 static const struct got_error *
1073 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1074 int *last_displayed_line, int *eof, int max_lines)
1076 const struct got_error *err;
1077 int nlines = 0, nprinted = 0;
1078 char *line;
1079 size_t len;
1080 wchar_t *wline;
1081 int width;
1083 rewind(f);
1084 werase(view->window);
1086 *eof = 0;
1087 while (nprinted < max_lines) {
1088 line = parse_next_line(f, &len);
1089 if (line == NULL) {
1090 *eof = 1;
1091 break;
1093 if (++nlines < *first_displayed_line) {
1094 free(line);
1095 continue;
1098 err = format_line(&wline, &width, line, view->ncols);
1099 if (err) {
1100 free(line);
1101 free(wline);
1102 return err;
1104 waddwstr(view->window, wline);
1105 if (width < view->ncols)
1106 waddch(view->window, '\n');
1107 if (++nprinted == 1)
1108 *first_displayed_line = nlines;
1109 free(line);
1110 free(wline);
1111 wline = NULL;
1113 *last_displayed_line = nlines;
1115 update_panels();
1116 doupdate();
1118 return NULL;
1121 static const struct got_error *
1122 show_diff_view(struct tog_view *view, struct got_object *obj1,
1123 struct got_object *obj2, struct got_repository *repo)
1125 const struct got_error *err;
1126 FILE *f;
1127 int ch, done = 0;
1128 int first_displayed_line = 1, last_displayed_line = view->nlines;
1129 int eof, i;
1131 if (obj1 != NULL && obj2 != NULL &&
1132 got_object_get_type(obj1) != got_object_get_type(obj2))
1133 return got_error(GOT_ERR_OBJ_TYPE);
1135 f = got_opentemp();
1136 if (f == NULL)
1137 return got_error_from_errno();
1139 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1140 case GOT_OBJ_TYPE_BLOB:
1141 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1142 break;
1143 case GOT_OBJ_TYPE_TREE:
1144 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1145 break;
1146 case GOT_OBJ_TYPE_COMMIT:
1147 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1148 break;
1149 default:
1150 return got_error(GOT_ERR_OBJ_TYPE);
1153 fflush(f);
1155 show_view(view);
1157 while (!done) {
1158 err = draw_file(view, f, &first_displayed_line,
1159 &last_displayed_line, &eof, view->nlines);
1160 if (err)
1161 break;
1162 nodelay(stdscr, FALSE);
1163 ch = wgetch(view->window);
1164 nodelay(stdscr, TRUE);
1165 switch (ch) {
1166 case 'q':
1167 done = 1;
1168 break;
1169 case 'k':
1170 case KEY_UP:
1171 if (first_displayed_line > 1)
1172 first_displayed_line--;
1173 break;
1174 case KEY_PPAGE:
1175 case KEY_BACKSPACE:
1176 i = 0;
1177 while (i++ < view->nlines - 1 &&
1178 first_displayed_line > 1)
1179 first_displayed_line--;
1180 break;
1181 case 'j':
1182 case KEY_DOWN:
1183 if (!eof)
1184 first_displayed_line++;
1185 break;
1186 case KEY_NPAGE:
1187 case ' ':
1188 i = 0;
1189 while (!eof && i++ < view->nlines - 1) {
1190 char *line = parse_next_line(f, NULL);
1191 first_displayed_line++;
1192 if (line == NULL)
1193 break;
1195 break;
1196 case KEY_RESIZE:
1197 err = view_resize(view);
1198 if (err)
1199 goto done;
1200 break;
1201 default:
1202 break;
1205 done:
1206 fclose(f);
1207 return err;
1210 static const struct got_error *
1211 cmd_diff(int argc, char *argv[])
1213 const struct got_error *error = NULL;
1214 struct got_repository *repo = NULL;
1215 struct got_object *obj1 = NULL, *obj2 = NULL;
1216 char *repo_path = NULL;
1217 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1218 int ch;
1219 struct tog_view *view;
1221 #ifndef PROFILE
1222 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1223 err(1, "pledge");
1224 #endif
1226 while ((ch = getopt(argc, argv, "")) != -1) {
1227 switch (ch) {
1228 default:
1229 usage();
1230 /* NOTREACHED */
1234 argc -= optind;
1235 argv += optind;
1237 if (argc == 0) {
1238 usage_diff(); /* TODO show local worktree changes */
1239 } else if (argc == 2) {
1240 repo_path = getcwd(NULL, 0);
1241 if (repo_path == NULL)
1242 return got_error_from_errno();
1243 obj_id_str1 = argv[0];
1244 obj_id_str2 = argv[1];
1245 } else if (argc == 3) {
1246 repo_path = realpath(argv[0], NULL);
1247 if (repo_path == NULL)
1248 return got_error_from_errno();
1249 obj_id_str1 = argv[1];
1250 obj_id_str2 = argv[2];
1251 } else
1252 usage_diff();
1254 error = got_repo_open(&repo, repo_path);
1255 free(repo_path);
1256 if (error)
1257 goto done;
1259 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1260 if (error)
1261 goto done;
1263 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1264 if (error)
1265 goto done;
1267 view = open_view(0, 0, 0, 0, NULL);
1268 if (view == NULL) {
1269 error = got_error_from_errno();
1270 goto done;
1272 error = show_diff_view(view, obj1, obj2, repo);
1273 close_view(view);
1274 done:
1275 got_repo_close(repo);
1276 if (obj1)
1277 got_object_close(obj1);
1278 if (obj2)
1279 got_object_close(obj2);
1280 return error;
1283 __dead static void
1284 usage_blame(void)
1286 endwin();
1287 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1288 getprogname());
1289 exit(1);
1292 struct tog_blame_line {
1293 int annotated;
1294 struct got_object_id *id;
1297 static const struct got_error *
1298 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1299 const char *path, struct tog_blame_line *lines, int nlines,
1300 int blame_complete, int selected_line, int *first_displayed_line,
1301 int *last_displayed_line, int *eof, int max_lines)
1303 const struct got_error *err;
1304 int lineno = 0, nprinted = 0;
1305 char *line;
1306 size_t len;
1307 wchar_t *wline;
1308 int width, wlimit;
1309 struct tog_blame_line *blame_line;
1310 struct got_object_id *prev_id = NULL;
1311 char *id_str;
1313 err = got_object_id_str(&id_str, id);
1314 if (err)
1315 return err;
1317 rewind(f);
1318 werase(view->window);
1320 if (asprintf(&line, "commit: %s", id_str) == -1) {
1321 err = got_error_from_errno();
1322 free(id_str);
1323 return err;
1326 err = format_line(&wline, &width, line, view->ncols);
1327 free(line);
1328 line = NULL;
1329 waddwstr(view->window, wline);
1330 free(wline);
1331 wline = NULL;
1332 if (width < view->ncols)
1333 waddch(view->window, '\n');
1335 if (asprintf(&line, "[%d/%d] %s%s",
1336 *first_displayed_line - 1 + selected_line, nlines,
1337 blame_complete ? "" : "annotating ", path) == -1) {
1338 free(id_str);
1339 return got_error_from_errno();
1341 free(id_str);
1342 err = format_line(&wline, &width, line, view->ncols);
1343 free(line);
1344 line = NULL;
1345 if (err)
1346 return err;
1347 waddwstr(view->window, wline);
1348 free(wline);
1349 wline = NULL;
1350 if (width < view->ncols)
1351 waddch(view->window, '\n');
1353 *eof = 0;
1354 while (nprinted < max_lines - 2) {
1355 line = parse_next_line(f, &len);
1356 if (line == NULL) {
1357 *eof = 1;
1358 break;
1360 if (++lineno < *first_displayed_line) {
1361 free(line);
1362 continue;
1365 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1366 err = format_line(&wline, &width, line, wlimit);
1367 if (err) {
1368 free(line);
1369 return err;
1372 if (nprinted == selected_line - 1)
1373 wstandout(view->window);
1375 blame_line = &lines[lineno - 1];
1376 if (blame_line->annotated && prev_id &&
1377 got_object_id_cmp(prev_id, blame_line->id) == 0)
1378 waddstr(view->window, " ");
1379 else if (blame_line->annotated) {
1380 char *id_str;
1381 err = got_object_id_str(&id_str, blame_line->id);
1382 if (err) {
1383 free(line);
1384 free(wline);
1385 return err;
1387 wprintw(view->window, "%.8s ", id_str);
1388 free(id_str);
1389 prev_id = blame_line->id;
1390 } else {
1391 waddstr(view->window, "........ ");
1392 prev_id = NULL;
1395 waddwstr(view->window, wline);
1396 while (width < wlimit) {
1397 waddch(view->window, ' ');
1398 width++;
1400 if (nprinted == selected_line - 1)
1401 wstandend(view->window);
1402 if (++nprinted == 1)
1403 *first_displayed_line = lineno;
1404 free(line);
1405 free(wline);
1406 wline = NULL;
1408 *last_displayed_line = lineno;
1410 update_panels();
1411 doupdate();
1413 return NULL;
1416 struct tog_blame_cb_args {
1417 pthread_mutex_t *mutex;
1418 struct tog_blame_line *lines; /* one per line */
1419 int nlines;
1421 struct tog_view *view;
1422 struct got_object_id *commit_id;
1423 FILE *f;
1424 const char *path;
1425 int *first_displayed_line;
1426 int *last_displayed_line;
1427 int *selected_line;
1428 int *quit;
1431 static const struct got_error *
1432 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1434 const struct got_error *err = NULL;
1435 struct tog_blame_cb_args *a = arg;
1436 struct tog_blame_line *line;
1437 int eof;
1439 if (nlines != a->nlines ||
1440 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1441 return got_error(GOT_ERR_RANGE);
1443 if (pthread_mutex_lock(a->mutex) != 0)
1444 return got_error_from_errno();
1446 if (*a->quit) { /* user has quit the blame view */
1447 err = got_error(GOT_ERR_ITER_COMPLETED);
1448 goto done;
1451 if (lineno == -1)
1452 goto done; /* no change in this commit */
1454 line = &a->lines[lineno - 1];
1455 if (line->annotated)
1456 goto done;
1458 line->id = got_object_id_dup(id);
1459 if (line->id == NULL) {
1460 err = got_error_from_errno();
1461 goto done;
1463 line->annotated = 1;
1465 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1466 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1467 a->last_displayed_line, &eof, a->view->nlines);
1468 done:
1469 if (pthread_mutex_unlock(a->mutex) != 0)
1470 return got_error_from_errno();
1471 return err;
1474 struct tog_blame_thread_args {
1475 const char *path;
1476 struct got_repository *repo;
1477 struct tog_blame_cb_args *cb_args;
1478 int *complete;
1481 static void *
1482 blame_thread(void *arg)
1484 const struct got_error *err;
1485 struct tog_blame_thread_args *ta = arg;
1486 struct tog_blame_cb_args *a = ta->cb_args;
1487 int eof;
1489 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1490 blame_cb, ta->cb_args);
1492 if (pthread_mutex_lock(a->mutex) != 0)
1493 return (void *)got_error_from_errno();
1495 got_repo_close(ta->repo);
1496 ta->repo = NULL;
1497 *ta->complete = 1;
1498 if (!err)
1499 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1500 a->lines, a->nlines, 1, *a->selected_line,
1501 a->first_displayed_line, a->last_displayed_line, &eof,
1502 a->view->nlines);
1504 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1505 err = got_error_from_errno();
1507 return (void *)err;
1510 static struct got_object_id *
1511 get_selected_commit_id(struct tog_blame_line *lines,
1512 int first_displayed_line, int selected_line)
1514 struct tog_blame_line *line;
1516 line = &lines[first_displayed_line - 1 + selected_line - 1];
1517 if (!line->annotated)
1518 return NULL;
1520 return line->id;
1523 static const struct got_error *
1524 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1525 struct tog_blame_line *lines, int first_displayed_line,
1526 int selected_line, struct got_repository *repo)
1528 const struct got_error *err = NULL;
1529 struct got_commit_object *commit = NULL;
1530 struct got_object_id *selected_id;
1531 struct got_object_qid *pid;
1533 *pobj = NULL;
1534 *obj = NULL;
1536 selected_id = get_selected_commit_id(lines,
1537 first_displayed_line, selected_line);
1538 if (selected_id == NULL)
1539 return NULL;
1541 err = got_object_open(obj, repo, selected_id);
1542 if (err)
1543 goto done;
1545 err = got_object_commit_open(&commit, repo, *obj);
1546 if (err)
1547 goto done;
1549 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1550 if (pid) {
1551 err = got_object_open(pobj, repo, pid->id);
1552 if (err)
1553 goto done;
1555 done:
1556 if (commit)
1557 got_object_commit_close(commit);
1558 return err;
1561 struct tog_blame {
1562 FILE *f;
1563 size_t filesize;
1564 struct tog_blame_line *lines;
1565 size_t nlines;
1566 pthread_t thread;
1567 struct tog_blame_thread_args thread_args;
1568 struct tog_blame_cb_args cb_args;
1569 const char *path;
1572 static const struct got_error *
1573 stop_blame(struct tog_blame *blame)
1575 const struct got_error *err = NULL;
1576 int i;
1578 if (blame->thread) {
1579 if (pthread_join(blame->thread, (void **)&err) != 0)
1580 err = got_error_from_errno();
1581 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1582 err = NULL;
1583 blame->thread = NULL;
1585 if (blame->thread_args.repo) {
1586 got_repo_close(blame->thread_args.repo);
1587 blame->thread_args.repo = NULL;
1589 if (blame->f) {
1590 fclose(blame->f);
1591 blame->f = NULL;
1593 for (i = 0; i < blame->nlines; i++)
1594 free(blame->lines[i].id);
1595 free(blame->lines);
1596 blame->lines = NULL;
1597 free(blame->cb_args.commit_id);
1598 blame->cb_args.commit_id = NULL;
1600 return err;
1603 static const struct got_error *
1604 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1605 struct tog_view *view, int *blame_complete,
1606 int *first_displayed_line, int *last_displayed_line,
1607 int *selected_line, int *done, const char *path,
1608 struct got_object_id *commit_id,
1609 struct got_repository *repo)
1611 const struct got_error *err = NULL;
1612 struct got_blob_object *blob = NULL;
1613 struct got_repository *thread_repo = NULL;
1614 struct got_object *obj;
1616 err = got_object_open_by_path(&obj, repo, commit_id, path);
1617 if (err)
1618 goto done;
1619 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1620 err = got_error(GOT_ERR_OBJ_TYPE);
1621 goto done;
1624 err = got_object_blob_open(&blob, repo, obj, 8192);
1625 if (err)
1626 goto done;
1627 blame->f = got_opentemp();
1628 if (blame->f == NULL) {
1629 err = got_error_from_errno();
1630 goto done;
1632 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1633 blame->f, blob);
1634 if (err)
1635 goto done;
1637 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1638 if (blame->lines == NULL) {
1639 err = got_error_from_errno();
1640 goto done;
1643 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1644 if (err)
1645 goto done;
1647 blame->cb_args.view = view;
1648 blame->cb_args.lines = blame->lines;
1649 blame->cb_args.nlines = blame->nlines;
1650 blame->cb_args.mutex = mutex;
1651 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1652 if (blame->cb_args.commit_id == NULL) {
1653 err = got_error_from_errno();
1654 goto done;
1656 blame->cb_args.f = blame->f;
1657 blame->cb_args.path = path;
1658 blame->cb_args.first_displayed_line = first_displayed_line;
1659 blame->cb_args.selected_line = selected_line;
1660 blame->cb_args.last_displayed_line = last_displayed_line;
1661 blame->cb_args.quit = done;
1663 blame->thread_args.path = path;
1664 blame->thread_args.repo = thread_repo;
1665 blame->thread_args.cb_args = &blame->cb_args;
1666 blame->thread_args.complete = blame_complete;
1667 *blame_complete = 0;
1669 if (pthread_create(&blame->thread, NULL, blame_thread,
1670 &blame->thread_args) != 0) {
1671 err = got_error_from_errno();
1672 goto done;
1675 done:
1676 if (blob)
1677 got_object_blob_close(blob);
1678 if (obj)
1679 got_object_close(obj);
1680 if (err)
1681 stop_blame(blame);
1682 return err;
1685 static const struct got_error *
1686 show_blame_view(struct tog_view *view, const char *path,
1687 struct got_object_id *commit_id, struct got_repository *repo)
1689 const struct got_error *err = NULL, *thread_err = NULL;
1690 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1691 int selected_line = first_displayed_line;
1692 int eof, blame_complete = 0;
1693 struct got_object *obj = NULL, *pobj = NULL;
1694 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1695 struct tog_blame blame;
1696 struct got_object_id_queue blamed_commits;
1697 struct got_object_qid *blamed_commit = NULL;
1698 struct tog_view *diff_view;
1700 SIMPLEQ_INIT(&blamed_commits);
1702 if (pthread_mutex_init(&mutex, NULL) != 0) {
1703 err = got_error_from_errno();
1704 goto done;
1707 err = got_object_qid_alloc(&blamed_commit, commit_id);
1708 if (err)
1709 goto done;
1710 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1712 show_view(view);
1713 last_displayed_line = view->nlines;
1715 memset(&blame, 0, sizeof(blame));
1716 err = run_blame(&blame, &mutex, view, &blame_complete,
1717 &first_displayed_line, &last_displayed_line,
1718 &selected_line, &done, path, blamed_commit->id, repo);
1719 if (err)
1720 return err;
1722 while (!done) {
1723 if (pthread_mutex_lock(&mutex) != 0) {
1724 err = got_error_from_errno();
1725 goto done;
1727 err = draw_blame(view, blamed_commit->id, blame.f, path,
1728 blame.lines, blame.nlines, blame_complete, selected_line,
1729 &first_displayed_line, &last_displayed_line, &eof,
1730 view->nlines);
1731 if (pthread_mutex_unlock(&mutex) != 0) {
1732 err = got_error_from_errno();
1733 goto done;
1735 if (err)
1736 break;
1737 nodelay(stdscr, FALSE);
1738 ch = wgetch(view->window);
1739 nodelay(stdscr, TRUE);
1740 if (pthread_mutex_lock(&mutex) != 0) {
1741 err = got_error_from_errno();
1742 goto done;
1744 switch (ch) {
1745 case 'q':
1746 done = 1;
1747 break;
1748 case 'k':
1749 case KEY_UP:
1750 if (selected_line > 1)
1751 selected_line--;
1752 else if (selected_line == 1 &&
1753 first_displayed_line > 1)
1754 first_displayed_line--;
1755 break;
1756 case KEY_PPAGE:
1757 case KEY_BACKSPACE:
1758 if (first_displayed_line == 1) {
1759 selected_line = 1;
1760 break;
1762 if (first_displayed_line > view->nlines - 2)
1763 first_displayed_line -=
1764 (view->nlines - 2);
1765 else
1766 first_displayed_line = 1;
1767 break;
1768 case 'j':
1769 case KEY_DOWN:
1770 if (selected_line < view->nlines - 2 &&
1771 first_displayed_line + selected_line <=
1772 blame.nlines)
1773 selected_line++;
1774 else if (last_displayed_line < blame.nlines)
1775 first_displayed_line++;
1776 break;
1777 case 'b':
1778 case 'p': {
1779 struct got_object_id *id;
1780 id = get_selected_commit_id(blame.lines,
1781 first_displayed_line, selected_line);
1782 if (id == NULL || got_object_id_cmp(id,
1783 blamed_commit->id) == 0)
1784 break;
1785 err = open_selected_commit(&pobj, &obj,
1786 blame.lines, first_displayed_line,
1787 selected_line, repo);
1788 if (err)
1789 break;
1790 if (pobj == NULL && obj == NULL)
1791 break;
1792 if (ch == 'p' && pobj == NULL)
1793 break;
1794 done = 1;
1795 if (pthread_mutex_unlock(&mutex) != 0) {
1796 err = got_error_from_errno();
1797 goto done;
1799 thread_err = stop_blame(&blame);
1800 done = 0;
1801 if (pthread_mutex_lock(&mutex) != 0) {
1802 err = got_error_from_errno();
1803 goto done;
1805 if (thread_err)
1806 break;
1807 id = got_object_get_id(ch == 'b' ? obj : pobj);
1808 got_object_close(obj);
1809 obj = NULL;
1810 if (pobj) {
1811 got_object_close(pobj);
1812 pobj = NULL;
1814 if (id == NULL) {
1815 err = got_error_from_errno();
1816 break;
1818 err = got_object_qid_alloc(&blamed_commit, id);
1819 free(id);
1820 if (err)
1821 goto done;
1822 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1823 blamed_commit, entry);
1824 err = run_blame(&blame, &mutex, view,
1825 &blame_complete, &first_displayed_line,
1826 &last_displayed_line, &selected_line,
1827 &done, path, blamed_commit->id, repo);
1828 if (err)
1829 break;
1830 break;
1832 case 'B': {
1833 struct got_object_qid *first;
1834 first = SIMPLEQ_FIRST(&blamed_commits);
1835 if (!got_object_id_cmp(first->id, commit_id))
1836 break;
1837 done = 1;
1838 if (pthread_mutex_unlock(&mutex) != 0) {
1839 err = got_error_from_errno();
1840 goto done;
1842 thread_err = stop_blame(&blame);
1843 done = 0;
1844 if (pthread_mutex_lock(&mutex) != 0) {
1845 err = got_error_from_errno();
1846 goto done;
1848 if (thread_err)
1849 break;
1850 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1851 got_object_qid_free(blamed_commit);
1852 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1853 err = run_blame(&blame, &mutex, view,
1854 &blame_complete, &first_displayed_line,
1855 &last_displayed_line, &selected_line,
1856 &done, path, blamed_commit->id, repo);
1857 if (err)
1858 break;
1859 break;
1861 case KEY_ENTER:
1862 case '\r':
1863 err = open_selected_commit(&pobj, &obj,
1864 blame.lines, first_displayed_line,
1865 selected_line, repo);
1866 if (err)
1867 break;
1868 if (pobj == NULL && obj == NULL)
1869 break;
1870 diff_view = open_view(0, 0, 0, 0, view);
1871 if (diff_view == NULL) {
1872 err = got_error_from_errno();
1873 break;
1875 err = show_diff_view(diff_view, pobj, obj, repo);
1876 close_view(diff_view);
1877 show_view(view);
1878 if (pobj) {
1879 got_object_close(pobj);
1880 pobj = NULL;
1882 got_object_close(obj);
1883 obj = NULL;
1884 if (err)
1885 break;
1886 break;
1887 case KEY_NPAGE:
1888 case ' ':
1889 if (last_displayed_line >= blame.nlines &&
1890 selected_line < view->nlines - 2) {
1891 selected_line = MIN(blame.nlines,
1892 view->nlines - 2);
1893 break;
1895 if (last_displayed_line + view->nlines - 2 <=
1896 blame.nlines)
1897 first_displayed_line +=
1898 view->nlines - 2;
1899 else
1900 first_displayed_line =
1901 blame.nlines - (view->nlines - 3);
1902 break;
1903 case KEY_RESIZE:
1904 err = view_resize(view);
1905 if (err)
1906 break;
1907 if (selected_line > view->nlines - 2) {
1908 selected_line = MIN(blame.nlines,
1909 view->nlines - 2);
1911 break;
1912 default:
1913 break;
1915 if (pthread_mutex_unlock(&mutex) != 0)
1916 err = got_error_from_errno();
1917 if (err || thread_err)
1918 break;
1920 done:
1921 if (pobj)
1922 got_object_close(pobj);
1923 if (blame.thread)
1924 thread_err = stop_blame(&blame);
1925 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1926 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1927 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1928 got_object_qid_free(blamed_commit);
1930 return thread_err ? thread_err : err;
1933 static const struct got_error *
1934 cmd_blame(int argc, char *argv[])
1936 const struct got_error *error;
1937 struct got_repository *repo = NULL;
1938 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1939 struct got_object_id *commit_id = NULL;
1940 char *commit_id_str = NULL;
1941 int ch;
1942 struct tog_view *view;
1944 #ifndef PROFILE
1945 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1946 err(1, "pledge");
1947 #endif
1949 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1950 switch (ch) {
1951 case 'c':
1952 commit_id_str = optarg;
1953 break;
1954 case 'r':
1955 repo_path = realpath(optarg, NULL);
1956 if (repo_path == NULL)
1957 err(1, "-r option");
1958 break;
1959 default:
1960 usage();
1961 /* NOTREACHED */
1965 argc -= optind;
1966 argv += optind;
1968 if (argc == 1)
1969 path = argv[0];
1970 else
1971 usage_blame();
1973 cwd = getcwd(NULL, 0);
1974 if (cwd == NULL) {
1975 error = got_error_from_errno();
1976 goto done;
1978 if (repo_path == NULL) {
1979 repo_path = strdup(cwd);
1980 if (repo_path == NULL) {
1981 error = got_error_from_errno();
1982 goto done;
1987 error = got_repo_open(&repo, repo_path);
1988 if (error != NULL)
1989 return error;
1991 error = got_repo_map_path(&in_repo_path, repo, path);
1992 if (error != NULL)
1993 goto done;
1995 if (commit_id_str == NULL) {
1996 struct got_reference *head_ref;
1997 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1998 if (error != NULL)
1999 goto done;
2000 error = got_ref_resolve(&commit_id, repo, head_ref);
2001 got_ref_close(head_ref);
2002 } else {
2003 struct got_object *obj;
2004 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2005 if (error != NULL)
2006 goto done;
2007 commit_id = got_object_get_id(obj);
2008 if (commit_id == NULL)
2009 error = got_error_from_errno();
2010 got_object_close(obj);
2012 if (error != NULL)
2013 goto done;
2015 view = open_view(0, 0, 0, 0, NULL);
2016 if (view == NULL) {
2017 error = got_error_from_errno();
2018 goto done;
2020 error = show_blame_view(view, in_repo_path, commit_id, repo);
2021 close_view(view);
2022 done:
2023 free(in_repo_path);
2024 free(repo_path);
2025 free(cwd);
2026 free(commit_id);
2027 if (repo)
2028 got_repo_close(repo);
2029 return error;
2032 static const struct got_error *
2033 draw_tree_entries(struct tog_view *view,
2034 struct got_tree_entry **first_displayed_entry,
2035 struct got_tree_entry **last_displayed_entry,
2036 struct got_tree_entry **selected_entry, int *ndisplayed,
2037 const char *label, int show_ids, const char *parent_path,
2038 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2040 const struct got_error *err = NULL;
2041 struct got_tree_entry *te;
2042 wchar_t *wline;
2043 int width, n;
2045 *ndisplayed = 0;
2047 werase(view->window);
2049 if (limit == 0)
2050 return NULL;
2052 err = format_line(&wline, &width, label, view->ncols);
2053 if (err)
2054 return err;
2055 waddwstr(view->window, wline);
2056 free(wline);
2057 wline = NULL;
2058 if (width < view->ncols)
2059 waddch(view->window, '\n');
2060 if (--limit <= 0)
2061 return NULL;
2062 err = format_line(&wline, &width, parent_path, view->ncols);
2063 if (err)
2064 return err;
2065 waddwstr(view->window, wline);
2066 free(wline);
2067 wline = NULL;
2068 if (width < view->ncols)
2069 waddch(view->window, '\n');
2070 if (--limit <= 0)
2071 return NULL;
2072 waddch(view->window, '\n');
2073 if (--limit <= 0)
2074 return NULL;
2076 te = SIMPLEQ_FIRST(&entries->head);
2077 if (*first_displayed_entry == NULL) {
2078 if (selected == 0) {
2079 wstandout(view->window);
2080 *selected_entry = NULL;
2082 waddstr(view->window, " ..\n"); /* parent directory */
2083 if (selected == 0)
2084 wstandend(view->window);
2085 (*ndisplayed)++;
2086 if (--limit <= 0)
2087 return NULL;
2088 n = 1;
2089 } else {
2090 n = 0;
2091 while (te != *first_displayed_entry)
2092 te = SIMPLEQ_NEXT(te, entry);
2095 while (te) {
2096 char *line = NULL, *id_str = NULL;
2098 if (show_ids) {
2099 err = got_object_id_str(&id_str, te->id);
2100 if (err)
2101 return got_error_from_errno();
2103 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2104 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2105 free(id_str);
2106 return got_error_from_errno();
2108 free(id_str);
2109 err = format_line(&wline, &width, line, view->ncols);
2110 if (err) {
2111 free(line);
2112 break;
2114 if (n == selected) {
2115 wstandout(view->window);
2116 *selected_entry = te;
2118 waddwstr(view->window, wline);
2119 if (width < view->ncols)
2120 waddch(view->window, '\n');
2121 if (n == selected)
2122 wstandend(view->window);
2123 free(line);
2124 free(wline);
2125 wline = NULL;
2126 n++;
2127 (*ndisplayed)++;
2128 *last_displayed_entry = te;
2129 if (--limit <= 0)
2130 break;
2131 te = SIMPLEQ_NEXT(te, entry);
2134 return err;
2137 static void
2138 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2139 const struct got_tree_entries *entries, int isroot)
2141 struct got_tree_entry *te, *prev;
2142 int i;
2144 if (*first_displayed_entry == NULL)
2145 return;
2147 te = SIMPLEQ_FIRST(&entries->head);
2148 if (*first_displayed_entry == te) {
2149 if (!isroot)
2150 *first_displayed_entry = NULL;
2151 return;
2154 /* XXX this is stupid... switch to TAILQ? */
2155 for (i = 0; i < maxscroll; i++) {
2156 while (te != *first_displayed_entry) {
2157 prev = te;
2158 te = SIMPLEQ_NEXT(te, entry);
2160 *first_displayed_entry = prev;
2161 te = SIMPLEQ_FIRST(&entries->head);
2163 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2164 *first_displayed_entry = NULL;
2167 static void
2168 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2169 struct got_tree_entry *last_displayed_entry,
2170 const struct got_tree_entries *entries)
2172 struct got_tree_entry *next;
2173 int n = 0;
2175 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2176 return;
2178 if (*first_displayed_entry)
2179 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2180 else
2181 next = SIMPLEQ_FIRST(&entries->head);
2182 while (next) {
2183 *first_displayed_entry = next;
2184 if (++n >= maxscroll)
2185 break;
2186 next = SIMPLEQ_NEXT(next, entry);
2190 struct tog_parent_tree {
2191 TAILQ_ENTRY(tog_parent_tree) entry;
2192 struct got_tree_object *tree;
2193 struct got_tree_entry *first_displayed_entry;
2194 struct got_tree_entry *selected_entry;
2195 int selected;
2198 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2200 static const struct got_error *
2201 tree_entry_path(char **path, struct tog_parent_trees *parents,
2202 struct got_tree_entry *te)
2204 const struct got_error *err = NULL;
2205 struct tog_parent_tree *pt;
2206 size_t len = 2; /* for leading slash and NUL */
2208 TAILQ_FOREACH(pt, parents, entry)
2209 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2210 if (te)
2211 len += strlen(te->name);
2213 *path = calloc(1, len);
2214 if (path == NULL)
2215 return got_error_from_errno();
2217 (*path)[0] = '/';
2218 pt = TAILQ_LAST(parents, tog_parent_trees);
2219 while (pt) {
2220 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2221 err = got_error(GOT_ERR_NO_SPACE);
2222 goto done;
2224 if (strlcat(*path, "/", len) >= len) {
2225 err = got_error(GOT_ERR_NO_SPACE);
2226 goto done;
2228 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2230 if (te) {
2231 if (strlcat(*path, te->name, len) >= len) {
2232 err = got_error(GOT_ERR_NO_SPACE);
2233 goto done;
2236 done:
2237 if (err) {
2238 free(*path);
2239 *path = NULL;
2241 return err;
2244 static const struct got_error *
2245 blame_tree_entry(struct tog_view *parent_view, struct got_tree_entry *te,
2246 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2247 struct got_repository *repo)
2249 const struct got_error *err = NULL;
2250 char *path;
2251 struct tog_view *view;
2253 err = tree_entry_path(&path, parents, te);
2254 if (err)
2255 return err;
2257 view = open_view(0, 0, 0, 0, parent_view);
2258 if (view) {
2259 err = show_blame_view(view, path, commit_id, repo);
2260 close_view(view);
2261 } else
2262 err = got_error_from_errno();
2264 show_view(parent_view);
2265 free(path);
2266 return err;
2269 static const struct got_error *
2270 log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2271 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2272 struct got_repository *repo)
2274 const struct got_error *err = NULL;
2275 char *path;
2277 err = tree_entry_path(&path, parents, te);
2278 if (err)
2279 return err;
2281 err = show_log_view(view, commit_id, repo, path);
2282 free(path);
2283 return err;
2286 static const struct got_error *
2287 show_tree_view(struct tog_view *view, struct got_tree_object *root,
2288 struct got_object_id *commit_id, struct got_repository *repo)
2290 const struct got_error *err = NULL;
2291 int ch, done = 0, selected = 0, show_ids = 0;
2292 struct got_tree_object *tree = root;
2293 const struct got_tree_entries *entries;
2294 struct got_tree_entry *first_displayed_entry = NULL;
2295 struct got_tree_entry *last_displayed_entry = NULL;
2296 struct got_tree_entry *selected_entry = NULL;
2297 char *commit_id_str = NULL, *tree_label = NULL;
2298 int nentries, ndisplayed;
2299 struct tog_parent_trees parents;
2301 TAILQ_INIT(&parents);
2303 err = got_object_id_str(&commit_id_str, commit_id);
2304 if (err != NULL)
2305 goto done;
2307 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2308 err = got_error_from_errno();
2309 goto done;
2312 show_view(view);
2314 entries = got_object_tree_get_entries(root);
2315 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2316 while (!done) {
2317 char *parent_path;
2318 entries = got_object_tree_get_entries(tree);
2319 nentries = entries->nentries;
2320 if (tree != root)
2321 nentries++; /* '..' directory */
2323 err = tree_entry_path(&parent_path, &parents, NULL);
2324 if (err)
2325 goto done;
2327 err = draw_tree_entries(view, &first_displayed_entry,
2328 &last_displayed_entry, &selected_entry, &ndisplayed,
2329 tree_label, show_ids, parent_path, entries, selected,
2330 view->nlines, tree == root);
2331 free(parent_path);
2332 if (err)
2333 break;
2335 nodelay(stdscr, FALSE);
2336 ch = wgetch(view->window);
2337 nodelay(stdscr, TRUE);
2338 switch (ch) {
2339 case 'q':
2340 done = 1;
2341 break;
2342 case 'i':
2343 show_ids = !show_ids;
2344 break;
2345 case 'l':
2346 if (selected_entry) {
2347 struct tog_view *log_view;
2348 log_view = open_view(0, 0, 0, 0, view);
2349 if (log_view == NULL) {
2350 err = got_error_from_errno();
2351 goto done;
2353 err = log_tree_entry(log_view,
2354 selected_entry, &parents,
2355 commit_id, repo);
2356 close_view(log_view);
2357 show_view(view);
2358 if (err)
2359 goto done;
2361 break;
2362 case 'k':
2363 case KEY_UP:
2364 if (selected > 0)
2365 selected--;
2366 if (selected > 0)
2367 break;
2368 tree_scroll_up(&first_displayed_entry, 1,
2369 entries, tree == root);
2370 break;
2371 case KEY_PPAGE:
2372 if (SIMPLEQ_FIRST(&entries->head) ==
2373 first_displayed_entry) {
2374 if (tree != root)
2375 first_displayed_entry = NULL;
2376 selected = 0;
2377 break;
2379 tree_scroll_up(&first_displayed_entry,
2380 view->nlines, entries, tree == root);
2381 break;
2382 case 'j':
2383 case KEY_DOWN:
2384 if (selected < ndisplayed - 1) {
2385 selected++;
2386 break;
2388 tree_scroll_down(&first_displayed_entry, 1,
2389 last_displayed_entry, entries);
2390 break;
2391 case KEY_NPAGE:
2392 tree_scroll_down(&first_displayed_entry,
2393 view->nlines, last_displayed_entry,
2394 entries);
2395 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2396 break;
2397 /* can't scroll any further; move cursor down */
2398 if (selected < ndisplayed - 1)
2399 selected = ndisplayed - 1;
2400 break;
2401 case KEY_ENTER:
2402 case '\r':
2403 if (selected_entry == NULL) {
2404 struct tog_parent_tree *parent;
2405 case KEY_BACKSPACE:
2406 /* user selected '..' */
2407 if (tree == root)
2408 break;
2409 parent = TAILQ_FIRST(&parents);
2410 TAILQ_REMOVE(&parents, parent, entry);
2411 got_object_tree_close(tree);
2412 tree = parent->tree;
2413 first_displayed_entry =
2414 parent->first_displayed_entry;
2415 selected_entry = parent->selected_entry;
2416 selected = parent->selected;
2417 free(parent);
2418 } else if (S_ISDIR(selected_entry->mode)) {
2419 struct tog_parent_tree *parent;
2420 struct got_tree_object *child;
2421 err = got_object_open_as_tree(
2422 &child, repo, selected_entry->id);
2423 if (err)
2424 goto done;
2425 parent = calloc(1, sizeof(*parent));
2426 if (parent == NULL) {
2427 err = got_error_from_errno();
2428 goto done;
2430 parent->tree = tree;
2431 parent->first_displayed_entry =
2432 first_displayed_entry;
2433 parent->selected_entry = selected_entry;
2434 parent->selected = selected;
2435 TAILQ_INSERT_HEAD(&parents, parent,
2436 entry);
2437 tree = child;
2438 selected = 0;
2439 first_displayed_entry = NULL;
2440 } else if (S_ISREG(selected_entry->mode)) {
2441 err = blame_tree_entry(view,
2442 selected_entry, &parents,
2443 commit_id, repo);
2444 if (err)
2445 goto done;
2447 break;
2448 case KEY_RESIZE:
2449 err = view_resize(view);
2450 if (err)
2451 goto done;
2452 if (selected > view->nlines)
2453 selected = ndisplayed - 1;
2454 break;
2455 default:
2456 break;
2459 done:
2460 free(tree_label);
2461 free(commit_id_str);
2462 while (!TAILQ_EMPTY(&parents)) {
2463 struct tog_parent_tree *parent;
2464 parent = TAILQ_FIRST(&parents);
2465 TAILQ_REMOVE(&parents, parent, entry);
2466 free(parent);
2469 if (tree != root)
2470 got_object_tree_close(tree);
2471 return err;
2474 __dead static void
2475 usage_tree(void)
2477 endwin();
2478 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2479 getprogname());
2480 exit(1);
2483 static const struct got_error *
2484 cmd_tree(int argc, char *argv[])
2486 const struct got_error *error;
2487 struct got_repository *repo = NULL;
2488 char *repo_path = NULL;
2489 struct got_object_id *commit_id = NULL;
2490 char *commit_id_arg = NULL;
2491 struct got_commit_object *commit = NULL;
2492 struct got_tree_object *tree = NULL;
2493 int ch;
2494 struct tog_view *view;
2496 #ifndef PROFILE
2497 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2498 err(1, "pledge");
2499 #endif
2501 while ((ch = getopt(argc, argv, "c:")) != -1) {
2502 switch (ch) {
2503 case 'c':
2504 commit_id_arg = optarg;
2505 break;
2506 default:
2507 usage();
2508 /* NOTREACHED */
2512 argc -= optind;
2513 argv += optind;
2515 if (argc == 0) {
2516 repo_path = getcwd(NULL, 0);
2517 if (repo_path == NULL)
2518 return got_error_from_errno();
2519 } else if (argc == 1) {
2520 repo_path = realpath(argv[0], NULL);
2521 if (repo_path == NULL)
2522 return got_error_from_errno();
2523 } else
2524 usage_log();
2526 error = got_repo_open(&repo, repo_path);
2527 free(repo_path);
2528 if (error != NULL)
2529 return error;
2531 if (commit_id_arg == NULL) {
2532 error = get_head_commit_id(&commit_id, repo);
2533 if (error != NULL)
2534 goto done;
2535 } else {
2536 struct got_object *obj;
2537 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2538 if (error == NULL) {
2539 commit_id = got_object_get_id(obj);
2540 if (commit_id == NULL)
2541 error = got_error_from_errno();
2544 if (error != NULL)
2545 goto done;
2547 error = got_object_open_as_commit(&commit, repo, commit_id);
2548 if (error != NULL)
2549 goto done;
2551 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2552 if (error != NULL)
2553 goto done;
2555 view = open_view(0, 0, 0, 0, NULL);
2556 if (view == NULL) {
2557 error = got_error_from_errno();
2558 goto done;
2560 error = show_tree_view(view, tree, commit_id, repo);
2561 close_view(view);
2562 done:
2563 free(commit_id);
2564 if (commit)
2565 got_object_commit_close(commit);
2566 if (tree)
2567 got_object_tree_close(tree);
2568 if (repo)
2569 got_repo_close(repo);
2570 return error;
2572 static void
2573 init_curses(void)
2575 initscr();
2576 cbreak();
2577 noecho();
2578 nonl();
2579 intrflush(stdscr, FALSE);
2580 keypad(stdscr, TRUE);
2581 curs_set(0);
2584 __dead static void
2585 usage(void)
2587 int i;
2589 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2590 "Available commands:\n", getprogname());
2591 for (i = 0; i < nitems(tog_commands); i++) {
2592 struct tog_cmd *cmd = &tog_commands[i];
2593 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2595 exit(1);
2598 static char **
2599 make_argv(const char *arg0, const char *arg1)
2601 char **argv;
2602 int argc = (arg1 == NULL ? 1 : 2);
2604 argv = calloc(argc, sizeof(char *));
2605 if (argv == NULL)
2606 err(1, "calloc");
2607 argv[0] = strdup(arg0);
2608 if (argv[0] == NULL)
2609 err(1, "calloc");
2610 if (arg1) {
2611 argv[1] = strdup(arg1);
2612 if (argv[1] == NULL)
2613 err(1, "calloc");
2616 return argv;
2619 int
2620 main(int argc, char *argv[])
2622 const struct got_error *error = NULL;
2623 struct tog_cmd *cmd = NULL;
2624 int ch, hflag = 0;
2625 char **cmd_argv = NULL;
2627 setlocale(LC_ALL, "");
2629 while ((ch = getopt(argc, argv, "h")) != -1) {
2630 switch (ch) {
2631 case 'h':
2632 hflag = 1;
2633 break;
2634 default:
2635 usage();
2636 /* NOTREACHED */
2640 argc -= optind;
2641 argv += optind;
2642 optind = 0;
2643 optreset = 1;
2645 if (argc == 0) {
2646 if (hflag)
2647 usage();
2648 /* Build an argument vector which runs a default command. */
2649 cmd = &tog_commands[0];
2650 cmd_argv = make_argv(cmd->name, NULL);
2651 argc = 1;
2652 } else {
2653 int i;
2655 /* Did the user specific a command? */
2656 for (i = 0; i < nitems(tog_commands); i++) {
2657 if (strncmp(tog_commands[i].name, argv[0],
2658 strlen(argv[0])) == 0) {
2659 cmd = &tog_commands[i];
2660 if (hflag)
2661 tog_commands[i].cmd_usage();
2662 break;
2665 if (cmd == NULL) {
2666 /* Did the user specify a repository? */
2667 char *repo_path = realpath(argv[0], NULL);
2668 if (repo_path) {
2669 struct got_repository *repo;
2670 error = got_repo_open(&repo, repo_path);
2671 if (error == NULL)
2672 got_repo_close(repo);
2673 } else
2674 error = got_error_from_errno();
2675 if (error) {
2676 if (hflag) {
2677 fprintf(stderr, "%s: '%s' is not a "
2678 "known command\n", getprogname(),
2679 argv[0]);
2680 usage();
2682 fprintf(stderr, "%s: '%s' is neither a known "
2683 "command nor a path to a repository\n",
2684 getprogname(), argv[0]);
2685 free(repo_path);
2686 return 1;
2688 cmd = &tog_commands[0];
2689 cmd_argv = make_argv(cmd->name, repo_path);
2690 argc = 2;
2691 free(repo_path);
2695 init_curses();
2697 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2698 if (error)
2699 goto done;
2700 done:
2701 endwin();
2702 free(cmd_argv);
2703 if (error)
2704 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2705 return 0;