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 err = got_error_set_errno(errcode);
1452 break;
1453 } else if (*a->quit)
1454 done = 1;
1455 else if (*a->first_displayed_entry == NULL) {
1456 *a->first_displayed_entry =
1457 TAILQ_FIRST(&a->commits->head);
1458 *a->selected_entry = *a->first_displayed_entry;
1461 if (done)
1462 a->commits_needed = 0;
1463 else if (a->commits_needed == 0) {
1464 errcode = pthread_cond_wait(&a->need_commits,
1465 &tog_mutex);
1466 if (errcode)
1467 err = got_error_set_errno(errcode);
1470 errcode = pthread_mutex_unlock(&tog_mutex);
1471 if (errcode && err == NULL)
1472 err = got_error_set_errno(errcode);
1474 a->log_complete = 1;
1475 return (void *)err;
1478 static const struct got_error *
1479 stop_log_thread(struct tog_log_view_state *s)
1481 const struct got_error *err = NULL;
1482 int errcode;
1484 if (s->thread) {
1485 s->quit = 1;
1486 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1487 if (errcode)
1488 return got_error_set_errno(errcode);
1489 errcode = pthread_mutex_unlock(&tog_mutex);
1490 if (errcode)
1491 return got_error_set_errno(errcode);
1492 errcode = pthread_join(s->thread, (void **)&err);
1493 if (errcode)
1494 return got_error_set_errno(errcode);
1495 errcode = pthread_mutex_lock(&tog_mutex);
1496 if (errcode)
1497 return got_error_set_errno(errcode);
1498 s->thread = NULL;
1501 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1502 if (errcode && err == NULL)
1503 err = got_error_set_errno(errcode);
1505 if (s->thread_args.repo) {
1506 got_repo_close(s->thread_args.repo);
1507 s->thread_args.repo = NULL;
1510 if (s->thread_args.graph) {
1511 got_commit_graph_close(s->thread_args.graph);
1512 s->thread_args.graph = NULL;
1515 return err;
1518 static const struct got_error *
1519 close_log_view(struct tog_view *view)
1521 const struct got_error *err = NULL;
1522 struct tog_log_view_state *s = &view->state.log;
1524 err = stop_log_thread(s);
1525 free_commits(&s->commits);
1526 free(s->in_repo_path);
1527 s->in_repo_path = NULL;
1528 free(s->start_id);
1529 s->start_id = NULL;
1530 return err;
1533 static const struct got_error *
1534 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1535 struct got_reflist_head *refs, struct got_repository *repo,
1536 const char *path, int check_disk)
1538 const struct got_error *err = NULL;
1539 struct tog_log_view_state *s = &view->state.log;
1540 struct got_repository *thread_repo = NULL;
1541 struct got_commit_graph *thread_graph = NULL;
1542 int errcode;
1544 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1545 if (err != NULL)
1546 goto done;
1548 /* The commit queue only contains commits being displayed. */
1549 TAILQ_INIT(&s->commits.head);
1550 s->commits.ncommits = 0;
1552 s->refs = refs;
1553 s->repo = repo;
1554 s->start_id = got_object_id_dup(start_id);
1555 if (s->start_id == NULL) {
1556 err = got_error_from_errno();
1557 goto done;
1560 view->show = show_log_view;
1561 view->input = input_log_view;
1562 view->close = close_log_view;
1564 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1565 if (err)
1566 goto done;
1567 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1568 0, thread_repo);
1569 if (err)
1570 goto done;
1572 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1573 if (errcode) {
1574 err = got_error_set_errno(errcode);
1575 goto done;
1578 s->thread_args.commits_needed = view->nlines;
1579 s->thread_args.graph = thread_graph;
1580 s->thread_args.commits = &s->commits;
1581 s->thread_args.in_repo_path = s->in_repo_path;
1582 s->thread_args.start_id = s->start_id;
1583 s->thread_args.repo = thread_repo;
1584 s->thread_args.log_complete = 0;
1585 s->thread_args.quit = &s->quit;
1586 s->thread_args.view = view;
1587 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1588 s->thread_args.selected_entry = &s->selected_entry;
1589 done:
1590 if (err)
1591 close_log_view(view);
1592 return err;
1595 static const struct got_error *
1596 show_log_view(struct tog_view *view)
1598 struct tog_log_view_state *s = &view->state.log;
1600 if (s->thread == NULL) {
1601 int errcode = pthread_create(&s->thread, NULL, log_thread,
1602 &s->thread_args);
1603 if (errcode)
1604 return got_error_set_errno(errcode);
1607 return draw_commits(view, &s->last_displayed_entry,
1608 &s->selected_entry, s->first_displayed_entry,
1609 &s->commits, s->selected, view->nlines, s->refs,
1610 s->in_repo_path, s->thread_args.commits_needed);
1613 static const struct got_error *
1614 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1615 struct tog_view **focus_view, struct tog_view *view, int ch)
1617 const struct got_error *err = NULL;
1618 struct tog_log_view_state *s = &view->state.log;
1619 char *parent_path;
1620 struct tog_view *diff_view = NULL, *tree_view = NULL;
1621 int begin_x = 0;
1623 switch (ch) {
1624 case 'q':
1625 s->quit = 1;
1626 break;
1627 case 'k':
1628 case KEY_UP:
1629 case '<':
1630 case ',':
1631 if (s->first_displayed_entry == NULL)
1632 break;
1633 if (s->selected > 0)
1634 s->selected--;
1635 if (s->selected > 0)
1636 break;
1637 scroll_up(view, &s->first_displayed_entry, 1,
1638 &s->commits);
1639 break;
1640 case KEY_PPAGE:
1641 if (s->first_displayed_entry == NULL)
1642 break;
1643 if (TAILQ_FIRST(&s->commits.head) ==
1644 s->first_displayed_entry) {
1645 if (s->selected == 0) {
1646 view_flash(view);
1647 break;
1649 s->selected = 0;
1650 break;
1652 scroll_up(view, &s->first_displayed_entry,
1653 view->nlines, &s->commits);
1654 break;
1655 case 'j':
1656 case KEY_DOWN:
1657 case '>':
1658 case '.':
1659 if (s->first_displayed_entry == NULL)
1660 break;
1661 if (s->selected < MIN(view->nlines - 2,
1662 s->commits.ncommits - 1)) {
1663 s->selected++;
1664 break;
1666 err = scroll_down(view, &s->first_displayed_entry, 1,
1667 &s->last_displayed_entry, &s->commits,
1668 &s->thread_args.log_complete,
1669 &s->thread_args.commits_needed,
1670 &s->thread_args.need_commits);
1671 break;
1672 case KEY_NPAGE: {
1673 struct commit_queue_entry *first;
1674 first = s->first_displayed_entry;
1675 if (first == NULL)
1676 break;
1677 err = scroll_down(view, &s->first_displayed_entry,
1678 view->nlines, &s->last_displayed_entry,
1679 &s->commits, &s->thread_args.log_complete,
1680 &s->thread_args.commits_needed,
1681 &s->thread_args.need_commits);
1682 if (first == s->first_displayed_entry &&
1683 s->selected < MIN(view->nlines - 2,
1684 s->commits.ncommits - 1)) {
1685 /* can't scroll further down */
1686 s->selected = MIN(view->nlines - 2,
1687 s->commits.ncommits - 1);
1689 err = NULL;
1690 break;
1692 case KEY_RESIZE:
1693 if (s->selected > view->nlines - 2)
1694 s->selected = view->nlines - 2;
1695 if (s->selected > s->commits.ncommits - 1)
1696 s->selected = s->commits.ncommits - 1;
1697 break;
1698 case KEY_ENTER:
1699 case '\r':
1700 if (s->selected_entry == NULL)
1701 break;
1702 if (view_is_parent_view(view))
1703 begin_x = view_split_begin_x(view->begin_x);
1704 err = open_diff_view_for_commit(&diff_view, begin_x,
1705 s->selected_entry->commit, s->selected_entry->id,
1706 view, s->refs, s->repo);
1707 if (err)
1708 break;
1709 if (view_is_parent_view(view)) {
1710 err = view_close_child(view);
1711 if (err)
1712 return err;
1713 err = view_set_child(view, diff_view);
1714 if (err) {
1715 view_close(diff_view);
1716 break;
1718 *focus_view = diff_view;
1719 view->child_focussed = 1;
1720 } else
1721 *new_view = diff_view;
1722 break;
1723 case 't':
1724 if (s->selected_entry == NULL)
1725 break;
1726 if (view_is_parent_view(view))
1727 begin_x = view_split_begin_x(view->begin_x);
1728 err = browse_commit(&tree_view, begin_x,
1729 s->selected_entry, s->refs, s->repo);
1730 if (err)
1731 break;
1732 if (view_is_parent_view(view)) {
1733 err = view_close_child(view);
1734 if (err)
1735 return err;
1736 err = view_set_child(view, tree_view);
1737 if (err) {
1738 view_close(tree_view);
1739 break;
1741 *focus_view = tree_view;
1742 view->child_focussed = 1;
1743 } else
1744 *new_view = tree_view;
1745 break;
1746 case KEY_BACKSPACE:
1747 if (strcmp(s->in_repo_path, "/") == 0)
1748 break;
1749 parent_path = dirname(s->in_repo_path);
1750 if (parent_path && strcmp(parent_path, ".") != 0) {
1751 struct tog_view *lv;
1752 err = stop_log_thread(s);
1753 if (err)
1754 return err;
1755 lv = view_open(view->nlines, view->ncols,
1756 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1757 if (lv == NULL)
1758 return got_error_from_errno();
1759 err = open_log_view(lv, s->start_id, s->refs,
1760 s->repo, parent_path, 0);
1761 if (err)
1762 return err;;
1763 if (view_is_parent_view(view))
1764 *new_view = lv;
1765 else {
1766 view_set_child(view->parent, lv);
1767 *focus_view = lv;
1769 return NULL;
1771 break;
1772 default:
1773 break;
1776 return err;
1779 static const struct got_error *
1780 apply_unveil(const char *repo_path, const char *worktree_path)
1782 const struct got_error *error;
1784 if (repo_path && unveil(repo_path, "r") != 0)
1785 return got_error_from_errno();
1787 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1788 return got_error_from_errno();
1790 if (unveil("/tmp", "rwc") != 0)
1791 return got_error_from_errno();
1793 error = got_privsep_unveil_exec_helpers();
1794 if (error != NULL)
1795 return error;
1797 if (unveil(NULL, NULL) != 0)
1798 return got_error_from_errno();
1800 return NULL;
1803 static void
1804 init_curses(void)
1806 initscr();
1807 cbreak();
1808 halfdelay(1); /* Do fast refresh while initial view is loading. */
1809 noecho();
1810 nonl();
1811 intrflush(stdscr, FALSE);
1812 keypad(stdscr, TRUE);
1813 curs_set(0);
1814 signal(SIGWINCH, tog_sigwinch);
1817 static const struct got_error *
1818 cmd_log(int argc, char *argv[])
1820 const struct got_error *error;
1821 struct got_repository *repo = NULL;
1822 struct got_worktree *worktree = NULL;
1823 struct got_reflist_head refs;
1824 struct got_object_id *start_id = NULL;
1825 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1826 char *start_commit = NULL;
1827 int ch;
1828 struct tog_view *view;
1830 SIMPLEQ_INIT(&refs);
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 cwd = getcwd(NULL, 0);
1858 if (cwd == NULL) {
1859 error = got_error_from_errno();
1860 goto done;
1862 error = got_worktree_open(&worktree, cwd);
1863 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1864 goto done;
1865 error = NULL;
1867 if (argc == 0) {
1868 path = strdup("");
1869 if (path == NULL) {
1870 error = got_error_from_errno();
1871 goto done;
1873 } else if (argc == 1) {
1874 if (worktree) {
1875 error = got_worktree_resolve_path(&path, worktree,
1876 argv[0]);
1877 if (error)
1878 goto done;
1879 } else {
1880 path = strdup(argv[0]);
1881 if (path == NULL) {
1882 error = got_error_from_errno();
1883 goto done;
1886 } else
1887 usage_log();
1889 repo_path = worktree ?
1890 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1891 if (repo_path == NULL) {
1892 error = got_error_from_errno();
1893 goto done;
1896 init_curses();
1898 error = got_repo_open(&repo, repo_path);
1899 if (error != NULL)
1900 goto done;
1902 error = apply_unveil(got_repo_get_path(repo),
1903 worktree ? got_worktree_get_root_path(worktree) : NULL);
1904 if (error)
1905 goto done;
1907 if (start_commit == NULL)
1908 error = get_head_commit_id(&start_id, repo);
1909 else
1910 error = got_object_resolve_id_str(&start_id, repo,
1911 start_commit);
1912 if (error != NULL)
1913 goto done;
1915 error = got_ref_list(&refs, repo);
1916 if (error)
1917 goto done;
1919 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1920 if (view == NULL) {
1921 error = got_error_from_errno();
1922 goto done;
1924 error = open_log_view(view, start_id, &refs, repo, path, 1);
1925 if (error)
1926 goto done;
1927 error = view_loop(view);
1928 done:
1929 free(repo_path);
1930 free(cwd);
1931 free(path);
1932 free(start_id);
1933 if (repo)
1934 got_repo_close(repo);
1935 if (worktree)
1936 got_worktree_close(worktree);
1937 got_ref_list_free(&refs);
1938 return error;
1941 __dead static void
1942 usage_diff(void)
1944 endwin();
1945 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1946 getprogname());
1947 exit(1);
1950 static char *
1951 parse_next_line(FILE *f, size_t *len)
1953 char *line;
1954 size_t linelen;
1955 size_t lineno;
1956 const char delim[3] = { '\0', '\0', '\0'};
1958 line = fparseln(f, &linelen, &lineno, delim, 0);
1959 if (len)
1960 *len = linelen;
1961 return line;
1964 static const struct got_error *
1965 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1966 int *last_displayed_line, int *eof, int max_lines,
1967 char * header)
1969 const struct got_error *err;
1970 int nlines = 0, nprinted = 0;
1971 char *line;
1972 size_t len;
1973 wchar_t *wline;
1974 int width;
1976 rewind(f);
1977 werase(view->window);
1979 if (header) {
1980 err = format_line(&wline, &width, header, view->ncols);
1981 if (err) {
1982 return err;
1985 if (view_needs_focus_indication(view))
1986 wstandout(view->window);
1987 waddwstr(view->window, wline);
1988 if (view_needs_focus_indication(view))
1989 wstandend(view->window);
1990 if (width < view->ncols)
1991 waddch(view->window, '\n');
1993 if (max_lines <= 1)
1994 return NULL;
1995 max_lines--;
1998 *eof = 0;
1999 while (nprinted < max_lines) {
2000 line = parse_next_line(f, &len);
2001 if (line == NULL) {
2002 *eof = 1;
2003 break;
2005 if (++nlines < *first_displayed_line) {
2006 free(line);
2007 continue;
2010 err = format_line(&wline, &width, line, view->ncols);
2011 if (err) {
2012 free(line);
2013 return err;
2015 waddwstr(view->window, wline);
2016 if (width < view->ncols)
2017 waddch(view->window, '\n');
2018 if (++nprinted == 1)
2019 *first_displayed_line = nlines;
2020 free(line);
2021 free(wline);
2022 wline = NULL;
2024 *last_displayed_line = nlines;
2026 view_vborder(view);
2028 return NULL;
2031 static char *
2032 get_datestr(time_t *time, char *datebuf)
2034 char *p, *s = ctime_r(time, datebuf);
2035 p = strchr(s, '\n');
2036 if (p)
2037 *p = '\0';
2038 return s;
2041 static const struct got_error *
2042 write_commit_info(struct got_object_id *commit_id,
2043 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2045 const struct got_error *err = NULL;
2046 char datebuf[26];
2047 struct got_commit_object *commit;
2048 char *id_str = NULL;
2049 time_t committer_time;
2050 const char *author, *committer;
2051 char *refs_str = NULL;
2053 if (refs) {
2054 err = build_refs_str(&refs_str, refs, commit_id);
2055 if (err)
2056 return err;
2059 err = got_object_open_as_commit(&commit, repo, commit_id);
2060 if (err)
2061 return err;
2063 err = got_object_id_str(&id_str, commit_id);
2064 if (err) {
2065 err = got_error_from_errno();
2066 goto done;
2069 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2070 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2071 err = got_error_from_errno();
2072 goto done;
2074 if (fprintf(outfile, "from: %s\n",
2075 got_object_commit_get_author(commit)) < 0) {
2076 err = got_error_from_errno();
2077 goto done;
2079 committer_time = got_object_commit_get_committer_time(commit);
2080 if (fprintf(outfile, "date: %s UTC\n",
2081 get_datestr(&committer_time, datebuf)) < 0) {
2082 err = got_error_from_errno();
2083 goto done;
2085 author = got_object_commit_get_author(commit);
2086 committer = got_object_commit_get_committer(commit);
2087 if (strcmp(author, committer) != 0 &&
2088 fprintf(outfile, "via: %s\n", committer) < 0) {
2089 err = got_error_from_errno();
2090 goto done;
2092 if (fprintf(outfile, "%s\n",
2093 got_object_commit_get_logmsg(commit)) < 0) {
2094 err = got_error_from_errno();
2095 goto done;
2097 done:
2098 free(id_str);
2099 free(refs_str);
2100 got_object_commit_close(commit);
2101 return err;
2104 static const struct got_error *
2105 create_diff(struct tog_diff_view_state *s)
2107 const struct got_error *err = NULL;
2108 FILE *f = NULL;
2109 int obj_type;
2111 f = got_opentemp();
2112 if (f == NULL) {
2113 err = got_error_from_errno();
2114 goto done;
2116 if (s->f && fclose(s->f) != 0) {
2117 err = got_error_from_errno();
2118 goto done;
2120 s->f = f;
2122 if (s->id1)
2123 err = got_object_get_type(&obj_type, s->repo, s->id1);
2124 else
2125 err = got_object_get_type(&obj_type, s->repo, s->id2);
2126 if (err)
2127 goto done;
2129 switch (obj_type) {
2130 case GOT_OBJ_TYPE_BLOB:
2131 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2132 s->diff_context, s->repo, f);
2133 break;
2134 case GOT_OBJ_TYPE_TREE:
2135 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2136 s->diff_context, s->repo, f);
2137 break;
2138 case GOT_OBJ_TYPE_COMMIT: {
2139 const struct got_object_id_queue *parent_ids;
2140 struct got_object_qid *pid;
2141 struct got_commit_object *commit2;
2143 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2144 if (err)
2145 break;
2146 /* Show commit info if we're diffing to a parent/root commit. */
2147 if (s->id1 == NULL)
2148 write_commit_info(s->id2, s->refs, s->repo, f);
2149 else {
2150 parent_ids = got_object_commit_get_parent_ids(commit2);
2151 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2152 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2153 write_commit_info(s->id2, s->refs,
2154 s->repo, f);
2155 break;
2159 got_object_commit_close(commit2);
2161 err = got_diff_objects_as_commits(s->id1, s->id2,
2162 s->diff_context, s->repo, f);
2163 break;
2165 default:
2166 err = got_error(GOT_ERR_OBJ_TYPE);
2167 break;
2169 done:
2170 if (f && fflush(f) != 0 && err == NULL)
2171 err = got_error_from_errno();
2172 return err;
2175 static void
2176 diff_view_indicate_progress(struct tog_view *view)
2178 werase(view->window);
2179 waddstr(view->window, "diffing...");
2180 update_panels();
2181 doupdate();
2184 static const struct got_error *
2185 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2186 struct got_object_id *id2, struct tog_view *log_view,
2187 struct got_reflist_head *refs, struct got_repository *repo)
2189 const struct got_error *err;
2191 if (id1 != NULL && id2 != NULL) {
2192 int type1, type2;
2193 err = got_object_get_type(&type1, repo, id1);
2194 if (err)
2195 return err;
2196 err = got_object_get_type(&type2, repo, id2);
2197 if (err)
2198 return err;
2200 if (type1 != type2)
2201 return got_error(GOT_ERR_OBJ_TYPE);
2204 if (id1) {
2205 view->state.diff.id1 = got_object_id_dup(id1);
2206 if (view->state.diff.id1 == NULL)
2207 return got_error_from_errno();
2208 } else
2209 view->state.diff.id1 = NULL;
2211 view->state.diff.id2 = got_object_id_dup(id2);
2212 if (view->state.diff.id2 == NULL) {
2213 free(view->state.diff.id1);
2214 view->state.diff.id1 = NULL;
2215 return got_error_from_errno();
2217 view->state.diff.f = NULL;
2218 view->state.diff.first_displayed_line = 1;
2219 view->state.diff.last_displayed_line = view->nlines;
2220 view->state.diff.diff_context = 3;
2221 view->state.diff.log_view = log_view;
2222 view->state.diff.repo = repo;
2223 view->state.diff.refs = refs;
2225 if (log_view && view_is_splitscreen(view))
2226 show_log_view(log_view); /* draw vborder */
2227 diff_view_indicate_progress(view);
2229 err = create_diff(&view->state.diff);
2230 if (err) {
2231 free(view->state.diff.id1);
2232 view->state.diff.id1 = NULL;
2233 free(view->state.diff.id2);
2234 view->state.diff.id2 = NULL;
2235 return err;
2238 view->show = show_diff_view;
2239 view->input = input_diff_view;
2240 view->close = close_diff_view;
2242 return NULL;
2245 static const struct got_error *
2246 close_diff_view(struct tog_view *view)
2248 const struct got_error *err = NULL;
2250 free(view->state.diff.id1);
2251 view->state.diff.id1 = NULL;
2252 free(view->state.diff.id2);
2253 view->state.diff.id2 = NULL;
2254 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2255 err = got_error_from_errno();
2256 return err;
2259 static const struct got_error *
2260 show_diff_view(struct tog_view *view)
2262 const struct got_error *err;
2263 struct tog_diff_view_state *s = &view->state.diff;
2264 char *id_str1 = NULL, *id_str2, *header;
2266 if (s->id1) {
2267 err = got_object_id_str(&id_str1, s->id1);
2268 if (err)
2269 return err;
2271 err = got_object_id_str(&id_str2, s->id2);
2272 if (err)
2273 return err;
2275 if (asprintf(&header, "diff %s %s",
2276 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2277 err = got_error_from_errno();
2278 free(id_str1);
2279 free(id_str2);
2280 return err;
2282 free(id_str1);
2283 free(id_str2);
2285 return draw_file(view, s->f, &s->first_displayed_line,
2286 &s->last_displayed_line, &s->eof, view->nlines,
2287 header);
2290 static const struct got_error *
2291 set_selected_commit(struct tog_diff_view_state *s,
2292 struct commit_queue_entry *entry)
2294 const struct got_error *err;
2295 const struct got_object_id_queue *parent_ids;
2296 struct got_commit_object *selected_commit;
2297 struct got_object_qid *pid;
2299 free(s->id2);
2300 s->id2 = got_object_id_dup(entry->id);
2301 if (s->id2 == NULL)
2302 return got_error_from_errno();
2304 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2305 if (err)
2306 return err;
2307 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2308 free(s->id1);
2309 pid = SIMPLEQ_FIRST(parent_ids);
2310 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2311 got_object_commit_close(selected_commit);
2312 return NULL;
2315 static const struct got_error *
2316 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2317 struct tog_view **focus_view, struct tog_view *view, int ch)
2319 const struct got_error *err = NULL;
2320 struct tog_diff_view_state *s = &view->state.diff;
2321 struct tog_log_view_state *ls;
2322 struct commit_queue_entry *entry;
2323 int i;
2325 switch (ch) {
2326 case 'k':
2327 case KEY_UP:
2328 if (s->first_displayed_line > 1)
2329 s->first_displayed_line--;
2330 else
2331 view_flash(view);
2332 break;
2333 case KEY_PPAGE:
2334 if (s->first_displayed_line == 1) {
2335 view_flash(view);
2336 break;
2338 i = 0;
2339 while (i++ < view->nlines - 1 &&
2340 s->first_displayed_line > 1)
2341 s->first_displayed_line--;
2342 break;
2343 case 'j':
2344 case KEY_DOWN:
2345 if (!s->eof)
2346 s->first_displayed_line++;
2347 else
2348 view_flash(view);
2349 break;
2350 case KEY_NPAGE:
2351 case ' ':
2352 if (s->eof) {
2353 view_flash(view);
2354 break;
2356 i = 0;
2357 while (!s->eof && i++ < view->nlines - 1) {
2358 char *line;
2359 line = parse_next_line(s->f, NULL);
2360 s->first_displayed_line++;
2361 if (line == NULL)
2362 break;
2364 break;
2365 case '[':
2366 if (s->diff_context > 0) {
2367 s->diff_context--;
2368 diff_view_indicate_progress(view);
2369 err = create_diff(s);
2371 break;
2372 case ']':
2373 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2374 s->diff_context++;
2375 diff_view_indicate_progress(view);
2376 err = create_diff(s);
2378 break;
2379 case '<':
2380 case ',':
2381 if (s->log_view == NULL)
2382 break;
2383 ls = &s->log_view->state.log;
2384 entry = TAILQ_PREV(ls->selected_entry,
2385 commit_queue_head, entry);
2386 if (entry == NULL)
2387 break;
2389 err = input_log_view(NULL, NULL, NULL, s->log_view,
2390 KEY_UP);
2391 if (err)
2392 break;
2394 err = set_selected_commit(s, entry);
2395 if (err)
2396 break;
2398 s->first_displayed_line = 1;
2399 s->last_displayed_line = view->nlines;
2401 diff_view_indicate_progress(view);
2402 err = create_diff(s);
2403 break;
2404 case '>':
2405 case '.':
2406 if (s->log_view == NULL)
2407 break;
2408 ls = &s->log_view->state.log;
2410 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2411 ls->thread_args.commits_needed++;
2413 /* Display "loading..." in log view. */
2414 show_log_view(s->log_view);
2415 update_panels();
2416 doupdate();
2418 err = trigger_log_thread(1 /* load_all */,
2419 &ls->thread_args.commits_needed,
2420 &ls->thread_args.log_complete,
2421 &ls->thread_args.need_commits);
2422 if (err)
2423 break;
2425 err = input_log_view(NULL, NULL, NULL, s->log_view,
2426 KEY_DOWN);
2427 if (err)
2428 break;
2430 entry = TAILQ_NEXT(ls->selected_entry, entry);
2431 if (entry == NULL)
2432 break;
2434 err = set_selected_commit(s, entry);
2435 if (err)
2436 break;
2438 s->first_displayed_line = 1;
2439 s->last_displayed_line = view->nlines;
2441 diff_view_indicate_progress(view);
2442 err = create_diff(s);
2443 break;
2444 default:
2445 break;
2448 return err;
2451 static const struct got_error *
2452 cmd_diff(int argc, char *argv[])
2454 const struct got_error *error = NULL;
2455 struct got_repository *repo = NULL;
2456 struct got_reflist_head refs;
2457 struct got_object_id *id1 = NULL, *id2 = NULL;
2458 char *repo_path = NULL;
2459 char *id_str1 = NULL, *id_str2 = NULL;
2460 int ch;
2461 struct tog_view *view;
2463 SIMPLEQ_INIT(&refs);
2465 #ifndef PROFILE
2466 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2467 NULL) == -1)
2468 err(1, "pledge");
2469 #endif
2471 while ((ch = getopt(argc, argv, "")) != -1) {
2472 switch (ch) {
2473 default:
2474 usage_diff();
2475 /* NOTREACHED */
2479 argc -= optind;
2480 argv += optind;
2482 if (argc == 0) {
2483 usage_diff(); /* TODO show local worktree changes */
2484 } else if (argc == 2) {
2485 repo_path = getcwd(NULL, 0);
2486 if (repo_path == NULL)
2487 return got_error_from_errno();
2488 id_str1 = argv[0];
2489 id_str2 = argv[1];
2490 } else if (argc == 3) {
2491 repo_path = realpath(argv[0], NULL);
2492 if (repo_path == NULL)
2493 return got_error_from_errno();
2494 id_str1 = argv[1];
2495 id_str2 = argv[2];
2496 } else
2497 usage_diff();
2499 init_curses();
2501 error = got_repo_open(&repo, repo_path);
2502 if (error)
2503 goto done;
2505 error = apply_unveil(got_repo_get_path(repo), NULL);
2506 if (error)
2507 goto done;
2509 error = got_object_resolve_id_str(&id1, repo, id_str1);
2510 if (error)
2511 goto done;
2513 error = got_object_resolve_id_str(&id2, repo, id_str2);
2514 if (error)
2515 goto done;
2517 error = got_ref_list(&refs, repo);
2518 if (error)
2519 goto done;
2521 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2522 if (view == NULL) {
2523 error = got_error_from_errno();
2524 goto done;
2526 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2527 if (error)
2528 goto done;
2529 error = view_loop(view);
2530 done:
2531 free(repo_path);
2532 got_repo_close(repo);
2533 got_ref_list_free(&refs);
2534 return error;
2537 __dead static void
2538 usage_blame(void)
2540 endwin();
2541 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2542 getprogname());
2543 exit(1);
2546 struct tog_blame_line {
2547 int annotated;
2548 struct got_object_id *id;
2551 static const struct got_error *
2552 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2553 const char *path, struct tog_blame_line *lines, int nlines,
2554 int blame_complete, int selected_line, int *first_displayed_line,
2555 int *last_displayed_line, int *eof, int max_lines)
2557 const struct got_error *err;
2558 int lineno = 0, nprinted = 0;
2559 char *line;
2560 size_t len;
2561 wchar_t *wline;
2562 int width, wlimit;
2563 struct tog_blame_line *blame_line;
2564 struct got_object_id *prev_id = NULL;
2565 char *id_str;
2567 err = got_object_id_str(&id_str, id);
2568 if (err)
2569 return err;
2571 rewind(f);
2572 werase(view->window);
2574 if (asprintf(&line, "commit %s", id_str) == -1) {
2575 err = got_error_from_errno();
2576 free(id_str);
2577 return err;
2580 err = format_line(&wline, &width, line, view->ncols);
2581 free(line);
2582 line = NULL;
2583 if (view_needs_focus_indication(view))
2584 wstandout(view->window);
2585 waddwstr(view->window, wline);
2586 if (view_needs_focus_indication(view))
2587 wstandend(view->window);
2588 free(wline);
2589 wline = NULL;
2590 if (width < view->ncols)
2591 waddch(view->window, '\n');
2593 if (asprintf(&line, "[%d/%d] %s%s",
2594 *first_displayed_line - 1 + selected_line, nlines,
2595 blame_complete ? "" : "annotating... ", path) == -1) {
2596 free(id_str);
2597 return got_error_from_errno();
2599 free(id_str);
2600 err = format_line(&wline, &width, line, view->ncols);
2601 free(line);
2602 line = NULL;
2603 if (err)
2604 return err;
2605 waddwstr(view->window, wline);
2606 free(wline);
2607 wline = NULL;
2608 if (width < view->ncols)
2609 waddch(view->window, '\n');
2611 *eof = 0;
2612 while (nprinted < max_lines - 2) {
2613 line = parse_next_line(f, &len);
2614 if (line == NULL) {
2615 *eof = 1;
2616 break;
2618 if (++lineno < *first_displayed_line) {
2619 free(line);
2620 continue;
2623 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2624 err = format_line(&wline, &width, line, wlimit);
2625 if (err) {
2626 free(line);
2627 return err;
2630 if (view->focussed && nprinted == selected_line - 1)
2631 wstandout(view->window);
2633 blame_line = &lines[lineno - 1];
2634 if (blame_line->annotated && prev_id &&
2635 got_object_id_cmp(prev_id, blame_line->id) == 0)
2636 waddstr(view->window, " ");
2637 else if (blame_line->annotated) {
2638 char *id_str;
2639 err = got_object_id_str(&id_str, blame_line->id);
2640 if (err) {
2641 free(line);
2642 free(wline);
2643 return err;
2645 wprintw(view->window, "%.8s ", id_str);
2646 free(id_str);
2647 prev_id = blame_line->id;
2648 } else {
2649 waddstr(view->window, "........ ");
2650 prev_id = NULL;
2653 waddwstr(view->window, wline);
2654 while (width < wlimit) {
2655 waddch(view->window, ' ');
2656 width++;
2658 if (view->focussed && nprinted == selected_line - 1)
2659 wstandend(view->window);
2660 if (++nprinted == 1)
2661 *first_displayed_line = lineno;
2662 free(line);
2663 free(wline);
2664 wline = NULL;
2666 *last_displayed_line = lineno;
2668 view_vborder(view);
2670 return NULL;
2673 static const struct got_error *
2674 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2676 const struct got_error *err = NULL;
2677 struct tog_blame_cb_args *a = arg;
2678 struct tog_blame_line *line;
2679 int errcode;
2681 if (nlines != a->nlines ||
2682 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2683 return got_error(GOT_ERR_RANGE);
2685 errcode = pthread_mutex_lock(&tog_mutex);
2686 if (errcode)
2687 return got_error_set_errno(errcode);
2689 if (*a->quit) { /* user has quit the blame view */
2690 err = got_error(GOT_ERR_ITER_COMPLETED);
2691 goto done;
2694 if (lineno == -1)
2695 goto done; /* no change in this commit */
2697 line = &a->lines[lineno - 1];
2698 if (line->annotated)
2699 goto done;
2701 line->id = got_object_id_dup(id);
2702 if (line->id == NULL) {
2703 err = got_error_from_errno();
2704 goto done;
2706 line->annotated = 1;
2707 done:
2708 errcode = pthread_mutex_unlock(&tog_mutex);
2709 if (errcode)
2710 err = got_error_set_errno(errcode);
2711 return err;
2714 static void *
2715 blame_thread(void *arg)
2717 const struct got_error *err;
2718 struct tog_blame_thread_args *ta = arg;
2719 struct tog_blame_cb_args *a = ta->cb_args;
2720 int errcode;
2722 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2723 blame_cb, ta->cb_args);
2725 errcode = pthread_mutex_lock(&tog_mutex);
2726 if (errcode)
2727 return (void *)got_error_set_errno(errcode);
2729 got_repo_close(ta->repo);
2730 ta->repo = NULL;
2731 *ta->complete = 1;
2733 errcode = pthread_mutex_unlock(&tog_mutex);
2734 if (errcode && err == NULL)
2735 err = got_error_set_errno(errcode);
2737 return (void *)err;
2740 static struct got_object_id *
2741 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2742 int selected_line)
2744 struct tog_blame_line *line;
2746 line = &lines[first_displayed_line - 1 + selected_line - 1];
2747 if (!line->annotated)
2748 return NULL;
2750 return line->id;
2753 static const struct got_error *
2754 stop_blame(struct tog_blame *blame)
2756 const struct got_error *err = NULL;
2757 int i;
2759 if (blame->thread) {
2760 int errcode;
2761 errcode = pthread_mutex_unlock(&tog_mutex);
2762 if (errcode)
2763 return got_error_set_errno(errcode);
2764 errcode = pthread_join(blame->thread, (void **)&err);
2765 if (errcode)
2766 return got_error_set_errno(errcode);
2767 errcode = pthread_mutex_lock(&tog_mutex);
2768 if (errcode)
2769 return got_error_set_errno(errcode);
2770 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2771 err = NULL;
2772 blame->thread = NULL;
2774 if (blame->thread_args.repo) {
2775 got_repo_close(blame->thread_args.repo);
2776 blame->thread_args.repo = NULL;
2778 if (blame->f) {
2779 if (fclose(blame->f) != 0 && err == NULL)
2780 err = got_error_from_errno();
2781 blame->f = NULL;
2783 if (blame->lines) {
2784 for (i = 0; i < blame->nlines; i++)
2785 free(blame->lines[i].id);
2786 free(blame->lines);
2787 blame->lines = NULL;
2789 free(blame->cb_args.commit_id);
2790 blame->cb_args.commit_id = NULL;
2792 return err;
2795 static const struct got_error *
2796 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2797 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2798 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2799 struct got_repository *repo)
2801 const struct got_error *err = NULL;
2802 struct got_blob_object *blob = NULL;
2803 struct got_repository *thread_repo = NULL;
2804 struct got_object_id *obj_id = NULL;
2805 int obj_type;
2807 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2808 if (err)
2809 return err;
2810 if (obj_id == NULL)
2811 return got_error(GOT_ERR_NO_OBJ);
2813 err = got_object_get_type(&obj_type, repo, obj_id);
2814 if (err)
2815 goto done;
2817 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2818 err = got_error(GOT_ERR_OBJ_TYPE);
2819 goto done;
2822 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2823 if (err)
2824 goto done;
2825 blame->f = got_opentemp();
2826 if (blame->f == NULL) {
2827 err = got_error_from_errno();
2828 goto done;
2830 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2831 blame->f, blob);
2832 if (err)
2833 goto done;
2835 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2836 if (blame->lines == NULL) {
2837 err = got_error_from_errno();
2838 goto done;
2841 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2842 if (err)
2843 goto done;
2845 blame->cb_args.view = view;
2846 blame->cb_args.lines = blame->lines;
2847 blame->cb_args.nlines = blame->nlines;
2848 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2849 if (blame->cb_args.commit_id == NULL) {
2850 err = got_error_from_errno();
2851 goto done;
2853 blame->cb_args.quit = done;
2855 blame->thread_args.path = path;
2856 blame->thread_args.repo = thread_repo;
2857 blame->thread_args.cb_args = &blame->cb_args;
2858 blame->thread_args.complete = blame_complete;
2859 *blame_complete = 0;
2861 done:
2862 if (blob)
2863 got_object_blob_close(blob);
2864 free(obj_id);
2865 if (err)
2866 stop_blame(blame);
2867 return err;
2870 static const struct got_error *
2871 open_blame_view(struct tog_view *view, char *path,
2872 struct got_object_id *commit_id, struct got_reflist_head *refs,
2873 struct got_repository *repo)
2875 const struct got_error *err = NULL;
2876 struct tog_blame_view_state *s = &view->state.blame;
2878 SIMPLEQ_INIT(&s->blamed_commits);
2880 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2881 if (err)
2882 return err;
2884 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2885 s->first_displayed_line = 1;
2886 s->last_displayed_line = view->nlines;
2887 s->selected_line = 1;
2888 s->blame_complete = 0;
2889 s->path = path;
2890 if (s->path == NULL)
2891 return got_error_from_errno();
2892 s->repo = repo;
2893 s->refs = refs;
2894 s->commit_id = commit_id;
2895 memset(&s->blame, 0, sizeof(s->blame));
2897 view->show = show_blame_view;
2898 view->input = input_blame_view;
2899 view->close = close_blame_view;
2901 return run_blame(&s->blame, view, &s->blame_complete,
2902 &s->first_displayed_line, &s->last_displayed_line,
2903 &s->selected_line, &s->done, &s->eof, s->path,
2904 s->blamed_commit->id, s->repo);
2907 static const struct got_error *
2908 close_blame_view(struct tog_view *view)
2910 const struct got_error *err = NULL;
2911 struct tog_blame_view_state *s = &view->state.blame;
2913 if (s->blame.thread)
2914 err = stop_blame(&s->blame);
2916 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2917 struct got_object_qid *blamed_commit;
2918 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2919 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2920 got_object_qid_free(blamed_commit);
2923 free(s->path);
2925 return err;
2928 static const struct got_error *
2929 show_blame_view(struct tog_view *view)
2931 const struct got_error *err = NULL;
2932 struct tog_blame_view_state *s = &view->state.blame;
2933 int errcode;
2935 if (s->blame.thread == NULL) {
2936 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2937 &s->blame.thread_args);
2938 if (errcode)
2939 return got_error_set_errno(errcode);
2942 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2943 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2944 s->selected_line, &s->first_displayed_line,
2945 &s->last_displayed_line, &s->eof, view->nlines);
2947 view_vborder(view);
2948 return err;
2951 static const struct got_error *
2952 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2953 struct tog_view **focus_view, struct tog_view *view, int ch)
2955 const struct got_error *err = NULL, *thread_err = NULL;
2956 struct tog_view *diff_view;
2957 struct tog_blame_view_state *s = &view->state.blame;
2958 int begin_x = 0;
2960 switch (ch) {
2961 case 'q':
2962 s->done = 1;
2963 break;
2964 case 'k':
2965 case KEY_UP:
2966 if (s->selected_line > 1)
2967 s->selected_line--;
2968 else if (s->selected_line == 1 &&
2969 s->first_displayed_line > 1)
2970 s->first_displayed_line--;
2971 else
2972 view_flash(view);
2973 break;
2974 case KEY_PPAGE:
2975 if (s->first_displayed_line == 1) {
2976 if (s->selected_line == 1) {
2977 view_flash(view);
2978 break;
2980 s->selected_line = 1;
2981 break;
2983 if (s->first_displayed_line > view->nlines - 2)
2984 s->first_displayed_line -=
2985 (view->nlines - 2);
2986 else
2987 s->first_displayed_line = 1;
2988 break;
2989 case 'j':
2990 case KEY_DOWN:
2991 if (s->selected_line < view->nlines - 2 &&
2992 s->first_displayed_line +
2993 s->selected_line <= s->blame.nlines)
2994 s->selected_line++;
2995 else if (s->last_displayed_line <
2996 s->blame.nlines)
2997 s->first_displayed_line++;
2998 else
2999 view_flash(view);
3000 break;
3001 case 'b':
3002 case 'p': {
3003 struct got_object_id *id = NULL;
3004 id = get_selected_commit_id(s->blame.lines,
3005 s->first_displayed_line, s->selected_line);
3006 if (id == NULL)
3007 break;
3008 if (ch == 'p') {
3009 struct got_commit_object *commit;
3010 struct got_object_qid *pid;
3011 struct got_object_id *blob_id = NULL;
3012 int obj_type;
3013 err = got_object_open_as_commit(&commit,
3014 s->repo, id);
3015 if (err)
3016 break;
3017 pid = SIMPLEQ_FIRST(
3018 got_object_commit_get_parent_ids(commit));
3019 if (pid == NULL) {
3020 got_object_commit_close(commit);
3021 break;
3023 /* Check if path history ends here. */
3024 err = got_object_id_by_path(&blob_id, s->repo,
3025 pid->id, s->path);
3026 if (err) {
3027 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3028 err = NULL;
3029 got_object_commit_close(commit);
3030 break;
3032 err = got_object_get_type(&obj_type, s->repo,
3033 blob_id);
3034 free(blob_id);
3035 /* Can't blame non-blob type objects. */
3036 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3037 got_object_commit_close(commit);
3038 break;
3040 err = got_object_qid_alloc(&s->blamed_commit,
3041 pid->id);
3042 got_object_commit_close(commit);
3043 } else {
3044 if (got_object_id_cmp(id,
3045 s->blamed_commit->id) == 0)
3046 break;
3047 err = got_object_qid_alloc(&s->blamed_commit,
3048 id);
3050 if (err)
3051 break;
3052 s->done = 1;
3053 thread_err = stop_blame(&s->blame);
3054 s->done = 0;
3055 if (thread_err)
3056 break;
3057 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3058 s->blamed_commit, entry);
3059 err = run_blame(&s->blame, view, &s->blame_complete,
3060 &s->first_displayed_line, &s->last_displayed_line,
3061 &s->selected_line, &s->done, &s->eof,
3062 s->path, s->blamed_commit->id, s->repo);
3063 if (err)
3064 break;
3065 break;
3067 case 'B': {
3068 struct got_object_qid *first;
3069 first = SIMPLEQ_FIRST(&s->blamed_commits);
3070 if (!got_object_id_cmp(first->id, s->commit_id))
3071 break;
3072 s->done = 1;
3073 thread_err = stop_blame(&s->blame);
3074 s->done = 0;
3075 if (thread_err)
3076 break;
3077 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3078 got_object_qid_free(s->blamed_commit);
3079 s->blamed_commit =
3080 SIMPLEQ_FIRST(&s->blamed_commits);
3081 err = run_blame(&s->blame, view, &s->blame_complete,
3082 &s->first_displayed_line, &s->last_displayed_line,
3083 &s->selected_line, &s->done, &s->eof, s->path,
3084 s->blamed_commit->id, s->repo);
3085 if (err)
3086 break;
3087 break;
3089 case KEY_ENTER:
3090 case '\r': {
3091 struct got_object_id *id = NULL;
3092 struct got_object_qid *pid;
3093 struct got_commit_object *commit = NULL;
3094 id = get_selected_commit_id(s->blame.lines,
3095 s->first_displayed_line, s->selected_line);
3096 if (id == NULL)
3097 break;
3098 err = got_object_open_as_commit(&commit, s->repo, id);
3099 if (err)
3100 break;
3101 pid = SIMPLEQ_FIRST(
3102 got_object_commit_get_parent_ids(commit));
3103 if (view_is_parent_view(view))
3104 begin_x = view_split_begin_x(view->begin_x);
3105 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3106 if (diff_view == NULL) {
3107 got_object_commit_close(commit);
3108 err = got_error_from_errno();
3109 break;
3111 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3112 id, NULL, s->refs, s->repo);
3113 got_object_commit_close(commit);
3114 if (err) {
3115 view_close(diff_view);
3116 break;
3118 if (view_is_parent_view(view)) {
3119 err = view_close_child(view);
3120 if (err)
3121 break;
3122 err = view_set_child(view, diff_view);
3123 if (err) {
3124 view_close(diff_view);
3125 break;
3127 *focus_view = diff_view;
3128 view->child_focussed = 1;
3129 } else
3130 *new_view = diff_view;
3131 if (err)
3132 break;
3133 break;
3135 case KEY_NPAGE:
3136 case ' ':
3137 if (s->last_displayed_line >= s->blame.nlines &&
3138 s->selected_line >= MIN(s->blame.nlines,
3139 view->nlines - 2)) {
3140 view_flash(view);
3141 break;
3143 if (s->last_displayed_line >= s->blame.nlines &&
3144 s->selected_line < view->nlines - 2) {
3145 s->selected_line = MIN(s->blame.nlines,
3146 view->nlines - 2);
3147 break;
3149 if (s->last_displayed_line + view->nlines - 2
3150 <= s->blame.nlines)
3151 s->first_displayed_line +=
3152 view->nlines - 2;
3153 else
3154 s->first_displayed_line =
3155 s->blame.nlines -
3156 (view->nlines - 3);
3157 break;
3158 case KEY_RESIZE:
3159 if (s->selected_line > view->nlines - 2) {
3160 s->selected_line = MIN(s->blame.nlines,
3161 view->nlines - 2);
3163 break;
3164 default:
3165 break;
3167 return thread_err ? thread_err : err;
3170 static const struct got_error *
3171 cmd_blame(int argc, char *argv[])
3173 const struct got_error *error;
3174 struct got_repository *repo = NULL;
3175 struct got_reflist_head refs;
3176 struct got_worktree *worktree = NULL;
3177 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3178 struct got_object_id *commit_id = NULL;
3179 char *commit_id_str = NULL;
3180 int ch;
3181 struct tog_view *view;
3183 SIMPLEQ_INIT(&refs);
3185 #ifndef PROFILE
3186 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3187 NULL) == -1)
3188 err(1, "pledge");
3189 #endif
3191 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3192 switch (ch) {
3193 case 'c':
3194 commit_id_str = optarg;
3195 break;
3196 case 'r':
3197 repo_path = realpath(optarg, NULL);
3198 if (repo_path == NULL)
3199 err(1, "-r option");
3200 break;
3201 default:
3202 usage_blame();
3203 /* NOTREACHED */
3207 argc -= optind;
3208 argv += optind;
3210 if (argc == 1)
3211 path = argv[0];
3212 else
3213 usage_blame();
3215 cwd = getcwd(NULL, 0);
3216 if (cwd == NULL) {
3217 error = got_error_from_errno();
3218 goto done;
3220 if (repo_path == NULL) {
3221 error = got_worktree_open(&worktree, cwd);
3222 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3223 goto done;
3224 else
3225 error = NULL;
3226 if (worktree) {
3227 repo_path =
3228 strdup(got_worktree_get_repo_path(worktree));
3229 if (repo_path == NULL)
3230 error = got_error_from_errno();
3231 if (error)
3232 goto done;
3233 } else {
3234 repo_path = strdup(cwd);
3235 if (repo_path == NULL) {
3236 error = got_error_from_errno();
3237 goto done;
3242 init_curses();
3244 error = got_repo_open(&repo, repo_path);
3245 if (error != NULL)
3246 goto done;
3248 error = apply_unveil(got_repo_get_path(repo), NULL);
3249 if (error)
3250 goto done;
3252 if (worktree) {
3253 const char *prefix = got_worktree_get_path_prefix(worktree);
3254 char *p, *worktree_subdir = cwd +
3255 strlen(got_worktree_get_root_path(worktree));
3256 if (asprintf(&p, "%s%s%s%s%s",
3257 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3258 worktree_subdir, worktree_subdir[0] ? "/" : "",
3259 path) == -1) {
3260 error = got_error_from_errno();
3261 goto done;
3263 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3264 free(p);
3265 } else {
3266 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3268 if (error)
3269 goto done;
3271 if (commit_id_str == NULL) {
3272 struct got_reference *head_ref;
3273 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3274 if (error != NULL)
3275 goto done;
3276 error = got_ref_resolve(&commit_id, repo, head_ref);
3277 got_ref_close(head_ref);
3278 } else {
3279 error = got_object_resolve_id_str(&commit_id, repo,
3280 commit_id_str);
3282 if (error != NULL)
3283 goto done;
3285 error = got_ref_list(&refs, repo);
3286 if (error)
3287 goto done;
3289 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3290 if (view == NULL) {
3291 error = got_error_from_errno();
3292 goto done;
3294 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3295 if (error)
3296 goto done;
3297 error = view_loop(view);
3298 done:
3299 free(repo_path);
3300 free(cwd);
3301 free(commit_id);
3302 if (worktree)
3303 got_worktree_close(worktree);
3304 if (repo)
3305 got_repo_close(repo);
3306 got_ref_list_free(&refs);
3307 return error;
3310 static const struct got_error *
3311 draw_tree_entries(struct tog_view *view,
3312 struct got_tree_entry **first_displayed_entry,
3313 struct got_tree_entry **last_displayed_entry,
3314 struct got_tree_entry **selected_entry, int *ndisplayed,
3315 const char *label, int show_ids, const char *parent_path,
3316 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3318 const struct got_error *err = NULL;
3319 struct got_tree_entry *te;
3320 wchar_t *wline;
3321 int width, n;
3323 *ndisplayed = 0;
3325 werase(view->window);
3327 if (limit == 0)
3328 return NULL;
3330 err = format_line(&wline, &width, label, view->ncols);
3331 if (err)
3332 return err;
3333 if (view_needs_focus_indication(view))
3334 wstandout(view->window);
3335 waddwstr(view->window, wline);
3336 if (view_needs_focus_indication(view))
3337 wstandend(view->window);
3338 free(wline);
3339 wline = NULL;
3340 if (width < view->ncols)
3341 waddch(view->window, '\n');
3342 if (--limit <= 0)
3343 return NULL;
3344 err = format_line(&wline, &width, parent_path, view->ncols);
3345 if (err)
3346 return err;
3347 waddwstr(view->window, wline);
3348 free(wline);
3349 wline = NULL;
3350 if (width < view->ncols)
3351 waddch(view->window, '\n');
3352 if (--limit <= 0)
3353 return NULL;
3354 waddch(view->window, '\n');
3355 if (--limit <= 0)
3356 return NULL;
3358 te = SIMPLEQ_FIRST(&entries->head);
3359 if (*first_displayed_entry == NULL) {
3360 if (selected == 0) {
3361 if (view->focussed)
3362 wstandout(view->window);
3363 *selected_entry = NULL;
3365 waddstr(view->window, " ..\n"); /* parent directory */
3366 if (selected == 0 && view->focussed)
3367 wstandend(view->window);
3368 (*ndisplayed)++;
3369 if (--limit <= 0)
3370 return NULL;
3371 n = 1;
3372 } else {
3373 n = 0;
3374 while (te != *first_displayed_entry)
3375 te = SIMPLEQ_NEXT(te, entry);
3378 while (te) {
3379 char *line = NULL, *id_str = NULL;
3381 if (show_ids) {
3382 err = got_object_id_str(&id_str, te->id);
3383 if (err)
3384 return got_error_from_errno();
3386 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3387 te->name, S_ISDIR(te->mode) ? "/" :
3388 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3389 free(id_str);
3390 return got_error_from_errno();
3392 free(id_str);
3393 err = format_line(&wline, &width, line, view->ncols);
3394 if (err) {
3395 free(line);
3396 break;
3398 if (n == selected) {
3399 if (view->focussed)
3400 wstandout(view->window);
3401 *selected_entry = te;
3403 waddwstr(view->window, wline);
3404 if (width < view->ncols)
3405 waddch(view->window, '\n');
3406 if (n == selected && view->focussed)
3407 wstandend(view->window);
3408 free(line);
3409 free(wline);
3410 wline = NULL;
3411 n++;
3412 (*ndisplayed)++;
3413 *last_displayed_entry = te;
3414 if (--limit <= 0)
3415 break;
3416 te = SIMPLEQ_NEXT(te, entry);
3419 return err;
3422 static void
3423 tree_scroll_up(struct tog_view *view,
3424 struct got_tree_entry **first_displayed_entry, int maxscroll,
3425 const struct got_tree_entries *entries, int isroot)
3427 struct got_tree_entry *te, *prev;
3428 int i;
3430 if (*first_displayed_entry == NULL) {
3431 view_flash(view);
3432 return;
3435 te = SIMPLEQ_FIRST(&entries->head);
3436 if (*first_displayed_entry == te) {
3437 view_flash(view);
3438 if (!isroot)
3439 *first_displayed_entry = NULL;
3440 return;
3443 /* XXX this is stupid... switch to TAILQ? */
3444 for (i = 0; i < maxscroll; i++) {
3445 while (te != *first_displayed_entry) {
3446 prev = te;
3447 te = SIMPLEQ_NEXT(te, entry);
3449 *first_displayed_entry = prev;
3450 te = SIMPLEQ_FIRST(&entries->head);
3452 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3453 *first_displayed_entry = NULL;
3456 static int
3457 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3458 struct got_tree_entry *last_displayed_entry,
3459 const struct got_tree_entries *entries)
3461 struct got_tree_entry *next, *last;
3462 int n = 0;
3464 if (*first_displayed_entry)
3465 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3466 else
3467 next = SIMPLEQ_FIRST(&entries->head);
3468 last = last_displayed_entry;
3469 while (next && last && n++ < maxscroll) {
3470 last = SIMPLEQ_NEXT(last, entry);
3471 if (last) {
3472 *first_displayed_entry = next;
3473 next = SIMPLEQ_NEXT(next, entry);
3476 return n;
3479 static const struct got_error *
3480 tree_entry_path(char **path, struct tog_parent_trees *parents,
3481 struct got_tree_entry *te)
3483 const struct got_error *err = NULL;
3484 struct tog_parent_tree *pt;
3485 size_t len = 2; /* for leading slash and NUL */
3487 TAILQ_FOREACH(pt, parents, entry)
3488 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3489 if (te)
3490 len += strlen(te->name);
3492 *path = calloc(1, len);
3493 if (path == NULL)
3494 return got_error_from_errno();
3496 (*path)[0] = '/';
3497 pt = TAILQ_LAST(parents, tog_parent_trees);
3498 while (pt) {
3499 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3500 err = got_error(GOT_ERR_NO_SPACE);
3501 goto done;
3503 if (strlcat(*path, "/", len) >= len) {
3504 err = got_error(GOT_ERR_NO_SPACE);
3505 goto done;
3507 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3509 if (te) {
3510 if (strlcat(*path, te->name, len) >= len) {
3511 err = got_error(GOT_ERR_NO_SPACE);
3512 goto done;
3515 done:
3516 if (err) {
3517 free(*path);
3518 *path = NULL;
3520 return err;
3523 static const struct got_error *
3524 blame_tree_entry(struct tog_view **new_view, int begin_x,
3525 struct got_tree_entry *te, struct tog_parent_trees *parents,
3526 struct got_object_id *commit_id, struct got_reflist_head *refs,
3527 struct got_repository *repo)
3529 const struct got_error *err = NULL;
3530 char *path;
3531 struct tog_view *blame_view;
3533 err = tree_entry_path(&path, parents, te);
3534 if (err)
3535 return err;
3537 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3538 if (blame_view == NULL)
3539 return got_error_from_errno();
3541 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3542 if (err) {
3543 view_close(blame_view);
3544 free(path);
3545 } else
3546 *new_view = blame_view;
3547 return err;
3550 static const struct got_error *
3551 log_tree_entry(struct tog_view **new_view, int begin_x,
3552 struct got_tree_entry *te, struct tog_parent_trees *parents,
3553 struct got_object_id *commit_id, struct got_reflist_head *refs,
3554 struct got_repository *repo)
3556 struct tog_view *log_view;
3557 const struct got_error *err = NULL;
3558 char *path;
3560 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3561 if (log_view == NULL)
3562 return got_error_from_errno();
3564 err = tree_entry_path(&path, parents, te);
3565 if (err)
3566 return err;
3568 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3569 if (err)
3570 view_close(log_view);
3571 else
3572 *new_view = log_view;
3573 free(path);
3574 return err;
3577 static const struct got_error *
3578 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3579 struct got_object_id *commit_id, struct got_reflist_head *refs,
3580 struct got_repository *repo)
3582 const struct got_error *err = NULL;
3583 char *commit_id_str = NULL;
3584 struct tog_tree_view_state *s = &view->state.tree;
3586 TAILQ_INIT(&s->parents);
3588 err = got_object_id_str(&commit_id_str, commit_id);
3589 if (err != NULL)
3590 goto done;
3592 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3593 err = got_error_from_errno();
3594 goto done;
3597 s->root = s->tree = root;
3598 s->entries = got_object_tree_get_entries(root);
3599 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3600 s->commit_id = got_object_id_dup(commit_id);
3601 if (s->commit_id == NULL) {
3602 err = got_error_from_errno();
3603 goto done;
3605 s->refs = refs;
3606 s->repo = repo;
3608 view->show = show_tree_view;
3609 view->input = input_tree_view;
3610 view->close = close_tree_view;
3611 done:
3612 free(commit_id_str);
3613 if (err) {
3614 free(s->tree_label);
3615 s->tree_label = NULL;
3617 return err;
3620 static const struct got_error *
3621 close_tree_view(struct tog_view *view)
3623 struct tog_tree_view_state *s = &view->state.tree;
3625 free(s->tree_label);
3626 s->tree_label = NULL;
3627 free(s->commit_id);
3628 s->commit_id = NULL;
3629 while (!TAILQ_EMPTY(&s->parents)) {
3630 struct tog_parent_tree *parent;
3631 parent = TAILQ_FIRST(&s->parents);
3632 TAILQ_REMOVE(&s->parents, parent, entry);
3633 free(parent);
3636 if (s->tree != s->root)
3637 got_object_tree_close(s->tree);
3638 got_object_tree_close(s->root);
3640 return NULL;
3643 static const struct got_error *
3644 show_tree_view(struct tog_view *view)
3646 const struct got_error *err = NULL;
3647 struct tog_tree_view_state *s = &view->state.tree;
3648 char *parent_path;
3650 err = tree_entry_path(&parent_path, &s->parents, NULL);
3651 if (err)
3652 return err;
3654 err = draw_tree_entries(view, &s->first_displayed_entry,
3655 &s->last_displayed_entry, &s->selected_entry,
3656 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3657 s->entries, s->selected, view->nlines, s->tree == s->root);
3658 free(parent_path);
3660 view_vborder(view);
3661 return err;
3664 static const struct got_error *
3665 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3666 struct tog_view **focus_view, struct tog_view *view, int ch)
3668 const struct got_error *err = NULL;
3669 struct tog_tree_view_state *s = &view->state.tree;
3670 struct tog_view *log_view;
3671 int begin_x = 0, nscrolled;
3673 switch (ch) {
3674 case 'i':
3675 s->show_ids = !s->show_ids;
3676 break;
3677 case 'l':
3678 if (!s->selected_entry)
3679 break;
3680 if (view_is_parent_view(view))
3681 begin_x = view_split_begin_x(view->begin_x);
3682 err = log_tree_entry(&log_view, begin_x,
3683 s->selected_entry, &s->parents,
3684 s->commit_id, s->refs, s->repo);
3685 if (view_is_parent_view(view)) {
3686 err = view_close_child(view);
3687 if (err)
3688 return err;
3689 err = view_set_child(view, log_view);
3690 if (err) {
3691 view_close(log_view);
3692 break;
3694 *focus_view = log_view;
3695 view->child_focussed = 1;
3696 } else
3697 *new_view = log_view;
3698 break;
3699 case 'k':
3700 case KEY_UP:
3701 if (s->selected > 0) {
3702 s->selected--;
3703 if (s->selected == 0)
3704 break;
3706 if (s->selected > 0)
3707 break;
3708 tree_scroll_up(view, &s->first_displayed_entry, 1,
3709 s->entries, s->tree == s->root);
3710 break;
3711 case KEY_PPAGE:
3712 tree_scroll_up(view, &s->first_displayed_entry,
3713 MAX(0, view->nlines - 4 - s->selected), s->entries,
3714 s->tree == s->root);
3715 s->selected = 0;
3716 if (SIMPLEQ_FIRST(&s->entries->head) ==
3717 s->first_displayed_entry && s->tree != s->root)
3718 s->first_displayed_entry = NULL;
3719 break;
3720 case 'j':
3721 case KEY_DOWN:
3722 if (s->selected < s->ndisplayed - 1) {
3723 s->selected++;
3724 break;
3726 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3727 == NULL) {
3728 /* can't scroll any further */
3729 view_flash(view);
3730 break;
3732 tree_scroll_down(&s->first_displayed_entry, 1,
3733 s->last_displayed_entry, s->entries);
3734 break;
3735 case KEY_NPAGE:
3736 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3737 == NULL) {
3738 /* can't scroll any further; move cursor down */
3739 if (s->selected < s->ndisplayed - 1)
3740 s->selected = s->ndisplayed - 1;
3741 else
3742 view_flash(view);
3743 break;
3745 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3746 view->nlines, s->last_displayed_entry, s->entries);
3747 if (nscrolled < view->nlines) {
3748 int ndisplayed = 0;
3749 struct got_tree_entry *te;
3750 te = s->first_displayed_entry;
3751 do {
3752 ndisplayed++;
3753 te = SIMPLEQ_NEXT(te, entry);
3754 } while (te);
3755 s->selected = ndisplayed - 1;
3757 break;
3758 case KEY_ENTER:
3759 case '\r':
3760 if (s->selected_entry == NULL) {
3761 struct tog_parent_tree *parent;
3762 case KEY_BACKSPACE:
3763 /* user selected '..' */
3764 if (s->tree == s->root)
3765 break;
3766 parent = TAILQ_FIRST(&s->parents);
3767 TAILQ_REMOVE(&s->parents, parent,
3768 entry);
3769 got_object_tree_close(s->tree);
3770 s->tree = parent->tree;
3771 s->entries =
3772 got_object_tree_get_entries(s->tree);
3773 s->first_displayed_entry =
3774 parent->first_displayed_entry;
3775 s->selected_entry =
3776 parent->selected_entry;
3777 s->selected = parent->selected;
3778 free(parent);
3779 } else if (S_ISDIR(s->selected_entry->mode)) {
3780 struct tog_parent_tree *parent;
3781 struct got_tree_object *child;
3782 err = got_object_open_as_tree(&child,
3783 s->repo, s->selected_entry->id);
3784 if (err)
3785 break;
3786 parent = calloc(1, sizeof(*parent));
3787 if (parent == NULL) {
3788 err = got_error_from_errno();
3789 break;
3791 parent->tree = s->tree;
3792 parent->first_displayed_entry =
3793 s->first_displayed_entry;
3794 parent->selected_entry = s->selected_entry;
3795 parent->selected = s->selected;
3796 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3797 s->tree = child;
3798 s->entries =
3799 got_object_tree_get_entries(s->tree);
3800 s->selected = 0;
3801 s->first_displayed_entry = NULL;
3802 } else if (S_ISREG(s->selected_entry->mode)) {
3803 struct tog_view *blame_view;
3804 int begin_x = view_is_parent_view(view) ?
3805 view_split_begin_x(view->begin_x) : 0;
3807 err = blame_tree_entry(&blame_view, begin_x,
3808 s->selected_entry, &s->parents,
3809 s->commit_id, s->refs, s->repo);
3810 if (err)
3811 break;
3812 if (view_is_parent_view(view)) {
3813 err = view_close_child(view);
3814 if (err)
3815 return err;
3816 err = view_set_child(view, blame_view);
3817 if (err) {
3818 view_close(blame_view);
3819 break;
3821 *focus_view = blame_view;
3822 view->child_focussed = 1;
3823 } else
3824 *new_view = blame_view;
3826 break;
3827 case KEY_RESIZE:
3828 if (s->selected > view->nlines)
3829 s->selected = s->ndisplayed - 1;
3830 break;
3831 default:
3832 break;
3835 return err;
3838 __dead static void
3839 usage_tree(void)
3841 endwin();
3842 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3843 getprogname());
3844 exit(1);
3847 static const struct got_error *
3848 cmd_tree(int argc, char *argv[])
3850 const struct got_error *error;
3851 struct got_repository *repo = NULL;
3852 struct got_reflist_head refs;
3853 char *repo_path = NULL;
3854 struct got_object_id *commit_id = NULL;
3855 char *commit_id_arg = NULL;
3856 struct got_commit_object *commit = NULL;
3857 struct got_tree_object *tree = NULL;
3858 int ch;
3859 struct tog_view *view;
3861 SIMPLEQ_INIT(&refs);
3863 #ifndef PROFILE
3864 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3865 NULL) == -1)
3866 err(1, "pledge");
3867 #endif
3869 while ((ch = getopt(argc, argv, "c:")) != -1) {
3870 switch (ch) {
3871 case 'c':
3872 commit_id_arg = optarg;
3873 break;
3874 default:
3875 usage_tree();
3876 /* NOTREACHED */
3880 argc -= optind;
3881 argv += optind;
3883 if (argc == 0) {
3884 struct got_worktree *worktree;
3885 char *cwd = getcwd(NULL, 0);
3886 if (cwd == NULL)
3887 return got_error_from_errno();
3888 error = got_worktree_open(&worktree, cwd);
3889 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3890 goto done;
3891 if (worktree) {
3892 free(cwd);
3893 repo_path =
3894 strdup(got_worktree_get_repo_path(worktree));
3895 got_worktree_close(worktree);
3896 } else
3897 repo_path = cwd;
3898 if (repo_path == NULL) {
3899 error = got_error_from_errno();
3900 goto done;
3902 } else if (argc == 1) {
3903 repo_path = realpath(argv[0], NULL);
3904 if (repo_path == NULL)
3905 return got_error_from_errno();
3906 } else
3907 usage_log();
3909 init_curses();
3911 error = got_repo_open(&repo, repo_path);
3912 if (error != NULL)
3913 goto done;
3915 error = apply_unveil(got_repo_get_path(repo), NULL);
3916 if (error)
3917 goto done;
3919 if (commit_id_arg == NULL)
3920 error = get_head_commit_id(&commit_id, repo);
3921 else
3922 error = got_object_resolve_id_str(&commit_id, repo,
3923 commit_id_arg);
3924 if (error != NULL)
3925 goto done;
3927 error = got_object_open_as_commit(&commit, repo, commit_id);
3928 if (error != NULL)
3929 goto done;
3931 error = got_object_open_as_tree(&tree, repo,
3932 got_object_commit_get_tree_id(commit));
3933 if (error != NULL)
3934 goto done;
3936 error = got_ref_list(&refs, repo);
3937 if (error)
3938 goto done;
3940 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3941 if (view == NULL) {
3942 error = got_error_from_errno();
3943 goto done;
3945 error = open_tree_view(view, tree, commit_id, &refs, repo);
3946 if (error)
3947 goto done;
3948 error = view_loop(view);
3949 done:
3950 free(repo_path);
3951 free(commit_id);
3952 if (commit)
3953 got_object_commit_close(commit);
3954 if (tree)
3955 got_object_tree_close(tree);
3956 if (repo)
3957 got_repo_close(repo);
3958 got_ref_list_free(&refs);
3959 return error;
3962 __dead static void
3963 usage(void)
3965 int i;
3967 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3968 "Available commands:\n", getprogname());
3969 for (i = 0; i < nitems(tog_commands); i++) {
3970 struct tog_cmd *cmd = &tog_commands[i];
3971 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3973 exit(1);
3976 static char **
3977 make_argv(const char *arg0, const char *arg1)
3979 char **argv;
3980 int argc = (arg1 == NULL ? 1 : 2);
3982 argv = calloc(argc, sizeof(char *));
3983 if (argv == NULL)
3984 err(1, "calloc");
3985 argv[0] = strdup(arg0);
3986 if (argv[0] == NULL)
3987 err(1, "calloc");
3988 if (arg1) {
3989 argv[1] = strdup(arg1);
3990 if (argv[1] == NULL)
3991 err(1, "calloc");
3994 return argv;
3997 int
3998 main(int argc, char *argv[])
4000 const struct got_error *error = NULL;
4001 struct tog_cmd *cmd = NULL;
4002 int ch, hflag = 0;
4003 char **cmd_argv = NULL;
4005 setlocale(LC_CTYPE, "");
4007 while ((ch = getopt(argc, argv, "h")) != -1) {
4008 switch (ch) {
4009 case 'h':
4010 hflag = 1;
4011 break;
4012 default:
4013 usage();
4014 /* NOTREACHED */
4018 argc -= optind;
4019 argv += optind;
4020 optind = 0;
4021 optreset = 1;
4023 if (argc == 0) {
4024 if (hflag)
4025 usage();
4026 /* Build an argument vector which runs a default command. */
4027 cmd = &tog_commands[0];
4028 cmd_argv = make_argv(cmd->name, NULL);
4029 argc = 1;
4030 } else {
4031 int i;
4033 /* Did the user specific a command? */
4034 for (i = 0; i < nitems(tog_commands); i++) {
4035 if (strncmp(tog_commands[i].name, argv[0],
4036 strlen(argv[0])) == 0) {
4037 cmd = &tog_commands[i];
4038 if (hflag)
4039 tog_commands[i].cmd_usage();
4040 break;
4043 if (cmd == NULL) {
4044 /* Did the user specify a repository? */
4045 char *repo_path = realpath(argv[0], NULL);
4046 if (repo_path) {
4047 struct got_repository *repo;
4048 error = got_repo_open(&repo, repo_path);
4049 if (error == NULL)
4050 got_repo_close(repo);
4051 } else
4052 error = got_error_from_errno();
4053 if (error) {
4054 if (hflag) {
4055 fprintf(stderr, "%s: '%s' is not a "
4056 "known command\n", getprogname(),
4057 argv[0]);
4058 usage();
4060 fprintf(stderr, "%s: '%s' is neither a known "
4061 "command nor a path to a repository\n",
4062 getprogname(), argv[0]);
4063 free(repo_path);
4064 return 1;
4066 cmd = &tog_commands[0];
4067 cmd_argv = make_argv(cmd->name, repo_path);
4068 argc = 2;
4069 free(repo_path);
4073 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4074 if (error)
4075 goto done;
4076 done:
4077 endwin();
4078 free(cmd_argv);
4079 if (error)
4080 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4081 return 0;