Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_worktree.h"
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 #ifndef MAX
57 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
58 #endif
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 struct tog_cmd {
66 const char *name;
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
69 const char *descr;
70 };
72 __dead static void usage(void);
73 __dead static void usage_log(void);
74 __dead static void usage_diff(void);
75 __dead static void usage_blame(void);
76 __dead static void usage_tree(void);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
83 static struct tog_cmd tog_commands[] = {
84 { "log", cmd_log, usage_log,
85 "show repository history" },
86 { "diff", cmd_diff, usage_diff,
87 "compare files and directories" },
88 { "blame", cmd_blame, usage_blame,
89 "show line-by-line file history" },
90 { "tree", cmd_tree, usage_tree,
91 "browse trees in repository" },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 int idx;
106 };
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
109 int ncommits;
110 struct commit_queue_head head;
111 };
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
115 FILE *f;
116 int first_displayed_line;
117 int last_displayed_line;
118 int eof;
119 int diff_context;
120 struct got_repository *repo;
122 /* passed from log view; may be NULL */
123 struct commit_queue_entry *selected_entry;
124 struct commit_queue *commits;
125 int *log_complete;
126 pthread_cond_t *need_commits;
127 int *commits_needed;
128 };
130 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
132 struct tog_log_thread_args {
133 pthread_cond_t need_commits;
134 int commits_needed;
135 struct got_commit_graph *graph;
136 struct commit_queue *commits;
137 const char *in_repo_path;
138 struct got_object_id *start_id;
139 struct got_repository *repo;
140 int log_complete;
141 sig_atomic_t *quit;
142 struct tog_view *view;
143 struct commit_queue_entry **first_displayed_entry;
144 struct commit_queue_entry **selected_entry;
145 };
147 struct tog_log_view_state {
148 struct commit_queue commits;
149 struct commit_queue_entry *first_displayed_entry;
150 struct commit_queue_entry *last_displayed_entry;
151 struct commit_queue_entry *selected_entry;
152 int selected;
153 char *in_repo_path;
154 struct got_repository *repo;
155 struct got_object_id *start_id;
156 sig_atomic_t quit;
157 pthread_t thread;
158 struct tog_log_thread_args thread_args;
159 };
161 struct tog_blame_cb_args {
162 struct tog_blame_line *lines; /* one per line */
163 int nlines;
165 struct tog_view *view;
166 struct got_object_id *commit_id;
167 int *quit;
168 };
170 struct tog_blame_thread_args {
171 const char *path;
172 struct got_repository *repo;
173 struct tog_blame_cb_args *cb_args;
174 int *complete;
175 };
177 struct tog_blame {
178 FILE *f;
179 size_t filesize;
180 struct tog_blame_line *lines;
181 int nlines;
182 pthread_t thread;
183 struct tog_blame_thread_args thread_args;
184 struct tog_blame_cb_args cb_args;
185 const char *path;
186 };
188 struct tog_blame_view_state {
189 int first_displayed_line;
190 int last_displayed_line;
191 int selected_line;
192 int blame_complete;
193 int eof;
194 int done;
195 struct got_object_id_queue blamed_commits;
196 struct got_object_qid *blamed_commit;
197 char *path;
198 struct got_repository *repo;
199 struct got_object_id *commit_id;
200 struct tog_blame blame;
201 };
203 struct tog_parent_tree {
204 TAILQ_ENTRY(tog_parent_tree) entry;
205 struct got_tree_object *tree;
206 struct got_tree_entry *first_displayed_entry;
207 struct got_tree_entry *selected_entry;
208 int selected;
209 };
211 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
213 struct tog_tree_view_state {
214 char *tree_label;
215 struct got_tree_object *root;
216 struct got_tree_object *tree;
217 const struct got_tree_entries *entries;
218 struct got_tree_entry *first_displayed_entry;
219 struct got_tree_entry *last_displayed_entry;
220 struct got_tree_entry *selected_entry;
221 int ndisplayed, selected, show_ids;
222 struct tog_parent_trees parents;
223 struct got_object_id *commit_id;
224 struct got_repository *repo;
225 };
227 /*
228 * We implement two types of views: parent views and child views.
230 * The 'Tab' key switches between a parent view and its child view.
231 * Child views are shown side-by-side to their parent view, provided
232 * there is enough screen estate.
234 * When a new view is opened from within a parent view, this new view
235 * becomes a child view of the parent view, replacing any existing child.
237 * When a new view is opened from within a child view, this new view
238 * becomes a parent view which will obscure the views below until the
239 * user quits the new parent view by typing 'q'.
241 * This list of views contains parent views only.
242 * Child views are only pointed to by their parent view.
243 */
244 TAILQ_HEAD(tog_view_list_head, tog_view);
246 struct tog_view {
247 TAILQ_ENTRY(tog_view) entry;
248 WINDOW *window;
249 PANEL *panel;
250 int nlines, ncols, begin_y, begin_x;
251 int lines, cols; /* copies of LINES and COLS */
252 int focussed;
253 struct tog_view *parent;
254 struct tog_view *child;
255 int child_focussed;
257 /* type-specific state */
258 enum tog_view_type type;
259 union {
260 struct tog_diff_view_state diff;
261 struct tog_log_view_state log;
262 struct tog_blame_view_state blame;
263 struct tog_tree_view_state tree;
264 } state;
266 const struct got_error *(*show)(struct tog_view *);
267 const struct got_error *(*input)(struct tog_view **,
268 struct tog_view **, struct tog_view**, struct tog_view *, int);
269 const struct got_error *(*close)(struct tog_view *);
270 };
272 static const struct got_error *open_diff_view(struct tog_view *,
273 struct got_object_id *, struct got_object_id *, struct commit_queue_entry *,
274 struct commit_queue *, int *, pthread_cond_t *, int *,
275 struct got_repository *);
276 static const struct got_error *show_diff_view(struct tog_view *);
277 static const struct got_error *input_diff_view(struct tog_view **,
278 struct tog_view **, struct tog_view **, struct tog_view *, int);
279 static const struct got_error* close_diff_view(struct tog_view *);
281 static const struct got_error *open_log_view(struct tog_view *,
282 struct got_object_id *, struct got_repository *, const char *, int);
283 static const struct got_error * show_log_view(struct tog_view *);
284 static const struct got_error *input_log_view(struct tog_view **,
285 struct tog_view **, struct tog_view **, struct tog_view *, int);
286 static const struct got_error *close_log_view(struct tog_view *);
288 static const struct got_error *open_blame_view(struct tog_view *, char *,
289 struct got_object_id *, struct got_repository *);
290 static const struct got_error *show_blame_view(struct tog_view *);
291 static const struct got_error *input_blame_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error *close_blame_view(struct tog_view *);
295 static const struct got_error *open_tree_view(struct tog_view *,
296 struct got_tree_object *, struct got_object_id *, struct got_repository *);
297 static const struct got_error *show_tree_view(struct tog_view *);
298 static const struct got_error *input_tree_view(struct tog_view **,
299 struct tog_view **, struct tog_view **, struct tog_view *, int);
300 static const struct got_error *close_tree_view(struct tog_view *);
302 static volatile sig_atomic_t tog_sigwinch_received;
304 static void
305 tog_sigwinch(int signo)
307 tog_sigwinch_received = 1;
310 static const struct got_error *
311 view_close(struct tog_view *view)
313 const struct got_error *err = NULL;
315 if (view->child) {
316 view_close(view->child);
317 view->child = NULL;
319 if (view->close)
320 err = view->close(view);
321 if (view->panel)
322 del_panel(view->panel);
323 if (view->window)
324 delwin(view->window);
325 free(view);
326 return err;
329 static struct tog_view *
330 view_open(int nlines, int ncols, int begin_y, int begin_x,
331 enum tog_view_type type)
333 struct tog_view *view = calloc(1, sizeof(*view));
335 if (view == NULL)
336 return NULL;
338 view->type = type;
339 view->lines = LINES;
340 view->cols = COLS;
341 view->nlines = nlines ? nlines : LINES - begin_y;
342 view->ncols = ncols ? ncols : COLS - begin_x;
343 view->begin_y = begin_y;
344 view->begin_x = begin_x;
345 view->window = newwin(nlines, ncols, begin_y, begin_x);
346 if (view->window == NULL) {
347 view_close(view);
348 return NULL;
350 view->panel = new_panel(view->window);
351 if (view->panel == NULL ||
352 set_panel_userptr(view->panel, view) != OK) {
353 view_close(view);
354 return NULL;
357 keypad(view->window, TRUE);
358 return view;
361 static int
362 view_split_begin_x(int begin_x)
364 if (begin_x > 0 || COLS < 120)
365 return 0;
366 return (COLS - MAX(COLS / 2, 80));
369 static const struct got_error *view_resize(struct tog_view *);
371 static const struct got_error *
372 view_splitscreen(struct tog_view *view)
374 const struct got_error *err = NULL;
376 view->begin_y = 0;
377 view->begin_x = view_split_begin_x(0);
378 view->nlines = LINES;
379 view->ncols = COLS - view->begin_x;
380 view->lines = LINES;
381 view->cols = COLS;
382 err = view_resize(view);
383 if (err)
384 return err;
386 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
387 return got_error_from_errno();
389 return NULL;
392 static const struct got_error *
393 view_fullscreen(struct tog_view *view)
395 const struct got_error *err = NULL;
397 view->begin_x = 0;
398 view->begin_y = 0;
399 view->nlines = LINES;
400 view->ncols = COLS;
401 view->lines = LINES;
402 view->cols = COLS;
403 err = view_resize(view);
404 if (err)
405 return err;
407 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 return got_error_from_errno();
410 return NULL;
413 static int
414 view_is_parent_view(struct tog_view *view)
416 return view->parent == NULL;
419 static const struct got_error *
420 view_resize(struct tog_view *view)
422 int nlines, ncols;
424 if (view->lines > LINES)
425 nlines = view->nlines - (view->lines - LINES);
426 else
427 nlines = view->nlines + (LINES - view->lines);
429 if (view->cols > COLS)
430 ncols = view->ncols - (view->cols - COLS);
431 else
432 ncols = view->ncols + (COLS - view->cols);
434 if (wresize(view->window, nlines, ncols) == ERR)
435 return got_error_from_errno();
436 if (replace_panel(view->panel, view->window) == ERR)
437 return got_error_from_errno();
438 wclear(view->window);
440 view->nlines = nlines;
441 view->ncols = ncols;
442 view->lines = LINES;
443 view->cols = COLS;
445 if (view->child) {
446 view->child->begin_x = view_split_begin_x(view->begin_x);
447 if (view->child->begin_x == 0) {
448 view_fullscreen(view->child);
449 if (view->child->focussed)
450 show_panel(view->child->panel);
451 else
452 show_panel(view->panel);
453 } else {
454 view_splitscreen(view->child);
455 show_panel(view->child->panel);
459 return NULL;
462 static const struct got_error *
463 view_close_child(struct tog_view *view)
465 const struct got_error *err = NULL;
467 if (view->child == NULL)
468 return NULL;
470 err = view_close(view->child);
471 view->child = NULL;
472 return err;
475 static const struct got_error *
476 view_set_child(struct tog_view *view, struct tog_view *child)
478 const struct got_error *err = NULL;
480 view->child = child;
481 child->parent = view;
482 return err;
485 static int
486 view_is_splitscreen(struct tog_view *view)
488 return !view_is_parent_view(view) && view->begin_x > 0;
491 static void
492 tog_resizeterm(void)
494 int cols, lines;
495 struct winsize size;
497 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
498 cols = 80; /* Default */
499 lines = 24;
500 } else {
501 cols = size.ws_col;
502 lines = size.ws_row;
504 resize_term(lines, cols);
507 static const struct got_error *
508 view_input(struct tog_view **new, struct tog_view **dead,
509 struct tog_view **focus, int *done, struct tog_view *view,
510 struct tog_view_list_head *views)
512 const struct got_error *err = NULL;
513 struct tog_view *v;
514 int ch, errcode;
516 *new = NULL;
517 *dead = NULL;
518 *focus = NULL;
520 nodelay(stdscr, FALSE);
521 /* Allow threads to make progress while we are waiting for input. */
522 errcode = pthread_mutex_unlock(&tog_mutex);
523 if (errcode)
524 return got_error_set_errno(errcode);
525 ch = wgetch(view->window);
526 errcode = pthread_mutex_lock(&tog_mutex);
527 if (errcode)
528 return got_error_set_errno(errcode);
529 nodelay(stdscr, TRUE);
531 if (tog_sigwinch_received) {
532 tog_resizeterm();
533 tog_sigwinch_received = 0;
534 TAILQ_FOREACH(v, views, entry) {
535 err = view_resize(v);
536 if (err)
537 return err;
538 err = v->input(new, dead, focus, v, KEY_RESIZE);
539 if (err)
540 return err;
544 switch (ch) {
545 case ERR:
546 break;
547 case '\t':
548 if (view->child) {
549 *focus = view->child;
550 view->child_focussed = 1;
551 } else if (view->parent) {
552 *focus = view->parent;
553 view->parent->child_focussed = 0;
555 break;
556 case 'q':
557 err = view->input(new, dead, focus, view, ch);
558 *dead = view;
559 break;
560 case 'Q':
561 *done = 1;
562 break;
563 case 'f':
564 if (view_is_parent_view(view)) {
565 if (view->child == NULL)
566 break;
567 if (view_is_splitscreen(view->child)) {
568 *focus = view->child;
569 view->child_focussed = 1;
570 err = view_fullscreen(view->child);
571 } else
572 err = view_splitscreen(view->child);
573 if (err)
574 break;
575 err = view->child->input(new, dead, focus,
576 view->child, KEY_RESIZE);
577 } else {
578 if (view_is_splitscreen(view)) {
579 *focus = view;
580 view->parent->child_focussed = 1;
581 err = view_fullscreen(view);
582 } else {
583 err = view_splitscreen(view);
585 if (err)
586 break;
587 err = view->input(new, dead, focus, view,
588 KEY_RESIZE);
590 break;
591 case KEY_RESIZE:
592 break;
593 default:
594 err = view->input(new, dead, focus, view, ch);
595 break;
598 return err;
601 void
602 view_vborder(struct tog_view *view)
604 PANEL *panel;
605 struct tog_view *view_above;
607 if (view->parent)
608 return view_vborder(view->parent);
610 panel = panel_above(view->panel);
611 if (panel == NULL)
612 return;
614 view_above = panel_userptr(panel);
615 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
616 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
619 int
620 view_needs_focus_indication(struct tog_view *view)
622 if (view_is_parent_view(view)) {
623 if (view->child == NULL || view->child_focussed)
624 return 0;
625 if (!view_is_splitscreen(view->child))
626 return 0;
627 } else if (!view_is_splitscreen(view))
628 return 0;
630 return view->focussed;
633 static const struct got_error *
634 view_loop(struct tog_view *view)
636 const struct got_error *err = NULL;
637 struct tog_view_list_head views;
638 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
639 int fast_refresh = 10;
640 int done = 0, errcode;
642 errcode = pthread_mutex_lock(&tog_mutex);
643 if (errcode)
644 return got_error_set_errno(errcode);
646 TAILQ_INIT(&views);
647 TAILQ_INSERT_HEAD(&views, view, entry);
649 main_view = view;
650 view->focussed = 1;
651 err = view->show(view);
652 if (err)
653 return err;
654 update_panels();
655 doupdate();
656 while (!TAILQ_EMPTY(&views) && !done) {
657 /* Refresh fast during initialization, then become slower. */
658 if (fast_refresh && fast_refresh-- == 0)
659 halfdelay(10); /* switch to once per second */
661 err = view_input(&new_view, &dead_view, &focus_view, &done,
662 view, &views);
663 if (err)
664 break;
665 if (dead_view) {
666 struct tog_view *prev = NULL;
668 if (view_is_parent_view(dead_view))
669 prev = TAILQ_PREV(dead_view,
670 tog_view_list_head, entry);
671 else if (view->parent != dead_view)
672 prev = view->parent;
674 if (dead_view->parent)
675 dead_view->parent->child = NULL;
676 else
677 TAILQ_REMOVE(&views, dead_view, entry);
679 err = view_close(dead_view);
680 if (err || dead_view == main_view)
681 goto done;
683 if (view == dead_view) {
684 if (focus_view)
685 view = focus_view;
686 else if (prev)
687 view = prev;
688 else if (!TAILQ_EMPTY(&views))
689 view = TAILQ_LAST(&views,
690 tog_view_list_head);
691 else
692 view = NULL;
693 if (view) {
694 if (view->child && view->child_focussed)
695 focus_view = view->child;
696 else
697 focus_view = view;
701 if (new_view) {
702 struct tog_view *v, *t;
703 /* Only allow one parent view per type. */
704 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
705 if (v->type != new_view->type)
706 continue;
707 TAILQ_REMOVE(&views, v, entry);
708 err = view_close(v);
709 if (err)
710 goto done;
711 break;
713 TAILQ_INSERT_TAIL(&views, new_view, entry);
714 view = new_view;
715 if (focus_view == NULL)
716 focus_view = new_view;
718 if (focus_view) {
719 show_panel(focus_view->panel);
720 if (view)
721 view->focussed = 0;
722 focus_view->focussed = 1;
723 view = focus_view;
724 if (new_view)
725 show_panel(new_view->panel);
726 if (view->child && view_is_splitscreen(view->child))
727 show_panel(view->child->panel);
729 if (view) {
730 if (focus_view == NULL) {
731 view->focussed = 1;
732 show_panel(view->panel);
733 if (view->child && view_is_splitscreen(view->child))
734 show_panel(view->child->panel);
735 focus_view = view;
737 if (view->parent) {
738 err = view->parent->show(view->parent);
739 if (err)
740 goto done;
742 err = view->show(view);
743 if (err)
744 goto done;
745 if (view->child) {
746 err = view->child->show(view->child);
747 if (err)
748 goto done;
750 update_panels();
751 doupdate();
754 done:
755 while (!TAILQ_EMPTY(&views)) {
756 view = TAILQ_FIRST(&views);
757 TAILQ_REMOVE(&views, view, entry);
758 view_close(view);
761 errcode = pthread_mutex_unlock(&tog_mutex);
762 if (errcode)
763 return got_error_set_errno(errcode);
765 return err;
768 __dead static void
769 usage_log(void)
771 endwin();
772 fprintf(stderr,
773 "usage: %s log [-c commit] [-r repository-path] [path]\n",
774 getprogname());
775 exit(1);
778 /* Create newly allocated wide-character string equivalent to a byte string. */
779 static const struct got_error *
780 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
782 char *vis = NULL;
783 const struct got_error *err = NULL;
785 *ws = NULL;
786 *wlen = mbstowcs(NULL, s, 0);
787 if (*wlen == (size_t)-1) {
788 int vislen;
789 if (errno != EILSEQ)
790 return got_error_from_errno();
792 /* byte string invalid in current encoding; try to "fix" it */
793 err = got_mbsavis(&vis, &vislen, s);
794 if (err)
795 return err;
796 *wlen = mbstowcs(NULL, vis, 0);
797 if (*wlen == (size_t)-1) {
798 err = got_error_from_errno(); /* give up */
799 goto done;
803 *ws = calloc(*wlen + 1, sizeof(*ws));
804 if (*ws == NULL) {
805 err = got_error_from_errno();
806 goto done;
809 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
810 err = got_error_from_errno();
811 done:
812 free(vis);
813 if (err) {
814 free(*ws);
815 *ws = NULL;
816 *wlen = 0;
818 return err;
821 /* Format a line for display, ensuring that it won't overflow a width limit. */
822 static const struct got_error *
823 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
825 const struct got_error *err = NULL;
826 int cols = 0;
827 wchar_t *wline = NULL;
828 size_t wlen;
829 int i;
831 *wlinep = NULL;
832 *widthp = 0;
834 err = mbs2ws(&wline, &wlen, line);
835 if (err)
836 return err;
838 i = 0;
839 while (i < wlen && cols < wlimit) {
840 int width = wcwidth(wline[i]);
841 switch (width) {
842 case 0:
843 i++;
844 break;
845 case 1:
846 case 2:
847 if (cols + width <= wlimit)
848 cols += width;
849 i++;
850 break;
851 case -1:
852 if (wline[i] == L'\t')
853 cols += TABSIZE - ((cols + 1) % TABSIZE);
854 i++;
855 break;
856 default:
857 err = got_error_from_errno();
858 goto done;
861 wline[i] = L'\0';
862 if (widthp)
863 *widthp = cols;
864 done:
865 if (err)
866 free(wline);
867 else
868 *wlinep = wline;
869 return err;
872 static const struct got_error *
873 draw_commit(struct tog_view *view, struct got_commit_object *commit,
874 struct got_object_id *id)
876 const struct got_error *err = NULL;
877 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
878 char *logmsg0 = NULL, *logmsg = NULL;
879 char *author0 = NULL, *author = NULL;
880 wchar_t *wlogmsg = NULL, *wauthor = NULL;
881 int author_width, logmsg_width;
882 char *newline, *smallerthan;
883 char *line = NULL;
884 int col, limit;
885 static const size_t date_display_cols = 9;
886 static const size_t author_display_cols = 16;
887 const int avail = view->ncols;
888 struct tm tm;
889 time_t committer_time;
891 committer_time = got_object_commit_get_committer_time(commit);
892 if (localtime_r(&committer_time, &tm) == NULL)
893 return got_error_from_errno();
894 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
895 >= sizeof(datebuf))
896 return got_error(GOT_ERR_NO_SPACE);
898 if (avail < date_display_cols)
899 limit = MIN(sizeof(datebuf) - 1, avail);
900 else
901 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
902 waddnstr(view->window, datebuf, limit);
903 col = limit + 1;
904 if (col > avail)
905 goto done;
907 author0 = strdup(got_object_commit_get_author(commit));
908 if (author0 == NULL) {
909 err = got_error_from_errno();
910 goto done;
912 author = author0;
913 smallerthan = strchr(author, '<');
914 if (smallerthan)
915 *smallerthan = '\0';
916 else {
917 char *at = strchr(author, '@');
918 if (at)
919 *at = '\0';
921 limit = avail - col;
922 err = format_line(&wauthor, &author_width, author, limit);
923 if (err)
924 goto done;
925 waddwstr(view->window, wauthor);
926 col += author_width;
927 while (col <= avail && author_width < author_display_cols + 1) {
928 waddch(view->window, ' ');
929 col++;
930 author_width++;
932 if (col > avail)
933 goto done;
935 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
936 if (logmsg0 == NULL) {
937 err = got_error_from_errno();
938 goto done;
940 logmsg = logmsg0;
941 while (*logmsg == '\n')
942 logmsg++;
943 newline = strchr(logmsg, '\n');
944 if (newline)
945 *newline = '\0';
946 limit = avail - col;
947 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
948 if (err)
949 goto done;
950 waddwstr(view->window, wlogmsg);
951 col += logmsg_width;
952 while (col <= avail) {
953 waddch(view->window, ' ');
954 col++;
956 done:
957 free(logmsg0);
958 free(wlogmsg);
959 free(author0);
960 free(wauthor);
961 free(line);
962 return err;
965 static struct commit_queue_entry *
966 alloc_commit_queue_entry(struct got_commit_object *commit,
967 struct got_object_id *id)
969 struct commit_queue_entry *entry;
971 entry = calloc(1, sizeof(*entry));
972 if (entry == NULL)
973 return NULL;
975 entry->id = id;
976 entry->commit = commit;
977 return entry;
980 static void
981 pop_commit(struct commit_queue *commits)
983 struct commit_queue_entry *entry;
985 entry = TAILQ_FIRST(&commits->head);
986 TAILQ_REMOVE(&commits->head, entry, entry);
987 got_object_commit_close(entry->commit);
988 commits->ncommits--;
989 /* Don't free entry->id! It is owned by the commit graph. */
990 free(entry);
993 static void
994 free_commits(struct commit_queue *commits)
996 while (!TAILQ_EMPTY(&commits->head))
997 pop_commit(commits);
1000 static const struct got_error *
1001 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1002 int minqueue, struct got_repository *repo, const char *path)
1004 const struct got_error *err = NULL;
1005 int nqueued = 0;
1008 * We keep all commits open throughout the lifetime of the log
1009 * view in order to avoid having to re-fetch commits from disk
1010 * while updating the display.
1012 while (nqueued < minqueue) {
1013 struct got_object_id *id;
1014 struct got_commit_object *commit;
1015 struct commit_queue_entry *entry;
1016 int errcode;
1018 err = got_commit_graph_iter_next(&id, graph);
1019 if (err) {
1020 if (err->code != GOT_ERR_ITER_NEED_MORE)
1021 break;
1022 err = got_commit_graph_fetch_commits(graph,
1023 minqueue, repo);
1024 if (err)
1025 return err;
1026 continue;
1029 if (id == NULL)
1030 break;
1032 err = got_object_open_as_commit(&commit, repo, id);
1033 if (err)
1034 break;
1035 entry = alloc_commit_queue_entry(commit, id);
1036 if (entry == NULL) {
1037 err = got_error_from_errno();
1038 break;
1041 errcode = pthread_mutex_lock(&tog_mutex);
1042 if (errcode) {
1043 err = got_error_set_errno(errcode);
1044 break;
1047 entry->idx = commits->ncommits;
1048 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1049 nqueued++;
1050 commits->ncommits++;
1052 errcode = pthread_mutex_unlock(&tog_mutex);
1053 if (errcode && err == NULL)
1054 err = got_error_set_errno(errcode);
1057 return err;
1060 static const struct got_error *
1061 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1063 const struct got_error *err = NULL;
1064 struct got_reference *head_ref;
1066 *head_id = NULL;
1068 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1069 if (err)
1070 return err;
1072 err = got_ref_resolve(head_id, repo, head_ref);
1073 got_ref_close(head_ref);
1074 if (err) {
1075 *head_id = NULL;
1076 return err;
1079 return NULL;
1082 static const struct got_error *
1083 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1084 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1085 struct commit_queue *commits, int selected_idx, int limit,
1086 const char *path, int commits_needed)
1088 const struct got_error *err = NULL;
1089 struct commit_queue_entry *entry;
1090 int ncommits, width;
1091 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1092 wchar_t *wline;
1094 entry = first;
1095 ncommits = 0;
1096 while (entry) {
1097 if (ncommits == selected_idx) {
1098 *selected = entry;
1099 break;
1101 entry = TAILQ_NEXT(entry, entry);
1102 ncommits++;
1105 if (*selected) {
1106 err = got_object_id_str(&id_str, (*selected)->id);
1107 if (err)
1108 return err;
1111 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1112 entry ? entry->idx + 1 : 0, commits->ncommits,
1113 commits_needed == 0 ? "" : " loading...") == -1)
1114 return got_error_from_errno();
1116 if (path && strcmp(path, "/") != 0) {
1117 if (asprintf(&header, "commit %s %s%s",
1118 id_str ? id_str : "........................................",
1119 path, ncommits_str) == -1) {
1120 err = got_error_from_errno();
1121 header = NULL;
1122 goto done;
1124 } else if (asprintf(&header, "commit %s%s",
1125 id_str ? id_str : "........................................",
1126 ncommits_str) == -1) {
1127 err = got_error_from_errno();
1128 header = NULL;
1129 goto done;
1131 err = format_line(&wline, &width, header, view->ncols);
1132 if (err)
1133 goto done;
1135 werase(view->window);
1137 if (view_needs_focus_indication(view))
1138 wstandout(view->window);
1139 waddwstr(view->window, wline);
1140 while (width < view->ncols) {
1141 waddch(view->window, ' ');
1142 width++;
1144 if (view_needs_focus_indication(view))
1145 wstandend(view->window);
1146 free(wline);
1147 if (limit <= 1)
1148 goto done;
1150 entry = first;
1151 *last = first;
1152 ncommits = 0;
1153 while (entry) {
1154 if (ncommits >= limit - 1)
1155 break;
1156 if (view->focussed && ncommits == selected_idx)
1157 wstandout(view->window);
1158 err = draw_commit(view, entry->commit, entry->id);
1159 if (view->focussed && ncommits == selected_idx)
1160 wstandend(view->window);
1161 if (err)
1162 break;
1163 ncommits++;
1164 *last = entry;
1165 entry = TAILQ_NEXT(entry, entry);
1168 view_vborder(view);
1169 done:
1170 free(id_str);
1171 free(ncommits_str);
1172 free(header);
1173 return err;
1176 static void
1177 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1178 struct commit_queue *commits)
1180 struct commit_queue_entry *entry;
1181 int nscrolled = 0;
1183 entry = TAILQ_FIRST(&commits->head);
1184 if (*first_displayed_entry == entry)
1185 return;
1187 entry = *first_displayed_entry;
1188 while (entry && nscrolled < maxscroll) {
1189 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1190 if (entry) {
1191 *first_displayed_entry = entry;
1192 nscrolled++;
1197 static const struct got_error *
1198 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1199 struct commit_queue_entry **last_displayed_entry,
1200 struct commit_queue *commits, int *log_complete, int *commits_needed,
1201 pthread_cond_t *need_commits)
1203 const struct got_error *err = NULL;
1204 struct commit_queue_entry *pentry;
1205 int nscrolled = 0;
1207 if (*last_displayed_entry == NULL)
1208 return NULL;
1210 do {
1211 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1212 if (pentry == NULL) {
1213 int errcode;
1214 if (*log_complete)
1215 return NULL;
1216 *commits_needed = maxscroll + 20;
1217 errcode = pthread_cond_signal(need_commits);
1218 if (errcode)
1219 return got_error_set_errno(errcode);
1220 return NULL;
1222 *last_displayed_entry = pentry;
1224 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1225 if (pentry == NULL)
1226 break;
1227 *first_displayed_entry = pentry;
1228 } while (++nscrolled < maxscroll);
1230 return err;
1233 static const struct got_error *
1234 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1235 struct commit_queue_entry *selected_entry,
1236 struct commit_queue *commits, int *log_complete,
1237 pthread_cond_t *need_commits, int *commits_needed,
1238 struct got_repository *repo)
1240 const struct got_error *err;
1241 struct got_object_qid *parent_id;
1242 struct tog_view *diff_view;
1243 struct got_commit_object *commit = selected_entry->commit;
1244 struct got_object_id *commit_id = selected_entry->id;
1246 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1247 if (diff_view == NULL)
1248 return got_error_from_errno();
1250 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1251 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1252 commit_id, selected_entry, commits, log_complete, need_commits,
1253 commits_needed, repo);
1254 if (err == NULL)
1255 *new_view = diff_view;
1256 return err;
1259 static const struct got_error *
1260 browse_commit(struct tog_view **new_view, int begin_x,
1261 struct commit_queue_entry *entry, struct got_repository *repo)
1263 const struct got_error *err = NULL;
1264 struct got_tree_object *tree;
1265 struct tog_view *tree_view;
1267 err = got_object_open_as_tree(&tree, repo,
1268 got_object_commit_get_tree_id(entry->commit));
1269 if (err)
1270 return err;
1272 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1273 if (tree_view == NULL)
1274 return got_error_from_errno();
1276 err = open_tree_view(tree_view, tree, entry->id, repo);
1277 if (err)
1278 got_object_tree_close(tree);
1279 else
1280 *new_view = tree_view;
1281 return err;
1284 static void *
1285 log_thread(void *arg)
1287 const struct got_error *err = NULL;
1288 int errcode = 0;
1289 struct tog_log_thread_args *a = arg;
1290 int done = 0;
1292 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1293 if (err)
1294 return (void *)err;
1296 while (!done && !err) {
1297 err = queue_commits(a->graph, a->commits, 1, a->repo,
1298 a->in_repo_path);
1299 if (err) {
1300 if (err->code != GOT_ERR_ITER_COMPLETED)
1301 return (void *)err;
1302 err = NULL;
1303 done = 1;
1304 } else if (a->commits_needed > 0)
1305 a->commits_needed--;
1307 errcode = pthread_mutex_lock(&tog_mutex);
1308 if (errcode)
1309 return (void *)got_error_set_errno(errcode);
1311 if (done)
1312 a->log_complete = 1;
1313 else if (*a->quit) {
1314 done = 1;
1315 a->log_complete = 1;
1316 } else if (*a->first_displayed_entry == NULL) {
1317 *a->first_displayed_entry =
1318 TAILQ_FIRST(&a->commits->head);
1319 *a->selected_entry = *a->first_displayed_entry;
1322 if (done)
1323 a->commits_needed = 0;
1324 else if (a->commits_needed == 0) {
1325 errcode = pthread_cond_wait(&a->need_commits,
1326 &tog_mutex);
1327 if (errcode)
1328 err = got_error_set_errno(errcode);
1331 errcode = pthread_mutex_unlock(&tog_mutex);
1332 if (errcode && err == NULL)
1333 err = got_error_set_errno(errcode);
1335 return (void *)err;
1338 static const struct got_error *
1339 stop_log_thread(struct tog_log_view_state *s)
1341 const struct got_error *err = NULL;
1342 int errcode;
1344 if (s->thread) {
1345 s->quit = 1;
1346 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1347 if (errcode)
1348 return got_error_set_errno(errcode);
1349 errcode = pthread_mutex_unlock(&tog_mutex);
1350 if (errcode)
1351 return got_error_set_errno(errcode);
1352 errcode = pthread_join(s->thread, (void **)&err);
1353 if (errcode)
1354 return got_error_set_errno(errcode);
1355 errcode = pthread_mutex_lock(&tog_mutex);
1356 if (errcode)
1357 return got_error_set_errno(errcode);
1358 s->thread = NULL;
1361 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1362 if (errcode && err == NULL)
1363 err = got_error_set_errno(errcode);
1365 if (s->thread_args.repo) {
1366 got_repo_close(s->thread_args.repo);
1367 s->thread_args.repo = NULL;
1370 if (s->thread_args.graph) {
1371 got_commit_graph_close(s->thread_args.graph);
1372 s->thread_args.graph = NULL;
1375 return err;
1378 static const struct got_error *
1379 close_log_view(struct tog_view *view)
1381 const struct got_error *err = NULL;
1382 struct tog_log_view_state *s = &view->state.log;
1384 err = stop_log_thread(s);
1385 free_commits(&s->commits);
1386 free(s->in_repo_path);
1387 s->in_repo_path = NULL;
1388 free(s->start_id);
1389 s->start_id = NULL;
1390 return err;
1393 static const struct got_error *
1394 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1395 struct got_repository *repo, const char *path, int check_disk)
1397 const struct got_error *err = NULL;
1398 struct tog_log_view_state *s = &view->state.log;
1399 struct got_repository *thread_repo = NULL;
1400 struct got_commit_graph *thread_graph = NULL;
1401 int errcode;
1403 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1404 if (err != NULL)
1405 goto done;
1407 /* The commit queue only contains commits being displayed. */
1408 TAILQ_INIT(&s->commits.head);
1409 s->commits.ncommits = 0;
1411 s->repo = repo;
1412 s->start_id = got_object_id_dup(start_id);
1413 if (s->start_id == NULL) {
1414 err = got_error_from_errno();
1415 goto done;
1418 view->show = show_log_view;
1419 view->input = input_log_view;
1420 view->close = close_log_view;
1422 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1423 if (err)
1424 goto done;
1425 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1426 0, thread_repo);
1427 if (err)
1428 goto done;
1430 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1431 if (errcode) {
1432 err = got_error_set_errno(errcode);
1433 goto done;
1436 s->thread_args.commits_needed = view->nlines;
1437 s->thread_args.graph = thread_graph;
1438 s->thread_args.commits = &s->commits;
1439 s->thread_args.in_repo_path = s->in_repo_path;
1440 s->thread_args.start_id = s->start_id;
1441 s->thread_args.repo = thread_repo;
1442 s->thread_args.log_complete = 0;
1443 s->thread_args.quit = &s->quit;
1444 s->thread_args.view = view;
1445 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1446 s->thread_args.selected_entry = &s->selected_entry;
1447 done:
1448 if (err)
1449 close_log_view(view);
1450 return err;
1453 static const struct got_error *
1454 show_log_view(struct tog_view *view)
1456 struct tog_log_view_state *s = &view->state.log;
1458 if (s->thread == NULL) {
1459 int errcode = pthread_create(&s->thread, NULL, log_thread,
1460 &s->thread_args);
1461 if (errcode)
1462 return got_error_set_errno(errcode);
1465 return draw_commits(view, &s->last_displayed_entry,
1466 &s->selected_entry, s->first_displayed_entry,
1467 &s->commits, s->selected, view->nlines,
1468 s->in_repo_path, s->thread_args.commits_needed);
1471 static const struct got_error *
1472 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1473 struct tog_view **focus_view, struct tog_view *view, int ch)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1477 char *parent_path;
1478 struct tog_view *diff_view = NULL, *tree_view = NULL;
1479 int begin_x = 0;
1481 switch (ch) {
1482 case 'q':
1483 s->quit = 1;
1484 break;
1485 case 'k':
1486 case KEY_UP:
1487 if (s->first_displayed_entry == NULL)
1488 break;
1489 if (s->selected > 0)
1490 s->selected--;
1491 if (s->selected > 0)
1492 break;
1493 scroll_up(&s->first_displayed_entry, 1,
1494 &s->commits);
1495 break;
1496 case KEY_PPAGE:
1497 if (s->first_displayed_entry == NULL)
1498 break;
1499 if (TAILQ_FIRST(&s->commits.head) ==
1500 s->first_displayed_entry) {
1501 s->selected = 0;
1502 break;
1504 scroll_up(&s->first_displayed_entry,
1505 view->nlines, &s->commits);
1506 break;
1507 case 'j':
1508 case KEY_DOWN:
1509 if (s->first_displayed_entry == NULL)
1510 break;
1511 if (s->selected < MIN(view->nlines - 2,
1512 s->commits.ncommits - 1)) {
1513 s->selected++;
1514 break;
1516 err = scroll_down(&s->first_displayed_entry, 1,
1517 &s->last_displayed_entry, &s->commits,
1518 &s->thread_args.log_complete,
1519 &s->thread_args.commits_needed,
1520 &s->thread_args.need_commits);
1521 break;
1522 case KEY_NPAGE: {
1523 struct commit_queue_entry *first;
1524 first = s->first_displayed_entry;
1525 if (first == NULL)
1526 break;
1527 err = scroll_down(&s->first_displayed_entry,
1528 view->nlines, &s->last_displayed_entry,
1529 &s->commits, &s->thread_args.log_complete,
1530 &s->thread_args.commits_needed,
1531 &s->thread_args.need_commits);
1532 if (first == s->first_displayed_entry &&
1533 s->selected < MIN(view->nlines - 2,
1534 s->commits.ncommits - 1)) {
1535 /* can't scroll further down */
1536 s->selected = MIN(view->nlines - 2,
1537 s->commits.ncommits - 1);
1539 err = NULL;
1540 break;
1542 case KEY_RESIZE:
1543 if (s->selected > view->nlines - 2)
1544 s->selected = view->nlines - 2;
1545 if (s->selected > s->commits.ncommits - 1)
1546 s->selected = s->commits.ncommits - 1;
1547 break;
1548 case KEY_ENTER:
1549 case '\r':
1550 if (s->selected_entry == NULL)
1551 break;
1552 if (view_is_parent_view(view))
1553 begin_x = view_split_begin_x(view->begin_x);
1554 err = open_diff_view_for_commit(&diff_view, begin_x,
1555 s->selected_entry, &s->commits,
1556 &s->thread_args.log_complete,
1557 &s->thread_args.need_commits,
1558 &s->thread_args.commits_needed, s->repo);
1559 if (err)
1560 break;
1561 if (view_is_parent_view(view)) {
1562 err = view_close_child(view);
1563 if (err)
1564 return err;
1565 err = view_set_child(view, diff_view);
1566 if (err) {
1567 view_close(diff_view);
1568 break;
1570 *focus_view = diff_view;
1571 view->child_focussed = 1;
1572 } else
1573 *new_view = diff_view;
1574 break;
1575 case 't':
1576 if (s->selected_entry == NULL)
1577 break;
1578 if (view_is_parent_view(view))
1579 begin_x = view_split_begin_x(view->begin_x);
1580 err = browse_commit(&tree_view, begin_x,
1581 s->selected_entry, s->repo);
1582 if (err)
1583 break;
1584 if (view_is_parent_view(view)) {
1585 err = view_close_child(view);
1586 if (err)
1587 return err;
1588 err = view_set_child(view, tree_view);
1589 if (err) {
1590 view_close(tree_view);
1591 break;
1593 *focus_view = tree_view;
1594 view->child_focussed = 1;
1595 } else
1596 *new_view = tree_view;
1597 break;
1598 case KEY_BACKSPACE:
1599 if (strcmp(s->in_repo_path, "/") == 0)
1600 break;
1601 parent_path = dirname(s->in_repo_path);
1602 if (parent_path && strcmp(parent_path, ".") != 0) {
1603 struct tog_view *lv;
1604 err = stop_log_thread(s);
1605 if (err)
1606 return err;
1607 lv = view_open(view->nlines, view->ncols,
1608 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1609 if (lv == NULL)
1610 return got_error_from_errno();
1611 err = open_log_view(lv, s->start_id, s->repo,
1612 parent_path, 0);
1613 if (err)
1614 return err;;
1615 if (view_is_parent_view(view))
1616 *new_view = lv;
1617 else {
1618 view_set_child(view->parent, lv);
1619 *focus_view = lv;
1621 return NULL;
1623 break;
1624 default:
1625 break;
1628 return err;
1631 static const struct got_error *
1632 apply_unveil(const char *repo_path, const char *worktree_path)
1634 const struct got_error *error;
1636 if (repo_path && unveil(repo_path, "r") != 0)
1637 return got_error_from_errno();
1639 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1640 return got_error_from_errno();
1642 if (unveil("/tmp", "rwc") != 0)
1643 return got_error_from_errno();
1645 error = got_privsep_unveil_exec_helpers();
1646 if (error != NULL)
1647 return error;
1649 if (unveil(NULL, NULL) != 0)
1650 return got_error_from_errno();
1652 return NULL;
1655 static void
1656 init_curses(void)
1658 initscr();
1659 cbreak();
1660 halfdelay(1); /* Do fast refresh while initial view is loading. */
1661 noecho();
1662 nonl();
1663 intrflush(stdscr, FALSE);
1664 keypad(stdscr, TRUE);
1665 curs_set(0);
1666 signal(SIGWINCH, tog_sigwinch);
1669 static const struct got_error *
1670 cmd_log(int argc, char *argv[])
1672 const struct got_error *error;
1673 struct got_repository *repo = NULL;
1674 struct got_object_id *start_id = NULL;
1675 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1676 char *start_commit = NULL;
1677 int ch;
1678 struct tog_view *view;
1680 #ifndef PROFILE
1681 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1682 NULL) == -1)
1683 err(1, "pledge");
1684 #endif
1686 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1687 switch (ch) {
1688 case 'c':
1689 start_commit = optarg;
1690 break;
1691 case 'r':
1692 repo_path = realpath(optarg, NULL);
1693 if (repo_path == NULL)
1694 err(1, "-r option");
1695 break;
1696 default:
1697 usage();
1698 /* NOTREACHED */
1702 argc -= optind;
1703 argv += optind;
1705 if (argc == 0)
1706 path = strdup("");
1707 else if (argc == 1)
1708 path = strdup(argv[0]);
1709 else
1710 usage_log();
1711 if (path == NULL)
1712 return got_error_from_errno();
1714 cwd = getcwd(NULL, 0);
1715 if (cwd == NULL) {
1716 error = got_error_from_errno();
1717 goto done;
1719 if (repo_path == NULL) {
1720 struct got_worktree *worktree;
1721 error = got_worktree_open(&worktree, cwd);
1722 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1723 goto done;
1724 if (worktree) {
1725 repo_path =
1726 strdup(got_worktree_get_repo_path(worktree));
1727 got_worktree_close(worktree);
1728 } else
1729 repo_path = strdup(cwd);
1730 if (repo_path == NULL) {
1731 error = got_error_from_errno();
1732 goto done;
1736 init_curses();
1738 error = apply_unveil(repo_path, NULL);
1739 if (error)
1740 goto done;
1742 error = got_repo_open(&repo, repo_path);
1743 if (error != NULL)
1744 goto done;
1746 if (start_commit == NULL)
1747 error = get_head_commit_id(&start_id, repo);
1748 else
1749 error = got_object_resolve_id_str(&start_id, repo,
1750 start_commit);
1751 if (error != NULL)
1752 goto done;
1754 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1755 if (view == NULL) {
1756 error = got_error_from_errno();
1757 goto done;
1759 error = open_log_view(view, start_id, repo, path, 1);
1760 if (error)
1761 goto done;
1762 error = view_loop(view);
1763 done:
1764 free(repo_path);
1765 free(cwd);
1766 free(path);
1767 free(start_id);
1768 if (repo)
1769 got_repo_close(repo);
1770 return error;
1773 __dead static void
1774 usage_diff(void)
1776 endwin();
1777 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1778 getprogname());
1779 exit(1);
1782 static char *
1783 parse_next_line(FILE *f, size_t *len)
1785 char *line;
1786 size_t linelen;
1787 size_t lineno;
1788 const char delim[3] = { '\0', '\0', '\0'};
1790 line = fparseln(f, &linelen, &lineno, delim, 0);
1791 if (len)
1792 *len = linelen;
1793 return line;
1796 static const struct got_error *
1797 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1798 int *last_displayed_line, int *eof, int max_lines,
1799 char * header)
1801 const struct got_error *err;
1802 int nlines = 0, nprinted = 0;
1803 char *line;
1804 size_t len;
1805 wchar_t *wline;
1806 int width;
1808 rewind(f);
1809 werase(view->window);
1811 if (header) {
1812 err = format_line(&wline, &width, header, view->ncols);
1813 if (err) {
1814 return err;
1817 if (view_needs_focus_indication(view))
1818 wstandout(view->window);
1819 waddwstr(view->window, wline);
1820 if (view_needs_focus_indication(view))
1821 wstandend(view->window);
1822 if (width < view->ncols)
1823 waddch(view->window, '\n');
1825 if (max_lines <= 1)
1826 return NULL;
1827 max_lines--;
1830 *eof = 0;
1831 while (nprinted < max_lines) {
1832 line = parse_next_line(f, &len);
1833 if (line == NULL) {
1834 *eof = 1;
1835 break;
1837 if (++nlines < *first_displayed_line) {
1838 free(line);
1839 continue;
1842 err = format_line(&wline, &width, line, view->ncols);
1843 if (err) {
1844 free(line);
1845 return err;
1847 waddwstr(view->window, wline);
1848 if (width < view->ncols)
1849 waddch(view->window, '\n');
1850 if (++nprinted == 1)
1851 *first_displayed_line = nlines;
1852 free(line);
1853 free(wline);
1854 wline = NULL;
1856 *last_displayed_line = nlines;
1858 view_vborder(view);
1860 return NULL;
1863 static char *
1864 get_datestr(time_t *time, char *datebuf)
1866 char *p, *s = ctime_r(time, datebuf);
1867 p = strchr(s, '\n');
1868 if (p)
1869 *p = '\0';
1870 return s;
1873 static const struct got_error *
1874 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1875 FILE *outfile)
1877 const struct got_error *err = NULL;
1878 char datebuf[26];
1879 struct got_commit_object *commit;
1880 char *id_str = NULL;
1881 time_t committer_time;
1882 const char *author, *committer;
1884 err = got_object_open_as_commit(&commit, repo, commit_id);
1885 if (err)
1886 return err;
1888 err = got_object_id_str(&id_str, commit_id);
1889 if (err) {
1890 err = got_error_from_errno();
1891 goto done;
1894 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1895 err = got_error_from_errno();
1896 goto done;
1898 if (fprintf(outfile, "from: %s\n",
1899 got_object_commit_get_author(commit)) < 0) {
1900 err = got_error_from_errno();
1901 goto done;
1903 committer_time = got_object_commit_get_committer_time(commit);
1904 if (fprintf(outfile, "date: %s UTC\n",
1905 get_datestr(&committer_time, datebuf)) < 0) {
1906 err = got_error_from_errno();
1907 goto done;
1909 author = got_object_commit_get_author(commit);
1910 committer = got_object_commit_get_committer(commit);
1911 if (strcmp(author, committer) != 0 &&
1912 fprintf(outfile, "via: %s\n", committer) < 0) {
1913 err = got_error_from_errno();
1914 goto done;
1916 if (fprintf(outfile, "%s\n",
1917 got_object_commit_get_logmsg(commit)) < 0) {
1918 err = got_error_from_errno();
1919 goto done;
1921 done:
1922 free(id_str);
1923 got_object_commit_close(commit);
1924 return err;
1927 static const struct got_error *
1928 create_diff(struct tog_diff_view_state *s)
1930 const struct got_error *err = NULL;
1931 FILE *f = NULL;
1932 int obj_type;
1934 f = got_opentemp();
1935 if (f == NULL) {
1936 err = got_error_from_errno();
1937 goto done;
1939 if (s->f && fclose(s->f) != 0) {
1940 err = got_error_from_errno();
1941 goto done;
1943 s->f = f;
1945 if (s->id1)
1946 err = got_object_get_type(&obj_type, s->repo, s->id1);
1947 else
1948 err = got_object_get_type(&obj_type, s->repo, s->id2);
1949 if (err)
1950 goto done;
1952 switch (obj_type) {
1953 case GOT_OBJ_TYPE_BLOB:
1954 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1955 s->diff_context, s->repo, f);
1956 break;
1957 case GOT_OBJ_TYPE_TREE:
1958 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1959 s->diff_context, s->repo, f);
1960 break;
1961 case GOT_OBJ_TYPE_COMMIT: {
1962 const struct got_object_id_queue *parent_ids;
1963 struct got_object_qid *pid;
1964 struct got_commit_object *commit2;
1966 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1967 if (err)
1968 break;
1969 /* Show commit info if we're diffing to a parent/root commit. */
1970 if (s->id1 == NULL)
1971 write_commit_info(s->id2, s->repo, f);
1972 else {
1973 parent_ids = got_object_commit_get_parent_ids(commit2);
1974 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1975 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1976 write_commit_info(s->id2, s->repo, f);
1977 break;
1981 got_object_commit_close(commit2);
1983 err = got_diff_objects_as_commits(s->id1, s->id2,
1984 s->diff_context, s->repo, f);
1985 break;
1987 default:
1988 err = got_error(GOT_ERR_OBJ_TYPE);
1989 break;
1991 done:
1992 if (f && fflush(f) != 0 && err == NULL)
1993 err = got_error_from_errno();
1994 return err;
1997 static const struct got_error *
1998 open_diff_view(struct tog_view *view, struct got_object_id *id1,
1999 struct got_object_id *id2, struct commit_queue_entry *selected_entry,
2000 struct commit_queue *commits, int *log_complete,
2001 pthread_cond_t *need_commits, int *commits_needed,
2002 struct got_repository *repo)
2004 const struct got_error *err;
2006 if (id1 != NULL && id2 != NULL) {
2007 int type1, type2;
2008 err = got_object_get_type(&type1, repo, id1);
2009 if (err)
2010 return err;
2011 err = got_object_get_type(&type2, repo, id2);
2012 if (err)
2013 return err;
2015 if (type1 != type2)
2016 return got_error(GOT_ERR_OBJ_TYPE);
2019 if (id1) {
2020 view->state.diff.id1 = got_object_id_dup(id1);
2021 if (view->state.diff.id1 == NULL)
2022 return got_error_from_errno();
2023 } else
2024 view->state.diff.id1 = NULL;
2026 view->state.diff.id2 = got_object_id_dup(id2);
2027 if (view->state.diff.id2 == NULL) {
2028 free(view->state.diff.id1);
2029 view->state.diff.id1 = NULL;
2030 return got_error_from_errno();
2032 view->state.diff.f = NULL;
2033 view->state.diff.first_displayed_line = 1;
2034 view->state.diff.last_displayed_line = view->nlines;
2035 view->state.diff.diff_context = 3;
2036 view->state.diff.selected_entry = selected_entry;
2037 view->state.diff.commits = commits;
2038 view->state.diff.log_complete = log_complete;
2039 view->state.diff.need_commits = need_commits;
2040 view->state.diff.commits_needed = commits_needed;
2041 view->state.diff.repo = repo;
2043 err = create_diff(&view->state.diff);
2044 if (err) {
2045 free(view->state.diff.id1);
2046 view->state.diff.id1 = NULL;
2047 free(view->state.diff.id2);
2048 view->state.diff.id2 = NULL;
2049 return err;
2052 view->show = show_diff_view;
2053 view->input = input_diff_view;
2054 view->close = close_diff_view;
2056 return NULL;
2059 static const struct got_error *
2060 close_diff_view(struct tog_view *view)
2062 const struct got_error *err = NULL;
2064 free(view->state.diff.id1);
2065 view->state.diff.id1 = NULL;
2066 free(view->state.diff.id2);
2067 view->state.diff.id2 = NULL;
2068 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2069 err = got_error_from_errno();
2070 return err;
2073 static const struct got_error *
2074 show_diff_view(struct tog_view *view)
2076 const struct got_error *err;
2077 struct tog_diff_view_state *s = &view->state.diff;
2078 char *id_str1 = NULL, *id_str2, *header;
2080 if (s->id1) {
2081 err = got_object_id_str(&id_str1, s->id1);
2082 if (err)
2083 return err;
2085 err = got_object_id_str(&id_str2, s->id2);
2086 if (err)
2087 return err;
2089 if (asprintf(&header, "diff %s %s",
2090 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2091 err = got_error_from_errno();
2092 free(id_str1);
2093 free(id_str2);
2094 return err;
2096 free(id_str1);
2097 free(id_str2);
2099 return draw_file(view, s->f, &s->first_displayed_line,
2100 &s->last_displayed_line, &s->eof, view->nlines,
2101 header);
2104 static const struct got_error *
2105 set_selected_commit(struct tog_diff_view_state *s,
2106 struct commit_queue_entry *entry)
2108 struct commit_queue_entry *pentry;
2110 free(s->id2);
2111 s->id2 = got_object_id_dup(entry->id);
2112 if (s->id2 == NULL)
2113 return got_error_from_errno();
2115 free(s->id1);
2116 s->id1 = NULL;
2117 pentry = TAILQ_NEXT(entry, entry);
2118 if (pentry) {
2119 s->id1 = got_object_id_dup(pentry->id);
2120 if (s->id1 == NULL)
2121 return got_error_from_errno();
2124 s->selected_entry = entry;
2125 return NULL;
2128 static const struct got_error *
2129 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2130 struct tog_view **focus_view, struct tog_view *view, int ch)
2132 const struct got_error *err = NULL;
2133 struct tog_diff_view_state *s = &view->state.diff;
2134 struct commit_queue_entry *entry, *pentry;
2135 int i;
2137 switch (ch) {
2138 case 'k':
2139 case KEY_UP:
2140 if (s->first_displayed_line > 1)
2141 s->first_displayed_line--;
2142 break;
2143 case KEY_PPAGE:
2144 i = 0;
2145 while (i++ < view->nlines - 1 &&
2146 s->first_displayed_line > 1)
2147 s->first_displayed_line--;
2148 break;
2149 case 'j':
2150 case KEY_DOWN:
2151 if (!s->eof)
2152 s->first_displayed_line++;
2153 break;
2154 case KEY_NPAGE:
2155 case ' ':
2156 i = 0;
2157 while (!s->eof && i++ < view->nlines - 1) {
2158 char *line;
2159 line = parse_next_line(s->f, NULL);
2160 s->first_displayed_line++;
2161 if (line == NULL)
2162 break;
2164 break;
2165 case '[':
2166 if (s->diff_context > 0) {
2167 s->diff_context--;
2168 err = create_diff(s);
2170 break;
2171 case ']':
2172 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2173 s->diff_context++;
2174 err = create_diff(s);
2176 break;
2177 case '<':
2178 case ',':
2179 if (s->commits == NULL)
2180 break;
2181 entry = TAILQ_PREV(s->selected_entry,
2182 commit_queue_head, entry);
2183 if (entry == NULL)
2184 break;
2186 err = set_selected_commit(s, entry);
2187 if (err)
2188 break;
2190 s->first_displayed_line = 1;
2191 s->last_displayed_line = view->nlines;
2193 err = create_diff(s);
2194 break;
2195 case '>':
2196 case '.':
2197 if (s->commits == NULL)
2198 break;
2199 entry = TAILQ_NEXT(s->selected_entry, entry);
2200 if (entry)
2201 pentry = TAILQ_NEXT(entry, entry);
2202 if (entry == NULL || pentry == NULL) {
2203 if (!*s->log_complete)
2204 *s->commits_needed = 2;
2206 while (entry == NULL || pentry == NULL) {
2207 int errcode;
2208 if (*s->log_complete)
2209 break;
2210 errcode = pthread_cond_signal(s->need_commits);
2211 if (errcode)
2212 return got_error_set_errno(errcode);
2213 errcode = pthread_mutex_unlock(&tog_mutex);
2214 if (errcode)
2215 return got_error_set_errno(errcode);
2216 pthread_yield();
2217 errcode = pthread_mutex_lock(&tog_mutex);
2218 if (errcode)
2219 return got_error_set_errno(errcode);
2220 entry = TAILQ_NEXT(s->selected_entry, entry);
2221 if (entry)
2222 pentry = TAILQ_NEXT(entry, entry);
2225 if (entry == NULL)
2226 break;
2228 err = set_selected_commit(s, entry);
2229 if (err)
2230 break;
2232 s->first_displayed_line = 1;
2233 s->last_displayed_line = view->nlines;
2235 err = create_diff(s);
2236 break;
2237 default:
2238 break;
2241 return err;
2244 static const struct got_error *
2245 cmd_diff(int argc, char *argv[])
2247 const struct got_error *error = NULL;
2248 struct got_repository *repo = NULL;
2249 struct got_object_id *id1 = NULL, *id2 = NULL;
2250 char *repo_path = NULL;
2251 char *id_str1 = NULL, *id_str2 = NULL;
2252 int ch;
2253 struct tog_view *view;
2255 #ifndef PROFILE
2256 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2257 NULL) == -1)
2258 err(1, "pledge");
2259 #endif
2261 while ((ch = getopt(argc, argv, "")) != -1) {
2262 switch (ch) {
2263 default:
2264 usage();
2265 /* NOTREACHED */
2269 argc -= optind;
2270 argv += optind;
2272 if (argc == 0) {
2273 usage_diff(); /* TODO show local worktree changes */
2274 } else if (argc == 2) {
2275 repo_path = getcwd(NULL, 0);
2276 if (repo_path == NULL)
2277 return got_error_from_errno();
2278 id_str1 = argv[0];
2279 id_str2 = argv[1];
2280 } else if (argc == 3) {
2281 repo_path = realpath(argv[0], NULL);
2282 if (repo_path == NULL)
2283 return got_error_from_errno();
2284 id_str1 = argv[1];
2285 id_str2 = argv[2];
2286 } else
2287 usage_diff();
2289 init_curses();
2291 error = apply_unveil(repo_path, NULL);
2292 if (error)
2293 goto done;
2295 error = got_repo_open(&repo, repo_path);
2296 free(repo_path);
2297 if (error)
2298 goto done;
2300 error = got_object_resolve_id_str(&id1, repo, id_str1);
2301 if (error)
2302 goto done;
2304 error = got_object_resolve_id_str(&id2, repo, id_str2);
2305 if (error)
2306 goto done;
2308 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2309 if (view == NULL) {
2310 error = got_error_from_errno();
2311 goto done;
2313 error = open_diff_view(view, id1, id2, NULL, NULL, NULL, NULL, NULL,
2314 repo);
2315 if (error)
2316 goto done;
2317 error = view_loop(view);
2318 done:
2319 got_repo_close(repo);
2320 return error;
2323 __dead static void
2324 usage_blame(void)
2326 endwin();
2327 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2328 getprogname());
2329 exit(1);
2332 struct tog_blame_line {
2333 int annotated;
2334 struct got_object_id *id;
2337 static const struct got_error *
2338 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2339 const char *path, struct tog_blame_line *lines, int nlines,
2340 int blame_complete, int selected_line, int *first_displayed_line,
2341 int *last_displayed_line, int *eof, int max_lines)
2343 const struct got_error *err;
2344 int lineno = 0, nprinted = 0;
2345 char *line;
2346 size_t len;
2347 wchar_t *wline;
2348 int width, wlimit;
2349 struct tog_blame_line *blame_line;
2350 struct got_object_id *prev_id = NULL;
2351 char *id_str;
2353 err = got_object_id_str(&id_str, id);
2354 if (err)
2355 return err;
2357 rewind(f);
2358 werase(view->window);
2360 if (asprintf(&line, "commit %s", id_str) == -1) {
2361 err = got_error_from_errno();
2362 free(id_str);
2363 return err;
2366 err = format_line(&wline, &width, line, view->ncols);
2367 free(line);
2368 line = NULL;
2369 if (view_needs_focus_indication(view))
2370 wstandout(view->window);
2371 waddwstr(view->window, wline);
2372 if (view_needs_focus_indication(view))
2373 wstandend(view->window);
2374 free(wline);
2375 wline = NULL;
2376 if (width < view->ncols)
2377 waddch(view->window, '\n');
2379 if (asprintf(&line, "[%d/%d] %s%s",
2380 *first_displayed_line - 1 + selected_line, nlines,
2381 blame_complete ? "" : "annotating ", path) == -1) {
2382 free(id_str);
2383 return got_error_from_errno();
2385 free(id_str);
2386 err = format_line(&wline, &width, line, view->ncols);
2387 free(line);
2388 line = NULL;
2389 if (err)
2390 return err;
2391 waddwstr(view->window, wline);
2392 free(wline);
2393 wline = NULL;
2394 if (width < view->ncols)
2395 waddch(view->window, '\n');
2397 *eof = 0;
2398 while (nprinted < max_lines - 2) {
2399 line = parse_next_line(f, &len);
2400 if (line == NULL) {
2401 *eof = 1;
2402 break;
2404 if (++lineno < *first_displayed_line) {
2405 free(line);
2406 continue;
2409 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2410 err = format_line(&wline, &width, line, wlimit);
2411 if (err) {
2412 free(line);
2413 return err;
2416 if (view->focussed && nprinted == selected_line - 1)
2417 wstandout(view->window);
2419 blame_line = &lines[lineno - 1];
2420 if (blame_line->annotated && prev_id &&
2421 got_object_id_cmp(prev_id, blame_line->id) == 0)
2422 waddstr(view->window, " ");
2423 else if (blame_line->annotated) {
2424 char *id_str;
2425 err = got_object_id_str(&id_str, blame_line->id);
2426 if (err) {
2427 free(line);
2428 free(wline);
2429 return err;
2431 wprintw(view->window, "%.8s ", id_str);
2432 free(id_str);
2433 prev_id = blame_line->id;
2434 } else {
2435 waddstr(view->window, "........ ");
2436 prev_id = NULL;
2439 waddwstr(view->window, wline);
2440 while (width < wlimit) {
2441 waddch(view->window, ' ');
2442 width++;
2444 if (view->focussed && nprinted == selected_line - 1)
2445 wstandend(view->window);
2446 if (++nprinted == 1)
2447 *first_displayed_line = lineno;
2448 free(line);
2449 free(wline);
2450 wline = NULL;
2452 *last_displayed_line = lineno;
2454 view_vborder(view);
2456 return NULL;
2459 static const struct got_error *
2460 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2462 const struct got_error *err = NULL;
2463 struct tog_blame_cb_args *a = arg;
2464 struct tog_blame_line *line;
2465 int errcode;
2467 if (nlines != a->nlines ||
2468 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2469 return got_error(GOT_ERR_RANGE);
2471 errcode = pthread_mutex_lock(&tog_mutex);
2472 if (errcode)
2473 return got_error_set_errno(errcode);
2475 if (*a->quit) { /* user has quit the blame view */
2476 err = got_error(GOT_ERR_ITER_COMPLETED);
2477 goto done;
2480 if (lineno == -1)
2481 goto done; /* no change in this commit */
2483 line = &a->lines[lineno - 1];
2484 if (line->annotated)
2485 goto done;
2487 line->id = got_object_id_dup(id);
2488 if (line->id == NULL) {
2489 err = got_error_from_errno();
2490 goto done;
2492 line->annotated = 1;
2493 done:
2494 errcode = pthread_mutex_unlock(&tog_mutex);
2495 if (errcode)
2496 err = got_error_set_errno(errcode);
2497 return err;
2500 static void *
2501 blame_thread(void *arg)
2503 const struct got_error *err;
2504 struct tog_blame_thread_args *ta = arg;
2505 struct tog_blame_cb_args *a = ta->cb_args;
2506 int errcode;
2508 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2509 blame_cb, ta->cb_args);
2511 errcode = pthread_mutex_lock(&tog_mutex);
2512 if (errcode)
2513 return (void *)got_error_set_errno(errcode);
2515 got_repo_close(ta->repo);
2516 ta->repo = NULL;
2517 *ta->complete = 1;
2519 errcode = pthread_mutex_unlock(&tog_mutex);
2520 if (errcode && err == NULL)
2521 err = got_error_set_errno(errcode);
2523 return (void *)err;
2526 static struct got_object_id *
2527 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2528 int selected_line)
2530 struct tog_blame_line *line;
2532 line = &lines[first_displayed_line - 1 + selected_line - 1];
2533 if (!line->annotated)
2534 return NULL;
2536 return line->id;
2539 static const struct got_error *
2540 stop_blame(struct tog_blame *blame)
2542 const struct got_error *err = NULL;
2543 int i;
2545 if (blame->thread) {
2546 int errcode;
2547 errcode = pthread_mutex_unlock(&tog_mutex);
2548 if (errcode)
2549 return got_error_set_errno(errcode);
2550 errcode = pthread_join(blame->thread, (void **)&err);
2551 if (errcode)
2552 return got_error_set_errno(errcode);
2553 errcode = pthread_mutex_lock(&tog_mutex);
2554 if (errcode)
2555 return got_error_set_errno(errcode);
2556 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2557 err = NULL;
2558 blame->thread = NULL;
2560 if (blame->thread_args.repo) {
2561 got_repo_close(blame->thread_args.repo);
2562 blame->thread_args.repo = NULL;
2564 if (blame->f) {
2565 if (fclose(blame->f) != 0 && err == NULL)
2566 err = got_error_from_errno();
2567 blame->f = NULL;
2569 if (blame->lines) {
2570 for (i = 0; i < blame->nlines; i++)
2571 free(blame->lines[i].id);
2572 free(blame->lines);
2573 blame->lines = NULL;
2575 free(blame->cb_args.commit_id);
2576 blame->cb_args.commit_id = NULL;
2578 return err;
2581 static const struct got_error *
2582 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2583 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2584 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2585 struct got_repository *repo)
2587 const struct got_error *err = NULL;
2588 struct got_blob_object *blob = NULL;
2589 struct got_repository *thread_repo = NULL;
2590 struct got_object_id *obj_id = NULL;
2591 int obj_type;
2593 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2594 if (err)
2595 return err;
2596 if (obj_id == NULL)
2597 return got_error(GOT_ERR_NO_OBJ);
2599 err = got_object_get_type(&obj_type, repo, obj_id);
2600 if (err)
2601 goto done;
2603 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2604 err = got_error(GOT_ERR_OBJ_TYPE);
2605 goto done;
2608 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2609 if (err)
2610 goto done;
2611 blame->f = got_opentemp();
2612 if (blame->f == NULL) {
2613 err = got_error_from_errno();
2614 goto done;
2616 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2617 blame->f, blob);
2618 if (err)
2619 goto done;
2621 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2622 if (blame->lines == NULL) {
2623 err = got_error_from_errno();
2624 goto done;
2627 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2628 if (err)
2629 goto done;
2631 blame->cb_args.view = view;
2632 blame->cb_args.lines = blame->lines;
2633 blame->cb_args.nlines = blame->nlines;
2634 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2635 if (blame->cb_args.commit_id == NULL) {
2636 err = got_error_from_errno();
2637 goto done;
2639 blame->cb_args.quit = done;
2641 blame->thread_args.path = path;
2642 blame->thread_args.repo = thread_repo;
2643 blame->thread_args.cb_args = &blame->cb_args;
2644 blame->thread_args.complete = blame_complete;
2645 *blame_complete = 0;
2647 done:
2648 if (blob)
2649 got_object_blob_close(blob);
2650 free(obj_id);
2651 if (err)
2652 stop_blame(blame);
2653 return err;
2656 static const struct got_error *
2657 open_blame_view(struct tog_view *view, char *path,
2658 struct got_object_id *commit_id, struct got_repository *repo)
2660 const struct got_error *err = NULL;
2661 struct tog_blame_view_state *s = &view->state.blame;
2663 SIMPLEQ_INIT(&s->blamed_commits);
2665 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2666 if (err)
2667 return err;
2669 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2670 s->first_displayed_line = 1;
2671 s->last_displayed_line = view->nlines;
2672 s->selected_line = 1;
2673 s->blame_complete = 0;
2674 s->path = path;
2675 if (s->path == NULL)
2676 return got_error_from_errno();
2677 s->repo = repo;
2678 s->commit_id = commit_id;
2679 memset(&s->blame, 0, sizeof(s->blame));
2681 view->show = show_blame_view;
2682 view->input = input_blame_view;
2683 view->close = close_blame_view;
2685 return run_blame(&s->blame, view, &s->blame_complete,
2686 &s->first_displayed_line, &s->last_displayed_line,
2687 &s->selected_line, &s->done, &s->eof, s->path,
2688 s->blamed_commit->id, s->repo);
2691 static const struct got_error *
2692 close_blame_view(struct tog_view *view)
2694 const struct got_error *err = NULL;
2695 struct tog_blame_view_state *s = &view->state.blame;
2697 if (s->blame.thread)
2698 err = stop_blame(&s->blame);
2700 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2701 struct got_object_qid *blamed_commit;
2702 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2703 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2704 got_object_qid_free(blamed_commit);
2707 free(s->path);
2709 return err;
2712 static const struct got_error *
2713 show_blame_view(struct tog_view *view)
2715 const struct got_error *err = NULL;
2716 struct tog_blame_view_state *s = &view->state.blame;
2717 int errcode;
2719 if (s->blame.thread == NULL) {
2720 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2721 &s->blame.thread_args);
2722 if (errcode)
2723 return got_error_set_errno(errcode);
2726 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2727 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2728 s->selected_line, &s->first_displayed_line,
2729 &s->last_displayed_line, &s->eof, view->nlines);
2731 view_vborder(view);
2732 return err;
2735 static const struct got_error *
2736 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2737 struct tog_view **focus_view, struct tog_view *view, int ch)
2739 const struct got_error *err = NULL, *thread_err = NULL;
2740 struct tog_view *diff_view;
2741 struct tog_blame_view_state *s = &view->state.blame;
2742 int begin_x = 0;
2744 switch (ch) {
2745 case 'q':
2746 s->done = 1;
2747 break;
2748 case 'k':
2749 case KEY_UP:
2750 if (s->selected_line > 1)
2751 s->selected_line--;
2752 else if (s->selected_line == 1 &&
2753 s->first_displayed_line > 1)
2754 s->first_displayed_line--;
2755 break;
2756 case KEY_PPAGE:
2757 if (s->first_displayed_line == 1) {
2758 s->selected_line = 1;
2759 break;
2761 if (s->first_displayed_line > view->nlines - 2)
2762 s->first_displayed_line -=
2763 (view->nlines - 2);
2764 else
2765 s->first_displayed_line = 1;
2766 break;
2767 case 'j':
2768 case KEY_DOWN:
2769 if (s->selected_line < view->nlines - 2 &&
2770 s->first_displayed_line +
2771 s->selected_line <= s->blame.nlines)
2772 s->selected_line++;
2773 else if (s->last_displayed_line <
2774 s->blame.nlines)
2775 s->first_displayed_line++;
2776 break;
2777 case 'b':
2778 case 'p': {
2779 struct got_object_id *id = NULL;
2780 id = get_selected_commit_id(s->blame.lines,
2781 s->first_displayed_line, s->selected_line);
2782 if (id == NULL)
2783 break;
2784 if (ch == 'p') {
2785 struct got_commit_object *commit;
2786 struct got_object_qid *pid;
2787 struct got_object_id *blob_id = NULL;
2788 int obj_type;
2789 err = got_object_open_as_commit(&commit,
2790 s->repo, id);
2791 if (err)
2792 break;
2793 pid = SIMPLEQ_FIRST(
2794 got_object_commit_get_parent_ids(commit));
2795 if (pid == NULL) {
2796 got_object_commit_close(commit);
2797 break;
2799 /* Check if path history ends here. */
2800 err = got_object_id_by_path(&blob_id, s->repo,
2801 pid->id, s->path);
2802 if (err) {
2803 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2804 err = NULL;
2805 got_object_commit_close(commit);
2806 break;
2808 err = got_object_get_type(&obj_type, s->repo,
2809 blob_id);
2810 free(blob_id);
2811 /* Can't blame non-blob type objects. */
2812 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2813 got_object_commit_close(commit);
2814 break;
2816 err = got_object_qid_alloc(&s->blamed_commit,
2817 pid->id);
2818 got_object_commit_close(commit);
2819 } else {
2820 if (got_object_id_cmp(id,
2821 s->blamed_commit->id) == 0)
2822 break;
2823 err = got_object_qid_alloc(&s->blamed_commit,
2824 id);
2826 if (err)
2827 break;
2828 s->done = 1;
2829 thread_err = stop_blame(&s->blame);
2830 s->done = 0;
2831 if (thread_err)
2832 break;
2833 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2834 s->blamed_commit, entry);
2835 err = run_blame(&s->blame, view, &s->blame_complete,
2836 &s->first_displayed_line, &s->last_displayed_line,
2837 &s->selected_line, &s->done, &s->eof,
2838 s->path, s->blamed_commit->id, s->repo);
2839 if (err)
2840 break;
2841 break;
2843 case 'B': {
2844 struct got_object_qid *first;
2845 first = SIMPLEQ_FIRST(&s->blamed_commits);
2846 if (!got_object_id_cmp(first->id, s->commit_id))
2847 break;
2848 s->done = 1;
2849 thread_err = stop_blame(&s->blame);
2850 s->done = 0;
2851 if (thread_err)
2852 break;
2853 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2854 got_object_qid_free(s->blamed_commit);
2855 s->blamed_commit =
2856 SIMPLEQ_FIRST(&s->blamed_commits);
2857 err = run_blame(&s->blame, view, &s->blame_complete,
2858 &s->first_displayed_line, &s->last_displayed_line,
2859 &s->selected_line, &s->done, &s->eof, s->path,
2860 s->blamed_commit->id, s->repo);
2861 if (err)
2862 break;
2863 break;
2865 case KEY_ENTER:
2866 case '\r': {
2867 struct got_object_id *id = NULL;
2868 struct got_object_qid *pid;
2869 struct got_commit_object *commit = NULL;
2870 id = get_selected_commit_id(s->blame.lines,
2871 s->first_displayed_line, s->selected_line);
2872 if (id == NULL)
2873 break;
2874 err = got_object_open_as_commit(&commit, s->repo, id);
2875 if (err)
2876 break;
2877 pid = SIMPLEQ_FIRST(
2878 got_object_commit_get_parent_ids(commit));
2879 if (view_is_parent_view(view))
2880 begin_x = view_split_begin_x(view->begin_x);
2881 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2882 if (diff_view == NULL) {
2883 got_object_commit_close(commit);
2884 err = got_error_from_errno();
2885 break;
2887 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2888 id, NULL, NULL, NULL, NULL, NULL, s->repo);
2889 got_object_commit_close(commit);
2890 if (err) {
2891 view_close(diff_view);
2892 break;
2894 if (view_is_parent_view(view)) {
2895 err = view_close_child(view);
2896 if (err)
2897 break;
2898 err = view_set_child(view, diff_view);
2899 if (err) {
2900 view_close(diff_view);
2901 break;
2903 *focus_view = diff_view;
2904 view->child_focussed = 1;
2905 } else
2906 *new_view = diff_view;
2907 if (err)
2908 break;
2909 break;
2911 case KEY_NPAGE:
2912 case ' ':
2913 if (s->last_displayed_line >= s->blame.nlines &&
2914 s->selected_line < view->nlines - 2) {
2915 s->selected_line = MIN(s->blame.nlines,
2916 view->nlines - 2);
2917 break;
2919 if (s->last_displayed_line + view->nlines - 2
2920 <= s->blame.nlines)
2921 s->first_displayed_line +=
2922 view->nlines - 2;
2923 else
2924 s->first_displayed_line =
2925 s->blame.nlines -
2926 (view->nlines - 3);
2927 break;
2928 case KEY_RESIZE:
2929 if (s->selected_line > view->nlines - 2) {
2930 s->selected_line = MIN(s->blame.nlines,
2931 view->nlines - 2);
2933 break;
2934 default:
2935 break;
2937 return thread_err ? thread_err : err;
2940 static const struct got_error *
2941 cmd_blame(int argc, char *argv[])
2943 const struct got_error *error;
2944 struct got_repository *repo = NULL;
2945 struct got_worktree *worktree = NULL;
2946 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2947 struct got_object_id *commit_id = NULL;
2948 char *commit_id_str = NULL;
2949 int ch;
2950 struct tog_view *view;
2952 #ifndef PROFILE
2953 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2954 NULL) == -1)
2955 err(1, "pledge");
2956 #endif
2958 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2959 switch (ch) {
2960 case 'c':
2961 commit_id_str = optarg;
2962 break;
2963 case 'r':
2964 repo_path = realpath(optarg, NULL);
2965 if (repo_path == NULL)
2966 err(1, "-r option");
2967 break;
2968 default:
2969 usage();
2970 /* NOTREACHED */
2974 argc -= optind;
2975 argv += optind;
2977 if (argc == 1)
2978 path = argv[0];
2979 else
2980 usage_blame();
2982 cwd = getcwd(NULL, 0);
2983 if (cwd == NULL) {
2984 error = got_error_from_errno();
2985 goto done;
2987 if (repo_path == NULL) {
2988 error = got_worktree_open(&worktree, cwd);
2989 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2990 goto done;
2991 else
2992 error = NULL;
2993 if (worktree) {
2994 repo_path =
2995 strdup(got_worktree_get_repo_path(worktree));
2996 if (repo_path == NULL)
2997 error = got_error_from_errno();
2998 if (error)
2999 goto done;
3000 } else {
3001 repo_path = strdup(cwd);
3002 if (repo_path == NULL) {
3003 error = got_error_from_errno();
3004 goto done;
3009 init_curses();
3011 error = apply_unveil(repo_path, NULL);
3012 if (error)
3013 goto done;
3015 error = got_repo_open(&repo, repo_path);
3016 if (error != NULL)
3017 goto done;
3019 if (worktree) {
3020 const char *prefix = got_worktree_get_path_prefix(worktree);
3021 char *p, *worktree_subdir = cwd +
3022 strlen(got_worktree_get_root_path(worktree));
3023 if (asprintf(&p, "%s%s%s%s%s",
3024 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3025 worktree_subdir, worktree_subdir[0] ? "/" : "",
3026 path) == -1) {
3027 error = got_error_from_errno();
3028 goto done;
3030 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3031 free(p);
3032 } else {
3033 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3035 if (error)
3036 goto done;
3038 if (commit_id_str == NULL) {
3039 struct got_reference *head_ref;
3040 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3041 if (error != NULL)
3042 goto done;
3043 error = got_ref_resolve(&commit_id, repo, head_ref);
3044 got_ref_close(head_ref);
3045 } else {
3046 error = got_object_resolve_id_str(&commit_id, repo,
3047 commit_id_str);
3049 if (error != NULL)
3050 goto done;
3052 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3053 if (view == NULL) {
3054 error = got_error_from_errno();
3055 goto done;
3057 error = open_blame_view(view, in_repo_path, commit_id, repo);
3058 if (error)
3059 goto done;
3060 error = view_loop(view);
3061 done:
3062 free(repo_path);
3063 free(cwd);
3064 free(commit_id);
3065 if (worktree)
3066 got_worktree_close(worktree);
3067 if (repo)
3068 got_repo_close(repo);
3069 return error;
3072 static const struct got_error *
3073 draw_tree_entries(struct tog_view *view,
3074 struct got_tree_entry **first_displayed_entry,
3075 struct got_tree_entry **last_displayed_entry,
3076 struct got_tree_entry **selected_entry, int *ndisplayed,
3077 const char *label, int show_ids, const char *parent_path,
3078 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3080 const struct got_error *err = NULL;
3081 struct got_tree_entry *te;
3082 wchar_t *wline;
3083 int width, n;
3085 *ndisplayed = 0;
3087 werase(view->window);
3089 if (limit == 0)
3090 return NULL;
3092 err = format_line(&wline, &width, label, view->ncols);
3093 if (err)
3094 return err;
3095 if (view_needs_focus_indication(view))
3096 wstandout(view->window);
3097 waddwstr(view->window, wline);
3098 if (view_needs_focus_indication(view))
3099 wstandend(view->window);
3100 free(wline);
3101 wline = NULL;
3102 if (width < view->ncols)
3103 waddch(view->window, '\n');
3104 if (--limit <= 0)
3105 return NULL;
3106 err = format_line(&wline, &width, parent_path, view->ncols);
3107 if (err)
3108 return err;
3109 waddwstr(view->window, wline);
3110 free(wline);
3111 wline = NULL;
3112 if (width < view->ncols)
3113 waddch(view->window, '\n');
3114 if (--limit <= 0)
3115 return NULL;
3116 waddch(view->window, '\n');
3117 if (--limit <= 0)
3118 return NULL;
3120 te = SIMPLEQ_FIRST(&entries->head);
3121 if (*first_displayed_entry == NULL) {
3122 if (selected == 0) {
3123 if (view->focussed)
3124 wstandout(view->window);
3125 *selected_entry = NULL;
3127 waddstr(view->window, " ..\n"); /* parent directory */
3128 if (selected == 0 && view->focussed)
3129 wstandend(view->window);
3130 (*ndisplayed)++;
3131 if (--limit <= 0)
3132 return NULL;
3133 n = 1;
3134 } else {
3135 n = 0;
3136 while (te != *first_displayed_entry)
3137 te = SIMPLEQ_NEXT(te, entry);
3140 while (te) {
3141 char *line = NULL, *id_str = NULL;
3143 if (show_ids) {
3144 err = got_object_id_str(&id_str, te->id);
3145 if (err)
3146 return got_error_from_errno();
3148 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3149 te->name, S_ISDIR(te->mode) ? "/" :
3150 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3151 free(id_str);
3152 return got_error_from_errno();
3154 free(id_str);
3155 err = format_line(&wline, &width, line, view->ncols);
3156 if (err) {
3157 free(line);
3158 break;
3160 if (n == selected) {
3161 if (view->focussed)
3162 wstandout(view->window);
3163 *selected_entry = te;
3165 waddwstr(view->window, wline);
3166 if (width < view->ncols)
3167 waddch(view->window, '\n');
3168 if (n == selected && view->focussed)
3169 wstandend(view->window);
3170 free(line);
3171 free(wline);
3172 wline = NULL;
3173 n++;
3174 (*ndisplayed)++;
3175 *last_displayed_entry = te;
3176 if (--limit <= 0)
3177 break;
3178 te = SIMPLEQ_NEXT(te, entry);
3181 return err;
3184 static void
3185 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3186 const struct got_tree_entries *entries, int isroot)
3188 struct got_tree_entry *te, *prev;
3189 int i;
3191 if (*first_displayed_entry == NULL)
3192 return;
3194 te = SIMPLEQ_FIRST(&entries->head);
3195 if (*first_displayed_entry == te) {
3196 if (!isroot)
3197 *first_displayed_entry = NULL;
3198 return;
3201 /* XXX this is stupid... switch to TAILQ? */
3202 for (i = 0; i < maxscroll; i++) {
3203 while (te != *first_displayed_entry) {
3204 prev = te;
3205 te = SIMPLEQ_NEXT(te, entry);
3207 *first_displayed_entry = prev;
3208 te = SIMPLEQ_FIRST(&entries->head);
3210 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3211 *first_displayed_entry = NULL;
3214 static int
3215 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3216 struct got_tree_entry *last_displayed_entry,
3217 const struct got_tree_entries *entries)
3219 struct got_tree_entry *next, *last;
3220 int n = 0;
3222 if (*first_displayed_entry)
3223 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3224 else
3225 next = SIMPLEQ_FIRST(&entries->head);
3226 last = last_displayed_entry;
3227 while (next && last && n++ < maxscroll) {
3228 last = SIMPLEQ_NEXT(last, entry);
3229 if (last) {
3230 *first_displayed_entry = next;
3231 next = SIMPLEQ_NEXT(next, entry);
3234 return n;
3237 static const struct got_error *
3238 tree_entry_path(char **path, struct tog_parent_trees *parents,
3239 struct got_tree_entry *te)
3241 const struct got_error *err = NULL;
3242 struct tog_parent_tree *pt;
3243 size_t len = 2; /* for leading slash and NUL */
3245 TAILQ_FOREACH(pt, parents, entry)
3246 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3247 if (te)
3248 len += strlen(te->name);
3250 *path = calloc(1, len);
3251 if (path == NULL)
3252 return got_error_from_errno();
3254 (*path)[0] = '/';
3255 pt = TAILQ_LAST(parents, tog_parent_trees);
3256 while (pt) {
3257 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3258 err = got_error(GOT_ERR_NO_SPACE);
3259 goto done;
3261 if (strlcat(*path, "/", len) >= len) {
3262 err = got_error(GOT_ERR_NO_SPACE);
3263 goto done;
3265 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3267 if (te) {
3268 if (strlcat(*path, te->name, len) >= len) {
3269 err = got_error(GOT_ERR_NO_SPACE);
3270 goto done;
3273 done:
3274 if (err) {
3275 free(*path);
3276 *path = NULL;
3278 return err;
3281 static const struct got_error *
3282 blame_tree_entry(struct tog_view **new_view, int begin_x,
3283 struct got_tree_entry *te, struct tog_parent_trees *parents,
3284 struct got_object_id *commit_id, struct got_repository *repo)
3286 const struct got_error *err = NULL;
3287 char *path;
3288 struct tog_view *blame_view;
3290 err = tree_entry_path(&path, parents, te);
3291 if (err)
3292 return err;
3294 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3295 if (blame_view == NULL)
3296 return got_error_from_errno();
3298 err = open_blame_view(blame_view, path, commit_id, repo);
3299 if (err) {
3300 view_close(blame_view);
3301 free(path);
3302 } else
3303 *new_view = blame_view;
3304 return err;
3307 static const struct got_error *
3308 log_tree_entry(struct tog_view **new_view, int begin_x,
3309 struct got_tree_entry *te, struct tog_parent_trees *parents,
3310 struct got_object_id *commit_id, struct got_repository *repo)
3312 struct tog_view *log_view;
3313 const struct got_error *err = NULL;
3314 char *path;
3316 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3317 if (log_view == NULL)
3318 return got_error_from_errno();
3320 err = tree_entry_path(&path, parents, te);
3321 if (err)
3322 return err;
3324 err = open_log_view(log_view, commit_id, repo, path, 0);
3325 if (err)
3326 view_close(log_view);
3327 else
3328 *new_view = log_view;
3329 free(path);
3330 return err;
3333 static const struct got_error *
3334 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3335 struct got_object_id *commit_id, struct got_repository *repo)
3337 const struct got_error *err = NULL;
3338 char *commit_id_str = NULL;
3339 struct tog_tree_view_state *s = &view->state.tree;
3341 TAILQ_INIT(&s->parents);
3343 err = got_object_id_str(&commit_id_str, commit_id);
3344 if (err != NULL)
3345 goto done;
3347 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3348 err = got_error_from_errno();
3349 goto done;
3352 s->root = s->tree = root;
3353 s->entries = got_object_tree_get_entries(root);
3354 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3355 s->commit_id = got_object_id_dup(commit_id);
3356 if (s->commit_id == NULL) {
3357 err = got_error_from_errno();
3358 goto done;
3360 s->repo = repo;
3362 view->show = show_tree_view;
3363 view->input = input_tree_view;
3364 view->close = close_tree_view;
3365 done:
3366 free(commit_id_str);
3367 if (err) {
3368 free(s->tree_label);
3369 s->tree_label = NULL;
3371 return err;
3374 static const struct got_error *
3375 close_tree_view(struct tog_view *view)
3377 struct tog_tree_view_state *s = &view->state.tree;
3379 free(s->tree_label);
3380 s->tree_label = NULL;
3381 free(s->commit_id);
3382 s->commit_id = NULL;
3383 while (!TAILQ_EMPTY(&s->parents)) {
3384 struct tog_parent_tree *parent;
3385 parent = TAILQ_FIRST(&s->parents);
3386 TAILQ_REMOVE(&s->parents, parent, entry);
3387 free(parent);
3390 if (s->tree != s->root)
3391 got_object_tree_close(s->tree);
3392 got_object_tree_close(s->root);
3394 return NULL;
3397 static const struct got_error *
3398 show_tree_view(struct tog_view *view)
3400 const struct got_error *err = NULL;
3401 struct tog_tree_view_state *s = &view->state.tree;
3402 char *parent_path;
3404 err = tree_entry_path(&parent_path, &s->parents, NULL);
3405 if (err)
3406 return err;
3408 err = draw_tree_entries(view, &s->first_displayed_entry,
3409 &s->last_displayed_entry, &s->selected_entry,
3410 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3411 s->entries, s->selected, view->nlines, s->tree == s->root);
3412 free(parent_path);
3414 view_vborder(view);
3415 return err;
3418 static const struct got_error *
3419 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3420 struct tog_view **focus_view, struct tog_view *view, int ch)
3422 const struct got_error *err = NULL;
3423 struct tog_tree_view_state *s = &view->state.tree;
3424 struct tog_view *log_view;
3425 int begin_x = 0, nscrolled;
3427 switch (ch) {
3428 case 'i':
3429 s->show_ids = !s->show_ids;
3430 break;
3431 case 'l':
3432 if (!s->selected_entry)
3433 break;
3434 if (view_is_parent_view(view))
3435 begin_x = view_split_begin_x(view->begin_x);
3436 err = log_tree_entry(&log_view, begin_x,
3437 s->selected_entry, &s->parents,
3438 s->commit_id, s->repo);
3439 if (view_is_parent_view(view)) {
3440 err = view_close_child(view);
3441 if (err)
3442 return err;
3443 err = view_set_child(view, log_view);
3444 if (err) {
3445 view_close(log_view);
3446 break;
3448 *focus_view = log_view;
3449 view->child_focussed = 1;
3450 } else
3451 *new_view = log_view;
3452 break;
3453 case 'k':
3454 case KEY_UP:
3455 if (s->selected > 0) {
3456 s->selected--;
3457 if (s->selected == 0)
3458 break;
3460 if (s->selected > 0)
3461 break;
3462 tree_scroll_up(&s->first_displayed_entry, 1,
3463 s->entries, s->tree == s->root);
3464 break;
3465 case KEY_PPAGE:
3466 tree_scroll_up(&s->first_displayed_entry,
3467 MAX(0, view->nlines - 4 - s->selected), s->entries,
3468 s->tree == s->root);
3469 s->selected = 0;
3470 if (SIMPLEQ_FIRST(&s->entries->head) ==
3471 s->first_displayed_entry && s->tree != s->root)
3472 s->first_displayed_entry = NULL;
3473 break;
3474 case 'j':
3475 case KEY_DOWN:
3476 if (s->selected < s->ndisplayed - 1) {
3477 s->selected++;
3478 break;
3480 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3481 == NULL) {
3482 /* can't scroll any further */
3483 break;
3485 tree_scroll_down(&s->first_displayed_entry, 1,
3486 s->last_displayed_entry, s->entries);
3487 break;
3488 case KEY_NPAGE:
3489 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3490 == NULL) {
3491 /* can't scroll any further; move cursor down */
3492 if (s->selected < s->ndisplayed - 1)
3493 s->selected = s->ndisplayed - 1;
3494 break;
3496 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3497 view->nlines, s->last_displayed_entry, s->entries);
3498 if (nscrolled < view->nlines) {
3499 int ndisplayed = 0;
3500 struct got_tree_entry *te;
3501 te = s->first_displayed_entry;
3502 do {
3503 ndisplayed++;
3504 te = SIMPLEQ_NEXT(te, entry);
3505 } while (te);
3506 s->selected = ndisplayed - 1;
3508 break;
3509 case KEY_ENTER:
3510 case '\r':
3511 if (s->selected_entry == NULL) {
3512 struct tog_parent_tree *parent;
3513 case KEY_BACKSPACE:
3514 /* user selected '..' */
3515 if (s->tree == s->root)
3516 break;
3517 parent = TAILQ_FIRST(&s->parents);
3518 TAILQ_REMOVE(&s->parents, parent,
3519 entry);
3520 got_object_tree_close(s->tree);
3521 s->tree = parent->tree;
3522 s->entries =
3523 got_object_tree_get_entries(s->tree);
3524 s->first_displayed_entry =
3525 parent->first_displayed_entry;
3526 s->selected_entry =
3527 parent->selected_entry;
3528 s->selected = parent->selected;
3529 free(parent);
3530 } else if (S_ISDIR(s->selected_entry->mode)) {
3531 struct tog_parent_tree *parent;
3532 struct got_tree_object *child;
3533 err = got_object_open_as_tree(&child,
3534 s->repo, s->selected_entry->id);
3535 if (err)
3536 break;
3537 parent = calloc(1, sizeof(*parent));
3538 if (parent == NULL) {
3539 err = got_error_from_errno();
3540 break;
3542 parent->tree = s->tree;
3543 parent->first_displayed_entry =
3544 s->first_displayed_entry;
3545 parent->selected_entry = s->selected_entry;
3546 parent->selected = s->selected;
3547 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3548 s->tree = child;
3549 s->entries =
3550 got_object_tree_get_entries(s->tree);
3551 s->selected = 0;
3552 s->first_displayed_entry = NULL;
3553 } else if (S_ISREG(s->selected_entry->mode)) {
3554 struct tog_view *blame_view;
3555 int begin_x = view_is_parent_view(view) ?
3556 view_split_begin_x(view->begin_x) : 0;
3558 err = blame_tree_entry(&blame_view, begin_x,
3559 s->selected_entry, &s->parents, s->commit_id,
3560 s->repo);
3561 if (err)
3562 break;
3563 if (view_is_parent_view(view)) {
3564 err = view_close_child(view);
3565 if (err)
3566 return err;
3567 err = view_set_child(view, blame_view);
3568 if (err) {
3569 view_close(blame_view);
3570 break;
3572 *focus_view = blame_view;
3573 view->child_focussed = 1;
3574 } else
3575 *new_view = blame_view;
3577 break;
3578 case KEY_RESIZE:
3579 if (s->selected > view->nlines)
3580 s->selected = s->ndisplayed - 1;
3581 break;
3582 default:
3583 break;
3586 return err;
3589 __dead static void
3590 usage_tree(void)
3592 endwin();
3593 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3594 getprogname());
3595 exit(1);
3598 static const struct got_error *
3599 cmd_tree(int argc, char *argv[])
3601 const struct got_error *error;
3602 struct got_repository *repo = NULL;
3603 char *repo_path = NULL;
3604 struct got_object_id *commit_id = NULL;
3605 char *commit_id_arg = NULL;
3606 struct got_commit_object *commit = NULL;
3607 struct got_tree_object *tree = NULL;
3608 int ch;
3609 struct tog_view *view;
3611 #ifndef PROFILE
3612 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3613 NULL) == -1)
3614 err(1, "pledge");
3615 #endif
3617 while ((ch = getopt(argc, argv, "c:")) != -1) {
3618 switch (ch) {
3619 case 'c':
3620 commit_id_arg = optarg;
3621 break;
3622 default:
3623 usage();
3624 /* NOTREACHED */
3628 argc -= optind;
3629 argv += optind;
3631 if (argc == 0) {
3632 struct got_worktree *worktree;
3633 char *cwd = getcwd(NULL, 0);
3634 if (cwd == NULL)
3635 return got_error_from_errno();
3636 error = got_worktree_open(&worktree, cwd);
3637 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3638 goto done;
3639 if (worktree) {
3640 free(cwd);
3641 repo_path =
3642 strdup(got_worktree_get_repo_path(worktree));
3643 got_worktree_close(worktree);
3644 } else
3645 repo_path = cwd;
3646 if (repo_path == NULL) {
3647 error = got_error_from_errno();
3648 goto done;
3650 } else if (argc == 1) {
3651 repo_path = realpath(argv[0], NULL);
3652 if (repo_path == NULL)
3653 return got_error_from_errno();
3654 } else
3655 usage_log();
3657 init_curses();
3659 error = apply_unveil(repo_path, NULL);
3660 if (error)
3661 goto done;
3663 error = got_repo_open(&repo, repo_path);
3664 if (error != NULL)
3665 goto done;
3667 if (commit_id_arg == NULL)
3668 error = get_head_commit_id(&commit_id, repo);
3669 else
3670 error = got_object_resolve_id_str(&commit_id, repo,
3671 commit_id_arg);
3672 if (error != NULL)
3673 goto done;
3675 error = got_object_open_as_commit(&commit, repo, commit_id);
3676 if (error != NULL)
3677 goto done;
3679 error = got_object_open_as_tree(&tree, repo,
3680 got_object_commit_get_tree_id(commit));
3681 if (error != NULL)
3682 goto done;
3684 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3685 if (view == NULL) {
3686 error = got_error_from_errno();
3687 goto done;
3689 error = open_tree_view(view, tree, commit_id, repo);
3690 if (error)
3691 goto done;
3692 error = view_loop(view);
3693 done:
3694 free(repo_path);
3695 free(commit_id);
3696 if (commit)
3697 got_object_commit_close(commit);
3698 if (tree)
3699 got_object_tree_close(tree);
3700 if (repo)
3701 got_repo_close(repo);
3702 return error;
3705 __dead static void
3706 usage(void)
3708 int i;
3710 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3711 "Available commands:\n", getprogname());
3712 for (i = 0; i < nitems(tog_commands); i++) {
3713 struct tog_cmd *cmd = &tog_commands[i];
3714 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3716 exit(1);
3719 static char **
3720 make_argv(const char *arg0, const char *arg1)
3722 char **argv;
3723 int argc = (arg1 == NULL ? 1 : 2);
3725 argv = calloc(argc, sizeof(char *));
3726 if (argv == NULL)
3727 err(1, "calloc");
3728 argv[0] = strdup(arg0);
3729 if (argv[0] == NULL)
3730 err(1, "calloc");
3731 if (arg1) {
3732 argv[1] = strdup(arg1);
3733 if (argv[1] == NULL)
3734 err(1, "calloc");
3737 return argv;
3740 int
3741 main(int argc, char *argv[])
3743 const struct got_error *error = NULL;
3744 struct tog_cmd *cmd = NULL;
3745 int ch, hflag = 0;
3746 char **cmd_argv = NULL;
3748 setlocale(LC_CTYPE, "");
3750 while ((ch = getopt(argc, argv, "h")) != -1) {
3751 switch (ch) {
3752 case 'h':
3753 hflag = 1;
3754 break;
3755 default:
3756 usage();
3757 /* NOTREACHED */
3761 argc -= optind;
3762 argv += optind;
3763 optind = 0;
3764 optreset = 1;
3766 if (argc == 0) {
3767 if (hflag)
3768 usage();
3769 /* Build an argument vector which runs a default command. */
3770 cmd = &tog_commands[0];
3771 cmd_argv = make_argv(cmd->name, NULL);
3772 argc = 1;
3773 } else {
3774 int i;
3776 /* Did the user specific a command? */
3777 for (i = 0; i < nitems(tog_commands); i++) {
3778 if (strncmp(tog_commands[i].name, argv[0],
3779 strlen(argv[0])) == 0) {
3780 cmd = &tog_commands[i];
3781 if (hflag)
3782 tog_commands[i].cmd_usage();
3783 break;
3786 if (cmd == NULL) {
3787 /* Did the user specify a repository? */
3788 char *repo_path = realpath(argv[0], NULL);
3789 if (repo_path) {
3790 struct got_repository *repo;
3791 error = got_repo_open(&repo, repo_path);
3792 if (error == NULL)
3793 got_repo_close(repo);
3794 } else
3795 error = got_error_from_errno();
3796 if (error) {
3797 if (hflag) {
3798 fprintf(stderr, "%s: '%s' is not a "
3799 "known command\n", getprogname(),
3800 argv[0]);
3801 usage();
3803 fprintf(stderr, "%s: '%s' is neither a known "
3804 "command nor a path to a repository\n",
3805 getprogname(), argv[0]);
3806 free(repo_path);
3807 return 1;
3809 cmd = &tog_commands[0];
3810 cmd_argv = make_argv(cmd->name, repo_path);
3811 argc = 2;
3812 free(repo_path);
3816 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3817 if (error)
3818 goto done;
3819 done:
3820 endwin();
3821 free(cmd_argv);
3822 if (error)
3823 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3824 return 0;