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, const char *branch_name,
1110 struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_reference *head_ref;
1115 *head_id = NULL;
1117 err = got_ref_open(&head_ref, repo, branch_name, 0);
1118 if (err)
1119 return err;
1121 err = got_ref_resolve(head_id, repo, head_ref);
1122 got_ref_close(head_ref);
1123 if (err) {
1124 *head_id = NULL;
1125 return err;
1128 return NULL;
1131 static const struct got_error *
1132 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1133 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1134 struct commit_queue *commits, int selected_idx, int limit,
1135 struct got_reflist_head *refs, const char *path, int commits_needed)
1137 const struct got_error *err = NULL;
1138 struct commit_queue_entry *entry;
1139 int ncommits, width;
1140 int author_cols = 10;
1141 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1142 char *refs_str = NULL;
1143 wchar_t *wline;
1145 entry = first;
1146 ncommits = 0;
1147 while (entry) {
1148 if (ncommits == selected_idx) {
1149 *selected = entry;
1150 break;
1152 entry = TAILQ_NEXT(entry, entry);
1153 ncommits++;
1156 if (*selected) {
1157 err = got_object_id_str(&id_str, (*selected)->id);
1158 if (err)
1159 return err;
1160 if (refs) {
1161 err = build_refs_str(&refs_str, refs, (*selected)->id);
1162 if (err)
1163 goto done;
1167 if (commits_needed == 0)
1168 halfdelay(10); /* disable fast refresh */
1170 if (asprintf(&ncommits_str, " [%d/%d] %s",
1171 entry ? entry->idx + 1 : 0, commits->ncommits,
1172 commits_needed > 0 ? "loading... " :
1173 (refs_str ? refs_str : "")) == -1) {
1174 err = got_error_from_errno("asprintf");
1175 goto done;
1178 if (path && strcmp(path, "/") != 0) {
1179 if (asprintf(&header, "commit %s %s%s",
1180 id_str ? id_str : "........................................",
1181 path, ncommits_str) == -1) {
1182 err = got_error_from_errno("asprintf");
1183 header = NULL;
1184 goto done;
1186 } else if (asprintf(&header, "commit %s%s",
1187 id_str ? id_str : "........................................",
1188 ncommits_str) == -1) {
1189 err = got_error_from_errno("asprintf");
1190 header = NULL;
1191 goto done;
1193 err = format_line(&wline, &width, header, view->ncols);
1194 if (err)
1195 goto done;
1197 werase(view->window);
1199 if (view_needs_focus_indication(view))
1200 wstandout(view->window);
1201 waddwstr(view->window, wline);
1202 while (width < view->ncols) {
1203 waddch(view->window, ' ');
1204 width++;
1206 if (view_needs_focus_indication(view))
1207 wstandend(view->window);
1208 free(wline);
1209 if (limit <= 1)
1210 goto done;
1212 /* Grow author column size if necessary. */
1213 entry = first;
1214 ncommits = 0;
1215 while (entry) {
1216 char *author;
1217 wchar_t *wauthor;
1218 int width;
1219 if (ncommits >= limit - 1)
1220 break;
1221 author = strdup(got_object_commit_get_author(entry->commit));
1222 if (author == NULL) {
1223 err = got_error_from_errno("strdup");
1224 goto done;
1226 err = format_author(&wauthor, &width, author, COLS);
1227 if (author_cols < width)
1228 author_cols = width;
1229 free(wauthor);
1230 free(author);
1231 entry = TAILQ_NEXT(entry, entry);
1234 entry = first;
1235 *last = first;
1236 ncommits = 0;
1237 while (entry) {
1238 if (ncommits >= limit - 1)
1239 break;
1240 if (ncommits == selected_idx)
1241 wstandout(view->window);
1242 err = draw_commit(view, entry->commit, entry->id, refs,
1243 author_cols);
1244 if (ncommits == selected_idx)
1245 wstandend(view->window);
1246 if (err)
1247 break;
1248 ncommits++;
1249 *last = entry;
1250 entry = TAILQ_NEXT(entry, entry);
1253 view_vborder(view);
1254 done:
1255 free(id_str);
1256 free(refs_str);
1257 free(ncommits_str);
1258 free(header);
1259 return err;
1262 static void
1263 scroll_up(struct tog_view *view,
1264 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1265 struct commit_queue *commits)
1267 struct commit_queue_entry *entry;
1268 int nscrolled = 0;
1270 entry = TAILQ_FIRST(&commits->head);
1271 if (*first_displayed_entry == entry)
1272 return;
1274 entry = *first_displayed_entry;
1275 while (entry && nscrolled < maxscroll) {
1276 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1277 if (entry) {
1278 *first_displayed_entry = entry;
1279 nscrolled++;
1284 static const struct got_error *
1285 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1286 pthread_cond_t *need_commits)
1288 int errcode;
1289 int max_wait = 20;
1291 halfdelay(1); /* fast refresh while loading commits */
1293 while (*commits_needed > 0) {
1294 if (*log_complete)
1295 break;
1297 /* Wake the log thread. */
1298 errcode = pthread_cond_signal(need_commits);
1299 if (errcode)
1300 return got_error_set_errno(errcode,
1301 "pthread_cond_signal");
1302 errcode = pthread_mutex_unlock(&tog_mutex);
1303 if (errcode)
1304 return got_error_set_errno(errcode,
1305 "pthread_mutex_unlock");
1306 pthread_yield();
1307 errcode = pthread_mutex_lock(&tog_mutex);
1308 if (errcode)
1309 return got_error_set_errno(errcode,
1310 "pthread_mutex_lock");
1312 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1314 * Thread is not done yet; lose a key press
1315 * and let the user retry... this way the GUI
1316 * remains interactive while logging deep paths
1317 * with few commits in history.
1319 return NULL;
1323 return NULL;
1326 static const struct got_error *
1327 scroll_down(struct tog_view *view,
1328 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1329 struct commit_queue_entry **last_displayed_entry,
1330 struct commit_queue *commits, int *log_complete, int *commits_needed,
1331 pthread_cond_t *need_commits)
1333 const struct got_error *err = NULL;
1334 struct commit_queue_entry *pentry;
1335 int nscrolled = 0;
1337 if (*last_displayed_entry == NULL)
1338 return NULL;
1340 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1341 if (pentry == NULL && !*log_complete) {
1343 * Ask the log thread for required amount of commits
1344 * plus some amount of pre-fetching.
1346 (*commits_needed) += maxscroll + 20;
1347 err = trigger_log_thread(0, commits_needed, log_complete,
1348 need_commits);
1349 if (err)
1350 return err;
1353 do {
1354 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1355 if (pentry == NULL)
1356 break;
1358 *last_displayed_entry = pentry;
1360 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1361 if (pentry == NULL)
1362 break;
1363 *first_displayed_entry = pentry;
1364 } while (++nscrolled < maxscroll);
1366 return err;
1369 static const struct got_error *
1370 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1371 struct got_commit_object *commit, struct got_object_id *commit_id,
1372 struct tog_view *log_view, struct got_reflist_head *refs,
1373 struct got_repository *repo)
1375 const struct got_error *err;
1376 struct got_object_qid *parent_id;
1377 struct tog_view *diff_view;
1379 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1380 if (diff_view == NULL)
1381 return got_error_from_errno("view_open");
1383 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1384 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1385 commit_id, log_view, refs, repo);
1386 if (err == NULL)
1387 *new_view = diff_view;
1388 return err;
1391 static const struct got_error *
1392 browse_commit(struct tog_view **new_view, int begin_x,
1393 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1394 struct got_repository *repo)
1396 const struct got_error *err = NULL;
1397 struct got_tree_object *tree;
1398 struct tog_view *tree_view;
1400 err = got_object_open_as_tree(&tree, repo,
1401 got_object_commit_get_tree_id(entry->commit));
1402 if (err)
1403 return err;
1405 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1406 if (tree_view == NULL)
1407 return got_error_from_errno("view_open");
1409 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1410 if (err)
1411 got_object_tree_close(tree);
1412 else
1413 *new_view = tree_view;
1414 return err;
1417 static void *
1418 log_thread(void *arg)
1420 const struct got_error *err = NULL;
1421 int errcode = 0;
1422 struct tog_log_thread_args *a = arg;
1423 int done = 0;
1425 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1426 if (err)
1427 return (void *)err;
1429 while (!done && !err) {
1430 err = queue_commits(a->graph, a->commits, 1, a->repo,
1431 a->in_repo_path);
1432 if (err) {
1433 if (err->code != GOT_ERR_ITER_COMPLETED)
1434 return (void *)err;
1435 err = NULL;
1436 done = 1;
1437 } else if (a->commits_needed > 0)
1438 a->commits_needed--;
1440 errcode = pthread_mutex_lock(&tog_mutex);
1441 if (errcode) {
1442 err = got_error_set_errno(errcode,
1443 "pthread_mutex_lock");
1444 break;
1445 } else if (*a->quit)
1446 done = 1;
1447 else if (*a->first_displayed_entry == NULL) {
1448 *a->first_displayed_entry =
1449 TAILQ_FIRST(&a->commits->head);
1450 *a->selected_entry = *a->first_displayed_entry;
1453 if (done)
1454 a->commits_needed = 0;
1455 else if (a->commits_needed == 0) {
1456 errcode = pthread_cond_wait(&a->need_commits,
1457 &tog_mutex);
1458 if (errcode)
1459 err = got_error_set_errno(errcode,
1460 "pthread_cond_wait");
1463 errcode = pthread_mutex_unlock(&tog_mutex);
1464 if (errcode && err == NULL)
1465 err = got_error_set_errno(errcode,
1466 "pthread_mutex_unlock");
1468 a->log_complete = 1;
1469 return (void *)err;
1472 static const struct got_error *
1473 stop_log_thread(struct tog_log_view_state *s)
1475 const struct got_error *err = NULL;
1476 int errcode;
1478 if (s->thread) {
1479 s->quit = 1;
1480 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1481 if (errcode)
1482 return got_error_set_errno(errcode,
1483 "pthread_cond_signal");
1484 errcode = pthread_mutex_unlock(&tog_mutex);
1485 if (errcode)
1486 return got_error_set_errno(errcode,
1487 "pthread_mutex_unlock");
1488 errcode = pthread_join(s->thread, (void **)&err);
1489 if (errcode)
1490 return got_error_set_errno(errcode, "pthread_join");
1491 errcode = pthread_mutex_lock(&tog_mutex);
1492 if (errcode)
1493 return got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1495 s->thread = NULL;
1498 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1499 if (errcode && err == NULL)
1500 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1502 if (s->thread_args.repo) {
1503 got_repo_close(s->thread_args.repo);
1504 s->thread_args.repo = NULL;
1507 if (s->thread_args.graph) {
1508 got_commit_graph_close(s->thread_args.graph);
1509 s->thread_args.graph = NULL;
1512 return err;
1515 static const struct got_error *
1516 close_log_view(struct tog_view *view)
1518 const struct got_error *err = NULL;
1519 struct tog_log_view_state *s = &view->state.log;
1521 err = stop_log_thread(s);
1522 free_commits(&s->commits);
1523 free(s->in_repo_path);
1524 s->in_repo_path = NULL;
1525 free(s->start_id);
1526 s->start_id = NULL;
1527 return err;
1530 static const struct got_error *
1531 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1532 struct got_reflist_head *refs, struct got_repository *repo,
1533 const char *path, int check_disk)
1535 const struct got_error *err = NULL;
1536 struct tog_log_view_state *s = &view->state.log;
1537 struct got_repository *thread_repo = NULL;
1538 struct got_commit_graph *thread_graph = NULL;
1539 int errcode;
1541 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1542 if (err != NULL)
1543 goto done;
1545 /* The commit queue only contains commits being displayed. */
1546 TAILQ_INIT(&s->commits.head);
1547 s->commits.ncommits = 0;
1549 s->refs = refs;
1550 s->repo = repo;
1551 s->start_id = got_object_id_dup(start_id);
1552 if (s->start_id == NULL) {
1553 err = got_error_from_errno("got_object_id_dup");
1554 goto done;
1557 view->show = show_log_view;
1558 view->input = input_log_view;
1559 view->close = close_log_view;
1561 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1562 if (err)
1563 goto done;
1564 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1565 0, thread_repo);
1566 if (err)
1567 goto done;
1569 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1570 if (errcode) {
1571 err = got_error_set_errno(errcode, "pthread_cond_init");
1572 goto done;
1575 s->thread_args.commits_needed = view->nlines;
1576 s->thread_args.graph = thread_graph;
1577 s->thread_args.commits = &s->commits;
1578 s->thread_args.in_repo_path = s->in_repo_path;
1579 s->thread_args.start_id = s->start_id;
1580 s->thread_args.repo = thread_repo;
1581 s->thread_args.log_complete = 0;
1582 s->thread_args.quit = &s->quit;
1583 s->thread_args.view = view;
1584 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1585 s->thread_args.selected_entry = &s->selected_entry;
1586 done:
1587 if (err)
1588 close_log_view(view);
1589 return err;
1592 static const struct got_error *
1593 show_log_view(struct tog_view *view)
1595 struct tog_log_view_state *s = &view->state.log;
1597 if (s->thread == NULL) {
1598 int errcode = pthread_create(&s->thread, NULL, log_thread,
1599 &s->thread_args);
1600 if (errcode)
1601 return got_error_set_errno(errcode, "pthread_create");
1604 return draw_commits(view, &s->last_displayed_entry,
1605 &s->selected_entry, s->first_displayed_entry,
1606 &s->commits, s->selected, view->nlines, s->refs,
1607 s->in_repo_path, s->thread_args.commits_needed);
1610 static const struct got_error *
1611 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1612 struct tog_view **focus_view, struct tog_view *view, int ch)
1614 const struct got_error *err = NULL;
1615 struct tog_log_view_state *s = &view->state.log;
1616 char *parent_path;
1617 struct tog_view *diff_view = NULL, *tree_view = NULL;
1618 int begin_x = 0;
1620 switch (ch) {
1621 case 'q':
1622 s->quit = 1;
1623 break;
1624 case 'k':
1625 case KEY_UP:
1626 case '<':
1627 case ',':
1628 if (s->first_displayed_entry == NULL)
1629 break;
1630 if (s->selected > 0)
1631 s->selected--;
1632 if (s->selected > 0)
1633 break;
1634 scroll_up(view, &s->first_displayed_entry, 1,
1635 &s->commits);
1636 break;
1637 case KEY_PPAGE:
1638 case CTRL('b'):
1639 if (s->first_displayed_entry == NULL)
1640 break;
1641 if (TAILQ_FIRST(&s->commits.head) ==
1642 s->first_displayed_entry) {
1643 s->selected = 0;
1644 break;
1646 scroll_up(view, &s->first_displayed_entry,
1647 view->nlines, &s->commits);
1648 break;
1649 case 'j':
1650 case KEY_DOWN:
1651 case '>':
1652 case '.':
1653 if (s->first_displayed_entry == NULL)
1654 break;
1655 if (s->selected < MIN(view->nlines - 2,
1656 s->commits.ncommits - 1)) {
1657 s->selected++;
1658 break;
1660 err = scroll_down(view, &s->first_displayed_entry, 1,
1661 &s->last_displayed_entry, &s->commits,
1662 &s->thread_args.log_complete,
1663 &s->thread_args.commits_needed,
1664 &s->thread_args.need_commits);
1665 break;
1666 case KEY_NPAGE:
1667 case CTRL('f'): {
1668 struct commit_queue_entry *first;
1669 first = s->first_displayed_entry;
1670 if (first == NULL)
1671 break;
1672 err = scroll_down(view, &s->first_displayed_entry,
1673 view->nlines, &s->last_displayed_entry,
1674 &s->commits, &s->thread_args.log_complete,
1675 &s->thread_args.commits_needed,
1676 &s->thread_args.need_commits);
1677 if (first == s->first_displayed_entry &&
1678 s->selected < MIN(view->nlines - 2,
1679 s->commits.ncommits - 1)) {
1680 /* can't scroll further down */
1681 s->selected = MIN(view->nlines - 2,
1682 s->commits.ncommits - 1);
1684 err = NULL;
1685 break;
1687 case KEY_RESIZE:
1688 if (s->selected > view->nlines - 2)
1689 s->selected = view->nlines - 2;
1690 if (s->selected > s->commits.ncommits - 1)
1691 s->selected = s->commits.ncommits - 1;
1692 break;
1693 case KEY_ENTER:
1694 case ' ':
1695 case '\r':
1696 if (s->selected_entry == NULL)
1697 break;
1698 if (view_is_parent_view(view))
1699 begin_x = view_split_begin_x(view->begin_x);
1700 err = open_diff_view_for_commit(&diff_view, begin_x,
1701 s->selected_entry->commit, s->selected_entry->id,
1702 view, s->refs, s->repo);
1703 if (err)
1704 break;
1705 if (view_is_parent_view(view)) {
1706 err = view_close_child(view);
1707 if (err)
1708 return err;
1709 err = view_set_child(view, diff_view);
1710 if (err) {
1711 view_close(diff_view);
1712 break;
1714 *focus_view = diff_view;
1715 view->child_focussed = 1;
1716 } else
1717 *new_view = diff_view;
1718 break;
1719 case 't':
1720 if (s->selected_entry == NULL)
1721 break;
1722 if (view_is_parent_view(view))
1723 begin_x = view_split_begin_x(view->begin_x);
1724 err = browse_commit(&tree_view, begin_x,
1725 s->selected_entry, s->refs, s->repo);
1726 if (err)
1727 break;
1728 if (view_is_parent_view(view)) {
1729 err = view_close_child(view);
1730 if (err)
1731 return err;
1732 err = view_set_child(view, tree_view);
1733 if (err) {
1734 view_close(tree_view);
1735 break;
1737 *focus_view = tree_view;
1738 view->child_focussed = 1;
1739 } else
1740 *new_view = tree_view;
1741 break;
1742 case KEY_BACKSPACE:
1743 if (strcmp(s->in_repo_path, "/") == 0)
1744 break;
1745 parent_path = dirname(s->in_repo_path);
1746 if (parent_path && strcmp(parent_path, ".") != 0) {
1747 struct tog_view *lv;
1748 err = stop_log_thread(s);
1749 if (err)
1750 return err;
1751 lv = view_open(view->nlines, view->ncols,
1752 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1753 if (lv == NULL)
1754 return got_error_from_errno(
1755 "view_open");
1756 err = open_log_view(lv, s->start_id, s->refs,
1757 s->repo, parent_path, 0);
1758 if (err)
1759 return err;;
1760 if (view_is_parent_view(view))
1761 *new_view = lv;
1762 else {
1763 view_set_child(view->parent, lv);
1764 *focus_view = lv;
1766 return NULL;
1768 break;
1769 default:
1770 break;
1773 return err;
1776 static const struct got_error *
1777 apply_unveil(const char *repo_path, const char *worktree_path)
1779 const struct got_error *error;
1781 if (repo_path && unveil(repo_path, "r") != 0)
1782 return got_error_from_errno2("unveil", repo_path);
1784 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1785 return got_error_from_errno2("unveil", worktree_path);
1787 if (unveil("/tmp", "rwc") != 0)
1788 return got_error_from_errno2("unveil", "/tmp");
1790 error = got_privsep_unveil_exec_helpers();
1791 if (error != NULL)
1792 return error;
1794 if (unveil(NULL, NULL) != 0)
1795 return got_error_from_errno("unveil");
1797 return NULL;
1800 static void
1801 init_curses(void)
1803 initscr();
1804 cbreak();
1805 halfdelay(1); /* Do fast refresh while initial view is loading. */
1806 noecho();
1807 nonl();
1808 intrflush(stdscr, FALSE);
1809 keypad(stdscr, TRUE);
1810 curs_set(0);
1811 signal(SIGWINCH, tog_sigwinch);
1814 static const struct got_error *
1815 cmd_log(int argc, char *argv[])
1817 const struct got_error *error;
1818 struct got_repository *repo = NULL;
1819 struct got_worktree *worktree = NULL;
1820 struct got_reflist_head refs;
1821 struct got_object_id *start_id = NULL;
1822 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1823 char *start_commit = NULL;
1824 int ch;
1825 struct tog_view *view;
1827 SIMPLEQ_INIT(&refs);
1829 #ifndef PROFILE
1830 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1831 NULL) == -1)
1832 err(1, "pledge");
1833 #endif
1835 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1836 switch (ch) {
1837 case 'c':
1838 start_commit = optarg;
1839 break;
1840 case 'r':
1841 repo_path = realpath(optarg, NULL);
1842 if (repo_path == NULL)
1843 err(1, "-r option");
1844 break;
1845 default:
1846 usage_log();
1847 /* NOTREACHED */
1851 argc -= optind;
1852 argv += optind;
1854 cwd = getcwd(NULL, 0);
1855 if (cwd == NULL) {
1856 error = got_error_from_errno("getcwd");
1857 goto done;
1859 error = got_worktree_open(&worktree, cwd);
1860 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1861 goto done;
1862 error = NULL;
1864 if (argc == 0) {
1865 path = strdup("");
1866 if (path == NULL) {
1867 error = got_error_from_errno("strdup");
1868 goto done;
1870 } else if (argc == 1) {
1871 if (worktree) {
1872 error = got_worktree_resolve_path(&path, worktree,
1873 argv[0]);
1874 if (error)
1875 goto done;
1876 } else {
1877 path = strdup(argv[0]);
1878 if (path == NULL) {
1879 error = got_error_from_errno("strdup");
1880 goto done;
1883 } else
1884 usage_log();
1886 repo_path = worktree ?
1887 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1888 if (repo_path == NULL) {
1889 error = got_error_from_errno("strdup");
1890 goto done;
1893 init_curses();
1895 error = got_repo_open(&repo, repo_path);
1896 if (error != NULL)
1897 goto done;
1899 error = apply_unveil(got_repo_get_path(repo),
1900 worktree ? got_worktree_get_root_path(worktree) : NULL);
1901 if (error)
1902 goto done;
1904 if (start_commit == NULL)
1905 error = get_head_commit_id(&start_id, worktree ?
1906 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
1907 repo);
1908 else
1909 error = got_object_resolve_id_str(&start_id, repo,
1910 start_commit);
1911 if (error != NULL)
1912 goto done;
1914 error = got_ref_list(&refs, repo);
1915 if (error)
1916 goto done;
1918 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1919 if (view == NULL) {
1920 error = got_error_from_errno("view_open");
1921 goto done;
1923 error = open_log_view(view, start_id, &refs, repo, path, 1);
1924 if (error)
1925 goto done;
1926 error = view_loop(view);
1927 done:
1928 free(repo_path);
1929 free(cwd);
1930 free(path);
1931 free(start_id);
1932 if (repo)
1933 got_repo_close(repo);
1934 if (worktree)
1935 got_worktree_close(worktree);
1936 got_ref_list_free(&refs);
1937 return error;
1940 __dead static void
1941 usage_diff(void)
1943 endwin();
1944 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1945 getprogname());
1946 exit(1);
1949 static char *
1950 parse_next_line(FILE *f, size_t *len)
1952 char *line;
1953 size_t linelen;
1954 size_t lineno;
1955 const char delim[3] = { '\0', '\0', '\0'};
1957 line = fparseln(f, &linelen, &lineno, delim, 0);
1958 if (len)
1959 *len = linelen;
1960 return line;
1963 static const struct got_error *
1964 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1965 int *last_displayed_line, int *eof, int max_lines,
1966 char *header)
1968 const struct got_error *err;
1969 int nlines = 0, nprinted = 0;
1970 char *line;
1971 size_t len;
1972 wchar_t *wline;
1973 int width;
1975 rewind(f);
1976 werase(view->window);
1978 if (header) {
1979 err = format_line(&wline, &width, header, view->ncols);
1980 if (err) {
1981 return err;
1984 if (view_needs_focus_indication(view))
1985 wstandout(view->window);
1986 waddwstr(view->window, wline);
1987 if (view_needs_focus_indication(view))
1988 wstandend(view->window);
1989 if (width < view->ncols)
1990 waddch(view->window, '\n');
1992 if (max_lines <= 1)
1993 return NULL;
1994 max_lines--;
1997 *eof = 0;
1998 while (nprinted < max_lines) {
1999 line = parse_next_line(f, &len);
2000 if (line == NULL) {
2001 *eof = 1;
2002 break;
2004 if (++nlines < *first_displayed_line) {
2005 free(line);
2006 continue;
2009 err = format_line(&wline, &width, line, view->ncols);
2010 if (err) {
2011 free(line);
2012 return err;
2014 waddwstr(view->window, wline);
2015 if (width < view->ncols)
2016 waddch(view->window, '\n');
2017 if (++nprinted == 1)
2018 *first_displayed_line = nlines;
2019 free(line);
2020 free(wline);
2021 wline = NULL;
2023 *last_displayed_line = nlines;
2025 view_vborder(view);
2027 if (*eof) {
2028 while (nprinted < view->nlines) {
2029 waddch(view->window, '\n');
2030 nprinted++;
2033 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2034 if (err) {
2035 return err;
2038 wstandout(view->window);
2039 waddwstr(view->window, wline);
2040 wstandend(view->window);
2043 return NULL;
2046 static char *
2047 get_datestr(time_t *time, char *datebuf)
2049 char *p, *s = ctime_r(time, datebuf);
2050 p = strchr(s, '\n');
2051 if (p)
2052 *p = '\0';
2053 return s;
2056 static const struct got_error *
2057 write_commit_info(struct got_object_id *commit_id,
2058 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2060 const struct got_error *err = NULL;
2061 char datebuf[26];
2062 struct got_commit_object *commit;
2063 char *id_str = NULL;
2064 time_t committer_time;
2065 const char *author, *committer;
2066 char *refs_str = NULL;
2068 if (refs) {
2069 err = build_refs_str(&refs_str, refs, commit_id);
2070 if (err)
2071 return err;
2074 err = got_object_open_as_commit(&commit, repo, commit_id);
2075 if (err)
2076 return err;
2078 err = got_object_id_str(&id_str, commit_id);
2079 if (err) {
2080 err = got_error_from_errno("got_object_id_str");
2081 goto done;
2084 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2085 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2086 err = got_error_from_errno("fprintf");
2087 goto done;
2089 if (fprintf(outfile, "from: %s\n",
2090 got_object_commit_get_author(commit)) < 0) {
2091 err = got_error_from_errno("fprintf");
2092 goto done;
2094 committer_time = got_object_commit_get_committer_time(commit);
2095 if (fprintf(outfile, "date: %s UTC\n",
2096 get_datestr(&committer_time, datebuf)) < 0) {
2097 err = got_error_from_errno("fprintf");
2098 goto done;
2100 author = got_object_commit_get_author(commit);
2101 committer = got_object_commit_get_committer(commit);
2102 if (strcmp(author, committer) != 0 &&
2103 fprintf(outfile, "via: %s\n", committer) < 0) {
2104 err = got_error_from_errno("fprintf");
2105 goto done;
2107 if (fprintf(outfile, "%s\n",
2108 got_object_commit_get_logmsg(commit)) < 0) {
2109 err = got_error_from_errno("fprintf");
2110 goto done;
2112 done:
2113 free(id_str);
2114 free(refs_str);
2115 got_object_commit_close(commit);
2116 return err;
2119 static const struct got_error *
2120 create_diff(struct tog_diff_view_state *s)
2122 const struct got_error *err = NULL;
2123 FILE *f = NULL;
2124 int obj_type;
2126 f = got_opentemp();
2127 if (f == NULL) {
2128 err = got_error_from_errno("got_opentemp");
2129 goto done;
2131 if (s->f && fclose(s->f) != 0) {
2132 err = got_error_from_errno("fclose");
2133 goto done;
2135 s->f = f;
2137 if (s->id1)
2138 err = got_object_get_type(&obj_type, s->repo, s->id1);
2139 else
2140 err = got_object_get_type(&obj_type, s->repo, s->id2);
2141 if (err)
2142 goto done;
2144 switch (obj_type) {
2145 case GOT_OBJ_TYPE_BLOB:
2146 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2147 s->diff_context, s->repo, f);
2148 break;
2149 case GOT_OBJ_TYPE_TREE:
2150 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2151 s->diff_context, s->repo, f);
2152 break;
2153 case GOT_OBJ_TYPE_COMMIT: {
2154 const struct got_object_id_queue *parent_ids;
2155 struct got_object_qid *pid;
2156 struct got_commit_object *commit2;
2158 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2159 if (err)
2160 break;
2161 /* Show commit info if we're diffing to a parent/root commit. */
2162 if (s->id1 == NULL)
2163 write_commit_info(s->id2, s->refs, s->repo, f);
2164 else {
2165 parent_ids = got_object_commit_get_parent_ids(commit2);
2166 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2167 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2168 write_commit_info(s->id2, s->refs,
2169 s->repo, f);
2170 break;
2174 got_object_commit_close(commit2);
2176 err = got_diff_objects_as_commits(s->id1, s->id2,
2177 s->diff_context, s->repo, f);
2178 break;
2180 default:
2181 err = got_error(GOT_ERR_OBJ_TYPE);
2182 break;
2184 done:
2185 if (f && fflush(f) != 0 && err == NULL)
2186 err = got_error_from_errno("fflush");
2187 return err;
2190 static void
2191 diff_view_indicate_progress(struct tog_view *view)
2193 mvwaddstr(view->window, 0, 0, "diffing...");
2194 update_panels();
2195 doupdate();
2198 static const struct got_error *
2199 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2200 struct got_object_id *id2, struct tog_view *log_view,
2201 struct got_reflist_head *refs, struct got_repository *repo)
2203 const struct got_error *err;
2205 if (id1 != NULL && id2 != NULL) {
2206 int type1, type2;
2207 err = got_object_get_type(&type1, repo, id1);
2208 if (err)
2209 return err;
2210 err = got_object_get_type(&type2, repo, id2);
2211 if (err)
2212 return err;
2214 if (type1 != type2)
2215 return got_error(GOT_ERR_OBJ_TYPE);
2218 if (id1) {
2219 view->state.diff.id1 = got_object_id_dup(id1);
2220 if (view->state.diff.id1 == NULL)
2221 return got_error_from_errno("got_object_id_dup");
2222 } else
2223 view->state.diff.id1 = NULL;
2225 view->state.diff.id2 = got_object_id_dup(id2);
2226 if (view->state.diff.id2 == NULL) {
2227 free(view->state.diff.id1);
2228 view->state.diff.id1 = NULL;
2229 return got_error_from_errno("got_object_id_dup");
2231 view->state.diff.f = NULL;
2232 view->state.diff.first_displayed_line = 1;
2233 view->state.diff.last_displayed_line = view->nlines;
2234 view->state.diff.diff_context = 3;
2235 view->state.diff.log_view = log_view;
2236 view->state.diff.repo = repo;
2237 view->state.diff.refs = refs;
2239 if (log_view && view_is_splitscreen(view))
2240 show_log_view(log_view); /* draw vborder */
2241 diff_view_indicate_progress(view);
2243 err = create_diff(&view->state.diff);
2244 if (err) {
2245 free(view->state.diff.id1);
2246 view->state.diff.id1 = NULL;
2247 free(view->state.diff.id2);
2248 view->state.diff.id2 = NULL;
2249 return err;
2252 view->show = show_diff_view;
2253 view->input = input_diff_view;
2254 view->close = close_diff_view;
2256 return NULL;
2259 static const struct got_error *
2260 close_diff_view(struct tog_view *view)
2262 const struct got_error *err = NULL;
2264 free(view->state.diff.id1);
2265 view->state.diff.id1 = NULL;
2266 free(view->state.diff.id2);
2267 view->state.diff.id2 = NULL;
2268 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2269 err = got_error_from_errno("fclose");
2270 return err;
2273 static const struct got_error *
2274 show_diff_view(struct tog_view *view)
2276 const struct got_error *err;
2277 struct tog_diff_view_state *s = &view->state.diff;
2278 char *id_str1 = NULL, *id_str2, *header;
2280 if (s->id1) {
2281 err = got_object_id_str(&id_str1, s->id1);
2282 if (err)
2283 return err;
2285 err = got_object_id_str(&id_str2, s->id2);
2286 if (err)
2287 return err;
2289 if (asprintf(&header, "diff %s %s",
2290 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2291 err = got_error_from_errno("asprintf");
2292 free(id_str1);
2293 free(id_str2);
2294 return err;
2296 free(id_str1);
2297 free(id_str2);
2299 return draw_file(view, s->f, &s->first_displayed_line,
2300 &s->last_displayed_line, &s->eof, view->nlines,
2301 header);
2304 static const struct got_error *
2305 set_selected_commit(struct tog_diff_view_state *s,
2306 struct commit_queue_entry *entry)
2308 const struct got_error *err;
2309 const struct got_object_id_queue *parent_ids;
2310 struct got_commit_object *selected_commit;
2311 struct got_object_qid *pid;
2313 free(s->id2);
2314 s->id2 = got_object_id_dup(entry->id);
2315 if (s->id2 == NULL)
2316 return got_error_from_errno("got_object_id_dup");
2318 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2319 if (err)
2320 return err;
2321 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2322 free(s->id1);
2323 pid = SIMPLEQ_FIRST(parent_ids);
2324 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2325 got_object_commit_close(selected_commit);
2326 return NULL;
2329 static const struct got_error *
2330 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2331 struct tog_view **focus_view, struct tog_view *view, int ch)
2333 const struct got_error *err = NULL;
2334 struct tog_diff_view_state *s = &view->state.diff;
2335 struct tog_log_view_state *ls;
2336 struct commit_queue_entry *entry;
2337 int i;
2339 switch (ch) {
2340 case 'k':
2341 case KEY_UP:
2342 if (s->first_displayed_line > 1)
2343 s->first_displayed_line--;
2344 break;
2345 case KEY_PPAGE:
2346 case CTRL('b'):
2347 if (s->first_displayed_line == 1)
2348 break;
2349 i = 0;
2350 while (i++ < view->nlines - 1 &&
2351 s->first_displayed_line > 1)
2352 s->first_displayed_line--;
2353 break;
2354 case 'j':
2355 case KEY_DOWN:
2356 if (!s->eof)
2357 s->first_displayed_line++;
2358 break;
2359 case KEY_NPAGE:
2360 case CTRL('f'):
2361 case ' ':
2362 if (s->eof)
2363 break;
2364 i = 0;
2365 while (!s->eof && i++ < view->nlines - 1) {
2366 char *line;
2367 line = parse_next_line(s->f, NULL);
2368 s->first_displayed_line++;
2369 if (line == NULL)
2370 break;
2372 break;
2373 case '[':
2374 if (s->diff_context > 0) {
2375 s->diff_context--;
2376 diff_view_indicate_progress(view);
2377 err = create_diff(s);
2379 break;
2380 case ']':
2381 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2382 s->diff_context++;
2383 diff_view_indicate_progress(view);
2384 err = create_diff(s);
2386 break;
2387 case '<':
2388 case ',':
2389 if (s->log_view == NULL)
2390 break;
2391 ls = &s->log_view->state.log;
2392 entry = TAILQ_PREV(ls->selected_entry,
2393 commit_queue_head, entry);
2394 if (entry == NULL)
2395 break;
2397 err = input_log_view(NULL, NULL, NULL, s->log_view,
2398 KEY_UP);
2399 if (err)
2400 break;
2402 err = set_selected_commit(s, entry);
2403 if (err)
2404 break;
2406 s->first_displayed_line = 1;
2407 s->last_displayed_line = view->nlines;
2409 diff_view_indicate_progress(view);
2410 err = create_diff(s);
2411 break;
2412 case '>':
2413 case '.':
2414 if (s->log_view == NULL)
2415 break;
2416 ls = &s->log_view->state.log;
2418 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2419 ls->thread_args.commits_needed++;
2421 /* Display "loading..." in log view. */
2422 show_log_view(s->log_view);
2423 update_panels();
2424 doupdate();
2426 err = trigger_log_thread(1 /* load_all */,
2427 &ls->thread_args.commits_needed,
2428 &ls->thread_args.log_complete,
2429 &ls->thread_args.need_commits);
2430 if (err)
2431 break;
2433 err = input_log_view(NULL, NULL, NULL, s->log_view,
2434 KEY_DOWN);
2435 if (err)
2436 break;
2438 entry = TAILQ_NEXT(ls->selected_entry, entry);
2439 if (entry == NULL)
2440 break;
2442 err = set_selected_commit(s, entry);
2443 if (err)
2444 break;
2446 s->first_displayed_line = 1;
2447 s->last_displayed_line = view->nlines;
2449 diff_view_indicate_progress(view);
2450 err = create_diff(s);
2451 break;
2452 default:
2453 break;
2456 return err;
2459 static const struct got_error *
2460 cmd_diff(int argc, char *argv[])
2462 const struct got_error *error = NULL;
2463 struct got_repository *repo = NULL;
2464 struct got_reflist_head refs;
2465 struct got_object_id *id1 = NULL, *id2 = NULL;
2466 char *repo_path = NULL;
2467 char *id_str1 = NULL, *id_str2 = NULL;
2468 int ch;
2469 struct tog_view *view;
2471 SIMPLEQ_INIT(&refs);
2473 #ifndef PROFILE
2474 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2475 NULL) == -1)
2476 err(1, "pledge");
2477 #endif
2479 while ((ch = getopt(argc, argv, "")) != -1) {
2480 switch (ch) {
2481 default:
2482 usage_diff();
2483 /* NOTREACHED */
2487 argc -= optind;
2488 argv += optind;
2490 if (argc == 0) {
2491 usage_diff(); /* TODO show local worktree changes */
2492 } else if (argc == 2) {
2493 repo_path = getcwd(NULL, 0);
2494 if (repo_path == NULL)
2495 return got_error_from_errno("getcwd");
2496 id_str1 = argv[0];
2497 id_str2 = argv[1];
2498 } else if (argc == 3) {
2499 repo_path = realpath(argv[0], NULL);
2500 if (repo_path == NULL)
2501 return got_error_from_errno2("realpath", argv[0]);
2502 id_str1 = argv[1];
2503 id_str2 = argv[2];
2504 } else
2505 usage_diff();
2507 init_curses();
2509 error = got_repo_open(&repo, repo_path);
2510 if (error)
2511 goto done;
2513 error = apply_unveil(got_repo_get_path(repo), NULL);
2514 if (error)
2515 goto done;
2517 error = got_object_resolve_id_str(&id1, repo, id_str1);
2518 if (error)
2519 goto done;
2521 error = got_object_resolve_id_str(&id2, repo, id_str2);
2522 if (error)
2523 goto done;
2525 error = got_ref_list(&refs, repo);
2526 if (error)
2527 goto done;
2529 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2530 if (view == NULL) {
2531 error = got_error_from_errno("view_open");
2532 goto done;
2534 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2535 if (error)
2536 goto done;
2537 error = view_loop(view);
2538 done:
2539 free(repo_path);
2540 got_repo_close(repo);
2541 got_ref_list_free(&refs);
2542 return error;
2545 __dead static void
2546 usage_blame(void)
2548 endwin();
2549 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2550 getprogname());
2551 exit(1);
2554 struct tog_blame_line {
2555 int annotated;
2556 struct got_object_id *id;
2559 static const struct got_error *
2560 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2561 const char *path, struct tog_blame_line *lines, int nlines,
2562 int blame_complete, int selected_line, int *first_displayed_line,
2563 int *last_displayed_line, int *eof, int max_lines)
2565 const struct got_error *err;
2566 int lineno = 0, nprinted = 0;
2567 char *line;
2568 size_t len;
2569 wchar_t *wline;
2570 int width, wlimit;
2571 struct tog_blame_line *blame_line;
2572 struct got_object_id *prev_id = NULL;
2573 char *id_str;
2575 err = got_object_id_str(&id_str, id);
2576 if (err)
2577 return err;
2579 rewind(f);
2580 werase(view->window);
2582 if (asprintf(&line, "commit %s", id_str) == -1) {
2583 err = got_error_from_errno("asprintf");
2584 free(id_str);
2585 return err;
2588 err = format_line(&wline, &width, line, view->ncols);
2589 free(line);
2590 line = NULL;
2591 if (view_needs_focus_indication(view))
2592 wstandout(view->window);
2593 waddwstr(view->window, wline);
2594 if (view_needs_focus_indication(view))
2595 wstandend(view->window);
2596 free(wline);
2597 wline = NULL;
2598 if (width < view->ncols)
2599 waddch(view->window, '\n');
2601 if (asprintf(&line, "[%d/%d] %s%s",
2602 *first_displayed_line - 1 + selected_line, nlines,
2603 blame_complete ? "" : "annotating... ", path) == -1) {
2604 free(id_str);
2605 return got_error_from_errno("asprintf");
2607 free(id_str);
2608 err = format_line(&wline, &width, line, view->ncols);
2609 free(line);
2610 line = NULL;
2611 if (err)
2612 return err;
2613 waddwstr(view->window, wline);
2614 free(wline);
2615 wline = NULL;
2616 if (width < view->ncols)
2617 waddch(view->window, '\n');
2619 *eof = 0;
2620 while (nprinted < max_lines - 2) {
2621 line = parse_next_line(f, &len);
2622 if (line == NULL) {
2623 *eof = 1;
2624 break;
2626 if (++lineno < *first_displayed_line) {
2627 free(line);
2628 continue;
2631 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2632 err = format_line(&wline, &width, line, wlimit);
2633 if (err) {
2634 free(line);
2635 return err;
2638 if (view->focussed && nprinted == selected_line - 1)
2639 wstandout(view->window);
2641 blame_line = &lines[lineno - 1];
2642 if (blame_line->annotated && prev_id &&
2643 got_object_id_cmp(prev_id, blame_line->id) == 0)
2644 waddstr(view->window, " ");
2645 else if (blame_line->annotated) {
2646 char *id_str;
2647 err = got_object_id_str(&id_str, blame_line->id);
2648 if (err) {
2649 free(line);
2650 free(wline);
2651 return err;
2653 wprintw(view->window, "%.8s ", id_str);
2654 free(id_str);
2655 prev_id = blame_line->id;
2656 } else {
2657 waddstr(view->window, "........ ");
2658 prev_id = NULL;
2661 waddwstr(view->window, wline);
2662 while (width < wlimit) {
2663 waddch(view->window, ' ');
2664 width++;
2666 if (view->focussed && nprinted == selected_line - 1)
2667 wstandend(view->window);
2668 if (++nprinted == 1)
2669 *first_displayed_line = lineno;
2670 free(line);
2671 free(wline);
2672 wline = NULL;
2674 *last_displayed_line = lineno;
2676 view_vborder(view);
2678 return NULL;
2681 static const struct got_error *
2682 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2684 const struct got_error *err = NULL;
2685 struct tog_blame_cb_args *a = arg;
2686 struct tog_blame_line *line;
2687 int errcode;
2689 if (nlines != a->nlines ||
2690 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2691 return got_error(GOT_ERR_RANGE);
2693 errcode = pthread_mutex_lock(&tog_mutex);
2694 if (errcode)
2695 return got_error_set_errno(errcode, "pthread_mutex_lock");
2697 if (*a->quit) { /* user has quit the blame view */
2698 err = got_error(GOT_ERR_ITER_COMPLETED);
2699 goto done;
2702 if (lineno == -1)
2703 goto done; /* no change in this commit */
2705 line = &a->lines[lineno - 1];
2706 if (line->annotated)
2707 goto done;
2709 line->id = got_object_id_dup(id);
2710 if (line->id == NULL) {
2711 err = got_error_from_errno("got_object_id_dup");
2712 goto done;
2714 line->annotated = 1;
2715 done:
2716 errcode = pthread_mutex_unlock(&tog_mutex);
2717 if (errcode)
2718 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2719 return err;
2722 static void *
2723 blame_thread(void *arg)
2725 const struct got_error *err;
2726 struct tog_blame_thread_args *ta = arg;
2727 struct tog_blame_cb_args *a = ta->cb_args;
2728 int errcode;
2730 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2731 blame_cb, ta->cb_args);
2733 errcode = pthread_mutex_lock(&tog_mutex);
2734 if (errcode)
2735 return (void *)got_error_set_errno(errcode,
2736 "pthread_mutex_lock");
2738 got_repo_close(ta->repo);
2739 ta->repo = NULL;
2740 *ta->complete = 1;
2742 errcode = pthread_mutex_unlock(&tog_mutex);
2743 if (errcode && err == NULL)
2744 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2746 return (void *)err;
2749 static struct got_object_id *
2750 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2751 int selected_line)
2753 struct tog_blame_line *line;
2755 line = &lines[first_displayed_line - 1 + selected_line - 1];
2756 if (!line->annotated)
2757 return NULL;
2759 return line->id;
2762 static const struct got_error *
2763 stop_blame(struct tog_blame *blame)
2765 const struct got_error *err = NULL;
2766 int i;
2768 if (blame->thread) {
2769 int errcode;
2770 errcode = pthread_mutex_unlock(&tog_mutex);
2771 if (errcode)
2772 return got_error_set_errno(errcode,
2773 "pthread_mutex_unlock");
2774 errcode = pthread_join(blame->thread, (void **)&err);
2775 if (errcode)
2776 return got_error_set_errno(errcode, "pthread_join");
2777 errcode = pthread_mutex_lock(&tog_mutex);
2778 if (errcode)
2779 return got_error_set_errno(errcode,
2780 "pthread_mutex_lock");
2781 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2782 err = NULL;
2783 blame->thread = NULL;
2785 if (blame->thread_args.repo) {
2786 got_repo_close(blame->thread_args.repo);
2787 blame->thread_args.repo = NULL;
2789 if (blame->f) {
2790 if (fclose(blame->f) != 0 && err == NULL)
2791 err = got_error_from_errno("fclose");
2792 blame->f = NULL;
2794 if (blame->lines) {
2795 for (i = 0; i < blame->nlines; i++)
2796 free(blame->lines[i].id);
2797 free(blame->lines);
2798 blame->lines = NULL;
2800 free(blame->cb_args.commit_id);
2801 blame->cb_args.commit_id = NULL;
2803 return err;
2806 static const struct got_error *
2807 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2808 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2809 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2810 struct got_repository *repo)
2812 const struct got_error *err = NULL;
2813 struct got_blob_object *blob = NULL;
2814 struct got_repository *thread_repo = NULL;
2815 struct got_object_id *obj_id = NULL;
2816 int obj_type;
2818 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2819 if (err)
2820 return err;
2821 if (obj_id == NULL)
2822 return got_error(GOT_ERR_NO_OBJ);
2824 err = got_object_get_type(&obj_type, repo, obj_id);
2825 if (err)
2826 goto done;
2828 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2829 err = got_error(GOT_ERR_OBJ_TYPE);
2830 goto done;
2833 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2834 if (err)
2835 goto done;
2836 blame->f = got_opentemp();
2837 if (blame->f == NULL) {
2838 err = got_error_from_errno("got_opentemp");
2839 goto done;
2841 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2842 blame->f, blob);
2843 if (err)
2844 goto done;
2846 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2847 if (blame->lines == NULL) {
2848 err = got_error_from_errno("calloc");
2849 goto done;
2852 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2853 if (err)
2854 goto done;
2856 blame->cb_args.view = view;
2857 blame->cb_args.lines = blame->lines;
2858 blame->cb_args.nlines = blame->nlines;
2859 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2860 if (blame->cb_args.commit_id == NULL) {
2861 err = got_error_from_errno("got_object_id_dup");
2862 goto done;
2864 blame->cb_args.quit = done;
2866 blame->thread_args.path = path;
2867 blame->thread_args.repo = thread_repo;
2868 blame->thread_args.cb_args = &blame->cb_args;
2869 blame->thread_args.complete = blame_complete;
2870 *blame_complete = 0;
2872 done:
2873 if (blob)
2874 got_object_blob_close(blob);
2875 free(obj_id);
2876 if (err)
2877 stop_blame(blame);
2878 return err;
2881 static const struct got_error *
2882 open_blame_view(struct tog_view *view, char *path,
2883 struct got_object_id *commit_id, struct got_reflist_head *refs,
2884 struct got_repository *repo)
2886 const struct got_error *err = NULL;
2887 struct tog_blame_view_state *s = &view->state.blame;
2889 SIMPLEQ_INIT(&s->blamed_commits);
2891 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2892 if (err)
2893 return err;
2895 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2896 s->first_displayed_line = 1;
2897 s->last_displayed_line = view->nlines;
2898 s->selected_line = 1;
2899 s->blame_complete = 0;
2900 s->path = path;
2901 if (s->path == NULL)
2902 return got_error_from_errno("open_blame_view");
2903 s->repo = repo;
2904 s->refs = refs;
2905 s->commit_id = commit_id;
2906 memset(&s->blame, 0, sizeof(s->blame));
2908 view->show = show_blame_view;
2909 view->input = input_blame_view;
2910 view->close = close_blame_view;
2912 return run_blame(&s->blame, view, &s->blame_complete,
2913 &s->first_displayed_line, &s->last_displayed_line,
2914 &s->selected_line, &s->done, &s->eof, s->path,
2915 s->blamed_commit->id, s->repo);
2918 static const struct got_error *
2919 close_blame_view(struct tog_view *view)
2921 const struct got_error *err = NULL;
2922 struct tog_blame_view_state *s = &view->state.blame;
2924 if (s->blame.thread)
2925 err = stop_blame(&s->blame);
2927 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2928 struct got_object_qid *blamed_commit;
2929 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2930 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2931 got_object_qid_free(blamed_commit);
2934 free(s->path);
2936 return err;
2939 static const struct got_error *
2940 show_blame_view(struct tog_view *view)
2942 const struct got_error *err = NULL;
2943 struct tog_blame_view_state *s = &view->state.blame;
2944 int errcode;
2946 if (s->blame.thread == NULL) {
2947 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2948 &s->blame.thread_args);
2949 if (errcode)
2950 return got_error_set_errno(errcode, "pthread_create");
2953 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2954 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2955 s->selected_line, &s->first_displayed_line,
2956 &s->last_displayed_line, &s->eof, view->nlines);
2958 view_vborder(view);
2959 return err;
2962 static const struct got_error *
2963 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2964 struct tog_view **focus_view, struct tog_view *view, int ch)
2966 const struct got_error *err = NULL, *thread_err = NULL;
2967 struct tog_view *diff_view;
2968 struct tog_blame_view_state *s = &view->state.blame;
2969 int begin_x = 0;
2971 switch (ch) {
2972 case 'q':
2973 s->done = 1;
2974 break;
2975 case 'k':
2976 case KEY_UP:
2977 if (s->selected_line > 1)
2978 s->selected_line--;
2979 else if (s->selected_line == 1 &&
2980 s->first_displayed_line > 1)
2981 s->first_displayed_line--;
2982 break;
2983 case KEY_PPAGE:
2984 if (s->first_displayed_line == 1) {
2985 s->selected_line = 1;
2986 break;
2988 if (s->first_displayed_line > view->nlines - 2)
2989 s->first_displayed_line -=
2990 (view->nlines - 2);
2991 else
2992 s->first_displayed_line = 1;
2993 break;
2994 case 'j':
2995 case KEY_DOWN:
2996 if (s->selected_line < view->nlines - 2 &&
2997 s->first_displayed_line +
2998 s->selected_line <= s->blame.nlines)
2999 s->selected_line++;
3000 else if (s->last_displayed_line <
3001 s->blame.nlines)
3002 s->first_displayed_line++;
3003 break;
3004 case 'b':
3005 case 'p': {
3006 struct got_object_id *id = NULL;
3007 id = get_selected_commit_id(s->blame.lines,
3008 s->first_displayed_line, s->selected_line);
3009 if (id == NULL)
3010 break;
3011 if (ch == 'p') {
3012 struct got_commit_object *commit;
3013 struct got_object_qid *pid;
3014 struct got_object_id *blob_id = NULL;
3015 int obj_type;
3016 err = got_object_open_as_commit(&commit,
3017 s->repo, id);
3018 if (err)
3019 break;
3020 pid = SIMPLEQ_FIRST(
3021 got_object_commit_get_parent_ids(commit));
3022 if (pid == NULL) {
3023 got_object_commit_close(commit);
3024 break;
3026 /* Check if path history ends here. */
3027 err = got_object_id_by_path(&blob_id, s->repo,
3028 pid->id, s->path);
3029 if (err) {
3030 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3031 err = NULL;
3032 got_object_commit_close(commit);
3033 break;
3035 err = got_object_get_type(&obj_type, s->repo,
3036 blob_id);
3037 free(blob_id);
3038 /* Can't blame non-blob type objects. */
3039 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3040 got_object_commit_close(commit);
3041 break;
3043 err = got_object_qid_alloc(&s->blamed_commit,
3044 pid->id);
3045 got_object_commit_close(commit);
3046 } else {
3047 if (got_object_id_cmp(id,
3048 s->blamed_commit->id) == 0)
3049 break;
3050 err = got_object_qid_alloc(&s->blamed_commit,
3051 id);
3053 if (err)
3054 break;
3055 s->done = 1;
3056 thread_err = stop_blame(&s->blame);
3057 s->done = 0;
3058 if (thread_err)
3059 break;
3060 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3061 s->blamed_commit, entry);
3062 err = run_blame(&s->blame, view, &s->blame_complete,
3063 &s->first_displayed_line, &s->last_displayed_line,
3064 &s->selected_line, &s->done, &s->eof,
3065 s->path, s->blamed_commit->id, s->repo);
3066 if (err)
3067 break;
3068 break;
3070 case 'B': {
3071 struct got_object_qid *first;
3072 first = SIMPLEQ_FIRST(&s->blamed_commits);
3073 if (!got_object_id_cmp(first->id, s->commit_id))
3074 break;
3075 s->done = 1;
3076 thread_err = stop_blame(&s->blame);
3077 s->done = 0;
3078 if (thread_err)
3079 break;
3080 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3081 got_object_qid_free(s->blamed_commit);
3082 s->blamed_commit =
3083 SIMPLEQ_FIRST(&s->blamed_commits);
3084 err = run_blame(&s->blame, view, &s->blame_complete,
3085 &s->first_displayed_line, &s->last_displayed_line,
3086 &s->selected_line, &s->done, &s->eof, s->path,
3087 s->blamed_commit->id, s->repo);
3088 if (err)
3089 break;
3090 break;
3092 case KEY_ENTER:
3093 case '\r': {
3094 struct got_object_id *id = NULL;
3095 struct got_object_qid *pid;
3096 struct got_commit_object *commit = NULL;
3097 id = get_selected_commit_id(s->blame.lines,
3098 s->first_displayed_line, s->selected_line);
3099 if (id == NULL)
3100 break;
3101 err = got_object_open_as_commit(&commit, s->repo, id);
3102 if (err)
3103 break;
3104 pid = SIMPLEQ_FIRST(
3105 got_object_commit_get_parent_ids(commit));
3106 if (view_is_parent_view(view))
3107 begin_x = view_split_begin_x(view->begin_x);
3108 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3109 if (diff_view == NULL) {
3110 got_object_commit_close(commit);
3111 err = got_error_from_errno("view_open");
3112 break;
3114 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3115 id, NULL, s->refs, s->repo);
3116 got_object_commit_close(commit);
3117 if (err) {
3118 view_close(diff_view);
3119 break;
3121 if (view_is_parent_view(view)) {
3122 err = view_close_child(view);
3123 if (err)
3124 break;
3125 err = view_set_child(view, diff_view);
3126 if (err) {
3127 view_close(diff_view);
3128 break;
3130 *focus_view = diff_view;
3131 view->child_focussed = 1;
3132 } else
3133 *new_view = diff_view;
3134 if (err)
3135 break;
3136 break;
3138 case KEY_NPAGE:
3139 case ' ':
3140 if (s->last_displayed_line >= s->blame.nlines &&
3141 s->selected_line >= MIN(s->blame.nlines,
3142 view->nlines - 2)) {
3143 break;
3145 if (s->last_displayed_line >= s->blame.nlines &&
3146 s->selected_line < view->nlines - 2) {
3147 s->selected_line = MIN(s->blame.nlines,
3148 view->nlines - 2);
3149 break;
3151 if (s->last_displayed_line + view->nlines - 2
3152 <= s->blame.nlines)
3153 s->first_displayed_line +=
3154 view->nlines - 2;
3155 else
3156 s->first_displayed_line =
3157 s->blame.nlines -
3158 (view->nlines - 3);
3159 break;
3160 case KEY_RESIZE:
3161 if (s->selected_line > view->nlines - 2) {
3162 s->selected_line = MIN(s->blame.nlines,
3163 view->nlines - 2);
3165 break;
3166 default:
3167 break;
3169 return thread_err ? thread_err : err;
3172 static const struct got_error *
3173 cmd_blame(int argc, char *argv[])
3175 const struct got_error *error;
3176 struct got_repository *repo = NULL;
3177 struct got_reflist_head refs;
3178 struct got_worktree *worktree = NULL;
3179 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3180 struct got_object_id *commit_id = NULL;
3181 char *commit_id_str = NULL;
3182 int ch;
3183 struct tog_view *view;
3185 SIMPLEQ_INIT(&refs);
3187 #ifndef PROFILE
3188 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3189 NULL) == -1)
3190 err(1, "pledge");
3191 #endif
3193 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3194 switch (ch) {
3195 case 'c':
3196 commit_id_str = optarg;
3197 break;
3198 case 'r':
3199 repo_path = realpath(optarg, NULL);
3200 if (repo_path == NULL)
3201 err(1, "-r option");
3202 break;
3203 default:
3204 usage_blame();
3205 /* NOTREACHED */
3209 argc -= optind;
3210 argv += optind;
3212 if (argc == 1)
3213 path = argv[0];
3214 else
3215 usage_blame();
3217 cwd = getcwd(NULL, 0);
3218 if (cwd == NULL) {
3219 error = got_error_from_errno("getcwd");
3220 goto done;
3222 if (repo_path == NULL) {
3223 error = got_worktree_open(&worktree, cwd);
3224 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3225 goto done;
3226 else
3227 error = NULL;
3228 if (worktree) {
3229 repo_path =
3230 strdup(got_worktree_get_repo_path(worktree));
3231 if (repo_path == NULL)
3232 error = got_error_from_errno("strdup");
3233 if (error)
3234 goto done;
3235 } else {
3236 repo_path = strdup(cwd);
3237 if (repo_path == NULL) {
3238 error = got_error_from_errno("strdup");
3239 goto done;
3244 init_curses();
3246 error = got_repo_open(&repo, repo_path);
3247 if (error != NULL)
3248 goto done;
3250 error = apply_unveil(got_repo_get_path(repo), NULL);
3251 if (error)
3252 goto done;
3254 if (worktree) {
3255 const char *prefix = got_worktree_get_path_prefix(worktree);
3256 char *p, *worktree_subdir = cwd +
3257 strlen(got_worktree_get_root_path(worktree));
3258 if (asprintf(&p, "%s%s%s%s%s",
3259 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3260 worktree_subdir, worktree_subdir[0] ? "/" : "",
3261 path) == -1) {
3262 error = got_error_from_errno("asprintf");
3263 goto done;
3265 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3266 free(p);
3267 } else {
3268 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3270 if (error)
3271 goto done;
3273 if (commit_id_str == NULL) {
3274 struct got_reference *head_ref;
3275 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3276 if (error != NULL)
3277 goto done;
3278 error = got_ref_resolve(&commit_id, repo, head_ref);
3279 got_ref_close(head_ref);
3280 } else {
3281 error = got_object_resolve_id_str(&commit_id, repo,
3282 commit_id_str);
3284 if (error != NULL)
3285 goto done;
3287 error = got_ref_list(&refs, repo);
3288 if (error)
3289 goto done;
3291 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3292 if (view == NULL) {
3293 error = got_error_from_errno("view_open");
3294 goto done;
3296 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3297 if (error)
3298 goto done;
3299 error = view_loop(view);
3300 done:
3301 free(repo_path);
3302 free(cwd);
3303 free(commit_id);
3304 if (worktree)
3305 got_worktree_close(worktree);
3306 if (repo)
3307 got_repo_close(repo);
3308 got_ref_list_free(&refs);
3309 return error;
3312 static const struct got_error *
3313 draw_tree_entries(struct tog_view *view,
3314 struct got_tree_entry **first_displayed_entry,
3315 struct got_tree_entry **last_displayed_entry,
3316 struct got_tree_entry **selected_entry, int *ndisplayed,
3317 const char *label, int show_ids, const char *parent_path,
3318 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3320 const struct got_error *err = NULL;
3321 struct got_tree_entry *te;
3322 wchar_t *wline;
3323 int width, n;
3325 *ndisplayed = 0;
3327 werase(view->window);
3329 if (limit == 0)
3330 return NULL;
3332 err = format_line(&wline, &width, label, view->ncols);
3333 if (err)
3334 return err;
3335 if (view_needs_focus_indication(view))
3336 wstandout(view->window);
3337 waddwstr(view->window, wline);
3338 if (view_needs_focus_indication(view))
3339 wstandend(view->window);
3340 free(wline);
3341 wline = NULL;
3342 if (width < view->ncols)
3343 waddch(view->window, '\n');
3344 if (--limit <= 0)
3345 return NULL;
3346 err = format_line(&wline, &width, parent_path, view->ncols);
3347 if (err)
3348 return err;
3349 waddwstr(view->window, wline);
3350 free(wline);
3351 wline = NULL;
3352 if (width < view->ncols)
3353 waddch(view->window, '\n');
3354 if (--limit <= 0)
3355 return NULL;
3356 waddch(view->window, '\n');
3357 if (--limit <= 0)
3358 return NULL;
3360 te = SIMPLEQ_FIRST(&entries->head);
3361 if (*first_displayed_entry == NULL) {
3362 if (selected == 0) {
3363 if (view->focussed)
3364 wstandout(view->window);
3365 *selected_entry = NULL;
3367 waddstr(view->window, " ..\n"); /* parent directory */
3368 if (selected == 0 && view->focussed)
3369 wstandend(view->window);
3370 (*ndisplayed)++;
3371 if (--limit <= 0)
3372 return NULL;
3373 n = 1;
3374 } else {
3375 n = 0;
3376 while (te != *first_displayed_entry)
3377 te = SIMPLEQ_NEXT(te, entry);
3380 while (te) {
3381 char *line = NULL, *id_str = NULL;
3383 if (show_ids) {
3384 err = got_object_id_str(&id_str, te->id);
3385 if (err)
3386 return got_error_from_errno(
3387 "got_object_id_str");
3389 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3390 te->name, S_ISDIR(te->mode) ? "/" :
3391 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3392 free(id_str);
3393 return got_error_from_errno("asprintf");
3395 free(id_str);
3396 err = format_line(&wline, &width, line, view->ncols);
3397 if (err) {
3398 free(line);
3399 break;
3401 if (n == selected) {
3402 if (view->focussed)
3403 wstandout(view->window);
3404 *selected_entry = te;
3406 waddwstr(view->window, wline);
3407 if (width < view->ncols)
3408 waddch(view->window, '\n');
3409 if (n == selected && view->focussed)
3410 wstandend(view->window);
3411 free(line);
3412 free(wline);
3413 wline = NULL;
3414 n++;
3415 (*ndisplayed)++;
3416 *last_displayed_entry = te;
3417 if (--limit <= 0)
3418 break;
3419 te = SIMPLEQ_NEXT(te, entry);
3422 return err;
3425 static void
3426 tree_scroll_up(struct tog_view *view,
3427 struct got_tree_entry **first_displayed_entry, int maxscroll,
3428 const struct got_tree_entries *entries, int isroot)
3430 struct got_tree_entry *te, *prev;
3431 int i;
3433 if (*first_displayed_entry == NULL)
3434 return;
3436 te = SIMPLEQ_FIRST(&entries->head);
3437 if (*first_displayed_entry == te) {
3438 if (!isroot)
3439 *first_displayed_entry = NULL;
3440 return;
3443 /* XXX this is stupid... switch to TAILQ? */
3444 for (i = 0; i < maxscroll; i++) {
3445 while (te != *first_displayed_entry) {
3446 prev = te;
3447 te = SIMPLEQ_NEXT(te, entry);
3449 *first_displayed_entry = prev;
3450 te = SIMPLEQ_FIRST(&entries->head);
3452 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3453 *first_displayed_entry = NULL;
3456 static int
3457 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3458 struct got_tree_entry *last_displayed_entry,
3459 const struct got_tree_entries *entries)
3461 struct got_tree_entry *next, *last;
3462 int n = 0;
3464 if (*first_displayed_entry)
3465 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3466 else
3467 next = SIMPLEQ_FIRST(&entries->head);
3468 last = last_displayed_entry;
3469 while (next && last && n++ < maxscroll) {
3470 last = SIMPLEQ_NEXT(last, entry);
3471 if (last) {
3472 *first_displayed_entry = next;
3473 next = SIMPLEQ_NEXT(next, entry);
3476 return n;
3479 static const struct got_error *
3480 tree_entry_path(char **path, struct tog_parent_trees *parents,
3481 struct got_tree_entry *te)
3483 const struct got_error *err = NULL;
3484 struct tog_parent_tree *pt;
3485 size_t len = 2; /* for leading slash and NUL */
3487 TAILQ_FOREACH(pt, parents, entry)
3488 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3489 if (te)
3490 len += strlen(te->name);
3492 *path = calloc(1, len);
3493 if (path == NULL)
3494 return got_error_from_errno("calloc");
3496 (*path)[0] = '/';
3497 pt = TAILQ_LAST(parents, tog_parent_trees);
3498 while (pt) {
3499 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3500 err = got_error(GOT_ERR_NO_SPACE);
3501 goto done;
3503 if (strlcat(*path, "/", len) >= len) {
3504 err = got_error(GOT_ERR_NO_SPACE);
3505 goto done;
3507 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3509 if (te) {
3510 if (strlcat(*path, te->name, len) >= len) {
3511 err = got_error(GOT_ERR_NO_SPACE);
3512 goto done;
3515 done:
3516 if (err) {
3517 free(*path);
3518 *path = NULL;
3520 return err;
3523 static const struct got_error *
3524 blame_tree_entry(struct tog_view **new_view, int begin_x,
3525 struct got_tree_entry *te, struct tog_parent_trees *parents,
3526 struct got_object_id *commit_id, struct got_reflist_head *refs,
3527 struct got_repository *repo)
3529 const struct got_error *err = NULL;
3530 char *path;
3531 struct tog_view *blame_view;
3533 err = tree_entry_path(&path, parents, te);
3534 if (err)
3535 return err;
3537 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3538 if (blame_view == NULL)
3539 return got_error_from_errno("view_open");
3541 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3542 if (err) {
3543 view_close(blame_view);
3544 free(path);
3545 } else
3546 *new_view = blame_view;
3547 return err;
3550 static const struct got_error *
3551 log_tree_entry(struct tog_view **new_view, int begin_x,
3552 struct got_tree_entry *te, struct tog_parent_trees *parents,
3553 struct got_object_id *commit_id, struct got_reflist_head *refs,
3554 struct got_repository *repo)
3556 struct tog_view *log_view;
3557 const struct got_error *err = NULL;
3558 char *path;
3560 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3561 if (log_view == NULL)
3562 return got_error_from_errno("view_open");
3564 err = tree_entry_path(&path, parents, te);
3565 if (err)
3566 return err;
3568 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3569 if (err)
3570 view_close(log_view);
3571 else
3572 *new_view = log_view;
3573 free(path);
3574 return err;
3577 static const struct got_error *
3578 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3579 struct got_object_id *commit_id, struct got_reflist_head *refs,
3580 struct got_repository *repo)
3582 const struct got_error *err = NULL;
3583 char *commit_id_str = NULL;
3584 struct tog_tree_view_state *s = &view->state.tree;
3586 TAILQ_INIT(&s->parents);
3588 err = got_object_id_str(&commit_id_str, commit_id);
3589 if (err != NULL)
3590 goto done;
3592 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3593 err = got_error_from_errno("asprintf");
3594 goto done;
3597 s->root = s->tree = root;
3598 s->entries = got_object_tree_get_entries(root);
3599 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3600 s->commit_id = got_object_id_dup(commit_id);
3601 if (s->commit_id == NULL) {
3602 err = got_error_from_errno("got_object_id_dup");
3603 goto done;
3605 s->refs = refs;
3606 s->repo = repo;
3608 view->show = show_tree_view;
3609 view->input = input_tree_view;
3610 view->close = close_tree_view;
3611 done:
3612 free(commit_id_str);
3613 if (err) {
3614 free(s->tree_label);
3615 s->tree_label = NULL;
3617 return err;
3620 static const struct got_error *
3621 close_tree_view(struct tog_view *view)
3623 struct tog_tree_view_state *s = &view->state.tree;
3625 free(s->tree_label);
3626 s->tree_label = NULL;
3627 free(s->commit_id);
3628 s->commit_id = NULL;
3629 while (!TAILQ_EMPTY(&s->parents)) {
3630 struct tog_parent_tree *parent;
3631 parent = TAILQ_FIRST(&s->parents);
3632 TAILQ_REMOVE(&s->parents, parent, entry);
3633 free(parent);
3636 if (s->tree != s->root)
3637 got_object_tree_close(s->tree);
3638 got_object_tree_close(s->root);
3640 return NULL;
3643 static const struct got_error *
3644 show_tree_view(struct tog_view *view)
3646 const struct got_error *err = NULL;
3647 struct tog_tree_view_state *s = &view->state.tree;
3648 char *parent_path;
3650 err = tree_entry_path(&parent_path, &s->parents, NULL);
3651 if (err)
3652 return err;
3654 err = draw_tree_entries(view, &s->first_displayed_entry,
3655 &s->last_displayed_entry, &s->selected_entry,
3656 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3657 s->entries, s->selected, view->nlines, s->tree == s->root);
3658 free(parent_path);
3660 view_vborder(view);
3661 return err;
3664 static const struct got_error *
3665 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3666 struct tog_view **focus_view, struct tog_view *view, int ch)
3668 const struct got_error *err = NULL;
3669 struct tog_tree_view_state *s = &view->state.tree;
3670 struct tog_view *log_view;
3671 int begin_x = 0, nscrolled;
3673 switch (ch) {
3674 case 'i':
3675 s->show_ids = !s->show_ids;
3676 break;
3677 case 'l':
3678 if (!s->selected_entry)
3679 break;
3680 if (view_is_parent_view(view))
3681 begin_x = view_split_begin_x(view->begin_x);
3682 err = log_tree_entry(&log_view, begin_x,
3683 s->selected_entry, &s->parents,
3684 s->commit_id, s->refs, s->repo);
3685 if (view_is_parent_view(view)) {
3686 err = view_close_child(view);
3687 if (err)
3688 return err;
3689 err = view_set_child(view, log_view);
3690 if (err) {
3691 view_close(log_view);
3692 break;
3694 *focus_view = log_view;
3695 view->child_focussed = 1;
3696 } else
3697 *new_view = log_view;
3698 break;
3699 case 'k':
3700 case KEY_UP:
3701 if (s->selected > 0) {
3702 s->selected--;
3703 if (s->selected == 0)
3704 break;
3706 if (s->selected > 0)
3707 break;
3708 tree_scroll_up(view, &s->first_displayed_entry, 1,
3709 s->entries, s->tree == s->root);
3710 break;
3711 case KEY_PPAGE:
3712 tree_scroll_up(view, &s->first_displayed_entry,
3713 MAX(0, view->nlines - 4 - s->selected), s->entries,
3714 s->tree == s->root);
3715 s->selected = 0;
3716 if (SIMPLEQ_FIRST(&s->entries->head) ==
3717 s->first_displayed_entry && s->tree != s->root)
3718 s->first_displayed_entry = NULL;
3719 break;
3720 case 'j':
3721 case KEY_DOWN:
3722 if (s->selected < s->ndisplayed - 1) {
3723 s->selected++;
3724 break;
3726 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3727 /* can't scroll any further */
3728 break;
3729 tree_scroll_down(&s->first_displayed_entry, 1,
3730 s->last_displayed_entry, s->entries);
3731 break;
3732 case KEY_NPAGE:
3733 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3734 == NULL) {
3735 /* can't scroll any further; move cursor down */
3736 if (s->selected < s->ndisplayed - 1)
3737 s->selected = s->ndisplayed - 1;
3738 break;
3740 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3741 view->nlines, s->last_displayed_entry, s->entries);
3742 if (nscrolled < view->nlines) {
3743 int ndisplayed = 0;
3744 struct got_tree_entry *te;
3745 te = s->first_displayed_entry;
3746 do {
3747 ndisplayed++;
3748 te = SIMPLEQ_NEXT(te, entry);
3749 } while (te);
3750 s->selected = ndisplayed - 1;
3752 break;
3753 case KEY_ENTER:
3754 case '\r':
3755 case KEY_BACKSPACE:
3756 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3757 struct tog_parent_tree *parent;
3758 /* user selected '..' */
3759 if (s->tree == s->root)
3760 break;
3761 parent = TAILQ_FIRST(&s->parents);
3762 TAILQ_REMOVE(&s->parents, parent,
3763 entry);
3764 got_object_tree_close(s->tree);
3765 s->tree = parent->tree;
3766 s->entries =
3767 got_object_tree_get_entries(s->tree);
3768 s->first_displayed_entry =
3769 parent->first_displayed_entry;
3770 s->selected_entry =
3771 parent->selected_entry;
3772 s->selected = parent->selected;
3773 free(parent);
3774 } else if (S_ISDIR(s->selected_entry->mode)) {
3775 struct tog_parent_tree *parent;
3776 struct got_tree_object *child;
3777 err = got_object_open_as_tree(&child,
3778 s->repo, s->selected_entry->id);
3779 if (err)
3780 break;
3781 parent = calloc(1, sizeof(*parent));
3782 if (parent == NULL) {
3783 err = got_error_from_errno("calloc");
3784 break;
3786 parent->tree = s->tree;
3787 parent->first_displayed_entry =
3788 s->first_displayed_entry;
3789 parent->selected_entry = s->selected_entry;
3790 parent->selected = s->selected;
3791 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3792 s->tree = child;
3793 s->entries =
3794 got_object_tree_get_entries(s->tree);
3795 s->selected = 0;
3796 s->first_displayed_entry = NULL;
3797 } else if (S_ISREG(s->selected_entry->mode)) {
3798 struct tog_view *blame_view;
3799 int begin_x = view_is_parent_view(view) ?
3800 view_split_begin_x(view->begin_x) : 0;
3802 err = blame_tree_entry(&blame_view, begin_x,
3803 s->selected_entry, &s->parents,
3804 s->commit_id, s->refs, s->repo);
3805 if (err)
3806 break;
3807 if (view_is_parent_view(view)) {
3808 err = view_close_child(view);
3809 if (err)
3810 return err;
3811 err = view_set_child(view, blame_view);
3812 if (err) {
3813 view_close(blame_view);
3814 break;
3816 *focus_view = blame_view;
3817 view->child_focussed = 1;
3818 } else
3819 *new_view = blame_view;
3821 break;
3822 case KEY_RESIZE:
3823 if (s->selected > view->nlines)
3824 s->selected = s->ndisplayed - 1;
3825 break;
3826 default:
3827 break;
3830 return err;
3833 __dead static void
3834 usage_tree(void)
3836 endwin();
3837 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3838 getprogname());
3839 exit(1);
3842 static const struct got_error *
3843 cmd_tree(int argc, char *argv[])
3845 const struct got_error *error;
3846 struct got_repository *repo = NULL;
3847 struct got_reflist_head refs;
3848 char *repo_path = NULL;
3849 struct got_object_id *commit_id = NULL;
3850 char *commit_id_arg = NULL;
3851 struct got_commit_object *commit = NULL;
3852 struct got_tree_object *tree = NULL;
3853 int ch;
3854 struct tog_view *view;
3856 SIMPLEQ_INIT(&refs);
3858 #ifndef PROFILE
3859 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3860 NULL) == -1)
3861 err(1, "pledge");
3862 #endif
3864 while ((ch = getopt(argc, argv, "c:")) != -1) {
3865 switch (ch) {
3866 case 'c':
3867 commit_id_arg = optarg;
3868 break;
3869 default:
3870 usage_tree();
3871 /* NOTREACHED */
3875 argc -= optind;
3876 argv += optind;
3878 if (argc == 0) {
3879 struct got_worktree *worktree;
3880 char *cwd = getcwd(NULL, 0);
3881 if (cwd == NULL)
3882 return got_error_from_errno("getcwd");
3883 error = got_worktree_open(&worktree, cwd);
3884 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3885 goto done;
3886 if (worktree) {
3887 free(cwd);
3888 repo_path =
3889 strdup(got_worktree_get_repo_path(worktree));
3890 got_worktree_close(worktree);
3891 } else
3892 repo_path = cwd;
3893 if (repo_path == NULL) {
3894 error = got_error_from_errno("strdup");
3895 goto done;
3897 } else if (argc == 1) {
3898 repo_path = realpath(argv[0], NULL);
3899 if (repo_path == NULL)
3900 return got_error_from_errno2("realpath", argv[0]);
3901 } else
3902 usage_log();
3904 init_curses();
3906 error = got_repo_open(&repo, repo_path);
3907 if (error != NULL)
3908 goto done;
3910 error = apply_unveil(got_repo_get_path(repo), NULL);
3911 if (error)
3912 goto done;
3914 if (commit_id_arg == NULL)
3915 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
3916 else
3917 error = got_object_resolve_id_str(&commit_id, repo,
3918 commit_id_arg);
3919 if (error != NULL)
3920 goto done;
3922 error = got_object_open_as_commit(&commit, repo, commit_id);
3923 if (error != NULL)
3924 goto done;
3926 error = got_object_open_as_tree(&tree, repo,
3927 got_object_commit_get_tree_id(commit));
3928 if (error != NULL)
3929 goto done;
3931 error = got_ref_list(&refs, repo);
3932 if (error)
3933 goto done;
3935 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3936 if (view == NULL) {
3937 error = got_error_from_errno("view_open");
3938 goto done;
3940 error = open_tree_view(view, tree, commit_id, &refs, repo);
3941 if (error)
3942 goto done;
3943 error = view_loop(view);
3944 done:
3945 free(repo_path);
3946 free(commit_id);
3947 if (commit)
3948 got_object_commit_close(commit);
3949 if (tree)
3950 got_object_tree_close(tree);
3951 if (repo)
3952 got_repo_close(repo);
3953 got_ref_list_free(&refs);
3954 return error;
3957 __dead static void
3958 usage(void)
3960 int i;
3962 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3963 "Available commands:\n", getprogname());
3964 for (i = 0; i < nitems(tog_commands); i++) {
3965 struct tog_cmd *cmd = &tog_commands[i];
3966 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3968 exit(1);
3971 static char **
3972 make_argv(const char *arg0, const char *arg1)
3974 char **argv;
3975 int argc = (arg1 == NULL ? 1 : 2);
3977 argv = calloc(argc, sizeof(char *));
3978 if (argv == NULL)
3979 err(1, "calloc");
3980 argv[0] = strdup(arg0);
3981 if (argv[0] == NULL)
3982 err(1, "calloc");
3983 if (arg1) {
3984 argv[1] = strdup(arg1);
3985 if (argv[1] == NULL)
3986 err(1, "calloc");
3989 return argv;
3992 int
3993 main(int argc, char *argv[])
3995 const struct got_error *error = NULL;
3996 struct tog_cmd *cmd = NULL;
3997 int ch, hflag = 0;
3998 char **cmd_argv = NULL;
4000 setlocale(LC_CTYPE, "");
4002 while ((ch = getopt(argc, argv, "h")) != -1) {
4003 switch (ch) {
4004 case 'h':
4005 hflag = 1;
4006 break;
4007 default:
4008 usage();
4009 /* NOTREACHED */
4013 argc -= optind;
4014 argv += optind;
4015 optind = 0;
4016 optreset = 1;
4018 if (argc == 0) {
4019 if (hflag)
4020 usage();
4021 /* Build an argument vector which runs a default command. */
4022 cmd = &tog_commands[0];
4023 cmd_argv = make_argv(cmd->name, NULL);
4024 argc = 1;
4025 } else {
4026 int i;
4028 /* Did the user specific a command? */
4029 for (i = 0; i < nitems(tog_commands); i++) {
4030 if (strncmp(tog_commands[i].name, argv[0],
4031 strlen(argv[0])) == 0) {
4032 cmd = &tog_commands[i];
4033 if (hflag)
4034 tog_commands[i].cmd_usage();
4035 break;
4038 if (cmd == NULL) {
4039 /* Did the user specify a repository? */
4040 char *repo_path = realpath(argv[0], NULL);
4041 if (repo_path) {
4042 struct got_repository *repo;
4043 error = got_repo_open(&repo, repo_path);
4044 if (error == NULL)
4045 got_repo_close(repo);
4046 } else
4047 error = got_error_from_errno2("realpath",
4048 argv[0]);
4049 if (error) {
4050 if (hflag) {
4051 fprintf(stderr, "%s: '%s' is not a "
4052 "known command\n", getprogname(),
4053 argv[0]);
4054 usage();
4056 fprintf(stderr, "%s: '%s' is neither a known "
4057 "command nor a path to a repository\n",
4058 getprogname(), argv[0]);
4059 free(repo_path);
4060 return 1;
4062 cmd = &tog_commands[0];
4063 cmd_argv = make_argv(cmd->name, repo_path);
4064 argc = 2;
4065 free(repo_path);
4069 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4070 if (error)
4071 goto done;
4072 done:
4073 endwin();
4074 free(cmd_argv);
4075 if (error)
4076 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4077 return 0;