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"
51 #ifndef MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 #ifndef MAX
56 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
57 #endif
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 struct tog_cmd {
65 const char *name;
66 const struct got_error *(*cmd_main)(int, char *[]);
67 void (*cmd_usage)(void);
68 const char *descr;
69 };
71 __dead static void usage(void);
72 __dead static void usage_log(void);
73 __dead static void usage_diff(void);
74 __dead static void usage_blame(void);
75 __dead static void usage_tree(void);
77 static const struct got_error* cmd_log(int, char *[]);
78 static const struct got_error* cmd_diff(int, char *[]);
79 static const struct got_error* cmd_blame(int, char *[]);
80 static const struct got_error* cmd_tree(int, char *[]);
82 static struct tog_cmd tog_commands[] = {
83 { "log", cmd_log, usage_log,
84 "show repository history" },
85 { "diff", cmd_diff, usage_diff,
86 "compare files and directories" },
87 { "blame", cmd_blame, usage_blame,
88 "show line-by-line file history" },
89 { "tree", cmd_tree, usage_tree,
90 "browse trees in repository" },
91 };
93 enum tog_view_type {
94 TOG_VIEW_DIFF,
95 TOG_VIEW_LOG,
96 TOG_VIEW_BLAME,
97 TOG_VIEW_TREE
98 };
100 struct tog_diff_view_state {
101 struct got_object_id *id1, *id2;
102 FILE *f;
103 int first_displayed_line;
104 int last_displayed_line;
105 int eof;
106 int diff_context;
107 struct got_repository *repo;
108 };
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
124 struct tog_log_thread_args {
125 pthread_cond_t need_commits;
126 int commits_needed;
127 struct got_commit_graph *graph;
128 struct commit_queue *commits;
129 const char *in_repo_path;
130 struct got_object_id *start_id;
131 struct got_repository *repo;
132 int log_complete;
133 sig_atomic_t *quit;
134 struct tog_view *view;
135 struct commit_queue_entry **first_displayed_entry;
136 struct commit_queue_entry **selected_entry;
137 };
139 struct tog_log_view_state {
140 struct commit_queue commits;
141 struct commit_queue_entry *first_displayed_entry;
142 struct commit_queue_entry *last_displayed_entry;
143 struct commit_queue_entry *selected_entry;
144 int selected;
145 char *in_repo_path;
146 struct got_repository *repo;
147 struct got_object_id *start_id;
148 sig_atomic_t quit;
149 pthread_t thread;
150 struct tog_log_thread_args thread_args;
151 };
153 struct tog_blame_cb_args {
154 struct tog_blame_line *lines; /* one per line */
155 int nlines;
157 struct tog_view *view;
158 struct got_object_id *commit_id;
159 int *quit;
160 };
162 struct tog_blame_thread_args {
163 const char *path;
164 struct got_repository *repo;
165 struct tog_blame_cb_args *cb_args;
166 int *complete;
167 };
169 struct tog_blame {
170 FILE *f;
171 size_t filesize;
172 struct tog_blame_line *lines;
173 int nlines;
174 pthread_t thread;
175 struct tog_blame_thread_args thread_args;
176 struct tog_blame_cb_args cb_args;
177 const char *path;
178 };
180 struct tog_blame_view_state {
181 int first_displayed_line;
182 int last_displayed_line;
183 int selected_line;
184 int blame_complete;
185 int eof;
186 int done;
187 struct got_object_id_queue blamed_commits;
188 struct got_object_qid *blamed_commit;
189 char *path;
190 struct got_repository *repo;
191 struct got_object_id *commit_id;
192 struct tog_blame blame;
193 };
195 struct tog_parent_tree {
196 TAILQ_ENTRY(tog_parent_tree) entry;
197 struct got_tree_object *tree;
198 struct got_tree_entry *first_displayed_entry;
199 struct got_tree_entry *selected_entry;
200 int selected;
201 };
203 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
205 struct tog_tree_view_state {
206 char *tree_label;
207 struct got_tree_object *root;
208 struct got_tree_object *tree;
209 const struct got_tree_entries *entries;
210 struct got_tree_entry *first_displayed_entry;
211 struct got_tree_entry *last_displayed_entry;
212 struct got_tree_entry *selected_entry;
213 int ndisplayed, selected, show_ids;
214 struct tog_parent_trees parents;
215 struct got_object_id *commit_id;
216 struct got_repository *repo;
217 };
219 /*
220 * We implement two types of views: parent views and child views.
222 * The 'Tab' key switches between a parent view and its child view.
223 * Child views are shown side-by-side to their parent view, provided
224 * there is enough screen estate.
226 * When a new view is opened from within a parent view, this new view
227 * becomes a child view of the parent view, replacing any existing child.
229 * When a new view is opened from within a child view, this new view
230 * becomes a parent view which will obscure the views below until the
231 * user quits the new parent view by typing 'q'.
233 * This list of views contains parent views only.
234 * Child views are only pointed to by their parent view.
235 */
236 TAILQ_HEAD(tog_view_list_head, tog_view);
238 struct tog_view {
239 TAILQ_ENTRY(tog_view) entry;
240 WINDOW *window;
241 PANEL *panel;
242 int nlines, ncols, begin_y, begin_x;
243 int lines, cols; /* copies of LINES and COLS */
244 int focussed;
245 struct tog_view *parent;
246 struct tog_view *child;
247 int child_focussed;
249 /* type-specific state */
250 enum tog_view_type type;
251 union {
252 struct tog_diff_view_state diff;
253 struct tog_log_view_state log;
254 struct tog_blame_view_state blame;
255 struct tog_tree_view_state tree;
256 } state;
258 const struct got_error *(*show)(struct tog_view *);
259 const struct got_error *(*input)(struct tog_view **,
260 struct tog_view **, struct tog_view**, struct tog_view *, int);
261 const struct got_error *(*close)(struct tog_view *);
262 };
264 static const struct got_error *open_diff_view(struct tog_view *,
265 struct got_object_id *, struct got_object_id *, struct got_repository *);
266 static const struct got_error *show_diff_view(struct tog_view *);
267 static const struct got_error *input_diff_view(struct tog_view **,
268 struct tog_view **, struct tog_view **, struct tog_view *, int);
269 static const struct got_error* close_diff_view(struct tog_view *);
271 static const struct got_error *open_log_view(struct tog_view *,
272 struct got_object_id *, struct got_repository *, const char *, int);
273 static const struct got_error * show_log_view(struct tog_view *);
274 static const struct got_error *input_log_view(struct tog_view **,
275 struct tog_view **, struct tog_view **, struct tog_view *, int);
276 static const struct got_error *close_log_view(struct tog_view *);
278 static const struct got_error *open_blame_view(struct tog_view *, char *,
279 struct got_object_id *, struct got_repository *);
280 static const struct got_error *show_blame_view(struct tog_view *);
281 static const struct got_error *input_blame_view(struct tog_view **,
282 struct tog_view **, struct tog_view **, struct tog_view *, int);
283 static const struct got_error *close_blame_view(struct tog_view *);
285 static const struct got_error *open_tree_view(struct tog_view *,
286 struct got_tree_object *, struct got_object_id *, struct got_repository *);
287 static const struct got_error *show_tree_view(struct tog_view *);
288 static const struct got_error *input_tree_view(struct tog_view **,
289 struct tog_view **, struct tog_view **, struct tog_view *, int);
290 static const struct got_error *close_tree_view(struct tog_view *);
292 static volatile sig_atomic_t tog_sigwinch_received;
294 static void
295 tog_sigwinch(int signo)
297 tog_sigwinch_received = 1;
300 static const struct got_error *
301 view_close(struct tog_view *view)
303 const struct got_error *err = NULL;
305 if (view->child) {
306 view_close(view->child);
307 view->child = NULL;
309 if (view->close)
310 err = view->close(view);
311 if (view->panel)
312 del_panel(view->panel);
313 if (view->window)
314 delwin(view->window);
315 free(view);
316 return err;
319 static struct tog_view *
320 view_open(int nlines, int ncols, int begin_y, int begin_x,
321 enum tog_view_type type)
323 struct tog_view *view = calloc(1, sizeof(*view));
325 if (view == NULL)
326 return NULL;
328 view->type = type;
329 view->lines = LINES;
330 view->cols = COLS;
331 view->nlines = nlines ? nlines : LINES - begin_y;
332 view->ncols = ncols ? ncols : COLS - begin_x;
333 view->begin_y = begin_y;
334 view->begin_x = begin_x;
335 view->window = newwin(nlines, ncols, begin_y, begin_x);
336 if (view->window == NULL) {
337 view_close(view);
338 return NULL;
340 view->panel = new_panel(view->window);
341 if (view->panel == NULL ||
342 set_panel_userptr(view->panel, view) != OK) {
343 view_close(view);
344 return NULL;
347 keypad(view->window, TRUE);
348 return view;
351 static int
352 view_split_begin_x(int begin_x)
354 if (begin_x > 0 || COLS < 120)
355 return 0;
356 return (COLS - MAX(COLS / 2, 80));
359 static const struct got_error *view_resize(struct tog_view *);
361 static const struct got_error *
362 view_splitscreen(struct tog_view *view)
364 const struct got_error *err = NULL;
366 view->begin_y = 0;
367 view->begin_x = view_split_begin_x(0);
368 view->nlines = LINES;
369 view->ncols = COLS - view->begin_x;
370 view->lines = LINES;
371 view->cols = COLS;
372 err = view_resize(view);
373 if (err)
374 return err;
376 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
377 return got_error_from_errno();
379 return NULL;
382 static const struct got_error *
383 view_fullscreen(struct tog_view *view)
385 const struct got_error *err = NULL;
387 view->begin_x = 0;
388 view->begin_y = 0;
389 view->nlines = LINES;
390 view->ncols = COLS;
391 view->lines = LINES;
392 view->cols = COLS;
393 err = view_resize(view);
394 if (err)
395 return err;
397 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
398 return got_error_from_errno();
400 return NULL;
403 static int
404 view_is_parent_view(struct tog_view *view)
406 return view->parent == NULL;
409 static const struct got_error *
410 view_resize(struct tog_view *view)
412 int nlines, ncols;
414 if (view->lines > LINES)
415 nlines = view->nlines - (view->lines - LINES);
416 else
417 nlines = view->nlines + (LINES - view->lines);
419 if (view->cols > COLS)
420 ncols = view->ncols - (view->cols - COLS);
421 else
422 ncols = view->ncols + (COLS - view->cols);
424 if (wresize(view->window, nlines, ncols) == ERR)
425 return got_error_from_errno();
426 if (replace_panel(view->panel, view->window) == ERR)
427 return got_error_from_errno();
428 wclear(view->window);
430 view->nlines = nlines;
431 view->ncols = ncols;
432 view->lines = LINES;
433 view->cols = COLS;
435 if (view->child) {
436 view->child->begin_x = view_split_begin_x(view->begin_x);
437 if (view->child->begin_x == 0) {
438 view_fullscreen(view->child);
439 if (view->child->focussed)
440 show_panel(view->child->panel);
441 else
442 show_panel(view->panel);
443 } else {
444 view_splitscreen(view->child);
445 show_panel(view->child->panel);
449 return NULL;
452 static const struct got_error *
453 view_close_child(struct tog_view *view)
455 const struct got_error *err = NULL;
457 if (view->child == NULL)
458 return NULL;
460 err = view_close(view->child);
461 view->child = NULL;
462 return err;
465 static const struct got_error *
466 view_set_child(struct tog_view *view, struct tog_view *child)
468 const struct got_error *err = NULL;
470 view->child = child;
471 child->parent = view;
472 return err;
475 static int
476 view_is_splitscreen(struct tog_view *view)
478 return !view_is_parent_view(view) && view->begin_x > 0;
481 static void
482 tog_resizeterm(void)
484 int cols, lines;
485 struct winsize size;
487 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
488 cols = 80; /* Default */
489 lines = 24;
490 } else {
491 cols = size.ws_col;
492 lines = size.ws_row;
494 resize_term(lines, cols);
497 static const struct got_error *
498 view_input(struct tog_view **new, struct tog_view **dead,
499 struct tog_view **focus, int *done, struct tog_view *view,
500 struct tog_view_list_head *views)
502 const struct got_error *err = NULL;
503 struct tog_view *v;
504 int ch, errcode;
506 *new = NULL;
507 *dead = NULL;
508 *focus = NULL;
510 nodelay(stdscr, FALSE);
511 /* Allow threads to make progress while we are waiting for input. */
512 errcode = pthread_mutex_unlock(&tog_mutex);
513 if (errcode)
514 return got_error_set_errno(errcode);
515 ch = wgetch(view->window);
516 errcode = pthread_mutex_lock(&tog_mutex);
517 if (errcode)
518 return got_error_set_errno(errcode);
519 nodelay(stdscr, TRUE);
521 if (tog_sigwinch_received) {
522 tog_resizeterm();
523 tog_sigwinch_received = 0;
524 TAILQ_FOREACH(v, views, entry) {
525 err = view_resize(v);
526 if (err)
527 return err;
528 err = v->input(new, dead, focus, v, KEY_RESIZE);
529 if (err)
530 return err;
534 switch (ch) {
535 case ERR:
536 break;
537 case '\t':
538 if (view->child) {
539 *focus = view->child;
540 view->child_focussed = 1;
541 } else if (view->parent) {
542 *focus = view->parent;
543 view->parent->child_focussed = 0;
545 break;
546 case 'q':
547 err = view->input(new, dead, focus, view, ch);
548 *dead = view;
549 break;
550 case 'Q':
551 *done = 1;
552 break;
553 case 'f':
554 if (view_is_parent_view(view)) {
555 if (view->child == NULL)
556 break;
557 if (view_is_splitscreen(view->child)) {
558 *focus = view->child;
559 view->child_focussed = 1;
560 err = view_fullscreen(view->child);
561 } else
562 err = view_splitscreen(view->child);
563 if (err)
564 break;
565 err = view->child->input(new, dead, focus,
566 view->child, KEY_RESIZE);
567 } else {
568 if (view_is_splitscreen(view)) {
569 *focus = view;
570 view->parent->child_focussed = 1;
571 err = view_fullscreen(view);
572 } else {
573 err = view_splitscreen(view);
575 if (err)
576 break;
577 err = view->input(new, dead, focus, view,
578 KEY_RESIZE);
580 break;
581 case KEY_RESIZE:
582 break;
583 default:
584 err = view->input(new, dead, focus, view, ch);
585 break;
588 return err;
591 void
592 view_vborder(struct tog_view *view)
594 PANEL *panel;
595 struct tog_view *view_above;
597 if (view->parent)
598 return view_vborder(view->parent);
600 panel = panel_above(view->panel);
601 if (panel == NULL)
602 return;
604 view_above = panel_userptr(panel);
605 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
606 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
609 int
610 view_needs_focus_indication(struct tog_view *view)
612 if (view_is_parent_view(view)) {
613 if (view->child == NULL || view->child_focussed)
614 return 0;
615 if (!view_is_splitscreen(view->child))
616 return 0;
617 } else if (!view_is_splitscreen(view))
618 return 0;
620 return view->focussed;
623 static const struct got_error *
624 view_loop(struct tog_view *view)
626 const struct got_error *err = NULL;
627 struct tog_view_list_head views;
628 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
629 int fast_refresh = 10;
630 int done = 0, errcode;
632 errcode = pthread_mutex_lock(&tog_mutex);
633 if (errcode)
634 return got_error_set_errno(errcode);
636 TAILQ_INIT(&views);
637 TAILQ_INSERT_HEAD(&views, view, entry);
639 main_view = view;
640 view->focussed = 1;
641 err = view->show(view);
642 if (err)
643 return err;
644 update_panels();
645 doupdate();
646 while (!TAILQ_EMPTY(&views) && !done) {
647 /* Refresh fast during initialization, then become slower. */
648 if (fast_refresh && fast_refresh-- == 0)
649 halfdelay(10); /* switch to once per second */
651 err = view_input(&new_view, &dead_view, &focus_view, &done,
652 view, &views);
653 if (err)
654 break;
655 if (dead_view) {
656 struct tog_view *prev = NULL;
658 if (view_is_parent_view(dead_view))
659 prev = TAILQ_PREV(dead_view,
660 tog_view_list_head, entry);
661 else if (view->parent != dead_view)
662 prev = view->parent;
664 if (dead_view->parent)
665 dead_view->parent->child = NULL;
666 else
667 TAILQ_REMOVE(&views, dead_view, entry);
669 err = view_close(dead_view);
670 if (err || dead_view == main_view)
671 goto done;
673 if (view == dead_view) {
674 if (focus_view)
675 view = focus_view;
676 else if (prev)
677 view = prev;
678 else if (!TAILQ_EMPTY(&views))
679 view = TAILQ_LAST(&views,
680 tog_view_list_head);
681 else
682 view = NULL;
683 if (view) {
684 if (view->child && view->child_focussed)
685 focus_view = view->child;
686 else
687 focus_view = view;
691 if (new_view) {
692 struct tog_view *v, *t;
693 /* Only allow one parent view per type. */
694 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
695 if (v->type != new_view->type)
696 continue;
697 TAILQ_REMOVE(&views, v, entry);
698 err = view_close(v);
699 if (err)
700 goto done;
701 break;
703 TAILQ_INSERT_TAIL(&views, new_view, entry);
704 view = new_view;
705 if (focus_view == NULL)
706 focus_view = new_view;
708 if (focus_view) {
709 show_panel(focus_view->panel);
710 if (view)
711 view->focussed = 0;
712 focus_view->focussed = 1;
713 view = focus_view;
714 if (new_view)
715 show_panel(new_view->panel);
716 if (view->child && view_is_splitscreen(view->child))
717 show_panel(view->child->panel);
719 if (view) {
720 if (focus_view == NULL) {
721 view->focussed = 1;
722 show_panel(view->panel);
723 if (view->child && view_is_splitscreen(view->child))
724 show_panel(view->child->panel);
725 focus_view = view;
727 if (view->parent) {
728 err = view->parent->show(view->parent);
729 if (err)
730 goto done;
732 err = view->show(view);
733 if (err)
734 goto done;
735 if (view->child) {
736 err = view->child->show(view->child);
737 if (err)
738 goto done;
740 update_panels();
741 doupdate();
744 done:
745 while (!TAILQ_EMPTY(&views)) {
746 view = TAILQ_FIRST(&views);
747 TAILQ_REMOVE(&views, view, entry);
748 view_close(view);
751 errcode = pthread_mutex_unlock(&tog_mutex);
752 if (errcode)
753 return got_error_set_errno(errcode);
755 return err;
758 __dead static void
759 usage_log(void)
761 endwin();
762 fprintf(stderr,
763 "usage: %s log [-c commit] [-r repository-path] [path]\n",
764 getprogname());
765 exit(1);
768 /* Create newly allocated wide-character string equivalent to a byte string. */
769 static const struct got_error *
770 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
772 char *vis = NULL;
773 const struct got_error *err = NULL;
775 *ws = NULL;
776 *wlen = mbstowcs(NULL, s, 0);
777 if (*wlen == (size_t)-1) {
778 int vislen;
779 if (errno != EILSEQ)
780 return got_error_from_errno();
782 /* byte string invalid in current encoding; try to "fix" it */
783 err = got_mbsavis(&vis, &vislen, s);
784 if (err)
785 return err;
786 *wlen = mbstowcs(NULL, vis, 0);
787 if (*wlen == (size_t)-1) {
788 err = got_error_from_errno(); /* give up */
789 goto done;
793 *ws = calloc(*wlen + 1, sizeof(*ws));
794 if (*ws == NULL) {
795 err = got_error_from_errno();
796 goto done;
799 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
800 err = got_error_from_errno();
801 done:
802 free(vis);
803 if (err) {
804 free(*ws);
805 *ws = NULL;
806 *wlen = 0;
808 return err;
811 /* Format a line for display, ensuring that it won't overflow a width limit. */
812 static const struct got_error *
813 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
815 const struct got_error *err = NULL;
816 int cols = 0;
817 wchar_t *wline = NULL;
818 size_t wlen;
819 int i;
821 *wlinep = NULL;
822 *widthp = 0;
824 err = mbs2ws(&wline, &wlen, line);
825 if (err)
826 return err;
828 i = 0;
829 while (i < wlen && cols < wlimit) {
830 int width = wcwidth(wline[i]);
831 switch (width) {
832 case 0:
833 i++;
834 break;
835 case 1:
836 case 2:
837 if (cols + width <= wlimit)
838 cols += width;
839 i++;
840 break;
841 case -1:
842 if (wline[i] == L'\t')
843 cols += TABSIZE - ((cols + 1) % TABSIZE);
844 i++;
845 break;
846 default:
847 err = got_error_from_errno();
848 goto done;
851 wline[i] = L'\0';
852 if (widthp)
853 *widthp = cols;
854 done:
855 if (err)
856 free(wline);
857 else
858 *wlinep = wline;
859 return err;
862 static const struct got_error *
863 draw_commit(struct tog_view *view, struct got_commit_object *commit,
864 struct got_object_id *id)
866 const struct got_error *err = NULL;
867 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
868 char *logmsg0 = NULL, *logmsg = NULL;
869 char *author0 = NULL, *author = NULL;
870 wchar_t *wlogmsg = NULL, *wauthor = NULL;
871 int author_width, logmsg_width;
872 char *newline, *smallerthan;
873 char *line = NULL;
874 int col, limit;
875 static const size_t date_display_cols = 9;
876 static const size_t author_display_cols = 16;
877 const int avail = view->ncols;
878 struct tm tm;
879 time_t committer_time;
881 committer_time = got_object_commit_get_committer_time(commit);
882 if (localtime_r(&committer_time, &tm) == NULL)
883 return got_error_from_errno();
884 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
885 >= sizeof(datebuf))
886 return got_error(GOT_ERR_NO_SPACE);
888 if (avail < date_display_cols)
889 limit = MIN(sizeof(datebuf) - 1, avail);
890 else
891 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
892 waddnstr(view->window, datebuf, limit);
893 col = limit + 1;
894 if (col > avail)
895 goto done;
897 author0 = strdup(got_object_commit_get_author(commit));
898 if (author0 == NULL) {
899 err = got_error_from_errno();
900 goto done;
902 author = author0;
903 smallerthan = strchr(author, '<');
904 if (smallerthan)
905 *smallerthan = '\0';
906 else {
907 char *at = strchr(author, '@');
908 if (at)
909 *at = '\0';
911 limit = avail - col;
912 err = format_line(&wauthor, &author_width, author, limit);
913 if (err)
914 goto done;
915 waddwstr(view->window, wauthor);
916 col += author_width;
917 while (col <= avail && author_width < author_display_cols + 1) {
918 waddch(view->window, ' ');
919 col++;
920 author_width++;
922 if (col > avail)
923 goto done;
925 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
926 if (logmsg0 == NULL) {
927 err = got_error_from_errno();
928 goto done;
930 logmsg = logmsg0;
931 while (*logmsg == '\n')
932 logmsg++;
933 newline = strchr(logmsg, '\n');
934 if (newline)
935 *newline = '\0';
936 limit = avail - col;
937 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
938 if (err)
939 goto done;
940 waddwstr(view->window, wlogmsg);
941 col += logmsg_width;
942 while (col <= avail) {
943 waddch(view->window, ' ');
944 col++;
946 done:
947 free(logmsg0);
948 free(wlogmsg);
949 free(author0);
950 free(wauthor);
951 free(line);
952 return err;
955 static struct commit_queue_entry *
956 alloc_commit_queue_entry(struct got_commit_object *commit,
957 struct got_object_id *id)
959 struct commit_queue_entry *entry;
961 entry = calloc(1, sizeof(*entry));
962 if (entry == NULL)
963 return NULL;
965 entry->id = id;
966 entry->commit = commit;
967 return entry;
970 static void
971 pop_commit(struct commit_queue *commits)
973 struct commit_queue_entry *entry;
975 entry = TAILQ_FIRST(&commits->head);
976 TAILQ_REMOVE(&commits->head, entry, entry);
977 got_object_commit_close(entry->commit);
978 commits->ncommits--;
979 /* Don't free entry->id! It is owned by the commit graph. */
980 free(entry);
983 static void
984 free_commits(struct commit_queue *commits)
986 while (!TAILQ_EMPTY(&commits->head))
987 pop_commit(commits);
990 static const struct got_error *
991 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
992 int minqueue, struct got_repository *repo, const char *path)
994 const struct got_error *err = NULL;
995 int nqueued = 0;
997 /*
998 * We keep all commits open throughout the lifetime of the log
999 * view in order to avoid having to re-fetch commits from disk
1000 * while updating the display.
1002 while (nqueued < minqueue) {
1003 struct got_object_id *id;
1004 struct got_commit_object *commit;
1005 struct commit_queue_entry *entry;
1006 int errcode;
1008 err = got_commit_graph_iter_next(&id, graph);
1009 if (err) {
1010 if (err->code != GOT_ERR_ITER_NEED_MORE)
1011 break;
1012 err = got_commit_graph_fetch_commits(graph,
1013 minqueue, repo);
1014 if (err)
1015 return err;
1016 continue;
1019 if (id == NULL)
1020 break;
1022 err = got_object_open_as_commit(&commit, repo, id);
1023 if (err)
1024 break;
1025 entry = alloc_commit_queue_entry(commit, id);
1026 if (entry == NULL) {
1027 err = got_error_from_errno();
1028 break;
1031 errcode = pthread_mutex_lock(&tog_mutex);
1032 if (errcode) {
1033 err = got_error_set_errno(errcode);
1034 break;
1037 entry->idx = commits->ncommits;
1038 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1039 nqueued++;
1040 commits->ncommits++;
1042 errcode = pthread_mutex_unlock(&tog_mutex);
1043 if (errcode && err == NULL)
1044 err = got_error_set_errno(errcode);
1047 return err;
1050 static const struct got_error *
1051 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1053 const struct got_error *err = NULL;
1054 struct got_reference *head_ref;
1056 *head_id = NULL;
1058 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1059 if (err)
1060 return err;
1062 err = got_ref_resolve(head_id, repo, head_ref);
1063 got_ref_close(head_ref);
1064 if (err) {
1065 *head_id = NULL;
1066 return err;
1069 return NULL;
1072 static const struct got_error *
1073 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1074 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1075 struct commit_queue *commits, int selected_idx, int limit,
1076 const char *path, int commits_needed)
1078 const struct got_error *err = NULL;
1079 struct commit_queue_entry *entry;
1080 int ncommits, width;
1081 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1082 wchar_t *wline;
1084 entry = first;
1085 ncommits = 0;
1086 while (entry) {
1087 if (ncommits == selected_idx) {
1088 *selected = entry;
1089 break;
1091 entry = TAILQ_NEXT(entry, entry);
1092 ncommits++;
1095 if (*selected) {
1096 err = got_object_id_str(&id_str, (*selected)->id);
1097 if (err)
1098 return err;
1101 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1102 entry ? entry->idx + 1 : 0, commits->ncommits,
1103 commits_needed == 0 ? "" : " loading...") == -1)
1104 return got_error_from_errno();
1106 if (path && strcmp(path, "/") != 0) {
1107 if (asprintf(&header, "commit %s %s%s",
1108 id_str ? id_str : "........................................",
1109 path, ncommits_str) == -1) {
1110 err = got_error_from_errno();
1111 header = NULL;
1112 goto done;
1114 } else if (asprintf(&header, "commit %s%s",
1115 id_str ? id_str : "........................................",
1116 ncommits_str) == -1) {
1117 err = got_error_from_errno();
1118 header = NULL;
1119 goto done;
1121 err = format_line(&wline, &width, header, view->ncols);
1122 if (err)
1123 goto done;
1125 werase(view->window);
1127 if (view_needs_focus_indication(view))
1128 wstandout(view->window);
1129 waddwstr(view->window, wline);
1130 while (width < view->ncols) {
1131 waddch(view->window, ' ');
1132 width++;
1134 if (view_needs_focus_indication(view))
1135 wstandend(view->window);
1136 free(wline);
1137 if (limit <= 1)
1138 goto done;
1140 entry = first;
1141 *last = first;
1142 ncommits = 0;
1143 while (entry) {
1144 if (ncommits >= limit - 1)
1145 break;
1146 if (view->focussed && ncommits == selected_idx)
1147 wstandout(view->window);
1148 err = draw_commit(view, entry->commit, entry->id);
1149 if (view->focussed && ncommits == selected_idx)
1150 wstandend(view->window);
1151 if (err)
1152 break;
1153 ncommits++;
1154 *last = entry;
1155 entry = TAILQ_NEXT(entry, entry);
1158 view_vborder(view);
1159 done:
1160 free(id_str);
1161 free(ncommits_str);
1162 free(header);
1163 return err;
1166 static void
1167 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1168 struct commit_queue *commits)
1170 struct commit_queue_entry *entry;
1171 int nscrolled = 0;
1173 entry = TAILQ_FIRST(&commits->head);
1174 if (*first_displayed_entry == entry)
1175 return;
1177 entry = *first_displayed_entry;
1178 while (entry && nscrolled < maxscroll) {
1179 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1180 if (entry) {
1181 *first_displayed_entry = entry;
1182 nscrolled++;
1187 static const struct got_error *
1188 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1189 struct commit_queue_entry **last_displayed_entry,
1190 struct commit_queue *commits, int *log_complete, int *commits_needed,
1191 pthread_cond_t *need_commits)
1193 const struct got_error *err = NULL;
1194 struct commit_queue_entry *pentry;
1195 int nscrolled = 0;
1197 if (*last_displayed_entry == NULL)
1198 return NULL;
1200 do {
1201 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1202 if (pentry == NULL) {
1203 int errcode;
1204 if (*log_complete)
1205 return NULL;
1206 *commits_needed = maxscroll + 20;
1207 errcode = pthread_cond_signal(need_commits);
1208 if (errcode)
1209 return got_error_set_errno(errcode);
1210 return NULL;
1212 *last_displayed_entry = pentry;
1214 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1215 if (pentry == NULL)
1216 break;
1217 *first_displayed_entry = pentry;
1218 } while (++nscrolled < maxscroll);
1220 return err;
1223 static const struct got_error *
1224 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1225 struct got_object_id *commit_id, struct got_commit_object *commit,
1226 struct got_repository *repo)
1228 const struct got_error *err;
1229 struct got_object_qid *parent_id;
1230 struct tog_view *diff_view;
1232 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1233 if (diff_view == NULL)
1234 return got_error_from_errno();
1236 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1237 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1238 commit_id, repo);
1239 if (err == NULL)
1240 *new_view = diff_view;
1241 return err;
1244 static const struct got_error *
1245 browse_commit(struct tog_view **new_view, int begin_x,
1246 struct commit_queue_entry *entry, struct got_repository *repo)
1248 const struct got_error *err = NULL;
1249 struct got_tree_object *tree;
1250 struct tog_view *tree_view;
1252 err = got_object_open_as_tree(&tree, repo,
1253 got_object_commit_get_tree_id(entry->commit));
1254 if (err)
1255 return err;
1257 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1258 if (tree_view == NULL)
1259 return got_error_from_errno();
1261 err = open_tree_view(tree_view, tree, entry->id, repo);
1262 if (err)
1263 got_object_tree_close(tree);
1264 else
1265 *new_view = tree_view;
1266 return err;
1269 static void *
1270 log_thread(void *arg)
1272 const struct got_error *err = NULL;
1273 int errcode = 0;
1274 struct tog_log_thread_args *a = arg;
1275 int done = 0;
1277 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1278 if (err)
1279 return (void *)err;
1281 while (!done && !err) {
1282 err = queue_commits(a->graph, a->commits, 1, a->repo,
1283 a->in_repo_path);
1284 if (err) {
1285 if (err->code != GOT_ERR_ITER_COMPLETED)
1286 return (void *)err;
1287 err = NULL;
1288 done = 1;
1289 } else if (a->commits_needed > 0)
1290 a->commits_needed--;
1292 errcode = pthread_mutex_lock(&tog_mutex);
1293 if (errcode)
1294 return (void *)got_error_set_errno(errcode);
1296 if (done)
1297 a->log_complete = 1;
1298 else if (*a->quit) {
1299 done = 1;
1300 a->log_complete = 1;
1301 } else if (*a->first_displayed_entry == NULL) {
1302 *a->first_displayed_entry =
1303 TAILQ_FIRST(&a->commits->head);
1304 *a->selected_entry = *a->first_displayed_entry;
1307 if (done)
1308 a->commits_needed = 0;
1309 else if (a->commits_needed == 0) {
1310 errcode = pthread_cond_wait(&a->need_commits,
1311 &tog_mutex);
1312 if (errcode)
1313 err = got_error_set_errno(errcode);
1316 errcode = pthread_mutex_unlock(&tog_mutex);
1317 if (errcode && err == NULL)
1318 err = got_error_set_errno(errcode);
1320 return (void *)err;
1323 static const struct got_error *
1324 stop_log_thread(struct tog_log_view_state *s)
1326 const struct got_error *err = NULL;
1327 int errcode;
1329 if (s->thread) {
1330 s->quit = 1;
1331 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1332 if (errcode)
1333 return got_error_set_errno(errcode);
1334 errcode = pthread_mutex_unlock(&tog_mutex);
1335 if (errcode)
1336 return got_error_set_errno(errcode);
1337 errcode = pthread_join(s->thread, (void **)&err);
1338 if (errcode)
1339 return got_error_set_errno(errcode);
1340 errcode = pthread_mutex_lock(&tog_mutex);
1341 if (errcode)
1342 return got_error_set_errno(errcode);
1343 s->thread = NULL;
1346 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1347 if (errcode && err == NULL)
1348 err = got_error_set_errno(errcode);
1350 if (s->thread_args.repo) {
1351 got_repo_close(s->thread_args.repo);
1352 s->thread_args.repo = NULL;
1355 if (s->thread_args.graph) {
1356 got_commit_graph_close(s->thread_args.graph);
1357 s->thread_args.graph = NULL;
1360 return err;
1363 static const struct got_error *
1364 close_log_view(struct tog_view *view)
1366 const struct got_error *err = NULL;
1367 struct tog_log_view_state *s = &view->state.log;
1369 err = stop_log_thread(s);
1370 free_commits(&s->commits);
1371 free(s->in_repo_path);
1372 s->in_repo_path = NULL;
1373 free(s->start_id);
1374 s->start_id = NULL;
1375 return err;
1378 static const struct got_error *
1379 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1380 struct got_repository *repo, const char *path, int check_disk)
1382 const struct got_error *err = NULL;
1383 struct tog_log_view_state *s = &view->state.log;
1384 struct got_repository *thread_repo = NULL;
1385 struct got_commit_graph *thread_graph = NULL;
1386 int errcode;
1388 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1389 if (err != NULL)
1390 goto done;
1392 /* The commit queue only contains commits being displayed. */
1393 TAILQ_INIT(&s->commits.head);
1394 s->commits.ncommits = 0;
1396 s->repo = repo;
1397 s->start_id = got_object_id_dup(start_id);
1398 if (s->start_id == NULL) {
1399 err = got_error_from_errno();
1400 goto done;
1403 view->show = show_log_view;
1404 view->input = input_log_view;
1405 view->close = close_log_view;
1407 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1408 if (err)
1409 goto done;
1410 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1411 0, thread_repo);
1412 if (err)
1413 goto done;
1415 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1416 if (errcode) {
1417 err = got_error_set_errno(errcode);
1418 goto done;
1421 s->thread_args.commits_needed = view->nlines;
1422 s->thread_args.graph = thread_graph;
1423 s->thread_args.commits = &s->commits;
1424 s->thread_args.in_repo_path = s->in_repo_path;
1425 s->thread_args.start_id = s->start_id;
1426 s->thread_args.repo = thread_repo;
1427 s->thread_args.log_complete = 0;
1428 s->thread_args.quit = &s->quit;
1429 s->thread_args.view = view;
1430 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1431 s->thread_args.selected_entry = &s->selected_entry;
1432 done:
1433 if (err)
1434 close_log_view(view);
1435 return err;
1438 static const struct got_error *
1439 show_log_view(struct tog_view *view)
1441 struct tog_log_view_state *s = &view->state.log;
1443 if (s->thread == NULL) {
1444 int errcode = pthread_create(&s->thread, NULL, log_thread,
1445 &s->thread_args);
1446 if (errcode)
1447 return got_error_set_errno(errcode);
1450 return draw_commits(view, &s->last_displayed_entry,
1451 &s->selected_entry, s->first_displayed_entry,
1452 &s->commits, s->selected, view->nlines,
1453 s->in_repo_path, s->thread_args.commits_needed);
1456 static const struct got_error *
1457 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1458 struct tog_view **focus_view, struct tog_view *view, int ch)
1460 const struct got_error *err = NULL;
1461 struct tog_log_view_state *s = &view->state.log;
1462 char *parent_path;
1463 struct tog_view *diff_view = NULL, *tree_view = NULL;
1464 int begin_x = 0;
1466 switch (ch) {
1467 case 'q':
1468 s->quit = 1;
1469 break;
1470 case 'k':
1471 case KEY_UP:
1472 if (s->first_displayed_entry == NULL)
1473 break;
1474 if (s->selected > 0)
1475 s->selected--;
1476 if (s->selected > 0)
1477 break;
1478 scroll_up(&s->first_displayed_entry, 1,
1479 &s->commits);
1480 break;
1481 case KEY_PPAGE:
1482 if (s->first_displayed_entry == NULL)
1483 break;
1484 if (TAILQ_FIRST(&s->commits.head) ==
1485 s->first_displayed_entry) {
1486 s->selected = 0;
1487 break;
1489 scroll_up(&s->first_displayed_entry,
1490 view->nlines, &s->commits);
1491 break;
1492 case 'j':
1493 case KEY_DOWN:
1494 if (s->first_displayed_entry == NULL)
1495 break;
1496 if (s->selected < MIN(view->nlines - 2,
1497 s->commits.ncommits - 1)) {
1498 s->selected++;
1499 break;
1501 err = scroll_down(&s->first_displayed_entry, 1,
1502 &s->last_displayed_entry, &s->commits,
1503 &s->thread_args.log_complete,
1504 &s->thread_args.commits_needed,
1505 &s->thread_args.need_commits);
1506 break;
1507 case KEY_NPAGE: {
1508 struct commit_queue_entry *first;
1509 first = s->first_displayed_entry;
1510 if (first == NULL)
1511 break;
1512 err = scroll_down(&s->first_displayed_entry,
1513 view->nlines, &s->last_displayed_entry,
1514 &s->commits, &s->thread_args.log_complete,
1515 &s->thread_args.commits_needed,
1516 &s->thread_args.need_commits);
1517 if (first == s->first_displayed_entry &&
1518 s->selected < MIN(view->nlines - 2,
1519 s->commits.ncommits - 1)) {
1520 /* can't scroll further down */
1521 s->selected = MIN(view->nlines - 2,
1522 s->commits.ncommits - 1);
1524 err = NULL;
1525 break;
1527 case KEY_RESIZE:
1528 if (s->selected > view->nlines - 2)
1529 s->selected = view->nlines - 2;
1530 if (s->selected > s->commits.ncommits - 1)
1531 s->selected = s->commits.ncommits - 1;
1532 break;
1533 case KEY_ENTER:
1534 case '\r':
1535 if (s->selected_entry == NULL)
1536 break;
1537 if (view_is_parent_view(view))
1538 begin_x = view_split_begin_x(view->begin_x);
1539 err = open_diff_view_for_commit(&diff_view, begin_x,
1540 s->selected_entry->id, s->selected_entry->commit,
1541 s->repo);
1542 if (err)
1543 break;
1544 if (view_is_parent_view(view)) {
1545 err = view_close_child(view);
1546 if (err)
1547 return err;
1548 err = view_set_child(view, diff_view);
1549 if (err) {
1550 view_close(diff_view);
1551 break;
1553 *focus_view = diff_view;
1554 view->child_focussed = 1;
1555 } else
1556 *new_view = diff_view;
1557 break;
1558 case 't':
1559 if (s->selected_entry == NULL)
1560 break;
1561 if (view_is_parent_view(view))
1562 begin_x = view_split_begin_x(view->begin_x);
1563 err = browse_commit(&tree_view, begin_x,
1564 s->selected_entry, s->repo);
1565 if (err)
1566 break;
1567 if (view_is_parent_view(view)) {
1568 err = view_close_child(view);
1569 if (err)
1570 return err;
1571 err = view_set_child(view, tree_view);
1572 if (err) {
1573 view_close(tree_view);
1574 break;
1576 *focus_view = tree_view;
1577 view->child_focussed = 1;
1578 } else
1579 *new_view = tree_view;
1580 break;
1581 case KEY_BACKSPACE:
1582 if (strcmp(s->in_repo_path, "/") == 0)
1583 break;
1584 parent_path = dirname(s->in_repo_path);
1585 if (parent_path && strcmp(parent_path, ".") != 0) {
1586 struct tog_view *lv;
1587 err = stop_log_thread(s);
1588 if (err)
1589 return err;
1590 lv = view_open(view->nlines, view->ncols,
1591 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1592 if (lv == NULL)
1593 return got_error_from_errno();
1594 err = open_log_view(lv, s->start_id, s->repo,
1595 parent_path, 0);
1596 if (err)
1597 return err;;
1598 if (view_is_parent_view(view))
1599 *new_view = lv;
1600 else {
1601 view_set_child(view->parent, lv);
1602 *focus_view = lv;
1604 return NULL;
1606 break;
1607 default:
1608 break;
1611 return err;
1614 static const struct got_error *
1615 apply_unveil(const char *repo_path, const char *worktree_path)
1617 const struct got_error *error;
1619 if (repo_path && unveil(repo_path, "r") != 0)
1620 return got_error_from_errno();
1622 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1623 return got_error_from_errno();
1625 if (unveil("/tmp", "rwc") != 0)
1626 return got_error_from_errno();
1628 error = got_privsep_unveil_exec_helpers();
1629 if (error != NULL)
1630 return error;
1632 if (unveil(NULL, NULL) != 0)
1633 return got_error_from_errno();
1635 return NULL;
1638 static const struct got_error *
1639 cmd_log(int argc, char *argv[])
1641 const struct got_error *error;
1642 struct got_repository *repo = NULL;
1643 struct got_object_id *start_id = NULL;
1644 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1645 char *start_commit = NULL;
1646 int ch;
1647 struct tog_view *view;
1649 #ifndef PROFILE
1650 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1651 NULL) == -1)
1652 err(1, "pledge");
1653 #endif
1655 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1656 switch (ch) {
1657 case 'c':
1658 start_commit = optarg;
1659 break;
1660 case 'r':
1661 repo_path = realpath(optarg, NULL);
1662 if (repo_path == NULL)
1663 err(1, "-r option");
1664 break;
1665 default:
1666 usage();
1667 /* NOTREACHED */
1671 argc -= optind;
1672 argv += optind;
1674 if (argc == 0)
1675 path = strdup("");
1676 else if (argc == 1)
1677 path = strdup(argv[0]);
1678 else
1679 usage_log();
1680 if (path == NULL)
1681 return got_error_from_errno();
1683 cwd = getcwd(NULL, 0);
1684 if (cwd == NULL) {
1685 error = got_error_from_errno();
1686 goto done;
1688 if (repo_path == NULL) {
1689 repo_path = strdup(cwd);
1690 if (repo_path == NULL) {
1691 error = got_error_from_errno();
1692 goto done;
1696 error = apply_unveil(repo_path, NULL);
1697 if (error)
1698 goto done;
1700 error = got_repo_open(&repo, repo_path);
1701 if (error != NULL)
1702 goto done;
1704 if (start_commit == NULL)
1705 error = get_head_commit_id(&start_id, repo);
1706 else
1707 error = got_object_resolve_id_str(&start_id, repo,
1708 start_commit);
1709 if (error != NULL)
1710 goto done;
1712 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1713 if (view == NULL) {
1714 error = got_error_from_errno();
1715 goto done;
1717 error = open_log_view(view, start_id, repo, path, 1);
1718 if (error)
1719 goto done;
1720 error = view_loop(view);
1721 done:
1722 free(repo_path);
1723 free(cwd);
1724 free(path);
1725 free(start_id);
1726 if (repo)
1727 got_repo_close(repo);
1728 return error;
1731 __dead static void
1732 usage_diff(void)
1734 endwin();
1735 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1736 getprogname());
1737 exit(1);
1740 static char *
1741 parse_next_line(FILE *f, size_t *len)
1743 char *line;
1744 size_t linelen;
1745 size_t lineno;
1746 const char delim[3] = { '\0', '\0', '\0'};
1748 line = fparseln(f, &linelen, &lineno, delim, 0);
1749 if (len)
1750 *len = linelen;
1751 return line;
1754 static const struct got_error *
1755 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1756 int *last_displayed_line, int *eof, int max_lines,
1757 char * header)
1759 const struct got_error *err;
1760 int nlines = 0, nprinted = 0;
1761 char *line;
1762 size_t len;
1763 wchar_t *wline;
1764 int width;
1766 rewind(f);
1767 werase(view->window);
1769 if (header) {
1770 err = format_line(&wline, &width, header, view->ncols);
1771 if (err) {
1772 return err;
1775 if (view_needs_focus_indication(view))
1776 wstandout(view->window);
1777 waddwstr(view->window, wline);
1778 if (view_needs_focus_indication(view))
1779 wstandend(view->window);
1780 if (width < view->ncols)
1781 waddch(view->window, '\n');
1783 if (max_lines <= 1)
1784 return NULL;
1785 max_lines--;
1788 *eof = 0;
1789 while (nprinted < max_lines) {
1790 line = parse_next_line(f, &len);
1791 if (line == NULL) {
1792 *eof = 1;
1793 break;
1795 if (++nlines < *first_displayed_line) {
1796 free(line);
1797 continue;
1800 err = format_line(&wline, &width, line, view->ncols);
1801 if (err) {
1802 free(line);
1803 return err;
1805 waddwstr(view->window, wline);
1806 if (width < view->ncols)
1807 waddch(view->window, '\n');
1808 if (++nprinted == 1)
1809 *first_displayed_line = nlines;
1810 free(line);
1811 free(wline);
1812 wline = NULL;
1814 *last_displayed_line = nlines;
1816 view_vborder(view);
1818 return NULL;
1821 static char *
1822 get_datestr(time_t *time, char *datebuf)
1824 char *p, *s = ctime_r(time, datebuf);
1825 p = strchr(s, '\n');
1826 if (p)
1827 *p = '\0';
1828 return s;
1831 static const struct got_error *
1832 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1833 FILE *outfile)
1835 const struct got_error *err = NULL;
1836 char datebuf[26];
1837 struct got_commit_object *commit;
1838 char *id_str = NULL;
1839 time_t committer_time;
1840 const char *author, *committer;
1842 err = got_object_open_as_commit(&commit, repo, commit_id);
1843 if (err)
1844 return err;
1846 err = got_object_id_str(&id_str, commit_id);
1847 if (err) {
1848 err = got_error_from_errno();
1849 goto done;
1852 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1853 err = got_error_from_errno();
1854 goto done;
1856 if (fprintf(outfile, "from: %s\n",
1857 got_object_commit_get_author(commit)) < 0) {
1858 err = got_error_from_errno();
1859 goto done;
1861 committer_time = got_object_commit_get_committer_time(commit);
1862 if (fprintf(outfile, "date: %s UTC\n",
1863 get_datestr(&committer_time, datebuf)) < 0) {
1864 err = got_error_from_errno();
1865 goto done;
1867 author = got_object_commit_get_author(commit);
1868 committer = got_object_commit_get_committer(commit);
1869 if (strcmp(author, committer) != 0 &&
1870 fprintf(outfile, "via: %s\n", committer) < 0) {
1871 err = got_error_from_errno();
1872 goto done;
1874 if (fprintf(outfile, "%s\n",
1875 got_object_commit_get_logmsg(commit)) < 0) {
1876 err = got_error_from_errno();
1877 goto done;
1879 done:
1880 free(id_str);
1881 got_object_commit_close(commit);
1882 return err;
1885 static const struct got_error *
1886 create_diff(struct tog_diff_view_state *s)
1888 const struct got_error *err = NULL;
1889 FILE *f = NULL;
1890 int obj_type;
1892 f = got_opentemp();
1893 if (f == NULL) {
1894 err = got_error_from_errno();
1895 goto done;
1897 if (s->f)
1898 fclose(s->f);
1899 s->f = f;
1901 if (s->id1)
1902 err = got_object_get_type(&obj_type, s->repo, s->id1);
1903 else
1904 err = got_object_get_type(&obj_type, s->repo, s->id2);
1905 if (err)
1906 goto done;
1908 switch (obj_type) {
1909 case GOT_OBJ_TYPE_BLOB:
1910 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1911 s->diff_context, s->repo, f);
1912 break;
1913 case GOT_OBJ_TYPE_TREE:
1914 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1915 s->diff_context, s->repo, f);
1916 break;
1917 case GOT_OBJ_TYPE_COMMIT: {
1918 const struct got_object_id_queue *parent_ids;
1919 struct got_object_qid *pid;
1920 struct got_commit_object *commit2;
1922 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1923 if (err)
1924 break;
1925 /* Show commit info if we're diffing to a parent commit. */
1926 parent_ids = got_object_commit_get_parent_ids(commit2);
1927 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1928 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1929 write_commit_info(s->id2, s->repo, f);
1930 break;
1933 got_object_commit_close(commit2);
1935 err = got_diff_objects_as_commits(s->id1, s->id2,
1936 s->diff_context, s->repo, f);
1937 break;
1939 default:
1940 err = got_error(GOT_ERR_OBJ_TYPE);
1941 break;
1943 done:
1944 if (f)
1945 fflush(f);
1946 return err;
1949 static const struct got_error *
1950 open_diff_view(struct tog_view *view, struct got_object_id *id1,
1951 struct got_object_id *id2, struct got_repository *repo)
1953 const struct got_error *err;
1955 if (id1 != NULL && id2 != NULL) {
1956 int type1, type2;
1957 err = got_object_get_type(&type1, repo, id1);
1958 if (err)
1959 return err;
1960 err = got_object_get_type(&type2, repo, id2);
1961 if (err)
1962 return err;
1964 if (type1 != type2)
1965 return got_error(GOT_ERR_OBJ_TYPE);
1968 if (id1) {
1969 view->state.diff.id1 = got_object_id_dup(id1);
1970 if (view->state.diff.id1 == NULL)
1971 return got_error_from_errno();
1972 } else
1973 view->state.diff.id1 = NULL;
1975 view->state.diff.id2 = got_object_id_dup(id2);
1976 if (view->state.diff.id2 == NULL) {
1977 free(view->state.diff.id1);
1978 view->state.diff.id1 = NULL;
1979 return got_error_from_errno();
1981 view->state.diff.f = NULL;
1982 view->state.diff.first_displayed_line = 1;
1983 view->state.diff.last_displayed_line = view->nlines;
1984 view->state.diff.diff_context = 3;
1985 view->state.diff.repo = repo;
1987 err = create_diff(&view->state.diff);
1988 if (err) {
1989 free(view->state.diff.id1);
1990 view->state.diff.id1 = NULL;
1991 free(view->state.diff.id2);
1992 view->state.diff.id2 = NULL;
1993 return err;
1996 view->show = show_diff_view;
1997 view->input = input_diff_view;
1998 view->close = close_diff_view;
2000 return NULL;
2003 static const struct got_error *
2004 close_diff_view(struct tog_view *view)
2006 const struct got_error *err = NULL;
2008 free(view->state.diff.id1);
2009 view->state.diff.id1 = NULL;
2010 free(view->state.diff.id2);
2011 view->state.diff.id2 = NULL;
2012 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2013 err = got_error_from_errno();
2014 return err;
2017 static const struct got_error *
2018 show_diff_view(struct tog_view *view)
2020 const struct got_error *err;
2021 struct tog_diff_view_state *s = &view->state.diff;
2022 char *id_str1 = NULL, *id_str2, *header;
2024 if (s->id1) {
2025 err = got_object_id_str(&id_str1, s->id1);
2026 if (err)
2027 return err;
2029 err = got_object_id_str(&id_str2, s->id2);
2030 if (err)
2031 return err;
2033 if (asprintf(&header, "diff %s %s",
2034 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2035 err = got_error_from_errno();
2036 free(id_str1);
2037 free(id_str2);
2038 return err;
2040 free(id_str1);
2041 free(id_str2);
2043 return draw_file(view, s->f, &s->first_displayed_line,
2044 &s->last_displayed_line, &s->eof, view->nlines,
2045 header);
2048 static const struct got_error *
2049 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2050 struct tog_view **focus_view, struct tog_view *view, int ch)
2052 const struct got_error *err = NULL;
2053 struct tog_diff_view_state *s = &view->state.diff;
2054 int i;
2056 switch (ch) {
2057 case 'k':
2058 case KEY_UP:
2059 if (s->first_displayed_line > 1)
2060 s->first_displayed_line--;
2061 break;
2062 case KEY_PPAGE:
2063 i = 0;
2064 while (i++ < view->nlines - 1 &&
2065 s->first_displayed_line > 1)
2066 s->first_displayed_line--;
2067 break;
2068 case 'j':
2069 case KEY_DOWN:
2070 if (!s->eof)
2071 s->first_displayed_line++;
2072 break;
2073 case KEY_NPAGE:
2074 case ' ':
2075 i = 0;
2076 while (!s->eof && i++ < view->nlines - 1) {
2077 char *line;
2078 line = parse_next_line(s->f, NULL);
2079 s->first_displayed_line++;
2080 if (line == NULL)
2081 break;
2083 break;
2084 case '[':
2085 if (s->diff_context > 0) {
2086 s->diff_context--;
2087 err = create_diff(s);
2089 break;
2090 case ']':
2091 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2092 s->diff_context++;
2093 err = create_diff(s);
2095 break;
2096 default:
2097 break;
2100 return err;
2103 static const struct got_error *
2104 cmd_diff(int argc, char *argv[])
2106 const struct got_error *error = NULL;
2107 struct got_repository *repo = NULL;
2108 struct got_object_id *id1 = NULL, *id2 = NULL;
2109 char *repo_path = NULL;
2110 char *id_str1 = NULL, *id_str2 = NULL;
2111 int ch;
2112 struct tog_view *view;
2114 #ifndef PROFILE
2115 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2116 NULL) == -1)
2117 err(1, "pledge");
2118 #endif
2120 while ((ch = getopt(argc, argv, "")) != -1) {
2121 switch (ch) {
2122 default:
2123 usage();
2124 /* NOTREACHED */
2128 argc -= optind;
2129 argv += optind;
2131 if (argc == 0) {
2132 usage_diff(); /* TODO show local worktree changes */
2133 } else if (argc == 2) {
2134 repo_path = getcwd(NULL, 0);
2135 if (repo_path == NULL)
2136 return got_error_from_errno();
2137 id_str1 = argv[0];
2138 id_str2 = argv[1];
2139 } else if (argc == 3) {
2140 repo_path = realpath(argv[0], NULL);
2141 if (repo_path == NULL)
2142 return got_error_from_errno();
2143 id_str1 = argv[1];
2144 id_str2 = argv[2];
2145 } else
2146 usage_diff();
2148 error = apply_unveil(repo_path, NULL);
2149 if (error)
2150 goto done;
2152 error = got_repo_open(&repo, repo_path);
2153 free(repo_path);
2154 if (error)
2155 goto done;
2157 error = got_object_resolve_id_str(&id1, repo, id_str1);
2158 if (error)
2159 goto done;
2161 error = got_object_resolve_id_str(&id2, repo, id_str2);
2162 if (error)
2163 goto done;
2165 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2166 if (view == NULL) {
2167 error = got_error_from_errno();
2168 goto done;
2170 error = open_diff_view(view, id1, id2, repo);
2171 if (error)
2172 goto done;
2173 error = view_loop(view);
2174 done:
2175 got_repo_close(repo);
2176 return error;
2179 __dead static void
2180 usage_blame(void)
2182 endwin();
2183 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2184 getprogname());
2185 exit(1);
2188 struct tog_blame_line {
2189 int annotated;
2190 struct got_object_id *id;
2193 static const struct got_error *
2194 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2195 const char *path, struct tog_blame_line *lines, int nlines,
2196 int blame_complete, int selected_line, int *first_displayed_line,
2197 int *last_displayed_line, int *eof, int max_lines)
2199 const struct got_error *err;
2200 int lineno = 0, nprinted = 0;
2201 char *line;
2202 size_t len;
2203 wchar_t *wline;
2204 int width, wlimit;
2205 struct tog_blame_line *blame_line;
2206 struct got_object_id *prev_id = NULL;
2207 char *id_str;
2209 err = got_object_id_str(&id_str, id);
2210 if (err)
2211 return err;
2213 rewind(f);
2214 werase(view->window);
2216 if (asprintf(&line, "commit %s", id_str) == -1) {
2217 err = got_error_from_errno();
2218 free(id_str);
2219 return err;
2222 err = format_line(&wline, &width, line, view->ncols);
2223 free(line);
2224 line = NULL;
2225 if (view_needs_focus_indication(view))
2226 wstandout(view->window);
2227 waddwstr(view->window, wline);
2228 if (view_needs_focus_indication(view))
2229 wstandend(view->window);
2230 free(wline);
2231 wline = NULL;
2232 if (width < view->ncols)
2233 waddch(view->window, '\n');
2235 if (asprintf(&line, "[%d/%d] %s%s",
2236 *first_displayed_line - 1 + selected_line, nlines,
2237 blame_complete ? "" : "annotating ", path) == -1) {
2238 free(id_str);
2239 return got_error_from_errno();
2241 free(id_str);
2242 err = format_line(&wline, &width, line, view->ncols);
2243 free(line);
2244 line = NULL;
2245 if (err)
2246 return err;
2247 waddwstr(view->window, wline);
2248 free(wline);
2249 wline = NULL;
2250 if (width < view->ncols)
2251 waddch(view->window, '\n');
2253 *eof = 0;
2254 while (nprinted < max_lines - 2) {
2255 line = parse_next_line(f, &len);
2256 if (line == NULL) {
2257 *eof = 1;
2258 break;
2260 if (++lineno < *first_displayed_line) {
2261 free(line);
2262 continue;
2265 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2266 err = format_line(&wline, &width, line, wlimit);
2267 if (err) {
2268 free(line);
2269 return err;
2272 if (view->focussed && nprinted == selected_line - 1)
2273 wstandout(view->window);
2275 blame_line = &lines[lineno - 1];
2276 if (blame_line->annotated && prev_id &&
2277 got_object_id_cmp(prev_id, blame_line->id) == 0)
2278 waddstr(view->window, " ");
2279 else if (blame_line->annotated) {
2280 char *id_str;
2281 err = got_object_id_str(&id_str, blame_line->id);
2282 if (err) {
2283 free(line);
2284 free(wline);
2285 return err;
2287 wprintw(view->window, "%.8s ", id_str);
2288 free(id_str);
2289 prev_id = blame_line->id;
2290 } else {
2291 waddstr(view->window, "........ ");
2292 prev_id = NULL;
2295 waddwstr(view->window, wline);
2296 while (width < wlimit) {
2297 waddch(view->window, ' ');
2298 width++;
2300 if (view->focussed && nprinted == selected_line - 1)
2301 wstandend(view->window);
2302 if (++nprinted == 1)
2303 *first_displayed_line = lineno;
2304 free(line);
2305 free(wline);
2306 wline = NULL;
2308 *last_displayed_line = lineno;
2310 view_vborder(view);
2312 return NULL;
2315 static const struct got_error *
2316 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2318 const struct got_error *err = NULL;
2319 struct tog_blame_cb_args *a = arg;
2320 struct tog_blame_line *line;
2321 int errcode;
2323 if (nlines != a->nlines ||
2324 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2325 return got_error(GOT_ERR_RANGE);
2327 errcode = pthread_mutex_lock(&tog_mutex);
2328 if (errcode)
2329 return got_error_set_errno(errcode);
2331 if (*a->quit) { /* user has quit the blame view */
2332 err = got_error(GOT_ERR_ITER_COMPLETED);
2333 goto done;
2336 if (lineno == -1)
2337 goto done; /* no change in this commit */
2339 line = &a->lines[lineno - 1];
2340 if (line->annotated)
2341 goto done;
2343 line->id = got_object_id_dup(id);
2344 if (line->id == NULL) {
2345 err = got_error_from_errno();
2346 goto done;
2348 line->annotated = 1;
2349 done:
2350 errcode = pthread_mutex_unlock(&tog_mutex);
2351 if (errcode)
2352 err = got_error_set_errno(errcode);
2353 return err;
2356 static void *
2357 blame_thread(void *arg)
2359 const struct got_error *err;
2360 struct tog_blame_thread_args *ta = arg;
2361 struct tog_blame_cb_args *a = ta->cb_args;
2362 int errcode;
2364 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2365 blame_cb, ta->cb_args);
2367 errcode = pthread_mutex_lock(&tog_mutex);
2368 if (errcode)
2369 return (void *)got_error_set_errno(errcode);
2371 got_repo_close(ta->repo);
2372 ta->repo = NULL;
2373 *ta->complete = 1;
2375 errcode = pthread_mutex_unlock(&tog_mutex);
2376 if (errcode && err == NULL)
2377 err = got_error_set_errno(errcode);
2379 return (void *)err;
2382 static struct got_object_id *
2383 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2384 int selected_line)
2386 struct tog_blame_line *line;
2388 line = &lines[first_displayed_line - 1 + selected_line - 1];
2389 if (!line->annotated)
2390 return NULL;
2392 return line->id;
2395 static const struct got_error *
2396 stop_blame(struct tog_blame *blame)
2398 const struct got_error *err = NULL;
2399 int i;
2401 if (blame->thread) {
2402 int errcode;
2403 errcode = pthread_mutex_unlock(&tog_mutex);
2404 if (errcode)
2405 return got_error_set_errno(errcode);
2406 errcode = pthread_join(blame->thread, (void **)&err);
2407 if (errcode)
2408 return got_error_set_errno(errcode);
2409 errcode = pthread_mutex_lock(&tog_mutex);
2410 if (errcode)
2411 return got_error_set_errno(errcode);
2412 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2413 err = NULL;
2414 blame->thread = NULL;
2416 if (blame->thread_args.repo) {
2417 got_repo_close(blame->thread_args.repo);
2418 blame->thread_args.repo = NULL;
2420 if (blame->f) {
2421 fclose(blame->f);
2422 blame->f = NULL;
2424 if (blame->lines) {
2425 for (i = 0; i < blame->nlines; i++)
2426 free(blame->lines[i].id);
2427 free(blame->lines);
2428 blame->lines = NULL;
2430 free(blame->cb_args.commit_id);
2431 blame->cb_args.commit_id = NULL;
2433 return err;
2436 static const struct got_error *
2437 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2438 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2439 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2440 struct got_repository *repo)
2442 const struct got_error *err = NULL;
2443 struct got_blob_object *blob = NULL;
2444 struct got_repository *thread_repo = NULL;
2445 struct got_object_id *obj_id = NULL;
2446 int obj_type;
2448 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2449 if (err)
2450 return err;
2451 if (obj_id == NULL)
2452 return got_error(GOT_ERR_NO_OBJ);
2454 err = got_object_get_type(&obj_type, repo, obj_id);
2455 if (err)
2456 goto done;
2458 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2459 err = got_error(GOT_ERR_OBJ_TYPE);
2460 goto done;
2463 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2464 if (err)
2465 goto done;
2466 blame->f = got_opentemp();
2467 if (blame->f == NULL) {
2468 err = got_error_from_errno();
2469 goto done;
2471 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2472 blame->f, blob);
2473 if (err)
2474 goto done;
2476 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2477 if (blame->lines == NULL) {
2478 err = got_error_from_errno();
2479 goto done;
2482 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2483 if (err)
2484 goto done;
2486 blame->cb_args.view = view;
2487 blame->cb_args.lines = blame->lines;
2488 blame->cb_args.nlines = blame->nlines;
2489 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2490 if (blame->cb_args.commit_id == NULL) {
2491 err = got_error_from_errno();
2492 goto done;
2494 blame->cb_args.quit = done;
2496 blame->thread_args.path = path;
2497 blame->thread_args.repo = thread_repo;
2498 blame->thread_args.cb_args = &blame->cb_args;
2499 blame->thread_args.complete = blame_complete;
2500 *blame_complete = 0;
2502 done:
2503 if (blob)
2504 got_object_blob_close(blob);
2505 free(obj_id);
2506 if (err)
2507 stop_blame(blame);
2508 return err;
2511 static const struct got_error *
2512 open_blame_view(struct tog_view *view, char *path,
2513 struct got_object_id *commit_id, struct got_repository *repo)
2515 const struct got_error *err = NULL;
2516 struct tog_blame_view_state *s = &view->state.blame;
2518 SIMPLEQ_INIT(&s->blamed_commits);
2520 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2521 if (err)
2522 return err;
2524 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2525 s->first_displayed_line = 1;
2526 s->last_displayed_line = view->nlines;
2527 s->selected_line = 1;
2528 s->blame_complete = 0;
2529 s->path = path;
2530 if (s->path == NULL)
2531 return got_error_from_errno();
2532 s->repo = repo;
2533 s->commit_id = commit_id;
2534 memset(&s->blame, 0, sizeof(s->blame));
2536 view->show = show_blame_view;
2537 view->input = input_blame_view;
2538 view->close = close_blame_view;
2540 return run_blame(&s->blame, view, &s->blame_complete,
2541 &s->first_displayed_line, &s->last_displayed_line,
2542 &s->selected_line, &s->done, &s->eof, s->path,
2543 s->blamed_commit->id, s->repo);
2546 static const struct got_error *
2547 close_blame_view(struct tog_view *view)
2549 const struct got_error *err = NULL;
2550 struct tog_blame_view_state *s = &view->state.blame;
2552 if (s->blame.thread)
2553 err = stop_blame(&s->blame);
2555 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2556 struct got_object_qid *blamed_commit;
2557 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2558 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2559 got_object_qid_free(blamed_commit);
2562 free(s->path);
2564 return err;
2567 static const struct got_error *
2568 show_blame_view(struct tog_view *view)
2570 const struct got_error *err = NULL;
2571 struct tog_blame_view_state *s = &view->state.blame;
2572 int errcode;
2574 if (s->blame.thread == NULL) {
2575 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2576 &s->blame.thread_args);
2577 if (errcode)
2578 return got_error_set_errno(errcode);
2581 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2582 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2583 s->selected_line, &s->first_displayed_line,
2584 &s->last_displayed_line, &s->eof, view->nlines);
2586 view_vborder(view);
2587 return err;
2590 static const struct got_error *
2591 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2592 struct tog_view **focus_view, struct tog_view *view, int ch)
2594 const struct got_error *err = NULL, *thread_err = NULL;
2595 struct tog_view *diff_view;
2596 struct tog_blame_view_state *s = &view->state.blame;
2597 int begin_x = 0;
2599 switch (ch) {
2600 case 'q':
2601 s->done = 1;
2602 break;
2603 case 'k':
2604 case KEY_UP:
2605 if (s->selected_line > 1)
2606 s->selected_line--;
2607 else if (s->selected_line == 1 &&
2608 s->first_displayed_line > 1)
2609 s->first_displayed_line--;
2610 break;
2611 case KEY_PPAGE:
2612 if (s->first_displayed_line == 1) {
2613 s->selected_line = 1;
2614 break;
2616 if (s->first_displayed_line > view->nlines - 2)
2617 s->first_displayed_line -=
2618 (view->nlines - 2);
2619 else
2620 s->first_displayed_line = 1;
2621 break;
2622 case 'j':
2623 case KEY_DOWN:
2624 if (s->selected_line < view->nlines - 2 &&
2625 s->first_displayed_line +
2626 s->selected_line <= s->blame.nlines)
2627 s->selected_line++;
2628 else if (s->last_displayed_line <
2629 s->blame.nlines)
2630 s->first_displayed_line++;
2631 break;
2632 case 'b':
2633 case 'p': {
2634 struct got_object_id *id = NULL;
2635 id = get_selected_commit_id(s->blame.lines,
2636 s->first_displayed_line, s->selected_line);
2637 if (id == NULL)
2638 break;
2639 if (ch == 'p') {
2640 struct got_commit_object *commit;
2641 struct got_object_qid *pid;
2642 struct got_object_id *blob_id = NULL;
2643 int obj_type;
2644 err = got_object_open_as_commit(&commit,
2645 s->repo, id);
2646 if (err)
2647 break;
2648 pid = SIMPLEQ_FIRST(
2649 got_object_commit_get_parent_ids(commit));
2650 if (pid == NULL) {
2651 got_object_commit_close(commit);
2652 break;
2654 /* Check if path history ends here. */
2655 err = got_object_id_by_path(&blob_id, s->repo,
2656 pid->id, s->path);
2657 if (err) {
2658 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2659 err = NULL;
2660 got_object_commit_close(commit);
2661 break;
2663 err = got_object_get_type(&obj_type, s->repo,
2664 blob_id);
2665 free(blob_id);
2666 /* Can't blame non-blob type objects. */
2667 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2668 got_object_commit_close(commit);
2669 break;
2671 err = got_object_qid_alloc(&s->blamed_commit,
2672 pid->id);
2673 got_object_commit_close(commit);
2674 } else {
2675 if (got_object_id_cmp(id,
2676 s->blamed_commit->id) == 0)
2677 break;
2678 err = got_object_qid_alloc(&s->blamed_commit,
2679 id);
2681 if (err)
2682 break;
2683 s->done = 1;
2684 thread_err = stop_blame(&s->blame);
2685 s->done = 0;
2686 if (thread_err)
2687 break;
2688 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2689 s->blamed_commit, entry);
2690 err = run_blame(&s->blame, view, &s->blame_complete,
2691 &s->first_displayed_line, &s->last_displayed_line,
2692 &s->selected_line, &s->done, &s->eof,
2693 s->path, s->blamed_commit->id, s->repo);
2694 if (err)
2695 break;
2696 break;
2698 case 'B': {
2699 struct got_object_qid *first;
2700 first = SIMPLEQ_FIRST(&s->blamed_commits);
2701 if (!got_object_id_cmp(first->id, s->commit_id))
2702 break;
2703 s->done = 1;
2704 thread_err = stop_blame(&s->blame);
2705 s->done = 0;
2706 if (thread_err)
2707 break;
2708 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2709 got_object_qid_free(s->blamed_commit);
2710 s->blamed_commit =
2711 SIMPLEQ_FIRST(&s->blamed_commits);
2712 err = run_blame(&s->blame, view, &s->blame_complete,
2713 &s->first_displayed_line, &s->last_displayed_line,
2714 &s->selected_line, &s->done, &s->eof, s->path,
2715 s->blamed_commit->id, s->repo);
2716 if (err)
2717 break;
2718 break;
2720 case KEY_ENTER:
2721 case '\r': {
2722 struct got_object_id *id = NULL;
2723 struct got_object_qid *pid;
2724 struct got_commit_object *commit = NULL;
2725 id = get_selected_commit_id(s->blame.lines,
2726 s->first_displayed_line, s->selected_line);
2727 if (id == NULL)
2728 break;
2729 err = got_object_open_as_commit(&commit, s->repo, id);
2730 if (err)
2731 break;
2732 pid = SIMPLEQ_FIRST(
2733 got_object_commit_get_parent_ids(commit));
2734 if (view_is_parent_view(view))
2735 begin_x = view_split_begin_x(view->begin_x);
2736 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2737 if (diff_view == NULL) {
2738 got_object_commit_close(commit);
2739 err = got_error_from_errno();
2740 break;
2742 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2743 id, s->repo);
2744 got_object_commit_close(commit);
2745 if (err) {
2746 view_close(diff_view);
2747 break;
2749 if (view_is_parent_view(view)) {
2750 err = view_close_child(view);
2751 if (err)
2752 break;
2753 err = view_set_child(view, diff_view);
2754 if (err) {
2755 view_close(diff_view);
2756 break;
2758 *focus_view = diff_view;
2759 view->child_focussed = 1;
2760 } else
2761 *new_view = diff_view;
2762 if (err)
2763 break;
2764 break;
2766 case KEY_NPAGE:
2767 case ' ':
2768 if (s->last_displayed_line >= s->blame.nlines &&
2769 s->selected_line < view->nlines - 2) {
2770 s->selected_line = MIN(s->blame.nlines,
2771 view->nlines - 2);
2772 break;
2774 if (s->last_displayed_line + view->nlines - 2
2775 <= s->blame.nlines)
2776 s->first_displayed_line +=
2777 view->nlines - 2;
2778 else
2779 s->first_displayed_line =
2780 s->blame.nlines -
2781 (view->nlines - 3);
2782 break;
2783 case KEY_RESIZE:
2784 if (s->selected_line > view->nlines - 2) {
2785 s->selected_line = MIN(s->blame.nlines,
2786 view->nlines - 2);
2788 break;
2789 default:
2790 break;
2792 return thread_err ? thread_err : err;
2795 static const struct got_error *
2796 cmd_blame(int argc, char *argv[])
2798 const struct got_error *error;
2799 struct got_repository *repo = NULL;
2800 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2801 struct got_object_id *commit_id = NULL;
2802 char *commit_id_str = NULL;
2803 int ch;
2804 struct tog_view *view;
2806 #ifndef PROFILE
2807 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2808 NULL) == -1)
2809 err(1, "pledge");
2810 #endif
2812 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2813 switch (ch) {
2814 case 'c':
2815 commit_id_str = optarg;
2816 break;
2817 case 'r':
2818 repo_path = realpath(optarg, NULL);
2819 if (repo_path == NULL)
2820 err(1, "-r option");
2821 break;
2822 default:
2823 usage();
2824 /* NOTREACHED */
2828 argc -= optind;
2829 argv += optind;
2831 if (argc == 1)
2832 path = argv[0];
2833 else
2834 usage_blame();
2836 cwd = getcwd(NULL, 0);
2837 if (cwd == NULL) {
2838 error = got_error_from_errno();
2839 goto done;
2841 if (repo_path == NULL) {
2842 repo_path = strdup(cwd);
2843 if (repo_path == NULL) {
2844 error = got_error_from_errno();
2845 goto done;
2849 error = apply_unveil(repo_path, NULL);
2850 if (error)
2851 goto done;
2853 error = got_repo_open(&repo, repo_path);
2854 if (error != NULL)
2855 goto done;
2857 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2858 if (error != NULL)
2859 goto done;
2861 if (commit_id_str == NULL) {
2862 struct got_reference *head_ref;
2863 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2864 if (error != NULL)
2865 goto done;
2866 error = got_ref_resolve(&commit_id, repo, head_ref);
2867 got_ref_close(head_ref);
2868 } else {
2869 error = got_object_resolve_id_str(&commit_id, repo,
2870 commit_id_str);
2872 if (error != NULL)
2873 goto done;
2875 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2876 if (view == NULL) {
2877 error = got_error_from_errno();
2878 goto done;
2880 error = open_blame_view(view, in_repo_path, commit_id, repo);
2881 if (error)
2882 goto done;
2883 error = view_loop(view);
2884 done:
2885 free(repo_path);
2886 free(cwd);
2887 free(commit_id);
2888 if (repo)
2889 got_repo_close(repo);
2890 return error;
2893 static const struct got_error *
2894 draw_tree_entries(struct tog_view *view,
2895 struct got_tree_entry **first_displayed_entry,
2896 struct got_tree_entry **last_displayed_entry,
2897 struct got_tree_entry **selected_entry, int *ndisplayed,
2898 const char *label, int show_ids, const char *parent_path,
2899 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2901 const struct got_error *err = NULL;
2902 struct got_tree_entry *te;
2903 wchar_t *wline;
2904 int width, n;
2906 *ndisplayed = 0;
2908 werase(view->window);
2910 if (limit == 0)
2911 return NULL;
2913 err = format_line(&wline, &width, label, view->ncols);
2914 if (err)
2915 return err;
2916 if (view_needs_focus_indication(view))
2917 wstandout(view->window);
2918 waddwstr(view->window, wline);
2919 if (view_needs_focus_indication(view))
2920 wstandend(view->window);
2921 free(wline);
2922 wline = NULL;
2923 if (width < view->ncols)
2924 waddch(view->window, '\n');
2925 if (--limit <= 0)
2926 return NULL;
2927 err = format_line(&wline, &width, parent_path, view->ncols);
2928 if (err)
2929 return err;
2930 waddwstr(view->window, wline);
2931 free(wline);
2932 wline = NULL;
2933 if (width < view->ncols)
2934 waddch(view->window, '\n');
2935 if (--limit <= 0)
2936 return NULL;
2937 waddch(view->window, '\n');
2938 if (--limit <= 0)
2939 return NULL;
2941 te = SIMPLEQ_FIRST(&entries->head);
2942 if (*first_displayed_entry == NULL) {
2943 if (selected == 0) {
2944 if (view->focussed)
2945 wstandout(view->window);
2946 *selected_entry = NULL;
2948 waddstr(view->window, " ..\n"); /* parent directory */
2949 if (selected == 0 && view->focussed)
2950 wstandend(view->window);
2951 (*ndisplayed)++;
2952 if (--limit <= 0)
2953 return NULL;
2954 n = 1;
2955 } else {
2956 n = 0;
2957 while (te != *first_displayed_entry)
2958 te = SIMPLEQ_NEXT(te, entry);
2961 while (te) {
2962 char *line = NULL, *id_str = NULL;
2964 if (show_ids) {
2965 err = got_object_id_str(&id_str, te->id);
2966 if (err)
2967 return got_error_from_errno();
2969 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2970 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2971 free(id_str);
2972 return got_error_from_errno();
2974 free(id_str);
2975 err = format_line(&wline, &width, line, view->ncols);
2976 if (err) {
2977 free(line);
2978 break;
2980 if (n == selected) {
2981 if (view->focussed)
2982 wstandout(view->window);
2983 *selected_entry = te;
2985 waddwstr(view->window, wline);
2986 if (width < view->ncols)
2987 waddch(view->window, '\n');
2988 if (n == selected && view->focussed)
2989 wstandend(view->window);
2990 free(line);
2991 free(wline);
2992 wline = NULL;
2993 n++;
2994 (*ndisplayed)++;
2995 *last_displayed_entry = te;
2996 if (--limit <= 0)
2997 break;
2998 te = SIMPLEQ_NEXT(te, entry);
3001 return err;
3004 static void
3005 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3006 const struct got_tree_entries *entries, int isroot)
3008 struct got_tree_entry *te, *prev;
3009 int i;
3011 if (*first_displayed_entry == NULL)
3012 return;
3014 te = SIMPLEQ_FIRST(&entries->head);
3015 if (*first_displayed_entry == te) {
3016 if (!isroot)
3017 *first_displayed_entry = NULL;
3018 return;
3021 /* XXX this is stupid... switch to TAILQ? */
3022 for (i = 0; i < maxscroll; i++) {
3023 while (te != *first_displayed_entry) {
3024 prev = te;
3025 te = SIMPLEQ_NEXT(te, entry);
3027 *first_displayed_entry = prev;
3028 te = SIMPLEQ_FIRST(&entries->head);
3030 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3031 *first_displayed_entry = NULL;
3034 static int
3035 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3036 struct got_tree_entry *last_displayed_entry,
3037 const struct got_tree_entries *entries)
3039 struct got_tree_entry *next, *last;
3040 int n = 0;
3042 if (*first_displayed_entry)
3043 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3044 else
3045 next = SIMPLEQ_FIRST(&entries->head);
3046 last = last_displayed_entry;
3047 while (next && last && n++ < maxscroll) {
3048 last = SIMPLEQ_NEXT(last, entry);
3049 if (last) {
3050 *first_displayed_entry = next;
3051 next = SIMPLEQ_NEXT(next, entry);
3054 return n;
3057 static const struct got_error *
3058 tree_entry_path(char **path, struct tog_parent_trees *parents,
3059 struct got_tree_entry *te)
3061 const struct got_error *err = NULL;
3062 struct tog_parent_tree *pt;
3063 size_t len = 2; /* for leading slash and NUL */
3065 TAILQ_FOREACH(pt, parents, entry)
3066 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3067 if (te)
3068 len += strlen(te->name);
3070 *path = calloc(1, len);
3071 if (path == NULL)
3072 return got_error_from_errno();
3074 (*path)[0] = '/';
3075 pt = TAILQ_LAST(parents, tog_parent_trees);
3076 while (pt) {
3077 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3078 err = got_error(GOT_ERR_NO_SPACE);
3079 goto done;
3081 if (strlcat(*path, "/", len) >= len) {
3082 err = got_error(GOT_ERR_NO_SPACE);
3083 goto done;
3085 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3087 if (te) {
3088 if (strlcat(*path, te->name, len) >= len) {
3089 err = got_error(GOT_ERR_NO_SPACE);
3090 goto done;
3093 done:
3094 if (err) {
3095 free(*path);
3096 *path = NULL;
3098 return err;
3101 static const struct got_error *
3102 blame_tree_entry(struct tog_view **new_view, int begin_x,
3103 struct got_tree_entry *te, struct tog_parent_trees *parents,
3104 struct got_object_id *commit_id, struct got_repository *repo)
3106 const struct got_error *err = NULL;
3107 char *path;
3108 struct tog_view *blame_view;
3110 err = tree_entry_path(&path, parents, te);
3111 if (err)
3112 return err;
3114 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3115 if (blame_view == NULL)
3116 return got_error_from_errno();
3118 err = open_blame_view(blame_view, path, commit_id, repo);
3119 if (err) {
3120 view_close(blame_view);
3121 free(path);
3122 } else
3123 *new_view = blame_view;
3124 return err;
3127 static const struct got_error *
3128 log_tree_entry(struct tog_view **new_view, int begin_x,
3129 struct got_tree_entry *te, struct tog_parent_trees *parents,
3130 struct got_object_id *commit_id, struct got_repository *repo)
3132 struct tog_view *log_view;
3133 const struct got_error *err = NULL;
3134 char *path;
3136 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3137 if (log_view == NULL)
3138 return got_error_from_errno();
3140 err = tree_entry_path(&path, parents, te);
3141 if (err)
3142 return err;
3144 err = open_log_view(log_view, commit_id, repo, path, 0);
3145 if (err)
3146 view_close(log_view);
3147 else
3148 *new_view = log_view;
3149 free(path);
3150 return err;
3153 static const struct got_error *
3154 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3155 struct got_object_id *commit_id, struct got_repository *repo)
3157 const struct got_error *err = NULL;
3158 char *commit_id_str = NULL;
3159 struct tog_tree_view_state *s = &view->state.tree;
3161 TAILQ_INIT(&s->parents);
3163 err = got_object_id_str(&commit_id_str, commit_id);
3164 if (err != NULL)
3165 goto done;
3167 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3168 err = got_error_from_errno();
3169 goto done;
3172 s->root = s->tree = root;
3173 s->entries = got_object_tree_get_entries(root);
3174 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3175 s->commit_id = got_object_id_dup(commit_id);
3176 if (s->commit_id == NULL) {
3177 err = got_error_from_errno();
3178 goto done;
3180 s->repo = repo;
3182 view->show = show_tree_view;
3183 view->input = input_tree_view;
3184 view->close = close_tree_view;
3185 done:
3186 free(commit_id_str);
3187 if (err) {
3188 free(s->tree_label);
3189 s->tree_label = NULL;
3191 return err;
3194 static const struct got_error *
3195 close_tree_view(struct tog_view *view)
3197 struct tog_tree_view_state *s = &view->state.tree;
3199 free(s->tree_label);
3200 s->tree_label = NULL;
3201 free(s->commit_id);
3202 s->commit_id = NULL;
3203 while (!TAILQ_EMPTY(&s->parents)) {
3204 struct tog_parent_tree *parent;
3205 parent = TAILQ_FIRST(&s->parents);
3206 TAILQ_REMOVE(&s->parents, parent, entry);
3207 free(parent);
3210 if (s->tree != s->root)
3211 got_object_tree_close(s->tree);
3212 got_object_tree_close(s->root);
3214 return NULL;
3217 static const struct got_error *
3218 show_tree_view(struct tog_view *view)
3220 const struct got_error *err = NULL;
3221 struct tog_tree_view_state *s = &view->state.tree;
3222 char *parent_path;
3224 err = tree_entry_path(&parent_path, &s->parents, NULL);
3225 if (err)
3226 return err;
3228 err = draw_tree_entries(view, &s->first_displayed_entry,
3229 &s->last_displayed_entry, &s->selected_entry,
3230 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3231 s->entries, s->selected, view->nlines, s->tree == s->root);
3232 free(parent_path);
3234 view_vborder(view);
3235 return err;
3238 static const struct got_error *
3239 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3240 struct tog_view **focus_view, struct tog_view *view, int ch)
3242 const struct got_error *err = NULL;
3243 struct tog_tree_view_state *s = &view->state.tree;
3244 struct tog_view *log_view;
3245 int begin_x = 0, nscrolled;
3247 switch (ch) {
3248 case 'i':
3249 s->show_ids = !s->show_ids;
3250 break;
3251 case 'l':
3252 if (!s->selected_entry)
3253 break;
3254 if (view_is_parent_view(view))
3255 begin_x = view_split_begin_x(view->begin_x);
3256 err = log_tree_entry(&log_view, begin_x,
3257 s->selected_entry, &s->parents,
3258 s->commit_id, s->repo);
3259 if (view_is_parent_view(view)) {
3260 err = view_close_child(view);
3261 if (err)
3262 return err;
3263 err = view_set_child(view, log_view);
3264 if (err) {
3265 view_close(log_view);
3266 break;
3268 *focus_view = log_view;
3269 view->child_focussed = 1;
3270 } else
3271 *new_view = log_view;
3272 break;
3273 case 'k':
3274 case KEY_UP:
3275 if (s->selected > 0) {
3276 s->selected--;
3277 if (s->selected == 0)
3278 break;
3280 if (s->selected > 0)
3281 break;
3282 tree_scroll_up(&s->first_displayed_entry, 1,
3283 s->entries, s->tree == s->root);
3284 break;
3285 case KEY_PPAGE:
3286 tree_scroll_up(&s->first_displayed_entry,
3287 MAX(0, view->nlines - 4 - s->selected), s->entries,
3288 s->tree == s->root);
3289 s->selected = 0;
3290 if (SIMPLEQ_FIRST(&s->entries->head) ==
3291 s->first_displayed_entry && s->tree != s->root)
3292 s->first_displayed_entry = NULL;
3293 break;
3294 case 'j':
3295 case KEY_DOWN:
3296 if (s->selected < s->ndisplayed - 1) {
3297 s->selected++;
3298 break;
3300 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3301 == NULL) {
3302 /* can't scroll any further */
3303 break;
3305 tree_scroll_down(&s->first_displayed_entry, 1,
3306 s->last_displayed_entry, s->entries);
3307 break;
3308 case KEY_NPAGE:
3309 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3310 == NULL) {
3311 /* can't scroll any further; move cursor down */
3312 if (s->selected < s->ndisplayed - 1)
3313 s->selected = s->ndisplayed - 1;
3314 break;
3316 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3317 view->nlines, s->last_displayed_entry, s->entries);
3318 if (nscrolled < view->nlines) {
3319 int ndisplayed = 0;
3320 struct got_tree_entry *te;
3321 te = s->first_displayed_entry;
3322 do {
3323 ndisplayed++;
3324 te = SIMPLEQ_NEXT(te, entry);
3325 } while (te);
3326 s->selected = ndisplayed - 1;
3328 break;
3329 case KEY_ENTER:
3330 case '\r':
3331 if (s->selected_entry == NULL) {
3332 struct tog_parent_tree *parent;
3333 case KEY_BACKSPACE:
3334 /* user selected '..' */
3335 if (s->tree == s->root)
3336 break;
3337 parent = TAILQ_FIRST(&s->parents);
3338 TAILQ_REMOVE(&s->parents, parent,
3339 entry);
3340 got_object_tree_close(s->tree);
3341 s->tree = parent->tree;
3342 s->entries =
3343 got_object_tree_get_entries(s->tree);
3344 s->first_displayed_entry =
3345 parent->first_displayed_entry;
3346 s->selected_entry =
3347 parent->selected_entry;
3348 s->selected = parent->selected;
3349 free(parent);
3350 } else if (S_ISDIR(s->selected_entry->mode)) {
3351 struct tog_parent_tree *parent;
3352 struct got_tree_object *child;
3353 err = got_object_open_as_tree(&child,
3354 s->repo, s->selected_entry->id);
3355 if (err)
3356 break;
3357 parent = calloc(1, sizeof(*parent));
3358 if (parent == NULL) {
3359 err = got_error_from_errno();
3360 break;
3362 parent->tree = s->tree;
3363 parent->first_displayed_entry =
3364 s->first_displayed_entry;
3365 parent->selected_entry = s->selected_entry;
3366 parent->selected = s->selected;
3367 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3368 s->tree = child;
3369 s->entries =
3370 got_object_tree_get_entries(s->tree);
3371 s->selected = 0;
3372 s->first_displayed_entry = NULL;
3373 } else if (S_ISREG(s->selected_entry->mode)) {
3374 struct tog_view *blame_view;
3375 int begin_x = view_is_parent_view(view) ?
3376 view_split_begin_x(view->begin_x) : 0;
3378 err = blame_tree_entry(&blame_view, begin_x,
3379 s->selected_entry, &s->parents, s->commit_id,
3380 s->repo);
3381 if (err)
3382 break;
3383 if (view_is_parent_view(view)) {
3384 err = view_close_child(view);
3385 if (err)
3386 return err;
3387 err = view_set_child(view, blame_view);
3388 if (err) {
3389 view_close(blame_view);
3390 break;
3392 *focus_view = blame_view;
3393 view->child_focussed = 1;
3394 } else
3395 *new_view = blame_view;
3397 break;
3398 case KEY_RESIZE:
3399 if (s->selected > view->nlines)
3400 s->selected = s->ndisplayed - 1;
3401 break;
3402 default:
3403 break;
3406 return err;
3409 __dead static void
3410 usage_tree(void)
3412 endwin();
3413 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3414 getprogname());
3415 exit(1);
3418 static const struct got_error *
3419 cmd_tree(int argc, char *argv[])
3421 const struct got_error *error;
3422 struct got_repository *repo = NULL;
3423 char *repo_path = NULL;
3424 struct got_object_id *commit_id = NULL;
3425 char *commit_id_arg = NULL;
3426 struct got_commit_object *commit = NULL;
3427 struct got_tree_object *tree = NULL;
3428 int ch;
3429 struct tog_view *view;
3431 #ifndef PROFILE
3432 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3433 NULL) == -1)
3434 err(1, "pledge");
3435 #endif
3437 while ((ch = getopt(argc, argv, "c:")) != -1) {
3438 switch (ch) {
3439 case 'c':
3440 commit_id_arg = optarg;
3441 break;
3442 default:
3443 usage();
3444 /* NOTREACHED */
3448 argc -= optind;
3449 argv += optind;
3451 if (argc == 0) {
3452 repo_path = getcwd(NULL, 0);
3453 if (repo_path == NULL)
3454 return got_error_from_errno();
3455 } else if (argc == 1) {
3456 repo_path = realpath(argv[0], NULL);
3457 if (repo_path == NULL)
3458 return got_error_from_errno();
3459 } else
3460 usage_log();
3462 error = apply_unveil(repo_path, NULL);
3463 if (error)
3464 return error;
3466 error = got_repo_open(&repo, repo_path);
3467 free(repo_path);
3468 if (error != NULL)
3469 return error;
3471 if (commit_id_arg == NULL)
3472 error = get_head_commit_id(&commit_id, repo);
3473 else
3474 error = got_object_resolve_id_str(&commit_id, repo,
3475 commit_id_arg);
3476 if (error != NULL)
3477 goto done;
3479 error = got_object_open_as_commit(&commit, repo, commit_id);
3480 if (error != NULL)
3481 goto done;
3483 error = got_object_open_as_tree(&tree, repo,
3484 got_object_commit_get_tree_id(commit));
3485 if (error != NULL)
3486 goto done;
3488 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3489 if (view == NULL) {
3490 error = got_error_from_errno();
3491 goto done;
3493 error = open_tree_view(view, tree, commit_id, repo);
3494 if (error)
3495 goto done;
3496 error = view_loop(view);
3497 done:
3498 free(commit_id);
3499 if (commit)
3500 got_object_commit_close(commit);
3501 if (tree)
3502 got_object_tree_close(tree);
3503 if (repo)
3504 got_repo_close(repo);
3505 return error;
3508 static void
3509 init_curses(void)
3511 initscr();
3512 cbreak();
3513 halfdelay(1); /* Do fast refresh while initial view is loading. */
3514 noecho();
3515 nonl();
3516 intrflush(stdscr, FALSE);
3517 keypad(stdscr, TRUE);
3518 curs_set(0);
3519 signal(SIGWINCH, tog_sigwinch);
3522 __dead static void
3523 usage(void)
3525 int i;
3527 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3528 "Available commands:\n", getprogname());
3529 for (i = 0; i < nitems(tog_commands); i++) {
3530 struct tog_cmd *cmd = &tog_commands[i];
3531 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3533 exit(1);
3536 static char **
3537 make_argv(const char *arg0, const char *arg1)
3539 char **argv;
3540 int argc = (arg1 == NULL ? 1 : 2);
3542 argv = calloc(argc, sizeof(char *));
3543 if (argv == NULL)
3544 err(1, "calloc");
3545 argv[0] = strdup(arg0);
3546 if (argv[0] == NULL)
3547 err(1, "calloc");
3548 if (arg1) {
3549 argv[1] = strdup(arg1);
3550 if (argv[1] == NULL)
3551 err(1, "calloc");
3554 return argv;
3557 int
3558 main(int argc, char *argv[])
3560 const struct got_error *error = NULL;
3561 struct tog_cmd *cmd = NULL;
3562 int ch, hflag = 0;
3563 char **cmd_argv = NULL;
3565 setlocale(LC_ALL, "");
3567 while ((ch = getopt(argc, argv, "h")) != -1) {
3568 switch (ch) {
3569 case 'h':
3570 hflag = 1;
3571 break;
3572 default:
3573 usage();
3574 /* NOTREACHED */
3578 argc -= optind;
3579 argv += optind;
3580 optind = 0;
3581 optreset = 1;
3583 if (argc == 0) {
3584 if (hflag)
3585 usage();
3586 /* Build an argument vector which runs a default command. */
3587 cmd = &tog_commands[0];
3588 cmd_argv = make_argv(cmd->name, NULL);
3589 argc = 1;
3590 } else {
3591 int i;
3593 /* Did the user specific a command? */
3594 for (i = 0; i < nitems(tog_commands); i++) {
3595 if (strncmp(tog_commands[i].name, argv[0],
3596 strlen(argv[0])) == 0) {
3597 cmd = &tog_commands[i];
3598 if (hflag)
3599 tog_commands[i].cmd_usage();
3600 break;
3603 if (cmd == NULL) {
3604 /* Did the user specify a repository? */
3605 char *repo_path = realpath(argv[0], NULL);
3606 if (repo_path) {
3607 struct got_repository *repo;
3608 error = got_repo_open(&repo, repo_path);
3609 if (error == NULL)
3610 got_repo_close(repo);
3611 } else
3612 error = got_error_from_errno();
3613 if (error) {
3614 if (hflag) {
3615 fprintf(stderr, "%s: '%s' is not a "
3616 "known command\n", getprogname(),
3617 argv[0]);
3618 usage();
3620 fprintf(stderr, "%s: '%s' is neither a known "
3621 "command nor a path to a repository\n",
3622 getprogname(), argv[0]);
3623 free(repo_path);
3624 return 1;
3626 cmd = &tog_commands[0];
3627 cmd_argv = make_argv(cmd->name, repo_path);
3628 argc = 2;
3629 free(repo_path);
3633 init_curses();
3635 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3636 if (error)
3637 goto done;
3638 done:
3639 endwin();
3640 free(cmd_argv);
3641 if (error)
3642 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3643 return 0;