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 if (first_displayed_line > 1)
978 first_displayed_line--;
979 break;
980 case KEY_PPAGE:
981 case KEY_BACKSPACE:
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 if (!eof)
990 first_displayed_line++;
991 break;
992 case KEY_NPAGE:
993 case ' ':
994 i = 0;
995 while (!eof && i++ < LINES - 1) {
996 char *line = parse_next_line(f, NULL);
997 first_displayed_line++;
998 if (line == NULL)
999 break;
1001 break;
1002 default:
1003 break;
1006 fclose(f);
1007 return err;
1010 static const struct got_error *
1011 cmd_diff(int argc, char *argv[])
1013 const struct got_error *error = NULL;
1014 struct got_repository *repo = NULL;
1015 struct got_object *obj1 = NULL, *obj2 = NULL;
1016 char *repo_path = NULL;
1017 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1018 int ch;
1020 #ifndef PROFILE
1021 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1022 err(1, "pledge");
1023 #endif
1025 while ((ch = getopt(argc, argv, "")) != -1) {
1026 switch (ch) {
1027 default:
1028 usage();
1029 /* NOTREACHED */
1033 argc -= optind;
1034 argv += optind;
1036 if (argc == 0) {
1037 usage_diff(); /* TODO show local worktree changes */
1038 } else if (argc == 2) {
1039 repo_path = getcwd(NULL, 0);
1040 if (repo_path == NULL)
1041 return got_error_from_errno();
1042 obj_id_str1 = argv[0];
1043 obj_id_str2 = argv[1];
1044 } else if (argc == 3) {
1045 repo_path = realpath(argv[0], NULL);
1046 if (repo_path == NULL)
1047 return got_error_from_errno();
1048 obj_id_str1 = argv[1];
1049 obj_id_str2 = argv[2];
1050 } else
1051 usage_diff();
1053 error = got_repo_open(&repo, repo_path);
1054 free(repo_path);
1055 if (error)
1056 goto done;
1058 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1059 if (error)
1060 goto done;
1062 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1063 if (error)
1064 goto done;
1066 error = show_diff_view(obj1, obj2, repo);
1067 done:
1068 got_repo_close(repo);
1069 if (obj1)
1070 got_object_close(obj1);
1071 if (obj2)
1072 got_object_close(obj2);
1073 return error;
1076 __dead static void
1077 usage_blame(void)
1079 endwin();
1080 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1081 getprogname());
1082 exit(1);
1085 struct tog_blame_line {
1086 int annotated;
1087 struct got_object_id *id;
1090 static const struct got_error *
1091 draw_blame(WINDOW *window, struct got_object_id *id, FILE *f, const char *path,
1092 struct tog_blame_line *lines, int nlines, int blame_complete,
1093 int selected_line, int *first_displayed_line, int *last_displayed_line,
1094 int *eof, int max_lines)
1096 const struct got_error *err;
1097 int lineno = 0, nprinted = 0;
1098 char *line;
1099 size_t len;
1100 wchar_t *wline;
1101 int width, wlimit;
1102 struct tog_blame_line *blame_line;
1103 struct got_object_id *prev_id = NULL;
1104 char *id_str;
1106 err = got_object_id_str(&id_str, id);
1107 if (err)
1108 return err;
1110 rewind(f);
1111 werase(window);
1113 if (asprintf(&line, "commit: %s", id_str) == -1) {
1114 err = got_error_from_errno();
1115 free(id_str);
1116 return err;
1119 err = format_line(&wline, &width, line, COLS);
1120 free(line);
1121 waddwstr(window, wline);
1122 if (width < COLS)
1123 waddch(window, '\n');
1125 if (asprintf(&line, "[%d/%d] %s%s",
1126 *first_displayed_line - 1 + selected_line, nlines,
1127 blame_complete ? "" : "annotating ", path) == -1) {
1128 free(id_str);
1129 return got_error_from_errno();
1131 free(id_str);
1132 err = format_line(&wline, &width, line, COLS);
1133 free(line);
1134 if (err)
1135 return err;
1136 waddwstr(window, wline);
1137 if (width < COLS)
1138 waddch(window, '\n');
1140 *eof = 0;
1141 while (nprinted < max_lines - 2) {
1142 line = parse_next_line(f, &len);
1143 if (line == NULL) {
1144 *eof = 1;
1145 break;
1147 if (++lineno < *first_displayed_line) {
1148 free(line);
1149 continue;
1152 wlimit = COLS < 9 ? 0 : COLS - 9;
1153 err = format_line(&wline, &width, line, wlimit);
1154 if (err) {
1155 free(line);
1156 return err;
1159 if (nprinted == selected_line - 1)
1160 wstandout(window);
1162 blame_line = &lines[lineno - 1];
1163 if (blame_line->annotated && prev_id &&
1164 got_object_id_cmp(prev_id, blame_line->id) == 0)
1165 waddstr(window, " ");
1166 else if (blame_line->annotated) {
1167 char *id_str;
1168 err = got_object_id_str(&id_str, blame_line->id);
1169 if (err) {
1170 free(line);
1171 return err;
1173 wprintw(window, "%.8s ", id_str);
1174 free(id_str);
1175 prev_id = blame_line->id;
1176 } else {
1177 waddstr(window, "........ ");
1178 prev_id = NULL;
1181 waddwstr(window, wline);
1182 while (width < wlimit) {
1183 waddch(window, ' '); /* width == wlimit - 1 ? '\n' : ' '); */
1184 width++;
1186 if (nprinted == selected_line - 1)
1187 wstandend(window);
1188 if (++nprinted == 1)
1189 *first_displayed_line = lineno;
1190 free(line);
1192 *last_displayed_line = lineno;
1194 update_panels();
1195 doupdate();
1197 return NULL;
1200 struct tog_blame_cb_args {
1201 pthread_mutex_t *mutex;
1202 struct tog_blame_line *lines; /* one per line */
1203 int nlines;
1205 struct got_object_id *commit_id;
1206 FILE *f;
1207 const char *path;
1208 int *first_displayed_line;
1209 int *last_displayed_line;
1210 int *selected_line;
1211 int *quit;
1214 static const struct got_error *
1215 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1217 const struct got_error *err = NULL;
1218 struct tog_blame_cb_args *a = arg;
1219 struct tog_blame_line *line;
1220 int eof;
1222 if (nlines != a->nlines ||
1223 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1224 return got_error(GOT_ERR_RANGE);
1226 if (pthread_mutex_lock(a->mutex) != 0)
1227 return got_error_from_errno();
1229 if (*a->quit) { /* user has quit the blame view */
1230 err = got_error(GOT_ERR_ITER_COMPLETED);
1231 goto done;
1234 if (lineno == -1)
1235 goto done; /* no change in this commit */
1237 line = &a->lines[lineno - 1];
1238 if (line->annotated)
1239 goto done;
1241 line->id = got_object_id_dup(id);
1242 if (line->id == NULL) {
1243 err = got_error_from_errno();
1244 goto done;
1246 line->annotated = 1;
1248 err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1249 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1250 a->last_displayed_line, &eof, LINES);
1251 done:
1252 if (pthread_mutex_unlock(a->mutex) != 0)
1253 return got_error_from_errno();
1254 return err;
1257 struct tog_blame_thread_args {
1258 const char *path;
1259 struct got_repository *repo;
1260 struct tog_blame_cb_args *cb_args;
1261 int *complete;
1264 static void *
1265 blame_thread(void *arg)
1267 const struct got_error *err;
1268 struct tog_blame_thread_args *ta = arg;
1269 struct tog_blame_cb_args *a = ta->cb_args;
1270 int eof;
1272 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1273 blame_cb, ta->cb_args);
1274 *ta->complete = 1;
1275 if (err)
1276 return (void *)err;
1278 if (pthread_mutex_lock(a->mutex) != 0)
1279 return (void *)got_error_from_errno();
1281 err = draw_blame(tog_blame_view.window, a->commit_id, a->f, a->path,
1282 a->lines, a->nlines, 1, *a->selected_line, a->first_displayed_line,
1283 a->last_displayed_line, &eof, LINES);
1285 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1286 err = got_error_from_errno();
1288 return (void *)err;
1291 static struct got_object_id *
1292 get_selected_commit_id(struct tog_blame_line *lines,
1293 int first_displayed_line, int selected_line)
1295 struct tog_blame_line *line;
1297 line = &lines[first_displayed_line - 1 + selected_line - 1];
1298 if (!line->annotated)
1299 return NULL;
1301 return line->id;
1304 static const struct got_error *
1305 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1306 struct tog_blame_line *lines, int first_displayed_line,
1307 int selected_line, struct got_repository *repo)
1309 const struct got_error *err = NULL;
1310 struct got_commit_object *commit = NULL;
1311 struct got_object_id *selected_id;
1312 struct got_object_qid *pid;
1314 *pobj = NULL;
1315 *obj = NULL;
1317 selected_id = get_selected_commit_id(lines,
1318 first_displayed_line, selected_line);
1319 if (selected_id == NULL)
1320 return NULL;
1322 err = got_object_open(obj, repo, selected_id);
1323 if (err)
1324 goto done;
1326 err = got_object_commit_open(&commit, repo, *obj);
1327 if (err)
1328 goto done;
1330 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1331 if (pid) {
1332 err = got_object_open(pobj, repo, pid->id);
1333 if (err)
1334 goto done;
1336 done:
1337 if (commit)
1338 got_object_commit_close(commit);
1339 return err;
1342 struct tog_blame {
1343 FILE *f;
1344 size_t filesize;
1345 struct tog_blame_line *lines;
1346 size_t nlines;
1347 pthread_t thread;
1348 struct tog_blame_thread_args thread_args;
1349 struct tog_blame_cb_args cb_args;
1350 const char *path;
1351 struct got_object_id *commit_id;
1354 static const struct got_error *
1355 stop_blame(struct tog_blame *blame)
1357 const struct got_error *err = NULL;
1358 int i;
1360 if (blame->thread) {
1361 if (pthread_join(blame->thread, (void **)&err) != 0)
1362 err = got_error_from_errno();
1363 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1364 err = NULL;
1365 blame->thread = NULL;
1367 if (blame->thread_args.repo) {
1368 got_repo_close(blame->thread_args.repo);
1369 blame->thread_args.repo = NULL;
1371 if (blame->f) {
1372 fclose(blame->f);
1373 blame->f = NULL;
1375 for (i = 0; i < blame->nlines; i++)
1376 free(blame->lines[i].id);
1377 free(blame->lines);
1378 blame->lines = NULL;
1379 free(blame->commit_id);
1380 blame->commit_id = NULL;
1382 return err;
1385 static const struct got_error *
1386 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex, int *blame_complete,
1387 int *first_displayed_line, int *last_displayed_line,
1388 int *selected_line, int *done, const char *path,
1389 struct got_object_id *commit_id,
1390 struct got_repository *repo)
1392 const struct got_error *err = NULL;
1393 struct got_blob_object *blob = NULL;
1394 struct got_repository *thread_repo = NULL;
1395 struct got_object *obj;
1397 err = got_object_open_by_path(&obj, repo, commit_id, path);
1398 if (err)
1399 goto done;
1400 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1401 err = got_error(GOT_ERR_OBJ_TYPE);
1402 goto done;
1405 err = got_object_blob_open(&blob, repo, obj, 8192);
1406 if (err)
1407 goto done;
1408 blame->f = got_opentemp();
1409 if (blame->f == NULL) {
1410 err = got_error_from_errno();
1411 goto done;
1413 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1414 blame->f, blob);
1415 if (err)
1416 goto done;
1418 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1419 if (blame->lines == NULL) {
1420 err = got_error_from_errno();
1421 goto done;
1424 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1425 if (err)
1426 goto done;
1428 blame->cb_args.lines = blame->lines;
1429 blame->cb_args.nlines = blame->nlines;
1430 blame->cb_args.mutex = mutex;
1431 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1432 if (blame->cb_args.commit_id == NULL) {
1433 err = got_error_from_errno();
1434 goto done;
1436 blame->cb_args.f = blame->f;
1437 blame->cb_args.path = path;
1438 blame->cb_args.first_displayed_line = first_displayed_line;
1439 blame->cb_args.selected_line = selected_line;
1440 blame->cb_args.last_displayed_line = last_displayed_line;
1441 blame->cb_args.quit = done;
1443 blame->thread_args.path = path;
1444 blame->thread_args.repo = thread_repo;
1445 blame->thread_args.cb_args = &blame->cb_args;
1446 blame->thread_args.complete = blame_complete;
1447 *blame_complete = 0;
1449 if (pthread_create(&blame->thread, NULL, blame_thread,
1450 &blame->thread_args) != 0) {
1451 err = got_error_from_errno();
1452 goto done;
1455 done:
1456 if (blob)
1457 got_object_blob_close(blob);
1458 if (obj)
1459 got_object_close(obj);
1460 if (err)
1461 stop_blame(blame);
1462 return err;
1465 static const struct got_error *
1466 show_blame_view(const char *path, struct got_object_id *commit_id,
1467 struct got_repository *repo)
1469 const struct got_error *err = NULL, *thread_err = NULL;
1470 int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
1471 int selected_line = first_displayed_line;
1472 int eof, blame_complete = 0;
1473 struct got_object *obj = NULL, *pobj = NULL;
1474 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1475 struct tog_blame blame;
1476 int blame_running = 0;
1477 struct got_object_id_queue blamed_commits;
1478 struct got_object_qid *blamed_commit = NULL;
1480 SIMPLEQ_INIT(&blamed_commits);
1482 if (pthread_mutex_init(&mutex, NULL) != 0) {
1483 err = got_error_from_errno();
1484 goto done;
1487 err = got_object_qid_alloc(&blamed_commit, commit_id);
1488 if (err)
1489 goto done;
1490 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1492 if (tog_blame_view.window == NULL) {
1493 tog_blame_view.window = newwin(0, 0, 0, 0);
1494 if (tog_blame_view.window == NULL)
1495 return got_error_from_errno();
1496 keypad(tog_blame_view.window, TRUE);
1498 if (tog_blame_view.panel == NULL) {
1499 tog_blame_view.panel = new_panel(tog_blame_view.window);
1500 if (tog_blame_view.panel == NULL)
1501 return got_error_from_errno();
1502 } else
1503 show_panel(tog_blame_view.panel);
1505 memset(&blame, 0, sizeof(blame));
1506 err = run_blame(&blame, &mutex, &blame_complete,
1507 &first_displayed_line, &last_displayed_line,
1508 &selected_line, &done, path, blamed_commit->id, repo);
1509 if (err)
1510 return err;
1512 while (!done) {
1513 if (pthread_mutex_lock(&mutex) != 0) {
1514 err = got_error_from_errno();
1515 goto done;
1517 err = draw_blame(tog_blame_view.window, blamed_commit->id,
1518 blame.f, path, blame.lines, blame.nlines, blame_complete,
1519 selected_line, &first_displayed_line, &last_displayed_line,
1520 &eof, LINES);
1521 if (pthread_mutex_unlock(&mutex) != 0) {
1522 err = got_error_from_errno();
1523 goto done;
1525 if (err)
1526 break;
1527 nodelay(stdscr, FALSE);
1528 ch = wgetch(tog_blame_view.window);
1529 nodelay(stdscr, TRUE);
1530 if (pthread_mutex_lock(&mutex) != 0) {
1531 err = got_error_from_errno();
1532 goto done;
1534 switch (ch) {
1535 case 'q':
1536 done = 1;
1537 break;
1538 case 'k':
1539 case KEY_UP:
1540 if (selected_line > 1)
1541 selected_line--;
1542 else if (selected_line == 1 &&
1543 first_displayed_line > 1)
1544 first_displayed_line--;
1545 break;
1546 case KEY_PPAGE:
1547 case KEY_BACKSPACE:
1548 if (first_displayed_line == 1) {
1549 selected_line = 1;
1550 break;
1552 if (first_displayed_line > LINES - 2)
1553 first_displayed_line -= (LINES - 2);
1554 else
1555 first_displayed_line = 1;
1556 break;
1557 case 'j':
1558 case KEY_DOWN:
1559 if (selected_line < LINES - 2 &&
1560 first_displayed_line + selected_line <=
1561 blame.nlines)
1562 selected_line++;
1563 else if (last_displayed_line < blame.nlines)
1564 first_displayed_line++;
1565 break;
1566 case 'b':
1567 case 'p': {
1568 struct got_object_id *id;
1569 id = get_selected_commit_id(blame.lines,
1570 first_displayed_line, selected_line);
1571 if (id == NULL || got_object_id_cmp(id,
1572 blamed_commit->id) == 0)
1573 break;
1574 err = open_selected_commit(&pobj, &obj,
1575 blame.lines, first_displayed_line,
1576 selected_line, repo);
1577 if (err)
1578 break;
1579 if (pobj == NULL && obj == NULL)
1580 break;
1581 if (ch == 'p' && pobj == NULL)
1582 break;
1583 done = 1;
1584 if (pthread_mutex_unlock(&mutex) != 0) {
1585 err = got_error_from_errno();
1586 goto done;
1588 thread_err = stop_blame(&blame);
1589 blame_running = 0;
1590 done = 0;
1591 if (pthread_mutex_lock(&mutex) != 0) {
1592 err = got_error_from_errno();
1593 goto done;
1595 if (thread_err)
1596 break;
1597 id = got_object_get_id(ch == 'b' ? obj : pobj);
1598 got_object_close(obj);
1599 obj = NULL;
1600 if (pobj) {
1601 got_object_close(pobj);
1602 pobj = NULL;
1604 if (id == NULL) {
1605 err = got_error_from_errno();
1606 break;
1608 err = got_object_qid_alloc(&blamed_commit, id);
1609 free(id);
1610 if (err)
1611 goto done;
1612 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1613 blamed_commit, entry);
1614 err = run_blame(&blame, &mutex,
1615 &blame_complete, &first_displayed_line,
1616 &last_displayed_line, &selected_line,
1617 &done, path, blamed_commit->id, repo);
1618 if (err)
1619 break;
1620 blame_running = 1;
1621 break;
1623 case 'B': {
1624 struct got_object_qid *first;
1625 first = SIMPLEQ_FIRST(&blamed_commits);
1626 if (!got_object_id_cmp(first->id, commit_id))
1627 break;
1628 done = 1;
1629 if (pthread_mutex_unlock(&mutex) != 0) {
1630 err = got_error_from_errno();
1631 goto done;
1633 thread_err = stop_blame(&blame);
1634 blame_running = 0;
1635 done = 0;
1636 if (pthread_mutex_lock(&mutex) != 0) {
1637 err = got_error_from_errno();
1638 goto done;
1640 if (thread_err)
1641 break;
1642 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1643 got_object_qid_free(blamed_commit);
1644 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1645 err = run_blame(&blame, &mutex,
1646 &blame_complete, &first_displayed_line,
1647 &last_displayed_line, &selected_line,
1648 &done, path, blamed_commit->id, repo);
1649 if (err)
1650 break;
1651 blame_running = 1;
1652 break;
1654 case KEY_ENTER:
1655 case '\r':
1656 err = open_selected_commit(&pobj, &obj,
1657 blame.lines, first_displayed_line,
1658 selected_line, repo);
1659 if (err)
1660 break;
1661 if (pobj == NULL && obj == NULL)
1662 break;
1663 err = show_diff_view(pobj, obj, repo);
1664 if (pobj) {
1665 got_object_close(pobj);
1666 pobj = NULL;
1668 got_object_close(obj);
1669 obj = NULL;
1670 show_panel(tog_blame_view.panel);
1671 if (err)
1672 break;
1673 break;
1674 case KEY_NPAGE:
1675 case ' ':
1676 if (last_displayed_line >= blame.nlines &&
1677 selected_line < LINES - 2) {
1678 selected_line = MIN(blame.nlines,
1679 LINES - 2);
1680 break;
1682 if (last_displayed_line + LINES - 2 <=
1683 blame.nlines)
1684 first_displayed_line += LINES - 2;
1685 else
1686 first_displayed_line =
1687 blame.nlines - (LINES - 3);
1688 break;
1689 default:
1690 break;
1692 if (pthread_mutex_unlock(&mutex) != 0)
1693 err = got_error_from_errno();
1694 if (err || thread_err)
1695 break;
1697 done:
1698 if (pobj)
1699 got_object_close(pobj);
1700 if (blame_running)
1701 thread_err = stop_blame(&blame);
1702 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1703 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1704 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1705 got_object_qid_free(blamed_commit);
1707 return thread_err ? thread_err : err;
1710 static const struct got_error *
1711 cmd_blame(int argc, char *argv[])
1713 const struct got_error *error;
1714 struct got_repository *repo = NULL;
1715 char *repo_path = NULL;
1716 char *path = NULL;
1717 struct got_object_id *commit_id = NULL;
1718 char *commit_id_str = NULL;
1719 int ch;
1721 #ifndef PROFILE
1722 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1723 err(1, "pledge");
1724 #endif
1726 while ((ch = getopt(argc, argv, "c:")) != -1) {
1727 switch (ch) {
1728 case 'c':
1729 commit_id_str = optarg;
1730 break;
1731 default:
1732 usage();
1733 /* NOTREACHED */
1737 argc -= optind;
1738 argv += optind;
1740 if (argc == 0) {
1741 usage_blame();
1742 } else if (argc == 1) {
1743 repo_path = getcwd(NULL, 0);
1744 if (repo_path == NULL)
1745 return got_error_from_errno();
1746 path = argv[0];
1747 } else if (argc == 2) {
1748 repo_path = realpath(argv[0], NULL);
1749 if (repo_path == NULL)
1750 return got_error_from_errno();
1751 path = argv[1];
1752 } else
1753 usage_blame();
1755 error = got_repo_open(&repo, repo_path);
1756 free(repo_path);
1757 if (error != NULL)
1758 return error;
1760 if (commit_id_str == NULL) {
1761 struct got_reference *head_ref;
1762 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1763 if (error != NULL)
1764 goto done;
1765 error = got_ref_resolve(&commit_id, repo, head_ref);
1766 got_ref_close(head_ref);
1767 } else {
1768 struct got_object *obj;
1769 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1770 if (error != NULL)
1771 goto done;
1772 commit_id = got_object_get_id(obj);
1773 if (commit_id == NULL)
1774 error = got_error_from_errno();
1775 got_object_close(obj);
1777 if (error != NULL)
1778 goto done;
1780 error = show_blame_view(path, commit_id, repo);
1781 done:
1782 free(commit_id);
1783 if (repo)
1784 got_repo_close(repo);
1785 return error;
1788 static const struct got_error *
1789 draw_tree_entries(struct got_tree_entry **first_displayed_entry,
1790 struct got_tree_entry **last_displayed_entry,
1791 struct got_tree_entry **selected_entry, int *ndisplayed,
1792 WINDOW *window, const char *label, int show_ids, const char *parent_path,
1793 const struct got_tree_entries *entries, int selected, int limit, int isroot)
1795 const struct got_error *err = NULL;
1796 struct got_tree_entry *te;
1797 wchar_t *wline;
1798 int width, n;
1800 *ndisplayed = 0;
1802 werase(window);
1804 if (limit == 0)
1805 return NULL;
1807 err = format_line(&wline, &width, label, COLS);
1808 if (err)
1809 return err;
1810 waddwstr(window, wline);
1811 if (width < COLS)
1812 waddch(window, '\n');
1813 if (--limit <= 0)
1814 return NULL;
1815 err = format_line(&wline, &width, parent_path, COLS);
1816 if (err)
1817 return err;
1818 waddwstr(window, wline);
1819 if (width < COLS)
1820 waddch(window, '\n');
1821 if (--limit <= 0)
1822 return NULL;
1823 waddch(window, '\n');
1824 if (--limit <= 0)
1825 return NULL;
1827 te = SIMPLEQ_FIRST(&entries->head);
1828 if (*first_displayed_entry == NULL) {
1829 if (selected == 0) {
1830 wstandout(window);
1831 *selected_entry = NULL;
1833 waddstr(window, " ..\n"); /* parent directory */
1834 if (selected == 0)
1835 wstandend(window);
1836 (*ndisplayed)++;
1837 if (--limit <= 0)
1838 return NULL;
1839 n = 1;
1840 } else {
1841 n = 0;
1842 while (te != *first_displayed_entry)
1843 te = SIMPLEQ_NEXT(te, entry);
1846 while (te) {
1847 char *line = NULL, *id_str = NULL;
1849 if (show_ids) {
1850 err = got_object_id_str(&id_str, te->id);
1851 if (err)
1852 return got_error_from_errno();
1854 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
1855 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
1856 free(id_str);
1857 return got_error_from_errno();
1859 free(id_str);
1860 err = format_line(&wline, &width, line, COLS);
1861 if (err) {
1862 free(line);
1863 break;
1865 if (n == selected) {
1866 wstandout(window);
1867 *selected_entry = te;
1869 waddwstr(window, wline);
1870 if (width < COLS)
1871 waddch(window, '\n');
1872 if (n == selected)
1873 wstandend(window);
1874 free(line);
1875 n++;
1876 (*ndisplayed)++;
1877 *last_displayed_entry = te;
1878 if (--limit <= 0)
1879 break;
1880 te = SIMPLEQ_NEXT(te, entry);
1883 return err;
1886 static void
1887 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
1888 const struct got_tree_entries *entries, int isroot)
1890 struct got_tree_entry *te, *prev;
1891 int i;
1893 if (*first_displayed_entry == NULL)
1894 return;
1896 te = SIMPLEQ_FIRST(&entries->head);
1897 if (*first_displayed_entry == te) {
1898 if (!isroot)
1899 *first_displayed_entry = NULL;
1900 return;
1903 /* XXX this is stupid... switch to TAILQ? */
1904 for (i = 0; i < maxscroll; i++) {
1905 while (te != *first_displayed_entry) {
1906 prev = te;
1907 te = SIMPLEQ_NEXT(te, entry);
1909 *first_displayed_entry = prev;
1910 te = SIMPLEQ_FIRST(&entries->head);
1912 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
1913 *first_displayed_entry = NULL;
1916 static void
1917 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
1918 struct got_tree_entry *last_displayed_entry,
1919 const struct got_tree_entries *entries)
1921 struct got_tree_entry *next;
1922 int n = 0;
1924 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
1925 return;
1927 if (*first_displayed_entry)
1928 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
1929 else
1930 next = SIMPLEQ_FIRST(&entries->head);
1931 while (next) {
1932 *first_displayed_entry = next;
1933 if (++n >= maxscroll)
1934 break;
1935 next = SIMPLEQ_NEXT(next, entry);
1939 struct tog_parent_tree {
1940 TAILQ_ENTRY(tog_parent_tree) entry;
1941 struct got_tree_object *tree;
1942 struct got_tree_entry *first_displayed_entry;
1943 struct got_tree_entry *selected_entry;
1944 int selected;
1947 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
1949 static const struct got_error *
1950 tree_entry_path(char **path, struct tog_parent_trees *parents,
1951 struct got_tree_entry *te)
1953 const struct got_error *err = NULL;
1954 struct tog_parent_tree *pt;
1955 size_t len = 2; /* for leading slash and NUL */
1957 TAILQ_FOREACH(pt, parents, entry)
1958 len += strlen(pt->selected_entry->name) + 1 /* slash */;
1959 if (te)
1960 len += strlen(te->name);
1962 *path = calloc(1, len);
1963 if (path == NULL)
1964 return got_error_from_errno();
1966 (*path)[0] = '/';
1967 pt = TAILQ_LAST(parents, tog_parent_trees);
1968 while (pt) {
1969 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
1970 err = got_error(GOT_ERR_NO_SPACE);
1971 goto done;
1973 if (strlcat(*path, "/", len) >= len) {
1974 err = got_error(GOT_ERR_NO_SPACE);
1975 goto done;
1977 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
1979 if (te) {
1980 if (strlcat(*path, te->name, len) >= len) {
1981 err = got_error(GOT_ERR_NO_SPACE);
1982 goto done;
1985 done:
1986 if (err) {
1987 free(*path);
1988 *path = NULL;
1990 return err;
1993 static const struct got_error *
1994 blame_tree_entry(struct got_tree_entry *te, struct tog_parent_trees *parents,
1995 struct got_object_id *commit_id, struct got_repository *repo)
1997 const struct got_error *err = NULL;
1998 char *path;
2000 err = tree_entry_path(&path, parents, te);
2001 if (err)
2002 return err;
2004 err = show_blame_view(path, commit_id, repo);
2005 free(path);
2006 return err;
2009 static const struct got_error *
2010 show_tree_view(struct got_tree_object *root, struct got_object_id *commit_id,
2011 struct got_repository *repo)
2013 const struct got_error *err = NULL;
2014 int ch, done = 0, selected = 0, show_ids = 0;
2015 struct got_tree_object *tree = root;
2016 const struct got_tree_entries *entries;
2017 struct got_tree_entry *first_displayed_entry = NULL;
2018 struct got_tree_entry *last_displayed_entry = NULL;
2019 struct got_tree_entry *selected_entry = NULL;
2020 char *commit_id_str = NULL, *tree_label = NULL;
2021 int nentries, ndisplayed;
2022 struct tog_parent_trees parents;
2024 TAILQ_INIT(&parents);
2026 err = got_object_id_str(&commit_id_str, commit_id);
2027 if (err != NULL)
2028 goto done;
2030 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2031 err = got_error_from_errno();
2032 goto done;
2035 if (tog_tree_view.window == NULL) {
2036 tog_tree_view.window = newwin(0, 0, 0, 0);
2037 if (tog_tree_view.window == NULL)
2038 return got_error_from_errno();
2039 keypad(tog_tree_view.window, TRUE);
2041 if (tog_tree_view.panel == NULL) {
2042 tog_tree_view.panel = new_panel(tog_tree_view.window);
2043 if (tog_tree_view.panel == NULL)
2044 return got_error_from_errno();
2045 } else
2046 show_panel(tog_tree_view.panel);
2048 entries = got_object_tree_get_entries(root);
2049 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2050 while (!done) {
2051 char *parent_path;
2052 entries = got_object_tree_get_entries(tree);
2053 nentries = entries->nentries;
2054 if (tree != root)
2055 nentries++; /* '..' directory */
2057 err = tree_entry_path(&parent_path, &parents, NULL);
2058 if (err)
2059 goto done;
2061 err = draw_tree_entries(&first_displayed_entry,
2062 &last_displayed_entry, &selected_entry, &ndisplayed,
2063 tog_tree_view.window, tree_label, show_ids,
2064 parent_path, entries, selected, LINES, tree == root);
2065 free(parent_path);
2066 if (err)
2067 break;
2069 nodelay(stdscr, FALSE);
2070 ch = wgetch(tog_tree_view.window);
2071 nodelay(stdscr, TRUE);
2072 switch (ch) {
2073 case 'q':
2074 done = 1;
2075 break;
2076 case 'i':
2077 show_ids = !show_ids;
2078 break;
2079 case 'k':
2080 case KEY_UP:
2081 if (selected > 0)
2082 selected--;
2083 if (selected > 0)
2084 break;
2085 tree_scroll_up(&first_displayed_entry, 1,
2086 entries, tree == root);
2087 break;
2088 case KEY_PPAGE:
2089 if (SIMPLEQ_FIRST(&entries->head) ==
2090 first_displayed_entry) {
2091 if (tree != root)
2092 first_displayed_entry = NULL;
2093 selected = 0;
2094 break;
2096 tree_scroll_up(&first_displayed_entry, LINES,
2097 entries, tree == root);
2098 break;
2099 case 'j':
2100 case KEY_DOWN:
2101 if (selected < ndisplayed - 1) {
2102 selected++;
2103 break;
2105 tree_scroll_down(&first_displayed_entry, 1,
2106 last_displayed_entry, entries);
2107 break;
2108 case KEY_NPAGE:
2109 tree_scroll_down(&first_displayed_entry, LINES,
2110 last_displayed_entry, entries);
2111 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2112 break;
2113 /* can't scroll any further; move cursor down */
2114 if (selected < ndisplayed - 1)
2115 selected = ndisplayed - 1;
2116 break;
2117 case KEY_ENTER:
2118 case '\r':
2119 if (selected_entry == NULL) {
2120 struct tog_parent_tree *parent;
2121 case KEY_BACKSPACE:
2122 /* user selected '..' */
2123 if (tree == root)
2124 break;
2125 parent = TAILQ_FIRST(&parents);
2126 TAILQ_REMOVE(&parents, parent, entry);
2127 got_object_tree_close(tree);
2128 tree = parent->tree;
2129 first_displayed_entry =
2130 parent->first_displayed_entry;
2131 selected_entry = parent->selected_entry;
2132 selected = parent->selected;
2133 free(parent);
2134 } else if (S_ISDIR(selected_entry->mode)) {
2135 struct tog_parent_tree *parent;
2136 struct got_tree_object *child;
2137 err = got_object_open_as_tree(
2138 &child, repo, selected_entry->id);
2139 if (err)
2140 goto done;
2141 parent = calloc(1, sizeof(*parent));
2142 if (parent == NULL) {
2143 err = got_error_from_errno();
2144 goto done;
2146 parent->tree = tree;
2147 parent->first_displayed_entry =
2148 first_displayed_entry;
2149 parent->selected_entry = selected_entry;
2150 parent->selected = selected;
2151 TAILQ_INSERT_HEAD(&parents, parent,
2152 entry);
2153 tree = child;
2154 selected = 0;
2155 first_displayed_entry = NULL;
2156 } else if (S_ISREG(selected_entry->mode)) {
2157 err = blame_tree_entry(selected_entry,
2158 &parents, commit_id, repo);
2159 if (err)
2160 goto done;
2162 break;
2163 case KEY_RESIZE:
2164 if (selected > LINES)
2165 selected = ndisplayed - 1;
2166 break;
2167 default:
2168 break;
2171 done:
2172 free(tree_label);
2173 free(commit_id_str);
2174 while (!TAILQ_EMPTY(&parents)) {
2175 struct tog_parent_tree *parent;
2176 parent = TAILQ_FIRST(&parents);
2177 TAILQ_REMOVE(&parents, parent, entry);
2178 free(parent);
2181 if (tree != root)
2182 got_object_tree_close(tree);
2183 return err;
2186 __dead static void
2187 usage_tree(void)
2189 endwin();
2190 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2191 getprogname());
2192 exit(1);
2195 static const struct got_error *
2196 cmd_tree(int argc, char *argv[])
2198 const struct got_error *error;
2199 struct got_repository *repo = NULL;
2200 char *repo_path = NULL;
2201 struct got_object_id *commit_id = NULL;
2202 char *commit_id_arg = NULL;
2203 struct got_commit_object *commit = NULL;
2204 struct got_tree_object *tree = NULL;
2205 int ch;
2207 #ifndef PROFILE
2208 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2209 err(1, "pledge");
2210 #endif
2212 while ((ch = getopt(argc, argv, "c:")) != -1) {
2213 switch (ch) {
2214 case 'c':
2215 commit_id_arg = optarg;
2216 break;
2217 default:
2218 usage();
2219 /* NOTREACHED */
2223 argc -= optind;
2224 argv += optind;
2226 if (argc == 0) {
2227 repo_path = getcwd(NULL, 0);
2228 if (repo_path == NULL)
2229 return got_error_from_errno();
2230 } else if (argc == 1) {
2231 repo_path = realpath(argv[0], NULL);
2232 if (repo_path == NULL)
2233 return got_error_from_errno();
2234 } else
2235 usage_log();
2237 error = got_repo_open(&repo, repo_path);
2238 free(repo_path);
2239 if (error != NULL)
2240 return error;
2242 if (commit_id_arg == NULL) {
2243 error = get_head_commit_id(&commit_id, repo);
2244 if (error != NULL)
2245 goto done;
2246 } else {
2247 struct got_object *obj;
2248 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2249 if (error == NULL) {
2250 commit_id = got_object_get_id(obj);
2251 if (commit_id == NULL)
2252 error = got_error_from_errno();
2255 if (error != NULL)
2256 goto done;
2258 error = got_object_open_as_commit(&commit, repo, commit_id);
2259 if (error != NULL)
2260 goto done;
2262 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2263 if (error != NULL)
2264 goto done;
2266 error = show_tree_view(tree, commit_id, repo);
2267 done:
2268 free(commit_id);
2269 if (commit)
2270 got_object_commit_close(commit);
2271 if (tree)
2272 got_object_tree_close(tree);
2273 if (repo)
2274 got_repo_close(repo);
2275 return error;
2277 static void
2278 init_curses(void)
2280 initscr();
2281 cbreak();
2282 noecho();
2283 nonl();
2284 intrflush(stdscr, FALSE);
2285 keypad(stdscr, TRUE);
2286 curs_set(0);
2289 __dead static void
2290 usage(void)
2292 int i;
2294 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2295 "Available commands:\n", getprogname());
2296 for (i = 0; i < nitems(tog_commands); i++) {
2297 struct tog_cmd *cmd = &tog_commands[i];
2298 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2300 exit(1);
2303 static char **
2304 make_argv(const char *arg0, const char *arg1)
2306 char **argv;
2307 int argc = (arg1 == NULL ? 1 : 2);
2309 argv = calloc(argc, sizeof(char *));
2310 if (argv == NULL)
2311 err(1, "calloc");
2312 argv[0] = strdup(arg0);
2313 if (argv[0] == NULL)
2314 err(1, "calloc");
2315 if (arg1) {
2316 argv[1] = strdup(arg1);
2317 if (argv[1] == NULL)
2318 err(1, "calloc");
2321 return argv;
2324 int
2325 main(int argc, char *argv[])
2327 const struct got_error *error = NULL;
2328 struct tog_cmd *cmd = NULL;
2329 int ch, hflag = 0;
2330 char **cmd_argv = NULL;
2332 setlocale(LC_ALL, "");
2334 while ((ch = getopt(argc, argv, "h")) != -1) {
2335 switch (ch) {
2336 case 'h':
2337 hflag = 1;
2338 break;
2339 default:
2340 usage();
2341 /* NOTREACHED */
2345 argc -= optind;
2346 argv += optind;
2347 optind = 0;
2348 optreset = 1;
2350 if (argc == 0) {
2351 if (hflag)
2352 usage();
2353 /* Build an argument vector which runs a default command. */
2354 cmd = &tog_commands[0];
2355 cmd_argv = make_argv(cmd->name, NULL);
2356 argc = 1;
2357 } else {
2358 int i;
2360 /* Did the user specific a command? */
2361 for (i = 0; i < nitems(tog_commands); i++) {
2362 if (strncmp(tog_commands[i].name, argv[0],
2363 strlen(argv[0])) == 0) {
2364 cmd = &tog_commands[i];
2365 if (hflag)
2366 tog_commands[i].cmd_usage();
2367 break;
2370 if (cmd == NULL) {
2371 /* Did the user specify a repository? */
2372 char *repo_path = realpath(argv[0], NULL);
2373 if (repo_path) {
2374 struct got_repository *repo;
2375 error = got_repo_open(&repo, repo_path);
2376 if (error == NULL)
2377 got_repo_close(repo);
2378 } else
2379 error = got_error_from_errno();
2380 if (error) {
2381 if (hflag) {
2382 fprintf(stderr, "%s: '%s' is not a "
2383 "known command\n", getprogname(),
2384 argv[0]);
2385 usage();
2387 fprintf(stderr, "%s: '%s' is neither a known "
2388 "command nor a path to a repository\n",
2389 getprogname(), argv[0]);
2390 free(repo_path);
2391 return 1;
2393 cmd = &tog_commands[0];
2394 cmd_argv = make_argv(cmd->name, repo_path);
2395 argc = 2;
2396 free(repo_path);
2400 init_curses();
2402 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2403 if (error)
2404 goto done;
2405 done:
2406 endwin();
2407 free(cmd_argv);
2408 if (error)
2409 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2410 return 0;