Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
39 #include <regex.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_commit_graph.h"
49 #include "got_utf8.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
52 #include "got_path.h"
53 #include "got_worktree.h"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 #ifndef MAX
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 #endif
63 #define CTRL(x) ((x) & 0x1f)
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 struct tog_cmd {
70 const char *name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 };
75 __dead static void usage(int);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
86 static struct tog_cmd tog_commands[] = {
87 { "log", cmd_log, usage_log },
88 { "diff", cmd_diff, usage_diff },
89 { "blame", cmd_blame, usage_blame },
90 { "tree", cmd_tree, usage_tree },
91 };
93 enum tog_view_type {
94 TOG_VIEW_DIFF,
95 TOG_VIEW_LOG,
96 TOG_VIEW_BLAME,
97 TOG_VIEW_TREE
98 };
100 #define TOG_EOF_STRING "(END)"
102 struct commit_queue_entry {
103 TAILQ_ENTRY(commit_queue_entry) entry;
104 struct got_object_id *id;
105 struct got_commit_object *commit;
106 int idx;
107 };
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
110 int ncommits;
111 struct commit_queue_head head;
112 };
114 struct tog_diff_view_state {
115 struct got_object_id *id1, *id2;
116 FILE *f;
117 int first_displayed_line;
118 int last_displayed_line;
119 int eof;
120 int diff_context;
121 struct got_repository *repo;
122 struct got_reflist_head *refs;
124 /* passed from log view; may be NULL */
125 struct tog_view *log_view;
126 };
128 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 struct tog_log_thread_args {
131 pthread_cond_t need_commits;
132 int commits_needed;
133 struct got_commit_graph *graph;
134 struct commit_queue *commits;
135 const char *in_repo_path;
136 struct got_object_id *start_id;
137 struct got_repository *repo;
138 int log_complete;
139 sig_atomic_t *quit;
140 struct tog_view *view;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
143 };
145 struct tog_log_view_state {
146 struct commit_queue commits;
147 struct commit_queue_entry *first_displayed_entry;
148 struct commit_queue_entry *last_displayed_entry;
149 struct commit_queue_entry *selected_entry;
150 int selected;
151 char *in_repo_path;
152 const char *head_ref_name;
153 struct got_repository *repo;
154 struct got_reflist_head *refs;
155 struct got_object_id *start_id;
156 sig_atomic_t quit;
157 pthread_t thread;
158 struct tog_log_thread_args thread_args;
159 struct commit_queue_entry *matched_entry;
160 struct commit_queue_entry *search_entry;
161 };
163 struct tog_blame_cb_args {
164 struct tog_blame_line *lines; /* one per line */
165 int nlines;
167 struct tog_view *view;
168 struct got_object_id *commit_id;
169 int *quit;
170 };
172 struct tog_blame_thread_args {
173 const char *path;
174 struct got_repository *repo;
175 struct tog_blame_cb_args *cb_args;
176 int *complete;
177 };
179 struct tog_blame {
180 FILE *f;
181 size_t filesize;
182 struct tog_blame_line *lines;
183 int nlines;
184 off_t *line_offsets;
185 pthread_t thread;
186 struct tog_blame_thread_args thread_args;
187 struct tog_blame_cb_args cb_args;
188 const char *path;
189 };
191 struct tog_blame_view_state {
192 int first_displayed_line;
193 int last_displayed_line;
194 int selected_line;
195 int blame_complete;
196 int eof;
197 int done;
198 struct got_object_id_queue blamed_commits;
199 struct got_object_qid *blamed_commit;
200 char *path;
201 struct got_repository *repo;
202 struct got_reflist_head *refs;
203 struct got_object_id *commit_id;
204 struct tog_blame blame;
205 int matched_line;
206 };
208 struct tog_parent_tree {
209 TAILQ_ENTRY(tog_parent_tree) entry;
210 struct got_tree_object *tree;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *selected_entry;
213 int selected;
214 };
216 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 struct tog_tree_view_state {
219 char *tree_label;
220 struct got_tree_object *root;
221 struct got_tree_object *tree;
222 const struct got_tree_entries *entries;
223 struct got_tree_entry *first_displayed_entry;
224 struct got_tree_entry *last_displayed_entry;
225 struct got_tree_entry *selected_entry;
226 int ndisplayed, selected, show_ids;
227 struct tog_parent_trees parents;
228 struct got_object_id *commit_id;
229 struct got_repository *repo;
230 struct got_reflist_head *refs;
231 struct got_tree_entry *matched_entry;
232 };
234 /*
235 * We implement two types of views: parent views and child views.
237 * The 'Tab' key switches between a parent view and its child view.
238 * Child views are shown side-by-side to their parent view, provided
239 * there is enough screen estate.
241 * When a new view is opened from within a parent view, this new view
242 * becomes a child view of the parent view, replacing any existing child.
244 * When a new view is opened from within a child view, this new view
245 * becomes a parent view which will obscure the views below until the
246 * user quits the new parent view by typing 'q'.
248 * This list of views contains parent views only.
249 * Child views are only pointed to by their parent view.
250 */
251 TAILQ_HEAD(tog_view_list_head, tog_view);
253 struct tog_view {
254 TAILQ_ENTRY(tog_view) entry;
255 WINDOW *window;
256 PANEL *panel;
257 int nlines, ncols, begin_y, begin_x;
258 int lines, cols; /* copies of LINES and COLS */
259 int focussed;
260 struct tog_view *parent;
261 struct tog_view *child;
262 int child_focussed;
264 /* type-specific state */
265 enum tog_view_type type;
266 union {
267 struct tog_diff_view_state diff;
268 struct tog_log_view_state log;
269 struct tog_blame_view_state blame;
270 struct tog_tree_view_state tree;
271 } state;
273 const struct got_error *(*show)(struct tog_view *);
274 const struct got_error *(*input)(struct tog_view **,
275 struct tog_view **, struct tog_view**, struct tog_view *, int);
276 const struct got_error *(*close)(struct tog_view *);
278 const struct got_error *(*search_start)(struct tog_view *);
279 const struct got_error *(*search_next)(struct tog_view *);
280 int searching;
281 #define TOG_SEARCH_FORWARD 1
282 #define TOG_SEARCH_BACKWARD 2
283 int search_next_done;
284 regex_t regex;
285 };
287 static const struct got_error *open_diff_view(struct tog_view *,
288 struct got_object_id *, struct got_object_id *, struct tog_view *,
289 struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_diff_view(struct tog_view *);
291 static const struct got_error *input_diff_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error* close_diff_view(struct tog_view *);
295 static const struct got_error *open_log_view(struct tog_view *,
296 struct got_object_id *, struct got_reflist_head *,
297 struct got_repository *, const char *, const char *, int);
298 static const struct got_error * show_log_view(struct tog_view *);
299 static const struct got_error *input_log_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_log_view(struct tog_view *);
302 static const struct got_error *search_start_log_view(struct tog_view *);
303 static const struct got_error *search_next_log_view(struct tog_view *);
305 static const struct got_error *open_blame_view(struct tog_view *, char *,
306 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
307 static const struct got_error *show_blame_view(struct tog_view *);
308 static const struct got_error *input_blame_view(struct tog_view **,
309 struct tog_view **, struct tog_view **, struct tog_view *, int);
310 static const struct got_error *close_blame_view(struct tog_view *);
311 static const struct got_error *search_start_blame_view(struct tog_view *);
312 static const struct got_error *search_next_blame_view(struct tog_view *);
314 static const struct got_error *open_tree_view(struct tog_view *,
315 struct got_tree_object *, struct got_object_id *,
316 struct got_reflist_head *, struct got_repository *);
317 static const struct got_error *show_tree_view(struct tog_view *);
318 static const struct got_error *input_tree_view(struct tog_view **,
319 struct tog_view **, struct tog_view **, struct tog_view *, int);
320 static const struct got_error *close_tree_view(struct tog_view *);
321 static const struct got_error *search_start_tree_view(struct tog_view *);
322 static const struct got_error *search_next_tree_view(struct tog_view *);
324 static volatile sig_atomic_t tog_sigwinch_received;
325 static volatile sig_atomic_t tog_sigpipe_received;
327 static void
328 tog_sigwinch(int signo)
330 tog_sigwinch_received = 1;
333 static void
334 tog_sigpipe(int signo)
336 tog_sigpipe_received = 1;
339 static const struct got_error *
340 view_close(struct tog_view *view)
342 const struct got_error *err = NULL;
344 if (view->child) {
345 view_close(view->child);
346 view->child = NULL;
348 if (view->close)
349 err = view->close(view);
350 if (view->panel)
351 del_panel(view->panel);
352 if (view->window)
353 delwin(view->window);
354 free(view);
355 return err;
358 static struct tog_view *
359 view_open(int nlines, int ncols, int begin_y, int begin_x,
360 enum tog_view_type type)
362 struct tog_view *view = calloc(1, sizeof(*view));
364 if (view == NULL)
365 return NULL;
367 view->type = type;
368 view->lines = LINES;
369 view->cols = COLS;
370 view->nlines = nlines ? nlines : LINES - begin_y;
371 view->ncols = ncols ? ncols : COLS - begin_x;
372 view->begin_y = begin_y;
373 view->begin_x = begin_x;
374 view->window = newwin(nlines, ncols, begin_y, begin_x);
375 if (view->window == NULL) {
376 view_close(view);
377 return NULL;
379 view->panel = new_panel(view->window);
380 if (view->panel == NULL ||
381 set_panel_userptr(view->panel, view) != OK) {
382 view_close(view);
383 return NULL;
386 keypad(view->window, TRUE);
387 return view;
390 static int
391 view_split_begin_x(int begin_x)
393 if (begin_x > 0 || COLS < 120)
394 return 0;
395 return (COLS - MAX(COLS / 2, 80));
398 static const struct got_error *view_resize(struct tog_view *);
400 static const struct got_error *
401 view_splitscreen(struct tog_view *view)
403 const struct got_error *err = NULL;
405 view->begin_y = 0;
406 view->begin_x = view_split_begin_x(0);
407 view->nlines = LINES;
408 view->ncols = COLS - view->begin_x;
409 view->lines = LINES;
410 view->cols = COLS;
411 err = view_resize(view);
412 if (err)
413 return err;
415 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
416 return got_error_from_errno("mvwin");
418 return NULL;
421 static const struct got_error *
422 view_fullscreen(struct tog_view *view)
424 const struct got_error *err = NULL;
426 view->begin_x = 0;
427 view->begin_y = 0;
428 view->nlines = LINES;
429 view->ncols = COLS;
430 view->lines = LINES;
431 view->cols = COLS;
432 err = view_resize(view);
433 if (err)
434 return err;
436 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
437 return got_error_from_errno("mvwin");
439 return NULL;
442 static int
443 view_is_parent_view(struct tog_view *view)
445 return view->parent == NULL;
448 static const struct got_error *
449 view_resize(struct tog_view *view)
451 int nlines, ncols;
453 if (view->lines > LINES)
454 nlines = view->nlines - (view->lines - LINES);
455 else
456 nlines = view->nlines + (LINES - view->lines);
458 if (view->cols > COLS)
459 ncols = view->ncols - (view->cols - COLS);
460 else
461 ncols = view->ncols + (COLS - view->cols);
463 if (wresize(view->window, nlines, ncols) == ERR)
464 return got_error_from_errno("wresize");
465 if (replace_panel(view->panel, view->window) == ERR)
466 return got_error_from_errno("replace_panel");
467 wclear(view->window);
469 view->nlines = nlines;
470 view->ncols = ncols;
471 view->lines = LINES;
472 view->cols = COLS;
474 if (view->child) {
475 view->child->begin_x = view_split_begin_x(view->begin_x);
476 if (view->child->begin_x == 0) {
477 view_fullscreen(view->child);
478 if (view->child->focussed)
479 show_panel(view->child->panel);
480 else
481 show_panel(view->panel);
482 } else {
483 view_splitscreen(view->child);
484 show_panel(view->child->panel);
488 return NULL;
491 static const struct got_error *
492 view_close_child(struct tog_view *view)
494 const struct got_error *err = NULL;
496 if (view->child == NULL)
497 return NULL;
499 err = view_close(view->child);
500 view->child = NULL;
501 return err;
504 static const struct got_error *
505 view_set_child(struct tog_view *view, struct tog_view *child)
507 const struct got_error *err = NULL;
509 view->child = child;
510 child->parent = view;
511 return err;
514 static int
515 view_is_splitscreen(struct tog_view *view)
517 return view->begin_x > 0;
520 static void
521 tog_resizeterm(void)
523 int cols, lines;
524 struct winsize size;
526 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
527 cols = 80; /* Default */
528 lines = 24;
529 } else {
530 cols = size.ws_col;
531 lines = size.ws_row;
533 resize_term(lines, cols);
536 static const struct got_error *
537 view_search_start(struct tog_view *view)
539 const struct got_error *err = NULL;
540 char pattern[1024];
541 int ret;
542 int begin_x = 0;
544 if (view->nlines < 1)
545 return NULL;
547 if (!view_is_parent_view(view))
548 begin_x = view_split_begin_x(view->begin_x);
549 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
550 begin_x, "/");
551 wclrtoeol(view->window);
553 nocbreak();
554 echo();
555 ret = wgetnstr(view->window, pattern, sizeof(pattern));
556 cbreak();
557 noecho();
558 if (ret == ERR)
559 return NULL;
561 if (view->searching) {
562 regfree(&view->regex);
563 view->searching = 0;
566 if (regcomp(&view->regex, pattern,
567 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
568 err = view->search_start(view);
569 if (err) {
570 regfree(&view->regex);
571 return err;
573 view->searching = TOG_SEARCH_FORWARD;
574 view->search_next_done = 0;
575 view->search_next(view);
578 return NULL;
581 static const struct got_error *
582 view_input(struct tog_view **new, struct tog_view **dead,
583 struct tog_view **focus, int *done, struct tog_view *view,
584 struct tog_view_list_head *views)
586 const struct got_error *err = NULL;
587 struct tog_view *v;
588 int ch, errcode;
590 *new = NULL;
591 *dead = NULL;
592 *focus = NULL;
594 if (view->searching && !view->search_next_done) {
595 errcode = pthread_mutex_unlock(&tog_mutex);
596 if (errcode)
597 return got_error_set_errno(errcode,
598 "pthread_mutex_unlock");
599 pthread_yield();
600 errcode = pthread_mutex_lock(&tog_mutex);
601 if (errcode)
602 return got_error_set_errno(errcode,
603 "pthread_mutex_lock");
604 view->search_next(view);
605 return NULL;
608 nodelay(stdscr, FALSE);
609 /* Allow threads to make progress while we are waiting for input. */
610 errcode = pthread_mutex_unlock(&tog_mutex);
611 if (errcode)
612 return got_error_set_errno(errcode, "pthread_mutex_unlock");
613 ch = wgetch(view->window);
614 errcode = pthread_mutex_lock(&tog_mutex);
615 if (errcode)
616 return got_error_set_errno(errcode, "pthread_mutex_lock");
617 nodelay(stdscr, TRUE);
619 if (tog_sigwinch_received) {
620 tog_resizeterm();
621 tog_sigwinch_received = 0;
622 TAILQ_FOREACH(v, views, entry) {
623 err = view_resize(v);
624 if (err)
625 return err;
626 err = v->input(new, dead, focus, v, KEY_RESIZE);
627 if (err)
628 return err;
632 switch (ch) {
633 case ERR:
634 break;
635 case '\t':
636 if (view->child) {
637 *focus = view->child;
638 view->child_focussed = 1;
639 } else if (view->parent) {
640 *focus = view->parent;
641 view->parent->child_focussed = 0;
643 break;
644 case 'q':
645 err = view->input(new, dead, focus, view, ch);
646 *dead = view;
647 break;
648 case 'Q':
649 *done = 1;
650 break;
651 case 'f':
652 if (view_is_parent_view(view)) {
653 if (view->child == NULL)
654 break;
655 if (view_is_splitscreen(view->child)) {
656 *focus = view->child;
657 view->child_focussed = 1;
658 err = view_fullscreen(view->child);
659 } else
660 err = view_splitscreen(view->child);
661 if (err)
662 break;
663 err = view->child->input(new, dead, focus,
664 view->child, KEY_RESIZE);
665 } else {
666 if (view_is_splitscreen(view)) {
667 *focus = view;
668 view->parent->child_focussed = 1;
669 err = view_fullscreen(view);
670 } else {
671 err = view_splitscreen(view);
673 if (err)
674 break;
675 err = view->input(new, dead, focus, view,
676 KEY_RESIZE);
678 break;
679 case KEY_RESIZE:
680 break;
681 case '/':
682 if (view->search_start)
683 view_search_start(view);
684 else
685 err = view->input(new, dead, focus, view, ch);
686 break;
687 case 'N':
688 case 'n':
689 if (view->search_next && view->searching) {
690 view->searching = (ch == 'n' ?
691 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
692 view->search_next_done = 0;
693 view->search_next(view);
694 } else
695 err = view->input(new, dead, focus, view, ch);
696 break;
697 default:
698 err = view->input(new, dead, focus, view, ch);
699 break;
702 return err;
705 void
706 view_vborder(struct tog_view *view)
708 PANEL *panel;
709 struct tog_view *view_above;
711 if (view->parent)
712 return view_vborder(view->parent);
714 panel = panel_above(view->panel);
715 if (panel == NULL)
716 return;
718 view_above = panel_userptr(panel);
719 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
720 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
723 int
724 view_needs_focus_indication(struct tog_view *view)
726 if (view_is_parent_view(view)) {
727 if (view->child == NULL || view->child_focussed)
728 return 0;
729 if (!view_is_splitscreen(view->child))
730 return 0;
731 } else if (!view_is_splitscreen(view))
732 return 0;
734 return view->focussed;
737 static const struct got_error *
738 view_loop(struct tog_view *view)
740 const struct got_error *err = NULL;
741 struct tog_view_list_head views;
742 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
743 int fast_refresh = 10;
744 int done = 0, errcode;
746 errcode = pthread_mutex_lock(&tog_mutex);
747 if (errcode)
748 return got_error_set_errno(errcode, "pthread_mutex_lock");
750 TAILQ_INIT(&views);
751 TAILQ_INSERT_HEAD(&views, view, entry);
753 main_view = view;
754 view->focussed = 1;
755 err = view->show(view);
756 if (err)
757 return err;
758 update_panels();
759 doupdate();
760 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
761 /* Refresh fast during initialization, then become slower. */
762 if (fast_refresh && fast_refresh-- == 0)
763 halfdelay(10); /* switch to once per second */
765 err = view_input(&new_view, &dead_view, &focus_view, &done,
766 view, &views);
767 if (err)
768 break;
769 if (dead_view) {
770 struct tog_view *prev = NULL;
772 if (view_is_parent_view(dead_view))
773 prev = TAILQ_PREV(dead_view,
774 tog_view_list_head, entry);
775 else if (view->parent != dead_view)
776 prev = view->parent;
778 if (dead_view->parent)
779 dead_view->parent->child = NULL;
780 else
781 TAILQ_REMOVE(&views, dead_view, entry);
783 err = view_close(dead_view);
784 if (err || (dead_view == main_view && new_view == NULL))
785 goto done;
787 if (view == dead_view) {
788 if (focus_view)
789 view = focus_view;
790 else if (prev)
791 view = prev;
792 else if (!TAILQ_EMPTY(&views))
793 view = TAILQ_LAST(&views,
794 tog_view_list_head);
795 else
796 view = NULL;
797 if (view) {
798 if (view->child && view->child_focussed)
799 focus_view = view->child;
800 else
801 focus_view = view;
805 if (new_view) {
806 struct tog_view *v, *t;
807 /* Only allow one parent view per type. */
808 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
809 if (v->type != new_view->type)
810 continue;
811 TAILQ_REMOVE(&views, v, entry);
812 err = view_close(v);
813 if (err)
814 goto done;
815 break;
817 TAILQ_INSERT_TAIL(&views, new_view, entry);
818 view = new_view;
819 if (focus_view == NULL)
820 focus_view = new_view;
822 if (focus_view) {
823 show_panel(focus_view->panel);
824 if (view)
825 view->focussed = 0;
826 focus_view->focussed = 1;
827 view = focus_view;
828 if (new_view)
829 show_panel(new_view->panel);
830 if (view->child && view_is_splitscreen(view->child))
831 show_panel(view->child->panel);
833 if (view) {
834 if (focus_view == NULL) {
835 view->focussed = 1;
836 show_panel(view->panel);
837 if (view->child && view_is_splitscreen(view->child))
838 show_panel(view->child->panel);
839 focus_view = view;
841 if (view->parent) {
842 err = view->parent->show(view->parent);
843 if (err)
844 goto done;
846 err = view->show(view);
847 if (err)
848 goto done;
849 if (view->child) {
850 err = view->child->show(view->child);
851 if (err)
852 goto done;
854 update_panels();
855 doupdate();
858 done:
859 while (!TAILQ_EMPTY(&views)) {
860 view = TAILQ_FIRST(&views);
861 TAILQ_REMOVE(&views, view, entry);
862 view_close(view);
865 errcode = pthread_mutex_unlock(&tog_mutex);
866 if (errcode && err == NULL)
867 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
869 return err;
872 __dead static void
873 usage_log(void)
875 endwin();
876 fprintf(stderr,
877 "usage: %s log [-c commit] [-r repository-path] [path]\n",
878 getprogname());
879 exit(1);
882 /* Create newly allocated wide-character string equivalent to a byte string. */
883 static const struct got_error *
884 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
886 char *vis = NULL;
887 const struct got_error *err = NULL;
889 *ws = NULL;
890 *wlen = mbstowcs(NULL, s, 0);
891 if (*wlen == (size_t)-1) {
892 int vislen;
893 if (errno != EILSEQ)
894 return got_error_from_errno("mbstowcs");
896 /* byte string invalid in current encoding; try to "fix" it */
897 err = got_mbsavis(&vis, &vislen, s);
898 if (err)
899 return err;
900 *wlen = mbstowcs(NULL, vis, 0);
901 if (*wlen == (size_t)-1) {
902 err = got_error_from_errno("mbstowcs"); /* give up */
903 goto done;
907 *ws = calloc(*wlen + 1, sizeof(*ws));
908 if (*ws == NULL) {
909 err = got_error_from_errno("calloc");
910 goto done;
913 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
914 err = got_error_from_errno("mbstowcs");
915 done:
916 free(vis);
917 if (err) {
918 free(*ws);
919 *ws = NULL;
920 *wlen = 0;
922 return err;
925 /* Format a line for display, ensuring that it won't overflow a width limit. */
926 static const struct got_error *
927 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
929 const struct got_error *err = NULL;
930 int cols = 0;
931 wchar_t *wline = NULL;
932 size_t wlen;
933 int i;
935 *wlinep = NULL;
936 *widthp = 0;
938 err = mbs2ws(&wline, &wlen, line);
939 if (err)
940 return err;
942 i = 0;
943 while (i < wlen && cols < wlimit) {
944 int width = wcwidth(wline[i]);
945 switch (width) {
946 case 0:
947 i++;
948 break;
949 case 1:
950 case 2:
951 if (cols + width <= wlimit)
952 cols += width;
953 i++;
954 break;
955 case -1:
956 if (wline[i] == L'\t')
957 cols += TABSIZE - ((cols + 1) % TABSIZE);
958 i++;
959 break;
960 default:
961 err = got_error_from_errno("wcwidth");
962 goto done;
965 wline[i] = L'\0';
966 if (widthp)
967 *widthp = cols;
968 done:
969 if (err)
970 free(wline);
971 else
972 *wlinep = wline;
973 return err;
976 static const struct got_error*
977 build_refs_str(char **refs_str, struct got_reflist_head *refs,
978 struct got_object_id *id, struct got_repository *repo)
980 static const struct got_error *err = NULL;
981 struct got_reflist_entry *re;
982 char *s;
983 const char *name;
985 *refs_str = NULL;
987 SIMPLEQ_FOREACH(re, refs, entry) {
988 struct got_tag_object *tag = NULL;
989 int cmp;
991 name = got_ref_get_name(re->ref);
992 if (strcmp(name, GOT_REF_HEAD) == 0)
993 continue;
994 if (strncmp(name, "refs/", 5) == 0)
995 name += 5;
996 if (strncmp(name, "got/", 4) == 0)
997 continue;
998 if (strncmp(name, "heads/", 6) == 0)
999 name += 6;
1000 if (strncmp(name, "remotes/", 8) == 0)
1001 name += 8;
1002 if (strncmp(name, "tags/", 5) == 0) {
1003 err = got_object_open_as_tag(&tag, repo, re->id);
1004 if (err) {
1005 if (err->code != GOT_ERR_OBJ_TYPE)
1006 break;
1007 /* Ref points at something other than a tag. */
1008 err = NULL;
1009 tag = NULL;
1012 cmp = got_object_id_cmp(tag ?
1013 got_object_tag_get_object_id(tag) : re->id, id);
1014 if (tag)
1015 got_object_tag_close(tag);
1016 if (cmp != 0)
1017 continue;
1018 s = *refs_str;
1019 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1020 s ? ", " : "", name) == -1) {
1021 err = got_error_from_errno("asprintf");
1022 free(s);
1023 *refs_str = NULL;
1024 break;
1026 free(s);
1029 return err;
1032 static const struct got_error *
1033 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1035 char *smallerthan, *at;
1037 smallerthan = strchr(author, '<');
1038 if (smallerthan && smallerthan[1] != '\0')
1039 author = smallerthan + 1;
1040 at = strchr(author, '@');
1041 if (at)
1042 *at = '\0';
1043 return format_line(wauthor, author_width, author, limit);
1046 static const struct got_error *
1047 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1048 struct got_object_id *id, struct got_reflist_head *refs,
1049 int author_display_cols)
1051 const struct got_error *err = NULL;
1052 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1053 char *logmsg0 = NULL, *logmsg = NULL;
1054 char *author = NULL;
1055 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1056 int author_width, logmsg_width;
1057 char *newline, *line = NULL;
1058 int col, limit;
1059 static const size_t date_display_cols = 9;
1060 const int avail = view->ncols;
1061 struct tm tm;
1062 time_t committer_time;
1064 committer_time = got_object_commit_get_committer_time(commit);
1065 if (localtime_r(&committer_time, &tm) == NULL)
1066 return got_error_from_errno("localtime_r");
1067 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1068 >= sizeof(datebuf))
1069 return got_error(GOT_ERR_NO_SPACE);
1071 if (avail < date_display_cols)
1072 limit = MIN(sizeof(datebuf) - 1, avail);
1073 else
1074 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1075 waddnstr(view->window, datebuf, limit);
1076 col = limit + 1;
1077 if (col > avail)
1078 goto done;
1080 author = strdup(got_object_commit_get_author(commit));
1081 if (author == NULL) {
1082 err = got_error_from_errno("strdup");
1083 goto done;
1085 err = format_author(&wauthor, &author_width, author, avail - col);
1086 if (err)
1087 goto done;
1088 waddwstr(view->window, wauthor);
1089 col += author_width;
1090 while (col <= avail && author_width < author_display_cols + 2) {
1091 waddch(view->window, ' ');
1092 col++;
1093 author_width++;
1095 if (col > avail)
1096 goto done;
1098 err = got_object_commit_get_logmsg(&logmsg0, commit);
1099 if (err)
1100 goto done;
1101 logmsg = logmsg0;
1102 while (*logmsg == '\n')
1103 logmsg++;
1104 newline = strchr(logmsg, '\n');
1105 if (newline)
1106 *newline = '\0';
1107 limit = avail - col;
1108 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1109 if (err)
1110 goto done;
1111 waddwstr(view->window, wlogmsg);
1112 col += logmsg_width;
1113 while (col <= avail) {
1114 waddch(view->window, ' ');
1115 col++;
1117 done:
1118 free(logmsg0);
1119 free(wlogmsg);
1120 free(author);
1121 free(wauthor);
1122 free(line);
1123 return err;
1126 static struct commit_queue_entry *
1127 alloc_commit_queue_entry(struct got_commit_object *commit,
1128 struct got_object_id *id)
1130 struct commit_queue_entry *entry;
1132 entry = calloc(1, sizeof(*entry));
1133 if (entry == NULL)
1134 return NULL;
1136 entry->id = id;
1137 entry->commit = commit;
1138 return entry;
1141 static void
1142 pop_commit(struct commit_queue *commits)
1144 struct commit_queue_entry *entry;
1146 entry = TAILQ_FIRST(&commits->head);
1147 TAILQ_REMOVE(&commits->head, entry, entry);
1148 got_object_commit_close(entry->commit);
1149 commits->ncommits--;
1150 /* Don't free entry->id! It is owned by the commit graph. */
1151 free(entry);
1154 static void
1155 free_commits(struct commit_queue *commits)
1157 while (!TAILQ_EMPTY(&commits->head))
1158 pop_commit(commits);
1161 static const struct got_error *
1162 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1163 int minqueue, struct got_repository *repo, const char *path)
1165 const struct got_error *err = NULL;
1166 int nqueued = 0;
1169 * We keep all commits open throughout the lifetime of the log
1170 * view in order to avoid having to re-fetch commits from disk
1171 * while updating the display.
1173 while (nqueued < minqueue) {
1174 struct got_object_id *id;
1175 struct got_commit_object *commit;
1176 struct commit_queue_entry *entry;
1177 int errcode;
1179 err = got_commit_graph_iter_next(&id, graph);
1180 if (err) {
1181 if (err->code != GOT_ERR_ITER_NEED_MORE)
1182 break;
1183 err = got_commit_graph_fetch_commits(graph,
1184 minqueue, repo);
1185 if (err)
1186 return err;
1187 continue;
1190 if (id == NULL)
1191 break;
1193 err = got_object_open_as_commit(&commit, repo, id);
1194 if (err)
1195 break;
1196 entry = alloc_commit_queue_entry(commit, id);
1197 if (entry == NULL) {
1198 err = got_error_from_errno("alloc_commit_queue_entry");
1199 break;
1202 errcode = pthread_mutex_lock(&tog_mutex);
1203 if (errcode) {
1204 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1205 break;
1208 entry->idx = commits->ncommits;
1209 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1210 nqueued++;
1211 commits->ncommits++;
1213 errcode = pthread_mutex_unlock(&tog_mutex);
1214 if (errcode && err == NULL)
1215 err = got_error_set_errno(errcode,
1216 "pthread_mutex_unlock");
1219 return err;
1222 static const struct got_error *
1223 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1224 struct got_repository *repo)
1226 const struct got_error *err = NULL;
1227 struct got_reference *head_ref;
1229 *head_id = NULL;
1231 err = got_ref_open(&head_ref, repo, branch_name, 0);
1232 if (err)
1233 return err;
1235 err = got_ref_resolve(head_id, repo, head_ref);
1236 got_ref_close(head_ref);
1237 if (err) {
1238 *head_id = NULL;
1239 return err;
1242 return NULL;
1245 static const struct got_error *
1246 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1247 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1248 struct commit_queue *commits, int selected_idx, int limit,
1249 struct got_reflist_head *refs, const char *path, int commits_needed)
1251 const struct got_error *err = NULL;
1252 struct tog_log_view_state *s = &view->state.log;
1253 struct commit_queue_entry *entry;
1254 int width;
1255 int ncommits, author_cols = 10;
1256 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1257 char *refs_str = NULL;
1258 wchar_t *wline;
1260 entry = first;
1261 ncommits = 0;
1262 while (entry) {
1263 if (ncommits == selected_idx) {
1264 *selected = entry;
1265 break;
1267 entry = TAILQ_NEXT(entry, entry);
1268 ncommits++;
1271 if (*selected && !(view->searching && view->search_next_done == 0)) {
1272 err = got_object_id_str(&id_str, (*selected)->id);
1273 if (err)
1274 return err;
1275 if (refs) {
1276 err = build_refs_str(&refs_str, refs, (*selected)->id,
1277 s->repo);
1278 if (err)
1279 goto done;
1283 if (commits_needed == 0)
1284 halfdelay(10); /* disable fast refresh */
1286 if (asprintf(&ncommits_str, " [%d/%d] %s",
1287 entry ? entry->idx + 1 : 0, commits->ncommits,
1288 commits_needed > 0 ?
1289 (view->searching && view->search_next_done == 0
1290 ? "searching..." : "loading... ") :
1291 (refs_str ? refs_str : "")) == -1) {
1292 err = got_error_from_errno("asprintf");
1293 goto done;
1296 if (path && strcmp(path, "/") != 0) {
1297 if (asprintf(&header, "commit %s %s%s",
1298 id_str ? id_str : "........................................",
1299 path, ncommits_str) == -1) {
1300 err = got_error_from_errno("asprintf");
1301 header = NULL;
1302 goto done;
1304 } else if (asprintf(&header, "commit %s%s",
1305 id_str ? id_str : "........................................",
1306 ncommits_str) == -1) {
1307 err = got_error_from_errno("asprintf");
1308 header = NULL;
1309 goto done;
1311 err = format_line(&wline, &width, header, view->ncols);
1312 if (err)
1313 goto done;
1315 werase(view->window);
1317 if (view_needs_focus_indication(view))
1318 wstandout(view->window);
1319 waddwstr(view->window, wline);
1320 while (width < view->ncols) {
1321 waddch(view->window, ' ');
1322 width++;
1324 if (view_needs_focus_indication(view))
1325 wstandend(view->window);
1326 free(wline);
1327 if (limit <= 1)
1328 goto done;
1330 /* Grow author column size if necessary. */
1331 entry = first;
1332 ncommits = 0;
1333 while (entry) {
1334 char *author;
1335 wchar_t *wauthor;
1336 int width;
1337 if (ncommits >= limit - 1)
1338 break;
1339 author = strdup(got_object_commit_get_author(entry->commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1342 goto done;
1344 err = format_author(&wauthor, &width, author, COLS);
1345 if (author_cols < width)
1346 author_cols = width;
1347 free(wauthor);
1348 free(author);
1349 entry = TAILQ_NEXT(entry, entry);
1352 entry = first;
1353 *last = first;
1354 ncommits = 0;
1355 while (entry) {
1356 if (ncommits >= limit - 1)
1357 break;
1358 if (ncommits == selected_idx)
1359 wstandout(view->window);
1360 err = draw_commit(view, entry->commit, entry->id, refs,
1361 author_cols);
1362 if (ncommits == selected_idx)
1363 wstandend(view->window);
1364 if (err)
1365 goto done;
1366 ncommits++;
1367 *last = entry;
1368 entry = TAILQ_NEXT(entry, entry);
1371 view_vborder(view);
1372 done:
1373 free(id_str);
1374 free(refs_str);
1375 free(ncommits_str);
1376 free(header);
1377 return err;
1380 static void
1381 scroll_up(struct tog_view *view,
1382 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1383 struct commit_queue *commits)
1385 struct commit_queue_entry *entry;
1386 int nscrolled = 0;
1388 entry = TAILQ_FIRST(&commits->head);
1389 if (*first_displayed_entry == entry)
1390 return;
1392 entry = *first_displayed_entry;
1393 while (entry && nscrolled < maxscroll) {
1394 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1395 if (entry) {
1396 *first_displayed_entry = entry;
1397 nscrolled++;
1402 static const struct got_error *
1403 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1404 pthread_cond_t *need_commits)
1406 int errcode;
1407 int max_wait = 20;
1409 halfdelay(1); /* fast refresh while loading commits */
1411 while (*commits_needed > 0) {
1412 if (*log_complete)
1413 break;
1415 /* Wake the log thread. */
1416 errcode = pthread_cond_signal(need_commits);
1417 if (errcode)
1418 return got_error_set_errno(errcode,
1419 "pthread_cond_signal");
1420 errcode = pthread_mutex_unlock(&tog_mutex);
1421 if (errcode)
1422 return got_error_set_errno(errcode,
1423 "pthread_mutex_unlock");
1424 pthread_yield();
1425 errcode = pthread_mutex_lock(&tog_mutex);
1426 if (errcode)
1427 return got_error_set_errno(errcode,
1428 "pthread_mutex_lock");
1430 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1432 * Thread is not done yet; lose a key press
1433 * and let the user retry... this way the GUI
1434 * remains interactive while logging deep paths
1435 * with few commits in history.
1437 return NULL;
1441 return NULL;
1444 static const struct got_error *
1445 scroll_down(struct tog_view *view,
1446 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1447 struct commit_queue_entry **last_displayed_entry,
1448 struct commit_queue *commits, int *log_complete, int *commits_needed,
1449 pthread_cond_t *need_commits)
1451 const struct got_error *err = NULL;
1452 struct commit_queue_entry *pentry;
1453 int nscrolled = 0;
1455 if (*last_displayed_entry == NULL)
1456 return NULL;
1458 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1459 if (pentry == NULL && !*log_complete) {
1461 * Ask the log thread for required amount of commits
1462 * plus some amount of pre-fetching.
1464 (*commits_needed) += maxscroll + 20;
1465 err = trigger_log_thread(0, commits_needed, log_complete,
1466 need_commits);
1467 if (err)
1468 return err;
1471 do {
1472 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1473 if (pentry == NULL)
1474 break;
1476 *last_displayed_entry = pentry;
1478 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1479 if (pentry == NULL)
1480 break;
1481 *first_displayed_entry = pentry;
1482 } while (++nscrolled < maxscroll);
1484 return err;
1487 static const struct got_error *
1488 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1489 struct got_commit_object *commit, struct got_object_id *commit_id,
1490 struct tog_view *log_view, struct got_reflist_head *refs,
1491 struct got_repository *repo)
1493 const struct got_error *err;
1494 struct got_object_qid *parent_id;
1495 struct tog_view *diff_view;
1497 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1498 if (diff_view == NULL)
1499 return got_error_from_errno("view_open");
1501 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1502 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1503 commit_id, log_view, refs, repo);
1504 if (err == NULL)
1505 *new_view = diff_view;
1506 return err;
1509 static const struct got_error *
1510 tree_view_visit_subtree(struct got_tree_object *subtree,
1511 struct tog_tree_view_state *s)
1513 struct tog_parent_tree *parent;
1515 parent = calloc(1, sizeof(*parent));
1516 if (parent == NULL)
1517 return got_error_from_errno("calloc");
1519 parent->tree = s->tree;
1520 parent->first_displayed_entry = s->first_displayed_entry;
1521 parent->selected_entry = s->selected_entry;
1522 parent->selected = s->selected;
1523 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1524 s->tree = subtree;
1525 s->entries = got_object_tree_get_entries(s->tree);
1526 s->selected = 0;
1527 s->first_displayed_entry = NULL;
1528 return NULL;
1532 static const struct got_error *
1533 browse_commit_tree(struct tog_view **new_view, int begin_x,
1534 struct commit_queue_entry *entry, const char *path,
1535 struct got_reflist_head *refs, struct got_repository *repo)
1537 const struct got_error *err = NULL;
1538 struct got_tree_object *tree;
1539 struct got_tree_entry *te;
1540 struct tog_tree_view_state *s;
1541 struct tog_view *tree_view;
1542 char *slash, *subpath = NULL;
1543 const char *p;
1545 err = got_object_open_as_tree(&tree, repo,
1546 got_object_commit_get_tree_id(entry->commit));
1547 if (err)
1548 return err;
1550 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1551 if (tree_view == NULL)
1552 return got_error_from_errno("view_open");
1554 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1555 if (err) {
1556 got_object_tree_close(tree);
1557 return err;
1559 s = &tree_view->state.tree;
1561 *new_view = tree_view;
1563 /* Walk the path and open corresponding tree objects. */
1564 p = path;
1565 while (p[0] == '/')
1566 p++;
1567 while (*p) {
1568 struct got_object_id *tree_id;
1570 /* Ensure the correct subtree entry is selected. */
1571 slash = strchr(p, '/');
1572 if (slash == NULL)
1573 slash = strchr(p, '\0');
1574 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1575 if (strncmp(p, te->name, slash - p) == 0) {
1576 s->selected_entry = te;
1577 break;
1579 s->selected++;
1581 if (s->selected_entry == NULL) {
1582 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1583 break;
1585 if (s->tree != s->root)
1586 s->selected++; /* skip '..' */
1588 if (!S_ISDIR(s->selected_entry->mode)) {
1589 /* Jump to this file's entry. */
1590 s->first_displayed_entry = s->selected_entry;
1591 s->selected = 0;
1592 break;
1595 slash = strchr(p, '/');
1596 if (slash)
1597 subpath = strndup(path, slash - path);
1598 else
1599 subpath = strdup(path);
1600 if (subpath == NULL) {
1601 err = got_error_from_errno("strdup");
1602 break;
1605 err = got_object_id_by_path(&tree_id, repo, entry->id,
1606 subpath);
1607 if (err)
1608 break;
1610 err = got_object_open_as_tree(&tree, repo, tree_id);
1611 free(tree_id);
1612 if (err)
1613 break;
1615 err = tree_view_visit_subtree(tree, s);
1616 if (err) {
1617 got_object_tree_close(tree);
1618 break;
1620 if (slash == NULL)
1621 break;
1622 free(subpath);
1623 subpath = NULL;
1624 p = slash;
1627 free(subpath);
1628 return err;
1631 static void *
1632 log_thread(void *arg)
1634 const struct got_error *err = NULL;
1635 int errcode = 0;
1636 struct tog_log_thread_args *a = arg;
1637 int done = 0;
1639 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1640 if (err)
1641 return (void *)err;
1643 while (!done && !err && !tog_sigpipe_received) {
1644 err = queue_commits(a->graph, a->commits, 1, a->repo,
1645 a->in_repo_path);
1646 if (err) {
1647 if (err->code != GOT_ERR_ITER_COMPLETED)
1648 return (void *)err;
1649 err = NULL;
1650 done = 1;
1651 } else if (a->commits_needed > 0)
1652 a->commits_needed--;
1654 errcode = pthread_mutex_lock(&tog_mutex);
1655 if (errcode) {
1656 err = got_error_set_errno(errcode,
1657 "pthread_mutex_lock");
1658 break;
1659 } else if (*a->quit)
1660 done = 1;
1661 else if (*a->first_displayed_entry == NULL) {
1662 *a->first_displayed_entry =
1663 TAILQ_FIRST(&a->commits->head);
1664 *a->selected_entry = *a->first_displayed_entry;
1667 if (done)
1668 a->commits_needed = 0;
1669 else if (a->commits_needed == 0) {
1670 errcode = pthread_cond_wait(&a->need_commits,
1671 &tog_mutex);
1672 if (errcode)
1673 err = got_error_set_errno(errcode,
1674 "pthread_cond_wait");
1677 errcode = pthread_mutex_unlock(&tog_mutex);
1678 if (errcode && err == NULL)
1679 err = got_error_set_errno(errcode,
1680 "pthread_mutex_unlock");
1682 a->log_complete = 1;
1683 return (void *)err;
1686 static const struct got_error *
1687 stop_log_thread(struct tog_log_view_state *s)
1689 const struct got_error *err = NULL;
1690 int errcode;
1692 if (s->thread) {
1693 s->quit = 1;
1694 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1695 if (errcode)
1696 return got_error_set_errno(errcode,
1697 "pthread_cond_signal");
1698 errcode = pthread_mutex_unlock(&tog_mutex);
1699 if (errcode)
1700 return got_error_set_errno(errcode,
1701 "pthread_mutex_unlock");
1702 errcode = pthread_join(s->thread, (void **)&err);
1703 if (errcode)
1704 return got_error_set_errno(errcode, "pthread_join");
1705 errcode = pthread_mutex_lock(&tog_mutex);
1706 if (errcode)
1707 return got_error_set_errno(errcode,
1708 "pthread_mutex_lock");
1709 s->thread = NULL;
1712 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1713 if (errcode && err == NULL)
1714 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1716 if (s->thread_args.repo) {
1717 got_repo_close(s->thread_args.repo);
1718 s->thread_args.repo = NULL;
1721 if (s->thread_args.graph) {
1722 got_commit_graph_close(s->thread_args.graph);
1723 s->thread_args.graph = NULL;
1726 return err;
1729 static const struct got_error *
1730 close_log_view(struct tog_view *view)
1732 const struct got_error *err = NULL;
1733 struct tog_log_view_state *s = &view->state.log;
1735 err = stop_log_thread(s);
1736 free_commits(&s->commits);
1737 free(s->in_repo_path);
1738 s->in_repo_path = NULL;
1739 free(s->start_id);
1740 s->start_id = NULL;
1741 return err;
1744 static const struct got_error *
1745 search_start_log_view(struct tog_view *view)
1747 struct tog_log_view_state *s = &view->state.log;
1749 s->matched_entry = NULL;
1750 s->search_entry = NULL;
1751 return NULL;
1754 static int
1755 match_commit(struct got_commit_object *commit, const char *id_str,
1756 const char *logmsg, regex_t *regex)
1758 regmatch_t regmatch;
1760 if (regexec(regex, got_object_commit_get_author(commit), 1,
1761 &regmatch, 0) == 0 ||
1762 regexec(regex, got_object_commit_get_committer(commit), 1,
1763 &regmatch, 0) == 0 ||
1764 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1765 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1766 return 1;
1768 return 0;
1771 static const struct got_error *
1772 search_next_log_view(struct tog_view *view)
1774 const struct got_error *err = NULL;
1775 struct tog_log_view_state *s = &view->state.log;
1776 struct commit_queue_entry *entry;
1778 if (!view->searching) {
1779 view->search_next_done = 1;
1780 return NULL;
1783 if (s->search_entry) {
1784 if (wgetch(view->window) == KEY_BACKSPACE) {
1785 view->search_next_done = 1;
1786 return NULL;
1788 if (view->searching == TOG_SEARCH_FORWARD)
1789 entry = TAILQ_NEXT(s->search_entry, entry);
1790 else
1791 entry = TAILQ_PREV(s->search_entry,
1792 commit_queue_head, entry);
1793 } else if (s->matched_entry) {
1794 if (view->searching == TOG_SEARCH_FORWARD)
1795 entry = TAILQ_NEXT(s->selected_entry, entry);
1796 else
1797 entry = TAILQ_PREV(s->selected_entry,
1798 commit_queue_head, entry);
1799 } else {
1800 if (view->searching == TOG_SEARCH_FORWARD)
1801 entry = TAILQ_FIRST(&s->commits.head);
1802 else
1803 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1806 while (1) {
1807 char *id_str, *logmsg;
1808 if (entry == NULL) {
1809 if (s->thread_args.log_complete ||
1810 view->searching == TOG_SEARCH_BACKWARD) {
1811 view->search_next_done = 1;
1812 return NULL;
1815 * Poke the log thread for more commits and return,
1816 * allowing the main loop to make progress. Search
1817 * will resume at s->search_entry once we come back.
1819 s->thread_args.commits_needed++;
1820 return trigger_log_thread(1,
1821 &s->thread_args.commits_needed,
1822 &s->thread_args.log_complete,
1823 &s->thread_args.need_commits);
1826 err = got_object_id_str(&id_str, entry->id);
1827 if (err)
1828 return err;
1830 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1831 if (err)
1832 return err;
1833 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1834 free(logmsg);
1835 view->search_next_done = 1;
1836 s->matched_entry = entry;
1837 free(id_str);
1838 break;
1840 free(logmsg);
1841 free(id_str);
1842 s->search_entry = entry;
1843 if (view->searching == TOG_SEARCH_FORWARD)
1844 entry = TAILQ_NEXT(entry, entry);
1845 else
1846 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1849 if (s->matched_entry) {
1850 int cur = s->selected_entry->idx;
1851 while (cur < s->matched_entry->idx) {
1852 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1853 if (err)
1854 return err;
1855 cur++;
1857 while (cur > s->matched_entry->idx) {
1858 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1859 if (err)
1860 return err;
1861 cur--;
1865 s->search_entry = NULL;
1867 return NULL;
1870 static const struct got_error *
1871 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1872 struct got_reflist_head *refs, struct got_repository *repo,
1873 const char *head_ref_name, const char *path, int check_disk)
1875 const struct got_error *err = NULL;
1876 struct tog_log_view_state *s = &view->state.log;
1877 struct got_repository *thread_repo = NULL;
1878 struct got_commit_graph *thread_graph = NULL;
1879 int errcode;
1881 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1882 if (err != NULL)
1883 goto done;
1885 /* The commit queue only contains commits being displayed. */
1886 TAILQ_INIT(&s->commits.head);
1887 s->commits.ncommits = 0;
1889 s->refs = refs;
1890 s->repo = repo;
1891 s->head_ref_name = head_ref_name;
1892 s->start_id = got_object_id_dup(start_id);
1893 if (s->start_id == NULL) {
1894 err = got_error_from_errno("got_object_id_dup");
1895 goto done;
1898 view->show = show_log_view;
1899 view->input = input_log_view;
1900 view->close = close_log_view;
1901 view->search_start = search_start_log_view;
1902 view->search_next = search_next_log_view;
1904 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1905 if (err)
1906 goto done;
1907 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1908 0, thread_repo);
1909 if (err)
1910 goto done;
1912 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1913 if (errcode) {
1914 err = got_error_set_errno(errcode, "pthread_cond_init");
1915 goto done;
1918 s->thread_args.commits_needed = view->nlines;
1919 s->thread_args.graph = thread_graph;
1920 s->thread_args.commits = &s->commits;
1921 s->thread_args.in_repo_path = s->in_repo_path;
1922 s->thread_args.start_id = s->start_id;
1923 s->thread_args.repo = thread_repo;
1924 s->thread_args.log_complete = 0;
1925 s->thread_args.quit = &s->quit;
1926 s->thread_args.view = view;
1927 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1928 s->thread_args.selected_entry = &s->selected_entry;
1929 done:
1930 if (err)
1931 close_log_view(view);
1932 return err;
1935 static const struct got_error *
1936 show_log_view(struct tog_view *view)
1938 struct tog_log_view_state *s = &view->state.log;
1940 if (s->thread == NULL) {
1941 int errcode = pthread_create(&s->thread, NULL, log_thread,
1942 &s->thread_args);
1943 if (errcode)
1944 return got_error_set_errno(errcode, "pthread_create");
1947 return draw_commits(view, &s->last_displayed_entry,
1948 &s->selected_entry, s->first_displayed_entry,
1949 &s->commits, s->selected, view->nlines, s->refs,
1950 s->in_repo_path, s->thread_args.commits_needed);
1953 static const struct got_error *
1954 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1955 struct tog_view **focus_view, struct tog_view *view, int ch)
1957 const struct got_error *err = NULL;
1958 struct tog_log_view_state *s = &view->state.log;
1959 char *parent_path, *in_repo_path = NULL;
1960 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1961 int begin_x = 0;
1962 struct got_object_id *start_id;
1964 switch (ch) {
1965 case 'q':
1966 s->quit = 1;
1967 break;
1968 case 'k':
1969 case KEY_UP:
1970 case '<':
1971 case ',':
1972 if (s->first_displayed_entry == NULL)
1973 break;
1974 if (s->selected > 0)
1975 s->selected--;
1976 else
1977 scroll_up(view, &s->first_displayed_entry, 1,
1978 &s->commits);
1979 break;
1980 case KEY_PPAGE:
1981 case CTRL('b'):
1982 if (s->first_displayed_entry == NULL)
1983 break;
1984 if (TAILQ_FIRST(&s->commits.head) ==
1985 s->first_displayed_entry) {
1986 s->selected = 0;
1987 break;
1989 scroll_up(view, &s->first_displayed_entry,
1990 view->nlines, &s->commits);
1991 break;
1992 case 'j':
1993 case KEY_DOWN:
1994 case '>':
1995 case '.':
1996 if (s->first_displayed_entry == NULL)
1997 break;
1998 if (s->selected < MIN(view->nlines - 2,
1999 s->commits.ncommits - 1)) {
2000 s->selected++;
2001 break;
2003 err = scroll_down(view, &s->first_displayed_entry, 1,
2004 &s->last_displayed_entry, &s->commits,
2005 &s->thread_args.log_complete,
2006 &s->thread_args.commits_needed,
2007 &s->thread_args.need_commits);
2008 break;
2009 case KEY_NPAGE:
2010 case CTRL('f'): {
2011 struct commit_queue_entry *first;
2012 first = s->first_displayed_entry;
2013 if (first == NULL)
2014 break;
2015 err = scroll_down(view, &s->first_displayed_entry,
2016 view->nlines, &s->last_displayed_entry,
2017 &s->commits, &s->thread_args.log_complete,
2018 &s->thread_args.commits_needed,
2019 &s->thread_args.need_commits);
2020 if (first == s->first_displayed_entry &&
2021 s->selected < MIN(view->nlines - 2,
2022 s->commits.ncommits - 1)) {
2023 /* can't scroll further down */
2024 s->selected = MIN(view->nlines - 2,
2025 s->commits.ncommits - 1);
2027 err = NULL;
2028 break;
2030 case KEY_RESIZE:
2031 if (s->selected > view->nlines - 2)
2032 s->selected = view->nlines - 2;
2033 if (s->selected > s->commits.ncommits - 1)
2034 s->selected = s->commits.ncommits - 1;
2035 break;
2036 case KEY_ENTER:
2037 case ' ':
2038 case '\r':
2039 if (s->selected_entry == NULL)
2040 break;
2041 if (view_is_parent_view(view))
2042 begin_x = view_split_begin_x(view->begin_x);
2043 err = open_diff_view_for_commit(&diff_view, begin_x,
2044 s->selected_entry->commit, s->selected_entry->id,
2045 view, s->refs, s->repo);
2046 if (err)
2047 break;
2048 if (view_is_parent_view(view)) {
2049 err = view_close_child(view);
2050 if (err)
2051 return err;
2052 err = view_set_child(view, diff_view);
2053 if (err) {
2054 view_close(diff_view);
2055 break;
2057 *focus_view = diff_view;
2058 view->child_focussed = 1;
2059 } else
2060 *new_view = diff_view;
2061 break;
2062 case 't':
2063 if (s->selected_entry == NULL)
2064 break;
2065 if (view_is_parent_view(view))
2066 begin_x = view_split_begin_x(view->begin_x);
2067 err = browse_commit_tree(&tree_view, begin_x,
2068 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2069 if (err)
2070 break;
2071 if (view_is_parent_view(view)) {
2072 err = view_close_child(view);
2073 if (err)
2074 return err;
2075 err = view_set_child(view, tree_view);
2076 if (err) {
2077 view_close(tree_view);
2078 break;
2080 *focus_view = tree_view;
2081 view->child_focussed = 1;
2082 } else
2083 *new_view = tree_view;
2084 break;
2085 case KEY_BACKSPACE:
2086 if (strcmp(s->in_repo_path, "/") == 0)
2087 break;
2088 parent_path = dirname(s->in_repo_path);
2089 if (parent_path && strcmp(parent_path, ".") != 0) {
2090 err = stop_log_thread(s);
2091 if (err)
2092 return err;
2093 lv = view_open(view->nlines, view->ncols,
2094 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2095 if (lv == NULL)
2096 return got_error_from_errno(
2097 "view_open");
2098 err = open_log_view(lv, s->start_id, s->refs,
2099 s->repo, s->head_ref_name, parent_path, 0);
2100 if (err)
2101 return err;;
2102 if (view_is_parent_view(view))
2103 *new_view = lv;
2104 else {
2105 view_set_child(view->parent, lv);
2106 *focus_view = lv;
2108 return NULL;
2110 break;
2111 case CTRL('l'):
2112 err = stop_log_thread(s);
2113 if (err)
2114 return err;
2115 lv = view_open(view->nlines, view->ncols,
2116 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2117 if (lv == NULL)
2118 return got_error_from_errno("view_open");
2119 err = get_head_commit_id(&start_id, s->head_ref_name ?
2120 s->head_ref_name : GOT_REF_HEAD, s->repo);
2121 if (err) {
2122 view_close(lv);
2123 return err;
2125 in_repo_path = strdup(s->in_repo_path);
2126 if (in_repo_path == NULL) {
2127 free(start_id);
2128 view_close(lv);
2129 return got_error_from_errno("strdup");
2131 err = open_log_view(lv, start_id, s->refs, s->repo,
2132 s->head_ref_name, in_repo_path, 0);
2133 if (err) {
2134 free(start_id);
2135 view_close(lv);
2136 return err;;
2138 *dead_view = view;
2139 *new_view = lv;
2140 break;
2141 default:
2142 break;
2145 return err;
2148 static const struct got_error *
2149 apply_unveil(const char *repo_path, const char *worktree_path)
2151 const struct got_error *error;
2153 #ifdef PROFILE
2154 if (unveil("gmon.out", "rwc") != 0)
2155 return got_error_from_errno2("unveil", "gmon.out");
2156 #endif
2157 if (repo_path && unveil(repo_path, "r") != 0)
2158 return got_error_from_errno2("unveil", repo_path);
2160 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2161 return got_error_from_errno2("unveil", worktree_path);
2163 if (unveil("/tmp", "rwc") != 0)
2164 return got_error_from_errno2("unveil", "/tmp");
2166 error = got_privsep_unveil_exec_helpers();
2167 if (error != NULL)
2168 return error;
2170 if (unveil(NULL, NULL) != 0)
2171 return got_error_from_errno("unveil");
2173 return NULL;
2176 static void
2177 init_curses(void)
2179 initscr();
2180 cbreak();
2181 halfdelay(1); /* Do fast refresh while initial view is loading. */
2182 noecho();
2183 nonl();
2184 intrflush(stdscr, FALSE);
2185 keypad(stdscr, TRUE);
2186 curs_set(0);
2187 signal(SIGWINCH, tog_sigwinch);
2188 signal(SIGPIPE, tog_sigpipe);
2191 static const struct got_error *
2192 cmd_log(int argc, char *argv[])
2194 const struct got_error *error;
2195 struct got_repository *repo = NULL;
2196 struct got_worktree *worktree = NULL;
2197 struct got_reflist_head refs;
2198 struct got_object_id *start_id = NULL;
2199 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2200 char *start_commit = NULL;
2201 int ch;
2202 struct tog_view *view;
2204 SIMPLEQ_INIT(&refs);
2206 #ifndef PROFILE
2207 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2208 NULL) == -1)
2209 err(1, "pledge");
2210 #endif
2212 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2213 switch (ch) {
2214 case 'c':
2215 start_commit = optarg;
2216 break;
2217 case 'r':
2218 repo_path = realpath(optarg, NULL);
2219 if (repo_path == NULL)
2220 err(1, "-r option");
2221 break;
2222 default:
2223 usage_log();
2224 /* NOTREACHED */
2228 argc -= optind;
2229 argv += optind;
2231 cwd = getcwd(NULL, 0);
2232 if (cwd == NULL) {
2233 error = got_error_from_errno("getcwd");
2234 goto done;
2236 error = got_worktree_open(&worktree, cwd);
2237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2238 goto done;
2239 error = NULL;
2241 if (argc == 0) {
2242 path = strdup("");
2243 if (path == NULL) {
2244 error = got_error_from_errno("strdup");
2245 goto done;
2247 } else if (argc == 1) {
2248 if (worktree) {
2249 error = got_worktree_resolve_path(&path, worktree,
2250 argv[0]);
2251 if (error)
2252 goto done;
2253 } else {
2254 path = strdup(argv[0]);
2255 if (path == NULL) {
2256 error = got_error_from_errno("strdup");
2257 goto done;
2260 } else
2261 usage_log();
2263 if (repo_path == NULL) {
2264 if (worktree)
2265 repo_path = strdup(
2266 got_worktree_get_repo_path(worktree));
2267 else
2268 repo_path = strdup(cwd);
2270 if (repo_path == NULL) {
2271 error = got_error_from_errno("strdup");
2272 goto done;
2275 init_curses();
2277 error = got_repo_open(&repo, repo_path);
2278 if (error != NULL)
2279 goto done;
2281 error = apply_unveil(got_repo_get_path(repo),
2282 worktree ? got_worktree_get_root_path(worktree) : NULL);
2283 if (error)
2284 goto done;
2286 if (start_commit == NULL)
2287 error = get_head_commit_id(&start_id, worktree ?
2288 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2289 repo);
2290 else {
2291 error = get_head_commit_id(&start_id, start_commit, repo);
2292 if (error) {
2293 if (error->code != GOT_ERR_NOT_REF)
2294 goto done;
2295 error = got_repo_match_object_id_prefix(&start_id,
2296 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2299 if (error != NULL)
2300 goto done;
2302 error = got_ref_list(&refs, repo);
2303 if (error)
2304 goto done;
2306 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2307 if (view == NULL) {
2308 error = got_error_from_errno("view_open");
2309 goto done;
2311 error = open_log_view(view, start_id, &refs, repo, worktree ?
2312 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2313 if (error)
2314 goto done;
2315 error = view_loop(view);
2316 done:
2317 free(repo_path);
2318 free(cwd);
2319 free(path);
2320 free(start_id);
2321 if (repo)
2322 got_repo_close(repo);
2323 if (worktree)
2324 got_worktree_close(worktree);
2325 got_ref_list_free(&refs);
2326 return error;
2329 __dead static void
2330 usage_diff(void)
2332 endwin();
2333 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2334 getprogname());
2335 exit(1);
2338 static char *
2339 parse_next_line(FILE *f, size_t *len)
2341 char *line;
2342 size_t linelen;
2343 size_t lineno;
2344 const char delim[3] = { '\0', '\0', '\0'};
2346 line = fparseln(f, &linelen, &lineno, delim, 0);
2347 if (len)
2348 *len = linelen;
2349 return line;
2352 static const struct got_error *
2353 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2354 int *last_displayed_line, int *eof, int max_lines,
2355 char *header)
2357 const struct got_error *err;
2358 int nlines = 0, nprinted = 0;
2359 char *line;
2360 size_t len;
2361 wchar_t *wline;
2362 int width;
2364 rewind(f);
2365 werase(view->window);
2367 if (header) {
2368 err = format_line(&wline, &width, header, view->ncols);
2369 if (err) {
2370 return err;
2373 if (view_needs_focus_indication(view))
2374 wstandout(view->window);
2375 waddwstr(view->window, wline);
2376 if (view_needs_focus_indication(view))
2377 wstandend(view->window);
2378 if (width < view->ncols - 1)
2379 waddch(view->window, '\n');
2381 if (max_lines <= 1)
2382 return NULL;
2383 max_lines--;
2386 *eof = 0;
2387 while (nprinted < max_lines) {
2388 line = parse_next_line(f, &len);
2389 if (line == NULL) {
2390 *eof = 1;
2391 break;
2393 if (++nlines < *first_displayed_line) {
2394 free(line);
2395 continue;
2398 err = format_line(&wline, &width, line, view->ncols);
2399 if (err) {
2400 free(line);
2401 return err;
2403 waddwstr(view->window, wline);
2404 if (width < view->ncols - 1)
2405 waddch(view->window, '\n');
2406 if (++nprinted == 1)
2407 *first_displayed_line = nlines;
2408 free(line);
2409 free(wline);
2410 wline = NULL;
2412 *last_displayed_line = nlines;
2414 view_vborder(view);
2416 if (*eof) {
2417 while (nprinted < view->nlines) {
2418 waddch(view->window, '\n');
2419 nprinted++;
2422 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2423 if (err) {
2424 return err;
2427 wstandout(view->window);
2428 waddwstr(view->window, wline);
2429 wstandend(view->window);
2432 return NULL;
2435 static char *
2436 get_datestr(time_t *time, char *datebuf)
2438 struct tm mytm, *tm;
2439 char *p, *s;
2441 tm = gmtime_r(time, &mytm);
2442 if (tm == NULL)
2443 return NULL;
2444 s = asctime_r(tm, datebuf);
2445 if (s == NULL)
2446 return NULL;
2447 p = strchr(s, '\n');
2448 if (p)
2449 *p = '\0';
2450 return s;
2453 static const struct got_error *
2454 write_commit_info(struct got_object_id *commit_id,
2455 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2457 const struct got_error *err = NULL;
2458 char datebuf[26], *datestr;
2459 struct got_commit_object *commit;
2460 char *id_str = NULL, *logmsg = NULL;
2461 time_t committer_time;
2462 const char *author, *committer;
2463 char *refs_str = NULL;
2465 if (refs) {
2466 err = build_refs_str(&refs_str, refs, commit_id, repo);
2467 if (err)
2468 return err;
2471 err = got_object_open_as_commit(&commit, repo, commit_id);
2472 if (err)
2473 return err;
2475 err = got_object_id_str(&id_str, commit_id);
2476 if (err) {
2477 err = got_error_from_errno("got_object_id_str");
2478 goto done;
2481 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2482 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2483 err = got_error_from_errno("fprintf");
2484 goto done;
2486 if (fprintf(outfile, "from: %s\n",
2487 got_object_commit_get_author(commit)) < 0) {
2488 err = got_error_from_errno("fprintf");
2489 goto done;
2491 committer_time = got_object_commit_get_committer_time(commit);
2492 datestr = get_datestr(&committer_time, datebuf);
2493 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2494 err = got_error_from_errno("fprintf");
2495 goto done;
2497 author = got_object_commit_get_author(commit);
2498 committer = got_object_commit_get_committer(commit);
2499 if (strcmp(author, committer) != 0 &&
2500 fprintf(outfile, "via: %s\n", committer) < 0) {
2501 err = got_error_from_errno("fprintf");
2502 goto done;
2504 err = got_object_commit_get_logmsg(&logmsg, commit);
2505 if (err)
2506 goto done;
2507 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2508 err = got_error_from_errno("fprintf");
2509 goto done;
2511 done:
2512 free(id_str);
2513 free(logmsg);
2514 free(refs_str);
2515 got_object_commit_close(commit);
2516 return err;
2519 static const struct got_error *
2520 create_diff(struct tog_diff_view_state *s)
2522 const struct got_error *err = NULL;
2523 FILE *f = NULL;
2524 int obj_type;
2526 f = got_opentemp();
2527 if (f == NULL) {
2528 err = got_error_from_errno("got_opentemp");
2529 goto done;
2531 if (s->f && fclose(s->f) != 0) {
2532 err = got_error_from_errno("fclose");
2533 goto done;
2535 s->f = f;
2537 if (s->id1)
2538 err = got_object_get_type(&obj_type, s->repo, s->id1);
2539 else
2540 err = got_object_get_type(&obj_type, s->repo, s->id2);
2541 if (err)
2542 goto done;
2544 switch (obj_type) {
2545 case GOT_OBJ_TYPE_BLOB:
2546 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2547 s->diff_context, s->repo, f);
2548 break;
2549 case GOT_OBJ_TYPE_TREE:
2550 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2551 s->diff_context, s->repo, f);
2552 break;
2553 case GOT_OBJ_TYPE_COMMIT: {
2554 const struct got_object_id_queue *parent_ids;
2555 struct got_object_qid *pid;
2556 struct got_commit_object *commit2;
2558 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2559 if (err)
2560 break;
2561 /* Show commit info if we're diffing to a parent/root commit. */
2562 if (s->id1 == NULL)
2563 write_commit_info(s->id2, s->refs, s->repo, f);
2564 else {
2565 parent_ids = got_object_commit_get_parent_ids(commit2);
2566 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2567 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2568 write_commit_info(s->id2, s->refs,
2569 s->repo, f);
2570 break;
2574 got_object_commit_close(commit2);
2576 err = got_diff_objects_as_commits(s->id1, s->id2,
2577 s->diff_context, s->repo, f);
2578 break;
2580 default:
2581 err = got_error(GOT_ERR_OBJ_TYPE);
2582 break;
2584 done:
2585 if (f && fflush(f) != 0 && err == NULL)
2586 err = got_error_from_errno("fflush");
2587 return err;
2590 static void
2591 diff_view_indicate_progress(struct tog_view *view)
2593 mvwaddstr(view->window, 0, 0, "diffing...");
2594 update_panels();
2595 doupdate();
2598 static const struct got_error *
2599 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2600 struct got_object_id *id2, struct tog_view *log_view,
2601 struct got_reflist_head *refs, struct got_repository *repo)
2603 const struct got_error *err;
2605 if (id1 != NULL && id2 != NULL) {
2606 int type1, type2;
2607 err = got_object_get_type(&type1, repo, id1);
2608 if (err)
2609 return err;
2610 err = got_object_get_type(&type2, repo, id2);
2611 if (err)
2612 return err;
2614 if (type1 != type2)
2615 return got_error(GOT_ERR_OBJ_TYPE);
2618 if (id1) {
2619 view->state.diff.id1 = got_object_id_dup(id1);
2620 if (view->state.diff.id1 == NULL)
2621 return got_error_from_errno("got_object_id_dup");
2622 } else
2623 view->state.diff.id1 = NULL;
2625 view->state.diff.id2 = got_object_id_dup(id2);
2626 if (view->state.diff.id2 == NULL) {
2627 free(view->state.diff.id1);
2628 view->state.diff.id1 = NULL;
2629 return got_error_from_errno("got_object_id_dup");
2631 view->state.diff.f = NULL;
2632 view->state.diff.first_displayed_line = 1;
2633 view->state.diff.last_displayed_line = view->nlines;
2634 view->state.diff.diff_context = 3;
2635 view->state.diff.log_view = log_view;
2636 view->state.diff.repo = repo;
2637 view->state.diff.refs = refs;
2639 if (log_view && view_is_splitscreen(view))
2640 show_log_view(log_view); /* draw vborder */
2641 diff_view_indicate_progress(view);
2643 err = create_diff(&view->state.diff);
2644 if (err) {
2645 free(view->state.diff.id1);
2646 view->state.diff.id1 = NULL;
2647 free(view->state.diff.id2);
2648 view->state.diff.id2 = NULL;
2649 return err;
2652 view->show = show_diff_view;
2653 view->input = input_diff_view;
2654 view->close = close_diff_view;
2656 return NULL;
2659 static const struct got_error *
2660 close_diff_view(struct tog_view *view)
2662 const struct got_error *err = NULL;
2664 free(view->state.diff.id1);
2665 view->state.diff.id1 = NULL;
2666 free(view->state.diff.id2);
2667 view->state.diff.id2 = NULL;
2668 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2669 err = got_error_from_errno("fclose");
2670 return err;
2673 static const struct got_error *
2674 show_diff_view(struct tog_view *view)
2676 const struct got_error *err;
2677 struct tog_diff_view_state *s = &view->state.diff;
2678 char *id_str1 = NULL, *id_str2, *header;
2680 if (s->id1) {
2681 err = got_object_id_str(&id_str1, s->id1);
2682 if (err)
2683 return err;
2685 err = got_object_id_str(&id_str2, s->id2);
2686 if (err)
2687 return err;
2689 if (asprintf(&header, "diff %s %s",
2690 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2691 err = got_error_from_errno("asprintf");
2692 free(id_str1);
2693 free(id_str2);
2694 return err;
2696 free(id_str1);
2697 free(id_str2);
2699 return draw_file(view, s->f, &s->first_displayed_line,
2700 &s->last_displayed_line, &s->eof, view->nlines,
2701 header);
2704 static const struct got_error *
2705 set_selected_commit(struct tog_diff_view_state *s,
2706 struct commit_queue_entry *entry)
2708 const struct got_error *err;
2709 const struct got_object_id_queue *parent_ids;
2710 struct got_commit_object *selected_commit;
2711 struct got_object_qid *pid;
2713 free(s->id2);
2714 s->id2 = got_object_id_dup(entry->id);
2715 if (s->id2 == NULL)
2716 return got_error_from_errno("got_object_id_dup");
2718 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2719 if (err)
2720 return err;
2721 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2722 free(s->id1);
2723 pid = SIMPLEQ_FIRST(parent_ids);
2724 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2725 got_object_commit_close(selected_commit);
2726 return NULL;
2729 static const struct got_error *
2730 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2731 struct tog_view **focus_view, struct tog_view *view, int ch)
2733 const struct got_error *err = NULL;
2734 struct tog_diff_view_state *s = &view->state.diff;
2735 struct tog_log_view_state *ls;
2736 struct commit_queue_entry *entry;
2737 int i;
2739 switch (ch) {
2740 case 'k':
2741 case KEY_UP:
2742 if (s->first_displayed_line > 1)
2743 s->first_displayed_line--;
2744 break;
2745 case KEY_PPAGE:
2746 case CTRL('b'):
2747 if (s->first_displayed_line == 1)
2748 break;
2749 i = 0;
2750 while (i++ < view->nlines - 1 &&
2751 s->first_displayed_line > 1)
2752 s->first_displayed_line--;
2753 break;
2754 case 'j':
2755 case KEY_DOWN:
2756 if (!s->eof)
2757 s->first_displayed_line++;
2758 break;
2759 case KEY_NPAGE:
2760 case CTRL('f'):
2761 case ' ':
2762 if (s->eof)
2763 break;
2764 i = 0;
2765 while (!s->eof && i++ < view->nlines - 1) {
2766 char *line;
2767 line = parse_next_line(s->f, NULL);
2768 s->first_displayed_line++;
2769 if (line == NULL)
2770 break;
2772 break;
2773 case '[':
2774 if (s->diff_context > 0) {
2775 s->diff_context--;
2776 diff_view_indicate_progress(view);
2777 err = create_diff(s);
2779 break;
2780 case ']':
2781 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2782 s->diff_context++;
2783 diff_view_indicate_progress(view);
2784 err = create_diff(s);
2786 break;
2787 case '<':
2788 case ',':
2789 if (s->log_view == NULL)
2790 break;
2791 ls = &s->log_view->state.log;
2792 entry = TAILQ_PREV(ls->selected_entry,
2793 commit_queue_head, entry);
2794 if (entry == NULL)
2795 break;
2797 err = input_log_view(NULL, NULL, NULL, s->log_view,
2798 KEY_UP);
2799 if (err)
2800 break;
2802 err = set_selected_commit(s, entry);
2803 if (err)
2804 break;
2806 s->first_displayed_line = 1;
2807 s->last_displayed_line = view->nlines;
2809 diff_view_indicate_progress(view);
2810 err = create_diff(s);
2811 break;
2812 case '>':
2813 case '.':
2814 if (s->log_view == NULL)
2815 break;
2816 ls = &s->log_view->state.log;
2818 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2819 ls->thread_args.commits_needed++;
2821 /* Display "loading..." in log view. */
2822 show_log_view(s->log_view);
2823 update_panels();
2824 doupdate();
2826 err = trigger_log_thread(1 /* load_all */,
2827 &ls->thread_args.commits_needed,
2828 &ls->thread_args.log_complete,
2829 &ls->thread_args.need_commits);
2830 if (err)
2831 break;
2833 err = input_log_view(NULL, NULL, NULL, s->log_view,
2834 KEY_DOWN);
2835 if (err)
2836 break;
2838 entry = TAILQ_NEXT(ls->selected_entry, entry);
2839 if (entry == NULL)
2840 break;
2842 err = set_selected_commit(s, entry);
2843 if (err)
2844 break;
2846 s->first_displayed_line = 1;
2847 s->last_displayed_line = view->nlines;
2849 diff_view_indicate_progress(view);
2850 err = create_diff(s);
2851 break;
2852 default:
2853 break;
2856 return err;
2859 static const struct got_error *
2860 cmd_diff(int argc, char *argv[])
2862 const struct got_error *error = NULL;
2863 struct got_repository *repo = NULL;
2864 struct got_reflist_head refs;
2865 struct got_object_id *id1 = NULL, *id2 = NULL;
2866 char *repo_path = NULL;
2867 char *id_str1 = NULL, *id_str2 = NULL;
2868 int ch;
2869 struct tog_view *view;
2871 SIMPLEQ_INIT(&refs);
2873 #ifndef PROFILE
2874 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2875 NULL) == -1)
2876 err(1, "pledge");
2877 #endif
2879 while ((ch = getopt(argc, argv, "")) != -1) {
2880 switch (ch) {
2881 default:
2882 usage_diff();
2883 /* NOTREACHED */
2887 argc -= optind;
2888 argv += optind;
2890 if (argc == 0) {
2891 usage_diff(); /* TODO show local worktree changes */
2892 } else if (argc == 2) {
2893 repo_path = getcwd(NULL, 0);
2894 if (repo_path == NULL)
2895 return got_error_from_errno("getcwd");
2896 id_str1 = argv[0];
2897 id_str2 = argv[1];
2898 } else if (argc == 3) {
2899 repo_path = realpath(argv[0], NULL);
2900 if (repo_path == NULL)
2901 return got_error_from_errno2("realpath", argv[0]);
2902 id_str1 = argv[1];
2903 id_str2 = argv[2];
2904 } else
2905 usage_diff();
2907 init_curses();
2909 error = got_repo_open(&repo, repo_path);
2910 if (error)
2911 goto done;
2913 error = apply_unveil(got_repo_get_path(repo), NULL);
2914 if (error)
2915 goto done;
2917 error = got_repo_match_object_id_prefix(&id1, id_str1,
2918 GOT_OBJ_TYPE_ANY, repo);
2919 if (error)
2920 goto done;
2922 error = got_repo_match_object_id_prefix(&id2, id_str2,
2923 GOT_OBJ_TYPE_ANY, repo);
2924 if (error)
2925 goto done;
2927 error = got_ref_list(&refs, repo);
2928 if (error)
2929 goto done;
2931 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2932 if (view == NULL) {
2933 error = got_error_from_errno("view_open");
2934 goto done;
2936 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2937 if (error)
2938 goto done;
2939 error = view_loop(view);
2940 done:
2941 free(repo_path);
2942 if (repo)
2943 got_repo_close(repo);
2944 got_ref_list_free(&refs);
2945 return error;
2948 __dead static void
2949 usage_blame(void)
2951 endwin();
2952 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2953 getprogname());
2954 exit(1);
2957 struct tog_blame_line {
2958 int annotated;
2959 struct got_object_id *id;
2962 static const struct got_error *
2963 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2964 const char *path, struct tog_blame_line *lines, int nlines,
2965 int blame_complete, int selected_line, int *first_displayed_line,
2966 int *last_displayed_line, int *eof, int max_lines)
2968 const struct got_error *err;
2969 int lineno = 0, nprinted = 0;
2970 char *line;
2971 size_t len;
2972 wchar_t *wline;
2973 int width, wlimit;
2974 struct tog_blame_line *blame_line;
2975 struct got_object_id *prev_id = NULL;
2976 char *id_str;
2978 err = got_object_id_str(&id_str, id);
2979 if (err)
2980 return err;
2982 rewind(f);
2983 werase(view->window);
2985 if (asprintf(&line, "commit %s", id_str) == -1) {
2986 err = got_error_from_errno("asprintf");
2987 free(id_str);
2988 return err;
2991 err = format_line(&wline, &width, line, view->ncols);
2992 free(line);
2993 line = NULL;
2994 if (view_needs_focus_indication(view))
2995 wstandout(view->window);
2996 waddwstr(view->window, wline);
2997 if (view_needs_focus_indication(view))
2998 wstandend(view->window);
2999 free(wline);
3000 wline = NULL;
3001 if (width < view->ncols - 1)
3002 waddch(view->window, '\n');
3004 if (asprintf(&line, "[%d/%d] %s%s",
3005 *first_displayed_line - 1 + selected_line, nlines,
3006 blame_complete ? "" : "annotating... ", path) == -1) {
3007 free(id_str);
3008 return got_error_from_errno("asprintf");
3010 free(id_str);
3011 err = format_line(&wline, &width, line, view->ncols);
3012 free(line);
3013 line = NULL;
3014 if (err)
3015 return err;
3016 waddwstr(view->window, wline);
3017 free(wline);
3018 wline = NULL;
3019 if (width < view->ncols - 1)
3020 waddch(view->window, '\n');
3022 *eof = 0;
3023 while (nprinted < max_lines - 2) {
3024 line = parse_next_line(f, &len);
3025 if (line == NULL) {
3026 *eof = 1;
3027 break;
3029 if (++lineno < *first_displayed_line) {
3030 free(line);
3031 continue;
3034 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3035 err = format_line(&wline, &width, line, wlimit);
3036 if (err) {
3037 free(line);
3038 return err;
3041 if (view->focussed && nprinted == selected_line - 1)
3042 wstandout(view->window);
3044 if (nlines > 0) {
3045 blame_line = &lines[lineno - 1];
3046 if (blame_line->annotated && prev_id &&
3047 got_object_id_cmp(prev_id, blame_line->id) == 0)
3048 waddstr(view->window, " ");
3049 else if (blame_line->annotated) {
3050 char *id_str;
3051 err = got_object_id_str(&id_str, blame_line->id);
3052 if (err) {
3053 free(line);
3054 free(wline);
3055 return err;
3057 wprintw(view->window, "%.8s ", id_str);
3058 free(id_str);
3059 prev_id = blame_line->id;
3060 } else {
3061 waddstr(view->window, "........ ");
3062 prev_id = NULL;
3064 } else {
3065 waddstr(view->window, "........ ");
3066 prev_id = NULL;
3069 waddwstr(view->window, wline);
3070 while (width < wlimit) {
3071 waddch(view->window, ' ');
3072 width++;
3074 if (view->focussed && nprinted == selected_line - 1)
3075 wstandend(view->window);
3076 if (++nprinted == 1)
3077 *first_displayed_line = lineno;
3078 free(line);
3079 free(wline);
3080 wline = NULL;
3082 *last_displayed_line = lineno;
3084 view_vborder(view);
3086 return NULL;
3089 static const struct got_error *
3090 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3092 const struct got_error *err = NULL;
3093 struct tog_blame_cb_args *a = arg;
3094 struct tog_blame_line *line;
3095 int errcode;
3097 if (nlines != a->nlines ||
3098 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3099 return got_error(GOT_ERR_RANGE);
3101 errcode = pthread_mutex_lock(&tog_mutex);
3102 if (errcode)
3103 return got_error_set_errno(errcode, "pthread_mutex_lock");
3105 if (*a->quit) { /* user has quit the blame view */
3106 err = got_error(GOT_ERR_ITER_COMPLETED);
3107 goto done;
3110 if (lineno == -1)
3111 goto done; /* no change in this commit */
3113 line = &a->lines[lineno - 1];
3114 if (line->annotated)
3115 goto done;
3117 line->id = got_object_id_dup(id);
3118 if (line->id == NULL) {
3119 err = got_error_from_errno("got_object_id_dup");
3120 goto done;
3122 line->annotated = 1;
3123 done:
3124 errcode = pthread_mutex_unlock(&tog_mutex);
3125 if (errcode)
3126 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3127 return err;
3130 static void *
3131 blame_thread(void *arg)
3133 const struct got_error *err;
3134 struct tog_blame_thread_args *ta = arg;
3135 struct tog_blame_cb_args *a = ta->cb_args;
3136 int errcode;
3138 err = got_blame(ta->path, a->commit_id, ta->repo,
3139 blame_cb, ta->cb_args);
3141 errcode = pthread_mutex_lock(&tog_mutex);
3142 if (errcode)
3143 return (void *)got_error_set_errno(errcode,
3144 "pthread_mutex_lock");
3146 got_repo_close(ta->repo);
3147 ta->repo = NULL;
3148 *ta->complete = 1;
3150 errcode = pthread_mutex_unlock(&tog_mutex);
3151 if (errcode && err == NULL)
3152 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3154 return (void *)err;
3157 static struct got_object_id *
3158 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3159 int first_displayed_line, int selected_line)
3161 struct tog_blame_line *line;
3163 if (nlines <= 0)
3164 return NULL;
3166 line = &lines[first_displayed_line - 1 + selected_line - 1];
3167 if (!line->annotated)
3168 return NULL;
3170 return line->id;
3173 static const struct got_error *
3174 stop_blame(struct tog_blame *blame)
3176 const struct got_error *err = NULL;
3177 int i;
3179 if (blame->thread) {
3180 int errcode;
3181 errcode = pthread_mutex_unlock(&tog_mutex);
3182 if (errcode)
3183 return got_error_set_errno(errcode,
3184 "pthread_mutex_unlock");
3185 errcode = pthread_join(blame->thread, (void **)&err);
3186 if (errcode)
3187 return got_error_set_errno(errcode, "pthread_join");
3188 errcode = pthread_mutex_lock(&tog_mutex);
3189 if (errcode)
3190 return got_error_set_errno(errcode,
3191 "pthread_mutex_lock");
3192 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3193 err = NULL;
3194 blame->thread = NULL;
3196 if (blame->thread_args.repo) {
3197 got_repo_close(blame->thread_args.repo);
3198 blame->thread_args.repo = NULL;
3200 if (blame->f) {
3201 if (fclose(blame->f) != 0 && err == NULL)
3202 err = got_error_from_errno("fclose");
3203 blame->f = NULL;
3205 if (blame->lines) {
3206 for (i = 0; i < blame->nlines; i++)
3207 free(blame->lines[i].id);
3208 free(blame->lines);
3209 blame->lines = NULL;
3211 free(blame->cb_args.commit_id);
3212 blame->cb_args.commit_id = NULL;
3214 return err;
3217 static const struct got_error *
3218 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3219 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3220 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3221 struct got_repository *repo)
3223 const struct got_error *err = NULL;
3224 struct got_blob_object *blob = NULL;
3225 struct got_repository *thread_repo = NULL;
3226 struct got_object_id *obj_id = NULL;
3227 int obj_type;
3229 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3230 if (err)
3231 return err;
3232 if (obj_id == NULL)
3233 return got_error(GOT_ERR_NO_OBJ);
3235 err = got_object_get_type(&obj_type, repo, obj_id);
3236 if (err)
3237 goto done;
3239 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3240 err = got_error(GOT_ERR_OBJ_TYPE);
3241 goto done;
3244 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3245 if (err)
3246 goto done;
3247 blame->f = got_opentemp();
3248 if (blame->f == NULL) {
3249 err = got_error_from_errno("got_opentemp");
3250 goto done;
3252 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3253 &blame->line_offsets, blame->f, blob);
3254 if (err || blame->nlines == 0)
3255 goto done;
3257 /* Don't include \n at EOF in the blame line count. */
3258 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3259 blame->nlines--;
3261 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3262 if (blame->lines == NULL) {
3263 err = got_error_from_errno("calloc");
3264 goto done;
3267 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3268 if (err)
3269 goto done;
3271 blame->cb_args.view = view;
3272 blame->cb_args.lines = blame->lines;
3273 blame->cb_args.nlines = blame->nlines;
3274 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3275 if (blame->cb_args.commit_id == NULL) {
3276 err = got_error_from_errno("got_object_id_dup");
3277 goto done;
3279 blame->cb_args.quit = done;
3281 blame->thread_args.path = path;
3282 blame->thread_args.repo = thread_repo;
3283 blame->thread_args.cb_args = &blame->cb_args;
3284 blame->thread_args.complete = blame_complete;
3285 *blame_complete = 0;
3287 done:
3288 if (blob)
3289 got_object_blob_close(blob);
3290 free(obj_id);
3291 if (err)
3292 stop_blame(blame);
3293 return err;
3296 static const struct got_error *
3297 open_blame_view(struct tog_view *view, char *path,
3298 struct got_object_id *commit_id, struct got_reflist_head *refs,
3299 struct got_repository *repo)
3301 const struct got_error *err = NULL;
3302 struct tog_blame_view_state *s = &view->state.blame;
3304 SIMPLEQ_INIT(&s->blamed_commits);
3306 s->path = strdup(path);
3307 if (s->path == NULL)
3308 return got_error_from_errno("strdup");
3310 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3311 if (err) {
3312 free(s->path);
3313 return err;
3316 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3317 s->first_displayed_line = 1;
3318 s->last_displayed_line = view->nlines;
3319 s->selected_line = 1;
3320 s->blame_complete = 0;
3321 s->repo = repo;
3322 s->refs = refs;
3323 s->commit_id = commit_id;
3324 memset(&s->blame, 0, sizeof(s->blame));
3326 view->show = show_blame_view;
3327 view->input = input_blame_view;
3328 view->close = close_blame_view;
3329 view->search_start = search_start_blame_view;
3330 view->search_next = search_next_blame_view;
3332 return run_blame(&s->blame, view, &s->blame_complete,
3333 &s->first_displayed_line, &s->last_displayed_line,
3334 &s->selected_line, &s->done, &s->eof, s->path,
3335 s->blamed_commit->id, s->repo);
3338 static const struct got_error *
3339 close_blame_view(struct tog_view *view)
3341 const struct got_error *err = NULL;
3342 struct tog_blame_view_state *s = &view->state.blame;
3344 if (s->blame.thread)
3345 err = stop_blame(&s->blame);
3347 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3348 struct got_object_qid *blamed_commit;
3349 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3350 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3351 got_object_qid_free(blamed_commit);
3354 free(s->path);
3356 return err;
3359 static const struct got_error *
3360 search_start_blame_view(struct tog_view *view)
3362 struct tog_blame_view_state *s = &view->state.blame;
3364 s->matched_line = 0;
3365 return NULL;
3368 static int
3369 match_line(const char *line, regex_t *regex)
3371 regmatch_t regmatch;
3373 return regexec(regex, line, 1, &regmatch, 0) == 0;
3377 static const struct got_error *
3378 search_next_blame_view(struct tog_view *view)
3380 struct tog_blame_view_state *s = &view->state.blame;
3381 int lineno;
3383 if (!view->searching) {
3384 view->search_next_done = 1;
3385 return NULL;
3388 if (s->matched_line) {
3389 if (view->searching == TOG_SEARCH_FORWARD)
3390 lineno = s->matched_line + 1;
3391 else
3392 lineno = s->matched_line - 1;
3393 } else {
3394 if (view->searching == TOG_SEARCH_FORWARD)
3395 lineno = 1;
3396 else
3397 lineno = s->blame.nlines;
3400 while (1) {
3401 char *line = NULL;
3402 off_t offset;
3403 size_t len;
3405 if (lineno <= 0 || lineno > s->blame.nlines) {
3406 if (s->matched_line == 0) {
3407 view->search_next_done = 1;
3408 free(line);
3409 break;
3412 if (view->searching == TOG_SEARCH_FORWARD)
3413 lineno = 1;
3414 else
3415 lineno = s->blame.nlines;
3418 offset = s->blame.line_offsets[lineno - 1];
3419 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3420 free(line);
3421 return got_error_from_errno("fseeko");
3423 free(line);
3424 line = parse_next_line(s->blame.f, &len);
3425 if (line && match_line(line, &view->regex)) {
3426 view->search_next_done = 1;
3427 s->matched_line = lineno;
3428 free(line);
3429 break;
3431 free(line);
3432 if (view->searching == TOG_SEARCH_FORWARD)
3433 lineno++;
3434 else
3435 lineno--;
3438 if (s->matched_line) {
3439 s->first_displayed_line = s->matched_line;
3440 s->selected_line = 1;
3443 return NULL;
3446 static const struct got_error *
3447 show_blame_view(struct tog_view *view)
3449 const struct got_error *err = NULL;
3450 struct tog_blame_view_state *s = &view->state.blame;
3451 int errcode;
3453 if (s->blame.thread == NULL) {
3454 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3455 &s->blame.thread_args);
3456 if (errcode)
3457 return got_error_set_errno(errcode, "pthread_create");
3459 halfdelay(1); /* fast refresh while annotating */
3462 if (s->blame_complete)
3463 halfdelay(10); /* disable fast refresh */
3465 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3466 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3467 s->selected_line, &s->first_displayed_line,
3468 &s->last_displayed_line, &s->eof, view->nlines);
3470 view_vborder(view);
3471 return err;
3474 static const struct got_error *
3475 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3476 struct tog_view **focus_view, struct tog_view *view, int ch)
3478 const struct got_error *err = NULL, *thread_err = NULL;
3479 struct tog_view *diff_view;
3480 struct tog_blame_view_state *s = &view->state.blame;
3481 int begin_x = 0;
3483 switch (ch) {
3484 case 'q':
3485 s->done = 1;
3486 break;
3487 case 'k':
3488 case KEY_UP:
3489 if (s->selected_line > 1)
3490 s->selected_line--;
3491 else if (s->selected_line == 1 &&
3492 s->first_displayed_line > 1)
3493 s->first_displayed_line--;
3494 break;
3495 case KEY_PPAGE:
3496 if (s->first_displayed_line == 1) {
3497 s->selected_line = 1;
3498 break;
3500 if (s->first_displayed_line > view->nlines - 2)
3501 s->first_displayed_line -=
3502 (view->nlines - 2);
3503 else
3504 s->first_displayed_line = 1;
3505 break;
3506 case 'j':
3507 case KEY_DOWN:
3508 if (s->selected_line < view->nlines - 2 &&
3509 s->first_displayed_line +
3510 s->selected_line <= s->blame.nlines)
3511 s->selected_line++;
3512 else if (s->last_displayed_line <
3513 s->blame.nlines)
3514 s->first_displayed_line++;
3515 break;
3516 case 'b':
3517 case 'p': {
3518 struct got_object_id *id = NULL;
3519 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3520 s->first_displayed_line, s->selected_line);
3521 if (id == NULL)
3522 break;
3523 if (ch == 'p') {
3524 struct got_commit_object *commit;
3525 struct got_object_qid *pid;
3526 struct got_object_id *blob_id = NULL;
3527 int obj_type;
3528 err = got_object_open_as_commit(&commit,
3529 s->repo, id);
3530 if (err)
3531 break;
3532 pid = SIMPLEQ_FIRST(
3533 got_object_commit_get_parent_ids(commit));
3534 if (pid == NULL) {
3535 got_object_commit_close(commit);
3536 break;
3538 /* Check if path history ends here. */
3539 err = got_object_id_by_path(&blob_id, s->repo,
3540 pid->id, s->path);
3541 if (err) {
3542 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3543 err = NULL;
3544 got_object_commit_close(commit);
3545 break;
3547 err = got_object_get_type(&obj_type, s->repo,
3548 blob_id);
3549 free(blob_id);
3550 /* Can't blame non-blob type objects. */
3551 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3552 got_object_commit_close(commit);
3553 break;
3555 err = got_object_qid_alloc(&s->blamed_commit,
3556 pid->id);
3557 got_object_commit_close(commit);
3558 } else {
3559 if (got_object_id_cmp(id,
3560 s->blamed_commit->id) == 0)
3561 break;
3562 err = got_object_qid_alloc(&s->blamed_commit,
3563 id);
3565 if (err)
3566 break;
3567 s->done = 1;
3568 thread_err = stop_blame(&s->blame);
3569 s->done = 0;
3570 if (thread_err)
3571 break;
3572 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3573 s->blamed_commit, entry);
3574 err = run_blame(&s->blame, view, &s->blame_complete,
3575 &s->first_displayed_line, &s->last_displayed_line,
3576 &s->selected_line, &s->done, &s->eof,
3577 s->path, s->blamed_commit->id, s->repo);
3578 if (err)
3579 break;
3580 break;
3582 case 'B': {
3583 struct got_object_qid *first;
3584 first = SIMPLEQ_FIRST(&s->blamed_commits);
3585 if (!got_object_id_cmp(first->id, s->commit_id))
3586 break;
3587 s->done = 1;
3588 thread_err = stop_blame(&s->blame);
3589 s->done = 0;
3590 if (thread_err)
3591 break;
3592 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3593 got_object_qid_free(s->blamed_commit);
3594 s->blamed_commit =
3595 SIMPLEQ_FIRST(&s->blamed_commits);
3596 err = run_blame(&s->blame, view, &s->blame_complete,
3597 &s->first_displayed_line, &s->last_displayed_line,
3598 &s->selected_line, &s->done, &s->eof, s->path,
3599 s->blamed_commit->id, s->repo);
3600 if (err)
3601 break;
3602 break;
3604 case KEY_ENTER:
3605 case '\r': {
3606 struct got_object_id *id = NULL;
3607 struct got_object_qid *pid;
3608 struct got_commit_object *commit = NULL;
3609 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3610 s->first_displayed_line, s->selected_line);
3611 if (id == NULL)
3612 break;
3613 err = got_object_open_as_commit(&commit, s->repo, id);
3614 if (err)
3615 break;
3616 pid = SIMPLEQ_FIRST(
3617 got_object_commit_get_parent_ids(commit));
3618 if (view_is_parent_view(view))
3619 begin_x = view_split_begin_x(view->begin_x);
3620 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3621 if (diff_view == NULL) {
3622 got_object_commit_close(commit);
3623 err = got_error_from_errno("view_open");
3624 break;
3626 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3627 id, NULL, s->refs, s->repo);
3628 got_object_commit_close(commit);
3629 if (err) {
3630 view_close(diff_view);
3631 break;
3633 if (view_is_parent_view(view)) {
3634 err = view_close_child(view);
3635 if (err)
3636 break;
3637 err = view_set_child(view, diff_view);
3638 if (err) {
3639 view_close(diff_view);
3640 break;
3642 *focus_view = diff_view;
3643 view->child_focussed = 1;
3644 } else
3645 *new_view = diff_view;
3646 if (err)
3647 break;
3648 break;
3650 case KEY_NPAGE:
3651 case ' ':
3652 if (s->last_displayed_line >= s->blame.nlines &&
3653 s->selected_line >= MIN(s->blame.nlines,
3654 view->nlines - 2)) {
3655 break;
3657 if (s->last_displayed_line >= s->blame.nlines &&
3658 s->selected_line < view->nlines - 2) {
3659 s->selected_line = MIN(s->blame.nlines,
3660 view->nlines - 2);
3661 break;
3663 if (s->last_displayed_line + view->nlines - 2
3664 <= s->blame.nlines)
3665 s->first_displayed_line +=
3666 view->nlines - 2;
3667 else
3668 s->first_displayed_line =
3669 s->blame.nlines -
3670 (view->nlines - 3);
3671 break;
3672 case KEY_RESIZE:
3673 if (s->selected_line > view->nlines - 2) {
3674 s->selected_line = MIN(s->blame.nlines,
3675 view->nlines - 2);
3677 break;
3678 default:
3679 break;
3681 return thread_err ? thread_err : err;
3684 static const struct got_error *
3685 cmd_blame(int argc, char *argv[])
3687 const struct got_error *error;
3688 struct got_repository *repo = NULL;
3689 struct got_reflist_head refs;
3690 struct got_worktree *worktree = NULL;
3691 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3692 struct got_object_id *commit_id = NULL;
3693 char *commit_id_str = NULL;
3694 int ch;
3695 struct tog_view *view;
3697 SIMPLEQ_INIT(&refs);
3699 #ifndef PROFILE
3700 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3701 NULL) == -1)
3702 err(1, "pledge");
3703 #endif
3705 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3706 switch (ch) {
3707 case 'c':
3708 commit_id_str = optarg;
3709 break;
3710 case 'r':
3711 repo_path = realpath(optarg, NULL);
3712 if (repo_path == NULL)
3713 err(1, "-r option");
3714 break;
3715 default:
3716 usage_blame();
3717 /* NOTREACHED */
3721 argc -= optind;
3722 argv += optind;
3724 if (argc == 1)
3725 path = argv[0];
3726 else
3727 usage_blame();
3729 cwd = getcwd(NULL, 0);
3730 if (cwd == NULL) {
3731 error = got_error_from_errno("getcwd");
3732 goto done;
3734 if (repo_path == NULL) {
3735 error = got_worktree_open(&worktree, cwd);
3736 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3737 goto done;
3738 else
3739 error = NULL;
3740 if (worktree) {
3741 repo_path =
3742 strdup(got_worktree_get_repo_path(worktree));
3743 if (repo_path == NULL)
3744 error = got_error_from_errno("strdup");
3745 if (error)
3746 goto done;
3747 } else {
3748 repo_path = strdup(cwd);
3749 if (repo_path == NULL) {
3750 error = got_error_from_errno("strdup");
3751 goto done;
3756 init_curses();
3758 error = got_repo_open(&repo, repo_path);
3759 if (error != NULL)
3760 goto done;
3762 error = apply_unveil(got_repo_get_path(repo), NULL);
3763 if (error)
3764 goto done;
3766 if (worktree) {
3767 const char *prefix = got_worktree_get_path_prefix(worktree);
3768 char *p, *worktree_subdir = cwd +
3769 strlen(got_worktree_get_root_path(worktree));
3770 if (asprintf(&p, "%s%s%s%s%s",
3771 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3772 worktree_subdir, worktree_subdir[0] ? "/" : "",
3773 path) == -1) {
3774 error = got_error_from_errno("asprintf");
3775 goto done;
3777 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3778 free(p);
3779 } else {
3780 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3782 if (error)
3783 goto done;
3785 if (commit_id_str == NULL) {
3786 struct got_reference *head_ref;
3787 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3788 if (error != NULL)
3789 goto done;
3790 error = got_ref_resolve(&commit_id, repo, head_ref);
3791 got_ref_close(head_ref);
3792 } else {
3793 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3794 if (error) {
3795 if (error->code != GOT_ERR_NOT_REF)
3796 goto done;
3797 error = got_repo_match_object_id_prefix(&commit_id,
3798 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3801 if (error != NULL)
3802 goto done;
3804 error = got_ref_list(&refs, repo);
3805 if (error)
3806 goto done;
3808 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3809 if (view == NULL) {
3810 error = got_error_from_errno("view_open");
3811 goto done;
3813 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3814 if (error)
3815 goto done;
3816 error = view_loop(view);
3817 done:
3818 free(repo_path);
3819 free(cwd);
3820 free(commit_id);
3821 if (worktree)
3822 got_worktree_close(worktree);
3823 if (repo)
3824 got_repo_close(repo);
3825 got_ref_list_free(&refs);
3826 return error;
3829 static const struct got_error *
3830 draw_tree_entries(struct tog_view *view,
3831 struct got_tree_entry **first_displayed_entry,
3832 struct got_tree_entry **last_displayed_entry,
3833 struct got_tree_entry **selected_entry, int *ndisplayed,
3834 const char *label, int show_ids, const char *parent_path,
3835 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3837 const struct got_error *err = NULL;
3838 struct got_tree_entry *te;
3839 wchar_t *wline;
3840 int width, n;
3842 *ndisplayed = 0;
3844 werase(view->window);
3846 if (limit == 0)
3847 return NULL;
3849 err = format_line(&wline, &width, label, view->ncols);
3850 if (err)
3851 return err;
3852 if (view_needs_focus_indication(view))
3853 wstandout(view->window);
3854 waddwstr(view->window, wline);
3855 if (view_needs_focus_indication(view))
3856 wstandend(view->window);
3857 free(wline);
3858 wline = NULL;
3859 if (width < view->ncols - 1)
3860 waddch(view->window, '\n');
3861 if (--limit <= 0)
3862 return NULL;
3863 err = format_line(&wline, &width, parent_path, view->ncols);
3864 if (err)
3865 return err;
3866 waddwstr(view->window, wline);
3867 free(wline);
3868 wline = NULL;
3869 if (width < view->ncols - 1)
3870 waddch(view->window, '\n');
3871 if (--limit <= 0)
3872 return NULL;
3873 waddch(view->window, '\n');
3874 if (--limit <= 0)
3875 return NULL;
3877 te = SIMPLEQ_FIRST(&entries->head);
3878 if (*first_displayed_entry == NULL) {
3879 if (selected == 0) {
3880 if (view->focussed)
3881 wstandout(view->window);
3882 *selected_entry = NULL;
3884 waddstr(view->window, " ..\n"); /* parent directory */
3885 if (selected == 0 && view->focussed)
3886 wstandend(view->window);
3887 (*ndisplayed)++;
3888 if (--limit <= 0)
3889 return NULL;
3890 n = 1;
3891 } else {
3892 n = 0;
3893 while (te != *first_displayed_entry)
3894 te = SIMPLEQ_NEXT(te, entry);
3897 while (te) {
3898 char *line = NULL, *id_str = NULL;
3899 const char *modestr = "";
3901 if (show_ids) {
3902 err = got_object_id_str(&id_str, te->id);
3903 if (err)
3904 return got_error_from_errno(
3905 "got_object_id_str");
3907 if (S_ISLNK(te->mode))
3908 modestr = "@";
3909 else if (S_ISDIR(te->mode))
3910 modestr = "/";
3911 else if (te->mode & S_IXUSR)
3912 modestr = "*";
3913 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3914 te->name, modestr) == -1) {
3915 free(id_str);
3916 return got_error_from_errno("asprintf");
3918 free(id_str);
3919 err = format_line(&wline, &width, line, view->ncols);
3920 if (err) {
3921 free(line);
3922 break;
3924 if (n == selected) {
3925 if (view->focussed)
3926 wstandout(view->window);
3927 *selected_entry = te;
3929 waddwstr(view->window, wline);
3930 if (width < view->ncols - 1)
3931 waddch(view->window, '\n');
3932 if (n == selected && view->focussed)
3933 wstandend(view->window);
3934 free(line);
3935 free(wline);
3936 wline = NULL;
3937 n++;
3938 (*ndisplayed)++;
3939 *last_displayed_entry = te;
3940 if (--limit <= 0)
3941 break;
3942 te = SIMPLEQ_NEXT(te, entry);
3945 return err;
3948 static void
3949 tree_scroll_up(struct tog_view *view,
3950 struct got_tree_entry **first_displayed_entry, int maxscroll,
3951 const struct got_tree_entries *entries, int isroot)
3953 struct got_tree_entry *te, *prev;
3954 int i;
3956 if (*first_displayed_entry == NULL)
3957 return;
3959 te = SIMPLEQ_FIRST(&entries->head);
3960 if (*first_displayed_entry == te) {
3961 if (!isroot)
3962 *first_displayed_entry = NULL;
3963 return;
3966 /* XXX this is stupid... switch to TAILQ? */
3967 for (i = 0; i < maxscroll; i++) {
3968 while (te != *first_displayed_entry) {
3969 prev = te;
3970 te = SIMPLEQ_NEXT(te, entry);
3972 *first_displayed_entry = prev;
3973 te = SIMPLEQ_FIRST(&entries->head);
3975 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3976 *first_displayed_entry = NULL;
3979 static int
3980 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3981 struct got_tree_entry *last_displayed_entry,
3982 const struct got_tree_entries *entries)
3984 struct got_tree_entry *next, *last;
3985 int n = 0;
3987 if (*first_displayed_entry)
3988 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3989 else
3990 next = SIMPLEQ_FIRST(&entries->head);
3991 last = last_displayed_entry;
3992 while (next && last && n++ < maxscroll) {
3993 last = SIMPLEQ_NEXT(last, entry);
3994 if (last) {
3995 *first_displayed_entry = next;
3996 next = SIMPLEQ_NEXT(next, entry);
3999 return n;
4002 static const struct got_error *
4003 tree_entry_path(char **path, struct tog_parent_trees *parents,
4004 struct got_tree_entry *te)
4006 const struct got_error *err = NULL;
4007 struct tog_parent_tree *pt;
4008 size_t len = 2; /* for leading slash and NUL */
4010 TAILQ_FOREACH(pt, parents, entry)
4011 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4012 if (te)
4013 len += strlen(te->name);
4015 *path = calloc(1, len);
4016 if (path == NULL)
4017 return got_error_from_errno("calloc");
4019 (*path)[0] = '/';
4020 pt = TAILQ_LAST(parents, tog_parent_trees);
4021 while (pt) {
4022 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4023 err = got_error(GOT_ERR_NO_SPACE);
4024 goto done;
4026 if (strlcat(*path, "/", len) >= len) {
4027 err = got_error(GOT_ERR_NO_SPACE);
4028 goto done;
4030 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4032 if (te) {
4033 if (strlcat(*path, te->name, len) >= len) {
4034 err = got_error(GOT_ERR_NO_SPACE);
4035 goto done;
4038 done:
4039 if (err) {
4040 free(*path);
4041 *path = NULL;
4043 return err;
4046 static const struct got_error *
4047 blame_tree_entry(struct tog_view **new_view, int begin_x,
4048 struct got_tree_entry *te, struct tog_parent_trees *parents,
4049 struct got_object_id *commit_id, struct got_reflist_head *refs,
4050 struct got_repository *repo)
4052 const struct got_error *err = NULL;
4053 char *path;
4054 struct tog_view *blame_view;
4056 *new_view = NULL;
4058 err = tree_entry_path(&path, parents, te);
4059 if (err)
4060 return err;
4062 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4063 if (blame_view == NULL) {
4064 err = got_error_from_errno("view_open");
4065 goto done;
4068 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4069 if (err) {
4070 view_close(blame_view);
4071 } else
4072 *new_view = blame_view;
4073 done:
4074 free(path);
4075 return err;
4078 static const struct got_error *
4079 log_tree_entry(struct tog_view **new_view, int begin_x,
4080 struct got_tree_entry *te, struct tog_parent_trees *parents,
4081 struct got_object_id *commit_id, struct got_reflist_head *refs,
4082 struct got_repository *repo)
4084 struct tog_view *log_view;
4085 const struct got_error *err = NULL;
4086 char *path;
4088 *new_view = NULL;
4090 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4091 if (log_view == NULL)
4092 return got_error_from_errno("view_open");
4094 err = tree_entry_path(&path, parents, te);
4095 if (err)
4096 return err;
4098 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4099 if (err)
4100 view_close(log_view);
4101 else
4102 *new_view = log_view;
4103 free(path);
4104 return err;
4107 static const struct got_error *
4108 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4109 struct got_object_id *commit_id, struct got_reflist_head *refs,
4110 struct got_repository *repo)
4112 const struct got_error *err = NULL;
4113 char *commit_id_str = NULL;
4114 struct tog_tree_view_state *s = &view->state.tree;
4116 TAILQ_INIT(&s->parents);
4118 err = got_object_id_str(&commit_id_str, commit_id);
4119 if (err != NULL)
4120 goto done;
4122 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4123 err = got_error_from_errno("asprintf");
4124 goto done;
4127 s->root = s->tree = root;
4128 s->entries = got_object_tree_get_entries(root);
4129 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4130 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4131 s->commit_id = got_object_id_dup(commit_id);
4132 if (s->commit_id == NULL) {
4133 err = got_error_from_errno("got_object_id_dup");
4134 goto done;
4136 s->refs = refs;
4137 s->repo = repo;
4139 view->show = show_tree_view;
4140 view->input = input_tree_view;
4141 view->close = close_tree_view;
4142 view->search_start = search_start_tree_view;
4143 view->search_next = search_next_tree_view;
4144 done:
4145 free(commit_id_str);
4146 if (err) {
4147 free(s->tree_label);
4148 s->tree_label = NULL;
4150 return err;
4153 static const struct got_error *
4154 close_tree_view(struct tog_view *view)
4156 struct tog_tree_view_state *s = &view->state.tree;
4158 free(s->tree_label);
4159 s->tree_label = NULL;
4160 free(s->commit_id);
4161 s->commit_id = NULL;
4162 while (!TAILQ_EMPTY(&s->parents)) {
4163 struct tog_parent_tree *parent;
4164 parent = TAILQ_FIRST(&s->parents);
4165 TAILQ_REMOVE(&s->parents, parent, entry);
4166 free(parent);
4169 if (s->tree != s->root)
4170 got_object_tree_close(s->tree);
4171 got_object_tree_close(s->root);
4173 return NULL;
4176 static const struct got_error *
4177 search_start_tree_view(struct tog_view *view)
4179 struct tog_tree_view_state *s = &view->state.tree;
4181 s->matched_entry = NULL;
4182 return NULL;
4185 static int
4186 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4188 regmatch_t regmatch;
4190 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4193 static const struct got_error *
4194 search_next_tree_view(struct tog_view *view)
4196 struct tog_tree_view_state *s = &view->state.tree;
4197 struct got_tree_entry *entry = NULL, *te;
4199 if (!view->searching) {
4200 view->search_next_done = 1;
4201 return NULL;
4204 if (s->matched_entry) {
4205 if (view->searching == TOG_SEARCH_FORWARD) {
4206 if (s->selected_entry)
4207 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4208 else
4209 entry = SIMPLEQ_FIRST(&s->entries->head);
4211 else {
4212 if (s->selected_entry == NULL) {
4213 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4214 entry = te;
4215 } else {
4216 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4217 entry = te;
4218 if (SIMPLEQ_NEXT(te, entry) ==
4219 s->selected_entry)
4220 break;
4224 } else {
4225 if (view->searching == TOG_SEARCH_FORWARD)
4226 entry = SIMPLEQ_FIRST(&s->entries->head);
4227 else {
4228 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4229 entry = te;
4233 while (1) {
4234 if (entry == NULL) {
4235 if (s->matched_entry == NULL) {
4236 view->search_next_done = 1;
4237 return NULL;
4239 if (view->searching == TOG_SEARCH_FORWARD)
4240 entry = SIMPLEQ_FIRST(&s->entries->head);
4241 else {
4242 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4243 entry = te;
4247 if (match_tree_entry(entry, &view->regex)) {
4248 view->search_next_done = 1;
4249 s->matched_entry = entry;
4250 break;
4253 if (view->searching == TOG_SEARCH_FORWARD)
4254 entry = SIMPLEQ_NEXT(entry, entry);
4255 else {
4256 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4257 entry = NULL;
4258 else {
4259 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4260 if (SIMPLEQ_NEXT(te, entry) == entry) {
4261 entry = te;
4262 break;
4269 if (s->matched_entry) {
4270 s->first_displayed_entry = s->matched_entry;
4271 s->selected = 0;
4274 return NULL;
4277 static const struct got_error *
4278 show_tree_view(struct tog_view *view)
4280 const struct got_error *err = NULL;
4281 struct tog_tree_view_state *s = &view->state.tree;
4282 char *parent_path;
4284 err = tree_entry_path(&parent_path, &s->parents, NULL);
4285 if (err)
4286 return err;
4288 err = draw_tree_entries(view, &s->first_displayed_entry,
4289 &s->last_displayed_entry, &s->selected_entry,
4290 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4291 s->entries, s->selected, view->nlines, s->tree == s->root);
4292 free(parent_path);
4294 view_vborder(view);
4295 return err;
4298 static const struct got_error *
4299 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4300 struct tog_view **focus_view, struct tog_view *view, int ch)
4302 const struct got_error *err = NULL;
4303 struct tog_tree_view_state *s = &view->state.tree;
4304 struct tog_view *log_view;
4305 int begin_x = 0, nscrolled;
4307 switch (ch) {
4308 case 'i':
4309 s->show_ids = !s->show_ids;
4310 break;
4311 case 'l':
4312 if (!s->selected_entry)
4313 break;
4314 if (view_is_parent_view(view))
4315 begin_x = view_split_begin_x(view->begin_x);
4316 err = log_tree_entry(&log_view, begin_x,
4317 s->selected_entry, &s->parents,
4318 s->commit_id, s->refs, s->repo);
4319 if (view_is_parent_view(view)) {
4320 err = view_close_child(view);
4321 if (err)
4322 return err;
4323 err = view_set_child(view, log_view);
4324 if (err) {
4325 view_close(log_view);
4326 break;
4328 *focus_view = log_view;
4329 view->child_focussed = 1;
4330 } else
4331 *new_view = log_view;
4332 break;
4333 case 'k':
4334 case KEY_UP:
4335 if (s->selected > 0) {
4336 s->selected--;
4337 if (s->selected == 0)
4338 break;
4340 if (s->selected > 0)
4341 break;
4342 tree_scroll_up(view, &s->first_displayed_entry, 1,
4343 s->entries, s->tree == s->root);
4344 break;
4345 case KEY_PPAGE:
4346 tree_scroll_up(view, &s->first_displayed_entry,
4347 MAX(0, view->nlines - 4 - s->selected), s->entries,
4348 s->tree == s->root);
4349 s->selected = 0;
4350 if (SIMPLEQ_FIRST(&s->entries->head) ==
4351 s->first_displayed_entry && s->tree != s->root)
4352 s->first_displayed_entry = NULL;
4353 break;
4354 case 'j':
4355 case KEY_DOWN:
4356 if (s->selected < s->ndisplayed - 1) {
4357 s->selected++;
4358 break;
4360 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4361 /* can't scroll any further */
4362 break;
4363 tree_scroll_down(&s->first_displayed_entry, 1,
4364 s->last_displayed_entry, s->entries);
4365 break;
4366 case KEY_NPAGE:
4367 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4368 == NULL) {
4369 /* can't scroll any further; move cursor down */
4370 if (s->selected < s->ndisplayed - 1)
4371 s->selected = s->ndisplayed - 1;
4372 break;
4374 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4375 view->nlines, s->last_displayed_entry, s->entries);
4376 if (nscrolled < view->nlines) {
4377 int ndisplayed = 0;
4378 struct got_tree_entry *te;
4379 te = s->first_displayed_entry;
4380 do {
4381 ndisplayed++;
4382 te = SIMPLEQ_NEXT(te, entry);
4383 } while (te);
4384 s->selected = ndisplayed - 1;
4386 break;
4387 case KEY_ENTER:
4388 case '\r':
4389 case KEY_BACKSPACE:
4390 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4391 struct tog_parent_tree *parent;
4392 /* user selected '..' */
4393 if (s->tree == s->root)
4394 break;
4395 parent = TAILQ_FIRST(&s->parents);
4396 TAILQ_REMOVE(&s->parents, parent,
4397 entry);
4398 got_object_tree_close(s->tree);
4399 s->tree = parent->tree;
4400 s->entries =
4401 got_object_tree_get_entries(s->tree);
4402 s->first_displayed_entry =
4403 parent->first_displayed_entry;
4404 s->selected_entry =
4405 parent->selected_entry;
4406 s->selected = parent->selected;
4407 free(parent);
4408 } else if (S_ISDIR(s->selected_entry->mode)) {
4409 struct got_tree_object *subtree;
4410 err = got_object_open_as_tree(&subtree,
4411 s->repo, s->selected_entry->id);
4412 if (err)
4413 break;
4414 err = tree_view_visit_subtree(subtree, s);
4415 if (err) {
4416 got_object_tree_close(subtree);
4417 break;
4419 } else if (S_ISREG(s->selected_entry->mode)) {
4420 struct tog_view *blame_view;
4421 int begin_x = view_is_parent_view(view) ?
4422 view_split_begin_x(view->begin_x) : 0;
4424 err = blame_tree_entry(&blame_view, begin_x,
4425 s->selected_entry, &s->parents,
4426 s->commit_id, s->refs, s->repo);
4427 if (err)
4428 break;
4429 if (view_is_parent_view(view)) {
4430 err = view_close_child(view);
4431 if (err)
4432 return err;
4433 err = view_set_child(view, blame_view);
4434 if (err) {
4435 view_close(blame_view);
4436 break;
4438 *focus_view = blame_view;
4439 view->child_focussed = 1;
4440 } else
4441 *new_view = blame_view;
4443 break;
4444 case KEY_RESIZE:
4445 if (s->selected > view->nlines)
4446 s->selected = s->ndisplayed - 1;
4447 break;
4448 default:
4449 break;
4452 return err;
4455 __dead static void
4456 usage_tree(void)
4458 endwin();
4459 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4460 getprogname());
4461 exit(1);
4464 static const struct got_error *
4465 cmd_tree(int argc, char *argv[])
4467 const struct got_error *error;
4468 struct got_repository *repo = NULL;
4469 struct got_reflist_head refs;
4470 char *repo_path = NULL;
4471 struct got_object_id *commit_id = NULL;
4472 char *commit_id_arg = NULL;
4473 struct got_commit_object *commit = NULL;
4474 struct got_tree_object *tree = NULL;
4475 int ch;
4476 struct tog_view *view;
4478 SIMPLEQ_INIT(&refs);
4480 #ifndef PROFILE
4481 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4482 NULL) == -1)
4483 err(1, "pledge");
4484 #endif
4486 while ((ch = getopt(argc, argv, "c:")) != -1) {
4487 switch (ch) {
4488 case 'c':
4489 commit_id_arg = optarg;
4490 break;
4491 default:
4492 usage_tree();
4493 /* NOTREACHED */
4497 argc -= optind;
4498 argv += optind;
4500 if (argc == 0) {
4501 struct got_worktree *worktree;
4502 char *cwd = getcwd(NULL, 0);
4503 if (cwd == NULL)
4504 return got_error_from_errno("getcwd");
4505 error = got_worktree_open(&worktree, cwd);
4506 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4507 goto done;
4508 if (worktree) {
4509 free(cwd);
4510 repo_path =
4511 strdup(got_worktree_get_repo_path(worktree));
4512 got_worktree_close(worktree);
4513 } else
4514 repo_path = cwd;
4515 if (repo_path == NULL) {
4516 error = got_error_from_errno("strdup");
4517 goto done;
4519 } else if (argc == 1) {
4520 repo_path = realpath(argv[0], NULL);
4521 if (repo_path == NULL)
4522 return got_error_from_errno2("realpath", argv[0]);
4523 } else
4524 usage_log();
4526 init_curses();
4528 error = got_repo_open(&repo, repo_path);
4529 if (error != NULL)
4530 goto done;
4532 error = apply_unveil(got_repo_get_path(repo), NULL);
4533 if (error)
4534 goto done;
4536 if (commit_id_arg == NULL)
4537 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4538 else {
4539 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4540 if (error) {
4541 if (error->code != GOT_ERR_NOT_REF)
4542 goto done;
4543 error = got_repo_match_object_id_prefix(&commit_id,
4544 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4547 if (error != NULL)
4548 goto done;
4550 error = got_object_open_as_commit(&commit, repo, commit_id);
4551 if (error != NULL)
4552 goto done;
4554 error = got_object_open_as_tree(&tree, repo,
4555 got_object_commit_get_tree_id(commit));
4556 if (error != NULL)
4557 goto done;
4559 error = got_ref_list(&refs, repo);
4560 if (error)
4561 goto done;
4563 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4564 if (view == NULL) {
4565 error = got_error_from_errno("view_open");
4566 goto done;
4568 error = open_tree_view(view, tree, commit_id, &refs, repo);
4569 if (error)
4570 goto done;
4571 error = view_loop(view);
4572 done:
4573 free(repo_path);
4574 free(commit_id);
4575 if (commit)
4576 got_object_commit_close(commit);
4577 if (tree)
4578 got_object_tree_close(tree);
4579 if (repo)
4580 got_repo_close(repo);
4581 got_ref_list_free(&refs);
4582 return error;
4585 static void
4586 list_commands(void)
4588 int i;
4590 fprintf(stderr, "commands:");
4591 for (i = 0; i < nitems(tog_commands); i++) {
4592 struct tog_cmd *cmd = &tog_commands[i];
4593 fprintf(stderr, " %s", cmd->name);
4595 fputc('\n', stderr);
4598 __dead static void
4599 usage(int hflag)
4601 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4602 getprogname());
4603 if (hflag)
4604 list_commands();
4605 exit(1);
4608 static char **
4609 make_argv(const char *arg0, const char *arg1)
4611 char **argv;
4612 int argc = (arg1 == NULL ? 1 : 2);
4614 argv = calloc(argc, sizeof(char *));
4615 if (argv == NULL)
4616 err(1, "calloc");
4617 argv[0] = strdup(arg0);
4618 if (argv[0] == NULL)
4619 err(1, "calloc");
4620 if (arg1) {
4621 argv[1] = strdup(arg1);
4622 if (argv[1] == NULL)
4623 err(1, "calloc");
4626 return argv;
4629 int
4630 main(int argc, char *argv[])
4632 const struct got_error *error = NULL;
4633 struct tog_cmd *cmd = NULL;
4634 int ch, hflag = 0, Vflag = 0;
4635 char **cmd_argv = NULL;
4637 setlocale(LC_CTYPE, "");
4639 while ((ch = getopt(argc, argv, "hV")) != -1) {
4640 switch (ch) {
4641 case 'h':
4642 hflag = 1;
4643 break;
4644 case 'V':
4645 Vflag = 1;
4646 break;
4647 default:
4648 usage(hflag);
4649 /* NOTREACHED */
4653 argc -= optind;
4654 argv += optind;
4655 optind = 0;
4656 optreset = 1;
4658 if (Vflag) {
4659 got_version_print_str();
4660 return 1;
4663 if (argc == 0) {
4664 if (hflag)
4665 usage(hflag);
4666 /* Build an argument vector which runs a default command. */
4667 cmd = &tog_commands[0];
4668 cmd_argv = make_argv(cmd->name, NULL);
4669 argc = 1;
4670 } else {
4671 int i;
4673 /* Did the user specific a command? */
4674 for (i = 0; i < nitems(tog_commands); i++) {
4675 if (strncmp(tog_commands[i].name, argv[0],
4676 strlen(argv[0])) == 0) {
4677 cmd = &tog_commands[i];
4678 break;
4682 if (cmd == NULL) {
4683 fprintf(stderr, "%s: unknown command '%s'\n",
4684 getprogname(), argv[0]);
4685 list_commands();
4686 return 1;
4690 if (hflag)
4691 cmd->cmd_usage();
4692 else
4693 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4695 endwin();
4696 free(cmd_argv);
4697 if (error)
4698 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4699 return 0;