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 if (pentry == NULL) {
1208 *commits_needed = maxscroll + 20;
1209 while (*commits_needed > 0) {
1210 int errcode;
1211 errcode = pthread_cond_signal(need_commits);
1212 if (errcode)
1213 return got_error_set_errno(errcode);
1214 errcode = pthread_mutex_unlock(&tog_mutex);
1215 if (errcode)
1216 return got_error_set_errno(errcode);
1217 pthread_yield();
1218 errcode = pthread_mutex_lock(&tog_mutex);
1219 if (errcode)
1220 return got_error_set_errno(errcode);
1223 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 const struct got_error *err;
2105 const struct got_object_id_queue *parent_ids;
2106 struct got_commit_object *selected_commit;
2107 struct got_object_qid *pid;
2109 free(s->id2);
2110 s->id2 = got_object_id_dup(entry->id);
2111 if (s->id2 == NULL)
2112 return got_error_from_errno();
2114 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2115 if (err)
2116 return err;
2117 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2118 free(s->id1);
2119 pid = SIMPLEQ_FIRST(parent_ids);
2120 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2122 return NULL;
2125 static const struct got_error *
2126 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2127 struct tog_view **focus_view, struct tog_view *view, int ch)
2129 const struct got_error *err = NULL;
2130 struct tog_diff_view_state *s = &view->state.diff;
2131 struct tog_log_view_state *ls;
2132 struct commit_queue_entry *entry;
2133 int i;
2135 switch (ch) {
2136 case 'k':
2137 case KEY_UP:
2138 if (s->first_displayed_line > 1)
2139 s->first_displayed_line--;
2140 break;
2141 case KEY_PPAGE:
2142 i = 0;
2143 while (i++ < view->nlines - 1 &&
2144 s->first_displayed_line > 1)
2145 s->first_displayed_line--;
2146 break;
2147 case 'j':
2148 case KEY_DOWN:
2149 if (!s->eof)
2150 s->first_displayed_line++;
2151 break;
2152 case KEY_NPAGE:
2153 case ' ':
2154 i = 0;
2155 while (!s->eof && i++ < view->nlines - 1) {
2156 char *line;
2157 line = parse_next_line(s->f, NULL);
2158 s->first_displayed_line++;
2159 if (line == NULL)
2160 break;
2162 break;
2163 case '[':
2164 if (s->diff_context > 0) {
2165 s->diff_context--;
2166 err = create_diff(s);
2168 break;
2169 case ']':
2170 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2171 s->diff_context++;
2172 err = create_diff(s);
2174 break;
2175 case '<':
2176 case ',':
2177 if (s->log_view == NULL)
2178 break;
2179 ls = &s->log_view->state.log;
2180 entry = TAILQ_PREV(ls->selected_entry,
2181 commit_queue_head, entry);
2182 if (entry == NULL)
2183 break;
2185 err = input_log_view(NULL, NULL, NULL, s->log_view,
2186 KEY_UP);
2187 if (err)
2188 break;
2190 err = set_selected_commit(s, entry);
2191 if (err)
2192 break;
2194 s->first_displayed_line = 1;
2195 s->last_displayed_line = view->nlines;
2197 err = create_diff(s);
2198 break;
2199 case '>':
2200 case '.':
2201 if (s->log_view == NULL)
2202 break;
2203 ls = &s->log_view->state.log;
2204 err = input_log_view(NULL, NULL, NULL, s->log_view,
2205 KEY_DOWN);
2206 if (err)
2207 break;
2209 /* Hack: Ensure two commits get loaded. */
2210 if (!ls->thread_args.log_complete) {
2211 err = input_log_view(NULL, NULL, NULL,
2212 s->log_view, KEY_DOWN);
2213 if (err)
2214 break;
2215 err = input_log_view(NULL, NULL, NULL,
2216 s->log_view, KEY_UP);
2217 if (err)
2218 break;
2221 entry = TAILQ_NEXT(ls->selected_entry, entry);
2222 if (entry == NULL)
2223 break;
2225 err = set_selected_commit(s, entry);
2226 if (err)
2227 break;
2229 s->first_displayed_line = 1;
2230 s->last_displayed_line = view->nlines;
2232 err = create_diff(s);
2233 break;
2234 default:
2235 break;
2238 return err;
2241 static const struct got_error *
2242 cmd_diff(int argc, char *argv[])
2244 const struct got_error *error = NULL;
2245 struct got_repository *repo = NULL;
2246 struct got_object_id *id1 = NULL, *id2 = NULL;
2247 char *repo_path = NULL;
2248 char *id_str1 = NULL, *id_str2 = NULL;
2249 int ch;
2250 struct tog_view *view;
2252 #ifndef PROFILE
2253 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2254 NULL) == -1)
2255 err(1, "pledge");
2256 #endif
2258 while ((ch = getopt(argc, argv, "")) != -1) {
2259 switch (ch) {
2260 default:
2261 usage();
2262 /* NOTREACHED */
2266 argc -= optind;
2267 argv += optind;
2269 if (argc == 0) {
2270 usage_diff(); /* TODO show local worktree changes */
2271 } else if (argc == 2) {
2272 repo_path = getcwd(NULL, 0);
2273 if (repo_path == NULL)
2274 return got_error_from_errno();
2275 id_str1 = argv[0];
2276 id_str2 = argv[1];
2277 } else if (argc == 3) {
2278 repo_path = realpath(argv[0], NULL);
2279 if (repo_path == NULL)
2280 return got_error_from_errno();
2281 id_str1 = argv[1];
2282 id_str2 = argv[2];
2283 } else
2284 usage_diff();
2286 init_curses();
2288 error = apply_unveil(repo_path, NULL);
2289 if (error)
2290 goto done;
2292 error = got_repo_open(&repo, repo_path);
2293 free(repo_path);
2294 if (error)
2295 goto done;
2297 error = got_object_resolve_id_str(&id1, repo, id_str1);
2298 if (error)
2299 goto done;
2301 error = got_object_resolve_id_str(&id2, repo, id_str2);
2302 if (error)
2303 goto done;
2305 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2306 if (view == NULL) {
2307 error = got_error_from_errno();
2308 goto done;
2310 error = open_diff_view(view, id1, id2, NULL, repo);
2311 if (error)
2312 goto done;
2313 error = view_loop(view);
2314 done:
2315 got_repo_close(repo);
2316 return error;
2319 __dead static void
2320 usage_blame(void)
2322 endwin();
2323 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2324 getprogname());
2325 exit(1);
2328 struct tog_blame_line {
2329 int annotated;
2330 struct got_object_id *id;
2333 static const struct got_error *
2334 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2335 const char *path, struct tog_blame_line *lines, int nlines,
2336 int blame_complete, int selected_line, int *first_displayed_line,
2337 int *last_displayed_line, int *eof, int max_lines)
2339 const struct got_error *err;
2340 int lineno = 0, nprinted = 0;
2341 char *line;
2342 size_t len;
2343 wchar_t *wline;
2344 int width, wlimit;
2345 struct tog_blame_line *blame_line;
2346 struct got_object_id *prev_id = NULL;
2347 char *id_str;
2349 err = got_object_id_str(&id_str, id);
2350 if (err)
2351 return err;
2353 rewind(f);
2354 werase(view->window);
2356 if (asprintf(&line, "commit %s", id_str) == -1) {
2357 err = got_error_from_errno();
2358 free(id_str);
2359 return err;
2362 err = format_line(&wline, &width, line, view->ncols);
2363 free(line);
2364 line = NULL;
2365 if (view_needs_focus_indication(view))
2366 wstandout(view->window);
2367 waddwstr(view->window, wline);
2368 if (view_needs_focus_indication(view))
2369 wstandend(view->window);
2370 free(wline);
2371 wline = NULL;
2372 if (width < view->ncols)
2373 waddch(view->window, '\n');
2375 if (asprintf(&line, "[%d/%d] %s%s",
2376 *first_displayed_line - 1 + selected_line, nlines,
2377 blame_complete ? "" : "annotating ", path) == -1) {
2378 free(id_str);
2379 return got_error_from_errno();
2381 free(id_str);
2382 err = format_line(&wline, &width, line, view->ncols);
2383 free(line);
2384 line = NULL;
2385 if (err)
2386 return err;
2387 waddwstr(view->window, wline);
2388 free(wline);
2389 wline = NULL;
2390 if (width < view->ncols)
2391 waddch(view->window, '\n');
2393 *eof = 0;
2394 while (nprinted < max_lines - 2) {
2395 line = parse_next_line(f, &len);
2396 if (line == NULL) {
2397 *eof = 1;
2398 break;
2400 if (++lineno < *first_displayed_line) {
2401 free(line);
2402 continue;
2405 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2406 err = format_line(&wline, &width, line, wlimit);
2407 if (err) {
2408 free(line);
2409 return err;
2412 if (view->focussed && nprinted == selected_line - 1)
2413 wstandout(view->window);
2415 blame_line = &lines[lineno - 1];
2416 if (blame_line->annotated && prev_id &&
2417 got_object_id_cmp(prev_id, blame_line->id) == 0)
2418 waddstr(view->window, " ");
2419 else if (blame_line->annotated) {
2420 char *id_str;
2421 err = got_object_id_str(&id_str, blame_line->id);
2422 if (err) {
2423 free(line);
2424 free(wline);
2425 return err;
2427 wprintw(view->window, "%.8s ", id_str);
2428 free(id_str);
2429 prev_id = blame_line->id;
2430 } else {
2431 waddstr(view->window, "........ ");
2432 prev_id = NULL;
2435 waddwstr(view->window, wline);
2436 while (width < wlimit) {
2437 waddch(view->window, ' ');
2438 width++;
2440 if (view->focussed && nprinted == selected_line - 1)
2441 wstandend(view->window);
2442 if (++nprinted == 1)
2443 *first_displayed_line = lineno;
2444 free(line);
2445 free(wline);
2446 wline = NULL;
2448 *last_displayed_line = lineno;
2450 view_vborder(view);
2452 return NULL;
2455 static const struct got_error *
2456 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2458 const struct got_error *err = NULL;
2459 struct tog_blame_cb_args *a = arg;
2460 struct tog_blame_line *line;
2461 int errcode;
2463 if (nlines != a->nlines ||
2464 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2465 return got_error(GOT_ERR_RANGE);
2467 errcode = pthread_mutex_lock(&tog_mutex);
2468 if (errcode)
2469 return got_error_set_errno(errcode);
2471 if (*a->quit) { /* user has quit the blame view */
2472 err = got_error(GOT_ERR_ITER_COMPLETED);
2473 goto done;
2476 if (lineno == -1)
2477 goto done; /* no change in this commit */
2479 line = &a->lines[lineno - 1];
2480 if (line->annotated)
2481 goto done;
2483 line->id = got_object_id_dup(id);
2484 if (line->id == NULL) {
2485 err = got_error_from_errno();
2486 goto done;
2488 line->annotated = 1;
2489 done:
2490 errcode = pthread_mutex_unlock(&tog_mutex);
2491 if (errcode)
2492 err = got_error_set_errno(errcode);
2493 return err;
2496 static void *
2497 blame_thread(void *arg)
2499 const struct got_error *err;
2500 struct tog_blame_thread_args *ta = arg;
2501 struct tog_blame_cb_args *a = ta->cb_args;
2502 int errcode;
2504 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2505 blame_cb, ta->cb_args);
2507 errcode = pthread_mutex_lock(&tog_mutex);
2508 if (errcode)
2509 return (void *)got_error_set_errno(errcode);
2511 got_repo_close(ta->repo);
2512 ta->repo = NULL;
2513 *ta->complete = 1;
2515 errcode = pthread_mutex_unlock(&tog_mutex);
2516 if (errcode && err == NULL)
2517 err = got_error_set_errno(errcode);
2519 return (void *)err;
2522 static struct got_object_id *
2523 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2524 int selected_line)
2526 struct tog_blame_line *line;
2528 line = &lines[first_displayed_line - 1 + selected_line - 1];
2529 if (!line->annotated)
2530 return NULL;
2532 return line->id;
2535 static const struct got_error *
2536 stop_blame(struct tog_blame *blame)
2538 const struct got_error *err = NULL;
2539 int i;
2541 if (blame->thread) {
2542 int errcode;
2543 errcode = pthread_mutex_unlock(&tog_mutex);
2544 if (errcode)
2545 return got_error_set_errno(errcode);
2546 errcode = pthread_join(blame->thread, (void **)&err);
2547 if (errcode)
2548 return got_error_set_errno(errcode);
2549 errcode = pthread_mutex_lock(&tog_mutex);
2550 if (errcode)
2551 return got_error_set_errno(errcode);
2552 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2553 err = NULL;
2554 blame->thread = NULL;
2556 if (blame->thread_args.repo) {
2557 got_repo_close(blame->thread_args.repo);
2558 blame->thread_args.repo = NULL;
2560 if (blame->f) {
2561 if (fclose(blame->f) != 0 && err == NULL)
2562 err = got_error_from_errno();
2563 blame->f = NULL;
2565 if (blame->lines) {
2566 for (i = 0; i < blame->nlines; i++)
2567 free(blame->lines[i].id);
2568 free(blame->lines);
2569 blame->lines = NULL;
2571 free(blame->cb_args.commit_id);
2572 blame->cb_args.commit_id = NULL;
2574 return err;
2577 static const struct got_error *
2578 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2579 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2580 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2581 struct got_repository *repo)
2583 const struct got_error *err = NULL;
2584 struct got_blob_object *blob = NULL;
2585 struct got_repository *thread_repo = NULL;
2586 struct got_object_id *obj_id = NULL;
2587 int obj_type;
2589 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2590 if (err)
2591 return err;
2592 if (obj_id == NULL)
2593 return got_error(GOT_ERR_NO_OBJ);
2595 err = got_object_get_type(&obj_type, repo, obj_id);
2596 if (err)
2597 goto done;
2599 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2600 err = got_error(GOT_ERR_OBJ_TYPE);
2601 goto done;
2604 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2605 if (err)
2606 goto done;
2607 blame->f = got_opentemp();
2608 if (blame->f == NULL) {
2609 err = got_error_from_errno();
2610 goto done;
2612 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2613 blame->f, blob);
2614 if (err)
2615 goto done;
2617 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2618 if (blame->lines == NULL) {
2619 err = got_error_from_errno();
2620 goto done;
2623 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2624 if (err)
2625 goto done;
2627 blame->cb_args.view = view;
2628 blame->cb_args.lines = blame->lines;
2629 blame->cb_args.nlines = blame->nlines;
2630 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2631 if (blame->cb_args.commit_id == NULL) {
2632 err = got_error_from_errno();
2633 goto done;
2635 blame->cb_args.quit = done;
2637 blame->thread_args.path = path;
2638 blame->thread_args.repo = thread_repo;
2639 blame->thread_args.cb_args = &blame->cb_args;
2640 blame->thread_args.complete = blame_complete;
2641 *blame_complete = 0;
2643 done:
2644 if (blob)
2645 got_object_blob_close(blob);
2646 free(obj_id);
2647 if (err)
2648 stop_blame(blame);
2649 return err;
2652 static const struct got_error *
2653 open_blame_view(struct tog_view *view, char *path,
2654 struct got_object_id *commit_id, struct got_repository *repo)
2656 const struct got_error *err = NULL;
2657 struct tog_blame_view_state *s = &view->state.blame;
2659 SIMPLEQ_INIT(&s->blamed_commits);
2661 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2662 if (err)
2663 return err;
2665 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2666 s->first_displayed_line = 1;
2667 s->last_displayed_line = view->nlines;
2668 s->selected_line = 1;
2669 s->blame_complete = 0;
2670 s->path = path;
2671 if (s->path == NULL)
2672 return got_error_from_errno();
2673 s->repo = repo;
2674 s->commit_id = commit_id;
2675 memset(&s->blame, 0, sizeof(s->blame));
2677 view->show = show_blame_view;
2678 view->input = input_blame_view;
2679 view->close = close_blame_view;
2681 return run_blame(&s->blame, view, &s->blame_complete,
2682 &s->first_displayed_line, &s->last_displayed_line,
2683 &s->selected_line, &s->done, &s->eof, s->path,
2684 s->blamed_commit->id, s->repo);
2687 static const struct got_error *
2688 close_blame_view(struct tog_view *view)
2690 const struct got_error *err = NULL;
2691 struct tog_blame_view_state *s = &view->state.blame;
2693 if (s->blame.thread)
2694 err = stop_blame(&s->blame);
2696 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2697 struct got_object_qid *blamed_commit;
2698 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2699 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2700 got_object_qid_free(blamed_commit);
2703 free(s->path);
2705 return err;
2708 static const struct got_error *
2709 show_blame_view(struct tog_view *view)
2711 const struct got_error *err = NULL;
2712 struct tog_blame_view_state *s = &view->state.blame;
2713 int errcode;
2715 if (s->blame.thread == NULL) {
2716 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2717 &s->blame.thread_args);
2718 if (errcode)
2719 return got_error_set_errno(errcode);
2722 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2723 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2724 s->selected_line, &s->first_displayed_line,
2725 &s->last_displayed_line, &s->eof, view->nlines);
2727 view_vborder(view);
2728 return err;
2731 static const struct got_error *
2732 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2733 struct tog_view **focus_view, struct tog_view *view, int ch)
2735 const struct got_error *err = NULL, *thread_err = NULL;
2736 struct tog_view *diff_view;
2737 struct tog_blame_view_state *s = &view->state.blame;
2738 int begin_x = 0;
2740 switch (ch) {
2741 case 'q':
2742 s->done = 1;
2743 break;
2744 case 'k':
2745 case KEY_UP:
2746 if (s->selected_line > 1)
2747 s->selected_line--;
2748 else if (s->selected_line == 1 &&
2749 s->first_displayed_line > 1)
2750 s->first_displayed_line--;
2751 break;
2752 case KEY_PPAGE:
2753 if (s->first_displayed_line == 1) {
2754 s->selected_line = 1;
2755 break;
2757 if (s->first_displayed_line > view->nlines - 2)
2758 s->first_displayed_line -=
2759 (view->nlines - 2);
2760 else
2761 s->first_displayed_line = 1;
2762 break;
2763 case 'j':
2764 case KEY_DOWN:
2765 if (s->selected_line < view->nlines - 2 &&
2766 s->first_displayed_line +
2767 s->selected_line <= s->blame.nlines)
2768 s->selected_line++;
2769 else if (s->last_displayed_line <
2770 s->blame.nlines)
2771 s->first_displayed_line++;
2772 break;
2773 case 'b':
2774 case 'p': {
2775 struct got_object_id *id = NULL;
2776 id = get_selected_commit_id(s->blame.lines,
2777 s->first_displayed_line, s->selected_line);
2778 if (id == NULL)
2779 break;
2780 if (ch == 'p') {
2781 struct got_commit_object *commit;
2782 struct got_object_qid *pid;
2783 struct got_object_id *blob_id = NULL;
2784 int obj_type;
2785 err = got_object_open_as_commit(&commit,
2786 s->repo, id);
2787 if (err)
2788 break;
2789 pid = SIMPLEQ_FIRST(
2790 got_object_commit_get_parent_ids(commit));
2791 if (pid == NULL) {
2792 got_object_commit_close(commit);
2793 break;
2795 /* Check if path history ends here. */
2796 err = got_object_id_by_path(&blob_id, s->repo,
2797 pid->id, s->path);
2798 if (err) {
2799 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2800 err = NULL;
2801 got_object_commit_close(commit);
2802 break;
2804 err = got_object_get_type(&obj_type, s->repo,
2805 blob_id);
2806 free(blob_id);
2807 /* Can't blame non-blob type objects. */
2808 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2809 got_object_commit_close(commit);
2810 break;
2812 err = got_object_qid_alloc(&s->blamed_commit,
2813 pid->id);
2814 got_object_commit_close(commit);
2815 } else {
2816 if (got_object_id_cmp(id,
2817 s->blamed_commit->id) == 0)
2818 break;
2819 err = got_object_qid_alloc(&s->blamed_commit,
2820 id);
2822 if (err)
2823 break;
2824 s->done = 1;
2825 thread_err = stop_blame(&s->blame);
2826 s->done = 0;
2827 if (thread_err)
2828 break;
2829 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2830 s->blamed_commit, entry);
2831 err = run_blame(&s->blame, view, &s->blame_complete,
2832 &s->first_displayed_line, &s->last_displayed_line,
2833 &s->selected_line, &s->done, &s->eof,
2834 s->path, s->blamed_commit->id, s->repo);
2835 if (err)
2836 break;
2837 break;
2839 case 'B': {
2840 struct got_object_qid *first;
2841 first = SIMPLEQ_FIRST(&s->blamed_commits);
2842 if (!got_object_id_cmp(first->id, s->commit_id))
2843 break;
2844 s->done = 1;
2845 thread_err = stop_blame(&s->blame);
2846 s->done = 0;
2847 if (thread_err)
2848 break;
2849 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2850 got_object_qid_free(s->blamed_commit);
2851 s->blamed_commit =
2852 SIMPLEQ_FIRST(&s->blamed_commits);
2853 err = run_blame(&s->blame, view, &s->blame_complete,
2854 &s->first_displayed_line, &s->last_displayed_line,
2855 &s->selected_line, &s->done, &s->eof, s->path,
2856 s->blamed_commit->id, s->repo);
2857 if (err)
2858 break;
2859 break;
2861 case KEY_ENTER:
2862 case '\r': {
2863 struct got_object_id *id = NULL;
2864 struct got_object_qid *pid;
2865 struct got_commit_object *commit = NULL;
2866 id = get_selected_commit_id(s->blame.lines,
2867 s->first_displayed_line, s->selected_line);
2868 if (id == NULL)
2869 break;
2870 err = got_object_open_as_commit(&commit, s->repo, id);
2871 if (err)
2872 break;
2873 pid = SIMPLEQ_FIRST(
2874 got_object_commit_get_parent_ids(commit));
2875 if (view_is_parent_view(view))
2876 begin_x = view_split_begin_x(view->begin_x);
2877 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2878 if (diff_view == NULL) {
2879 got_object_commit_close(commit);
2880 err = got_error_from_errno();
2881 break;
2883 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2884 id, NULL, s->repo);
2885 got_object_commit_close(commit);
2886 if (err) {
2887 view_close(diff_view);
2888 break;
2890 if (view_is_parent_view(view)) {
2891 err = view_close_child(view);
2892 if (err)
2893 break;
2894 err = view_set_child(view, diff_view);
2895 if (err) {
2896 view_close(diff_view);
2897 break;
2899 *focus_view = diff_view;
2900 view->child_focussed = 1;
2901 } else
2902 *new_view = diff_view;
2903 if (err)
2904 break;
2905 break;
2907 case KEY_NPAGE:
2908 case ' ':
2909 if (s->last_displayed_line >= s->blame.nlines &&
2910 s->selected_line < view->nlines - 2) {
2911 s->selected_line = MIN(s->blame.nlines,
2912 view->nlines - 2);
2913 break;
2915 if (s->last_displayed_line + view->nlines - 2
2916 <= s->blame.nlines)
2917 s->first_displayed_line +=
2918 view->nlines - 2;
2919 else
2920 s->first_displayed_line =
2921 s->blame.nlines -
2922 (view->nlines - 3);
2923 break;
2924 case KEY_RESIZE:
2925 if (s->selected_line > view->nlines - 2) {
2926 s->selected_line = MIN(s->blame.nlines,
2927 view->nlines - 2);
2929 break;
2930 default:
2931 break;
2933 return thread_err ? thread_err : err;
2936 static const struct got_error *
2937 cmd_blame(int argc, char *argv[])
2939 const struct got_error *error;
2940 struct got_repository *repo = NULL;
2941 struct got_worktree *worktree = NULL;
2942 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2943 struct got_object_id *commit_id = NULL;
2944 char *commit_id_str = NULL;
2945 int ch;
2946 struct tog_view *view;
2948 #ifndef PROFILE
2949 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2950 NULL) == -1)
2951 err(1, "pledge");
2952 #endif
2954 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2955 switch (ch) {
2956 case 'c':
2957 commit_id_str = optarg;
2958 break;
2959 case 'r':
2960 repo_path = realpath(optarg, NULL);
2961 if (repo_path == NULL)
2962 err(1, "-r option");
2963 break;
2964 default:
2965 usage();
2966 /* NOTREACHED */
2970 argc -= optind;
2971 argv += optind;
2973 if (argc == 1)
2974 path = argv[0];
2975 else
2976 usage_blame();
2978 cwd = getcwd(NULL, 0);
2979 if (cwd == NULL) {
2980 error = got_error_from_errno();
2981 goto done;
2983 if (repo_path == NULL) {
2984 error = got_worktree_open(&worktree, cwd);
2985 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2986 goto done;
2987 else
2988 error = NULL;
2989 if (worktree) {
2990 repo_path =
2991 strdup(got_worktree_get_repo_path(worktree));
2992 if (repo_path == NULL)
2993 error = got_error_from_errno();
2994 if (error)
2995 goto done;
2996 } else {
2997 repo_path = strdup(cwd);
2998 if (repo_path == NULL) {
2999 error = got_error_from_errno();
3000 goto done;
3005 init_curses();
3007 error = apply_unveil(repo_path, NULL);
3008 if (error)
3009 goto done;
3011 error = got_repo_open(&repo, repo_path);
3012 if (error != NULL)
3013 goto done;
3015 if (worktree) {
3016 const char *prefix = got_worktree_get_path_prefix(worktree);
3017 char *p, *worktree_subdir = cwd +
3018 strlen(got_worktree_get_root_path(worktree));
3019 if (asprintf(&p, "%s%s%s%s%s",
3020 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3021 worktree_subdir, worktree_subdir[0] ? "/" : "",
3022 path) == -1) {
3023 error = got_error_from_errno();
3024 goto done;
3026 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3027 free(p);
3028 } else {
3029 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3031 if (error)
3032 goto done;
3034 if (commit_id_str == NULL) {
3035 struct got_reference *head_ref;
3036 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3037 if (error != NULL)
3038 goto done;
3039 error = got_ref_resolve(&commit_id, repo, head_ref);
3040 got_ref_close(head_ref);
3041 } else {
3042 error = got_object_resolve_id_str(&commit_id, repo,
3043 commit_id_str);
3045 if (error != NULL)
3046 goto done;
3048 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3049 if (view == NULL) {
3050 error = got_error_from_errno();
3051 goto done;
3053 error = open_blame_view(view, in_repo_path, commit_id, repo);
3054 if (error)
3055 goto done;
3056 error = view_loop(view);
3057 done:
3058 free(repo_path);
3059 free(cwd);
3060 free(commit_id);
3061 if (worktree)
3062 got_worktree_close(worktree);
3063 if (repo)
3064 got_repo_close(repo);
3065 return error;
3068 static const struct got_error *
3069 draw_tree_entries(struct tog_view *view,
3070 struct got_tree_entry **first_displayed_entry,
3071 struct got_tree_entry **last_displayed_entry,
3072 struct got_tree_entry **selected_entry, int *ndisplayed,
3073 const char *label, int show_ids, const char *parent_path,
3074 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3076 const struct got_error *err = NULL;
3077 struct got_tree_entry *te;
3078 wchar_t *wline;
3079 int width, n;
3081 *ndisplayed = 0;
3083 werase(view->window);
3085 if (limit == 0)
3086 return NULL;
3088 err = format_line(&wline, &width, label, view->ncols);
3089 if (err)
3090 return err;
3091 if (view_needs_focus_indication(view))
3092 wstandout(view->window);
3093 waddwstr(view->window, wline);
3094 if (view_needs_focus_indication(view))
3095 wstandend(view->window);
3096 free(wline);
3097 wline = NULL;
3098 if (width < view->ncols)
3099 waddch(view->window, '\n');
3100 if (--limit <= 0)
3101 return NULL;
3102 err = format_line(&wline, &width, parent_path, view->ncols);
3103 if (err)
3104 return err;
3105 waddwstr(view->window, wline);
3106 free(wline);
3107 wline = NULL;
3108 if (width < view->ncols)
3109 waddch(view->window, '\n');
3110 if (--limit <= 0)
3111 return NULL;
3112 waddch(view->window, '\n');
3113 if (--limit <= 0)
3114 return NULL;
3116 te = SIMPLEQ_FIRST(&entries->head);
3117 if (*first_displayed_entry == NULL) {
3118 if (selected == 0) {
3119 if (view->focussed)
3120 wstandout(view->window);
3121 *selected_entry = NULL;
3123 waddstr(view->window, " ..\n"); /* parent directory */
3124 if (selected == 0 && view->focussed)
3125 wstandend(view->window);
3126 (*ndisplayed)++;
3127 if (--limit <= 0)
3128 return NULL;
3129 n = 1;
3130 } else {
3131 n = 0;
3132 while (te != *first_displayed_entry)
3133 te = SIMPLEQ_NEXT(te, entry);
3136 while (te) {
3137 char *line = NULL, *id_str = NULL;
3139 if (show_ids) {
3140 err = got_object_id_str(&id_str, te->id);
3141 if (err)
3142 return got_error_from_errno();
3144 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3145 te->name, S_ISDIR(te->mode) ? "/" :
3146 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3147 free(id_str);
3148 return got_error_from_errno();
3150 free(id_str);
3151 err = format_line(&wline, &width, line, view->ncols);
3152 if (err) {
3153 free(line);
3154 break;
3156 if (n == selected) {
3157 if (view->focussed)
3158 wstandout(view->window);
3159 *selected_entry = te;
3161 waddwstr(view->window, wline);
3162 if (width < view->ncols)
3163 waddch(view->window, '\n');
3164 if (n == selected && view->focussed)
3165 wstandend(view->window);
3166 free(line);
3167 free(wline);
3168 wline = NULL;
3169 n++;
3170 (*ndisplayed)++;
3171 *last_displayed_entry = te;
3172 if (--limit <= 0)
3173 break;
3174 te = SIMPLEQ_NEXT(te, entry);
3177 return err;
3180 static void
3181 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3182 const struct got_tree_entries *entries, int isroot)
3184 struct got_tree_entry *te, *prev;
3185 int i;
3187 if (*first_displayed_entry == NULL)
3188 return;
3190 te = SIMPLEQ_FIRST(&entries->head);
3191 if (*first_displayed_entry == te) {
3192 if (!isroot)
3193 *first_displayed_entry = NULL;
3194 return;
3197 /* XXX this is stupid... switch to TAILQ? */
3198 for (i = 0; i < maxscroll; i++) {
3199 while (te != *first_displayed_entry) {
3200 prev = te;
3201 te = SIMPLEQ_NEXT(te, entry);
3203 *first_displayed_entry = prev;
3204 te = SIMPLEQ_FIRST(&entries->head);
3206 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3207 *first_displayed_entry = NULL;
3210 static int
3211 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3212 struct got_tree_entry *last_displayed_entry,
3213 const struct got_tree_entries *entries)
3215 struct got_tree_entry *next, *last;
3216 int n = 0;
3218 if (*first_displayed_entry)
3219 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3220 else
3221 next = SIMPLEQ_FIRST(&entries->head);
3222 last = last_displayed_entry;
3223 while (next && last && n++ < maxscroll) {
3224 last = SIMPLEQ_NEXT(last, entry);
3225 if (last) {
3226 *first_displayed_entry = next;
3227 next = SIMPLEQ_NEXT(next, entry);
3230 return n;
3233 static const struct got_error *
3234 tree_entry_path(char **path, struct tog_parent_trees *parents,
3235 struct got_tree_entry *te)
3237 const struct got_error *err = NULL;
3238 struct tog_parent_tree *pt;
3239 size_t len = 2; /* for leading slash and NUL */
3241 TAILQ_FOREACH(pt, parents, entry)
3242 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3243 if (te)
3244 len += strlen(te->name);
3246 *path = calloc(1, len);
3247 if (path == NULL)
3248 return got_error_from_errno();
3250 (*path)[0] = '/';
3251 pt = TAILQ_LAST(parents, tog_parent_trees);
3252 while (pt) {
3253 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3254 err = got_error(GOT_ERR_NO_SPACE);
3255 goto done;
3257 if (strlcat(*path, "/", len) >= len) {
3258 err = got_error(GOT_ERR_NO_SPACE);
3259 goto done;
3261 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3263 if (te) {
3264 if (strlcat(*path, te->name, len) >= len) {
3265 err = got_error(GOT_ERR_NO_SPACE);
3266 goto done;
3269 done:
3270 if (err) {
3271 free(*path);
3272 *path = NULL;
3274 return err;
3277 static const struct got_error *
3278 blame_tree_entry(struct tog_view **new_view, int begin_x,
3279 struct got_tree_entry *te, struct tog_parent_trees *parents,
3280 struct got_object_id *commit_id, struct got_repository *repo)
3282 const struct got_error *err = NULL;
3283 char *path;
3284 struct tog_view *blame_view;
3286 err = tree_entry_path(&path, parents, te);
3287 if (err)
3288 return err;
3290 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3291 if (blame_view == NULL)
3292 return got_error_from_errno();
3294 err = open_blame_view(blame_view, path, commit_id, repo);
3295 if (err) {
3296 view_close(blame_view);
3297 free(path);
3298 } else
3299 *new_view = blame_view;
3300 return err;
3303 static const struct got_error *
3304 log_tree_entry(struct tog_view **new_view, int begin_x,
3305 struct got_tree_entry *te, struct tog_parent_trees *parents,
3306 struct got_object_id *commit_id, struct got_repository *repo)
3308 struct tog_view *log_view;
3309 const struct got_error *err = NULL;
3310 char *path;
3312 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3313 if (log_view == NULL)
3314 return got_error_from_errno();
3316 err = tree_entry_path(&path, parents, te);
3317 if (err)
3318 return err;
3320 err = open_log_view(log_view, commit_id, repo, path, 0);
3321 if (err)
3322 view_close(log_view);
3323 else
3324 *new_view = log_view;
3325 free(path);
3326 return err;
3329 static const struct got_error *
3330 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3331 struct got_object_id *commit_id, struct got_repository *repo)
3333 const struct got_error *err = NULL;
3334 char *commit_id_str = NULL;
3335 struct tog_tree_view_state *s = &view->state.tree;
3337 TAILQ_INIT(&s->parents);
3339 err = got_object_id_str(&commit_id_str, commit_id);
3340 if (err != NULL)
3341 goto done;
3343 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3344 err = got_error_from_errno();
3345 goto done;
3348 s->root = s->tree = root;
3349 s->entries = got_object_tree_get_entries(root);
3350 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3351 s->commit_id = got_object_id_dup(commit_id);
3352 if (s->commit_id == NULL) {
3353 err = got_error_from_errno();
3354 goto done;
3356 s->repo = repo;
3358 view->show = show_tree_view;
3359 view->input = input_tree_view;
3360 view->close = close_tree_view;
3361 done:
3362 free(commit_id_str);
3363 if (err) {
3364 free(s->tree_label);
3365 s->tree_label = NULL;
3367 return err;
3370 static const struct got_error *
3371 close_tree_view(struct tog_view *view)
3373 struct tog_tree_view_state *s = &view->state.tree;
3375 free(s->tree_label);
3376 s->tree_label = NULL;
3377 free(s->commit_id);
3378 s->commit_id = NULL;
3379 while (!TAILQ_EMPTY(&s->parents)) {
3380 struct tog_parent_tree *parent;
3381 parent = TAILQ_FIRST(&s->parents);
3382 TAILQ_REMOVE(&s->parents, parent, entry);
3383 free(parent);
3386 if (s->tree != s->root)
3387 got_object_tree_close(s->tree);
3388 got_object_tree_close(s->root);
3390 return NULL;
3393 static const struct got_error *
3394 show_tree_view(struct tog_view *view)
3396 const struct got_error *err = NULL;
3397 struct tog_tree_view_state *s = &view->state.tree;
3398 char *parent_path;
3400 err = tree_entry_path(&parent_path, &s->parents, NULL);
3401 if (err)
3402 return err;
3404 err = draw_tree_entries(view, &s->first_displayed_entry,
3405 &s->last_displayed_entry, &s->selected_entry,
3406 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3407 s->entries, s->selected, view->nlines, s->tree == s->root);
3408 free(parent_path);
3410 view_vborder(view);
3411 return err;
3414 static const struct got_error *
3415 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3416 struct tog_view **focus_view, struct tog_view *view, int ch)
3418 const struct got_error *err = NULL;
3419 struct tog_tree_view_state *s = &view->state.tree;
3420 struct tog_view *log_view;
3421 int begin_x = 0, nscrolled;
3423 switch (ch) {
3424 case 'i':
3425 s->show_ids = !s->show_ids;
3426 break;
3427 case 'l':
3428 if (!s->selected_entry)
3429 break;
3430 if (view_is_parent_view(view))
3431 begin_x = view_split_begin_x(view->begin_x);
3432 err = log_tree_entry(&log_view, begin_x,
3433 s->selected_entry, &s->parents,
3434 s->commit_id, s->repo);
3435 if (view_is_parent_view(view)) {
3436 err = view_close_child(view);
3437 if (err)
3438 return err;
3439 err = view_set_child(view, log_view);
3440 if (err) {
3441 view_close(log_view);
3442 break;
3444 *focus_view = log_view;
3445 view->child_focussed = 1;
3446 } else
3447 *new_view = log_view;
3448 break;
3449 case 'k':
3450 case KEY_UP:
3451 if (s->selected > 0) {
3452 s->selected--;
3453 if (s->selected == 0)
3454 break;
3456 if (s->selected > 0)
3457 break;
3458 tree_scroll_up(&s->first_displayed_entry, 1,
3459 s->entries, s->tree == s->root);
3460 break;
3461 case KEY_PPAGE:
3462 tree_scroll_up(&s->first_displayed_entry,
3463 MAX(0, view->nlines - 4 - s->selected), s->entries,
3464 s->tree == s->root);
3465 s->selected = 0;
3466 if (SIMPLEQ_FIRST(&s->entries->head) ==
3467 s->first_displayed_entry && s->tree != s->root)
3468 s->first_displayed_entry = NULL;
3469 break;
3470 case 'j':
3471 case KEY_DOWN:
3472 if (s->selected < s->ndisplayed - 1) {
3473 s->selected++;
3474 break;
3476 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3477 == NULL) {
3478 /* can't scroll any further */
3479 break;
3481 tree_scroll_down(&s->first_displayed_entry, 1,
3482 s->last_displayed_entry, s->entries);
3483 break;
3484 case KEY_NPAGE:
3485 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3486 == NULL) {
3487 /* can't scroll any further; move cursor down */
3488 if (s->selected < s->ndisplayed - 1)
3489 s->selected = s->ndisplayed - 1;
3490 break;
3492 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3493 view->nlines, s->last_displayed_entry, s->entries);
3494 if (nscrolled < view->nlines) {
3495 int ndisplayed = 0;
3496 struct got_tree_entry *te;
3497 te = s->first_displayed_entry;
3498 do {
3499 ndisplayed++;
3500 te = SIMPLEQ_NEXT(te, entry);
3501 } while (te);
3502 s->selected = ndisplayed - 1;
3504 break;
3505 case KEY_ENTER:
3506 case '\r':
3507 if (s->selected_entry == NULL) {
3508 struct tog_parent_tree *parent;
3509 case KEY_BACKSPACE:
3510 /* user selected '..' */
3511 if (s->tree == s->root)
3512 break;
3513 parent = TAILQ_FIRST(&s->parents);
3514 TAILQ_REMOVE(&s->parents, parent,
3515 entry);
3516 got_object_tree_close(s->tree);
3517 s->tree = parent->tree;
3518 s->entries =
3519 got_object_tree_get_entries(s->tree);
3520 s->first_displayed_entry =
3521 parent->first_displayed_entry;
3522 s->selected_entry =
3523 parent->selected_entry;
3524 s->selected = parent->selected;
3525 free(parent);
3526 } else if (S_ISDIR(s->selected_entry->mode)) {
3527 struct tog_parent_tree *parent;
3528 struct got_tree_object *child;
3529 err = got_object_open_as_tree(&child,
3530 s->repo, s->selected_entry->id);
3531 if (err)
3532 break;
3533 parent = calloc(1, sizeof(*parent));
3534 if (parent == NULL) {
3535 err = got_error_from_errno();
3536 break;
3538 parent->tree = s->tree;
3539 parent->first_displayed_entry =
3540 s->first_displayed_entry;
3541 parent->selected_entry = s->selected_entry;
3542 parent->selected = s->selected;
3543 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3544 s->tree = child;
3545 s->entries =
3546 got_object_tree_get_entries(s->tree);
3547 s->selected = 0;
3548 s->first_displayed_entry = NULL;
3549 } else if (S_ISREG(s->selected_entry->mode)) {
3550 struct tog_view *blame_view;
3551 int begin_x = view_is_parent_view(view) ?
3552 view_split_begin_x(view->begin_x) : 0;
3554 err = blame_tree_entry(&blame_view, begin_x,
3555 s->selected_entry, &s->parents, s->commit_id,
3556 s->repo);
3557 if (err)
3558 break;
3559 if (view_is_parent_view(view)) {
3560 err = view_close_child(view);
3561 if (err)
3562 return err;
3563 err = view_set_child(view, blame_view);
3564 if (err) {
3565 view_close(blame_view);
3566 break;
3568 *focus_view = blame_view;
3569 view->child_focussed = 1;
3570 } else
3571 *new_view = blame_view;
3573 break;
3574 case KEY_RESIZE:
3575 if (s->selected > view->nlines)
3576 s->selected = s->ndisplayed - 1;
3577 break;
3578 default:
3579 break;
3582 return err;
3585 __dead static void
3586 usage_tree(void)
3588 endwin();
3589 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3590 getprogname());
3591 exit(1);
3594 static const struct got_error *
3595 cmd_tree(int argc, char *argv[])
3597 const struct got_error *error;
3598 struct got_repository *repo = NULL;
3599 char *repo_path = NULL;
3600 struct got_object_id *commit_id = NULL;
3601 char *commit_id_arg = NULL;
3602 struct got_commit_object *commit = NULL;
3603 struct got_tree_object *tree = NULL;
3604 int ch;
3605 struct tog_view *view;
3607 #ifndef PROFILE
3608 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3609 NULL) == -1)
3610 err(1, "pledge");
3611 #endif
3613 while ((ch = getopt(argc, argv, "c:")) != -1) {
3614 switch (ch) {
3615 case 'c':
3616 commit_id_arg = optarg;
3617 break;
3618 default:
3619 usage();
3620 /* NOTREACHED */
3624 argc -= optind;
3625 argv += optind;
3627 if (argc == 0) {
3628 struct got_worktree *worktree;
3629 char *cwd = getcwd(NULL, 0);
3630 if (cwd == NULL)
3631 return got_error_from_errno();
3632 error = got_worktree_open(&worktree, cwd);
3633 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3634 goto done;
3635 if (worktree) {
3636 free(cwd);
3637 repo_path =
3638 strdup(got_worktree_get_repo_path(worktree));
3639 got_worktree_close(worktree);
3640 } else
3641 repo_path = cwd;
3642 if (repo_path == NULL) {
3643 error = got_error_from_errno();
3644 goto done;
3646 } else if (argc == 1) {
3647 repo_path = realpath(argv[0], NULL);
3648 if (repo_path == NULL)
3649 return got_error_from_errno();
3650 } else
3651 usage_log();
3653 init_curses();
3655 error = apply_unveil(repo_path, NULL);
3656 if (error)
3657 goto done;
3659 error = got_repo_open(&repo, repo_path);
3660 if (error != NULL)
3661 goto done;
3663 if (commit_id_arg == NULL)
3664 error = get_head_commit_id(&commit_id, repo);
3665 else
3666 error = got_object_resolve_id_str(&commit_id, repo,
3667 commit_id_arg);
3668 if (error != NULL)
3669 goto done;
3671 error = got_object_open_as_commit(&commit, repo, commit_id);
3672 if (error != NULL)
3673 goto done;
3675 error = got_object_open_as_tree(&tree, repo,
3676 got_object_commit_get_tree_id(commit));
3677 if (error != NULL)
3678 goto done;
3680 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3681 if (view == NULL) {
3682 error = got_error_from_errno();
3683 goto done;
3685 error = open_tree_view(view, tree, commit_id, repo);
3686 if (error)
3687 goto done;
3688 error = view_loop(view);
3689 done:
3690 free(repo_path);
3691 free(commit_id);
3692 if (commit)
3693 got_object_commit_close(commit);
3694 if (tree)
3695 got_object_tree_close(tree);
3696 if (repo)
3697 got_repo_close(repo);
3698 return error;
3701 __dead static void
3702 usage(void)
3704 int i;
3706 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3707 "Available commands:\n", getprogname());
3708 for (i = 0; i < nitems(tog_commands); i++) {
3709 struct tog_cmd *cmd = &tog_commands[i];
3710 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3712 exit(1);
3715 static char **
3716 make_argv(const char *arg0, const char *arg1)
3718 char **argv;
3719 int argc = (arg1 == NULL ? 1 : 2);
3721 argv = calloc(argc, sizeof(char *));
3722 if (argv == NULL)
3723 err(1, "calloc");
3724 argv[0] = strdup(arg0);
3725 if (argv[0] == NULL)
3726 err(1, "calloc");
3727 if (arg1) {
3728 argv[1] = strdup(arg1);
3729 if (argv[1] == NULL)
3730 err(1, "calloc");
3733 return argv;
3736 int
3737 main(int argc, char *argv[])
3739 const struct got_error *error = NULL;
3740 struct tog_cmd *cmd = NULL;
3741 int ch, hflag = 0;
3742 char **cmd_argv = NULL;
3744 setlocale(LC_CTYPE, "");
3746 while ((ch = getopt(argc, argv, "h")) != -1) {
3747 switch (ch) {
3748 case 'h':
3749 hflag = 1;
3750 break;
3751 default:
3752 usage();
3753 /* NOTREACHED */
3757 argc -= optind;
3758 argv += optind;
3759 optind = 0;
3760 optreset = 1;
3762 if (argc == 0) {
3763 if (hflag)
3764 usage();
3765 /* Build an argument vector which runs a default command. */
3766 cmd = &tog_commands[0];
3767 cmd_argv = make_argv(cmd->name, NULL);
3768 argc = 1;
3769 } else {
3770 int i;
3772 /* Did the user specific a command? */
3773 for (i = 0; i < nitems(tog_commands); i++) {
3774 if (strncmp(tog_commands[i].name, argv[0],
3775 strlen(argv[0])) == 0) {
3776 cmd = &tog_commands[i];
3777 if (hflag)
3778 tog_commands[i].cmd_usage();
3779 break;
3782 if (cmd == NULL) {
3783 /* Did the user specify a repository? */
3784 char *repo_path = realpath(argv[0], NULL);
3785 if (repo_path) {
3786 struct got_repository *repo;
3787 error = got_repo_open(&repo, repo_path);
3788 if (error == NULL)
3789 got_repo_close(repo);
3790 } else
3791 error = got_error_from_errno();
3792 if (error) {
3793 if (hflag) {
3794 fprintf(stderr, "%s: '%s' is not a "
3795 "known command\n", getprogname(),
3796 argv[0]);
3797 usage();
3799 fprintf(stderr, "%s: '%s' is neither a known "
3800 "command nor a path to a repository\n",
3801 getprogname(), argv[0]);
3802 free(repo_path);
3803 return 1;
3805 cmd = &tog_commands[0];
3806 cmd_argv = make_argv(cmd->name, repo_path);
3807 argc = 2;
3808 free(repo_path);
3812 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3813 if (error)
3814 goto done;
3815 done:
3816 endwin();
3817 free(cmd_argv);
3818 if (error)
3819 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3820 return 0;