Blob


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