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 if (!view_is_splitscreen(diff_view)) {
1567 *focus_view = diff_view;
1568 view->child_focussed = 1;
1570 } else
1571 *new_view = diff_view;
1572 break;
1573 case 't':
1574 if (s->selected_entry == NULL)
1575 break;
1576 if (view_is_parent_view(view))
1577 begin_x = view_split_begin_x(view->begin_x);
1578 err = browse_commit(&tree_view, begin_x,
1579 s->selected_entry, s->repo);
1580 if (err)
1581 break;
1582 if (view_is_parent_view(view)) {
1583 err = view_close_child(view);
1584 if (err)
1585 return err;
1586 err = view_set_child(view, tree_view);
1587 if (err) {
1588 view_close(tree_view);
1589 break;
1591 *focus_view = tree_view;
1592 view->child_focussed = 1;
1593 } else
1594 *new_view = tree_view;
1595 break;
1596 case KEY_BACKSPACE:
1597 if (strcmp(s->in_repo_path, "/") == 0)
1598 break;
1599 parent_path = dirname(s->in_repo_path);
1600 if (parent_path && strcmp(parent_path, ".") != 0) {
1601 struct tog_view *lv;
1602 err = stop_log_thread(s);
1603 if (err)
1604 return err;
1605 lv = view_open(view->nlines, view->ncols,
1606 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1607 if (lv == NULL)
1608 return got_error_from_errno();
1609 err = open_log_view(lv, s->start_id, s->repo,
1610 parent_path, 0);
1611 if (err)
1612 return err;;
1613 if (view_is_parent_view(view))
1614 *new_view = lv;
1615 else {
1616 view_set_child(view->parent, lv);
1617 *focus_view = lv;
1619 return NULL;
1621 break;
1622 default:
1623 break;
1626 return err;
1629 static const struct got_error *
1630 cmd_log(int argc, char *argv[])
1632 const struct got_error *error;
1633 struct got_repository *repo = NULL;
1634 struct got_object_id *start_id = NULL;
1635 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1636 char *start_commit = NULL;
1637 int ch;
1638 struct tog_view *view;
1640 #ifndef PROFILE
1641 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1642 == -1)
1643 err(1, "pledge");
1644 #endif
1646 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1647 switch (ch) {
1648 case 'c':
1649 start_commit = optarg;
1650 break;
1651 case 'r':
1652 repo_path = realpath(optarg, NULL);
1653 if (repo_path == NULL)
1654 err(1, "-r option");
1655 break;
1656 default:
1657 usage();
1658 /* NOTREACHED */
1662 argc -= optind;
1663 argv += optind;
1665 if (argc == 0)
1666 path = strdup("");
1667 else if (argc == 1)
1668 path = strdup(argv[0]);
1669 else
1670 usage_log();
1671 if (path == NULL)
1672 return got_error_from_errno();
1674 cwd = getcwd(NULL, 0);
1675 if (cwd == NULL) {
1676 error = got_error_from_errno();
1677 goto done;
1679 if (repo_path == NULL) {
1680 repo_path = strdup(cwd);
1681 if (repo_path == NULL) {
1682 error = got_error_from_errno();
1683 goto done;
1687 error = got_repo_open(&repo, repo_path);
1688 if (error != NULL)
1689 goto done;
1691 if (start_commit == NULL) {
1692 error = get_head_commit_id(&start_id, repo);
1693 if (error != NULL)
1694 goto done;
1695 } else {
1696 struct got_object *obj;
1697 error = got_object_open_by_id_str(&obj, repo, start_commit);
1698 if (error == NULL) {
1699 start_id = got_object_id_dup(got_object_get_id(obj));
1700 if (start_id == NULL)
1701 error = got_error_from_errno();
1702 goto done;
1705 if (error != NULL)
1706 goto done;
1708 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1709 if (view == NULL) {
1710 error = got_error_from_errno();
1711 goto done;
1713 error = open_log_view(view, start_id, repo, path, 1);
1714 if (error)
1715 goto done;
1716 error = view_loop(view);
1717 done:
1718 free(repo_path);
1719 free(cwd);
1720 free(path);
1721 free(start_id);
1722 if (repo)
1723 got_repo_close(repo);
1724 return error;
1727 __dead static void
1728 usage_diff(void)
1730 endwin();
1731 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1732 getprogname());
1733 exit(1);
1736 static char *
1737 parse_next_line(FILE *f, size_t *len)
1739 char *line;
1740 size_t linelen;
1741 size_t lineno;
1742 const char delim[3] = { '\0', '\0', '\0'};
1744 line = fparseln(f, &linelen, &lineno, delim, 0);
1745 if (len)
1746 *len = linelen;
1747 return line;
1750 static const struct got_error *
1751 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1752 int *last_displayed_line, int *eof, int max_lines,
1753 char * header)
1755 const struct got_error *err;
1756 int nlines = 0, nprinted = 0;
1757 char *line;
1758 size_t len;
1759 wchar_t *wline;
1760 int width;
1762 rewind(f);
1763 werase(view->window);
1765 if (header) {
1766 err = format_line(&wline, &width, header, view->ncols);
1767 if (err) {
1768 return err;
1771 if (view_needs_focus_indication(view))
1772 wstandout(view->window);
1773 waddwstr(view->window, wline);
1774 if (view_needs_focus_indication(view))
1775 wstandend(view->window);
1776 if (width < view->ncols)
1777 waddch(view->window, '\n');
1779 if (max_lines <= 1)
1780 return NULL;
1781 max_lines--;
1784 *eof = 0;
1785 while (nprinted < max_lines) {
1786 line = parse_next_line(f, &len);
1787 if (line == NULL) {
1788 *eof = 1;
1789 break;
1791 if (++nlines < *first_displayed_line) {
1792 free(line);
1793 continue;
1796 err = format_line(&wline, &width, line, view->ncols);
1797 if (err) {
1798 free(line);
1799 return err;
1801 waddwstr(view->window, wline);
1802 if (width < view->ncols)
1803 waddch(view->window, '\n');
1804 if (++nprinted == 1)
1805 *first_displayed_line = nlines;
1806 free(line);
1807 free(wline);
1808 wline = NULL;
1810 *last_displayed_line = nlines;
1812 view_vborder(view);
1814 return NULL;
1817 static const struct got_error *
1818 create_diff(struct tog_diff_view_state *s)
1820 const struct got_error *err = NULL;
1821 struct got_object *obj1 = NULL, *obj2 = NULL;
1822 FILE *f = NULL;
1824 if (s->id1) {
1825 err = got_object_open(&obj1, s->repo, s->id1);
1826 if (err)
1827 return err;
1830 err = got_object_open(&obj2, s->repo, s->id2);
1831 if (err)
1832 goto done;
1834 f = got_opentemp();
1835 if (f == NULL) {
1836 err = got_error_from_errno();
1837 goto done;
1839 if (s->f)
1840 fclose(s->f);
1841 s->f = f;
1843 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1844 case GOT_OBJ_TYPE_BLOB:
1845 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1846 s->diff_context, s->repo, f);
1847 break;
1848 case GOT_OBJ_TYPE_TREE:
1849 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1850 s->diff_context, s->repo, f);
1851 break;
1852 case GOT_OBJ_TYPE_COMMIT:
1853 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1854 s->repo, f);
1855 break;
1856 default:
1857 err = got_error(GOT_ERR_OBJ_TYPE);
1858 break;
1860 done:
1861 if (obj1)
1862 got_object_close(obj1);
1863 got_object_close(obj2);
1864 if (f)
1865 fflush(f);
1866 return err;
1869 static const struct got_error *
1870 open_diff_view(struct tog_view *view, struct got_object *obj1,
1871 struct got_object *obj2, struct got_repository *repo)
1873 const struct got_error *err;
1875 if (obj1 != NULL && obj2 != NULL &&
1876 got_object_get_type(obj1) != got_object_get_type(obj2))
1877 return got_error(GOT_ERR_OBJ_TYPE);
1879 if (obj1) {
1880 struct got_object_id *id1;
1881 id1 = got_object_id_dup(got_object_get_id(obj1));
1882 if (id1 == NULL)
1883 return got_error_from_errno();
1884 view->state.diff.id1 = id1;
1885 } else
1886 view->state.diff.id1 = NULL;
1888 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1889 if (view->state.diff.id2 == NULL) {
1890 free(view->state.diff.id1);
1891 view->state.diff.id1 = NULL;
1892 return got_error_from_errno();
1894 view->state.diff.f = NULL;
1895 view->state.diff.first_displayed_line = 1;
1896 view->state.diff.last_displayed_line = view->nlines;
1897 view->state.diff.diff_context = 3;
1898 view->state.diff.repo = repo;
1900 err = create_diff(&view->state.diff);
1901 if (err) {
1902 free(view->state.diff.id1);
1903 view->state.diff.id1 = NULL;
1904 free(view->state.diff.id2);
1905 view->state.diff.id2 = NULL;
1906 return err;
1909 view->show = show_diff_view;
1910 view->input = input_diff_view;
1911 view->close = close_diff_view;
1913 return NULL;
1916 static const struct got_error *
1917 close_diff_view(struct tog_view *view)
1919 const struct got_error *err = NULL;
1921 free(view->state.diff.id1);
1922 view->state.diff.id1 = NULL;
1923 free(view->state.diff.id2);
1924 view->state.diff.id2 = NULL;
1925 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1926 err = got_error_from_errno();
1927 return err;
1930 static const struct got_error *
1931 show_diff_view(struct tog_view *view)
1933 const struct got_error *err;
1934 struct tog_diff_view_state *s = &view->state.diff;
1935 char *id_str1 = NULL, *id_str2, *header;
1937 if (s->id1) {
1938 err = got_object_id_str(&id_str1, s->id1);
1939 if (err)
1940 return err;
1942 err = got_object_id_str(&id_str2, s->id2);
1943 if (err)
1944 return err;
1946 if (asprintf(&header, "diff: %s %s",
1947 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1948 err = got_error_from_errno();
1949 free(id_str1);
1950 free(id_str2);
1951 return err;
1953 free(id_str1);
1954 free(id_str2);
1956 return draw_file(view, s->f, &s->first_displayed_line,
1957 &s->last_displayed_line, &s->eof, view->nlines,
1958 header);
1961 static const struct got_error *
1962 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1963 struct tog_view **focus_view, struct tog_view *view, int ch)
1965 const struct got_error *err = NULL;
1966 struct tog_diff_view_state *s = &view->state.diff;
1967 int i;
1969 switch (ch) {
1970 case 'k':
1971 case KEY_UP:
1972 if (s->first_displayed_line > 1)
1973 s->first_displayed_line--;
1974 break;
1975 case KEY_PPAGE:
1976 i = 0;
1977 while (i++ < view->nlines - 1 &&
1978 s->first_displayed_line > 1)
1979 s->first_displayed_line--;
1980 break;
1981 case 'j':
1982 case KEY_DOWN:
1983 if (!s->eof)
1984 s->first_displayed_line++;
1985 break;
1986 case KEY_NPAGE:
1987 case ' ':
1988 i = 0;
1989 while (!s->eof && i++ < view->nlines - 1) {
1990 char *line;
1991 line = parse_next_line(s->f, NULL);
1992 s->first_displayed_line++;
1993 if (line == NULL)
1994 break;
1996 break;
1997 case '[':
1998 if (s->diff_context > 0) {
1999 s->diff_context--;
2000 err = create_diff(s);
2002 break;
2003 case ']':
2004 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2005 s->diff_context++;
2006 err = create_diff(s);
2008 break;
2009 default:
2010 break;
2013 return err;
2016 static const struct got_error *
2017 cmd_diff(int argc, char *argv[])
2019 const struct got_error *error = NULL;
2020 struct got_repository *repo = NULL;
2021 struct got_object *obj1 = NULL, *obj2 = NULL;
2022 char *repo_path = NULL;
2023 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
2024 int ch;
2025 struct tog_view *view;
2027 #ifndef PROFILE
2028 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2029 == -1)
2030 err(1, "pledge");
2031 #endif
2033 while ((ch = getopt(argc, argv, "")) != -1) {
2034 switch (ch) {
2035 default:
2036 usage();
2037 /* NOTREACHED */
2041 argc -= optind;
2042 argv += optind;
2044 if (argc == 0) {
2045 usage_diff(); /* TODO show local worktree changes */
2046 } else if (argc == 2) {
2047 repo_path = getcwd(NULL, 0);
2048 if (repo_path == NULL)
2049 return got_error_from_errno();
2050 obj_id_str1 = argv[0];
2051 obj_id_str2 = argv[1];
2052 } else if (argc == 3) {
2053 repo_path = realpath(argv[0], NULL);
2054 if (repo_path == NULL)
2055 return got_error_from_errno();
2056 obj_id_str1 = argv[1];
2057 obj_id_str2 = argv[2];
2058 } else
2059 usage_diff();
2061 error = got_repo_open(&repo, repo_path);
2062 free(repo_path);
2063 if (error)
2064 goto done;
2066 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2067 if (error)
2068 goto done;
2070 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2071 if (error)
2072 goto done;
2074 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2075 if (view == NULL) {
2076 error = got_error_from_errno();
2077 goto done;
2079 error = open_diff_view(view, obj1, obj2, repo);
2080 if (error)
2081 goto done;
2082 error = view_loop(view);
2083 done:
2084 got_repo_close(repo);
2085 if (obj1)
2086 got_object_close(obj1);
2087 if (obj2)
2088 got_object_close(obj2);
2089 return error;
2092 __dead static void
2093 usage_blame(void)
2095 endwin();
2096 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2097 getprogname());
2098 exit(1);
2101 struct tog_blame_line {
2102 int annotated;
2103 struct got_object_id *id;
2106 static const struct got_error *
2107 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2108 const char *path, struct tog_blame_line *lines, int nlines,
2109 int blame_complete, int selected_line, int *first_displayed_line,
2110 int *last_displayed_line, int *eof, int max_lines)
2112 const struct got_error *err;
2113 int lineno = 0, nprinted = 0;
2114 char *line;
2115 size_t len;
2116 wchar_t *wline;
2117 int width, wlimit;
2118 struct tog_blame_line *blame_line;
2119 struct got_object_id *prev_id = NULL;
2120 char *id_str;
2122 err = got_object_id_str(&id_str, id);
2123 if (err)
2124 return err;
2126 rewind(f);
2127 werase(view->window);
2129 if (asprintf(&line, "commit: %s", id_str) == -1) {
2130 err = got_error_from_errno();
2131 free(id_str);
2132 return err;
2135 err = format_line(&wline, &width, line, view->ncols);
2136 free(line);
2137 line = NULL;
2138 if (view_needs_focus_indication(view))
2139 wstandout(view->window);
2140 waddwstr(view->window, wline);
2141 if (view_needs_focus_indication(view))
2142 wstandend(view->window);
2143 free(wline);
2144 wline = NULL;
2145 if (width < view->ncols)
2146 waddch(view->window, '\n');
2148 if (asprintf(&line, "[%d/%d] %s%s",
2149 *first_displayed_line - 1 + selected_line, nlines,
2150 blame_complete ? "" : "annotating ", path) == -1) {
2151 free(id_str);
2152 return got_error_from_errno();
2154 free(id_str);
2155 err = format_line(&wline, &width, line, view->ncols);
2156 free(line);
2157 line = NULL;
2158 if (err)
2159 return err;
2160 waddwstr(view->window, wline);
2161 free(wline);
2162 wline = NULL;
2163 if (width < view->ncols)
2164 waddch(view->window, '\n');
2166 *eof = 0;
2167 while (nprinted < max_lines - 2) {
2168 line = parse_next_line(f, &len);
2169 if (line == NULL) {
2170 *eof = 1;
2171 break;
2173 if (++lineno < *first_displayed_line) {
2174 free(line);
2175 continue;
2178 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2179 err = format_line(&wline, &width, line, wlimit);
2180 if (err) {
2181 free(line);
2182 return err;
2185 if (view->focussed && nprinted == selected_line - 1)
2186 wstandout(view->window);
2188 blame_line = &lines[lineno - 1];
2189 if (blame_line->annotated && prev_id &&
2190 got_object_id_cmp(prev_id, blame_line->id) == 0)
2191 waddstr(view->window, " ");
2192 else if (blame_line->annotated) {
2193 char *id_str;
2194 err = got_object_id_str(&id_str, blame_line->id);
2195 if (err) {
2196 free(line);
2197 free(wline);
2198 return err;
2200 wprintw(view->window, "%.8s ", id_str);
2201 free(id_str);
2202 prev_id = blame_line->id;
2203 } else {
2204 waddstr(view->window, "........ ");
2205 prev_id = NULL;
2208 waddwstr(view->window, wline);
2209 while (width < wlimit) {
2210 waddch(view->window, ' ');
2211 width++;
2213 if (view->focussed && nprinted == selected_line - 1)
2214 wstandend(view->window);
2215 if (++nprinted == 1)
2216 *first_displayed_line = lineno;
2217 free(line);
2218 free(wline);
2219 wline = NULL;
2221 *last_displayed_line = lineno;
2223 view_vborder(view);
2225 return NULL;
2228 static const struct got_error *
2229 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2231 const struct got_error *err = NULL;
2232 struct tog_blame_cb_args *a = arg;
2233 struct tog_blame_line *line;
2234 int errcode;
2236 if (nlines != a->nlines ||
2237 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2238 return got_error(GOT_ERR_RANGE);
2240 errcode = pthread_mutex_lock(&tog_mutex);
2241 if (errcode)
2242 return got_error_set_errno(errcode);
2244 if (*a->quit) { /* user has quit the blame view */
2245 err = got_error(GOT_ERR_ITER_COMPLETED);
2246 goto done;
2249 if (lineno == -1)
2250 goto done; /* no change in this commit */
2252 line = &a->lines[lineno - 1];
2253 if (line->annotated)
2254 goto done;
2256 line->id = got_object_id_dup(id);
2257 if (line->id == NULL) {
2258 err = got_error_from_errno();
2259 goto done;
2261 line->annotated = 1;
2262 done:
2263 errcode = pthread_mutex_unlock(&tog_mutex);
2264 if (errcode)
2265 err = got_error_set_errno(errcode);
2266 return err;
2269 static void *
2270 blame_thread(void *arg)
2272 const struct got_error *err;
2273 struct tog_blame_thread_args *ta = arg;
2274 struct tog_blame_cb_args *a = ta->cb_args;
2275 int errcode;
2277 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2278 blame_cb, ta->cb_args);
2280 errcode = pthread_mutex_lock(&tog_mutex);
2281 if (errcode)
2282 return (void *)got_error_set_errno(errcode);
2284 got_repo_close(ta->repo);
2285 ta->repo = NULL;
2286 *ta->complete = 1;
2288 errcode = pthread_mutex_unlock(&tog_mutex);
2289 if (errcode && err == NULL)
2290 err = got_error_set_errno(errcode);
2292 return (void *)err;
2295 static struct got_object_id *
2296 get_selected_commit_id(struct tog_blame_line *lines,
2297 int first_displayed_line, int selected_line)
2299 struct tog_blame_line *line;
2301 line = &lines[first_displayed_line - 1 + selected_line - 1];
2302 if (!line->annotated)
2303 return NULL;
2305 return line->id;
2308 static const struct got_error *
2309 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2310 struct tog_blame_line *lines, int first_displayed_line,
2311 int selected_line, struct got_repository *repo)
2313 const struct got_error *err = NULL;
2314 struct got_commit_object *commit = NULL;
2315 struct got_object_id *selected_id;
2316 struct got_object_qid *pid;
2318 *pobj = NULL;
2319 *obj = NULL;
2321 selected_id = get_selected_commit_id(lines,
2322 first_displayed_line, selected_line);
2323 if (selected_id == NULL)
2324 return NULL;
2326 err = got_object_open(obj, repo, selected_id);
2327 if (err)
2328 goto done;
2330 err = got_object_commit_open(&commit, repo, *obj);
2331 if (err)
2332 goto done;
2334 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2335 if (pid) {
2336 err = got_object_open(pobj, repo, pid->id);
2337 if (err)
2338 goto done;
2340 done:
2341 if (commit)
2342 got_object_commit_close(commit);
2343 return err;
2346 static const struct got_error *
2347 stop_blame(struct tog_blame *blame)
2349 const struct got_error *err = NULL;
2350 int i;
2352 if (blame->thread) {
2353 int errcode;
2354 errcode = pthread_mutex_unlock(&tog_mutex);
2355 if (errcode)
2356 return got_error_set_errno(errcode);
2357 errcode = pthread_join(blame->thread, (void **)&err);
2358 if (errcode)
2359 return got_error_set_errno(errcode);
2360 errcode = pthread_mutex_lock(&tog_mutex);
2361 if (errcode)
2362 return got_error_set_errno(errcode);
2363 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2364 err = NULL;
2365 blame->thread = NULL;
2367 if (blame->thread_args.repo) {
2368 got_repo_close(blame->thread_args.repo);
2369 blame->thread_args.repo = NULL;
2371 if (blame->f) {
2372 fclose(blame->f);
2373 blame->f = NULL;
2375 for (i = 0; i < blame->nlines; i++)
2376 free(blame->lines[i].id);
2377 free(blame->lines);
2378 blame->lines = NULL;
2379 free(blame->cb_args.commit_id);
2380 blame->cb_args.commit_id = NULL;
2382 return err;
2385 static const struct got_error *
2386 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2387 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2388 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2389 struct got_repository *repo)
2391 const struct got_error *err = NULL;
2392 struct got_blob_object *blob = NULL;
2393 struct got_repository *thread_repo = NULL;
2394 struct got_object_id *obj_id = NULL;
2395 struct got_object *obj = NULL;
2397 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2398 if (err)
2399 goto done;
2401 err = got_object_open(&obj, repo, obj_id);
2402 if (err)
2403 goto done;
2405 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2406 err = got_error(GOT_ERR_OBJ_TYPE);
2407 goto done;
2410 err = got_object_blob_open(&blob, repo, obj, 8192);
2411 if (err)
2412 goto done;
2413 blame->f = got_opentemp();
2414 if (blame->f == NULL) {
2415 err = got_error_from_errno();
2416 goto done;
2418 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2419 blame->f, blob);
2420 if (err)
2421 goto done;
2423 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2424 if (blame->lines == NULL) {
2425 err = got_error_from_errno();
2426 goto done;
2429 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2430 if (err)
2431 goto done;
2433 blame->cb_args.view = view;
2434 blame->cb_args.lines = blame->lines;
2435 blame->cb_args.nlines = blame->nlines;
2436 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2437 if (blame->cb_args.commit_id == NULL) {
2438 err = got_error_from_errno();
2439 goto done;
2441 blame->cb_args.quit = done;
2443 blame->thread_args.path = path;
2444 blame->thread_args.repo = thread_repo;
2445 blame->thread_args.cb_args = &blame->cb_args;
2446 blame->thread_args.complete = blame_complete;
2447 *blame_complete = 0;
2449 done:
2450 if (blob)
2451 got_object_blob_close(blob);
2452 free(obj_id);
2453 if (obj)
2454 got_object_close(obj);
2455 if (err)
2456 stop_blame(blame);
2457 return err;
2460 static const struct got_error *
2461 open_blame_view(struct tog_view *view, char *path,
2462 struct got_object_id *commit_id, struct got_repository *repo)
2464 const struct got_error *err = NULL;
2465 struct tog_blame_view_state *s = &view->state.blame;
2467 SIMPLEQ_INIT(&s->blamed_commits);
2469 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2470 if (err)
2471 return err;
2473 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2474 s->first_displayed_line = 1;
2475 s->last_displayed_line = view->nlines;
2476 s->selected_line = 1;
2477 s->blame_complete = 0;
2478 s->path = path;
2479 if (s->path == NULL)
2480 return got_error_from_errno();
2481 s->repo = repo;
2482 s->commit_id = commit_id;
2483 memset(&s->blame, 0, sizeof(s->blame));
2485 view->show = show_blame_view;
2486 view->input = input_blame_view;
2487 view->close = close_blame_view;
2489 return run_blame(&s->blame, view, &s->blame_complete,
2490 &s->first_displayed_line, &s->last_displayed_line,
2491 &s->selected_line, &s->done, &s->eof, s->path,
2492 s->blamed_commit->id, s->repo);
2495 static const struct got_error *
2496 close_blame_view(struct tog_view *view)
2498 const struct got_error *err = NULL;
2499 struct tog_blame_view_state *s = &view->state.blame;
2501 if (s->blame.thread)
2502 err = stop_blame(&s->blame);
2504 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2505 struct got_object_qid *blamed_commit;
2506 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2507 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2508 got_object_qid_free(blamed_commit);
2511 free(s->path);
2513 return err;
2516 static const struct got_error *
2517 show_blame_view(struct tog_view *view)
2519 const struct got_error *err = NULL;
2520 struct tog_blame_view_state *s = &view->state.blame;
2521 int errcode;
2523 if (s->blame.thread == NULL) {
2524 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2525 &s->blame.thread_args);
2526 if (errcode)
2527 return got_error_set_errno(errcode);
2530 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2531 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2532 s->selected_line, &s->first_displayed_line,
2533 &s->last_displayed_line, &s->eof, view->nlines);
2535 view_vborder(view);
2536 return err;
2539 static const struct got_error *
2540 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2541 struct tog_view **focus_view, struct tog_view *view, int ch)
2543 const struct got_error *err = NULL, *thread_err = NULL;
2544 struct got_object *obj = NULL, *pobj = NULL;
2545 struct tog_view *diff_view;
2546 struct tog_blame_view_state *s = &view->state.blame;
2547 int begin_x = 0;
2549 switch (ch) {
2550 case 'q':
2551 s->done = 1;
2552 break;
2553 case 'k':
2554 case KEY_UP:
2555 if (s->selected_line > 1)
2556 s->selected_line--;
2557 else if (s->selected_line == 1 &&
2558 s->first_displayed_line > 1)
2559 s->first_displayed_line--;
2560 break;
2561 case KEY_PPAGE:
2562 if (s->first_displayed_line == 1) {
2563 s->selected_line = 1;
2564 break;
2566 if (s->first_displayed_line > view->nlines - 2)
2567 s->first_displayed_line -=
2568 (view->nlines - 2);
2569 else
2570 s->first_displayed_line = 1;
2571 break;
2572 case 'j':
2573 case KEY_DOWN:
2574 if (s->selected_line < view->nlines - 2 &&
2575 s->first_displayed_line +
2576 s->selected_line <= s->blame.nlines)
2577 s->selected_line++;
2578 else if (s->last_displayed_line <
2579 s->blame.nlines)
2580 s->first_displayed_line++;
2581 break;
2582 case 'b':
2583 case 'p': {
2584 struct got_object_id *id;
2585 id = get_selected_commit_id(s->blame.lines,
2586 s->first_displayed_line, s->selected_line);
2587 if (id == NULL || got_object_id_cmp(id,
2588 s->blamed_commit->id) == 0)
2589 break;
2590 err = open_selected_commit(&pobj, &obj,
2591 s->blame.lines, s->first_displayed_line,
2592 s->selected_line, s->repo);
2593 if (err)
2594 break;
2595 if (pobj == NULL && obj == NULL)
2596 break;
2597 if (ch == 'p' && pobj == NULL)
2598 break;
2599 s->done = 1;
2600 thread_err = stop_blame(&s->blame);
2601 s->done = 0;
2602 if (thread_err)
2603 break;
2604 id = got_object_get_id(ch == 'b' ? obj : pobj);
2605 got_object_close(obj);
2606 obj = NULL;
2607 if (pobj) {
2608 got_object_close(pobj);
2609 pobj = NULL;
2611 err = got_object_qid_alloc(&s->blamed_commit, id);
2612 if (err)
2613 goto done;
2614 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2615 s->blamed_commit, entry);
2616 err = run_blame(&s->blame, view, &s->blame_complete,
2617 &s->first_displayed_line, &s->last_displayed_line,
2618 &s->selected_line, &s->done, &s->eof,
2619 s->path, s->blamed_commit->id, s->repo);
2620 if (err)
2621 break;
2622 break;
2624 case 'B': {
2625 struct got_object_qid *first;
2626 first = SIMPLEQ_FIRST(&s->blamed_commits);
2627 if (!got_object_id_cmp(first->id, s->commit_id))
2628 break;
2629 s->done = 1;
2630 thread_err = stop_blame(&s->blame);
2631 s->done = 0;
2632 if (thread_err)
2633 break;
2634 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2635 got_object_qid_free(s->blamed_commit);
2636 s->blamed_commit =
2637 SIMPLEQ_FIRST(&s->blamed_commits);
2638 err = run_blame(&s->blame, view, &s->blame_complete,
2639 &s->first_displayed_line, &s->last_displayed_line,
2640 &s->selected_line, &s->done, &s->eof, s->path,
2641 s->blamed_commit->id, s->repo);
2642 if (err)
2643 break;
2644 break;
2646 case KEY_ENTER:
2647 case '\r':
2648 err = open_selected_commit(&pobj, &obj,
2649 s->blame.lines, s->first_displayed_line,
2650 s->selected_line, s->repo);
2651 if (err)
2652 break;
2653 if (pobj == NULL && obj == NULL)
2654 break;
2656 if (view_is_parent_view(view))
2657 begin_x = view_split_begin_x(view->begin_x);
2658 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2659 if (diff_view == NULL) {
2660 err = got_error_from_errno();
2661 break;
2663 err = open_diff_view(diff_view, pobj, obj, s->repo);
2664 if (err) {
2665 view_close(diff_view);
2666 break;
2668 if (view_is_parent_view(view)) {
2669 err = view_close_child(view);
2670 if (err)
2671 return err;
2672 err = view_set_child(view, diff_view);
2673 if (err) {
2674 view_close(diff_view);
2675 break;
2677 if (!view_is_splitscreen(diff_view)) {
2678 *focus_view = diff_view;
2679 view->child_focussed = 1;
2681 } else
2682 *new_view = diff_view;
2683 if (pobj) {
2684 got_object_close(pobj);
2685 pobj = NULL;
2687 got_object_close(obj);
2688 obj = NULL;
2689 if (err)
2690 break;
2691 break;
2692 case KEY_NPAGE:
2693 case ' ':
2694 if (s->last_displayed_line >= s->blame.nlines &&
2695 s->selected_line < view->nlines - 2) {
2696 s->selected_line = MIN(s->blame.nlines,
2697 view->nlines - 2);
2698 break;
2700 if (s->last_displayed_line + view->nlines - 2
2701 <= s->blame.nlines)
2702 s->first_displayed_line +=
2703 view->nlines - 2;
2704 else
2705 s->first_displayed_line =
2706 s->blame.nlines -
2707 (view->nlines - 3);
2708 break;
2709 case KEY_RESIZE:
2710 if (s->selected_line > view->nlines - 2) {
2711 s->selected_line = MIN(s->blame.nlines,
2712 view->nlines - 2);
2714 break;
2715 default:
2716 break;
2718 done:
2719 if (pobj)
2720 got_object_close(pobj);
2721 return thread_err ? thread_err : err;
2724 static const struct got_error *
2725 cmd_blame(int argc, char *argv[])
2727 const struct got_error *error;
2728 struct got_repository *repo = NULL;
2729 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2730 struct got_object_id *commit_id = NULL;
2731 char *commit_id_str = NULL;
2732 int ch;
2733 struct tog_view *view;
2735 #ifndef PROFILE
2736 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2737 == -1)
2738 err(1, "pledge");
2739 #endif
2741 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2742 switch (ch) {
2743 case 'c':
2744 commit_id_str = optarg;
2745 break;
2746 case 'r':
2747 repo_path = realpath(optarg, NULL);
2748 if (repo_path == NULL)
2749 err(1, "-r option");
2750 break;
2751 default:
2752 usage();
2753 /* NOTREACHED */
2757 argc -= optind;
2758 argv += optind;
2760 if (argc == 1)
2761 path = argv[0];
2762 else
2763 usage_blame();
2765 cwd = getcwd(NULL, 0);
2766 if (cwd == NULL) {
2767 error = got_error_from_errno();
2768 goto done;
2770 if (repo_path == NULL) {
2771 repo_path = strdup(cwd);
2772 if (repo_path == NULL) {
2773 error = got_error_from_errno();
2774 goto done;
2779 error = got_repo_open(&repo, repo_path);
2780 if (error != NULL)
2781 return error;
2783 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2784 if (error != NULL)
2785 goto done;
2787 if (commit_id_str == NULL) {
2788 struct got_reference *head_ref;
2789 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2790 if (error != NULL)
2791 goto done;
2792 error = got_ref_resolve(&commit_id, repo, head_ref);
2793 got_ref_close(head_ref);
2794 } else {
2795 struct got_object *obj;
2796 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2797 if (error != NULL)
2798 goto done;
2799 commit_id = got_object_id_dup(got_object_get_id(obj));
2800 if (commit_id == NULL)
2801 error = got_error_from_errno();
2802 got_object_close(obj);
2804 if (error != NULL)
2805 goto done;
2807 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2808 if (view == NULL) {
2809 error = got_error_from_errno();
2810 goto done;
2812 error = open_blame_view(view, in_repo_path, commit_id, repo);
2813 if (error)
2814 goto done;
2815 error = view_loop(view);
2816 done:
2817 free(repo_path);
2818 free(cwd);
2819 free(commit_id);
2820 if (repo)
2821 got_repo_close(repo);
2822 return error;
2825 static const struct got_error *
2826 draw_tree_entries(struct tog_view *view,
2827 struct got_tree_entry **first_displayed_entry,
2828 struct got_tree_entry **last_displayed_entry,
2829 struct got_tree_entry **selected_entry, int *ndisplayed,
2830 const char *label, int show_ids, const char *parent_path,
2831 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2833 const struct got_error *err = NULL;
2834 struct got_tree_entry *te;
2835 wchar_t *wline;
2836 int width, n;
2838 *ndisplayed = 0;
2840 werase(view->window);
2842 if (limit == 0)
2843 return NULL;
2845 err = format_line(&wline, &width, label, view->ncols);
2846 if (err)
2847 return err;
2848 if (view_needs_focus_indication(view))
2849 wstandout(view->window);
2850 waddwstr(view->window, wline);
2851 if (view_needs_focus_indication(view))
2852 wstandend(view->window);
2853 free(wline);
2854 wline = NULL;
2855 if (width < view->ncols)
2856 waddch(view->window, '\n');
2857 if (--limit <= 0)
2858 return NULL;
2859 err = format_line(&wline, &width, parent_path, view->ncols);
2860 if (err)
2861 return err;
2862 waddwstr(view->window, wline);
2863 free(wline);
2864 wline = NULL;
2865 if (width < view->ncols)
2866 waddch(view->window, '\n');
2867 if (--limit <= 0)
2868 return NULL;
2869 waddch(view->window, '\n');
2870 if (--limit <= 0)
2871 return NULL;
2873 te = SIMPLEQ_FIRST(&entries->head);
2874 if (*first_displayed_entry == NULL) {
2875 if (selected == 0) {
2876 if (view->focussed)
2877 wstandout(view->window);
2878 *selected_entry = NULL;
2880 waddstr(view->window, " ..\n"); /* parent directory */
2881 if (selected == 0 && view->focussed)
2882 wstandend(view->window);
2883 (*ndisplayed)++;
2884 if (--limit <= 0)
2885 return NULL;
2886 n = 1;
2887 } else {
2888 n = 0;
2889 while (te != *first_displayed_entry)
2890 te = SIMPLEQ_NEXT(te, entry);
2893 while (te) {
2894 char *line = NULL, *id_str = NULL;
2896 if (show_ids) {
2897 err = got_object_id_str(&id_str, te->id);
2898 if (err)
2899 return got_error_from_errno();
2901 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2902 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2903 free(id_str);
2904 return got_error_from_errno();
2906 free(id_str);
2907 err = format_line(&wline, &width, line, view->ncols);
2908 if (err) {
2909 free(line);
2910 break;
2912 if (n == selected) {
2913 if (view->focussed)
2914 wstandout(view->window);
2915 *selected_entry = te;
2917 waddwstr(view->window, wline);
2918 if (width < view->ncols)
2919 waddch(view->window, '\n');
2920 if (n == selected && view->focussed)
2921 wstandend(view->window);
2922 free(line);
2923 free(wline);
2924 wline = NULL;
2925 n++;
2926 (*ndisplayed)++;
2927 *last_displayed_entry = te;
2928 if (--limit <= 0)
2929 break;
2930 te = SIMPLEQ_NEXT(te, entry);
2933 return err;
2936 static void
2937 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2938 const struct got_tree_entries *entries, int isroot)
2940 struct got_tree_entry *te, *prev;
2941 int i;
2943 if (*first_displayed_entry == NULL)
2944 return;
2946 te = SIMPLEQ_FIRST(&entries->head);
2947 if (*first_displayed_entry == te) {
2948 if (!isroot)
2949 *first_displayed_entry = NULL;
2950 return;
2953 /* XXX this is stupid... switch to TAILQ? */
2954 for (i = 0; i < maxscroll; i++) {
2955 while (te != *first_displayed_entry) {
2956 prev = te;
2957 te = SIMPLEQ_NEXT(te, entry);
2959 *first_displayed_entry = prev;
2960 te = SIMPLEQ_FIRST(&entries->head);
2962 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2963 *first_displayed_entry = NULL;
2966 static void
2967 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2968 struct got_tree_entry *last_displayed_entry,
2969 const struct got_tree_entries *entries)
2971 struct got_tree_entry *next;
2972 int n = 0;
2974 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2975 return;
2977 if (*first_displayed_entry)
2978 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2979 else
2980 next = SIMPLEQ_FIRST(&entries->head);
2981 while (next) {
2982 *first_displayed_entry = next;
2983 if (++n >= maxscroll)
2984 break;
2985 next = SIMPLEQ_NEXT(next, entry);
2989 static const struct got_error *
2990 tree_entry_path(char **path, struct tog_parent_trees *parents,
2991 struct got_tree_entry *te)
2993 const struct got_error *err = NULL;
2994 struct tog_parent_tree *pt;
2995 size_t len = 2; /* for leading slash and NUL */
2997 TAILQ_FOREACH(pt, parents, entry)
2998 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2999 if (te)
3000 len += strlen(te->name);
3002 *path = calloc(1, len);
3003 if (path == NULL)
3004 return got_error_from_errno();
3006 (*path)[0] = '/';
3007 pt = TAILQ_LAST(parents, tog_parent_trees);
3008 while (pt) {
3009 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3010 err = got_error(GOT_ERR_NO_SPACE);
3011 goto done;
3013 if (strlcat(*path, "/", len) >= len) {
3014 err = got_error(GOT_ERR_NO_SPACE);
3015 goto done;
3017 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3019 if (te) {
3020 if (strlcat(*path, te->name, len) >= len) {
3021 err = got_error(GOT_ERR_NO_SPACE);
3022 goto done;
3025 done:
3026 if (err) {
3027 free(*path);
3028 *path = NULL;
3030 return err;
3033 static const struct got_error *
3034 blame_tree_entry(struct tog_view **new_view, int begin_x,
3035 struct got_tree_entry *te, struct tog_parent_trees *parents,
3036 struct got_object_id *commit_id, struct got_repository *repo)
3038 const struct got_error *err = NULL;
3039 char *path;
3040 struct tog_view *blame_view;
3042 err = tree_entry_path(&path, parents, te);
3043 if (err)
3044 return err;
3046 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3047 if (blame_view == NULL)
3048 return got_error_from_errno();
3050 err = open_blame_view(blame_view, path, commit_id, repo);
3051 if (err) {
3052 view_close(blame_view);
3053 free(path);
3054 } else
3055 *new_view = blame_view;
3056 return err;
3059 static const struct got_error *
3060 log_tree_entry(struct tog_view **new_view, int begin_x,
3061 struct got_tree_entry *te, struct tog_parent_trees *parents,
3062 struct got_object_id *commit_id, struct got_repository *repo)
3064 struct tog_view *log_view;
3065 const struct got_error *err = NULL;
3066 char *path;
3068 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3069 if (log_view == NULL)
3070 return got_error_from_errno();
3072 err = tree_entry_path(&path, parents, te);
3073 if (err)
3074 return err;
3076 err = open_log_view(log_view, commit_id, repo, path, 0);
3077 if (err)
3078 view_close(log_view);
3079 else
3080 *new_view = log_view;
3081 free(path);
3082 return err;
3085 static const struct got_error *
3086 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3087 struct got_object_id *commit_id, struct got_repository *repo)
3089 const struct got_error *err = NULL;
3090 char *commit_id_str = NULL;
3091 struct tog_tree_view_state *s = &view->state.tree;
3093 TAILQ_INIT(&s->parents);
3095 err = got_object_id_str(&commit_id_str, commit_id);
3096 if (err != NULL)
3097 goto done;
3099 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3100 err = got_error_from_errno();
3101 goto done;
3104 s->root = s->tree = root;
3105 s->entries = got_object_tree_get_entries(root);
3106 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3107 s->commit_id = got_object_id_dup(commit_id);
3108 if (s->commit_id == NULL) {
3109 err = got_error_from_errno();
3110 goto done;
3112 s->repo = repo;
3114 view->show = show_tree_view;
3115 view->input = input_tree_view;
3116 view->close = close_tree_view;
3117 done:
3118 free(commit_id_str);
3119 if (err) {
3120 free(s->tree_label);
3121 s->tree_label = NULL;
3123 return err;
3126 static const struct got_error *
3127 close_tree_view(struct tog_view *view)
3129 struct tog_tree_view_state *s = &view->state.tree;
3131 free(s->tree_label);
3132 s->tree_label = NULL;
3133 free(s->commit_id);
3134 s->commit_id = NULL;
3135 while (!TAILQ_EMPTY(&s->parents)) {
3136 struct tog_parent_tree *parent;
3137 parent = TAILQ_FIRST(&s->parents);
3138 TAILQ_REMOVE(&s->parents, parent, entry);
3139 free(parent);
3142 if (s->tree != s->root)
3143 got_object_tree_close(s->tree);
3144 got_object_tree_close(s->root);
3146 return NULL;
3149 static const struct got_error *
3150 show_tree_view(struct tog_view *view)
3152 const struct got_error *err = NULL;
3153 struct tog_tree_view_state *s = &view->state.tree;
3154 char *parent_path;
3156 err = tree_entry_path(&parent_path, &s->parents, NULL);
3157 if (err)
3158 return err;
3160 err = draw_tree_entries(view, &s->first_displayed_entry,
3161 &s->last_displayed_entry, &s->selected_entry,
3162 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3163 s->entries, s->selected, view->nlines, s->tree == s->root);
3164 free(parent_path);
3166 view_vborder(view);
3167 return err;
3170 static const struct got_error *
3171 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3172 struct tog_view **focus_view, struct tog_view *view, int ch)
3174 const struct got_error *err = NULL;
3175 struct tog_tree_view_state *s = &view->state.tree;
3176 struct tog_view *log_view;
3177 int begin_x = 0;
3179 switch (ch) {
3180 case 'i':
3181 s->show_ids = !s->show_ids;
3182 break;
3183 case 'l':
3184 if (!s->selected_entry)
3185 break;
3186 if (view_is_parent_view(view))
3187 begin_x = view_split_begin_x(view->begin_x);
3188 err = log_tree_entry(&log_view, begin_x,
3189 s->selected_entry, &s->parents,
3190 s->commit_id, s->repo);
3191 if (view_is_parent_view(view)) {
3192 err = view_close_child(view);
3193 if (err)
3194 return err;
3195 err = view_set_child(view, log_view);
3196 if (err) {
3197 view_close(log_view);
3198 break;
3200 *focus_view = log_view;
3201 view->child_focussed = 1;
3202 } else
3203 *new_view = log_view;
3204 break;
3205 case 'k':
3206 case KEY_UP:
3207 if (s->selected > 0)
3208 s->selected--;
3209 if (s->selected > 0)
3210 break;
3211 tree_scroll_up(&s->first_displayed_entry, 1,
3212 s->entries, s->tree == s->root);
3213 break;
3214 case KEY_PPAGE:
3215 s->selected = 0;
3216 if (SIMPLEQ_FIRST(&s->entries->head) ==
3217 s->first_displayed_entry) {
3218 if (s->tree != s->root)
3219 s->first_displayed_entry = NULL;
3220 break;
3222 tree_scroll_up(&s->first_displayed_entry,
3223 view->nlines, s->entries,
3224 s->tree == s->root);
3225 break;
3226 case 'j':
3227 case KEY_DOWN:
3228 if (s->selected < s->ndisplayed - 1) {
3229 s->selected++;
3230 break;
3232 tree_scroll_down(&s->first_displayed_entry, 1,
3233 s->last_displayed_entry, s->entries);
3234 break;
3235 case KEY_NPAGE:
3236 tree_scroll_down(&s->first_displayed_entry,
3237 view->nlines, s->last_displayed_entry,
3238 s->entries);
3239 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3240 entry))
3241 break;
3242 /* can't scroll any further; move cursor down */
3243 if (s->selected < s->ndisplayed - 1)
3244 s->selected = s->ndisplayed - 1;
3245 break;
3246 case KEY_ENTER:
3247 case '\r':
3248 if (s->selected_entry == NULL) {
3249 struct tog_parent_tree *parent;
3250 case KEY_BACKSPACE:
3251 /* user selected '..' */
3252 if (s->tree == s->root)
3253 break;
3254 parent = TAILQ_FIRST(&s->parents);
3255 TAILQ_REMOVE(&s->parents, parent,
3256 entry);
3257 got_object_tree_close(s->tree);
3258 s->tree = parent->tree;
3259 s->entries =
3260 got_object_tree_get_entries(s->tree);
3261 s->first_displayed_entry =
3262 parent->first_displayed_entry;
3263 s->selected_entry =
3264 parent->selected_entry;
3265 s->selected = parent->selected;
3266 free(parent);
3267 } else if (S_ISDIR(s->selected_entry->mode)) {
3268 struct tog_parent_tree *parent;
3269 struct got_tree_object *child;
3270 err = got_object_open_as_tree(&child,
3271 s->repo, s->selected_entry->id);
3272 if (err)
3273 break;
3274 parent = calloc(1, sizeof(*parent));
3275 if (parent == NULL) {
3276 err = got_error_from_errno();
3277 break;
3279 parent->tree = s->tree;
3280 parent->first_displayed_entry =
3281 s->first_displayed_entry;
3282 parent->selected_entry = s->selected_entry;
3283 parent->selected = s->selected;
3284 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3285 s->tree = child;
3286 s->entries =
3287 got_object_tree_get_entries(s->tree);
3288 s->selected = 0;
3289 s->first_displayed_entry = NULL;
3290 } else if (S_ISREG(s->selected_entry->mode)) {
3291 struct tog_view *blame_view;
3292 int begin_x = view_is_parent_view(view) ?
3293 view_split_begin_x(view->begin_x) : 0;
3295 err = blame_tree_entry(&blame_view, begin_x,
3296 s->selected_entry, &s->parents, s->commit_id,
3297 s->repo);
3298 if (err)
3299 break;
3300 if (view_is_parent_view(view)) {
3301 err = view_close_child(view);
3302 if (err)
3303 return err;
3304 err = view_set_child(view, blame_view);
3305 if (err) {
3306 view_close(blame_view);
3307 break;
3309 *focus_view = blame_view;
3310 view->child_focussed = 1;
3311 } else
3312 *new_view = blame_view;
3314 break;
3315 case KEY_RESIZE:
3316 if (s->selected > view->nlines)
3317 s->selected = s->ndisplayed - 1;
3318 break;
3319 default:
3320 break;
3323 return err;
3326 __dead static void
3327 usage_tree(void)
3329 endwin();
3330 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3331 getprogname());
3332 exit(1);
3335 static const struct got_error *
3336 cmd_tree(int argc, char *argv[])
3338 const struct got_error *error;
3339 struct got_repository *repo = NULL;
3340 char *repo_path = NULL;
3341 struct got_object_id *commit_id = NULL;
3342 char *commit_id_arg = NULL;
3343 struct got_commit_object *commit = NULL;
3344 struct got_tree_object *tree = NULL;
3345 int ch;
3346 struct tog_view *view;
3348 #ifndef PROFILE
3349 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3350 == -1)
3351 err(1, "pledge");
3352 #endif
3354 while ((ch = getopt(argc, argv, "c:")) != -1) {
3355 switch (ch) {
3356 case 'c':
3357 commit_id_arg = optarg;
3358 break;
3359 default:
3360 usage();
3361 /* NOTREACHED */
3365 argc -= optind;
3366 argv += optind;
3368 if (argc == 0) {
3369 repo_path = getcwd(NULL, 0);
3370 if (repo_path == NULL)
3371 return got_error_from_errno();
3372 } else if (argc == 1) {
3373 repo_path = realpath(argv[0], NULL);
3374 if (repo_path == NULL)
3375 return got_error_from_errno();
3376 } else
3377 usage_log();
3379 error = got_repo_open(&repo, repo_path);
3380 free(repo_path);
3381 if (error != NULL)
3382 return error;
3384 if (commit_id_arg == NULL) {
3385 error = get_head_commit_id(&commit_id, repo);
3386 if (error != NULL)
3387 goto done;
3388 } else {
3389 struct got_object *obj;
3390 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3391 if (error == NULL) {
3392 commit_id = got_object_id_dup(got_object_get_id(obj));
3393 if (commit_id == NULL)
3394 error = got_error_from_errno();
3397 if (error != NULL)
3398 goto done;
3400 error = got_object_open_as_commit(&commit, repo, commit_id);
3401 if (error != NULL)
3402 goto done;
3404 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3405 if (error != NULL)
3406 goto done;
3408 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3409 if (view == NULL) {
3410 error = got_error_from_errno();
3411 goto done;
3413 error = open_tree_view(view, tree, commit_id, repo);
3414 if (error)
3415 goto done;
3416 error = view_loop(view);
3417 done:
3418 free(commit_id);
3419 if (commit)
3420 got_object_commit_close(commit);
3421 if (tree)
3422 got_object_tree_close(tree);
3423 if (repo)
3424 got_repo_close(repo);
3425 return error;
3428 static void
3429 init_curses(void)
3431 initscr();
3432 cbreak();
3433 halfdelay(1); /* Do fast refresh while initial view is loading. */
3434 noecho();
3435 nonl();
3436 intrflush(stdscr, FALSE);
3437 keypad(stdscr, TRUE);
3438 curs_set(0);
3439 signal(SIGWINCH, tog_sigwinch);
3442 __dead static void
3443 usage(void)
3445 int i;
3447 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3448 "Available commands:\n", getprogname());
3449 for (i = 0; i < nitems(tog_commands); i++) {
3450 struct tog_cmd *cmd = &tog_commands[i];
3451 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3453 exit(1);
3456 static char **
3457 make_argv(const char *arg0, const char *arg1)
3459 char **argv;
3460 int argc = (arg1 == NULL ? 1 : 2);
3462 argv = calloc(argc, sizeof(char *));
3463 if (argv == NULL)
3464 err(1, "calloc");
3465 argv[0] = strdup(arg0);
3466 if (argv[0] == NULL)
3467 err(1, "calloc");
3468 if (arg1) {
3469 argv[1] = strdup(arg1);
3470 if (argv[1] == NULL)
3471 err(1, "calloc");
3474 return argv;
3477 int
3478 main(int argc, char *argv[])
3480 const struct got_error *error = NULL;
3481 struct tog_cmd *cmd = NULL;
3482 int ch, hflag = 0;
3483 char **cmd_argv = NULL;
3485 setlocale(LC_ALL, "");
3487 while ((ch = getopt(argc, argv, "h")) != -1) {
3488 switch (ch) {
3489 case 'h':
3490 hflag = 1;
3491 break;
3492 default:
3493 usage();
3494 /* NOTREACHED */
3498 argc -= optind;
3499 argv += optind;
3500 optind = 0;
3501 optreset = 1;
3503 if (argc == 0) {
3504 if (hflag)
3505 usage();
3506 /* Build an argument vector which runs a default command. */
3507 cmd = &tog_commands[0];
3508 cmd_argv = make_argv(cmd->name, NULL);
3509 argc = 1;
3510 } else {
3511 int i;
3513 /* Did the user specific a command? */
3514 for (i = 0; i < nitems(tog_commands); i++) {
3515 if (strncmp(tog_commands[i].name, argv[0],
3516 strlen(argv[0])) == 0) {
3517 cmd = &tog_commands[i];
3518 if (hflag)
3519 tog_commands[i].cmd_usage();
3520 break;
3523 if (cmd == NULL) {
3524 /* Did the user specify a repository? */
3525 char *repo_path = realpath(argv[0], NULL);
3526 if (repo_path) {
3527 struct got_repository *repo;
3528 error = got_repo_open(&repo, repo_path);
3529 if (error == NULL)
3530 got_repo_close(repo);
3531 } else
3532 error = got_error_from_errno();
3533 if (error) {
3534 if (hflag) {
3535 fprintf(stderr, "%s: '%s' is not a "
3536 "known command\n", getprogname(),
3537 argv[0]);
3538 usage();
3540 fprintf(stderr, "%s: '%s' is neither a known "
3541 "command nor a path to a repository\n",
3542 getprogname(), argv[0]);
3543 free(repo_path);
3544 return 1;
3546 cmd = &tog_commands[0];
3547 cmd_argv = make_argv(cmd->name, repo_path);
3548 argc = 2;
3549 free(repo_path);
3553 init_curses();
3555 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3556 if (error)
3557 goto done;
3558 done:
3559 endwin();
3560 free(cmd_argv);
3561 if (error)
3562 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3563 return 0;