Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
39 #include <regex.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_utf8.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_path.h"
54 #include "got_worktree.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 #define CTRL(x) ((x) & 0x1f)
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct tog_cmd {
71 const char *name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 };
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_diff_view_state {
116 struct got_object_id *id1, *id2;
117 FILE *f;
118 int first_displayed_line;
119 int last_displayed_line;
120 int eof;
121 int diff_context;
122 struct got_repository *repo;
123 struct got_reflist_head *refs;
125 /* passed from log view; may be NULL */
126 struct tog_view *log_view;
127 };
129 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 struct tog_log_thread_args {
132 pthread_cond_t need_commits;
133 int commits_needed;
134 struct got_commit_graph *graph;
135 struct commit_queue *commits;
136 const char *in_repo_path;
137 struct got_object_id *start_id;
138 struct got_repository *repo;
139 int log_complete;
140 sig_atomic_t *quit;
141 struct tog_view *view;
142 struct commit_queue_entry **first_displayed_entry;
143 struct commit_queue_entry **selected_entry;
144 };
146 struct tog_log_view_state {
147 struct commit_queue commits;
148 struct commit_queue_entry *first_displayed_entry;
149 struct commit_queue_entry *last_displayed_entry;
150 struct commit_queue_entry *selected_entry;
151 int selected;
152 char *in_repo_path;
153 const char *head_ref_name;
154 struct got_repository *repo;
155 struct got_reflist_head *refs;
156 struct got_object_id *start_id;
157 sig_atomic_t quit;
158 pthread_t thread;
159 struct tog_log_thread_args thread_args;
160 struct commit_queue_entry *matched_entry;
161 struct commit_queue_entry *search_entry;
162 };
164 struct tog_blame_cb_args {
165 struct tog_blame_line *lines; /* one per line */
166 int nlines;
168 struct tog_view *view;
169 struct got_object_id *commit_id;
170 int *quit;
171 };
173 struct tog_blame_thread_args {
174 const char *path;
175 struct got_repository *repo;
176 struct tog_blame_cb_args *cb_args;
177 int *complete;
178 got_cancel_cb cancel_cb;
179 void *cancel_arg;
180 };
182 struct tog_blame {
183 FILE *f;
184 size_t filesize;
185 struct tog_blame_line *lines;
186 int nlines;
187 off_t *line_offsets;
188 pthread_t thread;
189 struct tog_blame_thread_args thread_args;
190 struct tog_blame_cb_args cb_args;
191 const char *path;
192 };
194 struct tog_blame_view_state {
195 int first_displayed_line;
196 int last_displayed_line;
197 int selected_line;
198 int blame_complete;
199 int eof;
200 int done;
201 struct got_object_id_queue blamed_commits;
202 struct got_object_qid *blamed_commit;
203 char *path;
204 struct got_repository *repo;
205 struct got_reflist_head *refs;
206 struct got_object_id *commit_id;
207 struct tog_blame blame;
208 int matched_line;
209 };
211 struct tog_parent_tree {
212 TAILQ_ENTRY(tog_parent_tree) entry;
213 struct got_tree_object *tree;
214 struct got_tree_entry *first_displayed_entry;
215 struct got_tree_entry *selected_entry;
216 int selected;
217 };
219 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
221 struct tog_tree_view_state {
222 char *tree_label;
223 struct got_tree_object *root;
224 struct got_tree_object *tree;
225 const struct got_tree_entries *entries;
226 struct got_tree_entry *first_displayed_entry;
227 struct got_tree_entry *last_displayed_entry;
228 struct got_tree_entry *selected_entry;
229 int ndisplayed, selected, show_ids;
230 struct tog_parent_trees parents;
231 struct got_object_id *commit_id;
232 struct got_repository *repo;
233 struct got_reflist_head *refs;
234 struct got_tree_entry *matched_entry;
235 };
237 /*
238 * We implement two types of views: parent views and child views.
240 * The 'Tab' key switches between a parent view and its child view.
241 * Child views are shown side-by-side to their parent view, provided
242 * there is enough screen estate.
244 * When a new view is opened from within a parent view, this new view
245 * becomes a child view of the parent view, replacing any existing child.
247 * When a new view is opened from within a child view, this new view
248 * becomes a parent view which will obscure the views below until the
249 * user quits the new parent view by typing 'q'.
251 * This list of views contains parent views only.
252 * Child views are only pointed to by their parent view.
253 */
254 TAILQ_HEAD(tog_view_list_head, tog_view);
256 struct tog_view {
257 TAILQ_ENTRY(tog_view) entry;
258 WINDOW *window;
259 PANEL *panel;
260 int nlines, ncols, begin_y, begin_x;
261 int lines, cols; /* copies of LINES and COLS */
262 int focussed;
263 struct tog_view *parent;
264 struct tog_view *child;
265 int child_focussed;
267 /* type-specific state */
268 enum tog_view_type type;
269 union {
270 struct tog_diff_view_state diff;
271 struct tog_log_view_state log;
272 struct tog_blame_view_state blame;
273 struct tog_tree_view_state tree;
274 } state;
276 const struct got_error *(*show)(struct tog_view *);
277 const struct got_error *(*input)(struct tog_view **,
278 struct tog_view **, struct tog_view**, struct tog_view *, int);
279 const struct got_error *(*close)(struct tog_view *);
281 const struct got_error *(*search_start)(struct tog_view *);
282 const struct got_error *(*search_next)(struct tog_view *);
283 int searching;
284 #define TOG_SEARCH_FORWARD 1
285 #define TOG_SEARCH_BACKWARD 2
286 int search_next_done;
287 regex_t regex;
288 };
290 static const struct got_error *open_diff_view(struct tog_view *,
291 struct got_object_id *, struct got_object_id *, struct tog_view *,
292 struct got_reflist_head *, struct got_repository *);
293 static const struct got_error *show_diff_view(struct tog_view *);
294 static const struct got_error *input_diff_view(struct tog_view **,
295 struct tog_view **, struct tog_view **, struct tog_view *, int);
296 static const struct got_error* close_diff_view(struct tog_view *);
298 static const struct got_error *open_log_view(struct tog_view *,
299 struct got_object_id *, struct got_reflist_head *,
300 struct got_repository *, const char *, const char *, int);
301 static const struct got_error * show_log_view(struct tog_view *);
302 static const struct got_error *input_log_view(struct tog_view **,
303 struct tog_view **, struct tog_view **, struct tog_view *, int);
304 static const struct got_error *close_log_view(struct tog_view *);
305 static const struct got_error *search_start_log_view(struct tog_view *);
306 static const struct got_error *search_next_log_view(struct tog_view *);
308 static const struct got_error *open_blame_view(struct tog_view *, char *,
309 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
310 static const struct got_error *show_blame_view(struct tog_view *);
311 static const struct got_error *input_blame_view(struct tog_view **,
312 struct tog_view **, struct tog_view **, struct tog_view *, int);
313 static const struct got_error *close_blame_view(struct tog_view *);
314 static const struct got_error *search_start_blame_view(struct tog_view *);
315 static const struct got_error *search_next_blame_view(struct tog_view *);
317 static const struct got_error *open_tree_view(struct tog_view *,
318 struct got_tree_object *, struct got_object_id *,
319 struct got_reflist_head *, struct got_repository *);
320 static const struct got_error *show_tree_view(struct tog_view *);
321 static const struct got_error *input_tree_view(struct tog_view **,
322 struct tog_view **, struct tog_view **, struct tog_view *, int);
323 static const struct got_error *close_tree_view(struct tog_view *);
324 static const struct got_error *search_start_tree_view(struct tog_view *);
325 static const struct got_error *search_next_tree_view(struct tog_view *);
327 static volatile sig_atomic_t tog_sigwinch_received;
328 static volatile sig_atomic_t tog_sigpipe_received;
330 static void
331 tog_sigwinch(int signo)
333 tog_sigwinch_received = 1;
336 static void
337 tog_sigpipe(int signo)
339 tog_sigpipe_received = 1;
342 static const struct got_error *
343 view_close(struct tog_view *view)
345 const struct got_error *err = NULL;
347 if (view->child) {
348 view_close(view->child);
349 view->child = NULL;
351 if (view->close)
352 err = view->close(view);
353 if (view->panel)
354 del_panel(view->panel);
355 if (view->window)
356 delwin(view->window);
357 free(view);
358 return err;
361 static struct tog_view *
362 view_open(int nlines, int ncols, int begin_y, int begin_x,
363 enum tog_view_type type)
365 struct tog_view *view = calloc(1, sizeof(*view));
367 if (view == NULL)
368 return NULL;
370 view->type = type;
371 view->lines = LINES;
372 view->cols = COLS;
373 view->nlines = nlines ? nlines : LINES - begin_y;
374 view->ncols = ncols ? ncols : COLS - begin_x;
375 view->begin_y = begin_y;
376 view->begin_x = begin_x;
377 view->window = newwin(nlines, ncols, begin_y, begin_x);
378 if (view->window == NULL) {
379 view_close(view);
380 return NULL;
382 view->panel = new_panel(view->window);
383 if (view->panel == NULL ||
384 set_panel_userptr(view->panel, view) != OK) {
385 view_close(view);
386 return NULL;
389 keypad(view->window, TRUE);
390 return view;
393 static int
394 view_split_begin_x(int begin_x)
396 if (begin_x > 0 || COLS < 120)
397 return 0;
398 return (COLS - MAX(COLS / 2, 80));
401 static const struct got_error *view_resize(struct tog_view *);
403 static const struct got_error *
404 view_splitscreen(struct tog_view *view)
406 const struct got_error *err = NULL;
408 view->begin_y = 0;
409 view->begin_x = view_split_begin_x(0);
410 view->nlines = LINES;
411 view->ncols = COLS - view->begin_x;
412 view->lines = LINES;
413 view->cols = COLS;
414 err = view_resize(view);
415 if (err)
416 return err;
418 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
419 return got_error_from_errno("mvwin");
421 return NULL;
424 static const struct got_error *
425 view_fullscreen(struct tog_view *view)
427 const struct got_error *err = NULL;
429 view->begin_x = 0;
430 view->begin_y = 0;
431 view->nlines = LINES;
432 view->ncols = COLS;
433 view->lines = LINES;
434 view->cols = COLS;
435 err = view_resize(view);
436 if (err)
437 return err;
439 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
440 return got_error_from_errno("mvwin");
442 return NULL;
445 static int
446 view_is_parent_view(struct tog_view *view)
448 return view->parent == NULL;
451 static const struct got_error *
452 view_resize(struct tog_view *view)
454 int nlines, ncols;
456 if (view->lines > LINES)
457 nlines = view->nlines - (view->lines - LINES);
458 else
459 nlines = view->nlines + (LINES - view->lines);
461 if (view->cols > COLS)
462 ncols = view->ncols - (view->cols - COLS);
463 else
464 ncols = view->ncols + (COLS - view->cols);
466 if (wresize(view->window, nlines, ncols) == ERR)
467 return got_error_from_errno("wresize");
468 if (replace_panel(view->panel, view->window) == ERR)
469 return got_error_from_errno("replace_panel");
470 wclear(view->window);
472 view->nlines = nlines;
473 view->ncols = ncols;
474 view->lines = LINES;
475 view->cols = COLS;
477 if (view->child) {
478 view->child->begin_x = view_split_begin_x(view->begin_x);
479 if (view->child->begin_x == 0) {
480 view_fullscreen(view->child);
481 if (view->child->focussed)
482 show_panel(view->child->panel);
483 else
484 show_panel(view->panel);
485 } else {
486 view_splitscreen(view->child);
487 show_panel(view->child->panel);
491 return NULL;
494 static const struct got_error *
495 view_close_child(struct tog_view *view)
497 const struct got_error *err = NULL;
499 if (view->child == NULL)
500 return NULL;
502 err = view_close(view->child);
503 view->child = NULL;
504 return err;
507 static const struct got_error *
508 view_set_child(struct tog_view *view, struct tog_view *child)
510 const struct got_error *err = NULL;
512 view->child = child;
513 child->parent = view;
514 return err;
517 static int
518 view_is_splitscreen(struct tog_view *view)
520 return view->begin_x > 0;
523 static void
524 tog_resizeterm(void)
526 int cols, lines;
527 struct winsize size;
529 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
530 cols = 80; /* Default */
531 lines = 24;
532 } else {
533 cols = size.ws_col;
534 lines = size.ws_row;
536 resize_term(lines, cols);
539 static const struct got_error *
540 view_search_start(struct tog_view *view)
542 const struct got_error *err = NULL;
543 char pattern[1024];
544 int ret;
545 int begin_x = 0;
547 if (view->nlines < 1)
548 return NULL;
550 if (!view_is_parent_view(view))
551 begin_x = view_split_begin_x(view->begin_x);
552 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
553 begin_x, "/");
554 wclrtoeol(view->window);
556 nocbreak();
557 echo();
558 ret = wgetnstr(view->window, pattern, sizeof(pattern));
559 cbreak();
560 noecho();
561 if (ret == ERR)
562 return NULL;
564 if (view->searching) {
565 regfree(&view->regex);
566 view->searching = 0;
569 if (regcomp(&view->regex, pattern,
570 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
571 err = view->search_start(view);
572 if (err) {
573 regfree(&view->regex);
574 return err;
576 view->searching = TOG_SEARCH_FORWARD;
577 view->search_next_done = 0;
578 view->search_next(view);
581 return NULL;
584 static const struct got_error *
585 view_input(struct tog_view **new, struct tog_view **dead,
586 struct tog_view **focus, int *done, struct tog_view *view,
587 struct tog_view_list_head *views)
589 const struct got_error *err = NULL;
590 struct tog_view *v;
591 int ch, errcode;
593 *new = NULL;
594 *dead = NULL;
595 *focus = NULL;
597 if (view->searching && !view->search_next_done) {
598 errcode = pthread_mutex_unlock(&tog_mutex);
599 if (errcode)
600 return got_error_set_errno(errcode,
601 "pthread_mutex_unlock");
602 pthread_yield();
603 errcode = pthread_mutex_lock(&tog_mutex);
604 if (errcode)
605 return got_error_set_errno(errcode,
606 "pthread_mutex_lock");
607 view->search_next(view);
608 return NULL;
611 nodelay(stdscr, FALSE);
612 /* Allow threads to make progress while we are waiting for input. */
613 errcode = pthread_mutex_unlock(&tog_mutex);
614 if (errcode)
615 return got_error_set_errno(errcode, "pthread_mutex_unlock");
616 ch = wgetch(view->window);
617 errcode = pthread_mutex_lock(&tog_mutex);
618 if (errcode)
619 return got_error_set_errno(errcode, "pthread_mutex_lock");
620 nodelay(stdscr, TRUE);
622 if (tog_sigwinch_received) {
623 tog_resizeterm();
624 tog_sigwinch_received = 0;
625 TAILQ_FOREACH(v, views, entry) {
626 err = view_resize(v);
627 if (err)
628 return err;
629 err = v->input(new, dead, focus, v, KEY_RESIZE);
630 if (err)
631 return err;
635 switch (ch) {
636 case ERR:
637 break;
638 case '\t':
639 if (view->child) {
640 *focus = view->child;
641 view->child_focussed = 1;
642 } else if (view->parent) {
643 *focus = view->parent;
644 view->parent->child_focussed = 0;
646 break;
647 case 'q':
648 err = view->input(new, dead, focus, view, ch);
649 *dead = view;
650 break;
651 case 'Q':
652 *done = 1;
653 break;
654 case 'f':
655 if (view_is_parent_view(view)) {
656 if (view->child == NULL)
657 break;
658 if (view_is_splitscreen(view->child)) {
659 *focus = view->child;
660 view->child_focussed = 1;
661 err = view_fullscreen(view->child);
662 } else
663 err = view_splitscreen(view->child);
664 if (err)
665 break;
666 err = view->child->input(new, dead, focus,
667 view->child, KEY_RESIZE);
668 } else {
669 if (view_is_splitscreen(view)) {
670 *focus = view;
671 view->parent->child_focussed = 1;
672 err = view_fullscreen(view);
673 } else {
674 err = view_splitscreen(view);
676 if (err)
677 break;
678 err = view->input(new, dead, focus, view,
679 KEY_RESIZE);
681 break;
682 case KEY_RESIZE:
683 break;
684 case '/':
685 if (view->search_start)
686 view_search_start(view);
687 else
688 err = view->input(new, dead, focus, view, ch);
689 break;
690 case 'N':
691 case 'n':
692 if (view->search_next && view->searching) {
693 view->searching = (ch == 'n' ?
694 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
695 view->search_next_done = 0;
696 view->search_next(view);
697 } else
698 err = view->input(new, dead, focus, view, ch);
699 break;
700 default:
701 err = view->input(new, dead, focus, view, ch);
702 break;
705 return err;
708 void
709 view_vborder(struct tog_view *view)
711 PANEL *panel;
712 struct tog_view *view_above;
714 if (view->parent)
715 return view_vborder(view->parent);
717 panel = panel_above(view->panel);
718 if (panel == NULL)
719 return;
721 view_above = panel_userptr(panel);
722 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
723 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
726 int
727 view_needs_focus_indication(struct tog_view *view)
729 if (view_is_parent_view(view)) {
730 if (view->child == NULL || view->child_focussed)
731 return 0;
732 if (!view_is_splitscreen(view->child))
733 return 0;
734 } else if (!view_is_splitscreen(view))
735 return 0;
737 return view->focussed;
740 static const struct got_error *
741 view_loop(struct tog_view *view)
743 const struct got_error *err = NULL;
744 struct tog_view_list_head views;
745 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
746 int fast_refresh = 10;
747 int done = 0, errcode;
749 errcode = pthread_mutex_lock(&tog_mutex);
750 if (errcode)
751 return got_error_set_errno(errcode, "pthread_mutex_lock");
753 TAILQ_INIT(&views);
754 TAILQ_INSERT_HEAD(&views, view, entry);
756 main_view = view;
757 view->focussed = 1;
758 err = view->show(view);
759 if (err)
760 return err;
761 update_panels();
762 doupdate();
763 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
764 /* Refresh fast during initialization, then become slower. */
765 if (fast_refresh && fast_refresh-- == 0)
766 halfdelay(10); /* switch to once per second */
768 err = view_input(&new_view, &dead_view, &focus_view, &done,
769 view, &views);
770 if (err)
771 break;
772 if (dead_view) {
773 struct tog_view *prev = NULL;
775 if (view_is_parent_view(dead_view))
776 prev = TAILQ_PREV(dead_view,
777 tog_view_list_head, entry);
778 else if (view->parent != dead_view)
779 prev = view->parent;
781 if (dead_view->parent)
782 dead_view->parent->child = NULL;
783 else
784 TAILQ_REMOVE(&views, dead_view, entry);
786 err = view_close(dead_view);
787 if (err || (dead_view == main_view && new_view == NULL))
788 goto done;
790 if (view == dead_view) {
791 if (focus_view)
792 view = focus_view;
793 else if (prev)
794 view = prev;
795 else if (!TAILQ_EMPTY(&views))
796 view = TAILQ_LAST(&views,
797 tog_view_list_head);
798 else
799 view = NULL;
800 if (view) {
801 if (view->child && view->child_focussed)
802 focus_view = view->child;
803 else
804 focus_view = view;
808 if (new_view) {
809 struct tog_view *v, *t;
810 /* Only allow one parent view per type. */
811 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
812 if (v->type != new_view->type)
813 continue;
814 TAILQ_REMOVE(&views, v, entry);
815 err = view_close(v);
816 if (err)
817 goto done;
818 break;
820 TAILQ_INSERT_TAIL(&views, new_view, entry);
821 view = new_view;
822 if (focus_view == NULL)
823 focus_view = new_view;
825 if (focus_view) {
826 show_panel(focus_view->panel);
827 if (view)
828 view->focussed = 0;
829 focus_view->focussed = 1;
830 view = focus_view;
831 if (new_view)
832 show_panel(new_view->panel);
833 if (view->child && view_is_splitscreen(view->child))
834 show_panel(view->child->panel);
836 if (view) {
837 if (focus_view == NULL) {
838 view->focussed = 1;
839 show_panel(view->panel);
840 if (view->child && view_is_splitscreen(view->child))
841 show_panel(view->child->panel);
842 focus_view = view;
844 if (view->parent) {
845 err = view->parent->show(view->parent);
846 if (err)
847 goto done;
849 err = view->show(view);
850 if (err)
851 goto done;
852 if (view->child) {
853 err = view->child->show(view->child);
854 if (err)
855 goto done;
857 update_panels();
858 doupdate();
861 done:
862 while (!TAILQ_EMPTY(&views)) {
863 view = TAILQ_FIRST(&views);
864 TAILQ_REMOVE(&views, view, entry);
865 view_close(view);
868 errcode = pthread_mutex_unlock(&tog_mutex);
869 if (errcode && err == NULL)
870 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
872 return err;
875 __dead static void
876 usage_log(void)
878 endwin();
879 fprintf(stderr,
880 "usage: %s log [-c commit] [-r repository-path] [path]\n",
881 getprogname());
882 exit(1);
885 /* Create newly allocated wide-character string equivalent to a byte string. */
886 static const struct got_error *
887 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
889 char *vis = NULL;
890 const struct got_error *err = NULL;
892 *ws = NULL;
893 *wlen = mbstowcs(NULL, s, 0);
894 if (*wlen == (size_t)-1) {
895 int vislen;
896 if (errno != EILSEQ)
897 return got_error_from_errno("mbstowcs");
899 /* byte string invalid in current encoding; try to "fix" it */
900 err = got_mbsavis(&vis, &vislen, s);
901 if (err)
902 return err;
903 *wlen = mbstowcs(NULL, vis, 0);
904 if (*wlen == (size_t)-1) {
905 err = got_error_from_errno("mbstowcs"); /* give up */
906 goto done;
910 *ws = calloc(*wlen + 1, sizeof(**ws));
911 if (*ws == NULL) {
912 err = got_error_from_errno("calloc");
913 goto done;
916 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
917 err = got_error_from_errno("mbstowcs");
918 done:
919 free(vis);
920 if (err) {
921 free(*ws);
922 *ws = NULL;
923 *wlen = 0;
925 return err;
928 /* Format a line for display, ensuring that it won't overflow a width limit. */
929 static const struct got_error *
930 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
931 int col_tab_align)
933 const struct got_error *err = NULL;
934 int cols = 0;
935 wchar_t *wline = NULL;
936 size_t wlen;
937 int i;
939 *wlinep = NULL;
940 *widthp = 0;
942 err = mbs2ws(&wline, &wlen, line);
943 if (err)
944 return err;
946 i = 0;
947 while (i < wlen) {
948 int width = wcwidth(wline[i]);
950 if (width == 0) {
951 i++;
952 continue;
955 if (width == 1 || width == 2) {
956 if (cols + width > wlimit)
957 break;
958 cols += width;
959 i++;
960 } else if (width == -1) {
961 if (wline[i] == L'\t') {
962 width = TABSIZE -
963 ((cols + col_tab_align) % TABSIZE);
964 if (cols + width > wlimit)
965 break;
966 cols += width;
968 i++;
969 } else {
970 err = got_error_from_errno("wcwidth");
971 goto done;
974 wline[i] = L'\0';
975 if (widthp)
976 *widthp = cols;
977 done:
978 if (err)
979 free(wline);
980 else
981 *wlinep = wline;
982 return err;
985 static const struct got_error*
986 build_refs_str(char **refs_str, struct got_reflist_head *refs,
987 struct got_object_id *id, struct got_repository *repo)
989 static const struct got_error *err = NULL;
990 struct got_reflist_entry *re;
991 char *s;
992 const char *name;
994 *refs_str = NULL;
996 SIMPLEQ_FOREACH(re, refs, entry) {
997 struct got_tag_object *tag = NULL;
998 int cmp;
1000 name = got_ref_get_name(re->ref);
1001 if (strcmp(name, GOT_REF_HEAD) == 0)
1002 continue;
1003 if (strncmp(name, "refs/", 5) == 0)
1004 name += 5;
1005 if (strncmp(name, "got/", 4) == 0)
1006 continue;
1007 if (strncmp(name, "heads/", 6) == 0)
1008 name += 6;
1009 if (strncmp(name, "remotes/", 8) == 0)
1010 name += 8;
1011 if (strncmp(name, "tags/", 5) == 0) {
1012 err = got_object_open_as_tag(&tag, repo, re->id);
1013 if (err) {
1014 if (err->code != GOT_ERR_OBJ_TYPE)
1015 break;
1016 /* Ref points at something other than a tag. */
1017 err = NULL;
1018 tag = NULL;
1021 cmp = got_object_id_cmp(tag ?
1022 got_object_tag_get_object_id(tag) : re->id, id);
1023 if (tag)
1024 got_object_tag_close(tag);
1025 if (cmp != 0)
1026 continue;
1027 s = *refs_str;
1028 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1029 s ? ", " : "", name) == -1) {
1030 err = got_error_from_errno("asprintf");
1031 free(s);
1032 *refs_str = NULL;
1033 break;
1035 free(s);
1038 return err;
1041 static const struct got_error *
1042 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1043 int col_tab_align)
1045 char *smallerthan, *at;
1047 smallerthan = strchr(author, '<');
1048 if (smallerthan && smallerthan[1] != '\0')
1049 author = smallerthan + 1;
1050 at = strchr(author, '@');
1051 if (at)
1052 *at = '\0';
1053 return format_line(wauthor, author_width, author, limit, col_tab_align);
1056 static const struct got_error *
1057 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1058 struct got_object_id *id, struct got_reflist_head *refs,
1059 const size_t date_display_cols, int author_display_cols)
1061 const struct got_error *err = NULL;
1062 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1063 char *logmsg0 = NULL, *logmsg = NULL;
1064 char *author = NULL;
1065 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1066 int author_width, logmsg_width;
1067 char *newline, *line = NULL;
1068 int col, limit;
1069 const int avail = view->ncols;
1070 struct tm tm;
1071 time_t committer_time;
1073 committer_time = got_object_commit_get_committer_time(commit);
1074 if (localtime_r(&committer_time, &tm) == NULL)
1075 return got_error_from_errno("localtime_r");
1076 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1077 >= sizeof(datebuf))
1078 return got_error(GOT_ERR_NO_SPACE);
1080 if (avail <= date_display_cols)
1081 limit = MIN(sizeof(datebuf) - 1, avail);
1082 else
1083 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1084 waddnstr(view->window, datebuf, limit);
1085 col = limit;
1086 if (col > avail)
1087 goto done;
1089 author = strdup(got_object_commit_get_author(commit));
1090 if (author == NULL) {
1091 err = got_error_from_errno("strdup");
1092 goto done;
1094 err = format_author(&wauthor, &author_width, author, avail - col, col);
1095 if (err)
1096 goto done;
1097 waddwstr(view->window, wauthor);
1098 col += author_width;
1099 while (col < avail && author_width < author_display_cols + 2) {
1100 waddch(view->window, ' ');
1101 col++;
1102 author_width++;
1104 if (col > avail)
1105 goto done;
1107 err = got_object_commit_get_logmsg(&logmsg0, commit);
1108 if (err)
1109 goto done;
1110 logmsg = logmsg0;
1111 while (*logmsg == '\n')
1112 logmsg++;
1113 newline = strchr(logmsg, '\n');
1114 if (newline)
1115 *newline = '\0';
1116 limit = avail - col;
1117 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1118 if (err)
1119 goto done;
1120 waddwstr(view->window, wlogmsg);
1121 col += logmsg_width;
1122 while (col < avail) {
1123 waddch(view->window, ' ');
1124 col++;
1126 done:
1127 free(logmsg0);
1128 free(wlogmsg);
1129 free(author);
1130 free(wauthor);
1131 free(line);
1132 return err;
1135 static struct commit_queue_entry *
1136 alloc_commit_queue_entry(struct got_commit_object *commit,
1137 struct got_object_id *id)
1139 struct commit_queue_entry *entry;
1141 entry = calloc(1, sizeof(*entry));
1142 if (entry == NULL)
1143 return NULL;
1145 entry->id = id;
1146 entry->commit = commit;
1147 return entry;
1150 static void
1151 pop_commit(struct commit_queue *commits)
1153 struct commit_queue_entry *entry;
1155 entry = TAILQ_FIRST(&commits->head);
1156 TAILQ_REMOVE(&commits->head, entry, entry);
1157 got_object_commit_close(entry->commit);
1158 commits->ncommits--;
1159 /* Don't free entry->id! It is owned by the commit graph. */
1160 free(entry);
1163 static void
1164 free_commits(struct commit_queue *commits)
1166 while (!TAILQ_EMPTY(&commits->head))
1167 pop_commit(commits);
1170 static const struct got_error *
1171 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1172 int minqueue, struct got_repository *repo, const char *path)
1174 const struct got_error *err = NULL;
1175 int nqueued = 0;
1178 * We keep all commits open throughout the lifetime of the log
1179 * view in order to avoid having to re-fetch commits from disk
1180 * while updating the display.
1182 while (nqueued < minqueue) {
1183 struct got_object_id *id;
1184 struct got_commit_object *commit;
1185 struct commit_queue_entry *entry;
1186 int errcode;
1188 err = got_commit_graph_iter_next(&id, graph);
1189 if (err) {
1190 if (err->code != GOT_ERR_ITER_NEED_MORE)
1191 break;
1192 err = got_commit_graph_fetch_commits(graph,
1193 minqueue, repo, NULL, NULL);
1194 if (err)
1195 return err;
1196 continue;
1199 if (id == NULL)
1200 break;
1202 err = got_object_open_as_commit(&commit, repo, id);
1203 if (err)
1204 break;
1205 entry = alloc_commit_queue_entry(commit, id);
1206 if (entry == NULL) {
1207 err = got_error_from_errno("alloc_commit_queue_entry");
1208 break;
1211 errcode = pthread_mutex_lock(&tog_mutex);
1212 if (errcode) {
1213 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1214 break;
1217 entry->idx = commits->ncommits;
1218 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1219 nqueued++;
1220 commits->ncommits++;
1222 errcode = pthread_mutex_unlock(&tog_mutex);
1223 if (errcode && err == NULL)
1224 err = got_error_set_errno(errcode,
1225 "pthread_mutex_unlock");
1228 return err;
1231 static const struct got_error *
1232 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1233 struct got_repository *repo)
1235 const struct got_error *err = NULL;
1236 struct got_reference *head_ref;
1238 *head_id = NULL;
1240 err = got_ref_open(&head_ref, repo, branch_name, 0);
1241 if (err)
1242 return err;
1244 err = got_ref_resolve(head_id, repo, head_ref);
1245 got_ref_close(head_ref);
1246 if (err) {
1247 *head_id = NULL;
1248 return err;
1251 return NULL;
1254 static const struct got_error *
1255 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1256 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1257 struct commit_queue *commits, int selected_idx, int limit,
1258 struct got_reflist_head *refs, const char *path, int commits_needed)
1260 const struct got_error *err = NULL;
1261 struct tog_log_view_state *s = &view->state.log;
1262 struct commit_queue_entry *entry;
1263 int width;
1264 int ncommits, author_cols = 10;
1265 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1266 char *refs_str = NULL;
1267 wchar_t *wline;
1268 static const size_t date_display_cols = 9;
1270 entry = first;
1271 ncommits = 0;
1272 while (entry) {
1273 if (ncommits == selected_idx) {
1274 *selected = entry;
1275 break;
1277 entry = TAILQ_NEXT(entry, entry);
1278 ncommits++;
1281 if (*selected && !(view->searching && view->search_next_done == 0)) {
1282 err = got_object_id_str(&id_str, (*selected)->id);
1283 if (err)
1284 return err;
1285 if (refs) {
1286 err = build_refs_str(&refs_str, refs, (*selected)->id,
1287 s->repo);
1288 if (err)
1289 goto done;
1293 if (commits_needed == 0)
1294 halfdelay(10); /* disable fast refresh */
1296 if (asprintf(&ncommits_str, " [%d/%d] %s",
1297 entry ? entry->idx + 1 : 0, commits->ncommits,
1298 commits_needed > 0 ?
1299 (view->searching && view->search_next_done == 0
1300 ? "searching..." : "loading... ") :
1301 (refs_str ? refs_str : "")) == -1) {
1302 err = got_error_from_errno("asprintf");
1303 goto done;
1306 if (path && strcmp(path, "/") != 0) {
1307 if (asprintf(&header, "commit %s %s%s",
1308 id_str ? id_str : "........................................",
1309 path, ncommits_str) == -1) {
1310 err = got_error_from_errno("asprintf");
1311 header = NULL;
1312 goto done;
1314 } else if (asprintf(&header, "commit %s%s",
1315 id_str ? id_str : "........................................",
1316 ncommits_str) == -1) {
1317 err = got_error_from_errno("asprintf");
1318 header = NULL;
1319 goto done;
1321 err = format_line(&wline, &width, header, view->ncols, 0);
1322 if (err)
1323 goto done;
1325 werase(view->window);
1327 if (view_needs_focus_indication(view))
1328 wstandout(view->window);
1329 waddwstr(view->window, wline);
1330 while (width < view->ncols) {
1331 waddch(view->window, ' ');
1332 width++;
1334 if (view_needs_focus_indication(view))
1335 wstandend(view->window);
1336 free(wline);
1337 if (limit <= 1)
1338 goto done;
1340 /* Grow author column size if necessary. */
1341 entry = first;
1342 ncommits = 0;
1343 while (entry) {
1344 char *author;
1345 wchar_t *wauthor;
1346 int width;
1347 if (ncommits >= limit - 1)
1348 break;
1349 author = strdup(got_object_commit_get_author(entry->commit));
1350 if (author == NULL) {
1351 err = got_error_from_errno("strdup");
1352 goto done;
1354 err = format_author(&wauthor, &width, author, COLS,
1355 date_display_cols);
1356 if (author_cols < width)
1357 author_cols = width;
1358 free(wauthor);
1359 free(author);
1360 entry = TAILQ_NEXT(entry, entry);
1363 entry = first;
1364 *last = first;
1365 ncommits = 0;
1366 while (entry) {
1367 if (ncommits >= limit - 1)
1368 break;
1369 if (ncommits == selected_idx)
1370 wstandout(view->window);
1371 err = draw_commit(view, entry->commit, entry->id, refs,
1372 date_display_cols, author_cols);
1373 if (ncommits == selected_idx)
1374 wstandend(view->window);
1375 if (err)
1376 goto done;
1377 ncommits++;
1378 *last = entry;
1379 entry = TAILQ_NEXT(entry, entry);
1382 view_vborder(view);
1383 done:
1384 free(id_str);
1385 free(refs_str);
1386 free(ncommits_str);
1387 free(header);
1388 return err;
1391 static void
1392 scroll_up(struct tog_view *view,
1393 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1394 struct commit_queue *commits)
1396 struct commit_queue_entry *entry;
1397 int nscrolled = 0;
1399 entry = TAILQ_FIRST(&commits->head);
1400 if (*first_displayed_entry == entry)
1401 return;
1403 entry = *first_displayed_entry;
1404 while (entry && nscrolled < maxscroll) {
1405 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1406 if (entry) {
1407 *first_displayed_entry = entry;
1408 nscrolled++;
1413 static const struct got_error *
1414 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1415 pthread_cond_t *need_commits)
1417 int errcode;
1418 int max_wait = 20;
1420 halfdelay(1); /* fast refresh while loading commits */
1422 while (*commits_needed > 0) {
1423 if (*log_complete)
1424 break;
1426 /* Wake the log thread. */
1427 errcode = pthread_cond_signal(need_commits);
1428 if (errcode)
1429 return got_error_set_errno(errcode,
1430 "pthread_cond_signal");
1431 errcode = pthread_mutex_unlock(&tog_mutex);
1432 if (errcode)
1433 return got_error_set_errno(errcode,
1434 "pthread_mutex_unlock");
1435 pthread_yield();
1436 errcode = pthread_mutex_lock(&tog_mutex);
1437 if (errcode)
1438 return got_error_set_errno(errcode,
1439 "pthread_mutex_lock");
1441 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1443 * Thread is not done yet; lose a key press
1444 * and let the user retry... this way the GUI
1445 * remains interactive while logging deep paths
1446 * with few commits in history.
1448 return NULL;
1452 return NULL;
1455 static const struct got_error *
1456 scroll_down(struct tog_view *view,
1457 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1458 struct commit_queue_entry **last_displayed_entry,
1459 struct commit_queue *commits, int *log_complete, int *commits_needed,
1460 pthread_cond_t *need_commits)
1462 const struct got_error *err = NULL;
1463 struct commit_queue_entry *pentry;
1464 int nscrolled = 0;
1466 if (*last_displayed_entry == NULL)
1467 return NULL;
1469 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1470 if (pentry == NULL && !*log_complete) {
1472 * Ask the log thread for required amount of commits
1473 * plus some amount of pre-fetching.
1475 (*commits_needed) += maxscroll + 20;
1476 err = trigger_log_thread(0, commits_needed, log_complete,
1477 need_commits);
1478 if (err)
1479 return err;
1482 do {
1483 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1484 if (pentry == NULL)
1485 break;
1487 *last_displayed_entry = pentry;
1489 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1490 if (pentry == NULL)
1491 break;
1492 *first_displayed_entry = pentry;
1493 } while (++nscrolled < maxscroll);
1495 return err;
1498 static const struct got_error *
1499 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1500 struct got_commit_object *commit, struct got_object_id *commit_id,
1501 struct tog_view *log_view, struct got_reflist_head *refs,
1502 struct got_repository *repo)
1504 const struct got_error *err;
1505 struct got_object_qid *parent_id;
1506 struct tog_view *diff_view;
1508 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1509 if (diff_view == NULL)
1510 return got_error_from_errno("view_open");
1512 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1513 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1514 commit_id, log_view, refs, repo);
1515 if (err == NULL)
1516 *new_view = diff_view;
1517 return err;
1520 static const struct got_error *
1521 tree_view_visit_subtree(struct got_tree_object *subtree,
1522 struct tog_tree_view_state *s)
1524 struct tog_parent_tree *parent;
1526 parent = calloc(1, sizeof(*parent));
1527 if (parent == NULL)
1528 return got_error_from_errno("calloc");
1530 parent->tree = s->tree;
1531 parent->first_displayed_entry = s->first_displayed_entry;
1532 parent->selected_entry = s->selected_entry;
1533 parent->selected = s->selected;
1534 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1535 s->tree = subtree;
1536 s->entries = got_object_tree_get_entries(s->tree);
1537 s->selected = 0;
1538 s->first_displayed_entry = NULL;
1539 return NULL;
1543 static const struct got_error *
1544 browse_commit_tree(struct tog_view **new_view, int begin_x,
1545 struct commit_queue_entry *entry, const char *path,
1546 struct got_reflist_head *refs, struct got_repository *repo)
1548 const struct got_error *err = NULL;
1549 struct got_tree_object *tree;
1550 struct got_tree_entry *te;
1551 struct tog_tree_view_state *s;
1552 struct tog_view *tree_view;
1553 char *slash, *subpath = NULL;
1554 const char *p;
1556 err = got_object_open_as_tree(&tree, repo,
1557 got_object_commit_get_tree_id(entry->commit));
1558 if (err)
1559 return err;
1561 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1562 if (tree_view == NULL)
1563 return got_error_from_errno("view_open");
1565 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1566 if (err) {
1567 got_object_tree_close(tree);
1568 return err;
1570 s = &tree_view->state.tree;
1572 *new_view = tree_view;
1574 /* Walk the path and open corresponding tree objects. */
1575 p = path;
1576 while (p[0] == '/')
1577 p++;
1578 while (*p) {
1579 struct got_object_id *tree_id;
1581 /* Ensure the correct subtree entry is selected. */
1582 slash = strchr(p, '/');
1583 if (slash == NULL)
1584 slash = strchr(p, '\0');
1585 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1586 if (strncmp(p, te->name, slash - p) == 0) {
1587 s->selected_entry = te;
1588 break;
1590 s->selected++;
1592 if (s->selected_entry == NULL) {
1593 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1594 break;
1596 if (s->tree != s->root)
1597 s->selected++; /* skip '..' */
1599 if (!S_ISDIR(s->selected_entry->mode)) {
1600 /* Jump to this file's entry. */
1601 s->first_displayed_entry = s->selected_entry;
1602 s->selected = 0;
1603 break;
1606 slash = strchr(p, '/');
1607 if (slash)
1608 subpath = strndup(path, slash - path);
1609 else
1610 subpath = strdup(path);
1611 if (subpath == NULL) {
1612 err = got_error_from_errno("strdup");
1613 break;
1616 err = got_object_id_by_path(&tree_id, repo, entry->id,
1617 subpath);
1618 if (err)
1619 break;
1621 err = got_object_open_as_tree(&tree, repo, tree_id);
1622 free(tree_id);
1623 if (err)
1624 break;
1626 err = tree_view_visit_subtree(tree, s);
1627 if (err) {
1628 got_object_tree_close(tree);
1629 break;
1631 if (slash == NULL)
1632 break;
1633 free(subpath);
1634 subpath = NULL;
1635 p = slash;
1638 free(subpath);
1639 return err;
1642 static void *
1643 log_thread(void *arg)
1645 const struct got_error *err = NULL;
1646 int errcode = 0;
1647 struct tog_log_thread_args *a = arg;
1648 int done = 0;
1650 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1651 NULL, NULL);
1652 if (err)
1653 return (void *)err;
1655 while (!done && !err && !tog_sigpipe_received) {
1656 err = queue_commits(a->graph, a->commits, 1, a->repo,
1657 a->in_repo_path);
1658 if (err) {
1659 if (err->code != GOT_ERR_ITER_COMPLETED)
1660 return (void *)err;
1661 err = NULL;
1662 done = 1;
1663 } else if (a->commits_needed > 0)
1664 a->commits_needed--;
1666 errcode = pthread_mutex_lock(&tog_mutex);
1667 if (errcode) {
1668 err = got_error_set_errno(errcode,
1669 "pthread_mutex_lock");
1670 break;
1671 } else if (*a->quit)
1672 done = 1;
1673 else if (*a->first_displayed_entry == NULL) {
1674 *a->first_displayed_entry =
1675 TAILQ_FIRST(&a->commits->head);
1676 *a->selected_entry = *a->first_displayed_entry;
1679 if (done)
1680 a->commits_needed = 0;
1681 else if (a->commits_needed == 0) {
1682 errcode = pthread_cond_wait(&a->need_commits,
1683 &tog_mutex);
1684 if (errcode)
1685 err = got_error_set_errno(errcode,
1686 "pthread_cond_wait");
1689 errcode = pthread_mutex_unlock(&tog_mutex);
1690 if (errcode && err == NULL)
1691 err = got_error_set_errno(errcode,
1692 "pthread_mutex_unlock");
1694 a->log_complete = 1;
1695 return (void *)err;
1698 static const struct got_error *
1699 stop_log_thread(struct tog_log_view_state *s)
1701 const struct got_error *err = NULL;
1702 int errcode;
1704 if (s->thread) {
1705 s->quit = 1;
1706 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1707 if (errcode)
1708 return got_error_set_errno(errcode,
1709 "pthread_cond_signal");
1710 errcode = pthread_mutex_unlock(&tog_mutex);
1711 if (errcode)
1712 return got_error_set_errno(errcode,
1713 "pthread_mutex_unlock");
1714 errcode = pthread_join(s->thread, (void **)&err);
1715 if (errcode)
1716 return got_error_set_errno(errcode, "pthread_join");
1717 errcode = pthread_mutex_lock(&tog_mutex);
1718 if (errcode)
1719 return got_error_set_errno(errcode,
1720 "pthread_mutex_lock");
1721 s->thread = NULL;
1724 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1725 if (errcode && err == NULL)
1726 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1728 if (s->thread_args.repo) {
1729 got_repo_close(s->thread_args.repo);
1730 s->thread_args.repo = NULL;
1733 if (s->thread_args.graph) {
1734 got_commit_graph_close(s->thread_args.graph);
1735 s->thread_args.graph = NULL;
1738 return err;
1741 static const struct got_error *
1742 close_log_view(struct tog_view *view)
1744 const struct got_error *err = NULL;
1745 struct tog_log_view_state *s = &view->state.log;
1747 err = stop_log_thread(s);
1748 free_commits(&s->commits);
1749 free(s->in_repo_path);
1750 s->in_repo_path = NULL;
1751 free(s->start_id);
1752 s->start_id = NULL;
1753 return err;
1756 static const struct got_error *
1757 search_start_log_view(struct tog_view *view)
1759 struct tog_log_view_state *s = &view->state.log;
1761 s->matched_entry = NULL;
1762 s->search_entry = NULL;
1763 return NULL;
1766 static int
1767 match_commit(struct got_commit_object *commit, const char *id_str,
1768 const char *logmsg, regex_t *regex)
1770 regmatch_t regmatch;
1772 if (regexec(regex, got_object_commit_get_author(commit), 1,
1773 &regmatch, 0) == 0 ||
1774 regexec(regex, got_object_commit_get_committer(commit), 1,
1775 &regmatch, 0) == 0 ||
1776 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1777 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1778 return 1;
1780 return 0;
1783 static const struct got_error *
1784 search_next_log_view(struct tog_view *view)
1786 const struct got_error *err = NULL;
1787 struct tog_log_view_state *s = &view->state.log;
1788 struct commit_queue_entry *entry;
1790 if (!view->searching) {
1791 view->search_next_done = 1;
1792 return NULL;
1795 if (s->search_entry) {
1796 if (wgetch(view->window) == KEY_BACKSPACE) {
1797 view->search_next_done = 1;
1798 return NULL;
1800 if (view->searching == TOG_SEARCH_FORWARD)
1801 entry = TAILQ_NEXT(s->search_entry, entry);
1802 else
1803 entry = TAILQ_PREV(s->search_entry,
1804 commit_queue_head, entry);
1805 } else if (s->matched_entry) {
1806 if (view->searching == TOG_SEARCH_FORWARD)
1807 entry = TAILQ_NEXT(s->selected_entry, entry);
1808 else
1809 entry = TAILQ_PREV(s->selected_entry,
1810 commit_queue_head, entry);
1811 } else {
1812 if (view->searching == TOG_SEARCH_FORWARD)
1813 entry = TAILQ_FIRST(&s->commits.head);
1814 else
1815 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1818 while (1) {
1819 char *id_str, *logmsg;
1820 if (entry == NULL) {
1821 if (s->thread_args.log_complete ||
1822 view->searching == TOG_SEARCH_BACKWARD) {
1823 view->search_next_done = 1;
1824 return NULL;
1827 * Poke the log thread for more commits and return,
1828 * allowing the main loop to make progress. Search
1829 * will resume at s->search_entry once we come back.
1831 s->thread_args.commits_needed++;
1832 return trigger_log_thread(1,
1833 &s->thread_args.commits_needed,
1834 &s->thread_args.log_complete,
1835 &s->thread_args.need_commits);
1838 err = got_object_id_str(&id_str, entry->id);
1839 if (err)
1840 return err;
1842 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1843 if (err)
1844 return err;
1845 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1846 free(logmsg);
1847 view->search_next_done = 1;
1848 s->matched_entry = entry;
1849 free(id_str);
1850 break;
1852 free(logmsg);
1853 free(id_str);
1854 s->search_entry = entry;
1855 if (view->searching == TOG_SEARCH_FORWARD)
1856 entry = TAILQ_NEXT(entry, entry);
1857 else
1858 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1861 if (s->matched_entry) {
1862 int cur = s->selected_entry->idx;
1863 while (cur < s->matched_entry->idx) {
1864 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1865 if (err)
1866 return err;
1867 cur++;
1869 while (cur > s->matched_entry->idx) {
1870 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1871 if (err)
1872 return err;
1873 cur--;
1877 s->search_entry = NULL;
1879 return NULL;
1882 static const struct got_error *
1883 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1884 struct got_reflist_head *refs, struct got_repository *repo,
1885 const char *head_ref_name, const char *path, int check_disk)
1887 const struct got_error *err = NULL;
1888 struct tog_log_view_state *s = &view->state.log;
1889 struct got_repository *thread_repo = NULL;
1890 struct got_commit_graph *thread_graph = NULL;
1891 int errcode;
1893 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1894 if (err != NULL)
1895 goto done;
1897 /* The commit queue only contains commits being displayed. */
1898 TAILQ_INIT(&s->commits.head);
1899 s->commits.ncommits = 0;
1901 s->refs = refs;
1902 s->repo = repo;
1903 s->head_ref_name = head_ref_name;
1904 s->start_id = got_object_id_dup(start_id);
1905 if (s->start_id == NULL) {
1906 err = got_error_from_errno("got_object_id_dup");
1907 goto done;
1910 view->show = show_log_view;
1911 view->input = input_log_view;
1912 view->close = close_log_view;
1913 view->search_start = search_start_log_view;
1914 view->search_next = search_next_log_view;
1916 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1917 if (err)
1918 goto done;
1919 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1920 0, thread_repo);
1921 if (err)
1922 goto done;
1924 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1925 if (errcode) {
1926 err = got_error_set_errno(errcode, "pthread_cond_init");
1927 goto done;
1930 s->thread_args.commits_needed = view->nlines;
1931 s->thread_args.graph = thread_graph;
1932 s->thread_args.commits = &s->commits;
1933 s->thread_args.in_repo_path = s->in_repo_path;
1934 s->thread_args.start_id = s->start_id;
1935 s->thread_args.repo = thread_repo;
1936 s->thread_args.log_complete = 0;
1937 s->thread_args.quit = &s->quit;
1938 s->thread_args.view = view;
1939 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1940 s->thread_args.selected_entry = &s->selected_entry;
1941 done:
1942 if (err)
1943 close_log_view(view);
1944 return err;
1947 static const struct got_error *
1948 show_log_view(struct tog_view *view)
1950 struct tog_log_view_state *s = &view->state.log;
1952 if (s->thread == NULL) {
1953 int errcode = pthread_create(&s->thread, NULL, log_thread,
1954 &s->thread_args);
1955 if (errcode)
1956 return got_error_set_errno(errcode, "pthread_create");
1959 return draw_commits(view, &s->last_displayed_entry,
1960 &s->selected_entry, s->first_displayed_entry,
1961 &s->commits, s->selected, view->nlines, s->refs,
1962 s->in_repo_path, s->thread_args.commits_needed);
1965 static const struct got_error *
1966 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1967 struct tog_view **focus_view, struct tog_view *view, int ch)
1969 const struct got_error *err = NULL;
1970 struct tog_log_view_state *s = &view->state.log;
1971 char *parent_path, *in_repo_path = NULL;
1972 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1973 int begin_x = 0;
1974 struct got_object_id *start_id;
1976 switch (ch) {
1977 case 'q':
1978 s->quit = 1;
1979 break;
1980 case 'k':
1981 case KEY_UP:
1982 case '<':
1983 case ',':
1984 if (s->first_displayed_entry == NULL)
1985 break;
1986 if (s->selected > 0)
1987 s->selected--;
1988 else
1989 scroll_up(view, &s->first_displayed_entry, 1,
1990 &s->commits);
1991 break;
1992 case KEY_PPAGE:
1993 case CTRL('b'):
1994 if (s->first_displayed_entry == NULL)
1995 break;
1996 if (TAILQ_FIRST(&s->commits.head) ==
1997 s->first_displayed_entry) {
1998 s->selected = 0;
1999 break;
2001 scroll_up(view, &s->first_displayed_entry,
2002 view->nlines, &s->commits);
2003 break;
2004 case 'j':
2005 case KEY_DOWN:
2006 case '>':
2007 case '.':
2008 if (s->first_displayed_entry == NULL)
2009 break;
2010 if (s->selected < MIN(view->nlines - 2,
2011 s->commits.ncommits - 1)) {
2012 s->selected++;
2013 break;
2015 err = scroll_down(view, &s->first_displayed_entry, 1,
2016 &s->last_displayed_entry, &s->commits,
2017 &s->thread_args.log_complete,
2018 &s->thread_args.commits_needed,
2019 &s->thread_args.need_commits);
2020 break;
2021 case KEY_NPAGE:
2022 case CTRL('f'): {
2023 struct commit_queue_entry *first;
2024 first = s->first_displayed_entry;
2025 if (first == NULL)
2026 break;
2027 err = scroll_down(view, &s->first_displayed_entry,
2028 view->nlines, &s->last_displayed_entry,
2029 &s->commits, &s->thread_args.log_complete,
2030 &s->thread_args.commits_needed,
2031 &s->thread_args.need_commits);
2032 if (err)
2033 break;
2034 if (first == s->first_displayed_entry &&
2035 s->selected < MIN(view->nlines - 2,
2036 s->commits.ncommits - 1)) {
2037 /* can't scroll further down */
2038 s->selected = MIN(view->nlines - 2,
2039 s->commits.ncommits - 1);
2041 err = NULL;
2042 break;
2044 case KEY_RESIZE:
2045 if (s->selected > view->nlines - 2)
2046 s->selected = view->nlines - 2;
2047 if (s->selected > s->commits.ncommits - 1)
2048 s->selected = s->commits.ncommits - 1;
2049 break;
2050 case KEY_ENTER:
2051 case ' ':
2052 case '\r':
2053 if (s->selected_entry == NULL)
2054 break;
2055 if (view_is_parent_view(view))
2056 begin_x = view_split_begin_x(view->begin_x);
2057 err = open_diff_view_for_commit(&diff_view, begin_x,
2058 s->selected_entry->commit, s->selected_entry->id,
2059 view, s->refs, s->repo);
2060 if (err)
2061 break;
2062 if (view_is_parent_view(view)) {
2063 err = view_close_child(view);
2064 if (err)
2065 return err;
2066 err = view_set_child(view, diff_view);
2067 if (err) {
2068 view_close(diff_view);
2069 break;
2071 *focus_view = diff_view;
2072 view->child_focussed = 1;
2073 } else
2074 *new_view = diff_view;
2075 break;
2076 case 't':
2077 if (s->selected_entry == NULL)
2078 break;
2079 if (view_is_parent_view(view))
2080 begin_x = view_split_begin_x(view->begin_x);
2081 err = browse_commit_tree(&tree_view, begin_x,
2082 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2083 if (err)
2084 break;
2085 if (view_is_parent_view(view)) {
2086 err = view_close_child(view);
2087 if (err)
2088 return err;
2089 err = view_set_child(view, tree_view);
2090 if (err) {
2091 view_close(tree_view);
2092 break;
2094 *focus_view = tree_view;
2095 view->child_focussed = 1;
2096 } else
2097 *new_view = tree_view;
2098 break;
2099 case KEY_BACKSPACE:
2100 if (strcmp(s->in_repo_path, "/") == 0)
2101 break;
2102 parent_path = dirname(s->in_repo_path);
2103 if (parent_path && strcmp(parent_path, ".") != 0) {
2104 err = stop_log_thread(s);
2105 if (err)
2106 return err;
2107 lv = view_open(view->nlines, view->ncols,
2108 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2109 if (lv == NULL)
2110 return got_error_from_errno(
2111 "view_open");
2112 err = open_log_view(lv, s->start_id, s->refs,
2113 s->repo, s->head_ref_name, parent_path, 0);
2114 if (err)
2115 return err;;
2116 if (view_is_parent_view(view))
2117 *new_view = lv;
2118 else {
2119 view_set_child(view->parent, lv);
2120 *focus_view = lv;
2122 return NULL;
2124 break;
2125 case CTRL('l'):
2126 err = stop_log_thread(s);
2127 if (err)
2128 return err;
2129 lv = view_open(view->nlines, view->ncols,
2130 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2131 if (lv == NULL)
2132 return got_error_from_errno("view_open");
2133 err = get_head_commit_id(&start_id, s->head_ref_name ?
2134 s->head_ref_name : GOT_REF_HEAD, s->repo);
2135 if (err) {
2136 view_close(lv);
2137 return err;
2139 in_repo_path = strdup(s->in_repo_path);
2140 if (in_repo_path == NULL) {
2141 free(start_id);
2142 view_close(lv);
2143 return got_error_from_errno("strdup");
2145 got_ref_list_free(s->refs);
2146 err = got_ref_list(s->refs, s->repo, NULL,
2147 got_ref_cmp_by_name, NULL);
2148 if (err) {
2149 free(start_id);
2150 view_close(lv);
2151 return err;
2153 err = open_log_view(lv, start_id, s->refs, s->repo,
2154 s->head_ref_name, in_repo_path, 0);
2155 if (err) {
2156 free(start_id);
2157 view_close(lv);
2158 return err;;
2160 *dead_view = view;
2161 *new_view = lv;
2162 break;
2163 default:
2164 break;
2167 return err;
2170 static const struct got_error *
2171 apply_unveil(const char *repo_path, const char *worktree_path)
2173 const struct got_error *error;
2175 #ifdef PROFILE
2176 if (unveil("gmon.out", "rwc") != 0)
2177 return got_error_from_errno2("unveil", "gmon.out");
2178 #endif
2179 if (repo_path && unveil(repo_path, "r") != 0)
2180 return got_error_from_errno2("unveil", repo_path);
2182 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2183 return got_error_from_errno2("unveil", worktree_path);
2185 if (unveil("/tmp", "rwc") != 0)
2186 return got_error_from_errno2("unveil", "/tmp");
2188 error = got_privsep_unveil_exec_helpers();
2189 if (error != NULL)
2190 return error;
2192 if (unveil(NULL, NULL) != 0)
2193 return got_error_from_errno("unveil");
2195 return NULL;
2198 static void
2199 init_curses(void)
2201 initscr();
2202 cbreak();
2203 halfdelay(1); /* Do fast refresh while initial view is loading. */
2204 noecho();
2205 nonl();
2206 intrflush(stdscr, FALSE);
2207 keypad(stdscr, TRUE);
2208 curs_set(0);
2209 signal(SIGWINCH, tog_sigwinch);
2210 signal(SIGPIPE, tog_sigpipe);
2213 static const struct got_error *
2214 cmd_log(int argc, char *argv[])
2216 const struct got_error *error;
2217 struct got_repository *repo = NULL;
2218 struct got_worktree *worktree = NULL;
2219 struct got_reflist_head refs;
2220 struct got_object_id *start_id = NULL;
2221 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2222 char *start_commit = NULL, *head_ref_name = NULL;
2223 int ch;
2224 struct tog_view *view;
2226 SIMPLEQ_INIT(&refs);
2228 #ifndef PROFILE
2229 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2230 NULL) == -1)
2231 err(1, "pledge");
2232 #endif
2234 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2235 switch (ch) {
2236 case 'c':
2237 start_commit = optarg;
2238 break;
2239 case 'r':
2240 repo_path = realpath(optarg, NULL);
2241 if (repo_path == NULL)
2242 err(1, "-r option");
2243 break;
2244 default:
2245 usage_log();
2246 /* NOTREACHED */
2250 argc -= optind;
2251 argv += optind;
2253 cwd = getcwd(NULL, 0);
2254 if (cwd == NULL) {
2255 error = got_error_from_errno("getcwd");
2256 goto done;
2258 error = got_worktree_open(&worktree, cwd);
2259 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2260 goto done;
2261 error = NULL;
2263 if (argc == 0) {
2264 path = strdup("");
2265 if (path == NULL) {
2266 error = got_error_from_errno("strdup");
2267 goto done;
2269 } else if (argc == 1) {
2270 if (worktree) {
2271 error = got_worktree_resolve_path(&path, worktree,
2272 argv[0]);
2273 if (error)
2274 goto done;
2275 } else {
2276 path = strdup(argv[0]);
2277 if (path == NULL) {
2278 error = got_error_from_errno("strdup");
2279 goto done;
2282 } else
2283 usage_log();
2285 if (repo_path == NULL) {
2286 if (worktree)
2287 repo_path = strdup(
2288 got_worktree_get_repo_path(worktree));
2289 else
2290 repo_path = strdup(cwd);
2292 if (repo_path == NULL) {
2293 error = got_error_from_errno("strdup");
2294 goto done;
2297 init_curses();
2299 error = got_repo_open(&repo, repo_path, NULL);
2300 if (error != NULL)
2301 goto done;
2303 error = apply_unveil(got_repo_get_path(repo),
2304 worktree ? got_worktree_get_root_path(worktree) : NULL);
2305 if (error)
2306 goto done;
2308 if (start_commit == NULL)
2309 error = get_head_commit_id(&start_id, worktree ?
2310 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2311 repo);
2312 else {
2313 error = get_head_commit_id(&start_id, start_commit, repo);
2314 if (error) {
2315 if (error->code != GOT_ERR_NOT_REF)
2316 goto done;
2317 error = got_repo_match_object_id_prefix(&start_id,
2318 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2321 if (error != NULL)
2322 goto done;
2324 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2325 if (error)
2326 goto done;
2328 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2329 if (view == NULL) {
2330 error = got_error_from_errno("view_open");
2331 goto done;
2333 if (worktree) {
2334 head_ref_name = strdup(
2335 got_worktree_get_head_ref_name(worktree));
2336 if (head_ref_name == NULL) {
2337 error = got_error_from_errno("strdup");
2338 goto done;
2341 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2342 path, 1);
2343 if (error)
2344 goto done;
2345 if (worktree) {
2346 /* Release work tree lock. */
2347 got_worktree_close(worktree);
2348 worktree = NULL;
2350 error = view_loop(view);
2351 done:
2352 free(repo_path);
2353 free(cwd);
2354 free(path);
2355 free(start_id);
2356 free(head_ref_name);
2357 if (repo)
2358 got_repo_close(repo);
2359 if (worktree)
2360 got_worktree_close(worktree);
2361 got_ref_list_free(&refs);
2362 return error;
2365 __dead static void
2366 usage_diff(void)
2368 endwin();
2369 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2370 getprogname());
2371 exit(1);
2374 static char *
2375 parse_next_line(FILE *f, size_t *len)
2377 char *line;
2378 size_t linelen;
2379 size_t lineno;
2380 const char delim[3] = { '\0', '\0', '\0'};
2382 line = fparseln(f, &linelen, &lineno, delim, 0);
2383 if (len)
2384 *len = linelen;
2385 return line;
2388 static const struct got_error *
2389 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2390 int *last_displayed_line, int *eof, int max_lines,
2391 char *header)
2393 const struct got_error *err;
2394 int nlines = 0, nprinted = 0;
2395 char *line;
2396 size_t len;
2397 wchar_t *wline;
2398 int width;
2400 rewind(f);
2401 werase(view->window);
2403 if (header) {
2404 err = format_line(&wline, &width, header, view->ncols, 0);
2405 if (err) {
2406 return err;
2409 if (view_needs_focus_indication(view))
2410 wstandout(view->window);
2411 waddwstr(view->window, wline);
2412 if (view_needs_focus_indication(view))
2413 wstandend(view->window);
2414 if (width <= view->ncols - 1)
2415 waddch(view->window, '\n');
2417 if (max_lines <= 1)
2418 return NULL;
2419 max_lines--;
2422 *eof = 0;
2423 while (nprinted < max_lines) {
2424 line = parse_next_line(f, &len);
2425 if (line == NULL) {
2426 *eof = 1;
2427 break;
2429 if (++nlines < *first_displayed_line) {
2430 free(line);
2431 continue;
2434 err = format_line(&wline, &width, line, view->ncols, 0);
2435 if (err) {
2436 free(line);
2437 return err;
2439 waddwstr(view->window, wline);
2440 if (width <= view->ncols - 1)
2441 waddch(view->window, '\n');
2442 if (++nprinted == 1)
2443 *first_displayed_line = nlines;
2444 free(line);
2445 free(wline);
2446 wline = NULL;
2448 *last_displayed_line = nlines;
2450 view_vborder(view);
2452 if (*eof) {
2453 while (nprinted < view->nlines) {
2454 waddch(view->window, '\n');
2455 nprinted++;
2458 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2459 if (err) {
2460 return err;
2463 wstandout(view->window);
2464 waddwstr(view->window, wline);
2465 wstandend(view->window);
2468 return NULL;
2471 static char *
2472 get_datestr(time_t *time, char *datebuf)
2474 struct tm mytm, *tm;
2475 char *p, *s;
2477 tm = gmtime_r(time, &mytm);
2478 if (tm == NULL)
2479 return NULL;
2480 s = asctime_r(tm, datebuf);
2481 if (s == NULL)
2482 return NULL;
2483 p = strchr(s, '\n');
2484 if (p)
2485 *p = '\0';
2486 return s;
2489 static const struct got_error *
2490 write_commit_info(struct got_object_id *commit_id,
2491 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2493 const struct got_error *err = NULL;
2494 char datebuf[26], *datestr;
2495 struct got_commit_object *commit;
2496 char *id_str = NULL, *logmsg = NULL;
2497 time_t committer_time;
2498 const char *author, *committer;
2499 char *refs_str = NULL;
2501 if (refs) {
2502 err = build_refs_str(&refs_str, refs, commit_id, repo);
2503 if (err)
2504 return err;
2507 err = got_object_open_as_commit(&commit, repo, commit_id);
2508 if (err)
2509 return err;
2511 err = got_object_id_str(&id_str, commit_id);
2512 if (err) {
2513 err = got_error_from_errno("got_object_id_str");
2514 goto done;
2517 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2518 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2519 err = got_error_from_errno("fprintf");
2520 goto done;
2522 if (fprintf(outfile, "from: %s\n",
2523 got_object_commit_get_author(commit)) < 0) {
2524 err = got_error_from_errno("fprintf");
2525 goto done;
2527 committer_time = got_object_commit_get_committer_time(commit);
2528 datestr = get_datestr(&committer_time, datebuf);
2529 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2530 err = got_error_from_errno("fprintf");
2531 goto done;
2533 author = got_object_commit_get_author(commit);
2534 committer = got_object_commit_get_committer(commit);
2535 if (strcmp(author, committer) != 0 &&
2536 fprintf(outfile, "via: %s\n", committer) < 0) {
2537 err = got_error_from_errno("fprintf");
2538 goto done;
2540 err = got_object_commit_get_logmsg(&logmsg, commit);
2541 if (err)
2542 goto done;
2543 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2544 err = got_error_from_errno("fprintf");
2545 goto done;
2547 done:
2548 free(id_str);
2549 free(logmsg);
2550 free(refs_str);
2551 got_object_commit_close(commit);
2552 return err;
2555 static const struct got_error *
2556 create_diff(struct tog_diff_view_state *s)
2558 const struct got_error *err = NULL;
2559 FILE *f = NULL;
2560 int obj_type;
2562 f = got_opentemp();
2563 if (f == NULL) {
2564 err = got_error_from_errno("got_opentemp");
2565 goto done;
2567 if (s->f && fclose(s->f) != 0) {
2568 err = got_error_from_errno("fclose");
2569 goto done;
2571 s->f = f;
2573 if (s->id1)
2574 err = got_object_get_type(&obj_type, s->repo, s->id1);
2575 else
2576 err = got_object_get_type(&obj_type, s->repo, s->id2);
2577 if (err)
2578 goto done;
2580 switch (obj_type) {
2581 case GOT_OBJ_TYPE_BLOB:
2582 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2583 s->diff_context, 0, s->repo, f);
2584 break;
2585 case GOT_OBJ_TYPE_TREE:
2586 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2587 s->diff_context, 0, s->repo, f);
2588 break;
2589 case GOT_OBJ_TYPE_COMMIT: {
2590 const struct got_object_id_queue *parent_ids;
2591 struct got_object_qid *pid;
2592 struct got_commit_object *commit2;
2594 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2595 if (err)
2596 break;
2597 /* Show commit info if we're diffing to a parent/root commit. */
2598 if (s->id1 == NULL)
2599 write_commit_info(s->id2, s->refs, s->repo, f);
2600 else {
2601 parent_ids = got_object_commit_get_parent_ids(commit2);
2602 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2603 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2604 write_commit_info(s->id2, s->refs,
2605 s->repo, f);
2606 break;
2610 got_object_commit_close(commit2);
2612 err = got_diff_objects_as_commits(s->id1, s->id2,
2613 s->diff_context, 0, s->repo, f);
2614 break;
2616 default:
2617 err = got_error(GOT_ERR_OBJ_TYPE);
2618 break;
2620 done:
2621 if (f && fflush(f) != 0 && err == NULL)
2622 err = got_error_from_errno("fflush");
2623 return err;
2626 static void
2627 diff_view_indicate_progress(struct tog_view *view)
2629 mvwaddstr(view->window, 0, 0, "diffing...");
2630 update_panels();
2631 doupdate();
2634 static const struct got_error *
2635 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2636 struct got_object_id *id2, struct tog_view *log_view,
2637 struct got_reflist_head *refs, struct got_repository *repo)
2639 const struct got_error *err;
2641 if (id1 != NULL && id2 != NULL) {
2642 int type1, type2;
2643 err = got_object_get_type(&type1, repo, id1);
2644 if (err)
2645 return err;
2646 err = got_object_get_type(&type2, repo, id2);
2647 if (err)
2648 return err;
2650 if (type1 != type2)
2651 return got_error(GOT_ERR_OBJ_TYPE);
2654 if (id1) {
2655 view->state.diff.id1 = got_object_id_dup(id1);
2656 if (view->state.diff.id1 == NULL)
2657 return got_error_from_errno("got_object_id_dup");
2658 } else
2659 view->state.diff.id1 = NULL;
2661 view->state.diff.id2 = got_object_id_dup(id2);
2662 if (view->state.diff.id2 == NULL) {
2663 free(view->state.diff.id1);
2664 view->state.diff.id1 = NULL;
2665 return got_error_from_errno("got_object_id_dup");
2667 view->state.diff.f = NULL;
2668 view->state.diff.first_displayed_line = 1;
2669 view->state.diff.last_displayed_line = view->nlines;
2670 view->state.diff.diff_context = 3;
2671 view->state.diff.log_view = log_view;
2672 view->state.diff.repo = repo;
2673 view->state.diff.refs = refs;
2675 if (log_view && view_is_splitscreen(view))
2676 show_log_view(log_view); /* draw vborder */
2677 diff_view_indicate_progress(view);
2679 err = create_diff(&view->state.diff);
2680 if (err) {
2681 free(view->state.diff.id1);
2682 view->state.diff.id1 = NULL;
2683 free(view->state.diff.id2);
2684 view->state.diff.id2 = NULL;
2685 return err;
2688 view->show = show_diff_view;
2689 view->input = input_diff_view;
2690 view->close = close_diff_view;
2692 return NULL;
2695 static const struct got_error *
2696 close_diff_view(struct tog_view *view)
2698 const struct got_error *err = NULL;
2700 free(view->state.diff.id1);
2701 view->state.diff.id1 = NULL;
2702 free(view->state.diff.id2);
2703 view->state.diff.id2 = NULL;
2704 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2705 err = got_error_from_errno("fclose");
2706 return err;
2709 static const struct got_error *
2710 show_diff_view(struct tog_view *view)
2712 const struct got_error *err;
2713 struct tog_diff_view_state *s = &view->state.diff;
2714 char *id_str1 = NULL, *id_str2, *header;
2716 if (s->id1) {
2717 err = got_object_id_str(&id_str1, s->id1);
2718 if (err)
2719 return err;
2721 err = got_object_id_str(&id_str2, s->id2);
2722 if (err)
2723 return err;
2725 if (asprintf(&header, "diff %s %s",
2726 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2727 err = got_error_from_errno("asprintf");
2728 free(id_str1);
2729 free(id_str2);
2730 return err;
2732 free(id_str1);
2733 free(id_str2);
2735 return draw_file(view, s->f, &s->first_displayed_line,
2736 &s->last_displayed_line, &s->eof, view->nlines,
2737 header);
2740 static const struct got_error *
2741 set_selected_commit(struct tog_diff_view_state *s,
2742 struct commit_queue_entry *entry)
2744 const struct got_error *err;
2745 const struct got_object_id_queue *parent_ids;
2746 struct got_commit_object *selected_commit;
2747 struct got_object_qid *pid;
2749 free(s->id2);
2750 s->id2 = got_object_id_dup(entry->id);
2751 if (s->id2 == NULL)
2752 return got_error_from_errno("got_object_id_dup");
2754 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2755 if (err)
2756 return err;
2757 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2758 free(s->id1);
2759 pid = SIMPLEQ_FIRST(parent_ids);
2760 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2761 got_object_commit_close(selected_commit);
2762 return NULL;
2765 static const struct got_error *
2766 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2767 struct tog_view **focus_view, struct tog_view *view, int ch)
2769 const struct got_error *err = NULL;
2770 struct tog_diff_view_state *s = &view->state.diff;
2771 struct tog_log_view_state *ls;
2772 struct commit_queue_entry *entry;
2773 int i;
2775 switch (ch) {
2776 case 'k':
2777 case KEY_UP:
2778 if (s->first_displayed_line > 1)
2779 s->first_displayed_line--;
2780 break;
2781 case KEY_PPAGE:
2782 case CTRL('b'):
2783 if (s->first_displayed_line == 1)
2784 break;
2785 i = 0;
2786 while (i++ < view->nlines - 1 &&
2787 s->first_displayed_line > 1)
2788 s->first_displayed_line--;
2789 break;
2790 case 'j':
2791 case KEY_DOWN:
2792 if (!s->eof)
2793 s->first_displayed_line++;
2794 break;
2795 case KEY_NPAGE:
2796 case CTRL('f'):
2797 case ' ':
2798 if (s->eof)
2799 break;
2800 i = 0;
2801 while (!s->eof && i++ < view->nlines - 1) {
2802 char *line;
2803 line = parse_next_line(s->f, NULL);
2804 s->first_displayed_line++;
2805 if (line == NULL)
2806 break;
2808 break;
2809 case '[':
2810 if (s->diff_context > 0) {
2811 s->diff_context--;
2812 diff_view_indicate_progress(view);
2813 err = create_diff(s);
2815 break;
2816 case ']':
2817 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2818 s->diff_context++;
2819 diff_view_indicate_progress(view);
2820 err = create_diff(s);
2822 break;
2823 case '<':
2824 case ',':
2825 if (s->log_view == NULL)
2826 break;
2827 ls = &s->log_view->state.log;
2828 entry = TAILQ_PREV(ls->selected_entry,
2829 commit_queue_head, entry);
2830 if (entry == NULL)
2831 break;
2833 err = input_log_view(NULL, NULL, NULL, s->log_view,
2834 KEY_UP);
2835 if (err)
2836 break;
2838 err = set_selected_commit(s, entry);
2839 if (err)
2840 break;
2842 s->first_displayed_line = 1;
2843 s->last_displayed_line = view->nlines;
2845 diff_view_indicate_progress(view);
2846 err = create_diff(s);
2847 break;
2848 case '>':
2849 case '.':
2850 if (s->log_view == NULL)
2851 break;
2852 ls = &s->log_view->state.log;
2854 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2855 ls->thread_args.commits_needed++;
2857 /* Display "loading..." in log view. */
2858 show_log_view(s->log_view);
2859 update_panels();
2860 doupdate();
2862 err = trigger_log_thread(1 /* load_all */,
2863 &ls->thread_args.commits_needed,
2864 &ls->thread_args.log_complete,
2865 &ls->thread_args.need_commits);
2866 if (err)
2867 break;
2869 err = input_log_view(NULL, NULL, NULL, s->log_view,
2870 KEY_DOWN);
2871 if (err)
2872 break;
2874 entry = TAILQ_NEXT(ls->selected_entry, entry);
2875 if (entry == NULL)
2876 break;
2878 err = set_selected_commit(s, entry);
2879 if (err)
2880 break;
2882 s->first_displayed_line = 1;
2883 s->last_displayed_line = view->nlines;
2885 diff_view_indicate_progress(view);
2886 err = create_diff(s);
2887 break;
2888 default:
2889 break;
2892 return err;
2895 static const struct got_error *
2896 cmd_diff(int argc, char *argv[])
2898 const struct got_error *error = NULL;
2899 struct got_repository *repo = NULL;
2900 struct got_reflist_head refs;
2901 struct got_object_id *id1 = NULL, *id2 = NULL;
2902 char *repo_path = NULL;
2903 char *id_str1 = NULL, *id_str2 = NULL;
2904 int ch;
2905 struct tog_view *view;
2907 SIMPLEQ_INIT(&refs);
2909 #ifndef PROFILE
2910 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2911 NULL) == -1)
2912 err(1, "pledge");
2913 #endif
2915 while ((ch = getopt(argc, argv, "")) != -1) {
2916 switch (ch) {
2917 default:
2918 usage_diff();
2919 /* NOTREACHED */
2923 argc -= optind;
2924 argv += optind;
2926 if (argc == 0) {
2927 usage_diff(); /* TODO show local worktree changes */
2928 } else if (argc == 2) {
2929 repo_path = getcwd(NULL, 0);
2930 if (repo_path == NULL)
2931 return got_error_from_errno("getcwd");
2932 id_str1 = argv[0];
2933 id_str2 = argv[1];
2934 } else if (argc == 3) {
2935 repo_path = realpath(argv[0], NULL);
2936 if (repo_path == NULL)
2937 return got_error_from_errno2("realpath", argv[0]);
2938 id_str1 = argv[1];
2939 id_str2 = argv[2];
2940 } else
2941 usage_diff();
2943 init_curses();
2945 error = got_repo_open(&repo, repo_path, NULL);
2946 if (error)
2947 goto done;
2949 error = apply_unveil(got_repo_get_path(repo), NULL);
2950 if (error)
2951 goto done;
2953 error = got_repo_match_object_id_prefix(&id1, id_str1,
2954 GOT_OBJ_TYPE_ANY, repo);
2955 if (error)
2956 goto done;
2958 error = got_repo_match_object_id_prefix(&id2, id_str2,
2959 GOT_OBJ_TYPE_ANY, repo);
2960 if (error)
2961 goto done;
2963 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2964 if (error)
2965 goto done;
2967 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2968 if (view == NULL) {
2969 error = got_error_from_errno("view_open");
2970 goto done;
2972 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2973 if (error)
2974 goto done;
2975 error = view_loop(view);
2976 done:
2977 free(repo_path);
2978 if (repo)
2979 got_repo_close(repo);
2980 got_ref_list_free(&refs);
2981 return error;
2984 __dead static void
2985 usage_blame(void)
2987 endwin();
2988 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2989 getprogname());
2990 exit(1);
2993 struct tog_blame_line {
2994 int annotated;
2995 struct got_object_id *id;
2998 static const struct got_error *
2999 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3000 const char *path, struct tog_blame_line *lines, int nlines,
3001 int blame_complete, int selected_line, int *first_displayed_line,
3002 int *last_displayed_line, int *eof, int max_lines)
3004 const struct got_error *err;
3005 int lineno = 0, nprinted = 0;
3006 char *line;
3007 size_t len;
3008 wchar_t *wline;
3009 int width;
3010 struct tog_blame_line *blame_line;
3011 struct got_object_id *prev_id = NULL;
3012 char *id_str;
3014 err = got_object_id_str(&id_str, id);
3015 if (err)
3016 return err;
3018 rewind(f);
3019 werase(view->window);
3021 if (asprintf(&line, "commit %s", id_str) == -1) {
3022 err = got_error_from_errno("asprintf");
3023 free(id_str);
3024 return err;
3027 err = format_line(&wline, &width, line, view->ncols, 0);
3028 free(line);
3029 line = NULL;
3030 if (err)
3031 return err;
3032 if (view_needs_focus_indication(view))
3033 wstandout(view->window);
3034 waddwstr(view->window, wline);
3035 if (view_needs_focus_indication(view))
3036 wstandend(view->window);
3037 free(wline);
3038 wline = NULL;
3039 if (width < view->ncols - 1)
3040 waddch(view->window, '\n');
3042 if (asprintf(&line, "[%d/%d] %s%s",
3043 *first_displayed_line - 1 + selected_line, nlines,
3044 blame_complete ? "" : "annotating... ", path) == -1) {
3045 free(id_str);
3046 return got_error_from_errno("asprintf");
3048 free(id_str);
3049 err = format_line(&wline, &width, line, view->ncols, 0);
3050 free(line);
3051 line = NULL;
3052 if (err)
3053 return err;
3054 waddwstr(view->window, wline);
3055 free(wline);
3056 wline = NULL;
3057 if (width < view->ncols - 1)
3058 waddch(view->window, '\n');
3060 *eof = 0;
3061 while (nprinted < max_lines - 2) {
3062 line = parse_next_line(f, &len);
3063 if (line == NULL) {
3064 *eof = 1;
3065 break;
3067 if (++lineno < *first_displayed_line) {
3068 free(line);
3069 continue;
3072 if (view->ncols <= 9) {
3073 width = 9;
3074 wline = wcsdup(L"");
3075 if (wline == NULL)
3076 err = got_error_from_errno("wcsdup");
3077 } else {
3078 err = format_line(&wline, &width, line,
3079 view->ncols - 9, 9);
3080 width += 9;
3082 if (err) {
3083 free(line);
3084 return err;
3087 if (view->focussed && nprinted == selected_line - 1)
3088 wstandout(view->window);
3090 if (nlines > 0) {
3091 blame_line = &lines[lineno - 1];
3092 if (blame_line->annotated && prev_id &&
3093 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3094 !(view->focussed &&
3095 nprinted == selected_line - 1)) {
3096 waddstr(view->window, " ");
3097 } else if (blame_line->annotated) {
3098 char *id_str;
3099 err = got_object_id_str(&id_str, blame_line->id);
3100 if (err) {
3101 free(line);
3102 free(wline);
3103 return err;
3105 wprintw(view->window, "%.8s", id_str);
3106 free(id_str);
3107 prev_id = blame_line->id;
3108 } else {
3109 waddstr(view->window, "........");
3110 prev_id = NULL;
3112 } else {
3113 waddstr(view->window, "........");
3114 prev_id = NULL;
3117 if (view->focussed && nprinted == selected_line - 1)
3118 wstandend(view->window);
3119 waddstr(view->window, " ");
3121 waddwstr(view->window, wline);
3122 if (width <= view->ncols - 1)
3123 waddch(view->window, '\n');
3124 if (++nprinted == 1)
3125 *first_displayed_line = lineno;
3126 free(line);
3127 free(wline);
3128 wline = NULL;
3130 *last_displayed_line = lineno;
3132 view_vborder(view);
3134 return NULL;
3137 static const struct got_error *
3138 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3140 const struct got_error *err = NULL;
3141 struct tog_blame_cb_args *a = arg;
3142 struct tog_blame_line *line;
3143 int errcode;
3145 if (nlines != a->nlines ||
3146 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3147 return got_error(GOT_ERR_RANGE);
3149 errcode = pthread_mutex_lock(&tog_mutex);
3150 if (errcode)
3151 return got_error_set_errno(errcode, "pthread_mutex_lock");
3153 if (*a->quit) { /* user has quit the blame view */
3154 err = got_error(GOT_ERR_ITER_COMPLETED);
3155 goto done;
3158 if (lineno == -1)
3159 goto done; /* no change in this commit */
3161 line = &a->lines[lineno - 1];
3162 if (line->annotated)
3163 goto done;
3165 line->id = got_object_id_dup(id);
3166 if (line->id == NULL) {
3167 err = got_error_from_errno("got_object_id_dup");
3168 goto done;
3170 line->annotated = 1;
3171 done:
3172 errcode = pthread_mutex_unlock(&tog_mutex);
3173 if (errcode)
3174 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3175 return err;
3178 static void *
3179 blame_thread(void *arg)
3181 const struct got_error *err;
3182 struct tog_blame_thread_args *ta = arg;
3183 struct tog_blame_cb_args *a = ta->cb_args;
3184 int errcode;
3186 err = got_blame(ta->path, a->commit_id, ta->repo,
3187 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3188 if (err && err->code == GOT_ERR_CANCELLED)
3189 err = NULL;
3191 errcode = pthread_mutex_lock(&tog_mutex);
3192 if (errcode)
3193 return (void *)got_error_set_errno(errcode,
3194 "pthread_mutex_lock");
3196 got_repo_close(ta->repo);
3197 ta->repo = NULL;
3198 *ta->complete = 1;
3200 errcode = pthread_mutex_unlock(&tog_mutex);
3201 if (errcode && err == NULL)
3202 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3204 return (void *)err;
3207 static struct got_object_id *
3208 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3209 int first_displayed_line, int selected_line)
3211 struct tog_blame_line *line;
3213 if (nlines <= 0)
3214 return NULL;
3216 line = &lines[first_displayed_line - 1 + selected_line - 1];
3217 if (!line->annotated)
3218 return NULL;
3220 return line->id;
3223 static const struct got_error *
3224 stop_blame(struct tog_blame *blame)
3226 const struct got_error *err = NULL;
3227 int i;
3229 if (blame->thread) {
3230 int errcode;
3231 errcode = pthread_mutex_unlock(&tog_mutex);
3232 if (errcode)
3233 return got_error_set_errno(errcode,
3234 "pthread_mutex_unlock");
3235 errcode = pthread_join(blame->thread, (void **)&err);
3236 if (errcode)
3237 return got_error_set_errno(errcode, "pthread_join");
3238 errcode = pthread_mutex_lock(&tog_mutex);
3239 if (errcode)
3240 return got_error_set_errno(errcode,
3241 "pthread_mutex_lock");
3242 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3243 err = NULL;
3244 blame->thread = NULL;
3246 if (blame->thread_args.repo) {
3247 got_repo_close(blame->thread_args.repo);
3248 blame->thread_args.repo = NULL;
3250 if (blame->f) {
3251 if (fclose(blame->f) != 0 && err == NULL)
3252 err = got_error_from_errno("fclose");
3253 blame->f = NULL;
3255 if (blame->lines) {
3256 for (i = 0; i < blame->nlines; i++)
3257 free(blame->lines[i].id);
3258 free(blame->lines);
3259 blame->lines = NULL;
3261 free(blame->cb_args.commit_id);
3262 blame->cb_args.commit_id = NULL;
3264 return err;
3267 static const struct got_error *
3268 cancel_blame_view(void *arg)
3270 const struct got_error *err = NULL;
3271 int *done = arg;
3272 int errcode;
3274 errcode = pthread_mutex_lock(&tog_mutex);
3275 if (errcode)
3276 return got_error_set_errno(errcode,
3277 "pthread_mutex_unlock");
3279 if (*done)
3280 err = got_error(GOT_ERR_CANCELLED);
3282 errcode = pthread_mutex_unlock(&tog_mutex);
3283 if (errcode)
3284 return got_error_set_errno(errcode,
3285 "pthread_mutex_lock");
3287 return err;
3290 static const struct got_error *
3291 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3292 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3293 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3294 struct got_repository *repo)
3296 const struct got_error *err = NULL;
3297 struct got_blob_object *blob = NULL;
3298 struct got_repository *thread_repo = NULL;
3299 struct got_object_id *obj_id = NULL;
3300 int obj_type;
3302 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3303 if (err)
3304 return err;
3305 if (obj_id == NULL)
3306 return got_error(GOT_ERR_NO_OBJ);
3308 err = got_object_get_type(&obj_type, repo, obj_id);
3309 if (err)
3310 goto done;
3312 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3313 err = got_error(GOT_ERR_OBJ_TYPE);
3314 goto done;
3317 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3318 if (err)
3319 goto done;
3320 blame->f = got_opentemp();
3321 if (blame->f == NULL) {
3322 err = got_error_from_errno("got_opentemp");
3323 goto done;
3325 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3326 &blame->line_offsets, blame->f, blob);
3327 if (err || blame->nlines == 0)
3328 goto done;
3330 /* Don't include \n at EOF in the blame line count. */
3331 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3332 blame->nlines--;
3334 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3335 if (blame->lines == NULL) {
3336 err = got_error_from_errno("calloc");
3337 goto done;
3340 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3341 if (err)
3342 goto done;
3344 blame->cb_args.view = view;
3345 blame->cb_args.lines = blame->lines;
3346 blame->cb_args.nlines = blame->nlines;
3347 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3348 if (blame->cb_args.commit_id == NULL) {
3349 err = got_error_from_errno("got_object_id_dup");
3350 goto done;
3352 blame->cb_args.quit = done;
3354 blame->thread_args.path = path;
3355 blame->thread_args.repo = thread_repo;
3356 blame->thread_args.cb_args = &blame->cb_args;
3357 blame->thread_args.complete = blame_complete;
3358 blame->thread_args.cancel_cb = cancel_blame_view;
3359 blame->thread_args.cancel_arg = done;
3360 *blame_complete = 0;
3362 done:
3363 if (blob)
3364 got_object_blob_close(blob);
3365 free(obj_id);
3366 if (err)
3367 stop_blame(blame);
3368 return err;
3371 static const struct got_error *
3372 open_blame_view(struct tog_view *view, char *path,
3373 struct got_object_id *commit_id, struct got_reflist_head *refs,
3374 struct got_repository *repo)
3376 const struct got_error *err = NULL;
3377 struct tog_blame_view_state *s = &view->state.blame;
3379 SIMPLEQ_INIT(&s->blamed_commits);
3381 s->path = strdup(path);
3382 if (s->path == NULL)
3383 return got_error_from_errno("strdup");
3385 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3386 if (err) {
3387 free(s->path);
3388 return err;
3391 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3392 s->first_displayed_line = 1;
3393 s->last_displayed_line = view->nlines;
3394 s->selected_line = 1;
3395 s->blame_complete = 0;
3396 s->repo = repo;
3397 s->refs = refs;
3398 s->commit_id = commit_id;
3399 memset(&s->blame, 0, sizeof(s->blame));
3401 view->show = show_blame_view;
3402 view->input = input_blame_view;
3403 view->close = close_blame_view;
3404 view->search_start = search_start_blame_view;
3405 view->search_next = search_next_blame_view;
3407 return run_blame(&s->blame, view, &s->blame_complete,
3408 &s->first_displayed_line, &s->last_displayed_line,
3409 &s->selected_line, &s->done, &s->eof, s->path,
3410 s->blamed_commit->id, s->repo);
3413 static const struct got_error *
3414 close_blame_view(struct tog_view *view)
3416 const struct got_error *err = NULL;
3417 struct tog_blame_view_state *s = &view->state.blame;
3419 if (s->blame.thread)
3420 err = stop_blame(&s->blame);
3422 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3423 struct got_object_qid *blamed_commit;
3424 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3425 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3426 got_object_qid_free(blamed_commit);
3429 free(s->path);
3431 return err;
3434 static const struct got_error *
3435 search_start_blame_view(struct tog_view *view)
3437 struct tog_blame_view_state *s = &view->state.blame;
3439 s->matched_line = 0;
3440 return NULL;
3443 static int
3444 match_line(const char *line, regex_t *regex)
3446 regmatch_t regmatch;
3448 return regexec(regex, line, 1, &regmatch, 0) == 0;
3452 static const struct got_error *
3453 search_next_blame_view(struct tog_view *view)
3455 struct tog_blame_view_state *s = &view->state.blame;
3456 int lineno;
3458 if (!view->searching) {
3459 view->search_next_done = 1;
3460 return NULL;
3463 if (s->matched_line) {
3464 if (view->searching == TOG_SEARCH_FORWARD)
3465 lineno = s->matched_line + 1;
3466 else
3467 lineno = s->matched_line - 1;
3468 } else {
3469 if (view->searching == TOG_SEARCH_FORWARD)
3470 lineno = 1;
3471 else
3472 lineno = s->blame.nlines;
3475 while (1) {
3476 char *line = NULL;
3477 off_t offset;
3478 size_t len;
3480 if (lineno <= 0 || lineno > s->blame.nlines) {
3481 if (s->matched_line == 0) {
3482 view->search_next_done = 1;
3483 free(line);
3484 break;
3487 if (view->searching == TOG_SEARCH_FORWARD)
3488 lineno = 1;
3489 else
3490 lineno = s->blame.nlines;
3493 offset = s->blame.line_offsets[lineno - 1];
3494 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3495 free(line);
3496 return got_error_from_errno("fseeko");
3498 free(line);
3499 line = parse_next_line(s->blame.f, &len);
3500 if (line && match_line(line, &view->regex)) {
3501 view->search_next_done = 1;
3502 s->matched_line = lineno;
3503 free(line);
3504 break;
3506 free(line);
3507 if (view->searching == TOG_SEARCH_FORWARD)
3508 lineno++;
3509 else
3510 lineno--;
3513 if (s->matched_line) {
3514 s->first_displayed_line = s->matched_line;
3515 s->selected_line = 1;
3518 return NULL;
3521 static const struct got_error *
3522 show_blame_view(struct tog_view *view)
3524 const struct got_error *err = NULL;
3525 struct tog_blame_view_state *s = &view->state.blame;
3526 int errcode;
3528 if (s->blame.thread == NULL) {
3529 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3530 &s->blame.thread_args);
3531 if (errcode)
3532 return got_error_set_errno(errcode, "pthread_create");
3534 halfdelay(1); /* fast refresh while annotating */
3537 if (s->blame_complete)
3538 halfdelay(10); /* disable fast refresh */
3540 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3541 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3542 s->selected_line, &s->first_displayed_line,
3543 &s->last_displayed_line, &s->eof, view->nlines);
3545 view_vborder(view);
3546 return err;
3549 static const struct got_error *
3550 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3551 struct tog_view **focus_view, struct tog_view *view, int ch)
3553 const struct got_error *err = NULL, *thread_err = NULL;
3554 struct tog_view *diff_view;
3555 struct tog_blame_view_state *s = &view->state.blame;
3556 int begin_x = 0;
3558 switch (ch) {
3559 case 'q':
3560 s->done = 1;
3561 break;
3562 case 'k':
3563 case KEY_UP:
3564 if (s->selected_line > 1)
3565 s->selected_line--;
3566 else if (s->selected_line == 1 &&
3567 s->first_displayed_line > 1)
3568 s->first_displayed_line--;
3569 break;
3570 case KEY_PPAGE:
3571 if (s->first_displayed_line == 1) {
3572 s->selected_line = 1;
3573 break;
3575 if (s->first_displayed_line > view->nlines - 2)
3576 s->first_displayed_line -=
3577 (view->nlines - 2);
3578 else
3579 s->first_displayed_line = 1;
3580 break;
3581 case 'j':
3582 case KEY_DOWN:
3583 if (s->selected_line < view->nlines - 2 &&
3584 s->first_displayed_line +
3585 s->selected_line <= s->blame.nlines)
3586 s->selected_line++;
3587 else if (s->last_displayed_line <
3588 s->blame.nlines)
3589 s->first_displayed_line++;
3590 break;
3591 case 'b':
3592 case 'p': {
3593 struct got_object_id *id = NULL;
3594 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3595 s->first_displayed_line, s->selected_line);
3596 if (id == NULL)
3597 break;
3598 if (ch == 'p') {
3599 struct got_commit_object *commit;
3600 struct got_object_qid *pid;
3601 struct got_object_id *blob_id = NULL;
3602 int obj_type;
3603 err = got_object_open_as_commit(&commit,
3604 s->repo, id);
3605 if (err)
3606 break;
3607 pid = SIMPLEQ_FIRST(
3608 got_object_commit_get_parent_ids(commit));
3609 if (pid == NULL) {
3610 got_object_commit_close(commit);
3611 break;
3613 /* Check if path history ends here. */
3614 err = got_object_id_by_path(&blob_id, s->repo,
3615 pid->id, s->path);
3616 if (err) {
3617 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3618 err = NULL;
3619 got_object_commit_close(commit);
3620 break;
3622 err = got_object_get_type(&obj_type, s->repo,
3623 blob_id);
3624 free(blob_id);
3625 /* Can't blame non-blob type objects. */
3626 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3627 got_object_commit_close(commit);
3628 break;
3630 err = got_object_qid_alloc(&s->blamed_commit,
3631 pid->id);
3632 got_object_commit_close(commit);
3633 } else {
3634 if (got_object_id_cmp(id,
3635 s->blamed_commit->id) == 0)
3636 break;
3637 err = got_object_qid_alloc(&s->blamed_commit,
3638 id);
3640 if (err)
3641 break;
3642 s->done = 1;
3643 thread_err = stop_blame(&s->blame);
3644 s->done = 0;
3645 if (thread_err)
3646 break;
3647 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3648 s->blamed_commit, entry);
3649 err = run_blame(&s->blame, view, &s->blame_complete,
3650 &s->first_displayed_line, &s->last_displayed_line,
3651 &s->selected_line, &s->done, &s->eof,
3652 s->path, s->blamed_commit->id, s->repo);
3653 if (err)
3654 break;
3655 break;
3657 case 'B': {
3658 struct got_object_qid *first;
3659 first = SIMPLEQ_FIRST(&s->blamed_commits);
3660 if (!got_object_id_cmp(first->id, s->commit_id))
3661 break;
3662 s->done = 1;
3663 thread_err = stop_blame(&s->blame);
3664 s->done = 0;
3665 if (thread_err)
3666 break;
3667 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3668 got_object_qid_free(s->blamed_commit);
3669 s->blamed_commit =
3670 SIMPLEQ_FIRST(&s->blamed_commits);
3671 err = run_blame(&s->blame, view, &s->blame_complete,
3672 &s->first_displayed_line, &s->last_displayed_line,
3673 &s->selected_line, &s->done, &s->eof, s->path,
3674 s->blamed_commit->id, s->repo);
3675 if (err)
3676 break;
3677 break;
3679 case KEY_ENTER:
3680 case '\r': {
3681 struct got_object_id *id = NULL;
3682 struct got_object_qid *pid;
3683 struct got_commit_object *commit = NULL;
3684 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3685 s->first_displayed_line, s->selected_line);
3686 if (id == NULL)
3687 break;
3688 err = got_object_open_as_commit(&commit, s->repo, id);
3689 if (err)
3690 break;
3691 pid = SIMPLEQ_FIRST(
3692 got_object_commit_get_parent_ids(commit));
3693 if (view_is_parent_view(view))
3694 begin_x = view_split_begin_x(view->begin_x);
3695 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3696 if (diff_view == NULL) {
3697 got_object_commit_close(commit);
3698 err = got_error_from_errno("view_open");
3699 break;
3701 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3702 id, NULL, s->refs, s->repo);
3703 got_object_commit_close(commit);
3704 if (err) {
3705 view_close(diff_view);
3706 break;
3708 if (view_is_parent_view(view)) {
3709 err = view_close_child(view);
3710 if (err)
3711 break;
3712 err = view_set_child(view, diff_view);
3713 if (err) {
3714 view_close(diff_view);
3715 break;
3717 *focus_view = diff_view;
3718 view->child_focussed = 1;
3719 } else
3720 *new_view = diff_view;
3721 if (err)
3722 break;
3723 break;
3725 case KEY_NPAGE:
3726 case ' ':
3727 if (s->last_displayed_line >= s->blame.nlines &&
3728 s->selected_line >= MIN(s->blame.nlines,
3729 view->nlines - 2)) {
3730 break;
3732 if (s->last_displayed_line >= s->blame.nlines &&
3733 s->selected_line < view->nlines - 2) {
3734 s->selected_line = MIN(s->blame.nlines,
3735 view->nlines - 2);
3736 break;
3738 if (s->last_displayed_line + view->nlines - 2
3739 <= s->blame.nlines)
3740 s->first_displayed_line +=
3741 view->nlines - 2;
3742 else
3743 s->first_displayed_line =
3744 s->blame.nlines -
3745 (view->nlines - 3);
3746 break;
3747 case KEY_RESIZE:
3748 if (s->selected_line > view->nlines - 2) {
3749 s->selected_line = MIN(s->blame.nlines,
3750 view->nlines - 2);
3752 break;
3753 default:
3754 break;
3756 return thread_err ? thread_err : err;
3759 static const struct got_error *
3760 cmd_blame(int argc, char *argv[])
3762 const struct got_error *error;
3763 struct got_repository *repo = NULL;
3764 struct got_reflist_head refs;
3765 struct got_worktree *worktree = NULL;
3766 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3767 struct got_object_id *commit_id = NULL;
3768 char *commit_id_str = NULL;
3769 int ch;
3770 struct tog_view *view;
3772 SIMPLEQ_INIT(&refs);
3774 #ifndef PROFILE
3775 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3776 NULL) == -1)
3777 err(1, "pledge");
3778 #endif
3780 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3781 switch (ch) {
3782 case 'c':
3783 commit_id_str = optarg;
3784 break;
3785 case 'r':
3786 repo_path = realpath(optarg, NULL);
3787 if (repo_path == NULL)
3788 err(1, "-r option");
3789 break;
3790 default:
3791 usage_blame();
3792 /* NOTREACHED */
3796 argc -= optind;
3797 argv += optind;
3799 if (argc == 1)
3800 path = argv[0];
3801 else
3802 usage_blame();
3804 cwd = getcwd(NULL, 0);
3805 if (cwd == NULL) {
3806 error = got_error_from_errno("getcwd");
3807 goto done;
3809 if (repo_path == NULL) {
3810 error = got_worktree_open(&worktree, cwd);
3811 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3812 goto done;
3813 else
3814 error = NULL;
3815 if (worktree) {
3816 repo_path =
3817 strdup(got_worktree_get_repo_path(worktree));
3818 if (repo_path == NULL)
3819 error = got_error_from_errno("strdup");
3820 if (error)
3821 goto done;
3822 } else {
3823 repo_path = strdup(cwd);
3824 if (repo_path == NULL) {
3825 error = got_error_from_errno("strdup");
3826 goto done;
3831 init_curses();
3833 error = got_repo_open(&repo, repo_path, NULL);
3834 if (error != NULL)
3835 goto done;
3837 error = apply_unveil(got_repo_get_path(repo), NULL);
3838 if (error)
3839 goto done;
3841 if (worktree) {
3842 const char *prefix = got_worktree_get_path_prefix(worktree);
3843 char *p, *worktree_subdir = cwd +
3844 strlen(got_worktree_get_root_path(worktree));
3845 if (asprintf(&p, "%s%s%s%s%s",
3846 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3847 worktree_subdir, worktree_subdir[0] ? "/" : "",
3848 path) == -1) {
3849 error = got_error_from_errno("asprintf");
3850 goto done;
3852 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3853 free(p);
3854 } else {
3855 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3857 if (error)
3858 goto done;
3860 if (commit_id_str == NULL) {
3861 struct got_reference *head_ref;
3862 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3863 if (error != NULL)
3864 goto done;
3865 error = got_ref_resolve(&commit_id, repo, head_ref);
3866 got_ref_close(head_ref);
3867 } else {
3868 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3869 if (error) {
3870 if (error->code != GOT_ERR_NOT_REF)
3871 goto done;
3872 error = got_repo_match_object_id_prefix(&commit_id,
3873 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3876 if (error != NULL)
3877 goto done;
3879 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3880 if (error)
3881 goto done;
3883 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3884 if (view == NULL) {
3885 error = got_error_from_errno("view_open");
3886 goto done;
3888 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3889 if (error)
3890 goto done;
3891 if (worktree) {
3892 /* Release work tree lock. */
3893 got_worktree_close(worktree);
3894 worktree = NULL;
3896 error = view_loop(view);
3897 done:
3898 free(repo_path);
3899 free(cwd);
3900 free(commit_id);
3901 if (worktree)
3902 got_worktree_close(worktree);
3903 if (repo)
3904 got_repo_close(repo);
3905 got_ref_list_free(&refs);
3906 return error;
3909 static const struct got_error *
3910 draw_tree_entries(struct tog_view *view,
3911 struct got_tree_entry **first_displayed_entry,
3912 struct got_tree_entry **last_displayed_entry,
3913 struct got_tree_entry **selected_entry, int *ndisplayed,
3914 const char *label, int show_ids, const char *parent_path,
3915 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3917 const struct got_error *err = NULL;
3918 struct got_tree_entry *te;
3919 wchar_t *wline;
3920 int width, n;
3922 *ndisplayed = 0;
3924 werase(view->window);
3926 if (limit == 0)
3927 return NULL;
3929 err = format_line(&wline, &width, label, view->ncols, 0);
3930 if (err)
3931 return err;
3932 if (view_needs_focus_indication(view))
3933 wstandout(view->window);
3934 waddwstr(view->window, wline);
3935 if (view_needs_focus_indication(view))
3936 wstandend(view->window);
3937 free(wline);
3938 wline = NULL;
3939 if (width < view->ncols - 1)
3940 waddch(view->window, '\n');
3941 if (--limit <= 0)
3942 return NULL;
3943 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3944 if (err)
3945 return err;
3946 waddwstr(view->window, wline);
3947 free(wline);
3948 wline = NULL;
3949 if (width < view->ncols - 1)
3950 waddch(view->window, '\n');
3951 if (--limit <= 0)
3952 return NULL;
3953 waddch(view->window, '\n');
3954 if (--limit <= 0)
3955 return NULL;
3957 te = SIMPLEQ_FIRST(&entries->head);
3958 if (*first_displayed_entry == NULL) {
3959 if (selected == 0) {
3960 if (view->focussed)
3961 wstandout(view->window);
3962 *selected_entry = NULL;
3964 waddstr(view->window, " ..\n"); /* parent directory */
3965 if (selected == 0 && view->focussed)
3966 wstandend(view->window);
3967 (*ndisplayed)++;
3968 if (--limit <= 0)
3969 return NULL;
3970 n = 1;
3971 } else {
3972 n = 0;
3973 while (te != *first_displayed_entry)
3974 te = SIMPLEQ_NEXT(te, entry);
3977 while (te) {
3978 char *line = NULL, *id_str = NULL;
3979 const char *modestr = "";
3981 if (show_ids) {
3982 err = got_object_id_str(&id_str, te->id);
3983 if (err)
3984 return got_error_from_errno(
3985 "got_object_id_str");
3987 if (got_object_tree_entry_is_submodule(te))
3988 modestr = "$";
3989 else if (S_ISLNK(te->mode))
3990 modestr = "@";
3991 else if (S_ISDIR(te->mode))
3992 modestr = "/";
3993 else if (te->mode & S_IXUSR)
3994 modestr = "*";
3995 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3996 te->name, modestr) == -1) {
3997 free(id_str);
3998 return got_error_from_errno("asprintf");
4000 free(id_str);
4001 err = format_line(&wline, &width, line, view->ncols, 0);
4002 if (err) {
4003 free(line);
4004 break;
4006 if (n == selected) {
4007 if (view->focussed)
4008 wstandout(view->window);
4009 *selected_entry = te;
4011 waddwstr(view->window, wline);
4012 if (width < view->ncols - 1)
4013 waddch(view->window, '\n');
4014 if (n == selected && view->focussed)
4015 wstandend(view->window);
4016 free(line);
4017 free(wline);
4018 wline = NULL;
4019 n++;
4020 (*ndisplayed)++;
4021 *last_displayed_entry = te;
4022 if (--limit <= 0)
4023 break;
4024 te = SIMPLEQ_NEXT(te, entry);
4027 return err;
4030 static void
4031 tree_scroll_up(struct tog_view *view,
4032 struct got_tree_entry **first_displayed_entry, int maxscroll,
4033 const struct got_tree_entries *entries, int isroot)
4035 struct got_tree_entry *te, *prev;
4036 int i;
4038 if (*first_displayed_entry == NULL)
4039 return;
4041 te = SIMPLEQ_FIRST(&entries->head);
4042 if (*first_displayed_entry == te) {
4043 if (!isroot)
4044 *first_displayed_entry = NULL;
4045 return;
4048 /* XXX this is stupid... switch to TAILQ? */
4049 for (i = 0; i < maxscroll; i++) {
4050 while (te != *first_displayed_entry) {
4051 prev = te;
4052 te = SIMPLEQ_NEXT(te, entry);
4054 *first_displayed_entry = prev;
4055 te = SIMPLEQ_FIRST(&entries->head);
4057 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4058 *first_displayed_entry = NULL;
4061 static int
4062 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4063 struct got_tree_entry *last_displayed_entry,
4064 const struct got_tree_entries *entries)
4066 struct got_tree_entry *next, *last;
4067 int n = 0;
4069 if (*first_displayed_entry)
4070 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4071 else
4072 next = SIMPLEQ_FIRST(&entries->head);
4073 last = last_displayed_entry;
4074 while (next && last && n++ < maxscroll) {
4075 last = SIMPLEQ_NEXT(last, entry);
4076 if (last) {
4077 *first_displayed_entry = next;
4078 next = SIMPLEQ_NEXT(next, entry);
4081 return n;
4084 static const struct got_error *
4085 tree_entry_path(char **path, struct tog_parent_trees *parents,
4086 struct got_tree_entry *te)
4088 const struct got_error *err = NULL;
4089 struct tog_parent_tree *pt;
4090 size_t len = 2; /* for leading slash and NUL */
4092 TAILQ_FOREACH(pt, parents, entry)
4093 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4094 if (te)
4095 len += strlen(te->name);
4097 *path = calloc(1, len);
4098 if (path == NULL)
4099 return got_error_from_errno("calloc");
4101 (*path)[0] = '/';
4102 pt = TAILQ_LAST(parents, tog_parent_trees);
4103 while (pt) {
4104 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4105 err = got_error(GOT_ERR_NO_SPACE);
4106 goto done;
4108 if (strlcat(*path, "/", len) >= len) {
4109 err = got_error(GOT_ERR_NO_SPACE);
4110 goto done;
4112 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4114 if (te) {
4115 if (strlcat(*path, te->name, len) >= len) {
4116 err = got_error(GOT_ERR_NO_SPACE);
4117 goto done;
4120 done:
4121 if (err) {
4122 free(*path);
4123 *path = NULL;
4125 return err;
4128 static const struct got_error *
4129 blame_tree_entry(struct tog_view **new_view, int begin_x,
4130 struct got_tree_entry *te, struct tog_parent_trees *parents,
4131 struct got_object_id *commit_id, struct got_reflist_head *refs,
4132 struct got_repository *repo)
4134 const struct got_error *err = NULL;
4135 char *path;
4136 struct tog_view *blame_view;
4138 *new_view = NULL;
4140 err = tree_entry_path(&path, parents, te);
4141 if (err)
4142 return err;
4144 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4145 if (blame_view == NULL) {
4146 err = got_error_from_errno("view_open");
4147 goto done;
4150 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4151 if (err) {
4152 if (err->code == GOT_ERR_CANCELLED)
4153 err = NULL;
4154 view_close(blame_view);
4155 } else
4156 *new_view = blame_view;
4157 done:
4158 free(path);
4159 return err;
4162 static const struct got_error *
4163 log_tree_entry(struct tog_view **new_view, int begin_x,
4164 struct got_tree_entry *te, struct tog_parent_trees *parents,
4165 struct got_object_id *commit_id, struct got_reflist_head *refs,
4166 struct got_repository *repo)
4168 struct tog_view *log_view;
4169 const struct got_error *err = NULL;
4170 char *path;
4172 *new_view = NULL;
4174 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4175 if (log_view == NULL)
4176 return got_error_from_errno("view_open");
4178 err = tree_entry_path(&path, parents, te);
4179 if (err)
4180 return err;
4182 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4183 if (err)
4184 view_close(log_view);
4185 else
4186 *new_view = log_view;
4187 free(path);
4188 return err;
4191 static const struct got_error *
4192 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4193 struct got_object_id *commit_id, struct got_reflist_head *refs,
4194 struct got_repository *repo)
4196 const struct got_error *err = NULL;
4197 char *commit_id_str = NULL;
4198 struct tog_tree_view_state *s = &view->state.tree;
4200 TAILQ_INIT(&s->parents);
4202 err = got_object_id_str(&commit_id_str, commit_id);
4203 if (err != NULL)
4204 goto done;
4206 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4207 err = got_error_from_errno("asprintf");
4208 goto done;
4211 s->root = s->tree = root;
4212 s->entries = got_object_tree_get_entries(root);
4213 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4214 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4215 s->commit_id = got_object_id_dup(commit_id);
4216 if (s->commit_id == NULL) {
4217 err = got_error_from_errno("got_object_id_dup");
4218 goto done;
4220 s->refs = refs;
4221 s->repo = repo;
4223 view->show = show_tree_view;
4224 view->input = input_tree_view;
4225 view->close = close_tree_view;
4226 view->search_start = search_start_tree_view;
4227 view->search_next = search_next_tree_view;
4228 done:
4229 free(commit_id_str);
4230 if (err) {
4231 free(s->tree_label);
4232 s->tree_label = NULL;
4234 return err;
4237 static const struct got_error *
4238 close_tree_view(struct tog_view *view)
4240 struct tog_tree_view_state *s = &view->state.tree;
4242 free(s->tree_label);
4243 s->tree_label = NULL;
4244 free(s->commit_id);
4245 s->commit_id = NULL;
4246 while (!TAILQ_EMPTY(&s->parents)) {
4247 struct tog_parent_tree *parent;
4248 parent = TAILQ_FIRST(&s->parents);
4249 TAILQ_REMOVE(&s->parents, parent, entry);
4250 free(parent);
4253 if (s->tree != s->root)
4254 got_object_tree_close(s->tree);
4255 got_object_tree_close(s->root);
4257 return NULL;
4260 static const struct got_error *
4261 search_start_tree_view(struct tog_view *view)
4263 struct tog_tree_view_state *s = &view->state.tree;
4265 s->matched_entry = NULL;
4266 return NULL;
4269 static int
4270 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4272 regmatch_t regmatch;
4274 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4277 static const struct got_error *
4278 search_next_tree_view(struct tog_view *view)
4280 struct tog_tree_view_state *s = &view->state.tree;
4281 struct got_tree_entry *entry = NULL, *te;
4283 if (!view->searching) {
4284 view->search_next_done = 1;
4285 return NULL;
4288 if (s->matched_entry) {
4289 if (view->searching == TOG_SEARCH_FORWARD) {
4290 if (s->selected_entry)
4291 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4292 else
4293 entry = SIMPLEQ_FIRST(&s->entries->head);
4295 else {
4296 if (s->selected_entry == NULL) {
4297 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4298 entry = te;
4299 } else {
4300 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4301 entry = te;
4302 if (SIMPLEQ_NEXT(te, entry) ==
4303 s->selected_entry)
4304 break;
4308 } else {
4309 if (view->searching == TOG_SEARCH_FORWARD)
4310 entry = SIMPLEQ_FIRST(&s->entries->head);
4311 else {
4312 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4313 entry = te;
4317 while (1) {
4318 if (entry == NULL) {
4319 if (s->matched_entry == NULL) {
4320 view->search_next_done = 1;
4321 return NULL;
4323 if (view->searching == TOG_SEARCH_FORWARD)
4324 entry = SIMPLEQ_FIRST(&s->entries->head);
4325 else {
4326 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4327 entry = te;
4331 if (match_tree_entry(entry, &view->regex)) {
4332 view->search_next_done = 1;
4333 s->matched_entry = entry;
4334 break;
4337 if (view->searching == TOG_SEARCH_FORWARD)
4338 entry = SIMPLEQ_NEXT(entry, entry);
4339 else {
4340 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4341 entry = NULL;
4342 else {
4343 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4344 if (SIMPLEQ_NEXT(te, entry) == entry) {
4345 entry = te;
4346 break;
4353 if (s->matched_entry) {
4354 s->first_displayed_entry = s->matched_entry;
4355 s->selected = 0;
4358 return NULL;
4361 static const struct got_error *
4362 show_tree_view(struct tog_view *view)
4364 const struct got_error *err = NULL;
4365 struct tog_tree_view_state *s = &view->state.tree;
4366 char *parent_path;
4368 err = tree_entry_path(&parent_path, &s->parents, NULL);
4369 if (err)
4370 return err;
4372 err = draw_tree_entries(view, &s->first_displayed_entry,
4373 &s->last_displayed_entry, &s->selected_entry,
4374 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4375 s->entries, s->selected, view->nlines, s->tree == s->root);
4376 free(parent_path);
4378 view_vborder(view);
4379 return err;
4382 static const struct got_error *
4383 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4384 struct tog_view **focus_view, struct tog_view *view, int ch)
4386 const struct got_error *err = NULL;
4387 struct tog_tree_view_state *s = &view->state.tree;
4388 struct tog_view *log_view;
4389 int begin_x = 0, nscrolled;
4391 switch (ch) {
4392 case 'i':
4393 s->show_ids = !s->show_ids;
4394 break;
4395 case 'l':
4396 if (!s->selected_entry)
4397 break;
4398 if (view_is_parent_view(view))
4399 begin_x = view_split_begin_x(view->begin_x);
4400 err = log_tree_entry(&log_view, begin_x,
4401 s->selected_entry, &s->parents,
4402 s->commit_id, s->refs, s->repo);
4403 if (view_is_parent_view(view)) {
4404 err = view_close_child(view);
4405 if (err)
4406 return err;
4407 err = view_set_child(view, log_view);
4408 if (err) {
4409 view_close(log_view);
4410 break;
4412 *focus_view = log_view;
4413 view->child_focussed = 1;
4414 } else
4415 *new_view = log_view;
4416 break;
4417 case 'k':
4418 case KEY_UP:
4419 if (s->selected > 0) {
4420 s->selected--;
4421 if (s->selected == 0)
4422 break;
4424 if (s->selected > 0)
4425 break;
4426 tree_scroll_up(view, &s->first_displayed_entry, 1,
4427 s->entries, s->tree == s->root);
4428 break;
4429 case KEY_PPAGE:
4430 tree_scroll_up(view, &s->first_displayed_entry,
4431 MAX(0, view->nlines - 4 - s->selected), s->entries,
4432 s->tree == s->root);
4433 s->selected = 0;
4434 if (SIMPLEQ_FIRST(&s->entries->head) ==
4435 s->first_displayed_entry && s->tree != s->root)
4436 s->first_displayed_entry = NULL;
4437 break;
4438 case 'j':
4439 case KEY_DOWN:
4440 if (s->selected < s->ndisplayed - 1) {
4441 s->selected++;
4442 break;
4444 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4445 /* can't scroll any further */
4446 break;
4447 tree_scroll_down(&s->first_displayed_entry, 1,
4448 s->last_displayed_entry, s->entries);
4449 break;
4450 case KEY_NPAGE:
4451 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4452 == NULL) {
4453 /* can't scroll any further; move cursor down */
4454 if (s->selected < s->ndisplayed - 1)
4455 s->selected = s->ndisplayed - 1;
4456 break;
4458 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4459 view->nlines, s->last_displayed_entry, s->entries);
4460 if (nscrolled < view->nlines) {
4461 int ndisplayed = 0;
4462 struct got_tree_entry *te;
4463 te = s->first_displayed_entry;
4464 do {
4465 ndisplayed++;
4466 te = SIMPLEQ_NEXT(te, entry);
4467 } while (te);
4468 s->selected = ndisplayed - 1;
4470 break;
4471 case KEY_ENTER:
4472 case '\r':
4473 case KEY_BACKSPACE:
4474 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4475 struct tog_parent_tree *parent;
4476 /* user selected '..' */
4477 if (s->tree == s->root)
4478 break;
4479 parent = TAILQ_FIRST(&s->parents);
4480 TAILQ_REMOVE(&s->parents, parent,
4481 entry);
4482 got_object_tree_close(s->tree);
4483 s->tree = parent->tree;
4484 s->entries =
4485 got_object_tree_get_entries(s->tree);
4486 s->first_displayed_entry =
4487 parent->first_displayed_entry;
4488 s->selected_entry =
4489 parent->selected_entry;
4490 s->selected = parent->selected;
4491 free(parent);
4492 } else if (S_ISDIR(s->selected_entry->mode)) {
4493 struct got_tree_object *subtree;
4494 err = got_object_open_as_tree(&subtree,
4495 s->repo, s->selected_entry->id);
4496 if (err)
4497 break;
4498 err = tree_view_visit_subtree(subtree, s);
4499 if (err) {
4500 got_object_tree_close(subtree);
4501 break;
4503 } else if (S_ISREG(s->selected_entry->mode)) {
4504 struct tog_view *blame_view;
4505 int begin_x = view_is_parent_view(view) ?
4506 view_split_begin_x(view->begin_x) : 0;
4508 err = blame_tree_entry(&blame_view, begin_x,
4509 s->selected_entry, &s->parents,
4510 s->commit_id, s->refs, s->repo);
4511 if (err)
4512 break;
4513 if (view_is_parent_view(view)) {
4514 err = view_close_child(view);
4515 if (err)
4516 return err;
4517 err = view_set_child(view, blame_view);
4518 if (err) {
4519 view_close(blame_view);
4520 break;
4522 *focus_view = blame_view;
4523 view->child_focussed = 1;
4524 } else
4525 *new_view = blame_view;
4527 break;
4528 case KEY_RESIZE:
4529 if (s->selected > view->nlines)
4530 s->selected = s->ndisplayed - 1;
4531 break;
4532 default:
4533 break;
4536 return err;
4539 __dead static void
4540 usage_tree(void)
4542 endwin();
4543 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4544 getprogname());
4545 exit(1);
4548 static const struct got_error *
4549 cmd_tree(int argc, char *argv[])
4551 const struct got_error *error;
4552 struct got_repository *repo = NULL;
4553 struct got_reflist_head refs;
4554 char *repo_path = NULL;
4555 struct got_object_id *commit_id = NULL;
4556 char *commit_id_arg = NULL;
4557 struct got_commit_object *commit = NULL;
4558 struct got_tree_object *tree = NULL;
4559 int ch;
4560 struct tog_view *view;
4562 SIMPLEQ_INIT(&refs);
4564 #ifndef PROFILE
4565 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4566 NULL) == -1)
4567 err(1, "pledge");
4568 #endif
4570 while ((ch = getopt(argc, argv, "c:")) != -1) {
4571 switch (ch) {
4572 case 'c':
4573 commit_id_arg = optarg;
4574 break;
4575 default:
4576 usage_tree();
4577 /* NOTREACHED */
4581 argc -= optind;
4582 argv += optind;
4584 if (argc == 0) {
4585 struct got_worktree *worktree;
4586 char *cwd = getcwd(NULL, 0);
4587 if (cwd == NULL)
4588 return got_error_from_errno("getcwd");
4589 error = got_worktree_open(&worktree, cwd);
4590 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4591 goto done;
4592 if (worktree) {
4593 free(cwd);
4594 repo_path =
4595 strdup(got_worktree_get_repo_path(worktree));
4596 got_worktree_close(worktree);
4597 } else
4598 repo_path = cwd;
4599 if (repo_path == NULL) {
4600 error = got_error_from_errno("strdup");
4601 goto done;
4603 } else if (argc == 1) {
4604 repo_path = realpath(argv[0], NULL);
4605 if (repo_path == NULL)
4606 return got_error_from_errno2("realpath", argv[0]);
4607 } else
4608 usage_log();
4610 init_curses();
4612 error = got_repo_open(&repo, repo_path, NULL);
4613 if (error != NULL)
4614 goto done;
4616 error = apply_unveil(got_repo_get_path(repo), NULL);
4617 if (error)
4618 goto done;
4620 if (commit_id_arg == NULL)
4621 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4622 else {
4623 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4624 if (error) {
4625 if (error->code != GOT_ERR_NOT_REF)
4626 goto done;
4627 error = got_repo_match_object_id_prefix(&commit_id,
4628 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4631 if (error != NULL)
4632 goto done;
4634 error = got_object_open_as_commit(&commit, repo, commit_id);
4635 if (error != NULL)
4636 goto done;
4638 error = got_object_open_as_tree(&tree, repo,
4639 got_object_commit_get_tree_id(commit));
4640 if (error != NULL)
4641 goto done;
4643 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4644 if (error)
4645 goto done;
4647 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4648 if (view == NULL) {
4649 error = got_error_from_errno("view_open");
4650 goto done;
4652 error = open_tree_view(view, tree, commit_id, &refs, repo);
4653 if (error)
4654 goto done;
4655 error = view_loop(view);
4656 done:
4657 free(repo_path);
4658 free(commit_id);
4659 if (commit)
4660 got_object_commit_close(commit);
4661 if (tree)
4662 got_object_tree_close(tree);
4663 if (repo)
4664 got_repo_close(repo);
4665 got_ref_list_free(&refs);
4666 return error;
4669 static void
4670 list_commands(void)
4672 int i;
4674 fprintf(stderr, "commands:");
4675 for (i = 0; i < nitems(tog_commands); i++) {
4676 struct tog_cmd *cmd = &tog_commands[i];
4677 fprintf(stderr, " %s", cmd->name);
4679 fputc('\n', stderr);
4682 __dead static void
4683 usage(int hflag)
4685 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4686 getprogname());
4687 if (hflag)
4688 list_commands();
4689 exit(1);
4692 static char **
4693 make_argv(const char *arg0, const char *arg1)
4695 char **argv;
4696 int argc = (arg1 == NULL ? 1 : 2);
4698 argv = calloc(argc, sizeof(char *));
4699 if (argv == NULL)
4700 err(1, "calloc");
4701 argv[0] = strdup(arg0);
4702 if (argv[0] == NULL)
4703 err(1, "strdup");
4704 if (arg1) {
4705 argv[1] = strdup(arg1);
4706 if (argv[1] == NULL)
4707 err(1, "strdup");
4710 return argv;
4713 int
4714 main(int argc, char *argv[])
4716 const struct got_error *error = NULL;
4717 struct tog_cmd *cmd = NULL;
4718 int ch, hflag = 0, Vflag = 0;
4719 char **cmd_argv = NULL;
4721 setlocale(LC_CTYPE, "");
4723 while ((ch = getopt(argc, argv, "hV")) != -1) {
4724 switch (ch) {
4725 case 'h':
4726 hflag = 1;
4727 break;
4728 case 'V':
4729 Vflag = 1;
4730 break;
4731 default:
4732 usage(hflag);
4733 /* NOTREACHED */
4737 argc -= optind;
4738 argv += optind;
4739 optind = 0;
4740 optreset = 1;
4742 if (Vflag) {
4743 got_version_print_str();
4744 return 1;
4747 if (argc == 0) {
4748 if (hflag)
4749 usage(hflag);
4750 /* Build an argument vector which runs a default command. */
4751 cmd = &tog_commands[0];
4752 cmd_argv = make_argv(cmd->name, NULL);
4753 argc = 1;
4754 } else {
4755 int i;
4757 /* Did the user specific a command? */
4758 for (i = 0; i < nitems(tog_commands); i++) {
4759 if (strncmp(tog_commands[i].name, argv[0],
4760 strlen(argv[0])) == 0) {
4761 cmd = &tog_commands[i];
4762 break;
4766 if (cmd == NULL) {
4767 fprintf(stderr, "%s: unknown command '%s'\n",
4768 getprogname(), argv[0]);
4769 list_commands();
4770 return 1;
4774 if (hflag)
4775 cmd->cmd_usage();
4776 else
4777 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4779 endwin();
4780 free(cmd_argv);
4781 if (error && error->code != GOT_ERR_CANCELLED)
4782 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4783 return 0;