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 err = open_log_view(lv, start_id, s->refs, s->repo,
2146 s->head_ref_name, in_repo_path, 0);
2147 if (err) {
2148 free(start_id);
2149 view_close(lv);
2150 return err;;
2152 *dead_view = view;
2153 *new_view = lv;
2154 break;
2155 default:
2156 break;
2159 return err;
2162 static const struct got_error *
2163 apply_unveil(const char *repo_path, const char *worktree_path)
2165 const struct got_error *error;
2167 #ifdef PROFILE
2168 if (unveil("gmon.out", "rwc") != 0)
2169 return got_error_from_errno2("unveil", "gmon.out");
2170 #endif
2171 if (repo_path && unveil(repo_path, "r") != 0)
2172 return got_error_from_errno2("unveil", repo_path);
2174 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2175 return got_error_from_errno2("unveil", worktree_path);
2177 if (unveil("/tmp", "rwc") != 0)
2178 return got_error_from_errno2("unveil", "/tmp");
2180 error = got_privsep_unveil_exec_helpers();
2181 if (error != NULL)
2182 return error;
2184 if (unveil(NULL, NULL) != 0)
2185 return got_error_from_errno("unveil");
2187 return NULL;
2190 static void
2191 init_curses(void)
2193 initscr();
2194 cbreak();
2195 halfdelay(1); /* Do fast refresh while initial view is loading. */
2196 noecho();
2197 nonl();
2198 intrflush(stdscr, FALSE);
2199 keypad(stdscr, TRUE);
2200 curs_set(0);
2201 signal(SIGWINCH, tog_sigwinch);
2202 signal(SIGPIPE, tog_sigpipe);
2205 static const struct got_error *
2206 cmd_log(int argc, char *argv[])
2208 const struct got_error *error;
2209 struct got_repository *repo = NULL;
2210 struct got_worktree *worktree = NULL;
2211 struct got_reflist_head refs;
2212 struct got_object_id *start_id = NULL;
2213 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2214 char *start_commit = NULL, *head_ref_name = NULL;
2215 int ch;
2216 struct tog_view *view;
2218 SIMPLEQ_INIT(&refs);
2220 #ifndef PROFILE
2221 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2222 NULL) == -1)
2223 err(1, "pledge");
2224 #endif
2226 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2227 switch (ch) {
2228 case 'c':
2229 start_commit = optarg;
2230 break;
2231 case 'r':
2232 repo_path = realpath(optarg, NULL);
2233 if (repo_path == NULL)
2234 err(1, "-r option");
2235 break;
2236 default:
2237 usage_log();
2238 /* NOTREACHED */
2242 argc -= optind;
2243 argv += optind;
2245 cwd = getcwd(NULL, 0);
2246 if (cwd == NULL) {
2247 error = got_error_from_errno("getcwd");
2248 goto done;
2250 error = got_worktree_open(&worktree, cwd);
2251 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2252 goto done;
2253 error = NULL;
2255 if (argc == 0) {
2256 path = strdup("");
2257 if (path == NULL) {
2258 error = got_error_from_errno("strdup");
2259 goto done;
2261 } else if (argc == 1) {
2262 if (worktree) {
2263 error = got_worktree_resolve_path(&path, worktree,
2264 argv[0]);
2265 if (error)
2266 goto done;
2267 } else {
2268 path = strdup(argv[0]);
2269 if (path == NULL) {
2270 error = got_error_from_errno("strdup");
2271 goto done;
2274 } else
2275 usage_log();
2277 if (repo_path == NULL) {
2278 if (worktree)
2279 repo_path = strdup(
2280 got_worktree_get_repo_path(worktree));
2281 else
2282 repo_path = strdup(cwd);
2284 if (repo_path == NULL) {
2285 error = got_error_from_errno("strdup");
2286 goto done;
2289 init_curses();
2291 error = got_repo_open(&repo, repo_path, NULL);
2292 if (error != NULL)
2293 goto done;
2295 error = apply_unveil(got_repo_get_path(repo),
2296 worktree ? got_worktree_get_root_path(worktree) : NULL);
2297 if (error)
2298 goto done;
2300 if (start_commit == NULL)
2301 error = get_head_commit_id(&start_id, worktree ?
2302 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2303 repo);
2304 else {
2305 error = get_head_commit_id(&start_id, start_commit, repo);
2306 if (error) {
2307 if (error->code != GOT_ERR_NOT_REF)
2308 goto done;
2309 error = got_repo_match_object_id_prefix(&start_id,
2310 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2313 if (error != NULL)
2314 goto done;
2316 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2317 if (error)
2318 goto done;
2320 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2321 if (view == NULL) {
2322 error = got_error_from_errno("view_open");
2323 goto done;
2325 if (worktree) {
2326 head_ref_name = strdup(
2327 got_worktree_get_head_ref_name(worktree));
2328 if (head_ref_name == NULL) {
2329 error = got_error_from_errno("strdup");
2330 goto done;
2333 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2334 path, 1);
2335 if (error)
2336 goto done;
2337 if (worktree) {
2338 /* Release work tree lock. */
2339 got_worktree_close(worktree);
2340 worktree = NULL;
2342 error = view_loop(view);
2343 done:
2344 free(repo_path);
2345 free(cwd);
2346 free(path);
2347 free(start_id);
2348 free(head_ref_name);
2349 if (repo)
2350 got_repo_close(repo);
2351 if (worktree)
2352 got_worktree_close(worktree);
2353 got_ref_list_free(&refs);
2354 return error;
2357 __dead static void
2358 usage_diff(void)
2360 endwin();
2361 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2362 getprogname());
2363 exit(1);
2366 static char *
2367 parse_next_line(FILE *f, size_t *len)
2369 char *line;
2370 size_t linelen;
2371 size_t lineno;
2372 const char delim[3] = { '\0', '\0', '\0'};
2374 line = fparseln(f, &linelen, &lineno, delim, 0);
2375 if (len)
2376 *len = linelen;
2377 return line;
2380 static const struct got_error *
2381 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2382 int *last_displayed_line, int *eof, int max_lines,
2383 char *header)
2385 const struct got_error *err;
2386 int nlines = 0, nprinted = 0;
2387 char *line;
2388 size_t len;
2389 wchar_t *wline;
2390 int width;
2392 rewind(f);
2393 werase(view->window);
2395 if (header) {
2396 err = format_line(&wline, &width, header, view->ncols, 0);
2397 if (err) {
2398 return err;
2401 if (view_needs_focus_indication(view))
2402 wstandout(view->window);
2403 waddwstr(view->window, wline);
2404 if (view_needs_focus_indication(view))
2405 wstandend(view->window);
2406 if (width <= view->ncols - 1)
2407 waddch(view->window, '\n');
2409 if (max_lines <= 1)
2410 return NULL;
2411 max_lines--;
2414 *eof = 0;
2415 while (nprinted < max_lines) {
2416 line = parse_next_line(f, &len);
2417 if (line == NULL) {
2418 *eof = 1;
2419 break;
2421 if (++nlines < *first_displayed_line) {
2422 free(line);
2423 continue;
2426 err = format_line(&wline, &width, line, view->ncols, 0);
2427 if (err) {
2428 free(line);
2429 return err;
2431 waddwstr(view->window, wline);
2432 if (width <= view->ncols - 1)
2433 waddch(view->window, '\n');
2434 if (++nprinted == 1)
2435 *first_displayed_line = nlines;
2436 free(line);
2437 free(wline);
2438 wline = NULL;
2440 *last_displayed_line = nlines;
2442 view_vborder(view);
2444 if (*eof) {
2445 while (nprinted < view->nlines) {
2446 waddch(view->window, '\n');
2447 nprinted++;
2450 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2451 if (err) {
2452 return err;
2455 wstandout(view->window);
2456 waddwstr(view->window, wline);
2457 wstandend(view->window);
2460 return NULL;
2463 static char *
2464 get_datestr(time_t *time, char *datebuf)
2466 struct tm mytm, *tm;
2467 char *p, *s;
2469 tm = gmtime_r(time, &mytm);
2470 if (tm == NULL)
2471 return NULL;
2472 s = asctime_r(tm, datebuf);
2473 if (s == NULL)
2474 return NULL;
2475 p = strchr(s, '\n');
2476 if (p)
2477 *p = '\0';
2478 return s;
2481 static const struct got_error *
2482 write_commit_info(struct got_object_id *commit_id,
2483 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2485 const struct got_error *err = NULL;
2486 char datebuf[26], *datestr;
2487 struct got_commit_object *commit;
2488 char *id_str = NULL, *logmsg = NULL;
2489 time_t committer_time;
2490 const char *author, *committer;
2491 char *refs_str = NULL;
2493 if (refs) {
2494 err = build_refs_str(&refs_str, refs, commit_id, repo);
2495 if (err)
2496 return err;
2499 err = got_object_open_as_commit(&commit, repo, commit_id);
2500 if (err)
2501 return err;
2503 err = got_object_id_str(&id_str, commit_id);
2504 if (err) {
2505 err = got_error_from_errno("got_object_id_str");
2506 goto done;
2509 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2510 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2511 err = got_error_from_errno("fprintf");
2512 goto done;
2514 if (fprintf(outfile, "from: %s\n",
2515 got_object_commit_get_author(commit)) < 0) {
2516 err = got_error_from_errno("fprintf");
2517 goto done;
2519 committer_time = got_object_commit_get_committer_time(commit);
2520 datestr = get_datestr(&committer_time, datebuf);
2521 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2522 err = got_error_from_errno("fprintf");
2523 goto done;
2525 author = got_object_commit_get_author(commit);
2526 committer = got_object_commit_get_committer(commit);
2527 if (strcmp(author, committer) != 0 &&
2528 fprintf(outfile, "via: %s\n", committer) < 0) {
2529 err = got_error_from_errno("fprintf");
2530 goto done;
2532 err = got_object_commit_get_logmsg(&logmsg, commit);
2533 if (err)
2534 goto done;
2535 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2536 err = got_error_from_errno("fprintf");
2537 goto done;
2539 done:
2540 free(id_str);
2541 free(logmsg);
2542 free(refs_str);
2543 got_object_commit_close(commit);
2544 return err;
2547 static const struct got_error *
2548 create_diff(struct tog_diff_view_state *s)
2550 const struct got_error *err = NULL;
2551 FILE *f = NULL;
2552 int obj_type;
2554 f = got_opentemp();
2555 if (f == NULL) {
2556 err = got_error_from_errno("got_opentemp");
2557 goto done;
2559 if (s->f && fclose(s->f) != 0) {
2560 err = got_error_from_errno("fclose");
2561 goto done;
2563 s->f = f;
2565 if (s->id1)
2566 err = got_object_get_type(&obj_type, s->repo, s->id1);
2567 else
2568 err = got_object_get_type(&obj_type, s->repo, s->id2);
2569 if (err)
2570 goto done;
2572 switch (obj_type) {
2573 case GOT_OBJ_TYPE_BLOB:
2574 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2575 s->diff_context, s->repo, f);
2576 break;
2577 case GOT_OBJ_TYPE_TREE:
2578 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2579 s->diff_context, s->repo, f);
2580 break;
2581 case GOT_OBJ_TYPE_COMMIT: {
2582 const struct got_object_id_queue *parent_ids;
2583 struct got_object_qid *pid;
2584 struct got_commit_object *commit2;
2586 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2587 if (err)
2588 break;
2589 /* Show commit info if we're diffing to a parent/root commit. */
2590 if (s->id1 == NULL)
2591 write_commit_info(s->id2, s->refs, s->repo, f);
2592 else {
2593 parent_ids = got_object_commit_get_parent_ids(commit2);
2594 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2595 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2596 write_commit_info(s->id2, s->refs,
2597 s->repo, f);
2598 break;
2602 got_object_commit_close(commit2);
2604 err = got_diff_objects_as_commits(s->id1, s->id2,
2605 s->diff_context, s->repo, f);
2606 break;
2608 default:
2609 err = got_error(GOT_ERR_OBJ_TYPE);
2610 break;
2612 done:
2613 if (f && fflush(f) != 0 && err == NULL)
2614 err = got_error_from_errno("fflush");
2615 return err;
2618 static void
2619 diff_view_indicate_progress(struct tog_view *view)
2621 mvwaddstr(view->window, 0, 0, "diffing...");
2622 update_panels();
2623 doupdate();
2626 static const struct got_error *
2627 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2628 struct got_object_id *id2, struct tog_view *log_view,
2629 struct got_reflist_head *refs, struct got_repository *repo)
2631 const struct got_error *err;
2633 if (id1 != NULL && id2 != NULL) {
2634 int type1, type2;
2635 err = got_object_get_type(&type1, repo, id1);
2636 if (err)
2637 return err;
2638 err = got_object_get_type(&type2, repo, id2);
2639 if (err)
2640 return err;
2642 if (type1 != type2)
2643 return got_error(GOT_ERR_OBJ_TYPE);
2646 if (id1) {
2647 view->state.diff.id1 = got_object_id_dup(id1);
2648 if (view->state.diff.id1 == NULL)
2649 return got_error_from_errno("got_object_id_dup");
2650 } else
2651 view->state.diff.id1 = NULL;
2653 view->state.diff.id2 = got_object_id_dup(id2);
2654 if (view->state.diff.id2 == NULL) {
2655 free(view->state.diff.id1);
2656 view->state.diff.id1 = NULL;
2657 return got_error_from_errno("got_object_id_dup");
2659 view->state.diff.f = NULL;
2660 view->state.diff.first_displayed_line = 1;
2661 view->state.diff.last_displayed_line = view->nlines;
2662 view->state.diff.diff_context = 3;
2663 view->state.diff.log_view = log_view;
2664 view->state.diff.repo = repo;
2665 view->state.diff.refs = refs;
2667 if (log_view && view_is_splitscreen(view))
2668 show_log_view(log_view); /* draw vborder */
2669 diff_view_indicate_progress(view);
2671 err = create_diff(&view->state.diff);
2672 if (err) {
2673 free(view->state.diff.id1);
2674 view->state.diff.id1 = NULL;
2675 free(view->state.diff.id2);
2676 view->state.diff.id2 = NULL;
2677 return err;
2680 view->show = show_diff_view;
2681 view->input = input_diff_view;
2682 view->close = close_diff_view;
2684 return NULL;
2687 static const struct got_error *
2688 close_diff_view(struct tog_view *view)
2690 const struct got_error *err = NULL;
2692 free(view->state.diff.id1);
2693 view->state.diff.id1 = NULL;
2694 free(view->state.diff.id2);
2695 view->state.diff.id2 = NULL;
2696 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2697 err = got_error_from_errno("fclose");
2698 return err;
2701 static const struct got_error *
2702 show_diff_view(struct tog_view *view)
2704 const struct got_error *err;
2705 struct tog_diff_view_state *s = &view->state.diff;
2706 char *id_str1 = NULL, *id_str2, *header;
2708 if (s->id1) {
2709 err = got_object_id_str(&id_str1, s->id1);
2710 if (err)
2711 return err;
2713 err = got_object_id_str(&id_str2, s->id2);
2714 if (err)
2715 return err;
2717 if (asprintf(&header, "diff %s %s",
2718 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2719 err = got_error_from_errno("asprintf");
2720 free(id_str1);
2721 free(id_str2);
2722 return err;
2724 free(id_str1);
2725 free(id_str2);
2727 return draw_file(view, s->f, &s->first_displayed_line,
2728 &s->last_displayed_line, &s->eof, view->nlines,
2729 header);
2732 static const struct got_error *
2733 set_selected_commit(struct tog_diff_view_state *s,
2734 struct commit_queue_entry *entry)
2736 const struct got_error *err;
2737 const struct got_object_id_queue *parent_ids;
2738 struct got_commit_object *selected_commit;
2739 struct got_object_qid *pid;
2741 free(s->id2);
2742 s->id2 = got_object_id_dup(entry->id);
2743 if (s->id2 == NULL)
2744 return got_error_from_errno("got_object_id_dup");
2746 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2747 if (err)
2748 return err;
2749 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2750 free(s->id1);
2751 pid = SIMPLEQ_FIRST(parent_ids);
2752 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2753 got_object_commit_close(selected_commit);
2754 return NULL;
2757 static const struct got_error *
2758 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2759 struct tog_view **focus_view, struct tog_view *view, int ch)
2761 const struct got_error *err = NULL;
2762 struct tog_diff_view_state *s = &view->state.diff;
2763 struct tog_log_view_state *ls;
2764 struct commit_queue_entry *entry;
2765 int i;
2767 switch (ch) {
2768 case 'k':
2769 case KEY_UP:
2770 if (s->first_displayed_line > 1)
2771 s->first_displayed_line--;
2772 break;
2773 case KEY_PPAGE:
2774 case CTRL('b'):
2775 if (s->first_displayed_line == 1)
2776 break;
2777 i = 0;
2778 while (i++ < view->nlines - 1 &&
2779 s->first_displayed_line > 1)
2780 s->first_displayed_line--;
2781 break;
2782 case 'j':
2783 case KEY_DOWN:
2784 if (!s->eof)
2785 s->first_displayed_line++;
2786 break;
2787 case KEY_NPAGE:
2788 case CTRL('f'):
2789 case ' ':
2790 if (s->eof)
2791 break;
2792 i = 0;
2793 while (!s->eof && i++ < view->nlines - 1) {
2794 char *line;
2795 line = parse_next_line(s->f, NULL);
2796 s->first_displayed_line++;
2797 if (line == NULL)
2798 break;
2800 break;
2801 case '[':
2802 if (s->diff_context > 0) {
2803 s->diff_context--;
2804 diff_view_indicate_progress(view);
2805 err = create_diff(s);
2807 break;
2808 case ']':
2809 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2810 s->diff_context++;
2811 diff_view_indicate_progress(view);
2812 err = create_diff(s);
2814 break;
2815 case '<':
2816 case ',':
2817 if (s->log_view == NULL)
2818 break;
2819 ls = &s->log_view->state.log;
2820 entry = TAILQ_PREV(ls->selected_entry,
2821 commit_queue_head, entry);
2822 if (entry == NULL)
2823 break;
2825 err = input_log_view(NULL, NULL, NULL, s->log_view,
2826 KEY_UP);
2827 if (err)
2828 break;
2830 err = set_selected_commit(s, entry);
2831 if (err)
2832 break;
2834 s->first_displayed_line = 1;
2835 s->last_displayed_line = view->nlines;
2837 diff_view_indicate_progress(view);
2838 err = create_diff(s);
2839 break;
2840 case '>':
2841 case '.':
2842 if (s->log_view == NULL)
2843 break;
2844 ls = &s->log_view->state.log;
2846 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2847 ls->thread_args.commits_needed++;
2849 /* Display "loading..." in log view. */
2850 show_log_view(s->log_view);
2851 update_panels();
2852 doupdate();
2854 err = trigger_log_thread(1 /* load_all */,
2855 &ls->thread_args.commits_needed,
2856 &ls->thread_args.log_complete,
2857 &ls->thread_args.need_commits);
2858 if (err)
2859 break;
2861 err = input_log_view(NULL, NULL, NULL, s->log_view,
2862 KEY_DOWN);
2863 if (err)
2864 break;
2866 entry = TAILQ_NEXT(ls->selected_entry, entry);
2867 if (entry == NULL)
2868 break;
2870 err = set_selected_commit(s, entry);
2871 if (err)
2872 break;
2874 s->first_displayed_line = 1;
2875 s->last_displayed_line = view->nlines;
2877 diff_view_indicate_progress(view);
2878 err = create_diff(s);
2879 break;
2880 default:
2881 break;
2884 return err;
2887 static const struct got_error *
2888 cmd_diff(int argc, char *argv[])
2890 const struct got_error *error = NULL;
2891 struct got_repository *repo = NULL;
2892 struct got_reflist_head refs;
2893 struct got_object_id *id1 = NULL, *id2 = NULL;
2894 char *repo_path = NULL;
2895 char *id_str1 = NULL, *id_str2 = NULL;
2896 int ch;
2897 struct tog_view *view;
2899 SIMPLEQ_INIT(&refs);
2901 #ifndef PROFILE
2902 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2903 NULL) == -1)
2904 err(1, "pledge");
2905 #endif
2907 while ((ch = getopt(argc, argv, "")) != -1) {
2908 switch (ch) {
2909 default:
2910 usage_diff();
2911 /* NOTREACHED */
2915 argc -= optind;
2916 argv += optind;
2918 if (argc == 0) {
2919 usage_diff(); /* TODO show local worktree changes */
2920 } else if (argc == 2) {
2921 repo_path = getcwd(NULL, 0);
2922 if (repo_path == NULL)
2923 return got_error_from_errno("getcwd");
2924 id_str1 = argv[0];
2925 id_str2 = argv[1];
2926 } else if (argc == 3) {
2927 repo_path = realpath(argv[0], NULL);
2928 if (repo_path == NULL)
2929 return got_error_from_errno2("realpath", argv[0]);
2930 id_str1 = argv[1];
2931 id_str2 = argv[2];
2932 } else
2933 usage_diff();
2935 init_curses();
2937 error = got_repo_open(&repo, repo_path, NULL);
2938 if (error)
2939 goto done;
2941 error = apply_unveil(got_repo_get_path(repo), NULL);
2942 if (error)
2943 goto done;
2945 error = got_repo_match_object_id_prefix(&id1, id_str1,
2946 GOT_OBJ_TYPE_ANY, repo);
2947 if (error)
2948 goto done;
2950 error = got_repo_match_object_id_prefix(&id2, id_str2,
2951 GOT_OBJ_TYPE_ANY, repo);
2952 if (error)
2953 goto done;
2955 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2956 if (error)
2957 goto done;
2959 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2960 if (view == NULL) {
2961 error = got_error_from_errno("view_open");
2962 goto done;
2964 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2965 if (error)
2966 goto done;
2967 error = view_loop(view);
2968 done:
2969 free(repo_path);
2970 if (repo)
2971 got_repo_close(repo);
2972 got_ref_list_free(&refs);
2973 return error;
2976 __dead static void
2977 usage_blame(void)
2979 endwin();
2980 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2981 getprogname());
2982 exit(1);
2985 struct tog_blame_line {
2986 int annotated;
2987 struct got_object_id *id;
2990 static const struct got_error *
2991 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2992 const char *path, struct tog_blame_line *lines, int nlines,
2993 int blame_complete, int selected_line, int *first_displayed_line,
2994 int *last_displayed_line, int *eof, int max_lines)
2996 const struct got_error *err;
2997 int lineno = 0, nprinted = 0;
2998 char *line;
2999 size_t len;
3000 wchar_t *wline;
3001 int width;
3002 struct tog_blame_line *blame_line;
3003 struct got_object_id *prev_id = NULL;
3004 char *id_str;
3006 err = got_object_id_str(&id_str, id);
3007 if (err)
3008 return err;
3010 rewind(f);
3011 werase(view->window);
3013 if (asprintf(&line, "commit %s", id_str) == -1) {
3014 err = got_error_from_errno("asprintf");
3015 free(id_str);
3016 return err;
3019 err = format_line(&wline, &width, line, view->ncols, 0);
3020 free(line);
3021 line = NULL;
3022 if (err)
3023 return err;
3024 if (view_needs_focus_indication(view))
3025 wstandout(view->window);
3026 waddwstr(view->window, wline);
3027 if (view_needs_focus_indication(view))
3028 wstandend(view->window);
3029 free(wline);
3030 wline = NULL;
3031 if (width < view->ncols - 1)
3032 waddch(view->window, '\n');
3034 if (asprintf(&line, "[%d/%d] %s%s",
3035 *first_displayed_line - 1 + selected_line, nlines,
3036 blame_complete ? "" : "annotating... ", path) == -1) {
3037 free(id_str);
3038 return got_error_from_errno("asprintf");
3040 free(id_str);
3041 err = format_line(&wline, &width, line, view->ncols, 0);
3042 free(line);
3043 line = NULL;
3044 if (err)
3045 return err;
3046 waddwstr(view->window, wline);
3047 free(wline);
3048 wline = NULL;
3049 if (width < view->ncols - 1)
3050 waddch(view->window, '\n');
3052 *eof = 0;
3053 while (nprinted < max_lines - 2) {
3054 line = parse_next_line(f, &len);
3055 if (line == NULL) {
3056 *eof = 1;
3057 break;
3059 if (++lineno < *first_displayed_line) {
3060 free(line);
3061 continue;
3064 if (view->ncols <= 9) {
3065 width = 9;
3066 wline = wcsdup(L"");
3067 if (wline == NULL)
3068 err = got_error_from_errno("wcsdup");
3069 } else {
3070 err = format_line(&wline, &width, line,
3071 view->ncols - 9, 9);
3072 width += 9;
3074 if (err) {
3075 free(line);
3076 return err;
3079 if (view->focussed && nprinted == selected_line - 1)
3080 wstandout(view->window);
3082 if (nlines > 0) {
3083 blame_line = &lines[lineno - 1];
3084 if (blame_line->annotated && prev_id &&
3085 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3086 !(view->focussed &&
3087 nprinted == selected_line - 1)) {
3088 waddstr(view->window, " ");
3089 } else if (blame_line->annotated) {
3090 char *id_str;
3091 err = got_object_id_str(&id_str, blame_line->id);
3092 if (err) {
3093 free(line);
3094 free(wline);
3095 return err;
3097 wprintw(view->window, "%.8s", id_str);
3098 free(id_str);
3099 prev_id = blame_line->id;
3100 } else {
3101 waddstr(view->window, "........");
3102 prev_id = NULL;
3104 } else {
3105 waddstr(view->window, "........");
3106 prev_id = NULL;
3109 if (view->focussed && nprinted == selected_line - 1)
3110 wstandend(view->window);
3111 waddstr(view->window, " ");
3113 waddwstr(view->window, wline);
3114 if (width <= view->ncols - 1)
3115 waddch(view->window, '\n');
3116 if (++nprinted == 1)
3117 *first_displayed_line = lineno;
3118 free(line);
3119 free(wline);
3120 wline = NULL;
3122 *last_displayed_line = lineno;
3124 view_vborder(view);
3126 return NULL;
3129 static const struct got_error *
3130 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3132 const struct got_error *err = NULL;
3133 struct tog_blame_cb_args *a = arg;
3134 struct tog_blame_line *line;
3135 int errcode;
3137 if (nlines != a->nlines ||
3138 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3139 return got_error(GOT_ERR_RANGE);
3141 errcode = pthread_mutex_lock(&tog_mutex);
3142 if (errcode)
3143 return got_error_set_errno(errcode, "pthread_mutex_lock");
3145 if (*a->quit) { /* user has quit the blame view */
3146 err = got_error(GOT_ERR_ITER_COMPLETED);
3147 goto done;
3150 if (lineno == -1)
3151 goto done; /* no change in this commit */
3153 line = &a->lines[lineno - 1];
3154 if (line->annotated)
3155 goto done;
3157 line->id = got_object_id_dup(id);
3158 if (line->id == NULL) {
3159 err = got_error_from_errno("got_object_id_dup");
3160 goto done;
3162 line->annotated = 1;
3163 done:
3164 errcode = pthread_mutex_unlock(&tog_mutex);
3165 if (errcode)
3166 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3167 return err;
3170 static void *
3171 blame_thread(void *arg)
3173 const struct got_error *err;
3174 struct tog_blame_thread_args *ta = arg;
3175 struct tog_blame_cb_args *a = ta->cb_args;
3176 int errcode;
3178 err = got_blame(ta->path, a->commit_id, ta->repo,
3179 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3180 if (err && err->code == GOT_ERR_CANCELLED)
3181 err = NULL;
3183 errcode = pthread_mutex_lock(&tog_mutex);
3184 if (errcode)
3185 return (void *)got_error_set_errno(errcode,
3186 "pthread_mutex_lock");
3188 got_repo_close(ta->repo);
3189 ta->repo = NULL;
3190 *ta->complete = 1;
3192 errcode = pthread_mutex_unlock(&tog_mutex);
3193 if (errcode && err == NULL)
3194 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3196 return (void *)err;
3199 static struct got_object_id *
3200 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3201 int first_displayed_line, int selected_line)
3203 struct tog_blame_line *line;
3205 if (nlines <= 0)
3206 return NULL;
3208 line = &lines[first_displayed_line - 1 + selected_line - 1];
3209 if (!line->annotated)
3210 return NULL;
3212 return line->id;
3215 static const struct got_error *
3216 stop_blame(struct tog_blame *blame)
3218 const struct got_error *err = NULL;
3219 int i;
3221 if (blame->thread) {
3222 int errcode;
3223 errcode = pthread_mutex_unlock(&tog_mutex);
3224 if (errcode)
3225 return got_error_set_errno(errcode,
3226 "pthread_mutex_unlock");
3227 errcode = pthread_join(blame->thread, (void **)&err);
3228 if (errcode)
3229 return got_error_set_errno(errcode, "pthread_join");
3230 errcode = pthread_mutex_lock(&tog_mutex);
3231 if (errcode)
3232 return got_error_set_errno(errcode,
3233 "pthread_mutex_lock");
3234 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3235 err = NULL;
3236 blame->thread = NULL;
3238 if (blame->thread_args.repo) {
3239 got_repo_close(blame->thread_args.repo);
3240 blame->thread_args.repo = NULL;
3242 if (blame->f) {
3243 if (fclose(blame->f) != 0 && err == NULL)
3244 err = got_error_from_errno("fclose");
3245 blame->f = NULL;
3247 if (blame->lines) {
3248 for (i = 0; i < blame->nlines; i++)
3249 free(blame->lines[i].id);
3250 free(blame->lines);
3251 blame->lines = NULL;
3253 free(blame->cb_args.commit_id);
3254 blame->cb_args.commit_id = NULL;
3256 return err;
3259 static const struct got_error *
3260 cancel_blame_view(void *arg)
3262 const struct got_error *err = NULL;
3263 int *done = arg;
3264 int errcode;
3266 errcode = pthread_mutex_lock(&tog_mutex);
3267 if (errcode)
3268 return got_error_set_errno(errcode,
3269 "pthread_mutex_unlock");
3271 if (*done)
3272 err = got_error(GOT_ERR_CANCELLED);
3274 errcode = pthread_mutex_unlock(&tog_mutex);
3275 if (errcode)
3276 return got_error_set_errno(errcode,
3277 "pthread_mutex_lock");
3279 return err;
3282 static const struct got_error *
3283 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3284 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3285 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3286 struct got_repository *repo)
3288 const struct got_error *err = NULL;
3289 struct got_blob_object *blob = NULL;
3290 struct got_repository *thread_repo = NULL;
3291 struct got_object_id *obj_id = NULL;
3292 int obj_type;
3294 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3295 if (err)
3296 return err;
3297 if (obj_id == NULL)
3298 return got_error(GOT_ERR_NO_OBJ);
3300 err = got_object_get_type(&obj_type, repo, obj_id);
3301 if (err)
3302 goto done;
3304 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3305 err = got_error(GOT_ERR_OBJ_TYPE);
3306 goto done;
3309 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3310 if (err)
3311 goto done;
3312 blame->f = got_opentemp();
3313 if (blame->f == NULL) {
3314 err = got_error_from_errno("got_opentemp");
3315 goto done;
3317 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3318 &blame->line_offsets, blame->f, blob);
3319 if (err || blame->nlines == 0)
3320 goto done;
3322 /* Don't include \n at EOF in the blame line count. */
3323 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3324 blame->nlines--;
3326 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3327 if (blame->lines == NULL) {
3328 err = got_error_from_errno("calloc");
3329 goto done;
3332 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3333 if (err)
3334 goto done;
3336 blame->cb_args.view = view;
3337 blame->cb_args.lines = blame->lines;
3338 blame->cb_args.nlines = blame->nlines;
3339 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3340 if (blame->cb_args.commit_id == NULL) {
3341 err = got_error_from_errno("got_object_id_dup");
3342 goto done;
3344 blame->cb_args.quit = done;
3346 blame->thread_args.path = path;
3347 blame->thread_args.repo = thread_repo;
3348 blame->thread_args.cb_args = &blame->cb_args;
3349 blame->thread_args.complete = blame_complete;
3350 blame->thread_args.cancel_cb = cancel_blame_view;
3351 blame->thread_args.cancel_arg = done;
3352 *blame_complete = 0;
3354 done:
3355 if (blob)
3356 got_object_blob_close(blob);
3357 free(obj_id);
3358 if (err)
3359 stop_blame(blame);
3360 return err;
3363 static const struct got_error *
3364 open_blame_view(struct tog_view *view, char *path,
3365 struct got_object_id *commit_id, struct got_reflist_head *refs,
3366 struct got_repository *repo)
3368 const struct got_error *err = NULL;
3369 struct tog_blame_view_state *s = &view->state.blame;
3371 SIMPLEQ_INIT(&s->blamed_commits);
3373 s->path = strdup(path);
3374 if (s->path == NULL)
3375 return got_error_from_errno("strdup");
3377 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3378 if (err) {
3379 free(s->path);
3380 return err;
3383 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3384 s->first_displayed_line = 1;
3385 s->last_displayed_line = view->nlines;
3386 s->selected_line = 1;
3387 s->blame_complete = 0;
3388 s->repo = repo;
3389 s->refs = refs;
3390 s->commit_id = commit_id;
3391 memset(&s->blame, 0, sizeof(s->blame));
3393 view->show = show_blame_view;
3394 view->input = input_blame_view;
3395 view->close = close_blame_view;
3396 view->search_start = search_start_blame_view;
3397 view->search_next = search_next_blame_view;
3399 return run_blame(&s->blame, view, &s->blame_complete,
3400 &s->first_displayed_line, &s->last_displayed_line,
3401 &s->selected_line, &s->done, &s->eof, s->path,
3402 s->blamed_commit->id, s->repo);
3405 static const struct got_error *
3406 close_blame_view(struct tog_view *view)
3408 const struct got_error *err = NULL;
3409 struct tog_blame_view_state *s = &view->state.blame;
3411 if (s->blame.thread)
3412 err = stop_blame(&s->blame);
3414 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3415 struct got_object_qid *blamed_commit;
3416 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3417 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3418 got_object_qid_free(blamed_commit);
3421 free(s->path);
3423 return err;
3426 static const struct got_error *
3427 search_start_blame_view(struct tog_view *view)
3429 struct tog_blame_view_state *s = &view->state.blame;
3431 s->matched_line = 0;
3432 return NULL;
3435 static int
3436 match_line(const char *line, regex_t *regex)
3438 regmatch_t regmatch;
3440 return regexec(regex, line, 1, &regmatch, 0) == 0;
3444 static const struct got_error *
3445 search_next_blame_view(struct tog_view *view)
3447 struct tog_blame_view_state *s = &view->state.blame;
3448 int lineno;
3450 if (!view->searching) {
3451 view->search_next_done = 1;
3452 return NULL;
3455 if (s->matched_line) {
3456 if (view->searching == TOG_SEARCH_FORWARD)
3457 lineno = s->matched_line + 1;
3458 else
3459 lineno = s->matched_line - 1;
3460 } else {
3461 if (view->searching == TOG_SEARCH_FORWARD)
3462 lineno = 1;
3463 else
3464 lineno = s->blame.nlines;
3467 while (1) {
3468 char *line = NULL;
3469 off_t offset;
3470 size_t len;
3472 if (lineno <= 0 || lineno > s->blame.nlines) {
3473 if (s->matched_line == 0) {
3474 view->search_next_done = 1;
3475 free(line);
3476 break;
3479 if (view->searching == TOG_SEARCH_FORWARD)
3480 lineno = 1;
3481 else
3482 lineno = s->blame.nlines;
3485 offset = s->blame.line_offsets[lineno - 1];
3486 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3487 free(line);
3488 return got_error_from_errno("fseeko");
3490 free(line);
3491 line = parse_next_line(s->blame.f, &len);
3492 if (line && match_line(line, &view->regex)) {
3493 view->search_next_done = 1;
3494 s->matched_line = lineno;
3495 free(line);
3496 break;
3498 free(line);
3499 if (view->searching == TOG_SEARCH_FORWARD)
3500 lineno++;
3501 else
3502 lineno--;
3505 if (s->matched_line) {
3506 s->first_displayed_line = s->matched_line;
3507 s->selected_line = 1;
3510 return NULL;
3513 static const struct got_error *
3514 show_blame_view(struct tog_view *view)
3516 const struct got_error *err = NULL;
3517 struct tog_blame_view_state *s = &view->state.blame;
3518 int errcode;
3520 if (s->blame.thread == NULL) {
3521 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3522 &s->blame.thread_args);
3523 if (errcode)
3524 return got_error_set_errno(errcode, "pthread_create");
3526 halfdelay(1); /* fast refresh while annotating */
3529 if (s->blame_complete)
3530 halfdelay(10); /* disable fast refresh */
3532 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3533 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3534 s->selected_line, &s->first_displayed_line,
3535 &s->last_displayed_line, &s->eof, view->nlines);
3537 view_vborder(view);
3538 return err;
3541 static const struct got_error *
3542 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3543 struct tog_view **focus_view, struct tog_view *view, int ch)
3545 const struct got_error *err = NULL, *thread_err = NULL;
3546 struct tog_view *diff_view;
3547 struct tog_blame_view_state *s = &view->state.blame;
3548 int begin_x = 0;
3550 switch (ch) {
3551 case 'q':
3552 s->done = 1;
3553 break;
3554 case 'k':
3555 case KEY_UP:
3556 if (s->selected_line > 1)
3557 s->selected_line--;
3558 else if (s->selected_line == 1 &&
3559 s->first_displayed_line > 1)
3560 s->first_displayed_line--;
3561 break;
3562 case KEY_PPAGE:
3563 if (s->first_displayed_line == 1) {
3564 s->selected_line = 1;
3565 break;
3567 if (s->first_displayed_line > view->nlines - 2)
3568 s->first_displayed_line -=
3569 (view->nlines - 2);
3570 else
3571 s->first_displayed_line = 1;
3572 break;
3573 case 'j':
3574 case KEY_DOWN:
3575 if (s->selected_line < view->nlines - 2 &&
3576 s->first_displayed_line +
3577 s->selected_line <= s->blame.nlines)
3578 s->selected_line++;
3579 else if (s->last_displayed_line <
3580 s->blame.nlines)
3581 s->first_displayed_line++;
3582 break;
3583 case 'b':
3584 case 'p': {
3585 struct got_object_id *id = NULL;
3586 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3587 s->first_displayed_line, s->selected_line);
3588 if (id == NULL)
3589 break;
3590 if (ch == 'p') {
3591 struct got_commit_object *commit;
3592 struct got_object_qid *pid;
3593 struct got_object_id *blob_id = NULL;
3594 int obj_type;
3595 err = got_object_open_as_commit(&commit,
3596 s->repo, id);
3597 if (err)
3598 break;
3599 pid = SIMPLEQ_FIRST(
3600 got_object_commit_get_parent_ids(commit));
3601 if (pid == NULL) {
3602 got_object_commit_close(commit);
3603 break;
3605 /* Check if path history ends here. */
3606 err = got_object_id_by_path(&blob_id, s->repo,
3607 pid->id, s->path);
3608 if (err) {
3609 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3610 err = NULL;
3611 got_object_commit_close(commit);
3612 break;
3614 err = got_object_get_type(&obj_type, s->repo,
3615 blob_id);
3616 free(blob_id);
3617 /* Can't blame non-blob type objects. */
3618 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3619 got_object_commit_close(commit);
3620 break;
3622 err = got_object_qid_alloc(&s->blamed_commit,
3623 pid->id);
3624 got_object_commit_close(commit);
3625 } else {
3626 if (got_object_id_cmp(id,
3627 s->blamed_commit->id) == 0)
3628 break;
3629 err = got_object_qid_alloc(&s->blamed_commit,
3630 id);
3632 if (err)
3633 break;
3634 s->done = 1;
3635 thread_err = stop_blame(&s->blame);
3636 s->done = 0;
3637 if (thread_err)
3638 break;
3639 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3640 s->blamed_commit, entry);
3641 err = run_blame(&s->blame, view, &s->blame_complete,
3642 &s->first_displayed_line, &s->last_displayed_line,
3643 &s->selected_line, &s->done, &s->eof,
3644 s->path, s->blamed_commit->id, s->repo);
3645 if (err)
3646 break;
3647 break;
3649 case 'B': {
3650 struct got_object_qid *first;
3651 first = SIMPLEQ_FIRST(&s->blamed_commits);
3652 if (!got_object_id_cmp(first->id, s->commit_id))
3653 break;
3654 s->done = 1;
3655 thread_err = stop_blame(&s->blame);
3656 s->done = 0;
3657 if (thread_err)
3658 break;
3659 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3660 got_object_qid_free(s->blamed_commit);
3661 s->blamed_commit =
3662 SIMPLEQ_FIRST(&s->blamed_commits);
3663 err = run_blame(&s->blame, view, &s->blame_complete,
3664 &s->first_displayed_line, &s->last_displayed_line,
3665 &s->selected_line, &s->done, &s->eof, s->path,
3666 s->blamed_commit->id, s->repo);
3667 if (err)
3668 break;
3669 break;
3671 case KEY_ENTER:
3672 case '\r': {
3673 struct got_object_id *id = NULL;
3674 struct got_object_qid *pid;
3675 struct got_commit_object *commit = NULL;
3676 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3677 s->first_displayed_line, s->selected_line);
3678 if (id == NULL)
3679 break;
3680 err = got_object_open_as_commit(&commit, s->repo, id);
3681 if (err)
3682 break;
3683 pid = SIMPLEQ_FIRST(
3684 got_object_commit_get_parent_ids(commit));
3685 if (view_is_parent_view(view))
3686 begin_x = view_split_begin_x(view->begin_x);
3687 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3688 if (diff_view == NULL) {
3689 got_object_commit_close(commit);
3690 err = got_error_from_errno("view_open");
3691 break;
3693 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3694 id, NULL, s->refs, s->repo);
3695 got_object_commit_close(commit);
3696 if (err) {
3697 view_close(diff_view);
3698 break;
3700 if (view_is_parent_view(view)) {
3701 err = view_close_child(view);
3702 if (err)
3703 break;
3704 err = view_set_child(view, diff_view);
3705 if (err) {
3706 view_close(diff_view);
3707 break;
3709 *focus_view = diff_view;
3710 view->child_focussed = 1;
3711 } else
3712 *new_view = diff_view;
3713 if (err)
3714 break;
3715 break;
3717 case KEY_NPAGE:
3718 case ' ':
3719 if (s->last_displayed_line >= s->blame.nlines &&
3720 s->selected_line >= MIN(s->blame.nlines,
3721 view->nlines - 2)) {
3722 break;
3724 if (s->last_displayed_line >= s->blame.nlines &&
3725 s->selected_line < view->nlines - 2) {
3726 s->selected_line = MIN(s->blame.nlines,
3727 view->nlines - 2);
3728 break;
3730 if (s->last_displayed_line + view->nlines - 2
3731 <= s->blame.nlines)
3732 s->first_displayed_line +=
3733 view->nlines - 2;
3734 else
3735 s->first_displayed_line =
3736 s->blame.nlines -
3737 (view->nlines - 3);
3738 break;
3739 case KEY_RESIZE:
3740 if (s->selected_line > view->nlines - 2) {
3741 s->selected_line = MIN(s->blame.nlines,
3742 view->nlines - 2);
3744 break;
3745 default:
3746 break;
3748 return thread_err ? thread_err : err;
3751 static const struct got_error *
3752 cmd_blame(int argc, char *argv[])
3754 const struct got_error *error;
3755 struct got_repository *repo = NULL;
3756 struct got_reflist_head refs;
3757 struct got_worktree *worktree = NULL;
3758 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3759 struct got_object_id *commit_id = NULL;
3760 char *commit_id_str = NULL;
3761 int ch;
3762 struct tog_view *view;
3764 SIMPLEQ_INIT(&refs);
3766 #ifndef PROFILE
3767 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3768 NULL) == -1)
3769 err(1, "pledge");
3770 #endif
3772 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3773 switch (ch) {
3774 case 'c':
3775 commit_id_str = optarg;
3776 break;
3777 case 'r':
3778 repo_path = realpath(optarg, NULL);
3779 if (repo_path == NULL)
3780 err(1, "-r option");
3781 break;
3782 default:
3783 usage_blame();
3784 /* NOTREACHED */
3788 argc -= optind;
3789 argv += optind;
3791 if (argc == 1)
3792 path = argv[0];
3793 else
3794 usage_blame();
3796 cwd = getcwd(NULL, 0);
3797 if (cwd == NULL) {
3798 error = got_error_from_errno("getcwd");
3799 goto done;
3801 if (repo_path == NULL) {
3802 error = got_worktree_open(&worktree, cwd);
3803 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3804 goto done;
3805 else
3806 error = NULL;
3807 if (worktree) {
3808 repo_path =
3809 strdup(got_worktree_get_repo_path(worktree));
3810 if (repo_path == NULL)
3811 error = got_error_from_errno("strdup");
3812 if (error)
3813 goto done;
3814 } else {
3815 repo_path = strdup(cwd);
3816 if (repo_path == NULL) {
3817 error = got_error_from_errno("strdup");
3818 goto done;
3823 init_curses();
3825 error = got_repo_open(&repo, repo_path, NULL);
3826 if (error != NULL)
3827 goto done;
3829 error = apply_unveil(got_repo_get_path(repo), NULL);
3830 if (error)
3831 goto done;
3833 if (worktree) {
3834 const char *prefix = got_worktree_get_path_prefix(worktree);
3835 char *p, *worktree_subdir = cwd +
3836 strlen(got_worktree_get_root_path(worktree));
3837 if (asprintf(&p, "%s%s%s%s%s",
3838 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3839 worktree_subdir, worktree_subdir[0] ? "/" : "",
3840 path) == -1) {
3841 error = got_error_from_errno("asprintf");
3842 goto done;
3844 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3845 free(p);
3846 } else {
3847 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3849 if (error)
3850 goto done;
3852 if (commit_id_str == NULL) {
3853 struct got_reference *head_ref;
3854 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3855 if (error != NULL)
3856 goto done;
3857 error = got_ref_resolve(&commit_id, repo, head_ref);
3858 got_ref_close(head_ref);
3859 } else {
3860 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3861 if (error) {
3862 if (error->code != GOT_ERR_NOT_REF)
3863 goto done;
3864 error = got_repo_match_object_id_prefix(&commit_id,
3865 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3868 if (error != NULL)
3869 goto done;
3871 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3872 if (error)
3873 goto done;
3875 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3876 if (view == NULL) {
3877 error = got_error_from_errno("view_open");
3878 goto done;
3880 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3881 if (error)
3882 goto done;
3883 if (worktree) {
3884 /* Release work tree lock. */
3885 got_worktree_close(worktree);
3886 worktree = NULL;
3888 error = view_loop(view);
3889 done:
3890 free(repo_path);
3891 free(cwd);
3892 free(commit_id);
3893 if (worktree)
3894 got_worktree_close(worktree);
3895 if (repo)
3896 got_repo_close(repo);
3897 got_ref_list_free(&refs);
3898 return error;
3901 static const struct got_error *
3902 draw_tree_entries(struct tog_view *view,
3903 struct got_tree_entry **first_displayed_entry,
3904 struct got_tree_entry **last_displayed_entry,
3905 struct got_tree_entry **selected_entry, int *ndisplayed,
3906 const char *label, int show_ids, const char *parent_path,
3907 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3909 const struct got_error *err = NULL;
3910 struct got_tree_entry *te;
3911 wchar_t *wline;
3912 int width, n;
3914 *ndisplayed = 0;
3916 werase(view->window);
3918 if (limit == 0)
3919 return NULL;
3921 err = format_line(&wline, &width, label, view->ncols, 0);
3922 if (err)
3923 return err;
3924 if (view_needs_focus_indication(view))
3925 wstandout(view->window);
3926 waddwstr(view->window, wline);
3927 if (view_needs_focus_indication(view))
3928 wstandend(view->window);
3929 free(wline);
3930 wline = NULL;
3931 if (width < view->ncols - 1)
3932 waddch(view->window, '\n');
3933 if (--limit <= 0)
3934 return NULL;
3935 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3936 if (err)
3937 return err;
3938 waddwstr(view->window, wline);
3939 free(wline);
3940 wline = NULL;
3941 if (width < view->ncols - 1)
3942 waddch(view->window, '\n');
3943 if (--limit <= 0)
3944 return NULL;
3945 waddch(view->window, '\n');
3946 if (--limit <= 0)
3947 return NULL;
3949 te = SIMPLEQ_FIRST(&entries->head);
3950 if (*first_displayed_entry == NULL) {
3951 if (selected == 0) {
3952 if (view->focussed)
3953 wstandout(view->window);
3954 *selected_entry = NULL;
3956 waddstr(view->window, " ..\n"); /* parent directory */
3957 if (selected == 0 && view->focussed)
3958 wstandend(view->window);
3959 (*ndisplayed)++;
3960 if (--limit <= 0)
3961 return NULL;
3962 n = 1;
3963 } else {
3964 n = 0;
3965 while (te != *first_displayed_entry)
3966 te = SIMPLEQ_NEXT(te, entry);
3969 while (te) {
3970 char *line = NULL, *id_str = NULL;
3971 const char *modestr = "";
3973 if (show_ids) {
3974 err = got_object_id_str(&id_str, te->id);
3975 if (err)
3976 return got_error_from_errno(
3977 "got_object_id_str");
3979 if (got_object_tree_entry_is_submodule(te))
3980 modestr = "$";
3981 else if (S_ISLNK(te->mode))
3982 modestr = "@";
3983 else if (S_ISDIR(te->mode))
3984 modestr = "/";
3985 else if (te->mode & S_IXUSR)
3986 modestr = "*";
3987 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3988 te->name, modestr) == -1) {
3989 free(id_str);
3990 return got_error_from_errno("asprintf");
3992 free(id_str);
3993 err = format_line(&wline, &width, line, view->ncols, 0);
3994 if (err) {
3995 free(line);
3996 break;
3998 if (n == selected) {
3999 if (view->focussed)
4000 wstandout(view->window);
4001 *selected_entry = te;
4003 waddwstr(view->window, wline);
4004 if (width < view->ncols - 1)
4005 waddch(view->window, '\n');
4006 if (n == selected && view->focussed)
4007 wstandend(view->window);
4008 free(line);
4009 free(wline);
4010 wline = NULL;
4011 n++;
4012 (*ndisplayed)++;
4013 *last_displayed_entry = te;
4014 if (--limit <= 0)
4015 break;
4016 te = SIMPLEQ_NEXT(te, entry);
4019 return err;
4022 static void
4023 tree_scroll_up(struct tog_view *view,
4024 struct got_tree_entry **first_displayed_entry, int maxscroll,
4025 const struct got_tree_entries *entries, int isroot)
4027 struct got_tree_entry *te, *prev;
4028 int i;
4030 if (*first_displayed_entry == NULL)
4031 return;
4033 te = SIMPLEQ_FIRST(&entries->head);
4034 if (*first_displayed_entry == te) {
4035 if (!isroot)
4036 *first_displayed_entry = NULL;
4037 return;
4040 /* XXX this is stupid... switch to TAILQ? */
4041 for (i = 0; i < maxscroll; i++) {
4042 while (te != *first_displayed_entry) {
4043 prev = te;
4044 te = SIMPLEQ_NEXT(te, entry);
4046 *first_displayed_entry = prev;
4047 te = SIMPLEQ_FIRST(&entries->head);
4049 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4050 *first_displayed_entry = NULL;
4053 static int
4054 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4055 struct got_tree_entry *last_displayed_entry,
4056 const struct got_tree_entries *entries)
4058 struct got_tree_entry *next, *last;
4059 int n = 0;
4061 if (*first_displayed_entry)
4062 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4063 else
4064 next = SIMPLEQ_FIRST(&entries->head);
4065 last = last_displayed_entry;
4066 while (next && last && n++ < maxscroll) {
4067 last = SIMPLEQ_NEXT(last, entry);
4068 if (last) {
4069 *first_displayed_entry = next;
4070 next = SIMPLEQ_NEXT(next, entry);
4073 return n;
4076 static const struct got_error *
4077 tree_entry_path(char **path, struct tog_parent_trees *parents,
4078 struct got_tree_entry *te)
4080 const struct got_error *err = NULL;
4081 struct tog_parent_tree *pt;
4082 size_t len = 2; /* for leading slash and NUL */
4084 TAILQ_FOREACH(pt, parents, entry)
4085 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4086 if (te)
4087 len += strlen(te->name);
4089 *path = calloc(1, len);
4090 if (path == NULL)
4091 return got_error_from_errno("calloc");
4093 (*path)[0] = '/';
4094 pt = TAILQ_LAST(parents, tog_parent_trees);
4095 while (pt) {
4096 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4097 err = got_error(GOT_ERR_NO_SPACE);
4098 goto done;
4100 if (strlcat(*path, "/", len) >= len) {
4101 err = got_error(GOT_ERR_NO_SPACE);
4102 goto done;
4104 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4106 if (te) {
4107 if (strlcat(*path, te->name, len) >= len) {
4108 err = got_error(GOT_ERR_NO_SPACE);
4109 goto done;
4112 done:
4113 if (err) {
4114 free(*path);
4115 *path = NULL;
4117 return err;
4120 static const struct got_error *
4121 blame_tree_entry(struct tog_view **new_view, int begin_x,
4122 struct got_tree_entry *te, struct tog_parent_trees *parents,
4123 struct got_object_id *commit_id, struct got_reflist_head *refs,
4124 struct got_repository *repo)
4126 const struct got_error *err = NULL;
4127 char *path;
4128 struct tog_view *blame_view;
4130 *new_view = NULL;
4132 err = tree_entry_path(&path, parents, te);
4133 if (err)
4134 return err;
4136 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4137 if (blame_view == NULL) {
4138 err = got_error_from_errno("view_open");
4139 goto done;
4142 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4143 if (err) {
4144 if (err->code == GOT_ERR_CANCELLED)
4145 err = NULL;
4146 view_close(blame_view);
4147 } else
4148 *new_view = blame_view;
4149 done:
4150 free(path);
4151 return err;
4154 static const struct got_error *
4155 log_tree_entry(struct tog_view **new_view, int begin_x,
4156 struct got_tree_entry *te, struct tog_parent_trees *parents,
4157 struct got_object_id *commit_id, struct got_reflist_head *refs,
4158 struct got_repository *repo)
4160 struct tog_view *log_view;
4161 const struct got_error *err = NULL;
4162 char *path;
4164 *new_view = NULL;
4166 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4167 if (log_view == NULL)
4168 return got_error_from_errno("view_open");
4170 err = tree_entry_path(&path, parents, te);
4171 if (err)
4172 return err;
4174 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4175 if (err)
4176 view_close(log_view);
4177 else
4178 *new_view = log_view;
4179 free(path);
4180 return err;
4183 static const struct got_error *
4184 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4185 struct got_object_id *commit_id, struct got_reflist_head *refs,
4186 struct got_repository *repo)
4188 const struct got_error *err = NULL;
4189 char *commit_id_str = NULL;
4190 struct tog_tree_view_state *s = &view->state.tree;
4192 TAILQ_INIT(&s->parents);
4194 err = got_object_id_str(&commit_id_str, commit_id);
4195 if (err != NULL)
4196 goto done;
4198 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4199 err = got_error_from_errno("asprintf");
4200 goto done;
4203 s->root = s->tree = root;
4204 s->entries = got_object_tree_get_entries(root);
4205 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4206 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4207 s->commit_id = got_object_id_dup(commit_id);
4208 if (s->commit_id == NULL) {
4209 err = got_error_from_errno("got_object_id_dup");
4210 goto done;
4212 s->refs = refs;
4213 s->repo = repo;
4215 view->show = show_tree_view;
4216 view->input = input_tree_view;
4217 view->close = close_tree_view;
4218 view->search_start = search_start_tree_view;
4219 view->search_next = search_next_tree_view;
4220 done:
4221 free(commit_id_str);
4222 if (err) {
4223 free(s->tree_label);
4224 s->tree_label = NULL;
4226 return err;
4229 static const struct got_error *
4230 close_tree_view(struct tog_view *view)
4232 struct tog_tree_view_state *s = &view->state.tree;
4234 free(s->tree_label);
4235 s->tree_label = NULL;
4236 free(s->commit_id);
4237 s->commit_id = NULL;
4238 while (!TAILQ_EMPTY(&s->parents)) {
4239 struct tog_parent_tree *parent;
4240 parent = TAILQ_FIRST(&s->parents);
4241 TAILQ_REMOVE(&s->parents, parent, entry);
4242 free(parent);
4245 if (s->tree != s->root)
4246 got_object_tree_close(s->tree);
4247 got_object_tree_close(s->root);
4249 return NULL;
4252 static const struct got_error *
4253 search_start_tree_view(struct tog_view *view)
4255 struct tog_tree_view_state *s = &view->state.tree;
4257 s->matched_entry = NULL;
4258 return NULL;
4261 static int
4262 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4264 regmatch_t regmatch;
4266 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4269 static const struct got_error *
4270 search_next_tree_view(struct tog_view *view)
4272 struct tog_tree_view_state *s = &view->state.tree;
4273 struct got_tree_entry *entry = NULL, *te;
4275 if (!view->searching) {
4276 view->search_next_done = 1;
4277 return NULL;
4280 if (s->matched_entry) {
4281 if (view->searching == TOG_SEARCH_FORWARD) {
4282 if (s->selected_entry)
4283 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4284 else
4285 entry = SIMPLEQ_FIRST(&s->entries->head);
4287 else {
4288 if (s->selected_entry == NULL) {
4289 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4290 entry = te;
4291 } else {
4292 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4293 entry = te;
4294 if (SIMPLEQ_NEXT(te, entry) ==
4295 s->selected_entry)
4296 break;
4300 } else {
4301 if (view->searching == TOG_SEARCH_FORWARD)
4302 entry = SIMPLEQ_FIRST(&s->entries->head);
4303 else {
4304 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4305 entry = te;
4309 while (1) {
4310 if (entry == NULL) {
4311 if (s->matched_entry == NULL) {
4312 view->search_next_done = 1;
4313 return NULL;
4315 if (view->searching == TOG_SEARCH_FORWARD)
4316 entry = SIMPLEQ_FIRST(&s->entries->head);
4317 else {
4318 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4319 entry = te;
4323 if (match_tree_entry(entry, &view->regex)) {
4324 view->search_next_done = 1;
4325 s->matched_entry = entry;
4326 break;
4329 if (view->searching == TOG_SEARCH_FORWARD)
4330 entry = SIMPLEQ_NEXT(entry, entry);
4331 else {
4332 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4333 entry = NULL;
4334 else {
4335 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4336 if (SIMPLEQ_NEXT(te, entry) == entry) {
4337 entry = te;
4338 break;
4345 if (s->matched_entry) {
4346 s->first_displayed_entry = s->matched_entry;
4347 s->selected = 0;
4350 return NULL;
4353 static const struct got_error *
4354 show_tree_view(struct tog_view *view)
4356 const struct got_error *err = NULL;
4357 struct tog_tree_view_state *s = &view->state.tree;
4358 char *parent_path;
4360 err = tree_entry_path(&parent_path, &s->parents, NULL);
4361 if (err)
4362 return err;
4364 err = draw_tree_entries(view, &s->first_displayed_entry,
4365 &s->last_displayed_entry, &s->selected_entry,
4366 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4367 s->entries, s->selected, view->nlines, s->tree == s->root);
4368 free(parent_path);
4370 view_vborder(view);
4371 return err;
4374 static const struct got_error *
4375 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4376 struct tog_view **focus_view, struct tog_view *view, int ch)
4378 const struct got_error *err = NULL;
4379 struct tog_tree_view_state *s = &view->state.tree;
4380 struct tog_view *log_view;
4381 int begin_x = 0, nscrolled;
4383 switch (ch) {
4384 case 'i':
4385 s->show_ids = !s->show_ids;
4386 break;
4387 case 'l':
4388 if (!s->selected_entry)
4389 break;
4390 if (view_is_parent_view(view))
4391 begin_x = view_split_begin_x(view->begin_x);
4392 err = log_tree_entry(&log_view, begin_x,
4393 s->selected_entry, &s->parents,
4394 s->commit_id, s->refs, s->repo);
4395 if (view_is_parent_view(view)) {
4396 err = view_close_child(view);
4397 if (err)
4398 return err;
4399 err = view_set_child(view, log_view);
4400 if (err) {
4401 view_close(log_view);
4402 break;
4404 *focus_view = log_view;
4405 view->child_focussed = 1;
4406 } else
4407 *new_view = log_view;
4408 break;
4409 case 'k':
4410 case KEY_UP:
4411 if (s->selected > 0) {
4412 s->selected--;
4413 if (s->selected == 0)
4414 break;
4416 if (s->selected > 0)
4417 break;
4418 tree_scroll_up(view, &s->first_displayed_entry, 1,
4419 s->entries, s->tree == s->root);
4420 break;
4421 case KEY_PPAGE:
4422 tree_scroll_up(view, &s->first_displayed_entry,
4423 MAX(0, view->nlines - 4 - s->selected), s->entries,
4424 s->tree == s->root);
4425 s->selected = 0;
4426 if (SIMPLEQ_FIRST(&s->entries->head) ==
4427 s->first_displayed_entry && s->tree != s->root)
4428 s->first_displayed_entry = NULL;
4429 break;
4430 case 'j':
4431 case KEY_DOWN:
4432 if (s->selected < s->ndisplayed - 1) {
4433 s->selected++;
4434 break;
4436 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4437 /* can't scroll any further */
4438 break;
4439 tree_scroll_down(&s->first_displayed_entry, 1,
4440 s->last_displayed_entry, s->entries);
4441 break;
4442 case KEY_NPAGE:
4443 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4444 == NULL) {
4445 /* can't scroll any further; move cursor down */
4446 if (s->selected < s->ndisplayed - 1)
4447 s->selected = s->ndisplayed - 1;
4448 break;
4450 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4451 view->nlines, s->last_displayed_entry, s->entries);
4452 if (nscrolled < view->nlines) {
4453 int ndisplayed = 0;
4454 struct got_tree_entry *te;
4455 te = s->first_displayed_entry;
4456 do {
4457 ndisplayed++;
4458 te = SIMPLEQ_NEXT(te, entry);
4459 } while (te);
4460 s->selected = ndisplayed - 1;
4462 break;
4463 case KEY_ENTER:
4464 case '\r':
4465 case KEY_BACKSPACE:
4466 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4467 struct tog_parent_tree *parent;
4468 /* user selected '..' */
4469 if (s->tree == s->root)
4470 break;
4471 parent = TAILQ_FIRST(&s->parents);
4472 TAILQ_REMOVE(&s->parents, parent,
4473 entry);
4474 got_object_tree_close(s->tree);
4475 s->tree = parent->tree;
4476 s->entries =
4477 got_object_tree_get_entries(s->tree);
4478 s->first_displayed_entry =
4479 parent->first_displayed_entry;
4480 s->selected_entry =
4481 parent->selected_entry;
4482 s->selected = parent->selected;
4483 free(parent);
4484 } else if (S_ISDIR(s->selected_entry->mode)) {
4485 struct got_tree_object *subtree;
4486 err = got_object_open_as_tree(&subtree,
4487 s->repo, s->selected_entry->id);
4488 if (err)
4489 break;
4490 err = tree_view_visit_subtree(subtree, s);
4491 if (err) {
4492 got_object_tree_close(subtree);
4493 break;
4495 } else if (S_ISREG(s->selected_entry->mode)) {
4496 struct tog_view *blame_view;
4497 int begin_x = view_is_parent_view(view) ?
4498 view_split_begin_x(view->begin_x) : 0;
4500 err = blame_tree_entry(&blame_view, begin_x,
4501 s->selected_entry, &s->parents,
4502 s->commit_id, s->refs, s->repo);
4503 if (err)
4504 break;
4505 if (view_is_parent_view(view)) {
4506 err = view_close_child(view);
4507 if (err)
4508 return err;
4509 err = view_set_child(view, blame_view);
4510 if (err) {
4511 view_close(blame_view);
4512 break;
4514 *focus_view = blame_view;
4515 view->child_focussed = 1;
4516 } else
4517 *new_view = blame_view;
4519 break;
4520 case KEY_RESIZE:
4521 if (s->selected > view->nlines)
4522 s->selected = s->ndisplayed - 1;
4523 break;
4524 default:
4525 break;
4528 return err;
4531 __dead static void
4532 usage_tree(void)
4534 endwin();
4535 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4536 getprogname());
4537 exit(1);
4540 static const struct got_error *
4541 cmd_tree(int argc, char *argv[])
4543 const struct got_error *error;
4544 struct got_repository *repo = NULL;
4545 struct got_reflist_head refs;
4546 char *repo_path = NULL;
4547 struct got_object_id *commit_id = NULL;
4548 char *commit_id_arg = NULL;
4549 struct got_commit_object *commit = NULL;
4550 struct got_tree_object *tree = NULL;
4551 int ch;
4552 struct tog_view *view;
4554 SIMPLEQ_INIT(&refs);
4556 #ifndef PROFILE
4557 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4558 NULL) == -1)
4559 err(1, "pledge");
4560 #endif
4562 while ((ch = getopt(argc, argv, "c:")) != -1) {
4563 switch (ch) {
4564 case 'c':
4565 commit_id_arg = optarg;
4566 break;
4567 default:
4568 usage_tree();
4569 /* NOTREACHED */
4573 argc -= optind;
4574 argv += optind;
4576 if (argc == 0) {
4577 struct got_worktree *worktree;
4578 char *cwd = getcwd(NULL, 0);
4579 if (cwd == NULL)
4580 return got_error_from_errno("getcwd");
4581 error = got_worktree_open(&worktree, cwd);
4582 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4583 goto done;
4584 if (worktree) {
4585 free(cwd);
4586 repo_path =
4587 strdup(got_worktree_get_repo_path(worktree));
4588 got_worktree_close(worktree);
4589 } else
4590 repo_path = cwd;
4591 if (repo_path == NULL) {
4592 error = got_error_from_errno("strdup");
4593 goto done;
4595 } else if (argc == 1) {
4596 repo_path = realpath(argv[0], NULL);
4597 if (repo_path == NULL)
4598 return got_error_from_errno2("realpath", argv[0]);
4599 } else
4600 usage_log();
4602 init_curses();
4604 error = got_repo_open(&repo, repo_path, NULL);
4605 if (error != NULL)
4606 goto done;
4608 error = apply_unveil(got_repo_get_path(repo), NULL);
4609 if (error)
4610 goto done;
4612 if (commit_id_arg == NULL)
4613 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4614 else {
4615 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4616 if (error) {
4617 if (error->code != GOT_ERR_NOT_REF)
4618 goto done;
4619 error = got_repo_match_object_id_prefix(&commit_id,
4620 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4623 if (error != NULL)
4624 goto done;
4626 error = got_object_open_as_commit(&commit, repo, commit_id);
4627 if (error != NULL)
4628 goto done;
4630 error = got_object_open_as_tree(&tree, repo,
4631 got_object_commit_get_tree_id(commit));
4632 if (error != NULL)
4633 goto done;
4635 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4636 if (error)
4637 goto done;
4639 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4640 if (view == NULL) {
4641 error = got_error_from_errno("view_open");
4642 goto done;
4644 error = open_tree_view(view, tree, commit_id, &refs, repo);
4645 if (error)
4646 goto done;
4647 error = view_loop(view);
4648 done:
4649 free(repo_path);
4650 free(commit_id);
4651 if (commit)
4652 got_object_commit_close(commit);
4653 if (tree)
4654 got_object_tree_close(tree);
4655 if (repo)
4656 got_repo_close(repo);
4657 got_ref_list_free(&refs);
4658 return error;
4661 static void
4662 list_commands(void)
4664 int i;
4666 fprintf(stderr, "commands:");
4667 for (i = 0; i < nitems(tog_commands); i++) {
4668 struct tog_cmd *cmd = &tog_commands[i];
4669 fprintf(stderr, " %s", cmd->name);
4671 fputc('\n', stderr);
4674 __dead static void
4675 usage(int hflag)
4677 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4678 getprogname());
4679 if (hflag)
4680 list_commands();
4681 exit(1);
4684 static char **
4685 make_argv(const char *arg0, const char *arg1)
4687 char **argv;
4688 int argc = (arg1 == NULL ? 1 : 2);
4690 argv = calloc(argc, sizeof(char *));
4691 if (argv == NULL)
4692 err(1, "calloc");
4693 argv[0] = strdup(arg0);
4694 if (argv[0] == NULL)
4695 err(1, "strdup");
4696 if (arg1) {
4697 argv[1] = strdup(arg1);
4698 if (argv[1] == NULL)
4699 err(1, "strdup");
4702 return argv;
4705 int
4706 main(int argc, char *argv[])
4708 const struct got_error *error = NULL;
4709 struct tog_cmd *cmd = NULL;
4710 int ch, hflag = 0, Vflag = 0;
4711 char **cmd_argv = NULL;
4713 setlocale(LC_CTYPE, "");
4715 while ((ch = getopt(argc, argv, "hV")) != -1) {
4716 switch (ch) {
4717 case 'h':
4718 hflag = 1;
4719 break;
4720 case 'V':
4721 Vflag = 1;
4722 break;
4723 default:
4724 usage(hflag);
4725 /* NOTREACHED */
4729 argc -= optind;
4730 argv += optind;
4731 optind = 0;
4732 optreset = 1;
4734 if (Vflag) {
4735 got_version_print_str();
4736 return 1;
4739 if (argc == 0) {
4740 if (hflag)
4741 usage(hflag);
4742 /* Build an argument vector which runs a default command. */
4743 cmd = &tog_commands[0];
4744 cmd_argv = make_argv(cmd->name, NULL);
4745 argc = 1;
4746 } else {
4747 int i;
4749 /* Did the user specific a command? */
4750 for (i = 0; i < nitems(tog_commands); i++) {
4751 if (strncmp(tog_commands[i].name, argv[0],
4752 strlen(argv[0])) == 0) {
4753 cmd = &tog_commands[i];
4754 break;
4758 if (cmd == NULL) {
4759 fprintf(stderr, "%s: unknown command '%s'\n",
4760 getprogname(), argv[0]);
4761 list_commands();
4762 return 1;
4766 if (hflag)
4767 cmd->cmd_usage();
4768 else
4769 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4771 endwin();
4772 free(cmd_argv);
4773 if (error && error->code != GOT_ERR_CANCELLED)
4774 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4775 return 0;