Blob


1 /*
2 * Copyright (c) 2018 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"
50 #ifndef MIN
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 #endif
54 #ifndef MAX
55 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
56 #endif
59 #ifndef nitems
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 #endif
63 struct tog_cmd {
64 const char *name;
65 const struct got_error *(*cmd_main)(int, char *[]);
66 void (*cmd_usage)(void);
67 const char *descr;
68 };
70 __dead static void usage(void);
71 __dead static void usage_log(void);
72 __dead static void usage_diff(void);
73 __dead static void usage_blame(void);
74 __dead static void usage_tree(void);
76 static const struct got_error* cmd_log(int, char *[]);
77 static const struct got_error* cmd_diff(int, char *[]);
78 static const struct got_error* cmd_blame(int, char *[]);
79 static const struct got_error* cmd_tree(int, char *[]);
81 static struct tog_cmd tog_commands[] = {
82 { "log", cmd_log, usage_log,
83 "show repository history" },
84 { "diff", cmd_diff, usage_diff,
85 "compare files and directories" },
86 { "blame", cmd_blame, usage_blame,
87 "show line-by-line file history" },
88 { "tree", cmd_tree, usage_tree,
89 "browse trees in repository" },
90 };
92 enum tog_view_type {
93 TOG_VIEW_DIFF,
94 TOG_VIEW_LOG,
95 TOG_VIEW_BLAME,
96 TOG_VIEW_TREE
97 };
99 struct tog_diff_view_state {
100 struct got_object_id *id1, *id2;
101 FILE *f;
102 int first_displayed_line;
103 int last_displayed_line;
104 int eof;
105 int diff_context;
106 struct got_repository *repo;
107 };
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
123 struct tog_log_thread_args {
124 pthread_cond_t need_commits;
125 int commits_needed;
126 struct got_commit_graph *graph;
127 struct commit_queue *commits;
128 const char *in_repo_path;
129 struct got_object_id *start_id;
130 struct got_repository *repo;
131 int log_complete;
132 sig_atomic_t *quit;
133 struct tog_view *view;
134 struct commit_queue_entry **first_displayed_entry;
135 struct commit_queue_entry **selected_entry;
136 };
138 struct tog_log_view_state {
139 struct commit_queue commits;
140 struct commit_queue_entry *first_displayed_entry;
141 struct commit_queue_entry *last_displayed_entry;
142 struct commit_queue_entry *selected_entry;
143 int selected;
144 char *in_repo_path;
145 struct got_repository *repo;
146 struct got_object_id *start_id;
147 sig_atomic_t quit;
148 pthread_t thread;
149 struct tog_log_thread_args thread_args;
150 };
152 struct tog_blame_cb_args {
153 struct tog_blame_line *lines; /* one per line */
154 int nlines;
156 struct tog_view *view;
157 struct got_object_id *commit_id;
158 int *quit;
159 };
161 struct tog_blame_thread_args {
162 const char *path;
163 struct got_repository *repo;
164 struct tog_blame_cb_args *cb_args;
165 int *complete;
166 };
168 struct tog_blame {
169 FILE *f;
170 size_t filesize;
171 struct tog_blame_line *lines;
172 int nlines;
173 pthread_t thread;
174 struct tog_blame_thread_args thread_args;
175 struct tog_blame_cb_args cb_args;
176 const char *path;
177 };
179 struct tog_blame_view_state {
180 int first_displayed_line;
181 int last_displayed_line;
182 int selected_line;
183 int blame_complete;
184 int eof;
185 int done;
186 struct got_object_id_queue blamed_commits;
187 struct got_object_qid *blamed_commit;
188 char *path;
189 struct got_repository *repo;
190 struct got_object_id *commit_id;
191 struct tog_blame blame;
192 };
194 struct tog_parent_tree {
195 TAILQ_ENTRY(tog_parent_tree) entry;
196 struct got_tree_object *tree;
197 struct got_tree_entry *first_displayed_entry;
198 struct got_tree_entry *selected_entry;
199 int selected;
200 };
202 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
204 struct tog_tree_view_state {
205 char *tree_label;
206 struct got_tree_object *root;
207 struct got_tree_object *tree;
208 const struct got_tree_entries *entries;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *last_displayed_entry;
211 struct got_tree_entry *selected_entry;
212 int nentries, ndisplayed, selected, show_ids;
213 struct tog_parent_trees parents;
214 struct got_object_id *commit_id;
215 struct got_repository *repo;
216 };
218 /*
219 * We implement two types of views: parent views and child views.
221 * The 'Tab' key switches between a parent view and its child view.
222 * Child views are shown side-by-side to their parent view, provided
223 * there is enough screen estate.
225 * When a new view is opened from within a parent view, this new view
226 * becomes a child view of the parent view, replacing any existing child.
228 * When a new view is opened from within a child view, this new view
229 * becomes a parent view which will obscure the views below until the
230 * user quits the new parent view by typing 'q'.
232 * This list of views contains parent views only.
233 * Child views are only pointed to by their parent view.
234 */
235 TAILQ_HEAD(tog_view_list_head, tog_view);
237 struct tog_view {
238 TAILQ_ENTRY(tog_view) entry;
239 WINDOW *window;
240 PANEL *panel;
241 int nlines, ncols, begin_y, begin_x;
242 int lines, cols; /* copies of LINES and COLS */
243 int focussed;
244 struct tog_view *parent;
245 struct tog_view *child;
246 int child_focussed;
248 /* type-specific state */
249 enum tog_view_type type;
250 union {
251 struct tog_diff_view_state diff;
252 struct tog_log_view_state log;
253 struct tog_blame_view_state blame;
254 struct tog_tree_view_state tree;
255 } state;
257 const struct got_error *(*show)(struct tog_view *);
258 const struct got_error *(*input)(struct tog_view **,
259 struct tog_view **, struct tog_view**, struct tog_view *, int);
260 const struct got_error *(*close)(struct tog_view *);
261 };
263 static const struct got_error *open_diff_view(struct tog_view *,
264 struct got_object *, struct got_object *, struct got_repository *);
265 static const struct got_error *show_diff_view(struct tog_view *);
266 static const struct got_error *input_diff_view(struct tog_view **,
267 struct tog_view **, struct tog_view **, struct tog_view *, int);
268 static const struct got_error* close_diff_view(struct tog_view *);
270 static const struct got_error *open_log_view(struct tog_view *,
271 struct got_object_id *, struct got_repository *, const char *, int);
272 static const struct got_error * show_log_view(struct tog_view *);
273 static const struct got_error *input_log_view(struct tog_view **,
274 struct tog_view **, struct tog_view **, struct tog_view *, int);
275 static const struct got_error *close_log_view(struct tog_view *);
277 static const struct got_error *open_blame_view(struct tog_view *, char *,
278 struct got_object_id *, struct got_repository *);
279 static const struct got_error *show_blame_view(struct tog_view *);
280 static const struct got_error *input_blame_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error *close_blame_view(struct tog_view *);
284 static const struct got_error *open_tree_view(struct tog_view *,
285 struct got_tree_object *, struct got_object_id *, struct got_repository *);
286 static const struct got_error *show_tree_view(struct tog_view *);
287 static const struct got_error *input_tree_view(struct tog_view **,
288 struct tog_view **, struct tog_view **, struct tog_view *, int);
289 static const struct got_error *close_tree_view(struct tog_view *);
291 static volatile sig_atomic_t tog_sigwinch_received;
293 static void
294 tog_sigwinch(int signo)
296 tog_sigwinch_received = 1;
299 static const struct got_error *
300 view_close(struct tog_view *view)
302 const struct got_error *err = NULL;
304 if (view->child) {
305 view_close(view->child);
306 view->child = NULL;
308 if (view->close)
309 err = view->close(view);
310 if (view->panel)
311 del_panel(view->panel);
312 if (view->window)
313 delwin(view->window);
314 free(view);
315 return err;
318 static struct tog_view *
319 view_open(int nlines, int ncols, int begin_y, int begin_x,
320 enum tog_view_type type)
322 struct tog_view *view = calloc(1, sizeof(*view));
324 if (view == NULL)
325 return NULL;
327 view->type = type;
328 view->lines = LINES;
329 view->cols = COLS;
330 view->nlines = nlines ? nlines : LINES - begin_y;
331 view->ncols = ncols ? ncols : COLS - begin_x;
332 view->begin_y = begin_y;
333 view->begin_x = begin_x;
334 view->window = newwin(nlines, ncols, begin_y, begin_x);
335 if (view->window == NULL) {
336 view_close(view);
337 return NULL;
339 view->panel = new_panel(view->window);
340 if (view->panel == NULL ||
341 set_panel_userptr(view->panel, view) != OK) {
342 view_close(view);
343 return NULL;
346 keypad(view->window, TRUE);
347 return view;
350 static int
351 view_split_begin_x(int begin_x)
353 if (begin_x > 0 || COLS < 120)
354 return 0;
355 return (COLS - MAX(COLS / 2, 80));
358 static const struct got_error *view_resize(struct tog_view *);
360 static const struct got_error *
361 view_splitscreen(struct tog_view *view)
363 const struct got_error *err = NULL;
365 view->begin_y = 0;
366 view->begin_x = view_split_begin_x(0);
367 view->nlines = LINES;
368 view->ncols = COLS - view->begin_x;
369 view->lines = LINES;
370 view->cols = COLS;
371 err = view_resize(view);
372 if (err)
373 return err;
375 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
376 return got_error_from_errno();
378 return NULL;
381 static const struct got_error *
382 view_fullscreen(struct tog_view *view)
384 const struct got_error *err = NULL;
386 view->begin_x = 0;
387 view->begin_y = 0;
388 view->nlines = LINES;
389 view->ncols = COLS;
390 view->lines = LINES;
391 view->cols = COLS;
392 err = view_resize(view);
393 if (err)
394 return err;
396 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
397 return got_error_from_errno();
399 return NULL;
402 static int
403 view_is_parent_view(struct tog_view *view)
405 return view->parent == NULL;
408 static const struct got_error *
409 view_resize(struct tog_view *view)
411 int nlines, ncols;
413 if (view->lines > LINES)
414 nlines = view->nlines - (view->lines - LINES);
415 else
416 nlines = view->nlines + (LINES - view->lines);
418 if (view->cols > COLS)
419 ncols = view->ncols - (view->cols - COLS);
420 else
421 ncols = view->ncols + (COLS - view->cols);
423 if (wresize(view->window, nlines, ncols) == ERR)
424 return got_error_from_errno();
425 if (replace_panel(view->panel, view->window) == ERR)
426 return got_error_from_errno();
427 wclear(view->window);
429 view->nlines = nlines;
430 view->ncols = ncols;
431 view->lines = LINES;
432 view->cols = COLS;
434 if (view->child) {
435 view->child->begin_x = view_split_begin_x(view->begin_x);
436 if (view->child->begin_x == 0) {
437 view_fullscreen(view->child);
438 if (view->child->focussed)
439 show_panel(view->child->panel);
440 else
441 show_panel(view->panel);
442 } else {
443 view_splitscreen(view->child);
444 show_panel(view->child->panel);
448 return NULL;
451 static const struct got_error *
452 view_close_child(struct tog_view *view)
454 const struct got_error *err = NULL;
456 if (view->child == NULL)
457 return NULL;
459 err = view_close(view->child);
460 view->child = NULL;
461 return err;
464 static const struct got_error *
465 view_set_child(struct tog_view *view, struct tog_view *child)
467 const struct got_error *err = NULL;
469 view->child = child;
470 child->parent = view;
471 return err;
474 static int
475 view_is_splitscreen(struct tog_view *view)
477 return !view_is_parent_view(view) && view->begin_x > 0;
480 static void
481 tog_resizeterm(void)
483 int cols, lines;
484 struct winsize size;
486 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
487 cols = 80; /* Default */
488 lines = 24;
489 } else {
490 cols = size.ws_col;
491 lines = size.ws_row;
493 resize_term(lines, cols);
496 static const struct got_error *
497 view_input(struct tog_view **new, struct tog_view **dead,
498 struct tog_view **focus, int *done, struct tog_view *view,
499 struct tog_view_list_head *views)
501 const struct got_error *err = NULL;
502 struct tog_view *v;
503 int ch, errcode;
505 *new = NULL;
506 *dead = NULL;
507 *focus = NULL;
509 nodelay(stdscr, FALSE);
510 /* Allow threads to make progress while we are waiting for input. */
511 errcode = pthread_mutex_unlock(&tog_mutex);
512 if (errcode)
513 return got_error_set_errno(errcode);
514 ch = wgetch(view->window);
515 errcode = pthread_mutex_lock(&tog_mutex);
516 if (errcode)
517 return got_error_set_errno(errcode);
518 nodelay(stdscr, TRUE);
520 if (tog_sigwinch_received) {
521 tog_resizeterm();
522 tog_sigwinch_received = 0;
523 TAILQ_FOREACH(v, views, entry) {
524 err = view_resize(v);
525 if (err)
526 return err;
527 err = v->input(new, dead, focus, v, KEY_RESIZE);
528 if (err)
529 return err;
533 switch (ch) {
534 case ERR:
535 break;
536 case '\t':
537 if (view->child) {
538 *focus = view->child;
539 view->child_focussed = 1;
540 } else if (view->parent) {
541 *focus = view->parent;
542 view->parent->child_focussed = 0;
544 break;
545 case 'q':
546 err = view->input(new, dead, focus, view, ch);
547 *dead = view;
548 break;
549 case 'Q':
550 *done = 1;
551 break;
552 case 'f':
553 if (view_is_parent_view(view)) {
554 if (view->child == NULL)
555 break;
556 if (view_is_splitscreen(view->child)) {
557 *focus = view->child;
558 view->child_focussed = 1;
559 err = view_fullscreen(view->child);
560 } else
561 err = view_splitscreen(view->child);
562 if (err)
563 break;
564 err = view->child->input(new, dead, focus,
565 view->child, KEY_RESIZE);
566 } else {
567 if (view_is_splitscreen(view)) {
568 *focus = view;
569 view->parent->child_focussed = 1;
570 err = view_fullscreen(view);
571 } else {
572 err = view_splitscreen(view);
574 if (err)
575 break;
576 err = view->input(new, dead, focus, view,
577 KEY_RESIZE);
579 break;
580 case KEY_RESIZE:
581 break;
582 default:
583 err = view->input(new, dead, focus, view, ch);
584 break;
587 return err;
590 void
591 view_vborder(struct tog_view *view)
593 PANEL *panel;
594 struct tog_view *view_above;
596 if (view->parent)
597 return view_vborder(view->parent);
599 panel = panel_above(view->panel);
600 if (panel == NULL)
601 return;
603 view_above = panel_userptr(panel);
604 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
605 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
608 int
609 view_needs_focus_indication(struct tog_view *view)
611 if (view_is_parent_view(view)) {
612 if (view->child == NULL || view->child_focussed)
613 return 0;
614 if (!view_is_splitscreen(view->child))
615 return 0;
616 } else if (!view_is_splitscreen(view))
617 return 0;
619 return view->focussed;
622 static const struct got_error *
623 view_loop(struct tog_view *view)
625 const struct got_error *err = NULL;
626 struct tog_view_list_head views;
627 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
628 int fast_refresh = 10;
629 int done = 0, errcode;
631 errcode = pthread_mutex_lock(&tog_mutex);
632 if (errcode)
633 return got_error_set_errno(errcode);
635 TAILQ_INIT(&views);
636 TAILQ_INSERT_HEAD(&views, view, entry);
638 main_view = view;
639 view->focussed = 1;
640 err = view->show(view);
641 if (err)
642 return err;
643 update_panels();
644 doupdate();
645 while (!TAILQ_EMPTY(&views) && !done) {
646 /* Refresh fast during initialization, then become slower. */
647 if (fast_refresh && fast_refresh-- == 0)
648 halfdelay(10); /* switch to once per second */
650 err = view_input(&new_view, &dead_view, &focus_view, &done,
651 view, &views);
652 if (err)
653 break;
654 if (dead_view) {
655 struct tog_view *prev = NULL;
657 if (view_is_parent_view(dead_view))
658 prev = TAILQ_PREV(dead_view,
659 tog_view_list_head, entry);
660 else if (view->parent != dead_view)
661 prev = view->parent;
663 if (dead_view->parent)
664 dead_view->parent->child = NULL;
665 else
666 TAILQ_REMOVE(&views, dead_view, entry);
668 err = view_close(dead_view);
669 if (err || dead_view == main_view)
670 goto done;
672 if (view == dead_view) {
673 if (focus_view)
674 view = focus_view;
675 else if (prev)
676 view = prev;
677 else if (!TAILQ_EMPTY(&views))
678 view = TAILQ_LAST(&views,
679 tog_view_list_head);
680 else
681 view = NULL;
682 if (view) {
683 if (view->child && view->child_focussed)
684 focus_view = view->child;
685 else
686 focus_view = view;
690 if (new_view) {
691 struct tog_view *v, *t;
692 /* Only allow one parent view per type. */
693 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
694 if (v->type != new_view->type)
695 continue;
696 TAILQ_REMOVE(&views, v, entry);
697 err = view_close(v);
698 if (err)
699 goto done;
700 break;
702 TAILQ_INSERT_TAIL(&views, new_view, entry);
703 view = new_view;
704 if (focus_view == NULL)
705 focus_view = new_view;
707 if (focus_view) {
708 show_panel(focus_view->panel);
709 if (view)
710 view->focussed = 0;
711 focus_view->focussed = 1;
712 view = focus_view;
713 if (new_view)
714 show_panel(new_view->panel);
715 if (view->child && view_is_splitscreen(view->child))
716 show_panel(view->child->panel);
718 if (view) {
719 if (focus_view == NULL) {
720 view->focussed = 1;
721 show_panel(view->panel);
722 if (view->child && view_is_splitscreen(view->child))
723 show_panel(view->child->panel);
724 focus_view = view;
726 if (view->parent) {
727 err = view->parent->show(view->parent);
728 if (err)
729 goto done;
731 err = view->show(view);
732 if (err)
733 goto done;
734 if (view->child) {
735 err = view->child->show(view->child);
736 if (err)
737 goto done;
739 update_panels();
740 doupdate();
743 done:
744 while (!TAILQ_EMPTY(&views)) {
745 view = TAILQ_FIRST(&views);
746 TAILQ_REMOVE(&views, view, entry);
747 view_close(view);
750 errcode = pthread_mutex_unlock(&tog_mutex);
751 if (errcode)
752 return got_error_set_errno(errcode);
754 return err;
757 __dead static void
758 usage_log(void)
760 endwin();
761 fprintf(stderr,
762 "usage: %s log [-c commit] [-r repository-path] [path]\n",
763 getprogname());
764 exit(1);
767 /* Create newly allocated wide-character string equivalent to a byte string. */
768 static const struct got_error *
769 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
771 char *vis = NULL;
772 const struct got_error *err = NULL;
774 *ws = NULL;
775 *wlen = mbstowcs(NULL, s, 0);
776 if (*wlen == (size_t)-1) {
777 int vislen;
778 if (errno != EILSEQ)
779 return got_error_from_errno();
781 /* byte string invalid in current encoding; try to "fix" it */
782 err = got_mbsavis(&vis, &vislen, s);
783 if (err)
784 return err;
785 *wlen = mbstowcs(NULL, vis, 0);
786 if (*wlen == (size_t)-1) {
787 err = got_error_from_errno(); /* give up */
788 goto done;
792 *ws = calloc(*wlen + 1, sizeof(*ws));
793 if (*ws == NULL) {
794 err = got_error_from_errno();
795 goto done;
798 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
799 err = got_error_from_errno();
800 done:
801 free(vis);
802 if (err) {
803 free(*ws);
804 *ws = NULL;
805 *wlen = 0;
807 return err;
810 /* Format a line for display, ensuring that it won't overflow a width limit. */
811 static const struct got_error *
812 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
814 const struct got_error *err = NULL;
815 int cols = 0;
816 wchar_t *wline = NULL;
817 size_t wlen;
818 int i;
820 *wlinep = NULL;
821 *widthp = 0;
823 err = mbs2ws(&wline, &wlen, line);
824 if (err)
825 return err;
827 i = 0;
828 while (i < wlen && cols < wlimit) {
829 int width = wcwidth(wline[i]);
830 switch (width) {
831 case 0:
832 i++;
833 break;
834 case 1:
835 case 2:
836 if (cols + width <= wlimit)
837 cols += width;
838 i++;
839 break;
840 case -1:
841 if (wline[i] == L'\t')
842 cols += TABSIZE - ((cols + 1) % TABSIZE);
843 i++;
844 break;
845 default:
846 err = got_error_from_errno();
847 goto done;
850 wline[i] = L'\0';
851 if (widthp)
852 *widthp = cols;
853 done:
854 if (err)
855 free(wline);
856 else
857 *wlinep = wline;
858 return err;
861 static const struct got_error *
862 draw_commit(struct tog_view *view, struct got_commit_object *commit,
863 struct got_object_id *id)
865 const struct got_error *err = NULL;
866 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
867 char *logmsg0 = NULL, *logmsg = NULL;
868 char *author0 = NULL, *author = NULL;
869 wchar_t *wlogmsg = NULL, *wauthor = NULL;
870 int author_width, logmsg_width;
871 char *newline, *smallerthan;
872 char *line = NULL;
873 int col, limit;
874 static const size_t date_display_cols = 9;
875 static const size_t author_display_cols = 16;
876 const int avail = view->ncols;
877 struct tm tm;
879 if (localtime_r(&commit->committer_time, &tm) == NULL)
880 return got_error_from_errno();
881 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
882 >= sizeof(datebuf))
883 return got_error(GOT_ERR_NO_SPACE);
885 if (avail < date_display_cols)
886 limit = MIN(sizeof(datebuf) - 1, avail);
887 else
888 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
889 waddnstr(view->window, datebuf, limit);
890 col = limit + 1;
891 if (col > avail)
892 goto done;
894 author0 = strdup(commit->author);
895 if (author0 == NULL) {
896 err = got_error_from_errno();
897 goto done;
899 author = author0;
900 smallerthan = strchr(author, '<');
901 if (smallerthan)
902 *smallerthan = '\0';
903 else {
904 char *at = strchr(author, '@');
905 if (at)
906 *at = '\0';
908 limit = avail - col;
909 err = format_line(&wauthor, &author_width, author, limit);
910 if (err)
911 goto done;
912 waddwstr(view->window, wauthor);
913 col += author_width;
914 while (col <= avail && author_width < author_display_cols + 1) {
915 waddch(view->window, ' ');
916 col++;
917 author_width++;
919 if (col > avail)
920 goto done;
922 logmsg0 = strdup(commit->logmsg);
923 if (logmsg0 == NULL) {
924 err = got_error_from_errno();
925 goto done;
927 logmsg = logmsg0;
928 while (*logmsg == '\n')
929 logmsg++;
930 newline = strchr(logmsg, '\n');
931 if (newline)
932 *newline = '\0';
933 limit = avail - col;
934 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
935 if (err)
936 goto done;
937 waddwstr(view->window, wlogmsg);
938 col += logmsg_width;
939 while (col <= avail) {
940 waddch(view->window, ' ');
941 col++;
943 done:
944 free(logmsg0);
945 free(wlogmsg);
946 free(author0);
947 free(wauthor);
948 free(line);
949 return err;
952 static struct commit_queue_entry *
953 alloc_commit_queue_entry(struct got_commit_object *commit,
954 struct got_object_id *id)
956 struct commit_queue_entry *entry;
958 entry = calloc(1, sizeof(*entry));
959 if (entry == NULL)
960 return NULL;
962 entry->id = id;
963 entry->commit = commit;
964 return entry;
967 static void
968 pop_commit(struct commit_queue *commits)
970 struct commit_queue_entry *entry;
972 entry = TAILQ_FIRST(&commits->head);
973 TAILQ_REMOVE(&commits->head, entry, entry);
974 got_object_commit_close(entry->commit);
975 commits->ncommits--;
976 /* Don't free entry->id! It is owned by the commit graph. */
977 free(entry);
980 static void
981 free_commits(struct commit_queue *commits)
983 while (!TAILQ_EMPTY(&commits->head))
984 pop_commit(commits);
987 static const struct got_error *
988 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
989 int minqueue, struct got_repository *repo, const char *path)
991 const struct got_error *err = NULL;
992 int nqueued = 0;
994 /*
995 * We keep all commits open throughout the lifetime of the log
996 * view in order to avoid having to re-fetch commits from disk
997 * while updating the display.
998 */
999 while (nqueued < minqueue) {
1000 struct got_object_id *id;
1001 struct got_commit_object *commit;
1002 struct commit_queue_entry *entry;
1003 int errcode;
1005 err = got_commit_graph_iter_next(&id, graph);
1006 if (err) {
1007 if (err->code != GOT_ERR_ITER_NEED_MORE)
1008 break;
1009 err = got_commit_graph_fetch_commits(graph,
1010 minqueue, repo);
1011 if (err)
1012 return err;
1013 continue;
1016 if (id == NULL)
1017 break;
1019 err = got_object_open_as_commit(&commit, repo, id);
1020 if (err)
1021 break;
1022 entry = alloc_commit_queue_entry(commit, id);
1023 if (entry == NULL) {
1024 err = got_error_from_errno();
1025 break;
1028 errcode = pthread_mutex_lock(&tog_mutex);
1029 if (errcode) {
1030 err = got_error_set_errno(errcode);
1031 break;
1034 entry->idx = commits->ncommits;
1035 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1036 nqueued++;
1037 commits->ncommits++;
1039 errcode = pthread_mutex_unlock(&tog_mutex);
1040 if (errcode && err == NULL)
1041 err = got_error_set_errno(errcode);
1044 return err;
1047 static const struct got_error *
1048 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1050 const struct got_error *err = NULL;
1051 struct got_reference *head_ref;
1053 *head_id = NULL;
1055 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1056 if (err)
1057 return err;
1059 err = got_ref_resolve(head_id, repo, head_ref);
1060 got_ref_close(head_ref);
1061 if (err) {
1062 *head_id = NULL;
1063 return err;
1066 return NULL;
1069 static const struct got_error *
1070 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1071 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1072 struct commit_queue *commits, int selected_idx, int limit,
1073 const char *path, int commits_needed)
1075 const struct got_error *err = NULL;
1076 struct commit_queue_entry *entry;
1077 int ncommits, width;
1078 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1079 wchar_t *wline;
1081 entry = first;
1082 ncommits = 0;
1083 while (entry) {
1084 if (ncommits == selected_idx) {
1085 *selected = entry;
1086 break;
1088 entry = TAILQ_NEXT(entry, entry);
1089 ncommits++;
1092 if (*selected) {
1093 err = got_object_id_str(&id_str, (*selected)->id);
1094 if (err)
1095 return err;
1098 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1099 entry ? entry->idx + 1 : 0, commits->ncommits,
1100 commits_needed == 0 ? "" : " loading...") == -1)
1101 return got_error_from_errno();
1103 if (path && strcmp(path, "/") != 0) {
1104 if (asprintf(&header, "commit: %s %s%s",
1105 id_str ? id_str : "........................................",
1106 path, ncommits_str) == -1) {
1107 err = got_error_from_errno();
1108 header = NULL;
1109 goto done;
1111 } else if (asprintf(&header, "commit: %s%s",
1112 id_str ? id_str : "........................................",
1113 ncommits_str) == -1) {
1114 err = got_error_from_errno();
1115 header = NULL;
1116 goto done;
1118 err = format_line(&wline, &width, header, view->ncols);
1119 if (err)
1120 goto done;
1122 werase(view->window);
1124 if (view_needs_focus_indication(view))
1125 wstandout(view->window);
1126 waddwstr(view->window, wline);
1127 while (width < view->ncols) {
1128 waddch(view->window, ' ');
1129 width++;
1131 if (view_needs_focus_indication(view))
1132 wstandend(view->window);
1133 free(wline);
1134 if (limit <= 1)
1135 goto done;
1137 entry = first;
1138 *last = first;
1139 ncommits = 0;
1140 while (entry) {
1141 if (ncommits >= limit - 1)
1142 break;
1143 if (view->focussed && ncommits == selected_idx)
1144 wstandout(view->window);
1145 err = draw_commit(view, entry->commit, entry->id);
1146 if (view->focussed && ncommits == selected_idx)
1147 wstandend(view->window);
1148 if (err)
1149 break;
1150 ncommits++;
1151 *last = entry;
1152 entry = TAILQ_NEXT(entry, entry);
1155 view_vborder(view);
1156 done:
1157 free(id_str);
1158 free(ncommits_str);
1159 free(header);
1160 return err;
1163 static void
1164 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1165 struct commit_queue *commits)
1167 struct commit_queue_entry *entry;
1168 int nscrolled = 0;
1170 entry = TAILQ_FIRST(&commits->head);
1171 if (*first_displayed_entry == entry)
1172 return;
1174 entry = *first_displayed_entry;
1175 while (entry && nscrolled < maxscroll) {
1176 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1177 if (entry) {
1178 *first_displayed_entry = entry;
1179 nscrolled++;
1184 static const struct got_error *
1185 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1186 struct commit_queue_entry **last_displayed_entry,
1187 struct commit_queue *commits, int *log_complete, int *commits_needed,
1188 pthread_cond_t *need_commits)
1190 const struct got_error *err = NULL;
1191 struct commit_queue_entry *pentry;
1192 int nscrolled = 0;
1194 if (*last_displayed_entry == NULL)
1195 return NULL;
1197 do {
1198 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1199 if (pentry == NULL) {
1200 int errcode;
1201 if (*log_complete)
1202 return NULL;
1203 *commits_needed = maxscroll + 20;
1204 errcode = pthread_cond_signal(need_commits);
1205 if (errcode)
1206 return got_error_set_errno(errcode);
1207 return NULL;
1209 *last_displayed_entry = pentry;
1211 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1212 if (pentry == NULL)
1213 break;
1214 *first_displayed_entry = pentry;
1215 } while (++nscrolled < maxscroll);
1217 return err;
1220 static const struct got_error *
1221 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1222 struct got_object_id *commit_id, struct got_commit_object *commit,
1223 struct got_repository *repo)
1225 const struct got_error *err;
1226 struct got_object *obj1 = NULL, *obj2 = NULL;
1227 struct got_object_qid *parent_id;
1228 struct tog_view *diff_view;
1230 err = got_object_open(&obj2, repo, commit_id);
1231 if (err)
1232 return err;
1234 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1235 if (parent_id) {
1236 err = got_object_open(&obj1, repo, parent_id->id);
1237 if (err)
1238 goto done;
1241 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1242 if (diff_view == NULL) {
1243 err = got_error_from_errno();
1244 goto done;
1247 err = open_diff_view(diff_view, obj1, obj2, repo);
1248 if (err == NULL)
1249 *new_view = diff_view;
1250 done:
1251 if (obj1)
1252 got_object_close(obj1);
1253 if (obj2)
1254 got_object_close(obj2);
1255 return err;
1258 static const struct got_error *
1259 browse_commit(struct tog_view **new_view, int begin_x,
1260 struct commit_queue_entry *entry, struct got_repository *repo)
1262 const struct got_error *err = NULL;
1263 struct got_tree_object *tree;
1264 struct tog_view *tree_view;
1266 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1267 if (err)
1268 return err;
1270 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1271 if (tree_view == NULL)
1272 return got_error_from_errno();
1274 err = open_tree_view(tree_view, tree, entry->id, repo);
1275 if (err)
1276 got_object_tree_close(tree);
1277 else
1278 *new_view = tree_view;
1279 return err;
1282 static void *
1283 log_thread(void *arg)
1285 const struct got_error *err = NULL;
1286 int errcode = 0;
1287 struct tog_log_thread_args *a = arg;
1288 int done = 0;
1290 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1291 if (err)
1292 return (void *)err;
1294 while (!done && !err) {
1295 err = queue_commits(a->graph, a->commits, 1, a->repo,
1296 a->in_repo_path);
1297 if (err) {
1298 if (err->code != GOT_ERR_ITER_COMPLETED)
1299 return (void *)err;
1300 err = NULL;
1301 done = 1;
1302 } else if (a->commits_needed > 0)
1303 a->commits_needed--;
1305 errcode = pthread_mutex_lock(&tog_mutex);
1306 if (errcode)
1307 return (void *)got_error_set_errno(errcode);
1309 if (done)
1310 a->log_complete = 1;
1311 else if (*a->quit) {
1312 done = 1;
1313 a->log_complete = 1;
1314 } else if (*a->first_displayed_entry == NULL) {
1315 *a->first_displayed_entry =
1316 TAILQ_FIRST(&a->commits->head);
1317 *a->selected_entry = *a->first_displayed_entry;
1320 if (done)
1321 a->commits_needed = 0;
1322 else if (a->commits_needed == 0) {
1323 errcode = pthread_cond_wait(&a->need_commits,
1324 &tog_mutex);
1325 if (errcode)
1326 err = got_error_set_errno(errcode);
1329 errcode = pthread_mutex_unlock(&tog_mutex);
1330 if (errcode && err == NULL)
1331 err = got_error_set_errno(errcode);
1333 return (void *)err;
1336 static const struct got_error *
1337 stop_log_thread(struct tog_log_view_state *s)
1339 const struct got_error *err = NULL;
1340 int errcode;
1342 if (s->thread) {
1343 s->quit = 1;
1344 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1345 if (errcode)
1346 return got_error_set_errno(errcode);
1347 errcode = pthread_mutex_unlock(&tog_mutex);
1348 if (errcode)
1349 return got_error_set_errno(errcode);
1350 errcode = pthread_join(s->thread, (void **)&err);
1351 if (errcode)
1352 return got_error_set_errno(errcode);
1353 errcode = pthread_mutex_lock(&tog_mutex);
1354 if (errcode)
1355 return got_error_set_errno(errcode);
1356 s->thread = NULL;
1359 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1360 if (errcode && err == NULL)
1361 err = got_error_set_errno(errcode);
1363 if (s->thread_args.repo) {
1364 got_repo_close(s->thread_args.repo);
1365 s->thread_args.repo = NULL;
1368 if (s->thread_args.graph) {
1369 got_commit_graph_close(s->thread_args.graph);
1370 s->thread_args.graph = NULL;
1373 return err;
1376 static const struct got_error *
1377 close_log_view(struct tog_view *view)
1379 const struct got_error *err = NULL;
1380 struct tog_log_view_state *s = &view->state.log;
1382 err = stop_log_thread(s);
1383 free_commits(&s->commits);
1384 free(s->in_repo_path);
1385 s->in_repo_path = NULL;
1386 free(s->start_id);
1387 s->start_id = NULL;
1388 return err;
1391 static const struct got_error *
1392 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1393 struct got_repository *repo, const char *path, int check_disk)
1395 const struct got_error *err = NULL;
1396 struct tog_log_view_state *s = &view->state.log;
1397 struct got_repository *thread_repo = NULL;
1398 struct got_commit_graph *thread_graph = NULL;
1399 int errcode;
1401 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1402 if (err != NULL)
1403 goto done;
1405 /* The commit queue only contains commits being displayed. */
1406 TAILQ_INIT(&s->commits.head);
1407 s->commits.ncommits = 0;
1409 s->repo = repo;
1410 s->start_id = got_object_id_dup(start_id);
1411 if (s->start_id == NULL) {
1412 err = got_error_from_errno();
1413 goto done;
1416 view->show = show_log_view;
1417 view->input = input_log_view;
1418 view->close = close_log_view;
1420 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1421 if (err)
1422 goto done;
1423 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1424 0, thread_repo);
1425 if (err)
1426 goto done;
1428 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1429 if (errcode) {
1430 err = got_error_set_errno(errcode);
1431 goto done;
1434 s->thread_args.commits_needed = view->nlines;
1435 s->thread_args.graph = thread_graph;
1436 s->thread_args.commits = &s->commits;
1437 s->thread_args.in_repo_path = s->in_repo_path;
1438 s->thread_args.start_id = s->start_id;
1439 s->thread_args.repo = thread_repo;
1440 s->thread_args.log_complete = 0;
1441 s->thread_args.quit = &s->quit;
1442 s->thread_args.view = view;
1443 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1444 s->thread_args.selected_entry = &s->selected_entry;
1445 done:
1446 if (err)
1447 close_log_view(view);
1448 return err;
1451 static const struct got_error *
1452 show_log_view(struct tog_view *view)
1454 struct tog_log_view_state *s = &view->state.log;
1456 if (s->thread == NULL) {
1457 int errcode = pthread_create(&s->thread, NULL, log_thread,
1458 &s->thread_args);
1459 if (errcode)
1460 return got_error_set_errno(errcode);
1463 return draw_commits(view, &s->last_displayed_entry,
1464 &s->selected_entry, s->first_displayed_entry,
1465 &s->commits, s->selected, view->nlines,
1466 s->in_repo_path, s->thread_args.commits_needed);
1469 static const struct got_error *
1470 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1471 struct tog_view **focus_view, struct tog_view *view, int ch)
1473 const struct got_error *err = NULL;
1474 struct tog_log_view_state *s = &view->state.log;
1475 char *parent_path;
1476 struct tog_view *diff_view = NULL, *tree_view = NULL;
1477 int begin_x = 0;
1479 switch (ch) {
1480 case 'q':
1481 s->quit = 1;
1482 break;
1483 case 'k':
1484 case KEY_UP:
1485 if (s->first_displayed_entry == NULL)
1486 break;
1487 if (s->selected > 0)
1488 s->selected--;
1489 if (s->selected > 0)
1490 break;
1491 scroll_up(&s->first_displayed_entry, 1,
1492 &s->commits);
1493 break;
1494 case KEY_PPAGE:
1495 if (s->first_displayed_entry == NULL)
1496 break;
1497 if (TAILQ_FIRST(&s->commits.head) ==
1498 s->first_displayed_entry) {
1499 s->selected = 0;
1500 break;
1502 scroll_up(&s->first_displayed_entry,
1503 view->nlines, &s->commits);
1504 break;
1505 case 'j':
1506 case KEY_DOWN:
1507 if (s->first_displayed_entry == NULL)
1508 break;
1509 if (s->selected < MIN(view->nlines - 2,
1510 s->commits.ncommits - 1)) {
1511 s->selected++;
1512 break;
1514 err = scroll_down(&s->first_displayed_entry, 1,
1515 &s->last_displayed_entry, &s->commits,
1516 &s->thread_args.log_complete,
1517 &s->thread_args.commits_needed,
1518 &s->thread_args.need_commits);
1519 break;
1520 case KEY_NPAGE: {
1521 struct commit_queue_entry *first;
1522 first = s->first_displayed_entry;
1523 if (first == NULL)
1524 break;
1525 err = scroll_down(&s->first_displayed_entry,
1526 view->nlines, &s->last_displayed_entry,
1527 &s->commits, &s->thread_args.log_complete,
1528 &s->thread_args.commits_needed,
1529 &s->thread_args.need_commits);
1530 if (first == s->first_displayed_entry &&
1531 s->selected < MIN(view->nlines - 2,
1532 s->commits.ncommits - 1)) {
1533 /* can't scroll further down */
1534 s->selected = MIN(view->nlines - 2,
1535 s->commits.ncommits - 1);
1537 err = NULL;
1538 break;
1540 case KEY_RESIZE:
1541 if (s->selected > view->nlines - 2)
1542 s->selected = view->nlines - 2;
1543 if (s->selected > s->commits.ncommits - 1)
1544 s->selected = s->commits.ncommits - 1;
1545 break;
1546 case KEY_ENTER:
1547 case '\r':
1548 if (s->selected_entry == NULL)
1549 break;
1550 if (view_is_parent_view(view))
1551 begin_x = view_split_begin_x(view->begin_x);
1552 err = open_diff_view_for_commit(&diff_view, begin_x,
1553 s->selected_entry->id, s->selected_entry->commit,
1554 s->repo);
1555 if (err)
1556 break;
1557 if (view_is_parent_view(view)) {
1558 err = view_close_child(view);
1559 if (err)
1560 return err;
1561 err = view_set_child(view, diff_view);
1562 if (err) {
1563 view_close(diff_view);
1564 break;
1566 *focus_view = diff_view;
1567 view->child_focussed = 1;
1568 } else
1569 *new_view = diff_view;
1570 break;
1571 case 't':
1572 if (s->selected_entry == NULL)
1573 break;
1574 if (view_is_parent_view(view))
1575 begin_x = view_split_begin_x(view->begin_x);
1576 err = browse_commit(&tree_view, begin_x,
1577 s->selected_entry, s->repo);
1578 if (err)
1579 break;
1580 if (view_is_parent_view(view)) {
1581 err = view_close_child(view);
1582 if (err)
1583 return err;
1584 err = view_set_child(view, tree_view);
1585 if (err) {
1586 view_close(tree_view);
1587 break;
1589 *focus_view = tree_view;
1590 view->child_focussed = 1;
1591 } else
1592 *new_view = tree_view;
1593 break;
1594 case KEY_BACKSPACE:
1595 if (strcmp(s->in_repo_path, "/") == 0)
1596 break;
1597 parent_path = dirname(s->in_repo_path);
1598 if (parent_path && strcmp(parent_path, ".") != 0) {
1599 struct tog_view *lv;
1600 err = stop_log_thread(s);
1601 if (err)
1602 return err;
1603 lv = view_open(view->nlines, view->ncols,
1604 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1605 if (lv == NULL)
1606 return got_error_from_errno();
1607 err = open_log_view(lv, s->start_id, s->repo,
1608 parent_path, 0);
1609 if (err)
1610 return err;;
1611 if (view_is_parent_view(view))
1612 *new_view = lv;
1613 else {
1614 view_set_child(view->parent, lv);
1615 *focus_view = lv;
1617 return NULL;
1619 break;
1620 default:
1621 break;
1624 return err;
1627 static const struct got_error *
1628 cmd_log(int argc, char *argv[])
1630 const struct got_error *error;
1631 struct got_repository *repo = NULL;
1632 struct got_object_id *start_id = NULL;
1633 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1634 char *start_commit = NULL;
1635 int ch;
1636 struct tog_view *view;
1638 #ifndef PROFILE
1639 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1640 == -1)
1641 err(1, "pledge");
1642 #endif
1644 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1645 switch (ch) {
1646 case 'c':
1647 start_commit = optarg;
1648 break;
1649 case 'r':
1650 repo_path = realpath(optarg, NULL);
1651 if (repo_path == NULL)
1652 err(1, "-r option");
1653 break;
1654 default:
1655 usage();
1656 /* NOTREACHED */
1660 argc -= optind;
1661 argv += optind;
1663 if (argc == 0)
1664 path = strdup("");
1665 else if (argc == 1)
1666 path = strdup(argv[0]);
1667 else
1668 usage_log();
1669 if (path == NULL)
1670 return got_error_from_errno();
1672 cwd = getcwd(NULL, 0);
1673 if (cwd == NULL) {
1674 error = got_error_from_errno();
1675 goto done;
1677 if (repo_path == NULL) {
1678 repo_path = strdup(cwd);
1679 if (repo_path == NULL) {
1680 error = got_error_from_errno();
1681 goto done;
1685 error = got_repo_open(&repo, repo_path);
1686 if (error != NULL)
1687 goto done;
1689 if (start_commit == NULL) {
1690 error = get_head_commit_id(&start_id, repo);
1691 if (error != NULL)
1692 goto done;
1693 } else {
1694 struct got_object *obj;
1695 error = got_object_open_by_id_str(&obj, repo, start_commit);
1696 if (error == NULL) {
1697 start_id = got_object_id_dup(got_object_get_id(obj));
1698 if (start_id == NULL)
1699 error = got_error_from_errno();
1700 goto done;
1703 if (error != NULL)
1704 goto done;
1706 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1707 if (view == NULL) {
1708 error = got_error_from_errno();
1709 goto done;
1711 error = open_log_view(view, start_id, repo, path, 1);
1712 if (error)
1713 goto done;
1714 error = view_loop(view);
1715 done:
1716 free(repo_path);
1717 free(cwd);
1718 free(path);
1719 free(start_id);
1720 if (repo)
1721 got_repo_close(repo);
1722 return error;
1725 __dead static void
1726 usage_diff(void)
1728 endwin();
1729 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1730 getprogname());
1731 exit(1);
1734 static char *
1735 parse_next_line(FILE *f, size_t *len)
1737 char *line;
1738 size_t linelen;
1739 size_t lineno;
1740 const char delim[3] = { '\0', '\0', '\0'};
1742 line = fparseln(f, &linelen, &lineno, delim, 0);
1743 if (len)
1744 *len = linelen;
1745 return line;
1748 static const struct got_error *
1749 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1750 int *last_displayed_line, int *eof, int max_lines,
1751 char * header)
1753 const struct got_error *err;
1754 int nlines = 0, nprinted = 0;
1755 char *line;
1756 size_t len;
1757 wchar_t *wline;
1758 int width;
1760 rewind(f);
1761 werase(view->window);
1763 if (header) {
1764 err = format_line(&wline, &width, header, view->ncols);
1765 if (err) {
1766 return err;
1769 if (view_needs_focus_indication(view))
1770 wstandout(view->window);
1771 waddwstr(view->window, wline);
1772 if (view_needs_focus_indication(view))
1773 wstandend(view->window);
1774 if (width < view->ncols)
1775 waddch(view->window, '\n');
1777 if (max_lines <= 1)
1778 return NULL;
1779 max_lines--;
1782 *eof = 0;
1783 while (nprinted < max_lines) {
1784 line = parse_next_line(f, &len);
1785 if (line == NULL) {
1786 *eof = 1;
1787 break;
1789 if (++nlines < *first_displayed_line) {
1790 free(line);
1791 continue;
1794 err = format_line(&wline, &width, line, view->ncols);
1795 if (err) {
1796 free(line);
1797 return err;
1799 waddwstr(view->window, wline);
1800 if (width < view->ncols)
1801 waddch(view->window, '\n');
1802 if (++nprinted == 1)
1803 *first_displayed_line = nlines;
1804 free(line);
1805 free(wline);
1806 wline = NULL;
1808 *last_displayed_line = nlines;
1810 view_vborder(view);
1812 return NULL;
1815 static char *
1816 get_datestr(time_t *time, char *datebuf)
1818 char *p, *s = ctime_r(time, datebuf);
1819 p = strchr(s, '\n');
1820 if (p)
1821 *p = '\0';
1822 return s;
1825 static const struct got_error *
1826 write_commit_info(struct got_object *obj, struct got_repository *repo,
1827 FILE *outfile)
1829 const struct got_error *err = NULL;
1830 char *id_str;
1831 char datebuf[26];
1832 struct got_commit_object *commit = NULL;
1834 err = got_object_id_str(&id_str, got_object_get_id(obj));
1835 if (err)
1836 return err;
1838 err = got_object_commit_open(&commit, repo, obj);
1840 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
1841 err = got_error_from_errno();
1842 goto done;
1844 if (fprintf(outfile, "from: %s\n", commit->author) < 0) {
1845 err = got_error_from_errno();
1846 goto done;
1848 if (fprintf(outfile, "date: %s UTC\n",
1849 get_datestr(&commit->committer_time, datebuf)) < 0) {
1850 err = got_error_from_errno();
1851 goto done;
1853 if (strcmp(commit->author, commit->committer) != 0 &&
1854 fprintf(outfile, "via: %s\n", commit->committer) < 0) {
1855 err = got_error_from_errno();
1856 goto done;
1858 if (fprintf(outfile, "%s\n", commit->logmsg) < 0) {
1859 err = got_error_from_errno();
1860 goto done;
1862 done:
1863 free(id_str);
1864 if (commit)
1865 got_object_commit_close(commit);
1866 return err;
1869 static const struct got_error *
1870 create_diff(struct tog_diff_view_state *s)
1872 const struct got_error *err = NULL;
1873 struct got_object *obj1 = NULL, *obj2 = NULL;
1874 FILE *f = NULL;
1876 if (s->id1) {
1877 err = got_object_open(&obj1, s->repo, s->id1);
1878 if (err)
1879 return err;
1882 err = got_object_open(&obj2, s->repo, s->id2);
1883 if (err)
1884 goto done;
1886 f = got_opentemp();
1887 if (f == NULL) {
1888 err = got_error_from_errno();
1889 goto done;
1891 if (s->f)
1892 fclose(s->f);
1893 s->f = f;
1895 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1896 case GOT_OBJ_TYPE_BLOB:
1897 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1898 s->diff_context, s->repo, f);
1899 break;
1900 case GOT_OBJ_TYPE_TREE:
1901 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1902 s->diff_context, s->repo, f);
1903 break;
1904 case GOT_OBJ_TYPE_COMMIT: {
1905 struct got_object_qid *pid;
1906 struct got_commit_object *commit2;
1908 err = got_object_commit_open(&commit2, s->repo, obj2);
1909 if (err)
1910 break;
1911 /* Show commit info if we're diffing to a parent commit. */
1912 SIMPLEQ_FOREACH(pid, &commit2->parent_ids, entry) {
1913 struct got_object_id *id1 = got_object_get_id(obj1);
1914 if (got_object_id_cmp(id1, pid->id) == 0) {
1915 write_commit_info(obj2, s->repo, f);
1916 break;
1919 got_object_commit_close(commit2);
1921 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1922 s->repo, f);
1923 break;
1925 default:
1926 err = got_error(GOT_ERR_OBJ_TYPE);
1927 break;
1929 done:
1930 if (obj1)
1931 got_object_close(obj1);
1932 got_object_close(obj2);
1933 if (f)
1934 fflush(f);
1935 return err;
1938 static const struct got_error *
1939 open_diff_view(struct tog_view *view, struct got_object *obj1,
1940 struct got_object *obj2, struct got_repository *repo)
1942 const struct got_error *err;
1944 if (obj1 != NULL && obj2 != NULL &&
1945 got_object_get_type(obj1) != got_object_get_type(obj2))
1946 return got_error(GOT_ERR_OBJ_TYPE);
1948 if (obj1) {
1949 struct got_object_id *id1;
1950 id1 = got_object_id_dup(got_object_get_id(obj1));
1951 if (id1 == NULL)
1952 return got_error_from_errno();
1953 view->state.diff.id1 = id1;
1954 } else
1955 view->state.diff.id1 = NULL;
1957 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1958 if (view->state.diff.id2 == NULL) {
1959 free(view->state.diff.id1);
1960 view->state.diff.id1 = NULL;
1961 return got_error_from_errno();
1963 view->state.diff.f = NULL;
1964 view->state.diff.first_displayed_line = 1;
1965 view->state.diff.last_displayed_line = view->nlines;
1966 view->state.diff.diff_context = 3;
1967 view->state.diff.repo = repo;
1969 err = create_diff(&view->state.diff);
1970 if (err) {
1971 free(view->state.diff.id1);
1972 view->state.diff.id1 = NULL;
1973 free(view->state.diff.id2);
1974 view->state.diff.id2 = NULL;
1975 return err;
1978 view->show = show_diff_view;
1979 view->input = input_diff_view;
1980 view->close = close_diff_view;
1982 return NULL;
1985 static const struct got_error *
1986 close_diff_view(struct tog_view *view)
1988 const struct got_error *err = NULL;
1990 free(view->state.diff.id1);
1991 view->state.diff.id1 = NULL;
1992 free(view->state.diff.id2);
1993 view->state.diff.id2 = NULL;
1994 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1995 err = got_error_from_errno();
1996 return err;
1999 static const struct got_error *
2000 show_diff_view(struct tog_view *view)
2002 const struct got_error *err;
2003 struct tog_diff_view_state *s = &view->state.diff;
2004 char *id_str1 = NULL, *id_str2, *header;
2006 if (s->id1) {
2007 err = got_object_id_str(&id_str1, s->id1);
2008 if (err)
2009 return err;
2011 err = got_object_id_str(&id_str2, s->id2);
2012 if (err)
2013 return err;
2015 if (asprintf(&header, "diff: %s %s",
2016 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2017 err = got_error_from_errno();
2018 free(id_str1);
2019 free(id_str2);
2020 return err;
2022 free(id_str1);
2023 free(id_str2);
2025 return draw_file(view, s->f, &s->first_displayed_line,
2026 &s->last_displayed_line, &s->eof, view->nlines,
2027 header);
2030 static const struct got_error *
2031 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2032 struct tog_view **focus_view, struct tog_view *view, int ch)
2034 const struct got_error *err = NULL;
2035 struct tog_diff_view_state *s = &view->state.diff;
2036 int i;
2038 switch (ch) {
2039 case 'k':
2040 case KEY_UP:
2041 if (s->first_displayed_line > 1)
2042 s->first_displayed_line--;
2043 break;
2044 case KEY_PPAGE:
2045 i = 0;
2046 while (i++ < view->nlines - 1 &&
2047 s->first_displayed_line > 1)
2048 s->first_displayed_line--;
2049 break;
2050 case 'j':
2051 case KEY_DOWN:
2052 if (!s->eof)
2053 s->first_displayed_line++;
2054 break;
2055 case KEY_NPAGE:
2056 case ' ':
2057 i = 0;
2058 while (!s->eof && i++ < view->nlines - 1) {
2059 char *line;
2060 line = parse_next_line(s->f, NULL);
2061 s->first_displayed_line++;
2062 if (line == NULL)
2063 break;
2065 break;
2066 case '[':
2067 if (s->diff_context > 0) {
2068 s->diff_context--;
2069 err = create_diff(s);
2071 break;
2072 case ']':
2073 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2074 s->diff_context++;
2075 err = create_diff(s);
2077 break;
2078 default:
2079 break;
2082 return err;
2085 static const struct got_error *
2086 cmd_diff(int argc, char *argv[])
2088 const struct got_error *error = NULL;
2089 struct got_repository *repo = NULL;
2090 struct got_object *obj1 = NULL, *obj2 = NULL;
2091 char *repo_path = NULL;
2092 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
2093 int ch;
2094 struct tog_view *view;
2096 #ifndef PROFILE
2097 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2098 == -1)
2099 err(1, "pledge");
2100 #endif
2102 while ((ch = getopt(argc, argv, "")) != -1) {
2103 switch (ch) {
2104 default:
2105 usage();
2106 /* NOTREACHED */
2110 argc -= optind;
2111 argv += optind;
2113 if (argc == 0) {
2114 usage_diff(); /* TODO show local worktree changes */
2115 } else if (argc == 2) {
2116 repo_path = getcwd(NULL, 0);
2117 if (repo_path == NULL)
2118 return got_error_from_errno();
2119 obj_id_str1 = argv[0];
2120 obj_id_str2 = argv[1];
2121 } else if (argc == 3) {
2122 repo_path = realpath(argv[0], NULL);
2123 if (repo_path == NULL)
2124 return got_error_from_errno();
2125 obj_id_str1 = argv[1];
2126 obj_id_str2 = argv[2];
2127 } else
2128 usage_diff();
2130 error = got_repo_open(&repo, repo_path);
2131 free(repo_path);
2132 if (error)
2133 goto done;
2135 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2136 if (error)
2137 goto done;
2139 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2140 if (error)
2141 goto done;
2143 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2144 if (view == NULL) {
2145 error = got_error_from_errno();
2146 goto done;
2148 error = open_diff_view(view, obj1, obj2, repo);
2149 if (error)
2150 goto done;
2151 error = view_loop(view);
2152 done:
2153 got_repo_close(repo);
2154 if (obj1)
2155 got_object_close(obj1);
2156 if (obj2)
2157 got_object_close(obj2);
2158 return error;
2161 __dead static void
2162 usage_blame(void)
2164 endwin();
2165 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2166 getprogname());
2167 exit(1);
2170 struct tog_blame_line {
2171 int annotated;
2172 struct got_object_id *id;
2175 static const struct got_error *
2176 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2177 const char *path, struct tog_blame_line *lines, int nlines,
2178 int blame_complete, int selected_line, int *first_displayed_line,
2179 int *last_displayed_line, int *eof, int max_lines)
2181 const struct got_error *err;
2182 int lineno = 0, nprinted = 0;
2183 char *line;
2184 size_t len;
2185 wchar_t *wline;
2186 int width, wlimit;
2187 struct tog_blame_line *blame_line;
2188 struct got_object_id *prev_id = NULL;
2189 char *id_str;
2191 err = got_object_id_str(&id_str, id);
2192 if (err)
2193 return err;
2195 rewind(f);
2196 werase(view->window);
2198 if (asprintf(&line, "commit: %s", id_str) == -1) {
2199 err = got_error_from_errno();
2200 free(id_str);
2201 return err;
2204 err = format_line(&wline, &width, line, view->ncols);
2205 free(line);
2206 line = NULL;
2207 if (view_needs_focus_indication(view))
2208 wstandout(view->window);
2209 waddwstr(view->window, wline);
2210 if (view_needs_focus_indication(view))
2211 wstandend(view->window);
2212 free(wline);
2213 wline = NULL;
2214 if (width < view->ncols)
2215 waddch(view->window, '\n');
2217 if (asprintf(&line, "[%d/%d] %s%s",
2218 *first_displayed_line - 1 + selected_line, nlines,
2219 blame_complete ? "" : "annotating ", path) == -1) {
2220 free(id_str);
2221 return got_error_from_errno();
2223 free(id_str);
2224 err = format_line(&wline, &width, line, view->ncols);
2225 free(line);
2226 line = NULL;
2227 if (err)
2228 return err;
2229 waddwstr(view->window, wline);
2230 free(wline);
2231 wline = NULL;
2232 if (width < view->ncols)
2233 waddch(view->window, '\n');
2235 *eof = 0;
2236 while (nprinted < max_lines - 2) {
2237 line = parse_next_line(f, &len);
2238 if (line == NULL) {
2239 *eof = 1;
2240 break;
2242 if (++lineno < *first_displayed_line) {
2243 free(line);
2244 continue;
2247 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2248 err = format_line(&wline, &width, line, wlimit);
2249 if (err) {
2250 free(line);
2251 return err;
2254 if (view->focussed && nprinted == selected_line - 1)
2255 wstandout(view->window);
2257 blame_line = &lines[lineno - 1];
2258 if (blame_line->annotated && prev_id &&
2259 got_object_id_cmp(prev_id, blame_line->id) == 0)
2260 waddstr(view->window, " ");
2261 else if (blame_line->annotated) {
2262 char *id_str;
2263 err = got_object_id_str(&id_str, blame_line->id);
2264 if (err) {
2265 free(line);
2266 free(wline);
2267 return err;
2269 wprintw(view->window, "%.8s ", id_str);
2270 free(id_str);
2271 prev_id = blame_line->id;
2272 } else {
2273 waddstr(view->window, "........ ");
2274 prev_id = NULL;
2277 waddwstr(view->window, wline);
2278 while (width < wlimit) {
2279 waddch(view->window, ' ');
2280 width++;
2282 if (view->focussed && nprinted == selected_line - 1)
2283 wstandend(view->window);
2284 if (++nprinted == 1)
2285 *first_displayed_line = lineno;
2286 free(line);
2287 free(wline);
2288 wline = NULL;
2290 *last_displayed_line = lineno;
2292 view_vborder(view);
2294 return NULL;
2297 static const struct got_error *
2298 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2300 const struct got_error *err = NULL;
2301 struct tog_blame_cb_args *a = arg;
2302 struct tog_blame_line *line;
2303 int errcode;
2305 if (nlines != a->nlines ||
2306 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2307 return got_error(GOT_ERR_RANGE);
2309 errcode = pthread_mutex_lock(&tog_mutex);
2310 if (errcode)
2311 return got_error_set_errno(errcode);
2313 if (*a->quit) { /* user has quit the blame view */
2314 err = got_error(GOT_ERR_ITER_COMPLETED);
2315 goto done;
2318 if (lineno == -1)
2319 goto done; /* no change in this commit */
2321 line = &a->lines[lineno - 1];
2322 if (line->annotated)
2323 goto done;
2325 line->id = got_object_id_dup(id);
2326 if (line->id == NULL) {
2327 err = got_error_from_errno();
2328 goto done;
2330 line->annotated = 1;
2331 done:
2332 errcode = pthread_mutex_unlock(&tog_mutex);
2333 if (errcode)
2334 err = got_error_set_errno(errcode);
2335 return err;
2338 static void *
2339 blame_thread(void *arg)
2341 const struct got_error *err;
2342 struct tog_blame_thread_args *ta = arg;
2343 struct tog_blame_cb_args *a = ta->cb_args;
2344 int errcode;
2346 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2347 blame_cb, ta->cb_args);
2349 errcode = pthread_mutex_lock(&tog_mutex);
2350 if (errcode)
2351 return (void *)got_error_set_errno(errcode);
2353 got_repo_close(ta->repo);
2354 ta->repo = NULL;
2355 *ta->complete = 1;
2357 errcode = pthread_mutex_unlock(&tog_mutex);
2358 if (errcode && err == NULL)
2359 err = got_error_set_errno(errcode);
2361 return (void *)err;
2364 static struct got_object_id *
2365 get_selected_commit_id(struct tog_blame_line *lines,
2366 int first_displayed_line, int selected_line)
2368 struct tog_blame_line *line;
2370 line = &lines[first_displayed_line - 1 + selected_line - 1];
2371 if (!line->annotated)
2372 return NULL;
2374 return line->id;
2377 static const struct got_error *
2378 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2379 struct tog_blame_line *lines, int first_displayed_line,
2380 int selected_line, struct got_repository *repo)
2382 const struct got_error *err = NULL;
2383 struct got_commit_object *commit = NULL;
2384 struct got_object_id *selected_id;
2385 struct got_object_qid *pid;
2387 *pobj = NULL;
2388 *obj = NULL;
2390 selected_id = get_selected_commit_id(lines,
2391 first_displayed_line, selected_line);
2392 if (selected_id == NULL)
2393 return NULL;
2395 err = got_object_open(obj, repo, selected_id);
2396 if (err)
2397 goto done;
2399 err = got_object_commit_open(&commit, repo, *obj);
2400 if (err)
2401 goto done;
2403 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2404 if (pid) {
2405 err = got_object_open(pobj, repo, pid->id);
2406 if (err)
2407 goto done;
2409 done:
2410 if (commit)
2411 got_object_commit_close(commit);
2412 return err;
2415 static const struct got_error *
2416 stop_blame(struct tog_blame *blame)
2418 const struct got_error *err = NULL;
2419 int i;
2421 if (blame->thread) {
2422 int errcode;
2423 errcode = pthread_mutex_unlock(&tog_mutex);
2424 if (errcode)
2425 return got_error_set_errno(errcode);
2426 errcode = pthread_join(blame->thread, (void **)&err);
2427 if (errcode)
2428 return got_error_set_errno(errcode);
2429 errcode = pthread_mutex_lock(&tog_mutex);
2430 if (errcode)
2431 return got_error_set_errno(errcode);
2432 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2433 err = NULL;
2434 blame->thread = NULL;
2436 if (blame->thread_args.repo) {
2437 got_repo_close(blame->thread_args.repo);
2438 blame->thread_args.repo = NULL;
2440 if (blame->f) {
2441 fclose(blame->f);
2442 blame->f = NULL;
2444 for (i = 0; i < blame->nlines; i++)
2445 free(blame->lines[i].id);
2446 free(blame->lines);
2447 blame->lines = NULL;
2448 free(blame->cb_args.commit_id);
2449 blame->cb_args.commit_id = NULL;
2451 return err;
2454 static const struct got_error *
2455 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2456 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2457 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2458 struct got_repository *repo)
2460 const struct got_error *err = NULL;
2461 struct got_blob_object *blob = NULL;
2462 struct got_repository *thread_repo = NULL;
2463 struct got_object_id *obj_id = NULL;
2464 struct got_object *obj = NULL;
2466 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2467 if (err)
2468 goto done;
2470 err = got_object_open(&obj, repo, obj_id);
2471 if (err)
2472 goto done;
2474 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2475 err = got_error(GOT_ERR_OBJ_TYPE);
2476 goto done;
2479 err = got_object_blob_open(&blob, repo, obj, 8192);
2480 if (err)
2481 goto done;
2482 blame->f = got_opentemp();
2483 if (blame->f == NULL) {
2484 err = got_error_from_errno();
2485 goto done;
2487 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2488 blame->f, blob);
2489 if (err)
2490 goto done;
2492 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2493 if (blame->lines == NULL) {
2494 err = got_error_from_errno();
2495 goto done;
2498 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2499 if (err)
2500 goto done;
2502 blame->cb_args.view = view;
2503 blame->cb_args.lines = blame->lines;
2504 blame->cb_args.nlines = blame->nlines;
2505 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2506 if (blame->cb_args.commit_id == NULL) {
2507 err = got_error_from_errno();
2508 goto done;
2510 blame->cb_args.quit = done;
2512 blame->thread_args.path = path;
2513 blame->thread_args.repo = thread_repo;
2514 blame->thread_args.cb_args = &blame->cb_args;
2515 blame->thread_args.complete = blame_complete;
2516 *blame_complete = 0;
2518 done:
2519 if (blob)
2520 got_object_blob_close(blob);
2521 free(obj_id);
2522 if (obj)
2523 got_object_close(obj);
2524 if (err)
2525 stop_blame(blame);
2526 return err;
2529 static const struct got_error *
2530 open_blame_view(struct tog_view *view, char *path,
2531 struct got_object_id *commit_id, struct got_repository *repo)
2533 const struct got_error *err = NULL;
2534 struct tog_blame_view_state *s = &view->state.blame;
2536 SIMPLEQ_INIT(&s->blamed_commits);
2538 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2539 if (err)
2540 return err;
2542 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2543 s->first_displayed_line = 1;
2544 s->last_displayed_line = view->nlines;
2545 s->selected_line = 1;
2546 s->blame_complete = 0;
2547 s->path = path;
2548 if (s->path == NULL)
2549 return got_error_from_errno();
2550 s->repo = repo;
2551 s->commit_id = commit_id;
2552 memset(&s->blame, 0, sizeof(s->blame));
2554 view->show = show_blame_view;
2555 view->input = input_blame_view;
2556 view->close = close_blame_view;
2558 return run_blame(&s->blame, view, &s->blame_complete,
2559 &s->first_displayed_line, &s->last_displayed_line,
2560 &s->selected_line, &s->done, &s->eof, s->path,
2561 s->blamed_commit->id, s->repo);
2564 static const struct got_error *
2565 close_blame_view(struct tog_view *view)
2567 const struct got_error *err = NULL;
2568 struct tog_blame_view_state *s = &view->state.blame;
2570 if (s->blame.thread)
2571 err = stop_blame(&s->blame);
2573 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2574 struct got_object_qid *blamed_commit;
2575 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2576 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2577 got_object_qid_free(blamed_commit);
2580 free(s->path);
2582 return err;
2585 static const struct got_error *
2586 show_blame_view(struct tog_view *view)
2588 const struct got_error *err = NULL;
2589 struct tog_blame_view_state *s = &view->state.blame;
2590 int errcode;
2592 if (s->blame.thread == NULL) {
2593 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2594 &s->blame.thread_args);
2595 if (errcode)
2596 return got_error_set_errno(errcode);
2599 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2600 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2601 s->selected_line, &s->first_displayed_line,
2602 &s->last_displayed_line, &s->eof, view->nlines);
2604 view_vborder(view);
2605 return err;
2608 static const struct got_error *
2609 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2610 struct tog_view **focus_view, struct tog_view *view, int ch)
2612 const struct got_error *err = NULL, *thread_err = NULL;
2613 struct got_object *obj = NULL, *pobj = NULL;
2614 struct tog_view *diff_view;
2615 struct tog_blame_view_state *s = &view->state.blame;
2616 int begin_x = 0;
2618 switch (ch) {
2619 case 'q':
2620 s->done = 1;
2621 break;
2622 case 'k':
2623 case KEY_UP:
2624 if (s->selected_line > 1)
2625 s->selected_line--;
2626 else if (s->selected_line == 1 &&
2627 s->first_displayed_line > 1)
2628 s->first_displayed_line--;
2629 break;
2630 case KEY_PPAGE:
2631 if (s->first_displayed_line == 1) {
2632 s->selected_line = 1;
2633 break;
2635 if (s->first_displayed_line > view->nlines - 2)
2636 s->first_displayed_line -=
2637 (view->nlines - 2);
2638 else
2639 s->first_displayed_line = 1;
2640 break;
2641 case 'j':
2642 case KEY_DOWN:
2643 if (s->selected_line < view->nlines - 2 &&
2644 s->first_displayed_line +
2645 s->selected_line <= s->blame.nlines)
2646 s->selected_line++;
2647 else if (s->last_displayed_line <
2648 s->blame.nlines)
2649 s->first_displayed_line++;
2650 break;
2651 case 'b':
2652 case 'p': {
2653 struct got_object_id *id;
2654 id = get_selected_commit_id(s->blame.lines,
2655 s->first_displayed_line, s->selected_line);
2656 if (id == NULL || got_object_id_cmp(id,
2657 s->blamed_commit->id) == 0)
2658 break;
2659 err = open_selected_commit(&pobj, &obj,
2660 s->blame.lines, s->first_displayed_line,
2661 s->selected_line, s->repo);
2662 if (err)
2663 break;
2664 if (pobj == NULL && obj == NULL)
2665 break;
2666 if (ch == 'p' && pobj == NULL)
2667 break;
2668 s->done = 1;
2669 thread_err = stop_blame(&s->blame);
2670 s->done = 0;
2671 if (thread_err)
2672 break;
2673 id = got_object_get_id(ch == 'b' ? obj : pobj);
2674 got_object_close(obj);
2675 obj = NULL;
2676 if (pobj) {
2677 got_object_close(pobj);
2678 pobj = NULL;
2680 err = got_object_qid_alloc(&s->blamed_commit, id);
2681 if (err)
2682 goto done;
2683 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2684 s->blamed_commit, entry);
2685 err = run_blame(&s->blame, view, &s->blame_complete,
2686 &s->first_displayed_line, &s->last_displayed_line,
2687 &s->selected_line, &s->done, &s->eof,
2688 s->path, s->blamed_commit->id, s->repo);
2689 if (err)
2690 break;
2691 break;
2693 case 'B': {
2694 struct got_object_qid *first;
2695 first = SIMPLEQ_FIRST(&s->blamed_commits);
2696 if (!got_object_id_cmp(first->id, s->commit_id))
2697 break;
2698 s->done = 1;
2699 thread_err = stop_blame(&s->blame);
2700 s->done = 0;
2701 if (thread_err)
2702 break;
2703 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2704 got_object_qid_free(s->blamed_commit);
2705 s->blamed_commit =
2706 SIMPLEQ_FIRST(&s->blamed_commits);
2707 err = run_blame(&s->blame, view, &s->blame_complete,
2708 &s->first_displayed_line, &s->last_displayed_line,
2709 &s->selected_line, &s->done, &s->eof, s->path,
2710 s->blamed_commit->id, s->repo);
2711 if (err)
2712 break;
2713 break;
2715 case KEY_ENTER:
2716 case '\r':
2717 err = open_selected_commit(&pobj, &obj,
2718 s->blame.lines, s->first_displayed_line,
2719 s->selected_line, s->repo);
2720 if (err)
2721 break;
2722 if (pobj == NULL && obj == NULL)
2723 break;
2725 if (view_is_parent_view(view))
2726 begin_x = view_split_begin_x(view->begin_x);
2727 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2728 if (diff_view == NULL) {
2729 err = got_error_from_errno();
2730 break;
2732 err = open_diff_view(diff_view, pobj, obj, s->repo);
2733 if (err) {
2734 view_close(diff_view);
2735 break;
2737 if (view_is_parent_view(view)) {
2738 err = view_close_child(view);
2739 if (err)
2740 return err;
2741 err = view_set_child(view, diff_view);
2742 if (err) {
2743 view_close(diff_view);
2744 break;
2746 *focus_view = diff_view;
2747 view->child_focussed = 1;
2748 } else
2749 *new_view = diff_view;
2750 if (pobj) {
2751 got_object_close(pobj);
2752 pobj = NULL;
2754 got_object_close(obj);
2755 obj = NULL;
2756 if (err)
2757 break;
2758 break;
2759 case KEY_NPAGE:
2760 case ' ':
2761 if (s->last_displayed_line >= s->blame.nlines &&
2762 s->selected_line < view->nlines - 2) {
2763 s->selected_line = MIN(s->blame.nlines,
2764 view->nlines - 2);
2765 break;
2767 if (s->last_displayed_line + view->nlines - 2
2768 <= s->blame.nlines)
2769 s->first_displayed_line +=
2770 view->nlines - 2;
2771 else
2772 s->first_displayed_line =
2773 s->blame.nlines -
2774 (view->nlines - 3);
2775 break;
2776 case KEY_RESIZE:
2777 if (s->selected_line > view->nlines - 2) {
2778 s->selected_line = MIN(s->blame.nlines,
2779 view->nlines - 2);
2781 break;
2782 default:
2783 break;
2785 done:
2786 if (pobj)
2787 got_object_close(pobj);
2788 return thread_err ? thread_err : err;
2791 static const struct got_error *
2792 cmd_blame(int argc, char *argv[])
2794 const struct got_error *error;
2795 struct got_repository *repo = NULL;
2796 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2797 struct got_object_id *commit_id = NULL;
2798 char *commit_id_str = NULL;
2799 int ch;
2800 struct tog_view *view;
2802 #ifndef PROFILE
2803 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2804 == -1)
2805 err(1, "pledge");
2806 #endif
2808 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2809 switch (ch) {
2810 case 'c':
2811 commit_id_str = optarg;
2812 break;
2813 case 'r':
2814 repo_path = realpath(optarg, NULL);
2815 if (repo_path == NULL)
2816 err(1, "-r option");
2817 break;
2818 default:
2819 usage();
2820 /* NOTREACHED */
2824 argc -= optind;
2825 argv += optind;
2827 if (argc == 1)
2828 path = argv[0];
2829 else
2830 usage_blame();
2832 cwd = getcwd(NULL, 0);
2833 if (cwd == NULL) {
2834 error = got_error_from_errno();
2835 goto done;
2837 if (repo_path == NULL) {
2838 repo_path = strdup(cwd);
2839 if (repo_path == NULL) {
2840 error = got_error_from_errno();
2841 goto done;
2846 error = got_repo_open(&repo, repo_path);
2847 if (error != NULL)
2848 return error;
2850 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2851 if (error != NULL)
2852 goto done;
2854 if (commit_id_str == NULL) {
2855 struct got_reference *head_ref;
2856 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2857 if (error != NULL)
2858 goto done;
2859 error = got_ref_resolve(&commit_id, repo, head_ref);
2860 got_ref_close(head_ref);
2861 } else {
2862 struct got_object *obj;
2863 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2864 if (error != NULL)
2865 goto done;
2866 commit_id = got_object_id_dup(got_object_get_id(obj));
2867 if (commit_id == NULL)
2868 error = got_error_from_errno();
2869 got_object_close(obj);
2871 if (error != NULL)
2872 goto done;
2874 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2875 if (view == NULL) {
2876 error = got_error_from_errno();
2877 goto done;
2879 error = open_blame_view(view, in_repo_path, commit_id, repo);
2880 if (error)
2881 goto done;
2882 error = view_loop(view);
2883 done:
2884 free(repo_path);
2885 free(cwd);
2886 free(commit_id);
2887 if (repo)
2888 got_repo_close(repo);
2889 return error;
2892 static const struct got_error *
2893 draw_tree_entries(struct tog_view *view,
2894 struct got_tree_entry **first_displayed_entry,
2895 struct got_tree_entry **last_displayed_entry,
2896 struct got_tree_entry **selected_entry, int *ndisplayed,
2897 const char *label, int show_ids, const char *parent_path,
2898 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2900 const struct got_error *err = NULL;
2901 struct got_tree_entry *te;
2902 wchar_t *wline;
2903 int width, n;
2905 *ndisplayed = 0;
2907 werase(view->window);
2909 if (limit == 0)
2910 return NULL;
2912 err = format_line(&wline, &width, label, view->ncols);
2913 if (err)
2914 return err;
2915 if (view_needs_focus_indication(view))
2916 wstandout(view->window);
2917 waddwstr(view->window, wline);
2918 if (view_needs_focus_indication(view))
2919 wstandend(view->window);
2920 free(wline);
2921 wline = NULL;
2922 if (width < view->ncols)
2923 waddch(view->window, '\n');
2924 if (--limit <= 0)
2925 return NULL;
2926 err = format_line(&wline, &width, parent_path, view->ncols);
2927 if (err)
2928 return err;
2929 waddwstr(view->window, wline);
2930 free(wline);
2931 wline = NULL;
2932 if (width < view->ncols)
2933 waddch(view->window, '\n');
2934 if (--limit <= 0)
2935 return NULL;
2936 waddch(view->window, '\n');
2937 if (--limit <= 0)
2938 return NULL;
2940 te = SIMPLEQ_FIRST(&entries->head);
2941 if (*first_displayed_entry == NULL) {
2942 if (selected == 0) {
2943 if (view->focussed)
2944 wstandout(view->window);
2945 *selected_entry = NULL;
2947 waddstr(view->window, " ..\n"); /* parent directory */
2948 if (selected == 0 && view->focussed)
2949 wstandend(view->window);
2950 (*ndisplayed)++;
2951 if (--limit <= 0)
2952 return NULL;
2953 n = 1;
2954 } else {
2955 n = 0;
2956 while (te != *first_displayed_entry)
2957 te = SIMPLEQ_NEXT(te, entry);
2960 while (te) {
2961 char *line = NULL, *id_str = NULL;
2963 if (show_ids) {
2964 err = got_object_id_str(&id_str, te->id);
2965 if (err)
2966 return got_error_from_errno();
2968 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2969 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2970 free(id_str);
2971 return got_error_from_errno();
2973 free(id_str);
2974 err = format_line(&wline, &width, line, view->ncols);
2975 if (err) {
2976 free(line);
2977 break;
2979 if (n == selected) {
2980 if (view->focussed)
2981 wstandout(view->window);
2982 *selected_entry = te;
2984 waddwstr(view->window, wline);
2985 if (width < view->ncols)
2986 waddch(view->window, '\n');
2987 if (n == selected && view->focussed)
2988 wstandend(view->window);
2989 free(line);
2990 free(wline);
2991 wline = NULL;
2992 n++;
2993 (*ndisplayed)++;
2994 *last_displayed_entry = te;
2995 if (--limit <= 0)
2996 break;
2997 te = SIMPLEQ_NEXT(te, entry);
3000 return err;
3003 static void
3004 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3005 const struct got_tree_entries *entries, int isroot)
3007 struct got_tree_entry *te, *prev;
3008 int i;
3010 if (*first_displayed_entry == NULL)
3011 return;
3013 te = SIMPLEQ_FIRST(&entries->head);
3014 if (*first_displayed_entry == te) {
3015 if (!isroot)
3016 *first_displayed_entry = NULL;
3017 return;
3020 /* XXX this is stupid... switch to TAILQ? */
3021 for (i = 0; i < maxscroll; i++) {
3022 while (te != *first_displayed_entry) {
3023 prev = te;
3024 te = SIMPLEQ_NEXT(te, entry);
3026 *first_displayed_entry = prev;
3027 te = SIMPLEQ_FIRST(&entries->head);
3029 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3030 *first_displayed_entry = NULL;
3033 static void
3034 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3035 struct got_tree_entry *last_displayed_entry,
3036 const struct got_tree_entries *entries)
3038 struct got_tree_entry *next;
3039 int n = 0;
3041 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
3042 return;
3044 if (*first_displayed_entry)
3045 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3046 else
3047 next = SIMPLEQ_FIRST(&entries->head);
3048 while (next) {
3049 *first_displayed_entry = next;
3050 if (++n >= maxscroll)
3051 break;
3052 next = SIMPLEQ_NEXT(next, entry);
3056 static const struct got_error *
3057 tree_entry_path(char **path, struct tog_parent_trees *parents,
3058 struct got_tree_entry *te)
3060 const struct got_error *err = NULL;
3061 struct tog_parent_tree *pt;
3062 size_t len = 2; /* for leading slash and NUL */
3064 TAILQ_FOREACH(pt, parents, entry)
3065 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3066 if (te)
3067 len += strlen(te->name);
3069 *path = calloc(1, len);
3070 if (path == NULL)
3071 return got_error_from_errno();
3073 (*path)[0] = '/';
3074 pt = TAILQ_LAST(parents, tog_parent_trees);
3075 while (pt) {
3076 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3077 err = got_error(GOT_ERR_NO_SPACE);
3078 goto done;
3080 if (strlcat(*path, "/", len) >= len) {
3081 err = got_error(GOT_ERR_NO_SPACE);
3082 goto done;
3084 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3086 if (te) {
3087 if (strlcat(*path, te->name, len) >= len) {
3088 err = got_error(GOT_ERR_NO_SPACE);
3089 goto done;
3092 done:
3093 if (err) {
3094 free(*path);
3095 *path = NULL;
3097 return err;
3100 static const struct got_error *
3101 blame_tree_entry(struct tog_view **new_view, int begin_x,
3102 struct got_tree_entry *te, struct tog_parent_trees *parents,
3103 struct got_object_id *commit_id, struct got_repository *repo)
3105 const struct got_error *err = NULL;
3106 char *path;
3107 struct tog_view *blame_view;
3109 err = tree_entry_path(&path, parents, te);
3110 if (err)
3111 return err;
3113 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3114 if (blame_view == NULL)
3115 return got_error_from_errno();
3117 err = open_blame_view(blame_view, path, commit_id, repo);
3118 if (err) {
3119 view_close(blame_view);
3120 free(path);
3121 } else
3122 *new_view = blame_view;
3123 return err;
3126 static const struct got_error *
3127 log_tree_entry(struct tog_view **new_view, int begin_x,
3128 struct got_tree_entry *te, struct tog_parent_trees *parents,
3129 struct got_object_id *commit_id, struct got_repository *repo)
3131 struct tog_view *log_view;
3132 const struct got_error *err = NULL;
3133 char *path;
3135 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3136 if (log_view == NULL)
3137 return got_error_from_errno();
3139 err = tree_entry_path(&path, parents, te);
3140 if (err)
3141 return err;
3143 err = open_log_view(log_view, commit_id, repo, path, 0);
3144 if (err)
3145 view_close(log_view);
3146 else
3147 *new_view = log_view;
3148 free(path);
3149 return err;
3152 static const struct got_error *
3153 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3154 struct got_object_id *commit_id, struct got_repository *repo)
3156 const struct got_error *err = NULL;
3157 char *commit_id_str = NULL;
3158 struct tog_tree_view_state *s = &view->state.tree;
3160 TAILQ_INIT(&s->parents);
3162 err = got_object_id_str(&commit_id_str, commit_id);
3163 if (err != NULL)
3164 goto done;
3166 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3167 err = got_error_from_errno();
3168 goto done;
3171 s->root = s->tree = root;
3172 s->entries = got_object_tree_get_entries(root);
3173 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3174 s->commit_id = got_object_id_dup(commit_id);
3175 if (s->commit_id == NULL) {
3176 err = got_error_from_errno();
3177 goto done;
3179 s->repo = repo;
3181 view->show = show_tree_view;
3182 view->input = input_tree_view;
3183 view->close = close_tree_view;
3184 done:
3185 free(commit_id_str);
3186 if (err) {
3187 free(s->tree_label);
3188 s->tree_label = NULL;
3190 return err;
3193 static const struct got_error *
3194 close_tree_view(struct tog_view *view)
3196 struct tog_tree_view_state *s = &view->state.tree;
3198 free(s->tree_label);
3199 s->tree_label = NULL;
3200 free(s->commit_id);
3201 s->commit_id = NULL;
3202 while (!TAILQ_EMPTY(&s->parents)) {
3203 struct tog_parent_tree *parent;
3204 parent = TAILQ_FIRST(&s->parents);
3205 TAILQ_REMOVE(&s->parents, parent, entry);
3206 free(parent);
3209 if (s->tree != s->root)
3210 got_object_tree_close(s->tree);
3211 got_object_tree_close(s->root);
3213 return NULL;
3216 static const struct got_error *
3217 show_tree_view(struct tog_view *view)
3219 const struct got_error *err = NULL;
3220 struct tog_tree_view_state *s = &view->state.tree;
3221 char *parent_path;
3223 err = tree_entry_path(&parent_path, &s->parents, NULL);
3224 if (err)
3225 return err;
3227 err = draw_tree_entries(view, &s->first_displayed_entry,
3228 &s->last_displayed_entry, &s->selected_entry,
3229 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3230 s->entries, s->selected, view->nlines, s->tree == s->root);
3231 free(parent_path);
3233 view_vborder(view);
3234 return err;
3237 static const struct got_error *
3238 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3239 struct tog_view **focus_view, struct tog_view *view, int ch)
3241 const struct got_error *err = NULL;
3242 struct tog_tree_view_state *s = &view->state.tree;
3243 struct tog_view *log_view;
3244 int begin_x = 0;
3246 switch (ch) {
3247 case 'i':
3248 s->show_ids = !s->show_ids;
3249 break;
3250 case 'l':
3251 if (!s->selected_entry)
3252 break;
3253 if (view_is_parent_view(view))
3254 begin_x = view_split_begin_x(view->begin_x);
3255 err = log_tree_entry(&log_view, begin_x,
3256 s->selected_entry, &s->parents,
3257 s->commit_id, s->repo);
3258 if (view_is_parent_view(view)) {
3259 err = view_close_child(view);
3260 if (err)
3261 return err;
3262 err = view_set_child(view, log_view);
3263 if (err) {
3264 view_close(log_view);
3265 break;
3267 *focus_view = log_view;
3268 view->child_focussed = 1;
3269 } else
3270 *new_view = log_view;
3271 break;
3272 case 'k':
3273 case KEY_UP:
3274 if (s->selected > 0)
3275 s->selected--;
3276 if (s->selected > 0)
3277 break;
3278 tree_scroll_up(&s->first_displayed_entry, 1,
3279 s->entries, s->tree == s->root);
3280 break;
3281 case KEY_PPAGE:
3282 s->selected = 0;
3283 if (SIMPLEQ_FIRST(&s->entries->head) ==
3284 s->first_displayed_entry) {
3285 if (s->tree != s->root)
3286 s->first_displayed_entry = NULL;
3287 break;
3289 tree_scroll_up(&s->first_displayed_entry,
3290 view->nlines, s->entries,
3291 s->tree == s->root);
3292 break;
3293 case 'j':
3294 case KEY_DOWN:
3295 if (s->selected < s->ndisplayed - 1) {
3296 s->selected++;
3297 break;
3299 tree_scroll_down(&s->first_displayed_entry, 1,
3300 s->last_displayed_entry, s->entries);
3301 break;
3302 case KEY_NPAGE:
3303 tree_scroll_down(&s->first_displayed_entry,
3304 view->nlines, s->last_displayed_entry,
3305 s->entries);
3306 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3307 entry))
3308 break;
3309 /* can't scroll any further; move cursor down */
3310 if (s->selected < s->ndisplayed - 1)
3311 s->selected = s->ndisplayed - 1;
3312 break;
3313 case KEY_ENTER:
3314 case '\r':
3315 if (s->selected_entry == NULL) {
3316 struct tog_parent_tree *parent;
3317 case KEY_BACKSPACE:
3318 /* user selected '..' */
3319 if (s->tree == s->root)
3320 break;
3321 parent = TAILQ_FIRST(&s->parents);
3322 TAILQ_REMOVE(&s->parents, parent,
3323 entry);
3324 got_object_tree_close(s->tree);
3325 s->tree = parent->tree;
3326 s->entries =
3327 got_object_tree_get_entries(s->tree);
3328 s->first_displayed_entry =
3329 parent->first_displayed_entry;
3330 s->selected_entry =
3331 parent->selected_entry;
3332 s->selected = parent->selected;
3333 free(parent);
3334 } else if (S_ISDIR(s->selected_entry->mode)) {
3335 struct tog_parent_tree *parent;
3336 struct got_tree_object *child;
3337 err = got_object_open_as_tree(&child,
3338 s->repo, s->selected_entry->id);
3339 if (err)
3340 break;
3341 parent = calloc(1, sizeof(*parent));
3342 if (parent == NULL) {
3343 err = got_error_from_errno();
3344 break;
3346 parent->tree = s->tree;
3347 parent->first_displayed_entry =
3348 s->first_displayed_entry;
3349 parent->selected_entry = s->selected_entry;
3350 parent->selected = s->selected;
3351 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3352 s->tree = child;
3353 s->entries =
3354 got_object_tree_get_entries(s->tree);
3355 s->selected = 0;
3356 s->first_displayed_entry = NULL;
3357 } else if (S_ISREG(s->selected_entry->mode)) {
3358 struct tog_view *blame_view;
3359 int begin_x = view_is_parent_view(view) ?
3360 view_split_begin_x(view->begin_x) : 0;
3362 err = blame_tree_entry(&blame_view, begin_x,
3363 s->selected_entry, &s->parents, s->commit_id,
3364 s->repo);
3365 if (err)
3366 break;
3367 if (view_is_parent_view(view)) {
3368 err = view_close_child(view);
3369 if (err)
3370 return err;
3371 err = view_set_child(view, blame_view);
3372 if (err) {
3373 view_close(blame_view);
3374 break;
3376 *focus_view = blame_view;
3377 view->child_focussed = 1;
3378 } else
3379 *new_view = blame_view;
3381 break;
3382 case KEY_RESIZE:
3383 if (s->selected > view->nlines)
3384 s->selected = s->ndisplayed - 1;
3385 break;
3386 default:
3387 break;
3390 return err;
3393 __dead static void
3394 usage_tree(void)
3396 endwin();
3397 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3398 getprogname());
3399 exit(1);
3402 static const struct got_error *
3403 cmd_tree(int argc, char *argv[])
3405 const struct got_error *error;
3406 struct got_repository *repo = NULL;
3407 char *repo_path = NULL;
3408 struct got_object_id *commit_id = NULL;
3409 char *commit_id_arg = NULL;
3410 struct got_commit_object *commit = NULL;
3411 struct got_tree_object *tree = NULL;
3412 int ch;
3413 struct tog_view *view;
3415 #ifndef PROFILE
3416 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3417 == -1)
3418 err(1, "pledge");
3419 #endif
3421 while ((ch = getopt(argc, argv, "c:")) != -1) {
3422 switch (ch) {
3423 case 'c':
3424 commit_id_arg = optarg;
3425 break;
3426 default:
3427 usage();
3428 /* NOTREACHED */
3432 argc -= optind;
3433 argv += optind;
3435 if (argc == 0) {
3436 repo_path = getcwd(NULL, 0);
3437 if (repo_path == NULL)
3438 return got_error_from_errno();
3439 } else if (argc == 1) {
3440 repo_path = realpath(argv[0], NULL);
3441 if (repo_path == NULL)
3442 return got_error_from_errno();
3443 } else
3444 usage_log();
3446 error = got_repo_open(&repo, repo_path);
3447 free(repo_path);
3448 if (error != NULL)
3449 return error;
3451 if (commit_id_arg == NULL) {
3452 error = get_head_commit_id(&commit_id, repo);
3453 if (error != NULL)
3454 goto done;
3455 } else {
3456 struct got_object *obj;
3457 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3458 if (error == NULL) {
3459 commit_id = got_object_id_dup(got_object_get_id(obj));
3460 if (commit_id == NULL)
3461 error = got_error_from_errno();
3464 if (error != NULL)
3465 goto done;
3467 error = got_object_open_as_commit(&commit, repo, commit_id);
3468 if (error != NULL)
3469 goto done;
3471 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3472 if (error != NULL)
3473 goto done;
3475 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3476 if (view == NULL) {
3477 error = got_error_from_errno();
3478 goto done;
3480 error = open_tree_view(view, tree, commit_id, repo);
3481 if (error)
3482 goto done;
3483 error = view_loop(view);
3484 done:
3485 free(commit_id);
3486 if (commit)
3487 got_object_commit_close(commit);
3488 if (tree)
3489 got_object_tree_close(tree);
3490 if (repo)
3491 got_repo_close(repo);
3492 return error;
3495 static void
3496 init_curses(void)
3498 initscr();
3499 cbreak();
3500 halfdelay(1); /* Do fast refresh while initial view is loading. */
3501 noecho();
3502 nonl();
3503 intrflush(stdscr, FALSE);
3504 keypad(stdscr, TRUE);
3505 curs_set(0);
3506 signal(SIGWINCH, tog_sigwinch);
3509 __dead static void
3510 usage(void)
3512 int i;
3514 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3515 "Available commands:\n", getprogname());
3516 for (i = 0; i < nitems(tog_commands); i++) {
3517 struct tog_cmd *cmd = &tog_commands[i];
3518 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3520 exit(1);
3523 static char **
3524 make_argv(const char *arg0, const char *arg1)
3526 char **argv;
3527 int argc = (arg1 == NULL ? 1 : 2);
3529 argv = calloc(argc, sizeof(char *));
3530 if (argv == NULL)
3531 err(1, "calloc");
3532 argv[0] = strdup(arg0);
3533 if (argv[0] == NULL)
3534 err(1, "calloc");
3535 if (arg1) {
3536 argv[1] = strdup(arg1);
3537 if (argv[1] == NULL)
3538 err(1, "calloc");
3541 return argv;
3544 int
3545 main(int argc, char *argv[])
3547 const struct got_error *error = NULL;
3548 struct tog_cmd *cmd = NULL;
3549 int ch, hflag = 0;
3550 char **cmd_argv = NULL;
3552 setlocale(LC_ALL, "");
3554 while ((ch = getopt(argc, argv, "h")) != -1) {
3555 switch (ch) {
3556 case 'h':
3557 hflag = 1;
3558 break;
3559 default:
3560 usage();
3561 /* NOTREACHED */
3565 argc -= optind;
3566 argv += optind;
3567 optind = 0;
3568 optreset = 1;
3570 if (argc == 0) {
3571 if (hflag)
3572 usage();
3573 /* Build an argument vector which runs a default command. */
3574 cmd = &tog_commands[0];
3575 cmd_argv = make_argv(cmd->name, NULL);
3576 argc = 1;
3577 } else {
3578 int i;
3580 /* Did the user specific a command? */
3581 for (i = 0; i < nitems(tog_commands); i++) {
3582 if (strncmp(tog_commands[i].name, argv[0],
3583 strlen(argv[0])) == 0) {
3584 cmd = &tog_commands[i];
3585 if (hflag)
3586 tog_commands[i].cmd_usage();
3587 break;
3590 if (cmd == NULL) {
3591 /* Did the user specify a repository? */
3592 char *repo_path = realpath(argv[0], NULL);
3593 if (repo_path) {
3594 struct got_repository *repo;
3595 error = got_repo_open(&repo, repo_path);
3596 if (error == NULL)
3597 got_repo_close(repo);
3598 } else
3599 error = got_error_from_errno();
3600 if (error) {
3601 if (hflag) {
3602 fprintf(stderr, "%s: '%s' is not a "
3603 "known command\n", getprogname(),
3604 argv[0]);
3605 usage();
3607 fprintf(stderr, "%s: '%s' is neither a known "
3608 "command nor a path to a repository\n",
3609 getprogname(), argv[0]);
3610 free(repo_path);
3611 return 1;
3613 cmd = &tog_commands[0];
3614 cmd_argv = make_argv(cmd->name, repo_path);
3615 argc = 2;
3616 free(repo_path);
3620 init_curses();
3622 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3623 if (error)
3624 goto done;
3625 done:
3626 endwin();
3627 free(cmd_argv);
3628 if (error)
3629 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3630 return 0;