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_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_diff.h"
46 #include "got_opentemp.h"
47 #include "got_commit_graph.h"
48 #include "got_utf8.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_path.h"
52 #include "got_worktree.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 #define CTRL(x) ((x) & 0x1f)
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 struct tog_cmd {
69 const char *name;
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
72 const char *descr;
73 };
75 __dead static void usage(void);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
86 static struct tog_cmd tog_commands[] = {
87 { "log", cmd_log, usage_log,
88 "show repository history" },
89 { "diff", cmd_diff, usage_diff,
90 "compare files and directories" },
91 { "blame", cmd_blame, usage_blame,
92 "show line-by-line file history" },
93 { "tree", cmd_tree, usage_tree,
94 "browse trees in repository" },
95 };
97 enum tog_view_type {
98 TOG_VIEW_DIFF,
99 TOG_VIEW_LOG,
100 TOG_VIEW_BLAME,
101 TOG_VIEW_TREE
102 };
104 #define TOG_EOF_STRING "(END)"
106 struct commit_queue_entry {
107 TAILQ_ENTRY(commit_queue_entry) entry;
108 struct got_object_id *id;
109 struct got_commit_object *commit;
110 int idx;
111 };
112 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 struct commit_queue {
114 int ncommits;
115 struct commit_queue_head head;
116 };
118 struct tog_diff_view_state {
119 struct got_object_id *id1, *id2;
120 FILE *f;
121 int first_displayed_line;
122 int last_displayed_line;
123 int eof;
124 int diff_context;
125 struct got_repository *repo;
126 struct got_reflist_head *refs;
128 /* passed from log view; may be NULL */
129 struct tog_view *log_view;
130 };
132 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
134 struct tog_log_thread_args {
135 pthread_cond_t need_commits;
136 int commits_needed;
137 struct got_commit_graph *graph;
138 struct commit_queue *commits;
139 const char *in_repo_path;
140 struct got_object_id *start_id;
141 struct got_repository *repo;
142 int log_complete;
143 sig_atomic_t *quit;
144 struct tog_view *view;
145 struct commit_queue_entry **first_displayed_entry;
146 struct commit_queue_entry **selected_entry;
147 };
149 struct tog_log_view_state {
150 struct commit_queue commits;
151 struct commit_queue_entry *first_displayed_entry;
152 struct commit_queue_entry *last_displayed_entry;
153 struct commit_queue_entry *selected_entry;
154 int selected;
155 char *in_repo_path;
156 struct got_repository *repo;
157 struct got_reflist_head *refs;
158 struct got_object_id *start_id;
159 sig_atomic_t quit;
160 pthread_t thread;
161 struct tog_log_thread_args thread_args;
162 struct commit_queue_entry *matched_entry;
163 };
165 struct tog_blame_cb_args {
166 struct tog_blame_line *lines; /* one per line */
167 int nlines;
169 struct tog_view *view;
170 struct got_object_id *commit_id;
171 int *quit;
172 };
174 struct tog_blame_thread_args {
175 const char *path;
176 struct got_repository *repo;
177 struct tog_blame_cb_args *cb_args;
178 int *complete;
179 };
181 struct tog_blame {
182 FILE *f;
183 size_t filesize;
184 struct tog_blame_line *lines;
185 int nlines;
186 pthread_t thread;
187 struct tog_blame_thread_args thread_args;
188 struct tog_blame_cb_args cb_args;
189 const char *path;
190 };
192 struct tog_blame_view_state {
193 int first_displayed_line;
194 int last_displayed_line;
195 int selected_line;
196 int blame_complete;
197 int eof;
198 int done;
199 struct got_object_id_queue blamed_commits;
200 struct got_object_qid *blamed_commit;
201 char *path;
202 struct got_repository *repo;
203 struct got_reflist_head *refs;
204 struct got_object_id *commit_id;
205 struct tog_blame blame;
206 };
208 struct tog_parent_tree {
209 TAILQ_ENTRY(tog_parent_tree) entry;
210 struct got_tree_object *tree;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *selected_entry;
213 int selected;
214 };
216 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 struct tog_tree_view_state {
219 char *tree_label;
220 struct got_tree_object *root;
221 struct got_tree_object *tree;
222 const struct got_tree_entries *entries;
223 struct got_tree_entry *first_displayed_entry;
224 struct got_tree_entry *last_displayed_entry;
225 struct got_tree_entry *selected_entry;
226 int ndisplayed, selected, show_ids;
227 struct tog_parent_trees parents;
228 struct got_object_id *commit_id;
229 struct got_repository *repo;
230 struct got_reflist_head *refs;
231 };
233 /*
234 * We implement two types of views: parent views and child views.
236 * The 'Tab' key switches between a parent view and its child view.
237 * Child views are shown side-by-side to their parent view, provided
238 * there is enough screen estate.
240 * When a new view is opened from within a parent view, this new view
241 * becomes a child view of the parent view, replacing any existing child.
243 * When a new view is opened from within a child view, this new view
244 * becomes a parent view which will obscure the views below until the
245 * user quits the new parent view by typing 'q'.
247 * This list of views contains parent views only.
248 * Child views are only pointed to by their parent view.
249 */
250 TAILQ_HEAD(tog_view_list_head, tog_view);
252 struct tog_view {
253 TAILQ_ENTRY(tog_view) entry;
254 WINDOW *window;
255 PANEL *panel;
256 int nlines, ncols, begin_y, begin_x;
257 int lines, cols; /* copies of LINES and COLS */
258 int focussed;
259 struct tog_view *parent;
260 struct tog_view *child;
261 int child_focussed;
263 /* type-specific state */
264 enum tog_view_type type;
265 union {
266 struct tog_diff_view_state diff;
267 struct tog_log_view_state log;
268 struct tog_blame_view_state blame;
269 struct tog_tree_view_state tree;
270 } state;
272 const struct got_error *(*show)(struct tog_view *);
273 const struct got_error *(*input)(struct tog_view **,
274 struct tog_view **, struct tog_view**, struct tog_view *, int);
275 const struct got_error *(*close)(struct tog_view *);
277 const struct got_error *(*search_start)(struct tog_view *);
278 const struct got_error *(*search_next)(struct tog_view *);
279 int searching;
280 #define TOG_SEARCH_FORWARD 1
281 #define TOG_SEARCH_BACKWARD 2
282 int search_next_done;
283 regex_t regex;
284 };
286 static const struct got_error *open_diff_view(struct tog_view *,
287 struct got_object_id *, struct got_object_id *, struct tog_view *,
288 struct got_reflist_head *, struct got_repository *);
289 static const struct got_error *show_diff_view(struct tog_view *);
290 static const struct got_error *input_diff_view(struct tog_view **,
291 struct tog_view **, struct tog_view **, struct tog_view *, int);
292 static const struct got_error* close_diff_view(struct tog_view *);
294 static const struct got_error *open_log_view(struct tog_view *,
295 struct got_object_id *, struct got_reflist_head *,
296 struct got_repository *, const char *, int);
297 static const struct got_error * show_log_view(struct tog_view *);
298 static const struct got_error *input_log_view(struct tog_view **,
299 struct tog_view **, struct tog_view **, struct tog_view *, int);
300 static const struct got_error *close_log_view(struct tog_view *);
301 static const struct got_error *search_start_log_view(struct tog_view *);
302 static const struct got_error *search_next_log_view(struct tog_view *);
304 static const struct got_error *open_blame_view(struct tog_view *, char *,
305 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
306 static const struct got_error *show_blame_view(struct tog_view *);
307 static const struct got_error *input_blame_view(struct tog_view **,
308 struct tog_view **, struct tog_view **, struct tog_view *, int);
309 static const struct got_error *close_blame_view(struct tog_view *);
311 static const struct got_error *open_tree_view(struct tog_view *,
312 struct got_tree_object *, struct got_object_id *,
313 struct got_reflist_head *, struct got_repository *);
314 static const struct got_error *show_tree_view(struct tog_view *);
315 static const struct got_error *input_tree_view(struct tog_view **,
316 struct tog_view **, struct tog_view **, struct tog_view *, int);
317 static const struct got_error *close_tree_view(struct tog_view *);
319 static volatile sig_atomic_t tog_sigwinch_received;
321 static void
322 tog_sigwinch(int signo)
324 tog_sigwinch_received = 1;
327 static const struct got_error *
328 view_close(struct tog_view *view)
330 const struct got_error *err = NULL;
332 if (view->child) {
333 view_close(view->child);
334 view->child = NULL;
336 if (view->close)
337 err = view->close(view);
338 if (view->panel)
339 del_panel(view->panel);
340 if (view->window)
341 delwin(view->window);
342 free(view);
343 return err;
346 static struct tog_view *
347 view_open(int nlines, int ncols, int begin_y, int begin_x,
348 enum tog_view_type type)
350 struct tog_view *view = calloc(1, sizeof(*view));
352 if (view == NULL)
353 return NULL;
355 view->type = type;
356 view->lines = LINES;
357 view->cols = COLS;
358 view->nlines = nlines ? nlines : LINES - begin_y;
359 view->ncols = ncols ? ncols : COLS - begin_x;
360 view->begin_y = begin_y;
361 view->begin_x = begin_x;
362 view->window = newwin(nlines, ncols, begin_y, begin_x);
363 if (view->window == NULL) {
364 view_close(view);
365 return NULL;
367 view->panel = new_panel(view->window);
368 if (view->panel == NULL ||
369 set_panel_userptr(view->panel, view) != OK) {
370 view_close(view);
371 return NULL;
374 keypad(view->window, TRUE);
375 return view;
378 static int
379 view_split_begin_x(int begin_x)
381 if (begin_x > 0 || COLS < 120)
382 return 0;
383 return (COLS - MAX(COLS / 2, 80));
386 static const struct got_error *view_resize(struct tog_view *);
388 static const struct got_error *
389 view_splitscreen(struct tog_view *view)
391 const struct got_error *err = NULL;
393 view->begin_y = 0;
394 view->begin_x = view_split_begin_x(0);
395 view->nlines = LINES;
396 view->ncols = COLS - view->begin_x;
397 view->lines = LINES;
398 view->cols = COLS;
399 err = view_resize(view);
400 if (err)
401 return err;
403 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
404 return got_error_from_errno("mvwin");
406 return NULL;
409 static const struct got_error *
410 view_fullscreen(struct tog_view *view)
412 const struct got_error *err = NULL;
414 view->begin_x = 0;
415 view->begin_y = 0;
416 view->nlines = LINES;
417 view->ncols = COLS;
418 view->lines = LINES;
419 view->cols = COLS;
420 err = view_resize(view);
421 if (err)
422 return err;
424 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
425 return got_error_from_errno("mvwin");
427 return NULL;
430 static int
431 view_is_parent_view(struct tog_view *view)
433 return view->parent == NULL;
436 static const struct got_error *
437 view_resize(struct tog_view *view)
439 int nlines, ncols;
441 if (view->lines > LINES)
442 nlines = view->nlines - (view->lines - LINES);
443 else
444 nlines = view->nlines + (LINES - view->lines);
446 if (view->cols > COLS)
447 ncols = view->ncols - (view->cols - COLS);
448 else
449 ncols = view->ncols + (COLS - view->cols);
451 if (wresize(view->window, nlines, ncols) == ERR)
452 return got_error_from_errno("wresize");
453 if (replace_panel(view->panel, view->window) == ERR)
454 return got_error_from_errno("replace_panel");
455 wclear(view->window);
457 view->nlines = nlines;
458 view->ncols = ncols;
459 view->lines = LINES;
460 view->cols = COLS;
462 if (view->child) {
463 view->child->begin_x = view_split_begin_x(view->begin_x);
464 if (view->child->begin_x == 0) {
465 view_fullscreen(view->child);
466 if (view->child->focussed)
467 show_panel(view->child->panel);
468 else
469 show_panel(view->panel);
470 } else {
471 view_splitscreen(view->child);
472 show_panel(view->child->panel);
476 return NULL;
479 static const struct got_error *
480 view_close_child(struct tog_view *view)
482 const struct got_error *err = NULL;
484 if (view->child == NULL)
485 return NULL;
487 err = view_close(view->child);
488 view->child = NULL;
489 return err;
492 static const struct got_error *
493 view_set_child(struct tog_view *view, struct tog_view *child)
495 const struct got_error *err = NULL;
497 view->child = child;
498 child->parent = view;
499 return err;
502 static int
503 view_is_splitscreen(struct tog_view *view)
505 return view->begin_x > 0;
508 static void
509 tog_resizeterm(void)
511 int cols, lines;
512 struct winsize size;
514 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
515 cols = 80; /* Default */
516 lines = 24;
517 } else {
518 cols = size.ws_col;
519 lines = size.ws_row;
521 resize_term(lines, cols);
524 static const struct got_error *
525 view_search_start(struct tog_view *view)
527 char pattern[1024];
528 int ret;
530 if (view->nlines < 1)
531 return NULL;
533 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
534 view->begin_x, "/");
535 wclrtoeol(view->window);
537 nocbreak();
538 echo();
539 ret = wgetnstr(view->window, pattern, sizeof(pattern));
540 cbreak();
541 noecho();
542 if (ret == ERR)
543 return NULL;
545 if (view->searching) {
546 regfree(&view->regex);
547 view->searching = 0;
550 if (regcomp(&view->regex, pattern,
551 REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE) == 0) {
552 view->search_start(view);
553 view->searching = TOG_SEARCH_FORWARD;
554 view->search_next_done = 0;
555 view->search_next(view);
558 return NULL;
561 static const struct got_error *
562 view_input(struct tog_view **new, struct tog_view **dead,
563 struct tog_view **focus, int *done, struct tog_view *view,
564 struct tog_view_list_head *views)
566 const struct got_error *err = NULL;
567 struct tog_view *v;
568 int ch, errcode;
570 *new = NULL;
571 *dead = NULL;
572 *focus = NULL;
574 if (view->searching && !view->search_next_done) {
575 errcode = pthread_mutex_unlock(&tog_mutex);
576 if (errcode)
577 return got_error_set_errno(errcode,
578 "pthread_mutex_unlock");
579 pthread_yield();
580 errcode = pthread_mutex_lock(&tog_mutex);
581 if (errcode)
582 return got_error_set_errno(errcode,
583 "pthread_mutex_lock");
584 view->search_next(view);
585 return NULL;
588 nodelay(stdscr, FALSE);
589 /* Allow threads to make progress while we are waiting for input. */
590 errcode = pthread_mutex_unlock(&tog_mutex);
591 if (errcode)
592 return got_error_set_errno(errcode, "pthread_mutex_unlock");
593 ch = wgetch(view->window);
594 errcode = pthread_mutex_lock(&tog_mutex);
595 if (errcode)
596 return got_error_set_errno(errcode, "pthread_mutex_lock");
597 nodelay(stdscr, TRUE);
599 if (tog_sigwinch_received) {
600 tog_resizeterm();
601 tog_sigwinch_received = 0;
602 TAILQ_FOREACH(v, views, entry) {
603 err = view_resize(v);
604 if (err)
605 return err;
606 err = v->input(new, dead, focus, v, KEY_RESIZE);
607 if (err)
608 return err;
612 switch (ch) {
613 case ERR:
614 break;
615 case '\t':
616 if (view->child) {
617 *focus = view->child;
618 view->child_focussed = 1;
619 } else if (view->parent) {
620 *focus = view->parent;
621 view->parent->child_focussed = 0;
623 break;
624 case 'q':
625 err = view->input(new, dead, focus, view, ch);
626 *dead = view;
627 break;
628 case 'Q':
629 *done = 1;
630 break;
631 case 'f':
632 if (view_is_parent_view(view)) {
633 if (view->child == NULL)
634 break;
635 if (view_is_splitscreen(view->child)) {
636 *focus = view->child;
637 view->child_focussed = 1;
638 err = view_fullscreen(view->child);
639 } else
640 err = view_splitscreen(view->child);
641 if (err)
642 break;
643 err = view->child->input(new, dead, focus,
644 view->child, KEY_RESIZE);
645 } else {
646 if (view_is_splitscreen(view)) {
647 *focus = view;
648 view->parent->child_focussed = 1;
649 err = view_fullscreen(view);
650 } else {
651 err = view_splitscreen(view);
653 if (err)
654 break;
655 err = view->input(new, dead, focus, view,
656 KEY_RESIZE);
658 break;
659 case KEY_RESIZE:
660 break;
661 case '/':
662 if (view->search_start)
663 view_search_start(view);
664 else
665 err = view->input(new, dead, focus, view, ch);
666 break;
667 case 'N':
668 case 'n':
669 if (view->search_next && view->searching) {
670 view->searching = (ch == 'n' ?
671 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
672 view->search_next_done = 0;
673 view->search_next(view);
674 } else
675 err = view->input(new, dead, focus, view, ch);
676 break;
677 default:
678 err = view->input(new, dead, focus, view, ch);
679 break;
682 return err;
685 void
686 view_vborder(struct tog_view *view)
688 PANEL *panel;
689 struct tog_view *view_above;
691 if (view->parent)
692 return view_vborder(view->parent);
694 panel = panel_above(view->panel);
695 if (panel == NULL)
696 return;
698 view_above = panel_userptr(panel);
699 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
700 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
703 int
704 view_needs_focus_indication(struct tog_view *view)
706 if (view_is_parent_view(view)) {
707 if (view->child == NULL || view->child_focussed)
708 return 0;
709 if (!view_is_splitscreen(view->child))
710 return 0;
711 } else if (!view_is_splitscreen(view))
712 return 0;
714 return view->focussed;
717 static const struct got_error *
718 view_loop(struct tog_view *view)
720 const struct got_error *err = NULL;
721 struct tog_view_list_head views;
722 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
723 int fast_refresh = 10;
724 int done = 0, errcode;
726 errcode = pthread_mutex_lock(&tog_mutex);
727 if (errcode)
728 return got_error_set_errno(errcode, "pthread_mutex_lock");
730 TAILQ_INIT(&views);
731 TAILQ_INSERT_HEAD(&views, view, entry);
733 main_view = view;
734 view->focussed = 1;
735 err = view->show(view);
736 if (err)
737 return err;
738 update_panels();
739 doupdate();
740 while (!TAILQ_EMPTY(&views) && !done) {
741 /* Refresh fast during initialization, then become slower. */
742 if (fast_refresh && fast_refresh-- == 0)
743 halfdelay(10); /* switch to once per second */
745 err = view_input(&new_view, &dead_view, &focus_view, &done,
746 view, &views);
747 if (err)
748 break;
749 if (dead_view) {
750 struct tog_view *prev = NULL;
752 if (view_is_parent_view(dead_view))
753 prev = TAILQ_PREV(dead_view,
754 tog_view_list_head, entry);
755 else if (view->parent != dead_view)
756 prev = view->parent;
758 if (dead_view->parent)
759 dead_view->parent->child = NULL;
760 else
761 TAILQ_REMOVE(&views, dead_view, entry);
763 err = view_close(dead_view);
764 if (err || dead_view == main_view)
765 goto done;
767 if (view == dead_view) {
768 if (focus_view)
769 view = focus_view;
770 else if (prev)
771 view = prev;
772 else if (!TAILQ_EMPTY(&views))
773 view = TAILQ_LAST(&views,
774 tog_view_list_head);
775 else
776 view = NULL;
777 if (view) {
778 if (view->child && view->child_focussed)
779 focus_view = view->child;
780 else
781 focus_view = view;
785 if (new_view) {
786 struct tog_view *v, *t;
787 /* Only allow one parent view per type. */
788 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
789 if (v->type != new_view->type)
790 continue;
791 TAILQ_REMOVE(&views, v, entry);
792 err = view_close(v);
793 if (err)
794 goto done;
795 break;
797 TAILQ_INSERT_TAIL(&views, new_view, entry);
798 view = new_view;
799 if (focus_view == NULL)
800 focus_view = new_view;
802 if (focus_view) {
803 show_panel(focus_view->panel);
804 if (view)
805 view->focussed = 0;
806 focus_view->focussed = 1;
807 view = focus_view;
808 if (new_view)
809 show_panel(new_view->panel);
810 if (view->child && view_is_splitscreen(view->child))
811 show_panel(view->child->panel);
813 if (view) {
814 if (focus_view == NULL) {
815 view->focussed = 1;
816 show_panel(view->panel);
817 if (view->child && view_is_splitscreen(view->child))
818 show_panel(view->child->panel);
819 focus_view = view;
821 if (view->parent) {
822 err = view->parent->show(view->parent);
823 if (err)
824 goto done;
826 err = view->show(view);
827 if (err)
828 goto done;
829 if (view->child) {
830 err = view->child->show(view->child);
831 if (err)
832 goto done;
834 update_panels();
835 doupdate();
838 done:
839 while (!TAILQ_EMPTY(&views)) {
840 view = TAILQ_FIRST(&views);
841 TAILQ_REMOVE(&views, view, entry);
842 view_close(view);
845 errcode = pthread_mutex_unlock(&tog_mutex);
846 if (errcode)
847 return got_error_set_errno(errcode, "pthread_mutex_unlock");
849 return err;
852 __dead static void
853 usage_log(void)
855 endwin();
856 fprintf(stderr,
857 "usage: %s log [-c commit] [-r repository-path] [path]\n",
858 getprogname());
859 exit(1);
862 /* Create newly allocated wide-character string equivalent to a byte string. */
863 static const struct got_error *
864 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
866 char *vis = NULL;
867 const struct got_error *err = NULL;
869 *ws = NULL;
870 *wlen = mbstowcs(NULL, s, 0);
871 if (*wlen == (size_t)-1) {
872 int vislen;
873 if (errno != EILSEQ)
874 return got_error_from_errno("mbstowcs");
876 /* byte string invalid in current encoding; try to "fix" it */
877 err = got_mbsavis(&vis, &vislen, s);
878 if (err)
879 return err;
880 *wlen = mbstowcs(NULL, vis, 0);
881 if (*wlen == (size_t)-1) {
882 err = got_error_from_errno("mbstowcs"); /* give up */
883 goto done;
887 *ws = calloc(*wlen + 1, sizeof(*ws));
888 if (*ws == NULL) {
889 err = got_error_from_errno("calloc");
890 goto done;
893 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
894 err = got_error_from_errno("mbstowcs");
895 done:
896 free(vis);
897 if (err) {
898 free(*ws);
899 *ws = NULL;
900 *wlen = 0;
902 return err;
905 /* Format a line for display, ensuring that it won't overflow a width limit. */
906 static const struct got_error *
907 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
909 const struct got_error *err = NULL;
910 int cols = 0;
911 wchar_t *wline = NULL;
912 size_t wlen;
913 int i;
915 *wlinep = NULL;
916 *widthp = 0;
918 err = mbs2ws(&wline, &wlen, line);
919 if (err)
920 return err;
922 i = 0;
923 while (i < wlen && cols < wlimit) {
924 int width = wcwidth(wline[i]);
925 switch (width) {
926 case 0:
927 i++;
928 break;
929 case 1:
930 case 2:
931 if (cols + width <= wlimit)
932 cols += width;
933 i++;
934 break;
935 case -1:
936 if (wline[i] == L'\t')
937 cols += TABSIZE - ((cols + 1) % TABSIZE);
938 i++;
939 break;
940 default:
941 err = got_error_from_errno("wcwidth");
942 goto done;
945 wline[i] = L'\0';
946 if (widthp)
947 *widthp = cols;
948 done:
949 if (err)
950 free(wline);
951 else
952 *wlinep = wline;
953 return err;
956 static const struct got_error*
957 build_refs_str(char **refs_str, struct got_reflist_head *refs,
958 struct got_object_id *id)
960 static const struct got_error *err = NULL;
961 struct got_reflist_entry *re;
962 char *s;
963 const char *name;
965 *refs_str = NULL;
967 SIMPLEQ_FOREACH(re, refs, entry) {
968 if (got_object_id_cmp(re->id, id) != 0)
969 continue;
970 name = got_ref_get_name(re->ref);
971 if (strcmp(name, GOT_REF_HEAD) == 0)
972 continue;
973 if (strncmp(name, "refs/", 5) == 0)
974 name += 5;
975 if (strncmp(name, "got/", 4) == 0)
976 continue;
977 if (strncmp(name, "heads/", 6) == 0)
978 name += 6;
979 if (strncmp(name, "remotes/", 8) == 0)
980 name += 8;
981 s = *refs_str;
982 if (asprintf(refs_str, "%s%s%s", s ? s : "",
983 s ? ", " : "", name) == -1) {
984 err = got_error_from_errno("asprintf");
985 free(s);
986 *refs_str = NULL;
987 break;
989 free(s);
992 return err;
995 static const struct got_error *
996 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
998 char *smallerthan, *at;
1000 smallerthan = strchr(author, '<');
1001 if (smallerthan && smallerthan[1] != '\0')
1002 author = smallerthan + 1;
1003 at = strchr(author, '@');
1004 if (at)
1005 *at = '\0';
1006 return format_line(wauthor, author_width, author, limit);
1009 static const struct got_error *
1010 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1011 struct got_object_id *id, struct got_reflist_head *refs,
1012 int author_display_cols)
1014 const struct got_error *err = NULL;
1015 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1016 char *logmsg0 = NULL, *logmsg = NULL;
1017 char *author = NULL;
1018 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1019 int author_width, logmsg_width;
1020 char *newline, *line = NULL;
1021 int col, limit;
1022 static const size_t date_display_cols = 9;
1023 const int avail = view->ncols;
1024 struct tm tm;
1025 time_t committer_time;
1027 committer_time = got_object_commit_get_committer_time(commit);
1028 if (localtime_r(&committer_time, &tm) == NULL)
1029 return got_error_from_errno("localtime_r");
1030 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1031 >= sizeof(datebuf))
1032 return got_error(GOT_ERR_NO_SPACE);
1034 if (avail < date_display_cols)
1035 limit = MIN(sizeof(datebuf) - 1, avail);
1036 else
1037 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1038 waddnstr(view->window, datebuf, limit);
1039 col = limit + 1;
1040 if (col > avail)
1041 goto done;
1043 author = strdup(got_object_commit_get_author(commit));
1044 if (author == NULL) {
1045 err = got_error_from_errno("strdup");
1046 goto done;
1048 err = format_author(&wauthor, &author_width, author, avail - col);
1049 if (err)
1050 goto done;
1051 waddwstr(view->window, wauthor);
1052 col += author_width;
1053 while (col <= avail && author_width < author_display_cols + 2) {
1054 waddch(view->window, ' ');
1055 col++;
1056 author_width++;
1058 if (col > avail)
1059 goto done;
1061 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1062 if (logmsg0 == NULL) {
1063 err = got_error_from_errno("strdup");
1064 goto done;
1066 logmsg = logmsg0;
1067 while (*logmsg == '\n')
1068 logmsg++;
1069 newline = strchr(logmsg, '\n');
1070 if (newline)
1071 *newline = '\0';
1072 limit = avail - col;
1073 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1074 if (err)
1075 goto done;
1076 waddwstr(view->window, wlogmsg);
1077 col += logmsg_width;
1078 while (col <= avail) {
1079 waddch(view->window, ' ');
1080 col++;
1082 done:
1083 free(logmsg0);
1084 free(wlogmsg);
1085 free(author);
1086 free(wauthor);
1087 free(line);
1088 return err;
1091 static struct commit_queue_entry *
1092 alloc_commit_queue_entry(struct got_commit_object *commit,
1093 struct got_object_id *id)
1095 struct commit_queue_entry *entry;
1097 entry = calloc(1, sizeof(*entry));
1098 if (entry == NULL)
1099 return NULL;
1101 entry->id = id;
1102 entry->commit = commit;
1103 return entry;
1106 static void
1107 pop_commit(struct commit_queue *commits)
1109 struct commit_queue_entry *entry;
1111 entry = TAILQ_FIRST(&commits->head);
1112 TAILQ_REMOVE(&commits->head, entry, entry);
1113 got_object_commit_close(entry->commit);
1114 commits->ncommits--;
1115 /* Don't free entry->id! It is owned by the commit graph. */
1116 free(entry);
1119 static void
1120 free_commits(struct commit_queue *commits)
1122 while (!TAILQ_EMPTY(&commits->head))
1123 pop_commit(commits);
1126 static const struct got_error *
1127 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1128 int minqueue, struct got_repository *repo, const char *path)
1130 const struct got_error *err = NULL;
1131 int nqueued = 0;
1134 * We keep all commits open throughout the lifetime of the log
1135 * view in order to avoid having to re-fetch commits from disk
1136 * while updating the display.
1138 while (nqueued < minqueue) {
1139 struct got_object_id *id;
1140 struct got_commit_object *commit;
1141 struct commit_queue_entry *entry;
1142 int errcode;
1144 err = got_commit_graph_iter_next(&id, graph);
1145 if (err) {
1146 if (err->code != GOT_ERR_ITER_NEED_MORE)
1147 break;
1148 err = got_commit_graph_fetch_commits(graph,
1149 minqueue, repo);
1150 if (err)
1151 return err;
1152 continue;
1155 if (id == NULL)
1156 break;
1158 err = got_object_open_as_commit(&commit, repo, id);
1159 if (err)
1160 break;
1161 entry = alloc_commit_queue_entry(commit, id);
1162 if (entry == NULL) {
1163 err = got_error_from_errno("alloc_commit_queue_entry");
1164 break;
1167 errcode = pthread_mutex_lock(&tog_mutex);
1168 if (errcode) {
1169 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1170 break;
1173 entry->idx = commits->ncommits;
1174 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1175 nqueued++;
1176 commits->ncommits++;
1178 errcode = pthread_mutex_unlock(&tog_mutex);
1179 if (errcode && err == NULL)
1180 err = got_error_set_errno(errcode,
1181 "pthread_mutex_unlock");
1184 return err;
1187 static const struct got_error *
1188 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1189 struct got_repository *repo)
1191 const struct got_error *err = NULL;
1192 struct got_reference *head_ref;
1194 *head_id = NULL;
1196 err = got_ref_open(&head_ref, repo, branch_name, 0);
1197 if (err)
1198 return err;
1200 err = got_ref_resolve(head_id, repo, head_ref);
1201 got_ref_close(head_ref);
1202 if (err) {
1203 *head_id = NULL;
1204 return err;
1207 return NULL;
1210 static const struct got_error *
1211 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1212 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1213 struct commit_queue *commits, int selected_idx, int limit,
1214 struct got_reflist_head *refs, const char *path, int commits_needed)
1216 const struct got_error *err = NULL;
1217 struct commit_queue_entry *entry;
1218 int width;
1219 int ncommits, author_cols = 10;
1220 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1221 char *refs_str = NULL;
1222 wchar_t *wline;
1224 entry = first;
1225 ncommits = 0;
1226 while (entry) {
1227 if (ncommits == selected_idx) {
1228 *selected = entry;
1229 break;
1231 entry = TAILQ_NEXT(entry, entry);
1232 ncommits++;
1235 if (*selected && !(view->searching && view->search_next_done == 0)) {
1236 err = got_object_id_str(&id_str, (*selected)->id);
1237 if (err)
1238 return err;
1239 if (refs) {
1240 err = build_refs_str(&refs_str, refs, (*selected)->id);
1241 if (err)
1242 goto done;
1246 if (commits_needed == 0)
1247 halfdelay(10); /* disable fast refresh */
1249 if (asprintf(&ncommits_str, " [%d/%d] %s",
1250 entry ? entry->idx + 1 : 0, commits->ncommits,
1251 commits_needed > 0 ?
1252 (view->searching && view->search_next_done == 0
1253 ? "searching..." : "loading... ") :
1254 (refs_str ? refs_str : "")) == -1) {
1255 err = got_error_from_errno("asprintf");
1256 goto done;
1259 if (path && strcmp(path, "/") != 0) {
1260 if (asprintf(&header, "commit %s %s%s",
1261 id_str ? id_str : "........................................",
1262 path, ncommits_str) == -1) {
1263 err = got_error_from_errno("asprintf");
1264 header = NULL;
1265 goto done;
1267 } else if (asprintf(&header, "commit %s%s",
1268 id_str ? id_str : "........................................",
1269 ncommits_str) == -1) {
1270 err = got_error_from_errno("asprintf");
1271 header = NULL;
1272 goto done;
1274 err = format_line(&wline, &width, header, view->ncols);
1275 if (err)
1276 goto done;
1278 werase(view->window);
1280 if (view_needs_focus_indication(view))
1281 wstandout(view->window);
1282 waddwstr(view->window, wline);
1283 while (width < view->ncols) {
1284 waddch(view->window, ' ');
1285 width++;
1287 if (view_needs_focus_indication(view))
1288 wstandend(view->window);
1289 free(wline);
1290 if (limit <= 1)
1291 goto done;
1293 /* Grow author column size if necessary. */
1294 entry = first;
1295 ncommits = 0;
1296 while (entry) {
1297 char *author;
1298 wchar_t *wauthor;
1299 int width;
1300 if (ncommits >= limit - 1)
1301 break;
1302 author = strdup(got_object_commit_get_author(entry->commit));
1303 if (author == NULL) {
1304 err = got_error_from_errno("strdup");
1305 goto done;
1307 err = format_author(&wauthor, &width, author, COLS);
1308 if (author_cols < width)
1309 author_cols = width;
1310 free(wauthor);
1311 free(author);
1312 entry = TAILQ_NEXT(entry, entry);
1315 entry = first;
1316 *last = first;
1317 ncommits = 0;
1318 while (entry) {
1319 if (ncommits >= limit - 1)
1320 break;
1321 if (ncommits == selected_idx)
1322 wstandout(view->window);
1323 err = draw_commit(view, entry->commit, entry->id, refs,
1324 author_cols);
1325 if (ncommits == selected_idx)
1326 wstandend(view->window);
1327 if (err)
1328 goto done;
1329 ncommits++;
1330 *last = entry;
1331 entry = TAILQ_NEXT(entry, entry);
1334 view_vborder(view);
1335 done:
1336 free(id_str);
1337 free(refs_str);
1338 free(ncommits_str);
1339 free(header);
1340 return err;
1343 static void
1344 scroll_up(struct tog_view *view,
1345 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1346 struct commit_queue *commits)
1348 struct commit_queue_entry *entry;
1349 int nscrolled = 0;
1351 entry = TAILQ_FIRST(&commits->head);
1352 if (*first_displayed_entry == entry)
1353 return;
1355 entry = *first_displayed_entry;
1356 while (entry && nscrolled < maxscroll) {
1357 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1358 if (entry) {
1359 *first_displayed_entry = entry;
1360 nscrolled++;
1365 static const struct got_error *
1366 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1367 pthread_cond_t *need_commits)
1369 int errcode;
1370 int max_wait = 20;
1372 halfdelay(1); /* fast refresh while loading commits */
1374 while (*commits_needed > 0) {
1375 if (*log_complete)
1376 break;
1378 /* Wake the log thread. */
1379 errcode = pthread_cond_signal(need_commits);
1380 if (errcode)
1381 return got_error_set_errno(errcode,
1382 "pthread_cond_signal");
1383 errcode = pthread_mutex_unlock(&tog_mutex);
1384 if (errcode)
1385 return got_error_set_errno(errcode,
1386 "pthread_mutex_unlock");
1387 pthread_yield();
1388 errcode = pthread_mutex_lock(&tog_mutex);
1389 if (errcode)
1390 return got_error_set_errno(errcode,
1391 "pthread_mutex_lock");
1393 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1395 * Thread is not done yet; lose a key press
1396 * and let the user retry... this way the GUI
1397 * remains interactive while logging deep paths
1398 * with few commits in history.
1400 return NULL;
1404 return NULL;
1407 static const struct got_error *
1408 scroll_down(struct tog_view *view,
1409 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1410 struct commit_queue_entry **last_displayed_entry,
1411 struct commit_queue *commits, int *log_complete, int *commits_needed,
1412 pthread_cond_t *need_commits)
1414 const struct got_error *err = NULL;
1415 struct commit_queue_entry *pentry;
1416 int nscrolled = 0;
1418 if (*last_displayed_entry == NULL)
1419 return NULL;
1421 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1422 if (pentry == NULL && !*log_complete) {
1424 * Ask the log thread for required amount of commits
1425 * plus some amount of pre-fetching.
1427 (*commits_needed) += maxscroll + 20;
1428 err = trigger_log_thread(0, commits_needed, log_complete,
1429 need_commits);
1430 if (err)
1431 return err;
1434 do {
1435 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1436 if (pentry == NULL)
1437 break;
1439 *last_displayed_entry = pentry;
1441 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1442 if (pentry == NULL)
1443 break;
1444 *first_displayed_entry = pentry;
1445 } while (++nscrolled < maxscroll);
1447 return err;
1450 static const struct got_error *
1451 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1452 struct got_commit_object *commit, struct got_object_id *commit_id,
1453 struct tog_view *log_view, struct got_reflist_head *refs,
1454 struct got_repository *repo)
1456 const struct got_error *err;
1457 struct got_object_qid *parent_id;
1458 struct tog_view *diff_view;
1460 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1461 if (diff_view == NULL)
1462 return got_error_from_errno("view_open");
1464 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1465 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1466 commit_id, log_view, refs, repo);
1467 if (err == NULL)
1468 *new_view = diff_view;
1469 return err;
1472 static const struct got_error *
1473 tree_view_visit_subtree(struct got_tree_object *subtree,
1474 struct tog_tree_view_state *s)
1476 struct tog_parent_tree *parent;
1478 parent = calloc(1, sizeof(*parent));
1479 if (parent == NULL)
1480 return got_error_from_errno("calloc");
1482 parent->tree = s->tree;
1483 parent->first_displayed_entry = s->first_displayed_entry;
1484 parent->selected_entry = s->selected_entry;
1485 parent->selected = s->selected;
1486 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1487 s->tree = subtree;
1488 s->entries = got_object_tree_get_entries(s->tree);
1489 s->selected = 0;
1490 s->first_displayed_entry = NULL;
1491 return NULL;
1495 static const struct got_error *
1496 browse_commit_tree(struct tog_view **new_view, int begin_x,
1497 struct commit_queue_entry *entry, const char *path,
1498 struct got_reflist_head *refs, struct got_repository *repo)
1500 const struct got_error *err = NULL;
1501 struct got_tree_object *tree;
1502 struct got_tree_entry *te;
1503 struct tog_tree_view_state *s;
1504 struct tog_view *tree_view;
1505 char *slash, *subpath = NULL;
1506 const char *p;
1508 err = got_object_open_as_tree(&tree, repo,
1509 got_object_commit_get_tree_id(entry->commit));
1510 if (err)
1511 return err;
1513 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1514 if (tree_view == NULL)
1515 return got_error_from_errno("view_open");
1517 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1518 if (err) {
1519 got_object_tree_close(tree);
1520 return err;
1522 s = &tree_view->state.tree;
1524 *new_view = tree_view;
1526 /* Walk the path and open corresponding tree objects. */
1527 p = path;
1528 while (*p) {
1529 struct got_object_id *tree_id;
1531 while (p[0] == '/')
1532 p++;
1534 /* Ensure the correct subtree entry is selected. */
1535 slash = strchr(p, '/');
1536 if (slash == NULL)
1537 slash = strchr(p, '\0');
1538 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1539 if (strncmp(p, te->name, slash - p) == 0) {
1540 s->selected_entry = te;
1541 break;
1543 s->selected++;
1545 if (s->selected_entry == NULL) {
1546 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1547 break;
1549 if (s->tree != s->root)
1550 s->selected++; /* skip '..' */
1552 if (!S_ISDIR(s->selected_entry->mode)) {
1553 /* Jump to this file's entry. */
1554 s->first_displayed_entry = s->selected_entry;
1555 s->selected = 0;
1556 break;
1559 slash = strchr(p, '/');
1560 if (slash)
1561 subpath = strndup(path, slash - path);
1562 else
1563 subpath = strdup(path);
1564 if (subpath == NULL) {
1565 err = got_error_from_errno("strdup");
1566 break;
1569 err = got_object_id_by_path(&tree_id, repo, entry->id,
1570 subpath);
1571 if (err)
1572 break;
1574 err = got_object_open_as_tree(&tree, repo, tree_id);
1575 free(tree_id);
1576 if (err)
1577 break;
1579 err = tree_view_visit_subtree(tree, s);
1580 if (err) {
1581 got_object_tree_close(tree);
1582 break;
1584 if (slash == NULL)
1585 break;
1586 free(subpath);
1587 subpath = NULL;
1588 p = slash;
1591 free(subpath);
1592 return err;
1595 static void *
1596 log_thread(void *arg)
1598 const struct got_error *err = NULL;
1599 int errcode = 0;
1600 struct tog_log_thread_args *a = arg;
1601 int done = 0;
1603 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1604 if (err)
1605 return (void *)err;
1607 while (!done && !err) {
1608 err = queue_commits(a->graph, a->commits, 1, a->repo,
1609 a->in_repo_path);
1610 if (err) {
1611 if (err->code != GOT_ERR_ITER_COMPLETED)
1612 return (void *)err;
1613 err = NULL;
1614 done = 1;
1615 } else if (a->commits_needed > 0)
1616 a->commits_needed--;
1618 errcode = pthread_mutex_lock(&tog_mutex);
1619 if (errcode) {
1620 err = got_error_set_errno(errcode,
1621 "pthread_mutex_lock");
1622 break;
1623 } else if (*a->quit)
1624 done = 1;
1625 else if (*a->first_displayed_entry == NULL) {
1626 *a->first_displayed_entry =
1627 TAILQ_FIRST(&a->commits->head);
1628 *a->selected_entry = *a->first_displayed_entry;
1631 if (done)
1632 a->commits_needed = 0;
1633 else if (a->commits_needed == 0) {
1634 errcode = pthread_cond_wait(&a->need_commits,
1635 &tog_mutex);
1636 if (errcode)
1637 err = got_error_set_errno(errcode,
1638 "pthread_cond_wait");
1641 errcode = pthread_mutex_unlock(&tog_mutex);
1642 if (errcode && err == NULL)
1643 err = got_error_set_errno(errcode,
1644 "pthread_mutex_unlock");
1646 a->log_complete = 1;
1647 return (void *)err;
1650 static const struct got_error *
1651 stop_log_thread(struct tog_log_view_state *s)
1653 const struct got_error *err = NULL;
1654 int errcode;
1656 if (s->thread) {
1657 s->quit = 1;
1658 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1659 if (errcode)
1660 return got_error_set_errno(errcode,
1661 "pthread_cond_signal");
1662 errcode = pthread_mutex_unlock(&tog_mutex);
1663 if (errcode)
1664 return got_error_set_errno(errcode,
1665 "pthread_mutex_unlock");
1666 errcode = pthread_join(s->thread, (void **)&err);
1667 if (errcode)
1668 return got_error_set_errno(errcode, "pthread_join");
1669 errcode = pthread_mutex_lock(&tog_mutex);
1670 if (errcode)
1671 return got_error_set_errno(errcode,
1672 "pthread_mutex_lock");
1673 s->thread = NULL;
1676 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1677 if (errcode && err == NULL)
1678 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1680 if (s->thread_args.repo) {
1681 got_repo_close(s->thread_args.repo);
1682 s->thread_args.repo = NULL;
1685 if (s->thread_args.graph) {
1686 got_commit_graph_close(s->thread_args.graph);
1687 s->thread_args.graph = NULL;
1690 return err;
1693 static const struct got_error *
1694 close_log_view(struct tog_view *view)
1696 const struct got_error *err = NULL;
1697 struct tog_log_view_state *s = &view->state.log;
1699 err = stop_log_thread(s);
1700 free_commits(&s->commits);
1701 free(s->in_repo_path);
1702 s->in_repo_path = NULL;
1703 free(s->start_id);
1704 s->start_id = NULL;
1705 return err;
1708 static const struct got_error *
1709 search_start_log_view(struct tog_view *view)
1711 struct tog_log_view_state *s = &view->state.log;
1713 s->matched_entry = NULL;
1714 return NULL;
1717 static int
1718 match_commit(struct got_commit_object *commit, regex_t *regex)
1720 regmatch_t regmatch;
1722 if (regexec(regex, got_object_commit_get_author(commit), 1,
1723 &regmatch, 0) == 0 ||
1724 regexec(regex, got_object_commit_get_committer(commit), 1,
1725 &regmatch, 0) == 0 ||
1726 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1727 &regmatch, 0) == 0)
1728 return 1;
1730 return 0;
1733 static const struct got_error *
1734 search_next_log_view(struct tog_view *view)
1736 const struct got_error *err = NULL;
1737 struct tog_log_view_state *s = &view->state.log;
1738 struct commit_queue_entry *entry;
1740 if (!view->searching) {
1741 view->search_next_done = 1;
1742 return NULL;
1745 if (s->matched_entry) {
1746 if (view->searching == TOG_SEARCH_FORWARD)
1747 entry = TAILQ_NEXT(s->selected_entry, entry);
1748 else
1749 entry = TAILQ_PREV(s->selected_entry,
1750 commit_queue_head, entry);
1751 } else {
1752 if (view->searching == TOG_SEARCH_FORWARD)
1753 entry = TAILQ_FIRST(&s->commits.head);
1754 else
1755 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1758 while (1) {
1759 if (entry == NULL) {
1760 if (s->thread_args.log_complete) {
1761 if (s->matched_entry == NULL) {
1762 view->search_next_done = 1;
1763 return NULL;
1765 if (view->searching == TOG_SEARCH_FORWARD)
1766 entry = TAILQ_FIRST(&s->commits.head);
1767 else
1768 entry = TAILQ_LAST(&s->commits.head,
1769 commit_queue_head);
1770 } else {
1771 s->thread_args.commits_needed = 1;
1772 return trigger_log_thread(0,
1773 &s->thread_args.commits_needed,
1774 &s->thread_args.log_complete,
1775 &s->thread_args.need_commits);
1779 if (match_commit(entry->commit, &view->regex)) {
1780 view->search_next_done = 1;
1781 s->matched_entry = entry;
1782 break;
1784 if (view->searching == TOG_SEARCH_FORWARD)
1785 entry = TAILQ_NEXT(entry, entry);
1786 else
1787 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1790 if (s->matched_entry) {
1791 int cur = s->selected_entry->idx;
1792 while (cur < s->matched_entry->idx) {
1793 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1794 if (err)
1795 return err;
1796 cur++;
1798 while (cur > s->matched_entry->idx) {
1799 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1800 if (err)
1801 return err;
1802 cur--;
1806 return NULL;
1810 static const struct got_error *
1811 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1812 struct got_reflist_head *refs, struct got_repository *repo,
1813 const char *path, int check_disk)
1815 const struct got_error *err = NULL;
1816 struct tog_log_view_state *s = &view->state.log;
1817 struct got_repository *thread_repo = NULL;
1818 struct got_commit_graph *thread_graph = NULL;
1819 int errcode;
1821 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1822 if (err != NULL)
1823 goto done;
1825 /* The commit queue only contains commits being displayed. */
1826 TAILQ_INIT(&s->commits.head);
1827 s->commits.ncommits = 0;
1829 s->refs = refs;
1830 s->repo = repo;
1831 s->start_id = got_object_id_dup(start_id);
1832 if (s->start_id == NULL) {
1833 err = got_error_from_errno("got_object_id_dup");
1834 goto done;
1837 view->show = show_log_view;
1838 view->input = input_log_view;
1839 view->close = close_log_view;
1840 view->search_start = search_start_log_view;
1841 view->search_next = search_next_log_view;
1843 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1844 if (err)
1845 goto done;
1846 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1847 0, thread_repo);
1848 if (err)
1849 goto done;
1851 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1852 if (errcode) {
1853 err = got_error_set_errno(errcode, "pthread_cond_init");
1854 goto done;
1857 s->thread_args.commits_needed = view->nlines;
1858 s->thread_args.graph = thread_graph;
1859 s->thread_args.commits = &s->commits;
1860 s->thread_args.in_repo_path = s->in_repo_path;
1861 s->thread_args.start_id = s->start_id;
1862 s->thread_args.repo = thread_repo;
1863 s->thread_args.log_complete = 0;
1864 s->thread_args.quit = &s->quit;
1865 s->thread_args.view = view;
1866 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1867 s->thread_args.selected_entry = &s->selected_entry;
1868 done:
1869 if (err)
1870 close_log_view(view);
1871 return err;
1874 static const struct got_error *
1875 show_log_view(struct tog_view *view)
1877 struct tog_log_view_state *s = &view->state.log;
1879 if (s->thread == NULL) {
1880 int errcode = pthread_create(&s->thread, NULL, log_thread,
1881 &s->thread_args);
1882 if (errcode)
1883 return got_error_set_errno(errcode, "pthread_create");
1886 return draw_commits(view, &s->last_displayed_entry,
1887 &s->selected_entry, s->first_displayed_entry,
1888 &s->commits, s->selected, view->nlines, s->refs,
1889 s->in_repo_path, s->thread_args.commits_needed);
1892 static const struct got_error *
1893 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1894 struct tog_view **focus_view, struct tog_view *view, int ch)
1896 const struct got_error *err = NULL;
1897 struct tog_log_view_state *s = &view->state.log;
1898 char *parent_path;
1899 struct tog_view *diff_view = NULL, *tree_view = NULL;
1900 int begin_x = 0;
1902 switch (ch) {
1903 case 'q':
1904 s->quit = 1;
1905 break;
1906 case 'k':
1907 case KEY_UP:
1908 case '<':
1909 case ',':
1910 if (s->first_displayed_entry == NULL)
1911 break;
1912 if (s->selected > 0)
1913 s->selected--;
1914 else
1915 scroll_up(view, &s->first_displayed_entry, 1,
1916 &s->commits);
1917 break;
1918 case KEY_PPAGE:
1919 case CTRL('b'):
1920 if (s->first_displayed_entry == NULL)
1921 break;
1922 if (TAILQ_FIRST(&s->commits.head) ==
1923 s->first_displayed_entry) {
1924 s->selected = 0;
1925 break;
1927 scroll_up(view, &s->first_displayed_entry,
1928 view->nlines, &s->commits);
1929 break;
1930 case 'j':
1931 case KEY_DOWN:
1932 case '>':
1933 case '.':
1934 if (s->first_displayed_entry == NULL)
1935 break;
1936 if (s->selected < MIN(view->nlines - 2,
1937 s->commits.ncommits - 1)) {
1938 s->selected++;
1939 break;
1941 err = scroll_down(view, &s->first_displayed_entry, 1,
1942 &s->last_displayed_entry, &s->commits,
1943 &s->thread_args.log_complete,
1944 &s->thread_args.commits_needed,
1945 &s->thread_args.need_commits);
1946 break;
1947 case KEY_NPAGE:
1948 case CTRL('f'): {
1949 struct commit_queue_entry *first;
1950 first = s->first_displayed_entry;
1951 if (first == NULL)
1952 break;
1953 err = scroll_down(view, &s->first_displayed_entry,
1954 view->nlines, &s->last_displayed_entry,
1955 &s->commits, &s->thread_args.log_complete,
1956 &s->thread_args.commits_needed,
1957 &s->thread_args.need_commits);
1958 if (first == s->first_displayed_entry &&
1959 s->selected < MIN(view->nlines - 2,
1960 s->commits.ncommits - 1)) {
1961 /* can't scroll further down */
1962 s->selected = MIN(view->nlines - 2,
1963 s->commits.ncommits - 1);
1965 err = NULL;
1966 break;
1968 case KEY_RESIZE:
1969 if (s->selected > view->nlines - 2)
1970 s->selected = view->nlines - 2;
1971 if (s->selected > s->commits.ncommits - 1)
1972 s->selected = s->commits.ncommits - 1;
1973 break;
1974 case KEY_ENTER:
1975 case ' ':
1976 case '\r':
1977 if (s->selected_entry == NULL)
1978 break;
1979 if (view_is_parent_view(view))
1980 begin_x = view_split_begin_x(view->begin_x);
1981 err = open_diff_view_for_commit(&diff_view, begin_x,
1982 s->selected_entry->commit, s->selected_entry->id,
1983 view, s->refs, s->repo);
1984 if (err)
1985 break;
1986 if (view_is_parent_view(view)) {
1987 err = view_close_child(view);
1988 if (err)
1989 return err;
1990 err = view_set_child(view, diff_view);
1991 if (err) {
1992 view_close(diff_view);
1993 break;
1995 *focus_view = diff_view;
1996 view->child_focussed = 1;
1997 } else
1998 *new_view = diff_view;
1999 break;
2000 case 't':
2001 if (s->selected_entry == NULL)
2002 break;
2003 if (view_is_parent_view(view))
2004 begin_x = view_split_begin_x(view->begin_x);
2005 err = browse_commit_tree(&tree_view, begin_x,
2006 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2007 if (err)
2008 break;
2009 if (view_is_parent_view(view)) {
2010 err = view_close_child(view);
2011 if (err)
2012 return err;
2013 err = view_set_child(view, tree_view);
2014 if (err) {
2015 view_close(tree_view);
2016 break;
2018 *focus_view = tree_view;
2019 view->child_focussed = 1;
2020 } else
2021 *new_view = tree_view;
2022 break;
2023 case KEY_BACKSPACE:
2024 if (strcmp(s->in_repo_path, "/") == 0)
2025 break;
2026 parent_path = dirname(s->in_repo_path);
2027 if (parent_path && strcmp(parent_path, ".") != 0) {
2028 struct tog_view *lv;
2029 err = stop_log_thread(s);
2030 if (err)
2031 return err;
2032 lv = view_open(view->nlines, view->ncols,
2033 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2034 if (lv == NULL)
2035 return got_error_from_errno(
2036 "view_open");
2037 err = open_log_view(lv, s->start_id, s->refs,
2038 s->repo, parent_path, 0);
2039 if (err)
2040 return err;;
2041 if (view_is_parent_view(view))
2042 *new_view = lv;
2043 else {
2044 view_set_child(view->parent, lv);
2045 *focus_view = lv;
2047 return NULL;
2049 break;
2050 default:
2051 break;
2054 return err;
2057 static const struct got_error *
2058 apply_unveil(const char *repo_path, const char *worktree_path)
2060 const struct got_error *error;
2062 if (repo_path && unveil(repo_path, "r") != 0)
2063 return got_error_from_errno2("unveil", repo_path);
2065 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2066 return got_error_from_errno2("unveil", worktree_path);
2068 if (unveil("/tmp", "rwc") != 0)
2069 return got_error_from_errno2("unveil", "/tmp");
2071 error = got_privsep_unveil_exec_helpers();
2072 if (error != NULL)
2073 return error;
2075 if (unveil(NULL, NULL) != 0)
2076 return got_error_from_errno("unveil");
2078 return NULL;
2081 static void
2082 init_curses(void)
2084 initscr();
2085 cbreak();
2086 halfdelay(1); /* Do fast refresh while initial view is loading. */
2087 noecho();
2088 nonl();
2089 intrflush(stdscr, FALSE);
2090 keypad(stdscr, TRUE);
2091 curs_set(0);
2092 signal(SIGWINCH, tog_sigwinch);
2095 static const struct got_error *
2096 cmd_log(int argc, char *argv[])
2098 const struct got_error *error;
2099 struct got_repository *repo = NULL;
2100 struct got_worktree *worktree = NULL;
2101 struct got_reflist_head refs;
2102 struct got_object_id *start_id = NULL;
2103 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2104 char *start_commit = NULL;
2105 int ch;
2106 struct tog_view *view;
2108 SIMPLEQ_INIT(&refs);
2110 #ifndef PROFILE
2111 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2112 NULL) == -1)
2113 err(1, "pledge");
2114 #endif
2116 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2117 switch (ch) {
2118 case 'c':
2119 start_commit = optarg;
2120 break;
2121 case 'r':
2122 repo_path = realpath(optarg, NULL);
2123 if (repo_path == NULL)
2124 err(1, "-r option");
2125 break;
2126 default:
2127 usage_log();
2128 /* NOTREACHED */
2132 argc -= optind;
2133 argv += optind;
2135 cwd = getcwd(NULL, 0);
2136 if (cwd == NULL) {
2137 error = got_error_from_errno("getcwd");
2138 goto done;
2140 error = got_worktree_open(&worktree, cwd);
2141 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2142 goto done;
2143 error = NULL;
2145 if (argc == 0) {
2146 path = strdup("");
2147 if (path == NULL) {
2148 error = got_error_from_errno("strdup");
2149 goto done;
2151 } else if (argc == 1) {
2152 if (worktree) {
2153 error = got_worktree_resolve_path(&path, worktree,
2154 argv[0]);
2155 if (error)
2156 goto done;
2157 } else {
2158 path = strdup(argv[0]);
2159 if (path == NULL) {
2160 error = got_error_from_errno("strdup");
2161 goto done;
2164 } else
2165 usage_log();
2167 repo_path = worktree ?
2168 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2169 if (repo_path == NULL) {
2170 error = got_error_from_errno("strdup");
2171 goto done;
2174 init_curses();
2176 error = got_repo_open(&repo, repo_path);
2177 if (error != NULL)
2178 goto done;
2180 error = apply_unveil(got_repo_get_path(repo),
2181 worktree ? got_worktree_get_root_path(worktree) : NULL);
2182 if (error)
2183 goto done;
2185 if (start_commit == NULL)
2186 error = get_head_commit_id(&start_id, worktree ?
2187 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2188 repo);
2189 else
2190 error = got_object_resolve_id_str(&start_id, repo,
2191 start_commit);
2192 if (error != NULL)
2193 goto done;
2195 error = got_ref_list(&refs, repo);
2196 if (error)
2197 goto done;
2199 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2200 if (view == NULL) {
2201 error = got_error_from_errno("view_open");
2202 goto done;
2204 error = open_log_view(view, start_id, &refs, repo, path, 1);
2205 if (error)
2206 goto done;
2207 error = view_loop(view);
2208 done:
2209 free(repo_path);
2210 free(cwd);
2211 free(path);
2212 free(start_id);
2213 if (repo)
2214 got_repo_close(repo);
2215 if (worktree)
2216 got_worktree_close(worktree);
2217 got_ref_list_free(&refs);
2218 return error;
2221 __dead static void
2222 usage_diff(void)
2224 endwin();
2225 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2226 getprogname());
2227 exit(1);
2230 static char *
2231 parse_next_line(FILE *f, size_t *len)
2233 char *line;
2234 size_t linelen;
2235 size_t lineno;
2236 const char delim[3] = { '\0', '\0', '\0'};
2238 line = fparseln(f, &linelen, &lineno, delim, 0);
2239 if (len)
2240 *len = linelen;
2241 return line;
2244 static const struct got_error *
2245 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2246 int *last_displayed_line, int *eof, int max_lines,
2247 char *header)
2249 const struct got_error *err;
2250 int nlines = 0, nprinted = 0;
2251 char *line;
2252 size_t len;
2253 wchar_t *wline;
2254 int width;
2256 rewind(f);
2257 werase(view->window);
2259 if (header) {
2260 err = format_line(&wline, &width, header, view->ncols);
2261 if (err) {
2262 return err;
2265 if (view_needs_focus_indication(view))
2266 wstandout(view->window);
2267 waddwstr(view->window, wline);
2268 if (view_needs_focus_indication(view))
2269 wstandend(view->window);
2270 if (width < view->ncols - 1)
2271 waddch(view->window, '\n');
2273 if (max_lines <= 1)
2274 return NULL;
2275 max_lines--;
2278 *eof = 0;
2279 while (nprinted < max_lines) {
2280 line = parse_next_line(f, &len);
2281 if (line == NULL) {
2282 *eof = 1;
2283 break;
2285 if (++nlines < *first_displayed_line) {
2286 free(line);
2287 continue;
2290 err = format_line(&wline, &width, line, view->ncols);
2291 if (err) {
2292 free(line);
2293 return err;
2295 waddwstr(view->window, wline);
2296 if (width < view->ncols - 1)
2297 waddch(view->window, '\n');
2298 if (++nprinted == 1)
2299 *first_displayed_line = nlines;
2300 free(line);
2301 free(wline);
2302 wline = NULL;
2304 *last_displayed_line = nlines;
2306 view_vborder(view);
2308 if (*eof) {
2309 while (nprinted < view->nlines) {
2310 waddch(view->window, '\n');
2311 nprinted++;
2314 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2315 if (err) {
2316 return err;
2319 wstandout(view->window);
2320 waddwstr(view->window, wline);
2321 wstandend(view->window);
2324 return NULL;
2327 static char *
2328 get_datestr(time_t *time, char *datebuf)
2330 char *p, *s = ctime_r(time, datebuf);
2331 p = strchr(s, '\n');
2332 if (p)
2333 *p = '\0';
2334 return s;
2337 static const struct got_error *
2338 write_commit_info(struct got_object_id *commit_id,
2339 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2341 const struct got_error *err = NULL;
2342 char datebuf[26];
2343 struct got_commit_object *commit;
2344 char *id_str = NULL;
2345 time_t committer_time;
2346 const char *author, *committer;
2347 char *refs_str = NULL;
2349 if (refs) {
2350 err = build_refs_str(&refs_str, refs, commit_id);
2351 if (err)
2352 return err;
2355 err = got_object_open_as_commit(&commit, repo, commit_id);
2356 if (err)
2357 return err;
2359 err = got_object_id_str(&id_str, commit_id);
2360 if (err) {
2361 err = got_error_from_errno("got_object_id_str");
2362 goto done;
2365 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2366 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2367 err = got_error_from_errno("fprintf");
2368 goto done;
2370 if (fprintf(outfile, "from: %s\n",
2371 got_object_commit_get_author(commit)) < 0) {
2372 err = got_error_from_errno("fprintf");
2373 goto done;
2375 committer_time = got_object_commit_get_committer_time(commit);
2376 if (fprintf(outfile, "date: %s UTC\n",
2377 get_datestr(&committer_time, datebuf)) < 0) {
2378 err = got_error_from_errno("fprintf");
2379 goto done;
2381 author = got_object_commit_get_author(commit);
2382 committer = got_object_commit_get_committer(commit);
2383 if (strcmp(author, committer) != 0 &&
2384 fprintf(outfile, "via: %s\n", committer) < 0) {
2385 err = got_error_from_errno("fprintf");
2386 goto done;
2388 if (fprintf(outfile, "%s\n",
2389 got_object_commit_get_logmsg(commit)) < 0) {
2390 err = got_error_from_errno("fprintf");
2391 goto done;
2393 done:
2394 free(id_str);
2395 free(refs_str);
2396 got_object_commit_close(commit);
2397 return err;
2400 static const struct got_error *
2401 create_diff(struct tog_diff_view_state *s)
2403 const struct got_error *err = NULL;
2404 FILE *f = NULL;
2405 int obj_type;
2407 f = got_opentemp();
2408 if (f == NULL) {
2409 err = got_error_from_errno("got_opentemp");
2410 goto done;
2412 if (s->f && fclose(s->f) != 0) {
2413 err = got_error_from_errno("fclose");
2414 goto done;
2416 s->f = f;
2418 if (s->id1)
2419 err = got_object_get_type(&obj_type, s->repo, s->id1);
2420 else
2421 err = got_object_get_type(&obj_type, s->repo, s->id2);
2422 if (err)
2423 goto done;
2425 switch (obj_type) {
2426 case GOT_OBJ_TYPE_BLOB:
2427 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2428 s->diff_context, s->repo, f);
2429 break;
2430 case GOT_OBJ_TYPE_TREE:
2431 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2432 s->diff_context, s->repo, f);
2433 break;
2434 case GOT_OBJ_TYPE_COMMIT: {
2435 const struct got_object_id_queue *parent_ids;
2436 struct got_object_qid *pid;
2437 struct got_commit_object *commit2;
2439 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2440 if (err)
2441 break;
2442 /* Show commit info if we're diffing to a parent/root commit. */
2443 if (s->id1 == NULL)
2444 write_commit_info(s->id2, s->refs, s->repo, f);
2445 else {
2446 parent_ids = got_object_commit_get_parent_ids(commit2);
2447 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2448 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2449 write_commit_info(s->id2, s->refs,
2450 s->repo, f);
2451 break;
2455 got_object_commit_close(commit2);
2457 err = got_diff_objects_as_commits(s->id1, s->id2,
2458 s->diff_context, s->repo, f);
2459 break;
2461 default:
2462 err = got_error(GOT_ERR_OBJ_TYPE);
2463 break;
2465 done:
2466 if (f && fflush(f) != 0 && err == NULL)
2467 err = got_error_from_errno("fflush");
2468 return err;
2471 static void
2472 diff_view_indicate_progress(struct tog_view *view)
2474 mvwaddstr(view->window, 0, 0, "diffing...");
2475 update_panels();
2476 doupdate();
2479 static const struct got_error *
2480 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2481 struct got_object_id *id2, struct tog_view *log_view,
2482 struct got_reflist_head *refs, struct got_repository *repo)
2484 const struct got_error *err;
2486 if (id1 != NULL && id2 != NULL) {
2487 int type1, type2;
2488 err = got_object_get_type(&type1, repo, id1);
2489 if (err)
2490 return err;
2491 err = got_object_get_type(&type2, repo, id2);
2492 if (err)
2493 return err;
2495 if (type1 != type2)
2496 return got_error(GOT_ERR_OBJ_TYPE);
2499 if (id1) {
2500 view->state.diff.id1 = got_object_id_dup(id1);
2501 if (view->state.diff.id1 == NULL)
2502 return got_error_from_errno("got_object_id_dup");
2503 } else
2504 view->state.diff.id1 = NULL;
2506 view->state.diff.id2 = got_object_id_dup(id2);
2507 if (view->state.diff.id2 == NULL) {
2508 free(view->state.diff.id1);
2509 view->state.diff.id1 = NULL;
2510 return got_error_from_errno("got_object_id_dup");
2512 view->state.diff.f = NULL;
2513 view->state.diff.first_displayed_line = 1;
2514 view->state.diff.last_displayed_line = view->nlines;
2515 view->state.diff.diff_context = 3;
2516 view->state.diff.log_view = log_view;
2517 view->state.diff.repo = repo;
2518 view->state.diff.refs = refs;
2520 if (log_view && view_is_splitscreen(view))
2521 show_log_view(log_view); /* draw vborder */
2522 diff_view_indicate_progress(view);
2524 err = create_diff(&view->state.diff);
2525 if (err) {
2526 free(view->state.diff.id1);
2527 view->state.diff.id1 = NULL;
2528 free(view->state.diff.id2);
2529 view->state.diff.id2 = NULL;
2530 return err;
2533 view->show = show_diff_view;
2534 view->input = input_diff_view;
2535 view->close = close_diff_view;
2537 return NULL;
2540 static const struct got_error *
2541 close_diff_view(struct tog_view *view)
2543 const struct got_error *err = NULL;
2545 free(view->state.diff.id1);
2546 view->state.diff.id1 = NULL;
2547 free(view->state.diff.id2);
2548 view->state.diff.id2 = NULL;
2549 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2550 err = got_error_from_errno("fclose");
2551 return err;
2554 static const struct got_error *
2555 show_diff_view(struct tog_view *view)
2557 const struct got_error *err;
2558 struct tog_diff_view_state *s = &view->state.diff;
2559 char *id_str1 = NULL, *id_str2, *header;
2561 if (s->id1) {
2562 err = got_object_id_str(&id_str1, s->id1);
2563 if (err)
2564 return err;
2566 err = got_object_id_str(&id_str2, s->id2);
2567 if (err)
2568 return err;
2570 if (asprintf(&header, "diff %s %s",
2571 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2572 err = got_error_from_errno("asprintf");
2573 free(id_str1);
2574 free(id_str2);
2575 return err;
2577 free(id_str1);
2578 free(id_str2);
2580 return draw_file(view, s->f, &s->first_displayed_line,
2581 &s->last_displayed_line, &s->eof, view->nlines,
2582 header);
2585 static const struct got_error *
2586 set_selected_commit(struct tog_diff_view_state *s,
2587 struct commit_queue_entry *entry)
2589 const struct got_error *err;
2590 const struct got_object_id_queue *parent_ids;
2591 struct got_commit_object *selected_commit;
2592 struct got_object_qid *pid;
2594 free(s->id2);
2595 s->id2 = got_object_id_dup(entry->id);
2596 if (s->id2 == NULL)
2597 return got_error_from_errno("got_object_id_dup");
2599 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2600 if (err)
2601 return err;
2602 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2603 free(s->id1);
2604 pid = SIMPLEQ_FIRST(parent_ids);
2605 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2606 got_object_commit_close(selected_commit);
2607 return NULL;
2610 static const struct got_error *
2611 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2612 struct tog_view **focus_view, struct tog_view *view, int ch)
2614 const struct got_error *err = NULL;
2615 struct tog_diff_view_state *s = &view->state.diff;
2616 struct tog_log_view_state *ls;
2617 struct commit_queue_entry *entry;
2618 int i;
2620 switch (ch) {
2621 case 'k':
2622 case KEY_UP:
2623 if (s->first_displayed_line > 1)
2624 s->first_displayed_line--;
2625 break;
2626 case KEY_PPAGE:
2627 case CTRL('b'):
2628 if (s->first_displayed_line == 1)
2629 break;
2630 i = 0;
2631 while (i++ < view->nlines - 1 &&
2632 s->first_displayed_line > 1)
2633 s->first_displayed_line--;
2634 break;
2635 case 'j':
2636 case KEY_DOWN:
2637 if (!s->eof)
2638 s->first_displayed_line++;
2639 break;
2640 case KEY_NPAGE:
2641 case CTRL('f'):
2642 case ' ':
2643 if (s->eof)
2644 break;
2645 i = 0;
2646 while (!s->eof && i++ < view->nlines - 1) {
2647 char *line;
2648 line = parse_next_line(s->f, NULL);
2649 s->first_displayed_line++;
2650 if (line == NULL)
2651 break;
2653 break;
2654 case '[':
2655 if (s->diff_context > 0) {
2656 s->diff_context--;
2657 diff_view_indicate_progress(view);
2658 err = create_diff(s);
2660 break;
2661 case ']':
2662 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2663 s->diff_context++;
2664 diff_view_indicate_progress(view);
2665 err = create_diff(s);
2667 break;
2668 case '<':
2669 case ',':
2670 if (s->log_view == NULL)
2671 break;
2672 ls = &s->log_view->state.log;
2673 entry = TAILQ_PREV(ls->selected_entry,
2674 commit_queue_head, entry);
2675 if (entry == NULL)
2676 break;
2678 err = input_log_view(NULL, NULL, NULL, s->log_view,
2679 KEY_UP);
2680 if (err)
2681 break;
2683 err = set_selected_commit(s, entry);
2684 if (err)
2685 break;
2687 s->first_displayed_line = 1;
2688 s->last_displayed_line = view->nlines;
2690 diff_view_indicate_progress(view);
2691 err = create_diff(s);
2692 break;
2693 case '>':
2694 case '.':
2695 if (s->log_view == NULL)
2696 break;
2697 ls = &s->log_view->state.log;
2699 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2700 ls->thread_args.commits_needed++;
2702 /* Display "loading..." in log view. */
2703 show_log_view(s->log_view);
2704 update_panels();
2705 doupdate();
2707 err = trigger_log_thread(1 /* load_all */,
2708 &ls->thread_args.commits_needed,
2709 &ls->thread_args.log_complete,
2710 &ls->thread_args.need_commits);
2711 if (err)
2712 break;
2714 err = input_log_view(NULL, NULL, NULL, s->log_view,
2715 KEY_DOWN);
2716 if (err)
2717 break;
2719 entry = TAILQ_NEXT(ls->selected_entry, entry);
2720 if (entry == NULL)
2721 break;
2723 err = set_selected_commit(s, entry);
2724 if (err)
2725 break;
2727 s->first_displayed_line = 1;
2728 s->last_displayed_line = view->nlines;
2730 diff_view_indicate_progress(view);
2731 err = create_diff(s);
2732 break;
2733 default:
2734 break;
2737 return err;
2740 static const struct got_error *
2741 cmd_diff(int argc, char *argv[])
2743 const struct got_error *error = NULL;
2744 struct got_repository *repo = NULL;
2745 struct got_reflist_head refs;
2746 struct got_object_id *id1 = NULL, *id2 = NULL;
2747 char *repo_path = NULL;
2748 char *id_str1 = NULL, *id_str2 = NULL;
2749 int ch;
2750 struct tog_view *view;
2752 SIMPLEQ_INIT(&refs);
2754 #ifndef PROFILE
2755 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2756 NULL) == -1)
2757 err(1, "pledge");
2758 #endif
2760 while ((ch = getopt(argc, argv, "")) != -1) {
2761 switch (ch) {
2762 default:
2763 usage_diff();
2764 /* NOTREACHED */
2768 argc -= optind;
2769 argv += optind;
2771 if (argc == 0) {
2772 usage_diff(); /* TODO show local worktree changes */
2773 } else if (argc == 2) {
2774 repo_path = getcwd(NULL, 0);
2775 if (repo_path == NULL)
2776 return got_error_from_errno("getcwd");
2777 id_str1 = argv[0];
2778 id_str2 = argv[1];
2779 } else if (argc == 3) {
2780 repo_path = realpath(argv[0], NULL);
2781 if (repo_path == NULL)
2782 return got_error_from_errno2("realpath", argv[0]);
2783 id_str1 = argv[1];
2784 id_str2 = argv[2];
2785 } else
2786 usage_diff();
2788 init_curses();
2790 error = got_repo_open(&repo, repo_path);
2791 if (error)
2792 goto done;
2794 error = apply_unveil(got_repo_get_path(repo), NULL);
2795 if (error)
2796 goto done;
2798 error = got_object_resolve_id_str(&id1, repo, id_str1);
2799 if (error)
2800 goto done;
2802 error = got_object_resolve_id_str(&id2, repo, id_str2);
2803 if (error)
2804 goto done;
2806 error = got_ref_list(&refs, repo);
2807 if (error)
2808 goto done;
2810 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2811 if (view == NULL) {
2812 error = got_error_from_errno("view_open");
2813 goto done;
2815 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2816 if (error)
2817 goto done;
2818 error = view_loop(view);
2819 done:
2820 free(repo_path);
2821 got_repo_close(repo);
2822 got_ref_list_free(&refs);
2823 return error;
2826 __dead static void
2827 usage_blame(void)
2829 endwin();
2830 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2831 getprogname());
2832 exit(1);
2835 struct tog_blame_line {
2836 int annotated;
2837 struct got_object_id *id;
2840 static const struct got_error *
2841 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2842 const char *path, struct tog_blame_line *lines, int nlines,
2843 int blame_complete, int selected_line, int *first_displayed_line,
2844 int *last_displayed_line, int *eof, int max_lines)
2846 const struct got_error *err;
2847 int lineno = 0, nprinted = 0;
2848 char *line;
2849 size_t len;
2850 wchar_t *wline;
2851 int width, wlimit;
2852 struct tog_blame_line *blame_line;
2853 struct got_object_id *prev_id = NULL;
2854 char *id_str;
2856 err = got_object_id_str(&id_str, id);
2857 if (err)
2858 return err;
2860 rewind(f);
2861 werase(view->window);
2863 if (asprintf(&line, "commit %s", id_str) == -1) {
2864 err = got_error_from_errno("asprintf");
2865 free(id_str);
2866 return err;
2869 err = format_line(&wline, &width, line, view->ncols);
2870 free(line);
2871 line = NULL;
2872 if (view_needs_focus_indication(view))
2873 wstandout(view->window);
2874 waddwstr(view->window, wline);
2875 if (view_needs_focus_indication(view))
2876 wstandend(view->window);
2877 free(wline);
2878 wline = NULL;
2879 if (width < view->ncols - 1)
2880 waddch(view->window, '\n');
2882 if (asprintf(&line, "[%d/%d] %s%s",
2883 *first_displayed_line - 1 + selected_line, nlines,
2884 blame_complete ? "" : "annotating... ", path) == -1) {
2885 free(id_str);
2886 return got_error_from_errno("asprintf");
2888 free(id_str);
2889 err = format_line(&wline, &width, line, view->ncols);
2890 free(line);
2891 line = NULL;
2892 if (err)
2893 return err;
2894 waddwstr(view->window, wline);
2895 free(wline);
2896 wline = NULL;
2897 if (width < view->ncols - 1)
2898 waddch(view->window, '\n');
2900 *eof = 0;
2901 while (nprinted < max_lines - 2) {
2902 line = parse_next_line(f, &len);
2903 if (line == NULL) {
2904 *eof = 1;
2905 break;
2907 if (++lineno < *first_displayed_line) {
2908 free(line);
2909 continue;
2912 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2913 err = format_line(&wline, &width, line, wlimit);
2914 if (err) {
2915 free(line);
2916 return err;
2919 if (view->focussed && nprinted == selected_line - 1)
2920 wstandout(view->window);
2922 blame_line = &lines[lineno - 1];
2923 if (blame_line->annotated && prev_id &&
2924 got_object_id_cmp(prev_id, blame_line->id) == 0)
2925 waddstr(view->window, " ");
2926 else if (blame_line->annotated) {
2927 char *id_str;
2928 err = got_object_id_str(&id_str, blame_line->id);
2929 if (err) {
2930 free(line);
2931 free(wline);
2932 return err;
2934 wprintw(view->window, "%.8s ", id_str);
2935 free(id_str);
2936 prev_id = blame_line->id;
2937 } else {
2938 waddstr(view->window, "........ ");
2939 prev_id = NULL;
2942 waddwstr(view->window, wline);
2943 while (width < wlimit) {
2944 waddch(view->window, ' ');
2945 width++;
2947 if (view->focussed && nprinted == selected_line - 1)
2948 wstandend(view->window);
2949 if (++nprinted == 1)
2950 *first_displayed_line = lineno;
2951 free(line);
2952 free(wline);
2953 wline = NULL;
2955 *last_displayed_line = lineno;
2957 view_vborder(view);
2959 return NULL;
2962 static const struct got_error *
2963 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2965 const struct got_error *err = NULL;
2966 struct tog_blame_cb_args *a = arg;
2967 struct tog_blame_line *line;
2968 int errcode;
2970 if (nlines != a->nlines ||
2971 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2972 return got_error(GOT_ERR_RANGE);
2974 errcode = pthread_mutex_lock(&tog_mutex);
2975 if (errcode)
2976 return got_error_set_errno(errcode, "pthread_mutex_lock");
2978 if (*a->quit) { /* user has quit the blame view */
2979 err = got_error(GOT_ERR_ITER_COMPLETED);
2980 goto done;
2983 if (lineno == -1)
2984 goto done; /* no change in this commit */
2986 line = &a->lines[lineno - 1];
2987 if (line->annotated)
2988 goto done;
2990 line->id = got_object_id_dup(id);
2991 if (line->id == NULL) {
2992 err = got_error_from_errno("got_object_id_dup");
2993 goto done;
2995 line->annotated = 1;
2996 done:
2997 errcode = pthread_mutex_unlock(&tog_mutex);
2998 if (errcode)
2999 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3000 return err;
3003 static void *
3004 blame_thread(void *arg)
3006 const struct got_error *err;
3007 struct tog_blame_thread_args *ta = arg;
3008 struct tog_blame_cb_args *a = ta->cb_args;
3009 int errcode;
3011 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3012 blame_cb, ta->cb_args);
3014 errcode = pthread_mutex_lock(&tog_mutex);
3015 if (errcode)
3016 return (void *)got_error_set_errno(errcode,
3017 "pthread_mutex_lock");
3019 got_repo_close(ta->repo);
3020 ta->repo = NULL;
3021 *ta->complete = 1;
3023 errcode = pthread_mutex_unlock(&tog_mutex);
3024 if (errcode && err == NULL)
3025 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3027 return (void *)err;
3030 static struct got_object_id *
3031 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3032 int selected_line)
3034 struct tog_blame_line *line;
3036 line = &lines[first_displayed_line - 1 + selected_line - 1];
3037 if (!line->annotated)
3038 return NULL;
3040 return line->id;
3043 static const struct got_error *
3044 stop_blame(struct tog_blame *blame)
3046 const struct got_error *err = NULL;
3047 int i;
3049 if (blame->thread) {
3050 int errcode;
3051 errcode = pthread_mutex_unlock(&tog_mutex);
3052 if (errcode)
3053 return got_error_set_errno(errcode,
3054 "pthread_mutex_unlock");
3055 errcode = pthread_join(blame->thread, (void **)&err);
3056 if (errcode)
3057 return got_error_set_errno(errcode, "pthread_join");
3058 errcode = pthread_mutex_lock(&tog_mutex);
3059 if (errcode)
3060 return got_error_set_errno(errcode,
3061 "pthread_mutex_lock");
3062 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3063 err = NULL;
3064 blame->thread = NULL;
3066 if (blame->thread_args.repo) {
3067 got_repo_close(blame->thread_args.repo);
3068 blame->thread_args.repo = NULL;
3070 if (blame->f) {
3071 if (fclose(blame->f) != 0 && err == NULL)
3072 err = got_error_from_errno("fclose");
3073 blame->f = NULL;
3075 if (blame->lines) {
3076 for (i = 0; i < blame->nlines; i++)
3077 free(blame->lines[i].id);
3078 free(blame->lines);
3079 blame->lines = NULL;
3081 free(blame->cb_args.commit_id);
3082 blame->cb_args.commit_id = NULL;
3084 return err;
3087 static const struct got_error *
3088 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3089 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3090 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3091 struct got_repository *repo)
3093 const struct got_error *err = NULL;
3094 struct got_blob_object *blob = NULL;
3095 struct got_repository *thread_repo = NULL;
3096 struct got_object_id *obj_id = NULL;
3097 int obj_type;
3099 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3100 if (err)
3101 return err;
3102 if (obj_id == NULL)
3103 return got_error(GOT_ERR_NO_OBJ);
3105 err = got_object_get_type(&obj_type, repo, obj_id);
3106 if (err)
3107 goto done;
3109 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3110 err = got_error(GOT_ERR_OBJ_TYPE);
3111 goto done;
3114 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3115 if (err)
3116 goto done;
3117 blame->f = got_opentemp();
3118 if (blame->f == NULL) {
3119 err = got_error_from_errno("got_opentemp");
3120 goto done;
3122 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3123 blame->f, blob);
3124 if (err)
3125 goto done;
3127 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3128 if (blame->lines == NULL) {
3129 err = got_error_from_errno("calloc");
3130 goto done;
3133 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3134 if (err)
3135 goto done;
3137 blame->cb_args.view = view;
3138 blame->cb_args.lines = blame->lines;
3139 blame->cb_args.nlines = blame->nlines;
3140 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3141 if (blame->cb_args.commit_id == NULL) {
3142 err = got_error_from_errno("got_object_id_dup");
3143 goto done;
3145 blame->cb_args.quit = done;
3147 blame->thread_args.path = path;
3148 blame->thread_args.repo = thread_repo;
3149 blame->thread_args.cb_args = &blame->cb_args;
3150 blame->thread_args.complete = blame_complete;
3151 *blame_complete = 0;
3153 done:
3154 if (blob)
3155 got_object_blob_close(blob);
3156 free(obj_id);
3157 if (err)
3158 stop_blame(blame);
3159 return err;
3162 static const struct got_error *
3163 open_blame_view(struct tog_view *view, char *path,
3164 struct got_object_id *commit_id, struct got_reflist_head *refs,
3165 struct got_repository *repo)
3167 const struct got_error *err = NULL;
3168 struct tog_blame_view_state *s = &view->state.blame;
3170 SIMPLEQ_INIT(&s->blamed_commits);
3172 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3173 if (err)
3174 return err;
3176 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3177 s->first_displayed_line = 1;
3178 s->last_displayed_line = view->nlines;
3179 s->selected_line = 1;
3180 s->blame_complete = 0;
3181 s->path = path;
3182 if (s->path == NULL)
3183 return got_error_from_errno("open_blame_view");
3184 s->repo = repo;
3185 s->refs = refs;
3186 s->commit_id = commit_id;
3187 memset(&s->blame, 0, sizeof(s->blame));
3189 view->show = show_blame_view;
3190 view->input = input_blame_view;
3191 view->close = close_blame_view;
3193 return run_blame(&s->blame, view, &s->blame_complete,
3194 &s->first_displayed_line, &s->last_displayed_line,
3195 &s->selected_line, &s->done, &s->eof, s->path,
3196 s->blamed_commit->id, s->repo);
3199 static const struct got_error *
3200 close_blame_view(struct tog_view *view)
3202 const struct got_error *err = NULL;
3203 struct tog_blame_view_state *s = &view->state.blame;
3205 if (s->blame.thread)
3206 err = stop_blame(&s->blame);
3208 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3209 struct got_object_qid *blamed_commit;
3210 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3211 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3212 got_object_qid_free(blamed_commit);
3215 free(s->path);
3217 return err;
3220 static const struct got_error *
3221 show_blame_view(struct tog_view *view)
3223 const struct got_error *err = NULL;
3224 struct tog_blame_view_state *s = &view->state.blame;
3225 int errcode;
3227 if (s->blame.thread == NULL) {
3228 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3229 &s->blame.thread_args);
3230 if (errcode)
3231 return got_error_set_errno(errcode, "pthread_create");
3234 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3235 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3236 s->selected_line, &s->first_displayed_line,
3237 &s->last_displayed_line, &s->eof, view->nlines);
3239 view_vborder(view);
3240 return err;
3243 static const struct got_error *
3244 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3245 struct tog_view **focus_view, struct tog_view *view, int ch)
3247 const struct got_error *err = NULL, *thread_err = NULL;
3248 struct tog_view *diff_view;
3249 struct tog_blame_view_state *s = &view->state.blame;
3250 int begin_x = 0;
3252 switch (ch) {
3253 case 'q':
3254 s->done = 1;
3255 break;
3256 case 'k':
3257 case KEY_UP:
3258 if (s->selected_line > 1)
3259 s->selected_line--;
3260 else if (s->selected_line == 1 &&
3261 s->first_displayed_line > 1)
3262 s->first_displayed_line--;
3263 break;
3264 case KEY_PPAGE:
3265 if (s->first_displayed_line == 1) {
3266 s->selected_line = 1;
3267 break;
3269 if (s->first_displayed_line > view->nlines - 2)
3270 s->first_displayed_line -=
3271 (view->nlines - 2);
3272 else
3273 s->first_displayed_line = 1;
3274 break;
3275 case 'j':
3276 case KEY_DOWN:
3277 if (s->selected_line < view->nlines - 2 &&
3278 s->first_displayed_line +
3279 s->selected_line <= s->blame.nlines)
3280 s->selected_line++;
3281 else if (s->last_displayed_line <
3282 s->blame.nlines)
3283 s->first_displayed_line++;
3284 break;
3285 case 'b':
3286 case 'p': {
3287 struct got_object_id *id = NULL;
3288 id = get_selected_commit_id(s->blame.lines,
3289 s->first_displayed_line, s->selected_line);
3290 if (id == NULL)
3291 break;
3292 if (ch == 'p') {
3293 struct got_commit_object *commit;
3294 struct got_object_qid *pid;
3295 struct got_object_id *blob_id = NULL;
3296 int obj_type;
3297 err = got_object_open_as_commit(&commit,
3298 s->repo, id);
3299 if (err)
3300 break;
3301 pid = SIMPLEQ_FIRST(
3302 got_object_commit_get_parent_ids(commit));
3303 if (pid == NULL) {
3304 got_object_commit_close(commit);
3305 break;
3307 /* Check if path history ends here. */
3308 err = got_object_id_by_path(&blob_id, s->repo,
3309 pid->id, s->path);
3310 if (err) {
3311 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3312 err = NULL;
3313 got_object_commit_close(commit);
3314 break;
3316 err = got_object_get_type(&obj_type, s->repo,
3317 blob_id);
3318 free(blob_id);
3319 /* Can't blame non-blob type objects. */
3320 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3321 got_object_commit_close(commit);
3322 break;
3324 err = got_object_qid_alloc(&s->blamed_commit,
3325 pid->id);
3326 got_object_commit_close(commit);
3327 } else {
3328 if (got_object_id_cmp(id,
3329 s->blamed_commit->id) == 0)
3330 break;
3331 err = got_object_qid_alloc(&s->blamed_commit,
3332 id);
3334 if (err)
3335 break;
3336 s->done = 1;
3337 thread_err = stop_blame(&s->blame);
3338 s->done = 0;
3339 if (thread_err)
3340 break;
3341 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3342 s->blamed_commit, entry);
3343 err = run_blame(&s->blame, view, &s->blame_complete,
3344 &s->first_displayed_line, &s->last_displayed_line,
3345 &s->selected_line, &s->done, &s->eof,
3346 s->path, s->blamed_commit->id, s->repo);
3347 if (err)
3348 break;
3349 break;
3351 case 'B': {
3352 struct got_object_qid *first;
3353 first = SIMPLEQ_FIRST(&s->blamed_commits);
3354 if (!got_object_id_cmp(first->id, s->commit_id))
3355 break;
3356 s->done = 1;
3357 thread_err = stop_blame(&s->blame);
3358 s->done = 0;
3359 if (thread_err)
3360 break;
3361 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3362 got_object_qid_free(s->blamed_commit);
3363 s->blamed_commit =
3364 SIMPLEQ_FIRST(&s->blamed_commits);
3365 err = run_blame(&s->blame, view, &s->blame_complete,
3366 &s->first_displayed_line, &s->last_displayed_line,
3367 &s->selected_line, &s->done, &s->eof, s->path,
3368 s->blamed_commit->id, s->repo);
3369 if (err)
3370 break;
3371 break;
3373 case KEY_ENTER:
3374 case '\r': {
3375 struct got_object_id *id = NULL;
3376 struct got_object_qid *pid;
3377 struct got_commit_object *commit = NULL;
3378 id = get_selected_commit_id(s->blame.lines,
3379 s->first_displayed_line, s->selected_line);
3380 if (id == NULL)
3381 break;
3382 err = got_object_open_as_commit(&commit, s->repo, id);
3383 if (err)
3384 break;
3385 pid = SIMPLEQ_FIRST(
3386 got_object_commit_get_parent_ids(commit));
3387 if (view_is_parent_view(view))
3388 begin_x = view_split_begin_x(view->begin_x);
3389 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3390 if (diff_view == NULL) {
3391 got_object_commit_close(commit);
3392 err = got_error_from_errno("view_open");
3393 break;
3395 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3396 id, NULL, s->refs, s->repo);
3397 got_object_commit_close(commit);
3398 if (err) {
3399 view_close(diff_view);
3400 break;
3402 if (view_is_parent_view(view)) {
3403 err = view_close_child(view);
3404 if (err)
3405 break;
3406 err = view_set_child(view, diff_view);
3407 if (err) {
3408 view_close(diff_view);
3409 break;
3411 *focus_view = diff_view;
3412 view->child_focussed = 1;
3413 } else
3414 *new_view = diff_view;
3415 if (err)
3416 break;
3417 break;
3419 case KEY_NPAGE:
3420 case ' ':
3421 if (s->last_displayed_line >= s->blame.nlines &&
3422 s->selected_line >= MIN(s->blame.nlines,
3423 view->nlines - 2)) {
3424 break;
3426 if (s->last_displayed_line >= s->blame.nlines &&
3427 s->selected_line < view->nlines - 2) {
3428 s->selected_line = MIN(s->blame.nlines,
3429 view->nlines - 2);
3430 break;
3432 if (s->last_displayed_line + view->nlines - 2
3433 <= s->blame.nlines)
3434 s->first_displayed_line +=
3435 view->nlines - 2;
3436 else
3437 s->first_displayed_line =
3438 s->blame.nlines -
3439 (view->nlines - 3);
3440 break;
3441 case KEY_RESIZE:
3442 if (s->selected_line > view->nlines - 2) {
3443 s->selected_line = MIN(s->blame.nlines,
3444 view->nlines - 2);
3446 break;
3447 default:
3448 break;
3450 return thread_err ? thread_err : err;
3453 static const struct got_error *
3454 cmd_blame(int argc, char *argv[])
3456 const struct got_error *error;
3457 struct got_repository *repo = NULL;
3458 struct got_reflist_head refs;
3459 struct got_worktree *worktree = NULL;
3460 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3461 struct got_object_id *commit_id = NULL;
3462 char *commit_id_str = NULL;
3463 int ch;
3464 struct tog_view *view;
3466 SIMPLEQ_INIT(&refs);
3468 #ifndef PROFILE
3469 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3470 NULL) == -1)
3471 err(1, "pledge");
3472 #endif
3474 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3475 switch (ch) {
3476 case 'c':
3477 commit_id_str = optarg;
3478 break;
3479 case 'r':
3480 repo_path = realpath(optarg, NULL);
3481 if (repo_path == NULL)
3482 err(1, "-r option");
3483 break;
3484 default:
3485 usage_blame();
3486 /* NOTREACHED */
3490 argc -= optind;
3491 argv += optind;
3493 if (argc == 1)
3494 path = argv[0];
3495 else
3496 usage_blame();
3498 cwd = getcwd(NULL, 0);
3499 if (cwd == NULL) {
3500 error = got_error_from_errno("getcwd");
3501 goto done;
3503 if (repo_path == NULL) {
3504 error = got_worktree_open(&worktree, cwd);
3505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3506 goto done;
3507 else
3508 error = NULL;
3509 if (worktree) {
3510 repo_path =
3511 strdup(got_worktree_get_repo_path(worktree));
3512 if (repo_path == NULL)
3513 error = got_error_from_errno("strdup");
3514 if (error)
3515 goto done;
3516 } else {
3517 repo_path = strdup(cwd);
3518 if (repo_path == NULL) {
3519 error = got_error_from_errno("strdup");
3520 goto done;
3525 init_curses();
3527 error = got_repo_open(&repo, repo_path);
3528 if (error != NULL)
3529 goto done;
3531 error = apply_unveil(got_repo_get_path(repo), NULL);
3532 if (error)
3533 goto done;
3535 if (worktree) {
3536 const char *prefix = got_worktree_get_path_prefix(worktree);
3537 char *p, *worktree_subdir = cwd +
3538 strlen(got_worktree_get_root_path(worktree));
3539 if (asprintf(&p, "%s%s%s%s%s",
3540 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3541 worktree_subdir, worktree_subdir[0] ? "/" : "",
3542 path) == -1) {
3543 error = got_error_from_errno("asprintf");
3544 goto done;
3546 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3547 free(p);
3548 } else {
3549 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3551 if (error)
3552 goto done;
3554 if (commit_id_str == NULL) {
3555 struct got_reference *head_ref;
3556 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3557 if (error != NULL)
3558 goto done;
3559 error = got_ref_resolve(&commit_id, repo, head_ref);
3560 got_ref_close(head_ref);
3561 } else {
3562 error = got_object_resolve_id_str(&commit_id, repo,
3563 commit_id_str);
3565 if (error != NULL)
3566 goto done;
3568 error = got_ref_list(&refs, repo);
3569 if (error)
3570 goto done;
3572 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3573 if (view == NULL) {
3574 error = got_error_from_errno("view_open");
3575 goto done;
3577 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3578 if (error)
3579 goto done;
3580 error = view_loop(view);
3581 done:
3582 free(repo_path);
3583 free(cwd);
3584 free(commit_id);
3585 if (worktree)
3586 got_worktree_close(worktree);
3587 if (repo)
3588 got_repo_close(repo);
3589 got_ref_list_free(&refs);
3590 return error;
3593 static const struct got_error *
3594 draw_tree_entries(struct tog_view *view,
3595 struct got_tree_entry **first_displayed_entry,
3596 struct got_tree_entry **last_displayed_entry,
3597 struct got_tree_entry **selected_entry, int *ndisplayed,
3598 const char *label, int show_ids, const char *parent_path,
3599 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3601 const struct got_error *err = NULL;
3602 struct got_tree_entry *te;
3603 wchar_t *wline;
3604 int width, n;
3606 *ndisplayed = 0;
3608 werase(view->window);
3610 if (limit == 0)
3611 return NULL;
3613 err = format_line(&wline, &width, label, view->ncols);
3614 if (err)
3615 return err;
3616 if (view_needs_focus_indication(view))
3617 wstandout(view->window);
3618 waddwstr(view->window, wline);
3619 if (view_needs_focus_indication(view))
3620 wstandend(view->window);
3621 free(wline);
3622 wline = NULL;
3623 if (width < view->ncols - 1)
3624 waddch(view->window, '\n');
3625 if (--limit <= 0)
3626 return NULL;
3627 err = format_line(&wline, &width, parent_path, view->ncols);
3628 if (err)
3629 return err;
3630 waddwstr(view->window, wline);
3631 free(wline);
3632 wline = NULL;
3633 if (width < view->ncols - 1)
3634 waddch(view->window, '\n');
3635 if (--limit <= 0)
3636 return NULL;
3637 waddch(view->window, '\n');
3638 if (--limit <= 0)
3639 return NULL;
3641 te = SIMPLEQ_FIRST(&entries->head);
3642 if (*first_displayed_entry == NULL) {
3643 if (selected == 0) {
3644 if (view->focussed)
3645 wstandout(view->window);
3646 *selected_entry = NULL;
3648 waddstr(view->window, " ..\n"); /* parent directory */
3649 if (selected == 0 && view->focussed)
3650 wstandend(view->window);
3651 (*ndisplayed)++;
3652 if (--limit <= 0)
3653 return NULL;
3654 n = 1;
3655 } else {
3656 n = 0;
3657 while (te != *first_displayed_entry)
3658 te = SIMPLEQ_NEXT(te, entry);
3661 while (te) {
3662 char *line = NULL, *id_str = NULL;
3664 if (show_ids) {
3665 err = got_object_id_str(&id_str, te->id);
3666 if (err)
3667 return got_error_from_errno(
3668 "got_object_id_str");
3670 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3671 te->name, S_ISDIR(te->mode) ? "/" :
3672 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3673 free(id_str);
3674 return got_error_from_errno("asprintf");
3676 free(id_str);
3677 err = format_line(&wline, &width, line, view->ncols);
3678 if (err) {
3679 free(line);
3680 break;
3682 if (n == selected) {
3683 if (view->focussed)
3684 wstandout(view->window);
3685 *selected_entry = te;
3687 waddwstr(view->window, wline);
3688 if (width < view->ncols - 1)
3689 waddch(view->window, '\n');
3690 if (n == selected && view->focussed)
3691 wstandend(view->window);
3692 free(line);
3693 free(wline);
3694 wline = NULL;
3695 n++;
3696 (*ndisplayed)++;
3697 *last_displayed_entry = te;
3698 if (--limit <= 0)
3699 break;
3700 te = SIMPLEQ_NEXT(te, entry);
3703 return err;
3706 static void
3707 tree_scroll_up(struct tog_view *view,
3708 struct got_tree_entry **first_displayed_entry, int maxscroll,
3709 const struct got_tree_entries *entries, int isroot)
3711 struct got_tree_entry *te, *prev;
3712 int i;
3714 if (*first_displayed_entry == NULL)
3715 return;
3717 te = SIMPLEQ_FIRST(&entries->head);
3718 if (*first_displayed_entry == te) {
3719 if (!isroot)
3720 *first_displayed_entry = NULL;
3721 return;
3724 /* XXX this is stupid... switch to TAILQ? */
3725 for (i = 0; i < maxscroll; i++) {
3726 while (te != *first_displayed_entry) {
3727 prev = te;
3728 te = SIMPLEQ_NEXT(te, entry);
3730 *first_displayed_entry = prev;
3731 te = SIMPLEQ_FIRST(&entries->head);
3733 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3734 *first_displayed_entry = NULL;
3737 static int
3738 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3739 struct got_tree_entry *last_displayed_entry,
3740 const struct got_tree_entries *entries)
3742 struct got_tree_entry *next, *last;
3743 int n = 0;
3745 if (*first_displayed_entry)
3746 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3747 else
3748 next = SIMPLEQ_FIRST(&entries->head);
3749 last = last_displayed_entry;
3750 while (next && last && n++ < maxscroll) {
3751 last = SIMPLEQ_NEXT(last, entry);
3752 if (last) {
3753 *first_displayed_entry = next;
3754 next = SIMPLEQ_NEXT(next, entry);
3757 return n;
3760 static const struct got_error *
3761 tree_entry_path(char **path, struct tog_parent_trees *parents,
3762 struct got_tree_entry *te)
3764 const struct got_error *err = NULL;
3765 struct tog_parent_tree *pt;
3766 size_t len = 2; /* for leading slash and NUL */
3768 TAILQ_FOREACH(pt, parents, entry)
3769 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3770 if (te)
3771 len += strlen(te->name);
3773 *path = calloc(1, len);
3774 if (path == NULL)
3775 return got_error_from_errno("calloc");
3777 (*path)[0] = '/';
3778 pt = TAILQ_LAST(parents, tog_parent_trees);
3779 while (pt) {
3780 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3781 err = got_error(GOT_ERR_NO_SPACE);
3782 goto done;
3784 if (strlcat(*path, "/", len) >= len) {
3785 err = got_error(GOT_ERR_NO_SPACE);
3786 goto done;
3788 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3790 if (te) {
3791 if (strlcat(*path, te->name, len) >= len) {
3792 err = got_error(GOT_ERR_NO_SPACE);
3793 goto done;
3796 done:
3797 if (err) {
3798 free(*path);
3799 *path = NULL;
3801 return err;
3804 static const struct got_error *
3805 blame_tree_entry(struct tog_view **new_view, int begin_x,
3806 struct got_tree_entry *te, struct tog_parent_trees *parents,
3807 struct got_object_id *commit_id, struct got_reflist_head *refs,
3808 struct got_repository *repo)
3810 const struct got_error *err = NULL;
3811 char *path;
3812 struct tog_view *blame_view;
3814 err = tree_entry_path(&path, parents, te);
3815 if (err)
3816 return err;
3818 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3819 if (blame_view == NULL)
3820 return got_error_from_errno("view_open");
3822 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3823 if (err) {
3824 view_close(blame_view);
3825 free(path);
3826 } else
3827 *new_view = blame_view;
3828 return err;
3831 static const struct got_error *
3832 log_tree_entry(struct tog_view **new_view, int begin_x,
3833 struct got_tree_entry *te, struct tog_parent_trees *parents,
3834 struct got_object_id *commit_id, struct got_reflist_head *refs,
3835 struct got_repository *repo)
3837 struct tog_view *log_view;
3838 const struct got_error *err = NULL;
3839 char *path;
3841 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3842 if (log_view == NULL)
3843 return got_error_from_errno("view_open");
3845 err = tree_entry_path(&path, parents, te);
3846 if (err)
3847 return err;
3849 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3850 if (err)
3851 view_close(log_view);
3852 else
3853 *new_view = log_view;
3854 free(path);
3855 return err;
3858 static const struct got_error *
3859 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3860 struct got_object_id *commit_id, struct got_reflist_head *refs,
3861 struct got_repository *repo)
3863 const struct got_error *err = NULL;
3864 char *commit_id_str = NULL;
3865 struct tog_tree_view_state *s = &view->state.tree;
3867 TAILQ_INIT(&s->parents);
3869 err = got_object_id_str(&commit_id_str, commit_id);
3870 if (err != NULL)
3871 goto done;
3873 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3874 err = got_error_from_errno("asprintf");
3875 goto done;
3878 s->root = s->tree = root;
3879 s->entries = got_object_tree_get_entries(root);
3880 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3881 s->commit_id = got_object_id_dup(commit_id);
3882 if (s->commit_id == NULL) {
3883 err = got_error_from_errno("got_object_id_dup");
3884 goto done;
3886 s->refs = refs;
3887 s->repo = repo;
3889 view->show = show_tree_view;
3890 view->input = input_tree_view;
3891 view->close = close_tree_view;
3892 done:
3893 free(commit_id_str);
3894 if (err) {
3895 free(s->tree_label);
3896 s->tree_label = NULL;
3898 return err;
3901 static const struct got_error *
3902 close_tree_view(struct tog_view *view)
3904 struct tog_tree_view_state *s = &view->state.tree;
3906 free(s->tree_label);
3907 s->tree_label = NULL;
3908 free(s->commit_id);
3909 s->commit_id = NULL;
3910 while (!TAILQ_EMPTY(&s->parents)) {
3911 struct tog_parent_tree *parent;
3912 parent = TAILQ_FIRST(&s->parents);
3913 TAILQ_REMOVE(&s->parents, parent, entry);
3914 free(parent);
3917 if (s->tree != s->root)
3918 got_object_tree_close(s->tree);
3919 got_object_tree_close(s->root);
3921 return NULL;
3924 static const struct got_error *
3925 show_tree_view(struct tog_view *view)
3927 const struct got_error *err = NULL;
3928 struct tog_tree_view_state *s = &view->state.tree;
3929 char *parent_path;
3931 err = tree_entry_path(&parent_path, &s->parents, NULL);
3932 if (err)
3933 return err;
3935 err = draw_tree_entries(view, &s->first_displayed_entry,
3936 &s->last_displayed_entry, &s->selected_entry,
3937 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3938 s->entries, s->selected, view->nlines, s->tree == s->root);
3939 free(parent_path);
3941 view_vborder(view);
3942 return err;
3945 static const struct got_error *
3946 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3947 struct tog_view **focus_view, struct tog_view *view, int ch)
3949 const struct got_error *err = NULL;
3950 struct tog_tree_view_state *s = &view->state.tree;
3951 struct tog_view *log_view;
3952 int begin_x = 0, nscrolled;
3954 switch (ch) {
3955 case 'i':
3956 s->show_ids = !s->show_ids;
3957 break;
3958 case 'l':
3959 if (!s->selected_entry)
3960 break;
3961 if (view_is_parent_view(view))
3962 begin_x = view_split_begin_x(view->begin_x);
3963 err = log_tree_entry(&log_view, begin_x,
3964 s->selected_entry, &s->parents,
3965 s->commit_id, s->refs, s->repo);
3966 if (view_is_parent_view(view)) {
3967 err = view_close_child(view);
3968 if (err)
3969 return err;
3970 err = view_set_child(view, log_view);
3971 if (err) {
3972 view_close(log_view);
3973 break;
3975 *focus_view = log_view;
3976 view->child_focussed = 1;
3977 } else
3978 *new_view = log_view;
3979 break;
3980 case 'k':
3981 case KEY_UP:
3982 if (s->selected > 0) {
3983 s->selected--;
3984 if (s->selected == 0)
3985 break;
3987 if (s->selected > 0)
3988 break;
3989 tree_scroll_up(view, &s->first_displayed_entry, 1,
3990 s->entries, s->tree == s->root);
3991 break;
3992 case KEY_PPAGE:
3993 tree_scroll_up(view, &s->first_displayed_entry,
3994 MAX(0, view->nlines - 4 - s->selected), s->entries,
3995 s->tree == s->root);
3996 s->selected = 0;
3997 if (SIMPLEQ_FIRST(&s->entries->head) ==
3998 s->first_displayed_entry && s->tree != s->root)
3999 s->first_displayed_entry = NULL;
4000 break;
4001 case 'j':
4002 case KEY_DOWN:
4003 if (s->selected < s->ndisplayed - 1) {
4004 s->selected++;
4005 break;
4007 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4008 /* can't scroll any further */
4009 break;
4010 tree_scroll_down(&s->first_displayed_entry, 1,
4011 s->last_displayed_entry, s->entries);
4012 break;
4013 case KEY_NPAGE:
4014 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4015 == NULL) {
4016 /* can't scroll any further; move cursor down */
4017 if (s->selected < s->ndisplayed - 1)
4018 s->selected = s->ndisplayed - 1;
4019 break;
4021 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4022 view->nlines, s->last_displayed_entry, s->entries);
4023 if (nscrolled < view->nlines) {
4024 int ndisplayed = 0;
4025 struct got_tree_entry *te;
4026 te = s->first_displayed_entry;
4027 do {
4028 ndisplayed++;
4029 te = SIMPLEQ_NEXT(te, entry);
4030 } while (te);
4031 s->selected = ndisplayed - 1;
4033 break;
4034 case KEY_ENTER:
4035 case '\r':
4036 case KEY_BACKSPACE:
4037 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4038 struct tog_parent_tree *parent;
4039 /* user selected '..' */
4040 if (s->tree == s->root)
4041 break;
4042 parent = TAILQ_FIRST(&s->parents);
4043 TAILQ_REMOVE(&s->parents, parent,
4044 entry);
4045 got_object_tree_close(s->tree);
4046 s->tree = parent->tree;
4047 s->entries =
4048 got_object_tree_get_entries(s->tree);
4049 s->first_displayed_entry =
4050 parent->first_displayed_entry;
4051 s->selected_entry =
4052 parent->selected_entry;
4053 s->selected = parent->selected;
4054 free(parent);
4055 } else if (S_ISDIR(s->selected_entry->mode)) {
4056 struct got_tree_object *subtree;
4057 err = got_object_open_as_tree(&subtree,
4058 s->repo, s->selected_entry->id);
4059 if (err)
4060 break;
4061 err = tree_view_visit_subtree(subtree, s);
4062 if (err) {
4063 got_object_tree_close(subtree);
4064 break;
4066 } else if (S_ISREG(s->selected_entry->mode)) {
4067 struct tog_view *blame_view;
4068 int begin_x = view_is_parent_view(view) ?
4069 view_split_begin_x(view->begin_x) : 0;
4071 err = blame_tree_entry(&blame_view, begin_x,
4072 s->selected_entry, &s->parents,
4073 s->commit_id, s->refs, s->repo);
4074 if (err)
4075 break;
4076 if (view_is_parent_view(view)) {
4077 err = view_close_child(view);
4078 if (err)
4079 return err;
4080 err = view_set_child(view, blame_view);
4081 if (err) {
4082 view_close(blame_view);
4083 break;
4085 *focus_view = blame_view;
4086 view->child_focussed = 1;
4087 } else
4088 *new_view = blame_view;
4090 break;
4091 case KEY_RESIZE:
4092 if (s->selected > view->nlines)
4093 s->selected = s->ndisplayed - 1;
4094 break;
4095 default:
4096 break;
4099 return err;
4102 __dead static void
4103 usage_tree(void)
4105 endwin();
4106 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4107 getprogname());
4108 exit(1);
4111 static const struct got_error *
4112 cmd_tree(int argc, char *argv[])
4114 const struct got_error *error;
4115 struct got_repository *repo = NULL;
4116 struct got_reflist_head refs;
4117 char *repo_path = NULL;
4118 struct got_object_id *commit_id = NULL;
4119 char *commit_id_arg = NULL;
4120 struct got_commit_object *commit = NULL;
4121 struct got_tree_object *tree = NULL;
4122 int ch;
4123 struct tog_view *view;
4125 SIMPLEQ_INIT(&refs);
4127 #ifndef PROFILE
4128 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4129 NULL) == -1)
4130 err(1, "pledge");
4131 #endif
4133 while ((ch = getopt(argc, argv, "c:")) != -1) {
4134 switch (ch) {
4135 case 'c':
4136 commit_id_arg = optarg;
4137 break;
4138 default:
4139 usage_tree();
4140 /* NOTREACHED */
4144 argc -= optind;
4145 argv += optind;
4147 if (argc == 0) {
4148 struct got_worktree *worktree;
4149 char *cwd = getcwd(NULL, 0);
4150 if (cwd == NULL)
4151 return got_error_from_errno("getcwd");
4152 error = got_worktree_open(&worktree, cwd);
4153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4154 goto done;
4155 if (worktree) {
4156 free(cwd);
4157 repo_path =
4158 strdup(got_worktree_get_repo_path(worktree));
4159 got_worktree_close(worktree);
4160 } else
4161 repo_path = cwd;
4162 if (repo_path == NULL) {
4163 error = got_error_from_errno("strdup");
4164 goto done;
4166 } else if (argc == 1) {
4167 repo_path = realpath(argv[0], NULL);
4168 if (repo_path == NULL)
4169 return got_error_from_errno2("realpath", argv[0]);
4170 } else
4171 usage_log();
4173 init_curses();
4175 error = got_repo_open(&repo, repo_path);
4176 if (error != NULL)
4177 goto done;
4179 error = apply_unveil(got_repo_get_path(repo), NULL);
4180 if (error)
4181 goto done;
4183 if (commit_id_arg == NULL)
4184 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4185 else
4186 error = got_object_resolve_id_str(&commit_id, repo,
4187 commit_id_arg);
4188 if (error != NULL)
4189 goto done;
4191 error = got_object_open_as_commit(&commit, repo, commit_id);
4192 if (error != NULL)
4193 goto done;
4195 error = got_object_open_as_tree(&tree, repo,
4196 got_object_commit_get_tree_id(commit));
4197 if (error != NULL)
4198 goto done;
4200 error = got_ref_list(&refs, repo);
4201 if (error)
4202 goto done;
4204 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4205 if (view == NULL) {
4206 error = got_error_from_errno("view_open");
4207 goto done;
4209 error = open_tree_view(view, tree, commit_id, &refs, repo);
4210 if (error)
4211 goto done;
4212 error = view_loop(view);
4213 done:
4214 free(repo_path);
4215 free(commit_id);
4216 if (commit)
4217 got_object_commit_close(commit);
4218 if (tree)
4219 got_object_tree_close(tree);
4220 if (repo)
4221 got_repo_close(repo);
4222 got_ref_list_free(&refs);
4223 return error;
4226 __dead static void
4227 usage(void)
4229 int i;
4231 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4232 "Available commands:\n", getprogname());
4233 for (i = 0; i < nitems(tog_commands); i++) {
4234 struct tog_cmd *cmd = &tog_commands[i];
4235 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4237 exit(1);
4240 static char **
4241 make_argv(const char *arg0, const char *arg1)
4243 char **argv;
4244 int argc = (arg1 == NULL ? 1 : 2);
4246 argv = calloc(argc, sizeof(char *));
4247 if (argv == NULL)
4248 err(1, "calloc");
4249 argv[0] = strdup(arg0);
4250 if (argv[0] == NULL)
4251 err(1, "calloc");
4252 if (arg1) {
4253 argv[1] = strdup(arg1);
4254 if (argv[1] == NULL)
4255 err(1, "calloc");
4258 return argv;
4261 int
4262 main(int argc, char *argv[])
4264 const struct got_error *error = NULL;
4265 struct tog_cmd *cmd = NULL;
4266 int ch, hflag = 0;
4267 char **cmd_argv = NULL;
4269 setlocale(LC_CTYPE, "");
4271 while ((ch = getopt(argc, argv, "h")) != -1) {
4272 switch (ch) {
4273 case 'h':
4274 hflag = 1;
4275 break;
4276 default:
4277 usage();
4278 /* NOTREACHED */
4282 argc -= optind;
4283 argv += optind;
4284 optind = 0;
4285 optreset = 1;
4287 if (argc == 0) {
4288 if (hflag)
4289 usage();
4290 /* Build an argument vector which runs a default command. */
4291 cmd = &tog_commands[0];
4292 cmd_argv = make_argv(cmd->name, NULL);
4293 argc = 1;
4294 } else {
4295 int i;
4297 /* Did the user specific a command? */
4298 for (i = 0; i < nitems(tog_commands); i++) {
4299 if (strncmp(tog_commands[i].name, argv[0],
4300 strlen(argv[0])) == 0) {
4301 cmd = &tog_commands[i];
4302 if (hflag)
4303 tog_commands[i].cmd_usage();
4304 break;
4307 if (cmd == NULL) {
4308 /* Did the user specify a repository? */
4309 char *repo_path = realpath(argv[0], NULL);
4310 if (repo_path) {
4311 struct got_repository *repo;
4312 error = got_repo_open(&repo, repo_path);
4313 if (error == NULL)
4314 got_repo_close(repo);
4315 } else
4316 error = got_error_from_errno2("realpath",
4317 argv[0]);
4318 if (error) {
4319 if (hflag) {
4320 fprintf(stderr, "%s: '%s' is not a "
4321 "known command\n", getprogname(),
4322 argv[0]);
4323 usage();
4325 fprintf(stderr, "%s: '%s' is neither a known "
4326 "command nor a path to a repository\n",
4327 getprogname(), argv[0]);
4328 free(repo_path);
4329 return 1;
4331 cmd = &tog_commands[0];
4332 cmd_argv = make_argv(cmd->name, repo_path);
4333 argc = 2;
4334 free(repo_path);
4338 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4339 if (error)
4340 goto done;
4341 done:
4342 endwin();
4343 free(cmd_argv);
4344 if (error)
4345 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4346 return 0;