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 return NULL;
552 nodelay(stdscr, FALSE);
553 /* Allow threads to make progress while we are waiting for input. */
554 errcode = pthread_mutex_unlock(&tog_mutex);
555 if (errcode)
556 return got_error_set_errno(errcode, "pthread_mutex_unlock");
557 ch = wgetch(view->window);
558 errcode = pthread_mutex_lock(&tog_mutex);
559 if (errcode)
560 return got_error_set_errno(errcode, "pthread_mutex_lock");
561 nodelay(stdscr, TRUE);
563 if (tog_sigwinch_received) {
564 tog_resizeterm();
565 tog_sigwinch_received = 0;
566 TAILQ_FOREACH(v, views, entry) {
567 err = view_resize(v);
568 if (err)
569 return err;
570 err = v->input(new, dead, focus, v, KEY_RESIZE);
571 if (err)
572 return err;
576 switch (ch) {
577 case ERR:
578 break;
579 case '\t':
580 if (view->child) {
581 *focus = view->child;
582 view->child_focussed = 1;
583 } else if (view->parent) {
584 *focus = view->parent;
585 view->parent->child_focussed = 0;
587 break;
588 case 'q':
589 err = view->input(new, dead, focus, view, ch);
590 *dead = view;
591 break;
592 case 'Q':
593 *done = 1;
594 break;
595 case 'f':
596 if (view_is_parent_view(view)) {
597 if (view->child == NULL)
598 break;
599 if (view_is_splitscreen(view->child)) {
600 *focus = view->child;
601 view->child_focussed = 1;
602 err = view_fullscreen(view->child);
603 } else
604 err = view_splitscreen(view->child);
605 if (err)
606 break;
607 err = view->child->input(new, dead, focus,
608 view->child, KEY_RESIZE);
609 } else {
610 if (view_is_splitscreen(view)) {
611 *focus = view;
612 view->parent->child_focussed = 1;
613 err = view_fullscreen(view);
614 } else {
615 err = view_splitscreen(view);
617 if (err)
618 break;
619 err = view->input(new, dead, focus, view,
620 KEY_RESIZE);
622 break;
623 case KEY_RESIZE:
624 break;
625 case '/':
626 if (view->search_start)
627 view->search_start(view);
628 else
629 err = view->input(new, dead, focus, view, ch);
630 break;
631 case 'N':
632 case 'n':
633 if (view->search_next && view->searching) {
634 view->searching = (ch == 'n' ?
635 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
636 view->search_next_done = 0;
637 view->search_next(view);
638 } else
639 err = view->input(new, dead, focus, view, ch);
640 break;
641 default:
642 err = view->input(new, dead, focus, view, ch);
643 break;
646 return err;
649 void
650 view_vborder(struct tog_view *view)
652 PANEL *panel;
653 struct tog_view *view_above;
655 if (view->parent)
656 return view_vborder(view->parent);
658 panel = panel_above(view->panel);
659 if (panel == NULL)
660 return;
662 view_above = panel_userptr(panel);
663 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
664 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
667 int
668 view_needs_focus_indication(struct tog_view *view)
670 if (view_is_parent_view(view)) {
671 if (view->child == NULL || view->child_focussed)
672 return 0;
673 if (!view_is_splitscreen(view->child))
674 return 0;
675 } else if (!view_is_splitscreen(view))
676 return 0;
678 return view->focussed;
681 static const struct got_error *
682 view_loop(struct tog_view *view)
684 const struct got_error *err = NULL;
685 struct tog_view_list_head views;
686 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
687 int fast_refresh = 10;
688 int done = 0, errcode;
690 errcode = pthread_mutex_lock(&tog_mutex);
691 if (errcode)
692 return got_error_set_errno(errcode, "pthread_mutex_lock");
694 TAILQ_INIT(&views);
695 TAILQ_INSERT_HEAD(&views, view, entry);
697 main_view = view;
698 view->focussed = 1;
699 err = view->show(view);
700 if (err)
701 return err;
702 update_panels();
703 doupdate();
704 while (!TAILQ_EMPTY(&views) && !done) {
705 /* Refresh fast during initialization, then become slower. */
706 if (fast_refresh && fast_refresh-- == 0)
707 halfdelay(10); /* switch to once per second */
709 err = view_input(&new_view, &dead_view, &focus_view, &done,
710 view, &views);
711 if (err)
712 break;
713 if (dead_view) {
714 struct tog_view *prev = NULL;
716 if (view_is_parent_view(dead_view))
717 prev = TAILQ_PREV(dead_view,
718 tog_view_list_head, entry);
719 else if (view->parent != dead_view)
720 prev = view->parent;
722 if (dead_view->parent)
723 dead_view->parent->child = NULL;
724 else
725 TAILQ_REMOVE(&views, dead_view, entry);
727 err = view_close(dead_view);
728 if (err || dead_view == main_view)
729 goto done;
731 if (view == dead_view) {
732 if (focus_view)
733 view = focus_view;
734 else if (prev)
735 view = prev;
736 else if (!TAILQ_EMPTY(&views))
737 view = TAILQ_LAST(&views,
738 tog_view_list_head);
739 else
740 view = NULL;
741 if (view) {
742 if (view->child && view->child_focussed)
743 focus_view = view->child;
744 else
745 focus_view = view;
749 if (new_view) {
750 struct tog_view *v, *t;
751 /* Only allow one parent view per type. */
752 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
753 if (v->type != new_view->type)
754 continue;
755 TAILQ_REMOVE(&views, v, entry);
756 err = view_close(v);
757 if (err)
758 goto done;
759 break;
761 TAILQ_INSERT_TAIL(&views, new_view, entry);
762 view = new_view;
763 if (focus_view == NULL)
764 focus_view = new_view;
766 if (focus_view) {
767 show_panel(focus_view->panel);
768 if (view)
769 view->focussed = 0;
770 focus_view->focussed = 1;
771 view = focus_view;
772 if (new_view)
773 show_panel(new_view->panel);
774 if (view->child && view_is_splitscreen(view->child))
775 show_panel(view->child->panel);
777 if (view) {
778 if (focus_view == NULL) {
779 view->focussed = 1;
780 show_panel(view->panel);
781 if (view->child && view_is_splitscreen(view->child))
782 show_panel(view->child->panel);
783 focus_view = view;
785 if (view->parent) {
786 err = view->parent->show(view->parent);
787 if (err)
788 goto done;
790 err = view->show(view);
791 if (err)
792 goto done;
793 if (view->child) {
794 err = view->child->show(view->child);
795 if (err)
796 goto done;
798 update_panels();
799 doupdate();
802 done:
803 while (!TAILQ_EMPTY(&views)) {
804 view = TAILQ_FIRST(&views);
805 TAILQ_REMOVE(&views, view, entry);
806 view_close(view);
809 errcode = pthread_mutex_unlock(&tog_mutex);
810 if (errcode)
811 return got_error_set_errno(errcode, "pthread_mutex_unlock");
813 return err;
816 __dead static void
817 usage_log(void)
819 endwin();
820 fprintf(stderr,
821 "usage: %s log [-c commit] [-r repository-path] [path]\n",
822 getprogname());
823 exit(1);
826 /* Create newly allocated wide-character string equivalent to a byte string. */
827 static const struct got_error *
828 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
830 char *vis = NULL;
831 const struct got_error *err = NULL;
833 *ws = NULL;
834 *wlen = mbstowcs(NULL, s, 0);
835 if (*wlen == (size_t)-1) {
836 int vislen;
837 if (errno != EILSEQ)
838 return got_error_from_errno("mbstowcs");
840 /* byte string invalid in current encoding; try to "fix" it */
841 err = got_mbsavis(&vis, &vislen, s);
842 if (err)
843 return err;
844 *wlen = mbstowcs(NULL, vis, 0);
845 if (*wlen == (size_t)-1) {
846 err = got_error_from_errno("mbstowcs"); /* give up */
847 goto done;
851 *ws = calloc(*wlen + 1, sizeof(*ws));
852 if (*ws == NULL) {
853 err = got_error_from_errno("calloc");
854 goto done;
857 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
858 err = got_error_from_errno("mbstowcs");
859 done:
860 free(vis);
861 if (err) {
862 free(*ws);
863 *ws = NULL;
864 *wlen = 0;
866 return err;
869 /* Format a line for display, ensuring that it won't overflow a width limit. */
870 static const struct got_error *
871 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
873 const struct got_error *err = NULL;
874 int cols = 0;
875 wchar_t *wline = NULL;
876 size_t wlen;
877 int i;
879 *wlinep = NULL;
880 *widthp = 0;
882 err = mbs2ws(&wline, &wlen, line);
883 if (err)
884 return err;
886 i = 0;
887 while (i < wlen && cols < wlimit) {
888 int width = wcwidth(wline[i]);
889 switch (width) {
890 case 0:
891 i++;
892 break;
893 case 1:
894 case 2:
895 if (cols + width <= wlimit)
896 cols += width;
897 i++;
898 break;
899 case -1:
900 if (wline[i] == L'\t')
901 cols += TABSIZE - ((cols + 1) % TABSIZE);
902 i++;
903 break;
904 default:
905 err = got_error_from_errno("wcwidth");
906 goto done;
909 wline[i] = L'\0';
910 if (widthp)
911 *widthp = cols;
912 done:
913 if (err)
914 free(wline);
915 else
916 *wlinep = wline;
917 return err;
920 static const struct got_error*
921 build_refs_str(char **refs_str, struct got_reflist_head *refs,
922 struct got_object_id *id)
924 static const struct got_error *err = NULL;
925 struct got_reflist_entry *re;
926 char *s;
927 const char *name;
929 *refs_str = NULL;
931 SIMPLEQ_FOREACH(re, refs, entry) {
932 if (got_object_id_cmp(re->id, id) != 0)
933 continue;
934 name = got_ref_get_name(re->ref);
935 if (strcmp(name, GOT_REF_HEAD) == 0)
936 continue;
937 if (strncmp(name, "refs/", 5) == 0)
938 name += 5;
939 if (strncmp(name, "got/", 4) == 0)
940 continue;
941 if (strncmp(name, "heads/", 6) == 0)
942 name += 6;
943 if (strncmp(name, "remotes/", 8) == 0)
944 name += 8;
945 s = *refs_str;
946 if (asprintf(refs_str, "%s%s%s", s ? s : "",
947 s ? ", " : "", name) == -1) {
948 err = got_error_from_errno("asprintf");
949 free(s);
950 *refs_str = NULL;
951 break;
953 free(s);
956 return err;
959 static const struct got_error *
960 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
962 char *smallerthan, *at;
964 smallerthan = strchr(author, '<');
965 if (smallerthan && smallerthan[1] != '\0')
966 author = smallerthan + 1;
967 at = strchr(author, '@');
968 if (at)
969 *at = '\0';
970 return format_line(wauthor, author_width, author, limit);
973 static const struct got_error *
974 draw_commit(struct tog_view *view, struct got_commit_object *commit,
975 struct got_object_id *id, struct got_reflist_head *refs,
976 int author_display_cols)
978 const struct got_error *err = NULL;
979 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
980 char *logmsg0 = NULL, *logmsg = NULL;
981 char *author = NULL;
982 wchar_t *wlogmsg = NULL, *wauthor = NULL;
983 int author_width, logmsg_width;
984 char *newline, *line = NULL;
985 int col, limit;
986 static const size_t date_display_cols = 9;
987 const int avail = view->ncols;
988 struct tm tm;
989 time_t committer_time;
991 committer_time = got_object_commit_get_committer_time(commit);
992 if (localtime_r(&committer_time, &tm) == NULL)
993 return got_error_from_errno("localtime_r");
994 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
995 >= sizeof(datebuf))
996 return got_error(GOT_ERR_NO_SPACE);
998 if (avail < date_display_cols)
999 limit = MIN(sizeof(datebuf) - 1, avail);
1000 else
1001 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1002 waddnstr(view->window, datebuf, limit);
1003 col = limit + 1;
1004 if (col > avail)
1005 goto done;
1007 author = strdup(got_object_commit_get_author(commit));
1008 if (author == NULL) {
1009 err = got_error_from_errno("strdup");
1010 goto done;
1012 err = format_author(&wauthor, &author_width, author, avail - col);
1013 if (err)
1014 goto done;
1015 waddwstr(view->window, wauthor);
1016 col += author_width;
1017 while (col <= avail && author_width < author_display_cols + 2) {
1018 waddch(view->window, ' ');
1019 col++;
1020 author_width++;
1022 if (col > avail)
1023 goto done;
1025 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1026 if (logmsg0 == NULL) {
1027 err = got_error_from_errno("strdup");
1028 goto done;
1030 logmsg = logmsg0;
1031 while (*logmsg == '\n')
1032 logmsg++;
1033 newline = strchr(logmsg, '\n');
1034 if (newline)
1035 *newline = '\0';
1036 limit = avail - col;
1037 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1038 if (err)
1039 goto done;
1040 waddwstr(view->window, wlogmsg);
1041 col += logmsg_width;
1042 while (col <= avail) {
1043 waddch(view->window, ' ');
1044 col++;
1046 done:
1047 free(logmsg0);
1048 free(wlogmsg);
1049 free(author);
1050 free(wauthor);
1051 free(line);
1052 return err;
1055 static struct commit_queue_entry *
1056 alloc_commit_queue_entry(struct got_commit_object *commit,
1057 struct got_object_id *id)
1059 struct commit_queue_entry *entry;
1061 entry = calloc(1, sizeof(*entry));
1062 if (entry == NULL)
1063 return NULL;
1065 entry->id = id;
1066 entry->commit = commit;
1067 return entry;
1070 static void
1071 pop_commit(struct commit_queue *commits)
1073 struct commit_queue_entry *entry;
1075 entry = TAILQ_FIRST(&commits->head);
1076 TAILQ_REMOVE(&commits->head, entry, entry);
1077 got_object_commit_close(entry->commit);
1078 commits->ncommits--;
1079 /* Don't free entry->id! It is owned by the commit graph. */
1080 free(entry);
1083 static void
1084 free_commits(struct commit_queue *commits)
1086 while (!TAILQ_EMPTY(&commits->head))
1087 pop_commit(commits);
1090 static const struct got_error *
1091 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1092 int minqueue, struct got_repository *repo, const char *path)
1094 const struct got_error *err = NULL;
1095 int nqueued = 0;
1098 * We keep all commits open throughout the lifetime of the log
1099 * view in order to avoid having to re-fetch commits from disk
1100 * while updating the display.
1102 while (nqueued < minqueue) {
1103 struct got_object_id *id;
1104 struct got_commit_object *commit;
1105 struct commit_queue_entry *entry;
1106 int errcode;
1108 err = got_commit_graph_iter_next(&id, graph);
1109 if (err) {
1110 if (err->code != GOT_ERR_ITER_NEED_MORE)
1111 break;
1112 err = got_commit_graph_fetch_commits(graph,
1113 minqueue, repo);
1114 if (err)
1115 return err;
1116 continue;
1119 if (id == NULL)
1120 break;
1122 err = got_object_open_as_commit(&commit, repo, id);
1123 if (err)
1124 break;
1125 entry = alloc_commit_queue_entry(commit, id);
1126 if (entry == NULL) {
1127 err = got_error_from_errno("alloc_commit_queue_entry");
1128 break;
1131 errcode = pthread_mutex_lock(&tog_mutex);
1132 if (errcode) {
1133 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1134 break;
1137 entry->idx = commits->ncommits;
1138 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1139 nqueued++;
1140 commits->ncommits++;
1142 errcode = pthread_mutex_unlock(&tog_mutex);
1143 if (errcode && err == NULL)
1144 err = got_error_set_errno(errcode,
1145 "pthread_mutex_unlock");
1148 return err;
1151 static const struct got_error *
1152 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1153 struct got_repository *repo)
1155 const struct got_error *err = NULL;
1156 struct got_reference *head_ref;
1158 *head_id = NULL;
1160 err = got_ref_open(&head_ref, repo, branch_name, 0);
1161 if (err)
1162 return err;
1164 err = got_ref_resolve(head_id, repo, head_ref);
1165 got_ref_close(head_ref);
1166 if (err) {
1167 *head_id = NULL;
1168 return err;
1171 return NULL;
1174 static const struct got_error *
1175 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1176 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1177 struct commit_queue *commits, int selected_idx, int limit,
1178 struct got_reflist_head *refs, const char *path, int commits_needed)
1180 const struct got_error *err = NULL;
1181 struct commit_queue_entry *entry;
1182 int width;
1183 int ncommits, author_cols = 10;
1184 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1185 char *refs_str = NULL;
1186 wchar_t *wline;
1188 entry = first;
1189 ncommits = 0;
1190 while (entry) {
1191 if (ncommits == selected_idx) {
1192 *selected = entry;
1193 break;
1195 entry = TAILQ_NEXT(entry, entry);
1196 ncommits++;
1199 if (*selected && !(view->searching && view->search_next_done == 0)) {
1200 err = got_object_id_str(&id_str, (*selected)->id);
1201 if (err)
1202 return err;
1203 if (refs) {
1204 err = build_refs_str(&refs_str, refs, (*selected)->id);
1205 if (err)
1206 goto done;
1210 if (commits_needed == 0)
1211 halfdelay(10); /* disable fast refresh */
1213 if (asprintf(&ncommits_str, " [%d/%d] %s",
1214 entry ? entry->idx + 1 : 0, commits->ncommits,
1215 commits_needed > 0 ?
1216 (view->searching && view->search_next_done == 0
1217 ? "searching..." : "loading... ") :
1218 (refs_str ? refs_str : "")) == -1) {
1219 err = got_error_from_errno("asprintf");
1220 goto done;
1223 if (path && strcmp(path, "/") != 0) {
1224 if (asprintf(&header, "commit %s %s%s",
1225 id_str ? id_str : "........................................",
1226 path, ncommits_str) == -1) {
1227 err = got_error_from_errno("asprintf");
1228 header = NULL;
1229 goto done;
1231 } else if (asprintf(&header, "commit %s%s",
1232 id_str ? id_str : "........................................",
1233 ncommits_str) == -1) {
1234 err = got_error_from_errno("asprintf");
1235 header = NULL;
1236 goto done;
1238 err = format_line(&wline, &width, header, view->ncols);
1239 if (err)
1240 goto done;
1242 werase(view->window);
1244 if (view_needs_focus_indication(view))
1245 wstandout(view->window);
1246 waddwstr(view->window, wline);
1247 while (width < view->ncols) {
1248 waddch(view->window, ' ');
1249 width++;
1251 if (view_needs_focus_indication(view))
1252 wstandend(view->window);
1253 free(wline);
1254 if (limit <= 1)
1255 goto done;
1257 /* Grow author column size if necessary. */
1258 entry = first;
1259 ncommits = 0;
1260 while (entry) {
1261 char *author;
1262 wchar_t *wauthor;
1263 int width;
1264 if (ncommits >= limit - 1)
1265 break;
1266 author = strdup(got_object_commit_get_author(entry->commit));
1267 if (author == NULL) {
1268 err = got_error_from_errno("strdup");
1269 goto done;
1271 err = format_author(&wauthor, &width, author, COLS);
1272 if (author_cols < width)
1273 author_cols = width;
1274 free(wauthor);
1275 free(author);
1276 entry = TAILQ_NEXT(entry, entry);
1279 entry = first;
1280 *last = first;
1281 ncommits = 0;
1282 while (entry) {
1283 if (ncommits >= limit - 1)
1284 break;
1285 if (ncommits == selected_idx)
1286 wstandout(view->window);
1287 err = draw_commit(view, entry->commit, entry->id, refs,
1288 author_cols);
1289 if (ncommits == selected_idx)
1290 wstandend(view->window);
1291 if (err)
1292 goto done;
1293 ncommits++;
1294 *last = entry;
1295 entry = TAILQ_NEXT(entry, entry);
1298 view_vborder(view);
1299 done:
1300 free(id_str);
1301 free(refs_str);
1302 free(ncommits_str);
1303 free(header);
1304 return err;
1307 static void
1308 scroll_up(struct tog_view *view,
1309 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1310 struct commit_queue *commits)
1312 struct commit_queue_entry *entry;
1313 int nscrolled = 0;
1315 entry = TAILQ_FIRST(&commits->head);
1316 if (*first_displayed_entry == entry)
1317 return;
1319 entry = *first_displayed_entry;
1320 while (entry && nscrolled < maxscroll) {
1321 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1322 if (entry) {
1323 *first_displayed_entry = entry;
1324 nscrolled++;
1329 static const struct got_error *
1330 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1331 pthread_cond_t *need_commits)
1333 int errcode;
1334 int max_wait = 20;
1336 halfdelay(1); /* fast refresh while loading commits */
1338 while (*commits_needed > 0) {
1339 if (*log_complete)
1340 break;
1342 /* Wake the log thread. */
1343 errcode = pthread_cond_signal(need_commits);
1344 if (errcode)
1345 return got_error_set_errno(errcode,
1346 "pthread_cond_signal");
1347 errcode = pthread_mutex_unlock(&tog_mutex);
1348 if (errcode)
1349 return got_error_set_errno(errcode,
1350 "pthread_mutex_unlock");
1351 pthread_yield();
1352 errcode = pthread_mutex_lock(&tog_mutex);
1353 if (errcode)
1354 return got_error_set_errno(errcode,
1355 "pthread_mutex_lock");
1357 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1359 * Thread is not done yet; lose a key press
1360 * and let the user retry... this way the GUI
1361 * remains interactive while logging deep paths
1362 * with few commits in history.
1364 return NULL;
1368 return NULL;
1371 static const struct got_error *
1372 scroll_down(struct tog_view *view,
1373 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1374 struct commit_queue_entry **last_displayed_entry,
1375 struct commit_queue *commits, int *log_complete, int *commits_needed,
1376 pthread_cond_t *need_commits)
1378 const struct got_error *err = NULL;
1379 struct commit_queue_entry *pentry;
1380 int nscrolled = 0;
1382 if (*last_displayed_entry == NULL)
1383 return NULL;
1385 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1386 if (pentry == NULL && !*log_complete) {
1388 * Ask the log thread for required amount of commits
1389 * plus some amount of pre-fetching.
1391 (*commits_needed) += maxscroll + 20;
1392 err = trigger_log_thread(0, commits_needed, log_complete,
1393 need_commits);
1394 if (err)
1395 return err;
1398 do {
1399 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1400 if (pentry == NULL)
1401 break;
1403 *last_displayed_entry = pentry;
1405 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1406 if (pentry == NULL)
1407 break;
1408 *first_displayed_entry = pentry;
1409 } while (++nscrolled < maxscroll);
1411 return err;
1414 static const struct got_error *
1415 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1416 struct got_commit_object *commit, struct got_object_id *commit_id,
1417 struct tog_view *log_view, struct got_reflist_head *refs,
1418 struct got_repository *repo)
1420 const struct got_error *err;
1421 struct got_object_qid *parent_id;
1422 struct tog_view *diff_view;
1424 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1425 if (diff_view == NULL)
1426 return got_error_from_errno("view_open");
1428 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1429 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1430 commit_id, log_view, refs, repo);
1431 if (err == NULL)
1432 *new_view = diff_view;
1433 return err;
1436 static const struct got_error *
1437 tree_view_visit_subtree(struct got_tree_object *subtree,
1438 struct tog_tree_view_state *s)
1440 struct tog_parent_tree *parent;
1442 parent = calloc(1, sizeof(*parent));
1443 if (parent == NULL)
1444 return got_error_from_errno("calloc");
1446 parent->tree = s->tree;
1447 parent->first_displayed_entry = s->first_displayed_entry;
1448 parent->selected_entry = s->selected_entry;
1449 parent->selected = s->selected;
1450 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1451 s->tree = subtree;
1452 s->entries = got_object_tree_get_entries(s->tree);
1453 s->selected = 0;
1454 s->first_displayed_entry = NULL;
1455 return NULL;
1459 static const struct got_error *
1460 browse_commit_tree(struct tog_view **new_view, int begin_x,
1461 struct commit_queue_entry *entry, const char *path,
1462 struct got_reflist_head *refs, struct got_repository *repo)
1464 const struct got_error *err = NULL;
1465 struct got_tree_object *tree;
1466 struct got_tree_entry *te;
1467 struct tog_tree_view_state *s;
1468 struct tog_view *tree_view;
1469 char *slash, *subpath = NULL;
1470 const char *p;
1472 err = got_object_open_as_tree(&tree, repo,
1473 got_object_commit_get_tree_id(entry->commit));
1474 if (err)
1475 return err;
1477 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1478 if (tree_view == NULL)
1479 return got_error_from_errno("view_open");
1481 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1482 if (err) {
1483 got_object_tree_close(tree);
1484 return err;
1486 s = &tree_view->state.tree;
1488 *new_view = tree_view;
1490 /* Walk the path and open corresponding tree objects. */
1491 p = path;
1492 while (*p) {
1493 struct got_object_id *tree_id;
1495 while (p[0] == '/')
1496 p++;
1498 /* Ensure the correct subtree entry is selected. */
1499 slash = strchr(p, '/');
1500 if (slash == NULL)
1501 slash = strchr(p, '\0');
1502 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1503 if (strncmp(p, te->name, slash - p) == 0) {
1504 s->selected_entry = te;
1505 break;
1507 s->selected++;
1509 if (s->selected_entry == NULL) {
1510 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1511 break;
1513 if (s->tree != s->root)
1514 s->selected++; /* skip '..' */
1516 if (!S_ISDIR(s->selected_entry->mode)) {
1517 /* Jump to this file's entry. */
1518 s->first_displayed_entry = s->selected_entry;
1519 s->selected = 0;
1520 break;
1523 slash = strchr(p, '/');
1524 if (slash)
1525 subpath = strndup(path, slash - path);
1526 else
1527 subpath = strdup(path);
1528 if (subpath == NULL) {
1529 err = got_error_from_errno("strdup");
1530 break;
1533 err = got_object_id_by_path(&tree_id, repo, entry->id,
1534 subpath);
1535 if (err)
1536 break;
1538 err = got_object_open_as_tree(&tree, repo, tree_id);
1539 free(tree_id);
1540 if (err)
1541 break;
1543 err = tree_view_visit_subtree(tree, s);
1544 if (err) {
1545 got_object_tree_close(tree);
1546 break;
1548 if (slash == NULL)
1549 break;
1550 free(subpath);
1551 subpath = NULL;
1552 p = slash;
1555 free(subpath);
1556 return err;
1559 static void *
1560 log_thread(void *arg)
1562 const struct got_error *err = NULL;
1563 int errcode = 0;
1564 struct tog_log_thread_args *a = arg;
1565 int done = 0;
1567 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1568 if (err)
1569 return (void *)err;
1571 while (!done && !err) {
1572 err = queue_commits(a->graph, a->commits, 1, a->repo,
1573 a->in_repo_path);
1574 if (err) {
1575 if (err->code != GOT_ERR_ITER_COMPLETED)
1576 return (void *)err;
1577 err = NULL;
1578 done = 1;
1579 } else if (a->commits_needed > 0)
1580 a->commits_needed--;
1582 errcode = pthread_mutex_lock(&tog_mutex);
1583 if (errcode) {
1584 err = got_error_set_errno(errcode,
1585 "pthread_mutex_lock");
1586 break;
1587 } else if (*a->quit)
1588 done = 1;
1589 else if (*a->first_displayed_entry == NULL) {
1590 *a->first_displayed_entry =
1591 TAILQ_FIRST(&a->commits->head);
1592 *a->selected_entry = *a->first_displayed_entry;
1595 if (done)
1596 a->commits_needed = 0;
1597 else if (a->commits_needed == 0) {
1598 errcode = pthread_cond_wait(&a->need_commits,
1599 &tog_mutex);
1600 if (errcode)
1601 err = got_error_set_errno(errcode,
1602 "pthread_cond_wait");
1605 errcode = pthread_mutex_unlock(&tog_mutex);
1606 if (errcode && err == NULL)
1607 err = got_error_set_errno(errcode,
1608 "pthread_mutex_unlock");
1610 a->log_complete = 1;
1611 return (void *)err;
1614 static const struct got_error *
1615 stop_log_thread(struct tog_log_view_state *s)
1617 const struct got_error *err = NULL;
1618 int errcode;
1620 if (s->thread) {
1621 s->quit = 1;
1622 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1623 if (errcode)
1624 return got_error_set_errno(errcode,
1625 "pthread_cond_signal");
1626 errcode = pthread_mutex_unlock(&tog_mutex);
1627 if (errcode)
1628 return got_error_set_errno(errcode,
1629 "pthread_mutex_unlock");
1630 errcode = pthread_join(s->thread, (void **)&err);
1631 if (errcode)
1632 return got_error_set_errno(errcode, "pthread_join");
1633 errcode = pthread_mutex_lock(&tog_mutex);
1634 if (errcode)
1635 return got_error_set_errno(errcode,
1636 "pthread_mutex_lock");
1637 s->thread = NULL;
1640 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1641 if (errcode && err == NULL)
1642 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1644 if (s->thread_args.repo) {
1645 got_repo_close(s->thread_args.repo);
1646 s->thread_args.repo = NULL;
1649 if (s->thread_args.graph) {
1650 got_commit_graph_close(s->thread_args.graph);
1651 s->thread_args.graph = NULL;
1654 return err;
1657 static const struct got_error *
1658 close_log_view(struct tog_view *view)
1660 const struct got_error *err = NULL;
1661 struct tog_log_view_state *s = &view->state.log;
1663 err = stop_log_thread(s);
1664 free_commits(&s->commits);
1665 free(s->in_repo_path);
1666 s->in_repo_path = NULL;
1667 free(s->start_id);
1668 s->start_id = NULL;
1669 return err;
1672 static const struct got_error *
1673 search_start_log_view(struct tog_view *view)
1675 struct tog_log_view_state *s = &view->state.log;
1676 char pattern[1024];
1677 int ret;
1679 if (view->nlines < 1)
1680 return NULL;
1682 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
1683 view->begin_x, "/");
1684 wclrtoeol(view->window);
1686 nocbreak();
1687 echo();
1688 ret = wgetnstr(view->window, pattern, sizeof(pattern));
1689 cbreak();
1690 noecho();
1691 if (ret == ERR)
1692 return NULL;
1694 if (view->searching) {
1695 regfree(&s->regex);
1696 view->searching = 0;
1699 s->matched_entry = NULL;
1700 if (regcomp(&s->regex, pattern,
1701 REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE) == 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->selected_entry, entry);
1741 else
1742 entry = TAILQ_PREV(s->selected_entry,
1743 commit_queue_head, entry);
1744 } else {
1745 if (view->searching == TOG_SEARCH_FORWARD)
1746 entry = TAILQ_FIRST(&s->commits.head);
1747 else
1748 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1751 while (1) {
1752 if (entry == NULL) {
1753 if (s->thread_args.log_complete) {
1754 if (s->matched_entry == NULL) {
1755 view->search_next_done = 1;
1756 return NULL;
1758 if (view->searching == TOG_SEARCH_FORWARD)
1759 entry = TAILQ_FIRST(&s->commits.head);
1760 else
1761 entry = TAILQ_LAST(&s->commits.head,
1762 commit_queue_head);
1763 } else {
1764 s->thread_args.commits_needed = 1;
1765 return trigger_log_thread(0,
1766 &s->thread_args.commits_needed,
1767 &s->thread_args.log_complete,
1768 &s->thread_args.need_commits);
1772 if (match_commit(entry->commit, &s->regex)) {
1773 view->search_next_done = 1;
1774 break;
1776 if (view->searching == TOG_SEARCH_FORWARD)
1777 entry = TAILQ_NEXT(entry, entry);
1778 else
1779 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1782 if (entry) {
1783 int cur = s->selected_entry->idx;
1784 s->matched_entry = entry;
1785 while (cur < s->matched_entry->idx) {
1786 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1787 if (err)
1788 return err;
1789 cur++;
1791 while (cur > s->matched_entry->idx) {
1792 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1793 if (err)
1794 return err;
1795 cur--;
1799 return NULL;
1803 static const struct got_error *
1804 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1805 struct got_reflist_head *refs, struct got_repository *repo,
1806 const char *path, int check_disk)
1808 const struct got_error *err = NULL;
1809 struct tog_log_view_state *s = &view->state.log;
1810 struct got_repository *thread_repo = NULL;
1811 struct got_commit_graph *thread_graph = NULL;
1812 int errcode;
1814 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1815 if (err != NULL)
1816 goto done;
1818 /* The commit queue only contains commits being displayed. */
1819 TAILQ_INIT(&s->commits.head);
1820 s->commits.ncommits = 0;
1822 s->refs = refs;
1823 s->repo = repo;
1824 s->start_id = got_object_id_dup(start_id);
1825 if (s->start_id == NULL) {
1826 err = got_error_from_errno("got_object_id_dup");
1827 goto done;
1830 view->show = show_log_view;
1831 view->input = input_log_view;
1832 view->close = close_log_view;
1833 view->search_start = search_start_log_view;
1834 view->search_next = search_next_log_view;
1836 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1837 if (err)
1838 goto done;
1839 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1840 0, thread_repo);
1841 if (err)
1842 goto done;
1844 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1845 if (errcode) {
1846 err = got_error_set_errno(errcode, "pthread_cond_init");
1847 goto done;
1850 s->thread_args.commits_needed = view->nlines;
1851 s->thread_args.graph = thread_graph;
1852 s->thread_args.commits = &s->commits;
1853 s->thread_args.in_repo_path = s->in_repo_path;
1854 s->thread_args.start_id = s->start_id;
1855 s->thread_args.repo = thread_repo;
1856 s->thread_args.log_complete = 0;
1857 s->thread_args.quit = &s->quit;
1858 s->thread_args.view = view;
1859 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1860 s->thread_args.selected_entry = &s->selected_entry;
1861 done:
1862 if (err)
1863 close_log_view(view);
1864 return err;
1867 static const struct got_error *
1868 show_log_view(struct tog_view *view)
1870 struct tog_log_view_state *s = &view->state.log;
1872 if (s->thread == NULL) {
1873 int errcode = pthread_create(&s->thread, NULL, log_thread,
1874 &s->thread_args);
1875 if (errcode)
1876 return got_error_set_errno(errcode, "pthread_create");
1879 return draw_commits(view, &s->last_displayed_entry,
1880 &s->selected_entry, s->first_displayed_entry,
1881 &s->commits, s->selected, view->nlines, s->refs,
1882 s->in_repo_path, s->thread_args.commits_needed);
1885 static const struct got_error *
1886 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1887 struct tog_view **focus_view, struct tog_view *view, int ch)
1889 const struct got_error *err = NULL;
1890 struct tog_log_view_state *s = &view->state.log;
1891 char *parent_path;
1892 struct tog_view *diff_view = NULL, *tree_view = NULL;
1893 int begin_x = 0;
1895 switch (ch) {
1896 case 'q':
1897 s->quit = 1;
1898 break;
1899 case 'k':
1900 case KEY_UP:
1901 case '<':
1902 case ',':
1903 if (s->first_displayed_entry == NULL)
1904 break;
1905 if (s->selected > 0)
1906 s->selected--;
1907 else
1908 scroll_up(view, &s->first_displayed_entry, 1,
1909 &s->commits);
1910 break;
1911 case KEY_PPAGE:
1912 case CTRL('b'):
1913 if (s->first_displayed_entry == NULL)
1914 break;
1915 if (TAILQ_FIRST(&s->commits.head) ==
1916 s->first_displayed_entry) {
1917 s->selected = 0;
1918 break;
1920 scroll_up(view, &s->first_displayed_entry,
1921 view->nlines, &s->commits);
1922 break;
1923 case 'j':
1924 case KEY_DOWN:
1925 case '>':
1926 case '.':
1927 if (s->first_displayed_entry == NULL)
1928 break;
1929 if (s->selected < MIN(view->nlines - 2,
1930 s->commits.ncommits - 1)) {
1931 s->selected++;
1932 break;
1934 err = scroll_down(view, &s->first_displayed_entry, 1,
1935 &s->last_displayed_entry, &s->commits,
1936 &s->thread_args.log_complete,
1937 &s->thread_args.commits_needed,
1938 &s->thread_args.need_commits);
1939 break;
1940 case KEY_NPAGE:
1941 case CTRL('f'): {
1942 struct commit_queue_entry *first;
1943 first = s->first_displayed_entry;
1944 if (first == NULL)
1945 break;
1946 err = scroll_down(view, &s->first_displayed_entry,
1947 view->nlines, &s->last_displayed_entry,
1948 &s->commits, &s->thread_args.log_complete,
1949 &s->thread_args.commits_needed,
1950 &s->thread_args.need_commits);
1951 if (first == s->first_displayed_entry &&
1952 s->selected < MIN(view->nlines - 2,
1953 s->commits.ncommits - 1)) {
1954 /* can't scroll further down */
1955 s->selected = MIN(view->nlines - 2,
1956 s->commits.ncommits - 1);
1958 err = NULL;
1959 break;
1961 case KEY_RESIZE:
1962 if (s->selected > view->nlines - 2)
1963 s->selected = view->nlines - 2;
1964 if (s->selected > s->commits.ncommits - 1)
1965 s->selected = s->commits.ncommits - 1;
1966 break;
1967 case KEY_ENTER:
1968 case ' ':
1969 case '\r':
1970 if (s->selected_entry == NULL)
1971 break;
1972 if (view_is_parent_view(view))
1973 begin_x = view_split_begin_x(view->begin_x);
1974 err = open_diff_view_for_commit(&diff_view, begin_x,
1975 s->selected_entry->commit, s->selected_entry->id,
1976 view, s->refs, s->repo);
1977 if (err)
1978 break;
1979 if (view_is_parent_view(view)) {
1980 err = view_close_child(view);
1981 if (err)
1982 return err;
1983 err = view_set_child(view, diff_view);
1984 if (err) {
1985 view_close(diff_view);
1986 break;
1988 *focus_view = diff_view;
1989 view->child_focussed = 1;
1990 } else
1991 *new_view = diff_view;
1992 break;
1993 case 't':
1994 if (s->selected_entry == NULL)
1995 break;
1996 if (view_is_parent_view(view))
1997 begin_x = view_split_begin_x(view->begin_x);
1998 err = browse_commit_tree(&tree_view, begin_x,
1999 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2000 if (err)
2001 break;
2002 if (view_is_parent_view(view)) {
2003 err = view_close_child(view);
2004 if (err)
2005 return err;
2006 err = view_set_child(view, tree_view);
2007 if (err) {
2008 view_close(tree_view);
2009 break;
2011 *focus_view = tree_view;
2012 view->child_focussed = 1;
2013 } else
2014 *new_view = tree_view;
2015 break;
2016 case KEY_BACKSPACE:
2017 if (strcmp(s->in_repo_path, "/") == 0)
2018 break;
2019 parent_path = dirname(s->in_repo_path);
2020 if (parent_path && strcmp(parent_path, ".") != 0) {
2021 struct tog_view *lv;
2022 err = stop_log_thread(s);
2023 if (err)
2024 return err;
2025 lv = view_open(view->nlines, view->ncols,
2026 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2027 if (lv == NULL)
2028 return got_error_from_errno(
2029 "view_open");
2030 err = open_log_view(lv, s->start_id, s->refs,
2031 s->repo, parent_path, 0);
2032 if (err)
2033 return err;;
2034 if (view_is_parent_view(view))
2035 *new_view = lv;
2036 else {
2037 view_set_child(view->parent, lv);
2038 *focus_view = lv;
2040 return NULL;
2042 break;
2043 default:
2044 break;
2047 return err;
2050 static const struct got_error *
2051 apply_unveil(const char *repo_path, const char *worktree_path)
2053 const struct got_error *error;
2055 if (repo_path && unveil(repo_path, "r") != 0)
2056 return got_error_from_errno2("unveil", repo_path);
2058 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2059 return got_error_from_errno2("unveil", worktree_path);
2061 if (unveil("/tmp", "rwc") != 0)
2062 return got_error_from_errno2("unveil", "/tmp");
2064 error = got_privsep_unveil_exec_helpers();
2065 if (error != NULL)
2066 return error;
2068 if (unveil(NULL, NULL) != 0)
2069 return got_error_from_errno("unveil");
2071 return NULL;
2074 static void
2075 init_curses(void)
2077 initscr();
2078 cbreak();
2079 halfdelay(1); /* Do fast refresh while initial view is loading. */
2080 noecho();
2081 nonl();
2082 intrflush(stdscr, FALSE);
2083 keypad(stdscr, TRUE);
2084 curs_set(0);
2085 signal(SIGWINCH, tog_sigwinch);
2088 static const struct got_error *
2089 cmd_log(int argc, char *argv[])
2091 const struct got_error *error;
2092 struct got_repository *repo = NULL;
2093 struct got_worktree *worktree = NULL;
2094 struct got_reflist_head refs;
2095 struct got_object_id *start_id = NULL;
2096 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2097 char *start_commit = NULL;
2098 int ch;
2099 struct tog_view *view;
2101 SIMPLEQ_INIT(&refs);
2103 #ifndef PROFILE
2104 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2105 NULL) == -1)
2106 err(1, "pledge");
2107 #endif
2109 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2110 switch (ch) {
2111 case 'c':
2112 start_commit = optarg;
2113 break;
2114 case 'r':
2115 repo_path = realpath(optarg, NULL);
2116 if (repo_path == NULL)
2117 err(1, "-r option");
2118 break;
2119 default:
2120 usage_log();
2121 /* NOTREACHED */
2125 argc -= optind;
2126 argv += optind;
2128 cwd = getcwd(NULL, 0);
2129 if (cwd == NULL) {
2130 error = got_error_from_errno("getcwd");
2131 goto done;
2133 error = got_worktree_open(&worktree, cwd);
2134 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2135 goto done;
2136 error = NULL;
2138 if (argc == 0) {
2139 path = strdup("");
2140 if (path == NULL) {
2141 error = got_error_from_errno("strdup");
2142 goto done;
2144 } else if (argc == 1) {
2145 if (worktree) {
2146 error = got_worktree_resolve_path(&path, worktree,
2147 argv[0]);
2148 if (error)
2149 goto done;
2150 } else {
2151 path = strdup(argv[0]);
2152 if (path == NULL) {
2153 error = got_error_from_errno("strdup");
2154 goto done;
2157 } else
2158 usage_log();
2160 repo_path = worktree ?
2161 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2162 if (repo_path == NULL) {
2163 error = got_error_from_errno("strdup");
2164 goto done;
2167 init_curses();
2169 error = got_repo_open(&repo, repo_path);
2170 if (error != NULL)
2171 goto done;
2173 error = apply_unveil(got_repo_get_path(repo),
2174 worktree ? got_worktree_get_root_path(worktree) : NULL);
2175 if (error)
2176 goto done;
2178 if (start_commit == NULL)
2179 error = get_head_commit_id(&start_id, worktree ?
2180 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2181 repo);
2182 else
2183 error = got_object_resolve_id_str(&start_id, repo,
2184 start_commit);
2185 if (error != NULL)
2186 goto done;
2188 error = got_ref_list(&refs, repo);
2189 if (error)
2190 goto done;
2192 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2193 if (view == NULL) {
2194 error = got_error_from_errno("view_open");
2195 goto done;
2197 error = open_log_view(view, start_id, &refs, repo, path, 1);
2198 if (error)
2199 goto done;
2200 error = view_loop(view);
2201 done:
2202 free(repo_path);
2203 free(cwd);
2204 free(path);
2205 free(start_id);
2206 if (repo)
2207 got_repo_close(repo);
2208 if (worktree)
2209 got_worktree_close(worktree);
2210 got_ref_list_free(&refs);
2211 return error;
2214 __dead static void
2215 usage_diff(void)
2217 endwin();
2218 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2219 getprogname());
2220 exit(1);
2223 static char *
2224 parse_next_line(FILE *f, size_t *len)
2226 char *line;
2227 size_t linelen;
2228 size_t lineno;
2229 const char delim[3] = { '\0', '\0', '\0'};
2231 line = fparseln(f, &linelen, &lineno, delim, 0);
2232 if (len)
2233 *len = linelen;
2234 return line;
2237 static const struct got_error *
2238 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2239 int *last_displayed_line, int *eof, int max_lines,
2240 char *header)
2242 const struct got_error *err;
2243 int nlines = 0, nprinted = 0;
2244 char *line;
2245 size_t len;
2246 wchar_t *wline;
2247 int width;
2249 rewind(f);
2250 werase(view->window);
2252 if (header) {
2253 err = format_line(&wline, &width, header, view->ncols);
2254 if (err) {
2255 return err;
2258 if (view_needs_focus_indication(view))
2259 wstandout(view->window);
2260 waddwstr(view->window, wline);
2261 if (view_needs_focus_indication(view))
2262 wstandend(view->window);
2263 if (width < view->ncols - 1)
2264 waddch(view->window, '\n');
2266 if (max_lines <= 1)
2267 return NULL;
2268 max_lines--;
2271 *eof = 0;
2272 while (nprinted < max_lines) {
2273 line = parse_next_line(f, &len);
2274 if (line == NULL) {
2275 *eof = 1;
2276 break;
2278 if (++nlines < *first_displayed_line) {
2279 free(line);
2280 continue;
2283 err = format_line(&wline, &width, line, view->ncols);
2284 if (err) {
2285 free(line);
2286 return err;
2288 waddwstr(view->window, wline);
2289 if (width < view->ncols - 1)
2290 waddch(view->window, '\n');
2291 if (++nprinted == 1)
2292 *first_displayed_line = nlines;
2293 free(line);
2294 free(wline);
2295 wline = NULL;
2297 *last_displayed_line = nlines;
2299 view_vborder(view);
2301 if (*eof) {
2302 while (nprinted < view->nlines) {
2303 waddch(view->window, '\n');
2304 nprinted++;
2307 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2308 if (err) {
2309 return err;
2312 wstandout(view->window);
2313 waddwstr(view->window, wline);
2314 wstandend(view->window);
2317 return NULL;
2320 static char *
2321 get_datestr(time_t *time, char *datebuf)
2323 char *p, *s = ctime_r(time, datebuf);
2324 p = strchr(s, '\n');
2325 if (p)
2326 *p = '\0';
2327 return s;
2330 static const struct got_error *
2331 write_commit_info(struct got_object_id *commit_id,
2332 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2334 const struct got_error *err = NULL;
2335 char datebuf[26];
2336 struct got_commit_object *commit;
2337 char *id_str = NULL;
2338 time_t committer_time;
2339 const char *author, *committer;
2340 char *refs_str = NULL;
2342 if (refs) {
2343 err = build_refs_str(&refs_str, refs, commit_id);
2344 if (err)
2345 return err;
2348 err = got_object_open_as_commit(&commit, repo, commit_id);
2349 if (err)
2350 return err;
2352 err = got_object_id_str(&id_str, commit_id);
2353 if (err) {
2354 err = got_error_from_errno("got_object_id_str");
2355 goto done;
2358 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2359 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2360 err = got_error_from_errno("fprintf");
2361 goto done;
2363 if (fprintf(outfile, "from: %s\n",
2364 got_object_commit_get_author(commit)) < 0) {
2365 err = got_error_from_errno("fprintf");
2366 goto done;
2368 committer_time = got_object_commit_get_committer_time(commit);
2369 if (fprintf(outfile, "date: %s UTC\n",
2370 get_datestr(&committer_time, datebuf)) < 0) {
2371 err = got_error_from_errno("fprintf");
2372 goto done;
2374 author = got_object_commit_get_author(commit);
2375 committer = got_object_commit_get_committer(commit);
2376 if (strcmp(author, committer) != 0 &&
2377 fprintf(outfile, "via: %s\n", committer) < 0) {
2378 err = got_error_from_errno("fprintf");
2379 goto done;
2381 if (fprintf(outfile, "%s\n",
2382 got_object_commit_get_logmsg(commit)) < 0) {
2383 err = got_error_from_errno("fprintf");
2384 goto done;
2386 done:
2387 free(id_str);
2388 free(refs_str);
2389 got_object_commit_close(commit);
2390 return err;
2393 static const struct got_error *
2394 create_diff(struct tog_diff_view_state *s)
2396 const struct got_error *err = NULL;
2397 FILE *f = NULL;
2398 int obj_type;
2400 f = got_opentemp();
2401 if (f == NULL) {
2402 err = got_error_from_errno("got_opentemp");
2403 goto done;
2405 if (s->f && fclose(s->f) != 0) {
2406 err = got_error_from_errno("fclose");
2407 goto done;
2409 s->f = f;
2411 if (s->id1)
2412 err = got_object_get_type(&obj_type, s->repo, s->id1);
2413 else
2414 err = got_object_get_type(&obj_type, s->repo, s->id2);
2415 if (err)
2416 goto done;
2418 switch (obj_type) {
2419 case GOT_OBJ_TYPE_BLOB:
2420 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2421 s->diff_context, s->repo, f);
2422 break;
2423 case GOT_OBJ_TYPE_TREE:
2424 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2425 s->diff_context, s->repo, f);
2426 break;
2427 case GOT_OBJ_TYPE_COMMIT: {
2428 const struct got_object_id_queue *parent_ids;
2429 struct got_object_qid *pid;
2430 struct got_commit_object *commit2;
2432 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2433 if (err)
2434 break;
2435 /* Show commit info if we're diffing to a parent/root commit. */
2436 if (s->id1 == NULL)
2437 write_commit_info(s->id2, s->refs, s->repo, f);
2438 else {
2439 parent_ids = got_object_commit_get_parent_ids(commit2);
2440 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2441 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2442 write_commit_info(s->id2, s->refs,
2443 s->repo, f);
2444 break;
2448 got_object_commit_close(commit2);
2450 err = got_diff_objects_as_commits(s->id1, s->id2,
2451 s->diff_context, s->repo, f);
2452 break;
2454 default:
2455 err = got_error(GOT_ERR_OBJ_TYPE);
2456 break;
2458 done:
2459 if (f && fflush(f) != 0 && err == NULL)
2460 err = got_error_from_errno("fflush");
2461 return err;
2464 static void
2465 diff_view_indicate_progress(struct tog_view *view)
2467 mvwaddstr(view->window, 0, 0, "diffing...");
2468 update_panels();
2469 doupdate();
2472 static const struct got_error *
2473 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2474 struct got_object_id *id2, struct tog_view *log_view,
2475 struct got_reflist_head *refs, struct got_repository *repo)
2477 const struct got_error *err;
2479 if (id1 != NULL && id2 != NULL) {
2480 int type1, type2;
2481 err = got_object_get_type(&type1, repo, id1);
2482 if (err)
2483 return err;
2484 err = got_object_get_type(&type2, repo, id2);
2485 if (err)
2486 return err;
2488 if (type1 != type2)
2489 return got_error(GOT_ERR_OBJ_TYPE);
2492 if (id1) {
2493 view->state.diff.id1 = got_object_id_dup(id1);
2494 if (view->state.diff.id1 == NULL)
2495 return got_error_from_errno("got_object_id_dup");
2496 } else
2497 view->state.diff.id1 = NULL;
2499 view->state.diff.id2 = got_object_id_dup(id2);
2500 if (view->state.diff.id2 == NULL) {
2501 free(view->state.diff.id1);
2502 view->state.diff.id1 = NULL;
2503 return got_error_from_errno("got_object_id_dup");
2505 view->state.diff.f = NULL;
2506 view->state.diff.first_displayed_line = 1;
2507 view->state.diff.last_displayed_line = view->nlines;
2508 view->state.diff.diff_context = 3;
2509 view->state.diff.log_view = log_view;
2510 view->state.diff.repo = repo;
2511 view->state.diff.refs = refs;
2513 if (log_view && view_is_splitscreen(view))
2514 show_log_view(log_view); /* draw vborder */
2515 diff_view_indicate_progress(view);
2517 err = create_diff(&view->state.diff);
2518 if (err) {
2519 free(view->state.diff.id1);
2520 view->state.diff.id1 = NULL;
2521 free(view->state.diff.id2);
2522 view->state.diff.id2 = NULL;
2523 return err;
2526 view->show = show_diff_view;
2527 view->input = input_diff_view;
2528 view->close = close_diff_view;
2530 return NULL;
2533 static const struct got_error *
2534 close_diff_view(struct tog_view *view)
2536 const struct got_error *err = NULL;
2538 free(view->state.diff.id1);
2539 view->state.diff.id1 = NULL;
2540 free(view->state.diff.id2);
2541 view->state.diff.id2 = NULL;
2542 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2543 err = got_error_from_errno("fclose");
2544 return err;
2547 static const struct got_error *
2548 show_diff_view(struct tog_view *view)
2550 const struct got_error *err;
2551 struct tog_diff_view_state *s = &view->state.diff;
2552 char *id_str1 = NULL, *id_str2, *header;
2554 if (s->id1) {
2555 err = got_object_id_str(&id_str1, s->id1);
2556 if (err)
2557 return err;
2559 err = got_object_id_str(&id_str2, s->id2);
2560 if (err)
2561 return err;
2563 if (asprintf(&header, "diff %s %s",
2564 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2565 err = got_error_from_errno("asprintf");
2566 free(id_str1);
2567 free(id_str2);
2568 return err;
2570 free(id_str1);
2571 free(id_str2);
2573 return draw_file(view, s->f, &s->first_displayed_line,
2574 &s->last_displayed_line, &s->eof, view->nlines,
2575 header);
2578 static const struct got_error *
2579 set_selected_commit(struct tog_diff_view_state *s,
2580 struct commit_queue_entry *entry)
2582 const struct got_error *err;
2583 const struct got_object_id_queue *parent_ids;
2584 struct got_commit_object *selected_commit;
2585 struct got_object_qid *pid;
2587 free(s->id2);
2588 s->id2 = got_object_id_dup(entry->id);
2589 if (s->id2 == NULL)
2590 return got_error_from_errno("got_object_id_dup");
2592 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2593 if (err)
2594 return err;
2595 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2596 free(s->id1);
2597 pid = SIMPLEQ_FIRST(parent_ids);
2598 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2599 got_object_commit_close(selected_commit);
2600 return NULL;
2603 static const struct got_error *
2604 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2605 struct tog_view **focus_view, struct tog_view *view, int ch)
2607 const struct got_error *err = NULL;
2608 struct tog_diff_view_state *s = &view->state.diff;
2609 struct tog_log_view_state *ls;
2610 struct commit_queue_entry *entry;
2611 int i;
2613 switch (ch) {
2614 case 'k':
2615 case KEY_UP:
2616 if (s->first_displayed_line > 1)
2617 s->first_displayed_line--;
2618 break;
2619 case KEY_PPAGE:
2620 case CTRL('b'):
2621 if (s->first_displayed_line == 1)
2622 break;
2623 i = 0;
2624 while (i++ < view->nlines - 1 &&
2625 s->first_displayed_line > 1)
2626 s->first_displayed_line--;
2627 break;
2628 case 'j':
2629 case KEY_DOWN:
2630 if (!s->eof)
2631 s->first_displayed_line++;
2632 break;
2633 case KEY_NPAGE:
2634 case CTRL('f'):
2635 case ' ':
2636 if (s->eof)
2637 break;
2638 i = 0;
2639 while (!s->eof && i++ < view->nlines - 1) {
2640 char *line;
2641 line = parse_next_line(s->f, NULL);
2642 s->first_displayed_line++;
2643 if (line == NULL)
2644 break;
2646 break;
2647 case '[':
2648 if (s->diff_context > 0) {
2649 s->diff_context--;
2650 diff_view_indicate_progress(view);
2651 err = create_diff(s);
2653 break;
2654 case ']':
2655 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2656 s->diff_context++;
2657 diff_view_indicate_progress(view);
2658 err = create_diff(s);
2660 break;
2661 case '<':
2662 case ',':
2663 if (s->log_view == NULL)
2664 break;
2665 ls = &s->log_view->state.log;
2666 entry = TAILQ_PREV(ls->selected_entry,
2667 commit_queue_head, entry);
2668 if (entry == NULL)
2669 break;
2671 err = input_log_view(NULL, NULL, NULL, s->log_view,
2672 KEY_UP);
2673 if (err)
2674 break;
2676 err = set_selected_commit(s, entry);
2677 if (err)
2678 break;
2680 s->first_displayed_line = 1;
2681 s->last_displayed_line = view->nlines;
2683 diff_view_indicate_progress(view);
2684 err = create_diff(s);
2685 break;
2686 case '>':
2687 case '.':
2688 if (s->log_view == NULL)
2689 break;
2690 ls = &s->log_view->state.log;
2692 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2693 ls->thread_args.commits_needed++;
2695 /* Display "loading..." in log view. */
2696 show_log_view(s->log_view);
2697 update_panels();
2698 doupdate();
2700 err = trigger_log_thread(1 /* load_all */,
2701 &ls->thread_args.commits_needed,
2702 &ls->thread_args.log_complete,
2703 &ls->thread_args.need_commits);
2704 if (err)
2705 break;
2707 err = input_log_view(NULL, NULL, NULL, s->log_view,
2708 KEY_DOWN);
2709 if (err)
2710 break;
2712 entry = TAILQ_NEXT(ls->selected_entry, entry);
2713 if (entry == NULL)
2714 break;
2716 err = set_selected_commit(s, entry);
2717 if (err)
2718 break;
2720 s->first_displayed_line = 1;
2721 s->last_displayed_line = view->nlines;
2723 diff_view_indicate_progress(view);
2724 err = create_diff(s);
2725 break;
2726 default:
2727 break;
2730 return err;
2733 static const struct got_error *
2734 cmd_diff(int argc, char *argv[])
2736 const struct got_error *error = NULL;
2737 struct got_repository *repo = NULL;
2738 struct got_reflist_head refs;
2739 struct got_object_id *id1 = NULL, *id2 = NULL;
2740 char *repo_path = NULL;
2741 char *id_str1 = NULL, *id_str2 = NULL;
2742 int ch;
2743 struct tog_view *view;
2745 SIMPLEQ_INIT(&refs);
2747 #ifndef PROFILE
2748 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2749 NULL) == -1)
2750 err(1, "pledge");
2751 #endif
2753 while ((ch = getopt(argc, argv, "")) != -1) {
2754 switch (ch) {
2755 default:
2756 usage_diff();
2757 /* NOTREACHED */
2761 argc -= optind;
2762 argv += optind;
2764 if (argc == 0) {
2765 usage_diff(); /* TODO show local worktree changes */
2766 } else if (argc == 2) {
2767 repo_path = getcwd(NULL, 0);
2768 if (repo_path == NULL)
2769 return got_error_from_errno("getcwd");
2770 id_str1 = argv[0];
2771 id_str2 = argv[1];
2772 } else if (argc == 3) {
2773 repo_path = realpath(argv[0], NULL);
2774 if (repo_path == NULL)
2775 return got_error_from_errno2("realpath", argv[0]);
2776 id_str1 = argv[1];
2777 id_str2 = argv[2];
2778 } else
2779 usage_diff();
2781 init_curses();
2783 error = got_repo_open(&repo, repo_path);
2784 if (error)
2785 goto done;
2787 error = apply_unveil(got_repo_get_path(repo), NULL);
2788 if (error)
2789 goto done;
2791 error = got_object_resolve_id_str(&id1, repo, id_str1);
2792 if (error)
2793 goto done;
2795 error = got_object_resolve_id_str(&id2, repo, id_str2);
2796 if (error)
2797 goto done;
2799 error = got_ref_list(&refs, repo);
2800 if (error)
2801 goto done;
2803 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2804 if (view == NULL) {
2805 error = got_error_from_errno("view_open");
2806 goto done;
2808 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2809 if (error)
2810 goto done;
2811 error = view_loop(view);
2812 done:
2813 free(repo_path);
2814 got_repo_close(repo);
2815 got_ref_list_free(&refs);
2816 return error;
2819 __dead static void
2820 usage_blame(void)
2822 endwin();
2823 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2824 getprogname());
2825 exit(1);
2828 struct tog_blame_line {
2829 int annotated;
2830 struct got_object_id *id;
2833 static const struct got_error *
2834 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2835 const char *path, struct tog_blame_line *lines, int nlines,
2836 int blame_complete, int selected_line, int *first_displayed_line,
2837 int *last_displayed_line, int *eof, int max_lines)
2839 const struct got_error *err;
2840 int lineno = 0, nprinted = 0;
2841 char *line;
2842 size_t len;
2843 wchar_t *wline;
2844 int width, wlimit;
2845 struct tog_blame_line *blame_line;
2846 struct got_object_id *prev_id = NULL;
2847 char *id_str;
2849 err = got_object_id_str(&id_str, id);
2850 if (err)
2851 return err;
2853 rewind(f);
2854 werase(view->window);
2856 if (asprintf(&line, "commit %s", id_str) == -1) {
2857 err = got_error_from_errno("asprintf");
2858 free(id_str);
2859 return err;
2862 err = format_line(&wline, &width, line, view->ncols);
2863 free(line);
2864 line = NULL;
2865 if (view_needs_focus_indication(view))
2866 wstandout(view->window);
2867 waddwstr(view->window, wline);
2868 if (view_needs_focus_indication(view))
2869 wstandend(view->window);
2870 free(wline);
2871 wline = NULL;
2872 if (width < view->ncols - 1)
2873 waddch(view->window, '\n');
2875 if (asprintf(&line, "[%d/%d] %s%s",
2876 *first_displayed_line - 1 + selected_line, nlines,
2877 blame_complete ? "" : "annotating... ", path) == -1) {
2878 free(id_str);
2879 return got_error_from_errno("asprintf");
2881 free(id_str);
2882 err = format_line(&wline, &width, line, view->ncols);
2883 free(line);
2884 line = NULL;
2885 if (err)
2886 return err;
2887 waddwstr(view->window, wline);
2888 free(wline);
2889 wline = NULL;
2890 if (width < view->ncols - 1)
2891 waddch(view->window, '\n');
2893 *eof = 0;
2894 while (nprinted < max_lines - 2) {
2895 line = parse_next_line(f, &len);
2896 if (line == NULL) {
2897 *eof = 1;
2898 break;
2900 if (++lineno < *first_displayed_line) {
2901 free(line);
2902 continue;
2905 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2906 err = format_line(&wline, &width, line, wlimit);
2907 if (err) {
2908 free(line);
2909 return err;
2912 if (view->focussed && nprinted == selected_line - 1)
2913 wstandout(view->window);
2915 blame_line = &lines[lineno - 1];
2916 if (blame_line->annotated && prev_id &&
2917 got_object_id_cmp(prev_id, blame_line->id) == 0)
2918 waddstr(view->window, " ");
2919 else if (blame_line->annotated) {
2920 char *id_str;
2921 err = got_object_id_str(&id_str, blame_line->id);
2922 if (err) {
2923 free(line);
2924 free(wline);
2925 return err;
2927 wprintw(view->window, "%.8s ", id_str);
2928 free(id_str);
2929 prev_id = blame_line->id;
2930 } else {
2931 waddstr(view->window, "........ ");
2932 prev_id = NULL;
2935 waddwstr(view->window, wline);
2936 while (width < wlimit) {
2937 waddch(view->window, ' ');
2938 width++;
2940 if (view->focussed && nprinted == selected_line - 1)
2941 wstandend(view->window);
2942 if (++nprinted == 1)
2943 *first_displayed_line = lineno;
2944 free(line);
2945 free(wline);
2946 wline = NULL;
2948 *last_displayed_line = lineno;
2950 view_vborder(view);
2952 return NULL;
2955 static const struct got_error *
2956 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2958 const struct got_error *err = NULL;
2959 struct tog_blame_cb_args *a = arg;
2960 struct tog_blame_line *line;
2961 int errcode;
2963 if (nlines != a->nlines ||
2964 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2965 return got_error(GOT_ERR_RANGE);
2967 errcode = pthread_mutex_lock(&tog_mutex);
2968 if (errcode)
2969 return got_error_set_errno(errcode, "pthread_mutex_lock");
2971 if (*a->quit) { /* user has quit the blame view */
2972 err = got_error(GOT_ERR_ITER_COMPLETED);
2973 goto done;
2976 if (lineno == -1)
2977 goto done; /* no change in this commit */
2979 line = &a->lines[lineno - 1];
2980 if (line->annotated)
2981 goto done;
2983 line->id = got_object_id_dup(id);
2984 if (line->id == NULL) {
2985 err = got_error_from_errno("got_object_id_dup");
2986 goto done;
2988 line->annotated = 1;
2989 done:
2990 errcode = pthread_mutex_unlock(&tog_mutex);
2991 if (errcode)
2992 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2993 return err;
2996 static void *
2997 blame_thread(void *arg)
2999 const struct got_error *err;
3000 struct tog_blame_thread_args *ta = arg;
3001 struct tog_blame_cb_args *a = ta->cb_args;
3002 int errcode;
3004 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3005 blame_cb, ta->cb_args);
3007 errcode = pthread_mutex_lock(&tog_mutex);
3008 if (errcode)
3009 return (void *)got_error_set_errno(errcode,
3010 "pthread_mutex_lock");
3012 got_repo_close(ta->repo);
3013 ta->repo = NULL;
3014 *ta->complete = 1;
3016 errcode = pthread_mutex_unlock(&tog_mutex);
3017 if (errcode && err == NULL)
3018 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3020 return (void *)err;
3023 static struct got_object_id *
3024 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3025 int selected_line)
3027 struct tog_blame_line *line;
3029 line = &lines[first_displayed_line - 1 + selected_line - 1];
3030 if (!line->annotated)
3031 return NULL;
3033 return line->id;
3036 static const struct got_error *
3037 stop_blame(struct tog_blame *blame)
3039 const struct got_error *err = NULL;
3040 int i;
3042 if (blame->thread) {
3043 int errcode;
3044 errcode = pthread_mutex_unlock(&tog_mutex);
3045 if (errcode)
3046 return got_error_set_errno(errcode,
3047 "pthread_mutex_unlock");
3048 errcode = pthread_join(blame->thread, (void **)&err);
3049 if (errcode)
3050 return got_error_set_errno(errcode, "pthread_join");
3051 errcode = pthread_mutex_lock(&tog_mutex);
3052 if (errcode)
3053 return got_error_set_errno(errcode,
3054 "pthread_mutex_lock");
3055 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3056 err = NULL;
3057 blame->thread = NULL;
3059 if (blame->thread_args.repo) {
3060 got_repo_close(blame->thread_args.repo);
3061 blame->thread_args.repo = NULL;
3063 if (blame->f) {
3064 if (fclose(blame->f) != 0 && err == NULL)
3065 err = got_error_from_errno("fclose");
3066 blame->f = NULL;
3068 if (blame->lines) {
3069 for (i = 0; i < blame->nlines; i++)
3070 free(blame->lines[i].id);
3071 free(blame->lines);
3072 blame->lines = NULL;
3074 free(blame->cb_args.commit_id);
3075 blame->cb_args.commit_id = NULL;
3077 return err;
3080 static const struct got_error *
3081 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3082 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3083 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3084 struct got_repository *repo)
3086 const struct got_error *err = NULL;
3087 struct got_blob_object *blob = NULL;
3088 struct got_repository *thread_repo = NULL;
3089 struct got_object_id *obj_id = NULL;
3090 int obj_type;
3092 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3093 if (err)
3094 return err;
3095 if (obj_id == NULL)
3096 return got_error(GOT_ERR_NO_OBJ);
3098 err = got_object_get_type(&obj_type, repo, obj_id);
3099 if (err)
3100 goto done;
3102 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3103 err = got_error(GOT_ERR_OBJ_TYPE);
3104 goto done;
3107 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3108 if (err)
3109 goto done;
3110 blame->f = got_opentemp();
3111 if (blame->f == NULL) {
3112 err = got_error_from_errno("got_opentemp");
3113 goto done;
3115 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3116 blame->f, blob);
3117 if (err)
3118 goto done;
3120 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3121 if (blame->lines == NULL) {
3122 err = got_error_from_errno("calloc");
3123 goto done;
3126 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3127 if (err)
3128 goto done;
3130 blame->cb_args.view = view;
3131 blame->cb_args.lines = blame->lines;
3132 blame->cb_args.nlines = blame->nlines;
3133 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3134 if (blame->cb_args.commit_id == NULL) {
3135 err = got_error_from_errno("got_object_id_dup");
3136 goto done;
3138 blame->cb_args.quit = done;
3140 blame->thread_args.path = path;
3141 blame->thread_args.repo = thread_repo;
3142 blame->thread_args.cb_args = &blame->cb_args;
3143 blame->thread_args.complete = blame_complete;
3144 *blame_complete = 0;
3146 done:
3147 if (blob)
3148 got_object_blob_close(blob);
3149 free(obj_id);
3150 if (err)
3151 stop_blame(blame);
3152 return err;
3155 static const struct got_error *
3156 open_blame_view(struct tog_view *view, char *path,
3157 struct got_object_id *commit_id, struct got_reflist_head *refs,
3158 struct got_repository *repo)
3160 const struct got_error *err = NULL;
3161 struct tog_blame_view_state *s = &view->state.blame;
3163 SIMPLEQ_INIT(&s->blamed_commits);
3165 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3166 if (err)
3167 return err;
3169 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3170 s->first_displayed_line = 1;
3171 s->last_displayed_line = view->nlines;
3172 s->selected_line = 1;
3173 s->blame_complete = 0;
3174 s->path = path;
3175 if (s->path == NULL)
3176 return got_error_from_errno("open_blame_view");
3177 s->repo = repo;
3178 s->refs = refs;
3179 s->commit_id = commit_id;
3180 memset(&s->blame, 0, sizeof(s->blame));
3182 view->show = show_blame_view;
3183 view->input = input_blame_view;
3184 view->close = close_blame_view;
3186 return run_blame(&s->blame, view, &s->blame_complete,
3187 &s->first_displayed_line, &s->last_displayed_line,
3188 &s->selected_line, &s->done, &s->eof, s->path,
3189 s->blamed_commit->id, s->repo);
3192 static const struct got_error *
3193 close_blame_view(struct tog_view *view)
3195 const struct got_error *err = NULL;
3196 struct tog_blame_view_state *s = &view->state.blame;
3198 if (s->blame.thread)
3199 err = stop_blame(&s->blame);
3201 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3202 struct got_object_qid *blamed_commit;
3203 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3204 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3205 got_object_qid_free(blamed_commit);
3208 free(s->path);
3210 return err;
3213 static const struct got_error *
3214 show_blame_view(struct tog_view *view)
3216 const struct got_error *err = NULL;
3217 struct tog_blame_view_state *s = &view->state.blame;
3218 int errcode;
3220 if (s->blame.thread == NULL) {
3221 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3222 &s->blame.thread_args);
3223 if (errcode)
3224 return got_error_set_errno(errcode, "pthread_create");
3227 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3228 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3229 s->selected_line, &s->first_displayed_line,
3230 &s->last_displayed_line, &s->eof, view->nlines);
3232 view_vborder(view);
3233 return err;
3236 static const struct got_error *
3237 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3238 struct tog_view **focus_view, struct tog_view *view, int ch)
3240 const struct got_error *err = NULL, *thread_err = NULL;
3241 struct tog_view *diff_view;
3242 struct tog_blame_view_state *s = &view->state.blame;
3243 int begin_x = 0;
3245 switch (ch) {
3246 case 'q':
3247 s->done = 1;
3248 break;
3249 case 'k':
3250 case KEY_UP:
3251 if (s->selected_line > 1)
3252 s->selected_line--;
3253 else if (s->selected_line == 1 &&
3254 s->first_displayed_line > 1)
3255 s->first_displayed_line--;
3256 break;
3257 case KEY_PPAGE:
3258 if (s->first_displayed_line == 1) {
3259 s->selected_line = 1;
3260 break;
3262 if (s->first_displayed_line > view->nlines - 2)
3263 s->first_displayed_line -=
3264 (view->nlines - 2);
3265 else
3266 s->first_displayed_line = 1;
3267 break;
3268 case 'j':
3269 case KEY_DOWN:
3270 if (s->selected_line < view->nlines - 2 &&
3271 s->first_displayed_line +
3272 s->selected_line <= s->blame.nlines)
3273 s->selected_line++;
3274 else if (s->last_displayed_line <
3275 s->blame.nlines)
3276 s->first_displayed_line++;
3277 break;
3278 case 'b':
3279 case 'p': {
3280 struct got_object_id *id = NULL;
3281 id = get_selected_commit_id(s->blame.lines,
3282 s->first_displayed_line, s->selected_line);
3283 if (id == NULL)
3284 break;
3285 if (ch == 'p') {
3286 struct got_commit_object *commit;
3287 struct got_object_qid *pid;
3288 struct got_object_id *blob_id = NULL;
3289 int obj_type;
3290 err = got_object_open_as_commit(&commit,
3291 s->repo, id);
3292 if (err)
3293 break;
3294 pid = SIMPLEQ_FIRST(
3295 got_object_commit_get_parent_ids(commit));
3296 if (pid == NULL) {
3297 got_object_commit_close(commit);
3298 break;
3300 /* Check if path history ends here. */
3301 err = got_object_id_by_path(&blob_id, s->repo,
3302 pid->id, s->path);
3303 if (err) {
3304 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3305 err = NULL;
3306 got_object_commit_close(commit);
3307 break;
3309 err = got_object_get_type(&obj_type, s->repo,
3310 blob_id);
3311 free(blob_id);
3312 /* Can't blame non-blob type objects. */
3313 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3314 got_object_commit_close(commit);
3315 break;
3317 err = got_object_qid_alloc(&s->blamed_commit,
3318 pid->id);
3319 got_object_commit_close(commit);
3320 } else {
3321 if (got_object_id_cmp(id,
3322 s->blamed_commit->id) == 0)
3323 break;
3324 err = got_object_qid_alloc(&s->blamed_commit,
3325 id);
3327 if (err)
3328 break;
3329 s->done = 1;
3330 thread_err = stop_blame(&s->blame);
3331 s->done = 0;
3332 if (thread_err)
3333 break;
3334 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3335 s->blamed_commit, entry);
3336 err = run_blame(&s->blame, view, &s->blame_complete,
3337 &s->first_displayed_line, &s->last_displayed_line,
3338 &s->selected_line, &s->done, &s->eof,
3339 s->path, s->blamed_commit->id, s->repo);
3340 if (err)
3341 break;
3342 break;
3344 case 'B': {
3345 struct got_object_qid *first;
3346 first = SIMPLEQ_FIRST(&s->blamed_commits);
3347 if (!got_object_id_cmp(first->id, s->commit_id))
3348 break;
3349 s->done = 1;
3350 thread_err = stop_blame(&s->blame);
3351 s->done = 0;
3352 if (thread_err)
3353 break;
3354 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3355 got_object_qid_free(s->blamed_commit);
3356 s->blamed_commit =
3357 SIMPLEQ_FIRST(&s->blamed_commits);
3358 err = run_blame(&s->blame, view, &s->blame_complete,
3359 &s->first_displayed_line, &s->last_displayed_line,
3360 &s->selected_line, &s->done, &s->eof, s->path,
3361 s->blamed_commit->id, s->repo);
3362 if (err)
3363 break;
3364 break;
3366 case KEY_ENTER:
3367 case '\r': {
3368 struct got_object_id *id = NULL;
3369 struct got_object_qid *pid;
3370 struct got_commit_object *commit = NULL;
3371 id = get_selected_commit_id(s->blame.lines,
3372 s->first_displayed_line, s->selected_line);
3373 if (id == NULL)
3374 break;
3375 err = got_object_open_as_commit(&commit, s->repo, id);
3376 if (err)
3377 break;
3378 pid = SIMPLEQ_FIRST(
3379 got_object_commit_get_parent_ids(commit));
3380 if (view_is_parent_view(view))
3381 begin_x = view_split_begin_x(view->begin_x);
3382 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3383 if (diff_view == NULL) {
3384 got_object_commit_close(commit);
3385 err = got_error_from_errno("view_open");
3386 break;
3388 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3389 id, NULL, s->refs, s->repo);
3390 got_object_commit_close(commit);
3391 if (err) {
3392 view_close(diff_view);
3393 break;
3395 if (view_is_parent_view(view)) {
3396 err = view_close_child(view);
3397 if (err)
3398 break;
3399 err = view_set_child(view, diff_view);
3400 if (err) {
3401 view_close(diff_view);
3402 break;
3404 *focus_view = diff_view;
3405 view->child_focussed = 1;
3406 } else
3407 *new_view = diff_view;
3408 if (err)
3409 break;
3410 break;
3412 case KEY_NPAGE:
3413 case ' ':
3414 if (s->last_displayed_line >= s->blame.nlines &&
3415 s->selected_line >= MIN(s->blame.nlines,
3416 view->nlines - 2)) {
3417 break;
3419 if (s->last_displayed_line >= s->blame.nlines &&
3420 s->selected_line < view->nlines - 2) {
3421 s->selected_line = MIN(s->blame.nlines,
3422 view->nlines - 2);
3423 break;
3425 if (s->last_displayed_line + view->nlines - 2
3426 <= s->blame.nlines)
3427 s->first_displayed_line +=
3428 view->nlines - 2;
3429 else
3430 s->first_displayed_line =
3431 s->blame.nlines -
3432 (view->nlines - 3);
3433 break;
3434 case KEY_RESIZE:
3435 if (s->selected_line > view->nlines - 2) {
3436 s->selected_line = MIN(s->blame.nlines,
3437 view->nlines - 2);
3439 break;
3440 default:
3441 break;
3443 return thread_err ? thread_err : err;
3446 static const struct got_error *
3447 cmd_blame(int argc, char *argv[])
3449 const struct got_error *error;
3450 struct got_repository *repo = NULL;
3451 struct got_reflist_head refs;
3452 struct got_worktree *worktree = NULL;
3453 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3454 struct got_object_id *commit_id = NULL;
3455 char *commit_id_str = NULL;
3456 int ch;
3457 struct tog_view *view;
3459 SIMPLEQ_INIT(&refs);
3461 #ifndef PROFILE
3462 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3463 NULL) == -1)
3464 err(1, "pledge");
3465 #endif
3467 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3468 switch (ch) {
3469 case 'c':
3470 commit_id_str = optarg;
3471 break;
3472 case 'r':
3473 repo_path = realpath(optarg, NULL);
3474 if (repo_path == NULL)
3475 err(1, "-r option");
3476 break;
3477 default:
3478 usage_blame();
3479 /* NOTREACHED */
3483 argc -= optind;
3484 argv += optind;
3486 if (argc == 1)
3487 path = argv[0];
3488 else
3489 usage_blame();
3491 cwd = getcwd(NULL, 0);
3492 if (cwd == NULL) {
3493 error = got_error_from_errno("getcwd");
3494 goto done;
3496 if (repo_path == NULL) {
3497 error = got_worktree_open(&worktree, cwd);
3498 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3499 goto done;
3500 else
3501 error = NULL;
3502 if (worktree) {
3503 repo_path =
3504 strdup(got_worktree_get_repo_path(worktree));
3505 if (repo_path == NULL)
3506 error = got_error_from_errno("strdup");
3507 if (error)
3508 goto done;
3509 } else {
3510 repo_path = strdup(cwd);
3511 if (repo_path == NULL) {
3512 error = got_error_from_errno("strdup");
3513 goto done;
3518 init_curses();
3520 error = got_repo_open(&repo, repo_path);
3521 if (error != NULL)
3522 goto done;
3524 error = apply_unveil(got_repo_get_path(repo), NULL);
3525 if (error)
3526 goto done;
3528 if (worktree) {
3529 const char *prefix = got_worktree_get_path_prefix(worktree);
3530 char *p, *worktree_subdir = cwd +
3531 strlen(got_worktree_get_root_path(worktree));
3532 if (asprintf(&p, "%s%s%s%s%s",
3533 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3534 worktree_subdir, worktree_subdir[0] ? "/" : "",
3535 path) == -1) {
3536 error = got_error_from_errno("asprintf");
3537 goto done;
3539 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3540 free(p);
3541 } else {
3542 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3544 if (error)
3545 goto done;
3547 if (commit_id_str == NULL) {
3548 struct got_reference *head_ref;
3549 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3550 if (error != NULL)
3551 goto done;
3552 error = got_ref_resolve(&commit_id, repo, head_ref);
3553 got_ref_close(head_ref);
3554 } else {
3555 error = got_object_resolve_id_str(&commit_id, repo,
3556 commit_id_str);
3558 if (error != NULL)
3559 goto done;
3561 error = got_ref_list(&refs, repo);
3562 if (error)
3563 goto done;
3565 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3566 if (view == NULL) {
3567 error = got_error_from_errno("view_open");
3568 goto done;
3570 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3571 if (error)
3572 goto done;
3573 error = view_loop(view);
3574 done:
3575 free(repo_path);
3576 free(cwd);
3577 free(commit_id);
3578 if (worktree)
3579 got_worktree_close(worktree);
3580 if (repo)
3581 got_repo_close(repo);
3582 got_ref_list_free(&refs);
3583 return error;
3586 static const struct got_error *
3587 draw_tree_entries(struct tog_view *view,
3588 struct got_tree_entry **first_displayed_entry,
3589 struct got_tree_entry **last_displayed_entry,
3590 struct got_tree_entry **selected_entry, int *ndisplayed,
3591 const char *label, int show_ids, const char *parent_path,
3592 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3594 const struct got_error *err = NULL;
3595 struct got_tree_entry *te;
3596 wchar_t *wline;
3597 int width, n;
3599 *ndisplayed = 0;
3601 werase(view->window);
3603 if (limit == 0)
3604 return NULL;
3606 err = format_line(&wline, &width, label, view->ncols);
3607 if (err)
3608 return err;
3609 if (view_needs_focus_indication(view))
3610 wstandout(view->window);
3611 waddwstr(view->window, wline);
3612 if (view_needs_focus_indication(view))
3613 wstandend(view->window);
3614 free(wline);
3615 wline = NULL;
3616 if (width < view->ncols - 1)
3617 waddch(view->window, '\n');
3618 if (--limit <= 0)
3619 return NULL;
3620 err = format_line(&wline, &width, parent_path, view->ncols);
3621 if (err)
3622 return err;
3623 waddwstr(view->window, wline);
3624 free(wline);
3625 wline = NULL;
3626 if (width < view->ncols - 1)
3627 waddch(view->window, '\n');
3628 if (--limit <= 0)
3629 return NULL;
3630 waddch(view->window, '\n');
3631 if (--limit <= 0)
3632 return NULL;
3634 te = SIMPLEQ_FIRST(&entries->head);
3635 if (*first_displayed_entry == NULL) {
3636 if (selected == 0) {
3637 if (view->focussed)
3638 wstandout(view->window);
3639 *selected_entry = NULL;
3641 waddstr(view->window, " ..\n"); /* parent directory */
3642 if (selected == 0 && view->focussed)
3643 wstandend(view->window);
3644 (*ndisplayed)++;
3645 if (--limit <= 0)
3646 return NULL;
3647 n = 1;
3648 } else {
3649 n = 0;
3650 while (te != *first_displayed_entry)
3651 te = SIMPLEQ_NEXT(te, entry);
3654 while (te) {
3655 char *line = NULL, *id_str = NULL;
3657 if (show_ids) {
3658 err = got_object_id_str(&id_str, te->id);
3659 if (err)
3660 return got_error_from_errno(
3661 "got_object_id_str");
3663 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3664 te->name, S_ISDIR(te->mode) ? "/" :
3665 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3666 free(id_str);
3667 return got_error_from_errno("asprintf");
3669 free(id_str);
3670 err = format_line(&wline, &width, line, view->ncols);
3671 if (err) {
3672 free(line);
3673 break;
3675 if (n == selected) {
3676 if (view->focussed)
3677 wstandout(view->window);
3678 *selected_entry = te;
3680 waddwstr(view->window, wline);
3681 if (width < view->ncols - 1)
3682 waddch(view->window, '\n');
3683 if (n == selected && view->focussed)
3684 wstandend(view->window);
3685 free(line);
3686 free(wline);
3687 wline = NULL;
3688 n++;
3689 (*ndisplayed)++;
3690 *last_displayed_entry = te;
3691 if (--limit <= 0)
3692 break;
3693 te = SIMPLEQ_NEXT(te, entry);
3696 return err;
3699 static void
3700 tree_scroll_up(struct tog_view *view,
3701 struct got_tree_entry **first_displayed_entry, int maxscroll,
3702 const struct got_tree_entries *entries, int isroot)
3704 struct got_tree_entry *te, *prev;
3705 int i;
3707 if (*first_displayed_entry == NULL)
3708 return;
3710 te = SIMPLEQ_FIRST(&entries->head);
3711 if (*first_displayed_entry == te) {
3712 if (!isroot)
3713 *first_displayed_entry = NULL;
3714 return;
3717 /* XXX this is stupid... switch to TAILQ? */
3718 for (i = 0; i < maxscroll; i++) {
3719 while (te != *first_displayed_entry) {
3720 prev = te;
3721 te = SIMPLEQ_NEXT(te, entry);
3723 *first_displayed_entry = prev;
3724 te = SIMPLEQ_FIRST(&entries->head);
3726 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3727 *first_displayed_entry = NULL;
3730 static int
3731 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3732 struct got_tree_entry *last_displayed_entry,
3733 const struct got_tree_entries *entries)
3735 struct got_tree_entry *next, *last;
3736 int n = 0;
3738 if (*first_displayed_entry)
3739 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3740 else
3741 next = SIMPLEQ_FIRST(&entries->head);
3742 last = last_displayed_entry;
3743 while (next && last && n++ < maxscroll) {
3744 last = SIMPLEQ_NEXT(last, entry);
3745 if (last) {
3746 *first_displayed_entry = next;
3747 next = SIMPLEQ_NEXT(next, entry);
3750 return n;
3753 static const struct got_error *
3754 tree_entry_path(char **path, struct tog_parent_trees *parents,
3755 struct got_tree_entry *te)
3757 const struct got_error *err = NULL;
3758 struct tog_parent_tree *pt;
3759 size_t len = 2; /* for leading slash and NUL */
3761 TAILQ_FOREACH(pt, parents, entry)
3762 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3763 if (te)
3764 len += strlen(te->name);
3766 *path = calloc(1, len);
3767 if (path == NULL)
3768 return got_error_from_errno("calloc");
3770 (*path)[0] = '/';
3771 pt = TAILQ_LAST(parents, tog_parent_trees);
3772 while (pt) {
3773 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3774 err = got_error(GOT_ERR_NO_SPACE);
3775 goto done;
3777 if (strlcat(*path, "/", len) >= len) {
3778 err = got_error(GOT_ERR_NO_SPACE);
3779 goto done;
3781 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3783 if (te) {
3784 if (strlcat(*path, te->name, len) >= len) {
3785 err = got_error(GOT_ERR_NO_SPACE);
3786 goto done;
3789 done:
3790 if (err) {
3791 free(*path);
3792 *path = NULL;
3794 return err;
3797 static const struct got_error *
3798 blame_tree_entry(struct tog_view **new_view, int begin_x,
3799 struct got_tree_entry *te, struct tog_parent_trees *parents,
3800 struct got_object_id *commit_id, struct got_reflist_head *refs,
3801 struct got_repository *repo)
3803 const struct got_error *err = NULL;
3804 char *path;
3805 struct tog_view *blame_view;
3807 err = tree_entry_path(&path, parents, te);
3808 if (err)
3809 return err;
3811 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3812 if (blame_view == NULL)
3813 return got_error_from_errno("view_open");
3815 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3816 if (err) {
3817 view_close(blame_view);
3818 free(path);
3819 } else
3820 *new_view = blame_view;
3821 return err;
3824 static const struct got_error *
3825 log_tree_entry(struct tog_view **new_view, int begin_x,
3826 struct got_tree_entry *te, struct tog_parent_trees *parents,
3827 struct got_object_id *commit_id, struct got_reflist_head *refs,
3828 struct got_repository *repo)
3830 struct tog_view *log_view;
3831 const struct got_error *err = NULL;
3832 char *path;
3834 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3835 if (log_view == NULL)
3836 return got_error_from_errno("view_open");
3838 err = tree_entry_path(&path, parents, te);
3839 if (err)
3840 return err;
3842 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3843 if (err)
3844 view_close(log_view);
3845 else
3846 *new_view = log_view;
3847 free(path);
3848 return err;
3851 static const struct got_error *
3852 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3853 struct got_object_id *commit_id, struct got_reflist_head *refs,
3854 struct got_repository *repo)
3856 const struct got_error *err = NULL;
3857 char *commit_id_str = NULL;
3858 struct tog_tree_view_state *s = &view->state.tree;
3860 TAILQ_INIT(&s->parents);
3862 err = got_object_id_str(&commit_id_str, commit_id);
3863 if (err != NULL)
3864 goto done;
3866 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3867 err = got_error_from_errno("asprintf");
3868 goto done;
3871 s->root = s->tree = root;
3872 s->entries = got_object_tree_get_entries(root);
3873 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3874 s->commit_id = got_object_id_dup(commit_id);
3875 if (s->commit_id == NULL) {
3876 err = got_error_from_errno("got_object_id_dup");
3877 goto done;
3879 s->refs = refs;
3880 s->repo = repo;
3882 view->show = show_tree_view;
3883 view->input = input_tree_view;
3884 view->close = close_tree_view;
3885 done:
3886 free(commit_id_str);
3887 if (err) {
3888 free(s->tree_label);
3889 s->tree_label = NULL;
3891 return err;
3894 static const struct got_error *
3895 close_tree_view(struct tog_view *view)
3897 struct tog_tree_view_state *s = &view->state.tree;
3899 free(s->tree_label);
3900 s->tree_label = NULL;
3901 free(s->commit_id);
3902 s->commit_id = NULL;
3903 while (!TAILQ_EMPTY(&s->parents)) {
3904 struct tog_parent_tree *parent;
3905 parent = TAILQ_FIRST(&s->parents);
3906 TAILQ_REMOVE(&s->parents, parent, entry);
3907 free(parent);
3910 if (s->tree != s->root)
3911 got_object_tree_close(s->tree);
3912 got_object_tree_close(s->root);
3914 return NULL;
3917 static const struct got_error *
3918 show_tree_view(struct tog_view *view)
3920 const struct got_error *err = NULL;
3921 struct tog_tree_view_state *s = &view->state.tree;
3922 char *parent_path;
3924 err = tree_entry_path(&parent_path, &s->parents, NULL);
3925 if (err)
3926 return err;
3928 err = draw_tree_entries(view, &s->first_displayed_entry,
3929 &s->last_displayed_entry, &s->selected_entry,
3930 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3931 s->entries, s->selected, view->nlines, s->tree == s->root);
3932 free(parent_path);
3934 view_vborder(view);
3935 return err;
3938 static const struct got_error *
3939 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3940 struct tog_view **focus_view, struct tog_view *view, int ch)
3942 const struct got_error *err = NULL;
3943 struct tog_tree_view_state *s = &view->state.tree;
3944 struct tog_view *log_view;
3945 int begin_x = 0, nscrolled;
3947 switch (ch) {
3948 case 'i':
3949 s->show_ids = !s->show_ids;
3950 break;
3951 case 'l':
3952 if (!s->selected_entry)
3953 break;
3954 if (view_is_parent_view(view))
3955 begin_x = view_split_begin_x(view->begin_x);
3956 err = log_tree_entry(&log_view, begin_x,
3957 s->selected_entry, &s->parents,
3958 s->commit_id, s->refs, s->repo);
3959 if (view_is_parent_view(view)) {
3960 err = view_close_child(view);
3961 if (err)
3962 return err;
3963 err = view_set_child(view, log_view);
3964 if (err) {
3965 view_close(log_view);
3966 break;
3968 *focus_view = log_view;
3969 view->child_focussed = 1;
3970 } else
3971 *new_view = log_view;
3972 break;
3973 case 'k':
3974 case KEY_UP:
3975 if (s->selected > 0) {
3976 s->selected--;
3977 if (s->selected == 0)
3978 break;
3980 if (s->selected > 0)
3981 break;
3982 tree_scroll_up(view, &s->first_displayed_entry, 1,
3983 s->entries, s->tree == s->root);
3984 break;
3985 case KEY_PPAGE:
3986 tree_scroll_up(view, &s->first_displayed_entry,
3987 MAX(0, view->nlines - 4 - s->selected), s->entries,
3988 s->tree == s->root);
3989 s->selected = 0;
3990 if (SIMPLEQ_FIRST(&s->entries->head) ==
3991 s->first_displayed_entry && s->tree != s->root)
3992 s->first_displayed_entry = NULL;
3993 break;
3994 case 'j':
3995 case KEY_DOWN:
3996 if (s->selected < s->ndisplayed - 1) {
3997 s->selected++;
3998 break;
4000 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4001 /* can't scroll any further */
4002 break;
4003 tree_scroll_down(&s->first_displayed_entry, 1,
4004 s->last_displayed_entry, s->entries);
4005 break;
4006 case KEY_NPAGE:
4007 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4008 == NULL) {
4009 /* can't scroll any further; move cursor down */
4010 if (s->selected < s->ndisplayed - 1)
4011 s->selected = s->ndisplayed - 1;
4012 break;
4014 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4015 view->nlines, s->last_displayed_entry, s->entries);
4016 if (nscrolled < view->nlines) {
4017 int ndisplayed = 0;
4018 struct got_tree_entry *te;
4019 te = s->first_displayed_entry;
4020 do {
4021 ndisplayed++;
4022 te = SIMPLEQ_NEXT(te, entry);
4023 } while (te);
4024 s->selected = ndisplayed - 1;
4026 break;
4027 case KEY_ENTER:
4028 case '\r':
4029 case KEY_BACKSPACE:
4030 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4031 struct tog_parent_tree *parent;
4032 /* user selected '..' */
4033 if (s->tree == s->root)
4034 break;
4035 parent = TAILQ_FIRST(&s->parents);
4036 TAILQ_REMOVE(&s->parents, parent,
4037 entry);
4038 got_object_tree_close(s->tree);
4039 s->tree = parent->tree;
4040 s->entries =
4041 got_object_tree_get_entries(s->tree);
4042 s->first_displayed_entry =
4043 parent->first_displayed_entry;
4044 s->selected_entry =
4045 parent->selected_entry;
4046 s->selected = parent->selected;
4047 free(parent);
4048 } else if (S_ISDIR(s->selected_entry->mode)) {
4049 struct got_tree_object *subtree;
4050 err = got_object_open_as_tree(&subtree,
4051 s->repo, s->selected_entry->id);
4052 if (err)
4053 break;
4054 err = tree_view_visit_subtree(subtree, s);
4055 if (err) {
4056 got_object_tree_close(subtree);
4057 break;
4059 } else if (S_ISREG(s->selected_entry->mode)) {
4060 struct tog_view *blame_view;
4061 int begin_x = view_is_parent_view(view) ?
4062 view_split_begin_x(view->begin_x) : 0;
4064 err = blame_tree_entry(&blame_view, begin_x,
4065 s->selected_entry, &s->parents,
4066 s->commit_id, s->refs, s->repo);
4067 if (err)
4068 break;
4069 if (view_is_parent_view(view)) {
4070 err = view_close_child(view);
4071 if (err)
4072 return err;
4073 err = view_set_child(view, blame_view);
4074 if (err) {
4075 view_close(blame_view);
4076 break;
4078 *focus_view = blame_view;
4079 view->child_focussed = 1;
4080 } else
4081 *new_view = blame_view;
4083 break;
4084 case KEY_RESIZE:
4085 if (s->selected > view->nlines)
4086 s->selected = s->ndisplayed - 1;
4087 break;
4088 default:
4089 break;
4092 return err;
4095 __dead static void
4096 usage_tree(void)
4098 endwin();
4099 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4100 getprogname());
4101 exit(1);
4104 static const struct got_error *
4105 cmd_tree(int argc, char *argv[])
4107 const struct got_error *error;
4108 struct got_repository *repo = NULL;
4109 struct got_reflist_head refs;
4110 char *repo_path = NULL;
4111 struct got_object_id *commit_id = NULL;
4112 char *commit_id_arg = NULL;
4113 struct got_commit_object *commit = NULL;
4114 struct got_tree_object *tree = NULL;
4115 int ch;
4116 struct tog_view *view;
4118 SIMPLEQ_INIT(&refs);
4120 #ifndef PROFILE
4121 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4122 NULL) == -1)
4123 err(1, "pledge");
4124 #endif
4126 while ((ch = getopt(argc, argv, "c:")) != -1) {
4127 switch (ch) {
4128 case 'c':
4129 commit_id_arg = optarg;
4130 break;
4131 default:
4132 usage_tree();
4133 /* NOTREACHED */
4137 argc -= optind;
4138 argv += optind;
4140 if (argc == 0) {
4141 struct got_worktree *worktree;
4142 char *cwd = getcwd(NULL, 0);
4143 if (cwd == NULL)
4144 return got_error_from_errno("getcwd");
4145 error = got_worktree_open(&worktree, cwd);
4146 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4147 goto done;
4148 if (worktree) {
4149 free(cwd);
4150 repo_path =
4151 strdup(got_worktree_get_repo_path(worktree));
4152 got_worktree_close(worktree);
4153 } else
4154 repo_path = cwd;
4155 if (repo_path == NULL) {
4156 error = got_error_from_errno("strdup");
4157 goto done;
4159 } else if (argc == 1) {
4160 repo_path = realpath(argv[0], NULL);
4161 if (repo_path == NULL)
4162 return got_error_from_errno2("realpath", argv[0]);
4163 } else
4164 usage_log();
4166 init_curses();
4168 error = got_repo_open(&repo, repo_path);
4169 if (error != NULL)
4170 goto done;
4172 error = apply_unveil(got_repo_get_path(repo), NULL);
4173 if (error)
4174 goto done;
4176 if (commit_id_arg == NULL)
4177 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4178 else
4179 error = got_object_resolve_id_str(&commit_id, repo,
4180 commit_id_arg);
4181 if (error != NULL)
4182 goto done;
4184 error = got_object_open_as_commit(&commit, repo, commit_id);
4185 if (error != NULL)
4186 goto done;
4188 error = got_object_open_as_tree(&tree, repo,
4189 got_object_commit_get_tree_id(commit));
4190 if (error != NULL)
4191 goto done;
4193 error = got_ref_list(&refs, repo);
4194 if (error)
4195 goto done;
4197 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4198 if (view == NULL) {
4199 error = got_error_from_errno("view_open");
4200 goto done;
4202 error = open_tree_view(view, tree, commit_id, &refs, repo);
4203 if (error)
4204 goto done;
4205 error = view_loop(view);
4206 done:
4207 free(repo_path);
4208 free(commit_id);
4209 if (commit)
4210 got_object_commit_close(commit);
4211 if (tree)
4212 got_object_tree_close(tree);
4213 if (repo)
4214 got_repo_close(repo);
4215 got_ref_list_free(&refs);
4216 return error;
4219 __dead static void
4220 usage(void)
4222 int i;
4224 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4225 "Available commands:\n", getprogname());
4226 for (i = 0; i < nitems(tog_commands); i++) {
4227 struct tog_cmd *cmd = &tog_commands[i];
4228 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4230 exit(1);
4233 static char **
4234 make_argv(const char *arg0, const char *arg1)
4236 char **argv;
4237 int argc = (arg1 == NULL ? 1 : 2);
4239 argv = calloc(argc, sizeof(char *));
4240 if (argv == NULL)
4241 err(1, "calloc");
4242 argv[0] = strdup(arg0);
4243 if (argv[0] == NULL)
4244 err(1, "calloc");
4245 if (arg1) {
4246 argv[1] = strdup(arg1);
4247 if (argv[1] == NULL)
4248 err(1, "calloc");
4251 return argv;
4254 int
4255 main(int argc, char *argv[])
4257 const struct got_error *error = NULL;
4258 struct tog_cmd *cmd = NULL;
4259 int ch, hflag = 0;
4260 char **cmd_argv = NULL;
4262 setlocale(LC_CTYPE, "");
4264 while ((ch = getopt(argc, argv, "h")) != -1) {
4265 switch (ch) {
4266 case 'h':
4267 hflag = 1;
4268 break;
4269 default:
4270 usage();
4271 /* NOTREACHED */
4275 argc -= optind;
4276 argv += optind;
4277 optind = 0;
4278 optreset = 1;
4280 if (argc == 0) {
4281 if (hflag)
4282 usage();
4283 /* Build an argument vector which runs a default command. */
4284 cmd = &tog_commands[0];
4285 cmd_argv = make_argv(cmd->name, NULL);
4286 argc = 1;
4287 } else {
4288 int i;
4290 /* Did the user specific a command? */
4291 for (i = 0; i < nitems(tog_commands); i++) {
4292 if (strncmp(tog_commands[i].name, argv[0],
4293 strlen(argv[0])) == 0) {
4294 cmd = &tog_commands[i];
4295 if (hflag)
4296 tog_commands[i].cmd_usage();
4297 break;
4300 if (cmd == NULL) {
4301 /* Did the user specify a repository? */
4302 char *repo_path = realpath(argv[0], NULL);
4303 if (repo_path) {
4304 struct got_repository *repo;
4305 error = got_repo_open(&repo, repo_path);
4306 if (error == NULL)
4307 got_repo_close(repo);
4308 } else
4309 error = got_error_from_errno2("realpath",
4310 argv[0]);
4311 if (error) {
4312 if (hflag) {
4313 fprintf(stderr, "%s: '%s' is not a "
4314 "known command\n", getprogname(),
4315 argv[0]);
4316 usage();
4318 fprintf(stderr, "%s: '%s' is neither a known "
4319 "command nor a path to a repository\n",
4320 getprogname(), argv[0]);
4321 free(repo_path);
4322 return 1;
4324 cmd = &tog_commands[0];
4325 cmd_argv = make_argv(cmd->name, repo_path);
4326 argc = 2;
4327 free(repo_path);
4331 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4332 if (error)
4333 goto done;
4334 done:
4335 endwin();
4336 free(cmd_argv);
4337 if (error)
4338 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4339 return 0;