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, "heads/", 6) == 0)
906 name += 6;
907 if (strncmp(name, "remotes/", 8) == 0)
908 name += 8;
909 s = *refs_str;
910 if (asprintf(refs_str, "%s%s%s", s ? s : "",
911 s ? ", " : "", name) == -1) {
912 err = got_error_from_errno();
913 free(s);
914 *refs_str = NULL;
915 break;
917 free(s);
920 return err;
923 static const struct got_error *
924 draw_commit(struct tog_view *view, struct got_commit_object *commit,
925 struct got_object_id *id, struct got_reflist_head *refs)
927 const struct got_error *err = NULL;
928 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
929 char *logmsg0 = NULL, *logmsg = NULL;
930 char *author0 = NULL, *author = NULL;
931 wchar_t *wlogmsg = NULL, *wauthor = NULL;
932 int author_width, logmsg_width;
933 char *newline, *smallerthan;
934 char *line = NULL;
935 int col, limit;
936 static const size_t date_display_cols = 9;
937 static const size_t author_display_cols = 16;
938 const int avail = view->ncols;
939 struct tm tm;
940 time_t committer_time;
942 committer_time = got_object_commit_get_committer_time(commit);
943 if (localtime_r(&committer_time, &tm) == NULL)
944 return got_error_from_errno();
945 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
946 >= sizeof(datebuf))
947 return got_error(GOT_ERR_NO_SPACE);
949 if (avail < date_display_cols)
950 limit = MIN(sizeof(datebuf) - 1, avail);
951 else
952 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
953 waddnstr(view->window, datebuf, limit);
954 col = limit + 1;
955 if (col > avail)
956 goto done;
958 author0 = strdup(got_object_commit_get_author(commit));
959 if (author0 == NULL) {
960 err = got_error_from_errno();
961 goto done;
963 author = author0;
964 smallerthan = strchr(author, '<');
965 if (smallerthan)
966 *smallerthan = '\0';
967 else {
968 char *at = strchr(author, '@');
969 if (at)
970 *at = '\0';
972 limit = avail - col;
973 err = format_line(&wauthor, &author_width, author, limit);
974 if (err)
975 goto done;
976 waddwstr(view->window, wauthor);
977 col += author_width;
978 while (col <= avail && author_width < author_display_cols + 1) {
979 waddch(view->window, ' ');
980 col++;
981 author_width++;
983 if (col > avail)
984 goto done;
986 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
987 if (logmsg0 == NULL) {
988 err = got_error_from_errno();
989 goto done;
991 logmsg = logmsg0;
992 while (*logmsg == '\n')
993 logmsg++;
994 newline = strchr(logmsg, '\n');
995 if (newline)
996 *newline = '\0';
997 limit = avail - col;
998 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
999 if (err)
1000 goto done;
1001 waddwstr(view->window, wlogmsg);
1002 col += logmsg_width;
1003 while (col <= avail) {
1004 waddch(view->window, ' ');
1005 col++;
1007 done:
1008 free(logmsg0);
1009 free(wlogmsg);
1010 free(author0);
1011 free(wauthor);
1012 free(line);
1013 return err;
1016 static struct commit_queue_entry *
1017 alloc_commit_queue_entry(struct got_commit_object *commit,
1018 struct got_object_id *id)
1020 struct commit_queue_entry *entry;
1022 entry = calloc(1, sizeof(*entry));
1023 if (entry == NULL)
1024 return NULL;
1026 entry->id = id;
1027 entry->commit = commit;
1028 return entry;
1031 static void
1032 pop_commit(struct commit_queue *commits)
1034 struct commit_queue_entry *entry;
1036 entry = TAILQ_FIRST(&commits->head);
1037 TAILQ_REMOVE(&commits->head, entry, entry);
1038 got_object_commit_close(entry->commit);
1039 commits->ncommits--;
1040 /* Don't free entry->id! It is owned by the commit graph. */
1041 free(entry);
1044 static void
1045 free_commits(struct commit_queue *commits)
1047 while (!TAILQ_EMPTY(&commits->head))
1048 pop_commit(commits);
1051 static const struct got_error *
1052 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1053 int minqueue, struct got_repository *repo, const char *path)
1055 const struct got_error *err = NULL;
1056 int nqueued = 0;
1059 * We keep all commits open throughout the lifetime of the log
1060 * view in order to avoid having to re-fetch commits from disk
1061 * while updating the display.
1063 while (nqueued < minqueue) {
1064 struct got_object_id *id;
1065 struct got_commit_object *commit;
1066 struct commit_queue_entry *entry;
1067 int errcode;
1069 err = got_commit_graph_iter_next(&id, graph);
1070 if (err) {
1071 if (err->code != GOT_ERR_ITER_NEED_MORE)
1072 break;
1073 err = got_commit_graph_fetch_commits(graph,
1074 minqueue, repo);
1075 if (err)
1076 return err;
1077 continue;
1080 if (id == NULL)
1081 break;
1083 err = got_object_open_as_commit(&commit, repo, id);
1084 if (err)
1085 break;
1086 entry = alloc_commit_queue_entry(commit, id);
1087 if (entry == NULL) {
1088 err = got_error_from_errno();
1089 break;
1092 errcode = pthread_mutex_lock(&tog_mutex);
1093 if (errcode) {
1094 err = got_error_set_errno(errcode);
1095 break;
1098 entry->idx = commits->ncommits;
1099 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1100 nqueued++;
1101 commits->ncommits++;
1103 errcode = pthread_mutex_unlock(&tog_mutex);
1104 if (errcode && err == NULL)
1105 err = got_error_set_errno(errcode);
1108 return err;
1111 static const struct got_error *
1112 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1114 const struct got_error *err = NULL;
1115 struct got_reference *head_ref;
1117 *head_id = NULL;
1119 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1120 if (err)
1121 return err;
1123 err = got_ref_resolve(head_id, repo, head_ref);
1124 got_ref_close(head_ref);
1125 if (err) {
1126 *head_id = NULL;
1127 return err;
1130 return NULL;
1133 static const struct got_error *
1134 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1135 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1136 struct commit_queue *commits, int selected_idx, int limit,
1137 struct got_reflist_head *refs, const char *path, int commits_needed)
1139 const struct got_error *err = NULL;
1140 struct commit_queue_entry *entry;
1141 int ncommits, width;
1142 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1143 char *refs_str = NULL;
1144 wchar_t *wline;
1146 entry = first;
1147 ncommits = 0;
1148 while (entry) {
1149 if (ncommits == selected_idx) {
1150 *selected = entry;
1151 break;
1153 entry = TAILQ_NEXT(entry, entry);
1154 ncommits++;
1157 if (*selected) {
1158 err = got_object_id_str(&id_str, (*selected)->id);
1159 if (err)
1160 return err;
1161 if (refs) {
1162 err = build_refs_str(&refs_str, refs, (*selected)->id);
1163 if (err)
1164 goto done;
1168 if (commits_needed == 0)
1169 halfdelay(10); /* disable fast refresh */
1171 if (asprintf(&ncommits_str, " [%d/%d] %s",
1172 entry ? entry->idx + 1 : 0, commits->ncommits,
1173 commits_needed > 0 ? "loading... " :
1174 (refs_str ? refs_str : "")) == -1) {
1175 err = got_error_from_errno();
1176 goto done;
1179 if (path && strcmp(path, "/") != 0) {
1180 if (asprintf(&header, "commit %s %s%s",
1181 id_str ? id_str : "........................................",
1182 path, ncommits_str) == -1) {
1183 err = got_error_from_errno();
1184 header = NULL;
1185 goto done;
1187 } else if (asprintf(&header, "commit %s%s",
1188 id_str ? id_str : "........................................",
1189 ncommits_str) == -1) {
1190 err = got_error_from_errno();
1191 header = NULL;
1192 goto done;
1194 err = format_line(&wline, &width, header, view->ncols);
1195 if (err)
1196 goto done;
1198 werase(view->window);
1200 if (view_needs_focus_indication(view))
1201 wstandout(view->window);
1202 waddwstr(view->window, wline);
1203 while (width < view->ncols) {
1204 waddch(view->window, ' ');
1205 width++;
1207 if (view_needs_focus_indication(view))
1208 wstandend(view->window);
1209 free(wline);
1210 if (limit <= 1)
1211 goto done;
1213 entry = first;
1214 *last = first;
1215 ncommits = 0;
1216 while (entry) {
1217 if (ncommits >= limit - 1)
1218 break;
1219 if (view->focussed && ncommits == selected_idx)
1220 wstandout(view->window);
1221 err = draw_commit(view, entry->commit, entry->id, refs);
1222 if (view->focussed && ncommits == selected_idx)
1223 wstandend(view->window);
1224 if (err)
1225 break;
1226 ncommits++;
1227 *last = entry;
1228 entry = TAILQ_NEXT(entry, entry);
1231 view_vborder(view);
1232 done:
1233 free(id_str);
1234 free(refs_str);
1235 free(ncommits_str);
1236 free(header);
1237 return err;
1240 static void
1241 scroll_up(struct tog_view *view,
1242 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1243 struct commit_queue *commits)
1245 struct commit_queue_entry *entry;
1246 int nscrolled = 0;
1248 entry = TAILQ_FIRST(&commits->head);
1249 if (*first_displayed_entry == entry) {
1250 view_flash(view);
1251 return;
1254 entry = *first_displayed_entry;
1255 while (entry && nscrolled < maxscroll) {
1256 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1257 if (entry) {
1258 *first_displayed_entry = entry;
1259 nscrolled++;
1264 static const struct got_error *
1265 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1266 pthread_cond_t *need_commits)
1268 int errcode;
1269 int max_wait = 20;
1271 halfdelay(1); /* fast refresh while loading commits */
1273 while (*commits_needed > 0) {
1274 if (*log_complete)
1275 break;
1277 /* Wake the log thread. */
1278 errcode = pthread_cond_signal(need_commits);
1279 if (errcode)
1280 return got_error_set_errno(errcode);
1281 errcode = pthread_mutex_unlock(&tog_mutex);
1282 if (errcode)
1283 return got_error_set_errno(errcode);
1284 pthread_yield();
1285 errcode = pthread_mutex_lock(&tog_mutex);
1286 if (errcode)
1287 return got_error_set_errno(errcode);
1289 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1291 * Thread is not done yet; lose a key press
1292 * and let the user retry... this way the GUI
1293 * remains interactive while logging deep paths
1294 * with few commits in history.
1296 return NULL;
1300 return NULL;
1303 static const struct got_error *
1304 scroll_down(struct tog_view *view,
1305 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1306 struct commit_queue_entry **last_displayed_entry,
1307 struct commit_queue *commits, int *log_complete, int *commits_needed,
1308 pthread_cond_t *need_commits)
1310 const struct got_error *err = NULL;
1311 struct commit_queue_entry *pentry;
1312 int nscrolled = 0;
1314 if (*last_displayed_entry == NULL)
1315 return NULL;
1317 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1318 if (pentry == NULL && !*log_complete) {
1320 * Ask the log thread for required amount of commits
1321 * plus some amount of pre-fetching.
1323 (*commits_needed) += maxscroll + 20;
1324 err = trigger_log_thread(0, commits_needed, log_complete,
1325 need_commits);
1326 if (err)
1327 return err;
1330 do {
1331 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1332 if (pentry == NULL) {
1333 if (*log_complete)
1334 view_flash(view);
1335 break;
1338 *last_displayed_entry = pentry;
1340 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1341 if (pentry == NULL)
1342 break;
1343 *first_displayed_entry = pentry;
1344 } while (++nscrolled < maxscroll);
1346 return err;
1349 static const struct got_error *
1350 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1351 struct got_commit_object *commit, struct got_object_id *commit_id,
1352 struct tog_view *log_view, struct got_reflist_head *refs,
1353 struct got_repository *repo)
1355 const struct got_error *err;
1356 struct got_object_qid *parent_id;
1357 struct tog_view *diff_view;
1359 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1360 if (diff_view == NULL)
1361 return got_error_from_errno();
1363 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1364 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1365 commit_id, log_view, refs, repo);
1366 if (err == NULL)
1367 *new_view = diff_view;
1368 return err;
1371 static const struct got_error *
1372 browse_commit(struct tog_view **new_view, int begin_x,
1373 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1374 struct got_repository *repo)
1376 const struct got_error *err = NULL;
1377 struct got_tree_object *tree;
1378 struct tog_view *tree_view;
1380 err = got_object_open_as_tree(&tree, repo,
1381 got_object_commit_get_tree_id(entry->commit));
1382 if (err)
1383 return err;
1385 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1386 if (tree_view == NULL)
1387 return got_error_from_errno();
1389 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1390 if (err)
1391 got_object_tree_close(tree);
1392 else
1393 *new_view = tree_view;
1394 return err;
1397 static void *
1398 log_thread(void *arg)
1400 const struct got_error *err = NULL;
1401 int errcode = 0;
1402 struct tog_log_thread_args *a = arg;
1403 int done = 0;
1405 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1406 if (err)
1407 return (void *)err;
1409 while (!done && !err) {
1410 err = queue_commits(a->graph, a->commits, 1, a->repo,
1411 a->in_repo_path);
1412 if (err) {
1413 if (err->code != GOT_ERR_ITER_COMPLETED)
1414 return (void *)err;
1415 err = NULL;
1416 done = 1;
1417 } else if (a->commits_needed > 0)
1418 a->commits_needed--;
1420 errcode = pthread_mutex_lock(&tog_mutex);
1421 if (errcode)
1422 return (void *)got_error_set_errno(errcode);
1424 if (done)
1425 a->log_complete = 1;
1426 else if (*a->quit) {
1427 done = 1;
1428 a->log_complete = 1;
1429 } else if (*a->first_displayed_entry == NULL) {
1430 *a->first_displayed_entry =
1431 TAILQ_FIRST(&a->commits->head);
1432 *a->selected_entry = *a->first_displayed_entry;
1435 if (done)
1436 a->commits_needed = 0;
1437 else if (a->commits_needed == 0) {
1438 errcode = pthread_cond_wait(&a->need_commits,
1439 &tog_mutex);
1440 if (errcode)
1441 err = got_error_set_errno(errcode);
1444 errcode = pthread_mutex_unlock(&tog_mutex);
1445 if (errcode && err == NULL)
1446 err = got_error_set_errno(errcode);
1448 return (void *)err;
1451 static const struct got_error *
1452 stop_log_thread(struct tog_log_view_state *s)
1454 const struct got_error *err = NULL;
1455 int errcode;
1457 if (s->thread) {
1458 s->quit = 1;
1459 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1460 if (errcode)
1461 return got_error_set_errno(errcode);
1462 errcode = pthread_mutex_unlock(&tog_mutex);
1463 if (errcode)
1464 return got_error_set_errno(errcode);
1465 errcode = pthread_join(s->thread, (void **)&err);
1466 if (errcode)
1467 return got_error_set_errno(errcode);
1468 errcode = pthread_mutex_lock(&tog_mutex);
1469 if (errcode)
1470 return got_error_set_errno(errcode);
1471 s->thread = NULL;
1474 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1475 if (errcode && err == NULL)
1476 err = got_error_set_errno(errcode);
1478 if (s->thread_args.repo) {
1479 got_repo_close(s->thread_args.repo);
1480 s->thread_args.repo = NULL;
1483 if (s->thread_args.graph) {
1484 got_commit_graph_close(s->thread_args.graph);
1485 s->thread_args.graph = NULL;
1488 return err;
1491 static const struct got_error *
1492 close_log_view(struct tog_view *view)
1494 const struct got_error *err = NULL;
1495 struct tog_log_view_state *s = &view->state.log;
1497 err = stop_log_thread(s);
1498 free_commits(&s->commits);
1499 free(s->in_repo_path);
1500 s->in_repo_path = NULL;
1501 free(s->start_id);
1502 s->start_id = NULL;
1503 return err;
1506 static const struct got_error *
1507 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1508 struct got_reflist_head *refs, struct got_repository *repo,
1509 const char *path, int check_disk)
1511 const struct got_error *err = NULL;
1512 struct tog_log_view_state *s = &view->state.log;
1513 struct got_repository *thread_repo = NULL;
1514 struct got_commit_graph *thread_graph = NULL;
1515 int errcode;
1517 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1518 if (err != NULL)
1519 goto done;
1521 /* The commit queue only contains commits being displayed. */
1522 TAILQ_INIT(&s->commits.head);
1523 s->commits.ncommits = 0;
1525 s->refs = refs;
1526 s->repo = repo;
1527 s->start_id = got_object_id_dup(start_id);
1528 if (s->start_id == NULL) {
1529 err = got_error_from_errno();
1530 goto done;
1533 view->show = show_log_view;
1534 view->input = input_log_view;
1535 view->close = close_log_view;
1537 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1538 if (err)
1539 goto done;
1540 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1541 0, thread_repo);
1542 if (err)
1543 goto done;
1545 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1546 if (errcode) {
1547 err = got_error_set_errno(errcode);
1548 goto done;
1551 s->thread_args.commits_needed = view->nlines;
1552 s->thread_args.graph = thread_graph;
1553 s->thread_args.commits = &s->commits;
1554 s->thread_args.in_repo_path = s->in_repo_path;
1555 s->thread_args.start_id = s->start_id;
1556 s->thread_args.repo = thread_repo;
1557 s->thread_args.log_complete = 0;
1558 s->thread_args.quit = &s->quit;
1559 s->thread_args.view = view;
1560 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1561 s->thread_args.selected_entry = &s->selected_entry;
1562 done:
1563 if (err)
1564 close_log_view(view);
1565 return err;
1568 static const struct got_error *
1569 show_log_view(struct tog_view *view)
1571 struct tog_log_view_state *s = &view->state.log;
1573 if (s->thread == NULL) {
1574 int errcode = pthread_create(&s->thread, NULL, log_thread,
1575 &s->thread_args);
1576 if (errcode)
1577 return got_error_set_errno(errcode);
1580 return draw_commits(view, &s->last_displayed_entry,
1581 &s->selected_entry, s->first_displayed_entry,
1582 &s->commits, s->selected, view->nlines, s->refs,
1583 s->in_repo_path, s->thread_args.commits_needed);
1586 static const struct got_error *
1587 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1588 struct tog_view **focus_view, struct tog_view *view, int ch)
1590 const struct got_error *err = NULL;
1591 struct tog_log_view_state *s = &view->state.log;
1592 char *parent_path;
1593 struct tog_view *diff_view = NULL, *tree_view = NULL;
1594 int begin_x = 0;
1596 switch (ch) {
1597 case 'q':
1598 s->quit = 1;
1599 break;
1600 case 'k':
1601 case KEY_UP:
1602 case '<':
1603 case ',':
1604 if (s->first_displayed_entry == NULL)
1605 break;
1606 if (s->selected > 0)
1607 s->selected--;
1608 if (s->selected > 0)
1609 break;
1610 scroll_up(view, &s->first_displayed_entry, 1,
1611 &s->commits);
1612 break;
1613 case KEY_PPAGE:
1614 if (s->first_displayed_entry == NULL)
1615 break;
1616 if (TAILQ_FIRST(&s->commits.head) ==
1617 s->first_displayed_entry) {
1618 if (s->selected == 0) {
1619 view_flash(view);
1620 break;
1622 s->selected = 0;
1623 break;
1625 scroll_up(view, &s->first_displayed_entry,
1626 view->nlines, &s->commits);
1627 break;
1628 case 'j':
1629 case KEY_DOWN:
1630 case '>':
1631 case '.':
1632 if (s->first_displayed_entry == NULL)
1633 break;
1634 if (s->selected < MIN(view->nlines - 2,
1635 s->commits.ncommits - 1)) {
1636 s->selected++;
1637 break;
1639 err = scroll_down(view, &s->first_displayed_entry, 1,
1640 &s->last_displayed_entry, &s->commits,
1641 &s->thread_args.log_complete,
1642 &s->thread_args.commits_needed,
1643 &s->thread_args.need_commits);
1644 break;
1645 case KEY_NPAGE: {
1646 struct commit_queue_entry *first;
1647 first = s->first_displayed_entry;
1648 if (first == NULL)
1649 break;
1650 err = scroll_down(view, &s->first_displayed_entry,
1651 view->nlines, &s->last_displayed_entry,
1652 &s->commits, &s->thread_args.log_complete,
1653 &s->thread_args.commits_needed,
1654 &s->thread_args.need_commits);
1655 if (first == s->first_displayed_entry &&
1656 s->selected < MIN(view->nlines - 2,
1657 s->commits.ncommits - 1)) {
1658 /* can't scroll further down */
1659 s->selected = MIN(view->nlines - 2,
1660 s->commits.ncommits - 1);
1662 err = NULL;
1663 break;
1665 case KEY_RESIZE:
1666 if (s->selected > view->nlines - 2)
1667 s->selected = view->nlines - 2;
1668 if (s->selected > s->commits.ncommits - 1)
1669 s->selected = s->commits.ncommits - 1;
1670 break;
1671 case KEY_ENTER:
1672 case '\r':
1673 if (s->selected_entry == NULL)
1674 break;
1675 if (view_is_parent_view(view))
1676 begin_x = view_split_begin_x(view->begin_x);
1677 err = open_diff_view_for_commit(&diff_view, begin_x,
1678 s->selected_entry->commit, s->selected_entry->id,
1679 view, s->refs, s->repo);
1680 if (err)
1681 break;
1682 if (view_is_parent_view(view)) {
1683 err = view_close_child(view);
1684 if (err)
1685 return err;
1686 err = view_set_child(view, diff_view);
1687 if (err) {
1688 view_close(diff_view);
1689 break;
1691 *focus_view = diff_view;
1692 view->child_focussed = 1;
1693 } else
1694 *new_view = diff_view;
1695 break;
1696 case 't':
1697 if (s->selected_entry == NULL)
1698 break;
1699 if (view_is_parent_view(view))
1700 begin_x = view_split_begin_x(view->begin_x);
1701 err = browse_commit(&tree_view, begin_x,
1702 s->selected_entry, s->refs, s->repo);
1703 if (err)
1704 break;
1705 if (view_is_parent_view(view)) {
1706 err = view_close_child(view);
1707 if (err)
1708 return err;
1709 err = view_set_child(view, tree_view);
1710 if (err) {
1711 view_close(tree_view);
1712 break;
1714 *focus_view = tree_view;
1715 view->child_focussed = 1;
1716 } else
1717 *new_view = tree_view;
1718 break;
1719 case KEY_BACKSPACE:
1720 if (strcmp(s->in_repo_path, "/") == 0)
1721 break;
1722 parent_path = dirname(s->in_repo_path);
1723 if (parent_path && strcmp(parent_path, ".") != 0) {
1724 struct tog_view *lv;
1725 err = stop_log_thread(s);
1726 if (err)
1727 return err;
1728 lv = view_open(view->nlines, view->ncols,
1729 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1730 if (lv == NULL)
1731 return got_error_from_errno();
1732 err = open_log_view(lv, s->start_id, s->refs,
1733 s->repo, parent_path, 0);
1734 if (err)
1735 return err;;
1736 if (view_is_parent_view(view))
1737 *new_view = lv;
1738 else {
1739 view_set_child(view->parent, lv);
1740 *focus_view = lv;
1742 return NULL;
1744 break;
1745 default:
1746 break;
1749 return err;
1752 static const struct got_error *
1753 apply_unveil(const char *repo_path, const char *worktree_path)
1755 const struct got_error *error;
1757 if (repo_path && unveil(repo_path, "r") != 0)
1758 return got_error_from_errno();
1760 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1761 return got_error_from_errno();
1763 if (unveil("/tmp", "rwc") != 0)
1764 return got_error_from_errno();
1766 error = got_privsep_unveil_exec_helpers();
1767 if (error != NULL)
1768 return error;
1770 if (unveil(NULL, NULL) != 0)
1771 return got_error_from_errno();
1773 return NULL;
1776 static void
1777 init_curses(void)
1779 initscr();
1780 cbreak();
1781 halfdelay(1); /* Do fast refresh while initial view is loading. */
1782 noecho();
1783 nonl();
1784 intrflush(stdscr, FALSE);
1785 keypad(stdscr, TRUE);
1786 curs_set(0);
1787 signal(SIGWINCH, tog_sigwinch);
1790 static const struct got_error *
1791 cmd_log(int argc, char *argv[])
1793 const struct got_error *error;
1794 struct got_repository *repo = NULL;
1795 struct got_reflist_head refs;
1796 struct got_object_id *start_id = NULL;
1797 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1798 char *start_commit = NULL;
1799 int ch;
1800 struct tog_view *view;
1802 #ifndef PROFILE
1803 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1804 NULL) == -1)
1805 err(1, "pledge");
1806 #endif
1808 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1809 switch (ch) {
1810 case 'c':
1811 start_commit = optarg;
1812 break;
1813 case 'r':
1814 repo_path = realpath(optarg, NULL);
1815 if (repo_path == NULL)
1816 err(1, "-r option");
1817 break;
1818 default:
1819 usage();
1820 /* NOTREACHED */
1824 argc -= optind;
1825 argv += optind;
1827 if (argc == 0)
1828 path = strdup("");
1829 else if (argc == 1)
1830 path = strdup(argv[0]);
1831 else
1832 usage_log();
1833 if (path == NULL)
1834 return got_error_from_errno();
1836 cwd = getcwd(NULL, 0);
1837 if (cwd == NULL) {
1838 error = got_error_from_errno();
1839 goto done;
1841 if (repo_path == NULL) {
1842 struct got_worktree *worktree;
1843 error = got_worktree_open(&worktree, cwd);
1844 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1845 goto done;
1846 if (worktree) {
1847 repo_path =
1848 strdup(got_worktree_get_repo_path(worktree));
1849 got_worktree_close(worktree);
1850 } else
1851 repo_path = strdup(cwd);
1852 if (repo_path == NULL) {
1853 error = got_error_from_errno();
1854 goto done;
1858 init_curses();
1860 error = apply_unveil(repo_path, NULL);
1861 if (error)
1862 goto done;
1864 error = got_repo_open(&repo, repo_path);
1865 if (error != NULL)
1866 goto done;
1868 if (start_commit == NULL)
1869 error = get_head_commit_id(&start_id, repo);
1870 else
1871 error = got_object_resolve_id_str(&start_id, repo,
1872 start_commit);
1873 if (error != NULL)
1874 goto done;
1876 SIMPLEQ_INIT(&refs);
1877 error = got_ref_list(&refs, repo);
1878 if (error)
1879 goto done;
1881 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1882 if (view == NULL) {
1883 error = got_error_from_errno();
1884 goto done;
1886 error = open_log_view(view, start_id, &refs, repo, path, 1);
1887 if (error)
1888 goto done;
1889 error = view_loop(view);
1890 done:
1891 free(repo_path);
1892 free(cwd);
1893 free(path);
1894 free(start_id);
1895 if (repo)
1896 got_repo_close(repo);
1897 return error;
1900 __dead static void
1901 usage_diff(void)
1903 endwin();
1904 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1905 getprogname());
1906 exit(1);
1909 static char *
1910 parse_next_line(FILE *f, size_t *len)
1912 char *line;
1913 size_t linelen;
1914 size_t lineno;
1915 const char delim[3] = { '\0', '\0', '\0'};
1917 line = fparseln(f, &linelen, &lineno, delim, 0);
1918 if (len)
1919 *len = linelen;
1920 return line;
1923 static const struct got_error *
1924 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1925 int *last_displayed_line, int *eof, int max_lines,
1926 char * header)
1928 const struct got_error *err;
1929 int nlines = 0, nprinted = 0;
1930 char *line;
1931 size_t len;
1932 wchar_t *wline;
1933 int width;
1935 rewind(f);
1936 werase(view->window);
1938 if (header) {
1939 err = format_line(&wline, &width, header, view->ncols);
1940 if (err) {
1941 return err;
1944 if (view_needs_focus_indication(view))
1945 wstandout(view->window);
1946 waddwstr(view->window, wline);
1947 if (view_needs_focus_indication(view))
1948 wstandend(view->window);
1949 if (width < view->ncols)
1950 waddch(view->window, '\n');
1952 if (max_lines <= 1)
1953 return NULL;
1954 max_lines--;
1957 *eof = 0;
1958 while (nprinted < max_lines) {
1959 line = parse_next_line(f, &len);
1960 if (line == NULL) {
1961 *eof = 1;
1962 break;
1964 if (++nlines < *first_displayed_line) {
1965 free(line);
1966 continue;
1969 err = format_line(&wline, &width, line, view->ncols);
1970 if (err) {
1971 free(line);
1972 return err;
1974 waddwstr(view->window, wline);
1975 if (width < view->ncols)
1976 waddch(view->window, '\n');
1977 if (++nprinted == 1)
1978 *first_displayed_line = nlines;
1979 free(line);
1980 free(wline);
1981 wline = NULL;
1983 *last_displayed_line = nlines;
1985 view_vborder(view);
1987 return NULL;
1990 static char *
1991 get_datestr(time_t *time, char *datebuf)
1993 char *p, *s = ctime_r(time, datebuf);
1994 p = strchr(s, '\n');
1995 if (p)
1996 *p = '\0';
1997 return s;
2000 static const struct got_error *
2001 write_commit_info(struct got_object_id *commit_id,
2002 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2004 const struct got_error *err = NULL;
2005 char datebuf[26];
2006 struct got_commit_object *commit;
2007 char *id_str = NULL;
2008 time_t committer_time;
2009 const char *author, *committer;
2010 char *refs_str = NULL;
2012 if (refs) {
2013 err = build_refs_str(&refs_str, refs, commit_id);
2014 if (err)
2015 return err;
2018 err = got_object_open_as_commit(&commit, repo, commit_id);
2019 if (err)
2020 return err;
2022 err = got_object_id_str(&id_str, commit_id);
2023 if (err) {
2024 err = got_error_from_errno();
2025 goto done;
2028 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2029 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2030 err = got_error_from_errno();
2031 goto done;
2033 if (fprintf(outfile, "from: %s\n",
2034 got_object_commit_get_author(commit)) < 0) {
2035 err = got_error_from_errno();
2036 goto done;
2038 committer_time = got_object_commit_get_committer_time(commit);
2039 if (fprintf(outfile, "date: %s UTC\n",
2040 get_datestr(&committer_time, datebuf)) < 0) {
2041 err = got_error_from_errno();
2042 goto done;
2044 author = got_object_commit_get_author(commit);
2045 committer = got_object_commit_get_committer(commit);
2046 if (strcmp(author, committer) != 0 &&
2047 fprintf(outfile, "via: %s\n", committer) < 0) {
2048 err = got_error_from_errno();
2049 goto done;
2051 if (fprintf(outfile, "%s\n",
2052 got_object_commit_get_logmsg(commit)) < 0) {
2053 err = got_error_from_errno();
2054 goto done;
2056 done:
2057 free(id_str);
2058 free(refs_str);
2059 got_object_commit_close(commit);
2060 return err;
2063 static const struct got_error *
2064 create_diff(struct tog_diff_view_state *s)
2066 const struct got_error *err = NULL;
2067 FILE *f = NULL;
2068 int obj_type;
2070 f = got_opentemp();
2071 if (f == NULL) {
2072 err = got_error_from_errno();
2073 goto done;
2075 if (s->f && fclose(s->f) != 0) {
2076 err = got_error_from_errno();
2077 goto done;
2079 s->f = f;
2081 if (s->id1)
2082 err = got_object_get_type(&obj_type, s->repo, s->id1);
2083 else
2084 err = got_object_get_type(&obj_type, s->repo, s->id2);
2085 if (err)
2086 goto done;
2088 switch (obj_type) {
2089 case GOT_OBJ_TYPE_BLOB:
2090 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2091 s->diff_context, s->repo, f);
2092 break;
2093 case GOT_OBJ_TYPE_TREE:
2094 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2095 s->diff_context, s->repo, f);
2096 break;
2097 case GOT_OBJ_TYPE_COMMIT: {
2098 const struct got_object_id_queue *parent_ids;
2099 struct got_object_qid *pid;
2100 struct got_commit_object *commit2;
2102 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2103 if (err)
2104 break;
2105 /* Show commit info if we're diffing to a parent/root commit. */
2106 if (s->id1 == NULL)
2107 write_commit_info(s->id2, s->refs, s->repo, f);
2108 else {
2109 parent_ids = got_object_commit_get_parent_ids(commit2);
2110 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2111 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2112 write_commit_info(s->id2, s->refs,
2113 s->repo, f);
2114 break;
2118 got_object_commit_close(commit2);
2120 err = got_diff_objects_as_commits(s->id1, s->id2,
2121 s->diff_context, s->repo, f);
2122 break;
2124 default:
2125 err = got_error(GOT_ERR_OBJ_TYPE);
2126 break;
2128 done:
2129 if (f && fflush(f) != 0 && err == NULL)
2130 err = got_error_from_errno();
2131 return err;
2134 static void
2135 diff_view_indicate_progress(struct tog_view *view)
2137 werase(view->window);
2138 waddstr(view->window, "diffing...");
2139 update_panels();
2140 doupdate();
2143 static const struct got_error *
2144 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2145 struct got_object_id *id2, struct tog_view *log_view,
2146 struct got_reflist_head *refs, struct got_repository *repo)
2148 const struct got_error *err;
2150 if (id1 != NULL && id2 != NULL) {
2151 int type1, type2;
2152 err = got_object_get_type(&type1, repo, id1);
2153 if (err)
2154 return err;
2155 err = got_object_get_type(&type2, repo, id2);
2156 if (err)
2157 return err;
2159 if (type1 != type2)
2160 return got_error(GOT_ERR_OBJ_TYPE);
2163 if (id1) {
2164 view->state.diff.id1 = got_object_id_dup(id1);
2165 if (view->state.diff.id1 == NULL)
2166 return got_error_from_errno();
2167 } else
2168 view->state.diff.id1 = NULL;
2170 view->state.diff.id2 = got_object_id_dup(id2);
2171 if (view->state.diff.id2 == NULL) {
2172 free(view->state.diff.id1);
2173 view->state.diff.id1 = NULL;
2174 return got_error_from_errno();
2176 view->state.diff.f = NULL;
2177 view->state.diff.first_displayed_line = 1;
2178 view->state.diff.last_displayed_line = view->nlines;
2179 view->state.diff.diff_context = 3;
2180 view->state.diff.log_view = log_view;
2181 view->state.diff.repo = repo;
2182 view->state.diff.refs = refs;
2184 if (log_view && view_is_splitscreen(view))
2185 show_log_view(log_view); /* draw vborder */
2186 diff_view_indicate_progress(view);
2188 err = create_diff(&view->state.diff);
2189 if (err) {
2190 free(view->state.diff.id1);
2191 view->state.diff.id1 = NULL;
2192 free(view->state.diff.id2);
2193 view->state.diff.id2 = NULL;
2194 return err;
2197 view->show = show_diff_view;
2198 view->input = input_diff_view;
2199 view->close = close_diff_view;
2201 return NULL;
2204 static const struct got_error *
2205 close_diff_view(struct tog_view *view)
2207 const struct got_error *err = NULL;
2209 free(view->state.diff.id1);
2210 view->state.diff.id1 = NULL;
2211 free(view->state.diff.id2);
2212 view->state.diff.id2 = NULL;
2213 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2214 err = got_error_from_errno();
2215 return err;
2218 static const struct got_error *
2219 show_diff_view(struct tog_view *view)
2221 const struct got_error *err;
2222 struct tog_diff_view_state *s = &view->state.diff;
2223 char *id_str1 = NULL, *id_str2, *header;
2225 if (s->id1) {
2226 err = got_object_id_str(&id_str1, s->id1);
2227 if (err)
2228 return err;
2230 err = got_object_id_str(&id_str2, s->id2);
2231 if (err)
2232 return err;
2234 if (asprintf(&header, "diff %s %s",
2235 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2236 err = got_error_from_errno();
2237 free(id_str1);
2238 free(id_str2);
2239 return err;
2241 free(id_str1);
2242 free(id_str2);
2244 return draw_file(view, s->f, &s->first_displayed_line,
2245 &s->last_displayed_line, &s->eof, view->nlines,
2246 header);
2249 static const struct got_error *
2250 set_selected_commit(struct tog_diff_view_state *s,
2251 struct commit_queue_entry *entry)
2253 const struct got_error *err;
2254 const struct got_object_id_queue *parent_ids;
2255 struct got_commit_object *selected_commit;
2256 struct got_object_qid *pid;
2258 free(s->id2);
2259 s->id2 = got_object_id_dup(entry->id);
2260 if (s->id2 == NULL)
2261 return got_error_from_errno();
2263 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2264 if (err)
2265 return err;
2266 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2267 free(s->id1);
2268 pid = SIMPLEQ_FIRST(parent_ids);
2269 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2270 got_object_commit_close(selected_commit);
2271 return NULL;
2274 static const struct got_error *
2275 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2276 struct tog_view **focus_view, struct tog_view *view, int ch)
2278 const struct got_error *err = NULL;
2279 struct tog_diff_view_state *s = &view->state.diff;
2280 struct tog_log_view_state *ls;
2281 struct commit_queue_entry *entry;
2282 int i;
2284 switch (ch) {
2285 case 'k':
2286 case KEY_UP:
2287 if (s->first_displayed_line > 1)
2288 s->first_displayed_line--;
2289 else
2290 view_flash(view);
2291 break;
2292 case KEY_PPAGE:
2293 if (s->first_displayed_line == 1) {
2294 view_flash(view);
2295 break;
2297 i = 0;
2298 while (i++ < view->nlines - 1 &&
2299 s->first_displayed_line > 1)
2300 s->first_displayed_line--;
2301 break;
2302 case 'j':
2303 case KEY_DOWN:
2304 if (!s->eof)
2305 s->first_displayed_line++;
2306 else
2307 view_flash(view);
2308 break;
2309 case KEY_NPAGE:
2310 case ' ':
2311 if (s->eof) {
2312 view_flash(view);
2313 break;
2315 i = 0;
2316 while (!s->eof && i++ < view->nlines - 1) {
2317 char *line;
2318 line = parse_next_line(s->f, NULL);
2319 s->first_displayed_line++;
2320 if (line == NULL)
2321 break;
2323 break;
2324 case '[':
2325 if (s->diff_context > 0) {
2326 s->diff_context--;
2327 diff_view_indicate_progress(view);
2328 err = create_diff(s);
2330 break;
2331 case ']':
2332 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2333 s->diff_context++;
2334 diff_view_indicate_progress(view);
2335 err = create_diff(s);
2337 break;
2338 case '<':
2339 case ',':
2340 if (s->log_view == NULL)
2341 break;
2342 ls = &s->log_view->state.log;
2343 entry = TAILQ_PREV(ls->selected_entry,
2344 commit_queue_head, entry);
2345 if (entry == NULL)
2346 break;
2348 err = input_log_view(NULL, NULL, NULL, s->log_view,
2349 KEY_UP);
2350 if (err)
2351 break;
2353 err = set_selected_commit(s, entry);
2354 if (err)
2355 break;
2357 s->first_displayed_line = 1;
2358 s->last_displayed_line = view->nlines;
2360 diff_view_indicate_progress(view);
2361 err = create_diff(s);
2362 break;
2363 case '>':
2364 case '.':
2365 if (s->log_view == NULL)
2366 break;
2367 ls = &s->log_view->state.log;
2369 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2370 ls->thread_args.commits_needed++;
2372 /* Display "loading..." in log view. */
2373 show_log_view(s->log_view);
2374 update_panels();
2375 doupdate();
2377 err = trigger_log_thread(1 /* load_all */,
2378 &ls->thread_args.commits_needed,
2379 &ls->thread_args.log_complete,
2380 &ls->thread_args.need_commits);
2381 if (err)
2382 break;
2384 err = input_log_view(NULL, NULL, NULL, s->log_view,
2385 KEY_DOWN);
2386 if (err)
2387 break;
2389 entry = TAILQ_NEXT(ls->selected_entry, entry);
2390 if (entry == NULL)
2391 break;
2393 err = set_selected_commit(s, entry);
2394 if (err)
2395 break;
2397 s->first_displayed_line = 1;
2398 s->last_displayed_line = view->nlines;
2400 diff_view_indicate_progress(view);
2401 err = create_diff(s);
2402 break;
2403 default:
2404 break;
2407 return err;
2410 static const struct got_error *
2411 cmd_diff(int argc, char *argv[])
2413 const struct got_error *error = NULL;
2414 struct got_repository *repo = NULL;
2415 struct got_reflist_head refs;
2416 struct got_object_id *id1 = NULL, *id2 = NULL;
2417 char *repo_path = NULL;
2418 char *id_str1 = NULL, *id_str2 = NULL;
2419 int ch;
2420 struct tog_view *view;
2422 #ifndef PROFILE
2423 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2424 NULL) == -1)
2425 err(1, "pledge");
2426 #endif
2428 while ((ch = getopt(argc, argv, "")) != -1) {
2429 switch (ch) {
2430 default:
2431 usage();
2432 /* NOTREACHED */
2436 argc -= optind;
2437 argv += optind;
2439 if (argc == 0) {
2440 usage_diff(); /* TODO show local worktree changes */
2441 } else if (argc == 2) {
2442 repo_path = getcwd(NULL, 0);
2443 if (repo_path == NULL)
2444 return got_error_from_errno();
2445 id_str1 = argv[0];
2446 id_str2 = argv[1];
2447 } else if (argc == 3) {
2448 repo_path = realpath(argv[0], NULL);
2449 if (repo_path == NULL)
2450 return got_error_from_errno();
2451 id_str1 = argv[1];
2452 id_str2 = argv[2];
2453 } else
2454 usage_diff();
2456 init_curses();
2458 error = apply_unveil(repo_path, NULL);
2459 if (error)
2460 goto done;
2462 error = got_repo_open(&repo, repo_path);
2463 free(repo_path);
2464 if (error)
2465 goto done;
2467 error = got_object_resolve_id_str(&id1, repo, id_str1);
2468 if (error)
2469 goto done;
2471 error = got_object_resolve_id_str(&id2, repo, id_str2);
2472 if (error)
2473 goto done;
2475 SIMPLEQ_INIT(&refs);
2476 error = got_ref_list(&refs, repo);
2477 if (error)
2478 goto done;
2480 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2481 if (view == NULL) {
2482 error = got_error_from_errno();
2483 goto done;
2485 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2486 if (error)
2487 goto done;
2488 error = view_loop(view);
2489 done:
2490 got_repo_close(repo);
2491 return error;
2494 __dead static void
2495 usage_blame(void)
2497 endwin();
2498 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2499 getprogname());
2500 exit(1);
2503 struct tog_blame_line {
2504 int annotated;
2505 struct got_object_id *id;
2508 static const struct got_error *
2509 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2510 const char *path, struct tog_blame_line *lines, int nlines,
2511 int blame_complete, int selected_line, int *first_displayed_line,
2512 int *last_displayed_line, int *eof, int max_lines)
2514 const struct got_error *err;
2515 int lineno = 0, nprinted = 0;
2516 char *line;
2517 size_t len;
2518 wchar_t *wline;
2519 int width, wlimit;
2520 struct tog_blame_line *blame_line;
2521 struct got_object_id *prev_id = NULL;
2522 char *id_str;
2524 err = got_object_id_str(&id_str, id);
2525 if (err)
2526 return err;
2528 rewind(f);
2529 werase(view->window);
2531 if (asprintf(&line, "commit %s", id_str) == -1) {
2532 err = got_error_from_errno();
2533 free(id_str);
2534 return err;
2537 err = format_line(&wline, &width, line, view->ncols);
2538 free(line);
2539 line = NULL;
2540 if (view_needs_focus_indication(view))
2541 wstandout(view->window);
2542 waddwstr(view->window, wline);
2543 if (view_needs_focus_indication(view))
2544 wstandend(view->window);
2545 free(wline);
2546 wline = NULL;
2547 if (width < view->ncols)
2548 waddch(view->window, '\n');
2550 if (asprintf(&line, "[%d/%d] %s%s",
2551 *first_displayed_line - 1 + selected_line, nlines,
2552 blame_complete ? "" : "annotating... ", path) == -1) {
2553 free(id_str);
2554 return got_error_from_errno();
2556 free(id_str);
2557 err = format_line(&wline, &width, line, view->ncols);
2558 free(line);
2559 line = NULL;
2560 if (err)
2561 return err;
2562 waddwstr(view->window, wline);
2563 free(wline);
2564 wline = NULL;
2565 if (width < view->ncols)
2566 waddch(view->window, '\n');
2568 *eof = 0;
2569 while (nprinted < max_lines - 2) {
2570 line = parse_next_line(f, &len);
2571 if (line == NULL) {
2572 *eof = 1;
2573 break;
2575 if (++lineno < *first_displayed_line) {
2576 free(line);
2577 continue;
2580 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2581 err = format_line(&wline, &width, line, wlimit);
2582 if (err) {
2583 free(line);
2584 return err;
2587 if (view->focussed && nprinted == selected_line - 1)
2588 wstandout(view->window);
2590 blame_line = &lines[lineno - 1];
2591 if (blame_line->annotated && prev_id &&
2592 got_object_id_cmp(prev_id, blame_line->id) == 0)
2593 waddstr(view->window, " ");
2594 else if (blame_line->annotated) {
2595 char *id_str;
2596 err = got_object_id_str(&id_str, blame_line->id);
2597 if (err) {
2598 free(line);
2599 free(wline);
2600 return err;
2602 wprintw(view->window, "%.8s ", id_str);
2603 free(id_str);
2604 prev_id = blame_line->id;
2605 } else {
2606 waddstr(view->window, "........ ");
2607 prev_id = NULL;
2610 waddwstr(view->window, wline);
2611 while (width < wlimit) {
2612 waddch(view->window, ' ');
2613 width++;
2615 if (view->focussed && nprinted == selected_line - 1)
2616 wstandend(view->window);
2617 if (++nprinted == 1)
2618 *first_displayed_line = lineno;
2619 free(line);
2620 free(wline);
2621 wline = NULL;
2623 *last_displayed_line = lineno;
2625 view_vborder(view);
2627 return NULL;
2630 static const struct got_error *
2631 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2633 const struct got_error *err = NULL;
2634 struct tog_blame_cb_args *a = arg;
2635 struct tog_blame_line *line;
2636 int errcode;
2638 if (nlines != a->nlines ||
2639 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2640 return got_error(GOT_ERR_RANGE);
2642 errcode = pthread_mutex_lock(&tog_mutex);
2643 if (errcode)
2644 return got_error_set_errno(errcode);
2646 if (*a->quit) { /* user has quit the blame view */
2647 err = got_error(GOT_ERR_ITER_COMPLETED);
2648 goto done;
2651 if (lineno == -1)
2652 goto done; /* no change in this commit */
2654 line = &a->lines[lineno - 1];
2655 if (line->annotated)
2656 goto done;
2658 line->id = got_object_id_dup(id);
2659 if (line->id == NULL) {
2660 err = got_error_from_errno();
2661 goto done;
2663 line->annotated = 1;
2664 done:
2665 errcode = pthread_mutex_unlock(&tog_mutex);
2666 if (errcode)
2667 err = got_error_set_errno(errcode);
2668 return err;
2671 static void *
2672 blame_thread(void *arg)
2674 const struct got_error *err;
2675 struct tog_blame_thread_args *ta = arg;
2676 struct tog_blame_cb_args *a = ta->cb_args;
2677 int errcode;
2679 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2680 blame_cb, ta->cb_args);
2682 errcode = pthread_mutex_lock(&tog_mutex);
2683 if (errcode)
2684 return (void *)got_error_set_errno(errcode);
2686 got_repo_close(ta->repo);
2687 ta->repo = NULL;
2688 *ta->complete = 1;
2690 errcode = pthread_mutex_unlock(&tog_mutex);
2691 if (errcode && err == NULL)
2692 err = got_error_set_errno(errcode);
2694 return (void *)err;
2697 static struct got_object_id *
2698 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2699 int selected_line)
2701 struct tog_blame_line *line;
2703 line = &lines[first_displayed_line - 1 + selected_line - 1];
2704 if (!line->annotated)
2705 return NULL;
2707 return line->id;
2710 static const struct got_error *
2711 stop_blame(struct tog_blame *blame)
2713 const struct got_error *err = NULL;
2714 int i;
2716 if (blame->thread) {
2717 int errcode;
2718 errcode = pthread_mutex_unlock(&tog_mutex);
2719 if (errcode)
2720 return got_error_set_errno(errcode);
2721 errcode = pthread_join(blame->thread, (void **)&err);
2722 if (errcode)
2723 return got_error_set_errno(errcode);
2724 errcode = pthread_mutex_lock(&tog_mutex);
2725 if (errcode)
2726 return got_error_set_errno(errcode);
2727 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2728 err = NULL;
2729 blame->thread = NULL;
2731 if (blame->thread_args.repo) {
2732 got_repo_close(blame->thread_args.repo);
2733 blame->thread_args.repo = NULL;
2735 if (blame->f) {
2736 if (fclose(blame->f) != 0 && err == NULL)
2737 err = got_error_from_errno();
2738 blame->f = NULL;
2740 if (blame->lines) {
2741 for (i = 0; i < blame->nlines; i++)
2742 free(blame->lines[i].id);
2743 free(blame->lines);
2744 blame->lines = NULL;
2746 free(blame->cb_args.commit_id);
2747 blame->cb_args.commit_id = NULL;
2749 return err;
2752 static const struct got_error *
2753 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2754 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2755 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2756 struct got_repository *repo)
2758 const struct got_error *err = NULL;
2759 struct got_blob_object *blob = NULL;
2760 struct got_repository *thread_repo = NULL;
2761 struct got_object_id *obj_id = NULL;
2762 int obj_type;
2764 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2765 if (err)
2766 return err;
2767 if (obj_id == NULL)
2768 return got_error(GOT_ERR_NO_OBJ);
2770 err = got_object_get_type(&obj_type, repo, obj_id);
2771 if (err)
2772 goto done;
2774 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2775 err = got_error(GOT_ERR_OBJ_TYPE);
2776 goto done;
2779 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2780 if (err)
2781 goto done;
2782 blame->f = got_opentemp();
2783 if (blame->f == NULL) {
2784 err = got_error_from_errno();
2785 goto done;
2787 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2788 blame->f, blob);
2789 if (err)
2790 goto done;
2792 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2793 if (blame->lines == NULL) {
2794 err = got_error_from_errno();
2795 goto done;
2798 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2799 if (err)
2800 goto done;
2802 blame->cb_args.view = view;
2803 blame->cb_args.lines = blame->lines;
2804 blame->cb_args.nlines = blame->nlines;
2805 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2806 if (blame->cb_args.commit_id == NULL) {
2807 err = got_error_from_errno();
2808 goto done;
2810 blame->cb_args.quit = done;
2812 blame->thread_args.path = path;
2813 blame->thread_args.repo = thread_repo;
2814 blame->thread_args.cb_args = &blame->cb_args;
2815 blame->thread_args.complete = blame_complete;
2816 *blame_complete = 0;
2818 done:
2819 if (blob)
2820 got_object_blob_close(blob);
2821 free(obj_id);
2822 if (err)
2823 stop_blame(blame);
2824 return err;
2827 static const struct got_error *
2828 open_blame_view(struct tog_view *view, char *path,
2829 struct got_object_id *commit_id, struct got_reflist_head *refs,
2830 struct got_repository *repo)
2832 const struct got_error *err = NULL;
2833 struct tog_blame_view_state *s = &view->state.blame;
2835 SIMPLEQ_INIT(&s->blamed_commits);
2837 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2838 if (err)
2839 return err;
2841 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2842 s->first_displayed_line = 1;
2843 s->last_displayed_line = view->nlines;
2844 s->selected_line = 1;
2845 s->blame_complete = 0;
2846 s->path = path;
2847 if (s->path == NULL)
2848 return got_error_from_errno();
2849 s->repo = repo;
2850 s->refs = refs;
2851 s->commit_id = commit_id;
2852 memset(&s->blame, 0, sizeof(s->blame));
2854 view->show = show_blame_view;
2855 view->input = input_blame_view;
2856 view->close = close_blame_view;
2858 return run_blame(&s->blame, view, &s->blame_complete,
2859 &s->first_displayed_line, &s->last_displayed_line,
2860 &s->selected_line, &s->done, &s->eof, s->path,
2861 s->blamed_commit->id, s->repo);
2864 static const struct got_error *
2865 close_blame_view(struct tog_view *view)
2867 const struct got_error *err = NULL;
2868 struct tog_blame_view_state *s = &view->state.blame;
2870 if (s->blame.thread)
2871 err = stop_blame(&s->blame);
2873 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2874 struct got_object_qid *blamed_commit;
2875 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2876 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2877 got_object_qid_free(blamed_commit);
2880 free(s->path);
2882 return err;
2885 static const struct got_error *
2886 show_blame_view(struct tog_view *view)
2888 const struct got_error *err = NULL;
2889 struct tog_blame_view_state *s = &view->state.blame;
2890 int errcode;
2892 if (s->blame.thread == NULL) {
2893 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2894 &s->blame.thread_args);
2895 if (errcode)
2896 return got_error_set_errno(errcode);
2899 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2900 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2901 s->selected_line, &s->first_displayed_line,
2902 &s->last_displayed_line, &s->eof, view->nlines);
2904 view_vborder(view);
2905 return err;
2908 static const struct got_error *
2909 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2910 struct tog_view **focus_view, struct tog_view *view, int ch)
2912 const struct got_error *err = NULL, *thread_err = NULL;
2913 struct tog_view *diff_view;
2914 struct tog_blame_view_state *s = &view->state.blame;
2915 int begin_x = 0;
2917 switch (ch) {
2918 case 'q':
2919 s->done = 1;
2920 break;
2921 case 'k':
2922 case KEY_UP:
2923 if (s->selected_line > 1)
2924 s->selected_line--;
2925 else if (s->selected_line == 1 &&
2926 s->first_displayed_line > 1)
2927 s->first_displayed_line--;
2928 else
2929 view_flash(view);
2930 break;
2931 case KEY_PPAGE:
2932 if (s->first_displayed_line == 1) {
2933 if (s->selected_line == 1) {
2934 view_flash(view);
2935 break;
2937 s->selected_line = 1;
2938 break;
2940 if (s->first_displayed_line > view->nlines - 2)
2941 s->first_displayed_line -=
2942 (view->nlines - 2);
2943 else
2944 s->first_displayed_line = 1;
2945 break;
2946 case 'j':
2947 case KEY_DOWN:
2948 if (s->selected_line < view->nlines - 2 &&
2949 s->first_displayed_line +
2950 s->selected_line <= s->blame.nlines)
2951 s->selected_line++;
2952 else if (s->last_displayed_line <
2953 s->blame.nlines)
2954 s->first_displayed_line++;
2955 else
2956 view_flash(view);
2957 break;
2958 case 'b':
2959 case 'p': {
2960 struct got_object_id *id = NULL;
2961 id = get_selected_commit_id(s->blame.lines,
2962 s->first_displayed_line, s->selected_line);
2963 if (id == NULL)
2964 break;
2965 if (ch == 'p') {
2966 struct got_commit_object *commit;
2967 struct got_object_qid *pid;
2968 struct got_object_id *blob_id = NULL;
2969 int obj_type;
2970 err = got_object_open_as_commit(&commit,
2971 s->repo, id);
2972 if (err)
2973 break;
2974 pid = SIMPLEQ_FIRST(
2975 got_object_commit_get_parent_ids(commit));
2976 if (pid == NULL) {
2977 got_object_commit_close(commit);
2978 break;
2980 /* Check if path history ends here. */
2981 err = got_object_id_by_path(&blob_id, s->repo,
2982 pid->id, s->path);
2983 if (err) {
2984 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2985 err = NULL;
2986 got_object_commit_close(commit);
2987 break;
2989 err = got_object_get_type(&obj_type, s->repo,
2990 blob_id);
2991 free(blob_id);
2992 /* Can't blame non-blob type objects. */
2993 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2994 got_object_commit_close(commit);
2995 break;
2997 err = got_object_qid_alloc(&s->blamed_commit,
2998 pid->id);
2999 got_object_commit_close(commit);
3000 } else {
3001 if (got_object_id_cmp(id,
3002 s->blamed_commit->id) == 0)
3003 break;
3004 err = got_object_qid_alloc(&s->blamed_commit,
3005 id);
3007 if (err)
3008 break;
3009 s->done = 1;
3010 thread_err = stop_blame(&s->blame);
3011 s->done = 0;
3012 if (thread_err)
3013 break;
3014 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3015 s->blamed_commit, entry);
3016 err = run_blame(&s->blame, view, &s->blame_complete,
3017 &s->first_displayed_line, &s->last_displayed_line,
3018 &s->selected_line, &s->done, &s->eof,
3019 s->path, s->blamed_commit->id, s->repo);
3020 if (err)
3021 break;
3022 break;
3024 case 'B': {
3025 struct got_object_qid *first;
3026 first = SIMPLEQ_FIRST(&s->blamed_commits);
3027 if (!got_object_id_cmp(first->id, s->commit_id))
3028 break;
3029 s->done = 1;
3030 thread_err = stop_blame(&s->blame);
3031 s->done = 0;
3032 if (thread_err)
3033 break;
3034 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3035 got_object_qid_free(s->blamed_commit);
3036 s->blamed_commit =
3037 SIMPLEQ_FIRST(&s->blamed_commits);
3038 err = run_blame(&s->blame, view, &s->blame_complete,
3039 &s->first_displayed_line, &s->last_displayed_line,
3040 &s->selected_line, &s->done, &s->eof, s->path,
3041 s->blamed_commit->id, s->repo);
3042 if (err)
3043 break;
3044 break;
3046 case KEY_ENTER:
3047 case '\r': {
3048 struct got_object_id *id = NULL;
3049 struct got_object_qid *pid;
3050 struct got_commit_object *commit = NULL;
3051 id = get_selected_commit_id(s->blame.lines,
3052 s->first_displayed_line, s->selected_line);
3053 if (id == NULL)
3054 break;
3055 err = got_object_open_as_commit(&commit, s->repo, id);
3056 if (err)
3057 break;
3058 pid = SIMPLEQ_FIRST(
3059 got_object_commit_get_parent_ids(commit));
3060 if (view_is_parent_view(view))
3061 begin_x = view_split_begin_x(view->begin_x);
3062 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3063 if (diff_view == NULL) {
3064 got_object_commit_close(commit);
3065 err = got_error_from_errno();
3066 break;
3068 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3069 id, NULL, s->refs, s->repo);
3070 got_object_commit_close(commit);
3071 if (err) {
3072 view_close(diff_view);
3073 break;
3075 if (view_is_parent_view(view)) {
3076 err = view_close_child(view);
3077 if (err)
3078 break;
3079 err = view_set_child(view, diff_view);
3080 if (err) {
3081 view_close(diff_view);
3082 break;
3084 *focus_view = diff_view;
3085 view->child_focussed = 1;
3086 } else
3087 *new_view = diff_view;
3088 if (err)
3089 break;
3090 break;
3092 case KEY_NPAGE:
3093 case ' ':
3094 if (s->last_displayed_line >= s->blame.nlines &&
3095 s->selected_line >= MIN(s->blame.nlines,
3096 view->nlines - 2)) {
3097 view_flash(view);
3098 break;
3100 if (s->last_displayed_line >= s->blame.nlines &&
3101 s->selected_line < view->nlines - 2) {
3102 s->selected_line = MIN(s->blame.nlines,
3103 view->nlines - 2);
3104 break;
3106 if (s->last_displayed_line + view->nlines - 2
3107 <= s->blame.nlines)
3108 s->first_displayed_line +=
3109 view->nlines - 2;
3110 else
3111 s->first_displayed_line =
3112 s->blame.nlines -
3113 (view->nlines - 3);
3114 break;
3115 case KEY_RESIZE:
3116 if (s->selected_line > view->nlines - 2) {
3117 s->selected_line = MIN(s->blame.nlines,
3118 view->nlines - 2);
3120 break;
3121 default:
3122 break;
3124 return thread_err ? thread_err : err;
3127 static const struct got_error *
3128 cmd_blame(int argc, char *argv[])
3130 const struct got_error *error;
3131 struct got_repository *repo = NULL;
3132 struct got_reflist_head refs;
3133 struct got_worktree *worktree = NULL;
3134 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3135 struct got_object_id *commit_id = NULL;
3136 char *commit_id_str = NULL;
3137 int ch;
3138 struct tog_view *view;
3140 #ifndef PROFILE
3141 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3142 NULL) == -1)
3143 err(1, "pledge");
3144 #endif
3146 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3147 switch (ch) {
3148 case 'c':
3149 commit_id_str = optarg;
3150 break;
3151 case 'r':
3152 repo_path = realpath(optarg, NULL);
3153 if (repo_path == NULL)
3154 err(1, "-r option");
3155 break;
3156 default:
3157 usage();
3158 /* NOTREACHED */
3162 argc -= optind;
3163 argv += optind;
3165 if (argc == 1)
3166 path = argv[0];
3167 else
3168 usage_blame();
3170 cwd = getcwd(NULL, 0);
3171 if (cwd == NULL) {
3172 error = got_error_from_errno();
3173 goto done;
3175 if (repo_path == NULL) {
3176 error = got_worktree_open(&worktree, cwd);
3177 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3178 goto done;
3179 else
3180 error = NULL;
3181 if (worktree) {
3182 repo_path =
3183 strdup(got_worktree_get_repo_path(worktree));
3184 if (repo_path == NULL)
3185 error = got_error_from_errno();
3186 if (error)
3187 goto done;
3188 } else {
3189 repo_path = strdup(cwd);
3190 if (repo_path == NULL) {
3191 error = got_error_from_errno();
3192 goto done;
3197 init_curses();
3199 error = apply_unveil(repo_path, NULL);
3200 if (error)
3201 goto done;
3203 error = got_repo_open(&repo, repo_path);
3204 if (error != NULL)
3205 goto done;
3207 if (worktree) {
3208 const char *prefix = got_worktree_get_path_prefix(worktree);
3209 char *p, *worktree_subdir = cwd +
3210 strlen(got_worktree_get_root_path(worktree));
3211 if (asprintf(&p, "%s%s%s%s%s",
3212 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3213 worktree_subdir, worktree_subdir[0] ? "/" : "",
3214 path) == -1) {
3215 error = got_error_from_errno();
3216 goto done;
3218 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3219 free(p);
3220 } else {
3221 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3223 if (error)
3224 goto done;
3226 if (commit_id_str == NULL) {
3227 struct got_reference *head_ref;
3228 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3229 if (error != NULL)
3230 goto done;
3231 error = got_ref_resolve(&commit_id, repo, head_ref);
3232 got_ref_close(head_ref);
3233 } else {
3234 error = got_object_resolve_id_str(&commit_id, repo,
3235 commit_id_str);
3237 if (error != NULL)
3238 goto done;
3240 SIMPLEQ_INIT(&refs);
3241 error = got_ref_list(&refs, repo);
3242 if (error)
3243 goto done;
3245 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3246 if (view == NULL) {
3247 error = got_error_from_errno();
3248 goto done;
3250 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3251 if (error)
3252 goto done;
3253 error = view_loop(view);
3254 done:
3255 free(repo_path);
3256 free(cwd);
3257 free(commit_id);
3258 if (worktree)
3259 got_worktree_close(worktree);
3260 if (repo)
3261 got_repo_close(repo);
3262 return error;
3265 static const struct got_error *
3266 draw_tree_entries(struct tog_view *view,
3267 struct got_tree_entry **first_displayed_entry,
3268 struct got_tree_entry **last_displayed_entry,
3269 struct got_tree_entry **selected_entry, int *ndisplayed,
3270 const char *label, int show_ids, const char *parent_path,
3271 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3273 const struct got_error *err = NULL;
3274 struct got_tree_entry *te;
3275 wchar_t *wline;
3276 int width, n;
3278 *ndisplayed = 0;
3280 werase(view->window);
3282 if (limit == 0)
3283 return NULL;
3285 err = format_line(&wline, &width, label, view->ncols);
3286 if (err)
3287 return err;
3288 if (view_needs_focus_indication(view))
3289 wstandout(view->window);
3290 waddwstr(view->window, wline);
3291 if (view_needs_focus_indication(view))
3292 wstandend(view->window);
3293 free(wline);
3294 wline = NULL;
3295 if (width < view->ncols)
3296 waddch(view->window, '\n');
3297 if (--limit <= 0)
3298 return NULL;
3299 err = format_line(&wline, &width, parent_path, view->ncols);
3300 if (err)
3301 return err;
3302 waddwstr(view->window, wline);
3303 free(wline);
3304 wline = NULL;
3305 if (width < view->ncols)
3306 waddch(view->window, '\n');
3307 if (--limit <= 0)
3308 return NULL;
3309 waddch(view->window, '\n');
3310 if (--limit <= 0)
3311 return NULL;
3313 te = SIMPLEQ_FIRST(&entries->head);
3314 if (*first_displayed_entry == NULL) {
3315 if (selected == 0) {
3316 if (view->focussed)
3317 wstandout(view->window);
3318 *selected_entry = NULL;
3320 waddstr(view->window, " ..\n"); /* parent directory */
3321 if (selected == 0 && view->focussed)
3322 wstandend(view->window);
3323 (*ndisplayed)++;
3324 if (--limit <= 0)
3325 return NULL;
3326 n = 1;
3327 } else {
3328 n = 0;
3329 while (te != *first_displayed_entry)
3330 te = SIMPLEQ_NEXT(te, entry);
3333 while (te) {
3334 char *line = NULL, *id_str = NULL;
3336 if (show_ids) {
3337 err = got_object_id_str(&id_str, te->id);
3338 if (err)
3339 return got_error_from_errno();
3341 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3342 te->name, S_ISDIR(te->mode) ? "/" :
3343 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3344 free(id_str);
3345 return got_error_from_errno();
3347 free(id_str);
3348 err = format_line(&wline, &width, line, view->ncols);
3349 if (err) {
3350 free(line);
3351 break;
3353 if (n == selected) {
3354 if (view->focussed)
3355 wstandout(view->window);
3356 *selected_entry = te;
3358 waddwstr(view->window, wline);
3359 if (width < view->ncols)
3360 waddch(view->window, '\n');
3361 if (n == selected && view->focussed)
3362 wstandend(view->window);
3363 free(line);
3364 free(wline);
3365 wline = NULL;
3366 n++;
3367 (*ndisplayed)++;
3368 *last_displayed_entry = te;
3369 if (--limit <= 0)
3370 break;
3371 te = SIMPLEQ_NEXT(te, entry);
3374 return err;
3377 static void
3378 tree_scroll_up(struct tog_view *view,
3379 struct got_tree_entry **first_displayed_entry, int maxscroll,
3380 const struct got_tree_entries *entries, int isroot)
3382 struct got_tree_entry *te, *prev;
3383 int i;
3385 if (*first_displayed_entry == NULL)
3386 return;
3388 te = SIMPLEQ_FIRST(&entries->head);
3389 if (*first_displayed_entry == te) {
3390 view_flash(view);
3391 if (!isroot)
3392 *first_displayed_entry = NULL;
3393 return;
3396 /* XXX this is stupid... switch to TAILQ? */
3397 for (i = 0; i < maxscroll; i++) {
3398 while (te != *first_displayed_entry) {
3399 prev = te;
3400 te = SIMPLEQ_NEXT(te, entry);
3402 *first_displayed_entry = prev;
3403 te = SIMPLEQ_FIRST(&entries->head);
3405 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3406 *first_displayed_entry = NULL;
3409 static int
3410 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3411 struct got_tree_entry *last_displayed_entry,
3412 const struct got_tree_entries *entries)
3414 struct got_tree_entry *next, *last;
3415 int n = 0;
3417 if (*first_displayed_entry)
3418 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3419 else
3420 next = SIMPLEQ_FIRST(&entries->head);
3421 last = last_displayed_entry;
3422 while (next && last && n++ < maxscroll) {
3423 last = SIMPLEQ_NEXT(last, entry);
3424 if (last) {
3425 *first_displayed_entry = next;
3426 next = SIMPLEQ_NEXT(next, entry);
3429 return n;
3432 static const struct got_error *
3433 tree_entry_path(char **path, struct tog_parent_trees *parents,
3434 struct got_tree_entry *te)
3436 const struct got_error *err = NULL;
3437 struct tog_parent_tree *pt;
3438 size_t len = 2; /* for leading slash and NUL */
3440 TAILQ_FOREACH(pt, parents, entry)
3441 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3442 if (te)
3443 len += strlen(te->name);
3445 *path = calloc(1, len);
3446 if (path == NULL)
3447 return got_error_from_errno();
3449 (*path)[0] = '/';
3450 pt = TAILQ_LAST(parents, tog_parent_trees);
3451 while (pt) {
3452 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3453 err = got_error(GOT_ERR_NO_SPACE);
3454 goto done;
3456 if (strlcat(*path, "/", len) >= len) {
3457 err = got_error(GOT_ERR_NO_SPACE);
3458 goto done;
3460 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3462 if (te) {
3463 if (strlcat(*path, te->name, len) >= len) {
3464 err = got_error(GOT_ERR_NO_SPACE);
3465 goto done;
3468 done:
3469 if (err) {
3470 free(*path);
3471 *path = NULL;
3473 return err;
3476 static const struct got_error *
3477 blame_tree_entry(struct tog_view **new_view, int begin_x,
3478 struct got_tree_entry *te, struct tog_parent_trees *parents,
3479 struct got_object_id *commit_id, struct got_reflist_head *refs,
3480 struct got_repository *repo)
3482 const struct got_error *err = NULL;
3483 char *path;
3484 struct tog_view *blame_view;
3486 err = tree_entry_path(&path, parents, te);
3487 if (err)
3488 return err;
3490 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3491 if (blame_view == NULL)
3492 return got_error_from_errno();
3494 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3495 if (err) {
3496 view_close(blame_view);
3497 free(path);
3498 } else
3499 *new_view = blame_view;
3500 return err;
3503 static const struct got_error *
3504 log_tree_entry(struct tog_view **new_view, int begin_x,
3505 struct got_tree_entry *te, struct tog_parent_trees *parents,
3506 struct got_object_id *commit_id, struct got_reflist_head *refs,
3507 struct got_repository *repo)
3509 struct tog_view *log_view;
3510 const struct got_error *err = NULL;
3511 char *path;
3513 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3514 if (log_view == NULL)
3515 return got_error_from_errno();
3517 err = tree_entry_path(&path, parents, te);
3518 if (err)
3519 return err;
3521 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3522 if (err)
3523 view_close(log_view);
3524 else
3525 *new_view = log_view;
3526 free(path);
3527 return err;
3530 static const struct got_error *
3531 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3532 struct got_object_id *commit_id, struct got_reflist_head *refs,
3533 struct got_repository *repo)
3535 const struct got_error *err = NULL;
3536 char *commit_id_str = NULL;
3537 struct tog_tree_view_state *s = &view->state.tree;
3539 TAILQ_INIT(&s->parents);
3541 err = got_object_id_str(&commit_id_str, commit_id);
3542 if (err != NULL)
3543 goto done;
3545 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3546 err = got_error_from_errno();
3547 goto done;
3550 s->root = s->tree = root;
3551 s->entries = got_object_tree_get_entries(root);
3552 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3553 s->commit_id = got_object_id_dup(commit_id);
3554 if (s->commit_id == NULL) {
3555 err = got_error_from_errno();
3556 goto done;
3558 s->refs = refs;
3559 s->repo = repo;
3561 view->show = show_tree_view;
3562 view->input = input_tree_view;
3563 view->close = close_tree_view;
3564 done:
3565 free(commit_id_str);
3566 if (err) {
3567 free(s->tree_label);
3568 s->tree_label = NULL;
3570 return err;
3573 static const struct got_error *
3574 close_tree_view(struct tog_view *view)
3576 struct tog_tree_view_state *s = &view->state.tree;
3578 free(s->tree_label);
3579 s->tree_label = NULL;
3580 free(s->commit_id);
3581 s->commit_id = NULL;
3582 while (!TAILQ_EMPTY(&s->parents)) {
3583 struct tog_parent_tree *parent;
3584 parent = TAILQ_FIRST(&s->parents);
3585 TAILQ_REMOVE(&s->parents, parent, entry);
3586 free(parent);
3589 if (s->tree != s->root)
3590 got_object_tree_close(s->tree);
3591 got_object_tree_close(s->root);
3593 return NULL;
3596 static const struct got_error *
3597 show_tree_view(struct tog_view *view)
3599 const struct got_error *err = NULL;
3600 struct tog_tree_view_state *s = &view->state.tree;
3601 char *parent_path;
3603 err = tree_entry_path(&parent_path, &s->parents, NULL);
3604 if (err)
3605 return err;
3607 err = draw_tree_entries(view, &s->first_displayed_entry,
3608 &s->last_displayed_entry, &s->selected_entry,
3609 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3610 s->entries, s->selected, view->nlines, s->tree == s->root);
3611 free(parent_path);
3613 view_vborder(view);
3614 return err;
3617 static const struct got_error *
3618 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3619 struct tog_view **focus_view, struct tog_view *view, int ch)
3621 const struct got_error *err = NULL;
3622 struct tog_tree_view_state *s = &view->state.tree;
3623 struct tog_view *log_view;
3624 int begin_x = 0, nscrolled;
3626 switch (ch) {
3627 case 'i':
3628 s->show_ids = !s->show_ids;
3629 break;
3630 case 'l':
3631 if (!s->selected_entry)
3632 break;
3633 if (view_is_parent_view(view))
3634 begin_x = view_split_begin_x(view->begin_x);
3635 err = log_tree_entry(&log_view, begin_x,
3636 s->selected_entry, &s->parents,
3637 s->commit_id, s->refs, s->repo);
3638 if (view_is_parent_view(view)) {
3639 err = view_close_child(view);
3640 if (err)
3641 return err;
3642 err = view_set_child(view, log_view);
3643 if (err) {
3644 view_close(log_view);
3645 break;
3647 *focus_view = log_view;
3648 view->child_focussed = 1;
3649 } else
3650 *new_view = log_view;
3651 break;
3652 case 'k':
3653 case KEY_UP:
3654 if (s->selected == 0) {
3655 view_flash(view);
3656 break;
3658 if (s->selected > 0) {
3659 s->selected--;
3660 if (s->selected == 0)
3661 break;
3663 if (s->selected > 0)
3664 break;
3665 tree_scroll_up(view, &s->first_displayed_entry, 1,
3666 s->entries, s->tree == s->root);
3667 break;
3668 case KEY_PPAGE:
3669 tree_scroll_up(view, &s->first_displayed_entry,
3670 MAX(0, view->nlines - 4 - s->selected), s->entries,
3671 s->tree == s->root);
3672 s->selected = 0;
3673 if (SIMPLEQ_FIRST(&s->entries->head) ==
3674 s->first_displayed_entry && s->tree != s->root)
3675 s->first_displayed_entry = NULL;
3676 break;
3677 case 'j':
3678 case KEY_DOWN:
3679 if (s->selected < s->ndisplayed - 1) {
3680 s->selected++;
3681 break;
3683 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3684 == NULL) {
3685 /* can't scroll any further */
3686 view_flash(view);
3687 break;
3689 tree_scroll_down(&s->first_displayed_entry, 1,
3690 s->last_displayed_entry, s->entries);
3691 break;
3692 case KEY_NPAGE:
3693 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3694 == NULL) {
3695 /* can't scroll any further; move cursor down */
3696 if (s->selected < s->ndisplayed - 1)
3697 s->selected = s->ndisplayed - 1;
3698 else
3699 view_flash(view);
3700 break;
3702 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3703 view->nlines, s->last_displayed_entry, s->entries);
3704 if (nscrolled < view->nlines) {
3705 int ndisplayed = 0;
3706 struct got_tree_entry *te;
3707 te = s->first_displayed_entry;
3708 do {
3709 ndisplayed++;
3710 te = SIMPLEQ_NEXT(te, entry);
3711 } while (te);
3712 s->selected = ndisplayed - 1;
3714 break;
3715 case KEY_ENTER:
3716 case '\r':
3717 if (s->selected_entry == NULL) {
3718 struct tog_parent_tree *parent;
3719 case KEY_BACKSPACE:
3720 /* user selected '..' */
3721 if (s->tree == s->root)
3722 break;
3723 parent = TAILQ_FIRST(&s->parents);
3724 TAILQ_REMOVE(&s->parents, parent,
3725 entry);
3726 got_object_tree_close(s->tree);
3727 s->tree = parent->tree;
3728 s->entries =
3729 got_object_tree_get_entries(s->tree);
3730 s->first_displayed_entry =
3731 parent->first_displayed_entry;
3732 s->selected_entry =
3733 parent->selected_entry;
3734 s->selected = parent->selected;
3735 free(parent);
3736 } else if (S_ISDIR(s->selected_entry->mode)) {
3737 struct tog_parent_tree *parent;
3738 struct got_tree_object *child;
3739 err = got_object_open_as_tree(&child,
3740 s->repo, s->selected_entry->id);
3741 if (err)
3742 break;
3743 parent = calloc(1, sizeof(*parent));
3744 if (parent == NULL) {
3745 err = got_error_from_errno();
3746 break;
3748 parent->tree = s->tree;
3749 parent->first_displayed_entry =
3750 s->first_displayed_entry;
3751 parent->selected_entry = s->selected_entry;
3752 parent->selected = s->selected;
3753 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3754 s->tree = child;
3755 s->entries =
3756 got_object_tree_get_entries(s->tree);
3757 s->selected = 0;
3758 s->first_displayed_entry = NULL;
3759 } else if (S_ISREG(s->selected_entry->mode)) {
3760 struct tog_view *blame_view;
3761 int begin_x = view_is_parent_view(view) ?
3762 view_split_begin_x(view->begin_x) : 0;
3764 err = blame_tree_entry(&blame_view, begin_x,
3765 s->selected_entry, &s->parents,
3766 s->commit_id, s->refs, s->repo);
3767 if (err)
3768 break;
3769 if (view_is_parent_view(view)) {
3770 err = view_close_child(view);
3771 if (err)
3772 return err;
3773 err = view_set_child(view, blame_view);
3774 if (err) {
3775 view_close(blame_view);
3776 break;
3778 *focus_view = blame_view;
3779 view->child_focussed = 1;
3780 } else
3781 *new_view = blame_view;
3783 break;
3784 case KEY_RESIZE:
3785 if (s->selected > view->nlines)
3786 s->selected = s->ndisplayed - 1;
3787 break;
3788 default:
3789 break;
3792 return err;
3795 __dead static void
3796 usage_tree(void)
3798 endwin();
3799 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3800 getprogname());
3801 exit(1);
3804 static const struct got_error *
3805 cmd_tree(int argc, char *argv[])
3807 const struct got_error *error;
3808 struct got_repository *repo = NULL;
3809 struct got_reflist_head refs;
3810 char *repo_path = NULL;
3811 struct got_object_id *commit_id = NULL;
3812 char *commit_id_arg = NULL;
3813 struct got_commit_object *commit = NULL;
3814 struct got_tree_object *tree = NULL;
3815 int ch;
3816 struct tog_view *view;
3818 #ifndef PROFILE
3819 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3820 NULL) == -1)
3821 err(1, "pledge");
3822 #endif
3824 while ((ch = getopt(argc, argv, "c:")) != -1) {
3825 switch (ch) {
3826 case 'c':
3827 commit_id_arg = optarg;
3828 break;
3829 default:
3830 usage();
3831 /* NOTREACHED */
3835 argc -= optind;
3836 argv += optind;
3838 if (argc == 0) {
3839 struct got_worktree *worktree;
3840 char *cwd = getcwd(NULL, 0);
3841 if (cwd == NULL)
3842 return got_error_from_errno();
3843 error = got_worktree_open(&worktree, cwd);
3844 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3845 goto done;
3846 if (worktree) {
3847 free(cwd);
3848 repo_path =
3849 strdup(got_worktree_get_repo_path(worktree));
3850 got_worktree_close(worktree);
3851 } else
3852 repo_path = cwd;
3853 if (repo_path == NULL) {
3854 error = got_error_from_errno();
3855 goto done;
3857 } else if (argc == 1) {
3858 repo_path = realpath(argv[0], NULL);
3859 if (repo_path == NULL)
3860 return got_error_from_errno();
3861 } else
3862 usage_log();
3864 init_curses();
3866 error = apply_unveil(repo_path, NULL);
3867 if (error)
3868 goto done;
3870 error = got_repo_open(&repo, repo_path);
3871 if (error != NULL)
3872 goto done;
3874 if (commit_id_arg == NULL)
3875 error = get_head_commit_id(&commit_id, repo);
3876 else
3877 error = got_object_resolve_id_str(&commit_id, repo,
3878 commit_id_arg);
3879 if (error != NULL)
3880 goto done;
3882 error = got_object_open_as_commit(&commit, repo, commit_id);
3883 if (error != NULL)
3884 goto done;
3886 error = got_object_open_as_tree(&tree, repo,
3887 got_object_commit_get_tree_id(commit));
3888 if (error != NULL)
3889 goto done;
3891 SIMPLEQ_INIT(&refs);
3892 error = got_ref_list(&refs, repo);
3893 if (error)
3894 goto done;
3896 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3897 if (view == NULL) {
3898 error = got_error_from_errno();
3899 goto done;
3901 error = open_tree_view(view, tree, commit_id, &refs, repo);
3902 if (error)
3903 goto done;
3904 error = view_loop(view);
3905 done:
3906 free(repo_path);
3907 free(commit_id);
3908 if (commit)
3909 got_object_commit_close(commit);
3910 if (tree)
3911 got_object_tree_close(tree);
3912 if (repo)
3913 got_repo_close(repo);
3914 return error;
3917 __dead static void
3918 usage(void)
3920 int i;
3922 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3923 "Available commands:\n", getprogname());
3924 for (i = 0; i < nitems(tog_commands); i++) {
3925 struct tog_cmd *cmd = &tog_commands[i];
3926 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3928 exit(1);
3931 static char **
3932 make_argv(const char *arg0, const char *arg1)
3934 char **argv;
3935 int argc = (arg1 == NULL ? 1 : 2);
3937 argv = calloc(argc, sizeof(char *));
3938 if (argv == NULL)
3939 err(1, "calloc");
3940 argv[0] = strdup(arg0);
3941 if (argv[0] == NULL)
3942 err(1, "calloc");
3943 if (arg1) {
3944 argv[1] = strdup(arg1);
3945 if (argv[1] == NULL)
3946 err(1, "calloc");
3949 return argv;
3952 int
3953 main(int argc, char *argv[])
3955 const struct got_error *error = NULL;
3956 struct tog_cmd *cmd = NULL;
3957 int ch, hflag = 0;
3958 char **cmd_argv = NULL;
3960 setlocale(LC_CTYPE, "");
3962 while ((ch = getopt(argc, argv, "h")) != -1) {
3963 switch (ch) {
3964 case 'h':
3965 hflag = 1;
3966 break;
3967 default:
3968 usage();
3969 /* NOTREACHED */
3973 argc -= optind;
3974 argv += optind;
3975 optind = 0;
3976 optreset = 1;
3978 if (argc == 0) {
3979 if (hflag)
3980 usage();
3981 /* Build an argument vector which runs a default command. */
3982 cmd = &tog_commands[0];
3983 cmd_argv = make_argv(cmd->name, NULL);
3984 argc = 1;
3985 } else {
3986 int i;
3988 /* Did the user specific a command? */
3989 for (i = 0; i < nitems(tog_commands); i++) {
3990 if (strncmp(tog_commands[i].name, argv[0],
3991 strlen(argv[0])) == 0) {
3992 cmd = &tog_commands[i];
3993 if (hflag)
3994 tog_commands[i].cmd_usage();
3995 break;
3998 if (cmd == NULL) {
3999 /* Did the user specify a repository? */
4000 char *repo_path = realpath(argv[0], NULL);
4001 if (repo_path) {
4002 struct got_repository *repo;
4003 error = got_repo_open(&repo, repo_path);
4004 if (error == NULL)
4005 got_repo_close(repo);
4006 } else
4007 error = got_error_from_errno();
4008 if (error) {
4009 if (hflag) {
4010 fprintf(stderr, "%s: '%s' is not a "
4011 "known command\n", getprogname(),
4012 argv[0]);
4013 usage();
4015 fprintf(stderr, "%s: '%s' is neither a known "
4016 "command nor a path to a repository\n",
4017 getprogname(), argv[0]);
4018 free(repo_path);
4019 return 1;
4021 cmd = &tog_commands[0];
4022 cmd_argv = make_argv(cmd->name, repo_path);
4023 argc = 2;
4024 free(repo_path);
4028 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4029 if (error)
4030 goto done;
4031 done:
4032 endwin();
4033 free(cmd_argv);
4034 if (error)
4035 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4036 return 0;