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;
878 time_t committer_time;
880 committer_time = got_object_commit_get_committer_time(commit);
881 if (localtime_r(&committer_time, &tm) == NULL)
882 return got_error_from_errno();
883 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
884 >= sizeof(datebuf))
885 return got_error(GOT_ERR_NO_SPACE);
887 if (avail < date_display_cols)
888 limit = MIN(sizeof(datebuf) - 1, avail);
889 else
890 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
891 waddnstr(view->window, datebuf, limit);
892 col = limit + 1;
893 if (col > avail)
894 goto done;
896 author0 = strdup(got_object_commit_get_author(commit));
897 if (author0 == NULL) {
898 err = got_error_from_errno();
899 goto done;
901 author = author0;
902 smallerthan = strchr(author, '<');
903 if (smallerthan)
904 *smallerthan = '\0';
905 else {
906 char *at = strchr(author, '@');
907 if (at)
908 *at = '\0';
910 limit = avail - col;
911 err = format_line(&wauthor, &author_width, author, limit);
912 if (err)
913 goto done;
914 waddwstr(view->window, wauthor);
915 col += author_width;
916 while (col <= avail && author_width < author_display_cols + 1) {
917 waddch(view->window, ' ');
918 col++;
919 author_width++;
921 if (col > avail)
922 goto done;
924 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
925 if (logmsg0 == NULL) {
926 err = got_error_from_errno();
927 goto done;
929 logmsg = logmsg0;
930 while (*logmsg == '\n')
931 logmsg++;
932 newline = strchr(logmsg, '\n');
933 if (newline)
934 *newline = '\0';
935 limit = avail - col;
936 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
937 if (err)
938 goto done;
939 waddwstr(view->window, wlogmsg);
940 col += logmsg_width;
941 while (col <= avail) {
942 waddch(view->window, ' ');
943 col++;
945 done:
946 free(logmsg0);
947 free(wlogmsg);
948 free(author0);
949 free(wauthor);
950 free(line);
951 return err;
954 static struct commit_queue_entry *
955 alloc_commit_queue_entry(struct got_commit_object *commit,
956 struct got_object_id *id)
958 struct commit_queue_entry *entry;
960 entry = calloc(1, sizeof(*entry));
961 if (entry == NULL)
962 return NULL;
964 entry->id = id;
965 entry->commit = commit;
966 return entry;
969 static void
970 pop_commit(struct commit_queue *commits)
972 struct commit_queue_entry *entry;
974 entry = TAILQ_FIRST(&commits->head);
975 TAILQ_REMOVE(&commits->head, entry, entry);
976 got_object_commit_close(entry->commit);
977 commits->ncommits--;
978 /* Don't free entry->id! It is owned by the commit graph. */
979 free(entry);
982 static void
983 free_commits(struct commit_queue *commits)
985 while (!TAILQ_EMPTY(&commits->head))
986 pop_commit(commits);
989 static const struct got_error *
990 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
991 int minqueue, struct got_repository *repo, const char *path)
993 const struct got_error *err = NULL;
994 int nqueued = 0;
996 /*
997 * We keep all commits open throughout the lifetime of the log
998 * view in order to avoid having to re-fetch commits from disk
999 * while updating the display.
1001 while (nqueued < minqueue) {
1002 struct got_object_id *id;
1003 struct got_commit_object *commit;
1004 struct commit_queue_entry *entry;
1005 int errcode;
1007 err = got_commit_graph_iter_next(&id, graph);
1008 if (err) {
1009 if (err->code != GOT_ERR_ITER_NEED_MORE)
1010 break;
1011 err = got_commit_graph_fetch_commits(graph,
1012 minqueue, repo);
1013 if (err)
1014 return err;
1015 continue;
1018 if (id == NULL)
1019 break;
1021 err = got_object_open_as_commit(&commit, repo, id);
1022 if (err)
1023 break;
1024 entry = alloc_commit_queue_entry(commit, id);
1025 if (entry == NULL) {
1026 err = got_error_from_errno();
1027 break;
1030 errcode = pthread_mutex_lock(&tog_mutex);
1031 if (errcode) {
1032 err = got_error_set_errno(errcode);
1033 break;
1036 entry->idx = commits->ncommits;
1037 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1038 nqueued++;
1039 commits->ncommits++;
1041 errcode = pthread_mutex_unlock(&tog_mutex);
1042 if (errcode && err == NULL)
1043 err = got_error_set_errno(errcode);
1046 return err;
1049 static const struct got_error *
1050 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1052 const struct got_error *err = NULL;
1053 struct got_reference *head_ref;
1055 *head_id = NULL;
1057 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1058 if (err)
1059 return err;
1061 err = got_ref_resolve(head_id, repo, head_ref);
1062 got_ref_close(head_ref);
1063 if (err) {
1064 *head_id = NULL;
1065 return err;
1068 return NULL;
1071 static const struct got_error *
1072 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1073 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1074 struct commit_queue *commits, int selected_idx, int limit,
1075 const char *path, int commits_needed)
1077 const struct got_error *err = NULL;
1078 struct commit_queue_entry *entry;
1079 int ncommits, width;
1080 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1081 wchar_t *wline;
1083 entry = first;
1084 ncommits = 0;
1085 while (entry) {
1086 if (ncommits == selected_idx) {
1087 *selected = entry;
1088 break;
1090 entry = TAILQ_NEXT(entry, entry);
1091 ncommits++;
1094 if (*selected) {
1095 err = got_object_id_str(&id_str, (*selected)->id);
1096 if (err)
1097 return err;
1100 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1101 entry ? entry->idx + 1 : 0, commits->ncommits,
1102 commits_needed == 0 ? "" : " loading...") == -1)
1103 return got_error_from_errno();
1105 if (path && strcmp(path, "/") != 0) {
1106 if (asprintf(&header, "commit: %s %s%s",
1107 id_str ? id_str : "........................................",
1108 path, ncommits_str) == -1) {
1109 err = got_error_from_errno();
1110 header = NULL;
1111 goto done;
1113 } else if (asprintf(&header, "commit: %s%s",
1114 id_str ? id_str : "........................................",
1115 ncommits_str) == -1) {
1116 err = got_error_from_errno();
1117 header = NULL;
1118 goto done;
1120 err = format_line(&wline, &width, header, view->ncols);
1121 if (err)
1122 goto done;
1124 werase(view->window);
1126 if (view_needs_focus_indication(view))
1127 wstandout(view->window);
1128 waddwstr(view->window, wline);
1129 while (width < view->ncols) {
1130 waddch(view->window, ' ');
1131 width++;
1133 if (view_needs_focus_indication(view))
1134 wstandend(view->window);
1135 free(wline);
1136 if (limit <= 1)
1137 goto done;
1139 entry = first;
1140 *last = first;
1141 ncommits = 0;
1142 while (entry) {
1143 if (ncommits >= limit - 1)
1144 break;
1145 if (view->focussed && ncommits == selected_idx)
1146 wstandout(view->window);
1147 err = draw_commit(view, entry->commit, entry->id);
1148 if (view->focussed && ncommits == selected_idx)
1149 wstandend(view->window);
1150 if (err)
1151 break;
1152 ncommits++;
1153 *last = entry;
1154 entry = TAILQ_NEXT(entry, entry);
1157 view_vborder(view);
1158 done:
1159 free(id_str);
1160 free(ncommits_str);
1161 free(header);
1162 return err;
1165 static void
1166 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1167 struct commit_queue *commits)
1169 struct commit_queue_entry *entry;
1170 int nscrolled = 0;
1172 entry = TAILQ_FIRST(&commits->head);
1173 if (*first_displayed_entry == entry)
1174 return;
1176 entry = *first_displayed_entry;
1177 while (entry && nscrolled < maxscroll) {
1178 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1179 if (entry) {
1180 *first_displayed_entry = entry;
1181 nscrolled++;
1186 static const struct got_error *
1187 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1188 struct commit_queue_entry **last_displayed_entry,
1189 struct commit_queue *commits, int *log_complete, int *commits_needed,
1190 pthread_cond_t *need_commits)
1192 const struct got_error *err = NULL;
1193 struct commit_queue_entry *pentry;
1194 int nscrolled = 0;
1196 if (*last_displayed_entry == NULL)
1197 return NULL;
1199 do {
1200 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1201 if (pentry == NULL) {
1202 int errcode;
1203 if (*log_complete)
1204 return NULL;
1205 *commits_needed = maxscroll + 20;
1206 errcode = pthread_cond_signal(need_commits);
1207 if (errcode)
1208 return got_error_set_errno(errcode);
1209 return NULL;
1211 *last_displayed_entry = pentry;
1213 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1214 if (pentry == NULL)
1215 break;
1216 *first_displayed_entry = pentry;
1217 } while (++nscrolled < maxscroll);
1219 return err;
1222 static const struct got_error *
1223 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1224 struct got_object_id *commit_id, struct got_commit_object *commit,
1225 struct got_repository *repo)
1227 const struct got_error *err;
1228 struct got_object *obj1 = NULL, *obj2 = NULL;
1229 struct got_object_qid *parent_id;
1230 struct tog_view *diff_view;
1232 err = got_object_open(&obj2, repo, commit_id);
1233 if (err)
1234 return err;
1236 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1237 if (parent_id) {
1238 err = got_object_open(&obj1, repo, parent_id->id);
1239 if (err)
1240 goto done;
1243 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1244 if (diff_view == NULL) {
1245 err = got_error_from_errno();
1246 goto done;
1249 err = open_diff_view(diff_view, obj1, obj2, repo);
1250 if (err == NULL)
1251 *new_view = diff_view;
1252 done:
1253 if (obj1)
1254 got_object_close(obj1);
1255 if (obj2)
1256 got_object_close(obj2);
1257 return err;
1260 static const struct got_error *
1261 browse_commit(struct tog_view **new_view, int begin_x,
1262 struct commit_queue_entry *entry, struct got_repository *repo)
1264 const struct got_error *err = NULL;
1265 struct got_tree_object *tree;
1266 struct tog_view *tree_view;
1268 err = got_object_open_as_tree(&tree, repo,
1269 got_object_commit_get_tree_id(entry->commit));
1270 if (err)
1271 return err;
1273 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1274 if (tree_view == NULL)
1275 return got_error_from_errno();
1277 err = open_tree_view(tree_view, tree, entry->id, repo);
1278 if (err)
1279 got_object_tree_close(tree);
1280 else
1281 *new_view = tree_view;
1282 return err;
1285 static void *
1286 log_thread(void *arg)
1288 const struct got_error *err = NULL;
1289 int errcode = 0;
1290 struct tog_log_thread_args *a = arg;
1291 int done = 0;
1293 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1294 if (err)
1295 return (void *)err;
1297 while (!done && !err) {
1298 err = queue_commits(a->graph, a->commits, 1, a->repo,
1299 a->in_repo_path);
1300 if (err) {
1301 if (err->code != GOT_ERR_ITER_COMPLETED)
1302 return (void *)err;
1303 err = NULL;
1304 done = 1;
1305 } else if (a->commits_needed > 0)
1306 a->commits_needed--;
1308 errcode = pthread_mutex_lock(&tog_mutex);
1309 if (errcode)
1310 return (void *)got_error_set_errno(errcode);
1312 if (done)
1313 a->log_complete = 1;
1314 else if (*a->quit) {
1315 done = 1;
1316 a->log_complete = 1;
1317 } else if (*a->first_displayed_entry == NULL) {
1318 *a->first_displayed_entry =
1319 TAILQ_FIRST(&a->commits->head);
1320 *a->selected_entry = *a->first_displayed_entry;
1323 if (done)
1324 a->commits_needed = 0;
1325 else if (a->commits_needed == 0) {
1326 errcode = pthread_cond_wait(&a->need_commits,
1327 &tog_mutex);
1328 if (errcode)
1329 err = got_error_set_errno(errcode);
1332 errcode = pthread_mutex_unlock(&tog_mutex);
1333 if (errcode && err == NULL)
1334 err = got_error_set_errno(errcode);
1336 return (void *)err;
1339 static const struct got_error *
1340 stop_log_thread(struct tog_log_view_state *s)
1342 const struct got_error *err = NULL;
1343 int errcode;
1345 if (s->thread) {
1346 s->quit = 1;
1347 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1348 if (errcode)
1349 return got_error_set_errno(errcode);
1350 errcode = pthread_mutex_unlock(&tog_mutex);
1351 if (errcode)
1352 return got_error_set_errno(errcode);
1353 errcode = pthread_join(s->thread, (void **)&err);
1354 if (errcode)
1355 return got_error_set_errno(errcode);
1356 errcode = pthread_mutex_lock(&tog_mutex);
1357 if (errcode)
1358 return got_error_set_errno(errcode);
1359 s->thread = NULL;
1362 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1363 if (errcode && err == NULL)
1364 err = got_error_set_errno(errcode);
1366 if (s->thread_args.repo) {
1367 got_repo_close(s->thread_args.repo);
1368 s->thread_args.repo = NULL;
1371 if (s->thread_args.graph) {
1372 got_commit_graph_close(s->thread_args.graph);
1373 s->thread_args.graph = NULL;
1376 return err;
1379 static const struct got_error *
1380 close_log_view(struct tog_view *view)
1382 const struct got_error *err = NULL;
1383 struct tog_log_view_state *s = &view->state.log;
1385 err = stop_log_thread(s);
1386 free_commits(&s->commits);
1387 free(s->in_repo_path);
1388 s->in_repo_path = NULL;
1389 free(s->start_id);
1390 s->start_id = NULL;
1391 return err;
1394 static const struct got_error *
1395 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1396 struct got_repository *repo, const char *path, int check_disk)
1398 const struct got_error *err = NULL;
1399 struct tog_log_view_state *s = &view->state.log;
1400 struct got_repository *thread_repo = NULL;
1401 struct got_commit_graph *thread_graph = NULL;
1402 int errcode;
1404 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1405 if (err != NULL)
1406 goto done;
1408 /* The commit queue only contains commits being displayed. */
1409 TAILQ_INIT(&s->commits.head);
1410 s->commits.ncommits = 0;
1412 s->repo = repo;
1413 s->start_id = got_object_id_dup(start_id);
1414 if (s->start_id == NULL) {
1415 err = got_error_from_errno();
1416 goto done;
1419 view->show = show_log_view;
1420 view->input = input_log_view;
1421 view->close = close_log_view;
1423 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1424 if (err)
1425 goto done;
1426 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1427 0, thread_repo);
1428 if (err)
1429 goto done;
1431 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1432 if (errcode) {
1433 err = got_error_set_errno(errcode);
1434 goto done;
1437 s->thread_args.commits_needed = view->nlines;
1438 s->thread_args.graph = thread_graph;
1439 s->thread_args.commits = &s->commits;
1440 s->thread_args.in_repo_path = s->in_repo_path;
1441 s->thread_args.start_id = s->start_id;
1442 s->thread_args.repo = thread_repo;
1443 s->thread_args.log_complete = 0;
1444 s->thread_args.quit = &s->quit;
1445 s->thread_args.view = view;
1446 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1447 s->thread_args.selected_entry = &s->selected_entry;
1448 done:
1449 if (err)
1450 close_log_view(view);
1451 return err;
1454 static const struct got_error *
1455 show_log_view(struct tog_view *view)
1457 struct tog_log_view_state *s = &view->state.log;
1459 if (s->thread == NULL) {
1460 int errcode = pthread_create(&s->thread, NULL, log_thread,
1461 &s->thread_args);
1462 if (errcode)
1463 return got_error_set_errno(errcode);
1466 return draw_commits(view, &s->last_displayed_entry,
1467 &s->selected_entry, s->first_displayed_entry,
1468 &s->commits, s->selected, view->nlines,
1469 s->in_repo_path, s->thread_args.commits_needed);
1472 static const struct got_error *
1473 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1474 struct tog_view **focus_view, struct tog_view *view, int ch)
1476 const struct got_error *err = NULL;
1477 struct tog_log_view_state *s = &view->state.log;
1478 char *parent_path;
1479 struct tog_view *diff_view = NULL, *tree_view = NULL;
1480 int begin_x = 0;
1482 switch (ch) {
1483 case 'q':
1484 s->quit = 1;
1485 break;
1486 case 'k':
1487 case KEY_UP:
1488 if (s->first_displayed_entry == NULL)
1489 break;
1490 if (s->selected > 0)
1491 s->selected--;
1492 if (s->selected > 0)
1493 break;
1494 scroll_up(&s->first_displayed_entry, 1,
1495 &s->commits);
1496 break;
1497 case KEY_PPAGE:
1498 if (s->first_displayed_entry == NULL)
1499 break;
1500 if (TAILQ_FIRST(&s->commits.head) ==
1501 s->first_displayed_entry) {
1502 s->selected = 0;
1503 break;
1505 scroll_up(&s->first_displayed_entry,
1506 view->nlines, &s->commits);
1507 break;
1508 case 'j':
1509 case KEY_DOWN:
1510 if (s->first_displayed_entry == NULL)
1511 break;
1512 if (s->selected < MIN(view->nlines - 2,
1513 s->commits.ncommits - 1)) {
1514 s->selected++;
1515 break;
1517 err = scroll_down(&s->first_displayed_entry, 1,
1518 &s->last_displayed_entry, &s->commits,
1519 &s->thread_args.log_complete,
1520 &s->thread_args.commits_needed,
1521 &s->thread_args.need_commits);
1522 break;
1523 case KEY_NPAGE: {
1524 struct commit_queue_entry *first;
1525 first = s->first_displayed_entry;
1526 if (first == NULL)
1527 break;
1528 err = scroll_down(&s->first_displayed_entry,
1529 view->nlines, &s->last_displayed_entry,
1530 &s->commits, &s->thread_args.log_complete,
1531 &s->thread_args.commits_needed,
1532 &s->thread_args.need_commits);
1533 if (first == s->first_displayed_entry &&
1534 s->selected < MIN(view->nlines - 2,
1535 s->commits.ncommits - 1)) {
1536 /* can't scroll further down */
1537 s->selected = MIN(view->nlines - 2,
1538 s->commits.ncommits - 1);
1540 err = NULL;
1541 break;
1543 case KEY_RESIZE:
1544 if (s->selected > view->nlines - 2)
1545 s->selected = view->nlines - 2;
1546 if (s->selected > s->commits.ncommits - 1)
1547 s->selected = s->commits.ncommits - 1;
1548 break;
1549 case KEY_ENTER:
1550 case '\r':
1551 if (s->selected_entry == NULL)
1552 break;
1553 if (view_is_parent_view(view))
1554 begin_x = view_split_begin_x(view->begin_x);
1555 err = open_diff_view_for_commit(&diff_view, begin_x,
1556 s->selected_entry->id, s->selected_entry->commit,
1557 s->repo);
1558 if (err)
1559 break;
1560 if (view_is_parent_view(view)) {
1561 err = view_close_child(view);
1562 if (err)
1563 return err;
1564 err = view_set_child(view, diff_view);
1565 if (err) {
1566 view_close(diff_view);
1567 break;
1569 *focus_view = diff_view;
1570 view->child_focussed = 1;
1571 } else
1572 *new_view = diff_view;
1573 break;
1574 case 't':
1575 if (s->selected_entry == NULL)
1576 break;
1577 if (view_is_parent_view(view))
1578 begin_x = view_split_begin_x(view->begin_x);
1579 err = browse_commit(&tree_view, begin_x,
1580 s->selected_entry, s->repo);
1581 if (err)
1582 break;
1583 if (view_is_parent_view(view)) {
1584 err = view_close_child(view);
1585 if (err)
1586 return err;
1587 err = view_set_child(view, tree_view);
1588 if (err) {
1589 view_close(tree_view);
1590 break;
1592 *focus_view = tree_view;
1593 view->child_focussed = 1;
1594 } else
1595 *new_view = tree_view;
1596 break;
1597 case KEY_BACKSPACE:
1598 if (strcmp(s->in_repo_path, "/") == 0)
1599 break;
1600 parent_path = dirname(s->in_repo_path);
1601 if (parent_path && strcmp(parent_path, ".") != 0) {
1602 struct tog_view *lv;
1603 err = stop_log_thread(s);
1604 if (err)
1605 return err;
1606 lv = view_open(view->nlines, view->ncols,
1607 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1608 if (lv == NULL)
1609 return got_error_from_errno();
1610 err = open_log_view(lv, s->start_id, s->repo,
1611 parent_path, 0);
1612 if (err)
1613 return err;;
1614 if (view_is_parent_view(view))
1615 *new_view = lv;
1616 else {
1617 view_set_child(view->parent, lv);
1618 *focus_view = lv;
1620 return NULL;
1622 break;
1623 default:
1624 break;
1627 return err;
1630 static const struct got_error *
1631 cmd_log(int argc, char *argv[])
1633 const struct got_error *error;
1634 struct got_repository *repo = NULL;
1635 struct got_object_id *start_id = NULL;
1636 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1637 char *start_commit = NULL;
1638 int ch;
1639 struct tog_view *view;
1641 #ifndef PROFILE
1642 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1643 == -1)
1644 err(1, "pledge");
1645 #endif
1647 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1648 switch (ch) {
1649 case 'c':
1650 start_commit = optarg;
1651 break;
1652 case 'r':
1653 repo_path = realpath(optarg, NULL);
1654 if (repo_path == NULL)
1655 err(1, "-r option");
1656 break;
1657 default:
1658 usage();
1659 /* NOTREACHED */
1663 argc -= optind;
1664 argv += optind;
1666 if (argc == 0)
1667 path = strdup("");
1668 else if (argc == 1)
1669 path = strdup(argv[0]);
1670 else
1671 usage_log();
1672 if (path == NULL)
1673 return got_error_from_errno();
1675 cwd = getcwd(NULL, 0);
1676 if (cwd == NULL) {
1677 error = got_error_from_errno();
1678 goto done;
1680 if (repo_path == NULL) {
1681 repo_path = strdup(cwd);
1682 if (repo_path == NULL) {
1683 error = got_error_from_errno();
1684 goto done;
1688 error = got_repo_open(&repo, repo_path);
1689 if (error != NULL)
1690 goto done;
1692 if (start_commit == NULL) {
1693 error = get_head_commit_id(&start_id, repo);
1694 if (error != NULL)
1695 goto done;
1696 } else {
1697 struct got_object *obj;
1698 error = got_object_open_by_id_str(&obj, repo, start_commit);
1699 if (error == NULL) {
1700 start_id = got_object_id_dup(got_object_get_id(obj));
1701 if (start_id == NULL)
1702 error = got_error_from_errno();
1703 goto done;
1706 if (error != NULL)
1707 goto done;
1709 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1710 if (view == NULL) {
1711 error = got_error_from_errno();
1712 goto done;
1714 error = open_log_view(view, start_id, repo, path, 1);
1715 if (error)
1716 goto done;
1717 error = view_loop(view);
1718 done:
1719 free(repo_path);
1720 free(cwd);
1721 free(path);
1722 free(start_id);
1723 if (repo)
1724 got_repo_close(repo);
1725 return error;
1728 __dead static void
1729 usage_diff(void)
1731 endwin();
1732 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1733 getprogname());
1734 exit(1);
1737 static char *
1738 parse_next_line(FILE *f, size_t *len)
1740 char *line;
1741 size_t linelen;
1742 size_t lineno;
1743 const char delim[3] = { '\0', '\0', '\0'};
1745 line = fparseln(f, &linelen, &lineno, delim, 0);
1746 if (len)
1747 *len = linelen;
1748 return line;
1751 static const struct got_error *
1752 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1753 int *last_displayed_line, int *eof, int max_lines,
1754 char * header)
1756 const struct got_error *err;
1757 int nlines = 0, nprinted = 0;
1758 char *line;
1759 size_t len;
1760 wchar_t *wline;
1761 int width;
1763 rewind(f);
1764 werase(view->window);
1766 if (header) {
1767 err = format_line(&wline, &width, header, view->ncols);
1768 if (err) {
1769 return err;
1772 if (view_needs_focus_indication(view))
1773 wstandout(view->window);
1774 waddwstr(view->window, wline);
1775 if (view_needs_focus_indication(view))
1776 wstandend(view->window);
1777 if (width < view->ncols)
1778 waddch(view->window, '\n');
1780 if (max_lines <= 1)
1781 return NULL;
1782 max_lines--;
1785 *eof = 0;
1786 while (nprinted < max_lines) {
1787 line = parse_next_line(f, &len);
1788 if (line == NULL) {
1789 *eof = 1;
1790 break;
1792 if (++nlines < *first_displayed_line) {
1793 free(line);
1794 continue;
1797 err = format_line(&wline, &width, line, view->ncols);
1798 if (err) {
1799 free(line);
1800 return err;
1802 waddwstr(view->window, wline);
1803 if (width < view->ncols)
1804 waddch(view->window, '\n');
1805 if (++nprinted == 1)
1806 *first_displayed_line = nlines;
1807 free(line);
1808 free(wline);
1809 wline = NULL;
1811 *last_displayed_line = nlines;
1813 view_vborder(view);
1815 return NULL;
1818 static char *
1819 get_datestr(time_t *time, char *datebuf)
1821 char *p, *s = ctime_r(time, datebuf);
1822 p = strchr(s, '\n');
1823 if (p)
1824 *p = '\0';
1825 return s;
1828 static const struct got_error *
1829 write_commit_info(struct got_object *obj, struct got_repository *repo,
1830 FILE *outfile)
1832 const struct got_error *err = NULL;
1833 char *id_str;
1834 char datebuf[26];
1835 struct got_commit_object *commit = NULL;
1836 time_t committer_time;
1837 const char *author, *committer;
1839 err = got_object_id_str(&id_str, got_object_get_id(obj));
1840 if (err)
1841 return err;
1843 err = got_object_commit_open(&commit, repo, obj);
1845 if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
1846 err = got_error_from_errno();
1847 goto done;
1849 if (fprintf(outfile, "from: %s\n",
1850 got_object_commit_get_author(commit)) < 0) {
1851 err = got_error_from_errno();
1852 goto done;
1854 committer_time = got_object_commit_get_committer_time(commit);
1855 if (fprintf(outfile, "date: %s UTC\n",
1856 get_datestr(&committer_time, datebuf)) < 0) {
1857 err = got_error_from_errno();
1858 goto done;
1860 author = got_object_commit_get_author(commit);
1861 committer = got_object_commit_get_committer(commit);
1862 if (strcmp(author, committer) != 0 &&
1863 fprintf(outfile, "via: %s\n", committer) < 0) {
1864 err = got_error_from_errno();
1865 goto done;
1867 if (fprintf(outfile, "%s\n",
1868 got_object_commit_get_logmsg(commit)) < 0) {
1869 err = got_error_from_errno();
1870 goto done;
1872 done:
1873 free(id_str);
1874 if (commit)
1875 got_object_commit_close(commit);
1876 return err;
1879 static const struct got_error *
1880 create_diff(struct tog_diff_view_state *s)
1882 const struct got_error *err = NULL;
1883 struct got_object *obj1 = NULL, *obj2 = NULL;
1884 FILE *f = NULL;
1886 if (s->id1) {
1887 err = got_object_open(&obj1, s->repo, s->id1);
1888 if (err)
1889 return err;
1892 err = got_object_open(&obj2, s->repo, s->id2);
1893 if (err)
1894 goto done;
1896 f = got_opentemp();
1897 if (f == NULL) {
1898 err = got_error_from_errno();
1899 goto done;
1901 if (s->f)
1902 fclose(s->f);
1903 s->f = f;
1905 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1906 case GOT_OBJ_TYPE_BLOB:
1907 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1908 s->diff_context, s->repo, f);
1909 break;
1910 case GOT_OBJ_TYPE_TREE:
1911 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1912 s->diff_context, s->repo, f);
1913 break;
1914 case GOT_OBJ_TYPE_COMMIT: {
1915 const struct got_object_id_queue *parent_ids;
1916 struct got_object_qid *pid;
1917 struct got_commit_object *commit2;
1919 err = got_object_commit_open(&commit2, s->repo, obj2);
1920 if (err)
1921 break;
1922 /* Show commit info if we're diffing to a parent commit. */
1923 parent_ids = got_object_commit_get_parent_ids(commit2);
1924 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1925 struct got_object_id *id1 = got_object_get_id(obj1);
1926 if (got_object_id_cmp(id1, pid->id) == 0) {
1927 write_commit_info(obj2, s->repo, f);
1928 break;
1931 got_object_commit_close(commit2);
1933 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1934 s->repo, f);
1935 break;
1937 default:
1938 err = got_error(GOT_ERR_OBJ_TYPE);
1939 break;
1941 done:
1942 if (obj1)
1943 got_object_close(obj1);
1944 got_object_close(obj2);
1945 if (f)
1946 fflush(f);
1947 return err;
1950 static const struct got_error *
1951 open_diff_view(struct tog_view *view, struct got_object *obj1,
1952 struct got_object *obj2, struct got_repository *repo)
1954 const struct got_error *err;
1956 if (obj1 != NULL && obj2 != NULL &&
1957 got_object_get_type(obj1) != got_object_get_type(obj2))
1958 return got_error(GOT_ERR_OBJ_TYPE);
1960 if (obj1) {
1961 struct got_object_id *id1;
1962 id1 = got_object_id_dup(got_object_get_id(obj1));
1963 if (id1 == NULL)
1964 return got_error_from_errno();
1965 view->state.diff.id1 = id1;
1966 } else
1967 view->state.diff.id1 = NULL;
1969 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1970 if (view->state.diff.id2 == NULL) {
1971 free(view->state.diff.id1);
1972 view->state.diff.id1 = NULL;
1973 return got_error_from_errno();
1975 view->state.diff.f = NULL;
1976 view->state.diff.first_displayed_line = 1;
1977 view->state.diff.last_displayed_line = view->nlines;
1978 view->state.diff.diff_context = 3;
1979 view->state.diff.repo = repo;
1981 err = create_diff(&view->state.diff);
1982 if (err) {
1983 free(view->state.diff.id1);
1984 view->state.diff.id1 = NULL;
1985 free(view->state.diff.id2);
1986 view->state.diff.id2 = NULL;
1987 return err;
1990 view->show = show_diff_view;
1991 view->input = input_diff_view;
1992 view->close = close_diff_view;
1994 return NULL;
1997 static const struct got_error *
1998 close_diff_view(struct tog_view *view)
2000 const struct got_error *err = NULL;
2002 free(view->state.diff.id1);
2003 view->state.diff.id1 = NULL;
2004 free(view->state.diff.id2);
2005 view->state.diff.id2 = NULL;
2006 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2007 err = got_error_from_errno();
2008 return err;
2011 static const struct got_error *
2012 show_diff_view(struct tog_view *view)
2014 const struct got_error *err;
2015 struct tog_diff_view_state *s = &view->state.diff;
2016 char *id_str1 = NULL, *id_str2, *header;
2018 if (s->id1) {
2019 err = got_object_id_str(&id_str1, s->id1);
2020 if (err)
2021 return err;
2023 err = got_object_id_str(&id_str2, s->id2);
2024 if (err)
2025 return err;
2027 if (asprintf(&header, "diff %s %s",
2028 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2029 err = got_error_from_errno();
2030 free(id_str1);
2031 free(id_str2);
2032 return err;
2034 free(id_str1);
2035 free(id_str2);
2037 return draw_file(view, s->f, &s->first_displayed_line,
2038 &s->last_displayed_line, &s->eof, view->nlines,
2039 header);
2042 static const struct got_error *
2043 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2044 struct tog_view **focus_view, struct tog_view *view, int ch)
2046 const struct got_error *err = NULL;
2047 struct tog_diff_view_state *s = &view->state.diff;
2048 int i;
2050 switch (ch) {
2051 case 'k':
2052 case KEY_UP:
2053 if (s->first_displayed_line > 1)
2054 s->first_displayed_line--;
2055 break;
2056 case KEY_PPAGE:
2057 i = 0;
2058 while (i++ < view->nlines - 1 &&
2059 s->first_displayed_line > 1)
2060 s->first_displayed_line--;
2061 break;
2062 case 'j':
2063 case KEY_DOWN:
2064 if (!s->eof)
2065 s->first_displayed_line++;
2066 break;
2067 case KEY_NPAGE:
2068 case ' ':
2069 i = 0;
2070 while (!s->eof && i++ < view->nlines - 1) {
2071 char *line;
2072 line = parse_next_line(s->f, NULL);
2073 s->first_displayed_line++;
2074 if (line == NULL)
2075 break;
2077 break;
2078 case '[':
2079 if (s->diff_context > 0) {
2080 s->diff_context--;
2081 err = create_diff(s);
2083 break;
2084 case ']':
2085 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2086 s->diff_context++;
2087 err = create_diff(s);
2089 break;
2090 default:
2091 break;
2094 return err;
2097 static const struct got_error *
2098 cmd_diff(int argc, char *argv[])
2100 const struct got_error *error = NULL;
2101 struct got_repository *repo = NULL;
2102 struct got_object *obj1 = NULL, *obj2 = NULL;
2103 char *repo_path = NULL;
2104 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
2105 int ch;
2106 struct tog_view *view;
2108 #ifndef PROFILE
2109 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2110 == -1)
2111 err(1, "pledge");
2112 #endif
2114 while ((ch = getopt(argc, argv, "")) != -1) {
2115 switch (ch) {
2116 default:
2117 usage();
2118 /* NOTREACHED */
2122 argc -= optind;
2123 argv += optind;
2125 if (argc == 0) {
2126 usage_diff(); /* TODO show local worktree changes */
2127 } else if (argc == 2) {
2128 repo_path = getcwd(NULL, 0);
2129 if (repo_path == NULL)
2130 return got_error_from_errno();
2131 obj_id_str1 = argv[0];
2132 obj_id_str2 = argv[1];
2133 } else if (argc == 3) {
2134 repo_path = realpath(argv[0], NULL);
2135 if (repo_path == NULL)
2136 return got_error_from_errno();
2137 obj_id_str1 = argv[1];
2138 obj_id_str2 = argv[2];
2139 } else
2140 usage_diff();
2142 error = got_repo_open(&repo, repo_path);
2143 free(repo_path);
2144 if (error)
2145 goto done;
2147 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2148 if (error)
2149 goto done;
2151 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2152 if (error)
2153 goto done;
2155 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2156 if (view == NULL) {
2157 error = got_error_from_errno();
2158 goto done;
2160 error = open_diff_view(view, obj1, obj2, repo);
2161 if (error)
2162 goto done;
2163 error = view_loop(view);
2164 done:
2165 got_repo_close(repo);
2166 if (obj1)
2167 got_object_close(obj1);
2168 if (obj2)
2169 got_object_close(obj2);
2170 return error;
2173 __dead static void
2174 usage_blame(void)
2176 endwin();
2177 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2178 getprogname());
2179 exit(1);
2182 struct tog_blame_line {
2183 int annotated;
2184 struct got_object_id *id;
2187 static const struct got_error *
2188 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2189 const char *path, struct tog_blame_line *lines, int nlines,
2190 int blame_complete, int selected_line, int *first_displayed_line,
2191 int *last_displayed_line, int *eof, int max_lines)
2193 const struct got_error *err;
2194 int lineno = 0, nprinted = 0;
2195 char *line;
2196 size_t len;
2197 wchar_t *wline;
2198 int width, wlimit;
2199 struct tog_blame_line *blame_line;
2200 struct got_object_id *prev_id = NULL;
2201 char *id_str;
2203 err = got_object_id_str(&id_str, id);
2204 if (err)
2205 return err;
2207 rewind(f);
2208 werase(view->window);
2210 if (asprintf(&line, "commit: %s", id_str) == -1) {
2211 err = got_error_from_errno();
2212 free(id_str);
2213 return err;
2216 err = format_line(&wline, &width, line, view->ncols);
2217 free(line);
2218 line = NULL;
2219 if (view_needs_focus_indication(view))
2220 wstandout(view->window);
2221 waddwstr(view->window, wline);
2222 if (view_needs_focus_indication(view))
2223 wstandend(view->window);
2224 free(wline);
2225 wline = NULL;
2226 if (width < view->ncols)
2227 waddch(view->window, '\n');
2229 if (asprintf(&line, "[%d/%d] %s%s",
2230 *first_displayed_line - 1 + selected_line, nlines,
2231 blame_complete ? "" : "annotating ", path) == -1) {
2232 free(id_str);
2233 return got_error_from_errno();
2235 free(id_str);
2236 err = format_line(&wline, &width, line, view->ncols);
2237 free(line);
2238 line = NULL;
2239 if (err)
2240 return err;
2241 waddwstr(view->window, wline);
2242 free(wline);
2243 wline = NULL;
2244 if (width < view->ncols)
2245 waddch(view->window, '\n');
2247 *eof = 0;
2248 while (nprinted < max_lines - 2) {
2249 line = parse_next_line(f, &len);
2250 if (line == NULL) {
2251 *eof = 1;
2252 break;
2254 if (++lineno < *first_displayed_line) {
2255 free(line);
2256 continue;
2259 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2260 err = format_line(&wline, &width, line, wlimit);
2261 if (err) {
2262 free(line);
2263 return err;
2266 if (view->focussed && nprinted == selected_line - 1)
2267 wstandout(view->window);
2269 blame_line = &lines[lineno - 1];
2270 if (blame_line->annotated && prev_id &&
2271 got_object_id_cmp(prev_id, blame_line->id) == 0)
2272 waddstr(view->window, " ");
2273 else if (blame_line->annotated) {
2274 char *id_str;
2275 err = got_object_id_str(&id_str, blame_line->id);
2276 if (err) {
2277 free(line);
2278 free(wline);
2279 return err;
2281 wprintw(view->window, "%.8s ", id_str);
2282 free(id_str);
2283 prev_id = blame_line->id;
2284 } else {
2285 waddstr(view->window, "........ ");
2286 prev_id = NULL;
2289 waddwstr(view->window, wline);
2290 while (width < wlimit) {
2291 waddch(view->window, ' ');
2292 width++;
2294 if (view->focussed && nprinted == selected_line - 1)
2295 wstandend(view->window);
2296 if (++nprinted == 1)
2297 *first_displayed_line = lineno;
2298 free(line);
2299 free(wline);
2300 wline = NULL;
2302 *last_displayed_line = lineno;
2304 view_vborder(view);
2306 return NULL;
2309 static const struct got_error *
2310 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2312 const struct got_error *err = NULL;
2313 struct tog_blame_cb_args *a = arg;
2314 struct tog_blame_line *line;
2315 int errcode;
2317 if (nlines != a->nlines ||
2318 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2319 return got_error(GOT_ERR_RANGE);
2321 errcode = pthread_mutex_lock(&tog_mutex);
2322 if (errcode)
2323 return got_error_set_errno(errcode);
2325 if (*a->quit) { /* user has quit the blame view */
2326 err = got_error(GOT_ERR_ITER_COMPLETED);
2327 goto done;
2330 if (lineno == -1)
2331 goto done; /* no change in this commit */
2333 line = &a->lines[lineno - 1];
2334 if (line->annotated)
2335 goto done;
2337 line->id = got_object_id_dup(id);
2338 if (line->id == NULL) {
2339 err = got_error_from_errno();
2340 goto done;
2342 line->annotated = 1;
2343 done:
2344 errcode = pthread_mutex_unlock(&tog_mutex);
2345 if (errcode)
2346 err = got_error_set_errno(errcode);
2347 return err;
2350 static void *
2351 blame_thread(void *arg)
2353 const struct got_error *err;
2354 struct tog_blame_thread_args *ta = arg;
2355 struct tog_blame_cb_args *a = ta->cb_args;
2356 int errcode;
2358 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2359 blame_cb, ta->cb_args);
2361 errcode = pthread_mutex_lock(&tog_mutex);
2362 if (errcode)
2363 return (void *)got_error_set_errno(errcode);
2365 got_repo_close(ta->repo);
2366 ta->repo = NULL;
2367 *ta->complete = 1;
2369 errcode = pthread_mutex_unlock(&tog_mutex);
2370 if (errcode && err == NULL)
2371 err = got_error_set_errno(errcode);
2373 return (void *)err;
2376 static struct got_object_id *
2377 get_selected_commit_id(struct tog_blame_line *lines,
2378 int first_displayed_line, int selected_line)
2380 struct tog_blame_line *line;
2382 line = &lines[first_displayed_line - 1 + selected_line - 1];
2383 if (!line->annotated)
2384 return NULL;
2386 return line->id;
2389 static const struct got_error *
2390 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2391 struct tog_blame_line *lines, int first_displayed_line,
2392 int selected_line, struct got_repository *repo)
2394 const struct got_error *err = NULL;
2395 struct got_commit_object *commit = NULL;
2396 struct got_object_id *selected_id;
2397 struct got_object_qid *pid;
2399 *pobj = NULL;
2400 *obj = NULL;
2402 selected_id = get_selected_commit_id(lines,
2403 first_displayed_line, selected_line);
2404 if (selected_id == NULL)
2405 return NULL;
2407 err = got_object_open(obj, repo, selected_id);
2408 if (err)
2409 goto done;
2411 err = got_object_commit_open(&commit, repo, *obj);
2412 if (err)
2413 goto done;
2415 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2416 if (pid) {
2417 err = got_object_open(pobj, repo, pid->id);
2418 if (err)
2419 goto done;
2421 done:
2422 if (commit)
2423 got_object_commit_close(commit);
2424 return err;
2427 static const struct got_error *
2428 stop_blame(struct tog_blame *blame)
2430 const struct got_error *err = NULL;
2431 int i;
2433 if (blame->thread) {
2434 int errcode;
2435 errcode = pthread_mutex_unlock(&tog_mutex);
2436 if (errcode)
2437 return got_error_set_errno(errcode);
2438 errcode = pthread_join(blame->thread, (void **)&err);
2439 if (errcode)
2440 return got_error_set_errno(errcode);
2441 errcode = pthread_mutex_lock(&tog_mutex);
2442 if (errcode)
2443 return got_error_set_errno(errcode);
2444 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2445 err = NULL;
2446 blame->thread = NULL;
2448 if (blame->thread_args.repo) {
2449 got_repo_close(blame->thread_args.repo);
2450 blame->thread_args.repo = NULL;
2452 if (blame->f) {
2453 fclose(blame->f);
2454 blame->f = NULL;
2456 for (i = 0; i < blame->nlines; i++)
2457 free(blame->lines[i].id);
2458 free(blame->lines);
2459 blame->lines = NULL;
2460 free(blame->cb_args.commit_id);
2461 blame->cb_args.commit_id = NULL;
2463 return err;
2466 static const struct got_error *
2467 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2468 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2469 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2470 struct got_repository *repo)
2472 const struct got_error *err = NULL;
2473 struct got_blob_object *blob = NULL;
2474 struct got_repository *thread_repo = NULL;
2475 struct got_object_id *obj_id = NULL;
2476 struct got_object *obj = NULL;
2478 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2479 if (err)
2480 goto done;
2482 err = got_object_open(&obj, repo, obj_id);
2483 if (err)
2484 goto done;
2486 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2487 err = got_error(GOT_ERR_OBJ_TYPE);
2488 goto done;
2491 err = got_object_blob_open(&blob, repo, obj, 8192);
2492 if (err)
2493 goto done;
2494 blame->f = got_opentemp();
2495 if (blame->f == NULL) {
2496 err = got_error_from_errno();
2497 goto done;
2499 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2500 blame->f, blob);
2501 if (err)
2502 goto done;
2504 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2505 if (blame->lines == NULL) {
2506 err = got_error_from_errno();
2507 goto done;
2510 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2511 if (err)
2512 goto done;
2514 blame->cb_args.view = view;
2515 blame->cb_args.lines = blame->lines;
2516 blame->cb_args.nlines = blame->nlines;
2517 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2518 if (blame->cb_args.commit_id == NULL) {
2519 err = got_error_from_errno();
2520 goto done;
2522 blame->cb_args.quit = done;
2524 blame->thread_args.path = path;
2525 blame->thread_args.repo = thread_repo;
2526 blame->thread_args.cb_args = &blame->cb_args;
2527 blame->thread_args.complete = blame_complete;
2528 *blame_complete = 0;
2530 done:
2531 if (blob)
2532 got_object_blob_close(blob);
2533 free(obj_id);
2534 if (obj)
2535 got_object_close(obj);
2536 if (err)
2537 stop_blame(blame);
2538 return err;
2541 static const struct got_error *
2542 open_blame_view(struct tog_view *view, char *path,
2543 struct got_object_id *commit_id, struct got_repository *repo)
2545 const struct got_error *err = NULL;
2546 struct tog_blame_view_state *s = &view->state.blame;
2548 SIMPLEQ_INIT(&s->blamed_commits);
2550 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2551 if (err)
2552 return err;
2554 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2555 s->first_displayed_line = 1;
2556 s->last_displayed_line = view->nlines;
2557 s->selected_line = 1;
2558 s->blame_complete = 0;
2559 s->path = path;
2560 if (s->path == NULL)
2561 return got_error_from_errno();
2562 s->repo = repo;
2563 s->commit_id = commit_id;
2564 memset(&s->blame, 0, sizeof(s->blame));
2566 view->show = show_blame_view;
2567 view->input = input_blame_view;
2568 view->close = close_blame_view;
2570 return run_blame(&s->blame, view, &s->blame_complete,
2571 &s->first_displayed_line, &s->last_displayed_line,
2572 &s->selected_line, &s->done, &s->eof, s->path,
2573 s->blamed_commit->id, s->repo);
2576 static const struct got_error *
2577 close_blame_view(struct tog_view *view)
2579 const struct got_error *err = NULL;
2580 struct tog_blame_view_state *s = &view->state.blame;
2582 if (s->blame.thread)
2583 err = stop_blame(&s->blame);
2585 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2586 struct got_object_qid *blamed_commit;
2587 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2588 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2589 got_object_qid_free(blamed_commit);
2592 free(s->path);
2594 return err;
2597 static const struct got_error *
2598 show_blame_view(struct tog_view *view)
2600 const struct got_error *err = NULL;
2601 struct tog_blame_view_state *s = &view->state.blame;
2602 int errcode;
2604 if (s->blame.thread == NULL) {
2605 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2606 &s->blame.thread_args);
2607 if (errcode)
2608 return got_error_set_errno(errcode);
2611 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2612 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2613 s->selected_line, &s->first_displayed_line,
2614 &s->last_displayed_line, &s->eof, view->nlines);
2616 view_vborder(view);
2617 return err;
2620 static const struct got_error *
2621 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2622 struct tog_view **focus_view, struct tog_view *view, int ch)
2624 const struct got_error *err = NULL, *thread_err = NULL;
2625 struct got_object *obj = NULL, *pobj = NULL;
2626 struct tog_view *diff_view;
2627 struct tog_blame_view_state *s = &view->state.blame;
2628 int begin_x = 0;
2630 switch (ch) {
2631 case 'q':
2632 s->done = 1;
2633 break;
2634 case 'k':
2635 case KEY_UP:
2636 if (s->selected_line > 1)
2637 s->selected_line--;
2638 else if (s->selected_line == 1 &&
2639 s->first_displayed_line > 1)
2640 s->first_displayed_line--;
2641 break;
2642 case KEY_PPAGE:
2643 if (s->first_displayed_line == 1) {
2644 s->selected_line = 1;
2645 break;
2647 if (s->first_displayed_line > view->nlines - 2)
2648 s->first_displayed_line -=
2649 (view->nlines - 2);
2650 else
2651 s->first_displayed_line = 1;
2652 break;
2653 case 'j':
2654 case KEY_DOWN:
2655 if (s->selected_line < view->nlines - 2 &&
2656 s->first_displayed_line +
2657 s->selected_line <= s->blame.nlines)
2658 s->selected_line++;
2659 else if (s->last_displayed_line <
2660 s->blame.nlines)
2661 s->first_displayed_line++;
2662 break;
2663 case 'b':
2664 case 'p': {
2665 struct got_object_id *id;
2666 id = get_selected_commit_id(s->blame.lines,
2667 s->first_displayed_line, s->selected_line);
2668 if (id == NULL || got_object_id_cmp(id,
2669 s->blamed_commit->id) == 0)
2670 break;
2671 err = open_selected_commit(&pobj, &obj,
2672 s->blame.lines, s->first_displayed_line,
2673 s->selected_line, s->repo);
2674 if (err)
2675 break;
2676 if (pobj == NULL && obj == NULL)
2677 break;
2678 if (ch == 'p' && pobj == NULL)
2679 break;
2680 s->done = 1;
2681 thread_err = stop_blame(&s->blame);
2682 s->done = 0;
2683 if (thread_err)
2684 break;
2685 id = got_object_get_id(ch == 'b' ? obj : pobj);
2686 got_object_close(obj);
2687 obj = NULL;
2688 if (pobj) {
2689 got_object_close(pobj);
2690 pobj = NULL;
2692 err = got_object_qid_alloc(&s->blamed_commit, id);
2693 if (err)
2694 goto done;
2695 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2696 s->blamed_commit, entry);
2697 err = run_blame(&s->blame, view, &s->blame_complete,
2698 &s->first_displayed_line, &s->last_displayed_line,
2699 &s->selected_line, &s->done, &s->eof,
2700 s->path, s->blamed_commit->id, s->repo);
2701 if (err)
2702 break;
2703 break;
2705 case 'B': {
2706 struct got_object_qid *first;
2707 first = SIMPLEQ_FIRST(&s->blamed_commits);
2708 if (!got_object_id_cmp(first->id, s->commit_id))
2709 break;
2710 s->done = 1;
2711 thread_err = stop_blame(&s->blame);
2712 s->done = 0;
2713 if (thread_err)
2714 break;
2715 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2716 got_object_qid_free(s->blamed_commit);
2717 s->blamed_commit =
2718 SIMPLEQ_FIRST(&s->blamed_commits);
2719 err = run_blame(&s->blame, view, &s->blame_complete,
2720 &s->first_displayed_line, &s->last_displayed_line,
2721 &s->selected_line, &s->done, &s->eof, s->path,
2722 s->blamed_commit->id, s->repo);
2723 if (err)
2724 break;
2725 break;
2727 case KEY_ENTER:
2728 case '\r':
2729 err = open_selected_commit(&pobj, &obj,
2730 s->blame.lines, s->first_displayed_line,
2731 s->selected_line, s->repo);
2732 if (err)
2733 break;
2734 if (pobj == NULL && obj == NULL)
2735 break;
2737 if (view_is_parent_view(view))
2738 begin_x = view_split_begin_x(view->begin_x);
2739 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2740 if (diff_view == NULL) {
2741 err = got_error_from_errno();
2742 break;
2744 err = open_diff_view(diff_view, pobj, obj, s->repo);
2745 if (err) {
2746 view_close(diff_view);
2747 break;
2749 if (view_is_parent_view(view)) {
2750 err = view_close_child(view);
2751 if (err)
2752 return err;
2753 err = view_set_child(view, diff_view);
2754 if (err) {
2755 view_close(diff_view);
2756 break;
2758 *focus_view = diff_view;
2759 view->child_focussed = 1;
2760 } else
2761 *new_view = diff_view;
2762 if (pobj) {
2763 got_object_close(pobj);
2764 pobj = NULL;
2766 got_object_close(obj);
2767 obj = NULL;
2768 if (err)
2769 break;
2770 break;
2771 case KEY_NPAGE:
2772 case ' ':
2773 if (s->last_displayed_line >= s->blame.nlines &&
2774 s->selected_line < view->nlines - 2) {
2775 s->selected_line = MIN(s->blame.nlines,
2776 view->nlines - 2);
2777 break;
2779 if (s->last_displayed_line + view->nlines - 2
2780 <= s->blame.nlines)
2781 s->first_displayed_line +=
2782 view->nlines - 2;
2783 else
2784 s->first_displayed_line =
2785 s->blame.nlines -
2786 (view->nlines - 3);
2787 break;
2788 case KEY_RESIZE:
2789 if (s->selected_line > view->nlines - 2) {
2790 s->selected_line = MIN(s->blame.nlines,
2791 view->nlines - 2);
2793 break;
2794 default:
2795 break;
2797 done:
2798 if (pobj)
2799 got_object_close(pobj);
2800 return thread_err ? thread_err : err;
2803 static const struct got_error *
2804 cmd_blame(int argc, char *argv[])
2806 const struct got_error *error;
2807 struct got_repository *repo = NULL;
2808 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2809 struct got_object_id *commit_id = NULL;
2810 char *commit_id_str = NULL;
2811 int ch;
2812 struct tog_view *view;
2814 #ifndef PROFILE
2815 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2816 == -1)
2817 err(1, "pledge");
2818 #endif
2820 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2821 switch (ch) {
2822 case 'c':
2823 commit_id_str = optarg;
2824 break;
2825 case 'r':
2826 repo_path = realpath(optarg, NULL);
2827 if (repo_path == NULL)
2828 err(1, "-r option");
2829 break;
2830 default:
2831 usage();
2832 /* NOTREACHED */
2836 argc -= optind;
2837 argv += optind;
2839 if (argc == 1)
2840 path = argv[0];
2841 else
2842 usage_blame();
2844 cwd = getcwd(NULL, 0);
2845 if (cwd == NULL) {
2846 error = got_error_from_errno();
2847 goto done;
2849 if (repo_path == NULL) {
2850 repo_path = strdup(cwd);
2851 if (repo_path == NULL) {
2852 error = got_error_from_errno();
2853 goto done;
2858 error = got_repo_open(&repo, repo_path);
2859 if (error != NULL)
2860 return error;
2862 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2863 if (error != NULL)
2864 goto done;
2866 if (commit_id_str == NULL) {
2867 struct got_reference *head_ref;
2868 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2869 if (error != NULL)
2870 goto done;
2871 error = got_ref_resolve(&commit_id, repo, head_ref);
2872 got_ref_close(head_ref);
2873 } else {
2874 struct got_object *obj;
2875 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2876 if (error != NULL)
2877 goto done;
2878 commit_id = got_object_id_dup(got_object_get_id(obj));
2879 if (commit_id == NULL)
2880 error = got_error_from_errno();
2881 got_object_close(obj);
2883 if (error != NULL)
2884 goto done;
2886 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2887 if (view == NULL) {
2888 error = got_error_from_errno();
2889 goto done;
2891 error = open_blame_view(view, in_repo_path, commit_id, repo);
2892 if (error)
2893 goto done;
2894 error = view_loop(view);
2895 done:
2896 free(repo_path);
2897 free(cwd);
2898 free(commit_id);
2899 if (repo)
2900 got_repo_close(repo);
2901 return error;
2904 static const struct got_error *
2905 draw_tree_entries(struct tog_view *view,
2906 struct got_tree_entry **first_displayed_entry,
2907 struct got_tree_entry **last_displayed_entry,
2908 struct got_tree_entry **selected_entry, int *ndisplayed,
2909 const char *label, int show_ids, const char *parent_path,
2910 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2912 const struct got_error *err = NULL;
2913 struct got_tree_entry *te;
2914 wchar_t *wline;
2915 int width, n;
2917 *ndisplayed = 0;
2919 werase(view->window);
2921 if (limit == 0)
2922 return NULL;
2924 err = format_line(&wline, &width, label, view->ncols);
2925 if (err)
2926 return err;
2927 if (view_needs_focus_indication(view))
2928 wstandout(view->window);
2929 waddwstr(view->window, wline);
2930 if (view_needs_focus_indication(view))
2931 wstandend(view->window);
2932 free(wline);
2933 wline = NULL;
2934 if (width < view->ncols)
2935 waddch(view->window, '\n');
2936 if (--limit <= 0)
2937 return NULL;
2938 err = format_line(&wline, &width, parent_path, view->ncols);
2939 if (err)
2940 return err;
2941 waddwstr(view->window, wline);
2942 free(wline);
2943 wline = NULL;
2944 if (width < view->ncols)
2945 waddch(view->window, '\n');
2946 if (--limit <= 0)
2947 return NULL;
2948 waddch(view->window, '\n');
2949 if (--limit <= 0)
2950 return NULL;
2952 te = SIMPLEQ_FIRST(&entries->head);
2953 if (*first_displayed_entry == NULL) {
2954 if (selected == 0) {
2955 if (view->focussed)
2956 wstandout(view->window);
2957 *selected_entry = NULL;
2959 waddstr(view->window, " ..\n"); /* parent directory */
2960 if (selected == 0 && view->focussed)
2961 wstandend(view->window);
2962 (*ndisplayed)++;
2963 if (--limit <= 0)
2964 return NULL;
2965 n = 1;
2966 } else {
2967 n = 0;
2968 while (te != *first_displayed_entry)
2969 te = SIMPLEQ_NEXT(te, entry);
2972 while (te) {
2973 char *line = NULL, *id_str = NULL;
2975 if (show_ids) {
2976 err = got_object_id_str(&id_str, te->id);
2977 if (err)
2978 return got_error_from_errno();
2980 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2981 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2982 free(id_str);
2983 return got_error_from_errno();
2985 free(id_str);
2986 err = format_line(&wline, &width, line, view->ncols);
2987 if (err) {
2988 free(line);
2989 break;
2991 if (n == selected) {
2992 if (view->focussed)
2993 wstandout(view->window);
2994 *selected_entry = te;
2996 waddwstr(view->window, wline);
2997 if (width < view->ncols)
2998 waddch(view->window, '\n');
2999 if (n == selected && view->focussed)
3000 wstandend(view->window);
3001 free(line);
3002 free(wline);
3003 wline = NULL;
3004 n++;
3005 (*ndisplayed)++;
3006 *last_displayed_entry = te;
3007 if (--limit <= 0)
3008 break;
3009 te = SIMPLEQ_NEXT(te, entry);
3012 return err;
3015 static void
3016 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3017 const struct got_tree_entries *entries, int isroot)
3019 struct got_tree_entry *te, *prev;
3020 int i;
3022 if (*first_displayed_entry == NULL)
3023 return;
3025 te = SIMPLEQ_FIRST(&entries->head);
3026 if (*first_displayed_entry == te) {
3027 if (!isroot)
3028 *first_displayed_entry = NULL;
3029 return;
3032 /* XXX this is stupid... switch to TAILQ? */
3033 for (i = 0; i < maxscroll; i++) {
3034 while (te != *first_displayed_entry) {
3035 prev = te;
3036 te = SIMPLEQ_NEXT(te, entry);
3038 *first_displayed_entry = prev;
3039 te = SIMPLEQ_FIRST(&entries->head);
3041 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3042 *first_displayed_entry = NULL;
3045 static void
3046 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3047 struct got_tree_entry *last_displayed_entry,
3048 const struct got_tree_entries *entries)
3050 struct got_tree_entry *next;
3051 int n = 0;
3053 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
3054 return;
3056 if (*first_displayed_entry)
3057 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3058 else
3059 next = SIMPLEQ_FIRST(&entries->head);
3060 while (next) {
3061 *first_displayed_entry = next;
3062 if (++n >= maxscroll)
3063 break;
3064 next = SIMPLEQ_NEXT(next, entry);
3068 static const struct got_error *
3069 tree_entry_path(char **path, struct tog_parent_trees *parents,
3070 struct got_tree_entry *te)
3072 const struct got_error *err = NULL;
3073 struct tog_parent_tree *pt;
3074 size_t len = 2; /* for leading slash and NUL */
3076 TAILQ_FOREACH(pt, parents, entry)
3077 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3078 if (te)
3079 len += strlen(te->name);
3081 *path = calloc(1, len);
3082 if (path == NULL)
3083 return got_error_from_errno();
3085 (*path)[0] = '/';
3086 pt = TAILQ_LAST(parents, tog_parent_trees);
3087 while (pt) {
3088 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3089 err = got_error(GOT_ERR_NO_SPACE);
3090 goto done;
3092 if (strlcat(*path, "/", len) >= len) {
3093 err = got_error(GOT_ERR_NO_SPACE);
3094 goto done;
3096 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3098 if (te) {
3099 if (strlcat(*path, te->name, len) >= len) {
3100 err = got_error(GOT_ERR_NO_SPACE);
3101 goto done;
3104 done:
3105 if (err) {
3106 free(*path);
3107 *path = NULL;
3109 return err;
3112 static const struct got_error *
3113 blame_tree_entry(struct tog_view **new_view, int begin_x,
3114 struct got_tree_entry *te, struct tog_parent_trees *parents,
3115 struct got_object_id *commit_id, struct got_repository *repo)
3117 const struct got_error *err = NULL;
3118 char *path;
3119 struct tog_view *blame_view;
3121 err = tree_entry_path(&path, parents, te);
3122 if (err)
3123 return err;
3125 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3126 if (blame_view == NULL)
3127 return got_error_from_errno();
3129 err = open_blame_view(blame_view, path, commit_id, repo);
3130 if (err) {
3131 view_close(blame_view);
3132 free(path);
3133 } else
3134 *new_view = blame_view;
3135 return err;
3138 static const struct got_error *
3139 log_tree_entry(struct tog_view **new_view, int begin_x,
3140 struct got_tree_entry *te, struct tog_parent_trees *parents,
3141 struct got_object_id *commit_id, struct got_repository *repo)
3143 struct tog_view *log_view;
3144 const struct got_error *err = NULL;
3145 char *path;
3147 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3148 if (log_view == NULL)
3149 return got_error_from_errno();
3151 err = tree_entry_path(&path, parents, te);
3152 if (err)
3153 return err;
3155 err = open_log_view(log_view, commit_id, repo, path, 0);
3156 if (err)
3157 view_close(log_view);
3158 else
3159 *new_view = log_view;
3160 free(path);
3161 return err;
3164 static const struct got_error *
3165 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3166 struct got_object_id *commit_id, struct got_repository *repo)
3168 const struct got_error *err = NULL;
3169 char *commit_id_str = NULL;
3170 struct tog_tree_view_state *s = &view->state.tree;
3172 TAILQ_INIT(&s->parents);
3174 err = got_object_id_str(&commit_id_str, commit_id);
3175 if (err != NULL)
3176 goto done;
3178 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3179 err = got_error_from_errno();
3180 goto done;
3183 s->root = s->tree = root;
3184 s->entries = got_object_tree_get_entries(root);
3185 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3186 s->commit_id = got_object_id_dup(commit_id);
3187 if (s->commit_id == NULL) {
3188 err = got_error_from_errno();
3189 goto done;
3191 s->repo = repo;
3193 view->show = show_tree_view;
3194 view->input = input_tree_view;
3195 view->close = close_tree_view;
3196 done:
3197 free(commit_id_str);
3198 if (err) {
3199 free(s->tree_label);
3200 s->tree_label = NULL;
3202 return err;
3205 static const struct got_error *
3206 close_tree_view(struct tog_view *view)
3208 struct tog_tree_view_state *s = &view->state.tree;
3210 free(s->tree_label);
3211 s->tree_label = NULL;
3212 free(s->commit_id);
3213 s->commit_id = NULL;
3214 while (!TAILQ_EMPTY(&s->parents)) {
3215 struct tog_parent_tree *parent;
3216 parent = TAILQ_FIRST(&s->parents);
3217 TAILQ_REMOVE(&s->parents, parent, entry);
3218 free(parent);
3221 if (s->tree != s->root)
3222 got_object_tree_close(s->tree);
3223 got_object_tree_close(s->root);
3225 return NULL;
3228 static const struct got_error *
3229 show_tree_view(struct tog_view *view)
3231 const struct got_error *err = NULL;
3232 struct tog_tree_view_state *s = &view->state.tree;
3233 char *parent_path;
3235 err = tree_entry_path(&parent_path, &s->parents, NULL);
3236 if (err)
3237 return err;
3239 err = draw_tree_entries(view, &s->first_displayed_entry,
3240 &s->last_displayed_entry, &s->selected_entry,
3241 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3242 s->entries, s->selected, view->nlines, s->tree == s->root);
3243 free(parent_path);
3245 view_vborder(view);
3246 return err;
3249 static const struct got_error *
3250 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3251 struct tog_view **focus_view, struct tog_view *view, int ch)
3253 const struct got_error *err = NULL;
3254 struct tog_tree_view_state *s = &view->state.tree;
3255 struct tog_view *log_view;
3256 int begin_x = 0;
3258 switch (ch) {
3259 case 'i':
3260 s->show_ids = !s->show_ids;
3261 break;
3262 case 'l':
3263 if (!s->selected_entry)
3264 break;
3265 if (view_is_parent_view(view))
3266 begin_x = view_split_begin_x(view->begin_x);
3267 err = log_tree_entry(&log_view, begin_x,
3268 s->selected_entry, &s->parents,
3269 s->commit_id, s->repo);
3270 if (view_is_parent_view(view)) {
3271 err = view_close_child(view);
3272 if (err)
3273 return err;
3274 err = view_set_child(view, log_view);
3275 if (err) {
3276 view_close(log_view);
3277 break;
3279 *focus_view = log_view;
3280 view->child_focussed = 1;
3281 } else
3282 *new_view = log_view;
3283 break;
3284 case 'k':
3285 case KEY_UP:
3286 if (s->selected > 0)
3287 s->selected--;
3288 if (s->selected > 0)
3289 break;
3290 tree_scroll_up(&s->first_displayed_entry, 1,
3291 s->entries, s->tree == s->root);
3292 break;
3293 case KEY_PPAGE:
3294 s->selected = 0;
3295 if (SIMPLEQ_FIRST(&s->entries->head) ==
3296 s->first_displayed_entry) {
3297 if (s->tree != s->root)
3298 s->first_displayed_entry = NULL;
3299 break;
3301 tree_scroll_up(&s->first_displayed_entry,
3302 view->nlines, s->entries,
3303 s->tree == s->root);
3304 break;
3305 case 'j':
3306 case KEY_DOWN:
3307 if (s->selected < s->ndisplayed - 1) {
3308 s->selected++;
3309 break;
3311 tree_scroll_down(&s->first_displayed_entry, 1,
3312 s->last_displayed_entry, s->entries);
3313 break;
3314 case KEY_NPAGE:
3315 tree_scroll_down(&s->first_displayed_entry,
3316 view->nlines, s->last_displayed_entry,
3317 s->entries);
3318 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3319 entry))
3320 break;
3321 /* can't scroll any further; move cursor down */
3322 if (s->selected < s->ndisplayed - 1)
3323 s->selected = s->ndisplayed - 1;
3324 break;
3325 case KEY_ENTER:
3326 case '\r':
3327 if (s->selected_entry == NULL) {
3328 struct tog_parent_tree *parent;
3329 case KEY_BACKSPACE:
3330 /* user selected '..' */
3331 if (s->tree == s->root)
3332 break;
3333 parent = TAILQ_FIRST(&s->parents);
3334 TAILQ_REMOVE(&s->parents, parent,
3335 entry);
3336 got_object_tree_close(s->tree);
3337 s->tree = parent->tree;
3338 s->entries =
3339 got_object_tree_get_entries(s->tree);
3340 s->first_displayed_entry =
3341 parent->first_displayed_entry;
3342 s->selected_entry =
3343 parent->selected_entry;
3344 s->selected = parent->selected;
3345 free(parent);
3346 } else if (S_ISDIR(s->selected_entry->mode)) {
3347 struct tog_parent_tree *parent;
3348 struct got_tree_object *child;
3349 err = got_object_open_as_tree(&child,
3350 s->repo, s->selected_entry->id);
3351 if (err)
3352 break;
3353 parent = calloc(1, sizeof(*parent));
3354 if (parent == NULL) {
3355 err = got_error_from_errno();
3356 break;
3358 parent->tree = s->tree;
3359 parent->first_displayed_entry =
3360 s->first_displayed_entry;
3361 parent->selected_entry = s->selected_entry;
3362 parent->selected = s->selected;
3363 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3364 s->tree = child;
3365 s->entries =
3366 got_object_tree_get_entries(s->tree);
3367 s->selected = 0;
3368 s->first_displayed_entry = NULL;
3369 } else if (S_ISREG(s->selected_entry->mode)) {
3370 struct tog_view *blame_view;
3371 int begin_x = view_is_parent_view(view) ?
3372 view_split_begin_x(view->begin_x) : 0;
3374 err = blame_tree_entry(&blame_view, begin_x,
3375 s->selected_entry, &s->parents, s->commit_id,
3376 s->repo);
3377 if (err)
3378 break;
3379 if (view_is_parent_view(view)) {
3380 err = view_close_child(view);
3381 if (err)
3382 return err;
3383 err = view_set_child(view, blame_view);
3384 if (err) {
3385 view_close(blame_view);
3386 break;
3388 *focus_view = blame_view;
3389 view->child_focussed = 1;
3390 } else
3391 *new_view = blame_view;
3393 break;
3394 case KEY_RESIZE:
3395 if (s->selected > view->nlines)
3396 s->selected = s->ndisplayed - 1;
3397 break;
3398 default:
3399 break;
3402 return err;
3405 __dead static void
3406 usage_tree(void)
3408 endwin();
3409 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3410 getprogname());
3411 exit(1);
3414 static const struct got_error *
3415 cmd_tree(int argc, char *argv[])
3417 const struct got_error *error;
3418 struct got_repository *repo = NULL;
3419 char *repo_path = NULL;
3420 struct got_object_id *commit_id = NULL;
3421 char *commit_id_arg = NULL;
3422 struct got_commit_object *commit = NULL;
3423 struct got_tree_object *tree = NULL;
3424 int ch;
3425 struct tog_view *view;
3427 #ifndef PROFILE
3428 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3429 == -1)
3430 err(1, "pledge");
3431 #endif
3433 while ((ch = getopt(argc, argv, "c:")) != -1) {
3434 switch (ch) {
3435 case 'c':
3436 commit_id_arg = optarg;
3437 break;
3438 default:
3439 usage();
3440 /* NOTREACHED */
3444 argc -= optind;
3445 argv += optind;
3447 if (argc == 0) {
3448 repo_path = getcwd(NULL, 0);
3449 if (repo_path == NULL)
3450 return got_error_from_errno();
3451 } else if (argc == 1) {
3452 repo_path = realpath(argv[0], NULL);
3453 if (repo_path == NULL)
3454 return got_error_from_errno();
3455 } else
3456 usage_log();
3458 error = got_repo_open(&repo, repo_path);
3459 free(repo_path);
3460 if (error != NULL)
3461 return error;
3463 if (commit_id_arg == NULL) {
3464 error = get_head_commit_id(&commit_id, repo);
3465 if (error != NULL)
3466 goto done;
3467 } else {
3468 struct got_object *obj;
3469 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3470 if (error == NULL) {
3471 commit_id = got_object_id_dup(got_object_get_id(obj));
3472 if (commit_id == NULL)
3473 error = got_error_from_errno();
3476 if (error != NULL)
3477 goto done;
3479 error = got_object_open_as_commit(&commit, repo, commit_id);
3480 if (error != NULL)
3481 goto done;
3483 error = got_object_open_as_tree(&tree, repo,
3484 got_object_commit_get_tree_id(commit));
3485 if (error != NULL)
3486 goto done;
3488 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3489 if (view == NULL) {
3490 error = got_error_from_errno();
3491 goto done;
3493 error = open_tree_view(view, tree, commit_id, repo);
3494 if (error)
3495 goto done;
3496 error = view_loop(view);
3497 done:
3498 free(commit_id);
3499 if (commit)
3500 got_object_commit_close(commit);
3501 if (tree)
3502 got_object_tree_close(tree);
3503 if (repo)
3504 got_repo_close(repo);
3505 return error;
3508 static void
3509 init_curses(void)
3511 initscr();
3512 cbreak();
3513 halfdelay(1); /* Do fast refresh while initial view is loading. */
3514 noecho();
3515 nonl();
3516 intrflush(stdscr, FALSE);
3517 keypad(stdscr, TRUE);
3518 curs_set(0);
3519 signal(SIGWINCH, tog_sigwinch);
3522 __dead static void
3523 usage(void)
3525 int i;
3527 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3528 "Available commands:\n", getprogname());
3529 for (i = 0; i < nitems(tog_commands); i++) {
3530 struct tog_cmd *cmd = &tog_commands[i];
3531 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3533 exit(1);
3536 static char **
3537 make_argv(const char *arg0, const char *arg1)
3539 char **argv;
3540 int argc = (arg1 == NULL ? 1 : 2);
3542 argv = calloc(argc, sizeof(char *));
3543 if (argv == NULL)
3544 err(1, "calloc");
3545 argv[0] = strdup(arg0);
3546 if (argv[0] == NULL)
3547 err(1, "calloc");
3548 if (arg1) {
3549 argv[1] = strdup(arg1);
3550 if (argv[1] == NULL)
3551 err(1, "calloc");
3554 return argv;
3557 int
3558 main(int argc, char *argv[])
3560 const struct got_error *error = NULL;
3561 struct tog_cmd *cmd = NULL;
3562 int ch, hflag = 0;
3563 char **cmd_argv = NULL;
3565 setlocale(LC_ALL, "");
3567 while ((ch = getopt(argc, argv, "h")) != -1) {
3568 switch (ch) {
3569 case 'h':
3570 hflag = 1;
3571 break;
3572 default:
3573 usage();
3574 /* NOTREACHED */
3578 argc -= optind;
3579 argv += optind;
3580 optind = 0;
3581 optreset = 1;
3583 if (argc == 0) {
3584 if (hflag)
3585 usage();
3586 /* Build an argument vector which runs a default command. */
3587 cmd = &tog_commands[0];
3588 cmd_argv = make_argv(cmd->name, NULL);
3589 argc = 1;
3590 } else {
3591 int i;
3593 /* Did the user specific a command? */
3594 for (i = 0; i < nitems(tog_commands); i++) {
3595 if (strncmp(tog_commands[i].name, argv[0],
3596 strlen(argv[0])) == 0) {
3597 cmd = &tog_commands[i];
3598 if (hflag)
3599 tog_commands[i].cmd_usage();
3600 break;
3603 if (cmd == NULL) {
3604 /* Did the user specify a repository? */
3605 char *repo_path = realpath(argv[0], NULL);
3606 if (repo_path) {
3607 struct got_repository *repo;
3608 error = got_repo_open(&repo, repo_path);
3609 if (error == NULL)
3610 got_repo_close(repo);
3611 } else
3612 error = got_error_from_errno();
3613 if (error) {
3614 if (hflag) {
3615 fprintf(stderr, "%s: '%s' is not a "
3616 "known command\n", getprogname(),
3617 argv[0]);
3618 usage();
3620 fprintf(stderr, "%s: '%s' is neither a known "
3621 "command nor a path to a repository\n",
3622 getprogname(), argv[0]);
3623 free(repo_path);
3624 return 1;
3626 cmd = &tog_commands[0];
3627 cmd_argv = make_argv(cmd->name, repo_path);
3628 argc = 2;
3629 free(repo_path);
3633 init_curses();
3635 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3636 if (error)
3637 goto done;
3638 done:
3639 endwin();
3640 free(cmd_argv);
3641 if (error)
3642 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3643 return 0;