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_path.h"
51 #include "got_worktree.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef MAX
58 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 #endif
61 #define CTRL(x) ((x) & 0x1f)
63 #ifndef nitems
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 #endif
67 struct tog_cmd {
68 const char *name;
69 const struct got_error *(*cmd_main)(int, char *[]);
70 void (*cmd_usage)(void);
71 const char *descr;
72 };
74 __dead static void usage(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log,
87 "show repository history" },
88 { "diff", cmd_diff, usage_diff,
89 "compare files and directories" },
90 { "blame", cmd_blame, usage_blame,
91 "show line-by-line file history" },
92 { "tree", cmd_tree, usage_tree,
93 "browse trees in repository" },
94 };
96 enum tog_view_type {
97 TOG_VIEW_DIFF,
98 TOG_VIEW_LOG,
99 TOG_VIEW_BLAME,
100 TOG_VIEW_TREE
101 };
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_diff_view_state {
116 struct got_object_id *id1, *id2;
117 FILE *f;
118 int first_displayed_line;
119 int last_displayed_line;
120 int eof;
121 int diff_context;
122 struct got_repository *repo;
123 struct got_reflist_head *refs;
125 /* passed from log view; may be NULL */
126 struct tog_view *log_view;
127 };
129 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 struct tog_log_thread_args {
132 pthread_cond_t need_commits;
133 int commits_needed;
134 struct got_commit_graph *graph;
135 struct commit_queue *commits;
136 const char *in_repo_path;
137 struct got_object_id *start_id;
138 struct got_repository *repo;
139 int log_complete;
140 sig_atomic_t *quit;
141 struct tog_view *view;
142 struct commit_queue_entry **first_displayed_entry;
143 struct commit_queue_entry **selected_entry;
144 };
146 struct tog_log_view_state {
147 struct commit_queue commits;
148 struct commit_queue_entry *first_displayed_entry;
149 struct commit_queue_entry *last_displayed_entry;
150 struct commit_queue_entry *selected_entry;
151 int selected;
152 char *in_repo_path;
153 struct got_repository *repo;
154 struct got_reflist_head *refs;
155 struct got_object_id *start_id;
156 sig_atomic_t quit;
157 pthread_t thread;
158 struct tog_log_thread_args thread_args;
159 };
161 struct tog_blame_cb_args {
162 struct tog_blame_line *lines; /* one per line */
163 int nlines;
165 struct tog_view *view;
166 struct got_object_id *commit_id;
167 int *quit;
168 };
170 struct tog_blame_thread_args {
171 const char *path;
172 struct got_repository *repo;
173 struct tog_blame_cb_args *cb_args;
174 int *complete;
175 };
177 struct tog_blame {
178 FILE *f;
179 size_t filesize;
180 struct tog_blame_line *lines;
181 int nlines;
182 pthread_t thread;
183 struct tog_blame_thread_args thread_args;
184 struct tog_blame_cb_args cb_args;
185 const char *path;
186 };
188 struct tog_blame_view_state {
189 int first_displayed_line;
190 int last_displayed_line;
191 int selected_line;
192 int blame_complete;
193 int eof;
194 int done;
195 struct got_object_id_queue blamed_commits;
196 struct got_object_qid *blamed_commit;
197 char *path;
198 struct got_repository *repo;
199 struct got_reflist_head *refs;
200 struct got_object_id *commit_id;
201 struct tog_blame blame;
202 };
204 struct tog_parent_tree {
205 TAILQ_ENTRY(tog_parent_tree) entry;
206 struct got_tree_object *tree;
207 struct got_tree_entry *first_displayed_entry;
208 struct got_tree_entry *selected_entry;
209 int selected;
210 };
212 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
214 struct tog_tree_view_state {
215 char *tree_label;
216 struct got_tree_object *root;
217 struct got_tree_object *tree;
218 const struct got_tree_entries *entries;
219 struct got_tree_entry *first_displayed_entry;
220 struct got_tree_entry *last_displayed_entry;
221 struct got_tree_entry *selected_entry;
222 int ndisplayed, selected, show_ids;
223 struct tog_parent_trees parents;
224 struct got_object_id *commit_id;
225 struct got_repository *repo;
226 struct got_reflist_head *refs;
227 };
229 /*
230 * We implement two types of views: parent views and child views.
232 * The 'Tab' key switches between a parent view and its child view.
233 * Child views are shown side-by-side to their parent view, provided
234 * there is enough screen estate.
236 * When a new view is opened from within a parent view, this new view
237 * becomes a child view of the parent view, replacing any existing child.
239 * When a new view is opened from within a child view, this new view
240 * becomes a parent view which will obscure the views below until the
241 * user quits the new parent view by typing 'q'.
243 * This list of views contains parent views only.
244 * Child views are only pointed to by their parent view.
245 */
246 TAILQ_HEAD(tog_view_list_head, tog_view);
248 struct tog_view {
249 TAILQ_ENTRY(tog_view) entry;
250 WINDOW *window;
251 PANEL *panel;
252 int nlines, ncols, begin_y, begin_x;
253 int lines, cols; /* copies of LINES and COLS */
254 int focussed;
255 struct tog_view *parent;
256 struct tog_view *child;
257 int child_focussed;
259 /* type-specific state */
260 enum tog_view_type type;
261 union {
262 struct tog_diff_view_state diff;
263 struct tog_log_view_state log;
264 struct tog_blame_view_state blame;
265 struct tog_tree_view_state tree;
266 } state;
268 const struct got_error *(*show)(struct tog_view *);
269 const struct got_error *(*input)(struct tog_view **,
270 struct tog_view **, struct tog_view**, struct tog_view *, int);
271 const struct got_error *(*close)(struct tog_view *);
272 };
274 static const struct got_error *open_diff_view(struct tog_view *,
275 struct got_object_id *, struct got_object_id *, struct tog_view *,
276 struct got_reflist_head *, struct got_repository *);
277 static const struct got_error *show_diff_view(struct tog_view *);
278 static const struct got_error *input_diff_view(struct tog_view **,
279 struct tog_view **, struct tog_view **, struct tog_view *, int);
280 static const struct got_error* close_diff_view(struct tog_view *);
282 static const struct got_error *open_log_view(struct tog_view *,
283 struct got_object_id *, struct got_reflist_head *,
284 struct got_repository *, const char *, int);
285 static const struct got_error * show_log_view(struct tog_view *);
286 static const struct got_error *input_log_view(struct tog_view **,
287 struct tog_view **, struct tog_view **, struct tog_view *, int);
288 static const struct got_error *close_log_view(struct tog_view *);
290 static const struct got_error *open_blame_view(struct tog_view *, char *,
291 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
292 static const struct got_error *show_blame_view(struct tog_view *);
293 static const struct got_error *input_blame_view(struct tog_view **,
294 struct tog_view **, struct tog_view **, struct tog_view *, int);
295 static const struct got_error *close_blame_view(struct tog_view *);
297 static const struct got_error *open_tree_view(struct tog_view *,
298 struct got_tree_object *, struct got_object_id *,
299 struct got_reflist_head *, struct got_repository *);
300 static const struct got_error *show_tree_view(struct tog_view *);
301 static const struct got_error *input_tree_view(struct tog_view **,
302 struct tog_view **, struct tog_view **, struct tog_view *, int);
303 static const struct got_error *close_tree_view(struct tog_view *);
305 static volatile sig_atomic_t tog_sigwinch_received;
307 static void
308 tog_sigwinch(int signo)
310 tog_sigwinch_received = 1;
313 static const struct got_error *
314 view_close(struct tog_view *view)
316 const struct got_error *err = NULL;
318 if (view->child) {
319 view_close(view->child);
320 view->child = NULL;
322 if (view->close)
323 err = view->close(view);
324 if (view->panel)
325 del_panel(view->panel);
326 if (view->window)
327 delwin(view->window);
328 free(view);
329 return err;
332 static struct tog_view *
333 view_open(int nlines, int ncols, int begin_y, int begin_x,
334 enum tog_view_type type)
336 struct tog_view *view = calloc(1, sizeof(*view));
338 if (view == NULL)
339 return NULL;
341 view->type = type;
342 view->lines = LINES;
343 view->cols = COLS;
344 view->nlines = nlines ? nlines : LINES - begin_y;
345 view->ncols = ncols ? ncols : COLS - begin_x;
346 view->begin_y = begin_y;
347 view->begin_x = begin_x;
348 view->window = newwin(nlines, ncols, begin_y, begin_x);
349 if (view->window == NULL) {
350 view_close(view);
351 return NULL;
353 view->panel = new_panel(view->window);
354 if (view->panel == NULL ||
355 set_panel_userptr(view->panel, view) != OK) {
356 view_close(view);
357 return NULL;
360 keypad(view->window, TRUE);
361 return view;
364 static int
365 view_split_begin_x(int begin_x)
367 if (begin_x > 0 || COLS < 120)
368 return 0;
369 return (COLS - MAX(COLS / 2, 80));
372 static const struct got_error *view_resize(struct tog_view *);
374 static const struct got_error *
375 view_splitscreen(struct tog_view *view)
377 const struct got_error *err = NULL;
379 view->begin_y = 0;
380 view->begin_x = view_split_begin_x(0);
381 view->nlines = LINES;
382 view->ncols = COLS - view->begin_x;
383 view->lines = LINES;
384 view->cols = COLS;
385 err = view_resize(view);
386 if (err)
387 return err;
389 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
390 return got_error_from_errno("mvwin");
392 return NULL;
395 static const struct got_error *
396 view_fullscreen(struct tog_view *view)
398 const struct got_error *err = NULL;
400 view->begin_x = 0;
401 view->begin_y = 0;
402 view->nlines = LINES;
403 view->ncols = COLS;
404 view->lines = LINES;
405 view->cols = COLS;
406 err = view_resize(view);
407 if (err)
408 return err;
410 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
411 return got_error_from_errno("mvwin");
413 return NULL;
416 static int
417 view_is_parent_view(struct tog_view *view)
419 return view->parent == NULL;
422 static const struct got_error *
423 view_resize(struct tog_view *view)
425 int nlines, ncols;
427 if (view->lines > LINES)
428 nlines = view->nlines - (view->lines - LINES);
429 else
430 nlines = view->nlines + (LINES - view->lines);
432 if (view->cols > COLS)
433 ncols = view->ncols - (view->cols - COLS);
434 else
435 ncols = view->ncols + (COLS - view->cols);
437 if (wresize(view->window, nlines, ncols) == ERR)
438 return got_error_from_errno("wresize");
439 if (replace_panel(view->panel, view->window) == ERR)
440 return got_error_from_errno("replace_panel");
441 wclear(view->window);
443 view->nlines = nlines;
444 view->ncols = ncols;
445 view->lines = LINES;
446 view->cols = COLS;
448 if (view->child) {
449 view->child->begin_x = view_split_begin_x(view->begin_x);
450 if (view->child->begin_x == 0) {
451 view_fullscreen(view->child);
452 if (view->child->focussed)
453 show_panel(view->child->panel);
454 else
455 show_panel(view->panel);
456 } else {
457 view_splitscreen(view->child);
458 show_panel(view->child->panel);
462 return NULL;
465 static const struct got_error *
466 view_close_child(struct tog_view *view)
468 const struct got_error *err = NULL;
470 if (view->child == NULL)
471 return NULL;
473 err = view_close(view->child);
474 view->child = NULL;
475 return err;
478 static const struct got_error *
479 view_set_child(struct tog_view *view, struct tog_view *child)
481 const struct got_error *err = NULL;
483 view->child = child;
484 child->parent = view;
485 return err;
488 static int
489 view_is_splitscreen(struct tog_view *view)
491 return view->begin_x > 0;
494 static void
495 tog_resizeterm(void)
497 int cols, lines;
498 struct winsize size;
500 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
501 cols = 80; /* Default */
502 lines = 24;
503 } else {
504 cols = size.ws_col;
505 lines = size.ws_row;
507 resize_term(lines, cols);
510 static const struct got_error *
511 view_input(struct tog_view **new, struct tog_view **dead,
512 struct tog_view **focus, int *done, struct tog_view *view,
513 struct tog_view_list_head *views)
515 const struct got_error *err = NULL;
516 struct tog_view *v;
517 int ch, errcode;
519 *new = NULL;
520 *dead = NULL;
521 *focus = NULL;
523 nodelay(stdscr, FALSE);
524 /* Allow threads to make progress while we are waiting for input. */
525 errcode = pthread_mutex_unlock(&tog_mutex);
526 if (errcode)
527 return got_error_set_errno(errcode, "pthread_mutex_unlock");
528 ch = wgetch(view->window);
529 errcode = pthread_mutex_lock(&tog_mutex);
530 if (errcode)
531 return got_error_set_errno(errcode, "pthread_mutex_lock");
532 nodelay(stdscr, TRUE);
534 if (tog_sigwinch_received) {
535 tog_resizeterm();
536 tog_sigwinch_received = 0;
537 TAILQ_FOREACH(v, views, entry) {
538 err = view_resize(v);
539 if (err)
540 return err;
541 err = v->input(new, dead, focus, v, KEY_RESIZE);
542 if (err)
543 return err;
547 switch (ch) {
548 case ERR:
549 break;
550 case '\t':
551 if (view->child) {
552 *focus = view->child;
553 view->child_focussed = 1;
554 } else if (view->parent) {
555 *focus = view->parent;
556 view->parent->child_focussed = 0;
558 break;
559 case 'q':
560 err = view->input(new, dead, focus, view, ch);
561 *dead = view;
562 break;
563 case 'Q':
564 *done = 1;
565 break;
566 case 'f':
567 if (view_is_parent_view(view)) {
568 if (view->child == NULL)
569 break;
570 if (view_is_splitscreen(view->child)) {
571 *focus = view->child;
572 view->child_focussed = 1;
573 err = view_fullscreen(view->child);
574 } else
575 err = view_splitscreen(view->child);
576 if (err)
577 break;
578 err = view->child->input(new, dead, focus,
579 view->child, KEY_RESIZE);
580 } else {
581 if (view_is_splitscreen(view)) {
582 *focus = view;
583 view->parent->child_focussed = 1;
584 err = view_fullscreen(view);
585 } else {
586 err = view_splitscreen(view);
588 if (err)
589 break;
590 err = view->input(new, dead, focus, view,
591 KEY_RESIZE);
593 break;
594 case KEY_RESIZE:
595 break;
596 default:
597 err = view->input(new, dead, focus, view, ch);
598 break;
601 return err;
604 void
605 view_vborder(struct tog_view *view)
607 PANEL *panel;
608 struct tog_view *view_above;
610 if (view->parent)
611 return view_vborder(view->parent);
613 panel = panel_above(view->panel);
614 if (panel == NULL)
615 return;
617 view_above = panel_userptr(panel);
618 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
619 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
622 int
623 view_needs_focus_indication(struct tog_view *view)
625 if (view_is_parent_view(view)) {
626 if (view->child == NULL || view->child_focussed)
627 return 0;
628 if (!view_is_splitscreen(view->child))
629 return 0;
630 } else if (!view_is_splitscreen(view))
631 return 0;
633 return view->focussed;
636 static const struct got_error *
637 view_loop(struct tog_view *view)
639 const struct got_error *err = NULL;
640 struct tog_view_list_head views;
641 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
642 int fast_refresh = 10;
643 int done = 0, errcode;
645 errcode = pthread_mutex_lock(&tog_mutex);
646 if (errcode)
647 return got_error_set_errno(errcode, "pthread_mutex_lock");
649 TAILQ_INIT(&views);
650 TAILQ_INSERT_HEAD(&views, view, entry);
652 main_view = view;
653 view->focussed = 1;
654 err = view->show(view);
655 if (err)
656 return err;
657 update_panels();
658 doupdate();
659 while (!TAILQ_EMPTY(&views) && !done) {
660 /* Refresh fast during initialization, then become slower. */
661 if (fast_refresh && fast_refresh-- == 0)
662 halfdelay(10); /* switch to once per second */
664 err = view_input(&new_view, &dead_view, &focus_view, &done,
665 view, &views);
666 if (err)
667 break;
668 if (dead_view) {
669 struct tog_view *prev = NULL;
671 if (view_is_parent_view(dead_view))
672 prev = TAILQ_PREV(dead_view,
673 tog_view_list_head, entry);
674 else if (view->parent != dead_view)
675 prev = view->parent;
677 if (dead_view->parent)
678 dead_view->parent->child = NULL;
679 else
680 TAILQ_REMOVE(&views, dead_view, entry);
682 err = view_close(dead_view);
683 if (err || dead_view == main_view)
684 goto done;
686 if (view == dead_view) {
687 if (focus_view)
688 view = focus_view;
689 else if (prev)
690 view = prev;
691 else if (!TAILQ_EMPTY(&views))
692 view = TAILQ_LAST(&views,
693 tog_view_list_head);
694 else
695 view = NULL;
696 if (view) {
697 if (view->child && view->child_focussed)
698 focus_view = view->child;
699 else
700 focus_view = view;
704 if (new_view) {
705 struct tog_view *v, *t;
706 /* Only allow one parent view per type. */
707 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
708 if (v->type != new_view->type)
709 continue;
710 TAILQ_REMOVE(&views, v, entry);
711 err = view_close(v);
712 if (err)
713 goto done;
714 break;
716 TAILQ_INSERT_TAIL(&views, new_view, entry);
717 view = new_view;
718 if (focus_view == NULL)
719 focus_view = new_view;
721 if (focus_view) {
722 show_panel(focus_view->panel);
723 if (view)
724 view->focussed = 0;
725 focus_view->focussed = 1;
726 view = focus_view;
727 if (new_view)
728 show_panel(new_view->panel);
729 if (view->child && view_is_splitscreen(view->child))
730 show_panel(view->child->panel);
732 if (view) {
733 if (focus_view == NULL) {
734 view->focussed = 1;
735 show_panel(view->panel);
736 if (view->child && view_is_splitscreen(view->child))
737 show_panel(view->child->panel);
738 focus_view = view;
740 if (view->parent) {
741 err = view->parent->show(view->parent);
742 if (err)
743 goto done;
745 err = view->show(view);
746 if (err)
747 goto done;
748 if (view->child) {
749 err = view->child->show(view->child);
750 if (err)
751 goto done;
753 update_panels();
754 doupdate();
757 done:
758 while (!TAILQ_EMPTY(&views)) {
759 view = TAILQ_FIRST(&views);
760 TAILQ_REMOVE(&views, view, entry);
761 view_close(view);
764 errcode = pthread_mutex_unlock(&tog_mutex);
765 if (errcode)
766 return got_error_set_errno(errcode, "pthread_mutex_unlock");
768 return err;
771 __dead static void
772 usage_log(void)
774 endwin();
775 fprintf(stderr,
776 "usage: %s log [-c commit] [-r repository-path] [path]\n",
777 getprogname());
778 exit(1);
781 /* Create newly allocated wide-character string equivalent to a byte string. */
782 static const struct got_error *
783 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
785 char *vis = NULL;
786 const struct got_error *err = NULL;
788 *ws = NULL;
789 *wlen = mbstowcs(NULL, s, 0);
790 if (*wlen == (size_t)-1) {
791 int vislen;
792 if (errno != EILSEQ)
793 return got_error_from_errno("mbstowcs");
795 /* byte string invalid in current encoding; try to "fix" it */
796 err = got_mbsavis(&vis, &vislen, s);
797 if (err)
798 return err;
799 *wlen = mbstowcs(NULL, vis, 0);
800 if (*wlen == (size_t)-1) {
801 err = got_error_from_errno("mbstowcs"); /* give up */
802 goto done;
806 *ws = calloc(*wlen + 1, sizeof(*ws));
807 if (*ws == NULL) {
808 err = got_error_from_errno("calloc");
809 goto done;
812 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
813 err = got_error_from_errno("mbstowcs");
814 done:
815 free(vis);
816 if (err) {
817 free(*ws);
818 *ws = NULL;
819 *wlen = 0;
821 return err;
824 /* Format a line for display, ensuring that it won't overflow a width limit. */
825 static const struct got_error *
826 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
828 const struct got_error *err = NULL;
829 int cols = 0;
830 wchar_t *wline = NULL;
831 size_t wlen;
832 int i;
834 *wlinep = NULL;
835 *widthp = 0;
837 err = mbs2ws(&wline, &wlen, line);
838 if (err)
839 return err;
841 i = 0;
842 while (i < wlen && cols < wlimit) {
843 int width = wcwidth(wline[i]);
844 switch (width) {
845 case 0:
846 i++;
847 break;
848 case 1:
849 case 2:
850 if (cols + width <= wlimit)
851 cols += width;
852 i++;
853 break;
854 case -1:
855 if (wline[i] == L'\t')
856 cols += TABSIZE - ((cols + 1) % TABSIZE);
857 i++;
858 break;
859 default:
860 err = got_error_from_errno("wcwidth");
861 goto done;
864 wline[i] = L'\0';
865 if (widthp)
866 *widthp = cols;
867 done:
868 if (err)
869 free(wline);
870 else
871 *wlinep = wline;
872 return err;
875 static const struct got_error*
876 build_refs_str(char **refs_str, struct got_reflist_head *refs,
877 struct got_object_id *id)
879 static const struct got_error *err = NULL;
880 struct got_reflist_entry *re;
881 char *s;
882 const char *name;
884 *refs_str = NULL;
886 SIMPLEQ_FOREACH(re, refs, entry) {
887 if (got_object_id_cmp(re->id, id) != 0)
888 continue;
889 name = got_ref_get_name(re->ref);
890 if (strcmp(name, GOT_REF_HEAD) == 0)
891 continue;
892 if (strncmp(name, "refs/", 5) == 0)
893 name += 5;
894 if (strncmp(name, "got/", 4) == 0)
895 continue;
896 if (strncmp(name, "heads/", 6) == 0)
897 name += 6;
898 if (strncmp(name, "remotes/", 8) == 0)
899 name += 8;
900 s = *refs_str;
901 if (asprintf(refs_str, "%s%s%s", s ? s : "",
902 s ? ", " : "", name) == -1) {
903 err = got_error_from_errno("asprintf");
904 free(s);
905 *refs_str = NULL;
906 break;
908 free(s);
911 return err;
914 static const struct got_error *
915 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
917 char *smallerthan, *at;
919 smallerthan = strchr(author, '<');
920 if (smallerthan && smallerthan[1] != '\0')
921 author = smallerthan + 1;
922 at = strchr(author, '@');
923 if (at)
924 *at = '\0';
925 return format_line(wauthor, author_width, author, limit);
928 static const struct got_error *
929 draw_commit(struct tog_view *view, struct got_commit_object *commit,
930 struct got_object_id *id, struct got_reflist_head *refs,
931 int author_display_cols)
933 const struct got_error *err = NULL;
934 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
935 char *logmsg0 = NULL, *logmsg = NULL;
936 char *author = NULL;
937 wchar_t *wlogmsg = NULL, *wauthor = NULL;
938 int author_width, logmsg_width;
939 char *newline, *line = NULL;
940 int col, limit;
941 static const size_t date_display_cols = 9;
942 const int avail = view->ncols;
943 struct tm tm;
944 time_t committer_time;
946 committer_time = got_object_commit_get_committer_time(commit);
947 if (localtime_r(&committer_time, &tm) == NULL)
948 return got_error_from_errno("localtime_r");
949 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
950 >= sizeof(datebuf))
951 return got_error(GOT_ERR_NO_SPACE);
953 if (avail < date_display_cols)
954 limit = MIN(sizeof(datebuf) - 1, avail);
955 else
956 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
957 waddnstr(view->window, datebuf, limit);
958 col = limit + 1;
959 if (col > avail)
960 goto done;
962 author = strdup(got_object_commit_get_author(commit));
963 if (author == NULL) {
964 err = got_error_from_errno("strdup");
965 goto done;
967 err = format_author(&wauthor, &author_width, author, avail - col);
968 if (err)
969 goto done;
970 waddwstr(view->window, wauthor);
971 col += author_width;
972 while (col <= avail && author_width < author_display_cols + 2) {
973 waddch(view->window, ' ');
974 col++;
975 author_width++;
977 if (col > avail)
978 goto done;
980 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
981 if (logmsg0 == NULL) {
982 err = got_error_from_errno("strdup");
983 goto done;
985 logmsg = logmsg0;
986 while (*logmsg == '\n')
987 logmsg++;
988 newline = strchr(logmsg, '\n');
989 if (newline)
990 *newline = '\0';
991 limit = avail - col;
992 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
993 if (err)
994 goto done;
995 waddwstr(view->window, wlogmsg);
996 col += logmsg_width;
997 while (col <= avail) {
998 waddch(view->window, ' ');
999 col++;
1001 done:
1002 free(logmsg0);
1003 free(wlogmsg);
1004 free(author);
1005 free(wauthor);
1006 free(line);
1007 return err;
1010 static struct commit_queue_entry *
1011 alloc_commit_queue_entry(struct got_commit_object *commit,
1012 struct got_object_id *id)
1014 struct commit_queue_entry *entry;
1016 entry = calloc(1, sizeof(*entry));
1017 if (entry == NULL)
1018 return NULL;
1020 entry->id = id;
1021 entry->commit = commit;
1022 return entry;
1025 static void
1026 pop_commit(struct commit_queue *commits)
1028 struct commit_queue_entry *entry;
1030 entry = TAILQ_FIRST(&commits->head);
1031 TAILQ_REMOVE(&commits->head, entry, entry);
1032 got_object_commit_close(entry->commit);
1033 commits->ncommits--;
1034 /* Don't free entry->id! It is owned by the commit graph. */
1035 free(entry);
1038 static void
1039 free_commits(struct commit_queue *commits)
1041 while (!TAILQ_EMPTY(&commits->head))
1042 pop_commit(commits);
1045 static const struct got_error *
1046 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1047 int minqueue, struct got_repository *repo, const char *path)
1049 const struct got_error *err = NULL;
1050 int nqueued = 0;
1053 * We keep all commits open throughout the lifetime of the log
1054 * view in order to avoid having to re-fetch commits from disk
1055 * while updating the display.
1057 while (nqueued < minqueue) {
1058 struct got_object_id *id;
1059 struct got_commit_object *commit;
1060 struct commit_queue_entry *entry;
1061 int errcode;
1063 err = got_commit_graph_iter_next(&id, graph);
1064 if (err) {
1065 if (err->code != GOT_ERR_ITER_NEED_MORE)
1066 break;
1067 err = got_commit_graph_fetch_commits(graph,
1068 minqueue, repo);
1069 if (err)
1070 return err;
1071 continue;
1074 if (id == NULL)
1075 break;
1077 err = got_object_open_as_commit(&commit, repo, id);
1078 if (err)
1079 break;
1080 entry = alloc_commit_queue_entry(commit, id);
1081 if (entry == NULL) {
1082 err = got_error_from_errno("alloc_commit_queue_entry");
1083 break;
1086 errcode = pthread_mutex_lock(&tog_mutex);
1087 if (errcode) {
1088 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1089 break;
1092 entry->idx = commits->ncommits;
1093 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1094 nqueued++;
1095 commits->ncommits++;
1097 errcode = pthread_mutex_unlock(&tog_mutex);
1098 if (errcode && err == NULL)
1099 err = got_error_set_errno(errcode,
1100 "pthread_mutex_unlock");
1103 return err;
1106 static const struct got_error *
1107 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1109 const struct got_error *err = NULL;
1110 struct got_reference *head_ref;
1112 *head_id = NULL;
1114 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1115 if (err)
1116 return err;
1118 err = got_ref_resolve(head_id, repo, head_ref);
1119 got_ref_close(head_ref);
1120 if (err) {
1121 *head_id = NULL;
1122 return err;
1125 return NULL;
1128 static const struct got_error *
1129 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1130 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1131 struct commit_queue *commits, int selected_idx, int limit,
1132 struct got_reflist_head *refs, const char *path, int commits_needed)
1134 const struct got_error *err = NULL;
1135 struct commit_queue_entry *entry;
1136 int ncommits, width;
1137 int author_cols = 10;
1138 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1139 char *refs_str = NULL;
1140 wchar_t *wline;
1142 entry = first;
1143 ncommits = 0;
1144 while (entry) {
1145 if (ncommits == selected_idx) {
1146 *selected = entry;
1147 break;
1149 entry = TAILQ_NEXT(entry, entry);
1150 ncommits++;
1153 if (*selected) {
1154 err = got_object_id_str(&id_str, (*selected)->id);
1155 if (err)
1156 return err;
1157 if (refs) {
1158 err = build_refs_str(&refs_str, refs, (*selected)->id);
1159 if (err)
1160 goto done;
1164 if (commits_needed == 0)
1165 halfdelay(10); /* disable fast refresh */
1167 if (asprintf(&ncommits_str, " [%d/%d] %s",
1168 entry ? entry->idx + 1 : 0, commits->ncommits,
1169 commits_needed > 0 ? "loading... " :
1170 (refs_str ? refs_str : "")) == -1) {
1171 err = got_error_from_errno("asprintf");
1172 goto done;
1175 if (path && strcmp(path, "/") != 0) {
1176 if (asprintf(&header, "commit %s %s%s",
1177 id_str ? id_str : "........................................",
1178 path, ncommits_str) == -1) {
1179 err = got_error_from_errno("asprintf");
1180 header = NULL;
1181 goto done;
1183 } else if (asprintf(&header, "commit %s%s",
1184 id_str ? id_str : "........................................",
1185 ncommits_str) == -1) {
1186 err = got_error_from_errno("asprintf");
1187 header = NULL;
1188 goto done;
1190 err = format_line(&wline, &width, header, view->ncols);
1191 if (err)
1192 goto done;
1194 werase(view->window);
1196 if (view_needs_focus_indication(view))
1197 wstandout(view->window);
1198 waddwstr(view->window, wline);
1199 while (width < view->ncols) {
1200 waddch(view->window, ' ');
1201 width++;
1203 if (view_needs_focus_indication(view))
1204 wstandend(view->window);
1205 free(wline);
1206 if (limit <= 1)
1207 goto done;
1209 /* Grow author column size if necessary. */
1210 entry = first;
1211 ncommits = 0;
1212 while (entry) {
1213 char *author;
1214 wchar_t *wauthor;
1215 int width;
1216 if (ncommits >= limit - 1)
1217 break;
1218 author = strdup(got_object_commit_get_author(entry->commit));
1219 if (author == NULL) {
1220 err = got_error_from_errno("strdup");
1221 goto done;
1223 err = format_author(&wauthor, &width, author, COLS);
1224 if (author_cols < width)
1225 author_cols = width;
1226 free(wauthor);
1227 free(author);
1228 entry = TAILQ_NEXT(entry, entry);
1231 entry = first;
1232 *last = first;
1233 ncommits = 0;
1234 while (entry) {
1235 if (ncommits >= limit - 1)
1236 break;
1237 if (ncommits == selected_idx)
1238 wstandout(view->window);
1239 err = draw_commit(view, entry->commit, entry->id, refs,
1240 author_cols);
1241 if (ncommits == selected_idx)
1242 wstandend(view->window);
1243 if (err)
1244 break;
1245 ncommits++;
1246 *last = entry;
1247 entry = TAILQ_NEXT(entry, entry);
1250 view_vborder(view);
1251 done:
1252 free(id_str);
1253 free(refs_str);
1254 free(ncommits_str);
1255 free(header);
1256 return err;
1259 static void
1260 scroll_up(struct tog_view *view,
1261 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1262 struct commit_queue *commits)
1264 struct commit_queue_entry *entry;
1265 int nscrolled = 0;
1267 entry = TAILQ_FIRST(&commits->head);
1268 if (*first_displayed_entry == entry)
1269 return;
1271 entry = *first_displayed_entry;
1272 while (entry && nscrolled < maxscroll) {
1273 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1274 if (entry) {
1275 *first_displayed_entry = entry;
1276 nscrolled++;
1281 static const struct got_error *
1282 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1283 pthread_cond_t *need_commits)
1285 int errcode;
1286 int max_wait = 20;
1288 halfdelay(1); /* fast refresh while loading commits */
1290 while (*commits_needed > 0) {
1291 if (*log_complete)
1292 break;
1294 /* Wake the log thread. */
1295 errcode = pthread_cond_signal(need_commits);
1296 if (errcode)
1297 return got_error_set_errno(errcode,
1298 "pthread_cond_signal");
1299 errcode = pthread_mutex_unlock(&tog_mutex);
1300 if (errcode)
1301 return got_error_set_errno(errcode,
1302 "pthread_mutex_unlock");
1303 pthread_yield();
1304 errcode = pthread_mutex_lock(&tog_mutex);
1305 if (errcode)
1306 return got_error_set_errno(errcode,
1307 "pthread_mutex_lock");
1309 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1311 * Thread is not done yet; lose a key press
1312 * and let the user retry... this way the GUI
1313 * remains interactive while logging deep paths
1314 * with few commits in history.
1316 return NULL;
1320 return NULL;
1323 static const struct got_error *
1324 scroll_down(struct tog_view *view,
1325 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1326 struct commit_queue_entry **last_displayed_entry,
1327 struct commit_queue *commits, int *log_complete, int *commits_needed,
1328 pthread_cond_t *need_commits)
1330 const struct got_error *err = NULL;
1331 struct commit_queue_entry *pentry;
1332 int nscrolled = 0;
1334 if (*last_displayed_entry == NULL)
1335 return NULL;
1337 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1338 if (pentry == NULL && !*log_complete) {
1340 * Ask the log thread for required amount of commits
1341 * plus some amount of pre-fetching.
1343 (*commits_needed) += maxscroll + 20;
1344 err = trigger_log_thread(0, commits_needed, log_complete,
1345 need_commits);
1346 if (err)
1347 return err;
1350 do {
1351 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1352 if (pentry == NULL)
1353 break;
1355 *last_displayed_entry = pentry;
1357 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1358 if (pentry == NULL)
1359 break;
1360 *first_displayed_entry = pentry;
1361 } while (++nscrolled < maxscroll);
1363 return err;
1366 static const struct got_error *
1367 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1368 struct got_commit_object *commit, struct got_object_id *commit_id,
1369 struct tog_view *log_view, struct got_reflist_head *refs,
1370 struct got_repository *repo)
1372 const struct got_error *err;
1373 struct got_object_qid *parent_id;
1374 struct tog_view *diff_view;
1376 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1377 if (diff_view == NULL)
1378 return got_error_from_errno("view_open");
1380 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1381 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1382 commit_id, log_view, refs, repo);
1383 if (err == NULL)
1384 *new_view = diff_view;
1385 return err;
1388 static const struct got_error *
1389 browse_commit(struct tog_view **new_view, int begin_x,
1390 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1391 struct got_repository *repo)
1393 const struct got_error *err = NULL;
1394 struct got_tree_object *tree;
1395 struct tog_view *tree_view;
1397 err = got_object_open_as_tree(&tree, repo,
1398 got_object_commit_get_tree_id(entry->commit));
1399 if (err)
1400 return err;
1402 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1403 if (tree_view == NULL)
1404 return got_error_from_errno("view_open");
1406 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1407 if (err)
1408 got_object_tree_close(tree);
1409 else
1410 *new_view = tree_view;
1411 return err;
1414 static void *
1415 log_thread(void *arg)
1417 const struct got_error *err = NULL;
1418 int errcode = 0;
1419 struct tog_log_thread_args *a = arg;
1420 int done = 0;
1422 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1423 if (err)
1424 return (void *)err;
1426 while (!done && !err) {
1427 err = queue_commits(a->graph, a->commits, 1, a->repo,
1428 a->in_repo_path);
1429 if (err) {
1430 if (err->code != GOT_ERR_ITER_COMPLETED)
1431 return (void *)err;
1432 err = NULL;
1433 done = 1;
1434 } else if (a->commits_needed > 0)
1435 a->commits_needed--;
1437 errcode = pthread_mutex_lock(&tog_mutex);
1438 if (errcode) {
1439 err = got_error_set_errno(errcode,
1440 "pthread_mutex_lock");
1441 break;
1442 } else if (*a->quit)
1443 done = 1;
1444 else if (*a->first_displayed_entry == NULL) {
1445 *a->first_displayed_entry =
1446 TAILQ_FIRST(&a->commits->head);
1447 *a->selected_entry = *a->first_displayed_entry;
1450 if (done)
1451 a->commits_needed = 0;
1452 else if (a->commits_needed == 0) {
1453 errcode = pthread_cond_wait(&a->need_commits,
1454 &tog_mutex);
1455 if (errcode)
1456 err = got_error_set_errno(errcode,
1457 "pthread_cond_wait");
1460 errcode = pthread_mutex_unlock(&tog_mutex);
1461 if (errcode && err == NULL)
1462 err = got_error_set_errno(errcode,
1463 "pthread_mutex_unlock");
1465 a->log_complete = 1;
1466 return (void *)err;
1469 static const struct got_error *
1470 stop_log_thread(struct tog_log_view_state *s)
1472 const struct got_error *err = NULL;
1473 int errcode;
1475 if (s->thread) {
1476 s->quit = 1;
1477 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1478 if (errcode)
1479 return got_error_set_errno(errcode,
1480 "pthread_cond_signal");
1481 errcode = pthread_mutex_unlock(&tog_mutex);
1482 if (errcode)
1483 return got_error_set_errno(errcode,
1484 "pthread_mutex_unlock");
1485 errcode = pthread_join(s->thread, (void **)&err);
1486 if (errcode)
1487 return got_error_set_errno(errcode, "pthread_join");
1488 errcode = pthread_mutex_lock(&tog_mutex);
1489 if (errcode)
1490 return got_error_set_errno(errcode,
1491 "pthread_mutex_lock");
1492 s->thread = NULL;
1495 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1496 if (errcode && err == NULL)
1497 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1499 if (s->thread_args.repo) {
1500 got_repo_close(s->thread_args.repo);
1501 s->thread_args.repo = NULL;
1504 if (s->thread_args.graph) {
1505 got_commit_graph_close(s->thread_args.graph);
1506 s->thread_args.graph = NULL;
1509 return err;
1512 static const struct got_error *
1513 close_log_view(struct tog_view *view)
1515 const struct got_error *err = NULL;
1516 struct tog_log_view_state *s = &view->state.log;
1518 err = stop_log_thread(s);
1519 free_commits(&s->commits);
1520 free(s->in_repo_path);
1521 s->in_repo_path = NULL;
1522 free(s->start_id);
1523 s->start_id = NULL;
1524 return err;
1527 static const struct got_error *
1528 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1529 struct got_reflist_head *refs, struct got_repository *repo,
1530 const char *path, int check_disk)
1532 const struct got_error *err = NULL;
1533 struct tog_log_view_state *s = &view->state.log;
1534 struct got_repository *thread_repo = NULL;
1535 struct got_commit_graph *thread_graph = NULL;
1536 int errcode;
1538 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1539 if (err != NULL)
1540 goto done;
1542 /* The commit queue only contains commits being displayed. */
1543 TAILQ_INIT(&s->commits.head);
1544 s->commits.ncommits = 0;
1546 s->refs = refs;
1547 s->repo = repo;
1548 s->start_id = got_object_id_dup(start_id);
1549 if (s->start_id == NULL) {
1550 err = got_error_from_errno("got_object_id_dup");
1551 goto done;
1554 view->show = show_log_view;
1555 view->input = input_log_view;
1556 view->close = close_log_view;
1558 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1559 if (err)
1560 goto done;
1561 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1562 0, thread_repo);
1563 if (err)
1564 goto done;
1566 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1567 if (errcode) {
1568 err = got_error_set_errno(errcode, "pthread_cond_init");
1569 goto done;
1572 s->thread_args.commits_needed = view->nlines;
1573 s->thread_args.graph = thread_graph;
1574 s->thread_args.commits = &s->commits;
1575 s->thread_args.in_repo_path = s->in_repo_path;
1576 s->thread_args.start_id = s->start_id;
1577 s->thread_args.repo = thread_repo;
1578 s->thread_args.log_complete = 0;
1579 s->thread_args.quit = &s->quit;
1580 s->thread_args.view = view;
1581 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1582 s->thread_args.selected_entry = &s->selected_entry;
1583 done:
1584 if (err)
1585 close_log_view(view);
1586 return err;
1589 static const struct got_error *
1590 show_log_view(struct tog_view *view)
1592 struct tog_log_view_state *s = &view->state.log;
1594 if (s->thread == NULL) {
1595 int errcode = pthread_create(&s->thread, NULL, log_thread,
1596 &s->thread_args);
1597 if (errcode)
1598 return got_error_set_errno(errcode, "pthread_create");
1601 return draw_commits(view, &s->last_displayed_entry,
1602 &s->selected_entry, s->first_displayed_entry,
1603 &s->commits, s->selected, view->nlines, s->refs,
1604 s->in_repo_path, s->thread_args.commits_needed);
1607 static const struct got_error *
1608 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1609 struct tog_view **focus_view, struct tog_view *view, int ch)
1611 const struct got_error *err = NULL;
1612 struct tog_log_view_state *s = &view->state.log;
1613 char *parent_path;
1614 struct tog_view *diff_view = NULL, *tree_view = NULL;
1615 int begin_x = 0;
1617 switch (ch) {
1618 case 'q':
1619 s->quit = 1;
1620 break;
1621 case 'k':
1622 case KEY_UP:
1623 case '<':
1624 case ',':
1625 if (s->first_displayed_entry == NULL)
1626 break;
1627 if (s->selected > 0)
1628 s->selected--;
1629 if (s->selected > 0)
1630 break;
1631 scroll_up(view, &s->first_displayed_entry, 1,
1632 &s->commits);
1633 break;
1634 case KEY_PPAGE:
1635 case CTRL('b'):
1636 if (s->first_displayed_entry == NULL)
1637 break;
1638 if (TAILQ_FIRST(&s->commits.head) ==
1639 s->first_displayed_entry) {
1640 s->selected = 0;
1641 break;
1643 scroll_up(view, &s->first_displayed_entry,
1644 view->nlines, &s->commits);
1645 break;
1646 case 'j':
1647 case KEY_DOWN:
1648 case '>':
1649 case '.':
1650 if (s->first_displayed_entry == NULL)
1651 break;
1652 if (s->selected < MIN(view->nlines - 2,
1653 s->commits.ncommits - 1)) {
1654 s->selected++;
1655 break;
1657 err = scroll_down(view, &s->first_displayed_entry, 1,
1658 &s->last_displayed_entry, &s->commits,
1659 &s->thread_args.log_complete,
1660 &s->thread_args.commits_needed,
1661 &s->thread_args.need_commits);
1662 break;
1663 case KEY_NPAGE:
1664 case CTRL('f'): {
1665 struct commit_queue_entry *first;
1666 first = s->first_displayed_entry;
1667 if (first == NULL)
1668 break;
1669 err = scroll_down(view, &s->first_displayed_entry,
1670 view->nlines, &s->last_displayed_entry,
1671 &s->commits, &s->thread_args.log_complete,
1672 &s->thread_args.commits_needed,
1673 &s->thread_args.need_commits);
1674 if (first == s->first_displayed_entry &&
1675 s->selected < MIN(view->nlines - 2,
1676 s->commits.ncommits - 1)) {
1677 /* can't scroll further down */
1678 s->selected = MIN(view->nlines - 2,
1679 s->commits.ncommits - 1);
1681 err = NULL;
1682 break;
1684 case KEY_RESIZE:
1685 if (s->selected > view->nlines - 2)
1686 s->selected = view->nlines - 2;
1687 if (s->selected > s->commits.ncommits - 1)
1688 s->selected = s->commits.ncommits - 1;
1689 break;
1690 case KEY_ENTER:
1691 case ' ':
1692 case '\r':
1693 if (s->selected_entry == NULL)
1694 break;
1695 if (view_is_parent_view(view))
1696 begin_x = view_split_begin_x(view->begin_x);
1697 err = open_diff_view_for_commit(&diff_view, begin_x,
1698 s->selected_entry->commit, s->selected_entry->id,
1699 view, s->refs, s->repo);
1700 if (err)
1701 break;
1702 if (view_is_parent_view(view)) {
1703 err = view_close_child(view);
1704 if (err)
1705 return err;
1706 err = view_set_child(view, diff_view);
1707 if (err) {
1708 view_close(diff_view);
1709 break;
1711 *focus_view = diff_view;
1712 view->child_focussed = 1;
1713 } else
1714 *new_view = diff_view;
1715 break;
1716 case 't':
1717 if (s->selected_entry == NULL)
1718 break;
1719 if (view_is_parent_view(view))
1720 begin_x = view_split_begin_x(view->begin_x);
1721 err = browse_commit(&tree_view, begin_x,
1722 s->selected_entry, s->refs, s->repo);
1723 if (err)
1724 break;
1725 if (view_is_parent_view(view)) {
1726 err = view_close_child(view);
1727 if (err)
1728 return err;
1729 err = view_set_child(view, tree_view);
1730 if (err) {
1731 view_close(tree_view);
1732 break;
1734 *focus_view = tree_view;
1735 view->child_focussed = 1;
1736 } else
1737 *new_view = tree_view;
1738 break;
1739 case KEY_BACKSPACE:
1740 if (strcmp(s->in_repo_path, "/") == 0)
1741 break;
1742 parent_path = dirname(s->in_repo_path);
1743 if (parent_path && strcmp(parent_path, ".") != 0) {
1744 struct tog_view *lv;
1745 err = stop_log_thread(s);
1746 if (err)
1747 return err;
1748 lv = view_open(view->nlines, view->ncols,
1749 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1750 if (lv == NULL)
1751 return got_error_from_errno(
1752 "view_open");
1753 err = open_log_view(lv, s->start_id, s->refs,
1754 s->repo, parent_path, 0);
1755 if (err)
1756 return err;;
1757 if (view_is_parent_view(view))
1758 *new_view = lv;
1759 else {
1760 view_set_child(view->parent, lv);
1761 *focus_view = lv;
1763 return NULL;
1765 break;
1766 default:
1767 break;
1770 return err;
1773 static const struct got_error *
1774 apply_unveil(const char *repo_path, const char *worktree_path)
1776 const struct got_error *error;
1778 if (repo_path && unveil(repo_path, "r") != 0)
1779 return got_error_from_errno2("unveil", repo_path);
1781 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1782 return got_error_from_errno2("unveil", worktree_path);
1784 if (unveil("/tmp", "rwc") != 0)
1785 return got_error_from_errno2("unveil", "/tmp");
1787 error = got_privsep_unveil_exec_helpers();
1788 if (error != NULL)
1789 return error;
1791 if (unveil(NULL, NULL) != 0)
1792 return got_error_from_errno("unveil");
1794 return NULL;
1797 static void
1798 init_curses(void)
1800 initscr();
1801 cbreak();
1802 halfdelay(1); /* Do fast refresh while initial view is loading. */
1803 noecho();
1804 nonl();
1805 intrflush(stdscr, FALSE);
1806 keypad(stdscr, TRUE);
1807 curs_set(0);
1808 signal(SIGWINCH, tog_sigwinch);
1811 static const struct got_error *
1812 cmd_log(int argc, char *argv[])
1814 const struct got_error *error;
1815 struct got_repository *repo = NULL;
1816 struct got_worktree *worktree = NULL;
1817 struct got_reflist_head refs;
1818 struct got_object_id *start_id = NULL;
1819 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1820 char *start_commit = NULL;
1821 int ch;
1822 struct tog_view *view;
1824 SIMPLEQ_INIT(&refs);
1826 #ifndef PROFILE
1827 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1828 NULL) == -1)
1829 err(1, "pledge");
1830 #endif
1832 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1833 switch (ch) {
1834 case 'c':
1835 start_commit = optarg;
1836 break;
1837 case 'r':
1838 repo_path = realpath(optarg, NULL);
1839 if (repo_path == NULL)
1840 err(1, "-r option");
1841 break;
1842 default:
1843 usage_log();
1844 /* NOTREACHED */
1848 argc -= optind;
1849 argv += optind;
1851 cwd = getcwd(NULL, 0);
1852 if (cwd == NULL) {
1853 error = got_error_from_errno("getcwd");
1854 goto done;
1856 error = got_worktree_open(&worktree, cwd);
1857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1858 goto done;
1859 error = NULL;
1861 if (argc == 0) {
1862 path = strdup("");
1863 if (path == NULL) {
1864 error = got_error_from_errno("strdup");
1865 goto done;
1867 } else if (argc == 1) {
1868 if (worktree) {
1869 error = got_worktree_resolve_path(&path, worktree,
1870 argv[0]);
1871 if (error)
1872 goto done;
1873 } else {
1874 path = strdup(argv[0]);
1875 if (path == NULL) {
1876 error = got_error_from_errno("strdup");
1877 goto done;
1880 } else
1881 usage_log();
1883 repo_path = worktree ?
1884 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1885 if (repo_path == NULL) {
1886 error = got_error_from_errno("strdup");
1887 goto done;
1890 init_curses();
1892 error = got_repo_open(&repo, repo_path);
1893 if (error != NULL)
1894 goto done;
1896 error = apply_unveil(got_repo_get_path(repo),
1897 worktree ? got_worktree_get_root_path(worktree) : NULL);
1898 if (error)
1899 goto done;
1901 if (start_commit == NULL)
1902 error = get_head_commit_id(&start_id, repo);
1903 else
1904 error = got_object_resolve_id_str(&start_id, repo,
1905 start_commit);
1906 if (error != NULL)
1907 goto done;
1909 error = got_ref_list(&refs, repo);
1910 if (error)
1911 goto done;
1913 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1914 if (view == NULL) {
1915 error = got_error_from_errno("view_open");
1916 goto done;
1918 error = open_log_view(view, start_id, &refs, repo, path, 1);
1919 if (error)
1920 goto done;
1921 error = view_loop(view);
1922 done:
1923 free(repo_path);
1924 free(cwd);
1925 free(path);
1926 free(start_id);
1927 if (repo)
1928 got_repo_close(repo);
1929 if (worktree)
1930 got_worktree_close(worktree);
1931 got_ref_list_free(&refs);
1932 return error;
1935 __dead static void
1936 usage_diff(void)
1938 endwin();
1939 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1940 getprogname());
1941 exit(1);
1944 static char *
1945 parse_next_line(FILE *f, size_t *len)
1947 char *line;
1948 size_t linelen;
1949 size_t lineno;
1950 const char delim[3] = { '\0', '\0', '\0'};
1952 line = fparseln(f, &linelen, &lineno, delim, 0);
1953 if (len)
1954 *len = linelen;
1955 return line;
1958 static const struct got_error *
1959 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1960 int *last_displayed_line, int *eof, int max_lines,
1961 char * header)
1963 const struct got_error *err;
1964 int nlines = 0, nprinted = 0;
1965 char *line;
1966 size_t len;
1967 wchar_t *wline;
1968 int width;
1970 rewind(f);
1971 werase(view->window);
1973 if (header) {
1974 err = format_line(&wline, &width, header, view->ncols);
1975 if (err) {
1976 return err;
1979 if (view_needs_focus_indication(view))
1980 wstandout(view->window);
1981 waddwstr(view->window, wline);
1982 if (view_needs_focus_indication(view))
1983 wstandend(view->window);
1984 if (width < view->ncols)
1985 waddch(view->window, '\n');
1987 if (max_lines <= 1)
1988 return NULL;
1989 max_lines--;
1992 *eof = 0;
1993 while (nprinted < max_lines) {
1994 line = parse_next_line(f, &len);
1995 if (line == NULL) {
1996 *eof = 1;
1997 break;
1999 if (++nlines < *first_displayed_line) {
2000 free(line);
2001 continue;
2004 err = format_line(&wline, &width, line, view->ncols);
2005 if (err) {
2006 free(line);
2007 return err;
2009 waddwstr(view->window, wline);
2010 if (width < view->ncols)
2011 waddch(view->window, '\n');
2012 if (++nprinted == 1)
2013 *first_displayed_line = nlines;
2014 free(line);
2015 free(wline);
2016 wline = NULL;
2018 *last_displayed_line = nlines;
2020 view_vborder(view);
2022 return NULL;
2025 static char *
2026 get_datestr(time_t *time, char *datebuf)
2028 char *p, *s = ctime_r(time, datebuf);
2029 p = strchr(s, '\n');
2030 if (p)
2031 *p = '\0';
2032 return s;
2035 static const struct got_error *
2036 write_commit_info(struct got_object_id *commit_id,
2037 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2039 const struct got_error *err = NULL;
2040 char datebuf[26];
2041 struct got_commit_object *commit;
2042 char *id_str = NULL;
2043 time_t committer_time;
2044 const char *author, *committer;
2045 char *refs_str = NULL;
2047 if (refs) {
2048 err = build_refs_str(&refs_str, refs, commit_id);
2049 if (err)
2050 return err;
2053 err = got_object_open_as_commit(&commit, repo, commit_id);
2054 if (err)
2055 return err;
2057 err = got_object_id_str(&id_str, commit_id);
2058 if (err) {
2059 err = got_error_from_errno("got_object_id_str");
2060 goto done;
2063 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2064 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2065 err = got_error_from_errno("fprintf");
2066 goto done;
2068 if (fprintf(outfile, "from: %s\n",
2069 got_object_commit_get_author(commit)) < 0) {
2070 err = got_error_from_errno("fprintf");
2071 goto done;
2073 committer_time = got_object_commit_get_committer_time(commit);
2074 if (fprintf(outfile, "date: %s UTC\n",
2075 get_datestr(&committer_time, datebuf)) < 0) {
2076 err = got_error_from_errno("fprintf");
2077 goto done;
2079 author = got_object_commit_get_author(commit);
2080 committer = got_object_commit_get_committer(commit);
2081 if (strcmp(author, committer) != 0 &&
2082 fprintf(outfile, "via: %s\n", committer) < 0) {
2083 err = got_error_from_errno("fprintf");
2084 goto done;
2086 if (fprintf(outfile, "%s\n",
2087 got_object_commit_get_logmsg(commit)) < 0) {
2088 err = got_error_from_errno("fprintf");
2089 goto done;
2091 done:
2092 free(id_str);
2093 free(refs_str);
2094 got_object_commit_close(commit);
2095 return err;
2098 static const struct got_error *
2099 create_diff(struct tog_diff_view_state *s)
2101 const struct got_error *err = NULL;
2102 FILE *f = NULL;
2103 int obj_type;
2105 f = got_opentemp();
2106 if (f == NULL) {
2107 err = got_error_from_errno("got_opentemp");
2108 goto done;
2110 if (s->f && fclose(s->f) != 0) {
2111 err = got_error_from_errno("fclose");
2112 goto done;
2114 s->f = f;
2116 if (s->id1)
2117 err = got_object_get_type(&obj_type, s->repo, s->id1);
2118 else
2119 err = got_object_get_type(&obj_type, s->repo, s->id2);
2120 if (err)
2121 goto done;
2123 switch (obj_type) {
2124 case GOT_OBJ_TYPE_BLOB:
2125 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2126 s->diff_context, s->repo, f);
2127 break;
2128 case GOT_OBJ_TYPE_TREE:
2129 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2130 s->diff_context, s->repo, f);
2131 break;
2132 case GOT_OBJ_TYPE_COMMIT: {
2133 const struct got_object_id_queue *parent_ids;
2134 struct got_object_qid *pid;
2135 struct got_commit_object *commit2;
2137 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2138 if (err)
2139 break;
2140 /* Show commit info if we're diffing to a parent/root commit. */
2141 if (s->id1 == NULL)
2142 write_commit_info(s->id2, s->refs, s->repo, f);
2143 else {
2144 parent_ids = got_object_commit_get_parent_ids(commit2);
2145 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2146 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2147 write_commit_info(s->id2, s->refs,
2148 s->repo, f);
2149 break;
2153 got_object_commit_close(commit2);
2155 err = got_diff_objects_as_commits(s->id1, s->id2,
2156 s->diff_context, s->repo, f);
2157 break;
2159 default:
2160 err = got_error(GOT_ERR_OBJ_TYPE);
2161 break;
2163 done:
2164 if (f && fflush(f) != 0 && err == NULL)
2165 err = got_error_from_errno("fflush");
2166 return err;
2169 static void
2170 diff_view_indicate_progress(struct tog_view *view)
2172 werase(view->window);
2173 waddstr(view->window, "diffing...");
2174 update_panels();
2175 doupdate();
2178 static const struct got_error *
2179 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2180 struct got_object_id *id2, struct tog_view *log_view,
2181 struct got_reflist_head *refs, struct got_repository *repo)
2183 const struct got_error *err;
2185 if (id1 != NULL && id2 != NULL) {
2186 int type1, type2;
2187 err = got_object_get_type(&type1, repo, id1);
2188 if (err)
2189 return err;
2190 err = got_object_get_type(&type2, repo, id2);
2191 if (err)
2192 return err;
2194 if (type1 != type2)
2195 return got_error(GOT_ERR_OBJ_TYPE);
2198 if (id1) {
2199 view->state.diff.id1 = got_object_id_dup(id1);
2200 if (view->state.diff.id1 == NULL)
2201 return got_error_from_errno("got_object_id_dup");
2202 } else
2203 view->state.diff.id1 = NULL;
2205 view->state.diff.id2 = got_object_id_dup(id2);
2206 if (view->state.diff.id2 == NULL) {
2207 free(view->state.diff.id1);
2208 view->state.diff.id1 = NULL;
2209 return got_error_from_errno("got_object_id_dup");
2211 view->state.diff.f = NULL;
2212 view->state.diff.first_displayed_line = 1;
2213 view->state.diff.last_displayed_line = view->nlines;
2214 view->state.diff.diff_context = 3;
2215 view->state.diff.log_view = log_view;
2216 view->state.diff.repo = repo;
2217 view->state.diff.refs = refs;
2219 if (log_view && view_is_splitscreen(view))
2220 show_log_view(log_view); /* draw vborder */
2221 diff_view_indicate_progress(view);
2223 err = create_diff(&view->state.diff);
2224 if (err) {
2225 free(view->state.diff.id1);
2226 view->state.diff.id1 = NULL;
2227 free(view->state.diff.id2);
2228 view->state.diff.id2 = NULL;
2229 return err;
2232 view->show = show_diff_view;
2233 view->input = input_diff_view;
2234 view->close = close_diff_view;
2236 return NULL;
2239 static const struct got_error *
2240 close_diff_view(struct tog_view *view)
2242 const struct got_error *err = NULL;
2244 free(view->state.diff.id1);
2245 view->state.diff.id1 = NULL;
2246 free(view->state.diff.id2);
2247 view->state.diff.id2 = NULL;
2248 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2249 err = got_error_from_errno("fclose");
2250 return err;
2253 static const struct got_error *
2254 show_diff_view(struct tog_view *view)
2256 const struct got_error *err;
2257 struct tog_diff_view_state *s = &view->state.diff;
2258 char *id_str1 = NULL, *id_str2, *header;
2260 if (s->id1) {
2261 err = got_object_id_str(&id_str1, s->id1);
2262 if (err)
2263 return err;
2265 err = got_object_id_str(&id_str2, s->id2);
2266 if (err)
2267 return err;
2269 if (asprintf(&header, "diff %s %s",
2270 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2271 err = got_error_from_errno("asprintf");
2272 free(id_str1);
2273 free(id_str2);
2274 return err;
2276 free(id_str1);
2277 free(id_str2);
2279 return draw_file(view, s->f, &s->first_displayed_line,
2280 &s->last_displayed_line, &s->eof, view->nlines,
2281 header);
2284 static const struct got_error *
2285 set_selected_commit(struct tog_diff_view_state *s,
2286 struct commit_queue_entry *entry)
2288 const struct got_error *err;
2289 const struct got_object_id_queue *parent_ids;
2290 struct got_commit_object *selected_commit;
2291 struct got_object_qid *pid;
2293 free(s->id2);
2294 s->id2 = got_object_id_dup(entry->id);
2295 if (s->id2 == NULL)
2296 return got_error_from_errno("got_object_id_dup");
2298 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2299 if (err)
2300 return err;
2301 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2302 free(s->id1);
2303 pid = SIMPLEQ_FIRST(parent_ids);
2304 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2305 got_object_commit_close(selected_commit);
2306 return NULL;
2309 static const struct got_error *
2310 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2311 struct tog_view **focus_view, struct tog_view *view, int ch)
2313 const struct got_error *err = NULL;
2314 struct tog_diff_view_state *s = &view->state.diff;
2315 struct tog_log_view_state *ls;
2316 struct commit_queue_entry *entry;
2317 int i;
2319 switch (ch) {
2320 case 'k':
2321 case KEY_UP:
2322 if (s->first_displayed_line > 1)
2323 s->first_displayed_line--;
2324 break;
2325 case KEY_PPAGE:
2326 if (s->first_displayed_line == 1)
2327 break;
2328 i = 0;
2329 while (i++ < view->nlines - 1 &&
2330 s->first_displayed_line > 1)
2331 s->first_displayed_line--;
2332 break;
2333 case 'j':
2334 case KEY_DOWN:
2335 if (!s->eof)
2336 s->first_displayed_line++;
2337 break;
2338 case KEY_NPAGE:
2339 case ' ':
2340 if (s->eof)
2341 break;
2342 i = 0;
2343 while (!s->eof && i++ < view->nlines - 1) {
2344 char *line;
2345 line = parse_next_line(s->f, NULL);
2346 s->first_displayed_line++;
2347 if (line == NULL)
2348 break;
2350 break;
2351 case '[':
2352 if (s->diff_context > 0) {
2353 s->diff_context--;
2354 diff_view_indicate_progress(view);
2355 err = create_diff(s);
2357 break;
2358 case ']':
2359 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2360 s->diff_context++;
2361 diff_view_indicate_progress(view);
2362 err = create_diff(s);
2364 break;
2365 case '<':
2366 case ',':
2367 if (s->log_view == NULL)
2368 break;
2369 ls = &s->log_view->state.log;
2370 entry = TAILQ_PREV(ls->selected_entry,
2371 commit_queue_head, entry);
2372 if (entry == NULL)
2373 break;
2375 err = input_log_view(NULL, NULL, NULL, s->log_view,
2376 KEY_UP);
2377 if (err)
2378 break;
2380 err = set_selected_commit(s, entry);
2381 if (err)
2382 break;
2384 s->first_displayed_line = 1;
2385 s->last_displayed_line = view->nlines;
2387 diff_view_indicate_progress(view);
2388 err = create_diff(s);
2389 break;
2390 case '>':
2391 case '.':
2392 if (s->log_view == NULL)
2393 break;
2394 ls = &s->log_view->state.log;
2396 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2397 ls->thread_args.commits_needed++;
2399 /* Display "loading..." in log view. */
2400 show_log_view(s->log_view);
2401 update_panels();
2402 doupdate();
2404 err = trigger_log_thread(1 /* load_all */,
2405 &ls->thread_args.commits_needed,
2406 &ls->thread_args.log_complete,
2407 &ls->thread_args.need_commits);
2408 if (err)
2409 break;
2411 err = input_log_view(NULL, NULL, NULL, s->log_view,
2412 KEY_DOWN);
2413 if (err)
2414 break;
2416 entry = TAILQ_NEXT(ls->selected_entry, entry);
2417 if (entry == NULL)
2418 break;
2420 err = set_selected_commit(s, entry);
2421 if (err)
2422 break;
2424 s->first_displayed_line = 1;
2425 s->last_displayed_line = view->nlines;
2427 diff_view_indicate_progress(view);
2428 err = create_diff(s);
2429 break;
2430 default:
2431 break;
2434 return err;
2437 static const struct got_error *
2438 cmd_diff(int argc, char *argv[])
2440 const struct got_error *error = NULL;
2441 struct got_repository *repo = NULL;
2442 struct got_reflist_head refs;
2443 struct got_object_id *id1 = NULL, *id2 = NULL;
2444 char *repo_path = NULL;
2445 char *id_str1 = NULL, *id_str2 = NULL;
2446 int ch;
2447 struct tog_view *view;
2449 SIMPLEQ_INIT(&refs);
2451 #ifndef PROFILE
2452 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2453 NULL) == -1)
2454 err(1, "pledge");
2455 #endif
2457 while ((ch = getopt(argc, argv, "")) != -1) {
2458 switch (ch) {
2459 default:
2460 usage_diff();
2461 /* NOTREACHED */
2465 argc -= optind;
2466 argv += optind;
2468 if (argc == 0) {
2469 usage_diff(); /* TODO show local worktree changes */
2470 } else if (argc == 2) {
2471 repo_path = getcwd(NULL, 0);
2472 if (repo_path == NULL)
2473 return got_error_from_errno("getcwd");
2474 id_str1 = argv[0];
2475 id_str2 = argv[1];
2476 } else if (argc == 3) {
2477 repo_path = realpath(argv[0], NULL);
2478 if (repo_path == NULL)
2479 return got_error_from_errno2("realpath", argv[0]);
2480 id_str1 = argv[1];
2481 id_str2 = argv[2];
2482 } else
2483 usage_diff();
2485 init_curses();
2487 error = got_repo_open(&repo, repo_path);
2488 if (error)
2489 goto done;
2491 error = apply_unveil(got_repo_get_path(repo), NULL);
2492 if (error)
2493 goto done;
2495 error = got_object_resolve_id_str(&id1, repo, id_str1);
2496 if (error)
2497 goto done;
2499 error = got_object_resolve_id_str(&id2, repo, id_str2);
2500 if (error)
2501 goto done;
2503 error = got_ref_list(&refs, repo);
2504 if (error)
2505 goto done;
2507 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2508 if (view == NULL) {
2509 error = got_error_from_errno("view_open");
2510 goto done;
2512 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2513 if (error)
2514 goto done;
2515 error = view_loop(view);
2516 done:
2517 free(repo_path);
2518 got_repo_close(repo);
2519 got_ref_list_free(&refs);
2520 return error;
2523 __dead static void
2524 usage_blame(void)
2526 endwin();
2527 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2528 getprogname());
2529 exit(1);
2532 struct tog_blame_line {
2533 int annotated;
2534 struct got_object_id *id;
2537 static const struct got_error *
2538 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2539 const char *path, struct tog_blame_line *lines, int nlines,
2540 int blame_complete, int selected_line, int *first_displayed_line,
2541 int *last_displayed_line, int *eof, int max_lines)
2543 const struct got_error *err;
2544 int lineno = 0, nprinted = 0;
2545 char *line;
2546 size_t len;
2547 wchar_t *wline;
2548 int width, wlimit;
2549 struct tog_blame_line *blame_line;
2550 struct got_object_id *prev_id = NULL;
2551 char *id_str;
2553 err = got_object_id_str(&id_str, id);
2554 if (err)
2555 return err;
2557 rewind(f);
2558 werase(view->window);
2560 if (asprintf(&line, "commit %s", id_str) == -1) {
2561 err = got_error_from_errno("asprintf");
2562 free(id_str);
2563 return err;
2566 err = format_line(&wline, &width, line, view->ncols);
2567 free(line);
2568 line = NULL;
2569 if (view_needs_focus_indication(view))
2570 wstandout(view->window);
2571 waddwstr(view->window, wline);
2572 if (view_needs_focus_indication(view))
2573 wstandend(view->window);
2574 free(wline);
2575 wline = NULL;
2576 if (width < view->ncols)
2577 waddch(view->window, '\n');
2579 if (asprintf(&line, "[%d/%d] %s%s",
2580 *first_displayed_line - 1 + selected_line, nlines,
2581 blame_complete ? "" : "annotating... ", path) == -1) {
2582 free(id_str);
2583 return got_error_from_errno("asprintf");
2585 free(id_str);
2586 err = format_line(&wline, &width, line, view->ncols);
2587 free(line);
2588 line = NULL;
2589 if (err)
2590 return err;
2591 waddwstr(view->window, wline);
2592 free(wline);
2593 wline = NULL;
2594 if (width < view->ncols)
2595 waddch(view->window, '\n');
2597 *eof = 0;
2598 while (nprinted < max_lines - 2) {
2599 line = parse_next_line(f, &len);
2600 if (line == NULL) {
2601 *eof = 1;
2602 break;
2604 if (++lineno < *first_displayed_line) {
2605 free(line);
2606 continue;
2609 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2610 err = format_line(&wline, &width, line, wlimit);
2611 if (err) {
2612 free(line);
2613 return err;
2616 if (view->focussed && nprinted == selected_line - 1)
2617 wstandout(view->window);
2619 blame_line = &lines[lineno - 1];
2620 if (blame_line->annotated && prev_id &&
2621 got_object_id_cmp(prev_id, blame_line->id) == 0)
2622 waddstr(view->window, " ");
2623 else if (blame_line->annotated) {
2624 char *id_str;
2625 err = got_object_id_str(&id_str, blame_line->id);
2626 if (err) {
2627 free(line);
2628 free(wline);
2629 return err;
2631 wprintw(view->window, "%.8s ", id_str);
2632 free(id_str);
2633 prev_id = blame_line->id;
2634 } else {
2635 waddstr(view->window, "........ ");
2636 prev_id = NULL;
2639 waddwstr(view->window, wline);
2640 while (width < wlimit) {
2641 waddch(view->window, ' ');
2642 width++;
2644 if (view->focussed && nprinted == selected_line - 1)
2645 wstandend(view->window);
2646 if (++nprinted == 1)
2647 *first_displayed_line = lineno;
2648 free(line);
2649 free(wline);
2650 wline = NULL;
2652 *last_displayed_line = lineno;
2654 view_vborder(view);
2656 return NULL;
2659 static const struct got_error *
2660 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2662 const struct got_error *err = NULL;
2663 struct tog_blame_cb_args *a = arg;
2664 struct tog_blame_line *line;
2665 int errcode;
2667 if (nlines != a->nlines ||
2668 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2669 return got_error(GOT_ERR_RANGE);
2671 errcode = pthread_mutex_lock(&tog_mutex);
2672 if (errcode)
2673 return got_error_set_errno(errcode, "pthread_mutex_lock");
2675 if (*a->quit) { /* user has quit the blame view */
2676 err = got_error(GOT_ERR_ITER_COMPLETED);
2677 goto done;
2680 if (lineno == -1)
2681 goto done; /* no change in this commit */
2683 line = &a->lines[lineno - 1];
2684 if (line->annotated)
2685 goto done;
2687 line->id = got_object_id_dup(id);
2688 if (line->id == NULL) {
2689 err = got_error_from_errno("got_object_id_dup");
2690 goto done;
2692 line->annotated = 1;
2693 done:
2694 errcode = pthread_mutex_unlock(&tog_mutex);
2695 if (errcode)
2696 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2697 return err;
2700 static void *
2701 blame_thread(void *arg)
2703 const struct got_error *err;
2704 struct tog_blame_thread_args *ta = arg;
2705 struct tog_blame_cb_args *a = ta->cb_args;
2706 int errcode;
2708 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2709 blame_cb, ta->cb_args);
2711 errcode = pthread_mutex_lock(&tog_mutex);
2712 if (errcode)
2713 return (void *)got_error_set_errno(errcode,
2714 "pthread_mutex_lock");
2716 got_repo_close(ta->repo);
2717 ta->repo = NULL;
2718 *ta->complete = 1;
2720 errcode = pthread_mutex_unlock(&tog_mutex);
2721 if (errcode && err == NULL)
2722 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2724 return (void *)err;
2727 static struct got_object_id *
2728 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2729 int selected_line)
2731 struct tog_blame_line *line;
2733 line = &lines[first_displayed_line - 1 + selected_line - 1];
2734 if (!line->annotated)
2735 return NULL;
2737 return line->id;
2740 static const struct got_error *
2741 stop_blame(struct tog_blame *blame)
2743 const struct got_error *err = NULL;
2744 int i;
2746 if (blame->thread) {
2747 int errcode;
2748 errcode = pthread_mutex_unlock(&tog_mutex);
2749 if (errcode)
2750 return got_error_set_errno(errcode,
2751 "pthread_mutex_unlock");
2752 errcode = pthread_join(blame->thread, (void **)&err);
2753 if (errcode)
2754 return got_error_set_errno(errcode, "pthread_join");
2755 errcode = pthread_mutex_lock(&tog_mutex);
2756 if (errcode)
2757 return got_error_set_errno(errcode,
2758 "pthread_mutex_lock");
2759 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2760 err = NULL;
2761 blame->thread = NULL;
2763 if (blame->thread_args.repo) {
2764 got_repo_close(blame->thread_args.repo);
2765 blame->thread_args.repo = NULL;
2767 if (blame->f) {
2768 if (fclose(blame->f) != 0 && err == NULL)
2769 err = got_error_from_errno("fclose");
2770 blame->f = NULL;
2772 if (blame->lines) {
2773 for (i = 0; i < blame->nlines; i++)
2774 free(blame->lines[i].id);
2775 free(blame->lines);
2776 blame->lines = NULL;
2778 free(blame->cb_args.commit_id);
2779 blame->cb_args.commit_id = NULL;
2781 return err;
2784 static const struct got_error *
2785 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2786 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2787 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2788 struct got_repository *repo)
2790 const struct got_error *err = NULL;
2791 struct got_blob_object *blob = NULL;
2792 struct got_repository *thread_repo = NULL;
2793 struct got_object_id *obj_id = NULL;
2794 int obj_type;
2796 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2797 if (err)
2798 return err;
2799 if (obj_id == NULL)
2800 return got_error(GOT_ERR_NO_OBJ);
2802 err = got_object_get_type(&obj_type, repo, obj_id);
2803 if (err)
2804 goto done;
2806 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2807 err = got_error(GOT_ERR_OBJ_TYPE);
2808 goto done;
2811 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2812 if (err)
2813 goto done;
2814 blame->f = got_opentemp();
2815 if (blame->f == NULL) {
2816 err = got_error_from_errno("got_opentemp");
2817 goto done;
2819 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2820 blame->f, blob);
2821 if (err)
2822 goto done;
2824 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2825 if (blame->lines == NULL) {
2826 err = got_error_from_errno("calloc");
2827 goto done;
2830 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2831 if (err)
2832 goto done;
2834 blame->cb_args.view = view;
2835 blame->cb_args.lines = blame->lines;
2836 blame->cb_args.nlines = blame->nlines;
2837 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2838 if (blame->cb_args.commit_id == NULL) {
2839 err = got_error_from_errno("got_object_id_dup");
2840 goto done;
2842 blame->cb_args.quit = done;
2844 blame->thread_args.path = path;
2845 blame->thread_args.repo = thread_repo;
2846 blame->thread_args.cb_args = &blame->cb_args;
2847 blame->thread_args.complete = blame_complete;
2848 *blame_complete = 0;
2850 done:
2851 if (blob)
2852 got_object_blob_close(blob);
2853 free(obj_id);
2854 if (err)
2855 stop_blame(blame);
2856 return err;
2859 static const struct got_error *
2860 open_blame_view(struct tog_view *view, char *path,
2861 struct got_object_id *commit_id, struct got_reflist_head *refs,
2862 struct got_repository *repo)
2864 const struct got_error *err = NULL;
2865 struct tog_blame_view_state *s = &view->state.blame;
2867 SIMPLEQ_INIT(&s->blamed_commits);
2869 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2870 if (err)
2871 return err;
2873 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2874 s->first_displayed_line = 1;
2875 s->last_displayed_line = view->nlines;
2876 s->selected_line = 1;
2877 s->blame_complete = 0;
2878 s->path = path;
2879 if (s->path == NULL)
2880 return got_error_from_errno("open_blame_view");
2881 s->repo = repo;
2882 s->refs = refs;
2883 s->commit_id = commit_id;
2884 memset(&s->blame, 0, sizeof(s->blame));
2886 view->show = show_blame_view;
2887 view->input = input_blame_view;
2888 view->close = close_blame_view;
2890 return run_blame(&s->blame, view, &s->blame_complete,
2891 &s->first_displayed_line, &s->last_displayed_line,
2892 &s->selected_line, &s->done, &s->eof, s->path,
2893 s->blamed_commit->id, s->repo);
2896 static const struct got_error *
2897 close_blame_view(struct tog_view *view)
2899 const struct got_error *err = NULL;
2900 struct tog_blame_view_state *s = &view->state.blame;
2902 if (s->blame.thread)
2903 err = stop_blame(&s->blame);
2905 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2906 struct got_object_qid *blamed_commit;
2907 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2908 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2909 got_object_qid_free(blamed_commit);
2912 free(s->path);
2914 return err;
2917 static const struct got_error *
2918 show_blame_view(struct tog_view *view)
2920 const struct got_error *err = NULL;
2921 struct tog_blame_view_state *s = &view->state.blame;
2922 int errcode;
2924 if (s->blame.thread == NULL) {
2925 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2926 &s->blame.thread_args);
2927 if (errcode)
2928 return got_error_set_errno(errcode, "pthread_create");
2931 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2932 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2933 s->selected_line, &s->first_displayed_line,
2934 &s->last_displayed_line, &s->eof, view->nlines);
2936 view_vborder(view);
2937 return err;
2940 static const struct got_error *
2941 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2942 struct tog_view **focus_view, struct tog_view *view, int ch)
2944 const struct got_error *err = NULL, *thread_err = NULL;
2945 struct tog_view *diff_view;
2946 struct tog_blame_view_state *s = &view->state.blame;
2947 int begin_x = 0;
2949 switch (ch) {
2950 case 'q':
2951 s->done = 1;
2952 break;
2953 case 'k':
2954 case KEY_UP:
2955 if (s->selected_line > 1)
2956 s->selected_line--;
2957 else if (s->selected_line == 1 &&
2958 s->first_displayed_line > 1)
2959 s->first_displayed_line--;
2960 break;
2961 case KEY_PPAGE:
2962 if (s->first_displayed_line == 1) {
2963 s->selected_line = 1;
2964 break;
2966 if (s->first_displayed_line > view->nlines - 2)
2967 s->first_displayed_line -=
2968 (view->nlines - 2);
2969 else
2970 s->first_displayed_line = 1;
2971 break;
2972 case 'j':
2973 case KEY_DOWN:
2974 if (s->selected_line < view->nlines - 2 &&
2975 s->first_displayed_line +
2976 s->selected_line <= s->blame.nlines)
2977 s->selected_line++;
2978 else if (s->last_displayed_line <
2979 s->blame.nlines)
2980 s->first_displayed_line++;
2981 break;
2982 case 'b':
2983 case 'p': {
2984 struct got_object_id *id = NULL;
2985 id = get_selected_commit_id(s->blame.lines,
2986 s->first_displayed_line, s->selected_line);
2987 if (id == NULL)
2988 break;
2989 if (ch == 'p') {
2990 struct got_commit_object *commit;
2991 struct got_object_qid *pid;
2992 struct got_object_id *blob_id = NULL;
2993 int obj_type;
2994 err = got_object_open_as_commit(&commit,
2995 s->repo, id);
2996 if (err)
2997 break;
2998 pid = SIMPLEQ_FIRST(
2999 got_object_commit_get_parent_ids(commit));
3000 if (pid == NULL) {
3001 got_object_commit_close(commit);
3002 break;
3004 /* Check if path history ends here. */
3005 err = got_object_id_by_path(&blob_id, s->repo,
3006 pid->id, s->path);
3007 if (err) {
3008 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3009 err = NULL;
3010 got_object_commit_close(commit);
3011 break;
3013 err = got_object_get_type(&obj_type, s->repo,
3014 blob_id);
3015 free(blob_id);
3016 /* Can't blame non-blob type objects. */
3017 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3018 got_object_commit_close(commit);
3019 break;
3021 err = got_object_qid_alloc(&s->blamed_commit,
3022 pid->id);
3023 got_object_commit_close(commit);
3024 } else {
3025 if (got_object_id_cmp(id,
3026 s->blamed_commit->id) == 0)
3027 break;
3028 err = got_object_qid_alloc(&s->blamed_commit,
3029 id);
3031 if (err)
3032 break;
3033 s->done = 1;
3034 thread_err = stop_blame(&s->blame);
3035 s->done = 0;
3036 if (thread_err)
3037 break;
3038 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3039 s->blamed_commit, entry);
3040 err = run_blame(&s->blame, view, &s->blame_complete,
3041 &s->first_displayed_line, &s->last_displayed_line,
3042 &s->selected_line, &s->done, &s->eof,
3043 s->path, s->blamed_commit->id, s->repo);
3044 if (err)
3045 break;
3046 break;
3048 case 'B': {
3049 struct got_object_qid *first;
3050 first = SIMPLEQ_FIRST(&s->blamed_commits);
3051 if (!got_object_id_cmp(first->id, s->commit_id))
3052 break;
3053 s->done = 1;
3054 thread_err = stop_blame(&s->blame);
3055 s->done = 0;
3056 if (thread_err)
3057 break;
3058 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3059 got_object_qid_free(s->blamed_commit);
3060 s->blamed_commit =
3061 SIMPLEQ_FIRST(&s->blamed_commits);
3062 err = run_blame(&s->blame, view, &s->blame_complete,
3063 &s->first_displayed_line, &s->last_displayed_line,
3064 &s->selected_line, &s->done, &s->eof, s->path,
3065 s->blamed_commit->id, s->repo);
3066 if (err)
3067 break;
3068 break;
3070 case KEY_ENTER:
3071 case '\r': {
3072 struct got_object_id *id = NULL;
3073 struct got_object_qid *pid;
3074 struct got_commit_object *commit = NULL;
3075 id = get_selected_commit_id(s->blame.lines,
3076 s->first_displayed_line, s->selected_line);
3077 if (id == NULL)
3078 break;
3079 err = got_object_open_as_commit(&commit, s->repo, id);
3080 if (err)
3081 break;
3082 pid = SIMPLEQ_FIRST(
3083 got_object_commit_get_parent_ids(commit));
3084 if (view_is_parent_view(view))
3085 begin_x = view_split_begin_x(view->begin_x);
3086 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3087 if (diff_view == NULL) {
3088 got_object_commit_close(commit);
3089 err = got_error_from_errno("view_open");
3090 break;
3092 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3093 id, NULL, s->refs, s->repo);
3094 got_object_commit_close(commit);
3095 if (err) {
3096 view_close(diff_view);
3097 break;
3099 if (view_is_parent_view(view)) {
3100 err = view_close_child(view);
3101 if (err)
3102 break;
3103 err = view_set_child(view, diff_view);
3104 if (err) {
3105 view_close(diff_view);
3106 break;
3108 *focus_view = diff_view;
3109 view->child_focussed = 1;
3110 } else
3111 *new_view = diff_view;
3112 if (err)
3113 break;
3114 break;
3116 case KEY_NPAGE:
3117 case ' ':
3118 if (s->last_displayed_line >= s->blame.nlines &&
3119 s->selected_line >= MIN(s->blame.nlines,
3120 view->nlines - 2)) {
3121 break;
3123 if (s->last_displayed_line >= s->blame.nlines &&
3124 s->selected_line < view->nlines - 2) {
3125 s->selected_line = MIN(s->blame.nlines,
3126 view->nlines - 2);
3127 break;
3129 if (s->last_displayed_line + view->nlines - 2
3130 <= s->blame.nlines)
3131 s->first_displayed_line +=
3132 view->nlines - 2;
3133 else
3134 s->first_displayed_line =
3135 s->blame.nlines -
3136 (view->nlines - 3);
3137 break;
3138 case KEY_RESIZE:
3139 if (s->selected_line > view->nlines - 2) {
3140 s->selected_line = MIN(s->blame.nlines,
3141 view->nlines - 2);
3143 break;
3144 default:
3145 break;
3147 return thread_err ? thread_err : err;
3150 static const struct got_error *
3151 cmd_blame(int argc, char *argv[])
3153 const struct got_error *error;
3154 struct got_repository *repo = NULL;
3155 struct got_reflist_head refs;
3156 struct got_worktree *worktree = NULL;
3157 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3158 struct got_object_id *commit_id = NULL;
3159 char *commit_id_str = NULL;
3160 int ch;
3161 struct tog_view *view;
3163 SIMPLEQ_INIT(&refs);
3165 #ifndef PROFILE
3166 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3167 NULL) == -1)
3168 err(1, "pledge");
3169 #endif
3171 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3172 switch (ch) {
3173 case 'c':
3174 commit_id_str = optarg;
3175 break;
3176 case 'r':
3177 repo_path = realpath(optarg, NULL);
3178 if (repo_path == NULL)
3179 err(1, "-r option");
3180 break;
3181 default:
3182 usage_blame();
3183 /* NOTREACHED */
3187 argc -= optind;
3188 argv += optind;
3190 if (argc == 1)
3191 path = argv[0];
3192 else
3193 usage_blame();
3195 cwd = getcwd(NULL, 0);
3196 if (cwd == NULL) {
3197 error = got_error_from_errno("getcwd");
3198 goto done;
3200 if (repo_path == NULL) {
3201 error = got_worktree_open(&worktree, cwd);
3202 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3203 goto done;
3204 else
3205 error = NULL;
3206 if (worktree) {
3207 repo_path =
3208 strdup(got_worktree_get_repo_path(worktree));
3209 if (repo_path == NULL)
3210 error = got_error_from_errno("strdup");
3211 if (error)
3212 goto done;
3213 } else {
3214 repo_path = strdup(cwd);
3215 if (repo_path == NULL) {
3216 error = got_error_from_errno("strdup");
3217 goto done;
3222 init_curses();
3224 error = got_repo_open(&repo, repo_path);
3225 if (error != NULL)
3226 goto done;
3228 error = apply_unveil(got_repo_get_path(repo), NULL);
3229 if (error)
3230 goto done;
3232 if (worktree) {
3233 const char *prefix = got_worktree_get_path_prefix(worktree);
3234 char *p, *worktree_subdir = cwd +
3235 strlen(got_worktree_get_root_path(worktree));
3236 if (asprintf(&p, "%s%s%s%s%s",
3237 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3238 worktree_subdir, worktree_subdir[0] ? "/" : "",
3239 path) == -1) {
3240 error = got_error_from_errno("asprintf");
3241 goto done;
3243 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3244 free(p);
3245 } else {
3246 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3248 if (error)
3249 goto done;
3251 if (commit_id_str == NULL) {
3252 struct got_reference *head_ref;
3253 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3254 if (error != NULL)
3255 goto done;
3256 error = got_ref_resolve(&commit_id, repo, head_ref);
3257 got_ref_close(head_ref);
3258 } else {
3259 error = got_object_resolve_id_str(&commit_id, repo,
3260 commit_id_str);
3262 if (error != NULL)
3263 goto done;
3265 error = got_ref_list(&refs, repo);
3266 if (error)
3267 goto done;
3269 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3270 if (view == NULL) {
3271 error = got_error_from_errno("view_open");
3272 goto done;
3274 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3275 if (error)
3276 goto done;
3277 error = view_loop(view);
3278 done:
3279 free(repo_path);
3280 free(cwd);
3281 free(commit_id);
3282 if (worktree)
3283 got_worktree_close(worktree);
3284 if (repo)
3285 got_repo_close(repo);
3286 got_ref_list_free(&refs);
3287 return error;
3290 static const struct got_error *
3291 draw_tree_entries(struct tog_view *view,
3292 struct got_tree_entry **first_displayed_entry,
3293 struct got_tree_entry **last_displayed_entry,
3294 struct got_tree_entry **selected_entry, int *ndisplayed,
3295 const char *label, int show_ids, const char *parent_path,
3296 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3298 const struct got_error *err = NULL;
3299 struct got_tree_entry *te;
3300 wchar_t *wline;
3301 int width, n;
3303 *ndisplayed = 0;
3305 werase(view->window);
3307 if (limit == 0)
3308 return NULL;
3310 err = format_line(&wline, &width, label, view->ncols);
3311 if (err)
3312 return err;
3313 if (view_needs_focus_indication(view))
3314 wstandout(view->window);
3315 waddwstr(view->window, wline);
3316 if (view_needs_focus_indication(view))
3317 wstandend(view->window);
3318 free(wline);
3319 wline = NULL;
3320 if (width < view->ncols)
3321 waddch(view->window, '\n');
3322 if (--limit <= 0)
3323 return NULL;
3324 err = format_line(&wline, &width, parent_path, view->ncols);
3325 if (err)
3326 return err;
3327 waddwstr(view->window, wline);
3328 free(wline);
3329 wline = NULL;
3330 if (width < view->ncols)
3331 waddch(view->window, '\n');
3332 if (--limit <= 0)
3333 return NULL;
3334 waddch(view->window, '\n');
3335 if (--limit <= 0)
3336 return NULL;
3338 te = SIMPLEQ_FIRST(&entries->head);
3339 if (*first_displayed_entry == NULL) {
3340 if (selected == 0) {
3341 if (view->focussed)
3342 wstandout(view->window);
3343 *selected_entry = NULL;
3345 waddstr(view->window, " ..\n"); /* parent directory */
3346 if (selected == 0 && view->focussed)
3347 wstandend(view->window);
3348 (*ndisplayed)++;
3349 if (--limit <= 0)
3350 return NULL;
3351 n = 1;
3352 } else {
3353 n = 0;
3354 while (te != *first_displayed_entry)
3355 te = SIMPLEQ_NEXT(te, entry);
3358 while (te) {
3359 char *line = NULL, *id_str = NULL;
3361 if (show_ids) {
3362 err = got_object_id_str(&id_str, te->id);
3363 if (err)
3364 return got_error_from_errno(
3365 "got_object_id_str");
3367 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3368 te->name, S_ISDIR(te->mode) ? "/" :
3369 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3370 free(id_str);
3371 return got_error_from_errno("asprintf");
3373 free(id_str);
3374 err = format_line(&wline, &width, line, view->ncols);
3375 if (err) {
3376 free(line);
3377 break;
3379 if (n == selected) {
3380 if (view->focussed)
3381 wstandout(view->window);
3382 *selected_entry = te;
3384 waddwstr(view->window, wline);
3385 if (width < view->ncols)
3386 waddch(view->window, '\n');
3387 if (n == selected && view->focussed)
3388 wstandend(view->window);
3389 free(line);
3390 free(wline);
3391 wline = NULL;
3392 n++;
3393 (*ndisplayed)++;
3394 *last_displayed_entry = te;
3395 if (--limit <= 0)
3396 break;
3397 te = SIMPLEQ_NEXT(te, entry);
3400 return err;
3403 static void
3404 tree_scroll_up(struct tog_view *view,
3405 struct got_tree_entry **first_displayed_entry, int maxscroll,
3406 const struct got_tree_entries *entries, int isroot)
3408 struct got_tree_entry *te, *prev;
3409 int i;
3411 if (*first_displayed_entry == NULL)
3412 return;
3414 te = SIMPLEQ_FIRST(&entries->head);
3415 if (*first_displayed_entry == te) {
3416 if (!isroot)
3417 *first_displayed_entry = NULL;
3418 return;
3421 /* XXX this is stupid... switch to TAILQ? */
3422 for (i = 0; i < maxscroll; i++) {
3423 while (te != *first_displayed_entry) {
3424 prev = te;
3425 te = SIMPLEQ_NEXT(te, entry);
3427 *first_displayed_entry = prev;
3428 te = SIMPLEQ_FIRST(&entries->head);
3430 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3431 *first_displayed_entry = NULL;
3434 static int
3435 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3436 struct got_tree_entry *last_displayed_entry,
3437 const struct got_tree_entries *entries)
3439 struct got_tree_entry *next, *last;
3440 int n = 0;
3442 if (*first_displayed_entry)
3443 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3444 else
3445 next = SIMPLEQ_FIRST(&entries->head);
3446 last = last_displayed_entry;
3447 while (next && last && n++ < maxscroll) {
3448 last = SIMPLEQ_NEXT(last, entry);
3449 if (last) {
3450 *first_displayed_entry = next;
3451 next = SIMPLEQ_NEXT(next, entry);
3454 return n;
3457 static const struct got_error *
3458 tree_entry_path(char **path, struct tog_parent_trees *parents,
3459 struct got_tree_entry *te)
3461 const struct got_error *err = NULL;
3462 struct tog_parent_tree *pt;
3463 size_t len = 2; /* for leading slash and NUL */
3465 TAILQ_FOREACH(pt, parents, entry)
3466 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3467 if (te)
3468 len += strlen(te->name);
3470 *path = calloc(1, len);
3471 if (path == NULL)
3472 return got_error_from_errno("calloc");
3474 (*path)[0] = '/';
3475 pt = TAILQ_LAST(parents, tog_parent_trees);
3476 while (pt) {
3477 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3478 err = got_error(GOT_ERR_NO_SPACE);
3479 goto done;
3481 if (strlcat(*path, "/", len) >= len) {
3482 err = got_error(GOT_ERR_NO_SPACE);
3483 goto done;
3485 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3487 if (te) {
3488 if (strlcat(*path, te->name, len) >= len) {
3489 err = got_error(GOT_ERR_NO_SPACE);
3490 goto done;
3493 done:
3494 if (err) {
3495 free(*path);
3496 *path = NULL;
3498 return err;
3501 static const struct got_error *
3502 blame_tree_entry(struct tog_view **new_view, int begin_x,
3503 struct got_tree_entry *te, struct tog_parent_trees *parents,
3504 struct got_object_id *commit_id, struct got_reflist_head *refs,
3505 struct got_repository *repo)
3507 const struct got_error *err = NULL;
3508 char *path;
3509 struct tog_view *blame_view;
3511 err = tree_entry_path(&path, parents, te);
3512 if (err)
3513 return err;
3515 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3516 if (blame_view == NULL)
3517 return got_error_from_errno("view_open");
3519 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3520 if (err) {
3521 view_close(blame_view);
3522 free(path);
3523 } else
3524 *new_view = blame_view;
3525 return err;
3528 static const struct got_error *
3529 log_tree_entry(struct tog_view **new_view, int begin_x,
3530 struct got_tree_entry *te, struct tog_parent_trees *parents,
3531 struct got_object_id *commit_id, struct got_reflist_head *refs,
3532 struct got_repository *repo)
3534 struct tog_view *log_view;
3535 const struct got_error *err = NULL;
3536 char *path;
3538 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3539 if (log_view == NULL)
3540 return got_error_from_errno("view_open");
3542 err = tree_entry_path(&path, parents, te);
3543 if (err)
3544 return err;
3546 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3547 if (err)
3548 view_close(log_view);
3549 else
3550 *new_view = log_view;
3551 free(path);
3552 return err;
3555 static const struct got_error *
3556 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3557 struct got_object_id *commit_id, struct got_reflist_head *refs,
3558 struct got_repository *repo)
3560 const struct got_error *err = NULL;
3561 char *commit_id_str = NULL;
3562 struct tog_tree_view_state *s = &view->state.tree;
3564 TAILQ_INIT(&s->parents);
3566 err = got_object_id_str(&commit_id_str, commit_id);
3567 if (err != NULL)
3568 goto done;
3570 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3571 err = got_error_from_errno("asprintf");
3572 goto done;
3575 s->root = s->tree = root;
3576 s->entries = got_object_tree_get_entries(root);
3577 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3578 s->commit_id = got_object_id_dup(commit_id);
3579 if (s->commit_id == NULL) {
3580 err = got_error_from_errno("got_object_id_dup");
3581 goto done;
3583 s->refs = refs;
3584 s->repo = repo;
3586 view->show = show_tree_view;
3587 view->input = input_tree_view;
3588 view->close = close_tree_view;
3589 done:
3590 free(commit_id_str);
3591 if (err) {
3592 free(s->tree_label);
3593 s->tree_label = NULL;
3595 return err;
3598 static const struct got_error *
3599 close_tree_view(struct tog_view *view)
3601 struct tog_tree_view_state *s = &view->state.tree;
3603 free(s->tree_label);
3604 s->tree_label = NULL;
3605 free(s->commit_id);
3606 s->commit_id = NULL;
3607 while (!TAILQ_EMPTY(&s->parents)) {
3608 struct tog_parent_tree *parent;
3609 parent = TAILQ_FIRST(&s->parents);
3610 TAILQ_REMOVE(&s->parents, parent, entry);
3611 free(parent);
3614 if (s->tree != s->root)
3615 got_object_tree_close(s->tree);
3616 got_object_tree_close(s->root);
3618 return NULL;
3621 static const struct got_error *
3622 show_tree_view(struct tog_view *view)
3624 const struct got_error *err = NULL;
3625 struct tog_tree_view_state *s = &view->state.tree;
3626 char *parent_path;
3628 err = tree_entry_path(&parent_path, &s->parents, NULL);
3629 if (err)
3630 return err;
3632 err = draw_tree_entries(view, &s->first_displayed_entry,
3633 &s->last_displayed_entry, &s->selected_entry,
3634 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3635 s->entries, s->selected, view->nlines, s->tree == s->root);
3636 free(parent_path);
3638 view_vborder(view);
3639 return err;
3642 static const struct got_error *
3643 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3644 struct tog_view **focus_view, struct tog_view *view, int ch)
3646 const struct got_error *err = NULL;
3647 struct tog_tree_view_state *s = &view->state.tree;
3648 struct tog_view *log_view;
3649 int begin_x = 0, nscrolled;
3651 switch (ch) {
3652 case 'i':
3653 s->show_ids = !s->show_ids;
3654 break;
3655 case 'l':
3656 if (!s->selected_entry)
3657 break;
3658 if (view_is_parent_view(view))
3659 begin_x = view_split_begin_x(view->begin_x);
3660 err = log_tree_entry(&log_view, begin_x,
3661 s->selected_entry, &s->parents,
3662 s->commit_id, s->refs, s->repo);
3663 if (view_is_parent_view(view)) {
3664 err = view_close_child(view);
3665 if (err)
3666 return err;
3667 err = view_set_child(view, log_view);
3668 if (err) {
3669 view_close(log_view);
3670 break;
3672 *focus_view = log_view;
3673 view->child_focussed = 1;
3674 } else
3675 *new_view = log_view;
3676 break;
3677 case 'k':
3678 case KEY_UP:
3679 if (s->selected > 0) {
3680 s->selected--;
3681 if (s->selected == 0)
3682 break;
3684 if (s->selected > 0)
3685 break;
3686 tree_scroll_up(view, &s->first_displayed_entry, 1,
3687 s->entries, s->tree == s->root);
3688 break;
3689 case KEY_PPAGE:
3690 tree_scroll_up(view, &s->first_displayed_entry,
3691 MAX(0, view->nlines - 4 - s->selected), s->entries,
3692 s->tree == s->root);
3693 s->selected = 0;
3694 if (SIMPLEQ_FIRST(&s->entries->head) ==
3695 s->first_displayed_entry && s->tree != s->root)
3696 s->first_displayed_entry = NULL;
3697 break;
3698 case 'j':
3699 case KEY_DOWN:
3700 if (s->selected < s->ndisplayed - 1) {
3701 s->selected++;
3702 break;
3704 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3705 /* can't scroll any further */
3706 break;
3707 tree_scroll_down(&s->first_displayed_entry, 1,
3708 s->last_displayed_entry, s->entries);
3709 break;
3710 case KEY_NPAGE:
3711 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3712 == NULL) {
3713 /* can't scroll any further; move cursor down */
3714 if (s->selected < s->ndisplayed - 1)
3715 s->selected = s->ndisplayed - 1;
3716 break;
3718 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3719 view->nlines, s->last_displayed_entry, s->entries);
3720 if (nscrolled < view->nlines) {
3721 int ndisplayed = 0;
3722 struct got_tree_entry *te;
3723 te = s->first_displayed_entry;
3724 do {
3725 ndisplayed++;
3726 te = SIMPLEQ_NEXT(te, entry);
3727 } while (te);
3728 s->selected = ndisplayed - 1;
3730 break;
3731 case KEY_ENTER:
3732 case '\r':
3733 case KEY_BACKSPACE:
3734 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3735 struct tog_parent_tree *parent;
3736 /* user selected '..' */
3737 if (s->tree == s->root)
3738 break;
3739 parent = TAILQ_FIRST(&s->parents);
3740 TAILQ_REMOVE(&s->parents, parent,
3741 entry);
3742 got_object_tree_close(s->tree);
3743 s->tree = parent->tree;
3744 s->entries =
3745 got_object_tree_get_entries(s->tree);
3746 s->first_displayed_entry =
3747 parent->first_displayed_entry;
3748 s->selected_entry =
3749 parent->selected_entry;
3750 s->selected = parent->selected;
3751 free(parent);
3752 } else if (S_ISDIR(s->selected_entry->mode)) {
3753 struct tog_parent_tree *parent;
3754 struct got_tree_object *child;
3755 err = got_object_open_as_tree(&child,
3756 s->repo, s->selected_entry->id);
3757 if (err)
3758 break;
3759 parent = calloc(1, sizeof(*parent));
3760 if (parent == NULL) {
3761 err = got_error_from_errno("calloc");
3762 break;
3764 parent->tree = s->tree;
3765 parent->first_displayed_entry =
3766 s->first_displayed_entry;
3767 parent->selected_entry = s->selected_entry;
3768 parent->selected = s->selected;
3769 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3770 s->tree = child;
3771 s->entries =
3772 got_object_tree_get_entries(s->tree);
3773 s->selected = 0;
3774 s->first_displayed_entry = NULL;
3775 } else if (S_ISREG(s->selected_entry->mode)) {
3776 struct tog_view *blame_view;
3777 int begin_x = view_is_parent_view(view) ?
3778 view_split_begin_x(view->begin_x) : 0;
3780 err = blame_tree_entry(&blame_view, begin_x,
3781 s->selected_entry, &s->parents,
3782 s->commit_id, s->refs, s->repo);
3783 if (err)
3784 break;
3785 if (view_is_parent_view(view)) {
3786 err = view_close_child(view);
3787 if (err)
3788 return err;
3789 err = view_set_child(view, blame_view);
3790 if (err) {
3791 view_close(blame_view);
3792 break;
3794 *focus_view = blame_view;
3795 view->child_focussed = 1;
3796 } else
3797 *new_view = blame_view;
3799 break;
3800 case KEY_RESIZE:
3801 if (s->selected > view->nlines)
3802 s->selected = s->ndisplayed - 1;
3803 break;
3804 default:
3805 break;
3808 return err;
3811 __dead static void
3812 usage_tree(void)
3814 endwin();
3815 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3816 getprogname());
3817 exit(1);
3820 static const struct got_error *
3821 cmd_tree(int argc, char *argv[])
3823 const struct got_error *error;
3824 struct got_repository *repo = NULL;
3825 struct got_reflist_head refs;
3826 char *repo_path = NULL;
3827 struct got_object_id *commit_id = NULL;
3828 char *commit_id_arg = NULL;
3829 struct got_commit_object *commit = NULL;
3830 struct got_tree_object *tree = NULL;
3831 int ch;
3832 struct tog_view *view;
3834 SIMPLEQ_INIT(&refs);
3836 #ifndef PROFILE
3837 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3838 NULL) == -1)
3839 err(1, "pledge");
3840 #endif
3842 while ((ch = getopt(argc, argv, "c:")) != -1) {
3843 switch (ch) {
3844 case 'c':
3845 commit_id_arg = optarg;
3846 break;
3847 default:
3848 usage_tree();
3849 /* NOTREACHED */
3853 argc -= optind;
3854 argv += optind;
3856 if (argc == 0) {
3857 struct got_worktree *worktree;
3858 char *cwd = getcwd(NULL, 0);
3859 if (cwd == NULL)
3860 return got_error_from_errno("getcwd");
3861 error = got_worktree_open(&worktree, cwd);
3862 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3863 goto done;
3864 if (worktree) {
3865 free(cwd);
3866 repo_path =
3867 strdup(got_worktree_get_repo_path(worktree));
3868 got_worktree_close(worktree);
3869 } else
3870 repo_path = cwd;
3871 if (repo_path == NULL) {
3872 error = got_error_from_errno("strdup");
3873 goto done;
3875 } else if (argc == 1) {
3876 repo_path = realpath(argv[0], NULL);
3877 if (repo_path == NULL)
3878 return got_error_from_errno2("realpath", argv[0]);
3879 } else
3880 usage_log();
3882 init_curses();
3884 error = got_repo_open(&repo, repo_path);
3885 if (error != NULL)
3886 goto done;
3888 error = apply_unveil(got_repo_get_path(repo), NULL);
3889 if (error)
3890 goto done;
3892 if (commit_id_arg == NULL)
3893 error = get_head_commit_id(&commit_id, repo);
3894 else
3895 error = got_object_resolve_id_str(&commit_id, repo,
3896 commit_id_arg);
3897 if (error != NULL)
3898 goto done;
3900 error = got_object_open_as_commit(&commit, repo, commit_id);
3901 if (error != NULL)
3902 goto done;
3904 error = got_object_open_as_tree(&tree, repo,
3905 got_object_commit_get_tree_id(commit));
3906 if (error != NULL)
3907 goto done;
3909 error = got_ref_list(&refs, repo);
3910 if (error)
3911 goto done;
3913 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3914 if (view == NULL) {
3915 error = got_error_from_errno("view_open");
3916 goto done;
3918 error = open_tree_view(view, tree, commit_id, &refs, repo);
3919 if (error)
3920 goto done;
3921 error = view_loop(view);
3922 done:
3923 free(repo_path);
3924 free(commit_id);
3925 if (commit)
3926 got_object_commit_close(commit);
3927 if (tree)
3928 got_object_tree_close(tree);
3929 if (repo)
3930 got_repo_close(repo);
3931 got_ref_list_free(&refs);
3932 return error;
3935 __dead static void
3936 usage(void)
3938 int i;
3940 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3941 "Available commands:\n", getprogname());
3942 for (i = 0; i < nitems(tog_commands); i++) {
3943 struct tog_cmd *cmd = &tog_commands[i];
3944 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3946 exit(1);
3949 static char **
3950 make_argv(const char *arg0, const char *arg1)
3952 char **argv;
3953 int argc = (arg1 == NULL ? 1 : 2);
3955 argv = calloc(argc, sizeof(char *));
3956 if (argv == NULL)
3957 err(1, "calloc");
3958 argv[0] = strdup(arg0);
3959 if (argv[0] == NULL)
3960 err(1, "calloc");
3961 if (arg1) {
3962 argv[1] = strdup(arg1);
3963 if (argv[1] == NULL)
3964 err(1, "calloc");
3967 return argv;
3970 int
3971 main(int argc, char *argv[])
3973 const struct got_error *error = NULL;
3974 struct tog_cmd *cmd = NULL;
3975 int ch, hflag = 0;
3976 char **cmd_argv = NULL;
3978 setlocale(LC_CTYPE, "");
3980 while ((ch = getopt(argc, argv, "h")) != -1) {
3981 switch (ch) {
3982 case 'h':
3983 hflag = 1;
3984 break;
3985 default:
3986 usage();
3987 /* NOTREACHED */
3991 argc -= optind;
3992 argv += optind;
3993 optind = 0;
3994 optreset = 1;
3996 if (argc == 0) {
3997 if (hflag)
3998 usage();
3999 /* Build an argument vector which runs a default command. */
4000 cmd = &tog_commands[0];
4001 cmd_argv = make_argv(cmd->name, NULL);
4002 argc = 1;
4003 } else {
4004 int i;
4006 /* Did the user specific a command? */
4007 for (i = 0; i < nitems(tog_commands); i++) {
4008 if (strncmp(tog_commands[i].name, argv[0],
4009 strlen(argv[0])) == 0) {
4010 cmd = &tog_commands[i];
4011 if (hflag)
4012 tog_commands[i].cmd_usage();
4013 break;
4016 if (cmd == NULL) {
4017 /* Did the user specify a repository? */
4018 char *repo_path = realpath(argv[0], NULL);
4019 if (repo_path) {
4020 struct got_repository *repo;
4021 error = got_repo_open(&repo, repo_path);
4022 if (error == NULL)
4023 got_repo_close(repo);
4024 } else
4025 error = got_error_from_errno2("realpath",
4026 argv[0]);
4027 if (error) {
4028 if (hflag) {
4029 fprintf(stderr, "%s: '%s' is not a "
4030 "known command\n", getprogname(),
4031 argv[0]);
4032 usage();
4034 fprintf(stderr, "%s: '%s' is neither a known "
4035 "command nor a path to a repository\n",
4036 getprogname(), argv[0]);
4037 free(repo_path);
4038 return 1;
4040 cmd = &tog_commands[0];
4041 cmd_argv = make_argv(cmd->name, repo_path);
4042 argc = 2;
4043 free(repo_path);
4047 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4048 if (error)
4049 goto done;
4050 done:
4051 endwin();
4052 free(cmd_argv);
4053 if (error)
4054 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4055 return 0;