Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_worktree.h"
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 #ifndef MAX
57 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
58 #endif
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 struct tog_cmd {
66 const char *name;
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
69 const char *descr;
70 };
72 __dead static void usage(void);
73 __dead static void usage_log(void);
74 __dead static void usage_diff(void);
75 __dead static void usage_blame(void);
76 __dead static void usage_tree(void);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
83 static struct tog_cmd tog_commands[] = {
84 { "log", cmd_log, usage_log,
85 "show repository history" },
86 { "diff", cmd_diff, usage_diff,
87 "compare files and directories" },
88 { "blame", cmd_blame, usage_blame,
89 "show line-by-line file history" },
90 { "tree", cmd_tree, usage_tree,
91 "browse trees in repository" },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 int idx;
106 };
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
109 int ncommits;
110 struct commit_queue_head head;
111 };
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
115 FILE *f;
116 int first_displayed_line;
117 int last_displayed_line;
118 int eof;
119 int diff_context;
120 struct got_repository *repo;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
125 };
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
131 int commits_needed;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
137 int log_complete;
138 sig_atomic_t *quit;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
142 };
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
149 int selected;
150 char *in_repo_path;
151 struct got_repository *repo;
152 struct got_reflist_head *refs;
153 struct got_object_id *start_id;
154 sig_atomic_t quit;
155 pthread_t thread;
156 struct tog_log_thread_args thread_args;
157 };
159 struct tog_blame_cb_args {
160 struct tog_blame_line *lines; /* one per line */
161 int nlines;
163 struct tog_view *view;
164 struct got_object_id *commit_id;
165 int *quit;
166 };
168 struct tog_blame_thread_args {
169 const char *path;
170 struct got_repository *repo;
171 struct tog_blame_cb_args *cb_args;
172 int *complete;
173 };
175 struct tog_blame {
176 FILE *f;
177 size_t filesize;
178 struct tog_blame_line *lines;
179 int nlines;
180 pthread_t thread;
181 struct tog_blame_thread_args thread_args;
182 struct tog_blame_cb_args cb_args;
183 const char *path;
184 };
186 struct tog_blame_view_state {
187 int first_displayed_line;
188 int last_displayed_line;
189 int selected_line;
190 int blame_complete;
191 int eof;
192 int done;
193 struct got_object_id_queue blamed_commits;
194 struct got_object_qid *blamed_commit;
195 char *path;
196 struct got_repository *repo;
197 struct got_reflist_head *refs;
198 struct got_object_id *commit_id;
199 struct tog_blame blame;
200 };
202 struct tog_parent_tree {
203 TAILQ_ENTRY(tog_parent_tree) entry;
204 struct got_tree_object *tree;
205 struct got_tree_entry *first_displayed_entry;
206 struct got_tree_entry *selected_entry;
207 int selected;
208 };
210 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
212 struct tog_tree_view_state {
213 char *tree_label;
214 struct got_tree_object *root;
215 struct got_tree_object *tree;
216 const struct got_tree_entries *entries;
217 struct got_tree_entry *first_displayed_entry;
218 struct got_tree_entry *last_displayed_entry;
219 struct got_tree_entry *selected_entry;
220 int ndisplayed, selected, show_ids;
221 struct tog_parent_trees parents;
222 struct got_object_id *commit_id;
223 struct got_repository *repo;
224 struct got_reflist_head *refs;
225 };
227 /*
228 * We implement two types of views: parent views and child views.
230 * The 'Tab' key switches between a parent view and its child view.
231 * Child views are shown side-by-side to their parent view, provided
232 * there is enough screen estate.
234 * When a new view is opened from within a parent view, this new view
235 * becomes a child view of the parent view, replacing any existing child.
237 * When a new view is opened from within a child view, this new view
238 * becomes a parent view which will obscure the views below until the
239 * user quits the new parent view by typing 'q'.
241 * This list of views contains parent views only.
242 * Child views are only pointed to by their parent view.
243 */
244 TAILQ_HEAD(tog_view_list_head, tog_view);
246 struct tog_view {
247 TAILQ_ENTRY(tog_view) entry;
248 WINDOW *window;
249 PANEL *panel;
250 int nlines, ncols, begin_y, begin_x;
251 int lines, cols; /* copies of LINES and COLS */
252 int focussed;
253 struct tog_view *parent;
254 struct tog_view *child;
255 int child_focussed;
257 /* type-specific state */
258 enum tog_view_type type;
259 union {
260 struct tog_diff_view_state diff;
261 struct tog_log_view_state log;
262 struct tog_blame_view_state blame;
263 struct tog_tree_view_state tree;
264 } state;
266 const struct got_error *(*show)(struct tog_view *);
267 const struct got_error *(*input)(struct tog_view **,
268 struct tog_view **, struct tog_view**, struct tog_view *, int);
269 const struct got_error *(*close)(struct tog_view *);
270 };
272 static const struct got_error *open_diff_view(struct tog_view *,
273 struct got_object_id *, struct got_object_id *, struct tog_view *,
274 struct got_reflist_head *, struct got_repository *);
275 static const struct got_error *show_diff_view(struct tog_view *);
276 static const struct got_error *input_diff_view(struct tog_view **,
277 struct tog_view **, struct tog_view **, struct tog_view *, int);
278 static const struct got_error* close_diff_view(struct tog_view *);
280 static const struct got_error *open_log_view(struct tog_view *,
281 struct got_object_id *, struct got_reflist_head *,
282 struct got_repository *, const char *, int);
283 static const struct got_error * show_log_view(struct tog_view *);
284 static const struct got_error *input_log_view(struct tog_view **,
285 struct tog_view **, struct tog_view **, struct tog_view *, int);
286 static const struct got_error *close_log_view(struct tog_view *);
288 static const struct got_error *open_blame_view(struct tog_view *, char *,
289 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_blame_view(struct tog_view *);
291 static const struct got_error *input_blame_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error *close_blame_view(struct tog_view *);
295 static const struct got_error *open_tree_view(struct tog_view *,
296 struct got_tree_object *, struct got_object_id *,
297 struct got_reflist_head *, struct got_repository *);
298 static const struct got_error *show_tree_view(struct tog_view *);
299 static const struct got_error *input_tree_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_tree_view(struct tog_view *);
303 static volatile sig_atomic_t tog_sigwinch_received;
305 static void
306 tog_sigwinch(int signo)
308 tog_sigwinch_received = 1;
311 static const struct got_error *
312 view_close(struct tog_view *view)
314 const struct got_error *err = NULL;
316 if (view->child) {
317 view_close(view->child);
318 view->child = NULL;
320 if (view->close)
321 err = view->close(view);
322 if (view->panel)
323 del_panel(view->panel);
324 if (view->window)
325 delwin(view->window);
326 free(view);
327 return err;
330 static struct tog_view *
331 view_open(int nlines, int ncols, int begin_y, int begin_x,
332 enum tog_view_type type)
334 struct tog_view *view = calloc(1, sizeof(*view));
336 if (view == NULL)
337 return NULL;
339 view->type = type;
340 view->lines = LINES;
341 view->cols = COLS;
342 view->nlines = nlines ? nlines : LINES - begin_y;
343 view->ncols = ncols ? ncols : COLS - begin_x;
344 view->begin_y = begin_y;
345 view->begin_x = begin_x;
346 view->window = newwin(nlines, ncols, begin_y, begin_x);
347 if (view->window == NULL) {
348 view_close(view);
349 return NULL;
351 view->panel = new_panel(view->window);
352 if (view->panel == NULL ||
353 set_panel_userptr(view->panel, view) != OK) {
354 view_close(view);
355 return NULL;
358 keypad(view->window, TRUE);
359 return view;
362 static int
363 view_split_begin_x(int begin_x)
365 if (begin_x > 0 || COLS < 120)
366 return 0;
367 return (COLS - MAX(COLS / 2, 80));
370 static const struct got_error *view_resize(struct tog_view *);
372 static const struct got_error *
373 view_splitscreen(struct tog_view *view)
375 const struct got_error *err = NULL;
377 view->begin_y = 0;
378 view->begin_x = view_split_begin_x(0);
379 view->nlines = LINES;
380 view->ncols = COLS - view->begin_x;
381 view->lines = LINES;
382 view->cols = COLS;
383 err = view_resize(view);
384 if (err)
385 return err;
387 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 return got_error_from_errno();
390 return NULL;
393 static const struct got_error *
394 view_fullscreen(struct tog_view *view)
396 const struct got_error *err = NULL;
398 view->begin_x = 0;
399 view->begin_y = 0;
400 view->nlines = LINES;
401 view->ncols = COLS;
402 view->lines = LINES;
403 view->cols = COLS;
404 err = view_resize(view);
405 if (err)
406 return err;
408 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 return got_error_from_errno();
411 return NULL;
414 static int
415 view_is_parent_view(struct tog_view *view)
417 return view->parent == NULL;
420 static const struct got_error *
421 view_resize(struct tog_view *view)
423 int nlines, ncols;
425 if (view->lines > LINES)
426 nlines = view->nlines - (view->lines - LINES);
427 else
428 nlines = view->nlines + (LINES - view->lines);
430 if (view->cols > COLS)
431 ncols = view->ncols - (view->cols - COLS);
432 else
433 ncols = view->ncols + (COLS - view->cols);
435 if (wresize(view->window, nlines, ncols) == ERR)
436 return got_error_from_errno();
437 if (replace_panel(view->panel, view->window) == ERR)
438 return got_error_from_errno();
439 wclear(view->window);
441 view->nlines = nlines;
442 view->ncols = ncols;
443 view->lines = LINES;
444 view->cols = COLS;
446 if (view->child) {
447 view->child->begin_x = view_split_begin_x(view->begin_x);
448 if (view->child->begin_x == 0) {
449 view_fullscreen(view->child);
450 if (view->child->focussed)
451 show_panel(view->child->panel);
452 else
453 show_panel(view->panel);
454 } else {
455 view_splitscreen(view->child);
456 show_panel(view->child->panel);
460 return NULL;
463 static const struct got_error *
464 view_close_child(struct tog_view *view)
466 const struct got_error *err = NULL;
468 if (view->child == NULL)
469 return NULL;
471 err = view_close(view->child);
472 view->child = NULL;
473 return err;
476 static const struct got_error *
477 view_set_child(struct tog_view *view, struct tog_view *child)
479 const struct got_error *err = NULL;
481 view->child = child;
482 child->parent = view;
483 return err;
486 static int
487 view_is_splitscreen(struct tog_view *view)
489 return view->begin_x > 0;
492 /*
493 * Erase all content of the view. Can be used to "flash" the view because
494 * the view loop will redraw it quickly, providing a more subtle visual
495 * effect than curs_flash(3) would provide.
496 */
497 static void
498 view_flash(struct tog_view *view)
500 werase(view->window);
501 update_panels();
502 doupdate();
505 static void
506 tog_resizeterm(void)
508 int cols, lines;
509 struct winsize size;
511 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
512 cols = 80; /* Default */
513 lines = 24;
514 } else {
515 cols = size.ws_col;
516 lines = size.ws_row;
518 resize_term(lines, cols);
521 static const struct got_error *
522 view_input(struct tog_view **new, struct tog_view **dead,
523 struct tog_view **focus, int *done, struct tog_view *view,
524 struct tog_view_list_head *views)
526 const struct got_error *err = NULL;
527 struct tog_view *v;
528 int ch, errcode;
530 *new = NULL;
531 *dead = NULL;
532 *focus = NULL;
534 nodelay(stdscr, FALSE);
535 /* Allow threads to make progress while we are waiting for input. */
536 errcode = pthread_mutex_unlock(&tog_mutex);
537 if (errcode)
538 return got_error_set_errno(errcode);
539 ch = wgetch(view->window);
540 errcode = pthread_mutex_lock(&tog_mutex);
541 if (errcode)
542 return got_error_set_errno(errcode);
543 nodelay(stdscr, TRUE);
545 if (tog_sigwinch_received) {
546 tog_resizeterm();
547 tog_sigwinch_received = 0;
548 TAILQ_FOREACH(v, views, entry) {
549 err = view_resize(v);
550 if (err)
551 return err;
552 err = v->input(new, dead, focus, v, KEY_RESIZE);
553 if (err)
554 return err;
558 switch (ch) {
559 case ERR:
560 break;
561 case '\t':
562 if (view->child) {
563 *focus = view->child;
564 view->child_focussed = 1;
565 } else if (view->parent) {
566 *focus = view->parent;
567 view->parent->child_focussed = 0;
569 break;
570 case 'q':
571 err = view->input(new, dead, focus, view, ch);
572 *dead = view;
573 break;
574 case 'Q':
575 *done = 1;
576 break;
577 case 'f':
578 if (view_is_parent_view(view)) {
579 if (view->child == NULL)
580 break;
581 if (view_is_splitscreen(view->child)) {
582 *focus = view->child;
583 view->child_focussed = 1;
584 err = view_fullscreen(view->child);
585 } else
586 err = view_splitscreen(view->child);
587 if (err)
588 break;
589 err = view->child->input(new, dead, focus,
590 view->child, KEY_RESIZE);
591 } else {
592 if (view_is_splitscreen(view)) {
593 *focus = view;
594 view->parent->child_focussed = 1;
595 err = view_fullscreen(view);
596 } else {
597 err = view_splitscreen(view);
599 if (err)
600 break;
601 err = view->input(new, dead, focus, view,
602 KEY_RESIZE);
604 break;
605 case KEY_RESIZE:
606 break;
607 default:
608 err = view->input(new, dead, focus, view, ch);
609 break;
612 return err;
615 void
616 view_vborder(struct tog_view *view)
618 PANEL *panel;
619 struct tog_view *view_above;
621 if (view->parent)
622 return view_vborder(view->parent);
624 panel = panel_above(view->panel);
625 if (panel == NULL)
626 return;
628 view_above = panel_userptr(panel);
629 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
630 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
633 int
634 view_needs_focus_indication(struct tog_view *view)
636 if (view_is_parent_view(view)) {
637 if (view->child == NULL || view->child_focussed)
638 return 0;
639 if (!view_is_splitscreen(view->child))
640 return 0;
641 } else if (!view_is_splitscreen(view))
642 return 0;
644 return view->focussed;
647 static const struct got_error *
648 view_loop(struct tog_view *view)
650 const struct got_error *err = NULL;
651 struct tog_view_list_head views;
652 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
653 int fast_refresh = 10;
654 int done = 0, errcode;
656 errcode = pthread_mutex_lock(&tog_mutex);
657 if (errcode)
658 return got_error_set_errno(errcode);
660 TAILQ_INIT(&views);
661 TAILQ_INSERT_HEAD(&views, view, entry);
663 main_view = view;
664 view->focussed = 1;
665 err = view->show(view);
666 if (err)
667 return err;
668 update_panels();
669 doupdate();
670 while (!TAILQ_EMPTY(&views) && !done) {
671 /* Refresh fast during initialization, then become slower. */
672 if (fast_refresh && fast_refresh-- == 0)
673 halfdelay(10); /* switch to once per second */
675 err = view_input(&new_view, &dead_view, &focus_view, &done,
676 view, &views);
677 if (err)
678 break;
679 if (dead_view) {
680 struct tog_view *prev = NULL;
682 if (view_is_parent_view(dead_view))
683 prev = TAILQ_PREV(dead_view,
684 tog_view_list_head, entry);
685 else if (view->parent != dead_view)
686 prev = view->parent;
688 if (dead_view->parent)
689 dead_view->parent->child = NULL;
690 else
691 TAILQ_REMOVE(&views, dead_view, entry);
693 err = view_close(dead_view);
694 if (err || dead_view == main_view)
695 goto done;
697 if (view == dead_view) {
698 if (focus_view)
699 view = focus_view;
700 else if (prev)
701 view = prev;
702 else if (!TAILQ_EMPTY(&views))
703 view = TAILQ_LAST(&views,
704 tog_view_list_head);
705 else
706 view = NULL;
707 if (view) {
708 if (view->child && view->child_focussed)
709 focus_view = view->child;
710 else
711 focus_view = view;
715 if (new_view) {
716 struct tog_view *v, *t;
717 /* Only allow one parent view per type. */
718 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
719 if (v->type != new_view->type)
720 continue;
721 TAILQ_REMOVE(&views, v, entry);
722 err = view_close(v);
723 if (err)
724 goto done;
725 break;
727 TAILQ_INSERT_TAIL(&views, new_view, entry);
728 view = new_view;
729 if (focus_view == NULL)
730 focus_view = new_view;
732 if (focus_view) {
733 show_panel(focus_view->panel);
734 if (view)
735 view->focussed = 0;
736 focus_view->focussed = 1;
737 view = focus_view;
738 if (new_view)
739 show_panel(new_view->panel);
740 if (view->child && view_is_splitscreen(view->child))
741 show_panel(view->child->panel);
743 if (view) {
744 if (focus_view == NULL) {
745 view->focussed = 1;
746 show_panel(view->panel);
747 if (view->child && view_is_splitscreen(view->child))
748 show_panel(view->child->panel);
749 focus_view = view;
751 if (view->parent) {
752 err = view->parent->show(view->parent);
753 if (err)
754 goto done;
756 err = view->show(view);
757 if (err)
758 goto done;
759 if (view->child) {
760 err = view->child->show(view->child);
761 if (err)
762 goto done;
764 update_panels();
765 doupdate();
768 done:
769 while (!TAILQ_EMPTY(&views)) {
770 view = TAILQ_FIRST(&views);
771 TAILQ_REMOVE(&views, view, entry);
772 view_close(view);
775 errcode = pthread_mutex_unlock(&tog_mutex);
776 if (errcode)
777 return got_error_set_errno(errcode);
779 return err;
782 __dead static void
783 usage_log(void)
785 endwin();
786 fprintf(stderr,
787 "usage: %s log [-c commit] [-r repository-path] [path]\n",
788 getprogname());
789 exit(1);
792 /* Create newly allocated wide-character string equivalent to a byte string. */
793 static const struct got_error *
794 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
796 char *vis = NULL;
797 const struct got_error *err = NULL;
799 *ws = NULL;
800 *wlen = mbstowcs(NULL, s, 0);
801 if (*wlen == (size_t)-1) {
802 int vislen;
803 if (errno != EILSEQ)
804 return got_error_from_errno();
806 /* byte string invalid in current encoding; try to "fix" it */
807 err = got_mbsavis(&vis, &vislen, s);
808 if (err)
809 return err;
810 *wlen = mbstowcs(NULL, vis, 0);
811 if (*wlen == (size_t)-1) {
812 err = got_error_from_errno(); /* give up */
813 goto done;
817 *ws = calloc(*wlen + 1, sizeof(*ws));
818 if (*ws == NULL) {
819 err = got_error_from_errno();
820 goto done;
823 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
824 err = got_error_from_errno();
825 done:
826 free(vis);
827 if (err) {
828 free(*ws);
829 *ws = NULL;
830 *wlen = 0;
832 return err;
835 /* Format a line for display, ensuring that it won't overflow a width limit. */
836 static const struct got_error *
837 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
839 const struct got_error *err = NULL;
840 int cols = 0;
841 wchar_t *wline = NULL;
842 size_t wlen;
843 int i;
845 *wlinep = NULL;
846 *widthp = 0;
848 err = mbs2ws(&wline, &wlen, line);
849 if (err)
850 return err;
852 i = 0;
853 while (i < wlen && cols < wlimit) {
854 int width = wcwidth(wline[i]);
855 switch (width) {
856 case 0:
857 i++;
858 break;
859 case 1:
860 case 2:
861 if (cols + width <= wlimit)
862 cols += width;
863 i++;
864 break;
865 case -1:
866 if (wline[i] == L'\t')
867 cols += TABSIZE - ((cols + 1) % TABSIZE);
868 i++;
869 break;
870 default:
871 err = got_error_from_errno();
872 goto done;
875 wline[i] = L'\0';
876 if (widthp)
877 *widthp = cols;
878 done:
879 if (err)
880 free(wline);
881 else
882 *wlinep = wline;
883 return err;
886 static const struct got_error*
887 build_refs_str(char **refs_str, struct got_reflist_head *refs,
888 struct got_object_id *id)
890 static const struct got_error *err = NULL;
891 struct got_reflist_entry *re;
892 char *s;
893 const char *name;
895 *refs_str = NULL;
897 SIMPLEQ_FOREACH(re, refs, entry) {
898 if (got_object_id_cmp(re->id, id) != 0)
899 continue;
900 name = got_ref_get_name(re->ref);
901 if (strcmp(name, GOT_REF_HEAD) == 0)
902 continue;
903 if (strncmp(name, "refs/", 5) == 0)
904 name += 5;
905 if (strncmp(name, "got/", 4) == 0)
906 continue;
907 if (strncmp(name, "heads/", 6) == 0)
908 name += 6;
909 if (strncmp(name, "remotes/", 8) == 0)
910 name += 8;
911 s = *refs_str;
912 if (asprintf(refs_str, "%s%s%s", s ? s : "",
913 s ? ", " : "", name) == -1) {
914 err = got_error_from_errno();
915 free(s);
916 *refs_str = NULL;
917 break;
919 free(s);
922 return err;
925 static const struct got_error *
926 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
928 char *smallerthan, *at;
930 smallerthan = strchr(author, '<');
931 if (smallerthan && smallerthan[1] != '\0')
932 author = smallerthan + 1;
933 at = strchr(author, '@');
934 if (at)
935 *at = '\0';
936 return format_line(wauthor, author_width, author, limit);
939 static const struct got_error *
940 draw_commit(struct tog_view *view, struct got_commit_object *commit,
941 struct got_object_id *id, struct got_reflist_head *refs,
942 int author_display_cols)
944 const struct got_error *err = NULL;
945 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
946 char *logmsg0 = NULL, *logmsg = NULL;
947 char *author = NULL;
948 wchar_t *wlogmsg = NULL, *wauthor = NULL;
949 int author_width, logmsg_width;
950 char *newline, *line = NULL;
951 int col, limit;
952 static const size_t date_display_cols = 9;
953 const int avail = view->ncols;
954 struct tm tm;
955 time_t committer_time;
957 committer_time = got_object_commit_get_committer_time(commit);
958 if (localtime_r(&committer_time, &tm) == NULL)
959 return got_error_from_errno();
960 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
961 >= sizeof(datebuf))
962 return got_error(GOT_ERR_NO_SPACE);
964 if (avail < date_display_cols)
965 limit = MIN(sizeof(datebuf) - 1, avail);
966 else
967 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
968 waddnstr(view->window, datebuf, limit);
969 col = limit + 1;
970 if (col > avail)
971 goto done;
973 author = strdup(got_object_commit_get_author(commit));
974 if (author == NULL) {
975 err = got_error_from_errno();
976 goto done;
978 err = format_author(&wauthor, &author_width, author, avail - col);
979 if (err)
980 goto done;
981 waddwstr(view->window, wauthor);
982 col += author_width;
983 while (col <= avail && author_width < author_display_cols + 2) {
984 waddch(view->window, ' ');
985 col++;
986 author_width++;
988 if (col > avail)
989 goto done;
991 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
992 if (logmsg0 == NULL) {
993 err = got_error_from_errno();
994 goto done;
996 logmsg = logmsg0;
997 while (*logmsg == '\n')
998 logmsg++;
999 newline = strchr(logmsg, '\n');
1000 if (newline)
1001 *newline = '\0';
1002 limit = avail - col;
1003 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1004 if (err)
1005 goto done;
1006 waddwstr(view->window, wlogmsg);
1007 col += logmsg_width;
1008 while (col <= avail) {
1009 waddch(view->window, ' ');
1010 col++;
1012 done:
1013 free(logmsg0);
1014 free(wlogmsg);
1015 free(author);
1016 free(wauthor);
1017 free(line);
1018 return err;
1021 static struct commit_queue_entry *
1022 alloc_commit_queue_entry(struct got_commit_object *commit,
1023 struct got_object_id *id)
1025 struct commit_queue_entry *entry;
1027 entry = calloc(1, sizeof(*entry));
1028 if (entry == NULL)
1029 return NULL;
1031 entry->id = id;
1032 entry->commit = commit;
1033 return entry;
1036 static void
1037 pop_commit(struct commit_queue *commits)
1039 struct commit_queue_entry *entry;
1041 entry = TAILQ_FIRST(&commits->head);
1042 TAILQ_REMOVE(&commits->head, entry, entry);
1043 got_object_commit_close(entry->commit);
1044 commits->ncommits--;
1045 /* Don't free entry->id! It is owned by the commit graph. */
1046 free(entry);
1049 static void
1050 free_commits(struct commit_queue *commits)
1052 while (!TAILQ_EMPTY(&commits->head))
1053 pop_commit(commits);
1056 static const struct got_error *
1057 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1058 int minqueue, struct got_repository *repo, const char *path)
1060 const struct got_error *err = NULL;
1061 int nqueued = 0;
1064 * We keep all commits open throughout the lifetime of the log
1065 * view in order to avoid having to re-fetch commits from disk
1066 * while updating the display.
1068 while (nqueued < minqueue) {
1069 struct got_object_id *id;
1070 struct got_commit_object *commit;
1071 struct commit_queue_entry *entry;
1072 int errcode;
1074 err = got_commit_graph_iter_next(&id, graph);
1075 if (err) {
1076 if (err->code != GOT_ERR_ITER_NEED_MORE)
1077 break;
1078 err = got_commit_graph_fetch_commits(graph,
1079 minqueue, repo);
1080 if (err)
1081 return err;
1082 continue;
1085 if (id == NULL)
1086 break;
1088 err = got_object_open_as_commit(&commit, repo, id);
1089 if (err)
1090 break;
1091 entry = alloc_commit_queue_entry(commit, id);
1092 if (entry == NULL) {
1093 err = got_error_from_errno();
1094 break;
1097 errcode = pthread_mutex_lock(&tog_mutex);
1098 if (errcode) {
1099 err = got_error_set_errno(errcode);
1100 break;
1103 entry->idx = commits->ncommits;
1104 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1105 nqueued++;
1106 commits->ncommits++;
1108 errcode = pthread_mutex_unlock(&tog_mutex);
1109 if (errcode && err == NULL)
1110 err = got_error_set_errno(errcode);
1113 return err;
1116 static const struct got_error *
1117 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1119 const struct got_error *err = NULL;
1120 struct got_reference *head_ref;
1122 *head_id = NULL;
1124 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1125 if (err)
1126 return err;
1128 err = got_ref_resolve(head_id, repo, head_ref);
1129 got_ref_close(head_ref);
1130 if (err) {
1131 *head_id = NULL;
1132 return err;
1135 return NULL;
1138 static const struct got_error *
1139 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1140 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1141 struct commit_queue *commits, int selected_idx, int limit,
1142 struct got_reflist_head *refs, const char *path, int commits_needed)
1144 const struct got_error *err = NULL;
1145 struct commit_queue_entry *entry;
1146 int ncommits, width;
1147 int author_cols = 10;
1148 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1149 char *refs_str = NULL;
1150 wchar_t *wline;
1152 entry = first;
1153 ncommits = 0;
1154 while (entry) {
1155 if (ncommits == selected_idx) {
1156 *selected = entry;
1157 break;
1159 entry = TAILQ_NEXT(entry, entry);
1160 ncommits++;
1163 if (*selected) {
1164 err = got_object_id_str(&id_str, (*selected)->id);
1165 if (err)
1166 return err;
1167 if (refs) {
1168 err = build_refs_str(&refs_str, refs, (*selected)->id);
1169 if (err)
1170 goto done;
1174 if (commits_needed == 0)
1175 halfdelay(10); /* disable fast refresh */
1177 if (asprintf(&ncommits_str, " [%d/%d] %s",
1178 entry ? entry->idx + 1 : 0, commits->ncommits,
1179 commits_needed > 0 ? "loading... " :
1180 (refs_str ? refs_str : "")) == -1) {
1181 err = got_error_from_errno();
1182 goto done;
1185 if (path && strcmp(path, "/") != 0) {
1186 if (asprintf(&header, "commit %s %s%s",
1187 id_str ? id_str : "........................................",
1188 path, ncommits_str) == -1) {
1189 err = got_error_from_errno();
1190 header = NULL;
1191 goto done;
1193 } else if (asprintf(&header, "commit %s%s",
1194 id_str ? id_str : "........................................",
1195 ncommits_str) == -1) {
1196 err = got_error_from_errno();
1197 header = NULL;
1198 goto done;
1200 err = format_line(&wline, &width, header, view->ncols);
1201 if (err)
1202 goto done;
1204 werase(view->window);
1206 if (view_needs_focus_indication(view))
1207 wstandout(view->window);
1208 waddwstr(view->window, wline);
1209 while (width < view->ncols) {
1210 waddch(view->window, ' ');
1211 width++;
1213 if (view_needs_focus_indication(view))
1214 wstandend(view->window);
1215 free(wline);
1216 if (limit <= 1)
1217 goto done;
1219 /* Grow author column size if necessary. */
1220 entry = first;
1221 ncommits = 0;
1222 while (entry) {
1223 char *author;
1224 wchar_t *wauthor;
1225 int width;
1226 if (ncommits >= limit - 1)
1227 break;
1228 author = strdup(got_object_commit_get_author(entry->commit));
1229 if (author == NULL) {
1230 err = got_error_from_errno();
1231 goto done;
1233 err = format_author(&wauthor, &width, author, COLS);
1234 if (author_cols < width)
1235 author_cols = width;
1236 free(wauthor);
1237 free(author);
1238 entry = TAILQ_NEXT(entry, entry);
1241 entry = first;
1242 *last = first;
1243 ncommits = 0;
1244 while (entry) {
1245 if (ncommits >= limit - 1)
1246 break;
1247 if (ncommits == selected_idx)
1248 wstandout(view->window);
1249 err = draw_commit(view, entry->commit, entry->id, refs,
1250 author_cols);
1251 if (ncommits == selected_idx)
1252 wstandend(view->window);
1253 if (err)
1254 break;
1255 ncommits++;
1256 *last = entry;
1257 entry = TAILQ_NEXT(entry, entry);
1260 view_vborder(view);
1261 done:
1262 free(id_str);
1263 free(refs_str);
1264 free(ncommits_str);
1265 free(header);
1266 return err;
1269 static void
1270 scroll_up(struct tog_view *view,
1271 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1272 struct commit_queue *commits)
1274 struct commit_queue_entry *entry;
1275 int nscrolled = 0;
1277 entry = TAILQ_FIRST(&commits->head);
1278 if (*first_displayed_entry == entry) {
1279 view_flash(view);
1280 return;
1283 entry = *first_displayed_entry;
1284 while (entry && nscrolled < maxscroll) {
1285 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1286 if (entry) {
1287 *first_displayed_entry = entry;
1288 nscrolled++;
1293 static const struct got_error *
1294 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1295 pthread_cond_t *need_commits)
1297 int errcode;
1298 int max_wait = 20;
1300 halfdelay(1); /* fast refresh while loading commits */
1302 while (*commits_needed > 0) {
1303 if (*log_complete)
1304 break;
1306 /* Wake the log thread. */
1307 errcode = pthread_cond_signal(need_commits);
1308 if (errcode)
1309 return got_error_set_errno(errcode);
1310 errcode = pthread_mutex_unlock(&tog_mutex);
1311 if (errcode)
1312 return got_error_set_errno(errcode);
1313 pthread_yield();
1314 errcode = pthread_mutex_lock(&tog_mutex);
1315 if (errcode)
1316 return got_error_set_errno(errcode);
1318 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1320 * Thread is not done yet; lose a key press
1321 * and let the user retry... this way the GUI
1322 * remains interactive while logging deep paths
1323 * with few commits in history.
1325 return NULL;
1329 return NULL;
1332 static const struct got_error *
1333 scroll_down(struct tog_view *view,
1334 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1335 struct commit_queue_entry **last_displayed_entry,
1336 struct commit_queue *commits, int *log_complete, int *commits_needed,
1337 pthread_cond_t *need_commits)
1339 const struct got_error *err = NULL;
1340 struct commit_queue_entry *pentry;
1341 int nscrolled = 0;
1343 if (*last_displayed_entry == NULL)
1344 return NULL;
1346 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1347 if (pentry == NULL && !*log_complete) {
1349 * Ask the log thread for required amount of commits
1350 * plus some amount of pre-fetching.
1352 (*commits_needed) += maxscroll + 20;
1353 err = trigger_log_thread(0, commits_needed, log_complete,
1354 need_commits);
1355 if (err)
1356 return err;
1359 do {
1360 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1361 if (pentry == NULL) {
1362 if (*log_complete)
1363 view_flash(view);
1364 break;
1367 *last_displayed_entry = pentry;
1369 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1370 if (pentry == NULL)
1371 break;
1372 *first_displayed_entry = pentry;
1373 } while (++nscrolled < maxscroll);
1375 return err;
1378 static const struct got_error *
1379 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1380 struct got_commit_object *commit, struct got_object_id *commit_id,
1381 struct tog_view *log_view, struct got_reflist_head *refs,
1382 struct got_repository *repo)
1384 const struct got_error *err;
1385 struct got_object_qid *parent_id;
1386 struct tog_view *diff_view;
1388 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1389 if (diff_view == NULL)
1390 return got_error_from_errno();
1392 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1393 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1394 commit_id, log_view, refs, repo);
1395 if (err == NULL)
1396 *new_view = diff_view;
1397 return err;
1400 static const struct got_error *
1401 browse_commit(struct tog_view **new_view, int begin_x,
1402 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1403 struct got_repository *repo)
1405 const struct got_error *err = NULL;
1406 struct got_tree_object *tree;
1407 struct tog_view *tree_view;
1409 err = got_object_open_as_tree(&tree, repo,
1410 got_object_commit_get_tree_id(entry->commit));
1411 if (err)
1412 return err;
1414 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1415 if (tree_view == NULL)
1416 return got_error_from_errno();
1418 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1419 if (err)
1420 got_object_tree_close(tree);
1421 else
1422 *new_view = tree_view;
1423 return err;
1426 static void *
1427 log_thread(void *arg)
1429 const struct got_error *err = NULL;
1430 int errcode = 0;
1431 struct tog_log_thread_args *a = arg;
1432 int done = 0;
1434 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1435 if (err)
1436 return (void *)err;
1438 while (!done && !err) {
1439 err = queue_commits(a->graph, a->commits, 1, a->repo,
1440 a->in_repo_path);
1441 if (err) {
1442 if (err->code != GOT_ERR_ITER_COMPLETED)
1443 return (void *)err;
1444 err = NULL;
1445 done = 1;
1446 } else if (a->commits_needed > 0)
1447 a->commits_needed--;
1449 errcode = pthread_mutex_lock(&tog_mutex);
1450 if (errcode)
1451 return (void *)got_error_set_errno(errcode);
1453 if (done)
1454 a->log_complete = 1;
1455 else if (*a->quit) {
1456 done = 1;
1457 a->log_complete = 1;
1458 } else if (*a->first_displayed_entry == NULL) {
1459 *a->first_displayed_entry =
1460 TAILQ_FIRST(&a->commits->head);
1461 *a->selected_entry = *a->first_displayed_entry;
1464 if (done)
1465 a->commits_needed = 0;
1466 else if (a->commits_needed == 0) {
1467 errcode = pthread_cond_wait(&a->need_commits,
1468 &tog_mutex);
1469 if (errcode)
1470 err = got_error_set_errno(errcode);
1473 errcode = pthread_mutex_unlock(&tog_mutex);
1474 if (errcode && err == NULL)
1475 err = got_error_set_errno(errcode);
1477 return (void *)err;
1480 static const struct got_error *
1481 stop_log_thread(struct tog_log_view_state *s)
1483 const struct got_error *err = NULL;
1484 int errcode;
1486 if (s->thread) {
1487 s->quit = 1;
1488 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1489 if (errcode)
1490 return got_error_set_errno(errcode);
1491 errcode = pthread_mutex_unlock(&tog_mutex);
1492 if (errcode)
1493 return got_error_set_errno(errcode);
1494 errcode = pthread_join(s->thread, (void **)&err);
1495 if (errcode)
1496 return got_error_set_errno(errcode);
1497 errcode = pthread_mutex_lock(&tog_mutex);
1498 if (errcode)
1499 return got_error_set_errno(errcode);
1500 s->thread = NULL;
1503 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1504 if (errcode && err == NULL)
1505 err = got_error_set_errno(errcode);
1507 if (s->thread_args.repo) {
1508 got_repo_close(s->thread_args.repo);
1509 s->thread_args.repo = NULL;
1512 if (s->thread_args.graph) {
1513 got_commit_graph_close(s->thread_args.graph);
1514 s->thread_args.graph = NULL;
1517 return err;
1520 static const struct got_error *
1521 close_log_view(struct tog_view *view)
1523 const struct got_error *err = NULL;
1524 struct tog_log_view_state *s = &view->state.log;
1526 err = stop_log_thread(s);
1527 free_commits(&s->commits);
1528 free(s->in_repo_path);
1529 s->in_repo_path = NULL;
1530 free(s->start_id);
1531 s->start_id = NULL;
1532 return err;
1535 static const struct got_error *
1536 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1537 struct got_reflist_head *refs, struct got_repository *repo,
1538 const char *path, int check_disk)
1540 const struct got_error *err = NULL;
1541 struct tog_log_view_state *s = &view->state.log;
1542 struct got_repository *thread_repo = NULL;
1543 struct got_commit_graph *thread_graph = NULL;
1544 int errcode;
1546 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1547 if (err != NULL)
1548 goto done;
1550 /* The commit queue only contains commits being displayed. */
1551 TAILQ_INIT(&s->commits.head);
1552 s->commits.ncommits = 0;
1554 s->refs = refs;
1555 s->repo = repo;
1556 s->start_id = got_object_id_dup(start_id);
1557 if (s->start_id == NULL) {
1558 err = got_error_from_errno();
1559 goto done;
1562 view->show = show_log_view;
1563 view->input = input_log_view;
1564 view->close = close_log_view;
1566 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1567 if (err)
1568 goto done;
1569 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1570 0, thread_repo);
1571 if (err)
1572 goto done;
1574 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1575 if (errcode) {
1576 err = got_error_set_errno(errcode);
1577 goto done;
1580 s->thread_args.commits_needed = view->nlines;
1581 s->thread_args.graph = thread_graph;
1582 s->thread_args.commits = &s->commits;
1583 s->thread_args.in_repo_path = s->in_repo_path;
1584 s->thread_args.start_id = s->start_id;
1585 s->thread_args.repo = thread_repo;
1586 s->thread_args.log_complete = 0;
1587 s->thread_args.quit = &s->quit;
1588 s->thread_args.view = view;
1589 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1590 s->thread_args.selected_entry = &s->selected_entry;
1591 done:
1592 if (err)
1593 close_log_view(view);
1594 return err;
1597 static const struct got_error *
1598 show_log_view(struct tog_view *view)
1600 struct tog_log_view_state *s = &view->state.log;
1602 if (s->thread == NULL) {
1603 int errcode = pthread_create(&s->thread, NULL, log_thread,
1604 &s->thread_args);
1605 if (errcode)
1606 return got_error_set_errno(errcode);
1609 return draw_commits(view, &s->last_displayed_entry,
1610 &s->selected_entry, s->first_displayed_entry,
1611 &s->commits, s->selected, view->nlines, s->refs,
1612 s->in_repo_path, s->thread_args.commits_needed);
1615 static const struct got_error *
1616 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1617 struct tog_view **focus_view, struct tog_view *view, int ch)
1619 const struct got_error *err = NULL;
1620 struct tog_log_view_state *s = &view->state.log;
1621 char *parent_path;
1622 struct tog_view *diff_view = NULL, *tree_view = NULL;
1623 int begin_x = 0;
1625 switch (ch) {
1626 case 'q':
1627 s->quit = 1;
1628 break;
1629 case 'k':
1630 case KEY_UP:
1631 case '<':
1632 case ',':
1633 if (s->first_displayed_entry == NULL)
1634 break;
1635 if (s->selected > 0)
1636 s->selected--;
1637 if (s->selected > 0)
1638 break;
1639 scroll_up(view, &s->first_displayed_entry, 1,
1640 &s->commits);
1641 break;
1642 case KEY_PPAGE:
1643 if (s->first_displayed_entry == NULL)
1644 break;
1645 if (TAILQ_FIRST(&s->commits.head) ==
1646 s->first_displayed_entry) {
1647 if (s->selected == 0) {
1648 view_flash(view);
1649 break;
1651 s->selected = 0;
1652 break;
1654 scroll_up(view, &s->first_displayed_entry,
1655 view->nlines, &s->commits);
1656 break;
1657 case 'j':
1658 case KEY_DOWN:
1659 case '>':
1660 case '.':
1661 if (s->first_displayed_entry == NULL)
1662 break;
1663 if (s->selected < MIN(view->nlines - 2,
1664 s->commits.ncommits - 1)) {
1665 s->selected++;
1666 break;
1668 err = scroll_down(view, &s->first_displayed_entry, 1,
1669 &s->last_displayed_entry, &s->commits,
1670 &s->thread_args.log_complete,
1671 &s->thread_args.commits_needed,
1672 &s->thread_args.need_commits);
1673 break;
1674 case KEY_NPAGE: {
1675 struct commit_queue_entry *first;
1676 first = s->first_displayed_entry;
1677 if (first == NULL)
1678 break;
1679 err = scroll_down(view, &s->first_displayed_entry,
1680 view->nlines, &s->last_displayed_entry,
1681 &s->commits, &s->thread_args.log_complete,
1682 &s->thread_args.commits_needed,
1683 &s->thread_args.need_commits);
1684 if (first == s->first_displayed_entry &&
1685 s->selected < MIN(view->nlines - 2,
1686 s->commits.ncommits - 1)) {
1687 /* can't scroll further down */
1688 s->selected = MIN(view->nlines - 2,
1689 s->commits.ncommits - 1);
1691 err = NULL;
1692 break;
1694 case KEY_RESIZE:
1695 if (s->selected > view->nlines - 2)
1696 s->selected = view->nlines - 2;
1697 if (s->selected > s->commits.ncommits - 1)
1698 s->selected = s->commits.ncommits - 1;
1699 break;
1700 case KEY_ENTER:
1701 case '\r':
1702 if (s->selected_entry == NULL)
1703 break;
1704 if (view_is_parent_view(view))
1705 begin_x = view_split_begin_x(view->begin_x);
1706 err = open_diff_view_for_commit(&diff_view, begin_x,
1707 s->selected_entry->commit, s->selected_entry->id,
1708 view, s->refs, s->repo);
1709 if (err)
1710 break;
1711 if (view_is_parent_view(view)) {
1712 err = view_close_child(view);
1713 if (err)
1714 return err;
1715 err = view_set_child(view, diff_view);
1716 if (err) {
1717 view_close(diff_view);
1718 break;
1720 *focus_view = diff_view;
1721 view->child_focussed = 1;
1722 } else
1723 *new_view = diff_view;
1724 break;
1725 case 't':
1726 if (s->selected_entry == NULL)
1727 break;
1728 if (view_is_parent_view(view))
1729 begin_x = view_split_begin_x(view->begin_x);
1730 err = browse_commit(&tree_view, begin_x,
1731 s->selected_entry, s->refs, s->repo);
1732 if (err)
1733 break;
1734 if (view_is_parent_view(view)) {
1735 err = view_close_child(view);
1736 if (err)
1737 return err;
1738 err = view_set_child(view, tree_view);
1739 if (err) {
1740 view_close(tree_view);
1741 break;
1743 *focus_view = tree_view;
1744 view->child_focussed = 1;
1745 } else
1746 *new_view = tree_view;
1747 break;
1748 case KEY_BACKSPACE:
1749 if (strcmp(s->in_repo_path, "/") == 0)
1750 break;
1751 parent_path = dirname(s->in_repo_path);
1752 if (parent_path && strcmp(parent_path, ".") != 0) {
1753 struct tog_view *lv;
1754 err = stop_log_thread(s);
1755 if (err)
1756 return err;
1757 lv = view_open(view->nlines, view->ncols,
1758 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1759 if (lv == NULL)
1760 return got_error_from_errno();
1761 err = open_log_view(lv, s->start_id, s->refs,
1762 s->repo, parent_path, 0);
1763 if (err)
1764 return err;;
1765 if (view_is_parent_view(view))
1766 *new_view = lv;
1767 else {
1768 view_set_child(view->parent, lv);
1769 *focus_view = lv;
1771 return NULL;
1773 break;
1774 default:
1775 break;
1778 return err;
1781 static const struct got_error *
1782 apply_unveil(const char *repo_path, const char *worktree_path)
1784 const struct got_error *error;
1786 if (repo_path && unveil(repo_path, "r") != 0)
1787 return got_error_from_errno();
1789 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1790 return got_error_from_errno();
1792 if (unveil("/tmp", "rwc") != 0)
1793 return got_error_from_errno();
1795 error = got_privsep_unveil_exec_helpers();
1796 if (error != NULL)
1797 return error;
1799 if (unveil(NULL, NULL) != 0)
1800 return got_error_from_errno();
1802 return NULL;
1805 static void
1806 init_curses(void)
1808 initscr();
1809 cbreak();
1810 halfdelay(1); /* Do fast refresh while initial view is loading. */
1811 noecho();
1812 nonl();
1813 intrflush(stdscr, FALSE);
1814 keypad(stdscr, TRUE);
1815 curs_set(0);
1816 signal(SIGWINCH, tog_sigwinch);
1819 static const struct got_error *
1820 cmd_log(int argc, char *argv[])
1822 const struct got_error *error;
1823 struct got_repository *repo = NULL;
1824 struct got_worktree *worktree = NULL;
1825 struct got_reflist_head refs;
1826 struct got_object_id *start_id = NULL;
1827 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1828 char *start_commit = NULL;
1829 int ch;
1830 struct tog_view *view;
1832 #ifndef PROFILE
1833 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1834 NULL) == -1)
1835 err(1, "pledge");
1836 #endif
1838 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1839 switch (ch) {
1840 case 'c':
1841 start_commit = optarg;
1842 break;
1843 case 'r':
1844 repo_path = realpath(optarg, NULL);
1845 if (repo_path == NULL)
1846 err(1, "-r option");
1847 break;
1848 default:
1849 usage_log();
1850 /* NOTREACHED */
1854 argc -= optind;
1855 argv += optind;
1857 if (argc == 0)
1858 path = strdup("");
1859 else if (argc == 1)
1860 path = strdup(argv[0]);
1861 else
1862 usage_log();
1863 if (path == NULL)
1864 return got_error_from_errno();
1866 cwd = getcwd(NULL, 0);
1867 if (cwd == NULL) {
1868 error = got_error_from_errno();
1869 goto done;
1871 if (repo_path == NULL) {
1872 error = got_worktree_open(&worktree, cwd);
1873 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1874 goto done;
1875 if (worktree) {
1876 repo_path =
1877 strdup(got_worktree_get_repo_path(worktree));
1878 } else
1879 repo_path = strdup(cwd);
1880 if (repo_path == NULL) {
1881 error = got_error_from_errno();
1882 goto done;
1886 init_curses();
1888 error = apply_unveil(repo_path,
1889 worktree ? got_worktree_get_root_path(worktree) : NULL);
1890 if (error)
1891 goto done;
1893 error = got_repo_open(&repo, repo_path);
1894 if (error != NULL)
1895 goto done;
1897 if (start_commit == NULL)
1898 error = get_head_commit_id(&start_id, repo);
1899 else
1900 error = got_object_resolve_id_str(&start_id, repo,
1901 start_commit);
1902 if (error != NULL)
1903 goto done;
1905 SIMPLEQ_INIT(&refs);
1906 error = got_ref_list(&refs, repo);
1907 if (error)
1908 goto done;
1910 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1911 if (view == NULL) {
1912 error = got_error_from_errno();
1913 goto done;
1915 error = open_log_view(view, start_id, &refs, repo, path, 1);
1916 if (error)
1917 goto done;
1918 error = view_loop(view);
1919 done:
1920 free(repo_path);
1921 free(cwd);
1922 free(path);
1923 free(start_id);
1924 if (repo)
1925 got_repo_close(repo);
1926 if (worktree)
1927 got_worktree_close(worktree);
1928 got_ref_list_free(&refs);
1929 return error;
1932 __dead static void
1933 usage_diff(void)
1935 endwin();
1936 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1937 getprogname());
1938 exit(1);
1941 static char *
1942 parse_next_line(FILE *f, size_t *len)
1944 char *line;
1945 size_t linelen;
1946 size_t lineno;
1947 const char delim[3] = { '\0', '\0', '\0'};
1949 line = fparseln(f, &linelen, &lineno, delim, 0);
1950 if (len)
1951 *len = linelen;
1952 return line;
1955 static const struct got_error *
1956 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1957 int *last_displayed_line, int *eof, int max_lines,
1958 char * header)
1960 const struct got_error *err;
1961 int nlines = 0, nprinted = 0;
1962 char *line;
1963 size_t len;
1964 wchar_t *wline;
1965 int width;
1967 rewind(f);
1968 werase(view->window);
1970 if (header) {
1971 err = format_line(&wline, &width, header, view->ncols);
1972 if (err) {
1973 return err;
1976 if (view_needs_focus_indication(view))
1977 wstandout(view->window);
1978 waddwstr(view->window, wline);
1979 if (view_needs_focus_indication(view))
1980 wstandend(view->window);
1981 if (width < view->ncols)
1982 waddch(view->window, '\n');
1984 if (max_lines <= 1)
1985 return NULL;
1986 max_lines--;
1989 *eof = 0;
1990 while (nprinted < max_lines) {
1991 line = parse_next_line(f, &len);
1992 if (line == NULL) {
1993 *eof = 1;
1994 break;
1996 if (++nlines < *first_displayed_line) {
1997 free(line);
1998 continue;
2001 err = format_line(&wline, &width, line, view->ncols);
2002 if (err) {
2003 free(line);
2004 return err;
2006 waddwstr(view->window, wline);
2007 if (width < view->ncols)
2008 waddch(view->window, '\n');
2009 if (++nprinted == 1)
2010 *first_displayed_line = nlines;
2011 free(line);
2012 free(wline);
2013 wline = NULL;
2015 *last_displayed_line = nlines;
2017 view_vborder(view);
2019 return NULL;
2022 static char *
2023 get_datestr(time_t *time, char *datebuf)
2025 char *p, *s = ctime_r(time, datebuf);
2026 p = strchr(s, '\n');
2027 if (p)
2028 *p = '\0';
2029 return s;
2032 static const struct got_error *
2033 write_commit_info(struct got_object_id *commit_id,
2034 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2036 const struct got_error *err = NULL;
2037 char datebuf[26];
2038 struct got_commit_object *commit;
2039 char *id_str = NULL;
2040 time_t committer_time;
2041 const char *author, *committer;
2042 char *refs_str = NULL;
2044 if (refs) {
2045 err = build_refs_str(&refs_str, refs, commit_id);
2046 if (err)
2047 return err;
2050 err = got_object_open_as_commit(&commit, repo, commit_id);
2051 if (err)
2052 return err;
2054 err = got_object_id_str(&id_str, commit_id);
2055 if (err) {
2056 err = got_error_from_errno();
2057 goto done;
2060 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2061 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2062 err = got_error_from_errno();
2063 goto done;
2065 if (fprintf(outfile, "from: %s\n",
2066 got_object_commit_get_author(commit)) < 0) {
2067 err = got_error_from_errno();
2068 goto done;
2070 committer_time = got_object_commit_get_committer_time(commit);
2071 if (fprintf(outfile, "date: %s UTC\n",
2072 get_datestr(&committer_time, datebuf)) < 0) {
2073 err = got_error_from_errno();
2074 goto done;
2076 author = got_object_commit_get_author(commit);
2077 committer = got_object_commit_get_committer(commit);
2078 if (strcmp(author, committer) != 0 &&
2079 fprintf(outfile, "via: %s\n", committer) < 0) {
2080 err = got_error_from_errno();
2081 goto done;
2083 if (fprintf(outfile, "%s\n",
2084 got_object_commit_get_logmsg(commit)) < 0) {
2085 err = got_error_from_errno();
2086 goto done;
2088 done:
2089 free(id_str);
2090 free(refs_str);
2091 got_object_commit_close(commit);
2092 return err;
2095 static const struct got_error *
2096 create_diff(struct tog_diff_view_state *s)
2098 const struct got_error *err = NULL;
2099 FILE *f = NULL;
2100 int obj_type;
2102 f = got_opentemp();
2103 if (f == NULL) {
2104 err = got_error_from_errno();
2105 goto done;
2107 if (s->f && fclose(s->f) != 0) {
2108 err = got_error_from_errno();
2109 goto done;
2111 s->f = f;
2113 if (s->id1)
2114 err = got_object_get_type(&obj_type, s->repo, s->id1);
2115 else
2116 err = got_object_get_type(&obj_type, s->repo, s->id2);
2117 if (err)
2118 goto done;
2120 switch (obj_type) {
2121 case GOT_OBJ_TYPE_BLOB:
2122 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2123 s->diff_context, s->repo, f);
2124 break;
2125 case GOT_OBJ_TYPE_TREE:
2126 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2127 s->diff_context, s->repo, f);
2128 break;
2129 case GOT_OBJ_TYPE_COMMIT: {
2130 const struct got_object_id_queue *parent_ids;
2131 struct got_object_qid *pid;
2132 struct got_commit_object *commit2;
2134 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2135 if (err)
2136 break;
2137 /* Show commit info if we're diffing to a parent/root commit. */
2138 if (s->id1 == NULL)
2139 write_commit_info(s->id2, s->refs, s->repo, f);
2140 else {
2141 parent_ids = got_object_commit_get_parent_ids(commit2);
2142 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2143 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2144 write_commit_info(s->id2, s->refs,
2145 s->repo, f);
2146 break;
2150 got_object_commit_close(commit2);
2152 err = got_diff_objects_as_commits(s->id1, s->id2,
2153 s->diff_context, s->repo, f);
2154 break;
2156 default:
2157 err = got_error(GOT_ERR_OBJ_TYPE);
2158 break;
2160 done:
2161 if (f && fflush(f) != 0 && err == NULL)
2162 err = got_error_from_errno();
2163 return err;
2166 static void
2167 diff_view_indicate_progress(struct tog_view *view)
2169 werase(view->window);
2170 waddstr(view->window, "diffing...");
2171 update_panels();
2172 doupdate();
2175 static const struct got_error *
2176 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2177 struct got_object_id *id2, struct tog_view *log_view,
2178 struct got_reflist_head *refs, struct got_repository *repo)
2180 const struct got_error *err;
2182 if (id1 != NULL && id2 != NULL) {
2183 int type1, type2;
2184 err = got_object_get_type(&type1, repo, id1);
2185 if (err)
2186 return err;
2187 err = got_object_get_type(&type2, repo, id2);
2188 if (err)
2189 return err;
2191 if (type1 != type2)
2192 return got_error(GOT_ERR_OBJ_TYPE);
2195 if (id1) {
2196 view->state.diff.id1 = got_object_id_dup(id1);
2197 if (view->state.diff.id1 == NULL)
2198 return got_error_from_errno();
2199 } else
2200 view->state.diff.id1 = NULL;
2202 view->state.diff.id2 = got_object_id_dup(id2);
2203 if (view->state.diff.id2 == NULL) {
2204 free(view->state.diff.id1);
2205 view->state.diff.id1 = NULL;
2206 return got_error_from_errno();
2208 view->state.diff.f = NULL;
2209 view->state.diff.first_displayed_line = 1;
2210 view->state.diff.last_displayed_line = view->nlines;
2211 view->state.diff.diff_context = 3;
2212 view->state.diff.log_view = log_view;
2213 view->state.diff.repo = repo;
2214 view->state.diff.refs = refs;
2216 if (log_view && view_is_splitscreen(view))
2217 show_log_view(log_view); /* draw vborder */
2218 diff_view_indicate_progress(view);
2220 err = create_diff(&view->state.diff);
2221 if (err) {
2222 free(view->state.diff.id1);
2223 view->state.diff.id1 = NULL;
2224 free(view->state.diff.id2);
2225 view->state.diff.id2 = NULL;
2226 return err;
2229 view->show = show_diff_view;
2230 view->input = input_diff_view;
2231 view->close = close_diff_view;
2233 return NULL;
2236 static const struct got_error *
2237 close_diff_view(struct tog_view *view)
2239 const struct got_error *err = NULL;
2241 free(view->state.diff.id1);
2242 view->state.diff.id1 = NULL;
2243 free(view->state.diff.id2);
2244 view->state.diff.id2 = NULL;
2245 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2246 err = got_error_from_errno();
2247 return err;
2250 static const struct got_error *
2251 show_diff_view(struct tog_view *view)
2253 const struct got_error *err;
2254 struct tog_diff_view_state *s = &view->state.diff;
2255 char *id_str1 = NULL, *id_str2, *header;
2257 if (s->id1) {
2258 err = got_object_id_str(&id_str1, s->id1);
2259 if (err)
2260 return err;
2262 err = got_object_id_str(&id_str2, s->id2);
2263 if (err)
2264 return err;
2266 if (asprintf(&header, "diff %s %s",
2267 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2268 err = got_error_from_errno();
2269 free(id_str1);
2270 free(id_str2);
2271 return err;
2273 free(id_str1);
2274 free(id_str2);
2276 return draw_file(view, s->f, &s->first_displayed_line,
2277 &s->last_displayed_line, &s->eof, view->nlines,
2278 header);
2281 static const struct got_error *
2282 set_selected_commit(struct tog_diff_view_state *s,
2283 struct commit_queue_entry *entry)
2285 const struct got_error *err;
2286 const struct got_object_id_queue *parent_ids;
2287 struct got_commit_object *selected_commit;
2288 struct got_object_qid *pid;
2290 free(s->id2);
2291 s->id2 = got_object_id_dup(entry->id);
2292 if (s->id2 == NULL)
2293 return got_error_from_errno();
2295 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2296 if (err)
2297 return err;
2298 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2299 free(s->id1);
2300 pid = SIMPLEQ_FIRST(parent_ids);
2301 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2302 got_object_commit_close(selected_commit);
2303 return NULL;
2306 static const struct got_error *
2307 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2308 struct tog_view **focus_view, struct tog_view *view, int ch)
2310 const struct got_error *err = NULL;
2311 struct tog_diff_view_state *s = &view->state.diff;
2312 struct tog_log_view_state *ls;
2313 struct commit_queue_entry *entry;
2314 int i;
2316 switch (ch) {
2317 case 'k':
2318 case KEY_UP:
2319 if (s->first_displayed_line > 1)
2320 s->first_displayed_line--;
2321 else
2322 view_flash(view);
2323 break;
2324 case KEY_PPAGE:
2325 if (s->first_displayed_line == 1) {
2326 view_flash(view);
2327 break;
2329 i = 0;
2330 while (i++ < view->nlines - 1 &&
2331 s->first_displayed_line > 1)
2332 s->first_displayed_line--;
2333 break;
2334 case 'j':
2335 case KEY_DOWN:
2336 if (!s->eof)
2337 s->first_displayed_line++;
2338 else
2339 view_flash(view);
2340 break;
2341 case KEY_NPAGE:
2342 case ' ':
2343 if (s->eof) {
2344 view_flash(view);
2345 break;
2347 i = 0;
2348 while (!s->eof && i++ < view->nlines - 1) {
2349 char *line;
2350 line = parse_next_line(s->f, NULL);
2351 s->first_displayed_line++;
2352 if (line == NULL)
2353 break;
2355 break;
2356 case '[':
2357 if (s->diff_context > 0) {
2358 s->diff_context--;
2359 diff_view_indicate_progress(view);
2360 err = create_diff(s);
2362 break;
2363 case ']':
2364 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2365 s->diff_context++;
2366 diff_view_indicate_progress(view);
2367 err = create_diff(s);
2369 break;
2370 case '<':
2371 case ',':
2372 if (s->log_view == NULL)
2373 break;
2374 ls = &s->log_view->state.log;
2375 entry = TAILQ_PREV(ls->selected_entry,
2376 commit_queue_head, entry);
2377 if (entry == NULL)
2378 break;
2380 err = input_log_view(NULL, NULL, NULL, s->log_view,
2381 KEY_UP);
2382 if (err)
2383 break;
2385 err = set_selected_commit(s, entry);
2386 if (err)
2387 break;
2389 s->first_displayed_line = 1;
2390 s->last_displayed_line = view->nlines;
2392 diff_view_indicate_progress(view);
2393 err = create_diff(s);
2394 break;
2395 case '>':
2396 case '.':
2397 if (s->log_view == NULL)
2398 break;
2399 ls = &s->log_view->state.log;
2401 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2402 ls->thread_args.commits_needed++;
2404 /* Display "loading..." in log view. */
2405 show_log_view(s->log_view);
2406 update_panels();
2407 doupdate();
2409 err = trigger_log_thread(1 /* load_all */,
2410 &ls->thread_args.commits_needed,
2411 &ls->thread_args.log_complete,
2412 &ls->thread_args.need_commits);
2413 if (err)
2414 break;
2416 err = input_log_view(NULL, NULL, NULL, s->log_view,
2417 KEY_DOWN);
2418 if (err)
2419 break;
2421 entry = TAILQ_NEXT(ls->selected_entry, entry);
2422 if (entry == NULL)
2423 break;
2425 err = set_selected_commit(s, entry);
2426 if (err)
2427 break;
2429 s->first_displayed_line = 1;
2430 s->last_displayed_line = view->nlines;
2432 diff_view_indicate_progress(view);
2433 err = create_diff(s);
2434 break;
2435 default:
2436 break;
2439 return err;
2442 static const struct got_error *
2443 cmd_diff(int argc, char *argv[])
2445 const struct got_error *error = NULL;
2446 struct got_repository *repo = NULL;
2447 struct got_reflist_head refs;
2448 struct got_object_id *id1 = NULL, *id2 = NULL;
2449 char *repo_path = NULL;
2450 char *id_str1 = NULL, *id_str2 = NULL;
2451 int ch;
2452 struct tog_view *view;
2454 #ifndef PROFILE
2455 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2456 NULL) == -1)
2457 err(1, "pledge");
2458 #endif
2460 while ((ch = getopt(argc, argv, "")) != -1) {
2461 switch (ch) {
2462 default:
2463 usage_diff();
2464 /* NOTREACHED */
2468 argc -= optind;
2469 argv += optind;
2471 if (argc == 0) {
2472 usage_diff(); /* TODO show local worktree changes */
2473 } else if (argc == 2) {
2474 repo_path = getcwd(NULL, 0);
2475 if (repo_path == NULL)
2476 return got_error_from_errno();
2477 id_str1 = argv[0];
2478 id_str2 = argv[1];
2479 } else if (argc == 3) {
2480 repo_path = realpath(argv[0], NULL);
2481 if (repo_path == NULL)
2482 return got_error_from_errno();
2483 id_str1 = argv[1];
2484 id_str2 = argv[2];
2485 } else
2486 usage_diff();
2488 init_curses();
2490 error = apply_unveil(repo_path, NULL);
2491 if (error)
2492 goto done;
2494 error = got_repo_open(&repo, repo_path);
2495 free(repo_path);
2496 if (error)
2497 goto done;
2499 error = got_object_resolve_id_str(&id1, repo, id_str1);
2500 if (error)
2501 goto done;
2503 error = got_object_resolve_id_str(&id2, repo, id_str2);
2504 if (error)
2505 goto done;
2507 SIMPLEQ_INIT(&refs);
2508 error = got_ref_list(&refs, repo);
2509 if (error)
2510 goto done;
2512 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2513 if (view == NULL) {
2514 error = got_error_from_errno();
2515 goto done;
2517 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2518 if (error)
2519 goto done;
2520 error = view_loop(view);
2521 done:
2522 got_repo_close(repo);
2523 got_ref_list_free(&refs);
2524 return error;
2527 __dead static void
2528 usage_blame(void)
2530 endwin();
2531 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2532 getprogname());
2533 exit(1);
2536 struct tog_blame_line {
2537 int annotated;
2538 struct got_object_id *id;
2541 static const struct got_error *
2542 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2543 const char *path, struct tog_blame_line *lines, int nlines,
2544 int blame_complete, int selected_line, int *first_displayed_line,
2545 int *last_displayed_line, int *eof, int max_lines)
2547 const struct got_error *err;
2548 int lineno = 0, nprinted = 0;
2549 char *line;
2550 size_t len;
2551 wchar_t *wline;
2552 int width, wlimit;
2553 struct tog_blame_line *blame_line;
2554 struct got_object_id *prev_id = NULL;
2555 char *id_str;
2557 err = got_object_id_str(&id_str, id);
2558 if (err)
2559 return err;
2561 rewind(f);
2562 werase(view->window);
2564 if (asprintf(&line, "commit %s", id_str) == -1) {
2565 err = got_error_from_errno();
2566 free(id_str);
2567 return err;
2570 err = format_line(&wline, &width, line, view->ncols);
2571 free(line);
2572 line = NULL;
2573 if (view_needs_focus_indication(view))
2574 wstandout(view->window);
2575 waddwstr(view->window, wline);
2576 if (view_needs_focus_indication(view))
2577 wstandend(view->window);
2578 free(wline);
2579 wline = NULL;
2580 if (width < view->ncols)
2581 waddch(view->window, '\n');
2583 if (asprintf(&line, "[%d/%d] %s%s",
2584 *first_displayed_line - 1 + selected_line, nlines,
2585 blame_complete ? "" : "annotating... ", path) == -1) {
2586 free(id_str);
2587 return got_error_from_errno();
2589 free(id_str);
2590 err = format_line(&wline, &width, line, view->ncols);
2591 free(line);
2592 line = NULL;
2593 if (err)
2594 return err;
2595 waddwstr(view->window, wline);
2596 free(wline);
2597 wline = NULL;
2598 if (width < view->ncols)
2599 waddch(view->window, '\n');
2601 *eof = 0;
2602 while (nprinted < max_lines - 2) {
2603 line = parse_next_line(f, &len);
2604 if (line == NULL) {
2605 *eof = 1;
2606 break;
2608 if (++lineno < *first_displayed_line) {
2609 free(line);
2610 continue;
2613 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2614 err = format_line(&wline, &width, line, wlimit);
2615 if (err) {
2616 free(line);
2617 return err;
2620 if (view->focussed && nprinted == selected_line - 1)
2621 wstandout(view->window);
2623 blame_line = &lines[lineno - 1];
2624 if (blame_line->annotated && prev_id &&
2625 got_object_id_cmp(prev_id, blame_line->id) == 0)
2626 waddstr(view->window, " ");
2627 else if (blame_line->annotated) {
2628 char *id_str;
2629 err = got_object_id_str(&id_str, blame_line->id);
2630 if (err) {
2631 free(line);
2632 free(wline);
2633 return err;
2635 wprintw(view->window, "%.8s ", id_str);
2636 free(id_str);
2637 prev_id = blame_line->id;
2638 } else {
2639 waddstr(view->window, "........ ");
2640 prev_id = NULL;
2643 waddwstr(view->window, wline);
2644 while (width < wlimit) {
2645 waddch(view->window, ' ');
2646 width++;
2648 if (view->focussed && nprinted == selected_line - 1)
2649 wstandend(view->window);
2650 if (++nprinted == 1)
2651 *first_displayed_line = lineno;
2652 free(line);
2653 free(wline);
2654 wline = NULL;
2656 *last_displayed_line = lineno;
2658 view_vborder(view);
2660 return NULL;
2663 static const struct got_error *
2664 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2666 const struct got_error *err = NULL;
2667 struct tog_blame_cb_args *a = arg;
2668 struct tog_blame_line *line;
2669 int errcode;
2671 if (nlines != a->nlines ||
2672 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2673 return got_error(GOT_ERR_RANGE);
2675 errcode = pthread_mutex_lock(&tog_mutex);
2676 if (errcode)
2677 return got_error_set_errno(errcode);
2679 if (*a->quit) { /* user has quit the blame view */
2680 err = got_error(GOT_ERR_ITER_COMPLETED);
2681 goto done;
2684 if (lineno == -1)
2685 goto done; /* no change in this commit */
2687 line = &a->lines[lineno - 1];
2688 if (line->annotated)
2689 goto done;
2691 line->id = got_object_id_dup(id);
2692 if (line->id == NULL) {
2693 err = got_error_from_errno();
2694 goto done;
2696 line->annotated = 1;
2697 done:
2698 errcode = pthread_mutex_unlock(&tog_mutex);
2699 if (errcode)
2700 err = got_error_set_errno(errcode);
2701 return err;
2704 static void *
2705 blame_thread(void *arg)
2707 const struct got_error *err;
2708 struct tog_blame_thread_args *ta = arg;
2709 struct tog_blame_cb_args *a = ta->cb_args;
2710 int errcode;
2712 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2713 blame_cb, ta->cb_args);
2715 errcode = pthread_mutex_lock(&tog_mutex);
2716 if (errcode)
2717 return (void *)got_error_set_errno(errcode);
2719 got_repo_close(ta->repo);
2720 ta->repo = NULL;
2721 *ta->complete = 1;
2723 errcode = pthread_mutex_unlock(&tog_mutex);
2724 if (errcode && err == NULL)
2725 err = got_error_set_errno(errcode);
2727 return (void *)err;
2730 static struct got_object_id *
2731 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2732 int selected_line)
2734 struct tog_blame_line *line;
2736 line = &lines[first_displayed_line - 1 + selected_line - 1];
2737 if (!line->annotated)
2738 return NULL;
2740 return line->id;
2743 static const struct got_error *
2744 stop_blame(struct tog_blame *blame)
2746 const struct got_error *err = NULL;
2747 int i;
2749 if (blame->thread) {
2750 int errcode;
2751 errcode = pthread_mutex_unlock(&tog_mutex);
2752 if (errcode)
2753 return got_error_set_errno(errcode);
2754 errcode = pthread_join(blame->thread, (void **)&err);
2755 if (errcode)
2756 return got_error_set_errno(errcode);
2757 errcode = pthread_mutex_lock(&tog_mutex);
2758 if (errcode)
2759 return got_error_set_errno(errcode);
2760 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2761 err = NULL;
2762 blame->thread = NULL;
2764 if (blame->thread_args.repo) {
2765 got_repo_close(blame->thread_args.repo);
2766 blame->thread_args.repo = NULL;
2768 if (blame->f) {
2769 if (fclose(blame->f) != 0 && err == NULL)
2770 err = got_error_from_errno();
2771 blame->f = NULL;
2773 if (blame->lines) {
2774 for (i = 0; i < blame->nlines; i++)
2775 free(blame->lines[i].id);
2776 free(blame->lines);
2777 blame->lines = NULL;
2779 free(blame->cb_args.commit_id);
2780 blame->cb_args.commit_id = NULL;
2782 return err;
2785 static const struct got_error *
2786 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2787 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2788 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2789 struct got_repository *repo)
2791 const struct got_error *err = NULL;
2792 struct got_blob_object *blob = NULL;
2793 struct got_repository *thread_repo = NULL;
2794 struct got_object_id *obj_id = NULL;
2795 int obj_type;
2797 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2798 if (err)
2799 return err;
2800 if (obj_id == NULL)
2801 return got_error(GOT_ERR_NO_OBJ);
2803 err = got_object_get_type(&obj_type, repo, obj_id);
2804 if (err)
2805 goto done;
2807 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2808 err = got_error(GOT_ERR_OBJ_TYPE);
2809 goto done;
2812 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2813 if (err)
2814 goto done;
2815 blame->f = got_opentemp();
2816 if (blame->f == NULL) {
2817 err = got_error_from_errno();
2818 goto done;
2820 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2821 blame->f, blob);
2822 if (err)
2823 goto done;
2825 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2826 if (blame->lines == NULL) {
2827 err = got_error_from_errno();
2828 goto done;
2831 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2832 if (err)
2833 goto done;
2835 blame->cb_args.view = view;
2836 blame->cb_args.lines = blame->lines;
2837 blame->cb_args.nlines = blame->nlines;
2838 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2839 if (blame->cb_args.commit_id == NULL) {
2840 err = got_error_from_errno();
2841 goto done;
2843 blame->cb_args.quit = done;
2845 blame->thread_args.path = path;
2846 blame->thread_args.repo = thread_repo;
2847 blame->thread_args.cb_args = &blame->cb_args;
2848 blame->thread_args.complete = blame_complete;
2849 *blame_complete = 0;
2851 done:
2852 if (blob)
2853 got_object_blob_close(blob);
2854 free(obj_id);
2855 if (err)
2856 stop_blame(blame);
2857 return err;
2860 static const struct got_error *
2861 open_blame_view(struct tog_view *view, char *path,
2862 struct got_object_id *commit_id, struct got_reflist_head *refs,
2863 struct got_repository *repo)
2865 const struct got_error *err = NULL;
2866 struct tog_blame_view_state *s = &view->state.blame;
2868 SIMPLEQ_INIT(&s->blamed_commits);
2870 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2871 if (err)
2872 return err;
2874 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2875 s->first_displayed_line = 1;
2876 s->last_displayed_line = view->nlines;
2877 s->selected_line = 1;
2878 s->blame_complete = 0;
2879 s->path = path;
2880 if (s->path == NULL)
2881 return got_error_from_errno();
2882 s->repo = repo;
2883 s->refs = refs;
2884 s->commit_id = commit_id;
2885 memset(&s->blame, 0, sizeof(s->blame));
2887 view->show = show_blame_view;
2888 view->input = input_blame_view;
2889 view->close = close_blame_view;
2891 return run_blame(&s->blame, view, &s->blame_complete,
2892 &s->first_displayed_line, &s->last_displayed_line,
2893 &s->selected_line, &s->done, &s->eof, s->path,
2894 s->blamed_commit->id, s->repo);
2897 static const struct got_error *
2898 close_blame_view(struct tog_view *view)
2900 const struct got_error *err = NULL;
2901 struct tog_blame_view_state *s = &view->state.blame;
2903 if (s->blame.thread)
2904 err = stop_blame(&s->blame);
2906 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2907 struct got_object_qid *blamed_commit;
2908 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2909 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2910 got_object_qid_free(blamed_commit);
2913 free(s->path);
2915 return err;
2918 static const struct got_error *
2919 show_blame_view(struct tog_view *view)
2921 const struct got_error *err = NULL;
2922 struct tog_blame_view_state *s = &view->state.blame;
2923 int errcode;
2925 if (s->blame.thread == NULL) {
2926 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2927 &s->blame.thread_args);
2928 if (errcode)
2929 return got_error_set_errno(errcode);
2932 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2933 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2934 s->selected_line, &s->first_displayed_line,
2935 &s->last_displayed_line, &s->eof, view->nlines);
2937 view_vborder(view);
2938 return err;
2941 static const struct got_error *
2942 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2943 struct tog_view **focus_view, struct tog_view *view, int ch)
2945 const struct got_error *err = NULL, *thread_err = NULL;
2946 struct tog_view *diff_view;
2947 struct tog_blame_view_state *s = &view->state.blame;
2948 int begin_x = 0;
2950 switch (ch) {
2951 case 'q':
2952 s->done = 1;
2953 break;
2954 case 'k':
2955 case KEY_UP:
2956 if (s->selected_line > 1)
2957 s->selected_line--;
2958 else if (s->selected_line == 1 &&
2959 s->first_displayed_line > 1)
2960 s->first_displayed_line--;
2961 else
2962 view_flash(view);
2963 break;
2964 case KEY_PPAGE:
2965 if (s->first_displayed_line == 1) {
2966 if (s->selected_line == 1) {
2967 view_flash(view);
2968 break;
2970 s->selected_line = 1;
2971 break;
2973 if (s->first_displayed_line > view->nlines - 2)
2974 s->first_displayed_line -=
2975 (view->nlines - 2);
2976 else
2977 s->first_displayed_line = 1;
2978 break;
2979 case 'j':
2980 case KEY_DOWN:
2981 if (s->selected_line < view->nlines - 2 &&
2982 s->first_displayed_line +
2983 s->selected_line <= s->blame.nlines)
2984 s->selected_line++;
2985 else if (s->last_displayed_line <
2986 s->blame.nlines)
2987 s->first_displayed_line++;
2988 else
2989 view_flash(view);
2990 break;
2991 case 'b':
2992 case 'p': {
2993 struct got_object_id *id = NULL;
2994 id = get_selected_commit_id(s->blame.lines,
2995 s->first_displayed_line, s->selected_line);
2996 if (id == NULL)
2997 break;
2998 if (ch == 'p') {
2999 struct got_commit_object *commit;
3000 struct got_object_qid *pid;
3001 struct got_object_id *blob_id = NULL;
3002 int obj_type;
3003 err = got_object_open_as_commit(&commit,
3004 s->repo, id);
3005 if (err)
3006 break;
3007 pid = SIMPLEQ_FIRST(
3008 got_object_commit_get_parent_ids(commit));
3009 if (pid == NULL) {
3010 got_object_commit_close(commit);
3011 break;
3013 /* Check if path history ends here. */
3014 err = got_object_id_by_path(&blob_id, s->repo,
3015 pid->id, s->path);
3016 if (err) {
3017 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3018 err = NULL;
3019 got_object_commit_close(commit);
3020 break;
3022 err = got_object_get_type(&obj_type, s->repo,
3023 blob_id);
3024 free(blob_id);
3025 /* Can't blame non-blob type objects. */
3026 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3027 got_object_commit_close(commit);
3028 break;
3030 err = got_object_qid_alloc(&s->blamed_commit,
3031 pid->id);
3032 got_object_commit_close(commit);
3033 } else {
3034 if (got_object_id_cmp(id,
3035 s->blamed_commit->id) == 0)
3036 break;
3037 err = got_object_qid_alloc(&s->blamed_commit,
3038 id);
3040 if (err)
3041 break;
3042 s->done = 1;
3043 thread_err = stop_blame(&s->blame);
3044 s->done = 0;
3045 if (thread_err)
3046 break;
3047 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3048 s->blamed_commit, entry);
3049 err = run_blame(&s->blame, view, &s->blame_complete,
3050 &s->first_displayed_line, &s->last_displayed_line,
3051 &s->selected_line, &s->done, &s->eof,
3052 s->path, s->blamed_commit->id, s->repo);
3053 if (err)
3054 break;
3055 break;
3057 case 'B': {
3058 struct got_object_qid *first;
3059 first = SIMPLEQ_FIRST(&s->blamed_commits);
3060 if (!got_object_id_cmp(first->id, s->commit_id))
3061 break;
3062 s->done = 1;
3063 thread_err = stop_blame(&s->blame);
3064 s->done = 0;
3065 if (thread_err)
3066 break;
3067 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3068 got_object_qid_free(s->blamed_commit);
3069 s->blamed_commit =
3070 SIMPLEQ_FIRST(&s->blamed_commits);
3071 err = run_blame(&s->blame, view, &s->blame_complete,
3072 &s->first_displayed_line, &s->last_displayed_line,
3073 &s->selected_line, &s->done, &s->eof, s->path,
3074 s->blamed_commit->id, s->repo);
3075 if (err)
3076 break;
3077 break;
3079 case KEY_ENTER:
3080 case '\r': {
3081 struct got_object_id *id = NULL;
3082 struct got_object_qid *pid;
3083 struct got_commit_object *commit = NULL;
3084 id = get_selected_commit_id(s->blame.lines,
3085 s->first_displayed_line, s->selected_line);
3086 if (id == NULL)
3087 break;
3088 err = got_object_open_as_commit(&commit, s->repo, id);
3089 if (err)
3090 break;
3091 pid = SIMPLEQ_FIRST(
3092 got_object_commit_get_parent_ids(commit));
3093 if (view_is_parent_view(view))
3094 begin_x = view_split_begin_x(view->begin_x);
3095 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3096 if (diff_view == NULL) {
3097 got_object_commit_close(commit);
3098 err = got_error_from_errno();
3099 break;
3101 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3102 id, NULL, s->refs, s->repo);
3103 got_object_commit_close(commit);
3104 if (err) {
3105 view_close(diff_view);
3106 break;
3108 if (view_is_parent_view(view)) {
3109 err = view_close_child(view);
3110 if (err)
3111 break;
3112 err = view_set_child(view, diff_view);
3113 if (err) {
3114 view_close(diff_view);
3115 break;
3117 *focus_view = diff_view;
3118 view->child_focussed = 1;
3119 } else
3120 *new_view = diff_view;
3121 if (err)
3122 break;
3123 break;
3125 case KEY_NPAGE:
3126 case ' ':
3127 if (s->last_displayed_line >= s->blame.nlines &&
3128 s->selected_line >= MIN(s->blame.nlines,
3129 view->nlines - 2)) {
3130 view_flash(view);
3131 break;
3133 if (s->last_displayed_line >= s->blame.nlines &&
3134 s->selected_line < view->nlines - 2) {
3135 s->selected_line = MIN(s->blame.nlines,
3136 view->nlines - 2);
3137 break;
3139 if (s->last_displayed_line + view->nlines - 2
3140 <= s->blame.nlines)
3141 s->first_displayed_line +=
3142 view->nlines - 2;
3143 else
3144 s->first_displayed_line =
3145 s->blame.nlines -
3146 (view->nlines - 3);
3147 break;
3148 case KEY_RESIZE:
3149 if (s->selected_line > view->nlines - 2) {
3150 s->selected_line = MIN(s->blame.nlines,
3151 view->nlines - 2);
3153 break;
3154 default:
3155 break;
3157 return thread_err ? thread_err : err;
3160 static const struct got_error *
3161 cmd_blame(int argc, char *argv[])
3163 const struct got_error *error;
3164 struct got_repository *repo = NULL;
3165 struct got_reflist_head refs;
3166 struct got_worktree *worktree = NULL;
3167 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3168 struct got_object_id *commit_id = NULL;
3169 char *commit_id_str = NULL;
3170 int ch;
3171 struct tog_view *view;
3173 #ifndef PROFILE
3174 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3175 NULL) == -1)
3176 err(1, "pledge");
3177 #endif
3179 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3180 switch (ch) {
3181 case 'c':
3182 commit_id_str = optarg;
3183 break;
3184 case 'r':
3185 repo_path = realpath(optarg, NULL);
3186 if (repo_path == NULL)
3187 err(1, "-r option");
3188 break;
3189 default:
3190 usage_blame();
3191 /* NOTREACHED */
3195 argc -= optind;
3196 argv += optind;
3198 if (argc == 1)
3199 path = argv[0];
3200 else
3201 usage_blame();
3203 cwd = getcwd(NULL, 0);
3204 if (cwd == NULL) {
3205 error = got_error_from_errno();
3206 goto done;
3208 if (repo_path == NULL) {
3209 error = got_worktree_open(&worktree, cwd);
3210 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3211 goto done;
3212 else
3213 error = NULL;
3214 if (worktree) {
3215 repo_path =
3216 strdup(got_worktree_get_repo_path(worktree));
3217 if (repo_path == NULL)
3218 error = got_error_from_errno();
3219 if (error)
3220 goto done;
3221 } else {
3222 repo_path = strdup(cwd);
3223 if (repo_path == NULL) {
3224 error = got_error_from_errno();
3225 goto done;
3230 init_curses();
3232 error = apply_unveil(repo_path, NULL);
3233 if (error)
3234 goto done;
3236 error = got_repo_open(&repo, repo_path);
3237 if (error != NULL)
3238 goto done;
3240 if (worktree) {
3241 const char *prefix = got_worktree_get_path_prefix(worktree);
3242 char *p, *worktree_subdir = cwd +
3243 strlen(got_worktree_get_root_path(worktree));
3244 if (asprintf(&p, "%s%s%s%s%s",
3245 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3246 worktree_subdir, worktree_subdir[0] ? "/" : "",
3247 path) == -1) {
3248 error = got_error_from_errno();
3249 goto done;
3251 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3252 free(p);
3253 } else {
3254 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3256 if (error)
3257 goto done;
3259 if (commit_id_str == NULL) {
3260 struct got_reference *head_ref;
3261 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3262 if (error != NULL)
3263 goto done;
3264 error = got_ref_resolve(&commit_id, repo, head_ref);
3265 got_ref_close(head_ref);
3266 } else {
3267 error = got_object_resolve_id_str(&commit_id, repo,
3268 commit_id_str);
3270 if (error != NULL)
3271 goto done;
3273 SIMPLEQ_INIT(&refs);
3274 error = got_ref_list(&refs, repo);
3275 if (error)
3276 goto done;
3278 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3279 if (view == NULL) {
3280 error = got_error_from_errno();
3281 goto done;
3283 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3284 if (error)
3285 goto done;
3286 error = view_loop(view);
3287 done:
3288 free(repo_path);
3289 free(cwd);
3290 free(commit_id);
3291 if (worktree)
3292 got_worktree_close(worktree);
3293 if (repo)
3294 got_repo_close(repo);
3295 got_ref_list_free(&refs);
3296 return error;
3299 static const struct got_error *
3300 draw_tree_entries(struct tog_view *view,
3301 struct got_tree_entry **first_displayed_entry,
3302 struct got_tree_entry **last_displayed_entry,
3303 struct got_tree_entry **selected_entry, int *ndisplayed,
3304 const char *label, int show_ids, const char *parent_path,
3305 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3307 const struct got_error *err = NULL;
3308 struct got_tree_entry *te;
3309 wchar_t *wline;
3310 int width, n;
3312 *ndisplayed = 0;
3314 werase(view->window);
3316 if (limit == 0)
3317 return NULL;
3319 err = format_line(&wline, &width, label, view->ncols);
3320 if (err)
3321 return err;
3322 if (view_needs_focus_indication(view))
3323 wstandout(view->window);
3324 waddwstr(view->window, wline);
3325 if (view_needs_focus_indication(view))
3326 wstandend(view->window);
3327 free(wline);
3328 wline = NULL;
3329 if (width < view->ncols)
3330 waddch(view->window, '\n');
3331 if (--limit <= 0)
3332 return NULL;
3333 err = format_line(&wline, &width, parent_path, view->ncols);
3334 if (err)
3335 return err;
3336 waddwstr(view->window, wline);
3337 free(wline);
3338 wline = NULL;
3339 if (width < view->ncols)
3340 waddch(view->window, '\n');
3341 if (--limit <= 0)
3342 return NULL;
3343 waddch(view->window, '\n');
3344 if (--limit <= 0)
3345 return NULL;
3347 te = SIMPLEQ_FIRST(&entries->head);
3348 if (*first_displayed_entry == NULL) {
3349 if (selected == 0) {
3350 if (view->focussed)
3351 wstandout(view->window);
3352 *selected_entry = NULL;
3354 waddstr(view->window, " ..\n"); /* parent directory */
3355 if (selected == 0 && view->focussed)
3356 wstandend(view->window);
3357 (*ndisplayed)++;
3358 if (--limit <= 0)
3359 return NULL;
3360 n = 1;
3361 } else {
3362 n = 0;
3363 while (te != *first_displayed_entry)
3364 te = SIMPLEQ_NEXT(te, entry);
3367 while (te) {
3368 char *line = NULL, *id_str = NULL;
3370 if (show_ids) {
3371 err = got_object_id_str(&id_str, te->id);
3372 if (err)
3373 return got_error_from_errno();
3375 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3376 te->name, S_ISDIR(te->mode) ? "/" :
3377 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3378 free(id_str);
3379 return got_error_from_errno();
3381 free(id_str);
3382 err = format_line(&wline, &width, line, view->ncols);
3383 if (err) {
3384 free(line);
3385 break;
3387 if (n == selected) {
3388 if (view->focussed)
3389 wstandout(view->window);
3390 *selected_entry = te;
3392 waddwstr(view->window, wline);
3393 if (width < view->ncols)
3394 waddch(view->window, '\n');
3395 if (n == selected && view->focussed)
3396 wstandend(view->window);
3397 free(line);
3398 free(wline);
3399 wline = NULL;
3400 n++;
3401 (*ndisplayed)++;
3402 *last_displayed_entry = te;
3403 if (--limit <= 0)
3404 break;
3405 te = SIMPLEQ_NEXT(te, entry);
3408 return err;
3411 static void
3412 tree_scroll_up(struct tog_view *view,
3413 struct got_tree_entry **first_displayed_entry, int maxscroll,
3414 const struct got_tree_entries *entries, int isroot)
3416 struct got_tree_entry *te, *prev;
3417 int i;
3419 if (*first_displayed_entry == NULL) {
3420 view_flash(view);
3421 return;
3424 te = SIMPLEQ_FIRST(&entries->head);
3425 if (*first_displayed_entry == te) {
3426 view_flash(view);
3427 if (!isroot)
3428 *first_displayed_entry = NULL;
3429 return;
3432 /* XXX this is stupid... switch to TAILQ? */
3433 for (i = 0; i < maxscroll; i++) {
3434 while (te != *first_displayed_entry) {
3435 prev = te;
3436 te = SIMPLEQ_NEXT(te, entry);
3438 *first_displayed_entry = prev;
3439 te = SIMPLEQ_FIRST(&entries->head);
3441 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3442 *first_displayed_entry = NULL;
3445 static int
3446 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3447 struct got_tree_entry *last_displayed_entry,
3448 const struct got_tree_entries *entries)
3450 struct got_tree_entry *next, *last;
3451 int n = 0;
3453 if (*first_displayed_entry)
3454 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3455 else
3456 next = SIMPLEQ_FIRST(&entries->head);
3457 last = last_displayed_entry;
3458 while (next && last && n++ < maxscroll) {
3459 last = SIMPLEQ_NEXT(last, entry);
3460 if (last) {
3461 *first_displayed_entry = next;
3462 next = SIMPLEQ_NEXT(next, entry);
3465 return n;
3468 static const struct got_error *
3469 tree_entry_path(char **path, struct tog_parent_trees *parents,
3470 struct got_tree_entry *te)
3472 const struct got_error *err = NULL;
3473 struct tog_parent_tree *pt;
3474 size_t len = 2; /* for leading slash and NUL */
3476 TAILQ_FOREACH(pt, parents, entry)
3477 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3478 if (te)
3479 len += strlen(te->name);
3481 *path = calloc(1, len);
3482 if (path == NULL)
3483 return got_error_from_errno();
3485 (*path)[0] = '/';
3486 pt = TAILQ_LAST(parents, tog_parent_trees);
3487 while (pt) {
3488 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3489 err = got_error(GOT_ERR_NO_SPACE);
3490 goto done;
3492 if (strlcat(*path, "/", len) >= len) {
3493 err = got_error(GOT_ERR_NO_SPACE);
3494 goto done;
3496 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3498 if (te) {
3499 if (strlcat(*path, te->name, len) >= len) {
3500 err = got_error(GOT_ERR_NO_SPACE);
3501 goto done;
3504 done:
3505 if (err) {
3506 free(*path);
3507 *path = NULL;
3509 return err;
3512 static const struct got_error *
3513 blame_tree_entry(struct tog_view **new_view, int begin_x,
3514 struct got_tree_entry *te, struct tog_parent_trees *parents,
3515 struct got_object_id *commit_id, struct got_reflist_head *refs,
3516 struct got_repository *repo)
3518 const struct got_error *err = NULL;
3519 char *path;
3520 struct tog_view *blame_view;
3522 err = tree_entry_path(&path, parents, te);
3523 if (err)
3524 return err;
3526 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3527 if (blame_view == NULL)
3528 return got_error_from_errno();
3530 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3531 if (err) {
3532 view_close(blame_view);
3533 free(path);
3534 } else
3535 *new_view = blame_view;
3536 return err;
3539 static const struct got_error *
3540 log_tree_entry(struct tog_view **new_view, int begin_x,
3541 struct got_tree_entry *te, struct tog_parent_trees *parents,
3542 struct got_object_id *commit_id, struct got_reflist_head *refs,
3543 struct got_repository *repo)
3545 struct tog_view *log_view;
3546 const struct got_error *err = NULL;
3547 char *path;
3549 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3550 if (log_view == NULL)
3551 return got_error_from_errno();
3553 err = tree_entry_path(&path, parents, te);
3554 if (err)
3555 return err;
3557 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3558 if (err)
3559 view_close(log_view);
3560 else
3561 *new_view = log_view;
3562 free(path);
3563 return err;
3566 static const struct got_error *
3567 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3568 struct got_object_id *commit_id, struct got_reflist_head *refs,
3569 struct got_repository *repo)
3571 const struct got_error *err = NULL;
3572 char *commit_id_str = NULL;
3573 struct tog_tree_view_state *s = &view->state.tree;
3575 TAILQ_INIT(&s->parents);
3577 err = got_object_id_str(&commit_id_str, commit_id);
3578 if (err != NULL)
3579 goto done;
3581 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3582 err = got_error_from_errno();
3583 goto done;
3586 s->root = s->tree = root;
3587 s->entries = got_object_tree_get_entries(root);
3588 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3589 s->commit_id = got_object_id_dup(commit_id);
3590 if (s->commit_id == NULL) {
3591 err = got_error_from_errno();
3592 goto done;
3594 s->refs = refs;
3595 s->repo = repo;
3597 view->show = show_tree_view;
3598 view->input = input_tree_view;
3599 view->close = close_tree_view;
3600 done:
3601 free(commit_id_str);
3602 if (err) {
3603 free(s->tree_label);
3604 s->tree_label = NULL;
3606 return err;
3609 static const struct got_error *
3610 close_tree_view(struct tog_view *view)
3612 struct tog_tree_view_state *s = &view->state.tree;
3614 free(s->tree_label);
3615 s->tree_label = NULL;
3616 free(s->commit_id);
3617 s->commit_id = NULL;
3618 while (!TAILQ_EMPTY(&s->parents)) {
3619 struct tog_parent_tree *parent;
3620 parent = TAILQ_FIRST(&s->parents);
3621 TAILQ_REMOVE(&s->parents, parent, entry);
3622 free(parent);
3625 if (s->tree != s->root)
3626 got_object_tree_close(s->tree);
3627 got_object_tree_close(s->root);
3629 return NULL;
3632 static const struct got_error *
3633 show_tree_view(struct tog_view *view)
3635 const struct got_error *err = NULL;
3636 struct tog_tree_view_state *s = &view->state.tree;
3637 char *parent_path;
3639 err = tree_entry_path(&parent_path, &s->parents, NULL);
3640 if (err)
3641 return err;
3643 err = draw_tree_entries(view, &s->first_displayed_entry,
3644 &s->last_displayed_entry, &s->selected_entry,
3645 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3646 s->entries, s->selected, view->nlines, s->tree == s->root);
3647 free(parent_path);
3649 view_vborder(view);
3650 return err;
3653 static const struct got_error *
3654 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3655 struct tog_view **focus_view, struct tog_view *view, int ch)
3657 const struct got_error *err = NULL;
3658 struct tog_tree_view_state *s = &view->state.tree;
3659 struct tog_view *log_view;
3660 int begin_x = 0, nscrolled;
3662 switch (ch) {
3663 case 'i':
3664 s->show_ids = !s->show_ids;
3665 break;
3666 case 'l':
3667 if (!s->selected_entry)
3668 break;
3669 if (view_is_parent_view(view))
3670 begin_x = view_split_begin_x(view->begin_x);
3671 err = log_tree_entry(&log_view, begin_x,
3672 s->selected_entry, &s->parents,
3673 s->commit_id, s->refs, s->repo);
3674 if (view_is_parent_view(view)) {
3675 err = view_close_child(view);
3676 if (err)
3677 return err;
3678 err = view_set_child(view, log_view);
3679 if (err) {
3680 view_close(log_view);
3681 break;
3683 *focus_view = log_view;
3684 view->child_focussed = 1;
3685 } else
3686 *new_view = log_view;
3687 break;
3688 case 'k':
3689 case KEY_UP:
3690 if (s->selected > 0) {
3691 s->selected--;
3692 if (s->selected == 0)
3693 break;
3695 if (s->selected > 0)
3696 break;
3697 tree_scroll_up(view, &s->first_displayed_entry, 1,
3698 s->entries, s->tree == s->root);
3699 break;
3700 case KEY_PPAGE:
3701 tree_scroll_up(view, &s->first_displayed_entry,
3702 MAX(0, view->nlines - 4 - s->selected), s->entries,
3703 s->tree == s->root);
3704 s->selected = 0;
3705 if (SIMPLEQ_FIRST(&s->entries->head) ==
3706 s->first_displayed_entry && s->tree != s->root)
3707 s->first_displayed_entry = NULL;
3708 break;
3709 case 'j':
3710 case KEY_DOWN:
3711 if (s->selected < s->ndisplayed - 1) {
3712 s->selected++;
3713 break;
3715 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3716 == NULL) {
3717 /* can't scroll any further */
3718 view_flash(view);
3719 break;
3721 tree_scroll_down(&s->first_displayed_entry, 1,
3722 s->last_displayed_entry, s->entries);
3723 break;
3724 case KEY_NPAGE:
3725 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3726 == NULL) {
3727 /* can't scroll any further; move cursor down */
3728 if (s->selected < s->ndisplayed - 1)
3729 s->selected = s->ndisplayed - 1;
3730 else
3731 view_flash(view);
3732 break;
3734 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3735 view->nlines, s->last_displayed_entry, s->entries);
3736 if (nscrolled < view->nlines) {
3737 int ndisplayed = 0;
3738 struct got_tree_entry *te;
3739 te = s->first_displayed_entry;
3740 do {
3741 ndisplayed++;
3742 te = SIMPLEQ_NEXT(te, entry);
3743 } while (te);
3744 s->selected = ndisplayed - 1;
3746 break;
3747 case KEY_ENTER:
3748 case '\r':
3749 if (s->selected_entry == NULL) {
3750 struct tog_parent_tree *parent;
3751 case KEY_BACKSPACE:
3752 /* user selected '..' */
3753 if (s->tree == s->root)
3754 break;
3755 parent = TAILQ_FIRST(&s->parents);
3756 TAILQ_REMOVE(&s->parents, parent,
3757 entry);
3758 got_object_tree_close(s->tree);
3759 s->tree = parent->tree;
3760 s->entries =
3761 got_object_tree_get_entries(s->tree);
3762 s->first_displayed_entry =
3763 parent->first_displayed_entry;
3764 s->selected_entry =
3765 parent->selected_entry;
3766 s->selected = parent->selected;
3767 free(parent);
3768 } else if (S_ISDIR(s->selected_entry->mode)) {
3769 struct tog_parent_tree *parent;
3770 struct got_tree_object *child;
3771 err = got_object_open_as_tree(&child,
3772 s->repo, s->selected_entry->id);
3773 if (err)
3774 break;
3775 parent = calloc(1, sizeof(*parent));
3776 if (parent == NULL) {
3777 err = got_error_from_errno();
3778 break;
3780 parent->tree = s->tree;
3781 parent->first_displayed_entry =
3782 s->first_displayed_entry;
3783 parent->selected_entry = s->selected_entry;
3784 parent->selected = s->selected;
3785 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3786 s->tree = child;
3787 s->entries =
3788 got_object_tree_get_entries(s->tree);
3789 s->selected = 0;
3790 s->first_displayed_entry = NULL;
3791 } else if (S_ISREG(s->selected_entry->mode)) {
3792 struct tog_view *blame_view;
3793 int begin_x = view_is_parent_view(view) ?
3794 view_split_begin_x(view->begin_x) : 0;
3796 err = blame_tree_entry(&blame_view, begin_x,
3797 s->selected_entry, &s->parents,
3798 s->commit_id, s->refs, s->repo);
3799 if (err)
3800 break;
3801 if (view_is_parent_view(view)) {
3802 err = view_close_child(view);
3803 if (err)
3804 return err;
3805 err = view_set_child(view, blame_view);
3806 if (err) {
3807 view_close(blame_view);
3808 break;
3810 *focus_view = blame_view;
3811 view->child_focussed = 1;
3812 } else
3813 *new_view = blame_view;
3815 break;
3816 case KEY_RESIZE:
3817 if (s->selected > view->nlines)
3818 s->selected = s->ndisplayed - 1;
3819 break;
3820 default:
3821 break;
3824 return err;
3827 __dead static void
3828 usage_tree(void)
3830 endwin();
3831 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3832 getprogname());
3833 exit(1);
3836 static const struct got_error *
3837 cmd_tree(int argc, char *argv[])
3839 const struct got_error *error;
3840 struct got_repository *repo = NULL;
3841 struct got_reflist_head refs;
3842 char *repo_path = NULL;
3843 struct got_object_id *commit_id = NULL;
3844 char *commit_id_arg = NULL;
3845 struct got_commit_object *commit = NULL;
3846 struct got_tree_object *tree = NULL;
3847 int ch;
3848 struct tog_view *view;
3850 #ifndef PROFILE
3851 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3852 NULL) == -1)
3853 err(1, "pledge");
3854 #endif
3856 while ((ch = getopt(argc, argv, "c:")) != -1) {
3857 switch (ch) {
3858 case 'c':
3859 commit_id_arg = optarg;
3860 break;
3861 default:
3862 usage_tree();
3863 /* NOTREACHED */
3867 argc -= optind;
3868 argv += optind;
3870 if (argc == 0) {
3871 struct got_worktree *worktree;
3872 char *cwd = getcwd(NULL, 0);
3873 if (cwd == NULL)
3874 return got_error_from_errno();
3875 error = got_worktree_open(&worktree, cwd);
3876 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3877 goto done;
3878 if (worktree) {
3879 free(cwd);
3880 repo_path =
3881 strdup(got_worktree_get_repo_path(worktree));
3882 got_worktree_close(worktree);
3883 } else
3884 repo_path = cwd;
3885 if (repo_path == NULL) {
3886 error = got_error_from_errno();
3887 goto done;
3889 } else if (argc == 1) {
3890 repo_path = realpath(argv[0], NULL);
3891 if (repo_path == NULL)
3892 return got_error_from_errno();
3893 } else
3894 usage_log();
3896 init_curses();
3898 error = apply_unveil(repo_path, NULL);
3899 if (error)
3900 goto done;
3902 error = got_repo_open(&repo, repo_path);
3903 if (error != NULL)
3904 goto done;
3906 if (commit_id_arg == NULL)
3907 error = get_head_commit_id(&commit_id, repo);
3908 else
3909 error = got_object_resolve_id_str(&commit_id, repo,
3910 commit_id_arg);
3911 if (error != NULL)
3912 goto done;
3914 error = got_object_open_as_commit(&commit, repo, commit_id);
3915 if (error != NULL)
3916 goto done;
3918 error = got_object_open_as_tree(&tree, repo,
3919 got_object_commit_get_tree_id(commit));
3920 if (error != NULL)
3921 goto done;
3923 SIMPLEQ_INIT(&refs);
3924 error = got_ref_list(&refs, repo);
3925 if (error)
3926 goto done;
3928 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3929 if (view == NULL) {
3930 error = got_error_from_errno();
3931 goto done;
3933 error = open_tree_view(view, tree, commit_id, &refs, repo);
3934 if (error)
3935 goto done;
3936 error = view_loop(view);
3937 done:
3938 free(repo_path);
3939 free(commit_id);
3940 if (commit)
3941 got_object_commit_close(commit);
3942 if (tree)
3943 got_object_tree_close(tree);
3944 if (repo)
3945 got_repo_close(repo);
3946 got_ref_list_free(&refs);
3947 return error;
3950 __dead static void
3951 usage(void)
3953 int i;
3955 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3956 "Available commands:\n", getprogname());
3957 for (i = 0; i < nitems(tog_commands); i++) {
3958 struct tog_cmd *cmd = &tog_commands[i];
3959 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3961 exit(1);
3964 static char **
3965 make_argv(const char *arg0, const char *arg1)
3967 char **argv;
3968 int argc = (arg1 == NULL ? 1 : 2);
3970 argv = calloc(argc, sizeof(char *));
3971 if (argv == NULL)
3972 err(1, "calloc");
3973 argv[0] = strdup(arg0);
3974 if (argv[0] == NULL)
3975 err(1, "calloc");
3976 if (arg1) {
3977 argv[1] = strdup(arg1);
3978 if (argv[1] == NULL)
3979 err(1, "calloc");
3982 return argv;
3985 int
3986 main(int argc, char *argv[])
3988 const struct got_error *error = NULL;
3989 struct tog_cmd *cmd = NULL;
3990 int ch, hflag = 0;
3991 char **cmd_argv = NULL;
3993 setlocale(LC_CTYPE, "");
3995 while ((ch = getopt(argc, argv, "h")) != -1) {
3996 switch (ch) {
3997 case 'h':
3998 hflag = 1;
3999 break;
4000 default:
4001 usage();
4002 /* NOTREACHED */
4006 argc -= optind;
4007 argv += optind;
4008 optind = 0;
4009 optreset = 1;
4011 if (argc == 0) {
4012 if (hflag)
4013 usage();
4014 /* Build an argument vector which runs a default command. */
4015 cmd = &tog_commands[0];
4016 cmd_argv = make_argv(cmd->name, NULL);
4017 argc = 1;
4018 } else {
4019 int i;
4021 /* Did the user specific a command? */
4022 for (i = 0; i < nitems(tog_commands); i++) {
4023 if (strncmp(tog_commands[i].name, argv[0],
4024 strlen(argv[0])) == 0) {
4025 cmd = &tog_commands[i];
4026 if (hflag)
4027 tog_commands[i].cmd_usage();
4028 break;
4031 if (cmd == NULL) {
4032 /* Did the user specify a repository? */
4033 char *repo_path = realpath(argv[0], NULL);
4034 if (repo_path) {
4035 struct got_repository *repo;
4036 error = got_repo_open(&repo, repo_path);
4037 if (error == NULL)
4038 got_repo_close(repo);
4039 } else
4040 error = got_error_from_errno();
4041 if (error) {
4042 if (hflag) {
4043 fprintf(stderr, "%s: '%s' is not a "
4044 "known command\n", getprogname(),
4045 argv[0]);
4046 usage();
4048 fprintf(stderr, "%s: '%s' is neither a known "
4049 "command nor a path to a repository\n",
4050 getprogname(), argv[0]);
4051 free(repo_path);
4052 return 1;
4054 cmd = &tog_commands[0];
4055 cmd_argv = make_argv(cmd->name, repo_path);
4056 argc = 2;
4057 free(repo_path);
4061 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4062 if (error)
4063 goto done;
4064 done:
4065 endwin();
4066 free(cmd_argv);
4067 if (error)
4068 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4069 return 0;