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;
877 struct tm tm;
879 if (localtime_r(&commit->committer_time, &tm) == NULL)
880 return got_error_from_errno();
881 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
882 >= sizeof(datebuf))
883 return got_error(GOT_ERR_NO_SPACE);
885 if (avail < date_display_cols)
886 limit = MIN(sizeof(datebuf) - 1, avail);
887 else
888 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
889 waddnstr(view->window, datebuf, limit);
890 col = limit + 1;
891 if (col > avail)
892 goto done;
894 author0 = strdup(commit->author);
895 if (author0 == NULL) {
896 err = got_error_from_errno();
897 goto done;
899 author = author0;
900 smallerthan = strchr(author, '<');
901 if (smallerthan)
902 *smallerthan = '\0';
903 else {
904 char *at = strchr(author, '@');
905 if (at)
906 *at = '\0';
908 limit = avail - col;
909 err = format_line(&wauthor, &author_width, author, limit);
910 if (err)
911 goto done;
912 waddwstr(view->window, wauthor);
913 col += author_width;
914 while (col <= avail && author_width < author_display_cols + 1) {
915 waddch(view->window, ' ');
916 col++;
917 author_width++;
919 if (col > avail)
920 goto done;
922 logmsg0 = strdup(commit->logmsg);
923 if (logmsg0 == NULL) {
924 err = got_error_from_errno();
925 goto done;
927 logmsg = logmsg0;
928 while (*logmsg == '\n')
929 logmsg++;
930 newline = strchr(logmsg, '\n');
931 if (newline)
932 *newline = '\0';
933 limit = avail - col;
934 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
935 if (err)
936 goto done;
937 waddwstr(view->window, wlogmsg);
938 col += logmsg_width;
939 while (col <= avail) {
940 waddch(view->window, ' ');
941 col++;
943 done:
944 free(logmsg0);
945 free(wlogmsg);
946 free(author0);
947 free(wauthor);
948 free(line);
949 return err;
952 static struct commit_queue_entry *
953 alloc_commit_queue_entry(struct got_commit_object *commit,
954 struct got_object_id *id)
956 struct commit_queue_entry *entry;
958 entry = calloc(1, sizeof(*entry));
959 if (entry == NULL)
960 return NULL;
962 entry->id = id;
963 entry->commit = commit;
964 return entry;
967 static void
968 pop_commit(struct commit_queue *commits)
970 struct commit_queue_entry *entry;
972 entry = TAILQ_FIRST(&commits->head);
973 TAILQ_REMOVE(&commits->head, entry, entry);
974 got_object_commit_close(entry->commit);
975 commits->ncommits--;
976 /* Don't free entry->id! It is owned by the commit graph. */
977 free(entry);
980 static void
981 free_commits(struct commit_queue *commits)
983 while (!TAILQ_EMPTY(&commits->head))
984 pop_commit(commits);
987 static const struct got_error *
988 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
989 int minqueue, struct got_repository *repo, const char *path)
991 const struct got_error *err = NULL;
992 int nqueued = 0;
994 /*
995 * We keep all commits open throughout the lifetime of the log
996 * view in order to avoid having to re-fetch commits from disk
997 * while updating the display.
998 */
999 while (nqueued < minqueue) {
1000 struct got_object_id *id;
1001 struct got_commit_object *commit;
1002 struct commit_queue_entry *entry;
1003 int errcode;
1005 err = got_commit_graph_iter_next(&id, graph);
1006 if (err) {
1007 if (err->code != GOT_ERR_ITER_NEED_MORE)
1008 break;
1009 err = got_commit_graph_fetch_commits(graph,
1010 minqueue, repo);
1011 if (err)
1012 return err;
1013 continue;
1016 if (id == NULL)
1017 break;
1019 err = got_object_open_as_commit(&commit, repo, id);
1020 if (err)
1021 break;
1022 entry = alloc_commit_queue_entry(commit, id);
1023 if (entry == NULL) {
1024 err = got_error_from_errno();
1025 break;
1028 errcode = pthread_mutex_lock(&tog_mutex);
1029 if (errcode) {
1030 err = got_error_set_errno(errcode);
1031 break;
1034 entry->idx = commits->ncommits;
1035 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1036 nqueued++;
1037 commits->ncommits++;
1039 errcode = pthread_mutex_unlock(&tog_mutex);
1040 if (errcode && err == NULL)
1041 err = got_error_set_errno(errcode);
1044 return err;
1047 static const struct got_error *
1048 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1050 const struct got_error *err = NULL;
1051 struct got_reference *head_ref;
1053 *head_id = NULL;
1055 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1056 if (err)
1057 return err;
1059 err = got_ref_resolve(head_id, repo, head_ref);
1060 got_ref_close(head_ref);
1061 if (err) {
1062 *head_id = NULL;
1063 return err;
1066 return NULL;
1069 static const struct got_error *
1070 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1071 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1072 struct commit_queue *commits, int selected_idx, int limit,
1073 const char *path, int commits_needed)
1075 const struct got_error *err = NULL;
1076 struct commit_queue_entry *entry;
1077 int ncommits, width;
1078 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1079 wchar_t *wline;
1081 entry = first;
1082 ncommits = 0;
1083 while (entry) {
1084 if (ncommits == selected_idx) {
1085 *selected = entry;
1086 break;
1088 entry = TAILQ_NEXT(entry, entry);
1089 ncommits++;
1092 if (*selected) {
1093 err = got_object_id_str(&id_str, (*selected)->id);
1094 if (err)
1095 return err;
1098 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1099 entry ? entry->idx + 1 : 0, commits->ncommits,
1100 commits_needed == 0 ? "" : " loading...") == -1)
1101 return got_error_from_errno();
1103 if (path && strcmp(path, "/") != 0) {
1104 if (asprintf(&header, "commit: %s %s%s",
1105 id_str ? id_str : "........................................",
1106 path, ncommits_str) == -1) {
1107 err = got_error_from_errno();
1108 header = NULL;
1109 goto done;
1111 } else if (asprintf(&header, "commit: %s%s",
1112 id_str ? id_str : "........................................",
1113 ncommits_str) == -1) {
1114 err = got_error_from_errno();
1115 header = NULL;
1116 goto done;
1118 err = format_line(&wline, &width, header, view->ncols);
1119 if (err)
1120 goto done;
1122 werase(view->window);
1124 if (view_needs_focus_indication(view))
1125 wstandout(view->window);
1126 waddwstr(view->window, wline);
1127 while (width < view->ncols) {
1128 waddch(view->window, ' ');
1129 width++;
1131 if (view_needs_focus_indication(view))
1132 wstandend(view->window);
1133 free(wline);
1134 if (limit <= 1)
1135 goto done;
1137 entry = first;
1138 *last = first;
1139 ncommits = 0;
1140 while (entry) {
1141 if (ncommits >= limit - 1)
1142 break;
1143 if (view->focussed && ncommits == selected_idx)
1144 wstandout(view->window);
1145 err = draw_commit(view, entry->commit, entry->id);
1146 if (view->focussed && ncommits == selected_idx)
1147 wstandend(view->window);
1148 if (err)
1149 break;
1150 ncommits++;
1151 *last = entry;
1152 entry = TAILQ_NEXT(entry, entry);
1155 view_vborder(view);
1156 done:
1157 free(id_str);
1158 free(ncommits_str);
1159 free(header);
1160 return err;
1163 static void
1164 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1165 struct commit_queue *commits)
1167 struct commit_queue_entry *entry;
1168 int nscrolled = 0;
1170 entry = TAILQ_FIRST(&commits->head);
1171 if (*first_displayed_entry == entry)
1172 return;
1174 entry = *first_displayed_entry;
1175 while (entry && nscrolled < maxscroll) {
1176 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1177 if (entry) {
1178 *first_displayed_entry = entry;
1179 nscrolled++;
1184 static const struct got_error *
1185 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1186 struct commit_queue_entry **last_displayed_entry,
1187 struct commit_queue *commits, int *log_complete, int *commits_needed,
1188 pthread_cond_t *need_commits)
1190 const struct got_error *err = NULL;
1191 struct commit_queue_entry *pentry;
1192 int nscrolled = 0;
1194 if (*last_displayed_entry == NULL)
1195 return NULL;
1197 do {
1198 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1199 if (pentry == NULL) {
1200 int errcode;
1201 if (*log_complete)
1202 return NULL;
1203 *commits_needed = maxscroll + 20;
1204 errcode = pthread_cond_signal(need_commits);
1205 if (errcode)
1206 return got_error_set_errno(errcode);
1207 return NULL;
1209 *last_displayed_entry = pentry;
1211 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1212 if (pentry == NULL)
1213 break;
1214 *first_displayed_entry = pentry;
1215 } while (++nscrolled < maxscroll);
1217 return err;
1220 static const struct got_error *
1221 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1222 struct got_object_id *commit_id, struct got_commit_object *commit,
1223 struct got_repository *repo)
1225 const struct got_error *err;
1226 struct got_object *obj1 = NULL, *obj2 = NULL;
1227 struct got_object_qid *parent_id;
1228 struct tog_view *diff_view;
1230 err = got_object_open(&obj2, repo, commit_id);
1231 if (err)
1232 return err;
1234 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1235 if (parent_id) {
1236 err = got_object_open(&obj1, repo, parent_id->id);
1237 if (err)
1238 goto done;
1241 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1242 if (diff_view == NULL) {
1243 err = got_error_from_errno();
1244 goto done;
1247 err = open_diff_view(diff_view, obj1, obj2, repo);
1248 if (err == NULL)
1249 *new_view = diff_view;
1250 done:
1251 if (obj1)
1252 got_object_close(obj1);
1253 if (obj2)
1254 got_object_close(obj2);
1255 return err;
1258 static const struct got_error *
1259 browse_commit(struct tog_view **new_view, int begin_x,
1260 struct commit_queue_entry *entry, struct got_repository *repo)
1262 const struct got_error *err = NULL;
1263 struct got_tree_object *tree;
1264 struct tog_view *tree_view;
1266 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1267 if (err)
1268 return err;
1270 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1271 if (tree_view == NULL)
1272 return got_error_from_errno();
1274 err = open_tree_view(tree_view, tree, entry->id, repo);
1275 if (err)
1276 got_object_tree_close(tree);
1277 else
1278 *new_view = tree_view;
1279 return err;
1282 static void *
1283 log_thread(void *arg)
1285 const struct got_error *err = NULL;
1286 int errcode = 0;
1287 struct tog_log_thread_args *a = arg;
1288 int done = 0;
1290 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1291 if (err)
1292 return (void *)err;
1294 while (!done && !err) {
1295 err = queue_commits(a->graph, a->commits, 1, a->repo,
1296 a->in_repo_path);
1297 if (err) {
1298 if (err->code != GOT_ERR_ITER_COMPLETED)
1299 return (void *)err;
1300 err = NULL;
1301 done = 1;
1302 } else if (a->commits_needed > 0)
1303 a->commits_needed--;
1305 errcode = pthread_mutex_lock(&tog_mutex);
1306 if (errcode)
1307 return (void *)got_error_set_errno(errcode);
1309 if (done)
1310 a->log_complete = 1;
1311 else if (*a->quit) {
1312 done = 1;
1313 a->log_complete = 1;
1314 } else if (*a->first_displayed_entry == NULL) {
1315 *a->first_displayed_entry =
1316 TAILQ_FIRST(&a->commits->head);
1317 *a->selected_entry = *a->first_displayed_entry;
1320 if (done)
1321 a->commits_needed = 0;
1322 else if (a->commits_needed == 0) {
1323 errcode = pthread_cond_wait(&a->need_commits,
1324 &tog_mutex);
1325 if (errcode)
1326 err = got_error_set_errno(errcode);
1329 errcode = pthread_mutex_unlock(&tog_mutex);
1330 if (errcode && err == NULL)
1331 err = got_error_set_errno(errcode);
1333 return (void *)err;
1336 static const struct got_error *
1337 stop_log_thread(struct tog_log_view_state *s)
1339 const struct got_error *err = NULL;
1340 int errcode;
1342 if (s->thread) {
1343 s->quit = 1;
1344 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1345 if (errcode)
1346 return got_error_set_errno(errcode);
1347 errcode = pthread_mutex_unlock(&tog_mutex);
1348 if (errcode)
1349 return got_error_set_errno(errcode);
1350 errcode = pthread_join(s->thread, (void **)&err);
1351 if (errcode)
1352 return got_error_set_errno(errcode);
1353 errcode = pthread_mutex_lock(&tog_mutex);
1354 if (errcode)
1355 return got_error_set_errno(errcode);
1356 s->thread = NULL;
1359 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1360 if (errcode && err == NULL)
1361 err = got_error_set_errno(errcode);
1363 if (s->thread_args.repo) {
1364 got_repo_close(s->thread_args.repo);
1365 s->thread_args.repo = NULL;
1368 if (s->thread_args.graph) {
1369 got_commit_graph_close(s->thread_args.graph);
1370 s->thread_args.graph = NULL;
1373 return err;
1376 static const struct got_error *
1377 close_log_view(struct tog_view *view)
1379 const struct got_error *err = NULL;
1380 struct tog_log_view_state *s = &view->state.log;
1382 err = stop_log_thread(s);
1383 free_commits(&s->commits);
1384 free(s->in_repo_path);
1385 s->in_repo_path = NULL;
1386 free(s->start_id);
1387 s->start_id = NULL;
1388 return err;
1391 static const struct got_error *
1392 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1393 struct got_repository *repo, const char *path, int check_disk)
1395 const struct got_error *err = NULL;
1396 struct tog_log_view_state *s = &view->state.log;
1397 struct got_repository *thread_repo = NULL;
1398 struct got_commit_graph *thread_graph = NULL;
1399 int errcode;
1401 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1402 if (err != NULL)
1403 goto done;
1405 /* The commit queue only contains commits being displayed. */
1406 TAILQ_INIT(&s->commits.head);
1407 s->commits.ncommits = 0;
1409 s->repo = repo;
1410 s->start_id = got_object_id_dup(start_id);
1411 if (s->start_id == NULL) {
1412 err = got_error_from_errno();
1413 goto done;
1416 view->show = show_log_view;
1417 view->input = input_log_view;
1418 view->close = close_log_view;
1420 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1421 if (err)
1422 goto done;
1423 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1424 0, thread_repo);
1425 if (err)
1426 goto done;
1428 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1429 if (errcode) {
1430 err = got_error_set_errno(errcode);
1431 goto done;
1434 s->thread_args.commits_needed = view->nlines;
1435 s->thread_args.graph = thread_graph;
1436 s->thread_args.commits = &s->commits;
1437 s->thread_args.in_repo_path = s->in_repo_path;
1438 s->thread_args.start_id = s->start_id;
1439 s->thread_args.repo = thread_repo;
1440 s->thread_args.log_complete = 0;
1441 s->thread_args.quit = &s->quit;
1442 s->thread_args.view = view;
1443 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1444 s->thread_args.selected_entry = &s->selected_entry;
1445 done:
1446 if (err)
1447 close_log_view(view);
1448 return err;
1451 static const struct got_error *
1452 show_log_view(struct tog_view *view)
1454 struct tog_log_view_state *s = &view->state.log;
1456 if (s->thread == NULL) {
1457 int errcode = pthread_create(&s->thread, NULL, log_thread,
1458 &s->thread_args);
1459 if (errcode)
1460 return got_error_set_errno(errcode);
1463 return draw_commits(view, &s->last_displayed_entry,
1464 &s->selected_entry, s->first_displayed_entry,
1465 &s->commits, s->selected, view->nlines,
1466 s->in_repo_path, s->thread_args.commits_needed);
1469 static const struct got_error *
1470 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1471 struct tog_view **focus_view, struct tog_view *view, int ch)
1473 const struct got_error *err = NULL;
1474 struct tog_log_view_state *s = &view->state.log;
1475 char *parent_path;
1476 struct tog_view *diff_view = NULL, *tree_view = NULL;
1477 int begin_x = 0;
1479 switch (ch) {
1480 case 'q':
1481 s->quit = 1;
1482 break;
1483 case 'k':
1484 case KEY_UP:
1485 if (s->first_displayed_entry == NULL)
1486 break;
1487 if (s->selected > 0)
1488 s->selected--;
1489 if (s->selected > 0)
1490 break;
1491 scroll_up(&s->first_displayed_entry, 1,
1492 &s->commits);
1493 break;
1494 case KEY_PPAGE:
1495 if (s->first_displayed_entry == NULL)
1496 break;
1497 if (TAILQ_FIRST(&s->commits.head) ==
1498 s->first_displayed_entry) {
1499 s->selected = 0;
1500 break;
1502 scroll_up(&s->first_displayed_entry,
1503 view->nlines, &s->commits);
1504 break;
1505 case 'j':
1506 case KEY_DOWN:
1507 if (s->first_displayed_entry == NULL)
1508 break;
1509 if (s->selected < MIN(view->nlines - 2,
1510 s->commits.ncommits - 1)) {
1511 s->selected++;
1512 break;
1514 err = scroll_down(&s->first_displayed_entry, 1,
1515 &s->last_displayed_entry, &s->commits,
1516 &s->thread_args.log_complete,
1517 &s->thread_args.commits_needed,
1518 &s->thread_args.need_commits);
1519 break;
1520 case KEY_NPAGE: {
1521 struct commit_queue_entry *first;
1522 first = s->first_displayed_entry;
1523 if (first == NULL)
1524 break;
1525 err = scroll_down(&s->first_displayed_entry,
1526 view->nlines, &s->last_displayed_entry,
1527 &s->commits, &s->thread_args.log_complete,
1528 &s->thread_args.commits_needed,
1529 &s->thread_args.need_commits);
1530 if (first == s->first_displayed_entry &&
1531 s->selected < MIN(view->nlines - 2,
1532 s->commits.ncommits - 1)) {
1533 /* can't scroll further down */
1534 s->selected = MIN(view->nlines - 2,
1535 s->commits.ncommits - 1);
1537 err = NULL;
1538 break;
1540 case KEY_RESIZE:
1541 if (s->selected > view->nlines - 2)
1542 s->selected = view->nlines - 2;
1543 if (s->selected > s->commits.ncommits - 1)
1544 s->selected = s->commits.ncommits - 1;
1545 break;
1546 case KEY_ENTER:
1547 case '\r':
1548 if (s->selected_entry == NULL)
1549 break;
1550 if (view_is_parent_view(view))
1551 begin_x = view_split_begin_x(view->begin_x);
1552 err = open_diff_view_for_commit(&diff_view, begin_x,
1553 s->selected_entry->id, s->selected_entry->commit,
1554 s->repo);
1555 if (err)
1556 break;
1557 if (view_is_parent_view(view)) {
1558 err = view_close_child(view);
1559 if (err)
1560 return err;
1561 err = view_set_child(view, diff_view);
1562 if (err) {
1563 view_close(diff_view);
1564 break;
1566 *focus_view = diff_view;
1567 view->child_focussed = 1;
1568 } else
1569 *new_view = diff_view;
1570 break;
1571 case 't':
1572 if (s->selected_entry == NULL)
1573 break;
1574 if (view_is_parent_view(view))
1575 begin_x = view_split_begin_x(view->begin_x);
1576 err = browse_commit(&tree_view, begin_x,
1577 s->selected_entry, s->repo);
1578 if (err)
1579 break;
1580 if (view_is_parent_view(view)) {
1581 err = view_close_child(view);
1582 if (err)
1583 return err;
1584 err = view_set_child(view, tree_view);
1585 if (err) {
1586 view_close(tree_view);
1587 break;
1589 *focus_view = tree_view;
1590 view->child_focussed = 1;
1591 } else
1592 *new_view = tree_view;
1593 break;
1594 case KEY_BACKSPACE:
1595 if (strcmp(s->in_repo_path, "/") == 0)
1596 break;
1597 parent_path = dirname(s->in_repo_path);
1598 if (parent_path && strcmp(parent_path, ".") != 0) {
1599 struct tog_view *lv;
1600 err = stop_log_thread(s);
1601 if (err)
1602 return err;
1603 lv = view_open(view->nlines, view->ncols,
1604 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1605 if (lv == NULL)
1606 return got_error_from_errno();
1607 err = open_log_view(lv, s->start_id, s->repo,
1608 parent_path, 0);
1609 if (err)
1610 return err;;
1611 if (view_is_parent_view(view))
1612 *new_view = lv;
1613 else {
1614 view_set_child(view->parent, lv);
1615 *focus_view = lv;
1617 return NULL;
1619 break;
1620 default:
1621 break;
1624 return err;
1627 static const struct got_error *
1628 cmd_log(int argc, char *argv[])
1630 const struct got_error *error;
1631 struct got_repository *repo = NULL;
1632 struct got_object_id *start_id = NULL;
1633 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1634 char *start_commit = NULL;
1635 int ch;
1636 struct tog_view *view;
1638 #ifndef PROFILE
1639 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1640 == -1)
1641 err(1, "pledge");
1642 #endif
1644 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1645 switch (ch) {
1646 case 'c':
1647 start_commit = optarg;
1648 break;
1649 case 'r':
1650 repo_path = realpath(optarg, NULL);
1651 if (repo_path == NULL)
1652 err(1, "-r option");
1653 break;
1654 default:
1655 usage();
1656 /* NOTREACHED */
1660 argc -= optind;
1661 argv += optind;
1663 if (argc == 0)
1664 path = strdup("");
1665 else if (argc == 1)
1666 path = strdup(argv[0]);
1667 else
1668 usage_log();
1669 if (path == NULL)
1670 return got_error_from_errno();
1672 cwd = getcwd(NULL, 0);
1673 if (cwd == NULL) {
1674 error = got_error_from_errno();
1675 goto done;
1677 if (repo_path == NULL) {
1678 repo_path = strdup(cwd);
1679 if (repo_path == NULL) {
1680 error = got_error_from_errno();
1681 goto done;
1685 error = got_repo_open(&repo, repo_path);
1686 if (error != NULL)
1687 goto done;
1689 if (start_commit == NULL) {
1690 error = get_head_commit_id(&start_id, repo);
1691 if (error != NULL)
1692 goto done;
1693 } else {
1694 struct got_object *obj;
1695 error = got_object_open_by_id_str(&obj, repo, start_commit);
1696 if (error == NULL) {
1697 start_id = got_object_id_dup(got_object_get_id(obj));
1698 if (start_id == NULL)
1699 error = got_error_from_errno();
1700 goto done;
1703 if (error != NULL)
1704 goto done;
1706 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1707 if (view == NULL) {
1708 error = got_error_from_errno();
1709 goto done;
1711 error = open_log_view(view, start_id, repo, path, 1);
1712 if (error)
1713 goto done;
1714 error = view_loop(view);
1715 done:
1716 free(repo_path);
1717 free(cwd);
1718 free(path);
1719 free(start_id);
1720 if (repo)
1721 got_repo_close(repo);
1722 return error;
1725 __dead static void
1726 usage_diff(void)
1728 endwin();
1729 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1730 getprogname());
1731 exit(1);
1734 static char *
1735 parse_next_line(FILE *f, size_t *len)
1737 char *line;
1738 size_t linelen;
1739 size_t lineno;
1740 const char delim[3] = { '\0', '\0', '\0'};
1742 line = fparseln(f, &linelen, &lineno, delim, 0);
1743 if (len)
1744 *len = linelen;
1745 return line;
1748 static const struct got_error *
1749 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1750 int *last_displayed_line, int *eof, int max_lines,
1751 char * header)
1753 const struct got_error *err;
1754 int nlines = 0, nprinted = 0;
1755 char *line;
1756 size_t len;
1757 wchar_t *wline;
1758 int width;
1760 rewind(f);
1761 werase(view->window);
1763 if (header) {
1764 err = format_line(&wline, &width, header, view->ncols);
1765 if (err) {
1766 return err;
1769 if (view_needs_focus_indication(view))
1770 wstandout(view->window);
1771 waddwstr(view->window, wline);
1772 if (view_needs_focus_indication(view))
1773 wstandend(view->window);
1774 if (width < view->ncols)
1775 waddch(view->window, '\n');
1777 if (max_lines <= 1)
1778 return NULL;
1779 max_lines--;
1782 *eof = 0;
1783 while (nprinted < max_lines) {
1784 line = parse_next_line(f, &len);
1785 if (line == NULL) {
1786 *eof = 1;
1787 break;
1789 if (++nlines < *first_displayed_line) {
1790 free(line);
1791 continue;
1794 err = format_line(&wline, &width, line, view->ncols);
1795 if (err) {
1796 free(line);
1797 return err;
1799 waddwstr(view->window, wline);
1800 if (width < view->ncols)
1801 waddch(view->window, '\n');
1802 if (++nprinted == 1)
1803 *first_displayed_line = nlines;
1804 free(line);
1805 free(wline);
1806 wline = NULL;
1808 *last_displayed_line = nlines;
1810 view_vborder(view);
1812 return NULL;
1815 static const struct got_error *
1816 create_diff(struct tog_diff_view_state *s)
1818 const struct got_error *err = NULL;
1819 struct got_object *obj1 = NULL, *obj2 = NULL;
1820 FILE *f = NULL;
1822 if (s->id1) {
1823 err = got_object_open(&obj1, s->repo, s->id1);
1824 if (err)
1825 return err;
1828 err = got_object_open(&obj2, s->repo, s->id2);
1829 if (err)
1830 goto done;
1832 f = got_opentemp();
1833 if (f == NULL) {
1834 err = got_error_from_errno();
1835 goto done;
1837 if (s->f)
1838 fclose(s->f);
1839 s->f = f;
1841 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1842 case GOT_OBJ_TYPE_BLOB:
1843 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1844 s->diff_context, s->repo, f);
1845 break;
1846 case GOT_OBJ_TYPE_TREE:
1847 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1848 s->diff_context, s->repo, f);
1849 break;
1850 case GOT_OBJ_TYPE_COMMIT:
1851 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1852 s->repo, f);
1853 break;
1854 default:
1855 err = got_error(GOT_ERR_OBJ_TYPE);
1856 break;
1858 done:
1859 if (obj1)
1860 got_object_close(obj1);
1861 got_object_close(obj2);
1862 if (f)
1863 fflush(f);
1864 return err;
1867 static const struct got_error *
1868 open_diff_view(struct tog_view *view, struct got_object *obj1,
1869 struct got_object *obj2, struct got_repository *repo)
1871 const struct got_error *err;
1873 if (obj1 != NULL && obj2 != NULL &&
1874 got_object_get_type(obj1) != got_object_get_type(obj2))
1875 return got_error(GOT_ERR_OBJ_TYPE);
1877 if (obj1) {
1878 struct got_object_id *id1;
1879 id1 = got_object_id_dup(got_object_get_id(obj1));
1880 if (id1 == NULL)
1881 return got_error_from_errno();
1882 view->state.diff.id1 = id1;
1883 } else
1884 view->state.diff.id1 = NULL;
1886 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1887 if (view->state.diff.id2 == NULL) {
1888 free(view->state.diff.id1);
1889 view->state.diff.id1 = NULL;
1890 return got_error_from_errno();
1892 view->state.diff.f = NULL;
1893 view->state.diff.first_displayed_line = 1;
1894 view->state.diff.last_displayed_line = view->nlines;
1895 view->state.diff.diff_context = 3;
1896 view->state.diff.repo = repo;
1898 err = create_diff(&view->state.diff);
1899 if (err) {
1900 free(view->state.diff.id1);
1901 view->state.diff.id1 = NULL;
1902 free(view->state.diff.id2);
1903 view->state.diff.id2 = NULL;
1904 return err;
1907 view->show = show_diff_view;
1908 view->input = input_diff_view;
1909 view->close = close_diff_view;
1911 return NULL;
1914 static const struct got_error *
1915 close_diff_view(struct tog_view *view)
1917 const struct got_error *err = NULL;
1919 free(view->state.diff.id1);
1920 view->state.diff.id1 = NULL;
1921 free(view->state.diff.id2);
1922 view->state.diff.id2 = NULL;
1923 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1924 err = got_error_from_errno();
1925 return err;
1928 static const struct got_error *
1929 show_diff_view(struct tog_view *view)
1931 const struct got_error *err;
1932 struct tog_diff_view_state *s = &view->state.diff;
1933 char *id_str1 = NULL, *id_str2, *header;
1935 if (s->id1) {
1936 err = got_object_id_str(&id_str1, s->id1);
1937 if (err)
1938 return err;
1940 err = got_object_id_str(&id_str2, s->id2);
1941 if (err)
1942 return err;
1944 if (asprintf(&header, "diff: %s %s",
1945 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1946 err = got_error_from_errno();
1947 free(id_str1);
1948 free(id_str2);
1949 return err;
1951 free(id_str1);
1952 free(id_str2);
1954 return draw_file(view, s->f, &s->first_displayed_line,
1955 &s->last_displayed_line, &s->eof, view->nlines,
1956 header);
1959 static const struct got_error *
1960 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1961 struct tog_view **focus_view, struct tog_view *view, int ch)
1963 const struct got_error *err = NULL;
1964 struct tog_diff_view_state *s = &view->state.diff;
1965 int i;
1967 switch (ch) {
1968 case 'k':
1969 case KEY_UP:
1970 if (s->first_displayed_line > 1)
1971 s->first_displayed_line--;
1972 break;
1973 case KEY_PPAGE:
1974 i = 0;
1975 while (i++ < view->nlines - 1 &&
1976 s->first_displayed_line > 1)
1977 s->first_displayed_line--;
1978 break;
1979 case 'j':
1980 case KEY_DOWN:
1981 if (!s->eof)
1982 s->first_displayed_line++;
1983 break;
1984 case KEY_NPAGE:
1985 case ' ':
1986 i = 0;
1987 while (!s->eof && i++ < view->nlines - 1) {
1988 char *line;
1989 line = parse_next_line(s->f, NULL);
1990 s->first_displayed_line++;
1991 if (line == NULL)
1992 break;
1994 break;
1995 case '[':
1996 if (s->diff_context > 0) {
1997 s->diff_context--;
1998 err = create_diff(s);
2000 break;
2001 case ']':
2002 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2003 s->diff_context++;
2004 err = create_diff(s);
2006 break;
2007 default:
2008 break;
2011 return err;
2014 static const struct got_error *
2015 cmd_diff(int argc, char *argv[])
2017 const struct got_error *error = NULL;
2018 struct got_repository *repo = NULL;
2019 struct got_object *obj1 = NULL, *obj2 = NULL;
2020 char *repo_path = NULL;
2021 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
2022 int ch;
2023 struct tog_view *view;
2025 #ifndef PROFILE
2026 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2027 == -1)
2028 err(1, "pledge");
2029 #endif
2031 while ((ch = getopt(argc, argv, "")) != -1) {
2032 switch (ch) {
2033 default:
2034 usage();
2035 /* NOTREACHED */
2039 argc -= optind;
2040 argv += optind;
2042 if (argc == 0) {
2043 usage_diff(); /* TODO show local worktree changes */
2044 } else if (argc == 2) {
2045 repo_path = getcwd(NULL, 0);
2046 if (repo_path == NULL)
2047 return got_error_from_errno();
2048 obj_id_str1 = argv[0];
2049 obj_id_str2 = argv[1];
2050 } else if (argc == 3) {
2051 repo_path = realpath(argv[0], NULL);
2052 if (repo_path == NULL)
2053 return got_error_from_errno();
2054 obj_id_str1 = argv[1];
2055 obj_id_str2 = argv[2];
2056 } else
2057 usage_diff();
2059 error = got_repo_open(&repo, repo_path);
2060 free(repo_path);
2061 if (error)
2062 goto done;
2064 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2065 if (error)
2066 goto done;
2068 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2069 if (error)
2070 goto done;
2072 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2073 if (view == NULL) {
2074 error = got_error_from_errno();
2075 goto done;
2077 error = open_diff_view(view, obj1, obj2, repo);
2078 if (error)
2079 goto done;
2080 error = view_loop(view);
2081 done:
2082 got_repo_close(repo);
2083 if (obj1)
2084 got_object_close(obj1);
2085 if (obj2)
2086 got_object_close(obj2);
2087 return error;
2090 __dead static void
2091 usage_blame(void)
2093 endwin();
2094 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2095 getprogname());
2096 exit(1);
2099 struct tog_blame_line {
2100 int annotated;
2101 struct got_object_id *id;
2104 static const struct got_error *
2105 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2106 const char *path, struct tog_blame_line *lines, int nlines,
2107 int blame_complete, int selected_line, int *first_displayed_line,
2108 int *last_displayed_line, int *eof, int max_lines)
2110 const struct got_error *err;
2111 int lineno = 0, nprinted = 0;
2112 char *line;
2113 size_t len;
2114 wchar_t *wline;
2115 int width, wlimit;
2116 struct tog_blame_line *blame_line;
2117 struct got_object_id *prev_id = NULL;
2118 char *id_str;
2120 err = got_object_id_str(&id_str, id);
2121 if (err)
2122 return err;
2124 rewind(f);
2125 werase(view->window);
2127 if (asprintf(&line, "commit: %s", id_str) == -1) {
2128 err = got_error_from_errno();
2129 free(id_str);
2130 return err;
2133 err = format_line(&wline, &width, line, view->ncols);
2134 free(line);
2135 line = NULL;
2136 if (view_needs_focus_indication(view))
2137 wstandout(view->window);
2138 waddwstr(view->window, wline);
2139 if (view_needs_focus_indication(view))
2140 wstandend(view->window);
2141 free(wline);
2142 wline = NULL;
2143 if (width < view->ncols)
2144 waddch(view->window, '\n');
2146 if (asprintf(&line, "[%d/%d] %s%s",
2147 *first_displayed_line - 1 + selected_line, nlines,
2148 blame_complete ? "" : "annotating ", path) == -1) {
2149 free(id_str);
2150 return got_error_from_errno();
2152 free(id_str);
2153 err = format_line(&wline, &width, line, view->ncols);
2154 free(line);
2155 line = NULL;
2156 if (err)
2157 return err;
2158 waddwstr(view->window, wline);
2159 free(wline);
2160 wline = NULL;
2161 if (width < view->ncols)
2162 waddch(view->window, '\n');
2164 *eof = 0;
2165 while (nprinted < max_lines - 2) {
2166 line = parse_next_line(f, &len);
2167 if (line == NULL) {
2168 *eof = 1;
2169 break;
2171 if (++lineno < *first_displayed_line) {
2172 free(line);
2173 continue;
2176 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2177 err = format_line(&wline, &width, line, wlimit);
2178 if (err) {
2179 free(line);
2180 return err;
2183 if (view->focussed && nprinted == selected_line - 1)
2184 wstandout(view->window);
2186 blame_line = &lines[lineno - 1];
2187 if (blame_line->annotated && prev_id &&
2188 got_object_id_cmp(prev_id, blame_line->id) == 0)
2189 waddstr(view->window, " ");
2190 else if (blame_line->annotated) {
2191 char *id_str;
2192 err = got_object_id_str(&id_str, blame_line->id);
2193 if (err) {
2194 free(line);
2195 free(wline);
2196 return err;
2198 wprintw(view->window, "%.8s ", id_str);
2199 free(id_str);
2200 prev_id = blame_line->id;
2201 } else {
2202 waddstr(view->window, "........ ");
2203 prev_id = NULL;
2206 waddwstr(view->window, wline);
2207 while (width < wlimit) {
2208 waddch(view->window, ' ');
2209 width++;
2211 if (view->focussed && nprinted == selected_line - 1)
2212 wstandend(view->window);
2213 if (++nprinted == 1)
2214 *first_displayed_line = lineno;
2215 free(line);
2216 free(wline);
2217 wline = NULL;
2219 *last_displayed_line = lineno;
2221 view_vborder(view);
2223 return NULL;
2226 static const struct got_error *
2227 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2229 const struct got_error *err = NULL;
2230 struct tog_blame_cb_args *a = arg;
2231 struct tog_blame_line *line;
2232 int errcode;
2234 if (nlines != a->nlines ||
2235 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2236 return got_error(GOT_ERR_RANGE);
2238 errcode = pthread_mutex_lock(&tog_mutex);
2239 if (errcode)
2240 return got_error_set_errno(errcode);
2242 if (*a->quit) { /* user has quit the blame view */
2243 err = got_error(GOT_ERR_ITER_COMPLETED);
2244 goto done;
2247 if (lineno == -1)
2248 goto done; /* no change in this commit */
2250 line = &a->lines[lineno - 1];
2251 if (line->annotated)
2252 goto done;
2254 line->id = got_object_id_dup(id);
2255 if (line->id == NULL) {
2256 err = got_error_from_errno();
2257 goto done;
2259 line->annotated = 1;
2260 done:
2261 errcode = pthread_mutex_unlock(&tog_mutex);
2262 if (errcode)
2263 err = got_error_set_errno(errcode);
2264 return err;
2267 static void *
2268 blame_thread(void *arg)
2270 const struct got_error *err;
2271 struct tog_blame_thread_args *ta = arg;
2272 struct tog_blame_cb_args *a = ta->cb_args;
2273 int errcode;
2275 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2276 blame_cb, ta->cb_args);
2278 errcode = pthread_mutex_lock(&tog_mutex);
2279 if (errcode)
2280 return (void *)got_error_set_errno(errcode);
2282 got_repo_close(ta->repo);
2283 ta->repo = NULL;
2284 *ta->complete = 1;
2286 errcode = pthread_mutex_unlock(&tog_mutex);
2287 if (errcode && err == NULL)
2288 err = got_error_set_errno(errcode);
2290 return (void *)err;
2293 static struct got_object_id *
2294 get_selected_commit_id(struct tog_blame_line *lines,
2295 int first_displayed_line, int selected_line)
2297 struct tog_blame_line *line;
2299 line = &lines[first_displayed_line - 1 + selected_line - 1];
2300 if (!line->annotated)
2301 return NULL;
2303 return line->id;
2306 static const struct got_error *
2307 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2308 struct tog_blame_line *lines, int first_displayed_line,
2309 int selected_line, struct got_repository *repo)
2311 const struct got_error *err = NULL;
2312 struct got_commit_object *commit = NULL;
2313 struct got_object_id *selected_id;
2314 struct got_object_qid *pid;
2316 *pobj = NULL;
2317 *obj = NULL;
2319 selected_id = get_selected_commit_id(lines,
2320 first_displayed_line, selected_line);
2321 if (selected_id == NULL)
2322 return NULL;
2324 err = got_object_open(obj, repo, selected_id);
2325 if (err)
2326 goto done;
2328 err = got_object_commit_open(&commit, repo, *obj);
2329 if (err)
2330 goto done;
2332 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2333 if (pid) {
2334 err = got_object_open(pobj, repo, pid->id);
2335 if (err)
2336 goto done;
2338 done:
2339 if (commit)
2340 got_object_commit_close(commit);
2341 return err;
2344 static const struct got_error *
2345 stop_blame(struct tog_blame *blame)
2347 const struct got_error *err = NULL;
2348 int i;
2350 if (blame->thread) {
2351 int errcode;
2352 errcode = pthread_mutex_unlock(&tog_mutex);
2353 if (errcode)
2354 return got_error_set_errno(errcode);
2355 errcode = pthread_join(blame->thread, (void **)&err);
2356 if (errcode)
2357 return got_error_set_errno(errcode);
2358 errcode = pthread_mutex_lock(&tog_mutex);
2359 if (errcode)
2360 return got_error_set_errno(errcode);
2361 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2362 err = NULL;
2363 blame->thread = NULL;
2365 if (blame->thread_args.repo) {
2366 got_repo_close(blame->thread_args.repo);
2367 blame->thread_args.repo = NULL;
2369 if (blame->f) {
2370 fclose(blame->f);
2371 blame->f = NULL;
2373 for (i = 0; i < blame->nlines; i++)
2374 free(blame->lines[i].id);
2375 free(blame->lines);
2376 blame->lines = NULL;
2377 free(blame->cb_args.commit_id);
2378 blame->cb_args.commit_id = NULL;
2380 return err;
2383 static const struct got_error *
2384 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2385 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2386 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2387 struct got_repository *repo)
2389 const struct got_error *err = NULL;
2390 struct got_blob_object *blob = NULL;
2391 struct got_repository *thread_repo = NULL;
2392 struct got_object_id *obj_id = NULL;
2393 struct got_object *obj = NULL;
2395 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2396 if (err)
2397 goto done;
2399 err = got_object_open(&obj, repo, obj_id);
2400 if (err)
2401 goto done;
2403 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2404 err = got_error(GOT_ERR_OBJ_TYPE);
2405 goto done;
2408 err = got_object_blob_open(&blob, repo, obj, 8192);
2409 if (err)
2410 goto done;
2411 blame->f = got_opentemp();
2412 if (blame->f == NULL) {
2413 err = got_error_from_errno();
2414 goto done;
2416 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2417 blame->f, blob);
2418 if (err)
2419 goto done;
2421 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2422 if (blame->lines == NULL) {
2423 err = got_error_from_errno();
2424 goto done;
2427 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2428 if (err)
2429 goto done;
2431 blame->cb_args.view = view;
2432 blame->cb_args.lines = blame->lines;
2433 blame->cb_args.nlines = blame->nlines;
2434 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2435 if (blame->cb_args.commit_id == NULL) {
2436 err = got_error_from_errno();
2437 goto done;
2439 blame->cb_args.quit = done;
2441 blame->thread_args.path = path;
2442 blame->thread_args.repo = thread_repo;
2443 blame->thread_args.cb_args = &blame->cb_args;
2444 blame->thread_args.complete = blame_complete;
2445 *blame_complete = 0;
2447 done:
2448 if (blob)
2449 got_object_blob_close(blob);
2450 free(obj_id);
2451 if (obj)
2452 got_object_close(obj);
2453 if (err)
2454 stop_blame(blame);
2455 return err;
2458 static const struct got_error *
2459 open_blame_view(struct tog_view *view, char *path,
2460 struct got_object_id *commit_id, struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct tog_blame_view_state *s = &view->state.blame;
2465 SIMPLEQ_INIT(&s->blamed_commits);
2467 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2468 if (err)
2469 return err;
2471 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2472 s->first_displayed_line = 1;
2473 s->last_displayed_line = view->nlines;
2474 s->selected_line = 1;
2475 s->blame_complete = 0;
2476 s->path = path;
2477 if (s->path == NULL)
2478 return got_error_from_errno();
2479 s->repo = repo;
2480 s->commit_id = commit_id;
2481 memset(&s->blame, 0, sizeof(s->blame));
2483 view->show = show_blame_view;
2484 view->input = input_blame_view;
2485 view->close = close_blame_view;
2487 return run_blame(&s->blame, view, &s->blame_complete,
2488 &s->first_displayed_line, &s->last_displayed_line,
2489 &s->selected_line, &s->done, &s->eof, s->path,
2490 s->blamed_commit->id, s->repo);
2493 static const struct got_error *
2494 close_blame_view(struct tog_view *view)
2496 const struct got_error *err = NULL;
2497 struct tog_blame_view_state *s = &view->state.blame;
2499 if (s->blame.thread)
2500 err = stop_blame(&s->blame);
2502 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2503 struct got_object_qid *blamed_commit;
2504 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2505 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2506 got_object_qid_free(blamed_commit);
2509 free(s->path);
2511 return err;
2514 static const struct got_error *
2515 show_blame_view(struct tog_view *view)
2517 const struct got_error *err = NULL;
2518 struct tog_blame_view_state *s = &view->state.blame;
2519 int errcode;
2521 if (s->blame.thread == NULL) {
2522 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2523 &s->blame.thread_args);
2524 if (errcode)
2525 return got_error_set_errno(errcode);
2528 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2529 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2530 s->selected_line, &s->first_displayed_line,
2531 &s->last_displayed_line, &s->eof, view->nlines);
2533 view_vborder(view);
2534 return err;
2537 static const struct got_error *
2538 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2539 struct tog_view **focus_view, struct tog_view *view, int ch)
2541 const struct got_error *err = NULL, *thread_err = NULL;
2542 struct got_object *obj = NULL, *pobj = NULL;
2543 struct tog_view *diff_view;
2544 struct tog_blame_view_state *s = &view->state.blame;
2545 int begin_x = 0;
2547 switch (ch) {
2548 case 'q':
2549 s->done = 1;
2550 break;
2551 case 'k':
2552 case KEY_UP:
2553 if (s->selected_line > 1)
2554 s->selected_line--;
2555 else if (s->selected_line == 1 &&
2556 s->first_displayed_line > 1)
2557 s->first_displayed_line--;
2558 break;
2559 case KEY_PPAGE:
2560 if (s->first_displayed_line == 1) {
2561 s->selected_line = 1;
2562 break;
2564 if (s->first_displayed_line > view->nlines - 2)
2565 s->first_displayed_line -=
2566 (view->nlines - 2);
2567 else
2568 s->first_displayed_line = 1;
2569 break;
2570 case 'j':
2571 case KEY_DOWN:
2572 if (s->selected_line < view->nlines - 2 &&
2573 s->first_displayed_line +
2574 s->selected_line <= s->blame.nlines)
2575 s->selected_line++;
2576 else if (s->last_displayed_line <
2577 s->blame.nlines)
2578 s->first_displayed_line++;
2579 break;
2580 case 'b':
2581 case 'p': {
2582 struct got_object_id *id;
2583 id = get_selected_commit_id(s->blame.lines,
2584 s->first_displayed_line, s->selected_line);
2585 if (id == NULL || got_object_id_cmp(id,
2586 s->blamed_commit->id) == 0)
2587 break;
2588 err = open_selected_commit(&pobj, &obj,
2589 s->blame.lines, s->first_displayed_line,
2590 s->selected_line, s->repo);
2591 if (err)
2592 break;
2593 if (pobj == NULL && obj == NULL)
2594 break;
2595 if (ch == 'p' && pobj == NULL)
2596 break;
2597 s->done = 1;
2598 thread_err = stop_blame(&s->blame);
2599 s->done = 0;
2600 if (thread_err)
2601 break;
2602 id = got_object_get_id(ch == 'b' ? obj : pobj);
2603 got_object_close(obj);
2604 obj = NULL;
2605 if (pobj) {
2606 got_object_close(pobj);
2607 pobj = NULL;
2609 err = got_object_qid_alloc(&s->blamed_commit, id);
2610 if (err)
2611 goto done;
2612 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2613 s->blamed_commit, entry);
2614 err = run_blame(&s->blame, view, &s->blame_complete,
2615 &s->first_displayed_line, &s->last_displayed_line,
2616 &s->selected_line, &s->done, &s->eof,
2617 s->path, s->blamed_commit->id, s->repo);
2618 if (err)
2619 break;
2620 break;
2622 case 'B': {
2623 struct got_object_qid *first;
2624 first = SIMPLEQ_FIRST(&s->blamed_commits);
2625 if (!got_object_id_cmp(first->id, s->commit_id))
2626 break;
2627 s->done = 1;
2628 thread_err = stop_blame(&s->blame);
2629 s->done = 0;
2630 if (thread_err)
2631 break;
2632 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2633 got_object_qid_free(s->blamed_commit);
2634 s->blamed_commit =
2635 SIMPLEQ_FIRST(&s->blamed_commits);
2636 err = run_blame(&s->blame, view, &s->blame_complete,
2637 &s->first_displayed_line, &s->last_displayed_line,
2638 &s->selected_line, &s->done, &s->eof, s->path,
2639 s->blamed_commit->id, s->repo);
2640 if (err)
2641 break;
2642 break;
2644 case KEY_ENTER:
2645 case '\r':
2646 err = open_selected_commit(&pobj, &obj,
2647 s->blame.lines, s->first_displayed_line,
2648 s->selected_line, s->repo);
2649 if (err)
2650 break;
2651 if (pobj == NULL && obj == NULL)
2652 break;
2654 if (view_is_parent_view(view))
2655 begin_x = view_split_begin_x(view->begin_x);
2656 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2657 if (diff_view == NULL) {
2658 err = got_error_from_errno();
2659 break;
2661 err = open_diff_view(diff_view, pobj, obj, s->repo);
2662 if (err) {
2663 view_close(diff_view);
2664 break;
2666 if (view_is_parent_view(view)) {
2667 err = view_close_child(view);
2668 if (err)
2669 return err;
2670 err = view_set_child(view, diff_view);
2671 if (err) {
2672 view_close(diff_view);
2673 break;
2675 if (!view_is_splitscreen(diff_view)) {
2676 *focus_view = diff_view;
2677 view->child_focussed = 1;
2679 } else
2680 *new_view = diff_view;
2681 if (pobj) {
2682 got_object_close(pobj);
2683 pobj = NULL;
2685 got_object_close(obj);
2686 obj = NULL;
2687 if (err)
2688 break;
2689 break;
2690 case KEY_NPAGE:
2691 case ' ':
2692 if (s->last_displayed_line >= s->blame.nlines &&
2693 s->selected_line < view->nlines - 2) {
2694 s->selected_line = MIN(s->blame.nlines,
2695 view->nlines - 2);
2696 break;
2698 if (s->last_displayed_line + view->nlines - 2
2699 <= s->blame.nlines)
2700 s->first_displayed_line +=
2701 view->nlines - 2;
2702 else
2703 s->first_displayed_line =
2704 s->blame.nlines -
2705 (view->nlines - 3);
2706 break;
2707 case KEY_RESIZE:
2708 if (s->selected_line > view->nlines - 2) {
2709 s->selected_line = MIN(s->blame.nlines,
2710 view->nlines - 2);
2712 break;
2713 default:
2714 break;
2716 done:
2717 if (pobj)
2718 got_object_close(pobj);
2719 return thread_err ? thread_err : err;
2722 static const struct got_error *
2723 cmd_blame(int argc, char *argv[])
2725 const struct got_error *error;
2726 struct got_repository *repo = NULL;
2727 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2728 struct got_object_id *commit_id = NULL;
2729 char *commit_id_str = NULL;
2730 int ch;
2731 struct tog_view *view;
2733 #ifndef PROFILE
2734 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2735 == -1)
2736 err(1, "pledge");
2737 #endif
2739 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2740 switch (ch) {
2741 case 'c':
2742 commit_id_str = optarg;
2743 break;
2744 case 'r':
2745 repo_path = realpath(optarg, NULL);
2746 if (repo_path == NULL)
2747 err(1, "-r option");
2748 break;
2749 default:
2750 usage();
2751 /* NOTREACHED */
2755 argc -= optind;
2756 argv += optind;
2758 if (argc == 1)
2759 path = argv[0];
2760 else
2761 usage_blame();
2763 cwd = getcwd(NULL, 0);
2764 if (cwd == NULL) {
2765 error = got_error_from_errno();
2766 goto done;
2768 if (repo_path == NULL) {
2769 repo_path = strdup(cwd);
2770 if (repo_path == NULL) {
2771 error = got_error_from_errno();
2772 goto done;
2777 error = got_repo_open(&repo, repo_path);
2778 if (error != NULL)
2779 return error;
2781 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2782 if (error != NULL)
2783 goto done;
2785 if (commit_id_str == NULL) {
2786 struct got_reference *head_ref;
2787 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2788 if (error != NULL)
2789 goto done;
2790 error = got_ref_resolve(&commit_id, repo, head_ref);
2791 got_ref_close(head_ref);
2792 } else {
2793 struct got_object *obj;
2794 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2795 if (error != NULL)
2796 goto done;
2797 commit_id = got_object_id_dup(got_object_get_id(obj));
2798 if (commit_id == NULL)
2799 error = got_error_from_errno();
2800 got_object_close(obj);
2802 if (error != NULL)
2803 goto done;
2805 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2806 if (view == NULL) {
2807 error = got_error_from_errno();
2808 goto done;
2810 error = open_blame_view(view, in_repo_path, commit_id, repo);
2811 if (error)
2812 goto done;
2813 error = view_loop(view);
2814 done:
2815 free(repo_path);
2816 free(cwd);
2817 free(commit_id);
2818 if (repo)
2819 got_repo_close(repo);
2820 return error;
2823 static const struct got_error *
2824 draw_tree_entries(struct tog_view *view,
2825 struct got_tree_entry **first_displayed_entry,
2826 struct got_tree_entry **last_displayed_entry,
2827 struct got_tree_entry **selected_entry, int *ndisplayed,
2828 const char *label, int show_ids, const char *parent_path,
2829 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2831 const struct got_error *err = NULL;
2832 struct got_tree_entry *te;
2833 wchar_t *wline;
2834 int width, n;
2836 *ndisplayed = 0;
2838 werase(view->window);
2840 if (limit == 0)
2841 return NULL;
2843 err = format_line(&wline, &width, label, view->ncols);
2844 if (err)
2845 return err;
2846 if (view_needs_focus_indication(view))
2847 wstandout(view->window);
2848 waddwstr(view->window, wline);
2849 if (view_needs_focus_indication(view))
2850 wstandend(view->window);
2851 free(wline);
2852 wline = NULL;
2853 if (width < view->ncols)
2854 waddch(view->window, '\n');
2855 if (--limit <= 0)
2856 return NULL;
2857 err = format_line(&wline, &width, parent_path, view->ncols);
2858 if (err)
2859 return err;
2860 waddwstr(view->window, wline);
2861 free(wline);
2862 wline = NULL;
2863 if (width < view->ncols)
2864 waddch(view->window, '\n');
2865 if (--limit <= 0)
2866 return NULL;
2867 waddch(view->window, '\n');
2868 if (--limit <= 0)
2869 return NULL;
2871 te = SIMPLEQ_FIRST(&entries->head);
2872 if (*first_displayed_entry == NULL) {
2873 if (selected == 0) {
2874 if (view->focussed)
2875 wstandout(view->window);
2876 *selected_entry = NULL;
2878 waddstr(view->window, " ..\n"); /* parent directory */
2879 if (selected == 0 && view->focussed)
2880 wstandend(view->window);
2881 (*ndisplayed)++;
2882 if (--limit <= 0)
2883 return NULL;
2884 n = 1;
2885 } else {
2886 n = 0;
2887 while (te != *first_displayed_entry)
2888 te = SIMPLEQ_NEXT(te, entry);
2891 while (te) {
2892 char *line = NULL, *id_str = NULL;
2894 if (show_ids) {
2895 err = got_object_id_str(&id_str, te->id);
2896 if (err)
2897 return got_error_from_errno();
2899 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2900 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2901 free(id_str);
2902 return got_error_from_errno();
2904 free(id_str);
2905 err = format_line(&wline, &width, line, view->ncols);
2906 if (err) {
2907 free(line);
2908 break;
2910 if (n == selected) {
2911 if (view->focussed)
2912 wstandout(view->window);
2913 *selected_entry = te;
2915 waddwstr(view->window, wline);
2916 if (width < view->ncols)
2917 waddch(view->window, '\n');
2918 if (n == selected && view->focussed)
2919 wstandend(view->window);
2920 free(line);
2921 free(wline);
2922 wline = NULL;
2923 n++;
2924 (*ndisplayed)++;
2925 *last_displayed_entry = te;
2926 if (--limit <= 0)
2927 break;
2928 te = SIMPLEQ_NEXT(te, entry);
2931 return err;
2934 static void
2935 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2936 const struct got_tree_entries *entries, int isroot)
2938 struct got_tree_entry *te, *prev;
2939 int i;
2941 if (*first_displayed_entry == NULL)
2942 return;
2944 te = SIMPLEQ_FIRST(&entries->head);
2945 if (*first_displayed_entry == te) {
2946 if (!isroot)
2947 *first_displayed_entry = NULL;
2948 return;
2951 /* XXX this is stupid... switch to TAILQ? */
2952 for (i = 0; i < maxscroll; i++) {
2953 while (te != *first_displayed_entry) {
2954 prev = te;
2955 te = SIMPLEQ_NEXT(te, entry);
2957 *first_displayed_entry = prev;
2958 te = SIMPLEQ_FIRST(&entries->head);
2960 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2961 *first_displayed_entry = NULL;
2964 static void
2965 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2966 struct got_tree_entry *last_displayed_entry,
2967 const struct got_tree_entries *entries)
2969 struct got_tree_entry *next;
2970 int n = 0;
2972 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2973 return;
2975 if (*first_displayed_entry)
2976 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2977 else
2978 next = SIMPLEQ_FIRST(&entries->head);
2979 while (next) {
2980 *first_displayed_entry = next;
2981 if (++n >= maxscroll)
2982 break;
2983 next = SIMPLEQ_NEXT(next, entry);
2987 static const struct got_error *
2988 tree_entry_path(char **path, struct tog_parent_trees *parents,
2989 struct got_tree_entry *te)
2991 const struct got_error *err = NULL;
2992 struct tog_parent_tree *pt;
2993 size_t len = 2; /* for leading slash and NUL */
2995 TAILQ_FOREACH(pt, parents, entry)
2996 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2997 if (te)
2998 len += strlen(te->name);
3000 *path = calloc(1, len);
3001 if (path == NULL)
3002 return got_error_from_errno();
3004 (*path)[0] = '/';
3005 pt = TAILQ_LAST(parents, tog_parent_trees);
3006 while (pt) {
3007 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3008 err = got_error(GOT_ERR_NO_SPACE);
3009 goto done;
3011 if (strlcat(*path, "/", len) >= len) {
3012 err = got_error(GOT_ERR_NO_SPACE);
3013 goto done;
3015 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3017 if (te) {
3018 if (strlcat(*path, te->name, len) >= len) {
3019 err = got_error(GOT_ERR_NO_SPACE);
3020 goto done;
3023 done:
3024 if (err) {
3025 free(*path);
3026 *path = NULL;
3028 return err;
3031 static const struct got_error *
3032 blame_tree_entry(struct tog_view **new_view, int begin_x,
3033 struct got_tree_entry *te, struct tog_parent_trees *parents,
3034 struct got_object_id *commit_id, struct got_repository *repo)
3036 const struct got_error *err = NULL;
3037 char *path;
3038 struct tog_view *blame_view;
3040 err = tree_entry_path(&path, parents, te);
3041 if (err)
3042 return err;
3044 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3045 if (blame_view == NULL)
3046 return got_error_from_errno();
3048 err = open_blame_view(blame_view, path, commit_id, repo);
3049 if (err) {
3050 view_close(blame_view);
3051 free(path);
3052 } else
3053 *new_view = blame_view;
3054 return err;
3057 static const struct got_error *
3058 log_tree_entry(struct tog_view **new_view, int begin_x,
3059 struct got_tree_entry *te, struct tog_parent_trees *parents,
3060 struct got_object_id *commit_id, struct got_repository *repo)
3062 struct tog_view *log_view;
3063 const struct got_error *err = NULL;
3064 char *path;
3066 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3067 if (log_view == NULL)
3068 return got_error_from_errno();
3070 err = tree_entry_path(&path, parents, te);
3071 if (err)
3072 return err;
3074 err = open_log_view(log_view, commit_id, repo, path, 0);
3075 if (err)
3076 view_close(log_view);
3077 else
3078 *new_view = log_view;
3079 free(path);
3080 return err;
3083 static const struct got_error *
3084 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3085 struct got_object_id *commit_id, struct got_repository *repo)
3087 const struct got_error *err = NULL;
3088 char *commit_id_str = NULL;
3089 struct tog_tree_view_state *s = &view->state.tree;
3091 TAILQ_INIT(&s->parents);
3093 err = got_object_id_str(&commit_id_str, commit_id);
3094 if (err != NULL)
3095 goto done;
3097 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3098 err = got_error_from_errno();
3099 goto done;
3102 s->root = s->tree = root;
3103 s->entries = got_object_tree_get_entries(root);
3104 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3105 s->commit_id = got_object_id_dup(commit_id);
3106 if (s->commit_id == NULL) {
3107 err = got_error_from_errno();
3108 goto done;
3110 s->repo = repo;
3112 view->show = show_tree_view;
3113 view->input = input_tree_view;
3114 view->close = close_tree_view;
3115 done:
3116 free(commit_id_str);
3117 if (err) {
3118 free(s->tree_label);
3119 s->tree_label = NULL;
3121 return err;
3124 static const struct got_error *
3125 close_tree_view(struct tog_view *view)
3127 struct tog_tree_view_state *s = &view->state.tree;
3129 free(s->tree_label);
3130 s->tree_label = NULL;
3131 free(s->commit_id);
3132 s->commit_id = NULL;
3133 while (!TAILQ_EMPTY(&s->parents)) {
3134 struct tog_parent_tree *parent;
3135 parent = TAILQ_FIRST(&s->parents);
3136 TAILQ_REMOVE(&s->parents, parent, entry);
3137 free(parent);
3140 if (s->tree != s->root)
3141 got_object_tree_close(s->tree);
3142 got_object_tree_close(s->root);
3144 return NULL;
3147 static const struct got_error *
3148 show_tree_view(struct tog_view *view)
3150 const struct got_error *err = NULL;
3151 struct tog_tree_view_state *s = &view->state.tree;
3152 char *parent_path;
3154 err = tree_entry_path(&parent_path, &s->parents, NULL);
3155 if (err)
3156 return err;
3158 err = draw_tree_entries(view, &s->first_displayed_entry,
3159 &s->last_displayed_entry, &s->selected_entry,
3160 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3161 s->entries, s->selected, view->nlines, s->tree == s->root);
3162 free(parent_path);
3164 view_vborder(view);
3165 return err;
3168 static const struct got_error *
3169 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3170 struct tog_view **focus_view, struct tog_view *view, int ch)
3172 const struct got_error *err = NULL;
3173 struct tog_tree_view_state *s = &view->state.tree;
3174 struct tog_view *log_view;
3175 int begin_x = 0;
3177 switch (ch) {
3178 case 'i':
3179 s->show_ids = !s->show_ids;
3180 break;
3181 case 'l':
3182 if (!s->selected_entry)
3183 break;
3184 if (view_is_parent_view(view))
3185 begin_x = view_split_begin_x(view->begin_x);
3186 err = log_tree_entry(&log_view, begin_x,
3187 s->selected_entry, &s->parents,
3188 s->commit_id, s->repo);
3189 if (view_is_parent_view(view)) {
3190 err = view_close_child(view);
3191 if (err)
3192 return err;
3193 err = view_set_child(view, log_view);
3194 if (err) {
3195 view_close(log_view);
3196 break;
3198 *focus_view = log_view;
3199 view->child_focussed = 1;
3200 } else
3201 *new_view = log_view;
3202 break;
3203 case 'k':
3204 case KEY_UP:
3205 if (s->selected > 0)
3206 s->selected--;
3207 if (s->selected > 0)
3208 break;
3209 tree_scroll_up(&s->first_displayed_entry, 1,
3210 s->entries, s->tree == s->root);
3211 break;
3212 case KEY_PPAGE:
3213 s->selected = 0;
3214 if (SIMPLEQ_FIRST(&s->entries->head) ==
3215 s->first_displayed_entry) {
3216 if (s->tree != s->root)
3217 s->first_displayed_entry = NULL;
3218 break;
3220 tree_scroll_up(&s->first_displayed_entry,
3221 view->nlines, s->entries,
3222 s->tree == s->root);
3223 break;
3224 case 'j':
3225 case KEY_DOWN:
3226 if (s->selected < s->ndisplayed - 1) {
3227 s->selected++;
3228 break;
3230 tree_scroll_down(&s->first_displayed_entry, 1,
3231 s->last_displayed_entry, s->entries);
3232 break;
3233 case KEY_NPAGE:
3234 tree_scroll_down(&s->first_displayed_entry,
3235 view->nlines, s->last_displayed_entry,
3236 s->entries);
3237 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3238 entry))
3239 break;
3240 /* can't scroll any further; move cursor down */
3241 if (s->selected < s->ndisplayed - 1)
3242 s->selected = s->ndisplayed - 1;
3243 break;
3244 case KEY_ENTER:
3245 case '\r':
3246 if (s->selected_entry == NULL) {
3247 struct tog_parent_tree *parent;
3248 case KEY_BACKSPACE:
3249 /* user selected '..' */
3250 if (s->tree == s->root)
3251 break;
3252 parent = TAILQ_FIRST(&s->parents);
3253 TAILQ_REMOVE(&s->parents, parent,
3254 entry);
3255 got_object_tree_close(s->tree);
3256 s->tree = parent->tree;
3257 s->entries =
3258 got_object_tree_get_entries(s->tree);
3259 s->first_displayed_entry =
3260 parent->first_displayed_entry;
3261 s->selected_entry =
3262 parent->selected_entry;
3263 s->selected = parent->selected;
3264 free(parent);
3265 } else if (S_ISDIR(s->selected_entry->mode)) {
3266 struct tog_parent_tree *parent;
3267 struct got_tree_object *child;
3268 err = got_object_open_as_tree(&child,
3269 s->repo, s->selected_entry->id);
3270 if (err)
3271 break;
3272 parent = calloc(1, sizeof(*parent));
3273 if (parent == NULL) {
3274 err = got_error_from_errno();
3275 break;
3277 parent->tree = s->tree;
3278 parent->first_displayed_entry =
3279 s->first_displayed_entry;
3280 parent->selected_entry = s->selected_entry;
3281 parent->selected = s->selected;
3282 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3283 s->tree = child;
3284 s->entries =
3285 got_object_tree_get_entries(s->tree);
3286 s->selected = 0;
3287 s->first_displayed_entry = NULL;
3288 } else if (S_ISREG(s->selected_entry->mode)) {
3289 struct tog_view *blame_view;
3290 int begin_x = view_is_parent_view(view) ?
3291 view_split_begin_x(view->begin_x) : 0;
3293 err = blame_tree_entry(&blame_view, begin_x,
3294 s->selected_entry, &s->parents, s->commit_id,
3295 s->repo);
3296 if (err)
3297 break;
3298 if (view_is_parent_view(view)) {
3299 err = view_close_child(view);
3300 if (err)
3301 return err;
3302 err = view_set_child(view, blame_view);
3303 if (err) {
3304 view_close(blame_view);
3305 break;
3307 *focus_view = blame_view;
3308 view->child_focussed = 1;
3309 } else
3310 *new_view = blame_view;
3312 break;
3313 case KEY_RESIZE:
3314 if (s->selected > view->nlines)
3315 s->selected = s->ndisplayed - 1;
3316 break;
3317 default:
3318 break;
3321 return err;
3324 __dead static void
3325 usage_tree(void)
3327 endwin();
3328 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3329 getprogname());
3330 exit(1);
3333 static const struct got_error *
3334 cmd_tree(int argc, char *argv[])
3336 const struct got_error *error;
3337 struct got_repository *repo = NULL;
3338 char *repo_path = NULL;
3339 struct got_object_id *commit_id = NULL;
3340 char *commit_id_arg = NULL;
3341 struct got_commit_object *commit = NULL;
3342 struct got_tree_object *tree = NULL;
3343 int ch;
3344 struct tog_view *view;
3346 #ifndef PROFILE
3347 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3348 == -1)
3349 err(1, "pledge");
3350 #endif
3352 while ((ch = getopt(argc, argv, "c:")) != -1) {
3353 switch (ch) {
3354 case 'c':
3355 commit_id_arg = optarg;
3356 break;
3357 default:
3358 usage();
3359 /* NOTREACHED */
3363 argc -= optind;
3364 argv += optind;
3366 if (argc == 0) {
3367 repo_path = getcwd(NULL, 0);
3368 if (repo_path == NULL)
3369 return got_error_from_errno();
3370 } else if (argc == 1) {
3371 repo_path = realpath(argv[0], NULL);
3372 if (repo_path == NULL)
3373 return got_error_from_errno();
3374 } else
3375 usage_log();
3377 error = got_repo_open(&repo, repo_path);
3378 free(repo_path);
3379 if (error != NULL)
3380 return error;
3382 if (commit_id_arg == NULL) {
3383 error = get_head_commit_id(&commit_id, repo);
3384 if (error != NULL)
3385 goto done;
3386 } else {
3387 struct got_object *obj;
3388 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3389 if (error == NULL) {
3390 commit_id = got_object_id_dup(got_object_get_id(obj));
3391 if (commit_id == NULL)
3392 error = got_error_from_errno();
3395 if (error != NULL)
3396 goto done;
3398 error = got_object_open_as_commit(&commit, repo, commit_id);
3399 if (error != NULL)
3400 goto done;
3402 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3403 if (error != NULL)
3404 goto done;
3406 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3407 if (view == NULL) {
3408 error = got_error_from_errno();
3409 goto done;
3411 error = open_tree_view(view, tree, commit_id, repo);
3412 if (error)
3413 goto done;
3414 error = view_loop(view);
3415 done:
3416 free(commit_id);
3417 if (commit)
3418 got_object_commit_close(commit);
3419 if (tree)
3420 got_object_tree_close(tree);
3421 if (repo)
3422 got_repo_close(repo);
3423 return error;
3426 static void
3427 init_curses(void)
3429 initscr();
3430 cbreak();
3431 halfdelay(1); /* Do fast refresh while initial view is loading. */
3432 noecho();
3433 nonl();
3434 intrflush(stdscr, FALSE);
3435 keypad(stdscr, TRUE);
3436 curs_set(0);
3437 signal(SIGWINCH, tog_sigwinch);
3440 __dead static void
3441 usage(void)
3443 int i;
3445 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3446 "Available commands:\n", getprogname());
3447 for (i = 0; i < nitems(tog_commands); i++) {
3448 struct tog_cmd *cmd = &tog_commands[i];
3449 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3451 exit(1);
3454 static char **
3455 make_argv(const char *arg0, const char *arg1)
3457 char **argv;
3458 int argc = (arg1 == NULL ? 1 : 2);
3460 argv = calloc(argc, sizeof(char *));
3461 if (argv == NULL)
3462 err(1, "calloc");
3463 argv[0] = strdup(arg0);
3464 if (argv[0] == NULL)
3465 err(1, "calloc");
3466 if (arg1) {
3467 argv[1] = strdup(arg1);
3468 if (argv[1] == NULL)
3469 err(1, "calloc");
3472 return argv;
3475 int
3476 main(int argc, char *argv[])
3478 const struct got_error *error = NULL;
3479 struct tog_cmd *cmd = NULL;
3480 int ch, hflag = 0;
3481 char **cmd_argv = NULL;
3483 setlocale(LC_ALL, "");
3485 while ((ch = getopt(argc, argv, "h")) != -1) {
3486 switch (ch) {
3487 case 'h':
3488 hflag = 1;
3489 break;
3490 default:
3491 usage();
3492 /* NOTREACHED */
3496 argc -= optind;
3497 argv += optind;
3498 optind = 0;
3499 optreset = 1;
3501 if (argc == 0) {
3502 if (hflag)
3503 usage();
3504 /* Build an argument vector which runs a default command. */
3505 cmd = &tog_commands[0];
3506 cmd_argv = make_argv(cmd->name, NULL);
3507 argc = 1;
3508 } else {
3509 int i;
3511 /* Did the user specific a command? */
3512 for (i = 0; i < nitems(tog_commands); i++) {
3513 if (strncmp(tog_commands[i].name, argv[0],
3514 strlen(argv[0])) == 0) {
3515 cmd = &tog_commands[i];
3516 if (hflag)
3517 tog_commands[i].cmd_usage();
3518 break;
3521 if (cmd == NULL) {
3522 /* Did the user specify a repository? */
3523 char *repo_path = realpath(argv[0], NULL);
3524 if (repo_path) {
3525 struct got_repository *repo;
3526 error = got_repo_open(&repo, repo_path);
3527 if (error == NULL)
3528 got_repo_close(repo);
3529 } else
3530 error = got_error_from_errno();
3531 if (error) {
3532 if (hflag) {
3533 fprintf(stderr, "%s: '%s' is not a "
3534 "known command\n", getprogname(),
3535 argv[0]);
3536 usage();
3538 fprintf(stderr, "%s: '%s' is neither a known "
3539 "command nor a path to a repository\n",
3540 getprogname(), argv[0]);
3541 free(repo_path);
3542 return 1;
3544 cmd = &tog_commands[0];
3545 cmd_argv = make_argv(cmd->name, repo_path);
3546 argc = 2;
3547 free(repo_path);
3551 init_curses();
3553 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3554 if (error)
3555 goto done;
3556 done:
3557 endwin();
3558 free(cmd_argv);
3559 if (error)
3560 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3561 return 0;