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>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_path.h"
51 #include "got_worktree.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef MAX
58 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 #endif
61 #define CTRL(x) ((x) & 0x1f)
63 #ifndef nitems
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 #endif
67 struct tog_cmd {
68 const char *name;
69 const struct got_error *(*cmd_main)(int, char *[]);
70 void (*cmd_usage)(void);
71 const char *descr;
72 };
74 __dead static void usage(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log,
87 "show repository history" },
88 { "diff", cmd_diff, usage_diff,
89 "compare files and directories" },
90 { "blame", cmd_blame, usage_blame,
91 "show line-by-line file history" },
92 { "tree", cmd_tree, usage_tree,
93 "browse trees in repository" },
94 };
96 enum tog_view_type {
97 TOG_VIEW_DIFF,
98 TOG_VIEW_LOG,
99 TOG_VIEW_BLAME,
100 TOG_VIEW_TREE
101 };
103 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
109 int idx;
110 };
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
113 int ncommits;
114 struct commit_queue_head head;
115 };
117 struct tog_diff_view_state {
118 struct got_object_id *id1, *id2;
119 FILE *f;
120 int first_displayed_line;
121 int last_displayed_line;
122 int eof;
123 int diff_context;
124 struct got_repository *repo;
125 struct got_reflist_head *refs;
127 /* passed from log view; may be NULL */
128 struct tog_view *log_view;
129 };
131 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
133 struct tog_log_thread_args {
134 pthread_cond_t need_commits;
135 int commits_needed;
136 struct got_commit_graph *graph;
137 struct commit_queue *commits;
138 const char *in_repo_path;
139 struct got_object_id *start_id;
140 struct got_repository *repo;
141 int log_complete;
142 sig_atomic_t *quit;
143 struct tog_view *view;
144 struct commit_queue_entry **first_displayed_entry;
145 struct commit_queue_entry **selected_entry;
146 };
148 struct tog_log_view_state {
149 struct commit_queue commits;
150 struct commit_queue_entry *first_displayed_entry;
151 struct commit_queue_entry *last_displayed_entry;
152 struct commit_queue_entry *selected_entry;
153 int selected;
154 char *in_repo_path;
155 struct got_repository *repo;
156 struct got_reflist_head *refs;
157 struct got_object_id *start_id;
158 sig_atomic_t quit;
159 pthread_t thread;
160 struct tog_log_thread_args thread_args;
161 };
163 struct tog_blame_cb_args {
164 struct tog_blame_line *lines; /* one per line */
165 int nlines;
167 struct tog_view *view;
168 struct got_object_id *commit_id;
169 int *quit;
170 };
172 struct tog_blame_thread_args {
173 const char *path;
174 struct got_repository *repo;
175 struct tog_blame_cb_args *cb_args;
176 int *complete;
177 };
179 struct tog_blame {
180 FILE *f;
181 size_t filesize;
182 struct tog_blame_line *lines;
183 int nlines;
184 pthread_t thread;
185 struct tog_blame_thread_args thread_args;
186 struct tog_blame_cb_args cb_args;
187 const char *path;
188 };
190 struct tog_blame_view_state {
191 int first_displayed_line;
192 int last_displayed_line;
193 int selected_line;
194 int blame_complete;
195 int eof;
196 int done;
197 struct got_object_id_queue blamed_commits;
198 struct got_object_qid *blamed_commit;
199 char *path;
200 struct got_repository *repo;
201 struct got_reflist_head *refs;
202 struct got_object_id *commit_id;
203 struct tog_blame blame;
204 };
206 struct tog_parent_tree {
207 TAILQ_ENTRY(tog_parent_tree) entry;
208 struct got_tree_object *tree;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *selected_entry;
211 int selected;
212 };
214 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
216 struct tog_tree_view_state {
217 char *tree_label;
218 struct got_tree_object *root;
219 struct got_tree_object *tree;
220 const struct got_tree_entries *entries;
221 struct got_tree_entry *first_displayed_entry;
222 struct got_tree_entry *last_displayed_entry;
223 struct got_tree_entry *selected_entry;
224 int ndisplayed, selected, show_ids;
225 struct tog_parent_trees parents;
226 struct got_object_id *commit_id;
227 struct got_repository *repo;
228 struct got_reflist_head *refs;
229 };
231 /*
232 * We implement two types of views: parent views and child views.
234 * The 'Tab' key switches between a parent view and its child view.
235 * Child views are shown side-by-side to their parent view, provided
236 * there is enough screen estate.
238 * When a new view is opened from within a parent view, this new view
239 * becomes a child view of the parent view, replacing any existing child.
241 * When a new view is opened from within a child view, this new view
242 * becomes a parent view which will obscure the views below until the
243 * user quits the new parent view by typing 'q'.
245 * This list of views contains parent views only.
246 * Child views are only pointed to by their parent view.
247 */
248 TAILQ_HEAD(tog_view_list_head, tog_view);
250 struct tog_view {
251 TAILQ_ENTRY(tog_view) entry;
252 WINDOW *window;
253 PANEL *panel;
254 int nlines, ncols, begin_y, begin_x;
255 int lines, cols; /* copies of LINES and COLS */
256 int focussed;
257 struct tog_view *parent;
258 struct tog_view *child;
259 int child_focussed;
261 /* type-specific state */
262 enum tog_view_type type;
263 union {
264 struct tog_diff_view_state diff;
265 struct tog_log_view_state log;
266 struct tog_blame_view_state blame;
267 struct tog_tree_view_state tree;
268 } state;
270 const struct got_error *(*show)(struct tog_view *);
271 const struct got_error *(*input)(struct tog_view **,
272 struct tog_view **, struct tog_view**, struct tog_view *, int);
273 const struct got_error *(*close)(struct tog_view *);
274 };
276 static const struct got_error *open_diff_view(struct tog_view *,
277 struct got_object_id *, struct got_object_id *, struct tog_view *,
278 struct got_reflist_head *, struct got_repository *);
279 static const struct got_error *show_diff_view(struct tog_view *);
280 static const struct got_error *input_diff_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error* close_diff_view(struct tog_view *);
284 static const struct got_error *open_log_view(struct tog_view *,
285 struct got_object_id *, struct got_reflist_head *,
286 struct got_repository *, const char *, int);
287 static const struct got_error * show_log_view(struct tog_view *);
288 static const struct got_error *input_log_view(struct tog_view **,
289 struct tog_view **, struct tog_view **, struct tog_view *, int);
290 static const struct got_error *close_log_view(struct tog_view *);
292 static const struct got_error *open_blame_view(struct tog_view *, char *,
293 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
294 static const struct got_error *show_blame_view(struct tog_view *);
295 static const struct got_error *input_blame_view(struct tog_view **,
296 struct tog_view **, struct tog_view **, struct tog_view *, int);
297 static const struct got_error *close_blame_view(struct tog_view *);
299 static const struct got_error *open_tree_view(struct tog_view *,
300 struct got_tree_object *, struct got_object_id *,
301 struct got_reflist_head *, struct got_repository *);
302 static const struct got_error *show_tree_view(struct tog_view *);
303 static const struct got_error *input_tree_view(struct tog_view **,
304 struct tog_view **, struct tog_view **, struct tog_view *, int);
305 static const struct got_error *close_tree_view(struct tog_view *);
307 static volatile sig_atomic_t tog_sigwinch_received;
309 static void
310 tog_sigwinch(int signo)
312 tog_sigwinch_received = 1;
315 static const struct got_error *
316 view_close(struct tog_view *view)
318 const struct got_error *err = NULL;
320 if (view->child) {
321 view_close(view->child);
322 view->child = NULL;
324 if (view->close)
325 err = view->close(view);
326 if (view->panel)
327 del_panel(view->panel);
328 if (view->window)
329 delwin(view->window);
330 free(view);
331 return err;
334 static struct tog_view *
335 view_open(int nlines, int ncols, int begin_y, int begin_x,
336 enum tog_view_type type)
338 struct tog_view *view = calloc(1, sizeof(*view));
340 if (view == NULL)
341 return NULL;
343 view->type = type;
344 view->lines = LINES;
345 view->cols = COLS;
346 view->nlines = nlines ? nlines : LINES - begin_y;
347 view->ncols = ncols ? ncols : COLS - begin_x;
348 view->begin_y = begin_y;
349 view->begin_x = begin_x;
350 view->window = newwin(nlines, ncols, begin_y, begin_x);
351 if (view->window == NULL) {
352 view_close(view);
353 return NULL;
355 view->panel = new_panel(view->window);
356 if (view->panel == NULL ||
357 set_panel_userptr(view->panel, view) != OK) {
358 view_close(view);
359 return NULL;
362 keypad(view->window, TRUE);
363 return view;
366 static int
367 view_split_begin_x(int begin_x)
369 if (begin_x > 0 || COLS < 120)
370 return 0;
371 return (COLS - MAX(COLS / 2, 80));
374 static const struct got_error *view_resize(struct tog_view *);
376 static const struct got_error *
377 view_splitscreen(struct tog_view *view)
379 const struct got_error *err = NULL;
381 view->begin_y = 0;
382 view->begin_x = view_split_begin_x(0);
383 view->nlines = LINES;
384 view->ncols = COLS - view->begin_x;
385 view->lines = LINES;
386 view->cols = COLS;
387 err = view_resize(view);
388 if (err)
389 return err;
391 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
392 return got_error_from_errno("mvwin");
394 return NULL;
397 static const struct got_error *
398 view_fullscreen(struct tog_view *view)
400 const struct got_error *err = NULL;
402 view->begin_x = 0;
403 view->begin_y = 0;
404 view->nlines = LINES;
405 view->ncols = COLS;
406 view->lines = LINES;
407 view->cols = COLS;
408 err = view_resize(view);
409 if (err)
410 return err;
412 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
413 return got_error_from_errno("mvwin");
415 return NULL;
418 static int
419 view_is_parent_view(struct tog_view *view)
421 return view->parent == NULL;
424 static const struct got_error *
425 view_resize(struct tog_view *view)
427 int nlines, ncols;
429 if (view->lines > LINES)
430 nlines = view->nlines - (view->lines - LINES);
431 else
432 nlines = view->nlines + (LINES - view->lines);
434 if (view->cols > COLS)
435 ncols = view->ncols - (view->cols - COLS);
436 else
437 ncols = view->ncols + (COLS - view->cols);
439 if (wresize(view->window, nlines, ncols) == ERR)
440 return got_error_from_errno("wresize");
441 if (replace_panel(view->panel, view->window) == ERR)
442 return got_error_from_errno("replace_panel");
443 wclear(view->window);
445 view->nlines = nlines;
446 view->ncols = ncols;
447 view->lines = LINES;
448 view->cols = COLS;
450 if (view->child) {
451 view->child->begin_x = view_split_begin_x(view->begin_x);
452 if (view->child->begin_x == 0) {
453 view_fullscreen(view->child);
454 if (view->child->focussed)
455 show_panel(view->child->panel);
456 else
457 show_panel(view->panel);
458 } else {
459 view_splitscreen(view->child);
460 show_panel(view->child->panel);
464 return NULL;
467 static const struct got_error *
468 view_close_child(struct tog_view *view)
470 const struct got_error *err = NULL;
472 if (view->child == NULL)
473 return NULL;
475 err = view_close(view->child);
476 view->child = NULL;
477 return err;
480 static const struct got_error *
481 view_set_child(struct tog_view *view, struct tog_view *child)
483 const struct got_error *err = NULL;
485 view->child = child;
486 child->parent = view;
487 return err;
490 static int
491 view_is_splitscreen(struct tog_view *view)
493 return view->begin_x > 0;
496 static void
497 tog_resizeterm(void)
499 int cols, lines;
500 struct winsize size;
502 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
503 cols = 80; /* Default */
504 lines = 24;
505 } else {
506 cols = size.ws_col;
507 lines = size.ws_row;
509 resize_term(lines, cols);
512 static const struct got_error *
513 view_input(struct tog_view **new, struct tog_view **dead,
514 struct tog_view **focus, int *done, struct tog_view *view,
515 struct tog_view_list_head *views)
517 const struct got_error *err = NULL;
518 struct tog_view *v;
519 int ch, errcode;
521 *new = NULL;
522 *dead = NULL;
523 *focus = NULL;
525 nodelay(stdscr, FALSE);
526 /* Allow threads to make progress while we are waiting for input. */
527 errcode = pthread_mutex_unlock(&tog_mutex);
528 if (errcode)
529 return got_error_set_errno(errcode, "pthread_mutex_unlock");
530 ch = wgetch(view->window);
531 errcode = pthread_mutex_lock(&tog_mutex);
532 if (errcode)
533 return got_error_set_errno(errcode, "pthread_mutex_lock");
534 nodelay(stdscr, TRUE);
536 if (tog_sigwinch_received) {
537 tog_resizeterm();
538 tog_sigwinch_received = 0;
539 TAILQ_FOREACH(v, views, entry) {
540 err = view_resize(v);
541 if (err)
542 return err;
543 err = v->input(new, dead, focus, v, KEY_RESIZE);
544 if (err)
545 return err;
549 switch (ch) {
550 case ERR:
551 break;
552 case '\t':
553 if (view->child) {
554 *focus = view->child;
555 view->child_focussed = 1;
556 } else if (view->parent) {
557 *focus = view->parent;
558 view->parent->child_focussed = 0;
560 break;
561 case 'q':
562 err = view->input(new, dead, focus, view, ch);
563 *dead = view;
564 break;
565 case 'Q':
566 *done = 1;
567 break;
568 case 'f':
569 if (view_is_parent_view(view)) {
570 if (view->child == NULL)
571 break;
572 if (view_is_splitscreen(view->child)) {
573 *focus = view->child;
574 view->child_focussed = 1;
575 err = view_fullscreen(view->child);
576 } else
577 err = view_splitscreen(view->child);
578 if (err)
579 break;
580 err = view->child->input(new, dead, focus,
581 view->child, KEY_RESIZE);
582 } else {
583 if (view_is_splitscreen(view)) {
584 *focus = view;
585 view->parent->child_focussed = 1;
586 err = view_fullscreen(view);
587 } else {
588 err = view_splitscreen(view);
590 if (err)
591 break;
592 err = view->input(new, dead, focus, view,
593 KEY_RESIZE);
595 break;
596 case KEY_RESIZE:
597 break;
598 default:
599 err = view->input(new, dead, focus, view, ch);
600 break;
603 return err;
606 void
607 view_vborder(struct tog_view *view)
609 PANEL *panel;
610 struct tog_view *view_above;
612 if (view->parent)
613 return view_vborder(view->parent);
615 panel = panel_above(view->panel);
616 if (panel == NULL)
617 return;
619 view_above = panel_userptr(panel);
620 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
621 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
624 int
625 view_needs_focus_indication(struct tog_view *view)
627 if (view_is_parent_view(view)) {
628 if (view->child == NULL || view->child_focussed)
629 return 0;
630 if (!view_is_splitscreen(view->child))
631 return 0;
632 } else if (!view_is_splitscreen(view))
633 return 0;
635 return view->focussed;
638 static const struct got_error *
639 view_loop(struct tog_view *view)
641 const struct got_error *err = NULL;
642 struct tog_view_list_head views;
643 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
644 int fast_refresh = 10;
645 int done = 0, errcode;
647 errcode = pthread_mutex_lock(&tog_mutex);
648 if (errcode)
649 return got_error_set_errno(errcode, "pthread_mutex_lock");
651 TAILQ_INIT(&views);
652 TAILQ_INSERT_HEAD(&views, view, entry);
654 main_view = view;
655 view->focussed = 1;
656 err = view->show(view);
657 if (err)
658 return err;
659 update_panels();
660 doupdate();
661 while (!TAILQ_EMPTY(&views) && !done) {
662 /* Refresh fast during initialization, then become slower. */
663 if (fast_refresh && fast_refresh-- == 0)
664 halfdelay(10); /* switch to once per second */
666 err = view_input(&new_view, &dead_view, &focus_view, &done,
667 view, &views);
668 if (err)
669 break;
670 if (dead_view) {
671 struct tog_view *prev = NULL;
673 if (view_is_parent_view(dead_view))
674 prev = TAILQ_PREV(dead_view,
675 tog_view_list_head, entry);
676 else if (view->parent != dead_view)
677 prev = view->parent;
679 if (dead_view->parent)
680 dead_view->parent->child = NULL;
681 else
682 TAILQ_REMOVE(&views, dead_view, entry);
684 err = view_close(dead_view);
685 if (err || dead_view == main_view)
686 goto done;
688 if (view == dead_view) {
689 if (focus_view)
690 view = focus_view;
691 else if (prev)
692 view = prev;
693 else if (!TAILQ_EMPTY(&views))
694 view = TAILQ_LAST(&views,
695 tog_view_list_head);
696 else
697 view = NULL;
698 if (view) {
699 if (view->child && view->child_focussed)
700 focus_view = view->child;
701 else
702 focus_view = view;
706 if (new_view) {
707 struct tog_view *v, *t;
708 /* Only allow one parent view per type. */
709 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
710 if (v->type != new_view->type)
711 continue;
712 TAILQ_REMOVE(&views, v, entry);
713 err = view_close(v);
714 if (err)
715 goto done;
716 break;
718 TAILQ_INSERT_TAIL(&views, new_view, entry);
719 view = new_view;
720 if (focus_view == NULL)
721 focus_view = new_view;
723 if (focus_view) {
724 show_panel(focus_view->panel);
725 if (view)
726 view->focussed = 0;
727 focus_view->focussed = 1;
728 view = focus_view;
729 if (new_view)
730 show_panel(new_view->panel);
731 if (view->child && view_is_splitscreen(view->child))
732 show_panel(view->child->panel);
734 if (view) {
735 if (focus_view == NULL) {
736 view->focussed = 1;
737 show_panel(view->panel);
738 if (view->child && view_is_splitscreen(view->child))
739 show_panel(view->child->panel);
740 focus_view = view;
742 if (view->parent) {
743 err = view->parent->show(view->parent);
744 if (err)
745 goto done;
747 err = view->show(view);
748 if (err)
749 goto done;
750 if (view->child) {
751 err = view->child->show(view->child);
752 if (err)
753 goto done;
755 update_panels();
756 doupdate();
759 done:
760 while (!TAILQ_EMPTY(&views)) {
761 view = TAILQ_FIRST(&views);
762 TAILQ_REMOVE(&views, view, entry);
763 view_close(view);
766 errcode = pthread_mutex_unlock(&tog_mutex);
767 if (errcode)
768 return got_error_set_errno(errcode, "pthread_mutex_unlock");
770 return err;
773 __dead static void
774 usage_log(void)
776 endwin();
777 fprintf(stderr,
778 "usage: %s log [-c commit] [-r repository-path] [path]\n",
779 getprogname());
780 exit(1);
783 /* Create newly allocated wide-character string equivalent to a byte string. */
784 static const struct got_error *
785 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
787 char *vis = NULL;
788 const struct got_error *err = NULL;
790 *ws = NULL;
791 *wlen = mbstowcs(NULL, s, 0);
792 if (*wlen == (size_t)-1) {
793 int vislen;
794 if (errno != EILSEQ)
795 return got_error_from_errno("mbstowcs");
797 /* byte string invalid in current encoding; try to "fix" it */
798 err = got_mbsavis(&vis, &vislen, s);
799 if (err)
800 return err;
801 *wlen = mbstowcs(NULL, vis, 0);
802 if (*wlen == (size_t)-1) {
803 err = got_error_from_errno("mbstowcs"); /* give up */
804 goto done;
808 *ws = calloc(*wlen + 1, sizeof(*ws));
809 if (*ws == NULL) {
810 err = got_error_from_errno("calloc");
811 goto done;
814 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
815 err = got_error_from_errno("mbstowcs");
816 done:
817 free(vis);
818 if (err) {
819 free(*ws);
820 *ws = NULL;
821 *wlen = 0;
823 return err;
826 /* Format a line for display, ensuring that it won't overflow a width limit. */
827 static const struct got_error *
828 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
830 const struct got_error *err = NULL;
831 int cols = 0;
832 wchar_t *wline = NULL;
833 size_t wlen;
834 int i;
836 *wlinep = NULL;
837 *widthp = 0;
839 err = mbs2ws(&wline, &wlen, line);
840 if (err)
841 return err;
843 i = 0;
844 while (i < wlen && cols < wlimit) {
845 int width = wcwidth(wline[i]);
846 switch (width) {
847 case 0:
848 i++;
849 break;
850 case 1:
851 case 2:
852 if (cols + width <= wlimit)
853 cols += width;
854 i++;
855 break;
856 case -1:
857 if (wline[i] == L'\t')
858 cols += TABSIZE - ((cols + 1) % TABSIZE);
859 i++;
860 break;
861 default:
862 err = got_error_from_errno("wcwidth");
863 goto done;
866 wline[i] = L'\0';
867 if (widthp)
868 *widthp = cols;
869 done:
870 if (err)
871 free(wline);
872 else
873 *wlinep = wline;
874 return err;
877 static const struct got_error*
878 build_refs_str(char **refs_str, struct got_reflist_head *refs,
879 struct got_object_id *id)
881 static const struct got_error *err = NULL;
882 struct got_reflist_entry *re;
883 char *s;
884 const char *name;
886 *refs_str = NULL;
888 SIMPLEQ_FOREACH(re, refs, entry) {
889 if (got_object_id_cmp(re->id, id) != 0)
890 continue;
891 name = got_ref_get_name(re->ref);
892 if (strcmp(name, GOT_REF_HEAD) == 0)
893 continue;
894 if (strncmp(name, "refs/", 5) == 0)
895 name += 5;
896 if (strncmp(name, "got/", 4) == 0)
897 continue;
898 if (strncmp(name, "heads/", 6) == 0)
899 name += 6;
900 if (strncmp(name, "remotes/", 8) == 0)
901 name += 8;
902 s = *refs_str;
903 if (asprintf(refs_str, "%s%s%s", s ? s : "",
904 s ? ", " : "", name) == -1) {
905 err = got_error_from_errno("asprintf");
906 free(s);
907 *refs_str = NULL;
908 break;
910 free(s);
913 return err;
916 static const struct got_error *
917 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
919 char *smallerthan, *at;
921 smallerthan = strchr(author, '<');
922 if (smallerthan && smallerthan[1] != '\0')
923 author = smallerthan + 1;
924 at = strchr(author, '@');
925 if (at)
926 *at = '\0';
927 return format_line(wauthor, author_width, author, limit);
930 static const struct got_error *
931 draw_commit(struct tog_view *view, struct got_commit_object *commit,
932 struct got_object_id *id, struct got_reflist_head *refs,
933 int author_display_cols)
935 const struct got_error *err = NULL;
936 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
937 char *logmsg0 = NULL, *logmsg = NULL;
938 char *author = NULL;
939 wchar_t *wlogmsg = NULL, *wauthor = NULL;
940 int author_width, logmsg_width;
941 char *newline, *line = NULL;
942 int col, limit;
943 static const size_t date_display_cols = 9;
944 const int avail = view->ncols;
945 struct tm tm;
946 time_t committer_time;
948 committer_time = got_object_commit_get_committer_time(commit);
949 if (localtime_r(&committer_time, &tm) == NULL)
950 return got_error_from_errno("localtime_r");
951 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
952 >= sizeof(datebuf))
953 return got_error(GOT_ERR_NO_SPACE);
955 if (avail < date_display_cols)
956 limit = MIN(sizeof(datebuf) - 1, avail);
957 else
958 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
959 waddnstr(view->window, datebuf, limit);
960 col = limit + 1;
961 if (col > avail)
962 goto done;
964 author = strdup(got_object_commit_get_author(commit));
965 if (author == NULL) {
966 err = got_error_from_errno("strdup");
967 goto done;
969 err = format_author(&wauthor, &author_width, author, avail - col);
970 if (err)
971 goto done;
972 waddwstr(view->window, wauthor);
973 col += author_width;
974 while (col <= avail && author_width < author_display_cols + 2) {
975 waddch(view->window, ' ');
976 col++;
977 author_width++;
979 if (col > avail)
980 goto done;
982 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
983 if (logmsg0 == NULL) {
984 err = got_error_from_errno("strdup");
985 goto done;
987 logmsg = logmsg0;
988 while (*logmsg == '\n')
989 logmsg++;
990 newline = strchr(logmsg, '\n');
991 if (newline)
992 *newline = '\0';
993 limit = avail - col;
994 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
995 if (err)
996 goto done;
997 waddwstr(view->window, wlogmsg);
998 col += logmsg_width;
999 while (col <= avail) {
1000 waddch(view->window, ' ');
1001 col++;
1003 done:
1004 free(logmsg0);
1005 free(wlogmsg);
1006 free(author);
1007 free(wauthor);
1008 free(line);
1009 return err;
1012 static struct commit_queue_entry *
1013 alloc_commit_queue_entry(struct got_commit_object *commit,
1014 struct got_object_id *id)
1016 struct commit_queue_entry *entry;
1018 entry = calloc(1, sizeof(*entry));
1019 if (entry == NULL)
1020 return NULL;
1022 entry->id = id;
1023 entry->commit = commit;
1024 return entry;
1027 static void
1028 pop_commit(struct commit_queue *commits)
1030 struct commit_queue_entry *entry;
1032 entry = TAILQ_FIRST(&commits->head);
1033 TAILQ_REMOVE(&commits->head, entry, entry);
1034 got_object_commit_close(entry->commit);
1035 commits->ncommits--;
1036 /* Don't free entry->id! It is owned by the commit graph. */
1037 free(entry);
1040 static void
1041 free_commits(struct commit_queue *commits)
1043 while (!TAILQ_EMPTY(&commits->head))
1044 pop_commit(commits);
1047 static const struct got_error *
1048 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1049 int minqueue, struct got_repository *repo, const char *path)
1051 const struct got_error *err = NULL;
1052 int nqueued = 0;
1055 * We keep all commits open throughout the lifetime of the log
1056 * view in order to avoid having to re-fetch commits from disk
1057 * while updating the display.
1059 while (nqueued < minqueue) {
1060 struct got_object_id *id;
1061 struct got_commit_object *commit;
1062 struct commit_queue_entry *entry;
1063 int errcode;
1065 err = got_commit_graph_iter_next(&id, graph);
1066 if (err) {
1067 if (err->code != GOT_ERR_ITER_NEED_MORE)
1068 break;
1069 err = got_commit_graph_fetch_commits(graph,
1070 minqueue, repo);
1071 if (err)
1072 return err;
1073 continue;
1076 if (id == NULL)
1077 break;
1079 err = got_object_open_as_commit(&commit, repo, id);
1080 if (err)
1081 break;
1082 entry = alloc_commit_queue_entry(commit, id);
1083 if (entry == NULL) {
1084 err = got_error_from_errno("alloc_commit_queue_entry");
1085 break;
1088 errcode = pthread_mutex_lock(&tog_mutex);
1089 if (errcode) {
1090 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1091 break;
1094 entry->idx = commits->ncommits;
1095 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1096 nqueued++;
1097 commits->ncommits++;
1099 errcode = pthread_mutex_unlock(&tog_mutex);
1100 if (errcode && err == NULL)
1101 err = got_error_set_errno(errcode,
1102 "pthread_mutex_unlock");
1105 return err;
1108 static const struct got_error *
1109 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1111 const struct got_error *err = NULL;
1112 struct got_reference *head_ref;
1114 *head_id = NULL;
1116 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1117 if (err)
1118 return err;
1120 err = got_ref_resolve(head_id, repo, head_ref);
1121 got_ref_close(head_ref);
1122 if (err) {
1123 *head_id = NULL;
1124 return err;
1127 return NULL;
1130 static const struct got_error *
1131 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1132 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1133 struct commit_queue *commits, int selected_idx, int limit,
1134 struct got_reflist_head *refs, const char *path, int commits_needed)
1136 const struct got_error *err = NULL;
1137 struct commit_queue_entry *entry;
1138 int ncommits, width;
1139 int author_cols = 10;
1140 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1141 char *refs_str = NULL;
1142 wchar_t *wline;
1144 entry = first;
1145 ncommits = 0;
1146 while (entry) {
1147 if (ncommits == selected_idx) {
1148 *selected = entry;
1149 break;
1151 entry = TAILQ_NEXT(entry, entry);
1152 ncommits++;
1155 if (*selected) {
1156 err = got_object_id_str(&id_str, (*selected)->id);
1157 if (err)
1158 return err;
1159 if (refs) {
1160 err = build_refs_str(&refs_str, refs, (*selected)->id);
1161 if (err)
1162 goto done;
1166 if (commits_needed == 0)
1167 halfdelay(10); /* disable fast refresh */
1169 if (asprintf(&ncommits_str, " [%d/%d] %s",
1170 entry ? entry->idx + 1 : 0, commits->ncommits,
1171 commits_needed > 0 ? "loading... " :
1172 (refs_str ? refs_str : "")) == -1) {
1173 err = got_error_from_errno("asprintf");
1174 goto done;
1177 if (path && strcmp(path, "/") != 0) {
1178 if (asprintf(&header, "commit %s %s%s",
1179 id_str ? id_str : "........................................",
1180 path, ncommits_str) == -1) {
1181 err = got_error_from_errno("asprintf");
1182 header = NULL;
1183 goto done;
1185 } else if (asprintf(&header, "commit %s%s",
1186 id_str ? id_str : "........................................",
1187 ncommits_str) == -1) {
1188 err = got_error_from_errno("asprintf");
1189 header = NULL;
1190 goto done;
1192 err = format_line(&wline, &width, header, view->ncols);
1193 if (err)
1194 goto done;
1196 werase(view->window);
1198 if (view_needs_focus_indication(view))
1199 wstandout(view->window);
1200 waddwstr(view->window, wline);
1201 while (width < view->ncols) {
1202 waddch(view->window, ' ');
1203 width++;
1205 if (view_needs_focus_indication(view))
1206 wstandend(view->window);
1207 free(wline);
1208 if (limit <= 1)
1209 goto done;
1211 /* Grow author column size if necessary. */
1212 entry = first;
1213 ncommits = 0;
1214 while (entry) {
1215 char *author;
1216 wchar_t *wauthor;
1217 int width;
1218 if (ncommits >= limit - 1)
1219 break;
1220 author = strdup(got_object_commit_get_author(entry->commit));
1221 if (author == NULL) {
1222 err = got_error_from_errno("strdup");
1223 goto done;
1225 err = format_author(&wauthor, &width, author, COLS);
1226 if (author_cols < width)
1227 author_cols = width;
1228 free(wauthor);
1229 free(author);
1230 entry = TAILQ_NEXT(entry, entry);
1233 entry = first;
1234 *last = first;
1235 ncommits = 0;
1236 while (entry) {
1237 if (ncommits >= limit - 1)
1238 break;
1239 if (ncommits == selected_idx)
1240 wstandout(view->window);
1241 err = draw_commit(view, entry->commit, entry->id, refs,
1242 author_cols);
1243 if (ncommits == selected_idx)
1244 wstandend(view->window);
1245 if (err)
1246 break;
1247 ncommits++;
1248 *last = entry;
1249 entry = TAILQ_NEXT(entry, entry);
1252 view_vborder(view);
1253 done:
1254 free(id_str);
1255 free(refs_str);
1256 free(ncommits_str);
1257 free(header);
1258 return err;
1261 static void
1262 scroll_up(struct tog_view *view,
1263 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1264 struct commit_queue *commits)
1266 struct commit_queue_entry *entry;
1267 int nscrolled = 0;
1269 entry = TAILQ_FIRST(&commits->head);
1270 if (*first_displayed_entry == entry)
1271 return;
1273 entry = *first_displayed_entry;
1274 while (entry && nscrolled < maxscroll) {
1275 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1276 if (entry) {
1277 *first_displayed_entry = entry;
1278 nscrolled++;
1283 static const struct got_error *
1284 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1285 pthread_cond_t *need_commits)
1287 int errcode;
1288 int max_wait = 20;
1290 halfdelay(1); /* fast refresh while loading commits */
1292 while (*commits_needed > 0) {
1293 if (*log_complete)
1294 break;
1296 /* Wake the log thread. */
1297 errcode = pthread_cond_signal(need_commits);
1298 if (errcode)
1299 return got_error_set_errno(errcode,
1300 "pthread_cond_signal");
1301 errcode = pthread_mutex_unlock(&tog_mutex);
1302 if (errcode)
1303 return got_error_set_errno(errcode,
1304 "pthread_mutex_unlock");
1305 pthread_yield();
1306 errcode = pthread_mutex_lock(&tog_mutex);
1307 if (errcode)
1308 return got_error_set_errno(errcode,
1309 "pthread_mutex_lock");
1311 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1313 * Thread is not done yet; lose a key press
1314 * and let the user retry... this way the GUI
1315 * remains interactive while logging deep paths
1316 * with few commits in history.
1318 return NULL;
1322 return NULL;
1325 static const struct got_error *
1326 scroll_down(struct tog_view *view,
1327 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1328 struct commit_queue_entry **last_displayed_entry,
1329 struct commit_queue *commits, int *log_complete, int *commits_needed,
1330 pthread_cond_t *need_commits)
1332 const struct got_error *err = NULL;
1333 struct commit_queue_entry *pentry;
1334 int nscrolled = 0;
1336 if (*last_displayed_entry == NULL)
1337 return NULL;
1339 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1340 if (pentry == NULL && !*log_complete) {
1342 * Ask the log thread for required amount of commits
1343 * plus some amount of pre-fetching.
1345 (*commits_needed) += maxscroll + 20;
1346 err = trigger_log_thread(0, commits_needed, log_complete,
1347 need_commits);
1348 if (err)
1349 return err;
1352 do {
1353 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1354 if (pentry == NULL)
1355 break;
1357 *last_displayed_entry = pentry;
1359 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1360 if (pentry == NULL)
1361 break;
1362 *first_displayed_entry = pentry;
1363 } while (++nscrolled < maxscroll);
1365 return err;
1368 static const struct got_error *
1369 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1370 struct got_commit_object *commit, struct got_object_id *commit_id,
1371 struct tog_view *log_view, struct got_reflist_head *refs,
1372 struct got_repository *repo)
1374 const struct got_error *err;
1375 struct got_object_qid *parent_id;
1376 struct tog_view *diff_view;
1378 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1379 if (diff_view == NULL)
1380 return got_error_from_errno("view_open");
1382 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1383 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1384 commit_id, log_view, refs, repo);
1385 if (err == NULL)
1386 *new_view = diff_view;
1387 return err;
1390 static const struct got_error *
1391 browse_commit(struct tog_view **new_view, int begin_x,
1392 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1393 struct got_repository *repo)
1395 const struct got_error *err = NULL;
1396 struct got_tree_object *tree;
1397 struct tog_view *tree_view;
1399 err = got_object_open_as_tree(&tree, repo,
1400 got_object_commit_get_tree_id(entry->commit));
1401 if (err)
1402 return err;
1404 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1405 if (tree_view == NULL)
1406 return got_error_from_errno("view_open");
1408 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1409 if (err)
1410 got_object_tree_close(tree);
1411 else
1412 *new_view = tree_view;
1413 return err;
1416 static void *
1417 log_thread(void *arg)
1419 const struct got_error *err = NULL;
1420 int errcode = 0;
1421 struct tog_log_thread_args *a = arg;
1422 int done = 0;
1424 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1425 if (err)
1426 return (void *)err;
1428 while (!done && !err) {
1429 err = queue_commits(a->graph, a->commits, 1, a->repo,
1430 a->in_repo_path);
1431 if (err) {
1432 if (err->code != GOT_ERR_ITER_COMPLETED)
1433 return (void *)err;
1434 err = NULL;
1435 done = 1;
1436 } else if (a->commits_needed > 0)
1437 a->commits_needed--;
1439 errcode = pthread_mutex_lock(&tog_mutex);
1440 if (errcode) {
1441 err = got_error_set_errno(errcode,
1442 "pthread_mutex_lock");
1443 break;
1444 } else if (*a->quit)
1445 done = 1;
1446 else if (*a->first_displayed_entry == NULL) {
1447 *a->first_displayed_entry =
1448 TAILQ_FIRST(&a->commits->head);
1449 *a->selected_entry = *a->first_displayed_entry;
1452 if (done)
1453 a->commits_needed = 0;
1454 else if (a->commits_needed == 0) {
1455 errcode = pthread_cond_wait(&a->need_commits,
1456 &tog_mutex);
1457 if (errcode)
1458 err = got_error_set_errno(errcode,
1459 "pthread_cond_wait");
1462 errcode = pthread_mutex_unlock(&tog_mutex);
1463 if (errcode && err == NULL)
1464 err = got_error_set_errno(errcode,
1465 "pthread_mutex_unlock");
1467 a->log_complete = 1;
1468 return (void *)err;
1471 static const struct got_error *
1472 stop_log_thread(struct tog_log_view_state *s)
1474 const struct got_error *err = NULL;
1475 int errcode;
1477 if (s->thread) {
1478 s->quit = 1;
1479 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1480 if (errcode)
1481 return got_error_set_errno(errcode,
1482 "pthread_cond_signal");
1483 errcode = pthread_mutex_unlock(&tog_mutex);
1484 if (errcode)
1485 return got_error_set_errno(errcode,
1486 "pthread_mutex_unlock");
1487 errcode = pthread_join(s->thread, (void **)&err);
1488 if (errcode)
1489 return got_error_set_errno(errcode, "pthread_join");
1490 errcode = pthread_mutex_lock(&tog_mutex);
1491 if (errcode)
1492 return got_error_set_errno(errcode,
1493 "pthread_mutex_lock");
1494 s->thread = NULL;
1497 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1498 if (errcode && err == NULL)
1499 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1501 if (s->thread_args.repo) {
1502 got_repo_close(s->thread_args.repo);
1503 s->thread_args.repo = NULL;
1506 if (s->thread_args.graph) {
1507 got_commit_graph_close(s->thread_args.graph);
1508 s->thread_args.graph = NULL;
1511 return err;
1514 static const struct got_error *
1515 close_log_view(struct tog_view *view)
1517 const struct got_error *err = NULL;
1518 struct tog_log_view_state *s = &view->state.log;
1520 err = stop_log_thread(s);
1521 free_commits(&s->commits);
1522 free(s->in_repo_path);
1523 s->in_repo_path = NULL;
1524 free(s->start_id);
1525 s->start_id = NULL;
1526 return err;
1529 static const struct got_error *
1530 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1531 struct got_reflist_head *refs, struct got_repository *repo,
1532 const char *path, int check_disk)
1534 const struct got_error *err = NULL;
1535 struct tog_log_view_state *s = &view->state.log;
1536 struct got_repository *thread_repo = NULL;
1537 struct got_commit_graph *thread_graph = NULL;
1538 int errcode;
1540 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1541 if (err != NULL)
1542 goto done;
1544 /* The commit queue only contains commits being displayed. */
1545 TAILQ_INIT(&s->commits.head);
1546 s->commits.ncommits = 0;
1548 s->refs = refs;
1549 s->repo = repo;
1550 s->start_id = got_object_id_dup(start_id);
1551 if (s->start_id == NULL) {
1552 err = got_error_from_errno("got_object_id_dup");
1553 goto done;
1556 view->show = show_log_view;
1557 view->input = input_log_view;
1558 view->close = close_log_view;
1560 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1561 if (err)
1562 goto done;
1563 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1564 0, thread_repo);
1565 if (err)
1566 goto done;
1568 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1569 if (errcode) {
1570 err = got_error_set_errno(errcode, "pthread_cond_init");
1571 goto done;
1574 s->thread_args.commits_needed = view->nlines;
1575 s->thread_args.graph = thread_graph;
1576 s->thread_args.commits = &s->commits;
1577 s->thread_args.in_repo_path = s->in_repo_path;
1578 s->thread_args.start_id = s->start_id;
1579 s->thread_args.repo = thread_repo;
1580 s->thread_args.log_complete = 0;
1581 s->thread_args.quit = &s->quit;
1582 s->thread_args.view = view;
1583 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1584 s->thread_args.selected_entry = &s->selected_entry;
1585 done:
1586 if (err)
1587 close_log_view(view);
1588 return err;
1591 static const struct got_error *
1592 show_log_view(struct tog_view *view)
1594 struct tog_log_view_state *s = &view->state.log;
1596 if (s->thread == NULL) {
1597 int errcode = pthread_create(&s->thread, NULL, log_thread,
1598 &s->thread_args);
1599 if (errcode)
1600 return got_error_set_errno(errcode, "pthread_create");
1603 return draw_commits(view, &s->last_displayed_entry,
1604 &s->selected_entry, s->first_displayed_entry,
1605 &s->commits, s->selected, view->nlines, s->refs,
1606 s->in_repo_path, s->thread_args.commits_needed);
1609 static const struct got_error *
1610 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1611 struct tog_view **focus_view, struct tog_view *view, int ch)
1613 const struct got_error *err = NULL;
1614 struct tog_log_view_state *s = &view->state.log;
1615 char *parent_path;
1616 struct tog_view *diff_view = NULL, *tree_view = NULL;
1617 int begin_x = 0;
1619 switch (ch) {
1620 case 'q':
1621 s->quit = 1;
1622 break;
1623 case 'k':
1624 case KEY_UP:
1625 case '<':
1626 case ',':
1627 if (s->first_displayed_entry == NULL)
1628 break;
1629 if (s->selected > 0)
1630 s->selected--;
1631 if (s->selected > 0)
1632 break;
1633 scroll_up(view, &s->first_displayed_entry, 1,
1634 &s->commits);
1635 break;
1636 case KEY_PPAGE:
1637 case CTRL('b'):
1638 if (s->first_displayed_entry == NULL)
1639 break;
1640 if (TAILQ_FIRST(&s->commits.head) ==
1641 s->first_displayed_entry) {
1642 s->selected = 0;
1643 break;
1645 scroll_up(view, &s->first_displayed_entry,
1646 view->nlines, &s->commits);
1647 break;
1648 case 'j':
1649 case KEY_DOWN:
1650 case '>':
1651 case '.':
1652 if (s->first_displayed_entry == NULL)
1653 break;
1654 if (s->selected < MIN(view->nlines - 2,
1655 s->commits.ncommits - 1)) {
1656 s->selected++;
1657 break;
1659 err = scroll_down(view, &s->first_displayed_entry, 1,
1660 &s->last_displayed_entry, &s->commits,
1661 &s->thread_args.log_complete,
1662 &s->thread_args.commits_needed,
1663 &s->thread_args.need_commits);
1664 break;
1665 case KEY_NPAGE:
1666 case CTRL('f'): {
1667 struct commit_queue_entry *first;
1668 first = s->first_displayed_entry;
1669 if (first == NULL)
1670 break;
1671 err = scroll_down(view, &s->first_displayed_entry,
1672 view->nlines, &s->last_displayed_entry,
1673 &s->commits, &s->thread_args.log_complete,
1674 &s->thread_args.commits_needed,
1675 &s->thread_args.need_commits);
1676 if (first == s->first_displayed_entry &&
1677 s->selected < MIN(view->nlines - 2,
1678 s->commits.ncommits - 1)) {
1679 /* can't scroll further down */
1680 s->selected = MIN(view->nlines - 2,
1681 s->commits.ncommits - 1);
1683 err = NULL;
1684 break;
1686 case KEY_RESIZE:
1687 if (s->selected > view->nlines - 2)
1688 s->selected = view->nlines - 2;
1689 if (s->selected > s->commits.ncommits - 1)
1690 s->selected = s->commits.ncommits - 1;
1691 break;
1692 case KEY_ENTER:
1693 case ' ':
1694 case '\r':
1695 if (s->selected_entry == NULL)
1696 break;
1697 if (view_is_parent_view(view))
1698 begin_x = view_split_begin_x(view->begin_x);
1699 err = open_diff_view_for_commit(&diff_view, begin_x,
1700 s->selected_entry->commit, s->selected_entry->id,
1701 view, s->refs, s->repo);
1702 if (err)
1703 break;
1704 if (view_is_parent_view(view)) {
1705 err = view_close_child(view);
1706 if (err)
1707 return err;
1708 err = view_set_child(view, diff_view);
1709 if (err) {
1710 view_close(diff_view);
1711 break;
1713 *focus_view = diff_view;
1714 view->child_focussed = 1;
1715 } else
1716 *new_view = diff_view;
1717 break;
1718 case 't':
1719 if (s->selected_entry == NULL)
1720 break;
1721 if (view_is_parent_view(view))
1722 begin_x = view_split_begin_x(view->begin_x);
1723 err = browse_commit(&tree_view, begin_x,
1724 s->selected_entry, s->refs, s->repo);
1725 if (err)
1726 break;
1727 if (view_is_parent_view(view)) {
1728 err = view_close_child(view);
1729 if (err)
1730 return err;
1731 err = view_set_child(view, tree_view);
1732 if (err) {
1733 view_close(tree_view);
1734 break;
1736 *focus_view = tree_view;
1737 view->child_focussed = 1;
1738 } else
1739 *new_view = tree_view;
1740 break;
1741 case KEY_BACKSPACE:
1742 if (strcmp(s->in_repo_path, "/") == 0)
1743 break;
1744 parent_path = dirname(s->in_repo_path);
1745 if (parent_path && strcmp(parent_path, ".") != 0) {
1746 struct tog_view *lv;
1747 err = stop_log_thread(s);
1748 if (err)
1749 return err;
1750 lv = view_open(view->nlines, view->ncols,
1751 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1752 if (lv == NULL)
1753 return got_error_from_errno(
1754 "view_open");
1755 err = open_log_view(lv, s->start_id, s->refs,
1756 s->repo, parent_path, 0);
1757 if (err)
1758 return err;;
1759 if (view_is_parent_view(view))
1760 *new_view = lv;
1761 else {
1762 view_set_child(view->parent, lv);
1763 *focus_view = lv;
1765 return NULL;
1767 break;
1768 default:
1769 break;
1772 return err;
1775 static const struct got_error *
1776 apply_unveil(const char *repo_path, const char *worktree_path)
1778 const struct got_error *error;
1780 if (repo_path && unveil(repo_path, "r") != 0)
1781 return got_error_from_errno2("unveil", repo_path);
1783 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1784 return got_error_from_errno2("unveil", worktree_path);
1786 if (unveil("/tmp", "rwc") != 0)
1787 return got_error_from_errno2("unveil", "/tmp");
1789 error = got_privsep_unveil_exec_helpers();
1790 if (error != NULL)
1791 return error;
1793 if (unveil(NULL, NULL) != 0)
1794 return got_error_from_errno("unveil");
1796 return NULL;
1799 static void
1800 init_curses(void)
1802 initscr();
1803 cbreak();
1804 halfdelay(1); /* Do fast refresh while initial view is loading. */
1805 noecho();
1806 nonl();
1807 intrflush(stdscr, FALSE);
1808 keypad(stdscr, TRUE);
1809 curs_set(0);
1810 signal(SIGWINCH, tog_sigwinch);
1813 static const struct got_error *
1814 cmd_log(int argc, char *argv[])
1816 const struct got_error *error;
1817 struct got_repository *repo = NULL;
1818 struct got_worktree *worktree = NULL;
1819 struct got_reflist_head refs;
1820 struct got_object_id *start_id = NULL;
1821 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1822 char *start_commit = NULL;
1823 int ch;
1824 struct tog_view *view;
1826 SIMPLEQ_INIT(&refs);
1828 #ifndef PROFILE
1829 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1830 NULL) == -1)
1831 err(1, "pledge");
1832 #endif
1834 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1835 switch (ch) {
1836 case 'c':
1837 start_commit = optarg;
1838 break;
1839 case 'r':
1840 repo_path = realpath(optarg, NULL);
1841 if (repo_path == NULL)
1842 err(1, "-r option");
1843 break;
1844 default:
1845 usage_log();
1846 /* NOTREACHED */
1850 argc -= optind;
1851 argv += optind;
1853 cwd = getcwd(NULL, 0);
1854 if (cwd == NULL) {
1855 error = got_error_from_errno("getcwd");
1856 goto done;
1858 error = got_worktree_open(&worktree, cwd);
1859 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1860 goto done;
1861 error = NULL;
1863 if (argc == 0) {
1864 path = strdup("");
1865 if (path == NULL) {
1866 error = got_error_from_errno("strdup");
1867 goto done;
1869 } else if (argc == 1) {
1870 if (worktree) {
1871 error = got_worktree_resolve_path(&path, worktree,
1872 argv[0]);
1873 if (error)
1874 goto done;
1875 } else {
1876 path = strdup(argv[0]);
1877 if (path == NULL) {
1878 error = got_error_from_errno("strdup");
1879 goto done;
1882 } else
1883 usage_log();
1885 repo_path = worktree ?
1886 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1887 if (repo_path == NULL) {
1888 error = got_error_from_errno("strdup");
1889 goto done;
1892 init_curses();
1894 error = got_repo_open(&repo, repo_path);
1895 if (error != NULL)
1896 goto done;
1898 error = apply_unveil(got_repo_get_path(repo),
1899 worktree ? got_worktree_get_root_path(worktree) : NULL);
1900 if (error)
1901 goto done;
1903 if (start_commit == NULL)
1904 error = get_head_commit_id(&start_id, repo);
1905 else
1906 error = got_object_resolve_id_str(&start_id, repo,
1907 start_commit);
1908 if (error != NULL)
1909 goto done;
1911 error = got_ref_list(&refs, repo);
1912 if (error)
1913 goto done;
1915 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1916 if (view == NULL) {
1917 error = got_error_from_errno("view_open");
1918 goto done;
1920 error = open_log_view(view, start_id, &refs, repo, path, 1);
1921 if (error)
1922 goto done;
1923 error = view_loop(view);
1924 done:
1925 free(repo_path);
1926 free(cwd);
1927 free(path);
1928 free(start_id);
1929 if (repo)
1930 got_repo_close(repo);
1931 if (worktree)
1932 got_worktree_close(worktree);
1933 got_ref_list_free(&refs);
1934 return error;
1937 __dead static void
1938 usage_diff(void)
1940 endwin();
1941 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1942 getprogname());
1943 exit(1);
1946 static char *
1947 parse_next_line(FILE *f, size_t *len)
1949 char *line;
1950 size_t linelen;
1951 size_t lineno;
1952 const char delim[3] = { '\0', '\0', '\0'};
1954 line = fparseln(f, &linelen, &lineno, delim, 0);
1955 if (len)
1956 *len = linelen;
1957 return line;
1960 static const struct got_error *
1961 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1962 int *last_displayed_line, int *eof, int max_lines,
1963 char *header)
1965 const struct got_error *err;
1966 int nlines = 0, nprinted = 0;
1967 char *line;
1968 size_t len;
1969 wchar_t *wline;
1970 int width;
1972 rewind(f);
1973 werase(view->window);
1975 if (header) {
1976 err = format_line(&wline, &width, header, view->ncols);
1977 if (err) {
1978 return err;
1981 if (view_needs_focus_indication(view))
1982 wstandout(view->window);
1983 waddwstr(view->window, wline);
1984 if (view_needs_focus_indication(view))
1985 wstandend(view->window);
1986 if (width < view->ncols)
1987 waddch(view->window, '\n');
1989 if (max_lines <= 1)
1990 return NULL;
1991 max_lines--;
1994 *eof = 0;
1995 while (nprinted < max_lines) {
1996 line = parse_next_line(f, &len);
1997 if (line == NULL) {
1998 *eof = 1;
1999 break;
2001 if (++nlines < *first_displayed_line) {
2002 free(line);
2003 continue;
2006 err = format_line(&wline, &width, line, view->ncols);
2007 if (err) {
2008 free(line);
2009 return err;
2011 waddwstr(view->window, wline);
2012 if (width < view->ncols)
2013 waddch(view->window, '\n');
2014 if (++nprinted == 1)
2015 *first_displayed_line = nlines;
2016 free(line);
2017 free(wline);
2018 wline = NULL;
2020 *last_displayed_line = nlines;
2022 view_vborder(view);
2024 if (*eof) {
2025 while (nprinted < view->nlines) {
2026 waddch(view->window, '\n');
2027 nprinted++;
2030 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2031 if (err) {
2032 return err;
2035 wstandout(view->window);
2036 waddwstr(view->window, wline);
2037 wstandend(view->window);
2040 return NULL;
2043 static char *
2044 get_datestr(time_t *time, char *datebuf)
2046 char *p, *s = ctime_r(time, datebuf);
2047 p = strchr(s, '\n');
2048 if (p)
2049 *p = '\0';
2050 return s;
2053 static const struct got_error *
2054 write_commit_info(struct got_object_id *commit_id,
2055 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2057 const struct got_error *err = NULL;
2058 char datebuf[26];
2059 struct got_commit_object *commit;
2060 char *id_str = NULL;
2061 time_t committer_time;
2062 const char *author, *committer;
2063 char *refs_str = NULL;
2065 if (refs) {
2066 err = build_refs_str(&refs_str, refs, commit_id);
2067 if (err)
2068 return err;
2071 err = got_object_open_as_commit(&commit, repo, commit_id);
2072 if (err)
2073 return err;
2075 err = got_object_id_str(&id_str, commit_id);
2076 if (err) {
2077 err = got_error_from_errno("got_object_id_str");
2078 goto done;
2081 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2082 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2083 err = got_error_from_errno("fprintf");
2084 goto done;
2086 if (fprintf(outfile, "from: %s\n",
2087 got_object_commit_get_author(commit)) < 0) {
2088 err = got_error_from_errno("fprintf");
2089 goto done;
2091 committer_time = got_object_commit_get_committer_time(commit);
2092 if (fprintf(outfile, "date: %s UTC\n",
2093 get_datestr(&committer_time, datebuf)) < 0) {
2094 err = got_error_from_errno("fprintf");
2095 goto done;
2097 author = got_object_commit_get_author(commit);
2098 committer = got_object_commit_get_committer(commit);
2099 if (strcmp(author, committer) != 0 &&
2100 fprintf(outfile, "via: %s\n", committer) < 0) {
2101 err = got_error_from_errno("fprintf");
2102 goto done;
2104 if (fprintf(outfile, "%s\n",
2105 got_object_commit_get_logmsg(commit)) < 0) {
2106 err = got_error_from_errno("fprintf");
2107 goto done;
2109 done:
2110 free(id_str);
2111 free(refs_str);
2112 got_object_commit_close(commit);
2113 return err;
2116 static const struct got_error *
2117 create_diff(struct tog_diff_view_state *s)
2119 const struct got_error *err = NULL;
2120 FILE *f = NULL;
2121 int obj_type;
2123 f = got_opentemp();
2124 if (f == NULL) {
2125 err = got_error_from_errno("got_opentemp");
2126 goto done;
2128 if (s->f && fclose(s->f) != 0) {
2129 err = got_error_from_errno("fclose");
2130 goto done;
2132 s->f = f;
2134 if (s->id1)
2135 err = got_object_get_type(&obj_type, s->repo, s->id1);
2136 else
2137 err = got_object_get_type(&obj_type, s->repo, s->id2);
2138 if (err)
2139 goto done;
2141 switch (obj_type) {
2142 case GOT_OBJ_TYPE_BLOB:
2143 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2144 s->diff_context, s->repo, f);
2145 break;
2146 case GOT_OBJ_TYPE_TREE:
2147 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2148 s->diff_context, s->repo, f);
2149 break;
2150 case GOT_OBJ_TYPE_COMMIT: {
2151 const struct got_object_id_queue *parent_ids;
2152 struct got_object_qid *pid;
2153 struct got_commit_object *commit2;
2155 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2156 if (err)
2157 break;
2158 /* Show commit info if we're diffing to a parent/root commit. */
2159 if (s->id1 == NULL)
2160 write_commit_info(s->id2, s->refs, s->repo, f);
2161 else {
2162 parent_ids = got_object_commit_get_parent_ids(commit2);
2163 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2164 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2165 write_commit_info(s->id2, s->refs,
2166 s->repo, f);
2167 break;
2171 got_object_commit_close(commit2);
2173 err = got_diff_objects_as_commits(s->id1, s->id2,
2174 s->diff_context, s->repo, f);
2175 break;
2177 default:
2178 err = got_error(GOT_ERR_OBJ_TYPE);
2179 break;
2181 done:
2182 if (f && fflush(f) != 0 && err == NULL)
2183 err = got_error_from_errno("fflush");
2184 return err;
2187 static void
2188 diff_view_indicate_progress(struct tog_view *view)
2190 mvwaddstr(view->window, 0, 0, "diffing...");
2191 update_panels();
2192 doupdate();
2195 static const struct got_error *
2196 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2197 struct got_object_id *id2, struct tog_view *log_view,
2198 struct got_reflist_head *refs, struct got_repository *repo)
2200 const struct got_error *err;
2202 if (id1 != NULL && id2 != NULL) {
2203 int type1, type2;
2204 err = got_object_get_type(&type1, repo, id1);
2205 if (err)
2206 return err;
2207 err = got_object_get_type(&type2, repo, id2);
2208 if (err)
2209 return err;
2211 if (type1 != type2)
2212 return got_error(GOT_ERR_OBJ_TYPE);
2215 if (id1) {
2216 view->state.diff.id1 = got_object_id_dup(id1);
2217 if (view->state.diff.id1 == NULL)
2218 return got_error_from_errno("got_object_id_dup");
2219 } else
2220 view->state.diff.id1 = NULL;
2222 view->state.diff.id2 = got_object_id_dup(id2);
2223 if (view->state.diff.id2 == NULL) {
2224 free(view->state.diff.id1);
2225 view->state.diff.id1 = NULL;
2226 return got_error_from_errno("got_object_id_dup");
2228 view->state.diff.f = NULL;
2229 view->state.diff.first_displayed_line = 1;
2230 view->state.diff.last_displayed_line = view->nlines;
2231 view->state.diff.diff_context = 3;
2232 view->state.diff.log_view = log_view;
2233 view->state.diff.repo = repo;
2234 view->state.diff.refs = refs;
2236 if (log_view && view_is_splitscreen(view))
2237 show_log_view(log_view); /* draw vborder */
2238 diff_view_indicate_progress(view);
2240 err = create_diff(&view->state.diff);
2241 if (err) {
2242 free(view->state.diff.id1);
2243 view->state.diff.id1 = NULL;
2244 free(view->state.diff.id2);
2245 view->state.diff.id2 = NULL;
2246 return err;
2249 view->show = show_diff_view;
2250 view->input = input_diff_view;
2251 view->close = close_diff_view;
2253 return NULL;
2256 static const struct got_error *
2257 close_diff_view(struct tog_view *view)
2259 const struct got_error *err = NULL;
2261 free(view->state.diff.id1);
2262 view->state.diff.id1 = NULL;
2263 free(view->state.diff.id2);
2264 view->state.diff.id2 = NULL;
2265 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2266 err = got_error_from_errno("fclose");
2267 return err;
2270 static const struct got_error *
2271 show_diff_view(struct tog_view *view)
2273 const struct got_error *err;
2274 struct tog_diff_view_state *s = &view->state.diff;
2275 char *id_str1 = NULL, *id_str2, *header;
2277 if (s->id1) {
2278 err = got_object_id_str(&id_str1, s->id1);
2279 if (err)
2280 return err;
2282 err = got_object_id_str(&id_str2, s->id2);
2283 if (err)
2284 return err;
2286 if (asprintf(&header, "diff %s %s",
2287 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2288 err = got_error_from_errno("asprintf");
2289 free(id_str1);
2290 free(id_str2);
2291 return err;
2293 free(id_str1);
2294 free(id_str2);
2296 return draw_file(view, s->f, &s->first_displayed_line,
2297 &s->last_displayed_line, &s->eof, view->nlines,
2298 header);
2301 static const struct got_error *
2302 set_selected_commit(struct tog_diff_view_state *s,
2303 struct commit_queue_entry *entry)
2305 const struct got_error *err;
2306 const struct got_object_id_queue *parent_ids;
2307 struct got_commit_object *selected_commit;
2308 struct got_object_qid *pid;
2310 free(s->id2);
2311 s->id2 = got_object_id_dup(entry->id);
2312 if (s->id2 == NULL)
2313 return got_error_from_errno("got_object_id_dup");
2315 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2316 if (err)
2317 return err;
2318 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2319 free(s->id1);
2320 pid = SIMPLEQ_FIRST(parent_ids);
2321 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2322 got_object_commit_close(selected_commit);
2323 return NULL;
2326 static const struct got_error *
2327 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2328 struct tog_view **focus_view, struct tog_view *view, int ch)
2330 const struct got_error *err = NULL;
2331 struct tog_diff_view_state *s = &view->state.diff;
2332 struct tog_log_view_state *ls;
2333 struct commit_queue_entry *entry;
2334 int i;
2336 switch (ch) {
2337 case 'k':
2338 case KEY_UP:
2339 if (s->first_displayed_line > 1)
2340 s->first_displayed_line--;
2341 break;
2342 case KEY_PPAGE:
2343 case CTRL('b'):
2344 if (s->first_displayed_line == 1)
2345 break;
2346 i = 0;
2347 while (i++ < view->nlines - 1 &&
2348 s->first_displayed_line > 1)
2349 s->first_displayed_line--;
2350 break;
2351 case 'j':
2352 case KEY_DOWN:
2353 if (!s->eof)
2354 s->first_displayed_line++;
2355 break;
2356 case KEY_NPAGE:
2357 case CTRL('f'):
2358 case ' ':
2359 if (s->eof)
2360 break;
2361 i = 0;
2362 while (!s->eof && i++ < view->nlines - 1) {
2363 char *line;
2364 line = parse_next_line(s->f, NULL);
2365 s->first_displayed_line++;
2366 if (line == NULL)
2367 break;
2369 break;
2370 case '[':
2371 if (s->diff_context > 0) {
2372 s->diff_context--;
2373 diff_view_indicate_progress(view);
2374 err = create_diff(s);
2376 break;
2377 case ']':
2378 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2379 s->diff_context++;
2380 diff_view_indicate_progress(view);
2381 err = create_diff(s);
2383 break;
2384 case '<':
2385 case ',':
2386 if (s->log_view == NULL)
2387 break;
2388 ls = &s->log_view->state.log;
2389 entry = TAILQ_PREV(ls->selected_entry,
2390 commit_queue_head, entry);
2391 if (entry == NULL)
2392 break;
2394 err = input_log_view(NULL, NULL, NULL, s->log_view,
2395 KEY_UP);
2396 if (err)
2397 break;
2399 err = set_selected_commit(s, entry);
2400 if (err)
2401 break;
2403 s->first_displayed_line = 1;
2404 s->last_displayed_line = view->nlines;
2406 diff_view_indicate_progress(view);
2407 err = create_diff(s);
2408 break;
2409 case '>':
2410 case '.':
2411 if (s->log_view == NULL)
2412 break;
2413 ls = &s->log_view->state.log;
2415 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2416 ls->thread_args.commits_needed++;
2418 /* Display "loading..." in log view. */
2419 show_log_view(s->log_view);
2420 update_panels();
2421 doupdate();
2423 err = trigger_log_thread(1 /* load_all */,
2424 &ls->thread_args.commits_needed,
2425 &ls->thread_args.log_complete,
2426 &ls->thread_args.need_commits);
2427 if (err)
2428 break;
2430 err = input_log_view(NULL, NULL, NULL, s->log_view,
2431 KEY_DOWN);
2432 if (err)
2433 break;
2435 entry = TAILQ_NEXT(ls->selected_entry, entry);
2436 if (entry == NULL)
2437 break;
2439 err = set_selected_commit(s, entry);
2440 if (err)
2441 break;
2443 s->first_displayed_line = 1;
2444 s->last_displayed_line = view->nlines;
2446 diff_view_indicate_progress(view);
2447 err = create_diff(s);
2448 break;
2449 default:
2450 break;
2453 return err;
2456 static const struct got_error *
2457 cmd_diff(int argc, char *argv[])
2459 const struct got_error *error = NULL;
2460 struct got_repository *repo = NULL;
2461 struct got_reflist_head refs;
2462 struct got_object_id *id1 = NULL, *id2 = NULL;
2463 char *repo_path = NULL;
2464 char *id_str1 = NULL, *id_str2 = NULL;
2465 int ch;
2466 struct tog_view *view;
2468 SIMPLEQ_INIT(&refs);
2470 #ifndef PROFILE
2471 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2472 NULL) == -1)
2473 err(1, "pledge");
2474 #endif
2476 while ((ch = getopt(argc, argv, "")) != -1) {
2477 switch (ch) {
2478 default:
2479 usage_diff();
2480 /* NOTREACHED */
2484 argc -= optind;
2485 argv += optind;
2487 if (argc == 0) {
2488 usage_diff(); /* TODO show local worktree changes */
2489 } else if (argc == 2) {
2490 repo_path = getcwd(NULL, 0);
2491 if (repo_path == NULL)
2492 return got_error_from_errno("getcwd");
2493 id_str1 = argv[0];
2494 id_str2 = argv[1];
2495 } else if (argc == 3) {
2496 repo_path = realpath(argv[0], NULL);
2497 if (repo_path == NULL)
2498 return got_error_from_errno2("realpath", argv[0]);
2499 id_str1 = argv[1];
2500 id_str2 = argv[2];
2501 } else
2502 usage_diff();
2504 init_curses();
2506 error = got_repo_open(&repo, repo_path);
2507 if (error)
2508 goto done;
2510 error = apply_unveil(got_repo_get_path(repo), NULL);
2511 if (error)
2512 goto done;
2514 error = got_object_resolve_id_str(&id1, repo, id_str1);
2515 if (error)
2516 goto done;
2518 error = got_object_resolve_id_str(&id2, repo, id_str2);
2519 if (error)
2520 goto done;
2522 error = got_ref_list(&refs, repo);
2523 if (error)
2524 goto done;
2526 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2527 if (view == NULL) {
2528 error = got_error_from_errno("view_open");
2529 goto done;
2531 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2532 if (error)
2533 goto done;
2534 error = view_loop(view);
2535 done:
2536 free(repo_path);
2537 got_repo_close(repo);
2538 got_ref_list_free(&refs);
2539 return error;
2542 __dead static void
2543 usage_blame(void)
2545 endwin();
2546 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2547 getprogname());
2548 exit(1);
2551 struct tog_blame_line {
2552 int annotated;
2553 struct got_object_id *id;
2556 static const struct got_error *
2557 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2558 const char *path, struct tog_blame_line *lines, int nlines,
2559 int blame_complete, int selected_line, int *first_displayed_line,
2560 int *last_displayed_line, int *eof, int max_lines)
2562 const struct got_error *err;
2563 int lineno = 0, nprinted = 0;
2564 char *line;
2565 size_t len;
2566 wchar_t *wline;
2567 int width, wlimit;
2568 struct tog_blame_line *blame_line;
2569 struct got_object_id *prev_id = NULL;
2570 char *id_str;
2572 err = got_object_id_str(&id_str, id);
2573 if (err)
2574 return err;
2576 rewind(f);
2577 werase(view->window);
2579 if (asprintf(&line, "commit %s", id_str) == -1) {
2580 err = got_error_from_errno("asprintf");
2581 free(id_str);
2582 return err;
2585 err = format_line(&wline, &width, line, view->ncols);
2586 free(line);
2587 line = NULL;
2588 if (view_needs_focus_indication(view))
2589 wstandout(view->window);
2590 waddwstr(view->window, wline);
2591 if (view_needs_focus_indication(view))
2592 wstandend(view->window);
2593 free(wline);
2594 wline = NULL;
2595 if (width < view->ncols)
2596 waddch(view->window, '\n');
2598 if (asprintf(&line, "[%d/%d] %s%s",
2599 *first_displayed_line - 1 + selected_line, nlines,
2600 blame_complete ? "" : "annotating... ", path) == -1) {
2601 free(id_str);
2602 return got_error_from_errno("asprintf");
2604 free(id_str);
2605 err = format_line(&wline, &width, line, view->ncols);
2606 free(line);
2607 line = NULL;
2608 if (err)
2609 return err;
2610 waddwstr(view->window, wline);
2611 free(wline);
2612 wline = NULL;
2613 if (width < view->ncols)
2614 waddch(view->window, '\n');
2616 *eof = 0;
2617 while (nprinted < max_lines - 2) {
2618 line = parse_next_line(f, &len);
2619 if (line == NULL) {
2620 *eof = 1;
2621 break;
2623 if (++lineno < *first_displayed_line) {
2624 free(line);
2625 continue;
2628 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2629 err = format_line(&wline, &width, line, wlimit);
2630 if (err) {
2631 free(line);
2632 return err;
2635 if (view->focussed && nprinted == selected_line - 1)
2636 wstandout(view->window);
2638 blame_line = &lines[lineno - 1];
2639 if (blame_line->annotated && prev_id &&
2640 got_object_id_cmp(prev_id, blame_line->id) == 0)
2641 waddstr(view->window, " ");
2642 else if (blame_line->annotated) {
2643 char *id_str;
2644 err = got_object_id_str(&id_str, blame_line->id);
2645 if (err) {
2646 free(line);
2647 free(wline);
2648 return err;
2650 wprintw(view->window, "%.8s ", id_str);
2651 free(id_str);
2652 prev_id = blame_line->id;
2653 } else {
2654 waddstr(view->window, "........ ");
2655 prev_id = NULL;
2658 waddwstr(view->window, wline);
2659 while (width < wlimit) {
2660 waddch(view->window, ' ');
2661 width++;
2663 if (view->focussed && nprinted == selected_line - 1)
2664 wstandend(view->window);
2665 if (++nprinted == 1)
2666 *first_displayed_line = lineno;
2667 free(line);
2668 free(wline);
2669 wline = NULL;
2671 *last_displayed_line = lineno;
2673 view_vborder(view);
2675 return NULL;
2678 static const struct got_error *
2679 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2681 const struct got_error *err = NULL;
2682 struct tog_blame_cb_args *a = arg;
2683 struct tog_blame_line *line;
2684 int errcode;
2686 if (nlines != a->nlines ||
2687 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2688 return got_error(GOT_ERR_RANGE);
2690 errcode = pthread_mutex_lock(&tog_mutex);
2691 if (errcode)
2692 return got_error_set_errno(errcode, "pthread_mutex_lock");
2694 if (*a->quit) { /* user has quit the blame view */
2695 err = got_error(GOT_ERR_ITER_COMPLETED);
2696 goto done;
2699 if (lineno == -1)
2700 goto done; /* no change in this commit */
2702 line = &a->lines[lineno - 1];
2703 if (line->annotated)
2704 goto done;
2706 line->id = got_object_id_dup(id);
2707 if (line->id == NULL) {
2708 err = got_error_from_errno("got_object_id_dup");
2709 goto done;
2711 line->annotated = 1;
2712 done:
2713 errcode = pthread_mutex_unlock(&tog_mutex);
2714 if (errcode)
2715 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2716 return err;
2719 static void *
2720 blame_thread(void *arg)
2722 const struct got_error *err;
2723 struct tog_blame_thread_args *ta = arg;
2724 struct tog_blame_cb_args *a = ta->cb_args;
2725 int errcode;
2727 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2728 blame_cb, ta->cb_args);
2730 errcode = pthread_mutex_lock(&tog_mutex);
2731 if (errcode)
2732 return (void *)got_error_set_errno(errcode,
2733 "pthread_mutex_lock");
2735 got_repo_close(ta->repo);
2736 ta->repo = NULL;
2737 *ta->complete = 1;
2739 errcode = pthread_mutex_unlock(&tog_mutex);
2740 if (errcode && err == NULL)
2741 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2743 return (void *)err;
2746 static struct got_object_id *
2747 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2748 int selected_line)
2750 struct tog_blame_line *line;
2752 line = &lines[first_displayed_line - 1 + selected_line - 1];
2753 if (!line->annotated)
2754 return NULL;
2756 return line->id;
2759 static const struct got_error *
2760 stop_blame(struct tog_blame *blame)
2762 const struct got_error *err = NULL;
2763 int i;
2765 if (blame->thread) {
2766 int errcode;
2767 errcode = pthread_mutex_unlock(&tog_mutex);
2768 if (errcode)
2769 return got_error_set_errno(errcode,
2770 "pthread_mutex_unlock");
2771 errcode = pthread_join(blame->thread, (void **)&err);
2772 if (errcode)
2773 return got_error_set_errno(errcode, "pthread_join");
2774 errcode = pthread_mutex_lock(&tog_mutex);
2775 if (errcode)
2776 return got_error_set_errno(errcode,
2777 "pthread_mutex_lock");
2778 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2779 err = NULL;
2780 blame->thread = NULL;
2782 if (blame->thread_args.repo) {
2783 got_repo_close(blame->thread_args.repo);
2784 blame->thread_args.repo = NULL;
2786 if (blame->f) {
2787 if (fclose(blame->f) != 0 && err == NULL)
2788 err = got_error_from_errno("fclose");
2789 blame->f = NULL;
2791 if (blame->lines) {
2792 for (i = 0; i < blame->nlines; i++)
2793 free(blame->lines[i].id);
2794 free(blame->lines);
2795 blame->lines = NULL;
2797 free(blame->cb_args.commit_id);
2798 blame->cb_args.commit_id = NULL;
2800 return err;
2803 static const struct got_error *
2804 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2805 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2806 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2807 struct got_repository *repo)
2809 const struct got_error *err = NULL;
2810 struct got_blob_object *blob = NULL;
2811 struct got_repository *thread_repo = NULL;
2812 struct got_object_id *obj_id = NULL;
2813 int obj_type;
2815 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2816 if (err)
2817 return err;
2818 if (obj_id == NULL)
2819 return got_error(GOT_ERR_NO_OBJ);
2821 err = got_object_get_type(&obj_type, repo, obj_id);
2822 if (err)
2823 goto done;
2825 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2826 err = got_error(GOT_ERR_OBJ_TYPE);
2827 goto done;
2830 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2831 if (err)
2832 goto done;
2833 blame->f = got_opentemp();
2834 if (blame->f == NULL) {
2835 err = got_error_from_errno("got_opentemp");
2836 goto done;
2838 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2839 blame->f, blob);
2840 if (err)
2841 goto done;
2843 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2844 if (blame->lines == NULL) {
2845 err = got_error_from_errno("calloc");
2846 goto done;
2849 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2850 if (err)
2851 goto done;
2853 blame->cb_args.view = view;
2854 blame->cb_args.lines = blame->lines;
2855 blame->cb_args.nlines = blame->nlines;
2856 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2857 if (blame->cb_args.commit_id == NULL) {
2858 err = got_error_from_errno("got_object_id_dup");
2859 goto done;
2861 blame->cb_args.quit = done;
2863 blame->thread_args.path = path;
2864 blame->thread_args.repo = thread_repo;
2865 blame->thread_args.cb_args = &blame->cb_args;
2866 blame->thread_args.complete = blame_complete;
2867 *blame_complete = 0;
2869 done:
2870 if (blob)
2871 got_object_blob_close(blob);
2872 free(obj_id);
2873 if (err)
2874 stop_blame(blame);
2875 return err;
2878 static const struct got_error *
2879 open_blame_view(struct tog_view *view, char *path,
2880 struct got_object_id *commit_id, struct got_reflist_head *refs,
2881 struct got_repository *repo)
2883 const struct got_error *err = NULL;
2884 struct tog_blame_view_state *s = &view->state.blame;
2886 SIMPLEQ_INIT(&s->blamed_commits);
2888 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2889 if (err)
2890 return err;
2892 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2893 s->first_displayed_line = 1;
2894 s->last_displayed_line = view->nlines;
2895 s->selected_line = 1;
2896 s->blame_complete = 0;
2897 s->path = path;
2898 if (s->path == NULL)
2899 return got_error_from_errno("open_blame_view");
2900 s->repo = repo;
2901 s->refs = refs;
2902 s->commit_id = commit_id;
2903 memset(&s->blame, 0, sizeof(s->blame));
2905 view->show = show_blame_view;
2906 view->input = input_blame_view;
2907 view->close = close_blame_view;
2909 return run_blame(&s->blame, view, &s->blame_complete,
2910 &s->first_displayed_line, &s->last_displayed_line,
2911 &s->selected_line, &s->done, &s->eof, s->path,
2912 s->blamed_commit->id, s->repo);
2915 static const struct got_error *
2916 close_blame_view(struct tog_view *view)
2918 const struct got_error *err = NULL;
2919 struct tog_blame_view_state *s = &view->state.blame;
2921 if (s->blame.thread)
2922 err = stop_blame(&s->blame);
2924 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2925 struct got_object_qid *blamed_commit;
2926 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2927 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2928 got_object_qid_free(blamed_commit);
2931 free(s->path);
2933 return err;
2936 static const struct got_error *
2937 show_blame_view(struct tog_view *view)
2939 const struct got_error *err = NULL;
2940 struct tog_blame_view_state *s = &view->state.blame;
2941 int errcode;
2943 if (s->blame.thread == NULL) {
2944 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2945 &s->blame.thread_args);
2946 if (errcode)
2947 return got_error_set_errno(errcode, "pthread_create");
2950 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2951 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2952 s->selected_line, &s->first_displayed_line,
2953 &s->last_displayed_line, &s->eof, view->nlines);
2955 view_vborder(view);
2956 return err;
2959 static const struct got_error *
2960 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2961 struct tog_view **focus_view, struct tog_view *view, int ch)
2963 const struct got_error *err = NULL, *thread_err = NULL;
2964 struct tog_view *diff_view;
2965 struct tog_blame_view_state *s = &view->state.blame;
2966 int begin_x = 0;
2968 switch (ch) {
2969 case 'q':
2970 s->done = 1;
2971 break;
2972 case 'k':
2973 case KEY_UP:
2974 if (s->selected_line > 1)
2975 s->selected_line--;
2976 else if (s->selected_line == 1 &&
2977 s->first_displayed_line > 1)
2978 s->first_displayed_line--;
2979 break;
2980 case KEY_PPAGE:
2981 if (s->first_displayed_line == 1) {
2982 s->selected_line = 1;
2983 break;
2985 if (s->first_displayed_line > view->nlines - 2)
2986 s->first_displayed_line -=
2987 (view->nlines - 2);
2988 else
2989 s->first_displayed_line = 1;
2990 break;
2991 case 'j':
2992 case KEY_DOWN:
2993 if (s->selected_line < view->nlines - 2 &&
2994 s->first_displayed_line +
2995 s->selected_line <= s->blame.nlines)
2996 s->selected_line++;
2997 else if (s->last_displayed_line <
2998 s->blame.nlines)
2999 s->first_displayed_line++;
3000 break;
3001 case 'b':
3002 case 'p': {
3003 struct got_object_id *id = NULL;
3004 id = get_selected_commit_id(s->blame.lines,
3005 s->first_displayed_line, s->selected_line);
3006 if (id == NULL)
3007 break;
3008 if (ch == 'p') {
3009 struct got_commit_object *commit;
3010 struct got_object_qid *pid;
3011 struct got_object_id *blob_id = NULL;
3012 int obj_type;
3013 err = got_object_open_as_commit(&commit,
3014 s->repo, id);
3015 if (err)
3016 break;
3017 pid = SIMPLEQ_FIRST(
3018 got_object_commit_get_parent_ids(commit));
3019 if (pid == NULL) {
3020 got_object_commit_close(commit);
3021 break;
3023 /* Check if path history ends here. */
3024 err = got_object_id_by_path(&blob_id, s->repo,
3025 pid->id, s->path);
3026 if (err) {
3027 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3028 err = NULL;
3029 got_object_commit_close(commit);
3030 break;
3032 err = got_object_get_type(&obj_type, s->repo,
3033 blob_id);
3034 free(blob_id);
3035 /* Can't blame non-blob type objects. */
3036 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3037 got_object_commit_close(commit);
3038 break;
3040 err = got_object_qid_alloc(&s->blamed_commit,
3041 pid->id);
3042 got_object_commit_close(commit);
3043 } else {
3044 if (got_object_id_cmp(id,
3045 s->blamed_commit->id) == 0)
3046 break;
3047 err = got_object_qid_alloc(&s->blamed_commit,
3048 id);
3050 if (err)
3051 break;
3052 s->done = 1;
3053 thread_err = stop_blame(&s->blame);
3054 s->done = 0;
3055 if (thread_err)
3056 break;
3057 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3058 s->blamed_commit, entry);
3059 err = run_blame(&s->blame, view, &s->blame_complete,
3060 &s->first_displayed_line, &s->last_displayed_line,
3061 &s->selected_line, &s->done, &s->eof,
3062 s->path, s->blamed_commit->id, s->repo);
3063 if (err)
3064 break;
3065 break;
3067 case 'B': {
3068 struct got_object_qid *first;
3069 first = SIMPLEQ_FIRST(&s->blamed_commits);
3070 if (!got_object_id_cmp(first->id, s->commit_id))
3071 break;
3072 s->done = 1;
3073 thread_err = stop_blame(&s->blame);
3074 s->done = 0;
3075 if (thread_err)
3076 break;
3077 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3078 got_object_qid_free(s->blamed_commit);
3079 s->blamed_commit =
3080 SIMPLEQ_FIRST(&s->blamed_commits);
3081 err = run_blame(&s->blame, view, &s->blame_complete,
3082 &s->first_displayed_line, &s->last_displayed_line,
3083 &s->selected_line, &s->done, &s->eof, s->path,
3084 s->blamed_commit->id, s->repo);
3085 if (err)
3086 break;
3087 break;
3089 case KEY_ENTER:
3090 case '\r': {
3091 struct got_object_id *id = NULL;
3092 struct got_object_qid *pid;
3093 struct got_commit_object *commit = NULL;
3094 id = get_selected_commit_id(s->blame.lines,
3095 s->first_displayed_line, s->selected_line);
3096 if (id == NULL)
3097 break;
3098 err = got_object_open_as_commit(&commit, s->repo, id);
3099 if (err)
3100 break;
3101 pid = SIMPLEQ_FIRST(
3102 got_object_commit_get_parent_ids(commit));
3103 if (view_is_parent_view(view))
3104 begin_x = view_split_begin_x(view->begin_x);
3105 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3106 if (diff_view == NULL) {
3107 got_object_commit_close(commit);
3108 err = got_error_from_errno("view_open");
3109 break;
3111 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3112 id, NULL, s->refs, s->repo);
3113 got_object_commit_close(commit);
3114 if (err) {
3115 view_close(diff_view);
3116 break;
3118 if (view_is_parent_view(view)) {
3119 err = view_close_child(view);
3120 if (err)
3121 break;
3122 err = view_set_child(view, diff_view);
3123 if (err) {
3124 view_close(diff_view);
3125 break;
3127 *focus_view = diff_view;
3128 view->child_focussed = 1;
3129 } else
3130 *new_view = diff_view;
3131 if (err)
3132 break;
3133 break;
3135 case KEY_NPAGE:
3136 case ' ':
3137 if (s->last_displayed_line >= s->blame.nlines &&
3138 s->selected_line >= MIN(s->blame.nlines,
3139 view->nlines - 2)) {
3140 break;
3142 if (s->last_displayed_line >= s->blame.nlines &&
3143 s->selected_line < view->nlines - 2) {
3144 s->selected_line = MIN(s->blame.nlines,
3145 view->nlines - 2);
3146 break;
3148 if (s->last_displayed_line + view->nlines - 2
3149 <= s->blame.nlines)
3150 s->first_displayed_line +=
3151 view->nlines - 2;
3152 else
3153 s->first_displayed_line =
3154 s->blame.nlines -
3155 (view->nlines - 3);
3156 break;
3157 case KEY_RESIZE:
3158 if (s->selected_line > view->nlines - 2) {
3159 s->selected_line = MIN(s->blame.nlines,
3160 view->nlines - 2);
3162 break;
3163 default:
3164 break;
3166 return thread_err ? thread_err : err;
3169 static const struct got_error *
3170 cmd_blame(int argc, char *argv[])
3172 const struct got_error *error;
3173 struct got_repository *repo = NULL;
3174 struct got_reflist_head refs;
3175 struct got_worktree *worktree = NULL;
3176 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3177 struct got_object_id *commit_id = NULL;
3178 char *commit_id_str = NULL;
3179 int ch;
3180 struct tog_view *view;
3182 SIMPLEQ_INIT(&refs);
3184 #ifndef PROFILE
3185 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3186 NULL) == -1)
3187 err(1, "pledge");
3188 #endif
3190 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3191 switch (ch) {
3192 case 'c':
3193 commit_id_str = optarg;
3194 break;
3195 case 'r':
3196 repo_path = realpath(optarg, NULL);
3197 if (repo_path == NULL)
3198 err(1, "-r option");
3199 break;
3200 default:
3201 usage_blame();
3202 /* NOTREACHED */
3206 argc -= optind;
3207 argv += optind;
3209 if (argc == 1)
3210 path = argv[0];
3211 else
3212 usage_blame();
3214 cwd = getcwd(NULL, 0);
3215 if (cwd == NULL) {
3216 error = got_error_from_errno("getcwd");
3217 goto done;
3219 if (repo_path == NULL) {
3220 error = got_worktree_open(&worktree, cwd);
3221 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3222 goto done;
3223 else
3224 error = NULL;
3225 if (worktree) {
3226 repo_path =
3227 strdup(got_worktree_get_repo_path(worktree));
3228 if (repo_path == NULL)
3229 error = got_error_from_errno("strdup");
3230 if (error)
3231 goto done;
3232 } else {
3233 repo_path = strdup(cwd);
3234 if (repo_path == NULL) {
3235 error = got_error_from_errno("strdup");
3236 goto done;
3241 init_curses();
3243 error = got_repo_open(&repo, repo_path);
3244 if (error != NULL)
3245 goto done;
3247 error = apply_unveil(got_repo_get_path(repo), NULL);
3248 if (error)
3249 goto done;
3251 if (worktree) {
3252 const char *prefix = got_worktree_get_path_prefix(worktree);
3253 char *p, *worktree_subdir = cwd +
3254 strlen(got_worktree_get_root_path(worktree));
3255 if (asprintf(&p, "%s%s%s%s%s",
3256 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3257 worktree_subdir, worktree_subdir[0] ? "/" : "",
3258 path) == -1) {
3259 error = got_error_from_errno("asprintf");
3260 goto done;
3262 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3263 free(p);
3264 } else {
3265 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3267 if (error)
3268 goto done;
3270 if (commit_id_str == NULL) {
3271 struct got_reference *head_ref;
3272 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3273 if (error != NULL)
3274 goto done;
3275 error = got_ref_resolve(&commit_id, repo, head_ref);
3276 got_ref_close(head_ref);
3277 } else {
3278 error = got_object_resolve_id_str(&commit_id, repo,
3279 commit_id_str);
3281 if (error != NULL)
3282 goto done;
3284 error = got_ref_list(&refs, repo);
3285 if (error)
3286 goto done;
3288 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3289 if (view == NULL) {
3290 error = got_error_from_errno("view_open");
3291 goto done;
3293 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3294 if (error)
3295 goto done;
3296 error = view_loop(view);
3297 done:
3298 free(repo_path);
3299 free(cwd);
3300 free(commit_id);
3301 if (worktree)
3302 got_worktree_close(worktree);
3303 if (repo)
3304 got_repo_close(repo);
3305 got_ref_list_free(&refs);
3306 return error;
3309 static const struct got_error *
3310 draw_tree_entries(struct tog_view *view,
3311 struct got_tree_entry **first_displayed_entry,
3312 struct got_tree_entry **last_displayed_entry,
3313 struct got_tree_entry **selected_entry, int *ndisplayed,
3314 const char *label, int show_ids, const char *parent_path,
3315 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3317 const struct got_error *err = NULL;
3318 struct got_tree_entry *te;
3319 wchar_t *wline;
3320 int width, n;
3322 *ndisplayed = 0;
3324 werase(view->window);
3326 if (limit == 0)
3327 return NULL;
3329 err = format_line(&wline, &width, label, view->ncols);
3330 if (err)
3331 return err;
3332 if (view_needs_focus_indication(view))
3333 wstandout(view->window);
3334 waddwstr(view->window, wline);
3335 if (view_needs_focus_indication(view))
3336 wstandend(view->window);
3337 free(wline);
3338 wline = NULL;
3339 if (width < view->ncols)
3340 waddch(view->window, '\n');
3341 if (--limit <= 0)
3342 return NULL;
3343 err = format_line(&wline, &width, parent_path, view->ncols);
3344 if (err)
3345 return err;
3346 waddwstr(view->window, wline);
3347 free(wline);
3348 wline = NULL;
3349 if (width < view->ncols)
3350 waddch(view->window, '\n');
3351 if (--limit <= 0)
3352 return NULL;
3353 waddch(view->window, '\n');
3354 if (--limit <= 0)
3355 return NULL;
3357 te = SIMPLEQ_FIRST(&entries->head);
3358 if (*first_displayed_entry == NULL) {
3359 if (selected == 0) {
3360 if (view->focussed)
3361 wstandout(view->window);
3362 *selected_entry = NULL;
3364 waddstr(view->window, " ..\n"); /* parent directory */
3365 if (selected == 0 && view->focussed)
3366 wstandend(view->window);
3367 (*ndisplayed)++;
3368 if (--limit <= 0)
3369 return NULL;
3370 n = 1;
3371 } else {
3372 n = 0;
3373 while (te != *first_displayed_entry)
3374 te = SIMPLEQ_NEXT(te, entry);
3377 while (te) {
3378 char *line = NULL, *id_str = NULL;
3380 if (show_ids) {
3381 err = got_object_id_str(&id_str, te->id);
3382 if (err)
3383 return got_error_from_errno(
3384 "got_object_id_str");
3386 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3387 te->name, S_ISDIR(te->mode) ? "/" :
3388 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3389 free(id_str);
3390 return got_error_from_errno("asprintf");
3392 free(id_str);
3393 err = format_line(&wline, &width, line, view->ncols);
3394 if (err) {
3395 free(line);
3396 break;
3398 if (n == selected) {
3399 if (view->focussed)
3400 wstandout(view->window);
3401 *selected_entry = te;
3403 waddwstr(view->window, wline);
3404 if (width < view->ncols)
3405 waddch(view->window, '\n');
3406 if (n == selected && view->focussed)
3407 wstandend(view->window);
3408 free(line);
3409 free(wline);
3410 wline = NULL;
3411 n++;
3412 (*ndisplayed)++;
3413 *last_displayed_entry = te;
3414 if (--limit <= 0)
3415 break;
3416 te = SIMPLEQ_NEXT(te, entry);
3419 return err;
3422 static void
3423 tree_scroll_up(struct tog_view *view,
3424 struct got_tree_entry **first_displayed_entry, int maxscroll,
3425 const struct got_tree_entries *entries, int isroot)
3427 struct got_tree_entry *te, *prev;
3428 int i;
3430 if (*first_displayed_entry == NULL)
3431 return;
3433 te = SIMPLEQ_FIRST(&entries->head);
3434 if (*first_displayed_entry == te) {
3435 if (!isroot)
3436 *first_displayed_entry = NULL;
3437 return;
3440 /* XXX this is stupid... switch to TAILQ? */
3441 for (i = 0; i < maxscroll; i++) {
3442 while (te != *first_displayed_entry) {
3443 prev = te;
3444 te = SIMPLEQ_NEXT(te, entry);
3446 *first_displayed_entry = prev;
3447 te = SIMPLEQ_FIRST(&entries->head);
3449 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3450 *first_displayed_entry = NULL;
3453 static int
3454 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3455 struct got_tree_entry *last_displayed_entry,
3456 const struct got_tree_entries *entries)
3458 struct got_tree_entry *next, *last;
3459 int n = 0;
3461 if (*first_displayed_entry)
3462 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3463 else
3464 next = SIMPLEQ_FIRST(&entries->head);
3465 last = last_displayed_entry;
3466 while (next && last && n++ < maxscroll) {
3467 last = SIMPLEQ_NEXT(last, entry);
3468 if (last) {
3469 *first_displayed_entry = next;
3470 next = SIMPLEQ_NEXT(next, entry);
3473 return n;
3476 static const struct got_error *
3477 tree_entry_path(char **path, struct tog_parent_trees *parents,
3478 struct got_tree_entry *te)
3480 const struct got_error *err = NULL;
3481 struct tog_parent_tree *pt;
3482 size_t len = 2; /* for leading slash and NUL */
3484 TAILQ_FOREACH(pt, parents, entry)
3485 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3486 if (te)
3487 len += strlen(te->name);
3489 *path = calloc(1, len);
3490 if (path == NULL)
3491 return got_error_from_errno("calloc");
3493 (*path)[0] = '/';
3494 pt = TAILQ_LAST(parents, tog_parent_trees);
3495 while (pt) {
3496 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3497 err = got_error(GOT_ERR_NO_SPACE);
3498 goto done;
3500 if (strlcat(*path, "/", len) >= len) {
3501 err = got_error(GOT_ERR_NO_SPACE);
3502 goto done;
3504 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3506 if (te) {
3507 if (strlcat(*path, te->name, len) >= len) {
3508 err = got_error(GOT_ERR_NO_SPACE);
3509 goto done;
3512 done:
3513 if (err) {
3514 free(*path);
3515 *path = NULL;
3517 return err;
3520 static const struct got_error *
3521 blame_tree_entry(struct tog_view **new_view, int begin_x,
3522 struct got_tree_entry *te, struct tog_parent_trees *parents,
3523 struct got_object_id *commit_id, struct got_reflist_head *refs,
3524 struct got_repository *repo)
3526 const struct got_error *err = NULL;
3527 char *path;
3528 struct tog_view *blame_view;
3530 err = tree_entry_path(&path, parents, te);
3531 if (err)
3532 return err;
3534 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3535 if (blame_view == NULL)
3536 return got_error_from_errno("view_open");
3538 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3539 if (err) {
3540 view_close(blame_view);
3541 free(path);
3542 } else
3543 *new_view = blame_view;
3544 return err;
3547 static const struct got_error *
3548 log_tree_entry(struct tog_view **new_view, int begin_x,
3549 struct got_tree_entry *te, struct tog_parent_trees *parents,
3550 struct got_object_id *commit_id, struct got_reflist_head *refs,
3551 struct got_repository *repo)
3553 struct tog_view *log_view;
3554 const struct got_error *err = NULL;
3555 char *path;
3557 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3558 if (log_view == NULL)
3559 return got_error_from_errno("view_open");
3561 err = tree_entry_path(&path, parents, te);
3562 if (err)
3563 return err;
3565 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3566 if (err)
3567 view_close(log_view);
3568 else
3569 *new_view = log_view;
3570 free(path);
3571 return err;
3574 static const struct got_error *
3575 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3576 struct got_object_id *commit_id, struct got_reflist_head *refs,
3577 struct got_repository *repo)
3579 const struct got_error *err = NULL;
3580 char *commit_id_str = NULL;
3581 struct tog_tree_view_state *s = &view->state.tree;
3583 TAILQ_INIT(&s->parents);
3585 err = got_object_id_str(&commit_id_str, commit_id);
3586 if (err != NULL)
3587 goto done;
3589 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3590 err = got_error_from_errno("asprintf");
3591 goto done;
3594 s->root = s->tree = root;
3595 s->entries = got_object_tree_get_entries(root);
3596 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3597 s->commit_id = got_object_id_dup(commit_id);
3598 if (s->commit_id == NULL) {
3599 err = got_error_from_errno("got_object_id_dup");
3600 goto done;
3602 s->refs = refs;
3603 s->repo = repo;
3605 view->show = show_tree_view;
3606 view->input = input_tree_view;
3607 view->close = close_tree_view;
3608 done:
3609 free(commit_id_str);
3610 if (err) {
3611 free(s->tree_label);
3612 s->tree_label = NULL;
3614 return err;
3617 static const struct got_error *
3618 close_tree_view(struct tog_view *view)
3620 struct tog_tree_view_state *s = &view->state.tree;
3622 free(s->tree_label);
3623 s->tree_label = NULL;
3624 free(s->commit_id);
3625 s->commit_id = NULL;
3626 while (!TAILQ_EMPTY(&s->parents)) {
3627 struct tog_parent_tree *parent;
3628 parent = TAILQ_FIRST(&s->parents);
3629 TAILQ_REMOVE(&s->parents, parent, entry);
3630 free(parent);
3633 if (s->tree != s->root)
3634 got_object_tree_close(s->tree);
3635 got_object_tree_close(s->root);
3637 return NULL;
3640 static const struct got_error *
3641 show_tree_view(struct tog_view *view)
3643 const struct got_error *err = NULL;
3644 struct tog_tree_view_state *s = &view->state.tree;
3645 char *parent_path;
3647 err = tree_entry_path(&parent_path, &s->parents, NULL);
3648 if (err)
3649 return err;
3651 err = draw_tree_entries(view, &s->first_displayed_entry,
3652 &s->last_displayed_entry, &s->selected_entry,
3653 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3654 s->entries, s->selected, view->nlines, s->tree == s->root);
3655 free(parent_path);
3657 view_vborder(view);
3658 return err;
3661 static const struct got_error *
3662 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3663 struct tog_view **focus_view, struct tog_view *view, int ch)
3665 const struct got_error *err = NULL;
3666 struct tog_tree_view_state *s = &view->state.tree;
3667 struct tog_view *log_view;
3668 int begin_x = 0, nscrolled;
3670 switch (ch) {
3671 case 'i':
3672 s->show_ids = !s->show_ids;
3673 break;
3674 case 'l':
3675 if (!s->selected_entry)
3676 break;
3677 if (view_is_parent_view(view))
3678 begin_x = view_split_begin_x(view->begin_x);
3679 err = log_tree_entry(&log_view, begin_x,
3680 s->selected_entry, &s->parents,
3681 s->commit_id, s->refs, s->repo);
3682 if (view_is_parent_view(view)) {
3683 err = view_close_child(view);
3684 if (err)
3685 return err;
3686 err = view_set_child(view, log_view);
3687 if (err) {
3688 view_close(log_view);
3689 break;
3691 *focus_view = log_view;
3692 view->child_focussed = 1;
3693 } else
3694 *new_view = log_view;
3695 break;
3696 case 'k':
3697 case KEY_UP:
3698 if (s->selected > 0) {
3699 s->selected--;
3700 if (s->selected == 0)
3701 break;
3703 if (s->selected > 0)
3704 break;
3705 tree_scroll_up(view, &s->first_displayed_entry, 1,
3706 s->entries, s->tree == s->root);
3707 break;
3708 case KEY_PPAGE:
3709 tree_scroll_up(view, &s->first_displayed_entry,
3710 MAX(0, view->nlines - 4 - s->selected), s->entries,
3711 s->tree == s->root);
3712 s->selected = 0;
3713 if (SIMPLEQ_FIRST(&s->entries->head) ==
3714 s->first_displayed_entry && s->tree != s->root)
3715 s->first_displayed_entry = NULL;
3716 break;
3717 case 'j':
3718 case KEY_DOWN:
3719 if (s->selected < s->ndisplayed - 1) {
3720 s->selected++;
3721 break;
3723 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3724 /* can't scroll any further */
3725 break;
3726 tree_scroll_down(&s->first_displayed_entry, 1,
3727 s->last_displayed_entry, s->entries);
3728 break;
3729 case KEY_NPAGE:
3730 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3731 == NULL) {
3732 /* can't scroll any further; move cursor down */
3733 if (s->selected < s->ndisplayed - 1)
3734 s->selected = s->ndisplayed - 1;
3735 break;
3737 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3738 view->nlines, s->last_displayed_entry, s->entries);
3739 if (nscrolled < view->nlines) {
3740 int ndisplayed = 0;
3741 struct got_tree_entry *te;
3742 te = s->first_displayed_entry;
3743 do {
3744 ndisplayed++;
3745 te = SIMPLEQ_NEXT(te, entry);
3746 } while (te);
3747 s->selected = ndisplayed - 1;
3749 break;
3750 case KEY_ENTER:
3751 case '\r':
3752 case KEY_BACKSPACE:
3753 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3754 struct tog_parent_tree *parent;
3755 /* user selected '..' */
3756 if (s->tree == s->root)
3757 break;
3758 parent = TAILQ_FIRST(&s->parents);
3759 TAILQ_REMOVE(&s->parents, parent,
3760 entry);
3761 got_object_tree_close(s->tree);
3762 s->tree = parent->tree;
3763 s->entries =
3764 got_object_tree_get_entries(s->tree);
3765 s->first_displayed_entry =
3766 parent->first_displayed_entry;
3767 s->selected_entry =
3768 parent->selected_entry;
3769 s->selected = parent->selected;
3770 free(parent);
3771 } else if (S_ISDIR(s->selected_entry->mode)) {
3772 struct tog_parent_tree *parent;
3773 struct got_tree_object *child;
3774 err = got_object_open_as_tree(&child,
3775 s->repo, s->selected_entry->id);
3776 if (err)
3777 break;
3778 parent = calloc(1, sizeof(*parent));
3779 if (parent == NULL) {
3780 err = got_error_from_errno("calloc");
3781 break;
3783 parent->tree = s->tree;
3784 parent->first_displayed_entry =
3785 s->first_displayed_entry;
3786 parent->selected_entry = s->selected_entry;
3787 parent->selected = s->selected;
3788 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3789 s->tree = child;
3790 s->entries =
3791 got_object_tree_get_entries(s->tree);
3792 s->selected = 0;
3793 s->first_displayed_entry = NULL;
3794 } else if (S_ISREG(s->selected_entry->mode)) {
3795 struct tog_view *blame_view;
3796 int begin_x = view_is_parent_view(view) ?
3797 view_split_begin_x(view->begin_x) : 0;
3799 err = blame_tree_entry(&blame_view, begin_x,
3800 s->selected_entry, &s->parents,
3801 s->commit_id, s->refs, s->repo);
3802 if (err)
3803 break;
3804 if (view_is_parent_view(view)) {
3805 err = view_close_child(view);
3806 if (err)
3807 return err;
3808 err = view_set_child(view, blame_view);
3809 if (err) {
3810 view_close(blame_view);
3811 break;
3813 *focus_view = blame_view;
3814 view->child_focussed = 1;
3815 } else
3816 *new_view = blame_view;
3818 break;
3819 case KEY_RESIZE:
3820 if (s->selected > view->nlines)
3821 s->selected = s->ndisplayed - 1;
3822 break;
3823 default:
3824 break;
3827 return err;
3830 __dead static void
3831 usage_tree(void)
3833 endwin();
3834 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3835 getprogname());
3836 exit(1);
3839 static const struct got_error *
3840 cmd_tree(int argc, char *argv[])
3842 const struct got_error *error;
3843 struct got_repository *repo = NULL;
3844 struct got_reflist_head refs;
3845 char *repo_path = NULL;
3846 struct got_object_id *commit_id = NULL;
3847 char *commit_id_arg = NULL;
3848 struct got_commit_object *commit = NULL;
3849 struct got_tree_object *tree = NULL;
3850 int ch;
3851 struct tog_view *view;
3853 SIMPLEQ_INIT(&refs);
3855 #ifndef PROFILE
3856 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3857 NULL) == -1)
3858 err(1, "pledge");
3859 #endif
3861 while ((ch = getopt(argc, argv, "c:")) != -1) {
3862 switch (ch) {
3863 case 'c':
3864 commit_id_arg = optarg;
3865 break;
3866 default:
3867 usage_tree();
3868 /* NOTREACHED */
3872 argc -= optind;
3873 argv += optind;
3875 if (argc == 0) {
3876 struct got_worktree *worktree;
3877 char *cwd = getcwd(NULL, 0);
3878 if (cwd == NULL)
3879 return got_error_from_errno("getcwd");
3880 error = got_worktree_open(&worktree, cwd);
3881 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3882 goto done;
3883 if (worktree) {
3884 free(cwd);
3885 repo_path =
3886 strdup(got_worktree_get_repo_path(worktree));
3887 got_worktree_close(worktree);
3888 } else
3889 repo_path = cwd;
3890 if (repo_path == NULL) {
3891 error = got_error_from_errno("strdup");
3892 goto done;
3894 } else if (argc == 1) {
3895 repo_path = realpath(argv[0], NULL);
3896 if (repo_path == NULL)
3897 return got_error_from_errno2("realpath", argv[0]);
3898 } else
3899 usage_log();
3901 init_curses();
3903 error = got_repo_open(&repo, repo_path);
3904 if (error != NULL)
3905 goto done;
3907 error = apply_unveil(got_repo_get_path(repo), NULL);
3908 if (error)
3909 goto done;
3911 if (commit_id_arg == NULL)
3912 error = get_head_commit_id(&commit_id, repo);
3913 else
3914 error = got_object_resolve_id_str(&commit_id, repo,
3915 commit_id_arg);
3916 if (error != NULL)
3917 goto done;
3919 error = got_object_open_as_commit(&commit, repo, commit_id);
3920 if (error != NULL)
3921 goto done;
3923 error = got_object_open_as_tree(&tree, repo,
3924 got_object_commit_get_tree_id(commit));
3925 if (error != NULL)
3926 goto done;
3928 error = got_ref_list(&refs, repo);
3929 if (error)
3930 goto done;
3932 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3933 if (view == NULL) {
3934 error = got_error_from_errno("view_open");
3935 goto done;
3937 error = open_tree_view(view, tree, commit_id, &refs, repo);
3938 if (error)
3939 goto done;
3940 error = view_loop(view);
3941 done:
3942 free(repo_path);
3943 free(commit_id);
3944 if (commit)
3945 got_object_commit_close(commit);
3946 if (tree)
3947 got_object_tree_close(tree);
3948 if (repo)
3949 got_repo_close(repo);
3950 got_ref_list_free(&refs);
3951 return error;
3954 __dead static void
3955 usage(void)
3957 int i;
3959 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3960 "Available commands:\n", getprogname());
3961 for (i = 0; i < nitems(tog_commands); i++) {
3962 struct tog_cmd *cmd = &tog_commands[i];
3963 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3965 exit(1);
3968 static char **
3969 make_argv(const char *arg0, const char *arg1)
3971 char **argv;
3972 int argc = (arg1 == NULL ? 1 : 2);
3974 argv = calloc(argc, sizeof(char *));
3975 if (argv == NULL)
3976 err(1, "calloc");
3977 argv[0] = strdup(arg0);
3978 if (argv[0] == NULL)
3979 err(1, "calloc");
3980 if (arg1) {
3981 argv[1] = strdup(arg1);
3982 if (argv[1] == NULL)
3983 err(1, "calloc");
3986 return argv;
3989 int
3990 main(int argc, char *argv[])
3992 const struct got_error *error = NULL;
3993 struct tog_cmd *cmd = NULL;
3994 int ch, hflag = 0;
3995 char **cmd_argv = NULL;
3997 setlocale(LC_CTYPE, "");
3999 while ((ch = getopt(argc, argv, "h")) != -1) {
4000 switch (ch) {
4001 case 'h':
4002 hflag = 1;
4003 break;
4004 default:
4005 usage();
4006 /* NOTREACHED */
4010 argc -= optind;
4011 argv += optind;
4012 optind = 0;
4013 optreset = 1;
4015 if (argc == 0) {
4016 if (hflag)
4017 usage();
4018 /* Build an argument vector which runs a default command. */
4019 cmd = &tog_commands[0];
4020 cmd_argv = make_argv(cmd->name, NULL);
4021 argc = 1;
4022 } else {
4023 int i;
4025 /* Did the user specific a command? */
4026 for (i = 0; i < nitems(tog_commands); i++) {
4027 if (strncmp(tog_commands[i].name, argv[0],
4028 strlen(argv[0])) == 0) {
4029 cmd = &tog_commands[i];
4030 if (hflag)
4031 tog_commands[i].cmd_usage();
4032 break;
4035 if (cmd == NULL) {
4036 /* Did the user specify a repository? */
4037 char *repo_path = realpath(argv[0], NULL);
4038 if (repo_path) {
4039 struct got_repository *repo;
4040 error = got_repo_open(&repo, repo_path);
4041 if (error == NULL)
4042 got_repo_close(repo);
4043 } else
4044 error = got_error_from_errno2("realpath",
4045 argv[0]);
4046 if (error) {
4047 if (hflag) {
4048 fprintf(stderr, "%s: '%s' is not a "
4049 "known command\n", getprogname(),
4050 argv[0]);
4051 usage();
4053 fprintf(stderr, "%s: '%s' is neither a known "
4054 "command nor a path to a repository\n",
4055 getprogname(), argv[0]);
4056 free(repo_path);
4057 return 1;
4059 cmd = &tog_commands[0];
4060 cmd_argv = make_argv(cmd->name, repo_path);
4061 argc = 2;
4062 free(repo_path);
4066 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4067 if (error)
4068 goto done;
4069 done:
4070 endwin();
4071 free(cmd_argv);
4072 if (error)
4073 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4074 return 0;