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_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_commit_graph.h"
49 #include "got_utf8.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
52 #include "got_path.h"
53 #include "got_worktree.h"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 #ifndef MAX
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 #endif
63 #define CTRL(x) ((x) & 0x1f)
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 struct tog_cmd {
70 const char *name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 };
75 __dead static void usage(int);
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 { "diff", cmd_diff, usage_diff },
89 { "blame", cmd_blame, usage_blame },
90 { "tree", cmd_tree, usage_tree },
91 };
93 enum tog_view_type {
94 TOG_VIEW_DIFF,
95 TOG_VIEW_LOG,
96 TOG_VIEW_BLAME,
97 TOG_VIEW_TREE
98 };
100 #define TOG_EOF_STRING "(END)"
102 struct commit_queue_entry {
103 TAILQ_ENTRY(commit_queue_entry) entry;
104 struct got_object_id *id;
105 struct got_commit_object *commit;
106 int idx;
107 };
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
110 int ncommits;
111 struct commit_queue_head head;
112 };
114 struct tog_diff_view_state {
115 struct got_object_id *id1, *id2;
116 FILE *f;
117 int first_displayed_line;
118 int last_displayed_line;
119 int eof;
120 int diff_context;
121 struct got_repository *repo;
122 struct got_reflist_head *refs;
124 /* passed from log view; may be NULL */
125 struct tog_view *log_view;
126 };
128 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 struct tog_log_thread_args {
131 pthread_cond_t need_commits;
132 int commits_needed;
133 struct got_commit_graph *graph;
134 struct commit_queue *commits;
135 const char *in_repo_path;
136 struct got_object_id *start_id;
137 struct got_repository *repo;
138 int log_complete;
139 sig_atomic_t *quit;
140 struct tog_view *view;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
143 };
145 struct tog_log_view_state {
146 struct commit_queue commits;
147 struct commit_queue_entry *first_displayed_entry;
148 struct commit_queue_entry *last_displayed_entry;
149 struct commit_queue_entry *selected_entry;
150 int selected;
151 char *in_repo_path;
152 const char *head_ref_name;
153 struct got_repository *repo;
154 struct got_reflist_head *refs;
155 struct got_object_id *start_id;
156 sig_atomic_t quit;
157 pthread_t thread;
158 struct tog_log_thread_args thread_args;
159 struct commit_queue_entry *matched_entry;
160 struct commit_queue_entry *search_entry;
161 };
163 struct tog_blame_cb_args {
164 struct tog_blame_line *lines; /* one per line */
165 int nlines;
167 struct tog_view *view;
168 struct got_object_id *commit_id;
169 int *quit;
170 };
172 struct tog_blame_thread_args {
173 const char *path;
174 struct got_repository *repo;
175 struct tog_blame_cb_args *cb_args;
176 int *complete;
177 };
179 struct tog_blame {
180 FILE *f;
181 size_t filesize;
182 struct tog_blame_line *lines;
183 int nlines;
184 off_t *line_offsets;
185 pthread_t thread;
186 struct tog_blame_thread_args thread_args;
187 struct tog_blame_cb_args cb_args;
188 const char *path;
189 };
191 struct tog_blame_view_state {
192 int first_displayed_line;
193 int last_displayed_line;
194 int selected_line;
195 int blame_complete;
196 int eof;
197 int done;
198 struct got_object_id_queue blamed_commits;
199 struct got_object_qid *blamed_commit;
200 char *path;
201 struct got_repository *repo;
202 struct got_reflist_head *refs;
203 struct got_object_id *commit_id;
204 struct tog_blame blame;
205 int matched_line;
206 };
208 struct tog_parent_tree {
209 TAILQ_ENTRY(tog_parent_tree) entry;
210 struct got_tree_object *tree;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *selected_entry;
213 int selected;
214 };
216 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 struct tog_tree_view_state {
219 char *tree_label;
220 struct got_tree_object *root;
221 struct got_tree_object *tree;
222 const struct got_tree_entries *entries;
223 struct got_tree_entry *first_displayed_entry;
224 struct got_tree_entry *last_displayed_entry;
225 struct got_tree_entry *selected_entry;
226 int ndisplayed, selected, show_ids;
227 struct tog_parent_trees parents;
228 struct got_object_id *commit_id;
229 struct got_repository *repo;
230 struct got_reflist_head *refs;
231 struct got_tree_entry *matched_entry;
232 };
234 /*
235 * We implement two types of views: parent views and child views.
237 * The 'Tab' key switches between a parent view and its child view.
238 * Child views are shown side-by-side to their parent view, provided
239 * there is enough screen estate.
241 * When a new view is opened from within a parent view, this new view
242 * becomes a child view of the parent view, replacing any existing child.
244 * When a new view is opened from within a child view, this new view
245 * becomes a parent view which will obscure the views below until the
246 * user quits the new parent view by typing 'q'.
248 * This list of views contains parent views only.
249 * Child views are only pointed to by their parent view.
250 */
251 TAILQ_HEAD(tog_view_list_head, tog_view);
253 struct tog_view {
254 TAILQ_ENTRY(tog_view) entry;
255 WINDOW *window;
256 PANEL *panel;
257 int nlines, ncols, begin_y, begin_x;
258 int lines, cols; /* copies of LINES and COLS */
259 int focussed;
260 struct tog_view *parent;
261 struct tog_view *child;
262 int child_focussed;
264 /* type-specific state */
265 enum tog_view_type type;
266 union {
267 struct tog_diff_view_state diff;
268 struct tog_log_view_state log;
269 struct tog_blame_view_state blame;
270 struct tog_tree_view_state tree;
271 } state;
273 const struct got_error *(*show)(struct tog_view *);
274 const struct got_error *(*input)(struct tog_view **,
275 struct tog_view **, struct tog_view**, struct tog_view *, int);
276 const struct got_error *(*close)(struct tog_view *);
278 const struct got_error *(*search_start)(struct tog_view *);
279 const struct got_error *(*search_next)(struct tog_view *);
280 int searching;
281 #define TOG_SEARCH_FORWARD 1
282 #define TOG_SEARCH_BACKWARD 2
283 int search_next_done;
284 regex_t regex;
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 *, 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 *);
311 static const struct got_error *search_start_blame_view(struct tog_view *);
312 static const struct got_error *search_next_blame_view(struct tog_view *);
314 static const struct got_error *open_tree_view(struct tog_view *,
315 struct got_tree_object *, struct got_object_id *,
316 struct got_reflist_head *, struct got_repository *);
317 static const struct got_error *show_tree_view(struct tog_view *);
318 static const struct got_error *input_tree_view(struct tog_view **,
319 struct tog_view **, struct tog_view **, struct tog_view *, int);
320 static const struct got_error *close_tree_view(struct tog_view *);
321 static const struct got_error *search_start_tree_view(struct tog_view *);
322 static const struct got_error *search_next_tree_view(struct tog_view *);
324 static volatile sig_atomic_t tog_sigwinch_received;
325 static volatile sig_atomic_t tog_sigpipe_received;
327 static void
328 tog_sigwinch(int signo)
330 tog_sigwinch_received = 1;
333 static void
334 tog_sigpipe(int signo)
336 tog_sigpipe_received = 1;
339 static const struct got_error *
340 view_close(struct tog_view *view)
342 const struct got_error *err = NULL;
344 if (view->child) {
345 view_close(view->child);
346 view->child = NULL;
348 if (view->close)
349 err = view->close(view);
350 if (view->panel)
351 del_panel(view->panel);
352 if (view->window)
353 delwin(view->window);
354 free(view);
355 return err;
358 static struct tog_view *
359 view_open(int nlines, int ncols, int begin_y, int begin_x,
360 enum tog_view_type type)
362 struct tog_view *view = calloc(1, sizeof(*view));
364 if (view == NULL)
365 return NULL;
367 view->type = type;
368 view->lines = LINES;
369 view->cols = COLS;
370 view->nlines = nlines ? nlines : LINES - begin_y;
371 view->ncols = ncols ? ncols : COLS - begin_x;
372 view->begin_y = begin_y;
373 view->begin_x = begin_x;
374 view->window = newwin(nlines, ncols, begin_y, begin_x);
375 if (view->window == NULL) {
376 view_close(view);
377 return NULL;
379 view->panel = new_panel(view->window);
380 if (view->panel == NULL ||
381 set_panel_userptr(view->panel, view) != OK) {
382 view_close(view);
383 return NULL;
386 keypad(view->window, TRUE);
387 return view;
390 static int
391 view_split_begin_x(int begin_x)
393 if (begin_x > 0 || COLS < 120)
394 return 0;
395 return (COLS - MAX(COLS / 2, 80));
398 static const struct got_error *view_resize(struct tog_view *);
400 static const struct got_error *
401 view_splitscreen(struct tog_view *view)
403 const struct got_error *err = NULL;
405 view->begin_y = 0;
406 view->begin_x = view_split_begin_x(0);
407 view->nlines = LINES;
408 view->ncols = COLS - view->begin_x;
409 view->lines = LINES;
410 view->cols = COLS;
411 err = view_resize(view);
412 if (err)
413 return err;
415 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
416 return got_error_from_errno("mvwin");
418 return NULL;
421 static const struct got_error *
422 view_fullscreen(struct tog_view *view)
424 const struct got_error *err = NULL;
426 view->begin_x = 0;
427 view->begin_y = 0;
428 view->nlines = LINES;
429 view->ncols = COLS;
430 view->lines = LINES;
431 view->cols = COLS;
432 err = view_resize(view);
433 if (err)
434 return err;
436 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
437 return got_error_from_errno("mvwin");
439 return NULL;
442 static int
443 view_is_parent_view(struct tog_view *view)
445 return view->parent == NULL;
448 static const struct got_error *
449 view_resize(struct tog_view *view)
451 int nlines, ncols;
453 if (view->lines > LINES)
454 nlines = view->nlines - (view->lines - LINES);
455 else
456 nlines = view->nlines + (LINES - view->lines);
458 if (view->cols > COLS)
459 ncols = view->ncols - (view->cols - COLS);
460 else
461 ncols = view->ncols + (COLS - view->cols);
463 if (wresize(view->window, nlines, ncols) == ERR)
464 return got_error_from_errno("wresize");
465 if (replace_panel(view->panel, view->window) == ERR)
466 return got_error_from_errno("replace_panel");
467 wclear(view->window);
469 view->nlines = nlines;
470 view->ncols = ncols;
471 view->lines = LINES;
472 view->cols = COLS;
474 if (view->child) {
475 view->child->begin_x = view_split_begin_x(view->begin_x);
476 if (view->child->begin_x == 0) {
477 view_fullscreen(view->child);
478 if (view->child->focussed)
479 show_panel(view->child->panel);
480 else
481 show_panel(view->panel);
482 } else {
483 view_splitscreen(view->child);
484 show_panel(view->child->panel);
488 return NULL;
491 static const struct got_error *
492 view_close_child(struct tog_view *view)
494 const struct got_error *err = NULL;
496 if (view->child == NULL)
497 return NULL;
499 err = view_close(view->child);
500 view->child = NULL;
501 return err;
504 static const struct got_error *
505 view_set_child(struct tog_view *view, struct tog_view *child)
507 const struct got_error *err = NULL;
509 view->child = child;
510 child->parent = view;
511 return err;
514 static int
515 view_is_splitscreen(struct tog_view *view)
517 return view->begin_x > 0;
520 static void
521 tog_resizeterm(void)
523 int cols, lines;
524 struct winsize size;
526 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
527 cols = 80; /* Default */
528 lines = 24;
529 } else {
530 cols = size.ws_col;
531 lines = size.ws_row;
533 resize_term(lines, cols);
536 static const struct got_error *
537 view_search_start(struct tog_view *view)
539 const struct got_error *err = NULL;
540 char pattern[1024];
541 int ret;
543 if (view->nlines < 1)
544 return NULL;
546 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
547 view->begin_x, "/");
548 wclrtoeol(view->window);
550 nocbreak();
551 echo();
552 ret = wgetnstr(view->window, pattern, sizeof(pattern));
553 cbreak();
554 noecho();
555 if (ret == ERR)
556 return NULL;
558 if (view->searching) {
559 regfree(&view->regex);
560 view->searching = 0;
563 if (regcomp(&view->regex, pattern,
564 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
565 err = view->search_start(view);
566 if (err) {
567 regfree(&view->regex);
568 return err;
570 view->searching = TOG_SEARCH_FORWARD;
571 view->search_next_done = 0;
572 view->search_next(view);
575 return NULL;
578 static const struct got_error *
579 view_input(struct tog_view **new, struct tog_view **dead,
580 struct tog_view **focus, int *done, struct tog_view *view,
581 struct tog_view_list_head *views)
583 const struct got_error *err = NULL;
584 struct tog_view *v;
585 int ch, errcode;
587 *new = NULL;
588 *dead = NULL;
589 *focus = NULL;
591 if (view->searching && !view->search_next_done) {
592 errcode = pthread_mutex_unlock(&tog_mutex);
593 if (errcode)
594 return got_error_set_errno(errcode,
595 "pthread_mutex_unlock");
596 pthread_yield();
597 errcode = pthread_mutex_lock(&tog_mutex);
598 if (errcode)
599 return got_error_set_errno(errcode,
600 "pthread_mutex_lock");
601 view->search_next(view);
602 return NULL;
605 nodelay(stdscr, FALSE);
606 /* Allow threads to make progress while we are waiting for input. */
607 errcode = pthread_mutex_unlock(&tog_mutex);
608 if (errcode)
609 return got_error_set_errno(errcode, "pthread_mutex_unlock");
610 ch = wgetch(view->window);
611 errcode = pthread_mutex_lock(&tog_mutex);
612 if (errcode)
613 return got_error_set_errno(errcode, "pthread_mutex_lock");
614 nodelay(stdscr, TRUE);
616 if (tog_sigwinch_received) {
617 tog_resizeterm();
618 tog_sigwinch_received = 0;
619 TAILQ_FOREACH(v, views, entry) {
620 err = view_resize(v);
621 if (err)
622 return err;
623 err = v->input(new, dead, focus, v, KEY_RESIZE);
624 if (err)
625 return err;
629 switch (ch) {
630 case ERR:
631 break;
632 case '\t':
633 if (view->child) {
634 *focus = view->child;
635 view->child_focussed = 1;
636 } else if (view->parent) {
637 *focus = view->parent;
638 view->parent->child_focussed = 0;
640 break;
641 case 'q':
642 err = view->input(new, dead, focus, view, ch);
643 *dead = view;
644 break;
645 case 'Q':
646 *done = 1;
647 break;
648 case 'f':
649 if (view_is_parent_view(view)) {
650 if (view->child == NULL)
651 break;
652 if (view_is_splitscreen(view->child)) {
653 *focus = view->child;
654 view->child_focussed = 1;
655 err = view_fullscreen(view->child);
656 } else
657 err = view_splitscreen(view->child);
658 if (err)
659 break;
660 err = view->child->input(new, dead, focus,
661 view->child, KEY_RESIZE);
662 } else {
663 if (view_is_splitscreen(view)) {
664 *focus = view;
665 view->parent->child_focussed = 1;
666 err = view_fullscreen(view);
667 } else {
668 err = view_splitscreen(view);
670 if (err)
671 break;
672 err = view->input(new, dead, focus, view,
673 KEY_RESIZE);
675 break;
676 case KEY_RESIZE:
677 break;
678 case '/':
679 if (view->search_start)
680 view_search_start(view);
681 else
682 err = view->input(new, dead, focus, view, ch);
683 break;
684 case 'N':
685 case 'n':
686 if (view->search_next && view->searching) {
687 view->searching = (ch == 'n' ?
688 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
689 view->search_next_done = 0;
690 view->search_next(view);
691 } else
692 err = view->input(new, dead, focus, view, ch);
693 break;
694 default:
695 err = view->input(new, dead, focus, view, ch);
696 break;
699 return err;
702 void
703 view_vborder(struct tog_view *view)
705 PANEL *panel;
706 struct tog_view *view_above;
708 if (view->parent)
709 return view_vborder(view->parent);
711 panel = panel_above(view->panel);
712 if (panel == NULL)
713 return;
715 view_above = panel_userptr(panel);
716 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
717 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
720 int
721 view_needs_focus_indication(struct tog_view *view)
723 if (view_is_parent_view(view)) {
724 if (view->child == NULL || view->child_focussed)
725 return 0;
726 if (!view_is_splitscreen(view->child))
727 return 0;
728 } else if (!view_is_splitscreen(view))
729 return 0;
731 return view->focussed;
734 static const struct got_error *
735 view_loop(struct tog_view *view)
737 const struct got_error *err = NULL;
738 struct tog_view_list_head views;
739 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
740 int fast_refresh = 10;
741 int done = 0, errcode;
743 errcode = pthread_mutex_lock(&tog_mutex);
744 if (errcode)
745 return got_error_set_errno(errcode, "pthread_mutex_lock");
747 TAILQ_INIT(&views);
748 TAILQ_INSERT_HEAD(&views, view, entry);
750 main_view = view;
751 view->focussed = 1;
752 err = view->show(view);
753 if (err)
754 return err;
755 update_panels();
756 doupdate();
757 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
758 /* Refresh fast during initialization, then become slower. */
759 if (fast_refresh && fast_refresh-- == 0)
760 halfdelay(10); /* switch to once per second */
762 err = view_input(&new_view, &dead_view, &focus_view, &done,
763 view, &views);
764 if (err)
765 break;
766 if (dead_view) {
767 struct tog_view *prev = NULL;
769 if (view_is_parent_view(dead_view))
770 prev = TAILQ_PREV(dead_view,
771 tog_view_list_head, entry);
772 else if (view->parent != dead_view)
773 prev = view->parent;
775 if (dead_view->parent)
776 dead_view->parent->child = NULL;
777 else
778 TAILQ_REMOVE(&views, dead_view, entry);
780 err = view_close(dead_view);
781 if (err || (dead_view == main_view && new_view == NULL))
782 goto done;
784 if (view == dead_view) {
785 if (focus_view)
786 view = focus_view;
787 else if (prev)
788 view = prev;
789 else if (!TAILQ_EMPTY(&views))
790 view = TAILQ_LAST(&views,
791 tog_view_list_head);
792 else
793 view = NULL;
794 if (view) {
795 if (view->child && view->child_focussed)
796 focus_view = view->child;
797 else
798 focus_view = view;
802 if (new_view) {
803 struct tog_view *v, *t;
804 /* Only allow one parent view per type. */
805 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
806 if (v->type != new_view->type)
807 continue;
808 TAILQ_REMOVE(&views, v, entry);
809 err = view_close(v);
810 if (err)
811 goto done;
812 break;
814 TAILQ_INSERT_TAIL(&views, new_view, entry);
815 view = new_view;
816 if (focus_view == NULL)
817 focus_view = new_view;
819 if (focus_view) {
820 show_panel(focus_view->panel);
821 if (view)
822 view->focussed = 0;
823 focus_view->focussed = 1;
824 view = focus_view;
825 if (new_view)
826 show_panel(new_view->panel);
827 if (view->child && view_is_splitscreen(view->child))
828 show_panel(view->child->panel);
830 if (view) {
831 if (focus_view == NULL) {
832 view->focussed = 1;
833 show_panel(view->panel);
834 if (view->child && view_is_splitscreen(view->child))
835 show_panel(view->child->panel);
836 focus_view = view;
838 if (view->parent) {
839 err = view->parent->show(view->parent);
840 if (err)
841 goto done;
843 err = view->show(view);
844 if (err)
845 goto done;
846 if (view->child) {
847 err = view->child->show(view->child);
848 if (err)
849 goto done;
851 update_panels();
852 doupdate();
855 done:
856 while (!TAILQ_EMPTY(&views)) {
857 view = TAILQ_FIRST(&views);
858 TAILQ_REMOVE(&views, view, entry);
859 view_close(view);
862 errcode = pthread_mutex_unlock(&tog_mutex);
863 if (errcode && err == NULL)
864 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
866 return err;
869 __dead static void
870 usage_log(void)
872 endwin();
873 fprintf(stderr,
874 "usage: %s log [-c commit] [-r repository-path] [path]\n",
875 getprogname());
876 exit(1);
879 /* Create newly allocated wide-character string equivalent to a byte string. */
880 static const struct got_error *
881 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
883 char *vis = NULL;
884 const struct got_error *err = NULL;
886 *ws = NULL;
887 *wlen = mbstowcs(NULL, s, 0);
888 if (*wlen == (size_t)-1) {
889 int vislen;
890 if (errno != EILSEQ)
891 return got_error_from_errno("mbstowcs");
893 /* byte string invalid in current encoding; try to "fix" it */
894 err = got_mbsavis(&vis, &vislen, s);
895 if (err)
896 return err;
897 *wlen = mbstowcs(NULL, vis, 0);
898 if (*wlen == (size_t)-1) {
899 err = got_error_from_errno("mbstowcs"); /* give up */
900 goto done;
904 *ws = calloc(*wlen + 1, sizeof(*ws));
905 if (*ws == NULL) {
906 err = got_error_from_errno("calloc");
907 goto done;
910 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
911 err = got_error_from_errno("mbstowcs");
912 done:
913 free(vis);
914 if (err) {
915 free(*ws);
916 *ws = NULL;
917 *wlen = 0;
919 return err;
922 /* Format a line for display, ensuring that it won't overflow a width limit. */
923 static const struct got_error *
924 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
926 const struct got_error *err = NULL;
927 int cols = 0;
928 wchar_t *wline = NULL;
929 size_t wlen;
930 int i;
932 *wlinep = NULL;
933 *widthp = 0;
935 err = mbs2ws(&wline, &wlen, line);
936 if (err)
937 return err;
939 i = 0;
940 while (i < wlen && cols < wlimit) {
941 int width = wcwidth(wline[i]);
942 switch (width) {
943 case 0:
944 i++;
945 break;
946 case 1:
947 case 2:
948 if (cols + width <= wlimit)
949 cols += width;
950 i++;
951 break;
952 case -1:
953 if (wline[i] == L'\t')
954 cols += TABSIZE - ((cols + 1) % TABSIZE);
955 i++;
956 break;
957 default:
958 err = got_error_from_errno("wcwidth");
959 goto done;
962 wline[i] = L'\0';
963 if (widthp)
964 *widthp = cols;
965 done:
966 if (err)
967 free(wline);
968 else
969 *wlinep = wline;
970 return err;
973 static const struct got_error*
974 build_refs_str(char **refs_str, struct got_reflist_head *refs,
975 struct got_object_id *id, struct got_repository *repo)
977 static const struct got_error *err = NULL;
978 struct got_reflist_entry *re;
979 char *s;
980 const char *name;
982 *refs_str = NULL;
984 SIMPLEQ_FOREACH(re, refs, entry) {
985 struct got_tag_object *tag = NULL;
986 int cmp;
988 name = got_ref_get_name(re->ref);
989 if (strcmp(name, GOT_REF_HEAD) == 0)
990 continue;
991 if (strncmp(name, "refs/", 5) == 0)
992 name += 5;
993 if (strncmp(name, "got/", 4) == 0)
994 continue;
995 if (strncmp(name, "heads/", 6) == 0)
996 name += 6;
997 if (strncmp(name, "remotes/", 8) == 0)
998 name += 8;
999 if (strncmp(name, "tags/", 5) == 0) {
1000 err = got_object_open_as_tag(&tag, repo, re->id);
1001 if (err) {
1002 if (err->code != GOT_ERR_OBJ_TYPE)
1003 break;
1004 /* Ref points at something other than a tag. */
1005 err = NULL;
1006 tag = NULL;
1009 cmp = got_object_id_cmp(tag ?
1010 got_object_tag_get_object_id(tag) : re->id, id);
1011 if (tag)
1012 got_object_tag_close(tag);
1013 if (cmp != 0)
1014 continue;
1015 s = *refs_str;
1016 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1017 s ? ", " : "", name) == -1) {
1018 err = got_error_from_errno("asprintf");
1019 free(s);
1020 *refs_str = NULL;
1021 break;
1023 free(s);
1026 return err;
1029 static const struct got_error *
1030 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1032 char *smallerthan, *at;
1034 smallerthan = strchr(author, '<');
1035 if (smallerthan && smallerthan[1] != '\0')
1036 author = smallerthan + 1;
1037 at = strchr(author, '@');
1038 if (at)
1039 *at = '\0';
1040 return format_line(wauthor, author_width, author, limit);
1043 static const struct got_error *
1044 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1045 struct got_object_id *id, struct got_reflist_head *refs,
1046 int author_display_cols)
1048 const struct got_error *err = NULL;
1049 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1050 char *logmsg0 = NULL, *logmsg = NULL;
1051 char *author = NULL;
1052 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1053 int author_width, logmsg_width;
1054 char *newline, *line = NULL;
1055 int col, limit;
1056 static const size_t date_display_cols = 9;
1057 const int avail = view->ncols;
1058 struct tm tm;
1059 time_t committer_time;
1061 committer_time = got_object_commit_get_committer_time(commit);
1062 if (localtime_r(&committer_time, &tm) == NULL)
1063 return got_error_from_errno("localtime_r");
1064 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1065 >= sizeof(datebuf))
1066 return got_error(GOT_ERR_NO_SPACE);
1068 if (avail < date_display_cols)
1069 limit = MIN(sizeof(datebuf) - 1, avail);
1070 else
1071 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1072 waddnstr(view->window, datebuf, limit);
1073 col = limit + 1;
1074 if (col > avail)
1075 goto done;
1077 author = strdup(got_object_commit_get_author(commit));
1078 if (author == NULL) {
1079 err = got_error_from_errno("strdup");
1080 goto done;
1082 err = format_author(&wauthor, &author_width, author, avail - col);
1083 if (err)
1084 goto done;
1085 waddwstr(view->window, wauthor);
1086 col += author_width;
1087 while (col <= avail && author_width < author_display_cols + 2) {
1088 waddch(view->window, ' ');
1089 col++;
1090 author_width++;
1092 if (col > avail)
1093 goto done;
1095 err = got_object_commit_get_logmsg(&logmsg0, commit);
1096 if (err)
1097 goto done;
1098 logmsg = logmsg0;
1099 while (*logmsg == '\n')
1100 logmsg++;
1101 newline = strchr(logmsg, '\n');
1102 if (newline)
1103 *newline = '\0';
1104 limit = avail - col;
1105 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1106 if (err)
1107 goto done;
1108 waddwstr(view->window, wlogmsg);
1109 col += logmsg_width;
1110 while (col <= avail) {
1111 waddch(view->window, ' ');
1112 col++;
1114 done:
1115 free(logmsg0);
1116 free(wlogmsg);
1117 free(author);
1118 free(wauthor);
1119 free(line);
1120 return err;
1123 static struct commit_queue_entry *
1124 alloc_commit_queue_entry(struct got_commit_object *commit,
1125 struct got_object_id *id)
1127 struct commit_queue_entry *entry;
1129 entry = calloc(1, sizeof(*entry));
1130 if (entry == NULL)
1131 return NULL;
1133 entry->id = id;
1134 entry->commit = commit;
1135 return entry;
1138 static void
1139 pop_commit(struct commit_queue *commits)
1141 struct commit_queue_entry *entry;
1143 entry = TAILQ_FIRST(&commits->head);
1144 TAILQ_REMOVE(&commits->head, entry, entry);
1145 got_object_commit_close(entry->commit);
1146 commits->ncommits--;
1147 /* Don't free entry->id! It is owned by the commit graph. */
1148 free(entry);
1151 static void
1152 free_commits(struct commit_queue *commits)
1154 while (!TAILQ_EMPTY(&commits->head))
1155 pop_commit(commits);
1158 static const struct got_error *
1159 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1160 int minqueue, struct got_repository *repo, const char *path)
1162 const struct got_error *err = NULL;
1163 int nqueued = 0;
1166 * We keep all commits open throughout the lifetime of the log
1167 * view in order to avoid having to re-fetch commits from disk
1168 * while updating the display.
1170 while (nqueued < minqueue) {
1171 struct got_object_id *id;
1172 struct got_commit_object *commit;
1173 struct commit_queue_entry *entry;
1174 int errcode;
1176 err = got_commit_graph_iter_next(&id, graph);
1177 if (err) {
1178 if (err->code != GOT_ERR_ITER_NEED_MORE)
1179 break;
1180 err = got_commit_graph_fetch_commits(graph,
1181 minqueue, repo);
1182 if (err)
1183 return err;
1184 continue;
1187 if (id == NULL)
1188 break;
1190 err = got_object_open_as_commit(&commit, repo, id);
1191 if (err)
1192 break;
1193 entry = alloc_commit_queue_entry(commit, id);
1194 if (entry == NULL) {
1195 err = got_error_from_errno("alloc_commit_queue_entry");
1196 break;
1199 errcode = pthread_mutex_lock(&tog_mutex);
1200 if (errcode) {
1201 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1202 break;
1205 entry->idx = commits->ncommits;
1206 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1207 nqueued++;
1208 commits->ncommits++;
1210 errcode = pthread_mutex_unlock(&tog_mutex);
1211 if (errcode && err == NULL)
1212 err = got_error_set_errno(errcode,
1213 "pthread_mutex_unlock");
1216 return err;
1219 static const struct got_error *
1220 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1221 struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 struct got_reference *head_ref;
1226 *head_id = NULL;
1228 err = got_ref_open(&head_ref, repo, branch_name, 0);
1229 if (err)
1230 return err;
1232 err = got_ref_resolve(head_id, repo, head_ref);
1233 got_ref_close(head_ref);
1234 if (err) {
1235 *head_id = NULL;
1236 return err;
1239 return NULL;
1242 static const struct got_error *
1243 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1244 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1245 struct commit_queue *commits, int selected_idx, int limit,
1246 struct got_reflist_head *refs, const char *path, int commits_needed)
1248 const struct got_error *err = NULL;
1249 struct tog_log_view_state *s = &view->state.log;
1250 struct commit_queue_entry *entry;
1251 int width;
1252 int ncommits, author_cols = 10;
1253 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1254 char *refs_str = NULL;
1255 wchar_t *wline;
1257 entry = first;
1258 ncommits = 0;
1259 while (entry) {
1260 if (ncommits == selected_idx) {
1261 *selected = entry;
1262 break;
1264 entry = TAILQ_NEXT(entry, entry);
1265 ncommits++;
1268 if (*selected && !(view->searching && view->search_next_done == 0)) {
1269 err = got_object_id_str(&id_str, (*selected)->id);
1270 if (err)
1271 return err;
1272 if (refs) {
1273 err = build_refs_str(&refs_str, refs, (*selected)->id,
1274 s->repo);
1275 if (err)
1276 goto done;
1280 if (commits_needed == 0)
1281 halfdelay(10); /* disable fast refresh */
1283 if (asprintf(&ncommits_str, " [%d/%d] %s",
1284 entry ? entry->idx + 1 : 0, commits->ncommits,
1285 commits_needed > 0 ?
1286 (view->searching && view->search_next_done == 0
1287 ? "searching..." : "loading... ") :
1288 (refs_str ? refs_str : "")) == -1) {
1289 err = got_error_from_errno("asprintf");
1290 goto done;
1293 if (path && strcmp(path, "/") != 0) {
1294 if (asprintf(&header, "commit %s %s%s",
1295 id_str ? id_str : "........................................",
1296 path, ncommits_str) == -1) {
1297 err = got_error_from_errno("asprintf");
1298 header = NULL;
1299 goto done;
1301 } else if (asprintf(&header, "commit %s%s",
1302 id_str ? id_str : "........................................",
1303 ncommits_str) == -1) {
1304 err = got_error_from_errno("asprintf");
1305 header = NULL;
1306 goto done;
1308 err = format_line(&wline, &width, header, view->ncols);
1309 if (err)
1310 goto done;
1312 werase(view->window);
1314 if (view_needs_focus_indication(view))
1315 wstandout(view->window);
1316 waddwstr(view->window, wline);
1317 while (width < view->ncols) {
1318 waddch(view->window, ' ');
1319 width++;
1321 if (view_needs_focus_indication(view))
1322 wstandend(view->window);
1323 free(wline);
1324 if (limit <= 1)
1325 goto done;
1327 /* Grow author column size if necessary. */
1328 entry = first;
1329 ncommits = 0;
1330 while (entry) {
1331 char *author;
1332 wchar_t *wauthor;
1333 int width;
1334 if (ncommits >= limit - 1)
1335 break;
1336 author = strdup(got_object_commit_get_author(entry->commit));
1337 if (author == NULL) {
1338 err = got_error_from_errno("strdup");
1339 goto done;
1341 err = format_author(&wauthor, &width, author, COLS);
1342 if (author_cols < width)
1343 author_cols = width;
1344 free(wauthor);
1345 free(author);
1346 entry = TAILQ_NEXT(entry, entry);
1349 entry = first;
1350 *last = first;
1351 ncommits = 0;
1352 while (entry) {
1353 if (ncommits >= limit - 1)
1354 break;
1355 if (ncommits == selected_idx)
1356 wstandout(view->window);
1357 err = draw_commit(view, entry->commit, entry->id, refs,
1358 author_cols);
1359 if (ncommits == selected_idx)
1360 wstandend(view->window);
1361 if (err)
1362 goto done;
1363 ncommits++;
1364 *last = entry;
1365 entry = TAILQ_NEXT(entry, entry);
1368 view_vborder(view);
1369 done:
1370 free(id_str);
1371 free(refs_str);
1372 free(ncommits_str);
1373 free(header);
1374 return err;
1377 static void
1378 scroll_up(struct tog_view *view,
1379 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1380 struct commit_queue *commits)
1382 struct commit_queue_entry *entry;
1383 int nscrolled = 0;
1385 entry = TAILQ_FIRST(&commits->head);
1386 if (*first_displayed_entry == entry)
1387 return;
1389 entry = *first_displayed_entry;
1390 while (entry && nscrolled < maxscroll) {
1391 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1392 if (entry) {
1393 *first_displayed_entry = entry;
1394 nscrolled++;
1399 static const struct got_error *
1400 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1401 pthread_cond_t *need_commits)
1403 int errcode;
1404 int max_wait = 20;
1406 halfdelay(1); /* fast refresh while loading commits */
1408 while (*commits_needed > 0) {
1409 if (*log_complete)
1410 break;
1412 /* Wake the log thread. */
1413 errcode = pthread_cond_signal(need_commits);
1414 if (errcode)
1415 return got_error_set_errno(errcode,
1416 "pthread_cond_signal");
1417 errcode = pthread_mutex_unlock(&tog_mutex);
1418 if (errcode)
1419 return got_error_set_errno(errcode,
1420 "pthread_mutex_unlock");
1421 pthread_yield();
1422 errcode = pthread_mutex_lock(&tog_mutex);
1423 if (errcode)
1424 return got_error_set_errno(errcode,
1425 "pthread_mutex_lock");
1427 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1429 * Thread is not done yet; lose a key press
1430 * and let the user retry... this way the GUI
1431 * remains interactive while logging deep paths
1432 * with few commits in history.
1434 return NULL;
1438 return NULL;
1441 static const struct got_error *
1442 scroll_down(struct tog_view *view,
1443 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1444 struct commit_queue_entry **last_displayed_entry,
1445 struct commit_queue *commits, int *log_complete, int *commits_needed,
1446 pthread_cond_t *need_commits)
1448 const struct got_error *err = NULL;
1449 struct commit_queue_entry *pentry;
1450 int nscrolled = 0;
1452 if (*last_displayed_entry == NULL)
1453 return NULL;
1455 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1456 if (pentry == NULL && !*log_complete) {
1458 * Ask the log thread for required amount of commits
1459 * plus some amount of pre-fetching.
1461 (*commits_needed) += maxscroll + 20;
1462 err = trigger_log_thread(0, commits_needed, log_complete,
1463 need_commits);
1464 if (err)
1465 return err;
1468 do {
1469 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1470 if (pentry == NULL)
1471 break;
1473 *last_displayed_entry = pentry;
1475 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1476 if (pentry == NULL)
1477 break;
1478 *first_displayed_entry = pentry;
1479 } while (++nscrolled < maxscroll);
1481 return err;
1484 static const struct got_error *
1485 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1486 struct got_commit_object *commit, struct got_object_id *commit_id,
1487 struct tog_view *log_view, struct got_reflist_head *refs,
1488 struct got_repository *repo)
1490 const struct got_error *err;
1491 struct got_object_qid *parent_id;
1492 struct tog_view *diff_view;
1494 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1495 if (diff_view == NULL)
1496 return got_error_from_errno("view_open");
1498 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1499 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1500 commit_id, log_view, refs, repo);
1501 if (err == NULL)
1502 *new_view = diff_view;
1503 return err;
1506 static const struct got_error *
1507 tree_view_visit_subtree(struct got_tree_object *subtree,
1508 struct tog_tree_view_state *s)
1510 struct tog_parent_tree *parent;
1512 parent = calloc(1, sizeof(*parent));
1513 if (parent == NULL)
1514 return got_error_from_errno("calloc");
1516 parent->tree = s->tree;
1517 parent->first_displayed_entry = s->first_displayed_entry;
1518 parent->selected_entry = s->selected_entry;
1519 parent->selected = s->selected;
1520 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1521 s->tree = subtree;
1522 s->entries = got_object_tree_get_entries(s->tree);
1523 s->selected = 0;
1524 s->first_displayed_entry = NULL;
1525 return NULL;
1529 static const struct got_error *
1530 browse_commit_tree(struct tog_view **new_view, int begin_x,
1531 struct commit_queue_entry *entry, const char *path,
1532 struct got_reflist_head *refs, struct got_repository *repo)
1534 const struct got_error *err = NULL;
1535 struct got_tree_object *tree;
1536 struct got_tree_entry *te;
1537 struct tog_tree_view_state *s;
1538 struct tog_view *tree_view;
1539 char *slash, *subpath = NULL;
1540 const char *p;
1542 err = got_object_open_as_tree(&tree, repo,
1543 got_object_commit_get_tree_id(entry->commit));
1544 if (err)
1545 return err;
1547 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1548 if (tree_view == NULL)
1549 return got_error_from_errno("view_open");
1551 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1552 if (err) {
1553 got_object_tree_close(tree);
1554 return err;
1556 s = &tree_view->state.tree;
1558 *new_view = tree_view;
1560 /* Walk the path and open corresponding tree objects. */
1561 p = path;
1562 while (p[0] == '/')
1563 p++;
1564 while (*p) {
1565 struct got_object_id *tree_id;
1567 /* Ensure the correct subtree entry is selected. */
1568 slash = strchr(p, '/');
1569 if (slash == NULL)
1570 slash = strchr(p, '\0');
1571 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1572 if (strncmp(p, te->name, slash - p) == 0) {
1573 s->selected_entry = te;
1574 break;
1576 s->selected++;
1578 if (s->selected_entry == NULL) {
1579 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1580 break;
1582 if (s->tree != s->root)
1583 s->selected++; /* skip '..' */
1585 if (!S_ISDIR(s->selected_entry->mode)) {
1586 /* Jump to this file's entry. */
1587 s->first_displayed_entry = s->selected_entry;
1588 s->selected = 0;
1589 break;
1592 slash = strchr(p, '/');
1593 if (slash)
1594 subpath = strndup(path, slash - path);
1595 else
1596 subpath = strdup(path);
1597 if (subpath == NULL) {
1598 err = got_error_from_errno("strdup");
1599 break;
1602 err = got_object_id_by_path(&tree_id, repo, entry->id,
1603 subpath);
1604 if (err)
1605 break;
1607 err = got_object_open_as_tree(&tree, repo, tree_id);
1608 free(tree_id);
1609 if (err)
1610 break;
1612 err = tree_view_visit_subtree(tree, s);
1613 if (err) {
1614 got_object_tree_close(tree);
1615 break;
1617 if (slash == NULL)
1618 break;
1619 free(subpath);
1620 subpath = NULL;
1621 p = slash;
1624 free(subpath);
1625 return err;
1628 static void *
1629 log_thread(void *arg)
1631 const struct got_error *err = NULL;
1632 int errcode = 0;
1633 struct tog_log_thread_args *a = arg;
1634 int done = 0;
1636 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1637 if (err)
1638 return (void *)err;
1640 while (!done && !err && !tog_sigpipe_received) {
1641 err = queue_commits(a->graph, a->commits, 1, a->repo,
1642 a->in_repo_path);
1643 if (err) {
1644 if (err->code != GOT_ERR_ITER_COMPLETED)
1645 return (void *)err;
1646 err = NULL;
1647 done = 1;
1648 } else if (a->commits_needed > 0)
1649 a->commits_needed--;
1651 errcode = pthread_mutex_lock(&tog_mutex);
1652 if (errcode) {
1653 err = got_error_set_errno(errcode,
1654 "pthread_mutex_lock");
1655 break;
1656 } else if (*a->quit)
1657 done = 1;
1658 else if (*a->first_displayed_entry == NULL) {
1659 *a->first_displayed_entry =
1660 TAILQ_FIRST(&a->commits->head);
1661 *a->selected_entry = *a->first_displayed_entry;
1664 if (done)
1665 a->commits_needed = 0;
1666 else if (a->commits_needed == 0) {
1667 errcode = pthread_cond_wait(&a->need_commits,
1668 &tog_mutex);
1669 if (errcode)
1670 err = got_error_set_errno(errcode,
1671 "pthread_cond_wait");
1674 errcode = pthread_mutex_unlock(&tog_mutex);
1675 if (errcode && err == NULL)
1676 err = got_error_set_errno(errcode,
1677 "pthread_mutex_unlock");
1679 a->log_complete = 1;
1680 return (void *)err;
1683 static const struct got_error *
1684 stop_log_thread(struct tog_log_view_state *s)
1686 const struct got_error *err = NULL;
1687 int errcode;
1689 if (s->thread) {
1690 s->quit = 1;
1691 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1692 if (errcode)
1693 return got_error_set_errno(errcode,
1694 "pthread_cond_signal");
1695 errcode = pthread_mutex_unlock(&tog_mutex);
1696 if (errcode)
1697 return got_error_set_errno(errcode,
1698 "pthread_mutex_unlock");
1699 errcode = pthread_join(s->thread, (void **)&err);
1700 if (errcode)
1701 return got_error_set_errno(errcode, "pthread_join");
1702 errcode = pthread_mutex_lock(&tog_mutex);
1703 if (errcode)
1704 return got_error_set_errno(errcode,
1705 "pthread_mutex_lock");
1706 s->thread = NULL;
1709 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1710 if (errcode && err == NULL)
1711 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1713 if (s->thread_args.repo) {
1714 got_repo_close(s->thread_args.repo);
1715 s->thread_args.repo = NULL;
1718 if (s->thread_args.graph) {
1719 got_commit_graph_close(s->thread_args.graph);
1720 s->thread_args.graph = NULL;
1723 return err;
1726 static const struct got_error *
1727 close_log_view(struct tog_view *view)
1729 const struct got_error *err = NULL;
1730 struct tog_log_view_state *s = &view->state.log;
1732 err = stop_log_thread(s);
1733 free_commits(&s->commits);
1734 free(s->in_repo_path);
1735 s->in_repo_path = NULL;
1736 free(s->start_id);
1737 s->start_id = NULL;
1738 return err;
1741 static const struct got_error *
1742 search_start_log_view(struct tog_view *view)
1744 struct tog_log_view_state *s = &view->state.log;
1746 s->matched_entry = NULL;
1747 s->search_entry = NULL;
1748 return NULL;
1751 static int
1752 match_commit(struct got_commit_object *commit, const char *id_str,
1753 const char *logmsg, regex_t *regex)
1755 regmatch_t regmatch;
1757 if (regexec(regex, got_object_commit_get_author(commit), 1,
1758 &regmatch, 0) == 0 ||
1759 regexec(regex, got_object_commit_get_committer(commit), 1,
1760 &regmatch, 0) == 0 ||
1761 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1762 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1763 return 1;
1765 return 0;
1768 static const struct got_error *
1769 search_next_log_view(struct tog_view *view)
1771 const struct got_error *err = NULL;
1772 struct tog_log_view_state *s = &view->state.log;
1773 struct commit_queue_entry *entry;
1775 if (!view->searching) {
1776 view->search_next_done = 1;
1777 return NULL;
1780 if (s->search_entry) {
1781 if (wgetch(view->window) == KEY_BACKSPACE) {
1782 view->search_next_done = 1;
1783 return NULL;
1785 if (view->searching == TOG_SEARCH_FORWARD)
1786 entry = TAILQ_NEXT(s->search_entry, entry);
1787 else
1788 entry = TAILQ_PREV(s->search_entry,
1789 commit_queue_head, entry);
1790 } else if (s->matched_entry) {
1791 if (view->searching == TOG_SEARCH_FORWARD)
1792 entry = TAILQ_NEXT(s->selected_entry, entry);
1793 else
1794 entry = TAILQ_PREV(s->selected_entry,
1795 commit_queue_head, entry);
1796 } else {
1797 if (view->searching == TOG_SEARCH_FORWARD)
1798 entry = TAILQ_FIRST(&s->commits.head);
1799 else
1800 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1803 while (1) {
1804 char *id_str, *logmsg;
1805 if (entry == NULL) {
1806 if (s->thread_args.log_complete ||
1807 view->searching == TOG_SEARCH_BACKWARD) {
1808 view->search_next_done = 1;
1809 return NULL;
1812 * Poke the log thread for more commits and return,
1813 * allowing the main loop to make progress. Search
1814 * will resume at s->search_entry once we come back.
1816 s->thread_args.commits_needed++;
1817 return trigger_log_thread(1,
1818 &s->thread_args.commits_needed,
1819 &s->thread_args.log_complete,
1820 &s->thread_args.need_commits);
1823 err = got_object_id_str(&id_str, entry->id);
1824 if (err)
1825 return err;
1827 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1828 if (err)
1829 return err;
1830 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1831 free(logmsg);
1832 view->search_next_done = 1;
1833 s->matched_entry = entry;
1834 free(id_str);
1835 break;
1837 free(logmsg);
1838 free(id_str);
1839 s->search_entry = entry;
1840 if (view->searching == TOG_SEARCH_FORWARD)
1841 entry = TAILQ_NEXT(entry, entry);
1842 else
1843 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1846 if (s->matched_entry) {
1847 int cur = s->selected_entry->idx;
1848 while (cur < s->matched_entry->idx) {
1849 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1850 if (err)
1851 return err;
1852 cur++;
1854 while (cur > s->matched_entry->idx) {
1855 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1856 if (err)
1857 return err;
1858 cur--;
1862 s->search_entry = NULL;
1864 return NULL;
1867 static const struct got_error *
1868 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1869 struct got_reflist_head *refs, struct got_repository *repo,
1870 const char *head_ref_name, const char *path, int check_disk)
1872 const struct got_error *err = NULL;
1873 struct tog_log_view_state *s = &view->state.log;
1874 struct got_repository *thread_repo = NULL;
1875 struct got_commit_graph *thread_graph = NULL;
1876 int errcode;
1878 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1879 if (err != NULL)
1880 goto done;
1882 /* The commit queue only contains commits being displayed. */
1883 TAILQ_INIT(&s->commits.head);
1884 s->commits.ncommits = 0;
1886 s->refs = refs;
1887 s->repo = repo;
1888 s->head_ref_name = head_ref_name;
1889 s->start_id = got_object_id_dup(start_id);
1890 if (s->start_id == NULL) {
1891 err = got_error_from_errno("got_object_id_dup");
1892 goto done;
1895 view->show = show_log_view;
1896 view->input = input_log_view;
1897 view->close = close_log_view;
1898 view->search_start = search_start_log_view;
1899 view->search_next = search_next_log_view;
1901 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1902 if (err)
1903 goto done;
1904 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1905 0, thread_repo);
1906 if (err)
1907 goto done;
1909 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1910 if (errcode) {
1911 err = got_error_set_errno(errcode, "pthread_cond_init");
1912 goto done;
1915 s->thread_args.commits_needed = view->nlines;
1916 s->thread_args.graph = thread_graph;
1917 s->thread_args.commits = &s->commits;
1918 s->thread_args.in_repo_path = s->in_repo_path;
1919 s->thread_args.start_id = s->start_id;
1920 s->thread_args.repo = thread_repo;
1921 s->thread_args.log_complete = 0;
1922 s->thread_args.quit = &s->quit;
1923 s->thread_args.view = view;
1924 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1925 s->thread_args.selected_entry = &s->selected_entry;
1926 done:
1927 if (err)
1928 close_log_view(view);
1929 return err;
1932 static const struct got_error *
1933 show_log_view(struct tog_view *view)
1935 struct tog_log_view_state *s = &view->state.log;
1937 if (s->thread == NULL) {
1938 int errcode = pthread_create(&s->thread, NULL, log_thread,
1939 &s->thread_args);
1940 if (errcode)
1941 return got_error_set_errno(errcode, "pthread_create");
1944 return draw_commits(view, &s->last_displayed_entry,
1945 &s->selected_entry, s->first_displayed_entry,
1946 &s->commits, s->selected, view->nlines, s->refs,
1947 s->in_repo_path, s->thread_args.commits_needed);
1950 static const struct got_error *
1951 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1952 struct tog_view **focus_view, struct tog_view *view, int ch)
1954 const struct got_error *err = NULL;
1955 struct tog_log_view_state *s = &view->state.log;
1956 char *parent_path, *in_repo_path = NULL;
1957 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1958 int begin_x = 0;
1959 struct got_object_id *start_id;
1961 switch (ch) {
1962 case 'q':
1963 s->quit = 1;
1964 break;
1965 case 'k':
1966 case KEY_UP:
1967 case '<':
1968 case ',':
1969 if (s->first_displayed_entry == NULL)
1970 break;
1971 if (s->selected > 0)
1972 s->selected--;
1973 else
1974 scroll_up(view, &s->first_displayed_entry, 1,
1975 &s->commits);
1976 break;
1977 case KEY_PPAGE:
1978 case CTRL('b'):
1979 if (s->first_displayed_entry == NULL)
1980 break;
1981 if (TAILQ_FIRST(&s->commits.head) ==
1982 s->first_displayed_entry) {
1983 s->selected = 0;
1984 break;
1986 scroll_up(view, &s->first_displayed_entry,
1987 view->nlines, &s->commits);
1988 break;
1989 case 'j':
1990 case KEY_DOWN:
1991 case '>':
1992 case '.':
1993 if (s->first_displayed_entry == NULL)
1994 break;
1995 if (s->selected < MIN(view->nlines - 2,
1996 s->commits.ncommits - 1)) {
1997 s->selected++;
1998 break;
2000 err = scroll_down(view, &s->first_displayed_entry, 1,
2001 &s->last_displayed_entry, &s->commits,
2002 &s->thread_args.log_complete,
2003 &s->thread_args.commits_needed,
2004 &s->thread_args.need_commits);
2005 break;
2006 case KEY_NPAGE:
2007 case CTRL('f'): {
2008 struct commit_queue_entry *first;
2009 first = s->first_displayed_entry;
2010 if (first == NULL)
2011 break;
2012 err = scroll_down(view, &s->first_displayed_entry,
2013 view->nlines, &s->last_displayed_entry,
2014 &s->commits, &s->thread_args.log_complete,
2015 &s->thread_args.commits_needed,
2016 &s->thread_args.need_commits);
2017 if (first == s->first_displayed_entry &&
2018 s->selected < MIN(view->nlines - 2,
2019 s->commits.ncommits - 1)) {
2020 /* can't scroll further down */
2021 s->selected = MIN(view->nlines - 2,
2022 s->commits.ncommits - 1);
2024 err = NULL;
2025 break;
2027 case KEY_RESIZE:
2028 if (s->selected > view->nlines - 2)
2029 s->selected = view->nlines - 2;
2030 if (s->selected > s->commits.ncommits - 1)
2031 s->selected = s->commits.ncommits - 1;
2032 break;
2033 case KEY_ENTER:
2034 case ' ':
2035 case '\r':
2036 if (s->selected_entry == NULL)
2037 break;
2038 if (view_is_parent_view(view))
2039 begin_x = view_split_begin_x(view->begin_x);
2040 err = open_diff_view_for_commit(&diff_view, begin_x,
2041 s->selected_entry->commit, s->selected_entry->id,
2042 view, s->refs, s->repo);
2043 if (err)
2044 break;
2045 if (view_is_parent_view(view)) {
2046 err = view_close_child(view);
2047 if (err)
2048 return err;
2049 err = view_set_child(view, diff_view);
2050 if (err) {
2051 view_close(diff_view);
2052 break;
2054 *focus_view = diff_view;
2055 view->child_focussed = 1;
2056 } else
2057 *new_view = diff_view;
2058 break;
2059 case 't':
2060 if (s->selected_entry == NULL)
2061 break;
2062 if (view_is_parent_view(view))
2063 begin_x = view_split_begin_x(view->begin_x);
2064 err = browse_commit_tree(&tree_view, begin_x,
2065 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2066 if (err)
2067 break;
2068 if (view_is_parent_view(view)) {
2069 err = view_close_child(view);
2070 if (err)
2071 return err;
2072 err = view_set_child(view, tree_view);
2073 if (err) {
2074 view_close(tree_view);
2075 break;
2077 *focus_view = tree_view;
2078 view->child_focussed = 1;
2079 } else
2080 *new_view = tree_view;
2081 break;
2082 case KEY_BACKSPACE:
2083 if (strcmp(s->in_repo_path, "/") == 0)
2084 break;
2085 parent_path = dirname(s->in_repo_path);
2086 if (parent_path && strcmp(parent_path, ".") != 0) {
2087 err = stop_log_thread(s);
2088 if (err)
2089 return err;
2090 lv = view_open(view->nlines, view->ncols,
2091 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2092 if (lv == NULL)
2093 return got_error_from_errno(
2094 "view_open");
2095 err = open_log_view(lv, s->start_id, s->refs,
2096 s->repo, s->head_ref_name, parent_path, 0);
2097 if (err)
2098 return err;;
2099 if (view_is_parent_view(view))
2100 *new_view = lv;
2101 else {
2102 view_set_child(view->parent, lv);
2103 *focus_view = lv;
2105 return NULL;
2107 break;
2108 case CTRL('l'):
2109 err = stop_log_thread(s);
2110 if (err)
2111 return err;
2112 lv = view_open(view->nlines, view->ncols,
2113 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2114 if (lv == NULL)
2115 return got_error_from_errno("view_open");
2116 err = get_head_commit_id(&start_id, s->head_ref_name ?
2117 s->head_ref_name : GOT_REF_HEAD, s->repo);
2118 if (err) {
2119 view_close(lv);
2120 return err;
2122 in_repo_path = strdup(s->in_repo_path);
2123 if (in_repo_path == NULL) {
2124 free(start_id);
2125 view_close(lv);
2126 return got_error_from_errno("strdup");
2128 err = open_log_view(lv, start_id, s->refs, s->repo,
2129 s->head_ref_name, in_repo_path, 0);
2130 if (err) {
2131 free(start_id);
2132 view_close(lv);
2133 return err;;
2135 *dead_view = view;
2136 *new_view = lv;
2137 break;
2138 default:
2139 break;
2142 return err;
2145 static const struct got_error *
2146 apply_unveil(const char *repo_path, const char *worktree_path)
2148 const struct got_error *error;
2150 #ifdef PROFILE
2151 if (unveil("gmon.out", "rwc") != 0)
2152 return got_error_from_errno2("unveil", "gmon.out");
2153 #endif
2154 if (repo_path && unveil(repo_path, "r") != 0)
2155 return got_error_from_errno2("unveil", repo_path);
2157 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2158 return got_error_from_errno2("unveil", worktree_path);
2160 if (unveil("/tmp", "rwc") != 0)
2161 return got_error_from_errno2("unveil", "/tmp");
2163 error = got_privsep_unveil_exec_helpers();
2164 if (error != NULL)
2165 return error;
2167 if (unveil(NULL, NULL) != 0)
2168 return got_error_from_errno("unveil");
2170 return NULL;
2173 static void
2174 init_curses(void)
2176 initscr();
2177 cbreak();
2178 halfdelay(1); /* Do fast refresh while initial view is loading. */
2179 noecho();
2180 nonl();
2181 intrflush(stdscr, FALSE);
2182 keypad(stdscr, TRUE);
2183 curs_set(0);
2184 signal(SIGWINCH, tog_sigwinch);
2185 signal(SIGPIPE, tog_sigpipe);
2188 static const struct got_error *
2189 cmd_log(int argc, char *argv[])
2191 const struct got_error *error;
2192 struct got_repository *repo = NULL;
2193 struct got_worktree *worktree = NULL;
2194 struct got_reflist_head refs;
2195 struct got_object_id *start_id = NULL;
2196 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2197 char *start_commit = NULL;
2198 int ch;
2199 struct tog_view *view;
2201 SIMPLEQ_INIT(&refs);
2203 #ifndef PROFILE
2204 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2205 NULL) == -1)
2206 err(1, "pledge");
2207 #endif
2209 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2210 switch (ch) {
2211 case 'c':
2212 start_commit = optarg;
2213 break;
2214 case 'r':
2215 repo_path = realpath(optarg, NULL);
2216 if (repo_path == NULL)
2217 err(1, "-r option");
2218 break;
2219 default:
2220 usage_log();
2221 /* NOTREACHED */
2225 argc -= optind;
2226 argv += optind;
2228 cwd = getcwd(NULL, 0);
2229 if (cwd == NULL) {
2230 error = got_error_from_errno("getcwd");
2231 goto done;
2233 error = got_worktree_open(&worktree, cwd);
2234 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2235 goto done;
2236 error = NULL;
2238 if (argc == 0) {
2239 path = strdup("");
2240 if (path == NULL) {
2241 error = got_error_from_errno("strdup");
2242 goto done;
2244 } else if (argc == 1) {
2245 if (worktree) {
2246 error = got_worktree_resolve_path(&path, worktree,
2247 argv[0]);
2248 if (error)
2249 goto done;
2250 } else {
2251 path = strdup(argv[0]);
2252 if (path == NULL) {
2253 error = got_error_from_errno("strdup");
2254 goto done;
2257 } else
2258 usage_log();
2260 if (repo_path == NULL) {
2261 if (worktree)
2262 repo_path = strdup(
2263 got_worktree_get_repo_path(worktree));
2264 else
2265 repo_path = strdup(cwd);
2267 if (repo_path == NULL) {
2268 error = got_error_from_errno("strdup");
2269 goto done;
2272 init_curses();
2274 error = got_repo_open(&repo, repo_path);
2275 if (error != NULL)
2276 goto done;
2278 error = apply_unveil(got_repo_get_path(repo),
2279 worktree ? got_worktree_get_root_path(worktree) : NULL);
2280 if (error)
2281 goto done;
2283 if (start_commit == NULL)
2284 error = get_head_commit_id(&start_id, worktree ?
2285 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2286 repo);
2287 else {
2288 error = get_head_commit_id(&start_id, start_commit, repo);
2289 if (error) {
2290 if (error->code != GOT_ERR_NOT_REF)
2291 goto done;
2292 error = got_repo_match_object_id_prefix(&start_id,
2293 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2296 if (error != NULL)
2297 goto done;
2299 error = got_ref_list(&refs, repo);
2300 if (error)
2301 goto done;
2303 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2304 if (view == NULL) {
2305 error = got_error_from_errno("view_open");
2306 goto done;
2308 error = open_log_view(view, start_id, &refs, repo, worktree ?
2309 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2310 if (error)
2311 goto done;
2312 error = view_loop(view);
2313 done:
2314 free(repo_path);
2315 free(cwd);
2316 free(path);
2317 free(start_id);
2318 if (repo)
2319 got_repo_close(repo);
2320 if (worktree)
2321 got_worktree_close(worktree);
2322 got_ref_list_free(&refs);
2323 return error;
2326 __dead static void
2327 usage_diff(void)
2329 endwin();
2330 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2331 getprogname());
2332 exit(1);
2335 static char *
2336 parse_next_line(FILE *f, size_t *len)
2338 char *line;
2339 size_t linelen;
2340 size_t lineno;
2341 const char delim[3] = { '\0', '\0', '\0'};
2343 line = fparseln(f, &linelen, &lineno, delim, 0);
2344 if (len)
2345 *len = linelen;
2346 return line;
2349 static const struct got_error *
2350 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2351 int *last_displayed_line, int *eof, int max_lines,
2352 char *header)
2354 const struct got_error *err;
2355 int nlines = 0, nprinted = 0;
2356 char *line;
2357 size_t len;
2358 wchar_t *wline;
2359 int width;
2361 rewind(f);
2362 werase(view->window);
2364 if (header) {
2365 err = format_line(&wline, &width, header, view->ncols);
2366 if (err) {
2367 return err;
2370 if (view_needs_focus_indication(view))
2371 wstandout(view->window);
2372 waddwstr(view->window, wline);
2373 if (view_needs_focus_indication(view))
2374 wstandend(view->window);
2375 if (width < view->ncols - 1)
2376 waddch(view->window, '\n');
2378 if (max_lines <= 1)
2379 return NULL;
2380 max_lines--;
2383 *eof = 0;
2384 while (nprinted < max_lines) {
2385 line = parse_next_line(f, &len);
2386 if (line == NULL) {
2387 *eof = 1;
2388 break;
2390 if (++nlines < *first_displayed_line) {
2391 free(line);
2392 continue;
2395 err = format_line(&wline, &width, line, view->ncols);
2396 if (err) {
2397 free(line);
2398 return err;
2400 waddwstr(view->window, wline);
2401 if (width < view->ncols - 1)
2402 waddch(view->window, '\n');
2403 if (++nprinted == 1)
2404 *first_displayed_line = nlines;
2405 free(line);
2406 free(wline);
2407 wline = NULL;
2409 *last_displayed_line = nlines;
2411 view_vborder(view);
2413 if (*eof) {
2414 while (nprinted < view->nlines) {
2415 waddch(view->window, '\n');
2416 nprinted++;
2419 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2420 if (err) {
2421 return err;
2424 wstandout(view->window);
2425 waddwstr(view->window, wline);
2426 wstandend(view->window);
2429 return NULL;
2432 static char *
2433 get_datestr(time_t *time, char *datebuf)
2435 struct tm mytm, *tm;
2436 char *p, *s;
2438 tm = gmtime_r(time, &mytm);
2439 if (tm == NULL)
2440 return NULL;
2441 s = asctime_r(tm, datebuf);
2442 if (s == NULL)
2443 return NULL;
2444 p = strchr(s, '\n');
2445 if (p)
2446 *p = '\0';
2447 return s;
2450 static const struct got_error *
2451 write_commit_info(struct got_object_id *commit_id,
2452 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2454 const struct got_error *err = NULL;
2455 char datebuf[26], *datestr;
2456 struct got_commit_object *commit;
2457 char *id_str = NULL, *logmsg = NULL;
2458 time_t committer_time;
2459 const char *author, *committer;
2460 char *refs_str = NULL;
2462 if (refs) {
2463 err = build_refs_str(&refs_str, refs, commit_id, repo);
2464 if (err)
2465 return err;
2468 err = got_object_open_as_commit(&commit, repo, commit_id);
2469 if (err)
2470 return err;
2472 err = got_object_id_str(&id_str, commit_id);
2473 if (err) {
2474 err = got_error_from_errno("got_object_id_str");
2475 goto done;
2478 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2479 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2480 err = got_error_from_errno("fprintf");
2481 goto done;
2483 if (fprintf(outfile, "from: %s\n",
2484 got_object_commit_get_author(commit)) < 0) {
2485 err = got_error_from_errno("fprintf");
2486 goto done;
2488 committer_time = got_object_commit_get_committer_time(commit);
2489 datestr = get_datestr(&committer_time, datebuf);
2490 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2491 err = got_error_from_errno("fprintf");
2492 goto done;
2494 author = got_object_commit_get_author(commit);
2495 committer = got_object_commit_get_committer(commit);
2496 if (strcmp(author, committer) != 0 &&
2497 fprintf(outfile, "via: %s\n", committer) < 0) {
2498 err = got_error_from_errno("fprintf");
2499 goto done;
2501 err = got_object_commit_get_logmsg(&logmsg, commit);
2502 if (err)
2503 goto done;
2504 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2505 err = got_error_from_errno("fprintf");
2506 goto done;
2508 done:
2509 free(id_str);
2510 free(logmsg);
2511 free(refs_str);
2512 got_object_commit_close(commit);
2513 return err;
2516 static const struct got_error *
2517 create_diff(struct tog_diff_view_state *s)
2519 const struct got_error *err = NULL;
2520 FILE *f = NULL;
2521 int obj_type;
2523 f = got_opentemp();
2524 if (f == NULL) {
2525 err = got_error_from_errno("got_opentemp");
2526 goto done;
2528 if (s->f && fclose(s->f) != 0) {
2529 err = got_error_from_errno("fclose");
2530 goto done;
2532 s->f = f;
2534 if (s->id1)
2535 err = got_object_get_type(&obj_type, s->repo, s->id1);
2536 else
2537 err = got_object_get_type(&obj_type, s->repo, s->id2);
2538 if (err)
2539 goto done;
2541 switch (obj_type) {
2542 case GOT_OBJ_TYPE_BLOB:
2543 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2544 s->diff_context, s->repo, f);
2545 break;
2546 case GOT_OBJ_TYPE_TREE:
2547 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2548 s->diff_context, s->repo, f);
2549 break;
2550 case GOT_OBJ_TYPE_COMMIT: {
2551 const struct got_object_id_queue *parent_ids;
2552 struct got_object_qid *pid;
2553 struct got_commit_object *commit2;
2555 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2556 if (err)
2557 break;
2558 /* Show commit info if we're diffing to a parent/root commit. */
2559 if (s->id1 == NULL)
2560 write_commit_info(s->id2, s->refs, s->repo, f);
2561 else {
2562 parent_ids = got_object_commit_get_parent_ids(commit2);
2563 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2564 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2565 write_commit_info(s->id2, s->refs,
2566 s->repo, f);
2567 break;
2571 got_object_commit_close(commit2);
2573 err = got_diff_objects_as_commits(s->id1, s->id2,
2574 s->diff_context, s->repo, f);
2575 break;
2577 default:
2578 err = got_error(GOT_ERR_OBJ_TYPE);
2579 break;
2581 done:
2582 if (f && fflush(f) != 0 && err == NULL)
2583 err = got_error_from_errno("fflush");
2584 return err;
2587 static void
2588 diff_view_indicate_progress(struct tog_view *view)
2590 mvwaddstr(view->window, 0, 0, "diffing...");
2591 update_panels();
2592 doupdate();
2595 static const struct got_error *
2596 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2597 struct got_object_id *id2, struct tog_view *log_view,
2598 struct got_reflist_head *refs, struct got_repository *repo)
2600 const struct got_error *err;
2602 if (id1 != NULL && id2 != NULL) {
2603 int type1, type2;
2604 err = got_object_get_type(&type1, repo, id1);
2605 if (err)
2606 return err;
2607 err = got_object_get_type(&type2, repo, id2);
2608 if (err)
2609 return err;
2611 if (type1 != type2)
2612 return got_error(GOT_ERR_OBJ_TYPE);
2615 if (id1) {
2616 view->state.diff.id1 = got_object_id_dup(id1);
2617 if (view->state.diff.id1 == NULL)
2618 return got_error_from_errno("got_object_id_dup");
2619 } else
2620 view->state.diff.id1 = NULL;
2622 view->state.diff.id2 = got_object_id_dup(id2);
2623 if (view->state.diff.id2 == NULL) {
2624 free(view->state.diff.id1);
2625 view->state.diff.id1 = NULL;
2626 return got_error_from_errno("got_object_id_dup");
2628 view->state.diff.f = NULL;
2629 view->state.diff.first_displayed_line = 1;
2630 view->state.diff.last_displayed_line = view->nlines;
2631 view->state.diff.diff_context = 3;
2632 view->state.diff.log_view = log_view;
2633 view->state.diff.repo = repo;
2634 view->state.diff.refs = refs;
2636 if (log_view && view_is_splitscreen(view))
2637 show_log_view(log_view); /* draw vborder */
2638 diff_view_indicate_progress(view);
2640 err = create_diff(&view->state.diff);
2641 if (err) {
2642 free(view->state.diff.id1);
2643 view->state.diff.id1 = NULL;
2644 free(view->state.diff.id2);
2645 view->state.diff.id2 = NULL;
2646 return err;
2649 view->show = show_diff_view;
2650 view->input = input_diff_view;
2651 view->close = close_diff_view;
2653 return NULL;
2656 static const struct got_error *
2657 close_diff_view(struct tog_view *view)
2659 const struct got_error *err = NULL;
2661 free(view->state.diff.id1);
2662 view->state.diff.id1 = NULL;
2663 free(view->state.diff.id2);
2664 view->state.diff.id2 = NULL;
2665 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2666 err = got_error_from_errno("fclose");
2667 return err;
2670 static const struct got_error *
2671 show_diff_view(struct tog_view *view)
2673 const struct got_error *err;
2674 struct tog_diff_view_state *s = &view->state.diff;
2675 char *id_str1 = NULL, *id_str2, *header;
2677 if (s->id1) {
2678 err = got_object_id_str(&id_str1, s->id1);
2679 if (err)
2680 return err;
2682 err = got_object_id_str(&id_str2, s->id2);
2683 if (err)
2684 return err;
2686 if (asprintf(&header, "diff %s %s",
2687 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2688 err = got_error_from_errno("asprintf");
2689 free(id_str1);
2690 free(id_str2);
2691 return err;
2693 free(id_str1);
2694 free(id_str2);
2696 return draw_file(view, s->f, &s->first_displayed_line,
2697 &s->last_displayed_line, &s->eof, view->nlines,
2698 header);
2701 static const struct got_error *
2702 set_selected_commit(struct tog_diff_view_state *s,
2703 struct commit_queue_entry *entry)
2705 const struct got_error *err;
2706 const struct got_object_id_queue *parent_ids;
2707 struct got_commit_object *selected_commit;
2708 struct got_object_qid *pid;
2710 free(s->id2);
2711 s->id2 = got_object_id_dup(entry->id);
2712 if (s->id2 == NULL)
2713 return got_error_from_errno("got_object_id_dup");
2715 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2716 if (err)
2717 return err;
2718 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2719 free(s->id1);
2720 pid = SIMPLEQ_FIRST(parent_ids);
2721 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2722 got_object_commit_close(selected_commit);
2723 return NULL;
2726 static const struct got_error *
2727 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2728 struct tog_view **focus_view, struct tog_view *view, int ch)
2730 const struct got_error *err = NULL;
2731 struct tog_diff_view_state *s = &view->state.diff;
2732 struct tog_log_view_state *ls;
2733 struct commit_queue_entry *entry;
2734 int i;
2736 switch (ch) {
2737 case 'k':
2738 case KEY_UP:
2739 if (s->first_displayed_line > 1)
2740 s->first_displayed_line--;
2741 break;
2742 case KEY_PPAGE:
2743 case CTRL('b'):
2744 if (s->first_displayed_line == 1)
2745 break;
2746 i = 0;
2747 while (i++ < view->nlines - 1 &&
2748 s->first_displayed_line > 1)
2749 s->first_displayed_line--;
2750 break;
2751 case 'j':
2752 case KEY_DOWN:
2753 if (!s->eof)
2754 s->first_displayed_line++;
2755 break;
2756 case KEY_NPAGE:
2757 case CTRL('f'):
2758 case ' ':
2759 if (s->eof)
2760 break;
2761 i = 0;
2762 while (!s->eof && i++ < view->nlines - 1) {
2763 char *line;
2764 line = parse_next_line(s->f, NULL);
2765 s->first_displayed_line++;
2766 if (line == NULL)
2767 break;
2769 break;
2770 case '[':
2771 if (s->diff_context > 0) {
2772 s->diff_context--;
2773 diff_view_indicate_progress(view);
2774 err = create_diff(s);
2776 break;
2777 case ']':
2778 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2779 s->diff_context++;
2780 diff_view_indicate_progress(view);
2781 err = create_diff(s);
2783 break;
2784 case '<':
2785 case ',':
2786 if (s->log_view == NULL)
2787 break;
2788 ls = &s->log_view->state.log;
2789 entry = TAILQ_PREV(ls->selected_entry,
2790 commit_queue_head, entry);
2791 if (entry == NULL)
2792 break;
2794 err = input_log_view(NULL, NULL, NULL, s->log_view,
2795 KEY_UP);
2796 if (err)
2797 break;
2799 err = set_selected_commit(s, entry);
2800 if (err)
2801 break;
2803 s->first_displayed_line = 1;
2804 s->last_displayed_line = view->nlines;
2806 diff_view_indicate_progress(view);
2807 err = create_diff(s);
2808 break;
2809 case '>':
2810 case '.':
2811 if (s->log_view == NULL)
2812 break;
2813 ls = &s->log_view->state.log;
2815 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2816 ls->thread_args.commits_needed++;
2818 /* Display "loading..." in log view. */
2819 show_log_view(s->log_view);
2820 update_panels();
2821 doupdate();
2823 err = trigger_log_thread(1 /* load_all */,
2824 &ls->thread_args.commits_needed,
2825 &ls->thread_args.log_complete,
2826 &ls->thread_args.need_commits);
2827 if (err)
2828 break;
2830 err = input_log_view(NULL, NULL, NULL, s->log_view,
2831 KEY_DOWN);
2832 if (err)
2833 break;
2835 entry = TAILQ_NEXT(ls->selected_entry, entry);
2836 if (entry == NULL)
2837 break;
2839 err = set_selected_commit(s, entry);
2840 if (err)
2841 break;
2843 s->first_displayed_line = 1;
2844 s->last_displayed_line = view->nlines;
2846 diff_view_indicate_progress(view);
2847 err = create_diff(s);
2848 break;
2849 default:
2850 break;
2853 return err;
2856 static const struct got_error *
2857 cmd_diff(int argc, char *argv[])
2859 const struct got_error *error = NULL;
2860 struct got_repository *repo = NULL;
2861 struct got_reflist_head refs;
2862 struct got_object_id *id1 = NULL, *id2 = NULL;
2863 char *repo_path = NULL;
2864 char *id_str1 = NULL, *id_str2 = NULL;
2865 int ch;
2866 struct tog_view *view;
2868 SIMPLEQ_INIT(&refs);
2870 #ifndef PROFILE
2871 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2872 NULL) == -1)
2873 err(1, "pledge");
2874 #endif
2876 while ((ch = getopt(argc, argv, "")) != -1) {
2877 switch (ch) {
2878 default:
2879 usage_diff();
2880 /* NOTREACHED */
2884 argc -= optind;
2885 argv += optind;
2887 if (argc == 0) {
2888 usage_diff(); /* TODO show local worktree changes */
2889 } else if (argc == 2) {
2890 repo_path = getcwd(NULL, 0);
2891 if (repo_path == NULL)
2892 return got_error_from_errno("getcwd");
2893 id_str1 = argv[0];
2894 id_str2 = argv[1];
2895 } else if (argc == 3) {
2896 repo_path = realpath(argv[0], NULL);
2897 if (repo_path == NULL)
2898 return got_error_from_errno2("realpath", argv[0]);
2899 id_str1 = argv[1];
2900 id_str2 = argv[2];
2901 } else
2902 usage_diff();
2904 init_curses();
2906 error = got_repo_open(&repo, repo_path);
2907 if (error)
2908 goto done;
2910 error = apply_unveil(got_repo_get_path(repo), NULL);
2911 if (error)
2912 goto done;
2914 error = got_repo_match_object_id_prefix(&id1, id_str1,
2915 GOT_OBJ_TYPE_ANY, repo);
2916 if (error)
2917 goto done;
2919 error = got_repo_match_object_id_prefix(&id2, id_str2,
2920 GOT_OBJ_TYPE_ANY, repo);
2921 if (error)
2922 goto done;
2924 error = got_ref_list(&refs, repo);
2925 if (error)
2926 goto done;
2928 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2929 if (view == NULL) {
2930 error = got_error_from_errno("view_open");
2931 goto done;
2933 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2934 if (error)
2935 goto done;
2936 error = view_loop(view);
2937 done:
2938 free(repo_path);
2939 if (repo)
2940 got_repo_close(repo);
2941 got_ref_list_free(&refs);
2942 return error;
2945 __dead static void
2946 usage_blame(void)
2948 endwin();
2949 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2950 getprogname());
2951 exit(1);
2954 struct tog_blame_line {
2955 int annotated;
2956 struct got_object_id *id;
2959 static const struct got_error *
2960 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2961 const char *path, struct tog_blame_line *lines, int nlines,
2962 int blame_complete, int selected_line, int *first_displayed_line,
2963 int *last_displayed_line, int *eof, int max_lines)
2965 const struct got_error *err;
2966 int lineno = 0, nprinted = 0;
2967 char *line;
2968 size_t len;
2969 wchar_t *wline;
2970 int width, wlimit;
2971 struct tog_blame_line *blame_line;
2972 struct got_object_id *prev_id = NULL;
2973 char *id_str;
2975 err = got_object_id_str(&id_str, id);
2976 if (err)
2977 return err;
2979 rewind(f);
2980 werase(view->window);
2982 if (asprintf(&line, "commit %s", id_str) == -1) {
2983 err = got_error_from_errno("asprintf");
2984 free(id_str);
2985 return err;
2988 err = format_line(&wline, &width, line, view->ncols);
2989 free(line);
2990 line = NULL;
2991 if (view_needs_focus_indication(view))
2992 wstandout(view->window);
2993 waddwstr(view->window, wline);
2994 if (view_needs_focus_indication(view))
2995 wstandend(view->window);
2996 free(wline);
2997 wline = NULL;
2998 if (width < view->ncols - 1)
2999 waddch(view->window, '\n');
3001 if (asprintf(&line, "[%d/%d] %s%s",
3002 *first_displayed_line - 1 + selected_line, nlines,
3003 blame_complete ? "" : "annotating... ", path) == -1) {
3004 free(id_str);
3005 return got_error_from_errno("asprintf");
3007 free(id_str);
3008 err = format_line(&wline, &width, line, view->ncols);
3009 free(line);
3010 line = NULL;
3011 if (err)
3012 return err;
3013 waddwstr(view->window, wline);
3014 free(wline);
3015 wline = NULL;
3016 if (width < view->ncols - 1)
3017 waddch(view->window, '\n');
3019 *eof = 0;
3020 while (nprinted < max_lines - 2) {
3021 line = parse_next_line(f, &len);
3022 if (line == NULL) {
3023 *eof = 1;
3024 break;
3026 if (++lineno < *first_displayed_line) {
3027 free(line);
3028 continue;
3031 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3032 err = format_line(&wline, &width, line, wlimit);
3033 if (err) {
3034 free(line);
3035 return err;
3038 if (view->focussed && nprinted == selected_line - 1)
3039 wstandout(view->window);
3041 if (nlines > 0) {
3042 blame_line = &lines[lineno - 1];
3043 if (blame_line->annotated && prev_id &&
3044 got_object_id_cmp(prev_id, blame_line->id) == 0)
3045 waddstr(view->window, " ");
3046 else if (blame_line->annotated) {
3047 char *id_str;
3048 err = got_object_id_str(&id_str, blame_line->id);
3049 if (err) {
3050 free(line);
3051 free(wline);
3052 return err;
3054 wprintw(view->window, "%.8s ", id_str);
3055 free(id_str);
3056 prev_id = blame_line->id;
3057 } else {
3058 waddstr(view->window, "........ ");
3059 prev_id = NULL;
3061 } else {
3062 waddstr(view->window, "........ ");
3063 prev_id = NULL;
3066 waddwstr(view->window, wline);
3067 while (width < wlimit) {
3068 waddch(view->window, ' ');
3069 width++;
3071 if (view->focussed && nprinted == selected_line - 1)
3072 wstandend(view->window);
3073 if (++nprinted == 1)
3074 *first_displayed_line = lineno;
3075 free(line);
3076 free(wline);
3077 wline = NULL;
3079 *last_displayed_line = lineno;
3081 view_vborder(view);
3083 return NULL;
3086 static const struct got_error *
3087 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3089 const struct got_error *err = NULL;
3090 struct tog_blame_cb_args *a = arg;
3091 struct tog_blame_line *line;
3092 int errcode;
3094 if (nlines != a->nlines ||
3095 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3096 return got_error(GOT_ERR_RANGE);
3098 errcode = pthread_mutex_lock(&tog_mutex);
3099 if (errcode)
3100 return got_error_set_errno(errcode, "pthread_mutex_lock");
3102 if (*a->quit) { /* user has quit the blame view */
3103 err = got_error(GOT_ERR_ITER_COMPLETED);
3104 goto done;
3107 if (lineno == -1)
3108 goto done; /* no change in this commit */
3110 line = &a->lines[lineno - 1];
3111 if (line->annotated)
3112 goto done;
3114 line->id = got_object_id_dup(id);
3115 if (line->id == NULL) {
3116 err = got_error_from_errno("got_object_id_dup");
3117 goto done;
3119 line->annotated = 1;
3120 done:
3121 errcode = pthread_mutex_unlock(&tog_mutex);
3122 if (errcode)
3123 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3124 return err;
3127 static void *
3128 blame_thread(void *arg)
3130 const struct got_error *err;
3131 struct tog_blame_thread_args *ta = arg;
3132 struct tog_blame_cb_args *a = ta->cb_args;
3133 int errcode;
3135 err = got_blame(ta->path, a->commit_id, ta->repo,
3136 blame_cb, ta->cb_args);
3138 errcode = pthread_mutex_lock(&tog_mutex);
3139 if (errcode)
3140 return (void *)got_error_set_errno(errcode,
3141 "pthread_mutex_lock");
3143 got_repo_close(ta->repo);
3144 ta->repo = NULL;
3145 *ta->complete = 1;
3147 errcode = pthread_mutex_unlock(&tog_mutex);
3148 if (errcode && err == NULL)
3149 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3151 return (void *)err;
3154 static struct got_object_id *
3155 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3156 int first_displayed_line, int selected_line)
3158 struct tog_blame_line *line;
3160 if (nlines <= 0)
3161 return NULL;
3163 line = &lines[first_displayed_line - 1 + selected_line - 1];
3164 if (!line->annotated)
3165 return NULL;
3167 return line->id;
3170 static const struct got_error *
3171 stop_blame(struct tog_blame *blame)
3173 const struct got_error *err = NULL;
3174 int i;
3176 if (blame->thread) {
3177 int errcode;
3178 errcode = pthread_mutex_unlock(&tog_mutex);
3179 if (errcode)
3180 return got_error_set_errno(errcode,
3181 "pthread_mutex_unlock");
3182 errcode = pthread_join(blame->thread, (void **)&err);
3183 if (errcode)
3184 return got_error_set_errno(errcode, "pthread_join");
3185 errcode = pthread_mutex_lock(&tog_mutex);
3186 if (errcode)
3187 return got_error_set_errno(errcode,
3188 "pthread_mutex_lock");
3189 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3190 err = NULL;
3191 blame->thread = NULL;
3193 if (blame->thread_args.repo) {
3194 got_repo_close(blame->thread_args.repo);
3195 blame->thread_args.repo = NULL;
3197 if (blame->f) {
3198 if (fclose(blame->f) != 0 && err == NULL)
3199 err = got_error_from_errno("fclose");
3200 blame->f = NULL;
3202 if (blame->lines) {
3203 for (i = 0; i < blame->nlines; i++)
3204 free(blame->lines[i].id);
3205 free(blame->lines);
3206 blame->lines = NULL;
3208 free(blame->cb_args.commit_id);
3209 blame->cb_args.commit_id = NULL;
3211 return err;
3214 static const struct got_error *
3215 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3216 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3217 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3218 struct got_repository *repo)
3220 const struct got_error *err = NULL;
3221 struct got_blob_object *blob = NULL;
3222 struct got_repository *thread_repo = NULL;
3223 struct got_object_id *obj_id = NULL;
3224 int obj_type;
3226 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3227 if (err)
3228 return err;
3229 if (obj_id == NULL)
3230 return got_error(GOT_ERR_NO_OBJ);
3232 err = got_object_get_type(&obj_type, repo, obj_id);
3233 if (err)
3234 goto done;
3236 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3237 err = got_error(GOT_ERR_OBJ_TYPE);
3238 goto done;
3241 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3242 if (err)
3243 goto done;
3244 blame->f = got_opentemp();
3245 if (blame->f == NULL) {
3246 err = got_error_from_errno("got_opentemp");
3247 goto done;
3249 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3250 &blame->line_offsets, blame->f, blob);
3251 if (err || blame->nlines == 0)
3252 goto done;
3254 /* Don't include \n at EOF in the blame line count. */
3255 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3256 blame->nlines--;
3258 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3259 if (blame->lines == NULL) {
3260 err = got_error_from_errno("calloc");
3261 goto done;
3264 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3265 if (err)
3266 goto done;
3268 blame->cb_args.view = view;
3269 blame->cb_args.lines = blame->lines;
3270 blame->cb_args.nlines = blame->nlines;
3271 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3272 if (blame->cb_args.commit_id == NULL) {
3273 err = got_error_from_errno("got_object_id_dup");
3274 goto done;
3276 blame->cb_args.quit = done;
3278 blame->thread_args.path = path;
3279 blame->thread_args.repo = thread_repo;
3280 blame->thread_args.cb_args = &blame->cb_args;
3281 blame->thread_args.complete = blame_complete;
3282 *blame_complete = 0;
3284 done:
3285 if (blob)
3286 got_object_blob_close(blob);
3287 free(obj_id);
3288 if (err)
3289 stop_blame(blame);
3290 return err;
3293 static const struct got_error *
3294 open_blame_view(struct tog_view *view, char *path,
3295 struct got_object_id *commit_id, struct got_reflist_head *refs,
3296 struct got_repository *repo)
3298 const struct got_error *err = NULL;
3299 struct tog_blame_view_state *s = &view->state.blame;
3301 SIMPLEQ_INIT(&s->blamed_commits);
3303 s->path = strdup(path);
3304 if (s->path == NULL)
3305 return got_error_from_errno("strdup");
3307 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3308 if (err) {
3309 free(s->path);
3310 return err;
3313 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3314 s->first_displayed_line = 1;
3315 s->last_displayed_line = view->nlines;
3316 s->selected_line = 1;
3317 s->blame_complete = 0;
3318 s->repo = repo;
3319 s->refs = refs;
3320 s->commit_id = commit_id;
3321 memset(&s->blame, 0, sizeof(s->blame));
3323 view->show = show_blame_view;
3324 view->input = input_blame_view;
3325 view->close = close_blame_view;
3326 view->search_start = search_start_blame_view;
3327 view->search_next = search_next_blame_view;
3329 return run_blame(&s->blame, view, &s->blame_complete,
3330 &s->first_displayed_line, &s->last_displayed_line,
3331 &s->selected_line, &s->done, &s->eof, s->path,
3332 s->blamed_commit->id, s->repo);
3335 static const struct got_error *
3336 close_blame_view(struct tog_view *view)
3338 const struct got_error *err = NULL;
3339 struct tog_blame_view_state *s = &view->state.blame;
3341 if (s->blame.thread)
3342 err = stop_blame(&s->blame);
3344 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3345 struct got_object_qid *blamed_commit;
3346 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3347 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3348 got_object_qid_free(blamed_commit);
3351 free(s->path);
3353 return err;
3356 static const struct got_error *
3357 search_start_blame_view(struct tog_view *view)
3359 struct tog_blame_view_state *s = &view->state.blame;
3361 s->matched_line = 0;
3362 return NULL;
3365 static int
3366 match_line(const char *line, regex_t *regex)
3368 regmatch_t regmatch;
3370 return regexec(regex, line, 1, &regmatch, 0) == 0;
3374 static const struct got_error *
3375 search_next_blame_view(struct tog_view *view)
3377 struct tog_blame_view_state *s = &view->state.blame;
3378 int lineno;
3380 if (!view->searching) {
3381 view->search_next_done = 1;
3382 return NULL;
3385 if (s->matched_line) {
3386 if (view->searching == TOG_SEARCH_FORWARD)
3387 lineno = s->matched_line + 1;
3388 else
3389 lineno = s->matched_line - 1;
3390 } else {
3391 if (view->searching == TOG_SEARCH_FORWARD)
3392 lineno = 1;
3393 else
3394 lineno = s->blame.nlines;
3397 while (1) {
3398 char *line = NULL;
3399 off_t offset;
3400 size_t len;
3402 if (lineno <= 0 || lineno > s->blame.nlines) {
3403 if (s->matched_line == 0) {
3404 view->search_next_done = 1;
3405 free(line);
3406 break;
3409 if (view->searching == TOG_SEARCH_FORWARD)
3410 lineno = 1;
3411 else
3412 lineno = s->blame.nlines;
3415 offset = s->blame.line_offsets[lineno - 1];
3416 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3417 free(line);
3418 return got_error_from_errno("fseeko");
3420 free(line);
3421 line = parse_next_line(s->blame.f, &len);
3422 if (line && match_line(line, &view->regex)) {
3423 view->search_next_done = 1;
3424 s->matched_line = lineno;
3425 free(line);
3426 break;
3428 free(line);
3429 if (view->searching == TOG_SEARCH_FORWARD)
3430 lineno++;
3431 else
3432 lineno--;
3435 if (s->matched_line) {
3436 s->first_displayed_line = s->matched_line;
3437 s->selected_line = 1;
3440 return NULL;
3443 static const struct got_error *
3444 show_blame_view(struct tog_view *view)
3446 const struct got_error *err = NULL;
3447 struct tog_blame_view_state *s = &view->state.blame;
3448 int errcode;
3450 if (s->blame.thread == NULL) {
3451 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3452 &s->blame.thread_args);
3453 if (errcode)
3454 return got_error_set_errno(errcode, "pthread_create");
3456 halfdelay(1); /* fast refresh while annotating */
3459 if (s->blame_complete)
3460 halfdelay(10); /* disable fast refresh */
3462 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3463 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3464 s->selected_line, &s->first_displayed_line,
3465 &s->last_displayed_line, &s->eof, view->nlines);
3467 view_vborder(view);
3468 return err;
3471 static const struct got_error *
3472 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3473 struct tog_view **focus_view, struct tog_view *view, int ch)
3475 const struct got_error *err = NULL, *thread_err = NULL;
3476 struct tog_view *diff_view;
3477 struct tog_blame_view_state *s = &view->state.blame;
3478 int begin_x = 0;
3480 switch (ch) {
3481 case 'q':
3482 s->done = 1;
3483 break;
3484 case 'k':
3485 case KEY_UP:
3486 if (s->selected_line > 1)
3487 s->selected_line--;
3488 else if (s->selected_line == 1 &&
3489 s->first_displayed_line > 1)
3490 s->first_displayed_line--;
3491 break;
3492 case KEY_PPAGE:
3493 if (s->first_displayed_line == 1) {
3494 s->selected_line = 1;
3495 break;
3497 if (s->first_displayed_line > view->nlines - 2)
3498 s->first_displayed_line -=
3499 (view->nlines - 2);
3500 else
3501 s->first_displayed_line = 1;
3502 break;
3503 case 'j':
3504 case KEY_DOWN:
3505 if (s->selected_line < view->nlines - 2 &&
3506 s->first_displayed_line +
3507 s->selected_line <= s->blame.nlines)
3508 s->selected_line++;
3509 else if (s->last_displayed_line <
3510 s->blame.nlines)
3511 s->first_displayed_line++;
3512 break;
3513 case 'b':
3514 case 'p': {
3515 struct got_object_id *id = NULL;
3516 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3517 s->first_displayed_line, s->selected_line);
3518 if (id == NULL)
3519 break;
3520 if (ch == 'p') {
3521 struct got_commit_object *commit;
3522 struct got_object_qid *pid;
3523 struct got_object_id *blob_id = NULL;
3524 int obj_type;
3525 err = got_object_open_as_commit(&commit,
3526 s->repo, id);
3527 if (err)
3528 break;
3529 pid = SIMPLEQ_FIRST(
3530 got_object_commit_get_parent_ids(commit));
3531 if (pid == NULL) {
3532 got_object_commit_close(commit);
3533 break;
3535 /* Check if path history ends here. */
3536 err = got_object_id_by_path(&blob_id, s->repo,
3537 pid->id, s->path);
3538 if (err) {
3539 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3540 err = NULL;
3541 got_object_commit_close(commit);
3542 break;
3544 err = got_object_get_type(&obj_type, s->repo,
3545 blob_id);
3546 free(blob_id);
3547 /* Can't blame non-blob type objects. */
3548 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3549 got_object_commit_close(commit);
3550 break;
3552 err = got_object_qid_alloc(&s->blamed_commit,
3553 pid->id);
3554 got_object_commit_close(commit);
3555 } else {
3556 if (got_object_id_cmp(id,
3557 s->blamed_commit->id) == 0)
3558 break;
3559 err = got_object_qid_alloc(&s->blamed_commit,
3560 id);
3562 if (err)
3563 break;
3564 s->done = 1;
3565 thread_err = stop_blame(&s->blame);
3566 s->done = 0;
3567 if (thread_err)
3568 break;
3569 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3570 s->blamed_commit, entry);
3571 err = run_blame(&s->blame, view, &s->blame_complete,
3572 &s->first_displayed_line, &s->last_displayed_line,
3573 &s->selected_line, &s->done, &s->eof,
3574 s->path, s->blamed_commit->id, s->repo);
3575 if (err)
3576 break;
3577 break;
3579 case 'B': {
3580 struct got_object_qid *first;
3581 first = SIMPLEQ_FIRST(&s->blamed_commits);
3582 if (!got_object_id_cmp(first->id, s->commit_id))
3583 break;
3584 s->done = 1;
3585 thread_err = stop_blame(&s->blame);
3586 s->done = 0;
3587 if (thread_err)
3588 break;
3589 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3590 got_object_qid_free(s->blamed_commit);
3591 s->blamed_commit =
3592 SIMPLEQ_FIRST(&s->blamed_commits);
3593 err = run_blame(&s->blame, view, &s->blame_complete,
3594 &s->first_displayed_line, &s->last_displayed_line,
3595 &s->selected_line, &s->done, &s->eof, s->path,
3596 s->blamed_commit->id, s->repo);
3597 if (err)
3598 break;
3599 break;
3601 case KEY_ENTER:
3602 case '\r': {
3603 struct got_object_id *id = NULL;
3604 struct got_object_qid *pid;
3605 struct got_commit_object *commit = NULL;
3606 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3607 s->first_displayed_line, s->selected_line);
3608 if (id == NULL)
3609 break;
3610 err = got_object_open_as_commit(&commit, s->repo, id);
3611 if (err)
3612 break;
3613 pid = SIMPLEQ_FIRST(
3614 got_object_commit_get_parent_ids(commit));
3615 if (view_is_parent_view(view))
3616 begin_x = view_split_begin_x(view->begin_x);
3617 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3618 if (diff_view == NULL) {
3619 got_object_commit_close(commit);
3620 err = got_error_from_errno("view_open");
3621 break;
3623 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3624 id, NULL, s->refs, s->repo);
3625 got_object_commit_close(commit);
3626 if (err) {
3627 view_close(diff_view);
3628 break;
3630 if (view_is_parent_view(view)) {
3631 err = view_close_child(view);
3632 if (err)
3633 break;
3634 err = view_set_child(view, diff_view);
3635 if (err) {
3636 view_close(diff_view);
3637 break;
3639 *focus_view = diff_view;
3640 view->child_focussed = 1;
3641 } else
3642 *new_view = diff_view;
3643 if (err)
3644 break;
3645 break;
3647 case KEY_NPAGE:
3648 case ' ':
3649 if (s->last_displayed_line >= s->blame.nlines &&
3650 s->selected_line >= MIN(s->blame.nlines,
3651 view->nlines - 2)) {
3652 break;
3654 if (s->last_displayed_line >= s->blame.nlines &&
3655 s->selected_line < view->nlines - 2) {
3656 s->selected_line = MIN(s->blame.nlines,
3657 view->nlines - 2);
3658 break;
3660 if (s->last_displayed_line + view->nlines - 2
3661 <= s->blame.nlines)
3662 s->first_displayed_line +=
3663 view->nlines - 2;
3664 else
3665 s->first_displayed_line =
3666 s->blame.nlines -
3667 (view->nlines - 3);
3668 break;
3669 case KEY_RESIZE:
3670 if (s->selected_line > view->nlines - 2) {
3671 s->selected_line = MIN(s->blame.nlines,
3672 view->nlines - 2);
3674 break;
3675 default:
3676 break;
3678 return thread_err ? thread_err : err;
3681 static const struct got_error *
3682 cmd_blame(int argc, char *argv[])
3684 const struct got_error *error;
3685 struct got_repository *repo = NULL;
3686 struct got_reflist_head refs;
3687 struct got_worktree *worktree = NULL;
3688 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3689 struct got_object_id *commit_id = NULL;
3690 char *commit_id_str = NULL;
3691 int ch;
3692 struct tog_view *view;
3694 SIMPLEQ_INIT(&refs);
3696 #ifndef PROFILE
3697 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3698 NULL) == -1)
3699 err(1, "pledge");
3700 #endif
3702 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3703 switch (ch) {
3704 case 'c':
3705 commit_id_str = optarg;
3706 break;
3707 case 'r':
3708 repo_path = realpath(optarg, NULL);
3709 if (repo_path == NULL)
3710 err(1, "-r option");
3711 break;
3712 default:
3713 usage_blame();
3714 /* NOTREACHED */
3718 argc -= optind;
3719 argv += optind;
3721 if (argc == 1)
3722 path = argv[0];
3723 else
3724 usage_blame();
3726 cwd = getcwd(NULL, 0);
3727 if (cwd == NULL) {
3728 error = got_error_from_errno("getcwd");
3729 goto done;
3731 if (repo_path == NULL) {
3732 error = got_worktree_open(&worktree, cwd);
3733 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3734 goto done;
3735 else
3736 error = NULL;
3737 if (worktree) {
3738 repo_path =
3739 strdup(got_worktree_get_repo_path(worktree));
3740 if (repo_path == NULL)
3741 error = got_error_from_errno("strdup");
3742 if (error)
3743 goto done;
3744 } else {
3745 repo_path = strdup(cwd);
3746 if (repo_path == NULL) {
3747 error = got_error_from_errno("strdup");
3748 goto done;
3753 init_curses();
3755 error = got_repo_open(&repo, repo_path);
3756 if (error != NULL)
3757 goto done;
3759 error = apply_unveil(got_repo_get_path(repo), NULL);
3760 if (error)
3761 goto done;
3763 if (worktree) {
3764 const char *prefix = got_worktree_get_path_prefix(worktree);
3765 char *p, *worktree_subdir = cwd +
3766 strlen(got_worktree_get_root_path(worktree));
3767 if (asprintf(&p, "%s%s%s%s%s",
3768 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3769 worktree_subdir, worktree_subdir[0] ? "/" : "",
3770 path) == -1) {
3771 error = got_error_from_errno("asprintf");
3772 goto done;
3774 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3775 free(p);
3776 } else {
3777 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3779 if (error)
3780 goto done;
3782 if (commit_id_str == NULL) {
3783 struct got_reference *head_ref;
3784 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3785 if (error != NULL)
3786 goto done;
3787 error = got_ref_resolve(&commit_id, repo, head_ref);
3788 got_ref_close(head_ref);
3789 } else {
3790 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3791 if (error) {
3792 if (error->code != GOT_ERR_NOT_REF)
3793 goto done;
3794 error = got_repo_match_object_id_prefix(&commit_id,
3795 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3798 if (error != NULL)
3799 goto done;
3801 error = got_ref_list(&refs, repo);
3802 if (error)
3803 goto done;
3805 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3806 if (view == NULL) {
3807 error = got_error_from_errno("view_open");
3808 goto done;
3810 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3811 if (error)
3812 goto done;
3813 error = view_loop(view);
3814 done:
3815 free(repo_path);
3816 free(cwd);
3817 free(commit_id);
3818 if (worktree)
3819 got_worktree_close(worktree);
3820 if (repo)
3821 got_repo_close(repo);
3822 got_ref_list_free(&refs);
3823 return error;
3826 static const struct got_error *
3827 draw_tree_entries(struct tog_view *view,
3828 struct got_tree_entry **first_displayed_entry,
3829 struct got_tree_entry **last_displayed_entry,
3830 struct got_tree_entry **selected_entry, int *ndisplayed,
3831 const char *label, int show_ids, const char *parent_path,
3832 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3834 const struct got_error *err = NULL;
3835 struct got_tree_entry *te;
3836 wchar_t *wline;
3837 int width, n;
3839 *ndisplayed = 0;
3841 werase(view->window);
3843 if (limit == 0)
3844 return NULL;
3846 err = format_line(&wline, &width, label, view->ncols);
3847 if (err)
3848 return err;
3849 if (view_needs_focus_indication(view))
3850 wstandout(view->window);
3851 waddwstr(view->window, wline);
3852 if (view_needs_focus_indication(view))
3853 wstandend(view->window);
3854 free(wline);
3855 wline = NULL;
3856 if (width < view->ncols - 1)
3857 waddch(view->window, '\n');
3858 if (--limit <= 0)
3859 return NULL;
3860 err = format_line(&wline, &width, parent_path, view->ncols);
3861 if (err)
3862 return err;
3863 waddwstr(view->window, wline);
3864 free(wline);
3865 wline = NULL;
3866 if (width < view->ncols - 1)
3867 waddch(view->window, '\n');
3868 if (--limit <= 0)
3869 return NULL;
3870 waddch(view->window, '\n');
3871 if (--limit <= 0)
3872 return NULL;
3874 te = SIMPLEQ_FIRST(&entries->head);
3875 if (*first_displayed_entry == NULL) {
3876 if (selected == 0) {
3877 if (view->focussed)
3878 wstandout(view->window);
3879 *selected_entry = NULL;
3881 waddstr(view->window, " ..\n"); /* parent directory */
3882 if (selected == 0 && view->focussed)
3883 wstandend(view->window);
3884 (*ndisplayed)++;
3885 if (--limit <= 0)
3886 return NULL;
3887 n = 1;
3888 } else {
3889 n = 0;
3890 while (te != *first_displayed_entry)
3891 te = SIMPLEQ_NEXT(te, entry);
3894 while (te) {
3895 char *line = NULL, *id_str = NULL;
3896 const char *modestr = "";
3898 if (show_ids) {
3899 err = got_object_id_str(&id_str, te->id);
3900 if (err)
3901 return got_error_from_errno(
3902 "got_object_id_str");
3904 if (S_ISLNK(te->mode))
3905 modestr = "@";
3906 else if (S_ISDIR(te->mode))
3907 modestr = "/";
3908 else if (te->mode & S_IXUSR)
3909 modestr = "*";
3910 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3911 te->name, modestr) == -1) {
3912 free(id_str);
3913 return got_error_from_errno("asprintf");
3915 free(id_str);
3916 err = format_line(&wline, &width, line, view->ncols);
3917 if (err) {
3918 free(line);
3919 break;
3921 if (n == selected) {
3922 if (view->focussed)
3923 wstandout(view->window);
3924 *selected_entry = te;
3926 waddwstr(view->window, wline);
3927 if (width < view->ncols - 1)
3928 waddch(view->window, '\n');
3929 if (n == selected && view->focussed)
3930 wstandend(view->window);
3931 free(line);
3932 free(wline);
3933 wline = NULL;
3934 n++;
3935 (*ndisplayed)++;
3936 *last_displayed_entry = te;
3937 if (--limit <= 0)
3938 break;
3939 te = SIMPLEQ_NEXT(te, entry);
3942 return err;
3945 static void
3946 tree_scroll_up(struct tog_view *view,
3947 struct got_tree_entry **first_displayed_entry, int maxscroll,
3948 const struct got_tree_entries *entries, int isroot)
3950 struct got_tree_entry *te, *prev;
3951 int i;
3953 if (*first_displayed_entry == NULL)
3954 return;
3956 te = SIMPLEQ_FIRST(&entries->head);
3957 if (*first_displayed_entry == te) {
3958 if (!isroot)
3959 *first_displayed_entry = NULL;
3960 return;
3963 /* XXX this is stupid... switch to TAILQ? */
3964 for (i = 0; i < maxscroll; i++) {
3965 while (te != *first_displayed_entry) {
3966 prev = te;
3967 te = SIMPLEQ_NEXT(te, entry);
3969 *first_displayed_entry = prev;
3970 te = SIMPLEQ_FIRST(&entries->head);
3972 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3973 *first_displayed_entry = NULL;
3976 static int
3977 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3978 struct got_tree_entry *last_displayed_entry,
3979 const struct got_tree_entries *entries)
3981 struct got_tree_entry *next, *last;
3982 int n = 0;
3984 if (*first_displayed_entry)
3985 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3986 else
3987 next = SIMPLEQ_FIRST(&entries->head);
3988 last = last_displayed_entry;
3989 while (next && last && n++ < maxscroll) {
3990 last = SIMPLEQ_NEXT(last, entry);
3991 if (last) {
3992 *first_displayed_entry = next;
3993 next = SIMPLEQ_NEXT(next, entry);
3996 return n;
3999 static const struct got_error *
4000 tree_entry_path(char **path, struct tog_parent_trees *parents,
4001 struct got_tree_entry *te)
4003 const struct got_error *err = NULL;
4004 struct tog_parent_tree *pt;
4005 size_t len = 2; /* for leading slash and NUL */
4007 TAILQ_FOREACH(pt, parents, entry)
4008 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4009 if (te)
4010 len += strlen(te->name);
4012 *path = calloc(1, len);
4013 if (path == NULL)
4014 return got_error_from_errno("calloc");
4016 (*path)[0] = '/';
4017 pt = TAILQ_LAST(parents, tog_parent_trees);
4018 while (pt) {
4019 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4020 err = got_error(GOT_ERR_NO_SPACE);
4021 goto done;
4023 if (strlcat(*path, "/", len) >= len) {
4024 err = got_error(GOT_ERR_NO_SPACE);
4025 goto done;
4027 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4029 if (te) {
4030 if (strlcat(*path, te->name, len) >= len) {
4031 err = got_error(GOT_ERR_NO_SPACE);
4032 goto done;
4035 done:
4036 if (err) {
4037 free(*path);
4038 *path = NULL;
4040 return err;
4043 static const struct got_error *
4044 blame_tree_entry(struct tog_view **new_view, int begin_x,
4045 struct got_tree_entry *te, struct tog_parent_trees *parents,
4046 struct got_object_id *commit_id, struct got_reflist_head *refs,
4047 struct got_repository *repo)
4049 const struct got_error *err = NULL;
4050 char *path;
4051 struct tog_view *blame_view;
4053 *new_view = NULL;
4055 err = tree_entry_path(&path, parents, te);
4056 if (err)
4057 return err;
4059 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4060 if (blame_view == NULL) {
4061 err = got_error_from_errno("view_open");
4062 goto done;
4065 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4066 if (err) {
4067 view_close(blame_view);
4068 } else
4069 *new_view = blame_view;
4070 done:
4071 free(path);
4072 return err;
4075 static const struct got_error *
4076 log_tree_entry(struct tog_view **new_view, int begin_x,
4077 struct got_tree_entry *te, struct tog_parent_trees *parents,
4078 struct got_object_id *commit_id, struct got_reflist_head *refs,
4079 struct got_repository *repo)
4081 struct tog_view *log_view;
4082 const struct got_error *err = NULL;
4083 char *path;
4085 *new_view = NULL;
4087 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4088 if (log_view == NULL)
4089 return got_error_from_errno("view_open");
4091 err = tree_entry_path(&path, parents, te);
4092 if (err)
4093 return err;
4095 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4096 if (err)
4097 view_close(log_view);
4098 else
4099 *new_view = log_view;
4100 free(path);
4101 return err;
4104 static const struct got_error *
4105 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4106 struct got_object_id *commit_id, struct got_reflist_head *refs,
4107 struct got_repository *repo)
4109 const struct got_error *err = NULL;
4110 char *commit_id_str = NULL;
4111 struct tog_tree_view_state *s = &view->state.tree;
4113 TAILQ_INIT(&s->parents);
4115 err = got_object_id_str(&commit_id_str, commit_id);
4116 if (err != NULL)
4117 goto done;
4119 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4120 err = got_error_from_errno("asprintf");
4121 goto done;
4124 s->root = s->tree = root;
4125 s->entries = got_object_tree_get_entries(root);
4126 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4127 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4128 s->commit_id = got_object_id_dup(commit_id);
4129 if (s->commit_id == NULL) {
4130 err = got_error_from_errno("got_object_id_dup");
4131 goto done;
4133 s->refs = refs;
4134 s->repo = repo;
4136 view->show = show_tree_view;
4137 view->input = input_tree_view;
4138 view->close = close_tree_view;
4139 view->search_start = search_start_tree_view;
4140 view->search_next = search_next_tree_view;
4141 done:
4142 free(commit_id_str);
4143 if (err) {
4144 free(s->tree_label);
4145 s->tree_label = NULL;
4147 return err;
4150 static const struct got_error *
4151 close_tree_view(struct tog_view *view)
4153 struct tog_tree_view_state *s = &view->state.tree;
4155 free(s->tree_label);
4156 s->tree_label = NULL;
4157 free(s->commit_id);
4158 s->commit_id = NULL;
4159 while (!TAILQ_EMPTY(&s->parents)) {
4160 struct tog_parent_tree *parent;
4161 parent = TAILQ_FIRST(&s->parents);
4162 TAILQ_REMOVE(&s->parents, parent, entry);
4163 free(parent);
4166 if (s->tree != s->root)
4167 got_object_tree_close(s->tree);
4168 got_object_tree_close(s->root);
4170 return NULL;
4173 static const struct got_error *
4174 search_start_tree_view(struct tog_view *view)
4176 struct tog_tree_view_state *s = &view->state.tree;
4178 s->matched_entry = NULL;
4179 return NULL;
4182 static int
4183 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4185 regmatch_t regmatch;
4187 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4190 static const struct got_error *
4191 search_next_tree_view(struct tog_view *view)
4193 struct tog_tree_view_state *s = &view->state.tree;
4194 struct got_tree_entry *entry = NULL, *te;
4196 if (!view->searching) {
4197 view->search_next_done = 1;
4198 return NULL;
4201 if (s->matched_entry) {
4202 if (view->searching == TOG_SEARCH_FORWARD) {
4203 if (s->selected_entry)
4204 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4205 else
4206 entry = SIMPLEQ_FIRST(&s->entries->head);
4208 else {
4209 if (s->selected_entry == NULL) {
4210 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4211 entry = te;
4212 } else {
4213 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4214 entry = te;
4215 if (SIMPLEQ_NEXT(te, entry) ==
4216 s->selected_entry)
4217 break;
4221 } else {
4222 if (view->searching == TOG_SEARCH_FORWARD)
4223 entry = SIMPLEQ_FIRST(&s->entries->head);
4224 else {
4225 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4226 entry = te;
4230 while (1) {
4231 if (entry == NULL) {
4232 if (s->matched_entry == NULL) {
4233 view->search_next_done = 1;
4234 return NULL;
4236 if (view->searching == TOG_SEARCH_FORWARD)
4237 entry = SIMPLEQ_FIRST(&s->entries->head);
4238 else {
4239 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4240 entry = te;
4244 if (match_tree_entry(entry, &view->regex)) {
4245 view->search_next_done = 1;
4246 s->matched_entry = entry;
4247 break;
4250 if (view->searching == TOG_SEARCH_FORWARD)
4251 entry = SIMPLEQ_NEXT(entry, entry);
4252 else {
4253 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4254 entry = NULL;
4255 else {
4256 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4257 if (SIMPLEQ_NEXT(te, entry) == entry) {
4258 entry = te;
4259 break;
4266 if (s->matched_entry) {
4267 s->first_displayed_entry = s->matched_entry;
4268 s->selected = 0;
4271 return NULL;
4274 static const struct got_error *
4275 show_tree_view(struct tog_view *view)
4277 const struct got_error *err = NULL;
4278 struct tog_tree_view_state *s = &view->state.tree;
4279 char *parent_path;
4281 err = tree_entry_path(&parent_path, &s->parents, NULL);
4282 if (err)
4283 return err;
4285 err = draw_tree_entries(view, &s->first_displayed_entry,
4286 &s->last_displayed_entry, &s->selected_entry,
4287 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4288 s->entries, s->selected, view->nlines, s->tree == s->root);
4289 free(parent_path);
4291 view_vborder(view);
4292 return err;
4295 static const struct got_error *
4296 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4297 struct tog_view **focus_view, struct tog_view *view, int ch)
4299 const struct got_error *err = NULL;
4300 struct tog_tree_view_state *s = &view->state.tree;
4301 struct tog_view *log_view;
4302 int begin_x = 0, nscrolled;
4304 switch (ch) {
4305 case 'i':
4306 s->show_ids = !s->show_ids;
4307 break;
4308 case 'l':
4309 if (!s->selected_entry)
4310 break;
4311 if (view_is_parent_view(view))
4312 begin_x = view_split_begin_x(view->begin_x);
4313 err = log_tree_entry(&log_view, begin_x,
4314 s->selected_entry, &s->parents,
4315 s->commit_id, s->refs, s->repo);
4316 if (view_is_parent_view(view)) {
4317 err = view_close_child(view);
4318 if (err)
4319 return err;
4320 err = view_set_child(view, log_view);
4321 if (err) {
4322 view_close(log_view);
4323 break;
4325 *focus_view = log_view;
4326 view->child_focussed = 1;
4327 } else
4328 *new_view = log_view;
4329 break;
4330 case 'k':
4331 case KEY_UP:
4332 if (s->selected > 0) {
4333 s->selected--;
4334 if (s->selected == 0)
4335 break;
4337 if (s->selected > 0)
4338 break;
4339 tree_scroll_up(view, &s->first_displayed_entry, 1,
4340 s->entries, s->tree == s->root);
4341 break;
4342 case KEY_PPAGE:
4343 tree_scroll_up(view, &s->first_displayed_entry,
4344 MAX(0, view->nlines - 4 - s->selected), s->entries,
4345 s->tree == s->root);
4346 s->selected = 0;
4347 if (SIMPLEQ_FIRST(&s->entries->head) ==
4348 s->first_displayed_entry && s->tree != s->root)
4349 s->first_displayed_entry = NULL;
4350 break;
4351 case 'j':
4352 case KEY_DOWN:
4353 if (s->selected < s->ndisplayed - 1) {
4354 s->selected++;
4355 break;
4357 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4358 /* can't scroll any further */
4359 break;
4360 tree_scroll_down(&s->first_displayed_entry, 1,
4361 s->last_displayed_entry, s->entries);
4362 break;
4363 case KEY_NPAGE:
4364 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4365 == NULL) {
4366 /* can't scroll any further; move cursor down */
4367 if (s->selected < s->ndisplayed - 1)
4368 s->selected = s->ndisplayed - 1;
4369 break;
4371 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4372 view->nlines, s->last_displayed_entry, s->entries);
4373 if (nscrolled < view->nlines) {
4374 int ndisplayed = 0;
4375 struct got_tree_entry *te;
4376 te = s->first_displayed_entry;
4377 do {
4378 ndisplayed++;
4379 te = SIMPLEQ_NEXT(te, entry);
4380 } while (te);
4381 s->selected = ndisplayed - 1;
4383 break;
4384 case KEY_ENTER:
4385 case '\r':
4386 case KEY_BACKSPACE:
4387 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4388 struct tog_parent_tree *parent;
4389 /* user selected '..' */
4390 if (s->tree == s->root)
4391 break;
4392 parent = TAILQ_FIRST(&s->parents);
4393 TAILQ_REMOVE(&s->parents, parent,
4394 entry);
4395 got_object_tree_close(s->tree);
4396 s->tree = parent->tree;
4397 s->entries =
4398 got_object_tree_get_entries(s->tree);
4399 s->first_displayed_entry =
4400 parent->first_displayed_entry;
4401 s->selected_entry =
4402 parent->selected_entry;
4403 s->selected = parent->selected;
4404 free(parent);
4405 } else if (S_ISDIR(s->selected_entry->mode)) {
4406 struct got_tree_object *subtree;
4407 err = got_object_open_as_tree(&subtree,
4408 s->repo, s->selected_entry->id);
4409 if (err)
4410 break;
4411 err = tree_view_visit_subtree(subtree, s);
4412 if (err) {
4413 got_object_tree_close(subtree);
4414 break;
4416 } else if (S_ISREG(s->selected_entry->mode)) {
4417 struct tog_view *blame_view;
4418 int begin_x = view_is_parent_view(view) ?
4419 view_split_begin_x(view->begin_x) : 0;
4421 err = blame_tree_entry(&blame_view, begin_x,
4422 s->selected_entry, &s->parents,
4423 s->commit_id, s->refs, s->repo);
4424 if (err)
4425 break;
4426 if (view_is_parent_view(view)) {
4427 err = view_close_child(view);
4428 if (err)
4429 return err;
4430 err = view_set_child(view, blame_view);
4431 if (err) {
4432 view_close(blame_view);
4433 break;
4435 *focus_view = blame_view;
4436 view->child_focussed = 1;
4437 } else
4438 *new_view = blame_view;
4440 break;
4441 case KEY_RESIZE:
4442 if (s->selected > view->nlines)
4443 s->selected = s->ndisplayed - 1;
4444 break;
4445 default:
4446 break;
4449 return err;
4452 __dead static void
4453 usage_tree(void)
4455 endwin();
4456 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4457 getprogname());
4458 exit(1);
4461 static const struct got_error *
4462 cmd_tree(int argc, char *argv[])
4464 const struct got_error *error;
4465 struct got_repository *repo = NULL;
4466 struct got_reflist_head refs;
4467 char *repo_path = NULL;
4468 struct got_object_id *commit_id = NULL;
4469 char *commit_id_arg = NULL;
4470 struct got_commit_object *commit = NULL;
4471 struct got_tree_object *tree = NULL;
4472 int ch;
4473 struct tog_view *view;
4475 SIMPLEQ_INIT(&refs);
4477 #ifndef PROFILE
4478 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4479 NULL) == -1)
4480 err(1, "pledge");
4481 #endif
4483 while ((ch = getopt(argc, argv, "c:")) != -1) {
4484 switch (ch) {
4485 case 'c':
4486 commit_id_arg = optarg;
4487 break;
4488 default:
4489 usage_tree();
4490 /* NOTREACHED */
4494 argc -= optind;
4495 argv += optind;
4497 if (argc == 0) {
4498 struct got_worktree *worktree;
4499 char *cwd = getcwd(NULL, 0);
4500 if (cwd == NULL)
4501 return got_error_from_errno("getcwd");
4502 error = got_worktree_open(&worktree, cwd);
4503 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4504 goto done;
4505 if (worktree) {
4506 free(cwd);
4507 repo_path =
4508 strdup(got_worktree_get_repo_path(worktree));
4509 got_worktree_close(worktree);
4510 } else
4511 repo_path = cwd;
4512 if (repo_path == NULL) {
4513 error = got_error_from_errno("strdup");
4514 goto done;
4516 } else if (argc == 1) {
4517 repo_path = realpath(argv[0], NULL);
4518 if (repo_path == NULL)
4519 return got_error_from_errno2("realpath", argv[0]);
4520 } else
4521 usage_log();
4523 init_curses();
4525 error = got_repo_open(&repo, repo_path);
4526 if (error != NULL)
4527 goto done;
4529 error = apply_unveil(got_repo_get_path(repo), NULL);
4530 if (error)
4531 goto done;
4533 if (commit_id_arg == NULL)
4534 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4535 else {
4536 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4537 if (error) {
4538 if (error->code != GOT_ERR_NOT_REF)
4539 goto done;
4540 error = got_repo_match_object_id_prefix(&commit_id,
4541 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4544 if (error != NULL)
4545 goto done;
4547 error = got_object_open_as_commit(&commit, repo, commit_id);
4548 if (error != NULL)
4549 goto done;
4551 error = got_object_open_as_tree(&tree, repo,
4552 got_object_commit_get_tree_id(commit));
4553 if (error != NULL)
4554 goto done;
4556 error = got_ref_list(&refs, repo);
4557 if (error)
4558 goto done;
4560 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4561 if (view == NULL) {
4562 error = got_error_from_errno("view_open");
4563 goto done;
4565 error = open_tree_view(view, tree, commit_id, &refs, repo);
4566 if (error)
4567 goto done;
4568 error = view_loop(view);
4569 done:
4570 free(repo_path);
4571 free(commit_id);
4572 if (commit)
4573 got_object_commit_close(commit);
4574 if (tree)
4575 got_object_tree_close(tree);
4576 if (repo)
4577 got_repo_close(repo);
4578 got_ref_list_free(&refs);
4579 return error;
4582 static void
4583 list_commands(void)
4585 int i;
4587 fprintf(stderr, "commands:");
4588 for (i = 0; i < nitems(tog_commands); i++) {
4589 struct tog_cmd *cmd = &tog_commands[i];
4590 fprintf(stderr, " %s", cmd->name);
4592 fputc('\n', stderr);
4595 __dead static void
4596 usage(int hflag)
4598 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4599 getprogname());
4600 if (hflag)
4601 list_commands();
4602 exit(1);
4605 static char **
4606 make_argv(const char *arg0, const char *arg1)
4608 char **argv;
4609 int argc = (arg1 == NULL ? 1 : 2);
4611 argv = calloc(argc, sizeof(char *));
4612 if (argv == NULL)
4613 err(1, "calloc");
4614 argv[0] = strdup(arg0);
4615 if (argv[0] == NULL)
4616 err(1, "calloc");
4617 if (arg1) {
4618 argv[1] = strdup(arg1);
4619 if (argv[1] == NULL)
4620 err(1, "calloc");
4623 return argv;
4626 int
4627 main(int argc, char *argv[])
4629 const struct got_error *error = NULL;
4630 struct tog_cmd *cmd = NULL;
4631 int ch, hflag = 0, Vflag = 0;
4632 char **cmd_argv = NULL;
4634 setlocale(LC_CTYPE, "");
4636 while ((ch = getopt(argc, argv, "hV")) != -1) {
4637 switch (ch) {
4638 case 'h':
4639 hflag = 1;
4640 break;
4641 case 'V':
4642 Vflag = 1;
4643 break;
4644 default:
4645 usage(hflag);
4646 /* NOTREACHED */
4650 argc -= optind;
4651 argv += optind;
4652 optind = 0;
4653 optreset = 1;
4655 if (Vflag) {
4656 got_version_print_str();
4657 return 1;
4660 if (argc == 0) {
4661 if (hflag)
4662 usage(hflag);
4663 /* Build an argument vector which runs a default command. */
4664 cmd = &tog_commands[0];
4665 cmd_argv = make_argv(cmd->name, NULL);
4666 argc = 1;
4667 } else {
4668 int i;
4670 /* Did the user specific a command? */
4671 for (i = 0; i < nitems(tog_commands); i++) {
4672 if (strncmp(tog_commands[i].name, argv[0],
4673 strlen(argv[0])) == 0) {
4674 cmd = &tog_commands[i];
4675 break;
4679 if (cmd == NULL) {
4680 fprintf(stderr, "%s: unknown command '%s'\n",
4681 getprogname(), argv[0]);
4682 list_commands();
4683 return 1;
4687 if (hflag)
4688 cmd->cmd_usage();
4689 else
4690 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4692 endwin();
4693 free(cmd_argv);
4694 if (error)
4695 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4696 return 0;