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)
932 const struct got_error *err = NULL;
933 int cols = 0;
934 wchar_t *wline = NULL;
935 size_t wlen;
936 int i;
938 *wlinep = NULL;
939 *widthp = 0;
941 err = mbs2ws(&wline, &wlen, line);
942 if (err)
943 return err;
945 i = 0;
946 while (i < wlen && cols < wlimit) {
947 int width = wcwidth(wline[i]);
948 switch (width) {
949 case 0:
950 i++;
951 break;
952 case 1:
953 case 2:
954 if (cols + width <= wlimit)
955 cols += width;
956 i++;
957 break;
958 case -1:
959 if (wline[i] == L'\t')
960 cols += TABSIZE - ((cols + 1) % TABSIZE);
961 i++;
962 break;
963 default:
964 err = got_error_from_errno("wcwidth");
965 goto done;
968 wline[i] = L'\0';
969 if (widthp)
970 *widthp = cols;
971 done:
972 if (err)
973 free(wline);
974 else
975 *wlinep = wline;
976 return err;
979 static const struct got_error*
980 build_refs_str(char **refs_str, struct got_reflist_head *refs,
981 struct got_object_id *id, struct got_repository *repo)
983 static const struct got_error *err = NULL;
984 struct got_reflist_entry *re;
985 char *s;
986 const char *name;
988 *refs_str = NULL;
990 SIMPLEQ_FOREACH(re, refs, entry) {
991 struct got_tag_object *tag = NULL;
992 int cmp;
994 name = got_ref_get_name(re->ref);
995 if (strcmp(name, GOT_REF_HEAD) == 0)
996 continue;
997 if (strncmp(name, "refs/", 5) == 0)
998 name += 5;
999 if (strncmp(name, "got/", 4) == 0)
1000 continue;
1001 if (strncmp(name, "heads/", 6) == 0)
1002 name += 6;
1003 if (strncmp(name, "remotes/", 8) == 0)
1004 name += 8;
1005 if (strncmp(name, "tags/", 5) == 0) {
1006 err = got_object_open_as_tag(&tag, repo, re->id);
1007 if (err) {
1008 if (err->code != GOT_ERR_OBJ_TYPE)
1009 break;
1010 /* Ref points at something other than a tag. */
1011 err = NULL;
1012 tag = NULL;
1015 cmp = got_object_id_cmp(tag ?
1016 got_object_tag_get_object_id(tag) : re->id, id);
1017 if (tag)
1018 got_object_tag_close(tag);
1019 if (cmp != 0)
1020 continue;
1021 s = *refs_str;
1022 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1023 s ? ", " : "", name) == -1) {
1024 err = got_error_from_errno("asprintf");
1025 free(s);
1026 *refs_str = NULL;
1027 break;
1029 free(s);
1032 return err;
1035 static const struct got_error *
1036 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1038 char *smallerthan, *at;
1040 smallerthan = strchr(author, '<');
1041 if (smallerthan && smallerthan[1] != '\0')
1042 author = smallerthan + 1;
1043 at = strchr(author, '@');
1044 if (at)
1045 *at = '\0';
1046 return format_line(wauthor, author_width, author, limit);
1049 static const struct got_error *
1050 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1051 struct got_object_id *id, struct got_reflist_head *refs,
1052 int author_display_cols)
1054 const struct got_error *err = NULL;
1055 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1056 char *logmsg0 = NULL, *logmsg = NULL;
1057 char *author = NULL;
1058 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1059 int author_width, logmsg_width;
1060 char *newline, *line = NULL;
1061 int col, limit;
1062 static const size_t date_display_cols = 9;
1063 const int avail = view->ncols;
1064 struct tm tm;
1065 time_t committer_time;
1067 committer_time = got_object_commit_get_committer_time(commit);
1068 if (localtime_r(&committer_time, &tm) == NULL)
1069 return got_error_from_errno("localtime_r");
1070 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1071 >= sizeof(datebuf))
1072 return got_error(GOT_ERR_NO_SPACE);
1074 if (avail < date_display_cols)
1075 limit = MIN(sizeof(datebuf) - 1, avail);
1076 else
1077 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1078 waddnstr(view->window, datebuf, limit);
1079 col = limit + 1;
1080 if (col > avail)
1081 goto done;
1083 author = strdup(got_object_commit_get_author(commit));
1084 if (author == NULL) {
1085 err = got_error_from_errno("strdup");
1086 goto done;
1088 err = format_author(&wauthor, &author_width, author, avail - col);
1089 if (err)
1090 goto done;
1091 waddwstr(view->window, wauthor);
1092 col += author_width;
1093 while (col <= avail && author_width < author_display_cols + 2) {
1094 waddch(view->window, ' ');
1095 col++;
1096 author_width++;
1098 if (col > avail)
1099 goto done;
1101 err = got_object_commit_get_logmsg(&logmsg0, commit);
1102 if (err)
1103 goto done;
1104 logmsg = logmsg0;
1105 while (*logmsg == '\n')
1106 logmsg++;
1107 newline = strchr(logmsg, '\n');
1108 if (newline)
1109 *newline = '\0';
1110 limit = avail - col;
1111 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1112 if (err)
1113 goto done;
1114 waddwstr(view->window, wlogmsg);
1115 col += logmsg_width;
1116 while (col <= avail) {
1117 waddch(view->window, ' ');
1118 col++;
1120 done:
1121 free(logmsg0);
1122 free(wlogmsg);
1123 free(author);
1124 free(wauthor);
1125 free(line);
1126 return err;
1129 static struct commit_queue_entry *
1130 alloc_commit_queue_entry(struct got_commit_object *commit,
1131 struct got_object_id *id)
1133 struct commit_queue_entry *entry;
1135 entry = calloc(1, sizeof(*entry));
1136 if (entry == NULL)
1137 return NULL;
1139 entry->id = id;
1140 entry->commit = commit;
1141 return entry;
1144 static void
1145 pop_commit(struct commit_queue *commits)
1147 struct commit_queue_entry *entry;
1149 entry = TAILQ_FIRST(&commits->head);
1150 TAILQ_REMOVE(&commits->head, entry, entry);
1151 got_object_commit_close(entry->commit);
1152 commits->ncommits--;
1153 /* Don't free entry->id! It is owned by the commit graph. */
1154 free(entry);
1157 static void
1158 free_commits(struct commit_queue *commits)
1160 while (!TAILQ_EMPTY(&commits->head))
1161 pop_commit(commits);
1164 static const struct got_error *
1165 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1166 int minqueue, struct got_repository *repo, const char *path)
1168 const struct got_error *err = NULL;
1169 int nqueued = 0;
1172 * We keep all commits open throughout the lifetime of the log
1173 * view in order to avoid having to re-fetch commits from disk
1174 * while updating the display.
1176 while (nqueued < minqueue) {
1177 struct got_object_id *id;
1178 struct got_commit_object *commit;
1179 struct commit_queue_entry *entry;
1180 int errcode;
1182 err = got_commit_graph_iter_next(&id, graph);
1183 if (err) {
1184 if (err->code != GOT_ERR_ITER_NEED_MORE)
1185 break;
1186 err = got_commit_graph_fetch_commits(graph,
1187 minqueue, repo, NULL, NULL);
1188 if (err)
1189 return err;
1190 continue;
1193 if (id == NULL)
1194 break;
1196 err = got_object_open_as_commit(&commit, repo, id);
1197 if (err)
1198 break;
1199 entry = alloc_commit_queue_entry(commit, id);
1200 if (entry == NULL) {
1201 err = got_error_from_errno("alloc_commit_queue_entry");
1202 break;
1205 errcode = pthread_mutex_lock(&tog_mutex);
1206 if (errcode) {
1207 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1208 break;
1211 entry->idx = commits->ncommits;
1212 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1213 nqueued++;
1214 commits->ncommits++;
1216 errcode = pthread_mutex_unlock(&tog_mutex);
1217 if (errcode && err == NULL)
1218 err = got_error_set_errno(errcode,
1219 "pthread_mutex_unlock");
1222 return err;
1225 static const struct got_error *
1226 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1227 struct got_repository *repo)
1229 const struct got_error *err = NULL;
1230 struct got_reference *head_ref;
1232 *head_id = NULL;
1234 err = got_ref_open(&head_ref, repo, branch_name, 0);
1235 if (err)
1236 return err;
1238 err = got_ref_resolve(head_id, repo, head_ref);
1239 got_ref_close(head_ref);
1240 if (err) {
1241 *head_id = NULL;
1242 return err;
1245 return NULL;
1248 static const struct got_error *
1249 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1250 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1251 struct commit_queue *commits, int selected_idx, int limit,
1252 struct got_reflist_head *refs, const char *path, int commits_needed)
1254 const struct got_error *err = NULL;
1255 struct tog_log_view_state *s = &view->state.log;
1256 struct commit_queue_entry *entry;
1257 int width;
1258 int ncommits, author_cols = 10;
1259 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1260 char *refs_str = NULL;
1261 wchar_t *wline;
1263 entry = first;
1264 ncommits = 0;
1265 while (entry) {
1266 if (ncommits == selected_idx) {
1267 *selected = entry;
1268 break;
1270 entry = TAILQ_NEXT(entry, entry);
1271 ncommits++;
1274 if (*selected && !(view->searching && view->search_next_done == 0)) {
1275 err = got_object_id_str(&id_str, (*selected)->id);
1276 if (err)
1277 return err;
1278 if (refs) {
1279 err = build_refs_str(&refs_str, refs, (*selected)->id,
1280 s->repo);
1281 if (err)
1282 goto done;
1286 if (commits_needed == 0)
1287 halfdelay(10); /* disable fast refresh */
1289 if (asprintf(&ncommits_str, " [%d/%d] %s",
1290 entry ? entry->idx + 1 : 0, commits->ncommits,
1291 commits_needed > 0 ?
1292 (view->searching && view->search_next_done == 0
1293 ? "searching..." : "loading... ") :
1294 (refs_str ? refs_str : "")) == -1) {
1295 err = got_error_from_errno("asprintf");
1296 goto done;
1299 if (path && strcmp(path, "/") != 0) {
1300 if (asprintf(&header, "commit %s %s%s",
1301 id_str ? id_str : "........................................",
1302 path, ncommits_str) == -1) {
1303 err = got_error_from_errno("asprintf");
1304 header = NULL;
1305 goto done;
1307 } else if (asprintf(&header, "commit %s%s",
1308 id_str ? id_str : "........................................",
1309 ncommits_str) == -1) {
1310 err = got_error_from_errno("asprintf");
1311 header = NULL;
1312 goto done;
1314 err = format_line(&wline, &width, header, view->ncols);
1315 if (err)
1316 goto done;
1318 werase(view->window);
1320 if (view_needs_focus_indication(view))
1321 wstandout(view->window);
1322 waddwstr(view->window, wline);
1323 while (width < view->ncols) {
1324 waddch(view->window, ' ');
1325 width++;
1327 if (view_needs_focus_indication(view))
1328 wstandend(view->window);
1329 free(wline);
1330 if (limit <= 1)
1331 goto done;
1333 /* Grow author column size if necessary. */
1334 entry = first;
1335 ncommits = 0;
1336 while (entry) {
1337 char *author;
1338 wchar_t *wauthor;
1339 int width;
1340 if (ncommits >= limit - 1)
1341 break;
1342 author = strdup(got_object_commit_get_author(entry->commit));
1343 if (author == NULL) {
1344 err = got_error_from_errno("strdup");
1345 goto done;
1347 err = format_author(&wauthor, &width, author, COLS);
1348 if (author_cols < width)
1349 author_cols = width;
1350 free(wauthor);
1351 free(author);
1352 entry = TAILQ_NEXT(entry, entry);
1355 entry = first;
1356 *last = first;
1357 ncommits = 0;
1358 while (entry) {
1359 if (ncommits >= limit - 1)
1360 break;
1361 if (ncommits == selected_idx)
1362 wstandout(view->window);
1363 err = draw_commit(view, entry->commit, entry->id, refs,
1364 author_cols);
1365 if (ncommits == selected_idx)
1366 wstandend(view->window);
1367 if (err)
1368 goto done;
1369 ncommits++;
1370 *last = entry;
1371 entry = TAILQ_NEXT(entry, entry);
1374 view_vborder(view);
1375 done:
1376 free(id_str);
1377 free(refs_str);
1378 free(ncommits_str);
1379 free(header);
1380 return err;
1383 static void
1384 scroll_up(struct tog_view *view,
1385 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1386 struct commit_queue *commits)
1388 struct commit_queue_entry *entry;
1389 int nscrolled = 0;
1391 entry = TAILQ_FIRST(&commits->head);
1392 if (*first_displayed_entry == entry)
1393 return;
1395 entry = *first_displayed_entry;
1396 while (entry && nscrolled < maxscroll) {
1397 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1398 if (entry) {
1399 *first_displayed_entry = entry;
1400 nscrolled++;
1405 static const struct got_error *
1406 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1407 pthread_cond_t *need_commits)
1409 int errcode;
1410 int max_wait = 20;
1412 halfdelay(1); /* fast refresh while loading commits */
1414 while (*commits_needed > 0) {
1415 if (*log_complete)
1416 break;
1418 /* Wake the log thread. */
1419 errcode = pthread_cond_signal(need_commits);
1420 if (errcode)
1421 return got_error_set_errno(errcode,
1422 "pthread_cond_signal");
1423 errcode = pthread_mutex_unlock(&tog_mutex);
1424 if (errcode)
1425 return got_error_set_errno(errcode,
1426 "pthread_mutex_unlock");
1427 pthread_yield();
1428 errcode = pthread_mutex_lock(&tog_mutex);
1429 if (errcode)
1430 return got_error_set_errno(errcode,
1431 "pthread_mutex_lock");
1433 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1435 * Thread is not done yet; lose a key press
1436 * and let the user retry... this way the GUI
1437 * remains interactive while logging deep paths
1438 * with few commits in history.
1440 return NULL;
1444 return NULL;
1447 static const struct got_error *
1448 scroll_down(struct tog_view *view,
1449 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1450 struct commit_queue_entry **last_displayed_entry,
1451 struct commit_queue *commits, int *log_complete, int *commits_needed,
1452 pthread_cond_t *need_commits)
1454 const struct got_error *err = NULL;
1455 struct commit_queue_entry *pentry;
1456 int nscrolled = 0;
1458 if (*last_displayed_entry == NULL)
1459 return NULL;
1461 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1462 if (pentry == NULL && !*log_complete) {
1464 * Ask the log thread for required amount of commits
1465 * plus some amount of pre-fetching.
1467 (*commits_needed) += maxscroll + 20;
1468 err = trigger_log_thread(0, commits_needed, log_complete,
1469 need_commits);
1470 if (err)
1471 return err;
1474 do {
1475 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1476 if (pentry == NULL)
1477 break;
1479 *last_displayed_entry = pentry;
1481 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1482 if (pentry == NULL)
1483 break;
1484 *first_displayed_entry = pentry;
1485 } while (++nscrolled < maxscroll);
1487 return err;
1490 static const struct got_error *
1491 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1492 struct got_commit_object *commit, struct got_object_id *commit_id,
1493 struct tog_view *log_view, struct got_reflist_head *refs,
1494 struct got_repository *repo)
1496 const struct got_error *err;
1497 struct got_object_qid *parent_id;
1498 struct tog_view *diff_view;
1500 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1501 if (diff_view == NULL)
1502 return got_error_from_errno("view_open");
1504 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1505 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1506 commit_id, log_view, refs, repo);
1507 if (err == NULL)
1508 *new_view = diff_view;
1509 return err;
1512 static const struct got_error *
1513 tree_view_visit_subtree(struct got_tree_object *subtree,
1514 struct tog_tree_view_state *s)
1516 struct tog_parent_tree *parent;
1518 parent = calloc(1, sizeof(*parent));
1519 if (parent == NULL)
1520 return got_error_from_errno("calloc");
1522 parent->tree = s->tree;
1523 parent->first_displayed_entry = s->first_displayed_entry;
1524 parent->selected_entry = s->selected_entry;
1525 parent->selected = s->selected;
1526 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1527 s->tree = subtree;
1528 s->entries = got_object_tree_get_entries(s->tree);
1529 s->selected = 0;
1530 s->first_displayed_entry = NULL;
1531 return NULL;
1535 static const struct got_error *
1536 browse_commit_tree(struct tog_view **new_view, int begin_x,
1537 struct commit_queue_entry *entry, const char *path,
1538 struct got_reflist_head *refs, struct got_repository *repo)
1540 const struct got_error *err = NULL;
1541 struct got_tree_object *tree;
1542 struct got_tree_entry *te;
1543 struct tog_tree_view_state *s;
1544 struct tog_view *tree_view;
1545 char *slash, *subpath = NULL;
1546 const char *p;
1548 err = got_object_open_as_tree(&tree, repo,
1549 got_object_commit_get_tree_id(entry->commit));
1550 if (err)
1551 return err;
1553 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1554 if (tree_view == NULL)
1555 return got_error_from_errno("view_open");
1557 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1558 if (err) {
1559 got_object_tree_close(tree);
1560 return err;
1562 s = &tree_view->state.tree;
1564 *new_view = tree_view;
1566 /* Walk the path and open corresponding tree objects. */
1567 p = path;
1568 while (p[0] == '/')
1569 p++;
1570 while (*p) {
1571 struct got_object_id *tree_id;
1573 /* Ensure the correct subtree entry is selected. */
1574 slash = strchr(p, '/');
1575 if (slash == NULL)
1576 slash = strchr(p, '\0');
1577 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1578 if (strncmp(p, te->name, slash - p) == 0) {
1579 s->selected_entry = te;
1580 break;
1582 s->selected++;
1584 if (s->selected_entry == NULL) {
1585 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1586 break;
1588 if (s->tree != s->root)
1589 s->selected++; /* skip '..' */
1591 if (!S_ISDIR(s->selected_entry->mode)) {
1592 /* Jump to this file's entry. */
1593 s->first_displayed_entry = s->selected_entry;
1594 s->selected = 0;
1595 break;
1598 slash = strchr(p, '/');
1599 if (slash)
1600 subpath = strndup(path, slash - path);
1601 else
1602 subpath = strdup(path);
1603 if (subpath == NULL) {
1604 err = got_error_from_errno("strdup");
1605 break;
1608 err = got_object_id_by_path(&tree_id, repo, entry->id,
1609 subpath);
1610 if (err)
1611 break;
1613 err = got_object_open_as_tree(&tree, repo, tree_id);
1614 free(tree_id);
1615 if (err)
1616 break;
1618 err = tree_view_visit_subtree(tree, s);
1619 if (err) {
1620 got_object_tree_close(tree);
1621 break;
1623 if (slash == NULL)
1624 break;
1625 free(subpath);
1626 subpath = NULL;
1627 p = slash;
1630 free(subpath);
1631 return err;
1634 static void *
1635 log_thread(void *arg)
1637 const struct got_error *err = NULL;
1638 int errcode = 0;
1639 struct tog_log_thread_args *a = arg;
1640 int done = 0;
1642 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1643 NULL, NULL);
1644 if (err)
1645 return (void *)err;
1647 while (!done && !err && !tog_sigpipe_received) {
1648 err = queue_commits(a->graph, a->commits, 1, a->repo,
1649 a->in_repo_path);
1650 if (err) {
1651 if (err->code != GOT_ERR_ITER_COMPLETED)
1652 return (void *)err;
1653 err = NULL;
1654 done = 1;
1655 } else if (a->commits_needed > 0)
1656 a->commits_needed--;
1658 errcode = pthread_mutex_lock(&tog_mutex);
1659 if (errcode) {
1660 err = got_error_set_errno(errcode,
1661 "pthread_mutex_lock");
1662 break;
1663 } else if (*a->quit)
1664 done = 1;
1665 else if (*a->first_displayed_entry == NULL) {
1666 *a->first_displayed_entry =
1667 TAILQ_FIRST(&a->commits->head);
1668 *a->selected_entry = *a->first_displayed_entry;
1671 if (done)
1672 a->commits_needed = 0;
1673 else if (a->commits_needed == 0) {
1674 errcode = pthread_cond_wait(&a->need_commits,
1675 &tog_mutex);
1676 if (errcode)
1677 err = got_error_set_errno(errcode,
1678 "pthread_cond_wait");
1681 errcode = pthread_mutex_unlock(&tog_mutex);
1682 if (errcode && err == NULL)
1683 err = got_error_set_errno(errcode,
1684 "pthread_mutex_unlock");
1686 a->log_complete = 1;
1687 return (void *)err;
1690 static const struct got_error *
1691 stop_log_thread(struct tog_log_view_state *s)
1693 const struct got_error *err = NULL;
1694 int errcode;
1696 if (s->thread) {
1697 s->quit = 1;
1698 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1699 if (errcode)
1700 return got_error_set_errno(errcode,
1701 "pthread_cond_signal");
1702 errcode = pthread_mutex_unlock(&tog_mutex);
1703 if (errcode)
1704 return got_error_set_errno(errcode,
1705 "pthread_mutex_unlock");
1706 errcode = pthread_join(s->thread, (void **)&err);
1707 if (errcode)
1708 return got_error_set_errno(errcode, "pthread_join");
1709 errcode = pthread_mutex_lock(&tog_mutex);
1710 if (errcode)
1711 return got_error_set_errno(errcode,
1712 "pthread_mutex_lock");
1713 s->thread = NULL;
1716 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1717 if (errcode && err == NULL)
1718 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1720 if (s->thread_args.repo) {
1721 got_repo_close(s->thread_args.repo);
1722 s->thread_args.repo = NULL;
1725 if (s->thread_args.graph) {
1726 got_commit_graph_close(s->thread_args.graph);
1727 s->thread_args.graph = NULL;
1730 return err;
1733 static const struct got_error *
1734 close_log_view(struct tog_view *view)
1736 const struct got_error *err = NULL;
1737 struct tog_log_view_state *s = &view->state.log;
1739 err = stop_log_thread(s);
1740 free_commits(&s->commits);
1741 free(s->in_repo_path);
1742 s->in_repo_path = NULL;
1743 free(s->start_id);
1744 s->start_id = NULL;
1745 return err;
1748 static const struct got_error *
1749 search_start_log_view(struct tog_view *view)
1751 struct tog_log_view_state *s = &view->state.log;
1753 s->matched_entry = NULL;
1754 s->search_entry = NULL;
1755 return NULL;
1758 static int
1759 match_commit(struct got_commit_object *commit, const char *id_str,
1760 const char *logmsg, regex_t *regex)
1762 regmatch_t regmatch;
1764 if (regexec(regex, got_object_commit_get_author(commit), 1,
1765 &regmatch, 0) == 0 ||
1766 regexec(regex, got_object_commit_get_committer(commit), 1,
1767 &regmatch, 0) == 0 ||
1768 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1769 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1770 return 1;
1772 return 0;
1775 static const struct got_error *
1776 search_next_log_view(struct tog_view *view)
1778 const struct got_error *err = NULL;
1779 struct tog_log_view_state *s = &view->state.log;
1780 struct commit_queue_entry *entry;
1782 if (!view->searching) {
1783 view->search_next_done = 1;
1784 return NULL;
1787 if (s->search_entry) {
1788 if (wgetch(view->window) == KEY_BACKSPACE) {
1789 view->search_next_done = 1;
1790 return NULL;
1792 if (view->searching == TOG_SEARCH_FORWARD)
1793 entry = TAILQ_NEXT(s->search_entry, entry);
1794 else
1795 entry = TAILQ_PREV(s->search_entry,
1796 commit_queue_head, entry);
1797 } else if (s->matched_entry) {
1798 if (view->searching == TOG_SEARCH_FORWARD)
1799 entry = TAILQ_NEXT(s->selected_entry, entry);
1800 else
1801 entry = TAILQ_PREV(s->selected_entry,
1802 commit_queue_head, entry);
1803 } else {
1804 if (view->searching == TOG_SEARCH_FORWARD)
1805 entry = TAILQ_FIRST(&s->commits.head);
1806 else
1807 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1810 while (1) {
1811 char *id_str, *logmsg;
1812 if (entry == NULL) {
1813 if (s->thread_args.log_complete ||
1814 view->searching == TOG_SEARCH_BACKWARD) {
1815 view->search_next_done = 1;
1816 return NULL;
1819 * Poke the log thread for more commits and return,
1820 * allowing the main loop to make progress. Search
1821 * will resume at s->search_entry once we come back.
1823 s->thread_args.commits_needed++;
1824 return trigger_log_thread(1,
1825 &s->thread_args.commits_needed,
1826 &s->thread_args.log_complete,
1827 &s->thread_args.need_commits);
1830 err = got_object_id_str(&id_str, entry->id);
1831 if (err)
1832 return err;
1834 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1835 if (err)
1836 return err;
1837 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1838 free(logmsg);
1839 view->search_next_done = 1;
1840 s->matched_entry = entry;
1841 free(id_str);
1842 break;
1844 free(logmsg);
1845 free(id_str);
1846 s->search_entry = entry;
1847 if (view->searching == TOG_SEARCH_FORWARD)
1848 entry = TAILQ_NEXT(entry, entry);
1849 else
1850 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1853 if (s->matched_entry) {
1854 int cur = s->selected_entry->idx;
1855 while (cur < s->matched_entry->idx) {
1856 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1857 if (err)
1858 return err;
1859 cur++;
1861 while (cur > s->matched_entry->idx) {
1862 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1863 if (err)
1864 return err;
1865 cur--;
1869 s->search_entry = NULL;
1871 return NULL;
1874 static const struct got_error *
1875 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1876 struct got_reflist_head *refs, struct got_repository *repo,
1877 const char *head_ref_name, const char *path, int check_disk)
1879 const struct got_error *err = NULL;
1880 struct tog_log_view_state *s = &view->state.log;
1881 struct got_repository *thread_repo = NULL;
1882 struct got_commit_graph *thread_graph = NULL;
1883 int errcode;
1885 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1886 if (err != NULL)
1887 goto done;
1889 /* The commit queue only contains commits being displayed. */
1890 TAILQ_INIT(&s->commits.head);
1891 s->commits.ncommits = 0;
1893 s->refs = refs;
1894 s->repo = repo;
1895 s->head_ref_name = head_ref_name;
1896 s->start_id = got_object_id_dup(start_id);
1897 if (s->start_id == NULL) {
1898 err = got_error_from_errno("got_object_id_dup");
1899 goto done;
1902 view->show = show_log_view;
1903 view->input = input_log_view;
1904 view->close = close_log_view;
1905 view->search_start = search_start_log_view;
1906 view->search_next = search_next_log_view;
1908 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1909 if (err)
1910 goto done;
1911 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1912 0, thread_repo);
1913 if (err)
1914 goto done;
1916 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1917 if (errcode) {
1918 err = got_error_set_errno(errcode, "pthread_cond_init");
1919 goto done;
1922 s->thread_args.commits_needed = view->nlines;
1923 s->thread_args.graph = thread_graph;
1924 s->thread_args.commits = &s->commits;
1925 s->thread_args.in_repo_path = s->in_repo_path;
1926 s->thread_args.start_id = s->start_id;
1927 s->thread_args.repo = thread_repo;
1928 s->thread_args.log_complete = 0;
1929 s->thread_args.quit = &s->quit;
1930 s->thread_args.view = view;
1931 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1932 s->thread_args.selected_entry = &s->selected_entry;
1933 done:
1934 if (err)
1935 close_log_view(view);
1936 return err;
1939 static const struct got_error *
1940 show_log_view(struct tog_view *view)
1942 struct tog_log_view_state *s = &view->state.log;
1944 if (s->thread == NULL) {
1945 int errcode = pthread_create(&s->thread, NULL, log_thread,
1946 &s->thread_args);
1947 if (errcode)
1948 return got_error_set_errno(errcode, "pthread_create");
1951 return draw_commits(view, &s->last_displayed_entry,
1952 &s->selected_entry, s->first_displayed_entry,
1953 &s->commits, s->selected, view->nlines, s->refs,
1954 s->in_repo_path, s->thread_args.commits_needed);
1957 static const struct got_error *
1958 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1959 struct tog_view **focus_view, struct tog_view *view, int ch)
1961 const struct got_error *err = NULL;
1962 struct tog_log_view_state *s = &view->state.log;
1963 char *parent_path, *in_repo_path = NULL;
1964 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1965 int begin_x = 0;
1966 struct got_object_id *start_id;
1968 switch (ch) {
1969 case 'q':
1970 s->quit = 1;
1971 break;
1972 case 'k':
1973 case KEY_UP:
1974 case '<':
1975 case ',':
1976 if (s->first_displayed_entry == NULL)
1977 break;
1978 if (s->selected > 0)
1979 s->selected--;
1980 else
1981 scroll_up(view, &s->first_displayed_entry, 1,
1982 &s->commits);
1983 break;
1984 case KEY_PPAGE:
1985 case CTRL('b'):
1986 if (s->first_displayed_entry == NULL)
1987 break;
1988 if (TAILQ_FIRST(&s->commits.head) ==
1989 s->first_displayed_entry) {
1990 s->selected = 0;
1991 break;
1993 scroll_up(view, &s->first_displayed_entry,
1994 view->nlines, &s->commits);
1995 break;
1996 case 'j':
1997 case KEY_DOWN:
1998 case '>':
1999 case '.':
2000 if (s->first_displayed_entry == NULL)
2001 break;
2002 if (s->selected < MIN(view->nlines - 2,
2003 s->commits.ncommits - 1)) {
2004 s->selected++;
2005 break;
2007 err = scroll_down(view, &s->first_displayed_entry, 1,
2008 &s->last_displayed_entry, &s->commits,
2009 &s->thread_args.log_complete,
2010 &s->thread_args.commits_needed,
2011 &s->thread_args.need_commits);
2012 break;
2013 case KEY_NPAGE:
2014 case CTRL('f'): {
2015 struct commit_queue_entry *first;
2016 first = s->first_displayed_entry;
2017 if (first == NULL)
2018 break;
2019 err = scroll_down(view, &s->first_displayed_entry,
2020 view->nlines, &s->last_displayed_entry,
2021 &s->commits, &s->thread_args.log_complete,
2022 &s->thread_args.commits_needed,
2023 &s->thread_args.need_commits);
2024 if (first == s->first_displayed_entry &&
2025 s->selected < MIN(view->nlines - 2,
2026 s->commits.ncommits - 1)) {
2027 /* can't scroll further down */
2028 s->selected = MIN(view->nlines - 2,
2029 s->commits.ncommits - 1);
2031 err = NULL;
2032 break;
2034 case KEY_RESIZE:
2035 if (s->selected > view->nlines - 2)
2036 s->selected = view->nlines - 2;
2037 if (s->selected > s->commits.ncommits - 1)
2038 s->selected = s->commits.ncommits - 1;
2039 break;
2040 case KEY_ENTER:
2041 case ' ':
2042 case '\r':
2043 if (s->selected_entry == NULL)
2044 break;
2045 if (view_is_parent_view(view))
2046 begin_x = view_split_begin_x(view->begin_x);
2047 err = open_diff_view_for_commit(&diff_view, begin_x,
2048 s->selected_entry->commit, s->selected_entry->id,
2049 view, s->refs, s->repo);
2050 if (err)
2051 break;
2052 if (view_is_parent_view(view)) {
2053 err = view_close_child(view);
2054 if (err)
2055 return err;
2056 err = view_set_child(view, diff_view);
2057 if (err) {
2058 view_close(diff_view);
2059 break;
2061 *focus_view = diff_view;
2062 view->child_focussed = 1;
2063 } else
2064 *new_view = diff_view;
2065 break;
2066 case 't':
2067 if (s->selected_entry == NULL)
2068 break;
2069 if (view_is_parent_view(view))
2070 begin_x = view_split_begin_x(view->begin_x);
2071 err = browse_commit_tree(&tree_view, begin_x,
2072 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2073 if (err)
2074 break;
2075 if (view_is_parent_view(view)) {
2076 err = view_close_child(view);
2077 if (err)
2078 return err;
2079 err = view_set_child(view, tree_view);
2080 if (err) {
2081 view_close(tree_view);
2082 break;
2084 *focus_view = tree_view;
2085 view->child_focussed = 1;
2086 } else
2087 *new_view = tree_view;
2088 break;
2089 case KEY_BACKSPACE:
2090 if (strcmp(s->in_repo_path, "/") == 0)
2091 break;
2092 parent_path = dirname(s->in_repo_path);
2093 if (parent_path && strcmp(parent_path, ".") != 0) {
2094 err = stop_log_thread(s);
2095 if (err)
2096 return err;
2097 lv = view_open(view->nlines, view->ncols,
2098 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2099 if (lv == NULL)
2100 return got_error_from_errno(
2101 "view_open");
2102 err = open_log_view(lv, s->start_id, s->refs,
2103 s->repo, s->head_ref_name, parent_path, 0);
2104 if (err)
2105 return err;;
2106 if (view_is_parent_view(view))
2107 *new_view = lv;
2108 else {
2109 view_set_child(view->parent, lv);
2110 *focus_view = lv;
2112 return NULL;
2114 break;
2115 case CTRL('l'):
2116 err = stop_log_thread(s);
2117 if (err)
2118 return err;
2119 lv = view_open(view->nlines, view->ncols,
2120 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2121 if (lv == NULL)
2122 return got_error_from_errno("view_open");
2123 err = get_head_commit_id(&start_id, s->head_ref_name ?
2124 s->head_ref_name : GOT_REF_HEAD, s->repo);
2125 if (err) {
2126 view_close(lv);
2127 return err;
2129 in_repo_path = strdup(s->in_repo_path);
2130 if (in_repo_path == NULL) {
2131 free(start_id);
2132 view_close(lv);
2133 return got_error_from_errno("strdup");
2135 err = open_log_view(lv, start_id, s->refs, s->repo,
2136 s->head_ref_name, in_repo_path, 0);
2137 if (err) {
2138 free(start_id);
2139 view_close(lv);
2140 return err;;
2142 *dead_view = view;
2143 *new_view = lv;
2144 break;
2145 default:
2146 break;
2149 return err;
2152 static const struct got_error *
2153 apply_unveil(const char *repo_path, const char *worktree_path)
2155 const struct got_error *error;
2157 #ifdef PROFILE
2158 if (unveil("gmon.out", "rwc") != 0)
2159 return got_error_from_errno2("unveil", "gmon.out");
2160 #endif
2161 if (repo_path && unveil(repo_path, "r") != 0)
2162 return got_error_from_errno2("unveil", repo_path);
2164 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2165 return got_error_from_errno2("unveil", worktree_path);
2167 if (unveil("/tmp", "rwc") != 0)
2168 return got_error_from_errno2("unveil", "/tmp");
2170 error = got_privsep_unveil_exec_helpers();
2171 if (error != NULL)
2172 return error;
2174 if (unveil(NULL, NULL) != 0)
2175 return got_error_from_errno("unveil");
2177 return NULL;
2180 static void
2181 init_curses(void)
2183 initscr();
2184 cbreak();
2185 halfdelay(1); /* Do fast refresh while initial view is loading. */
2186 noecho();
2187 nonl();
2188 intrflush(stdscr, FALSE);
2189 keypad(stdscr, TRUE);
2190 curs_set(0);
2191 signal(SIGWINCH, tog_sigwinch);
2192 signal(SIGPIPE, tog_sigpipe);
2195 static const struct got_error *
2196 cmd_log(int argc, char *argv[])
2198 const struct got_error *error;
2199 struct got_repository *repo = NULL;
2200 struct got_worktree *worktree = NULL;
2201 struct got_reflist_head refs;
2202 struct got_object_id *start_id = NULL;
2203 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2204 char *start_commit = NULL, *head_ref_name = NULL;
2205 int ch;
2206 struct tog_view *view;
2208 SIMPLEQ_INIT(&refs);
2210 #ifndef PROFILE
2211 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2212 NULL) == -1)
2213 err(1, "pledge");
2214 #endif
2216 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2217 switch (ch) {
2218 case 'c':
2219 start_commit = optarg;
2220 break;
2221 case 'r':
2222 repo_path = realpath(optarg, NULL);
2223 if (repo_path == NULL)
2224 err(1, "-r option");
2225 break;
2226 default:
2227 usage_log();
2228 /* NOTREACHED */
2232 argc -= optind;
2233 argv += optind;
2235 cwd = getcwd(NULL, 0);
2236 if (cwd == NULL) {
2237 error = got_error_from_errno("getcwd");
2238 goto done;
2240 error = got_worktree_open(&worktree, cwd);
2241 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2242 goto done;
2243 error = NULL;
2245 if (argc == 0) {
2246 path = strdup("");
2247 if (path == NULL) {
2248 error = got_error_from_errno("strdup");
2249 goto done;
2251 } else if (argc == 1) {
2252 if (worktree) {
2253 error = got_worktree_resolve_path(&path, worktree,
2254 argv[0]);
2255 if (error)
2256 goto done;
2257 } else {
2258 path = strdup(argv[0]);
2259 if (path == NULL) {
2260 error = got_error_from_errno("strdup");
2261 goto done;
2264 } else
2265 usage_log();
2267 if (repo_path == NULL) {
2268 if (worktree)
2269 repo_path = strdup(
2270 got_worktree_get_repo_path(worktree));
2271 else
2272 repo_path = strdup(cwd);
2274 if (repo_path == NULL) {
2275 error = got_error_from_errno("strdup");
2276 goto done;
2279 init_curses();
2281 error = got_repo_open(&repo, repo_path);
2282 if (error != NULL)
2283 goto done;
2285 error = apply_unveil(got_repo_get_path(repo),
2286 worktree ? got_worktree_get_root_path(worktree) : NULL);
2287 if (error)
2288 goto done;
2290 if (start_commit == NULL)
2291 error = get_head_commit_id(&start_id, worktree ?
2292 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2293 repo);
2294 else {
2295 error = get_head_commit_id(&start_id, start_commit, repo);
2296 if (error) {
2297 if (error->code != GOT_ERR_NOT_REF)
2298 goto done;
2299 error = got_repo_match_object_id_prefix(&start_id,
2300 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2303 if (error != NULL)
2304 goto done;
2306 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2307 if (error)
2308 goto done;
2310 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2311 if (view == NULL) {
2312 error = got_error_from_errno("view_open");
2313 goto done;
2315 if (worktree) {
2316 head_ref_name = strdup(
2317 got_worktree_get_head_ref_name(worktree));
2318 if (head_ref_name == NULL) {
2319 error = got_error_from_errno("strdup");
2320 goto done;
2323 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2324 path, 1);
2325 if (error)
2326 goto done;
2327 if (worktree) {
2328 /* Release work tree lock. */
2329 got_worktree_close(worktree);
2330 worktree = NULL;
2332 error = view_loop(view);
2333 done:
2334 free(repo_path);
2335 free(cwd);
2336 free(path);
2337 free(start_id);
2338 free(head_ref_name);
2339 if (repo)
2340 got_repo_close(repo);
2341 if (worktree)
2342 got_worktree_close(worktree);
2343 got_ref_list_free(&refs);
2344 return error;
2347 __dead static void
2348 usage_diff(void)
2350 endwin();
2351 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2352 getprogname());
2353 exit(1);
2356 static char *
2357 parse_next_line(FILE *f, size_t *len)
2359 char *line;
2360 size_t linelen;
2361 size_t lineno;
2362 const char delim[3] = { '\0', '\0', '\0'};
2364 line = fparseln(f, &linelen, &lineno, delim, 0);
2365 if (len)
2366 *len = linelen;
2367 return line;
2370 static const struct got_error *
2371 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2372 int *last_displayed_line, int *eof, int max_lines,
2373 char *header)
2375 const struct got_error *err;
2376 int nlines = 0, nprinted = 0;
2377 char *line;
2378 size_t len;
2379 wchar_t *wline;
2380 int width;
2382 rewind(f);
2383 werase(view->window);
2385 if (header) {
2386 err = format_line(&wline, &width, header, view->ncols);
2387 if (err) {
2388 return err;
2391 if (view_needs_focus_indication(view))
2392 wstandout(view->window);
2393 waddwstr(view->window, wline);
2394 if (view_needs_focus_indication(view))
2395 wstandend(view->window);
2396 if (width < view->ncols - 1)
2397 waddch(view->window, '\n');
2399 if (max_lines <= 1)
2400 return NULL;
2401 max_lines--;
2404 *eof = 0;
2405 while (nprinted < max_lines) {
2406 line = parse_next_line(f, &len);
2407 if (line == NULL) {
2408 *eof = 1;
2409 break;
2411 if (++nlines < *first_displayed_line) {
2412 free(line);
2413 continue;
2416 err = format_line(&wline, &width, line, view->ncols);
2417 if (err) {
2418 free(line);
2419 return err;
2421 waddwstr(view->window, wline);
2422 if (width < view->ncols - 1)
2423 waddch(view->window, '\n');
2424 if (++nprinted == 1)
2425 *first_displayed_line = nlines;
2426 free(line);
2427 free(wline);
2428 wline = NULL;
2430 *last_displayed_line = nlines;
2432 view_vborder(view);
2434 if (*eof) {
2435 while (nprinted < view->nlines) {
2436 waddch(view->window, '\n');
2437 nprinted++;
2440 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2441 if (err) {
2442 return err;
2445 wstandout(view->window);
2446 waddwstr(view->window, wline);
2447 wstandend(view->window);
2450 return NULL;
2453 static char *
2454 get_datestr(time_t *time, char *datebuf)
2456 struct tm mytm, *tm;
2457 char *p, *s;
2459 tm = gmtime_r(time, &mytm);
2460 if (tm == NULL)
2461 return NULL;
2462 s = asctime_r(tm, datebuf);
2463 if (s == NULL)
2464 return NULL;
2465 p = strchr(s, '\n');
2466 if (p)
2467 *p = '\0';
2468 return s;
2471 static const struct got_error *
2472 write_commit_info(struct got_object_id *commit_id,
2473 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2475 const struct got_error *err = NULL;
2476 char datebuf[26], *datestr;
2477 struct got_commit_object *commit;
2478 char *id_str = NULL, *logmsg = NULL;
2479 time_t committer_time;
2480 const char *author, *committer;
2481 char *refs_str = NULL;
2483 if (refs) {
2484 err = build_refs_str(&refs_str, refs, commit_id, repo);
2485 if (err)
2486 return err;
2489 err = got_object_open_as_commit(&commit, repo, commit_id);
2490 if (err)
2491 return err;
2493 err = got_object_id_str(&id_str, commit_id);
2494 if (err) {
2495 err = got_error_from_errno("got_object_id_str");
2496 goto done;
2499 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2500 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2501 err = got_error_from_errno("fprintf");
2502 goto done;
2504 if (fprintf(outfile, "from: %s\n",
2505 got_object_commit_get_author(commit)) < 0) {
2506 err = got_error_from_errno("fprintf");
2507 goto done;
2509 committer_time = got_object_commit_get_committer_time(commit);
2510 datestr = get_datestr(&committer_time, datebuf);
2511 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2512 err = got_error_from_errno("fprintf");
2513 goto done;
2515 author = got_object_commit_get_author(commit);
2516 committer = got_object_commit_get_committer(commit);
2517 if (strcmp(author, committer) != 0 &&
2518 fprintf(outfile, "via: %s\n", committer) < 0) {
2519 err = got_error_from_errno("fprintf");
2520 goto done;
2522 err = got_object_commit_get_logmsg(&logmsg, commit);
2523 if (err)
2524 goto done;
2525 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2526 err = got_error_from_errno("fprintf");
2527 goto done;
2529 done:
2530 free(id_str);
2531 free(logmsg);
2532 free(refs_str);
2533 got_object_commit_close(commit);
2534 return err;
2537 static const struct got_error *
2538 create_diff(struct tog_diff_view_state *s)
2540 const struct got_error *err = NULL;
2541 FILE *f = NULL;
2542 int obj_type;
2544 f = got_opentemp();
2545 if (f == NULL) {
2546 err = got_error_from_errno("got_opentemp");
2547 goto done;
2549 if (s->f && fclose(s->f) != 0) {
2550 err = got_error_from_errno("fclose");
2551 goto done;
2553 s->f = f;
2555 if (s->id1)
2556 err = got_object_get_type(&obj_type, s->repo, s->id1);
2557 else
2558 err = got_object_get_type(&obj_type, s->repo, s->id2);
2559 if (err)
2560 goto done;
2562 switch (obj_type) {
2563 case GOT_OBJ_TYPE_BLOB:
2564 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2565 s->diff_context, s->repo, f);
2566 break;
2567 case GOT_OBJ_TYPE_TREE:
2568 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2569 s->diff_context, s->repo, f);
2570 break;
2571 case GOT_OBJ_TYPE_COMMIT: {
2572 const struct got_object_id_queue *parent_ids;
2573 struct got_object_qid *pid;
2574 struct got_commit_object *commit2;
2576 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2577 if (err)
2578 break;
2579 /* Show commit info if we're diffing to a parent/root commit. */
2580 if (s->id1 == NULL)
2581 write_commit_info(s->id2, s->refs, s->repo, f);
2582 else {
2583 parent_ids = got_object_commit_get_parent_ids(commit2);
2584 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2585 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2586 write_commit_info(s->id2, s->refs,
2587 s->repo, f);
2588 break;
2592 got_object_commit_close(commit2);
2594 err = got_diff_objects_as_commits(s->id1, s->id2,
2595 s->diff_context, s->repo, f);
2596 break;
2598 default:
2599 err = got_error(GOT_ERR_OBJ_TYPE);
2600 break;
2602 done:
2603 if (f && fflush(f) != 0 && err == NULL)
2604 err = got_error_from_errno("fflush");
2605 return err;
2608 static void
2609 diff_view_indicate_progress(struct tog_view *view)
2611 mvwaddstr(view->window, 0, 0, "diffing...");
2612 update_panels();
2613 doupdate();
2616 static const struct got_error *
2617 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2618 struct got_object_id *id2, struct tog_view *log_view,
2619 struct got_reflist_head *refs, struct got_repository *repo)
2621 const struct got_error *err;
2623 if (id1 != NULL && id2 != NULL) {
2624 int type1, type2;
2625 err = got_object_get_type(&type1, repo, id1);
2626 if (err)
2627 return err;
2628 err = got_object_get_type(&type2, repo, id2);
2629 if (err)
2630 return err;
2632 if (type1 != type2)
2633 return got_error(GOT_ERR_OBJ_TYPE);
2636 if (id1) {
2637 view->state.diff.id1 = got_object_id_dup(id1);
2638 if (view->state.diff.id1 == NULL)
2639 return got_error_from_errno("got_object_id_dup");
2640 } else
2641 view->state.diff.id1 = NULL;
2643 view->state.diff.id2 = got_object_id_dup(id2);
2644 if (view->state.diff.id2 == NULL) {
2645 free(view->state.diff.id1);
2646 view->state.diff.id1 = NULL;
2647 return got_error_from_errno("got_object_id_dup");
2649 view->state.diff.f = NULL;
2650 view->state.diff.first_displayed_line = 1;
2651 view->state.diff.last_displayed_line = view->nlines;
2652 view->state.diff.diff_context = 3;
2653 view->state.diff.log_view = log_view;
2654 view->state.diff.repo = repo;
2655 view->state.diff.refs = refs;
2657 if (log_view && view_is_splitscreen(view))
2658 show_log_view(log_view); /* draw vborder */
2659 diff_view_indicate_progress(view);
2661 err = create_diff(&view->state.diff);
2662 if (err) {
2663 free(view->state.diff.id1);
2664 view->state.diff.id1 = NULL;
2665 free(view->state.diff.id2);
2666 view->state.diff.id2 = NULL;
2667 return err;
2670 view->show = show_diff_view;
2671 view->input = input_diff_view;
2672 view->close = close_diff_view;
2674 return NULL;
2677 static const struct got_error *
2678 close_diff_view(struct tog_view *view)
2680 const struct got_error *err = NULL;
2682 free(view->state.diff.id1);
2683 view->state.diff.id1 = NULL;
2684 free(view->state.diff.id2);
2685 view->state.diff.id2 = NULL;
2686 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2687 err = got_error_from_errno("fclose");
2688 return err;
2691 static const struct got_error *
2692 show_diff_view(struct tog_view *view)
2694 const struct got_error *err;
2695 struct tog_diff_view_state *s = &view->state.diff;
2696 char *id_str1 = NULL, *id_str2, *header;
2698 if (s->id1) {
2699 err = got_object_id_str(&id_str1, s->id1);
2700 if (err)
2701 return err;
2703 err = got_object_id_str(&id_str2, s->id2);
2704 if (err)
2705 return err;
2707 if (asprintf(&header, "diff %s %s",
2708 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2709 err = got_error_from_errno("asprintf");
2710 free(id_str1);
2711 free(id_str2);
2712 return err;
2714 free(id_str1);
2715 free(id_str2);
2717 return draw_file(view, s->f, &s->first_displayed_line,
2718 &s->last_displayed_line, &s->eof, view->nlines,
2719 header);
2722 static const struct got_error *
2723 set_selected_commit(struct tog_diff_view_state *s,
2724 struct commit_queue_entry *entry)
2726 const struct got_error *err;
2727 const struct got_object_id_queue *parent_ids;
2728 struct got_commit_object *selected_commit;
2729 struct got_object_qid *pid;
2731 free(s->id2);
2732 s->id2 = got_object_id_dup(entry->id);
2733 if (s->id2 == NULL)
2734 return got_error_from_errno("got_object_id_dup");
2736 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2737 if (err)
2738 return err;
2739 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2740 free(s->id1);
2741 pid = SIMPLEQ_FIRST(parent_ids);
2742 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2743 got_object_commit_close(selected_commit);
2744 return NULL;
2747 static const struct got_error *
2748 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2749 struct tog_view **focus_view, struct tog_view *view, int ch)
2751 const struct got_error *err = NULL;
2752 struct tog_diff_view_state *s = &view->state.diff;
2753 struct tog_log_view_state *ls;
2754 struct commit_queue_entry *entry;
2755 int i;
2757 switch (ch) {
2758 case 'k':
2759 case KEY_UP:
2760 if (s->first_displayed_line > 1)
2761 s->first_displayed_line--;
2762 break;
2763 case KEY_PPAGE:
2764 case CTRL('b'):
2765 if (s->first_displayed_line == 1)
2766 break;
2767 i = 0;
2768 while (i++ < view->nlines - 1 &&
2769 s->first_displayed_line > 1)
2770 s->first_displayed_line--;
2771 break;
2772 case 'j':
2773 case KEY_DOWN:
2774 if (!s->eof)
2775 s->first_displayed_line++;
2776 break;
2777 case KEY_NPAGE:
2778 case CTRL('f'):
2779 case ' ':
2780 if (s->eof)
2781 break;
2782 i = 0;
2783 while (!s->eof && i++ < view->nlines - 1) {
2784 char *line;
2785 line = parse_next_line(s->f, NULL);
2786 s->first_displayed_line++;
2787 if (line == NULL)
2788 break;
2790 break;
2791 case '[':
2792 if (s->diff_context > 0) {
2793 s->diff_context--;
2794 diff_view_indicate_progress(view);
2795 err = create_diff(s);
2797 break;
2798 case ']':
2799 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2800 s->diff_context++;
2801 diff_view_indicate_progress(view);
2802 err = create_diff(s);
2804 break;
2805 case '<':
2806 case ',':
2807 if (s->log_view == NULL)
2808 break;
2809 ls = &s->log_view->state.log;
2810 entry = TAILQ_PREV(ls->selected_entry,
2811 commit_queue_head, entry);
2812 if (entry == NULL)
2813 break;
2815 err = input_log_view(NULL, NULL, NULL, s->log_view,
2816 KEY_UP);
2817 if (err)
2818 break;
2820 err = set_selected_commit(s, entry);
2821 if (err)
2822 break;
2824 s->first_displayed_line = 1;
2825 s->last_displayed_line = view->nlines;
2827 diff_view_indicate_progress(view);
2828 err = create_diff(s);
2829 break;
2830 case '>':
2831 case '.':
2832 if (s->log_view == NULL)
2833 break;
2834 ls = &s->log_view->state.log;
2836 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2837 ls->thread_args.commits_needed++;
2839 /* Display "loading..." in log view. */
2840 show_log_view(s->log_view);
2841 update_panels();
2842 doupdate();
2844 err = trigger_log_thread(1 /* load_all */,
2845 &ls->thread_args.commits_needed,
2846 &ls->thread_args.log_complete,
2847 &ls->thread_args.need_commits);
2848 if (err)
2849 break;
2851 err = input_log_view(NULL, NULL, NULL, s->log_view,
2852 KEY_DOWN);
2853 if (err)
2854 break;
2856 entry = TAILQ_NEXT(ls->selected_entry, entry);
2857 if (entry == NULL)
2858 break;
2860 err = set_selected_commit(s, entry);
2861 if (err)
2862 break;
2864 s->first_displayed_line = 1;
2865 s->last_displayed_line = view->nlines;
2867 diff_view_indicate_progress(view);
2868 err = create_diff(s);
2869 break;
2870 default:
2871 break;
2874 return err;
2877 static const struct got_error *
2878 cmd_diff(int argc, char *argv[])
2880 const struct got_error *error = NULL;
2881 struct got_repository *repo = NULL;
2882 struct got_reflist_head refs;
2883 struct got_object_id *id1 = NULL, *id2 = NULL;
2884 char *repo_path = NULL;
2885 char *id_str1 = NULL, *id_str2 = NULL;
2886 int ch;
2887 struct tog_view *view;
2889 SIMPLEQ_INIT(&refs);
2891 #ifndef PROFILE
2892 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2893 NULL) == -1)
2894 err(1, "pledge");
2895 #endif
2897 while ((ch = getopt(argc, argv, "")) != -1) {
2898 switch (ch) {
2899 default:
2900 usage_diff();
2901 /* NOTREACHED */
2905 argc -= optind;
2906 argv += optind;
2908 if (argc == 0) {
2909 usage_diff(); /* TODO show local worktree changes */
2910 } else if (argc == 2) {
2911 repo_path = getcwd(NULL, 0);
2912 if (repo_path == NULL)
2913 return got_error_from_errno("getcwd");
2914 id_str1 = argv[0];
2915 id_str2 = argv[1];
2916 } else if (argc == 3) {
2917 repo_path = realpath(argv[0], NULL);
2918 if (repo_path == NULL)
2919 return got_error_from_errno2("realpath", argv[0]);
2920 id_str1 = argv[1];
2921 id_str2 = argv[2];
2922 } else
2923 usage_diff();
2925 init_curses();
2927 error = got_repo_open(&repo, repo_path);
2928 if (error)
2929 goto done;
2931 error = apply_unveil(got_repo_get_path(repo), NULL);
2932 if (error)
2933 goto done;
2935 error = got_repo_match_object_id_prefix(&id1, id_str1,
2936 GOT_OBJ_TYPE_ANY, repo);
2937 if (error)
2938 goto done;
2940 error = got_repo_match_object_id_prefix(&id2, id_str2,
2941 GOT_OBJ_TYPE_ANY, repo);
2942 if (error)
2943 goto done;
2945 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2946 if (error)
2947 goto done;
2949 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2950 if (view == NULL) {
2951 error = got_error_from_errno("view_open");
2952 goto done;
2954 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2955 if (error)
2956 goto done;
2957 error = view_loop(view);
2958 done:
2959 free(repo_path);
2960 if (repo)
2961 got_repo_close(repo);
2962 got_ref_list_free(&refs);
2963 return error;
2966 __dead static void
2967 usage_blame(void)
2969 endwin();
2970 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2971 getprogname());
2972 exit(1);
2975 struct tog_blame_line {
2976 int annotated;
2977 struct got_object_id *id;
2980 static const struct got_error *
2981 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2982 const char *path, struct tog_blame_line *lines, int nlines,
2983 int blame_complete, int selected_line, int *first_displayed_line,
2984 int *last_displayed_line, int *eof, int max_lines)
2986 const struct got_error *err;
2987 int lineno = 0, nprinted = 0;
2988 char *line;
2989 size_t len;
2990 wchar_t *wline;
2991 int width, wlimit;
2992 struct tog_blame_line *blame_line;
2993 struct got_object_id *prev_id = NULL;
2994 char *id_str;
2996 err = got_object_id_str(&id_str, id);
2997 if (err)
2998 return err;
3000 rewind(f);
3001 werase(view->window);
3003 if (asprintf(&line, "commit %s", id_str) == -1) {
3004 err = got_error_from_errno("asprintf");
3005 free(id_str);
3006 return err;
3009 err = format_line(&wline, &width, line, view->ncols);
3010 free(line);
3011 line = NULL;
3012 if (view_needs_focus_indication(view))
3013 wstandout(view->window);
3014 waddwstr(view->window, wline);
3015 if (view_needs_focus_indication(view))
3016 wstandend(view->window);
3017 free(wline);
3018 wline = NULL;
3019 if (width < view->ncols - 1)
3020 waddch(view->window, '\n');
3022 if (asprintf(&line, "[%d/%d] %s%s",
3023 *first_displayed_line - 1 + selected_line, nlines,
3024 blame_complete ? "" : "annotating... ", path) == -1) {
3025 free(id_str);
3026 return got_error_from_errno("asprintf");
3028 free(id_str);
3029 err = format_line(&wline, &width, line, view->ncols);
3030 free(line);
3031 line = NULL;
3032 if (err)
3033 return err;
3034 waddwstr(view->window, wline);
3035 free(wline);
3036 wline = NULL;
3037 if (width < view->ncols - 1)
3038 waddch(view->window, '\n');
3040 *eof = 0;
3041 while (nprinted < max_lines - 2) {
3042 line = parse_next_line(f, &len);
3043 if (line == NULL) {
3044 *eof = 1;
3045 break;
3047 if (++lineno < *first_displayed_line) {
3048 free(line);
3049 continue;
3052 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3053 err = format_line(&wline, &width, line, wlimit);
3054 if (err) {
3055 free(line);
3056 return err;
3059 if (view->focussed && nprinted == selected_line - 1)
3060 wstandout(view->window);
3062 if (nlines > 0) {
3063 blame_line = &lines[lineno - 1];
3064 if (blame_line->annotated && prev_id &&
3065 got_object_id_cmp(prev_id, blame_line->id) == 0)
3066 waddstr(view->window, " ");
3067 else if (blame_line->annotated) {
3068 char *id_str;
3069 err = got_object_id_str(&id_str, blame_line->id);
3070 if (err) {
3071 free(line);
3072 free(wline);
3073 return err;
3075 wprintw(view->window, "%.8s ", id_str);
3076 free(id_str);
3077 prev_id = blame_line->id;
3078 } else {
3079 waddstr(view->window, "........ ");
3080 prev_id = NULL;
3082 } else {
3083 waddstr(view->window, "........ ");
3084 prev_id = NULL;
3087 waddwstr(view->window, wline);
3088 while (width < wlimit) {
3089 waddch(view->window, ' ');
3090 width++;
3092 if (view->focussed && nprinted == selected_line - 1)
3093 wstandend(view->window);
3094 if (++nprinted == 1)
3095 *first_displayed_line = lineno;
3096 free(line);
3097 free(wline);
3098 wline = NULL;
3100 *last_displayed_line = lineno;
3102 view_vborder(view);
3104 return NULL;
3107 static const struct got_error *
3108 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3110 const struct got_error *err = NULL;
3111 struct tog_blame_cb_args *a = arg;
3112 struct tog_blame_line *line;
3113 int errcode;
3115 if (nlines != a->nlines ||
3116 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3117 return got_error(GOT_ERR_RANGE);
3119 errcode = pthread_mutex_lock(&tog_mutex);
3120 if (errcode)
3121 return got_error_set_errno(errcode, "pthread_mutex_lock");
3123 if (*a->quit) { /* user has quit the blame view */
3124 err = got_error(GOT_ERR_ITER_COMPLETED);
3125 goto done;
3128 if (lineno == -1)
3129 goto done; /* no change in this commit */
3131 line = &a->lines[lineno - 1];
3132 if (line->annotated)
3133 goto done;
3135 line->id = got_object_id_dup(id);
3136 if (line->id == NULL) {
3137 err = got_error_from_errno("got_object_id_dup");
3138 goto done;
3140 line->annotated = 1;
3141 done:
3142 errcode = pthread_mutex_unlock(&tog_mutex);
3143 if (errcode)
3144 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3145 return err;
3148 static void *
3149 blame_thread(void *arg)
3151 const struct got_error *err;
3152 struct tog_blame_thread_args *ta = arg;
3153 struct tog_blame_cb_args *a = ta->cb_args;
3154 int errcode;
3156 err = got_blame(ta->path, a->commit_id, ta->repo,
3157 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3158 if (err && err->code == GOT_ERR_CANCELLED)
3159 err = NULL;
3161 errcode = pthread_mutex_lock(&tog_mutex);
3162 if (errcode)
3163 return (void *)got_error_set_errno(errcode,
3164 "pthread_mutex_lock");
3166 got_repo_close(ta->repo);
3167 ta->repo = NULL;
3168 *ta->complete = 1;
3170 errcode = pthread_mutex_unlock(&tog_mutex);
3171 if (errcode && err == NULL)
3172 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3174 return (void *)err;
3177 static struct got_object_id *
3178 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3179 int first_displayed_line, int selected_line)
3181 struct tog_blame_line *line;
3183 if (nlines <= 0)
3184 return NULL;
3186 line = &lines[first_displayed_line - 1 + selected_line - 1];
3187 if (!line->annotated)
3188 return NULL;
3190 return line->id;
3193 static const struct got_error *
3194 stop_blame(struct tog_blame *blame)
3196 const struct got_error *err = NULL;
3197 int i;
3199 if (blame->thread) {
3200 int errcode;
3201 errcode = pthread_mutex_unlock(&tog_mutex);
3202 if (errcode)
3203 return got_error_set_errno(errcode,
3204 "pthread_mutex_unlock");
3205 errcode = pthread_join(blame->thread, (void **)&err);
3206 if (errcode)
3207 return got_error_set_errno(errcode, "pthread_join");
3208 errcode = pthread_mutex_lock(&tog_mutex);
3209 if (errcode)
3210 return got_error_set_errno(errcode,
3211 "pthread_mutex_lock");
3212 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3213 err = NULL;
3214 blame->thread = NULL;
3216 if (blame->thread_args.repo) {
3217 got_repo_close(blame->thread_args.repo);
3218 blame->thread_args.repo = NULL;
3220 if (blame->f) {
3221 if (fclose(blame->f) != 0 && err == NULL)
3222 err = got_error_from_errno("fclose");
3223 blame->f = NULL;
3225 if (blame->lines) {
3226 for (i = 0; i < blame->nlines; i++)
3227 free(blame->lines[i].id);
3228 free(blame->lines);
3229 blame->lines = NULL;
3231 free(blame->cb_args.commit_id);
3232 blame->cb_args.commit_id = NULL;
3234 return err;
3237 static const struct got_error *
3238 cancel_blame_view(void *arg)
3240 const struct got_error *err = NULL;
3241 int *done = arg;
3242 int errcode;
3244 errcode = pthread_mutex_lock(&tog_mutex);
3245 if (errcode)
3246 return got_error_set_errno(errcode,
3247 "pthread_mutex_unlock");
3249 if (*done)
3250 err = got_error(GOT_ERR_CANCELLED);
3252 errcode = pthread_mutex_unlock(&tog_mutex);
3253 if (errcode)
3254 return got_error_set_errno(errcode,
3255 "pthread_mutex_lock");
3257 return err;
3260 static const struct got_error *
3261 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3262 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3263 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3264 struct got_repository *repo)
3266 const struct got_error *err = NULL;
3267 struct got_blob_object *blob = NULL;
3268 struct got_repository *thread_repo = NULL;
3269 struct got_object_id *obj_id = NULL;
3270 int obj_type;
3272 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3273 if (err)
3274 return err;
3275 if (obj_id == NULL)
3276 return got_error(GOT_ERR_NO_OBJ);
3278 err = got_object_get_type(&obj_type, repo, obj_id);
3279 if (err)
3280 goto done;
3282 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3283 err = got_error(GOT_ERR_OBJ_TYPE);
3284 goto done;
3287 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3288 if (err)
3289 goto done;
3290 blame->f = got_opentemp();
3291 if (blame->f == NULL) {
3292 err = got_error_from_errno("got_opentemp");
3293 goto done;
3295 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3296 &blame->line_offsets, blame->f, blob);
3297 if (err || blame->nlines == 0)
3298 goto done;
3300 /* Don't include \n at EOF in the blame line count. */
3301 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3302 blame->nlines--;
3304 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3305 if (blame->lines == NULL) {
3306 err = got_error_from_errno("calloc");
3307 goto done;
3310 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3311 if (err)
3312 goto done;
3314 blame->cb_args.view = view;
3315 blame->cb_args.lines = blame->lines;
3316 blame->cb_args.nlines = blame->nlines;
3317 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3318 if (blame->cb_args.commit_id == NULL) {
3319 err = got_error_from_errno("got_object_id_dup");
3320 goto done;
3322 blame->cb_args.quit = done;
3324 blame->thread_args.path = path;
3325 blame->thread_args.repo = thread_repo;
3326 blame->thread_args.cb_args = &blame->cb_args;
3327 blame->thread_args.complete = blame_complete;
3328 blame->thread_args.cancel_cb = cancel_blame_view;
3329 blame->thread_args.cancel_arg = done;
3330 *blame_complete = 0;
3332 done:
3333 if (blob)
3334 got_object_blob_close(blob);
3335 free(obj_id);
3336 if (err)
3337 stop_blame(blame);
3338 return err;
3341 static const struct got_error *
3342 open_blame_view(struct tog_view *view, char *path,
3343 struct got_object_id *commit_id, struct got_reflist_head *refs,
3344 struct got_repository *repo)
3346 const struct got_error *err = NULL;
3347 struct tog_blame_view_state *s = &view->state.blame;
3349 SIMPLEQ_INIT(&s->blamed_commits);
3351 s->path = strdup(path);
3352 if (s->path == NULL)
3353 return got_error_from_errno("strdup");
3355 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3356 if (err) {
3357 free(s->path);
3358 return err;
3361 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3362 s->first_displayed_line = 1;
3363 s->last_displayed_line = view->nlines;
3364 s->selected_line = 1;
3365 s->blame_complete = 0;
3366 s->repo = repo;
3367 s->refs = refs;
3368 s->commit_id = commit_id;
3369 memset(&s->blame, 0, sizeof(s->blame));
3371 view->show = show_blame_view;
3372 view->input = input_blame_view;
3373 view->close = close_blame_view;
3374 view->search_start = search_start_blame_view;
3375 view->search_next = search_next_blame_view;
3377 return run_blame(&s->blame, view, &s->blame_complete,
3378 &s->first_displayed_line, &s->last_displayed_line,
3379 &s->selected_line, &s->done, &s->eof, s->path,
3380 s->blamed_commit->id, s->repo);
3383 static const struct got_error *
3384 close_blame_view(struct tog_view *view)
3386 const struct got_error *err = NULL;
3387 struct tog_blame_view_state *s = &view->state.blame;
3389 if (s->blame.thread)
3390 err = stop_blame(&s->blame);
3392 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3393 struct got_object_qid *blamed_commit;
3394 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3395 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3396 got_object_qid_free(blamed_commit);
3399 free(s->path);
3401 return err;
3404 static const struct got_error *
3405 search_start_blame_view(struct tog_view *view)
3407 struct tog_blame_view_state *s = &view->state.blame;
3409 s->matched_line = 0;
3410 return NULL;
3413 static int
3414 match_line(const char *line, regex_t *regex)
3416 regmatch_t regmatch;
3418 return regexec(regex, line, 1, &regmatch, 0) == 0;
3422 static const struct got_error *
3423 search_next_blame_view(struct tog_view *view)
3425 struct tog_blame_view_state *s = &view->state.blame;
3426 int lineno;
3428 if (!view->searching) {
3429 view->search_next_done = 1;
3430 return NULL;
3433 if (s->matched_line) {
3434 if (view->searching == TOG_SEARCH_FORWARD)
3435 lineno = s->matched_line + 1;
3436 else
3437 lineno = s->matched_line - 1;
3438 } else {
3439 if (view->searching == TOG_SEARCH_FORWARD)
3440 lineno = 1;
3441 else
3442 lineno = s->blame.nlines;
3445 while (1) {
3446 char *line = NULL;
3447 off_t offset;
3448 size_t len;
3450 if (lineno <= 0 || lineno > s->blame.nlines) {
3451 if (s->matched_line == 0) {
3452 view->search_next_done = 1;
3453 free(line);
3454 break;
3457 if (view->searching == TOG_SEARCH_FORWARD)
3458 lineno = 1;
3459 else
3460 lineno = s->blame.nlines;
3463 offset = s->blame.line_offsets[lineno - 1];
3464 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3465 free(line);
3466 return got_error_from_errno("fseeko");
3468 free(line);
3469 line = parse_next_line(s->blame.f, &len);
3470 if (line && match_line(line, &view->regex)) {
3471 view->search_next_done = 1;
3472 s->matched_line = lineno;
3473 free(line);
3474 break;
3476 free(line);
3477 if (view->searching == TOG_SEARCH_FORWARD)
3478 lineno++;
3479 else
3480 lineno--;
3483 if (s->matched_line) {
3484 s->first_displayed_line = s->matched_line;
3485 s->selected_line = 1;
3488 return NULL;
3491 static const struct got_error *
3492 show_blame_view(struct tog_view *view)
3494 const struct got_error *err = NULL;
3495 struct tog_blame_view_state *s = &view->state.blame;
3496 int errcode;
3498 if (s->blame.thread == NULL) {
3499 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3500 &s->blame.thread_args);
3501 if (errcode)
3502 return got_error_set_errno(errcode, "pthread_create");
3504 halfdelay(1); /* fast refresh while annotating */
3507 if (s->blame_complete)
3508 halfdelay(10); /* disable fast refresh */
3510 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3511 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3512 s->selected_line, &s->first_displayed_line,
3513 &s->last_displayed_line, &s->eof, view->nlines);
3515 view_vborder(view);
3516 return err;
3519 static const struct got_error *
3520 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3521 struct tog_view **focus_view, struct tog_view *view, int ch)
3523 const struct got_error *err = NULL, *thread_err = NULL;
3524 struct tog_view *diff_view;
3525 struct tog_blame_view_state *s = &view->state.blame;
3526 int begin_x = 0;
3528 switch (ch) {
3529 case 'q':
3530 s->done = 1;
3531 break;
3532 case 'k':
3533 case KEY_UP:
3534 if (s->selected_line > 1)
3535 s->selected_line--;
3536 else if (s->selected_line == 1 &&
3537 s->first_displayed_line > 1)
3538 s->first_displayed_line--;
3539 break;
3540 case KEY_PPAGE:
3541 if (s->first_displayed_line == 1) {
3542 s->selected_line = 1;
3543 break;
3545 if (s->first_displayed_line > view->nlines - 2)
3546 s->first_displayed_line -=
3547 (view->nlines - 2);
3548 else
3549 s->first_displayed_line = 1;
3550 break;
3551 case 'j':
3552 case KEY_DOWN:
3553 if (s->selected_line < view->nlines - 2 &&
3554 s->first_displayed_line +
3555 s->selected_line <= s->blame.nlines)
3556 s->selected_line++;
3557 else if (s->last_displayed_line <
3558 s->blame.nlines)
3559 s->first_displayed_line++;
3560 break;
3561 case 'b':
3562 case 'p': {
3563 struct got_object_id *id = NULL;
3564 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3565 s->first_displayed_line, s->selected_line);
3566 if (id == NULL)
3567 break;
3568 if (ch == 'p') {
3569 struct got_commit_object *commit;
3570 struct got_object_qid *pid;
3571 struct got_object_id *blob_id = NULL;
3572 int obj_type;
3573 err = got_object_open_as_commit(&commit,
3574 s->repo, id);
3575 if (err)
3576 break;
3577 pid = SIMPLEQ_FIRST(
3578 got_object_commit_get_parent_ids(commit));
3579 if (pid == NULL) {
3580 got_object_commit_close(commit);
3581 break;
3583 /* Check if path history ends here. */
3584 err = got_object_id_by_path(&blob_id, s->repo,
3585 pid->id, s->path);
3586 if (err) {
3587 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3588 err = NULL;
3589 got_object_commit_close(commit);
3590 break;
3592 err = got_object_get_type(&obj_type, s->repo,
3593 blob_id);
3594 free(blob_id);
3595 /* Can't blame non-blob type objects. */
3596 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3597 got_object_commit_close(commit);
3598 break;
3600 err = got_object_qid_alloc(&s->blamed_commit,
3601 pid->id);
3602 got_object_commit_close(commit);
3603 } else {
3604 if (got_object_id_cmp(id,
3605 s->blamed_commit->id) == 0)
3606 break;
3607 err = got_object_qid_alloc(&s->blamed_commit,
3608 id);
3610 if (err)
3611 break;
3612 s->done = 1;
3613 thread_err = stop_blame(&s->blame);
3614 s->done = 0;
3615 if (thread_err)
3616 break;
3617 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3618 s->blamed_commit, entry);
3619 err = run_blame(&s->blame, view, &s->blame_complete,
3620 &s->first_displayed_line, &s->last_displayed_line,
3621 &s->selected_line, &s->done, &s->eof,
3622 s->path, s->blamed_commit->id, s->repo);
3623 if (err)
3624 break;
3625 break;
3627 case 'B': {
3628 struct got_object_qid *first;
3629 first = SIMPLEQ_FIRST(&s->blamed_commits);
3630 if (!got_object_id_cmp(first->id, s->commit_id))
3631 break;
3632 s->done = 1;
3633 thread_err = stop_blame(&s->blame);
3634 s->done = 0;
3635 if (thread_err)
3636 break;
3637 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3638 got_object_qid_free(s->blamed_commit);
3639 s->blamed_commit =
3640 SIMPLEQ_FIRST(&s->blamed_commits);
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, s->path,
3644 s->blamed_commit->id, s->repo);
3645 if (err)
3646 break;
3647 break;
3649 case KEY_ENTER:
3650 case '\r': {
3651 struct got_object_id *id = NULL;
3652 struct got_object_qid *pid;
3653 struct got_commit_object *commit = NULL;
3654 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3655 s->first_displayed_line, s->selected_line);
3656 if (id == NULL)
3657 break;
3658 err = got_object_open_as_commit(&commit, s->repo, id);
3659 if (err)
3660 break;
3661 pid = SIMPLEQ_FIRST(
3662 got_object_commit_get_parent_ids(commit));
3663 if (view_is_parent_view(view))
3664 begin_x = view_split_begin_x(view->begin_x);
3665 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3666 if (diff_view == NULL) {
3667 got_object_commit_close(commit);
3668 err = got_error_from_errno("view_open");
3669 break;
3671 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3672 id, NULL, s->refs, s->repo);
3673 got_object_commit_close(commit);
3674 if (err) {
3675 view_close(diff_view);
3676 break;
3678 if (view_is_parent_view(view)) {
3679 err = view_close_child(view);
3680 if (err)
3681 break;
3682 err = view_set_child(view, diff_view);
3683 if (err) {
3684 view_close(diff_view);
3685 break;
3687 *focus_view = diff_view;
3688 view->child_focussed = 1;
3689 } else
3690 *new_view = diff_view;
3691 if (err)
3692 break;
3693 break;
3695 case KEY_NPAGE:
3696 case ' ':
3697 if (s->last_displayed_line >= s->blame.nlines &&
3698 s->selected_line >= MIN(s->blame.nlines,
3699 view->nlines - 2)) {
3700 break;
3702 if (s->last_displayed_line >= s->blame.nlines &&
3703 s->selected_line < view->nlines - 2) {
3704 s->selected_line = MIN(s->blame.nlines,
3705 view->nlines - 2);
3706 break;
3708 if (s->last_displayed_line + view->nlines - 2
3709 <= s->blame.nlines)
3710 s->first_displayed_line +=
3711 view->nlines - 2;
3712 else
3713 s->first_displayed_line =
3714 s->blame.nlines -
3715 (view->nlines - 3);
3716 break;
3717 case KEY_RESIZE:
3718 if (s->selected_line > view->nlines - 2) {
3719 s->selected_line = MIN(s->blame.nlines,
3720 view->nlines - 2);
3722 break;
3723 default:
3724 break;
3726 return thread_err ? thread_err : err;
3729 static const struct got_error *
3730 cmd_blame(int argc, char *argv[])
3732 const struct got_error *error;
3733 struct got_repository *repo = NULL;
3734 struct got_reflist_head refs;
3735 struct got_worktree *worktree = NULL;
3736 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3737 struct got_object_id *commit_id = NULL;
3738 char *commit_id_str = NULL;
3739 int ch;
3740 struct tog_view *view;
3742 SIMPLEQ_INIT(&refs);
3744 #ifndef PROFILE
3745 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3746 NULL) == -1)
3747 err(1, "pledge");
3748 #endif
3750 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3751 switch (ch) {
3752 case 'c':
3753 commit_id_str = optarg;
3754 break;
3755 case 'r':
3756 repo_path = realpath(optarg, NULL);
3757 if (repo_path == NULL)
3758 err(1, "-r option");
3759 break;
3760 default:
3761 usage_blame();
3762 /* NOTREACHED */
3766 argc -= optind;
3767 argv += optind;
3769 if (argc == 1)
3770 path = argv[0];
3771 else
3772 usage_blame();
3774 cwd = getcwd(NULL, 0);
3775 if (cwd == NULL) {
3776 error = got_error_from_errno("getcwd");
3777 goto done;
3779 if (repo_path == NULL) {
3780 error = got_worktree_open(&worktree, cwd);
3781 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3782 goto done;
3783 else
3784 error = NULL;
3785 if (worktree) {
3786 repo_path =
3787 strdup(got_worktree_get_repo_path(worktree));
3788 if (repo_path == NULL)
3789 error = got_error_from_errno("strdup");
3790 if (error)
3791 goto done;
3792 } else {
3793 repo_path = strdup(cwd);
3794 if (repo_path == NULL) {
3795 error = got_error_from_errno("strdup");
3796 goto done;
3801 init_curses();
3803 error = got_repo_open(&repo, repo_path);
3804 if (error != NULL)
3805 goto done;
3807 error = apply_unveil(got_repo_get_path(repo), NULL);
3808 if (error)
3809 goto done;
3811 if (worktree) {
3812 const char *prefix = got_worktree_get_path_prefix(worktree);
3813 char *p, *worktree_subdir = cwd +
3814 strlen(got_worktree_get_root_path(worktree));
3815 if (asprintf(&p, "%s%s%s%s%s",
3816 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3817 worktree_subdir, worktree_subdir[0] ? "/" : "",
3818 path) == -1) {
3819 error = got_error_from_errno("asprintf");
3820 goto done;
3822 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3823 free(p);
3824 } else {
3825 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3827 if (error)
3828 goto done;
3830 if (commit_id_str == NULL) {
3831 struct got_reference *head_ref;
3832 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3833 if (error != NULL)
3834 goto done;
3835 error = got_ref_resolve(&commit_id, repo, head_ref);
3836 got_ref_close(head_ref);
3837 } else {
3838 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3839 if (error) {
3840 if (error->code != GOT_ERR_NOT_REF)
3841 goto done;
3842 error = got_repo_match_object_id_prefix(&commit_id,
3843 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3846 if (error != NULL)
3847 goto done;
3849 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3850 if (error)
3851 goto done;
3853 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3854 if (view == NULL) {
3855 error = got_error_from_errno("view_open");
3856 goto done;
3858 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3859 if (error)
3860 goto done;
3861 if (worktree) {
3862 /* Release work tree lock. */
3863 got_worktree_close(worktree);
3864 worktree = NULL;
3866 error = view_loop(view);
3867 done:
3868 free(repo_path);
3869 free(cwd);
3870 free(commit_id);
3871 if (worktree)
3872 got_worktree_close(worktree);
3873 if (repo)
3874 got_repo_close(repo);
3875 got_ref_list_free(&refs);
3876 return error;
3879 static const struct got_error *
3880 draw_tree_entries(struct tog_view *view,
3881 struct got_tree_entry **first_displayed_entry,
3882 struct got_tree_entry **last_displayed_entry,
3883 struct got_tree_entry **selected_entry, int *ndisplayed,
3884 const char *label, int show_ids, const char *parent_path,
3885 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3887 const struct got_error *err = NULL;
3888 struct got_tree_entry *te;
3889 wchar_t *wline;
3890 int width, n;
3892 *ndisplayed = 0;
3894 werase(view->window);
3896 if (limit == 0)
3897 return NULL;
3899 err = format_line(&wline, &width, label, view->ncols);
3900 if (err)
3901 return err;
3902 if (view_needs_focus_indication(view))
3903 wstandout(view->window);
3904 waddwstr(view->window, wline);
3905 if (view_needs_focus_indication(view))
3906 wstandend(view->window);
3907 free(wline);
3908 wline = NULL;
3909 if (width < view->ncols - 1)
3910 waddch(view->window, '\n');
3911 if (--limit <= 0)
3912 return NULL;
3913 err = format_line(&wline, &width, parent_path, view->ncols);
3914 if (err)
3915 return err;
3916 waddwstr(view->window, wline);
3917 free(wline);
3918 wline = NULL;
3919 if (width < view->ncols - 1)
3920 waddch(view->window, '\n');
3921 if (--limit <= 0)
3922 return NULL;
3923 waddch(view->window, '\n');
3924 if (--limit <= 0)
3925 return NULL;
3927 te = SIMPLEQ_FIRST(&entries->head);
3928 if (*first_displayed_entry == NULL) {
3929 if (selected == 0) {
3930 if (view->focussed)
3931 wstandout(view->window);
3932 *selected_entry = NULL;
3934 waddstr(view->window, " ..\n"); /* parent directory */
3935 if (selected == 0 && view->focussed)
3936 wstandend(view->window);
3937 (*ndisplayed)++;
3938 if (--limit <= 0)
3939 return NULL;
3940 n = 1;
3941 } else {
3942 n = 0;
3943 while (te != *first_displayed_entry)
3944 te = SIMPLEQ_NEXT(te, entry);
3947 while (te) {
3948 char *line = NULL, *id_str = NULL;
3949 const char *modestr = "";
3951 if (show_ids) {
3952 err = got_object_id_str(&id_str, te->id);
3953 if (err)
3954 return got_error_from_errno(
3955 "got_object_id_str");
3957 if (got_object_tree_entry_is_submodule(te))
3958 modestr = "$";
3959 else if (S_ISLNK(te->mode))
3960 modestr = "@";
3961 else if (S_ISDIR(te->mode))
3962 modestr = "/";
3963 else if (te->mode & S_IXUSR)
3964 modestr = "*";
3965 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3966 te->name, modestr) == -1) {
3967 free(id_str);
3968 return got_error_from_errno("asprintf");
3970 free(id_str);
3971 err = format_line(&wline, &width, line, view->ncols);
3972 if (err) {
3973 free(line);
3974 break;
3976 if (n == selected) {
3977 if (view->focussed)
3978 wstandout(view->window);
3979 *selected_entry = te;
3981 waddwstr(view->window, wline);
3982 if (width < view->ncols - 1)
3983 waddch(view->window, '\n');
3984 if (n == selected && view->focussed)
3985 wstandend(view->window);
3986 free(line);
3987 free(wline);
3988 wline = NULL;
3989 n++;
3990 (*ndisplayed)++;
3991 *last_displayed_entry = te;
3992 if (--limit <= 0)
3993 break;
3994 te = SIMPLEQ_NEXT(te, entry);
3997 return err;
4000 static void
4001 tree_scroll_up(struct tog_view *view,
4002 struct got_tree_entry **first_displayed_entry, int maxscroll,
4003 const struct got_tree_entries *entries, int isroot)
4005 struct got_tree_entry *te, *prev;
4006 int i;
4008 if (*first_displayed_entry == NULL)
4009 return;
4011 te = SIMPLEQ_FIRST(&entries->head);
4012 if (*first_displayed_entry == te) {
4013 if (!isroot)
4014 *first_displayed_entry = NULL;
4015 return;
4018 /* XXX this is stupid... switch to TAILQ? */
4019 for (i = 0; i < maxscroll; i++) {
4020 while (te != *first_displayed_entry) {
4021 prev = te;
4022 te = SIMPLEQ_NEXT(te, entry);
4024 *first_displayed_entry = prev;
4025 te = SIMPLEQ_FIRST(&entries->head);
4027 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4028 *first_displayed_entry = NULL;
4031 static int
4032 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4033 struct got_tree_entry *last_displayed_entry,
4034 const struct got_tree_entries *entries)
4036 struct got_tree_entry *next, *last;
4037 int n = 0;
4039 if (*first_displayed_entry)
4040 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4041 else
4042 next = SIMPLEQ_FIRST(&entries->head);
4043 last = last_displayed_entry;
4044 while (next && last && n++ < maxscroll) {
4045 last = SIMPLEQ_NEXT(last, entry);
4046 if (last) {
4047 *first_displayed_entry = next;
4048 next = SIMPLEQ_NEXT(next, entry);
4051 return n;
4054 static const struct got_error *
4055 tree_entry_path(char **path, struct tog_parent_trees *parents,
4056 struct got_tree_entry *te)
4058 const struct got_error *err = NULL;
4059 struct tog_parent_tree *pt;
4060 size_t len = 2; /* for leading slash and NUL */
4062 TAILQ_FOREACH(pt, parents, entry)
4063 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4064 if (te)
4065 len += strlen(te->name);
4067 *path = calloc(1, len);
4068 if (path == NULL)
4069 return got_error_from_errno("calloc");
4071 (*path)[0] = '/';
4072 pt = TAILQ_LAST(parents, tog_parent_trees);
4073 while (pt) {
4074 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4075 err = got_error(GOT_ERR_NO_SPACE);
4076 goto done;
4078 if (strlcat(*path, "/", len) >= len) {
4079 err = got_error(GOT_ERR_NO_SPACE);
4080 goto done;
4082 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4084 if (te) {
4085 if (strlcat(*path, te->name, len) >= len) {
4086 err = got_error(GOT_ERR_NO_SPACE);
4087 goto done;
4090 done:
4091 if (err) {
4092 free(*path);
4093 *path = NULL;
4095 return err;
4098 static const struct got_error *
4099 blame_tree_entry(struct tog_view **new_view, int begin_x,
4100 struct got_tree_entry *te, struct tog_parent_trees *parents,
4101 struct got_object_id *commit_id, struct got_reflist_head *refs,
4102 struct got_repository *repo)
4104 const struct got_error *err = NULL;
4105 char *path;
4106 struct tog_view *blame_view;
4108 *new_view = NULL;
4110 err = tree_entry_path(&path, parents, te);
4111 if (err)
4112 return err;
4114 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4115 if (blame_view == NULL) {
4116 err = got_error_from_errno("view_open");
4117 goto done;
4120 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4121 if (err) {
4122 if (err->code == GOT_ERR_CANCELLED)
4123 err = NULL;
4124 view_close(blame_view);
4125 } else
4126 *new_view = blame_view;
4127 done:
4128 free(path);
4129 return err;
4132 static const struct got_error *
4133 log_tree_entry(struct tog_view **new_view, int begin_x,
4134 struct got_tree_entry *te, struct tog_parent_trees *parents,
4135 struct got_object_id *commit_id, struct got_reflist_head *refs,
4136 struct got_repository *repo)
4138 struct tog_view *log_view;
4139 const struct got_error *err = NULL;
4140 char *path;
4142 *new_view = NULL;
4144 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4145 if (log_view == NULL)
4146 return got_error_from_errno("view_open");
4148 err = tree_entry_path(&path, parents, te);
4149 if (err)
4150 return err;
4152 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4153 if (err)
4154 view_close(log_view);
4155 else
4156 *new_view = log_view;
4157 free(path);
4158 return err;
4161 static const struct got_error *
4162 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4163 struct got_object_id *commit_id, struct got_reflist_head *refs,
4164 struct got_repository *repo)
4166 const struct got_error *err = NULL;
4167 char *commit_id_str = NULL;
4168 struct tog_tree_view_state *s = &view->state.tree;
4170 TAILQ_INIT(&s->parents);
4172 err = got_object_id_str(&commit_id_str, commit_id);
4173 if (err != NULL)
4174 goto done;
4176 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4177 err = got_error_from_errno("asprintf");
4178 goto done;
4181 s->root = s->tree = root;
4182 s->entries = got_object_tree_get_entries(root);
4183 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4184 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4185 s->commit_id = got_object_id_dup(commit_id);
4186 if (s->commit_id == NULL) {
4187 err = got_error_from_errno("got_object_id_dup");
4188 goto done;
4190 s->refs = refs;
4191 s->repo = repo;
4193 view->show = show_tree_view;
4194 view->input = input_tree_view;
4195 view->close = close_tree_view;
4196 view->search_start = search_start_tree_view;
4197 view->search_next = search_next_tree_view;
4198 done:
4199 free(commit_id_str);
4200 if (err) {
4201 free(s->tree_label);
4202 s->tree_label = NULL;
4204 return err;
4207 static const struct got_error *
4208 close_tree_view(struct tog_view *view)
4210 struct tog_tree_view_state *s = &view->state.tree;
4212 free(s->tree_label);
4213 s->tree_label = NULL;
4214 free(s->commit_id);
4215 s->commit_id = NULL;
4216 while (!TAILQ_EMPTY(&s->parents)) {
4217 struct tog_parent_tree *parent;
4218 parent = TAILQ_FIRST(&s->parents);
4219 TAILQ_REMOVE(&s->parents, parent, entry);
4220 free(parent);
4223 if (s->tree != s->root)
4224 got_object_tree_close(s->tree);
4225 got_object_tree_close(s->root);
4227 return NULL;
4230 static const struct got_error *
4231 search_start_tree_view(struct tog_view *view)
4233 struct tog_tree_view_state *s = &view->state.tree;
4235 s->matched_entry = NULL;
4236 return NULL;
4239 static int
4240 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4242 regmatch_t regmatch;
4244 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4247 static const struct got_error *
4248 search_next_tree_view(struct tog_view *view)
4250 struct tog_tree_view_state *s = &view->state.tree;
4251 struct got_tree_entry *entry = NULL, *te;
4253 if (!view->searching) {
4254 view->search_next_done = 1;
4255 return NULL;
4258 if (s->matched_entry) {
4259 if (view->searching == TOG_SEARCH_FORWARD) {
4260 if (s->selected_entry)
4261 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4262 else
4263 entry = SIMPLEQ_FIRST(&s->entries->head);
4265 else {
4266 if (s->selected_entry == NULL) {
4267 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4268 entry = te;
4269 } else {
4270 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4271 entry = te;
4272 if (SIMPLEQ_NEXT(te, entry) ==
4273 s->selected_entry)
4274 break;
4278 } else {
4279 if (view->searching == TOG_SEARCH_FORWARD)
4280 entry = SIMPLEQ_FIRST(&s->entries->head);
4281 else {
4282 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4283 entry = te;
4287 while (1) {
4288 if (entry == NULL) {
4289 if (s->matched_entry == NULL) {
4290 view->search_next_done = 1;
4291 return NULL;
4293 if (view->searching == TOG_SEARCH_FORWARD)
4294 entry = SIMPLEQ_FIRST(&s->entries->head);
4295 else {
4296 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4297 entry = te;
4301 if (match_tree_entry(entry, &view->regex)) {
4302 view->search_next_done = 1;
4303 s->matched_entry = entry;
4304 break;
4307 if (view->searching == TOG_SEARCH_FORWARD)
4308 entry = SIMPLEQ_NEXT(entry, entry);
4309 else {
4310 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4311 entry = NULL;
4312 else {
4313 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4314 if (SIMPLEQ_NEXT(te, entry) == entry) {
4315 entry = te;
4316 break;
4323 if (s->matched_entry) {
4324 s->first_displayed_entry = s->matched_entry;
4325 s->selected = 0;
4328 return NULL;
4331 static const struct got_error *
4332 show_tree_view(struct tog_view *view)
4334 const struct got_error *err = NULL;
4335 struct tog_tree_view_state *s = &view->state.tree;
4336 char *parent_path;
4338 err = tree_entry_path(&parent_path, &s->parents, NULL);
4339 if (err)
4340 return err;
4342 err = draw_tree_entries(view, &s->first_displayed_entry,
4343 &s->last_displayed_entry, &s->selected_entry,
4344 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4345 s->entries, s->selected, view->nlines, s->tree == s->root);
4346 free(parent_path);
4348 view_vborder(view);
4349 return err;
4352 static const struct got_error *
4353 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4354 struct tog_view **focus_view, struct tog_view *view, int ch)
4356 const struct got_error *err = NULL;
4357 struct tog_tree_view_state *s = &view->state.tree;
4358 struct tog_view *log_view;
4359 int begin_x = 0, nscrolled;
4361 switch (ch) {
4362 case 'i':
4363 s->show_ids = !s->show_ids;
4364 break;
4365 case 'l':
4366 if (!s->selected_entry)
4367 break;
4368 if (view_is_parent_view(view))
4369 begin_x = view_split_begin_x(view->begin_x);
4370 err = log_tree_entry(&log_view, begin_x,
4371 s->selected_entry, &s->parents,
4372 s->commit_id, s->refs, s->repo);
4373 if (view_is_parent_view(view)) {
4374 err = view_close_child(view);
4375 if (err)
4376 return err;
4377 err = view_set_child(view, log_view);
4378 if (err) {
4379 view_close(log_view);
4380 break;
4382 *focus_view = log_view;
4383 view->child_focussed = 1;
4384 } else
4385 *new_view = log_view;
4386 break;
4387 case 'k':
4388 case KEY_UP:
4389 if (s->selected > 0) {
4390 s->selected--;
4391 if (s->selected == 0)
4392 break;
4394 if (s->selected > 0)
4395 break;
4396 tree_scroll_up(view, &s->first_displayed_entry, 1,
4397 s->entries, s->tree == s->root);
4398 break;
4399 case KEY_PPAGE:
4400 tree_scroll_up(view, &s->first_displayed_entry,
4401 MAX(0, view->nlines - 4 - s->selected), s->entries,
4402 s->tree == s->root);
4403 s->selected = 0;
4404 if (SIMPLEQ_FIRST(&s->entries->head) ==
4405 s->first_displayed_entry && s->tree != s->root)
4406 s->first_displayed_entry = NULL;
4407 break;
4408 case 'j':
4409 case KEY_DOWN:
4410 if (s->selected < s->ndisplayed - 1) {
4411 s->selected++;
4412 break;
4414 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4415 /* can't scroll any further */
4416 break;
4417 tree_scroll_down(&s->first_displayed_entry, 1,
4418 s->last_displayed_entry, s->entries);
4419 break;
4420 case KEY_NPAGE:
4421 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4422 == NULL) {
4423 /* can't scroll any further; move cursor down */
4424 if (s->selected < s->ndisplayed - 1)
4425 s->selected = s->ndisplayed - 1;
4426 break;
4428 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4429 view->nlines, s->last_displayed_entry, s->entries);
4430 if (nscrolled < view->nlines) {
4431 int ndisplayed = 0;
4432 struct got_tree_entry *te;
4433 te = s->first_displayed_entry;
4434 do {
4435 ndisplayed++;
4436 te = SIMPLEQ_NEXT(te, entry);
4437 } while (te);
4438 s->selected = ndisplayed - 1;
4440 break;
4441 case KEY_ENTER:
4442 case '\r':
4443 case KEY_BACKSPACE:
4444 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4445 struct tog_parent_tree *parent;
4446 /* user selected '..' */
4447 if (s->tree == s->root)
4448 break;
4449 parent = TAILQ_FIRST(&s->parents);
4450 TAILQ_REMOVE(&s->parents, parent,
4451 entry);
4452 got_object_tree_close(s->tree);
4453 s->tree = parent->tree;
4454 s->entries =
4455 got_object_tree_get_entries(s->tree);
4456 s->first_displayed_entry =
4457 parent->first_displayed_entry;
4458 s->selected_entry =
4459 parent->selected_entry;
4460 s->selected = parent->selected;
4461 free(parent);
4462 } else if (S_ISDIR(s->selected_entry->mode)) {
4463 struct got_tree_object *subtree;
4464 err = got_object_open_as_tree(&subtree,
4465 s->repo, s->selected_entry->id);
4466 if (err)
4467 break;
4468 err = tree_view_visit_subtree(subtree, s);
4469 if (err) {
4470 got_object_tree_close(subtree);
4471 break;
4473 } else if (S_ISREG(s->selected_entry->mode)) {
4474 struct tog_view *blame_view;
4475 int begin_x = view_is_parent_view(view) ?
4476 view_split_begin_x(view->begin_x) : 0;
4478 err = blame_tree_entry(&blame_view, begin_x,
4479 s->selected_entry, &s->parents,
4480 s->commit_id, s->refs, s->repo);
4481 if (err)
4482 break;
4483 if (view_is_parent_view(view)) {
4484 err = view_close_child(view);
4485 if (err)
4486 return err;
4487 err = view_set_child(view, blame_view);
4488 if (err) {
4489 view_close(blame_view);
4490 break;
4492 *focus_view = blame_view;
4493 view->child_focussed = 1;
4494 } else
4495 *new_view = blame_view;
4497 break;
4498 case KEY_RESIZE:
4499 if (s->selected > view->nlines)
4500 s->selected = s->ndisplayed - 1;
4501 break;
4502 default:
4503 break;
4506 return err;
4509 __dead static void
4510 usage_tree(void)
4512 endwin();
4513 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4514 getprogname());
4515 exit(1);
4518 static const struct got_error *
4519 cmd_tree(int argc, char *argv[])
4521 const struct got_error *error;
4522 struct got_repository *repo = NULL;
4523 struct got_reflist_head refs;
4524 char *repo_path = NULL;
4525 struct got_object_id *commit_id = NULL;
4526 char *commit_id_arg = NULL;
4527 struct got_commit_object *commit = NULL;
4528 struct got_tree_object *tree = NULL;
4529 int ch;
4530 struct tog_view *view;
4532 SIMPLEQ_INIT(&refs);
4534 #ifndef PROFILE
4535 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4536 NULL) == -1)
4537 err(1, "pledge");
4538 #endif
4540 while ((ch = getopt(argc, argv, "c:")) != -1) {
4541 switch (ch) {
4542 case 'c':
4543 commit_id_arg = optarg;
4544 break;
4545 default:
4546 usage_tree();
4547 /* NOTREACHED */
4551 argc -= optind;
4552 argv += optind;
4554 if (argc == 0) {
4555 struct got_worktree *worktree;
4556 char *cwd = getcwd(NULL, 0);
4557 if (cwd == NULL)
4558 return got_error_from_errno("getcwd");
4559 error = got_worktree_open(&worktree, cwd);
4560 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4561 goto done;
4562 if (worktree) {
4563 free(cwd);
4564 repo_path =
4565 strdup(got_worktree_get_repo_path(worktree));
4566 got_worktree_close(worktree);
4567 } else
4568 repo_path = cwd;
4569 if (repo_path == NULL) {
4570 error = got_error_from_errno("strdup");
4571 goto done;
4573 } else if (argc == 1) {
4574 repo_path = realpath(argv[0], NULL);
4575 if (repo_path == NULL)
4576 return got_error_from_errno2("realpath", argv[0]);
4577 } else
4578 usage_log();
4580 init_curses();
4582 error = got_repo_open(&repo, repo_path);
4583 if (error != NULL)
4584 goto done;
4586 error = apply_unveil(got_repo_get_path(repo), NULL);
4587 if (error)
4588 goto done;
4590 if (commit_id_arg == NULL)
4591 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4592 else {
4593 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4594 if (error) {
4595 if (error->code != GOT_ERR_NOT_REF)
4596 goto done;
4597 error = got_repo_match_object_id_prefix(&commit_id,
4598 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4601 if (error != NULL)
4602 goto done;
4604 error = got_object_open_as_commit(&commit, repo, commit_id);
4605 if (error != NULL)
4606 goto done;
4608 error = got_object_open_as_tree(&tree, repo,
4609 got_object_commit_get_tree_id(commit));
4610 if (error != NULL)
4611 goto done;
4613 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4614 if (error)
4615 goto done;
4617 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4618 if (view == NULL) {
4619 error = got_error_from_errno("view_open");
4620 goto done;
4622 error = open_tree_view(view, tree, commit_id, &refs, repo);
4623 if (error)
4624 goto done;
4625 error = view_loop(view);
4626 done:
4627 free(repo_path);
4628 free(commit_id);
4629 if (commit)
4630 got_object_commit_close(commit);
4631 if (tree)
4632 got_object_tree_close(tree);
4633 if (repo)
4634 got_repo_close(repo);
4635 got_ref_list_free(&refs);
4636 return error;
4639 static void
4640 list_commands(void)
4642 int i;
4644 fprintf(stderr, "commands:");
4645 for (i = 0; i < nitems(tog_commands); i++) {
4646 struct tog_cmd *cmd = &tog_commands[i];
4647 fprintf(stderr, " %s", cmd->name);
4649 fputc('\n', stderr);
4652 __dead static void
4653 usage(int hflag)
4655 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4656 getprogname());
4657 if (hflag)
4658 list_commands();
4659 exit(1);
4662 static char **
4663 make_argv(const char *arg0, const char *arg1)
4665 char **argv;
4666 int argc = (arg1 == NULL ? 1 : 2);
4668 argv = calloc(argc, sizeof(char *));
4669 if (argv == NULL)
4670 err(1, "calloc");
4671 argv[0] = strdup(arg0);
4672 if (argv[0] == NULL)
4673 err(1, "calloc");
4674 if (arg1) {
4675 argv[1] = strdup(arg1);
4676 if (argv[1] == NULL)
4677 err(1, "calloc");
4680 return argv;
4683 int
4684 main(int argc, char *argv[])
4686 const struct got_error *error = NULL;
4687 struct tog_cmd *cmd = NULL;
4688 int ch, hflag = 0, Vflag = 0;
4689 char **cmd_argv = NULL;
4691 setlocale(LC_CTYPE, "");
4693 while ((ch = getopt(argc, argv, "hV")) != -1) {
4694 switch (ch) {
4695 case 'h':
4696 hflag = 1;
4697 break;
4698 case 'V':
4699 Vflag = 1;
4700 break;
4701 default:
4702 usage(hflag);
4703 /* NOTREACHED */
4707 argc -= optind;
4708 argv += optind;
4709 optind = 0;
4710 optreset = 1;
4712 if (Vflag) {
4713 got_version_print_str();
4714 return 1;
4717 if (argc == 0) {
4718 if (hflag)
4719 usage(hflag);
4720 /* Build an argument vector which runs a default command. */
4721 cmd = &tog_commands[0];
4722 cmd_argv = make_argv(cmd->name, NULL);
4723 argc = 1;
4724 } else {
4725 int i;
4727 /* Did the user specific a command? */
4728 for (i = 0; i < nitems(tog_commands); i++) {
4729 if (strncmp(tog_commands[i].name, argv[0],
4730 strlen(argv[0])) == 0) {
4731 cmd = &tog_commands[i];
4732 break;
4736 if (cmd == NULL) {
4737 fprintf(stderr, "%s: unknown command '%s'\n",
4738 getprogname(), argv[0]);
4739 list_commands();
4740 return 1;
4744 if (hflag)
4745 cmd->cmd_usage();
4746 else
4747 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4749 endwin();
4750 free(cmd_argv);
4751 if (error && error->code != GOT_ERR_CANCELLED)
4752 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4753 return 0;