Blob


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