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 datebuf[10]; /* YY-MM-DD + SPACE + NUL */
207 char *logmsg0 = NULL, *logmsg = NULL;
208 char *author0 = NULL, *author = NULL;
209 wchar_t *wlogmsg = NULL, *wauthor = NULL;
210 int author_width, logmsg_width;
211 char *newline, *smallerthan;
212 char *line = NULL;
213 char *id_str = NULL;
214 size_t id_len;
215 int col, limit;
216 static const size_t date_display_cols = 9;
217 static const size_t id_display_cols = 8;
218 static const size_t author_display_cols = 16;
219 const int avail = COLS;
221 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &commit->tm_committer)
222 >= sizeof(datebuf))
223 return got_error(GOT_ERR_NO_SPACE);
225 if (avail < date_display_cols)
226 limit = MIN(sizeof(datebuf) - 1, avail);
227 else
228 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
229 waddnstr(tog_log_view.window, datebuf, limit);
230 col = limit + 1;
231 if (col > avail)
232 goto done;
234 err = got_object_id_str(&id_str, id);
235 if (err)
236 return err;
237 id_len = strlen(id_str);
238 if (avail < date_display_cols + id_display_cols) {
239 limit = MIN(id_len, avail - date_display_cols);
240 waddnstr(tog_log_view.window, id_str, limit);
241 } else {
242 limit = MIN(id_display_cols, id_len);
243 waddnstr(tog_log_view.window, id_str, limit);
245 col += limit;
246 while (col <= avail && col < date_display_cols + id_display_cols + 2) {
247 waddch(tog_log_view.window, ' ');
248 col++;
250 if (col > avail)
251 goto done;
253 author0 = strdup(commit->author);
254 if (author0 == NULL) {
255 err = got_error_from_errno();
256 goto done;
258 author = author0;
259 smallerthan = strchr(author, '<');
260 if (smallerthan)
261 *smallerthan = '\0';
262 else {
263 char *at = strchr(author, '@');
264 if (at)
265 *at = '\0';
267 limit = avail - col;
268 err = format_line(&wauthor, &author_width, author, limit);
269 if (err)
270 goto done;
271 waddwstr(tog_log_view.window, wauthor);
272 col += author_width;
273 while (col <= avail && author_width < author_display_cols + 1) {
274 waddch(tog_log_view.window, ' ');
275 col++;
276 author_width++;
278 if (col > avail)
279 goto done;
281 logmsg0 = strdup(commit->logmsg);
282 if (logmsg0 == NULL) {
283 err = got_error_from_errno();
284 goto done;
286 logmsg = logmsg0;
287 while (*logmsg == '\n')
288 logmsg++;
289 newline = strchr(logmsg, '\n');
290 if (newline)
291 *newline = '\0';
292 limit = avail - col;
293 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
294 if (err)
295 goto done;
296 waddwstr(tog_log_view.window, wlogmsg);
297 col += logmsg_width;
298 while (col <= avail) {
299 waddch(tog_log_view.window, ' ');
300 col++;
302 done:
303 free(logmsg0);
304 free(wlogmsg);
305 free(author0);
306 free(wauthor);
307 free(line);
308 free(id_str);
309 return err;
312 struct commit_queue_entry {
313 TAILQ_ENTRY(commit_queue_entry) entry;
314 struct got_object_id *id;
315 struct got_commit_object *commit;
316 };
317 TAILQ_HEAD(commit_queue, commit_queue_entry);
319 static struct commit_queue_entry *
320 alloc_commit_queue_entry(struct got_commit_object *commit,
321 struct got_object_id *id)
323 struct commit_queue_entry *entry;
325 entry = calloc(1, sizeof(*entry));
326 if (entry == NULL)
327 return NULL;
329 entry->id = id;
330 entry->commit = commit;
331 return entry;
334 static void
335 pop_commit(struct commit_queue *commits)
337 struct commit_queue_entry *entry;
339 entry = TAILQ_FIRST(commits);
340 TAILQ_REMOVE(commits, entry, entry);
341 got_object_commit_close(entry->commit);
342 /* Don't free entry->id! It is owned by the commit graph. */
343 free(entry);
346 static void
347 free_commits(struct commit_queue *commits)
349 while (!TAILQ_EMPTY(commits))
350 pop_commit(commits);
353 static const struct got_error *
354 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
355 struct got_object_id *start_id, struct got_repository *repo)
357 const struct got_error *err = NULL;
358 struct got_object_id *id;
359 struct commit_queue_entry *entry;
361 err = got_commit_graph_iter_start(graph, start_id);
362 if (err)
363 return err;
365 entry = TAILQ_LAST(commits, commit_queue);
366 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
367 int nfetched;
369 /* Start ID's commit is already on the queue; skip over it. */
370 err = got_commit_graph_iter_next(&id, graph);
371 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
372 return err;
374 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
375 if (err)
376 return err;
379 while (1) {
380 struct got_commit_object *commit;
382 err = got_commit_graph_iter_next(&id, graph);
383 if (err) {
384 if (err->code == GOT_ERR_ITER_NEED_MORE)
385 err = NULL;
386 break;
389 err = got_object_open_as_commit(&commit, repo, id);
390 if (err)
391 break;
393 entry = alloc_commit_queue_entry(commit, id);
394 if (entry == NULL) {
395 err = got_error_from_errno();
396 break;
399 TAILQ_INSERT_TAIL(commits, entry, entry);
402 return err;
405 static const struct got_error *
406 fetch_next_commit(struct commit_queue_entry **pentry,
407 struct commit_queue_entry *entry, struct commit_queue *commits,
408 struct got_commit_graph *graph, struct got_repository *repo)
410 const struct got_error *err = NULL;
411 struct got_object_qid *qid;
413 *pentry = NULL;
415 /* Populate commit graph with entry's parent commits. */
416 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
417 int nfetched;
418 err = got_commit_graph_fetch_commits_up_to(&nfetched,
419 graph, qid->id, repo);
420 if (err)
421 return err;
424 /* Append outstanding commits to queue in graph sort order. */
425 err = queue_commits(graph, commits, entry->id, repo);
426 if (err) {
427 if (err->code == GOT_ERR_ITER_COMPLETED)
428 err = NULL;
429 return err;
432 /* Next entry to display should now be available. */
433 *pentry = TAILQ_NEXT(entry, entry);
434 if (*pentry == NULL)
435 return got_error(GOT_ERR_NO_OBJ);
437 return NULL;
440 static const struct got_error *
441 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
443 const struct got_error *err = NULL;
444 struct got_reference *head_ref;
446 *head_id = NULL;
448 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
449 if (err)
450 return err;
452 err = got_ref_resolve(head_id, repo, head_ref);
453 got_ref_close(head_ref);
454 if (err) {
455 *head_id = NULL;
456 return err;
459 return NULL;
462 static const struct got_error *
463 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
464 struct commit_queue_entry *first, int selected_idx, int limit)
466 const struct got_error *err = NULL;
467 struct commit_queue_entry *entry;
468 int ncommits = 0;
470 werase(tog_log_view.window);
472 entry = first;
473 *last = first;
474 while (entry) {
475 if (ncommits == limit)
476 break;
477 if (ncommits == selected_idx) {
478 wstandout(tog_log_view.window);
479 *selected = entry;
481 err = draw_commit(entry->commit, entry->id);
482 if (ncommits == selected_idx)
483 wstandend(tog_log_view.window);
484 if (err)
485 break;
486 ncommits++;
487 *last = entry;
488 entry = TAILQ_NEXT(entry, entry);
491 update_panels();
492 doupdate();
494 return err;
497 static void
498 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
499 struct commit_queue *commits)
501 struct commit_queue_entry *entry;
502 int nscrolled = 0;
504 entry = TAILQ_FIRST(commits);
505 if (*first_displayed_entry == entry)
506 return;
508 entry = *first_displayed_entry;
509 while (entry && nscrolled < maxscroll) {
510 entry = TAILQ_PREV(entry, commit_queue, entry);
511 if (entry) {
512 *first_displayed_entry = entry;
513 nscrolled++;
518 static const struct got_error *
519 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
520 struct commit_queue_entry *last_displayed_entry,
521 struct commit_queue *commits, struct got_commit_graph *graph,
522 struct got_repository *repo)
524 const struct got_error *err = NULL;
525 struct commit_queue_entry *pentry;
526 int nscrolled = 0;
528 do {
529 pentry = TAILQ_NEXT(last_displayed_entry, entry);
530 if (pentry == NULL) {
531 err = fetch_next_commit(&pentry, last_displayed_entry,
532 commits, graph, repo);
533 if (err || pentry == NULL)
534 break;
536 last_displayed_entry = pentry;
538 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
539 if (pentry == NULL)
540 break;
541 *first_displayed_entry = pentry;
542 } while (++nscrolled < maxscroll);
544 return err;
547 static int
548 num_parents(struct commit_queue_entry *entry)
550 int nparents = 0;
552 while (entry) {
553 entry = TAILQ_NEXT(entry, entry);
554 nparents++;
557 return nparents;
560 static const struct got_error *
561 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
563 const struct got_error *err;
564 struct got_object *obj1 = NULL, *obj2 = NULL;
565 struct got_object_qid *parent_id;
567 err = got_object_open(&obj2, repo, entry->id);
568 if (err)
569 return err;
571 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
572 if (parent_id) {
573 err = got_object_open(&obj1, repo, parent_id->id);
574 if (err)
575 goto done;
578 err = show_diff_view(obj1, obj2, repo);
579 done:
580 if (obj1)
581 got_object_close(obj1);
582 if (obj2)
583 got_object_close(obj2);
584 return err;
587 static const struct got_error *
588 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
590 const struct got_error *err = NULL;
591 struct got_tree_object *tree;
593 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
594 if (err)
595 return err;
597 err = show_tree_view(tree, entry->id, repo);
598 got_object_tree_close(tree);
599 return err;
602 static const struct got_error *
603 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
605 const struct got_error *err = NULL;
606 struct got_object_id *head_id = NULL;
607 int ch, done = 0, selected = 0, nparents, nfetched;
608 struct got_commit_graph *graph;
609 struct commit_queue commits;
610 struct commit_queue_entry *entry = NULL;
611 struct commit_queue_entry *first_displayed_entry = NULL;
612 struct commit_queue_entry *last_displayed_entry = NULL;
613 struct commit_queue_entry *selected_entry = NULL;
615 if (tog_log_view.window == NULL) {
616 tog_log_view.window = newwin(0, 0, 0, 0);
617 if (tog_log_view.window == NULL)
618 return got_error_from_errno();
619 keypad(tog_log_view.window, TRUE);
621 if (tog_log_view.panel == NULL) {
622 tog_log_view.panel = new_panel(tog_log_view.window);
623 if (tog_log_view.panel == NULL)
624 return got_error_from_errno();
625 } else
626 show_panel(tog_log_view.panel);
628 err = get_head_commit_id(&head_id, repo);
629 if (err)
630 return err;
632 TAILQ_INIT(&commits);
634 err = got_commit_graph_open(&graph, head_id, 0, repo);
635 if (err)
636 goto done;
638 /* Populate commit graph with a sufficient number of commits. */
639 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
640 repo);
641 if (err)
642 goto done;
643 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
644 if (err)
645 goto done;
647 /*
648 * Open the initial batch of commits, sorted in commit graph order.
649 * We keep all commits open throughout the lifetime of the log view
650 * in order to avoid having to re-fetch commits from disk while
651 * updating the display.
652 */
653 err = queue_commits(graph, &commits, head_id, repo);
654 if (err && err->code != GOT_ERR_ITER_COMPLETED)
655 goto done;
657 /* Find entry corresponding to the first commit to display. */
658 TAILQ_FOREACH(entry, &commits, entry) {
659 if (got_object_id_cmp(entry->id, start_id) == 0) {
660 first_displayed_entry = entry;
661 break;
664 if (first_displayed_entry == NULL) {
665 err = got_error(GOT_ERR_NO_OBJ);
666 goto done;
669 while (!done) {
670 err = draw_commits(&last_displayed_entry, &selected_entry,
671 first_displayed_entry, selected, LINES);
672 if (err)
673 goto done;
675 nodelay(stdscr, FALSE);
676 ch = wgetch(tog_log_view.window);
677 nodelay(stdscr, TRUE);
678 switch (ch) {
679 case ERR:
680 if (errno) {
681 err = got_error_from_errno();
682 goto done;
684 break;
685 case 'q':
686 done = 1;
687 break;
688 case 'k':
689 case KEY_UP:
690 if (selected > 0)
691 selected--;
692 if (selected > 0)
693 break;
694 scroll_up(&first_displayed_entry, 1, &commits);
695 break;
696 case KEY_PPAGE:
697 if (TAILQ_FIRST(&commits) ==
698 first_displayed_entry) {
699 selected = 0;
700 break;
702 scroll_up(&first_displayed_entry, LINES,
703 &commits);
704 break;
705 case 'j':
706 case KEY_DOWN:
707 nparents = num_parents(first_displayed_entry);
708 if (selected < LINES - 1 &&
709 selected < nparents - 1) {
710 selected++;
711 break;
713 err = scroll_down(&first_displayed_entry, 1,
714 last_displayed_entry, &commits, graph,
715 repo);
716 if (err)
717 goto done;
718 break;
719 case KEY_NPAGE:
720 err = scroll_down(&first_displayed_entry, LINES,
721 last_displayed_entry, &commits, graph,
722 repo);
723 if (err)
724 goto done;
725 if (last_displayed_entry->commit->nparents > 0)
726 break;
727 /* can't scroll any further; move cursor down */
728 nparents = num_parents(first_displayed_entry);
729 if (selected < LINES - 1 ||
730 selected < nparents - 1)
731 selected = MIN(LINES - 1, nparents - 1);
732 break;
733 case KEY_RESIZE:
734 if (selected > LINES)
735 selected = LINES - 1;
736 break;
737 case KEY_ENTER:
738 case '\r':
739 err = show_commit(selected_entry, repo);
740 if (err)
741 goto done;
742 show_panel(tog_log_view.panel);
743 break;
744 case 't':
745 err = browse_commit(selected_entry, repo);
746 if (err)
747 goto done;
748 show_panel(tog_log_view.panel);
749 break;
750 default:
751 break;
754 done:
755 free(head_id);
756 if (graph)
757 got_commit_graph_close(graph);
758 free_commits(&commits);
759 return err;
762 static const struct got_error *
763 cmd_log(int argc, char *argv[])
765 const struct got_error *error;
766 struct got_repository *repo;
767 struct got_object_id *start_id = NULL;
768 char *repo_path = NULL;
769 char *start_commit = NULL;
770 int ch;
772 #ifndef PROFILE
773 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
774 err(1, "pledge");
775 #endif
777 while ((ch = getopt(argc, argv, "c:")) != -1) {
778 switch (ch) {
779 case 'c':
780 start_commit = optarg;
781 break;
782 default:
783 usage();
784 /* NOTREACHED */
788 argc -= optind;
789 argv += optind;
791 if (argc == 0) {
792 repo_path = getcwd(NULL, 0);
793 if (repo_path == NULL)
794 return got_error_from_errno();
795 } else if (argc == 1) {
796 repo_path = realpath(argv[0], NULL);
797 if (repo_path == NULL)
798 return got_error_from_errno();
799 } else
800 usage_log();
802 error = got_repo_open(&repo, repo_path);
803 free(repo_path);
804 if (error != NULL)
805 return error;
807 if (start_commit == NULL) {
808 error = get_head_commit_id(&start_id, repo);
809 if (error != NULL)
810 return error;
811 } else {
812 struct got_object *obj;
813 error = got_object_open_by_id_str(&obj, repo, start_commit);
814 if (error == NULL) {
815 start_id = got_object_get_id(obj);
816 if (start_id == NULL)
817 error = got_error_from_errno();
820 if (error != NULL)
821 return error;
822 error = show_log_view(start_id, repo);
823 free(start_id);
824 got_repo_close(repo);
825 return error;
828 __dead static void
829 usage_diff(void)
831 endwin();
832 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
833 getprogname());
834 exit(1);
837 static char *
838 parse_next_line(FILE *f, size_t *len)
840 char *line;
841 size_t linelen;
842 size_t lineno;
843 const char delim[3] = { '\0', '\0', '\0'};
845 line = fparseln(f, &linelen, &lineno, delim, 0);
846 if (len)
847 *len = linelen;
848 return line;
851 static const struct got_error *
852 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
853 int *last_displayed_line, int *eof, int max_lines)
855 const struct got_error *err;
856 int nlines = 0, nprinted = 0;
857 char *line;
858 size_t len;
859 wchar_t *wline;
860 int width;
862 rewind(f);
863 werase(window);
865 *eof = 0;
866 while (nprinted < max_lines) {
867 line = parse_next_line(f, &len);
868 if (line == NULL) {
869 *eof = 1;
870 break;
872 if (++nlines < *first_displayed_line) {
873 free(line);
874 continue;
877 err = format_line(&wline, &width, line, COLS);
878 if (err) {
879 free(line);
880 return err;
882 waddwstr(window, wline);
883 if (width < COLS)
884 waddch(window, '\n');
885 if (++nprinted == 1)
886 *first_displayed_line = nlines;
887 free(line);
889 *last_displayed_line = nlines;
891 update_panels();
892 doupdate();
894 return NULL;
897 static const struct got_error *
898 show_diff_view(struct got_object *obj1, struct got_object *obj2,
899 struct got_repository *repo)
901 const struct got_error *err;
902 FILE *f;
903 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
904 int eof, i;
906 if (obj1 != NULL && obj2 != NULL &&
907 got_object_get_type(obj1) != got_object_get_type(obj2))
908 return got_error(GOT_ERR_OBJ_TYPE);
910 f = got_opentemp();
911 if (f == NULL)
912 return got_error_from_errno();
914 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
915 case GOT_OBJ_TYPE_BLOB:
916 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
917 break;
918 case GOT_OBJ_TYPE_TREE:
919 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
920 break;
921 case GOT_OBJ_TYPE_COMMIT:
922 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
923 break;
924 default:
925 return got_error(GOT_ERR_OBJ_TYPE);
928 fflush(f);
930 if (tog_diff_view.window == NULL) {
931 tog_diff_view.window = newwin(0, 0, 0, 0);
932 if (tog_diff_view.window == NULL)
933 return got_error_from_errno();
934 keypad(tog_diff_view.window, TRUE);
936 if (tog_diff_view.panel == NULL) {
937 tog_diff_view.panel = new_panel(tog_diff_view.window);
938 if (tog_diff_view.panel == NULL)
939 return got_error_from_errno();
940 } else
941 show_panel(tog_diff_view.panel);
943 while (!done) {
944 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
945 &last_displayed_line, &eof, LINES);
946 if (err)
947 break;
948 nodelay(stdscr, FALSE);
949 ch = wgetch(tog_diff_view.window);
950 nodelay(stdscr, TRUE);
951 switch (ch) {
952 case 'q':
953 done = 1;
954 break;
955 case 'k':
956 case KEY_UP:
957 case KEY_BACKSPACE:
958 if (first_displayed_line > 1)
959 first_displayed_line--;
960 break;
961 case KEY_PPAGE:
962 i = 0;
963 while (i++ < LINES - 1 &&
964 first_displayed_line > 1)
965 first_displayed_line--;
966 break;
967 case 'j':
968 case KEY_DOWN:
969 case KEY_ENTER:
970 case '\r':
971 if (!eof)
972 first_displayed_line++;
973 break;
974 case KEY_NPAGE:
975 case ' ':
976 i = 0;
977 while (!eof && i++ < LINES - 1) {
978 char *line = parse_next_line(f, NULL);
979 first_displayed_line++;
980 if (line == NULL)
981 break;
983 break;
984 default:
985 break;
988 fclose(f);
989 return err;
992 static const struct got_error *
993 cmd_diff(int argc, char *argv[])
995 const struct got_error *error = NULL;
996 struct got_repository *repo = NULL;
997 struct got_object *obj1 = NULL, *obj2 = NULL;
998 char *repo_path = NULL;
999 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1000 int ch;
1002 #ifndef PROFILE
1003 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1004 err(1, "pledge");
1005 #endif
1007 while ((ch = getopt(argc, argv, "")) != -1) {
1008 switch (ch) {
1009 default:
1010 usage();
1011 /* NOTREACHED */
1015 argc -= optind;
1016 argv += optind;
1018 if (argc == 0) {
1019 usage_diff(); /* TODO show local worktree changes */
1020 } else if (argc == 2) {
1021 repo_path = getcwd(NULL, 0);
1022 if (repo_path == NULL)
1023 return got_error_from_errno();
1024 obj_id_str1 = argv[0];
1025 obj_id_str2 = argv[1];
1026 } else if (argc == 3) {
1027 repo_path = realpath(argv[0], NULL);
1028 if (repo_path == NULL)
1029 return got_error_from_errno();
1030 obj_id_str1 = argv[1];
1031 obj_id_str2 = argv[2];
1032 } else
1033 usage_diff();
1035 error = got_repo_open(&repo, repo_path);
1036 free(repo_path);
1037 if (error)
1038 goto done;
1040 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1041 if (error)
1042 goto done;
1044 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1045 if (error)
1046 goto done;
1048 error = show_diff_view(obj1, obj2, repo);
1049 done:
1050 got_repo_close(repo);
1051 if (obj1)
1052 got_object_close(obj1);
1053 if (obj2)
1054 got_object_close(obj2);
1055 return error;
1058 __dead static void
1059 usage_blame(void)
1061 endwin();
1062 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1063 getprogname());
1064 exit(1);
1067 struct tog_blame_line {
1068 int annotated;
1069 struct got_object_id *id;
1072 static const struct got_error *
1073 draw_blame(WINDOW *window, FILE *f, const char *path,
1074 struct tog_blame_line *lines, int nlines, int blame_complete,
1075 int *first_displayed_line, int *last_displayed_line,
1076 int *eof, int max_lines)
1078 const struct got_error *err;
1079 int lineno = 0, nprinted = 0;
1080 char *line;
1081 size_t len;
1082 wchar_t *wline;
1083 int width;
1084 struct tog_blame_line *blame_line;
1086 rewind(f);
1087 werase(window);
1089 if (asprintf(&line, "[%d-%d/%d] annotation of %s%s",
1090 *first_displayed_line, *last_displayed_line, nlines,
1091 path, blame_complete ? "" : " in progress...") == -1)
1092 return got_error_from_errno();
1093 err = format_line(&wline, &width, line, COLS);
1094 free(line);
1095 if (err)
1096 return err;
1097 waddwstr(window, wline);
1098 if (width < COLS)
1099 waddch(window, '\n');
1101 *eof = 0;
1102 while (nprinted < max_lines - 1) {
1103 line = parse_next_line(f, &len);
1104 if (line == NULL) {
1105 *eof = 1;
1106 break;
1108 if (++lineno < *first_displayed_line) {
1109 free(line);
1110 continue;
1113 err = format_line(&wline, &width, line, COLS - 9);
1114 if (err) {
1115 free(line);
1116 return err;
1119 blame_line = &lines[lineno - 1];
1120 if (blame_line->annotated) {
1121 char *id_str;
1122 err = got_object_id_str(&id_str, blame_line->id);
1123 if (err) {
1124 free(line);
1125 return err;
1127 wprintw(window, "%.8s ", id_str);
1128 free(id_str);
1129 } else
1130 waddstr(window, " ");
1132 waddwstr(window, wline);
1133 if (width < COLS - 9)
1134 waddch(window, '\n');
1135 if (++nprinted == 1)
1136 *first_displayed_line = lineno;
1137 free(line);
1139 *last_displayed_line = lineno;
1141 update_panels();
1142 doupdate();
1144 return NULL;
1147 struct tog_blame_cb_args {
1148 pthread_mutex_t *mutex;
1149 struct tog_blame_line *lines; /* one per line */
1150 int nlines;
1152 FILE *f;
1153 const char *path;
1154 WINDOW *window;
1155 int *first_displayed_line;
1156 int *last_displayed_line;
1157 int *quit;
1160 static const struct got_error *
1161 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1163 const struct got_error *err = NULL;
1164 struct tog_blame_cb_args *a = arg;
1165 struct tog_blame_line *line;
1166 int eof;
1168 if (nlines != a->nlines ||
1169 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1170 return got_error(GOT_ERR_RANGE);
1172 if (pthread_mutex_lock(a->mutex) != 0)
1173 return got_error_from_errno();
1175 if (*a->quit) { /* user has quit the blame view */
1176 err = got_error(GOT_ERR_ITER_COMPLETED);
1177 goto done;
1180 if (lineno == -1)
1181 goto done; /* no change in this commit */
1183 line = &a->lines[lineno - 1];
1184 if (line->annotated)
1185 goto done;
1187 line->id = got_object_id_dup(id);
1188 if (line->id == NULL) {
1189 err = got_error_from_errno();
1190 goto done;
1192 line->annotated = 1;
1194 err = draw_blame(a->window, a->f, a->path, a->lines, a->nlines, 0,
1195 a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1196 done:
1197 if (pthread_mutex_unlock(a->mutex) != 0)
1198 return got_error_from_errno();
1199 return err;
1202 struct tog_blame_thread_args {
1203 const char *path;
1204 struct got_object_id *commit_id;
1205 struct got_repository *repo;
1206 void *blame_cb_args;
1207 int *complete;
1210 static void *
1211 blame_thread(void *arg)
1213 const struct got_error *err;
1214 struct tog_blame_thread_args *ta = arg;
1215 struct tog_blame_cb_args *a = ta->blame_cb_args;
1216 int eof;
1218 err = got_blame_incremental(ta->path, ta->commit_id, ta->repo,
1219 blame_cb, ta->blame_cb_args);
1220 *ta->complete = 1;
1221 if (err)
1222 return (void *)err;
1224 if (pthread_mutex_lock(a->mutex) != 0)
1225 return (void *)got_error_from_errno();
1227 err = draw_blame(a->window, a->f, a->path, a->lines, a->nlines, 1,
1228 a->first_displayed_line, a->last_displayed_line, &eof, LINES);
1230 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1231 err = got_error_from_errno();
1233 return (void *)err;
1236 static const struct got_error *
1237 show_blame_view(const char *path, struct got_object_id *commit_id,
1238 struct got_repository *repo)
1240 const struct got_error *err = NULL;
1241 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1242 int eof, i, blame_complete = 0;
1243 struct got_object *obj = NULL;
1244 struct got_blob_object *blob = NULL;
1245 FILE *f = NULL;
1246 size_t filesize, nlines = 0;
1247 struct tog_blame_line *lines = NULL;
1248 pthread_t thread = NULL;
1249 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1250 struct tog_blame_cb_args blame_cb_args;
1251 struct tog_blame_thread_args blame_thread_args;
1253 err = got_object_open_by_path(&obj, repo, commit_id, path);
1254 if (err)
1255 goto done;
1256 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1257 err = got_error(GOT_ERR_OBJ_TYPE);
1258 got_object_close(obj);
1259 goto done;
1262 err = got_object_blob_open(&blob, repo, obj, 8192);
1263 got_object_close(obj);
1264 if (err)
1265 goto done;
1266 f = got_opentemp();
1267 if (f == NULL) {
1268 err = got_error_from_errno();
1269 goto done;
1271 err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1272 if (err)
1273 goto done;
1275 lines = calloc(nlines, sizeof(*lines));
1276 if (lines == NULL) {
1277 err = got_error_from_errno();
1278 goto done;
1281 if (tog_blame_view.window == NULL) {
1282 tog_blame_view.window = newwin(0, 0, 0, 0);
1283 if (tog_blame_view.window == NULL)
1284 return got_error_from_errno();
1285 keypad(tog_blame_view.window, TRUE);
1287 if (tog_blame_view.panel == NULL) {
1288 tog_blame_view.panel = new_panel(tog_blame_view.window);
1289 if (tog_blame_view.panel == NULL)
1290 return got_error_from_errno();
1291 } else
1292 show_panel(tog_blame_view.panel);
1294 if (pthread_mutex_init(&mutex, NULL) != 0) {
1295 err = got_error_from_errno();
1296 goto done;
1298 blame_cb_args.lines = lines;
1299 blame_cb_args.nlines = nlines;
1300 blame_cb_args.mutex = &mutex;
1301 blame_cb_args.f = f;
1302 blame_cb_args.path = path;
1303 blame_cb_args.window = tog_blame_view.window;
1304 blame_cb_args.first_displayed_line = &first_displayed_line;
1305 blame_cb_args.last_displayed_line = &last_displayed_line;
1306 blame_cb_args.quit = &done;
1308 blame_thread_args.path = path;
1309 blame_thread_args.commit_id = commit_id;
1310 blame_thread_args.repo = repo;
1311 blame_thread_args.blame_cb_args = &blame_cb_args;
1312 blame_thread_args.complete = &blame_complete;
1314 if (pthread_create(&thread, NULL, blame_thread,
1315 &blame_thread_args) != 0) {
1316 err = got_error_from_errno();
1317 goto done;
1320 while (!done) {
1321 if (pthread_mutex_lock(&mutex) != 0) {
1322 err = got_error_from_errno();
1323 goto done;
1325 err = draw_blame(tog_blame_view.window, f, path, lines, nlines,
1326 blame_complete, &first_displayed_line, &last_displayed_line,
1327 &eof, LINES);
1328 if (pthread_mutex_unlock(&mutex) != 0) {
1329 err = got_error_from_errno();
1330 goto done;
1332 if (err)
1333 break;
1334 nodelay(stdscr, FALSE);
1335 ch = wgetch(tog_blame_view.window);
1336 nodelay(stdscr, TRUE);
1337 if (pthread_mutex_lock(&mutex) != 0) {
1338 err = got_error_from_errno();
1339 goto done;
1341 switch (ch) {
1342 case 'q':
1343 done = 1;
1344 break;
1345 case 'k':
1346 case KEY_UP:
1347 case KEY_BACKSPACE:
1348 if (first_displayed_line > 1)
1349 first_displayed_line--;
1350 break;
1351 case KEY_PPAGE:
1352 i = 0;
1353 while (i++ < LINES - 1 &&
1354 first_displayed_line > 1)
1355 first_displayed_line--;
1356 break;
1357 case 'j':
1358 case KEY_DOWN:
1359 case KEY_ENTER:
1360 case '\r':
1361 if (!eof)
1362 first_displayed_line++;
1363 break;
1364 case KEY_NPAGE:
1365 case ' ':
1366 i = 0;
1367 while (!eof && i++ < LINES - 1) {
1368 char *line = parse_next_line(f, NULL);
1369 first_displayed_line++;
1370 if (line == NULL)
1371 break;
1373 break;
1374 default:
1375 break;
1377 if (pthread_mutex_unlock(&mutex) != 0) {
1378 err = got_error_from_errno();
1379 goto done;
1382 done:
1383 if (thread) {
1384 if (pthread_join(thread, (void **)&err) != 0)
1385 err = got_error_from_errno();
1386 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1387 err = NULL;
1389 if (blob)
1390 got_object_blob_close(blob);
1391 if (f)
1392 fclose(f);
1393 for (i = 0; i < nlines; i++)
1394 free(lines[i].id);
1395 free(lines);
1396 return err;
1399 static const struct got_error *
1400 cmd_blame(int argc, char *argv[])
1402 const struct got_error *error;
1403 struct got_repository *repo = NULL;
1404 char *repo_path = NULL;
1405 char *path = NULL;
1406 struct got_object_id *commit_id = NULL;
1407 char *commit_id_str = NULL;
1408 int ch;
1410 #ifndef PROFILE
1411 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1412 err(1, "pledge");
1413 #endif
1415 while ((ch = getopt(argc, argv, "c:")) != -1) {
1416 switch (ch) {
1417 case 'c':
1418 commit_id_str = optarg;
1419 break;
1420 default:
1421 usage();
1422 /* NOTREACHED */
1426 argc -= optind;
1427 argv += optind;
1429 if (argc == 0) {
1430 usage_blame();
1431 } else if (argc == 1) {
1432 repo_path = getcwd(NULL, 0);
1433 if (repo_path == NULL)
1434 return got_error_from_errno();
1435 path = argv[0];
1436 } else if (argc == 2) {
1437 repo_path = realpath(argv[0], NULL);
1438 if (repo_path == NULL)
1439 return got_error_from_errno();
1440 path = argv[1];
1441 } else
1442 usage_blame();
1444 error = got_repo_open(&repo, repo_path);
1445 free(repo_path);
1446 if (error != NULL)
1447 return error;
1449 if (commit_id_str == NULL) {
1450 struct got_reference *head_ref;
1451 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1452 if (error != NULL)
1453 goto done;
1454 error = got_ref_resolve(&commit_id, repo, head_ref);
1455 got_ref_close(head_ref);
1456 } else {
1457 struct got_object *obj;
1458 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1459 if (error != NULL)
1460 goto done;
1461 commit_id = got_object_get_id(obj);
1462 if (commit_id == NULL)
1463 error = got_error_from_errno();
1464 got_object_close(obj);
1466 if (error != NULL)
1467 goto done;
1469 error = show_blame_view(path, commit_id, repo);
1470 done:
1471 free(commit_id);
1472 if (repo)
1473 got_repo_close(repo);
1474 return error;
1477 static const struct got_error *
1478 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1479 struct got_tree_entry **last_displayed_entry,
1480 struct got_tree_entry **selected_entry, int *ndisplayed,
1481 WINDOW *window, const char *label, const char *parent_path,
1482 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1484 const struct got_error *err = NULL;
1485 struct got_tree_entry *te;
1486 wchar_t *wline;
1487 int width, n;
1489 *ndisplayed = 0;
1491 werase(window);
1493 if (limit == 0)
1494 return NULL;
1496 err = format_line(&wline, &width, label, COLS);
1497 if (err)
1498 return err;
1499 waddwstr(window, wline);
1500 if (width < COLS)
1501 waddch(window, '\n');
1502 if (--limit <= 0)
1503 return NULL;
1504 err = format_line(&wline, &width, parent_path, COLS);
1505 if (err)
1506 return err;
1507 waddwstr(window, wline);
1508 if (width < COLS)
1509 waddch(window, '\n');
1510 if (--limit <= 0)
1511 return NULL;
1512 waddch(window, '\n');
1513 if (--limit <= 0)
1514 return NULL;
1516 te = SIMPLEQ_FIRST(&entries->head);
1517 if (*first_displayed_entry == NULL) {
1518 if (selected == 0) {
1519 wstandout(window);
1520 *selected_entry = NULL;
1522 waddstr(window, " ..\n"); /* parent directory */
1523 if (selected == 0)
1524 wstandend(window);
1525 (*ndisplayed)++;
1526 if (--limit <= 0)
1527 return NULL;
1528 n = 1;
1529 } else {
1530 n = 0;
1531 while (te != *first_displayed_entry)
1532 te = SIMPLEQ_NEXT(te, entry);
1535 while (te) {
1536 char *line = NULL;
1537 if (asprintf(&line, " %s%s",
1538 te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1539 return got_error_from_errno();
1540 err = format_line(&wline, &width, line, COLS);
1541 if (err) {
1542 free(line);
1543 break;
1545 if (n == selected) {
1546 wstandout(window);
1547 *selected_entry = te;
1549 waddwstr(window, wline);
1550 if (width < COLS)
1551 waddch(window, '\n');
1552 if (n == selected)
1553 wstandend(window);
1554 free(line);
1555 n++;
1556 (*ndisplayed)++;
1557 *last_displayed_entry = te;
1558 if (--limit <= 0)
1559 break;
1560 te = SIMPLEQ_NEXT(te, entry);
1563 return err;
1566 static void
1567 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1568 const struct got_tree_entries *entries, int isroot)
1570 struct got_tree_entry *te, *prev;
1571 int i;
1573 if (*first_displayed_entry == NULL)
1574 return;
1576 te = SIMPLEQ_FIRST(&entries->head);
1577 if (*first_displayed_entry == te) {
1578 if (!isroot)
1579 *first_displayed_entry = NULL;
1580 return;
1583 /* XXX this is stupid... switch to TAILQ? */
1584 for (i = 0; i < maxscroll; i++) {
1585 while (te != *first_displayed_entry) {
1586 prev = te;
1587 te = SIMPLEQ_NEXT(te, entry);
1589 *first_displayed_entry = prev;
1590 te = SIMPLEQ_FIRST(&entries->head);
1592 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1593 *first_displayed_entry = NULL;
1596 static void
1597 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1598 struct got_tree_entry *last_displayed_entry,
1599 const struct got_tree_entries *entries)
1601 struct got_tree_entry *next;
1602 int n = 0;
1604 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1605 return;
1607 if (*first_displayed_entry)
1608 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1609 else
1610 next = SIMPLEQ_FIRST(&entries->head);
1611 while (next) {
1612 *first_displayed_entry = next;
1613 if (++n >= maxscroll)
1614 break;
1615 next = SIMPLEQ_NEXT(next, entry);
1619 struct tog_parent_tree {
1620 TAILQ_ENTRY(tog_parent_tree) entry;
1621 struct got_tree_object *tree;
1622 struct got_tree_entry *first_displayed_entry;
1623 struct got_tree_entry *selected_entry;
1624 int selected;
1627 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1629 static const struct got_error *
1630 tree_entry_path(char **path, struct tog_parent_trees *parents,
1631 struct got_tree_entry *te)
1633 const struct got_error *err = NULL;
1634 struct tog_parent_tree *pt;
1635 size_t len = 2; /* for leading slash and NUL */
1637 TAILQ_FOREACH(pt, parents, entry)
1638 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1639 if (te)
1640 len += strlen(te->name);
1642 *path = calloc(1, len);
1643 if (path == NULL)
1644 return got_error_from_errno();
1646 (*path)[0] = '/';
1647 pt = TAILQ_LAST(parents, tog_parent_trees);
1648 while (pt) {
1649 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1650 err = got_error(GOT_ERR_NO_SPACE);
1651 goto done;
1653 if (strlcat(*path, "/", len) >= len) {
1654 err = got_error(GOT_ERR_NO_SPACE);
1655 goto done;
1657 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1659 if (te) {
1660 if (strlcat(*path, te->name, len) >= len) {
1661 err = got_error(GOT_ERR_NO_SPACE);
1662 goto done;
1665 done:
1666 if (err) {
1667 free(*path);
1668 *path = NULL;
1670 return err;
1673 static const struct got_error *
1674 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1675 struct got_object_id *commit_id, struct got_repository *repo)
1677 const struct got_error *err = NULL;
1678 char *path;
1680 err = tree_entry_path(&path, parents, te);
1681 if (err)
1682 return err;
1684 err = show_blame_view(path, commit_id, repo);
1685 free(path);
1686 return err;
1689 static const struct got_error *
1690 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1691 struct got_repository *repo)
1693 const struct got_error *err = NULL;
1694 int ch, done = 0, selected = 0;
1695 struct got_tree_object *tree = root;
1696 const struct got_tree_entries *entries;
1697 struct got_tree_entry *first_displayed_entry = NULL;
1698 struct got_tree_entry *last_displayed_entry = NULL;
1699 struct got_tree_entry *selected_entry = NULL;
1700 char *commit_id_str = NULL, *tree_label = NULL;
1701 int nentries, ndisplayed;
1702 struct tog_parent_trees parents;
1704 TAILQ_INIT(&parents);
1706 err = got_object_id_str(&commit_id_str, commit_id);
1707 if (err != NULL)
1708 goto done;
1710 if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1711 err = got_error_from_errno();
1712 goto done;
1715 if (tog_tree_view.window == NULL) {
1716 tog_tree_view.window = newwin(0, 0, 0, 0);
1717 if (tog_tree_view.window == NULL)
1718 return got_error_from_errno();
1719 keypad(tog_tree_view.window, TRUE);
1721 if (tog_tree_view.panel == NULL) {
1722 tog_tree_view.panel = new_panel(tog_tree_view.window);
1723 if (tog_tree_view.panel == NULL)
1724 return got_error_from_errno();
1725 } else
1726 show_panel(tog_tree_view.panel);
1728 entries = got_object_tree_get_entries(root);
1729 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1730 while (!done) {
1731 char *parent_path;
1732 entries = got_object_tree_get_entries(tree);
1733 nentries = entries->nentries;
1734 if (tree != root)
1735 nentries++; /* '..' directory */
1737 err = tree_entry_path(&parent_path, &parents, NULL);
1738 if (err)
1739 goto done;
1741 err = draw_tree_entries(&first_displayed_entry,
1742 &last_displayed_entry, &selected_entry, &ndisplayed,
1743 tog_tree_view.window, tree_label, parent_path, entries,
1744 selected, LINES, tree == root);
1745 free(parent_path);
1746 if (err)
1747 break;
1749 nodelay(stdscr, FALSE);
1750 ch = wgetch(tog_tree_view.window);
1751 nodelay(stdscr, TRUE);
1752 switch (ch) {
1753 case 'q':
1754 done = 1;
1755 break;
1756 case 'k':
1757 case KEY_UP:
1758 if (selected > 0)
1759 selected--;
1760 if (selected > 0)
1761 break;
1762 tree_scroll_up(&first_displayed_entry, 1,
1763 entries, tree == root);
1764 break;
1765 case KEY_PPAGE:
1766 if (SIMPLEQ_FIRST(&entries->head) ==
1767 first_displayed_entry) {
1768 if (tree != root)
1769 first_displayed_entry = NULL;
1770 selected = 0;
1771 break;
1773 tree_scroll_up(&first_displayed_entry, LINES,
1774 entries, tree == root);
1775 break;
1776 case 'j':
1777 case KEY_DOWN:
1778 if (selected < ndisplayed - 1) {
1779 selected++;
1780 break;
1782 tree_scroll_down(&first_displayed_entry, 1,
1783 last_displayed_entry, entries);
1784 break;
1785 case KEY_NPAGE:
1786 tree_scroll_down(&first_displayed_entry, LINES,
1787 last_displayed_entry, entries);
1788 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1789 break;
1790 /* can't scroll any further; move cursor down */
1791 if (selected < ndisplayed - 1)
1792 selected = ndisplayed - 1;
1793 break;
1794 case KEY_ENTER:
1795 case '\r':
1796 if (selected_entry == NULL) {
1797 struct tog_parent_tree *parent;
1798 case KEY_BACKSPACE:
1799 /* user selected '..' */
1800 if (tree == root)
1801 break;
1802 parent = TAILQ_FIRST(&parents);
1803 TAILQ_REMOVE(&parents, parent, entry);
1804 got_object_tree_close(tree);
1805 tree = parent->tree;
1806 first_displayed_entry =
1807 parent->first_displayed_entry;
1808 selected_entry = parent->selected_entry;
1809 selected = parent->selected;
1810 free(parent);
1811 } else if (S_ISDIR(selected_entry->mode)) {
1812 struct tog_parent_tree *parent;
1813 struct got_tree_object *child;
1814 err = got_object_open_as_tree(
1815 &child, repo, selected_entry->id);
1816 if (err)
1817 goto done;
1818 parent = calloc(1, sizeof(*parent));
1819 if (parent == NULL) {
1820 err = got_error_from_errno();
1821 goto done;
1823 parent->tree = tree;
1824 parent->first_displayed_entry =
1825 first_displayed_entry;
1826 parent->selected_entry = selected_entry;
1827 parent->selected = selected;
1828 TAILQ_INSERT_HEAD(&parents, parent,
1829 entry);
1830 tree = child;
1831 selected = 0;
1832 first_displayed_entry = NULL;
1833 } else if (S_ISREG(selected_entry->mode)) {
1834 err = blame_tree_entry(selected_entry,
1835 &parents, commit_id, repo);
1836 if (err)
1837 goto done;
1839 break;
1840 case KEY_RESIZE:
1841 if (selected > LINES)
1842 selected = ndisplayed - 1;
1843 break;
1844 default:
1845 break;
1848 done:
1849 free(tree_label);
1850 free(commit_id_str);
1851 while (!TAILQ_EMPTY(&parents)) {
1852 struct tog_parent_tree *parent;
1853 parent = TAILQ_FIRST(&parents);
1854 TAILQ_REMOVE(&parents, parent, entry);
1855 free(parent);
1858 if (tree != root)
1859 got_object_tree_close(tree);
1860 return err;
1863 __dead static void
1864 usage_tree(void)
1866 endwin();
1867 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1868 getprogname());
1869 exit(1);
1872 static const struct got_error *
1873 cmd_tree(int argc, char *argv[])
1875 const struct got_error *error;
1876 struct got_repository *repo = NULL;
1877 char *repo_path = NULL;
1878 struct got_object_id *commit_id = NULL;
1879 char *commit_id_arg = NULL;
1880 struct got_commit_object *commit = NULL;
1881 struct got_tree_object *tree = NULL;
1882 int ch;
1884 #ifndef PROFILE
1885 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1886 err(1, "pledge");
1887 #endif
1889 while ((ch = getopt(argc, argv, "c:")) != -1) {
1890 switch (ch) {
1891 case 'c':
1892 commit_id_arg = optarg;
1893 break;
1894 default:
1895 usage();
1896 /* NOTREACHED */
1900 argc -= optind;
1901 argv += optind;
1903 if (argc == 0) {
1904 repo_path = getcwd(NULL, 0);
1905 if (repo_path == NULL)
1906 return got_error_from_errno();
1907 } else if (argc == 1) {
1908 repo_path = realpath(argv[0], NULL);
1909 if (repo_path == NULL)
1910 return got_error_from_errno();
1911 } else
1912 usage_log();
1914 error = got_repo_open(&repo, repo_path);
1915 free(repo_path);
1916 if (error != NULL)
1917 return error;
1919 if (commit_id_arg == NULL) {
1920 error = get_head_commit_id(&commit_id, repo);
1921 if (error != NULL)
1922 goto done;
1923 } else {
1924 struct got_object *obj;
1925 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
1926 if (error == NULL) {
1927 commit_id = got_object_get_id(obj);
1928 if (commit_id == NULL)
1929 error = got_error_from_errno();
1932 if (error != NULL)
1933 goto done;
1935 error = got_object_open_as_commit(&commit, repo, commit_id);
1936 if (error != NULL)
1937 goto done;
1939 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
1940 if (error != NULL)
1941 goto done;
1943 error = show_tree_view(tree, commit_id, repo);
1944 done:
1945 free(commit_id);
1946 if (commit)
1947 got_object_commit_close(commit);
1948 if (tree)
1949 got_object_tree_close(tree);
1950 if (repo)
1951 got_repo_close(repo);
1952 return error;
1954 static void
1955 init_curses(void)
1957 initscr();
1958 cbreak();
1959 noecho();
1960 nonl();
1961 intrflush(stdscr, FALSE);
1962 keypad(stdscr, TRUE);
1963 curs_set(0);
1966 __dead static void
1967 usage(void)
1969 int i;
1971 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
1972 "Available commands:\n", getprogname());
1973 for (i = 0; i < nitems(tog_commands); i++) {
1974 struct tog_cmd *cmd = &tog_commands[i];
1975 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
1977 exit(1);
1980 static char **
1981 make_argv(const char *arg0, const char *arg1)
1983 char **argv;
1984 int argc = (arg1 == NULL ? 1 : 2);
1986 argv = calloc(argc, sizeof(char *));
1987 if (argv == NULL)
1988 err(1, "calloc");
1989 argv[0] = strdup(arg0);
1990 if (argv[0] == NULL)
1991 err(1, "calloc");
1992 if (arg1) {
1993 argv[1] = strdup(arg1);
1994 if (argv[1] == NULL)
1995 err(1, "calloc");
1998 return argv;
2001 int
2002 main(int argc, char *argv[])
2004 const struct got_error *error = NULL;
2005 struct tog_cmd *cmd = NULL;
2006 int ch, hflag = 0;
2007 char **cmd_argv = NULL;
2009 setlocale(LC_ALL, "");
2011 while ((ch = getopt(argc, argv, "h")) != -1) {
2012 switch (ch) {
2013 case 'h':
2014 hflag = 1;
2015 break;
2016 default:
2017 usage();
2018 /* NOTREACHED */
2022 argc -= optind;
2023 argv += optind;
2024 optind = 0;
2025 optreset = 1;
2027 if (argc == 0) {
2028 if (hflag)
2029 usage();
2030 /* Build an argument vector which runs a default command. */
2031 cmd = &tog_commands[0];
2032 cmd_argv = make_argv(cmd->name, NULL);
2033 argc = 1;
2034 } else {
2035 int i;
2037 /* Did the user specific a command? */
2038 for (i = 0; i < nitems(tog_commands); i++) {
2039 if (strncmp(tog_commands[i].name, argv[0],
2040 strlen(argv[0])) == 0) {
2041 cmd = &tog_commands[i];
2042 if (hflag)
2043 tog_commands[i].cmd_usage();
2044 break;
2047 if (cmd == NULL) {
2048 /* Did the user specify a repository? */
2049 char *repo_path = realpath(argv[0], NULL);
2050 if (repo_path) {
2051 struct got_repository *repo;
2052 error = got_repo_open(&repo, repo_path);
2053 if (error == NULL)
2054 got_repo_close(repo);
2055 } else
2056 error = got_error_from_errno();
2057 if (error) {
2058 if (hflag) {
2059 fprintf(stderr, "%s: '%s' is not a "
2060 "known command\n", getprogname(),
2061 argv[0]);
2062 usage();
2064 fprintf(stderr, "%s: '%s' is neither a known "
2065 "command nor a path to a repository\n",
2066 getprogname(), argv[0]);
2067 free(repo_path);
2068 return 1;
2070 cmd = &tog_commands[0];
2071 cmd_argv = make_argv(cmd->name, repo_path);
2072 argc = 2;
2073 free(repo_path);
2077 init_curses();
2079 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2080 if (error)
2081 goto done;
2082 done:
2083 endwin();
2084 free(cmd_argv);
2085 if (error)
2086 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2087 return 0;