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_utf8.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_path.h"
54 #include "got_worktree.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 #define CTRL(x) ((x) & 0x1f)
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct tog_cmd {
71 const char *name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 };
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_diff_view_state {
116 struct got_object_id *id1, *id2;
117 FILE *f;
118 int first_displayed_line;
119 int last_displayed_line;
120 int eof;
121 int diff_context;
122 struct got_repository *repo;
123 struct got_reflist_head *refs;
125 /* passed from log view; may be NULL */
126 struct tog_view *log_view;
127 };
129 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 struct tog_log_thread_args {
132 pthread_cond_t need_commits;
133 int commits_needed;
134 struct got_commit_graph *graph;
135 struct commit_queue *commits;
136 const char *in_repo_path;
137 struct got_object_id *start_id;
138 struct got_repository *repo;
139 int log_complete;
140 sig_atomic_t *quit;
141 struct tog_view *view;
142 struct commit_queue_entry **first_displayed_entry;
143 struct commit_queue_entry **selected_entry;
144 };
146 struct tog_log_view_state {
147 struct commit_queue commits;
148 struct commit_queue_entry *first_displayed_entry;
149 struct commit_queue_entry *last_displayed_entry;
150 struct commit_queue_entry *selected_entry;
151 int selected;
152 char *in_repo_path;
153 const char *head_ref_name;
154 struct got_repository *repo;
155 struct got_reflist_head *refs;
156 struct got_object_id *start_id;
157 sig_atomic_t quit;
158 pthread_t thread;
159 struct tog_log_thread_args thread_args;
160 struct commit_queue_entry *matched_entry;
161 struct commit_queue_entry *search_entry;
162 };
164 struct tog_blame_cb_args {
165 struct tog_blame_line *lines; /* one per line */
166 int nlines;
168 struct tog_view *view;
169 struct got_object_id *commit_id;
170 int *quit;
171 };
173 struct tog_blame_thread_args {
174 const char *path;
175 struct got_repository *repo;
176 struct tog_blame_cb_args *cb_args;
177 int *complete;
178 };
180 struct tog_blame {
181 FILE *f;
182 size_t filesize;
183 struct tog_blame_line *lines;
184 int nlines;
185 off_t *line_offsets;
186 pthread_t thread;
187 struct tog_blame_thread_args thread_args;
188 struct tog_blame_cb_args cb_args;
189 const char *path;
190 };
192 struct tog_blame_view_state {
193 int first_displayed_line;
194 int last_displayed_line;
195 int selected_line;
196 int blame_complete;
197 int eof;
198 int done;
199 struct got_object_id_queue blamed_commits;
200 struct got_object_qid *blamed_commit;
201 char *path;
202 struct got_repository *repo;
203 struct got_reflist_head *refs;
204 struct got_object_id *commit_id;
205 struct tog_blame blame;
206 int matched_line;
207 };
209 struct tog_parent_tree {
210 TAILQ_ENTRY(tog_parent_tree) entry;
211 struct got_tree_object *tree;
212 struct got_tree_entry *first_displayed_entry;
213 struct got_tree_entry *selected_entry;
214 int selected;
215 };
217 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
219 struct tog_tree_view_state {
220 char *tree_label;
221 struct got_tree_object *root;
222 struct got_tree_object *tree;
223 const struct got_tree_entries *entries;
224 struct got_tree_entry *first_displayed_entry;
225 struct got_tree_entry *last_displayed_entry;
226 struct got_tree_entry *selected_entry;
227 int ndisplayed, selected, show_ids;
228 struct tog_parent_trees parents;
229 struct got_object_id *commit_id;
230 struct got_repository *repo;
231 struct got_reflist_head *refs;
232 struct got_tree_entry *matched_entry;
233 };
235 /*
236 * We implement two types of views: parent views and child views.
238 * The 'Tab' key switches between a parent view and its child view.
239 * Child views are shown side-by-side to their parent view, provided
240 * there is enough screen estate.
242 * When a new view is opened from within a parent view, this new view
243 * becomes a child view of the parent view, replacing any existing child.
245 * When a new view is opened from within a child view, this new view
246 * becomes a parent view which will obscure the views below until the
247 * user quits the new parent view by typing 'q'.
249 * This list of views contains parent views only.
250 * Child views are only pointed to by their parent view.
251 */
252 TAILQ_HEAD(tog_view_list_head, tog_view);
254 struct tog_view {
255 TAILQ_ENTRY(tog_view) entry;
256 WINDOW *window;
257 PANEL *panel;
258 int nlines, ncols, begin_y, begin_x;
259 int lines, cols; /* copies of LINES and COLS */
260 int focussed;
261 struct tog_view *parent;
262 struct tog_view *child;
263 int child_focussed;
265 /* type-specific state */
266 enum tog_view_type type;
267 union {
268 struct tog_diff_view_state diff;
269 struct tog_log_view_state log;
270 struct tog_blame_view_state blame;
271 struct tog_tree_view_state tree;
272 } state;
274 const struct got_error *(*show)(struct tog_view *);
275 const struct got_error *(*input)(struct tog_view **,
276 struct tog_view **, struct tog_view**, struct tog_view *, int);
277 const struct got_error *(*close)(struct tog_view *);
279 const struct got_error *(*search_start)(struct tog_view *);
280 const struct got_error *(*search_next)(struct tog_view *);
281 int searching;
282 #define TOG_SEARCH_FORWARD 1
283 #define TOG_SEARCH_BACKWARD 2
284 int search_next_done;
285 regex_t regex;
286 };
288 static const struct got_error *open_diff_view(struct tog_view *,
289 struct got_object_id *, struct got_object_id *, struct tog_view *,
290 struct got_reflist_head *, struct got_repository *);
291 static const struct got_error *show_diff_view(struct tog_view *);
292 static const struct got_error *input_diff_view(struct tog_view **,
293 struct tog_view **, struct tog_view **, struct tog_view *, int);
294 static const struct got_error* close_diff_view(struct tog_view *);
296 static const struct got_error *open_log_view(struct tog_view *,
297 struct got_object_id *, struct got_reflist_head *,
298 struct got_repository *, const char *, const char *, int);
299 static const struct got_error * show_log_view(struct tog_view *);
300 static const struct got_error *input_log_view(struct tog_view **,
301 struct tog_view **, struct tog_view **, struct tog_view *, int);
302 static const struct got_error *close_log_view(struct tog_view *);
303 static const struct got_error *search_start_log_view(struct tog_view *);
304 static const struct got_error *search_next_log_view(struct tog_view *);
306 static const struct got_error *open_blame_view(struct tog_view *, char *,
307 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
308 static const struct got_error *show_blame_view(struct tog_view *);
309 static const struct got_error *input_blame_view(struct tog_view **,
310 struct tog_view **, struct tog_view **, struct tog_view *, int);
311 static const struct got_error *close_blame_view(struct tog_view *);
312 static const struct got_error *search_start_blame_view(struct tog_view *);
313 static const struct got_error *search_next_blame_view(struct tog_view *);
315 static const struct got_error *open_tree_view(struct tog_view *,
316 struct got_tree_object *, struct got_object_id *,
317 struct got_reflist_head *, struct got_repository *);
318 static const struct got_error *show_tree_view(struct tog_view *);
319 static const struct got_error *input_tree_view(struct tog_view **,
320 struct tog_view **, struct tog_view **, struct tog_view *, int);
321 static const struct got_error *close_tree_view(struct tog_view *);
322 static const struct got_error *search_start_tree_view(struct tog_view *);
323 static const struct got_error *search_next_tree_view(struct tog_view *);
325 static volatile sig_atomic_t tog_sigwinch_received;
326 static volatile sig_atomic_t tog_sigpipe_received;
328 static void
329 tog_sigwinch(int signo)
331 tog_sigwinch_received = 1;
334 static void
335 tog_sigpipe(int signo)
337 tog_sigpipe_received = 1;
340 static const struct got_error *
341 view_close(struct tog_view *view)
343 const struct got_error *err = NULL;
345 if (view->child) {
346 view_close(view->child);
347 view->child = NULL;
349 if (view->close)
350 err = view->close(view);
351 if (view->panel)
352 del_panel(view->panel);
353 if (view->window)
354 delwin(view->window);
355 free(view);
356 return err;
359 static struct tog_view *
360 view_open(int nlines, int ncols, int begin_y, int begin_x,
361 enum tog_view_type type)
363 struct tog_view *view = calloc(1, sizeof(*view));
365 if (view == NULL)
366 return NULL;
368 view->type = type;
369 view->lines = LINES;
370 view->cols = COLS;
371 view->nlines = nlines ? nlines : LINES - begin_y;
372 view->ncols = ncols ? ncols : COLS - begin_x;
373 view->begin_y = begin_y;
374 view->begin_x = begin_x;
375 view->window = newwin(nlines, ncols, begin_y, begin_x);
376 if (view->window == NULL) {
377 view_close(view);
378 return NULL;
380 view->panel = new_panel(view->window);
381 if (view->panel == NULL ||
382 set_panel_userptr(view->panel, view) != OK) {
383 view_close(view);
384 return NULL;
387 keypad(view->window, TRUE);
388 return view;
391 static int
392 view_split_begin_x(int begin_x)
394 if (begin_x > 0 || COLS < 120)
395 return 0;
396 return (COLS - MAX(COLS / 2, 80));
399 static const struct got_error *view_resize(struct tog_view *);
401 static const struct got_error *
402 view_splitscreen(struct tog_view *view)
404 const struct got_error *err = NULL;
406 view->begin_y = 0;
407 view->begin_x = view_split_begin_x(0);
408 view->nlines = LINES;
409 view->ncols = COLS - view->begin_x;
410 view->lines = LINES;
411 view->cols = COLS;
412 err = view_resize(view);
413 if (err)
414 return err;
416 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
417 return got_error_from_errno("mvwin");
419 return NULL;
422 static const struct got_error *
423 view_fullscreen(struct tog_view *view)
425 const struct got_error *err = NULL;
427 view->begin_x = 0;
428 view->begin_y = 0;
429 view->nlines = LINES;
430 view->ncols = COLS;
431 view->lines = LINES;
432 view->cols = COLS;
433 err = view_resize(view);
434 if (err)
435 return err;
437 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
438 return got_error_from_errno("mvwin");
440 return NULL;
443 static int
444 view_is_parent_view(struct tog_view *view)
446 return view->parent == NULL;
449 static const struct got_error *
450 view_resize(struct tog_view *view)
452 int nlines, ncols;
454 if (view->lines > LINES)
455 nlines = view->nlines - (view->lines - LINES);
456 else
457 nlines = view->nlines + (LINES - view->lines);
459 if (view->cols > COLS)
460 ncols = view->ncols - (view->cols - COLS);
461 else
462 ncols = view->ncols + (COLS - view->cols);
464 if (wresize(view->window, nlines, ncols) == ERR)
465 return got_error_from_errno("wresize");
466 if (replace_panel(view->panel, view->window) == ERR)
467 return got_error_from_errno("replace_panel");
468 wclear(view->window);
470 view->nlines = nlines;
471 view->ncols = ncols;
472 view->lines = LINES;
473 view->cols = COLS;
475 if (view->child) {
476 view->child->begin_x = view_split_begin_x(view->begin_x);
477 if (view->child->begin_x == 0) {
478 view_fullscreen(view->child);
479 if (view->child->focussed)
480 show_panel(view->child->panel);
481 else
482 show_panel(view->panel);
483 } else {
484 view_splitscreen(view->child);
485 show_panel(view->child->panel);
489 return NULL;
492 static const struct got_error *
493 view_close_child(struct tog_view *view)
495 const struct got_error *err = NULL;
497 if (view->child == NULL)
498 return NULL;
500 err = view_close(view->child);
501 view->child = NULL;
502 return err;
505 static const struct got_error *
506 view_set_child(struct tog_view *view, struct tog_view *child)
508 const struct got_error *err = NULL;
510 view->child = child;
511 child->parent = view;
512 return err;
515 static int
516 view_is_splitscreen(struct tog_view *view)
518 return view->begin_x > 0;
521 static void
522 tog_resizeterm(void)
524 int cols, lines;
525 struct winsize size;
527 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
528 cols = 80; /* Default */
529 lines = 24;
530 } else {
531 cols = size.ws_col;
532 lines = size.ws_row;
534 resize_term(lines, cols);
537 static const struct got_error *
538 view_search_start(struct tog_view *view)
540 const struct got_error *err = NULL;
541 char pattern[1024];
542 int ret;
543 int begin_x = 0;
545 if (view->nlines < 1)
546 return NULL;
548 if (!view_is_parent_view(view))
549 begin_x = view_split_begin_x(view->begin_x);
550 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
551 begin_x, "/");
552 wclrtoeol(view->window);
554 nocbreak();
555 echo();
556 ret = wgetnstr(view->window, pattern, sizeof(pattern));
557 cbreak();
558 noecho();
559 if (ret == ERR)
560 return NULL;
562 if (view->searching) {
563 regfree(&view->regex);
564 view->searching = 0;
567 if (regcomp(&view->regex, pattern,
568 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
569 err = view->search_start(view);
570 if (err) {
571 regfree(&view->regex);
572 return err;
574 view->searching = TOG_SEARCH_FORWARD;
575 view->search_next_done = 0;
576 view->search_next(view);
579 return NULL;
582 static const struct got_error *
583 view_input(struct tog_view **new, struct tog_view **dead,
584 struct tog_view **focus, int *done, struct tog_view *view,
585 struct tog_view_list_head *views)
587 const struct got_error *err = NULL;
588 struct tog_view *v;
589 int ch, errcode;
591 *new = NULL;
592 *dead = NULL;
593 *focus = NULL;
595 if (view->searching && !view->search_next_done) {
596 errcode = pthread_mutex_unlock(&tog_mutex);
597 if (errcode)
598 return got_error_set_errno(errcode,
599 "pthread_mutex_unlock");
600 pthread_yield();
601 errcode = pthread_mutex_lock(&tog_mutex);
602 if (errcode)
603 return got_error_set_errno(errcode,
604 "pthread_mutex_lock");
605 view->search_next(view);
606 return NULL;
609 nodelay(stdscr, FALSE);
610 /* Allow threads to make progress while we are waiting for input. */
611 errcode = pthread_mutex_unlock(&tog_mutex);
612 if (errcode)
613 return got_error_set_errno(errcode, "pthread_mutex_unlock");
614 ch = wgetch(view->window);
615 errcode = pthread_mutex_lock(&tog_mutex);
616 if (errcode)
617 return got_error_set_errno(errcode, "pthread_mutex_lock");
618 nodelay(stdscr, TRUE);
620 if (tog_sigwinch_received) {
621 tog_resizeterm();
622 tog_sigwinch_received = 0;
623 TAILQ_FOREACH(v, views, entry) {
624 err = view_resize(v);
625 if (err)
626 return err;
627 err = v->input(new, dead, focus, v, KEY_RESIZE);
628 if (err)
629 return err;
633 switch (ch) {
634 case ERR:
635 break;
636 case '\t':
637 if (view->child) {
638 *focus = view->child;
639 view->child_focussed = 1;
640 } else if (view->parent) {
641 *focus = view->parent;
642 view->parent->child_focussed = 0;
644 break;
645 case 'q':
646 err = view->input(new, dead, focus, view, ch);
647 *dead = view;
648 break;
649 case 'Q':
650 *done = 1;
651 break;
652 case 'f':
653 if (view_is_parent_view(view)) {
654 if (view->child == NULL)
655 break;
656 if (view_is_splitscreen(view->child)) {
657 *focus = view->child;
658 view->child_focussed = 1;
659 err = view_fullscreen(view->child);
660 } else
661 err = view_splitscreen(view->child);
662 if (err)
663 break;
664 err = view->child->input(new, dead, focus,
665 view->child, KEY_RESIZE);
666 } else {
667 if (view_is_splitscreen(view)) {
668 *focus = view;
669 view->parent->child_focussed = 1;
670 err = view_fullscreen(view);
671 } else {
672 err = view_splitscreen(view);
674 if (err)
675 break;
676 err = view->input(new, dead, focus, view,
677 KEY_RESIZE);
679 break;
680 case KEY_RESIZE:
681 break;
682 case '/':
683 if (view->search_start)
684 view_search_start(view);
685 else
686 err = view->input(new, dead, focus, view, ch);
687 break;
688 case 'N':
689 case 'n':
690 if (view->search_next && view->searching) {
691 view->searching = (ch == 'n' ?
692 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
693 view->search_next_done = 0;
694 view->search_next(view);
695 } else
696 err = view->input(new, dead, focus, view, ch);
697 break;
698 default:
699 err = view->input(new, dead, focus, view, ch);
700 break;
703 return err;
706 void
707 view_vborder(struct tog_view *view)
709 PANEL *panel;
710 struct tog_view *view_above;
712 if (view->parent)
713 return view_vborder(view->parent);
715 panel = panel_above(view->panel);
716 if (panel == NULL)
717 return;
719 view_above = panel_userptr(panel);
720 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
721 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
724 int
725 view_needs_focus_indication(struct tog_view *view)
727 if (view_is_parent_view(view)) {
728 if (view->child == NULL || view->child_focussed)
729 return 0;
730 if (!view_is_splitscreen(view->child))
731 return 0;
732 } else if (!view_is_splitscreen(view))
733 return 0;
735 return view->focussed;
738 static const struct got_error *
739 view_loop(struct tog_view *view)
741 const struct got_error *err = NULL;
742 struct tog_view_list_head views;
743 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
744 int fast_refresh = 10;
745 int done = 0, errcode;
747 errcode = pthread_mutex_lock(&tog_mutex);
748 if (errcode)
749 return got_error_set_errno(errcode, "pthread_mutex_lock");
751 TAILQ_INIT(&views);
752 TAILQ_INSERT_HEAD(&views, view, entry);
754 main_view = view;
755 view->focussed = 1;
756 err = view->show(view);
757 if (err)
758 return err;
759 update_panels();
760 doupdate();
761 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
762 /* Refresh fast during initialization, then become slower. */
763 if (fast_refresh && fast_refresh-- == 0)
764 halfdelay(10); /* switch to once per second */
766 err = view_input(&new_view, &dead_view, &focus_view, &done,
767 view, &views);
768 if (err)
769 break;
770 if (dead_view) {
771 struct tog_view *prev = NULL;
773 if (view_is_parent_view(dead_view))
774 prev = TAILQ_PREV(dead_view,
775 tog_view_list_head, entry);
776 else if (view->parent != dead_view)
777 prev = view->parent;
779 if (dead_view->parent)
780 dead_view->parent->child = NULL;
781 else
782 TAILQ_REMOVE(&views, dead_view, entry);
784 err = view_close(dead_view);
785 if (err || (dead_view == main_view && new_view == NULL))
786 goto done;
788 if (view == dead_view) {
789 if (focus_view)
790 view = focus_view;
791 else if (prev)
792 view = prev;
793 else if (!TAILQ_EMPTY(&views))
794 view = TAILQ_LAST(&views,
795 tog_view_list_head);
796 else
797 view = NULL;
798 if (view) {
799 if (view->child && view->child_focussed)
800 focus_view = view->child;
801 else
802 focus_view = view;
806 if (new_view) {
807 struct tog_view *v, *t;
808 /* Only allow one parent view per type. */
809 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
810 if (v->type != new_view->type)
811 continue;
812 TAILQ_REMOVE(&views, v, entry);
813 err = view_close(v);
814 if (err)
815 goto done;
816 break;
818 TAILQ_INSERT_TAIL(&views, new_view, entry);
819 view = new_view;
820 if (focus_view == NULL)
821 focus_view = new_view;
823 if (focus_view) {
824 show_panel(focus_view->panel);
825 if (view)
826 view->focussed = 0;
827 focus_view->focussed = 1;
828 view = focus_view;
829 if (new_view)
830 show_panel(new_view->panel);
831 if (view->child && view_is_splitscreen(view->child))
832 show_panel(view->child->panel);
834 if (view) {
835 if (focus_view == NULL) {
836 view->focussed = 1;
837 show_panel(view->panel);
838 if (view->child && view_is_splitscreen(view->child))
839 show_panel(view->child->panel);
840 focus_view = view;
842 if (view->parent) {
843 err = view->parent->show(view->parent);
844 if (err)
845 goto done;
847 err = view->show(view);
848 if (err)
849 goto done;
850 if (view->child) {
851 err = view->child->show(view->child);
852 if (err)
853 goto done;
855 update_panels();
856 doupdate();
859 done:
860 while (!TAILQ_EMPTY(&views)) {
861 view = TAILQ_FIRST(&views);
862 TAILQ_REMOVE(&views, view, entry);
863 view_close(view);
866 errcode = pthread_mutex_unlock(&tog_mutex);
867 if (errcode && err == NULL)
868 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
870 return err;
873 __dead static void
874 usage_log(void)
876 endwin();
877 fprintf(stderr,
878 "usage: %s log [-c commit] [-r repository-path] [path]\n",
879 getprogname());
880 exit(1);
883 /* Create newly allocated wide-character string equivalent to a byte string. */
884 static const struct got_error *
885 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
887 char *vis = NULL;
888 const struct got_error *err = NULL;
890 *ws = NULL;
891 *wlen = mbstowcs(NULL, s, 0);
892 if (*wlen == (size_t)-1) {
893 int vislen;
894 if (errno != EILSEQ)
895 return got_error_from_errno("mbstowcs");
897 /* byte string invalid in current encoding; try to "fix" it */
898 err = got_mbsavis(&vis, &vislen, s);
899 if (err)
900 return err;
901 *wlen = mbstowcs(NULL, vis, 0);
902 if (*wlen == (size_t)-1) {
903 err = got_error_from_errno("mbstowcs"); /* give up */
904 goto done;
908 *ws = calloc(*wlen + 1, sizeof(*ws));
909 if (*ws == NULL) {
910 err = got_error_from_errno("calloc");
911 goto done;
914 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
915 err = got_error_from_errno("mbstowcs");
916 done:
917 free(vis);
918 if (err) {
919 free(*ws);
920 *ws = NULL;
921 *wlen = 0;
923 return err;
926 /* Format a line for display, ensuring that it won't overflow a width limit. */
927 static const struct got_error *
928 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
930 const struct got_error *err = NULL;
931 int cols = 0;
932 wchar_t *wline = NULL;
933 size_t wlen;
934 int i;
936 *wlinep = NULL;
937 *widthp = 0;
939 err = mbs2ws(&wline, &wlen, line);
940 if (err)
941 return err;
943 i = 0;
944 while (i < wlen && cols < wlimit) {
945 int width = wcwidth(wline[i]);
946 switch (width) {
947 case 0:
948 i++;
949 break;
950 case 1:
951 case 2:
952 if (cols + width <= wlimit)
953 cols += width;
954 i++;
955 break;
956 case -1:
957 if (wline[i] == L'\t')
958 cols += TABSIZE - ((cols + 1) % TABSIZE);
959 i++;
960 break;
961 default:
962 err = got_error_from_errno("wcwidth");
963 goto done;
966 wline[i] = L'\0';
967 if (widthp)
968 *widthp = cols;
969 done:
970 if (err)
971 free(wline);
972 else
973 *wlinep = wline;
974 return err;
977 static const struct got_error*
978 build_refs_str(char **refs_str, struct got_reflist_head *refs,
979 struct got_object_id *id, struct got_repository *repo)
981 static const struct got_error *err = NULL;
982 struct got_reflist_entry *re;
983 char *s;
984 const char *name;
986 *refs_str = NULL;
988 SIMPLEQ_FOREACH(re, refs, entry) {
989 struct got_tag_object *tag = NULL;
990 int cmp;
992 name = got_ref_get_name(re->ref);
993 if (strcmp(name, GOT_REF_HEAD) == 0)
994 continue;
995 if (strncmp(name, "refs/", 5) == 0)
996 name += 5;
997 if (strncmp(name, "got/", 4) == 0)
998 continue;
999 if (strncmp(name, "heads/", 6) == 0)
1000 name += 6;
1001 if (strncmp(name, "remotes/", 8) == 0)
1002 name += 8;
1003 if (strncmp(name, "tags/", 5) == 0) {
1004 err = got_object_open_as_tag(&tag, repo, re->id);
1005 if (err) {
1006 if (err->code != GOT_ERR_OBJ_TYPE)
1007 break;
1008 /* Ref points at something other than a tag. */
1009 err = NULL;
1010 tag = NULL;
1013 cmp = got_object_id_cmp(tag ?
1014 got_object_tag_get_object_id(tag) : re->id, id);
1015 if (tag)
1016 got_object_tag_close(tag);
1017 if (cmp != 0)
1018 continue;
1019 s = *refs_str;
1020 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1021 s ? ", " : "", name) == -1) {
1022 err = got_error_from_errno("asprintf");
1023 free(s);
1024 *refs_str = NULL;
1025 break;
1027 free(s);
1030 return err;
1033 static const struct got_error *
1034 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1036 char *smallerthan, *at;
1038 smallerthan = strchr(author, '<');
1039 if (smallerthan && smallerthan[1] != '\0')
1040 author = smallerthan + 1;
1041 at = strchr(author, '@');
1042 if (at)
1043 *at = '\0';
1044 return format_line(wauthor, author_width, author, limit);
1047 static const struct got_error *
1048 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1049 struct got_object_id *id, struct got_reflist_head *refs,
1050 int author_display_cols)
1052 const struct got_error *err = NULL;
1053 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1054 char *logmsg0 = NULL, *logmsg = NULL;
1055 char *author = NULL;
1056 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1057 int author_width, logmsg_width;
1058 char *newline, *line = NULL;
1059 int col, limit;
1060 static const size_t date_display_cols = 9;
1061 const int avail = view->ncols;
1062 struct tm tm;
1063 time_t committer_time;
1065 committer_time = got_object_commit_get_committer_time(commit);
1066 if (localtime_r(&committer_time, &tm) == NULL)
1067 return got_error_from_errno("localtime_r");
1068 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1069 >= sizeof(datebuf))
1070 return got_error(GOT_ERR_NO_SPACE);
1072 if (avail < date_display_cols)
1073 limit = MIN(sizeof(datebuf) - 1, avail);
1074 else
1075 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1076 waddnstr(view->window, datebuf, limit);
1077 col = limit + 1;
1078 if (col > avail)
1079 goto done;
1081 author = strdup(got_object_commit_get_author(commit));
1082 if (author == NULL) {
1083 err = got_error_from_errno("strdup");
1084 goto done;
1086 err = format_author(&wauthor, &author_width, author, avail - col);
1087 if (err)
1088 goto done;
1089 waddwstr(view->window, wauthor);
1090 col += author_width;
1091 while (col <= avail && author_width < author_display_cols + 2) {
1092 waddch(view->window, ' ');
1093 col++;
1094 author_width++;
1096 if (col > avail)
1097 goto done;
1099 err = got_object_commit_get_logmsg(&logmsg0, commit);
1100 if (err)
1101 goto done;
1102 logmsg = logmsg0;
1103 while (*logmsg == '\n')
1104 logmsg++;
1105 newline = strchr(logmsg, '\n');
1106 if (newline)
1107 *newline = '\0';
1108 limit = avail - col;
1109 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1110 if (err)
1111 goto done;
1112 waddwstr(view->window, wlogmsg);
1113 col += logmsg_width;
1114 while (col <= avail) {
1115 waddch(view->window, ' ');
1116 col++;
1118 done:
1119 free(logmsg0);
1120 free(wlogmsg);
1121 free(author);
1122 free(wauthor);
1123 free(line);
1124 return err;
1127 static struct commit_queue_entry *
1128 alloc_commit_queue_entry(struct got_commit_object *commit,
1129 struct got_object_id *id)
1131 struct commit_queue_entry *entry;
1133 entry = calloc(1, sizeof(*entry));
1134 if (entry == NULL)
1135 return NULL;
1137 entry->id = id;
1138 entry->commit = commit;
1139 return entry;
1142 static void
1143 pop_commit(struct commit_queue *commits)
1145 struct commit_queue_entry *entry;
1147 entry = TAILQ_FIRST(&commits->head);
1148 TAILQ_REMOVE(&commits->head, entry, entry);
1149 got_object_commit_close(entry->commit);
1150 commits->ncommits--;
1151 /* Don't free entry->id! It is owned by the commit graph. */
1152 free(entry);
1155 static void
1156 free_commits(struct commit_queue *commits)
1158 while (!TAILQ_EMPTY(&commits->head))
1159 pop_commit(commits);
1162 static const struct got_error *
1163 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1164 int minqueue, struct got_repository *repo, const char *path)
1166 const struct got_error *err = NULL;
1167 int nqueued = 0;
1170 * We keep all commits open throughout the lifetime of the log
1171 * view in order to avoid having to re-fetch commits from disk
1172 * while updating the display.
1174 while (nqueued < minqueue) {
1175 struct got_object_id *id;
1176 struct got_commit_object *commit;
1177 struct commit_queue_entry *entry;
1178 int errcode;
1180 err = got_commit_graph_iter_next(&id, graph);
1181 if (err) {
1182 if (err->code != GOT_ERR_ITER_NEED_MORE)
1183 break;
1184 err = got_commit_graph_fetch_commits(graph,
1185 minqueue, repo, NULL, NULL);
1186 if (err)
1187 return err;
1188 continue;
1191 if (id == NULL)
1192 break;
1194 err = got_object_open_as_commit(&commit, repo, id);
1195 if (err)
1196 break;
1197 entry = alloc_commit_queue_entry(commit, id);
1198 if (entry == NULL) {
1199 err = got_error_from_errno("alloc_commit_queue_entry");
1200 break;
1203 errcode = pthread_mutex_lock(&tog_mutex);
1204 if (errcode) {
1205 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1206 break;
1209 entry->idx = commits->ncommits;
1210 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1211 nqueued++;
1212 commits->ncommits++;
1214 errcode = pthread_mutex_unlock(&tog_mutex);
1215 if (errcode && err == NULL)
1216 err = got_error_set_errno(errcode,
1217 "pthread_mutex_unlock");
1220 return err;
1223 static const struct got_error *
1224 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1225 struct got_repository *repo)
1227 const struct got_error *err = NULL;
1228 struct got_reference *head_ref;
1230 *head_id = NULL;
1232 err = got_ref_open(&head_ref, repo, branch_name, 0);
1233 if (err)
1234 return err;
1236 err = got_ref_resolve(head_id, repo, head_ref);
1237 got_ref_close(head_ref);
1238 if (err) {
1239 *head_id = NULL;
1240 return err;
1243 return NULL;
1246 static const struct got_error *
1247 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1248 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1249 struct commit_queue *commits, int selected_idx, int limit,
1250 struct got_reflist_head *refs, const char *path, int commits_needed)
1252 const struct got_error *err = NULL;
1253 struct tog_log_view_state *s = &view->state.log;
1254 struct commit_queue_entry *entry;
1255 int width;
1256 int ncommits, author_cols = 10;
1257 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1258 char *refs_str = NULL;
1259 wchar_t *wline;
1261 entry = first;
1262 ncommits = 0;
1263 while (entry) {
1264 if (ncommits == selected_idx) {
1265 *selected = entry;
1266 break;
1268 entry = TAILQ_NEXT(entry, entry);
1269 ncommits++;
1272 if (*selected && !(view->searching && view->search_next_done == 0)) {
1273 err = got_object_id_str(&id_str, (*selected)->id);
1274 if (err)
1275 return err;
1276 if (refs) {
1277 err = build_refs_str(&refs_str, refs, (*selected)->id,
1278 s->repo);
1279 if (err)
1280 goto done;
1284 if (commits_needed == 0)
1285 halfdelay(10); /* disable fast refresh */
1287 if (asprintf(&ncommits_str, " [%d/%d] %s",
1288 entry ? entry->idx + 1 : 0, commits->ncommits,
1289 commits_needed > 0 ?
1290 (view->searching && view->search_next_done == 0
1291 ? "searching..." : "loading... ") :
1292 (refs_str ? refs_str : "")) == -1) {
1293 err = got_error_from_errno("asprintf");
1294 goto done;
1297 if (path && strcmp(path, "/") != 0) {
1298 if (asprintf(&header, "commit %s %s%s",
1299 id_str ? id_str : "........................................",
1300 path, ncommits_str) == -1) {
1301 err = got_error_from_errno("asprintf");
1302 header = NULL;
1303 goto done;
1305 } else if (asprintf(&header, "commit %s%s",
1306 id_str ? id_str : "........................................",
1307 ncommits_str) == -1) {
1308 err = got_error_from_errno("asprintf");
1309 header = NULL;
1310 goto done;
1312 err = format_line(&wline, &width, header, view->ncols);
1313 if (err)
1314 goto done;
1316 werase(view->window);
1318 if (view_needs_focus_indication(view))
1319 wstandout(view->window);
1320 waddwstr(view->window, wline);
1321 while (width < view->ncols) {
1322 waddch(view->window, ' ');
1323 width++;
1325 if (view_needs_focus_indication(view))
1326 wstandend(view->window);
1327 free(wline);
1328 if (limit <= 1)
1329 goto done;
1331 /* Grow author column size if necessary. */
1332 entry = first;
1333 ncommits = 0;
1334 while (entry) {
1335 char *author;
1336 wchar_t *wauthor;
1337 int width;
1338 if (ncommits >= limit - 1)
1339 break;
1340 author = strdup(got_object_commit_get_author(entry->commit));
1341 if (author == NULL) {
1342 err = got_error_from_errno("strdup");
1343 goto done;
1345 err = format_author(&wauthor, &width, author, COLS);
1346 if (author_cols < width)
1347 author_cols = width;
1348 free(wauthor);
1349 free(author);
1350 entry = TAILQ_NEXT(entry, entry);
1353 entry = first;
1354 *last = first;
1355 ncommits = 0;
1356 while (entry) {
1357 if (ncommits >= limit - 1)
1358 break;
1359 if (ncommits == selected_idx)
1360 wstandout(view->window);
1361 err = draw_commit(view, entry->commit, entry->id, refs,
1362 author_cols);
1363 if (ncommits == selected_idx)
1364 wstandend(view->window);
1365 if (err)
1366 goto done;
1367 ncommits++;
1368 *last = entry;
1369 entry = TAILQ_NEXT(entry, entry);
1372 view_vborder(view);
1373 done:
1374 free(id_str);
1375 free(refs_str);
1376 free(ncommits_str);
1377 free(header);
1378 return err;
1381 static void
1382 scroll_up(struct tog_view *view,
1383 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1384 struct commit_queue *commits)
1386 struct commit_queue_entry *entry;
1387 int nscrolled = 0;
1389 entry = TAILQ_FIRST(&commits->head);
1390 if (*first_displayed_entry == entry)
1391 return;
1393 entry = *first_displayed_entry;
1394 while (entry && nscrolled < maxscroll) {
1395 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1396 if (entry) {
1397 *first_displayed_entry = entry;
1398 nscrolled++;
1403 static const struct got_error *
1404 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1405 pthread_cond_t *need_commits)
1407 int errcode;
1408 int max_wait = 20;
1410 halfdelay(1); /* fast refresh while loading commits */
1412 while (*commits_needed > 0) {
1413 if (*log_complete)
1414 break;
1416 /* Wake the log thread. */
1417 errcode = pthread_cond_signal(need_commits);
1418 if (errcode)
1419 return got_error_set_errno(errcode,
1420 "pthread_cond_signal");
1421 errcode = pthread_mutex_unlock(&tog_mutex);
1422 if (errcode)
1423 return got_error_set_errno(errcode,
1424 "pthread_mutex_unlock");
1425 pthread_yield();
1426 errcode = pthread_mutex_lock(&tog_mutex);
1427 if (errcode)
1428 return got_error_set_errno(errcode,
1429 "pthread_mutex_lock");
1431 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1433 * Thread is not done yet; lose a key press
1434 * and let the user retry... this way the GUI
1435 * remains interactive while logging deep paths
1436 * with few commits in history.
1438 return NULL;
1442 return NULL;
1445 static const struct got_error *
1446 scroll_down(struct tog_view *view,
1447 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1448 struct commit_queue_entry **last_displayed_entry,
1449 struct commit_queue *commits, int *log_complete, int *commits_needed,
1450 pthread_cond_t *need_commits)
1452 const struct got_error *err = NULL;
1453 struct commit_queue_entry *pentry;
1454 int nscrolled = 0;
1456 if (*last_displayed_entry == NULL)
1457 return NULL;
1459 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1460 if (pentry == NULL && !*log_complete) {
1462 * Ask the log thread for required amount of commits
1463 * plus some amount of pre-fetching.
1465 (*commits_needed) += maxscroll + 20;
1466 err = trigger_log_thread(0, commits_needed, log_complete,
1467 need_commits);
1468 if (err)
1469 return err;
1472 do {
1473 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1474 if (pentry == NULL)
1475 break;
1477 *last_displayed_entry = pentry;
1479 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1480 if (pentry == NULL)
1481 break;
1482 *first_displayed_entry = pentry;
1483 } while (++nscrolled < maxscroll);
1485 return err;
1488 static const struct got_error *
1489 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1490 struct got_commit_object *commit, struct got_object_id *commit_id,
1491 struct tog_view *log_view, struct got_reflist_head *refs,
1492 struct got_repository *repo)
1494 const struct got_error *err;
1495 struct got_object_qid *parent_id;
1496 struct tog_view *diff_view;
1498 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1499 if (diff_view == NULL)
1500 return got_error_from_errno("view_open");
1502 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1503 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1504 commit_id, log_view, refs, repo);
1505 if (err == NULL)
1506 *new_view = diff_view;
1507 return err;
1510 static const struct got_error *
1511 tree_view_visit_subtree(struct got_tree_object *subtree,
1512 struct tog_tree_view_state *s)
1514 struct tog_parent_tree *parent;
1516 parent = calloc(1, sizeof(*parent));
1517 if (parent == NULL)
1518 return got_error_from_errno("calloc");
1520 parent->tree = s->tree;
1521 parent->first_displayed_entry = s->first_displayed_entry;
1522 parent->selected_entry = s->selected_entry;
1523 parent->selected = s->selected;
1524 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1525 s->tree = subtree;
1526 s->entries = got_object_tree_get_entries(s->tree);
1527 s->selected = 0;
1528 s->first_displayed_entry = NULL;
1529 return NULL;
1533 static const struct got_error *
1534 browse_commit_tree(struct tog_view **new_view, int begin_x,
1535 struct commit_queue_entry *entry, const char *path,
1536 struct got_reflist_head *refs, struct got_repository *repo)
1538 const struct got_error *err = NULL;
1539 struct got_tree_object *tree;
1540 struct got_tree_entry *te;
1541 struct tog_tree_view_state *s;
1542 struct tog_view *tree_view;
1543 char *slash, *subpath = NULL;
1544 const char *p;
1546 err = got_object_open_as_tree(&tree, repo,
1547 got_object_commit_get_tree_id(entry->commit));
1548 if (err)
1549 return err;
1551 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1552 if (tree_view == NULL)
1553 return got_error_from_errno("view_open");
1555 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1556 if (err) {
1557 got_object_tree_close(tree);
1558 return err;
1560 s = &tree_view->state.tree;
1562 *new_view = tree_view;
1564 /* Walk the path and open corresponding tree objects. */
1565 p = path;
1566 while (p[0] == '/')
1567 p++;
1568 while (*p) {
1569 struct got_object_id *tree_id;
1571 /* Ensure the correct subtree entry is selected. */
1572 slash = strchr(p, '/');
1573 if (slash == NULL)
1574 slash = strchr(p, '\0');
1575 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1576 if (strncmp(p, te->name, slash - p) == 0) {
1577 s->selected_entry = te;
1578 break;
1580 s->selected++;
1582 if (s->selected_entry == NULL) {
1583 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1584 break;
1586 if (s->tree != s->root)
1587 s->selected++; /* skip '..' */
1589 if (!S_ISDIR(s->selected_entry->mode)) {
1590 /* Jump to this file's entry. */
1591 s->first_displayed_entry = s->selected_entry;
1592 s->selected = 0;
1593 break;
1596 slash = strchr(p, '/');
1597 if (slash)
1598 subpath = strndup(path, slash - path);
1599 else
1600 subpath = strdup(path);
1601 if (subpath == NULL) {
1602 err = got_error_from_errno("strdup");
1603 break;
1606 err = got_object_id_by_path(&tree_id, repo, entry->id,
1607 subpath);
1608 if (err)
1609 break;
1611 err = got_object_open_as_tree(&tree, repo, tree_id);
1612 free(tree_id);
1613 if (err)
1614 break;
1616 err = tree_view_visit_subtree(tree, s);
1617 if (err) {
1618 got_object_tree_close(tree);
1619 break;
1621 if (slash == NULL)
1622 break;
1623 free(subpath);
1624 subpath = NULL;
1625 p = slash;
1628 free(subpath);
1629 return err;
1632 static void *
1633 log_thread(void *arg)
1635 const struct got_error *err = NULL;
1636 int errcode = 0;
1637 struct tog_log_thread_args *a = arg;
1638 int done = 0;
1640 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1641 NULL, NULL);
1642 if (err)
1643 return (void *)err;
1645 while (!done && !err && !tog_sigpipe_received) {
1646 err = queue_commits(a->graph, a->commits, 1, a->repo,
1647 a->in_repo_path);
1648 if (err) {
1649 if (err->code != GOT_ERR_ITER_COMPLETED)
1650 return (void *)err;
1651 err = NULL;
1652 done = 1;
1653 } else if (a->commits_needed > 0)
1654 a->commits_needed--;
1656 errcode = pthread_mutex_lock(&tog_mutex);
1657 if (errcode) {
1658 err = got_error_set_errno(errcode,
1659 "pthread_mutex_lock");
1660 break;
1661 } else if (*a->quit)
1662 done = 1;
1663 else if (*a->first_displayed_entry == NULL) {
1664 *a->first_displayed_entry =
1665 TAILQ_FIRST(&a->commits->head);
1666 *a->selected_entry = *a->first_displayed_entry;
1669 if (done)
1670 a->commits_needed = 0;
1671 else if (a->commits_needed == 0) {
1672 errcode = pthread_cond_wait(&a->need_commits,
1673 &tog_mutex);
1674 if (errcode)
1675 err = got_error_set_errno(errcode,
1676 "pthread_cond_wait");
1679 errcode = pthread_mutex_unlock(&tog_mutex);
1680 if (errcode && err == NULL)
1681 err = got_error_set_errno(errcode,
1682 "pthread_mutex_unlock");
1684 a->log_complete = 1;
1685 return (void *)err;
1688 static const struct got_error *
1689 stop_log_thread(struct tog_log_view_state *s)
1691 const struct got_error *err = NULL;
1692 int errcode;
1694 if (s->thread) {
1695 s->quit = 1;
1696 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1697 if (errcode)
1698 return got_error_set_errno(errcode,
1699 "pthread_cond_signal");
1700 errcode = pthread_mutex_unlock(&tog_mutex);
1701 if (errcode)
1702 return got_error_set_errno(errcode,
1703 "pthread_mutex_unlock");
1704 errcode = pthread_join(s->thread, (void **)&err);
1705 if (errcode)
1706 return got_error_set_errno(errcode, "pthread_join");
1707 errcode = pthread_mutex_lock(&tog_mutex);
1708 if (errcode)
1709 return got_error_set_errno(errcode,
1710 "pthread_mutex_lock");
1711 s->thread = NULL;
1714 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1715 if (errcode && err == NULL)
1716 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1718 if (s->thread_args.repo) {
1719 got_repo_close(s->thread_args.repo);
1720 s->thread_args.repo = NULL;
1723 if (s->thread_args.graph) {
1724 got_commit_graph_close(s->thread_args.graph);
1725 s->thread_args.graph = NULL;
1728 return err;
1731 static const struct got_error *
1732 close_log_view(struct tog_view *view)
1734 const struct got_error *err = NULL;
1735 struct tog_log_view_state *s = &view->state.log;
1737 err = stop_log_thread(s);
1738 free_commits(&s->commits);
1739 free(s->in_repo_path);
1740 s->in_repo_path = NULL;
1741 free(s->start_id);
1742 s->start_id = NULL;
1743 return err;
1746 static const struct got_error *
1747 search_start_log_view(struct tog_view *view)
1749 struct tog_log_view_state *s = &view->state.log;
1751 s->matched_entry = NULL;
1752 s->search_entry = NULL;
1753 return NULL;
1756 static int
1757 match_commit(struct got_commit_object *commit, const char *id_str,
1758 const char *logmsg, regex_t *regex)
1760 regmatch_t regmatch;
1762 if (regexec(regex, got_object_commit_get_author(commit), 1,
1763 &regmatch, 0) == 0 ||
1764 regexec(regex, got_object_commit_get_committer(commit), 1,
1765 &regmatch, 0) == 0 ||
1766 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1767 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1768 return 1;
1770 return 0;
1773 static const struct got_error *
1774 search_next_log_view(struct tog_view *view)
1776 const struct got_error *err = NULL;
1777 struct tog_log_view_state *s = &view->state.log;
1778 struct commit_queue_entry *entry;
1780 if (!view->searching) {
1781 view->search_next_done = 1;
1782 return NULL;
1785 if (s->search_entry) {
1786 if (wgetch(view->window) == KEY_BACKSPACE) {
1787 view->search_next_done = 1;
1788 return NULL;
1790 if (view->searching == TOG_SEARCH_FORWARD)
1791 entry = TAILQ_NEXT(s->search_entry, entry);
1792 else
1793 entry = TAILQ_PREV(s->search_entry,
1794 commit_queue_head, entry);
1795 } else if (s->matched_entry) {
1796 if (view->searching == TOG_SEARCH_FORWARD)
1797 entry = TAILQ_NEXT(s->selected_entry, entry);
1798 else
1799 entry = TAILQ_PREV(s->selected_entry,
1800 commit_queue_head, entry);
1801 } else {
1802 if (view->searching == TOG_SEARCH_FORWARD)
1803 entry = TAILQ_FIRST(&s->commits.head);
1804 else
1805 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1808 while (1) {
1809 char *id_str, *logmsg;
1810 if (entry == NULL) {
1811 if (s->thread_args.log_complete ||
1812 view->searching == TOG_SEARCH_BACKWARD) {
1813 view->search_next_done = 1;
1814 return NULL;
1817 * Poke the log thread for more commits and return,
1818 * allowing the main loop to make progress. Search
1819 * will resume at s->search_entry once we come back.
1821 s->thread_args.commits_needed++;
1822 return trigger_log_thread(1,
1823 &s->thread_args.commits_needed,
1824 &s->thread_args.log_complete,
1825 &s->thread_args.need_commits);
1828 err = got_object_id_str(&id_str, entry->id);
1829 if (err)
1830 return err;
1832 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1833 if (err)
1834 return err;
1835 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1836 free(logmsg);
1837 view->search_next_done = 1;
1838 s->matched_entry = entry;
1839 free(id_str);
1840 break;
1842 free(logmsg);
1843 free(id_str);
1844 s->search_entry = entry;
1845 if (view->searching == TOG_SEARCH_FORWARD)
1846 entry = TAILQ_NEXT(entry, entry);
1847 else
1848 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1851 if (s->matched_entry) {
1852 int cur = s->selected_entry->idx;
1853 while (cur < s->matched_entry->idx) {
1854 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1855 if (err)
1856 return err;
1857 cur++;
1859 while (cur > s->matched_entry->idx) {
1860 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1861 if (err)
1862 return err;
1863 cur--;
1867 s->search_entry = NULL;
1869 return NULL;
1872 static const struct got_error *
1873 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1874 struct got_reflist_head *refs, struct got_repository *repo,
1875 const char *head_ref_name, const char *path, int check_disk)
1877 const struct got_error *err = NULL;
1878 struct tog_log_view_state *s = &view->state.log;
1879 struct got_repository *thread_repo = NULL;
1880 struct got_commit_graph *thread_graph = NULL;
1881 int errcode;
1883 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1884 if (err != NULL)
1885 goto done;
1887 /* The commit queue only contains commits being displayed. */
1888 TAILQ_INIT(&s->commits.head);
1889 s->commits.ncommits = 0;
1891 s->refs = refs;
1892 s->repo = repo;
1893 s->head_ref_name = head_ref_name;
1894 s->start_id = got_object_id_dup(start_id);
1895 if (s->start_id == NULL) {
1896 err = got_error_from_errno("got_object_id_dup");
1897 goto done;
1900 view->show = show_log_view;
1901 view->input = input_log_view;
1902 view->close = close_log_view;
1903 view->search_start = search_start_log_view;
1904 view->search_next = search_next_log_view;
1906 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1907 if (err)
1908 goto done;
1909 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1910 0, thread_repo);
1911 if (err)
1912 goto done;
1914 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1915 if (errcode) {
1916 err = got_error_set_errno(errcode, "pthread_cond_init");
1917 goto done;
1920 s->thread_args.commits_needed = view->nlines;
1921 s->thread_args.graph = thread_graph;
1922 s->thread_args.commits = &s->commits;
1923 s->thread_args.in_repo_path = s->in_repo_path;
1924 s->thread_args.start_id = s->start_id;
1925 s->thread_args.repo = thread_repo;
1926 s->thread_args.log_complete = 0;
1927 s->thread_args.quit = &s->quit;
1928 s->thread_args.view = view;
1929 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1930 s->thread_args.selected_entry = &s->selected_entry;
1931 done:
1932 if (err)
1933 close_log_view(view);
1934 return err;
1937 static const struct got_error *
1938 show_log_view(struct tog_view *view)
1940 struct tog_log_view_state *s = &view->state.log;
1942 if (s->thread == NULL) {
1943 int errcode = pthread_create(&s->thread, NULL, log_thread,
1944 &s->thread_args);
1945 if (errcode)
1946 return got_error_set_errno(errcode, "pthread_create");
1949 return draw_commits(view, &s->last_displayed_entry,
1950 &s->selected_entry, s->first_displayed_entry,
1951 &s->commits, s->selected, view->nlines, s->refs,
1952 s->in_repo_path, s->thread_args.commits_needed);
1955 static const struct got_error *
1956 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1957 struct tog_view **focus_view, struct tog_view *view, int ch)
1959 const struct got_error *err = NULL;
1960 struct tog_log_view_state *s = &view->state.log;
1961 char *parent_path, *in_repo_path = NULL;
1962 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1963 int begin_x = 0;
1964 struct got_object_id *start_id;
1966 switch (ch) {
1967 case 'q':
1968 s->quit = 1;
1969 break;
1970 case 'k':
1971 case KEY_UP:
1972 case '<':
1973 case ',':
1974 if (s->first_displayed_entry == NULL)
1975 break;
1976 if (s->selected > 0)
1977 s->selected--;
1978 else
1979 scroll_up(view, &s->first_displayed_entry, 1,
1980 &s->commits);
1981 break;
1982 case KEY_PPAGE:
1983 case CTRL('b'):
1984 if (s->first_displayed_entry == NULL)
1985 break;
1986 if (TAILQ_FIRST(&s->commits.head) ==
1987 s->first_displayed_entry) {
1988 s->selected = 0;
1989 break;
1991 scroll_up(view, &s->first_displayed_entry,
1992 view->nlines, &s->commits);
1993 break;
1994 case 'j':
1995 case KEY_DOWN:
1996 case '>':
1997 case '.':
1998 if (s->first_displayed_entry == NULL)
1999 break;
2000 if (s->selected < MIN(view->nlines - 2,
2001 s->commits.ncommits - 1)) {
2002 s->selected++;
2003 break;
2005 err = scroll_down(view, &s->first_displayed_entry, 1,
2006 &s->last_displayed_entry, &s->commits,
2007 &s->thread_args.log_complete,
2008 &s->thread_args.commits_needed,
2009 &s->thread_args.need_commits);
2010 break;
2011 case KEY_NPAGE:
2012 case CTRL('f'): {
2013 struct commit_queue_entry *first;
2014 first = s->first_displayed_entry;
2015 if (first == NULL)
2016 break;
2017 err = scroll_down(view, &s->first_displayed_entry,
2018 view->nlines, &s->last_displayed_entry,
2019 &s->commits, &s->thread_args.log_complete,
2020 &s->thread_args.commits_needed,
2021 &s->thread_args.need_commits);
2022 if (first == s->first_displayed_entry &&
2023 s->selected < MIN(view->nlines - 2,
2024 s->commits.ncommits - 1)) {
2025 /* can't scroll further down */
2026 s->selected = MIN(view->nlines - 2,
2027 s->commits.ncommits - 1);
2029 err = NULL;
2030 break;
2032 case KEY_RESIZE:
2033 if (s->selected > view->nlines - 2)
2034 s->selected = view->nlines - 2;
2035 if (s->selected > s->commits.ncommits - 1)
2036 s->selected = s->commits.ncommits - 1;
2037 break;
2038 case KEY_ENTER:
2039 case ' ':
2040 case '\r':
2041 if (s->selected_entry == NULL)
2042 break;
2043 if (view_is_parent_view(view))
2044 begin_x = view_split_begin_x(view->begin_x);
2045 err = open_diff_view_for_commit(&diff_view, begin_x,
2046 s->selected_entry->commit, s->selected_entry->id,
2047 view, s->refs, s->repo);
2048 if (err)
2049 break;
2050 if (view_is_parent_view(view)) {
2051 err = view_close_child(view);
2052 if (err)
2053 return err;
2054 err = view_set_child(view, diff_view);
2055 if (err) {
2056 view_close(diff_view);
2057 break;
2059 *focus_view = diff_view;
2060 view->child_focussed = 1;
2061 } else
2062 *new_view = diff_view;
2063 break;
2064 case 't':
2065 if (s->selected_entry == NULL)
2066 break;
2067 if (view_is_parent_view(view))
2068 begin_x = view_split_begin_x(view->begin_x);
2069 err = browse_commit_tree(&tree_view, begin_x,
2070 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2071 if (err)
2072 break;
2073 if (view_is_parent_view(view)) {
2074 err = view_close_child(view);
2075 if (err)
2076 return err;
2077 err = view_set_child(view, tree_view);
2078 if (err) {
2079 view_close(tree_view);
2080 break;
2082 *focus_view = tree_view;
2083 view->child_focussed = 1;
2084 } else
2085 *new_view = tree_view;
2086 break;
2087 case KEY_BACKSPACE:
2088 if (strcmp(s->in_repo_path, "/") == 0)
2089 break;
2090 parent_path = dirname(s->in_repo_path);
2091 if (parent_path && strcmp(parent_path, ".") != 0) {
2092 err = stop_log_thread(s);
2093 if (err)
2094 return err;
2095 lv = view_open(view->nlines, view->ncols,
2096 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2097 if (lv == NULL)
2098 return got_error_from_errno(
2099 "view_open");
2100 err = open_log_view(lv, s->start_id, s->refs,
2101 s->repo, s->head_ref_name, parent_path, 0);
2102 if (err)
2103 return err;;
2104 if (view_is_parent_view(view))
2105 *new_view = lv;
2106 else {
2107 view_set_child(view->parent, lv);
2108 *focus_view = lv;
2110 return NULL;
2112 break;
2113 case CTRL('l'):
2114 err = stop_log_thread(s);
2115 if (err)
2116 return err;
2117 lv = view_open(view->nlines, view->ncols,
2118 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2119 if (lv == NULL)
2120 return got_error_from_errno("view_open");
2121 err = get_head_commit_id(&start_id, s->head_ref_name ?
2122 s->head_ref_name : GOT_REF_HEAD, s->repo);
2123 if (err) {
2124 view_close(lv);
2125 return err;
2127 in_repo_path = strdup(s->in_repo_path);
2128 if (in_repo_path == NULL) {
2129 free(start_id);
2130 view_close(lv);
2131 return got_error_from_errno("strdup");
2133 err = open_log_view(lv, start_id, s->refs, s->repo,
2134 s->head_ref_name, in_repo_path, 0);
2135 if (err) {
2136 free(start_id);
2137 view_close(lv);
2138 return err;;
2140 *dead_view = view;
2141 *new_view = lv;
2142 break;
2143 default:
2144 break;
2147 return err;
2150 static const struct got_error *
2151 apply_unveil(const char *repo_path, const char *worktree_path)
2153 const struct got_error *error;
2155 #ifdef PROFILE
2156 if (unveil("gmon.out", "rwc") != 0)
2157 return got_error_from_errno2("unveil", "gmon.out");
2158 #endif
2159 if (repo_path && unveil(repo_path, "r") != 0)
2160 return got_error_from_errno2("unveil", repo_path);
2162 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2163 return got_error_from_errno2("unveil", worktree_path);
2165 if (unveil("/tmp", "rwc") != 0)
2166 return got_error_from_errno2("unveil", "/tmp");
2168 error = got_privsep_unveil_exec_helpers();
2169 if (error != NULL)
2170 return error;
2172 if (unveil(NULL, NULL) != 0)
2173 return got_error_from_errno("unveil");
2175 return NULL;
2178 static void
2179 init_curses(void)
2181 initscr();
2182 cbreak();
2183 halfdelay(1); /* Do fast refresh while initial view is loading. */
2184 noecho();
2185 nonl();
2186 intrflush(stdscr, FALSE);
2187 keypad(stdscr, TRUE);
2188 curs_set(0);
2189 signal(SIGWINCH, tog_sigwinch);
2190 signal(SIGPIPE, tog_sigpipe);
2193 static const struct got_error *
2194 cmd_log(int argc, char *argv[])
2196 const struct got_error *error;
2197 struct got_repository *repo = NULL;
2198 struct got_worktree *worktree = NULL;
2199 struct got_reflist_head refs;
2200 struct got_object_id *start_id = NULL;
2201 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2202 char *start_commit = NULL;
2203 int ch;
2204 struct tog_view *view;
2206 SIMPLEQ_INIT(&refs);
2208 #ifndef PROFILE
2209 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2210 NULL) == -1)
2211 err(1, "pledge");
2212 #endif
2214 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2215 switch (ch) {
2216 case 'c':
2217 start_commit = optarg;
2218 break;
2219 case 'r':
2220 repo_path = realpath(optarg, NULL);
2221 if (repo_path == NULL)
2222 err(1, "-r option");
2223 break;
2224 default:
2225 usage_log();
2226 /* NOTREACHED */
2230 argc -= optind;
2231 argv += optind;
2233 cwd = getcwd(NULL, 0);
2234 if (cwd == NULL) {
2235 error = got_error_from_errno("getcwd");
2236 goto done;
2238 error = got_worktree_open(&worktree, cwd);
2239 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2240 goto done;
2241 error = NULL;
2243 if (argc == 0) {
2244 path = strdup("");
2245 if (path == NULL) {
2246 error = got_error_from_errno("strdup");
2247 goto done;
2249 } else if (argc == 1) {
2250 if (worktree) {
2251 error = got_worktree_resolve_path(&path, worktree,
2252 argv[0]);
2253 if (error)
2254 goto done;
2255 } else {
2256 path = strdup(argv[0]);
2257 if (path == NULL) {
2258 error = got_error_from_errno("strdup");
2259 goto done;
2262 } else
2263 usage_log();
2265 if (repo_path == NULL) {
2266 if (worktree)
2267 repo_path = strdup(
2268 got_worktree_get_repo_path(worktree));
2269 else
2270 repo_path = strdup(cwd);
2272 if (repo_path == NULL) {
2273 error = got_error_from_errno("strdup");
2274 goto done;
2277 init_curses();
2279 error = got_repo_open(&repo, repo_path);
2280 if (error != NULL)
2281 goto done;
2283 error = apply_unveil(got_repo_get_path(repo),
2284 worktree ? got_worktree_get_root_path(worktree) : NULL);
2285 if (error)
2286 goto done;
2288 if (start_commit == NULL)
2289 error = get_head_commit_id(&start_id, worktree ?
2290 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2291 repo);
2292 else {
2293 error = get_head_commit_id(&start_id, start_commit, repo);
2294 if (error) {
2295 if (error->code != GOT_ERR_NOT_REF)
2296 goto done;
2297 error = got_repo_match_object_id_prefix(&start_id,
2298 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2301 if (error != NULL)
2302 goto done;
2304 error = got_ref_list(&refs, repo);
2305 if (error)
2306 goto done;
2308 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2309 if (view == NULL) {
2310 error = got_error_from_errno("view_open");
2311 goto done;
2313 error = open_log_view(view, start_id, &refs, repo, worktree ?
2314 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2315 if (error)
2316 goto done;
2317 error = view_loop(view);
2318 done:
2319 free(repo_path);
2320 free(cwd);
2321 free(path);
2322 free(start_id);
2323 if (repo)
2324 got_repo_close(repo);
2325 if (worktree)
2326 got_worktree_close(worktree);
2327 got_ref_list_free(&refs);
2328 return error;
2331 __dead static void
2332 usage_diff(void)
2334 endwin();
2335 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2336 getprogname());
2337 exit(1);
2340 static char *
2341 parse_next_line(FILE *f, size_t *len)
2343 char *line;
2344 size_t linelen;
2345 size_t lineno;
2346 const char delim[3] = { '\0', '\0', '\0'};
2348 line = fparseln(f, &linelen, &lineno, delim, 0);
2349 if (len)
2350 *len = linelen;
2351 return line;
2354 static const struct got_error *
2355 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2356 int *last_displayed_line, int *eof, int max_lines,
2357 char *header)
2359 const struct got_error *err;
2360 int nlines = 0, nprinted = 0;
2361 char *line;
2362 size_t len;
2363 wchar_t *wline;
2364 int width;
2366 rewind(f);
2367 werase(view->window);
2369 if (header) {
2370 err = format_line(&wline, &width, header, view->ncols);
2371 if (err) {
2372 return err;
2375 if (view_needs_focus_indication(view))
2376 wstandout(view->window);
2377 waddwstr(view->window, wline);
2378 if (view_needs_focus_indication(view))
2379 wstandend(view->window);
2380 if (width < view->ncols - 1)
2381 waddch(view->window, '\n');
2383 if (max_lines <= 1)
2384 return NULL;
2385 max_lines--;
2388 *eof = 0;
2389 while (nprinted < max_lines) {
2390 line = parse_next_line(f, &len);
2391 if (line == NULL) {
2392 *eof = 1;
2393 break;
2395 if (++nlines < *first_displayed_line) {
2396 free(line);
2397 continue;
2400 err = format_line(&wline, &width, line, view->ncols);
2401 if (err) {
2402 free(line);
2403 return err;
2405 waddwstr(view->window, wline);
2406 if (width < view->ncols - 1)
2407 waddch(view->window, '\n');
2408 if (++nprinted == 1)
2409 *first_displayed_line = nlines;
2410 free(line);
2411 free(wline);
2412 wline = NULL;
2414 *last_displayed_line = nlines;
2416 view_vborder(view);
2418 if (*eof) {
2419 while (nprinted < view->nlines) {
2420 waddch(view->window, '\n');
2421 nprinted++;
2424 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2425 if (err) {
2426 return err;
2429 wstandout(view->window);
2430 waddwstr(view->window, wline);
2431 wstandend(view->window);
2434 return NULL;
2437 static char *
2438 get_datestr(time_t *time, char *datebuf)
2440 struct tm mytm, *tm;
2441 char *p, *s;
2443 tm = gmtime_r(time, &mytm);
2444 if (tm == NULL)
2445 return NULL;
2446 s = asctime_r(tm, datebuf);
2447 if (s == NULL)
2448 return NULL;
2449 p = strchr(s, '\n');
2450 if (p)
2451 *p = '\0';
2452 return s;
2455 static const struct got_error *
2456 write_commit_info(struct got_object_id *commit_id,
2457 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2459 const struct got_error *err = NULL;
2460 char datebuf[26], *datestr;
2461 struct got_commit_object *commit;
2462 char *id_str = NULL, *logmsg = NULL;
2463 time_t committer_time;
2464 const char *author, *committer;
2465 char *refs_str = NULL;
2467 if (refs) {
2468 err = build_refs_str(&refs_str, refs, commit_id, repo);
2469 if (err)
2470 return err;
2473 err = got_object_open_as_commit(&commit, repo, commit_id);
2474 if (err)
2475 return err;
2477 err = got_object_id_str(&id_str, commit_id);
2478 if (err) {
2479 err = got_error_from_errno("got_object_id_str");
2480 goto done;
2483 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2484 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2485 err = got_error_from_errno("fprintf");
2486 goto done;
2488 if (fprintf(outfile, "from: %s\n",
2489 got_object_commit_get_author(commit)) < 0) {
2490 err = got_error_from_errno("fprintf");
2491 goto done;
2493 committer_time = got_object_commit_get_committer_time(commit);
2494 datestr = get_datestr(&committer_time, datebuf);
2495 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2496 err = got_error_from_errno("fprintf");
2497 goto done;
2499 author = got_object_commit_get_author(commit);
2500 committer = got_object_commit_get_committer(commit);
2501 if (strcmp(author, committer) != 0 &&
2502 fprintf(outfile, "via: %s\n", committer) < 0) {
2503 err = got_error_from_errno("fprintf");
2504 goto done;
2506 err = got_object_commit_get_logmsg(&logmsg, commit);
2507 if (err)
2508 goto done;
2509 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2510 err = got_error_from_errno("fprintf");
2511 goto done;
2513 done:
2514 free(id_str);
2515 free(logmsg);
2516 free(refs_str);
2517 got_object_commit_close(commit);
2518 return err;
2521 static const struct got_error *
2522 create_diff(struct tog_diff_view_state *s)
2524 const struct got_error *err = NULL;
2525 FILE *f = NULL;
2526 int obj_type;
2528 f = got_opentemp();
2529 if (f == NULL) {
2530 err = got_error_from_errno("got_opentemp");
2531 goto done;
2533 if (s->f && fclose(s->f) != 0) {
2534 err = got_error_from_errno("fclose");
2535 goto done;
2537 s->f = f;
2539 if (s->id1)
2540 err = got_object_get_type(&obj_type, s->repo, s->id1);
2541 else
2542 err = got_object_get_type(&obj_type, s->repo, s->id2);
2543 if (err)
2544 goto done;
2546 switch (obj_type) {
2547 case GOT_OBJ_TYPE_BLOB:
2548 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2549 s->diff_context, s->repo, f);
2550 break;
2551 case GOT_OBJ_TYPE_TREE:
2552 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2553 s->diff_context, s->repo, f);
2554 break;
2555 case GOT_OBJ_TYPE_COMMIT: {
2556 const struct got_object_id_queue *parent_ids;
2557 struct got_object_qid *pid;
2558 struct got_commit_object *commit2;
2560 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2561 if (err)
2562 break;
2563 /* Show commit info if we're diffing to a parent/root commit. */
2564 if (s->id1 == NULL)
2565 write_commit_info(s->id2, s->refs, s->repo, f);
2566 else {
2567 parent_ids = got_object_commit_get_parent_ids(commit2);
2568 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2569 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2570 write_commit_info(s->id2, s->refs,
2571 s->repo, f);
2572 break;
2576 got_object_commit_close(commit2);
2578 err = got_diff_objects_as_commits(s->id1, s->id2,
2579 s->diff_context, s->repo, f);
2580 break;
2582 default:
2583 err = got_error(GOT_ERR_OBJ_TYPE);
2584 break;
2586 done:
2587 if (f && fflush(f) != 0 && err == NULL)
2588 err = got_error_from_errno("fflush");
2589 return err;
2592 static void
2593 diff_view_indicate_progress(struct tog_view *view)
2595 mvwaddstr(view->window, 0, 0, "diffing...");
2596 update_panels();
2597 doupdate();
2600 static const struct got_error *
2601 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2602 struct got_object_id *id2, struct tog_view *log_view,
2603 struct got_reflist_head *refs, struct got_repository *repo)
2605 const struct got_error *err;
2607 if (id1 != NULL && id2 != NULL) {
2608 int type1, type2;
2609 err = got_object_get_type(&type1, repo, id1);
2610 if (err)
2611 return err;
2612 err = got_object_get_type(&type2, repo, id2);
2613 if (err)
2614 return err;
2616 if (type1 != type2)
2617 return got_error(GOT_ERR_OBJ_TYPE);
2620 if (id1) {
2621 view->state.diff.id1 = got_object_id_dup(id1);
2622 if (view->state.diff.id1 == NULL)
2623 return got_error_from_errno("got_object_id_dup");
2624 } else
2625 view->state.diff.id1 = NULL;
2627 view->state.diff.id2 = got_object_id_dup(id2);
2628 if (view->state.diff.id2 == NULL) {
2629 free(view->state.diff.id1);
2630 view->state.diff.id1 = NULL;
2631 return got_error_from_errno("got_object_id_dup");
2633 view->state.diff.f = NULL;
2634 view->state.diff.first_displayed_line = 1;
2635 view->state.diff.last_displayed_line = view->nlines;
2636 view->state.diff.diff_context = 3;
2637 view->state.diff.log_view = log_view;
2638 view->state.diff.repo = repo;
2639 view->state.diff.refs = refs;
2641 if (log_view && view_is_splitscreen(view))
2642 show_log_view(log_view); /* draw vborder */
2643 diff_view_indicate_progress(view);
2645 err = create_diff(&view->state.diff);
2646 if (err) {
2647 free(view->state.diff.id1);
2648 view->state.diff.id1 = NULL;
2649 free(view->state.diff.id2);
2650 view->state.diff.id2 = NULL;
2651 return err;
2654 view->show = show_diff_view;
2655 view->input = input_diff_view;
2656 view->close = close_diff_view;
2658 return NULL;
2661 static const struct got_error *
2662 close_diff_view(struct tog_view *view)
2664 const struct got_error *err = NULL;
2666 free(view->state.diff.id1);
2667 view->state.diff.id1 = NULL;
2668 free(view->state.diff.id2);
2669 view->state.diff.id2 = NULL;
2670 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2671 err = got_error_from_errno("fclose");
2672 return err;
2675 static const struct got_error *
2676 show_diff_view(struct tog_view *view)
2678 const struct got_error *err;
2679 struct tog_diff_view_state *s = &view->state.diff;
2680 char *id_str1 = NULL, *id_str2, *header;
2682 if (s->id1) {
2683 err = got_object_id_str(&id_str1, s->id1);
2684 if (err)
2685 return err;
2687 err = got_object_id_str(&id_str2, s->id2);
2688 if (err)
2689 return err;
2691 if (asprintf(&header, "diff %s %s",
2692 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2693 err = got_error_from_errno("asprintf");
2694 free(id_str1);
2695 free(id_str2);
2696 return err;
2698 free(id_str1);
2699 free(id_str2);
2701 return draw_file(view, s->f, &s->first_displayed_line,
2702 &s->last_displayed_line, &s->eof, view->nlines,
2703 header);
2706 static const struct got_error *
2707 set_selected_commit(struct tog_diff_view_state *s,
2708 struct commit_queue_entry *entry)
2710 const struct got_error *err;
2711 const struct got_object_id_queue *parent_ids;
2712 struct got_commit_object *selected_commit;
2713 struct got_object_qid *pid;
2715 free(s->id2);
2716 s->id2 = got_object_id_dup(entry->id);
2717 if (s->id2 == NULL)
2718 return got_error_from_errno("got_object_id_dup");
2720 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2721 if (err)
2722 return err;
2723 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2724 free(s->id1);
2725 pid = SIMPLEQ_FIRST(parent_ids);
2726 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2727 got_object_commit_close(selected_commit);
2728 return NULL;
2731 static const struct got_error *
2732 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2733 struct tog_view **focus_view, struct tog_view *view, int ch)
2735 const struct got_error *err = NULL;
2736 struct tog_diff_view_state *s = &view->state.diff;
2737 struct tog_log_view_state *ls;
2738 struct commit_queue_entry *entry;
2739 int i;
2741 switch (ch) {
2742 case 'k':
2743 case KEY_UP:
2744 if (s->first_displayed_line > 1)
2745 s->first_displayed_line--;
2746 break;
2747 case KEY_PPAGE:
2748 case CTRL('b'):
2749 if (s->first_displayed_line == 1)
2750 break;
2751 i = 0;
2752 while (i++ < view->nlines - 1 &&
2753 s->first_displayed_line > 1)
2754 s->first_displayed_line--;
2755 break;
2756 case 'j':
2757 case KEY_DOWN:
2758 if (!s->eof)
2759 s->first_displayed_line++;
2760 break;
2761 case KEY_NPAGE:
2762 case CTRL('f'):
2763 case ' ':
2764 if (s->eof)
2765 break;
2766 i = 0;
2767 while (!s->eof && i++ < view->nlines - 1) {
2768 char *line;
2769 line = parse_next_line(s->f, NULL);
2770 s->first_displayed_line++;
2771 if (line == NULL)
2772 break;
2774 break;
2775 case '[':
2776 if (s->diff_context > 0) {
2777 s->diff_context--;
2778 diff_view_indicate_progress(view);
2779 err = create_diff(s);
2781 break;
2782 case ']':
2783 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2784 s->diff_context++;
2785 diff_view_indicate_progress(view);
2786 err = create_diff(s);
2788 break;
2789 case '<':
2790 case ',':
2791 if (s->log_view == NULL)
2792 break;
2793 ls = &s->log_view->state.log;
2794 entry = TAILQ_PREV(ls->selected_entry,
2795 commit_queue_head, entry);
2796 if (entry == NULL)
2797 break;
2799 err = input_log_view(NULL, NULL, NULL, s->log_view,
2800 KEY_UP);
2801 if (err)
2802 break;
2804 err = set_selected_commit(s, entry);
2805 if (err)
2806 break;
2808 s->first_displayed_line = 1;
2809 s->last_displayed_line = view->nlines;
2811 diff_view_indicate_progress(view);
2812 err = create_diff(s);
2813 break;
2814 case '>':
2815 case '.':
2816 if (s->log_view == NULL)
2817 break;
2818 ls = &s->log_view->state.log;
2820 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2821 ls->thread_args.commits_needed++;
2823 /* Display "loading..." in log view. */
2824 show_log_view(s->log_view);
2825 update_panels();
2826 doupdate();
2828 err = trigger_log_thread(1 /* load_all */,
2829 &ls->thread_args.commits_needed,
2830 &ls->thread_args.log_complete,
2831 &ls->thread_args.need_commits);
2832 if (err)
2833 break;
2835 err = input_log_view(NULL, NULL, NULL, s->log_view,
2836 KEY_DOWN);
2837 if (err)
2838 break;
2840 entry = TAILQ_NEXT(ls->selected_entry, entry);
2841 if (entry == NULL)
2842 break;
2844 err = set_selected_commit(s, entry);
2845 if (err)
2846 break;
2848 s->first_displayed_line = 1;
2849 s->last_displayed_line = view->nlines;
2851 diff_view_indicate_progress(view);
2852 err = create_diff(s);
2853 break;
2854 default:
2855 break;
2858 return err;
2861 static const struct got_error *
2862 cmd_diff(int argc, char *argv[])
2864 const struct got_error *error = NULL;
2865 struct got_repository *repo = NULL;
2866 struct got_reflist_head refs;
2867 struct got_object_id *id1 = NULL, *id2 = NULL;
2868 char *repo_path = NULL;
2869 char *id_str1 = NULL, *id_str2 = NULL;
2870 int ch;
2871 struct tog_view *view;
2873 SIMPLEQ_INIT(&refs);
2875 #ifndef PROFILE
2876 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2877 NULL) == -1)
2878 err(1, "pledge");
2879 #endif
2881 while ((ch = getopt(argc, argv, "")) != -1) {
2882 switch (ch) {
2883 default:
2884 usage_diff();
2885 /* NOTREACHED */
2889 argc -= optind;
2890 argv += optind;
2892 if (argc == 0) {
2893 usage_diff(); /* TODO show local worktree changes */
2894 } else if (argc == 2) {
2895 repo_path = getcwd(NULL, 0);
2896 if (repo_path == NULL)
2897 return got_error_from_errno("getcwd");
2898 id_str1 = argv[0];
2899 id_str2 = argv[1];
2900 } else if (argc == 3) {
2901 repo_path = realpath(argv[0], NULL);
2902 if (repo_path == NULL)
2903 return got_error_from_errno2("realpath", argv[0]);
2904 id_str1 = argv[1];
2905 id_str2 = argv[2];
2906 } else
2907 usage_diff();
2909 init_curses();
2911 error = got_repo_open(&repo, repo_path);
2912 if (error)
2913 goto done;
2915 error = apply_unveil(got_repo_get_path(repo), NULL);
2916 if (error)
2917 goto done;
2919 error = got_repo_match_object_id_prefix(&id1, id_str1,
2920 GOT_OBJ_TYPE_ANY, repo);
2921 if (error)
2922 goto done;
2924 error = got_repo_match_object_id_prefix(&id2, id_str2,
2925 GOT_OBJ_TYPE_ANY, repo);
2926 if (error)
2927 goto done;
2929 error = got_ref_list(&refs, repo);
2930 if (error)
2931 goto done;
2933 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2934 if (view == NULL) {
2935 error = got_error_from_errno("view_open");
2936 goto done;
2938 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2939 if (error)
2940 goto done;
2941 error = view_loop(view);
2942 done:
2943 free(repo_path);
2944 if (repo)
2945 got_repo_close(repo);
2946 got_ref_list_free(&refs);
2947 return error;
2950 __dead static void
2951 usage_blame(void)
2953 endwin();
2954 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2955 getprogname());
2956 exit(1);
2959 struct tog_blame_line {
2960 int annotated;
2961 struct got_object_id *id;
2964 static const struct got_error *
2965 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2966 const char *path, struct tog_blame_line *lines, int nlines,
2967 int blame_complete, int selected_line, int *first_displayed_line,
2968 int *last_displayed_line, int *eof, int max_lines)
2970 const struct got_error *err;
2971 int lineno = 0, nprinted = 0;
2972 char *line;
2973 size_t len;
2974 wchar_t *wline;
2975 int width, wlimit;
2976 struct tog_blame_line *blame_line;
2977 struct got_object_id *prev_id = NULL;
2978 char *id_str;
2980 err = got_object_id_str(&id_str, id);
2981 if (err)
2982 return err;
2984 rewind(f);
2985 werase(view->window);
2987 if (asprintf(&line, "commit %s", id_str) == -1) {
2988 err = got_error_from_errno("asprintf");
2989 free(id_str);
2990 return err;
2993 err = format_line(&wline, &width, line, view->ncols);
2994 free(line);
2995 line = NULL;
2996 if (view_needs_focus_indication(view))
2997 wstandout(view->window);
2998 waddwstr(view->window, wline);
2999 if (view_needs_focus_indication(view))
3000 wstandend(view->window);
3001 free(wline);
3002 wline = NULL;
3003 if (width < view->ncols - 1)
3004 waddch(view->window, '\n');
3006 if (asprintf(&line, "[%d/%d] %s%s",
3007 *first_displayed_line - 1 + selected_line, nlines,
3008 blame_complete ? "" : "annotating... ", path) == -1) {
3009 free(id_str);
3010 return got_error_from_errno("asprintf");
3012 free(id_str);
3013 err = format_line(&wline, &width, line, view->ncols);
3014 free(line);
3015 line = NULL;
3016 if (err)
3017 return err;
3018 waddwstr(view->window, wline);
3019 free(wline);
3020 wline = NULL;
3021 if (width < view->ncols - 1)
3022 waddch(view->window, '\n');
3024 *eof = 0;
3025 while (nprinted < max_lines - 2) {
3026 line = parse_next_line(f, &len);
3027 if (line == NULL) {
3028 *eof = 1;
3029 break;
3031 if (++lineno < *first_displayed_line) {
3032 free(line);
3033 continue;
3036 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3037 err = format_line(&wline, &width, line, wlimit);
3038 if (err) {
3039 free(line);
3040 return err;
3043 if (view->focussed && nprinted == selected_line - 1)
3044 wstandout(view->window);
3046 if (nlines > 0) {
3047 blame_line = &lines[lineno - 1];
3048 if (blame_line->annotated && prev_id &&
3049 got_object_id_cmp(prev_id, blame_line->id) == 0)
3050 waddstr(view->window, " ");
3051 else if (blame_line->annotated) {
3052 char *id_str;
3053 err = got_object_id_str(&id_str, blame_line->id);
3054 if (err) {
3055 free(line);
3056 free(wline);
3057 return err;
3059 wprintw(view->window, "%.8s ", id_str);
3060 free(id_str);
3061 prev_id = blame_line->id;
3062 } else {
3063 waddstr(view->window, "........ ");
3064 prev_id = NULL;
3066 } else {
3067 waddstr(view->window, "........ ");
3068 prev_id = NULL;
3071 waddwstr(view->window, wline);
3072 while (width < wlimit) {
3073 waddch(view->window, ' ');
3074 width++;
3076 if (view->focussed && nprinted == selected_line - 1)
3077 wstandend(view->window);
3078 if (++nprinted == 1)
3079 *first_displayed_line = lineno;
3080 free(line);
3081 free(wline);
3082 wline = NULL;
3084 *last_displayed_line = lineno;
3086 view_vborder(view);
3088 return NULL;
3091 static const struct got_error *
3092 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3094 const struct got_error *err = NULL;
3095 struct tog_blame_cb_args *a = arg;
3096 struct tog_blame_line *line;
3097 int errcode;
3099 if (nlines != a->nlines ||
3100 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3101 return got_error(GOT_ERR_RANGE);
3103 errcode = pthread_mutex_lock(&tog_mutex);
3104 if (errcode)
3105 return got_error_set_errno(errcode, "pthread_mutex_lock");
3107 if (*a->quit) { /* user has quit the blame view */
3108 err = got_error(GOT_ERR_ITER_COMPLETED);
3109 goto done;
3112 if (lineno == -1)
3113 goto done; /* no change in this commit */
3115 line = &a->lines[lineno - 1];
3116 if (line->annotated)
3117 goto done;
3119 line->id = got_object_id_dup(id);
3120 if (line->id == NULL) {
3121 err = got_error_from_errno("got_object_id_dup");
3122 goto done;
3124 line->annotated = 1;
3125 done:
3126 errcode = pthread_mutex_unlock(&tog_mutex);
3127 if (errcode)
3128 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3129 return err;
3132 static void *
3133 blame_thread(void *arg)
3135 const struct got_error *err;
3136 struct tog_blame_thread_args *ta = arg;
3137 struct tog_blame_cb_args *a = ta->cb_args;
3138 int errcode;
3140 err = got_blame(ta->path, a->commit_id, ta->repo,
3141 blame_cb, ta->cb_args, NULL, NULL);
3143 errcode = pthread_mutex_lock(&tog_mutex);
3144 if (errcode)
3145 return (void *)got_error_set_errno(errcode,
3146 "pthread_mutex_lock");
3148 got_repo_close(ta->repo);
3149 ta->repo = NULL;
3150 *ta->complete = 1;
3152 errcode = pthread_mutex_unlock(&tog_mutex);
3153 if (errcode && err == NULL)
3154 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3156 return (void *)err;
3159 static struct got_object_id *
3160 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3161 int first_displayed_line, int selected_line)
3163 struct tog_blame_line *line;
3165 if (nlines <= 0)
3166 return NULL;
3168 line = &lines[first_displayed_line - 1 + selected_line - 1];
3169 if (!line->annotated)
3170 return NULL;
3172 return line->id;
3175 static const struct got_error *
3176 stop_blame(struct tog_blame *blame)
3178 const struct got_error *err = NULL;
3179 int i;
3181 if (blame->thread) {
3182 int errcode;
3183 errcode = pthread_mutex_unlock(&tog_mutex);
3184 if (errcode)
3185 return got_error_set_errno(errcode,
3186 "pthread_mutex_unlock");
3187 errcode = pthread_join(blame->thread, (void **)&err);
3188 if (errcode)
3189 return got_error_set_errno(errcode, "pthread_join");
3190 errcode = pthread_mutex_lock(&tog_mutex);
3191 if (errcode)
3192 return got_error_set_errno(errcode,
3193 "pthread_mutex_lock");
3194 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3195 err = NULL;
3196 blame->thread = NULL;
3198 if (blame->thread_args.repo) {
3199 got_repo_close(blame->thread_args.repo);
3200 blame->thread_args.repo = NULL;
3202 if (blame->f) {
3203 if (fclose(blame->f) != 0 && err == NULL)
3204 err = got_error_from_errno("fclose");
3205 blame->f = NULL;
3207 if (blame->lines) {
3208 for (i = 0; i < blame->nlines; i++)
3209 free(blame->lines[i].id);
3210 free(blame->lines);
3211 blame->lines = NULL;
3213 free(blame->cb_args.commit_id);
3214 blame->cb_args.commit_id = NULL;
3216 return err;
3219 static const struct got_error *
3220 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3221 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3222 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3223 struct got_repository *repo)
3225 const struct got_error *err = NULL;
3226 struct got_blob_object *blob = NULL;
3227 struct got_repository *thread_repo = NULL;
3228 struct got_object_id *obj_id = NULL;
3229 int obj_type;
3231 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3232 if (err)
3233 return err;
3234 if (obj_id == NULL)
3235 return got_error(GOT_ERR_NO_OBJ);
3237 err = got_object_get_type(&obj_type, repo, obj_id);
3238 if (err)
3239 goto done;
3241 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3242 err = got_error(GOT_ERR_OBJ_TYPE);
3243 goto done;
3246 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3247 if (err)
3248 goto done;
3249 blame->f = got_opentemp();
3250 if (blame->f == NULL) {
3251 err = got_error_from_errno("got_opentemp");
3252 goto done;
3254 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3255 &blame->line_offsets, blame->f, blob);
3256 if (err || blame->nlines == 0)
3257 goto done;
3259 /* Don't include \n at EOF in the blame line count. */
3260 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3261 blame->nlines--;
3263 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3264 if (blame->lines == NULL) {
3265 err = got_error_from_errno("calloc");
3266 goto done;
3269 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3270 if (err)
3271 goto done;
3273 blame->cb_args.view = view;
3274 blame->cb_args.lines = blame->lines;
3275 blame->cb_args.nlines = blame->nlines;
3276 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3277 if (blame->cb_args.commit_id == NULL) {
3278 err = got_error_from_errno("got_object_id_dup");
3279 goto done;
3281 blame->cb_args.quit = done;
3283 blame->thread_args.path = path;
3284 blame->thread_args.repo = thread_repo;
3285 blame->thread_args.cb_args = &blame->cb_args;
3286 blame->thread_args.complete = blame_complete;
3287 *blame_complete = 0;
3289 done:
3290 if (blob)
3291 got_object_blob_close(blob);
3292 free(obj_id);
3293 if (err)
3294 stop_blame(blame);
3295 return err;
3298 static const struct got_error *
3299 open_blame_view(struct tog_view *view, char *path,
3300 struct got_object_id *commit_id, struct got_reflist_head *refs,
3301 struct got_repository *repo)
3303 const struct got_error *err = NULL;
3304 struct tog_blame_view_state *s = &view->state.blame;
3306 SIMPLEQ_INIT(&s->blamed_commits);
3308 s->path = strdup(path);
3309 if (s->path == NULL)
3310 return got_error_from_errno("strdup");
3312 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3313 if (err) {
3314 free(s->path);
3315 return err;
3318 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3319 s->first_displayed_line = 1;
3320 s->last_displayed_line = view->nlines;
3321 s->selected_line = 1;
3322 s->blame_complete = 0;
3323 s->repo = repo;
3324 s->refs = refs;
3325 s->commit_id = commit_id;
3326 memset(&s->blame, 0, sizeof(s->blame));
3328 view->show = show_blame_view;
3329 view->input = input_blame_view;
3330 view->close = close_blame_view;
3331 view->search_start = search_start_blame_view;
3332 view->search_next = search_next_blame_view;
3334 return run_blame(&s->blame, view, &s->blame_complete,
3335 &s->first_displayed_line, &s->last_displayed_line,
3336 &s->selected_line, &s->done, &s->eof, s->path,
3337 s->blamed_commit->id, s->repo);
3340 static const struct got_error *
3341 close_blame_view(struct tog_view *view)
3343 const struct got_error *err = NULL;
3344 struct tog_blame_view_state *s = &view->state.blame;
3346 if (s->blame.thread)
3347 err = stop_blame(&s->blame);
3349 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3350 struct got_object_qid *blamed_commit;
3351 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3352 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3353 got_object_qid_free(blamed_commit);
3356 free(s->path);
3358 return err;
3361 static const struct got_error *
3362 search_start_blame_view(struct tog_view *view)
3364 struct tog_blame_view_state *s = &view->state.blame;
3366 s->matched_line = 0;
3367 return NULL;
3370 static int
3371 match_line(const char *line, regex_t *regex)
3373 regmatch_t regmatch;
3375 return regexec(regex, line, 1, &regmatch, 0) == 0;
3379 static const struct got_error *
3380 search_next_blame_view(struct tog_view *view)
3382 struct tog_blame_view_state *s = &view->state.blame;
3383 int lineno;
3385 if (!view->searching) {
3386 view->search_next_done = 1;
3387 return NULL;
3390 if (s->matched_line) {
3391 if (view->searching == TOG_SEARCH_FORWARD)
3392 lineno = s->matched_line + 1;
3393 else
3394 lineno = s->matched_line - 1;
3395 } else {
3396 if (view->searching == TOG_SEARCH_FORWARD)
3397 lineno = 1;
3398 else
3399 lineno = s->blame.nlines;
3402 while (1) {
3403 char *line = NULL;
3404 off_t offset;
3405 size_t len;
3407 if (lineno <= 0 || lineno > s->blame.nlines) {
3408 if (s->matched_line == 0) {
3409 view->search_next_done = 1;
3410 free(line);
3411 break;
3414 if (view->searching == TOG_SEARCH_FORWARD)
3415 lineno = 1;
3416 else
3417 lineno = s->blame.nlines;
3420 offset = s->blame.line_offsets[lineno - 1];
3421 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3422 free(line);
3423 return got_error_from_errno("fseeko");
3425 free(line);
3426 line = parse_next_line(s->blame.f, &len);
3427 if (line && match_line(line, &view->regex)) {
3428 view->search_next_done = 1;
3429 s->matched_line = lineno;
3430 free(line);
3431 break;
3433 free(line);
3434 if (view->searching == TOG_SEARCH_FORWARD)
3435 lineno++;
3436 else
3437 lineno--;
3440 if (s->matched_line) {
3441 s->first_displayed_line = s->matched_line;
3442 s->selected_line = 1;
3445 return NULL;
3448 static const struct got_error *
3449 show_blame_view(struct tog_view *view)
3451 const struct got_error *err = NULL;
3452 struct tog_blame_view_state *s = &view->state.blame;
3453 int errcode;
3455 if (s->blame.thread == NULL) {
3456 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3457 &s->blame.thread_args);
3458 if (errcode)
3459 return got_error_set_errno(errcode, "pthread_create");
3461 halfdelay(1); /* fast refresh while annotating */
3464 if (s->blame_complete)
3465 halfdelay(10); /* disable fast refresh */
3467 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3468 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3469 s->selected_line, &s->first_displayed_line,
3470 &s->last_displayed_line, &s->eof, view->nlines);
3472 view_vborder(view);
3473 return err;
3476 static const struct got_error *
3477 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3478 struct tog_view **focus_view, struct tog_view *view, int ch)
3480 const struct got_error *err = NULL, *thread_err = NULL;
3481 struct tog_view *diff_view;
3482 struct tog_blame_view_state *s = &view->state.blame;
3483 int begin_x = 0;
3485 switch (ch) {
3486 case 'q':
3487 s->done = 1;
3488 break;
3489 case 'k':
3490 case KEY_UP:
3491 if (s->selected_line > 1)
3492 s->selected_line--;
3493 else if (s->selected_line == 1 &&
3494 s->first_displayed_line > 1)
3495 s->first_displayed_line--;
3496 break;
3497 case KEY_PPAGE:
3498 if (s->first_displayed_line == 1) {
3499 s->selected_line = 1;
3500 break;
3502 if (s->first_displayed_line > view->nlines - 2)
3503 s->first_displayed_line -=
3504 (view->nlines - 2);
3505 else
3506 s->first_displayed_line = 1;
3507 break;
3508 case 'j':
3509 case KEY_DOWN:
3510 if (s->selected_line < view->nlines - 2 &&
3511 s->first_displayed_line +
3512 s->selected_line <= s->blame.nlines)
3513 s->selected_line++;
3514 else if (s->last_displayed_line <
3515 s->blame.nlines)
3516 s->first_displayed_line++;
3517 break;
3518 case 'b':
3519 case 'p': {
3520 struct got_object_id *id = NULL;
3521 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3522 s->first_displayed_line, s->selected_line);
3523 if (id == NULL)
3524 break;
3525 if (ch == 'p') {
3526 struct got_commit_object *commit;
3527 struct got_object_qid *pid;
3528 struct got_object_id *blob_id = NULL;
3529 int obj_type;
3530 err = got_object_open_as_commit(&commit,
3531 s->repo, id);
3532 if (err)
3533 break;
3534 pid = SIMPLEQ_FIRST(
3535 got_object_commit_get_parent_ids(commit));
3536 if (pid == NULL) {
3537 got_object_commit_close(commit);
3538 break;
3540 /* Check if path history ends here. */
3541 err = got_object_id_by_path(&blob_id, s->repo,
3542 pid->id, s->path);
3543 if (err) {
3544 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3545 err = NULL;
3546 got_object_commit_close(commit);
3547 break;
3549 err = got_object_get_type(&obj_type, s->repo,
3550 blob_id);
3551 free(blob_id);
3552 /* Can't blame non-blob type objects. */
3553 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3554 got_object_commit_close(commit);
3555 break;
3557 err = got_object_qid_alloc(&s->blamed_commit,
3558 pid->id);
3559 got_object_commit_close(commit);
3560 } else {
3561 if (got_object_id_cmp(id,
3562 s->blamed_commit->id) == 0)
3563 break;
3564 err = got_object_qid_alloc(&s->blamed_commit,
3565 id);
3567 if (err)
3568 break;
3569 s->done = 1;
3570 thread_err = stop_blame(&s->blame);
3571 s->done = 0;
3572 if (thread_err)
3573 break;
3574 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3575 s->blamed_commit, entry);
3576 err = run_blame(&s->blame, view, &s->blame_complete,
3577 &s->first_displayed_line, &s->last_displayed_line,
3578 &s->selected_line, &s->done, &s->eof,
3579 s->path, s->blamed_commit->id, s->repo);
3580 if (err)
3581 break;
3582 break;
3584 case 'B': {
3585 struct got_object_qid *first;
3586 first = SIMPLEQ_FIRST(&s->blamed_commits);
3587 if (!got_object_id_cmp(first->id, s->commit_id))
3588 break;
3589 s->done = 1;
3590 thread_err = stop_blame(&s->blame);
3591 s->done = 0;
3592 if (thread_err)
3593 break;
3594 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3595 got_object_qid_free(s->blamed_commit);
3596 s->blamed_commit =
3597 SIMPLEQ_FIRST(&s->blamed_commits);
3598 err = run_blame(&s->blame, view, &s->blame_complete,
3599 &s->first_displayed_line, &s->last_displayed_line,
3600 &s->selected_line, &s->done, &s->eof, s->path,
3601 s->blamed_commit->id, s->repo);
3602 if (err)
3603 break;
3604 break;
3606 case KEY_ENTER:
3607 case '\r': {
3608 struct got_object_id *id = NULL;
3609 struct got_object_qid *pid;
3610 struct got_commit_object *commit = NULL;
3611 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3612 s->first_displayed_line, s->selected_line);
3613 if (id == NULL)
3614 break;
3615 err = got_object_open_as_commit(&commit, s->repo, id);
3616 if (err)
3617 break;
3618 pid = SIMPLEQ_FIRST(
3619 got_object_commit_get_parent_ids(commit));
3620 if (view_is_parent_view(view))
3621 begin_x = view_split_begin_x(view->begin_x);
3622 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3623 if (diff_view == NULL) {
3624 got_object_commit_close(commit);
3625 err = got_error_from_errno("view_open");
3626 break;
3628 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3629 id, NULL, s->refs, s->repo);
3630 got_object_commit_close(commit);
3631 if (err) {
3632 view_close(diff_view);
3633 break;
3635 if (view_is_parent_view(view)) {
3636 err = view_close_child(view);
3637 if (err)
3638 break;
3639 err = view_set_child(view, diff_view);
3640 if (err) {
3641 view_close(diff_view);
3642 break;
3644 *focus_view = diff_view;
3645 view->child_focussed = 1;
3646 } else
3647 *new_view = diff_view;
3648 if (err)
3649 break;
3650 break;
3652 case KEY_NPAGE:
3653 case ' ':
3654 if (s->last_displayed_line >= s->blame.nlines &&
3655 s->selected_line >= MIN(s->blame.nlines,
3656 view->nlines - 2)) {
3657 break;
3659 if (s->last_displayed_line >= s->blame.nlines &&
3660 s->selected_line < view->nlines - 2) {
3661 s->selected_line = MIN(s->blame.nlines,
3662 view->nlines - 2);
3663 break;
3665 if (s->last_displayed_line + view->nlines - 2
3666 <= s->blame.nlines)
3667 s->first_displayed_line +=
3668 view->nlines - 2;
3669 else
3670 s->first_displayed_line =
3671 s->blame.nlines -
3672 (view->nlines - 3);
3673 break;
3674 case KEY_RESIZE:
3675 if (s->selected_line > view->nlines - 2) {
3676 s->selected_line = MIN(s->blame.nlines,
3677 view->nlines - 2);
3679 break;
3680 default:
3681 break;
3683 return thread_err ? thread_err : err;
3686 static const struct got_error *
3687 cmd_blame(int argc, char *argv[])
3689 const struct got_error *error;
3690 struct got_repository *repo = NULL;
3691 struct got_reflist_head refs;
3692 struct got_worktree *worktree = NULL;
3693 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3694 struct got_object_id *commit_id = NULL;
3695 char *commit_id_str = NULL;
3696 int ch;
3697 struct tog_view *view;
3699 SIMPLEQ_INIT(&refs);
3701 #ifndef PROFILE
3702 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3703 NULL) == -1)
3704 err(1, "pledge");
3705 #endif
3707 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3708 switch (ch) {
3709 case 'c':
3710 commit_id_str = optarg;
3711 break;
3712 case 'r':
3713 repo_path = realpath(optarg, NULL);
3714 if (repo_path == NULL)
3715 err(1, "-r option");
3716 break;
3717 default:
3718 usage_blame();
3719 /* NOTREACHED */
3723 argc -= optind;
3724 argv += optind;
3726 if (argc == 1)
3727 path = argv[0];
3728 else
3729 usage_blame();
3731 cwd = getcwd(NULL, 0);
3732 if (cwd == NULL) {
3733 error = got_error_from_errno("getcwd");
3734 goto done;
3736 if (repo_path == NULL) {
3737 error = got_worktree_open(&worktree, cwd);
3738 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3739 goto done;
3740 else
3741 error = NULL;
3742 if (worktree) {
3743 repo_path =
3744 strdup(got_worktree_get_repo_path(worktree));
3745 if (repo_path == NULL)
3746 error = got_error_from_errno("strdup");
3747 if (error)
3748 goto done;
3749 } else {
3750 repo_path = strdup(cwd);
3751 if (repo_path == NULL) {
3752 error = got_error_from_errno("strdup");
3753 goto done;
3758 init_curses();
3760 error = got_repo_open(&repo, repo_path);
3761 if (error != NULL)
3762 goto done;
3764 error = apply_unveil(got_repo_get_path(repo), NULL);
3765 if (error)
3766 goto done;
3768 if (worktree) {
3769 const char *prefix = got_worktree_get_path_prefix(worktree);
3770 char *p, *worktree_subdir = cwd +
3771 strlen(got_worktree_get_root_path(worktree));
3772 if (asprintf(&p, "%s%s%s%s%s",
3773 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3774 worktree_subdir, worktree_subdir[0] ? "/" : "",
3775 path) == -1) {
3776 error = got_error_from_errno("asprintf");
3777 goto done;
3779 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3780 free(p);
3781 } else {
3782 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3784 if (error)
3785 goto done;
3787 if (commit_id_str == NULL) {
3788 struct got_reference *head_ref;
3789 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3790 if (error != NULL)
3791 goto done;
3792 error = got_ref_resolve(&commit_id, repo, head_ref);
3793 got_ref_close(head_ref);
3794 } else {
3795 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3796 if (error) {
3797 if (error->code != GOT_ERR_NOT_REF)
3798 goto done;
3799 error = got_repo_match_object_id_prefix(&commit_id,
3800 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3803 if (error != NULL)
3804 goto done;
3806 error = got_ref_list(&refs, repo);
3807 if (error)
3808 goto done;
3810 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3811 if (view == NULL) {
3812 error = got_error_from_errno("view_open");
3813 goto done;
3815 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3816 if (error)
3817 goto done;
3818 error = view_loop(view);
3819 done:
3820 free(repo_path);
3821 free(cwd);
3822 free(commit_id);
3823 if (worktree)
3824 got_worktree_close(worktree);
3825 if (repo)
3826 got_repo_close(repo);
3827 got_ref_list_free(&refs);
3828 return error;
3831 static const struct got_error *
3832 draw_tree_entries(struct tog_view *view,
3833 struct got_tree_entry **first_displayed_entry,
3834 struct got_tree_entry **last_displayed_entry,
3835 struct got_tree_entry **selected_entry, int *ndisplayed,
3836 const char *label, int show_ids, const char *parent_path,
3837 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3839 const struct got_error *err = NULL;
3840 struct got_tree_entry *te;
3841 wchar_t *wline;
3842 int width, n;
3844 *ndisplayed = 0;
3846 werase(view->window);
3848 if (limit == 0)
3849 return NULL;
3851 err = format_line(&wline, &width, label, view->ncols);
3852 if (err)
3853 return err;
3854 if (view_needs_focus_indication(view))
3855 wstandout(view->window);
3856 waddwstr(view->window, wline);
3857 if (view_needs_focus_indication(view))
3858 wstandend(view->window);
3859 free(wline);
3860 wline = NULL;
3861 if (width < view->ncols - 1)
3862 waddch(view->window, '\n');
3863 if (--limit <= 0)
3864 return NULL;
3865 err = format_line(&wline, &width, parent_path, view->ncols);
3866 if (err)
3867 return err;
3868 waddwstr(view->window, wline);
3869 free(wline);
3870 wline = NULL;
3871 if (width < view->ncols - 1)
3872 waddch(view->window, '\n');
3873 if (--limit <= 0)
3874 return NULL;
3875 waddch(view->window, '\n');
3876 if (--limit <= 0)
3877 return NULL;
3879 te = SIMPLEQ_FIRST(&entries->head);
3880 if (*first_displayed_entry == NULL) {
3881 if (selected == 0) {
3882 if (view->focussed)
3883 wstandout(view->window);
3884 *selected_entry = NULL;
3886 waddstr(view->window, " ..\n"); /* parent directory */
3887 if (selected == 0 && view->focussed)
3888 wstandend(view->window);
3889 (*ndisplayed)++;
3890 if (--limit <= 0)
3891 return NULL;
3892 n = 1;
3893 } else {
3894 n = 0;
3895 while (te != *first_displayed_entry)
3896 te = SIMPLEQ_NEXT(te, entry);
3899 while (te) {
3900 char *line = NULL, *id_str = NULL;
3901 const char *modestr = "";
3903 if (show_ids) {
3904 err = got_object_id_str(&id_str, te->id);
3905 if (err)
3906 return got_error_from_errno(
3907 "got_object_id_str");
3909 if (S_ISLNK(te->mode))
3910 modestr = "@";
3911 else if (S_ISDIR(te->mode))
3912 modestr = "/";
3913 else if (te->mode & S_IXUSR)
3914 modestr = "*";
3915 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3916 te->name, modestr) == -1) {
3917 free(id_str);
3918 return got_error_from_errno("asprintf");
3920 free(id_str);
3921 err = format_line(&wline, &width, line, view->ncols);
3922 if (err) {
3923 free(line);
3924 break;
3926 if (n == selected) {
3927 if (view->focussed)
3928 wstandout(view->window);
3929 *selected_entry = te;
3931 waddwstr(view->window, wline);
3932 if (width < view->ncols - 1)
3933 waddch(view->window, '\n');
3934 if (n == selected && view->focussed)
3935 wstandend(view->window);
3936 free(line);
3937 free(wline);
3938 wline = NULL;
3939 n++;
3940 (*ndisplayed)++;
3941 *last_displayed_entry = te;
3942 if (--limit <= 0)
3943 break;
3944 te = SIMPLEQ_NEXT(te, entry);
3947 return err;
3950 static void
3951 tree_scroll_up(struct tog_view *view,
3952 struct got_tree_entry **first_displayed_entry, int maxscroll,
3953 const struct got_tree_entries *entries, int isroot)
3955 struct got_tree_entry *te, *prev;
3956 int i;
3958 if (*first_displayed_entry == NULL)
3959 return;
3961 te = SIMPLEQ_FIRST(&entries->head);
3962 if (*first_displayed_entry == te) {
3963 if (!isroot)
3964 *first_displayed_entry = NULL;
3965 return;
3968 /* XXX this is stupid... switch to TAILQ? */
3969 for (i = 0; i < maxscroll; i++) {
3970 while (te != *first_displayed_entry) {
3971 prev = te;
3972 te = SIMPLEQ_NEXT(te, entry);
3974 *first_displayed_entry = prev;
3975 te = SIMPLEQ_FIRST(&entries->head);
3977 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3978 *first_displayed_entry = NULL;
3981 static int
3982 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3983 struct got_tree_entry *last_displayed_entry,
3984 const struct got_tree_entries *entries)
3986 struct got_tree_entry *next, *last;
3987 int n = 0;
3989 if (*first_displayed_entry)
3990 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3991 else
3992 next = SIMPLEQ_FIRST(&entries->head);
3993 last = last_displayed_entry;
3994 while (next && last && n++ < maxscroll) {
3995 last = SIMPLEQ_NEXT(last, entry);
3996 if (last) {
3997 *first_displayed_entry = next;
3998 next = SIMPLEQ_NEXT(next, entry);
4001 return n;
4004 static const struct got_error *
4005 tree_entry_path(char **path, struct tog_parent_trees *parents,
4006 struct got_tree_entry *te)
4008 const struct got_error *err = NULL;
4009 struct tog_parent_tree *pt;
4010 size_t len = 2; /* for leading slash and NUL */
4012 TAILQ_FOREACH(pt, parents, entry)
4013 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4014 if (te)
4015 len += strlen(te->name);
4017 *path = calloc(1, len);
4018 if (path == NULL)
4019 return got_error_from_errno("calloc");
4021 (*path)[0] = '/';
4022 pt = TAILQ_LAST(parents, tog_parent_trees);
4023 while (pt) {
4024 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4025 err = got_error(GOT_ERR_NO_SPACE);
4026 goto done;
4028 if (strlcat(*path, "/", len) >= len) {
4029 err = got_error(GOT_ERR_NO_SPACE);
4030 goto done;
4032 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4034 if (te) {
4035 if (strlcat(*path, te->name, len) >= len) {
4036 err = got_error(GOT_ERR_NO_SPACE);
4037 goto done;
4040 done:
4041 if (err) {
4042 free(*path);
4043 *path = NULL;
4045 return err;
4048 static const struct got_error *
4049 blame_tree_entry(struct tog_view **new_view, int begin_x,
4050 struct got_tree_entry *te, struct tog_parent_trees *parents,
4051 struct got_object_id *commit_id, struct got_reflist_head *refs,
4052 struct got_repository *repo)
4054 const struct got_error *err = NULL;
4055 char *path;
4056 struct tog_view *blame_view;
4058 *new_view = NULL;
4060 err = tree_entry_path(&path, parents, te);
4061 if (err)
4062 return err;
4064 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4065 if (blame_view == NULL) {
4066 err = got_error_from_errno("view_open");
4067 goto done;
4070 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4071 if (err) {
4072 view_close(blame_view);
4073 } else
4074 *new_view = blame_view;
4075 done:
4076 free(path);
4077 return err;
4080 static const struct got_error *
4081 log_tree_entry(struct tog_view **new_view, int begin_x,
4082 struct got_tree_entry *te, struct tog_parent_trees *parents,
4083 struct got_object_id *commit_id, struct got_reflist_head *refs,
4084 struct got_repository *repo)
4086 struct tog_view *log_view;
4087 const struct got_error *err = NULL;
4088 char *path;
4090 *new_view = NULL;
4092 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4093 if (log_view == NULL)
4094 return got_error_from_errno("view_open");
4096 err = tree_entry_path(&path, parents, te);
4097 if (err)
4098 return err;
4100 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4101 if (err)
4102 view_close(log_view);
4103 else
4104 *new_view = log_view;
4105 free(path);
4106 return err;
4109 static const struct got_error *
4110 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4111 struct got_object_id *commit_id, struct got_reflist_head *refs,
4112 struct got_repository *repo)
4114 const struct got_error *err = NULL;
4115 char *commit_id_str = NULL;
4116 struct tog_tree_view_state *s = &view->state.tree;
4118 TAILQ_INIT(&s->parents);
4120 err = got_object_id_str(&commit_id_str, commit_id);
4121 if (err != NULL)
4122 goto done;
4124 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4125 err = got_error_from_errno("asprintf");
4126 goto done;
4129 s->root = s->tree = root;
4130 s->entries = got_object_tree_get_entries(root);
4131 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4132 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4133 s->commit_id = got_object_id_dup(commit_id);
4134 if (s->commit_id == NULL) {
4135 err = got_error_from_errno("got_object_id_dup");
4136 goto done;
4138 s->refs = refs;
4139 s->repo = repo;
4141 view->show = show_tree_view;
4142 view->input = input_tree_view;
4143 view->close = close_tree_view;
4144 view->search_start = search_start_tree_view;
4145 view->search_next = search_next_tree_view;
4146 done:
4147 free(commit_id_str);
4148 if (err) {
4149 free(s->tree_label);
4150 s->tree_label = NULL;
4152 return err;
4155 static const struct got_error *
4156 close_tree_view(struct tog_view *view)
4158 struct tog_tree_view_state *s = &view->state.tree;
4160 free(s->tree_label);
4161 s->tree_label = NULL;
4162 free(s->commit_id);
4163 s->commit_id = NULL;
4164 while (!TAILQ_EMPTY(&s->parents)) {
4165 struct tog_parent_tree *parent;
4166 parent = TAILQ_FIRST(&s->parents);
4167 TAILQ_REMOVE(&s->parents, parent, entry);
4168 free(parent);
4171 if (s->tree != s->root)
4172 got_object_tree_close(s->tree);
4173 got_object_tree_close(s->root);
4175 return NULL;
4178 static const struct got_error *
4179 search_start_tree_view(struct tog_view *view)
4181 struct tog_tree_view_state *s = &view->state.tree;
4183 s->matched_entry = NULL;
4184 return NULL;
4187 static int
4188 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4190 regmatch_t regmatch;
4192 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4195 static const struct got_error *
4196 search_next_tree_view(struct tog_view *view)
4198 struct tog_tree_view_state *s = &view->state.tree;
4199 struct got_tree_entry *entry = NULL, *te;
4201 if (!view->searching) {
4202 view->search_next_done = 1;
4203 return NULL;
4206 if (s->matched_entry) {
4207 if (view->searching == TOG_SEARCH_FORWARD) {
4208 if (s->selected_entry)
4209 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4210 else
4211 entry = SIMPLEQ_FIRST(&s->entries->head);
4213 else {
4214 if (s->selected_entry == NULL) {
4215 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4216 entry = te;
4217 } else {
4218 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4219 entry = te;
4220 if (SIMPLEQ_NEXT(te, entry) ==
4221 s->selected_entry)
4222 break;
4226 } else {
4227 if (view->searching == TOG_SEARCH_FORWARD)
4228 entry = SIMPLEQ_FIRST(&s->entries->head);
4229 else {
4230 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4231 entry = te;
4235 while (1) {
4236 if (entry == NULL) {
4237 if (s->matched_entry == NULL) {
4238 view->search_next_done = 1;
4239 return NULL;
4241 if (view->searching == TOG_SEARCH_FORWARD)
4242 entry = SIMPLEQ_FIRST(&s->entries->head);
4243 else {
4244 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4245 entry = te;
4249 if (match_tree_entry(entry, &view->regex)) {
4250 view->search_next_done = 1;
4251 s->matched_entry = entry;
4252 break;
4255 if (view->searching == TOG_SEARCH_FORWARD)
4256 entry = SIMPLEQ_NEXT(entry, entry);
4257 else {
4258 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4259 entry = NULL;
4260 else {
4261 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4262 if (SIMPLEQ_NEXT(te, entry) == entry) {
4263 entry = te;
4264 break;
4271 if (s->matched_entry) {
4272 s->first_displayed_entry = s->matched_entry;
4273 s->selected = 0;
4276 return NULL;
4279 static const struct got_error *
4280 show_tree_view(struct tog_view *view)
4282 const struct got_error *err = NULL;
4283 struct tog_tree_view_state *s = &view->state.tree;
4284 char *parent_path;
4286 err = tree_entry_path(&parent_path, &s->parents, NULL);
4287 if (err)
4288 return err;
4290 err = draw_tree_entries(view, &s->first_displayed_entry,
4291 &s->last_displayed_entry, &s->selected_entry,
4292 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4293 s->entries, s->selected, view->nlines, s->tree == s->root);
4294 free(parent_path);
4296 view_vborder(view);
4297 return err;
4300 static const struct got_error *
4301 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4302 struct tog_view **focus_view, struct tog_view *view, int ch)
4304 const struct got_error *err = NULL;
4305 struct tog_tree_view_state *s = &view->state.tree;
4306 struct tog_view *log_view;
4307 int begin_x = 0, nscrolled;
4309 switch (ch) {
4310 case 'i':
4311 s->show_ids = !s->show_ids;
4312 break;
4313 case 'l':
4314 if (!s->selected_entry)
4315 break;
4316 if (view_is_parent_view(view))
4317 begin_x = view_split_begin_x(view->begin_x);
4318 err = log_tree_entry(&log_view, begin_x,
4319 s->selected_entry, &s->parents,
4320 s->commit_id, s->refs, s->repo);
4321 if (view_is_parent_view(view)) {
4322 err = view_close_child(view);
4323 if (err)
4324 return err;
4325 err = view_set_child(view, log_view);
4326 if (err) {
4327 view_close(log_view);
4328 break;
4330 *focus_view = log_view;
4331 view->child_focussed = 1;
4332 } else
4333 *new_view = log_view;
4334 break;
4335 case 'k':
4336 case KEY_UP:
4337 if (s->selected > 0) {
4338 s->selected--;
4339 if (s->selected == 0)
4340 break;
4342 if (s->selected > 0)
4343 break;
4344 tree_scroll_up(view, &s->first_displayed_entry, 1,
4345 s->entries, s->tree == s->root);
4346 break;
4347 case KEY_PPAGE:
4348 tree_scroll_up(view, &s->first_displayed_entry,
4349 MAX(0, view->nlines - 4 - s->selected), s->entries,
4350 s->tree == s->root);
4351 s->selected = 0;
4352 if (SIMPLEQ_FIRST(&s->entries->head) ==
4353 s->first_displayed_entry && s->tree != s->root)
4354 s->first_displayed_entry = NULL;
4355 break;
4356 case 'j':
4357 case KEY_DOWN:
4358 if (s->selected < s->ndisplayed - 1) {
4359 s->selected++;
4360 break;
4362 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4363 /* can't scroll any further */
4364 break;
4365 tree_scroll_down(&s->first_displayed_entry, 1,
4366 s->last_displayed_entry, s->entries);
4367 break;
4368 case KEY_NPAGE:
4369 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4370 == NULL) {
4371 /* can't scroll any further; move cursor down */
4372 if (s->selected < s->ndisplayed - 1)
4373 s->selected = s->ndisplayed - 1;
4374 break;
4376 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4377 view->nlines, s->last_displayed_entry, s->entries);
4378 if (nscrolled < view->nlines) {
4379 int ndisplayed = 0;
4380 struct got_tree_entry *te;
4381 te = s->first_displayed_entry;
4382 do {
4383 ndisplayed++;
4384 te = SIMPLEQ_NEXT(te, entry);
4385 } while (te);
4386 s->selected = ndisplayed - 1;
4388 break;
4389 case KEY_ENTER:
4390 case '\r':
4391 case KEY_BACKSPACE:
4392 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4393 struct tog_parent_tree *parent;
4394 /* user selected '..' */
4395 if (s->tree == s->root)
4396 break;
4397 parent = TAILQ_FIRST(&s->parents);
4398 TAILQ_REMOVE(&s->parents, parent,
4399 entry);
4400 got_object_tree_close(s->tree);
4401 s->tree = parent->tree;
4402 s->entries =
4403 got_object_tree_get_entries(s->tree);
4404 s->first_displayed_entry =
4405 parent->first_displayed_entry;
4406 s->selected_entry =
4407 parent->selected_entry;
4408 s->selected = parent->selected;
4409 free(parent);
4410 } else if (S_ISDIR(s->selected_entry->mode)) {
4411 struct got_tree_object *subtree;
4412 err = got_object_open_as_tree(&subtree,
4413 s->repo, s->selected_entry->id);
4414 if (err)
4415 break;
4416 err = tree_view_visit_subtree(subtree, s);
4417 if (err) {
4418 got_object_tree_close(subtree);
4419 break;
4421 } else if (S_ISREG(s->selected_entry->mode)) {
4422 struct tog_view *blame_view;
4423 int begin_x = view_is_parent_view(view) ?
4424 view_split_begin_x(view->begin_x) : 0;
4426 err = blame_tree_entry(&blame_view, begin_x,
4427 s->selected_entry, &s->parents,
4428 s->commit_id, s->refs, s->repo);
4429 if (err)
4430 break;
4431 if (view_is_parent_view(view)) {
4432 err = view_close_child(view);
4433 if (err)
4434 return err;
4435 err = view_set_child(view, blame_view);
4436 if (err) {
4437 view_close(blame_view);
4438 break;
4440 *focus_view = blame_view;
4441 view->child_focussed = 1;
4442 } else
4443 *new_view = blame_view;
4445 break;
4446 case KEY_RESIZE:
4447 if (s->selected > view->nlines)
4448 s->selected = s->ndisplayed - 1;
4449 break;
4450 default:
4451 break;
4454 return err;
4457 __dead static void
4458 usage_tree(void)
4460 endwin();
4461 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4462 getprogname());
4463 exit(1);
4466 static const struct got_error *
4467 cmd_tree(int argc, char *argv[])
4469 const struct got_error *error;
4470 struct got_repository *repo = NULL;
4471 struct got_reflist_head refs;
4472 char *repo_path = NULL;
4473 struct got_object_id *commit_id = NULL;
4474 char *commit_id_arg = NULL;
4475 struct got_commit_object *commit = NULL;
4476 struct got_tree_object *tree = NULL;
4477 int ch;
4478 struct tog_view *view;
4480 SIMPLEQ_INIT(&refs);
4482 #ifndef PROFILE
4483 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4484 NULL) == -1)
4485 err(1, "pledge");
4486 #endif
4488 while ((ch = getopt(argc, argv, "c:")) != -1) {
4489 switch (ch) {
4490 case 'c':
4491 commit_id_arg = optarg;
4492 break;
4493 default:
4494 usage_tree();
4495 /* NOTREACHED */
4499 argc -= optind;
4500 argv += optind;
4502 if (argc == 0) {
4503 struct got_worktree *worktree;
4504 char *cwd = getcwd(NULL, 0);
4505 if (cwd == NULL)
4506 return got_error_from_errno("getcwd");
4507 error = got_worktree_open(&worktree, cwd);
4508 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4509 goto done;
4510 if (worktree) {
4511 free(cwd);
4512 repo_path =
4513 strdup(got_worktree_get_repo_path(worktree));
4514 got_worktree_close(worktree);
4515 } else
4516 repo_path = cwd;
4517 if (repo_path == NULL) {
4518 error = got_error_from_errno("strdup");
4519 goto done;
4521 } else if (argc == 1) {
4522 repo_path = realpath(argv[0], NULL);
4523 if (repo_path == NULL)
4524 return got_error_from_errno2("realpath", argv[0]);
4525 } else
4526 usage_log();
4528 init_curses();
4530 error = got_repo_open(&repo, repo_path);
4531 if (error != NULL)
4532 goto done;
4534 error = apply_unveil(got_repo_get_path(repo), NULL);
4535 if (error)
4536 goto done;
4538 if (commit_id_arg == NULL)
4539 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4540 else {
4541 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4542 if (error) {
4543 if (error->code != GOT_ERR_NOT_REF)
4544 goto done;
4545 error = got_repo_match_object_id_prefix(&commit_id,
4546 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4549 if (error != NULL)
4550 goto done;
4552 error = got_object_open_as_commit(&commit, repo, commit_id);
4553 if (error != NULL)
4554 goto done;
4556 error = got_object_open_as_tree(&tree, repo,
4557 got_object_commit_get_tree_id(commit));
4558 if (error != NULL)
4559 goto done;
4561 error = got_ref_list(&refs, repo);
4562 if (error)
4563 goto done;
4565 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4566 if (view == NULL) {
4567 error = got_error_from_errno("view_open");
4568 goto done;
4570 error = open_tree_view(view, tree, commit_id, &refs, repo);
4571 if (error)
4572 goto done;
4573 error = view_loop(view);
4574 done:
4575 free(repo_path);
4576 free(commit_id);
4577 if (commit)
4578 got_object_commit_close(commit);
4579 if (tree)
4580 got_object_tree_close(tree);
4581 if (repo)
4582 got_repo_close(repo);
4583 got_ref_list_free(&refs);
4584 return error;
4587 static void
4588 list_commands(void)
4590 int i;
4592 fprintf(stderr, "commands:");
4593 for (i = 0; i < nitems(tog_commands); i++) {
4594 struct tog_cmd *cmd = &tog_commands[i];
4595 fprintf(stderr, " %s", cmd->name);
4597 fputc('\n', stderr);
4600 __dead static void
4601 usage(int hflag)
4603 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4604 getprogname());
4605 if (hflag)
4606 list_commands();
4607 exit(1);
4610 static char **
4611 make_argv(const char *arg0, const char *arg1)
4613 char **argv;
4614 int argc = (arg1 == NULL ? 1 : 2);
4616 argv = calloc(argc, sizeof(char *));
4617 if (argv == NULL)
4618 err(1, "calloc");
4619 argv[0] = strdup(arg0);
4620 if (argv[0] == NULL)
4621 err(1, "calloc");
4622 if (arg1) {
4623 argv[1] = strdup(arg1);
4624 if (argv[1] == NULL)
4625 err(1, "calloc");
4628 return argv;
4631 int
4632 main(int argc, char *argv[])
4634 const struct got_error *error = NULL;
4635 struct tog_cmd *cmd = NULL;
4636 int ch, hflag = 0, Vflag = 0;
4637 char **cmd_argv = NULL;
4639 setlocale(LC_CTYPE, "");
4641 while ((ch = getopt(argc, argv, "hV")) != -1) {
4642 switch (ch) {
4643 case 'h':
4644 hflag = 1;
4645 break;
4646 case 'V':
4647 Vflag = 1;
4648 break;
4649 default:
4650 usage(hflag);
4651 /* NOTREACHED */
4655 argc -= optind;
4656 argv += optind;
4657 optind = 0;
4658 optreset = 1;
4660 if (Vflag) {
4661 got_version_print_str();
4662 return 1;
4665 if (argc == 0) {
4666 if (hflag)
4667 usage(hflag);
4668 /* Build an argument vector which runs a default command. */
4669 cmd = &tog_commands[0];
4670 cmd_argv = make_argv(cmd->name, NULL);
4671 argc = 1;
4672 } else {
4673 int i;
4675 /* Did the user specific a command? */
4676 for (i = 0; i < nitems(tog_commands); i++) {
4677 if (strncmp(tog_commands[i].name, argv[0],
4678 strlen(argv[0])) == 0) {
4679 cmd = &tog_commands[i];
4680 break;
4684 if (cmd == NULL) {
4685 fprintf(stderr, "%s: unknown command '%s'\n",
4686 getprogname(), argv[0]);
4687 list_commands();
4688 return 1;
4692 if (hflag)
4693 cmd->cmd_usage();
4694 else
4695 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4697 endwin();
4698 free(cmd_argv);
4699 if (error)
4700 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4701 return 0;