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 static struct tog_view {
86 WINDOW *window;
87 PANEL *panel;
88 } tog_log_view, tog_diff_view, tog_blame_view, tog_tree_view;
90 static const struct got_error *
91 show_diff_view(struct got_object *, struct got_object *,
92 struct got_repository *);
93 static const struct got_error *
94 show_log_view(struct got_object_id *, struct got_repository *);
95 static const struct got_error *
96 show_blame_view(const char *, struct got_object_id *, struct got_repository *);
97 static const struct got_error *
98 show_tree_view(struct got_tree_object *, struct got_object_id *,
99 struct got_repository *);
101 __dead static void
102 usage_log(void)
104 endwin();
105 fprintf(stderr, "usage: %s log [-c commit] [repository-path]\n",
106 getprogname());
107 exit(1);
110 /* Create newly allocated wide-character string equivalent to a byte string. */
111 static const struct got_error *
112 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
114 char *vis = NULL;
115 const struct got_error *err = NULL;
117 *ws = NULL;
118 *wlen = mbstowcs(NULL, s, 0);
119 if (*wlen == (size_t)-1) {
120 int vislen;
121 if (errno != EILSEQ)
122 return got_error_from_errno();
124 /* byte string invalid in current encoding; try to "fix" it */
125 err = got_mbsavis(&vis, &vislen, s);
126 if (err)
127 return err;
128 *wlen = mbstowcs(NULL, vis, 0);
129 if (*wlen == (size_t)-1) {
130 err = got_error_from_errno(); /* give up */
131 goto done;
135 *ws = calloc(*wlen + 1, sizeof(*ws));
136 if (*ws == NULL) {
137 err = got_error_from_errno();
138 goto done;
141 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
142 err = got_error_from_errno();
143 done:
144 free(vis);
145 if (err) {
146 free(*ws);
147 *ws = NULL;
148 *wlen = 0;
150 return err;
153 /* Format a line for display, ensuring that it won't overflow a width limit. */
154 static const struct got_error *
155 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
157 const struct got_error *err = NULL;
158 int cols = 0;
159 wchar_t *wline = NULL;
160 size_t wlen;
161 int i;
163 *wlinep = NULL;
165 err = mbs2ws(&wline, &wlen, line);
166 if (err)
167 return err;
169 i = 0;
170 while (i < wlen && cols <= wlimit) {
171 int width = wcwidth(wline[i]);
172 switch (width) {
173 case 0:
174 break;
175 case 1:
176 case 2:
177 cols += width;
178 break;
179 case -1:
180 if (wline[i] == L'\t')
181 cols += TABSIZE - (cols % TABSIZE);
182 break;
183 default:
184 err = got_error_from_errno();
185 goto done;
187 if (cols <= COLS) {
188 i++;
189 if (widthp)
190 *widthp = cols;
193 wline[i] = L'\0';
194 done:
195 if (err)
196 free(wline);
197 else
198 *wlinep = wline;
199 return err;
202 static const struct got_error *
203 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
205 const struct got_error *err = NULL;
206 char *logmsg0 = NULL, *logmsg = NULL;
207 char *author0 = NULL, *author = NULL;
208 wchar_t *wlogmsg = NULL, *wauthor = NULL;
209 int author_width, logmsg_width;
210 char *newline, *smallerthan;
211 char *line = NULL;
212 char *id_str = NULL;
213 size_t id_len;
214 int col, limit;
215 static const size_t id_display_cols = 8;
216 static const size_t author_display_cols = 16;
217 const int avail = COLS;
219 err = got_object_id_str(&id_str, id);
220 if (err)
221 return err;
222 id_len = strlen(id_str);
223 if (avail < id_display_cols) {
224 limit = MIN(id_len, avail);
225 waddnstr(tog_log_view.window, id_str, limit);
226 } else {
227 limit = MIN(id_display_cols, id_len);
228 waddnstr(tog_log_view.window, id_str, limit);
230 col = limit + 1;
231 while (col <= avail && col < id_display_cols + 2) {
232 waddch(tog_log_view.window, ' ');
233 col++;
235 if (col > avail)
236 goto done;
238 author0 = strdup(commit->author);
239 if (author0 == NULL) {
240 err = got_error_from_errno();
241 goto done;
243 author = author0;
244 smallerthan = strchr(author, '<');
245 if (smallerthan)
246 *smallerthan = '\0';
247 else {
248 char *at = strchr(author, '@');
249 if (at)
250 *at = '\0';
252 limit = MIN(avail, author_display_cols);
253 err = format_line(&wauthor, &author_width, author, limit);
254 if (err)
255 goto done;
256 waddwstr(tog_log_view.window, wauthor);
257 col += author_width;
258 while (col <= avail && author_width < author_display_cols + 1) {
259 waddch(tog_log_view.window, ' ');
260 col++;
261 author_width++;
263 if (col > avail)
264 goto done;
266 logmsg0 = strdup(commit->logmsg);
267 if (logmsg0 == NULL) {
268 err = got_error_from_errno();
269 goto done;
271 logmsg = logmsg0;
272 while (*logmsg == '\n')
273 logmsg++;
274 newline = strchr(logmsg, '\n');
275 if (newline)
276 *newline = '\0';
277 limit = avail - col;
278 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
279 if (err)
280 goto done;
281 waddwstr(tog_log_view.window, wlogmsg);
282 col += logmsg_width;
283 while (col <= avail) {
284 waddch(tog_log_view.window, ' ');
285 col++;
287 done:
288 free(logmsg0);
289 free(wlogmsg);
290 free(author0);
291 free(wauthor);
292 free(line);
293 free(id_str);
294 return err;
297 struct commit_queue_entry {
298 TAILQ_ENTRY(commit_queue_entry) entry;
299 struct got_object_id *id;
300 struct got_commit_object *commit;
301 };
302 TAILQ_HEAD(commit_queue, commit_queue_entry);
304 static struct commit_queue_entry *
305 alloc_commit_queue_entry(struct got_commit_object *commit,
306 struct got_object_id *id)
308 struct commit_queue_entry *entry;
310 entry = calloc(1, sizeof(*entry));
311 if (entry == NULL)
312 return NULL;
314 entry->id = id;
315 entry->commit = commit;
316 return entry;
319 static void
320 pop_commit(struct commit_queue *commits)
322 struct commit_queue_entry *entry;
324 entry = TAILQ_FIRST(commits);
325 TAILQ_REMOVE(commits, entry, entry);
326 got_object_commit_close(entry->commit);
327 /* Don't free entry->id! It is owned by the commit graph. */
328 free(entry);
331 static void
332 free_commits(struct commit_queue *commits)
334 while (!TAILQ_EMPTY(commits))
335 pop_commit(commits);
338 static const struct got_error *
339 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
340 struct got_object_id *start_id, struct got_repository *repo)
342 const struct got_error *err = NULL;
343 struct got_object_id *id;
344 struct commit_queue_entry *entry;
346 err = got_commit_graph_iter_start(graph, start_id);
347 if (err)
348 return err;
350 entry = TAILQ_LAST(commits, commit_queue);
351 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
352 int nfetched;
354 /* Start ID's commit is already on the queue; skip over it. */
355 err = got_commit_graph_iter_next(&id, graph);
356 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
357 return err;
359 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
360 if (err)
361 return err;
364 while (1) {
365 struct got_commit_object *commit;
367 err = got_commit_graph_iter_next(&id, graph);
368 if (err) {
369 if (err->code == GOT_ERR_ITER_NEED_MORE)
370 err = NULL;
371 break;
374 err = got_object_open_as_commit(&commit, repo, id);
375 if (err)
376 break;
378 entry = alloc_commit_queue_entry(commit, id);
379 if (entry == NULL) {
380 err = got_error_from_errno();
381 break;
384 TAILQ_INSERT_TAIL(commits, entry, entry);
387 return err;
390 static const struct got_error *
391 fetch_next_commit(struct commit_queue_entry **pentry,
392 struct commit_queue_entry *entry, struct commit_queue *commits,
393 struct got_commit_graph *graph, struct got_repository *repo)
395 const struct got_error *err = NULL;
396 struct got_object_qid *qid;
398 *pentry = NULL;
400 /* Populate commit graph with entry's parent commits. */
401 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
402 int nfetched;
403 err = got_commit_graph_fetch_commits_up_to(&nfetched,
404 graph, qid->id, repo);
405 if (err)
406 return err;
409 /* Append outstanding commits to queue in graph sort order. */
410 err = queue_commits(graph, commits, entry->id, repo);
411 if (err) {
412 if (err->code == GOT_ERR_ITER_COMPLETED)
413 err = NULL;
414 return err;
417 /* Next entry to display should now be available. */
418 *pentry = TAILQ_NEXT(entry, entry);
419 if (*pentry == NULL)
420 return got_error(GOT_ERR_NO_OBJ);
422 return NULL;
425 static const struct got_error *
426 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
428 const struct got_error *err = NULL;
429 struct got_reference *head_ref;
431 *head_id = NULL;
433 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
434 if (err)
435 return err;
437 err = got_ref_resolve(head_id, repo, head_ref);
438 got_ref_close(head_ref);
439 if (err) {
440 *head_id = NULL;
441 return err;
444 return NULL;
447 static const struct got_error *
448 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
449 struct commit_queue_entry *first, int selected_idx, int limit)
451 const struct got_error *err = NULL;
452 struct commit_queue_entry *entry;
453 int ncommits = 0;
455 werase(tog_log_view.window);
457 entry = first;
458 *last = first;
459 while (entry) {
460 if (ncommits == limit)
461 break;
462 if (ncommits == selected_idx) {
463 wstandout(tog_log_view.window);
464 *selected = entry;
466 err = draw_commit(entry->commit, entry->id);
467 if (ncommits == selected_idx)
468 wstandend(tog_log_view.window);
469 if (err)
470 break;
471 ncommits++;
472 *last = entry;
473 entry = TAILQ_NEXT(entry, entry);
476 update_panels();
477 doupdate();
479 return err;
482 static void
483 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
484 struct commit_queue *commits)
486 struct commit_queue_entry *entry;
487 int nscrolled = 0;
489 entry = TAILQ_FIRST(commits);
490 if (*first_displayed_entry == entry)
491 return;
493 entry = *first_displayed_entry;
494 while (entry && nscrolled < maxscroll) {
495 entry = TAILQ_PREV(entry, commit_queue, entry);
496 if (entry) {
497 *first_displayed_entry = entry;
498 nscrolled++;
503 static const struct got_error *
504 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
505 struct commit_queue_entry *last_displayed_entry,
506 struct commit_queue *commits, struct got_commit_graph *graph,
507 struct got_repository *repo)
509 const struct got_error *err = NULL;
510 struct commit_queue_entry *pentry;
511 int nscrolled = 0;
513 do {
514 pentry = TAILQ_NEXT(last_displayed_entry, entry);
515 if (pentry == NULL) {
516 err = fetch_next_commit(&pentry, last_displayed_entry,
517 commits, graph, repo);
518 if (err || pentry == NULL)
519 break;
521 last_displayed_entry = pentry;
523 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
524 if (pentry == NULL)
525 break;
526 *first_displayed_entry = pentry;
527 } while (++nscrolled < maxscroll);
529 return err;
532 static int
533 num_parents(struct commit_queue_entry *entry)
535 int nparents = 0;
537 while (entry) {
538 entry = TAILQ_NEXT(entry, entry);
539 nparents++;
542 return nparents;
545 static const struct got_error *
546 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
548 const struct got_error *err;
549 struct got_object *obj1 = NULL, *obj2 = NULL;
550 struct got_object_qid *parent_id;
552 err = got_object_open(&obj2, repo, entry->id);
553 if (err)
554 return err;
556 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
557 if (parent_id) {
558 err = got_object_open(&obj1, repo, parent_id->id);
559 if (err)
560 goto done;
563 err = show_diff_view(obj1, obj2, repo);
564 done:
565 if (obj1)
566 got_object_close(obj1);
567 if (obj2)
568 got_object_close(obj2);
569 return err;
572 static const struct got_error *
573 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
575 const struct got_error *err = NULL;
576 struct got_tree_object *tree;
578 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
579 if (err)
580 return err;
582 err = show_tree_view(tree, entry->id, repo);
583 got_object_tree_close(tree);
584 return err;
587 static const struct got_error *
588 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
590 const struct got_error *err = NULL;
591 struct got_object_id *head_id = NULL;
592 int ch, done = 0, selected = 0, nparents, nfetched;
593 struct got_commit_graph *graph;
594 struct commit_queue commits;
595 struct commit_queue_entry *entry = NULL;
596 struct commit_queue_entry *first_displayed_entry = NULL;
597 struct commit_queue_entry *last_displayed_entry = NULL;
598 struct commit_queue_entry *selected_entry = NULL;
600 if (tog_log_view.window == NULL) {
601 tog_log_view.window = newwin(0, 0, 0, 0);
602 if (tog_log_view.window == NULL)
603 return got_error_from_errno();
604 keypad(tog_log_view.window, TRUE);
606 if (tog_log_view.panel == NULL) {
607 tog_log_view.panel = new_panel(tog_log_view.window);
608 if (tog_log_view.panel == NULL)
609 return got_error_from_errno();
610 } else
611 show_panel(tog_log_view.panel);
613 err = get_head_commit_id(&head_id, repo);
614 if (err)
615 return err;
617 TAILQ_INIT(&commits);
619 err = got_commit_graph_open(&graph, head_id, 0, repo);
620 if (err)
621 goto done;
623 /* Populate commit graph with a sufficient number of commits. */
624 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
625 repo);
626 if (err)
627 goto done;
628 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
629 if (err)
630 goto done;
632 /*
633 * Open the initial batch of commits, sorted in commit graph order.
634 * We keep all commits open throughout the lifetime of the log view
635 * in order to avoid having to re-fetch commits from disk while
636 * updating the display.
637 */
638 err = queue_commits(graph, &commits, head_id, repo);
639 if (err && err->code != GOT_ERR_ITER_COMPLETED)
640 goto done;
642 /* Find entry corresponding to the first commit to display. */
643 TAILQ_FOREACH(entry, &commits, entry) {
644 if (got_object_id_cmp(entry->id, start_id) == 0) {
645 first_displayed_entry = entry;
646 break;
649 if (first_displayed_entry == NULL) {
650 err = got_error(GOT_ERR_NO_OBJ);
651 goto done;
654 while (!done) {
655 err = draw_commits(&last_displayed_entry, &selected_entry,
656 first_displayed_entry, selected, LINES);
657 if (err)
658 goto done;
660 nodelay(stdscr, FALSE);
661 ch = wgetch(tog_log_view.window);
662 nodelay(stdscr, TRUE);
663 switch (ch) {
664 case ERR:
665 if (errno) {
666 err = got_error_from_errno();
667 goto done;
669 break;
670 case 'q':
671 done = 1;
672 break;
673 case 'k':
674 case KEY_UP:
675 if (selected > 0)
676 selected--;
677 if (selected > 0)
678 break;
679 scroll_up(&first_displayed_entry, 1, &commits);
680 break;
681 case KEY_PPAGE:
682 if (TAILQ_FIRST(&commits) ==
683 first_displayed_entry) {
684 selected = 0;
685 break;
687 scroll_up(&first_displayed_entry, LINES,
688 &commits);
689 break;
690 case 'j':
691 case KEY_DOWN:
692 nparents = num_parents(first_displayed_entry);
693 if (selected < LINES - 1 &&
694 selected < nparents - 1) {
695 selected++;
696 break;
698 err = scroll_down(&first_displayed_entry, 1,
699 last_displayed_entry, &commits, graph,
700 repo);
701 if (err)
702 goto done;
703 break;
704 case KEY_NPAGE:
705 err = scroll_down(&first_displayed_entry, LINES,
706 last_displayed_entry, &commits, graph,
707 repo);
708 if (err)
709 goto done;
710 if (last_displayed_entry->commit->nparents > 0)
711 break;
712 /* can't scroll any further; move cursor down */
713 nparents = num_parents(first_displayed_entry);
714 if (selected < LINES - 1 ||
715 selected < nparents - 1)
716 selected = MIN(LINES - 1, nparents - 1);
717 break;
718 case KEY_RESIZE:
719 if (selected > LINES)
720 selected = LINES - 1;
721 break;
722 case KEY_ENTER:
723 case '\r':
724 err = show_commit(selected_entry, repo);
725 if (err)
726 goto done;
727 show_panel(tog_log_view.panel);
728 break;
729 case 't':
730 err = browse_commit(selected_entry, repo);
731 if (err)
732 goto done;
733 show_panel(tog_log_view.panel);
734 break;
735 default:
736 break;
739 done:
740 free(head_id);
741 if (graph)
742 got_commit_graph_close(graph);
743 free_commits(&commits);
744 return err;
747 static const struct got_error *
748 cmd_log(int argc, char *argv[])
750 const struct got_error *error;
751 struct got_repository *repo;
752 struct got_object_id *start_id = NULL;
753 char *repo_path = NULL;
754 char *start_commit = NULL;
755 int ch;
757 #ifndef PROFILE
758 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
759 err(1, "pledge");
760 #endif
762 while ((ch = getopt(argc, argv, "c:")) != -1) {
763 switch (ch) {
764 case 'c':
765 start_commit = optarg;
766 break;
767 default:
768 usage();
769 /* NOTREACHED */
773 argc -= optind;
774 argv += optind;
776 if (argc == 0) {
777 repo_path = getcwd(NULL, 0);
778 if (repo_path == NULL)
779 return got_error_from_errno();
780 } else if (argc == 1) {
781 repo_path = realpath(argv[0], NULL);
782 if (repo_path == NULL)
783 return got_error_from_errno();
784 } else
785 usage_log();
787 error = got_repo_open(&repo, repo_path);
788 free(repo_path);
789 if (error != NULL)
790 return error;
792 if (start_commit == NULL) {
793 error = get_head_commit_id(&start_id, repo);
794 if (error != NULL)
795 return error;
796 } else {
797 struct got_object *obj;
798 error = got_object_open_by_id_str(&obj, repo, start_commit);
799 if (error == NULL) {
800 start_id = got_object_get_id(obj);
801 if (start_id == NULL)
802 error = got_error_from_errno();
805 if (error != NULL)
806 return error;
807 error = show_log_view(start_id, repo);
808 free(start_id);
809 got_repo_close(repo);
810 return error;
813 __dead static void
814 usage_diff(void)
816 endwin();
817 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
818 getprogname());
819 exit(1);
822 static char *
823 parse_next_line(FILE *f, size_t *len)
825 char *line;
826 size_t linelen;
827 size_t lineno;
828 const char delim[3] = { '\0', '\0', '\0'};
830 line = fparseln(f, &linelen, &lineno, delim, 0);
831 if (len)
832 *len = linelen;
833 return line;
836 static const struct got_error *
837 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
838 int *last_displayed_line, int *eof, int max_lines)
840 const struct got_error *err;
841 int nlines = 0, nprinted = 0;
842 char *line;
843 size_t len;
844 wchar_t *wline;
845 int width;
847 rewind(f);
848 werase(window);
850 *eof = 0;
851 while (nprinted < max_lines) {
852 line = parse_next_line(f, &len);
853 if (line == NULL) {
854 *eof = 1;
855 break;
857 if (++nlines < *first_displayed_line) {
858 free(line);
859 continue;
862 err = format_line(&wline, &width, line, COLS);
863 if (err) {
864 free(line);
865 return err;
867 waddwstr(window, wline);
868 if (width < COLS)
869 waddch(window, '\n');
870 if (++nprinted == 1)
871 *first_displayed_line = nlines;
872 free(line);
874 *last_displayed_line = nlines;
876 update_panels();
877 doupdate();
879 return NULL;
882 static const struct got_error *
883 show_diff_view(struct got_object *obj1, struct got_object *obj2,
884 struct got_repository *repo)
886 const struct got_error *err;
887 FILE *f;
888 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
889 int eof, i;
891 if (obj1 != NULL && obj2 != NULL &&
892 got_object_get_type(obj1) != got_object_get_type(obj2))
893 return got_error(GOT_ERR_OBJ_TYPE);
895 f = got_opentemp();
896 if (f == NULL)
897 return got_error_from_errno();
899 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
900 case GOT_OBJ_TYPE_BLOB:
901 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
902 break;
903 case GOT_OBJ_TYPE_TREE:
904 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
905 break;
906 case GOT_OBJ_TYPE_COMMIT:
907 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
908 break;
909 default:
910 return got_error(GOT_ERR_OBJ_TYPE);
913 fflush(f);
915 if (tog_diff_view.window == NULL) {
916 tog_diff_view.window = newwin(0, 0, 0, 0);
917 if (tog_diff_view.window == NULL)
918 return got_error_from_errno();
919 keypad(tog_diff_view.window, TRUE);
921 if (tog_diff_view.panel == NULL) {
922 tog_diff_view.panel = new_panel(tog_diff_view.window);
923 if (tog_diff_view.panel == NULL)
924 return got_error_from_errno();
925 } else
926 show_panel(tog_diff_view.panel);
928 while (!done) {
929 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
930 &last_displayed_line, &eof, LINES);
931 if (err)
932 break;
933 nodelay(stdscr, FALSE);
934 ch = wgetch(tog_diff_view.window);
935 nodelay(stdscr, TRUE);
936 switch (ch) {
937 case 'q':
938 done = 1;
939 break;
940 case 'k':
941 case KEY_UP:
942 case KEY_BACKSPACE:
943 if (first_displayed_line > 1)
944 first_displayed_line--;
945 break;
946 case KEY_PPAGE:
947 i = 0;
948 while (i++ < LINES - 1 &&
949 first_displayed_line > 1)
950 first_displayed_line--;
951 break;
952 case 'j':
953 case KEY_DOWN:
954 case KEY_ENTER:
955 case '\r':
956 if (!eof)
957 first_displayed_line++;
958 break;
959 case KEY_NPAGE:
960 case ' ':
961 i = 0;
962 while (!eof && i++ < LINES - 1) {
963 char *line = parse_next_line(f, NULL);
964 first_displayed_line++;
965 if (line == NULL)
966 break;
968 break;
969 default:
970 break;
973 fclose(f);
974 return err;
977 static const struct got_error *
978 cmd_diff(int argc, char *argv[])
980 const struct got_error *error = NULL;
981 struct got_repository *repo = NULL;
982 struct got_object *obj1 = NULL, *obj2 = NULL;
983 char *repo_path = NULL;
984 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
985 int ch;
987 #ifndef PROFILE
988 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
989 err(1, "pledge");
990 #endif
992 while ((ch = getopt(argc, argv, "")) != -1) {
993 switch (ch) {
994 default:
995 usage();
996 /* NOTREACHED */
1000 argc -= optind;
1001 argv += optind;
1003 if (argc == 0) {
1004 usage_diff(); /* TODO show local worktree changes */
1005 } else if (argc == 2) {
1006 repo_path = getcwd(NULL, 0);
1007 if (repo_path == NULL)
1008 return got_error_from_errno();
1009 obj_id_str1 = argv[0];
1010 obj_id_str2 = argv[1];
1011 } else if (argc == 3) {
1012 repo_path = realpath(argv[0], NULL);
1013 if (repo_path == NULL)
1014 return got_error_from_errno();
1015 obj_id_str1 = argv[1];
1016 obj_id_str2 = argv[2];
1017 } else
1018 usage_diff();
1020 error = got_repo_open(&repo, repo_path);
1021 free(repo_path);
1022 if (error)
1023 goto done;
1025 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1026 if (error)
1027 goto done;
1029 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1030 if (error)
1031 goto done;
1033 error = show_diff_view(obj1, obj2, repo);
1034 done:
1035 got_repo_close(repo);
1036 if (obj1)
1037 got_object_close(obj1);
1038 if (obj2)
1039 got_object_close(obj2);
1040 return error;
1043 __dead static void
1044 usage_blame(void)
1046 endwin();
1047 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1048 getprogname());
1049 exit(1);
1052 struct tog_blame_line {
1053 int annotated;
1054 struct got_object_id *id;
1057 static const struct got_error *
1058 draw_blame(WINDOW *window, FILE *f, struct tog_blame_line *lines, int nlines,
1059 int *first_displayed_line, int *last_displayed_line, int *eof,
1060 int max_lines)
1062 const struct got_error *err;
1063 int lineno = 0, nprinted = 0;
1064 char *line;
1065 size_t len;
1066 wchar_t *wline;
1067 int width;
1068 struct tog_blame_line *blame_line;
1070 rewind(f);
1071 werase(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 (++lineno < *first_displayed_line) {
1081 free(line);
1082 continue;
1085 err = format_line(&wline, &width, line, COLS - 9);
1086 if (err) {
1087 free(line);
1088 return err;
1091 blame_line = &lines[lineno - 1];
1092 if (blame_line->annotated) {
1093 char *id_str;
1094 err = got_object_id_str(&id_str, blame_line->id);
1095 if (err) {
1096 free(line);
1097 return err;
1099 wprintw(window, "%.8s ", id_str);
1100 free(id_str);
1101 } else
1102 waddstr(window, " ");
1104 waddwstr(window, wline);
1105 if (width < COLS - 9)
1106 waddch(window, '\n');
1107 if (++nprinted == 1)
1108 *first_displayed_line = lineno;
1109 free(line);
1111 *last_displayed_line = lineno;
1113 update_panels();
1114 doupdate();
1116 return NULL;
1119 struct tog_blame_cb_args {
1120 pthread_mutex_t *mutex;
1121 struct tog_blame_line *lines; /* one per line */
1122 int nlines;
1124 FILE *f;
1125 WINDOW *window;
1126 int *first_displayed_line;
1127 int *last_displayed_line;
1128 int *done;
1131 static const struct got_error *
1132 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1134 const struct got_error *err = NULL;
1135 struct tog_blame_cb_args *a = arg;
1136 struct tog_blame_line *line;
1137 int eof;
1139 if (nlines != a->nlines ||
1140 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1141 return got_error(GOT_ERR_RANGE);
1143 if (pthread_mutex_lock(a->mutex) != 0)
1144 return got_error_from_errno();
1146 if (*a->done) { /* user has quit the blame view */
1147 err = got_error(GOT_ERR_ITER_COMPLETED);
1148 goto done;
1151 if (lineno == -1)
1152 goto done; /* no change in this commit */
1154 line = &a->lines[lineno - 1];
1155 if (line->annotated)
1156 goto done;
1158 line->id = got_object_id_dup(id);
1159 if (line->id == NULL) {
1160 err = got_error_from_errno();
1161 goto done;
1163 line->annotated = 1;
1165 err = draw_blame(a->window, a->f, a->lines, a->nlines,
1166 a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1167 done:
1168 if (pthread_mutex_unlock(a->mutex) != 0)
1169 return got_error_from_errno();
1170 return err;
1173 struct tog_blame_thread_args {
1174 const char *path;
1175 struct got_object_id *commit_id;
1176 struct got_repository *repo;
1177 void *blame_cb_args;
1180 static void *
1181 blame_thread(void *arg)
1183 struct tog_blame_thread_args *a = arg;
1184 return (void *)got_blame_incremental(a->path, a->commit_id, a->repo,
1185 blame_cb, a->blame_cb_args);
1188 static const struct got_error *
1189 show_blame_view(const char *path, struct got_object_id *commit_id,
1190 struct got_repository *repo)
1192 const struct got_error *err = NULL;
1193 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1194 int eof, i;
1195 struct got_object *obj = NULL;
1196 struct got_blob_object *blob = NULL;
1197 FILE *f = NULL;
1198 size_t filesize, nlines = 0;
1199 struct tog_blame_line *lines = NULL;
1200 pthread_t thread = NULL;
1201 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1202 struct tog_blame_cb_args blame_cb_args;
1203 struct tog_blame_thread_args blame_thread_args;
1205 err = got_object_open_by_path(&obj, repo, commit_id, path);
1206 if (err)
1207 goto done;
1208 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1209 err = got_error(GOT_ERR_OBJ_TYPE);
1210 got_object_close(obj);
1211 goto done;
1214 err = got_object_blob_open(&blob, repo, obj, 8192);
1215 got_object_close(obj);
1216 if (err)
1217 goto done;
1218 f = got_opentemp();
1219 if (f == NULL) {
1220 err = got_error_from_errno();
1221 goto done;
1223 err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1224 if (err)
1225 goto done;
1227 lines = calloc(nlines, sizeof(*lines));
1228 if (lines == NULL) {
1229 err = got_error_from_errno();
1230 goto done;
1233 if (tog_blame_view.window == NULL) {
1234 tog_blame_view.window = newwin(0, 0, 0, 0);
1235 if (tog_blame_view.window == NULL)
1236 return got_error_from_errno();
1237 keypad(tog_blame_view.window, TRUE);
1239 if (tog_blame_view.panel == NULL) {
1240 tog_blame_view.panel = new_panel(tog_blame_view.window);
1241 if (tog_blame_view.panel == NULL)
1242 return got_error_from_errno();
1243 } else
1244 show_panel(tog_blame_view.panel);
1246 if (pthread_mutex_init(&mutex, NULL) != 0) {
1247 err = got_error_from_errno();
1248 goto done;
1250 blame_cb_args.lines = lines;
1251 blame_cb_args.nlines = nlines;
1252 blame_cb_args.mutex = &mutex;
1253 blame_cb_args.f = f;
1254 blame_cb_args.window = tog_blame_view.window;
1255 blame_cb_args.first_displayed_line = &first_displayed_line;
1256 blame_cb_args.last_displayed_line = &last_displayed_line;
1257 blame_cb_args.done = &done;
1259 blame_thread_args.path = path;
1260 blame_thread_args.commit_id = commit_id;
1261 blame_thread_args.repo = repo;
1262 blame_thread_args.blame_cb_args = &blame_cb_args;
1264 if (pthread_create(&thread, NULL, blame_thread,
1265 &blame_thread_args) != 0) {
1266 err = got_error_from_errno();
1267 goto done;
1270 while (!done) {
1271 if (pthread_mutex_lock(&mutex) != 0) {
1272 err = got_error_from_errno();
1273 goto done;
1275 err = draw_blame(tog_blame_view.window, f, lines, nlines,
1276 &first_displayed_line, &last_displayed_line, &eof, LINES);
1277 if (pthread_mutex_unlock(&mutex) != 0) {
1278 err = got_error_from_errno();
1279 goto done;
1281 if (err)
1282 break;
1283 nodelay(stdscr, FALSE);
1284 ch = wgetch(tog_blame_view.window);
1285 nodelay(stdscr, TRUE);
1286 if (pthread_mutex_lock(&mutex) != 0) {
1287 err = got_error_from_errno();
1288 goto done;
1290 switch (ch) {
1291 case 'q':
1292 done = 1;
1293 break;
1294 case 'k':
1295 case KEY_UP:
1296 case KEY_BACKSPACE:
1297 if (first_displayed_line > 1)
1298 first_displayed_line--;
1299 break;
1300 case KEY_PPAGE:
1301 i = 0;
1302 while (i++ < LINES - 1 &&
1303 first_displayed_line > 1)
1304 first_displayed_line--;
1305 break;
1306 case 'j':
1307 case KEY_DOWN:
1308 case KEY_ENTER:
1309 case '\r':
1310 if (!eof)
1311 first_displayed_line++;
1312 break;
1313 case KEY_NPAGE:
1314 case ' ':
1315 i = 0;
1316 while (!eof && i++ < LINES - 1) {
1317 char *line = parse_next_line(f, NULL);
1318 first_displayed_line++;
1319 if (line == NULL)
1320 break;
1322 break;
1323 default:
1324 break;
1326 if (pthread_mutex_unlock(&mutex) != 0) {
1327 err = got_error_from_errno();
1328 goto done;
1331 done:
1332 if (thread) {
1333 if (pthread_join(thread, (void **)&err) != 0)
1334 err = got_error_from_errno();
1335 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1336 err = NULL;
1338 if (blob)
1339 got_object_blob_close(blob);
1340 if (f)
1341 fclose(f);
1342 for (i = 0; i < nlines; i++)
1343 free(lines[i].id);
1344 free(lines);
1345 return err;
1348 static const struct got_error *
1349 cmd_blame(int argc, char *argv[])
1351 const struct got_error *error;
1352 struct got_repository *repo = NULL;
1353 char *repo_path = NULL;
1354 char *path = NULL;
1355 struct got_object_id *commit_id = NULL;
1356 char *commit_id_str = NULL;
1357 int ch;
1359 #ifndef PROFILE
1360 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1361 err(1, "pledge");
1362 #endif
1364 while ((ch = getopt(argc, argv, "c:")) != -1) {
1365 switch (ch) {
1366 case 'c':
1367 commit_id_str = optarg;
1368 break;
1369 default:
1370 usage();
1371 /* NOTREACHED */
1375 argc -= optind;
1376 argv += optind;
1378 if (argc == 0) {
1379 usage_blame();
1380 } else if (argc == 1) {
1381 repo_path = getcwd(NULL, 0);
1382 if (repo_path == NULL)
1383 return got_error_from_errno();
1384 path = argv[0];
1385 } else if (argc == 2) {
1386 repo_path = realpath(argv[0], NULL);
1387 if (repo_path == NULL)
1388 return got_error_from_errno();
1389 path = argv[1];
1390 } else
1391 usage_blame();
1393 error = got_repo_open(&repo, repo_path);
1394 free(repo_path);
1395 if (error != NULL)
1396 return error;
1398 if (commit_id_str == NULL) {
1399 struct got_reference *head_ref;
1400 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1401 if (error != NULL)
1402 goto done;
1403 error = got_ref_resolve(&commit_id, repo, head_ref);
1404 got_ref_close(head_ref);
1405 } else {
1406 struct got_object *obj;
1407 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1408 if (error != NULL)
1409 goto done;
1410 commit_id = got_object_get_id(obj);
1411 if (commit_id == NULL)
1412 error = got_error_from_errno();
1413 got_object_close(obj);
1415 if (error != NULL)
1416 goto done;
1418 error = show_blame_view(path, commit_id, repo);
1419 done:
1420 free(commit_id);
1421 if (repo)
1422 got_repo_close(repo);
1423 return error;
1426 static const struct got_error *
1427 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1428 struct got_tree_entry **last_displayed_entry,
1429 struct got_tree_entry **selected_entry, int *ndisplayed,
1430 WINDOW *window, const char *label, const char *parent_path,
1431 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1433 const struct got_error *err = NULL;
1434 struct got_tree_entry *te;
1435 wchar_t *wline;
1436 int width, n;
1438 *ndisplayed = 0;
1440 werase(window);
1442 if (limit == 0)
1443 return NULL;
1445 err = format_line(&wline, &width, label, COLS);
1446 if (err)
1447 return err;
1448 waddwstr(window, wline);
1449 if (width < COLS)
1450 waddch(window, '\n');
1451 if (--limit <= 0)
1452 return NULL;
1453 err = format_line(&wline, &width, parent_path, COLS);
1454 if (err)
1455 return err;
1456 waddwstr(window, wline);
1457 if (width < COLS)
1458 waddch(window, '\n');
1459 if (--limit <= 0)
1460 return NULL;
1461 waddch(window, '\n');
1462 if (--limit <= 0)
1463 return NULL;
1465 te = SIMPLEQ_FIRST(&entries->head);
1466 if (*first_displayed_entry == NULL) {
1467 if (selected == 0) {
1468 wstandout(window);
1469 *selected_entry = NULL;
1471 waddstr(window, " ..\n"); /* parent directory */
1472 if (selected == 0)
1473 wstandend(window);
1474 (*ndisplayed)++;
1475 if (--limit <= 0)
1476 return NULL;
1477 n = 1;
1478 } else {
1479 n = 0;
1480 while (te != *first_displayed_entry)
1481 te = SIMPLEQ_NEXT(te, entry);
1484 while (te) {
1485 char *line = NULL;
1486 if (asprintf(&line, " %s%s",
1487 te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1488 return got_error_from_errno();
1489 err = format_line(&wline, &width, line, COLS);
1490 if (err) {
1491 free(line);
1492 break;
1494 if (n == selected) {
1495 wstandout(window);
1496 *selected_entry = te;
1498 waddwstr(window, wline);
1499 if (width < COLS)
1500 waddch(window, '\n');
1501 if (n == selected)
1502 wstandend(window);
1503 free(line);
1504 n++;
1505 (*ndisplayed)++;
1506 *last_displayed_entry = te;
1507 if (--limit <= 0)
1508 break;
1509 te = SIMPLEQ_NEXT(te, entry);
1512 return err;
1515 static void
1516 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1517 const struct got_tree_entries *entries, int isroot)
1519 struct got_tree_entry *te, *prev;
1520 int i;
1522 if (*first_displayed_entry == NULL)
1523 return;
1525 te = SIMPLEQ_FIRST(&entries->head);
1526 if (*first_displayed_entry == te) {
1527 if (!isroot)
1528 *first_displayed_entry = NULL;
1529 return;
1532 /* XXX this is stupid... switch to TAILQ? */
1533 for (i = 0; i < maxscroll; i++) {
1534 while (te != *first_displayed_entry) {
1535 prev = te;
1536 te = SIMPLEQ_NEXT(te, entry);
1538 *first_displayed_entry = prev;
1539 te = SIMPLEQ_FIRST(&entries->head);
1541 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1542 *first_displayed_entry = NULL;
1545 static void
1546 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1547 struct got_tree_entry *last_displayed_entry,
1548 const struct got_tree_entries *entries)
1550 struct got_tree_entry *next;
1551 int n = 0;
1553 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1554 return;
1556 if (*first_displayed_entry)
1557 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1558 else
1559 next = SIMPLEQ_FIRST(&entries->head);
1560 while (next) {
1561 *first_displayed_entry = next;
1562 if (++n >= maxscroll)
1563 break;
1564 next = SIMPLEQ_NEXT(next, entry);
1568 struct tog_parent_tree {
1569 TAILQ_ENTRY(tog_parent_tree) entry;
1570 struct got_tree_object *tree;
1571 struct got_tree_entry *first_displayed_entry;
1572 struct got_tree_entry *selected_entry;
1573 int selected;
1576 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1578 static const struct got_error *
1579 tree_entry_path(char **path, struct tog_parent_trees *parents,
1580 struct got_tree_entry *te)
1582 const struct got_error *err = NULL;
1583 struct tog_parent_tree *pt;
1584 size_t len = 2; /* for leading slash and NUL */
1586 TAILQ_FOREACH(pt, parents, entry)
1587 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1588 if (te)
1589 len += strlen(te->name);
1591 *path = calloc(1, len);
1592 if (path == NULL)
1593 return got_error_from_errno();
1595 (*path)[0] = '/';
1596 pt = TAILQ_LAST(parents, tog_parent_trees);
1597 while (pt) {
1598 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1599 err = got_error(GOT_ERR_NO_SPACE);
1600 goto done;
1602 if (strlcat(*path, "/", len) >= len) {
1603 err = got_error(GOT_ERR_NO_SPACE);
1604 goto done;
1606 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1608 if (te) {
1609 if (strlcat(*path, te->name, len) >= len) {
1610 err = got_error(GOT_ERR_NO_SPACE);
1611 goto done;
1614 done:
1615 if (err) {
1616 free(*path);
1617 *path = NULL;
1619 return err;
1622 static const struct got_error *
1623 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1624 struct got_object_id *commit_id, struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 char *path;
1629 err = tree_entry_path(&path, parents, te);
1630 if (err)
1631 return err;
1633 err = show_blame_view(path, commit_id, repo);
1634 free(path);
1635 return err;
1638 static const struct got_error *
1639 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1640 struct got_repository *repo)
1642 const struct got_error *err = NULL;
1643 int ch, done = 0, selected = 0;
1644 struct got_tree_object *tree = root;
1645 const struct got_tree_entries *entries;
1646 struct got_tree_entry *first_displayed_entry = NULL;
1647 struct got_tree_entry *last_displayed_entry = NULL;
1648 struct got_tree_entry *selected_entry = NULL;
1649 char *commit_id_str = NULL, *tree_label = NULL;
1650 int nentries, ndisplayed;
1651 struct tog_parent_trees parents;
1653 TAILQ_INIT(&parents);
1655 err = got_object_id_str(&commit_id_str, commit_id);
1656 if (err != NULL)
1657 goto done;
1659 if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1660 err = got_error_from_errno();
1661 goto done;
1664 if (tog_tree_view.window == NULL) {
1665 tog_tree_view.window = newwin(0, 0, 0, 0);
1666 if (tog_tree_view.window == NULL)
1667 return got_error_from_errno();
1668 keypad(tog_tree_view.window, TRUE);
1670 if (tog_tree_view.panel == NULL) {
1671 tog_tree_view.panel = new_panel(tog_tree_view.window);
1672 if (tog_tree_view.panel == NULL)
1673 return got_error_from_errno();
1674 } else
1675 show_panel(tog_tree_view.panel);
1677 entries = got_object_tree_get_entries(root);
1678 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1679 while (!done) {
1680 char *parent_path;
1681 entries = got_object_tree_get_entries(tree);
1682 nentries = entries->nentries;
1683 if (tree != root)
1684 nentries++; /* '..' directory */
1686 err = tree_entry_path(&parent_path, &parents, NULL);
1687 if (err)
1688 goto done;
1690 err = draw_tree_entries(&first_displayed_entry,
1691 &last_displayed_entry, &selected_entry, &ndisplayed,
1692 tog_tree_view.window, tree_label, parent_path, entries,
1693 selected, LINES, tree == root);
1694 free(parent_path);
1695 if (err)
1696 break;
1698 nodelay(stdscr, FALSE);
1699 ch = wgetch(tog_tree_view.window);
1700 nodelay(stdscr, TRUE);
1701 switch (ch) {
1702 case 'q':
1703 done = 1;
1704 break;
1705 case 'k':
1706 case KEY_UP:
1707 if (selected > 0)
1708 selected--;
1709 if (selected > 0)
1710 break;
1711 tree_scroll_up(&first_displayed_entry, 1,
1712 entries, tree == root);
1713 break;
1714 case KEY_PPAGE:
1715 if (SIMPLEQ_FIRST(&entries->head) ==
1716 first_displayed_entry) {
1717 if (tree != root)
1718 first_displayed_entry = NULL;
1719 selected = 0;
1720 break;
1722 tree_scroll_up(&first_displayed_entry, LINES,
1723 entries, tree == root);
1724 break;
1725 case 'j':
1726 case KEY_DOWN:
1727 if (selected < ndisplayed - 1) {
1728 selected++;
1729 break;
1731 tree_scroll_down(&first_displayed_entry, 1,
1732 last_displayed_entry, entries);
1733 break;
1734 case KEY_NPAGE:
1735 tree_scroll_down(&first_displayed_entry, LINES,
1736 last_displayed_entry, entries);
1737 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1738 break;
1739 /* can't scroll any further; move cursor down */
1740 if (selected < ndisplayed - 1)
1741 selected = ndisplayed - 1;
1742 break;
1743 case KEY_ENTER:
1744 case '\r':
1745 if (selected_entry == NULL) {
1746 struct tog_parent_tree *parent;
1747 case KEY_BACKSPACE:
1748 /* user selected '..' */
1749 if (tree == root)
1750 break;
1751 parent = TAILQ_FIRST(&parents);
1752 TAILQ_REMOVE(&parents, parent, entry);
1753 got_object_tree_close(tree);
1754 tree = parent->tree;
1755 first_displayed_entry =
1756 parent->first_displayed_entry;
1757 selected_entry = parent->selected_entry;
1758 selected = parent->selected;
1759 free(parent);
1760 } else if (S_ISDIR(selected_entry->mode)) {
1761 struct tog_parent_tree *parent;
1762 struct got_tree_object *child;
1763 err = got_object_open_as_tree(
1764 &child, repo, selected_entry->id);
1765 if (err)
1766 goto done;
1767 parent = calloc(1, sizeof(*parent));
1768 if (parent == NULL) {
1769 err = got_error_from_errno();
1770 goto done;
1772 parent->tree = tree;
1773 parent->first_displayed_entry =
1774 first_displayed_entry;
1775 parent->selected_entry = selected_entry;
1776 parent->selected = selected;
1777 TAILQ_INSERT_HEAD(&parents, parent,
1778 entry);
1779 tree = child;
1780 selected = 0;
1781 first_displayed_entry = NULL;
1782 } else if (S_ISREG(selected_entry->mode)) {
1783 err = blame_tree_entry(selected_entry,
1784 &parents, commit_id, repo);
1785 if (err)
1786 goto done;
1788 break;
1789 case KEY_RESIZE:
1790 if (selected > LINES)
1791 selected = ndisplayed - 1;
1792 break;
1793 default:
1794 break;
1797 done:
1798 free(tree_label);
1799 free(commit_id_str);
1800 while (!TAILQ_EMPTY(&parents)) {
1801 struct tog_parent_tree *parent;
1802 parent = TAILQ_FIRST(&parents);
1803 TAILQ_REMOVE(&parents, parent, entry);
1804 free(parent);
1807 if (tree != root)
1808 got_object_tree_close(tree);
1809 return err;
1812 __dead static void
1813 usage_tree(void)
1815 endwin();
1816 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1817 getprogname());
1818 exit(1);
1821 static const struct got_error *
1822 cmd_tree(int argc, char *argv[])
1824 const struct got_error *error;
1825 struct got_repository *repo = NULL;
1826 char *repo_path = NULL;
1827 struct got_object_id *commit_id = NULL;
1828 char *commit_id_arg = NULL;
1829 struct got_commit_object *commit = NULL;
1830 struct got_tree_object *tree = NULL;
1831 int ch;
1833 #ifndef PROFILE
1834 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1835 err(1, "pledge");
1836 #endif
1838 while ((ch = getopt(argc, argv, "c:")) != -1) {
1839 switch (ch) {
1840 case 'c':
1841 commit_id_arg = optarg;
1842 break;
1843 default:
1844 usage();
1845 /* NOTREACHED */
1849 argc -= optind;
1850 argv += optind;
1852 if (argc == 0) {
1853 repo_path = getcwd(NULL, 0);
1854 if (repo_path == NULL)
1855 return got_error_from_errno();
1856 } else if (argc == 1) {
1857 repo_path = realpath(argv[0], NULL);
1858 if (repo_path == NULL)
1859 return got_error_from_errno();
1860 } else
1861 usage_log();
1863 error = got_repo_open(&repo, repo_path);
1864 free(repo_path);
1865 if (error != NULL)
1866 return error;
1868 if (commit_id_arg == NULL) {
1869 error = get_head_commit_id(&commit_id, repo);
1870 if (error != NULL)
1871 goto done;
1872 } else {
1873 struct got_object *obj;
1874 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1875 if (error == NULL) {
1876 commit_id = got_object_get_id(obj);
1877 if (commit_id == NULL)
1878 error = got_error_from_errno();
1881 if (error != NULL)
1882 goto done;
1884 error = got_object_open_as_commit(&commit, repo, commit_id);
1885 if (error != NULL)
1886 goto done;
1888 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1889 if (error != NULL)
1890 goto done;
1892 error = show_tree_view(tree, commit_id, repo);
1893 done:
1894 free(commit_id);
1895 if (commit)
1896 got_object_commit_close(commit);
1897 if (tree)
1898 got_object_tree_close(tree);
1899 if (repo)
1900 got_repo_close(repo);
1901 return error;
1903 static void
1904 init_curses(void)
1906 initscr();
1907 cbreak();
1908 noecho();
1909 nonl();
1910 intrflush(stdscr, FALSE);
1911 keypad(stdscr, TRUE);
1912 curs_set(0);
1915 __dead static void
1916 usage(void)
1918 int i;
1920 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1921 "Available commands:\n", getprogname());
1922 for (i = 0; i < nitems(tog_commands); i++) {
1923 struct tog_cmd *cmd = &tog_commands[i];
1924 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1926 exit(1);
1929 static char **
1930 make_argv(const char *arg0, const char *arg1)
1932 char **argv;
1933 int argc = (arg1 == NULL ? 1 : 2);
1935 argv = calloc(argc, sizeof(char *));
1936 if (argv == NULL)
1937 err(1, "calloc");
1938 argv[0] = strdup(arg0);
1939 if (argv[0] == NULL)
1940 err(1, "calloc");
1941 if (arg1) {
1942 argv[1] = strdup(arg1);
1943 if (argv[1] == NULL)
1944 err(1, "calloc");
1947 return argv;
1950 int
1951 main(int argc, char *argv[])
1953 const struct got_error *error = NULL;
1954 struct tog_cmd *cmd = NULL;
1955 int ch, hflag = 0;
1956 char **cmd_argv = NULL;
1958 setlocale(LC_ALL, "");
1960 while ((ch = getopt(argc, argv, "h")) != -1) {
1961 switch (ch) {
1962 case 'h':
1963 hflag = 1;
1964 break;
1965 default:
1966 usage();
1967 /* NOTREACHED */
1971 argc -= optind;
1972 argv += optind;
1973 optind = 0;
1974 optreset = 1;
1976 if (argc == 0) {
1977 if (hflag)
1978 usage();
1979 /* Build an argument vector which runs a default command. */
1980 cmd = &tog_commands[0];
1981 cmd_argv = make_argv(cmd->name, NULL);
1982 argc = 1;
1983 } else {
1984 int i;
1986 /* Did the user specific a command? */
1987 for (i = 0; i < nitems(tog_commands); i++) {
1988 if (strncmp(tog_commands[i].name, argv[0],
1989 strlen(argv[0])) == 0) {
1990 cmd = &tog_commands[i];
1991 if (hflag)
1992 tog_commands[i].cmd_usage();
1993 break;
1996 if (cmd == NULL) {
1997 /* Did the user specify a repository? */
1998 char *repo_path = realpath(argv[0], NULL);
1999 if (repo_path) {
2000 struct got_repository *repo;
2001 error = got_repo_open(&repo, repo_path);
2002 if (error == NULL)
2003 got_repo_close(repo);
2004 } else
2005 error = got_error_from_errno();
2006 if (error) {
2007 if (hflag) {
2008 fprintf(stderr, "%s: '%s' is not a "
2009 "known command\n", getprogname(),
2010 argv[0]);
2011 usage();
2013 fprintf(stderr, "%s: '%s' is neither a known "
2014 "command nor a path to a repository\n",
2015 getprogname(), argv[0]);
2016 free(repo_path);
2017 return 1;
2019 cmd = &tog_commands[0];
2020 cmd_argv = make_argv(cmd->name, repo_path);
2021 argc = 2;
2022 free(repo_path);
2026 init_curses();
2028 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2029 if (error)
2030 goto done;
2031 done:
2032 endwin();
2033 free(cmd_argv);
2034 if (error)
2035 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2036 return 0;