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, struct got_object_id *id, 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;
1106 char *id_str;
1108 err = got_object_id_str(&id_str, id);
1109 if (err)
1110 return err;
1112 rewind(f);
1113 werase(window);
1115 if (asprintf(&line, "commit: %s", id_str) == -1) {
1116 err = got_error_from_errno();
1117 free(id_str);
1118 return err;
1121 err = format_line(&wline, &width, line, COLS);
1122 free(line);
1123 waddwstr(window, wline);
1124 if (width < COLS)
1125 waddch(window, '\n');
1127 if (asprintf(&line, "[%d/%d] %s%s",
1128 *first_displayed_line - 1 + selected_line, nlines,
1129 path, blame_complete ? "" : " annotating...") == -1) {
1130 free(id_str);
1131 return got_error_from_errno();
1133 free(id_str);
1134 err = format_line(&wline, &width, line, COLS);
1135 free(line);
1136 if (err)
1137 return err;
1138 waddwstr(window, wline);
1139 if (width < COLS)
1140 waddch(window, '\n');
1142 *eof = 0;
1143 while (nprinted < max_lines - 2) {
1144 line = parse_next_line(f, &len);
1145 if (line == NULL) {
1146 *eof = 1;
1147 break;
1149 if (++lineno < *first_displayed_line) {
1150 free(line);
1151 continue;
1154 wlimit = COLS < 9 ? 0 : COLS - 9;
1155 err = format_line(&wline, &width, line, wlimit);
1156 if (err) {
1157 free(line);
1158 return err;
1161 if (nprinted == selected_line - 1)
1162 wstandout(window);
1164 blame_line = &lines[lineno - 1];
1165 if (blame_line->annotated && prev_id &&
1166 got_object_id_cmp(prev_id, blame_line->id) == 0)
1167 waddstr(window, " ");
1168 else if (blame_line->annotated) {
1169 char *id_str;
1170 err = got_object_id_str(&id_str, blame_line->id);
1171 if (err) {
1172 free(line);
1173 return err;
1175 wprintw(window, "%.8s ", id_str);
1176 free(id_str);
1177 prev_id = blame_line->id;
1178 } else {
1179 waddstr(window, "........ ");
1180 prev_id = NULL;
1183 waddwstr(window, wline);
1184 while (width < wlimit) {
1185 waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1186 width++;
1188 if (nprinted == selected_line - 1)
1189 wstandend(window);
1190 if (++nprinted == 1)
1191 *first_displayed_line = lineno;
1192 free(line);
1194 *last_displayed_line = lineno;
1196 update_panels();
1197 doupdate();
1199 return NULL;
1202 struct tog_blame_cb_args {
1203 pthread_mutex_t *mutex;
1204 struct tog_blame_line *lines; /* one per line */
1205 int nlines;
1207 struct got_object_id *commit_id;
1208 FILE *f;
1209 const char *path;
1210 int *first_displayed_line;
1211 int *last_displayed_line;
1212 int *selected_line;
1213 int *quit;
1216 static const struct got_error *
1217 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1219 const struct got_error *err = NULL;
1220 struct tog_blame_cb_args *a = arg;
1221 struct tog_blame_line *line;
1222 int eof;
1224 if (nlines != a->nlines ||
1225 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1226 return got_error(GOT_ERR_RANGE);
1228 if (pthread_mutex_lock(a->mutex) != 0)
1229 return got_error_from_errno();
1231 if (*a->quit) { /* user has quit the blame view */
1232 err = got_error(GOT_ERR_ITER_COMPLETED);
1233 goto done;
1236 if (lineno == -1)
1237 goto done; /* no change in this commit */
1239 line = &a->lines[lineno - 1];
1240 if (line->annotated)
1241 goto done;
1243 line->id = got_object_id_dup(id);
1244 if (line->id == NULL) {
1245 err = got_error_from_errno();
1246 goto done;
1248 line->annotated = 1;
1250 err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1251 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1252 a->last_displayed_line, &eof, LINES);
1253 done:
1254 if (pthread_mutex_unlock(a->mutex) != 0)
1255 return got_error_from_errno();
1256 return err;
1259 struct tog_blame_thread_args {
1260 const char *path;
1261 struct got_repository *repo;
1262 struct tog_blame_cb_args *cb_args;
1263 int *complete;
1266 static void *
1267 blame_thread(void *arg)
1269 const struct got_error *err;
1270 struct tog_blame_thread_args *ta = arg;
1271 struct tog_blame_cb_args *a = ta->cb_args;
1272 int eof;
1274 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1275 blame_cb, ta->cb_args);
1276 *ta->complete = 1;
1277 if (err)
1278 return (void *)err;
1280 if (pthread_mutex_lock(a->mutex) != 0)
1281 return (void *)got_error_from_errno();
1283 err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1284 a->lines, a->nlines, 1, *a->selected_line, a->first_displayed_line,
1285 a->last_displayed_line, &eof, LINES);
1287 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1288 err = got_error_from_errno();
1290 return (void *)err;
1293 static struct got_object_id *
1294 get_selected_commit_id(struct tog_blame_line *lines,
1295 int first_displayed_line, int selected_line)
1297 struct tog_blame_line *line;
1299 line = &lines[first_displayed_line - 1 + selected_line - 1];
1300 if (!line->annotated)
1301 return NULL;
1303 return line->id;
1306 static const struct got_error *
1307 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1308 struct tog_blame_line *lines, int first_displayed_line,
1309 int selected_line, struct got_repository *repo)
1311 const struct got_error *err = NULL;
1312 struct got_commit_object *commit = NULL;
1313 struct got_object_id *selected_id;
1314 struct got_object_qid *pid;
1316 *pobj = NULL;
1317 *obj = NULL;
1319 selected_id = get_selected_commit_id(lines,
1320 first_displayed_line, selected_line);
1321 if (selected_id == NULL)
1322 return NULL;
1324 err = got_object_open(obj, repo, selected_id);
1325 if (err)
1326 goto done;
1328 err = got_object_commit_open(&commit, repo, *obj);
1329 if (err)
1330 goto done;
1332 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1333 if (pid) {
1334 err = got_object_open(pobj, repo, pid->id);
1335 if (err)
1336 goto done;
1338 done:
1339 if (commit)
1340 got_object_commit_close(commit);
1341 return err;
1344 struct tog_blame {
1345 FILE *f;
1346 size_t filesize;
1347 struct tog_blame_line *lines;
1348 size_t nlines;
1349 pthread_t thread;
1350 struct tog_blame_thread_args thread_args;
1351 struct tog_blame_cb_args cb_args;
1352 const char *path;
1353 struct got_object_id *commit_id;
1356 static const struct got_error *
1357 stop_blame(struct tog_blame *blame)
1359 const struct got_error *err = NULL;
1360 int i;
1362 if (blame->thread) {
1363 if (pthread_join(blame->thread, (void **)&err) != 0)
1364 err = got_error_from_errno();
1365 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1366 err = NULL;
1367 blame->thread = NULL;
1369 if (blame->thread_args.repo) {
1370 got_repo_close(blame->thread_args.repo);
1371 blame->thread_args.repo = NULL;
1373 if (blame->f) {
1374 fclose(blame->f);
1375 blame->f = NULL;
1377 for (i = 0; i < blame->nlines; i++)
1378 free(blame->lines[i].id);
1379 free(blame->lines);
1380 blame->lines = NULL;
1381 free(blame->commit_id);
1382 blame->commit_id = NULL;
1384 return err;
1387 static const struct got_error *
1388 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex, int *blame_complete,
1389 int *first_displayed_line, int *last_displayed_line,
1390 int *selected_line, int *done, const char *path,
1391 struct got_object_id *commit_id,
1392 struct got_repository *repo)
1394 const struct got_error *err = NULL;
1395 struct got_blob_object *blob = NULL;
1396 struct got_repository *thread_repo = NULL;
1397 struct got_object *obj;
1399 err = got_object_open_by_path(&obj, repo, commit_id, path);
1400 if (err)
1401 goto done;
1402 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1403 err = got_error(GOT_ERR_OBJ_TYPE);
1404 goto done;
1407 err = got_object_blob_open(&blob, repo, obj, 8192);
1408 if (err)
1409 goto done;
1410 blame->f = got_opentemp();
1411 if (blame->f == NULL) {
1412 err = got_error_from_errno();
1413 goto done;
1415 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1416 blame->f, blob);
1417 if (err)
1418 goto done;
1420 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1421 if (blame->lines == NULL) {
1422 err = got_error_from_errno();
1423 goto done;
1426 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1427 if (err)
1428 goto done;
1430 blame->cb_args.lines = blame->lines;
1431 blame->cb_args.nlines = blame->nlines;
1432 blame->cb_args.mutex = mutex;
1433 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1434 if (blame->cb_args.commit_id == NULL) {
1435 err = got_error_from_errno();
1436 goto done;
1438 blame->cb_args.f = blame->f;
1439 blame->cb_args.path = path;
1440 blame->cb_args.first_displayed_line = first_displayed_line;
1441 blame->cb_args.selected_line = selected_line;
1442 blame->cb_args.last_displayed_line = last_displayed_line;
1443 blame->cb_args.quit = done;
1445 blame->thread_args.path = path;
1446 blame->thread_args.repo = thread_repo;
1447 blame->thread_args.cb_args = &blame->cb_args;
1448 blame->thread_args.complete = blame_complete;
1449 *blame_complete = 0;
1451 if (pthread_create(&blame->thread, NULL, blame_thread,
1452 &blame->thread_args) != 0) {
1453 err = got_error_from_errno();
1454 goto done;
1457 done:
1458 if (blob)
1459 got_object_blob_close(blob);
1460 if (obj)
1461 got_object_close(obj);
1462 if (err)
1463 stop_blame(blame);
1464 return err;
1467 static const struct got_error *
1468 show_blame_view(const char *path, struct got_object_id *commit_id,
1469 struct got_repository *repo)
1471 const struct got_error *err = NULL, *thread_err = NULL;
1472 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1473 int selected_line = first_displayed_line;
1474 int eof, blame_complete = 0;
1475 struct got_object *obj = NULL, *pobj = NULL;
1476 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1477 struct tog_blame blame;
1478 int blame_running = 0;
1479 struct got_object_id_queue blamed_commits;
1480 struct got_object_qid *blamed_commit = NULL;
1482 SIMPLEQ_INIT(&blamed_commits);
1484 if (pthread_mutex_init(&mutex, NULL) != 0) {
1485 err = got_error_from_errno();
1486 goto done;
1489 err = got_object_qid_alloc(&blamed_commit, commit_id);
1490 if (err)
1491 goto done;
1492 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1494 if (tog_blame_view.window == NULL) {
1495 tog_blame_view.window = newwin(0, 0, 0, 0);
1496 if (tog_blame_view.window == NULL)
1497 return got_error_from_errno();
1498 keypad(tog_blame_view.window, TRUE);
1500 if (tog_blame_view.panel == NULL) {
1501 tog_blame_view.panel = new_panel(tog_blame_view.window);
1502 if (tog_blame_view.panel == NULL)
1503 return got_error_from_errno();
1504 } else
1505 show_panel(tog_blame_view.panel);
1507 memset(&blame, 0, sizeof(blame));
1508 err = run_blame(&blame, &mutex, &blame_complete,
1509 &first_displayed_line, &last_displayed_line,
1510 &selected_line, &done, path, blamed_commit->id, repo);
1511 if (err)
1512 return err;
1514 while (!done) {
1515 if (pthread_mutex_lock(&mutex) != 0) {
1516 err = got_error_from_errno();
1517 goto done;
1519 err = draw_blame(tog_blame_view.window, blamed_commit->id,
1520 blame.f, path, blame.lines, blame.nlines, blame_complete,
1521 selected_line, &first_displayed_line, &last_displayed_line,
1522 &eof, LINES);
1523 if (pthread_mutex_unlock(&mutex) != 0) {
1524 err = got_error_from_errno();
1525 goto done;
1527 if (err)
1528 break;
1529 nodelay(stdscr, FALSE);
1530 ch = wgetch(tog_blame_view.window);
1531 nodelay(stdscr, TRUE);
1532 if (pthread_mutex_lock(&mutex) != 0) {
1533 err = got_error_from_errno();
1534 goto done;
1536 switch (ch) {
1537 case 'q':
1538 done = 1;
1539 break;
1540 case 'k':
1541 case KEY_UP:
1542 if (selected_line > 1)
1543 selected_line--;
1544 else if (selected_line == 1 &&
1545 first_displayed_line > 1)
1546 first_displayed_line--;
1547 break;
1548 case KEY_PPAGE:
1549 if (first_displayed_line == 1) {
1550 selected_line = 1;
1551 break;
1553 if (first_displayed_line > LINES - 2)
1554 first_displayed_line -= (LINES - 2);
1555 else
1556 first_displayed_line = 1;
1557 break;
1558 case 'j':
1559 case KEY_DOWN:
1560 if (selected_line < LINES - 2)
1561 selected_line++;
1562 else if (last_displayed_line < blame.nlines)
1563 first_displayed_line++;
1564 break;
1565 case 'b': {
1566 struct got_object_id *id;
1567 id = get_selected_commit_id(blame.lines,
1568 first_displayed_line, selected_line);
1569 if (id == NULL || got_object_id_cmp(id,
1570 blamed_commit->id) == 0)
1571 break;
1572 err = open_selected_commit(&pobj, &obj,
1573 blame.lines, first_displayed_line,
1574 selected_line, repo);
1575 if (err)
1576 break;
1577 if (pobj == NULL && obj == NULL)
1578 break;
1579 done = 1;
1580 if (pthread_mutex_unlock(&mutex) != 0) {
1581 err = got_error_from_errno();
1582 goto done;
1584 thread_err = stop_blame(&blame);
1585 blame_running = 0;
1586 done = 0;
1587 if (pthread_mutex_lock(&mutex) != 0) {
1588 err = got_error_from_errno();
1589 goto done;
1591 if (thread_err)
1592 break;
1593 id = got_object_get_id(obj);
1594 if (id == NULL) {
1595 err = got_error_from_errno();
1596 break;
1598 err = got_object_qid_alloc(&blamed_commit, id);
1599 free(id);
1600 if (err)
1601 goto done;
1602 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1603 blamed_commit, entry);
1604 err = run_blame(&blame, &mutex,
1605 &blame_complete, &first_displayed_line,
1606 &last_displayed_line, &selected_line,
1607 &done, path, blamed_commit->id, repo);
1608 if (err)
1609 break;
1610 blame_running = 1;
1611 break;
1613 case 'B': {
1614 struct got_object_qid *first;
1615 first = SIMPLEQ_FIRST(&blamed_commits);
1616 if (!got_object_id_cmp(first->id, commit_id))
1617 break;
1618 done = 1;
1619 if (pthread_mutex_unlock(&mutex) != 0) {
1620 err = got_error_from_errno();
1621 goto done;
1623 thread_err = stop_blame(&blame);
1624 blame_running = 0;
1625 done = 0;
1626 if (pthread_mutex_lock(&mutex) != 0) {
1627 err = got_error_from_errno();
1628 goto done;
1630 if (thread_err)
1631 break;
1632 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1633 got_object_qid_free(blamed_commit);
1634 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1635 err = run_blame(&blame, &mutex,
1636 &blame_complete, &first_displayed_line,
1637 &last_displayed_line, &selected_line,
1638 &done, path, blamed_commit->id, repo);
1639 if (err)
1640 break;
1641 blame_running = 1;
1642 break;
1644 case KEY_ENTER:
1645 case '\r':
1646 err = open_selected_commit(&pobj, &obj,
1647 blame.lines, first_displayed_line,
1648 selected_line, repo);
1649 if (err)
1650 break;
1651 if (pobj == NULL && obj == NULL)
1652 break;
1653 err = show_diff_view(pobj, obj, repo);
1654 if (pobj) {
1655 got_object_close(pobj);
1656 pobj = NULL;
1658 got_object_close(obj);
1659 obj = NULL;
1660 show_panel(tog_blame_view.panel);
1661 if (err)
1662 break;
1663 break;
1664 case KEY_NPAGE:
1665 case ' ':
1666 if (last_displayed_line >= blame.nlines &&
1667 selected_line < LINES - 2) {
1668 selected_line = LINES - 2;
1669 break;
1671 if (last_displayed_line + LINES - 2 <=
1672 blame.nlines)
1673 first_displayed_line += LINES - 2;
1674 else
1675 first_displayed_line =
1676 blame.nlines - (LINES - 3);
1677 break;
1678 default:
1679 break;
1681 if (pthread_mutex_unlock(&mutex) != 0)
1682 err = got_error_from_errno();
1683 if (err || thread_err)
1684 break;
1686 done:
1687 if (pobj)
1688 got_object_close(pobj);
1689 if (blame_running)
1690 thread_err = stop_blame(&blame);
1691 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1692 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1693 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1694 got_object_qid_free(blamed_commit);
1696 return thread_err ? thread_err : err;
1699 static const struct got_error *
1700 cmd_blame(int argc, char *argv[])
1702 const struct got_error *error;
1703 struct got_repository *repo = NULL;
1704 char *repo_path = NULL;
1705 char *path = NULL;
1706 struct got_object_id *commit_id = NULL;
1707 char *commit_id_str = NULL;
1708 int ch;
1710 #ifndef PROFILE
1711 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1712 err(1, "pledge");
1713 #endif
1715 while ((ch = getopt(argc, argv, "c:")) != -1) {
1716 switch (ch) {
1717 case 'c':
1718 commit_id_str = optarg;
1719 break;
1720 default:
1721 usage();
1722 /* NOTREACHED */
1726 argc -= optind;
1727 argv += optind;
1729 if (argc == 0) {
1730 usage_blame();
1731 } else if (argc == 1) {
1732 repo_path = getcwd(NULL, 0);
1733 if (repo_path == NULL)
1734 return got_error_from_errno();
1735 path = argv[0];
1736 } else if (argc == 2) {
1737 repo_path = realpath(argv[0], NULL);
1738 if (repo_path == NULL)
1739 return got_error_from_errno();
1740 path = argv[1];
1741 } else
1742 usage_blame();
1744 error = got_repo_open(&repo, repo_path);
1745 free(repo_path);
1746 if (error != NULL)
1747 return error;
1749 if (commit_id_str == NULL) {
1750 struct got_reference *head_ref;
1751 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1752 if (error != NULL)
1753 goto done;
1754 error = got_ref_resolve(&commit_id, repo, head_ref);
1755 got_ref_close(head_ref);
1756 } else {
1757 struct got_object *obj;
1758 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1759 if (error != NULL)
1760 goto done;
1761 commit_id = got_object_get_id(obj);
1762 if (commit_id == NULL)
1763 error = got_error_from_errno();
1764 got_object_close(obj);
1766 if (error != NULL)
1767 goto done;
1769 error = show_blame_view(path, commit_id, repo);
1770 done:
1771 free(commit_id);
1772 if (repo)
1773 got_repo_close(repo);
1774 return error;
1777 static const struct got_error *
1778 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1779 struct got_tree_entry **last_displayed_entry,
1780 struct got_tree_entry **selected_entry, int *ndisplayed,
1781 WINDOW *window, const char *label, int show_ids, const char *parent_path,
1782 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1784 const struct got_error *err = NULL;
1785 struct got_tree_entry *te;
1786 wchar_t *wline;
1787 int width, n;
1789 *ndisplayed = 0;
1791 werase(window);
1793 if (limit == 0)
1794 return NULL;
1796 err = format_line(&wline, &width, label, COLS);
1797 if (err)
1798 return err;
1799 waddwstr(window, wline);
1800 if (width < COLS)
1801 waddch(window, '\n');
1802 if (--limit <= 0)
1803 return NULL;
1804 err = format_line(&wline, &width, parent_path, COLS);
1805 if (err)
1806 return err;
1807 waddwstr(window, wline);
1808 if (width < COLS)
1809 waddch(window, '\n');
1810 if (--limit <= 0)
1811 return NULL;
1812 waddch(window, '\n');
1813 if (--limit <= 0)
1814 return NULL;
1816 te = SIMPLEQ_FIRST(&entries->head);
1817 if (*first_displayed_entry == NULL) {
1818 if (selected == 0) {
1819 wstandout(window);
1820 *selected_entry = NULL;
1822 waddstr(window, " ..\n"); /* parent directory */
1823 if (selected == 0)
1824 wstandend(window);
1825 (*ndisplayed)++;
1826 if (--limit <= 0)
1827 return NULL;
1828 n = 1;
1829 } else {
1830 n = 0;
1831 while (te != *first_displayed_entry)
1832 te = SIMPLEQ_NEXT(te, entry);
1835 while (te) {
1836 char *line = NULL, *id_str = NULL;
1838 if (show_ids) {
1839 err = got_object_id_str(&id_str, te->id);
1840 if (err)
1841 return got_error_from_errno();
1843 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
1844 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
1845 free(id_str);
1846 return got_error_from_errno();
1848 free(id_str);
1849 err = format_line(&wline, &width, line, COLS);
1850 if (err) {
1851 free(line);
1852 break;
1854 if (n == selected) {
1855 wstandout(window);
1856 *selected_entry = te;
1858 waddwstr(window, wline);
1859 if (width < COLS)
1860 waddch(window, '\n');
1861 if (n == selected)
1862 wstandend(window);
1863 free(line);
1864 n++;
1865 (*ndisplayed)++;
1866 *last_displayed_entry = te;
1867 if (--limit <= 0)
1868 break;
1869 te = SIMPLEQ_NEXT(te, entry);
1872 return err;
1875 static void
1876 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1877 const struct got_tree_entries *entries, int isroot)
1879 struct got_tree_entry *te, *prev;
1880 int i;
1882 if (*first_displayed_entry == NULL)
1883 return;
1885 te = SIMPLEQ_FIRST(&entries->head);
1886 if (*first_displayed_entry == te) {
1887 if (!isroot)
1888 *first_displayed_entry = NULL;
1889 return;
1892 /* XXX this is stupid... switch to TAILQ? */
1893 for (i = 0; i < maxscroll; i++) {
1894 while (te != *first_displayed_entry) {
1895 prev = te;
1896 te = SIMPLEQ_NEXT(te, entry);
1898 *first_displayed_entry = prev;
1899 te = SIMPLEQ_FIRST(&entries->head);
1901 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1902 *first_displayed_entry = NULL;
1905 static void
1906 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1907 struct got_tree_entry *last_displayed_entry,
1908 const struct got_tree_entries *entries)
1910 struct got_tree_entry *next;
1911 int n = 0;
1913 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1914 return;
1916 if (*first_displayed_entry)
1917 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1918 else
1919 next = SIMPLEQ_FIRST(&entries->head);
1920 while (next) {
1921 *first_displayed_entry = next;
1922 if (++n >= maxscroll)
1923 break;
1924 next = SIMPLEQ_NEXT(next, entry);
1928 struct tog_parent_tree {
1929 TAILQ_ENTRY(tog_parent_tree) entry;
1930 struct got_tree_object *tree;
1931 struct got_tree_entry *first_displayed_entry;
1932 struct got_tree_entry *selected_entry;
1933 int selected;
1936 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1938 static const struct got_error *
1939 tree_entry_path(char **path, struct tog_parent_trees *parents,
1940 struct got_tree_entry *te)
1942 const struct got_error *err = NULL;
1943 struct tog_parent_tree *pt;
1944 size_t len = 2; /* for leading slash and NUL */
1946 TAILQ_FOREACH(pt, parents, entry)
1947 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1948 if (te)
1949 len += strlen(te->name);
1951 *path = calloc(1, len);
1952 if (path == NULL)
1953 return got_error_from_errno();
1955 (*path)[0] = '/';
1956 pt = TAILQ_LAST(parents, tog_parent_trees);
1957 while (pt) {
1958 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1959 err = got_error(GOT_ERR_NO_SPACE);
1960 goto done;
1962 if (strlcat(*path, "/", len) >= len) {
1963 err = got_error(GOT_ERR_NO_SPACE);
1964 goto done;
1966 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1968 if (te) {
1969 if (strlcat(*path, te->name, len) >= len) {
1970 err = got_error(GOT_ERR_NO_SPACE);
1971 goto done;
1974 done:
1975 if (err) {
1976 free(*path);
1977 *path = NULL;
1979 return err;
1982 static const struct got_error *
1983 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1984 struct got_object_id *commit_id, struct got_repository *repo)
1986 const struct got_error *err = NULL;
1987 char *path;
1989 err = tree_entry_path(&path, parents, te);
1990 if (err)
1991 return err;
1993 err = show_blame_view(path, commit_id, repo);
1994 free(path);
1995 return err;
1998 static const struct got_error *
1999 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2000 struct got_repository *repo)
2002 const struct got_error *err = NULL;
2003 int ch, done = 0, selected = 0, show_ids = 0;
2004 struct got_tree_object *tree = root;
2005 const struct got_tree_entries *entries;
2006 struct got_tree_entry *first_displayed_entry = NULL;
2007 struct got_tree_entry *last_displayed_entry = NULL;
2008 struct got_tree_entry *selected_entry = NULL;
2009 char *commit_id_str = NULL, *tree_label = NULL;
2010 int nentries, ndisplayed;
2011 struct tog_parent_trees parents;
2013 TAILQ_INIT(&parents);
2015 err = got_object_id_str(&commit_id_str, commit_id);
2016 if (err != NULL)
2017 goto done;
2019 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2020 err = got_error_from_errno();
2021 goto done;
2024 if (tog_tree_view.window == NULL) {
2025 tog_tree_view.window = newwin(0, 0, 0, 0);
2026 if (tog_tree_view.window == NULL)
2027 return got_error_from_errno();
2028 keypad(tog_tree_view.window, TRUE);
2030 if (tog_tree_view.panel == NULL) {
2031 tog_tree_view.panel = new_panel(tog_tree_view.window);
2032 if (tog_tree_view.panel == NULL)
2033 return got_error_from_errno();
2034 } else
2035 show_panel(tog_tree_view.panel);
2037 entries = got_object_tree_get_entries(root);
2038 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2039 while (!done) {
2040 char *parent_path;
2041 entries = got_object_tree_get_entries(tree);
2042 nentries = entries->nentries;
2043 if (tree != root)
2044 nentries++; /* '..' directory */
2046 err = tree_entry_path(&parent_path, &parents, NULL);
2047 if (err)
2048 goto done;
2050 err = draw_tree_entries(&first_displayed_entry,
2051 &last_displayed_entry, &selected_entry, &ndisplayed,
2052 tog_tree_view.window, tree_label, show_ids,
2053 parent_path, entries, selected, LINES, tree == root);
2054 free(parent_path);
2055 if (err)
2056 break;
2058 nodelay(stdscr, FALSE);
2059 ch = wgetch(tog_tree_view.window);
2060 nodelay(stdscr, TRUE);
2061 switch (ch) {
2062 case 'q':
2063 done = 1;
2064 break;
2065 case 'i':
2066 show_ids = !show_ids;
2067 break;
2068 case 'k':
2069 case KEY_UP:
2070 if (selected > 0)
2071 selected--;
2072 if (selected > 0)
2073 break;
2074 tree_scroll_up(&first_displayed_entry, 1,
2075 entries, tree == root);
2076 break;
2077 case KEY_PPAGE:
2078 if (SIMPLEQ_FIRST(&entries->head) ==
2079 first_displayed_entry) {
2080 if (tree != root)
2081 first_displayed_entry = NULL;
2082 selected = 0;
2083 break;
2085 tree_scroll_up(&first_displayed_entry, LINES,
2086 entries, tree == root);
2087 break;
2088 case 'j':
2089 case KEY_DOWN:
2090 if (selected < ndisplayed - 1) {
2091 selected++;
2092 break;
2094 tree_scroll_down(&first_displayed_entry, 1,
2095 last_displayed_entry, entries);
2096 break;
2097 case KEY_NPAGE:
2098 tree_scroll_down(&first_displayed_entry, LINES,
2099 last_displayed_entry, entries);
2100 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2101 break;
2102 /* can't scroll any further; move cursor down */
2103 if (selected < ndisplayed - 1)
2104 selected = ndisplayed - 1;
2105 break;
2106 case KEY_ENTER:
2107 case '\r':
2108 if (selected_entry == NULL) {
2109 struct tog_parent_tree *parent;
2110 case KEY_BACKSPACE:
2111 /* user selected '..' */
2112 if (tree == root)
2113 break;
2114 parent = TAILQ_FIRST(&parents);
2115 TAILQ_REMOVE(&parents, parent, entry);
2116 got_object_tree_close(tree);
2117 tree = parent->tree;
2118 first_displayed_entry =
2119 parent->first_displayed_entry;
2120 selected_entry = parent->selected_entry;
2121 selected = parent->selected;
2122 free(parent);
2123 } else if (S_ISDIR(selected_entry->mode)) {
2124 struct tog_parent_tree *parent;
2125 struct got_tree_object *child;
2126 err = got_object_open_as_tree(
2127 &child, repo, selected_entry->id);
2128 if (err)
2129 goto done;
2130 parent = calloc(1, sizeof(*parent));
2131 if (parent == NULL) {
2132 err = got_error_from_errno();
2133 goto done;
2135 parent->tree = tree;
2136 parent->first_displayed_entry =
2137 first_displayed_entry;
2138 parent->selected_entry = selected_entry;
2139 parent->selected = selected;
2140 TAILQ_INSERT_HEAD(&parents, parent,
2141 entry);
2142 tree = child;
2143 selected = 0;
2144 first_displayed_entry = NULL;
2145 } else if (S_ISREG(selected_entry->mode)) {
2146 err = blame_tree_entry(selected_entry,
2147 &parents, commit_id, repo);
2148 if (err)
2149 goto done;
2151 break;
2152 case KEY_RESIZE:
2153 if (selected > LINES)
2154 selected = ndisplayed - 1;
2155 break;
2156 default:
2157 break;
2160 done:
2161 free(tree_label);
2162 free(commit_id_str);
2163 while (!TAILQ_EMPTY(&parents)) {
2164 struct tog_parent_tree *parent;
2165 parent = TAILQ_FIRST(&parents);
2166 TAILQ_REMOVE(&parents, parent, entry);
2167 free(parent);
2170 if (tree != root)
2171 got_object_tree_close(tree);
2172 return err;
2175 __dead static void
2176 usage_tree(void)
2178 endwin();
2179 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2180 getprogname());
2181 exit(1);
2184 static const struct got_error *
2185 cmd_tree(int argc, char *argv[])
2187 const struct got_error *error;
2188 struct got_repository *repo = NULL;
2189 char *repo_path = NULL;
2190 struct got_object_id *commit_id = NULL;
2191 char *commit_id_arg = NULL;
2192 struct got_commit_object *commit = NULL;
2193 struct got_tree_object *tree = NULL;
2194 int ch;
2196 #ifndef PROFILE
2197 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2198 err(1, "pledge");
2199 #endif
2201 while ((ch = getopt(argc, argv, "c:")) != -1) {
2202 switch (ch) {
2203 case 'c':
2204 commit_id_arg = optarg;
2205 break;
2206 default:
2207 usage();
2208 /* NOTREACHED */
2212 argc -= optind;
2213 argv += optind;
2215 if (argc == 0) {
2216 repo_path = getcwd(NULL, 0);
2217 if (repo_path == NULL)
2218 return got_error_from_errno();
2219 } else if (argc == 1) {
2220 repo_path = realpath(argv[0], NULL);
2221 if (repo_path == NULL)
2222 return got_error_from_errno();
2223 } else
2224 usage_log();
2226 error = got_repo_open(&repo, repo_path);
2227 free(repo_path);
2228 if (error != NULL)
2229 return error;
2231 if (commit_id_arg == NULL) {
2232 error = get_head_commit_id(&commit_id, repo);
2233 if (error != NULL)
2234 goto done;
2235 } else {
2236 struct got_object *obj;
2237 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2238 if (error == NULL) {
2239 commit_id = got_object_get_id(obj);
2240 if (commit_id == NULL)
2241 error = got_error_from_errno();
2244 if (error != NULL)
2245 goto done;
2247 error = got_object_open_as_commit(&commit, repo, commit_id);
2248 if (error != NULL)
2249 goto done;
2251 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2252 if (error != NULL)
2253 goto done;
2255 error = show_tree_view(tree, commit_id, repo);
2256 done:
2257 free(commit_id);
2258 if (commit)
2259 got_object_commit_close(commit);
2260 if (tree)
2261 got_object_tree_close(tree);
2262 if (repo)
2263 got_repo_close(repo);
2264 return error;
2266 static void
2267 init_curses(void)
2269 initscr();
2270 cbreak();
2271 noecho();
2272 nonl();
2273 intrflush(stdscr, FALSE);
2274 keypad(stdscr, TRUE);
2275 curs_set(0);
2278 __dead static void
2279 usage(void)
2281 int i;
2283 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2284 "Available commands:\n", getprogname());
2285 for (i = 0; i < nitems(tog_commands); i++) {
2286 struct tog_cmd *cmd = &tog_commands[i];
2287 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2289 exit(1);
2292 static char **
2293 make_argv(const char *arg0, const char *arg1)
2295 char **argv;
2296 int argc = (arg1 == NULL ? 1 : 2);
2298 argv = calloc(argc, sizeof(char *));
2299 if (argv == NULL)
2300 err(1, "calloc");
2301 argv[0] = strdup(arg0);
2302 if (argv[0] == NULL)
2303 err(1, "calloc");
2304 if (arg1) {
2305 argv[1] = strdup(arg1);
2306 if (argv[1] == NULL)
2307 err(1, "calloc");
2310 return argv;
2313 int
2314 main(int argc, char *argv[])
2316 const struct got_error *error = NULL;
2317 struct tog_cmd *cmd = NULL;
2318 int ch, hflag = 0;
2319 char **cmd_argv = NULL;
2321 setlocale(LC_ALL, "");
2323 while ((ch = getopt(argc, argv, "h")) != -1) {
2324 switch (ch) {
2325 case 'h':
2326 hflag = 1;
2327 break;
2328 default:
2329 usage();
2330 /* NOTREACHED */
2334 argc -= optind;
2335 argv += optind;
2336 optind = 0;
2337 optreset = 1;
2339 if (argc == 0) {
2340 if (hflag)
2341 usage();
2342 /* Build an argument vector which runs a default command. */
2343 cmd = &tog_commands[0];
2344 cmd_argv = make_argv(cmd->name, NULL);
2345 argc = 1;
2346 } else {
2347 int i;
2349 /* Did the user specific a command? */
2350 for (i = 0; i < nitems(tog_commands); i++) {
2351 if (strncmp(tog_commands[i].name, argv[0],
2352 strlen(argv[0])) == 0) {
2353 cmd = &tog_commands[i];
2354 if (hflag)
2355 tog_commands[i].cmd_usage();
2356 break;
2359 if (cmd == NULL) {
2360 /* Did the user specify a repository? */
2361 char *repo_path = realpath(argv[0], NULL);
2362 if (repo_path) {
2363 struct got_repository *repo;
2364 error = got_repo_open(&repo, repo_path);
2365 if (error == NULL)
2366 got_repo_close(repo);
2367 } else
2368 error = got_error_from_errno();
2369 if (error) {
2370 if (hflag) {
2371 fprintf(stderr, "%s: '%s' is not a "
2372 "known command\n", getprogname(),
2373 argv[0]);
2374 usage();
2376 fprintf(stderr, "%s: '%s' is neither a known "
2377 "command nor a path to a repository\n",
2378 getprogname(), argv[0]);
2379 free(repo_path);
2380 return 1;
2382 cmd = &tog_commands[0];
2383 cmd_argv = make_argv(cmd->name, repo_path);
2384 argc = 2;
2385 free(repo_path);
2389 init_curses();
2391 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2392 if (error)
2393 goto done;
2394 done:
2395 endwin();
2396 free(cmd_argv);
2397 if (error)
2398 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2399 return 0;