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 (commits_needed == 0)
1156 halfdelay(10); /* disable fast refresh */
1158 if (asprintf(&ncommits_str, " [%d/%d] %s",
1159 entry ? entry->idx + 1 : 0, commits->ncommits,
1160 commits_needed > 0 ? "loading... " :
1161 (refs_str ? refs_str : "")) == -1) {
1162 err = got_error_from_errno();
1163 goto done;
1166 if (path && strcmp(path, "/") != 0) {
1167 if (asprintf(&header, "commit %s %s%s",
1168 id_str ? id_str : "........................................",
1169 path, ncommits_str) == -1) {
1170 err = got_error_from_errno();
1171 header = NULL;
1172 goto done;
1174 } else if (asprintf(&header, "commit %s%s",
1175 id_str ? id_str : "........................................",
1176 ncommits_str) == -1) {
1177 err = got_error_from_errno();
1178 header = NULL;
1179 goto done;
1181 err = format_line(&wline, &width, header, view->ncols);
1182 if (err)
1183 goto done;
1185 werase(view->window);
1187 if (view_needs_focus_indication(view))
1188 wstandout(view->window);
1189 waddwstr(view->window, wline);
1190 while (width < view->ncols) {
1191 waddch(view->window, ' ');
1192 width++;
1194 if (view_needs_focus_indication(view))
1195 wstandend(view->window);
1196 free(wline);
1197 if (limit <= 1)
1198 goto done;
1200 entry = first;
1201 *last = first;
1202 ncommits = 0;
1203 while (entry) {
1204 if (ncommits >= limit - 1)
1205 break;
1206 if (view->focussed && ncommits == selected_idx)
1207 wstandout(view->window);
1208 err = draw_commit(view, entry->commit, entry->id, refs);
1209 if (view->focussed && ncommits == selected_idx)
1210 wstandend(view->window);
1211 if (err)
1212 break;
1213 ncommits++;
1214 *last = entry;
1215 entry = TAILQ_NEXT(entry, entry);
1218 view_vborder(view);
1219 done:
1220 free(id_str);
1221 free(refs_str);
1222 free(ncommits_str);
1223 free(header);
1224 return err;
1227 static void
1228 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1229 struct commit_queue *commits)
1231 struct commit_queue_entry *entry;
1232 int nscrolled = 0;
1234 entry = TAILQ_FIRST(&commits->head);
1235 if (*first_displayed_entry == entry)
1236 return;
1238 entry = *first_displayed_entry;
1239 while (entry && nscrolled < maxscroll) {
1240 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1241 if (entry) {
1242 *first_displayed_entry = entry;
1243 nscrolled++;
1248 static const struct got_error *
1249 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1250 pthread_cond_t *need_commits)
1252 int errcode;
1254 while (*commits_needed > 0) {
1255 if (*log_complete)
1256 break;
1258 /* Wake the log thread. */
1259 errcode = pthread_cond_signal(need_commits);
1260 if (errcode)
1261 return got_error_set_errno(errcode);
1262 errcode = pthread_mutex_unlock(&tog_mutex);
1263 if (errcode)
1264 return got_error_set_errno(errcode);
1265 pthread_yield();
1266 errcode = pthread_mutex_lock(&tog_mutex);
1267 if (errcode)
1268 return got_error_set_errno(errcode);
1270 if (*commits_needed > 0 && !load_all) {
1272 * Thread is not done yet; lose a key press
1273 * and let the user retry... this way the GUI
1274 * remains interactive while logging deep paths
1275 * with few commits in history.
1277 halfdelay(1); /* fast refresh while loading */
1278 return NULL;
1282 return NULL;
1285 static const struct got_error *
1286 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1287 struct commit_queue_entry **last_displayed_entry,
1288 struct commit_queue *commits, int *log_complete, int *commits_needed,
1289 pthread_cond_t *need_commits)
1291 const struct got_error *err = NULL;
1292 struct commit_queue_entry *pentry;
1293 int nscrolled = 0;
1295 if (*last_displayed_entry == NULL)
1296 return NULL;
1298 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1299 if (pentry == NULL && !*log_complete) {
1301 * Ask the log thread for required amount of commits
1302 * plus some amount of pre-fetching.
1304 (*commits_needed) += maxscroll + 20;
1305 err = trigger_log_thread(0, commits_needed, log_complete,
1306 need_commits);
1307 if (err)
1308 return err;
1311 do {
1312 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1313 if (pentry == NULL)
1314 break;
1316 *last_displayed_entry = pentry;
1318 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1319 if (pentry == NULL)
1320 break;
1321 *first_displayed_entry = pentry;
1322 } while (++nscrolled < maxscroll);
1324 return err;
1327 static const struct got_error *
1328 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1329 struct got_commit_object *commit, struct got_object_id *commit_id,
1330 struct tog_view *log_view, struct got_reflist_head *refs,
1331 struct got_repository *repo)
1333 const struct got_error *err;
1334 struct got_object_qid *parent_id;
1335 struct tog_view *diff_view;
1337 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1338 if (diff_view == NULL)
1339 return got_error_from_errno();
1341 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1342 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1343 commit_id, log_view, refs, repo);
1344 if (err == NULL)
1345 *new_view = diff_view;
1346 return err;
1349 static const struct got_error *
1350 browse_commit(struct tog_view **new_view, int begin_x,
1351 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1352 struct got_repository *repo)
1354 const struct got_error *err = NULL;
1355 struct got_tree_object *tree;
1356 struct tog_view *tree_view;
1358 err = got_object_open_as_tree(&tree, repo,
1359 got_object_commit_get_tree_id(entry->commit));
1360 if (err)
1361 return err;
1363 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1364 if (tree_view == NULL)
1365 return got_error_from_errno();
1367 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1368 if (err)
1369 got_object_tree_close(tree);
1370 else
1371 *new_view = tree_view;
1372 return err;
1375 static void *
1376 log_thread(void *arg)
1378 const struct got_error *err = NULL;
1379 int errcode = 0;
1380 struct tog_log_thread_args *a = arg;
1381 int done = 0;
1383 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1384 if (err)
1385 return (void *)err;
1387 while (!done && !err) {
1388 err = queue_commits(a->graph, a->commits, 1, a->repo,
1389 a->in_repo_path);
1390 if (err) {
1391 if (err->code != GOT_ERR_ITER_COMPLETED)
1392 return (void *)err;
1393 err = NULL;
1394 done = 1;
1395 } else if (a->commits_needed > 0)
1396 a->commits_needed--;
1398 errcode = pthread_mutex_lock(&tog_mutex);
1399 if (errcode)
1400 return (void *)got_error_set_errno(errcode);
1402 if (done)
1403 a->log_complete = 1;
1404 else if (*a->quit) {
1405 done = 1;
1406 a->log_complete = 1;
1407 } else if (*a->first_displayed_entry == NULL) {
1408 *a->first_displayed_entry =
1409 TAILQ_FIRST(&a->commits->head);
1410 *a->selected_entry = *a->first_displayed_entry;
1413 if (done)
1414 a->commits_needed = 0;
1415 else if (a->commits_needed == 0) {
1416 errcode = pthread_cond_wait(&a->need_commits,
1417 &tog_mutex);
1418 if (errcode)
1419 err = got_error_set_errno(errcode);
1422 errcode = pthread_mutex_unlock(&tog_mutex);
1423 if (errcode && err == NULL)
1424 err = got_error_set_errno(errcode);
1426 return (void *)err;
1429 static const struct got_error *
1430 stop_log_thread(struct tog_log_view_state *s)
1432 const struct got_error *err = NULL;
1433 int errcode;
1435 if (s->thread) {
1436 s->quit = 1;
1437 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1438 if (errcode)
1439 return got_error_set_errno(errcode);
1440 errcode = pthread_mutex_unlock(&tog_mutex);
1441 if (errcode)
1442 return got_error_set_errno(errcode);
1443 errcode = pthread_join(s->thread, (void **)&err);
1444 if (errcode)
1445 return got_error_set_errno(errcode);
1446 errcode = pthread_mutex_lock(&tog_mutex);
1447 if (errcode)
1448 return got_error_set_errno(errcode);
1449 s->thread = NULL;
1452 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1453 if (errcode && err == NULL)
1454 err = got_error_set_errno(errcode);
1456 if (s->thread_args.repo) {
1457 got_repo_close(s->thread_args.repo);
1458 s->thread_args.repo = NULL;
1461 if (s->thread_args.graph) {
1462 got_commit_graph_close(s->thread_args.graph);
1463 s->thread_args.graph = NULL;
1466 return err;
1469 static const struct got_error *
1470 close_log_view(struct tog_view *view)
1472 const struct got_error *err = NULL;
1473 struct tog_log_view_state *s = &view->state.log;
1475 err = stop_log_thread(s);
1476 free_commits(&s->commits);
1477 free(s->in_repo_path);
1478 s->in_repo_path = NULL;
1479 free(s->start_id);
1480 s->start_id = NULL;
1481 return err;
1484 static const struct got_error *
1485 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1486 struct got_reflist_head *refs, struct got_repository *repo,
1487 const char *path, int check_disk)
1489 const struct got_error *err = NULL;
1490 struct tog_log_view_state *s = &view->state.log;
1491 struct got_repository *thread_repo = NULL;
1492 struct got_commit_graph *thread_graph = NULL;
1493 int errcode;
1495 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1496 if (err != NULL)
1497 goto done;
1499 /* The commit queue only contains commits being displayed. */
1500 TAILQ_INIT(&s->commits.head);
1501 s->commits.ncommits = 0;
1503 s->refs = refs;
1504 s->repo = repo;
1505 s->start_id = got_object_id_dup(start_id);
1506 if (s->start_id == NULL) {
1507 err = got_error_from_errno();
1508 goto done;
1511 view->show = show_log_view;
1512 view->input = input_log_view;
1513 view->close = close_log_view;
1515 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1516 if (err)
1517 goto done;
1518 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1519 0, thread_repo);
1520 if (err)
1521 goto done;
1523 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1524 if (errcode) {
1525 err = got_error_set_errno(errcode);
1526 goto done;
1529 s->thread_args.commits_needed = view->nlines;
1530 s->thread_args.graph = thread_graph;
1531 s->thread_args.commits = &s->commits;
1532 s->thread_args.in_repo_path = s->in_repo_path;
1533 s->thread_args.start_id = s->start_id;
1534 s->thread_args.repo = thread_repo;
1535 s->thread_args.log_complete = 0;
1536 s->thread_args.quit = &s->quit;
1537 s->thread_args.view = view;
1538 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1539 s->thread_args.selected_entry = &s->selected_entry;
1540 done:
1541 if (err)
1542 close_log_view(view);
1543 return err;
1546 static const struct got_error *
1547 show_log_view(struct tog_view *view)
1549 struct tog_log_view_state *s = &view->state.log;
1551 if (s->thread == NULL) {
1552 int errcode = pthread_create(&s->thread, NULL, log_thread,
1553 &s->thread_args);
1554 if (errcode)
1555 return got_error_set_errno(errcode);
1558 return draw_commits(view, &s->last_displayed_entry,
1559 &s->selected_entry, s->first_displayed_entry,
1560 &s->commits, s->selected, view->nlines, s->refs,
1561 s->in_repo_path, s->thread_args.commits_needed);
1564 static const struct got_error *
1565 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1566 struct tog_view **focus_view, struct tog_view *view, int ch)
1568 const struct got_error *err = NULL;
1569 struct tog_log_view_state *s = &view->state.log;
1570 char *parent_path;
1571 struct tog_view *diff_view = NULL, *tree_view = NULL;
1572 int begin_x = 0;
1574 switch (ch) {
1575 case 'q':
1576 s->quit = 1;
1577 break;
1578 case 'k':
1579 case KEY_UP:
1580 case '<':
1581 case ',':
1582 if (s->first_displayed_entry == NULL)
1583 break;
1584 if (s->selected > 0)
1585 s->selected--;
1586 if (s->selected > 0)
1587 break;
1588 scroll_up(&s->first_displayed_entry, 1,
1589 &s->commits);
1590 break;
1591 case KEY_PPAGE:
1592 if (s->first_displayed_entry == NULL)
1593 break;
1594 if (TAILQ_FIRST(&s->commits.head) ==
1595 s->first_displayed_entry) {
1596 s->selected = 0;
1597 break;
1599 scroll_up(&s->first_displayed_entry,
1600 view->nlines, &s->commits);
1601 break;
1602 case 'j':
1603 case KEY_DOWN:
1604 case '>':
1605 case '.':
1606 if (s->first_displayed_entry == NULL)
1607 break;
1608 if (s->selected < MIN(view->nlines - 2,
1609 s->commits.ncommits - 1)) {
1610 s->selected++;
1611 break;
1613 err = scroll_down(&s->first_displayed_entry, 1,
1614 &s->last_displayed_entry, &s->commits,
1615 &s->thread_args.log_complete,
1616 &s->thread_args.commits_needed,
1617 &s->thread_args.need_commits);
1618 break;
1619 case KEY_NPAGE: {
1620 struct commit_queue_entry *first;
1621 first = s->first_displayed_entry;
1622 if (first == NULL)
1623 break;
1624 err = scroll_down(&s->first_displayed_entry,
1625 view->nlines, &s->last_displayed_entry,
1626 &s->commits, &s->thread_args.log_complete,
1627 &s->thread_args.commits_needed,
1628 &s->thread_args.need_commits);
1629 if (first == s->first_displayed_entry &&
1630 s->selected < MIN(view->nlines - 2,
1631 s->commits.ncommits - 1)) {
1632 /* can't scroll further down */
1633 s->selected = MIN(view->nlines - 2,
1634 s->commits.ncommits - 1);
1636 err = NULL;
1637 break;
1639 case KEY_RESIZE:
1640 if (s->selected > view->nlines - 2)
1641 s->selected = view->nlines - 2;
1642 if (s->selected > s->commits.ncommits - 1)
1643 s->selected = s->commits.ncommits - 1;
1644 break;
1645 case KEY_ENTER:
1646 case '\r':
1647 if (s->selected_entry == NULL)
1648 break;
1649 if (view_is_parent_view(view))
1650 begin_x = view_split_begin_x(view->begin_x);
1651 err = open_diff_view_for_commit(&diff_view, begin_x,
1652 s->selected_entry->commit, s->selected_entry->id,
1653 view, s->refs, s->repo);
1654 if (err)
1655 break;
1656 if (view_is_parent_view(view)) {
1657 err = view_close_child(view);
1658 if (err)
1659 return err;
1660 err = view_set_child(view, diff_view);
1661 if (err) {
1662 view_close(diff_view);
1663 break;
1665 *focus_view = diff_view;
1666 view->child_focussed = 1;
1667 } else
1668 *new_view = diff_view;
1669 break;
1670 case 't':
1671 if (s->selected_entry == NULL)
1672 break;
1673 if (view_is_parent_view(view))
1674 begin_x = view_split_begin_x(view->begin_x);
1675 err = browse_commit(&tree_view, begin_x,
1676 s->selected_entry, s->refs, s->repo);
1677 if (err)
1678 break;
1679 if (view_is_parent_view(view)) {
1680 err = view_close_child(view);
1681 if (err)
1682 return err;
1683 err = view_set_child(view, tree_view);
1684 if (err) {
1685 view_close(tree_view);
1686 break;
1688 *focus_view = tree_view;
1689 view->child_focussed = 1;
1690 } else
1691 *new_view = tree_view;
1692 break;
1693 case KEY_BACKSPACE:
1694 if (strcmp(s->in_repo_path, "/") == 0)
1695 break;
1696 parent_path = dirname(s->in_repo_path);
1697 if (parent_path && strcmp(parent_path, ".") != 0) {
1698 struct tog_view *lv;
1699 err = stop_log_thread(s);
1700 if (err)
1701 return err;
1702 lv = view_open(view->nlines, view->ncols,
1703 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1704 if (lv == NULL)
1705 return got_error_from_errno();
1706 err = open_log_view(lv, s->start_id, s->refs,
1707 s->repo, parent_path, 0);
1708 if (err)
1709 return err;;
1710 if (view_is_parent_view(view))
1711 *new_view = lv;
1712 else {
1713 view_set_child(view->parent, lv);
1714 *focus_view = lv;
1716 return NULL;
1718 break;
1719 default:
1720 break;
1723 return err;
1726 static const struct got_error *
1727 apply_unveil(const char *repo_path, const char *worktree_path)
1729 const struct got_error *error;
1731 if (repo_path && unveil(repo_path, "r") != 0)
1732 return got_error_from_errno();
1734 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1735 return got_error_from_errno();
1737 if (unveil("/tmp", "rwc") != 0)
1738 return got_error_from_errno();
1740 error = got_privsep_unveil_exec_helpers();
1741 if (error != NULL)
1742 return error;
1744 if (unveil(NULL, NULL) != 0)
1745 return got_error_from_errno();
1747 return NULL;
1750 static void
1751 init_curses(void)
1753 initscr();
1754 cbreak();
1755 halfdelay(1); /* Do fast refresh while initial view is loading. */
1756 noecho();
1757 nonl();
1758 intrflush(stdscr, FALSE);
1759 keypad(stdscr, TRUE);
1760 curs_set(0);
1761 signal(SIGWINCH, tog_sigwinch);
1764 static const struct got_error *
1765 cmd_log(int argc, char *argv[])
1767 const struct got_error *error;
1768 struct got_repository *repo = NULL;
1769 struct got_reflist_head refs;
1770 struct got_object_id *start_id = NULL;
1771 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1772 char *start_commit = NULL;
1773 int ch;
1774 struct tog_view *view;
1776 #ifndef PROFILE
1777 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1778 NULL) == -1)
1779 err(1, "pledge");
1780 #endif
1782 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1783 switch (ch) {
1784 case 'c':
1785 start_commit = optarg;
1786 break;
1787 case 'r':
1788 repo_path = realpath(optarg, NULL);
1789 if (repo_path == NULL)
1790 err(1, "-r option");
1791 break;
1792 default:
1793 usage();
1794 /* NOTREACHED */
1798 argc -= optind;
1799 argv += optind;
1801 if (argc == 0)
1802 path = strdup("");
1803 else if (argc == 1)
1804 path = strdup(argv[0]);
1805 else
1806 usage_log();
1807 if (path == NULL)
1808 return got_error_from_errno();
1810 cwd = getcwd(NULL, 0);
1811 if (cwd == NULL) {
1812 error = got_error_from_errno();
1813 goto done;
1815 if (repo_path == NULL) {
1816 struct got_worktree *worktree;
1817 error = got_worktree_open(&worktree, cwd);
1818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1819 goto done;
1820 if (worktree) {
1821 repo_path =
1822 strdup(got_worktree_get_repo_path(worktree));
1823 got_worktree_close(worktree);
1824 } else
1825 repo_path = strdup(cwd);
1826 if (repo_path == NULL) {
1827 error = got_error_from_errno();
1828 goto done;
1832 init_curses();
1834 error = apply_unveil(repo_path, NULL);
1835 if (error)
1836 goto done;
1838 error = got_repo_open(&repo, repo_path);
1839 if (error != NULL)
1840 goto done;
1842 if (start_commit == NULL)
1843 error = get_head_commit_id(&start_id, repo);
1844 else
1845 error = got_object_resolve_id_str(&start_id, repo,
1846 start_commit);
1847 if (error != NULL)
1848 goto done;
1850 SIMPLEQ_INIT(&refs);
1851 error = got_ref_list(&refs, repo);
1852 if (error)
1853 goto done;
1855 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1856 if (view == NULL) {
1857 error = got_error_from_errno();
1858 goto done;
1860 error = open_log_view(view, start_id, &refs, repo, path, 1);
1861 if (error)
1862 goto done;
1863 error = view_loop(view);
1864 done:
1865 free(repo_path);
1866 free(cwd);
1867 free(path);
1868 free(start_id);
1869 if (repo)
1870 got_repo_close(repo);
1871 return error;
1874 __dead static void
1875 usage_diff(void)
1877 endwin();
1878 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1879 getprogname());
1880 exit(1);
1883 static char *
1884 parse_next_line(FILE *f, size_t *len)
1886 char *line;
1887 size_t linelen;
1888 size_t lineno;
1889 const char delim[3] = { '\0', '\0', '\0'};
1891 line = fparseln(f, &linelen, &lineno, delim, 0);
1892 if (len)
1893 *len = linelen;
1894 return line;
1897 static const struct got_error *
1898 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1899 int *last_displayed_line, int *eof, int max_lines,
1900 char * header)
1902 const struct got_error *err;
1903 int nlines = 0, nprinted = 0;
1904 char *line;
1905 size_t len;
1906 wchar_t *wline;
1907 int width;
1909 rewind(f);
1910 werase(view->window);
1912 if (header) {
1913 err = format_line(&wline, &width, header, view->ncols);
1914 if (err) {
1915 return err;
1918 if (view_needs_focus_indication(view))
1919 wstandout(view->window);
1920 waddwstr(view->window, wline);
1921 if (view_needs_focus_indication(view))
1922 wstandend(view->window);
1923 if (width < view->ncols)
1924 waddch(view->window, '\n');
1926 if (max_lines <= 1)
1927 return NULL;
1928 max_lines--;
1931 *eof = 0;
1932 while (nprinted < max_lines) {
1933 line = parse_next_line(f, &len);
1934 if (line == NULL) {
1935 *eof = 1;
1936 break;
1938 if (++nlines < *first_displayed_line) {
1939 free(line);
1940 continue;
1943 err = format_line(&wline, &width, line, view->ncols);
1944 if (err) {
1945 free(line);
1946 return err;
1948 waddwstr(view->window, wline);
1949 if (width < view->ncols)
1950 waddch(view->window, '\n');
1951 if (++nprinted == 1)
1952 *first_displayed_line = nlines;
1953 free(line);
1954 free(wline);
1955 wline = NULL;
1957 *last_displayed_line = nlines;
1959 view_vborder(view);
1961 return NULL;
1964 static char *
1965 get_datestr(time_t *time, char *datebuf)
1967 char *p, *s = ctime_r(time, datebuf);
1968 p = strchr(s, '\n');
1969 if (p)
1970 *p = '\0';
1971 return s;
1974 static const struct got_error *
1975 write_commit_info(struct got_object_id *commit_id,
1976 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
1978 const struct got_error *err = NULL;
1979 char datebuf[26];
1980 struct got_commit_object *commit;
1981 char *id_str = NULL;
1982 time_t committer_time;
1983 const char *author, *committer;
1984 char *refs_str = NULL;
1986 if (refs) {
1987 err = build_refs_str(&refs_str, refs, commit_id);
1988 if (err)
1989 return err;
1992 err = got_object_open_as_commit(&commit, repo, commit_id);
1993 if (err)
1994 return err;
1996 err = got_object_id_str(&id_str, commit_id);
1997 if (err) {
1998 err = got_error_from_errno();
1999 goto done;
2002 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2003 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2004 err = got_error_from_errno();
2005 goto done;
2007 if (fprintf(outfile, "from: %s\n",
2008 got_object_commit_get_author(commit)) < 0) {
2009 err = got_error_from_errno();
2010 goto done;
2012 committer_time = got_object_commit_get_committer_time(commit);
2013 if (fprintf(outfile, "date: %s UTC\n",
2014 get_datestr(&committer_time, datebuf)) < 0) {
2015 err = got_error_from_errno();
2016 goto done;
2018 author = got_object_commit_get_author(commit);
2019 committer = got_object_commit_get_committer(commit);
2020 if (strcmp(author, committer) != 0 &&
2021 fprintf(outfile, "via: %s\n", committer) < 0) {
2022 err = got_error_from_errno();
2023 goto done;
2025 if (fprintf(outfile, "%s\n",
2026 got_object_commit_get_logmsg(commit)) < 0) {
2027 err = got_error_from_errno();
2028 goto done;
2030 done:
2031 free(id_str);
2032 free(refs_str);
2033 got_object_commit_close(commit);
2034 return err;
2037 static const struct got_error *
2038 create_diff(struct tog_diff_view_state *s)
2040 const struct got_error *err = NULL;
2041 FILE *f = NULL;
2042 int obj_type;
2044 f = got_opentemp();
2045 if (f == NULL) {
2046 err = got_error_from_errno();
2047 goto done;
2049 if (s->f && fclose(s->f) != 0) {
2050 err = got_error_from_errno();
2051 goto done;
2053 s->f = f;
2055 if (s->id1)
2056 err = got_object_get_type(&obj_type, s->repo, s->id1);
2057 else
2058 err = got_object_get_type(&obj_type, s->repo, s->id2);
2059 if (err)
2060 goto done;
2062 switch (obj_type) {
2063 case GOT_OBJ_TYPE_BLOB:
2064 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2065 s->diff_context, s->repo, f);
2066 break;
2067 case GOT_OBJ_TYPE_TREE:
2068 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2069 s->diff_context, s->repo, f);
2070 break;
2071 case GOT_OBJ_TYPE_COMMIT: {
2072 const struct got_object_id_queue *parent_ids;
2073 struct got_object_qid *pid;
2074 struct got_commit_object *commit2;
2076 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2077 if (err)
2078 break;
2079 /* Show commit info if we're diffing to a parent/root commit. */
2080 if (s->id1 == NULL)
2081 write_commit_info(s->id2, s->refs, s->repo, f);
2082 else {
2083 parent_ids = got_object_commit_get_parent_ids(commit2);
2084 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2085 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2086 write_commit_info(s->id2, s->refs,
2087 s->repo, f);
2088 break;
2092 got_object_commit_close(commit2);
2094 err = got_diff_objects_as_commits(s->id1, s->id2,
2095 s->diff_context, s->repo, f);
2096 break;
2098 default:
2099 err = got_error(GOT_ERR_OBJ_TYPE);
2100 break;
2102 done:
2103 if (f && fflush(f) != 0 && err == NULL)
2104 err = got_error_from_errno();
2105 return err;
2108 static const struct got_error *
2109 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2110 struct got_object_id *id2, struct tog_view *log_view,
2111 struct got_reflist_head *refs, struct got_repository *repo)
2113 const struct got_error *err;
2115 if (id1 != NULL && id2 != NULL) {
2116 int type1, type2;
2117 err = got_object_get_type(&type1, repo, id1);
2118 if (err)
2119 return err;
2120 err = got_object_get_type(&type2, repo, id2);
2121 if (err)
2122 return err;
2124 if (type1 != type2)
2125 return got_error(GOT_ERR_OBJ_TYPE);
2128 if (id1) {
2129 view->state.diff.id1 = got_object_id_dup(id1);
2130 if (view->state.diff.id1 == NULL)
2131 return got_error_from_errno();
2132 } else
2133 view->state.diff.id1 = NULL;
2135 view->state.diff.id2 = got_object_id_dup(id2);
2136 if (view->state.diff.id2 == NULL) {
2137 free(view->state.diff.id1);
2138 view->state.diff.id1 = NULL;
2139 return got_error_from_errno();
2141 view->state.diff.f = NULL;
2142 view->state.diff.first_displayed_line = 1;
2143 view->state.diff.last_displayed_line = view->nlines;
2144 view->state.diff.diff_context = 3;
2145 view->state.diff.log_view = log_view;
2146 view->state.diff.repo = repo;
2147 view->state.diff.refs = refs;
2149 err = create_diff(&view->state.diff);
2150 if (err) {
2151 free(view->state.diff.id1);
2152 view->state.diff.id1 = NULL;
2153 free(view->state.diff.id2);
2154 view->state.diff.id2 = NULL;
2155 return err;
2158 view->show = show_diff_view;
2159 view->input = input_diff_view;
2160 view->close = close_diff_view;
2162 return NULL;
2165 static const struct got_error *
2166 close_diff_view(struct tog_view *view)
2168 const struct got_error *err = NULL;
2170 free(view->state.diff.id1);
2171 view->state.diff.id1 = NULL;
2172 free(view->state.diff.id2);
2173 view->state.diff.id2 = NULL;
2174 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2175 err = got_error_from_errno();
2176 return err;
2179 static const struct got_error *
2180 show_diff_view(struct tog_view *view)
2182 const struct got_error *err;
2183 struct tog_diff_view_state *s = &view->state.diff;
2184 char *id_str1 = NULL, *id_str2, *header;
2186 if (s->id1) {
2187 err = got_object_id_str(&id_str1, s->id1);
2188 if (err)
2189 return err;
2191 err = got_object_id_str(&id_str2, s->id2);
2192 if (err)
2193 return err;
2195 if (asprintf(&header, "diff %s %s",
2196 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2197 err = got_error_from_errno();
2198 free(id_str1);
2199 free(id_str2);
2200 return err;
2202 free(id_str1);
2203 free(id_str2);
2205 return draw_file(view, s->f, &s->first_displayed_line,
2206 &s->last_displayed_line, &s->eof, view->nlines,
2207 header);
2210 static const struct got_error *
2211 set_selected_commit(struct tog_diff_view_state *s,
2212 struct commit_queue_entry *entry)
2214 const struct got_error *err;
2215 const struct got_object_id_queue *parent_ids;
2216 struct got_commit_object *selected_commit;
2217 struct got_object_qid *pid;
2219 free(s->id2);
2220 s->id2 = got_object_id_dup(entry->id);
2221 if (s->id2 == NULL)
2222 return got_error_from_errno();
2224 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2225 if (err)
2226 return err;
2227 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2228 free(s->id1);
2229 pid = SIMPLEQ_FIRST(parent_ids);
2230 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2231 got_object_commit_close(selected_commit);
2232 return NULL;
2235 static const struct got_error *
2236 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2237 struct tog_view **focus_view, struct tog_view *view, int ch)
2239 const struct got_error *err = NULL;
2240 struct tog_diff_view_state *s = &view->state.diff;
2241 struct tog_log_view_state *ls;
2242 struct commit_queue_entry *entry;
2243 int i;
2245 switch (ch) {
2246 case 'k':
2247 case KEY_UP:
2248 if (s->first_displayed_line > 1)
2249 s->first_displayed_line--;
2250 break;
2251 case KEY_PPAGE:
2252 i = 0;
2253 while (i++ < view->nlines - 1 &&
2254 s->first_displayed_line > 1)
2255 s->first_displayed_line--;
2256 break;
2257 case 'j':
2258 case KEY_DOWN:
2259 if (!s->eof)
2260 s->first_displayed_line++;
2261 break;
2262 case KEY_NPAGE:
2263 case ' ':
2264 i = 0;
2265 while (!s->eof && i++ < view->nlines - 1) {
2266 char *line;
2267 line = parse_next_line(s->f, NULL);
2268 s->first_displayed_line++;
2269 if (line == NULL)
2270 break;
2272 break;
2273 case '[':
2274 if (s->diff_context > 0) {
2275 s->diff_context--;
2276 err = create_diff(s);
2278 break;
2279 case ']':
2280 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2281 s->diff_context++;
2282 err = create_diff(s);
2284 break;
2285 case '<':
2286 case ',':
2287 if (s->log_view == NULL)
2288 break;
2289 ls = &s->log_view->state.log;
2290 entry = TAILQ_PREV(ls->selected_entry,
2291 commit_queue_head, entry);
2292 if (entry == NULL)
2293 break;
2295 err = input_log_view(NULL, NULL, NULL, s->log_view,
2296 KEY_UP);
2297 if (err)
2298 break;
2300 err = set_selected_commit(s, entry);
2301 if (err)
2302 break;
2304 s->first_displayed_line = 1;
2305 s->last_displayed_line = view->nlines;
2307 err = create_diff(s);
2308 break;
2309 case '>':
2310 case '.':
2311 if (s->log_view == NULL)
2312 break;
2313 ls = &s->log_view->state.log;
2315 if (ls->thread_args.commits_needed == 0) {
2316 ls->thread_args.commits_needed++;
2318 /* Display "loading..." in log view. */
2319 show_log_view(s->log_view);
2320 update_panels();
2321 doupdate();
2323 err = trigger_log_thread(1 /* load_all */,
2324 &ls->thread_args.commits_needed,
2325 &ls->thread_args.log_complete,
2326 &ls->thread_args.need_commits);
2327 if (err)
2328 break;
2330 err = input_log_view(NULL, NULL, NULL, s->log_view,
2331 KEY_DOWN);
2332 if (err)
2333 break;
2335 entry = TAILQ_NEXT(ls->selected_entry, entry);
2336 if (entry == NULL)
2337 break;
2339 err = set_selected_commit(s, entry);
2340 if (err)
2341 break;
2343 s->first_displayed_line = 1;
2344 s->last_displayed_line = view->nlines;
2346 err = create_diff(s);
2347 break;
2348 default:
2349 break;
2352 return err;
2355 static const struct got_error *
2356 cmd_diff(int argc, char *argv[])
2358 const struct got_error *error = NULL;
2359 struct got_repository *repo = NULL;
2360 struct got_reflist_head refs;
2361 struct got_object_id *id1 = NULL, *id2 = NULL;
2362 char *repo_path = NULL;
2363 char *id_str1 = NULL, *id_str2 = NULL;
2364 int ch;
2365 struct tog_view *view;
2367 #ifndef PROFILE
2368 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2369 NULL) == -1)
2370 err(1, "pledge");
2371 #endif
2373 while ((ch = getopt(argc, argv, "")) != -1) {
2374 switch (ch) {
2375 default:
2376 usage();
2377 /* NOTREACHED */
2381 argc -= optind;
2382 argv += optind;
2384 if (argc == 0) {
2385 usage_diff(); /* TODO show local worktree changes */
2386 } else if (argc == 2) {
2387 repo_path = getcwd(NULL, 0);
2388 if (repo_path == NULL)
2389 return got_error_from_errno();
2390 id_str1 = argv[0];
2391 id_str2 = argv[1];
2392 } else if (argc == 3) {
2393 repo_path = realpath(argv[0], NULL);
2394 if (repo_path == NULL)
2395 return got_error_from_errno();
2396 id_str1 = argv[1];
2397 id_str2 = argv[2];
2398 } else
2399 usage_diff();
2401 init_curses();
2403 error = apply_unveil(repo_path, NULL);
2404 if (error)
2405 goto done;
2407 error = got_repo_open(&repo, repo_path);
2408 free(repo_path);
2409 if (error)
2410 goto done;
2412 error = got_object_resolve_id_str(&id1, repo, id_str1);
2413 if (error)
2414 goto done;
2416 error = got_object_resolve_id_str(&id2, repo, id_str2);
2417 if (error)
2418 goto done;
2420 SIMPLEQ_INIT(&refs);
2421 error = got_ref_list(&refs, repo);
2422 if (error)
2423 goto done;
2425 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2426 if (view == NULL) {
2427 error = got_error_from_errno();
2428 goto done;
2430 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2431 if (error)
2432 goto done;
2433 error = view_loop(view);
2434 done:
2435 got_repo_close(repo);
2436 return error;
2439 __dead static void
2440 usage_blame(void)
2442 endwin();
2443 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2444 getprogname());
2445 exit(1);
2448 struct tog_blame_line {
2449 int annotated;
2450 struct got_object_id *id;
2453 static const struct got_error *
2454 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2455 const char *path, struct tog_blame_line *lines, int nlines,
2456 int blame_complete, int selected_line, int *first_displayed_line,
2457 int *last_displayed_line, int *eof, int max_lines)
2459 const struct got_error *err;
2460 int lineno = 0, nprinted = 0;
2461 char *line;
2462 size_t len;
2463 wchar_t *wline;
2464 int width, wlimit;
2465 struct tog_blame_line *blame_line;
2466 struct got_object_id *prev_id = NULL;
2467 char *id_str;
2469 err = got_object_id_str(&id_str, id);
2470 if (err)
2471 return err;
2473 rewind(f);
2474 werase(view->window);
2476 if (asprintf(&line, "commit %s", id_str) == -1) {
2477 err = got_error_from_errno();
2478 free(id_str);
2479 return err;
2482 err = format_line(&wline, &width, line, view->ncols);
2483 free(line);
2484 line = NULL;
2485 if (view_needs_focus_indication(view))
2486 wstandout(view->window);
2487 waddwstr(view->window, wline);
2488 if (view_needs_focus_indication(view))
2489 wstandend(view->window);
2490 free(wline);
2491 wline = NULL;
2492 if (width < view->ncols)
2493 waddch(view->window, '\n');
2495 if (asprintf(&line, "[%d/%d] %s%s",
2496 *first_displayed_line - 1 + selected_line, nlines,
2497 blame_complete ? "" : "annotating... ", path) == -1) {
2498 free(id_str);
2499 return got_error_from_errno();
2501 free(id_str);
2502 err = format_line(&wline, &width, line, view->ncols);
2503 free(line);
2504 line = NULL;
2505 if (err)
2506 return err;
2507 waddwstr(view->window, wline);
2508 free(wline);
2509 wline = NULL;
2510 if (width < view->ncols)
2511 waddch(view->window, '\n');
2513 *eof = 0;
2514 while (nprinted < max_lines - 2) {
2515 line = parse_next_line(f, &len);
2516 if (line == NULL) {
2517 *eof = 1;
2518 break;
2520 if (++lineno < *first_displayed_line) {
2521 free(line);
2522 continue;
2525 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2526 err = format_line(&wline, &width, line, wlimit);
2527 if (err) {
2528 free(line);
2529 return err;
2532 if (view->focussed && nprinted == selected_line - 1)
2533 wstandout(view->window);
2535 blame_line = &lines[lineno - 1];
2536 if (blame_line->annotated && prev_id &&
2537 got_object_id_cmp(prev_id, blame_line->id) == 0)
2538 waddstr(view->window, " ");
2539 else if (blame_line->annotated) {
2540 char *id_str;
2541 err = got_object_id_str(&id_str, blame_line->id);
2542 if (err) {
2543 free(line);
2544 free(wline);
2545 return err;
2547 wprintw(view->window, "%.8s ", id_str);
2548 free(id_str);
2549 prev_id = blame_line->id;
2550 } else {
2551 waddstr(view->window, "........ ");
2552 prev_id = NULL;
2555 waddwstr(view->window, wline);
2556 while (width < wlimit) {
2557 waddch(view->window, ' ');
2558 width++;
2560 if (view->focussed && nprinted == selected_line - 1)
2561 wstandend(view->window);
2562 if (++nprinted == 1)
2563 *first_displayed_line = lineno;
2564 free(line);
2565 free(wline);
2566 wline = NULL;
2568 *last_displayed_line = lineno;
2570 view_vborder(view);
2572 return NULL;
2575 static const struct got_error *
2576 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2578 const struct got_error *err = NULL;
2579 struct tog_blame_cb_args *a = arg;
2580 struct tog_blame_line *line;
2581 int errcode;
2583 if (nlines != a->nlines ||
2584 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2585 return got_error(GOT_ERR_RANGE);
2587 errcode = pthread_mutex_lock(&tog_mutex);
2588 if (errcode)
2589 return got_error_set_errno(errcode);
2591 if (*a->quit) { /* user has quit the blame view */
2592 err = got_error(GOT_ERR_ITER_COMPLETED);
2593 goto done;
2596 if (lineno == -1)
2597 goto done; /* no change in this commit */
2599 line = &a->lines[lineno - 1];
2600 if (line->annotated)
2601 goto done;
2603 line->id = got_object_id_dup(id);
2604 if (line->id == NULL) {
2605 err = got_error_from_errno();
2606 goto done;
2608 line->annotated = 1;
2609 done:
2610 errcode = pthread_mutex_unlock(&tog_mutex);
2611 if (errcode)
2612 err = got_error_set_errno(errcode);
2613 return err;
2616 static void *
2617 blame_thread(void *arg)
2619 const struct got_error *err;
2620 struct tog_blame_thread_args *ta = arg;
2621 struct tog_blame_cb_args *a = ta->cb_args;
2622 int errcode;
2624 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2625 blame_cb, ta->cb_args);
2627 errcode = pthread_mutex_lock(&tog_mutex);
2628 if (errcode)
2629 return (void *)got_error_set_errno(errcode);
2631 got_repo_close(ta->repo);
2632 ta->repo = NULL;
2633 *ta->complete = 1;
2635 errcode = pthread_mutex_unlock(&tog_mutex);
2636 if (errcode && err == NULL)
2637 err = got_error_set_errno(errcode);
2639 return (void *)err;
2642 static struct got_object_id *
2643 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2644 int selected_line)
2646 struct tog_blame_line *line;
2648 line = &lines[first_displayed_line - 1 + selected_line - 1];
2649 if (!line->annotated)
2650 return NULL;
2652 return line->id;
2655 static const struct got_error *
2656 stop_blame(struct tog_blame *blame)
2658 const struct got_error *err = NULL;
2659 int i;
2661 if (blame->thread) {
2662 int errcode;
2663 errcode = pthread_mutex_unlock(&tog_mutex);
2664 if (errcode)
2665 return got_error_set_errno(errcode);
2666 errcode = pthread_join(blame->thread, (void **)&err);
2667 if (errcode)
2668 return got_error_set_errno(errcode);
2669 errcode = pthread_mutex_lock(&tog_mutex);
2670 if (errcode)
2671 return got_error_set_errno(errcode);
2672 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2673 err = NULL;
2674 blame->thread = NULL;
2676 if (blame->thread_args.repo) {
2677 got_repo_close(blame->thread_args.repo);
2678 blame->thread_args.repo = NULL;
2680 if (blame->f) {
2681 if (fclose(blame->f) != 0 && err == NULL)
2682 err = got_error_from_errno();
2683 blame->f = NULL;
2685 if (blame->lines) {
2686 for (i = 0; i < blame->nlines; i++)
2687 free(blame->lines[i].id);
2688 free(blame->lines);
2689 blame->lines = NULL;
2691 free(blame->cb_args.commit_id);
2692 blame->cb_args.commit_id = NULL;
2694 return err;
2697 static const struct got_error *
2698 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2699 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2700 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2701 struct got_repository *repo)
2703 const struct got_error *err = NULL;
2704 struct got_blob_object *blob = NULL;
2705 struct got_repository *thread_repo = NULL;
2706 struct got_object_id *obj_id = NULL;
2707 int obj_type;
2709 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2710 if (err)
2711 return err;
2712 if (obj_id == NULL)
2713 return got_error(GOT_ERR_NO_OBJ);
2715 err = got_object_get_type(&obj_type, repo, obj_id);
2716 if (err)
2717 goto done;
2719 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2720 err = got_error(GOT_ERR_OBJ_TYPE);
2721 goto done;
2724 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2725 if (err)
2726 goto done;
2727 blame->f = got_opentemp();
2728 if (blame->f == NULL) {
2729 err = got_error_from_errno();
2730 goto done;
2732 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2733 blame->f, blob);
2734 if (err)
2735 goto done;
2737 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2738 if (blame->lines == NULL) {
2739 err = got_error_from_errno();
2740 goto done;
2743 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2744 if (err)
2745 goto done;
2747 blame->cb_args.view = view;
2748 blame->cb_args.lines = blame->lines;
2749 blame->cb_args.nlines = blame->nlines;
2750 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2751 if (blame->cb_args.commit_id == NULL) {
2752 err = got_error_from_errno();
2753 goto done;
2755 blame->cb_args.quit = done;
2757 blame->thread_args.path = path;
2758 blame->thread_args.repo = thread_repo;
2759 blame->thread_args.cb_args = &blame->cb_args;
2760 blame->thread_args.complete = blame_complete;
2761 *blame_complete = 0;
2763 done:
2764 if (blob)
2765 got_object_blob_close(blob);
2766 free(obj_id);
2767 if (err)
2768 stop_blame(blame);
2769 return err;
2772 static const struct got_error *
2773 open_blame_view(struct tog_view *view, char *path,
2774 struct got_object_id *commit_id, struct got_reflist_head *refs,
2775 struct got_repository *repo)
2777 const struct got_error *err = NULL;
2778 struct tog_blame_view_state *s = &view->state.blame;
2780 SIMPLEQ_INIT(&s->blamed_commits);
2782 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2783 if (err)
2784 return err;
2786 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2787 s->first_displayed_line = 1;
2788 s->last_displayed_line = view->nlines;
2789 s->selected_line = 1;
2790 s->blame_complete = 0;
2791 s->path = path;
2792 if (s->path == NULL)
2793 return got_error_from_errno();
2794 s->repo = repo;
2795 s->refs = refs;
2796 s->commit_id = commit_id;
2797 memset(&s->blame, 0, sizeof(s->blame));
2799 view->show = show_blame_view;
2800 view->input = input_blame_view;
2801 view->close = close_blame_view;
2803 return run_blame(&s->blame, view, &s->blame_complete,
2804 &s->first_displayed_line, &s->last_displayed_line,
2805 &s->selected_line, &s->done, &s->eof, s->path,
2806 s->blamed_commit->id, s->repo);
2809 static const struct got_error *
2810 close_blame_view(struct tog_view *view)
2812 const struct got_error *err = NULL;
2813 struct tog_blame_view_state *s = &view->state.blame;
2815 if (s->blame.thread)
2816 err = stop_blame(&s->blame);
2818 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2819 struct got_object_qid *blamed_commit;
2820 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2821 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2822 got_object_qid_free(blamed_commit);
2825 free(s->path);
2827 return err;
2830 static const struct got_error *
2831 show_blame_view(struct tog_view *view)
2833 const struct got_error *err = NULL;
2834 struct tog_blame_view_state *s = &view->state.blame;
2835 int errcode;
2837 if (s->blame.thread == NULL) {
2838 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2839 &s->blame.thread_args);
2840 if (errcode)
2841 return got_error_set_errno(errcode);
2844 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2845 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2846 s->selected_line, &s->first_displayed_line,
2847 &s->last_displayed_line, &s->eof, view->nlines);
2849 view_vborder(view);
2850 return err;
2853 static const struct got_error *
2854 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2855 struct tog_view **focus_view, struct tog_view *view, int ch)
2857 const struct got_error *err = NULL, *thread_err = NULL;
2858 struct tog_view *diff_view;
2859 struct tog_blame_view_state *s = &view->state.blame;
2860 int begin_x = 0;
2862 switch (ch) {
2863 case 'q':
2864 s->done = 1;
2865 break;
2866 case 'k':
2867 case KEY_UP:
2868 if (s->selected_line > 1)
2869 s->selected_line--;
2870 else if (s->selected_line == 1 &&
2871 s->first_displayed_line > 1)
2872 s->first_displayed_line--;
2873 break;
2874 case KEY_PPAGE:
2875 if (s->first_displayed_line == 1) {
2876 s->selected_line = 1;
2877 break;
2879 if (s->first_displayed_line > view->nlines - 2)
2880 s->first_displayed_line -=
2881 (view->nlines - 2);
2882 else
2883 s->first_displayed_line = 1;
2884 break;
2885 case 'j':
2886 case KEY_DOWN:
2887 if (s->selected_line < view->nlines - 2 &&
2888 s->first_displayed_line +
2889 s->selected_line <= s->blame.nlines)
2890 s->selected_line++;
2891 else if (s->last_displayed_line <
2892 s->blame.nlines)
2893 s->first_displayed_line++;
2894 break;
2895 case 'b':
2896 case 'p': {
2897 struct got_object_id *id = NULL;
2898 id = get_selected_commit_id(s->blame.lines,
2899 s->first_displayed_line, s->selected_line);
2900 if (id == NULL)
2901 break;
2902 if (ch == 'p') {
2903 struct got_commit_object *commit;
2904 struct got_object_qid *pid;
2905 struct got_object_id *blob_id = NULL;
2906 int obj_type;
2907 err = got_object_open_as_commit(&commit,
2908 s->repo, id);
2909 if (err)
2910 break;
2911 pid = SIMPLEQ_FIRST(
2912 got_object_commit_get_parent_ids(commit));
2913 if (pid == NULL) {
2914 got_object_commit_close(commit);
2915 break;
2917 /* Check if path history ends here. */
2918 err = got_object_id_by_path(&blob_id, s->repo,
2919 pid->id, s->path);
2920 if (err) {
2921 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2922 err = NULL;
2923 got_object_commit_close(commit);
2924 break;
2926 err = got_object_get_type(&obj_type, s->repo,
2927 blob_id);
2928 free(blob_id);
2929 /* Can't blame non-blob type objects. */
2930 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2931 got_object_commit_close(commit);
2932 break;
2934 err = got_object_qid_alloc(&s->blamed_commit,
2935 pid->id);
2936 got_object_commit_close(commit);
2937 } else {
2938 if (got_object_id_cmp(id,
2939 s->blamed_commit->id) == 0)
2940 break;
2941 err = got_object_qid_alloc(&s->blamed_commit,
2942 id);
2944 if (err)
2945 break;
2946 s->done = 1;
2947 thread_err = stop_blame(&s->blame);
2948 s->done = 0;
2949 if (thread_err)
2950 break;
2951 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2952 s->blamed_commit, entry);
2953 err = run_blame(&s->blame, view, &s->blame_complete,
2954 &s->first_displayed_line, &s->last_displayed_line,
2955 &s->selected_line, &s->done, &s->eof,
2956 s->path, s->blamed_commit->id, s->repo);
2957 if (err)
2958 break;
2959 break;
2961 case 'B': {
2962 struct got_object_qid *first;
2963 first = SIMPLEQ_FIRST(&s->blamed_commits);
2964 if (!got_object_id_cmp(first->id, s->commit_id))
2965 break;
2966 s->done = 1;
2967 thread_err = stop_blame(&s->blame);
2968 s->done = 0;
2969 if (thread_err)
2970 break;
2971 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2972 got_object_qid_free(s->blamed_commit);
2973 s->blamed_commit =
2974 SIMPLEQ_FIRST(&s->blamed_commits);
2975 err = run_blame(&s->blame, view, &s->blame_complete,
2976 &s->first_displayed_line, &s->last_displayed_line,
2977 &s->selected_line, &s->done, &s->eof, s->path,
2978 s->blamed_commit->id, s->repo);
2979 if (err)
2980 break;
2981 break;
2983 case KEY_ENTER:
2984 case '\r': {
2985 struct got_object_id *id = NULL;
2986 struct got_object_qid *pid;
2987 struct got_commit_object *commit = NULL;
2988 id = get_selected_commit_id(s->blame.lines,
2989 s->first_displayed_line, s->selected_line);
2990 if (id == NULL)
2991 break;
2992 err = got_object_open_as_commit(&commit, s->repo, id);
2993 if (err)
2994 break;
2995 pid = SIMPLEQ_FIRST(
2996 got_object_commit_get_parent_ids(commit));
2997 if (view_is_parent_view(view))
2998 begin_x = view_split_begin_x(view->begin_x);
2999 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3000 if (diff_view == NULL) {
3001 got_object_commit_close(commit);
3002 err = got_error_from_errno();
3003 break;
3005 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3006 id, NULL, s->refs, s->repo);
3007 got_object_commit_close(commit);
3008 if (err) {
3009 view_close(diff_view);
3010 break;
3012 if (view_is_parent_view(view)) {
3013 err = view_close_child(view);
3014 if (err)
3015 break;
3016 err = view_set_child(view, diff_view);
3017 if (err) {
3018 view_close(diff_view);
3019 break;
3021 *focus_view = diff_view;
3022 view->child_focussed = 1;
3023 } else
3024 *new_view = diff_view;
3025 if (err)
3026 break;
3027 break;
3029 case KEY_NPAGE:
3030 case ' ':
3031 if (s->last_displayed_line >= s->blame.nlines &&
3032 s->selected_line < view->nlines - 2) {
3033 s->selected_line = MIN(s->blame.nlines,
3034 view->nlines - 2);
3035 break;
3037 if (s->last_displayed_line + view->nlines - 2
3038 <= s->blame.nlines)
3039 s->first_displayed_line +=
3040 view->nlines - 2;
3041 else
3042 s->first_displayed_line =
3043 s->blame.nlines -
3044 (view->nlines - 3);
3045 break;
3046 case KEY_RESIZE:
3047 if (s->selected_line > view->nlines - 2) {
3048 s->selected_line = MIN(s->blame.nlines,
3049 view->nlines - 2);
3051 break;
3052 default:
3053 break;
3055 return thread_err ? thread_err : err;
3058 static const struct got_error *
3059 cmd_blame(int argc, char *argv[])
3061 const struct got_error *error;
3062 struct got_repository *repo = NULL;
3063 struct got_reflist_head refs;
3064 struct got_worktree *worktree = NULL;
3065 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3066 struct got_object_id *commit_id = NULL;
3067 char *commit_id_str = NULL;
3068 int ch;
3069 struct tog_view *view;
3071 #ifndef PROFILE
3072 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3073 NULL) == -1)
3074 err(1, "pledge");
3075 #endif
3077 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3078 switch (ch) {
3079 case 'c':
3080 commit_id_str = optarg;
3081 break;
3082 case 'r':
3083 repo_path = realpath(optarg, NULL);
3084 if (repo_path == NULL)
3085 err(1, "-r option");
3086 break;
3087 default:
3088 usage();
3089 /* NOTREACHED */
3093 argc -= optind;
3094 argv += optind;
3096 if (argc == 1)
3097 path = argv[0];
3098 else
3099 usage_blame();
3101 cwd = getcwd(NULL, 0);
3102 if (cwd == NULL) {
3103 error = got_error_from_errno();
3104 goto done;
3106 if (repo_path == NULL) {
3107 error = got_worktree_open(&worktree, cwd);
3108 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3109 goto done;
3110 else
3111 error = NULL;
3112 if (worktree) {
3113 repo_path =
3114 strdup(got_worktree_get_repo_path(worktree));
3115 if (repo_path == NULL)
3116 error = got_error_from_errno();
3117 if (error)
3118 goto done;
3119 } else {
3120 repo_path = strdup(cwd);
3121 if (repo_path == NULL) {
3122 error = got_error_from_errno();
3123 goto done;
3128 init_curses();
3130 error = apply_unveil(repo_path, NULL);
3131 if (error)
3132 goto done;
3134 error = got_repo_open(&repo, repo_path);
3135 if (error != NULL)
3136 goto done;
3138 if (worktree) {
3139 const char *prefix = got_worktree_get_path_prefix(worktree);
3140 char *p, *worktree_subdir = cwd +
3141 strlen(got_worktree_get_root_path(worktree));
3142 if (asprintf(&p, "%s%s%s%s%s",
3143 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3144 worktree_subdir, worktree_subdir[0] ? "/" : "",
3145 path) == -1) {
3146 error = got_error_from_errno();
3147 goto done;
3149 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3150 free(p);
3151 } else {
3152 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3154 if (error)
3155 goto done;
3157 if (commit_id_str == NULL) {
3158 struct got_reference *head_ref;
3159 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3160 if (error != NULL)
3161 goto done;
3162 error = got_ref_resolve(&commit_id, repo, head_ref);
3163 got_ref_close(head_ref);
3164 } else {
3165 error = got_object_resolve_id_str(&commit_id, repo,
3166 commit_id_str);
3168 if (error != NULL)
3169 goto done;
3171 SIMPLEQ_INIT(&refs);
3172 error = got_ref_list(&refs, repo);
3173 if (error)
3174 goto done;
3176 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3177 if (view == NULL) {
3178 error = got_error_from_errno();
3179 goto done;
3181 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3182 if (error)
3183 goto done;
3184 error = view_loop(view);
3185 done:
3186 free(repo_path);
3187 free(cwd);
3188 free(commit_id);
3189 if (worktree)
3190 got_worktree_close(worktree);
3191 if (repo)
3192 got_repo_close(repo);
3193 return error;
3196 static const struct got_error *
3197 draw_tree_entries(struct tog_view *view,
3198 struct got_tree_entry **first_displayed_entry,
3199 struct got_tree_entry **last_displayed_entry,
3200 struct got_tree_entry **selected_entry, int *ndisplayed,
3201 const char *label, int show_ids, const char *parent_path,
3202 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3204 const struct got_error *err = NULL;
3205 struct got_tree_entry *te;
3206 wchar_t *wline;
3207 int width, n;
3209 *ndisplayed = 0;
3211 werase(view->window);
3213 if (limit == 0)
3214 return NULL;
3216 err = format_line(&wline, &width, label, view->ncols);
3217 if (err)
3218 return err;
3219 if (view_needs_focus_indication(view))
3220 wstandout(view->window);
3221 waddwstr(view->window, wline);
3222 if (view_needs_focus_indication(view))
3223 wstandend(view->window);
3224 free(wline);
3225 wline = NULL;
3226 if (width < view->ncols)
3227 waddch(view->window, '\n');
3228 if (--limit <= 0)
3229 return NULL;
3230 err = format_line(&wline, &width, parent_path, view->ncols);
3231 if (err)
3232 return err;
3233 waddwstr(view->window, wline);
3234 free(wline);
3235 wline = NULL;
3236 if (width < view->ncols)
3237 waddch(view->window, '\n');
3238 if (--limit <= 0)
3239 return NULL;
3240 waddch(view->window, '\n');
3241 if (--limit <= 0)
3242 return NULL;
3244 te = SIMPLEQ_FIRST(&entries->head);
3245 if (*first_displayed_entry == NULL) {
3246 if (selected == 0) {
3247 if (view->focussed)
3248 wstandout(view->window);
3249 *selected_entry = NULL;
3251 waddstr(view->window, " ..\n"); /* parent directory */
3252 if (selected == 0 && view->focussed)
3253 wstandend(view->window);
3254 (*ndisplayed)++;
3255 if (--limit <= 0)
3256 return NULL;
3257 n = 1;
3258 } else {
3259 n = 0;
3260 while (te != *first_displayed_entry)
3261 te = SIMPLEQ_NEXT(te, entry);
3264 while (te) {
3265 char *line = NULL, *id_str = NULL;
3267 if (show_ids) {
3268 err = got_object_id_str(&id_str, te->id);
3269 if (err)
3270 return got_error_from_errno();
3272 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3273 te->name, S_ISDIR(te->mode) ? "/" :
3274 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3275 free(id_str);
3276 return got_error_from_errno();
3278 free(id_str);
3279 err = format_line(&wline, &width, line, view->ncols);
3280 if (err) {
3281 free(line);
3282 break;
3284 if (n == selected) {
3285 if (view->focussed)
3286 wstandout(view->window);
3287 *selected_entry = te;
3289 waddwstr(view->window, wline);
3290 if (width < view->ncols)
3291 waddch(view->window, '\n');
3292 if (n == selected && view->focussed)
3293 wstandend(view->window);
3294 free(line);
3295 free(wline);
3296 wline = NULL;
3297 n++;
3298 (*ndisplayed)++;
3299 *last_displayed_entry = te;
3300 if (--limit <= 0)
3301 break;
3302 te = SIMPLEQ_NEXT(te, entry);
3305 return err;
3308 static void
3309 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3310 const struct got_tree_entries *entries, int isroot)
3312 struct got_tree_entry *te, *prev;
3313 int i;
3315 if (*first_displayed_entry == NULL)
3316 return;
3318 te = SIMPLEQ_FIRST(&entries->head);
3319 if (*first_displayed_entry == te) {
3320 if (!isroot)
3321 *first_displayed_entry = NULL;
3322 return;
3325 /* XXX this is stupid... switch to TAILQ? */
3326 for (i = 0; i < maxscroll; i++) {
3327 while (te != *first_displayed_entry) {
3328 prev = te;
3329 te = SIMPLEQ_NEXT(te, entry);
3331 *first_displayed_entry = prev;
3332 te = SIMPLEQ_FIRST(&entries->head);
3334 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3335 *first_displayed_entry = NULL;
3338 static int
3339 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3340 struct got_tree_entry *last_displayed_entry,
3341 const struct got_tree_entries *entries)
3343 struct got_tree_entry *next, *last;
3344 int n = 0;
3346 if (*first_displayed_entry)
3347 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3348 else
3349 next = SIMPLEQ_FIRST(&entries->head);
3350 last = last_displayed_entry;
3351 while (next && last && n++ < maxscroll) {
3352 last = SIMPLEQ_NEXT(last, entry);
3353 if (last) {
3354 *first_displayed_entry = next;
3355 next = SIMPLEQ_NEXT(next, entry);
3358 return n;
3361 static const struct got_error *
3362 tree_entry_path(char **path, struct tog_parent_trees *parents,
3363 struct got_tree_entry *te)
3365 const struct got_error *err = NULL;
3366 struct tog_parent_tree *pt;
3367 size_t len = 2; /* for leading slash and NUL */
3369 TAILQ_FOREACH(pt, parents, entry)
3370 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3371 if (te)
3372 len += strlen(te->name);
3374 *path = calloc(1, len);
3375 if (path == NULL)
3376 return got_error_from_errno();
3378 (*path)[0] = '/';
3379 pt = TAILQ_LAST(parents, tog_parent_trees);
3380 while (pt) {
3381 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3382 err = got_error(GOT_ERR_NO_SPACE);
3383 goto done;
3385 if (strlcat(*path, "/", len) >= len) {
3386 err = got_error(GOT_ERR_NO_SPACE);
3387 goto done;
3389 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3391 if (te) {
3392 if (strlcat(*path, te->name, len) >= len) {
3393 err = got_error(GOT_ERR_NO_SPACE);
3394 goto done;
3397 done:
3398 if (err) {
3399 free(*path);
3400 *path = NULL;
3402 return err;
3405 static const struct got_error *
3406 blame_tree_entry(struct tog_view **new_view, int begin_x,
3407 struct got_tree_entry *te, struct tog_parent_trees *parents,
3408 struct got_object_id *commit_id, struct got_reflist_head *refs,
3409 struct got_repository *repo)
3411 const struct got_error *err = NULL;
3412 char *path;
3413 struct tog_view *blame_view;
3415 err = tree_entry_path(&path, parents, te);
3416 if (err)
3417 return err;
3419 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3420 if (blame_view == NULL)
3421 return got_error_from_errno();
3423 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3424 if (err) {
3425 view_close(blame_view);
3426 free(path);
3427 } else
3428 *new_view = blame_view;
3429 return err;
3432 static const struct got_error *
3433 log_tree_entry(struct tog_view **new_view, int begin_x,
3434 struct got_tree_entry *te, struct tog_parent_trees *parents,
3435 struct got_object_id *commit_id, struct got_reflist_head *refs,
3436 struct got_repository *repo)
3438 struct tog_view *log_view;
3439 const struct got_error *err = NULL;
3440 char *path;
3442 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3443 if (log_view == NULL)
3444 return got_error_from_errno();
3446 err = tree_entry_path(&path, parents, te);
3447 if (err)
3448 return err;
3450 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3451 if (err)
3452 view_close(log_view);
3453 else
3454 *new_view = log_view;
3455 free(path);
3456 return err;
3459 static const struct got_error *
3460 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3461 struct got_object_id *commit_id, struct got_reflist_head *refs,
3462 struct got_repository *repo)
3464 const struct got_error *err = NULL;
3465 char *commit_id_str = NULL;
3466 struct tog_tree_view_state *s = &view->state.tree;
3468 TAILQ_INIT(&s->parents);
3470 err = got_object_id_str(&commit_id_str, commit_id);
3471 if (err != NULL)
3472 goto done;
3474 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3475 err = got_error_from_errno();
3476 goto done;
3479 s->root = s->tree = root;
3480 s->entries = got_object_tree_get_entries(root);
3481 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3482 s->commit_id = got_object_id_dup(commit_id);
3483 if (s->commit_id == NULL) {
3484 err = got_error_from_errno();
3485 goto done;
3487 s->refs = refs;
3488 s->repo = repo;
3490 view->show = show_tree_view;
3491 view->input = input_tree_view;
3492 view->close = close_tree_view;
3493 done:
3494 free(commit_id_str);
3495 if (err) {
3496 free(s->tree_label);
3497 s->tree_label = NULL;
3499 return err;
3502 static const struct got_error *
3503 close_tree_view(struct tog_view *view)
3505 struct tog_tree_view_state *s = &view->state.tree;
3507 free(s->tree_label);
3508 s->tree_label = NULL;
3509 free(s->commit_id);
3510 s->commit_id = NULL;
3511 while (!TAILQ_EMPTY(&s->parents)) {
3512 struct tog_parent_tree *parent;
3513 parent = TAILQ_FIRST(&s->parents);
3514 TAILQ_REMOVE(&s->parents, parent, entry);
3515 free(parent);
3518 if (s->tree != s->root)
3519 got_object_tree_close(s->tree);
3520 got_object_tree_close(s->root);
3522 return NULL;
3525 static const struct got_error *
3526 show_tree_view(struct tog_view *view)
3528 const struct got_error *err = NULL;
3529 struct tog_tree_view_state *s = &view->state.tree;
3530 char *parent_path;
3532 err = tree_entry_path(&parent_path, &s->parents, NULL);
3533 if (err)
3534 return err;
3536 err = draw_tree_entries(view, &s->first_displayed_entry,
3537 &s->last_displayed_entry, &s->selected_entry,
3538 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3539 s->entries, s->selected, view->nlines, s->tree == s->root);
3540 free(parent_path);
3542 view_vborder(view);
3543 return err;
3546 static const struct got_error *
3547 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3548 struct tog_view **focus_view, struct tog_view *view, int ch)
3550 const struct got_error *err = NULL;
3551 struct tog_tree_view_state *s = &view->state.tree;
3552 struct tog_view *log_view;
3553 int begin_x = 0, nscrolled;
3555 switch (ch) {
3556 case 'i':
3557 s->show_ids = !s->show_ids;
3558 break;
3559 case 'l':
3560 if (!s->selected_entry)
3561 break;
3562 if (view_is_parent_view(view))
3563 begin_x = view_split_begin_x(view->begin_x);
3564 err = log_tree_entry(&log_view, begin_x,
3565 s->selected_entry, &s->parents,
3566 s->commit_id, s->refs, s->repo);
3567 if (view_is_parent_view(view)) {
3568 err = view_close_child(view);
3569 if (err)
3570 return err;
3571 err = view_set_child(view, log_view);
3572 if (err) {
3573 view_close(log_view);
3574 break;
3576 *focus_view = log_view;
3577 view->child_focussed = 1;
3578 } else
3579 *new_view = log_view;
3580 break;
3581 case 'k':
3582 case KEY_UP:
3583 if (s->selected > 0) {
3584 s->selected--;
3585 if (s->selected == 0)
3586 break;
3588 if (s->selected > 0)
3589 break;
3590 tree_scroll_up(&s->first_displayed_entry, 1,
3591 s->entries, s->tree == s->root);
3592 break;
3593 case KEY_PPAGE:
3594 tree_scroll_up(&s->first_displayed_entry,
3595 MAX(0, view->nlines - 4 - s->selected), s->entries,
3596 s->tree == s->root);
3597 s->selected = 0;
3598 if (SIMPLEQ_FIRST(&s->entries->head) ==
3599 s->first_displayed_entry && s->tree != s->root)
3600 s->first_displayed_entry = NULL;
3601 break;
3602 case 'j':
3603 case KEY_DOWN:
3604 if (s->selected < s->ndisplayed - 1) {
3605 s->selected++;
3606 break;
3608 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3609 == NULL) {
3610 /* can't scroll any further */
3611 break;
3613 tree_scroll_down(&s->first_displayed_entry, 1,
3614 s->last_displayed_entry, s->entries);
3615 break;
3616 case KEY_NPAGE:
3617 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3618 == NULL) {
3619 /* can't scroll any further; move cursor down */
3620 if (s->selected < s->ndisplayed - 1)
3621 s->selected = s->ndisplayed - 1;
3622 break;
3624 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3625 view->nlines, s->last_displayed_entry, s->entries);
3626 if (nscrolled < view->nlines) {
3627 int ndisplayed = 0;
3628 struct got_tree_entry *te;
3629 te = s->first_displayed_entry;
3630 do {
3631 ndisplayed++;
3632 te = SIMPLEQ_NEXT(te, entry);
3633 } while (te);
3634 s->selected = ndisplayed - 1;
3636 break;
3637 case KEY_ENTER:
3638 case '\r':
3639 if (s->selected_entry == NULL) {
3640 struct tog_parent_tree *parent;
3641 case KEY_BACKSPACE:
3642 /* user selected '..' */
3643 if (s->tree == s->root)
3644 break;
3645 parent = TAILQ_FIRST(&s->parents);
3646 TAILQ_REMOVE(&s->parents, parent,
3647 entry);
3648 got_object_tree_close(s->tree);
3649 s->tree = parent->tree;
3650 s->entries =
3651 got_object_tree_get_entries(s->tree);
3652 s->first_displayed_entry =
3653 parent->first_displayed_entry;
3654 s->selected_entry =
3655 parent->selected_entry;
3656 s->selected = parent->selected;
3657 free(parent);
3658 } else if (S_ISDIR(s->selected_entry->mode)) {
3659 struct tog_parent_tree *parent;
3660 struct got_tree_object *child;
3661 err = got_object_open_as_tree(&child,
3662 s->repo, s->selected_entry->id);
3663 if (err)
3664 break;
3665 parent = calloc(1, sizeof(*parent));
3666 if (parent == NULL) {
3667 err = got_error_from_errno();
3668 break;
3670 parent->tree = s->tree;
3671 parent->first_displayed_entry =
3672 s->first_displayed_entry;
3673 parent->selected_entry = s->selected_entry;
3674 parent->selected = s->selected;
3675 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3676 s->tree = child;
3677 s->entries =
3678 got_object_tree_get_entries(s->tree);
3679 s->selected = 0;
3680 s->first_displayed_entry = NULL;
3681 } else if (S_ISREG(s->selected_entry->mode)) {
3682 struct tog_view *blame_view;
3683 int begin_x = view_is_parent_view(view) ?
3684 view_split_begin_x(view->begin_x) : 0;
3686 err = blame_tree_entry(&blame_view, begin_x,
3687 s->selected_entry, &s->parents,
3688 s->commit_id, s->refs, s->repo);
3689 if (err)
3690 break;
3691 if (view_is_parent_view(view)) {
3692 err = view_close_child(view);
3693 if (err)
3694 return err;
3695 err = view_set_child(view, blame_view);
3696 if (err) {
3697 view_close(blame_view);
3698 break;
3700 *focus_view = blame_view;
3701 view->child_focussed = 1;
3702 } else
3703 *new_view = blame_view;
3705 break;
3706 case KEY_RESIZE:
3707 if (s->selected > view->nlines)
3708 s->selected = s->ndisplayed - 1;
3709 break;
3710 default:
3711 break;
3714 return err;
3717 __dead static void
3718 usage_tree(void)
3720 endwin();
3721 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3722 getprogname());
3723 exit(1);
3726 static const struct got_error *
3727 cmd_tree(int argc, char *argv[])
3729 const struct got_error *error;
3730 struct got_repository *repo = NULL;
3731 struct got_reflist_head refs;
3732 char *repo_path = NULL;
3733 struct got_object_id *commit_id = NULL;
3734 char *commit_id_arg = NULL;
3735 struct got_commit_object *commit = NULL;
3736 struct got_tree_object *tree = NULL;
3737 int ch;
3738 struct tog_view *view;
3740 #ifndef PROFILE
3741 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3742 NULL) == -1)
3743 err(1, "pledge");
3744 #endif
3746 while ((ch = getopt(argc, argv, "c:")) != -1) {
3747 switch (ch) {
3748 case 'c':
3749 commit_id_arg = optarg;
3750 break;
3751 default:
3752 usage();
3753 /* NOTREACHED */
3757 argc -= optind;
3758 argv += optind;
3760 if (argc == 0) {
3761 struct got_worktree *worktree;
3762 char *cwd = getcwd(NULL, 0);
3763 if (cwd == NULL)
3764 return got_error_from_errno();
3765 error = got_worktree_open(&worktree, cwd);
3766 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3767 goto done;
3768 if (worktree) {
3769 free(cwd);
3770 repo_path =
3771 strdup(got_worktree_get_repo_path(worktree));
3772 got_worktree_close(worktree);
3773 } else
3774 repo_path = cwd;
3775 if (repo_path == NULL) {
3776 error = got_error_from_errno();
3777 goto done;
3779 } else if (argc == 1) {
3780 repo_path = realpath(argv[0], NULL);
3781 if (repo_path == NULL)
3782 return got_error_from_errno();
3783 } else
3784 usage_log();
3786 init_curses();
3788 error = apply_unveil(repo_path, NULL);
3789 if (error)
3790 goto done;
3792 error = got_repo_open(&repo, repo_path);
3793 if (error != NULL)
3794 goto done;
3796 if (commit_id_arg == NULL)
3797 error = get_head_commit_id(&commit_id, repo);
3798 else
3799 error = got_object_resolve_id_str(&commit_id, repo,
3800 commit_id_arg);
3801 if (error != NULL)
3802 goto done;
3804 error = got_object_open_as_commit(&commit, repo, commit_id);
3805 if (error != NULL)
3806 goto done;
3808 error = got_object_open_as_tree(&tree, repo,
3809 got_object_commit_get_tree_id(commit));
3810 if (error != NULL)
3811 goto done;
3813 SIMPLEQ_INIT(&refs);
3814 error = got_ref_list(&refs, repo);
3815 if (error)
3816 goto done;
3818 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3819 if (view == NULL) {
3820 error = got_error_from_errno();
3821 goto done;
3823 error = open_tree_view(view, tree, commit_id, &refs, repo);
3824 if (error)
3825 goto done;
3826 error = view_loop(view);
3827 done:
3828 free(repo_path);
3829 free(commit_id);
3830 if (commit)
3831 got_object_commit_close(commit);
3832 if (tree)
3833 got_object_tree_close(tree);
3834 if (repo)
3835 got_repo_close(repo);
3836 return error;
3839 __dead static void
3840 usage(void)
3842 int i;
3844 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3845 "Available commands:\n", getprogname());
3846 for (i = 0; i < nitems(tog_commands); i++) {
3847 struct tog_cmd *cmd = &tog_commands[i];
3848 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3850 exit(1);
3853 static char **
3854 make_argv(const char *arg0, const char *arg1)
3856 char **argv;
3857 int argc = (arg1 == NULL ? 1 : 2);
3859 argv = calloc(argc, sizeof(char *));
3860 if (argv == NULL)
3861 err(1, "calloc");
3862 argv[0] = strdup(arg0);
3863 if (argv[0] == NULL)
3864 err(1, "calloc");
3865 if (arg1) {
3866 argv[1] = strdup(arg1);
3867 if (argv[1] == NULL)
3868 err(1, "calloc");
3871 return argv;
3874 int
3875 main(int argc, char *argv[])
3877 const struct got_error *error = NULL;
3878 struct tog_cmd *cmd = NULL;
3879 int ch, hflag = 0;
3880 char **cmd_argv = NULL;
3882 setlocale(LC_CTYPE, "");
3884 while ((ch = getopt(argc, argv, "h")) != -1) {
3885 switch (ch) {
3886 case 'h':
3887 hflag = 1;
3888 break;
3889 default:
3890 usage();
3891 /* NOTREACHED */
3895 argc -= optind;
3896 argv += optind;
3897 optind = 0;
3898 optreset = 1;
3900 if (argc == 0) {
3901 if (hflag)
3902 usage();
3903 /* Build an argument vector which runs a default command. */
3904 cmd = &tog_commands[0];
3905 cmd_argv = make_argv(cmd->name, NULL);
3906 argc = 1;
3907 } else {
3908 int i;
3910 /* Did the user specific a command? */
3911 for (i = 0; i < nitems(tog_commands); i++) {
3912 if (strncmp(tog_commands[i].name, argv[0],
3913 strlen(argv[0])) == 0) {
3914 cmd = &tog_commands[i];
3915 if (hflag)
3916 tog_commands[i].cmd_usage();
3917 break;
3920 if (cmd == NULL) {
3921 /* Did the user specify a repository? */
3922 char *repo_path = realpath(argv[0], NULL);
3923 if (repo_path) {
3924 struct got_repository *repo;
3925 error = got_repo_open(&repo, repo_path);
3926 if (error == NULL)
3927 got_repo_close(repo);
3928 } else
3929 error = got_error_from_errno();
3930 if (error) {
3931 if (hflag) {
3932 fprintf(stderr, "%s: '%s' is not a "
3933 "known command\n", getprogname(),
3934 argv[0]);
3935 usage();
3937 fprintf(stderr, "%s: '%s' is neither a known "
3938 "command nor a path to a repository\n",
3939 getprogname(), argv[0]);
3940 free(repo_path);
3941 return 1;
3943 cmd = &tog_commands[0];
3944 cmd_argv = make_argv(cmd->name, repo_path);
3945 argc = 2;
3946 free(repo_path);
3950 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3951 if (error)
3952 goto done;
3953 done:
3954 endwin();
3955 free(cmd_argv);
3956 if (error)
3957 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3958 return 0;