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 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1247 pthread_cond_t *need_commits)
1249 int errcode;
1251 while (*commits_needed > 0) {
1252 if (*log_complete)
1253 break;
1255 /* Wake the log thread. */
1256 errcode = pthread_cond_signal(need_commits);
1257 if (errcode)
1258 return got_error_set_errno(errcode);
1259 errcode = pthread_mutex_unlock(&tog_mutex);
1260 if (errcode)
1261 return got_error_set_errno(errcode);
1262 pthread_yield();
1263 errcode = pthread_mutex_lock(&tog_mutex);
1264 if (errcode)
1265 return got_error_set_errno(errcode);
1267 if (*commits_needed > 0 && !load_all) {
1269 * Thread is not done yet; lose a key press
1270 * and let the user retry... this way the GUI
1271 * remains interactive while logging deep paths
1272 * with few commits in history.
1274 return NULL;
1278 return NULL;
1281 static const struct got_error *
1282 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1283 struct commit_queue_entry **last_displayed_entry,
1284 struct commit_queue *commits, int *log_complete, int *commits_needed,
1285 pthread_cond_t *need_commits)
1287 const struct got_error *err = NULL;
1288 struct commit_queue_entry *pentry;
1289 int nscrolled = 0;
1291 if (*last_displayed_entry == NULL)
1292 return NULL;
1294 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1295 if (pentry == NULL && !*log_complete) {
1296 (*commits_needed) += maxscroll;
1297 err = trigger_log_thread(0, commits_needed, log_complete,
1298 need_commits);
1299 if (err)
1300 return err;
1303 do {
1304 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1305 if (pentry == NULL)
1306 break;
1308 *last_displayed_entry = pentry;
1310 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1311 if (pentry == NULL)
1312 break;
1313 *first_displayed_entry = pentry;
1314 } while (++nscrolled < maxscroll);
1316 return err;
1319 static const struct got_error *
1320 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1321 struct got_commit_object *commit, struct got_object_id *commit_id,
1322 struct tog_view *log_view, struct got_reflist_head *refs,
1323 struct got_repository *repo)
1325 const struct got_error *err;
1326 struct got_object_qid *parent_id;
1327 struct tog_view *diff_view;
1329 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1330 if (diff_view == NULL)
1331 return got_error_from_errno();
1333 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1334 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1335 commit_id, log_view, refs, repo);
1336 if (err == NULL)
1337 *new_view = diff_view;
1338 return err;
1341 static const struct got_error *
1342 browse_commit(struct tog_view **new_view, int begin_x,
1343 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1344 struct got_repository *repo)
1346 const struct got_error *err = NULL;
1347 struct got_tree_object *tree;
1348 struct tog_view *tree_view;
1350 err = got_object_open_as_tree(&tree, repo,
1351 got_object_commit_get_tree_id(entry->commit));
1352 if (err)
1353 return err;
1355 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1356 if (tree_view == NULL)
1357 return got_error_from_errno();
1359 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1360 if (err)
1361 got_object_tree_close(tree);
1362 else
1363 *new_view = tree_view;
1364 return err;
1367 static void *
1368 log_thread(void *arg)
1370 const struct got_error *err = NULL;
1371 int errcode = 0;
1372 struct tog_log_thread_args *a = arg;
1373 int done = 0;
1375 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1376 if (err)
1377 return (void *)err;
1379 while (!done && !err) {
1380 err = queue_commits(a->graph, a->commits, 1, a->repo,
1381 a->in_repo_path);
1382 if (err) {
1383 if (err->code != GOT_ERR_ITER_COMPLETED)
1384 return (void *)err;
1385 err = NULL;
1386 done = 1;
1387 } else if (a->commits_needed > 0)
1388 a->commits_needed--;
1390 errcode = pthread_mutex_lock(&tog_mutex);
1391 if (errcode)
1392 return (void *)got_error_set_errno(errcode);
1394 if (done)
1395 a->log_complete = 1;
1396 else if (*a->quit) {
1397 done = 1;
1398 a->log_complete = 1;
1399 } else if (*a->first_displayed_entry == NULL) {
1400 *a->first_displayed_entry =
1401 TAILQ_FIRST(&a->commits->head);
1402 *a->selected_entry = *a->first_displayed_entry;
1405 if (done)
1406 a->commits_needed = 0;
1407 else if (a->commits_needed == 0) {
1408 errcode = pthread_cond_wait(&a->need_commits,
1409 &tog_mutex);
1410 if (errcode)
1411 err = got_error_set_errno(errcode);
1414 errcode = pthread_mutex_unlock(&tog_mutex);
1415 if (errcode && err == NULL)
1416 err = got_error_set_errno(errcode);
1418 return (void *)err;
1421 static const struct got_error *
1422 stop_log_thread(struct tog_log_view_state *s)
1424 const struct got_error *err = NULL;
1425 int errcode;
1427 if (s->thread) {
1428 s->quit = 1;
1429 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1430 if (errcode)
1431 return got_error_set_errno(errcode);
1432 errcode = pthread_mutex_unlock(&tog_mutex);
1433 if (errcode)
1434 return got_error_set_errno(errcode);
1435 errcode = pthread_join(s->thread, (void **)&err);
1436 if (errcode)
1437 return got_error_set_errno(errcode);
1438 errcode = pthread_mutex_lock(&tog_mutex);
1439 if (errcode)
1440 return got_error_set_errno(errcode);
1441 s->thread = NULL;
1444 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1445 if (errcode && err == NULL)
1446 err = got_error_set_errno(errcode);
1448 if (s->thread_args.repo) {
1449 got_repo_close(s->thread_args.repo);
1450 s->thread_args.repo = NULL;
1453 if (s->thread_args.graph) {
1454 got_commit_graph_close(s->thread_args.graph);
1455 s->thread_args.graph = NULL;
1458 return err;
1461 static const struct got_error *
1462 close_log_view(struct tog_view *view)
1464 const struct got_error *err = NULL;
1465 struct tog_log_view_state *s = &view->state.log;
1467 err = stop_log_thread(s);
1468 free_commits(&s->commits);
1469 free(s->in_repo_path);
1470 s->in_repo_path = NULL;
1471 free(s->start_id);
1472 s->start_id = NULL;
1473 return err;
1476 static const struct got_error *
1477 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1478 struct got_reflist_head *refs, struct got_repository *repo,
1479 const char *path, int check_disk)
1481 const struct got_error *err = NULL;
1482 struct tog_log_view_state *s = &view->state.log;
1483 struct got_repository *thread_repo = NULL;
1484 struct got_commit_graph *thread_graph = NULL;
1485 int errcode;
1487 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1488 if (err != NULL)
1489 goto done;
1491 /* The commit queue only contains commits being displayed. */
1492 TAILQ_INIT(&s->commits.head);
1493 s->commits.ncommits = 0;
1495 s->refs = refs;
1496 s->repo = repo;
1497 s->start_id = got_object_id_dup(start_id);
1498 if (s->start_id == NULL) {
1499 err = got_error_from_errno();
1500 goto done;
1503 view->show = show_log_view;
1504 view->input = input_log_view;
1505 view->close = close_log_view;
1507 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1508 if (err)
1509 goto done;
1510 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1511 0, thread_repo);
1512 if (err)
1513 goto done;
1515 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1516 if (errcode) {
1517 err = got_error_set_errno(errcode);
1518 goto done;
1521 s->thread_args.commits_needed = view->nlines;
1522 s->thread_args.graph = thread_graph;
1523 s->thread_args.commits = &s->commits;
1524 s->thread_args.in_repo_path = s->in_repo_path;
1525 s->thread_args.start_id = s->start_id;
1526 s->thread_args.repo = thread_repo;
1527 s->thread_args.log_complete = 0;
1528 s->thread_args.quit = &s->quit;
1529 s->thread_args.view = view;
1530 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1531 s->thread_args.selected_entry = &s->selected_entry;
1532 done:
1533 if (err)
1534 close_log_view(view);
1535 return err;
1538 static const struct got_error *
1539 show_log_view(struct tog_view *view)
1541 struct tog_log_view_state *s = &view->state.log;
1543 if (s->thread == NULL) {
1544 int errcode = pthread_create(&s->thread, NULL, log_thread,
1545 &s->thread_args);
1546 if (errcode)
1547 return got_error_set_errno(errcode);
1550 return draw_commits(view, &s->last_displayed_entry,
1551 &s->selected_entry, s->first_displayed_entry,
1552 &s->commits, s->selected, view->nlines, s->refs,
1553 s->in_repo_path, s->thread_args.commits_needed);
1556 static const struct got_error *
1557 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1558 struct tog_view **focus_view, struct tog_view *view, int ch)
1560 const struct got_error *err = NULL;
1561 struct tog_log_view_state *s = &view->state.log;
1562 char *parent_path;
1563 struct tog_view *diff_view = NULL, *tree_view = NULL;
1564 int begin_x = 0;
1566 switch (ch) {
1567 case 'q':
1568 s->quit = 1;
1569 break;
1570 case 'k':
1571 case KEY_UP:
1572 case '<':
1573 case ',':
1574 if (s->first_displayed_entry == NULL)
1575 break;
1576 if (s->selected > 0)
1577 s->selected--;
1578 if (s->selected > 0)
1579 break;
1580 scroll_up(&s->first_displayed_entry, 1,
1581 &s->commits);
1582 break;
1583 case KEY_PPAGE:
1584 if (s->first_displayed_entry == NULL)
1585 break;
1586 if (TAILQ_FIRST(&s->commits.head) ==
1587 s->first_displayed_entry) {
1588 s->selected = 0;
1589 break;
1591 scroll_up(&s->first_displayed_entry,
1592 view->nlines, &s->commits);
1593 break;
1594 case 'j':
1595 case KEY_DOWN:
1596 case '>':
1597 case '.':
1598 if (s->first_displayed_entry == NULL)
1599 break;
1600 if (s->selected < MIN(view->nlines - 2,
1601 s->commits.ncommits - 1)) {
1602 s->selected++;
1603 break;
1605 err = scroll_down(&s->first_displayed_entry, 1,
1606 &s->last_displayed_entry, &s->commits,
1607 &s->thread_args.log_complete,
1608 &s->thread_args.commits_needed,
1609 &s->thread_args.need_commits);
1610 break;
1611 case KEY_NPAGE: {
1612 struct commit_queue_entry *first;
1613 first = s->first_displayed_entry;
1614 if (first == NULL)
1615 break;
1616 err = scroll_down(&s->first_displayed_entry,
1617 view->nlines, &s->last_displayed_entry,
1618 &s->commits, &s->thread_args.log_complete,
1619 &s->thread_args.commits_needed,
1620 &s->thread_args.need_commits);
1621 if (first == s->first_displayed_entry &&
1622 s->selected < MIN(view->nlines - 2,
1623 s->commits.ncommits - 1)) {
1624 /* can't scroll further down */
1625 s->selected = MIN(view->nlines - 2,
1626 s->commits.ncommits - 1);
1628 err = NULL;
1629 break;
1631 case KEY_RESIZE:
1632 if (s->selected > view->nlines - 2)
1633 s->selected = view->nlines - 2;
1634 if (s->selected > s->commits.ncommits - 1)
1635 s->selected = s->commits.ncommits - 1;
1636 break;
1637 case KEY_ENTER:
1638 case '\r':
1639 if (s->selected_entry == NULL)
1640 break;
1641 if (view_is_parent_view(view))
1642 begin_x = view_split_begin_x(view->begin_x);
1643 err = open_diff_view_for_commit(&diff_view, begin_x,
1644 s->selected_entry->commit, s->selected_entry->id,
1645 view, s->refs, s->repo);
1646 if (err)
1647 break;
1648 if (view_is_parent_view(view)) {
1649 err = view_close_child(view);
1650 if (err)
1651 return err;
1652 err = view_set_child(view, diff_view);
1653 if (err) {
1654 view_close(diff_view);
1655 break;
1657 *focus_view = diff_view;
1658 view->child_focussed = 1;
1659 } else
1660 *new_view = diff_view;
1661 break;
1662 case 't':
1663 if (s->selected_entry == NULL)
1664 break;
1665 if (view_is_parent_view(view))
1666 begin_x = view_split_begin_x(view->begin_x);
1667 err = browse_commit(&tree_view, begin_x,
1668 s->selected_entry, s->refs, s->repo);
1669 if (err)
1670 break;
1671 if (view_is_parent_view(view)) {
1672 err = view_close_child(view);
1673 if (err)
1674 return err;
1675 err = view_set_child(view, tree_view);
1676 if (err) {
1677 view_close(tree_view);
1678 break;
1680 *focus_view = tree_view;
1681 view->child_focussed = 1;
1682 } else
1683 *new_view = tree_view;
1684 break;
1685 case KEY_BACKSPACE:
1686 if (strcmp(s->in_repo_path, "/") == 0)
1687 break;
1688 parent_path = dirname(s->in_repo_path);
1689 if (parent_path && strcmp(parent_path, ".") != 0) {
1690 struct tog_view *lv;
1691 err = stop_log_thread(s);
1692 if (err)
1693 return err;
1694 lv = view_open(view->nlines, view->ncols,
1695 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1696 if (lv == NULL)
1697 return got_error_from_errno();
1698 err = open_log_view(lv, s->start_id, s->refs,
1699 s->repo, parent_path, 0);
1700 if (err)
1701 return err;;
1702 if (view_is_parent_view(view))
1703 *new_view = lv;
1704 else {
1705 view_set_child(view->parent, lv);
1706 *focus_view = lv;
1708 return NULL;
1710 break;
1711 default:
1712 break;
1715 return err;
1718 static const struct got_error *
1719 apply_unveil(const char *repo_path, const char *worktree_path)
1721 const struct got_error *error;
1723 if (repo_path && unveil(repo_path, "r") != 0)
1724 return got_error_from_errno();
1726 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1727 return got_error_from_errno();
1729 if (unveil("/tmp", "rwc") != 0)
1730 return got_error_from_errno();
1732 error = got_privsep_unveil_exec_helpers();
1733 if (error != NULL)
1734 return error;
1736 if (unveil(NULL, NULL) != 0)
1737 return got_error_from_errno();
1739 return NULL;
1742 static void
1743 init_curses(void)
1745 initscr();
1746 cbreak();
1747 halfdelay(1); /* Do fast refresh while initial view is loading. */
1748 noecho();
1749 nonl();
1750 intrflush(stdscr, FALSE);
1751 keypad(stdscr, TRUE);
1752 curs_set(0);
1753 signal(SIGWINCH, tog_sigwinch);
1756 static const struct got_error *
1757 cmd_log(int argc, char *argv[])
1759 const struct got_error *error;
1760 struct got_repository *repo = NULL;
1761 struct got_reflist_head refs;
1762 struct got_object_id *start_id = NULL;
1763 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1764 char *start_commit = NULL;
1765 int ch;
1766 struct tog_view *view;
1768 #ifndef PROFILE
1769 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1770 NULL) == -1)
1771 err(1, "pledge");
1772 #endif
1774 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1775 switch (ch) {
1776 case 'c':
1777 start_commit = optarg;
1778 break;
1779 case 'r':
1780 repo_path = realpath(optarg, NULL);
1781 if (repo_path == NULL)
1782 err(1, "-r option");
1783 break;
1784 default:
1785 usage();
1786 /* NOTREACHED */
1790 argc -= optind;
1791 argv += optind;
1793 if (argc == 0)
1794 path = strdup("");
1795 else if (argc == 1)
1796 path = strdup(argv[0]);
1797 else
1798 usage_log();
1799 if (path == NULL)
1800 return got_error_from_errno();
1802 cwd = getcwd(NULL, 0);
1803 if (cwd == NULL) {
1804 error = got_error_from_errno();
1805 goto done;
1807 if (repo_path == NULL) {
1808 struct got_worktree *worktree;
1809 error = got_worktree_open(&worktree, cwd);
1810 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1811 goto done;
1812 if (worktree) {
1813 repo_path =
1814 strdup(got_worktree_get_repo_path(worktree));
1815 got_worktree_close(worktree);
1816 } else
1817 repo_path = strdup(cwd);
1818 if (repo_path == NULL) {
1819 error = got_error_from_errno();
1820 goto done;
1824 init_curses();
1826 error = apply_unveil(repo_path, NULL);
1827 if (error)
1828 goto done;
1830 error = got_repo_open(&repo, repo_path);
1831 if (error != NULL)
1832 goto done;
1834 if (start_commit == NULL)
1835 error = get_head_commit_id(&start_id, repo);
1836 else
1837 error = got_object_resolve_id_str(&start_id, repo,
1838 start_commit);
1839 if (error != NULL)
1840 goto done;
1842 SIMPLEQ_INIT(&refs);
1843 error = got_ref_list(&refs, repo);
1844 if (error)
1845 goto done;
1847 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1848 if (view == NULL) {
1849 error = got_error_from_errno();
1850 goto done;
1852 error = open_log_view(view, start_id, &refs, repo, path, 1);
1853 if (error)
1854 goto done;
1855 error = view_loop(view);
1856 done:
1857 free(repo_path);
1858 free(cwd);
1859 free(path);
1860 free(start_id);
1861 if (repo)
1862 got_repo_close(repo);
1863 return error;
1866 __dead static void
1867 usage_diff(void)
1869 endwin();
1870 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1871 getprogname());
1872 exit(1);
1875 static char *
1876 parse_next_line(FILE *f, size_t *len)
1878 char *line;
1879 size_t linelen;
1880 size_t lineno;
1881 const char delim[3] = { '\0', '\0', '\0'};
1883 line = fparseln(f, &linelen, &lineno, delim, 0);
1884 if (len)
1885 *len = linelen;
1886 return line;
1889 static const struct got_error *
1890 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1891 int *last_displayed_line, int *eof, int max_lines,
1892 char * header)
1894 const struct got_error *err;
1895 int nlines = 0, nprinted = 0;
1896 char *line;
1897 size_t len;
1898 wchar_t *wline;
1899 int width;
1901 rewind(f);
1902 werase(view->window);
1904 if (header) {
1905 err = format_line(&wline, &width, header, view->ncols);
1906 if (err) {
1907 return err;
1910 if (view_needs_focus_indication(view))
1911 wstandout(view->window);
1912 waddwstr(view->window, wline);
1913 if (view_needs_focus_indication(view))
1914 wstandend(view->window);
1915 if (width < view->ncols)
1916 waddch(view->window, '\n');
1918 if (max_lines <= 1)
1919 return NULL;
1920 max_lines--;
1923 *eof = 0;
1924 while (nprinted < max_lines) {
1925 line = parse_next_line(f, &len);
1926 if (line == NULL) {
1927 *eof = 1;
1928 break;
1930 if (++nlines < *first_displayed_line) {
1931 free(line);
1932 continue;
1935 err = format_line(&wline, &width, line, view->ncols);
1936 if (err) {
1937 free(line);
1938 return err;
1940 waddwstr(view->window, wline);
1941 if (width < view->ncols)
1942 waddch(view->window, '\n');
1943 if (++nprinted == 1)
1944 *first_displayed_line = nlines;
1945 free(line);
1946 free(wline);
1947 wline = NULL;
1949 *last_displayed_line = nlines;
1951 view_vborder(view);
1953 return NULL;
1956 static char *
1957 get_datestr(time_t *time, char *datebuf)
1959 char *p, *s = ctime_r(time, datebuf);
1960 p = strchr(s, '\n');
1961 if (p)
1962 *p = '\0';
1963 return s;
1966 static const struct got_error *
1967 write_commit_info(struct got_object_id *commit_id,
1968 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
1970 const struct got_error *err = NULL;
1971 char datebuf[26];
1972 struct got_commit_object *commit;
1973 char *id_str = NULL;
1974 time_t committer_time;
1975 const char *author, *committer;
1976 char *refs_str = NULL;
1978 if (refs) {
1979 err = build_refs_str(&refs_str, refs, commit_id);
1980 if (err)
1981 return err;
1984 err = got_object_open_as_commit(&commit, repo, commit_id);
1985 if (err)
1986 return err;
1988 err = got_object_id_str(&id_str, commit_id);
1989 if (err) {
1990 err = got_error_from_errno();
1991 goto done;
1994 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1995 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
1996 err = got_error_from_errno();
1997 goto done;
1999 if (fprintf(outfile, "from: %s\n",
2000 got_object_commit_get_author(commit)) < 0) {
2001 err = got_error_from_errno();
2002 goto done;
2004 committer_time = got_object_commit_get_committer_time(commit);
2005 if (fprintf(outfile, "date: %s UTC\n",
2006 get_datestr(&committer_time, datebuf)) < 0) {
2007 err = got_error_from_errno();
2008 goto done;
2010 author = got_object_commit_get_author(commit);
2011 committer = got_object_commit_get_committer(commit);
2012 if (strcmp(author, committer) != 0 &&
2013 fprintf(outfile, "via: %s\n", committer) < 0) {
2014 err = got_error_from_errno();
2015 goto done;
2017 if (fprintf(outfile, "%s\n",
2018 got_object_commit_get_logmsg(commit)) < 0) {
2019 err = got_error_from_errno();
2020 goto done;
2022 done:
2023 free(id_str);
2024 free(refs_str);
2025 got_object_commit_close(commit);
2026 return err;
2029 static const struct got_error *
2030 create_diff(struct tog_diff_view_state *s)
2032 const struct got_error *err = NULL;
2033 FILE *f = NULL;
2034 int obj_type;
2036 f = got_opentemp();
2037 if (f == NULL) {
2038 err = got_error_from_errno();
2039 goto done;
2041 if (s->f && fclose(s->f) != 0) {
2042 err = got_error_from_errno();
2043 goto done;
2045 s->f = f;
2047 if (s->id1)
2048 err = got_object_get_type(&obj_type, s->repo, s->id1);
2049 else
2050 err = got_object_get_type(&obj_type, s->repo, s->id2);
2051 if (err)
2052 goto done;
2054 switch (obj_type) {
2055 case GOT_OBJ_TYPE_BLOB:
2056 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2057 s->diff_context, s->repo, f);
2058 break;
2059 case GOT_OBJ_TYPE_TREE:
2060 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2061 s->diff_context, s->repo, f);
2062 break;
2063 case GOT_OBJ_TYPE_COMMIT: {
2064 const struct got_object_id_queue *parent_ids;
2065 struct got_object_qid *pid;
2066 struct got_commit_object *commit2;
2068 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2069 if (err)
2070 break;
2071 /* Show commit info if we're diffing to a parent/root commit. */
2072 if (s->id1 == NULL)
2073 write_commit_info(s->id2, s->refs, s->repo, f);
2074 else {
2075 parent_ids = got_object_commit_get_parent_ids(commit2);
2076 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2077 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2078 write_commit_info(s->id2, s->refs,
2079 s->repo, f);
2080 break;
2084 got_object_commit_close(commit2);
2086 err = got_diff_objects_as_commits(s->id1, s->id2,
2087 s->diff_context, s->repo, f);
2088 break;
2090 default:
2091 err = got_error(GOT_ERR_OBJ_TYPE);
2092 break;
2094 done:
2095 if (f && fflush(f) != 0 && err == NULL)
2096 err = got_error_from_errno();
2097 return err;
2100 static const struct got_error *
2101 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2102 struct got_object_id *id2, struct tog_view *log_view,
2103 struct got_reflist_head *refs, struct got_repository *repo)
2105 const struct got_error *err;
2107 if (id1 != NULL && id2 != NULL) {
2108 int type1, type2;
2109 err = got_object_get_type(&type1, repo, id1);
2110 if (err)
2111 return err;
2112 err = got_object_get_type(&type2, repo, id2);
2113 if (err)
2114 return err;
2116 if (type1 != type2)
2117 return got_error(GOT_ERR_OBJ_TYPE);
2120 if (id1) {
2121 view->state.diff.id1 = got_object_id_dup(id1);
2122 if (view->state.diff.id1 == NULL)
2123 return got_error_from_errno();
2124 } else
2125 view->state.diff.id1 = NULL;
2127 view->state.diff.id2 = got_object_id_dup(id2);
2128 if (view->state.diff.id2 == NULL) {
2129 free(view->state.diff.id1);
2130 view->state.diff.id1 = NULL;
2131 return got_error_from_errno();
2133 view->state.diff.f = NULL;
2134 view->state.diff.first_displayed_line = 1;
2135 view->state.diff.last_displayed_line = view->nlines;
2136 view->state.diff.diff_context = 3;
2137 view->state.diff.log_view = log_view;
2138 view->state.diff.repo = repo;
2139 view->state.diff.refs = refs;
2141 err = create_diff(&view->state.diff);
2142 if (err) {
2143 free(view->state.diff.id1);
2144 view->state.diff.id1 = NULL;
2145 free(view->state.diff.id2);
2146 view->state.diff.id2 = NULL;
2147 return err;
2150 view->show = show_diff_view;
2151 view->input = input_diff_view;
2152 view->close = close_diff_view;
2154 return NULL;
2157 static const struct got_error *
2158 close_diff_view(struct tog_view *view)
2160 const struct got_error *err = NULL;
2162 free(view->state.diff.id1);
2163 view->state.diff.id1 = NULL;
2164 free(view->state.diff.id2);
2165 view->state.diff.id2 = NULL;
2166 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2167 err = got_error_from_errno();
2168 return err;
2171 static const struct got_error *
2172 show_diff_view(struct tog_view *view)
2174 const struct got_error *err;
2175 struct tog_diff_view_state *s = &view->state.diff;
2176 char *id_str1 = NULL, *id_str2, *header;
2178 if (s->id1) {
2179 err = got_object_id_str(&id_str1, s->id1);
2180 if (err)
2181 return err;
2183 err = got_object_id_str(&id_str2, s->id2);
2184 if (err)
2185 return err;
2187 if (asprintf(&header, "diff %s %s",
2188 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2189 err = got_error_from_errno();
2190 free(id_str1);
2191 free(id_str2);
2192 return err;
2194 free(id_str1);
2195 free(id_str2);
2197 return draw_file(view, s->f, &s->first_displayed_line,
2198 &s->last_displayed_line, &s->eof, view->nlines,
2199 header);
2202 static const struct got_error *
2203 set_selected_commit(struct tog_diff_view_state *s,
2204 struct commit_queue_entry *entry)
2206 const struct got_error *err;
2207 const struct got_object_id_queue *parent_ids;
2208 struct got_commit_object *selected_commit;
2209 struct got_object_qid *pid;
2211 free(s->id2);
2212 s->id2 = got_object_id_dup(entry->id);
2213 if (s->id2 == NULL)
2214 return got_error_from_errno();
2216 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2217 if (err)
2218 return err;
2219 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2220 free(s->id1);
2221 pid = SIMPLEQ_FIRST(parent_ids);
2222 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2223 got_object_commit_close(selected_commit);
2224 return NULL;
2227 static const struct got_error *
2228 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2229 struct tog_view **focus_view, struct tog_view *view, int ch)
2231 const struct got_error *err = NULL;
2232 struct tog_diff_view_state *s = &view->state.diff;
2233 struct tog_log_view_state *ls;
2234 struct commit_queue_entry *entry;
2235 int i;
2237 switch (ch) {
2238 case 'k':
2239 case KEY_UP:
2240 if (s->first_displayed_line > 1)
2241 s->first_displayed_line--;
2242 break;
2243 case KEY_PPAGE:
2244 i = 0;
2245 while (i++ < view->nlines - 1 &&
2246 s->first_displayed_line > 1)
2247 s->first_displayed_line--;
2248 break;
2249 case 'j':
2250 case KEY_DOWN:
2251 if (!s->eof)
2252 s->first_displayed_line++;
2253 break;
2254 case KEY_NPAGE:
2255 case ' ':
2256 i = 0;
2257 while (!s->eof && i++ < view->nlines - 1) {
2258 char *line;
2259 line = parse_next_line(s->f, NULL);
2260 s->first_displayed_line++;
2261 if (line == NULL)
2262 break;
2264 break;
2265 case '[':
2266 if (s->diff_context > 0) {
2267 s->diff_context--;
2268 err = create_diff(s);
2270 break;
2271 case ']':
2272 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2273 s->diff_context++;
2274 err = create_diff(s);
2276 break;
2277 case '<':
2278 case ',':
2279 if (s->log_view == NULL)
2280 break;
2281 ls = &s->log_view->state.log;
2282 entry = TAILQ_PREV(ls->selected_entry,
2283 commit_queue_head, entry);
2284 if (entry == NULL)
2285 break;
2287 err = input_log_view(NULL, NULL, NULL, s->log_view,
2288 KEY_UP);
2289 if (err)
2290 break;
2292 err = set_selected_commit(s, entry);
2293 if (err)
2294 break;
2296 s->first_displayed_line = 1;
2297 s->last_displayed_line = view->nlines;
2299 err = create_diff(s);
2300 break;
2301 case '>':
2302 case '.':
2303 if (s->log_view == NULL)
2304 break;
2305 ls = &s->log_view->state.log;
2307 if (ls->thread_args.commits_needed == 0) {
2308 ls->thread_args.commits_needed++;
2310 /* Display "loading..." in log view. */
2311 show_log_view(s->log_view);
2312 update_panels();
2313 doupdate();
2315 err = trigger_log_thread(1 /* load_all */,
2316 &ls->thread_args.commits_needed,
2317 &ls->thread_args.log_complete,
2318 &ls->thread_args.need_commits);
2319 if (err)
2320 break;
2322 err = input_log_view(NULL, NULL, NULL, s->log_view,
2323 KEY_DOWN);
2324 if (err)
2325 break;
2327 entry = TAILQ_NEXT(ls->selected_entry, entry);
2328 if (entry == NULL)
2329 break;
2331 err = set_selected_commit(s, entry);
2332 if (err)
2333 break;
2335 s->first_displayed_line = 1;
2336 s->last_displayed_line = view->nlines;
2338 err = create_diff(s);
2339 break;
2340 default:
2341 break;
2344 return err;
2347 static const struct got_error *
2348 cmd_diff(int argc, char *argv[])
2350 const struct got_error *error = NULL;
2351 struct got_repository *repo = NULL;
2352 struct got_reflist_head refs;
2353 struct got_object_id *id1 = NULL, *id2 = NULL;
2354 char *repo_path = NULL;
2355 char *id_str1 = NULL, *id_str2 = NULL;
2356 int ch;
2357 struct tog_view *view;
2359 #ifndef PROFILE
2360 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2361 NULL) == -1)
2362 err(1, "pledge");
2363 #endif
2365 while ((ch = getopt(argc, argv, "")) != -1) {
2366 switch (ch) {
2367 default:
2368 usage();
2369 /* NOTREACHED */
2373 argc -= optind;
2374 argv += optind;
2376 if (argc == 0) {
2377 usage_diff(); /* TODO show local worktree changes */
2378 } else if (argc == 2) {
2379 repo_path = getcwd(NULL, 0);
2380 if (repo_path == NULL)
2381 return got_error_from_errno();
2382 id_str1 = argv[0];
2383 id_str2 = argv[1];
2384 } else if (argc == 3) {
2385 repo_path = realpath(argv[0], NULL);
2386 if (repo_path == NULL)
2387 return got_error_from_errno();
2388 id_str1 = argv[1];
2389 id_str2 = argv[2];
2390 } else
2391 usage_diff();
2393 init_curses();
2395 error = apply_unveil(repo_path, NULL);
2396 if (error)
2397 goto done;
2399 error = got_repo_open(&repo, repo_path);
2400 free(repo_path);
2401 if (error)
2402 goto done;
2404 error = got_object_resolve_id_str(&id1, repo, id_str1);
2405 if (error)
2406 goto done;
2408 error = got_object_resolve_id_str(&id2, repo, id_str2);
2409 if (error)
2410 goto done;
2412 SIMPLEQ_INIT(&refs);
2413 error = got_ref_list(&refs, repo);
2414 if (error)
2415 goto done;
2417 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2418 if (view == NULL) {
2419 error = got_error_from_errno();
2420 goto done;
2422 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2423 if (error)
2424 goto done;
2425 error = view_loop(view);
2426 done:
2427 got_repo_close(repo);
2428 return error;
2431 __dead static void
2432 usage_blame(void)
2434 endwin();
2435 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2436 getprogname());
2437 exit(1);
2440 struct tog_blame_line {
2441 int annotated;
2442 struct got_object_id *id;
2445 static const struct got_error *
2446 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2447 const char *path, struct tog_blame_line *lines, int nlines,
2448 int blame_complete, int selected_line, int *first_displayed_line,
2449 int *last_displayed_line, int *eof, int max_lines)
2451 const struct got_error *err;
2452 int lineno = 0, nprinted = 0;
2453 char *line;
2454 size_t len;
2455 wchar_t *wline;
2456 int width, wlimit;
2457 struct tog_blame_line *blame_line;
2458 struct got_object_id *prev_id = NULL;
2459 char *id_str;
2461 err = got_object_id_str(&id_str, id);
2462 if (err)
2463 return err;
2465 rewind(f);
2466 werase(view->window);
2468 if (asprintf(&line, "commit %s", id_str) == -1) {
2469 err = got_error_from_errno();
2470 free(id_str);
2471 return err;
2474 err = format_line(&wline, &width, line, view->ncols);
2475 free(line);
2476 line = NULL;
2477 if (view_needs_focus_indication(view))
2478 wstandout(view->window);
2479 waddwstr(view->window, wline);
2480 if (view_needs_focus_indication(view))
2481 wstandend(view->window);
2482 free(wline);
2483 wline = NULL;
2484 if (width < view->ncols)
2485 waddch(view->window, '\n');
2487 if (asprintf(&line, "[%d/%d] %s%s",
2488 *first_displayed_line - 1 + selected_line, nlines,
2489 blame_complete ? "" : "annotating... ", path) == -1) {
2490 free(id_str);
2491 return got_error_from_errno();
2493 free(id_str);
2494 err = format_line(&wline, &width, line, view->ncols);
2495 free(line);
2496 line = NULL;
2497 if (err)
2498 return err;
2499 waddwstr(view->window, wline);
2500 free(wline);
2501 wline = NULL;
2502 if (width < view->ncols)
2503 waddch(view->window, '\n');
2505 *eof = 0;
2506 while (nprinted < max_lines - 2) {
2507 line = parse_next_line(f, &len);
2508 if (line == NULL) {
2509 *eof = 1;
2510 break;
2512 if (++lineno < *first_displayed_line) {
2513 free(line);
2514 continue;
2517 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2518 err = format_line(&wline, &width, line, wlimit);
2519 if (err) {
2520 free(line);
2521 return err;
2524 if (view->focussed && nprinted == selected_line - 1)
2525 wstandout(view->window);
2527 blame_line = &lines[lineno - 1];
2528 if (blame_line->annotated && prev_id &&
2529 got_object_id_cmp(prev_id, blame_line->id) == 0)
2530 waddstr(view->window, " ");
2531 else if (blame_line->annotated) {
2532 char *id_str;
2533 err = got_object_id_str(&id_str, blame_line->id);
2534 if (err) {
2535 free(line);
2536 free(wline);
2537 return err;
2539 wprintw(view->window, "%.8s ", id_str);
2540 free(id_str);
2541 prev_id = blame_line->id;
2542 } else {
2543 waddstr(view->window, "........ ");
2544 prev_id = NULL;
2547 waddwstr(view->window, wline);
2548 while (width < wlimit) {
2549 waddch(view->window, ' ');
2550 width++;
2552 if (view->focussed && nprinted == selected_line - 1)
2553 wstandend(view->window);
2554 if (++nprinted == 1)
2555 *first_displayed_line = lineno;
2556 free(line);
2557 free(wline);
2558 wline = NULL;
2560 *last_displayed_line = lineno;
2562 view_vborder(view);
2564 return NULL;
2567 static const struct got_error *
2568 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2570 const struct got_error *err = NULL;
2571 struct tog_blame_cb_args *a = arg;
2572 struct tog_blame_line *line;
2573 int errcode;
2575 if (nlines != a->nlines ||
2576 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2577 return got_error(GOT_ERR_RANGE);
2579 errcode = pthread_mutex_lock(&tog_mutex);
2580 if (errcode)
2581 return got_error_set_errno(errcode);
2583 if (*a->quit) { /* user has quit the blame view */
2584 err = got_error(GOT_ERR_ITER_COMPLETED);
2585 goto done;
2588 if (lineno == -1)
2589 goto done; /* no change in this commit */
2591 line = &a->lines[lineno - 1];
2592 if (line->annotated)
2593 goto done;
2595 line->id = got_object_id_dup(id);
2596 if (line->id == NULL) {
2597 err = got_error_from_errno();
2598 goto done;
2600 line->annotated = 1;
2601 done:
2602 errcode = pthread_mutex_unlock(&tog_mutex);
2603 if (errcode)
2604 err = got_error_set_errno(errcode);
2605 return err;
2608 static void *
2609 blame_thread(void *arg)
2611 const struct got_error *err;
2612 struct tog_blame_thread_args *ta = arg;
2613 struct tog_blame_cb_args *a = ta->cb_args;
2614 int errcode;
2616 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2617 blame_cb, ta->cb_args);
2619 errcode = pthread_mutex_lock(&tog_mutex);
2620 if (errcode)
2621 return (void *)got_error_set_errno(errcode);
2623 got_repo_close(ta->repo);
2624 ta->repo = NULL;
2625 *ta->complete = 1;
2627 errcode = pthread_mutex_unlock(&tog_mutex);
2628 if (errcode && err == NULL)
2629 err = got_error_set_errno(errcode);
2631 return (void *)err;
2634 static struct got_object_id *
2635 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2636 int selected_line)
2638 struct tog_blame_line *line;
2640 line = &lines[first_displayed_line - 1 + selected_line - 1];
2641 if (!line->annotated)
2642 return NULL;
2644 return line->id;
2647 static const struct got_error *
2648 stop_blame(struct tog_blame *blame)
2650 const struct got_error *err = NULL;
2651 int i;
2653 if (blame->thread) {
2654 int errcode;
2655 errcode = pthread_mutex_unlock(&tog_mutex);
2656 if (errcode)
2657 return got_error_set_errno(errcode);
2658 errcode = pthread_join(blame->thread, (void **)&err);
2659 if (errcode)
2660 return got_error_set_errno(errcode);
2661 errcode = pthread_mutex_lock(&tog_mutex);
2662 if (errcode)
2663 return got_error_set_errno(errcode);
2664 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2665 err = NULL;
2666 blame->thread = NULL;
2668 if (blame->thread_args.repo) {
2669 got_repo_close(blame->thread_args.repo);
2670 blame->thread_args.repo = NULL;
2672 if (blame->f) {
2673 if (fclose(blame->f) != 0 && err == NULL)
2674 err = got_error_from_errno();
2675 blame->f = NULL;
2677 if (blame->lines) {
2678 for (i = 0; i < blame->nlines; i++)
2679 free(blame->lines[i].id);
2680 free(blame->lines);
2681 blame->lines = NULL;
2683 free(blame->cb_args.commit_id);
2684 blame->cb_args.commit_id = NULL;
2686 return err;
2689 static const struct got_error *
2690 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2691 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2692 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2693 struct got_repository *repo)
2695 const struct got_error *err = NULL;
2696 struct got_blob_object *blob = NULL;
2697 struct got_repository *thread_repo = NULL;
2698 struct got_object_id *obj_id = NULL;
2699 int obj_type;
2701 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2702 if (err)
2703 return err;
2704 if (obj_id == NULL)
2705 return got_error(GOT_ERR_NO_OBJ);
2707 err = got_object_get_type(&obj_type, repo, obj_id);
2708 if (err)
2709 goto done;
2711 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2712 err = got_error(GOT_ERR_OBJ_TYPE);
2713 goto done;
2716 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2717 if (err)
2718 goto done;
2719 blame->f = got_opentemp();
2720 if (blame->f == NULL) {
2721 err = got_error_from_errno();
2722 goto done;
2724 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2725 blame->f, blob);
2726 if (err)
2727 goto done;
2729 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2730 if (blame->lines == NULL) {
2731 err = got_error_from_errno();
2732 goto done;
2735 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2736 if (err)
2737 goto done;
2739 blame->cb_args.view = view;
2740 blame->cb_args.lines = blame->lines;
2741 blame->cb_args.nlines = blame->nlines;
2742 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2743 if (blame->cb_args.commit_id == NULL) {
2744 err = got_error_from_errno();
2745 goto done;
2747 blame->cb_args.quit = done;
2749 blame->thread_args.path = path;
2750 blame->thread_args.repo = thread_repo;
2751 blame->thread_args.cb_args = &blame->cb_args;
2752 blame->thread_args.complete = blame_complete;
2753 *blame_complete = 0;
2755 done:
2756 if (blob)
2757 got_object_blob_close(blob);
2758 free(obj_id);
2759 if (err)
2760 stop_blame(blame);
2761 return err;
2764 static const struct got_error *
2765 open_blame_view(struct tog_view *view, char *path,
2766 struct got_object_id *commit_id, struct got_reflist_head *refs,
2767 struct got_repository *repo)
2769 const struct got_error *err = NULL;
2770 struct tog_blame_view_state *s = &view->state.blame;
2772 SIMPLEQ_INIT(&s->blamed_commits);
2774 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2775 if (err)
2776 return err;
2778 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2779 s->first_displayed_line = 1;
2780 s->last_displayed_line = view->nlines;
2781 s->selected_line = 1;
2782 s->blame_complete = 0;
2783 s->path = path;
2784 if (s->path == NULL)
2785 return got_error_from_errno();
2786 s->repo = repo;
2787 s->refs = refs;
2788 s->commit_id = commit_id;
2789 memset(&s->blame, 0, sizeof(s->blame));
2791 view->show = show_blame_view;
2792 view->input = input_blame_view;
2793 view->close = close_blame_view;
2795 return run_blame(&s->blame, view, &s->blame_complete,
2796 &s->first_displayed_line, &s->last_displayed_line,
2797 &s->selected_line, &s->done, &s->eof, s->path,
2798 s->blamed_commit->id, s->repo);
2801 static const struct got_error *
2802 close_blame_view(struct tog_view *view)
2804 const struct got_error *err = NULL;
2805 struct tog_blame_view_state *s = &view->state.blame;
2807 if (s->blame.thread)
2808 err = stop_blame(&s->blame);
2810 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2811 struct got_object_qid *blamed_commit;
2812 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2813 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2814 got_object_qid_free(blamed_commit);
2817 free(s->path);
2819 return err;
2822 static const struct got_error *
2823 show_blame_view(struct tog_view *view)
2825 const struct got_error *err = NULL;
2826 struct tog_blame_view_state *s = &view->state.blame;
2827 int errcode;
2829 if (s->blame.thread == NULL) {
2830 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2831 &s->blame.thread_args);
2832 if (errcode)
2833 return got_error_set_errno(errcode);
2836 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2837 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2838 s->selected_line, &s->first_displayed_line,
2839 &s->last_displayed_line, &s->eof, view->nlines);
2841 view_vborder(view);
2842 return err;
2845 static const struct got_error *
2846 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2847 struct tog_view **focus_view, struct tog_view *view, int ch)
2849 const struct got_error *err = NULL, *thread_err = NULL;
2850 struct tog_view *diff_view;
2851 struct tog_blame_view_state *s = &view->state.blame;
2852 int begin_x = 0;
2854 switch (ch) {
2855 case 'q':
2856 s->done = 1;
2857 break;
2858 case 'k':
2859 case KEY_UP:
2860 if (s->selected_line > 1)
2861 s->selected_line--;
2862 else if (s->selected_line == 1 &&
2863 s->first_displayed_line > 1)
2864 s->first_displayed_line--;
2865 break;
2866 case KEY_PPAGE:
2867 if (s->first_displayed_line == 1) {
2868 s->selected_line = 1;
2869 break;
2871 if (s->first_displayed_line > view->nlines - 2)
2872 s->first_displayed_line -=
2873 (view->nlines - 2);
2874 else
2875 s->first_displayed_line = 1;
2876 break;
2877 case 'j':
2878 case KEY_DOWN:
2879 if (s->selected_line < view->nlines - 2 &&
2880 s->first_displayed_line +
2881 s->selected_line <= s->blame.nlines)
2882 s->selected_line++;
2883 else if (s->last_displayed_line <
2884 s->blame.nlines)
2885 s->first_displayed_line++;
2886 break;
2887 case 'b':
2888 case 'p': {
2889 struct got_object_id *id = NULL;
2890 id = get_selected_commit_id(s->blame.lines,
2891 s->first_displayed_line, s->selected_line);
2892 if (id == NULL)
2893 break;
2894 if (ch == 'p') {
2895 struct got_commit_object *commit;
2896 struct got_object_qid *pid;
2897 struct got_object_id *blob_id = NULL;
2898 int obj_type;
2899 err = got_object_open_as_commit(&commit,
2900 s->repo, id);
2901 if (err)
2902 break;
2903 pid = SIMPLEQ_FIRST(
2904 got_object_commit_get_parent_ids(commit));
2905 if (pid == NULL) {
2906 got_object_commit_close(commit);
2907 break;
2909 /* Check if path history ends here. */
2910 err = got_object_id_by_path(&blob_id, s->repo,
2911 pid->id, s->path);
2912 if (err) {
2913 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2914 err = NULL;
2915 got_object_commit_close(commit);
2916 break;
2918 err = got_object_get_type(&obj_type, s->repo,
2919 blob_id);
2920 free(blob_id);
2921 /* Can't blame non-blob type objects. */
2922 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2923 got_object_commit_close(commit);
2924 break;
2926 err = got_object_qid_alloc(&s->blamed_commit,
2927 pid->id);
2928 got_object_commit_close(commit);
2929 } else {
2930 if (got_object_id_cmp(id,
2931 s->blamed_commit->id) == 0)
2932 break;
2933 err = got_object_qid_alloc(&s->blamed_commit,
2934 id);
2936 if (err)
2937 break;
2938 s->done = 1;
2939 thread_err = stop_blame(&s->blame);
2940 s->done = 0;
2941 if (thread_err)
2942 break;
2943 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2944 s->blamed_commit, entry);
2945 err = run_blame(&s->blame, view, &s->blame_complete,
2946 &s->first_displayed_line, &s->last_displayed_line,
2947 &s->selected_line, &s->done, &s->eof,
2948 s->path, s->blamed_commit->id, s->repo);
2949 if (err)
2950 break;
2951 break;
2953 case 'B': {
2954 struct got_object_qid *first;
2955 first = SIMPLEQ_FIRST(&s->blamed_commits);
2956 if (!got_object_id_cmp(first->id, s->commit_id))
2957 break;
2958 s->done = 1;
2959 thread_err = stop_blame(&s->blame);
2960 s->done = 0;
2961 if (thread_err)
2962 break;
2963 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2964 got_object_qid_free(s->blamed_commit);
2965 s->blamed_commit =
2966 SIMPLEQ_FIRST(&s->blamed_commits);
2967 err = run_blame(&s->blame, view, &s->blame_complete,
2968 &s->first_displayed_line, &s->last_displayed_line,
2969 &s->selected_line, &s->done, &s->eof, s->path,
2970 s->blamed_commit->id, s->repo);
2971 if (err)
2972 break;
2973 break;
2975 case KEY_ENTER:
2976 case '\r': {
2977 struct got_object_id *id = NULL;
2978 struct got_object_qid *pid;
2979 struct got_commit_object *commit = NULL;
2980 id = get_selected_commit_id(s->blame.lines,
2981 s->first_displayed_line, s->selected_line);
2982 if (id == NULL)
2983 break;
2984 err = got_object_open_as_commit(&commit, s->repo, id);
2985 if (err)
2986 break;
2987 pid = SIMPLEQ_FIRST(
2988 got_object_commit_get_parent_ids(commit));
2989 if (view_is_parent_view(view))
2990 begin_x = view_split_begin_x(view->begin_x);
2991 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2992 if (diff_view == NULL) {
2993 got_object_commit_close(commit);
2994 err = got_error_from_errno();
2995 break;
2997 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2998 id, NULL, s->refs, s->repo);
2999 got_object_commit_close(commit);
3000 if (err) {
3001 view_close(diff_view);
3002 break;
3004 if (view_is_parent_view(view)) {
3005 err = view_close_child(view);
3006 if (err)
3007 break;
3008 err = view_set_child(view, diff_view);
3009 if (err) {
3010 view_close(diff_view);
3011 break;
3013 *focus_view = diff_view;
3014 view->child_focussed = 1;
3015 } else
3016 *new_view = diff_view;
3017 if (err)
3018 break;
3019 break;
3021 case KEY_NPAGE:
3022 case ' ':
3023 if (s->last_displayed_line >= s->blame.nlines &&
3024 s->selected_line < view->nlines - 2) {
3025 s->selected_line = MIN(s->blame.nlines,
3026 view->nlines - 2);
3027 break;
3029 if (s->last_displayed_line + view->nlines - 2
3030 <= s->blame.nlines)
3031 s->first_displayed_line +=
3032 view->nlines - 2;
3033 else
3034 s->first_displayed_line =
3035 s->blame.nlines -
3036 (view->nlines - 3);
3037 break;
3038 case KEY_RESIZE:
3039 if (s->selected_line > view->nlines - 2) {
3040 s->selected_line = MIN(s->blame.nlines,
3041 view->nlines - 2);
3043 break;
3044 default:
3045 break;
3047 return thread_err ? thread_err : err;
3050 static const struct got_error *
3051 cmd_blame(int argc, char *argv[])
3053 const struct got_error *error;
3054 struct got_repository *repo = NULL;
3055 struct got_reflist_head refs;
3056 struct got_worktree *worktree = NULL;
3057 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3058 struct got_object_id *commit_id = NULL;
3059 char *commit_id_str = NULL;
3060 int ch;
3061 struct tog_view *view;
3063 #ifndef PROFILE
3064 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3065 NULL) == -1)
3066 err(1, "pledge");
3067 #endif
3069 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3070 switch (ch) {
3071 case 'c':
3072 commit_id_str = optarg;
3073 break;
3074 case 'r':
3075 repo_path = realpath(optarg, NULL);
3076 if (repo_path == NULL)
3077 err(1, "-r option");
3078 break;
3079 default:
3080 usage();
3081 /* NOTREACHED */
3085 argc -= optind;
3086 argv += optind;
3088 if (argc == 1)
3089 path = argv[0];
3090 else
3091 usage_blame();
3093 cwd = getcwd(NULL, 0);
3094 if (cwd == NULL) {
3095 error = got_error_from_errno();
3096 goto done;
3098 if (repo_path == NULL) {
3099 error = got_worktree_open(&worktree, cwd);
3100 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3101 goto done;
3102 else
3103 error = NULL;
3104 if (worktree) {
3105 repo_path =
3106 strdup(got_worktree_get_repo_path(worktree));
3107 if (repo_path == NULL)
3108 error = got_error_from_errno();
3109 if (error)
3110 goto done;
3111 } else {
3112 repo_path = strdup(cwd);
3113 if (repo_path == NULL) {
3114 error = got_error_from_errno();
3115 goto done;
3120 init_curses();
3122 error = apply_unveil(repo_path, NULL);
3123 if (error)
3124 goto done;
3126 error = got_repo_open(&repo, repo_path);
3127 if (error != NULL)
3128 goto done;
3130 if (worktree) {
3131 const char *prefix = got_worktree_get_path_prefix(worktree);
3132 char *p, *worktree_subdir = cwd +
3133 strlen(got_worktree_get_root_path(worktree));
3134 if (asprintf(&p, "%s%s%s%s%s",
3135 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3136 worktree_subdir, worktree_subdir[0] ? "/" : "",
3137 path) == -1) {
3138 error = got_error_from_errno();
3139 goto done;
3141 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3142 free(p);
3143 } else {
3144 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3146 if (error)
3147 goto done;
3149 if (commit_id_str == NULL) {
3150 struct got_reference *head_ref;
3151 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3152 if (error != NULL)
3153 goto done;
3154 error = got_ref_resolve(&commit_id, repo, head_ref);
3155 got_ref_close(head_ref);
3156 } else {
3157 error = got_object_resolve_id_str(&commit_id, repo,
3158 commit_id_str);
3160 if (error != NULL)
3161 goto done;
3163 SIMPLEQ_INIT(&refs);
3164 error = got_ref_list(&refs, repo);
3165 if (error)
3166 goto done;
3168 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3169 if (view == NULL) {
3170 error = got_error_from_errno();
3171 goto done;
3173 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3174 if (error)
3175 goto done;
3176 error = view_loop(view);
3177 done:
3178 free(repo_path);
3179 free(cwd);
3180 free(commit_id);
3181 if (worktree)
3182 got_worktree_close(worktree);
3183 if (repo)
3184 got_repo_close(repo);
3185 return error;
3188 static const struct got_error *
3189 draw_tree_entries(struct tog_view *view,
3190 struct got_tree_entry **first_displayed_entry,
3191 struct got_tree_entry **last_displayed_entry,
3192 struct got_tree_entry **selected_entry, int *ndisplayed,
3193 const char *label, int show_ids, const char *parent_path,
3194 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3196 const struct got_error *err = NULL;
3197 struct got_tree_entry *te;
3198 wchar_t *wline;
3199 int width, n;
3201 *ndisplayed = 0;
3203 werase(view->window);
3205 if (limit == 0)
3206 return NULL;
3208 err = format_line(&wline, &width, label, view->ncols);
3209 if (err)
3210 return err;
3211 if (view_needs_focus_indication(view))
3212 wstandout(view->window);
3213 waddwstr(view->window, wline);
3214 if (view_needs_focus_indication(view))
3215 wstandend(view->window);
3216 free(wline);
3217 wline = NULL;
3218 if (width < view->ncols)
3219 waddch(view->window, '\n');
3220 if (--limit <= 0)
3221 return NULL;
3222 err = format_line(&wline, &width, parent_path, view->ncols);
3223 if (err)
3224 return err;
3225 waddwstr(view->window, wline);
3226 free(wline);
3227 wline = NULL;
3228 if (width < view->ncols)
3229 waddch(view->window, '\n');
3230 if (--limit <= 0)
3231 return NULL;
3232 waddch(view->window, '\n');
3233 if (--limit <= 0)
3234 return NULL;
3236 te = SIMPLEQ_FIRST(&entries->head);
3237 if (*first_displayed_entry == NULL) {
3238 if (selected == 0) {
3239 if (view->focussed)
3240 wstandout(view->window);
3241 *selected_entry = NULL;
3243 waddstr(view->window, " ..\n"); /* parent directory */
3244 if (selected == 0 && view->focussed)
3245 wstandend(view->window);
3246 (*ndisplayed)++;
3247 if (--limit <= 0)
3248 return NULL;
3249 n = 1;
3250 } else {
3251 n = 0;
3252 while (te != *first_displayed_entry)
3253 te = SIMPLEQ_NEXT(te, entry);
3256 while (te) {
3257 char *line = NULL, *id_str = NULL;
3259 if (show_ids) {
3260 err = got_object_id_str(&id_str, te->id);
3261 if (err)
3262 return got_error_from_errno();
3264 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3265 te->name, S_ISDIR(te->mode) ? "/" :
3266 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3267 free(id_str);
3268 return got_error_from_errno();
3270 free(id_str);
3271 err = format_line(&wline, &width, line, view->ncols);
3272 if (err) {
3273 free(line);
3274 break;
3276 if (n == selected) {
3277 if (view->focussed)
3278 wstandout(view->window);
3279 *selected_entry = te;
3281 waddwstr(view->window, wline);
3282 if (width < view->ncols)
3283 waddch(view->window, '\n');
3284 if (n == selected && view->focussed)
3285 wstandend(view->window);
3286 free(line);
3287 free(wline);
3288 wline = NULL;
3289 n++;
3290 (*ndisplayed)++;
3291 *last_displayed_entry = te;
3292 if (--limit <= 0)
3293 break;
3294 te = SIMPLEQ_NEXT(te, entry);
3297 return err;
3300 static void
3301 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3302 const struct got_tree_entries *entries, int isroot)
3304 struct got_tree_entry *te, *prev;
3305 int i;
3307 if (*first_displayed_entry == NULL)
3308 return;
3310 te = SIMPLEQ_FIRST(&entries->head);
3311 if (*first_displayed_entry == te) {
3312 if (!isroot)
3313 *first_displayed_entry = NULL;
3314 return;
3317 /* XXX this is stupid... switch to TAILQ? */
3318 for (i = 0; i < maxscroll; i++) {
3319 while (te != *first_displayed_entry) {
3320 prev = te;
3321 te = SIMPLEQ_NEXT(te, entry);
3323 *first_displayed_entry = prev;
3324 te = SIMPLEQ_FIRST(&entries->head);
3326 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3327 *first_displayed_entry = NULL;
3330 static int
3331 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3332 struct got_tree_entry *last_displayed_entry,
3333 const struct got_tree_entries *entries)
3335 struct got_tree_entry *next, *last;
3336 int n = 0;
3338 if (*first_displayed_entry)
3339 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3340 else
3341 next = SIMPLEQ_FIRST(&entries->head);
3342 last = last_displayed_entry;
3343 while (next && last && n++ < maxscroll) {
3344 last = SIMPLEQ_NEXT(last, entry);
3345 if (last) {
3346 *first_displayed_entry = next;
3347 next = SIMPLEQ_NEXT(next, entry);
3350 return n;
3353 static const struct got_error *
3354 tree_entry_path(char **path, struct tog_parent_trees *parents,
3355 struct got_tree_entry *te)
3357 const struct got_error *err = NULL;
3358 struct tog_parent_tree *pt;
3359 size_t len = 2; /* for leading slash and NUL */
3361 TAILQ_FOREACH(pt, parents, entry)
3362 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3363 if (te)
3364 len += strlen(te->name);
3366 *path = calloc(1, len);
3367 if (path == NULL)
3368 return got_error_from_errno();
3370 (*path)[0] = '/';
3371 pt = TAILQ_LAST(parents, tog_parent_trees);
3372 while (pt) {
3373 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3374 err = got_error(GOT_ERR_NO_SPACE);
3375 goto done;
3377 if (strlcat(*path, "/", len) >= len) {
3378 err = got_error(GOT_ERR_NO_SPACE);
3379 goto done;
3381 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3383 if (te) {
3384 if (strlcat(*path, te->name, len) >= len) {
3385 err = got_error(GOT_ERR_NO_SPACE);
3386 goto done;
3389 done:
3390 if (err) {
3391 free(*path);
3392 *path = NULL;
3394 return err;
3397 static const struct got_error *
3398 blame_tree_entry(struct tog_view **new_view, int begin_x,
3399 struct got_tree_entry *te, struct tog_parent_trees *parents,
3400 struct got_object_id *commit_id, struct got_reflist_head *refs,
3401 struct got_repository *repo)
3403 const struct got_error *err = NULL;
3404 char *path;
3405 struct tog_view *blame_view;
3407 err = tree_entry_path(&path, parents, te);
3408 if (err)
3409 return err;
3411 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3412 if (blame_view == NULL)
3413 return got_error_from_errno();
3415 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3416 if (err) {
3417 view_close(blame_view);
3418 free(path);
3419 } else
3420 *new_view = blame_view;
3421 return err;
3424 static const struct got_error *
3425 log_tree_entry(struct tog_view **new_view, int begin_x,
3426 struct got_tree_entry *te, struct tog_parent_trees *parents,
3427 struct got_object_id *commit_id, struct got_reflist_head *refs,
3428 struct got_repository *repo)
3430 struct tog_view *log_view;
3431 const struct got_error *err = NULL;
3432 char *path;
3434 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3435 if (log_view == NULL)
3436 return got_error_from_errno();
3438 err = tree_entry_path(&path, parents, te);
3439 if (err)
3440 return err;
3442 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3443 if (err)
3444 view_close(log_view);
3445 else
3446 *new_view = log_view;
3447 free(path);
3448 return err;
3451 static const struct got_error *
3452 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3453 struct got_object_id *commit_id, struct got_reflist_head *refs,
3454 struct got_repository *repo)
3456 const struct got_error *err = NULL;
3457 char *commit_id_str = NULL;
3458 struct tog_tree_view_state *s = &view->state.tree;
3460 TAILQ_INIT(&s->parents);
3462 err = got_object_id_str(&commit_id_str, commit_id);
3463 if (err != NULL)
3464 goto done;
3466 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3467 err = got_error_from_errno();
3468 goto done;
3471 s->root = s->tree = root;
3472 s->entries = got_object_tree_get_entries(root);
3473 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3474 s->commit_id = got_object_id_dup(commit_id);
3475 if (s->commit_id == NULL) {
3476 err = got_error_from_errno();
3477 goto done;
3479 s->refs = refs;
3480 s->repo = repo;
3482 view->show = show_tree_view;
3483 view->input = input_tree_view;
3484 view->close = close_tree_view;
3485 done:
3486 free(commit_id_str);
3487 if (err) {
3488 free(s->tree_label);
3489 s->tree_label = NULL;
3491 return err;
3494 static const struct got_error *
3495 close_tree_view(struct tog_view *view)
3497 struct tog_tree_view_state *s = &view->state.tree;
3499 free(s->tree_label);
3500 s->tree_label = NULL;
3501 free(s->commit_id);
3502 s->commit_id = NULL;
3503 while (!TAILQ_EMPTY(&s->parents)) {
3504 struct tog_parent_tree *parent;
3505 parent = TAILQ_FIRST(&s->parents);
3506 TAILQ_REMOVE(&s->parents, parent, entry);
3507 free(parent);
3510 if (s->tree != s->root)
3511 got_object_tree_close(s->tree);
3512 got_object_tree_close(s->root);
3514 return NULL;
3517 static const struct got_error *
3518 show_tree_view(struct tog_view *view)
3520 const struct got_error *err = NULL;
3521 struct tog_tree_view_state *s = &view->state.tree;
3522 char *parent_path;
3524 err = tree_entry_path(&parent_path, &s->parents, NULL);
3525 if (err)
3526 return err;
3528 err = draw_tree_entries(view, &s->first_displayed_entry,
3529 &s->last_displayed_entry, &s->selected_entry,
3530 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3531 s->entries, s->selected, view->nlines, s->tree == s->root);
3532 free(parent_path);
3534 view_vborder(view);
3535 return err;
3538 static const struct got_error *
3539 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3540 struct tog_view **focus_view, struct tog_view *view, int ch)
3542 const struct got_error *err = NULL;
3543 struct tog_tree_view_state *s = &view->state.tree;
3544 struct tog_view *log_view;
3545 int begin_x = 0, nscrolled;
3547 switch (ch) {
3548 case 'i':
3549 s->show_ids = !s->show_ids;
3550 break;
3551 case 'l':
3552 if (!s->selected_entry)
3553 break;
3554 if (view_is_parent_view(view))
3555 begin_x = view_split_begin_x(view->begin_x);
3556 err = log_tree_entry(&log_view, begin_x,
3557 s->selected_entry, &s->parents,
3558 s->commit_id, s->refs, s->repo);
3559 if (view_is_parent_view(view)) {
3560 err = view_close_child(view);
3561 if (err)
3562 return err;
3563 err = view_set_child(view, log_view);
3564 if (err) {
3565 view_close(log_view);
3566 break;
3568 *focus_view = log_view;
3569 view->child_focussed = 1;
3570 } else
3571 *new_view = log_view;
3572 break;
3573 case 'k':
3574 case KEY_UP:
3575 if (s->selected > 0) {
3576 s->selected--;
3577 if (s->selected == 0)
3578 break;
3580 if (s->selected > 0)
3581 break;
3582 tree_scroll_up(&s->first_displayed_entry, 1,
3583 s->entries, s->tree == s->root);
3584 break;
3585 case KEY_PPAGE:
3586 tree_scroll_up(&s->first_displayed_entry,
3587 MAX(0, view->nlines - 4 - s->selected), s->entries,
3588 s->tree == s->root);
3589 s->selected = 0;
3590 if (SIMPLEQ_FIRST(&s->entries->head) ==
3591 s->first_displayed_entry && s->tree != s->root)
3592 s->first_displayed_entry = NULL;
3593 break;
3594 case 'j':
3595 case KEY_DOWN:
3596 if (s->selected < s->ndisplayed - 1) {
3597 s->selected++;
3598 break;
3600 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3601 == NULL) {
3602 /* can't scroll any further */
3603 break;
3605 tree_scroll_down(&s->first_displayed_entry, 1,
3606 s->last_displayed_entry, s->entries);
3607 break;
3608 case KEY_NPAGE:
3609 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3610 == NULL) {
3611 /* can't scroll any further; move cursor down */
3612 if (s->selected < s->ndisplayed - 1)
3613 s->selected = s->ndisplayed - 1;
3614 break;
3616 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3617 view->nlines, s->last_displayed_entry, s->entries);
3618 if (nscrolled < view->nlines) {
3619 int ndisplayed = 0;
3620 struct got_tree_entry *te;
3621 te = s->first_displayed_entry;
3622 do {
3623 ndisplayed++;
3624 te = SIMPLEQ_NEXT(te, entry);
3625 } while (te);
3626 s->selected = ndisplayed - 1;
3628 break;
3629 case KEY_ENTER:
3630 case '\r':
3631 if (s->selected_entry == NULL) {
3632 struct tog_parent_tree *parent;
3633 case KEY_BACKSPACE:
3634 /* user selected '..' */
3635 if (s->tree == s->root)
3636 break;
3637 parent = TAILQ_FIRST(&s->parents);
3638 TAILQ_REMOVE(&s->parents, parent,
3639 entry);
3640 got_object_tree_close(s->tree);
3641 s->tree = parent->tree;
3642 s->entries =
3643 got_object_tree_get_entries(s->tree);
3644 s->first_displayed_entry =
3645 parent->first_displayed_entry;
3646 s->selected_entry =
3647 parent->selected_entry;
3648 s->selected = parent->selected;
3649 free(parent);
3650 } else if (S_ISDIR(s->selected_entry->mode)) {
3651 struct tog_parent_tree *parent;
3652 struct got_tree_object *child;
3653 err = got_object_open_as_tree(&child,
3654 s->repo, s->selected_entry->id);
3655 if (err)
3656 break;
3657 parent = calloc(1, sizeof(*parent));
3658 if (parent == NULL) {
3659 err = got_error_from_errno();
3660 break;
3662 parent->tree = s->tree;
3663 parent->first_displayed_entry =
3664 s->first_displayed_entry;
3665 parent->selected_entry = s->selected_entry;
3666 parent->selected = s->selected;
3667 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3668 s->tree = child;
3669 s->entries =
3670 got_object_tree_get_entries(s->tree);
3671 s->selected = 0;
3672 s->first_displayed_entry = NULL;
3673 } else if (S_ISREG(s->selected_entry->mode)) {
3674 struct tog_view *blame_view;
3675 int begin_x = view_is_parent_view(view) ?
3676 view_split_begin_x(view->begin_x) : 0;
3678 err = blame_tree_entry(&blame_view, begin_x,
3679 s->selected_entry, &s->parents,
3680 s->commit_id, s->refs, s->repo);
3681 if (err)
3682 break;
3683 if (view_is_parent_view(view)) {
3684 err = view_close_child(view);
3685 if (err)
3686 return err;
3687 err = view_set_child(view, blame_view);
3688 if (err) {
3689 view_close(blame_view);
3690 break;
3692 *focus_view = blame_view;
3693 view->child_focussed = 1;
3694 } else
3695 *new_view = blame_view;
3697 break;
3698 case KEY_RESIZE:
3699 if (s->selected > view->nlines)
3700 s->selected = s->ndisplayed - 1;
3701 break;
3702 default:
3703 break;
3706 return err;
3709 __dead static void
3710 usage_tree(void)
3712 endwin();
3713 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3714 getprogname());
3715 exit(1);
3718 static const struct got_error *
3719 cmd_tree(int argc, char *argv[])
3721 const struct got_error *error;
3722 struct got_repository *repo = NULL;
3723 struct got_reflist_head refs;
3724 char *repo_path = NULL;
3725 struct got_object_id *commit_id = NULL;
3726 char *commit_id_arg = NULL;
3727 struct got_commit_object *commit = NULL;
3728 struct got_tree_object *tree = NULL;
3729 int ch;
3730 struct tog_view *view;
3732 #ifndef PROFILE
3733 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3734 NULL) == -1)
3735 err(1, "pledge");
3736 #endif
3738 while ((ch = getopt(argc, argv, "c:")) != -1) {
3739 switch (ch) {
3740 case 'c':
3741 commit_id_arg = optarg;
3742 break;
3743 default:
3744 usage();
3745 /* NOTREACHED */
3749 argc -= optind;
3750 argv += optind;
3752 if (argc == 0) {
3753 struct got_worktree *worktree;
3754 char *cwd = getcwd(NULL, 0);
3755 if (cwd == NULL)
3756 return got_error_from_errno();
3757 error = got_worktree_open(&worktree, cwd);
3758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3759 goto done;
3760 if (worktree) {
3761 free(cwd);
3762 repo_path =
3763 strdup(got_worktree_get_repo_path(worktree));
3764 got_worktree_close(worktree);
3765 } else
3766 repo_path = cwd;
3767 if (repo_path == NULL) {
3768 error = got_error_from_errno();
3769 goto done;
3771 } else if (argc == 1) {
3772 repo_path = realpath(argv[0], NULL);
3773 if (repo_path == NULL)
3774 return got_error_from_errno();
3775 } else
3776 usage_log();
3778 init_curses();
3780 error = apply_unveil(repo_path, NULL);
3781 if (error)
3782 goto done;
3784 error = got_repo_open(&repo, repo_path);
3785 if (error != NULL)
3786 goto done;
3788 if (commit_id_arg == NULL)
3789 error = get_head_commit_id(&commit_id, repo);
3790 else
3791 error = got_object_resolve_id_str(&commit_id, repo,
3792 commit_id_arg);
3793 if (error != NULL)
3794 goto done;
3796 error = got_object_open_as_commit(&commit, repo, commit_id);
3797 if (error != NULL)
3798 goto done;
3800 error = got_object_open_as_tree(&tree, repo,
3801 got_object_commit_get_tree_id(commit));
3802 if (error != NULL)
3803 goto done;
3805 SIMPLEQ_INIT(&refs);
3806 error = got_ref_list(&refs, repo);
3807 if (error)
3808 goto done;
3810 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3811 if (view == NULL) {
3812 error = got_error_from_errno();
3813 goto done;
3815 error = open_tree_view(view, tree, commit_id, &refs, repo);
3816 if (error)
3817 goto done;
3818 error = view_loop(view);
3819 done:
3820 free(repo_path);
3821 free(commit_id);
3822 if (commit)
3823 got_object_commit_close(commit);
3824 if (tree)
3825 got_object_tree_close(tree);
3826 if (repo)
3827 got_repo_close(repo);
3828 return error;
3831 __dead static void
3832 usage(void)
3834 int i;
3836 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3837 "Available commands:\n", getprogname());
3838 for (i = 0; i < nitems(tog_commands); i++) {
3839 struct tog_cmd *cmd = &tog_commands[i];
3840 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3842 exit(1);
3845 static char **
3846 make_argv(const char *arg0, const char *arg1)
3848 char **argv;
3849 int argc = (arg1 == NULL ? 1 : 2);
3851 argv = calloc(argc, sizeof(char *));
3852 if (argv == NULL)
3853 err(1, "calloc");
3854 argv[0] = strdup(arg0);
3855 if (argv[0] == NULL)
3856 err(1, "calloc");
3857 if (arg1) {
3858 argv[1] = strdup(arg1);
3859 if (argv[1] == NULL)
3860 err(1, "calloc");
3863 return argv;
3866 int
3867 main(int argc, char *argv[])
3869 const struct got_error *error = NULL;
3870 struct tog_cmd *cmd = NULL;
3871 int ch, hflag = 0;
3872 char **cmd_argv = NULL;
3874 setlocale(LC_CTYPE, "");
3876 while ((ch = getopt(argc, argv, "h")) != -1) {
3877 switch (ch) {
3878 case 'h':
3879 hflag = 1;
3880 break;
3881 default:
3882 usage();
3883 /* NOTREACHED */
3887 argc -= optind;
3888 argv += optind;
3889 optind = 0;
3890 optreset = 1;
3892 if (argc == 0) {
3893 if (hflag)
3894 usage();
3895 /* Build an argument vector which runs a default command. */
3896 cmd = &tog_commands[0];
3897 cmd_argv = make_argv(cmd->name, NULL);
3898 argc = 1;
3899 } else {
3900 int i;
3902 /* Did the user specific a command? */
3903 for (i = 0; i < nitems(tog_commands); i++) {
3904 if (strncmp(tog_commands[i].name, argv[0],
3905 strlen(argv[0])) == 0) {
3906 cmd = &tog_commands[i];
3907 if (hflag)
3908 tog_commands[i].cmd_usage();
3909 break;
3912 if (cmd == NULL) {
3913 /* Did the user specify a repository? */
3914 char *repo_path = realpath(argv[0], NULL);
3915 if (repo_path) {
3916 struct got_repository *repo;
3917 error = got_repo_open(&repo, repo_path);
3918 if (error == NULL)
3919 got_repo_close(repo);
3920 } else
3921 error = got_error_from_errno();
3922 if (error) {
3923 if (hflag) {
3924 fprintf(stderr, "%s: '%s' is not a "
3925 "known command\n", getprogname(),
3926 argv[0]);
3927 usage();
3929 fprintf(stderr, "%s: '%s' is neither a known "
3930 "command nor a path to a repository\n",
3931 getprogname(), argv[0]);
3932 free(repo_path);
3933 return 1;
3935 cmd = &tog_commands[0];
3936 cmd_argv = make_argv(cmd->name, repo_path);
3937 argc = 2;
3938 free(repo_path);
3942 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3943 if (error)
3944 goto done;
3945 done:
3946 endwin();
3947 free(cmd_argv);
3948 if (error)
3949 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3950 return 0;