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)++;
1264 if (nscrolled == 0) {
1265 /* Redraw screen for "loading..." message. */
1266 err = show_log_view(view);
1267 if (err)
1268 return err;
1269 update_panels();
1270 doupdate();
1273 while (*commits_needed > 0) {
1274 int errcode;
1275 errcode = pthread_cond_signal(need_commits);
1276 if (errcode)
1277 return got_error_set_errno(errcode);
1278 errcode = pthread_mutex_unlock(&tog_mutex);
1279 if (errcode)
1280 return got_error_set_errno(errcode);
1281 pthread_yield();
1282 errcode = pthread_mutex_lock(&tog_mutex);
1283 if (errcode)
1284 return got_error_set_errno(errcode);
1287 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1288 if (pentry == NULL)
1289 break;
1291 *last_displayed_entry = pentry;
1293 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1294 if (pentry == NULL)
1295 break;
1296 *first_displayed_entry = pentry;
1297 } while (++nscrolled < maxscroll);
1299 return err;
1302 static const struct got_error *
1303 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1304 struct got_commit_object *commit, struct got_object_id *commit_id,
1305 struct tog_view *log_view, struct got_reflist_head *refs,
1306 struct got_repository *repo)
1308 const struct got_error *err;
1309 struct got_object_qid *parent_id;
1310 struct tog_view *diff_view;
1312 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1313 if (diff_view == NULL)
1314 return got_error_from_errno();
1316 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1317 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1318 commit_id, log_view, refs, repo);
1319 if (err == NULL)
1320 *new_view = diff_view;
1321 return err;
1324 static const struct got_error *
1325 browse_commit(struct tog_view **new_view, int begin_x,
1326 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1327 struct got_repository *repo)
1329 const struct got_error *err = NULL;
1330 struct got_tree_object *tree;
1331 struct tog_view *tree_view;
1333 err = got_object_open_as_tree(&tree, repo,
1334 got_object_commit_get_tree_id(entry->commit));
1335 if (err)
1336 return err;
1338 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1339 if (tree_view == NULL)
1340 return got_error_from_errno();
1342 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1343 if (err)
1344 got_object_tree_close(tree);
1345 else
1346 *new_view = tree_view;
1347 return err;
1350 static void *
1351 log_thread(void *arg)
1353 const struct got_error *err = NULL;
1354 int errcode = 0;
1355 struct tog_log_thread_args *a = arg;
1356 int done = 0;
1358 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1359 if (err)
1360 return (void *)err;
1362 while (!done && !err) {
1363 err = queue_commits(a->graph, a->commits, 1, a->repo,
1364 a->in_repo_path);
1365 if (err) {
1366 if (err->code != GOT_ERR_ITER_COMPLETED)
1367 return (void *)err;
1368 err = NULL;
1369 done = 1;
1370 } else if (a->commits_needed > 0)
1371 a->commits_needed--;
1373 errcode = pthread_mutex_lock(&tog_mutex);
1374 if (errcode)
1375 return (void *)got_error_set_errno(errcode);
1377 if (done)
1378 a->log_complete = 1;
1379 else if (*a->quit) {
1380 done = 1;
1381 a->log_complete = 1;
1382 } else if (*a->first_displayed_entry == NULL) {
1383 *a->first_displayed_entry =
1384 TAILQ_FIRST(&a->commits->head);
1385 *a->selected_entry = *a->first_displayed_entry;
1388 if (done)
1389 a->commits_needed = 0;
1390 else if (a->commits_needed == 0) {
1391 errcode = pthread_cond_wait(&a->need_commits,
1392 &tog_mutex);
1393 if (errcode)
1394 err = got_error_set_errno(errcode);
1397 errcode = pthread_mutex_unlock(&tog_mutex);
1398 if (errcode && err == NULL)
1399 err = got_error_set_errno(errcode);
1401 return (void *)err;
1404 static const struct got_error *
1405 stop_log_thread(struct tog_log_view_state *s)
1407 const struct got_error *err = NULL;
1408 int errcode;
1410 if (s->thread) {
1411 s->quit = 1;
1412 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1413 if (errcode)
1414 return got_error_set_errno(errcode);
1415 errcode = pthread_mutex_unlock(&tog_mutex);
1416 if (errcode)
1417 return got_error_set_errno(errcode);
1418 errcode = pthread_join(s->thread, (void **)&err);
1419 if (errcode)
1420 return got_error_set_errno(errcode);
1421 errcode = pthread_mutex_lock(&tog_mutex);
1422 if (errcode)
1423 return got_error_set_errno(errcode);
1424 s->thread = NULL;
1427 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1428 if (errcode && err == NULL)
1429 err = got_error_set_errno(errcode);
1431 if (s->thread_args.repo) {
1432 got_repo_close(s->thread_args.repo);
1433 s->thread_args.repo = NULL;
1436 if (s->thread_args.graph) {
1437 got_commit_graph_close(s->thread_args.graph);
1438 s->thread_args.graph = NULL;
1441 return err;
1444 static const struct got_error *
1445 close_log_view(struct tog_view *view)
1447 const struct got_error *err = NULL;
1448 struct tog_log_view_state *s = &view->state.log;
1450 err = stop_log_thread(s);
1451 free_commits(&s->commits);
1452 free(s->in_repo_path);
1453 s->in_repo_path = NULL;
1454 free(s->start_id);
1455 s->start_id = NULL;
1456 return err;
1459 static const struct got_error *
1460 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1461 struct got_reflist_head *refs, struct got_repository *repo,
1462 const char *path, int check_disk)
1464 const struct got_error *err = NULL;
1465 struct tog_log_view_state *s = &view->state.log;
1466 struct got_repository *thread_repo = NULL;
1467 struct got_commit_graph *thread_graph = NULL;
1468 int errcode;
1470 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1471 if (err != NULL)
1472 goto done;
1474 /* The commit queue only contains commits being displayed. */
1475 TAILQ_INIT(&s->commits.head);
1476 s->commits.ncommits = 0;
1478 s->refs = refs;
1479 s->repo = repo;
1480 s->start_id = got_object_id_dup(start_id);
1481 if (s->start_id == NULL) {
1482 err = got_error_from_errno();
1483 goto done;
1486 view->show = show_log_view;
1487 view->input = input_log_view;
1488 view->close = close_log_view;
1490 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1491 if (err)
1492 goto done;
1493 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1494 0, thread_repo);
1495 if (err)
1496 goto done;
1498 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1499 if (errcode) {
1500 err = got_error_set_errno(errcode);
1501 goto done;
1504 s->thread_args.commits_needed = view->nlines;
1505 s->thread_args.graph = thread_graph;
1506 s->thread_args.commits = &s->commits;
1507 s->thread_args.in_repo_path = s->in_repo_path;
1508 s->thread_args.start_id = s->start_id;
1509 s->thread_args.repo = thread_repo;
1510 s->thread_args.log_complete = 0;
1511 s->thread_args.quit = &s->quit;
1512 s->thread_args.view = view;
1513 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1514 s->thread_args.selected_entry = &s->selected_entry;
1515 done:
1516 if (err)
1517 close_log_view(view);
1518 return err;
1521 static const struct got_error *
1522 show_log_view(struct tog_view *view)
1524 struct tog_log_view_state *s = &view->state.log;
1526 if (s->thread == NULL) {
1527 int errcode = pthread_create(&s->thread, NULL, log_thread,
1528 &s->thread_args);
1529 if (errcode)
1530 return got_error_set_errno(errcode);
1533 return draw_commits(view, &s->last_displayed_entry,
1534 &s->selected_entry, s->first_displayed_entry,
1535 &s->commits, s->selected, view->nlines, s->refs,
1536 s->in_repo_path, s->thread_args.commits_needed);
1539 static const struct got_error *
1540 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1541 struct tog_view **focus_view, struct tog_view *view, int ch)
1543 const struct got_error *err = NULL;
1544 struct tog_log_view_state *s = &view->state.log;
1545 char *parent_path;
1546 struct tog_view *diff_view = NULL, *tree_view = NULL;
1547 int begin_x = 0;
1549 switch (ch) {
1550 case 'q':
1551 s->quit = 1;
1552 break;
1553 case 'k':
1554 case KEY_UP:
1555 case '<':
1556 case ',':
1557 if (s->first_displayed_entry == NULL)
1558 break;
1559 if (s->selected > 0)
1560 s->selected--;
1561 if (s->selected > 0)
1562 break;
1563 scroll_up(&s->first_displayed_entry, 1,
1564 &s->commits);
1565 break;
1566 case KEY_PPAGE:
1567 if (s->first_displayed_entry == NULL)
1568 break;
1569 if (TAILQ_FIRST(&s->commits.head) ==
1570 s->first_displayed_entry) {
1571 s->selected = 0;
1572 break;
1574 scroll_up(&s->first_displayed_entry,
1575 view->nlines, &s->commits);
1576 break;
1577 case 'j':
1578 case KEY_DOWN:
1579 case '>':
1580 case '.':
1581 if (s->first_displayed_entry == NULL)
1582 break;
1583 if (s->selected < MIN(view->nlines - 2,
1584 s->commits.ncommits - 1)) {
1585 s->selected++;
1586 break;
1588 err = scroll_down(view, &s->first_displayed_entry, 1,
1589 &s->last_displayed_entry, &s->commits,
1590 &s->thread_args.log_complete,
1591 &s->thread_args.commits_needed,
1592 &s->thread_args.need_commits);
1593 break;
1594 case KEY_NPAGE: {
1595 struct commit_queue_entry *first;
1596 first = s->first_displayed_entry;
1597 if (first == NULL)
1598 break;
1599 err = scroll_down(view, &s->first_displayed_entry,
1600 view->nlines, &s->last_displayed_entry,
1601 &s->commits, &s->thread_args.log_complete,
1602 &s->thread_args.commits_needed,
1603 &s->thread_args.need_commits);
1604 if (first == s->first_displayed_entry &&
1605 s->selected < MIN(view->nlines - 2,
1606 s->commits.ncommits - 1)) {
1607 /* can't scroll further down */
1608 s->selected = MIN(view->nlines - 2,
1609 s->commits.ncommits - 1);
1611 err = NULL;
1612 break;
1614 case KEY_RESIZE:
1615 if (s->selected > view->nlines - 2)
1616 s->selected = view->nlines - 2;
1617 if (s->selected > s->commits.ncommits - 1)
1618 s->selected = s->commits.ncommits - 1;
1619 break;
1620 case KEY_ENTER:
1621 case '\r':
1622 if (s->selected_entry == NULL)
1623 break;
1624 if (view_is_parent_view(view))
1625 begin_x = view_split_begin_x(view->begin_x);
1626 err = open_diff_view_for_commit(&diff_view, begin_x,
1627 s->selected_entry->commit, s->selected_entry->id,
1628 view, s->refs, s->repo);
1629 if (err)
1630 break;
1631 if (view_is_parent_view(view)) {
1632 err = view_close_child(view);
1633 if (err)
1634 return err;
1635 err = view_set_child(view, diff_view);
1636 if (err) {
1637 view_close(diff_view);
1638 break;
1640 *focus_view = diff_view;
1641 view->child_focussed = 1;
1642 } else
1643 *new_view = diff_view;
1644 break;
1645 case 't':
1646 if (s->selected_entry == NULL)
1647 break;
1648 if (view_is_parent_view(view))
1649 begin_x = view_split_begin_x(view->begin_x);
1650 err = browse_commit(&tree_view, begin_x,
1651 s->selected_entry, s->refs, s->repo);
1652 if (err)
1653 break;
1654 if (view_is_parent_view(view)) {
1655 err = view_close_child(view);
1656 if (err)
1657 return err;
1658 err = view_set_child(view, tree_view);
1659 if (err) {
1660 view_close(tree_view);
1661 break;
1663 *focus_view = tree_view;
1664 view->child_focussed = 1;
1665 } else
1666 *new_view = tree_view;
1667 break;
1668 case KEY_BACKSPACE:
1669 if (strcmp(s->in_repo_path, "/") == 0)
1670 break;
1671 parent_path = dirname(s->in_repo_path);
1672 if (parent_path && strcmp(parent_path, ".") != 0) {
1673 struct tog_view *lv;
1674 err = stop_log_thread(s);
1675 if (err)
1676 return err;
1677 lv = view_open(view->nlines, view->ncols,
1678 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1679 if (lv == NULL)
1680 return got_error_from_errno();
1681 err = open_log_view(lv, s->start_id, s->refs,
1682 s->repo, parent_path, 0);
1683 if (err)
1684 return err;;
1685 if (view_is_parent_view(view))
1686 *new_view = lv;
1687 else {
1688 view_set_child(view->parent, lv);
1689 *focus_view = lv;
1691 return NULL;
1693 break;
1694 default:
1695 break;
1698 return err;
1701 static const struct got_error *
1702 apply_unveil(const char *repo_path, const char *worktree_path)
1704 const struct got_error *error;
1706 if (repo_path && unveil(repo_path, "r") != 0)
1707 return got_error_from_errno();
1709 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1710 return got_error_from_errno();
1712 if (unveil("/tmp", "rwc") != 0)
1713 return got_error_from_errno();
1715 error = got_privsep_unveil_exec_helpers();
1716 if (error != NULL)
1717 return error;
1719 if (unveil(NULL, NULL) != 0)
1720 return got_error_from_errno();
1722 return NULL;
1725 static void
1726 init_curses(void)
1728 initscr();
1729 cbreak();
1730 halfdelay(1); /* Do fast refresh while initial view is loading. */
1731 noecho();
1732 nonl();
1733 intrflush(stdscr, FALSE);
1734 keypad(stdscr, TRUE);
1735 curs_set(0);
1736 signal(SIGWINCH, tog_sigwinch);
1739 static const struct got_error *
1740 cmd_log(int argc, char *argv[])
1742 const struct got_error *error;
1743 struct got_repository *repo = NULL;
1744 struct got_reflist_head refs;
1745 struct got_object_id *start_id = NULL;
1746 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1747 char *start_commit = NULL;
1748 int ch;
1749 struct tog_view *view;
1751 #ifndef PROFILE
1752 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1753 NULL) == -1)
1754 err(1, "pledge");
1755 #endif
1757 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1758 switch (ch) {
1759 case 'c':
1760 start_commit = optarg;
1761 break;
1762 case 'r':
1763 repo_path = realpath(optarg, NULL);
1764 if (repo_path == NULL)
1765 err(1, "-r option");
1766 break;
1767 default:
1768 usage();
1769 /* NOTREACHED */
1773 argc -= optind;
1774 argv += optind;
1776 if (argc == 0)
1777 path = strdup("");
1778 else if (argc == 1)
1779 path = strdup(argv[0]);
1780 else
1781 usage_log();
1782 if (path == NULL)
1783 return got_error_from_errno();
1785 cwd = getcwd(NULL, 0);
1786 if (cwd == NULL) {
1787 error = got_error_from_errno();
1788 goto done;
1790 if (repo_path == NULL) {
1791 struct got_worktree *worktree;
1792 error = got_worktree_open(&worktree, cwd);
1793 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1794 goto done;
1795 if (worktree) {
1796 repo_path =
1797 strdup(got_worktree_get_repo_path(worktree));
1798 got_worktree_close(worktree);
1799 } else
1800 repo_path = strdup(cwd);
1801 if (repo_path == NULL) {
1802 error = got_error_from_errno();
1803 goto done;
1807 init_curses();
1809 error = apply_unveil(repo_path, NULL);
1810 if (error)
1811 goto done;
1813 error = got_repo_open(&repo, repo_path);
1814 if (error != NULL)
1815 goto done;
1817 if (start_commit == NULL)
1818 error = get_head_commit_id(&start_id, repo);
1819 else
1820 error = got_object_resolve_id_str(&start_id, repo,
1821 start_commit);
1822 if (error != NULL)
1823 goto done;
1825 SIMPLEQ_INIT(&refs);
1826 error = got_ref_list(&refs, repo);
1827 if (error)
1828 goto done;
1830 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1831 if (view == NULL) {
1832 error = got_error_from_errno();
1833 goto done;
1835 error = open_log_view(view, start_id, &refs, repo, path, 1);
1836 if (error)
1837 goto done;
1838 error = view_loop(view);
1839 done:
1840 free(repo_path);
1841 free(cwd);
1842 free(path);
1843 free(start_id);
1844 if (repo)
1845 got_repo_close(repo);
1846 return error;
1849 __dead static void
1850 usage_diff(void)
1852 endwin();
1853 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1854 getprogname());
1855 exit(1);
1858 static char *
1859 parse_next_line(FILE *f, size_t *len)
1861 char *line;
1862 size_t linelen;
1863 size_t lineno;
1864 const char delim[3] = { '\0', '\0', '\0'};
1866 line = fparseln(f, &linelen, &lineno, delim, 0);
1867 if (len)
1868 *len = linelen;
1869 return line;
1872 static const struct got_error *
1873 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1874 int *last_displayed_line, int *eof, int max_lines,
1875 char * header)
1877 const struct got_error *err;
1878 int nlines = 0, nprinted = 0;
1879 char *line;
1880 size_t len;
1881 wchar_t *wline;
1882 int width;
1884 rewind(f);
1885 werase(view->window);
1887 if (header) {
1888 err = format_line(&wline, &width, header, view->ncols);
1889 if (err) {
1890 return err;
1893 if (view_needs_focus_indication(view))
1894 wstandout(view->window);
1895 waddwstr(view->window, wline);
1896 if (view_needs_focus_indication(view))
1897 wstandend(view->window);
1898 if (width < view->ncols)
1899 waddch(view->window, '\n');
1901 if (max_lines <= 1)
1902 return NULL;
1903 max_lines--;
1906 *eof = 0;
1907 while (nprinted < max_lines) {
1908 line = parse_next_line(f, &len);
1909 if (line == NULL) {
1910 *eof = 1;
1911 break;
1913 if (++nlines < *first_displayed_line) {
1914 free(line);
1915 continue;
1918 err = format_line(&wline, &width, line, view->ncols);
1919 if (err) {
1920 free(line);
1921 return err;
1923 waddwstr(view->window, wline);
1924 if (width < view->ncols)
1925 waddch(view->window, '\n');
1926 if (++nprinted == 1)
1927 *first_displayed_line = nlines;
1928 free(line);
1929 free(wline);
1930 wline = NULL;
1932 *last_displayed_line = nlines;
1934 view_vborder(view);
1936 return NULL;
1939 static char *
1940 get_datestr(time_t *time, char *datebuf)
1942 char *p, *s = ctime_r(time, datebuf);
1943 p = strchr(s, '\n');
1944 if (p)
1945 *p = '\0';
1946 return s;
1949 static const struct got_error *
1950 write_commit_info(struct got_object_id *commit_id,
1951 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
1953 const struct got_error *err = NULL;
1954 char datebuf[26];
1955 struct got_commit_object *commit;
1956 char *id_str = NULL;
1957 time_t committer_time;
1958 const char *author, *committer;
1959 char *refs_str = NULL;
1961 if (refs) {
1962 err = build_refs_str(&refs_str, refs, commit_id);
1963 if (err)
1964 return err;
1967 err = got_object_open_as_commit(&commit, repo, commit_id);
1968 if (err)
1969 return err;
1971 err = got_object_id_str(&id_str, commit_id);
1972 if (err) {
1973 err = got_error_from_errno();
1974 goto done;
1977 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1978 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
1979 err = got_error_from_errno();
1980 goto done;
1982 if (fprintf(outfile, "from: %s\n",
1983 got_object_commit_get_author(commit)) < 0) {
1984 err = got_error_from_errno();
1985 goto done;
1987 committer_time = got_object_commit_get_committer_time(commit);
1988 if (fprintf(outfile, "date: %s UTC\n",
1989 get_datestr(&committer_time, datebuf)) < 0) {
1990 err = got_error_from_errno();
1991 goto done;
1993 author = got_object_commit_get_author(commit);
1994 committer = got_object_commit_get_committer(commit);
1995 if (strcmp(author, committer) != 0 &&
1996 fprintf(outfile, "via: %s\n", committer) < 0) {
1997 err = got_error_from_errno();
1998 goto done;
2000 if (fprintf(outfile, "%s\n",
2001 got_object_commit_get_logmsg(commit)) < 0) {
2002 err = got_error_from_errno();
2003 goto done;
2005 done:
2006 free(id_str);
2007 free(refs_str);
2008 got_object_commit_close(commit);
2009 return err;
2012 static const struct got_error *
2013 create_diff(struct tog_diff_view_state *s)
2015 const struct got_error *err = NULL;
2016 FILE *f = NULL;
2017 int obj_type;
2019 f = got_opentemp();
2020 if (f == NULL) {
2021 err = got_error_from_errno();
2022 goto done;
2024 if (s->f && fclose(s->f) != 0) {
2025 err = got_error_from_errno();
2026 goto done;
2028 s->f = f;
2030 if (s->id1)
2031 err = got_object_get_type(&obj_type, s->repo, s->id1);
2032 else
2033 err = got_object_get_type(&obj_type, s->repo, s->id2);
2034 if (err)
2035 goto done;
2037 switch (obj_type) {
2038 case GOT_OBJ_TYPE_BLOB:
2039 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2040 s->diff_context, s->repo, f);
2041 break;
2042 case GOT_OBJ_TYPE_TREE:
2043 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2044 s->diff_context, s->repo, f);
2045 break;
2046 case GOT_OBJ_TYPE_COMMIT: {
2047 const struct got_object_id_queue *parent_ids;
2048 struct got_object_qid *pid;
2049 struct got_commit_object *commit2;
2051 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2052 if (err)
2053 break;
2054 /* Show commit info if we're diffing to a parent/root commit. */
2055 if (s->id1 == NULL)
2056 write_commit_info(s->id2, s->refs, s->repo, f);
2057 else {
2058 parent_ids = got_object_commit_get_parent_ids(commit2);
2059 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2060 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2061 write_commit_info(s->id2, s->refs,
2062 s->repo, f);
2063 break;
2067 got_object_commit_close(commit2);
2069 err = got_diff_objects_as_commits(s->id1, s->id2,
2070 s->diff_context, s->repo, f);
2071 break;
2073 default:
2074 err = got_error(GOT_ERR_OBJ_TYPE);
2075 break;
2077 done:
2078 if (f && fflush(f) != 0 && err == NULL)
2079 err = got_error_from_errno();
2080 return err;
2083 static const struct got_error *
2084 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2085 struct got_object_id *id2, struct tog_view *log_view,
2086 struct got_reflist_head *refs, struct got_repository *repo)
2088 const struct got_error *err;
2090 if (id1 != NULL && id2 != NULL) {
2091 int type1, type2;
2092 err = got_object_get_type(&type1, repo, id1);
2093 if (err)
2094 return err;
2095 err = got_object_get_type(&type2, repo, id2);
2096 if (err)
2097 return err;
2099 if (type1 != type2)
2100 return got_error(GOT_ERR_OBJ_TYPE);
2103 if (id1) {
2104 view->state.diff.id1 = got_object_id_dup(id1);
2105 if (view->state.diff.id1 == NULL)
2106 return got_error_from_errno();
2107 } else
2108 view->state.diff.id1 = NULL;
2110 view->state.diff.id2 = got_object_id_dup(id2);
2111 if (view->state.diff.id2 == NULL) {
2112 free(view->state.diff.id1);
2113 view->state.diff.id1 = NULL;
2114 return got_error_from_errno();
2116 view->state.diff.f = NULL;
2117 view->state.diff.first_displayed_line = 1;
2118 view->state.diff.last_displayed_line = view->nlines;
2119 view->state.diff.diff_context = 3;
2120 view->state.diff.log_view = log_view;
2121 view->state.diff.repo = repo;
2122 view->state.diff.refs = refs;
2124 err = create_diff(&view->state.diff);
2125 if (err) {
2126 free(view->state.diff.id1);
2127 view->state.diff.id1 = NULL;
2128 free(view->state.diff.id2);
2129 view->state.diff.id2 = NULL;
2130 return err;
2133 view->show = show_diff_view;
2134 view->input = input_diff_view;
2135 view->close = close_diff_view;
2137 return NULL;
2140 static const struct got_error *
2141 close_diff_view(struct tog_view *view)
2143 const struct got_error *err = NULL;
2145 free(view->state.diff.id1);
2146 view->state.diff.id1 = NULL;
2147 free(view->state.diff.id2);
2148 view->state.diff.id2 = NULL;
2149 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2150 err = got_error_from_errno();
2151 return err;
2154 static const struct got_error *
2155 show_diff_view(struct tog_view *view)
2157 const struct got_error *err;
2158 struct tog_diff_view_state *s = &view->state.diff;
2159 char *id_str1 = NULL, *id_str2, *header;
2161 if (s->id1) {
2162 err = got_object_id_str(&id_str1, s->id1);
2163 if (err)
2164 return err;
2166 err = got_object_id_str(&id_str2, s->id2);
2167 if (err)
2168 return err;
2170 if (asprintf(&header, "diff %s %s",
2171 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2172 err = got_error_from_errno();
2173 free(id_str1);
2174 free(id_str2);
2175 return err;
2177 free(id_str1);
2178 free(id_str2);
2180 return draw_file(view, s->f, &s->first_displayed_line,
2181 &s->last_displayed_line, &s->eof, view->nlines,
2182 header);
2185 static const struct got_error *
2186 set_selected_commit(struct tog_diff_view_state *s,
2187 struct commit_queue_entry *entry)
2189 const struct got_error *err;
2190 const struct got_object_id_queue *parent_ids;
2191 struct got_commit_object *selected_commit;
2192 struct got_object_qid *pid;
2194 free(s->id2);
2195 s->id2 = got_object_id_dup(entry->id);
2196 if (s->id2 == NULL)
2197 return got_error_from_errno();
2199 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2200 if (err)
2201 return err;
2202 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2203 free(s->id1);
2204 pid = SIMPLEQ_FIRST(parent_ids);
2205 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2206 got_object_commit_close(selected_commit);
2207 return NULL;
2210 static const struct got_error *
2211 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2212 struct tog_view **focus_view, struct tog_view *view, int ch)
2214 const struct got_error *err = NULL;
2215 struct tog_diff_view_state *s = &view->state.diff;
2216 struct tog_log_view_state *ls;
2217 struct commit_queue_entry *entry;
2218 int i;
2220 switch (ch) {
2221 case 'k':
2222 case KEY_UP:
2223 if (s->first_displayed_line > 1)
2224 s->first_displayed_line--;
2225 break;
2226 case KEY_PPAGE:
2227 i = 0;
2228 while (i++ < view->nlines - 1 &&
2229 s->first_displayed_line > 1)
2230 s->first_displayed_line--;
2231 break;
2232 case 'j':
2233 case KEY_DOWN:
2234 if (!s->eof)
2235 s->first_displayed_line++;
2236 break;
2237 case KEY_NPAGE:
2238 case ' ':
2239 i = 0;
2240 while (!s->eof && i++ < view->nlines - 1) {
2241 char *line;
2242 line = parse_next_line(s->f, NULL);
2243 s->first_displayed_line++;
2244 if (line == NULL)
2245 break;
2247 break;
2248 case '[':
2249 if (s->diff_context > 0) {
2250 s->diff_context--;
2251 err = create_diff(s);
2253 break;
2254 case ']':
2255 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2256 s->diff_context++;
2257 err = create_diff(s);
2259 break;
2260 case '<':
2261 case ',':
2262 if (s->log_view == NULL)
2263 break;
2264 ls = &s->log_view->state.log;
2265 entry = TAILQ_PREV(ls->selected_entry,
2266 commit_queue_head, entry);
2267 if (entry == NULL)
2268 break;
2270 err = input_log_view(NULL, NULL, NULL, s->log_view,
2271 KEY_UP);
2272 if (err)
2273 break;
2275 err = set_selected_commit(s, entry);
2276 if (err)
2277 break;
2279 s->first_displayed_line = 1;
2280 s->last_displayed_line = view->nlines;
2282 err = create_diff(s);
2283 break;
2284 case '>':
2285 case '.':
2286 if (s->log_view == NULL)
2287 break;
2288 ls = &s->log_view->state.log;
2289 err = input_log_view(NULL, NULL, NULL, s->log_view,
2290 KEY_DOWN);
2291 if (err)
2292 break;
2294 /* Hack: Ensure two commits get loaded. */
2295 if (!ls->thread_args.log_complete) {
2296 err = input_log_view(NULL, NULL, NULL,
2297 s->log_view, KEY_DOWN);
2298 if (err)
2299 break;
2300 err = input_log_view(NULL, NULL, NULL,
2301 s->log_view, KEY_UP);
2302 if (err)
2303 break;
2306 entry = TAILQ_NEXT(ls->selected_entry, entry);
2307 if (entry == NULL)
2308 break;
2310 err = set_selected_commit(s, entry);
2311 if (err)
2312 break;
2314 s->first_displayed_line = 1;
2315 s->last_displayed_line = view->nlines;
2317 err = create_diff(s);
2318 break;
2319 default:
2320 break;
2323 return err;
2326 static const struct got_error *
2327 cmd_diff(int argc, char *argv[])
2329 const struct got_error *error = NULL;
2330 struct got_repository *repo = NULL;
2331 struct got_reflist_head refs;
2332 struct got_object_id *id1 = NULL, *id2 = NULL;
2333 char *repo_path = NULL;
2334 char *id_str1 = NULL, *id_str2 = NULL;
2335 int ch;
2336 struct tog_view *view;
2338 #ifndef PROFILE
2339 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2340 NULL) == -1)
2341 err(1, "pledge");
2342 #endif
2344 while ((ch = getopt(argc, argv, "")) != -1) {
2345 switch (ch) {
2346 default:
2347 usage();
2348 /* NOTREACHED */
2352 argc -= optind;
2353 argv += optind;
2355 if (argc == 0) {
2356 usage_diff(); /* TODO show local worktree changes */
2357 } else if (argc == 2) {
2358 repo_path = getcwd(NULL, 0);
2359 if (repo_path == NULL)
2360 return got_error_from_errno();
2361 id_str1 = argv[0];
2362 id_str2 = argv[1];
2363 } else if (argc == 3) {
2364 repo_path = realpath(argv[0], NULL);
2365 if (repo_path == NULL)
2366 return got_error_from_errno();
2367 id_str1 = argv[1];
2368 id_str2 = argv[2];
2369 } else
2370 usage_diff();
2372 init_curses();
2374 error = apply_unveil(repo_path, NULL);
2375 if (error)
2376 goto done;
2378 error = got_repo_open(&repo, repo_path);
2379 free(repo_path);
2380 if (error)
2381 goto done;
2383 error = got_object_resolve_id_str(&id1, repo, id_str1);
2384 if (error)
2385 goto done;
2387 error = got_object_resolve_id_str(&id2, repo, id_str2);
2388 if (error)
2389 goto done;
2391 SIMPLEQ_INIT(&refs);
2392 error = got_ref_list(&refs, repo);
2393 if (error)
2394 goto done;
2396 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2397 if (view == NULL) {
2398 error = got_error_from_errno();
2399 goto done;
2401 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2402 if (error)
2403 goto done;
2404 error = view_loop(view);
2405 done:
2406 got_repo_close(repo);
2407 return error;
2410 __dead static void
2411 usage_blame(void)
2413 endwin();
2414 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2415 getprogname());
2416 exit(1);
2419 struct tog_blame_line {
2420 int annotated;
2421 struct got_object_id *id;
2424 static const struct got_error *
2425 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2426 const char *path, struct tog_blame_line *lines, int nlines,
2427 int blame_complete, int selected_line, int *first_displayed_line,
2428 int *last_displayed_line, int *eof, int max_lines)
2430 const struct got_error *err;
2431 int lineno = 0, nprinted = 0;
2432 char *line;
2433 size_t len;
2434 wchar_t *wline;
2435 int width, wlimit;
2436 struct tog_blame_line *blame_line;
2437 struct got_object_id *prev_id = NULL;
2438 char *id_str;
2440 err = got_object_id_str(&id_str, id);
2441 if (err)
2442 return err;
2444 rewind(f);
2445 werase(view->window);
2447 if (asprintf(&line, "commit %s", id_str) == -1) {
2448 err = got_error_from_errno();
2449 free(id_str);
2450 return err;
2453 err = format_line(&wline, &width, line, view->ncols);
2454 free(line);
2455 line = NULL;
2456 if (view_needs_focus_indication(view))
2457 wstandout(view->window);
2458 waddwstr(view->window, wline);
2459 if (view_needs_focus_indication(view))
2460 wstandend(view->window);
2461 free(wline);
2462 wline = NULL;
2463 if (width < view->ncols)
2464 waddch(view->window, '\n');
2466 if (asprintf(&line, "[%d/%d] %s%s",
2467 *first_displayed_line - 1 + selected_line, nlines,
2468 blame_complete ? "" : "annotating... ", path) == -1) {
2469 free(id_str);
2470 return got_error_from_errno();
2472 free(id_str);
2473 err = format_line(&wline, &width, line, view->ncols);
2474 free(line);
2475 line = NULL;
2476 if (err)
2477 return err;
2478 waddwstr(view->window, wline);
2479 free(wline);
2480 wline = NULL;
2481 if (width < view->ncols)
2482 waddch(view->window, '\n');
2484 *eof = 0;
2485 while (nprinted < max_lines - 2) {
2486 line = parse_next_line(f, &len);
2487 if (line == NULL) {
2488 *eof = 1;
2489 break;
2491 if (++lineno < *first_displayed_line) {
2492 free(line);
2493 continue;
2496 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2497 err = format_line(&wline, &width, line, wlimit);
2498 if (err) {
2499 free(line);
2500 return err;
2503 if (view->focussed && nprinted == selected_line - 1)
2504 wstandout(view->window);
2506 blame_line = &lines[lineno - 1];
2507 if (blame_line->annotated && prev_id &&
2508 got_object_id_cmp(prev_id, blame_line->id) == 0)
2509 waddstr(view->window, " ");
2510 else if (blame_line->annotated) {
2511 char *id_str;
2512 err = got_object_id_str(&id_str, blame_line->id);
2513 if (err) {
2514 free(line);
2515 free(wline);
2516 return err;
2518 wprintw(view->window, "%.8s ", id_str);
2519 free(id_str);
2520 prev_id = blame_line->id;
2521 } else {
2522 waddstr(view->window, "........ ");
2523 prev_id = NULL;
2526 waddwstr(view->window, wline);
2527 while (width < wlimit) {
2528 waddch(view->window, ' ');
2529 width++;
2531 if (view->focussed && nprinted == selected_line - 1)
2532 wstandend(view->window);
2533 if (++nprinted == 1)
2534 *first_displayed_line = lineno;
2535 free(line);
2536 free(wline);
2537 wline = NULL;
2539 *last_displayed_line = lineno;
2541 view_vborder(view);
2543 return NULL;
2546 static const struct got_error *
2547 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2549 const struct got_error *err = NULL;
2550 struct tog_blame_cb_args *a = arg;
2551 struct tog_blame_line *line;
2552 int errcode;
2554 if (nlines != a->nlines ||
2555 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2556 return got_error(GOT_ERR_RANGE);
2558 errcode = pthread_mutex_lock(&tog_mutex);
2559 if (errcode)
2560 return got_error_set_errno(errcode);
2562 if (*a->quit) { /* user has quit the blame view */
2563 err = got_error(GOT_ERR_ITER_COMPLETED);
2564 goto done;
2567 if (lineno == -1)
2568 goto done; /* no change in this commit */
2570 line = &a->lines[lineno - 1];
2571 if (line->annotated)
2572 goto done;
2574 line->id = got_object_id_dup(id);
2575 if (line->id == NULL) {
2576 err = got_error_from_errno();
2577 goto done;
2579 line->annotated = 1;
2580 done:
2581 errcode = pthread_mutex_unlock(&tog_mutex);
2582 if (errcode)
2583 err = got_error_set_errno(errcode);
2584 return err;
2587 static void *
2588 blame_thread(void *arg)
2590 const struct got_error *err;
2591 struct tog_blame_thread_args *ta = arg;
2592 struct tog_blame_cb_args *a = ta->cb_args;
2593 int errcode;
2595 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2596 blame_cb, ta->cb_args);
2598 errcode = pthread_mutex_lock(&tog_mutex);
2599 if (errcode)
2600 return (void *)got_error_set_errno(errcode);
2602 got_repo_close(ta->repo);
2603 ta->repo = NULL;
2604 *ta->complete = 1;
2606 errcode = pthread_mutex_unlock(&tog_mutex);
2607 if (errcode && err == NULL)
2608 err = got_error_set_errno(errcode);
2610 return (void *)err;
2613 static struct got_object_id *
2614 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2615 int selected_line)
2617 struct tog_blame_line *line;
2619 line = &lines[first_displayed_line - 1 + selected_line - 1];
2620 if (!line->annotated)
2621 return NULL;
2623 return line->id;
2626 static const struct got_error *
2627 stop_blame(struct tog_blame *blame)
2629 const struct got_error *err = NULL;
2630 int i;
2632 if (blame->thread) {
2633 int errcode;
2634 errcode = pthread_mutex_unlock(&tog_mutex);
2635 if (errcode)
2636 return got_error_set_errno(errcode);
2637 errcode = pthread_join(blame->thread, (void **)&err);
2638 if (errcode)
2639 return got_error_set_errno(errcode);
2640 errcode = pthread_mutex_lock(&tog_mutex);
2641 if (errcode)
2642 return got_error_set_errno(errcode);
2643 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2644 err = NULL;
2645 blame->thread = NULL;
2647 if (blame->thread_args.repo) {
2648 got_repo_close(blame->thread_args.repo);
2649 blame->thread_args.repo = NULL;
2651 if (blame->f) {
2652 if (fclose(blame->f) != 0 && err == NULL)
2653 err = got_error_from_errno();
2654 blame->f = NULL;
2656 if (blame->lines) {
2657 for (i = 0; i < blame->nlines; i++)
2658 free(blame->lines[i].id);
2659 free(blame->lines);
2660 blame->lines = NULL;
2662 free(blame->cb_args.commit_id);
2663 blame->cb_args.commit_id = NULL;
2665 return err;
2668 static const struct got_error *
2669 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2670 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2671 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2672 struct got_repository *repo)
2674 const struct got_error *err = NULL;
2675 struct got_blob_object *blob = NULL;
2676 struct got_repository *thread_repo = NULL;
2677 struct got_object_id *obj_id = NULL;
2678 int obj_type;
2680 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2681 if (err)
2682 return err;
2683 if (obj_id == NULL)
2684 return got_error(GOT_ERR_NO_OBJ);
2686 err = got_object_get_type(&obj_type, repo, obj_id);
2687 if (err)
2688 goto done;
2690 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2691 err = got_error(GOT_ERR_OBJ_TYPE);
2692 goto done;
2695 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2696 if (err)
2697 goto done;
2698 blame->f = got_opentemp();
2699 if (blame->f == NULL) {
2700 err = got_error_from_errno();
2701 goto done;
2703 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2704 blame->f, blob);
2705 if (err)
2706 goto done;
2708 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2709 if (blame->lines == NULL) {
2710 err = got_error_from_errno();
2711 goto done;
2714 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2715 if (err)
2716 goto done;
2718 blame->cb_args.view = view;
2719 blame->cb_args.lines = blame->lines;
2720 blame->cb_args.nlines = blame->nlines;
2721 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2722 if (blame->cb_args.commit_id == NULL) {
2723 err = got_error_from_errno();
2724 goto done;
2726 blame->cb_args.quit = done;
2728 blame->thread_args.path = path;
2729 blame->thread_args.repo = thread_repo;
2730 blame->thread_args.cb_args = &blame->cb_args;
2731 blame->thread_args.complete = blame_complete;
2732 *blame_complete = 0;
2734 done:
2735 if (blob)
2736 got_object_blob_close(blob);
2737 free(obj_id);
2738 if (err)
2739 stop_blame(blame);
2740 return err;
2743 static const struct got_error *
2744 open_blame_view(struct tog_view *view, char *path,
2745 struct got_object_id *commit_id, struct got_reflist_head *refs,
2746 struct got_repository *repo)
2748 const struct got_error *err = NULL;
2749 struct tog_blame_view_state *s = &view->state.blame;
2751 SIMPLEQ_INIT(&s->blamed_commits);
2753 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2754 if (err)
2755 return err;
2757 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2758 s->first_displayed_line = 1;
2759 s->last_displayed_line = view->nlines;
2760 s->selected_line = 1;
2761 s->blame_complete = 0;
2762 s->path = path;
2763 if (s->path == NULL)
2764 return got_error_from_errno();
2765 s->repo = repo;
2766 s->refs = refs;
2767 s->commit_id = commit_id;
2768 memset(&s->blame, 0, sizeof(s->blame));
2770 view->show = show_blame_view;
2771 view->input = input_blame_view;
2772 view->close = close_blame_view;
2774 return run_blame(&s->blame, view, &s->blame_complete,
2775 &s->first_displayed_line, &s->last_displayed_line,
2776 &s->selected_line, &s->done, &s->eof, s->path,
2777 s->blamed_commit->id, s->repo);
2780 static const struct got_error *
2781 close_blame_view(struct tog_view *view)
2783 const struct got_error *err = NULL;
2784 struct tog_blame_view_state *s = &view->state.blame;
2786 if (s->blame.thread)
2787 err = stop_blame(&s->blame);
2789 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2790 struct got_object_qid *blamed_commit;
2791 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2792 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2793 got_object_qid_free(blamed_commit);
2796 free(s->path);
2798 return err;
2801 static const struct got_error *
2802 show_blame_view(struct tog_view *view)
2804 const struct got_error *err = NULL;
2805 struct tog_blame_view_state *s = &view->state.blame;
2806 int errcode;
2808 if (s->blame.thread == NULL) {
2809 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2810 &s->blame.thread_args);
2811 if (errcode)
2812 return got_error_set_errno(errcode);
2815 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2816 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2817 s->selected_line, &s->first_displayed_line,
2818 &s->last_displayed_line, &s->eof, view->nlines);
2820 view_vborder(view);
2821 return err;
2824 static const struct got_error *
2825 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2826 struct tog_view **focus_view, struct tog_view *view, int ch)
2828 const struct got_error *err = NULL, *thread_err = NULL;
2829 struct tog_view *diff_view;
2830 struct tog_blame_view_state *s = &view->state.blame;
2831 int begin_x = 0;
2833 switch (ch) {
2834 case 'q':
2835 s->done = 1;
2836 break;
2837 case 'k':
2838 case KEY_UP:
2839 if (s->selected_line > 1)
2840 s->selected_line--;
2841 else if (s->selected_line == 1 &&
2842 s->first_displayed_line > 1)
2843 s->first_displayed_line--;
2844 break;
2845 case KEY_PPAGE:
2846 if (s->first_displayed_line == 1) {
2847 s->selected_line = 1;
2848 break;
2850 if (s->first_displayed_line > view->nlines - 2)
2851 s->first_displayed_line -=
2852 (view->nlines - 2);
2853 else
2854 s->first_displayed_line = 1;
2855 break;
2856 case 'j':
2857 case KEY_DOWN:
2858 if (s->selected_line < view->nlines - 2 &&
2859 s->first_displayed_line +
2860 s->selected_line <= s->blame.nlines)
2861 s->selected_line++;
2862 else if (s->last_displayed_line <
2863 s->blame.nlines)
2864 s->first_displayed_line++;
2865 break;
2866 case 'b':
2867 case 'p': {
2868 struct got_object_id *id = NULL;
2869 id = get_selected_commit_id(s->blame.lines,
2870 s->first_displayed_line, s->selected_line);
2871 if (id == NULL)
2872 break;
2873 if (ch == 'p') {
2874 struct got_commit_object *commit;
2875 struct got_object_qid *pid;
2876 struct got_object_id *blob_id = NULL;
2877 int obj_type;
2878 err = got_object_open_as_commit(&commit,
2879 s->repo, id);
2880 if (err)
2881 break;
2882 pid = SIMPLEQ_FIRST(
2883 got_object_commit_get_parent_ids(commit));
2884 if (pid == NULL) {
2885 got_object_commit_close(commit);
2886 break;
2888 /* Check if path history ends here. */
2889 err = got_object_id_by_path(&blob_id, s->repo,
2890 pid->id, s->path);
2891 if (err) {
2892 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2893 err = NULL;
2894 got_object_commit_close(commit);
2895 break;
2897 err = got_object_get_type(&obj_type, s->repo,
2898 blob_id);
2899 free(blob_id);
2900 /* Can't blame non-blob type objects. */
2901 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2902 got_object_commit_close(commit);
2903 break;
2905 err = got_object_qid_alloc(&s->blamed_commit,
2906 pid->id);
2907 got_object_commit_close(commit);
2908 } else {
2909 if (got_object_id_cmp(id,
2910 s->blamed_commit->id) == 0)
2911 break;
2912 err = got_object_qid_alloc(&s->blamed_commit,
2913 id);
2915 if (err)
2916 break;
2917 s->done = 1;
2918 thread_err = stop_blame(&s->blame);
2919 s->done = 0;
2920 if (thread_err)
2921 break;
2922 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2923 s->blamed_commit, entry);
2924 err = run_blame(&s->blame, view, &s->blame_complete,
2925 &s->first_displayed_line, &s->last_displayed_line,
2926 &s->selected_line, &s->done, &s->eof,
2927 s->path, s->blamed_commit->id, s->repo);
2928 if (err)
2929 break;
2930 break;
2932 case 'B': {
2933 struct got_object_qid *first;
2934 first = SIMPLEQ_FIRST(&s->blamed_commits);
2935 if (!got_object_id_cmp(first->id, s->commit_id))
2936 break;
2937 s->done = 1;
2938 thread_err = stop_blame(&s->blame);
2939 s->done = 0;
2940 if (thread_err)
2941 break;
2942 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2943 got_object_qid_free(s->blamed_commit);
2944 s->blamed_commit =
2945 SIMPLEQ_FIRST(&s->blamed_commits);
2946 err = run_blame(&s->blame, view, &s->blame_complete,
2947 &s->first_displayed_line, &s->last_displayed_line,
2948 &s->selected_line, &s->done, &s->eof, s->path,
2949 s->blamed_commit->id, s->repo);
2950 if (err)
2951 break;
2952 break;
2954 case KEY_ENTER:
2955 case '\r': {
2956 struct got_object_id *id = NULL;
2957 struct got_object_qid *pid;
2958 struct got_commit_object *commit = NULL;
2959 id = get_selected_commit_id(s->blame.lines,
2960 s->first_displayed_line, s->selected_line);
2961 if (id == NULL)
2962 break;
2963 err = got_object_open_as_commit(&commit, s->repo, id);
2964 if (err)
2965 break;
2966 pid = SIMPLEQ_FIRST(
2967 got_object_commit_get_parent_ids(commit));
2968 if (view_is_parent_view(view))
2969 begin_x = view_split_begin_x(view->begin_x);
2970 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2971 if (diff_view == NULL) {
2972 got_object_commit_close(commit);
2973 err = got_error_from_errno();
2974 break;
2976 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2977 id, NULL, s->refs, s->repo);
2978 got_object_commit_close(commit);
2979 if (err) {
2980 view_close(diff_view);
2981 break;
2983 if (view_is_parent_view(view)) {
2984 err = view_close_child(view);
2985 if (err)
2986 break;
2987 err = view_set_child(view, diff_view);
2988 if (err) {
2989 view_close(diff_view);
2990 break;
2992 *focus_view = diff_view;
2993 view->child_focussed = 1;
2994 } else
2995 *new_view = diff_view;
2996 if (err)
2997 break;
2998 break;
3000 case KEY_NPAGE:
3001 case ' ':
3002 if (s->last_displayed_line >= s->blame.nlines &&
3003 s->selected_line < view->nlines - 2) {
3004 s->selected_line = MIN(s->blame.nlines,
3005 view->nlines - 2);
3006 break;
3008 if (s->last_displayed_line + view->nlines - 2
3009 <= s->blame.nlines)
3010 s->first_displayed_line +=
3011 view->nlines - 2;
3012 else
3013 s->first_displayed_line =
3014 s->blame.nlines -
3015 (view->nlines - 3);
3016 break;
3017 case KEY_RESIZE:
3018 if (s->selected_line > view->nlines - 2) {
3019 s->selected_line = MIN(s->blame.nlines,
3020 view->nlines - 2);
3022 break;
3023 default:
3024 break;
3026 return thread_err ? thread_err : err;
3029 static const struct got_error *
3030 cmd_blame(int argc, char *argv[])
3032 const struct got_error *error;
3033 struct got_repository *repo = NULL;
3034 struct got_reflist_head refs;
3035 struct got_worktree *worktree = NULL;
3036 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3037 struct got_object_id *commit_id = NULL;
3038 char *commit_id_str = NULL;
3039 int ch;
3040 struct tog_view *view;
3042 #ifndef PROFILE
3043 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3044 NULL) == -1)
3045 err(1, "pledge");
3046 #endif
3048 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3049 switch (ch) {
3050 case 'c':
3051 commit_id_str = optarg;
3052 break;
3053 case 'r':
3054 repo_path = realpath(optarg, NULL);
3055 if (repo_path == NULL)
3056 err(1, "-r option");
3057 break;
3058 default:
3059 usage();
3060 /* NOTREACHED */
3064 argc -= optind;
3065 argv += optind;
3067 if (argc == 1)
3068 path = argv[0];
3069 else
3070 usage_blame();
3072 cwd = getcwd(NULL, 0);
3073 if (cwd == NULL) {
3074 error = got_error_from_errno();
3075 goto done;
3077 if (repo_path == NULL) {
3078 error = got_worktree_open(&worktree, cwd);
3079 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3080 goto done;
3081 else
3082 error = NULL;
3083 if (worktree) {
3084 repo_path =
3085 strdup(got_worktree_get_repo_path(worktree));
3086 if (repo_path == NULL)
3087 error = got_error_from_errno();
3088 if (error)
3089 goto done;
3090 } else {
3091 repo_path = strdup(cwd);
3092 if (repo_path == NULL) {
3093 error = got_error_from_errno();
3094 goto done;
3099 init_curses();
3101 error = apply_unveil(repo_path, NULL);
3102 if (error)
3103 goto done;
3105 error = got_repo_open(&repo, repo_path);
3106 if (error != NULL)
3107 goto done;
3109 if (worktree) {
3110 const char *prefix = got_worktree_get_path_prefix(worktree);
3111 char *p, *worktree_subdir = cwd +
3112 strlen(got_worktree_get_root_path(worktree));
3113 if (asprintf(&p, "%s%s%s%s%s",
3114 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3115 worktree_subdir, worktree_subdir[0] ? "/" : "",
3116 path) == -1) {
3117 error = got_error_from_errno();
3118 goto done;
3120 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3121 free(p);
3122 } else {
3123 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3125 if (error)
3126 goto done;
3128 if (commit_id_str == NULL) {
3129 struct got_reference *head_ref;
3130 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3131 if (error != NULL)
3132 goto done;
3133 error = got_ref_resolve(&commit_id, repo, head_ref);
3134 got_ref_close(head_ref);
3135 } else {
3136 error = got_object_resolve_id_str(&commit_id, repo,
3137 commit_id_str);
3139 if (error != NULL)
3140 goto done;
3142 SIMPLEQ_INIT(&refs);
3143 error = got_ref_list(&refs, repo);
3144 if (error)
3145 goto done;
3147 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3148 if (view == NULL) {
3149 error = got_error_from_errno();
3150 goto done;
3152 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3153 if (error)
3154 goto done;
3155 error = view_loop(view);
3156 done:
3157 free(repo_path);
3158 free(cwd);
3159 free(commit_id);
3160 if (worktree)
3161 got_worktree_close(worktree);
3162 if (repo)
3163 got_repo_close(repo);
3164 return error;
3167 static const struct got_error *
3168 draw_tree_entries(struct tog_view *view,
3169 struct got_tree_entry **first_displayed_entry,
3170 struct got_tree_entry **last_displayed_entry,
3171 struct got_tree_entry **selected_entry, int *ndisplayed,
3172 const char *label, int show_ids, const char *parent_path,
3173 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3175 const struct got_error *err = NULL;
3176 struct got_tree_entry *te;
3177 wchar_t *wline;
3178 int width, n;
3180 *ndisplayed = 0;
3182 werase(view->window);
3184 if (limit == 0)
3185 return NULL;
3187 err = format_line(&wline, &width, label, view->ncols);
3188 if (err)
3189 return err;
3190 if (view_needs_focus_indication(view))
3191 wstandout(view->window);
3192 waddwstr(view->window, wline);
3193 if (view_needs_focus_indication(view))
3194 wstandend(view->window);
3195 free(wline);
3196 wline = NULL;
3197 if (width < view->ncols)
3198 waddch(view->window, '\n');
3199 if (--limit <= 0)
3200 return NULL;
3201 err = format_line(&wline, &width, parent_path, view->ncols);
3202 if (err)
3203 return err;
3204 waddwstr(view->window, wline);
3205 free(wline);
3206 wline = NULL;
3207 if (width < view->ncols)
3208 waddch(view->window, '\n');
3209 if (--limit <= 0)
3210 return NULL;
3211 waddch(view->window, '\n');
3212 if (--limit <= 0)
3213 return NULL;
3215 te = SIMPLEQ_FIRST(&entries->head);
3216 if (*first_displayed_entry == NULL) {
3217 if (selected == 0) {
3218 if (view->focussed)
3219 wstandout(view->window);
3220 *selected_entry = NULL;
3222 waddstr(view->window, " ..\n"); /* parent directory */
3223 if (selected == 0 && view->focussed)
3224 wstandend(view->window);
3225 (*ndisplayed)++;
3226 if (--limit <= 0)
3227 return NULL;
3228 n = 1;
3229 } else {
3230 n = 0;
3231 while (te != *first_displayed_entry)
3232 te = SIMPLEQ_NEXT(te, entry);
3235 while (te) {
3236 char *line = NULL, *id_str = NULL;
3238 if (show_ids) {
3239 err = got_object_id_str(&id_str, te->id);
3240 if (err)
3241 return got_error_from_errno();
3243 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3244 te->name, S_ISDIR(te->mode) ? "/" :
3245 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3246 free(id_str);
3247 return got_error_from_errno();
3249 free(id_str);
3250 err = format_line(&wline, &width, line, view->ncols);
3251 if (err) {
3252 free(line);
3253 break;
3255 if (n == selected) {
3256 if (view->focussed)
3257 wstandout(view->window);
3258 *selected_entry = te;
3260 waddwstr(view->window, wline);
3261 if (width < view->ncols)
3262 waddch(view->window, '\n');
3263 if (n == selected && view->focussed)
3264 wstandend(view->window);
3265 free(line);
3266 free(wline);
3267 wline = NULL;
3268 n++;
3269 (*ndisplayed)++;
3270 *last_displayed_entry = te;
3271 if (--limit <= 0)
3272 break;
3273 te = SIMPLEQ_NEXT(te, entry);
3276 return err;
3279 static void
3280 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3281 const struct got_tree_entries *entries, int isroot)
3283 struct got_tree_entry *te, *prev;
3284 int i;
3286 if (*first_displayed_entry == NULL)
3287 return;
3289 te = SIMPLEQ_FIRST(&entries->head);
3290 if (*first_displayed_entry == te) {
3291 if (!isroot)
3292 *first_displayed_entry = NULL;
3293 return;
3296 /* XXX this is stupid... switch to TAILQ? */
3297 for (i = 0; i < maxscroll; i++) {
3298 while (te != *first_displayed_entry) {
3299 prev = te;
3300 te = SIMPLEQ_NEXT(te, entry);
3302 *first_displayed_entry = prev;
3303 te = SIMPLEQ_FIRST(&entries->head);
3305 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3306 *first_displayed_entry = NULL;
3309 static int
3310 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3311 struct got_tree_entry *last_displayed_entry,
3312 const struct got_tree_entries *entries)
3314 struct got_tree_entry *next, *last;
3315 int n = 0;
3317 if (*first_displayed_entry)
3318 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3319 else
3320 next = SIMPLEQ_FIRST(&entries->head);
3321 last = last_displayed_entry;
3322 while (next && last && n++ < maxscroll) {
3323 last = SIMPLEQ_NEXT(last, entry);
3324 if (last) {
3325 *first_displayed_entry = next;
3326 next = SIMPLEQ_NEXT(next, entry);
3329 return n;
3332 static const struct got_error *
3333 tree_entry_path(char **path, struct tog_parent_trees *parents,
3334 struct got_tree_entry *te)
3336 const struct got_error *err = NULL;
3337 struct tog_parent_tree *pt;
3338 size_t len = 2; /* for leading slash and NUL */
3340 TAILQ_FOREACH(pt, parents, entry)
3341 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3342 if (te)
3343 len += strlen(te->name);
3345 *path = calloc(1, len);
3346 if (path == NULL)
3347 return got_error_from_errno();
3349 (*path)[0] = '/';
3350 pt = TAILQ_LAST(parents, tog_parent_trees);
3351 while (pt) {
3352 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3353 err = got_error(GOT_ERR_NO_SPACE);
3354 goto done;
3356 if (strlcat(*path, "/", len) >= len) {
3357 err = got_error(GOT_ERR_NO_SPACE);
3358 goto done;
3360 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3362 if (te) {
3363 if (strlcat(*path, te->name, len) >= len) {
3364 err = got_error(GOT_ERR_NO_SPACE);
3365 goto done;
3368 done:
3369 if (err) {
3370 free(*path);
3371 *path = NULL;
3373 return err;
3376 static const struct got_error *
3377 blame_tree_entry(struct tog_view **new_view, int begin_x,
3378 struct got_tree_entry *te, struct tog_parent_trees *parents,
3379 struct got_object_id *commit_id, struct got_reflist_head *refs,
3380 struct got_repository *repo)
3382 const struct got_error *err = NULL;
3383 char *path;
3384 struct tog_view *blame_view;
3386 err = tree_entry_path(&path, parents, te);
3387 if (err)
3388 return err;
3390 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3391 if (blame_view == NULL)
3392 return got_error_from_errno();
3394 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3395 if (err) {
3396 view_close(blame_view);
3397 free(path);
3398 } else
3399 *new_view = blame_view;
3400 return err;
3403 static const struct got_error *
3404 log_tree_entry(struct tog_view **new_view, int begin_x,
3405 struct got_tree_entry *te, struct tog_parent_trees *parents,
3406 struct got_object_id *commit_id, struct got_reflist_head *refs,
3407 struct got_repository *repo)
3409 struct tog_view *log_view;
3410 const struct got_error *err = NULL;
3411 char *path;
3413 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3414 if (log_view == NULL)
3415 return got_error_from_errno();
3417 err = tree_entry_path(&path, parents, te);
3418 if (err)
3419 return err;
3421 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3422 if (err)
3423 view_close(log_view);
3424 else
3425 *new_view = log_view;
3426 free(path);
3427 return err;
3430 static const struct got_error *
3431 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3432 struct got_object_id *commit_id, struct got_reflist_head *refs,
3433 struct got_repository *repo)
3435 const struct got_error *err = NULL;
3436 char *commit_id_str = NULL;
3437 struct tog_tree_view_state *s = &view->state.tree;
3439 TAILQ_INIT(&s->parents);
3441 err = got_object_id_str(&commit_id_str, commit_id);
3442 if (err != NULL)
3443 goto done;
3445 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3446 err = got_error_from_errno();
3447 goto done;
3450 s->root = s->tree = root;
3451 s->entries = got_object_tree_get_entries(root);
3452 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3453 s->commit_id = got_object_id_dup(commit_id);
3454 if (s->commit_id == NULL) {
3455 err = got_error_from_errno();
3456 goto done;
3458 s->refs = refs;
3459 s->repo = repo;
3461 view->show = show_tree_view;
3462 view->input = input_tree_view;
3463 view->close = close_tree_view;
3464 done:
3465 free(commit_id_str);
3466 if (err) {
3467 free(s->tree_label);
3468 s->tree_label = NULL;
3470 return err;
3473 static const struct got_error *
3474 close_tree_view(struct tog_view *view)
3476 struct tog_tree_view_state *s = &view->state.tree;
3478 free(s->tree_label);
3479 s->tree_label = NULL;
3480 free(s->commit_id);
3481 s->commit_id = NULL;
3482 while (!TAILQ_EMPTY(&s->parents)) {
3483 struct tog_parent_tree *parent;
3484 parent = TAILQ_FIRST(&s->parents);
3485 TAILQ_REMOVE(&s->parents, parent, entry);
3486 free(parent);
3489 if (s->tree != s->root)
3490 got_object_tree_close(s->tree);
3491 got_object_tree_close(s->root);
3493 return NULL;
3496 static const struct got_error *
3497 show_tree_view(struct tog_view *view)
3499 const struct got_error *err = NULL;
3500 struct tog_tree_view_state *s = &view->state.tree;
3501 char *parent_path;
3503 err = tree_entry_path(&parent_path, &s->parents, NULL);
3504 if (err)
3505 return err;
3507 err = draw_tree_entries(view, &s->first_displayed_entry,
3508 &s->last_displayed_entry, &s->selected_entry,
3509 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3510 s->entries, s->selected, view->nlines, s->tree == s->root);
3511 free(parent_path);
3513 view_vborder(view);
3514 return err;
3517 static const struct got_error *
3518 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3519 struct tog_view **focus_view, struct tog_view *view, int ch)
3521 const struct got_error *err = NULL;
3522 struct tog_tree_view_state *s = &view->state.tree;
3523 struct tog_view *log_view;
3524 int begin_x = 0, nscrolled;
3526 switch (ch) {
3527 case 'i':
3528 s->show_ids = !s->show_ids;
3529 break;
3530 case 'l':
3531 if (!s->selected_entry)
3532 break;
3533 if (view_is_parent_view(view))
3534 begin_x = view_split_begin_x(view->begin_x);
3535 err = log_tree_entry(&log_view, begin_x,
3536 s->selected_entry, &s->parents,
3537 s->commit_id, s->refs, s->repo);
3538 if (view_is_parent_view(view)) {
3539 err = view_close_child(view);
3540 if (err)
3541 return err;
3542 err = view_set_child(view, log_view);
3543 if (err) {
3544 view_close(log_view);
3545 break;
3547 *focus_view = log_view;
3548 view->child_focussed = 1;
3549 } else
3550 *new_view = log_view;
3551 break;
3552 case 'k':
3553 case KEY_UP:
3554 if (s->selected > 0) {
3555 s->selected--;
3556 if (s->selected == 0)
3557 break;
3559 if (s->selected > 0)
3560 break;
3561 tree_scroll_up(&s->first_displayed_entry, 1,
3562 s->entries, s->tree == s->root);
3563 break;
3564 case KEY_PPAGE:
3565 tree_scroll_up(&s->first_displayed_entry,
3566 MAX(0, view->nlines - 4 - s->selected), s->entries,
3567 s->tree == s->root);
3568 s->selected = 0;
3569 if (SIMPLEQ_FIRST(&s->entries->head) ==
3570 s->first_displayed_entry && s->tree != s->root)
3571 s->first_displayed_entry = NULL;
3572 break;
3573 case 'j':
3574 case KEY_DOWN:
3575 if (s->selected < s->ndisplayed - 1) {
3576 s->selected++;
3577 break;
3579 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3580 == NULL) {
3581 /* can't scroll any further */
3582 break;
3584 tree_scroll_down(&s->first_displayed_entry, 1,
3585 s->last_displayed_entry, s->entries);
3586 break;
3587 case KEY_NPAGE:
3588 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3589 == NULL) {
3590 /* can't scroll any further; move cursor down */
3591 if (s->selected < s->ndisplayed - 1)
3592 s->selected = s->ndisplayed - 1;
3593 break;
3595 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3596 view->nlines, s->last_displayed_entry, s->entries);
3597 if (nscrolled < view->nlines) {
3598 int ndisplayed = 0;
3599 struct got_tree_entry *te;
3600 te = s->first_displayed_entry;
3601 do {
3602 ndisplayed++;
3603 te = SIMPLEQ_NEXT(te, entry);
3604 } while (te);
3605 s->selected = ndisplayed - 1;
3607 break;
3608 case KEY_ENTER:
3609 case '\r':
3610 if (s->selected_entry == NULL) {
3611 struct tog_parent_tree *parent;
3612 case KEY_BACKSPACE:
3613 /* user selected '..' */
3614 if (s->tree == s->root)
3615 break;
3616 parent = TAILQ_FIRST(&s->parents);
3617 TAILQ_REMOVE(&s->parents, parent,
3618 entry);
3619 got_object_tree_close(s->tree);
3620 s->tree = parent->tree;
3621 s->entries =
3622 got_object_tree_get_entries(s->tree);
3623 s->first_displayed_entry =
3624 parent->first_displayed_entry;
3625 s->selected_entry =
3626 parent->selected_entry;
3627 s->selected = parent->selected;
3628 free(parent);
3629 } else if (S_ISDIR(s->selected_entry->mode)) {
3630 struct tog_parent_tree *parent;
3631 struct got_tree_object *child;
3632 err = got_object_open_as_tree(&child,
3633 s->repo, s->selected_entry->id);
3634 if (err)
3635 break;
3636 parent = calloc(1, sizeof(*parent));
3637 if (parent == NULL) {
3638 err = got_error_from_errno();
3639 break;
3641 parent->tree = s->tree;
3642 parent->first_displayed_entry =
3643 s->first_displayed_entry;
3644 parent->selected_entry = s->selected_entry;
3645 parent->selected = s->selected;
3646 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3647 s->tree = child;
3648 s->entries =
3649 got_object_tree_get_entries(s->tree);
3650 s->selected = 0;
3651 s->first_displayed_entry = NULL;
3652 } else if (S_ISREG(s->selected_entry->mode)) {
3653 struct tog_view *blame_view;
3654 int begin_x = view_is_parent_view(view) ?
3655 view_split_begin_x(view->begin_x) : 0;
3657 err = blame_tree_entry(&blame_view, begin_x,
3658 s->selected_entry, &s->parents,
3659 s->commit_id, s->refs, s->repo);
3660 if (err)
3661 break;
3662 if (view_is_parent_view(view)) {
3663 err = view_close_child(view);
3664 if (err)
3665 return err;
3666 err = view_set_child(view, blame_view);
3667 if (err) {
3668 view_close(blame_view);
3669 break;
3671 *focus_view = blame_view;
3672 view->child_focussed = 1;
3673 } else
3674 *new_view = blame_view;
3676 break;
3677 case KEY_RESIZE:
3678 if (s->selected > view->nlines)
3679 s->selected = s->ndisplayed - 1;
3680 break;
3681 default:
3682 break;
3685 return err;
3688 __dead static void
3689 usage_tree(void)
3691 endwin();
3692 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3693 getprogname());
3694 exit(1);
3697 static const struct got_error *
3698 cmd_tree(int argc, char *argv[])
3700 const struct got_error *error;
3701 struct got_repository *repo = NULL;
3702 struct got_reflist_head refs;
3703 char *repo_path = NULL;
3704 struct got_object_id *commit_id = NULL;
3705 char *commit_id_arg = NULL;
3706 struct got_commit_object *commit = NULL;
3707 struct got_tree_object *tree = NULL;
3708 int ch;
3709 struct tog_view *view;
3711 #ifndef PROFILE
3712 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3713 NULL) == -1)
3714 err(1, "pledge");
3715 #endif
3717 while ((ch = getopt(argc, argv, "c:")) != -1) {
3718 switch (ch) {
3719 case 'c':
3720 commit_id_arg = optarg;
3721 break;
3722 default:
3723 usage();
3724 /* NOTREACHED */
3728 argc -= optind;
3729 argv += optind;
3731 if (argc == 0) {
3732 struct got_worktree *worktree;
3733 char *cwd = getcwd(NULL, 0);
3734 if (cwd == NULL)
3735 return got_error_from_errno();
3736 error = got_worktree_open(&worktree, cwd);
3737 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3738 goto done;
3739 if (worktree) {
3740 free(cwd);
3741 repo_path =
3742 strdup(got_worktree_get_repo_path(worktree));
3743 got_worktree_close(worktree);
3744 } else
3745 repo_path = cwd;
3746 if (repo_path == NULL) {
3747 error = got_error_from_errno();
3748 goto done;
3750 } else if (argc == 1) {
3751 repo_path = realpath(argv[0], NULL);
3752 if (repo_path == NULL)
3753 return got_error_from_errno();
3754 } else
3755 usage_log();
3757 init_curses();
3759 error = apply_unveil(repo_path, NULL);
3760 if (error)
3761 goto done;
3763 error = got_repo_open(&repo, repo_path);
3764 if (error != NULL)
3765 goto done;
3767 if (commit_id_arg == NULL)
3768 error = get_head_commit_id(&commit_id, repo);
3769 else
3770 error = got_object_resolve_id_str(&commit_id, repo,
3771 commit_id_arg);
3772 if (error != NULL)
3773 goto done;
3775 error = got_object_open_as_commit(&commit, repo, commit_id);
3776 if (error != NULL)
3777 goto done;
3779 error = got_object_open_as_tree(&tree, repo,
3780 got_object_commit_get_tree_id(commit));
3781 if (error != NULL)
3782 goto done;
3784 SIMPLEQ_INIT(&refs);
3785 error = got_ref_list(&refs, repo);
3786 if (error)
3787 goto done;
3789 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3790 if (view == NULL) {
3791 error = got_error_from_errno();
3792 goto done;
3794 error = open_tree_view(view, tree, commit_id, &refs, repo);
3795 if (error)
3796 goto done;
3797 error = view_loop(view);
3798 done:
3799 free(repo_path);
3800 free(commit_id);
3801 if (commit)
3802 got_object_commit_close(commit);
3803 if (tree)
3804 got_object_tree_close(tree);
3805 if (repo)
3806 got_repo_close(repo);
3807 return error;
3810 __dead static void
3811 usage(void)
3813 int i;
3815 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3816 "Available commands:\n", getprogname());
3817 for (i = 0; i < nitems(tog_commands); i++) {
3818 struct tog_cmd *cmd = &tog_commands[i];
3819 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3821 exit(1);
3824 static char **
3825 make_argv(const char *arg0, const char *arg1)
3827 char **argv;
3828 int argc = (arg1 == NULL ? 1 : 2);
3830 argv = calloc(argc, sizeof(char *));
3831 if (argv == NULL)
3832 err(1, "calloc");
3833 argv[0] = strdup(arg0);
3834 if (argv[0] == NULL)
3835 err(1, "calloc");
3836 if (arg1) {
3837 argv[1] = strdup(arg1);
3838 if (argv[1] == NULL)
3839 err(1, "calloc");
3842 return argv;
3845 int
3846 main(int argc, char *argv[])
3848 const struct got_error *error = NULL;
3849 struct tog_cmd *cmd = NULL;
3850 int ch, hflag = 0;
3851 char **cmd_argv = NULL;
3853 setlocale(LC_CTYPE, "");
3855 while ((ch = getopt(argc, argv, "h")) != -1) {
3856 switch (ch) {
3857 case 'h':
3858 hflag = 1;
3859 break;
3860 default:
3861 usage();
3862 /* NOTREACHED */
3866 argc -= optind;
3867 argv += optind;
3868 optind = 0;
3869 optreset = 1;
3871 if (argc == 0) {
3872 if (hflag)
3873 usage();
3874 /* Build an argument vector which runs a default command. */
3875 cmd = &tog_commands[0];
3876 cmd_argv = make_argv(cmd->name, NULL);
3877 argc = 1;
3878 } else {
3879 int i;
3881 /* Did the user specific a command? */
3882 for (i = 0; i < nitems(tog_commands); i++) {
3883 if (strncmp(tog_commands[i].name, argv[0],
3884 strlen(argv[0])) == 0) {
3885 cmd = &tog_commands[i];
3886 if (hflag)
3887 tog_commands[i].cmd_usage();
3888 break;
3891 if (cmd == NULL) {
3892 /* Did the user specify a repository? */
3893 char *repo_path = realpath(argv[0], NULL);
3894 if (repo_path) {
3895 struct got_repository *repo;
3896 error = got_repo_open(&repo, repo_path);
3897 if (error == NULL)
3898 got_repo_close(repo);
3899 } else
3900 error = got_error_from_errno();
3901 if (error) {
3902 if (hflag) {
3903 fprintf(stderr, "%s: '%s' is not a "
3904 "known command\n", getprogname(),
3905 argv[0]);
3906 usage();
3908 fprintf(stderr, "%s: '%s' is neither a known "
3909 "command nor a path to a repository\n",
3910 getprogname(), argv[0]);
3911 free(repo_path);
3912 return 1;
3914 cmd = &tog_commands[0];
3915 cmd_argv = make_argv(cmd->name, repo_path);
3916 argc = 2;
3917 free(repo_path);
3921 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3922 if (error)
3923 goto done;
3924 done:
3925 endwin();
3926 free(cmd_argv);
3927 if (error)
3928 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3929 return 0;