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;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
125 };
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
131 int commits_needed;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
137 int log_complete;
138 sig_atomic_t *quit;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
142 };
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
149 int selected;
150 char *in_repo_path;
151 struct got_repository *repo;
152 struct got_reflist_head *refs;
153 struct got_object_id *start_id;
154 sig_atomic_t quit;
155 pthread_t thread;
156 struct tog_log_thread_args thread_args;
157 };
159 struct tog_blame_cb_args {
160 struct tog_blame_line *lines; /* one per line */
161 int nlines;
163 struct tog_view *view;
164 struct got_object_id *commit_id;
165 int *quit;
166 };
168 struct tog_blame_thread_args {
169 const char *path;
170 struct got_repository *repo;
171 struct tog_blame_cb_args *cb_args;
172 int *complete;
173 };
175 struct tog_blame {
176 FILE *f;
177 size_t filesize;
178 struct tog_blame_line *lines;
179 int nlines;
180 pthread_t thread;
181 struct tog_blame_thread_args thread_args;
182 struct tog_blame_cb_args cb_args;
183 const char *path;
184 };
186 struct tog_blame_view_state {
187 int first_displayed_line;
188 int last_displayed_line;
189 int selected_line;
190 int blame_complete;
191 int eof;
192 int done;
193 struct got_object_id_queue blamed_commits;
194 struct got_object_qid *blamed_commit;
195 char *path;
196 struct got_repository *repo;
197 struct got_reflist_head *refs;
198 struct got_object_id *commit_id;
199 struct tog_blame blame;
200 };
202 struct tog_parent_tree {
203 TAILQ_ENTRY(tog_parent_tree) entry;
204 struct got_tree_object *tree;
205 struct got_tree_entry *first_displayed_entry;
206 struct got_tree_entry *selected_entry;
207 int selected;
208 };
210 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
212 struct tog_tree_view_state {
213 char *tree_label;
214 struct got_tree_object *root;
215 struct got_tree_object *tree;
216 const struct got_tree_entries *entries;
217 struct got_tree_entry *first_displayed_entry;
218 struct got_tree_entry *last_displayed_entry;
219 struct got_tree_entry *selected_entry;
220 int ndisplayed, selected, show_ids;
221 struct tog_parent_trees parents;
222 struct got_object_id *commit_id;
223 struct got_repository *repo;
224 struct got_reflist_head *refs;
225 };
227 /*
228 * We implement two types of views: parent views and child views.
230 * The 'Tab' key switches between a parent view and its child view.
231 * Child views are shown side-by-side to their parent view, provided
232 * there is enough screen estate.
234 * When a new view is opened from within a parent view, this new view
235 * becomes a child view of the parent view, replacing any existing child.
237 * When a new view is opened from within a child view, this new view
238 * becomes a parent view which will obscure the views below until the
239 * user quits the new parent view by typing 'q'.
241 * This list of views contains parent views only.
242 * Child views are only pointed to by their parent view.
243 */
244 TAILQ_HEAD(tog_view_list_head, tog_view);
246 struct tog_view {
247 TAILQ_ENTRY(tog_view) entry;
248 WINDOW *window;
249 PANEL *panel;
250 int nlines, ncols, begin_y, begin_x;
251 int lines, cols; /* copies of LINES and COLS */
252 int focussed;
253 struct tog_view *parent;
254 struct tog_view *child;
255 int child_focussed;
257 /* type-specific state */
258 enum tog_view_type type;
259 union {
260 struct tog_diff_view_state diff;
261 struct tog_log_view_state log;
262 struct tog_blame_view_state blame;
263 struct tog_tree_view_state tree;
264 } state;
266 const struct got_error *(*show)(struct tog_view *);
267 const struct got_error *(*input)(struct tog_view **,
268 struct tog_view **, struct tog_view**, struct tog_view *, int);
269 const struct got_error *(*close)(struct tog_view *);
270 };
272 static const struct got_error *open_diff_view(struct tog_view *,
273 struct got_object_id *, struct got_object_id *, struct tog_view *,
274 struct got_reflist_head *, struct got_repository *);
275 static const struct got_error *show_diff_view(struct tog_view *);
276 static const struct got_error *input_diff_view(struct tog_view **,
277 struct tog_view **, struct tog_view **, struct tog_view *, int);
278 static const struct got_error* close_diff_view(struct tog_view *);
280 static const struct got_error *open_log_view(struct tog_view *,
281 struct got_object_id *, struct got_reflist_head *,
282 struct got_repository *, const char *, int);
283 static const struct got_error * show_log_view(struct tog_view *);
284 static const struct got_error *input_log_view(struct tog_view **,
285 struct tog_view **, struct tog_view **, struct tog_view *, int);
286 static const struct got_error *close_log_view(struct tog_view *);
288 static const struct got_error *open_blame_view(struct tog_view *, char *,
289 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_blame_view(struct tog_view *);
291 static const struct got_error *input_blame_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error *close_blame_view(struct tog_view *);
295 static const struct got_error *open_tree_view(struct tog_view *,
296 struct got_tree_object *, struct got_object_id *,
297 struct got_reflist_head *, struct got_repository *);
298 static const struct got_error *show_tree_view(struct tog_view *);
299 static const struct got_error *input_tree_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_tree_view(struct tog_view *);
303 static volatile sig_atomic_t tog_sigwinch_received;
305 static void
306 tog_sigwinch(int signo)
308 tog_sigwinch_received = 1;
311 static const struct got_error *
312 view_close(struct tog_view *view)
314 const struct got_error *err = NULL;
316 if (view->child) {
317 view_close(view->child);
318 view->child = NULL;
320 if (view->close)
321 err = view->close(view);
322 if (view->panel)
323 del_panel(view->panel);
324 if (view->window)
325 delwin(view->window);
326 free(view);
327 return err;
330 static struct tog_view *
331 view_open(int nlines, int ncols, int begin_y, int begin_x,
332 enum tog_view_type type)
334 struct tog_view *view = calloc(1, sizeof(*view));
336 if (view == NULL)
337 return NULL;
339 view->type = type;
340 view->lines = LINES;
341 view->cols = COLS;
342 view->nlines = nlines ? nlines : LINES - begin_y;
343 view->ncols = ncols ? ncols : COLS - begin_x;
344 view->begin_y = begin_y;
345 view->begin_x = begin_x;
346 view->window = newwin(nlines, ncols, begin_y, begin_x);
347 if (view->window == NULL) {
348 view_close(view);
349 return NULL;
351 view->panel = new_panel(view->window);
352 if (view->panel == NULL ||
353 set_panel_userptr(view->panel, view) != OK) {
354 view_close(view);
355 return NULL;
358 keypad(view->window, TRUE);
359 return view;
362 static int
363 view_split_begin_x(int begin_x)
365 if (begin_x > 0 || COLS < 120)
366 return 0;
367 return (COLS - MAX(COLS / 2, 80));
370 static const struct got_error *view_resize(struct tog_view *);
372 static const struct got_error *
373 view_splitscreen(struct tog_view *view)
375 const struct got_error *err = NULL;
377 view->begin_y = 0;
378 view->begin_x = view_split_begin_x(0);
379 view->nlines = LINES;
380 view->ncols = COLS - view->begin_x;
381 view->lines = LINES;
382 view->cols = COLS;
383 err = view_resize(view);
384 if (err)
385 return err;
387 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 return got_error_from_errno();
390 return NULL;
393 static const struct got_error *
394 view_fullscreen(struct tog_view *view)
396 const struct got_error *err = NULL;
398 view->begin_x = 0;
399 view->begin_y = 0;
400 view->nlines = LINES;
401 view->ncols = COLS;
402 view->lines = LINES;
403 view->cols = COLS;
404 err = view_resize(view);
405 if (err)
406 return err;
408 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 return got_error_from_errno();
411 return NULL;
414 static int
415 view_is_parent_view(struct tog_view *view)
417 return view->parent == NULL;
420 static const struct got_error *
421 view_resize(struct tog_view *view)
423 int nlines, ncols;
425 if (view->lines > LINES)
426 nlines = view->nlines - (view->lines - LINES);
427 else
428 nlines = view->nlines + (LINES - view->lines);
430 if (view->cols > COLS)
431 ncols = view->ncols - (view->cols - COLS);
432 else
433 ncols = view->ncols + (COLS - view->cols);
435 if (wresize(view->window, nlines, ncols) == ERR)
436 return got_error_from_errno();
437 if (replace_panel(view->panel, view->window) == ERR)
438 return got_error_from_errno();
439 wclear(view->window);
441 view->nlines = nlines;
442 view->ncols = ncols;
443 view->lines = LINES;
444 view->cols = COLS;
446 if (view->child) {
447 view->child->begin_x = view_split_begin_x(view->begin_x);
448 if (view->child->begin_x == 0) {
449 view_fullscreen(view->child);
450 if (view->child->focussed)
451 show_panel(view->child->panel);
452 else
453 show_panel(view->panel);
454 } else {
455 view_splitscreen(view->child);
456 show_panel(view->child->panel);
460 return NULL;
463 static const struct got_error *
464 view_close_child(struct tog_view *view)
466 const struct got_error *err = NULL;
468 if (view->child == NULL)
469 return NULL;
471 err = view_close(view->child);
472 view->child = NULL;
473 return err;
476 static const struct got_error *
477 view_set_child(struct tog_view *view, struct tog_view *child)
479 const struct got_error *err = NULL;
481 view->child = child;
482 child->parent = view;
483 return err;
486 static int
487 view_is_splitscreen(struct tog_view *view)
489 return !view_is_parent_view(view) && view->begin_x > 0;
492 static void
493 tog_resizeterm(void)
495 int cols, lines;
496 struct winsize size;
498 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
499 cols = 80; /* Default */
500 lines = 24;
501 } else {
502 cols = size.ws_col;
503 lines = size.ws_row;
505 resize_term(lines, cols);
508 static const struct got_error *
509 view_input(struct tog_view **new, struct tog_view **dead,
510 struct tog_view **focus, int *done, struct tog_view *view,
511 struct tog_view_list_head *views)
513 const struct got_error *err = NULL;
514 struct tog_view *v;
515 int ch, errcode;
517 *new = NULL;
518 *dead = NULL;
519 *focus = NULL;
521 nodelay(stdscr, FALSE);
522 /* Allow threads to make progress while we are waiting for input. */
523 errcode = pthread_mutex_unlock(&tog_mutex);
524 if (errcode)
525 return got_error_set_errno(errcode);
526 ch = wgetch(view->window);
527 errcode = pthread_mutex_lock(&tog_mutex);
528 if (errcode)
529 return got_error_set_errno(errcode);
530 nodelay(stdscr, TRUE);
532 if (tog_sigwinch_received) {
533 tog_resizeterm();
534 tog_sigwinch_received = 0;
535 TAILQ_FOREACH(v, views, entry) {
536 err = view_resize(v);
537 if (err)
538 return err;
539 err = v->input(new, dead, focus, v, KEY_RESIZE);
540 if (err)
541 return err;
545 switch (ch) {
546 case ERR:
547 break;
548 case '\t':
549 if (view->child) {
550 *focus = view->child;
551 view->child_focussed = 1;
552 } else if (view->parent) {
553 *focus = view->parent;
554 view->parent->child_focussed = 0;
556 break;
557 case 'q':
558 err = view->input(new, dead, focus, view, ch);
559 *dead = view;
560 break;
561 case 'Q':
562 *done = 1;
563 break;
564 case 'f':
565 if (view_is_parent_view(view)) {
566 if (view->child == NULL)
567 break;
568 if (view_is_splitscreen(view->child)) {
569 *focus = view->child;
570 view->child_focussed = 1;
571 err = view_fullscreen(view->child);
572 } else
573 err = view_splitscreen(view->child);
574 if (err)
575 break;
576 err = view->child->input(new, dead, focus,
577 view->child, KEY_RESIZE);
578 } else {
579 if (view_is_splitscreen(view)) {
580 *focus = view;
581 view->parent->child_focussed = 1;
582 err = view_fullscreen(view);
583 } else {
584 err = view_splitscreen(view);
586 if (err)
587 break;
588 err = view->input(new, dead, focus, view,
589 KEY_RESIZE);
591 break;
592 case KEY_RESIZE:
593 break;
594 default:
595 err = view->input(new, dead, focus, view, ch);
596 break;
599 return err;
602 void
603 view_vborder(struct tog_view *view)
605 PANEL *panel;
606 struct tog_view *view_above;
608 if (view->parent)
609 return view_vborder(view->parent);
611 panel = panel_above(view->panel);
612 if (panel == NULL)
613 return;
615 view_above = panel_userptr(panel);
616 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
617 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
620 int
621 view_needs_focus_indication(struct tog_view *view)
623 if (view_is_parent_view(view)) {
624 if (view->child == NULL || view->child_focussed)
625 return 0;
626 if (!view_is_splitscreen(view->child))
627 return 0;
628 } else if (!view_is_splitscreen(view))
629 return 0;
631 return view->focussed;
634 static const struct got_error *
635 view_loop(struct tog_view *view)
637 const struct got_error *err = NULL;
638 struct tog_view_list_head views;
639 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
640 int fast_refresh = 10;
641 int done = 0, errcode;
643 errcode = pthread_mutex_lock(&tog_mutex);
644 if (errcode)
645 return got_error_set_errno(errcode);
647 TAILQ_INIT(&views);
648 TAILQ_INSERT_HEAD(&views, view, entry);
650 main_view = view;
651 view->focussed = 1;
652 err = view->show(view);
653 if (err)
654 return err;
655 update_panels();
656 doupdate();
657 while (!TAILQ_EMPTY(&views) && !done) {
658 /* Refresh fast during initialization, then become slower. */
659 if (fast_refresh && fast_refresh-- == 0)
660 halfdelay(10); /* switch to once per second */
662 err = view_input(&new_view, &dead_view, &focus_view, &done,
663 view, &views);
664 if (err)
665 break;
666 if (dead_view) {
667 struct tog_view *prev = NULL;
669 if (view_is_parent_view(dead_view))
670 prev = TAILQ_PREV(dead_view,
671 tog_view_list_head, entry);
672 else if (view->parent != dead_view)
673 prev = view->parent;
675 if (dead_view->parent)
676 dead_view->parent->child = NULL;
677 else
678 TAILQ_REMOVE(&views, dead_view, entry);
680 err = view_close(dead_view);
681 if (err || dead_view == main_view)
682 goto done;
684 if (view == dead_view) {
685 if (focus_view)
686 view = focus_view;
687 else if (prev)
688 view = prev;
689 else if (!TAILQ_EMPTY(&views))
690 view = TAILQ_LAST(&views,
691 tog_view_list_head);
692 else
693 view = NULL;
694 if (view) {
695 if (view->child && view->child_focussed)
696 focus_view = view->child;
697 else
698 focus_view = view;
702 if (new_view) {
703 struct tog_view *v, *t;
704 /* Only allow one parent view per type. */
705 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
706 if (v->type != new_view->type)
707 continue;
708 TAILQ_REMOVE(&views, v, entry);
709 err = view_close(v);
710 if (err)
711 goto done;
712 break;
714 TAILQ_INSERT_TAIL(&views, new_view, entry);
715 view = new_view;
716 if (focus_view == NULL)
717 focus_view = new_view;
719 if (focus_view) {
720 show_panel(focus_view->panel);
721 if (view)
722 view->focussed = 0;
723 focus_view->focussed = 1;
724 view = focus_view;
725 if (new_view)
726 show_panel(new_view->panel);
727 if (view->child && view_is_splitscreen(view->child))
728 show_panel(view->child->panel);
730 if (view) {
731 if (focus_view == NULL) {
732 view->focussed = 1;
733 show_panel(view->panel);
734 if (view->child && view_is_splitscreen(view->child))
735 show_panel(view->child->panel);
736 focus_view = view;
738 if (view->parent) {
739 err = view->parent->show(view->parent);
740 if (err)
741 goto done;
743 err = view->show(view);
744 if (err)
745 goto done;
746 if (view->child) {
747 err = view->child->show(view->child);
748 if (err)
749 goto done;
751 update_panels();
752 doupdate();
755 done:
756 while (!TAILQ_EMPTY(&views)) {
757 view = TAILQ_FIRST(&views);
758 TAILQ_REMOVE(&views, view, entry);
759 view_close(view);
762 errcode = pthread_mutex_unlock(&tog_mutex);
763 if (errcode)
764 return got_error_set_errno(errcode);
766 return err;
769 __dead static void
770 usage_log(void)
772 endwin();
773 fprintf(stderr,
774 "usage: %s log [-c commit] [-r repository-path] [path]\n",
775 getprogname());
776 exit(1);
779 /* Create newly allocated wide-character string equivalent to a byte string. */
780 static const struct got_error *
781 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
783 char *vis = NULL;
784 const struct got_error *err = NULL;
786 *ws = NULL;
787 *wlen = mbstowcs(NULL, s, 0);
788 if (*wlen == (size_t)-1) {
789 int vislen;
790 if (errno != EILSEQ)
791 return got_error_from_errno();
793 /* byte string invalid in current encoding; try to "fix" it */
794 err = got_mbsavis(&vis, &vislen, s);
795 if (err)
796 return err;
797 *wlen = mbstowcs(NULL, vis, 0);
798 if (*wlen == (size_t)-1) {
799 err = got_error_from_errno(); /* give up */
800 goto done;
804 *ws = calloc(*wlen + 1, sizeof(*ws));
805 if (*ws == NULL) {
806 err = got_error_from_errno();
807 goto done;
810 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
811 err = got_error_from_errno();
812 done:
813 free(vis);
814 if (err) {
815 free(*ws);
816 *ws = NULL;
817 *wlen = 0;
819 return err;
822 /* Format a line for display, ensuring that it won't overflow a width limit. */
823 static const struct got_error *
824 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
826 const struct got_error *err = NULL;
827 int cols = 0;
828 wchar_t *wline = NULL;
829 size_t wlen;
830 int i;
832 *wlinep = NULL;
833 *widthp = 0;
835 err = mbs2ws(&wline, &wlen, line);
836 if (err)
837 return err;
839 i = 0;
840 while (i < wlen && cols < wlimit) {
841 int width = wcwidth(wline[i]);
842 switch (width) {
843 case 0:
844 i++;
845 break;
846 case 1:
847 case 2:
848 if (cols + width <= wlimit)
849 cols += width;
850 i++;
851 break;
852 case -1:
853 if (wline[i] == L'\t')
854 cols += TABSIZE - ((cols + 1) % TABSIZE);
855 i++;
856 break;
857 default:
858 err = got_error_from_errno();
859 goto done;
862 wline[i] = L'\0';
863 if (widthp)
864 *widthp = cols;
865 done:
866 if (err)
867 free(wline);
868 else
869 *wlinep = wline;
870 return err;
873 static const struct got_error*
874 build_refs_str(char **refs_str, struct got_reflist_head *refs,
875 struct got_object_id *id)
877 static const struct got_error *err = NULL;
878 struct got_reflist_entry *re;
879 char *s;
880 const char *name;
882 *refs_str = NULL;
884 SIMPLEQ_FOREACH(re, refs, entry) {
885 if (got_object_id_cmp(re->id, id) != 0)
886 continue;
887 name = got_ref_get_name(re->ref);
888 if (strcmp(name, GOT_REF_HEAD) == 0)
889 continue;
890 if (strncmp(name, "refs/", 5) == 0)
891 name += 5;
892 if (strncmp(name, "heads/", 6) == 0)
893 name += 6;
894 if (strncmp(name, "remotes/", 8) == 0)
895 name += 8;
896 s = *refs_str;
897 if (asprintf(refs_str, "%s%s%s", s ? s : "",
898 s ? ", " : "", name) == -1) {
899 err = got_error_from_errno();
900 free(s);
901 *refs_str = NULL;
902 break;
904 free(s);
907 return err;
910 static const struct got_error *
911 draw_commit(struct tog_view *view, struct got_commit_object *commit,
912 struct got_object_id *id, struct got_reflist_head *refs)
914 const struct got_error *err = NULL;
915 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
916 char *logmsg0 = NULL, *logmsg = NULL;
917 char *author0 = NULL, *author = NULL;
918 wchar_t *wlogmsg = NULL, *wauthor = NULL;
919 int author_width, logmsg_width;
920 char *newline, *smallerthan;
921 char *line = NULL;
922 int col, limit;
923 static const size_t date_display_cols = 9;
924 static const size_t author_display_cols = 16;
925 const int avail = view->ncols;
926 struct tm tm;
927 time_t committer_time;
929 committer_time = got_object_commit_get_committer_time(commit);
930 if (localtime_r(&committer_time, &tm) == NULL)
931 return got_error_from_errno();
932 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
933 >= sizeof(datebuf))
934 return got_error(GOT_ERR_NO_SPACE);
936 if (avail < date_display_cols)
937 limit = MIN(sizeof(datebuf) - 1, avail);
938 else
939 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
940 waddnstr(view->window, datebuf, limit);
941 col = limit + 1;
942 if (col > avail)
943 goto done;
945 author0 = strdup(got_object_commit_get_author(commit));
946 if (author0 == NULL) {
947 err = got_error_from_errno();
948 goto done;
950 author = author0;
951 smallerthan = strchr(author, '<');
952 if (smallerthan)
953 *smallerthan = '\0';
954 else {
955 char *at = strchr(author, '@');
956 if (at)
957 *at = '\0';
959 limit = avail - col;
960 err = format_line(&wauthor, &author_width, author, limit);
961 if (err)
962 goto done;
963 waddwstr(view->window, wauthor);
964 col += author_width;
965 while (col <= avail && author_width < author_display_cols + 1) {
966 waddch(view->window, ' ');
967 col++;
968 author_width++;
970 if (col > avail)
971 goto done;
973 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
974 if (logmsg0 == NULL) {
975 err = got_error_from_errno();
976 goto done;
978 logmsg = logmsg0;
979 while (*logmsg == '\n')
980 logmsg++;
981 newline = strchr(logmsg, '\n');
982 if (newline)
983 *newline = '\0';
984 limit = avail - col;
985 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
986 if (err)
987 goto done;
988 waddwstr(view->window, wlogmsg);
989 col += logmsg_width;
990 while (col <= avail) {
991 waddch(view->window, ' ');
992 col++;
994 done:
995 free(logmsg0);
996 free(wlogmsg);
997 free(author0);
998 free(wauthor);
999 free(line);
1000 return err;
1003 static struct commit_queue_entry *
1004 alloc_commit_queue_entry(struct got_commit_object *commit,
1005 struct got_object_id *id)
1007 struct commit_queue_entry *entry;
1009 entry = calloc(1, sizeof(*entry));
1010 if (entry == NULL)
1011 return NULL;
1013 entry->id = id;
1014 entry->commit = commit;
1015 return entry;
1018 static void
1019 pop_commit(struct commit_queue *commits)
1021 struct commit_queue_entry *entry;
1023 entry = TAILQ_FIRST(&commits->head);
1024 TAILQ_REMOVE(&commits->head, entry, entry);
1025 got_object_commit_close(entry->commit);
1026 commits->ncommits--;
1027 /* Don't free entry->id! It is owned by the commit graph. */
1028 free(entry);
1031 static void
1032 free_commits(struct commit_queue *commits)
1034 while (!TAILQ_EMPTY(&commits->head))
1035 pop_commit(commits);
1038 static const struct got_error *
1039 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1040 int minqueue, struct got_repository *repo, const char *path)
1042 const struct got_error *err = NULL;
1043 int nqueued = 0;
1046 * We keep all commits open throughout the lifetime of the log
1047 * view in order to avoid having to re-fetch commits from disk
1048 * while updating the display.
1050 while (nqueued < minqueue) {
1051 struct got_object_id *id;
1052 struct got_commit_object *commit;
1053 struct commit_queue_entry *entry;
1054 int errcode;
1056 err = got_commit_graph_iter_next(&id, graph);
1057 if (err) {
1058 if (err->code != GOT_ERR_ITER_NEED_MORE)
1059 break;
1060 err = got_commit_graph_fetch_commits(graph,
1061 minqueue, repo);
1062 if (err)
1063 return err;
1064 continue;
1067 if (id == NULL)
1068 break;
1070 err = got_object_open_as_commit(&commit, repo, id);
1071 if (err)
1072 break;
1073 entry = alloc_commit_queue_entry(commit, id);
1074 if (entry == NULL) {
1075 err = got_error_from_errno();
1076 break;
1079 errcode = pthread_mutex_lock(&tog_mutex);
1080 if (errcode) {
1081 err = got_error_set_errno(errcode);
1082 break;
1085 entry->idx = commits->ncommits;
1086 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1087 nqueued++;
1088 commits->ncommits++;
1090 errcode = pthread_mutex_unlock(&tog_mutex);
1091 if (errcode && err == NULL)
1092 err = got_error_set_errno(errcode);
1095 return err;
1098 static const struct got_error *
1099 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1101 const struct got_error *err = NULL;
1102 struct got_reference *head_ref;
1104 *head_id = NULL;
1106 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1107 if (err)
1108 return err;
1110 err = got_ref_resolve(head_id, repo, head_ref);
1111 got_ref_close(head_ref);
1112 if (err) {
1113 *head_id = NULL;
1114 return err;
1117 return NULL;
1120 static const struct got_error *
1121 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1122 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1123 struct commit_queue *commits, int selected_idx, int limit,
1124 struct got_reflist_head *refs, const char *path, int commits_needed)
1126 const struct got_error *err = NULL;
1127 struct commit_queue_entry *entry;
1128 int ncommits, width;
1129 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1130 char *refs_str = NULL;
1131 wchar_t *wline;
1133 entry = first;
1134 ncommits = 0;
1135 while (entry) {
1136 if (ncommits == selected_idx) {
1137 *selected = entry;
1138 break;
1140 entry = TAILQ_NEXT(entry, entry);
1141 ncommits++;
1144 if (*selected) {
1145 err = got_object_id_str(&id_str, (*selected)->id);
1146 if (err)
1147 return err;
1148 if (refs) {
1149 err = build_refs_str(&refs_str, refs, (*selected)->id);
1150 if (err)
1151 goto done;
1155 if (asprintf(&ncommits_str, " [%d/%d] %s",
1156 entry ? entry->idx + 1 : 0, commits->ncommits,
1157 commits_needed > 0 ? "loading... " :
1158 (refs_str ? refs_str : "")) == -1) {
1159 err = got_error_from_errno();
1160 goto done;
1163 if (path && strcmp(path, "/") != 0) {
1164 if (asprintf(&header, "commit %s %s%s",
1165 id_str ? id_str : "........................................",
1166 path, ncommits_str) == -1) {
1167 err = got_error_from_errno();
1168 header = NULL;
1169 goto done;
1171 } else if (asprintf(&header, "commit %s%s",
1172 id_str ? id_str : "........................................",
1173 ncommits_str) == -1) {
1174 err = got_error_from_errno();
1175 header = NULL;
1176 goto done;
1178 err = format_line(&wline, &width, header, view->ncols);
1179 if (err)
1180 goto done;
1182 werase(view->window);
1184 if (view_needs_focus_indication(view))
1185 wstandout(view->window);
1186 waddwstr(view->window, wline);
1187 while (width < view->ncols) {
1188 waddch(view->window, ' ');
1189 width++;
1191 if (view_needs_focus_indication(view))
1192 wstandend(view->window);
1193 free(wline);
1194 if (limit <= 1)
1195 goto done;
1197 entry = first;
1198 *last = first;
1199 ncommits = 0;
1200 while (entry) {
1201 if (ncommits >= limit - 1)
1202 break;
1203 if (view->focussed && ncommits == selected_idx)
1204 wstandout(view->window);
1205 err = draw_commit(view, entry->commit, entry->id, refs);
1206 if (view->focussed && ncommits == selected_idx)
1207 wstandend(view->window);
1208 if (err)
1209 break;
1210 ncommits++;
1211 *last = entry;
1212 entry = TAILQ_NEXT(entry, entry);
1215 view_vborder(view);
1216 done:
1217 free(id_str);
1218 free(refs_str);
1219 free(ncommits_str);
1220 free(header);
1221 return err;
1224 static void
1225 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1226 struct commit_queue *commits)
1228 struct commit_queue_entry *entry;
1229 int nscrolled = 0;
1231 entry = TAILQ_FIRST(&commits->head);
1232 if (*first_displayed_entry == entry)
1233 return;
1235 entry = *first_displayed_entry;
1236 while (entry && nscrolled < maxscroll) {
1237 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1238 if (entry) {
1239 *first_displayed_entry = entry;
1240 nscrolled++;
1245 static const struct got_error *
1246 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1247 struct commit_queue_entry **last_displayed_entry,
1248 struct commit_queue *commits, int *log_complete, int *commits_needed,
1249 pthread_cond_t *need_commits)
1251 const struct got_error *err = NULL;
1252 struct commit_queue_entry *pentry;
1253 int nscrolled = 0;
1255 if (*last_displayed_entry == NULL)
1256 return NULL;
1258 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1259 if (pentry == NULL && !*log_complete) {
1260 int errcode;
1261 if (*commits_needed > 0)
1262 return NULL;
1263 (*commits_needed) = maxscroll;
1264 errcode = pthread_cond_signal(need_commits);
1265 if (errcode)
1266 return got_error_set_errno(errcode);
1267 errcode = pthread_mutex_unlock(&tog_mutex);
1268 if (errcode)
1269 return got_error_set_errno(errcode);
1270 pthread_yield();
1271 errcode = pthread_mutex_lock(&tog_mutex);
1272 if (errcode)
1273 return got_error_set_errno(errcode);
1274 if (*commits_needed > 0) {
1275 /* Thread is not done yet; lose a key press
1276 * and let the user retry... */
1277 return NULL;
1281 do {
1282 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1283 if (pentry == NULL)
1284 break;
1286 *last_displayed_entry = pentry;
1288 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1289 if (pentry == NULL)
1290 break;
1291 *first_displayed_entry = pentry;
1292 } while (++nscrolled < maxscroll);
1294 return err;
1297 static const struct got_error *
1298 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1299 struct got_commit_object *commit, struct got_object_id *commit_id,
1300 struct tog_view *log_view, struct got_reflist_head *refs,
1301 struct got_repository *repo)
1303 const struct got_error *err;
1304 struct got_object_qid *parent_id;
1305 struct tog_view *diff_view;
1307 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1308 if (diff_view == NULL)
1309 return got_error_from_errno();
1311 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1312 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1313 commit_id, log_view, refs, repo);
1314 if (err == NULL)
1315 *new_view = diff_view;
1316 return err;
1319 static const struct got_error *
1320 browse_commit(struct tog_view **new_view, int begin_x,
1321 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1322 struct got_repository *repo)
1324 const struct got_error *err = NULL;
1325 struct got_tree_object *tree;
1326 struct tog_view *tree_view;
1328 err = got_object_open_as_tree(&tree, repo,
1329 got_object_commit_get_tree_id(entry->commit));
1330 if (err)
1331 return err;
1333 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1334 if (tree_view == NULL)
1335 return got_error_from_errno();
1337 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1338 if (err)
1339 got_object_tree_close(tree);
1340 else
1341 *new_view = tree_view;
1342 return err;
1345 static void *
1346 log_thread(void *arg)
1348 const struct got_error *err = NULL;
1349 int errcode = 0;
1350 struct tog_log_thread_args *a = arg;
1351 int done = 0;
1353 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1354 if (err)
1355 return (void *)err;
1357 while (!done && !err) {
1358 err = queue_commits(a->graph, a->commits, 1, a->repo,
1359 a->in_repo_path);
1360 if (err) {
1361 if (err->code != GOT_ERR_ITER_COMPLETED)
1362 return (void *)err;
1363 err = NULL;
1364 done = 1;
1365 } else if (a->commits_needed > 0)
1366 a->commits_needed--;
1368 errcode = pthread_mutex_lock(&tog_mutex);
1369 if (errcode)
1370 return (void *)got_error_set_errno(errcode);
1372 if (done)
1373 a->log_complete = 1;
1374 else if (*a->quit) {
1375 done = 1;
1376 a->log_complete = 1;
1377 } else if (*a->first_displayed_entry == NULL) {
1378 *a->first_displayed_entry =
1379 TAILQ_FIRST(&a->commits->head);
1380 *a->selected_entry = *a->first_displayed_entry;
1383 if (done)
1384 a->commits_needed = 0;
1385 else if (a->commits_needed == 0) {
1386 errcode = pthread_cond_wait(&a->need_commits,
1387 &tog_mutex);
1388 if (errcode)
1389 err = got_error_set_errno(errcode);
1392 errcode = pthread_mutex_unlock(&tog_mutex);
1393 if (errcode && err == NULL)
1394 err = got_error_set_errno(errcode);
1396 return (void *)err;
1399 static const struct got_error *
1400 stop_log_thread(struct tog_log_view_state *s)
1402 const struct got_error *err = NULL;
1403 int errcode;
1405 if (s->thread) {
1406 s->quit = 1;
1407 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1408 if (errcode)
1409 return got_error_set_errno(errcode);
1410 errcode = pthread_mutex_unlock(&tog_mutex);
1411 if (errcode)
1412 return got_error_set_errno(errcode);
1413 errcode = pthread_join(s->thread, (void **)&err);
1414 if (errcode)
1415 return got_error_set_errno(errcode);
1416 errcode = pthread_mutex_lock(&tog_mutex);
1417 if (errcode)
1418 return got_error_set_errno(errcode);
1419 s->thread = NULL;
1422 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1423 if (errcode && err == NULL)
1424 err = got_error_set_errno(errcode);
1426 if (s->thread_args.repo) {
1427 got_repo_close(s->thread_args.repo);
1428 s->thread_args.repo = NULL;
1431 if (s->thread_args.graph) {
1432 got_commit_graph_close(s->thread_args.graph);
1433 s->thread_args.graph = NULL;
1436 return err;
1439 static const struct got_error *
1440 close_log_view(struct tog_view *view)
1442 const struct got_error *err = NULL;
1443 struct tog_log_view_state *s = &view->state.log;
1445 err = stop_log_thread(s);
1446 free_commits(&s->commits);
1447 free(s->in_repo_path);
1448 s->in_repo_path = NULL;
1449 free(s->start_id);
1450 s->start_id = NULL;
1451 return err;
1454 static const struct got_error *
1455 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1456 struct got_reflist_head *refs, struct got_repository *repo,
1457 const char *path, int check_disk)
1459 const struct got_error *err = NULL;
1460 struct tog_log_view_state *s = &view->state.log;
1461 struct got_repository *thread_repo = NULL;
1462 struct got_commit_graph *thread_graph = NULL;
1463 int errcode;
1465 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1466 if (err != NULL)
1467 goto done;
1469 /* The commit queue only contains commits being displayed. */
1470 TAILQ_INIT(&s->commits.head);
1471 s->commits.ncommits = 0;
1473 s->refs = refs;
1474 s->repo = repo;
1475 s->start_id = got_object_id_dup(start_id);
1476 if (s->start_id == NULL) {
1477 err = got_error_from_errno();
1478 goto done;
1481 view->show = show_log_view;
1482 view->input = input_log_view;
1483 view->close = close_log_view;
1485 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1486 if (err)
1487 goto done;
1488 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1489 0, thread_repo);
1490 if (err)
1491 goto done;
1493 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1494 if (errcode) {
1495 err = got_error_set_errno(errcode);
1496 goto done;
1499 s->thread_args.commits_needed = view->nlines;
1500 s->thread_args.graph = thread_graph;
1501 s->thread_args.commits = &s->commits;
1502 s->thread_args.in_repo_path = s->in_repo_path;
1503 s->thread_args.start_id = s->start_id;
1504 s->thread_args.repo = thread_repo;
1505 s->thread_args.log_complete = 0;
1506 s->thread_args.quit = &s->quit;
1507 s->thread_args.view = view;
1508 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1509 s->thread_args.selected_entry = &s->selected_entry;
1510 done:
1511 if (err)
1512 close_log_view(view);
1513 return err;
1516 static const struct got_error *
1517 show_log_view(struct tog_view *view)
1519 struct tog_log_view_state *s = &view->state.log;
1521 if (s->thread == NULL) {
1522 int errcode = pthread_create(&s->thread, NULL, log_thread,
1523 &s->thread_args);
1524 if (errcode)
1525 return got_error_set_errno(errcode);
1528 return draw_commits(view, &s->last_displayed_entry,
1529 &s->selected_entry, s->first_displayed_entry,
1530 &s->commits, s->selected, view->nlines, s->refs,
1531 s->in_repo_path, s->thread_args.commits_needed);
1534 static const struct got_error *
1535 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1536 struct tog_view **focus_view, struct tog_view *view, int ch)
1538 const struct got_error *err = NULL;
1539 struct tog_log_view_state *s = &view->state.log;
1540 char *parent_path;
1541 struct tog_view *diff_view = NULL, *tree_view = NULL;
1542 int begin_x = 0;
1544 switch (ch) {
1545 case 'q':
1546 s->quit = 1;
1547 break;
1548 case 'k':
1549 case KEY_UP:
1550 case '<':
1551 case ',':
1552 if (s->first_displayed_entry == NULL)
1553 break;
1554 if (s->selected > 0)
1555 s->selected--;
1556 if (s->selected > 0)
1557 break;
1558 scroll_up(&s->first_displayed_entry, 1,
1559 &s->commits);
1560 break;
1561 case KEY_PPAGE:
1562 if (s->first_displayed_entry == NULL)
1563 break;
1564 if (TAILQ_FIRST(&s->commits.head) ==
1565 s->first_displayed_entry) {
1566 s->selected = 0;
1567 break;
1569 scroll_up(&s->first_displayed_entry,
1570 view->nlines, &s->commits);
1571 break;
1572 case 'j':
1573 case KEY_DOWN:
1574 case '>':
1575 case '.':
1576 if (s->first_displayed_entry == NULL)
1577 break;
1578 if (s->selected < MIN(view->nlines - 2,
1579 s->commits.ncommits - 1)) {
1580 s->selected++;
1581 break;
1583 err = scroll_down(&s->first_displayed_entry, 1,
1584 &s->last_displayed_entry, &s->commits,
1585 &s->thread_args.log_complete,
1586 &s->thread_args.commits_needed,
1587 &s->thread_args.need_commits);
1588 break;
1589 case KEY_NPAGE: {
1590 struct commit_queue_entry *first;
1591 first = s->first_displayed_entry;
1592 if (first == NULL)
1593 break;
1594 err = scroll_down(&s->first_displayed_entry,
1595 view->nlines, &s->last_displayed_entry,
1596 &s->commits, &s->thread_args.log_complete,
1597 &s->thread_args.commits_needed,
1598 &s->thread_args.need_commits);
1599 if (first == s->first_displayed_entry &&
1600 s->selected < MIN(view->nlines - 2,
1601 s->commits.ncommits - 1)) {
1602 /* can't scroll further down */
1603 s->selected = MIN(view->nlines - 2,
1604 s->commits.ncommits - 1);
1606 err = NULL;
1607 break;
1609 case KEY_RESIZE:
1610 if (s->selected > view->nlines - 2)
1611 s->selected = view->nlines - 2;
1612 if (s->selected > s->commits.ncommits - 1)
1613 s->selected = s->commits.ncommits - 1;
1614 break;
1615 case KEY_ENTER:
1616 case '\r':
1617 if (s->selected_entry == NULL)
1618 break;
1619 if (view_is_parent_view(view))
1620 begin_x = view_split_begin_x(view->begin_x);
1621 err = open_diff_view_for_commit(&diff_view, begin_x,
1622 s->selected_entry->commit, s->selected_entry->id,
1623 view, s->refs, s->repo);
1624 if (err)
1625 break;
1626 if (view_is_parent_view(view)) {
1627 err = view_close_child(view);
1628 if (err)
1629 return err;
1630 err = view_set_child(view, diff_view);
1631 if (err) {
1632 view_close(diff_view);
1633 break;
1635 *focus_view = diff_view;
1636 view->child_focussed = 1;
1637 } else
1638 *new_view = diff_view;
1639 break;
1640 case 't':
1641 if (s->selected_entry == NULL)
1642 break;
1643 if (view_is_parent_view(view))
1644 begin_x = view_split_begin_x(view->begin_x);
1645 err = browse_commit(&tree_view, begin_x,
1646 s->selected_entry, s->refs, s->repo);
1647 if (err)
1648 break;
1649 if (view_is_parent_view(view)) {
1650 err = view_close_child(view);
1651 if (err)
1652 return err;
1653 err = view_set_child(view, tree_view);
1654 if (err) {
1655 view_close(tree_view);
1656 break;
1658 *focus_view = tree_view;
1659 view->child_focussed = 1;
1660 } else
1661 *new_view = tree_view;
1662 break;
1663 case KEY_BACKSPACE:
1664 if (strcmp(s->in_repo_path, "/") == 0)
1665 break;
1666 parent_path = dirname(s->in_repo_path);
1667 if (parent_path && strcmp(parent_path, ".") != 0) {
1668 struct tog_view *lv;
1669 err = stop_log_thread(s);
1670 if (err)
1671 return err;
1672 lv = view_open(view->nlines, view->ncols,
1673 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1674 if (lv == NULL)
1675 return got_error_from_errno();
1676 err = open_log_view(lv, s->start_id, s->refs,
1677 s->repo, parent_path, 0);
1678 if (err)
1679 return err;;
1680 if (view_is_parent_view(view))
1681 *new_view = lv;
1682 else {
1683 view_set_child(view->parent, lv);
1684 *focus_view = lv;
1686 return NULL;
1688 break;
1689 default:
1690 break;
1693 return err;
1696 static const struct got_error *
1697 apply_unveil(const char *repo_path, const char *worktree_path)
1699 const struct got_error *error;
1701 if (repo_path && unveil(repo_path, "r") != 0)
1702 return got_error_from_errno();
1704 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1705 return got_error_from_errno();
1707 if (unveil("/tmp", "rwc") != 0)
1708 return got_error_from_errno();
1710 error = got_privsep_unveil_exec_helpers();
1711 if (error != NULL)
1712 return error;
1714 if (unveil(NULL, NULL) != 0)
1715 return got_error_from_errno();
1717 return NULL;
1720 static void
1721 init_curses(void)
1723 initscr();
1724 cbreak();
1725 halfdelay(1); /* Do fast refresh while initial view is loading. */
1726 noecho();
1727 nonl();
1728 intrflush(stdscr, FALSE);
1729 keypad(stdscr, TRUE);
1730 curs_set(0);
1731 signal(SIGWINCH, tog_sigwinch);
1734 static const struct got_error *
1735 cmd_log(int argc, char *argv[])
1737 const struct got_error *error;
1738 struct got_repository *repo = NULL;
1739 struct got_reflist_head refs;
1740 struct got_object_id *start_id = NULL;
1741 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1742 char *start_commit = NULL;
1743 int ch;
1744 struct tog_view *view;
1746 #ifndef PROFILE
1747 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1748 NULL) == -1)
1749 err(1, "pledge");
1750 #endif
1752 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1753 switch (ch) {
1754 case 'c':
1755 start_commit = optarg;
1756 break;
1757 case 'r':
1758 repo_path = realpath(optarg, NULL);
1759 if (repo_path == NULL)
1760 err(1, "-r option");
1761 break;
1762 default:
1763 usage();
1764 /* NOTREACHED */
1768 argc -= optind;
1769 argv += optind;
1771 if (argc == 0)
1772 path = strdup("");
1773 else if (argc == 1)
1774 path = strdup(argv[0]);
1775 else
1776 usage_log();
1777 if (path == NULL)
1778 return got_error_from_errno();
1780 cwd = getcwd(NULL, 0);
1781 if (cwd == NULL) {
1782 error = got_error_from_errno();
1783 goto done;
1785 if (repo_path == NULL) {
1786 struct got_worktree *worktree;
1787 error = got_worktree_open(&worktree, cwd);
1788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1789 goto done;
1790 if (worktree) {
1791 repo_path =
1792 strdup(got_worktree_get_repo_path(worktree));
1793 got_worktree_close(worktree);
1794 } else
1795 repo_path = strdup(cwd);
1796 if (repo_path == NULL) {
1797 error = got_error_from_errno();
1798 goto done;
1802 init_curses();
1804 error = apply_unveil(repo_path, NULL);
1805 if (error)
1806 goto done;
1808 error = got_repo_open(&repo, repo_path);
1809 if (error != NULL)
1810 goto done;
1812 if (start_commit == NULL)
1813 error = get_head_commit_id(&start_id, repo);
1814 else
1815 error = got_object_resolve_id_str(&start_id, repo,
1816 start_commit);
1817 if (error != NULL)
1818 goto done;
1820 SIMPLEQ_INIT(&refs);
1821 error = got_ref_list(&refs, repo);
1822 if (error)
1823 goto done;
1825 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1826 if (view == NULL) {
1827 error = got_error_from_errno();
1828 goto done;
1830 error = open_log_view(view, start_id, &refs, repo, path, 1);
1831 if (error)
1832 goto done;
1833 error = view_loop(view);
1834 done:
1835 free(repo_path);
1836 free(cwd);
1837 free(path);
1838 free(start_id);
1839 if (repo)
1840 got_repo_close(repo);
1841 return error;
1844 __dead static void
1845 usage_diff(void)
1847 endwin();
1848 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1849 getprogname());
1850 exit(1);
1853 static char *
1854 parse_next_line(FILE *f, size_t *len)
1856 char *line;
1857 size_t linelen;
1858 size_t lineno;
1859 const char delim[3] = { '\0', '\0', '\0'};
1861 line = fparseln(f, &linelen, &lineno, delim, 0);
1862 if (len)
1863 *len = linelen;
1864 return line;
1867 static const struct got_error *
1868 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1869 int *last_displayed_line, int *eof, int max_lines,
1870 char * header)
1872 const struct got_error *err;
1873 int nlines = 0, nprinted = 0;
1874 char *line;
1875 size_t len;
1876 wchar_t *wline;
1877 int width;
1879 rewind(f);
1880 werase(view->window);
1882 if (header) {
1883 err = format_line(&wline, &width, header, view->ncols);
1884 if (err) {
1885 return err;
1888 if (view_needs_focus_indication(view))
1889 wstandout(view->window);
1890 waddwstr(view->window, wline);
1891 if (view_needs_focus_indication(view))
1892 wstandend(view->window);
1893 if (width < view->ncols)
1894 waddch(view->window, '\n');
1896 if (max_lines <= 1)
1897 return NULL;
1898 max_lines--;
1901 *eof = 0;
1902 while (nprinted < max_lines) {
1903 line = parse_next_line(f, &len);
1904 if (line == NULL) {
1905 *eof = 1;
1906 break;
1908 if (++nlines < *first_displayed_line) {
1909 free(line);
1910 continue;
1913 err = format_line(&wline, &width, line, view->ncols);
1914 if (err) {
1915 free(line);
1916 return err;
1918 waddwstr(view->window, wline);
1919 if (width < view->ncols)
1920 waddch(view->window, '\n');
1921 if (++nprinted == 1)
1922 *first_displayed_line = nlines;
1923 free(line);
1924 free(wline);
1925 wline = NULL;
1927 *last_displayed_line = nlines;
1929 view_vborder(view);
1931 return NULL;
1934 static char *
1935 get_datestr(time_t *time, char *datebuf)
1937 char *p, *s = ctime_r(time, datebuf);
1938 p = strchr(s, '\n');
1939 if (p)
1940 *p = '\0';
1941 return s;
1944 static const struct got_error *
1945 write_commit_info(struct got_object_id *commit_id,
1946 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
1948 const struct got_error *err = NULL;
1949 char datebuf[26];
1950 struct got_commit_object *commit;
1951 char *id_str = NULL;
1952 time_t committer_time;
1953 const char *author, *committer;
1954 char *refs_str = NULL;
1956 if (refs) {
1957 err = build_refs_str(&refs_str, refs, commit_id);
1958 if (err)
1959 return err;
1962 err = got_object_open_as_commit(&commit, repo, commit_id);
1963 if (err)
1964 return err;
1966 err = got_object_id_str(&id_str, commit_id);
1967 if (err) {
1968 err = got_error_from_errno();
1969 goto done;
1972 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1973 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
1974 err = got_error_from_errno();
1975 goto done;
1977 if (fprintf(outfile, "from: %s\n",
1978 got_object_commit_get_author(commit)) < 0) {
1979 err = got_error_from_errno();
1980 goto done;
1982 committer_time = got_object_commit_get_committer_time(commit);
1983 if (fprintf(outfile, "date: %s UTC\n",
1984 get_datestr(&committer_time, datebuf)) < 0) {
1985 err = got_error_from_errno();
1986 goto done;
1988 author = got_object_commit_get_author(commit);
1989 committer = got_object_commit_get_committer(commit);
1990 if (strcmp(author, committer) != 0 &&
1991 fprintf(outfile, "via: %s\n", committer) < 0) {
1992 err = got_error_from_errno();
1993 goto done;
1995 if (fprintf(outfile, "%s\n",
1996 got_object_commit_get_logmsg(commit)) < 0) {
1997 err = got_error_from_errno();
1998 goto done;
2000 done:
2001 free(id_str);
2002 free(refs_str);
2003 got_object_commit_close(commit);
2004 return err;
2007 static const struct got_error *
2008 create_diff(struct tog_diff_view_state *s)
2010 const struct got_error *err = NULL;
2011 FILE *f = NULL;
2012 int obj_type;
2014 f = got_opentemp();
2015 if (f == NULL) {
2016 err = got_error_from_errno();
2017 goto done;
2019 if (s->f && fclose(s->f) != 0) {
2020 err = got_error_from_errno();
2021 goto done;
2023 s->f = f;
2025 if (s->id1)
2026 err = got_object_get_type(&obj_type, s->repo, s->id1);
2027 else
2028 err = got_object_get_type(&obj_type, s->repo, s->id2);
2029 if (err)
2030 goto done;
2032 switch (obj_type) {
2033 case GOT_OBJ_TYPE_BLOB:
2034 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2035 s->diff_context, s->repo, f);
2036 break;
2037 case GOT_OBJ_TYPE_TREE:
2038 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2039 s->diff_context, s->repo, f);
2040 break;
2041 case GOT_OBJ_TYPE_COMMIT: {
2042 const struct got_object_id_queue *parent_ids;
2043 struct got_object_qid *pid;
2044 struct got_commit_object *commit2;
2046 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2047 if (err)
2048 break;
2049 /* Show commit info if we're diffing to a parent/root commit. */
2050 if (s->id1 == NULL)
2051 write_commit_info(s->id2, s->refs, s->repo, f);
2052 else {
2053 parent_ids = got_object_commit_get_parent_ids(commit2);
2054 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2055 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2056 write_commit_info(s->id2, s->refs,
2057 s->repo, f);
2058 break;
2062 got_object_commit_close(commit2);
2064 err = got_diff_objects_as_commits(s->id1, s->id2,
2065 s->diff_context, s->repo, f);
2066 break;
2068 default:
2069 err = got_error(GOT_ERR_OBJ_TYPE);
2070 break;
2072 done:
2073 if (f && fflush(f) != 0 && err == NULL)
2074 err = got_error_from_errno();
2075 return err;
2078 static const struct got_error *
2079 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2080 struct got_object_id *id2, struct tog_view *log_view,
2081 struct got_reflist_head *refs, struct got_repository *repo)
2083 const struct got_error *err;
2085 if (id1 != NULL && id2 != NULL) {
2086 int type1, type2;
2087 err = got_object_get_type(&type1, repo, id1);
2088 if (err)
2089 return err;
2090 err = got_object_get_type(&type2, repo, id2);
2091 if (err)
2092 return err;
2094 if (type1 != type2)
2095 return got_error(GOT_ERR_OBJ_TYPE);
2098 if (id1) {
2099 view->state.diff.id1 = got_object_id_dup(id1);
2100 if (view->state.diff.id1 == NULL)
2101 return got_error_from_errno();
2102 } else
2103 view->state.diff.id1 = NULL;
2105 view->state.diff.id2 = got_object_id_dup(id2);
2106 if (view->state.diff.id2 == NULL) {
2107 free(view->state.diff.id1);
2108 view->state.diff.id1 = NULL;
2109 return got_error_from_errno();
2111 view->state.diff.f = NULL;
2112 view->state.diff.first_displayed_line = 1;
2113 view->state.diff.last_displayed_line = view->nlines;
2114 view->state.diff.diff_context = 3;
2115 view->state.diff.log_view = log_view;
2116 view->state.diff.repo = repo;
2117 view->state.diff.refs = refs;
2119 err = create_diff(&view->state.diff);
2120 if (err) {
2121 free(view->state.diff.id1);
2122 view->state.diff.id1 = NULL;
2123 free(view->state.diff.id2);
2124 view->state.diff.id2 = NULL;
2125 return err;
2128 view->show = show_diff_view;
2129 view->input = input_diff_view;
2130 view->close = close_diff_view;
2132 return NULL;
2135 static const struct got_error *
2136 close_diff_view(struct tog_view *view)
2138 const struct got_error *err = NULL;
2140 free(view->state.diff.id1);
2141 view->state.diff.id1 = NULL;
2142 free(view->state.diff.id2);
2143 view->state.diff.id2 = NULL;
2144 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2145 err = got_error_from_errno();
2146 return err;
2149 static const struct got_error *
2150 show_diff_view(struct tog_view *view)
2152 const struct got_error *err;
2153 struct tog_diff_view_state *s = &view->state.diff;
2154 char *id_str1 = NULL, *id_str2, *header;
2156 if (s->id1) {
2157 err = got_object_id_str(&id_str1, s->id1);
2158 if (err)
2159 return err;
2161 err = got_object_id_str(&id_str2, s->id2);
2162 if (err)
2163 return err;
2165 if (asprintf(&header, "diff %s %s",
2166 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2167 err = got_error_from_errno();
2168 free(id_str1);
2169 free(id_str2);
2170 return err;
2172 free(id_str1);
2173 free(id_str2);
2175 return draw_file(view, s->f, &s->first_displayed_line,
2176 &s->last_displayed_line, &s->eof, view->nlines,
2177 header);
2180 static const struct got_error *
2181 set_selected_commit(struct tog_diff_view_state *s,
2182 struct commit_queue_entry *entry)
2184 const struct got_error *err;
2185 const struct got_object_id_queue *parent_ids;
2186 struct got_commit_object *selected_commit;
2187 struct got_object_qid *pid;
2189 free(s->id2);
2190 s->id2 = got_object_id_dup(entry->id);
2191 if (s->id2 == NULL)
2192 return got_error_from_errno();
2194 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2195 if (err)
2196 return err;
2197 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2198 free(s->id1);
2199 pid = SIMPLEQ_FIRST(parent_ids);
2200 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2201 got_object_commit_close(selected_commit);
2202 return NULL;
2205 static const struct got_error *
2206 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2207 struct tog_view **focus_view, struct tog_view *view, int ch)
2209 const struct got_error *err = NULL;
2210 struct tog_diff_view_state *s = &view->state.diff;
2211 struct tog_log_view_state *ls;
2212 struct commit_queue_entry *entry;
2213 int i;
2215 switch (ch) {
2216 case 'k':
2217 case KEY_UP:
2218 if (s->first_displayed_line > 1)
2219 s->first_displayed_line--;
2220 break;
2221 case KEY_PPAGE:
2222 i = 0;
2223 while (i++ < view->nlines - 1 &&
2224 s->first_displayed_line > 1)
2225 s->first_displayed_line--;
2226 break;
2227 case 'j':
2228 case KEY_DOWN:
2229 if (!s->eof)
2230 s->first_displayed_line++;
2231 break;
2232 case KEY_NPAGE:
2233 case ' ':
2234 i = 0;
2235 while (!s->eof && i++ < view->nlines - 1) {
2236 char *line;
2237 line = parse_next_line(s->f, NULL);
2238 s->first_displayed_line++;
2239 if (line == NULL)
2240 break;
2242 break;
2243 case '[':
2244 if (s->diff_context > 0) {
2245 s->diff_context--;
2246 err = create_diff(s);
2248 break;
2249 case ']':
2250 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2251 s->diff_context++;
2252 err = create_diff(s);
2254 break;
2255 case '<':
2256 case ',':
2257 if (s->log_view == NULL)
2258 break;
2259 ls = &s->log_view->state.log;
2260 entry = TAILQ_PREV(ls->selected_entry,
2261 commit_queue_head, entry);
2262 if (entry == NULL)
2263 break;
2265 err = input_log_view(NULL, NULL, NULL, s->log_view,
2266 KEY_UP);
2267 if (err)
2268 break;
2270 err = set_selected_commit(s, entry);
2271 if (err)
2272 break;
2274 s->first_displayed_line = 1;
2275 s->last_displayed_line = view->nlines;
2277 err = create_diff(s);
2278 break;
2279 case '>':
2280 case '.':
2281 if (s->log_view == NULL)
2282 break;
2283 ls = &s->log_view->state.log;
2284 err = input_log_view(NULL, NULL, NULL, s->log_view,
2285 KEY_DOWN);
2286 if (err)
2287 break;
2289 entry = TAILQ_NEXT(ls->selected_entry, entry);
2290 if (entry == NULL)
2291 break;
2293 err = set_selected_commit(s, entry);
2294 if (err)
2295 break;
2297 s->first_displayed_line = 1;
2298 s->last_displayed_line = view->nlines;
2300 err = create_diff(s);
2301 break;
2302 default:
2303 break;
2306 return err;
2309 static const struct got_error *
2310 cmd_diff(int argc, char *argv[])
2312 const struct got_error *error = NULL;
2313 struct got_repository *repo = NULL;
2314 struct got_reflist_head refs;
2315 struct got_object_id *id1 = NULL, *id2 = NULL;
2316 char *repo_path = NULL;
2317 char *id_str1 = NULL, *id_str2 = NULL;
2318 int ch;
2319 struct tog_view *view;
2321 #ifndef PROFILE
2322 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2323 NULL) == -1)
2324 err(1, "pledge");
2325 #endif
2327 while ((ch = getopt(argc, argv, "")) != -1) {
2328 switch (ch) {
2329 default:
2330 usage();
2331 /* NOTREACHED */
2335 argc -= optind;
2336 argv += optind;
2338 if (argc == 0) {
2339 usage_diff(); /* TODO show local worktree changes */
2340 } else if (argc == 2) {
2341 repo_path = getcwd(NULL, 0);
2342 if (repo_path == NULL)
2343 return got_error_from_errno();
2344 id_str1 = argv[0];
2345 id_str2 = argv[1];
2346 } else if (argc == 3) {
2347 repo_path = realpath(argv[0], NULL);
2348 if (repo_path == NULL)
2349 return got_error_from_errno();
2350 id_str1 = argv[1];
2351 id_str2 = argv[2];
2352 } else
2353 usage_diff();
2355 init_curses();
2357 error = apply_unveil(repo_path, NULL);
2358 if (error)
2359 goto done;
2361 error = got_repo_open(&repo, repo_path);
2362 free(repo_path);
2363 if (error)
2364 goto done;
2366 error = got_object_resolve_id_str(&id1, repo, id_str1);
2367 if (error)
2368 goto done;
2370 error = got_object_resolve_id_str(&id2, repo, id_str2);
2371 if (error)
2372 goto done;
2374 SIMPLEQ_INIT(&refs);
2375 error = got_ref_list(&refs, repo);
2376 if (error)
2377 goto done;
2379 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2380 if (view == NULL) {
2381 error = got_error_from_errno();
2382 goto done;
2384 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2385 if (error)
2386 goto done;
2387 error = view_loop(view);
2388 done:
2389 got_repo_close(repo);
2390 return error;
2393 __dead static void
2394 usage_blame(void)
2396 endwin();
2397 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2398 getprogname());
2399 exit(1);
2402 struct tog_blame_line {
2403 int annotated;
2404 struct got_object_id *id;
2407 static const struct got_error *
2408 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2409 const char *path, struct tog_blame_line *lines, int nlines,
2410 int blame_complete, int selected_line, int *first_displayed_line,
2411 int *last_displayed_line, int *eof, int max_lines)
2413 const struct got_error *err;
2414 int lineno = 0, nprinted = 0;
2415 char *line;
2416 size_t len;
2417 wchar_t *wline;
2418 int width, wlimit;
2419 struct tog_blame_line *blame_line;
2420 struct got_object_id *prev_id = NULL;
2421 char *id_str;
2423 err = got_object_id_str(&id_str, id);
2424 if (err)
2425 return err;
2427 rewind(f);
2428 werase(view->window);
2430 if (asprintf(&line, "commit %s", id_str) == -1) {
2431 err = got_error_from_errno();
2432 free(id_str);
2433 return err;
2436 err = format_line(&wline, &width, line, view->ncols);
2437 free(line);
2438 line = NULL;
2439 if (view_needs_focus_indication(view))
2440 wstandout(view->window);
2441 waddwstr(view->window, wline);
2442 if (view_needs_focus_indication(view))
2443 wstandend(view->window);
2444 free(wline);
2445 wline = NULL;
2446 if (width < view->ncols)
2447 waddch(view->window, '\n');
2449 if (asprintf(&line, "[%d/%d] %s%s",
2450 *first_displayed_line - 1 + selected_line, nlines,
2451 blame_complete ? "" : "annotating... ", path) == -1) {
2452 free(id_str);
2453 return got_error_from_errno();
2455 free(id_str);
2456 err = format_line(&wline, &width, line, view->ncols);
2457 free(line);
2458 line = NULL;
2459 if (err)
2460 return err;
2461 waddwstr(view->window, wline);
2462 free(wline);
2463 wline = NULL;
2464 if (width < view->ncols)
2465 waddch(view->window, '\n');
2467 *eof = 0;
2468 while (nprinted < max_lines - 2) {
2469 line = parse_next_line(f, &len);
2470 if (line == NULL) {
2471 *eof = 1;
2472 break;
2474 if (++lineno < *first_displayed_line) {
2475 free(line);
2476 continue;
2479 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2480 err = format_line(&wline, &width, line, wlimit);
2481 if (err) {
2482 free(line);
2483 return err;
2486 if (view->focussed && nprinted == selected_line - 1)
2487 wstandout(view->window);
2489 blame_line = &lines[lineno - 1];
2490 if (blame_line->annotated && prev_id &&
2491 got_object_id_cmp(prev_id, blame_line->id) == 0)
2492 waddstr(view->window, " ");
2493 else if (blame_line->annotated) {
2494 char *id_str;
2495 err = got_object_id_str(&id_str, blame_line->id);
2496 if (err) {
2497 free(line);
2498 free(wline);
2499 return err;
2501 wprintw(view->window, "%.8s ", id_str);
2502 free(id_str);
2503 prev_id = blame_line->id;
2504 } else {
2505 waddstr(view->window, "........ ");
2506 prev_id = NULL;
2509 waddwstr(view->window, wline);
2510 while (width < wlimit) {
2511 waddch(view->window, ' ');
2512 width++;
2514 if (view->focussed && nprinted == selected_line - 1)
2515 wstandend(view->window);
2516 if (++nprinted == 1)
2517 *first_displayed_line = lineno;
2518 free(line);
2519 free(wline);
2520 wline = NULL;
2522 *last_displayed_line = lineno;
2524 view_vborder(view);
2526 return NULL;
2529 static const struct got_error *
2530 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2532 const struct got_error *err = NULL;
2533 struct tog_blame_cb_args *a = arg;
2534 struct tog_blame_line *line;
2535 int errcode;
2537 if (nlines != a->nlines ||
2538 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2539 return got_error(GOT_ERR_RANGE);
2541 errcode = pthread_mutex_lock(&tog_mutex);
2542 if (errcode)
2543 return got_error_set_errno(errcode);
2545 if (*a->quit) { /* user has quit the blame view */
2546 err = got_error(GOT_ERR_ITER_COMPLETED);
2547 goto done;
2550 if (lineno == -1)
2551 goto done; /* no change in this commit */
2553 line = &a->lines[lineno - 1];
2554 if (line->annotated)
2555 goto done;
2557 line->id = got_object_id_dup(id);
2558 if (line->id == NULL) {
2559 err = got_error_from_errno();
2560 goto done;
2562 line->annotated = 1;
2563 done:
2564 errcode = pthread_mutex_unlock(&tog_mutex);
2565 if (errcode)
2566 err = got_error_set_errno(errcode);
2567 return err;
2570 static void *
2571 blame_thread(void *arg)
2573 const struct got_error *err;
2574 struct tog_blame_thread_args *ta = arg;
2575 struct tog_blame_cb_args *a = ta->cb_args;
2576 int errcode;
2578 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2579 blame_cb, ta->cb_args);
2581 errcode = pthread_mutex_lock(&tog_mutex);
2582 if (errcode)
2583 return (void *)got_error_set_errno(errcode);
2585 got_repo_close(ta->repo);
2586 ta->repo = NULL;
2587 *ta->complete = 1;
2589 errcode = pthread_mutex_unlock(&tog_mutex);
2590 if (errcode && err == NULL)
2591 err = got_error_set_errno(errcode);
2593 return (void *)err;
2596 static struct got_object_id *
2597 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2598 int selected_line)
2600 struct tog_blame_line *line;
2602 line = &lines[first_displayed_line - 1 + selected_line - 1];
2603 if (!line->annotated)
2604 return NULL;
2606 return line->id;
2609 static const struct got_error *
2610 stop_blame(struct tog_blame *blame)
2612 const struct got_error *err = NULL;
2613 int i;
2615 if (blame->thread) {
2616 int errcode;
2617 errcode = pthread_mutex_unlock(&tog_mutex);
2618 if (errcode)
2619 return got_error_set_errno(errcode);
2620 errcode = pthread_join(blame->thread, (void **)&err);
2621 if (errcode)
2622 return got_error_set_errno(errcode);
2623 errcode = pthread_mutex_lock(&tog_mutex);
2624 if (errcode)
2625 return got_error_set_errno(errcode);
2626 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2627 err = NULL;
2628 blame->thread = NULL;
2630 if (blame->thread_args.repo) {
2631 got_repo_close(blame->thread_args.repo);
2632 blame->thread_args.repo = NULL;
2634 if (blame->f) {
2635 if (fclose(blame->f) != 0 && err == NULL)
2636 err = got_error_from_errno();
2637 blame->f = NULL;
2639 if (blame->lines) {
2640 for (i = 0; i < blame->nlines; i++)
2641 free(blame->lines[i].id);
2642 free(blame->lines);
2643 blame->lines = NULL;
2645 free(blame->cb_args.commit_id);
2646 blame->cb_args.commit_id = NULL;
2648 return err;
2651 static const struct got_error *
2652 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2653 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2654 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2655 struct got_repository *repo)
2657 const struct got_error *err = NULL;
2658 struct got_blob_object *blob = NULL;
2659 struct got_repository *thread_repo = NULL;
2660 struct got_object_id *obj_id = NULL;
2661 int obj_type;
2663 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2664 if (err)
2665 return err;
2666 if (obj_id == NULL)
2667 return got_error(GOT_ERR_NO_OBJ);
2669 err = got_object_get_type(&obj_type, repo, obj_id);
2670 if (err)
2671 goto done;
2673 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2674 err = got_error(GOT_ERR_OBJ_TYPE);
2675 goto done;
2678 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2679 if (err)
2680 goto done;
2681 blame->f = got_opentemp();
2682 if (blame->f == NULL) {
2683 err = got_error_from_errno();
2684 goto done;
2686 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2687 blame->f, blob);
2688 if (err)
2689 goto done;
2691 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2692 if (blame->lines == NULL) {
2693 err = got_error_from_errno();
2694 goto done;
2697 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2698 if (err)
2699 goto done;
2701 blame->cb_args.view = view;
2702 blame->cb_args.lines = blame->lines;
2703 blame->cb_args.nlines = blame->nlines;
2704 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2705 if (blame->cb_args.commit_id == NULL) {
2706 err = got_error_from_errno();
2707 goto done;
2709 blame->cb_args.quit = done;
2711 blame->thread_args.path = path;
2712 blame->thread_args.repo = thread_repo;
2713 blame->thread_args.cb_args = &blame->cb_args;
2714 blame->thread_args.complete = blame_complete;
2715 *blame_complete = 0;
2717 done:
2718 if (blob)
2719 got_object_blob_close(blob);
2720 free(obj_id);
2721 if (err)
2722 stop_blame(blame);
2723 return err;
2726 static const struct got_error *
2727 open_blame_view(struct tog_view *view, char *path,
2728 struct got_object_id *commit_id, struct got_reflist_head *refs,
2729 struct got_repository *repo)
2731 const struct got_error *err = NULL;
2732 struct tog_blame_view_state *s = &view->state.blame;
2734 SIMPLEQ_INIT(&s->blamed_commits);
2736 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2737 if (err)
2738 return err;
2740 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2741 s->first_displayed_line = 1;
2742 s->last_displayed_line = view->nlines;
2743 s->selected_line = 1;
2744 s->blame_complete = 0;
2745 s->path = path;
2746 if (s->path == NULL)
2747 return got_error_from_errno();
2748 s->repo = repo;
2749 s->refs = refs;
2750 s->commit_id = commit_id;
2751 memset(&s->blame, 0, sizeof(s->blame));
2753 view->show = show_blame_view;
2754 view->input = input_blame_view;
2755 view->close = close_blame_view;
2757 return run_blame(&s->blame, view, &s->blame_complete,
2758 &s->first_displayed_line, &s->last_displayed_line,
2759 &s->selected_line, &s->done, &s->eof, s->path,
2760 s->blamed_commit->id, s->repo);
2763 static const struct got_error *
2764 close_blame_view(struct tog_view *view)
2766 const struct got_error *err = NULL;
2767 struct tog_blame_view_state *s = &view->state.blame;
2769 if (s->blame.thread)
2770 err = stop_blame(&s->blame);
2772 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2773 struct got_object_qid *blamed_commit;
2774 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2775 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2776 got_object_qid_free(blamed_commit);
2779 free(s->path);
2781 return err;
2784 static const struct got_error *
2785 show_blame_view(struct tog_view *view)
2787 const struct got_error *err = NULL;
2788 struct tog_blame_view_state *s = &view->state.blame;
2789 int errcode;
2791 if (s->blame.thread == NULL) {
2792 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2793 &s->blame.thread_args);
2794 if (errcode)
2795 return got_error_set_errno(errcode);
2798 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2799 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2800 s->selected_line, &s->first_displayed_line,
2801 &s->last_displayed_line, &s->eof, view->nlines);
2803 view_vborder(view);
2804 return err;
2807 static const struct got_error *
2808 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2809 struct tog_view **focus_view, struct tog_view *view, int ch)
2811 const struct got_error *err = NULL, *thread_err = NULL;
2812 struct tog_view *diff_view;
2813 struct tog_blame_view_state *s = &view->state.blame;
2814 int begin_x = 0;
2816 switch (ch) {
2817 case 'q':
2818 s->done = 1;
2819 break;
2820 case 'k':
2821 case KEY_UP:
2822 if (s->selected_line > 1)
2823 s->selected_line--;
2824 else if (s->selected_line == 1 &&
2825 s->first_displayed_line > 1)
2826 s->first_displayed_line--;
2827 break;
2828 case KEY_PPAGE:
2829 if (s->first_displayed_line == 1) {
2830 s->selected_line = 1;
2831 break;
2833 if (s->first_displayed_line > view->nlines - 2)
2834 s->first_displayed_line -=
2835 (view->nlines - 2);
2836 else
2837 s->first_displayed_line = 1;
2838 break;
2839 case 'j':
2840 case KEY_DOWN:
2841 if (s->selected_line < view->nlines - 2 &&
2842 s->first_displayed_line +
2843 s->selected_line <= s->blame.nlines)
2844 s->selected_line++;
2845 else if (s->last_displayed_line <
2846 s->blame.nlines)
2847 s->first_displayed_line++;
2848 break;
2849 case 'b':
2850 case 'p': {
2851 struct got_object_id *id = NULL;
2852 id = get_selected_commit_id(s->blame.lines,
2853 s->first_displayed_line, s->selected_line);
2854 if (id == NULL)
2855 break;
2856 if (ch == 'p') {
2857 struct got_commit_object *commit;
2858 struct got_object_qid *pid;
2859 struct got_object_id *blob_id = NULL;
2860 int obj_type;
2861 err = got_object_open_as_commit(&commit,
2862 s->repo, id);
2863 if (err)
2864 break;
2865 pid = SIMPLEQ_FIRST(
2866 got_object_commit_get_parent_ids(commit));
2867 if (pid == NULL) {
2868 got_object_commit_close(commit);
2869 break;
2871 /* Check if path history ends here. */
2872 err = got_object_id_by_path(&blob_id, s->repo,
2873 pid->id, s->path);
2874 if (err) {
2875 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2876 err = NULL;
2877 got_object_commit_close(commit);
2878 break;
2880 err = got_object_get_type(&obj_type, s->repo,
2881 blob_id);
2882 free(blob_id);
2883 /* Can't blame non-blob type objects. */
2884 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2885 got_object_commit_close(commit);
2886 break;
2888 err = got_object_qid_alloc(&s->blamed_commit,
2889 pid->id);
2890 got_object_commit_close(commit);
2891 } else {
2892 if (got_object_id_cmp(id,
2893 s->blamed_commit->id) == 0)
2894 break;
2895 err = got_object_qid_alloc(&s->blamed_commit,
2896 id);
2898 if (err)
2899 break;
2900 s->done = 1;
2901 thread_err = stop_blame(&s->blame);
2902 s->done = 0;
2903 if (thread_err)
2904 break;
2905 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2906 s->blamed_commit, entry);
2907 err = run_blame(&s->blame, view, &s->blame_complete,
2908 &s->first_displayed_line, &s->last_displayed_line,
2909 &s->selected_line, &s->done, &s->eof,
2910 s->path, s->blamed_commit->id, s->repo);
2911 if (err)
2912 break;
2913 break;
2915 case 'B': {
2916 struct got_object_qid *first;
2917 first = SIMPLEQ_FIRST(&s->blamed_commits);
2918 if (!got_object_id_cmp(first->id, s->commit_id))
2919 break;
2920 s->done = 1;
2921 thread_err = stop_blame(&s->blame);
2922 s->done = 0;
2923 if (thread_err)
2924 break;
2925 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2926 got_object_qid_free(s->blamed_commit);
2927 s->blamed_commit =
2928 SIMPLEQ_FIRST(&s->blamed_commits);
2929 err = run_blame(&s->blame, view, &s->blame_complete,
2930 &s->first_displayed_line, &s->last_displayed_line,
2931 &s->selected_line, &s->done, &s->eof, s->path,
2932 s->blamed_commit->id, s->repo);
2933 if (err)
2934 break;
2935 break;
2937 case KEY_ENTER:
2938 case '\r': {
2939 struct got_object_id *id = NULL;
2940 struct got_object_qid *pid;
2941 struct got_commit_object *commit = NULL;
2942 id = get_selected_commit_id(s->blame.lines,
2943 s->first_displayed_line, s->selected_line);
2944 if (id == NULL)
2945 break;
2946 err = got_object_open_as_commit(&commit, s->repo, id);
2947 if (err)
2948 break;
2949 pid = SIMPLEQ_FIRST(
2950 got_object_commit_get_parent_ids(commit));
2951 if (view_is_parent_view(view))
2952 begin_x = view_split_begin_x(view->begin_x);
2953 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2954 if (diff_view == NULL) {
2955 got_object_commit_close(commit);
2956 err = got_error_from_errno();
2957 break;
2959 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2960 id, NULL, s->refs, s->repo);
2961 got_object_commit_close(commit);
2962 if (err) {
2963 view_close(diff_view);
2964 break;
2966 if (view_is_parent_view(view)) {
2967 err = view_close_child(view);
2968 if (err)
2969 break;
2970 err = view_set_child(view, diff_view);
2971 if (err) {
2972 view_close(diff_view);
2973 break;
2975 *focus_view = diff_view;
2976 view->child_focussed = 1;
2977 } else
2978 *new_view = diff_view;
2979 if (err)
2980 break;
2981 break;
2983 case KEY_NPAGE:
2984 case ' ':
2985 if (s->last_displayed_line >= s->blame.nlines &&
2986 s->selected_line < view->nlines - 2) {
2987 s->selected_line = MIN(s->blame.nlines,
2988 view->nlines - 2);
2989 break;
2991 if (s->last_displayed_line + view->nlines - 2
2992 <= s->blame.nlines)
2993 s->first_displayed_line +=
2994 view->nlines - 2;
2995 else
2996 s->first_displayed_line =
2997 s->blame.nlines -
2998 (view->nlines - 3);
2999 break;
3000 case KEY_RESIZE:
3001 if (s->selected_line > view->nlines - 2) {
3002 s->selected_line = MIN(s->blame.nlines,
3003 view->nlines - 2);
3005 break;
3006 default:
3007 break;
3009 return thread_err ? thread_err : err;
3012 static const struct got_error *
3013 cmd_blame(int argc, char *argv[])
3015 const struct got_error *error;
3016 struct got_repository *repo = NULL;
3017 struct got_reflist_head refs;
3018 struct got_worktree *worktree = NULL;
3019 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3020 struct got_object_id *commit_id = NULL;
3021 char *commit_id_str = NULL;
3022 int ch;
3023 struct tog_view *view;
3025 #ifndef PROFILE
3026 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3027 NULL) == -1)
3028 err(1, "pledge");
3029 #endif
3031 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3032 switch (ch) {
3033 case 'c':
3034 commit_id_str = optarg;
3035 break;
3036 case 'r':
3037 repo_path = realpath(optarg, NULL);
3038 if (repo_path == NULL)
3039 err(1, "-r option");
3040 break;
3041 default:
3042 usage();
3043 /* NOTREACHED */
3047 argc -= optind;
3048 argv += optind;
3050 if (argc == 1)
3051 path = argv[0];
3052 else
3053 usage_blame();
3055 cwd = getcwd(NULL, 0);
3056 if (cwd == NULL) {
3057 error = got_error_from_errno();
3058 goto done;
3060 if (repo_path == NULL) {
3061 error = got_worktree_open(&worktree, cwd);
3062 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3063 goto done;
3064 else
3065 error = NULL;
3066 if (worktree) {
3067 repo_path =
3068 strdup(got_worktree_get_repo_path(worktree));
3069 if (repo_path == NULL)
3070 error = got_error_from_errno();
3071 if (error)
3072 goto done;
3073 } else {
3074 repo_path = strdup(cwd);
3075 if (repo_path == NULL) {
3076 error = got_error_from_errno();
3077 goto done;
3082 init_curses();
3084 error = apply_unveil(repo_path, NULL);
3085 if (error)
3086 goto done;
3088 error = got_repo_open(&repo, repo_path);
3089 if (error != NULL)
3090 goto done;
3092 if (worktree) {
3093 const char *prefix = got_worktree_get_path_prefix(worktree);
3094 char *p, *worktree_subdir = cwd +
3095 strlen(got_worktree_get_root_path(worktree));
3096 if (asprintf(&p, "%s%s%s%s%s",
3097 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3098 worktree_subdir, worktree_subdir[0] ? "/" : "",
3099 path) == -1) {
3100 error = got_error_from_errno();
3101 goto done;
3103 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3104 free(p);
3105 } else {
3106 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3108 if (error)
3109 goto done;
3111 if (commit_id_str == NULL) {
3112 struct got_reference *head_ref;
3113 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3114 if (error != NULL)
3115 goto done;
3116 error = got_ref_resolve(&commit_id, repo, head_ref);
3117 got_ref_close(head_ref);
3118 } else {
3119 error = got_object_resolve_id_str(&commit_id, repo,
3120 commit_id_str);
3122 if (error != NULL)
3123 goto done;
3125 SIMPLEQ_INIT(&refs);
3126 error = got_ref_list(&refs, repo);
3127 if (error)
3128 goto done;
3130 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3131 if (view == NULL) {
3132 error = got_error_from_errno();
3133 goto done;
3135 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3136 if (error)
3137 goto done;
3138 error = view_loop(view);
3139 done:
3140 free(repo_path);
3141 free(cwd);
3142 free(commit_id);
3143 if (worktree)
3144 got_worktree_close(worktree);
3145 if (repo)
3146 got_repo_close(repo);
3147 return error;
3150 static const struct got_error *
3151 draw_tree_entries(struct tog_view *view,
3152 struct got_tree_entry **first_displayed_entry,
3153 struct got_tree_entry **last_displayed_entry,
3154 struct got_tree_entry **selected_entry, int *ndisplayed,
3155 const char *label, int show_ids, const char *parent_path,
3156 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3158 const struct got_error *err = NULL;
3159 struct got_tree_entry *te;
3160 wchar_t *wline;
3161 int width, n;
3163 *ndisplayed = 0;
3165 werase(view->window);
3167 if (limit == 0)
3168 return NULL;
3170 err = format_line(&wline, &width, label, view->ncols);
3171 if (err)
3172 return err;
3173 if (view_needs_focus_indication(view))
3174 wstandout(view->window);
3175 waddwstr(view->window, wline);
3176 if (view_needs_focus_indication(view))
3177 wstandend(view->window);
3178 free(wline);
3179 wline = NULL;
3180 if (width < view->ncols)
3181 waddch(view->window, '\n');
3182 if (--limit <= 0)
3183 return NULL;
3184 err = format_line(&wline, &width, parent_path, view->ncols);
3185 if (err)
3186 return err;
3187 waddwstr(view->window, wline);
3188 free(wline);
3189 wline = NULL;
3190 if (width < view->ncols)
3191 waddch(view->window, '\n');
3192 if (--limit <= 0)
3193 return NULL;
3194 waddch(view->window, '\n');
3195 if (--limit <= 0)
3196 return NULL;
3198 te = SIMPLEQ_FIRST(&entries->head);
3199 if (*first_displayed_entry == NULL) {
3200 if (selected == 0) {
3201 if (view->focussed)
3202 wstandout(view->window);
3203 *selected_entry = NULL;
3205 waddstr(view->window, " ..\n"); /* parent directory */
3206 if (selected == 0 && view->focussed)
3207 wstandend(view->window);
3208 (*ndisplayed)++;
3209 if (--limit <= 0)
3210 return NULL;
3211 n = 1;
3212 } else {
3213 n = 0;
3214 while (te != *first_displayed_entry)
3215 te = SIMPLEQ_NEXT(te, entry);
3218 while (te) {
3219 char *line = NULL, *id_str = NULL;
3221 if (show_ids) {
3222 err = got_object_id_str(&id_str, te->id);
3223 if (err)
3224 return got_error_from_errno();
3226 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3227 te->name, S_ISDIR(te->mode) ? "/" :
3228 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3229 free(id_str);
3230 return got_error_from_errno();
3232 free(id_str);
3233 err = format_line(&wline, &width, line, view->ncols);
3234 if (err) {
3235 free(line);
3236 break;
3238 if (n == selected) {
3239 if (view->focussed)
3240 wstandout(view->window);
3241 *selected_entry = te;
3243 waddwstr(view->window, wline);
3244 if (width < view->ncols)
3245 waddch(view->window, '\n');
3246 if (n == selected && view->focussed)
3247 wstandend(view->window);
3248 free(line);
3249 free(wline);
3250 wline = NULL;
3251 n++;
3252 (*ndisplayed)++;
3253 *last_displayed_entry = te;
3254 if (--limit <= 0)
3255 break;
3256 te = SIMPLEQ_NEXT(te, entry);
3259 return err;
3262 static void
3263 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3264 const struct got_tree_entries *entries, int isroot)
3266 struct got_tree_entry *te, *prev;
3267 int i;
3269 if (*first_displayed_entry == NULL)
3270 return;
3272 te = SIMPLEQ_FIRST(&entries->head);
3273 if (*first_displayed_entry == te) {
3274 if (!isroot)
3275 *first_displayed_entry = NULL;
3276 return;
3279 /* XXX this is stupid... switch to TAILQ? */
3280 for (i = 0; i < maxscroll; i++) {
3281 while (te != *first_displayed_entry) {
3282 prev = te;
3283 te = SIMPLEQ_NEXT(te, entry);
3285 *first_displayed_entry = prev;
3286 te = SIMPLEQ_FIRST(&entries->head);
3288 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3289 *first_displayed_entry = NULL;
3292 static int
3293 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3294 struct got_tree_entry *last_displayed_entry,
3295 const struct got_tree_entries *entries)
3297 struct got_tree_entry *next, *last;
3298 int n = 0;
3300 if (*first_displayed_entry)
3301 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3302 else
3303 next = SIMPLEQ_FIRST(&entries->head);
3304 last = last_displayed_entry;
3305 while (next && last && n++ < maxscroll) {
3306 last = SIMPLEQ_NEXT(last, entry);
3307 if (last) {
3308 *first_displayed_entry = next;
3309 next = SIMPLEQ_NEXT(next, entry);
3312 return n;
3315 static const struct got_error *
3316 tree_entry_path(char **path, struct tog_parent_trees *parents,
3317 struct got_tree_entry *te)
3319 const struct got_error *err = NULL;
3320 struct tog_parent_tree *pt;
3321 size_t len = 2; /* for leading slash and NUL */
3323 TAILQ_FOREACH(pt, parents, entry)
3324 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3325 if (te)
3326 len += strlen(te->name);
3328 *path = calloc(1, len);
3329 if (path == NULL)
3330 return got_error_from_errno();
3332 (*path)[0] = '/';
3333 pt = TAILQ_LAST(parents, tog_parent_trees);
3334 while (pt) {
3335 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3336 err = got_error(GOT_ERR_NO_SPACE);
3337 goto done;
3339 if (strlcat(*path, "/", len) >= len) {
3340 err = got_error(GOT_ERR_NO_SPACE);
3341 goto done;
3343 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3345 if (te) {
3346 if (strlcat(*path, te->name, len) >= len) {
3347 err = got_error(GOT_ERR_NO_SPACE);
3348 goto done;
3351 done:
3352 if (err) {
3353 free(*path);
3354 *path = NULL;
3356 return err;
3359 static const struct got_error *
3360 blame_tree_entry(struct tog_view **new_view, int begin_x,
3361 struct got_tree_entry *te, struct tog_parent_trees *parents,
3362 struct got_object_id *commit_id, struct got_reflist_head *refs,
3363 struct got_repository *repo)
3365 const struct got_error *err = NULL;
3366 char *path;
3367 struct tog_view *blame_view;
3369 err = tree_entry_path(&path, parents, te);
3370 if (err)
3371 return err;
3373 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3374 if (blame_view == NULL)
3375 return got_error_from_errno();
3377 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3378 if (err) {
3379 view_close(blame_view);
3380 free(path);
3381 } else
3382 *new_view = blame_view;
3383 return err;
3386 static const struct got_error *
3387 log_tree_entry(struct tog_view **new_view, int begin_x,
3388 struct got_tree_entry *te, struct tog_parent_trees *parents,
3389 struct got_object_id *commit_id, struct got_reflist_head *refs,
3390 struct got_repository *repo)
3392 struct tog_view *log_view;
3393 const struct got_error *err = NULL;
3394 char *path;
3396 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3397 if (log_view == NULL)
3398 return got_error_from_errno();
3400 err = tree_entry_path(&path, parents, te);
3401 if (err)
3402 return err;
3404 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3405 if (err)
3406 view_close(log_view);
3407 else
3408 *new_view = log_view;
3409 free(path);
3410 return err;
3413 static const struct got_error *
3414 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3415 struct got_object_id *commit_id, struct got_reflist_head *refs,
3416 struct got_repository *repo)
3418 const struct got_error *err = NULL;
3419 char *commit_id_str = NULL;
3420 struct tog_tree_view_state *s = &view->state.tree;
3422 TAILQ_INIT(&s->parents);
3424 err = got_object_id_str(&commit_id_str, commit_id);
3425 if (err != NULL)
3426 goto done;
3428 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3429 err = got_error_from_errno();
3430 goto done;
3433 s->root = s->tree = root;
3434 s->entries = got_object_tree_get_entries(root);
3435 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3436 s->commit_id = got_object_id_dup(commit_id);
3437 if (s->commit_id == NULL) {
3438 err = got_error_from_errno();
3439 goto done;
3441 s->refs = refs;
3442 s->repo = repo;
3444 view->show = show_tree_view;
3445 view->input = input_tree_view;
3446 view->close = close_tree_view;
3447 done:
3448 free(commit_id_str);
3449 if (err) {
3450 free(s->tree_label);
3451 s->tree_label = NULL;
3453 return err;
3456 static const struct got_error *
3457 close_tree_view(struct tog_view *view)
3459 struct tog_tree_view_state *s = &view->state.tree;
3461 free(s->tree_label);
3462 s->tree_label = NULL;
3463 free(s->commit_id);
3464 s->commit_id = NULL;
3465 while (!TAILQ_EMPTY(&s->parents)) {
3466 struct tog_parent_tree *parent;
3467 parent = TAILQ_FIRST(&s->parents);
3468 TAILQ_REMOVE(&s->parents, parent, entry);
3469 free(parent);
3472 if (s->tree != s->root)
3473 got_object_tree_close(s->tree);
3474 got_object_tree_close(s->root);
3476 return NULL;
3479 static const struct got_error *
3480 show_tree_view(struct tog_view *view)
3482 const struct got_error *err = NULL;
3483 struct tog_tree_view_state *s = &view->state.tree;
3484 char *parent_path;
3486 err = tree_entry_path(&parent_path, &s->parents, NULL);
3487 if (err)
3488 return err;
3490 err = draw_tree_entries(view, &s->first_displayed_entry,
3491 &s->last_displayed_entry, &s->selected_entry,
3492 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3493 s->entries, s->selected, view->nlines, s->tree == s->root);
3494 free(parent_path);
3496 view_vborder(view);
3497 return err;
3500 static const struct got_error *
3501 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3502 struct tog_view **focus_view, struct tog_view *view, int ch)
3504 const struct got_error *err = NULL;
3505 struct tog_tree_view_state *s = &view->state.tree;
3506 struct tog_view *log_view;
3507 int begin_x = 0, nscrolled;
3509 switch (ch) {
3510 case 'i':
3511 s->show_ids = !s->show_ids;
3512 break;
3513 case 'l':
3514 if (!s->selected_entry)
3515 break;
3516 if (view_is_parent_view(view))
3517 begin_x = view_split_begin_x(view->begin_x);
3518 err = log_tree_entry(&log_view, begin_x,
3519 s->selected_entry, &s->parents,
3520 s->commit_id, s->refs, s->repo);
3521 if (view_is_parent_view(view)) {
3522 err = view_close_child(view);
3523 if (err)
3524 return err;
3525 err = view_set_child(view, log_view);
3526 if (err) {
3527 view_close(log_view);
3528 break;
3530 *focus_view = log_view;
3531 view->child_focussed = 1;
3532 } else
3533 *new_view = log_view;
3534 break;
3535 case 'k':
3536 case KEY_UP:
3537 if (s->selected > 0) {
3538 s->selected--;
3539 if (s->selected == 0)
3540 break;
3542 if (s->selected > 0)
3543 break;
3544 tree_scroll_up(&s->first_displayed_entry, 1,
3545 s->entries, s->tree == s->root);
3546 break;
3547 case KEY_PPAGE:
3548 tree_scroll_up(&s->first_displayed_entry,
3549 MAX(0, view->nlines - 4 - s->selected), s->entries,
3550 s->tree == s->root);
3551 s->selected = 0;
3552 if (SIMPLEQ_FIRST(&s->entries->head) ==
3553 s->first_displayed_entry && s->tree != s->root)
3554 s->first_displayed_entry = NULL;
3555 break;
3556 case 'j':
3557 case KEY_DOWN:
3558 if (s->selected < s->ndisplayed - 1) {
3559 s->selected++;
3560 break;
3562 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3563 == NULL) {
3564 /* can't scroll any further */
3565 break;
3567 tree_scroll_down(&s->first_displayed_entry, 1,
3568 s->last_displayed_entry, s->entries);
3569 break;
3570 case KEY_NPAGE:
3571 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3572 == NULL) {
3573 /* can't scroll any further; move cursor down */
3574 if (s->selected < s->ndisplayed - 1)
3575 s->selected = s->ndisplayed - 1;
3576 break;
3578 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3579 view->nlines, s->last_displayed_entry, s->entries);
3580 if (nscrolled < view->nlines) {
3581 int ndisplayed = 0;
3582 struct got_tree_entry *te;
3583 te = s->first_displayed_entry;
3584 do {
3585 ndisplayed++;
3586 te = SIMPLEQ_NEXT(te, entry);
3587 } while (te);
3588 s->selected = ndisplayed - 1;
3590 break;
3591 case KEY_ENTER:
3592 case '\r':
3593 if (s->selected_entry == NULL) {
3594 struct tog_parent_tree *parent;
3595 case KEY_BACKSPACE:
3596 /* user selected '..' */
3597 if (s->tree == s->root)
3598 break;
3599 parent = TAILQ_FIRST(&s->parents);
3600 TAILQ_REMOVE(&s->parents, parent,
3601 entry);
3602 got_object_tree_close(s->tree);
3603 s->tree = parent->tree;
3604 s->entries =
3605 got_object_tree_get_entries(s->tree);
3606 s->first_displayed_entry =
3607 parent->first_displayed_entry;
3608 s->selected_entry =
3609 parent->selected_entry;
3610 s->selected = parent->selected;
3611 free(parent);
3612 } else if (S_ISDIR(s->selected_entry->mode)) {
3613 struct tog_parent_tree *parent;
3614 struct got_tree_object *child;
3615 err = got_object_open_as_tree(&child,
3616 s->repo, s->selected_entry->id);
3617 if (err)
3618 break;
3619 parent = calloc(1, sizeof(*parent));
3620 if (parent == NULL) {
3621 err = got_error_from_errno();
3622 break;
3624 parent->tree = s->tree;
3625 parent->first_displayed_entry =
3626 s->first_displayed_entry;
3627 parent->selected_entry = s->selected_entry;
3628 parent->selected = s->selected;
3629 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3630 s->tree = child;
3631 s->entries =
3632 got_object_tree_get_entries(s->tree);
3633 s->selected = 0;
3634 s->first_displayed_entry = NULL;
3635 } else if (S_ISREG(s->selected_entry->mode)) {
3636 struct tog_view *blame_view;
3637 int begin_x = view_is_parent_view(view) ?
3638 view_split_begin_x(view->begin_x) : 0;
3640 err = blame_tree_entry(&blame_view, begin_x,
3641 s->selected_entry, &s->parents,
3642 s->commit_id, s->refs, s->repo);
3643 if (err)
3644 break;
3645 if (view_is_parent_view(view)) {
3646 err = view_close_child(view);
3647 if (err)
3648 return err;
3649 err = view_set_child(view, blame_view);
3650 if (err) {
3651 view_close(blame_view);
3652 break;
3654 *focus_view = blame_view;
3655 view->child_focussed = 1;
3656 } else
3657 *new_view = blame_view;
3659 break;
3660 case KEY_RESIZE:
3661 if (s->selected > view->nlines)
3662 s->selected = s->ndisplayed - 1;
3663 break;
3664 default:
3665 break;
3668 return err;
3671 __dead static void
3672 usage_tree(void)
3674 endwin();
3675 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3676 getprogname());
3677 exit(1);
3680 static const struct got_error *
3681 cmd_tree(int argc, char *argv[])
3683 const struct got_error *error;
3684 struct got_repository *repo = NULL;
3685 struct got_reflist_head refs;
3686 char *repo_path = NULL;
3687 struct got_object_id *commit_id = NULL;
3688 char *commit_id_arg = NULL;
3689 struct got_commit_object *commit = NULL;
3690 struct got_tree_object *tree = NULL;
3691 int ch;
3692 struct tog_view *view;
3694 #ifndef PROFILE
3695 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3696 NULL) == -1)
3697 err(1, "pledge");
3698 #endif
3700 while ((ch = getopt(argc, argv, "c:")) != -1) {
3701 switch (ch) {
3702 case 'c':
3703 commit_id_arg = optarg;
3704 break;
3705 default:
3706 usage();
3707 /* NOTREACHED */
3711 argc -= optind;
3712 argv += optind;
3714 if (argc == 0) {
3715 struct got_worktree *worktree;
3716 char *cwd = getcwd(NULL, 0);
3717 if (cwd == NULL)
3718 return got_error_from_errno();
3719 error = got_worktree_open(&worktree, cwd);
3720 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3721 goto done;
3722 if (worktree) {
3723 free(cwd);
3724 repo_path =
3725 strdup(got_worktree_get_repo_path(worktree));
3726 got_worktree_close(worktree);
3727 } else
3728 repo_path = cwd;
3729 if (repo_path == NULL) {
3730 error = got_error_from_errno();
3731 goto done;
3733 } else if (argc == 1) {
3734 repo_path = realpath(argv[0], NULL);
3735 if (repo_path == NULL)
3736 return got_error_from_errno();
3737 } else
3738 usage_log();
3740 init_curses();
3742 error = apply_unveil(repo_path, NULL);
3743 if (error)
3744 goto done;
3746 error = got_repo_open(&repo, repo_path);
3747 if (error != NULL)
3748 goto done;
3750 if (commit_id_arg == NULL)
3751 error = get_head_commit_id(&commit_id, repo);
3752 else
3753 error = got_object_resolve_id_str(&commit_id, repo,
3754 commit_id_arg);
3755 if (error != NULL)
3756 goto done;
3758 error = got_object_open_as_commit(&commit, repo, commit_id);
3759 if (error != NULL)
3760 goto done;
3762 error = got_object_open_as_tree(&tree, repo,
3763 got_object_commit_get_tree_id(commit));
3764 if (error != NULL)
3765 goto done;
3767 SIMPLEQ_INIT(&refs);
3768 error = got_ref_list(&refs, repo);
3769 if (error)
3770 goto done;
3772 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3773 if (view == NULL) {
3774 error = got_error_from_errno();
3775 goto done;
3777 error = open_tree_view(view, tree, commit_id, &refs, repo);
3778 if (error)
3779 goto done;
3780 error = view_loop(view);
3781 done:
3782 free(repo_path);
3783 free(commit_id);
3784 if (commit)
3785 got_object_commit_close(commit);
3786 if (tree)
3787 got_object_tree_close(tree);
3788 if (repo)
3789 got_repo_close(repo);
3790 return error;
3793 __dead static void
3794 usage(void)
3796 int i;
3798 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3799 "Available commands:\n", getprogname());
3800 for (i = 0; i < nitems(tog_commands); i++) {
3801 struct tog_cmd *cmd = &tog_commands[i];
3802 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3804 exit(1);
3807 static char **
3808 make_argv(const char *arg0, const char *arg1)
3810 char **argv;
3811 int argc = (arg1 == NULL ? 1 : 2);
3813 argv = calloc(argc, sizeof(char *));
3814 if (argv == NULL)
3815 err(1, "calloc");
3816 argv[0] = strdup(arg0);
3817 if (argv[0] == NULL)
3818 err(1, "calloc");
3819 if (arg1) {
3820 argv[1] = strdup(arg1);
3821 if (argv[1] == NULL)
3822 err(1, "calloc");
3825 return argv;
3828 int
3829 main(int argc, char *argv[])
3831 const struct got_error *error = NULL;
3832 struct tog_cmd *cmd = NULL;
3833 int ch, hflag = 0;
3834 char **cmd_argv = NULL;
3836 setlocale(LC_CTYPE, "");
3838 while ((ch = getopt(argc, argv, "h")) != -1) {
3839 switch (ch) {
3840 case 'h':
3841 hflag = 1;
3842 break;
3843 default:
3844 usage();
3845 /* NOTREACHED */
3849 argc -= optind;
3850 argv += optind;
3851 optind = 0;
3852 optreset = 1;
3854 if (argc == 0) {
3855 if (hflag)
3856 usage();
3857 /* Build an argument vector which runs a default command. */
3858 cmd = &tog_commands[0];
3859 cmd_argv = make_argv(cmd->name, NULL);
3860 argc = 1;
3861 } else {
3862 int i;
3864 /* Did the user specific a command? */
3865 for (i = 0; i < nitems(tog_commands); i++) {
3866 if (strncmp(tog_commands[i].name, argv[0],
3867 strlen(argv[0])) == 0) {
3868 cmd = &tog_commands[i];
3869 if (hflag)
3870 tog_commands[i].cmd_usage();
3871 break;
3874 if (cmd == NULL) {
3875 /* Did the user specify a repository? */
3876 char *repo_path = realpath(argv[0], NULL);
3877 if (repo_path) {
3878 struct got_repository *repo;
3879 error = got_repo_open(&repo, repo_path);
3880 if (error == NULL)
3881 got_repo_close(repo);
3882 } else
3883 error = got_error_from_errno();
3884 if (error) {
3885 if (hflag) {
3886 fprintf(stderr, "%s: '%s' is not a "
3887 "known command\n", getprogname(),
3888 argv[0]);
3889 usage();
3891 fprintf(stderr, "%s: '%s' is neither a known "
3892 "command nor a path to a repository\n",
3893 getprogname(), argv[0]);
3894 free(repo_path);
3895 return 1;
3897 cmd = &tog_commands[0];
3898 cmd_argv = make_argv(cmd->name, repo_path);
3899 argc = 2;
3900 free(repo_path);
3904 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3905 if (error)
3906 goto done;
3907 done:
3908 endwin();
3909 free(cmd_argv);
3910 if (error)
3911 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3912 return 0;