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()
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;
1443 errcode = pthread_create(&s->thread, NULL, log_thread,
1444 &s->thread_args);
1445 if (errcode) {
1446 err = got_error_set_errno(errcode);
1447 goto done;
1450 done:
1451 if (err)
1452 close_log_view(view);
1453 return err;
1456 static const struct got_error *
1457 show_log_view(struct tog_view *view)
1459 struct tog_log_view_state *s = &view->state.log;
1461 return draw_commits(view, &s->last_displayed_entry,
1462 &s->selected_entry, s->first_displayed_entry,
1463 &s->commits, s->selected, view->nlines,
1464 s->in_repo_path, s->thread_args.commits_needed);
1467 static const struct got_error *
1468 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1469 struct tog_view **focus_view, struct tog_view *view, int ch)
1471 const struct got_error *err = NULL;
1472 struct tog_log_view_state *s = &view->state.log;
1473 char *parent_path;
1474 struct tog_view *diff_view = NULL, *tree_view = NULL;
1475 int begin_x = 0;
1477 switch (ch) {
1478 case 'q':
1479 s->quit = 1;
1480 break;
1481 case 'k':
1482 case KEY_UP:
1483 if (s->selected > 0)
1484 s->selected--;
1485 if (s->selected > 0)
1486 break;
1487 scroll_up(&s->first_displayed_entry, 1,
1488 &s->commits);
1489 break;
1490 case KEY_PPAGE:
1491 if (TAILQ_FIRST(&s->commits.head) ==
1492 s->first_displayed_entry) {
1493 s->selected = 0;
1494 break;
1496 scroll_up(&s->first_displayed_entry,
1497 view->nlines, &s->commits);
1498 break;
1499 case 'j':
1500 case KEY_DOWN:
1501 if (s->selected < MIN(view->nlines - 2,
1502 s->commits.ncommits - 1)) {
1503 s->selected++;
1504 break;
1506 err = scroll_down(&s->first_displayed_entry, 1,
1507 &s->last_displayed_entry, &s->commits,
1508 &s->thread_args.log_complete,
1509 &s->thread_args.commits_needed,
1510 &s->thread_args.need_commits);
1511 break;
1512 case KEY_NPAGE: {
1513 struct commit_queue_entry *first;
1514 first = s->first_displayed_entry;
1515 err = scroll_down(&s->first_displayed_entry,
1516 view->nlines, &s->last_displayed_entry,
1517 &s->commits, &s->thread_args.log_complete,
1518 &s->thread_args.commits_needed,
1519 &s->thread_args.need_commits);
1520 if (first == s->first_displayed_entry &&
1521 s->selected < MIN(view->nlines - 2,
1522 s->commits.ncommits - 1)) {
1523 /* can't scroll further down */
1524 s->selected = MIN(view->nlines - 2,
1525 s->commits.ncommits - 1);
1527 err = NULL;
1528 break;
1530 case KEY_RESIZE:
1531 if (s->selected > view->nlines - 2)
1532 s->selected = view->nlines - 2;
1533 if (s->selected > s->commits.ncommits - 1)
1534 s->selected = s->commits.ncommits - 1;
1535 break;
1536 case KEY_ENTER:
1537 case '\r':
1538 if (view_is_parent_view(view))
1539 begin_x = view_split_begin_x(view->begin_x);
1540 err = open_diff_view_for_commit(&diff_view, begin_x,
1541 s->selected_entry->id, s->selected_entry->commit,
1542 s->repo);
1543 if (err)
1544 break;
1545 if (view_is_parent_view(view)) {
1546 err = view_close_child(view);
1547 if (err)
1548 return err;
1549 err = view_set_child(view, diff_view);
1550 if (err) {
1551 view_close(diff_view);
1552 break;
1554 if (!view_is_splitscreen(diff_view)) {
1555 *focus_view = diff_view;
1556 view->child_focussed = 1;
1558 } else
1559 *new_view = diff_view;
1560 break;
1561 case 't':
1562 if (view_is_parent_view(view))
1563 begin_x = view_split_begin_x(view->begin_x);
1564 err = browse_commit(&tree_view, begin_x,
1565 s->selected_entry, s->repo);
1566 if (err)
1567 break;
1568 if (view_is_parent_view(view)) {
1569 err = view_close_child(view);
1570 if (err)
1571 return err;
1572 err = view_set_child(view, tree_view);
1573 if (err) {
1574 view_close(tree_view);
1575 break;
1577 *focus_view = tree_view;
1578 view->child_focussed = 1;
1579 } else
1580 *new_view = tree_view;
1581 break;
1582 case KEY_BACKSPACE:
1583 if (strcmp(s->in_repo_path, "/") == 0)
1584 break;
1585 parent_path = dirname(s->in_repo_path);
1586 if (parent_path && strcmp(parent_path, ".") != 0) {
1587 struct tog_view *lv;
1588 err = stop_log_thread(s);
1589 if (err)
1590 return err;
1591 lv = view_open(view->nlines, view->ncols,
1592 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1593 if (lv == NULL)
1594 return got_error_from_errno();
1595 err = open_log_view(lv, s->start_id, s->repo,
1596 parent_path, 0);
1597 if (err)
1598 return err;;
1599 if (view_is_parent_view(view))
1600 *new_view = lv;
1601 else {
1602 view_set_child(view->parent, lv);
1603 *focus_view = lv;
1605 return NULL;
1607 break;
1608 default:
1609 break;
1612 return err;
1615 static const struct got_error *
1616 cmd_log(int argc, char *argv[])
1618 const struct got_error *error;
1619 struct got_repository *repo = NULL;
1620 struct got_object_id *start_id = NULL;
1621 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1622 char *start_commit = NULL;
1623 int ch;
1624 struct tog_view *view;
1626 #ifndef PROFILE
1627 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1628 == -1)
1629 err(1, "pledge");
1630 #endif
1632 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1633 switch (ch) {
1634 case 'c':
1635 start_commit = optarg;
1636 break;
1637 case 'r':
1638 repo_path = realpath(optarg, NULL);
1639 if (repo_path == NULL)
1640 err(1, "-r option");
1641 break;
1642 default:
1643 usage();
1644 /* NOTREACHED */
1648 argc -= optind;
1649 argv += optind;
1651 if (argc == 0)
1652 path = strdup("");
1653 else if (argc == 1)
1654 path = strdup(argv[0]);
1655 else
1656 usage_log();
1657 if (path == NULL)
1658 return got_error_from_errno();
1660 cwd = getcwd(NULL, 0);
1661 if (cwd == NULL) {
1662 error = got_error_from_errno();
1663 goto done;
1665 if (repo_path == NULL) {
1666 repo_path = strdup(cwd);
1667 if (repo_path == NULL) {
1668 error = got_error_from_errno();
1669 goto done;
1673 error = got_repo_open(&repo, repo_path);
1674 if (error != NULL)
1675 goto done;
1677 if (start_commit == NULL) {
1678 error = get_head_commit_id(&start_id, repo);
1679 if (error != NULL)
1680 goto done;
1681 } else {
1682 struct got_object *obj;
1683 error = got_object_open_by_id_str(&obj, repo, start_commit);
1684 if (error == NULL) {
1685 start_id = got_object_id_dup(got_object_get_id(obj));
1686 if (start_id == NULL)
1687 error = got_error_from_errno();
1688 goto done;
1691 if (error != NULL)
1692 goto done;
1694 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1695 if (view == NULL) {
1696 error = got_error_from_errno();
1697 goto done;
1699 error = open_log_view(view, start_id, repo, path, 1);
1700 if (error)
1701 goto done;
1702 error = view_loop(view);
1703 done:
1704 free(repo_path);
1705 free(cwd);
1706 free(path);
1707 free(start_id);
1708 if (repo)
1709 got_repo_close(repo);
1710 return error;
1713 __dead static void
1714 usage_diff(void)
1716 endwin();
1717 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1718 getprogname());
1719 exit(1);
1722 static char *
1723 parse_next_line(FILE *f, size_t *len)
1725 char *line;
1726 size_t linelen;
1727 size_t lineno;
1728 const char delim[3] = { '\0', '\0', '\0'};
1730 line = fparseln(f, &linelen, &lineno, delim, 0);
1731 if (len)
1732 *len = linelen;
1733 return line;
1736 static const struct got_error *
1737 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1738 int *last_displayed_line, int *eof, int max_lines,
1739 char * header)
1741 const struct got_error *err;
1742 int nlines = 0, nprinted = 0;
1743 char *line;
1744 size_t len;
1745 wchar_t *wline;
1746 int width;
1748 rewind(f);
1749 werase(view->window);
1751 if (header) {
1752 err = format_line(&wline, &width, header, view->ncols);
1753 if (err) {
1754 return err;
1757 if (view_needs_focus_indication(view))
1758 wstandout(view->window);
1759 waddwstr(view->window, wline);
1760 if (view_needs_focus_indication(view))
1761 wstandend(view->window);
1762 if (width < view->ncols)
1763 waddch(view->window, '\n');
1765 if (max_lines <= 1)
1766 return NULL;
1767 max_lines--;
1770 *eof = 0;
1771 while (nprinted < max_lines) {
1772 line = parse_next_line(f, &len);
1773 if (line == NULL) {
1774 *eof = 1;
1775 break;
1777 if (++nlines < *first_displayed_line) {
1778 free(line);
1779 continue;
1782 err = format_line(&wline, &width, line, view->ncols);
1783 if (err) {
1784 free(line);
1785 return err;
1787 waddwstr(view->window, wline);
1788 if (width < view->ncols)
1789 waddch(view->window, '\n');
1790 if (++nprinted == 1)
1791 *first_displayed_line = nlines;
1792 free(line);
1793 free(wline);
1794 wline = NULL;
1796 *last_displayed_line = nlines;
1798 view_vborder(view);
1800 return NULL;
1803 static const struct got_error *
1804 create_diff(struct tog_diff_view_state *s)
1806 const struct got_error *err = NULL;
1807 struct got_object *obj1 = NULL, *obj2 = NULL;
1808 FILE *f = NULL;
1810 if (s->id1) {
1811 err = got_object_open(&obj1, s->repo, s->id1);
1812 if (err)
1813 return err;
1816 err = got_object_open(&obj2, s->repo, s->id2);
1817 if (err)
1818 goto done;
1820 f = got_opentemp();
1821 if (f == NULL) {
1822 err = got_error_from_errno();
1823 goto done;
1825 if (s->f)
1826 fclose(s->f);
1827 s->f = f;
1829 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1830 case GOT_OBJ_TYPE_BLOB:
1831 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1832 s->diff_context, s->repo, f);
1833 break;
1834 case GOT_OBJ_TYPE_TREE:
1835 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1836 s->diff_context, s->repo, f);
1837 break;
1838 case GOT_OBJ_TYPE_COMMIT:
1839 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1840 s->repo, f);
1841 break;
1842 default:
1843 err = got_error(GOT_ERR_OBJ_TYPE);
1844 break;
1846 done:
1847 if (obj1)
1848 got_object_close(obj1);
1849 got_object_close(obj2);
1850 if (f)
1851 fflush(f);
1852 return err;
1855 static const struct got_error *
1856 open_diff_view(struct tog_view *view, struct got_object *obj1,
1857 struct got_object *obj2, struct got_repository *repo)
1859 const struct got_error *err;
1861 if (obj1 != NULL && obj2 != NULL &&
1862 got_object_get_type(obj1) != got_object_get_type(obj2))
1863 return got_error(GOT_ERR_OBJ_TYPE);
1865 if (obj1) {
1866 struct got_object_id *id1;
1867 id1 = got_object_id_dup(got_object_get_id(obj1));
1868 if (id1 == NULL)
1869 return got_error_from_errno();
1870 view->state.diff.id1 = id1;
1871 } else
1872 view->state.diff.id1 = NULL;
1874 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1875 if (view->state.diff.id2 == NULL) {
1876 free(view->state.diff.id1);
1877 view->state.diff.id1 = NULL;
1878 return got_error_from_errno();
1880 view->state.diff.f = NULL;
1881 view->state.diff.first_displayed_line = 1;
1882 view->state.diff.last_displayed_line = view->nlines;
1883 view->state.diff.diff_context = 3;
1884 view->state.diff.repo = repo;
1886 err = create_diff(&view->state.diff);
1887 if (err) {
1888 free(view->state.diff.id1);
1889 view->state.diff.id1 = NULL;
1890 free(view->state.diff.id2);
1891 view->state.diff.id2 = NULL;
1892 return err;
1895 view->show = show_diff_view;
1896 view->input = input_diff_view;
1897 view->close = close_diff_view;
1899 return NULL;
1902 static const struct got_error *
1903 close_diff_view(struct tog_view *view)
1905 const struct got_error *err = NULL;
1907 free(view->state.diff.id1);
1908 view->state.diff.id1 = NULL;
1909 free(view->state.diff.id2);
1910 view->state.diff.id2 = NULL;
1911 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1912 err = got_error_from_errno();
1913 return err;
1916 static const struct got_error *
1917 show_diff_view(struct tog_view *view)
1919 const struct got_error *err;
1920 struct tog_diff_view_state *s = &view->state.diff;
1921 char *id_str1 = NULL, *id_str2, *header;
1923 if (s->id1) {
1924 err = got_object_id_str(&id_str1, s->id1);
1925 if (err)
1926 return err;
1928 err = got_object_id_str(&id_str2, s->id2);
1929 if (err)
1930 return err;
1932 if (asprintf(&header, "diff: %s %s",
1933 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1934 err = got_error_from_errno();
1935 free(id_str1);
1936 free(id_str2);
1937 return err;
1939 free(id_str1);
1940 free(id_str2);
1942 return draw_file(view, s->f, &s->first_displayed_line,
1943 &s->last_displayed_line, &s->eof, view->nlines,
1944 header);
1947 static const struct got_error *
1948 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1949 struct tog_view **focus_view, struct tog_view *view, int ch)
1951 const struct got_error *err = NULL;
1952 struct tog_diff_view_state *s = &view->state.diff;
1953 int i;
1955 switch (ch) {
1956 case 'k':
1957 case KEY_UP:
1958 if (s->first_displayed_line > 1)
1959 s->first_displayed_line--;
1960 break;
1961 case KEY_PPAGE:
1962 i = 0;
1963 while (i++ < view->nlines - 1 &&
1964 s->first_displayed_line > 1)
1965 s->first_displayed_line--;
1966 break;
1967 case 'j':
1968 case KEY_DOWN:
1969 if (!s->eof)
1970 s->first_displayed_line++;
1971 break;
1972 case KEY_NPAGE:
1973 case ' ':
1974 i = 0;
1975 while (!s->eof && i++ < view->nlines - 1) {
1976 char *line;
1977 line = parse_next_line(s->f, NULL);
1978 s->first_displayed_line++;
1979 if (line == NULL)
1980 break;
1982 break;
1983 case '[':
1984 if (s->diff_context > 0) {
1985 s->diff_context--;
1986 err = create_diff(s);
1988 break;
1989 case ']':
1990 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1991 s->diff_context++;
1992 err = create_diff(s);
1994 break;
1995 default:
1996 break;
1999 return err;
2002 static const struct got_error *
2003 cmd_diff(int argc, char *argv[])
2005 const struct got_error *error = NULL;
2006 struct got_repository *repo = NULL;
2007 struct got_object *obj1 = NULL, *obj2 = NULL;
2008 char *repo_path = NULL;
2009 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
2010 int ch;
2011 struct tog_view *view;
2013 #ifndef PROFILE
2014 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2015 == -1)
2016 err(1, "pledge");
2017 #endif
2019 while ((ch = getopt(argc, argv, "")) != -1) {
2020 switch (ch) {
2021 default:
2022 usage();
2023 /* NOTREACHED */
2027 argc -= optind;
2028 argv += optind;
2030 if (argc == 0) {
2031 usage_diff(); /* TODO show local worktree changes */
2032 } else if (argc == 2) {
2033 repo_path = getcwd(NULL, 0);
2034 if (repo_path == NULL)
2035 return got_error_from_errno();
2036 obj_id_str1 = argv[0];
2037 obj_id_str2 = argv[1];
2038 } else if (argc == 3) {
2039 repo_path = realpath(argv[0], NULL);
2040 if (repo_path == NULL)
2041 return got_error_from_errno();
2042 obj_id_str1 = argv[1];
2043 obj_id_str2 = argv[2];
2044 } else
2045 usage_diff();
2047 error = got_repo_open(&repo, repo_path);
2048 free(repo_path);
2049 if (error)
2050 goto done;
2052 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2053 if (error)
2054 goto done;
2056 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2057 if (error)
2058 goto done;
2060 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2061 if (view == NULL) {
2062 error = got_error_from_errno();
2063 goto done;
2065 error = open_diff_view(view, obj1, obj2, repo);
2066 if (error)
2067 goto done;
2068 error = view_loop(view);
2069 done:
2070 got_repo_close(repo);
2071 if (obj1)
2072 got_object_close(obj1);
2073 if (obj2)
2074 got_object_close(obj2);
2075 return error;
2078 __dead static void
2079 usage_blame(void)
2081 endwin();
2082 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2083 getprogname());
2084 exit(1);
2087 struct tog_blame_line {
2088 int annotated;
2089 struct got_object_id *id;
2092 static const struct got_error *
2093 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2094 const char *path, struct tog_blame_line *lines, int nlines,
2095 int blame_complete, int selected_line, int *first_displayed_line,
2096 int *last_displayed_line, int *eof, int max_lines)
2098 const struct got_error *err;
2099 int lineno = 0, nprinted = 0;
2100 char *line;
2101 size_t len;
2102 wchar_t *wline;
2103 int width, wlimit;
2104 struct tog_blame_line *blame_line;
2105 struct got_object_id *prev_id = NULL;
2106 char *id_str;
2108 err = got_object_id_str(&id_str, id);
2109 if (err)
2110 return err;
2112 rewind(f);
2113 werase(view->window);
2115 if (asprintf(&line, "commit: %s", id_str) == -1) {
2116 err = got_error_from_errno();
2117 free(id_str);
2118 return err;
2121 err = format_line(&wline, &width, line, view->ncols);
2122 free(line);
2123 line = NULL;
2124 if (view_needs_focus_indication(view))
2125 wstandout(view->window);
2126 waddwstr(view->window, wline);
2127 if (view_needs_focus_indication(view))
2128 wstandend(view->window);
2129 free(wline);
2130 wline = NULL;
2131 if (width < view->ncols)
2132 waddch(view->window, '\n');
2134 if (asprintf(&line, "[%d/%d] %s%s",
2135 *first_displayed_line - 1 + selected_line, nlines,
2136 blame_complete ? "" : "annotating ", path) == -1) {
2137 free(id_str);
2138 return got_error_from_errno();
2140 free(id_str);
2141 err = format_line(&wline, &width, line, view->ncols);
2142 free(line);
2143 line = NULL;
2144 if (err)
2145 return err;
2146 waddwstr(view->window, wline);
2147 free(wline);
2148 wline = NULL;
2149 if (width < view->ncols)
2150 waddch(view->window, '\n');
2152 *eof = 0;
2153 while (nprinted < max_lines - 2) {
2154 line = parse_next_line(f, &len);
2155 if (line == NULL) {
2156 *eof = 1;
2157 break;
2159 if (++lineno < *first_displayed_line) {
2160 free(line);
2161 continue;
2164 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2165 err = format_line(&wline, &width, line, wlimit);
2166 if (err) {
2167 free(line);
2168 return err;
2171 if (view->focussed && nprinted == selected_line - 1)
2172 wstandout(view->window);
2174 blame_line = &lines[lineno - 1];
2175 if (blame_line->annotated && prev_id &&
2176 got_object_id_cmp(prev_id, blame_line->id) == 0)
2177 waddstr(view->window, " ");
2178 else if (blame_line->annotated) {
2179 char *id_str;
2180 err = got_object_id_str(&id_str, blame_line->id);
2181 if (err) {
2182 free(line);
2183 free(wline);
2184 return err;
2186 wprintw(view->window, "%.8s ", id_str);
2187 free(id_str);
2188 prev_id = blame_line->id;
2189 } else {
2190 waddstr(view->window, "........ ");
2191 prev_id = NULL;
2194 waddwstr(view->window, wline);
2195 while (width < wlimit) {
2196 waddch(view->window, ' ');
2197 width++;
2199 if (view->focussed && nprinted == selected_line - 1)
2200 wstandend(view->window);
2201 if (++nprinted == 1)
2202 *first_displayed_line = lineno;
2203 free(line);
2204 free(wline);
2205 wline = NULL;
2207 *last_displayed_line = lineno;
2209 view_vborder(view);
2211 return NULL;
2214 static const struct got_error *
2215 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2217 const struct got_error *err = NULL;
2218 struct tog_blame_cb_args *a = arg;
2219 struct tog_blame_line *line;
2220 int errcode;
2222 if (nlines != a->nlines ||
2223 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2224 return got_error(GOT_ERR_RANGE);
2226 errcode = pthread_mutex_lock(&tog_mutex);
2227 if (errcode)
2228 return got_error_set_errno(errcode);
2230 if (*a->quit) { /* user has quit the blame view */
2231 err = got_error(GOT_ERR_ITER_COMPLETED);
2232 goto done;
2235 if (lineno == -1)
2236 goto done; /* no change in this commit */
2238 line = &a->lines[lineno - 1];
2239 if (line->annotated)
2240 goto done;
2242 line->id = got_object_id_dup(id);
2243 if (line->id == NULL) {
2244 err = got_error_from_errno();
2245 goto done;
2247 line->annotated = 1;
2248 done:
2249 errcode = pthread_mutex_unlock(&tog_mutex);
2250 if (errcode)
2251 err = got_error_set_errno(errcode);
2252 return err;
2255 static void *
2256 blame_thread(void *arg)
2258 const struct got_error *err;
2259 struct tog_blame_thread_args *ta = arg;
2260 struct tog_blame_cb_args *a = ta->cb_args;
2261 int errcode;
2263 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2264 blame_cb, ta->cb_args);
2266 errcode = pthread_mutex_lock(&tog_mutex);
2267 if (errcode)
2268 return (void *)got_error_set_errno(errcode);
2270 got_repo_close(ta->repo);
2271 ta->repo = NULL;
2272 *ta->complete = 1;
2274 errcode = pthread_mutex_unlock(&tog_mutex);
2275 if (errcode && err == NULL)
2276 err = got_error_set_errno(errcode);
2278 return (void *)err;
2281 static struct got_object_id *
2282 get_selected_commit_id(struct tog_blame_line *lines,
2283 int first_displayed_line, int selected_line)
2285 struct tog_blame_line *line;
2287 line = &lines[first_displayed_line - 1 + selected_line - 1];
2288 if (!line->annotated)
2289 return NULL;
2291 return line->id;
2294 static const struct got_error *
2295 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2296 struct tog_blame_line *lines, int first_displayed_line,
2297 int selected_line, struct got_repository *repo)
2299 const struct got_error *err = NULL;
2300 struct got_commit_object *commit = NULL;
2301 struct got_object_id *selected_id;
2302 struct got_object_qid *pid;
2304 *pobj = NULL;
2305 *obj = NULL;
2307 selected_id = get_selected_commit_id(lines,
2308 first_displayed_line, selected_line);
2309 if (selected_id == NULL)
2310 return NULL;
2312 err = got_object_open(obj, repo, selected_id);
2313 if (err)
2314 goto done;
2316 err = got_object_commit_open(&commit, repo, *obj);
2317 if (err)
2318 goto done;
2320 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2321 if (pid) {
2322 err = got_object_open(pobj, repo, pid->id);
2323 if (err)
2324 goto done;
2326 done:
2327 if (commit)
2328 got_object_commit_close(commit);
2329 return err;
2332 static const struct got_error *
2333 stop_blame(struct tog_blame *blame)
2335 const struct got_error *err = NULL;
2336 int i;
2338 if (blame->thread) {
2339 int errcode;
2340 errcode = pthread_mutex_unlock(&tog_mutex);
2341 if (errcode)
2342 return got_error_set_errno(errcode);
2343 errcode = pthread_join(blame->thread, (void **)&err);
2344 if (errcode)
2345 return got_error_set_errno(errcode);
2346 errcode = pthread_mutex_lock(&tog_mutex);
2347 if (errcode)
2348 return got_error_set_errno(errcode);
2349 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2350 err = NULL;
2351 blame->thread = NULL;
2353 if (blame->thread_args.repo) {
2354 got_repo_close(blame->thread_args.repo);
2355 blame->thread_args.repo = NULL;
2357 if (blame->f) {
2358 fclose(blame->f);
2359 blame->f = NULL;
2361 for (i = 0; i < blame->nlines; i++)
2362 free(blame->lines[i].id);
2363 free(blame->lines);
2364 blame->lines = NULL;
2365 free(blame->cb_args.commit_id);
2366 blame->cb_args.commit_id = NULL;
2368 return err;
2371 static const struct got_error *
2372 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2373 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2374 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2375 struct got_repository *repo)
2377 const struct got_error *err = NULL;
2378 struct got_blob_object *blob = NULL;
2379 struct got_repository *thread_repo = NULL;
2380 struct got_object_id *obj_id = NULL;
2381 struct got_object *obj = NULL;
2382 int errcode;
2384 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2385 if (err)
2386 goto done;
2388 err = got_object_open(&obj, repo, obj_id);
2389 if (err)
2390 goto done;
2392 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2393 err = got_error(GOT_ERR_OBJ_TYPE);
2394 goto done;
2397 err = got_object_blob_open(&blob, repo, obj, 8192);
2398 if (err)
2399 goto done;
2400 blame->f = got_opentemp();
2401 if (blame->f == NULL) {
2402 err = got_error_from_errno();
2403 goto done;
2405 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2406 blame->f, blob);
2407 if (err)
2408 goto done;
2410 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2411 if (blame->lines == NULL) {
2412 err = got_error_from_errno();
2413 goto done;
2416 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2417 if (err)
2418 goto done;
2420 blame->cb_args.view = view;
2421 blame->cb_args.lines = blame->lines;
2422 blame->cb_args.nlines = blame->nlines;
2423 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2424 if (blame->cb_args.commit_id == NULL) {
2425 err = got_error_from_errno();
2426 goto done;
2428 blame->cb_args.quit = done;
2430 blame->thread_args.path = path;
2431 blame->thread_args.repo = thread_repo;
2432 blame->thread_args.cb_args = &blame->cb_args;
2433 blame->thread_args.complete = blame_complete;
2434 *blame_complete = 0;
2436 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2437 &blame->thread_args);
2438 if (errcode) {
2439 err = got_error_set_errno(errcode);
2440 goto done;
2443 done:
2444 if (blob)
2445 got_object_blob_close(blob);
2446 free(obj_id);
2447 if (obj)
2448 got_object_close(obj);
2449 if (err)
2450 stop_blame(blame);
2451 return err;
2454 static const struct got_error *
2455 open_blame_view(struct tog_view *view, char *path,
2456 struct got_object_id *commit_id, struct got_repository *repo)
2458 const struct got_error *err = NULL;
2459 struct tog_blame_view_state *s = &view->state.blame;
2461 SIMPLEQ_INIT(&s->blamed_commits);
2463 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2464 if (err)
2465 return err;
2467 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2468 s->first_displayed_line = 1;
2469 s->last_displayed_line = view->nlines;
2470 s->selected_line = 1;
2471 s->blame_complete = 0;
2472 s->path = path;
2473 if (s->path == NULL)
2474 return got_error_from_errno();
2475 s->repo = repo;
2476 s->commit_id = commit_id;
2477 memset(&s->blame, 0, sizeof(s->blame));
2479 view->show = show_blame_view;
2480 view->input = input_blame_view;
2481 view->close = close_blame_view;
2483 return run_blame(&s->blame, view, &s->blame_complete,
2484 &s->first_displayed_line, &s->last_displayed_line,
2485 &s->selected_line, &s->done, &s->eof, s->path,
2486 s->blamed_commit->id, s->repo);
2489 static const struct got_error *
2490 close_blame_view(struct tog_view *view)
2492 const struct got_error *err = NULL;
2493 struct tog_blame_view_state *s = &view->state.blame;
2495 if (s->blame.thread)
2496 err = stop_blame(&s->blame);
2498 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2499 struct got_object_qid *blamed_commit;
2500 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2501 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2502 got_object_qid_free(blamed_commit);
2505 free(s->path);
2507 return err;
2510 static const struct got_error *
2511 show_blame_view(struct tog_view *view)
2513 const struct got_error *err = NULL;
2514 struct tog_blame_view_state *s = &view->state.blame;
2516 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2517 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2518 s->selected_line, &s->first_displayed_line,
2519 &s->last_displayed_line, &s->eof, view->nlines);
2521 view_vborder(view);
2522 return err;
2525 static const struct got_error *
2526 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2527 struct tog_view **focus_view, struct tog_view *view, int ch)
2529 const struct got_error *err = NULL, *thread_err = NULL;
2530 struct got_object *obj = NULL, *pobj = NULL;
2531 struct tog_view *diff_view;
2532 struct tog_blame_view_state *s = &view->state.blame;
2533 int begin_x = 0;
2535 switch (ch) {
2536 case 'q':
2537 s->done = 1;
2538 break;
2539 case 'k':
2540 case KEY_UP:
2541 if (s->selected_line > 1)
2542 s->selected_line--;
2543 else if (s->selected_line == 1 &&
2544 s->first_displayed_line > 1)
2545 s->first_displayed_line--;
2546 break;
2547 case KEY_PPAGE:
2548 if (s->first_displayed_line == 1) {
2549 s->selected_line = 1;
2550 break;
2552 if (s->first_displayed_line > view->nlines - 2)
2553 s->first_displayed_line -=
2554 (view->nlines - 2);
2555 else
2556 s->first_displayed_line = 1;
2557 break;
2558 case 'j':
2559 case KEY_DOWN:
2560 if (s->selected_line < view->nlines - 2 &&
2561 s->first_displayed_line +
2562 s->selected_line <= s->blame.nlines)
2563 s->selected_line++;
2564 else if (s->last_displayed_line <
2565 s->blame.nlines)
2566 s->first_displayed_line++;
2567 break;
2568 case 'b':
2569 case 'p': {
2570 struct got_object_id *id;
2571 id = get_selected_commit_id(s->blame.lines,
2572 s->first_displayed_line, s->selected_line);
2573 if (id == NULL || got_object_id_cmp(id,
2574 s->blamed_commit->id) == 0)
2575 break;
2576 err = open_selected_commit(&pobj, &obj,
2577 s->blame.lines, s->first_displayed_line,
2578 s->selected_line, s->repo);
2579 if (err)
2580 break;
2581 if (pobj == NULL && obj == NULL)
2582 break;
2583 if (ch == 'p' && pobj == NULL)
2584 break;
2585 s->done = 1;
2586 thread_err = stop_blame(&s->blame);
2587 s->done = 0;
2588 if (thread_err)
2589 break;
2590 id = got_object_get_id(ch == 'b' ? obj : pobj);
2591 got_object_close(obj);
2592 obj = NULL;
2593 if (pobj) {
2594 got_object_close(pobj);
2595 pobj = NULL;
2597 err = got_object_qid_alloc(&s->blamed_commit, id);
2598 if (err)
2599 goto done;
2600 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2601 s->blamed_commit, entry);
2602 err = run_blame(&s->blame, view, &s->blame_complete,
2603 &s->first_displayed_line, &s->last_displayed_line,
2604 &s->selected_line, &s->done, &s->eof,
2605 s->path, s->blamed_commit->id, s->repo);
2606 if (err)
2607 break;
2608 break;
2610 case 'B': {
2611 struct got_object_qid *first;
2612 first = SIMPLEQ_FIRST(&s->blamed_commits);
2613 if (!got_object_id_cmp(first->id, s->commit_id))
2614 break;
2615 s->done = 1;
2616 thread_err = stop_blame(&s->blame);
2617 s->done = 0;
2618 if (thread_err)
2619 break;
2620 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2621 got_object_qid_free(s->blamed_commit);
2622 s->blamed_commit =
2623 SIMPLEQ_FIRST(&s->blamed_commits);
2624 err = run_blame(&s->blame, view, &s->blame_complete,
2625 &s->first_displayed_line, &s->last_displayed_line,
2626 &s->selected_line, &s->done, &s->eof, s->path,
2627 s->blamed_commit->id, s->repo);
2628 if (err)
2629 break;
2630 break;
2632 case KEY_ENTER:
2633 case '\r':
2634 err = open_selected_commit(&pobj, &obj,
2635 s->blame.lines, s->first_displayed_line,
2636 s->selected_line, s->repo);
2637 if (err)
2638 break;
2639 if (pobj == NULL && obj == NULL)
2640 break;
2642 if (view_is_parent_view(view))
2643 begin_x = view_split_begin_x(view->begin_x);
2644 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2645 if (diff_view == NULL) {
2646 err = got_error_from_errno();
2647 break;
2649 err = open_diff_view(diff_view, pobj, obj, s->repo);
2650 if (err) {
2651 view_close(diff_view);
2652 break;
2654 if (view_is_parent_view(view)) {
2655 err = view_close_child(view);
2656 if (err)
2657 return err;
2658 err = view_set_child(view, diff_view);
2659 if (err) {
2660 view_close(diff_view);
2661 break;
2663 if (!view_is_splitscreen(diff_view)) {
2664 *focus_view = diff_view;
2665 view->child_focussed = 1;
2667 } else
2668 *new_view = diff_view;
2669 if (pobj) {
2670 got_object_close(pobj);
2671 pobj = NULL;
2673 got_object_close(obj);
2674 obj = NULL;
2675 if (err)
2676 break;
2677 break;
2678 case KEY_NPAGE:
2679 case ' ':
2680 if (s->last_displayed_line >= s->blame.nlines &&
2681 s->selected_line < view->nlines - 2) {
2682 s->selected_line = MIN(s->blame.nlines,
2683 view->nlines - 2);
2684 break;
2686 if (s->last_displayed_line + view->nlines - 2
2687 <= s->blame.nlines)
2688 s->first_displayed_line +=
2689 view->nlines - 2;
2690 else
2691 s->first_displayed_line =
2692 s->blame.nlines -
2693 (view->nlines - 3);
2694 break;
2695 case KEY_RESIZE:
2696 if (s->selected_line > view->nlines - 2) {
2697 s->selected_line = MIN(s->blame.nlines,
2698 view->nlines - 2);
2700 break;
2701 default:
2702 break;
2704 done:
2705 if (pobj)
2706 got_object_close(pobj);
2707 return thread_err ? thread_err : err;
2710 static const struct got_error *
2711 cmd_blame(int argc, char *argv[])
2713 const struct got_error *error;
2714 struct got_repository *repo = NULL;
2715 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2716 struct got_object_id *commit_id = NULL;
2717 char *commit_id_str = NULL;
2718 int ch;
2719 struct tog_view *view;
2721 #ifndef PROFILE
2722 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2723 == -1)
2724 err(1, "pledge");
2725 #endif
2727 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2728 switch (ch) {
2729 case 'c':
2730 commit_id_str = optarg;
2731 break;
2732 case 'r':
2733 repo_path = realpath(optarg, NULL);
2734 if (repo_path == NULL)
2735 err(1, "-r option");
2736 break;
2737 default:
2738 usage();
2739 /* NOTREACHED */
2743 argc -= optind;
2744 argv += optind;
2746 if (argc == 1)
2747 path = argv[0];
2748 else
2749 usage_blame();
2751 cwd = getcwd(NULL, 0);
2752 if (cwd == NULL) {
2753 error = got_error_from_errno();
2754 goto done;
2756 if (repo_path == NULL) {
2757 repo_path = strdup(cwd);
2758 if (repo_path == NULL) {
2759 error = got_error_from_errno();
2760 goto done;
2765 error = got_repo_open(&repo, repo_path);
2766 if (error != NULL)
2767 return error;
2769 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2770 if (error != NULL)
2771 goto done;
2773 if (commit_id_str == NULL) {
2774 struct got_reference *head_ref;
2775 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2776 if (error != NULL)
2777 goto done;
2778 error = got_ref_resolve(&commit_id, repo, head_ref);
2779 got_ref_close(head_ref);
2780 } else {
2781 struct got_object *obj;
2782 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2783 if (error != NULL)
2784 goto done;
2785 commit_id = got_object_id_dup(got_object_get_id(obj));
2786 if (commit_id == NULL)
2787 error = got_error_from_errno();
2788 got_object_close(obj);
2790 if (error != NULL)
2791 goto done;
2793 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2794 if (view == NULL) {
2795 error = got_error_from_errno();
2796 goto done;
2798 error = open_blame_view(view, in_repo_path, commit_id, repo);
2799 if (error)
2800 goto done;
2801 error = view_loop(view);
2802 done:
2803 free(repo_path);
2804 free(cwd);
2805 free(commit_id);
2806 if (repo)
2807 got_repo_close(repo);
2808 return error;
2811 static const struct got_error *
2812 draw_tree_entries(struct tog_view *view,
2813 struct got_tree_entry **first_displayed_entry,
2814 struct got_tree_entry **last_displayed_entry,
2815 struct got_tree_entry **selected_entry, int *ndisplayed,
2816 const char *label, int show_ids, const char *parent_path,
2817 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2819 const struct got_error *err = NULL;
2820 struct got_tree_entry *te;
2821 wchar_t *wline;
2822 int width, n;
2824 *ndisplayed = 0;
2826 werase(view->window);
2828 if (limit == 0)
2829 return NULL;
2831 err = format_line(&wline, &width, label, view->ncols);
2832 if (err)
2833 return err;
2834 if (view_needs_focus_indication(view))
2835 wstandout(view->window);
2836 waddwstr(view->window, wline);
2837 if (view_needs_focus_indication(view))
2838 wstandend(view->window);
2839 free(wline);
2840 wline = NULL;
2841 if (width < view->ncols)
2842 waddch(view->window, '\n');
2843 if (--limit <= 0)
2844 return NULL;
2845 err = format_line(&wline, &width, parent_path, view->ncols);
2846 if (err)
2847 return err;
2848 waddwstr(view->window, wline);
2849 free(wline);
2850 wline = NULL;
2851 if (width < view->ncols)
2852 waddch(view->window, '\n');
2853 if (--limit <= 0)
2854 return NULL;
2855 waddch(view->window, '\n');
2856 if (--limit <= 0)
2857 return NULL;
2859 te = SIMPLEQ_FIRST(&entries->head);
2860 if (*first_displayed_entry == NULL) {
2861 if (selected == 0) {
2862 if (view->focussed)
2863 wstandout(view->window);
2864 *selected_entry = NULL;
2866 waddstr(view->window, " ..\n"); /* parent directory */
2867 if (selected == 0 && view->focussed)
2868 wstandend(view->window);
2869 (*ndisplayed)++;
2870 if (--limit <= 0)
2871 return NULL;
2872 n = 1;
2873 } else {
2874 n = 0;
2875 while (te != *first_displayed_entry)
2876 te = SIMPLEQ_NEXT(te, entry);
2879 while (te) {
2880 char *line = NULL, *id_str = NULL;
2882 if (show_ids) {
2883 err = got_object_id_str(&id_str, te->id);
2884 if (err)
2885 return got_error_from_errno();
2887 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2888 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2889 free(id_str);
2890 return got_error_from_errno();
2892 free(id_str);
2893 err = format_line(&wline, &width, line, view->ncols);
2894 if (err) {
2895 free(line);
2896 break;
2898 if (n == selected) {
2899 if (view->focussed)
2900 wstandout(view->window);
2901 *selected_entry = te;
2903 waddwstr(view->window, wline);
2904 if (width < view->ncols)
2905 waddch(view->window, '\n');
2906 if (n == selected && view->focussed)
2907 wstandend(view->window);
2908 free(line);
2909 free(wline);
2910 wline = NULL;
2911 n++;
2912 (*ndisplayed)++;
2913 *last_displayed_entry = te;
2914 if (--limit <= 0)
2915 break;
2916 te = SIMPLEQ_NEXT(te, entry);
2919 return err;
2922 static void
2923 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2924 const struct got_tree_entries *entries, int isroot)
2926 struct got_tree_entry *te, *prev;
2927 int i;
2929 if (*first_displayed_entry == NULL)
2930 return;
2932 te = SIMPLEQ_FIRST(&entries->head);
2933 if (*first_displayed_entry == te) {
2934 if (!isroot)
2935 *first_displayed_entry = NULL;
2936 return;
2939 /* XXX this is stupid... switch to TAILQ? */
2940 for (i = 0; i < maxscroll; i++) {
2941 while (te != *first_displayed_entry) {
2942 prev = te;
2943 te = SIMPLEQ_NEXT(te, entry);
2945 *first_displayed_entry = prev;
2946 te = SIMPLEQ_FIRST(&entries->head);
2948 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2949 *first_displayed_entry = NULL;
2952 static void
2953 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2954 struct got_tree_entry *last_displayed_entry,
2955 const struct got_tree_entries *entries)
2957 struct got_tree_entry *next;
2958 int n = 0;
2960 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2961 return;
2963 if (*first_displayed_entry)
2964 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2965 else
2966 next = SIMPLEQ_FIRST(&entries->head);
2967 while (next) {
2968 *first_displayed_entry = next;
2969 if (++n >= maxscroll)
2970 break;
2971 next = SIMPLEQ_NEXT(next, entry);
2975 static const struct got_error *
2976 tree_entry_path(char **path, struct tog_parent_trees *parents,
2977 struct got_tree_entry *te)
2979 const struct got_error *err = NULL;
2980 struct tog_parent_tree *pt;
2981 size_t len = 2; /* for leading slash and NUL */
2983 TAILQ_FOREACH(pt, parents, entry)
2984 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2985 if (te)
2986 len += strlen(te->name);
2988 *path = calloc(1, len);
2989 if (path == NULL)
2990 return got_error_from_errno();
2992 (*path)[0] = '/';
2993 pt = TAILQ_LAST(parents, tog_parent_trees);
2994 while (pt) {
2995 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2996 err = got_error(GOT_ERR_NO_SPACE);
2997 goto done;
2999 if (strlcat(*path, "/", len) >= len) {
3000 err = got_error(GOT_ERR_NO_SPACE);
3001 goto done;
3003 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3005 if (te) {
3006 if (strlcat(*path, te->name, len) >= len) {
3007 err = got_error(GOT_ERR_NO_SPACE);
3008 goto done;
3011 done:
3012 if (err) {
3013 free(*path);
3014 *path = NULL;
3016 return err;
3019 static const struct got_error *
3020 blame_tree_entry(struct tog_view **new_view, int begin_x,
3021 struct got_tree_entry *te, struct tog_parent_trees *parents,
3022 struct got_object_id *commit_id, struct got_repository *repo)
3024 const struct got_error *err = NULL;
3025 char *path;
3026 struct tog_view *blame_view;
3028 err = tree_entry_path(&path, parents, te);
3029 if (err)
3030 return err;
3032 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3033 if (blame_view == NULL)
3034 return got_error_from_errno();
3036 err = open_blame_view(blame_view, path, commit_id, repo);
3037 if (err) {
3038 view_close(blame_view);
3039 free(path);
3040 } else
3041 *new_view = blame_view;
3042 return err;
3045 static const struct got_error *
3046 log_tree_entry(struct tog_view **new_view, int begin_x,
3047 struct got_tree_entry *te, struct tog_parent_trees *parents,
3048 struct got_object_id *commit_id, struct got_repository *repo)
3050 struct tog_view *log_view;
3051 const struct got_error *err = NULL;
3052 char *path;
3054 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3055 if (log_view == NULL)
3056 return got_error_from_errno();
3058 err = tree_entry_path(&path, parents, te);
3059 if (err)
3060 return err;
3062 err = open_log_view(log_view, commit_id, repo, path, 0);
3063 if (err)
3064 view_close(log_view);
3065 else
3066 *new_view = log_view;
3067 free(path);
3068 return err;
3071 static const struct got_error *
3072 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3073 struct got_object_id *commit_id, struct got_repository *repo)
3075 const struct got_error *err = NULL;
3076 char *commit_id_str = NULL;
3077 struct tog_tree_view_state *s = &view->state.tree;
3079 TAILQ_INIT(&s->parents);
3081 err = got_object_id_str(&commit_id_str, commit_id);
3082 if (err != NULL)
3083 goto done;
3085 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3086 err = got_error_from_errno();
3087 goto done;
3090 s->root = s->tree = root;
3091 s->entries = got_object_tree_get_entries(root);
3092 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3093 s->commit_id = got_object_id_dup(commit_id);
3094 if (s->commit_id == NULL) {
3095 err = got_error_from_errno();
3096 goto done;
3098 s->repo = repo;
3100 view->show = show_tree_view;
3101 view->input = input_tree_view;
3102 view->close = close_tree_view;
3103 done:
3104 free(commit_id_str);
3105 if (err) {
3106 free(s->tree_label);
3107 s->tree_label = NULL;
3109 return err;
3112 static const struct got_error *
3113 close_tree_view(struct tog_view *view)
3115 struct tog_tree_view_state *s = &view->state.tree;
3117 free(s->tree_label);
3118 s->tree_label = NULL;
3119 free(s->commit_id);
3120 s->commit_id = NULL;
3121 while (!TAILQ_EMPTY(&s->parents)) {
3122 struct tog_parent_tree *parent;
3123 parent = TAILQ_FIRST(&s->parents);
3124 TAILQ_REMOVE(&s->parents, parent, entry);
3125 free(parent);
3128 if (s->tree != s->root)
3129 got_object_tree_close(s->tree);
3130 got_object_tree_close(s->root);
3132 return NULL;
3135 static const struct got_error *
3136 show_tree_view(struct tog_view *view)
3138 const struct got_error *err = NULL;
3139 struct tog_tree_view_state *s = &view->state.tree;
3140 char *parent_path;
3142 err = tree_entry_path(&parent_path, &s->parents, NULL);
3143 if (err)
3144 return err;
3146 err = draw_tree_entries(view, &s->first_displayed_entry,
3147 &s->last_displayed_entry, &s->selected_entry,
3148 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3149 s->entries, s->selected, view->nlines, s->tree == s->root);
3150 free(parent_path);
3152 view_vborder(view);
3153 return err;
3156 static const struct got_error *
3157 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3158 struct tog_view **focus_view, struct tog_view *view, int ch)
3160 const struct got_error *err = NULL;
3161 struct tog_tree_view_state *s = &view->state.tree;
3162 struct tog_view *log_view;
3163 int begin_x = 0;
3165 switch (ch) {
3166 case 'i':
3167 s->show_ids = !s->show_ids;
3168 break;
3169 case 'l':
3170 if (!s->selected_entry)
3171 break;
3172 if (view_is_parent_view(view))
3173 begin_x = view_split_begin_x(view->begin_x);
3174 err = log_tree_entry(&log_view, begin_x,
3175 s->selected_entry, &s->parents,
3176 s->commit_id, s->repo);
3177 if (view_is_parent_view(view)) {
3178 err = view_close_child(view);
3179 if (err)
3180 return err;
3181 err = view_set_child(view, log_view);
3182 if (err) {
3183 view_close(log_view);
3184 break;
3186 *focus_view = log_view;
3187 view->child_focussed = 1;
3188 } else
3189 *new_view = log_view;
3190 break;
3191 case 'k':
3192 case KEY_UP:
3193 if (s->selected > 0)
3194 s->selected--;
3195 if (s->selected > 0)
3196 break;
3197 tree_scroll_up(&s->first_displayed_entry, 1,
3198 s->entries, s->tree == s->root);
3199 break;
3200 case KEY_PPAGE:
3201 if (SIMPLEQ_FIRST(&s->entries->head) ==
3202 s->first_displayed_entry) {
3203 if (s->tree != s->root)
3204 s->first_displayed_entry = NULL;
3205 s->selected = 0;
3206 break;
3208 tree_scroll_up(&s->first_displayed_entry,
3209 view->nlines, s->entries,
3210 s->tree == s->root);
3211 break;
3212 case 'j':
3213 case KEY_DOWN:
3214 if (s->selected < s->ndisplayed - 1) {
3215 s->selected++;
3216 break;
3218 tree_scroll_down(&s->first_displayed_entry, 1,
3219 s->last_displayed_entry, s->entries);
3220 break;
3221 case KEY_NPAGE:
3222 tree_scroll_down(&s->first_displayed_entry,
3223 view->nlines, s->last_displayed_entry,
3224 s->entries);
3225 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3226 entry))
3227 break;
3228 /* can't scroll any further; move cursor down */
3229 if (s->selected < s->ndisplayed - 1)
3230 s->selected = s->ndisplayed - 1;
3231 break;
3232 case KEY_ENTER:
3233 case '\r':
3234 if (s->selected_entry == NULL) {
3235 struct tog_parent_tree *parent;
3236 case KEY_BACKSPACE:
3237 /* user selected '..' */
3238 if (s->tree == s->root)
3239 break;
3240 parent = TAILQ_FIRST(&s->parents);
3241 TAILQ_REMOVE(&s->parents, parent,
3242 entry);
3243 got_object_tree_close(s->tree);
3244 s->tree = parent->tree;
3245 s->entries =
3246 got_object_tree_get_entries(s->tree);
3247 s->first_displayed_entry =
3248 parent->first_displayed_entry;
3249 s->selected_entry =
3250 parent->selected_entry;
3251 s->selected = parent->selected;
3252 free(parent);
3253 } else if (S_ISDIR(s->selected_entry->mode)) {
3254 struct tog_parent_tree *parent;
3255 struct got_tree_object *child;
3256 err = got_object_open_as_tree(&child,
3257 s->repo, s->selected_entry->id);
3258 if (err)
3259 break;
3260 parent = calloc(1, sizeof(*parent));
3261 if (parent == NULL) {
3262 err = got_error_from_errno();
3263 break;
3265 parent->tree = s->tree;
3266 parent->first_displayed_entry =
3267 s->first_displayed_entry;
3268 parent->selected_entry = s->selected_entry;
3269 parent->selected = s->selected;
3270 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3271 s->tree = child;
3272 s->entries =
3273 got_object_tree_get_entries(s->tree);
3274 s->selected = 0;
3275 s->first_displayed_entry = NULL;
3276 } else if (S_ISREG(s->selected_entry->mode)) {
3277 struct tog_view *blame_view;
3278 int begin_x = view_is_parent_view(view) ?
3279 view_split_begin_x(view->begin_x) : 0;
3281 err = blame_tree_entry(&blame_view, begin_x,
3282 s->selected_entry, &s->parents, s->commit_id,
3283 s->repo);
3284 if (err)
3285 break;
3286 if (view_is_parent_view(view)) {
3287 err = view_close_child(view);
3288 if (err)
3289 return err;
3290 err = view_set_child(view, blame_view);
3291 if (err) {
3292 view_close(blame_view);
3293 break;
3295 *focus_view = blame_view;
3296 view->child_focussed = 1;
3297 } else
3298 *new_view = blame_view;
3300 break;
3301 case KEY_RESIZE:
3302 if (s->selected > view->nlines)
3303 s->selected = s->ndisplayed - 1;
3304 break;
3305 default:
3306 break;
3309 return err;
3312 __dead static void
3313 usage_tree(void)
3315 endwin();
3316 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3317 getprogname());
3318 exit(1);
3321 static const struct got_error *
3322 cmd_tree(int argc, char *argv[])
3324 const struct got_error *error;
3325 struct got_repository *repo = NULL;
3326 char *repo_path = NULL;
3327 struct got_object_id *commit_id = NULL;
3328 char *commit_id_arg = NULL;
3329 struct got_commit_object *commit = NULL;
3330 struct got_tree_object *tree = NULL;
3331 int ch;
3332 struct tog_view *view;
3334 #ifndef PROFILE
3335 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3336 == -1)
3337 err(1, "pledge");
3338 #endif
3340 while ((ch = getopt(argc, argv, "c:")) != -1) {
3341 switch (ch) {
3342 case 'c':
3343 commit_id_arg = optarg;
3344 break;
3345 default:
3346 usage();
3347 /* NOTREACHED */
3351 argc -= optind;
3352 argv += optind;
3354 if (argc == 0) {
3355 repo_path = getcwd(NULL, 0);
3356 if (repo_path == NULL)
3357 return got_error_from_errno();
3358 } else if (argc == 1) {
3359 repo_path = realpath(argv[0], NULL);
3360 if (repo_path == NULL)
3361 return got_error_from_errno();
3362 } else
3363 usage_log();
3365 error = got_repo_open(&repo, repo_path);
3366 free(repo_path);
3367 if (error != NULL)
3368 return error;
3370 if (commit_id_arg == NULL) {
3371 error = get_head_commit_id(&commit_id, repo);
3372 if (error != NULL)
3373 goto done;
3374 } else {
3375 struct got_object *obj;
3376 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3377 if (error == NULL) {
3378 commit_id = got_object_id_dup(got_object_get_id(obj));
3379 if (commit_id == NULL)
3380 error = got_error_from_errno();
3383 if (error != NULL)
3384 goto done;
3386 error = got_object_open_as_commit(&commit, repo, commit_id);
3387 if (error != NULL)
3388 goto done;
3390 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3391 if (error != NULL)
3392 goto done;
3394 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3395 if (view == NULL) {
3396 error = got_error_from_errno();
3397 goto done;
3399 error = open_tree_view(view, tree, commit_id, repo);
3400 if (error)
3401 goto done;
3402 error = view_loop(view);
3403 done:
3404 free(commit_id);
3405 if (commit)
3406 got_object_commit_close(commit);
3407 if (tree)
3408 got_object_tree_close(tree);
3409 if (repo)
3410 got_repo_close(repo);
3411 return error;
3414 static void
3415 init_curses(void)
3417 initscr();
3418 cbreak();
3419 halfdelay(1); /* Do fast refresh while initial view is loading. */
3420 noecho();
3421 nonl();
3422 intrflush(stdscr, FALSE);
3423 keypad(stdscr, TRUE);
3424 curs_set(0);
3425 signal(SIGWINCH, tog_sigwinch);
3428 __dead static void
3429 usage(void)
3431 int i;
3433 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3434 "Available commands:\n", getprogname());
3435 for (i = 0; i < nitems(tog_commands); i++) {
3436 struct tog_cmd *cmd = &tog_commands[i];
3437 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3439 exit(1);
3442 static char **
3443 make_argv(const char *arg0, const char *arg1)
3445 char **argv;
3446 int argc = (arg1 == NULL ? 1 : 2);
3448 argv = calloc(argc, sizeof(char *));
3449 if (argv == NULL)
3450 err(1, "calloc");
3451 argv[0] = strdup(arg0);
3452 if (argv[0] == NULL)
3453 err(1, "calloc");
3454 if (arg1) {
3455 argv[1] = strdup(arg1);
3456 if (argv[1] == NULL)
3457 err(1, "calloc");
3460 return argv;
3463 int
3464 main(int argc, char *argv[])
3466 const struct got_error *error = NULL;
3467 struct tog_cmd *cmd = NULL;
3468 int ch, hflag = 0;
3469 char **cmd_argv = NULL;
3471 setlocale(LC_ALL, "");
3473 while ((ch = getopt(argc, argv, "h")) != -1) {
3474 switch (ch) {
3475 case 'h':
3476 hflag = 1;
3477 break;
3478 default:
3479 usage();
3480 /* NOTREACHED */
3484 argc -= optind;
3485 argv += optind;
3486 optind = 0;
3487 optreset = 1;
3489 if (argc == 0) {
3490 if (hflag)
3491 usage();
3492 /* Build an argument vector which runs a default command. */
3493 cmd = &tog_commands[0];
3494 cmd_argv = make_argv(cmd->name, NULL);
3495 argc = 1;
3496 } else {
3497 int i;
3499 /* Did the user specific a command? */
3500 for (i = 0; i < nitems(tog_commands); i++) {
3501 if (strncmp(tog_commands[i].name, argv[0],
3502 strlen(argv[0])) == 0) {
3503 cmd = &tog_commands[i];
3504 if (hflag)
3505 tog_commands[i].cmd_usage();
3506 break;
3509 if (cmd == NULL) {
3510 /* Did the user specify a repository? */
3511 char *repo_path = realpath(argv[0], NULL);
3512 if (repo_path) {
3513 struct got_repository *repo;
3514 error = got_repo_open(&repo, repo_path);
3515 if (error == NULL)
3516 got_repo_close(repo);
3517 } else
3518 error = got_error_from_errno();
3519 if (error) {
3520 if (hflag) {
3521 fprintf(stderr, "%s: '%s' is not a "
3522 "known command\n", getprogname(),
3523 argv[0]);
3524 usage();
3526 fprintf(stderr, "%s: '%s' is neither a known "
3527 "command nor a path to a repository\n",
3528 getprogname(), argv[0]);
3529 free(repo_path);
3530 return 1;
3532 cmd = &tog_commands[0];
3533 cmd_argv = make_argv(cmd->name, repo_path);
3534 argc = 2;
3535 free(repo_path);
3539 init_curses();
3541 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3542 if (error)
3543 goto done;
3544 done:
3545 endwin();
3546 free(cmd_argv);
3547 if (error)
3548 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3549 return 0;