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 size_t 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;
878 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
879 &commit->tm_committer) >= sizeof(datebuf))
880 return got_error(GOT_ERR_NO_SPACE);
882 if (avail < date_display_cols)
883 limit = MIN(sizeof(datebuf) - 1, avail);
884 else
885 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
886 waddnstr(view->window, datebuf, limit);
887 col = limit + 1;
888 if (col > avail)
889 goto done;
891 author0 = strdup(commit->author);
892 if (author0 == NULL) {
893 err = got_error_from_errno();
894 goto done;
896 author = author0;
897 smallerthan = strchr(author, '<');
898 if (smallerthan)
899 *smallerthan = '\0';
900 else {
901 char *at = strchr(author, '@');
902 if (at)
903 *at = '\0';
905 limit = avail - col;
906 err = format_line(&wauthor, &author_width, author, limit);
907 if (err)
908 goto done;
909 waddwstr(view->window, wauthor);
910 col += author_width;
911 while (col <= avail && author_width < author_display_cols + 1) {
912 waddch(view->window, ' ');
913 col++;
914 author_width++;
916 if (col > avail)
917 goto done;
919 logmsg0 = strdup(commit->logmsg);
920 if (logmsg0 == NULL) {
921 err = got_error_from_errno();
922 goto done;
924 logmsg = logmsg0;
925 while (*logmsg == '\n')
926 logmsg++;
927 newline = strchr(logmsg, '\n');
928 if (newline)
929 *newline = '\0';
930 limit = avail - col;
931 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
932 if (err)
933 goto done;
934 waddwstr(view->window, wlogmsg);
935 col += logmsg_width;
936 while (col <= avail) {
937 waddch(view->window, ' ');
938 col++;
940 done:
941 free(logmsg0);
942 free(wlogmsg);
943 free(author0);
944 free(wauthor);
945 free(line);
946 return err;
949 static struct commit_queue_entry *
950 alloc_commit_queue_entry(struct got_commit_object *commit,
951 struct got_object_id *id)
953 struct commit_queue_entry *entry;
955 entry = calloc(1, sizeof(*entry));
956 if (entry == NULL)
957 return NULL;
959 entry->id = id;
960 entry->commit = commit;
961 return entry;
964 static void
965 pop_commit(struct commit_queue *commits)
967 struct commit_queue_entry *entry;
969 entry = TAILQ_FIRST(&commits->head);
970 TAILQ_REMOVE(&commits->head, entry, entry);
971 got_object_commit_close(entry->commit);
972 commits->ncommits--;
973 /* Don't free entry->id! It is owned by the commit graph. */
974 free(entry);
977 static void
978 free_commits(struct commit_queue *commits)
980 while (!TAILQ_EMPTY(&commits->head))
981 pop_commit(commits);
984 static const struct got_error *
985 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
986 int minqueue, struct got_repository *repo, const char *path)
988 const struct got_error *err = NULL;
989 int nqueued = 0;
991 /*
992 * We keep all commits open throughout the lifetime of the log
993 * view in order to avoid having to re-fetch commits from disk
994 * while updating the display.
995 */
996 while (nqueued < minqueue) {
997 struct got_object_id *id;
998 struct got_commit_object *commit;
999 struct commit_queue_entry *entry;
1000 int errcode;
1002 err = got_commit_graph_iter_next(&id, graph);
1003 if (err) {
1004 if (err->code != GOT_ERR_ITER_NEED_MORE)
1005 break;
1006 err = got_commit_graph_fetch_commits(graph,
1007 minqueue, repo);
1008 if (err)
1009 return err;
1010 continue;
1013 if (id == NULL)
1014 break;
1016 err = got_object_open_as_commit(&commit, repo, id);
1017 if (err)
1018 break;
1019 entry = alloc_commit_queue_entry(commit, id);
1020 if (entry == NULL) {
1021 err = got_error_from_errno();
1022 break;
1025 errcode = pthread_mutex_lock(&tog_mutex);
1026 if (errcode) {
1027 err = got_error_set_errno(errcode);
1028 break;
1031 entry->idx = commits->ncommits;
1032 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1033 nqueued++;
1034 commits->ncommits++;
1036 errcode = pthread_mutex_unlock(&tog_mutex);
1037 if (errcode && err == NULL)
1038 err = got_error_set_errno(errcode);
1041 return err;
1044 static const struct got_error *
1045 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1047 const struct got_error *err = NULL;
1048 struct got_reference *head_ref;
1050 *head_id = NULL;
1052 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1053 if (err)
1054 return err;
1056 err = got_ref_resolve(head_id, repo, head_ref);
1057 got_ref_close(head_ref);
1058 if (err) {
1059 *head_id = NULL;
1060 return err;
1063 return NULL;
1066 static const struct got_error *
1067 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1068 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1069 struct commit_queue *commits, int selected_idx, int limit,
1070 const char *path, int commits_needed)
1072 const struct got_error *err = NULL;
1073 struct commit_queue_entry *entry;
1074 int ncommits, width;
1075 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1076 wchar_t *wline;
1078 entry = first;
1079 ncommits = 0;
1080 while (entry) {
1081 if (ncommits == selected_idx) {
1082 *selected = entry;
1083 break;
1085 entry = TAILQ_NEXT(entry, entry);
1086 ncommits++;
1089 if (*selected) {
1090 err = got_object_id_str(&id_str, (*selected)->id);
1091 if (err)
1092 return err;
1095 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1096 entry ? entry->idx + 1 : 0, commits->ncommits,
1097 commits_needed == 0 ? "" : " loading...") == -1)
1098 return got_error_from_errno();
1100 if (path && strcmp(path, "/") != 0) {
1101 if (asprintf(&header, "commit: %s %s%s",
1102 id_str ? id_str : "........................................",
1103 path, ncommits_str) == -1) {
1104 err = got_error_from_errno();
1105 header = NULL;
1106 goto done;
1108 } else if (asprintf(&header, "commit: %s%s",
1109 id_str ? id_str : "........................................",
1110 ncommits_str) == -1) {
1111 err = got_error_from_errno();
1112 header = NULL;
1113 goto done;
1115 err = format_line(&wline, &width, header, view->ncols);
1116 if (err)
1117 goto done;
1119 werase(view->window);
1121 if (view_needs_focus_indication(view))
1122 wstandout(view->window);
1123 waddwstr(view->window, wline);
1124 while (width < view->ncols) {
1125 waddch(view->window, ' ');
1126 width++;
1128 if (view_needs_focus_indication(view))
1129 wstandend(view->window);
1130 free(wline);
1131 if (limit <= 1)
1132 goto done;
1134 entry = first;
1135 *last = first;
1136 ncommits = 0;
1137 while (entry) {
1138 if (ncommits >= limit - 1)
1139 break;
1140 if (view->focussed && ncommits == selected_idx)
1141 wstandout(view->window);
1142 err = draw_commit(view, entry->commit, entry->id);
1143 if (view->focussed && ncommits == selected_idx)
1144 wstandend(view->window);
1145 if (err)
1146 break;
1147 ncommits++;
1148 *last = entry;
1149 entry = TAILQ_NEXT(entry, entry);
1152 view_vborder(view);
1153 done:
1154 free(id_str);
1155 free(ncommits_str);
1156 free(header);
1157 return err;
1160 static void
1161 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1162 struct commit_queue *commits)
1164 struct commit_queue_entry *entry;
1165 int nscrolled = 0;
1167 entry = TAILQ_FIRST(&commits->head);
1168 if (*first_displayed_entry == entry)
1169 return;
1171 entry = *first_displayed_entry;
1172 while (entry && nscrolled < maxscroll) {
1173 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1174 if (entry) {
1175 *first_displayed_entry = entry;
1176 nscrolled++;
1181 static const struct got_error *
1182 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1183 struct commit_queue_entry **last_displayed_entry,
1184 struct commit_queue *commits, int *log_complete, int *commits_needed,
1185 pthread_cond_t *need_commits)
1187 const struct got_error *err = NULL;
1188 struct commit_queue_entry *pentry;
1189 int nscrolled = 0;
1191 if (*last_displayed_entry == NULL)
1192 return NULL;
1194 do {
1195 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1196 if (pentry == NULL) {
1197 int errcode;
1198 if (*log_complete)
1199 return NULL;
1200 *commits_needed = maxscroll + 20;
1201 errcode = pthread_cond_signal(need_commits);
1202 if (errcode)
1203 return got_error_set_errno(errcode);
1204 return NULL;
1206 *last_displayed_entry = pentry;
1208 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1209 if (pentry == NULL)
1210 break;
1211 *first_displayed_entry = pentry;
1212 } while (++nscrolled < maxscroll);
1214 return err;
1217 static const struct got_error *
1218 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1219 struct got_object_id *commit_id, struct got_commit_object *commit,
1220 struct got_repository *repo)
1222 const struct got_error *err;
1223 struct got_object *obj1 = NULL, *obj2 = NULL;
1224 struct got_object_qid *parent_id;
1225 struct tog_view *diff_view;
1227 err = got_object_open(&obj2, repo, commit_id);
1228 if (err)
1229 return err;
1231 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1232 if (parent_id) {
1233 err = got_object_open(&obj1, repo, parent_id->id);
1234 if (err)
1235 goto done;
1238 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1239 if (diff_view == NULL) {
1240 err = got_error_from_errno();
1241 goto done;
1244 err = open_diff_view(diff_view, obj1, obj2, repo);
1245 if (err == NULL)
1246 *new_view = diff_view;
1247 done:
1248 if (obj1)
1249 got_object_close(obj1);
1250 if (obj2)
1251 got_object_close(obj2);
1252 return err;
1255 static const struct got_error *
1256 browse_commit(struct tog_view **new_view, int begin_x,
1257 struct commit_queue_entry *entry, struct got_repository *repo)
1259 const struct got_error *err = NULL;
1260 struct got_tree_object *tree;
1261 struct tog_view *tree_view;
1263 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1264 if (err)
1265 return err;
1267 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1268 if (tree_view == NULL)
1269 return got_error_from_errno();
1271 err = open_tree_view(tree_view, tree, entry->id, repo);
1272 if (err)
1273 got_object_tree_close(tree);
1274 else
1275 *new_view = tree_view;
1276 return err;
1279 static void *
1280 log_thread(void *arg)
1282 const struct got_error *err = NULL;
1283 int errcode = 0;
1284 struct tog_log_thread_args *a = arg;
1285 int done = 0;
1287 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1288 if (err)
1289 return (void *)err;
1291 while (!done && !err) {
1292 err = queue_commits(a->graph, a->commits, 1, a->repo,
1293 a->in_repo_path);
1294 if (err) {
1295 if (err->code != GOT_ERR_ITER_COMPLETED)
1296 return (void *)err;
1297 err = NULL;
1298 done = 1;
1299 } else if (a->commits_needed > 0)
1300 a->commits_needed--;
1302 errcode = pthread_mutex_lock(&tog_mutex);
1303 if (errcode)
1304 return (void *)got_error_set_errno(errcode);
1306 if (done)
1307 a->log_complete = 1;
1308 else if (*a->quit) {
1309 done = 1;
1310 a->log_complete = 1;
1311 } else if (*a->first_displayed_entry == NULL) {
1312 *a->first_displayed_entry =
1313 TAILQ_FIRST(&a->commits->head);
1314 *a->selected_entry = *a->first_displayed_entry;
1317 if (done)
1318 a->commits_needed = 0;
1319 else if (a->commits_needed == 0) {
1320 errcode = pthread_cond_wait(&a->need_commits,
1321 &tog_mutex);
1322 if (errcode)
1323 err = got_error_set_errno(errcode);
1326 errcode = pthread_mutex_unlock(&tog_mutex);
1327 if (errcode && err == NULL)
1328 err = got_error_set_errno(errcode);
1330 return (void *)err;
1333 static const struct got_error *
1334 stop_log_thread(struct tog_log_view_state *s)
1336 const struct got_error *err = NULL;
1337 int errcode;
1339 if (s->thread) {
1340 s->quit = 1;
1341 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1342 if (errcode)
1343 return got_error_set_errno(errcode);
1344 errcode = pthread_mutex_unlock(&tog_mutex);
1345 if (errcode)
1346 return got_error_set_errno(errcode);
1347 errcode = pthread_join(s->thread, (void **)&err);
1348 if (errcode)
1349 return got_error_set_errno(errcode);
1350 errcode = pthread_mutex_lock(&tog_mutex);
1351 if (errcode)
1352 return got_error_set_errno(errcode);
1353 s->thread = NULL;
1356 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1357 if (errcode && err == NULL)
1358 err = got_error_set_errno(errcode);
1360 if (s->thread_args.repo) {
1361 got_repo_close(s->thread_args.repo);
1362 s->thread_args.repo = NULL;
1365 if (s->thread_args.graph) {
1366 got_commit_graph_close(s->thread_args.graph);
1367 s->thread_args.graph = NULL;
1370 return err;
1373 static const struct got_error *
1374 close_log_view(struct tog_view *view)
1376 const struct got_error *err = NULL;
1377 struct tog_log_view_state *s = &view->state.log;
1379 err = stop_log_thread(s);
1380 free_commits(&s->commits);
1381 free(s->in_repo_path);
1382 s->in_repo_path = NULL;
1383 free(s->start_id);
1384 s->start_id = NULL;
1385 return err;
1388 static const struct got_error *
1389 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1390 struct got_repository *repo, const char *path, int check_disk)
1392 const struct got_error *err = NULL;
1393 struct tog_log_view_state *s = &view->state.log;
1394 struct got_repository *thread_repo = NULL;
1395 struct got_commit_graph *thread_graph = NULL;
1396 int errcode;
1398 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1399 if (err != NULL)
1400 goto done;
1402 /* The commit queue only contains commits being displayed. */
1403 TAILQ_INIT(&s->commits.head);
1404 s->commits.ncommits = 0;
1406 s->repo = repo;
1407 s->start_id = got_object_id_dup(start_id);
1408 if (s->start_id == NULL) {
1409 err = got_error_from_errno();
1410 goto done;
1413 view->show = show_log_view;
1414 view->input = input_log_view;
1415 view->close = close_log_view;
1417 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1418 if (err)
1419 goto done;
1420 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1421 0, thread_repo);
1422 if (err)
1423 goto done;
1425 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1426 if (errcode) {
1427 err = got_error_set_errno(errcode);
1428 goto done;
1431 s->thread_args.commits_needed = view->nlines;
1432 s->thread_args.graph = thread_graph;
1433 s->thread_args.commits = &s->commits;
1434 s->thread_args.in_repo_path = s->in_repo_path;
1435 s->thread_args.start_id = s->start_id;
1436 s->thread_args.repo = thread_repo;
1437 s->thread_args.log_complete = 0;
1438 s->thread_args.quit = &s->quit;
1439 s->thread_args.view = view;
1440 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1441 s->thread_args.selected_entry = &s->selected_entry;
1442 done:
1443 if (err)
1444 close_log_view(view);
1445 return err;
1448 static const struct got_error *
1449 show_log_view(struct tog_view *view)
1451 struct tog_log_view_state *s = &view->state.log;
1453 if (s->thread == NULL) {
1454 int errcode = pthread_create(&s->thread, NULL, log_thread,
1455 &s->thread_args);
1456 if (errcode)
1457 return got_error_set_errno(errcode);
1460 return draw_commits(view, &s->last_displayed_entry,
1461 &s->selected_entry, s->first_displayed_entry,
1462 &s->commits, s->selected, view->nlines,
1463 s->in_repo_path, s->thread_args.commits_needed);
1466 static const struct got_error *
1467 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1468 struct tog_view **focus_view, struct tog_view *view, int ch)
1470 const struct got_error *err = NULL;
1471 struct tog_log_view_state *s = &view->state.log;
1472 char *parent_path;
1473 struct tog_view *diff_view = NULL, *tree_view = NULL;
1474 int begin_x = 0;
1476 switch (ch) {
1477 case 'q':
1478 s->quit = 1;
1479 break;
1480 case 'k':
1481 case KEY_UP:
1482 if (s->first_displayed_entry == NULL)
1483 break;
1484 if (s->selected > 0)
1485 s->selected--;
1486 if (s->selected > 0)
1487 break;
1488 scroll_up(&s->first_displayed_entry, 1,
1489 &s->commits);
1490 break;
1491 case KEY_PPAGE:
1492 if (s->first_displayed_entry == NULL)
1493 break;
1494 if (TAILQ_FIRST(&s->commits.head) ==
1495 s->first_displayed_entry) {
1496 s->selected = 0;
1497 break;
1499 scroll_up(&s->first_displayed_entry,
1500 view->nlines, &s->commits);
1501 break;
1502 case 'j':
1503 case KEY_DOWN:
1504 if (s->first_displayed_entry == NULL)
1505 break;
1506 if (s->selected < MIN(view->nlines - 2,
1507 s->commits.ncommits - 1)) {
1508 s->selected++;
1509 break;
1511 err = scroll_down(&s->first_displayed_entry, 1,
1512 &s->last_displayed_entry, &s->commits,
1513 &s->thread_args.log_complete,
1514 &s->thread_args.commits_needed,
1515 &s->thread_args.need_commits);
1516 break;
1517 case KEY_NPAGE: {
1518 struct commit_queue_entry *first;
1519 first = s->first_displayed_entry;
1520 if (first == NULL)
1521 break;
1522 err = scroll_down(&s->first_displayed_entry,
1523 view->nlines, &s->last_displayed_entry,
1524 &s->commits, &s->thread_args.log_complete,
1525 &s->thread_args.commits_needed,
1526 &s->thread_args.need_commits);
1527 if (first == s->first_displayed_entry &&
1528 s->selected < MIN(view->nlines - 2,
1529 s->commits.ncommits - 1)) {
1530 /* can't scroll further down */
1531 s->selected = MIN(view->nlines - 2,
1532 s->commits.ncommits - 1);
1534 err = NULL;
1535 break;
1537 case KEY_RESIZE:
1538 if (s->selected > view->nlines - 2)
1539 s->selected = view->nlines - 2;
1540 if (s->selected > s->commits.ncommits - 1)
1541 s->selected = s->commits.ncommits - 1;
1542 break;
1543 case KEY_ENTER:
1544 case '\r':
1545 if (s->selected_entry == NULL)
1546 break;
1547 if (view_is_parent_view(view))
1548 begin_x = view_split_begin_x(view->begin_x);
1549 err = open_diff_view_for_commit(&diff_view, begin_x,
1550 s->selected_entry->id, s->selected_entry->commit,
1551 s->repo);
1552 if (err)
1553 break;
1554 if (view_is_parent_view(view)) {
1555 err = view_close_child(view);
1556 if (err)
1557 return err;
1558 err = view_set_child(view, diff_view);
1559 if (err) {
1560 view_close(diff_view);
1561 break;
1563 if (!view_is_splitscreen(diff_view)) {
1564 *focus_view = diff_view;
1565 view->child_focussed = 1;
1567 } else
1568 *new_view = diff_view;
1569 break;
1570 case 't':
1571 if (s->selected_entry == NULL)
1572 break;
1573 if (view_is_parent_view(view))
1574 begin_x = view_split_begin_x(view->begin_x);
1575 err = browse_commit(&tree_view, begin_x,
1576 s->selected_entry, s->repo);
1577 if (err)
1578 break;
1579 if (view_is_parent_view(view)) {
1580 err = view_close_child(view);
1581 if (err)
1582 return err;
1583 err = view_set_child(view, tree_view);
1584 if (err) {
1585 view_close(tree_view);
1586 break;
1588 *focus_view = tree_view;
1589 view->child_focussed = 1;
1590 } else
1591 *new_view = tree_view;
1592 break;
1593 case KEY_BACKSPACE:
1594 if (strcmp(s->in_repo_path, "/") == 0)
1595 break;
1596 parent_path = dirname(s->in_repo_path);
1597 if (parent_path && strcmp(parent_path, ".") != 0) {
1598 struct tog_view *lv;
1599 err = stop_log_thread(s);
1600 if (err)
1601 return err;
1602 lv = view_open(view->nlines, view->ncols,
1603 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1604 if (lv == NULL)
1605 return got_error_from_errno();
1606 err = open_log_view(lv, s->start_id, s->repo,
1607 parent_path, 0);
1608 if (err)
1609 return err;;
1610 if (view_is_parent_view(view))
1611 *new_view = lv;
1612 else {
1613 view_set_child(view->parent, lv);
1614 *focus_view = lv;
1616 return NULL;
1618 break;
1619 default:
1620 break;
1623 return err;
1626 static const struct got_error *
1627 cmd_log(int argc, char *argv[])
1629 const struct got_error *error;
1630 struct got_repository *repo = NULL;
1631 struct got_object_id *start_id = NULL;
1632 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1633 char *start_commit = NULL;
1634 int ch;
1635 struct tog_view *view;
1637 #ifndef PROFILE
1638 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1639 == -1)
1640 err(1, "pledge");
1641 #endif
1643 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1644 switch (ch) {
1645 case 'c':
1646 start_commit = optarg;
1647 break;
1648 case 'r':
1649 repo_path = realpath(optarg, NULL);
1650 if (repo_path == NULL)
1651 err(1, "-r option");
1652 break;
1653 default:
1654 usage();
1655 /* NOTREACHED */
1659 argc -= optind;
1660 argv += optind;
1662 if (argc == 0)
1663 path = strdup("");
1664 else if (argc == 1)
1665 path = strdup(argv[0]);
1666 else
1667 usage_log();
1668 if (path == NULL)
1669 return got_error_from_errno();
1671 cwd = getcwd(NULL, 0);
1672 if (cwd == NULL) {
1673 error = got_error_from_errno();
1674 goto done;
1676 if (repo_path == NULL) {
1677 repo_path = strdup(cwd);
1678 if (repo_path == NULL) {
1679 error = got_error_from_errno();
1680 goto done;
1684 error = got_repo_open(&repo, repo_path);
1685 if (error != NULL)
1686 goto done;
1688 if (start_commit == NULL) {
1689 error = get_head_commit_id(&start_id, repo);
1690 if (error != NULL)
1691 goto done;
1692 } else {
1693 struct got_object *obj;
1694 error = got_object_open_by_id_str(&obj, repo, start_commit);
1695 if (error == NULL) {
1696 start_id = got_object_id_dup(got_object_get_id(obj));
1697 if (start_id == NULL)
1698 error = got_error_from_errno();
1699 goto done;
1702 if (error != NULL)
1703 goto done;
1705 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1706 if (view == NULL) {
1707 error = got_error_from_errno();
1708 goto done;
1710 error = open_log_view(view, start_id, repo, path, 1);
1711 if (error)
1712 goto done;
1713 error = view_loop(view);
1714 done:
1715 free(repo_path);
1716 free(cwd);
1717 free(path);
1718 free(start_id);
1719 if (repo)
1720 got_repo_close(repo);
1721 return error;
1724 __dead static void
1725 usage_diff(void)
1727 endwin();
1728 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1729 getprogname());
1730 exit(1);
1733 static char *
1734 parse_next_line(FILE *f, size_t *len)
1736 char *line;
1737 size_t linelen;
1738 size_t lineno;
1739 const char delim[3] = { '\0', '\0', '\0'};
1741 line = fparseln(f, &linelen, &lineno, delim, 0);
1742 if (len)
1743 *len = linelen;
1744 return line;
1747 static const struct got_error *
1748 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1749 int *last_displayed_line, int *eof, int max_lines,
1750 char * header)
1752 const struct got_error *err;
1753 int nlines = 0, nprinted = 0;
1754 char *line;
1755 size_t len;
1756 wchar_t *wline;
1757 int width;
1759 rewind(f);
1760 werase(view->window);
1762 if (header) {
1763 err = format_line(&wline, &width, header, view->ncols);
1764 if (err) {
1765 return err;
1768 if (view_needs_focus_indication(view))
1769 wstandout(view->window);
1770 waddwstr(view->window, wline);
1771 if (view_needs_focus_indication(view))
1772 wstandend(view->window);
1773 if (width < view->ncols)
1774 waddch(view->window, '\n');
1776 if (max_lines <= 1)
1777 return NULL;
1778 max_lines--;
1781 *eof = 0;
1782 while (nprinted < max_lines) {
1783 line = parse_next_line(f, &len);
1784 if (line == NULL) {
1785 *eof = 1;
1786 break;
1788 if (++nlines < *first_displayed_line) {
1789 free(line);
1790 continue;
1793 err = format_line(&wline, &width, line, view->ncols);
1794 if (err) {
1795 free(line);
1796 return err;
1798 waddwstr(view->window, wline);
1799 if (width < view->ncols)
1800 waddch(view->window, '\n');
1801 if (++nprinted == 1)
1802 *first_displayed_line = nlines;
1803 free(line);
1804 free(wline);
1805 wline = NULL;
1807 *last_displayed_line = nlines;
1809 view_vborder(view);
1811 return NULL;
1814 static const struct got_error *
1815 create_diff(struct tog_diff_view_state *s)
1817 const struct got_error *err = NULL;
1818 struct got_object *obj1 = NULL, *obj2 = NULL;
1819 FILE *f = NULL;
1821 if (s->id1) {
1822 err = got_object_open(&obj1, s->repo, s->id1);
1823 if (err)
1824 return err;
1827 err = got_object_open(&obj2, s->repo, s->id2);
1828 if (err)
1829 goto done;
1831 f = got_opentemp();
1832 if (f == NULL) {
1833 err = got_error_from_errno();
1834 goto done;
1836 if (s->f)
1837 fclose(s->f);
1838 s->f = f;
1840 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1841 case GOT_OBJ_TYPE_BLOB:
1842 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1843 s->diff_context, s->repo, f);
1844 break;
1845 case GOT_OBJ_TYPE_TREE:
1846 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1847 s->diff_context, s->repo, f);
1848 break;
1849 case GOT_OBJ_TYPE_COMMIT:
1850 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1851 s->repo, f);
1852 break;
1853 default:
1854 err = got_error(GOT_ERR_OBJ_TYPE);
1855 break;
1857 done:
1858 if (obj1)
1859 got_object_close(obj1);
1860 got_object_close(obj2);
1861 if (f)
1862 fflush(f);
1863 return err;
1866 static const struct got_error *
1867 open_diff_view(struct tog_view *view, struct got_object *obj1,
1868 struct got_object *obj2, struct got_repository *repo)
1870 const struct got_error *err;
1872 if (obj1 != NULL && obj2 != NULL &&
1873 got_object_get_type(obj1) != got_object_get_type(obj2))
1874 return got_error(GOT_ERR_OBJ_TYPE);
1876 if (obj1) {
1877 struct got_object_id *id1;
1878 id1 = got_object_id_dup(got_object_get_id(obj1));
1879 if (id1 == NULL)
1880 return got_error_from_errno();
1881 view->state.diff.id1 = id1;
1882 } else
1883 view->state.diff.id1 = NULL;
1885 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1886 if (view->state.diff.id2 == NULL) {
1887 free(view->state.diff.id1);
1888 view->state.diff.id1 = NULL;
1889 return got_error_from_errno();
1891 view->state.diff.f = NULL;
1892 view->state.diff.first_displayed_line = 1;
1893 view->state.diff.last_displayed_line = view->nlines;
1894 view->state.diff.diff_context = 3;
1895 view->state.diff.repo = repo;
1897 err = create_diff(&view->state.diff);
1898 if (err) {
1899 free(view->state.diff.id1);
1900 view->state.diff.id1 = NULL;
1901 free(view->state.diff.id2);
1902 view->state.diff.id2 = NULL;
1903 return err;
1906 view->show = show_diff_view;
1907 view->input = input_diff_view;
1908 view->close = close_diff_view;
1910 return NULL;
1913 static const struct got_error *
1914 close_diff_view(struct tog_view *view)
1916 const struct got_error *err = NULL;
1918 free(view->state.diff.id1);
1919 view->state.diff.id1 = NULL;
1920 free(view->state.diff.id2);
1921 view->state.diff.id2 = NULL;
1922 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1923 err = got_error_from_errno();
1924 return err;
1927 static const struct got_error *
1928 show_diff_view(struct tog_view *view)
1930 const struct got_error *err;
1931 struct tog_diff_view_state *s = &view->state.diff;
1932 char *id_str1 = NULL, *id_str2, *header;
1934 if (s->id1) {
1935 err = got_object_id_str(&id_str1, s->id1);
1936 if (err)
1937 return err;
1939 err = got_object_id_str(&id_str2, s->id2);
1940 if (err)
1941 return err;
1943 if (asprintf(&header, "diff: %s %s",
1944 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1945 err = got_error_from_errno();
1946 free(id_str1);
1947 free(id_str2);
1948 return err;
1950 free(id_str1);
1951 free(id_str2);
1953 return draw_file(view, s->f, &s->first_displayed_line,
1954 &s->last_displayed_line, &s->eof, view->nlines,
1955 header);
1958 static const struct got_error *
1959 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1960 struct tog_view **focus_view, struct tog_view *view, int ch)
1962 const struct got_error *err = NULL;
1963 struct tog_diff_view_state *s = &view->state.diff;
1964 int i;
1966 switch (ch) {
1967 case 'k':
1968 case KEY_UP:
1969 if (s->first_displayed_line > 1)
1970 s->first_displayed_line--;
1971 break;
1972 case KEY_PPAGE:
1973 i = 0;
1974 while (i++ < view->nlines - 1 &&
1975 s->first_displayed_line > 1)
1976 s->first_displayed_line--;
1977 break;
1978 case 'j':
1979 case KEY_DOWN:
1980 if (!s->eof)
1981 s->first_displayed_line++;
1982 break;
1983 case KEY_NPAGE:
1984 case ' ':
1985 i = 0;
1986 while (!s->eof && i++ < view->nlines - 1) {
1987 char *line;
1988 line = parse_next_line(s->f, NULL);
1989 s->first_displayed_line++;
1990 if (line == NULL)
1991 break;
1993 break;
1994 case '[':
1995 if (s->diff_context > 0) {
1996 s->diff_context--;
1997 err = create_diff(s);
1999 break;
2000 case ']':
2001 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2002 s->diff_context++;
2003 err = create_diff(s);
2005 break;
2006 default:
2007 break;
2010 return err;
2013 static const struct got_error *
2014 cmd_diff(int argc, char *argv[])
2016 const struct got_error *error = NULL;
2017 struct got_repository *repo = NULL;
2018 struct got_object *obj1 = NULL, *obj2 = NULL;
2019 char *repo_path = NULL;
2020 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
2021 int ch;
2022 struct tog_view *view;
2024 #ifndef PROFILE
2025 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2026 == -1)
2027 err(1, "pledge");
2028 #endif
2030 while ((ch = getopt(argc, argv, "")) != -1) {
2031 switch (ch) {
2032 default:
2033 usage();
2034 /* NOTREACHED */
2038 argc -= optind;
2039 argv += optind;
2041 if (argc == 0) {
2042 usage_diff(); /* TODO show local worktree changes */
2043 } else if (argc == 2) {
2044 repo_path = getcwd(NULL, 0);
2045 if (repo_path == NULL)
2046 return got_error_from_errno();
2047 obj_id_str1 = argv[0];
2048 obj_id_str2 = argv[1];
2049 } else if (argc == 3) {
2050 repo_path = realpath(argv[0], NULL);
2051 if (repo_path == NULL)
2052 return got_error_from_errno();
2053 obj_id_str1 = argv[1];
2054 obj_id_str2 = argv[2];
2055 } else
2056 usage_diff();
2058 error = got_repo_open(&repo, repo_path);
2059 free(repo_path);
2060 if (error)
2061 goto done;
2063 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2064 if (error)
2065 goto done;
2067 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2068 if (error)
2069 goto done;
2071 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2072 if (view == NULL) {
2073 error = got_error_from_errno();
2074 goto done;
2076 error = open_diff_view(view, obj1, obj2, repo);
2077 if (error)
2078 goto done;
2079 error = view_loop(view);
2080 done:
2081 got_repo_close(repo);
2082 if (obj1)
2083 got_object_close(obj1);
2084 if (obj2)
2085 got_object_close(obj2);
2086 return error;
2089 __dead static void
2090 usage_blame(void)
2092 endwin();
2093 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2094 getprogname());
2095 exit(1);
2098 struct tog_blame_line {
2099 int annotated;
2100 struct got_object_id *id;
2103 static const struct got_error *
2104 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2105 const char *path, struct tog_blame_line *lines, int nlines,
2106 int blame_complete, int selected_line, int *first_displayed_line,
2107 int *last_displayed_line, int *eof, int max_lines)
2109 const struct got_error *err;
2110 int lineno = 0, nprinted = 0;
2111 char *line;
2112 size_t len;
2113 wchar_t *wline;
2114 int width, wlimit;
2115 struct tog_blame_line *blame_line;
2116 struct got_object_id *prev_id = NULL;
2117 char *id_str;
2119 err = got_object_id_str(&id_str, id);
2120 if (err)
2121 return err;
2123 rewind(f);
2124 werase(view->window);
2126 if (asprintf(&line, "commit: %s", id_str) == -1) {
2127 err = got_error_from_errno();
2128 free(id_str);
2129 return err;
2132 err = format_line(&wline, &width, line, view->ncols);
2133 free(line);
2134 line = NULL;
2135 if (view_needs_focus_indication(view))
2136 wstandout(view->window);
2137 waddwstr(view->window, wline);
2138 if (view_needs_focus_indication(view))
2139 wstandend(view->window);
2140 free(wline);
2141 wline = NULL;
2142 if (width < view->ncols)
2143 waddch(view->window, '\n');
2145 if (asprintf(&line, "[%d/%d] %s%s",
2146 *first_displayed_line - 1 + selected_line, nlines,
2147 blame_complete ? "" : "annotating ", path) == -1) {
2148 free(id_str);
2149 return got_error_from_errno();
2151 free(id_str);
2152 err = format_line(&wline, &width, line, view->ncols);
2153 free(line);
2154 line = NULL;
2155 if (err)
2156 return err;
2157 waddwstr(view->window, wline);
2158 free(wline);
2159 wline = NULL;
2160 if (width < view->ncols)
2161 waddch(view->window, '\n');
2163 *eof = 0;
2164 while (nprinted < max_lines - 2) {
2165 line = parse_next_line(f, &len);
2166 if (line == NULL) {
2167 *eof = 1;
2168 break;
2170 if (++lineno < *first_displayed_line) {
2171 free(line);
2172 continue;
2175 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2176 err = format_line(&wline, &width, line, wlimit);
2177 if (err) {
2178 free(line);
2179 return err;
2182 if (view->focussed && nprinted == selected_line - 1)
2183 wstandout(view->window);
2185 blame_line = &lines[lineno - 1];
2186 if (blame_line->annotated && prev_id &&
2187 got_object_id_cmp(prev_id, blame_line->id) == 0)
2188 waddstr(view->window, " ");
2189 else if (blame_line->annotated) {
2190 char *id_str;
2191 err = got_object_id_str(&id_str, blame_line->id);
2192 if (err) {
2193 free(line);
2194 free(wline);
2195 return err;
2197 wprintw(view->window, "%.8s ", id_str);
2198 free(id_str);
2199 prev_id = blame_line->id;
2200 } else {
2201 waddstr(view->window, "........ ");
2202 prev_id = NULL;
2205 waddwstr(view->window, wline);
2206 while (width < wlimit) {
2207 waddch(view->window, ' ');
2208 width++;
2210 if (view->focussed && nprinted == selected_line - 1)
2211 wstandend(view->window);
2212 if (++nprinted == 1)
2213 *first_displayed_line = lineno;
2214 free(line);
2215 free(wline);
2216 wline = NULL;
2218 *last_displayed_line = lineno;
2220 view_vborder(view);
2222 return NULL;
2225 static const struct got_error *
2226 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2228 const struct got_error *err = NULL;
2229 struct tog_blame_cb_args *a = arg;
2230 struct tog_blame_line *line;
2231 int errcode;
2233 if (nlines != a->nlines ||
2234 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2235 return got_error(GOT_ERR_RANGE);
2237 errcode = pthread_mutex_lock(&tog_mutex);
2238 if (errcode)
2239 return got_error_set_errno(errcode);
2241 if (*a->quit) { /* user has quit the blame view */
2242 err = got_error(GOT_ERR_ITER_COMPLETED);
2243 goto done;
2246 if (lineno == -1)
2247 goto done; /* no change in this commit */
2249 line = &a->lines[lineno - 1];
2250 if (line->annotated)
2251 goto done;
2253 line->id = got_object_id_dup(id);
2254 if (line->id == NULL) {
2255 err = got_error_from_errno();
2256 goto done;
2258 line->annotated = 1;
2259 done:
2260 errcode = pthread_mutex_unlock(&tog_mutex);
2261 if (errcode)
2262 err = got_error_set_errno(errcode);
2263 return err;
2266 static void *
2267 blame_thread(void *arg)
2269 const struct got_error *err;
2270 struct tog_blame_thread_args *ta = arg;
2271 struct tog_blame_cb_args *a = ta->cb_args;
2272 int errcode;
2274 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2275 blame_cb, ta->cb_args);
2277 errcode = pthread_mutex_lock(&tog_mutex);
2278 if (errcode)
2279 return (void *)got_error_set_errno(errcode);
2281 got_repo_close(ta->repo);
2282 ta->repo = NULL;
2283 *ta->complete = 1;
2285 errcode = pthread_mutex_unlock(&tog_mutex);
2286 if (errcode && err == NULL)
2287 err = got_error_set_errno(errcode);
2289 return (void *)err;
2292 static struct got_object_id *
2293 get_selected_commit_id(struct tog_blame_line *lines,
2294 int first_displayed_line, int selected_line)
2296 struct tog_blame_line *line;
2298 line = &lines[first_displayed_line - 1 + selected_line - 1];
2299 if (!line->annotated)
2300 return NULL;
2302 return line->id;
2305 static const struct got_error *
2306 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2307 struct tog_blame_line *lines, int first_displayed_line,
2308 int selected_line, struct got_repository *repo)
2310 const struct got_error *err = NULL;
2311 struct got_commit_object *commit = NULL;
2312 struct got_object_id *selected_id;
2313 struct got_object_qid *pid;
2315 *pobj = NULL;
2316 *obj = NULL;
2318 selected_id = get_selected_commit_id(lines,
2319 first_displayed_line, selected_line);
2320 if (selected_id == NULL)
2321 return NULL;
2323 err = got_object_open(obj, repo, selected_id);
2324 if (err)
2325 goto done;
2327 err = got_object_commit_open(&commit, repo, *obj);
2328 if (err)
2329 goto done;
2331 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2332 if (pid) {
2333 err = got_object_open(pobj, repo, pid->id);
2334 if (err)
2335 goto done;
2337 done:
2338 if (commit)
2339 got_object_commit_close(commit);
2340 return err;
2343 static const struct got_error *
2344 stop_blame(struct tog_blame *blame)
2346 const struct got_error *err = NULL;
2347 int i;
2349 if (blame->thread) {
2350 int errcode;
2351 errcode = pthread_mutex_unlock(&tog_mutex);
2352 if (errcode)
2353 return got_error_set_errno(errcode);
2354 errcode = pthread_join(blame->thread, (void **)&err);
2355 if (errcode)
2356 return got_error_set_errno(errcode);
2357 errcode = pthread_mutex_lock(&tog_mutex);
2358 if (errcode)
2359 return got_error_set_errno(errcode);
2360 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2361 err = NULL;
2362 blame->thread = NULL;
2364 if (blame->thread_args.repo) {
2365 got_repo_close(blame->thread_args.repo);
2366 blame->thread_args.repo = NULL;
2368 if (blame->f) {
2369 fclose(blame->f);
2370 blame->f = NULL;
2372 for (i = 0; i < blame->nlines; i++)
2373 free(blame->lines[i].id);
2374 free(blame->lines);
2375 blame->lines = NULL;
2376 free(blame->cb_args.commit_id);
2377 blame->cb_args.commit_id = NULL;
2379 return err;
2382 static const struct got_error *
2383 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2384 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2385 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2386 struct got_repository *repo)
2388 const struct got_error *err = NULL;
2389 struct got_blob_object *blob = NULL;
2390 struct got_repository *thread_repo = NULL;
2391 struct got_object_id *obj_id = NULL;
2392 struct got_object *obj = NULL;
2394 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2395 if (err)
2396 goto done;
2398 err = got_object_open(&obj, repo, obj_id);
2399 if (err)
2400 goto done;
2402 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2403 err = got_error(GOT_ERR_OBJ_TYPE);
2404 goto done;
2407 err = got_object_blob_open(&blob, repo, obj, 8192);
2408 if (err)
2409 goto done;
2410 blame->f = got_opentemp();
2411 if (blame->f == NULL) {
2412 err = got_error_from_errno();
2413 goto done;
2415 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2416 blame->f, blob);
2417 if (err)
2418 goto done;
2420 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2421 if (blame->lines == NULL) {
2422 err = got_error_from_errno();
2423 goto done;
2426 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2427 if (err)
2428 goto done;
2430 blame->cb_args.view = view;
2431 blame->cb_args.lines = blame->lines;
2432 blame->cb_args.nlines = blame->nlines;
2433 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2434 if (blame->cb_args.commit_id == NULL) {
2435 err = got_error_from_errno();
2436 goto done;
2438 blame->cb_args.quit = done;
2440 blame->thread_args.path = path;
2441 blame->thread_args.repo = thread_repo;
2442 blame->thread_args.cb_args = &blame->cb_args;
2443 blame->thread_args.complete = blame_complete;
2444 *blame_complete = 0;
2446 done:
2447 if (blob)
2448 got_object_blob_close(blob);
2449 free(obj_id);
2450 if (obj)
2451 got_object_close(obj);
2452 if (err)
2453 stop_blame(blame);
2454 return err;
2457 static const struct got_error *
2458 open_blame_view(struct tog_view *view, char *path,
2459 struct got_object_id *commit_id, struct got_repository *repo)
2461 const struct got_error *err = NULL;
2462 struct tog_blame_view_state *s = &view->state.blame;
2464 SIMPLEQ_INIT(&s->blamed_commits);
2466 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2467 if (err)
2468 return err;
2470 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2471 s->first_displayed_line = 1;
2472 s->last_displayed_line = view->nlines;
2473 s->selected_line = 1;
2474 s->blame_complete = 0;
2475 s->path = path;
2476 if (s->path == NULL)
2477 return got_error_from_errno();
2478 s->repo = repo;
2479 s->commit_id = commit_id;
2480 memset(&s->blame, 0, sizeof(s->blame));
2482 view->show = show_blame_view;
2483 view->input = input_blame_view;
2484 view->close = close_blame_view;
2486 return run_blame(&s->blame, view, &s->blame_complete,
2487 &s->first_displayed_line, &s->last_displayed_line,
2488 &s->selected_line, &s->done, &s->eof, s->path,
2489 s->blamed_commit->id, s->repo);
2492 static const struct got_error *
2493 close_blame_view(struct tog_view *view)
2495 const struct got_error *err = NULL;
2496 struct tog_blame_view_state *s = &view->state.blame;
2498 if (s->blame.thread)
2499 err = stop_blame(&s->blame);
2501 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2502 struct got_object_qid *blamed_commit;
2503 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2504 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2505 got_object_qid_free(blamed_commit);
2508 free(s->path);
2510 return err;
2513 static const struct got_error *
2514 show_blame_view(struct tog_view *view)
2516 const struct got_error *err = NULL;
2517 struct tog_blame_view_state *s = &view->state.blame;
2518 int errcode;
2520 if (s->blame.thread == NULL) {
2521 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2522 &s->blame.thread_args);
2523 if (errcode)
2524 return got_error_set_errno(errcode);
2527 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2528 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2529 s->selected_line, &s->first_displayed_line,
2530 &s->last_displayed_line, &s->eof, view->nlines);
2532 view_vborder(view);
2533 return err;
2536 static const struct got_error *
2537 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2538 struct tog_view **focus_view, struct tog_view *view, int ch)
2540 const struct got_error *err = NULL, *thread_err = NULL;
2541 struct got_object *obj = NULL, *pobj = NULL;
2542 struct tog_view *diff_view;
2543 struct tog_blame_view_state *s = &view->state.blame;
2544 int begin_x = 0;
2546 switch (ch) {
2547 case 'q':
2548 s->done = 1;
2549 break;
2550 case 'k':
2551 case KEY_UP:
2552 if (s->selected_line > 1)
2553 s->selected_line--;
2554 else if (s->selected_line == 1 &&
2555 s->first_displayed_line > 1)
2556 s->first_displayed_line--;
2557 break;
2558 case KEY_PPAGE:
2559 if (s->first_displayed_line == 1) {
2560 s->selected_line = 1;
2561 break;
2563 if (s->first_displayed_line > view->nlines - 2)
2564 s->first_displayed_line -=
2565 (view->nlines - 2);
2566 else
2567 s->first_displayed_line = 1;
2568 break;
2569 case 'j':
2570 case KEY_DOWN:
2571 if (s->selected_line < view->nlines - 2 &&
2572 s->first_displayed_line +
2573 s->selected_line <= s->blame.nlines)
2574 s->selected_line++;
2575 else if (s->last_displayed_line <
2576 s->blame.nlines)
2577 s->first_displayed_line++;
2578 break;
2579 case 'b':
2580 case 'p': {
2581 struct got_object_id *id;
2582 id = get_selected_commit_id(s->blame.lines,
2583 s->first_displayed_line, s->selected_line);
2584 if (id == NULL || got_object_id_cmp(id,
2585 s->blamed_commit->id) == 0)
2586 break;
2587 err = open_selected_commit(&pobj, &obj,
2588 s->blame.lines, s->first_displayed_line,
2589 s->selected_line, s->repo);
2590 if (err)
2591 break;
2592 if (pobj == NULL && obj == NULL)
2593 break;
2594 if (ch == 'p' && pobj == NULL)
2595 break;
2596 s->done = 1;
2597 thread_err = stop_blame(&s->blame);
2598 s->done = 0;
2599 if (thread_err)
2600 break;
2601 id = got_object_get_id(ch == 'b' ? obj : pobj);
2602 got_object_close(obj);
2603 obj = NULL;
2604 if (pobj) {
2605 got_object_close(pobj);
2606 pobj = NULL;
2608 err = got_object_qid_alloc(&s->blamed_commit, id);
2609 if (err)
2610 goto done;
2611 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2612 s->blamed_commit, entry);
2613 err = run_blame(&s->blame, view, &s->blame_complete,
2614 &s->first_displayed_line, &s->last_displayed_line,
2615 &s->selected_line, &s->done, &s->eof,
2616 s->path, s->blamed_commit->id, s->repo);
2617 if (err)
2618 break;
2619 break;
2621 case 'B': {
2622 struct got_object_qid *first;
2623 first = SIMPLEQ_FIRST(&s->blamed_commits);
2624 if (!got_object_id_cmp(first->id, s->commit_id))
2625 break;
2626 s->done = 1;
2627 thread_err = stop_blame(&s->blame);
2628 s->done = 0;
2629 if (thread_err)
2630 break;
2631 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2632 got_object_qid_free(s->blamed_commit);
2633 s->blamed_commit =
2634 SIMPLEQ_FIRST(&s->blamed_commits);
2635 err = run_blame(&s->blame, view, &s->blame_complete,
2636 &s->first_displayed_line, &s->last_displayed_line,
2637 &s->selected_line, &s->done, &s->eof, s->path,
2638 s->blamed_commit->id, s->repo);
2639 if (err)
2640 break;
2641 break;
2643 case KEY_ENTER:
2644 case '\r':
2645 err = open_selected_commit(&pobj, &obj,
2646 s->blame.lines, s->first_displayed_line,
2647 s->selected_line, s->repo);
2648 if (err)
2649 break;
2650 if (pobj == NULL && obj == NULL)
2651 break;
2653 if (view_is_parent_view(view))
2654 begin_x = view_split_begin_x(view->begin_x);
2655 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2656 if (diff_view == NULL) {
2657 err = got_error_from_errno();
2658 break;
2660 err = open_diff_view(diff_view, pobj, obj, s->repo);
2661 if (err) {
2662 view_close(diff_view);
2663 break;
2665 if (view_is_parent_view(view)) {
2666 err = view_close_child(view);
2667 if (err)
2668 return err;
2669 err = view_set_child(view, diff_view);
2670 if (err) {
2671 view_close(diff_view);
2672 break;
2674 if (!view_is_splitscreen(diff_view)) {
2675 *focus_view = diff_view;
2676 view->child_focussed = 1;
2678 } else
2679 *new_view = diff_view;
2680 if (pobj) {
2681 got_object_close(pobj);
2682 pobj = NULL;
2684 got_object_close(obj);
2685 obj = NULL;
2686 if (err)
2687 break;
2688 break;
2689 case KEY_NPAGE:
2690 case ' ':
2691 if (s->last_displayed_line >= s->blame.nlines &&
2692 s->selected_line < view->nlines - 2) {
2693 s->selected_line = MIN(s->blame.nlines,
2694 view->nlines - 2);
2695 break;
2697 if (s->last_displayed_line + view->nlines - 2
2698 <= s->blame.nlines)
2699 s->first_displayed_line +=
2700 view->nlines - 2;
2701 else
2702 s->first_displayed_line =
2703 s->blame.nlines -
2704 (view->nlines - 3);
2705 break;
2706 case KEY_RESIZE:
2707 if (s->selected_line > view->nlines - 2) {
2708 s->selected_line = MIN(s->blame.nlines,
2709 view->nlines - 2);
2711 break;
2712 default:
2713 break;
2715 done:
2716 if (pobj)
2717 got_object_close(pobj);
2718 return thread_err ? thread_err : err;
2721 static const struct got_error *
2722 cmd_blame(int argc, char *argv[])
2724 const struct got_error *error;
2725 struct got_repository *repo = NULL;
2726 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2727 struct got_object_id *commit_id = NULL;
2728 char *commit_id_str = NULL;
2729 int ch;
2730 struct tog_view *view;
2732 #ifndef PROFILE
2733 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2734 == -1)
2735 err(1, "pledge");
2736 #endif
2738 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2739 switch (ch) {
2740 case 'c':
2741 commit_id_str = optarg;
2742 break;
2743 case 'r':
2744 repo_path = realpath(optarg, NULL);
2745 if (repo_path == NULL)
2746 err(1, "-r option");
2747 break;
2748 default:
2749 usage();
2750 /* NOTREACHED */
2754 argc -= optind;
2755 argv += optind;
2757 if (argc == 1)
2758 path = argv[0];
2759 else
2760 usage_blame();
2762 cwd = getcwd(NULL, 0);
2763 if (cwd == NULL) {
2764 error = got_error_from_errno();
2765 goto done;
2767 if (repo_path == NULL) {
2768 repo_path = strdup(cwd);
2769 if (repo_path == NULL) {
2770 error = got_error_from_errno();
2771 goto done;
2776 error = got_repo_open(&repo, repo_path);
2777 if (error != NULL)
2778 return error;
2780 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2781 if (error != NULL)
2782 goto done;
2784 if (commit_id_str == NULL) {
2785 struct got_reference *head_ref;
2786 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2787 if (error != NULL)
2788 goto done;
2789 error = got_ref_resolve(&commit_id, repo, head_ref);
2790 got_ref_close(head_ref);
2791 } else {
2792 struct got_object *obj;
2793 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2794 if (error != NULL)
2795 goto done;
2796 commit_id = got_object_id_dup(got_object_get_id(obj));
2797 if (commit_id == NULL)
2798 error = got_error_from_errno();
2799 got_object_close(obj);
2801 if (error != NULL)
2802 goto done;
2804 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2805 if (view == NULL) {
2806 error = got_error_from_errno();
2807 goto done;
2809 error = open_blame_view(view, in_repo_path, commit_id, repo);
2810 if (error)
2811 goto done;
2812 error = view_loop(view);
2813 done:
2814 free(repo_path);
2815 free(cwd);
2816 free(commit_id);
2817 if (repo)
2818 got_repo_close(repo);
2819 return error;
2822 static const struct got_error *
2823 draw_tree_entries(struct tog_view *view,
2824 struct got_tree_entry **first_displayed_entry,
2825 struct got_tree_entry **last_displayed_entry,
2826 struct got_tree_entry **selected_entry, int *ndisplayed,
2827 const char *label, int show_ids, const char *parent_path,
2828 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2830 const struct got_error *err = NULL;
2831 struct got_tree_entry *te;
2832 wchar_t *wline;
2833 int width, n;
2835 *ndisplayed = 0;
2837 werase(view->window);
2839 if (limit == 0)
2840 return NULL;
2842 err = format_line(&wline, &width, label, view->ncols);
2843 if (err)
2844 return err;
2845 if (view_needs_focus_indication(view))
2846 wstandout(view->window);
2847 waddwstr(view->window, wline);
2848 if (view_needs_focus_indication(view))
2849 wstandend(view->window);
2850 free(wline);
2851 wline = NULL;
2852 if (width < view->ncols)
2853 waddch(view->window, '\n');
2854 if (--limit <= 0)
2855 return NULL;
2856 err = format_line(&wline, &width, parent_path, view->ncols);
2857 if (err)
2858 return err;
2859 waddwstr(view->window, wline);
2860 free(wline);
2861 wline = NULL;
2862 if (width < view->ncols)
2863 waddch(view->window, '\n');
2864 if (--limit <= 0)
2865 return NULL;
2866 waddch(view->window, '\n');
2867 if (--limit <= 0)
2868 return NULL;
2870 te = SIMPLEQ_FIRST(&entries->head);
2871 if (*first_displayed_entry == NULL) {
2872 if (selected == 0) {
2873 if (view->focussed)
2874 wstandout(view->window);
2875 *selected_entry = NULL;
2877 waddstr(view->window, " ..\n"); /* parent directory */
2878 if (selected == 0 && view->focussed)
2879 wstandend(view->window);
2880 (*ndisplayed)++;
2881 if (--limit <= 0)
2882 return NULL;
2883 n = 1;
2884 } else {
2885 n = 0;
2886 while (te != *first_displayed_entry)
2887 te = SIMPLEQ_NEXT(te, entry);
2890 while (te) {
2891 char *line = NULL, *id_str = NULL;
2893 if (show_ids) {
2894 err = got_object_id_str(&id_str, te->id);
2895 if (err)
2896 return got_error_from_errno();
2898 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2899 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2900 free(id_str);
2901 return got_error_from_errno();
2903 free(id_str);
2904 err = format_line(&wline, &width, line, view->ncols);
2905 if (err) {
2906 free(line);
2907 break;
2909 if (n == selected) {
2910 if (view->focussed)
2911 wstandout(view->window);
2912 *selected_entry = te;
2914 waddwstr(view->window, wline);
2915 if (width < view->ncols)
2916 waddch(view->window, '\n');
2917 if (n == selected && view->focussed)
2918 wstandend(view->window);
2919 free(line);
2920 free(wline);
2921 wline = NULL;
2922 n++;
2923 (*ndisplayed)++;
2924 *last_displayed_entry = te;
2925 if (--limit <= 0)
2926 break;
2927 te = SIMPLEQ_NEXT(te, entry);
2930 return err;
2933 static void
2934 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2935 const struct got_tree_entries *entries, int isroot)
2937 struct got_tree_entry *te, *prev;
2938 int i;
2940 if (*first_displayed_entry == NULL)
2941 return;
2943 te = SIMPLEQ_FIRST(&entries->head);
2944 if (*first_displayed_entry == te) {
2945 if (!isroot)
2946 *first_displayed_entry = NULL;
2947 return;
2950 /* XXX this is stupid... switch to TAILQ? */
2951 for (i = 0; i < maxscroll; i++) {
2952 while (te != *first_displayed_entry) {
2953 prev = te;
2954 te = SIMPLEQ_NEXT(te, entry);
2956 *first_displayed_entry = prev;
2957 te = SIMPLEQ_FIRST(&entries->head);
2959 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2960 *first_displayed_entry = NULL;
2963 static void
2964 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2965 struct got_tree_entry *last_displayed_entry,
2966 const struct got_tree_entries *entries)
2968 struct got_tree_entry *next;
2969 int n = 0;
2971 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2972 return;
2974 if (*first_displayed_entry)
2975 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2976 else
2977 next = SIMPLEQ_FIRST(&entries->head);
2978 while (next) {
2979 *first_displayed_entry = next;
2980 if (++n >= maxscroll)
2981 break;
2982 next = SIMPLEQ_NEXT(next, entry);
2986 static const struct got_error *
2987 tree_entry_path(char **path, struct tog_parent_trees *parents,
2988 struct got_tree_entry *te)
2990 const struct got_error *err = NULL;
2991 struct tog_parent_tree *pt;
2992 size_t len = 2; /* for leading slash and NUL */
2994 TAILQ_FOREACH(pt, parents, entry)
2995 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2996 if (te)
2997 len += strlen(te->name);
2999 *path = calloc(1, len);
3000 if (path == NULL)
3001 return got_error_from_errno();
3003 (*path)[0] = '/';
3004 pt = TAILQ_LAST(parents, tog_parent_trees);
3005 while (pt) {
3006 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3007 err = got_error(GOT_ERR_NO_SPACE);
3008 goto done;
3010 if (strlcat(*path, "/", len) >= len) {
3011 err = got_error(GOT_ERR_NO_SPACE);
3012 goto done;
3014 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3016 if (te) {
3017 if (strlcat(*path, te->name, len) >= len) {
3018 err = got_error(GOT_ERR_NO_SPACE);
3019 goto done;
3022 done:
3023 if (err) {
3024 free(*path);
3025 *path = NULL;
3027 return err;
3030 static const struct got_error *
3031 blame_tree_entry(struct tog_view **new_view, int begin_x,
3032 struct got_tree_entry *te, struct tog_parent_trees *parents,
3033 struct got_object_id *commit_id, struct got_repository *repo)
3035 const struct got_error *err = NULL;
3036 char *path;
3037 struct tog_view *blame_view;
3039 err = tree_entry_path(&path, parents, te);
3040 if (err)
3041 return err;
3043 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3044 if (blame_view == NULL)
3045 return got_error_from_errno();
3047 err = open_blame_view(blame_view, path, commit_id, repo);
3048 if (err) {
3049 view_close(blame_view);
3050 free(path);
3051 } else
3052 *new_view = blame_view;
3053 return err;
3056 static const struct got_error *
3057 log_tree_entry(struct tog_view **new_view, int begin_x,
3058 struct got_tree_entry *te, struct tog_parent_trees *parents,
3059 struct got_object_id *commit_id, struct got_repository *repo)
3061 struct tog_view *log_view;
3062 const struct got_error *err = NULL;
3063 char *path;
3065 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3066 if (log_view == NULL)
3067 return got_error_from_errno();
3069 err = tree_entry_path(&path, parents, te);
3070 if (err)
3071 return err;
3073 err = open_log_view(log_view, commit_id, repo, path, 0);
3074 if (err)
3075 view_close(log_view);
3076 else
3077 *new_view = log_view;
3078 free(path);
3079 return err;
3082 static const struct got_error *
3083 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3084 struct got_object_id *commit_id, struct got_repository *repo)
3086 const struct got_error *err = NULL;
3087 char *commit_id_str = NULL;
3088 struct tog_tree_view_state *s = &view->state.tree;
3090 TAILQ_INIT(&s->parents);
3092 err = got_object_id_str(&commit_id_str, commit_id);
3093 if (err != NULL)
3094 goto done;
3096 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3097 err = got_error_from_errno();
3098 goto done;
3101 s->root = s->tree = root;
3102 s->entries = got_object_tree_get_entries(root);
3103 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3104 s->commit_id = got_object_id_dup(commit_id);
3105 if (s->commit_id == NULL) {
3106 err = got_error_from_errno();
3107 goto done;
3109 s->repo = repo;
3111 view->show = show_tree_view;
3112 view->input = input_tree_view;
3113 view->close = close_tree_view;
3114 done:
3115 free(commit_id_str);
3116 if (err) {
3117 free(s->tree_label);
3118 s->tree_label = NULL;
3120 return err;
3123 static const struct got_error *
3124 close_tree_view(struct tog_view *view)
3126 struct tog_tree_view_state *s = &view->state.tree;
3128 free(s->tree_label);
3129 s->tree_label = NULL;
3130 free(s->commit_id);
3131 s->commit_id = NULL;
3132 while (!TAILQ_EMPTY(&s->parents)) {
3133 struct tog_parent_tree *parent;
3134 parent = TAILQ_FIRST(&s->parents);
3135 TAILQ_REMOVE(&s->parents, parent, entry);
3136 free(parent);
3139 if (s->tree != s->root)
3140 got_object_tree_close(s->tree);
3141 got_object_tree_close(s->root);
3143 return NULL;
3146 static const struct got_error *
3147 show_tree_view(struct tog_view *view)
3149 const struct got_error *err = NULL;
3150 struct tog_tree_view_state *s = &view->state.tree;
3151 char *parent_path;
3153 err = tree_entry_path(&parent_path, &s->parents, NULL);
3154 if (err)
3155 return err;
3157 err = draw_tree_entries(view, &s->first_displayed_entry,
3158 &s->last_displayed_entry, &s->selected_entry,
3159 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3160 s->entries, s->selected, view->nlines, s->tree == s->root);
3161 free(parent_path);
3163 view_vborder(view);
3164 return err;
3167 static const struct got_error *
3168 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3169 struct tog_view **focus_view, struct tog_view *view, int ch)
3171 const struct got_error *err = NULL;
3172 struct tog_tree_view_state *s = &view->state.tree;
3173 struct tog_view *log_view;
3174 int begin_x = 0;
3176 switch (ch) {
3177 case 'i':
3178 s->show_ids = !s->show_ids;
3179 break;
3180 case 'l':
3181 if (!s->selected_entry)
3182 break;
3183 if (view_is_parent_view(view))
3184 begin_x = view_split_begin_x(view->begin_x);
3185 err = log_tree_entry(&log_view, begin_x,
3186 s->selected_entry, &s->parents,
3187 s->commit_id, s->repo);
3188 if (view_is_parent_view(view)) {
3189 err = view_close_child(view);
3190 if (err)
3191 return err;
3192 err = view_set_child(view, log_view);
3193 if (err) {
3194 view_close(log_view);
3195 break;
3197 *focus_view = log_view;
3198 view->child_focussed = 1;
3199 } else
3200 *new_view = log_view;
3201 break;
3202 case 'k':
3203 case KEY_UP:
3204 if (s->selected > 0)
3205 s->selected--;
3206 if (s->selected > 0)
3207 break;
3208 tree_scroll_up(&s->first_displayed_entry, 1,
3209 s->entries, s->tree == s->root);
3210 break;
3211 case KEY_PPAGE:
3212 s->selected = 0;
3213 if (SIMPLEQ_FIRST(&s->entries->head) ==
3214 s->first_displayed_entry) {
3215 if (s->tree != s->root)
3216 s->first_displayed_entry = NULL;
3217 break;
3219 tree_scroll_up(&s->first_displayed_entry,
3220 view->nlines, s->entries,
3221 s->tree == s->root);
3222 break;
3223 case 'j':
3224 case KEY_DOWN:
3225 if (s->selected < s->ndisplayed - 1) {
3226 s->selected++;
3227 break;
3229 tree_scroll_down(&s->first_displayed_entry, 1,
3230 s->last_displayed_entry, s->entries);
3231 break;
3232 case KEY_NPAGE:
3233 tree_scroll_down(&s->first_displayed_entry,
3234 view->nlines, s->last_displayed_entry,
3235 s->entries);
3236 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3237 entry))
3238 break;
3239 /* can't scroll any further; move cursor down */
3240 if (s->selected < s->ndisplayed - 1)
3241 s->selected = s->ndisplayed - 1;
3242 break;
3243 case KEY_ENTER:
3244 case '\r':
3245 if (s->selected_entry == NULL) {
3246 struct tog_parent_tree *parent;
3247 case KEY_BACKSPACE:
3248 /* user selected '..' */
3249 if (s->tree == s->root)
3250 break;
3251 parent = TAILQ_FIRST(&s->parents);
3252 TAILQ_REMOVE(&s->parents, parent,
3253 entry);
3254 got_object_tree_close(s->tree);
3255 s->tree = parent->tree;
3256 s->entries =
3257 got_object_tree_get_entries(s->tree);
3258 s->first_displayed_entry =
3259 parent->first_displayed_entry;
3260 s->selected_entry =
3261 parent->selected_entry;
3262 s->selected = parent->selected;
3263 free(parent);
3264 } else if (S_ISDIR(s->selected_entry->mode)) {
3265 struct tog_parent_tree *parent;
3266 struct got_tree_object *child;
3267 err = got_object_open_as_tree(&child,
3268 s->repo, s->selected_entry->id);
3269 if (err)
3270 break;
3271 parent = calloc(1, sizeof(*parent));
3272 if (parent == NULL) {
3273 err = got_error_from_errno();
3274 break;
3276 parent->tree = s->tree;
3277 parent->first_displayed_entry =
3278 s->first_displayed_entry;
3279 parent->selected_entry = s->selected_entry;
3280 parent->selected = s->selected;
3281 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3282 s->tree = child;
3283 s->entries =
3284 got_object_tree_get_entries(s->tree);
3285 s->selected = 0;
3286 s->first_displayed_entry = NULL;
3287 } else if (S_ISREG(s->selected_entry->mode)) {
3288 struct tog_view *blame_view;
3289 int begin_x = view_is_parent_view(view) ?
3290 view_split_begin_x(view->begin_x) : 0;
3292 err = blame_tree_entry(&blame_view, begin_x,
3293 s->selected_entry, &s->parents, s->commit_id,
3294 s->repo);
3295 if (err)
3296 break;
3297 if (view_is_parent_view(view)) {
3298 err = view_close_child(view);
3299 if (err)
3300 return err;
3301 err = view_set_child(view, blame_view);
3302 if (err) {
3303 view_close(blame_view);
3304 break;
3306 *focus_view = blame_view;
3307 view->child_focussed = 1;
3308 } else
3309 *new_view = blame_view;
3311 break;
3312 case KEY_RESIZE:
3313 if (s->selected > view->nlines)
3314 s->selected = s->ndisplayed - 1;
3315 break;
3316 default:
3317 break;
3320 return err;
3323 __dead static void
3324 usage_tree(void)
3326 endwin();
3327 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3328 getprogname());
3329 exit(1);
3332 static const struct got_error *
3333 cmd_tree(int argc, char *argv[])
3335 const struct got_error *error;
3336 struct got_repository *repo = NULL;
3337 char *repo_path = NULL;
3338 struct got_object_id *commit_id = NULL;
3339 char *commit_id_arg = NULL;
3340 struct got_commit_object *commit = NULL;
3341 struct got_tree_object *tree = NULL;
3342 int ch;
3343 struct tog_view *view;
3345 #ifndef PROFILE
3346 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3347 == -1)
3348 err(1, "pledge");
3349 #endif
3351 while ((ch = getopt(argc, argv, "c:")) != -1) {
3352 switch (ch) {
3353 case 'c':
3354 commit_id_arg = optarg;
3355 break;
3356 default:
3357 usage();
3358 /* NOTREACHED */
3362 argc -= optind;
3363 argv += optind;
3365 if (argc == 0) {
3366 repo_path = getcwd(NULL, 0);
3367 if (repo_path == NULL)
3368 return got_error_from_errno();
3369 } else if (argc == 1) {
3370 repo_path = realpath(argv[0], NULL);
3371 if (repo_path == NULL)
3372 return got_error_from_errno();
3373 } else
3374 usage_log();
3376 error = got_repo_open(&repo, repo_path);
3377 free(repo_path);
3378 if (error != NULL)
3379 return error;
3381 if (commit_id_arg == NULL) {
3382 error = get_head_commit_id(&commit_id, repo);
3383 if (error != NULL)
3384 goto done;
3385 } else {
3386 struct got_object *obj;
3387 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3388 if (error == NULL) {
3389 commit_id = got_object_id_dup(got_object_get_id(obj));
3390 if (commit_id == NULL)
3391 error = got_error_from_errno();
3394 if (error != NULL)
3395 goto done;
3397 error = got_object_open_as_commit(&commit, repo, commit_id);
3398 if (error != NULL)
3399 goto done;
3401 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3402 if (error != NULL)
3403 goto done;
3405 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3406 if (view == NULL) {
3407 error = got_error_from_errno();
3408 goto done;
3410 error = open_tree_view(view, tree, commit_id, repo);
3411 if (error)
3412 goto done;
3413 error = view_loop(view);
3414 done:
3415 free(commit_id);
3416 if (commit)
3417 got_object_commit_close(commit);
3418 if (tree)
3419 got_object_tree_close(tree);
3420 if (repo)
3421 got_repo_close(repo);
3422 return error;
3425 static void
3426 init_curses(void)
3428 initscr();
3429 cbreak();
3430 halfdelay(1); /* Do fast refresh while initial view is loading. */
3431 noecho();
3432 nonl();
3433 intrflush(stdscr, FALSE);
3434 keypad(stdscr, TRUE);
3435 curs_set(0);
3436 signal(SIGWINCH, tog_sigwinch);
3439 __dead static void
3440 usage(void)
3442 int i;
3444 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3445 "Available commands:\n", getprogname());
3446 for (i = 0; i < nitems(tog_commands); i++) {
3447 struct tog_cmd *cmd = &tog_commands[i];
3448 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3450 exit(1);
3453 static char **
3454 make_argv(const char *arg0, const char *arg1)
3456 char **argv;
3457 int argc = (arg1 == NULL ? 1 : 2);
3459 argv = calloc(argc, sizeof(char *));
3460 if (argv == NULL)
3461 err(1, "calloc");
3462 argv[0] = strdup(arg0);
3463 if (argv[0] == NULL)
3464 err(1, "calloc");
3465 if (arg1) {
3466 argv[1] = strdup(arg1);
3467 if (argv[1] == NULL)
3468 err(1, "calloc");
3471 return argv;
3474 int
3475 main(int argc, char *argv[])
3477 const struct got_error *error = NULL;
3478 struct tog_cmd *cmd = NULL;
3479 int ch, hflag = 0;
3480 char **cmd_argv = NULL;
3482 setlocale(LC_ALL, "");
3484 while ((ch = getopt(argc, argv, "h")) != -1) {
3485 switch (ch) {
3486 case 'h':
3487 hflag = 1;
3488 break;
3489 default:
3490 usage();
3491 /* NOTREACHED */
3495 argc -= optind;
3496 argv += optind;
3497 optind = 0;
3498 optreset = 1;
3500 if (argc == 0) {
3501 if (hflag)
3502 usage();
3503 /* Build an argument vector which runs a default command. */
3504 cmd = &tog_commands[0];
3505 cmd_argv = make_argv(cmd->name, NULL);
3506 argc = 1;
3507 } else {
3508 int i;
3510 /* Did the user specific a command? */
3511 for (i = 0; i < nitems(tog_commands); i++) {
3512 if (strncmp(tog_commands[i].name, argv[0],
3513 strlen(argv[0])) == 0) {
3514 cmd = &tog_commands[i];
3515 if (hflag)
3516 tog_commands[i].cmd_usage();
3517 break;
3520 if (cmd == NULL) {
3521 /* Did the user specify a repository? */
3522 char *repo_path = realpath(argv[0], NULL);
3523 if (repo_path) {
3524 struct got_repository *repo;
3525 error = got_repo_open(&repo, repo_path);
3526 if (error == NULL)
3527 got_repo_close(repo);
3528 } else
3529 error = got_error_from_errno();
3530 if (error) {
3531 if (hflag) {
3532 fprintf(stderr, "%s: '%s' is not a "
3533 "known command\n", getprogname(),
3534 argv[0]);
3535 usage();
3537 fprintf(stderr, "%s: '%s' is neither a known "
3538 "command nor a path to a repository\n",
3539 getprogname(), argv[0]);
3540 free(repo_path);
3541 return 1;
3543 cmd = &tog_commands[0];
3544 cmd_argv = make_argv(cmd->name, repo_path);
3545 argc = 2;
3546 free(repo_path);
3550 init_curses();
3552 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3553 if (error)
3554 goto done;
3555 done:
3556 endwin();
3557 free(cmd_argv);
3558 if (error)
3559 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3560 return 0;