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;
164 *widthp = 0;
166 err = mbs2ws(&wline, &wlen, line);
167 if (err)
168 return err;
170 i = 0;
171 while (i < wlen && cols < wlimit) {
172 int width = wcwidth(wline[i]);
173 switch (width) {
174 case 0:
175 i++;
176 break;
177 case 1:
178 case 2:
179 if (cols + width <= wlimit) {
180 cols += width;
181 i++;
183 break;
184 case -1:
185 if (wline[i] == L'\t')
186 cols += TABSIZE - ((cols + 1) % TABSIZE);
187 i++;
188 break;
189 default:
190 err = got_error_from_errno();
191 goto done;
194 wline[i] = L'\0';
195 if (widthp)
196 *widthp = cols;
197 done:
198 if (err)
199 free(wline);
200 else
201 *wlinep = wline;
202 return err;
205 static const struct got_error *
206 draw_commit(struct got_commit_object *commit, struct got_object_id *id)
208 const struct got_error *err = NULL;
209 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
210 char *logmsg0 = NULL, *logmsg = NULL;
211 char *author0 = NULL, *author = NULL;
212 wchar_t *wlogmsg = NULL, *wauthor = NULL;
213 int author_width, logmsg_width;
214 char *newline, *smallerthan;
215 char *line = NULL;
216 int col, limit;
217 static const size_t date_display_cols = 9;
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 author0 = strdup(commit->author);
235 if (author0 == NULL) {
236 err = got_error_from_errno();
237 goto done;
239 author = author0;
240 smallerthan = strchr(author, '<');
241 if (smallerthan)
242 *smallerthan = '\0';
243 else {
244 char *at = strchr(author, '@');
245 if (at)
246 *at = '\0';
248 limit = avail - col;
249 err = format_line(&wauthor, &author_width, author, limit);
250 if (err)
251 goto done;
252 waddwstr(tog_log_view.window, wauthor);
253 col += author_width;
254 while (col <= avail && author_width < author_display_cols + 1) {
255 waddch(tog_log_view.window, ' ');
256 col++;
257 author_width++;
259 if (col > avail)
260 goto done;
262 logmsg0 = strdup(commit->logmsg);
263 if (logmsg0 == NULL) {
264 err = got_error_from_errno();
265 goto done;
267 logmsg = logmsg0;
268 while (*logmsg == '\n')
269 logmsg++;
270 newline = strchr(logmsg, '\n');
271 if (newline)
272 *newline = '\0';
273 limit = avail - col;
274 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
275 if (err)
276 goto done;
277 waddwstr(tog_log_view.window, wlogmsg);
278 col += logmsg_width;
279 while (col <= avail) {
280 waddch(tog_log_view.window, ' ');
281 col++;
283 done:
284 free(logmsg0);
285 free(wlogmsg);
286 free(author0);
287 free(wauthor);
288 free(line);
289 return err;
292 struct commit_queue_entry {
293 TAILQ_ENTRY(commit_queue_entry) entry;
294 struct got_object_id *id;
295 struct got_commit_object *commit;
296 };
297 TAILQ_HEAD(commit_queue, commit_queue_entry);
299 static struct commit_queue_entry *
300 alloc_commit_queue_entry(struct got_commit_object *commit,
301 struct got_object_id *id)
303 struct commit_queue_entry *entry;
305 entry = calloc(1, sizeof(*entry));
306 if (entry == NULL)
307 return NULL;
309 entry->id = id;
310 entry->commit = commit;
311 return entry;
314 static void
315 pop_commit(struct commit_queue *commits)
317 struct commit_queue_entry *entry;
319 entry = TAILQ_FIRST(commits);
320 TAILQ_REMOVE(commits, entry, entry);
321 got_object_commit_close(entry->commit);
322 /* Don't free entry->id! It is owned by the commit graph. */
323 free(entry);
326 static void
327 free_commits(struct commit_queue *commits)
329 while (!TAILQ_EMPTY(commits))
330 pop_commit(commits);
333 static const struct got_error *
334 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
335 struct got_object_id *start_id, struct got_repository *repo)
337 const struct got_error *err = NULL;
338 struct got_object_id *id;
339 struct commit_queue_entry *entry;
341 err = got_commit_graph_iter_start(graph, start_id);
342 if (err)
343 return err;
345 entry = TAILQ_LAST(commits, commit_queue);
346 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
347 int nfetched;
349 /* Start ID's commit is already on the queue; skip over it. */
350 err = got_commit_graph_iter_next(&id, graph);
351 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
352 return err;
354 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
355 if (err)
356 return err;
359 while (1) {
360 struct got_commit_object *commit;
362 err = got_commit_graph_iter_next(&id, graph);
363 if (err) {
364 if (err->code == GOT_ERR_ITER_NEED_MORE)
365 err = NULL;
366 break;
369 err = got_object_open_as_commit(&commit, repo, id);
370 if (err)
371 break;
373 entry = alloc_commit_queue_entry(commit, id);
374 if (entry == NULL) {
375 err = got_error_from_errno();
376 break;
379 TAILQ_INSERT_TAIL(commits, entry, entry);
382 return err;
385 static const struct got_error *
386 fetch_next_commit(struct commit_queue_entry **pentry,
387 struct commit_queue_entry *entry, struct commit_queue *commits,
388 struct got_commit_graph *graph, struct got_repository *repo)
390 const struct got_error *err = NULL;
391 struct got_object_qid *qid;
393 *pentry = NULL;
395 /* Populate commit graph with entry's parent commits. */
396 SIMPLEQ_FOREACH(qid, &entry->commit->parent_ids, entry) {
397 int nfetched;
398 err = got_commit_graph_fetch_commits_up_to(&nfetched,
399 graph, qid->id, repo);
400 if (err)
401 return err;
404 /* Append outstanding commits to queue in graph sort order. */
405 err = queue_commits(graph, commits, entry->id, repo);
406 if (err) {
407 if (err->code == GOT_ERR_ITER_COMPLETED)
408 err = NULL;
409 return err;
412 /* Next entry to display should now be available. */
413 *pentry = TAILQ_NEXT(entry, entry);
414 if (*pentry == NULL)
415 return got_error(GOT_ERR_NO_OBJ);
417 return NULL;
420 static const struct got_error *
421 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
423 const struct got_error *err = NULL;
424 struct got_reference *head_ref;
426 *head_id = NULL;
428 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
429 if (err)
430 return err;
432 err = got_ref_resolve(head_id, repo, head_ref);
433 got_ref_close(head_ref);
434 if (err) {
435 *head_id = NULL;
436 return err;
439 return NULL;
442 static const struct got_error *
443 draw_commits(struct commit_queue_entry **last, struct commit_queue_entry **selected,
444 struct commit_queue_entry *first, int selected_idx, int limit)
446 const struct got_error *err = NULL;
447 struct commit_queue_entry *entry;
448 int ncommits;
449 char *id_str, *header;
450 size_t header_len;
452 entry = first;
453 *selected = NULL;
454 ncommits = 0;
455 while (entry) {
456 if (++ncommits - 1 == selected_idx) {
457 *selected = entry;
458 break;
460 entry = TAILQ_NEXT(entry, entry);
462 if (*selected == NULL)
463 return got_error(GOT_ERR_RANGE);
465 err = got_object_id_str(&id_str, (*selected)->id);
466 if (err)
467 return err;
469 if (asprintf(&header, "commit: %s", id_str) == -1) {
470 err = got_error_from_errno();
471 free(id_str);
472 return err;
475 werase(tog_log_view.window);
477 header_len = strlen(header);
478 if (header_len > COLS) {
479 id_str[COLS + 1] = '\0';
480 header_len = COLS;
482 wprintw(tog_log_view.window, header);
483 while (header_len < COLS) {
484 waddch(tog_log_view.window, ' ');
485 header_len++;
487 free(id_str);
488 free(header);
490 entry = first;
491 *last = first;
492 ncommits = 0;
493 while (entry) {
494 if (ncommits == limit - 1)
495 break;
496 if (ncommits == selected_idx) {
497 wstandout(tog_log_view.window);
498 *selected = entry;
500 err = draw_commit(entry->commit, entry->id);
501 if (ncommits == selected_idx)
502 wstandend(tog_log_view.window);
503 if (err)
504 break;
505 ncommits++;
506 *last = entry;
507 entry = TAILQ_NEXT(entry, entry);
510 update_panels();
511 doupdate();
513 return err;
516 static void
517 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
518 struct commit_queue *commits)
520 struct commit_queue_entry *entry;
521 int nscrolled = 0;
523 entry = TAILQ_FIRST(commits);
524 if (*first_displayed_entry == entry)
525 return;
527 entry = *first_displayed_entry;
528 while (entry && nscrolled < maxscroll) {
529 entry = TAILQ_PREV(entry, commit_queue, entry);
530 if (entry) {
531 *first_displayed_entry = entry;
532 nscrolled++;
537 static const struct got_error *
538 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
539 struct commit_queue_entry *last_displayed_entry,
540 struct commit_queue *commits, struct got_commit_graph *graph,
541 struct got_repository *repo)
543 const struct got_error *err = NULL;
544 struct commit_queue_entry *pentry;
545 int nscrolled = 0;
547 do {
548 pentry = TAILQ_NEXT(last_displayed_entry, entry);
549 if (pentry == NULL) {
550 err = fetch_next_commit(&pentry, last_displayed_entry,
551 commits, graph, repo);
552 if (err || pentry == NULL)
553 break;
555 last_displayed_entry = pentry;
557 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
558 if (pentry == NULL)
559 break;
560 *first_displayed_entry = pentry;
561 } while (++nscrolled < maxscroll);
563 return err;
566 static int
567 num_parents(struct commit_queue_entry *entry)
569 int nparents = 0;
571 while (entry) {
572 entry = TAILQ_NEXT(entry, entry);
573 nparents++;
576 return nparents;
579 static const struct got_error *
580 show_commit(struct commit_queue_entry *entry, struct got_repository *repo)
582 const struct got_error *err;
583 struct got_object *obj1 = NULL, *obj2 = NULL;
584 struct got_object_qid *parent_id;
586 err = got_object_open(&obj2, repo, entry->id);
587 if (err)
588 return err;
590 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
591 if (parent_id) {
592 err = got_object_open(&obj1, repo, parent_id->id);
593 if (err)
594 goto done;
597 err = show_diff_view(obj1, obj2, repo);
598 done:
599 if (obj1)
600 got_object_close(obj1);
601 if (obj2)
602 got_object_close(obj2);
603 return err;
606 static const struct got_error *
607 browse_commit(struct commit_queue_entry *entry, struct got_repository *repo)
609 const struct got_error *err = NULL;
610 struct got_tree_object *tree;
612 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
613 if (err)
614 return err;
616 err = show_tree_view(tree, entry->id, repo);
617 got_object_tree_close(tree);
618 return err;
621 static const struct got_error *
622 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
624 const struct got_error *err = NULL;
625 struct got_object_id *head_id = NULL;
626 int ch, done = 0, selected = 0, nparents, nfetched;
627 struct got_commit_graph *graph;
628 struct commit_queue commits;
629 struct commit_queue_entry *entry = NULL;
630 struct commit_queue_entry *first_displayed_entry = NULL;
631 struct commit_queue_entry *last_displayed_entry = NULL;
632 struct commit_queue_entry *selected_entry = NULL;
634 if (tog_log_view.window == NULL) {
635 tog_log_view.window = newwin(0, 0, 0, 0);
636 if (tog_log_view.window == NULL)
637 return got_error_from_errno();
638 keypad(tog_log_view.window, TRUE);
640 if (tog_log_view.panel == NULL) {
641 tog_log_view.panel = new_panel(tog_log_view.window);
642 if (tog_log_view.panel == NULL)
643 return got_error_from_errno();
644 } else
645 show_panel(tog_log_view.panel);
647 err = get_head_commit_id(&head_id, repo);
648 if (err)
649 return err;
651 TAILQ_INIT(&commits);
653 err = got_commit_graph_open(&graph, head_id, 0, repo);
654 if (err)
655 goto done;
657 /* Populate commit graph with a sufficient number of commits. */
658 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
659 repo);
660 if (err)
661 goto done;
662 err = got_commit_graph_fetch_commits(&nfetched, graph, LINES, repo);
663 if (err)
664 goto done;
666 /*
667 * Open the initial batch of commits, sorted in commit graph order.
668 * We keep all commits open throughout the lifetime of the log view
669 * in order to avoid having to re-fetch commits from disk while
670 * updating the display.
671 */
672 err = queue_commits(graph, &commits, head_id, repo);
673 if (err && err->code != GOT_ERR_ITER_COMPLETED)
674 goto done;
676 /* Find entry corresponding to the first commit to display. */
677 TAILQ_FOREACH(entry, &commits, entry) {
678 if (got_object_id_cmp(entry->id, start_id) == 0) {
679 first_displayed_entry = entry;
680 break;
683 if (first_displayed_entry == NULL) {
684 err = got_error(GOT_ERR_NO_OBJ);
685 goto done;
688 selected_entry = first_displayed_entry;
689 while (!done) {
690 err = draw_commits(&last_displayed_entry, &selected_entry,
691 first_displayed_entry, selected, LINES);
692 if (err)
693 goto done;
695 nodelay(stdscr, FALSE);
696 ch = wgetch(tog_log_view.window);
697 nodelay(stdscr, TRUE);
698 switch (ch) {
699 case ERR:
700 if (errno) {
701 err = got_error_from_errno();
702 goto done;
704 break;
705 case 'q':
706 done = 1;
707 break;
708 case 'k':
709 case KEY_UP:
710 if (selected > 0)
711 selected--;
712 if (selected > 0)
713 break;
714 scroll_up(&first_displayed_entry, 1, &commits);
715 break;
716 case KEY_PPAGE:
717 if (TAILQ_FIRST(&commits) ==
718 first_displayed_entry) {
719 selected = 0;
720 break;
722 scroll_up(&first_displayed_entry, LINES,
723 &commits);
724 break;
725 case 'j':
726 case KEY_DOWN:
727 nparents = num_parents(first_displayed_entry);
728 if (selected < LINES - 2 &&
729 selected < nparents - 1) {
730 selected++;
731 break;
733 err = scroll_down(&first_displayed_entry, 1,
734 last_displayed_entry, &commits, graph,
735 repo);
736 if (err)
737 goto done;
738 break;
739 case KEY_NPAGE:
740 err = scroll_down(&first_displayed_entry, LINES,
741 last_displayed_entry, &commits, graph,
742 repo);
743 if (err)
744 goto done;
745 if (last_displayed_entry->commit->nparents > 0)
746 break;
747 /* can't scroll any further; move cursor down */
748 nparents = num_parents(first_displayed_entry);
749 if (selected < LINES - 2 ||
750 selected < nparents - 1)
751 selected = MIN(LINES - 2, nparents - 1);
752 break;
753 case KEY_RESIZE:
754 if (selected > LINES - 1)
755 selected = LINES - 2;
756 break;
757 case KEY_ENTER:
758 case '\r':
759 err = show_commit(selected_entry, repo);
760 if (err)
761 goto done;
762 show_panel(tog_log_view.panel);
763 break;
764 case 't':
765 err = browse_commit(selected_entry, repo);
766 if (err)
767 goto done;
768 show_panel(tog_log_view.panel);
769 break;
770 default:
771 break;
774 done:
775 free(head_id);
776 if (graph)
777 got_commit_graph_close(graph);
778 free_commits(&commits);
779 return err;
782 static const struct got_error *
783 cmd_log(int argc, char *argv[])
785 const struct got_error *error;
786 struct got_repository *repo;
787 struct got_object_id *start_id = NULL;
788 char *repo_path = NULL;
789 char *start_commit = NULL;
790 int ch;
792 #ifndef PROFILE
793 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
794 err(1, "pledge");
795 #endif
797 while ((ch = getopt(argc, argv, "c:")) != -1) {
798 switch (ch) {
799 case 'c':
800 start_commit = optarg;
801 break;
802 default:
803 usage();
804 /* NOTREACHED */
808 argc -= optind;
809 argv += optind;
811 if (argc == 0) {
812 repo_path = getcwd(NULL, 0);
813 if (repo_path == NULL)
814 return got_error_from_errno();
815 } else if (argc == 1) {
816 repo_path = realpath(argv[0], NULL);
817 if (repo_path == NULL)
818 return got_error_from_errno();
819 } else
820 usage_log();
822 error = got_repo_open(&repo, repo_path);
823 free(repo_path);
824 if (error != NULL)
825 return error;
827 if (start_commit == NULL) {
828 error = get_head_commit_id(&start_id, repo);
829 if (error != NULL)
830 return error;
831 } else {
832 struct got_object *obj;
833 error = got_object_open_by_id_str(&obj, repo, start_commit);
834 if (error == NULL) {
835 start_id = got_object_get_id(obj);
836 if (start_id == NULL)
837 error = got_error_from_errno();
840 if (error != NULL)
841 return error;
842 error = show_log_view(start_id, repo);
843 free(start_id);
844 got_repo_close(repo);
845 return error;
848 __dead static void
849 usage_diff(void)
851 endwin();
852 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
853 getprogname());
854 exit(1);
857 static char *
858 parse_next_line(FILE *f, size_t *len)
860 char *line;
861 size_t linelen;
862 size_t lineno;
863 const char delim[3] = { '\0', '\0', '\0'};
865 line = fparseln(f, &linelen, &lineno, delim, 0);
866 if (len)
867 *len = linelen;
868 return line;
871 static const struct got_error *
872 draw_file(WINDOW *window, FILE *f, int *first_displayed_line,
873 int *last_displayed_line, int *eof, int max_lines)
875 const struct got_error *err;
876 int nlines = 0, nprinted = 0;
877 char *line;
878 size_t len;
879 wchar_t *wline;
880 int width;
882 rewind(f);
883 werase(window);
885 *eof = 0;
886 while (nprinted < max_lines) {
887 line = parse_next_line(f, &len);
888 if (line == NULL) {
889 *eof = 1;
890 break;
892 if (++nlines < *first_displayed_line) {
893 free(line);
894 continue;
897 err = format_line(&wline, &width, line, COLS);
898 if (err) {
899 free(line);
900 return err;
902 waddwstr(window, wline);
903 if (width < COLS)
904 waddch(window, '\n');
905 if (++nprinted == 1)
906 *first_displayed_line = nlines;
907 free(line);
909 *last_displayed_line = nlines;
911 update_panels();
912 doupdate();
914 return NULL;
917 static const struct got_error *
918 show_diff_view(struct got_object *obj1, struct got_object *obj2,
919 struct got_repository *repo)
921 const struct got_error *err;
922 FILE *f;
923 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
924 int eof, i;
926 if (obj1 != NULL && obj2 != NULL &&
927 got_object_get_type(obj1) != got_object_get_type(obj2))
928 return got_error(GOT_ERR_OBJ_TYPE);
930 f = got_opentemp();
931 if (f == NULL)
932 return got_error_from_errno();
934 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
935 case GOT_OBJ_TYPE_BLOB:
936 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
937 break;
938 case GOT_OBJ_TYPE_TREE:
939 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
940 break;
941 case GOT_OBJ_TYPE_COMMIT:
942 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
943 break;
944 default:
945 return got_error(GOT_ERR_OBJ_TYPE);
948 fflush(f);
950 if (tog_diff_view.window == NULL) {
951 tog_diff_view.window = newwin(0, 0, 0, 0);
952 if (tog_diff_view.window == NULL)
953 return got_error_from_errno();
954 keypad(tog_diff_view.window, TRUE);
956 if (tog_diff_view.panel == NULL) {
957 tog_diff_view.panel = new_panel(tog_diff_view.window);
958 if (tog_diff_view.panel == NULL)
959 return got_error_from_errno();
960 } else
961 show_panel(tog_diff_view.panel);
963 while (!done) {
964 err = draw_file(tog_diff_view.window, f, &first_displayed_line,
965 &last_displayed_line, &eof, LINES);
966 if (err)
967 break;
968 nodelay(stdscr, FALSE);
969 ch = wgetch(tog_diff_view.window);
970 nodelay(stdscr, TRUE);
971 switch (ch) {
972 case 'q':
973 done = 1;
974 break;
975 case 'k':
976 case KEY_UP:
977 case KEY_BACKSPACE:
978 if (first_displayed_line > 1)
979 first_displayed_line--;
980 break;
981 case KEY_PPAGE:
982 i = 0;
983 while (i++ < LINES - 1 &&
984 first_displayed_line > 1)
985 first_displayed_line--;
986 break;
987 case 'j':
988 case KEY_DOWN:
989 case KEY_ENTER:
990 case '\r':
991 if (!eof)
992 first_displayed_line++;
993 break;
994 case KEY_NPAGE:
995 case ' ':
996 i = 0;
997 while (!eof && i++ < LINES - 1) {
998 char *line = parse_next_line(f, NULL);
999 first_displayed_line++;
1000 if (line == NULL)
1001 break;
1003 break;
1004 default:
1005 break;
1008 fclose(f);
1009 return err;
1012 static const struct got_error *
1013 cmd_diff(int argc, char *argv[])
1015 const struct got_error *error = NULL;
1016 struct got_repository *repo = NULL;
1017 struct got_object *obj1 = NULL, *obj2 = NULL;
1018 char *repo_path = NULL;
1019 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1020 int ch;
1022 #ifndef PROFILE
1023 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1024 err(1, "pledge");
1025 #endif
1027 while ((ch = getopt(argc, argv, "")) != -1) {
1028 switch (ch) {
1029 default:
1030 usage();
1031 /* NOTREACHED */
1035 argc -= optind;
1036 argv += optind;
1038 if (argc == 0) {
1039 usage_diff(); /* TODO show local worktree changes */
1040 } else if (argc == 2) {
1041 repo_path = getcwd(NULL, 0);
1042 if (repo_path == NULL)
1043 return got_error_from_errno();
1044 obj_id_str1 = argv[0];
1045 obj_id_str2 = argv[1];
1046 } else if (argc == 3) {
1047 repo_path = realpath(argv[0], NULL);
1048 if (repo_path == NULL)
1049 return got_error_from_errno();
1050 obj_id_str1 = argv[1];
1051 obj_id_str2 = argv[2];
1052 } else
1053 usage_diff();
1055 error = got_repo_open(&repo, repo_path);
1056 free(repo_path);
1057 if (error)
1058 goto done;
1060 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1061 if (error)
1062 goto done;
1064 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1065 if (error)
1066 goto done;
1068 error = show_diff_view(obj1, obj2, repo);
1069 done:
1070 got_repo_close(repo);
1071 if (obj1)
1072 got_object_close(obj1);
1073 if (obj2)
1074 got_object_close(obj2);
1075 return error;
1078 __dead static void
1079 usage_blame(void)
1081 endwin();
1082 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1083 getprogname());
1084 exit(1);
1087 struct tog_blame_line {
1088 int annotated;
1089 struct got_object_id *id;
1092 static const struct got_error *
1093 draw_blame(WINDOW *window, FILE *f, const char *path,
1094 struct tog_blame_line *lines, int nlines, int blame_complete,
1095 int selected_line, int *first_displayed_line, int *last_displayed_line,
1096 int *eof, int max_lines)
1098 const struct got_error *err;
1099 int lineno = 0, nprinted = 0;
1100 char *line;
1101 size_t len;
1102 wchar_t *wline;
1103 int width, wlimit;
1104 struct tog_blame_line *blame_line;
1105 struct got_object_id *prev_id = NULL;
1107 rewind(f);
1108 werase(window);
1110 if (asprintf(&line, "[%d-%d/%d] annotation of %s%s",
1111 *first_displayed_line, *last_displayed_line, nlines,
1112 path, blame_complete ? "" : " in progress...") == -1)
1113 return got_error_from_errno();
1114 err = format_line(&wline, &width, line, COLS);
1115 free(line);
1116 if (err)
1117 return err;
1118 waddwstr(window, wline);
1119 if (width < COLS)
1120 waddch(window, '\n');
1122 *eof = 0;
1123 while (nprinted < max_lines - 1) {
1124 line = parse_next_line(f, &len);
1125 if (line == NULL) {
1126 *eof = 1;
1127 break;
1129 if (++lineno < *first_displayed_line) {
1130 free(line);
1131 continue;
1134 wlimit = COLS < 9 ? 0 : COLS - 9;
1135 err = format_line(&wline, &width, line, wlimit);
1136 if (err) {
1137 free(line);
1138 return err;
1141 if (nprinted == selected_line - 1)
1142 wstandout(window);
1144 blame_line = &lines[lineno - 1];
1145 if (blame_line->annotated && prev_id &&
1146 got_object_id_cmp(prev_id, blame_line->id) == 0)
1147 waddstr(window, " ");
1148 else if (blame_line->annotated) {
1149 char *id_str;
1150 err = got_object_id_str(&id_str, blame_line->id);
1151 if (err) {
1152 free(line);
1153 return err;
1155 wprintw(window, "%.8s ", id_str);
1156 free(id_str);
1157 prev_id = blame_line->id;
1158 } else {
1159 waddstr(window, "........ ");
1160 prev_id = NULL;
1163 waddwstr(window, wline);
1164 while (width < wlimit) {
1165 waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1166 width++;
1168 if (nprinted == selected_line - 1)
1169 wstandend(window);
1170 if (++nprinted == 1)
1171 *first_displayed_line = lineno;
1172 free(line);
1174 *last_displayed_line = lineno;
1176 update_panels();
1177 doupdate();
1179 return NULL;
1182 struct tog_blame_cb_args {
1183 pthread_mutex_t *mutex;
1184 struct tog_blame_line *lines; /* one per line */
1185 int nlines;
1187 FILE *f;
1188 const char *path;
1189 WINDOW *window;
1190 int *first_displayed_line;
1191 int *last_displayed_line;
1192 int *selected_line;
1193 int *quit;
1196 static const struct got_error *
1197 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1199 const struct got_error *err = NULL;
1200 struct tog_blame_cb_args *a = arg;
1201 struct tog_blame_line *line;
1202 int eof;
1204 if (nlines != a->nlines ||
1205 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1206 return got_error(GOT_ERR_RANGE);
1208 if (pthread_mutex_lock(a->mutex) != 0)
1209 return got_error_from_errno();
1211 if (*a->quit) { /* user has quit the blame view */
1212 err = got_error(GOT_ERR_ITER_COMPLETED);
1213 goto done;
1216 if (lineno == -1)
1217 goto done; /* no change in this commit */
1219 line = &a->lines[lineno - 1];
1220 if (line->annotated)
1221 goto done;
1223 line->id = got_object_id_dup(id);
1224 if (line->id == NULL) {
1225 err = got_error_from_errno();
1226 goto done;
1228 line->annotated = 1;
1230 err = draw_blame(a->window, a->f, a->path, a->lines, a->nlines, 0,
1231 *a->selected_line, a->first_displayed_line, a->last_displayed_line,
1232 &eof, LINES);
1233 done:
1234 if (pthread_mutex_unlock(a->mutex) != 0)
1235 return got_error_from_errno();
1236 return err;
1239 struct tog_blame_thread_args {
1240 const char *path;
1241 struct got_object_id *commit_id;
1242 struct got_repository *repo;
1243 void *blame_cb_args;
1244 int *complete;
1247 static void *
1248 blame_thread(void *arg)
1250 const struct got_error *err;
1251 struct tog_blame_thread_args *ta = arg;
1252 struct tog_blame_cb_args *a = ta->blame_cb_args;
1253 int eof;
1255 err = got_blame_incremental(ta->path, ta->commit_id, ta->repo,
1256 blame_cb, ta->blame_cb_args);
1257 *ta->complete = 1;
1258 if (err)
1259 return (void *)err;
1261 if (pthread_mutex_lock(a->mutex) != 0)
1262 return (void *)got_error_from_errno();
1264 err = draw_blame(a->window, a->f, a->path, a->lines, a->nlines, 1,
1265 *a->selected_line, a->first_displayed_line, a->last_displayed_line,
1266 &eof, LINES);
1268 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1269 err = got_error_from_errno();
1271 return (void *)err;
1275 static const struct got_error *
1276 open_blamed_commit_and_parent(struct got_object **pobj, struct got_object **obj,
1277 struct tog_blame_line *lines, int first_displayed_line,
1278 int selected_line, struct got_repository *repo)
1280 const struct got_error *err = NULL;
1281 struct tog_blame_line *line;
1282 struct got_commit_object *commit = NULL;
1283 struct got_object_qid *pid;
1285 *pobj = NULL;
1286 *obj = NULL;
1288 line = &lines[first_displayed_line - 1 + selected_line - 1];
1289 if (!line->annotated || line->id == NULL)
1290 return NULL;
1292 err = got_object_open(obj, repo, line->id);
1293 if (err)
1294 goto done;
1296 err = got_object_commit_open(&commit, repo, *obj);
1297 if (err)
1298 goto done;
1300 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1301 if (pid) {
1302 err = got_object_open(pobj, repo, pid->id);
1303 if (err)
1304 goto done;
1306 done:
1307 if (commit)
1308 got_object_commit_close(commit);
1309 return err;
1312 static const struct got_error *
1313 show_blame_view(const char *path, struct got_object_id *commit_id,
1314 struct got_repository *repo)
1316 const struct got_error *err = NULL;
1317 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1318 int selected_line = first_displayed_line;
1319 int eof, i, blame_complete = 0;
1320 struct got_object *obj = NULL, *pobj = NULL;
1321 struct got_blob_object *blob = NULL;
1322 FILE *f = NULL;
1323 size_t filesize, nlines = 0;
1324 struct tog_blame_line *lines = NULL;
1325 pthread_t thread = NULL;
1326 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1327 struct tog_blame_cb_args blame_cb_args;
1328 struct tog_blame_thread_args blame_thread_args;
1329 struct got_repository *blame_thread_repo = NULL;
1331 err = got_object_open_by_path(&obj, repo, commit_id, path);
1332 if (err)
1333 goto done;
1334 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1335 err = got_error(GOT_ERR_OBJ_TYPE);
1336 goto done;
1339 err = got_object_blob_open(&blob, repo, obj, 8192);
1340 if (err)
1341 goto done;
1342 f = got_opentemp();
1343 if (f == NULL) {
1344 err = got_error_from_errno();
1345 goto done;
1347 err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
1348 if (err)
1349 goto done;
1351 lines = calloc(nlines, sizeof(*lines));
1352 if (lines == NULL) {
1353 err = got_error_from_errno();
1354 goto done;
1357 err = got_repo_open(&blame_thread_repo, got_repo_get_path(repo));
1358 if (err)
1359 goto done;
1361 if (tog_blame_view.window == NULL) {
1362 tog_blame_view.window = newwin(0, 0, 0, 0);
1363 if (tog_blame_view.window == NULL)
1364 return got_error_from_errno();
1365 keypad(tog_blame_view.window, TRUE);
1367 if (tog_blame_view.panel == NULL) {
1368 tog_blame_view.panel = new_panel(tog_blame_view.window);
1369 if (tog_blame_view.panel == NULL)
1370 return got_error_from_errno();
1371 } else
1372 show_panel(tog_blame_view.panel);
1374 if (pthread_mutex_init(&mutex, NULL) != 0) {
1375 err = got_error_from_errno();
1376 goto done;
1378 blame_cb_args.lines = lines;
1379 blame_cb_args.nlines = nlines;
1380 blame_cb_args.mutex = &mutex;
1381 blame_cb_args.f = f;
1382 blame_cb_args.path = path;
1383 blame_cb_args.window = tog_blame_view.window;
1384 blame_cb_args.first_displayed_line = &first_displayed_line;
1385 blame_cb_args.selected_line = &selected_line;
1386 blame_cb_args.last_displayed_line = &last_displayed_line;
1387 blame_cb_args.quit = &done;
1389 blame_thread_args.path = path;
1390 blame_thread_args.commit_id = commit_id;
1391 blame_thread_args.repo = blame_thread_repo;
1392 blame_thread_args.blame_cb_args = &blame_cb_args;
1393 blame_thread_args.complete = &blame_complete;
1395 if (pthread_create(&thread, NULL, blame_thread,
1396 &blame_thread_args) != 0) {
1397 err = got_error_from_errno();
1398 goto done;
1401 while (!done) {
1402 if (pthread_mutex_lock(&mutex) != 0) {
1403 err = got_error_from_errno();
1404 goto done;
1406 err = draw_blame(tog_blame_view.window, f, path, lines, nlines,
1407 blame_complete, selected_line, &first_displayed_line,
1408 &last_displayed_line, &eof, LINES);
1409 if (pthread_mutex_unlock(&mutex) != 0) {
1410 err = got_error_from_errno();
1411 goto done;
1413 if (err)
1414 break;
1415 nodelay(stdscr, FALSE);
1416 ch = wgetch(tog_blame_view.window);
1417 nodelay(stdscr, TRUE);
1418 if (pthread_mutex_lock(&mutex) != 0) {
1419 err = got_error_from_errno();
1420 goto done;
1422 switch (ch) {
1423 case 'q':
1424 done = 1;
1425 break;
1426 case 'k':
1427 case KEY_UP:
1428 if (selected_line > 1)
1429 selected_line--;
1430 else if (selected_line == 1 &&
1431 first_displayed_line > 1)
1432 first_displayed_line--;
1433 break;
1434 case KEY_PPAGE:
1435 if (first_displayed_line == 1) {
1436 selected_line = 1;
1437 break;
1439 if (first_displayed_line > LINES - 1)
1440 first_displayed_line -= (LINES - 1);
1441 else
1442 first_displayed_line = 1;
1443 break;
1444 case 'j':
1445 case KEY_DOWN:
1446 if (selected_line < LINES - 1)
1447 selected_line++;
1448 else if (last_displayed_line < nlines)
1449 first_displayed_line++;
1450 break;
1451 case KEY_ENTER:
1452 case '\r':
1453 err = open_blamed_commit_and_parent(&pobj, &obj,
1454 lines, first_displayed_line, selected_line,
1455 repo);
1456 if (err)
1457 goto done;
1458 if (pobj == NULL && obj == NULL)
1459 break;
1460 err = show_diff_view(pobj, obj, repo);
1461 if (pobj) {
1462 got_object_close(pobj);
1463 pobj = NULL;
1465 got_object_close(obj);
1466 obj = NULL;
1467 show_panel(tog_blame_view.panel);
1468 if (err)
1469 goto done;
1470 break;
1471 case KEY_NPAGE:
1472 case ' ':
1473 if (last_displayed_line >= nlines &&
1474 selected_line < LINES - 1) {
1475 selected_line = LINES - 1;
1476 break;
1478 if (last_displayed_line + LINES - 1 <= nlines)
1479 first_displayed_line += LINES - 1;
1480 else
1481 first_displayed_line =
1482 nlines - (LINES - 2);
1483 break;
1484 default:
1485 break;
1487 if (pthread_mutex_unlock(&mutex) != 0) {
1488 err = got_error_from_errno();
1489 goto done;
1492 done:
1493 if (thread) {
1494 if (pthread_join(thread, (void **)&err) != 0)
1495 err = got_error_from_errno();
1496 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1497 err = NULL;
1499 if (blame_thread_repo)
1500 got_repo_close(blame_thread_repo);
1501 if (blob)
1502 got_object_blob_close(blob);
1503 if (pobj)
1504 got_object_close(pobj);
1505 if (obj)
1506 got_object_close(obj);
1507 if (f)
1508 fclose(f);
1509 for (i = 0; i < nlines; i++)
1510 free(lines[i].id);
1511 free(lines);
1512 return err;
1515 static const struct got_error *
1516 cmd_blame(int argc, char *argv[])
1518 const struct got_error *error;
1519 struct got_repository *repo = NULL;
1520 char *repo_path = NULL;
1521 char *path = NULL;
1522 struct got_object_id *commit_id = NULL;
1523 char *commit_id_str = NULL;
1524 int ch;
1526 #ifndef PROFILE
1527 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1528 err(1, "pledge");
1529 #endif
1531 while ((ch = getopt(argc, argv, "c:")) != -1) {
1532 switch (ch) {
1533 case 'c':
1534 commit_id_str = optarg;
1535 break;
1536 default:
1537 usage();
1538 /* NOTREACHED */
1542 argc -= optind;
1543 argv += optind;
1545 if (argc == 0) {
1546 usage_blame();
1547 } else if (argc == 1) {
1548 repo_path = getcwd(NULL, 0);
1549 if (repo_path == NULL)
1550 return got_error_from_errno();
1551 path = argv[0];
1552 } else if (argc == 2) {
1553 repo_path = realpath(argv[0], NULL);
1554 if (repo_path == NULL)
1555 return got_error_from_errno();
1556 path = argv[1];
1557 } else
1558 usage_blame();
1560 error = got_repo_open(&repo, repo_path);
1561 free(repo_path);
1562 if (error != NULL)
1563 return error;
1565 if (commit_id_str == NULL) {
1566 struct got_reference *head_ref;
1567 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1568 if (error != NULL)
1569 goto done;
1570 error = got_ref_resolve(&commit_id, repo, head_ref);
1571 got_ref_close(head_ref);
1572 } else {
1573 struct got_object *obj;
1574 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1575 if (error != NULL)
1576 goto done;
1577 commit_id = got_object_get_id(obj);
1578 if (commit_id == NULL)
1579 error = got_error_from_errno();
1580 got_object_close(obj);
1582 if (error != NULL)
1583 goto done;
1585 error = show_blame_view(path, commit_id, repo);
1586 done:
1587 free(commit_id);
1588 if (repo)
1589 got_repo_close(repo);
1590 return error;
1593 static const struct got_error *
1594 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1595 struct got_tree_entry **last_displayed_entry,
1596 struct got_tree_entry **selected_entry, int *ndisplayed,
1597 WINDOW *window, const char *label, const char *parent_path,
1598 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1600 const struct got_error *err = NULL;
1601 struct got_tree_entry *te;
1602 wchar_t *wline;
1603 int width, n;
1605 *ndisplayed = 0;
1607 werase(window);
1609 if (limit == 0)
1610 return NULL;
1612 err = format_line(&wline, &width, label, COLS);
1613 if (err)
1614 return err;
1615 waddwstr(window, wline);
1616 if (width < COLS)
1617 waddch(window, '\n');
1618 if (--limit <= 0)
1619 return NULL;
1620 err = format_line(&wline, &width, parent_path, COLS);
1621 if (err)
1622 return err;
1623 waddwstr(window, wline);
1624 if (width < COLS)
1625 waddch(window, '\n');
1626 if (--limit <= 0)
1627 return NULL;
1628 waddch(window, '\n');
1629 if (--limit <= 0)
1630 return NULL;
1632 te = SIMPLEQ_FIRST(&entries->head);
1633 if (*first_displayed_entry == NULL) {
1634 if (selected == 0) {
1635 wstandout(window);
1636 *selected_entry = NULL;
1638 waddstr(window, " ..\n"); /* parent directory */
1639 if (selected == 0)
1640 wstandend(window);
1641 (*ndisplayed)++;
1642 if (--limit <= 0)
1643 return NULL;
1644 n = 1;
1645 } else {
1646 n = 0;
1647 while (te != *first_displayed_entry)
1648 te = SIMPLEQ_NEXT(te, entry);
1651 while (te) {
1652 char *line = NULL;
1653 if (asprintf(&line, " %s%s",
1654 te->name, S_ISDIR(te->mode) ? "/" : "") == -1)
1655 return got_error_from_errno();
1656 err = format_line(&wline, &width, line, COLS);
1657 if (err) {
1658 free(line);
1659 break;
1661 if (n == selected) {
1662 wstandout(window);
1663 *selected_entry = te;
1665 waddwstr(window, wline);
1666 if (width < COLS)
1667 waddch(window, '\n');
1668 if (n == selected)
1669 wstandend(window);
1670 free(line);
1671 n++;
1672 (*ndisplayed)++;
1673 *last_displayed_entry = te;
1674 if (--limit <= 0)
1675 break;
1676 te = SIMPLEQ_NEXT(te, entry);
1679 return err;
1682 static void
1683 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1684 const struct got_tree_entries *entries, int isroot)
1686 struct got_tree_entry *te, *prev;
1687 int i;
1689 if (*first_displayed_entry == NULL)
1690 return;
1692 te = SIMPLEQ_FIRST(&entries->head);
1693 if (*first_displayed_entry == te) {
1694 if (!isroot)
1695 *first_displayed_entry = NULL;
1696 return;
1699 /* XXX this is stupid... switch to TAILQ? */
1700 for (i = 0; i < maxscroll; i++) {
1701 while (te != *first_displayed_entry) {
1702 prev = te;
1703 te = SIMPLEQ_NEXT(te, entry);
1705 *first_displayed_entry = prev;
1706 te = SIMPLEQ_FIRST(&entries->head);
1708 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1709 *first_displayed_entry = NULL;
1712 static void
1713 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1714 struct got_tree_entry *last_displayed_entry,
1715 const struct got_tree_entries *entries)
1717 struct got_tree_entry *next;
1718 int n = 0;
1720 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1721 return;
1723 if (*first_displayed_entry)
1724 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1725 else
1726 next = SIMPLEQ_FIRST(&entries->head);
1727 while (next) {
1728 *first_displayed_entry = next;
1729 if (++n >= maxscroll)
1730 break;
1731 next = SIMPLEQ_NEXT(next, entry);
1735 struct tog_parent_tree {
1736 TAILQ_ENTRY(tog_parent_tree) entry;
1737 struct got_tree_object *tree;
1738 struct got_tree_entry *first_displayed_entry;
1739 struct got_tree_entry *selected_entry;
1740 int selected;
1743 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1745 static const struct got_error *
1746 tree_entry_path(char **path, struct tog_parent_trees *parents,
1747 struct got_tree_entry *te)
1749 const struct got_error *err = NULL;
1750 struct tog_parent_tree *pt;
1751 size_t len = 2; /* for leading slash and NUL */
1753 TAILQ_FOREACH(pt, parents, entry)
1754 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1755 if (te)
1756 len += strlen(te->name);
1758 *path = calloc(1, len);
1759 if (path == NULL)
1760 return got_error_from_errno();
1762 (*path)[0] = '/';
1763 pt = TAILQ_LAST(parents, tog_parent_trees);
1764 while (pt) {
1765 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1766 err = got_error(GOT_ERR_NO_SPACE);
1767 goto done;
1769 if (strlcat(*path, "/", len) >= len) {
1770 err = got_error(GOT_ERR_NO_SPACE);
1771 goto done;
1773 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1775 if (te) {
1776 if (strlcat(*path, te->name, len) >= len) {
1777 err = got_error(GOT_ERR_NO_SPACE);
1778 goto done;
1781 done:
1782 if (err) {
1783 free(*path);
1784 *path = NULL;
1786 return err;
1789 static const struct got_error *
1790 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1791 struct got_object_id *commit_id, struct got_repository *repo)
1793 const struct got_error *err = NULL;
1794 char *path;
1796 err = tree_entry_path(&path, parents, te);
1797 if (err)
1798 return err;
1800 err = show_blame_view(path, commit_id, repo);
1801 free(path);
1802 return err;
1805 static const struct got_error *
1806 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
1807 struct got_repository *repo)
1809 const struct got_error *err = NULL;
1810 int ch, done = 0, selected = 0;
1811 struct got_tree_object *tree = root;
1812 const struct got_tree_entries *entries;
1813 struct got_tree_entry *first_displayed_entry = NULL;
1814 struct got_tree_entry *last_displayed_entry = NULL;
1815 struct got_tree_entry *selected_entry = NULL;
1816 char *commit_id_str = NULL, *tree_label = NULL;
1817 int nentries, ndisplayed;
1818 struct tog_parent_trees parents;
1820 TAILQ_INIT(&parents);
1822 err = got_object_id_str(&commit_id_str, commit_id);
1823 if (err != NULL)
1824 goto done;
1826 if (asprintf(&tree_label, "tree of commit %s", commit_id_str) == -1) {
1827 err = got_error_from_errno();
1828 goto done;
1831 if (tog_tree_view.window == NULL) {
1832 tog_tree_view.window = newwin(0, 0, 0, 0);
1833 if (tog_tree_view.window == NULL)
1834 return got_error_from_errno();
1835 keypad(tog_tree_view.window, TRUE);
1837 if (tog_tree_view.panel == NULL) {
1838 tog_tree_view.panel = new_panel(tog_tree_view.window);
1839 if (tog_tree_view.panel == NULL)
1840 return got_error_from_errno();
1841 } else
1842 show_panel(tog_tree_view.panel);
1844 entries = got_object_tree_get_entries(root);
1845 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
1846 while (!done) {
1847 char *parent_path;
1848 entries = got_object_tree_get_entries(tree);
1849 nentries = entries->nentries;
1850 if (tree != root)
1851 nentries++; /* '..' directory */
1853 err = tree_entry_path(&parent_path, &parents, NULL);
1854 if (err)
1855 goto done;
1857 err = draw_tree_entries(&first_displayed_entry,
1858 &last_displayed_entry, &selected_entry, &ndisplayed,
1859 tog_tree_view.window, tree_label, parent_path, entries,
1860 selected, LINES, tree == root);
1861 free(parent_path);
1862 if (err)
1863 break;
1865 nodelay(stdscr, FALSE);
1866 ch = wgetch(tog_tree_view.window);
1867 nodelay(stdscr, TRUE);
1868 switch (ch) {
1869 case 'q':
1870 done = 1;
1871 break;
1872 case 'k':
1873 case KEY_UP:
1874 if (selected > 0)
1875 selected--;
1876 if (selected > 0)
1877 break;
1878 tree_scroll_up(&first_displayed_entry, 1,
1879 entries, tree == root);
1880 break;
1881 case KEY_PPAGE:
1882 if (SIMPLEQ_FIRST(&entries->head) ==
1883 first_displayed_entry) {
1884 if (tree != root)
1885 first_displayed_entry = NULL;
1886 selected = 0;
1887 break;
1889 tree_scroll_up(&first_displayed_entry, LINES,
1890 entries, tree == root);
1891 break;
1892 case 'j':
1893 case KEY_DOWN:
1894 if (selected < ndisplayed - 1) {
1895 selected++;
1896 break;
1898 tree_scroll_down(&first_displayed_entry, 1,
1899 last_displayed_entry, entries);
1900 break;
1901 case KEY_NPAGE:
1902 tree_scroll_down(&first_displayed_entry, LINES,
1903 last_displayed_entry, entries);
1904 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
1905 break;
1906 /* can't scroll any further; move cursor down */
1907 if (selected < ndisplayed - 1)
1908 selected = ndisplayed - 1;
1909 break;
1910 case KEY_ENTER:
1911 case '\r':
1912 if (selected_entry == NULL) {
1913 struct tog_parent_tree *parent;
1914 case KEY_BACKSPACE:
1915 /* user selected '..' */
1916 if (tree == root)
1917 break;
1918 parent = TAILQ_FIRST(&parents);
1919 TAILQ_REMOVE(&parents, parent, entry);
1920 got_object_tree_close(tree);
1921 tree = parent->tree;
1922 first_displayed_entry =
1923 parent->first_displayed_entry;
1924 selected_entry = parent->selected_entry;
1925 selected = parent->selected;
1926 free(parent);
1927 } else if (S_ISDIR(selected_entry->mode)) {
1928 struct tog_parent_tree *parent;
1929 struct got_tree_object *child;
1930 err = got_object_open_as_tree(
1931 &child, repo, selected_entry->id);
1932 if (err)
1933 goto done;
1934 parent = calloc(1, sizeof(*parent));
1935 if (parent == NULL) {
1936 err = got_error_from_errno();
1937 goto done;
1939 parent->tree = tree;
1940 parent->first_displayed_entry =
1941 first_displayed_entry;
1942 parent->selected_entry = selected_entry;
1943 parent->selected = selected;
1944 TAILQ_INSERT_HEAD(&parents, parent,
1945 entry);
1946 tree = child;
1947 selected = 0;
1948 first_displayed_entry = NULL;
1949 } else if (S_ISREG(selected_entry->mode)) {
1950 err = blame_tree_entry(selected_entry,
1951 &parents, commit_id, repo);
1952 if (err)
1953 goto done;
1955 break;
1956 case KEY_RESIZE:
1957 if (selected > LINES)
1958 selected = ndisplayed - 1;
1959 break;
1960 default:
1961 break;
1964 done:
1965 free(tree_label);
1966 free(commit_id_str);
1967 while (!TAILQ_EMPTY(&parents)) {
1968 struct tog_parent_tree *parent;
1969 parent = TAILQ_FIRST(&parents);
1970 TAILQ_REMOVE(&parents, parent, entry);
1971 free(parent);
1974 if (tree != root)
1975 got_object_tree_close(tree);
1976 return err;
1979 __dead static void
1980 usage_tree(void)
1982 endwin();
1983 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
1984 getprogname());
1985 exit(1);
1988 static const struct got_error *
1989 cmd_tree(int argc, char *argv[])
1991 const struct got_error *error;
1992 struct got_repository *repo = NULL;
1993 char *repo_path = NULL;
1994 struct got_object_id *commit_id = NULL;
1995 char *commit_id_arg = NULL;
1996 struct got_commit_object *commit = NULL;
1997 struct got_tree_object *tree = NULL;
1998 int ch;
2000 #ifndef PROFILE
2001 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2002 err(1, "pledge");
2003 #endif
2005 while ((ch = getopt(argc, argv, "c:")) != -1) {
2006 switch (ch) {
2007 case 'c':
2008 commit_id_arg = optarg;
2009 break;
2010 default:
2011 usage();
2012 /* NOTREACHED */
2016 argc -= optind;
2017 argv += optind;
2019 if (argc == 0) {
2020 repo_path = getcwd(NULL, 0);
2021 if (repo_path == NULL)
2022 return got_error_from_errno();
2023 } else if (argc == 1) {
2024 repo_path = realpath(argv[0], NULL);
2025 if (repo_path == NULL)
2026 return got_error_from_errno();
2027 } else
2028 usage_log();
2030 error = got_repo_open(&repo, repo_path);
2031 free(repo_path);
2032 if (error != NULL)
2033 return error;
2035 if (commit_id_arg == NULL) {
2036 error = get_head_commit_id(&commit_id, repo);
2037 if (error != NULL)
2038 goto done;
2039 } else {
2040 struct got_object *obj;
2041 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2042 if (error == NULL) {
2043 commit_id = got_object_get_id(obj);
2044 if (commit_id == NULL)
2045 error = got_error_from_errno();
2048 if (error != NULL)
2049 goto done;
2051 error = got_object_open_as_commit(&commit, repo, commit_id);
2052 if (error != NULL)
2053 goto done;
2055 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2056 if (error != NULL)
2057 goto done;
2059 error = show_tree_view(tree, commit_id, repo);
2060 done:
2061 free(commit_id);
2062 if (commit)
2063 got_object_commit_close(commit);
2064 if (tree)
2065 got_object_tree_close(tree);
2066 if (repo)
2067 got_repo_close(repo);
2068 return error;
2070 static void
2071 init_curses(void)
2073 initscr();
2074 cbreak();
2075 noecho();
2076 nonl();
2077 intrflush(stdscr, FALSE);
2078 keypad(stdscr, TRUE);
2079 curs_set(0);
2082 __dead static void
2083 usage(void)
2085 int i;
2087 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2088 "Available commands:\n", getprogname());
2089 for (i = 0; i < nitems(tog_commands); i++) {
2090 struct tog_cmd *cmd = &tog_commands[i];
2091 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2093 exit(1);
2096 static char **
2097 make_argv(const char *arg0, const char *arg1)
2099 char **argv;
2100 int argc = (arg1 == NULL ? 1 : 2);
2102 argv = calloc(argc, sizeof(char *));
2103 if (argv == NULL)
2104 err(1, "calloc");
2105 argv[0] = strdup(arg0);
2106 if (argv[0] == NULL)
2107 err(1, "calloc");
2108 if (arg1) {
2109 argv[1] = strdup(arg1);
2110 if (argv[1] == NULL)
2111 err(1, "calloc");
2114 return argv;
2117 int
2118 main(int argc, char *argv[])
2120 const struct got_error *error = NULL;
2121 struct tog_cmd *cmd = NULL;
2122 int ch, hflag = 0;
2123 char **cmd_argv = NULL;
2125 setlocale(LC_ALL, "");
2127 while ((ch = getopt(argc, argv, "h")) != -1) {
2128 switch (ch) {
2129 case 'h':
2130 hflag = 1;
2131 break;
2132 default:
2133 usage();
2134 /* NOTREACHED */
2138 argc -= optind;
2139 argv += optind;
2140 optind = 0;
2141 optreset = 1;
2143 if (argc == 0) {
2144 if (hflag)
2145 usage();
2146 /* Build an argument vector which runs a default command. */
2147 cmd = &tog_commands[0];
2148 cmd_argv = make_argv(cmd->name, NULL);
2149 argc = 1;
2150 } else {
2151 int i;
2153 /* Did the user specific a command? */
2154 for (i = 0; i < nitems(tog_commands); i++) {
2155 if (strncmp(tog_commands[i].name, argv[0],
2156 strlen(argv[0])) == 0) {
2157 cmd = &tog_commands[i];
2158 if (hflag)
2159 tog_commands[i].cmd_usage();
2160 break;
2163 if (cmd == NULL) {
2164 /* Did the user specify a repository? */
2165 char *repo_path = realpath(argv[0], NULL);
2166 if (repo_path) {
2167 struct got_repository *repo;
2168 error = got_repo_open(&repo, repo_path);
2169 if (error == NULL)
2170 got_repo_close(repo);
2171 } else
2172 error = got_error_from_errno();
2173 if (error) {
2174 if (hflag) {
2175 fprintf(stderr, "%s: '%s' is not a "
2176 "known command\n", getprogname(),
2177 argv[0]);
2178 usage();
2180 fprintf(stderr, "%s: '%s' is neither a known "
2181 "command nor a path to a repository\n",
2182 getprogname(), argv[0]);
2183 free(repo_path);
2184 return 1;
2186 cmd = &tog_commands[0];
2187 cmd_argv = make_argv(cmd->name, repo_path);
2188 argc = 2;
2189 free(repo_path);
2193 init_curses();
2195 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2196 if (error)
2197 goto done;
2198 done:
2199 endwin();
2200 free(cmd_argv);
2201 if (error)
2202 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2203 return 0;