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 if (s->first_displayed_line == 1)
2344 break;
2345 i = 0;
2346 while (i++ < view->nlines - 1 &&
2347 s->first_displayed_line > 1)
2348 s->first_displayed_line--;
2349 break;
2350 case 'j':
2351 case KEY_DOWN:
2352 if (!s->eof)
2353 s->first_displayed_line++;
2354 break;
2355 case KEY_NPAGE:
2356 case ' ':
2357 if (s->eof)
2358 break;
2359 i = 0;
2360 while (!s->eof && i++ < view->nlines - 1) {
2361 char *line;
2362 line = parse_next_line(s->f, NULL);
2363 s->first_displayed_line++;
2364 if (line == NULL)
2365 break;
2367 break;
2368 case '[':
2369 if (s->diff_context > 0) {
2370 s->diff_context--;
2371 diff_view_indicate_progress(view);
2372 err = create_diff(s);
2374 break;
2375 case ']':
2376 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2377 s->diff_context++;
2378 diff_view_indicate_progress(view);
2379 err = create_diff(s);
2381 break;
2382 case '<':
2383 case ',':
2384 if (s->log_view == NULL)
2385 break;
2386 ls = &s->log_view->state.log;
2387 entry = TAILQ_PREV(ls->selected_entry,
2388 commit_queue_head, entry);
2389 if (entry == NULL)
2390 break;
2392 err = input_log_view(NULL, NULL, NULL, s->log_view,
2393 KEY_UP);
2394 if (err)
2395 break;
2397 err = set_selected_commit(s, entry);
2398 if (err)
2399 break;
2401 s->first_displayed_line = 1;
2402 s->last_displayed_line = view->nlines;
2404 diff_view_indicate_progress(view);
2405 err = create_diff(s);
2406 break;
2407 case '>':
2408 case '.':
2409 if (s->log_view == NULL)
2410 break;
2411 ls = &s->log_view->state.log;
2413 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2414 ls->thread_args.commits_needed++;
2416 /* Display "loading..." in log view. */
2417 show_log_view(s->log_view);
2418 update_panels();
2419 doupdate();
2421 err = trigger_log_thread(1 /* load_all */,
2422 &ls->thread_args.commits_needed,
2423 &ls->thread_args.log_complete,
2424 &ls->thread_args.need_commits);
2425 if (err)
2426 break;
2428 err = input_log_view(NULL, NULL, NULL, s->log_view,
2429 KEY_DOWN);
2430 if (err)
2431 break;
2433 entry = TAILQ_NEXT(ls->selected_entry, entry);
2434 if (entry == NULL)
2435 break;
2437 err = set_selected_commit(s, entry);
2438 if (err)
2439 break;
2441 s->first_displayed_line = 1;
2442 s->last_displayed_line = view->nlines;
2444 diff_view_indicate_progress(view);
2445 err = create_diff(s);
2446 break;
2447 default:
2448 break;
2451 return err;
2454 static const struct got_error *
2455 cmd_diff(int argc, char *argv[])
2457 const struct got_error *error = NULL;
2458 struct got_repository *repo = NULL;
2459 struct got_reflist_head refs;
2460 struct got_object_id *id1 = NULL, *id2 = NULL;
2461 char *repo_path = NULL;
2462 char *id_str1 = NULL, *id_str2 = NULL;
2463 int ch;
2464 struct tog_view *view;
2466 SIMPLEQ_INIT(&refs);
2468 #ifndef PROFILE
2469 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2470 NULL) == -1)
2471 err(1, "pledge");
2472 #endif
2474 while ((ch = getopt(argc, argv, "")) != -1) {
2475 switch (ch) {
2476 default:
2477 usage_diff();
2478 /* NOTREACHED */
2482 argc -= optind;
2483 argv += optind;
2485 if (argc == 0) {
2486 usage_diff(); /* TODO show local worktree changes */
2487 } else if (argc == 2) {
2488 repo_path = getcwd(NULL, 0);
2489 if (repo_path == NULL)
2490 return got_error_from_errno("getcwd");
2491 id_str1 = argv[0];
2492 id_str2 = argv[1];
2493 } else if (argc == 3) {
2494 repo_path = realpath(argv[0], NULL);
2495 if (repo_path == NULL)
2496 return got_error_from_errno2("realpath", argv[0]);
2497 id_str1 = argv[1];
2498 id_str2 = argv[2];
2499 } else
2500 usage_diff();
2502 init_curses();
2504 error = got_repo_open(&repo, repo_path);
2505 if (error)
2506 goto done;
2508 error = apply_unveil(got_repo_get_path(repo), NULL);
2509 if (error)
2510 goto done;
2512 error = got_object_resolve_id_str(&id1, repo, id_str1);
2513 if (error)
2514 goto done;
2516 error = got_object_resolve_id_str(&id2, repo, id_str2);
2517 if (error)
2518 goto done;
2520 error = got_ref_list(&refs, repo);
2521 if (error)
2522 goto done;
2524 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2525 if (view == NULL) {
2526 error = got_error_from_errno("view_open");
2527 goto done;
2529 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2530 if (error)
2531 goto done;
2532 error = view_loop(view);
2533 done:
2534 free(repo_path);
2535 got_repo_close(repo);
2536 got_ref_list_free(&refs);
2537 return error;
2540 __dead static void
2541 usage_blame(void)
2543 endwin();
2544 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2545 getprogname());
2546 exit(1);
2549 struct tog_blame_line {
2550 int annotated;
2551 struct got_object_id *id;
2554 static const struct got_error *
2555 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2556 const char *path, struct tog_blame_line *lines, int nlines,
2557 int blame_complete, int selected_line, int *first_displayed_line,
2558 int *last_displayed_line, int *eof, int max_lines)
2560 const struct got_error *err;
2561 int lineno = 0, nprinted = 0;
2562 char *line;
2563 size_t len;
2564 wchar_t *wline;
2565 int width, wlimit;
2566 struct tog_blame_line *blame_line;
2567 struct got_object_id *prev_id = NULL;
2568 char *id_str;
2570 err = got_object_id_str(&id_str, id);
2571 if (err)
2572 return err;
2574 rewind(f);
2575 werase(view->window);
2577 if (asprintf(&line, "commit %s", id_str) == -1) {
2578 err = got_error_from_errno("asprintf");
2579 free(id_str);
2580 return err;
2583 err = format_line(&wline, &width, line, view->ncols);
2584 free(line);
2585 line = NULL;
2586 if (view_needs_focus_indication(view))
2587 wstandout(view->window);
2588 waddwstr(view->window, wline);
2589 if (view_needs_focus_indication(view))
2590 wstandend(view->window);
2591 free(wline);
2592 wline = NULL;
2593 if (width < view->ncols)
2594 waddch(view->window, '\n');
2596 if (asprintf(&line, "[%d/%d] %s%s",
2597 *first_displayed_line - 1 + selected_line, nlines,
2598 blame_complete ? "" : "annotating... ", path) == -1) {
2599 free(id_str);
2600 return got_error_from_errno("asprintf");
2602 free(id_str);
2603 err = format_line(&wline, &width, line, view->ncols);
2604 free(line);
2605 line = NULL;
2606 if (err)
2607 return err;
2608 waddwstr(view->window, wline);
2609 free(wline);
2610 wline = NULL;
2611 if (width < view->ncols)
2612 waddch(view->window, '\n');
2614 *eof = 0;
2615 while (nprinted < max_lines - 2) {
2616 line = parse_next_line(f, &len);
2617 if (line == NULL) {
2618 *eof = 1;
2619 break;
2621 if (++lineno < *first_displayed_line) {
2622 free(line);
2623 continue;
2626 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2627 err = format_line(&wline, &width, line, wlimit);
2628 if (err) {
2629 free(line);
2630 return err;
2633 if (view->focussed && nprinted == selected_line - 1)
2634 wstandout(view->window);
2636 blame_line = &lines[lineno - 1];
2637 if (blame_line->annotated && prev_id &&
2638 got_object_id_cmp(prev_id, blame_line->id) == 0)
2639 waddstr(view->window, " ");
2640 else if (blame_line->annotated) {
2641 char *id_str;
2642 err = got_object_id_str(&id_str, blame_line->id);
2643 if (err) {
2644 free(line);
2645 free(wline);
2646 return err;
2648 wprintw(view->window, "%.8s ", id_str);
2649 free(id_str);
2650 prev_id = blame_line->id;
2651 } else {
2652 waddstr(view->window, "........ ");
2653 prev_id = NULL;
2656 waddwstr(view->window, wline);
2657 while (width < wlimit) {
2658 waddch(view->window, ' ');
2659 width++;
2661 if (view->focussed && nprinted == selected_line - 1)
2662 wstandend(view->window);
2663 if (++nprinted == 1)
2664 *first_displayed_line = lineno;
2665 free(line);
2666 free(wline);
2667 wline = NULL;
2669 *last_displayed_line = lineno;
2671 view_vborder(view);
2673 return NULL;
2676 static const struct got_error *
2677 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2679 const struct got_error *err = NULL;
2680 struct tog_blame_cb_args *a = arg;
2681 struct tog_blame_line *line;
2682 int errcode;
2684 if (nlines != a->nlines ||
2685 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2686 return got_error(GOT_ERR_RANGE);
2688 errcode = pthread_mutex_lock(&tog_mutex);
2689 if (errcode)
2690 return got_error_set_errno(errcode, "pthread_mutex_lock");
2692 if (*a->quit) { /* user has quit the blame view */
2693 err = got_error(GOT_ERR_ITER_COMPLETED);
2694 goto done;
2697 if (lineno == -1)
2698 goto done; /* no change in this commit */
2700 line = &a->lines[lineno - 1];
2701 if (line->annotated)
2702 goto done;
2704 line->id = got_object_id_dup(id);
2705 if (line->id == NULL) {
2706 err = got_error_from_errno("got_object_id_dup");
2707 goto done;
2709 line->annotated = 1;
2710 done:
2711 errcode = pthread_mutex_unlock(&tog_mutex);
2712 if (errcode)
2713 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2714 return err;
2717 static void *
2718 blame_thread(void *arg)
2720 const struct got_error *err;
2721 struct tog_blame_thread_args *ta = arg;
2722 struct tog_blame_cb_args *a = ta->cb_args;
2723 int errcode;
2725 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2726 blame_cb, ta->cb_args);
2728 errcode = pthread_mutex_lock(&tog_mutex);
2729 if (errcode)
2730 return (void *)got_error_set_errno(errcode,
2731 "pthread_mutex_lock");
2733 got_repo_close(ta->repo);
2734 ta->repo = NULL;
2735 *ta->complete = 1;
2737 errcode = pthread_mutex_unlock(&tog_mutex);
2738 if (errcode && err == NULL)
2739 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2741 return (void *)err;
2744 static struct got_object_id *
2745 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2746 int selected_line)
2748 struct tog_blame_line *line;
2750 line = &lines[first_displayed_line - 1 + selected_line - 1];
2751 if (!line->annotated)
2752 return NULL;
2754 return line->id;
2757 static const struct got_error *
2758 stop_blame(struct tog_blame *blame)
2760 const struct got_error *err = NULL;
2761 int i;
2763 if (blame->thread) {
2764 int errcode;
2765 errcode = pthread_mutex_unlock(&tog_mutex);
2766 if (errcode)
2767 return got_error_set_errno(errcode,
2768 "pthread_mutex_unlock");
2769 errcode = pthread_join(blame->thread, (void **)&err);
2770 if (errcode)
2771 return got_error_set_errno(errcode, "pthread_join");
2772 errcode = pthread_mutex_lock(&tog_mutex);
2773 if (errcode)
2774 return got_error_set_errno(errcode,
2775 "pthread_mutex_lock");
2776 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2777 err = NULL;
2778 blame->thread = NULL;
2780 if (blame->thread_args.repo) {
2781 got_repo_close(blame->thread_args.repo);
2782 blame->thread_args.repo = NULL;
2784 if (blame->f) {
2785 if (fclose(blame->f) != 0 && err == NULL)
2786 err = got_error_from_errno("fclose");
2787 blame->f = NULL;
2789 if (blame->lines) {
2790 for (i = 0; i < blame->nlines; i++)
2791 free(blame->lines[i].id);
2792 free(blame->lines);
2793 blame->lines = NULL;
2795 free(blame->cb_args.commit_id);
2796 blame->cb_args.commit_id = NULL;
2798 return err;
2801 static const struct got_error *
2802 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2803 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2804 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2805 struct got_repository *repo)
2807 const struct got_error *err = NULL;
2808 struct got_blob_object *blob = NULL;
2809 struct got_repository *thread_repo = NULL;
2810 struct got_object_id *obj_id = NULL;
2811 int obj_type;
2813 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2814 if (err)
2815 return err;
2816 if (obj_id == NULL)
2817 return got_error(GOT_ERR_NO_OBJ);
2819 err = got_object_get_type(&obj_type, repo, obj_id);
2820 if (err)
2821 goto done;
2823 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2824 err = got_error(GOT_ERR_OBJ_TYPE);
2825 goto done;
2828 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2829 if (err)
2830 goto done;
2831 blame->f = got_opentemp();
2832 if (blame->f == NULL) {
2833 err = got_error_from_errno("got_opentemp");
2834 goto done;
2836 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2837 blame->f, blob);
2838 if (err)
2839 goto done;
2841 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2842 if (blame->lines == NULL) {
2843 err = got_error_from_errno("calloc");
2844 goto done;
2847 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2848 if (err)
2849 goto done;
2851 blame->cb_args.view = view;
2852 blame->cb_args.lines = blame->lines;
2853 blame->cb_args.nlines = blame->nlines;
2854 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2855 if (blame->cb_args.commit_id == NULL) {
2856 err = got_error_from_errno("got_object_id_dup");
2857 goto done;
2859 blame->cb_args.quit = done;
2861 blame->thread_args.path = path;
2862 blame->thread_args.repo = thread_repo;
2863 blame->thread_args.cb_args = &blame->cb_args;
2864 blame->thread_args.complete = blame_complete;
2865 *blame_complete = 0;
2867 done:
2868 if (blob)
2869 got_object_blob_close(blob);
2870 free(obj_id);
2871 if (err)
2872 stop_blame(blame);
2873 return err;
2876 static const struct got_error *
2877 open_blame_view(struct tog_view *view, char *path,
2878 struct got_object_id *commit_id, struct got_reflist_head *refs,
2879 struct got_repository *repo)
2881 const struct got_error *err = NULL;
2882 struct tog_blame_view_state *s = &view->state.blame;
2884 SIMPLEQ_INIT(&s->blamed_commits);
2886 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2887 if (err)
2888 return err;
2890 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2891 s->first_displayed_line = 1;
2892 s->last_displayed_line = view->nlines;
2893 s->selected_line = 1;
2894 s->blame_complete = 0;
2895 s->path = path;
2896 if (s->path == NULL)
2897 return got_error_from_errno("open_blame_view");
2898 s->repo = repo;
2899 s->refs = refs;
2900 s->commit_id = commit_id;
2901 memset(&s->blame, 0, sizeof(s->blame));
2903 view->show = show_blame_view;
2904 view->input = input_blame_view;
2905 view->close = close_blame_view;
2907 return run_blame(&s->blame, view, &s->blame_complete,
2908 &s->first_displayed_line, &s->last_displayed_line,
2909 &s->selected_line, &s->done, &s->eof, s->path,
2910 s->blamed_commit->id, s->repo);
2913 static const struct got_error *
2914 close_blame_view(struct tog_view *view)
2916 const struct got_error *err = NULL;
2917 struct tog_blame_view_state *s = &view->state.blame;
2919 if (s->blame.thread)
2920 err = stop_blame(&s->blame);
2922 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2923 struct got_object_qid *blamed_commit;
2924 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2925 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2926 got_object_qid_free(blamed_commit);
2929 free(s->path);
2931 return err;
2934 static const struct got_error *
2935 show_blame_view(struct tog_view *view)
2937 const struct got_error *err = NULL;
2938 struct tog_blame_view_state *s = &view->state.blame;
2939 int errcode;
2941 if (s->blame.thread == NULL) {
2942 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2943 &s->blame.thread_args);
2944 if (errcode)
2945 return got_error_set_errno(errcode, "pthread_create");
2948 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2949 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2950 s->selected_line, &s->first_displayed_line,
2951 &s->last_displayed_line, &s->eof, view->nlines);
2953 view_vborder(view);
2954 return err;
2957 static const struct got_error *
2958 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2959 struct tog_view **focus_view, struct tog_view *view, int ch)
2961 const struct got_error *err = NULL, *thread_err = NULL;
2962 struct tog_view *diff_view;
2963 struct tog_blame_view_state *s = &view->state.blame;
2964 int begin_x = 0;
2966 switch (ch) {
2967 case 'q':
2968 s->done = 1;
2969 break;
2970 case 'k':
2971 case KEY_UP:
2972 if (s->selected_line > 1)
2973 s->selected_line--;
2974 else if (s->selected_line == 1 &&
2975 s->first_displayed_line > 1)
2976 s->first_displayed_line--;
2977 break;
2978 case KEY_PPAGE:
2979 if (s->first_displayed_line == 1) {
2980 s->selected_line = 1;
2981 break;
2983 if (s->first_displayed_line > view->nlines - 2)
2984 s->first_displayed_line -=
2985 (view->nlines - 2);
2986 else
2987 s->first_displayed_line = 1;
2988 break;
2989 case 'j':
2990 case KEY_DOWN:
2991 if (s->selected_line < view->nlines - 2 &&
2992 s->first_displayed_line +
2993 s->selected_line <= s->blame.nlines)
2994 s->selected_line++;
2995 else if (s->last_displayed_line <
2996 s->blame.nlines)
2997 s->first_displayed_line++;
2998 break;
2999 case 'b':
3000 case 'p': {
3001 struct got_object_id *id = NULL;
3002 id = get_selected_commit_id(s->blame.lines,
3003 s->first_displayed_line, s->selected_line);
3004 if (id == NULL)
3005 break;
3006 if (ch == 'p') {
3007 struct got_commit_object *commit;
3008 struct got_object_qid *pid;
3009 struct got_object_id *blob_id = NULL;
3010 int obj_type;
3011 err = got_object_open_as_commit(&commit,
3012 s->repo, id);
3013 if (err)
3014 break;
3015 pid = SIMPLEQ_FIRST(
3016 got_object_commit_get_parent_ids(commit));
3017 if (pid == NULL) {
3018 got_object_commit_close(commit);
3019 break;
3021 /* Check if path history ends here. */
3022 err = got_object_id_by_path(&blob_id, s->repo,
3023 pid->id, s->path);
3024 if (err) {
3025 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3026 err = NULL;
3027 got_object_commit_close(commit);
3028 break;
3030 err = got_object_get_type(&obj_type, s->repo,
3031 blob_id);
3032 free(blob_id);
3033 /* Can't blame non-blob type objects. */
3034 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3035 got_object_commit_close(commit);
3036 break;
3038 err = got_object_qid_alloc(&s->blamed_commit,
3039 pid->id);
3040 got_object_commit_close(commit);
3041 } else {
3042 if (got_object_id_cmp(id,
3043 s->blamed_commit->id) == 0)
3044 break;
3045 err = got_object_qid_alloc(&s->blamed_commit,
3046 id);
3048 if (err)
3049 break;
3050 s->done = 1;
3051 thread_err = stop_blame(&s->blame);
3052 s->done = 0;
3053 if (thread_err)
3054 break;
3055 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3056 s->blamed_commit, entry);
3057 err = run_blame(&s->blame, view, &s->blame_complete,
3058 &s->first_displayed_line, &s->last_displayed_line,
3059 &s->selected_line, &s->done, &s->eof,
3060 s->path, s->blamed_commit->id, s->repo);
3061 if (err)
3062 break;
3063 break;
3065 case 'B': {
3066 struct got_object_qid *first;
3067 first = SIMPLEQ_FIRST(&s->blamed_commits);
3068 if (!got_object_id_cmp(first->id, s->commit_id))
3069 break;
3070 s->done = 1;
3071 thread_err = stop_blame(&s->blame);
3072 s->done = 0;
3073 if (thread_err)
3074 break;
3075 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3076 got_object_qid_free(s->blamed_commit);
3077 s->blamed_commit =
3078 SIMPLEQ_FIRST(&s->blamed_commits);
3079 err = run_blame(&s->blame, view, &s->blame_complete,
3080 &s->first_displayed_line, &s->last_displayed_line,
3081 &s->selected_line, &s->done, &s->eof, s->path,
3082 s->blamed_commit->id, s->repo);
3083 if (err)
3084 break;
3085 break;
3087 case KEY_ENTER:
3088 case '\r': {
3089 struct got_object_id *id = NULL;
3090 struct got_object_qid *pid;
3091 struct got_commit_object *commit = NULL;
3092 id = get_selected_commit_id(s->blame.lines,
3093 s->first_displayed_line, s->selected_line);
3094 if (id == NULL)
3095 break;
3096 err = got_object_open_as_commit(&commit, s->repo, id);
3097 if (err)
3098 break;
3099 pid = SIMPLEQ_FIRST(
3100 got_object_commit_get_parent_ids(commit));
3101 if (view_is_parent_view(view))
3102 begin_x = view_split_begin_x(view->begin_x);
3103 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3104 if (diff_view == NULL) {
3105 got_object_commit_close(commit);
3106 err = got_error_from_errno("view_open");
3107 break;
3109 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3110 id, NULL, s->refs, s->repo);
3111 got_object_commit_close(commit);
3112 if (err) {
3113 view_close(diff_view);
3114 break;
3116 if (view_is_parent_view(view)) {
3117 err = view_close_child(view);
3118 if (err)
3119 break;
3120 err = view_set_child(view, diff_view);
3121 if (err) {
3122 view_close(diff_view);
3123 break;
3125 *focus_view = diff_view;
3126 view->child_focussed = 1;
3127 } else
3128 *new_view = diff_view;
3129 if (err)
3130 break;
3131 break;
3133 case KEY_NPAGE:
3134 case ' ':
3135 if (s->last_displayed_line >= s->blame.nlines &&
3136 s->selected_line >= MIN(s->blame.nlines,
3137 view->nlines - 2)) {
3138 break;
3140 if (s->last_displayed_line >= s->blame.nlines &&
3141 s->selected_line < view->nlines - 2) {
3142 s->selected_line = MIN(s->blame.nlines,
3143 view->nlines - 2);
3144 break;
3146 if (s->last_displayed_line + view->nlines - 2
3147 <= s->blame.nlines)
3148 s->first_displayed_line +=
3149 view->nlines - 2;
3150 else
3151 s->first_displayed_line =
3152 s->blame.nlines -
3153 (view->nlines - 3);
3154 break;
3155 case KEY_RESIZE:
3156 if (s->selected_line > view->nlines - 2) {
3157 s->selected_line = MIN(s->blame.nlines,
3158 view->nlines - 2);
3160 break;
3161 default:
3162 break;
3164 return thread_err ? thread_err : err;
3167 static const struct got_error *
3168 cmd_blame(int argc, char *argv[])
3170 const struct got_error *error;
3171 struct got_repository *repo = NULL;
3172 struct got_reflist_head refs;
3173 struct got_worktree *worktree = NULL;
3174 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3175 struct got_object_id *commit_id = NULL;
3176 char *commit_id_str = NULL;
3177 int ch;
3178 struct tog_view *view;
3180 SIMPLEQ_INIT(&refs);
3182 #ifndef PROFILE
3183 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3184 NULL) == -1)
3185 err(1, "pledge");
3186 #endif
3188 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3189 switch (ch) {
3190 case 'c':
3191 commit_id_str = optarg;
3192 break;
3193 case 'r':
3194 repo_path = realpath(optarg, NULL);
3195 if (repo_path == NULL)
3196 err(1, "-r option");
3197 break;
3198 default:
3199 usage_blame();
3200 /* NOTREACHED */
3204 argc -= optind;
3205 argv += optind;
3207 if (argc == 1)
3208 path = argv[0];
3209 else
3210 usage_blame();
3212 cwd = getcwd(NULL, 0);
3213 if (cwd == NULL) {
3214 error = got_error_from_errno("getcwd");
3215 goto done;
3217 if (repo_path == NULL) {
3218 error = got_worktree_open(&worktree, cwd);
3219 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3220 goto done;
3221 else
3222 error = NULL;
3223 if (worktree) {
3224 repo_path =
3225 strdup(got_worktree_get_repo_path(worktree));
3226 if (repo_path == NULL)
3227 error = got_error_from_errno("strdup");
3228 if (error)
3229 goto done;
3230 } else {
3231 repo_path = strdup(cwd);
3232 if (repo_path == NULL) {
3233 error = got_error_from_errno("strdup");
3234 goto done;
3239 init_curses();
3241 error = got_repo_open(&repo, repo_path);
3242 if (error != NULL)
3243 goto done;
3245 error = apply_unveil(got_repo_get_path(repo), NULL);
3246 if (error)
3247 goto done;
3249 if (worktree) {
3250 const char *prefix = got_worktree_get_path_prefix(worktree);
3251 char *p, *worktree_subdir = cwd +
3252 strlen(got_worktree_get_root_path(worktree));
3253 if (asprintf(&p, "%s%s%s%s%s",
3254 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3255 worktree_subdir, worktree_subdir[0] ? "/" : "",
3256 path) == -1) {
3257 error = got_error_from_errno("asprintf");
3258 goto done;
3260 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3261 free(p);
3262 } else {
3263 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3265 if (error)
3266 goto done;
3268 if (commit_id_str == NULL) {
3269 struct got_reference *head_ref;
3270 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3271 if (error != NULL)
3272 goto done;
3273 error = got_ref_resolve(&commit_id, repo, head_ref);
3274 got_ref_close(head_ref);
3275 } else {
3276 error = got_object_resolve_id_str(&commit_id, repo,
3277 commit_id_str);
3279 if (error != NULL)
3280 goto done;
3282 error = got_ref_list(&refs, repo);
3283 if (error)
3284 goto done;
3286 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3287 if (view == NULL) {
3288 error = got_error_from_errno("view_open");
3289 goto done;
3291 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3292 if (error)
3293 goto done;
3294 error = view_loop(view);
3295 done:
3296 free(repo_path);
3297 free(cwd);
3298 free(commit_id);
3299 if (worktree)
3300 got_worktree_close(worktree);
3301 if (repo)
3302 got_repo_close(repo);
3303 got_ref_list_free(&refs);
3304 return error;
3307 static const struct got_error *
3308 draw_tree_entries(struct tog_view *view,
3309 struct got_tree_entry **first_displayed_entry,
3310 struct got_tree_entry **last_displayed_entry,
3311 struct got_tree_entry **selected_entry, int *ndisplayed,
3312 const char *label, int show_ids, const char *parent_path,
3313 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3315 const struct got_error *err = NULL;
3316 struct got_tree_entry *te;
3317 wchar_t *wline;
3318 int width, n;
3320 *ndisplayed = 0;
3322 werase(view->window);
3324 if (limit == 0)
3325 return NULL;
3327 err = format_line(&wline, &width, label, view->ncols);
3328 if (err)
3329 return err;
3330 if (view_needs_focus_indication(view))
3331 wstandout(view->window);
3332 waddwstr(view->window, wline);
3333 if (view_needs_focus_indication(view))
3334 wstandend(view->window);
3335 free(wline);
3336 wline = NULL;
3337 if (width < view->ncols)
3338 waddch(view->window, '\n');
3339 if (--limit <= 0)
3340 return NULL;
3341 err = format_line(&wline, &width, parent_path, view->ncols);
3342 if (err)
3343 return err;
3344 waddwstr(view->window, wline);
3345 free(wline);
3346 wline = NULL;
3347 if (width < view->ncols)
3348 waddch(view->window, '\n');
3349 if (--limit <= 0)
3350 return NULL;
3351 waddch(view->window, '\n');
3352 if (--limit <= 0)
3353 return NULL;
3355 te = SIMPLEQ_FIRST(&entries->head);
3356 if (*first_displayed_entry == NULL) {
3357 if (selected == 0) {
3358 if (view->focussed)
3359 wstandout(view->window);
3360 *selected_entry = NULL;
3362 waddstr(view->window, " ..\n"); /* parent directory */
3363 if (selected == 0 && view->focussed)
3364 wstandend(view->window);
3365 (*ndisplayed)++;
3366 if (--limit <= 0)
3367 return NULL;
3368 n = 1;
3369 } else {
3370 n = 0;
3371 while (te != *first_displayed_entry)
3372 te = SIMPLEQ_NEXT(te, entry);
3375 while (te) {
3376 char *line = NULL, *id_str = NULL;
3378 if (show_ids) {
3379 err = got_object_id_str(&id_str, te->id);
3380 if (err)
3381 return got_error_from_errno(
3382 "got_object_id_str");
3384 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3385 te->name, S_ISDIR(te->mode) ? "/" :
3386 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3387 free(id_str);
3388 return got_error_from_errno("asprintf");
3390 free(id_str);
3391 err = format_line(&wline, &width, line, view->ncols);
3392 if (err) {
3393 free(line);
3394 break;
3396 if (n == selected) {
3397 if (view->focussed)
3398 wstandout(view->window);
3399 *selected_entry = te;
3401 waddwstr(view->window, wline);
3402 if (width < view->ncols)
3403 waddch(view->window, '\n');
3404 if (n == selected && view->focussed)
3405 wstandend(view->window);
3406 free(line);
3407 free(wline);
3408 wline = NULL;
3409 n++;
3410 (*ndisplayed)++;
3411 *last_displayed_entry = te;
3412 if (--limit <= 0)
3413 break;
3414 te = SIMPLEQ_NEXT(te, entry);
3417 return err;
3420 static void
3421 tree_scroll_up(struct tog_view *view,
3422 struct got_tree_entry **first_displayed_entry, int maxscroll,
3423 const struct got_tree_entries *entries, int isroot)
3425 struct got_tree_entry *te, *prev;
3426 int i;
3428 if (*first_displayed_entry == NULL)
3429 return;
3431 te = SIMPLEQ_FIRST(&entries->head);
3432 if (*first_displayed_entry == te) {
3433 if (!isroot)
3434 *first_displayed_entry = NULL;
3435 return;
3438 /* XXX this is stupid... switch to TAILQ? */
3439 for (i = 0; i < maxscroll; i++) {
3440 while (te != *first_displayed_entry) {
3441 prev = te;
3442 te = SIMPLEQ_NEXT(te, entry);
3444 *first_displayed_entry = prev;
3445 te = SIMPLEQ_FIRST(&entries->head);
3447 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3448 *first_displayed_entry = NULL;
3451 static int
3452 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3453 struct got_tree_entry *last_displayed_entry,
3454 const struct got_tree_entries *entries)
3456 struct got_tree_entry *next, *last;
3457 int n = 0;
3459 if (*first_displayed_entry)
3460 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3461 else
3462 next = SIMPLEQ_FIRST(&entries->head);
3463 last = last_displayed_entry;
3464 while (next && last && n++ < maxscroll) {
3465 last = SIMPLEQ_NEXT(last, entry);
3466 if (last) {
3467 *first_displayed_entry = next;
3468 next = SIMPLEQ_NEXT(next, entry);
3471 return n;
3474 static const struct got_error *
3475 tree_entry_path(char **path, struct tog_parent_trees *parents,
3476 struct got_tree_entry *te)
3478 const struct got_error *err = NULL;
3479 struct tog_parent_tree *pt;
3480 size_t len = 2; /* for leading slash and NUL */
3482 TAILQ_FOREACH(pt, parents, entry)
3483 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3484 if (te)
3485 len += strlen(te->name);
3487 *path = calloc(1, len);
3488 if (path == NULL)
3489 return got_error_from_errno("calloc");
3491 (*path)[0] = '/';
3492 pt = TAILQ_LAST(parents, tog_parent_trees);
3493 while (pt) {
3494 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3495 err = got_error(GOT_ERR_NO_SPACE);
3496 goto done;
3498 if (strlcat(*path, "/", len) >= len) {
3499 err = got_error(GOT_ERR_NO_SPACE);
3500 goto done;
3502 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3504 if (te) {
3505 if (strlcat(*path, te->name, len) >= len) {
3506 err = got_error(GOT_ERR_NO_SPACE);
3507 goto done;
3510 done:
3511 if (err) {
3512 free(*path);
3513 *path = NULL;
3515 return err;
3518 static const struct got_error *
3519 blame_tree_entry(struct tog_view **new_view, int begin_x,
3520 struct got_tree_entry *te, struct tog_parent_trees *parents,
3521 struct got_object_id *commit_id, struct got_reflist_head *refs,
3522 struct got_repository *repo)
3524 const struct got_error *err = NULL;
3525 char *path;
3526 struct tog_view *blame_view;
3528 err = tree_entry_path(&path, parents, te);
3529 if (err)
3530 return err;
3532 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3533 if (blame_view == NULL)
3534 return got_error_from_errno("view_open");
3536 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3537 if (err) {
3538 view_close(blame_view);
3539 free(path);
3540 } else
3541 *new_view = blame_view;
3542 return err;
3545 static const struct got_error *
3546 log_tree_entry(struct tog_view **new_view, int begin_x,
3547 struct got_tree_entry *te, struct tog_parent_trees *parents,
3548 struct got_object_id *commit_id, struct got_reflist_head *refs,
3549 struct got_repository *repo)
3551 struct tog_view *log_view;
3552 const struct got_error *err = NULL;
3553 char *path;
3555 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3556 if (log_view == NULL)
3557 return got_error_from_errno("view_open");
3559 err = tree_entry_path(&path, parents, te);
3560 if (err)
3561 return err;
3563 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3564 if (err)
3565 view_close(log_view);
3566 else
3567 *new_view = log_view;
3568 free(path);
3569 return err;
3572 static const struct got_error *
3573 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3574 struct got_object_id *commit_id, struct got_reflist_head *refs,
3575 struct got_repository *repo)
3577 const struct got_error *err = NULL;
3578 char *commit_id_str = NULL;
3579 struct tog_tree_view_state *s = &view->state.tree;
3581 TAILQ_INIT(&s->parents);
3583 err = got_object_id_str(&commit_id_str, commit_id);
3584 if (err != NULL)
3585 goto done;
3587 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3588 err = got_error_from_errno("asprintf");
3589 goto done;
3592 s->root = s->tree = root;
3593 s->entries = got_object_tree_get_entries(root);
3594 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3595 s->commit_id = got_object_id_dup(commit_id);
3596 if (s->commit_id == NULL) {
3597 err = got_error_from_errno("got_object_id_dup");
3598 goto done;
3600 s->refs = refs;
3601 s->repo = repo;
3603 view->show = show_tree_view;
3604 view->input = input_tree_view;
3605 view->close = close_tree_view;
3606 done:
3607 free(commit_id_str);
3608 if (err) {
3609 free(s->tree_label);
3610 s->tree_label = NULL;
3612 return err;
3615 static const struct got_error *
3616 close_tree_view(struct tog_view *view)
3618 struct tog_tree_view_state *s = &view->state.tree;
3620 free(s->tree_label);
3621 s->tree_label = NULL;
3622 free(s->commit_id);
3623 s->commit_id = NULL;
3624 while (!TAILQ_EMPTY(&s->parents)) {
3625 struct tog_parent_tree *parent;
3626 parent = TAILQ_FIRST(&s->parents);
3627 TAILQ_REMOVE(&s->parents, parent, entry);
3628 free(parent);
3631 if (s->tree != s->root)
3632 got_object_tree_close(s->tree);
3633 got_object_tree_close(s->root);
3635 return NULL;
3638 static const struct got_error *
3639 show_tree_view(struct tog_view *view)
3641 const struct got_error *err = NULL;
3642 struct tog_tree_view_state *s = &view->state.tree;
3643 char *parent_path;
3645 err = tree_entry_path(&parent_path, &s->parents, NULL);
3646 if (err)
3647 return err;
3649 err = draw_tree_entries(view, &s->first_displayed_entry,
3650 &s->last_displayed_entry, &s->selected_entry,
3651 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3652 s->entries, s->selected, view->nlines, s->tree == s->root);
3653 free(parent_path);
3655 view_vborder(view);
3656 return err;
3659 static const struct got_error *
3660 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3661 struct tog_view **focus_view, struct tog_view *view, int ch)
3663 const struct got_error *err = NULL;
3664 struct tog_tree_view_state *s = &view->state.tree;
3665 struct tog_view *log_view;
3666 int begin_x = 0, nscrolled;
3668 switch (ch) {
3669 case 'i':
3670 s->show_ids = !s->show_ids;
3671 break;
3672 case 'l':
3673 if (!s->selected_entry)
3674 break;
3675 if (view_is_parent_view(view))
3676 begin_x = view_split_begin_x(view->begin_x);
3677 err = log_tree_entry(&log_view, begin_x,
3678 s->selected_entry, &s->parents,
3679 s->commit_id, s->refs, s->repo);
3680 if (view_is_parent_view(view)) {
3681 err = view_close_child(view);
3682 if (err)
3683 return err;
3684 err = view_set_child(view, log_view);
3685 if (err) {
3686 view_close(log_view);
3687 break;
3689 *focus_view = log_view;
3690 view->child_focussed = 1;
3691 } else
3692 *new_view = log_view;
3693 break;
3694 case 'k':
3695 case KEY_UP:
3696 if (s->selected > 0) {
3697 s->selected--;
3698 if (s->selected == 0)
3699 break;
3701 if (s->selected > 0)
3702 break;
3703 tree_scroll_up(view, &s->first_displayed_entry, 1,
3704 s->entries, s->tree == s->root);
3705 break;
3706 case KEY_PPAGE:
3707 tree_scroll_up(view, &s->first_displayed_entry,
3708 MAX(0, view->nlines - 4 - s->selected), s->entries,
3709 s->tree == s->root);
3710 s->selected = 0;
3711 if (SIMPLEQ_FIRST(&s->entries->head) ==
3712 s->first_displayed_entry && s->tree != s->root)
3713 s->first_displayed_entry = NULL;
3714 break;
3715 case 'j':
3716 case KEY_DOWN:
3717 if (s->selected < s->ndisplayed - 1) {
3718 s->selected++;
3719 break;
3721 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3722 /* can't scroll any further */
3723 break;
3724 tree_scroll_down(&s->first_displayed_entry, 1,
3725 s->last_displayed_entry, s->entries);
3726 break;
3727 case KEY_NPAGE:
3728 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3729 == NULL) {
3730 /* can't scroll any further; move cursor down */
3731 if (s->selected < s->ndisplayed - 1)
3732 s->selected = s->ndisplayed - 1;
3733 break;
3735 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3736 view->nlines, s->last_displayed_entry, s->entries);
3737 if (nscrolled < view->nlines) {
3738 int ndisplayed = 0;
3739 struct got_tree_entry *te;
3740 te = s->first_displayed_entry;
3741 do {
3742 ndisplayed++;
3743 te = SIMPLEQ_NEXT(te, entry);
3744 } while (te);
3745 s->selected = ndisplayed - 1;
3747 break;
3748 case KEY_ENTER:
3749 case '\r':
3750 case KEY_BACKSPACE:
3751 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3752 struct tog_parent_tree *parent;
3753 /* user selected '..' */
3754 if (s->tree == s->root)
3755 break;
3756 parent = TAILQ_FIRST(&s->parents);
3757 TAILQ_REMOVE(&s->parents, parent,
3758 entry);
3759 got_object_tree_close(s->tree);
3760 s->tree = parent->tree;
3761 s->entries =
3762 got_object_tree_get_entries(s->tree);
3763 s->first_displayed_entry =
3764 parent->first_displayed_entry;
3765 s->selected_entry =
3766 parent->selected_entry;
3767 s->selected = parent->selected;
3768 free(parent);
3769 } else if (S_ISDIR(s->selected_entry->mode)) {
3770 struct tog_parent_tree *parent;
3771 struct got_tree_object *child;
3772 err = got_object_open_as_tree(&child,
3773 s->repo, s->selected_entry->id);
3774 if (err)
3775 break;
3776 parent = calloc(1, sizeof(*parent));
3777 if (parent == NULL) {
3778 err = got_error_from_errno("calloc");
3779 break;
3781 parent->tree = s->tree;
3782 parent->first_displayed_entry =
3783 s->first_displayed_entry;
3784 parent->selected_entry = s->selected_entry;
3785 parent->selected = s->selected;
3786 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3787 s->tree = child;
3788 s->entries =
3789 got_object_tree_get_entries(s->tree);
3790 s->selected = 0;
3791 s->first_displayed_entry = NULL;
3792 } else if (S_ISREG(s->selected_entry->mode)) {
3793 struct tog_view *blame_view;
3794 int begin_x = view_is_parent_view(view) ?
3795 view_split_begin_x(view->begin_x) : 0;
3797 err = blame_tree_entry(&blame_view, begin_x,
3798 s->selected_entry, &s->parents,
3799 s->commit_id, s->refs, s->repo);
3800 if (err)
3801 break;
3802 if (view_is_parent_view(view)) {
3803 err = view_close_child(view);
3804 if (err)
3805 return err;
3806 err = view_set_child(view, blame_view);
3807 if (err) {
3808 view_close(blame_view);
3809 break;
3811 *focus_view = blame_view;
3812 view->child_focussed = 1;
3813 } else
3814 *new_view = blame_view;
3816 break;
3817 case KEY_RESIZE:
3818 if (s->selected > view->nlines)
3819 s->selected = s->ndisplayed - 1;
3820 break;
3821 default:
3822 break;
3825 return err;
3828 __dead static void
3829 usage_tree(void)
3831 endwin();
3832 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3833 getprogname());
3834 exit(1);
3837 static const struct got_error *
3838 cmd_tree(int argc, char *argv[])
3840 const struct got_error *error;
3841 struct got_repository *repo = NULL;
3842 struct got_reflist_head refs;
3843 char *repo_path = NULL;
3844 struct got_object_id *commit_id = NULL;
3845 char *commit_id_arg = NULL;
3846 struct got_commit_object *commit = NULL;
3847 struct got_tree_object *tree = NULL;
3848 int ch;
3849 struct tog_view *view;
3851 SIMPLEQ_INIT(&refs);
3853 #ifndef PROFILE
3854 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3855 NULL) == -1)
3856 err(1, "pledge");
3857 #endif
3859 while ((ch = getopt(argc, argv, "c:")) != -1) {
3860 switch (ch) {
3861 case 'c':
3862 commit_id_arg = optarg;
3863 break;
3864 default:
3865 usage_tree();
3866 /* NOTREACHED */
3870 argc -= optind;
3871 argv += optind;
3873 if (argc == 0) {
3874 struct got_worktree *worktree;
3875 char *cwd = getcwd(NULL, 0);
3876 if (cwd == NULL)
3877 return got_error_from_errno("getcwd");
3878 error = got_worktree_open(&worktree, cwd);
3879 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3880 goto done;
3881 if (worktree) {
3882 free(cwd);
3883 repo_path =
3884 strdup(got_worktree_get_repo_path(worktree));
3885 got_worktree_close(worktree);
3886 } else
3887 repo_path = cwd;
3888 if (repo_path == NULL) {
3889 error = got_error_from_errno("strdup");
3890 goto done;
3892 } else if (argc == 1) {
3893 repo_path = realpath(argv[0], NULL);
3894 if (repo_path == NULL)
3895 return got_error_from_errno2("realpath", argv[0]);
3896 } else
3897 usage_log();
3899 init_curses();
3901 error = got_repo_open(&repo, repo_path);
3902 if (error != NULL)
3903 goto done;
3905 error = apply_unveil(got_repo_get_path(repo), NULL);
3906 if (error)
3907 goto done;
3909 if (commit_id_arg == NULL)
3910 error = get_head_commit_id(&commit_id, repo);
3911 else
3912 error = got_object_resolve_id_str(&commit_id, repo,
3913 commit_id_arg);
3914 if (error != NULL)
3915 goto done;
3917 error = got_object_open_as_commit(&commit, repo, commit_id);
3918 if (error != NULL)
3919 goto done;
3921 error = got_object_open_as_tree(&tree, repo,
3922 got_object_commit_get_tree_id(commit));
3923 if (error != NULL)
3924 goto done;
3926 error = got_ref_list(&refs, repo);
3927 if (error)
3928 goto done;
3930 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3931 if (view == NULL) {
3932 error = got_error_from_errno("view_open");
3933 goto done;
3935 error = open_tree_view(view, tree, commit_id, &refs, repo);
3936 if (error)
3937 goto done;
3938 error = view_loop(view);
3939 done:
3940 free(repo_path);
3941 free(commit_id);
3942 if (commit)
3943 got_object_commit_close(commit);
3944 if (tree)
3945 got_object_tree_close(tree);
3946 if (repo)
3947 got_repo_close(repo);
3948 got_ref_list_free(&refs);
3949 return error;
3952 __dead static void
3953 usage(void)
3955 int i;
3957 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3958 "Available commands:\n", getprogname());
3959 for (i = 0; i < nitems(tog_commands); i++) {
3960 struct tog_cmd *cmd = &tog_commands[i];
3961 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3963 exit(1);
3966 static char **
3967 make_argv(const char *arg0, const char *arg1)
3969 char **argv;
3970 int argc = (arg1 == NULL ? 1 : 2);
3972 argv = calloc(argc, sizeof(char *));
3973 if (argv == NULL)
3974 err(1, "calloc");
3975 argv[0] = strdup(arg0);
3976 if (argv[0] == NULL)
3977 err(1, "calloc");
3978 if (arg1) {
3979 argv[1] = strdup(arg1);
3980 if (argv[1] == NULL)
3981 err(1, "calloc");
3984 return argv;
3987 int
3988 main(int argc, char *argv[])
3990 const struct got_error *error = NULL;
3991 struct tog_cmd *cmd = NULL;
3992 int ch, hflag = 0;
3993 char **cmd_argv = NULL;
3995 setlocale(LC_CTYPE, "");
3997 while ((ch = getopt(argc, argv, "h")) != -1) {
3998 switch (ch) {
3999 case 'h':
4000 hflag = 1;
4001 break;
4002 default:
4003 usage();
4004 /* NOTREACHED */
4008 argc -= optind;
4009 argv += optind;
4010 optind = 0;
4011 optreset = 1;
4013 if (argc == 0) {
4014 if (hflag)
4015 usage();
4016 /* Build an argument vector which runs a default command. */
4017 cmd = &tog_commands[0];
4018 cmd_argv = make_argv(cmd->name, NULL);
4019 argc = 1;
4020 } else {
4021 int i;
4023 /* Did the user specific a command? */
4024 for (i = 0; i < nitems(tog_commands); i++) {
4025 if (strncmp(tog_commands[i].name, argv[0],
4026 strlen(argv[0])) == 0) {
4027 cmd = &tog_commands[i];
4028 if (hflag)
4029 tog_commands[i].cmd_usage();
4030 break;
4033 if (cmd == NULL) {
4034 /* Did the user specify a repository? */
4035 char *repo_path = realpath(argv[0], NULL);
4036 if (repo_path) {
4037 struct got_repository *repo;
4038 error = got_repo_open(&repo, repo_path);
4039 if (error == NULL)
4040 got_repo_close(repo);
4041 } else
4042 error = got_error_from_errno2("realpath",
4043 argv[0]);
4044 if (error) {
4045 if (hflag) {
4046 fprintf(stderr, "%s: '%s' is not a "
4047 "known command\n", getprogname(),
4048 argv[0]);
4049 usage();
4051 fprintf(stderr, "%s: '%s' is neither a known "
4052 "command nor a path to a repository\n",
4053 getprogname(), argv[0]);
4054 free(repo_path);
4055 return 1;
4057 cmd = &tog_commands[0];
4058 cmd_argv = make_argv(cmd->name, repo_path);
4059 argc = 2;
4060 free(repo_path);
4064 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4065 if (error)
4066 goto done;
4067 done:
4068 endwin();
4069 free(cmd_argv);
4070 if (error)
4071 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4072 return 0;