Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_worktree.h"
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 #ifndef MAX
57 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
58 #endif
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 struct tog_cmd {
66 const char *name;
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
69 const char *descr;
70 };
72 __dead static void usage(void);
73 __dead static void usage_log(void);
74 __dead static void usage_diff(void);
75 __dead static void usage_blame(void);
76 __dead static void usage_tree(void);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
83 static struct tog_cmd tog_commands[] = {
84 { "log", cmd_log, usage_log,
85 "show repository history" },
86 { "diff", cmd_diff, usage_diff,
87 "compare files and directories" },
88 { "blame", cmd_blame, usage_blame,
89 "show line-by-line file history" },
90 { "tree", cmd_tree, usage_tree,
91 "browse trees in repository" },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 int idx;
106 };
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
109 int ncommits;
110 struct commit_queue_head head;
111 };
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
115 FILE *f;
116 int first_displayed_line;
117 int last_displayed_line;
118 int eof;
119 int diff_context;
120 struct got_repository *repo;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
125 };
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
131 int commits_needed;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
137 int log_complete;
138 sig_atomic_t *quit;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
142 };
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
149 int selected;
150 char *in_repo_path;
151 struct got_repository *repo;
152 struct got_reflist_head *refs;
153 struct got_object_id *start_id;
154 sig_atomic_t quit;
155 pthread_t thread;
156 struct tog_log_thread_args thread_args;
157 };
159 struct tog_blame_cb_args {
160 struct tog_blame_line *lines; /* one per line */
161 int nlines;
163 struct tog_view *view;
164 struct got_object_id *commit_id;
165 int *quit;
166 };
168 struct tog_blame_thread_args {
169 const char *path;
170 struct got_repository *repo;
171 struct tog_blame_cb_args *cb_args;
172 int *complete;
173 };
175 struct tog_blame {
176 FILE *f;
177 size_t filesize;
178 struct tog_blame_line *lines;
179 int nlines;
180 pthread_t thread;
181 struct tog_blame_thread_args thread_args;
182 struct tog_blame_cb_args cb_args;
183 const char *path;
184 };
186 struct tog_blame_view_state {
187 int first_displayed_line;
188 int last_displayed_line;
189 int selected_line;
190 int blame_complete;
191 int eof;
192 int done;
193 struct got_object_id_queue blamed_commits;
194 struct got_object_qid *blamed_commit;
195 char *path;
196 struct got_repository *repo;
197 struct got_reflist_head *refs;
198 struct got_object_id *commit_id;
199 struct tog_blame blame;
200 };
202 struct tog_parent_tree {
203 TAILQ_ENTRY(tog_parent_tree) entry;
204 struct got_tree_object *tree;
205 struct got_tree_entry *first_displayed_entry;
206 struct got_tree_entry *selected_entry;
207 int selected;
208 };
210 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
212 struct tog_tree_view_state {
213 char *tree_label;
214 struct got_tree_object *root;
215 struct got_tree_object *tree;
216 const struct got_tree_entries *entries;
217 struct got_tree_entry *first_displayed_entry;
218 struct got_tree_entry *last_displayed_entry;
219 struct got_tree_entry *selected_entry;
220 int ndisplayed, selected, show_ids;
221 struct tog_parent_trees parents;
222 struct got_object_id *commit_id;
223 struct got_repository *repo;
224 struct got_reflist_head *refs;
225 };
227 /*
228 * We implement two types of views: parent views and child views.
230 * The 'Tab' key switches between a parent view and its child view.
231 * Child views are shown side-by-side to their parent view, provided
232 * there is enough screen estate.
234 * When a new view is opened from within a parent view, this new view
235 * becomes a child view of the parent view, replacing any existing child.
237 * When a new view is opened from within a child view, this new view
238 * becomes a parent view which will obscure the views below until the
239 * user quits the new parent view by typing 'q'.
241 * This list of views contains parent views only.
242 * Child views are only pointed to by their parent view.
243 */
244 TAILQ_HEAD(tog_view_list_head, tog_view);
246 struct tog_view {
247 TAILQ_ENTRY(tog_view) entry;
248 WINDOW *window;
249 PANEL *panel;
250 int nlines, ncols, begin_y, begin_x;
251 int lines, cols; /* copies of LINES and COLS */
252 int focussed;
253 struct tog_view *parent;
254 struct tog_view *child;
255 int child_focussed;
257 /* type-specific state */
258 enum tog_view_type type;
259 union {
260 struct tog_diff_view_state diff;
261 struct tog_log_view_state log;
262 struct tog_blame_view_state blame;
263 struct tog_tree_view_state tree;
264 } state;
266 const struct got_error *(*show)(struct tog_view *);
267 const struct got_error *(*input)(struct tog_view **,
268 struct tog_view **, struct tog_view**, struct tog_view *, int);
269 const struct got_error *(*close)(struct tog_view *);
270 };
272 static const struct got_error *open_diff_view(struct tog_view *,
273 struct got_object_id *, struct got_object_id *, struct tog_view *,
274 struct got_reflist_head *, struct got_repository *);
275 static const struct got_error *show_diff_view(struct tog_view *);
276 static const struct got_error *input_diff_view(struct tog_view **,
277 struct tog_view **, struct tog_view **, struct tog_view *, int);
278 static const struct got_error* close_diff_view(struct tog_view *);
280 static const struct got_error *open_log_view(struct tog_view *,
281 struct got_object_id *, struct got_reflist_head *,
282 struct got_repository *, const char *, int);
283 static const struct got_error * show_log_view(struct tog_view *);
284 static const struct got_error *input_log_view(struct tog_view **,
285 struct tog_view **, struct tog_view **, struct tog_view *, int);
286 static const struct got_error *close_log_view(struct tog_view *);
288 static const struct got_error *open_blame_view(struct tog_view *, char *,
289 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_blame_view(struct tog_view *);
291 static const struct got_error *input_blame_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error *close_blame_view(struct tog_view *);
295 static const struct got_error *open_tree_view(struct tog_view *,
296 struct got_tree_object *, struct got_object_id *,
297 struct got_reflist_head *, struct got_repository *);
298 static const struct got_error *show_tree_view(struct tog_view *);
299 static const struct got_error *input_tree_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_tree_view(struct tog_view *);
303 static volatile sig_atomic_t tog_sigwinch_received;
305 static void
306 tog_sigwinch(int signo)
308 tog_sigwinch_received = 1;
311 static const struct got_error *
312 view_close(struct tog_view *view)
314 const struct got_error *err = NULL;
316 if (view->child) {
317 view_close(view->child);
318 view->child = NULL;
320 if (view->close)
321 err = view->close(view);
322 if (view->panel)
323 del_panel(view->panel);
324 if (view->window)
325 delwin(view->window);
326 free(view);
327 return err;
330 static struct tog_view *
331 view_open(int nlines, int ncols, int begin_y, int begin_x,
332 enum tog_view_type type)
334 struct tog_view *view = calloc(1, sizeof(*view));
336 if (view == NULL)
337 return NULL;
339 view->type = type;
340 view->lines = LINES;
341 view->cols = COLS;
342 view->nlines = nlines ? nlines : LINES - begin_y;
343 view->ncols = ncols ? ncols : COLS - begin_x;
344 view->begin_y = begin_y;
345 view->begin_x = begin_x;
346 view->window = newwin(nlines, ncols, begin_y, begin_x);
347 if (view->window == NULL) {
348 view_close(view);
349 return NULL;
351 view->panel = new_panel(view->window);
352 if (view->panel == NULL ||
353 set_panel_userptr(view->panel, view) != OK) {
354 view_close(view);
355 return NULL;
358 keypad(view->window, TRUE);
359 return view;
362 static int
363 view_split_begin_x(int begin_x)
365 if (begin_x > 0 || COLS < 120)
366 return 0;
367 return (COLS - MAX(COLS / 2, 80));
370 static const struct got_error *view_resize(struct tog_view *);
372 static const struct got_error *
373 view_splitscreen(struct tog_view *view)
375 const struct got_error *err = NULL;
377 view->begin_y = 0;
378 view->begin_x = view_split_begin_x(0);
379 view->nlines = LINES;
380 view->ncols = COLS - view->begin_x;
381 view->lines = LINES;
382 view->cols = COLS;
383 err = view_resize(view);
384 if (err)
385 return err;
387 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 return got_error_from_errno();
390 return NULL;
393 static const struct got_error *
394 view_fullscreen(struct tog_view *view)
396 const struct got_error *err = NULL;
398 view->begin_x = 0;
399 view->begin_y = 0;
400 view->nlines = LINES;
401 view->ncols = COLS;
402 view->lines = LINES;
403 view->cols = COLS;
404 err = view_resize(view);
405 if (err)
406 return err;
408 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 return got_error_from_errno();
411 return NULL;
414 static int
415 view_is_parent_view(struct tog_view *view)
417 return view->parent == NULL;
420 static const struct got_error *
421 view_resize(struct tog_view *view)
423 int nlines, ncols;
425 if (view->lines > LINES)
426 nlines = view->nlines - (view->lines - LINES);
427 else
428 nlines = view->nlines + (LINES - view->lines);
430 if (view->cols > COLS)
431 ncols = view->ncols - (view->cols - COLS);
432 else
433 ncols = view->ncols + (COLS - view->cols);
435 if (wresize(view->window, nlines, ncols) == ERR)
436 return got_error_from_errno();
437 if (replace_panel(view->panel, view->window) == ERR)
438 return got_error_from_errno();
439 wclear(view->window);
441 view->nlines = nlines;
442 view->ncols = ncols;
443 view->lines = LINES;
444 view->cols = COLS;
446 if (view->child) {
447 view->child->begin_x = view_split_begin_x(view->begin_x);
448 if (view->child->begin_x == 0) {
449 view_fullscreen(view->child);
450 if (view->child->focussed)
451 show_panel(view->child->panel);
452 else
453 show_panel(view->panel);
454 } else {
455 view_splitscreen(view->child);
456 show_panel(view->child->panel);
460 return NULL;
463 static const struct got_error *
464 view_close_child(struct tog_view *view)
466 const struct got_error *err = NULL;
468 if (view->child == NULL)
469 return NULL;
471 err = view_close(view->child);
472 view->child = NULL;
473 return err;
476 static const struct got_error *
477 view_set_child(struct tog_view *view, struct tog_view *child)
479 const struct got_error *err = NULL;
481 view->child = child;
482 child->parent = view;
483 return err;
486 static int
487 view_is_splitscreen(struct tog_view *view)
489 return !view_is_parent_view(view) && view->begin_x > 0;
492 static void
493 tog_resizeterm(void)
495 int cols, lines;
496 struct winsize size;
498 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
499 cols = 80; /* Default */
500 lines = 24;
501 } else {
502 cols = size.ws_col;
503 lines = size.ws_row;
505 resize_term(lines, cols);
508 static const struct got_error *
509 view_input(struct tog_view **new, struct tog_view **dead,
510 struct tog_view **focus, int *done, struct tog_view *view,
511 struct tog_view_list_head *views)
513 const struct got_error *err = NULL;
514 struct tog_view *v;
515 int ch, errcode;
517 *new = NULL;
518 *dead = NULL;
519 *focus = NULL;
521 nodelay(stdscr, FALSE);
522 /* Allow threads to make progress while we are waiting for input. */
523 errcode = pthread_mutex_unlock(&tog_mutex);
524 if (errcode)
525 return got_error_set_errno(errcode);
526 ch = wgetch(view->window);
527 errcode = pthread_mutex_lock(&tog_mutex);
528 if (errcode)
529 return got_error_set_errno(errcode);
530 nodelay(stdscr, TRUE);
532 if (tog_sigwinch_received) {
533 tog_resizeterm();
534 tog_sigwinch_received = 0;
535 TAILQ_FOREACH(v, views, entry) {
536 err = view_resize(v);
537 if (err)
538 return err;
539 err = v->input(new, dead, focus, v, KEY_RESIZE);
540 if (err)
541 return err;
545 switch (ch) {
546 case ERR:
547 break;
548 case '\t':
549 if (view->child) {
550 *focus = view->child;
551 view->child_focussed = 1;
552 } else if (view->parent) {
553 *focus = view->parent;
554 view->parent->child_focussed = 0;
556 break;
557 case 'q':
558 err = view->input(new, dead, focus, view, ch);
559 *dead = view;
560 break;
561 case 'Q':
562 *done = 1;
563 break;
564 case 'f':
565 if (view_is_parent_view(view)) {
566 if (view->child == NULL)
567 break;
568 if (view_is_splitscreen(view->child)) {
569 *focus = view->child;
570 view->child_focussed = 1;
571 err = view_fullscreen(view->child);
572 } else
573 err = view_splitscreen(view->child);
574 if (err)
575 break;
576 err = view->child->input(new, dead, focus,
577 view->child, KEY_RESIZE);
578 } else {
579 if (view_is_splitscreen(view)) {
580 *focus = view;
581 view->parent->child_focussed = 1;
582 err = view_fullscreen(view);
583 } else {
584 err = view_splitscreen(view);
586 if (err)
587 break;
588 err = view->input(new, dead, focus, view,
589 KEY_RESIZE);
591 break;
592 case KEY_RESIZE:
593 break;
594 default:
595 err = view->input(new, dead, focus, view, ch);
596 break;
599 return err;
602 void
603 view_vborder(struct tog_view *view)
605 PANEL *panel;
606 struct tog_view *view_above;
608 if (view->parent)
609 return view_vborder(view->parent);
611 panel = panel_above(view->panel);
612 if (panel == NULL)
613 return;
615 view_above = panel_userptr(panel);
616 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
617 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
620 int
621 view_needs_focus_indication(struct tog_view *view)
623 if (view_is_parent_view(view)) {
624 if (view->child == NULL || view->child_focussed)
625 return 0;
626 if (!view_is_splitscreen(view->child))
627 return 0;
628 } else if (!view_is_splitscreen(view))
629 return 0;
631 return view->focussed;
634 static const struct got_error *
635 view_loop(struct tog_view *view)
637 const struct got_error *err = NULL;
638 struct tog_view_list_head views;
639 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
640 int fast_refresh = 10;
641 int done = 0, errcode;
643 errcode = pthread_mutex_lock(&tog_mutex);
644 if (errcode)
645 return got_error_set_errno(errcode);
647 TAILQ_INIT(&views);
648 TAILQ_INSERT_HEAD(&views, view, entry);
650 main_view = view;
651 view->focussed = 1;
652 err = view->show(view);
653 if (err)
654 return err;
655 update_panels();
656 doupdate();
657 while (!TAILQ_EMPTY(&views) && !done) {
658 /* Refresh fast during initialization, then become slower. */
659 if (fast_refresh && fast_refresh-- == 0)
660 halfdelay(10); /* switch to once per second */
662 err = view_input(&new_view, &dead_view, &focus_view, &done,
663 view, &views);
664 if (err)
665 break;
666 if (dead_view) {
667 struct tog_view *prev = NULL;
669 if (view_is_parent_view(dead_view))
670 prev = TAILQ_PREV(dead_view,
671 tog_view_list_head, entry);
672 else if (view->parent != dead_view)
673 prev = view->parent;
675 if (dead_view->parent)
676 dead_view->parent->child = NULL;
677 else
678 TAILQ_REMOVE(&views, dead_view, entry);
680 err = view_close(dead_view);
681 if (err || dead_view == main_view)
682 goto done;
684 if (view == dead_view) {
685 if (focus_view)
686 view = focus_view;
687 else if (prev)
688 view = prev;
689 else if (!TAILQ_EMPTY(&views))
690 view = TAILQ_LAST(&views,
691 tog_view_list_head);
692 else
693 view = NULL;
694 if (view) {
695 if (view->child && view->child_focussed)
696 focus_view = view->child;
697 else
698 focus_view = view;
702 if (new_view) {
703 struct tog_view *v, *t;
704 /* Only allow one parent view per type. */
705 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
706 if (v->type != new_view->type)
707 continue;
708 TAILQ_REMOVE(&views, v, entry);
709 err = view_close(v);
710 if (err)
711 goto done;
712 break;
714 TAILQ_INSERT_TAIL(&views, new_view, entry);
715 view = new_view;
716 if (focus_view == NULL)
717 focus_view = new_view;
719 if (focus_view) {
720 show_panel(focus_view->panel);
721 if (view)
722 view->focussed = 0;
723 focus_view->focussed = 1;
724 view = focus_view;
725 if (new_view)
726 show_panel(new_view->panel);
727 if (view->child && view_is_splitscreen(view->child))
728 show_panel(view->child->panel);
730 if (view) {
731 if (focus_view == NULL) {
732 view->focussed = 1;
733 show_panel(view->panel);
734 if (view->child && view_is_splitscreen(view->child))
735 show_panel(view->child->panel);
736 focus_view = view;
738 if (view->parent) {
739 err = view->parent->show(view->parent);
740 if (err)
741 goto done;
743 err = view->show(view);
744 if (err)
745 goto done;
746 if (view->child) {
747 err = view->child->show(view->child);
748 if (err)
749 goto done;
751 update_panels();
752 doupdate();
755 done:
756 while (!TAILQ_EMPTY(&views)) {
757 view = TAILQ_FIRST(&views);
758 TAILQ_REMOVE(&views, view, entry);
759 view_close(view);
762 errcode = pthread_mutex_unlock(&tog_mutex);
763 if (errcode)
764 return got_error_set_errno(errcode);
766 return err;
769 __dead static void
770 usage_log(void)
772 endwin();
773 fprintf(stderr,
774 "usage: %s log [-c commit] [-r repository-path] [path]\n",
775 getprogname());
776 exit(1);
779 /* Create newly allocated wide-character string equivalent to a byte string. */
780 static const struct got_error *
781 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
783 char *vis = NULL;
784 const struct got_error *err = NULL;
786 *ws = NULL;
787 *wlen = mbstowcs(NULL, s, 0);
788 if (*wlen == (size_t)-1) {
789 int vislen;
790 if (errno != EILSEQ)
791 return got_error_from_errno();
793 /* byte string invalid in current encoding; try to "fix" it */
794 err = got_mbsavis(&vis, &vislen, s);
795 if (err)
796 return err;
797 *wlen = mbstowcs(NULL, vis, 0);
798 if (*wlen == (size_t)-1) {
799 err = got_error_from_errno(); /* give up */
800 goto done;
804 *ws = calloc(*wlen + 1, sizeof(*ws));
805 if (*ws == NULL) {
806 err = got_error_from_errno();
807 goto done;
810 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
811 err = got_error_from_errno();
812 done:
813 free(vis);
814 if (err) {
815 free(*ws);
816 *ws = NULL;
817 *wlen = 0;
819 return err;
822 /* Format a line for display, ensuring that it won't overflow a width limit. */
823 static const struct got_error *
824 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
826 const struct got_error *err = NULL;
827 int cols = 0;
828 wchar_t *wline = NULL;
829 size_t wlen;
830 int i;
832 *wlinep = NULL;
833 *widthp = 0;
835 err = mbs2ws(&wline, &wlen, line);
836 if (err)
837 return err;
839 i = 0;
840 while (i < wlen && cols < wlimit) {
841 int width = wcwidth(wline[i]);
842 switch (width) {
843 case 0:
844 i++;
845 break;
846 case 1:
847 case 2:
848 if (cols + width <= wlimit)
849 cols += width;
850 i++;
851 break;
852 case -1:
853 if (wline[i] == L'\t')
854 cols += TABSIZE - ((cols + 1) % TABSIZE);
855 i++;
856 break;
857 default:
858 err = got_error_from_errno();
859 goto done;
862 wline[i] = L'\0';
863 if (widthp)
864 *widthp = cols;
865 done:
866 if (err)
867 free(wline);
868 else
869 *wlinep = wline;
870 return err;
873 static const struct got_error*
874 build_refs_str(char **refs_str, struct got_reflist_head *refs,
875 struct got_object_id *id)
877 static const struct got_error *err = NULL;
878 struct got_reflist_entry *re;
879 char *s;
880 const char *name;
882 *refs_str = NULL;
884 SIMPLEQ_FOREACH(re, refs, entry) {
885 if (got_object_id_cmp(re->id, id) != 0)
886 continue;
887 name = got_ref_get_name(re->ref);
888 if (strcmp(name, GOT_REF_HEAD) == 0)
889 continue;
890 if (strncmp(name, "refs/", 5) == 0)
891 name += 5;
892 if (strncmp(name, "heads/", 6) == 0)
893 name += 6;
894 if (strncmp(name, "remotes/", 8) == 0)
895 name += 8;
896 s = *refs_str;
897 if (asprintf(refs_str, "%s%s%s", s ? s : "",
898 s ? ", " : "", name) == -1) {
899 err = got_error_from_errno();
900 free(s);
901 *refs_str = NULL;
902 break;
904 free(s);
907 return err;
910 static const struct got_error *
911 draw_commit(struct tog_view *view, struct got_commit_object *commit,
912 struct got_object_id *id, struct got_reflist_head *refs)
914 const struct got_error *err = NULL;
915 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
916 char *logmsg0 = NULL, *logmsg = NULL;
917 char *author0 = NULL, *author = NULL;
918 wchar_t *wlogmsg = NULL, *wauthor = NULL;
919 int author_width, logmsg_width;
920 char *newline, *smallerthan;
921 char *line = NULL;
922 int col, limit;
923 static const size_t date_display_cols = 9;
924 static const size_t author_display_cols = 16;
925 const int avail = view->ncols;
926 struct tm tm;
927 time_t committer_time;
929 committer_time = got_object_commit_get_committer_time(commit);
930 if (localtime_r(&committer_time, &tm) == NULL)
931 return got_error_from_errno();
932 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
933 >= sizeof(datebuf))
934 return got_error(GOT_ERR_NO_SPACE);
936 if (avail < date_display_cols)
937 limit = MIN(sizeof(datebuf) - 1, avail);
938 else
939 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
940 waddnstr(view->window, datebuf, limit);
941 col = limit + 1;
942 if (col > avail)
943 goto done;
945 author0 = strdup(got_object_commit_get_author(commit));
946 if (author0 == NULL) {
947 err = got_error_from_errno();
948 goto done;
950 author = author0;
951 smallerthan = strchr(author, '<');
952 if (smallerthan)
953 *smallerthan = '\0';
954 else {
955 char *at = strchr(author, '@');
956 if (at)
957 *at = '\0';
959 limit = avail - col;
960 err = format_line(&wauthor, &author_width, author, limit);
961 if (err)
962 goto done;
963 waddwstr(view->window, wauthor);
964 col += author_width;
965 while (col <= avail && author_width < author_display_cols + 1) {
966 waddch(view->window, ' ');
967 col++;
968 author_width++;
970 if (col > avail)
971 goto done;
973 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
974 if (logmsg0 == NULL) {
975 err = got_error_from_errno();
976 goto done;
978 logmsg = logmsg0;
979 while (*logmsg == '\n')
980 logmsg++;
981 newline = strchr(logmsg, '\n');
982 if (newline)
983 *newline = '\0';
984 limit = avail - col;
985 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
986 if (err)
987 goto done;
988 waddwstr(view->window, wlogmsg);
989 col += logmsg_width;
990 while (col <= avail) {
991 waddch(view->window, ' ');
992 col++;
994 done:
995 free(logmsg0);
996 free(wlogmsg);
997 free(author0);
998 free(wauthor);
999 free(line);
1000 return err;
1003 static struct commit_queue_entry *
1004 alloc_commit_queue_entry(struct got_commit_object *commit,
1005 struct got_object_id *id)
1007 struct commit_queue_entry *entry;
1009 entry = calloc(1, sizeof(*entry));
1010 if (entry == NULL)
1011 return NULL;
1013 entry->id = id;
1014 entry->commit = commit;
1015 return entry;
1018 static void
1019 pop_commit(struct commit_queue *commits)
1021 struct commit_queue_entry *entry;
1023 entry = TAILQ_FIRST(&commits->head);
1024 TAILQ_REMOVE(&commits->head, entry, entry);
1025 got_object_commit_close(entry->commit);
1026 commits->ncommits--;
1027 /* Don't free entry->id! It is owned by the commit graph. */
1028 free(entry);
1031 static void
1032 free_commits(struct commit_queue *commits)
1034 while (!TAILQ_EMPTY(&commits->head))
1035 pop_commit(commits);
1038 static const struct got_error *
1039 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1040 int minqueue, struct got_repository *repo, const char *path)
1042 const struct got_error *err = NULL;
1043 int nqueued = 0;
1046 * We keep all commits open throughout the lifetime of the log
1047 * view in order to avoid having to re-fetch commits from disk
1048 * while updating the display.
1050 while (nqueued < minqueue) {
1051 struct got_object_id *id;
1052 struct got_commit_object *commit;
1053 struct commit_queue_entry *entry;
1054 int errcode;
1056 err = got_commit_graph_iter_next(&id, graph);
1057 if (err) {
1058 if (err->code != GOT_ERR_ITER_NEED_MORE)
1059 break;
1060 err = got_commit_graph_fetch_commits(graph,
1061 minqueue, repo);
1062 if (err)
1063 return err;
1064 continue;
1067 if (id == NULL)
1068 break;
1070 err = got_object_open_as_commit(&commit, repo, id);
1071 if (err)
1072 break;
1073 entry = alloc_commit_queue_entry(commit, id);
1074 if (entry == NULL) {
1075 err = got_error_from_errno();
1076 break;
1079 errcode = pthread_mutex_lock(&tog_mutex);
1080 if (errcode) {
1081 err = got_error_set_errno(errcode);
1082 break;
1085 entry->idx = commits->ncommits;
1086 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1087 nqueued++;
1088 commits->ncommits++;
1090 errcode = pthread_mutex_unlock(&tog_mutex);
1091 if (errcode && err == NULL)
1092 err = got_error_set_errno(errcode);
1095 return err;
1098 static const struct got_error *
1099 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1101 const struct got_error *err = NULL;
1102 struct got_reference *head_ref;
1104 *head_id = NULL;
1106 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1107 if (err)
1108 return err;
1110 err = got_ref_resolve(head_id, repo, head_ref);
1111 got_ref_close(head_ref);
1112 if (err) {
1113 *head_id = NULL;
1114 return err;
1117 return NULL;
1120 static const struct got_error *
1121 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1122 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1123 struct commit_queue *commits, int selected_idx, int limit,
1124 struct got_reflist_head *refs, const char *path, int commits_needed)
1126 const struct got_error *err = NULL;
1127 struct commit_queue_entry *entry;
1128 int ncommits, width;
1129 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1130 char *refs_str = NULL;
1131 wchar_t *wline;
1133 entry = first;
1134 ncommits = 0;
1135 while (entry) {
1136 if (ncommits == selected_idx) {
1137 *selected = entry;
1138 break;
1140 entry = TAILQ_NEXT(entry, entry);
1141 ncommits++;
1144 if (*selected) {
1145 err = got_object_id_str(&id_str, (*selected)->id);
1146 if (err)
1147 return err;
1148 if (refs) {
1149 err = build_refs_str(&refs_str, refs, (*selected)->id);
1150 if (err)
1151 goto done;
1155 if (asprintf(&ncommits_str, " [%d/%d] %s",
1156 entry ? entry->idx + 1 : 0, commits->ncommits,
1157 commits_needed > 0 ? "loading... " :
1158 (refs_str ? refs_str : "")) == -1) {
1159 err = got_error_from_errno();
1160 goto done;
1163 if (path && strcmp(path, "/") != 0) {
1164 if (asprintf(&header, "commit %s %s%s",
1165 id_str ? id_str : "........................................",
1166 path, ncommits_str) == -1) {
1167 err = got_error_from_errno();
1168 header = NULL;
1169 goto done;
1171 } else if (asprintf(&header, "commit %s%s",
1172 id_str ? id_str : "........................................",
1173 ncommits_str) == -1) {
1174 err = got_error_from_errno();
1175 header = NULL;
1176 goto done;
1178 err = format_line(&wline, &width, header, view->ncols);
1179 if (err)
1180 goto done;
1182 werase(view->window);
1184 if (view_needs_focus_indication(view))
1185 wstandout(view->window);
1186 waddwstr(view->window, wline);
1187 while (width < view->ncols) {
1188 waddch(view->window, ' ');
1189 width++;
1191 if (view_needs_focus_indication(view))
1192 wstandend(view->window);
1193 free(wline);
1194 if (limit <= 1)
1195 goto done;
1197 entry = first;
1198 *last = first;
1199 ncommits = 0;
1200 while (entry) {
1201 if (ncommits >= limit - 1)
1202 break;
1203 if (view->focussed && ncommits == selected_idx)
1204 wstandout(view->window);
1205 err = draw_commit(view, entry->commit, entry->id, refs);
1206 if (view->focussed && ncommits == selected_idx)
1207 wstandend(view->window);
1208 if (err)
1209 break;
1210 ncommits++;
1211 *last = entry;
1212 entry = TAILQ_NEXT(entry, entry);
1215 view_vborder(view);
1216 done:
1217 free(id_str);
1218 free(refs_str);
1219 free(ncommits_str);
1220 free(header);
1221 return err;
1224 static void
1225 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1226 struct commit_queue *commits)
1228 struct commit_queue_entry *entry;
1229 int nscrolled = 0;
1231 entry = TAILQ_FIRST(&commits->head);
1232 if (*first_displayed_entry == entry)
1233 return;
1235 entry = *first_displayed_entry;
1236 while (entry && nscrolled < maxscroll) {
1237 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1238 if (entry) {
1239 *first_displayed_entry = entry;
1240 nscrolled++;
1245 static const struct got_error *
1246 scroll_down(struct tog_view *view,
1247 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1248 struct commit_queue_entry **last_displayed_entry,
1249 struct commit_queue *commits, int *log_complete, int *commits_needed,
1250 pthread_cond_t *need_commits)
1252 const struct got_error *err = NULL;
1253 struct commit_queue_entry *pentry;
1254 int nscrolled = 0;
1256 if (*last_displayed_entry == NULL)
1257 return NULL;
1259 do {
1260 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1261 if (pentry == NULL && !*log_complete) {
1262 *commits_needed = maxscroll;
1264 /* Redraw screen for "loading..." message. */
1265 err = show_log_view(view);
1266 if (err)
1267 return err;
1268 update_panels();
1269 doupdate();
1271 while (*commits_needed > 0) {
1272 int errcode;
1273 errcode = pthread_cond_signal(need_commits);
1274 if (errcode)
1275 return got_error_set_errno(errcode);
1276 errcode = pthread_mutex_unlock(&tog_mutex);
1277 if (errcode)
1278 return got_error_set_errno(errcode);
1279 pthread_yield();
1280 errcode = pthread_mutex_lock(&tog_mutex);
1281 if (errcode)
1282 return got_error_set_errno(errcode);
1285 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1286 if (pentry == NULL)
1287 break;
1289 *last_displayed_entry = pentry;
1291 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1292 if (pentry == NULL)
1293 break;
1294 *first_displayed_entry = pentry;
1295 } while (++nscrolled < maxscroll);
1297 return err;
1300 static const struct got_error *
1301 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1302 struct got_commit_object *commit, struct got_object_id *commit_id,
1303 struct tog_view *log_view, struct got_reflist_head *refs,
1304 struct got_repository *repo)
1306 const struct got_error *err;
1307 struct got_object_qid *parent_id;
1308 struct tog_view *diff_view;
1310 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1311 if (diff_view == NULL)
1312 return got_error_from_errno();
1314 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1315 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1316 commit_id, log_view, refs, repo);
1317 if (err == NULL)
1318 *new_view = diff_view;
1319 return err;
1322 static const struct got_error *
1323 browse_commit(struct tog_view **new_view, int begin_x,
1324 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1325 struct got_repository *repo)
1327 const struct got_error *err = NULL;
1328 struct got_tree_object *tree;
1329 struct tog_view *tree_view;
1331 err = got_object_open_as_tree(&tree, repo,
1332 got_object_commit_get_tree_id(entry->commit));
1333 if (err)
1334 return err;
1336 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1337 if (tree_view == NULL)
1338 return got_error_from_errno();
1340 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1341 if (err)
1342 got_object_tree_close(tree);
1343 else
1344 *new_view = tree_view;
1345 return err;
1348 static void *
1349 log_thread(void *arg)
1351 const struct got_error *err = NULL;
1352 int errcode = 0;
1353 struct tog_log_thread_args *a = arg;
1354 int done = 0;
1356 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1357 if (err)
1358 return (void *)err;
1360 while (!done && !err) {
1361 err = queue_commits(a->graph, a->commits, 1, a->repo,
1362 a->in_repo_path);
1363 if (err) {
1364 if (err->code != GOT_ERR_ITER_COMPLETED)
1365 return (void *)err;
1366 err = NULL;
1367 done = 1;
1368 } else if (a->commits_needed > 0)
1369 a->commits_needed--;
1371 errcode = pthread_mutex_lock(&tog_mutex);
1372 if (errcode)
1373 return (void *)got_error_set_errno(errcode);
1375 if (done)
1376 a->log_complete = 1;
1377 else if (*a->quit) {
1378 done = 1;
1379 a->log_complete = 1;
1380 } else if (*a->first_displayed_entry == NULL) {
1381 *a->first_displayed_entry =
1382 TAILQ_FIRST(&a->commits->head);
1383 *a->selected_entry = *a->first_displayed_entry;
1386 if (done)
1387 a->commits_needed = 0;
1388 else if (a->commits_needed == 0) {
1389 errcode = pthread_cond_wait(&a->need_commits,
1390 &tog_mutex);
1391 if (errcode)
1392 err = got_error_set_errno(errcode);
1395 errcode = pthread_mutex_unlock(&tog_mutex);
1396 if (errcode && err == NULL)
1397 err = got_error_set_errno(errcode);
1399 return (void *)err;
1402 static const struct got_error *
1403 stop_log_thread(struct tog_log_view_state *s)
1405 const struct got_error *err = NULL;
1406 int errcode;
1408 if (s->thread) {
1409 s->quit = 1;
1410 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1411 if (errcode)
1412 return got_error_set_errno(errcode);
1413 errcode = pthread_mutex_unlock(&tog_mutex);
1414 if (errcode)
1415 return got_error_set_errno(errcode);
1416 errcode = pthread_join(s->thread, (void **)&err);
1417 if (errcode)
1418 return got_error_set_errno(errcode);
1419 errcode = pthread_mutex_lock(&tog_mutex);
1420 if (errcode)
1421 return got_error_set_errno(errcode);
1422 s->thread = NULL;
1425 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1426 if (errcode && err == NULL)
1427 err = got_error_set_errno(errcode);
1429 if (s->thread_args.repo) {
1430 got_repo_close(s->thread_args.repo);
1431 s->thread_args.repo = NULL;
1434 if (s->thread_args.graph) {
1435 got_commit_graph_close(s->thread_args.graph);
1436 s->thread_args.graph = NULL;
1439 return err;
1442 static const struct got_error *
1443 close_log_view(struct tog_view *view)
1445 const struct got_error *err = NULL;
1446 struct tog_log_view_state *s = &view->state.log;
1448 err = stop_log_thread(s);
1449 free_commits(&s->commits);
1450 free(s->in_repo_path);
1451 s->in_repo_path = NULL;
1452 free(s->start_id);
1453 s->start_id = NULL;
1454 return err;
1457 static const struct got_error *
1458 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1459 struct got_reflist_head *refs, struct got_repository *repo,
1460 const char *path, int check_disk)
1462 const struct got_error *err = NULL;
1463 struct tog_log_view_state *s = &view->state.log;
1464 struct got_repository *thread_repo = NULL;
1465 struct got_commit_graph *thread_graph = NULL;
1466 int errcode;
1468 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1469 if (err != NULL)
1470 goto done;
1472 /* The commit queue only contains commits being displayed. */
1473 TAILQ_INIT(&s->commits.head);
1474 s->commits.ncommits = 0;
1476 s->refs = refs;
1477 s->repo = repo;
1478 s->start_id = got_object_id_dup(start_id);
1479 if (s->start_id == NULL) {
1480 err = got_error_from_errno();
1481 goto done;
1484 view->show = show_log_view;
1485 view->input = input_log_view;
1486 view->close = close_log_view;
1488 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1489 if (err)
1490 goto done;
1491 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1492 0, thread_repo);
1493 if (err)
1494 goto done;
1496 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1497 if (errcode) {
1498 err = got_error_set_errno(errcode);
1499 goto done;
1502 s->thread_args.commits_needed = view->nlines;
1503 s->thread_args.graph = thread_graph;
1504 s->thread_args.commits = &s->commits;
1505 s->thread_args.in_repo_path = s->in_repo_path;
1506 s->thread_args.start_id = s->start_id;
1507 s->thread_args.repo = thread_repo;
1508 s->thread_args.log_complete = 0;
1509 s->thread_args.quit = &s->quit;
1510 s->thread_args.view = view;
1511 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1512 s->thread_args.selected_entry = &s->selected_entry;
1513 done:
1514 if (err)
1515 close_log_view(view);
1516 return err;
1519 static const struct got_error *
1520 show_log_view(struct tog_view *view)
1522 struct tog_log_view_state *s = &view->state.log;
1524 if (s->thread == NULL) {
1525 int errcode = pthread_create(&s->thread, NULL, log_thread,
1526 &s->thread_args);
1527 if (errcode)
1528 return got_error_set_errno(errcode);
1531 return draw_commits(view, &s->last_displayed_entry,
1532 &s->selected_entry, s->first_displayed_entry,
1533 &s->commits, s->selected, view->nlines, s->refs,
1534 s->in_repo_path, s->thread_args.commits_needed);
1537 static const struct got_error *
1538 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1539 struct tog_view **focus_view, struct tog_view *view, int ch)
1541 const struct got_error *err = NULL;
1542 struct tog_log_view_state *s = &view->state.log;
1543 char *parent_path;
1544 struct tog_view *diff_view = NULL, *tree_view = NULL;
1545 int begin_x = 0;
1547 switch (ch) {
1548 case 'q':
1549 s->quit = 1;
1550 break;
1551 case 'k':
1552 case KEY_UP:
1553 case '<':
1554 case ',':
1555 if (s->first_displayed_entry == NULL)
1556 break;
1557 if (s->selected > 0)
1558 s->selected--;
1559 if (s->selected > 0)
1560 break;
1561 scroll_up(&s->first_displayed_entry, 1,
1562 &s->commits);
1563 break;
1564 case KEY_PPAGE:
1565 if (s->first_displayed_entry == NULL)
1566 break;
1567 if (TAILQ_FIRST(&s->commits.head) ==
1568 s->first_displayed_entry) {
1569 s->selected = 0;
1570 break;
1572 scroll_up(&s->first_displayed_entry,
1573 view->nlines, &s->commits);
1574 break;
1575 case 'j':
1576 case KEY_DOWN:
1577 case '>':
1578 case '.':
1579 if (s->first_displayed_entry == NULL)
1580 break;
1581 if (s->selected < MIN(view->nlines - 2,
1582 s->commits.ncommits - 1)) {
1583 s->selected++;
1584 break;
1586 err = scroll_down(view, &s->first_displayed_entry, 1,
1587 &s->last_displayed_entry, &s->commits,
1588 &s->thread_args.log_complete,
1589 &s->thread_args.commits_needed,
1590 &s->thread_args.need_commits);
1591 break;
1592 case KEY_NPAGE: {
1593 struct commit_queue_entry *first;
1594 first = s->first_displayed_entry;
1595 if (first == NULL)
1596 break;
1597 err = scroll_down(view, &s->first_displayed_entry,
1598 view->nlines, &s->last_displayed_entry,
1599 &s->commits, &s->thread_args.log_complete,
1600 &s->thread_args.commits_needed,
1601 &s->thread_args.need_commits);
1602 if (first == s->first_displayed_entry &&
1603 s->selected < MIN(view->nlines - 2,
1604 s->commits.ncommits - 1)) {
1605 /* can't scroll further down */
1606 s->selected = MIN(view->nlines - 2,
1607 s->commits.ncommits - 1);
1609 err = NULL;
1610 break;
1612 case KEY_RESIZE:
1613 if (s->selected > view->nlines - 2)
1614 s->selected = view->nlines - 2;
1615 if (s->selected > s->commits.ncommits - 1)
1616 s->selected = s->commits.ncommits - 1;
1617 break;
1618 case KEY_ENTER:
1619 case '\r':
1620 if (s->selected_entry == NULL)
1621 break;
1622 if (view_is_parent_view(view))
1623 begin_x = view_split_begin_x(view->begin_x);
1624 err = open_diff_view_for_commit(&diff_view, begin_x,
1625 s->selected_entry->commit, s->selected_entry->id,
1626 view, s->refs, s->repo);
1627 if (err)
1628 break;
1629 if (view_is_parent_view(view)) {
1630 err = view_close_child(view);
1631 if (err)
1632 return err;
1633 err = view_set_child(view, diff_view);
1634 if (err) {
1635 view_close(diff_view);
1636 break;
1638 *focus_view = diff_view;
1639 view->child_focussed = 1;
1640 } else
1641 *new_view = diff_view;
1642 break;
1643 case 't':
1644 if (s->selected_entry == NULL)
1645 break;
1646 if (view_is_parent_view(view))
1647 begin_x = view_split_begin_x(view->begin_x);
1648 err = browse_commit(&tree_view, begin_x,
1649 s->selected_entry, s->refs, s->repo);
1650 if (err)
1651 break;
1652 if (view_is_parent_view(view)) {
1653 err = view_close_child(view);
1654 if (err)
1655 return err;
1656 err = view_set_child(view, tree_view);
1657 if (err) {
1658 view_close(tree_view);
1659 break;
1661 *focus_view = tree_view;
1662 view->child_focussed = 1;
1663 } else
1664 *new_view = tree_view;
1665 break;
1666 case KEY_BACKSPACE:
1667 if (strcmp(s->in_repo_path, "/") == 0)
1668 break;
1669 parent_path = dirname(s->in_repo_path);
1670 if (parent_path && strcmp(parent_path, ".") != 0) {
1671 struct tog_view *lv;
1672 err = stop_log_thread(s);
1673 if (err)
1674 return err;
1675 lv = view_open(view->nlines, view->ncols,
1676 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1677 if (lv == NULL)
1678 return got_error_from_errno();
1679 err = open_log_view(lv, s->start_id, s->refs,
1680 s->repo, parent_path, 0);
1681 if (err)
1682 return err;;
1683 if (view_is_parent_view(view))
1684 *new_view = lv;
1685 else {
1686 view_set_child(view->parent, lv);
1687 *focus_view = lv;
1689 return NULL;
1691 break;
1692 default:
1693 break;
1696 return err;
1699 static const struct got_error *
1700 apply_unveil(const char *repo_path, const char *worktree_path)
1702 const struct got_error *error;
1704 if (repo_path && unveil(repo_path, "r") != 0)
1705 return got_error_from_errno();
1707 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1708 return got_error_from_errno();
1710 if (unveil("/tmp", "rwc") != 0)
1711 return got_error_from_errno();
1713 error = got_privsep_unveil_exec_helpers();
1714 if (error != NULL)
1715 return error;
1717 if (unveil(NULL, NULL) != 0)
1718 return got_error_from_errno();
1720 return NULL;
1723 static void
1724 init_curses(void)
1726 initscr();
1727 cbreak();
1728 halfdelay(1); /* Do fast refresh while initial view is loading. */
1729 noecho();
1730 nonl();
1731 intrflush(stdscr, FALSE);
1732 keypad(stdscr, TRUE);
1733 curs_set(0);
1734 signal(SIGWINCH, tog_sigwinch);
1737 static const struct got_error *
1738 cmd_log(int argc, char *argv[])
1740 const struct got_error *error;
1741 struct got_repository *repo = NULL;
1742 struct got_reflist_head refs;
1743 struct got_object_id *start_id = NULL;
1744 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1745 char *start_commit = NULL;
1746 int ch;
1747 struct tog_view *view;
1749 #ifndef PROFILE
1750 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1751 NULL) == -1)
1752 err(1, "pledge");
1753 #endif
1755 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1756 switch (ch) {
1757 case 'c':
1758 start_commit = optarg;
1759 break;
1760 case 'r':
1761 repo_path = realpath(optarg, NULL);
1762 if (repo_path == NULL)
1763 err(1, "-r option");
1764 break;
1765 default:
1766 usage();
1767 /* NOTREACHED */
1771 argc -= optind;
1772 argv += optind;
1774 if (argc == 0)
1775 path = strdup("");
1776 else if (argc == 1)
1777 path = strdup(argv[0]);
1778 else
1779 usage_log();
1780 if (path == NULL)
1781 return got_error_from_errno();
1783 cwd = getcwd(NULL, 0);
1784 if (cwd == NULL) {
1785 error = got_error_from_errno();
1786 goto done;
1788 if (repo_path == NULL) {
1789 struct got_worktree *worktree;
1790 error = got_worktree_open(&worktree, cwd);
1791 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1792 goto done;
1793 if (worktree) {
1794 repo_path =
1795 strdup(got_worktree_get_repo_path(worktree));
1796 got_worktree_close(worktree);
1797 } else
1798 repo_path = strdup(cwd);
1799 if (repo_path == NULL) {
1800 error = got_error_from_errno();
1801 goto done;
1805 init_curses();
1807 error = apply_unveil(repo_path, NULL);
1808 if (error)
1809 goto done;
1811 error = got_repo_open(&repo, repo_path);
1812 if (error != NULL)
1813 goto done;
1815 if (start_commit == NULL)
1816 error = get_head_commit_id(&start_id, repo);
1817 else
1818 error = got_object_resolve_id_str(&start_id, repo,
1819 start_commit);
1820 if (error != NULL)
1821 goto done;
1823 SIMPLEQ_INIT(&refs);
1824 error = got_ref_list(&refs, repo);
1825 if (error)
1826 goto done;
1828 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1829 if (view == NULL) {
1830 error = got_error_from_errno();
1831 goto done;
1833 error = open_log_view(view, start_id, &refs, repo, path, 1);
1834 if (error)
1835 goto done;
1836 error = view_loop(view);
1837 done:
1838 free(repo_path);
1839 free(cwd);
1840 free(path);
1841 free(start_id);
1842 if (repo)
1843 got_repo_close(repo);
1844 return error;
1847 __dead static void
1848 usage_diff(void)
1850 endwin();
1851 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1852 getprogname());
1853 exit(1);
1856 static char *
1857 parse_next_line(FILE *f, size_t *len)
1859 char *line;
1860 size_t linelen;
1861 size_t lineno;
1862 const char delim[3] = { '\0', '\0', '\0'};
1864 line = fparseln(f, &linelen, &lineno, delim, 0);
1865 if (len)
1866 *len = linelen;
1867 return line;
1870 static const struct got_error *
1871 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1872 int *last_displayed_line, int *eof, int max_lines,
1873 char * header)
1875 const struct got_error *err;
1876 int nlines = 0, nprinted = 0;
1877 char *line;
1878 size_t len;
1879 wchar_t *wline;
1880 int width;
1882 rewind(f);
1883 werase(view->window);
1885 if (header) {
1886 err = format_line(&wline, &width, header, view->ncols);
1887 if (err) {
1888 return err;
1891 if (view_needs_focus_indication(view))
1892 wstandout(view->window);
1893 waddwstr(view->window, wline);
1894 if (view_needs_focus_indication(view))
1895 wstandend(view->window);
1896 if (width < view->ncols)
1897 waddch(view->window, '\n');
1899 if (max_lines <= 1)
1900 return NULL;
1901 max_lines--;
1904 *eof = 0;
1905 while (nprinted < max_lines) {
1906 line = parse_next_line(f, &len);
1907 if (line == NULL) {
1908 *eof = 1;
1909 break;
1911 if (++nlines < *first_displayed_line) {
1912 free(line);
1913 continue;
1916 err = format_line(&wline, &width, line, view->ncols);
1917 if (err) {
1918 free(line);
1919 return err;
1921 waddwstr(view->window, wline);
1922 if (width < view->ncols)
1923 waddch(view->window, '\n');
1924 if (++nprinted == 1)
1925 *first_displayed_line = nlines;
1926 free(line);
1927 free(wline);
1928 wline = NULL;
1930 *last_displayed_line = nlines;
1932 view_vborder(view);
1934 return NULL;
1937 static char *
1938 get_datestr(time_t *time, char *datebuf)
1940 char *p, *s = ctime_r(time, datebuf);
1941 p = strchr(s, '\n');
1942 if (p)
1943 *p = '\0';
1944 return s;
1947 static const struct got_error *
1948 write_commit_info(struct got_object_id *commit_id,
1949 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
1951 const struct got_error *err = NULL;
1952 char datebuf[26];
1953 struct got_commit_object *commit;
1954 char *id_str = NULL;
1955 time_t committer_time;
1956 const char *author, *committer;
1957 char *refs_str = NULL;
1959 if (refs) {
1960 err = build_refs_str(&refs_str, refs, commit_id);
1961 if (err)
1962 return err;
1965 err = got_object_open_as_commit(&commit, repo, commit_id);
1966 if (err)
1967 return err;
1969 err = got_object_id_str(&id_str, commit_id);
1970 if (err) {
1971 err = got_error_from_errno();
1972 goto done;
1975 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1976 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
1977 err = got_error_from_errno();
1978 goto done;
1980 if (fprintf(outfile, "from: %s\n",
1981 got_object_commit_get_author(commit)) < 0) {
1982 err = got_error_from_errno();
1983 goto done;
1985 committer_time = got_object_commit_get_committer_time(commit);
1986 if (fprintf(outfile, "date: %s UTC\n",
1987 get_datestr(&committer_time, datebuf)) < 0) {
1988 err = got_error_from_errno();
1989 goto done;
1991 author = got_object_commit_get_author(commit);
1992 committer = got_object_commit_get_committer(commit);
1993 if (strcmp(author, committer) != 0 &&
1994 fprintf(outfile, "via: %s\n", committer) < 0) {
1995 err = got_error_from_errno();
1996 goto done;
1998 if (fprintf(outfile, "%s\n",
1999 got_object_commit_get_logmsg(commit)) < 0) {
2000 err = got_error_from_errno();
2001 goto done;
2003 done:
2004 free(id_str);
2005 free(refs_str);
2006 got_object_commit_close(commit);
2007 return err;
2010 static const struct got_error *
2011 create_diff(struct tog_diff_view_state *s)
2013 const struct got_error *err = NULL;
2014 FILE *f = NULL;
2015 int obj_type;
2017 f = got_opentemp();
2018 if (f == NULL) {
2019 err = got_error_from_errno();
2020 goto done;
2022 if (s->f && fclose(s->f) != 0) {
2023 err = got_error_from_errno();
2024 goto done;
2026 s->f = f;
2028 if (s->id1)
2029 err = got_object_get_type(&obj_type, s->repo, s->id1);
2030 else
2031 err = got_object_get_type(&obj_type, s->repo, s->id2);
2032 if (err)
2033 goto done;
2035 switch (obj_type) {
2036 case GOT_OBJ_TYPE_BLOB:
2037 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2038 s->diff_context, s->repo, f);
2039 break;
2040 case GOT_OBJ_TYPE_TREE:
2041 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2042 s->diff_context, s->repo, f);
2043 break;
2044 case GOT_OBJ_TYPE_COMMIT: {
2045 const struct got_object_id_queue *parent_ids;
2046 struct got_object_qid *pid;
2047 struct got_commit_object *commit2;
2049 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2050 if (err)
2051 break;
2052 /* Show commit info if we're diffing to a parent/root commit. */
2053 if (s->id1 == NULL)
2054 write_commit_info(s->id2, s->refs, s->repo, f);
2055 else {
2056 parent_ids = got_object_commit_get_parent_ids(commit2);
2057 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2058 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2059 write_commit_info(s->id2, s->refs,
2060 s->repo, f);
2061 break;
2065 got_object_commit_close(commit2);
2067 err = got_diff_objects_as_commits(s->id1, s->id2,
2068 s->diff_context, s->repo, f);
2069 break;
2071 default:
2072 err = got_error(GOT_ERR_OBJ_TYPE);
2073 break;
2075 done:
2076 if (f && fflush(f) != 0 && err == NULL)
2077 err = got_error_from_errno();
2078 return err;
2081 static const struct got_error *
2082 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2083 struct got_object_id *id2, struct tog_view *log_view,
2084 struct got_reflist_head *refs, struct got_repository *repo)
2086 const struct got_error *err;
2088 if (id1 != NULL && id2 != NULL) {
2089 int type1, type2;
2090 err = got_object_get_type(&type1, repo, id1);
2091 if (err)
2092 return err;
2093 err = got_object_get_type(&type2, repo, id2);
2094 if (err)
2095 return err;
2097 if (type1 != type2)
2098 return got_error(GOT_ERR_OBJ_TYPE);
2101 if (id1) {
2102 view->state.diff.id1 = got_object_id_dup(id1);
2103 if (view->state.diff.id1 == NULL)
2104 return got_error_from_errno();
2105 } else
2106 view->state.diff.id1 = NULL;
2108 view->state.diff.id2 = got_object_id_dup(id2);
2109 if (view->state.diff.id2 == NULL) {
2110 free(view->state.diff.id1);
2111 view->state.diff.id1 = NULL;
2112 return got_error_from_errno();
2114 view->state.diff.f = NULL;
2115 view->state.diff.first_displayed_line = 1;
2116 view->state.diff.last_displayed_line = view->nlines;
2117 view->state.diff.diff_context = 3;
2118 view->state.diff.log_view = log_view;
2119 view->state.diff.repo = repo;
2120 view->state.diff.refs = refs;
2122 err = create_diff(&view->state.diff);
2123 if (err) {
2124 free(view->state.diff.id1);
2125 view->state.diff.id1 = NULL;
2126 free(view->state.diff.id2);
2127 view->state.diff.id2 = NULL;
2128 return err;
2131 view->show = show_diff_view;
2132 view->input = input_diff_view;
2133 view->close = close_diff_view;
2135 return NULL;
2138 static const struct got_error *
2139 close_diff_view(struct tog_view *view)
2141 const struct got_error *err = NULL;
2143 free(view->state.diff.id1);
2144 view->state.diff.id1 = NULL;
2145 free(view->state.diff.id2);
2146 view->state.diff.id2 = NULL;
2147 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2148 err = got_error_from_errno();
2149 return err;
2152 static const struct got_error *
2153 show_diff_view(struct tog_view *view)
2155 const struct got_error *err;
2156 struct tog_diff_view_state *s = &view->state.diff;
2157 char *id_str1 = NULL, *id_str2, *header;
2159 if (s->id1) {
2160 err = got_object_id_str(&id_str1, s->id1);
2161 if (err)
2162 return err;
2164 err = got_object_id_str(&id_str2, s->id2);
2165 if (err)
2166 return err;
2168 if (asprintf(&header, "diff %s %s",
2169 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2170 err = got_error_from_errno();
2171 free(id_str1);
2172 free(id_str2);
2173 return err;
2175 free(id_str1);
2176 free(id_str2);
2178 return draw_file(view, s->f, &s->first_displayed_line,
2179 &s->last_displayed_line, &s->eof, view->nlines,
2180 header);
2183 static const struct got_error *
2184 set_selected_commit(struct tog_diff_view_state *s,
2185 struct commit_queue_entry *entry)
2187 const struct got_error *err;
2188 const struct got_object_id_queue *parent_ids;
2189 struct got_commit_object *selected_commit;
2190 struct got_object_qid *pid;
2192 free(s->id2);
2193 s->id2 = got_object_id_dup(entry->id);
2194 if (s->id2 == NULL)
2195 return got_error_from_errno();
2197 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2198 if (err)
2199 return err;
2200 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2201 free(s->id1);
2202 pid = SIMPLEQ_FIRST(parent_ids);
2203 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2204 got_object_commit_close(selected_commit);
2205 return NULL;
2208 static const struct got_error *
2209 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2210 struct tog_view **focus_view, struct tog_view *view, int ch)
2212 const struct got_error *err = NULL;
2213 struct tog_diff_view_state *s = &view->state.diff;
2214 struct tog_log_view_state *ls;
2215 struct commit_queue_entry *entry;
2216 int i;
2218 switch (ch) {
2219 case 'k':
2220 case KEY_UP:
2221 if (s->first_displayed_line > 1)
2222 s->first_displayed_line--;
2223 break;
2224 case KEY_PPAGE:
2225 i = 0;
2226 while (i++ < view->nlines - 1 &&
2227 s->first_displayed_line > 1)
2228 s->first_displayed_line--;
2229 break;
2230 case 'j':
2231 case KEY_DOWN:
2232 if (!s->eof)
2233 s->first_displayed_line++;
2234 break;
2235 case KEY_NPAGE:
2236 case ' ':
2237 i = 0;
2238 while (!s->eof && i++ < view->nlines - 1) {
2239 char *line;
2240 line = parse_next_line(s->f, NULL);
2241 s->first_displayed_line++;
2242 if (line == NULL)
2243 break;
2245 break;
2246 case '[':
2247 if (s->diff_context > 0) {
2248 s->diff_context--;
2249 err = create_diff(s);
2251 break;
2252 case ']':
2253 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2254 s->diff_context++;
2255 err = create_diff(s);
2257 break;
2258 case '<':
2259 case ',':
2260 if (s->log_view == NULL)
2261 break;
2262 ls = &s->log_view->state.log;
2263 entry = TAILQ_PREV(ls->selected_entry,
2264 commit_queue_head, entry);
2265 if (entry == NULL)
2266 break;
2268 err = input_log_view(NULL, NULL, NULL, s->log_view,
2269 KEY_UP);
2270 if (err)
2271 break;
2273 err = set_selected_commit(s, entry);
2274 if (err)
2275 break;
2277 s->first_displayed_line = 1;
2278 s->last_displayed_line = view->nlines;
2280 err = create_diff(s);
2281 break;
2282 case '>':
2283 case '.':
2284 if (s->log_view == NULL)
2285 break;
2286 ls = &s->log_view->state.log;
2287 err = input_log_view(NULL, NULL, NULL, s->log_view,
2288 KEY_DOWN);
2289 if (err)
2290 break;
2292 /* Hack: Ensure two commits get loaded. */
2293 if (!ls->thread_args.log_complete) {
2294 err = input_log_view(NULL, NULL, NULL,
2295 s->log_view, KEY_DOWN);
2296 if (err)
2297 break;
2298 err = input_log_view(NULL, NULL, NULL,
2299 s->log_view, KEY_UP);
2300 if (err)
2301 break;
2304 entry = TAILQ_NEXT(ls->selected_entry, entry);
2305 if (entry == NULL)
2306 break;
2308 err = set_selected_commit(s, entry);
2309 if (err)
2310 break;
2312 s->first_displayed_line = 1;
2313 s->last_displayed_line = view->nlines;
2315 err = create_diff(s);
2316 break;
2317 default:
2318 break;
2321 return err;
2324 static const struct got_error *
2325 cmd_diff(int argc, char *argv[])
2327 const struct got_error *error = NULL;
2328 struct got_repository *repo = NULL;
2329 struct got_reflist_head refs;
2330 struct got_object_id *id1 = NULL, *id2 = NULL;
2331 char *repo_path = NULL;
2332 char *id_str1 = NULL, *id_str2 = NULL;
2333 int ch;
2334 struct tog_view *view;
2336 #ifndef PROFILE
2337 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2338 NULL) == -1)
2339 err(1, "pledge");
2340 #endif
2342 while ((ch = getopt(argc, argv, "")) != -1) {
2343 switch (ch) {
2344 default:
2345 usage();
2346 /* NOTREACHED */
2350 argc -= optind;
2351 argv += optind;
2353 if (argc == 0) {
2354 usage_diff(); /* TODO show local worktree changes */
2355 } else if (argc == 2) {
2356 repo_path = getcwd(NULL, 0);
2357 if (repo_path == NULL)
2358 return got_error_from_errno();
2359 id_str1 = argv[0];
2360 id_str2 = argv[1];
2361 } else if (argc == 3) {
2362 repo_path = realpath(argv[0], NULL);
2363 if (repo_path == NULL)
2364 return got_error_from_errno();
2365 id_str1 = argv[1];
2366 id_str2 = argv[2];
2367 } else
2368 usage_diff();
2370 init_curses();
2372 error = apply_unveil(repo_path, NULL);
2373 if (error)
2374 goto done;
2376 error = got_repo_open(&repo, repo_path);
2377 free(repo_path);
2378 if (error)
2379 goto done;
2381 error = got_object_resolve_id_str(&id1, repo, id_str1);
2382 if (error)
2383 goto done;
2385 error = got_object_resolve_id_str(&id2, repo, id_str2);
2386 if (error)
2387 goto done;
2389 SIMPLEQ_INIT(&refs);
2390 error = got_ref_list(&refs, repo);
2391 if (error)
2392 goto done;
2394 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2395 if (view == NULL) {
2396 error = got_error_from_errno();
2397 goto done;
2399 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2400 if (error)
2401 goto done;
2402 error = view_loop(view);
2403 done:
2404 got_repo_close(repo);
2405 return error;
2408 __dead static void
2409 usage_blame(void)
2411 endwin();
2412 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2413 getprogname());
2414 exit(1);
2417 struct tog_blame_line {
2418 int annotated;
2419 struct got_object_id *id;
2422 static const struct got_error *
2423 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2424 const char *path, struct tog_blame_line *lines, int nlines,
2425 int blame_complete, int selected_line, int *first_displayed_line,
2426 int *last_displayed_line, int *eof, int max_lines)
2428 const struct got_error *err;
2429 int lineno = 0, nprinted = 0;
2430 char *line;
2431 size_t len;
2432 wchar_t *wline;
2433 int width, wlimit;
2434 struct tog_blame_line *blame_line;
2435 struct got_object_id *prev_id = NULL;
2436 char *id_str;
2438 err = got_object_id_str(&id_str, id);
2439 if (err)
2440 return err;
2442 rewind(f);
2443 werase(view->window);
2445 if (asprintf(&line, "commit %s", id_str) == -1) {
2446 err = got_error_from_errno();
2447 free(id_str);
2448 return err;
2451 err = format_line(&wline, &width, line, view->ncols);
2452 free(line);
2453 line = NULL;
2454 if (view_needs_focus_indication(view))
2455 wstandout(view->window);
2456 waddwstr(view->window, wline);
2457 if (view_needs_focus_indication(view))
2458 wstandend(view->window);
2459 free(wline);
2460 wline = NULL;
2461 if (width < view->ncols)
2462 waddch(view->window, '\n');
2464 if (asprintf(&line, "[%d/%d] %s%s",
2465 *first_displayed_line - 1 + selected_line, nlines,
2466 blame_complete ? "" : "annotating... ", path) == -1) {
2467 free(id_str);
2468 return got_error_from_errno();
2470 free(id_str);
2471 err = format_line(&wline, &width, line, view->ncols);
2472 free(line);
2473 line = NULL;
2474 if (err)
2475 return err;
2476 waddwstr(view->window, wline);
2477 free(wline);
2478 wline = NULL;
2479 if (width < view->ncols)
2480 waddch(view->window, '\n');
2482 *eof = 0;
2483 while (nprinted < max_lines - 2) {
2484 line = parse_next_line(f, &len);
2485 if (line == NULL) {
2486 *eof = 1;
2487 break;
2489 if (++lineno < *first_displayed_line) {
2490 free(line);
2491 continue;
2494 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2495 err = format_line(&wline, &width, line, wlimit);
2496 if (err) {
2497 free(line);
2498 return err;
2501 if (view->focussed && nprinted == selected_line - 1)
2502 wstandout(view->window);
2504 blame_line = &lines[lineno - 1];
2505 if (blame_line->annotated && prev_id &&
2506 got_object_id_cmp(prev_id, blame_line->id) == 0)
2507 waddstr(view->window, " ");
2508 else if (blame_line->annotated) {
2509 char *id_str;
2510 err = got_object_id_str(&id_str, blame_line->id);
2511 if (err) {
2512 free(line);
2513 free(wline);
2514 return err;
2516 wprintw(view->window, "%.8s ", id_str);
2517 free(id_str);
2518 prev_id = blame_line->id;
2519 } else {
2520 waddstr(view->window, "........ ");
2521 prev_id = NULL;
2524 waddwstr(view->window, wline);
2525 while (width < wlimit) {
2526 waddch(view->window, ' ');
2527 width++;
2529 if (view->focussed && nprinted == selected_line - 1)
2530 wstandend(view->window);
2531 if (++nprinted == 1)
2532 *first_displayed_line = lineno;
2533 free(line);
2534 free(wline);
2535 wline = NULL;
2537 *last_displayed_line = lineno;
2539 view_vborder(view);
2541 return NULL;
2544 static const struct got_error *
2545 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2547 const struct got_error *err = NULL;
2548 struct tog_blame_cb_args *a = arg;
2549 struct tog_blame_line *line;
2550 int errcode;
2552 if (nlines != a->nlines ||
2553 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2554 return got_error(GOT_ERR_RANGE);
2556 errcode = pthread_mutex_lock(&tog_mutex);
2557 if (errcode)
2558 return got_error_set_errno(errcode);
2560 if (*a->quit) { /* user has quit the blame view */
2561 err = got_error(GOT_ERR_ITER_COMPLETED);
2562 goto done;
2565 if (lineno == -1)
2566 goto done; /* no change in this commit */
2568 line = &a->lines[lineno - 1];
2569 if (line->annotated)
2570 goto done;
2572 line->id = got_object_id_dup(id);
2573 if (line->id == NULL) {
2574 err = got_error_from_errno();
2575 goto done;
2577 line->annotated = 1;
2578 done:
2579 errcode = pthread_mutex_unlock(&tog_mutex);
2580 if (errcode)
2581 err = got_error_set_errno(errcode);
2582 return err;
2585 static void *
2586 blame_thread(void *arg)
2588 const struct got_error *err;
2589 struct tog_blame_thread_args *ta = arg;
2590 struct tog_blame_cb_args *a = ta->cb_args;
2591 int errcode;
2593 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2594 blame_cb, ta->cb_args);
2596 errcode = pthread_mutex_lock(&tog_mutex);
2597 if (errcode)
2598 return (void *)got_error_set_errno(errcode);
2600 got_repo_close(ta->repo);
2601 ta->repo = NULL;
2602 *ta->complete = 1;
2604 errcode = pthread_mutex_unlock(&tog_mutex);
2605 if (errcode && err == NULL)
2606 err = got_error_set_errno(errcode);
2608 return (void *)err;
2611 static struct got_object_id *
2612 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2613 int selected_line)
2615 struct tog_blame_line *line;
2617 line = &lines[first_displayed_line - 1 + selected_line - 1];
2618 if (!line->annotated)
2619 return NULL;
2621 return line->id;
2624 static const struct got_error *
2625 stop_blame(struct tog_blame *blame)
2627 const struct got_error *err = NULL;
2628 int i;
2630 if (blame->thread) {
2631 int errcode;
2632 errcode = pthread_mutex_unlock(&tog_mutex);
2633 if (errcode)
2634 return got_error_set_errno(errcode);
2635 errcode = pthread_join(blame->thread, (void **)&err);
2636 if (errcode)
2637 return got_error_set_errno(errcode);
2638 errcode = pthread_mutex_lock(&tog_mutex);
2639 if (errcode)
2640 return got_error_set_errno(errcode);
2641 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2642 err = NULL;
2643 blame->thread = NULL;
2645 if (blame->thread_args.repo) {
2646 got_repo_close(blame->thread_args.repo);
2647 blame->thread_args.repo = NULL;
2649 if (blame->f) {
2650 if (fclose(blame->f) != 0 && err == NULL)
2651 err = got_error_from_errno();
2652 blame->f = NULL;
2654 if (blame->lines) {
2655 for (i = 0; i < blame->nlines; i++)
2656 free(blame->lines[i].id);
2657 free(blame->lines);
2658 blame->lines = NULL;
2660 free(blame->cb_args.commit_id);
2661 blame->cb_args.commit_id = NULL;
2663 return err;
2666 static const struct got_error *
2667 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2668 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2669 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2670 struct got_repository *repo)
2672 const struct got_error *err = NULL;
2673 struct got_blob_object *blob = NULL;
2674 struct got_repository *thread_repo = NULL;
2675 struct got_object_id *obj_id = NULL;
2676 int obj_type;
2678 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2679 if (err)
2680 return err;
2681 if (obj_id == NULL)
2682 return got_error(GOT_ERR_NO_OBJ);
2684 err = got_object_get_type(&obj_type, repo, obj_id);
2685 if (err)
2686 goto done;
2688 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2689 err = got_error(GOT_ERR_OBJ_TYPE);
2690 goto done;
2693 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2694 if (err)
2695 goto done;
2696 blame->f = got_opentemp();
2697 if (blame->f == NULL) {
2698 err = got_error_from_errno();
2699 goto done;
2701 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2702 blame->f, blob);
2703 if (err)
2704 goto done;
2706 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2707 if (blame->lines == NULL) {
2708 err = got_error_from_errno();
2709 goto done;
2712 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2713 if (err)
2714 goto done;
2716 blame->cb_args.view = view;
2717 blame->cb_args.lines = blame->lines;
2718 blame->cb_args.nlines = blame->nlines;
2719 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2720 if (blame->cb_args.commit_id == NULL) {
2721 err = got_error_from_errno();
2722 goto done;
2724 blame->cb_args.quit = done;
2726 blame->thread_args.path = path;
2727 blame->thread_args.repo = thread_repo;
2728 blame->thread_args.cb_args = &blame->cb_args;
2729 blame->thread_args.complete = blame_complete;
2730 *blame_complete = 0;
2732 done:
2733 if (blob)
2734 got_object_blob_close(blob);
2735 free(obj_id);
2736 if (err)
2737 stop_blame(blame);
2738 return err;
2741 static const struct got_error *
2742 open_blame_view(struct tog_view *view, char *path,
2743 struct got_object_id *commit_id, struct got_reflist_head *refs,
2744 struct got_repository *repo)
2746 const struct got_error *err = NULL;
2747 struct tog_blame_view_state *s = &view->state.blame;
2749 SIMPLEQ_INIT(&s->blamed_commits);
2751 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2752 if (err)
2753 return err;
2755 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2756 s->first_displayed_line = 1;
2757 s->last_displayed_line = view->nlines;
2758 s->selected_line = 1;
2759 s->blame_complete = 0;
2760 s->path = path;
2761 if (s->path == NULL)
2762 return got_error_from_errno();
2763 s->repo = repo;
2764 s->refs = refs;
2765 s->commit_id = commit_id;
2766 memset(&s->blame, 0, sizeof(s->blame));
2768 view->show = show_blame_view;
2769 view->input = input_blame_view;
2770 view->close = close_blame_view;
2772 return run_blame(&s->blame, view, &s->blame_complete,
2773 &s->first_displayed_line, &s->last_displayed_line,
2774 &s->selected_line, &s->done, &s->eof, s->path,
2775 s->blamed_commit->id, s->repo);
2778 static const struct got_error *
2779 close_blame_view(struct tog_view *view)
2781 const struct got_error *err = NULL;
2782 struct tog_blame_view_state *s = &view->state.blame;
2784 if (s->blame.thread)
2785 err = stop_blame(&s->blame);
2787 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2788 struct got_object_qid *blamed_commit;
2789 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2790 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2791 got_object_qid_free(blamed_commit);
2794 free(s->path);
2796 return err;
2799 static const struct got_error *
2800 show_blame_view(struct tog_view *view)
2802 const struct got_error *err = NULL;
2803 struct tog_blame_view_state *s = &view->state.blame;
2804 int errcode;
2806 if (s->blame.thread == NULL) {
2807 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2808 &s->blame.thread_args);
2809 if (errcode)
2810 return got_error_set_errno(errcode);
2813 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2814 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2815 s->selected_line, &s->first_displayed_line,
2816 &s->last_displayed_line, &s->eof, view->nlines);
2818 view_vborder(view);
2819 return err;
2822 static const struct got_error *
2823 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2824 struct tog_view **focus_view, struct tog_view *view, int ch)
2826 const struct got_error *err = NULL, *thread_err = NULL;
2827 struct tog_view *diff_view;
2828 struct tog_blame_view_state *s = &view->state.blame;
2829 int begin_x = 0;
2831 switch (ch) {
2832 case 'q':
2833 s->done = 1;
2834 break;
2835 case 'k':
2836 case KEY_UP:
2837 if (s->selected_line > 1)
2838 s->selected_line--;
2839 else if (s->selected_line == 1 &&
2840 s->first_displayed_line > 1)
2841 s->first_displayed_line--;
2842 break;
2843 case KEY_PPAGE:
2844 if (s->first_displayed_line == 1) {
2845 s->selected_line = 1;
2846 break;
2848 if (s->first_displayed_line > view->nlines - 2)
2849 s->first_displayed_line -=
2850 (view->nlines - 2);
2851 else
2852 s->first_displayed_line = 1;
2853 break;
2854 case 'j':
2855 case KEY_DOWN:
2856 if (s->selected_line < view->nlines - 2 &&
2857 s->first_displayed_line +
2858 s->selected_line <= s->blame.nlines)
2859 s->selected_line++;
2860 else if (s->last_displayed_line <
2861 s->blame.nlines)
2862 s->first_displayed_line++;
2863 break;
2864 case 'b':
2865 case 'p': {
2866 struct got_object_id *id = NULL;
2867 id = get_selected_commit_id(s->blame.lines,
2868 s->first_displayed_line, s->selected_line);
2869 if (id == NULL)
2870 break;
2871 if (ch == 'p') {
2872 struct got_commit_object *commit;
2873 struct got_object_qid *pid;
2874 struct got_object_id *blob_id = NULL;
2875 int obj_type;
2876 err = got_object_open_as_commit(&commit,
2877 s->repo, id);
2878 if (err)
2879 break;
2880 pid = SIMPLEQ_FIRST(
2881 got_object_commit_get_parent_ids(commit));
2882 if (pid == NULL) {
2883 got_object_commit_close(commit);
2884 break;
2886 /* Check if path history ends here. */
2887 err = got_object_id_by_path(&blob_id, s->repo,
2888 pid->id, s->path);
2889 if (err) {
2890 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2891 err = NULL;
2892 got_object_commit_close(commit);
2893 break;
2895 err = got_object_get_type(&obj_type, s->repo,
2896 blob_id);
2897 free(blob_id);
2898 /* Can't blame non-blob type objects. */
2899 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2900 got_object_commit_close(commit);
2901 break;
2903 err = got_object_qid_alloc(&s->blamed_commit,
2904 pid->id);
2905 got_object_commit_close(commit);
2906 } else {
2907 if (got_object_id_cmp(id,
2908 s->blamed_commit->id) == 0)
2909 break;
2910 err = got_object_qid_alloc(&s->blamed_commit,
2911 id);
2913 if (err)
2914 break;
2915 s->done = 1;
2916 thread_err = stop_blame(&s->blame);
2917 s->done = 0;
2918 if (thread_err)
2919 break;
2920 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2921 s->blamed_commit, entry);
2922 err = run_blame(&s->blame, view, &s->blame_complete,
2923 &s->first_displayed_line, &s->last_displayed_line,
2924 &s->selected_line, &s->done, &s->eof,
2925 s->path, s->blamed_commit->id, s->repo);
2926 if (err)
2927 break;
2928 break;
2930 case 'B': {
2931 struct got_object_qid *first;
2932 first = SIMPLEQ_FIRST(&s->blamed_commits);
2933 if (!got_object_id_cmp(first->id, s->commit_id))
2934 break;
2935 s->done = 1;
2936 thread_err = stop_blame(&s->blame);
2937 s->done = 0;
2938 if (thread_err)
2939 break;
2940 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2941 got_object_qid_free(s->blamed_commit);
2942 s->blamed_commit =
2943 SIMPLEQ_FIRST(&s->blamed_commits);
2944 err = run_blame(&s->blame, view, &s->blame_complete,
2945 &s->first_displayed_line, &s->last_displayed_line,
2946 &s->selected_line, &s->done, &s->eof, s->path,
2947 s->blamed_commit->id, s->repo);
2948 if (err)
2949 break;
2950 break;
2952 case KEY_ENTER:
2953 case '\r': {
2954 struct got_object_id *id = NULL;
2955 struct got_object_qid *pid;
2956 struct got_commit_object *commit = NULL;
2957 id = get_selected_commit_id(s->blame.lines,
2958 s->first_displayed_line, s->selected_line);
2959 if (id == NULL)
2960 break;
2961 err = got_object_open_as_commit(&commit, s->repo, id);
2962 if (err)
2963 break;
2964 pid = SIMPLEQ_FIRST(
2965 got_object_commit_get_parent_ids(commit));
2966 if (view_is_parent_view(view))
2967 begin_x = view_split_begin_x(view->begin_x);
2968 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2969 if (diff_view == NULL) {
2970 got_object_commit_close(commit);
2971 err = got_error_from_errno();
2972 break;
2974 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2975 id, NULL, s->refs, s->repo);
2976 got_object_commit_close(commit);
2977 if (err) {
2978 view_close(diff_view);
2979 break;
2981 if (view_is_parent_view(view)) {
2982 err = view_close_child(view);
2983 if (err)
2984 break;
2985 err = view_set_child(view, diff_view);
2986 if (err) {
2987 view_close(diff_view);
2988 break;
2990 *focus_view = diff_view;
2991 view->child_focussed = 1;
2992 } else
2993 *new_view = diff_view;
2994 if (err)
2995 break;
2996 break;
2998 case KEY_NPAGE:
2999 case ' ':
3000 if (s->last_displayed_line >= s->blame.nlines &&
3001 s->selected_line < view->nlines - 2) {
3002 s->selected_line = MIN(s->blame.nlines,
3003 view->nlines - 2);
3004 break;
3006 if (s->last_displayed_line + view->nlines - 2
3007 <= s->blame.nlines)
3008 s->first_displayed_line +=
3009 view->nlines - 2;
3010 else
3011 s->first_displayed_line =
3012 s->blame.nlines -
3013 (view->nlines - 3);
3014 break;
3015 case KEY_RESIZE:
3016 if (s->selected_line > view->nlines - 2) {
3017 s->selected_line = MIN(s->blame.nlines,
3018 view->nlines - 2);
3020 break;
3021 default:
3022 break;
3024 return thread_err ? thread_err : err;
3027 static const struct got_error *
3028 cmd_blame(int argc, char *argv[])
3030 const struct got_error *error;
3031 struct got_repository *repo = NULL;
3032 struct got_reflist_head refs;
3033 struct got_worktree *worktree = NULL;
3034 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3035 struct got_object_id *commit_id = NULL;
3036 char *commit_id_str = NULL;
3037 int ch;
3038 struct tog_view *view;
3040 #ifndef PROFILE
3041 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3042 NULL) == -1)
3043 err(1, "pledge");
3044 #endif
3046 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3047 switch (ch) {
3048 case 'c':
3049 commit_id_str = optarg;
3050 break;
3051 case 'r':
3052 repo_path = realpath(optarg, NULL);
3053 if (repo_path == NULL)
3054 err(1, "-r option");
3055 break;
3056 default:
3057 usage();
3058 /* NOTREACHED */
3062 argc -= optind;
3063 argv += optind;
3065 if (argc == 1)
3066 path = argv[0];
3067 else
3068 usage_blame();
3070 cwd = getcwd(NULL, 0);
3071 if (cwd == NULL) {
3072 error = got_error_from_errno();
3073 goto done;
3075 if (repo_path == NULL) {
3076 error = got_worktree_open(&worktree, cwd);
3077 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3078 goto done;
3079 else
3080 error = NULL;
3081 if (worktree) {
3082 repo_path =
3083 strdup(got_worktree_get_repo_path(worktree));
3084 if (repo_path == NULL)
3085 error = got_error_from_errno();
3086 if (error)
3087 goto done;
3088 } else {
3089 repo_path = strdup(cwd);
3090 if (repo_path == NULL) {
3091 error = got_error_from_errno();
3092 goto done;
3097 init_curses();
3099 error = apply_unveil(repo_path, NULL);
3100 if (error)
3101 goto done;
3103 error = got_repo_open(&repo, repo_path);
3104 if (error != NULL)
3105 goto done;
3107 if (worktree) {
3108 const char *prefix = got_worktree_get_path_prefix(worktree);
3109 char *p, *worktree_subdir = cwd +
3110 strlen(got_worktree_get_root_path(worktree));
3111 if (asprintf(&p, "%s%s%s%s%s",
3112 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3113 worktree_subdir, worktree_subdir[0] ? "/" : "",
3114 path) == -1) {
3115 error = got_error_from_errno();
3116 goto done;
3118 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3119 free(p);
3120 } else {
3121 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3123 if (error)
3124 goto done;
3126 if (commit_id_str == NULL) {
3127 struct got_reference *head_ref;
3128 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3129 if (error != NULL)
3130 goto done;
3131 error = got_ref_resolve(&commit_id, repo, head_ref);
3132 got_ref_close(head_ref);
3133 } else {
3134 error = got_object_resolve_id_str(&commit_id, repo,
3135 commit_id_str);
3137 if (error != NULL)
3138 goto done;
3140 SIMPLEQ_INIT(&refs);
3141 error = got_ref_list(&refs, repo);
3142 if (error)
3143 goto done;
3145 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3146 if (view == NULL) {
3147 error = got_error_from_errno();
3148 goto done;
3150 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3151 if (error)
3152 goto done;
3153 error = view_loop(view);
3154 done:
3155 free(repo_path);
3156 free(cwd);
3157 free(commit_id);
3158 if (worktree)
3159 got_worktree_close(worktree);
3160 if (repo)
3161 got_repo_close(repo);
3162 return error;
3165 static const struct got_error *
3166 draw_tree_entries(struct tog_view *view,
3167 struct got_tree_entry **first_displayed_entry,
3168 struct got_tree_entry **last_displayed_entry,
3169 struct got_tree_entry **selected_entry, int *ndisplayed,
3170 const char *label, int show_ids, const char *parent_path,
3171 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3173 const struct got_error *err = NULL;
3174 struct got_tree_entry *te;
3175 wchar_t *wline;
3176 int width, n;
3178 *ndisplayed = 0;
3180 werase(view->window);
3182 if (limit == 0)
3183 return NULL;
3185 err = format_line(&wline, &width, label, view->ncols);
3186 if (err)
3187 return err;
3188 if (view_needs_focus_indication(view))
3189 wstandout(view->window);
3190 waddwstr(view->window, wline);
3191 if (view_needs_focus_indication(view))
3192 wstandend(view->window);
3193 free(wline);
3194 wline = NULL;
3195 if (width < view->ncols)
3196 waddch(view->window, '\n');
3197 if (--limit <= 0)
3198 return NULL;
3199 err = format_line(&wline, &width, parent_path, view->ncols);
3200 if (err)
3201 return err;
3202 waddwstr(view->window, wline);
3203 free(wline);
3204 wline = NULL;
3205 if (width < view->ncols)
3206 waddch(view->window, '\n');
3207 if (--limit <= 0)
3208 return NULL;
3209 waddch(view->window, '\n');
3210 if (--limit <= 0)
3211 return NULL;
3213 te = SIMPLEQ_FIRST(&entries->head);
3214 if (*first_displayed_entry == NULL) {
3215 if (selected == 0) {
3216 if (view->focussed)
3217 wstandout(view->window);
3218 *selected_entry = NULL;
3220 waddstr(view->window, " ..\n"); /* parent directory */
3221 if (selected == 0 && view->focussed)
3222 wstandend(view->window);
3223 (*ndisplayed)++;
3224 if (--limit <= 0)
3225 return NULL;
3226 n = 1;
3227 } else {
3228 n = 0;
3229 while (te != *first_displayed_entry)
3230 te = SIMPLEQ_NEXT(te, entry);
3233 while (te) {
3234 char *line = NULL, *id_str = NULL;
3236 if (show_ids) {
3237 err = got_object_id_str(&id_str, te->id);
3238 if (err)
3239 return got_error_from_errno();
3241 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3242 te->name, S_ISDIR(te->mode) ? "/" :
3243 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3244 free(id_str);
3245 return got_error_from_errno();
3247 free(id_str);
3248 err = format_line(&wline, &width, line, view->ncols);
3249 if (err) {
3250 free(line);
3251 break;
3253 if (n == selected) {
3254 if (view->focussed)
3255 wstandout(view->window);
3256 *selected_entry = te;
3258 waddwstr(view->window, wline);
3259 if (width < view->ncols)
3260 waddch(view->window, '\n');
3261 if (n == selected && view->focussed)
3262 wstandend(view->window);
3263 free(line);
3264 free(wline);
3265 wline = NULL;
3266 n++;
3267 (*ndisplayed)++;
3268 *last_displayed_entry = te;
3269 if (--limit <= 0)
3270 break;
3271 te = SIMPLEQ_NEXT(te, entry);
3274 return err;
3277 static void
3278 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3279 const struct got_tree_entries *entries, int isroot)
3281 struct got_tree_entry *te, *prev;
3282 int i;
3284 if (*first_displayed_entry == NULL)
3285 return;
3287 te = SIMPLEQ_FIRST(&entries->head);
3288 if (*first_displayed_entry == te) {
3289 if (!isroot)
3290 *first_displayed_entry = NULL;
3291 return;
3294 /* XXX this is stupid... switch to TAILQ? */
3295 for (i = 0; i < maxscroll; i++) {
3296 while (te != *first_displayed_entry) {
3297 prev = te;
3298 te = SIMPLEQ_NEXT(te, entry);
3300 *first_displayed_entry = prev;
3301 te = SIMPLEQ_FIRST(&entries->head);
3303 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3304 *first_displayed_entry = NULL;
3307 static int
3308 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3309 struct got_tree_entry *last_displayed_entry,
3310 const struct got_tree_entries *entries)
3312 struct got_tree_entry *next, *last;
3313 int n = 0;
3315 if (*first_displayed_entry)
3316 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3317 else
3318 next = SIMPLEQ_FIRST(&entries->head);
3319 last = last_displayed_entry;
3320 while (next && last && n++ < maxscroll) {
3321 last = SIMPLEQ_NEXT(last, entry);
3322 if (last) {
3323 *first_displayed_entry = next;
3324 next = SIMPLEQ_NEXT(next, entry);
3327 return n;
3330 static const struct got_error *
3331 tree_entry_path(char **path, struct tog_parent_trees *parents,
3332 struct got_tree_entry *te)
3334 const struct got_error *err = NULL;
3335 struct tog_parent_tree *pt;
3336 size_t len = 2; /* for leading slash and NUL */
3338 TAILQ_FOREACH(pt, parents, entry)
3339 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3340 if (te)
3341 len += strlen(te->name);
3343 *path = calloc(1, len);
3344 if (path == NULL)
3345 return got_error_from_errno();
3347 (*path)[0] = '/';
3348 pt = TAILQ_LAST(parents, tog_parent_trees);
3349 while (pt) {
3350 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3351 err = got_error(GOT_ERR_NO_SPACE);
3352 goto done;
3354 if (strlcat(*path, "/", len) >= len) {
3355 err = got_error(GOT_ERR_NO_SPACE);
3356 goto done;
3358 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3360 if (te) {
3361 if (strlcat(*path, te->name, len) >= len) {
3362 err = got_error(GOT_ERR_NO_SPACE);
3363 goto done;
3366 done:
3367 if (err) {
3368 free(*path);
3369 *path = NULL;
3371 return err;
3374 static const struct got_error *
3375 blame_tree_entry(struct tog_view **new_view, int begin_x,
3376 struct got_tree_entry *te, struct tog_parent_trees *parents,
3377 struct got_object_id *commit_id, struct got_reflist_head *refs,
3378 struct got_repository *repo)
3380 const struct got_error *err = NULL;
3381 char *path;
3382 struct tog_view *blame_view;
3384 err = tree_entry_path(&path, parents, te);
3385 if (err)
3386 return err;
3388 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3389 if (blame_view == NULL)
3390 return got_error_from_errno();
3392 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3393 if (err) {
3394 view_close(blame_view);
3395 free(path);
3396 } else
3397 *new_view = blame_view;
3398 return err;
3401 static const struct got_error *
3402 log_tree_entry(struct tog_view **new_view, int begin_x,
3403 struct got_tree_entry *te, struct tog_parent_trees *parents,
3404 struct got_object_id *commit_id, struct got_reflist_head *refs,
3405 struct got_repository *repo)
3407 struct tog_view *log_view;
3408 const struct got_error *err = NULL;
3409 char *path;
3411 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3412 if (log_view == NULL)
3413 return got_error_from_errno();
3415 err = tree_entry_path(&path, parents, te);
3416 if (err)
3417 return err;
3419 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3420 if (err)
3421 view_close(log_view);
3422 else
3423 *new_view = log_view;
3424 free(path);
3425 return err;
3428 static const struct got_error *
3429 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3430 struct got_object_id *commit_id, struct got_reflist_head *refs,
3431 struct got_repository *repo)
3433 const struct got_error *err = NULL;
3434 char *commit_id_str = NULL;
3435 struct tog_tree_view_state *s = &view->state.tree;
3437 TAILQ_INIT(&s->parents);
3439 err = got_object_id_str(&commit_id_str, commit_id);
3440 if (err != NULL)
3441 goto done;
3443 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3444 err = got_error_from_errno();
3445 goto done;
3448 s->root = s->tree = root;
3449 s->entries = got_object_tree_get_entries(root);
3450 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3451 s->commit_id = got_object_id_dup(commit_id);
3452 if (s->commit_id == NULL) {
3453 err = got_error_from_errno();
3454 goto done;
3456 s->refs = refs;
3457 s->repo = repo;
3459 view->show = show_tree_view;
3460 view->input = input_tree_view;
3461 view->close = close_tree_view;
3462 done:
3463 free(commit_id_str);
3464 if (err) {
3465 free(s->tree_label);
3466 s->tree_label = NULL;
3468 return err;
3471 static const struct got_error *
3472 close_tree_view(struct tog_view *view)
3474 struct tog_tree_view_state *s = &view->state.tree;
3476 free(s->tree_label);
3477 s->tree_label = NULL;
3478 free(s->commit_id);
3479 s->commit_id = NULL;
3480 while (!TAILQ_EMPTY(&s->parents)) {
3481 struct tog_parent_tree *parent;
3482 parent = TAILQ_FIRST(&s->parents);
3483 TAILQ_REMOVE(&s->parents, parent, entry);
3484 free(parent);
3487 if (s->tree != s->root)
3488 got_object_tree_close(s->tree);
3489 got_object_tree_close(s->root);
3491 return NULL;
3494 static const struct got_error *
3495 show_tree_view(struct tog_view *view)
3497 const struct got_error *err = NULL;
3498 struct tog_tree_view_state *s = &view->state.tree;
3499 char *parent_path;
3501 err = tree_entry_path(&parent_path, &s->parents, NULL);
3502 if (err)
3503 return err;
3505 err = draw_tree_entries(view, &s->first_displayed_entry,
3506 &s->last_displayed_entry, &s->selected_entry,
3507 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3508 s->entries, s->selected, view->nlines, s->tree == s->root);
3509 free(parent_path);
3511 view_vborder(view);
3512 return err;
3515 static const struct got_error *
3516 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3517 struct tog_view **focus_view, struct tog_view *view, int ch)
3519 const struct got_error *err = NULL;
3520 struct tog_tree_view_state *s = &view->state.tree;
3521 struct tog_view *log_view;
3522 int begin_x = 0, nscrolled;
3524 switch (ch) {
3525 case 'i':
3526 s->show_ids = !s->show_ids;
3527 break;
3528 case 'l':
3529 if (!s->selected_entry)
3530 break;
3531 if (view_is_parent_view(view))
3532 begin_x = view_split_begin_x(view->begin_x);
3533 err = log_tree_entry(&log_view, begin_x,
3534 s->selected_entry, &s->parents,
3535 s->commit_id, s->refs, s->repo);
3536 if (view_is_parent_view(view)) {
3537 err = view_close_child(view);
3538 if (err)
3539 return err;
3540 err = view_set_child(view, log_view);
3541 if (err) {
3542 view_close(log_view);
3543 break;
3545 *focus_view = log_view;
3546 view->child_focussed = 1;
3547 } else
3548 *new_view = log_view;
3549 break;
3550 case 'k':
3551 case KEY_UP:
3552 if (s->selected > 0) {
3553 s->selected--;
3554 if (s->selected == 0)
3555 break;
3557 if (s->selected > 0)
3558 break;
3559 tree_scroll_up(&s->first_displayed_entry, 1,
3560 s->entries, s->tree == s->root);
3561 break;
3562 case KEY_PPAGE:
3563 tree_scroll_up(&s->first_displayed_entry,
3564 MAX(0, view->nlines - 4 - s->selected), s->entries,
3565 s->tree == s->root);
3566 s->selected = 0;
3567 if (SIMPLEQ_FIRST(&s->entries->head) ==
3568 s->first_displayed_entry && s->tree != s->root)
3569 s->first_displayed_entry = NULL;
3570 break;
3571 case 'j':
3572 case KEY_DOWN:
3573 if (s->selected < s->ndisplayed - 1) {
3574 s->selected++;
3575 break;
3577 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3578 == NULL) {
3579 /* can't scroll any further */
3580 break;
3582 tree_scroll_down(&s->first_displayed_entry, 1,
3583 s->last_displayed_entry, s->entries);
3584 break;
3585 case KEY_NPAGE:
3586 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3587 == NULL) {
3588 /* can't scroll any further; move cursor down */
3589 if (s->selected < s->ndisplayed - 1)
3590 s->selected = s->ndisplayed - 1;
3591 break;
3593 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3594 view->nlines, s->last_displayed_entry, s->entries);
3595 if (nscrolled < view->nlines) {
3596 int ndisplayed = 0;
3597 struct got_tree_entry *te;
3598 te = s->first_displayed_entry;
3599 do {
3600 ndisplayed++;
3601 te = SIMPLEQ_NEXT(te, entry);
3602 } while (te);
3603 s->selected = ndisplayed - 1;
3605 break;
3606 case KEY_ENTER:
3607 case '\r':
3608 if (s->selected_entry == NULL) {
3609 struct tog_parent_tree *parent;
3610 case KEY_BACKSPACE:
3611 /* user selected '..' */
3612 if (s->tree == s->root)
3613 break;
3614 parent = TAILQ_FIRST(&s->parents);
3615 TAILQ_REMOVE(&s->parents, parent,
3616 entry);
3617 got_object_tree_close(s->tree);
3618 s->tree = parent->tree;
3619 s->entries =
3620 got_object_tree_get_entries(s->tree);
3621 s->first_displayed_entry =
3622 parent->first_displayed_entry;
3623 s->selected_entry =
3624 parent->selected_entry;
3625 s->selected = parent->selected;
3626 free(parent);
3627 } else if (S_ISDIR(s->selected_entry->mode)) {
3628 struct tog_parent_tree *parent;
3629 struct got_tree_object *child;
3630 err = got_object_open_as_tree(&child,
3631 s->repo, s->selected_entry->id);
3632 if (err)
3633 break;
3634 parent = calloc(1, sizeof(*parent));
3635 if (parent == NULL) {
3636 err = got_error_from_errno();
3637 break;
3639 parent->tree = s->tree;
3640 parent->first_displayed_entry =
3641 s->first_displayed_entry;
3642 parent->selected_entry = s->selected_entry;
3643 parent->selected = s->selected;
3644 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3645 s->tree = child;
3646 s->entries =
3647 got_object_tree_get_entries(s->tree);
3648 s->selected = 0;
3649 s->first_displayed_entry = NULL;
3650 } else if (S_ISREG(s->selected_entry->mode)) {
3651 struct tog_view *blame_view;
3652 int begin_x = view_is_parent_view(view) ?
3653 view_split_begin_x(view->begin_x) : 0;
3655 err = blame_tree_entry(&blame_view, begin_x,
3656 s->selected_entry, &s->parents,
3657 s->commit_id, s->refs, s->repo);
3658 if (err)
3659 break;
3660 if (view_is_parent_view(view)) {
3661 err = view_close_child(view);
3662 if (err)
3663 return err;
3664 err = view_set_child(view, blame_view);
3665 if (err) {
3666 view_close(blame_view);
3667 break;
3669 *focus_view = blame_view;
3670 view->child_focussed = 1;
3671 } else
3672 *new_view = blame_view;
3674 break;
3675 case KEY_RESIZE:
3676 if (s->selected > view->nlines)
3677 s->selected = s->ndisplayed - 1;
3678 break;
3679 default:
3680 break;
3683 return err;
3686 __dead static void
3687 usage_tree(void)
3689 endwin();
3690 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3691 getprogname());
3692 exit(1);
3695 static const struct got_error *
3696 cmd_tree(int argc, char *argv[])
3698 const struct got_error *error;
3699 struct got_repository *repo = NULL;
3700 struct got_reflist_head refs;
3701 char *repo_path = NULL;
3702 struct got_object_id *commit_id = NULL;
3703 char *commit_id_arg = NULL;
3704 struct got_commit_object *commit = NULL;
3705 struct got_tree_object *tree = NULL;
3706 int ch;
3707 struct tog_view *view;
3709 #ifndef PROFILE
3710 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3711 NULL) == -1)
3712 err(1, "pledge");
3713 #endif
3715 while ((ch = getopt(argc, argv, "c:")) != -1) {
3716 switch (ch) {
3717 case 'c':
3718 commit_id_arg = optarg;
3719 break;
3720 default:
3721 usage();
3722 /* NOTREACHED */
3726 argc -= optind;
3727 argv += optind;
3729 if (argc == 0) {
3730 struct got_worktree *worktree;
3731 char *cwd = getcwd(NULL, 0);
3732 if (cwd == NULL)
3733 return got_error_from_errno();
3734 error = got_worktree_open(&worktree, cwd);
3735 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3736 goto done;
3737 if (worktree) {
3738 free(cwd);
3739 repo_path =
3740 strdup(got_worktree_get_repo_path(worktree));
3741 got_worktree_close(worktree);
3742 } else
3743 repo_path = cwd;
3744 if (repo_path == NULL) {
3745 error = got_error_from_errno();
3746 goto done;
3748 } else if (argc == 1) {
3749 repo_path = realpath(argv[0], NULL);
3750 if (repo_path == NULL)
3751 return got_error_from_errno();
3752 } else
3753 usage_log();
3755 init_curses();
3757 error = apply_unveil(repo_path, NULL);
3758 if (error)
3759 goto done;
3761 error = got_repo_open(&repo, repo_path);
3762 if (error != NULL)
3763 goto done;
3765 if (commit_id_arg == NULL)
3766 error = get_head_commit_id(&commit_id, repo);
3767 else
3768 error = got_object_resolve_id_str(&commit_id, repo,
3769 commit_id_arg);
3770 if (error != NULL)
3771 goto done;
3773 error = got_object_open_as_commit(&commit, repo, commit_id);
3774 if (error != NULL)
3775 goto done;
3777 error = got_object_open_as_tree(&tree, repo,
3778 got_object_commit_get_tree_id(commit));
3779 if (error != NULL)
3780 goto done;
3782 SIMPLEQ_INIT(&refs);
3783 error = got_ref_list(&refs, repo);
3784 if (error)
3785 goto done;
3787 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3788 if (view == NULL) {
3789 error = got_error_from_errno();
3790 goto done;
3792 error = open_tree_view(view, tree, commit_id, &refs, repo);
3793 if (error)
3794 goto done;
3795 error = view_loop(view);
3796 done:
3797 free(repo_path);
3798 free(commit_id);
3799 if (commit)
3800 got_object_commit_close(commit);
3801 if (tree)
3802 got_object_tree_close(tree);
3803 if (repo)
3804 got_repo_close(repo);
3805 return error;
3808 __dead static void
3809 usage(void)
3811 int i;
3813 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3814 "Available commands:\n", getprogname());
3815 for (i = 0; i < nitems(tog_commands); i++) {
3816 struct tog_cmd *cmd = &tog_commands[i];
3817 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3819 exit(1);
3822 static char **
3823 make_argv(const char *arg0, const char *arg1)
3825 char **argv;
3826 int argc = (arg1 == NULL ? 1 : 2);
3828 argv = calloc(argc, sizeof(char *));
3829 if (argv == NULL)
3830 err(1, "calloc");
3831 argv[0] = strdup(arg0);
3832 if (argv[0] == NULL)
3833 err(1, "calloc");
3834 if (arg1) {
3835 argv[1] = strdup(arg1);
3836 if (argv[1] == NULL)
3837 err(1, "calloc");
3840 return argv;
3843 int
3844 main(int argc, char *argv[])
3846 const struct got_error *error = NULL;
3847 struct tog_cmd *cmd = NULL;
3848 int ch, hflag = 0;
3849 char **cmd_argv = NULL;
3851 setlocale(LC_CTYPE, "");
3853 while ((ch = getopt(argc, argv, "h")) != -1) {
3854 switch (ch) {
3855 case 'h':
3856 hflag = 1;
3857 break;
3858 default:
3859 usage();
3860 /* NOTREACHED */
3864 argc -= optind;
3865 argv += optind;
3866 optind = 0;
3867 optreset = 1;
3869 if (argc == 0) {
3870 if (hflag)
3871 usage();
3872 /* Build an argument vector which runs a default command. */
3873 cmd = &tog_commands[0];
3874 cmd_argv = make_argv(cmd->name, NULL);
3875 argc = 1;
3876 } else {
3877 int i;
3879 /* Did the user specific a command? */
3880 for (i = 0; i < nitems(tog_commands); i++) {
3881 if (strncmp(tog_commands[i].name, argv[0],
3882 strlen(argv[0])) == 0) {
3883 cmd = &tog_commands[i];
3884 if (hflag)
3885 tog_commands[i].cmd_usage();
3886 break;
3889 if (cmd == NULL) {
3890 /* Did the user specify a repository? */
3891 char *repo_path = realpath(argv[0], NULL);
3892 if (repo_path) {
3893 struct got_repository *repo;
3894 error = got_repo_open(&repo, repo_path);
3895 if (error == NULL)
3896 got_repo_close(repo);
3897 } else
3898 error = got_error_from_errno();
3899 if (error) {
3900 if (hflag) {
3901 fprintf(stderr, "%s: '%s' is not a "
3902 "known command\n", getprogname(),
3903 argv[0]);
3904 usage();
3906 fprintf(stderr, "%s: '%s' is neither a known "
3907 "command nor a path to a repository\n",
3908 getprogname(), argv[0]);
3909 free(repo_path);
3910 return 1;
3912 cmd = &tog_commands[0];
3913 cmd_argv = make_argv(cmd->name, repo_path);
3914 argc = 2;
3915 free(repo_path);
3919 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3920 if (error)
3921 goto done;
3922 done:
3923 endwin();
3924 free(cmd_argv);
3925 if (error)
3926 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3927 return 0;