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 mvwaddstr(view->window, 0, 0, "diffing...");
2173 update_panels();
2174 doupdate();
2177 static const struct got_error *
2178 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2179 struct got_object_id *id2, struct tog_view *log_view,
2180 struct got_reflist_head *refs, struct got_repository *repo)
2182 const struct got_error *err;
2184 if (id1 != NULL && id2 != NULL) {
2185 int type1, type2;
2186 err = got_object_get_type(&type1, repo, id1);
2187 if (err)
2188 return err;
2189 err = got_object_get_type(&type2, repo, id2);
2190 if (err)
2191 return err;
2193 if (type1 != type2)
2194 return got_error(GOT_ERR_OBJ_TYPE);
2197 if (id1) {
2198 view->state.diff.id1 = got_object_id_dup(id1);
2199 if (view->state.diff.id1 == NULL)
2200 return got_error_from_errno("got_object_id_dup");
2201 } else
2202 view->state.diff.id1 = NULL;
2204 view->state.diff.id2 = got_object_id_dup(id2);
2205 if (view->state.diff.id2 == NULL) {
2206 free(view->state.diff.id1);
2207 view->state.diff.id1 = NULL;
2208 return got_error_from_errno("got_object_id_dup");
2210 view->state.diff.f = NULL;
2211 view->state.diff.first_displayed_line = 1;
2212 view->state.diff.last_displayed_line = view->nlines;
2213 view->state.diff.diff_context = 3;
2214 view->state.diff.log_view = log_view;
2215 view->state.diff.repo = repo;
2216 view->state.diff.refs = refs;
2218 if (log_view && view_is_splitscreen(view))
2219 show_log_view(log_view); /* draw vborder */
2220 diff_view_indicate_progress(view);
2222 err = create_diff(&view->state.diff);
2223 if (err) {
2224 free(view->state.diff.id1);
2225 view->state.diff.id1 = NULL;
2226 free(view->state.diff.id2);
2227 view->state.diff.id2 = NULL;
2228 return err;
2231 view->show = show_diff_view;
2232 view->input = input_diff_view;
2233 view->close = close_diff_view;
2235 return NULL;
2238 static const struct got_error *
2239 close_diff_view(struct tog_view *view)
2241 const struct got_error *err = NULL;
2243 free(view->state.diff.id1);
2244 view->state.diff.id1 = NULL;
2245 free(view->state.diff.id2);
2246 view->state.diff.id2 = NULL;
2247 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2248 err = got_error_from_errno("fclose");
2249 return err;
2252 static const struct got_error *
2253 show_diff_view(struct tog_view *view)
2255 const struct got_error *err;
2256 struct tog_diff_view_state *s = &view->state.diff;
2257 char *id_str1 = NULL, *id_str2, *header;
2259 if (s->id1) {
2260 err = got_object_id_str(&id_str1, s->id1);
2261 if (err)
2262 return err;
2264 err = got_object_id_str(&id_str2, s->id2);
2265 if (err)
2266 return err;
2268 if (asprintf(&header, "diff %s %s",
2269 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2270 err = got_error_from_errno("asprintf");
2271 free(id_str1);
2272 free(id_str2);
2273 return err;
2275 free(id_str1);
2276 free(id_str2);
2278 return draw_file(view, s->f, &s->first_displayed_line,
2279 &s->last_displayed_line, &s->eof, view->nlines,
2280 header);
2283 static const struct got_error *
2284 set_selected_commit(struct tog_diff_view_state *s,
2285 struct commit_queue_entry *entry)
2287 const struct got_error *err;
2288 const struct got_object_id_queue *parent_ids;
2289 struct got_commit_object *selected_commit;
2290 struct got_object_qid *pid;
2292 free(s->id2);
2293 s->id2 = got_object_id_dup(entry->id);
2294 if (s->id2 == NULL)
2295 return got_error_from_errno("got_object_id_dup");
2297 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2298 if (err)
2299 return err;
2300 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2301 free(s->id1);
2302 pid = SIMPLEQ_FIRST(parent_ids);
2303 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2304 got_object_commit_close(selected_commit);
2305 return NULL;
2308 static const struct got_error *
2309 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2310 struct tog_view **focus_view, struct tog_view *view, int ch)
2312 const struct got_error *err = NULL;
2313 struct tog_diff_view_state *s = &view->state.diff;
2314 struct tog_log_view_state *ls;
2315 struct commit_queue_entry *entry;
2316 int i;
2318 switch (ch) {
2319 case 'k':
2320 case KEY_UP:
2321 if (s->first_displayed_line > 1)
2322 s->first_displayed_line--;
2323 break;
2324 case KEY_PPAGE:
2325 if (s->first_displayed_line == 1)
2326 break;
2327 i = 0;
2328 while (i++ < view->nlines - 1 &&
2329 s->first_displayed_line > 1)
2330 s->first_displayed_line--;
2331 break;
2332 case 'j':
2333 case KEY_DOWN:
2334 if (!s->eof)
2335 s->first_displayed_line++;
2336 break;
2337 case KEY_NPAGE:
2338 case ' ':
2339 if (s->eof)
2340 break;
2341 i = 0;
2342 while (!s->eof && i++ < view->nlines - 1) {
2343 char *line;
2344 line = parse_next_line(s->f, NULL);
2345 s->first_displayed_line++;
2346 if (line == NULL)
2347 break;
2349 break;
2350 case '[':
2351 if (s->diff_context > 0) {
2352 s->diff_context--;
2353 diff_view_indicate_progress(view);
2354 err = create_diff(s);
2356 break;
2357 case ']':
2358 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2359 s->diff_context++;
2360 diff_view_indicate_progress(view);
2361 err = create_diff(s);
2363 break;
2364 case '<':
2365 case ',':
2366 if (s->log_view == NULL)
2367 break;
2368 ls = &s->log_view->state.log;
2369 entry = TAILQ_PREV(ls->selected_entry,
2370 commit_queue_head, entry);
2371 if (entry == NULL)
2372 break;
2374 err = input_log_view(NULL, NULL, NULL, s->log_view,
2375 KEY_UP);
2376 if (err)
2377 break;
2379 err = set_selected_commit(s, entry);
2380 if (err)
2381 break;
2383 s->first_displayed_line = 1;
2384 s->last_displayed_line = view->nlines;
2386 diff_view_indicate_progress(view);
2387 err = create_diff(s);
2388 break;
2389 case '>':
2390 case '.':
2391 if (s->log_view == NULL)
2392 break;
2393 ls = &s->log_view->state.log;
2395 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2396 ls->thread_args.commits_needed++;
2398 /* Display "loading..." in log view. */
2399 show_log_view(s->log_view);
2400 update_panels();
2401 doupdate();
2403 err = trigger_log_thread(1 /* load_all */,
2404 &ls->thread_args.commits_needed,
2405 &ls->thread_args.log_complete,
2406 &ls->thread_args.need_commits);
2407 if (err)
2408 break;
2410 err = input_log_view(NULL, NULL, NULL, s->log_view,
2411 KEY_DOWN);
2412 if (err)
2413 break;
2415 entry = TAILQ_NEXT(ls->selected_entry, entry);
2416 if (entry == NULL)
2417 break;
2419 err = set_selected_commit(s, entry);
2420 if (err)
2421 break;
2423 s->first_displayed_line = 1;
2424 s->last_displayed_line = view->nlines;
2426 diff_view_indicate_progress(view);
2427 err = create_diff(s);
2428 break;
2429 default:
2430 break;
2433 return err;
2436 static const struct got_error *
2437 cmd_diff(int argc, char *argv[])
2439 const struct got_error *error = NULL;
2440 struct got_repository *repo = NULL;
2441 struct got_reflist_head refs;
2442 struct got_object_id *id1 = NULL, *id2 = NULL;
2443 char *repo_path = NULL;
2444 char *id_str1 = NULL, *id_str2 = NULL;
2445 int ch;
2446 struct tog_view *view;
2448 SIMPLEQ_INIT(&refs);
2450 #ifndef PROFILE
2451 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2452 NULL) == -1)
2453 err(1, "pledge");
2454 #endif
2456 while ((ch = getopt(argc, argv, "")) != -1) {
2457 switch (ch) {
2458 default:
2459 usage_diff();
2460 /* NOTREACHED */
2464 argc -= optind;
2465 argv += optind;
2467 if (argc == 0) {
2468 usage_diff(); /* TODO show local worktree changes */
2469 } else if (argc == 2) {
2470 repo_path = getcwd(NULL, 0);
2471 if (repo_path == NULL)
2472 return got_error_from_errno("getcwd");
2473 id_str1 = argv[0];
2474 id_str2 = argv[1];
2475 } else if (argc == 3) {
2476 repo_path = realpath(argv[0], NULL);
2477 if (repo_path == NULL)
2478 return got_error_from_errno2("realpath", argv[0]);
2479 id_str1 = argv[1];
2480 id_str2 = argv[2];
2481 } else
2482 usage_diff();
2484 init_curses();
2486 error = got_repo_open(&repo, repo_path);
2487 if (error)
2488 goto done;
2490 error = apply_unveil(got_repo_get_path(repo), NULL);
2491 if (error)
2492 goto done;
2494 error = got_object_resolve_id_str(&id1, repo, id_str1);
2495 if (error)
2496 goto done;
2498 error = got_object_resolve_id_str(&id2, repo, id_str2);
2499 if (error)
2500 goto done;
2502 error = got_ref_list(&refs, repo);
2503 if (error)
2504 goto done;
2506 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2507 if (view == NULL) {
2508 error = got_error_from_errno("view_open");
2509 goto done;
2511 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2512 if (error)
2513 goto done;
2514 error = view_loop(view);
2515 done:
2516 free(repo_path);
2517 got_repo_close(repo);
2518 got_ref_list_free(&refs);
2519 return error;
2522 __dead static void
2523 usage_blame(void)
2525 endwin();
2526 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2527 getprogname());
2528 exit(1);
2531 struct tog_blame_line {
2532 int annotated;
2533 struct got_object_id *id;
2536 static const struct got_error *
2537 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2538 const char *path, struct tog_blame_line *lines, int nlines,
2539 int blame_complete, int selected_line, int *first_displayed_line,
2540 int *last_displayed_line, int *eof, int max_lines)
2542 const struct got_error *err;
2543 int lineno = 0, nprinted = 0;
2544 char *line;
2545 size_t len;
2546 wchar_t *wline;
2547 int width, wlimit;
2548 struct tog_blame_line *blame_line;
2549 struct got_object_id *prev_id = NULL;
2550 char *id_str;
2552 err = got_object_id_str(&id_str, id);
2553 if (err)
2554 return err;
2556 rewind(f);
2557 werase(view->window);
2559 if (asprintf(&line, "commit %s", id_str) == -1) {
2560 err = got_error_from_errno("asprintf");
2561 free(id_str);
2562 return err;
2565 err = format_line(&wline, &width, line, view->ncols);
2566 free(line);
2567 line = NULL;
2568 if (view_needs_focus_indication(view))
2569 wstandout(view->window);
2570 waddwstr(view->window, wline);
2571 if (view_needs_focus_indication(view))
2572 wstandend(view->window);
2573 free(wline);
2574 wline = NULL;
2575 if (width < view->ncols)
2576 waddch(view->window, '\n');
2578 if (asprintf(&line, "[%d/%d] %s%s",
2579 *first_displayed_line - 1 + selected_line, nlines,
2580 blame_complete ? "" : "annotating... ", path) == -1) {
2581 free(id_str);
2582 return got_error_from_errno("asprintf");
2584 free(id_str);
2585 err = format_line(&wline, &width, line, view->ncols);
2586 free(line);
2587 line = NULL;
2588 if (err)
2589 return err;
2590 waddwstr(view->window, wline);
2591 free(wline);
2592 wline = NULL;
2593 if (width < view->ncols)
2594 waddch(view->window, '\n');
2596 *eof = 0;
2597 while (nprinted < max_lines - 2) {
2598 line = parse_next_line(f, &len);
2599 if (line == NULL) {
2600 *eof = 1;
2601 break;
2603 if (++lineno < *first_displayed_line) {
2604 free(line);
2605 continue;
2608 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2609 err = format_line(&wline, &width, line, wlimit);
2610 if (err) {
2611 free(line);
2612 return err;
2615 if (view->focussed && nprinted == selected_line - 1)
2616 wstandout(view->window);
2618 blame_line = &lines[lineno - 1];
2619 if (blame_line->annotated && prev_id &&
2620 got_object_id_cmp(prev_id, blame_line->id) == 0)
2621 waddstr(view->window, " ");
2622 else if (blame_line->annotated) {
2623 char *id_str;
2624 err = got_object_id_str(&id_str, blame_line->id);
2625 if (err) {
2626 free(line);
2627 free(wline);
2628 return err;
2630 wprintw(view->window, "%.8s ", id_str);
2631 free(id_str);
2632 prev_id = blame_line->id;
2633 } else {
2634 waddstr(view->window, "........ ");
2635 prev_id = NULL;
2638 waddwstr(view->window, wline);
2639 while (width < wlimit) {
2640 waddch(view->window, ' ');
2641 width++;
2643 if (view->focussed && nprinted == selected_line - 1)
2644 wstandend(view->window);
2645 if (++nprinted == 1)
2646 *first_displayed_line = lineno;
2647 free(line);
2648 free(wline);
2649 wline = NULL;
2651 *last_displayed_line = lineno;
2653 view_vborder(view);
2655 return NULL;
2658 static const struct got_error *
2659 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2661 const struct got_error *err = NULL;
2662 struct tog_blame_cb_args *a = arg;
2663 struct tog_blame_line *line;
2664 int errcode;
2666 if (nlines != a->nlines ||
2667 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2668 return got_error(GOT_ERR_RANGE);
2670 errcode = pthread_mutex_lock(&tog_mutex);
2671 if (errcode)
2672 return got_error_set_errno(errcode, "pthread_mutex_lock");
2674 if (*a->quit) { /* user has quit the blame view */
2675 err = got_error(GOT_ERR_ITER_COMPLETED);
2676 goto done;
2679 if (lineno == -1)
2680 goto done; /* no change in this commit */
2682 line = &a->lines[lineno - 1];
2683 if (line->annotated)
2684 goto done;
2686 line->id = got_object_id_dup(id);
2687 if (line->id == NULL) {
2688 err = got_error_from_errno("got_object_id_dup");
2689 goto done;
2691 line->annotated = 1;
2692 done:
2693 errcode = pthread_mutex_unlock(&tog_mutex);
2694 if (errcode)
2695 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2696 return err;
2699 static void *
2700 blame_thread(void *arg)
2702 const struct got_error *err;
2703 struct tog_blame_thread_args *ta = arg;
2704 struct tog_blame_cb_args *a = ta->cb_args;
2705 int errcode;
2707 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2708 blame_cb, ta->cb_args);
2710 errcode = pthread_mutex_lock(&tog_mutex);
2711 if (errcode)
2712 return (void *)got_error_set_errno(errcode,
2713 "pthread_mutex_lock");
2715 got_repo_close(ta->repo);
2716 ta->repo = NULL;
2717 *ta->complete = 1;
2719 errcode = pthread_mutex_unlock(&tog_mutex);
2720 if (errcode && err == NULL)
2721 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2723 return (void *)err;
2726 static struct got_object_id *
2727 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2728 int selected_line)
2730 struct tog_blame_line *line;
2732 line = &lines[first_displayed_line - 1 + selected_line - 1];
2733 if (!line->annotated)
2734 return NULL;
2736 return line->id;
2739 static const struct got_error *
2740 stop_blame(struct tog_blame *blame)
2742 const struct got_error *err = NULL;
2743 int i;
2745 if (blame->thread) {
2746 int errcode;
2747 errcode = pthread_mutex_unlock(&tog_mutex);
2748 if (errcode)
2749 return got_error_set_errno(errcode,
2750 "pthread_mutex_unlock");
2751 errcode = pthread_join(blame->thread, (void **)&err);
2752 if (errcode)
2753 return got_error_set_errno(errcode, "pthread_join");
2754 errcode = pthread_mutex_lock(&tog_mutex);
2755 if (errcode)
2756 return got_error_set_errno(errcode,
2757 "pthread_mutex_lock");
2758 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2759 err = NULL;
2760 blame->thread = NULL;
2762 if (blame->thread_args.repo) {
2763 got_repo_close(blame->thread_args.repo);
2764 blame->thread_args.repo = NULL;
2766 if (blame->f) {
2767 if (fclose(blame->f) != 0 && err == NULL)
2768 err = got_error_from_errno("fclose");
2769 blame->f = NULL;
2771 if (blame->lines) {
2772 for (i = 0; i < blame->nlines; i++)
2773 free(blame->lines[i].id);
2774 free(blame->lines);
2775 blame->lines = NULL;
2777 free(blame->cb_args.commit_id);
2778 blame->cb_args.commit_id = NULL;
2780 return err;
2783 static const struct got_error *
2784 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2785 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2786 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2787 struct got_repository *repo)
2789 const struct got_error *err = NULL;
2790 struct got_blob_object *blob = NULL;
2791 struct got_repository *thread_repo = NULL;
2792 struct got_object_id *obj_id = NULL;
2793 int obj_type;
2795 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2796 if (err)
2797 return err;
2798 if (obj_id == NULL)
2799 return got_error(GOT_ERR_NO_OBJ);
2801 err = got_object_get_type(&obj_type, repo, obj_id);
2802 if (err)
2803 goto done;
2805 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2806 err = got_error(GOT_ERR_OBJ_TYPE);
2807 goto done;
2810 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2811 if (err)
2812 goto done;
2813 blame->f = got_opentemp();
2814 if (blame->f == NULL) {
2815 err = got_error_from_errno("got_opentemp");
2816 goto done;
2818 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2819 blame->f, blob);
2820 if (err)
2821 goto done;
2823 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2824 if (blame->lines == NULL) {
2825 err = got_error_from_errno("calloc");
2826 goto done;
2829 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2830 if (err)
2831 goto done;
2833 blame->cb_args.view = view;
2834 blame->cb_args.lines = blame->lines;
2835 blame->cb_args.nlines = blame->nlines;
2836 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2837 if (blame->cb_args.commit_id == NULL) {
2838 err = got_error_from_errno("got_object_id_dup");
2839 goto done;
2841 blame->cb_args.quit = done;
2843 blame->thread_args.path = path;
2844 blame->thread_args.repo = thread_repo;
2845 blame->thread_args.cb_args = &blame->cb_args;
2846 blame->thread_args.complete = blame_complete;
2847 *blame_complete = 0;
2849 done:
2850 if (blob)
2851 got_object_blob_close(blob);
2852 free(obj_id);
2853 if (err)
2854 stop_blame(blame);
2855 return err;
2858 static const struct got_error *
2859 open_blame_view(struct tog_view *view, char *path,
2860 struct got_object_id *commit_id, struct got_reflist_head *refs,
2861 struct got_repository *repo)
2863 const struct got_error *err = NULL;
2864 struct tog_blame_view_state *s = &view->state.blame;
2866 SIMPLEQ_INIT(&s->blamed_commits);
2868 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2869 if (err)
2870 return err;
2872 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2873 s->first_displayed_line = 1;
2874 s->last_displayed_line = view->nlines;
2875 s->selected_line = 1;
2876 s->blame_complete = 0;
2877 s->path = path;
2878 if (s->path == NULL)
2879 return got_error_from_errno("open_blame_view");
2880 s->repo = repo;
2881 s->refs = refs;
2882 s->commit_id = commit_id;
2883 memset(&s->blame, 0, sizeof(s->blame));
2885 view->show = show_blame_view;
2886 view->input = input_blame_view;
2887 view->close = close_blame_view;
2889 return run_blame(&s->blame, view, &s->blame_complete,
2890 &s->first_displayed_line, &s->last_displayed_line,
2891 &s->selected_line, &s->done, &s->eof, s->path,
2892 s->blamed_commit->id, s->repo);
2895 static const struct got_error *
2896 close_blame_view(struct tog_view *view)
2898 const struct got_error *err = NULL;
2899 struct tog_blame_view_state *s = &view->state.blame;
2901 if (s->blame.thread)
2902 err = stop_blame(&s->blame);
2904 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2905 struct got_object_qid *blamed_commit;
2906 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2907 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2908 got_object_qid_free(blamed_commit);
2911 free(s->path);
2913 return err;
2916 static const struct got_error *
2917 show_blame_view(struct tog_view *view)
2919 const struct got_error *err = NULL;
2920 struct tog_blame_view_state *s = &view->state.blame;
2921 int errcode;
2923 if (s->blame.thread == NULL) {
2924 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2925 &s->blame.thread_args);
2926 if (errcode)
2927 return got_error_set_errno(errcode, "pthread_create");
2930 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2931 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2932 s->selected_line, &s->first_displayed_line,
2933 &s->last_displayed_line, &s->eof, view->nlines);
2935 view_vborder(view);
2936 return err;
2939 static const struct got_error *
2940 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2941 struct tog_view **focus_view, struct tog_view *view, int ch)
2943 const struct got_error *err = NULL, *thread_err = NULL;
2944 struct tog_view *diff_view;
2945 struct tog_blame_view_state *s = &view->state.blame;
2946 int begin_x = 0;
2948 switch (ch) {
2949 case 'q':
2950 s->done = 1;
2951 break;
2952 case 'k':
2953 case KEY_UP:
2954 if (s->selected_line > 1)
2955 s->selected_line--;
2956 else if (s->selected_line == 1 &&
2957 s->first_displayed_line > 1)
2958 s->first_displayed_line--;
2959 break;
2960 case KEY_PPAGE:
2961 if (s->first_displayed_line == 1) {
2962 s->selected_line = 1;
2963 break;
2965 if (s->first_displayed_line > view->nlines - 2)
2966 s->first_displayed_line -=
2967 (view->nlines - 2);
2968 else
2969 s->first_displayed_line = 1;
2970 break;
2971 case 'j':
2972 case KEY_DOWN:
2973 if (s->selected_line < view->nlines - 2 &&
2974 s->first_displayed_line +
2975 s->selected_line <= s->blame.nlines)
2976 s->selected_line++;
2977 else if (s->last_displayed_line <
2978 s->blame.nlines)
2979 s->first_displayed_line++;
2980 break;
2981 case 'b':
2982 case 'p': {
2983 struct got_object_id *id = NULL;
2984 id = get_selected_commit_id(s->blame.lines,
2985 s->first_displayed_line, s->selected_line);
2986 if (id == NULL)
2987 break;
2988 if (ch == 'p') {
2989 struct got_commit_object *commit;
2990 struct got_object_qid *pid;
2991 struct got_object_id *blob_id = NULL;
2992 int obj_type;
2993 err = got_object_open_as_commit(&commit,
2994 s->repo, id);
2995 if (err)
2996 break;
2997 pid = SIMPLEQ_FIRST(
2998 got_object_commit_get_parent_ids(commit));
2999 if (pid == NULL) {
3000 got_object_commit_close(commit);
3001 break;
3003 /* Check if path history ends here. */
3004 err = got_object_id_by_path(&blob_id, s->repo,
3005 pid->id, s->path);
3006 if (err) {
3007 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3008 err = NULL;
3009 got_object_commit_close(commit);
3010 break;
3012 err = got_object_get_type(&obj_type, s->repo,
3013 blob_id);
3014 free(blob_id);
3015 /* Can't blame non-blob type objects. */
3016 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3017 got_object_commit_close(commit);
3018 break;
3020 err = got_object_qid_alloc(&s->blamed_commit,
3021 pid->id);
3022 got_object_commit_close(commit);
3023 } else {
3024 if (got_object_id_cmp(id,
3025 s->blamed_commit->id) == 0)
3026 break;
3027 err = got_object_qid_alloc(&s->blamed_commit,
3028 id);
3030 if (err)
3031 break;
3032 s->done = 1;
3033 thread_err = stop_blame(&s->blame);
3034 s->done = 0;
3035 if (thread_err)
3036 break;
3037 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3038 s->blamed_commit, entry);
3039 err = run_blame(&s->blame, view, &s->blame_complete,
3040 &s->first_displayed_line, &s->last_displayed_line,
3041 &s->selected_line, &s->done, &s->eof,
3042 s->path, s->blamed_commit->id, s->repo);
3043 if (err)
3044 break;
3045 break;
3047 case 'B': {
3048 struct got_object_qid *first;
3049 first = SIMPLEQ_FIRST(&s->blamed_commits);
3050 if (!got_object_id_cmp(first->id, s->commit_id))
3051 break;
3052 s->done = 1;
3053 thread_err = stop_blame(&s->blame);
3054 s->done = 0;
3055 if (thread_err)
3056 break;
3057 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3058 got_object_qid_free(s->blamed_commit);
3059 s->blamed_commit =
3060 SIMPLEQ_FIRST(&s->blamed_commits);
3061 err = run_blame(&s->blame, view, &s->blame_complete,
3062 &s->first_displayed_line, &s->last_displayed_line,
3063 &s->selected_line, &s->done, &s->eof, s->path,
3064 s->blamed_commit->id, s->repo);
3065 if (err)
3066 break;
3067 break;
3069 case KEY_ENTER:
3070 case '\r': {
3071 struct got_object_id *id = NULL;
3072 struct got_object_qid *pid;
3073 struct got_commit_object *commit = NULL;
3074 id = get_selected_commit_id(s->blame.lines,
3075 s->first_displayed_line, s->selected_line);
3076 if (id == NULL)
3077 break;
3078 err = got_object_open_as_commit(&commit, s->repo, id);
3079 if (err)
3080 break;
3081 pid = SIMPLEQ_FIRST(
3082 got_object_commit_get_parent_ids(commit));
3083 if (view_is_parent_view(view))
3084 begin_x = view_split_begin_x(view->begin_x);
3085 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3086 if (diff_view == NULL) {
3087 got_object_commit_close(commit);
3088 err = got_error_from_errno("view_open");
3089 break;
3091 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3092 id, NULL, s->refs, s->repo);
3093 got_object_commit_close(commit);
3094 if (err) {
3095 view_close(diff_view);
3096 break;
3098 if (view_is_parent_view(view)) {
3099 err = view_close_child(view);
3100 if (err)
3101 break;
3102 err = view_set_child(view, diff_view);
3103 if (err) {
3104 view_close(diff_view);
3105 break;
3107 *focus_view = diff_view;
3108 view->child_focussed = 1;
3109 } else
3110 *new_view = diff_view;
3111 if (err)
3112 break;
3113 break;
3115 case KEY_NPAGE:
3116 case ' ':
3117 if (s->last_displayed_line >= s->blame.nlines &&
3118 s->selected_line >= MIN(s->blame.nlines,
3119 view->nlines - 2)) {
3120 break;
3122 if (s->last_displayed_line >= s->blame.nlines &&
3123 s->selected_line < view->nlines - 2) {
3124 s->selected_line = MIN(s->blame.nlines,
3125 view->nlines - 2);
3126 break;
3128 if (s->last_displayed_line + view->nlines - 2
3129 <= s->blame.nlines)
3130 s->first_displayed_line +=
3131 view->nlines - 2;
3132 else
3133 s->first_displayed_line =
3134 s->blame.nlines -
3135 (view->nlines - 3);
3136 break;
3137 case KEY_RESIZE:
3138 if (s->selected_line > view->nlines - 2) {
3139 s->selected_line = MIN(s->blame.nlines,
3140 view->nlines - 2);
3142 break;
3143 default:
3144 break;
3146 return thread_err ? thread_err : err;
3149 static const struct got_error *
3150 cmd_blame(int argc, char *argv[])
3152 const struct got_error *error;
3153 struct got_repository *repo = NULL;
3154 struct got_reflist_head refs;
3155 struct got_worktree *worktree = NULL;
3156 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3157 struct got_object_id *commit_id = NULL;
3158 char *commit_id_str = NULL;
3159 int ch;
3160 struct tog_view *view;
3162 SIMPLEQ_INIT(&refs);
3164 #ifndef PROFILE
3165 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3166 NULL) == -1)
3167 err(1, "pledge");
3168 #endif
3170 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3171 switch (ch) {
3172 case 'c':
3173 commit_id_str = optarg;
3174 break;
3175 case 'r':
3176 repo_path = realpath(optarg, NULL);
3177 if (repo_path == NULL)
3178 err(1, "-r option");
3179 break;
3180 default:
3181 usage_blame();
3182 /* NOTREACHED */
3186 argc -= optind;
3187 argv += optind;
3189 if (argc == 1)
3190 path = argv[0];
3191 else
3192 usage_blame();
3194 cwd = getcwd(NULL, 0);
3195 if (cwd == NULL) {
3196 error = got_error_from_errno("getcwd");
3197 goto done;
3199 if (repo_path == NULL) {
3200 error = got_worktree_open(&worktree, cwd);
3201 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3202 goto done;
3203 else
3204 error = NULL;
3205 if (worktree) {
3206 repo_path =
3207 strdup(got_worktree_get_repo_path(worktree));
3208 if (repo_path == NULL)
3209 error = got_error_from_errno("strdup");
3210 if (error)
3211 goto done;
3212 } else {
3213 repo_path = strdup(cwd);
3214 if (repo_path == NULL) {
3215 error = got_error_from_errno("strdup");
3216 goto done;
3221 init_curses();
3223 error = got_repo_open(&repo, repo_path);
3224 if (error != NULL)
3225 goto done;
3227 error = apply_unveil(got_repo_get_path(repo), NULL);
3228 if (error)
3229 goto done;
3231 if (worktree) {
3232 const char *prefix = got_worktree_get_path_prefix(worktree);
3233 char *p, *worktree_subdir = cwd +
3234 strlen(got_worktree_get_root_path(worktree));
3235 if (asprintf(&p, "%s%s%s%s%s",
3236 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3237 worktree_subdir, worktree_subdir[0] ? "/" : "",
3238 path) == -1) {
3239 error = got_error_from_errno("asprintf");
3240 goto done;
3242 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3243 free(p);
3244 } else {
3245 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3247 if (error)
3248 goto done;
3250 if (commit_id_str == NULL) {
3251 struct got_reference *head_ref;
3252 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3253 if (error != NULL)
3254 goto done;
3255 error = got_ref_resolve(&commit_id, repo, head_ref);
3256 got_ref_close(head_ref);
3257 } else {
3258 error = got_object_resolve_id_str(&commit_id, repo,
3259 commit_id_str);
3261 if (error != NULL)
3262 goto done;
3264 error = got_ref_list(&refs, repo);
3265 if (error)
3266 goto done;
3268 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3269 if (view == NULL) {
3270 error = got_error_from_errno("view_open");
3271 goto done;
3273 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3274 if (error)
3275 goto done;
3276 error = view_loop(view);
3277 done:
3278 free(repo_path);
3279 free(cwd);
3280 free(commit_id);
3281 if (worktree)
3282 got_worktree_close(worktree);
3283 if (repo)
3284 got_repo_close(repo);
3285 got_ref_list_free(&refs);
3286 return error;
3289 static const struct got_error *
3290 draw_tree_entries(struct tog_view *view,
3291 struct got_tree_entry **first_displayed_entry,
3292 struct got_tree_entry **last_displayed_entry,
3293 struct got_tree_entry **selected_entry, int *ndisplayed,
3294 const char *label, int show_ids, const char *parent_path,
3295 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3297 const struct got_error *err = NULL;
3298 struct got_tree_entry *te;
3299 wchar_t *wline;
3300 int width, n;
3302 *ndisplayed = 0;
3304 werase(view->window);
3306 if (limit == 0)
3307 return NULL;
3309 err = format_line(&wline, &width, label, view->ncols);
3310 if (err)
3311 return err;
3312 if (view_needs_focus_indication(view))
3313 wstandout(view->window);
3314 waddwstr(view->window, wline);
3315 if (view_needs_focus_indication(view))
3316 wstandend(view->window);
3317 free(wline);
3318 wline = NULL;
3319 if (width < view->ncols)
3320 waddch(view->window, '\n');
3321 if (--limit <= 0)
3322 return NULL;
3323 err = format_line(&wline, &width, parent_path, view->ncols);
3324 if (err)
3325 return err;
3326 waddwstr(view->window, wline);
3327 free(wline);
3328 wline = NULL;
3329 if (width < view->ncols)
3330 waddch(view->window, '\n');
3331 if (--limit <= 0)
3332 return NULL;
3333 waddch(view->window, '\n');
3334 if (--limit <= 0)
3335 return NULL;
3337 te = SIMPLEQ_FIRST(&entries->head);
3338 if (*first_displayed_entry == NULL) {
3339 if (selected == 0) {
3340 if (view->focussed)
3341 wstandout(view->window);
3342 *selected_entry = NULL;
3344 waddstr(view->window, " ..\n"); /* parent directory */
3345 if (selected == 0 && view->focussed)
3346 wstandend(view->window);
3347 (*ndisplayed)++;
3348 if (--limit <= 0)
3349 return NULL;
3350 n = 1;
3351 } else {
3352 n = 0;
3353 while (te != *first_displayed_entry)
3354 te = SIMPLEQ_NEXT(te, entry);
3357 while (te) {
3358 char *line = NULL, *id_str = NULL;
3360 if (show_ids) {
3361 err = got_object_id_str(&id_str, te->id);
3362 if (err)
3363 return got_error_from_errno(
3364 "got_object_id_str");
3366 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3367 te->name, S_ISDIR(te->mode) ? "/" :
3368 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3369 free(id_str);
3370 return got_error_from_errno("asprintf");
3372 free(id_str);
3373 err = format_line(&wline, &width, line, view->ncols);
3374 if (err) {
3375 free(line);
3376 break;
3378 if (n == selected) {
3379 if (view->focussed)
3380 wstandout(view->window);
3381 *selected_entry = te;
3383 waddwstr(view->window, wline);
3384 if (width < view->ncols)
3385 waddch(view->window, '\n');
3386 if (n == selected && view->focussed)
3387 wstandend(view->window);
3388 free(line);
3389 free(wline);
3390 wline = NULL;
3391 n++;
3392 (*ndisplayed)++;
3393 *last_displayed_entry = te;
3394 if (--limit <= 0)
3395 break;
3396 te = SIMPLEQ_NEXT(te, entry);
3399 return err;
3402 static void
3403 tree_scroll_up(struct tog_view *view,
3404 struct got_tree_entry **first_displayed_entry, int maxscroll,
3405 const struct got_tree_entries *entries, int isroot)
3407 struct got_tree_entry *te, *prev;
3408 int i;
3410 if (*first_displayed_entry == NULL)
3411 return;
3413 te = SIMPLEQ_FIRST(&entries->head);
3414 if (*first_displayed_entry == te) {
3415 if (!isroot)
3416 *first_displayed_entry = NULL;
3417 return;
3420 /* XXX this is stupid... switch to TAILQ? */
3421 for (i = 0; i < maxscroll; i++) {
3422 while (te != *first_displayed_entry) {
3423 prev = te;
3424 te = SIMPLEQ_NEXT(te, entry);
3426 *first_displayed_entry = prev;
3427 te = SIMPLEQ_FIRST(&entries->head);
3429 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3430 *first_displayed_entry = NULL;
3433 static int
3434 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3435 struct got_tree_entry *last_displayed_entry,
3436 const struct got_tree_entries *entries)
3438 struct got_tree_entry *next, *last;
3439 int n = 0;
3441 if (*first_displayed_entry)
3442 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3443 else
3444 next = SIMPLEQ_FIRST(&entries->head);
3445 last = last_displayed_entry;
3446 while (next && last && n++ < maxscroll) {
3447 last = SIMPLEQ_NEXT(last, entry);
3448 if (last) {
3449 *first_displayed_entry = next;
3450 next = SIMPLEQ_NEXT(next, entry);
3453 return n;
3456 static const struct got_error *
3457 tree_entry_path(char **path, struct tog_parent_trees *parents,
3458 struct got_tree_entry *te)
3460 const struct got_error *err = NULL;
3461 struct tog_parent_tree *pt;
3462 size_t len = 2; /* for leading slash and NUL */
3464 TAILQ_FOREACH(pt, parents, entry)
3465 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3466 if (te)
3467 len += strlen(te->name);
3469 *path = calloc(1, len);
3470 if (path == NULL)
3471 return got_error_from_errno("calloc");
3473 (*path)[0] = '/';
3474 pt = TAILQ_LAST(parents, tog_parent_trees);
3475 while (pt) {
3476 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3477 err = got_error(GOT_ERR_NO_SPACE);
3478 goto done;
3480 if (strlcat(*path, "/", len) >= len) {
3481 err = got_error(GOT_ERR_NO_SPACE);
3482 goto done;
3484 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3486 if (te) {
3487 if (strlcat(*path, te->name, len) >= len) {
3488 err = got_error(GOT_ERR_NO_SPACE);
3489 goto done;
3492 done:
3493 if (err) {
3494 free(*path);
3495 *path = NULL;
3497 return err;
3500 static const struct got_error *
3501 blame_tree_entry(struct tog_view **new_view, int begin_x,
3502 struct got_tree_entry *te, struct tog_parent_trees *parents,
3503 struct got_object_id *commit_id, struct got_reflist_head *refs,
3504 struct got_repository *repo)
3506 const struct got_error *err = NULL;
3507 char *path;
3508 struct tog_view *blame_view;
3510 err = tree_entry_path(&path, parents, te);
3511 if (err)
3512 return err;
3514 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3515 if (blame_view == NULL)
3516 return got_error_from_errno("view_open");
3518 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3519 if (err) {
3520 view_close(blame_view);
3521 free(path);
3522 } else
3523 *new_view = blame_view;
3524 return err;
3527 static const struct got_error *
3528 log_tree_entry(struct tog_view **new_view, int begin_x,
3529 struct got_tree_entry *te, struct tog_parent_trees *parents,
3530 struct got_object_id *commit_id, struct got_reflist_head *refs,
3531 struct got_repository *repo)
3533 struct tog_view *log_view;
3534 const struct got_error *err = NULL;
3535 char *path;
3537 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3538 if (log_view == NULL)
3539 return got_error_from_errno("view_open");
3541 err = tree_entry_path(&path, parents, te);
3542 if (err)
3543 return err;
3545 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3546 if (err)
3547 view_close(log_view);
3548 else
3549 *new_view = log_view;
3550 free(path);
3551 return err;
3554 static const struct got_error *
3555 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3556 struct got_object_id *commit_id, struct got_reflist_head *refs,
3557 struct got_repository *repo)
3559 const struct got_error *err = NULL;
3560 char *commit_id_str = NULL;
3561 struct tog_tree_view_state *s = &view->state.tree;
3563 TAILQ_INIT(&s->parents);
3565 err = got_object_id_str(&commit_id_str, commit_id);
3566 if (err != NULL)
3567 goto done;
3569 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3570 err = got_error_from_errno("asprintf");
3571 goto done;
3574 s->root = s->tree = root;
3575 s->entries = got_object_tree_get_entries(root);
3576 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3577 s->commit_id = got_object_id_dup(commit_id);
3578 if (s->commit_id == NULL) {
3579 err = got_error_from_errno("got_object_id_dup");
3580 goto done;
3582 s->refs = refs;
3583 s->repo = repo;
3585 view->show = show_tree_view;
3586 view->input = input_tree_view;
3587 view->close = close_tree_view;
3588 done:
3589 free(commit_id_str);
3590 if (err) {
3591 free(s->tree_label);
3592 s->tree_label = NULL;
3594 return err;
3597 static const struct got_error *
3598 close_tree_view(struct tog_view *view)
3600 struct tog_tree_view_state *s = &view->state.tree;
3602 free(s->tree_label);
3603 s->tree_label = NULL;
3604 free(s->commit_id);
3605 s->commit_id = NULL;
3606 while (!TAILQ_EMPTY(&s->parents)) {
3607 struct tog_parent_tree *parent;
3608 parent = TAILQ_FIRST(&s->parents);
3609 TAILQ_REMOVE(&s->parents, parent, entry);
3610 free(parent);
3613 if (s->tree != s->root)
3614 got_object_tree_close(s->tree);
3615 got_object_tree_close(s->root);
3617 return NULL;
3620 static const struct got_error *
3621 show_tree_view(struct tog_view *view)
3623 const struct got_error *err = NULL;
3624 struct tog_tree_view_state *s = &view->state.tree;
3625 char *parent_path;
3627 err = tree_entry_path(&parent_path, &s->parents, NULL);
3628 if (err)
3629 return err;
3631 err = draw_tree_entries(view, &s->first_displayed_entry,
3632 &s->last_displayed_entry, &s->selected_entry,
3633 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3634 s->entries, s->selected, view->nlines, s->tree == s->root);
3635 free(parent_path);
3637 view_vborder(view);
3638 return err;
3641 static const struct got_error *
3642 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3643 struct tog_view **focus_view, struct tog_view *view, int ch)
3645 const struct got_error *err = NULL;
3646 struct tog_tree_view_state *s = &view->state.tree;
3647 struct tog_view *log_view;
3648 int begin_x = 0, nscrolled;
3650 switch (ch) {
3651 case 'i':
3652 s->show_ids = !s->show_ids;
3653 break;
3654 case 'l':
3655 if (!s->selected_entry)
3656 break;
3657 if (view_is_parent_view(view))
3658 begin_x = view_split_begin_x(view->begin_x);
3659 err = log_tree_entry(&log_view, begin_x,
3660 s->selected_entry, &s->parents,
3661 s->commit_id, s->refs, s->repo);
3662 if (view_is_parent_view(view)) {
3663 err = view_close_child(view);
3664 if (err)
3665 return err;
3666 err = view_set_child(view, log_view);
3667 if (err) {
3668 view_close(log_view);
3669 break;
3671 *focus_view = log_view;
3672 view->child_focussed = 1;
3673 } else
3674 *new_view = log_view;
3675 break;
3676 case 'k':
3677 case KEY_UP:
3678 if (s->selected > 0) {
3679 s->selected--;
3680 if (s->selected == 0)
3681 break;
3683 if (s->selected > 0)
3684 break;
3685 tree_scroll_up(view, &s->first_displayed_entry, 1,
3686 s->entries, s->tree == s->root);
3687 break;
3688 case KEY_PPAGE:
3689 tree_scroll_up(view, &s->first_displayed_entry,
3690 MAX(0, view->nlines - 4 - s->selected), s->entries,
3691 s->tree == s->root);
3692 s->selected = 0;
3693 if (SIMPLEQ_FIRST(&s->entries->head) ==
3694 s->first_displayed_entry && s->tree != s->root)
3695 s->first_displayed_entry = NULL;
3696 break;
3697 case 'j':
3698 case KEY_DOWN:
3699 if (s->selected < s->ndisplayed - 1) {
3700 s->selected++;
3701 break;
3703 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3704 /* can't scroll any further */
3705 break;
3706 tree_scroll_down(&s->first_displayed_entry, 1,
3707 s->last_displayed_entry, s->entries);
3708 break;
3709 case KEY_NPAGE:
3710 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3711 == NULL) {
3712 /* can't scroll any further; move cursor down */
3713 if (s->selected < s->ndisplayed - 1)
3714 s->selected = s->ndisplayed - 1;
3715 break;
3717 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3718 view->nlines, s->last_displayed_entry, s->entries);
3719 if (nscrolled < view->nlines) {
3720 int ndisplayed = 0;
3721 struct got_tree_entry *te;
3722 te = s->first_displayed_entry;
3723 do {
3724 ndisplayed++;
3725 te = SIMPLEQ_NEXT(te, entry);
3726 } while (te);
3727 s->selected = ndisplayed - 1;
3729 break;
3730 case KEY_ENTER:
3731 case '\r':
3732 case KEY_BACKSPACE:
3733 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3734 struct tog_parent_tree *parent;
3735 /* user selected '..' */
3736 if (s->tree == s->root)
3737 break;
3738 parent = TAILQ_FIRST(&s->parents);
3739 TAILQ_REMOVE(&s->parents, parent,
3740 entry);
3741 got_object_tree_close(s->tree);
3742 s->tree = parent->tree;
3743 s->entries =
3744 got_object_tree_get_entries(s->tree);
3745 s->first_displayed_entry =
3746 parent->first_displayed_entry;
3747 s->selected_entry =
3748 parent->selected_entry;
3749 s->selected = parent->selected;
3750 free(parent);
3751 } else if (S_ISDIR(s->selected_entry->mode)) {
3752 struct tog_parent_tree *parent;
3753 struct got_tree_object *child;
3754 err = got_object_open_as_tree(&child,
3755 s->repo, s->selected_entry->id);
3756 if (err)
3757 break;
3758 parent = calloc(1, sizeof(*parent));
3759 if (parent == NULL) {
3760 err = got_error_from_errno("calloc");
3761 break;
3763 parent->tree = s->tree;
3764 parent->first_displayed_entry =
3765 s->first_displayed_entry;
3766 parent->selected_entry = s->selected_entry;
3767 parent->selected = s->selected;
3768 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3769 s->tree = child;
3770 s->entries =
3771 got_object_tree_get_entries(s->tree);
3772 s->selected = 0;
3773 s->first_displayed_entry = NULL;
3774 } else if (S_ISREG(s->selected_entry->mode)) {
3775 struct tog_view *blame_view;
3776 int begin_x = view_is_parent_view(view) ?
3777 view_split_begin_x(view->begin_x) : 0;
3779 err = blame_tree_entry(&blame_view, begin_x,
3780 s->selected_entry, &s->parents,
3781 s->commit_id, s->refs, s->repo);
3782 if (err)
3783 break;
3784 if (view_is_parent_view(view)) {
3785 err = view_close_child(view);
3786 if (err)
3787 return err;
3788 err = view_set_child(view, blame_view);
3789 if (err) {
3790 view_close(blame_view);
3791 break;
3793 *focus_view = blame_view;
3794 view->child_focussed = 1;
3795 } else
3796 *new_view = blame_view;
3798 break;
3799 case KEY_RESIZE:
3800 if (s->selected > view->nlines)
3801 s->selected = s->ndisplayed - 1;
3802 break;
3803 default:
3804 break;
3807 return err;
3810 __dead static void
3811 usage_tree(void)
3813 endwin();
3814 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3815 getprogname());
3816 exit(1);
3819 static const struct got_error *
3820 cmd_tree(int argc, char *argv[])
3822 const struct got_error *error;
3823 struct got_repository *repo = NULL;
3824 struct got_reflist_head refs;
3825 char *repo_path = NULL;
3826 struct got_object_id *commit_id = NULL;
3827 char *commit_id_arg = NULL;
3828 struct got_commit_object *commit = NULL;
3829 struct got_tree_object *tree = NULL;
3830 int ch;
3831 struct tog_view *view;
3833 SIMPLEQ_INIT(&refs);
3835 #ifndef PROFILE
3836 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3837 NULL) == -1)
3838 err(1, "pledge");
3839 #endif
3841 while ((ch = getopt(argc, argv, "c:")) != -1) {
3842 switch (ch) {
3843 case 'c':
3844 commit_id_arg = optarg;
3845 break;
3846 default:
3847 usage_tree();
3848 /* NOTREACHED */
3852 argc -= optind;
3853 argv += optind;
3855 if (argc == 0) {
3856 struct got_worktree *worktree;
3857 char *cwd = getcwd(NULL, 0);
3858 if (cwd == NULL)
3859 return got_error_from_errno("getcwd");
3860 error = got_worktree_open(&worktree, cwd);
3861 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3862 goto done;
3863 if (worktree) {
3864 free(cwd);
3865 repo_path =
3866 strdup(got_worktree_get_repo_path(worktree));
3867 got_worktree_close(worktree);
3868 } else
3869 repo_path = cwd;
3870 if (repo_path == NULL) {
3871 error = got_error_from_errno("strdup");
3872 goto done;
3874 } else if (argc == 1) {
3875 repo_path = realpath(argv[0], NULL);
3876 if (repo_path == NULL)
3877 return got_error_from_errno2("realpath", argv[0]);
3878 } else
3879 usage_log();
3881 init_curses();
3883 error = got_repo_open(&repo, repo_path);
3884 if (error != NULL)
3885 goto done;
3887 error = apply_unveil(got_repo_get_path(repo), NULL);
3888 if (error)
3889 goto done;
3891 if (commit_id_arg == NULL)
3892 error = get_head_commit_id(&commit_id, repo);
3893 else
3894 error = got_object_resolve_id_str(&commit_id, repo,
3895 commit_id_arg);
3896 if (error != NULL)
3897 goto done;
3899 error = got_object_open_as_commit(&commit, repo, commit_id);
3900 if (error != NULL)
3901 goto done;
3903 error = got_object_open_as_tree(&tree, repo,
3904 got_object_commit_get_tree_id(commit));
3905 if (error != NULL)
3906 goto done;
3908 error = got_ref_list(&refs, repo);
3909 if (error)
3910 goto done;
3912 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3913 if (view == NULL) {
3914 error = got_error_from_errno("view_open");
3915 goto done;
3917 error = open_tree_view(view, tree, commit_id, &refs, repo);
3918 if (error)
3919 goto done;
3920 error = view_loop(view);
3921 done:
3922 free(repo_path);
3923 free(commit_id);
3924 if (commit)
3925 got_object_commit_close(commit);
3926 if (tree)
3927 got_object_tree_close(tree);
3928 if (repo)
3929 got_repo_close(repo);
3930 got_ref_list_free(&refs);
3931 return error;
3934 __dead static void
3935 usage(void)
3937 int i;
3939 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3940 "Available commands:\n", getprogname());
3941 for (i = 0; i < nitems(tog_commands); i++) {
3942 struct tog_cmd *cmd = &tog_commands[i];
3943 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3945 exit(1);
3948 static char **
3949 make_argv(const char *arg0, const char *arg1)
3951 char **argv;
3952 int argc = (arg1 == NULL ? 1 : 2);
3954 argv = calloc(argc, sizeof(char *));
3955 if (argv == NULL)
3956 err(1, "calloc");
3957 argv[0] = strdup(arg0);
3958 if (argv[0] == NULL)
3959 err(1, "calloc");
3960 if (arg1) {
3961 argv[1] = strdup(arg1);
3962 if (argv[1] == NULL)
3963 err(1, "calloc");
3966 return argv;
3969 int
3970 main(int argc, char *argv[])
3972 const struct got_error *error = NULL;
3973 struct tog_cmd *cmd = NULL;
3974 int ch, hflag = 0;
3975 char **cmd_argv = NULL;
3977 setlocale(LC_CTYPE, "");
3979 while ((ch = getopt(argc, argv, "h")) != -1) {
3980 switch (ch) {
3981 case 'h':
3982 hflag = 1;
3983 break;
3984 default:
3985 usage();
3986 /* NOTREACHED */
3990 argc -= optind;
3991 argv += optind;
3992 optind = 0;
3993 optreset = 1;
3995 if (argc == 0) {
3996 if (hflag)
3997 usage();
3998 /* Build an argument vector which runs a default command. */
3999 cmd = &tog_commands[0];
4000 cmd_argv = make_argv(cmd->name, NULL);
4001 argc = 1;
4002 } else {
4003 int i;
4005 /* Did the user specific a command? */
4006 for (i = 0; i < nitems(tog_commands); i++) {
4007 if (strncmp(tog_commands[i].name, argv[0],
4008 strlen(argv[0])) == 0) {
4009 cmd = &tog_commands[i];
4010 if (hflag)
4011 tog_commands[i].cmd_usage();
4012 break;
4015 if (cmd == NULL) {
4016 /* Did the user specify a repository? */
4017 char *repo_path = realpath(argv[0], NULL);
4018 if (repo_path) {
4019 struct got_repository *repo;
4020 error = got_repo_open(&repo, repo_path);
4021 if (error == NULL)
4022 got_repo_close(repo);
4023 } else
4024 error = got_error_from_errno2("realpath",
4025 argv[0]);
4026 if (error) {
4027 if (hflag) {
4028 fprintf(stderr, "%s: '%s' is not a "
4029 "known command\n", getprogname(),
4030 argv[0]);
4031 usage();
4033 fprintf(stderr, "%s: '%s' is neither a known "
4034 "command nor a path to a repository\n",
4035 getprogname(), argv[0]);
4036 free(repo_path);
4037 return 1;
4039 cmd = &tog_commands[0];
4040 cmd_argv = make_argv(cmd->name, repo_path);
4041 argc = 2;
4042 free(repo_path);
4046 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4047 if (error)
4048 goto done;
4049 done:
4050 endwin();
4051 free(cmd_argv);
4052 if (error)
4053 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4054 return 0;