Blob


1 /*
2 * Copyright (c) 2018, 2019 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>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_worktree.h"
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 #ifndef MAX
57 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
58 #endif
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 struct tog_cmd {
66 const char *name;
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
69 const char *descr;
70 };
72 __dead static void usage(void);
73 __dead static void usage_log(void);
74 __dead static void usage_diff(void);
75 __dead static void usage_blame(void);
76 __dead static void usage_tree(void);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
83 static struct tog_cmd tog_commands[] = {
84 { "log", cmd_log, usage_log,
85 "show repository history" },
86 { "diff", cmd_diff, usage_diff,
87 "compare files and directories" },
88 { "blame", cmd_blame, usage_blame,
89 "show line-by-line file history" },
90 { "tree", cmd_tree, usage_tree,
91 "browse trees in repository" },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 int idx;
106 };
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
109 int ncommits;
110 struct commit_queue_head head;
111 };
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
115 FILE *f;
116 int first_displayed_line;
117 int last_displayed_line;
118 int eof;
119 int diff_context;
120 struct got_repository *repo;
122 /* passed from log view; may be NULL */
123 struct tog_view *log_view;
124 };
126 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
128 struct tog_log_thread_args {
129 pthread_cond_t need_commits;
130 int commits_needed;
131 struct got_commit_graph *graph;
132 struct commit_queue *commits;
133 const char *in_repo_path;
134 struct got_object_id *start_id;
135 struct got_repository *repo;
136 int log_complete;
137 sig_atomic_t *quit;
138 struct tog_view *view;
139 struct commit_queue_entry **first_displayed_entry;
140 struct commit_queue_entry **selected_entry;
141 };
143 struct tog_log_view_state {
144 struct commit_queue commits;
145 struct commit_queue_entry *first_displayed_entry;
146 struct commit_queue_entry *last_displayed_entry;
147 struct commit_queue_entry *selected_entry;
148 int selected;
149 char *in_repo_path;
150 struct got_repository *repo;
151 struct got_object_id *start_id;
152 sig_atomic_t quit;
153 pthread_t thread;
154 struct tog_log_thread_args thread_args;
155 };
157 struct tog_blame_cb_args {
158 struct tog_blame_line *lines; /* one per line */
159 int nlines;
161 struct tog_view *view;
162 struct got_object_id *commit_id;
163 int *quit;
164 };
166 struct tog_blame_thread_args {
167 const char *path;
168 struct got_repository *repo;
169 struct tog_blame_cb_args *cb_args;
170 int *complete;
171 };
173 struct tog_blame {
174 FILE *f;
175 size_t filesize;
176 struct tog_blame_line *lines;
177 int nlines;
178 pthread_t thread;
179 struct tog_blame_thread_args thread_args;
180 struct tog_blame_cb_args cb_args;
181 const char *path;
182 };
184 struct tog_blame_view_state {
185 int first_displayed_line;
186 int last_displayed_line;
187 int selected_line;
188 int blame_complete;
189 int eof;
190 int done;
191 struct got_object_id_queue blamed_commits;
192 struct got_object_qid *blamed_commit;
193 char *path;
194 struct got_repository *repo;
195 struct got_object_id *commit_id;
196 struct tog_blame blame;
197 };
199 struct tog_parent_tree {
200 TAILQ_ENTRY(tog_parent_tree) entry;
201 struct got_tree_object *tree;
202 struct got_tree_entry *first_displayed_entry;
203 struct got_tree_entry *selected_entry;
204 int selected;
205 };
207 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
209 struct tog_tree_view_state {
210 char *tree_label;
211 struct got_tree_object *root;
212 struct got_tree_object *tree;
213 const struct got_tree_entries *entries;
214 struct got_tree_entry *first_displayed_entry;
215 struct got_tree_entry *last_displayed_entry;
216 struct got_tree_entry *selected_entry;
217 int ndisplayed, selected, show_ids;
218 struct tog_parent_trees parents;
219 struct got_object_id *commit_id;
220 struct got_repository *repo;
221 };
223 /*
224 * We implement two types of views: parent views and child views.
226 * The 'Tab' key switches between a parent view and its child view.
227 * Child views are shown side-by-side to their parent view, provided
228 * there is enough screen estate.
230 * When a new view is opened from within a parent view, this new view
231 * becomes a child view of the parent view, replacing any existing child.
233 * When a new view is opened from within a child view, this new view
234 * becomes a parent view which will obscure the views below until the
235 * user quits the new parent view by typing 'q'.
237 * This list of views contains parent views only.
238 * Child views are only pointed to by their parent view.
239 */
240 TAILQ_HEAD(tog_view_list_head, tog_view);
242 struct tog_view {
243 TAILQ_ENTRY(tog_view) entry;
244 WINDOW *window;
245 PANEL *panel;
246 int nlines, ncols, begin_y, begin_x;
247 int lines, cols; /* copies of LINES and COLS */
248 int focussed;
249 struct tog_view *parent;
250 struct tog_view *child;
251 int child_focussed;
253 /* type-specific state */
254 enum tog_view_type type;
255 union {
256 struct tog_diff_view_state diff;
257 struct tog_log_view_state log;
258 struct tog_blame_view_state blame;
259 struct tog_tree_view_state tree;
260 } state;
262 const struct got_error *(*show)(struct tog_view *);
263 const struct got_error *(*input)(struct tog_view **,
264 struct tog_view **, struct tog_view**, struct tog_view *, int);
265 const struct got_error *(*close)(struct tog_view *);
266 };
268 static const struct got_error *open_diff_view(struct tog_view *,
269 struct got_object_id *, struct got_object_id *, struct tog_view *,
270 struct got_repository *);
271 static const struct got_error *show_diff_view(struct tog_view *);
272 static const struct got_error *input_diff_view(struct tog_view **,
273 struct tog_view **, struct tog_view **, struct tog_view *, int);
274 static const struct got_error* close_diff_view(struct tog_view *);
276 static const struct got_error *open_log_view(struct tog_view *,
277 struct got_object_id *, struct got_repository *, const char *, int);
278 static const struct got_error * show_log_view(struct tog_view *);
279 static const struct got_error *input_log_view(struct tog_view **,
280 struct tog_view **, struct tog_view **, struct tog_view *, int);
281 static const struct got_error *close_log_view(struct tog_view *);
283 static const struct got_error *open_blame_view(struct tog_view *, char *,
284 struct got_object_id *, struct got_repository *);
285 static const struct got_error *show_blame_view(struct tog_view *);
286 static const struct got_error *input_blame_view(struct tog_view **,
287 struct tog_view **, struct tog_view **, struct tog_view *, int);
288 static const struct got_error *close_blame_view(struct tog_view *);
290 static const struct got_error *open_tree_view(struct tog_view *,
291 struct got_tree_object *, struct got_object_id *, struct got_repository *);
292 static const struct got_error *show_tree_view(struct tog_view *);
293 static const struct got_error *input_tree_view(struct tog_view **,
294 struct tog_view **, struct tog_view **, struct tog_view *, int);
295 static const struct got_error *close_tree_view(struct tog_view *);
297 static volatile sig_atomic_t tog_sigwinch_received;
299 static void
300 tog_sigwinch(int signo)
302 tog_sigwinch_received = 1;
305 static const struct got_error *
306 view_close(struct tog_view *view)
308 const struct got_error *err = NULL;
310 if (view->child) {
311 view_close(view->child);
312 view->child = NULL;
314 if (view->close)
315 err = view->close(view);
316 if (view->panel)
317 del_panel(view->panel);
318 if (view->window)
319 delwin(view->window);
320 free(view);
321 return err;
324 static struct tog_view *
325 view_open(int nlines, int ncols, int begin_y, int begin_x,
326 enum tog_view_type type)
328 struct tog_view *view = calloc(1, sizeof(*view));
330 if (view == NULL)
331 return NULL;
333 view->type = type;
334 view->lines = LINES;
335 view->cols = COLS;
336 view->nlines = nlines ? nlines : LINES - begin_y;
337 view->ncols = ncols ? ncols : COLS - begin_x;
338 view->begin_y = begin_y;
339 view->begin_x = begin_x;
340 view->window = newwin(nlines, ncols, begin_y, begin_x);
341 if (view->window == NULL) {
342 view_close(view);
343 return NULL;
345 view->panel = new_panel(view->window);
346 if (view->panel == NULL ||
347 set_panel_userptr(view->panel, view) != OK) {
348 view_close(view);
349 return NULL;
352 keypad(view->window, TRUE);
353 return view;
356 static int
357 view_split_begin_x(int begin_x)
359 if (begin_x > 0 || COLS < 120)
360 return 0;
361 return (COLS - MAX(COLS / 2, 80));
364 static const struct got_error *view_resize(struct tog_view *);
366 static const struct got_error *
367 view_splitscreen(struct tog_view *view)
369 const struct got_error *err = NULL;
371 view->begin_y = 0;
372 view->begin_x = view_split_begin_x(0);
373 view->nlines = LINES;
374 view->ncols = COLS - view->begin_x;
375 view->lines = LINES;
376 view->cols = COLS;
377 err = view_resize(view);
378 if (err)
379 return err;
381 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
382 return got_error_from_errno();
384 return NULL;
387 static const struct got_error *
388 view_fullscreen(struct tog_view *view)
390 const struct got_error *err = NULL;
392 view->begin_x = 0;
393 view->begin_y = 0;
394 view->nlines = LINES;
395 view->ncols = COLS;
396 view->lines = LINES;
397 view->cols = COLS;
398 err = view_resize(view);
399 if (err)
400 return err;
402 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
403 return got_error_from_errno();
405 return NULL;
408 static int
409 view_is_parent_view(struct tog_view *view)
411 return view->parent == NULL;
414 static const struct got_error *
415 view_resize(struct tog_view *view)
417 int nlines, ncols;
419 if (view->lines > LINES)
420 nlines = view->nlines - (view->lines - LINES);
421 else
422 nlines = view->nlines + (LINES - view->lines);
424 if (view->cols > COLS)
425 ncols = view->ncols - (view->cols - COLS);
426 else
427 ncols = view->ncols + (COLS - view->cols);
429 if (wresize(view->window, nlines, ncols) == ERR)
430 return got_error_from_errno();
431 if (replace_panel(view->panel, view->window) == ERR)
432 return got_error_from_errno();
433 wclear(view->window);
435 view->nlines = nlines;
436 view->ncols = ncols;
437 view->lines = LINES;
438 view->cols = COLS;
440 if (view->child) {
441 view->child->begin_x = view_split_begin_x(view->begin_x);
442 if (view->child->begin_x == 0) {
443 view_fullscreen(view->child);
444 if (view->child->focussed)
445 show_panel(view->child->panel);
446 else
447 show_panel(view->panel);
448 } else {
449 view_splitscreen(view->child);
450 show_panel(view->child->panel);
454 return NULL;
457 static const struct got_error *
458 view_close_child(struct tog_view *view)
460 const struct got_error *err = NULL;
462 if (view->child == NULL)
463 return NULL;
465 err = view_close(view->child);
466 view->child = NULL;
467 return err;
470 static const struct got_error *
471 view_set_child(struct tog_view *view, struct tog_view *child)
473 const struct got_error *err = NULL;
475 view->child = child;
476 child->parent = view;
477 return err;
480 static int
481 view_is_splitscreen(struct tog_view *view)
483 return !view_is_parent_view(view) && view->begin_x > 0;
486 static void
487 tog_resizeterm(void)
489 int cols, lines;
490 struct winsize size;
492 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
493 cols = 80; /* Default */
494 lines = 24;
495 } else {
496 cols = size.ws_col;
497 lines = size.ws_row;
499 resize_term(lines, cols);
502 static const struct got_error *
503 view_input(struct tog_view **new, struct tog_view **dead,
504 struct tog_view **focus, int *done, struct tog_view *view,
505 struct tog_view_list_head *views)
507 const struct got_error *err = NULL;
508 struct tog_view *v;
509 int ch, errcode;
511 *new = NULL;
512 *dead = NULL;
513 *focus = NULL;
515 nodelay(stdscr, FALSE);
516 /* Allow threads to make progress while we are waiting for input. */
517 errcode = pthread_mutex_unlock(&tog_mutex);
518 if (errcode)
519 return got_error_set_errno(errcode);
520 ch = wgetch(view->window);
521 errcode = pthread_mutex_lock(&tog_mutex);
522 if (errcode)
523 return got_error_set_errno(errcode);
524 nodelay(stdscr, TRUE);
526 if (tog_sigwinch_received) {
527 tog_resizeterm();
528 tog_sigwinch_received = 0;
529 TAILQ_FOREACH(v, views, entry) {
530 err = view_resize(v);
531 if (err)
532 return err;
533 err = v->input(new, dead, focus, v, KEY_RESIZE);
534 if (err)
535 return err;
539 switch (ch) {
540 case ERR:
541 break;
542 case '\t':
543 if (view->child) {
544 *focus = view->child;
545 view->child_focussed = 1;
546 } else if (view->parent) {
547 *focus = view->parent;
548 view->parent->child_focussed = 0;
550 break;
551 case 'q':
552 err = view->input(new, dead, focus, view, ch);
553 *dead = view;
554 break;
555 case 'Q':
556 *done = 1;
557 break;
558 case 'f':
559 if (view_is_parent_view(view)) {
560 if (view->child == NULL)
561 break;
562 if (view_is_splitscreen(view->child)) {
563 *focus = view->child;
564 view->child_focussed = 1;
565 err = view_fullscreen(view->child);
566 } else
567 err = view_splitscreen(view->child);
568 if (err)
569 break;
570 err = view->child->input(new, dead, focus,
571 view->child, KEY_RESIZE);
572 } else {
573 if (view_is_splitscreen(view)) {
574 *focus = view;
575 view->parent->child_focussed = 1;
576 err = view_fullscreen(view);
577 } else {
578 err = view_splitscreen(view);
580 if (err)
581 break;
582 err = view->input(new, dead, focus, view,
583 KEY_RESIZE);
585 break;
586 case KEY_RESIZE:
587 break;
588 default:
589 err = view->input(new, dead, focus, view, ch);
590 break;
593 return err;
596 void
597 view_vborder(struct tog_view *view)
599 PANEL *panel;
600 struct tog_view *view_above;
602 if (view->parent)
603 return view_vborder(view->parent);
605 panel = panel_above(view->panel);
606 if (panel == NULL)
607 return;
609 view_above = panel_userptr(panel);
610 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
611 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
614 int
615 view_needs_focus_indication(struct tog_view *view)
617 if (view_is_parent_view(view)) {
618 if (view->child == NULL || view->child_focussed)
619 return 0;
620 if (!view_is_splitscreen(view->child))
621 return 0;
622 } else if (!view_is_splitscreen(view))
623 return 0;
625 return view->focussed;
628 static const struct got_error *
629 view_loop(struct tog_view *view)
631 const struct got_error *err = NULL;
632 struct tog_view_list_head views;
633 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
634 int fast_refresh = 10;
635 int done = 0, errcode;
637 errcode = pthread_mutex_lock(&tog_mutex);
638 if (errcode)
639 return got_error_set_errno(errcode);
641 TAILQ_INIT(&views);
642 TAILQ_INSERT_HEAD(&views, view, entry);
644 main_view = view;
645 view->focussed = 1;
646 err = view->show(view);
647 if (err)
648 return err;
649 update_panels();
650 doupdate();
651 while (!TAILQ_EMPTY(&views) && !done) {
652 /* Refresh fast during initialization, then become slower. */
653 if (fast_refresh && fast_refresh-- == 0)
654 halfdelay(10); /* switch to once per second */
656 err = view_input(&new_view, &dead_view, &focus_view, &done,
657 view, &views);
658 if (err)
659 break;
660 if (dead_view) {
661 struct tog_view *prev = NULL;
663 if (view_is_parent_view(dead_view))
664 prev = TAILQ_PREV(dead_view,
665 tog_view_list_head, entry);
666 else if (view->parent != dead_view)
667 prev = view->parent;
669 if (dead_view->parent)
670 dead_view->parent->child = NULL;
671 else
672 TAILQ_REMOVE(&views, dead_view, entry);
674 err = view_close(dead_view);
675 if (err || dead_view == main_view)
676 goto done;
678 if (view == dead_view) {
679 if (focus_view)
680 view = focus_view;
681 else if (prev)
682 view = prev;
683 else if (!TAILQ_EMPTY(&views))
684 view = TAILQ_LAST(&views,
685 tog_view_list_head);
686 else
687 view = NULL;
688 if (view) {
689 if (view->child && view->child_focussed)
690 focus_view = view->child;
691 else
692 focus_view = view;
696 if (new_view) {
697 struct tog_view *v, *t;
698 /* Only allow one parent view per type. */
699 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
700 if (v->type != new_view->type)
701 continue;
702 TAILQ_REMOVE(&views, v, entry);
703 err = view_close(v);
704 if (err)
705 goto done;
706 break;
708 TAILQ_INSERT_TAIL(&views, new_view, entry);
709 view = new_view;
710 if (focus_view == NULL)
711 focus_view = new_view;
713 if (focus_view) {
714 show_panel(focus_view->panel);
715 if (view)
716 view->focussed = 0;
717 focus_view->focussed = 1;
718 view = focus_view;
719 if (new_view)
720 show_panel(new_view->panel);
721 if (view->child && view_is_splitscreen(view->child))
722 show_panel(view->child->panel);
724 if (view) {
725 if (focus_view == NULL) {
726 view->focussed = 1;
727 show_panel(view->panel);
728 if (view->child && view_is_splitscreen(view->child))
729 show_panel(view->child->panel);
730 focus_view = view;
732 if (view->parent) {
733 err = view->parent->show(view->parent);
734 if (err)
735 goto done;
737 err = view->show(view);
738 if (err)
739 goto done;
740 if (view->child) {
741 err = view->child->show(view->child);
742 if (err)
743 goto done;
745 update_panels();
746 doupdate();
749 done:
750 while (!TAILQ_EMPTY(&views)) {
751 view = TAILQ_FIRST(&views);
752 TAILQ_REMOVE(&views, view, entry);
753 view_close(view);
756 errcode = pthread_mutex_unlock(&tog_mutex);
757 if (errcode)
758 return got_error_set_errno(errcode);
760 return err;
763 __dead static void
764 usage_log(void)
766 endwin();
767 fprintf(stderr,
768 "usage: %s log [-c commit] [-r repository-path] [path]\n",
769 getprogname());
770 exit(1);
773 /* Create newly allocated wide-character string equivalent to a byte string. */
774 static const struct got_error *
775 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
777 char *vis = NULL;
778 const struct got_error *err = NULL;
780 *ws = NULL;
781 *wlen = mbstowcs(NULL, s, 0);
782 if (*wlen == (size_t)-1) {
783 int vislen;
784 if (errno != EILSEQ)
785 return got_error_from_errno();
787 /* byte string invalid in current encoding; try to "fix" it */
788 err = got_mbsavis(&vis, &vislen, s);
789 if (err)
790 return err;
791 *wlen = mbstowcs(NULL, vis, 0);
792 if (*wlen == (size_t)-1) {
793 err = got_error_from_errno(); /* give up */
794 goto done;
798 *ws = calloc(*wlen + 1, sizeof(*ws));
799 if (*ws == NULL) {
800 err = got_error_from_errno();
801 goto done;
804 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
805 err = got_error_from_errno();
806 done:
807 free(vis);
808 if (err) {
809 free(*ws);
810 *ws = NULL;
811 *wlen = 0;
813 return err;
816 /* Format a line for display, ensuring that it won't overflow a width limit. */
817 static const struct got_error *
818 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
820 const struct got_error *err = NULL;
821 int cols = 0;
822 wchar_t *wline = NULL;
823 size_t wlen;
824 int i;
826 *wlinep = NULL;
827 *widthp = 0;
829 err = mbs2ws(&wline, &wlen, line);
830 if (err)
831 return err;
833 i = 0;
834 while (i < wlen && cols < wlimit) {
835 int width = wcwidth(wline[i]);
836 switch (width) {
837 case 0:
838 i++;
839 break;
840 case 1:
841 case 2:
842 if (cols + width <= wlimit)
843 cols += width;
844 i++;
845 break;
846 case -1:
847 if (wline[i] == L'\t')
848 cols += TABSIZE - ((cols + 1) % TABSIZE);
849 i++;
850 break;
851 default:
852 err = got_error_from_errno();
853 goto done;
856 wline[i] = L'\0';
857 if (widthp)
858 *widthp = cols;
859 done:
860 if (err)
861 free(wline);
862 else
863 *wlinep = wline;
864 return err;
867 static const struct got_error *
868 draw_commit(struct tog_view *view, struct got_commit_object *commit,
869 struct got_object_id *id)
871 const struct got_error *err = NULL;
872 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
873 char *logmsg0 = NULL, *logmsg = NULL;
874 char *author0 = NULL, *author = NULL;
875 wchar_t *wlogmsg = NULL, *wauthor = NULL;
876 int author_width, logmsg_width;
877 char *newline, *smallerthan;
878 char *line = NULL;
879 int col, limit;
880 static const size_t date_display_cols = 9;
881 static const size_t author_display_cols = 16;
882 const int avail = view->ncols;
883 struct tm tm;
884 time_t committer_time;
886 committer_time = got_object_commit_get_committer_time(commit);
887 if (localtime_r(&committer_time, &tm) == NULL)
888 return got_error_from_errno();
889 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
890 >= sizeof(datebuf))
891 return got_error(GOT_ERR_NO_SPACE);
893 if (avail < date_display_cols)
894 limit = MIN(sizeof(datebuf) - 1, avail);
895 else
896 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
897 waddnstr(view->window, datebuf, limit);
898 col = limit + 1;
899 if (col > avail)
900 goto done;
902 author0 = strdup(got_object_commit_get_author(commit));
903 if (author0 == NULL) {
904 err = got_error_from_errno();
905 goto done;
907 author = author0;
908 smallerthan = strchr(author, '<');
909 if (smallerthan)
910 *smallerthan = '\0';
911 else {
912 char *at = strchr(author, '@');
913 if (at)
914 *at = '\0';
916 limit = avail - col;
917 err = format_line(&wauthor, &author_width, author, limit);
918 if (err)
919 goto done;
920 waddwstr(view->window, wauthor);
921 col += author_width;
922 while (col <= avail && author_width < author_display_cols + 1) {
923 waddch(view->window, ' ');
924 col++;
925 author_width++;
927 if (col > avail)
928 goto done;
930 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
931 if (logmsg0 == NULL) {
932 err = got_error_from_errno();
933 goto done;
935 logmsg = logmsg0;
936 while (*logmsg == '\n')
937 logmsg++;
938 newline = strchr(logmsg, '\n');
939 if (newline)
940 *newline = '\0';
941 limit = avail - col;
942 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
943 if (err)
944 goto done;
945 waddwstr(view->window, wlogmsg);
946 col += logmsg_width;
947 while (col <= avail) {
948 waddch(view->window, ' ');
949 col++;
951 done:
952 free(logmsg0);
953 free(wlogmsg);
954 free(author0);
955 free(wauthor);
956 free(line);
957 return err;
960 static struct commit_queue_entry *
961 alloc_commit_queue_entry(struct got_commit_object *commit,
962 struct got_object_id *id)
964 struct commit_queue_entry *entry;
966 entry = calloc(1, sizeof(*entry));
967 if (entry == NULL)
968 return NULL;
970 entry->id = id;
971 entry->commit = commit;
972 return entry;
975 static void
976 pop_commit(struct commit_queue *commits)
978 struct commit_queue_entry *entry;
980 entry = TAILQ_FIRST(&commits->head);
981 TAILQ_REMOVE(&commits->head, entry, entry);
982 got_object_commit_close(entry->commit);
983 commits->ncommits--;
984 /* Don't free entry->id! It is owned by the commit graph. */
985 free(entry);
988 static void
989 free_commits(struct commit_queue *commits)
991 while (!TAILQ_EMPTY(&commits->head))
992 pop_commit(commits);
995 static const struct got_error *
996 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
997 int minqueue, struct got_repository *repo, const char *path)
999 const struct got_error *err = NULL;
1000 int nqueued = 0;
1003 * We keep all commits open throughout the lifetime of the log
1004 * view in order to avoid having to re-fetch commits from disk
1005 * while updating the display.
1007 while (nqueued < minqueue) {
1008 struct got_object_id *id;
1009 struct got_commit_object *commit;
1010 struct commit_queue_entry *entry;
1011 int errcode;
1013 err = got_commit_graph_iter_next(&id, graph);
1014 if (err) {
1015 if (err->code != GOT_ERR_ITER_NEED_MORE)
1016 break;
1017 err = got_commit_graph_fetch_commits(graph,
1018 minqueue, repo);
1019 if (err)
1020 return err;
1021 continue;
1024 if (id == NULL)
1025 break;
1027 err = got_object_open_as_commit(&commit, repo, id);
1028 if (err)
1029 break;
1030 entry = alloc_commit_queue_entry(commit, id);
1031 if (entry == NULL) {
1032 err = got_error_from_errno();
1033 break;
1036 errcode = pthread_mutex_lock(&tog_mutex);
1037 if (errcode) {
1038 err = got_error_set_errno(errcode);
1039 break;
1042 entry->idx = commits->ncommits;
1043 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1044 nqueued++;
1045 commits->ncommits++;
1047 errcode = pthread_mutex_unlock(&tog_mutex);
1048 if (errcode && err == NULL)
1049 err = got_error_set_errno(errcode);
1052 return err;
1055 static const struct got_error *
1056 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1058 const struct got_error *err = NULL;
1059 struct got_reference *head_ref;
1061 *head_id = NULL;
1063 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1064 if (err)
1065 return err;
1067 err = got_ref_resolve(head_id, repo, head_ref);
1068 got_ref_close(head_ref);
1069 if (err) {
1070 *head_id = NULL;
1071 return err;
1074 return NULL;
1077 static const struct got_error *
1078 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1079 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1080 struct commit_queue *commits, int selected_idx, int limit,
1081 const char *path, int commits_needed)
1083 const struct got_error *err = NULL;
1084 struct commit_queue_entry *entry;
1085 int ncommits, width;
1086 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1087 wchar_t *wline;
1089 entry = first;
1090 ncommits = 0;
1091 while (entry) {
1092 if (ncommits == selected_idx) {
1093 *selected = entry;
1094 break;
1096 entry = TAILQ_NEXT(entry, entry);
1097 ncommits++;
1100 if (*selected) {
1101 err = got_object_id_str(&id_str, (*selected)->id);
1102 if (err)
1103 return err;
1106 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1107 entry ? entry->idx + 1 : 0, commits->ncommits,
1108 commits_needed == 0 ? "" : " loading...") == -1)
1109 return got_error_from_errno();
1111 if (path && strcmp(path, "/") != 0) {
1112 if (asprintf(&header, "commit %s %s%s",
1113 id_str ? id_str : "........................................",
1114 path, ncommits_str) == -1) {
1115 err = got_error_from_errno();
1116 header = NULL;
1117 goto done;
1119 } else if (asprintf(&header, "commit %s%s",
1120 id_str ? id_str : "........................................",
1121 ncommits_str) == -1) {
1122 err = got_error_from_errno();
1123 header = NULL;
1124 goto done;
1126 err = format_line(&wline, &width, header, view->ncols);
1127 if (err)
1128 goto done;
1130 werase(view->window);
1132 if (view_needs_focus_indication(view))
1133 wstandout(view->window);
1134 waddwstr(view->window, wline);
1135 while (width < view->ncols) {
1136 waddch(view->window, ' ');
1137 width++;
1139 if (view_needs_focus_indication(view))
1140 wstandend(view->window);
1141 free(wline);
1142 if (limit <= 1)
1143 goto done;
1145 entry = first;
1146 *last = first;
1147 ncommits = 0;
1148 while (entry) {
1149 if (ncommits >= limit - 1)
1150 break;
1151 if (view->focussed && ncommits == selected_idx)
1152 wstandout(view->window);
1153 err = draw_commit(view, entry->commit, entry->id);
1154 if (view->focussed && ncommits == selected_idx)
1155 wstandend(view->window);
1156 if (err)
1157 break;
1158 ncommits++;
1159 *last = entry;
1160 entry = TAILQ_NEXT(entry, entry);
1163 view_vborder(view);
1164 done:
1165 free(id_str);
1166 free(ncommits_str);
1167 free(header);
1168 return err;
1171 static void
1172 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1173 struct commit_queue *commits)
1175 struct commit_queue_entry *entry;
1176 int nscrolled = 0;
1178 entry = TAILQ_FIRST(&commits->head);
1179 if (*first_displayed_entry == entry)
1180 return;
1182 entry = *first_displayed_entry;
1183 while (entry && nscrolled < maxscroll) {
1184 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1185 if (entry) {
1186 *first_displayed_entry = entry;
1187 nscrolled++;
1192 static const struct got_error *
1193 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1194 struct commit_queue_entry **last_displayed_entry,
1195 struct commit_queue *commits, int *log_complete, int *commits_needed,
1196 pthread_cond_t *need_commits)
1198 const struct got_error *err = NULL;
1199 struct commit_queue_entry *pentry;
1200 int nscrolled = 0;
1202 if (*last_displayed_entry == NULL)
1203 return NULL;
1205 do {
1206 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1207 while (pentry == NULL) {
1208 int errcode;
1209 if (*log_complete)
1210 break;
1211 *commits_needed = maxscroll + 20;
1212 errcode = pthread_cond_signal(need_commits);
1213 if (errcode)
1214 return got_error_set_errno(errcode);
1215 errcode = pthread_mutex_unlock(&tog_mutex);
1216 if (errcode)
1217 return got_error_set_errno(errcode);
1218 pthread_yield();
1219 errcode = pthread_mutex_lock(&tog_mutex);
1220 if (errcode)
1221 return got_error_set_errno(errcode);
1222 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1224 if (pentry == NULL)
1225 break;
1227 *last_displayed_entry = pentry;
1229 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1230 if (pentry == NULL)
1231 break;
1232 *first_displayed_entry = pentry;
1233 } while (++nscrolled < maxscroll);
1235 return err;
1238 static const struct got_error *
1239 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1240 struct got_commit_object *commit, struct got_object_id *commit_id,
1241 struct tog_view *log_view, struct got_repository *repo)
1243 const struct got_error *err;
1244 struct got_object_qid *parent_id;
1245 struct tog_view *diff_view;
1247 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1248 if (diff_view == NULL)
1249 return got_error_from_errno();
1251 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1252 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1253 commit_id, log_view, repo);
1254 if (err == NULL)
1255 *new_view = diff_view;
1256 return err;
1259 static const struct got_error *
1260 browse_commit(struct tog_view **new_view, int begin_x,
1261 struct commit_queue_entry *entry, struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 struct got_tree_object *tree;
1265 struct tog_view *tree_view;
1267 err = got_object_open_as_tree(&tree, repo,
1268 got_object_commit_get_tree_id(entry->commit));
1269 if (err)
1270 return err;
1272 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1273 if (tree_view == NULL)
1274 return got_error_from_errno();
1276 err = open_tree_view(tree_view, tree, entry->id, repo);
1277 if (err)
1278 got_object_tree_close(tree);
1279 else
1280 *new_view = tree_view;
1281 return err;
1284 static void *
1285 log_thread(void *arg)
1287 const struct got_error *err = NULL;
1288 int errcode = 0;
1289 struct tog_log_thread_args *a = arg;
1290 int done = 0;
1292 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1293 if (err)
1294 return (void *)err;
1296 while (!done && !err) {
1297 err = queue_commits(a->graph, a->commits, 1, a->repo,
1298 a->in_repo_path);
1299 if (err) {
1300 if (err->code != GOT_ERR_ITER_COMPLETED)
1301 return (void *)err;
1302 err = NULL;
1303 done = 1;
1304 } else if (a->commits_needed > 0)
1305 a->commits_needed--;
1307 errcode = pthread_mutex_lock(&tog_mutex);
1308 if (errcode)
1309 return (void *)got_error_set_errno(errcode);
1311 if (done)
1312 a->log_complete = 1;
1313 else if (*a->quit) {
1314 done = 1;
1315 a->log_complete = 1;
1316 } else if (*a->first_displayed_entry == NULL) {
1317 *a->first_displayed_entry =
1318 TAILQ_FIRST(&a->commits->head);
1319 *a->selected_entry = *a->first_displayed_entry;
1322 if (done)
1323 a->commits_needed = 0;
1324 else if (a->commits_needed == 0) {
1325 errcode = pthread_cond_wait(&a->need_commits,
1326 &tog_mutex);
1327 if (errcode)
1328 err = got_error_set_errno(errcode);
1331 errcode = pthread_mutex_unlock(&tog_mutex);
1332 if (errcode && err == NULL)
1333 err = got_error_set_errno(errcode);
1335 return (void *)err;
1338 static const struct got_error *
1339 stop_log_thread(struct tog_log_view_state *s)
1341 const struct got_error *err = NULL;
1342 int errcode;
1344 if (s->thread) {
1345 s->quit = 1;
1346 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1347 if (errcode)
1348 return got_error_set_errno(errcode);
1349 errcode = pthread_mutex_unlock(&tog_mutex);
1350 if (errcode)
1351 return got_error_set_errno(errcode);
1352 errcode = pthread_join(s->thread, (void **)&err);
1353 if (errcode)
1354 return got_error_set_errno(errcode);
1355 errcode = pthread_mutex_lock(&tog_mutex);
1356 if (errcode)
1357 return got_error_set_errno(errcode);
1358 s->thread = NULL;
1361 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1362 if (errcode && err == NULL)
1363 err = got_error_set_errno(errcode);
1365 if (s->thread_args.repo) {
1366 got_repo_close(s->thread_args.repo);
1367 s->thread_args.repo = NULL;
1370 if (s->thread_args.graph) {
1371 got_commit_graph_close(s->thread_args.graph);
1372 s->thread_args.graph = NULL;
1375 return err;
1378 static const struct got_error *
1379 close_log_view(struct tog_view *view)
1381 const struct got_error *err = NULL;
1382 struct tog_log_view_state *s = &view->state.log;
1384 err = stop_log_thread(s);
1385 free_commits(&s->commits);
1386 free(s->in_repo_path);
1387 s->in_repo_path = NULL;
1388 free(s->start_id);
1389 s->start_id = NULL;
1390 return err;
1393 static const struct got_error *
1394 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1395 struct got_repository *repo, const char *path, int check_disk)
1397 const struct got_error *err = NULL;
1398 struct tog_log_view_state *s = &view->state.log;
1399 struct got_repository *thread_repo = NULL;
1400 struct got_commit_graph *thread_graph = NULL;
1401 int errcode;
1403 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1404 if (err != NULL)
1405 goto done;
1407 /* The commit queue only contains commits being displayed. */
1408 TAILQ_INIT(&s->commits.head);
1409 s->commits.ncommits = 0;
1411 s->repo = repo;
1412 s->start_id = got_object_id_dup(start_id);
1413 if (s->start_id == NULL) {
1414 err = got_error_from_errno();
1415 goto done;
1418 view->show = show_log_view;
1419 view->input = input_log_view;
1420 view->close = close_log_view;
1422 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1423 if (err)
1424 goto done;
1425 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1426 0, thread_repo);
1427 if (err)
1428 goto done;
1430 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1431 if (errcode) {
1432 err = got_error_set_errno(errcode);
1433 goto done;
1436 s->thread_args.commits_needed = view->nlines;
1437 s->thread_args.graph = thread_graph;
1438 s->thread_args.commits = &s->commits;
1439 s->thread_args.in_repo_path = s->in_repo_path;
1440 s->thread_args.start_id = s->start_id;
1441 s->thread_args.repo = thread_repo;
1442 s->thread_args.log_complete = 0;
1443 s->thread_args.quit = &s->quit;
1444 s->thread_args.view = view;
1445 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1446 s->thread_args.selected_entry = &s->selected_entry;
1447 done:
1448 if (err)
1449 close_log_view(view);
1450 return err;
1453 static const struct got_error *
1454 show_log_view(struct tog_view *view)
1456 struct tog_log_view_state *s = &view->state.log;
1458 if (s->thread == NULL) {
1459 int errcode = pthread_create(&s->thread, NULL, log_thread,
1460 &s->thread_args);
1461 if (errcode)
1462 return got_error_set_errno(errcode);
1465 return draw_commits(view, &s->last_displayed_entry,
1466 &s->selected_entry, s->first_displayed_entry,
1467 &s->commits, s->selected, view->nlines,
1468 s->in_repo_path, s->thread_args.commits_needed);
1471 static const struct got_error *
1472 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1473 struct tog_view **focus_view, struct tog_view *view, int ch)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1477 char *parent_path;
1478 struct tog_view *diff_view = NULL, *tree_view = NULL;
1479 int begin_x = 0;
1481 switch (ch) {
1482 case 'q':
1483 s->quit = 1;
1484 break;
1485 case 'k':
1486 case KEY_UP:
1487 case '<':
1488 case ',':
1489 if (s->first_displayed_entry == NULL)
1490 break;
1491 if (s->selected > 0)
1492 s->selected--;
1493 if (s->selected > 0)
1494 break;
1495 scroll_up(&s->first_displayed_entry, 1,
1496 &s->commits);
1497 break;
1498 case KEY_PPAGE:
1499 if (s->first_displayed_entry == NULL)
1500 break;
1501 if (TAILQ_FIRST(&s->commits.head) ==
1502 s->first_displayed_entry) {
1503 s->selected = 0;
1504 break;
1506 scroll_up(&s->first_displayed_entry,
1507 view->nlines, &s->commits);
1508 break;
1509 case 'j':
1510 case KEY_DOWN:
1511 case '>':
1512 case '.':
1513 if (s->first_displayed_entry == NULL)
1514 break;
1515 if (s->selected < MIN(view->nlines - 2,
1516 s->commits.ncommits - 1)) {
1517 s->selected++;
1518 break;
1520 err = scroll_down(&s->first_displayed_entry, 1,
1521 &s->last_displayed_entry, &s->commits,
1522 &s->thread_args.log_complete,
1523 &s->thread_args.commits_needed,
1524 &s->thread_args.need_commits);
1525 break;
1526 case KEY_NPAGE: {
1527 struct commit_queue_entry *first;
1528 first = s->first_displayed_entry;
1529 if (first == NULL)
1530 break;
1531 err = scroll_down(&s->first_displayed_entry,
1532 view->nlines, &s->last_displayed_entry,
1533 &s->commits, &s->thread_args.log_complete,
1534 &s->thread_args.commits_needed,
1535 &s->thread_args.need_commits);
1536 if (first == s->first_displayed_entry &&
1537 s->selected < MIN(view->nlines - 2,
1538 s->commits.ncommits - 1)) {
1539 /* can't scroll further down */
1540 s->selected = MIN(view->nlines - 2,
1541 s->commits.ncommits - 1);
1543 err = NULL;
1544 break;
1546 case KEY_RESIZE:
1547 if (s->selected > view->nlines - 2)
1548 s->selected = view->nlines - 2;
1549 if (s->selected > s->commits.ncommits - 1)
1550 s->selected = s->commits.ncommits - 1;
1551 break;
1552 case KEY_ENTER:
1553 case '\r':
1554 if (s->selected_entry == NULL)
1555 break;
1556 if (view_is_parent_view(view))
1557 begin_x = view_split_begin_x(view->begin_x);
1558 err = open_diff_view_for_commit(&diff_view, begin_x,
1559 s->selected_entry->commit, s->selected_entry->id,
1560 view, s->repo);
1561 if (err)
1562 break;
1563 if (view_is_parent_view(view)) {
1564 err = view_close_child(view);
1565 if (err)
1566 return err;
1567 err = view_set_child(view, diff_view);
1568 if (err) {
1569 view_close(diff_view);
1570 break;
1572 *focus_view = diff_view;
1573 view->child_focussed = 1;
1574 } else
1575 *new_view = diff_view;
1576 break;
1577 case 't':
1578 if (s->selected_entry == NULL)
1579 break;
1580 if (view_is_parent_view(view))
1581 begin_x = view_split_begin_x(view->begin_x);
1582 err = browse_commit(&tree_view, begin_x,
1583 s->selected_entry, s->repo);
1584 if (err)
1585 break;
1586 if (view_is_parent_view(view)) {
1587 err = view_close_child(view);
1588 if (err)
1589 return err;
1590 err = view_set_child(view, tree_view);
1591 if (err) {
1592 view_close(tree_view);
1593 break;
1595 *focus_view = tree_view;
1596 view->child_focussed = 1;
1597 } else
1598 *new_view = tree_view;
1599 break;
1600 case KEY_BACKSPACE:
1601 if (strcmp(s->in_repo_path, "/") == 0)
1602 break;
1603 parent_path = dirname(s->in_repo_path);
1604 if (parent_path && strcmp(parent_path, ".") != 0) {
1605 struct tog_view *lv;
1606 err = stop_log_thread(s);
1607 if (err)
1608 return err;
1609 lv = view_open(view->nlines, view->ncols,
1610 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1611 if (lv == NULL)
1612 return got_error_from_errno();
1613 err = open_log_view(lv, s->start_id, s->repo,
1614 parent_path, 0);
1615 if (err)
1616 return err;;
1617 if (view_is_parent_view(view))
1618 *new_view = lv;
1619 else {
1620 view_set_child(view->parent, lv);
1621 *focus_view = lv;
1623 return NULL;
1625 break;
1626 default:
1627 break;
1630 return err;
1633 static const struct got_error *
1634 apply_unveil(const char *repo_path, const char *worktree_path)
1636 const struct got_error *error;
1638 if (repo_path && unveil(repo_path, "r") != 0)
1639 return got_error_from_errno();
1641 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1642 return got_error_from_errno();
1644 if (unveil("/tmp", "rwc") != 0)
1645 return got_error_from_errno();
1647 error = got_privsep_unveil_exec_helpers();
1648 if (error != NULL)
1649 return error;
1651 if (unveil(NULL, NULL) != 0)
1652 return got_error_from_errno();
1654 return NULL;
1657 static void
1658 init_curses(void)
1660 initscr();
1661 cbreak();
1662 halfdelay(1); /* Do fast refresh while initial view is loading. */
1663 noecho();
1664 nonl();
1665 intrflush(stdscr, FALSE);
1666 keypad(stdscr, TRUE);
1667 curs_set(0);
1668 signal(SIGWINCH, tog_sigwinch);
1671 static const struct got_error *
1672 cmd_log(int argc, char *argv[])
1674 const struct got_error *error;
1675 struct got_repository *repo = NULL;
1676 struct got_object_id *start_id = NULL;
1677 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1678 char *start_commit = NULL;
1679 int ch;
1680 struct tog_view *view;
1682 #ifndef PROFILE
1683 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1684 NULL) == -1)
1685 err(1, "pledge");
1686 #endif
1688 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1689 switch (ch) {
1690 case 'c':
1691 start_commit = optarg;
1692 break;
1693 case 'r':
1694 repo_path = realpath(optarg, NULL);
1695 if (repo_path == NULL)
1696 err(1, "-r option");
1697 break;
1698 default:
1699 usage();
1700 /* NOTREACHED */
1704 argc -= optind;
1705 argv += optind;
1707 if (argc == 0)
1708 path = strdup("");
1709 else if (argc == 1)
1710 path = strdup(argv[0]);
1711 else
1712 usage_log();
1713 if (path == NULL)
1714 return got_error_from_errno();
1716 cwd = getcwd(NULL, 0);
1717 if (cwd == NULL) {
1718 error = got_error_from_errno();
1719 goto done;
1721 if (repo_path == NULL) {
1722 struct got_worktree *worktree;
1723 error = got_worktree_open(&worktree, cwd);
1724 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1725 goto done;
1726 if (worktree) {
1727 repo_path =
1728 strdup(got_worktree_get_repo_path(worktree));
1729 got_worktree_close(worktree);
1730 } else
1731 repo_path = strdup(cwd);
1732 if (repo_path == NULL) {
1733 error = got_error_from_errno();
1734 goto done;
1738 init_curses();
1740 error = apply_unveil(repo_path, NULL);
1741 if (error)
1742 goto done;
1744 error = got_repo_open(&repo, repo_path);
1745 if (error != NULL)
1746 goto done;
1748 if (start_commit == NULL)
1749 error = get_head_commit_id(&start_id, repo);
1750 else
1751 error = got_object_resolve_id_str(&start_id, repo,
1752 start_commit);
1753 if (error != NULL)
1754 goto done;
1756 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1757 if (view == NULL) {
1758 error = got_error_from_errno();
1759 goto done;
1761 error = open_log_view(view, start_id, repo, path, 1);
1762 if (error)
1763 goto done;
1764 error = view_loop(view);
1765 done:
1766 free(repo_path);
1767 free(cwd);
1768 free(path);
1769 free(start_id);
1770 if (repo)
1771 got_repo_close(repo);
1772 return error;
1775 __dead static void
1776 usage_diff(void)
1778 endwin();
1779 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1780 getprogname());
1781 exit(1);
1784 static char *
1785 parse_next_line(FILE *f, size_t *len)
1787 char *line;
1788 size_t linelen;
1789 size_t lineno;
1790 const char delim[3] = { '\0', '\0', '\0'};
1792 line = fparseln(f, &linelen, &lineno, delim, 0);
1793 if (len)
1794 *len = linelen;
1795 return line;
1798 static const struct got_error *
1799 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1800 int *last_displayed_line, int *eof, int max_lines,
1801 char * header)
1803 const struct got_error *err;
1804 int nlines = 0, nprinted = 0;
1805 char *line;
1806 size_t len;
1807 wchar_t *wline;
1808 int width;
1810 rewind(f);
1811 werase(view->window);
1813 if (header) {
1814 err = format_line(&wline, &width, header, view->ncols);
1815 if (err) {
1816 return err;
1819 if (view_needs_focus_indication(view))
1820 wstandout(view->window);
1821 waddwstr(view->window, wline);
1822 if (view_needs_focus_indication(view))
1823 wstandend(view->window);
1824 if (width < view->ncols)
1825 waddch(view->window, '\n');
1827 if (max_lines <= 1)
1828 return NULL;
1829 max_lines--;
1832 *eof = 0;
1833 while (nprinted < max_lines) {
1834 line = parse_next_line(f, &len);
1835 if (line == NULL) {
1836 *eof = 1;
1837 break;
1839 if (++nlines < *first_displayed_line) {
1840 free(line);
1841 continue;
1844 err = format_line(&wline, &width, line, view->ncols);
1845 if (err) {
1846 free(line);
1847 return err;
1849 waddwstr(view->window, wline);
1850 if (width < view->ncols)
1851 waddch(view->window, '\n');
1852 if (++nprinted == 1)
1853 *first_displayed_line = nlines;
1854 free(line);
1855 free(wline);
1856 wline = NULL;
1858 *last_displayed_line = nlines;
1860 view_vborder(view);
1862 return NULL;
1865 static char *
1866 get_datestr(time_t *time, char *datebuf)
1868 char *p, *s = ctime_r(time, datebuf);
1869 p = strchr(s, '\n');
1870 if (p)
1871 *p = '\0';
1872 return s;
1875 static const struct got_error *
1876 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1877 FILE *outfile)
1879 const struct got_error *err = NULL;
1880 char datebuf[26];
1881 struct got_commit_object *commit;
1882 char *id_str = NULL;
1883 time_t committer_time;
1884 const char *author, *committer;
1886 err = got_object_open_as_commit(&commit, repo, commit_id);
1887 if (err)
1888 return err;
1890 err = got_object_id_str(&id_str, commit_id);
1891 if (err) {
1892 err = got_error_from_errno();
1893 goto done;
1896 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1897 err = got_error_from_errno();
1898 goto done;
1900 if (fprintf(outfile, "from: %s\n",
1901 got_object_commit_get_author(commit)) < 0) {
1902 err = got_error_from_errno();
1903 goto done;
1905 committer_time = got_object_commit_get_committer_time(commit);
1906 if (fprintf(outfile, "date: %s UTC\n",
1907 get_datestr(&committer_time, datebuf)) < 0) {
1908 err = got_error_from_errno();
1909 goto done;
1911 author = got_object_commit_get_author(commit);
1912 committer = got_object_commit_get_committer(commit);
1913 if (strcmp(author, committer) != 0 &&
1914 fprintf(outfile, "via: %s\n", committer) < 0) {
1915 err = got_error_from_errno();
1916 goto done;
1918 if (fprintf(outfile, "%s\n",
1919 got_object_commit_get_logmsg(commit)) < 0) {
1920 err = got_error_from_errno();
1921 goto done;
1923 done:
1924 free(id_str);
1925 got_object_commit_close(commit);
1926 return err;
1929 static const struct got_error *
1930 create_diff(struct tog_diff_view_state *s)
1932 const struct got_error *err = NULL;
1933 FILE *f = NULL;
1934 int obj_type;
1936 f = got_opentemp();
1937 if (f == NULL) {
1938 err = got_error_from_errno();
1939 goto done;
1941 if (s->f && fclose(s->f) != 0) {
1942 err = got_error_from_errno();
1943 goto done;
1945 s->f = f;
1947 if (s->id1)
1948 err = got_object_get_type(&obj_type, s->repo, s->id1);
1949 else
1950 err = got_object_get_type(&obj_type, s->repo, s->id2);
1951 if (err)
1952 goto done;
1954 switch (obj_type) {
1955 case GOT_OBJ_TYPE_BLOB:
1956 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1957 s->diff_context, s->repo, f);
1958 break;
1959 case GOT_OBJ_TYPE_TREE:
1960 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1961 s->diff_context, s->repo, f);
1962 break;
1963 case GOT_OBJ_TYPE_COMMIT: {
1964 const struct got_object_id_queue *parent_ids;
1965 struct got_object_qid *pid;
1966 struct got_commit_object *commit2;
1968 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1969 if (err)
1970 break;
1971 /* Show commit info if we're diffing to a parent/root commit. */
1972 if (s->id1 == NULL)
1973 write_commit_info(s->id2, s->repo, f);
1974 else {
1975 parent_ids = got_object_commit_get_parent_ids(commit2);
1976 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1977 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1978 write_commit_info(s->id2, s->repo, f);
1979 break;
1983 got_object_commit_close(commit2);
1985 err = got_diff_objects_as_commits(s->id1, s->id2,
1986 s->diff_context, s->repo, f);
1987 break;
1989 default:
1990 err = got_error(GOT_ERR_OBJ_TYPE);
1991 break;
1993 done:
1994 if (f && fflush(f) != 0 && err == NULL)
1995 err = got_error_from_errno();
1996 return err;
1999 static const struct got_error *
2000 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2001 struct got_object_id *id2, struct tog_view *log_view,
2002 struct got_repository *repo)
2004 const struct got_error *err;
2006 if (id1 != NULL && id2 != NULL) {
2007 int type1, type2;
2008 err = got_object_get_type(&type1, repo, id1);
2009 if (err)
2010 return err;
2011 err = got_object_get_type(&type2, repo, id2);
2012 if (err)
2013 return err;
2015 if (type1 != type2)
2016 return got_error(GOT_ERR_OBJ_TYPE);
2019 if (id1) {
2020 view->state.diff.id1 = got_object_id_dup(id1);
2021 if (view->state.diff.id1 == NULL)
2022 return got_error_from_errno();
2023 } else
2024 view->state.diff.id1 = NULL;
2026 view->state.diff.id2 = got_object_id_dup(id2);
2027 if (view->state.diff.id2 == NULL) {
2028 free(view->state.diff.id1);
2029 view->state.diff.id1 = NULL;
2030 return got_error_from_errno();
2032 view->state.diff.f = NULL;
2033 view->state.diff.first_displayed_line = 1;
2034 view->state.diff.last_displayed_line = view->nlines;
2035 view->state.diff.diff_context = 3;
2036 view->state.diff.log_view = log_view;
2037 view->state.diff.repo = repo;
2039 err = create_diff(&view->state.diff);
2040 if (err) {
2041 free(view->state.diff.id1);
2042 view->state.diff.id1 = NULL;
2043 free(view->state.diff.id2);
2044 view->state.diff.id2 = NULL;
2045 return err;
2048 view->show = show_diff_view;
2049 view->input = input_diff_view;
2050 view->close = close_diff_view;
2052 return NULL;
2055 static const struct got_error *
2056 close_diff_view(struct tog_view *view)
2058 const struct got_error *err = NULL;
2060 free(view->state.diff.id1);
2061 view->state.diff.id1 = NULL;
2062 free(view->state.diff.id2);
2063 view->state.diff.id2 = NULL;
2064 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2065 err = got_error_from_errno();
2066 return err;
2069 static const struct got_error *
2070 show_diff_view(struct tog_view *view)
2072 const struct got_error *err;
2073 struct tog_diff_view_state *s = &view->state.diff;
2074 char *id_str1 = NULL, *id_str2, *header;
2076 if (s->id1) {
2077 err = got_object_id_str(&id_str1, s->id1);
2078 if (err)
2079 return err;
2081 err = got_object_id_str(&id_str2, s->id2);
2082 if (err)
2083 return err;
2085 if (asprintf(&header, "diff %s %s",
2086 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2087 err = got_error_from_errno();
2088 free(id_str1);
2089 free(id_str2);
2090 return err;
2092 free(id_str1);
2093 free(id_str2);
2095 return draw_file(view, s->f, &s->first_displayed_line,
2096 &s->last_displayed_line, &s->eof, view->nlines,
2097 header);
2100 static const struct got_error *
2101 set_selected_commit(struct tog_diff_view_state *s,
2102 struct commit_queue_entry *entry)
2104 struct commit_queue_entry *pentry;
2106 free(s->id2);
2107 s->id2 = got_object_id_dup(entry->id);
2108 if (s->id2 == NULL)
2109 return got_error_from_errno();
2111 free(s->id1);
2112 s->id1 = NULL;
2113 pentry = TAILQ_NEXT(entry, entry);
2114 if (pentry) {
2115 s->id1 = got_object_id_dup(pentry->id);
2116 if (s->id1 == NULL)
2117 return got_error_from_errno();
2120 return NULL;
2123 static const struct got_error *
2124 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2125 struct tog_view **focus_view, struct tog_view *view, int ch)
2127 const struct got_error *err = NULL;
2128 struct tog_diff_view_state *s = &view->state.diff;
2129 struct tog_log_view_state *ls;
2130 struct commit_queue_entry *entry;
2131 int i;
2133 switch (ch) {
2134 case 'k':
2135 case KEY_UP:
2136 if (s->first_displayed_line > 1)
2137 s->first_displayed_line--;
2138 break;
2139 case KEY_PPAGE:
2140 i = 0;
2141 while (i++ < view->nlines - 1 &&
2142 s->first_displayed_line > 1)
2143 s->first_displayed_line--;
2144 break;
2145 case 'j':
2146 case KEY_DOWN:
2147 if (!s->eof)
2148 s->first_displayed_line++;
2149 break;
2150 case KEY_NPAGE:
2151 case ' ':
2152 i = 0;
2153 while (!s->eof && i++ < view->nlines - 1) {
2154 char *line;
2155 line = parse_next_line(s->f, NULL);
2156 s->first_displayed_line++;
2157 if (line == NULL)
2158 break;
2160 break;
2161 case '[':
2162 if (s->diff_context > 0) {
2163 s->diff_context--;
2164 err = create_diff(s);
2166 break;
2167 case ']':
2168 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2169 s->diff_context++;
2170 err = create_diff(s);
2172 break;
2173 case '<':
2174 case ',':
2175 if (s->log_view == NULL)
2176 break;
2177 err = input_log_view(NULL, NULL, NULL, s->log_view,
2178 KEY_UP);
2179 if (err)
2180 break;
2181 ls = &s->log_view->state.log;
2182 entry = TAILQ_PREV(ls->selected_entry,
2183 commit_queue_head, entry);
2184 if (entry == NULL)
2185 break;
2187 err = set_selected_commit(s, entry);
2188 if (err)
2189 break;
2191 s->first_displayed_line = 1;
2192 s->last_displayed_line = view->nlines;
2194 err = create_diff(s);
2195 break;
2196 case '>':
2197 case '.':
2198 if (s->log_view == NULL)
2199 break;
2201 ls = &s->log_view->state.log;
2202 err = input_log_view(NULL, NULL, NULL, s->log_view,
2203 KEY_DOWN);
2204 if (err)
2205 break;
2207 /* Hack: Ensure two commits get loaded. */
2208 if (!ls->thread_args.log_complete) {
2209 err = input_log_view(NULL, NULL, NULL,
2210 s->log_view, KEY_DOWN);
2211 if (err)
2212 break;
2213 err = input_log_view(NULL, NULL, NULL,
2214 s->log_view, KEY_UP);
2215 if (err)
2216 break;
2219 entry = TAILQ_NEXT(ls->selected_entry, entry);
2220 if (entry == NULL)
2221 break;
2223 err = set_selected_commit(s, entry);
2224 if (err)
2225 break;
2227 s->first_displayed_line = 1;
2228 s->last_displayed_line = view->nlines;
2230 err = create_diff(s);
2231 break;
2232 default:
2233 break;
2236 return err;
2239 static const struct got_error *
2240 cmd_diff(int argc, char *argv[])
2242 const struct got_error *error = NULL;
2243 struct got_repository *repo = NULL;
2244 struct got_object_id *id1 = NULL, *id2 = NULL;
2245 char *repo_path = NULL;
2246 char *id_str1 = NULL, *id_str2 = NULL;
2247 int ch;
2248 struct tog_view *view;
2250 #ifndef PROFILE
2251 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2252 NULL) == -1)
2253 err(1, "pledge");
2254 #endif
2256 while ((ch = getopt(argc, argv, "")) != -1) {
2257 switch (ch) {
2258 default:
2259 usage();
2260 /* NOTREACHED */
2264 argc -= optind;
2265 argv += optind;
2267 if (argc == 0) {
2268 usage_diff(); /* TODO show local worktree changes */
2269 } else if (argc == 2) {
2270 repo_path = getcwd(NULL, 0);
2271 if (repo_path == NULL)
2272 return got_error_from_errno();
2273 id_str1 = argv[0];
2274 id_str2 = argv[1];
2275 } else if (argc == 3) {
2276 repo_path = realpath(argv[0], NULL);
2277 if (repo_path == NULL)
2278 return got_error_from_errno();
2279 id_str1 = argv[1];
2280 id_str2 = argv[2];
2281 } else
2282 usage_diff();
2284 init_curses();
2286 error = apply_unveil(repo_path, NULL);
2287 if (error)
2288 goto done;
2290 error = got_repo_open(&repo, repo_path);
2291 free(repo_path);
2292 if (error)
2293 goto done;
2295 error = got_object_resolve_id_str(&id1, repo, id_str1);
2296 if (error)
2297 goto done;
2299 error = got_object_resolve_id_str(&id2, repo, id_str2);
2300 if (error)
2301 goto done;
2303 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2304 if (view == NULL) {
2305 error = got_error_from_errno();
2306 goto done;
2308 error = open_diff_view(view, id1, id2, NULL, repo);
2309 if (error)
2310 goto done;
2311 error = view_loop(view);
2312 done:
2313 got_repo_close(repo);
2314 return error;
2317 __dead static void
2318 usage_blame(void)
2320 endwin();
2321 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2322 getprogname());
2323 exit(1);
2326 struct tog_blame_line {
2327 int annotated;
2328 struct got_object_id *id;
2331 static const struct got_error *
2332 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2333 const char *path, struct tog_blame_line *lines, int nlines,
2334 int blame_complete, int selected_line, int *first_displayed_line,
2335 int *last_displayed_line, int *eof, int max_lines)
2337 const struct got_error *err;
2338 int lineno = 0, nprinted = 0;
2339 char *line;
2340 size_t len;
2341 wchar_t *wline;
2342 int width, wlimit;
2343 struct tog_blame_line *blame_line;
2344 struct got_object_id *prev_id = NULL;
2345 char *id_str;
2347 err = got_object_id_str(&id_str, id);
2348 if (err)
2349 return err;
2351 rewind(f);
2352 werase(view->window);
2354 if (asprintf(&line, "commit %s", id_str) == -1) {
2355 err = got_error_from_errno();
2356 free(id_str);
2357 return err;
2360 err = format_line(&wline, &width, line, view->ncols);
2361 free(line);
2362 line = NULL;
2363 if (view_needs_focus_indication(view))
2364 wstandout(view->window);
2365 waddwstr(view->window, wline);
2366 if (view_needs_focus_indication(view))
2367 wstandend(view->window);
2368 free(wline);
2369 wline = NULL;
2370 if (width < view->ncols)
2371 waddch(view->window, '\n');
2373 if (asprintf(&line, "[%d/%d] %s%s",
2374 *first_displayed_line - 1 + selected_line, nlines,
2375 blame_complete ? "" : "annotating ", path) == -1) {
2376 free(id_str);
2377 return got_error_from_errno();
2379 free(id_str);
2380 err = format_line(&wline, &width, line, view->ncols);
2381 free(line);
2382 line = NULL;
2383 if (err)
2384 return err;
2385 waddwstr(view->window, wline);
2386 free(wline);
2387 wline = NULL;
2388 if (width < view->ncols)
2389 waddch(view->window, '\n');
2391 *eof = 0;
2392 while (nprinted < max_lines - 2) {
2393 line = parse_next_line(f, &len);
2394 if (line == NULL) {
2395 *eof = 1;
2396 break;
2398 if (++lineno < *first_displayed_line) {
2399 free(line);
2400 continue;
2403 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2404 err = format_line(&wline, &width, line, wlimit);
2405 if (err) {
2406 free(line);
2407 return err;
2410 if (view->focussed && nprinted == selected_line - 1)
2411 wstandout(view->window);
2413 blame_line = &lines[lineno - 1];
2414 if (blame_line->annotated && prev_id &&
2415 got_object_id_cmp(prev_id, blame_line->id) == 0)
2416 waddstr(view->window, " ");
2417 else if (blame_line->annotated) {
2418 char *id_str;
2419 err = got_object_id_str(&id_str, blame_line->id);
2420 if (err) {
2421 free(line);
2422 free(wline);
2423 return err;
2425 wprintw(view->window, "%.8s ", id_str);
2426 free(id_str);
2427 prev_id = blame_line->id;
2428 } else {
2429 waddstr(view->window, "........ ");
2430 prev_id = NULL;
2433 waddwstr(view->window, wline);
2434 while (width < wlimit) {
2435 waddch(view->window, ' ');
2436 width++;
2438 if (view->focussed && nprinted == selected_line - 1)
2439 wstandend(view->window);
2440 if (++nprinted == 1)
2441 *first_displayed_line = lineno;
2442 free(line);
2443 free(wline);
2444 wline = NULL;
2446 *last_displayed_line = lineno;
2448 view_vborder(view);
2450 return NULL;
2453 static const struct got_error *
2454 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2456 const struct got_error *err = NULL;
2457 struct tog_blame_cb_args *a = arg;
2458 struct tog_blame_line *line;
2459 int errcode;
2461 if (nlines != a->nlines ||
2462 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2463 return got_error(GOT_ERR_RANGE);
2465 errcode = pthread_mutex_lock(&tog_mutex);
2466 if (errcode)
2467 return got_error_set_errno(errcode);
2469 if (*a->quit) { /* user has quit the blame view */
2470 err = got_error(GOT_ERR_ITER_COMPLETED);
2471 goto done;
2474 if (lineno == -1)
2475 goto done; /* no change in this commit */
2477 line = &a->lines[lineno - 1];
2478 if (line->annotated)
2479 goto done;
2481 line->id = got_object_id_dup(id);
2482 if (line->id == NULL) {
2483 err = got_error_from_errno();
2484 goto done;
2486 line->annotated = 1;
2487 done:
2488 errcode = pthread_mutex_unlock(&tog_mutex);
2489 if (errcode)
2490 err = got_error_set_errno(errcode);
2491 return err;
2494 static void *
2495 blame_thread(void *arg)
2497 const struct got_error *err;
2498 struct tog_blame_thread_args *ta = arg;
2499 struct tog_blame_cb_args *a = ta->cb_args;
2500 int errcode;
2502 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2503 blame_cb, ta->cb_args);
2505 errcode = pthread_mutex_lock(&tog_mutex);
2506 if (errcode)
2507 return (void *)got_error_set_errno(errcode);
2509 got_repo_close(ta->repo);
2510 ta->repo = NULL;
2511 *ta->complete = 1;
2513 errcode = pthread_mutex_unlock(&tog_mutex);
2514 if (errcode && err == NULL)
2515 err = got_error_set_errno(errcode);
2517 return (void *)err;
2520 static struct got_object_id *
2521 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2522 int selected_line)
2524 struct tog_blame_line *line;
2526 line = &lines[first_displayed_line - 1 + selected_line - 1];
2527 if (!line->annotated)
2528 return NULL;
2530 return line->id;
2533 static const struct got_error *
2534 stop_blame(struct tog_blame *blame)
2536 const struct got_error *err = NULL;
2537 int i;
2539 if (blame->thread) {
2540 int errcode;
2541 errcode = pthread_mutex_unlock(&tog_mutex);
2542 if (errcode)
2543 return got_error_set_errno(errcode);
2544 errcode = pthread_join(blame->thread, (void **)&err);
2545 if (errcode)
2546 return got_error_set_errno(errcode);
2547 errcode = pthread_mutex_lock(&tog_mutex);
2548 if (errcode)
2549 return got_error_set_errno(errcode);
2550 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2551 err = NULL;
2552 blame->thread = NULL;
2554 if (blame->thread_args.repo) {
2555 got_repo_close(blame->thread_args.repo);
2556 blame->thread_args.repo = NULL;
2558 if (blame->f) {
2559 if (fclose(blame->f) != 0 && err == NULL)
2560 err = got_error_from_errno();
2561 blame->f = NULL;
2563 if (blame->lines) {
2564 for (i = 0; i < blame->nlines; i++)
2565 free(blame->lines[i].id);
2566 free(blame->lines);
2567 blame->lines = NULL;
2569 free(blame->cb_args.commit_id);
2570 blame->cb_args.commit_id = NULL;
2572 return err;
2575 static const struct got_error *
2576 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2577 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2578 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2579 struct got_repository *repo)
2581 const struct got_error *err = NULL;
2582 struct got_blob_object *blob = NULL;
2583 struct got_repository *thread_repo = NULL;
2584 struct got_object_id *obj_id = NULL;
2585 int obj_type;
2587 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2588 if (err)
2589 return err;
2590 if (obj_id == NULL)
2591 return got_error(GOT_ERR_NO_OBJ);
2593 err = got_object_get_type(&obj_type, repo, obj_id);
2594 if (err)
2595 goto done;
2597 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2598 err = got_error(GOT_ERR_OBJ_TYPE);
2599 goto done;
2602 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2603 if (err)
2604 goto done;
2605 blame->f = got_opentemp();
2606 if (blame->f == NULL) {
2607 err = got_error_from_errno();
2608 goto done;
2610 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2611 blame->f, blob);
2612 if (err)
2613 goto done;
2615 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2616 if (blame->lines == NULL) {
2617 err = got_error_from_errno();
2618 goto done;
2621 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2622 if (err)
2623 goto done;
2625 blame->cb_args.view = view;
2626 blame->cb_args.lines = blame->lines;
2627 blame->cb_args.nlines = blame->nlines;
2628 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2629 if (blame->cb_args.commit_id == NULL) {
2630 err = got_error_from_errno();
2631 goto done;
2633 blame->cb_args.quit = done;
2635 blame->thread_args.path = path;
2636 blame->thread_args.repo = thread_repo;
2637 blame->thread_args.cb_args = &blame->cb_args;
2638 blame->thread_args.complete = blame_complete;
2639 *blame_complete = 0;
2641 done:
2642 if (blob)
2643 got_object_blob_close(blob);
2644 free(obj_id);
2645 if (err)
2646 stop_blame(blame);
2647 return err;
2650 static const struct got_error *
2651 open_blame_view(struct tog_view *view, char *path,
2652 struct got_object_id *commit_id, struct got_repository *repo)
2654 const struct got_error *err = NULL;
2655 struct tog_blame_view_state *s = &view->state.blame;
2657 SIMPLEQ_INIT(&s->blamed_commits);
2659 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2660 if (err)
2661 return err;
2663 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2664 s->first_displayed_line = 1;
2665 s->last_displayed_line = view->nlines;
2666 s->selected_line = 1;
2667 s->blame_complete = 0;
2668 s->path = path;
2669 if (s->path == NULL)
2670 return got_error_from_errno();
2671 s->repo = repo;
2672 s->commit_id = commit_id;
2673 memset(&s->blame, 0, sizeof(s->blame));
2675 view->show = show_blame_view;
2676 view->input = input_blame_view;
2677 view->close = close_blame_view;
2679 return run_blame(&s->blame, view, &s->blame_complete,
2680 &s->first_displayed_line, &s->last_displayed_line,
2681 &s->selected_line, &s->done, &s->eof, s->path,
2682 s->blamed_commit->id, s->repo);
2685 static const struct got_error *
2686 close_blame_view(struct tog_view *view)
2688 const struct got_error *err = NULL;
2689 struct tog_blame_view_state *s = &view->state.blame;
2691 if (s->blame.thread)
2692 err = stop_blame(&s->blame);
2694 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2695 struct got_object_qid *blamed_commit;
2696 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2697 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2698 got_object_qid_free(blamed_commit);
2701 free(s->path);
2703 return err;
2706 static const struct got_error *
2707 show_blame_view(struct tog_view *view)
2709 const struct got_error *err = NULL;
2710 struct tog_blame_view_state *s = &view->state.blame;
2711 int errcode;
2713 if (s->blame.thread == NULL) {
2714 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2715 &s->blame.thread_args);
2716 if (errcode)
2717 return got_error_set_errno(errcode);
2720 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2721 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2722 s->selected_line, &s->first_displayed_line,
2723 &s->last_displayed_line, &s->eof, view->nlines);
2725 view_vborder(view);
2726 return err;
2729 static const struct got_error *
2730 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2731 struct tog_view **focus_view, struct tog_view *view, int ch)
2733 const struct got_error *err = NULL, *thread_err = NULL;
2734 struct tog_view *diff_view;
2735 struct tog_blame_view_state *s = &view->state.blame;
2736 int begin_x = 0;
2738 switch (ch) {
2739 case 'q':
2740 s->done = 1;
2741 break;
2742 case 'k':
2743 case KEY_UP:
2744 if (s->selected_line > 1)
2745 s->selected_line--;
2746 else if (s->selected_line == 1 &&
2747 s->first_displayed_line > 1)
2748 s->first_displayed_line--;
2749 break;
2750 case KEY_PPAGE:
2751 if (s->first_displayed_line == 1) {
2752 s->selected_line = 1;
2753 break;
2755 if (s->first_displayed_line > view->nlines - 2)
2756 s->first_displayed_line -=
2757 (view->nlines - 2);
2758 else
2759 s->first_displayed_line = 1;
2760 break;
2761 case 'j':
2762 case KEY_DOWN:
2763 if (s->selected_line < view->nlines - 2 &&
2764 s->first_displayed_line +
2765 s->selected_line <= s->blame.nlines)
2766 s->selected_line++;
2767 else if (s->last_displayed_line <
2768 s->blame.nlines)
2769 s->first_displayed_line++;
2770 break;
2771 case 'b':
2772 case 'p': {
2773 struct got_object_id *id = NULL;
2774 id = get_selected_commit_id(s->blame.lines,
2775 s->first_displayed_line, s->selected_line);
2776 if (id == NULL)
2777 break;
2778 if (ch == 'p') {
2779 struct got_commit_object *commit;
2780 struct got_object_qid *pid;
2781 struct got_object_id *blob_id = NULL;
2782 int obj_type;
2783 err = got_object_open_as_commit(&commit,
2784 s->repo, id);
2785 if (err)
2786 break;
2787 pid = SIMPLEQ_FIRST(
2788 got_object_commit_get_parent_ids(commit));
2789 if (pid == NULL) {
2790 got_object_commit_close(commit);
2791 break;
2793 /* Check if path history ends here. */
2794 err = got_object_id_by_path(&blob_id, s->repo,
2795 pid->id, s->path);
2796 if (err) {
2797 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2798 err = NULL;
2799 got_object_commit_close(commit);
2800 break;
2802 err = got_object_get_type(&obj_type, s->repo,
2803 blob_id);
2804 free(blob_id);
2805 /* Can't blame non-blob type objects. */
2806 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2807 got_object_commit_close(commit);
2808 break;
2810 err = got_object_qid_alloc(&s->blamed_commit,
2811 pid->id);
2812 got_object_commit_close(commit);
2813 } else {
2814 if (got_object_id_cmp(id,
2815 s->blamed_commit->id) == 0)
2816 break;
2817 err = got_object_qid_alloc(&s->blamed_commit,
2818 id);
2820 if (err)
2821 break;
2822 s->done = 1;
2823 thread_err = stop_blame(&s->blame);
2824 s->done = 0;
2825 if (thread_err)
2826 break;
2827 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2828 s->blamed_commit, entry);
2829 err = run_blame(&s->blame, view, &s->blame_complete,
2830 &s->first_displayed_line, &s->last_displayed_line,
2831 &s->selected_line, &s->done, &s->eof,
2832 s->path, s->blamed_commit->id, s->repo);
2833 if (err)
2834 break;
2835 break;
2837 case 'B': {
2838 struct got_object_qid *first;
2839 first = SIMPLEQ_FIRST(&s->blamed_commits);
2840 if (!got_object_id_cmp(first->id, s->commit_id))
2841 break;
2842 s->done = 1;
2843 thread_err = stop_blame(&s->blame);
2844 s->done = 0;
2845 if (thread_err)
2846 break;
2847 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2848 got_object_qid_free(s->blamed_commit);
2849 s->blamed_commit =
2850 SIMPLEQ_FIRST(&s->blamed_commits);
2851 err = run_blame(&s->blame, view, &s->blame_complete,
2852 &s->first_displayed_line, &s->last_displayed_line,
2853 &s->selected_line, &s->done, &s->eof, s->path,
2854 s->blamed_commit->id, s->repo);
2855 if (err)
2856 break;
2857 break;
2859 case KEY_ENTER:
2860 case '\r': {
2861 struct got_object_id *id = NULL;
2862 struct got_object_qid *pid;
2863 struct got_commit_object *commit = NULL;
2864 id = get_selected_commit_id(s->blame.lines,
2865 s->first_displayed_line, s->selected_line);
2866 if (id == NULL)
2867 break;
2868 err = got_object_open_as_commit(&commit, s->repo, id);
2869 if (err)
2870 break;
2871 pid = SIMPLEQ_FIRST(
2872 got_object_commit_get_parent_ids(commit));
2873 if (view_is_parent_view(view))
2874 begin_x = view_split_begin_x(view->begin_x);
2875 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2876 if (diff_view == NULL) {
2877 got_object_commit_close(commit);
2878 err = got_error_from_errno();
2879 break;
2881 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2882 id, NULL, s->repo);
2883 got_object_commit_close(commit);
2884 if (err) {
2885 view_close(diff_view);
2886 break;
2888 if (view_is_parent_view(view)) {
2889 err = view_close_child(view);
2890 if (err)
2891 break;
2892 err = view_set_child(view, diff_view);
2893 if (err) {
2894 view_close(diff_view);
2895 break;
2897 *focus_view = diff_view;
2898 view->child_focussed = 1;
2899 } else
2900 *new_view = diff_view;
2901 if (err)
2902 break;
2903 break;
2905 case KEY_NPAGE:
2906 case ' ':
2907 if (s->last_displayed_line >= s->blame.nlines &&
2908 s->selected_line < view->nlines - 2) {
2909 s->selected_line = MIN(s->blame.nlines,
2910 view->nlines - 2);
2911 break;
2913 if (s->last_displayed_line + view->nlines - 2
2914 <= s->blame.nlines)
2915 s->first_displayed_line +=
2916 view->nlines - 2;
2917 else
2918 s->first_displayed_line =
2919 s->blame.nlines -
2920 (view->nlines - 3);
2921 break;
2922 case KEY_RESIZE:
2923 if (s->selected_line > view->nlines - 2) {
2924 s->selected_line = MIN(s->blame.nlines,
2925 view->nlines - 2);
2927 break;
2928 default:
2929 break;
2931 return thread_err ? thread_err : err;
2934 static const struct got_error *
2935 cmd_blame(int argc, char *argv[])
2937 const struct got_error *error;
2938 struct got_repository *repo = NULL;
2939 struct got_worktree *worktree = NULL;
2940 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2941 struct got_object_id *commit_id = NULL;
2942 char *commit_id_str = NULL;
2943 int ch;
2944 struct tog_view *view;
2946 #ifndef PROFILE
2947 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2948 NULL) == -1)
2949 err(1, "pledge");
2950 #endif
2952 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2953 switch (ch) {
2954 case 'c':
2955 commit_id_str = optarg;
2956 break;
2957 case 'r':
2958 repo_path = realpath(optarg, NULL);
2959 if (repo_path == NULL)
2960 err(1, "-r option");
2961 break;
2962 default:
2963 usage();
2964 /* NOTREACHED */
2968 argc -= optind;
2969 argv += optind;
2971 if (argc == 1)
2972 path = argv[0];
2973 else
2974 usage_blame();
2976 cwd = getcwd(NULL, 0);
2977 if (cwd == NULL) {
2978 error = got_error_from_errno();
2979 goto done;
2981 if (repo_path == NULL) {
2982 error = got_worktree_open(&worktree, cwd);
2983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2984 goto done;
2985 else
2986 error = NULL;
2987 if (worktree) {
2988 repo_path =
2989 strdup(got_worktree_get_repo_path(worktree));
2990 if (repo_path == NULL)
2991 error = got_error_from_errno();
2992 if (error)
2993 goto done;
2994 } else {
2995 repo_path = strdup(cwd);
2996 if (repo_path == NULL) {
2997 error = got_error_from_errno();
2998 goto done;
3003 init_curses();
3005 error = apply_unveil(repo_path, NULL);
3006 if (error)
3007 goto done;
3009 error = got_repo_open(&repo, repo_path);
3010 if (error != NULL)
3011 goto done;
3013 if (worktree) {
3014 const char *prefix = got_worktree_get_path_prefix(worktree);
3015 char *p, *worktree_subdir = cwd +
3016 strlen(got_worktree_get_root_path(worktree));
3017 if (asprintf(&p, "%s%s%s%s%s",
3018 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3019 worktree_subdir, worktree_subdir[0] ? "/" : "",
3020 path) == -1) {
3021 error = got_error_from_errno();
3022 goto done;
3024 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3025 free(p);
3026 } else {
3027 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3029 if (error)
3030 goto done;
3032 if (commit_id_str == NULL) {
3033 struct got_reference *head_ref;
3034 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3035 if (error != NULL)
3036 goto done;
3037 error = got_ref_resolve(&commit_id, repo, head_ref);
3038 got_ref_close(head_ref);
3039 } else {
3040 error = got_object_resolve_id_str(&commit_id, repo,
3041 commit_id_str);
3043 if (error != NULL)
3044 goto done;
3046 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3047 if (view == NULL) {
3048 error = got_error_from_errno();
3049 goto done;
3051 error = open_blame_view(view, in_repo_path, commit_id, repo);
3052 if (error)
3053 goto done;
3054 error = view_loop(view);
3055 done:
3056 free(repo_path);
3057 free(cwd);
3058 free(commit_id);
3059 if (worktree)
3060 got_worktree_close(worktree);
3061 if (repo)
3062 got_repo_close(repo);
3063 return error;
3066 static const struct got_error *
3067 draw_tree_entries(struct tog_view *view,
3068 struct got_tree_entry **first_displayed_entry,
3069 struct got_tree_entry **last_displayed_entry,
3070 struct got_tree_entry **selected_entry, int *ndisplayed,
3071 const char *label, int show_ids, const char *parent_path,
3072 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3074 const struct got_error *err = NULL;
3075 struct got_tree_entry *te;
3076 wchar_t *wline;
3077 int width, n;
3079 *ndisplayed = 0;
3081 werase(view->window);
3083 if (limit == 0)
3084 return NULL;
3086 err = format_line(&wline, &width, label, view->ncols);
3087 if (err)
3088 return err;
3089 if (view_needs_focus_indication(view))
3090 wstandout(view->window);
3091 waddwstr(view->window, wline);
3092 if (view_needs_focus_indication(view))
3093 wstandend(view->window);
3094 free(wline);
3095 wline = NULL;
3096 if (width < view->ncols)
3097 waddch(view->window, '\n');
3098 if (--limit <= 0)
3099 return NULL;
3100 err = format_line(&wline, &width, parent_path, view->ncols);
3101 if (err)
3102 return err;
3103 waddwstr(view->window, wline);
3104 free(wline);
3105 wline = NULL;
3106 if (width < view->ncols)
3107 waddch(view->window, '\n');
3108 if (--limit <= 0)
3109 return NULL;
3110 waddch(view->window, '\n');
3111 if (--limit <= 0)
3112 return NULL;
3114 te = SIMPLEQ_FIRST(&entries->head);
3115 if (*first_displayed_entry == NULL) {
3116 if (selected == 0) {
3117 if (view->focussed)
3118 wstandout(view->window);
3119 *selected_entry = NULL;
3121 waddstr(view->window, " ..\n"); /* parent directory */
3122 if (selected == 0 && view->focussed)
3123 wstandend(view->window);
3124 (*ndisplayed)++;
3125 if (--limit <= 0)
3126 return NULL;
3127 n = 1;
3128 } else {
3129 n = 0;
3130 while (te != *first_displayed_entry)
3131 te = SIMPLEQ_NEXT(te, entry);
3134 while (te) {
3135 char *line = NULL, *id_str = NULL;
3137 if (show_ids) {
3138 err = got_object_id_str(&id_str, te->id);
3139 if (err)
3140 return got_error_from_errno();
3142 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3143 te->name, S_ISDIR(te->mode) ? "/" :
3144 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3145 free(id_str);
3146 return got_error_from_errno();
3148 free(id_str);
3149 err = format_line(&wline, &width, line, view->ncols);
3150 if (err) {
3151 free(line);
3152 break;
3154 if (n == selected) {
3155 if (view->focussed)
3156 wstandout(view->window);
3157 *selected_entry = te;
3159 waddwstr(view->window, wline);
3160 if (width < view->ncols)
3161 waddch(view->window, '\n');
3162 if (n == selected && view->focussed)
3163 wstandend(view->window);
3164 free(line);
3165 free(wline);
3166 wline = NULL;
3167 n++;
3168 (*ndisplayed)++;
3169 *last_displayed_entry = te;
3170 if (--limit <= 0)
3171 break;
3172 te = SIMPLEQ_NEXT(te, entry);
3175 return err;
3178 static void
3179 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3180 const struct got_tree_entries *entries, int isroot)
3182 struct got_tree_entry *te, *prev;
3183 int i;
3185 if (*first_displayed_entry == NULL)
3186 return;
3188 te = SIMPLEQ_FIRST(&entries->head);
3189 if (*first_displayed_entry == te) {
3190 if (!isroot)
3191 *first_displayed_entry = NULL;
3192 return;
3195 /* XXX this is stupid... switch to TAILQ? */
3196 for (i = 0; i < maxscroll; i++) {
3197 while (te != *first_displayed_entry) {
3198 prev = te;
3199 te = SIMPLEQ_NEXT(te, entry);
3201 *first_displayed_entry = prev;
3202 te = SIMPLEQ_FIRST(&entries->head);
3204 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3205 *first_displayed_entry = NULL;
3208 static int
3209 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3210 struct got_tree_entry *last_displayed_entry,
3211 const struct got_tree_entries *entries)
3213 struct got_tree_entry *next, *last;
3214 int n = 0;
3216 if (*first_displayed_entry)
3217 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3218 else
3219 next = SIMPLEQ_FIRST(&entries->head);
3220 last = last_displayed_entry;
3221 while (next && last && n++ < maxscroll) {
3222 last = SIMPLEQ_NEXT(last, entry);
3223 if (last) {
3224 *first_displayed_entry = next;
3225 next = SIMPLEQ_NEXT(next, entry);
3228 return n;
3231 static const struct got_error *
3232 tree_entry_path(char **path, struct tog_parent_trees *parents,
3233 struct got_tree_entry *te)
3235 const struct got_error *err = NULL;
3236 struct tog_parent_tree *pt;
3237 size_t len = 2; /* for leading slash and NUL */
3239 TAILQ_FOREACH(pt, parents, entry)
3240 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3241 if (te)
3242 len += strlen(te->name);
3244 *path = calloc(1, len);
3245 if (path == NULL)
3246 return got_error_from_errno();
3248 (*path)[0] = '/';
3249 pt = TAILQ_LAST(parents, tog_parent_trees);
3250 while (pt) {
3251 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3252 err = got_error(GOT_ERR_NO_SPACE);
3253 goto done;
3255 if (strlcat(*path, "/", len) >= len) {
3256 err = got_error(GOT_ERR_NO_SPACE);
3257 goto done;
3259 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3261 if (te) {
3262 if (strlcat(*path, te->name, len) >= len) {
3263 err = got_error(GOT_ERR_NO_SPACE);
3264 goto done;
3267 done:
3268 if (err) {
3269 free(*path);
3270 *path = NULL;
3272 return err;
3275 static const struct got_error *
3276 blame_tree_entry(struct tog_view **new_view, int begin_x,
3277 struct got_tree_entry *te, struct tog_parent_trees *parents,
3278 struct got_object_id *commit_id, struct got_repository *repo)
3280 const struct got_error *err = NULL;
3281 char *path;
3282 struct tog_view *blame_view;
3284 err = tree_entry_path(&path, parents, te);
3285 if (err)
3286 return err;
3288 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3289 if (blame_view == NULL)
3290 return got_error_from_errno();
3292 err = open_blame_view(blame_view, path, commit_id, repo);
3293 if (err) {
3294 view_close(blame_view);
3295 free(path);
3296 } else
3297 *new_view = blame_view;
3298 return err;
3301 static const struct got_error *
3302 log_tree_entry(struct tog_view **new_view, int begin_x,
3303 struct got_tree_entry *te, struct tog_parent_trees *parents,
3304 struct got_object_id *commit_id, struct got_repository *repo)
3306 struct tog_view *log_view;
3307 const struct got_error *err = NULL;
3308 char *path;
3310 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3311 if (log_view == NULL)
3312 return got_error_from_errno();
3314 err = tree_entry_path(&path, parents, te);
3315 if (err)
3316 return err;
3318 err = open_log_view(log_view, commit_id, repo, path, 0);
3319 if (err)
3320 view_close(log_view);
3321 else
3322 *new_view = log_view;
3323 free(path);
3324 return err;
3327 static const struct got_error *
3328 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3329 struct got_object_id *commit_id, struct got_repository *repo)
3331 const struct got_error *err = NULL;
3332 char *commit_id_str = NULL;
3333 struct tog_tree_view_state *s = &view->state.tree;
3335 TAILQ_INIT(&s->parents);
3337 err = got_object_id_str(&commit_id_str, commit_id);
3338 if (err != NULL)
3339 goto done;
3341 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3342 err = got_error_from_errno();
3343 goto done;
3346 s->root = s->tree = root;
3347 s->entries = got_object_tree_get_entries(root);
3348 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3349 s->commit_id = got_object_id_dup(commit_id);
3350 if (s->commit_id == NULL) {
3351 err = got_error_from_errno();
3352 goto done;
3354 s->repo = repo;
3356 view->show = show_tree_view;
3357 view->input = input_tree_view;
3358 view->close = close_tree_view;
3359 done:
3360 free(commit_id_str);
3361 if (err) {
3362 free(s->tree_label);
3363 s->tree_label = NULL;
3365 return err;
3368 static const struct got_error *
3369 close_tree_view(struct tog_view *view)
3371 struct tog_tree_view_state *s = &view->state.tree;
3373 free(s->tree_label);
3374 s->tree_label = NULL;
3375 free(s->commit_id);
3376 s->commit_id = NULL;
3377 while (!TAILQ_EMPTY(&s->parents)) {
3378 struct tog_parent_tree *parent;
3379 parent = TAILQ_FIRST(&s->parents);
3380 TAILQ_REMOVE(&s->parents, parent, entry);
3381 free(parent);
3384 if (s->tree != s->root)
3385 got_object_tree_close(s->tree);
3386 got_object_tree_close(s->root);
3388 return NULL;
3391 static const struct got_error *
3392 show_tree_view(struct tog_view *view)
3394 const struct got_error *err = NULL;
3395 struct tog_tree_view_state *s = &view->state.tree;
3396 char *parent_path;
3398 err = tree_entry_path(&parent_path, &s->parents, NULL);
3399 if (err)
3400 return err;
3402 err = draw_tree_entries(view, &s->first_displayed_entry,
3403 &s->last_displayed_entry, &s->selected_entry,
3404 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3405 s->entries, s->selected, view->nlines, s->tree == s->root);
3406 free(parent_path);
3408 view_vborder(view);
3409 return err;
3412 static const struct got_error *
3413 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3414 struct tog_view **focus_view, struct tog_view *view, int ch)
3416 const struct got_error *err = NULL;
3417 struct tog_tree_view_state *s = &view->state.tree;
3418 struct tog_view *log_view;
3419 int begin_x = 0, nscrolled;
3421 switch (ch) {
3422 case 'i':
3423 s->show_ids = !s->show_ids;
3424 break;
3425 case 'l':
3426 if (!s->selected_entry)
3427 break;
3428 if (view_is_parent_view(view))
3429 begin_x = view_split_begin_x(view->begin_x);
3430 err = log_tree_entry(&log_view, begin_x,
3431 s->selected_entry, &s->parents,
3432 s->commit_id, s->repo);
3433 if (view_is_parent_view(view)) {
3434 err = view_close_child(view);
3435 if (err)
3436 return err;
3437 err = view_set_child(view, log_view);
3438 if (err) {
3439 view_close(log_view);
3440 break;
3442 *focus_view = log_view;
3443 view->child_focussed = 1;
3444 } else
3445 *new_view = log_view;
3446 break;
3447 case 'k':
3448 case KEY_UP:
3449 if (s->selected > 0) {
3450 s->selected--;
3451 if (s->selected == 0)
3452 break;
3454 if (s->selected > 0)
3455 break;
3456 tree_scroll_up(&s->first_displayed_entry, 1,
3457 s->entries, s->tree == s->root);
3458 break;
3459 case KEY_PPAGE:
3460 tree_scroll_up(&s->first_displayed_entry,
3461 MAX(0, view->nlines - 4 - s->selected), s->entries,
3462 s->tree == s->root);
3463 s->selected = 0;
3464 if (SIMPLEQ_FIRST(&s->entries->head) ==
3465 s->first_displayed_entry && s->tree != s->root)
3466 s->first_displayed_entry = NULL;
3467 break;
3468 case 'j':
3469 case KEY_DOWN:
3470 if (s->selected < s->ndisplayed - 1) {
3471 s->selected++;
3472 break;
3474 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3475 == NULL) {
3476 /* can't scroll any further */
3477 break;
3479 tree_scroll_down(&s->first_displayed_entry, 1,
3480 s->last_displayed_entry, s->entries);
3481 break;
3482 case KEY_NPAGE:
3483 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3484 == NULL) {
3485 /* can't scroll any further; move cursor down */
3486 if (s->selected < s->ndisplayed - 1)
3487 s->selected = s->ndisplayed - 1;
3488 break;
3490 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3491 view->nlines, s->last_displayed_entry, s->entries);
3492 if (nscrolled < view->nlines) {
3493 int ndisplayed = 0;
3494 struct got_tree_entry *te;
3495 te = s->first_displayed_entry;
3496 do {
3497 ndisplayed++;
3498 te = SIMPLEQ_NEXT(te, entry);
3499 } while (te);
3500 s->selected = ndisplayed - 1;
3502 break;
3503 case KEY_ENTER:
3504 case '\r':
3505 if (s->selected_entry == NULL) {
3506 struct tog_parent_tree *parent;
3507 case KEY_BACKSPACE:
3508 /* user selected '..' */
3509 if (s->tree == s->root)
3510 break;
3511 parent = TAILQ_FIRST(&s->parents);
3512 TAILQ_REMOVE(&s->parents, parent,
3513 entry);
3514 got_object_tree_close(s->tree);
3515 s->tree = parent->tree;
3516 s->entries =
3517 got_object_tree_get_entries(s->tree);
3518 s->first_displayed_entry =
3519 parent->first_displayed_entry;
3520 s->selected_entry =
3521 parent->selected_entry;
3522 s->selected = parent->selected;
3523 free(parent);
3524 } else if (S_ISDIR(s->selected_entry->mode)) {
3525 struct tog_parent_tree *parent;
3526 struct got_tree_object *child;
3527 err = got_object_open_as_tree(&child,
3528 s->repo, s->selected_entry->id);
3529 if (err)
3530 break;
3531 parent = calloc(1, sizeof(*parent));
3532 if (parent == NULL) {
3533 err = got_error_from_errno();
3534 break;
3536 parent->tree = s->tree;
3537 parent->first_displayed_entry =
3538 s->first_displayed_entry;
3539 parent->selected_entry = s->selected_entry;
3540 parent->selected = s->selected;
3541 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3542 s->tree = child;
3543 s->entries =
3544 got_object_tree_get_entries(s->tree);
3545 s->selected = 0;
3546 s->first_displayed_entry = NULL;
3547 } else if (S_ISREG(s->selected_entry->mode)) {
3548 struct tog_view *blame_view;
3549 int begin_x = view_is_parent_view(view) ?
3550 view_split_begin_x(view->begin_x) : 0;
3552 err = blame_tree_entry(&blame_view, begin_x,
3553 s->selected_entry, &s->parents, s->commit_id,
3554 s->repo);
3555 if (err)
3556 break;
3557 if (view_is_parent_view(view)) {
3558 err = view_close_child(view);
3559 if (err)
3560 return err;
3561 err = view_set_child(view, blame_view);
3562 if (err) {
3563 view_close(blame_view);
3564 break;
3566 *focus_view = blame_view;
3567 view->child_focussed = 1;
3568 } else
3569 *new_view = blame_view;
3571 break;
3572 case KEY_RESIZE:
3573 if (s->selected > view->nlines)
3574 s->selected = s->ndisplayed - 1;
3575 break;
3576 default:
3577 break;
3580 return err;
3583 __dead static void
3584 usage_tree(void)
3586 endwin();
3587 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3588 getprogname());
3589 exit(1);
3592 static const struct got_error *
3593 cmd_tree(int argc, char *argv[])
3595 const struct got_error *error;
3596 struct got_repository *repo = NULL;
3597 char *repo_path = NULL;
3598 struct got_object_id *commit_id = NULL;
3599 char *commit_id_arg = NULL;
3600 struct got_commit_object *commit = NULL;
3601 struct got_tree_object *tree = NULL;
3602 int ch;
3603 struct tog_view *view;
3605 #ifndef PROFILE
3606 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3607 NULL) == -1)
3608 err(1, "pledge");
3609 #endif
3611 while ((ch = getopt(argc, argv, "c:")) != -1) {
3612 switch (ch) {
3613 case 'c':
3614 commit_id_arg = optarg;
3615 break;
3616 default:
3617 usage();
3618 /* NOTREACHED */
3622 argc -= optind;
3623 argv += optind;
3625 if (argc == 0) {
3626 struct got_worktree *worktree;
3627 char *cwd = getcwd(NULL, 0);
3628 if (cwd == NULL)
3629 return got_error_from_errno();
3630 error = got_worktree_open(&worktree, cwd);
3631 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3632 goto done;
3633 if (worktree) {
3634 free(cwd);
3635 repo_path =
3636 strdup(got_worktree_get_repo_path(worktree));
3637 got_worktree_close(worktree);
3638 } else
3639 repo_path = cwd;
3640 if (repo_path == NULL) {
3641 error = got_error_from_errno();
3642 goto done;
3644 } else if (argc == 1) {
3645 repo_path = realpath(argv[0], NULL);
3646 if (repo_path == NULL)
3647 return got_error_from_errno();
3648 } else
3649 usage_log();
3651 init_curses();
3653 error = apply_unveil(repo_path, NULL);
3654 if (error)
3655 goto done;
3657 error = got_repo_open(&repo, repo_path);
3658 if (error != NULL)
3659 goto done;
3661 if (commit_id_arg == NULL)
3662 error = get_head_commit_id(&commit_id, repo);
3663 else
3664 error = got_object_resolve_id_str(&commit_id, repo,
3665 commit_id_arg);
3666 if (error != NULL)
3667 goto done;
3669 error = got_object_open_as_commit(&commit, repo, commit_id);
3670 if (error != NULL)
3671 goto done;
3673 error = got_object_open_as_tree(&tree, repo,
3674 got_object_commit_get_tree_id(commit));
3675 if (error != NULL)
3676 goto done;
3678 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3679 if (view == NULL) {
3680 error = got_error_from_errno();
3681 goto done;
3683 error = open_tree_view(view, tree, commit_id, repo);
3684 if (error)
3685 goto done;
3686 error = view_loop(view);
3687 done:
3688 free(repo_path);
3689 free(commit_id);
3690 if (commit)
3691 got_object_commit_close(commit);
3692 if (tree)
3693 got_object_tree_close(tree);
3694 if (repo)
3695 got_repo_close(repo);
3696 return error;
3699 __dead static void
3700 usage(void)
3702 int i;
3704 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3705 "Available commands:\n", getprogname());
3706 for (i = 0; i < nitems(tog_commands); i++) {
3707 struct tog_cmd *cmd = &tog_commands[i];
3708 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3710 exit(1);
3713 static char **
3714 make_argv(const char *arg0, const char *arg1)
3716 char **argv;
3717 int argc = (arg1 == NULL ? 1 : 2);
3719 argv = calloc(argc, sizeof(char *));
3720 if (argv == NULL)
3721 err(1, "calloc");
3722 argv[0] = strdup(arg0);
3723 if (argv[0] == NULL)
3724 err(1, "calloc");
3725 if (arg1) {
3726 argv[1] = strdup(arg1);
3727 if (argv[1] == NULL)
3728 err(1, "calloc");
3731 return argv;
3734 int
3735 main(int argc, char *argv[])
3737 const struct got_error *error = NULL;
3738 struct tog_cmd *cmd = NULL;
3739 int ch, hflag = 0;
3740 char **cmd_argv = NULL;
3742 setlocale(LC_CTYPE, "");
3744 while ((ch = getopt(argc, argv, "h")) != -1) {
3745 switch (ch) {
3746 case 'h':
3747 hflag = 1;
3748 break;
3749 default:
3750 usage();
3751 /* NOTREACHED */
3755 argc -= optind;
3756 argv += optind;
3757 optind = 0;
3758 optreset = 1;
3760 if (argc == 0) {
3761 if (hflag)
3762 usage();
3763 /* Build an argument vector which runs a default command. */
3764 cmd = &tog_commands[0];
3765 cmd_argv = make_argv(cmd->name, NULL);
3766 argc = 1;
3767 } else {
3768 int i;
3770 /* Did the user specific a command? */
3771 for (i = 0; i < nitems(tog_commands); i++) {
3772 if (strncmp(tog_commands[i].name, argv[0],
3773 strlen(argv[0])) == 0) {
3774 cmd = &tog_commands[i];
3775 if (hflag)
3776 tog_commands[i].cmd_usage();
3777 break;
3780 if (cmd == NULL) {
3781 /* Did the user specify a repository? */
3782 char *repo_path = realpath(argv[0], NULL);
3783 if (repo_path) {
3784 struct got_repository *repo;
3785 error = got_repo_open(&repo, repo_path);
3786 if (error == NULL)
3787 got_repo_close(repo);
3788 } else
3789 error = got_error_from_errno();
3790 if (error) {
3791 if (hflag) {
3792 fprintf(stderr, "%s: '%s' is not a "
3793 "known command\n", getprogname(),
3794 argv[0]);
3795 usage();
3797 fprintf(stderr, "%s: '%s' is neither a known "
3798 "command nor a path to a repository\n",
3799 getprogname(), argv[0]);
3800 free(repo_path);
3801 return 1;
3803 cmd = &tog_commands[0];
3804 cmd_argv = make_argv(cmd->name, repo_path);
3805 argc = 2;
3806 free(repo_path);
3810 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3811 if (error)
3812 goto done;
3813 done:
3814 endwin();
3815 free(cmd_argv);
3816 if (error)
3817 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3818 return 0;