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 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
926 char *smallerthan, *at;
928 smallerthan = strchr(author, '<');
929 if (smallerthan && smallerthan[1] != '\0')
930 author = smallerthan + 1;
931 at = strchr(author, '@');
932 if (at)
933 *at = '\0';
934 return format_line(wauthor, author_width, author, limit);
937 static const struct got_error *
938 draw_commit(struct tog_view *view, struct got_commit_object *commit,
939 struct got_object_id *id, struct got_reflist_head *refs,
940 int author_display_cols)
942 const struct got_error *err = NULL;
943 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
944 char *logmsg0 = NULL, *logmsg = NULL;
945 char *author = NULL;
946 wchar_t *wlogmsg = NULL, *wauthor = NULL;
947 int author_width, logmsg_width;
948 char *newline, *line = NULL;
949 int col, limit;
950 static const size_t date_display_cols = 9;
951 const int avail = view->ncols;
952 struct tm tm;
953 time_t committer_time;
955 committer_time = got_object_commit_get_committer_time(commit);
956 if (localtime_r(&committer_time, &tm) == NULL)
957 return got_error_from_errno();
958 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
959 >= sizeof(datebuf))
960 return got_error(GOT_ERR_NO_SPACE);
962 if (avail < date_display_cols)
963 limit = MIN(sizeof(datebuf) - 1, avail);
964 else
965 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
966 waddnstr(view->window, datebuf, limit);
967 col = limit + 1;
968 if (col > avail)
969 goto done;
971 author = strdup(got_object_commit_get_author(commit));
972 if (author == NULL) {
973 err = got_error_from_errno();
974 goto done;
976 err = format_author(&wauthor, &author_width, author, avail - col);
977 if (err)
978 goto done;
979 waddwstr(view->window, wauthor);
980 col += author_width;
981 while (col <= avail && author_width < author_display_cols + 2) {
982 waddch(view->window, ' ');
983 col++;
984 author_width++;
986 if (col > avail)
987 goto done;
989 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
990 if (logmsg0 == NULL) {
991 err = got_error_from_errno();
992 goto done;
994 logmsg = logmsg0;
995 while (*logmsg == '\n')
996 logmsg++;
997 newline = strchr(logmsg, '\n');
998 if (newline)
999 *newline = '\0';
1000 limit = avail - col;
1001 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1002 if (err)
1003 goto done;
1004 waddwstr(view->window, wlogmsg);
1005 col += logmsg_width;
1006 while (col <= avail) {
1007 waddch(view->window, ' ');
1008 col++;
1010 done:
1011 free(logmsg0);
1012 free(wlogmsg);
1013 free(author);
1014 free(wauthor);
1015 free(line);
1016 return err;
1019 static struct commit_queue_entry *
1020 alloc_commit_queue_entry(struct got_commit_object *commit,
1021 struct got_object_id *id)
1023 struct commit_queue_entry *entry;
1025 entry = calloc(1, sizeof(*entry));
1026 if (entry == NULL)
1027 return NULL;
1029 entry->id = id;
1030 entry->commit = commit;
1031 return entry;
1034 static void
1035 pop_commit(struct commit_queue *commits)
1037 struct commit_queue_entry *entry;
1039 entry = TAILQ_FIRST(&commits->head);
1040 TAILQ_REMOVE(&commits->head, entry, entry);
1041 got_object_commit_close(entry->commit);
1042 commits->ncommits--;
1043 /* Don't free entry->id! It is owned by the commit graph. */
1044 free(entry);
1047 static void
1048 free_commits(struct commit_queue *commits)
1050 while (!TAILQ_EMPTY(&commits->head))
1051 pop_commit(commits);
1054 static const struct got_error *
1055 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1056 int minqueue, struct got_repository *repo, const char *path)
1058 const struct got_error *err = NULL;
1059 int nqueued = 0;
1062 * We keep all commits open throughout the lifetime of the log
1063 * view in order to avoid having to re-fetch commits from disk
1064 * while updating the display.
1066 while (nqueued < minqueue) {
1067 struct got_object_id *id;
1068 struct got_commit_object *commit;
1069 struct commit_queue_entry *entry;
1070 int errcode;
1072 err = got_commit_graph_iter_next(&id, graph);
1073 if (err) {
1074 if (err->code != GOT_ERR_ITER_NEED_MORE)
1075 break;
1076 err = got_commit_graph_fetch_commits(graph,
1077 minqueue, repo);
1078 if (err)
1079 return err;
1080 continue;
1083 if (id == NULL)
1084 break;
1086 err = got_object_open_as_commit(&commit, repo, id);
1087 if (err)
1088 break;
1089 entry = alloc_commit_queue_entry(commit, id);
1090 if (entry == NULL) {
1091 err = got_error_from_errno();
1092 break;
1095 errcode = pthread_mutex_lock(&tog_mutex);
1096 if (errcode) {
1097 err = got_error_set_errno(errcode);
1098 break;
1101 entry->idx = commits->ncommits;
1102 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1103 nqueued++;
1104 commits->ncommits++;
1106 errcode = pthread_mutex_unlock(&tog_mutex);
1107 if (errcode && err == NULL)
1108 err = got_error_set_errno(errcode);
1111 return err;
1114 static const struct got_error *
1115 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1117 const struct got_error *err = NULL;
1118 struct got_reference *head_ref;
1120 *head_id = NULL;
1122 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1123 if (err)
1124 return err;
1126 err = got_ref_resolve(head_id, repo, head_ref);
1127 got_ref_close(head_ref);
1128 if (err) {
1129 *head_id = NULL;
1130 return err;
1133 return NULL;
1136 static const struct got_error *
1137 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1138 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1139 struct commit_queue *commits, int selected_idx, int limit,
1140 struct got_reflist_head *refs, const char *path, int commits_needed)
1142 const struct got_error *err = NULL;
1143 struct commit_queue_entry *entry;
1144 int ncommits, width;
1145 int author_cols = 10;
1146 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1147 char *refs_str = NULL;
1148 wchar_t *wline;
1150 entry = first;
1151 ncommits = 0;
1152 while (entry) {
1153 if (ncommits == selected_idx) {
1154 *selected = entry;
1155 break;
1157 entry = TAILQ_NEXT(entry, entry);
1158 ncommits++;
1161 if (*selected) {
1162 err = got_object_id_str(&id_str, (*selected)->id);
1163 if (err)
1164 return err;
1165 if (refs) {
1166 err = build_refs_str(&refs_str, refs, (*selected)->id);
1167 if (err)
1168 goto done;
1172 if (commits_needed == 0)
1173 halfdelay(10); /* disable fast refresh */
1175 if (asprintf(&ncommits_str, " [%d/%d] %s",
1176 entry ? entry->idx + 1 : 0, commits->ncommits,
1177 commits_needed > 0 ? "loading... " :
1178 (refs_str ? refs_str : "")) == -1) {
1179 err = got_error_from_errno();
1180 goto done;
1183 if (path && strcmp(path, "/") != 0) {
1184 if (asprintf(&header, "commit %s %s%s",
1185 id_str ? id_str : "........................................",
1186 path, ncommits_str) == -1) {
1187 err = got_error_from_errno();
1188 header = NULL;
1189 goto done;
1191 } else if (asprintf(&header, "commit %s%s",
1192 id_str ? id_str : "........................................",
1193 ncommits_str) == -1) {
1194 err = got_error_from_errno();
1195 header = NULL;
1196 goto done;
1198 err = format_line(&wline, &width, header, view->ncols);
1199 if (err)
1200 goto done;
1202 werase(view->window);
1204 if (view_needs_focus_indication(view))
1205 wstandout(view->window);
1206 waddwstr(view->window, wline);
1207 while (width < view->ncols) {
1208 waddch(view->window, ' ');
1209 width++;
1211 if (view_needs_focus_indication(view))
1212 wstandend(view->window);
1213 free(wline);
1214 if (limit <= 1)
1215 goto done;
1217 /* Grow author column size if necessary. */
1218 entry = first;
1219 ncommits = 0;
1220 while (entry) {
1221 char *author;
1222 wchar_t *wauthor;
1223 int width;
1224 if (ncommits >= limit - 1)
1225 break;
1226 author = strdup(got_object_commit_get_author(entry->commit));
1227 if (author == NULL) {
1228 err = got_error_from_errno();
1229 goto done;
1231 err = format_author(&wauthor, &width, author, COLS);
1232 if (author_cols < width)
1233 author_cols = width;
1234 free(wauthor);
1235 free(author);
1236 entry = TAILQ_NEXT(entry, entry);
1239 entry = first;
1240 *last = first;
1241 ncommits = 0;
1242 while (entry) {
1243 if (ncommits >= limit - 1)
1244 break;
1245 if (ncommits == selected_idx)
1246 wstandout(view->window);
1247 err = draw_commit(view, entry->commit, entry->id, refs,
1248 author_cols);
1249 if (ncommits == selected_idx)
1250 wstandend(view->window);
1251 if (err)
1252 break;
1253 ncommits++;
1254 *last = entry;
1255 entry = TAILQ_NEXT(entry, entry);
1258 view_vborder(view);
1259 done:
1260 free(id_str);
1261 free(refs_str);
1262 free(ncommits_str);
1263 free(header);
1264 return err;
1267 static void
1268 scroll_up(struct tog_view *view,
1269 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1270 struct commit_queue *commits)
1272 struct commit_queue_entry *entry;
1273 int nscrolled = 0;
1275 entry = TAILQ_FIRST(&commits->head);
1276 if (*first_displayed_entry == entry) {
1277 view_flash(view);
1278 return;
1281 entry = *first_displayed_entry;
1282 while (entry && nscrolled < maxscroll) {
1283 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1284 if (entry) {
1285 *first_displayed_entry = entry;
1286 nscrolled++;
1291 static const struct got_error *
1292 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1293 pthread_cond_t *need_commits)
1295 int errcode;
1296 int max_wait = 20;
1298 halfdelay(1); /* fast refresh while loading commits */
1300 while (*commits_needed > 0) {
1301 if (*log_complete)
1302 break;
1304 /* Wake the log thread. */
1305 errcode = pthread_cond_signal(need_commits);
1306 if (errcode)
1307 return got_error_set_errno(errcode);
1308 errcode = pthread_mutex_unlock(&tog_mutex);
1309 if (errcode)
1310 return got_error_set_errno(errcode);
1311 pthread_yield();
1312 errcode = pthread_mutex_lock(&tog_mutex);
1313 if (errcode)
1314 return got_error_set_errno(errcode);
1316 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1318 * Thread is not done yet; lose a key press
1319 * and let the user retry... this way the GUI
1320 * remains interactive while logging deep paths
1321 * with few commits in history.
1323 return NULL;
1327 return NULL;
1330 static const struct got_error *
1331 scroll_down(struct tog_view *view,
1332 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1333 struct commit_queue_entry **last_displayed_entry,
1334 struct commit_queue *commits, int *log_complete, int *commits_needed,
1335 pthread_cond_t *need_commits)
1337 const struct got_error *err = NULL;
1338 struct commit_queue_entry *pentry;
1339 int nscrolled = 0;
1341 if (*last_displayed_entry == NULL)
1342 return NULL;
1344 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1345 if (pentry == NULL && !*log_complete) {
1347 * Ask the log thread for required amount of commits
1348 * plus some amount of pre-fetching.
1350 (*commits_needed) += maxscroll + 20;
1351 err = trigger_log_thread(0, commits_needed, log_complete,
1352 need_commits);
1353 if (err)
1354 return err;
1357 do {
1358 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1359 if (pentry == NULL) {
1360 if (*log_complete)
1361 view_flash(view);
1362 break;
1365 *last_displayed_entry = pentry;
1367 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1368 if (pentry == NULL)
1369 break;
1370 *first_displayed_entry = pentry;
1371 } while (++nscrolled < maxscroll);
1373 return err;
1376 static const struct got_error *
1377 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1378 struct got_commit_object *commit, struct got_object_id *commit_id,
1379 struct tog_view *log_view, struct got_reflist_head *refs,
1380 struct got_repository *repo)
1382 const struct got_error *err;
1383 struct got_object_qid *parent_id;
1384 struct tog_view *diff_view;
1386 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1387 if (diff_view == NULL)
1388 return got_error_from_errno();
1390 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1391 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1392 commit_id, log_view, refs, repo);
1393 if (err == NULL)
1394 *new_view = diff_view;
1395 return err;
1398 static const struct got_error *
1399 browse_commit(struct tog_view **new_view, int begin_x,
1400 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1401 struct got_repository *repo)
1403 const struct got_error *err = NULL;
1404 struct got_tree_object *tree;
1405 struct tog_view *tree_view;
1407 err = got_object_open_as_tree(&tree, repo,
1408 got_object_commit_get_tree_id(entry->commit));
1409 if (err)
1410 return err;
1412 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1413 if (tree_view == NULL)
1414 return got_error_from_errno();
1416 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1417 if (err)
1418 got_object_tree_close(tree);
1419 else
1420 *new_view = tree_view;
1421 return err;
1424 static void *
1425 log_thread(void *arg)
1427 const struct got_error *err = NULL;
1428 int errcode = 0;
1429 struct tog_log_thread_args *a = arg;
1430 int done = 0;
1432 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1433 if (err)
1434 return (void *)err;
1436 while (!done && !err) {
1437 err = queue_commits(a->graph, a->commits, 1, a->repo,
1438 a->in_repo_path);
1439 if (err) {
1440 if (err->code != GOT_ERR_ITER_COMPLETED)
1441 return (void *)err;
1442 err = NULL;
1443 done = 1;
1444 } else if (a->commits_needed > 0)
1445 a->commits_needed--;
1447 errcode = pthread_mutex_lock(&tog_mutex);
1448 if (errcode)
1449 return (void *)got_error_set_errno(errcode);
1451 if (done)
1452 a->log_complete = 1;
1453 else if (*a->quit) {
1454 done = 1;
1455 a->log_complete = 1;
1456 } else if (*a->first_displayed_entry == NULL) {
1457 *a->first_displayed_entry =
1458 TAILQ_FIRST(&a->commits->head);
1459 *a->selected_entry = *a->first_displayed_entry;
1462 if (done)
1463 a->commits_needed = 0;
1464 else if (a->commits_needed == 0) {
1465 errcode = pthread_cond_wait(&a->need_commits,
1466 &tog_mutex);
1467 if (errcode)
1468 err = got_error_set_errno(errcode);
1471 errcode = pthread_mutex_unlock(&tog_mutex);
1472 if (errcode && err == NULL)
1473 err = got_error_set_errno(errcode);
1475 return (void *)err;
1478 static const struct got_error *
1479 stop_log_thread(struct tog_log_view_state *s)
1481 const struct got_error *err = NULL;
1482 int errcode;
1484 if (s->thread) {
1485 s->quit = 1;
1486 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1487 if (errcode)
1488 return got_error_set_errno(errcode);
1489 errcode = pthread_mutex_unlock(&tog_mutex);
1490 if (errcode)
1491 return got_error_set_errno(errcode);
1492 errcode = pthread_join(s->thread, (void **)&err);
1493 if (errcode)
1494 return got_error_set_errno(errcode);
1495 errcode = pthread_mutex_lock(&tog_mutex);
1496 if (errcode)
1497 return got_error_set_errno(errcode);
1498 s->thread = NULL;
1501 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1502 if (errcode && err == NULL)
1503 err = got_error_set_errno(errcode);
1505 if (s->thread_args.repo) {
1506 got_repo_close(s->thread_args.repo);
1507 s->thread_args.repo = NULL;
1510 if (s->thread_args.graph) {
1511 got_commit_graph_close(s->thread_args.graph);
1512 s->thread_args.graph = NULL;
1515 return err;
1518 static const struct got_error *
1519 close_log_view(struct tog_view *view)
1521 const struct got_error *err = NULL;
1522 struct tog_log_view_state *s = &view->state.log;
1524 err = stop_log_thread(s);
1525 free_commits(&s->commits);
1526 free(s->in_repo_path);
1527 s->in_repo_path = NULL;
1528 free(s->start_id);
1529 s->start_id = NULL;
1530 return err;
1533 static const struct got_error *
1534 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1535 struct got_reflist_head *refs, struct got_repository *repo,
1536 const char *path, int check_disk)
1538 const struct got_error *err = NULL;
1539 struct tog_log_view_state *s = &view->state.log;
1540 struct got_repository *thread_repo = NULL;
1541 struct got_commit_graph *thread_graph = NULL;
1542 int errcode;
1544 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1545 if (err != NULL)
1546 goto done;
1548 /* The commit queue only contains commits being displayed. */
1549 TAILQ_INIT(&s->commits.head);
1550 s->commits.ncommits = 0;
1552 s->refs = refs;
1553 s->repo = repo;
1554 s->start_id = got_object_id_dup(start_id);
1555 if (s->start_id == NULL) {
1556 err = got_error_from_errno();
1557 goto done;
1560 view->show = show_log_view;
1561 view->input = input_log_view;
1562 view->close = close_log_view;
1564 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1565 if (err)
1566 goto done;
1567 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1568 0, thread_repo);
1569 if (err)
1570 goto done;
1572 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1573 if (errcode) {
1574 err = got_error_set_errno(errcode);
1575 goto done;
1578 s->thread_args.commits_needed = view->nlines;
1579 s->thread_args.graph = thread_graph;
1580 s->thread_args.commits = &s->commits;
1581 s->thread_args.in_repo_path = s->in_repo_path;
1582 s->thread_args.start_id = s->start_id;
1583 s->thread_args.repo = thread_repo;
1584 s->thread_args.log_complete = 0;
1585 s->thread_args.quit = &s->quit;
1586 s->thread_args.view = view;
1587 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1588 s->thread_args.selected_entry = &s->selected_entry;
1589 done:
1590 if (err)
1591 close_log_view(view);
1592 return err;
1595 static const struct got_error *
1596 show_log_view(struct tog_view *view)
1598 struct tog_log_view_state *s = &view->state.log;
1600 if (s->thread == NULL) {
1601 int errcode = pthread_create(&s->thread, NULL, log_thread,
1602 &s->thread_args);
1603 if (errcode)
1604 return got_error_set_errno(errcode);
1607 return draw_commits(view, &s->last_displayed_entry,
1608 &s->selected_entry, s->first_displayed_entry,
1609 &s->commits, s->selected, view->nlines, s->refs,
1610 s->in_repo_path, s->thread_args.commits_needed);
1613 static const struct got_error *
1614 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1615 struct tog_view **focus_view, struct tog_view *view, int ch)
1617 const struct got_error *err = NULL;
1618 struct tog_log_view_state *s = &view->state.log;
1619 char *parent_path;
1620 struct tog_view *diff_view = NULL, *tree_view = NULL;
1621 int begin_x = 0;
1623 switch (ch) {
1624 case 'q':
1625 s->quit = 1;
1626 break;
1627 case 'k':
1628 case KEY_UP:
1629 case '<':
1630 case ',':
1631 if (s->first_displayed_entry == NULL)
1632 break;
1633 if (s->selected > 0)
1634 s->selected--;
1635 if (s->selected > 0)
1636 break;
1637 scroll_up(view, &s->first_displayed_entry, 1,
1638 &s->commits);
1639 break;
1640 case KEY_PPAGE:
1641 if (s->first_displayed_entry == NULL)
1642 break;
1643 if (TAILQ_FIRST(&s->commits.head) ==
1644 s->first_displayed_entry) {
1645 if (s->selected == 0) {
1646 view_flash(view);
1647 break;
1649 s->selected = 0;
1650 break;
1652 scroll_up(view, &s->first_displayed_entry,
1653 view->nlines, &s->commits);
1654 break;
1655 case 'j':
1656 case KEY_DOWN:
1657 case '>':
1658 case '.':
1659 if (s->first_displayed_entry == NULL)
1660 break;
1661 if (s->selected < MIN(view->nlines - 2,
1662 s->commits.ncommits - 1)) {
1663 s->selected++;
1664 break;
1666 err = scroll_down(view, &s->first_displayed_entry, 1,
1667 &s->last_displayed_entry, &s->commits,
1668 &s->thread_args.log_complete,
1669 &s->thread_args.commits_needed,
1670 &s->thread_args.need_commits);
1671 break;
1672 case KEY_NPAGE: {
1673 struct commit_queue_entry *first;
1674 first = s->first_displayed_entry;
1675 if (first == NULL)
1676 break;
1677 err = scroll_down(view, &s->first_displayed_entry,
1678 view->nlines, &s->last_displayed_entry,
1679 &s->commits, &s->thread_args.log_complete,
1680 &s->thread_args.commits_needed,
1681 &s->thread_args.need_commits);
1682 if (first == s->first_displayed_entry &&
1683 s->selected < MIN(view->nlines - 2,
1684 s->commits.ncommits - 1)) {
1685 /* can't scroll further down */
1686 s->selected = MIN(view->nlines - 2,
1687 s->commits.ncommits - 1);
1689 err = NULL;
1690 break;
1692 case KEY_RESIZE:
1693 if (s->selected > view->nlines - 2)
1694 s->selected = view->nlines - 2;
1695 if (s->selected > s->commits.ncommits - 1)
1696 s->selected = s->commits.ncommits - 1;
1697 break;
1698 case KEY_ENTER:
1699 case '\r':
1700 if (s->selected_entry == NULL)
1701 break;
1702 if (view_is_parent_view(view))
1703 begin_x = view_split_begin_x(view->begin_x);
1704 err = open_diff_view_for_commit(&diff_view, begin_x,
1705 s->selected_entry->commit, s->selected_entry->id,
1706 view, s->refs, s->repo);
1707 if (err)
1708 break;
1709 if (view_is_parent_view(view)) {
1710 err = view_close_child(view);
1711 if (err)
1712 return err;
1713 err = view_set_child(view, diff_view);
1714 if (err) {
1715 view_close(diff_view);
1716 break;
1718 *focus_view = diff_view;
1719 view->child_focussed = 1;
1720 } else
1721 *new_view = diff_view;
1722 break;
1723 case 't':
1724 if (s->selected_entry == NULL)
1725 break;
1726 if (view_is_parent_view(view))
1727 begin_x = view_split_begin_x(view->begin_x);
1728 err = browse_commit(&tree_view, begin_x,
1729 s->selected_entry, s->refs, s->repo);
1730 if (err)
1731 break;
1732 if (view_is_parent_view(view)) {
1733 err = view_close_child(view);
1734 if (err)
1735 return err;
1736 err = view_set_child(view, tree_view);
1737 if (err) {
1738 view_close(tree_view);
1739 break;
1741 *focus_view = tree_view;
1742 view->child_focussed = 1;
1743 } else
1744 *new_view = tree_view;
1745 break;
1746 case KEY_BACKSPACE:
1747 if (strcmp(s->in_repo_path, "/") == 0)
1748 break;
1749 parent_path = dirname(s->in_repo_path);
1750 if (parent_path && strcmp(parent_path, ".") != 0) {
1751 struct tog_view *lv;
1752 err = stop_log_thread(s);
1753 if (err)
1754 return err;
1755 lv = view_open(view->nlines, view->ncols,
1756 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1757 if (lv == NULL)
1758 return got_error_from_errno();
1759 err = open_log_view(lv, s->start_id, s->refs,
1760 s->repo, parent_path, 0);
1761 if (err)
1762 return err;;
1763 if (view_is_parent_view(view))
1764 *new_view = lv;
1765 else {
1766 view_set_child(view->parent, lv);
1767 *focus_view = lv;
1769 return NULL;
1771 break;
1772 default:
1773 break;
1776 return err;
1779 static const struct got_error *
1780 apply_unveil(const char *repo_path, const char *worktree_path)
1782 const struct got_error *error;
1784 if (repo_path && unveil(repo_path, "r") != 0)
1785 return got_error_from_errno();
1787 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1788 return got_error_from_errno();
1790 if (unveil("/tmp", "rwc") != 0)
1791 return got_error_from_errno();
1793 error = got_privsep_unveil_exec_helpers();
1794 if (error != NULL)
1795 return error;
1797 if (unveil(NULL, NULL) != 0)
1798 return got_error_from_errno();
1800 return NULL;
1803 static void
1804 init_curses(void)
1806 initscr();
1807 cbreak();
1808 halfdelay(1); /* Do fast refresh while initial view is loading. */
1809 noecho();
1810 nonl();
1811 intrflush(stdscr, FALSE);
1812 keypad(stdscr, TRUE);
1813 curs_set(0);
1814 signal(SIGWINCH, tog_sigwinch);
1817 static const struct got_error *
1818 cmd_log(int argc, char *argv[])
1820 const struct got_error *error;
1821 struct got_repository *repo = NULL;
1822 struct got_worktree *worktree = NULL;
1823 struct got_reflist_head refs;
1824 struct got_object_id *start_id = NULL;
1825 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1826 char *start_commit = NULL;
1827 int ch;
1828 struct tog_view *view;
1830 #ifndef PROFILE
1831 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1832 NULL) == -1)
1833 err(1, "pledge");
1834 #endif
1836 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1837 switch (ch) {
1838 case 'c':
1839 start_commit = optarg;
1840 break;
1841 case 'r':
1842 repo_path = realpath(optarg, NULL);
1843 if (repo_path == NULL)
1844 err(1, "-r option");
1845 break;
1846 default:
1847 usage_log();
1848 /* NOTREACHED */
1852 argc -= optind;
1853 argv += optind;
1855 if (argc == 0)
1856 path = strdup("");
1857 else if (argc == 1)
1858 path = strdup(argv[0]);
1859 else
1860 usage_log();
1861 if (path == NULL)
1862 return got_error_from_errno();
1864 cwd = getcwd(NULL, 0);
1865 if (cwd == NULL) {
1866 error = got_error_from_errno();
1867 goto done;
1869 if (repo_path == NULL) {
1870 error = got_worktree_open(&worktree, cwd);
1871 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1872 goto done;
1873 if (worktree) {
1874 repo_path =
1875 strdup(got_worktree_get_repo_path(worktree));
1876 } else
1877 repo_path = strdup(cwd);
1878 if (repo_path == NULL) {
1879 error = got_error_from_errno();
1880 goto done;
1884 init_curses();
1886 error = apply_unveil(repo_path,
1887 worktree ? got_worktree_get_root_path(worktree) : NULL);
1888 if (error)
1889 goto done;
1891 error = got_repo_open(&repo, repo_path);
1892 if (error != NULL)
1893 goto done;
1895 if (start_commit == NULL)
1896 error = get_head_commit_id(&start_id, repo);
1897 else
1898 error = got_object_resolve_id_str(&start_id, repo,
1899 start_commit);
1900 if (error != NULL)
1901 goto done;
1903 SIMPLEQ_INIT(&refs);
1904 error = got_ref_list(&refs, repo);
1905 if (error)
1906 goto done;
1908 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1909 if (view == NULL) {
1910 error = got_error_from_errno();
1911 goto done;
1913 error = open_log_view(view, start_id, &refs, repo, path, 1);
1914 if (error)
1915 goto done;
1916 error = view_loop(view);
1917 done:
1918 free(repo_path);
1919 free(cwd);
1920 free(path);
1921 free(start_id);
1922 if (repo)
1923 got_repo_close(repo);
1924 if (worktree)
1925 got_worktree_close(worktree);
1926 got_ref_list_free(&refs);
1927 return error;
1930 __dead static void
1931 usage_diff(void)
1933 endwin();
1934 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1935 getprogname());
1936 exit(1);
1939 static char *
1940 parse_next_line(FILE *f, size_t *len)
1942 char *line;
1943 size_t linelen;
1944 size_t lineno;
1945 const char delim[3] = { '\0', '\0', '\0'};
1947 line = fparseln(f, &linelen, &lineno, delim, 0);
1948 if (len)
1949 *len = linelen;
1950 return line;
1953 static const struct got_error *
1954 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1955 int *last_displayed_line, int *eof, int max_lines,
1956 char * header)
1958 const struct got_error *err;
1959 int nlines = 0, nprinted = 0;
1960 char *line;
1961 size_t len;
1962 wchar_t *wline;
1963 int width;
1965 rewind(f);
1966 werase(view->window);
1968 if (header) {
1969 err = format_line(&wline, &width, header, view->ncols);
1970 if (err) {
1971 return err;
1974 if (view_needs_focus_indication(view))
1975 wstandout(view->window);
1976 waddwstr(view->window, wline);
1977 if (view_needs_focus_indication(view))
1978 wstandend(view->window);
1979 if (width < view->ncols)
1980 waddch(view->window, '\n');
1982 if (max_lines <= 1)
1983 return NULL;
1984 max_lines--;
1987 *eof = 0;
1988 while (nprinted < max_lines) {
1989 line = parse_next_line(f, &len);
1990 if (line == NULL) {
1991 *eof = 1;
1992 break;
1994 if (++nlines < *first_displayed_line) {
1995 free(line);
1996 continue;
1999 err = format_line(&wline, &width, line, view->ncols);
2000 if (err) {
2001 free(line);
2002 return err;
2004 waddwstr(view->window, wline);
2005 if (width < view->ncols)
2006 waddch(view->window, '\n');
2007 if (++nprinted == 1)
2008 *first_displayed_line = nlines;
2009 free(line);
2010 free(wline);
2011 wline = NULL;
2013 *last_displayed_line = nlines;
2015 view_vborder(view);
2017 return NULL;
2020 static char *
2021 get_datestr(time_t *time, char *datebuf)
2023 char *p, *s = ctime_r(time, datebuf);
2024 p = strchr(s, '\n');
2025 if (p)
2026 *p = '\0';
2027 return s;
2030 static const struct got_error *
2031 write_commit_info(struct got_object_id *commit_id,
2032 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2034 const struct got_error *err = NULL;
2035 char datebuf[26];
2036 struct got_commit_object *commit;
2037 char *id_str = NULL;
2038 time_t committer_time;
2039 const char *author, *committer;
2040 char *refs_str = NULL;
2042 if (refs) {
2043 err = build_refs_str(&refs_str, refs, commit_id);
2044 if (err)
2045 return err;
2048 err = got_object_open_as_commit(&commit, repo, commit_id);
2049 if (err)
2050 return err;
2052 err = got_object_id_str(&id_str, commit_id);
2053 if (err) {
2054 err = got_error_from_errno();
2055 goto done;
2058 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2059 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2060 err = got_error_from_errno();
2061 goto done;
2063 if (fprintf(outfile, "from: %s\n",
2064 got_object_commit_get_author(commit)) < 0) {
2065 err = got_error_from_errno();
2066 goto done;
2068 committer_time = got_object_commit_get_committer_time(commit);
2069 if (fprintf(outfile, "date: %s UTC\n",
2070 get_datestr(&committer_time, datebuf)) < 0) {
2071 err = got_error_from_errno();
2072 goto done;
2074 author = got_object_commit_get_author(commit);
2075 committer = got_object_commit_get_committer(commit);
2076 if (strcmp(author, committer) != 0 &&
2077 fprintf(outfile, "via: %s\n", committer) < 0) {
2078 err = got_error_from_errno();
2079 goto done;
2081 if (fprintf(outfile, "%s\n",
2082 got_object_commit_get_logmsg(commit)) < 0) {
2083 err = got_error_from_errno();
2084 goto done;
2086 done:
2087 free(id_str);
2088 free(refs_str);
2089 got_object_commit_close(commit);
2090 return err;
2093 static const struct got_error *
2094 create_diff(struct tog_diff_view_state *s)
2096 const struct got_error *err = NULL;
2097 FILE *f = NULL;
2098 int obj_type;
2100 f = got_opentemp();
2101 if (f == NULL) {
2102 err = got_error_from_errno();
2103 goto done;
2105 if (s->f && fclose(s->f) != 0) {
2106 err = got_error_from_errno();
2107 goto done;
2109 s->f = f;
2111 if (s->id1)
2112 err = got_object_get_type(&obj_type, s->repo, s->id1);
2113 else
2114 err = got_object_get_type(&obj_type, s->repo, s->id2);
2115 if (err)
2116 goto done;
2118 switch (obj_type) {
2119 case GOT_OBJ_TYPE_BLOB:
2120 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2121 s->diff_context, s->repo, f);
2122 break;
2123 case GOT_OBJ_TYPE_TREE:
2124 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2125 s->diff_context, s->repo, f);
2126 break;
2127 case GOT_OBJ_TYPE_COMMIT: {
2128 const struct got_object_id_queue *parent_ids;
2129 struct got_object_qid *pid;
2130 struct got_commit_object *commit2;
2132 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2133 if (err)
2134 break;
2135 /* Show commit info if we're diffing to a parent/root commit. */
2136 if (s->id1 == NULL)
2137 write_commit_info(s->id2, s->refs, s->repo, f);
2138 else {
2139 parent_ids = got_object_commit_get_parent_ids(commit2);
2140 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2141 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2142 write_commit_info(s->id2, s->refs,
2143 s->repo, f);
2144 break;
2148 got_object_commit_close(commit2);
2150 err = got_diff_objects_as_commits(s->id1, s->id2,
2151 s->diff_context, s->repo, f);
2152 break;
2154 default:
2155 err = got_error(GOT_ERR_OBJ_TYPE);
2156 break;
2158 done:
2159 if (f && fflush(f) != 0 && err == NULL)
2160 err = got_error_from_errno();
2161 return err;
2164 static void
2165 diff_view_indicate_progress(struct tog_view *view)
2167 werase(view->window);
2168 waddstr(view->window, "diffing...");
2169 update_panels();
2170 doupdate();
2173 static const struct got_error *
2174 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2175 struct got_object_id *id2, struct tog_view *log_view,
2176 struct got_reflist_head *refs, struct got_repository *repo)
2178 const struct got_error *err;
2180 if (id1 != NULL && id2 != NULL) {
2181 int type1, type2;
2182 err = got_object_get_type(&type1, repo, id1);
2183 if (err)
2184 return err;
2185 err = got_object_get_type(&type2, repo, id2);
2186 if (err)
2187 return err;
2189 if (type1 != type2)
2190 return got_error(GOT_ERR_OBJ_TYPE);
2193 if (id1) {
2194 view->state.diff.id1 = got_object_id_dup(id1);
2195 if (view->state.diff.id1 == NULL)
2196 return got_error_from_errno();
2197 } else
2198 view->state.diff.id1 = NULL;
2200 view->state.diff.id2 = got_object_id_dup(id2);
2201 if (view->state.diff.id2 == NULL) {
2202 free(view->state.diff.id1);
2203 view->state.diff.id1 = NULL;
2204 return got_error_from_errno();
2206 view->state.diff.f = NULL;
2207 view->state.diff.first_displayed_line = 1;
2208 view->state.diff.last_displayed_line = view->nlines;
2209 view->state.diff.diff_context = 3;
2210 view->state.diff.log_view = log_view;
2211 view->state.diff.repo = repo;
2212 view->state.diff.refs = refs;
2214 if (log_view && view_is_splitscreen(view))
2215 show_log_view(log_view); /* draw vborder */
2216 diff_view_indicate_progress(view);
2218 err = create_diff(&view->state.diff);
2219 if (err) {
2220 free(view->state.diff.id1);
2221 view->state.diff.id1 = NULL;
2222 free(view->state.diff.id2);
2223 view->state.diff.id2 = NULL;
2224 return err;
2227 view->show = show_diff_view;
2228 view->input = input_diff_view;
2229 view->close = close_diff_view;
2231 return NULL;
2234 static const struct got_error *
2235 close_diff_view(struct tog_view *view)
2237 const struct got_error *err = NULL;
2239 free(view->state.diff.id1);
2240 view->state.diff.id1 = NULL;
2241 free(view->state.diff.id2);
2242 view->state.diff.id2 = NULL;
2243 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2244 err = got_error_from_errno();
2245 return err;
2248 static const struct got_error *
2249 show_diff_view(struct tog_view *view)
2251 const struct got_error *err;
2252 struct tog_diff_view_state *s = &view->state.diff;
2253 char *id_str1 = NULL, *id_str2, *header;
2255 if (s->id1) {
2256 err = got_object_id_str(&id_str1, s->id1);
2257 if (err)
2258 return err;
2260 err = got_object_id_str(&id_str2, s->id2);
2261 if (err)
2262 return err;
2264 if (asprintf(&header, "diff %s %s",
2265 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2266 err = got_error_from_errno();
2267 free(id_str1);
2268 free(id_str2);
2269 return err;
2271 free(id_str1);
2272 free(id_str2);
2274 return draw_file(view, s->f, &s->first_displayed_line,
2275 &s->last_displayed_line, &s->eof, view->nlines,
2276 header);
2279 static const struct got_error *
2280 set_selected_commit(struct tog_diff_view_state *s,
2281 struct commit_queue_entry *entry)
2283 const struct got_error *err;
2284 const struct got_object_id_queue *parent_ids;
2285 struct got_commit_object *selected_commit;
2286 struct got_object_qid *pid;
2288 free(s->id2);
2289 s->id2 = got_object_id_dup(entry->id);
2290 if (s->id2 == NULL)
2291 return got_error_from_errno();
2293 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2294 if (err)
2295 return err;
2296 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2297 free(s->id1);
2298 pid = SIMPLEQ_FIRST(parent_ids);
2299 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2300 got_object_commit_close(selected_commit);
2301 return NULL;
2304 static const struct got_error *
2305 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2306 struct tog_view **focus_view, struct tog_view *view, int ch)
2308 const struct got_error *err = NULL;
2309 struct tog_diff_view_state *s = &view->state.diff;
2310 struct tog_log_view_state *ls;
2311 struct commit_queue_entry *entry;
2312 int i;
2314 switch (ch) {
2315 case 'k':
2316 case KEY_UP:
2317 if (s->first_displayed_line > 1)
2318 s->first_displayed_line--;
2319 else
2320 view_flash(view);
2321 break;
2322 case KEY_PPAGE:
2323 if (s->first_displayed_line == 1) {
2324 view_flash(view);
2325 break;
2327 i = 0;
2328 while (i++ < view->nlines - 1 &&
2329 s->first_displayed_line > 1)
2330 s->first_displayed_line--;
2331 break;
2332 case 'j':
2333 case KEY_DOWN:
2334 if (!s->eof)
2335 s->first_displayed_line++;
2336 else
2337 view_flash(view);
2338 break;
2339 case KEY_NPAGE:
2340 case ' ':
2341 if (s->eof) {
2342 view_flash(view);
2343 break;
2345 i = 0;
2346 while (!s->eof && i++ < view->nlines - 1) {
2347 char *line;
2348 line = parse_next_line(s->f, NULL);
2349 s->first_displayed_line++;
2350 if (line == NULL)
2351 break;
2353 break;
2354 case '[':
2355 if (s->diff_context > 0) {
2356 s->diff_context--;
2357 diff_view_indicate_progress(view);
2358 err = create_diff(s);
2360 break;
2361 case ']':
2362 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2363 s->diff_context++;
2364 diff_view_indicate_progress(view);
2365 err = create_diff(s);
2367 break;
2368 case '<':
2369 case ',':
2370 if (s->log_view == NULL)
2371 break;
2372 ls = &s->log_view->state.log;
2373 entry = TAILQ_PREV(ls->selected_entry,
2374 commit_queue_head, entry);
2375 if (entry == NULL)
2376 break;
2378 err = input_log_view(NULL, NULL, NULL, s->log_view,
2379 KEY_UP);
2380 if (err)
2381 break;
2383 err = set_selected_commit(s, entry);
2384 if (err)
2385 break;
2387 s->first_displayed_line = 1;
2388 s->last_displayed_line = view->nlines;
2390 diff_view_indicate_progress(view);
2391 err = create_diff(s);
2392 break;
2393 case '>':
2394 case '.':
2395 if (s->log_view == NULL)
2396 break;
2397 ls = &s->log_view->state.log;
2399 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2400 ls->thread_args.commits_needed++;
2402 /* Display "loading..." in log view. */
2403 show_log_view(s->log_view);
2404 update_panels();
2405 doupdate();
2407 err = trigger_log_thread(1 /* load_all */,
2408 &ls->thread_args.commits_needed,
2409 &ls->thread_args.log_complete,
2410 &ls->thread_args.need_commits);
2411 if (err)
2412 break;
2414 err = input_log_view(NULL, NULL, NULL, s->log_view,
2415 KEY_DOWN);
2416 if (err)
2417 break;
2419 entry = TAILQ_NEXT(ls->selected_entry, entry);
2420 if (entry == NULL)
2421 break;
2423 err = set_selected_commit(s, entry);
2424 if (err)
2425 break;
2427 s->first_displayed_line = 1;
2428 s->last_displayed_line = view->nlines;
2430 diff_view_indicate_progress(view);
2431 err = create_diff(s);
2432 break;
2433 default:
2434 break;
2437 return err;
2440 static const struct got_error *
2441 cmd_diff(int argc, char *argv[])
2443 const struct got_error *error = NULL;
2444 struct got_repository *repo = NULL;
2445 struct got_reflist_head refs;
2446 struct got_object_id *id1 = NULL, *id2 = NULL;
2447 char *repo_path = NULL;
2448 char *id_str1 = NULL, *id_str2 = NULL;
2449 int ch;
2450 struct tog_view *view;
2452 #ifndef PROFILE
2453 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2454 NULL) == -1)
2455 err(1, "pledge");
2456 #endif
2458 while ((ch = getopt(argc, argv, "")) != -1) {
2459 switch (ch) {
2460 default:
2461 usage_diff();
2462 /* NOTREACHED */
2466 argc -= optind;
2467 argv += optind;
2469 if (argc == 0) {
2470 usage_diff(); /* TODO show local worktree changes */
2471 } else if (argc == 2) {
2472 repo_path = getcwd(NULL, 0);
2473 if (repo_path == NULL)
2474 return got_error_from_errno();
2475 id_str1 = argv[0];
2476 id_str2 = argv[1];
2477 } else if (argc == 3) {
2478 repo_path = realpath(argv[0], NULL);
2479 if (repo_path == NULL)
2480 return got_error_from_errno();
2481 id_str1 = argv[1];
2482 id_str2 = argv[2];
2483 } else
2484 usage_diff();
2486 init_curses();
2488 error = apply_unveil(repo_path, NULL);
2489 if (error)
2490 goto done;
2492 error = got_repo_open(&repo, repo_path);
2493 free(repo_path);
2494 if (error)
2495 goto done;
2497 error = got_object_resolve_id_str(&id1, repo, id_str1);
2498 if (error)
2499 goto done;
2501 error = got_object_resolve_id_str(&id2, repo, id_str2);
2502 if (error)
2503 goto done;
2505 SIMPLEQ_INIT(&refs);
2506 error = got_ref_list(&refs, repo);
2507 if (error)
2508 goto done;
2510 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2511 if (view == NULL) {
2512 error = got_error_from_errno();
2513 goto done;
2515 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2516 if (error)
2517 goto done;
2518 error = view_loop(view);
2519 done:
2520 got_repo_close(repo);
2521 got_ref_list_free(&refs);
2522 return error;
2525 __dead static void
2526 usage_blame(void)
2528 endwin();
2529 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2530 getprogname());
2531 exit(1);
2534 struct tog_blame_line {
2535 int annotated;
2536 struct got_object_id *id;
2539 static const struct got_error *
2540 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2541 const char *path, struct tog_blame_line *lines, int nlines,
2542 int blame_complete, int selected_line, int *first_displayed_line,
2543 int *last_displayed_line, int *eof, int max_lines)
2545 const struct got_error *err;
2546 int lineno = 0, nprinted = 0;
2547 char *line;
2548 size_t len;
2549 wchar_t *wline;
2550 int width, wlimit;
2551 struct tog_blame_line *blame_line;
2552 struct got_object_id *prev_id = NULL;
2553 char *id_str;
2555 err = got_object_id_str(&id_str, id);
2556 if (err)
2557 return err;
2559 rewind(f);
2560 werase(view->window);
2562 if (asprintf(&line, "commit %s", id_str) == -1) {
2563 err = got_error_from_errno();
2564 free(id_str);
2565 return err;
2568 err = format_line(&wline, &width, line, view->ncols);
2569 free(line);
2570 line = NULL;
2571 if (view_needs_focus_indication(view))
2572 wstandout(view->window);
2573 waddwstr(view->window, wline);
2574 if (view_needs_focus_indication(view))
2575 wstandend(view->window);
2576 free(wline);
2577 wline = NULL;
2578 if (width < view->ncols)
2579 waddch(view->window, '\n');
2581 if (asprintf(&line, "[%d/%d] %s%s",
2582 *first_displayed_line - 1 + selected_line, nlines,
2583 blame_complete ? "" : "annotating... ", path) == -1) {
2584 free(id_str);
2585 return got_error_from_errno();
2587 free(id_str);
2588 err = format_line(&wline, &width, line, view->ncols);
2589 free(line);
2590 line = NULL;
2591 if (err)
2592 return err;
2593 waddwstr(view->window, wline);
2594 free(wline);
2595 wline = NULL;
2596 if (width < view->ncols)
2597 waddch(view->window, '\n');
2599 *eof = 0;
2600 while (nprinted < max_lines - 2) {
2601 line = parse_next_line(f, &len);
2602 if (line == NULL) {
2603 *eof = 1;
2604 break;
2606 if (++lineno < *first_displayed_line) {
2607 free(line);
2608 continue;
2611 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2612 err = format_line(&wline, &width, line, wlimit);
2613 if (err) {
2614 free(line);
2615 return err;
2618 if (view->focussed && nprinted == selected_line - 1)
2619 wstandout(view->window);
2621 blame_line = &lines[lineno - 1];
2622 if (blame_line->annotated && prev_id &&
2623 got_object_id_cmp(prev_id, blame_line->id) == 0)
2624 waddstr(view->window, " ");
2625 else if (blame_line->annotated) {
2626 char *id_str;
2627 err = got_object_id_str(&id_str, blame_line->id);
2628 if (err) {
2629 free(line);
2630 free(wline);
2631 return err;
2633 wprintw(view->window, "%.8s ", id_str);
2634 free(id_str);
2635 prev_id = blame_line->id;
2636 } else {
2637 waddstr(view->window, "........ ");
2638 prev_id = NULL;
2641 waddwstr(view->window, wline);
2642 while (width < wlimit) {
2643 waddch(view->window, ' ');
2644 width++;
2646 if (view->focussed && nprinted == selected_line - 1)
2647 wstandend(view->window);
2648 if (++nprinted == 1)
2649 *first_displayed_line = lineno;
2650 free(line);
2651 free(wline);
2652 wline = NULL;
2654 *last_displayed_line = lineno;
2656 view_vborder(view);
2658 return NULL;
2661 static const struct got_error *
2662 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2664 const struct got_error *err = NULL;
2665 struct tog_blame_cb_args *a = arg;
2666 struct tog_blame_line *line;
2667 int errcode;
2669 if (nlines != a->nlines ||
2670 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2671 return got_error(GOT_ERR_RANGE);
2673 errcode = pthread_mutex_lock(&tog_mutex);
2674 if (errcode)
2675 return got_error_set_errno(errcode);
2677 if (*a->quit) { /* user has quit the blame view */
2678 err = got_error(GOT_ERR_ITER_COMPLETED);
2679 goto done;
2682 if (lineno == -1)
2683 goto done; /* no change in this commit */
2685 line = &a->lines[lineno - 1];
2686 if (line->annotated)
2687 goto done;
2689 line->id = got_object_id_dup(id);
2690 if (line->id == NULL) {
2691 err = got_error_from_errno();
2692 goto done;
2694 line->annotated = 1;
2695 done:
2696 errcode = pthread_mutex_unlock(&tog_mutex);
2697 if (errcode)
2698 err = got_error_set_errno(errcode);
2699 return err;
2702 static void *
2703 blame_thread(void *arg)
2705 const struct got_error *err;
2706 struct tog_blame_thread_args *ta = arg;
2707 struct tog_blame_cb_args *a = ta->cb_args;
2708 int errcode;
2710 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2711 blame_cb, ta->cb_args);
2713 errcode = pthread_mutex_lock(&tog_mutex);
2714 if (errcode)
2715 return (void *)got_error_set_errno(errcode);
2717 got_repo_close(ta->repo);
2718 ta->repo = NULL;
2719 *ta->complete = 1;
2721 errcode = pthread_mutex_unlock(&tog_mutex);
2722 if (errcode && err == NULL)
2723 err = got_error_set_errno(errcode);
2725 return (void *)err;
2728 static struct got_object_id *
2729 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2730 int selected_line)
2732 struct tog_blame_line *line;
2734 line = &lines[first_displayed_line - 1 + selected_line - 1];
2735 if (!line->annotated)
2736 return NULL;
2738 return line->id;
2741 static const struct got_error *
2742 stop_blame(struct tog_blame *blame)
2744 const struct got_error *err = NULL;
2745 int i;
2747 if (blame->thread) {
2748 int errcode;
2749 errcode = pthread_mutex_unlock(&tog_mutex);
2750 if (errcode)
2751 return got_error_set_errno(errcode);
2752 errcode = pthread_join(blame->thread, (void **)&err);
2753 if (errcode)
2754 return got_error_set_errno(errcode);
2755 errcode = pthread_mutex_lock(&tog_mutex);
2756 if (errcode)
2757 return got_error_set_errno(errcode);
2758 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2759 err = NULL;
2760 blame->thread = NULL;
2762 if (blame->thread_args.repo) {
2763 got_repo_close(blame->thread_args.repo);
2764 blame->thread_args.repo = NULL;
2766 if (blame->f) {
2767 if (fclose(blame->f) != 0 && err == NULL)
2768 err = got_error_from_errno();
2769 blame->f = NULL;
2771 if (blame->lines) {
2772 for (i = 0; i < blame->nlines; i++)
2773 free(blame->lines[i].id);
2774 free(blame->lines);
2775 blame->lines = NULL;
2777 free(blame->cb_args.commit_id);
2778 blame->cb_args.commit_id = NULL;
2780 return err;
2783 static const struct got_error *
2784 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2785 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2786 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2787 struct got_repository *repo)
2789 const struct got_error *err = NULL;
2790 struct got_blob_object *blob = NULL;
2791 struct got_repository *thread_repo = NULL;
2792 struct got_object_id *obj_id = NULL;
2793 int obj_type;
2795 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2796 if (err)
2797 return err;
2798 if (obj_id == NULL)
2799 return got_error(GOT_ERR_NO_OBJ);
2801 err = got_object_get_type(&obj_type, repo, obj_id);
2802 if (err)
2803 goto done;
2805 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2806 err = got_error(GOT_ERR_OBJ_TYPE);
2807 goto done;
2810 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2811 if (err)
2812 goto done;
2813 blame->f = got_opentemp();
2814 if (blame->f == NULL) {
2815 err = got_error_from_errno();
2816 goto done;
2818 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2819 blame->f, blob);
2820 if (err)
2821 goto done;
2823 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2824 if (blame->lines == NULL) {
2825 err = got_error_from_errno();
2826 goto done;
2829 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2830 if (err)
2831 goto done;
2833 blame->cb_args.view = view;
2834 blame->cb_args.lines = blame->lines;
2835 blame->cb_args.nlines = blame->nlines;
2836 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2837 if (blame->cb_args.commit_id == NULL) {
2838 err = got_error_from_errno();
2839 goto done;
2841 blame->cb_args.quit = done;
2843 blame->thread_args.path = path;
2844 blame->thread_args.repo = thread_repo;
2845 blame->thread_args.cb_args = &blame->cb_args;
2846 blame->thread_args.complete = blame_complete;
2847 *blame_complete = 0;
2849 done:
2850 if (blob)
2851 got_object_blob_close(blob);
2852 free(obj_id);
2853 if (err)
2854 stop_blame(blame);
2855 return err;
2858 static const struct got_error *
2859 open_blame_view(struct tog_view *view, char *path,
2860 struct got_object_id *commit_id, struct got_reflist_head *refs,
2861 struct got_repository *repo)
2863 const struct got_error *err = NULL;
2864 struct tog_blame_view_state *s = &view->state.blame;
2866 SIMPLEQ_INIT(&s->blamed_commits);
2868 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2869 if (err)
2870 return err;
2872 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2873 s->first_displayed_line = 1;
2874 s->last_displayed_line = view->nlines;
2875 s->selected_line = 1;
2876 s->blame_complete = 0;
2877 s->path = path;
2878 if (s->path == NULL)
2879 return got_error_from_errno();
2880 s->repo = repo;
2881 s->refs = refs;
2882 s->commit_id = commit_id;
2883 memset(&s->blame, 0, sizeof(s->blame));
2885 view->show = show_blame_view;
2886 view->input = input_blame_view;
2887 view->close = close_blame_view;
2889 return run_blame(&s->blame, view, &s->blame_complete,
2890 &s->first_displayed_line, &s->last_displayed_line,
2891 &s->selected_line, &s->done, &s->eof, s->path,
2892 s->blamed_commit->id, s->repo);
2895 static const struct got_error *
2896 close_blame_view(struct tog_view *view)
2898 const struct got_error *err = NULL;
2899 struct tog_blame_view_state *s = &view->state.blame;
2901 if (s->blame.thread)
2902 err = stop_blame(&s->blame);
2904 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2905 struct got_object_qid *blamed_commit;
2906 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2907 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2908 got_object_qid_free(blamed_commit);
2911 free(s->path);
2913 return err;
2916 static const struct got_error *
2917 show_blame_view(struct tog_view *view)
2919 const struct got_error *err = NULL;
2920 struct tog_blame_view_state *s = &view->state.blame;
2921 int errcode;
2923 if (s->blame.thread == NULL) {
2924 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2925 &s->blame.thread_args);
2926 if (errcode)
2927 return got_error_set_errno(errcode);
2930 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2931 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2932 s->selected_line, &s->first_displayed_line,
2933 &s->last_displayed_line, &s->eof, view->nlines);
2935 view_vborder(view);
2936 return err;
2939 static const struct got_error *
2940 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2941 struct tog_view **focus_view, struct tog_view *view, int ch)
2943 const struct got_error *err = NULL, *thread_err = NULL;
2944 struct tog_view *diff_view;
2945 struct tog_blame_view_state *s = &view->state.blame;
2946 int begin_x = 0;
2948 switch (ch) {
2949 case 'q':
2950 s->done = 1;
2951 break;
2952 case 'k':
2953 case KEY_UP:
2954 if (s->selected_line > 1)
2955 s->selected_line--;
2956 else if (s->selected_line == 1 &&
2957 s->first_displayed_line > 1)
2958 s->first_displayed_line--;
2959 else
2960 view_flash(view);
2961 break;
2962 case KEY_PPAGE:
2963 if (s->first_displayed_line == 1) {
2964 if (s->selected_line == 1) {
2965 view_flash(view);
2966 break;
2968 s->selected_line = 1;
2969 break;
2971 if (s->first_displayed_line > view->nlines - 2)
2972 s->first_displayed_line -=
2973 (view->nlines - 2);
2974 else
2975 s->first_displayed_line = 1;
2976 break;
2977 case 'j':
2978 case KEY_DOWN:
2979 if (s->selected_line < view->nlines - 2 &&
2980 s->first_displayed_line +
2981 s->selected_line <= s->blame.nlines)
2982 s->selected_line++;
2983 else if (s->last_displayed_line <
2984 s->blame.nlines)
2985 s->first_displayed_line++;
2986 else
2987 view_flash(view);
2988 break;
2989 case 'b':
2990 case 'p': {
2991 struct got_object_id *id = NULL;
2992 id = get_selected_commit_id(s->blame.lines,
2993 s->first_displayed_line, s->selected_line);
2994 if (id == NULL)
2995 break;
2996 if (ch == 'p') {
2997 struct got_commit_object *commit;
2998 struct got_object_qid *pid;
2999 struct got_object_id *blob_id = NULL;
3000 int obj_type;
3001 err = got_object_open_as_commit(&commit,
3002 s->repo, id);
3003 if (err)
3004 break;
3005 pid = SIMPLEQ_FIRST(
3006 got_object_commit_get_parent_ids(commit));
3007 if (pid == NULL) {
3008 got_object_commit_close(commit);
3009 break;
3011 /* Check if path history ends here. */
3012 err = got_object_id_by_path(&blob_id, s->repo,
3013 pid->id, s->path);
3014 if (err) {
3015 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3016 err = NULL;
3017 got_object_commit_close(commit);
3018 break;
3020 err = got_object_get_type(&obj_type, s->repo,
3021 blob_id);
3022 free(blob_id);
3023 /* Can't blame non-blob type objects. */
3024 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3025 got_object_commit_close(commit);
3026 break;
3028 err = got_object_qid_alloc(&s->blamed_commit,
3029 pid->id);
3030 got_object_commit_close(commit);
3031 } else {
3032 if (got_object_id_cmp(id,
3033 s->blamed_commit->id) == 0)
3034 break;
3035 err = got_object_qid_alloc(&s->blamed_commit,
3036 id);
3038 if (err)
3039 break;
3040 s->done = 1;
3041 thread_err = stop_blame(&s->blame);
3042 s->done = 0;
3043 if (thread_err)
3044 break;
3045 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3046 s->blamed_commit, entry);
3047 err = run_blame(&s->blame, view, &s->blame_complete,
3048 &s->first_displayed_line, &s->last_displayed_line,
3049 &s->selected_line, &s->done, &s->eof,
3050 s->path, s->blamed_commit->id, s->repo);
3051 if (err)
3052 break;
3053 break;
3055 case 'B': {
3056 struct got_object_qid *first;
3057 first = SIMPLEQ_FIRST(&s->blamed_commits);
3058 if (!got_object_id_cmp(first->id, s->commit_id))
3059 break;
3060 s->done = 1;
3061 thread_err = stop_blame(&s->blame);
3062 s->done = 0;
3063 if (thread_err)
3064 break;
3065 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3066 got_object_qid_free(s->blamed_commit);
3067 s->blamed_commit =
3068 SIMPLEQ_FIRST(&s->blamed_commits);
3069 err = run_blame(&s->blame, view, &s->blame_complete,
3070 &s->first_displayed_line, &s->last_displayed_line,
3071 &s->selected_line, &s->done, &s->eof, s->path,
3072 s->blamed_commit->id, s->repo);
3073 if (err)
3074 break;
3075 break;
3077 case KEY_ENTER:
3078 case '\r': {
3079 struct got_object_id *id = NULL;
3080 struct got_object_qid *pid;
3081 struct got_commit_object *commit = NULL;
3082 id = get_selected_commit_id(s->blame.lines,
3083 s->first_displayed_line, s->selected_line);
3084 if (id == NULL)
3085 break;
3086 err = got_object_open_as_commit(&commit, s->repo, id);
3087 if (err)
3088 break;
3089 pid = SIMPLEQ_FIRST(
3090 got_object_commit_get_parent_ids(commit));
3091 if (view_is_parent_view(view))
3092 begin_x = view_split_begin_x(view->begin_x);
3093 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3094 if (diff_view == NULL) {
3095 got_object_commit_close(commit);
3096 err = got_error_from_errno();
3097 break;
3099 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3100 id, NULL, s->refs, s->repo);
3101 got_object_commit_close(commit);
3102 if (err) {
3103 view_close(diff_view);
3104 break;
3106 if (view_is_parent_view(view)) {
3107 err = view_close_child(view);
3108 if (err)
3109 break;
3110 err = view_set_child(view, diff_view);
3111 if (err) {
3112 view_close(diff_view);
3113 break;
3115 *focus_view = diff_view;
3116 view->child_focussed = 1;
3117 } else
3118 *new_view = diff_view;
3119 if (err)
3120 break;
3121 break;
3123 case KEY_NPAGE:
3124 case ' ':
3125 if (s->last_displayed_line >= s->blame.nlines &&
3126 s->selected_line >= MIN(s->blame.nlines,
3127 view->nlines - 2)) {
3128 view_flash(view);
3129 break;
3131 if (s->last_displayed_line >= s->blame.nlines &&
3132 s->selected_line < view->nlines - 2) {
3133 s->selected_line = MIN(s->blame.nlines,
3134 view->nlines - 2);
3135 break;
3137 if (s->last_displayed_line + view->nlines - 2
3138 <= s->blame.nlines)
3139 s->first_displayed_line +=
3140 view->nlines - 2;
3141 else
3142 s->first_displayed_line =
3143 s->blame.nlines -
3144 (view->nlines - 3);
3145 break;
3146 case KEY_RESIZE:
3147 if (s->selected_line > view->nlines - 2) {
3148 s->selected_line = MIN(s->blame.nlines,
3149 view->nlines - 2);
3151 break;
3152 default:
3153 break;
3155 return thread_err ? thread_err : err;
3158 static const struct got_error *
3159 cmd_blame(int argc, char *argv[])
3161 const struct got_error *error;
3162 struct got_repository *repo = NULL;
3163 struct got_reflist_head refs;
3164 struct got_worktree *worktree = NULL;
3165 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3166 struct got_object_id *commit_id = NULL;
3167 char *commit_id_str = NULL;
3168 int ch;
3169 struct tog_view *view;
3171 #ifndef PROFILE
3172 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3173 NULL) == -1)
3174 err(1, "pledge");
3175 #endif
3177 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3178 switch (ch) {
3179 case 'c':
3180 commit_id_str = optarg;
3181 break;
3182 case 'r':
3183 repo_path = realpath(optarg, NULL);
3184 if (repo_path == NULL)
3185 err(1, "-r option");
3186 break;
3187 default:
3188 usage_blame();
3189 /* NOTREACHED */
3193 argc -= optind;
3194 argv += optind;
3196 if (argc == 1)
3197 path = argv[0];
3198 else
3199 usage_blame();
3201 cwd = getcwd(NULL, 0);
3202 if (cwd == NULL) {
3203 error = got_error_from_errno();
3204 goto done;
3206 if (repo_path == NULL) {
3207 error = got_worktree_open(&worktree, cwd);
3208 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3209 goto done;
3210 else
3211 error = NULL;
3212 if (worktree) {
3213 repo_path =
3214 strdup(got_worktree_get_repo_path(worktree));
3215 if (repo_path == NULL)
3216 error = got_error_from_errno();
3217 if (error)
3218 goto done;
3219 } else {
3220 repo_path = strdup(cwd);
3221 if (repo_path == NULL) {
3222 error = got_error_from_errno();
3223 goto done;
3228 init_curses();
3230 error = apply_unveil(repo_path, NULL);
3231 if (error)
3232 goto done;
3234 error = got_repo_open(&repo, repo_path);
3235 if (error != NULL)
3236 goto done;
3238 if (worktree) {
3239 const char *prefix = got_worktree_get_path_prefix(worktree);
3240 char *p, *worktree_subdir = cwd +
3241 strlen(got_worktree_get_root_path(worktree));
3242 if (asprintf(&p, "%s%s%s%s%s",
3243 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3244 worktree_subdir, worktree_subdir[0] ? "/" : "",
3245 path) == -1) {
3246 error = got_error_from_errno();
3247 goto done;
3249 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3250 free(p);
3251 } else {
3252 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3254 if (error)
3255 goto done;
3257 if (commit_id_str == NULL) {
3258 struct got_reference *head_ref;
3259 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3260 if (error != NULL)
3261 goto done;
3262 error = got_ref_resolve(&commit_id, repo, head_ref);
3263 got_ref_close(head_ref);
3264 } else {
3265 error = got_object_resolve_id_str(&commit_id, repo,
3266 commit_id_str);
3268 if (error != NULL)
3269 goto done;
3271 SIMPLEQ_INIT(&refs);
3272 error = got_ref_list(&refs, repo);
3273 if (error)
3274 goto done;
3276 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3277 if (view == NULL) {
3278 error = got_error_from_errno();
3279 goto done;
3281 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3282 if (error)
3283 goto done;
3284 error = view_loop(view);
3285 done:
3286 free(repo_path);
3287 free(cwd);
3288 free(commit_id);
3289 if (worktree)
3290 got_worktree_close(worktree);
3291 if (repo)
3292 got_repo_close(repo);
3293 got_ref_list_free(&refs);
3294 return error;
3297 static const struct got_error *
3298 draw_tree_entries(struct tog_view *view,
3299 struct got_tree_entry **first_displayed_entry,
3300 struct got_tree_entry **last_displayed_entry,
3301 struct got_tree_entry **selected_entry, int *ndisplayed,
3302 const char *label, int show_ids, const char *parent_path,
3303 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3305 const struct got_error *err = NULL;
3306 struct got_tree_entry *te;
3307 wchar_t *wline;
3308 int width, n;
3310 *ndisplayed = 0;
3312 werase(view->window);
3314 if (limit == 0)
3315 return NULL;
3317 err = format_line(&wline, &width, label, view->ncols);
3318 if (err)
3319 return err;
3320 if (view_needs_focus_indication(view))
3321 wstandout(view->window);
3322 waddwstr(view->window, wline);
3323 if (view_needs_focus_indication(view))
3324 wstandend(view->window);
3325 free(wline);
3326 wline = NULL;
3327 if (width < view->ncols)
3328 waddch(view->window, '\n');
3329 if (--limit <= 0)
3330 return NULL;
3331 err = format_line(&wline, &width, parent_path, view->ncols);
3332 if (err)
3333 return err;
3334 waddwstr(view->window, wline);
3335 free(wline);
3336 wline = NULL;
3337 if (width < view->ncols)
3338 waddch(view->window, '\n');
3339 if (--limit <= 0)
3340 return NULL;
3341 waddch(view->window, '\n');
3342 if (--limit <= 0)
3343 return NULL;
3345 te = SIMPLEQ_FIRST(&entries->head);
3346 if (*first_displayed_entry == NULL) {
3347 if (selected == 0) {
3348 if (view->focussed)
3349 wstandout(view->window);
3350 *selected_entry = NULL;
3352 waddstr(view->window, " ..\n"); /* parent directory */
3353 if (selected == 0 && view->focussed)
3354 wstandend(view->window);
3355 (*ndisplayed)++;
3356 if (--limit <= 0)
3357 return NULL;
3358 n = 1;
3359 } else {
3360 n = 0;
3361 while (te != *first_displayed_entry)
3362 te = SIMPLEQ_NEXT(te, entry);
3365 while (te) {
3366 char *line = NULL, *id_str = NULL;
3368 if (show_ids) {
3369 err = got_object_id_str(&id_str, te->id);
3370 if (err)
3371 return got_error_from_errno();
3373 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3374 te->name, S_ISDIR(te->mode) ? "/" :
3375 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3376 free(id_str);
3377 return got_error_from_errno();
3379 free(id_str);
3380 err = format_line(&wline, &width, line, view->ncols);
3381 if (err) {
3382 free(line);
3383 break;
3385 if (n == selected) {
3386 if (view->focussed)
3387 wstandout(view->window);
3388 *selected_entry = te;
3390 waddwstr(view->window, wline);
3391 if (width < view->ncols)
3392 waddch(view->window, '\n');
3393 if (n == selected && view->focussed)
3394 wstandend(view->window);
3395 free(line);
3396 free(wline);
3397 wline = NULL;
3398 n++;
3399 (*ndisplayed)++;
3400 *last_displayed_entry = te;
3401 if (--limit <= 0)
3402 break;
3403 te = SIMPLEQ_NEXT(te, entry);
3406 return err;
3409 static void
3410 tree_scroll_up(struct tog_view *view,
3411 struct got_tree_entry **first_displayed_entry, int maxscroll,
3412 const struct got_tree_entries *entries, int isroot)
3414 struct got_tree_entry *te, *prev;
3415 int i;
3417 if (*first_displayed_entry == NULL) {
3418 view_flash(view);
3419 return;
3422 te = SIMPLEQ_FIRST(&entries->head);
3423 if (*first_displayed_entry == te) {
3424 view_flash(view);
3425 if (!isroot)
3426 *first_displayed_entry = NULL;
3427 return;
3430 /* XXX this is stupid... switch to TAILQ? */
3431 for (i = 0; i < maxscroll; i++) {
3432 while (te != *first_displayed_entry) {
3433 prev = te;
3434 te = SIMPLEQ_NEXT(te, entry);
3436 *first_displayed_entry = prev;
3437 te = SIMPLEQ_FIRST(&entries->head);
3439 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3440 *first_displayed_entry = NULL;
3443 static int
3444 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3445 struct got_tree_entry *last_displayed_entry,
3446 const struct got_tree_entries *entries)
3448 struct got_tree_entry *next, *last;
3449 int n = 0;
3451 if (*first_displayed_entry)
3452 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3453 else
3454 next = SIMPLEQ_FIRST(&entries->head);
3455 last = last_displayed_entry;
3456 while (next && last && n++ < maxscroll) {
3457 last = SIMPLEQ_NEXT(last, entry);
3458 if (last) {
3459 *first_displayed_entry = next;
3460 next = SIMPLEQ_NEXT(next, entry);
3463 return n;
3466 static const struct got_error *
3467 tree_entry_path(char **path, struct tog_parent_trees *parents,
3468 struct got_tree_entry *te)
3470 const struct got_error *err = NULL;
3471 struct tog_parent_tree *pt;
3472 size_t len = 2; /* for leading slash and NUL */
3474 TAILQ_FOREACH(pt, parents, entry)
3475 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3476 if (te)
3477 len += strlen(te->name);
3479 *path = calloc(1, len);
3480 if (path == NULL)
3481 return got_error_from_errno();
3483 (*path)[0] = '/';
3484 pt = TAILQ_LAST(parents, tog_parent_trees);
3485 while (pt) {
3486 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3487 err = got_error(GOT_ERR_NO_SPACE);
3488 goto done;
3490 if (strlcat(*path, "/", len) >= len) {
3491 err = got_error(GOT_ERR_NO_SPACE);
3492 goto done;
3494 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3496 if (te) {
3497 if (strlcat(*path, te->name, len) >= len) {
3498 err = got_error(GOT_ERR_NO_SPACE);
3499 goto done;
3502 done:
3503 if (err) {
3504 free(*path);
3505 *path = NULL;
3507 return err;
3510 static const struct got_error *
3511 blame_tree_entry(struct tog_view **new_view, int begin_x,
3512 struct got_tree_entry *te, struct tog_parent_trees *parents,
3513 struct got_object_id *commit_id, struct got_reflist_head *refs,
3514 struct got_repository *repo)
3516 const struct got_error *err = NULL;
3517 char *path;
3518 struct tog_view *blame_view;
3520 err = tree_entry_path(&path, parents, te);
3521 if (err)
3522 return err;
3524 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3525 if (blame_view == NULL)
3526 return got_error_from_errno();
3528 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3529 if (err) {
3530 view_close(blame_view);
3531 free(path);
3532 } else
3533 *new_view = blame_view;
3534 return err;
3537 static const struct got_error *
3538 log_tree_entry(struct tog_view **new_view, int begin_x,
3539 struct got_tree_entry *te, struct tog_parent_trees *parents,
3540 struct got_object_id *commit_id, struct got_reflist_head *refs,
3541 struct got_repository *repo)
3543 struct tog_view *log_view;
3544 const struct got_error *err = NULL;
3545 char *path;
3547 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3548 if (log_view == NULL)
3549 return got_error_from_errno();
3551 err = tree_entry_path(&path, parents, te);
3552 if (err)
3553 return err;
3555 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3556 if (err)
3557 view_close(log_view);
3558 else
3559 *new_view = log_view;
3560 free(path);
3561 return err;
3564 static const struct got_error *
3565 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3566 struct got_object_id *commit_id, struct got_reflist_head *refs,
3567 struct got_repository *repo)
3569 const struct got_error *err = NULL;
3570 char *commit_id_str = NULL;
3571 struct tog_tree_view_state *s = &view->state.tree;
3573 TAILQ_INIT(&s->parents);
3575 err = got_object_id_str(&commit_id_str, commit_id);
3576 if (err != NULL)
3577 goto done;
3579 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3580 err = got_error_from_errno();
3581 goto done;
3584 s->root = s->tree = root;
3585 s->entries = got_object_tree_get_entries(root);
3586 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3587 s->commit_id = got_object_id_dup(commit_id);
3588 if (s->commit_id == NULL) {
3589 err = got_error_from_errno();
3590 goto done;
3592 s->refs = refs;
3593 s->repo = repo;
3595 view->show = show_tree_view;
3596 view->input = input_tree_view;
3597 view->close = close_tree_view;
3598 done:
3599 free(commit_id_str);
3600 if (err) {
3601 free(s->tree_label);
3602 s->tree_label = NULL;
3604 return err;
3607 static const struct got_error *
3608 close_tree_view(struct tog_view *view)
3610 struct tog_tree_view_state *s = &view->state.tree;
3612 free(s->tree_label);
3613 s->tree_label = NULL;
3614 free(s->commit_id);
3615 s->commit_id = NULL;
3616 while (!TAILQ_EMPTY(&s->parents)) {
3617 struct tog_parent_tree *parent;
3618 parent = TAILQ_FIRST(&s->parents);
3619 TAILQ_REMOVE(&s->parents, parent, entry);
3620 free(parent);
3623 if (s->tree != s->root)
3624 got_object_tree_close(s->tree);
3625 got_object_tree_close(s->root);
3627 return NULL;
3630 static const struct got_error *
3631 show_tree_view(struct tog_view *view)
3633 const struct got_error *err = NULL;
3634 struct tog_tree_view_state *s = &view->state.tree;
3635 char *parent_path;
3637 err = tree_entry_path(&parent_path, &s->parents, NULL);
3638 if (err)
3639 return err;
3641 err = draw_tree_entries(view, &s->first_displayed_entry,
3642 &s->last_displayed_entry, &s->selected_entry,
3643 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3644 s->entries, s->selected, view->nlines, s->tree == s->root);
3645 free(parent_path);
3647 view_vborder(view);
3648 return err;
3651 static const struct got_error *
3652 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3653 struct tog_view **focus_view, struct tog_view *view, int ch)
3655 const struct got_error *err = NULL;
3656 struct tog_tree_view_state *s = &view->state.tree;
3657 struct tog_view *log_view;
3658 int begin_x = 0, nscrolled;
3660 switch (ch) {
3661 case 'i':
3662 s->show_ids = !s->show_ids;
3663 break;
3664 case 'l':
3665 if (!s->selected_entry)
3666 break;
3667 if (view_is_parent_view(view))
3668 begin_x = view_split_begin_x(view->begin_x);
3669 err = log_tree_entry(&log_view, begin_x,
3670 s->selected_entry, &s->parents,
3671 s->commit_id, s->refs, s->repo);
3672 if (view_is_parent_view(view)) {
3673 err = view_close_child(view);
3674 if (err)
3675 return err;
3676 err = view_set_child(view, log_view);
3677 if (err) {
3678 view_close(log_view);
3679 break;
3681 *focus_view = log_view;
3682 view->child_focussed = 1;
3683 } else
3684 *new_view = log_view;
3685 break;
3686 case 'k':
3687 case KEY_UP:
3688 if (s->selected > 0) {
3689 s->selected--;
3690 if (s->selected == 0)
3691 break;
3693 if (s->selected > 0)
3694 break;
3695 tree_scroll_up(view, &s->first_displayed_entry, 1,
3696 s->entries, s->tree == s->root);
3697 break;
3698 case KEY_PPAGE:
3699 tree_scroll_up(view, &s->first_displayed_entry,
3700 MAX(0, view->nlines - 4 - s->selected), s->entries,
3701 s->tree == s->root);
3702 s->selected = 0;
3703 if (SIMPLEQ_FIRST(&s->entries->head) ==
3704 s->first_displayed_entry && s->tree != s->root)
3705 s->first_displayed_entry = NULL;
3706 break;
3707 case 'j':
3708 case KEY_DOWN:
3709 if (s->selected < s->ndisplayed - 1) {
3710 s->selected++;
3711 break;
3713 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3714 == NULL) {
3715 /* can't scroll any further */
3716 view_flash(view);
3717 break;
3719 tree_scroll_down(&s->first_displayed_entry, 1,
3720 s->last_displayed_entry, s->entries);
3721 break;
3722 case KEY_NPAGE:
3723 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3724 == NULL) {
3725 /* can't scroll any further; move cursor down */
3726 if (s->selected < s->ndisplayed - 1)
3727 s->selected = s->ndisplayed - 1;
3728 else
3729 view_flash(view);
3730 break;
3732 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3733 view->nlines, s->last_displayed_entry, s->entries);
3734 if (nscrolled < view->nlines) {
3735 int ndisplayed = 0;
3736 struct got_tree_entry *te;
3737 te = s->first_displayed_entry;
3738 do {
3739 ndisplayed++;
3740 te = SIMPLEQ_NEXT(te, entry);
3741 } while (te);
3742 s->selected = ndisplayed - 1;
3744 break;
3745 case KEY_ENTER:
3746 case '\r':
3747 if (s->selected_entry == NULL) {
3748 struct tog_parent_tree *parent;
3749 case KEY_BACKSPACE:
3750 /* user selected '..' */
3751 if (s->tree == s->root)
3752 break;
3753 parent = TAILQ_FIRST(&s->parents);
3754 TAILQ_REMOVE(&s->parents, parent,
3755 entry);
3756 got_object_tree_close(s->tree);
3757 s->tree = parent->tree;
3758 s->entries =
3759 got_object_tree_get_entries(s->tree);
3760 s->first_displayed_entry =
3761 parent->first_displayed_entry;
3762 s->selected_entry =
3763 parent->selected_entry;
3764 s->selected = parent->selected;
3765 free(parent);
3766 } else if (S_ISDIR(s->selected_entry->mode)) {
3767 struct tog_parent_tree *parent;
3768 struct got_tree_object *child;
3769 err = got_object_open_as_tree(&child,
3770 s->repo, s->selected_entry->id);
3771 if (err)
3772 break;
3773 parent = calloc(1, sizeof(*parent));
3774 if (parent == NULL) {
3775 err = got_error_from_errno();
3776 break;
3778 parent->tree = s->tree;
3779 parent->first_displayed_entry =
3780 s->first_displayed_entry;
3781 parent->selected_entry = s->selected_entry;
3782 parent->selected = s->selected;
3783 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3784 s->tree = child;
3785 s->entries =
3786 got_object_tree_get_entries(s->tree);
3787 s->selected = 0;
3788 s->first_displayed_entry = NULL;
3789 } else if (S_ISREG(s->selected_entry->mode)) {
3790 struct tog_view *blame_view;
3791 int begin_x = view_is_parent_view(view) ?
3792 view_split_begin_x(view->begin_x) : 0;
3794 err = blame_tree_entry(&blame_view, begin_x,
3795 s->selected_entry, &s->parents,
3796 s->commit_id, s->refs, s->repo);
3797 if (err)
3798 break;
3799 if (view_is_parent_view(view)) {
3800 err = view_close_child(view);
3801 if (err)
3802 return err;
3803 err = view_set_child(view, blame_view);
3804 if (err) {
3805 view_close(blame_view);
3806 break;
3808 *focus_view = blame_view;
3809 view->child_focussed = 1;
3810 } else
3811 *new_view = blame_view;
3813 break;
3814 case KEY_RESIZE:
3815 if (s->selected > view->nlines)
3816 s->selected = s->ndisplayed - 1;
3817 break;
3818 default:
3819 break;
3822 return err;
3825 __dead static void
3826 usage_tree(void)
3828 endwin();
3829 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3830 getprogname());
3831 exit(1);
3834 static const struct got_error *
3835 cmd_tree(int argc, char *argv[])
3837 const struct got_error *error;
3838 struct got_repository *repo = NULL;
3839 struct got_reflist_head refs;
3840 char *repo_path = NULL;
3841 struct got_object_id *commit_id = NULL;
3842 char *commit_id_arg = NULL;
3843 struct got_commit_object *commit = NULL;
3844 struct got_tree_object *tree = NULL;
3845 int ch;
3846 struct tog_view *view;
3848 #ifndef PROFILE
3849 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3850 NULL) == -1)
3851 err(1, "pledge");
3852 #endif
3854 while ((ch = getopt(argc, argv, "c:")) != -1) {
3855 switch (ch) {
3856 case 'c':
3857 commit_id_arg = optarg;
3858 break;
3859 default:
3860 usage_tree();
3861 /* NOTREACHED */
3865 argc -= optind;
3866 argv += optind;
3868 if (argc == 0) {
3869 struct got_worktree *worktree;
3870 char *cwd = getcwd(NULL, 0);
3871 if (cwd == NULL)
3872 return got_error_from_errno();
3873 error = got_worktree_open(&worktree, cwd);
3874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3875 goto done;
3876 if (worktree) {
3877 free(cwd);
3878 repo_path =
3879 strdup(got_worktree_get_repo_path(worktree));
3880 got_worktree_close(worktree);
3881 } else
3882 repo_path = cwd;
3883 if (repo_path == NULL) {
3884 error = got_error_from_errno();
3885 goto done;
3887 } else if (argc == 1) {
3888 repo_path = realpath(argv[0], NULL);
3889 if (repo_path == NULL)
3890 return got_error_from_errno();
3891 } else
3892 usage_log();
3894 init_curses();
3896 error = apply_unveil(repo_path, NULL);
3897 if (error)
3898 goto done;
3900 error = got_repo_open(&repo, repo_path);
3901 if (error != NULL)
3902 goto done;
3904 if (commit_id_arg == NULL)
3905 error = get_head_commit_id(&commit_id, repo);
3906 else
3907 error = got_object_resolve_id_str(&commit_id, repo,
3908 commit_id_arg);
3909 if (error != NULL)
3910 goto done;
3912 error = got_object_open_as_commit(&commit, repo, commit_id);
3913 if (error != NULL)
3914 goto done;
3916 error = got_object_open_as_tree(&tree, repo,
3917 got_object_commit_get_tree_id(commit));
3918 if (error != NULL)
3919 goto done;
3921 SIMPLEQ_INIT(&refs);
3922 error = got_ref_list(&refs, repo);
3923 if (error)
3924 goto done;
3926 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3927 if (view == NULL) {
3928 error = got_error_from_errno();
3929 goto done;
3931 error = open_tree_view(view, tree, commit_id, &refs, repo);
3932 if (error)
3933 goto done;
3934 error = view_loop(view);
3935 done:
3936 free(repo_path);
3937 free(commit_id);
3938 if (commit)
3939 got_object_commit_close(commit);
3940 if (tree)
3941 got_object_tree_close(tree);
3942 if (repo)
3943 got_repo_close(repo);
3944 got_ref_list_free(&refs);
3945 return error;
3948 __dead static void
3949 usage(void)
3951 int i;
3953 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3954 "Available commands:\n", getprogname());
3955 for (i = 0; i < nitems(tog_commands); i++) {
3956 struct tog_cmd *cmd = &tog_commands[i];
3957 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3959 exit(1);
3962 static char **
3963 make_argv(const char *arg0, const char *arg1)
3965 char **argv;
3966 int argc = (arg1 == NULL ? 1 : 2);
3968 argv = calloc(argc, sizeof(char *));
3969 if (argv == NULL)
3970 err(1, "calloc");
3971 argv[0] = strdup(arg0);
3972 if (argv[0] == NULL)
3973 err(1, "calloc");
3974 if (arg1) {
3975 argv[1] = strdup(arg1);
3976 if (argv[1] == NULL)
3977 err(1, "calloc");
3980 return argv;
3983 int
3984 main(int argc, char *argv[])
3986 const struct got_error *error = NULL;
3987 struct tog_cmd *cmd = NULL;
3988 int ch, hflag = 0;
3989 char **cmd_argv = NULL;
3991 setlocale(LC_CTYPE, "");
3993 while ((ch = getopt(argc, argv, "h")) != -1) {
3994 switch (ch) {
3995 case 'h':
3996 hflag = 1;
3997 break;
3998 default:
3999 usage();
4000 /* NOTREACHED */
4004 argc -= optind;
4005 argv += optind;
4006 optind = 0;
4007 optreset = 1;
4009 if (argc == 0) {
4010 if (hflag)
4011 usage();
4012 /* Build an argument vector which runs a default command. */
4013 cmd = &tog_commands[0];
4014 cmd_argv = make_argv(cmd->name, NULL);
4015 argc = 1;
4016 } else {
4017 int i;
4019 /* Did the user specific a command? */
4020 for (i = 0; i < nitems(tog_commands); i++) {
4021 if (strncmp(tog_commands[i].name, argv[0],
4022 strlen(argv[0])) == 0) {
4023 cmd = &tog_commands[i];
4024 if (hflag)
4025 tog_commands[i].cmd_usage();
4026 break;
4029 if (cmd == NULL) {
4030 /* Did the user specify a repository? */
4031 char *repo_path = realpath(argv[0], NULL);
4032 if (repo_path) {
4033 struct got_repository *repo;
4034 error = got_repo_open(&repo, repo_path);
4035 if (error == NULL)
4036 got_repo_close(repo);
4037 } else
4038 error = got_error_from_errno();
4039 if (error) {
4040 if (hflag) {
4041 fprintf(stderr, "%s: '%s' is not a "
4042 "known command\n", getprogname(),
4043 argv[0]);
4044 usage();
4046 fprintf(stderr, "%s: '%s' is neither a known "
4047 "command nor a path to a repository\n",
4048 getprogname(), argv[0]);
4049 free(repo_path);
4050 return 1;
4052 cmd = &tog_commands[0];
4053 cmd_argv = make_argv(cmd->name, repo_path);
4054 argc = 2;
4055 free(repo_path);
4059 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4060 if (error)
4061 goto done;
4062 done:
4063 endwin();
4064 free(cmd_argv);
4065 if (error)
4066 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4067 return 0;