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 enum tog_view_type {
86 TOG_VIEW_DIFF,
87 TOG_VIEW_LOG,
88 TOG_VIEW_BLAME,
89 TOG_VIEW_TREE
90 };
92 struct tog_diff_view_state {
93 struct got_object_id *id;
94 FILE *f;
95 int first_displayed_line;
96 int last_displayed_line;
97 int eof;
98 };
100 struct commit_queue_entry {
101 TAILQ_ENTRY(commit_queue_entry) entry;
102 struct got_object_id *id;
103 struct got_commit_object *commit;
104 };
105 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 struct commit_queue {
107 int ncommits;
108 struct commit_queue_head head;
109 };
111 struct tog_log_view_state {
112 struct got_commit_graph *graph;
113 struct commit_queue commits;
114 struct commit_queue_entry *first_displayed_entry;
115 struct commit_queue_entry *last_displayed_entry;
116 struct commit_queue_entry *selected_entry;
117 int selected;
118 char *in_repo_path;
119 struct got_repository *repo;
120 };
122 struct tog_blame_cb_args {
123 pthread_mutex_t *mutex;
124 struct tog_blame_line *lines; /* one per line */
125 int nlines;
127 struct tog_view *view;
128 struct got_object_id *commit_id;
129 FILE *f;
130 const char *path;
131 int *first_displayed_line;
132 int *last_displayed_line;
133 int *selected_line;
134 int *quit;
135 int *eof;
136 };
138 struct tog_blame_thread_args {
139 const char *path;
140 struct got_repository *repo;
141 struct tog_blame_cb_args *cb_args;
142 int *complete;
143 };
145 struct tog_blame {
146 FILE *f;
147 size_t filesize;
148 struct tog_blame_line *lines;
149 size_t nlines;
150 pthread_t thread;
151 struct tog_blame_thread_args thread_args;
152 struct tog_blame_cb_args cb_args;
153 const char *path;
154 };
156 struct tog_blame_view_state {
157 int first_displayed_line;
158 int last_displayed_line;
159 int selected_line;
160 int blame_complete;
161 int eof;
162 int done;
163 pthread_mutex_t mutex;
164 struct got_object_id_queue blamed_commits;
165 struct got_object_qid *blamed_commit;
166 char *path;
167 struct got_repository *repo;
168 struct got_object_id *commit_id;
169 struct tog_blame blame;
170 };
172 struct tog_parent_tree {
173 TAILQ_ENTRY(tog_parent_tree) entry;
174 struct got_tree_object *tree;
175 struct got_tree_entry *first_displayed_entry;
176 struct got_tree_entry *selected_entry;
177 int selected;
178 };
180 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 struct tog_tree_view_state {
183 char *tree_label;
184 struct got_tree_object *root;
185 struct got_tree_object *tree;
186 const struct got_tree_entries *entries;
187 struct got_tree_entry *first_displayed_entry;
188 struct got_tree_entry *last_displayed_entry;
189 struct got_tree_entry *selected_entry;
190 int nentries, ndisplayed, selected, show_ids;
191 struct tog_parent_trees parents;
192 struct got_object_id *commit_id;
193 struct got_repository *repo;
194 };
196 TAILQ_HEAD(tog_view_list_head, tog_view);
197 struct tog_view {
198 TAILQ_ENTRY(tog_view) entry;
199 WINDOW *window;
200 PANEL *panel;
201 int nlines, ncols, begin_y, begin_x;
202 int lines, cols; /* copies of LINES and COLS */
203 int focussed;
204 struct tog_view *parent;
205 struct tog_view *child;
207 /* type-specific state */
208 enum tog_view_type type;
209 union {
210 struct tog_diff_view_state diff;
211 struct tog_log_view_state log;
212 struct tog_blame_view_state blame;
213 struct tog_tree_view_state tree;
214 } state;
216 const struct got_error *(*show)(struct tog_view *);
217 const struct got_error *(*input)(struct tog_view **,
218 struct tog_view **, struct tog_view *, int);
219 const struct got_error *(*set_child)(struct tog_view *,
220 struct tog_view *);
221 const struct got_error *(*close)(struct tog_view *);
222 };
224 static const struct got_error *open_diff_view(struct tog_view *,
225 struct got_object *, struct got_object *, struct got_repository *);
226 static const struct got_error *show_diff_view(struct tog_view *);
227 static const struct got_error *input_diff_view(struct tog_view **,
228 struct tog_view **, struct tog_view *, int);
229 static const struct got_error* close_diff_view(struct tog_view *);
231 static const struct got_error *open_log_view(struct tog_view *,
232 struct got_object_id *, struct got_repository *, const char *);
233 static const struct got_error * show_log_view(struct tog_view *);
234 static const struct got_error *input_log_view(struct tog_view **,
235 struct tog_view **, struct tog_view *, int);
236 static const struct got_error *close_log_view(struct tog_view *);
237 static const struct got_error* set_child_log_view(struct tog_view *,
238 struct tog_view *);
240 static const struct got_error *open_blame_view(struct tog_view *, char *,
241 struct got_object_id *, struct got_repository *);
242 static const struct got_error *show_blame_view(struct tog_view *);
243 static const struct got_error *input_blame_view(struct tog_view **,
244 struct tog_view **, struct tog_view *, int);
245 static const struct got_error *close_blame_view(struct tog_view *);
247 static const struct got_error *open_tree_view(struct tog_view *,
248 struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 static const struct got_error *show_tree_view(struct tog_view *);
250 static const struct got_error *input_tree_view(struct tog_view **,
251 struct tog_view **, struct tog_view *, int);
252 static const struct got_error *close_tree_view(struct tog_view *);
254 static const struct got_error *
255 view_close(struct tog_view *view)
257 const struct got_error *err = NULL;
259 if (view->child)
260 view->child->parent = NULL;
261 if (view->parent)
262 view->parent->child = NULL;
263 if (view->close)
264 err = view->close(view);
265 if (view->panel)
266 del_panel(view->panel);
267 if (view->window)
268 delwin(view->window);
269 free(view);
270 return err;
273 static struct tog_view *
274 view_open(int nlines, int ncols, int begin_y, int begin_x,
275 struct tog_view *parent, enum tog_view_type type)
277 struct tog_view *view = calloc(1, sizeof(*view));
279 if (view == NULL)
280 return NULL;
282 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 begin_x = parent->ncols - 80;
285 view->parent = parent;
286 if (parent)
287 parent->child = view;
288 view->type = type;
289 view->lines = LINES;
290 view->cols = COLS;
291 view->nlines = nlines ? nlines : LINES - begin_y;
292 view->ncols = ncols ? ncols : COLS - begin_x;
293 view->begin_y = begin_y;
294 view->begin_x = begin_x;
295 view->window = newwin(nlines, ncols, begin_y, begin_x);
296 if (view->window == NULL) {
297 view_close(view);
298 return NULL;
300 view->panel = new_panel(view->window);
301 if (view->panel == NULL) {
302 view_close(view);
303 return NULL;
306 keypad(view->window, TRUE);
307 return view;
310 static const struct got_error *
311 view_show(struct tog_view *view)
313 const struct got_error *err;
315 if (view->parent) {
316 err = view->parent->show(view->parent);
317 if (err)
318 return err;
319 show_panel(view->parent->panel);
322 err = view->show(view);
323 if (err)
324 return err;
325 show_panel(view->panel);
327 if (view->child) {
328 err = view->child->show(view->child);
329 if (err)
330 return err;
331 show_panel(view->child->panel);
334 update_panels();
335 doupdate();
337 return err;
340 static const struct got_error *
341 view_resize(struct tog_view *view)
343 int nlines, ncols;
345 while (view) {
346 if (view->lines > LINES)
347 nlines = view->nlines - (view->lines - LINES);
348 else
349 nlines = view->nlines + (LINES - view->lines);
351 if (view->cols > COLS)
352 ncols = view->ncols - (view->cols - COLS);
353 else
354 ncols = view->ncols + (COLS - view->cols);
356 if (wresize(view->window, nlines, ncols) == ERR)
357 return got_error_from_errno();
358 replace_panel(view->panel, view->window);
360 view->nlines = nlines;
361 view->ncols = ncols;
362 view->lines = LINES;
363 view->cols = COLS;
365 view = view->parent;
368 return NULL;
371 static const struct got_error *
372 view_input(struct tog_view **new, struct tog_view **dead,
373 struct tog_view **focus, int *done, struct tog_view *view,
374 struct tog_view_list_head *views)
376 const struct got_error *err = NULL;
377 struct tog_view *next, *prev;
378 int ch;
380 *new = NULL;
381 *dead = NULL;
383 nodelay(stdscr, FALSE);
384 ch = wgetch(view->window);
385 nodelay(stdscr, TRUE);
386 switch (ch) {
387 case ERR:
388 break;
389 case '\t':
390 next = TAILQ_NEXT(view, entry);
391 if (next)
392 *focus = next;
393 else
394 *focus = TAILQ_FIRST(views);
395 view->focussed = 0;
396 (*focus)->focussed = 1;
397 break;
398 case KEY_BACKSPACE:
399 prev = TAILQ_PREV(view, tog_view_list_head, entry);
400 if (prev)
401 *focus = prev;
402 else
403 *focus = TAILQ_LAST(views, tog_view_list_head);
404 break;
405 case 'q':
406 err = view->input(new, dead, view, ch);
407 *dead = view;
408 break;
409 case 'Q':
410 *done = 1;
411 break;
412 case KEY_RESIZE:
413 err = view_resize(view);
414 if (err)
415 return err;
416 err = view->input(new, dead, view, ch);
417 break;
418 default:
419 err = view->input(new, dead, view, ch);
420 break;
423 return err;
426 static const struct got_error *
427 view_set_child(struct tog_view *view, struct tog_view *child)
429 const struct got_error *err;
431 if (view->set_child) {
432 err = view->set_child(view, child);
433 if (err)
434 return err;
437 view->child = child;
438 return NULL;
441 void
442 view_vborder(struct tog_view *view)
444 if (view->child == NULL)
445 return;
447 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
448 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
451 static const struct got_error *
452 view_loop(struct tog_view *view)
454 const struct got_error *err = NULL;
455 struct tog_view_list_head views;
456 struct tog_view *new_view, *dead_view;
457 int done = 0;
459 TAILQ_INIT(&views);
460 TAILQ_INSERT_HEAD(&views, view, entry);
462 view->focussed = 1;
463 while (!TAILQ_EMPTY(&views) && !done) {
464 err = view_show(view);
465 if (err)
466 break;
467 err = view_input(&new_view, &dead_view, &view, &done,
468 view, &views);
469 if (err)
470 break;
471 if (dead_view) {
472 TAILQ_REMOVE(&views, dead_view, entry);
473 TAILQ_FOREACH(view, &views, entry) {
474 if (view->parent == dead_view)
475 view->parent = NULL;
477 if (dead_view->parent)
478 view = dead_view->parent;
479 else
480 view = TAILQ_LAST(&views, tog_view_list_head);
481 if (dead_view->child) {
482 TAILQ_REMOVE(&views, dead_view->child, entry);
483 err = view_close(dead_view->child);
484 if (err)
485 goto done;
487 err = view_close(dead_view);
488 if (err)
489 goto done;
491 if (new_view) {
492 /* TODO: de-duplicate! */
493 TAILQ_INSERT_TAIL(&views, new_view, entry);
494 if (new_view->parent) {
495 err = view_set_child(new_view->parent, new_view);
496 if (err)
497 goto done;
498 new_view->parent->focussed = 0;
500 view = new_view;
501 view->focussed = 1;
504 done:
505 while (!TAILQ_EMPTY(&views)) {
506 view = TAILQ_FIRST(&views);
507 TAILQ_REMOVE(&views, view, entry);
508 view_close(view);
510 return err;
513 __dead static void
514 usage_log(void)
516 endwin();
517 fprintf(stderr,
518 "usage: %s log [-c commit] [-r repository-path] [path]\n",
519 getprogname());
520 exit(1);
523 /* Create newly allocated wide-character string equivalent to a byte string. */
524 static const struct got_error *
525 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
527 char *vis = NULL;
528 const struct got_error *err = NULL;
530 *ws = NULL;
531 *wlen = mbstowcs(NULL, s, 0);
532 if (*wlen == (size_t)-1) {
533 int vislen;
534 if (errno != EILSEQ)
535 return got_error_from_errno();
537 /* byte string invalid in current encoding; try to "fix" it */
538 err = got_mbsavis(&vis, &vislen, s);
539 if (err)
540 return err;
541 *wlen = mbstowcs(NULL, vis, 0);
542 if (*wlen == (size_t)-1) {
543 err = got_error_from_errno(); /* give up */
544 goto done;
548 *ws = calloc(*wlen + 1, sizeof(*ws));
549 if (*ws == NULL) {
550 err = got_error_from_errno();
551 goto done;
554 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
555 err = got_error_from_errno();
556 done:
557 free(vis);
558 if (err) {
559 free(*ws);
560 *ws = NULL;
561 *wlen = 0;
563 return err;
566 /* Format a line for display, ensuring that it won't overflow a width limit. */
567 static const struct got_error *
568 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
570 const struct got_error *err = NULL;
571 int cols = 0;
572 wchar_t *wline = NULL;
573 size_t wlen;
574 int i;
576 *wlinep = NULL;
577 *widthp = 0;
579 err = mbs2ws(&wline, &wlen, line);
580 if (err)
581 return err;
583 i = 0;
584 while (i < wlen && cols < wlimit) {
585 int width = wcwidth(wline[i]);
586 switch (width) {
587 case 0:
588 i++;
589 break;
590 case 1:
591 case 2:
592 if (cols + width <= wlimit) {
593 cols += width;
594 i++;
596 break;
597 case -1:
598 if (wline[i] == L'\t')
599 cols += TABSIZE - ((cols + 1) % TABSIZE);
600 i++;
601 break;
602 default:
603 err = got_error_from_errno();
604 goto done;
607 wline[i] = L'\0';
608 if (widthp)
609 *widthp = cols;
610 done:
611 if (err)
612 free(wline);
613 else
614 *wlinep = wline;
615 return err;
618 static const struct got_error *
619 draw_commit(struct tog_view *view, struct got_commit_object *commit,
620 struct got_object_id *id)
622 const struct got_error *err = NULL;
623 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
624 char *logmsg0 = NULL, *logmsg = NULL;
625 char *author0 = NULL, *author = NULL;
626 wchar_t *wlogmsg = NULL, *wauthor = NULL;
627 int author_width, logmsg_width;
628 char *newline, *smallerthan;
629 char *line = NULL;
630 int col, limit;
631 static const size_t date_display_cols = 9;
632 static const size_t author_display_cols = 16;
633 const int avail = view->ncols;
635 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
636 &commit->tm_committer) >= sizeof(datebuf))
637 return got_error(GOT_ERR_NO_SPACE);
639 if (avail < date_display_cols)
640 limit = MIN(sizeof(datebuf) - 1, avail);
641 else
642 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
643 waddnstr(view->window, datebuf, limit);
644 col = limit + 1;
645 if (col > avail)
646 goto done;
648 author0 = strdup(commit->author);
649 if (author0 == NULL) {
650 err = got_error_from_errno();
651 goto done;
653 author = author0;
654 smallerthan = strchr(author, '<');
655 if (smallerthan)
656 *smallerthan = '\0';
657 else {
658 char *at = strchr(author, '@');
659 if (at)
660 *at = '\0';
662 limit = avail - col;
663 err = format_line(&wauthor, &author_width, author, limit);
664 if (err)
665 goto done;
666 waddwstr(view->window, wauthor);
667 col += author_width;
668 while (col <= avail && author_width < author_display_cols + 1) {
669 waddch(view->window, ' ');
670 col++;
671 author_width++;
673 if (col > avail)
674 goto done;
676 logmsg0 = strdup(commit->logmsg);
677 if (logmsg0 == NULL) {
678 err = got_error_from_errno();
679 goto done;
681 logmsg = logmsg0;
682 while (*logmsg == '\n')
683 logmsg++;
684 newline = strchr(logmsg, '\n');
685 if (newline)
686 *newline = '\0';
687 limit = avail - col;
688 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
689 if (err)
690 goto done;
691 waddwstr(view->window, wlogmsg);
692 col += logmsg_width;
693 while (col <= avail) {
694 waddch(view->window, ' ');
695 col++;
697 done:
698 free(logmsg0);
699 free(wlogmsg);
700 free(author0);
701 free(wauthor);
702 free(line);
703 return err;
706 static struct commit_queue_entry *
707 alloc_commit_queue_entry(struct got_commit_object *commit,
708 struct got_object_id *id)
710 struct commit_queue_entry *entry;
712 entry = calloc(1, sizeof(*entry));
713 if (entry == NULL)
714 return NULL;
716 entry->id = id;
717 entry->commit = commit;
718 return entry;
721 static void
722 pop_commit(struct commit_queue *commits)
724 struct commit_queue_entry *entry;
726 entry = TAILQ_FIRST(&commits->head);
727 TAILQ_REMOVE(&commits->head, entry, entry);
728 got_object_commit_close(entry->commit);
729 commits->ncommits--;
730 /* Don't free entry->id! It is owned by the commit graph. */
731 free(entry);
734 static void
735 free_commits(struct commit_queue *commits)
737 while (!TAILQ_EMPTY(&commits->head))
738 pop_commit(commits);
741 static const struct got_error *
742 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
743 struct got_object_id *start_id, int minqueue, int init,
744 struct got_repository *repo, const char *path)
746 const struct got_error *err = NULL;
747 struct got_object_id *id;
748 struct commit_queue_entry *entry;
749 int nfetched, nqueued = 0, found_obj = 0;
750 int is_root_path = strcmp(path, "/") == 0;
752 err = got_commit_graph_iter_start(graph, start_id);
753 if (err)
754 return err;
756 entry = TAILQ_LAST(&commits->head, commit_queue_head);
757 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
758 int nfetched;
760 /* Start ID's commit is already on the queue; skip over it. */
761 err = got_commit_graph_iter_next(&id, graph);
762 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
763 return err;
765 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
766 if (err)
767 return err;
770 while (1) {
771 struct got_commit_object *commit;
773 err = got_commit_graph_iter_next(&id, graph);
774 if (err) {
775 if (err->code != GOT_ERR_ITER_NEED_MORE)
776 break;
777 if (nqueued >= minqueue) {
778 err = NULL;
779 break;
781 err = got_commit_graph_fetch_commits(&nfetched,
782 graph, 1, repo);
783 if (err)
784 return err;
785 continue;
787 if (id == NULL)
788 break;
790 err = got_object_open_as_commit(&commit, repo, id);
791 if (err)
792 break;
794 if (!is_root_path) {
795 struct got_object *obj;
796 struct got_object_qid *pid;
797 int changed = 0;
799 err = got_object_open_by_path(&obj, repo, id, path);
800 if (err) {
801 got_object_commit_close(commit);
802 if (err->code == GOT_ERR_NO_OBJ &&
803 (found_obj || !init)) {
804 /* History stops here. */
805 err = got_error(GOT_ERR_ITER_COMPLETED);
807 break;
809 found_obj = 1;
811 pid = SIMPLEQ_FIRST(&commit->parent_ids);
812 if (pid != NULL) {
813 struct got_object *pobj;
814 err = got_object_open_by_path(&pobj, repo,
815 pid->id, path);
816 if (err) {
817 if (err->code != GOT_ERR_NO_OBJ) {
818 got_object_close(obj);
819 got_object_commit_close(commit);
820 break;
822 err = NULL;
823 changed = 1;
824 } else {
825 struct got_object_id *id, *pid;
826 id = got_object_get_id(obj);
827 if (id == NULL) {
828 err = got_error_from_errno();
829 got_object_close(obj);
830 got_object_close(pobj);
831 break;
833 pid = got_object_get_id(pobj);
834 if (pid == NULL) {
835 err = got_error_from_errno();
836 free(id);
837 got_object_close(obj);
838 got_object_close(pobj);
839 break;
841 changed =
842 (got_object_id_cmp(id, pid) != 0);
843 got_object_close(pobj);
844 free(id);
845 free(pid);
848 got_object_close(obj);
849 if (!changed) {
850 got_object_commit_close(commit);
851 continue;
855 entry = alloc_commit_queue_entry(commit, id);
856 if (entry == NULL) {
857 err = got_error_from_errno();
858 break;
860 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
861 nqueued++;
862 commits->ncommits++;
865 return err;
868 static const struct got_error *
869 fetch_next_commit(struct commit_queue_entry **pentry,
870 struct commit_queue_entry *entry, struct commit_queue *commits,
871 struct got_commit_graph *graph, struct got_repository *repo,
872 const char *path)
874 const struct got_error *err = NULL;
876 *pentry = NULL;
878 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
879 if (err)
880 return err;
882 /* Next entry to display should now be available. */
883 *pentry = TAILQ_NEXT(entry, entry);
884 if (*pentry == NULL)
885 return got_error(GOT_ERR_NO_OBJ);
887 return NULL;
890 static const struct got_error *
891 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
893 const struct got_error *err = NULL;
894 struct got_reference *head_ref;
896 *head_id = NULL;
898 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
899 if (err)
900 return err;
902 err = got_ref_resolve(head_id, repo, head_ref);
903 got_ref_close(head_ref);
904 if (err) {
905 *head_id = NULL;
906 return err;
909 return NULL;
912 static const struct got_error *
913 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
914 struct commit_queue_entry **selected, struct commit_queue_entry *first,
915 struct commit_queue *commits, int selected_idx, int limit,
916 struct got_commit_graph *graph, struct got_repository *repo,
917 const char *path)
919 const struct got_error *err = NULL;
920 struct commit_queue_entry *entry;
921 int ncommits, width;
922 char *id_str, *header;
923 wchar_t *wline;
925 entry = first;
926 ncommits = 0;
927 while (entry) {
928 if (ncommits == selected_idx) {
929 *selected = entry;
930 break;
932 entry = TAILQ_NEXT(entry, entry);
933 ncommits++;
936 err = got_object_id_str(&id_str, (*selected)->id);
937 if (err)
938 return err;
940 if (path && strcmp(path, "/") != 0) {
941 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
942 err = got_error_from_errno();
943 free(id_str);
944 return err;
946 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
947 err = got_error_from_errno();
948 free(id_str);
949 return err;
951 free(id_str);
952 err = format_line(&wline, &width, header, view->ncols);
953 if (err) {
954 free(header);
955 return err;
957 free(header);
959 werase(view->window);
961 waddwstr(view->window, wline);
962 if (width < view->ncols)
963 waddch(view->window, '\n');
964 free(wline);
965 if (limit <= 1)
966 return NULL;
968 entry = first;
969 *last = first;
970 ncommits = 0;
971 while (entry) {
972 if (ncommits >= limit - 1)
973 break;
974 if (ncommits == selected_idx)
975 wstandout(view->window);
976 err = draw_commit(view, entry->commit, entry->id);
977 if (ncommits == selected_idx)
978 wstandend(view->window);
979 if (err)
980 break;
981 ncommits++;
982 *last = entry;
983 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
984 err = queue_commits(graph, commits, entry->id, 1,
985 0, repo, path);
986 if (err) {
987 if (err->code != GOT_ERR_ITER_COMPLETED)
988 return err;
989 err = NULL;
992 entry = TAILQ_NEXT(entry, entry);
995 view_vborder(view);
997 return err;
1000 static void
1001 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1002 struct commit_queue *commits)
1004 struct commit_queue_entry *entry;
1005 int nscrolled = 0;
1007 entry = TAILQ_FIRST(&commits->head);
1008 if (*first_displayed_entry == entry)
1009 return;
1011 entry = *first_displayed_entry;
1012 while (entry && nscrolled < maxscroll) {
1013 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1014 if (entry) {
1015 *first_displayed_entry = entry;
1016 nscrolled++;
1021 static const struct got_error *
1022 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1023 struct commit_queue_entry *last_displayed_entry,
1024 struct commit_queue *commits, struct got_commit_graph *graph,
1025 struct got_repository *repo, const char *path)
1027 const struct got_error *err = NULL;
1028 struct commit_queue_entry *pentry;
1029 int nscrolled = 0;
1031 do {
1032 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1033 if (pentry == NULL) {
1034 err = fetch_next_commit(&pentry, last_displayed_entry,
1035 commits, graph, repo, path);
1036 if (err || pentry == NULL)
1037 break;
1039 last_displayed_entry = pentry;
1041 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1042 if (pentry == NULL)
1043 break;
1044 *first_displayed_entry = pentry;
1045 } while (++nscrolled < maxscroll);
1047 return err;
1050 static const struct got_error *
1051 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1052 struct commit_queue_entry *entry, struct got_repository *repo)
1054 const struct got_error *err;
1055 struct got_object *obj1 = NULL, *obj2 = NULL;
1056 struct got_object_qid *parent_id;
1057 struct tog_view *diff_view;
1059 err = got_object_open(&obj2, repo, entry->id);
1060 if (err)
1061 return err;
1063 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1064 if (parent_id) {
1065 err = got_object_open(&obj1, repo, parent_id->id);
1066 if (err)
1067 goto done;
1070 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1071 if (diff_view == NULL) {
1072 err = got_error_from_errno();
1073 goto done;
1076 err = open_diff_view(diff_view, obj1, obj2, repo);
1077 if (err == NULL)
1078 *new_view = diff_view;
1079 done:
1080 if (obj1)
1081 got_object_close(obj1);
1082 if (obj2)
1083 got_object_close(obj2);
1084 return err;
1087 static const struct got_error *
1088 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1089 struct commit_queue_entry *entry, struct got_repository *repo)
1091 const struct got_error *err = NULL;
1092 struct got_tree_object *tree;
1093 struct tog_view *tree_view;
1095 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1096 if (err)
1097 return err;
1099 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1100 if (tree_view == NULL)
1101 return got_error_from_errno();
1103 err = open_tree_view(tree_view, tree, entry->id, repo);
1104 if (err)
1105 got_object_tree_close(tree);
1106 else
1107 *new_view = tree_view;
1108 return err;
1111 static const struct got_error *
1112 set_child_log_view(struct tog_view *view, struct tog_view *child)
1114 struct tog_log_view_state *s = &view->state.log;
1115 struct tog_diff_view_state *ds;
1116 struct commit_queue_entry *commit, *child_entry = NULL;
1117 int selected_idx = 0;
1119 if (child->type != TOG_VIEW_DIFF)
1120 return NULL;
1121 ds = &child->state.diff;
1123 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1124 if (got_object_id_cmp(commit->id, ds->id) == 0) {
1125 child_entry = commit;
1126 break;
1129 if (child_entry == NULL)
1130 return NULL;
1132 commit = s->first_displayed_entry;
1133 while (commit) {
1134 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1135 s->selected_entry = child_entry;
1136 s->selected = selected_idx;
1137 break;
1139 if (commit == s->last_displayed_entry)
1140 break;
1141 selected_idx++;
1142 commit = TAILQ_NEXT(commit, entry);
1145 return show_log_view(view);
1148 static const struct got_error *
1149 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1150 struct got_repository *repo, const char *path)
1152 const struct got_error *err = NULL;
1153 struct got_object_id *head_id = NULL;
1154 int nfetched;
1155 struct tog_log_view_state *s = &view->state.log;
1157 err = got_repo_map_path(&s->in_repo_path, repo, path);
1158 if (err != NULL)
1159 goto done;
1161 err = get_head_commit_id(&head_id, repo);
1162 if (err)
1163 return err;
1165 /* The graph contains all commits. */
1166 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1167 if (err)
1168 goto done;
1169 /* The commit queue contains a subset of commits filtered by path. */
1170 TAILQ_INIT(&s->commits.head);
1171 s->commits.ncommits = 0;
1173 /* Populate commit graph with a sufficient number of commits. */
1174 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1175 start_id, repo);
1176 if (err)
1177 goto done;
1180 * Open the initial batch of commits, sorted in commit graph order.
1181 * We keep all commits open throughout the lifetime of the log view
1182 * in order to avoid having to re-fetch commits from disk while
1183 * updating the display.
1185 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1186 repo, s->in_repo_path);
1187 if (err) {
1188 if (err->code != GOT_ERR_ITER_COMPLETED)
1189 goto done;
1190 err = NULL;
1193 s->first_displayed_entry =
1194 TAILQ_FIRST(&s->commits.head);
1195 s->selected_entry = s->first_displayed_entry;
1196 s->repo = repo;
1198 view->show = show_log_view;
1199 view->input = input_log_view;
1200 view->close = close_log_view;
1201 view->set_child = set_child_log_view;
1202 done:
1203 free(head_id);
1204 return err;
1207 static const struct got_error *
1208 close_log_view(struct tog_view *view)
1210 struct tog_log_view_state *s = &view->state.log;
1212 if (s->graph)
1213 got_commit_graph_close(s->graph);
1214 free_commits(&s->commits);
1215 free(s->in_repo_path);
1216 return NULL;
1219 static const struct got_error *
1220 update_diff_child_view(struct tog_view *parent,
1221 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 struct tog_diff_view_state *ds;
1225 struct got_object *obj1 = NULL, *obj2 = NULL;
1226 struct got_object_qid *parent_id;
1227 struct tog_view *child_view = parent->child;
1229 if (child_view == NULL)
1230 return NULL;
1231 if (child_view->type != TOG_VIEW_DIFF)
1232 return NULL;
1233 ds = &child_view->state.diff;
1234 if (got_object_id_cmp(ds->id, selected_entry->id) == 0)
1235 return NULL;
1237 err = got_object_open(&obj2, repo, selected_entry->id);
1238 if (err)
1239 return err;
1241 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1242 if (parent_id) {
1243 err = got_object_open(&obj1, repo, parent_id->id);
1244 if (err)
1245 goto done;
1248 err = close_diff_view(child_view);
1249 if (err)
1250 goto done;
1252 err = open_diff_view(child_view, obj1, obj2, repo);
1253 if (err)
1254 goto done;
1255 done:
1256 if (obj1)
1257 got_object_close(obj1);
1258 if (obj2)
1259 got_object_close(obj2);
1260 return err;
1263 static const struct got_error *
1264 show_log_view(struct tog_view *view)
1266 const struct got_error *err = NULL;
1267 struct tog_log_view_state *s = &view->state.log;
1269 err = draw_commits(view, &s->last_displayed_entry,
1270 &s->selected_entry, s->first_displayed_entry,
1271 &s->commits, s->selected, view->nlines, s->graph,
1272 s->repo, s->in_repo_path);
1273 if (err)
1274 return err;
1276 return update_diff_child_view(view, s->selected_entry, s->repo);
1279 static const struct got_error *
1280 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1281 struct tog_view *view, int ch)
1283 const struct got_error *err = NULL;
1284 struct tog_log_view_state *s = &view->state.log;
1286 switch (ch) {
1287 case 'k':
1288 case KEY_UP:
1289 case '[':
1290 if (s->selected > 0)
1291 s->selected--;
1292 if (s->selected > 0)
1293 break;
1294 scroll_up(&s->first_displayed_entry, 1,
1295 &s->commits);
1296 break;
1297 case KEY_PPAGE:
1298 if (TAILQ_FIRST(&s->commits.head) ==
1299 s->first_displayed_entry) {
1300 s->selected = 0;
1301 break;
1303 scroll_up(&s->first_displayed_entry,
1304 view->nlines, &s->commits);
1305 break;
1306 case 'j':
1307 case KEY_DOWN:
1308 case ']':
1309 if (s->selected < MIN(view->nlines - 2,
1310 s->commits.ncommits - 1)) {
1311 s->selected++;
1312 break;
1314 err = scroll_down(&s->first_displayed_entry, 1,
1315 s->last_displayed_entry, &s->commits,
1316 s->graph, s->repo, s->in_repo_path);
1317 if (err) {
1318 if (err->code != GOT_ERR_ITER_COMPLETED)
1319 break;
1320 err = NULL;
1322 break;
1323 case KEY_NPAGE: {
1324 struct commit_queue_entry *first;
1325 first = s->first_displayed_entry;
1326 err = scroll_down(&s->first_displayed_entry,
1327 view->nlines, s->last_displayed_entry,
1328 &s->commits, s->graph, s->repo,
1329 s->in_repo_path);
1330 if (err == NULL)
1331 break;
1332 if (err->code != GOT_ERR_ITER_COMPLETED)
1333 break;
1334 if (first == s->first_displayed_entry &&
1335 s->selected < MIN(view->nlines - 2,
1336 s->commits.ncommits - 1)) {
1337 /* can't scroll further down */
1338 s->selected = MIN(view->nlines - 2,
1339 s->commits.ncommits - 1);
1341 err = NULL;
1342 break;
1344 case KEY_RESIZE:
1345 if (s->selected > view->nlines - 2)
1346 s->selected = view->nlines - 2;
1347 if (s->selected > s->commits.ncommits - 1)
1348 s->selected = s->commits.ncommits - 1;
1349 break;
1350 case KEY_ENTER:
1351 case '\r':
1352 err = show_commit(new_view, view, s->selected_entry,
1353 s->repo);
1354 break;
1355 case 't':
1356 err = browse_commit(new_view, view, s->selected_entry,
1357 s->repo);
1358 break;
1359 default:
1360 break;
1363 return err;
1366 static const struct got_error *
1367 cmd_log(int argc, char *argv[])
1369 const struct got_error *error;
1370 struct got_repository *repo = NULL;
1371 struct got_object_id *start_id = NULL;
1372 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1373 char *start_commit = NULL;
1374 int ch;
1375 struct tog_view *view;
1377 #ifndef PROFILE
1378 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1379 err(1, "pledge");
1380 #endif
1382 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1383 switch (ch) {
1384 case 'c':
1385 start_commit = optarg;
1386 break;
1387 case 'r':
1388 repo_path = realpath(optarg, NULL);
1389 if (repo_path == NULL)
1390 err(1, "-r option");
1391 break;
1392 default:
1393 usage();
1394 /* NOTREACHED */
1398 argc -= optind;
1399 argv += optind;
1401 if (argc == 0)
1402 path = strdup("");
1403 else if (argc == 1)
1404 path = strdup(argv[0]);
1405 else
1406 usage_log();
1407 if (path == NULL)
1408 return got_error_from_errno();
1410 cwd = getcwd(NULL, 0);
1411 if (cwd == NULL) {
1412 error = got_error_from_errno();
1413 goto done;
1415 if (repo_path == NULL) {
1416 repo_path = strdup(cwd);
1417 if (repo_path == NULL) {
1418 error = got_error_from_errno();
1419 goto done;
1423 error = got_repo_open(&repo, repo_path);
1424 if (error != NULL)
1425 goto done;
1427 if (start_commit == NULL) {
1428 error = get_head_commit_id(&start_id, repo);
1429 if (error != NULL)
1430 goto done;
1431 } else {
1432 struct got_object *obj;
1433 error = got_object_open_by_id_str(&obj, repo, start_commit);
1434 if (error == NULL) {
1435 start_id = got_object_get_id(obj);
1436 if (start_id == NULL)
1437 error = got_error_from_errno();
1438 goto done;
1441 if (error != NULL)
1442 goto done;
1444 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1445 if (view == NULL) {
1446 error = got_error_from_errno();
1447 goto done;
1449 error = open_log_view(view, start_id, repo, path);
1450 if (error)
1451 goto done;
1452 error = view_loop(view);
1453 done:
1454 free(repo_path);
1455 free(cwd);
1456 free(path);
1457 free(start_id);
1458 if (repo)
1459 got_repo_close(repo);
1460 return error;
1463 __dead static void
1464 usage_diff(void)
1466 endwin();
1467 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1468 getprogname());
1469 exit(1);
1472 static char *
1473 parse_next_line(FILE *f, size_t *len)
1475 char *line;
1476 size_t linelen;
1477 size_t lineno;
1478 const char delim[3] = { '\0', '\0', '\0'};
1480 line = fparseln(f, &linelen, &lineno, delim, 0);
1481 if (len)
1482 *len = linelen;
1483 return line;
1486 static const struct got_error *
1487 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1488 int *last_displayed_line, int *eof, int max_lines)
1490 const struct got_error *err;
1491 int nlines = 0, nprinted = 0;
1492 char *line;
1493 size_t len;
1494 wchar_t *wline;
1495 int width;
1497 rewind(f);
1498 werase(view->window);
1500 *eof = 0;
1501 while (nprinted < max_lines) {
1502 line = parse_next_line(f, &len);
1503 if (line == NULL) {
1504 *eof = 1;
1505 break;
1507 if (++nlines < *first_displayed_line) {
1508 free(line);
1509 continue;
1512 err = format_line(&wline, &width, line, view->ncols);
1513 if (err) {
1514 free(line);
1515 free(wline);
1516 return err;
1518 waddwstr(view->window, wline);
1519 if (width < view->ncols)
1520 waddch(view->window, '\n');
1521 if (++nprinted == 1)
1522 *first_displayed_line = nlines;
1523 free(line);
1524 free(wline);
1525 wline = NULL;
1527 *last_displayed_line = nlines;
1529 view_vborder(view);
1531 return NULL;
1534 static const struct got_error *
1535 open_diff_view(struct tog_view *view, struct got_object *obj1,
1536 struct got_object *obj2, struct got_repository *repo)
1538 const struct got_error *err;
1539 FILE *f;
1541 if (obj1 != NULL && obj2 != NULL &&
1542 got_object_get_type(obj1) != got_object_get_type(obj2))
1543 return got_error(GOT_ERR_OBJ_TYPE);
1545 f = got_opentemp();
1546 if (f == NULL)
1547 return got_error_from_errno();
1549 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1550 case GOT_OBJ_TYPE_BLOB:
1551 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1552 break;
1553 case GOT_OBJ_TYPE_TREE:
1554 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1555 break;
1556 case GOT_OBJ_TYPE_COMMIT:
1557 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1558 break;
1559 default:
1560 return got_error(GOT_ERR_OBJ_TYPE);
1563 fflush(f);
1565 view->state.diff.id = got_object_get_id(obj2);
1566 if (view->state.diff.id == NULL)
1567 return got_error_from_errno();
1568 view->state.diff.f = f;
1569 view->state.diff.first_displayed_line = 1;
1570 view->state.diff.last_displayed_line = view->nlines;
1572 view->show = show_diff_view;
1573 view->input = input_diff_view;
1574 view->close = close_diff_view;
1576 return NULL;
1579 static const struct got_error *
1580 close_diff_view(struct tog_view *view)
1582 const struct got_error *err = NULL;
1584 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1585 err = got_error_from_errno();
1586 free(view->state.diff.id);
1587 return err;
1590 static const struct got_error *
1591 show_diff_view(struct tog_view *view)
1593 struct tog_diff_view_state *s = &view->state.diff;
1595 return draw_file(view, s->f, &s->first_displayed_line,
1596 &s->last_displayed_line, &s->eof, view->nlines);
1599 static const struct got_error *
1600 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1601 struct tog_view *view, int ch)
1603 const struct got_error *err = NULL;
1604 struct tog_diff_view_state *s = &view->state.diff;
1605 int i;
1607 switch (ch) {
1608 case 'k':
1609 case KEY_UP:
1610 if (s->first_displayed_line > 1)
1611 s->first_displayed_line--;
1612 break;
1613 case KEY_PPAGE:
1614 i = 0;
1615 while (i++ < view->nlines - 1 &&
1616 s->first_displayed_line > 1)
1617 s->first_displayed_line--;
1618 break;
1619 case 'j':
1620 case KEY_DOWN:
1621 if (!s->eof)
1622 s->first_displayed_line++;
1623 break;
1624 case KEY_NPAGE:
1625 case ' ':
1626 i = 0;
1627 while (!s->eof && i++ < view->nlines - 1) {
1628 char *line;
1629 line = parse_next_line(s->f, NULL);
1630 s->first_displayed_line++;
1631 if (line == NULL)
1632 break;
1634 break;
1635 case '[':
1636 case ']': {
1637 struct tog_log_view_state *ls;
1638 struct commit_queue_entry *entry;
1639 struct tog_view *diff_view;
1641 if (view->parent == NULL)
1642 break;
1643 if (view->parent->type != TOG_VIEW_LOG)
1644 break;
1645 ls = &view->parent->state.log;
1647 if (ch == '[') {
1648 entry = TAILQ_PREV(ls->selected_entry,
1649 commit_queue_head, entry);
1650 } else {
1651 entry = TAILQ_NEXT(ls->selected_entry, entry);
1652 if (entry == NULL) {
1653 err = fetch_next_commit(&entry,
1654 ls->selected_entry,
1655 &ls->commits, ls->graph,
1656 ls->repo, ls->in_repo_path);
1657 if (err)
1658 break;
1661 if (entry == NULL)
1662 break;
1663 err = show_commit(&diff_view, view->parent,
1664 entry, ls->repo);
1665 if (err)
1666 break;
1667 *new_view = diff_view;
1668 *dead_view = view;
1669 break;
1671 default:
1672 break;
1675 return err;
1678 static const struct got_error *
1679 cmd_diff(int argc, char *argv[])
1681 const struct got_error *error = NULL;
1682 struct got_repository *repo = NULL;
1683 struct got_object *obj1 = NULL, *obj2 = NULL;
1684 char *repo_path = NULL;
1685 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1686 int ch;
1687 struct tog_view *view;
1689 #ifndef PROFILE
1690 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1691 err(1, "pledge");
1692 #endif
1694 while ((ch = getopt(argc, argv, "")) != -1) {
1695 switch (ch) {
1696 default:
1697 usage();
1698 /* NOTREACHED */
1702 argc -= optind;
1703 argv += optind;
1705 if (argc == 0) {
1706 usage_diff(); /* TODO show local worktree changes */
1707 } else if (argc == 2) {
1708 repo_path = getcwd(NULL, 0);
1709 if (repo_path == NULL)
1710 return got_error_from_errno();
1711 obj_id_str1 = argv[0];
1712 obj_id_str2 = argv[1];
1713 } else if (argc == 3) {
1714 repo_path = realpath(argv[0], NULL);
1715 if (repo_path == NULL)
1716 return got_error_from_errno();
1717 obj_id_str1 = argv[1];
1718 obj_id_str2 = argv[2];
1719 } else
1720 usage_diff();
1722 error = got_repo_open(&repo, repo_path);
1723 free(repo_path);
1724 if (error)
1725 goto done;
1727 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1728 if (error)
1729 goto done;
1731 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1732 if (error)
1733 goto done;
1735 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1736 if (view == NULL) {
1737 error = got_error_from_errno();
1738 goto done;
1740 error = open_diff_view(view, obj1, obj2, repo);
1741 if (error)
1742 goto done;
1743 error = view_loop(view);
1744 done:
1745 got_repo_close(repo);
1746 if (obj1)
1747 got_object_close(obj1);
1748 if (obj2)
1749 got_object_close(obj2);
1750 return error;
1753 __dead static void
1754 usage_blame(void)
1756 endwin();
1757 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1758 getprogname());
1759 exit(1);
1762 struct tog_blame_line {
1763 int annotated;
1764 struct got_object_id *id;
1767 static const struct got_error *
1768 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1769 const char *path, struct tog_blame_line *lines, int nlines,
1770 int blame_complete, int selected_line, int *first_displayed_line,
1771 int *last_displayed_line, int *eof, int max_lines)
1773 const struct got_error *err;
1774 int lineno = 0, nprinted = 0;
1775 char *line;
1776 size_t len;
1777 wchar_t *wline;
1778 int width, wlimit;
1779 struct tog_blame_line *blame_line;
1780 struct got_object_id *prev_id = NULL;
1781 char *id_str;
1783 err = got_object_id_str(&id_str, id);
1784 if (err)
1785 return err;
1787 rewind(f);
1788 werase(view->window);
1790 if (asprintf(&line, "commit: %s", id_str) == -1) {
1791 err = got_error_from_errno();
1792 free(id_str);
1793 return err;
1796 err = format_line(&wline, &width, line, view->ncols);
1797 free(line);
1798 line = NULL;
1799 waddwstr(view->window, wline);
1800 free(wline);
1801 wline = NULL;
1802 if (width < view->ncols)
1803 waddch(view->window, '\n');
1805 if (asprintf(&line, "[%d/%d] %s%s",
1806 *first_displayed_line - 1 + selected_line, nlines,
1807 blame_complete ? "" : "annotating ", path) == -1) {
1808 free(id_str);
1809 return got_error_from_errno();
1811 free(id_str);
1812 err = format_line(&wline, &width, line, view->ncols);
1813 free(line);
1814 line = NULL;
1815 if (err)
1816 return err;
1817 waddwstr(view->window, wline);
1818 free(wline);
1819 wline = NULL;
1820 if (width < view->ncols)
1821 waddch(view->window, '\n');
1823 *eof = 0;
1824 while (nprinted < max_lines - 2) {
1825 line = parse_next_line(f, &len);
1826 if (line == NULL) {
1827 *eof = 1;
1828 break;
1830 if (++lineno < *first_displayed_line) {
1831 free(line);
1832 continue;
1835 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1836 err = format_line(&wline, &width, line, wlimit);
1837 if (err) {
1838 free(line);
1839 return err;
1842 if (nprinted == selected_line - 1)
1843 wstandout(view->window);
1845 blame_line = &lines[lineno - 1];
1846 if (blame_line->annotated && prev_id &&
1847 got_object_id_cmp(prev_id, blame_line->id) == 0)
1848 waddstr(view->window, " ");
1849 else if (blame_line->annotated) {
1850 char *id_str;
1851 err = got_object_id_str(&id_str, blame_line->id);
1852 if (err) {
1853 free(line);
1854 free(wline);
1855 return err;
1857 wprintw(view->window, "%.8s ", id_str);
1858 free(id_str);
1859 prev_id = blame_line->id;
1860 } else {
1861 waddstr(view->window, "........ ");
1862 prev_id = NULL;
1865 waddwstr(view->window, wline);
1866 while (width < wlimit) {
1867 waddch(view->window, ' ');
1868 width++;
1870 if (nprinted == selected_line - 1)
1871 wstandend(view->window);
1872 if (++nprinted == 1)
1873 *first_displayed_line = lineno;
1874 free(line);
1875 free(wline);
1876 wline = NULL;
1878 *last_displayed_line = lineno;
1880 view_vborder(view);
1882 return NULL;
1885 static const struct got_error *
1886 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1888 const struct got_error *err = NULL;
1889 struct tog_blame_cb_args *a = arg;
1890 struct tog_blame_line *line;
1892 if (nlines != a->nlines ||
1893 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1894 return got_error(GOT_ERR_RANGE);
1896 if (pthread_mutex_lock(a->mutex) != 0)
1897 return got_error_from_errno();
1899 if (*a->quit) { /* user has quit the blame view */
1900 err = got_error(GOT_ERR_ITER_COMPLETED);
1901 goto done;
1904 if (lineno == -1)
1905 goto done; /* no change in this commit */
1907 line = &a->lines[lineno - 1];
1908 if (line->annotated)
1909 goto done;
1911 line->id = got_object_id_dup(id);
1912 if (line->id == NULL) {
1913 err = got_error_from_errno();
1914 goto done;
1916 line->annotated = 1;
1918 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1919 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1920 a->last_displayed_line, a->eof, a->view->nlines);
1921 done:
1922 if (pthread_mutex_unlock(a->mutex) != 0)
1923 return got_error_from_errno();
1924 return err;
1927 static void *
1928 blame_thread(void *arg)
1930 const struct got_error *err;
1931 struct tog_blame_thread_args *ta = arg;
1932 struct tog_blame_cb_args *a = ta->cb_args;
1934 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1935 blame_cb, ta->cb_args);
1937 if (pthread_mutex_lock(a->mutex) != 0)
1938 return (void *)got_error_from_errno();
1940 got_repo_close(ta->repo);
1941 ta->repo = NULL;
1942 *ta->complete = 1;
1943 if (!err)
1944 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1945 a->lines, a->nlines, 1, *a->selected_line,
1946 a->first_displayed_line, a->last_displayed_line, a->eof,
1947 a->view->nlines);
1949 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1950 err = got_error_from_errno();
1952 return (void *)err;
1955 static struct got_object_id *
1956 get_selected_commit_id(struct tog_blame_line *lines,
1957 int first_displayed_line, int selected_line)
1959 struct tog_blame_line *line;
1961 line = &lines[first_displayed_line - 1 + selected_line - 1];
1962 if (!line->annotated)
1963 return NULL;
1965 return line->id;
1968 static const struct got_error *
1969 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1970 struct tog_blame_line *lines, int first_displayed_line,
1971 int selected_line, struct got_repository *repo)
1973 const struct got_error *err = NULL;
1974 struct got_commit_object *commit = NULL;
1975 struct got_object_id *selected_id;
1976 struct got_object_qid *pid;
1978 *pobj = NULL;
1979 *obj = NULL;
1981 selected_id = get_selected_commit_id(lines,
1982 first_displayed_line, selected_line);
1983 if (selected_id == NULL)
1984 return NULL;
1986 err = got_object_open(obj, repo, selected_id);
1987 if (err)
1988 goto done;
1990 err = got_object_commit_open(&commit, repo, *obj);
1991 if (err)
1992 goto done;
1994 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1995 if (pid) {
1996 err = got_object_open(pobj, repo, pid->id);
1997 if (err)
1998 goto done;
2000 done:
2001 if (commit)
2002 got_object_commit_close(commit);
2003 return err;
2006 static const struct got_error *
2007 stop_blame(struct tog_blame *blame)
2009 const struct got_error *err = NULL;
2010 int i;
2012 if (blame->thread) {
2013 if (pthread_join(blame->thread, (void **)&err) != 0)
2014 err = got_error_from_errno();
2015 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2016 err = NULL;
2017 blame->thread = NULL;
2019 if (blame->thread_args.repo) {
2020 got_repo_close(blame->thread_args.repo);
2021 blame->thread_args.repo = NULL;
2023 if (blame->f) {
2024 fclose(blame->f);
2025 blame->f = NULL;
2027 for (i = 0; i < blame->nlines; i++)
2028 free(blame->lines[i].id);
2029 free(blame->lines);
2030 blame->lines = NULL;
2031 free(blame->cb_args.commit_id);
2032 blame->cb_args.commit_id = NULL;
2034 return err;
2037 static const struct got_error *
2038 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2039 struct tog_view *view, int *blame_complete,
2040 int *first_displayed_line, int *last_displayed_line,
2041 int *selected_line, int *done, int *eof, const char *path,
2042 struct got_object_id *commit_id,
2043 struct got_repository *repo)
2045 const struct got_error *err = NULL;
2046 struct got_blob_object *blob = NULL;
2047 struct got_repository *thread_repo = NULL;
2048 struct got_object *obj;
2050 err = got_object_open_by_path(&obj, repo, commit_id, path);
2051 if (err)
2052 goto done;
2053 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2054 err = got_error(GOT_ERR_OBJ_TYPE);
2055 goto done;
2058 err = got_object_blob_open(&blob, repo, obj, 8192);
2059 if (err)
2060 goto done;
2061 blame->f = got_opentemp();
2062 if (blame->f == NULL) {
2063 err = got_error_from_errno();
2064 goto done;
2066 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2067 blame->f, blob);
2068 if (err)
2069 goto done;
2071 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2072 if (blame->lines == NULL) {
2073 err = got_error_from_errno();
2074 goto done;
2077 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2078 if (err)
2079 goto done;
2081 blame->cb_args.view = view;
2082 blame->cb_args.lines = blame->lines;
2083 blame->cb_args.nlines = blame->nlines;
2084 blame->cb_args.mutex = mutex;
2085 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2086 if (blame->cb_args.commit_id == NULL) {
2087 err = got_error_from_errno();
2088 goto done;
2090 blame->cb_args.f = blame->f;
2091 blame->cb_args.path = path;
2092 blame->cb_args.first_displayed_line = first_displayed_line;
2093 blame->cb_args.selected_line = selected_line;
2094 blame->cb_args.last_displayed_line = last_displayed_line;
2095 blame->cb_args.quit = done;
2096 blame->cb_args.eof = eof;
2098 blame->thread_args.path = path;
2099 blame->thread_args.repo = thread_repo;
2100 blame->thread_args.cb_args = &blame->cb_args;
2101 blame->thread_args.complete = blame_complete;
2102 *blame_complete = 0;
2104 if (pthread_create(&blame->thread, NULL, blame_thread,
2105 &blame->thread_args) != 0) {
2106 err = got_error_from_errno();
2107 goto done;
2110 done:
2111 if (blob)
2112 got_object_blob_close(blob);
2113 if (obj)
2114 got_object_close(obj);
2115 if (err)
2116 stop_blame(blame);
2117 return err;
2120 static const struct got_error *
2121 open_blame_view(struct tog_view *view, char *path,
2122 struct got_object_id *commit_id, struct got_repository *repo)
2124 const struct got_error *err = NULL;
2125 struct tog_blame_view_state *s = &view->state.blame;
2127 SIMPLEQ_INIT(&s->blamed_commits);
2129 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2130 return got_error_from_errno();
2132 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2133 if (err)
2134 return err;
2136 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2137 s->first_displayed_line = 1;
2138 s->last_displayed_line = view->nlines;
2139 s->selected_line = 1;
2140 s->blame_complete = 0;
2141 s->path = path;
2142 if (s->path == NULL)
2143 return got_error_from_errno();
2144 s->repo = repo;
2145 s->commit_id = commit_id;
2146 memset(&s->blame, 0, sizeof(s->blame));
2148 view->show = show_blame_view;
2149 view->input = input_blame_view;
2150 view->close = close_blame_view;
2152 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2153 &s->first_displayed_line, &s->last_displayed_line,
2154 &s->selected_line, &s->done, &s->eof, s->path,
2155 s->blamed_commit->id, s->repo);
2158 static const struct got_error *
2159 close_blame_view(struct tog_view *view)
2161 const struct got_error *err = NULL;
2162 struct tog_blame_view_state *s = &view->state.blame;
2164 if (s->blame.thread)
2165 err = stop_blame(&s->blame);
2167 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2168 struct got_object_qid *blamed_commit;
2169 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2170 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2171 got_object_qid_free(blamed_commit);
2174 free(s->path);
2176 return err;
2179 static const struct got_error *
2180 show_blame_view(struct tog_view *view)
2182 const struct got_error *err = NULL;
2183 struct tog_blame_view_state *s = &view->state.blame;
2185 if (pthread_mutex_lock(&s->mutex) != 0)
2186 return got_error_from_errno();
2188 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2189 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2190 s->selected_line, &s->first_displayed_line,
2191 &s->last_displayed_line, &s->eof, view->nlines);
2193 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2194 err = got_error_from_errno();
2196 return err;
2199 static const struct got_error *
2200 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2201 struct tog_view *view, int ch)
2203 const struct got_error *err = NULL, *thread_err = NULL;
2204 struct got_object *obj = NULL, *pobj = NULL;
2205 struct tog_view *diff_view;
2206 struct tog_blame_view_state *s = &view->state.blame;
2208 if (pthread_mutex_lock(&s->mutex) != 0) {
2209 err = got_error_from_errno();
2210 goto done;
2213 switch (ch) {
2214 case 'q':
2215 s->done = 1;
2216 if (pthread_mutex_unlock(&s->mutex) != 0) {
2217 err = got_error_from_errno();
2218 goto done;
2220 return stop_blame(&s->blame);
2221 case 'k':
2222 case KEY_UP:
2223 if (s->selected_line > 1)
2224 s->selected_line--;
2225 else if (s->selected_line == 1 &&
2226 s->first_displayed_line > 1)
2227 s->first_displayed_line--;
2228 break;
2229 case KEY_PPAGE:
2230 if (s->first_displayed_line == 1) {
2231 s->selected_line = 1;
2232 break;
2234 if (s->first_displayed_line > view->nlines - 2)
2235 s->first_displayed_line -=
2236 (view->nlines - 2);
2237 else
2238 s->first_displayed_line = 1;
2239 break;
2240 case 'j':
2241 case KEY_DOWN:
2242 if (s->selected_line < view->nlines - 2 &&
2243 s->first_displayed_line +
2244 s->selected_line <= s->blame.nlines)
2245 s->selected_line++;
2246 else if (s->last_displayed_line <
2247 s->blame.nlines)
2248 s->first_displayed_line++;
2249 break;
2250 case 'b':
2251 case 'p': {
2252 struct got_object_id *id;
2253 id = get_selected_commit_id(s->blame.lines,
2254 s->first_displayed_line, s->selected_line);
2255 if (id == NULL || got_object_id_cmp(id,
2256 s->blamed_commit->id) == 0)
2257 break;
2258 err = open_selected_commit(&pobj, &obj,
2259 s->blame.lines, s->first_displayed_line,
2260 s->selected_line, s->repo);
2261 if (err)
2262 break;
2263 if (pobj == NULL && obj == NULL)
2264 break;
2265 if (ch == 'p' && pobj == NULL)
2266 break;
2267 s->done = 1;
2268 if (pthread_mutex_unlock(&s->mutex) != 0) {
2269 err = got_error_from_errno();
2270 goto done;
2272 thread_err = stop_blame(&s->blame);
2273 s->done = 0;
2274 if (pthread_mutex_lock(&s->mutex) != 0) {
2275 err = got_error_from_errno();
2276 goto done;
2278 if (thread_err)
2279 break;
2280 id = got_object_get_id(ch == 'b' ? obj : pobj);
2281 got_object_close(obj);
2282 obj = NULL;
2283 if (pobj) {
2284 got_object_close(pobj);
2285 pobj = NULL;
2287 if (id == NULL) {
2288 err = got_error_from_errno();
2289 break;
2291 err = got_object_qid_alloc(
2292 &s->blamed_commit, id);
2293 free(id);
2294 if (err)
2295 goto done;
2296 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2297 s->blamed_commit, entry);
2298 err = run_blame(&s->blame, &s->mutex, view,
2299 &s->blame_complete,
2300 &s->first_displayed_line,
2301 &s->last_displayed_line,
2302 &s->selected_line, &s->done, &s->eof,
2303 s->path, s->blamed_commit->id, s->repo);
2304 if (err)
2305 break;
2306 break;
2308 case 'B': {
2309 struct got_object_qid *first;
2310 first = SIMPLEQ_FIRST(&s->blamed_commits);
2311 if (!got_object_id_cmp(first->id, s->commit_id))
2312 break;
2313 s->done = 1;
2314 if (pthread_mutex_unlock(&s->mutex) != 0) {
2315 err = got_error_from_errno();
2316 goto done;
2318 thread_err = stop_blame(&s->blame);
2319 s->done = 0;
2320 if (pthread_mutex_lock(&s->mutex) != 0) {
2321 err = got_error_from_errno();
2322 goto done;
2324 if (thread_err)
2325 break;
2326 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2327 got_object_qid_free(s->blamed_commit);
2328 s->blamed_commit =
2329 SIMPLEQ_FIRST(&s->blamed_commits);
2330 err = run_blame(&s->blame, &s->mutex, view,
2331 &s->blame_complete,
2332 &s->first_displayed_line,
2333 &s->last_displayed_line,
2334 &s->selected_line, &s->done, &s->eof, s->path,
2335 s->blamed_commit->id, s->repo);
2336 if (err)
2337 break;
2338 break;
2340 case KEY_ENTER:
2341 case '\r':
2342 err = open_selected_commit(&pobj, &obj,
2343 s->blame.lines, s->first_displayed_line,
2344 s->selected_line, s->repo);
2345 if (err)
2346 break;
2347 if (pobj == NULL && obj == NULL)
2348 break;
2349 diff_view = view_open(0, 0, 0, 0, view,
2350 TOG_VIEW_DIFF);
2351 if (diff_view == NULL) {
2352 err = got_error_from_errno();
2353 break;
2355 err = open_diff_view(diff_view, pobj, obj,
2356 s->repo);
2357 if (err) {
2358 view_close(diff_view);
2359 break;
2361 *new_view = diff_view;
2362 if (pobj) {
2363 got_object_close(pobj);
2364 pobj = NULL;
2366 got_object_close(obj);
2367 obj = NULL;
2368 if (err)
2369 break;
2370 break;
2371 case KEY_NPAGE:
2372 case ' ':
2373 if (s->last_displayed_line >= s->blame.nlines &&
2374 s->selected_line < view->nlines - 2) {
2375 s->selected_line = MIN(s->blame.nlines,
2376 view->nlines - 2);
2377 break;
2379 if (s->last_displayed_line + view->nlines - 2
2380 <= s->blame.nlines)
2381 s->first_displayed_line +=
2382 view->nlines - 2;
2383 else
2384 s->first_displayed_line =
2385 s->blame.nlines -
2386 (view->nlines - 3);
2387 break;
2388 case KEY_RESIZE:
2389 if (s->selected_line > view->nlines - 2) {
2390 s->selected_line = MIN(s->blame.nlines,
2391 view->nlines - 2);
2393 break;
2394 default:
2395 break;
2398 if (pthread_mutex_unlock(&s->mutex) != 0)
2399 err = got_error_from_errno();
2400 done:
2401 if (pobj)
2402 got_object_close(pobj);
2403 return thread_err ? thread_err : err;
2406 static const struct got_error *
2407 cmd_blame(int argc, char *argv[])
2409 const struct got_error *error;
2410 struct got_repository *repo = NULL;
2411 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2412 struct got_object_id *commit_id = NULL;
2413 char *commit_id_str = NULL;
2414 int ch;
2415 struct tog_view *view;
2417 #ifndef PROFILE
2418 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2419 err(1, "pledge");
2420 #endif
2422 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2423 switch (ch) {
2424 case 'c':
2425 commit_id_str = optarg;
2426 break;
2427 case 'r':
2428 repo_path = realpath(optarg, NULL);
2429 if (repo_path == NULL)
2430 err(1, "-r option");
2431 break;
2432 default:
2433 usage();
2434 /* NOTREACHED */
2438 argc -= optind;
2439 argv += optind;
2441 if (argc == 1)
2442 path = argv[0];
2443 else
2444 usage_blame();
2446 cwd = getcwd(NULL, 0);
2447 if (cwd == NULL) {
2448 error = got_error_from_errno();
2449 goto done;
2451 if (repo_path == NULL) {
2452 repo_path = strdup(cwd);
2453 if (repo_path == NULL) {
2454 error = got_error_from_errno();
2455 goto done;
2460 error = got_repo_open(&repo, repo_path);
2461 if (error != NULL)
2462 return error;
2464 error = got_repo_map_path(&in_repo_path, repo, path);
2465 if (error != NULL)
2466 goto done;
2468 if (commit_id_str == NULL) {
2469 struct got_reference *head_ref;
2470 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2471 if (error != NULL)
2472 goto done;
2473 error = got_ref_resolve(&commit_id, repo, head_ref);
2474 got_ref_close(head_ref);
2475 } else {
2476 struct got_object *obj;
2477 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2478 if (error != NULL)
2479 goto done;
2480 commit_id = got_object_get_id(obj);
2481 if (commit_id == NULL)
2482 error = got_error_from_errno();
2483 got_object_close(obj);
2485 if (error != NULL)
2486 goto done;
2488 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2489 if (view == NULL) {
2490 error = got_error_from_errno();
2491 goto done;
2493 error = open_blame_view(view, in_repo_path, commit_id, repo);
2494 if (error)
2495 goto done;
2496 error = view_loop(view);
2497 done:
2498 free(repo_path);
2499 free(cwd);
2500 free(commit_id);
2501 if (repo)
2502 got_repo_close(repo);
2503 return error;
2506 static const struct got_error *
2507 draw_tree_entries(struct tog_view *view,
2508 struct got_tree_entry **first_displayed_entry,
2509 struct got_tree_entry **last_displayed_entry,
2510 struct got_tree_entry **selected_entry, int *ndisplayed,
2511 const char *label, int show_ids, const char *parent_path,
2512 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2514 const struct got_error *err = NULL;
2515 struct got_tree_entry *te;
2516 wchar_t *wline;
2517 int width, n;
2519 *ndisplayed = 0;
2521 werase(view->window);
2523 if (limit == 0)
2524 return NULL;
2526 err = format_line(&wline, &width, label, view->ncols);
2527 if (err)
2528 return err;
2529 waddwstr(view->window, wline);
2530 free(wline);
2531 wline = NULL;
2532 if (width < view->ncols)
2533 waddch(view->window, '\n');
2534 if (--limit <= 0)
2535 return NULL;
2536 err = format_line(&wline, &width, parent_path, view->ncols);
2537 if (err)
2538 return err;
2539 waddwstr(view->window, wline);
2540 free(wline);
2541 wline = NULL;
2542 if (width < view->ncols)
2543 waddch(view->window, '\n');
2544 if (--limit <= 0)
2545 return NULL;
2546 waddch(view->window, '\n');
2547 if (--limit <= 0)
2548 return NULL;
2550 te = SIMPLEQ_FIRST(&entries->head);
2551 if (*first_displayed_entry == NULL) {
2552 if (selected == 0) {
2553 wstandout(view->window);
2554 *selected_entry = NULL;
2556 waddstr(view->window, " ..\n"); /* parent directory */
2557 if (selected == 0)
2558 wstandend(view->window);
2559 (*ndisplayed)++;
2560 if (--limit <= 0)
2561 return NULL;
2562 n = 1;
2563 } else {
2564 n = 0;
2565 while (te != *first_displayed_entry)
2566 te = SIMPLEQ_NEXT(te, entry);
2569 while (te) {
2570 char *line = NULL, *id_str = NULL;
2572 if (show_ids) {
2573 err = got_object_id_str(&id_str, te->id);
2574 if (err)
2575 return got_error_from_errno();
2577 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2578 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2579 free(id_str);
2580 return got_error_from_errno();
2582 free(id_str);
2583 err = format_line(&wline, &width, line, view->ncols);
2584 if (err) {
2585 free(line);
2586 break;
2588 if (n == selected) {
2589 wstandout(view->window);
2590 *selected_entry = te;
2592 waddwstr(view->window, wline);
2593 if (width < view->ncols)
2594 waddch(view->window, '\n');
2595 if (n == selected)
2596 wstandend(view->window);
2597 free(line);
2598 free(wline);
2599 wline = NULL;
2600 n++;
2601 (*ndisplayed)++;
2602 *last_displayed_entry = te;
2603 if (--limit <= 0)
2604 break;
2605 te = SIMPLEQ_NEXT(te, entry);
2608 view_vborder(view);
2609 return err;
2612 static void
2613 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2614 const struct got_tree_entries *entries, int isroot)
2616 struct got_tree_entry *te, *prev;
2617 int i;
2619 if (*first_displayed_entry == NULL)
2620 return;
2622 te = SIMPLEQ_FIRST(&entries->head);
2623 if (*first_displayed_entry == te) {
2624 if (!isroot)
2625 *first_displayed_entry = NULL;
2626 return;
2629 /* XXX this is stupid... switch to TAILQ? */
2630 for (i = 0; i < maxscroll; i++) {
2631 while (te != *first_displayed_entry) {
2632 prev = te;
2633 te = SIMPLEQ_NEXT(te, entry);
2635 *first_displayed_entry = prev;
2636 te = SIMPLEQ_FIRST(&entries->head);
2638 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2639 *first_displayed_entry = NULL;
2642 static void
2643 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2644 struct got_tree_entry *last_displayed_entry,
2645 const struct got_tree_entries *entries)
2647 struct got_tree_entry *next;
2648 int n = 0;
2650 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2651 return;
2653 if (*first_displayed_entry)
2654 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2655 else
2656 next = SIMPLEQ_FIRST(&entries->head);
2657 while (next) {
2658 *first_displayed_entry = next;
2659 if (++n >= maxscroll)
2660 break;
2661 next = SIMPLEQ_NEXT(next, entry);
2665 static const struct got_error *
2666 tree_entry_path(char **path, struct tog_parent_trees *parents,
2667 struct got_tree_entry *te)
2669 const struct got_error *err = NULL;
2670 struct tog_parent_tree *pt;
2671 size_t len = 2; /* for leading slash and NUL */
2673 TAILQ_FOREACH(pt, parents, entry)
2674 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2675 if (te)
2676 len += strlen(te->name);
2678 *path = calloc(1, len);
2679 if (path == NULL)
2680 return got_error_from_errno();
2682 (*path)[0] = '/';
2683 pt = TAILQ_LAST(parents, tog_parent_trees);
2684 while (pt) {
2685 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2686 err = got_error(GOT_ERR_NO_SPACE);
2687 goto done;
2689 if (strlcat(*path, "/", len) >= len) {
2690 err = got_error(GOT_ERR_NO_SPACE);
2691 goto done;
2693 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2695 if (te) {
2696 if (strlcat(*path, te->name, len) >= len) {
2697 err = got_error(GOT_ERR_NO_SPACE);
2698 goto done;
2701 done:
2702 if (err) {
2703 free(*path);
2704 *path = NULL;
2706 return err;
2709 static const struct got_error *
2710 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2711 struct got_tree_entry *te, struct tog_parent_trees *parents,
2712 struct got_object_id *commit_id, struct got_repository *repo)
2714 const struct got_error *err = NULL;
2715 char *path;
2716 struct tog_view *blame_view;
2718 err = tree_entry_path(&path, parents, te);
2719 if (err)
2720 return err;
2722 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2723 if (blame_view == NULL)
2724 return got_error_from_errno();
2726 err = open_blame_view(blame_view, path, commit_id, repo);
2727 if (err) {
2728 view_close(blame_view);
2729 free(path);
2730 } else
2731 *new_view = blame_view;
2732 return err;
2735 static const struct got_error *
2736 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2737 struct got_tree_entry *te, struct tog_parent_trees *parents,
2738 struct got_object_id *commit_id, struct got_repository *repo)
2740 struct tog_view *log_view;
2741 const struct got_error *err = NULL;
2742 char *path;
2744 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2745 if (log_view == NULL)
2746 return got_error_from_errno();
2748 err = tree_entry_path(&path, parents, te);
2749 if (err)
2750 return err;
2752 err = open_log_view(log_view, commit_id, repo, path);
2753 if (err)
2754 view_close(log_view);
2755 else
2756 *new_view = log_view;
2757 free(path);
2758 return err;
2761 static const struct got_error *
2762 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2763 struct got_object_id *commit_id, struct got_repository *repo)
2765 const struct got_error *err = NULL;
2766 char *commit_id_str = NULL;
2767 struct tog_tree_view_state *s = &view->state.tree;
2769 TAILQ_INIT(&s->parents);
2771 err = got_object_id_str(&commit_id_str, commit_id);
2772 if (err != NULL)
2773 goto done;
2775 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2776 err = got_error_from_errno();
2777 goto done;
2780 s->root = s->tree = root;
2781 s->entries = got_object_tree_get_entries(root);
2782 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2783 s->commit_id = commit_id;
2784 s->repo = repo;
2786 view->show = show_tree_view;
2787 view->input = input_tree_view;
2788 view->close = close_tree_view;
2789 done:
2790 free(commit_id_str);
2791 if (err)
2792 free(s->tree_label);
2793 return err;
2796 static const struct got_error *
2797 close_tree_view(struct tog_view *view)
2799 struct tog_tree_view_state *s = &view->state.tree;
2801 free(s->tree_label);
2802 while (!TAILQ_EMPTY(&s->parents)) {
2803 struct tog_parent_tree *parent;
2804 parent = TAILQ_FIRST(&s->parents);
2805 TAILQ_REMOVE(&s->parents, parent, entry);
2806 free(parent);
2809 if (s->tree != s->root)
2810 got_object_tree_close(s->tree);
2811 got_object_tree_close(s->root);
2813 return NULL;
2816 static const struct got_error *
2817 show_tree_view(struct tog_view *view)
2819 const struct got_error *err = NULL;
2820 struct tog_tree_view_state *s = &view->state.tree;
2821 char *parent_path;
2823 err = tree_entry_path(&parent_path, &s->parents, NULL);
2824 if (err)
2825 return err;
2827 err = draw_tree_entries(view, &s->first_displayed_entry,
2828 &s->last_displayed_entry, &s->selected_entry,
2829 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2830 s->entries, s->selected, view->nlines, s->tree == s->root);
2831 free(parent_path);
2832 return err;
2835 static const struct got_error *
2836 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2837 struct tog_view *view, int ch)
2839 const struct got_error *err = NULL;
2840 struct tog_tree_view_state *s = &view->state.tree;
2842 switch (ch) {
2843 case 'i':
2844 s->show_ids = !s->show_ids;
2845 break;
2846 case 'l':
2847 if (s->selected_entry) {
2848 err = log_tree_entry(new_view, view,
2849 s->selected_entry, &s->parents,
2850 s->commit_id, s->repo);
2852 break;
2853 case 'k':
2854 case KEY_UP:
2855 if (s->selected > 0)
2856 s->selected--;
2857 if (s->selected > 0)
2858 break;
2859 tree_scroll_up(&s->first_displayed_entry, 1,
2860 s->entries, s->tree == s->root);
2861 break;
2862 case KEY_PPAGE:
2863 if (SIMPLEQ_FIRST(&s->entries->head) ==
2864 s->first_displayed_entry) {
2865 if (s->tree != s->root)
2866 s->first_displayed_entry = NULL;
2867 s->selected = 0;
2868 break;
2870 tree_scroll_up(&s->first_displayed_entry,
2871 view->nlines, s->entries,
2872 s->tree == s->root);
2873 break;
2874 case 'j':
2875 case KEY_DOWN:
2876 if (s->selected < s->ndisplayed - 1) {
2877 s->selected++;
2878 break;
2880 tree_scroll_down(&s->first_displayed_entry, 1,
2881 s->last_displayed_entry, s->entries);
2882 break;
2883 case KEY_NPAGE:
2884 tree_scroll_down(&s->first_displayed_entry,
2885 view->nlines, s->last_displayed_entry,
2886 s->entries);
2887 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2888 entry))
2889 break;
2890 /* can't scroll any further; move cursor down */
2891 if (s->selected < s->ndisplayed - 1)
2892 s->selected = s->ndisplayed - 1;
2893 break;
2894 case KEY_ENTER:
2895 case '\r':
2896 if (s->selected_entry == NULL) {
2897 struct tog_parent_tree *parent;
2898 case KEY_LEFT:
2899 /* user selected '..' */
2900 if (s->tree == s->root)
2901 break;
2902 parent = TAILQ_FIRST(&s->parents);
2903 TAILQ_REMOVE(&s->parents, parent,
2904 entry);
2905 got_object_tree_close(s->tree);
2906 s->tree = parent->tree;
2907 s->entries =
2908 got_object_tree_get_entries(s->tree);
2909 s->first_displayed_entry =
2910 parent->first_displayed_entry;
2911 s->selected_entry =
2912 parent->selected_entry;
2913 s->selected = parent->selected;
2914 free(parent);
2915 } else if (S_ISDIR(s->selected_entry->mode)) {
2916 struct tog_parent_tree *parent;
2917 struct got_tree_object *child;
2918 err = got_object_open_as_tree(&child,
2919 s->repo, s->selected_entry->id);
2920 if (err)
2921 break;
2922 parent = calloc(1, sizeof(*parent));
2923 if (parent == NULL) {
2924 err = got_error_from_errno();
2925 break;
2927 parent->tree = s->tree;
2928 parent->first_displayed_entry =
2929 s->first_displayed_entry;
2930 parent->selected_entry = s->selected_entry;
2931 parent->selected = s->selected;
2932 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2933 s->tree = child;
2934 s->entries =
2935 got_object_tree_get_entries(s->tree);
2936 s->selected = 0;
2937 s->first_displayed_entry = NULL;
2938 } else if (S_ISREG(s->selected_entry->mode)) {
2939 err = blame_tree_entry(new_view, view,
2940 s->selected_entry, &s->parents,
2941 s->commit_id, s->repo);
2942 if (err)
2943 break;
2945 break;
2946 case KEY_RESIZE:
2947 if (s->selected > view->nlines)
2948 s->selected = s->ndisplayed - 1;
2949 break;
2950 default:
2951 break;
2954 return err;
2957 __dead static void
2958 usage_tree(void)
2960 endwin();
2961 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2962 getprogname());
2963 exit(1);
2966 static const struct got_error *
2967 cmd_tree(int argc, char *argv[])
2969 const struct got_error *error;
2970 struct got_repository *repo = NULL;
2971 char *repo_path = NULL;
2972 struct got_object_id *commit_id = NULL;
2973 char *commit_id_arg = NULL;
2974 struct got_commit_object *commit = NULL;
2975 struct got_tree_object *tree = NULL;
2976 int ch;
2977 struct tog_view *view;
2979 #ifndef PROFILE
2980 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2981 err(1, "pledge");
2982 #endif
2984 while ((ch = getopt(argc, argv, "c:")) != -1) {
2985 switch (ch) {
2986 case 'c':
2987 commit_id_arg = optarg;
2988 break;
2989 default:
2990 usage();
2991 /* NOTREACHED */
2995 argc -= optind;
2996 argv += optind;
2998 if (argc == 0) {
2999 repo_path = getcwd(NULL, 0);
3000 if (repo_path == NULL)
3001 return got_error_from_errno();
3002 } else if (argc == 1) {
3003 repo_path = realpath(argv[0], NULL);
3004 if (repo_path == NULL)
3005 return got_error_from_errno();
3006 } else
3007 usage_log();
3009 error = got_repo_open(&repo, repo_path);
3010 free(repo_path);
3011 if (error != NULL)
3012 return error;
3014 if (commit_id_arg == NULL) {
3015 error = get_head_commit_id(&commit_id, repo);
3016 if (error != NULL)
3017 goto done;
3018 } else {
3019 struct got_object *obj;
3020 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3021 if (error == NULL) {
3022 commit_id = got_object_get_id(obj);
3023 if (commit_id == NULL)
3024 error = got_error_from_errno();
3027 if (error != NULL)
3028 goto done;
3030 error = got_object_open_as_commit(&commit, repo, commit_id);
3031 if (error != NULL)
3032 goto done;
3034 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3035 if (error != NULL)
3036 goto done;
3038 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3039 if (view == NULL) {
3040 error = got_error_from_errno();
3041 goto done;
3043 error = open_tree_view(view, tree, commit_id, repo);
3044 if (error)
3045 goto done;
3046 error = view_loop(view);
3047 done:
3048 free(commit_id);
3049 if (commit)
3050 got_object_commit_close(commit);
3051 if (tree)
3052 got_object_tree_close(tree);
3053 if (repo)
3054 got_repo_close(repo);
3055 return error;
3057 static void
3058 init_curses(void)
3060 initscr();
3061 cbreak();
3062 noecho();
3063 nonl();
3064 intrflush(stdscr, FALSE);
3065 keypad(stdscr, TRUE);
3066 curs_set(0);
3069 __dead static void
3070 usage(void)
3072 int i;
3074 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3075 "Available commands:\n", getprogname());
3076 for (i = 0; i < nitems(tog_commands); i++) {
3077 struct tog_cmd *cmd = &tog_commands[i];
3078 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3080 exit(1);
3083 static char **
3084 make_argv(const char *arg0, const char *arg1)
3086 char **argv;
3087 int argc = (arg1 == NULL ? 1 : 2);
3089 argv = calloc(argc, sizeof(char *));
3090 if (argv == NULL)
3091 err(1, "calloc");
3092 argv[0] = strdup(arg0);
3093 if (argv[0] == NULL)
3094 err(1, "calloc");
3095 if (arg1) {
3096 argv[1] = strdup(arg1);
3097 if (argv[1] == NULL)
3098 err(1, "calloc");
3101 return argv;
3104 int
3105 main(int argc, char *argv[])
3107 const struct got_error *error = NULL;
3108 struct tog_cmd *cmd = NULL;
3109 int ch, hflag = 0;
3110 char **cmd_argv = NULL;
3112 setlocale(LC_ALL, "");
3114 while ((ch = getopt(argc, argv, "h")) != -1) {
3115 switch (ch) {
3116 case 'h':
3117 hflag = 1;
3118 break;
3119 default:
3120 usage();
3121 /* NOTREACHED */
3125 argc -= optind;
3126 argv += optind;
3127 optind = 0;
3128 optreset = 1;
3130 if (argc == 0) {
3131 if (hflag)
3132 usage();
3133 /* Build an argument vector which runs a default command. */
3134 cmd = &tog_commands[0];
3135 cmd_argv = make_argv(cmd->name, NULL);
3136 argc = 1;
3137 } else {
3138 int i;
3140 /* Did the user specific a command? */
3141 for (i = 0; i < nitems(tog_commands); i++) {
3142 if (strncmp(tog_commands[i].name, argv[0],
3143 strlen(argv[0])) == 0) {
3144 cmd = &tog_commands[i];
3145 if (hflag)
3146 tog_commands[i].cmd_usage();
3147 break;
3150 if (cmd == NULL) {
3151 /* Did the user specify a repository? */
3152 char *repo_path = realpath(argv[0], NULL);
3153 if (repo_path) {
3154 struct got_repository *repo;
3155 error = got_repo_open(&repo, repo_path);
3156 if (error == NULL)
3157 got_repo_close(repo);
3158 } else
3159 error = got_error_from_errno();
3160 if (error) {
3161 if (hflag) {
3162 fprintf(stderr, "%s: '%s' is not a "
3163 "known command\n", getprogname(),
3164 argv[0]);
3165 usage();
3167 fprintf(stderr, "%s: '%s' is neither a known "
3168 "command nor a path to a repository\n",
3169 getprogname(), argv[0]);
3170 free(repo_path);
3171 return 1;
3173 cmd = &tog_commands[0];
3174 cmd_argv = make_argv(cmd->name, repo_path);
3175 argc = 2;
3176 free(repo_path);
3180 init_curses();
3182 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3183 if (error)
3184 goto done;
3185 done:
3186 endwin();
3187 free(cmd_argv);
3188 if (error)
3189 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3190 return 0;