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>
39 #include <regex.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_diff.h"
46 #include "got_opentemp.h"
47 #include "got_commit_graph.h"
48 #include "got_utf8.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_path.h"
52 #include "got_worktree.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 #define CTRL(x) ((x) & 0x1f)
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 struct tog_cmd {
69 const char *name;
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
72 const char *descr;
73 };
75 __dead static void usage(void);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
86 static struct tog_cmd tog_commands[] = {
87 { "log", cmd_log, usage_log,
88 "show repository history" },
89 { "diff", cmd_diff, usage_diff,
90 "compare files and directories" },
91 { "blame", cmd_blame, usage_blame,
92 "show line-by-line file history" },
93 { "tree", cmd_tree, usage_tree,
94 "browse trees in repository" },
95 };
97 enum tog_view_type {
98 TOG_VIEW_DIFF,
99 TOG_VIEW_LOG,
100 TOG_VIEW_BLAME,
101 TOG_VIEW_TREE
102 };
104 #define TOG_EOF_STRING "(END)"
106 struct commit_queue_entry {
107 TAILQ_ENTRY(commit_queue_entry) entry;
108 struct got_object_id *id;
109 struct got_commit_object *commit;
110 int idx;
111 };
112 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 struct commit_queue {
114 int ncommits;
115 struct commit_queue_head head;
116 };
118 struct tog_diff_view_state {
119 struct got_object_id *id1, *id2;
120 FILE *f;
121 int first_displayed_line;
122 int last_displayed_line;
123 int eof;
124 int diff_context;
125 struct got_repository *repo;
126 struct got_reflist_head *refs;
128 /* passed from log view; may be NULL */
129 struct tog_view *log_view;
130 };
132 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
134 struct tog_log_thread_args {
135 pthread_cond_t need_commits;
136 int commits_needed;
137 struct got_commit_graph *graph;
138 struct commit_queue *commits;
139 const char *in_repo_path;
140 struct got_object_id *start_id;
141 struct got_repository *repo;
142 int log_complete;
143 sig_atomic_t *quit;
144 struct tog_view *view;
145 struct commit_queue_entry **first_displayed_entry;
146 struct commit_queue_entry **selected_entry;
147 };
149 struct tog_log_view_state {
150 struct commit_queue commits;
151 struct commit_queue_entry *first_displayed_entry;
152 struct commit_queue_entry *last_displayed_entry;
153 struct commit_queue_entry *selected_entry;
154 int selected;
155 char *in_repo_path;
156 struct got_repository *repo;
157 struct got_reflist_head *refs;
158 struct got_object_id *start_id;
159 sig_atomic_t quit;
160 pthread_t thread;
161 struct tog_log_thread_args thread_args;
163 regex_t regex;
164 struct commit_queue_entry *matched_entry;
165 };
167 struct tog_blame_cb_args {
168 struct tog_blame_line *lines; /* one per line */
169 int nlines;
171 struct tog_view *view;
172 struct got_object_id *commit_id;
173 int *quit;
174 };
176 struct tog_blame_thread_args {
177 const char *path;
178 struct got_repository *repo;
179 struct tog_blame_cb_args *cb_args;
180 int *complete;
181 };
183 struct tog_blame {
184 FILE *f;
185 size_t filesize;
186 struct tog_blame_line *lines;
187 int nlines;
188 pthread_t thread;
189 struct tog_blame_thread_args thread_args;
190 struct tog_blame_cb_args cb_args;
191 const char *path;
192 };
194 struct tog_blame_view_state {
195 int first_displayed_line;
196 int last_displayed_line;
197 int selected_line;
198 int blame_complete;
199 int eof;
200 int done;
201 struct got_object_id_queue blamed_commits;
202 struct got_object_qid *blamed_commit;
203 char *path;
204 struct got_repository *repo;
205 struct got_reflist_head *refs;
206 struct got_object_id *commit_id;
207 struct tog_blame blame;
208 };
210 struct tog_parent_tree {
211 TAILQ_ENTRY(tog_parent_tree) entry;
212 struct got_tree_object *tree;
213 struct got_tree_entry *first_displayed_entry;
214 struct got_tree_entry *selected_entry;
215 int selected;
216 };
218 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
220 struct tog_tree_view_state {
221 char *tree_label;
222 struct got_tree_object *root;
223 struct got_tree_object *tree;
224 const struct got_tree_entries *entries;
225 struct got_tree_entry *first_displayed_entry;
226 struct got_tree_entry *last_displayed_entry;
227 struct got_tree_entry *selected_entry;
228 int ndisplayed, selected, show_ids;
229 struct tog_parent_trees parents;
230 struct got_object_id *commit_id;
231 struct got_repository *repo;
232 struct got_reflist_head *refs;
233 };
235 /*
236 * We implement two types of views: parent views and child views.
238 * The 'Tab' key switches between a parent view and its child view.
239 * Child views are shown side-by-side to their parent view, provided
240 * there is enough screen estate.
242 * When a new view is opened from within a parent view, this new view
243 * becomes a child view of the parent view, replacing any existing child.
245 * When a new view is opened from within a child view, this new view
246 * becomes a parent view which will obscure the views below until the
247 * user quits the new parent view by typing 'q'.
249 * This list of views contains parent views only.
250 * Child views are only pointed to by their parent view.
251 */
252 TAILQ_HEAD(tog_view_list_head, tog_view);
254 struct tog_view {
255 TAILQ_ENTRY(tog_view) entry;
256 WINDOW *window;
257 PANEL *panel;
258 int nlines, ncols, begin_y, begin_x;
259 int lines, cols; /* copies of LINES and COLS */
260 int focussed;
261 struct tog_view *parent;
262 struct tog_view *child;
263 int child_focussed;
265 /* type-specific state */
266 enum tog_view_type type;
267 union {
268 struct tog_diff_view_state diff;
269 struct tog_log_view_state log;
270 struct tog_blame_view_state blame;
271 struct tog_tree_view_state tree;
272 } state;
274 const struct got_error *(*show)(struct tog_view *);
275 const struct got_error *(*input)(struct tog_view **,
276 struct tog_view **, struct tog_view**, struct tog_view *, int);
277 const struct got_error *(*close)(struct tog_view *);
279 const struct got_error *(*search_start)(struct tog_view *);
280 const struct got_error *(*search_next)(struct tog_view *);
281 int searching;
282 #define TOG_SEARCH_FORWARD 1
283 #define TOG_SEARCH_BACKWARD 2
284 int search_next_done;
285 };
287 static const struct got_error *open_diff_view(struct tog_view *,
288 struct got_object_id *, struct got_object_id *, struct tog_view *,
289 struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_diff_view(struct tog_view *);
291 static const struct got_error *input_diff_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error* close_diff_view(struct tog_view *);
295 static const struct got_error *open_log_view(struct tog_view *,
296 struct got_object_id *, struct got_reflist_head *,
297 struct got_repository *, const char *, int);
298 static const struct got_error * show_log_view(struct tog_view *);
299 static const struct got_error *input_log_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_log_view(struct tog_view *);
302 static const struct got_error *search_start_log_view(struct tog_view *);
303 static const struct got_error *search_next_log_view(struct tog_view *);
305 static const struct got_error *open_blame_view(struct tog_view *, char *,
306 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
307 static const struct got_error *show_blame_view(struct tog_view *);
308 static const struct got_error *input_blame_view(struct tog_view **,
309 struct tog_view **, struct tog_view **, struct tog_view *, int);
310 static const struct got_error *close_blame_view(struct tog_view *);
312 static const struct got_error *open_tree_view(struct tog_view *,
313 struct got_tree_object *, struct got_object_id *,
314 struct got_reflist_head *, struct got_repository *);
315 static const struct got_error *show_tree_view(struct tog_view *);
316 static const struct got_error *input_tree_view(struct tog_view **,
317 struct tog_view **, struct tog_view **, struct tog_view *, int);
318 static const struct got_error *close_tree_view(struct tog_view *);
320 static volatile sig_atomic_t tog_sigwinch_received;
322 static void
323 tog_sigwinch(int signo)
325 tog_sigwinch_received = 1;
328 static const struct got_error *
329 view_close(struct tog_view *view)
331 const struct got_error *err = NULL;
333 if (view->child) {
334 view_close(view->child);
335 view->child = NULL;
337 if (view->close)
338 err = view->close(view);
339 if (view->panel)
340 del_panel(view->panel);
341 if (view->window)
342 delwin(view->window);
343 free(view);
344 return err;
347 static struct tog_view *
348 view_open(int nlines, int ncols, int begin_y, int begin_x,
349 enum tog_view_type type)
351 struct tog_view *view = calloc(1, sizeof(*view));
353 if (view == NULL)
354 return NULL;
356 view->type = type;
357 view->lines = LINES;
358 view->cols = COLS;
359 view->nlines = nlines ? nlines : LINES - begin_y;
360 view->ncols = ncols ? ncols : COLS - begin_x;
361 view->begin_y = begin_y;
362 view->begin_x = begin_x;
363 view->window = newwin(nlines, ncols, begin_y, begin_x);
364 if (view->window == NULL) {
365 view_close(view);
366 return NULL;
368 view->panel = new_panel(view->window);
369 if (view->panel == NULL ||
370 set_panel_userptr(view->panel, view) != OK) {
371 view_close(view);
372 return NULL;
375 keypad(view->window, TRUE);
376 return view;
379 static int
380 view_split_begin_x(int begin_x)
382 if (begin_x > 0 || COLS < 120)
383 return 0;
384 return (COLS - MAX(COLS / 2, 80));
387 static const struct got_error *view_resize(struct tog_view *);
389 static const struct got_error *
390 view_splitscreen(struct tog_view *view)
392 const struct got_error *err = NULL;
394 view->begin_y = 0;
395 view->begin_x = view_split_begin_x(0);
396 view->nlines = LINES;
397 view->ncols = COLS - view->begin_x;
398 view->lines = LINES;
399 view->cols = COLS;
400 err = view_resize(view);
401 if (err)
402 return err;
404 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
405 return got_error_from_errno("mvwin");
407 return NULL;
410 static const struct got_error *
411 view_fullscreen(struct tog_view *view)
413 const struct got_error *err = NULL;
415 view->begin_x = 0;
416 view->begin_y = 0;
417 view->nlines = LINES;
418 view->ncols = COLS;
419 view->lines = LINES;
420 view->cols = COLS;
421 err = view_resize(view);
422 if (err)
423 return err;
425 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
426 return got_error_from_errno("mvwin");
428 return NULL;
431 static int
432 view_is_parent_view(struct tog_view *view)
434 return view->parent == NULL;
437 static const struct got_error *
438 view_resize(struct tog_view *view)
440 int nlines, ncols;
442 if (view->lines > LINES)
443 nlines = view->nlines - (view->lines - LINES);
444 else
445 nlines = view->nlines + (LINES - view->lines);
447 if (view->cols > COLS)
448 ncols = view->ncols - (view->cols - COLS);
449 else
450 ncols = view->ncols + (COLS - view->cols);
452 if (wresize(view->window, nlines, ncols) == ERR)
453 return got_error_from_errno("wresize");
454 if (replace_panel(view->panel, view->window) == ERR)
455 return got_error_from_errno("replace_panel");
456 wclear(view->window);
458 view->nlines = nlines;
459 view->ncols = ncols;
460 view->lines = LINES;
461 view->cols = COLS;
463 if (view->child) {
464 view->child->begin_x = view_split_begin_x(view->begin_x);
465 if (view->child->begin_x == 0) {
466 view_fullscreen(view->child);
467 if (view->child->focussed)
468 show_panel(view->child->panel);
469 else
470 show_panel(view->panel);
471 } else {
472 view_splitscreen(view->child);
473 show_panel(view->child->panel);
477 return NULL;
480 static const struct got_error *
481 view_close_child(struct tog_view *view)
483 const struct got_error *err = NULL;
485 if (view->child == NULL)
486 return NULL;
488 err = view_close(view->child);
489 view->child = NULL;
490 return err;
493 static const struct got_error *
494 view_set_child(struct tog_view *view, struct tog_view *child)
496 const struct got_error *err = NULL;
498 view->child = child;
499 child->parent = view;
500 return err;
503 static int
504 view_is_splitscreen(struct tog_view *view)
506 return view->begin_x > 0;
509 static void
510 tog_resizeterm(void)
512 int cols, lines;
513 struct winsize size;
515 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
516 cols = 80; /* Default */
517 lines = 24;
518 } else {
519 cols = size.ws_col;
520 lines = size.ws_row;
522 resize_term(lines, cols);
525 static const struct got_error *
526 view_input(struct tog_view **new, struct tog_view **dead,
527 struct tog_view **focus, int *done, struct tog_view *view,
528 struct tog_view_list_head *views)
530 const struct got_error *err = NULL;
531 struct tog_view *v;
532 int ch, errcode;
534 *new = NULL;
535 *dead = NULL;
536 *focus = NULL;
538 if (view->searching && !view->search_next_done) {
539 errcode = pthread_mutex_unlock(&tog_mutex);
540 if (errcode)
541 return got_error_set_errno(errcode,
542 "pthread_mutex_unlock");
543 pthread_yield();
544 errcode = pthread_mutex_lock(&tog_mutex);
545 if (errcode)
546 return got_error_set_errno(errcode,
547 "pthread_mutex_lock");
548 view->search_next(view);
549 nodelay(stdscr, TRUE);
550 return NULL;
553 nodelay(stdscr, FALSE);
554 /* Allow threads to make progress while we are waiting for input. */
555 errcode = pthread_mutex_unlock(&tog_mutex);
556 if (errcode)
557 return got_error_set_errno(errcode, "pthread_mutex_unlock");
558 ch = wgetch(view->window);
559 errcode = pthread_mutex_lock(&tog_mutex);
560 if (errcode)
561 return got_error_set_errno(errcode, "pthread_mutex_lock");
562 nodelay(stdscr, TRUE);
564 if (tog_sigwinch_received) {
565 tog_resizeterm();
566 tog_sigwinch_received = 0;
567 TAILQ_FOREACH(v, views, entry) {
568 err = view_resize(v);
569 if (err)
570 return err;
571 err = v->input(new, dead, focus, v, KEY_RESIZE);
572 if (err)
573 return err;
577 switch (ch) {
578 case ERR:
579 break;
580 case '\t':
581 if (view->child) {
582 *focus = view->child;
583 view->child_focussed = 1;
584 } else if (view->parent) {
585 *focus = view->parent;
586 view->parent->child_focussed = 0;
588 break;
589 case 'q':
590 err = view->input(new, dead, focus, view, ch);
591 *dead = view;
592 break;
593 case 'Q':
594 *done = 1;
595 break;
596 case 'f':
597 if (view_is_parent_view(view)) {
598 if (view->child == NULL)
599 break;
600 if (view_is_splitscreen(view->child)) {
601 *focus = view->child;
602 view->child_focussed = 1;
603 err = view_fullscreen(view->child);
604 } else
605 err = view_splitscreen(view->child);
606 if (err)
607 break;
608 err = view->child->input(new, dead, focus,
609 view->child, KEY_RESIZE);
610 } else {
611 if (view_is_splitscreen(view)) {
612 *focus = view;
613 view->parent->child_focussed = 1;
614 err = view_fullscreen(view);
615 } else {
616 err = view_splitscreen(view);
618 if (err)
619 break;
620 err = view->input(new, dead, focus, view,
621 KEY_RESIZE);
623 break;
624 case KEY_RESIZE:
625 break;
626 case '/':
627 if (view->search_start)
628 view->search_start(view);
629 else
630 err = view->input(new, dead, focus, view, ch);
631 break;
632 case 'N':
633 case 'n':
634 if (view->search_next && view->searching) {
635 view->searching = (ch == 'n' ?
636 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
637 view->search_next_done = 0;
638 view->search_next(view);
639 } else
640 err = view->input(new, dead, focus, view, ch);
641 break;
642 default:
643 err = view->input(new, dead, focus, view, ch);
644 break;
647 return err;
650 void
651 view_vborder(struct tog_view *view)
653 PANEL *panel;
654 struct tog_view *view_above;
656 if (view->parent)
657 return view_vborder(view->parent);
659 panel = panel_above(view->panel);
660 if (panel == NULL)
661 return;
663 view_above = panel_userptr(panel);
664 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
665 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
668 int
669 view_needs_focus_indication(struct tog_view *view)
671 if (view_is_parent_view(view)) {
672 if (view->child == NULL || view->child_focussed)
673 return 0;
674 if (!view_is_splitscreen(view->child))
675 return 0;
676 } else if (!view_is_splitscreen(view))
677 return 0;
679 return view->focussed;
682 static const struct got_error *
683 view_loop(struct tog_view *view)
685 const struct got_error *err = NULL;
686 struct tog_view_list_head views;
687 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
688 int fast_refresh = 10;
689 int done = 0, errcode;
691 errcode = pthread_mutex_lock(&tog_mutex);
692 if (errcode)
693 return got_error_set_errno(errcode, "pthread_mutex_lock");
695 TAILQ_INIT(&views);
696 TAILQ_INSERT_HEAD(&views, view, entry);
698 main_view = view;
699 view->focussed = 1;
700 err = view->show(view);
701 if (err)
702 return err;
703 update_panels();
704 doupdate();
705 while (!TAILQ_EMPTY(&views) && !done) {
706 /* Refresh fast during initialization, then become slower. */
707 if (fast_refresh && fast_refresh-- == 0)
708 halfdelay(10); /* switch to once per second */
710 err = view_input(&new_view, &dead_view, &focus_view, &done,
711 view, &views);
712 if (err)
713 break;
714 if (dead_view) {
715 struct tog_view *prev = NULL;
717 if (view_is_parent_view(dead_view))
718 prev = TAILQ_PREV(dead_view,
719 tog_view_list_head, entry);
720 else if (view->parent != dead_view)
721 prev = view->parent;
723 if (dead_view->parent)
724 dead_view->parent->child = NULL;
725 else
726 TAILQ_REMOVE(&views, dead_view, entry);
728 err = view_close(dead_view);
729 if (err || dead_view == main_view)
730 goto done;
732 if (view == dead_view) {
733 if (focus_view)
734 view = focus_view;
735 else if (prev)
736 view = prev;
737 else if (!TAILQ_EMPTY(&views))
738 view = TAILQ_LAST(&views,
739 tog_view_list_head);
740 else
741 view = NULL;
742 if (view) {
743 if (view->child && view->child_focussed)
744 focus_view = view->child;
745 else
746 focus_view = view;
750 if (new_view) {
751 struct tog_view *v, *t;
752 /* Only allow one parent view per type. */
753 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
754 if (v->type != new_view->type)
755 continue;
756 TAILQ_REMOVE(&views, v, entry);
757 err = view_close(v);
758 if (err)
759 goto done;
760 break;
762 TAILQ_INSERT_TAIL(&views, new_view, entry);
763 view = new_view;
764 if (focus_view == NULL)
765 focus_view = new_view;
767 if (focus_view) {
768 show_panel(focus_view->panel);
769 if (view)
770 view->focussed = 0;
771 focus_view->focussed = 1;
772 view = focus_view;
773 if (new_view)
774 show_panel(new_view->panel);
775 if (view->child && view_is_splitscreen(view->child))
776 show_panel(view->child->panel);
778 if (view) {
779 if (focus_view == NULL) {
780 view->focussed = 1;
781 show_panel(view->panel);
782 if (view->child && view_is_splitscreen(view->child))
783 show_panel(view->child->panel);
784 focus_view = view;
786 if (view->parent) {
787 err = view->parent->show(view->parent);
788 if (err)
789 goto done;
791 err = view->show(view);
792 if (err)
793 goto done;
794 if (view->child) {
795 err = view->child->show(view->child);
796 if (err)
797 goto done;
799 update_panels();
800 doupdate();
803 done:
804 while (!TAILQ_EMPTY(&views)) {
805 view = TAILQ_FIRST(&views);
806 TAILQ_REMOVE(&views, view, entry);
807 view_close(view);
810 errcode = pthread_mutex_unlock(&tog_mutex);
811 if (errcode)
812 return got_error_set_errno(errcode, "pthread_mutex_unlock");
814 return err;
817 __dead static void
818 usage_log(void)
820 endwin();
821 fprintf(stderr,
822 "usage: %s log [-c commit] [-r repository-path] [path]\n",
823 getprogname());
824 exit(1);
827 /* Create newly allocated wide-character string equivalent to a byte string. */
828 static const struct got_error *
829 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
831 char *vis = NULL;
832 const struct got_error *err = NULL;
834 *ws = NULL;
835 *wlen = mbstowcs(NULL, s, 0);
836 if (*wlen == (size_t)-1) {
837 int vislen;
838 if (errno != EILSEQ)
839 return got_error_from_errno("mbstowcs");
841 /* byte string invalid in current encoding; try to "fix" it */
842 err = got_mbsavis(&vis, &vislen, s);
843 if (err)
844 return err;
845 *wlen = mbstowcs(NULL, vis, 0);
846 if (*wlen == (size_t)-1) {
847 err = got_error_from_errno("mbstowcs"); /* give up */
848 goto done;
852 *ws = calloc(*wlen + 1, sizeof(*ws));
853 if (*ws == NULL) {
854 err = got_error_from_errno("calloc");
855 goto done;
858 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
859 err = got_error_from_errno("mbstowcs");
860 done:
861 free(vis);
862 if (err) {
863 free(*ws);
864 *ws = NULL;
865 *wlen = 0;
867 return err;
870 /* Format a line for display, ensuring that it won't overflow a width limit. */
871 static const struct got_error *
872 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
874 const struct got_error *err = NULL;
875 int cols = 0;
876 wchar_t *wline = NULL;
877 size_t wlen;
878 int i;
880 *wlinep = NULL;
881 *widthp = 0;
883 err = mbs2ws(&wline, &wlen, line);
884 if (err)
885 return err;
887 i = 0;
888 while (i < wlen && cols < wlimit) {
889 int width = wcwidth(wline[i]);
890 switch (width) {
891 case 0:
892 i++;
893 break;
894 case 1:
895 case 2:
896 if (cols + width <= wlimit)
897 cols += width;
898 i++;
899 break;
900 case -1:
901 if (wline[i] == L'\t')
902 cols += TABSIZE - ((cols + 1) % TABSIZE);
903 i++;
904 break;
905 default:
906 err = got_error_from_errno("wcwidth");
907 goto done;
910 wline[i] = L'\0';
911 if (widthp)
912 *widthp = cols;
913 done:
914 if (err)
915 free(wline);
916 else
917 *wlinep = wline;
918 return err;
921 static const struct got_error*
922 build_refs_str(char **refs_str, struct got_reflist_head *refs,
923 struct got_object_id *id)
925 static const struct got_error *err = NULL;
926 struct got_reflist_entry *re;
927 char *s;
928 const char *name;
930 *refs_str = NULL;
932 SIMPLEQ_FOREACH(re, refs, entry) {
933 if (got_object_id_cmp(re->id, id) != 0)
934 continue;
935 name = got_ref_get_name(re->ref);
936 if (strcmp(name, GOT_REF_HEAD) == 0)
937 continue;
938 if (strncmp(name, "refs/", 5) == 0)
939 name += 5;
940 if (strncmp(name, "got/", 4) == 0)
941 continue;
942 if (strncmp(name, "heads/", 6) == 0)
943 name += 6;
944 if (strncmp(name, "remotes/", 8) == 0)
945 name += 8;
946 s = *refs_str;
947 if (asprintf(refs_str, "%s%s%s", s ? s : "",
948 s ? ", " : "", name) == -1) {
949 err = got_error_from_errno("asprintf");
950 free(s);
951 *refs_str = NULL;
952 break;
954 free(s);
957 return err;
960 static const struct got_error *
961 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
963 char *smallerthan, *at;
965 smallerthan = strchr(author, '<');
966 if (smallerthan && smallerthan[1] != '\0')
967 author = smallerthan + 1;
968 at = strchr(author, '@');
969 if (at)
970 *at = '\0';
971 return format_line(wauthor, author_width, author, limit);
974 static const struct got_error *
975 draw_commit(struct tog_view *view, struct got_commit_object *commit,
976 struct got_object_id *id, struct got_reflist_head *refs,
977 int author_display_cols)
979 const struct got_error *err = NULL;
980 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
981 char *logmsg0 = NULL, *logmsg = NULL;
982 char *author = NULL;
983 wchar_t *wlogmsg = NULL, *wauthor = NULL;
984 int author_width, logmsg_width;
985 char *newline, *line = NULL;
986 int col, limit;
987 static const size_t date_display_cols = 9;
988 const int avail = view->ncols;
989 struct tm tm;
990 time_t committer_time;
992 committer_time = got_object_commit_get_committer_time(commit);
993 if (localtime_r(&committer_time, &tm) == NULL)
994 return got_error_from_errno("localtime_r");
995 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
996 >= sizeof(datebuf))
997 return got_error(GOT_ERR_NO_SPACE);
999 if (avail < date_display_cols)
1000 limit = MIN(sizeof(datebuf) - 1, avail);
1001 else
1002 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1003 waddnstr(view->window, datebuf, limit);
1004 col = limit + 1;
1005 if (col > avail)
1006 goto done;
1008 author = strdup(got_object_commit_get_author(commit));
1009 if (author == NULL) {
1010 err = got_error_from_errno("strdup");
1011 goto done;
1013 err = format_author(&wauthor, &author_width, author, avail - col);
1014 if (err)
1015 goto done;
1016 waddwstr(view->window, wauthor);
1017 col += author_width;
1018 while (col <= avail && author_width < author_display_cols + 2) {
1019 waddch(view->window, ' ');
1020 col++;
1021 author_width++;
1023 if (col > avail)
1024 goto done;
1026 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1027 if (logmsg0 == NULL) {
1028 err = got_error_from_errno("strdup");
1029 goto done;
1031 logmsg = logmsg0;
1032 while (*logmsg == '\n')
1033 logmsg++;
1034 newline = strchr(logmsg, '\n');
1035 if (newline)
1036 *newline = '\0';
1037 limit = avail - col;
1038 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1039 if (err)
1040 goto done;
1041 waddwstr(view->window, wlogmsg);
1042 col += logmsg_width;
1043 while (col <= avail) {
1044 waddch(view->window, ' ');
1045 col++;
1047 done:
1048 free(logmsg0);
1049 free(wlogmsg);
1050 free(author);
1051 free(wauthor);
1052 free(line);
1053 return err;
1056 static struct commit_queue_entry *
1057 alloc_commit_queue_entry(struct got_commit_object *commit,
1058 struct got_object_id *id)
1060 struct commit_queue_entry *entry;
1062 entry = calloc(1, sizeof(*entry));
1063 if (entry == NULL)
1064 return NULL;
1066 entry->id = id;
1067 entry->commit = commit;
1068 return entry;
1071 static void
1072 pop_commit(struct commit_queue *commits)
1074 struct commit_queue_entry *entry;
1076 entry = TAILQ_FIRST(&commits->head);
1077 TAILQ_REMOVE(&commits->head, entry, entry);
1078 got_object_commit_close(entry->commit);
1079 commits->ncommits--;
1080 /* Don't free entry->id! It is owned by the commit graph. */
1081 free(entry);
1084 static void
1085 free_commits(struct commit_queue *commits)
1087 while (!TAILQ_EMPTY(&commits->head))
1088 pop_commit(commits);
1091 static const struct got_error *
1092 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1093 int minqueue, struct got_repository *repo, const char *path)
1095 const struct got_error *err = NULL;
1096 int nqueued = 0;
1099 * We keep all commits open throughout the lifetime of the log
1100 * view in order to avoid having to re-fetch commits from disk
1101 * while updating the display.
1103 while (nqueued < minqueue) {
1104 struct got_object_id *id;
1105 struct got_commit_object *commit;
1106 struct commit_queue_entry *entry;
1107 int errcode;
1109 err = got_commit_graph_iter_next(&id, graph);
1110 if (err) {
1111 if (err->code != GOT_ERR_ITER_NEED_MORE)
1112 break;
1113 err = got_commit_graph_fetch_commits(graph,
1114 minqueue, repo);
1115 if (err)
1116 return err;
1117 continue;
1120 if (id == NULL)
1121 break;
1123 err = got_object_open_as_commit(&commit, repo, id);
1124 if (err)
1125 break;
1126 entry = alloc_commit_queue_entry(commit, id);
1127 if (entry == NULL) {
1128 err = got_error_from_errno("alloc_commit_queue_entry");
1129 break;
1132 errcode = pthread_mutex_lock(&tog_mutex);
1133 if (errcode) {
1134 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1135 break;
1138 entry->idx = commits->ncommits;
1139 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1140 nqueued++;
1141 commits->ncommits++;
1143 errcode = pthread_mutex_unlock(&tog_mutex);
1144 if (errcode && err == NULL)
1145 err = got_error_set_errno(errcode,
1146 "pthread_mutex_unlock");
1149 return err;
1152 static const struct got_error *
1153 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1154 struct got_repository *repo)
1156 const struct got_error *err = NULL;
1157 struct got_reference *head_ref;
1159 *head_id = NULL;
1161 err = got_ref_open(&head_ref, repo, branch_name, 0);
1162 if (err)
1163 return err;
1165 err = got_ref_resolve(head_id, repo, head_ref);
1166 got_ref_close(head_ref);
1167 if (err) {
1168 *head_id = NULL;
1169 return err;
1172 return NULL;
1175 static const struct got_error *
1176 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1177 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1178 struct commit_queue *commits, int selected_idx, int limit,
1179 struct got_reflist_head *refs, const char *path, int commits_needed)
1181 const struct got_error *err = NULL;
1182 struct commit_queue_entry *entry;
1183 int width;
1184 int ncommits, author_cols = 10;
1185 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1186 char *refs_str = NULL;
1187 wchar_t *wline;
1189 entry = first;
1190 ncommits = 0;
1191 while (entry) {
1192 if (ncommits == selected_idx) {
1193 *selected = entry;
1194 break;
1196 entry = TAILQ_NEXT(entry, entry);
1197 ncommits++;
1200 if (*selected && !(view->searching && view->search_next_done == 0)) {
1201 err = got_object_id_str(&id_str, (*selected)->id);
1202 if (err)
1203 return err;
1204 if (refs) {
1205 err = build_refs_str(&refs_str, refs, (*selected)->id);
1206 if (err)
1207 goto done;
1211 if (commits_needed == 0)
1212 halfdelay(10); /* disable fast refresh */
1214 if (asprintf(&ncommits_str, " [%d/%d] %s",
1215 entry ? entry->idx + 1 : 0, commits->ncommits,
1216 commits_needed > 0 ?
1217 (view->searching && view->search_next_done == 0
1218 ? "searching..." : "loading... ") :
1219 (refs_str ? refs_str : "")) == -1) {
1220 err = got_error_from_errno("asprintf");
1221 goto done;
1224 if (path && strcmp(path, "/") != 0) {
1225 if (asprintf(&header, "commit %s %s%s",
1226 id_str ? id_str : "........................................",
1227 path, ncommits_str) == -1) {
1228 err = got_error_from_errno("asprintf");
1229 header = NULL;
1230 goto done;
1232 } else if (asprintf(&header, "commit %s%s",
1233 id_str ? id_str : "........................................",
1234 ncommits_str) == -1) {
1235 err = got_error_from_errno("asprintf");
1236 header = NULL;
1237 goto done;
1239 err = format_line(&wline, &width, header, view->ncols);
1240 if (err)
1241 goto done;
1243 werase(view->window);
1245 if (view_needs_focus_indication(view))
1246 wstandout(view->window);
1247 waddwstr(view->window, wline);
1248 while (width < view->ncols) {
1249 waddch(view->window, ' ');
1250 width++;
1252 if (view_needs_focus_indication(view))
1253 wstandend(view->window);
1254 free(wline);
1255 if (limit <= 1)
1256 goto done;
1258 /* Grow author column size if necessary. */
1259 entry = first;
1260 ncommits = 0;
1261 while (entry) {
1262 char *author;
1263 wchar_t *wauthor;
1264 int width;
1265 if (ncommits >= limit - 1)
1266 break;
1267 author = strdup(got_object_commit_get_author(entry->commit));
1268 if (author == NULL) {
1269 err = got_error_from_errno("strdup");
1270 goto done;
1272 err = format_author(&wauthor, &width, author, COLS);
1273 if (author_cols < width)
1274 author_cols = width;
1275 free(wauthor);
1276 free(author);
1277 entry = TAILQ_NEXT(entry, entry);
1280 entry = first;
1281 *last = first;
1282 ncommits = 0;
1283 while (entry) {
1284 if (ncommits >= limit - 1)
1285 break;
1286 if (ncommits == selected_idx)
1287 wstandout(view->window);
1288 err = draw_commit(view, entry->commit, entry->id, refs,
1289 author_cols);
1290 if (ncommits == selected_idx)
1291 wstandend(view->window);
1292 if (err)
1293 goto done;
1294 ncommits++;
1295 *last = entry;
1296 entry = TAILQ_NEXT(entry, entry);
1299 view_vborder(view);
1300 done:
1301 free(id_str);
1302 free(refs_str);
1303 free(ncommits_str);
1304 free(header);
1305 return err;
1308 static void
1309 scroll_up(struct tog_view *view,
1310 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1311 struct commit_queue *commits)
1313 struct commit_queue_entry *entry;
1314 int nscrolled = 0;
1316 entry = TAILQ_FIRST(&commits->head);
1317 if (*first_displayed_entry == entry)
1318 return;
1320 entry = *first_displayed_entry;
1321 while (entry && nscrolled < maxscroll) {
1322 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1323 if (entry) {
1324 *first_displayed_entry = entry;
1325 nscrolled++;
1330 static const struct got_error *
1331 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1332 pthread_cond_t *need_commits)
1334 int errcode;
1335 int max_wait = 20;
1337 halfdelay(1); /* fast refresh while loading commits */
1339 while (*commits_needed > 0) {
1340 if (*log_complete)
1341 break;
1343 /* Wake the log thread. */
1344 errcode = pthread_cond_signal(need_commits);
1345 if (errcode)
1346 return got_error_set_errno(errcode,
1347 "pthread_cond_signal");
1348 errcode = pthread_mutex_unlock(&tog_mutex);
1349 if (errcode)
1350 return got_error_set_errno(errcode,
1351 "pthread_mutex_unlock");
1352 pthread_yield();
1353 errcode = pthread_mutex_lock(&tog_mutex);
1354 if (errcode)
1355 return got_error_set_errno(errcode,
1356 "pthread_mutex_lock");
1358 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1360 * Thread is not done yet; lose a key press
1361 * and let the user retry... this way the GUI
1362 * remains interactive while logging deep paths
1363 * with few commits in history.
1365 return NULL;
1369 return NULL;
1372 static const struct got_error *
1373 scroll_down(struct tog_view *view,
1374 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1375 struct commit_queue_entry **last_displayed_entry,
1376 struct commit_queue *commits, int *log_complete, int *commits_needed,
1377 pthread_cond_t *need_commits)
1379 const struct got_error *err = NULL;
1380 struct commit_queue_entry *pentry;
1381 int nscrolled = 0;
1383 if (*last_displayed_entry == NULL)
1384 return NULL;
1386 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1387 if (pentry == NULL && !*log_complete) {
1389 * Ask the log thread for required amount of commits
1390 * plus some amount of pre-fetching.
1392 (*commits_needed) += maxscroll + 20;
1393 err = trigger_log_thread(0, commits_needed, log_complete,
1394 need_commits);
1395 if (err)
1396 return err;
1399 do {
1400 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1401 if (pentry == NULL)
1402 break;
1404 *last_displayed_entry = pentry;
1406 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1407 if (pentry == NULL)
1408 break;
1409 *first_displayed_entry = pentry;
1410 } while (++nscrolled < maxscroll);
1412 return err;
1415 static const struct got_error *
1416 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1417 struct got_commit_object *commit, struct got_object_id *commit_id,
1418 struct tog_view *log_view, struct got_reflist_head *refs,
1419 struct got_repository *repo)
1421 const struct got_error *err;
1422 struct got_object_qid *parent_id;
1423 struct tog_view *diff_view;
1425 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1426 if (diff_view == NULL)
1427 return got_error_from_errno("view_open");
1429 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1430 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1431 commit_id, log_view, refs, repo);
1432 if (err == NULL)
1433 *new_view = diff_view;
1434 return err;
1437 static const struct got_error *
1438 tree_view_visit_subtree(struct got_tree_object *subtree,
1439 struct tog_tree_view_state *s)
1441 struct tog_parent_tree *parent;
1443 parent = calloc(1, sizeof(*parent));
1444 if (parent == NULL)
1445 return got_error_from_errno("calloc");
1447 parent->tree = s->tree;
1448 parent->first_displayed_entry = s->first_displayed_entry;
1449 parent->selected_entry = s->selected_entry;
1450 parent->selected = s->selected;
1451 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1452 s->tree = subtree;
1453 s->entries = got_object_tree_get_entries(s->tree);
1454 s->selected = 0;
1455 s->first_displayed_entry = NULL;
1456 return NULL;
1460 static const struct got_error *
1461 browse_commit_tree(struct tog_view **new_view, int begin_x,
1462 struct commit_queue_entry *entry, const char *path,
1463 struct got_reflist_head *refs, struct got_repository *repo)
1465 const struct got_error *err = NULL;
1466 struct got_tree_object *tree;
1467 struct got_tree_entry *te;
1468 struct tog_tree_view_state *s;
1469 struct tog_view *tree_view;
1470 char *slash, *subpath = NULL;
1471 const char *p;
1473 err = got_object_open_as_tree(&tree, repo,
1474 got_object_commit_get_tree_id(entry->commit));
1475 if (err)
1476 return err;
1478 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1479 if (tree_view == NULL)
1480 return got_error_from_errno("view_open");
1482 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1483 if (err) {
1484 got_object_tree_close(tree);
1485 return err;
1487 s = &tree_view->state.tree;
1489 *new_view = tree_view;
1491 /* Walk the path and open corresponding tree objects. */
1492 p = path;
1493 while (*p) {
1494 struct got_object_id *tree_id;
1496 while (p[0] == '/')
1497 p++;
1499 /* Ensure the correct subtree entry is selected. */
1500 slash = strchr(p, '/');
1501 if (slash == NULL)
1502 slash = strchr(p, '\0');
1503 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1504 if (strncmp(p, te->name, slash - p) == 0) {
1505 s->selected_entry = te;
1506 break;
1508 s->selected++;
1510 if (s->selected_entry == NULL) {
1511 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1512 break;
1514 if (s->tree != s->root)
1515 s->selected++; /* skip '..' */
1517 if (!S_ISDIR(s->selected_entry->mode)) {
1518 /* Jump to this file's entry. */
1519 s->first_displayed_entry = s->selected_entry;
1520 s->selected = 0;
1521 break;
1524 slash = strchr(p, '/');
1525 if (slash)
1526 subpath = strndup(path, slash - path);
1527 else
1528 subpath = strdup(path);
1529 if (subpath == NULL) {
1530 err = got_error_from_errno("strdup");
1531 break;
1534 err = got_object_id_by_path(&tree_id, repo, entry->id,
1535 subpath);
1536 if (err)
1537 break;
1539 err = got_object_open_as_tree(&tree, repo, tree_id);
1540 free(tree_id);
1541 if (err)
1542 break;
1544 err = tree_view_visit_subtree(tree, s);
1545 if (err) {
1546 got_object_tree_close(tree);
1547 break;
1549 if (slash == NULL)
1550 break;
1551 free(subpath);
1552 subpath = NULL;
1553 p = slash;
1556 free(subpath);
1557 return err;
1560 static void *
1561 log_thread(void *arg)
1563 const struct got_error *err = NULL;
1564 int errcode = 0;
1565 struct tog_log_thread_args *a = arg;
1566 int done = 0;
1568 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1569 if (err)
1570 return (void *)err;
1572 while (!done && !err) {
1573 err = queue_commits(a->graph, a->commits, 1, a->repo,
1574 a->in_repo_path);
1575 if (err) {
1576 if (err->code != GOT_ERR_ITER_COMPLETED)
1577 return (void *)err;
1578 err = NULL;
1579 done = 1;
1580 } else if (a->commits_needed > 0)
1581 a->commits_needed--;
1583 errcode = pthread_mutex_lock(&tog_mutex);
1584 if (errcode) {
1585 err = got_error_set_errno(errcode,
1586 "pthread_mutex_lock");
1587 break;
1588 } else if (*a->quit)
1589 done = 1;
1590 else if (*a->first_displayed_entry == NULL) {
1591 *a->first_displayed_entry =
1592 TAILQ_FIRST(&a->commits->head);
1593 *a->selected_entry = *a->first_displayed_entry;
1596 if (done)
1597 a->commits_needed = 0;
1598 else if (a->commits_needed == 0) {
1599 errcode = pthread_cond_wait(&a->need_commits,
1600 &tog_mutex);
1601 if (errcode)
1602 err = got_error_set_errno(errcode,
1603 "pthread_cond_wait");
1606 errcode = pthread_mutex_unlock(&tog_mutex);
1607 if (errcode && err == NULL)
1608 err = got_error_set_errno(errcode,
1609 "pthread_mutex_unlock");
1611 a->log_complete = 1;
1612 return (void *)err;
1615 static const struct got_error *
1616 stop_log_thread(struct tog_log_view_state *s)
1618 const struct got_error *err = NULL;
1619 int errcode;
1621 if (s->thread) {
1622 s->quit = 1;
1623 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1624 if (errcode)
1625 return got_error_set_errno(errcode,
1626 "pthread_cond_signal");
1627 errcode = pthread_mutex_unlock(&tog_mutex);
1628 if (errcode)
1629 return got_error_set_errno(errcode,
1630 "pthread_mutex_unlock");
1631 errcode = pthread_join(s->thread, (void **)&err);
1632 if (errcode)
1633 return got_error_set_errno(errcode, "pthread_join");
1634 errcode = pthread_mutex_lock(&tog_mutex);
1635 if (errcode)
1636 return got_error_set_errno(errcode,
1637 "pthread_mutex_lock");
1638 s->thread = NULL;
1641 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1642 if (errcode && err == NULL)
1643 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1645 if (s->thread_args.repo) {
1646 got_repo_close(s->thread_args.repo);
1647 s->thread_args.repo = NULL;
1650 if (s->thread_args.graph) {
1651 got_commit_graph_close(s->thread_args.graph);
1652 s->thread_args.graph = NULL;
1655 return err;
1658 static const struct got_error *
1659 close_log_view(struct tog_view *view)
1661 const struct got_error *err = NULL;
1662 struct tog_log_view_state *s = &view->state.log;
1664 err = stop_log_thread(s);
1665 free_commits(&s->commits);
1666 free(s->in_repo_path);
1667 s->in_repo_path = NULL;
1668 free(s->start_id);
1669 s->start_id = NULL;
1670 return err;
1673 static const struct got_error *
1674 search_start_log_view(struct tog_view *view)
1676 struct tog_log_view_state *s = &view->state.log;
1677 char pattern[1024];
1678 int ret;
1680 if (view->nlines < 1)
1681 return NULL;
1683 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
1684 view->begin_x, "/");
1685 wclrtoeol(view->window);
1687 nocbreak();
1688 echo();
1689 ret = wgetnstr(view->window, pattern, sizeof(pattern));
1690 cbreak();
1691 noecho();
1692 if (ret == ERR)
1693 return NULL;
1695 if (view->searching) {
1696 regfree(&s->regex);
1697 view->searching = 0;
1700 s->matched_entry = NULL;
1701 if (regcomp(&s->regex, pattern, REG_EXTENDED | REG_ICASE) == 0) {
1702 view->searching = TOG_SEARCH_FORWARD;
1703 view->search_next_done = 0;
1704 view->search_next(view);
1707 return NULL;
1710 static int
1711 match_commit(struct got_commit_object *commit, regex_t *regex)
1713 regmatch_t regmatch;
1715 if (regexec(regex, got_object_commit_get_author(commit), 1,
1716 &regmatch, 0) == 0 ||
1717 regexec(regex, got_object_commit_get_committer(commit), 1,
1718 &regmatch, 0) == 0 ||
1719 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1720 &regmatch, 0) == 0)
1721 return 1;
1723 return 0;
1726 static const struct got_error *
1727 search_next_log_view(struct tog_view *view)
1729 const struct got_error *err = NULL;
1730 struct tog_log_view_state *s = &view->state.log;
1731 struct commit_queue_entry *entry;
1733 if (!view->searching) {
1734 view->search_next_done = 1;
1735 return NULL;
1738 if (s->matched_entry) {
1739 if (view->searching == TOG_SEARCH_FORWARD)
1740 entry = TAILQ_NEXT(s->matched_entry, entry);
1741 else
1742 entry = TAILQ_PREV(s->matched_entry,
1743 commit_queue_head, entry);
1744 } else
1745 entry = TAILQ_FIRST(&s->commits.head);
1747 while (1) {
1748 if (entry == NULL) {
1749 if (s->thread_args.log_complete) {
1750 if (s->matched_entry == NULL) {
1751 view->search_next_done = 1;
1752 return NULL;
1754 if (view->searching == TOG_SEARCH_FORWARD)
1755 entry = TAILQ_FIRST(&s->commits.head);
1756 else
1757 entry = TAILQ_LAST(&s->commits.head,
1758 commit_queue_head);
1759 } else {
1760 s->thread_args.commits_needed = 1;
1761 return trigger_log_thread(0,
1762 &s->thread_args.commits_needed,
1763 &s->thread_args.log_complete,
1764 &s->thread_args.need_commits);
1768 if (match_commit(entry->commit, &s->regex)) {
1769 view->search_next_done = 1;
1770 break;
1772 if (view->searching == TOG_SEARCH_FORWARD)
1773 entry = TAILQ_NEXT(entry, entry);
1774 else
1775 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1778 if (entry) {
1779 int cur = s->selected_entry->idx;
1780 s->matched_entry = entry;
1781 while (cur < s->matched_entry->idx) {
1782 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1783 if (err)
1784 return err;
1785 cur++;
1787 while (cur > s->matched_entry->idx) {
1788 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1789 if (err)
1790 return err;
1791 cur--;
1795 return NULL;
1799 static const struct got_error *
1800 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1801 struct got_reflist_head *refs, struct got_repository *repo,
1802 const char *path, int check_disk)
1804 const struct got_error *err = NULL;
1805 struct tog_log_view_state *s = &view->state.log;
1806 struct got_repository *thread_repo = NULL;
1807 struct got_commit_graph *thread_graph = NULL;
1808 int errcode;
1810 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1811 if (err != NULL)
1812 goto done;
1814 /* The commit queue only contains commits being displayed. */
1815 TAILQ_INIT(&s->commits.head);
1816 s->commits.ncommits = 0;
1818 s->refs = refs;
1819 s->repo = repo;
1820 s->start_id = got_object_id_dup(start_id);
1821 if (s->start_id == NULL) {
1822 err = got_error_from_errno("got_object_id_dup");
1823 goto done;
1826 view->show = show_log_view;
1827 view->input = input_log_view;
1828 view->close = close_log_view;
1829 view->search_start = search_start_log_view;
1830 view->search_next = search_next_log_view;
1832 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1833 if (err)
1834 goto done;
1835 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1836 0, thread_repo);
1837 if (err)
1838 goto done;
1840 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1841 if (errcode) {
1842 err = got_error_set_errno(errcode, "pthread_cond_init");
1843 goto done;
1846 s->thread_args.commits_needed = view->nlines;
1847 s->thread_args.graph = thread_graph;
1848 s->thread_args.commits = &s->commits;
1849 s->thread_args.in_repo_path = s->in_repo_path;
1850 s->thread_args.start_id = s->start_id;
1851 s->thread_args.repo = thread_repo;
1852 s->thread_args.log_complete = 0;
1853 s->thread_args.quit = &s->quit;
1854 s->thread_args.view = view;
1855 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1856 s->thread_args.selected_entry = &s->selected_entry;
1857 done:
1858 if (err)
1859 close_log_view(view);
1860 return err;
1863 static const struct got_error *
1864 show_log_view(struct tog_view *view)
1866 struct tog_log_view_state *s = &view->state.log;
1868 if (s->thread == NULL) {
1869 int errcode = pthread_create(&s->thread, NULL, log_thread,
1870 &s->thread_args);
1871 if (errcode)
1872 return got_error_set_errno(errcode, "pthread_create");
1875 return draw_commits(view, &s->last_displayed_entry,
1876 &s->selected_entry, s->first_displayed_entry,
1877 &s->commits, s->selected, view->nlines, s->refs,
1878 s->in_repo_path, s->thread_args.commits_needed);
1881 static const struct got_error *
1882 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1883 struct tog_view **focus_view, struct tog_view *view, int ch)
1885 const struct got_error *err = NULL;
1886 struct tog_log_view_state *s = &view->state.log;
1887 char *parent_path;
1888 struct tog_view *diff_view = NULL, *tree_view = NULL;
1889 int begin_x = 0;
1891 switch (ch) {
1892 case 'q':
1893 s->quit = 1;
1894 break;
1895 case 'k':
1896 case KEY_UP:
1897 case '<':
1898 case ',':
1899 if (s->first_displayed_entry == NULL)
1900 break;
1901 if (s->selected > 0)
1902 s->selected--;
1903 if (s->selected > 0)
1904 break;
1905 scroll_up(view, &s->first_displayed_entry, 1,
1906 &s->commits);
1907 break;
1908 case KEY_PPAGE:
1909 case CTRL('b'):
1910 if (s->first_displayed_entry == NULL)
1911 break;
1912 if (TAILQ_FIRST(&s->commits.head) ==
1913 s->first_displayed_entry) {
1914 s->selected = 0;
1915 break;
1917 scroll_up(view, &s->first_displayed_entry,
1918 view->nlines, &s->commits);
1919 break;
1920 case 'j':
1921 case KEY_DOWN:
1922 case '>':
1923 case '.':
1924 if (s->first_displayed_entry == NULL)
1925 break;
1926 if (s->selected < MIN(view->nlines - 2,
1927 s->commits.ncommits - 1)) {
1928 s->selected++;
1929 break;
1931 err = scroll_down(view, &s->first_displayed_entry, 1,
1932 &s->last_displayed_entry, &s->commits,
1933 &s->thread_args.log_complete,
1934 &s->thread_args.commits_needed,
1935 &s->thread_args.need_commits);
1936 break;
1937 case KEY_NPAGE:
1938 case CTRL('f'): {
1939 struct commit_queue_entry *first;
1940 first = s->first_displayed_entry;
1941 if (first == NULL)
1942 break;
1943 err = scroll_down(view, &s->first_displayed_entry,
1944 view->nlines, &s->last_displayed_entry,
1945 &s->commits, &s->thread_args.log_complete,
1946 &s->thread_args.commits_needed,
1947 &s->thread_args.need_commits);
1948 if (first == s->first_displayed_entry &&
1949 s->selected < MIN(view->nlines - 2,
1950 s->commits.ncommits - 1)) {
1951 /* can't scroll further down */
1952 s->selected = MIN(view->nlines - 2,
1953 s->commits.ncommits - 1);
1955 err = NULL;
1956 break;
1958 case KEY_RESIZE:
1959 if (s->selected > view->nlines - 2)
1960 s->selected = view->nlines - 2;
1961 if (s->selected > s->commits.ncommits - 1)
1962 s->selected = s->commits.ncommits - 1;
1963 break;
1964 case KEY_ENTER:
1965 case ' ':
1966 case '\r':
1967 if (s->selected_entry == NULL)
1968 break;
1969 if (view_is_parent_view(view))
1970 begin_x = view_split_begin_x(view->begin_x);
1971 err = open_diff_view_for_commit(&diff_view, begin_x,
1972 s->selected_entry->commit, s->selected_entry->id,
1973 view, s->refs, s->repo);
1974 if (err)
1975 break;
1976 if (view_is_parent_view(view)) {
1977 err = view_close_child(view);
1978 if (err)
1979 return err;
1980 err = view_set_child(view, diff_view);
1981 if (err) {
1982 view_close(diff_view);
1983 break;
1985 *focus_view = diff_view;
1986 view->child_focussed = 1;
1987 } else
1988 *new_view = diff_view;
1989 break;
1990 case 't':
1991 if (s->selected_entry == NULL)
1992 break;
1993 if (view_is_parent_view(view))
1994 begin_x = view_split_begin_x(view->begin_x);
1995 err = browse_commit_tree(&tree_view, begin_x,
1996 s->selected_entry, s->in_repo_path, s->refs, s->repo);
1997 if (err)
1998 break;
1999 if (view_is_parent_view(view)) {
2000 err = view_close_child(view);
2001 if (err)
2002 return err;
2003 err = view_set_child(view, tree_view);
2004 if (err) {
2005 view_close(tree_view);
2006 break;
2008 *focus_view = tree_view;
2009 view->child_focussed = 1;
2010 } else
2011 *new_view = tree_view;
2012 break;
2013 case KEY_BACKSPACE:
2014 if (strcmp(s->in_repo_path, "/") == 0)
2015 break;
2016 parent_path = dirname(s->in_repo_path);
2017 if (parent_path && strcmp(parent_path, ".") != 0) {
2018 struct tog_view *lv;
2019 err = stop_log_thread(s);
2020 if (err)
2021 return err;
2022 lv = view_open(view->nlines, view->ncols,
2023 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2024 if (lv == NULL)
2025 return got_error_from_errno(
2026 "view_open");
2027 err = open_log_view(lv, s->start_id, s->refs,
2028 s->repo, parent_path, 0);
2029 if (err)
2030 return err;;
2031 if (view_is_parent_view(view))
2032 *new_view = lv;
2033 else {
2034 view_set_child(view->parent, lv);
2035 *focus_view = lv;
2037 return NULL;
2039 break;
2040 default:
2041 break;
2044 return err;
2047 static const struct got_error *
2048 apply_unveil(const char *repo_path, const char *worktree_path)
2050 const struct got_error *error;
2052 if (repo_path && unveil(repo_path, "r") != 0)
2053 return got_error_from_errno2("unveil", repo_path);
2055 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2056 return got_error_from_errno2("unveil", worktree_path);
2058 if (unveil("/tmp", "rwc") != 0)
2059 return got_error_from_errno2("unveil", "/tmp");
2061 error = got_privsep_unveil_exec_helpers();
2062 if (error != NULL)
2063 return error;
2065 if (unveil(NULL, NULL) != 0)
2066 return got_error_from_errno("unveil");
2068 return NULL;
2071 static void
2072 init_curses(void)
2074 initscr();
2075 cbreak();
2076 halfdelay(1); /* Do fast refresh while initial view is loading. */
2077 noecho();
2078 nonl();
2079 intrflush(stdscr, FALSE);
2080 keypad(stdscr, TRUE);
2081 curs_set(0);
2082 signal(SIGWINCH, tog_sigwinch);
2085 static const struct got_error *
2086 cmd_log(int argc, char *argv[])
2088 const struct got_error *error;
2089 struct got_repository *repo = NULL;
2090 struct got_worktree *worktree = NULL;
2091 struct got_reflist_head refs;
2092 struct got_object_id *start_id = NULL;
2093 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2094 char *start_commit = NULL;
2095 int ch;
2096 struct tog_view *view;
2098 SIMPLEQ_INIT(&refs);
2100 #ifndef PROFILE
2101 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2102 NULL) == -1)
2103 err(1, "pledge");
2104 #endif
2106 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2107 switch (ch) {
2108 case 'c':
2109 start_commit = optarg;
2110 break;
2111 case 'r':
2112 repo_path = realpath(optarg, NULL);
2113 if (repo_path == NULL)
2114 err(1, "-r option");
2115 break;
2116 default:
2117 usage_log();
2118 /* NOTREACHED */
2122 argc -= optind;
2123 argv += optind;
2125 cwd = getcwd(NULL, 0);
2126 if (cwd == NULL) {
2127 error = got_error_from_errno("getcwd");
2128 goto done;
2130 error = got_worktree_open(&worktree, cwd);
2131 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2132 goto done;
2133 error = NULL;
2135 if (argc == 0) {
2136 path = strdup("");
2137 if (path == NULL) {
2138 error = got_error_from_errno("strdup");
2139 goto done;
2141 } else if (argc == 1) {
2142 if (worktree) {
2143 error = got_worktree_resolve_path(&path, worktree,
2144 argv[0]);
2145 if (error)
2146 goto done;
2147 } else {
2148 path = strdup(argv[0]);
2149 if (path == NULL) {
2150 error = got_error_from_errno("strdup");
2151 goto done;
2154 } else
2155 usage_log();
2157 repo_path = worktree ?
2158 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2159 if (repo_path == NULL) {
2160 error = got_error_from_errno("strdup");
2161 goto done;
2164 init_curses();
2166 error = got_repo_open(&repo, repo_path);
2167 if (error != NULL)
2168 goto done;
2170 error = apply_unveil(got_repo_get_path(repo),
2171 worktree ? got_worktree_get_root_path(worktree) : NULL);
2172 if (error)
2173 goto done;
2175 if (start_commit == NULL)
2176 error = get_head_commit_id(&start_id, worktree ?
2177 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2178 repo);
2179 else
2180 error = got_object_resolve_id_str(&start_id, repo,
2181 start_commit);
2182 if (error != NULL)
2183 goto done;
2185 error = got_ref_list(&refs, repo);
2186 if (error)
2187 goto done;
2189 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2190 if (view == NULL) {
2191 error = got_error_from_errno("view_open");
2192 goto done;
2194 error = open_log_view(view, start_id, &refs, repo, path, 1);
2195 if (error)
2196 goto done;
2197 error = view_loop(view);
2198 done:
2199 free(repo_path);
2200 free(cwd);
2201 free(path);
2202 free(start_id);
2203 if (repo)
2204 got_repo_close(repo);
2205 if (worktree)
2206 got_worktree_close(worktree);
2207 got_ref_list_free(&refs);
2208 return error;
2211 __dead static void
2212 usage_diff(void)
2214 endwin();
2215 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2216 getprogname());
2217 exit(1);
2220 static char *
2221 parse_next_line(FILE *f, size_t *len)
2223 char *line;
2224 size_t linelen;
2225 size_t lineno;
2226 const char delim[3] = { '\0', '\0', '\0'};
2228 line = fparseln(f, &linelen, &lineno, delim, 0);
2229 if (len)
2230 *len = linelen;
2231 return line;
2234 static const struct got_error *
2235 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2236 int *last_displayed_line, int *eof, int max_lines,
2237 char *header)
2239 const struct got_error *err;
2240 int nlines = 0, nprinted = 0;
2241 char *line;
2242 size_t len;
2243 wchar_t *wline;
2244 int width;
2246 rewind(f);
2247 werase(view->window);
2249 if (header) {
2250 err = format_line(&wline, &width, header, view->ncols);
2251 if (err) {
2252 return err;
2255 if (view_needs_focus_indication(view))
2256 wstandout(view->window);
2257 waddwstr(view->window, wline);
2258 if (view_needs_focus_indication(view))
2259 wstandend(view->window);
2260 if (width < view->ncols - 1)
2261 waddch(view->window, '\n');
2263 if (max_lines <= 1)
2264 return NULL;
2265 max_lines--;
2268 *eof = 0;
2269 while (nprinted < max_lines) {
2270 line = parse_next_line(f, &len);
2271 if (line == NULL) {
2272 *eof = 1;
2273 break;
2275 if (++nlines < *first_displayed_line) {
2276 free(line);
2277 continue;
2280 err = format_line(&wline, &width, line, view->ncols);
2281 if (err) {
2282 free(line);
2283 return err;
2285 waddwstr(view->window, wline);
2286 if (width < view->ncols - 1)
2287 waddch(view->window, '\n');
2288 if (++nprinted == 1)
2289 *first_displayed_line = nlines;
2290 free(line);
2291 free(wline);
2292 wline = NULL;
2294 *last_displayed_line = nlines;
2296 view_vborder(view);
2298 if (*eof) {
2299 while (nprinted < view->nlines) {
2300 waddch(view->window, '\n');
2301 nprinted++;
2304 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2305 if (err) {
2306 return err;
2309 wstandout(view->window);
2310 waddwstr(view->window, wline);
2311 wstandend(view->window);
2314 return NULL;
2317 static char *
2318 get_datestr(time_t *time, char *datebuf)
2320 char *p, *s = ctime_r(time, datebuf);
2321 p = strchr(s, '\n');
2322 if (p)
2323 *p = '\0';
2324 return s;
2327 static const struct got_error *
2328 write_commit_info(struct got_object_id *commit_id,
2329 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2331 const struct got_error *err = NULL;
2332 char datebuf[26];
2333 struct got_commit_object *commit;
2334 char *id_str = NULL;
2335 time_t committer_time;
2336 const char *author, *committer;
2337 char *refs_str = NULL;
2339 if (refs) {
2340 err = build_refs_str(&refs_str, refs, commit_id);
2341 if (err)
2342 return err;
2345 err = got_object_open_as_commit(&commit, repo, commit_id);
2346 if (err)
2347 return err;
2349 err = got_object_id_str(&id_str, commit_id);
2350 if (err) {
2351 err = got_error_from_errno("got_object_id_str");
2352 goto done;
2355 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2356 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2357 err = got_error_from_errno("fprintf");
2358 goto done;
2360 if (fprintf(outfile, "from: %s\n",
2361 got_object_commit_get_author(commit)) < 0) {
2362 err = got_error_from_errno("fprintf");
2363 goto done;
2365 committer_time = got_object_commit_get_committer_time(commit);
2366 if (fprintf(outfile, "date: %s UTC\n",
2367 get_datestr(&committer_time, datebuf)) < 0) {
2368 err = got_error_from_errno("fprintf");
2369 goto done;
2371 author = got_object_commit_get_author(commit);
2372 committer = got_object_commit_get_committer(commit);
2373 if (strcmp(author, committer) != 0 &&
2374 fprintf(outfile, "via: %s\n", committer) < 0) {
2375 err = got_error_from_errno("fprintf");
2376 goto done;
2378 if (fprintf(outfile, "%s\n",
2379 got_object_commit_get_logmsg(commit)) < 0) {
2380 err = got_error_from_errno("fprintf");
2381 goto done;
2383 done:
2384 free(id_str);
2385 free(refs_str);
2386 got_object_commit_close(commit);
2387 return err;
2390 static const struct got_error *
2391 create_diff(struct tog_diff_view_state *s)
2393 const struct got_error *err = NULL;
2394 FILE *f = NULL;
2395 int obj_type;
2397 f = got_opentemp();
2398 if (f == NULL) {
2399 err = got_error_from_errno("got_opentemp");
2400 goto done;
2402 if (s->f && fclose(s->f) != 0) {
2403 err = got_error_from_errno("fclose");
2404 goto done;
2406 s->f = f;
2408 if (s->id1)
2409 err = got_object_get_type(&obj_type, s->repo, s->id1);
2410 else
2411 err = got_object_get_type(&obj_type, s->repo, s->id2);
2412 if (err)
2413 goto done;
2415 switch (obj_type) {
2416 case GOT_OBJ_TYPE_BLOB:
2417 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2418 s->diff_context, s->repo, f);
2419 break;
2420 case GOT_OBJ_TYPE_TREE:
2421 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2422 s->diff_context, s->repo, f);
2423 break;
2424 case GOT_OBJ_TYPE_COMMIT: {
2425 const struct got_object_id_queue *parent_ids;
2426 struct got_object_qid *pid;
2427 struct got_commit_object *commit2;
2429 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2430 if (err)
2431 break;
2432 /* Show commit info if we're diffing to a parent/root commit. */
2433 if (s->id1 == NULL)
2434 write_commit_info(s->id2, s->refs, s->repo, f);
2435 else {
2436 parent_ids = got_object_commit_get_parent_ids(commit2);
2437 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2438 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2439 write_commit_info(s->id2, s->refs,
2440 s->repo, f);
2441 break;
2445 got_object_commit_close(commit2);
2447 err = got_diff_objects_as_commits(s->id1, s->id2,
2448 s->diff_context, s->repo, f);
2449 break;
2451 default:
2452 err = got_error(GOT_ERR_OBJ_TYPE);
2453 break;
2455 done:
2456 if (f && fflush(f) != 0 && err == NULL)
2457 err = got_error_from_errno("fflush");
2458 return err;
2461 static void
2462 diff_view_indicate_progress(struct tog_view *view)
2464 mvwaddstr(view->window, 0, 0, "diffing...");
2465 update_panels();
2466 doupdate();
2469 static const struct got_error *
2470 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2471 struct got_object_id *id2, struct tog_view *log_view,
2472 struct got_reflist_head *refs, struct got_repository *repo)
2474 const struct got_error *err;
2476 if (id1 != NULL && id2 != NULL) {
2477 int type1, type2;
2478 err = got_object_get_type(&type1, repo, id1);
2479 if (err)
2480 return err;
2481 err = got_object_get_type(&type2, repo, id2);
2482 if (err)
2483 return err;
2485 if (type1 != type2)
2486 return got_error(GOT_ERR_OBJ_TYPE);
2489 if (id1) {
2490 view->state.diff.id1 = got_object_id_dup(id1);
2491 if (view->state.diff.id1 == NULL)
2492 return got_error_from_errno("got_object_id_dup");
2493 } else
2494 view->state.diff.id1 = NULL;
2496 view->state.diff.id2 = got_object_id_dup(id2);
2497 if (view->state.diff.id2 == NULL) {
2498 free(view->state.diff.id1);
2499 view->state.diff.id1 = NULL;
2500 return got_error_from_errno("got_object_id_dup");
2502 view->state.diff.f = NULL;
2503 view->state.diff.first_displayed_line = 1;
2504 view->state.diff.last_displayed_line = view->nlines;
2505 view->state.diff.diff_context = 3;
2506 view->state.diff.log_view = log_view;
2507 view->state.diff.repo = repo;
2508 view->state.diff.refs = refs;
2510 if (log_view && view_is_splitscreen(view))
2511 show_log_view(log_view); /* draw vborder */
2512 diff_view_indicate_progress(view);
2514 err = create_diff(&view->state.diff);
2515 if (err) {
2516 free(view->state.diff.id1);
2517 view->state.diff.id1 = NULL;
2518 free(view->state.diff.id2);
2519 view->state.diff.id2 = NULL;
2520 return err;
2523 view->show = show_diff_view;
2524 view->input = input_diff_view;
2525 view->close = close_diff_view;
2527 return NULL;
2530 static const struct got_error *
2531 close_diff_view(struct tog_view *view)
2533 const struct got_error *err = NULL;
2535 free(view->state.diff.id1);
2536 view->state.diff.id1 = NULL;
2537 free(view->state.diff.id2);
2538 view->state.diff.id2 = NULL;
2539 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2540 err = got_error_from_errno("fclose");
2541 return err;
2544 static const struct got_error *
2545 show_diff_view(struct tog_view *view)
2547 const struct got_error *err;
2548 struct tog_diff_view_state *s = &view->state.diff;
2549 char *id_str1 = NULL, *id_str2, *header;
2551 if (s->id1) {
2552 err = got_object_id_str(&id_str1, s->id1);
2553 if (err)
2554 return err;
2556 err = got_object_id_str(&id_str2, s->id2);
2557 if (err)
2558 return err;
2560 if (asprintf(&header, "diff %s %s",
2561 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2562 err = got_error_from_errno("asprintf");
2563 free(id_str1);
2564 free(id_str2);
2565 return err;
2567 free(id_str1);
2568 free(id_str2);
2570 return draw_file(view, s->f, &s->first_displayed_line,
2571 &s->last_displayed_line, &s->eof, view->nlines,
2572 header);
2575 static const struct got_error *
2576 set_selected_commit(struct tog_diff_view_state *s,
2577 struct commit_queue_entry *entry)
2579 const struct got_error *err;
2580 const struct got_object_id_queue *parent_ids;
2581 struct got_commit_object *selected_commit;
2582 struct got_object_qid *pid;
2584 free(s->id2);
2585 s->id2 = got_object_id_dup(entry->id);
2586 if (s->id2 == NULL)
2587 return got_error_from_errno("got_object_id_dup");
2589 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2590 if (err)
2591 return err;
2592 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2593 free(s->id1);
2594 pid = SIMPLEQ_FIRST(parent_ids);
2595 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2596 got_object_commit_close(selected_commit);
2597 return NULL;
2600 static const struct got_error *
2601 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2602 struct tog_view **focus_view, struct tog_view *view, int ch)
2604 const struct got_error *err = NULL;
2605 struct tog_diff_view_state *s = &view->state.diff;
2606 struct tog_log_view_state *ls;
2607 struct commit_queue_entry *entry;
2608 int i;
2610 switch (ch) {
2611 case 'k':
2612 case KEY_UP:
2613 if (s->first_displayed_line > 1)
2614 s->first_displayed_line--;
2615 break;
2616 case KEY_PPAGE:
2617 case CTRL('b'):
2618 if (s->first_displayed_line == 1)
2619 break;
2620 i = 0;
2621 while (i++ < view->nlines - 1 &&
2622 s->first_displayed_line > 1)
2623 s->first_displayed_line--;
2624 break;
2625 case 'j':
2626 case KEY_DOWN:
2627 if (!s->eof)
2628 s->first_displayed_line++;
2629 break;
2630 case KEY_NPAGE:
2631 case CTRL('f'):
2632 case ' ':
2633 if (s->eof)
2634 break;
2635 i = 0;
2636 while (!s->eof && i++ < view->nlines - 1) {
2637 char *line;
2638 line = parse_next_line(s->f, NULL);
2639 s->first_displayed_line++;
2640 if (line == NULL)
2641 break;
2643 break;
2644 case '[':
2645 if (s->diff_context > 0) {
2646 s->diff_context--;
2647 diff_view_indicate_progress(view);
2648 err = create_diff(s);
2650 break;
2651 case ']':
2652 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2653 s->diff_context++;
2654 diff_view_indicate_progress(view);
2655 err = create_diff(s);
2657 break;
2658 case '<':
2659 case ',':
2660 if (s->log_view == NULL)
2661 break;
2662 ls = &s->log_view->state.log;
2663 entry = TAILQ_PREV(ls->selected_entry,
2664 commit_queue_head, entry);
2665 if (entry == NULL)
2666 break;
2668 err = input_log_view(NULL, NULL, NULL, s->log_view,
2669 KEY_UP);
2670 if (err)
2671 break;
2673 err = set_selected_commit(s, entry);
2674 if (err)
2675 break;
2677 s->first_displayed_line = 1;
2678 s->last_displayed_line = view->nlines;
2680 diff_view_indicate_progress(view);
2681 err = create_diff(s);
2682 break;
2683 case '>':
2684 case '.':
2685 if (s->log_view == NULL)
2686 break;
2687 ls = &s->log_view->state.log;
2689 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2690 ls->thread_args.commits_needed++;
2692 /* Display "loading..." in log view. */
2693 show_log_view(s->log_view);
2694 update_panels();
2695 doupdate();
2697 err = trigger_log_thread(1 /* load_all */,
2698 &ls->thread_args.commits_needed,
2699 &ls->thread_args.log_complete,
2700 &ls->thread_args.need_commits);
2701 if (err)
2702 break;
2704 err = input_log_view(NULL, NULL, NULL, s->log_view,
2705 KEY_DOWN);
2706 if (err)
2707 break;
2709 entry = TAILQ_NEXT(ls->selected_entry, entry);
2710 if (entry == NULL)
2711 break;
2713 err = set_selected_commit(s, entry);
2714 if (err)
2715 break;
2717 s->first_displayed_line = 1;
2718 s->last_displayed_line = view->nlines;
2720 diff_view_indicate_progress(view);
2721 err = create_diff(s);
2722 break;
2723 default:
2724 break;
2727 return err;
2730 static const struct got_error *
2731 cmd_diff(int argc, char *argv[])
2733 const struct got_error *error = NULL;
2734 struct got_repository *repo = NULL;
2735 struct got_reflist_head refs;
2736 struct got_object_id *id1 = NULL, *id2 = NULL;
2737 char *repo_path = NULL;
2738 char *id_str1 = NULL, *id_str2 = NULL;
2739 int ch;
2740 struct tog_view *view;
2742 SIMPLEQ_INIT(&refs);
2744 #ifndef PROFILE
2745 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2746 NULL) == -1)
2747 err(1, "pledge");
2748 #endif
2750 while ((ch = getopt(argc, argv, "")) != -1) {
2751 switch (ch) {
2752 default:
2753 usage_diff();
2754 /* NOTREACHED */
2758 argc -= optind;
2759 argv += optind;
2761 if (argc == 0) {
2762 usage_diff(); /* TODO show local worktree changes */
2763 } else if (argc == 2) {
2764 repo_path = getcwd(NULL, 0);
2765 if (repo_path == NULL)
2766 return got_error_from_errno("getcwd");
2767 id_str1 = argv[0];
2768 id_str2 = argv[1];
2769 } else if (argc == 3) {
2770 repo_path = realpath(argv[0], NULL);
2771 if (repo_path == NULL)
2772 return got_error_from_errno2("realpath", argv[0]);
2773 id_str1 = argv[1];
2774 id_str2 = argv[2];
2775 } else
2776 usage_diff();
2778 init_curses();
2780 error = got_repo_open(&repo, repo_path);
2781 if (error)
2782 goto done;
2784 error = apply_unveil(got_repo_get_path(repo), NULL);
2785 if (error)
2786 goto done;
2788 error = got_object_resolve_id_str(&id1, repo, id_str1);
2789 if (error)
2790 goto done;
2792 error = got_object_resolve_id_str(&id2, repo, id_str2);
2793 if (error)
2794 goto done;
2796 error = got_ref_list(&refs, repo);
2797 if (error)
2798 goto done;
2800 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2801 if (view == NULL) {
2802 error = got_error_from_errno("view_open");
2803 goto done;
2805 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2806 if (error)
2807 goto done;
2808 error = view_loop(view);
2809 done:
2810 free(repo_path);
2811 got_repo_close(repo);
2812 got_ref_list_free(&refs);
2813 return error;
2816 __dead static void
2817 usage_blame(void)
2819 endwin();
2820 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2821 getprogname());
2822 exit(1);
2825 struct tog_blame_line {
2826 int annotated;
2827 struct got_object_id *id;
2830 static const struct got_error *
2831 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2832 const char *path, struct tog_blame_line *lines, int nlines,
2833 int blame_complete, int selected_line, int *first_displayed_line,
2834 int *last_displayed_line, int *eof, int max_lines)
2836 const struct got_error *err;
2837 int lineno = 0, nprinted = 0;
2838 char *line;
2839 size_t len;
2840 wchar_t *wline;
2841 int width, wlimit;
2842 struct tog_blame_line *blame_line;
2843 struct got_object_id *prev_id = NULL;
2844 char *id_str;
2846 err = got_object_id_str(&id_str, id);
2847 if (err)
2848 return err;
2850 rewind(f);
2851 werase(view->window);
2853 if (asprintf(&line, "commit %s", id_str) == -1) {
2854 err = got_error_from_errno("asprintf");
2855 free(id_str);
2856 return err;
2859 err = format_line(&wline, &width, line, view->ncols);
2860 free(line);
2861 line = NULL;
2862 if (view_needs_focus_indication(view))
2863 wstandout(view->window);
2864 waddwstr(view->window, wline);
2865 if (view_needs_focus_indication(view))
2866 wstandend(view->window);
2867 free(wline);
2868 wline = NULL;
2869 if (width < view->ncols - 1)
2870 waddch(view->window, '\n');
2872 if (asprintf(&line, "[%d/%d] %s%s",
2873 *first_displayed_line - 1 + selected_line, nlines,
2874 blame_complete ? "" : "annotating... ", path) == -1) {
2875 free(id_str);
2876 return got_error_from_errno("asprintf");
2878 free(id_str);
2879 err = format_line(&wline, &width, line, view->ncols);
2880 free(line);
2881 line = NULL;
2882 if (err)
2883 return err;
2884 waddwstr(view->window, wline);
2885 free(wline);
2886 wline = NULL;
2887 if (width < view->ncols - 1)
2888 waddch(view->window, '\n');
2890 *eof = 0;
2891 while (nprinted < max_lines - 2) {
2892 line = parse_next_line(f, &len);
2893 if (line == NULL) {
2894 *eof = 1;
2895 break;
2897 if (++lineno < *first_displayed_line) {
2898 free(line);
2899 continue;
2902 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2903 err = format_line(&wline, &width, line, wlimit);
2904 if (err) {
2905 free(line);
2906 return err;
2909 if (view->focussed && nprinted == selected_line - 1)
2910 wstandout(view->window);
2912 blame_line = &lines[lineno - 1];
2913 if (blame_line->annotated && prev_id &&
2914 got_object_id_cmp(prev_id, blame_line->id) == 0)
2915 waddstr(view->window, " ");
2916 else if (blame_line->annotated) {
2917 char *id_str;
2918 err = got_object_id_str(&id_str, blame_line->id);
2919 if (err) {
2920 free(line);
2921 free(wline);
2922 return err;
2924 wprintw(view->window, "%.8s ", id_str);
2925 free(id_str);
2926 prev_id = blame_line->id;
2927 } else {
2928 waddstr(view->window, "........ ");
2929 prev_id = NULL;
2932 waddwstr(view->window, wline);
2933 while (width < wlimit) {
2934 waddch(view->window, ' ');
2935 width++;
2937 if (view->focussed && nprinted == selected_line - 1)
2938 wstandend(view->window);
2939 if (++nprinted == 1)
2940 *first_displayed_line = lineno;
2941 free(line);
2942 free(wline);
2943 wline = NULL;
2945 *last_displayed_line = lineno;
2947 view_vborder(view);
2949 return NULL;
2952 static const struct got_error *
2953 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2955 const struct got_error *err = NULL;
2956 struct tog_blame_cb_args *a = arg;
2957 struct tog_blame_line *line;
2958 int errcode;
2960 if (nlines != a->nlines ||
2961 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2962 return got_error(GOT_ERR_RANGE);
2964 errcode = pthread_mutex_lock(&tog_mutex);
2965 if (errcode)
2966 return got_error_set_errno(errcode, "pthread_mutex_lock");
2968 if (*a->quit) { /* user has quit the blame view */
2969 err = got_error(GOT_ERR_ITER_COMPLETED);
2970 goto done;
2973 if (lineno == -1)
2974 goto done; /* no change in this commit */
2976 line = &a->lines[lineno - 1];
2977 if (line->annotated)
2978 goto done;
2980 line->id = got_object_id_dup(id);
2981 if (line->id == NULL) {
2982 err = got_error_from_errno("got_object_id_dup");
2983 goto done;
2985 line->annotated = 1;
2986 done:
2987 errcode = pthread_mutex_unlock(&tog_mutex);
2988 if (errcode)
2989 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2990 return err;
2993 static void *
2994 blame_thread(void *arg)
2996 const struct got_error *err;
2997 struct tog_blame_thread_args *ta = arg;
2998 struct tog_blame_cb_args *a = ta->cb_args;
2999 int errcode;
3001 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3002 blame_cb, ta->cb_args);
3004 errcode = pthread_mutex_lock(&tog_mutex);
3005 if (errcode)
3006 return (void *)got_error_set_errno(errcode,
3007 "pthread_mutex_lock");
3009 got_repo_close(ta->repo);
3010 ta->repo = NULL;
3011 *ta->complete = 1;
3013 errcode = pthread_mutex_unlock(&tog_mutex);
3014 if (errcode && err == NULL)
3015 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3017 return (void *)err;
3020 static struct got_object_id *
3021 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3022 int selected_line)
3024 struct tog_blame_line *line;
3026 line = &lines[first_displayed_line - 1 + selected_line - 1];
3027 if (!line->annotated)
3028 return NULL;
3030 return line->id;
3033 static const struct got_error *
3034 stop_blame(struct tog_blame *blame)
3036 const struct got_error *err = NULL;
3037 int i;
3039 if (blame->thread) {
3040 int errcode;
3041 errcode = pthread_mutex_unlock(&tog_mutex);
3042 if (errcode)
3043 return got_error_set_errno(errcode,
3044 "pthread_mutex_unlock");
3045 errcode = pthread_join(blame->thread, (void **)&err);
3046 if (errcode)
3047 return got_error_set_errno(errcode, "pthread_join");
3048 errcode = pthread_mutex_lock(&tog_mutex);
3049 if (errcode)
3050 return got_error_set_errno(errcode,
3051 "pthread_mutex_lock");
3052 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3053 err = NULL;
3054 blame->thread = NULL;
3056 if (blame->thread_args.repo) {
3057 got_repo_close(blame->thread_args.repo);
3058 blame->thread_args.repo = NULL;
3060 if (blame->f) {
3061 if (fclose(blame->f) != 0 && err == NULL)
3062 err = got_error_from_errno("fclose");
3063 blame->f = NULL;
3065 if (blame->lines) {
3066 for (i = 0; i < blame->nlines; i++)
3067 free(blame->lines[i].id);
3068 free(blame->lines);
3069 blame->lines = NULL;
3071 free(blame->cb_args.commit_id);
3072 blame->cb_args.commit_id = NULL;
3074 return err;
3077 static const struct got_error *
3078 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3079 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3080 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3081 struct got_repository *repo)
3083 const struct got_error *err = NULL;
3084 struct got_blob_object *blob = NULL;
3085 struct got_repository *thread_repo = NULL;
3086 struct got_object_id *obj_id = NULL;
3087 int obj_type;
3089 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3090 if (err)
3091 return err;
3092 if (obj_id == NULL)
3093 return got_error(GOT_ERR_NO_OBJ);
3095 err = got_object_get_type(&obj_type, repo, obj_id);
3096 if (err)
3097 goto done;
3099 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3100 err = got_error(GOT_ERR_OBJ_TYPE);
3101 goto done;
3104 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3105 if (err)
3106 goto done;
3107 blame->f = got_opentemp();
3108 if (blame->f == NULL) {
3109 err = got_error_from_errno("got_opentemp");
3110 goto done;
3112 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3113 blame->f, blob);
3114 if (err)
3115 goto done;
3117 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3118 if (blame->lines == NULL) {
3119 err = got_error_from_errno("calloc");
3120 goto done;
3123 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3124 if (err)
3125 goto done;
3127 blame->cb_args.view = view;
3128 blame->cb_args.lines = blame->lines;
3129 blame->cb_args.nlines = blame->nlines;
3130 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3131 if (blame->cb_args.commit_id == NULL) {
3132 err = got_error_from_errno("got_object_id_dup");
3133 goto done;
3135 blame->cb_args.quit = done;
3137 blame->thread_args.path = path;
3138 blame->thread_args.repo = thread_repo;
3139 blame->thread_args.cb_args = &blame->cb_args;
3140 blame->thread_args.complete = blame_complete;
3141 *blame_complete = 0;
3143 done:
3144 if (blob)
3145 got_object_blob_close(blob);
3146 free(obj_id);
3147 if (err)
3148 stop_blame(blame);
3149 return err;
3152 static const struct got_error *
3153 open_blame_view(struct tog_view *view, char *path,
3154 struct got_object_id *commit_id, struct got_reflist_head *refs,
3155 struct got_repository *repo)
3157 const struct got_error *err = NULL;
3158 struct tog_blame_view_state *s = &view->state.blame;
3160 SIMPLEQ_INIT(&s->blamed_commits);
3162 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3163 if (err)
3164 return err;
3166 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3167 s->first_displayed_line = 1;
3168 s->last_displayed_line = view->nlines;
3169 s->selected_line = 1;
3170 s->blame_complete = 0;
3171 s->path = path;
3172 if (s->path == NULL)
3173 return got_error_from_errno("open_blame_view");
3174 s->repo = repo;
3175 s->refs = refs;
3176 s->commit_id = commit_id;
3177 memset(&s->blame, 0, sizeof(s->blame));
3179 view->show = show_blame_view;
3180 view->input = input_blame_view;
3181 view->close = close_blame_view;
3183 return run_blame(&s->blame, view, &s->blame_complete,
3184 &s->first_displayed_line, &s->last_displayed_line,
3185 &s->selected_line, &s->done, &s->eof, s->path,
3186 s->blamed_commit->id, s->repo);
3189 static const struct got_error *
3190 close_blame_view(struct tog_view *view)
3192 const struct got_error *err = NULL;
3193 struct tog_blame_view_state *s = &view->state.blame;
3195 if (s->blame.thread)
3196 err = stop_blame(&s->blame);
3198 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3199 struct got_object_qid *blamed_commit;
3200 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3201 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3202 got_object_qid_free(blamed_commit);
3205 free(s->path);
3207 return err;
3210 static const struct got_error *
3211 show_blame_view(struct tog_view *view)
3213 const struct got_error *err = NULL;
3214 struct tog_blame_view_state *s = &view->state.blame;
3215 int errcode;
3217 if (s->blame.thread == NULL) {
3218 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3219 &s->blame.thread_args);
3220 if (errcode)
3221 return got_error_set_errno(errcode, "pthread_create");
3224 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3225 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3226 s->selected_line, &s->first_displayed_line,
3227 &s->last_displayed_line, &s->eof, view->nlines);
3229 view_vborder(view);
3230 return err;
3233 static const struct got_error *
3234 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3235 struct tog_view **focus_view, struct tog_view *view, int ch)
3237 const struct got_error *err = NULL, *thread_err = NULL;
3238 struct tog_view *diff_view;
3239 struct tog_blame_view_state *s = &view->state.blame;
3240 int begin_x = 0;
3242 switch (ch) {
3243 case 'q':
3244 s->done = 1;
3245 break;
3246 case 'k':
3247 case KEY_UP:
3248 if (s->selected_line > 1)
3249 s->selected_line--;
3250 else if (s->selected_line == 1 &&
3251 s->first_displayed_line > 1)
3252 s->first_displayed_line--;
3253 break;
3254 case KEY_PPAGE:
3255 if (s->first_displayed_line == 1) {
3256 s->selected_line = 1;
3257 break;
3259 if (s->first_displayed_line > view->nlines - 2)
3260 s->first_displayed_line -=
3261 (view->nlines - 2);
3262 else
3263 s->first_displayed_line = 1;
3264 break;
3265 case 'j':
3266 case KEY_DOWN:
3267 if (s->selected_line < view->nlines - 2 &&
3268 s->first_displayed_line +
3269 s->selected_line <= s->blame.nlines)
3270 s->selected_line++;
3271 else if (s->last_displayed_line <
3272 s->blame.nlines)
3273 s->first_displayed_line++;
3274 break;
3275 case 'b':
3276 case 'p': {
3277 struct got_object_id *id = NULL;
3278 id = get_selected_commit_id(s->blame.lines,
3279 s->first_displayed_line, s->selected_line);
3280 if (id == NULL)
3281 break;
3282 if (ch == 'p') {
3283 struct got_commit_object *commit;
3284 struct got_object_qid *pid;
3285 struct got_object_id *blob_id = NULL;
3286 int obj_type;
3287 err = got_object_open_as_commit(&commit,
3288 s->repo, id);
3289 if (err)
3290 break;
3291 pid = SIMPLEQ_FIRST(
3292 got_object_commit_get_parent_ids(commit));
3293 if (pid == NULL) {
3294 got_object_commit_close(commit);
3295 break;
3297 /* Check if path history ends here. */
3298 err = got_object_id_by_path(&blob_id, s->repo,
3299 pid->id, s->path);
3300 if (err) {
3301 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3302 err = NULL;
3303 got_object_commit_close(commit);
3304 break;
3306 err = got_object_get_type(&obj_type, s->repo,
3307 blob_id);
3308 free(blob_id);
3309 /* Can't blame non-blob type objects. */
3310 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3311 got_object_commit_close(commit);
3312 break;
3314 err = got_object_qid_alloc(&s->blamed_commit,
3315 pid->id);
3316 got_object_commit_close(commit);
3317 } else {
3318 if (got_object_id_cmp(id,
3319 s->blamed_commit->id) == 0)
3320 break;
3321 err = got_object_qid_alloc(&s->blamed_commit,
3322 id);
3324 if (err)
3325 break;
3326 s->done = 1;
3327 thread_err = stop_blame(&s->blame);
3328 s->done = 0;
3329 if (thread_err)
3330 break;
3331 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3332 s->blamed_commit, entry);
3333 err = run_blame(&s->blame, view, &s->blame_complete,
3334 &s->first_displayed_line, &s->last_displayed_line,
3335 &s->selected_line, &s->done, &s->eof,
3336 s->path, s->blamed_commit->id, s->repo);
3337 if (err)
3338 break;
3339 break;
3341 case 'B': {
3342 struct got_object_qid *first;
3343 first = SIMPLEQ_FIRST(&s->blamed_commits);
3344 if (!got_object_id_cmp(first->id, s->commit_id))
3345 break;
3346 s->done = 1;
3347 thread_err = stop_blame(&s->blame);
3348 s->done = 0;
3349 if (thread_err)
3350 break;
3351 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3352 got_object_qid_free(s->blamed_commit);
3353 s->blamed_commit =
3354 SIMPLEQ_FIRST(&s->blamed_commits);
3355 err = run_blame(&s->blame, view, &s->blame_complete,
3356 &s->first_displayed_line, &s->last_displayed_line,
3357 &s->selected_line, &s->done, &s->eof, s->path,
3358 s->blamed_commit->id, s->repo);
3359 if (err)
3360 break;
3361 break;
3363 case KEY_ENTER:
3364 case '\r': {
3365 struct got_object_id *id = NULL;
3366 struct got_object_qid *pid;
3367 struct got_commit_object *commit = NULL;
3368 id = get_selected_commit_id(s->blame.lines,
3369 s->first_displayed_line, s->selected_line);
3370 if (id == NULL)
3371 break;
3372 err = got_object_open_as_commit(&commit, s->repo, id);
3373 if (err)
3374 break;
3375 pid = SIMPLEQ_FIRST(
3376 got_object_commit_get_parent_ids(commit));
3377 if (view_is_parent_view(view))
3378 begin_x = view_split_begin_x(view->begin_x);
3379 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3380 if (diff_view == NULL) {
3381 got_object_commit_close(commit);
3382 err = got_error_from_errno("view_open");
3383 break;
3385 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3386 id, NULL, s->refs, s->repo);
3387 got_object_commit_close(commit);
3388 if (err) {
3389 view_close(diff_view);
3390 break;
3392 if (view_is_parent_view(view)) {
3393 err = view_close_child(view);
3394 if (err)
3395 break;
3396 err = view_set_child(view, diff_view);
3397 if (err) {
3398 view_close(diff_view);
3399 break;
3401 *focus_view = diff_view;
3402 view->child_focussed = 1;
3403 } else
3404 *new_view = diff_view;
3405 if (err)
3406 break;
3407 break;
3409 case KEY_NPAGE:
3410 case ' ':
3411 if (s->last_displayed_line >= s->blame.nlines &&
3412 s->selected_line >= MIN(s->blame.nlines,
3413 view->nlines - 2)) {
3414 break;
3416 if (s->last_displayed_line >= s->blame.nlines &&
3417 s->selected_line < view->nlines - 2) {
3418 s->selected_line = MIN(s->blame.nlines,
3419 view->nlines - 2);
3420 break;
3422 if (s->last_displayed_line + view->nlines - 2
3423 <= s->blame.nlines)
3424 s->first_displayed_line +=
3425 view->nlines - 2;
3426 else
3427 s->first_displayed_line =
3428 s->blame.nlines -
3429 (view->nlines - 3);
3430 break;
3431 case KEY_RESIZE:
3432 if (s->selected_line > view->nlines - 2) {
3433 s->selected_line = MIN(s->blame.nlines,
3434 view->nlines - 2);
3436 break;
3437 default:
3438 break;
3440 return thread_err ? thread_err : err;
3443 static const struct got_error *
3444 cmd_blame(int argc, char *argv[])
3446 const struct got_error *error;
3447 struct got_repository *repo = NULL;
3448 struct got_reflist_head refs;
3449 struct got_worktree *worktree = NULL;
3450 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3451 struct got_object_id *commit_id = NULL;
3452 char *commit_id_str = NULL;
3453 int ch;
3454 struct tog_view *view;
3456 SIMPLEQ_INIT(&refs);
3458 #ifndef PROFILE
3459 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3460 NULL) == -1)
3461 err(1, "pledge");
3462 #endif
3464 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3465 switch (ch) {
3466 case 'c':
3467 commit_id_str = optarg;
3468 break;
3469 case 'r':
3470 repo_path = realpath(optarg, NULL);
3471 if (repo_path == NULL)
3472 err(1, "-r option");
3473 break;
3474 default:
3475 usage_blame();
3476 /* NOTREACHED */
3480 argc -= optind;
3481 argv += optind;
3483 if (argc == 1)
3484 path = argv[0];
3485 else
3486 usage_blame();
3488 cwd = getcwd(NULL, 0);
3489 if (cwd == NULL) {
3490 error = got_error_from_errno("getcwd");
3491 goto done;
3493 if (repo_path == NULL) {
3494 error = got_worktree_open(&worktree, cwd);
3495 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3496 goto done;
3497 else
3498 error = NULL;
3499 if (worktree) {
3500 repo_path =
3501 strdup(got_worktree_get_repo_path(worktree));
3502 if (repo_path == NULL)
3503 error = got_error_from_errno("strdup");
3504 if (error)
3505 goto done;
3506 } else {
3507 repo_path = strdup(cwd);
3508 if (repo_path == NULL) {
3509 error = got_error_from_errno("strdup");
3510 goto done;
3515 init_curses();
3517 error = got_repo_open(&repo, repo_path);
3518 if (error != NULL)
3519 goto done;
3521 error = apply_unveil(got_repo_get_path(repo), NULL);
3522 if (error)
3523 goto done;
3525 if (worktree) {
3526 const char *prefix = got_worktree_get_path_prefix(worktree);
3527 char *p, *worktree_subdir = cwd +
3528 strlen(got_worktree_get_root_path(worktree));
3529 if (asprintf(&p, "%s%s%s%s%s",
3530 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3531 worktree_subdir, worktree_subdir[0] ? "/" : "",
3532 path) == -1) {
3533 error = got_error_from_errno("asprintf");
3534 goto done;
3536 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3537 free(p);
3538 } else {
3539 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3541 if (error)
3542 goto done;
3544 if (commit_id_str == NULL) {
3545 struct got_reference *head_ref;
3546 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3547 if (error != NULL)
3548 goto done;
3549 error = got_ref_resolve(&commit_id, repo, head_ref);
3550 got_ref_close(head_ref);
3551 } else {
3552 error = got_object_resolve_id_str(&commit_id, repo,
3553 commit_id_str);
3555 if (error != NULL)
3556 goto done;
3558 error = got_ref_list(&refs, repo);
3559 if (error)
3560 goto done;
3562 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3563 if (view == NULL) {
3564 error = got_error_from_errno("view_open");
3565 goto done;
3567 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3568 if (error)
3569 goto done;
3570 error = view_loop(view);
3571 done:
3572 free(repo_path);
3573 free(cwd);
3574 free(commit_id);
3575 if (worktree)
3576 got_worktree_close(worktree);
3577 if (repo)
3578 got_repo_close(repo);
3579 got_ref_list_free(&refs);
3580 return error;
3583 static const struct got_error *
3584 draw_tree_entries(struct tog_view *view,
3585 struct got_tree_entry **first_displayed_entry,
3586 struct got_tree_entry **last_displayed_entry,
3587 struct got_tree_entry **selected_entry, int *ndisplayed,
3588 const char *label, int show_ids, const char *parent_path,
3589 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3591 const struct got_error *err = NULL;
3592 struct got_tree_entry *te;
3593 wchar_t *wline;
3594 int width, n;
3596 *ndisplayed = 0;
3598 werase(view->window);
3600 if (limit == 0)
3601 return NULL;
3603 err = format_line(&wline, &width, label, view->ncols);
3604 if (err)
3605 return err;
3606 if (view_needs_focus_indication(view))
3607 wstandout(view->window);
3608 waddwstr(view->window, wline);
3609 if (view_needs_focus_indication(view))
3610 wstandend(view->window);
3611 free(wline);
3612 wline = NULL;
3613 if (width < view->ncols - 1)
3614 waddch(view->window, '\n');
3615 if (--limit <= 0)
3616 return NULL;
3617 err = format_line(&wline, &width, parent_path, view->ncols);
3618 if (err)
3619 return err;
3620 waddwstr(view->window, wline);
3621 free(wline);
3622 wline = NULL;
3623 if (width < view->ncols - 1)
3624 waddch(view->window, '\n');
3625 if (--limit <= 0)
3626 return NULL;
3627 waddch(view->window, '\n');
3628 if (--limit <= 0)
3629 return NULL;
3631 te = SIMPLEQ_FIRST(&entries->head);
3632 if (*first_displayed_entry == NULL) {
3633 if (selected == 0) {
3634 if (view->focussed)
3635 wstandout(view->window);
3636 *selected_entry = NULL;
3638 waddstr(view->window, " ..\n"); /* parent directory */
3639 if (selected == 0 && view->focussed)
3640 wstandend(view->window);
3641 (*ndisplayed)++;
3642 if (--limit <= 0)
3643 return NULL;
3644 n = 1;
3645 } else {
3646 n = 0;
3647 while (te != *first_displayed_entry)
3648 te = SIMPLEQ_NEXT(te, entry);
3651 while (te) {
3652 char *line = NULL, *id_str = NULL;
3654 if (show_ids) {
3655 err = got_object_id_str(&id_str, te->id);
3656 if (err)
3657 return got_error_from_errno(
3658 "got_object_id_str");
3660 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3661 te->name, S_ISDIR(te->mode) ? "/" :
3662 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3663 free(id_str);
3664 return got_error_from_errno("asprintf");
3666 free(id_str);
3667 err = format_line(&wline, &width, line, view->ncols);
3668 if (err) {
3669 free(line);
3670 break;
3672 if (n == selected) {
3673 if (view->focussed)
3674 wstandout(view->window);
3675 *selected_entry = te;
3677 waddwstr(view->window, wline);
3678 if (width < view->ncols - 1)
3679 waddch(view->window, '\n');
3680 if (n == selected && view->focussed)
3681 wstandend(view->window);
3682 free(line);
3683 free(wline);
3684 wline = NULL;
3685 n++;
3686 (*ndisplayed)++;
3687 *last_displayed_entry = te;
3688 if (--limit <= 0)
3689 break;
3690 te = SIMPLEQ_NEXT(te, entry);
3693 return err;
3696 static void
3697 tree_scroll_up(struct tog_view *view,
3698 struct got_tree_entry **first_displayed_entry, int maxscroll,
3699 const struct got_tree_entries *entries, int isroot)
3701 struct got_tree_entry *te, *prev;
3702 int i;
3704 if (*first_displayed_entry == NULL)
3705 return;
3707 te = SIMPLEQ_FIRST(&entries->head);
3708 if (*first_displayed_entry == te) {
3709 if (!isroot)
3710 *first_displayed_entry = NULL;
3711 return;
3714 /* XXX this is stupid... switch to TAILQ? */
3715 for (i = 0; i < maxscroll; i++) {
3716 while (te != *first_displayed_entry) {
3717 prev = te;
3718 te = SIMPLEQ_NEXT(te, entry);
3720 *first_displayed_entry = prev;
3721 te = SIMPLEQ_FIRST(&entries->head);
3723 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3724 *first_displayed_entry = NULL;
3727 static int
3728 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3729 struct got_tree_entry *last_displayed_entry,
3730 const struct got_tree_entries *entries)
3732 struct got_tree_entry *next, *last;
3733 int n = 0;
3735 if (*first_displayed_entry)
3736 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3737 else
3738 next = SIMPLEQ_FIRST(&entries->head);
3739 last = last_displayed_entry;
3740 while (next && last && n++ < maxscroll) {
3741 last = SIMPLEQ_NEXT(last, entry);
3742 if (last) {
3743 *first_displayed_entry = next;
3744 next = SIMPLEQ_NEXT(next, entry);
3747 return n;
3750 static const struct got_error *
3751 tree_entry_path(char **path, struct tog_parent_trees *parents,
3752 struct got_tree_entry *te)
3754 const struct got_error *err = NULL;
3755 struct tog_parent_tree *pt;
3756 size_t len = 2; /* for leading slash and NUL */
3758 TAILQ_FOREACH(pt, parents, entry)
3759 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3760 if (te)
3761 len += strlen(te->name);
3763 *path = calloc(1, len);
3764 if (path == NULL)
3765 return got_error_from_errno("calloc");
3767 (*path)[0] = '/';
3768 pt = TAILQ_LAST(parents, tog_parent_trees);
3769 while (pt) {
3770 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3771 err = got_error(GOT_ERR_NO_SPACE);
3772 goto done;
3774 if (strlcat(*path, "/", len) >= len) {
3775 err = got_error(GOT_ERR_NO_SPACE);
3776 goto done;
3778 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3780 if (te) {
3781 if (strlcat(*path, te->name, len) >= len) {
3782 err = got_error(GOT_ERR_NO_SPACE);
3783 goto done;
3786 done:
3787 if (err) {
3788 free(*path);
3789 *path = NULL;
3791 return err;
3794 static const struct got_error *
3795 blame_tree_entry(struct tog_view **new_view, int begin_x,
3796 struct got_tree_entry *te, struct tog_parent_trees *parents,
3797 struct got_object_id *commit_id, struct got_reflist_head *refs,
3798 struct got_repository *repo)
3800 const struct got_error *err = NULL;
3801 char *path;
3802 struct tog_view *blame_view;
3804 err = tree_entry_path(&path, parents, te);
3805 if (err)
3806 return err;
3808 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3809 if (blame_view == NULL)
3810 return got_error_from_errno("view_open");
3812 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3813 if (err) {
3814 view_close(blame_view);
3815 free(path);
3816 } else
3817 *new_view = blame_view;
3818 return err;
3821 static const struct got_error *
3822 log_tree_entry(struct tog_view **new_view, int begin_x,
3823 struct got_tree_entry *te, struct tog_parent_trees *parents,
3824 struct got_object_id *commit_id, struct got_reflist_head *refs,
3825 struct got_repository *repo)
3827 struct tog_view *log_view;
3828 const struct got_error *err = NULL;
3829 char *path;
3831 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3832 if (log_view == NULL)
3833 return got_error_from_errno("view_open");
3835 err = tree_entry_path(&path, parents, te);
3836 if (err)
3837 return err;
3839 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3840 if (err)
3841 view_close(log_view);
3842 else
3843 *new_view = log_view;
3844 free(path);
3845 return err;
3848 static const struct got_error *
3849 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3850 struct got_object_id *commit_id, struct got_reflist_head *refs,
3851 struct got_repository *repo)
3853 const struct got_error *err = NULL;
3854 char *commit_id_str = NULL;
3855 struct tog_tree_view_state *s = &view->state.tree;
3857 TAILQ_INIT(&s->parents);
3859 err = got_object_id_str(&commit_id_str, commit_id);
3860 if (err != NULL)
3861 goto done;
3863 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3864 err = got_error_from_errno("asprintf");
3865 goto done;
3868 s->root = s->tree = root;
3869 s->entries = got_object_tree_get_entries(root);
3870 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3871 s->commit_id = got_object_id_dup(commit_id);
3872 if (s->commit_id == NULL) {
3873 err = got_error_from_errno("got_object_id_dup");
3874 goto done;
3876 s->refs = refs;
3877 s->repo = repo;
3879 view->show = show_tree_view;
3880 view->input = input_tree_view;
3881 view->close = close_tree_view;
3882 done:
3883 free(commit_id_str);
3884 if (err) {
3885 free(s->tree_label);
3886 s->tree_label = NULL;
3888 return err;
3891 static const struct got_error *
3892 close_tree_view(struct tog_view *view)
3894 struct tog_tree_view_state *s = &view->state.tree;
3896 free(s->tree_label);
3897 s->tree_label = NULL;
3898 free(s->commit_id);
3899 s->commit_id = NULL;
3900 while (!TAILQ_EMPTY(&s->parents)) {
3901 struct tog_parent_tree *parent;
3902 parent = TAILQ_FIRST(&s->parents);
3903 TAILQ_REMOVE(&s->parents, parent, entry);
3904 free(parent);
3907 if (s->tree != s->root)
3908 got_object_tree_close(s->tree);
3909 got_object_tree_close(s->root);
3911 return NULL;
3914 static const struct got_error *
3915 show_tree_view(struct tog_view *view)
3917 const struct got_error *err = NULL;
3918 struct tog_tree_view_state *s = &view->state.tree;
3919 char *parent_path;
3921 err = tree_entry_path(&parent_path, &s->parents, NULL);
3922 if (err)
3923 return err;
3925 err = draw_tree_entries(view, &s->first_displayed_entry,
3926 &s->last_displayed_entry, &s->selected_entry,
3927 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3928 s->entries, s->selected, view->nlines, s->tree == s->root);
3929 free(parent_path);
3931 view_vborder(view);
3932 return err;
3935 static const struct got_error *
3936 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3937 struct tog_view **focus_view, struct tog_view *view, int ch)
3939 const struct got_error *err = NULL;
3940 struct tog_tree_view_state *s = &view->state.tree;
3941 struct tog_view *log_view;
3942 int begin_x = 0, nscrolled;
3944 switch (ch) {
3945 case 'i':
3946 s->show_ids = !s->show_ids;
3947 break;
3948 case 'l':
3949 if (!s->selected_entry)
3950 break;
3951 if (view_is_parent_view(view))
3952 begin_x = view_split_begin_x(view->begin_x);
3953 err = log_tree_entry(&log_view, begin_x,
3954 s->selected_entry, &s->parents,
3955 s->commit_id, s->refs, s->repo);
3956 if (view_is_parent_view(view)) {
3957 err = view_close_child(view);
3958 if (err)
3959 return err;
3960 err = view_set_child(view, log_view);
3961 if (err) {
3962 view_close(log_view);
3963 break;
3965 *focus_view = log_view;
3966 view->child_focussed = 1;
3967 } else
3968 *new_view = log_view;
3969 break;
3970 case 'k':
3971 case KEY_UP:
3972 if (s->selected > 0) {
3973 s->selected--;
3974 if (s->selected == 0)
3975 break;
3977 if (s->selected > 0)
3978 break;
3979 tree_scroll_up(view, &s->first_displayed_entry, 1,
3980 s->entries, s->tree == s->root);
3981 break;
3982 case KEY_PPAGE:
3983 tree_scroll_up(view, &s->first_displayed_entry,
3984 MAX(0, view->nlines - 4 - s->selected), s->entries,
3985 s->tree == s->root);
3986 s->selected = 0;
3987 if (SIMPLEQ_FIRST(&s->entries->head) ==
3988 s->first_displayed_entry && s->tree != s->root)
3989 s->first_displayed_entry = NULL;
3990 break;
3991 case 'j':
3992 case KEY_DOWN:
3993 if (s->selected < s->ndisplayed - 1) {
3994 s->selected++;
3995 break;
3997 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3998 /* can't scroll any further */
3999 break;
4000 tree_scroll_down(&s->first_displayed_entry, 1,
4001 s->last_displayed_entry, s->entries);
4002 break;
4003 case KEY_NPAGE:
4004 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4005 == NULL) {
4006 /* can't scroll any further; move cursor down */
4007 if (s->selected < s->ndisplayed - 1)
4008 s->selected = s->ndisplayed - 1;
4009 break;
4011 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4012 view->nlines, s->last_displayed_entry, s->entries);
4013 if (nscrolled < view->nlines) {
4014 int ndisplayed = 0;
4015 struct got_tree_entry *te;
4016 te = s->first_displayed_entry;
4017 do {
4018 ndisplayed++;
4019 te = SIMPLEQ_NEXT(te, entry);
4020 } while (te);
4021 s->selected = ndisplayed - 1;
4023 break;
4024 case KEY_ENTER:
4025 case '\r':
4026 case KEY_BACKSPACE:
4027 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4028 struct tog_parent_tree *parent;
4029 /* user selected '..' */
4030 if (s->tree == s->root)
4031 break;
4032 parent = TAILQ_FIRST(&s->parents);
4033 TAILQ_REMOVE(&s->parents, parent,
4034 entry);
4035 got_object_tree_close(s->tree);
4036 s->tree = parent->tree;
4037 s->entries =
4038 got_object_tree_get_entries(s->tree);
4039 s->first_displayed_entry =
4040 parent->first_displayed_entry;
4041 s->selected_entry =
4042 parent->selected_entry;
4043 s->selected = parent->selected;
4044 free(parent);
4045 } else if (S_ISDIR(s->selected_entry->mode)) {
4046 struct got_tree_object *subtree;
4047 err = got_object_open_as_tree(&subtree,
4048 s->repo, s->selected_entry->id);
4049 if (err)
4050 break;
4051 err = tree_view_visit_subtree(subtree, s);
4052 if (err) {
4053 got_object_tree_close(subtree);
4054 break;
4056 } else if (S_ISREG(s->selected_entry->mode)) {
4057 struct tog_view *blame_view;
4058 int begin_x = view_is_parent_view(view) ?
4059 view_split_begin_x(view->begin_x) : 0;
4061 err = blame_tree_entry(&blame_view, begin_x,
4062 s->selected_entry, &s->parents,
4063 s->commit_id, s->refs, s->repo);
4064 if (err)
4065 break;
4066 if (view_is_parent_view(view)) {
4067 err = view_close_child(view);
4068 if (err)
4069 return err;
4070 err = view_set_child(view, blame_view);
4071 if (err) {
4072 view_close(blame_view);
4073 break;
4075 *focus_view = blame_view;
4076 view->child_focussed = 1;
4077 } else
4078 *new_view = blame_view;
4080 break;
4081 case KEY_RESIZE:
4082 if (s->selected > view->nlines)
4083 s->selected = s->ndisplayed - 1;
4084 break;
4085 default:
4086 break;
4089 return err;
4092 __dead static void
4093 usage_tree(void)
4095 endwin();
4096 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4097 getprogname());
4098 exit(1);
4101 static const struct got_error *
4102 cmd_tree(int argc, char *argv[])
4104 const struct got_error *error;
4105 struct got_repository *repo = NULL;
4106 struct got_reflist_head refs;
4107 char *repo_path = NULL;
4108 struct got_object_id *commit_id = NULL;
4109 char *commit_id_arg = NULL;
4110 struct got_commit_object *commit = NULL;
4111 struct got_tree_object *tree = NULL;
4112 int ch;
4113 struct tog_view *view;
4115 SIMPLEQ_INIT(&refs);
4117 #ifndef PROFILE
4118 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4119 NULL) == -1)
4120 err(1, "pledge");
4121 #endif
4123 while ((ch = getopt(argc, argv, "c:")) != -1) {
4124 switch (ch) {
4125 case 'c':
4126 commit_id_arg = optarg;
4127 break;
4128 default:
4129 usage_tree();
4130 /* NOTREACHED */
4134 argc -= optind;
4135 argv += optind;
4137 if (argc == 0) {
4138 struct got_worktree *worktree;
4139 char *cwd = getcwd(NULL, 0);
4140 if (cwd == NULL)
4141 return got_error_from_errno("getcwd");
4142 error = got_worktree_open(&worktree, cwd);
4143 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4144 goto done;
4145 if (worktree) {
4146 free(cwd);
4147 repo_path =
4148 strdup(got_worktree_get_repo_path(worktree));
4149 got_worktree_close(worktree);
4150 } else
4151 repo_path = cwd;
4152 if (repo_path == NULL) {
4153 error = got_error_from_errno("strdup");
4154 goto done;
4156 } else if (argc == 1) {
4157 repo_path = realpath(argv[0], NULL);
4158 if (repo_path == NULL)
4159 return got_error_from_errno2("realpath", argv[0]);
4160 } else
4161 usage_log();
4163 init_curses();
4165 error = got_repo_open(&repo, repo_path);
4166 if (error != NULL)
4167 goto done;
4169 error = apply_unveil(got_repo_get_path(repo), NULL);
4170 if (error)
4171 goto done;
4173 if (commit_id_arg == NULL)
4174 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4175 else
4176 error = got_object_resolve_id_str(&commit_id, repo,
4177 commit_id_arg);
4178 if (error != NULL)
4179 goto done;
4181 error = got_object_open_as_commit(&commit, repo, commit_id);
4182 if (error != NULL)
4183 goto done;
4185 error = got_object_open_as_tree(&tree, repo,
4186 got_object_commit_get_tree_id(commit));
4187 if (error != NULL)
4188 goto done;
4190 error = got_ref_list(&refs, repo);
4191 if (error)
4192 goto done;
4194 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4195 if (view == NULL) {
4196 error = got_error_from_errno("view_open");
4197 goto done;
4199 error = open_tree_view(view, tree, commit_id, &refs, repo);
4200 if (error)
4201 goto done;
4202 error = view_loop(view);
4203 done:
4204 free(repo_path);
4205 free(commit_id);
4206 if (commit)
4207 got_object_commit_close(commit);
4208 if (tree)
4209 got_object_tree_close(tree);
4210 if (repo)
4211 got_repo_close(repo);
4212 got_ref_list_free(&refs);
4213 return error;
4216 __dead static void
4217 usage(void)
4219 int i;
4221 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4222 "Available commands:\n", getprogname());
4223 for (i = 0; i < nitems(tog_commands); i++) {
4224 struct tog_cmd *cmd = &tog_commands[i];
4225 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4227 exit(1);
4230 static char **
4231 make_argv(const char *arg0, const char *arg1)
4233 char **argv;
4234 int argc = (arg1 == NULL ? 1 : 2);
4236 argv = calloc(argc, sizeof(char *));
4237 if (argv == NULL)
4238 err(1, "calloc");
4239 argv[0] = strdup(arg0);
4240 if (argv[0] == NULL)
4241 err(1, "calloc");
4242 if (arg1) {
4243 argv[1] = strdup(arg1);
4244 if (argv[1] == NULL)
4245 err(1, "calloc");
4248 return argv;
4251 int
4252 main(int argc, char *argv[])
4254 const struct got_error *error = NULL;
4255 struct tog_cmd *cmd = NULL;
4256 int ch, hflag = 0;
4257 char **cmd_argv = NULL;
4259 setlocale(LC_CTYPE, "");
4261 while ((ch = getopt(argc, argv, "h")) != -1) {
4262 switch (ch) {
4263 case 'h':
4264 hflag = 1;
4265 break;
4266 default:
4267 usage();
4268 /* NOTREACHED */
4272 argc -= optind;
4273 argv += optind;
4274 optind = 0;
4275 optreset = 1;
4277 if (argc == 0) {
4278 if (hflag)
4279 usage();
4280 /* Build an argument vector which runs a default command. */
4281 cmd = &tog_commands[0];
4282 cmd_argv = make_argv(cmd->name, NULL);
4283 argc = 1;
4284 } else {
4285 int i;
4287 /* Did the user specific a command? */
4288 for (i = 0; i < nitems(tog_commands); i++) {
4289 if (strncmp(tog_commands[i].name, argv[0],
4290 strlen(argv[0])) == 0) {
4291 cmd = &tog_commands[i];
4292 if (hflag)
4293 tog_commands[i].cmd_usage();
4294 break;
4297 if (cmd == NULL) {
4298 /* Did the user specify a repository? */
4299 char *repo_path = realpath(argv[0], NULL);
4300 if (repo_path) {
4301 struct got_repository *repo;
4302 error = got_repo_open(&repo, repo_path);
4303 if (error == NULL)
4304 got_repo_close(repo);
4305 } else
4306 error = got_error_from_errno2("realpath",
4307 argv[0]);
4308 if (error) {
4309 if (hflag) {
4310 fprintf(stderr, "%s: '%s' is not a "
4311 "known command\n", getprogname(),
4312 argv[0]);
4313 usage();
4315 fprintf(stderr, "%s: '%s' is neither a known "
4316 "command nor a path to a repository\n",
4317 getprogname(), argv[0]);
4318 free(repo_path);
4319 return 1;
4321 cmd = &tog_commands[0];
4322 cmd_argv = make_argv(cmd->name, repo_path);
4323 argc = 2;
4324 free(repo_path);
4328 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4329 if (error)
4330 goto done;
4331 done:
4332 endwin();
4333 free(cmd_argv);
4334 if (error)
4335 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4336 return 0;