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 tog_diff_view_state {
102 struct got_object_id *id1, *id2;
103 FILE *f;
104 int first_displayed_line;
105 int last_displayed_line;
106 int eof;
107 int diff_context;
108 struct got_repository *repo;
109 };
111 struct commit_queue_entry {
112 TAILQ_ENTRY(commit_queue_entry) entry;
113 struct got_object_id *id;
114 struct got_commit_object *commit;
115 int idx;
116 };
117 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
118 struct commit_queue {
119 int ncommits;
120 struct commit_queue_head head;
121 };
123 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
125 struct tog_log_thread_args {
126 pthread_cond_t need_commits;
127 int commits_needed;
128 struct got_commit_graph *graph;
129 struct commit_queue *commits;
130 const char *in_repo_path;
131 struct got_object_id *start_id;
132 struct got_repository *repo;
133 int log_complete;
134 sig_atomic_t *quit;
135 struct tog_view *view;
136 struct commit_queue_entry **first_displayed_entry;
137 struct commit_queue_entry **selected_entry;
138 };
140 struct tog_log_view_state {
141 struct commit_queue commits;
142 struct commit_queue_entry *first_displayed_entry;
143 struct commit_queue_entry *last_displayed_entry;
144 struct commit_queue_entry *selected_entry;
145 int selected;
146 char *in_repo_path;
147 struct got_repository *repo;
148 struct got_object_id *start_id;
149 sig_atomic_t quit;
150 pthread_t thread;
151 struct tog_log_thread_args thread_args;
152 };
154 struct tog_blame_cb_args {
155 struct tog_blame_line *lines; /* one per line */
156 int nlines;
158 struct tog_view *view;
159 struct got_object_id *commit_id;
160 int *quit;
161 };
163 struct tog_blame_thread_args {
164 const char *path;
165 struct got_repository *repo;
166 struct tog_blame_cb_args *cb_args;
167 int *complete;
168 };
170 struct tog_blame {
171 FILE *f;
172 size_t filesize;
173 struct tog_blame_line *lines;
174 int nlines;
175 pthread_t thread;
176 struct tog_blame_thread_args thread_args;
177 struct tog_blame_cb_args cb_args;
178 const char *path;
179 };
181 struct tog_blame_view_state {
182 int first_displayed_line;
183 int last_displayed_line;
184 int selected_line;
185 int blame_complete;
186 int eof;
187 int done;
188 struct got_object_id_queue blamed_commits;
189 struct got_object_qid *blamed_commit;
190 char *path;
191 struct got_repository *repo;
192 struct got_object_id *commit_id;
193 struct tog_blame blame;
194 };
196 struct tog_parent_tree {
197 TAILQ_ENTRY(tog_parent_tree) entry;
198 struct got_tree_object *tree;
199 struct got_tree_entry *first_displayed_entry;
200 struct got_tree_entry *selected_entry;
201 int selected;
202 };
204 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
206 struct tog_tree_view_state {
207 char *tree_label;
208 struct got_tree_object *root;
209 struct got_tree_object *tree;
210 const struct got_tree_entries *entries;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *last_displayed_entry;
213 struct got_tree_entry *selected_entry;
214 int ndisplayed, selected, show_ids;
215 struct tog_parent_trees parents;
216 struct got_object_id *commit_id;
217 struct got_repository *repo;
218 };
220 /*
221 * We implement two types of views: parent views and child views.
223 * The 'Tab' key switches between a parent view and its child view.
224 * Child views are shown side-by-side to their parent view, provided
225 * there is enough screen estate.
227 * When a new view is opened from within a parent view, this new view
228 * becomes a child view of the parent view, replacing any existing child.
230 * When a new view is opened from within a child view, this new view
231 * becomes a parent view which will obscure the views below until the
232 * user quits the new parent view by typing 'q'.
234 * This list of views contains parent views only.
235 * Child views are only pointed to by their parent view.
236 */
237 TAILQ_HEAD(tog_view_list_head, tog_view);
239 struct tog_view {
240 TAILQ_ENTRY(tog_view) entry;
241 WINDOW *window;
242 PANEL *panel;
243 int nlines, ncols, begin_y, begin_x;
244 int lines, cols; /* copies of LINES and COLS */
245 int focussed;
246 struct tog_view *parent;
247 struct tog_view *child;
248 int child_focussed;
250 /* type-specific state */
251 enum tog_view_type type;
252 union {
253 struct tog_diff_view_state diff;
254 struct tog_log_view_state log;
255 struct tog_blame_view_state blame;
256 struct tog_tree_view_state tree;
257 } state;
259 const struct got_error *(*show)(struct tog_view *);
260 const struct got_error *(*input)(struct tog_view **,
261 struct tog_view **, struct tog_view**, struct tog_view *, int);
262 const struct got_error *(*close)(struct tog_view *);
263 };
265 static const struct got_error *open_diff_view(struct tog_view *,
266 struct got_object_id *, struct got_object_id *, struct got_repository *);
267 static const struct got_error *show_diff_view(struct tog_view *);
268 static const struct got_error *input_diff_view(struct tog_view **,
269 struct tog_view **, struct tog_view **, struct tog_view *, int);
270 static const struct got_error* close_diff_view(struct tog_view *);
272 static const struct got_error *open_log_view(struct tog_view *,
273 struct got_object_id *, struct got_repository *, const char *, int);
274 static const struct got_error * show_log_view(struct tog_view *);
275 static const struct got_error *input_log_view(struct tog_view **,
276 struct tog_view **, struct tog_view **, struct tog_view *, int);
277 static const struct got_error *close_log_view(struct tog_view *);
279 static const struct got_error *open_blame_view(struct tog_view *, char *,
280 struct got_object_id *, struct got_repository *);
281 static const struct got_error *show_blame_view(struct tog_view *);
282 static const struct got_error *input_blame_view(struct tog_view **,
283 struct tog_view **, struct tog_view **, struct tog_view *, int);
284 static const struct got_error *close_blame_view(struct tog_view *);
286 static const struct got_error *open_tree_view(struct tog_view *,
287 struct got_tree_object *, struct got_object_id *, struct got_repository *);
288 static const struct got_error *show_tree_view(struct tog_view *);
289 static const struct got_error *input_tree_view(struct tog_view **,
290 struct tog_view **, struct tog_view **, struct tog_view *, int);
291 static const struct got_error *close_tree_view(struct tog_view *);
293 static volatile sig_atomic_t tog_sigwinch_received;
295 static void
296 tog_sigwinch(int signo)
298 tog_sigwinch_received = 1;
301 static const struct got_error *
302 view_close(struct tog_view *view)
304 const struct got_error *err = NULL;
306 if (view->child) {
307 view_close(view->child);
308 view->child = NULL;
310 if (view->close)
311 err = view->close(view);
312 if (view->panel)
313 del_panel(view->panel);
314 if (view->window)
315 delwin(view->window);
316 free(view);
317 return err;
320 static struct tog_view *
321 view_open(int nlines, int ncols, int begin_y, int begin_x,
322 enum tog_view_type type)
324 struct tog_view *view = calloc(1, sizeof(*view));
326 if (view == NULL)
327 return NULL;
329 view->type = type;
330 view->lines = LINES;
331 view->cols = COLS;
332 view->nlines = nlines ? nlines : LINES - begin_y;
333 view->ncols = ncols ? ncols : COLS - begin_x;
334 view->begin_y = begin_y;
335 view->begin_x = begin_x;
336 view->window = newwin(nlines, ncols, begin_y, begin_x);
337 if (view->window == NULL) {
338 view_close(view);
339 return NULL;
341 view->panel = new_panel(view->window);
342 if (view->panel == NULL ||
343 set_panel_userptr(view->panel, view) != OK) {
344 view_close(view);
345 return NULL;
348 keypad(view->window, TRUE);
349 return view;
352 static int
353 view_split_begin_x(int begin_x)
355 if (begin_x > 0 || COLS < 120)
356 return 0;
357 return (COLS - MAX(COLS / 2, 80));
360 static const struct got_error *view_resize(struct tog_view *);
362 static const struct got_error *
363 view_splitscreen(struct tog_view *view)
365 const struct got_error *err = NULL;
367 view->begin_y = 0;
368 view->begin_x = view_split_begin_x(0);
369 view->nlines = LINES;
370 view->ncols = COLS - view->begin_x;
371 view->lines = LINES;
372 view->cols = COLS;
373 err = view_resize(view);
374 if (err)
375 return err;
377 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
378 return got_error_from_errno();
380 return NULL;
383 static const struct got_error *
384 view_fullscreen(struct tog_view *view)
386 const struct got_error *err = NULL;
388 view->begin_x = 0;
389 view->begin_y = 0;
390 view->nlines = LINES;
391 view->ncols = COLS;
392 view->lines = LINES;
393 view->cols = COLS;
394 err = view_resize(view);
395 if (err)
396 return err;
398 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
399 return got_error_from_errno();
401 return NULL;
404 static int
405 view_is_parent_view(struct tog_view *view)
407 return view->parent == NULL;
410 static const struct got_error *
411 view_resize(struct tog_view *view)
413 int nlines, ncols;
415 if (view->lines > LINES)
416 nlines = view->nlines - (view->lines - LINES);
417 else
418 nlines = view->nlines + (LINES - view->lines);
420 if (view->cols > COLS)
421 ncols = view->ncols - (view->cols - COLS);
422 else
423 ncols = view->ncols + (COLS - view->cols);
425 if (wresize(view->window, nlines, ncols) == ERR)
426 return got_error_from_errno();
427 if (replace_panel(view->panel, view->window) == ERR)
428 return got_error_from_errno();
429 wclear(view->window);
431 view->nlines = nlines;
432 view->ncols = ncols;
433 view->lines = LINES;
434 view->cols = COLS;
436 if (view->child) {
437 view->child->begin_x = view_split_begin_x(view->begin_x);
438 if (view->child->begin_x == 0) {
439 view_fullscreen(view->child);
440 if (view->child->focussed)
441 show_panel(view->child->panel);
442 else
443 show_panel(view->panel);
444 } else {
445 view_splitscreen(view->child);
446 show_panel(view->child->panel);
450 return NULL;
453 static const struct got_error *
454 view_close_child(struct tog_view *view)
456 const struct got_error *err = NULL;
458 if (view->child == NULL)
459 return NULL;
461 err = view_close(view->child);
462 view->child = NULL;
463 return err;
466 static const struct got_error *
467 view_set_child(struct tog_view *view, struct tog_view *child)
469 const struct got_error *err = NULL;
471 view->child = child;
472 child->parent = view;
473 return err;
476 static int
477 view_is_splitscreen(struct tog_view *view)
479 return !view_is_parent_view(view) && view->begin_x > 0;
482 static void
483 tog_resizeterm(void)
485 int cols, lines;
486 struct winsize size;
488 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
489 cols = 80; /* Default */
490 lines = 24;
491 } else {
492 cols = size.ws_col;
493 lines = size.ws_row;
495 resize_term(lines, cols);
498 static const struct got_error *
499 view_input(struct tog_view **new, struct tog_view **dead,
500 struct tog_view **focus, int *done, struct tog_view *view,
501 struct tog_view_list_head *views)
503 const struct got_error *err = NULL;
504 struct tog_view *v;
505 int ch, errcode;
507 *new = NULL;
508 *dead = NULL;
509 *focus = NULL;
511 nodelay(stdscr, FALSE);
512 /* Allow threads to make progress while we are waiting for input. */
513 errcode = pthread_mutex_unlock(&tog_mutex);
514 if (errcode)
515 return got_error_set_errno(errcode);
516 ch = wgetch(view->window);
517 errcode = pthread_mutex_lock(&tog_mutex);
518 if (errcode)
519 return got_error_set_errno(errcode);
520 nodelay(stdscr, TRUE);
522 if (tog_sigwinch_received) {
523 tog_resizeterm();
524 tog_sigwinch_received = 0;
525 TAILQ_FOREACH(v, views, entry) {
526 err = view_resize(v);
527 if (err)
528 return err;
529 err = v->input(new, dead, focus, v, KEY_RESIZE);
530 if (err)
531 return err;
535 switch (ch) {
536 case ERR:
537 break;
538 case '\t':
539 if (view->child) {
540 *focus = view->child;
541 view->child_focussed = 1;
542 } else if (view->parent) {
543 *focus = view->parent;
544 view->parent->child_focussed = 0;
546 break;
547 case 'q':
548 err = view->input(new, dead, focus, view, ch);
549 *dead = view;
550 break;
551 case 'Q':
552 *done = 1;
553 break;
554 case 'f':
555 if (view_is_parent_view(view)) {
556 if (view->child == NULL)
557 break;
558 if (view_is_splitscreen(view->child)) {
559 *focus = view->child;
560 view->child_focussed = 1;
561 err = view_fullscreen(view->child);
562 } else
563 err = view_splitscreen(view->child);
564 if (err)
565 break;
566 err = view->child->input(new, dead, focus,
567 view->child, KEY_RESIZE);
568 } else {
569 if (view_is_splitscreen(view)) {
570 *focus = view;
571 view->parent->child_focussed = 1;
572 err = view_fullscreen(view);
573 } else {
574 err = view_splitscreen(view);
576 if (err)
577 break;
578 err = view->input(new, dead, focus, view,
579 KEY_RESIZE);
581 break;
582 case KEY_RESIZE:
583 break;
584 default:
585 err = view->input(new, dead, focus, view, ch);
586 break;
589 return err;
592 void
593 view_vborder(struct tog_view *view)
595 PANEL *panel;
596 struct tog_view *view_above;
598 if (view->parent)
599 return view_vborder(view->parent);
601 panel = panel_above(view->panel);
602 if (panel == NULL)
603 return;
605 view_above = panel_userptr(panel);
606 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
607 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
610 int
611 view_needs_focus_indication(struct tog_view *view)
613 if (view_is_parent_view(view)) {
614 if (view->child == NULL || view->child_focussed)
615 return 0;
616 if (!view_is_splitscreen(view->child))
617 return 0;
618 } else if (!view_is_splitscreen(view))
619 return 0;
621 return view->focussed;
624 static const struct got_error *
625 view_loop(struct tog_view *view)
627 const struct got_error *err = NULL;
628 struct tog_view_list_head views;
629 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
630 int fast_refresh = 10;
631 int done = 0, errcode;
633 errcode = pthread_mutex_lock(&tog_mutex);
634 if (errcode)
635 return got_error_set_errno(errcode);
637 TAILQ_INIT(&views);
638 TAILQ_INSERT_HEAD(&views, view, entry);
640 main_view = view;
641 view->focussed = 1;
642 err = view->show(view);
643 if (err)
644 return err;
645 update_panels();
646 doupdate();
647 while (!TAILQ_EMPTY(&views) && !done) {
648 /* Refresh fast during initialization, then become slower. */
649 if (fast_refresh && fast_refresh-- == 0)
650 halfdelay(10); /* switch to once per second */
652 err = view_input(&new_view, &dead_view, &focus_view, &done,
653 view, &views);
654 if (err)
655 break;
656 if (dead_view) {
657 struct tog_view *prev = NULL;
659 if (view_is_parent_view(dead_view))
660 prev = TAILQ_PREV(dead_view,
661 tog_view_list_head, entry);
662 else if (view->parent != dead_view)
663 prev = view->parent;
665 if (dead_view->parent)
666 dead_view->parent->child = NULL;
667 else
668 TAILQ_REMOVE(&views, dead_view, entry);
670 err = view_close(dead_view);
671 if (err || dead_view == main_view)
672 goto done;
674 if (view == dead_view) {
675 if (focus_view)
676 view = focus_view;
677 else if (prev)
678 view = prev;
679 else if (!TAILQ_EMPTY(&views))
680 view = TAILQ_LAST(&views,
681 tog_view_list_head);
682 else
683 view = NULL;
684 if (view) {
685 if (view->child && view->child_focussed)
686 focus_view = view->child;
687 else
688 focus_view = view;
692 if (new_view) {
693 struct tog_view *v, *t;
694 /* Only allow one parent view per type. */
695 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
696 if (v->type != new_view->type)
697 continue;
698 TAILQ_REMOVE(&views, v, entry);
699 err = view_close(v);
700 if (err)
701 goto done;
702 break;
704 TAILQ_INSERT_TAIL(&views, new_view, entry);
705 view = new_view;
706 if (focus_view == NULL)
707 focus_view = new_view;
709 if (focus_view) {
710 show_panel(focus_view->panel);
711 if (view)
712 view->focussed = 0;
713 focus_view->focussed = 1;
714 view = focus_view;
715 if (new_view)
716 show_panel(new_view->panel);
717 if (view->child && view_is_splitscreen(view->child))
718 show_panel(view->child->panel);
720 if (view) {
721 if (focus_view == NULL) {
722 view->focussed = 1;
723 show_panel(view->panel);
724 if (view->child && view_is_splitscreen(view->child))
725 show_panel(view->child->panel);
726 focus_view = view;
728 if (view->parent) {
729 err = view->parent->show(view->parent);
730 if (err)
731 goto done;
733 err = view->show(view);
734 if (err)
735 goto done;
736 if (view->child) {
737 err = view->child->show(view->child);
738 if (err)
739 goto done;
741 update_panels();
742 doupdate();
745 done:
746 while (!TAILQ_EMPTY(&views)) {
747 view = TAILQ_FIRST(&views);
748 TAILQ_REMOVE(&views, view, entry);
749 view_close(view);
752 errcode = pthread_mutex_unlock(&tog_mutex);
753 if (errcode)
754 return got_error_set_errno(errcode);
756 return err;
759 __dead static void
760 usage_log(void)
762 endwin();
763 fprintf(stderr,
764 "usage: %s log [-c commit] [-r repository-path] [path]\n",
765 getprogname());
766 exit(1);
769 /* Create newly allocated wide-character string equivalent to a byte string. */
770 static const struct got_error *
771 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
773 char *vis = NULL;
774 const struct got_error *err = NULL;
776 *ws = NULL;
777 *wlen = mbstowcs(NULL, s, 0);
778 if (*wlen == (size_t)-1) {
779 int vislen;
780 if (errno != EILSEQ)
781 return got_error_from_errno();
783 /* byte string invalid in current encoding; try to "fix" it */
784 err = got_mbsavis(&vis, &vislen, s);
785 if (err)
786 return err;
787 *wlen = mbstowcs(NULL, vis, 0);
788 if (*wlen == (size_t)-1) {
789 err = got_error_from_errno(); /* give up */
790 goto done;
794 *ws = calloc(*wlen + 1, sizeof(*ws));
795 if (*ws == NULL) {
796 err = got_error_from_errno();
797 goto done;
800 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
801 err = got_error_from_errno();
802 done:
803 free(vis);
804 if (err) {
805 free(*ws);
806 *ws = NULL;
807 *wlen = 0;
809 return err;
812 /* Format a line for display, ensuring that it won't overflow a width limit. */
813 static const struct got_error *
814 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
816 const struct got_error *err = NULL;
817 int cols = 0;
818 wchar_t *wline = NULL;
819 size_t wlen;
820 int i;
822 *wlinep = NULL;
823 *widthp = 0;
825 err = mbs2ws(&wline, &wlen, line);
826 if (err)
827 return err;
829 i = 0;
830 while (i < wlen && cols < wlimit) {
831 int width = wcwidth(wline[i]);
832 switch (width) {
833 case 0:
834 i++;
835 break;
836 case 1:
837 case 2:
838 if (cols + width <= wlimit)
839 cols += width;
840 i++;
841 break;
842 case -1:
843 if (wline[i] == L'\t')
844 cols += TABSIZE - ((cols + 1) % TABSIZE);
845 i++;
846 break;
847 default:
848 err = got_error_from_errno();
849 goto done;
852 wline[i] = L'\0';
853 if (widthp)
854 *widthp = cols;
855 done:
856 if (err)
857 free(wline);
858 else
859 *wlinep = wline;
860 return err;
863 static const struct got_error *
864 draw_commit(struct tog_view *view, struct got_commit_object *commit,
865 struct got_object_id *id)
867 const struct got_error *err = NULL;
868 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
869 char *logmsg0 = NULL, *logmsg = NULL;
870 char *author0 = NULL, *author = NULL;
871 wchar_t *wlogmsg = NULL, *wauthor = NULL;
872 int author_width, logmsg_width;
873 char *newline, *smallerthan;
874 char *line = NULL;
875 int col, limit;
876 static const size_t date_display_cols = 9;
877 static const size_t author_display_cols = 16;
878 const int avail = view->ncols;
879 struct tm tm;
880 time_t committer_time;
882 committer_time = got_object_commit_get_committer_time(commit);
883 if (localtime_r(&committer_time, &tm) == NULL)
884 return got_error_from_errno();
885 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
886 >= sizeof(datebuf))
887 return got_error(GOT_ERR_NO_SPACE);
889 if (avail < date_display_cols)
890 limit = MIN(sizeof(datebuf) - 1, avail);
891 else
892 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
893 waddnstr(view->window, datebuf, limit);
894 col = limit + 1;
895 if (col > avail)
896 goto done;
898 author0 = strdup(got_object_commit_get_author(commit));
899 if (author0 == NULL) {
900 err = got_error_from_errno();
901 goto done;
903 author = author0;
904 smallerthan = strchr(author, '<');
905 if (smallerthan)
906 *smallerthan = '\0';
907 else {
908 char *at = strchr(author, '@');
909 if (at)
910 *at = '\0';
912 limit = avail - col;
913 err = format_line(&wauthor, &author_width, author, limit);
914 if (err)
915 goto done;
916 waddwstr(view->window, wauthor);
917 col += author_width;
918 while (col <= avail && author_width < author_display_cols + 1) {
919 waddch(view->window, ' ');
920 col++;
921 author_width++;
923 if (col > avail)
924 goto done;
926 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
927 if (logmsg0 == NULL) {
928 err = got_error_from_errno();
929 goto done;
931 logmsg = logmsg0;
932 while (*logmsg == '\n')
933 logmsg++;
934 newline = strchr(logmsg, '\n');
935 if (newline)
936 *newline = '\0';
937 limit = avail - col;
938 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
939 if (err)
940 goto done;
941 waddwstr(view->window, wlogmsg);
942 col += logmsg_width;
943 while (col <= avail) {
944 waddch(view->window, ' ');
945 col++;
947 done:
948 free(logmsg0);
949 free(wlogmsg);
950 free(author0);
951 free(wauthor);
952 free(line);
953 return err;
956 static struct commit_queue_entry *
957 alloc_commit_queue_entry(struct got_commit_object *commit,
958 struct got_object_id *id)
960 struct commit_queue_entry *entry;
962 entry = calloc(1, sizeof(*entry));
963 if (entry == NULL)
964 return NULL;
966 entry->id = id;
967 entry->commit = commit;
968 return entry;
971 static void
972 pop_commit(struct commit_queue *commits)
974 struct commit_queue_entry *entry;
976 entry = TAILQ_FIRST(&commits->head);
977 TAILQ_REMOVE(&commits->head, entry, entry);
978 got_object_commit_close(entry->commit);
979 commits->ncommits--;
980 /* Don't free entry->id! It is owned by the commit graph. */
981 free(entry);
984 static void
985 free_commits(struct commit_queue *commits)
987 while (!TAILQ_EMPTY(&commits->head))
988 pop_commit(commits);
991 static const struct got_error *
992 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
993 int minqueue, struct got_repository *repo, const char *path)
995 const struct got_error *err = NULL;
996 int nqueued = 0;
998 /*
999 * We keep all commits open throughout the lifetime of the log
1000 * view in order to avoid having to re-fetch commits from disk
1001 * while updating the display.
1003 while (nqueued < minqueue) {
1004 struct got_object_id *id;
1005 struct got_commit_object *commit;
1006 struct commit_queue_entry *entry;
1007 int errcode;
1009 err = got_commit_graph_iter_next(&id, graph);
1010 if (err) {
1011 if (err->code != GOT_ERR_ITER_NEED_MORE)
1012 break;
1013 err = got_commit_graph_fetch_commits(graph,
1014 minqueue, repo);
1015 if (err)
1016 return err;
1017 continue;
1020 if (id == NULL)
1021 break;
1023 err = got_object_open_as_commit(&commit, repo, id);
1024 if (err)
1025 break;
1026 entry = alloc_commit_queue_entry(commit, id);
1027 if (entry == NULL) {
1028 err = got_error_from_errno();
1029 break;
1032 errcode = pthread_mutex_lock(&tog_mutex);
1033 if (errcode) {
1034 err = got_error_set_errno(errcode);
1035 break;
1038 entry->idx = commits->ncommits;
1039 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1040 nqueued++;
1041 commits->ncommits++;
1043 errcode = pthread_mutex_unlock(&tog_mutex);
1044 if (errcode && err == NULL)
1045 err = got_error_set_errno(errcode);
1048 return err;
1051 static const struct got_error *
1052 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1054 const struct got_error *err = NULL;
1055 struct got_reference *head_ref;
1057 *head_id = NULL;
1059 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1060 if (err)
1061 return err;
1063 err = got_ref_resolve(head_id, repo, head_ref);
1064 got_ref_close(head_ref);
1065 if (err) {
1066 *head_id = NULL;
1067 return err;
1070 return NULL;
1073 static const struct got_error *
1074 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1075 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1076 struct commit_queue *commits, int selected_idx, int limit,
1077 const char *path, int commits_needed)
1079 const struct got_error *err = NULL;
1080 struct commit_queue_entry *entry;
1081 int ncommits, width;
1082 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1083 wchar_t *wline;
1085 entry = first;
1086 ncommits = 0;
1087 while (entry) {
1088 if (ncommits == selected_idx) {
1089 *selected = entry;
1090 break;
1092 entry = TAILQ_NEXT(entry, entry);
1093 ncommits++;
1096 if (*selected) {
1097 err = got_object_id_str(&id_str, (*selected)->id);
1098 if (err)
1099 return err;
1102 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1103 entry ? entry->idx + 1 : 0, commits->ncommits,
1104 commits_needed == 0 ? "" : " loading...") == -1)
1105 return got_error_from_errno();
1107 if (path && strcmp(path, "/") != 0) {
1108 if (asprintf(&header, "commit %s %s%s",
1109 id_str ? id_str : "........................................",
1110 path, ncommits_str) == -1) {
1111 err = got_error_from_errno();
1112 header = NULL;
1113 goto done;
1115 } else if (asprintf(&header, "commit %s%s",
1116 id_str ? id_str : "........................................",
1117 ncommits_str) == -1) {
1118 err = got_error_from_errno();
1119 header = NULL;
1120 goto done;
1122 err = format_line(&wline, &width, header, view->ncols);
1123 if (err)
1124 goto done;
1126 werase(view->window);
1128 if (view_needs_focus_indication(view))
1129 wstandout(view->window);
1130 waddwstr(view->window, wline);
1131 while (width < view->ncols) {
1132 waddch(view->window, ' ');
1133 width++;
1135 if (view_needs_focus_indication(view))
1136 wstandend(view->window);
1137 free(wline);
1138 if (limit <= 1)
1139 goto done;
1141 entry = first;
1142 *last = first;
1143 ncommits = 0;
1144 while (entry) {
1145 if (ncommits >= limit - 1)
1146 break;
1147 if (view->focussed && ncommits == selected_idx)
1148 wstandout(view->window);
1149 err = draw_commit(view, entry->commit, entry->id);
1150 if (view->focussed && ncommits == selected_idx)
1151 wstandend(view->window);
1152 if (err)
1153 break;
1154 ncommits++;
1155 *last = entry;
1156 entry = TAILQ_NEXT(entry, entry);
1159 view_vborder(view);
1160 done:
1161 free(id_str);
1162 free(ncommits_str);
1163 free(header);
1164 return err;
1167 static void
1168 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1169 struct commit_queue *commits)
1171 struct commit_queue_entry *entry;
1172 int nscrolled = 0;
1174 entry = TAILQ_FIRST(&commits->head);
1175 if (*first_displayed_entry == entry)
1176 return;
1178 entry = *first_displayed_entry;
1179 while (entry && nscrolled < maxscroll) {
1180 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1181 if (entry) {
1182 *first_displayed_entry = entry;
1183 nscrolled++;
1188 static const struct got_error *
1189 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1190 struct commit_queue_entry **last_displayed_entry,
1191 struct commit_queue *commits, int *log_complete, int *commits_needed,
1192 pthread_cond_t *need_commits)
1194 const struct got_error *err = NULL;
1195 struct commit_queue_entry *pentry;
1196 int nscrolled = 0;
1198 if (*last_displayed_entry == NULL)
1199 return NULL;
1201 do {
1202 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1203 if (pentry == NULL) {
1204 int errcode;
1205 if (*log_complete)
1206 return NULL;
1207 *commits_needed = maxscroll + 20;
1208 errcode = pthread_cond_signal(need_commits);
1209 if (errcode)
1210 return got_error_set_errno(errcode);
1211 return NULL;
1213 *last_displayed_entry = pentry;
1215 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1216 if (pentry == NULL)
1217 break;
1218 *first_displayed_entry = pentry;
1219 } while (++nscrolled < maxscroll);
1221 return err;
1224 static const struct got_error *
1225 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1226 struct got_object_id *commit_id, struct got_commit_object *commit,
1227 struct got_repository *repo)
1229 const struct got_error *err;
1230 struct got_object_qid *parent_id;
1231 struct tog_view *diff_view;
1233 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1234 if (diff_view == NULL)
1235 return got_error_from_errno();
1237 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1238 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1239 commit_id, repo);
1240 if (err == NULL)
1241 *new_view = diff_view;
1242 return err;
1245 static const struct got_error *
1246 browse_commit(struct tog_view **new_view, int begin_x,
1247 struct commit_queue_entry *entry, struct got_repository *repo)
1249 const struct got_error *err = NULL;
1250 struct got_tree_object *tree;
1251 struct tog_view *tree_view;
1253 err = got_object_open_as_tree(&tree, repo,
1254 got_object_commit_get_tree_id(entry->commit));
1255 if (err)
1256 return err;
1258 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1259 if (tree_view == NULL)
1260 return got_error_from_errno();
1262 err = open_tree_view(tree_view, tree, entry->id, repo);
1263 if (err)
1264 got_object_tree_close(tree);
1265 else
1266 *new_view = tree_view;
1267 return err;
1270 static void *
1271 log_thread(void *arg)
1273 const struct got_error *err = NULL;
1274 int errcode = 0;
1275 struct tog_log_thread_args *a = arg;
1276 int done = 0;
1278 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1279 if (err)
1280 return (void *)err;
1282 while (!done && !err) {
1283 err = queue_commits(a->graph, a->commits, 1, a->repo,
1284 a->in_repo_path);
1285 if (err) {
1286 if (err->code != GOT_ERR_ITER_COMPLETED)
1287 return (void *)err;
1288 err = NULL;
1289 done = 1;
1290 } else if (a->commits_needed > 0)
1291 a->commits_needed--;
1293 errcode = pthread_mutex_lock(&tog_mutex);
1294 if (errcode)
1295 return (void *)got_error_set_errno(errcode);
1297 if (done)
1298 a->log_complete = 1;
1299 else if (*a->quit) {
1300 done = 1;
1301 a->log_complete = 1;
1302 } else if (*a->first_displayed_entry == NULL) {
1303 *a->first_displayed_entry =
1304 TAILQ_FIRST(&a->commits->head);
1305 *a->selected_entry = *a->first_displayed_entry;
1308 if (done)
1309 a->commits_needed = 0;
1310 else if (a->commits_needed == 0) {
1311 errcode = pthread_cond_wait(&a->need_commits,
1312 &tog_mutex);
1313 if (errcode)
1314 err = got_error_set_errno(errcode);
1317 errcode = pthread_mutex_unlock(&tog_mutex);
1318 if (errcode && err == NULL)
1319 err = got_error_set_errno(errcode);
1321 return (void *)err;
1324 static const struct got_error *
1325 stop_log_thread(struct tog_log_view_state *s)
1327 const struct got_error *err = NULL;
1328 int errcode;
1330 if (s->thread) {
1331 s->quit = 1;
1332 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1333 if (errcode)
1334 return got_error_set_errno(errcode);
1335 errcode = pthread_mutex_unlock(&tog_mutex);
1336 if (errcode)
1337 return got_error_set_errno(errcode);
1338 errcode = pthread_join(s->thread, (void **)&err);
1339 if (errcode)
1340 return got_error_set_errno(errcode);
1341 errcode = pthread_mutex_lock(&tog_mutex);
1342 if (errcode)
1343 return got_error_set_errno(errcode);
1344 s->thread = NULL;
1347 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1348 if (errcode && err == NULL)
1349 err = got_error_set_errno(errcode);
1351 if (s->thread_args.repo) {
1352 got_repo_close(s->thread_args.repo);
1353 s->thread_args.repo = NULL;
1356 if (s->thread_args.graph) {
1357 got_commit_graph_close(s->thread_args.graph);
1358 s->thread_args.graph = NULL;
1361 return err;
1364 static const struct got_error *
1365 close_log_view(struct tog_view *view)
1367 const struct got_error *err = NULL;
1368 struct tog_log_view_state *s = &view->state.log;
1370 err = stop_log_thread(s);
1371 free_commits(&s->commits);
1372 free(s->in_repo_path);
1373 s->in_repo_path = NULL;
1374 free(s->start_id);
1375 s->start_id = NULL;
1376 return err;
1379 static const struct got_error *
1380 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1381 struct got_repository *repo, const char *path, int check_disk)
1383 const struct got_error *err = NULL;
1384 struct tog_log_view_state *s = &view->state.log;
1385 struct got_repository *thread_repo = NULL;
1386 struct got_commit_graph *thread_graph = NULL;
1387 int errcode;
1389 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1390 if (err != NULL)
1391 goto done;
1393 /* The commit queue only contains commits being displayed. */
1394 TAILQ_INIT(&s->commits.head);
1395 s->commits.ncommits = 0;
1397 s->repo = repo;
1398 s->start_id = got_object_id_dup(start_id);
1399 if (s->start_id == NULL) {
1400 err = got_error_from_errno();
1401 goto done;
1404 view->show = show_log_view;
1405 view->input = input_log_view;
1406 view->close = close_log_view;
1408 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1409 if (err)
1410 goto done;
1411 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1412 0, thread_repo);
1413 if (err)
1414 goto done;
1416 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1417 if (errcode) {
1418 err = got_error_set_errno(errcode);
1419 goto done;
1422 s->thread_args.commits_needed = view->nlines;
1423 s->thread_args.graph = thread_graph;
1424 s->thread_args.commits = &s->commits;
1425 s->thread_args.in_repo_path = s->in_repo_path;
1426 s->thread_args.start_id = s->start_id;
1427 s->thread_args.repo = thread_repo;
1428 s->thread_args.log_complete = 0;
1429 s->thread_args.quit = &s->quit;
1430 s->thread_args.view = view;
1431 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1432 s->thread_args.selected_entry = &s->selected_entry;
1433 done:
1434 if (err)
1435 close_log_view(view);
1436 return err;
1439 static const struct got_error *
1440 show_log_view(struct tog_view *view)
1442 struct tog_log_view_state *s = &view->state.log;
1444 if (s->thread == NULL) {
1445 int errcode = pthread_create(&s->thread, NULL, log_thread,
1446 &s->thread_args);
1447 if (errcode)
1448 return got_error_set_errno(errcode);
1451 return draw_commits(view, &s->last_displayed_entry,
1452 &s->selected_entry, s->first_displayed_entry,
1453 &s->commits, s->selected, view->nlines,
1454 s->in_repo_path, s->thread_args.commits_needed);
1457 static const struct got_error *
1458 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1459 struct tog_view **focus_view, struct tog_view *view, int ch)
1461 const struct got_error *err = NULL;
1462 struct tog_log_view_state *s = &view->state.log;
1463 char *parent_path;
1464 struct tog_view *diff_view = NULL, *tree_view = NULL;
1465 int begin_x = 0;
1467 switch (ch) {
1468 case 'q':
1469 s->quit = 1;
1470 break;
1471 case 'k':
1472 case KEY_UP:
1473 if (s->first_displayed_entry == NULL)
1474 break;
1475 if (s->selected > 0)
1476 s->selected--;
1477 if (s->selected > 0)
1478 break;
1479 scroll_up(&s->first_displayed_entry, 1,
1480 &s->commits);
1481 break;
1482 case KEY_PPAGE:
1483 if (s->first_displayed_entry == NULL)
1484 break;
1485 if (TAILQ_FIRST(&s->commits.head) ==
1486 s->first_displayed_entry) {
1487 s->selected = 0;
1488 break;
1490 scroll_up(&s->first_displayed_entry,
1491 view->nlines, &s->commits);
1492 break;
1493 case 'j':
1494 case KEY_DOWN:
1495 if (s->first_displayed_entry == NULL)
1496 break;
1497 if (s->selected < MIN(view->nlines - 2,
1498 s->commits.ncommits - 1)) {
1499 s->selected++;
1500 break;
1502 err = scroll_down(&s->first_displayed_entry, 1,
1503 &s->last_displayed_entry, &s->commits,
1504 &s->thread_args.log_complete,
1505 &s->thread_args.commits_needed,
1506 &s->thread_args.need_commits);
1507 break;
1508 case KEY_NPAGE: {
1509 struct commit_queue_entry *first;
1510 first = s->first_displayed_entry;
1511 if (first == NULL)
1512 break;
1513 err = scroll_down(&s->first_displayed_entry,
1514 view->nlines, &s->last_displayed_entry,
1515 &s->commits, &s->thread_args.log_complete,
1516 &s->thread_args.commits_needed,
1517 &s->thread_args.need_commits);
1518 if (first == s->first_displayed_entry &&
1519 s->selected < MIN(view->nlines - 2,
1520 s->commits.ncommits - 1)) {
1521 /* can't scroll further down */
1522 s->selected = MIN(view->nlines - 2,
1523 s->commits.ncommits - 1);
1525 err = NULL;
1526 break;
1528 case KEY_RESIZE:
1529 if (s->selected > view->nlines - 2)
1530 s->selected = view->nlines - 2;
1531 if (s->selected > s->commits.ncommits - 1)
1532 s->selected = s->commits.ncommits - 1;
1533 break;
1534 case KEY_ENTER:
1535 case '\r':
1536 if (s->selected_entry == NULL)
1537 break;
1538 if (view_is_parent_view(view))
1539 begin_x = view_split_begin_x(view->begin_x);
1540 err = open_diff_view_for_commit(&diff_view, begin_x,
1541 s->selected_entry->id, s->selected_entry->commit,
1542 s->repo);
1543 if (err)
1544 break;
1545 if (view_is_parent_view(view)) {
1546 err = view_close_child(view);
1547 if (err)
1548 return err;
1549 err = view_set_child(view, diff_view);
1550 if (err) {
1551 view_close(diff_view);
1552 break;
1554 *focus_view = diff_view;
1555 view->child_focussed = 1;
1556 } else
1557 *new_view = diff_view;
1558 break;
1559 case 't':
1560 if (s->selected_entry == NULL)
1561 break;
1562 if (view_is_parent_view(view))
1563 begin_x = view_split_begin_x(view->begin_x);
1564 err = browse_commit(&tree_view, begin_x,
1565 s->selected_entry, s->repo);
1566 if (err)
1567 break;
1568 if (view_is_parent_view(view)) {
1569 err = view_close_child(view);
1570 if (err)
1571 return err;
1572 err = view_set_child(view, tree_view);
1573 if (err) {
1574 view_close(tree_view);
1575 break;
1577 *focus_view = tree_view;
1578 view->child_focussed = 1;
1579 } else
1580 *new_view = tree_view;
1581 break;
1582 case KEY_BACKSPACE:
1583 if (strcmp(s->in_repo_path, "/") == 0)
1584 break;
1585 parent_path = dirname(s->in_repo_path);
1586 if (parent_path && strcmp(parent_path, ".") != 0) {
1587 struct tog_view *lv;
1588 err = stop_log_thread(s);
1589 if (err)
1590 return err;
1591 lv = view_open(view->nlines, view->ncols,
1592 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1593 if (lv == NULL)
1594 return got_error_from_errno();
1595 err = open_log_view(lv, s->start_id, s->repo,
1596 parent_path, 0);
1597 if (err)
1598 return err;;
1599 if (view_is_parent_view(view))
1600 *new_view = lv;
1601 else {
1602 view_set_child(view->parent, lv);
1603 *focus_view = lv;
1605 return NULL;
1607 break;
1608 default:
1609 break;
1612 return err;
1615 static const struct got_error *
1616 apply_unveil(const char *repo_path, const char *worktree_path)
1618 const struct got_error *error;
1620 if (repo_path && unveil(repo_path, "r") != 0)
1621 return got_error_from_errno();
1623 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1624 return got_error_from_errno();
1626 if (unveil("/tmp", "rwc") != 0)
1627 return got_error_from_errno();
1629 error = got_privsep_unveil_exec_helpers();
1630 if (error != NULL)
1631 return error;
1633 if (unveil(NULL, NULL) != 0)
1634 return got_error_from_errno();
1636 return NULL;
1639 static void
1640 init_curses(void)
1642 initscr();
1643 cbreak();
1644 halfdelay(1); /* Do fast refresh while initial view is loading. */
1645 noecho();
1646 nonl();
1647 intrflush(stdscr, FALSE);
1648 keypad(stdscr, TRUE);
1649 curs_set(0);
1650 signal(SIGWINCH, tog_sigwinch);
1653 static const struct got_error *
1654 cmd_log(int argc, char *argv[])
1656 const struct got_error *error;
1657 struct got_repository *repo = NULL;
1658 struct got_object_id *start_id = NULL;
1659 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1660 char *start_commit = NULL;
1661 int ch;
1662 struct tog_view *view;
1664 #ifndef PROFILE
1665 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1666 NULL) == -1)
1667 err(1, "pledge");
1668 #endif
1670 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1671 switch (ch) {
1672 case 'c':
1673 start_commit = optarg;
1674 break;
1675 case 'r':
1676 repo_path = realpath(optarg, NULL);
1677 if (repo_path == NULL)
1678 err(1, "-r option");
1679 break;
1680 default:
1681 usage();
1682 /* NOTREACHED */
1686 argc -= optind;
1687 argv += optind;
1689 if (argc == 0)
1690 path = strdup("");
1691 else if (argc == 1)
1692 path = strdup(argv[0]);
1693 else
1694 usage_log();
1695 if (path == NULL)
1696 return got_error_from_errno();
1698 cwd = getcwd(NULL, 0);
1699 if (cwd == NULL) {
1700 error = got_error_from_errno();
1701 goto done;
1703 if (repo_path == NULL) {
1704 struct got_worktree *worktree;
1705 error = got_worktree_open(&worktree, cwd);
1706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1707 goto done;
1708 if (worktree) {
1709 repo_path =
1710 strdup(got_worktree_get_repo_path(worktree));
1711 got_worktree_close(worktree);
1712 } else
1713 repo_path = strdup(cwd);
1714 if (repo_path == NULL) {
1715 error = got_error_from_errno();
1716 goto done;
1720 init_curses();
1722 error = apply_unveil(repo_path, NULL);
1723 if (error)
1724 goto done;
1726 error = got_repo_open(&repo, repo_path);
1727 if (error != NULL)
1728 goto done;
1730 if (start_commit == NULL)
1731 error = get_head_commit_id(&start_id, repo);
1732 else
1733 error = got_object_resolve_id_str(&start_id, repo,
1734 start_commit);
1735 if (error != NULL)
1736 goto done;
1738 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1739 if (view == NULL) {
1740 error = got_error_from_errno();
1741 goto done;
1743 error = open_log_view(view, start_id, repo, path, 1);
1744 if (error)
1745 goto done;
1746 error = view_loop(view);
1747 done:
1748 free(repo_path);
1749 free(cwd);
1750 free(path);
1751 free(start_id);
1752 if (repo)
1753 got_repo_close(repo);
1754 return error;
1757 __dead static void
1758 usage_diff(void)
1760 endwin();
1761 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1762 getprogname());
1763 exit(1);
1766 static char *
1767 parse_next_line(FILE *f, size_t *len)
1769 char *line;
1770 size_t linelen;
1771 size_t lineno;
1772 const char delim[3] = { '\0', '\0', '\0'};
1774 line = fparseln(f, &linelen, &lineno, delim, 0);
1775 if (len)
1776 *len = linelen;
1777 return line;
1780 static const struct got_error *
1781 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1782 int *last_displayed_line, int *eof, int max_lines,
1783 char * header)
1785 const struct got_error *err;
1786 int nlines = 0, nprinted = 0;
1787 char *line;
1788 size_t len;
1789 wchar_t *wline;
1790 int width;
1792 rewind(f);
1793 werase(view->window);
1795 if (header) {
1796 err = format_line(&wline, &width, header, view->ncols);
1797 if (err) {
1798 return err;
1801 if (view_needs_focus_indication(view))
1802 wstandout(view->window);
1803 waddwstr(view->window, wline);
1804 if (view_needs_focus_indication(view))
1805 wstandend(view->window);
1806 if (width < view->ncols)
1807 waddch(view->window, '\n');
1809 if (max_lines <= 1)
1810 return NULL;
1811 max_lines--;
1814 *eof = 0;
1815 while (nprinted < max_lines) {
1816 line = parse_next_line(f, &len);
1817 if (line == NULL) {
1818 *eof = 1;
1819 break;
1821 if (++nlines < *first_displayed_line) {
1822 free(line);
1823 continue;
1826 err = format_line(&wline, &width, line, view->ncols);
1827 if (err) {
1828 free(line);
1829 return err;
1831 waddwstr(view->window, wline);
1832 if (width < view->ncols)
1833 waddch(view->window, '\n');
1834 if (++nprinted == 1)
1835 *first_displayed_line = nlines;
1836 free(line);
1837 free(wline);
1838 wline = NULL;
1840 *last_displayed_line = nlines;
1842 view_vborder(view);
1844 return NULL;
1847 static char *
1848 get_datestr(time_t *time, char *datebuf)
1850 char *p, *s = ctime_r(time, datebuf);
1851 p = strchr(s, '\n');
1852 if (p)
1853 *p = '\0';
1854 return s;
1857 static const struct got_error *
1858 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1859 FILE *outfile)
1861 const struct got_error *err = NULL;
1862 char datebuf[26];
1863 struct got_commit_object *commit;
1864 char *id_str = NULL;
1865 time_t committer_time;
1866 const char *author, *committer;
1868 err = got_object_open_as_commit(&commit, repo, commit_id);
1869 if (err)
1870 return err;
1872 err = got_object_id_str(&id_str, commit_id);
1873 if (err) {
1874 err = got_error_from_errno();
1875 goto done;
1878 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1879 err = got_error_from_errno();
1880 goto done;
1882 if (fprintf(outfile, "from: %s\n",
1883 got_object_commit_get_author(commit)) < 0) {
1884 err = got_error_from_errno();
1885 goto done;
1887 committer_time = got_object_commit_get_committer_time(commit);
1888 if (fprintf(outfile, "date: %s UTC\n",
1889 get_datestr(&committer_time, datebuf)) < 0) {
1890 err = got_error_from_errno();
1891 goto done;
1893 author = got_object_commit_get_author(commit);
1894 committer = got_object_commit_get_committer(commit);
1895 if (strcmp(author, committer) != 0 &&
1896 fprintf(outfile, "via: %s\n", committer) < 0) {
1897 err = got_error_from_errno();
1898 goto done;
1900 if (fprintf(outfile, "%s\n",
1901 got_object_commit_get_logmsg(commit)) < 0) {
1902 err = got_error_from_errno();
1903 goto done;
1905 done:
1906 free(id_str);
1907 got_object_commit_close(commit);
1908 return err;
1911 static const struct got_error *
1912 create_diff(struct tog_diff_view_state *s)
1914 const struct got_error *err = NULL;
1915 FILE *f = NULL;
1916 int obj_type;
1918 f = got_opentemp();
1919 if (f == NULL) {
1920 err = got_error_from_errno();
1921 goto done;
1923 if (s->f)
1924 fclose(s->f);
1925 s->f = f;
1927 if (s->id1)
1928 err = got_object_get_type(&obj_type, s->repo, s->id1);
1929 else
1930 err = got_object_get_type(&obj_type, s->repo, s->id2);
1931 if (err)
1932 goto done;
1934 switch (obj_type) {
1935 case GOT_OBJ_TYPE_BLOB:
1936 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1937 s->diff_context, s->repo, f);
1938 break;
1939 case GOT_OBJ_TYPE_TREE:
1940 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1941 s->diff_context, s->repo, f);
1942 break;
1943 case GOT_OBJ_TYPE_COMMIT: {
1944 const struct got_object_id_queue *parent_ids;
1945 struct got_object_qid *pid;
1946 struct got_commit_object *commit2;
1948 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1949 if (err)
1950 break;
1951 /* Show commit info if we're diffing to a parent commit. */
1952 parent_ids = got_object_commit_get_parent_ids(commit2);
1953 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1954 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1955 write_commit_info(s->id2, s->repo, f);
1956 break;
1959 got_object_commit_close(commit2);
1961 err = got_diff_objects_as_commits(s->id1, s->id2,
1962 s->diff_context, s->repo, f);
1963 break;
1965 default:
1966 err = got_error(GOT_ERR_OBJ_TYPE);
1967 break;
1969 done:
1970 if (f)
1971 fflush(f);
1972 return err;
1975 static const struct got_error *
1976 open_diff_view(struct tog_view *view, struct got_object_id *id1,
1977 struct got_object_id *id2, struct got_repository *repo)
1979 const struct got_error *err;
1981 if (id1 != NULL && id2 != NULL) {
1982 int type1, type2;
1983 err = got_object_get_type(&type1, repo, id1);
1984 if (err)
1985 return err;
1986 err = got_object_get_type(&type2, repo, id2);
1987 if (err)
1988 return err;
1990 if (type1 != type2)
1991 return got_error(GOT_ERR_OBJ_TYPE);
1994 if (id1) {
1995 view->state.diff.id1 = got_object_id_dup(id1);
1996 if (view->state.diff.id1 == NULL)
1997 return got_error_from_errno();
1998 } else
1999 view->state.diff.id1 = NULL;
2001 view->state.diff.id2 = got_object_id_dup(id2);
2002 if (view->state.diff.id2 == NULL) {
2003 free(view->state.diff.id1);
2004 view->state.diff.id1 = NULL;
2005 return got_error_from_errno();
2007 view->state.diff.f = NULL;
2008 view->state.diff.first_displayed_line = 1;
2009 view->state.diff.last_displayed_line = view->nlines;
2010 view->state.diff.diff_context = 3;
2011 view->state.diff.repo = repo;
2013 err = create_diff(&view->state.diff);
2014 if (err) {
2015 free(view->state.diff.id1);
2016 view->state.diff.id1 = NULL;
2017 free(view->state.diff.id2);
2018 view->state.diff.id2 = NULL;
2019 return err;
2022 view->show = show_diff_view;
2023 view->input = input_diff_view;
2024 view->close = close_diff_view;
2026 return NULL;
2029 static const struct got_error *
2030 close_diff_view(struct tog_view *view)
2032 const struct got_error *err = NULL;
2034 free(view->state.diff.id1);
2035 view->state.diff.id1 = NULL;
2036 free(view->state.diff.id2);
2037 view->state.diff.id2 = NULL;
2038 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2039 err = got_error_from_errno();
2040 return err;
2043 static const struct got_error *
2044 show_diff_view(struct tog_view *view)
2046 const struct got_error *err;
2047 struct tog_diff_view_state *s = &view->state.diff;
2048 char *id_str1 = NULL, *id_str2, *header;
2050 if (s->id1) {
2051 err = got_object_id_str(&id_str1, s->id1);
2052 if (err)
2053 return err;
2055 err = got_object_id_str(&id_str2, s->id2);
2056 if (err)
2057 return err;
2059 if (asprintf(&header, "diff %s %s",
2060 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2061 err = got_error_from_errno();
2062 free(id_str1);
2063 free(id_str2);
2064 return err;
2066 free(id_str1);
2067 free(id_str2);
2069 return draw_file(view, s->f, &s->first_displayed_line,
2070 &s->last_displayed_line, &s->eof, view->nlines,
2071 header);
2074 static const struct got_error *
2075 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2076 struct tog_view **focus_view, struct tog_view *view, int ch)
2078 const struct got_error *err = NULL;
2079 struct tog_diff_view_state *s = &view->state.diff;
2080 int i;
2082 switch (ch) {
2083 case 'k':
2084 case KEY_UP:
2085 if (s->first_displayed_line > 1)
2086 s->first_displayed_line--;
2087 break;
2088 case KEY_PPAGE:
2089 i = 0;
2090 while (i++ < view->nlines - 1 &&
2091 s->first_displayed_line > 1)
2092 s->first_displayed_line--;
2093 break;
2094 case 'j':
2095 case KEY_DOWN:
2096 if (!s->eof)
2097 s->first_displayed_line++;
2098 break;
2099 case KEY_NPAGE:
2100 case ' ':
2101 i = 0;
2102 while (!s->eof && i++ < view->nlines - 1) {
2103 char *line;
2104 line = parse_next_line(s->f, NULL);
2105 s->first_displayed_line++;
2106 if (line == NULL)
2107 break;
2109 break;
2110 case '[':
2111 if (s->diff_context > 0) {
2112 s->diff_context--;
2113 err = create_diff(s);
2115 break;
2116 case ']':
2117 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2118 s->diff_context++;
2119 err = create_diff(s);
2121 break;
2122 default:
2123 break;
2126 return err;
2129 static const struct got_error *
2130 cmd_diff(int argc, char *argv[])
2132 const struct got_error *error = NULL;
2133 struct got_repository *repo = NULL;
2134 struct got_object_id *id1 = NULL, *id2 = NULL;
2135 char *repo_path = NULL;
2136 char *id_str1 = NULL, *id_str2 = NULL;
2137 int ch;
2138 struct tog_view *view;
2140 #ifndef PROFILE
2141 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2142 NULL) == -1)
2143 err(1, "pledge");
2144 #endif
2146 while ((ch = getopt(argc, argv, "")) != -1) {
2147 switch (ch) {
2148 default:
2149 usage();
2150 /* NOTREACHED */
2154 argc -= optind;
2155 argv += optind;
2157 if (argc == 0) {
2158 usage_diff(); /* TODO show local worktree changes */
2159 } else if (argc == 2) {
2160 repo_path = getcwd(NULL, 0);
2161 if (repo_path == NULL)
2162 return got_error_from_errno();
2163 id_str1 = argv[0];
2164 id_str2 = argv[1];
2165 } else if (argc == 3) {
2166 repo_path = realpath(argv[0], NULL);
2167 if (repo_path == NULL)
2168 return got_error_from_errno();
2169 id_str1 = argv[1];
2170 id_str2 = argv[2];
2171 } else
2172 usage_diff();
2174 init_curses();
2176 error = apply_unveil(repo_path, NULL);
2177 if (error)
2178 goto done;
2180 error = got_repo_open(&repo, repo_path);
2181 free(repo_path);
2182 if (error)
2183 goto done;
2185 error = got_object_resolve_id_str(&id1, repo, id_str1);
2186 if (error)
2187 goto done;
2189 error = got_object_resolve_id_str(&id2, repo, id_str2);
2190 if (error)
2191 goto done;
2193 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2194 if (view == NULL) {
2195 error = got_error_from_errno();
2196 goto done;
2198 error = open_diff_view(view, id1, id2, repo);
2199 if (error)
2200 goto done;
2201 error = view_loop(view);
2202 done:
2203 got_repo_close(repo);
2204 return error;
2207 __dead static void
2208 usage_blame(void)
2210 endwin();
2211 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2212 getprogname());
2213 exit(1);
2216 struct tog_blame_line {
2217 int annotated;
2218 struct got_object_id *id;
2221 static const struct got_error *
2222 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2223 const char *path, struct tog_blame_line *lines, int nlines,
2224 int blame_complete, int selected_line, int *first_displayed_line,
2225 int *last_displayed_line, int *eof, int max_lines)
2227 const struct got_error *err;
2228 int lineno = 0, nprinted = 0;
2229 char *line;
2230 size_t len;
2231 wchar_t *wline;
2232 int width, wlimit;
2233 struct tog_blame_line *blame_line;
2234 struct got_object_id *prev_id = NULL;
2235 char *id_str;
2237 err = got_object_id_str(&id_str, id);
2238 if (err)
2239 return err;
2241 rewind(f);
2242 werase(view->window);
2244 if (asprintf(&line, "commit %s", id_str) == -1) {
2245 err = got_error_from_errno();
2246 free(id_str);
2247 return err;
2250 err = format_line(&wline, &width, line, view->ncols);
2251 free(line);
2252 line = NULL;
2253 if (view_needs_focus_indication(view))
2254 wstandout(view->window);
2255 waddwstr(view->window, wline);
2256 if (view_needs_focus_indication(view))
2257 wstandend(view->window);
2258 free(wline);
2259 wline = NULL;
2260 if (width < view->ncols)
2261 waddch(view->window, '\n');
2263 if (asprintf(&line, "[%d/%d] %s%s",
2264 *first_displayed_line - 1 + selected_line, nlines,
2265 blame_complete ? "" : "annotating ", path) == -1) {
2266 free(id_str);
2267 return got_error_from_errno();
2269 free(id_str);
2270 err = format_line(&wline, &width, line, view->ncols);
2271 free(line);
2272 line = NULL;
2273 if (err)
2274 return err;
2275 waddwstr(view->window, wline);
2276 free(wline);
2277 wline = NULL;
2278 if (width < view->ncols)
2279 waddch(view->window, '\n');
2281 *eof = 0;
2282 while (nprinted < max_lines - 2) {
2283 line = parse_next_line(f, &len);
2284 if (line == NULL) {
2285 *eof = 1;
2286 break;
2288 if (++lineno < *first_displayed_line) {
2289 free(line);
2290 continue;
2293 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2294 err = format_line(&wline, &width, line, wlimit);
2295 if (err) {
2296 free(line);
2297 return err;
2300 if (view->focussed && nprinted == selected_line - 1)
2301 wstandout(view->window);
2303 blame_line = &lines[lineno - 1];
2304 if (blame_line->annotated && prev_id &&
2305 got_object_id_cmp(prev_id, blame_line->id) == 0)
2306 waddstr(view->window, " ");
2307 else if (blame_line->annotated) {
2308 char *id_str;
2309 err = got_object_id_str(&id_str, blame_line->id);
2310 if (err) {
2311 free(line);
2312 free(wline);
2313 return err;
2315 wprintw(view->window, "%.8s ", id_str);
2316 free(id_str);
2317 prev_id = blame_line->id;
2318 } else {
2319 waddstr(view->window, "........ ");
2320 prev_id = NULL;
2323 waddwstr(view->window, wline);
2324 while (width < wlimit) {
2325 waddch(view->window, ' ');
2326 width++;
2328 if (view->focussed && nprinted == selected_line - 1)
2329 wstandend(view->window);
2330 if (++nprinted == 1)
2331 *first_displayed_line = lineno;
2332 free(line);
2333 free(wline);
2334 wline = NULL;
2336 *last_displayed_line = lineno;
2338 view_vborder(view);
2340 return NULL;
2343 static const struct got_error *
2344 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2346 const struct got_error *err = NULL;
2347 struct tog_blame_cb_args *a = arg;
2348 struct tog_blame_line *line;
2349 int errcode;
2351 if (nlines != a->nlines ||
2352 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2353 return got_error(GOT_ERR_RANGE);
2355 errcode = pthread_mutex_lock(&tog_mutex);
2356 if (errcode)
2357 return got_error_set_errno(errcode);
2359 if (*a->quit) { /* user has quit the blame view */
2360 err = got_error(GOT_ERR_ITER_COMPLETED);
2361 goto done;
2364 if (lineno == -1)
2365 goto done; /* no change in this commit */
2367 line = &a->lines[lineno - 1];
2368 if (line->annotated)
2369 goto done;
2371 line->id = got_object_id_dup(id);
2372 if (line->id == NULL) {
2373 err = got_error_from_errno();
2374 goto done;
2376 line->annotated = 1;
2377 done:
2378 errcode = pthread_mutex_unlock(&tog_mutex);
2379 if (errcode)
2380 err = got_error_set_errno(errcode);
2381 return err;
2384 static void *
2385 blame_thread(void *arg)
2387 const struct got_error *err;
2388 struct tog_blame_thread_args *ta = arg;
2389 struct tog_blame_cb_args *a = ta->cb_args;
2390 int errcode;
2392 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2393 blame_cb, ta->cb_args);
2395 errcode = pthread_mutex_lock(&tog_mutex);
2396 if (errcode)
2397 return (void *)got_error_set_errno(errcode);
2399 got_repo_close(ta->repo);
2400 ta->repo = NULL;
2401 *ta->complete = 1;
2403 errcode = pthread_mutex_unlock(&tog_mutex);
2404 if (errcode && err == NULL)
2405 err = got_error_set_errno(errcode);
2407 return (void *)err;
2410 static struct got_object_id *
2411 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2412 int selected_line)
2414 struct tog_blame_line *line;
2416 line = &lines[first_displayed_line - 1 + selected_line - 1];
2417 if (!line->annotated)
2418 return NULL;
2420 return line->id;
2423 static const struct got_error *
2424 stop_blame(struct tog_blame *blame)
2426 const struct got_error *err = NULL;
2427 int i;
2429 if (blame->thread) {
2430 int errcode;
2431 errcode = pthread_mutex_unlock(&tog_mutex);
2432 if (errcode)
2433 return got_error_set_errno(errcode);
2434 errcode = pthread_join(blame->thread, (void **)&err);
2435 if (errcode)
2436 return got_error_set_errno(errcode);
2437 errcode = pthread_mutex_lock(&tog_mutex);
2438 if (errcode)
2439 return got_error_set_errno(errcode);
2440 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2441 err = NULL;
2442 blame->thread = NULL;
2444 if (blame->thread_args.repo) {
2445 got_repo_close(blame->thread_args.repo);
2446 blame->thread_args.repo = NULL;
2448 if (blame->f) {
2449 fclose(blame->f);
2450 blame->f = NULL;
2452 if (blame->lines) {
2453 for (i = 0; i < blame->nlines; i++)
2454 free(blame->lines[i].id);
2455 free(blame->lines);
2456 blame->lines = NULL;
2458 free(blame->cb_args.commit_id);
2459 blame->cb_args.commit_id = NULL;
2461 return err;
2464 static const struct got_error *
2465 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2466 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2467 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2468 struct got_repository *repo)
2470 const struct got_error *err = NULL;
2471 struct got_blob_object *blob = NULL;
2472 struct got_repository *thread_repo = NULL;
2473 struct got_object_id *obj_id = NULL;
2474 int obj_type;
2476 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2477 if (err)
2478 return err;
2479 if (obj_id == NULL)
2480 return got_error(GOT_ERR_NO_OBJ);
2482 err = got_object_get_type(&obj_type, repo, obj_id);
2483 if (err)
2484 goto done;
2486 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2487 err = got_error(GOT_ERR_OBJ_TYPE);
2488 goto done;
2491 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2492 if (err)
2493 goto done;
2494 blame->f = got_opentemp();
2495 if (blame->f == NULL) {
2496 err = got_error_from_errno();
2497 goto done;
2499 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2500 blame->f, blob);
2501 if (err)
2502 goto done;
2504 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2505 if (blame->lines == NULL) {
2506 err = got_error_from_errno();
2507 goto done;
2510 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2511 if (err)
2512 goto done;
2514 blame->cb_args.view = view;
2515 blame->cb_args.lines = blame->lines;
2516 blame->cb_args.nlines = blame->nlines;
2517 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2518 if (blame->cb_args.commit_id == NULL) {
2519 err = got_error_from_errno();
2520 goto done;
2522 blame->cb_args.quit = done;
2524 blame->thread_args.path = path;
2525 blame->thread_args.repo = thread_repo;
2526 blame->thread_args.cb_args = &blame->cb_args;
2527 blame->thread_args.complete = blame_complete;
2528 *blame_complete = 0;
2530 done:
2531 if (blob)
2532 got_object_blob_close(blob);
2533 free(obj_id);
2534 if (err)
2535 stop_blame(blame);
2536 return err;
2539 static const struct got_error *
2540 open_blame_view(struct tog_view *view, char *path,
2541 struct got_object_id *commit_id, struct got_repository *repo)
2543 const struct got_error *err = NULL;
2544 struct tog_blame_view_state *s = &view->state.blame;
2546 SIMPLEQ_INIT(&s->blamed_commits);
2548 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2549 if (err)
2550 return err;
2552 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2553 s->first_displayed_line = 1;
2554 s->last_displayed_line = view->nlines;
2555 s->selected_line = 1;
2556 s->blame_complete = 0;
2557 s->path = path;
2558 if (s->path == NULL)
2559 return got_error_from_errno();
2560 s->repo = repo;
2561 s->commit_id = commit_id;
2562 memset(&s->blame, 0, sizeof(s->blame));
2564 view->show = show_blame_view;
2565 view->input = input_blame_view;
2566 view->close = close_blame_view;
2568 return run_blame(&s->blame, view, &s->blame_complete,
2569 &s->first_displayed_line, &s->last_displayed_line,
2570 &s->selected_line, &s->done, &s->eof, s->path,
2571 s->blamed_commit->id, s->repo);
2574 static const struct got_error *
2575 close_blame_view(struct tog_view *view)
2577 const struct got_error *err = NULL;
2578 struct tog_blame_view_state *s = &view->state.blame;
2580 if (s->blame.thread)
2581 err = stop_blame(&s->blame);
2583 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2584 struct got_object_qid *blamed_commit;
2585 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2586 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2587 got_object_qid_free(blamed_commit);
2590 free(s->path);
2592 return err;
2595 static const struct got_error *
2596 show_blame_view(struct tog_view *view)
2598 const struct got_error *err = NULL;
2599 struct tog_blame_view_state *s = &view->state.blame;
2600 int errcode;
2602 if (s->blame.thread == NULL) {
2603 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2604 &s->blame.thread_args);
2605 if (errcode)
2606 return got_error_set_errno(errcode);
2609 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2610 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2611 s->selected_line, &s->first_displayed_line,
2612 &s->last_displayed_line, &s->eof, view->nlines);
2614 view_vborder(view);
2615 return err;
2618 static const struct got_error *
2619 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2620 struct tog_view **focus_view, struct tog_view *view, int ch)
2622 const struct got_error *err = NULL, *thread_err = NULL;
2623 struct tog_view *diff_view;
2624 struct tog_blame_view_state *s = &view->state.blame;
2625 int begin_x = 0;
2627 switch (ch) {
2628 case 'q':
2629 s->done = 1;
2630 break;
2631 case 'k':
2632 case KEY_UP:
2633 if (s->selected_line > 1)
2634 s->selected_line--;
2635 else if (s->selected_line == 1 &&
2636 s->first_displayed_line > 1)
2637 s->first_displayed_line--;
2638 break;
2639 case KEY_PPAGE:
2640 if (s->first_displayed_line == 1) {
2641 s->selected_line = 1;
2642 break;
2644 if (s->first_displayed_line > view->nlines - 2)
2645 s->first_displayed_line -=
2646 (view->nlines - 2);
2647 else
2648 s->first_displayed_line = 1;
2649 break;
2650 case 'j':
2651 case KEY_DOWN:
2652 if (s->selected_line < view->nlines - 2 &&
2653 s->first_displayed_line +
2654 s->selected_line <= s->blame.nlines)
2655 s->selected_line++;
2656 else if (s->last_displayed_line <
2657 s->blame.nlines)
2658 s->first_displayed_line++;
2659 break;
2660 case 'b':
2661 case 'p': {
2662 struct got_object_id *id = NULL;
2663 id = get_selected_commit_id(s->blame.lines,
2664 s->first_displayed_line, s->selected_line);
2665 if (id == NULL)
2666 break;
2667 if (ch == 'p') {
2668 struct got_commit_object *commit;
2669 struct got_object_qid *pid;
2670 struct got_object_id *blob_id = NULL;
2671 int obj_type;
2672 err = got_object_open_as_commit(&commit,
2673 s->repo, id);
2674 if (err)
2675 break;
2676 pid = SIMPLEQ_FIRST(
2677 got_object_commit_get_parent_ids(commit));
2678 if (pid == NULL) {
2679 got_object_commit_close(commit);
2680 break;
2682 /* Check if path history ends here. */
2683 err = got_object_id_by_path(&blob_id, s->repo,
2684 pid->id, s->path);
2685 if (err) {
2686 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2687 err = NULL;
2688 got_object_commit_close(commit);
2689 break;
2691 err = got_object_get_type(&obj_type, s->repo,
2692 blob_id);
2693 free(blob_id);
2694 /* Can't blame non-blob type objects. */
2695 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2696 got_object_commit_close(commit);
2697 break;
2699 err = got_object_qid_alloc(&s->blamed_commit,
2700 pid->id);
2701 got_object_commit_close(commit);
2702 } else {
2703 if (got_object_id_cmp(id,
2704 s->blamed_commit->id) == 0)
2705 break;
2706 err = got_object_qid_alloc(&s->blamed_commit,
2707 id);
2709 if (err)
2710 break;
2711 s->done = 1;
2712 thread_err = stop_blame(&s->blame);
2713 s->done = 0;
2714 if (thread_err)
2715 break;
2716 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2717 s->blamed_commit, entry);
2718 err = run_blame(&s->blame, view, &s->blame_complete,
2719 &s->first_displayed_line, &s->last_displayed_line,
2720 &s->selected_line, &s->done, &s->eof,
2721 s->path, s->blamed_commit->id, s->repo);
2722 if (err)
2723 break;
2724 break;
2726 case 'B': {
2727 struct got_object_qid *first;
2728 first = SIMPLEQ_FIRST(&s->blamed_commits);
2729 if (!got_object_id_cmp(first->id, s->commit_id))
2730 break;
2731 s->done = 1;
2732 thread_err = stop_blame(&s->blame);
2733 s->done = 0;
2734 if (thread_err)
2735 break;
2736 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2737 got_object_qid_free(s->blamed_commit);
2738 s->blamed_commit =
2739 SIMPLEQ_FIRST(&s->blamed_commits);
2740 err = run_blame(&s->blame, view, &s->blame_complete,
2741 &s->first_displayed_line, &s->last_displayed_line,
2742 &s->selected_line, &s->done, &s->eof, s->path,
2743 s->blamed_commit->id, s->repo);
2744 if (err)
2745 break;
2746 break;
2748 case KEY_ENTER:
2749 case '\r': {
2750 struct got_object_id *id = NULL;
2751 struct got_object_qid *pid;
2752 struct got_commit_object *commit = NULL;
2753 id = get_selected_commit_id(s->blame.lines,
2754 s->first_displayed_line, s->selected_line);
2755 if (id == NULL)
2756 break;
2757 err = got_object_open_as_commit(&commit, s->repo, id);
2758 if (err)
2759 break;
2760 pid = SIMPLEQ_FIRST(
2761 got_object_commit_get_parent_ids(commit));
2762 if (view_is_parent_view(view))
2763 begin_x = view_split_begin_x(view->begin_x);
2764 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2765 if (diff_view == NULL) {
2766 got_object_commit_close(commit);
2767 err = got_error_from_errno();
2768 break;
2770 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2771 id, s->repo);
2772 got_object_commit_close(commit);
2773 if (err) {
2774 view_close(diff_view);
2775 break;
2777 if (view_is_parent_view(view)) {
2778 err = view_close_child(view);
2779 if (err)
2780 break;
2781 err = view_set_child(view, diff_view);
2782 if (err) {
2783 view_close(diff_view);
2784 break;
2786 *focus_view = diff_view;
2787 view->child_focussed = 1;
2788 } else
2789 *new_view = diff_view;
2790 if (err)
2791 break;
2792 break;
2794 case KEY_NPAGE:
2795 case ' ':
2796 if (s->last_displayed_line >= s->blame.nlines &&
2797 s->selected_line < view->nlines - 2) {
2798 s->selected_line = MIN(s->blame.nlines,
2799 view->nlines - 2);
2800 break;
2802 if (s->last_displayed_line + view->nlines - 2
2803 <= s->blame.nlines)
2804 s->first_displayed_line +=
2805 view->nlines - 2;
2806 else
2807 s->first_displayed_line =
2808 s->blame.nlines -
2809 (view->nlines - 3);
2810 break;
2811 case KEY_RESIZE:
2812 if (s->selected_line > view->nlines - 2) {
2813 s->selected_line = MIN(s->blame.nlines,
2814 view->nlines - 2);
2816 break;
2817 default:
2818 break;
2820 return thread_err ? thread_err : err;
2823 static const struct got_error *
2824 cmd_blame(int argc, char *argv[])
2826 const struct got_error *error;
2827 struct got_repository *repo = NULL;
2828 struct got_worktree *worktree = NULL;
2829 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2830 struct got_object_id *commit_id = NULL;
2831 char *commit_id_str = NULL;
2832 int ch;
2833 struct tog_view *view;
2835 #ifndef PROFILE
2836 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2837 NULL) == -1)
2838 err(1, "pledge");
2839 #endif
2841 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2842 switch (ch) {
2843 case 'c':
2844 commit_id_str = optarg;
2845 break;
2846 case 'r':
2847 repo_path = realpath(optarg, NULL);
2848 if (repo_path == NULL)
2849 err(1, "-r option");
2850 break;
2851 default:
2852 usage();
2853 /* NOTREACHED */
2857 argc -= optind;
2858 argv += optind;
2860 if (argc == 1)
2861 path = argv[0];
2862 else
2863 usage_blame();
2865 cwd = getcwd(NULL, 0);
2866 if (cwd == NULL) {
2867 error = got_error_from_errno();
2868 goto done;
2870 if (repo_path == NULL) {
2871 error = got_worktree_open(&worktree, cwd);
2872 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2873 goto done;
2874 else
2875 error = NULL;
2876 if (worktree) {
2877 repo_path =
2878 strdup(got_worktree_get_repo_path(worktree));
2879 if (repo_path == NULL)
2880 error = got_error_from_errno();
2881 if (error)
2882 goto done;
2883 } else {
2884 repo_path = strdup(cwd);
2885 if (repo_path == NULL) {
2886 error = got_error_from_errno();
2887 goto done;
2892 init_curses();
2894 error = apply_unveil(repo_path, NULL);
2895 if (error)
2896 goto done;
2898 error = got_repo_open(&repo, repo_path);
2899 if (error != NULL)
2900 goto done;
2902 if (worktree) {
2903 const char *prefix = got_worktree_get_path_prefix(worktree);
2904 char *p, *worktree_subdir = cwd +
2905 strlen(got_worktree_get_root_path(worktree));
2906 if (asprintf(&p, "%s%s%s%s%s",
2907 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2908 worktree_subdir, worktree_subdir[0] ? "/" : "",
2909 path) == -1) {
2910 error = got_error_from_errno();
2911 goto done;
2913 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2914 free(p);
2915 } else {
2916 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2918 if (error)
2919 goto done;
2921 if (commit_id_str == NULL) {
2922 struct got_reference *head_ref;
2923 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2924 if (error != NULL)
2925 goto done;
2926 error = got_ref_resolve(&commit_id, repo, head_ref);
2927 got_ref_close(head_ref);
2928 } else {
2929 error = got_object_resolve_id_str(&commit_id, repo,
2930 commit_id_str);
2932 if (error != NULL)
2933 goto done;
2935 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2936 if (view == NULL) {
2937 error = got_error_from_errno();
2938 goto done;
2940 error = open_blame_view(view, in_repo_path, commit_id, repo);
2941 if (error)
2942 goto done;
2943 error = view_loop(view);
2944 done:
2945 free(repo_path);
2946 free(cwd);
2947 free(commit_id);
2948 if (worktree)
2949 got_worktree_close(worktree);
2950 if (repo)
2951 got_repo_close(repo);
2952 return error;
2955 static const struct got_error *
2956 draw_tree_entries(struct tog_view *view,
2957 struct got_tree_entry **first_displayed_entry,
2958 struct got_tree_entry **last_displayed_entry,
2959 struct got_tree_entry **selected_entry, int *ndisplayed,
2960 const char *label, int show_ids, const char *parent_path,
2961 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2963 const struct got_error *err = NULL;
2964 struct got_tree_entry *te;
2965 wchar_t *wline;
2966 int width, n;
2968 *ndisplayed = 0;
2970 werase(view->window);
2972 if (limit == 0)
2973 return NULL;
2975 err = format_line(&wline, &width, label, view->ncols);
2976 if (err)
2977 return err;
2978 if (view_needs_focus_indication(view))
2979 wstandout(view->window);
2980 waddwstr(view->window, wline);
2981 if (view_needs_focus_indication(view))
2982 wstandend(view->window);
2983 free(wline);
2984 wline = NULL;
2985 if (width < view->ncols)
2986 waddch(view->window, '\n');
2987 if (--limit <= 0)
2988 return NULL;
2989 err = format_line(&wline, &width, parent_path, view->ncols);
2990 if (err)
2991 return err;
2992 waddwstr(view->window, wline);
2993 free(wline);
2994 wline = NULL;
2995 if (width < view->ncols)
2996 waddch(view->window, '\n');
2997 if (--limit <= 0)
2998 return NULL;
2999 waddch(view->window, '\n');
3000 if (--limit <= 0)
3001 return NULL;
3003 te = SIMPLEQ_FIRST(&entries->head);
3004 if (*first_displayed_entry == NULL) {
3005 if (selected == 0) {
3006 if (view->focussed)
3007 wstandout(view->window);
3008 *selected_entry = NULL;
3010 waddstr(view->window, " ..\n"); /* parent directory */
3011 if (selected == 0 && view->focussed)
3012 wstandend(view->window);
3013 (*ndisplayed)++;
3014 if (--limit <= 0)
3015 return NULL;
3016 n = 1;
3017 } else {
3018 n = 0;
3019 while (te != *first_displayed_entry)
3020 te = SIMPLEQ_NEXT(te, entry);
3023 while (te) {
3024 char *line = NULL, *id_str = NULL;
3026 if (show_ids) {
3027 err = got_object_id_str(&id_str, te->id);
3028 if (err)
3029 return got_error_from_errno();
3031 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3032 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
3033 free(id_str);
3034 return got_error_from_errno();
3036 free(id_str);
3037 err = format_line(&wline, &width, line, view->ncols);
3038 if (err) {
3039 free(line);
3040 break;
3042 if (n == selected) {
3043 if (view->focussed)
3044 wstandout(view->window);
3045 *selected_entry = te;
3047 waddwstr(view->window, wline);
3048 if (width < view->ncols)
3049 waddch(view->window, '\n');
3050 if (n == selected && view->focussed)
3051 wstandend(view->window);
3052 free(line);
3053 free(wline);
3054 wline = NULL;
3055 n++;
3056 (*ndisplayed)++;
3057 *last_displayed_entry = te;
3058 if (--limit <= 0)
3059 break;
3060 te = SIMPLEQ_NEXT(te, entry);
3063 return err;
3066 static void
3067 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3068 const struct got_tree_entries *entries, int isroot)
3070 struct got_tree_entry *te, *prev;
3071 int i;
3073 if (*first_displayed_entry == NULL)
3074 return;
3076 te = SIMPLEQ_FIRST(&entries->head);
3077 if (*first_displayed_entry == te) {
3078 if (!isroot)
3079 *first_displayed_entry = NULL;
3080 return;
3083 /* XXX this is stupid... switch to TAILQ? */
3084 for (i = 0; i < maxscroll; i++) {
3085 while (te != *first_displayed_entry) {
3086 prev = te;
3087 te = SIMPLEQ_NEXT(te, entry);
3089 *first_displayed_entry = prev;
3090 te = SIMPLEQ_FIRST(&entries->head);
3092 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3093 *first_displayed_entry = NULL;
3096 static int
3097 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3098 struct got_tree_entry *last_displayed_entry,
3099 const struct got_tree_entries *entries)
3101 struct got_tree_entry *next, *last;
3102 int n = 0;
3104 if (*first_displayed_entry)
3105 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3106 else
3107 next = SIMPLEQ_FIRST(&entries->head);
3108 last = last_displayed_entry;
3109 while (next && last && n++ < maxscroll) {
3110 last = SIMPLEQ_NEXT(last, entry);
3111 if (last) {
3112 *first_displayed_entry = next;
3113 next = SIMPLEQ_NEXT(next, entry);
3116 return n;
3119 static const struct got_error *
3120 tree_entry_path(char **path, struct tog_parent_trees *parents,
3121 struct got_tree_entry *te)
3123 const struct got_error *err = NULL;
3124 struct tog_parent_tree *pt;
3125 size_t len = 2; /* for leading slash and NUL */
3127 TAILQ_FOREACH(pt, parents, entry)
3128 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3129 if (te)
3130 len += strlen(te->name);
3132 *path = calloc(1, len);
3133 if (path == NULL)
3134 return got_error_from_errno();
3136 (*path)[0] = '/';
3137 pt = TAILQ_LAST(parents, tog_parent_trees);
3138 while (pt) {
3139 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3140 err = got_error(GOT_ERR_NO_SPACE);
3141 goto done;
3143 if (strlcat(*path, "/", len) >= len) {
3144 err = got_error(GOT_ERR_NO_SPACE);
3145 goto done;
3147 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3149 if (te) {
3150 if (strlcat(*path, te->name, len) >= len) {
3151 err = got_error(GOT_ERR_NO_SPACE);
3152 goto done;
3155 done:
3156 if (err) {
3157 free(*path);
3158 *path = NULL;
3160 return err;
3163 static const struct got_error *
3164 blame_tree_entry(struct tog_view **new_view, int begin_x,
3165 struct got_tree_entry *te, struct tog_parent_trees *parents,
3166 struct got_object_id *commit_id, struct got_repository *repo)
3168 const struct got_error *err = NULL;
3169 char *path;
3170 struct tog_view *blame_view;
3172 err = tree_entry_path(&path, parents, te);
3173 if (err)
3174 return err;
3176 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3177 if (blame_view == NULL)
3178 return got_error_from_errno();
3180 err = open_blame_view(blame_view, path, commit_id, repo);
3181 if (err) {
3182 view_close(blame_view);
3183 free(path);
3184 } else
3185 *new_view = blame_view;
3186 return err;
3189 static const struct got_error *
3190 log_tree_entry(struct tog_view **new_view, int begin_x,
3191 struct got_tree_entry *te, struct tog_parent_trees *parents,
3192 struct got_object_id *commit_id, struct got_repository *repo)
3194 struct tog_view *log_view;
3195 const struct got_error *err = NULL;
3196 char *path;
3198 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3199 if (log_view == NULL)
3200 return got_error_from_errno();
3202 err = tree_entry_path(&path, parents, te);
3203 if (err)
3204 return err;
3206 err = open_log_view(log_view, commit_id, repo, path, 0);
3207 if (err)
3208 view_close(log_view);
3209 else
3210 *new_view = log_view;
3211 free(path);
3212 return err;
3215 static const struct got_error *
3216 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3217 struct got_object_id *commit_id, struct got_repository *repo)
3219 const struct got_error *err = NULL;
3220 char *commit_id_str = NULL;
3221 struct tog_tree_view_state *s = &view->state.tree;
3223 TAILQ_INIT(&s->parents);
3225 err = got_object_id_str(&commit_id_str, commit_id);
3226 if (err != NULL)
3227 goto done;
3229 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3230 err = got_error_from_errno();
3231 goto done;
3234 s->root = s->tree = root;
3235 s->entries = got_object_tree_get_entries(root);
3236 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3237 s->commit_id = got_object_id_dup(commit_id);
3238 if (s->commit_id == NULL) {
3239 err = got_error_from_errno();
3240 goto done;
3242 s->repo = repo;
3244 view->show = show_tree_view;
3245 view->input = input_tree_view;
3246 view->close = close_tree_view;
3247 done:
3248 free(commit_id_str);
3249 if (err) {
3250 free(s->tree_label);
3251 s->tree_label = NULL;
3253 return err;
3256 static const struct got_error *
3257 close_tree_view(struct tog_view *view)
3259 struct tog_tree_view_state *s = &view->state.tree;
3261 free(s->tree_label);
3262 s->tree_label = NULL;
3263 free(s->commit_id);
3264 s->commit_id = NULL;
3265 while (!TAILQ_EMPTY(&s->parents)) {
3266 struct tog_parent_tree *parent;
3267 parent = TAILQ_FIRST(&s->parents);
3268 TAILQ_REMOVE(&s->parents, parent, entry);
3269 free(parent);
3272 if (s->tree != s->root)
3273 got_object_tree_close(s->tree);
3274 got_object_tree_close(s->root);
3276 return NULL;
3279 static const struct got_error *
3280 show_tree_view(struct tog_view *view)
3282 const struct got_error *err = NULL;
3283 struct tog_tree_view_state *s = &view->state.tree;
3284 char *parent_path;
3286 err = tree_entry_path(&parent_path, &s->parents, NULL);
3287 if (err)
3288 return err;
3290 err = draw_tree_entries(view, &s->first_displayed_entry,
3291 &s->last_displayed_entry, &s->selected_entry,
3292 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3293 s->entries, s->selected, view->nlines, s->tree == s->root);
3294 free(parent_path);
3296 view_vborder(view);
3297 return err;
3300 static const struct got_error *
3301 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3302 struct tog_view **focus_view, struct tog_view *view, int ch)
3304 const struct got_error *err = NULL;
3305 struct tog_tree_view_state *s = &view->state.tree;
3306 struct tog_view *log_view;
3307 int begin_x = 0, nscrolled;
3309 switch (ch) {
3310 case 'i':
3311 s->show_ids = !s->show_ids;
3312 break;
3313 case 'l':
3314 if (!s->selected_entry)
3315 break;
3316 if (view_is_parent_view(view))
3317 begin_x = view_split_begin_x(view->begin_x);
3318 err = log_tree_entry(&log_view, begin_x,
3319 s->selected_entry, &s->parents,
3320 s->commit_id, s->repo);
3321 if (view_is_parent_view(view)) {
3322 err = view_close_child(view);
3323 if (err)
3324 return err;
3325 err = view_set_child(view, log_view);
3326 if (err) {
3327 view_close(log_view);
3328 break;
3330 *focus_view = log_view;
3331 view->child_focussed = 1;
3332 } else
3333 *new_view = log_view;
3334 break;
3335 case 'k':
3336 case KEY_UP:
3337 if (s->selected > 0) {
3338 s->selected--;
3339 if (s->selected == 0)
3340 break;
3342 if (s->selected > 0)
3343 break;
3344 tree_scroll_up(&s->first_displayed_entry, 1,
3345 s->entries, s->tree == s->root);
3346 break;
3347 case KEY_PPAGE:
3348 tree_scroll_up(&s->first_displayed_entry,
3349 MAX(0, view->nlines - 4 - s->selected), s->entries,
3350 s->tree == s->root);
3351 s->selected = 0;
3352 if (SIMPLEQ_FIRST(&s->entries->head) ==
3353 s->first_displayed_entry && s->tree != s->root)
3354 s->first_displayed_entry = NULL;
3355 break;
3356 case 'j':
3357 case KEY_DOWN:
3358 if (s->selected < s->ndisplayed - 1) {
3359 s->selected++;
3360 break;
3362 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3363 == NULL) {
3364 /* can't scroll any further */
3365 break;
3367 tree_scroll_down(&s->first_displayed_entry, 1,
3368 s->last_displayed_entry, s->entries);
3369 break;
3370 case KEY_NPAGE:
3371 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3372 == NULL) {
3373 /* can't scroll any further; move cursor down */
3374 if (s->selected < s->ndisplayed - 1)
3375 s->selected = s->ndisplayed - 1;
3376 break;
3378 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3379 view->nlines, s->last_displayed_entry, s->entries);
3380 if (nscrolled < view->nlines) {
3381 int ndisplayed = 0;
3382 struct got_tree_entry *te;
3383 te = s->first_displayed_entry;
3384 do {
3385 ndisplayed++;
3386 te = SIMPLEQ_NEXT(te, entry);
3387 } while (te);
3388 s->selected = ndisplayed - 1;
3390 break;
3391 case KEY_ENTER:
3392 case '\r':
3393 if (s->selected_entry == NULL) {
3394 struct tog_parent_tree *parent;
3395 case KEY_BACKSPACE:
3396 /* user selected '..' */
3397 if (s->tree == s->root)
3398 break;
3399 parent = TAILQ_FIRST(&s->parents);
3400 TAILQ_REMOVE(&s->parents, parent,
3401 entry);
3402 got_object_tree_close(s->tree);
3403 s->tree = parent->tree;
3404 s->entries =
3405 got_object_tree_get_entries(s->tree);
3406 s->first_displayed_entry =
3407 parent->first_displayed_entry;
3408 s->selected_entry =
3409 parent->selected_entry;
3410 s->selected = parent->selected;
3411 free(parent);
3412 } else if (S_ISDIR(s->selected_entry->mode)) {
3413 struct tog_parent_tree *parent;
3414 struct got_tree_object *child;
3415 err = got_object_open_as_tree(&child,
3416 s->repo, s->selected_entry->id);
3417 if (err)
3418 break;
3419 parent = calloc(1, sizeof(*parent));
3420 if (parent == NULL) {
3421 err = got_error_from_errno();
3422 break;
3424 parent->tree = s->tree;
3425 parent->first_displayed_entry =
3426 s->first_displayed_entry;
3427 parent->selected_entry = s->selected_entry;
3428 parent->selected = s->selected;
3429 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3430 s->tree = child;
3431 s->entries =
3432 got_object_tree_get_entries(s->tree);
3433 s->selected = 0;
3434 s->first_displayed_entry = NULL;
3435 } else if (S_ISREG(s->selected_entry->mode)) {
3436 struct tog_view *blame_view;
3437 int begin_x = view_is_parent_view(view) ?
3438 view_split_begin_x(view->begin_x) : 0;
3440 err = blame_tree_entry(&blame_view, begin_x,
3441 s->selected_entry, &s->parents, s->commit_id,
3442 s->repo);
3443 if (err)
3444 break;
3445 if (view_is_parent_view(view)) {
3446 err = view_close_child(view);
3447 if (err)
3448 return err;
3449 err = view_set_child(view, blame_view);
3450 if (err) {
3451 view_close(blame_view);
3452 break;
3454 *focus_view = blame_view;
3455 view->child_focussed = 1;
3456 } else
3457 *new_view = blame_view;
3459 break;
3460 case KEY_RESIZE:
3461 if (s->selected > view->nlines)
3462 s->selected = s->ndisplayed - 1;
3463 break;
3464 default:
3465 break;
3468 return err;
3471 __dead static void
3472 usage_tree(void)
3474 endwin();
3475 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3476 getprogname());
3477 exit(1);
3480 static const struct got_error *
3481 cmd_tree(int argc, char *argv[])
3483 const struct got_error *error;
3484 struct got_repository *repo = NULL;
3485 char *repo_path = NULL;
3486 struct got_object_id *commit_id = NULL;
3487 char *commit_id_arg = NULL;
3488 struct got_commit_object *commit = NULL;
3489 struct got_tree_object *tree = NULL;
3490 int ch;
3491 struct tog_view *view;
3493 #ifndef PROFILE
3494 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3495 NULL) == -1)
3496 err(1, "pledge");
3497 #endif
3499 while ((ch = getopt(argc, argv, "c:")) != -1) {
3500 switch (ch) {
3501 case 'c':
3502 commit_id_arg = optarg;
3503 break;
3504 default:
3505 usage();
3506 /* NOTREACHED */
3510 argc -= optind;
3511 argv += optind;
3513 if (argc == 0) {
3514 struct got_worktree *worktree;
3515 char *cwd = getcwd(NULL, 0);
3516 if (cwd == NULL)
3517 return got_error_from_errno();
3518 error = got_worktree_open(&worktree, cwd);
3519 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3520 goto done;
3521 if (worktree) {
3522 free(cwd);
3523 repo_path =
3524 strdup(got_worktree_get_repo_path(worktree));
3525 got_worktree_close(worktree);
3526 } else
3527 repo_path = cwd;
3528 if (repo_path == NULL) {
3529 error = got_error_from_errno();
3530 goto done;
3532 } else if (argc == 1) {
3533 repo_path = realpath(argv[0], NULL);
3534 if (repo_path == NULL)
3535 return got_error_from_errno();
3536 } else
3537 usage_log();
3539 init_curses();
3541 error = apply_unveil(repo_path, NULL);
3542 if (error)
3543 goto done;
3545 error = got_repo_open(&repo, repo_path);
3546 if (error != NULL)
3547 goto done;
3549 if (commit_id_arg == NULL)
3550 error = get_head_commit_id(&commit_id, repo);
3551 else
3552 error = got_object_resolve_id_str(&commit_id, repo,
3553 commit_id_arg);
3554 if (error != NULL)
3555 goto done;
3557 error = got_object_open_as_commit(&commit, repo, commit_id);
3558 if (error != NULL)
3559 goto done;
3561 error = got_object_open_as_tree(&tree, repo,
3562 got_object_commit_get_tree_id(commit));
3563 if (error != NULL)
3564 goto done;
3566 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3567 if (view == NULL) {
3568 error = got_error_from_errno();
3569 goto done;
3571 error = open_tree_view(view, tree, commit_id, repo);
3572 if (error)
3573 goto done;
3574 error = view_loop(view);
3575 done:
3576 free(repo_path);
3577 free(commit_id);
3578 if (commit)
3579 got_object_commit_close(commit);
3580 if (tree)
3581 got_object_tree_close(tree);
3582 if (repo)
3583 got_repo_close(repo);
3584 return error;
3587 __dead static void
3588 usage(void)
3590 int i;
3592 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3593 "Available commands:\n", getprogname());
3594 for (i = 0; i < nitems(tog_commands); i++) {
3595 struct tog_cmd *cmd = &tog_commands[i];
3596 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3598 exit(1);
3601 static char **
3602 make_argv(const char *arg0, const char *arg1)
3604 char **argv;
3605 int argc = (arg1 == NULL ? 1 : 2);
3607 argv = calloc(argc, sizeof(char *));
3608 if (argv == NULL)
3609 err(1, "calloc");
3610 argv[0] = strdup(arg0);
3611 if (argv[0] == NULL)
3612 err(1, "calloc");
3613 if (arg1) {
3614 argv[1] = strdup(arg1);
3615 if (argv[1] == NULL)
3616 err(1, "calloc");
3619 return argv;
3622 int
3623 main(int argc, char *argv[])
3625 const struct got_error *error = NULL;
3626 struct tog_cmd *cmd = NULL;
3627 int ch, hflag = 0;
3628 char **cmd_argv = NULL;
3630 setlocale(LC_CTYPE, "");
3632 while ((ch = getopt(argc, argv, "h")) != -1) {
3633 switch (ch) {
3634 case 'h':
3635 hflag = 1;
3636 break;
3637 default:
3638 usage();
3639 /* NOTREACHED */
3643 argc -= optind;
3644 argv += optind;
3645 optind = 0;
3646 optreset = 1;
3648 if (argc == 0) {
3649 if (hflag)
3650 usage();
3651 /* Build an argument vector which runs a default command. */
3652 cmd = &tog_commands[0];
3653 cmd_argv = make_argv(cmd->name, NULL);
3654 argc = 1;
3655 } else {
3656 int i;
3658 /* Did the user specific a command? */
3659 for (i = 0; i < nitems(tog_commands); i++) {
3660 if (strncmp(tog_commands[i].name, argv[0],
3661 strlen(argv[0])) == 0) {
3662 cmd = &tog_commands[i];
3663 if (hflag)
3664 tog_commands[i].cmd_usage();
3665 break;
3668 if (cmd == NULL) {
3669 /* Did the user specify a repository? */
3670 char *repo_path = realpath(argv[0], NULL);
3671 if (repo_path) {
3672 struct got_repository *repo;
3673 error = got_repo_open(&repo, repo_path);
3674 if (error == NULL)
3675 got_repo_close(repo);
3676 } else
3677 error = got_error_from_errno();
3678 if (error) {
3679 if (hflag) {
3680 fprintf(stderr, "%s: '%s' is not a "
3681 "known command\n", getprogname(),
3682 argv[0]);
3683 usage();
3685 fprintf(stderr, "%s: '%s' is neither a known "
3686 "command nor a path to a repository\n",
3687 getprogname(), argv[0]);
3688 free(repo_path);
3689 return 1;
3691 cmd = &tog_commands[0];
3692 cmd_argv = make_argv(cmd->name, repo_path);
3693 argc = 2;
3694 free(repo_path);
3698 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3699 if (error)
3700 goto done;
3701 done:
3702 endwin();
3703 free(cmd_argv);
3704 if (error)
3705 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3706 return 0;