Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
50 #ifndef MIN
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 #endif
54 #ifndef MAX
55 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
56 #endif
59 #ifndef nitems
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 #endif
63 struct tog_cmd {
64 const char *name;
65 const struct got_error *(*cmd_main)(int, char *[]);
66 void (*cmd_usage)(void);
67 const char *descr;
68 };
70 __dead static void usage(void);
71 __dead static void usage_log(void);
72 __dead static void usage_diff(void);
73 __dead static void usage_blame(void);
74 __dead static void usage_tree(void);
76 static const struct got_error* cmd_log(int, char *[]);
77 static const struct got_error* cmd_diff(int, char *[]);
78 static const struct got_error* cmd_blame(int, char *[]);
79 static const struct got_error* cmd_tree(int, char *[]);
81 static struct tog_cmd tog_commands[] = {
82 { "log", cmd_log, usage_log,
83 "show repository history" },
84 { "diff", cmd_diff, usage_diff,
85 "compare files and directories" },
86 { "blame", cmd_blame, usage_blame,
87 "show line-by-line file history" },
88 { "tree", cmd_tree, usage_tree,
89 "browse trees in repository" },
90 };
92 enum tog_view_type {
93 TOG_VIEW_DIFF,
94 TOG_VIEW_LOG,
95 TOG_VIEW_BLAME,
96 TOG_VIEW_TREE
97 };
99 struct tog_diff_view_state {
100 struct got_object_id *id1, *id2;
101 FILE *f;
102 int first_displayed_line;
103 int last_displayed_line;
104 int eof;
105 int diff_context;
106 struct got_repository *repo;
107 };
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
113 int idx;
114 };
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
117 int ncommits;
118 struct commit_queue_head head;
119 };
121 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
123 struct tog_log_thread_args {
124 pthread_cond_t need_commits;
125 int commits_needed;
126 struct got_commit_graph *graph;
127 struct commit_queue *commits;
128 const char *in_repo_path;
129 struct got_object_id *start_id;
130 struct got_repository *repo;
131 int log_complete;
132 sig_atomic_t *quit;
133 struct tog_view *view;
134 struct commit_queue_entry **first_displayed_entry;
135 struct commit_queue_entry **selected_entry;
136 };
138 struct tog_log_view_state {
139 struct commit_queue commits;
140 struct commit_queue_entry *first_displayed_entry;
141 struct commit_queue_entry *last_displayed_entry;
142 struct commit_queue_entry *selected_entry;
143 int selected;
144 char *in_repo_path;
145 struct got_repository *repo;
146 struct got_object_id *start_id;
147 sig_atomic_t quit;
148 pthread_t thread;
149 struct tog_log_thread_args thread_args;
150 };
152 struct tog_blame_cb_args {
153 struct tog_blame_line *lines; /* one per line */
154 int nlines;
156 struct tog_view *view;
157 struct got_object_id *commit_id;
158 int *quit;
159 };
161 struct tog_blame_thread_args {
162 const char *path;
163 struct got_repository *repo;
164 struct tog_blame_cb_args *cb_args;
165 int *complete;
166 };
168 struct tog_blame {
169 FILE *f;
170 size_t filesize;
171 struct tog_blame_line *lines;
172 int nlines;
173 pthread_t thread;
174 struct tog_blame_thread_args thread_args;
175 struct tog_blame_cb_args cb_args;
176 const char *path;
177 };
179 struct tog_blame_view_state {
180 int first_displayed_line;
181 int last_displayed_line;
182 int selected_line;
183 int blame_complete;
184 int eof;
185 int done;
186 struct got_object_id_queue blamed_commits;
187 struct got_object_qid *blamed_commit;
188 char *path;
189 struct got_repository *repo;
190 struct got_object_id *commit_id;
191 struct tog_blame blame;
192 };
194 struct tog_parent_tree {
195 TAILQ_ENTRY(tog_parent_tree) entry;
196 struct got_tree_object *tree;
197 struct got_tree_entry *first_displayed_entry;
198 struct got_tree_entry *selected_entry;
199 int selected;
200 };
202 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
204 struct tog_tree_view_state {
205 char *tree_label;
206 struct got_tree_object *root;
207 struct got_tree_object *tree;
208 const struct got_tree_entries *entries;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *last_displayed_entry;
211 struct got_tree_entry *selected_entry;
212 int nentries, ndisplayed, selected, show_ids;
213 struct tog_parent_trees parents;
214 struct got_object_id *commit_id;
215 struct got_repository *repo;
216 };
218 /*
219 * We implement two types of views: parent views and child views.
221 * The 'Tab' key switches between a parent view and its child view.
222 * Child views are shown side-by-side to their parent view, provided
223 * there is enough screen estate.
225 * When a new view is opened from within a parent view, this new view
226 * becomes a child view of the parent view, replacing any existing child.
228 * When a new view is opened from within a child view, this new view
229 * becomes a parent view which will obscure the views below until the
230 * user quits the new parent view by typing 'q'.
232 * This list of views contains parent views only.
233 * Child views are only pointed to by their parent view.
234 */
235 TAILQ_HEAD(tog_view_list_head, tog_view);
237 struct tog_view {
238 TAILQ_ENTRY(tog_view) entry;
239 WINDOW *window;
240 PANEL *panel;
241 int nlines, ncols, begin_y, begin_x;
242 int lines, cols; /* copies of LINES and COLS */
243 int focussed;
244 struct tog_view *parent;
245 struct tog_view *child;
246 int child_focussed;
248 /* type-specific state */
249 enum tog_view_type type;
250 union {
251 struct tog_diff_view_state diff;
252 struct tog_log_view_state log;
253 struct tog_blame_view_state blame;
254 struct tog_tree_view_state tree;
255 } state;
257 const struct got_error *(*show)(struct tog_view *);
258 const struct got_error *(*input)(struct tog_view **,
259 struct tog_view **, struct tog_view**, struct tog_view *, int);
260 const struct got_error *(*close)(struct tog_view *);
261 };
263 static const struct got_error *open_diff_view(struct tog_view *,
264 struct got_object_id *, struct got_object_id *, struct got_repository *);
265 static const struct got_error *show_diff_view(struct tog_view *);
266 static const struct got_error *input_diff_view(struct tog_view **,
267 struct tog_view **, struct tog_view **, struct tog_view *, int);
268 static const struct got_error* close_diff_view(struct tog_view *);
270 static const struct got_error *open_log_view(struct tog_view *,
271 struct got_object_id *, struct got_repository *, const char *, int);
272 static const struct got_error * show_log_view(struct tog_view *);
273 static const struct got_error *input_log_view(struct tog_view **,
274 struct tog_view **, struct tog_view **, struct tog_view *, int);
275 static const struct got_error *close_log_view(struct tog_view *);
277 static const struct got_error *open_blame_view(struct tog_view *, char *,
278 struct got_object_id *, struct got_repository *);
279 static const struct got_error *show_blame_view(struct tog_view *);
280 static const struct got_error *input_blame_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error *close_blame_view(struct tog_view *);
284 static const struct got_error *open_tree_view(struct tog_view *,
285 struct got_tree_object *, struct got_object_id *, struct got_repository *);
286 static const struct got_error *show_tree_view(struct tog_view *);
287 static const struct got_error *input_tree_view(struct tog_view **,
288 struct tog_view **, struct tog_view **, struct tog_view *, int);
289 static const struct got_error *close_tree_view(struct tog_view *);
291 static volatile sig_atomic_t tog_sigwinch_received;
293 static void
294 tog_sigwinch(int signo)
296 tog_sigwinch_received = 1;
299 static const struct got_error *
300 view_close(struct tog_view *view)
302 const struct got_error *err = NULL;
304 if (view->child) {
305 view_close(view->child);
306 view->child = NULL;
308 if (view->close)
309 err = view->close(view);
310 if (view->panel)
311 del_panel(view->panel);
312 if (view->window)
313 delwin(view->window);
314 free(view);
315 return err;
318 static struct tog_view *
319 view_open(int nlines, int ncols, int begin_y, int begin_x,
320 enum tog_view_type type)
322 struct tog_view *view = calloc(1, sizeof(*view));
324 if (view == NULL)
325 return NULL;
327 view->type = type;
328 view->lines = LINES;
329 view->cols = COLS;
330 view->nlines = nlines ? nlines : LINES - begin_y;
331 view->ncols = ncols ? ncols : COLS - begin_x;
332 view->begin_y = begin_y;
333 view->begin_x = begin_x;
334 view->window = newwin(nlines, ncols, begin_y, begin_x);
335 if (view->window == NULL) {
336 view_close(view);
337 return NULL;
339 view->panel = new_panel(view->window);
340 if (view->panel == NULL ||
341 set_panel_userptr(view->panel, view) != OK) {
342 view_close(view);
343 return NULL;
346 keypad(view->window, TRUE);
347 return view;
350 static int
351 view_split_begin_x(int begin_x)
353 if (begin_x > 0 || COLS < 120)
354 return 0;
355 return (COLS - MAX(COLS / 2, 80));
358 static const struct got_error *view_resize(struct tog_view *);
360 static const struct got_error *
361 view_splitscreen(struct tog_view *view)
363 const struct got_error *err = NULL;
365 view->begin_y = 0;
366 view->begin_x = view_split_begin_x(0);
367 view->nlines = LINES;
368 view->ncols = COLS - view->begin_x;
369 view->lines = LINES;
370 view->cols = COLS;
371 err = view_resize(view);
372 if (err)
373 return err;
375 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
376 return got_error_from_errno();
378 return NULL;
381 static const struct got_error *
382 view_fullscreen(struct tog_view *view)
384 const struct got_error *err = NULL;
386 view->begin_x = 0;
387 view->begin_y = 0;
388 view->nlines = LINES;
389 view->ncols = COLS;
390 view->lines = LINES;
391 view->cols = COLS;
392 err = view_resize(view);
393 if (err)
394 return err;
396 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
397 return got_error_from_errno();
399 return NULL;
402 static int
403 view_is_parent_view(struct tog_view *view)
405 return view->parent == NULL;
408 static const struct got_error *
409 view_resize(struct tog_view *view)
411 int nlines, ncols;
413 if (view->lines > LINES)
414 nlines = view->nlines - (view->lines - LINES);
415 else
416 nlines = view->nlines + (LINES - view->lines);
418 if (view->cols > COLS)
419 ncols = view->ncols - (view->cols - COLS);
420 else
421 ncols = view->ncols + (COLS - view->cols);
423 if (wresize(view->window, nlines, ncols) == ERR)
424 return got_error_from_errno();
425 if (replace_panel(view->panel, view->window) == ERR)
426 return got_error_from_errno();
427 wclear(view->window);
429 view->nlines = nlines;
430 view->ncols = ncols;
431 view->lines = LINES;
432 view->cols = COLS;
434 if (view->child) {
435 view->child->begin_x = view_split_begin_x(view->begin_x);
436 if (view->child->begin_x == 0) {
437 view_fullscreen(view->child);
438 if (view->child->focussed)
439 show_panel(view->child->panel);
440 else
441 show_panel(view->panel);
442 } else {
443 view_splitscreen(view->child);
444 show_panel(view->child->panel);
448 return NULL;
451 static const struct got_error *
452 view_close_child(struct tog_view *view)
454 const struct got_error *err = NULL;
456 if (view->child == NULL)
457 return NULL;
459 err = view_close(view->child);
460 view->child = NULL;
461 return err;
464 static const struct got_error *
465 view_set_child(struct tog_view *view, struct tog_view *child)
467 const struct got_error *err = NULL;
469 view->child = child;
470 child->parent = view;
471 return err;
474 static int
475 view_is_splitscreen(struct tog_view *view)
477 return !view_is_parent_view(view) && view->begin_x > 0;
480 static void
481 tog_resizeterm(void)
483 int cols, lines;
484 struct winsize size;
486 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
487 cols = 80; /* Default */
488 lines = 24;
489 } else {
490 cols = size.ws_col;
491 lines = size.ws_row;
493 resize_term(lines, cols);
496 static const struct got_error *
497 view_input(struct tog_view **new, struct tog_view **dead,
498 struct tog_view **focus, int *done, struct tog_view *view,
499 struct tog_view_list_head *views)
501 const struct got_error *err = NULL;
502 struct tog_view *v;
503 int ch, errcode;
505 *new = NULL;
506 *dead = NULL;
507 *focus = NULL;
509 nodelay(stdscr, FALSE);
510 /* Allow threads to make progress while we are waiting for input. */
511 errcode = pthread_mutex_unlock(&tog_mutex);
512 if (errcode)
513 return got_error_set_errno(errcode);
514 ch = wgetch(view->window);
515 errcode = pthread_mutex_lock(&tog_mutex);
516 if (errcode)
517 return got_error_set_errno(errcode);
518 nodelay(stdscr, TRUE);
520 if (tog_sigwinch_received) {
521 tog_resizeterm();
522 tog_sigwinch_received = 0;
523 TAILQ_FOREACH(v, views, entry) {
524 err = view_resize(v);
525 if (err)
526 return err;
527 err = v->input(new, dead, focus, v, KEY_RESIZE);
528 if (err)
529 return err;
533 switch (ch) {
534 case ERR:
535 break;
536 case '\t':
537 if (view->child) {
538 *focus = view->child;
539 view->child_focussed = 1;
540 } else if (view->parent) {
541 *focus = view->parent;
542 view->parent->child_focussed = 0;
544 break;
545 case 'q':
546 err = view->input(new, dead, focus, view, ch);
547 *dead = view;
548 break;
549 case 'Q':
550 *done = 1;
551 break;
552 case 'f':
553 if (view_is_parent_view(view)) {
554 if (view->child == NULL)
555 break;
556 if (view_is_splitscreen(view->child)) {
557 *focus = view->child;
558 view->child_focussed = 1;
559 err = view_fullscreen(view->child);
560 } else
561 err = view_splitscreen(view->child);
562 if (err)
563 break;
564 err = view->child->input(new, dead, focus,
565 view->child, KEY_RESIZE);
566 } else {
567 if (view_is_splitscreen(view)) {
568 *focus = view;
569 view->parent->child_focussed = 1;
570 err = view_fullscreen(view);
571 } else {
572 err = view_splitscreen(view);
574 if (err)
575 break;
576 err = view->input(new, dead, focus, view,
577 KEY_RESIZE);
579 break;
580 case KEY_RESIZE:
581 break;
582 default:
583 err = view->input(new, dead, focus, view, ch);
584 break;
587 return err;
590 void
591 view_vborder(struct tog_view *view)
593 PANEL *panel;
594 struct tog_view *view_above;
596 if (view->parent)
597 return view_vborder(view->parent);
599 panel = panel_above(view->panel);
600 if (panel == NULL)
601 return;
603 view_above = panel_userptr(panel);
604 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
605 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
608 int
609 view_needs_focus_indication(struct tog_view *view)
611 if (view_is_parent_view(view)) {
612 if (view->child == NULL || view->child_focussed)
613 return 0;
614 if (!view_is_splitscreen(view->child))
615 return 0;
616 } else if (!view_is_splitscreen(view))
617 return 0;
619 return view->focussed;
622 static const struct got_error *
623 view_loop(struct tog_view *view)
625 const struct got_error *err = NULL;
626 struct tog_view_list_head views;
627 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
628 int fast_refresh = 10;
629 int done = 0, errcode;
631 errcode = pthread_mutex_lock(&tog_mutex);
632 if (errcode)
633 return got_error_set_errno(errcode);
635 TAILQ_INIT(&views);
636 TAILQ_INSERT_HEAD(&views, view, entry);
638 main_view = view;
639 view->focussed = 1;
640 err = view->show(view);
641 if (err)
642 return err;
643 update_panels();
644 doupdate();
645 while (!TAILQ_EMPTY(&views) && !done) {
646 /* Refresh fast during initialization, then become slower. */
647 if (fast_refresh && fast_refresh-- == 0)
648 halfdelay(10); /* switch to once per second */
650 err = view_input(&new_view, &dead_view, &focus_view, &done,
651 view, &views);
652 if (err)
653 break;
654 if (dead_view) {
655 struct tog_view *prev = NULL;
657 if (view_is_parent_view(dead_view))
658 prev = TAILQ_PREV(dead_view,
659 tog_view_list_head, entry);
660 else if (view->parent != dead_view)
661 prev = view->parent;
663 if (dead_view->parent)
664 dead_view->parent->child = NULL;
665 else
666 TAILQ_REMOVE(&views, dead_view, entry);
668 err = view_close(dead_view);
669 if (err || dead_view == main_view)
670 goto done;
672 if (view == dead_view) {
673 if (focus_view)
674 view = focus_view;
675 else if (prev)
676 view = prev;
677 else if (!TAILQ_EMPTY(&views))
678 view = TAILQ_LAST(&views,
679 tog_view_list_head);
680 else
681 view = NULL;
682 if (view) {
683 if (view->child && view->child_focussed)
684 focus_view = view->child;
685 else
686 focus_view = view;
690 if (new_view) {
691 struct tog_view *v, *t;
692 /* Only allow one parent view per type. */
693 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
694 if (v->type != new_view->type)
695 continue;
696 TAILQ_REMOVE(&views, v, entry);
697 err = view_close(v);
698 if (err)
699 goto done;
700 break;
702 TAILQ_INSERT_TAIL(&views, new_view, entry);
703 view = new_view;
704 if (focus_view == NULL)
705 focus_view = new_view;
707 if (focus_view) {
708 show_panel(focus_view->panel);
709 if (view)
710 view->focussed = 0;
711 focus_view->focussed = 1;
712 view = focus_view;
713 if (new_view)
714 show_panel(new_view->panel);
715 if (view->child && view_is_splitscreen(view->child))
716 show_panel(view->child->panel);
718 if (view) {
719 if (focus_view == NULL) {
720 view->focussed = 1;
721 show_panel(view->panel);
722 if (view->child && view_is_splitscreen(view->child))
723 show_panel(view->child->panel);
724 focus_view = view;
726 if (view->parent) {
727 err = view->parent->show(view->parent);
728 if (err)
729 goto done;
731 err = view->show(view);
732 if (err)
733 goto done;
734 if (view->child) {
735 err = view->child->show(view->child);
736 if (err)
737 goto done;
739 update_panels();
740 doupdate();
743 done:
744 while (!TAILQ_EMPTY(&views)) {
745 view = TAILQ_FIRST(&views);
746 TAILQ_REMOVE(&views, view, entry);
747 view_close(view);
750 errcode = pthread_mutex_unlock(&tog_mutex);
751 if (errcode)
752 return got_error_set_errno(errcode);
754 return err;
757 __dead static void
758 usage_log(void)
760 endwin();
761 fprintf(stderr,
762 "usage: %s log [-c commit] [-r repository-path] [path]\n",
763 getprogname());
764 exit(1);
767 /* Create newly allocated wide-character string equivalent to a byte string. */
768 static const struct got_error *
769 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
771 char *vis = NULL;
772 const struct got_error *err = NULL;
774 *ws = NULL;
775 *wlen = mbstowcs(NULL, s, 0);
776 if (*wlen == (size_t)-1) {
777 int vislen;
778 if (errno != EILSEQ)
779 return got_error_from_errno();
781 /* byte string invalid in current encoding; try to "fix" it */
782 err = got_mbsavis(&vis, &vislen, s);
783 if (err)
784 return err;
785 *wlen = mbstowcs(NULL, vis, 0);
786 if (*wlen == (size_t)-1) {
787 err = got_error_from_errno(); /* give up */
788 goto done;
792 *ws = calloc(*wlen + 1, sizeof(*ws));
793 if (*ws == NULL) {
794 err = got_error_from_errno();
795 goto done;
798 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
799 err = got_error_from_errno();
800 done:
801 free(vis);
802 if (err) {
803 free(*ws);
804 *ws = NULL;
805 *wlen = 0;
807 return err;
810 /* Format a line for display, ensuring that it won't overflow a width limit. */
811 static const struct got_error *
812 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
814 const struct got_error *err = NULL;
815 int cols = 0;
816 wchar_t *wline = NULL;
817 size_t wlen;
818 int i;
820 *wlinep = NULL;
821 *widthp = 0;
823 err = mbs2ws(&wline, &wlen, line);
824 if (err)
825 return err;
827 i = 0;
828 while (i < wlen && cols < wlimit) {
829 int width = wcwidth(wline[i]);
830 switch (width) {
831 case 0:
832 i++;
833 break;
834 case 1:
835 case 2:
836 if (cols + width <= wlimit)
837 cols += width;
838 i++;
839 break;
840 case -1:
841 if (wline[i] == L'\t')
842 cols += TABSIZE - ((cols + 1) % TABSIZE);
843 i++;
844 break;
845 default:
846 err = got_error_from_errno();
847 goto done;
850 wline[i] = L'\0';
851 if (widthp)
852 *widthp = cols;
853 done:
854 if (err)
855 free(wline);
856 else
857 *wlinep = wline;
858 return err;
861 static const struct got_error *
862 draw_commit(struct tog_view *view, struct got_commit_object *commit,
863 struct got_object_id *id)
865 const struct got_error *err = NULL;
866 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
867 char *logmsg0 = NULL, *logmsg = NULL;
868 char *author0 = NULL, *author = NULL;
869 wchar_t *wlogmsg = NULL, *wauthor = NULL;
870 int author_width, logmsg_width;
871 char *newline, *smallerthan;
872 char *line = NULL;
873 int col, limit;
874 static const size_t date_display_cols = 9;
875 static const size_t author_display_cols = 16;
876 const int avail = view->ncols;
877 struct tm tm;
878 time_t committer_time;
880 committer_time = got_object_commit_get_committer_time(commit);
881 if (localtime_r(&committer_time, &tm) == NULL)
882 return got_error_from_errno();
883 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
884 >= sizeof(datebuf))
885 return got_error(GOT_ERR_NO_SPACE);
887 if (avail < date_display_cols)
888 limit = MIN(sizeof(datebuf) - 1, avail);
889 else
890 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
891 waddnstr(view->window, datebuf, limit);
892 col = limit + 1;
893 if (col > avail)
894 goto done;
896 author0 = strdup(got_object_commit_get_author(commit));
897 if (author0 == NULL) {
898 err = got_error_from_errno();
899 goto done;
901 author = author0;
902 smallerthan = strchr(author, '<');
903 if (smallerthan)
904 *smallerthan = '\0';
905 else {
906 char *at = strchr(author, '@');
907 if (at)
908 *at = '\0';
910 limit = avail - col;
911 err = format_line(&wauthor, &author_width, author, limit);
912 if (err)
913 goto done;
914 waddwstr(view->window, wauthor);
915 col += author_width;
916 while (col <= avail && author_width < author_display_cols + 1) {
917 waddch(view->window, ' ');
918 col++;
919 author_width++;
921 if (col > avail)
922 goto done;
924 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
925 if (logmsg0 == NULL) {
926 err = got_error_from_errno();
927 goto done;
929 logmsg = logmsg0;
930 while (*logmsg == '\n')
931 logmsg++;
932 newline = strchr(logmsg, '\n');
933 if (newline)
934 *newline = '\0';
935 limit = avail - col;
936 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
937 if (err)
938 goto done;
939 waddwstr(view->window, wlogmsg);
940 col += logmsg_width;
941 while (col <= avail) {
942 waddch(view->window, ' ');
943 col++;
945 done:
946 free(logmsg0);
947 free(wlogmsg);
948 free(author0);
949 free(wauthor);
950 free(line);
951 return err;
954 static struct commit_queue_entry *
955 alloc_commit_queue_entry(struct got_commit_object *commit,
956 struct got_object_id *id)
958 struct commit_queue_entry *entry;
960 entry = calloc(1, sizeof(*entry));
961 if (entry == NULL)
962 return NULL;
964 entry->id = id;
965 entry->commit = commit;
966 return entry;
969 static void
970 pop_commit(struct commit_queue *commits)
972 struct commit_queue_entry *entry;
974 entry = TAILQ_FIRST(&commits->head);
975 TAILQ_REMOVE(&commits->head, entry, entry);
976 got_object_commit_close(entry->commit);
977 commits->ncommits--;
978 /* Don't free entry->id! It is owned by the commit graph. */
979 free(entry);
982 static void
983 free_commits(struct commit_queue *commits)
985 while (!TAILQ_EMPTY(&commits->head))
986 pop_commit(commits);
989 static const struct got_error *
990 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
991 int minqueue, struct got_repository *repo, const char *path)
993 const struct got_error *err = NULL;
994 int nqueued = 0;
996 /*
997 * We keep all commits open throughout the lifetime of the log
998 * view in order to avoid having to re-fetch commits from disk
999 * while updating the display.
1001 while (nqueued < minqueue) {
1002 struct got_object_id *id;
1003 struct got_commit_object *commit;
1004 struct commit_queue_entry *entry;
1005 int errcode;
1007 err = got_commit_graph_iter_next(&id, graph);
1008 if (err) {
1009 if (err->code != GOT_ERR_ITER_NEED_MORE)
1010 break;
1011 err = got_commit_graph_fetch_commits(graph,
1012 minqueue, repo);
1013 if (err)
1014 return err;
1015 continue;
1018 if (id == NULL)
1019 break;
1021 err = got_object_open_as_commit(&commit, repo, id);
1022 if (err)
1023 break;
1024 entry = alloc_commit_queue_entry(commit, id);
1025 if (entry == NULL) {
1026 err = got_error_from_errno();
1027 break;
1030 errcode = pthread_mutex_lock(&tog_mutex);
1031 if (errcode) {
1032 err = got_error_set_errno(errcode);
1033 break;
1036 entry->idx = commits->ncommits;
1037 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1038 nqueued++;
1039 commits->ncommits++;
1041 errcode = pthread_mutex_unlock(&tog_mutex);
1042 if (errcode && err == NULL)
1043 err = got_error_set_errno(errcode);
1046 return err;
1049 static const struct got_error *
1050 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1052 const struct got_error *err = NULL;
1053 struct got_reference *head_ref;
1055 *head_id = NULL;
1057 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1058 if (err)
1059 return err;
1061 err = got_ref_resolve(head_id, repo, head_ref);
1062 got_ref_close(head_ref);
1063 if (err) {
1064 *head_id = NULL;
1065 return err;
1068 return NULL;
1071 static const struct got_error *
1072 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1073 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1074 struct commit_queue *commits, int selected_idx, int limit,
1075 const char *path, int commits_needed)
1077 const struct got_error *err = NULL;
1078 struct commit_queue_entry *entry;
1079 int ncommits, width;
1080 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1081 wchar_t *wline;
1083 entry = first;
1084 ncommits = 0;
1085 while (entry) {
1086 if (ncommits == selected_idx) {
1087 *selected = entry;
1088 break;
1090 entry = TAILQ_NEXT(entry, entry);
1091 ncommits++;
1094 if (*selected) {
1095 err = got_object_id_str(&id_str, (*selected)->id);
1096 if (err)
1097 return err;
1100 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1101 entry ? entry->idx + 1 : 0, commits->ncommits,
1102 commits_needed == 0 ? "" : " loading...") == -1)
1103 return got_error_from_errno();
1105 if (path && strcmp(path, "/") != 0) {
1106 if (asprintf(&header, "commit %s %s%s",
1107 id_str ? id_str : "........................................",
1108 path, ncommits_str) == -1) {
1109 err = got_error_from_errno();
1110 header = NULL;
1111 goto done;
1113 } else if (asprintf(&header, "commit %s%s",
1114 id_str ? id_str : "........................................",
1115 ncommits_str) == -1) {
1116 err = got_error_from_errno();
1117 header = NULL;
1118 goto done;
1120 err = format_line(&wline, &width, header, view->ncols);
1121 if (err)
1122 goto done;
1124 werase(view->window);
1126 if (view_needs_focus_indication(view))
1127 wstandout(view->window);
1128 waddwstr(view->window, wline);
1129 while (width < view->ncols) {
1130 waddch(view->window, ' ');
1131 width++;
1133 if (view_needs_focus_indication(view))
1134 wstandend(view->window);
1135 free(wline);
1136 if (limit <= 1)
1137 goto done;
1139 entry = first;
1140 *last = first;
1141 ncommits = 0;
1142 while (entry) {
1143 if (ncommits >= limit - 1)
1144 break;
1145 if (view->focussed && ncommits == selected_idx)
1146 wstandout(view->window);
1147 err = draw_commit(view, entry->commit, entry->id);
1148 if (view->focussed && ncommits == selected_idx)
1149 wstandend(view->window);
1150 if (err)
1151 break;
1152 ncommits++;
1153 *last = entry;
1154 entry = TAILQ_NEXT(entry, entry);
1157 view_vborder(view);
1158 done:
1159 free(id_str);
1160 free(ncommits_str);
1161 free(header);
1162 return err;
1165 static void
1166 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1167 struct commit_queue *commits)
1169 struct commit_queue_entry *entry;
1170 int nscrolled = 0;
1172 entry = TAILQ_FIRST(&commits->head);
1173 if (*first_displayed_entry == entry)
1174 return;
1176 entry = *first_displayed_entry;
1177 while (entry && nscrolled < maxscroll) {
1178 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1179 if (entry) {
1180 *first_displayed_entry = entry;
1181 nscrolled++;
1186 static const struct got_error *
1187 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1188 struct commit_queue_entry **last_displayed_entry,
1189 struct commit_queue *commits, int *log_complete, int *commits_needed,
1190 pthread_cond_t *need_commits)
1192 const struct got_error *err = NULL;
1193 struct commit_queue_entry *pentry;
1194 int nscrolled = 0;
1196 if (*last_displayed_entry == NULL)
1197 return NULL;
1199 do {
1200 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1201 if (pentry == NULL) {
1202 int errcode;
1203 if (*log_complete)
1204 return NULL;
1205 *commits_needed = maxscroll + 20;
1206 errcode = pthread_cond_signal(need_commits);
1207 if (errcode)
1208 return got_error_set_errno(errcode);
1209 return NULL;
1211 *last_displayed_entry = pentry;
1213 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1214 if (pentry == NULL)
1215 break;
1216 *first_displayed_entry = pentry;
1217 } while (++nscrolled < maxscroll);
1219 return err;
1222 static const struct got_error *
1223 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1224 struct got_object_id *commit_id, struct got_commit_object *commit,
1225 struct got_repository *repo)
1227 const struct got_error *err;
1228 struct got_object_qid *parent_id;
1229 struct tog_view *diff_view;
1231 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1233 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1234 if (diff_view == NULL)
1235 return got_error_from_errno();
1237 err = open_diff_view(diff_view, parent_id->id, commit_id, repo);
1238 if (err == NULL)
1239 *new_view = diff_view;
1240 return err;
1243 static const struct got_error *
1244 browse_commit(struct tog_view **new_view, int begin_x,
1245 struct commit_queue_entry *entry, struct got_repository *repo)
1247 const struct got_error *err = NULL;
1248 struct got_tree_object *tree;
1249 struct tog_view *tree_view;
1251 err = got_object_open_as_tree(&tree, repo,
1252 got_object_commit_get_tree_id(entry->commit));
1253 if (err)
1254 return err;
1256 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1257 if (tree_view == NULL)
1258 return got_error_from_errno();
1260 err = open_tree_view(tree_view, tree, entry->id, repo);
1261 if (err)
1262 got_object_tree_close(tree);
1263 else
1264 *new_view = tree_view;
1265 return err;
1268 static void *
1269 log_thread(void *arg)
1271 const struct got_error *err = NULL;
1272 int errcode = 0;
1273 struct tog_log_thread_args *a = arg;
1274 int done = 0;
1276 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1277 if (err)
1278 return (void *)err;
1280 while (!done && !err) {
1281 err = queue_commits(a->graph, a->commits, 1, a->repo,
1282 a->in_repo_path);
1283 if (err) {
1284 if (err->code != GOT_ERR_ITER_COMPLETED)
1285 return (void *)err;
1286 err = NULL;
1287 done = 1;
1288 } else if (a->commits_needed > 0)
1289 a->commits_needed--;
1291 errcode = pthread_mutex_lock(&tog_mutex);
1292 if (errcode)
1293 return (void *)got_error_set_errno(errcode);
1295 if (done)
1296 a->log_complete = 1;
1297 else if (*a->quit) {
1298 done = 1;
1299 a->log_complete = 1;
1300 } else if (*a->first_displayed_entry == NULL) {
1301 *a->first_displayed_entry =
1302 TAILQ_FIRST(&a->commits->head);
1303 *a->selected_entry = *a->first_displayed_entry;
1306 if (done)
1307 a->commits_needed = 0;
1308 else if (a->commits_needed == 0) {
1309 errcode = pthread_cond_wait(&a->need_commits,
1310 &tog_mutex);
1311 if (errcode)
1312 err = got_error_set_errno(errcode);
1315 errcode = pthread_mutex_unlock(&tog_mutex);
1316 if (errcode && err == NULL)
1317 err = got_error_set_errno(errcode);
1319 return (void *)err;
1322 static const struct got_error *
1323 stop_log_thread(struct tog_log_view_state *s)
1325 const struct got_error *err = NULL;
1326 int errcode;
1328 if (s->thread) {
1329 s->quit = 1;
1330 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1331 if (errcode)
1332 return got_error_set_errno(errcode);
1333 errcode = pthread_mutex_unlock(&tog_mutex);
1334 if (errcode)
1335 return got_error_set_errno(errcode);
1336 errcode = pthread_join(s->thread, (void **)&err);
1337 if (errcode)
1338 return got_error_set_errno(errcode);
1339 errcode = pthread_mutex_lock(&tog_mutex);
1340 if (errcode)
1341 return got_error_set_errno(errcode);
1342 s->thread = NULL;
1345 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1346 if (errcode && err == NULL)
1347 err = got_error_set_errno(errcode);
1349 if (s->thread_args.repo) {
1350 got_repo_close(s->thread_args.repo);
1351 s->thread_args.repo = NULL;
1354 if (s->thread_args.graph) {
1355 got_commit_graph_close(s->thread_args.graph);
1356 s->thread_args.graph = NULL;
1359 return err;
1362 static const struct got_error *
1363 close_log_view(struct tog_view *view)
1365 const struct got_error *err = NULL;
1366 struct tog_log_view_state *s = &view->state.log;
1368 err = stop_log_thread(s);
1369 free_commits(&s->commits);
1370 free(s->in_repo_path);
1371 s->in_repo_path = NULL;
1372 free(s->start_id);
1373 s->start_id = NULL;
1374 return err;
1377 static const struct got_error *
1378 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1379 struct got_repository *repo, const char *path, int check_disk)
1381 const struct got_error *err = NULL;
1382 struct tog_log_view_state *s = &view->state.log;
1383 struct got_repository *thread_repo = NULL;
1384 struct got_commit_graph *thread_graph = NULL;
1385 int errcode;
1387 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1388 if (err != NULL)
1389 goto done;
1391 /* The commit queue only contains commits being displayed. */
1392 TAILQ_INIT(&s->commits.head);
1393 s->commits.ncommits = 0;
1395 s->repo = repo;
1396 s->start_id = got_object_id_dup(start_id);
1397 if (s->start_id == NULL) {
1398 err = got_error_from_errno();
1399 goto done;
1402 view->show = show_log_view;
1403 view->input = input_log_view;
1404 view->close = close_log_view;
1406 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1407 if (err)
1408 goto done;
1409 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1410 0, thread_repo);
1411 if (err)
1412 goto done;
1414 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1415 if (errcode) {
1416 err = got_error_set_errno(errcode);
1417 goto done;
1420 s->thread_args.commits_needed = view->nlines;
1421 s->thread_args.graph = thread_graph;
1422 s->thread_args.commits = &s->commits;
1423 s->thread_args.in_repo_path = s->in_repo_path;
1424 s->thread_args.start_id = s->start_id;
1425 s->thread_args.repo = thread_repo;
1426 s->thread_args.log_complete = 0;
1427 s->thread_args.quit = &s->quit;
1428 s->thread_args.view = view;
1429 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1430 s->thread_args.selected_entry = &s->selected_entry;
1431 done:
1432 if (err)
1433 close_log_view(view);
1434 return err;
1437 static const struct got_error *
1438 show_log_view(struct tog_view *view)
1440 struct tog_log_view_state *s = &view->state.log;
1442 if (s->thread == NULL) {
1443 int errcode = pthread_create(&s->thread, NULL, log_thread,
1444 &s->thread_args);
1445 if (errcode)
1446 return got_error_set_errno(errcode);
1449 return draw_commits(view, &s->last_displayed_entry,
1450 &s->selected_entry, s->first_displayed_entry,
1451 &s->commits, s->selected, view->nlines,
1452 s->in_repo_path, s->thread_args.commits_needed);
1455 static const struct got_error *
1456 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1457 struct tog_view **focus_view, struct tog_view *view, int ch)
1459 const struct got_error *err = NULL;
1460 struct tog_log_view_state *s = &view->state.log;
1461 char *parent_path;
1462 struct tog_view *diff_view = NULL, *tree_view = NULL;
1463 int begin_x = 0;
1465 switch (ch) {
1466 case 'q':
1467 s->quit = 1;
1468 break;
1469 case 'k':
1470 case KEY_UP:
1471 if (s->first_displayed_entry == NULL)
1472 break;
1473 if (s->selected > 0)
1474 s->selected--;
1475 if (s->selected > 0)
1476 break;
1477 scroll_up(&s->first_displayed_entry, 1,
1478 &s->commits);
1479 break;
1480 case KEY_PPAGE:
1481 if (s->first_displayed_entry == NULL)
1482 break;
1483 if (TAILQ_FIRST(&s->commits.head) ==
1484 s->first_displayed_entry) {
1485 s->selected = 0;
1486 break;
1488 scroll_up(&s->first_displayed_entry,
1489 view->nlines, &s->commits);
1490 break;
1491 case 'j':
1492 case KEY_DOWN:
1493 if (s->first_displayed_entry == NULL)
1494 break;
1495 if (s->selected < MIN(view->nlines - 2,
1496 s->commits.ncommits - 1)) {
1497 s->selected++;
1498 break;
1500 err = scroll_down(&s->first_displayed_entry, 1,
1501 &s->last_displayed_entry, &s->commits,
1502 &s->thread_args.log_complete,
1503 &s->thread_args.commits_needed,
1504 &s->thread_args.need_commits);
1505 break;
1506 case KEY_NPAGE: {
1507 struct commit_queue_entry *first;
1508 first = s->first_displayed_entry;
1509 if (first == NULL)
1510 break;
1511 err = scroll_down(&s->first_displayed_entry,
1512 view->nlines, &s->last_displayed_entry,
1513 &s->commits, &s->thread_args.log_complete,
1514 &s->thread_args.commits_needed,
1515 &s->thread_args.need_commits);
1516 if (first == s->first_displayed_entry &&
1517 s->selected < MIN(view->nlines - 2,
1518 s->commits.ncommits - 1)) {
1519 /* can't scroll further down */
1520 s->selected = MIN(view->nlines - 2,
1521 s->commits.ncommits - 1);
1523 err = NULL;
1524 break;
1526 case KEY_RESIZE:
1527 if (s->selected > view->nlines - 2)
1528 s->selected = view->nlines - 2;
1529 if (s->selected > s->commits.ncommits - 1)
1530 s->selected = s->commits.ncommits - 1;
1531 break;
1532 case KEY_ENTER:
1533 case '\r':
1534 if (s->selected_entry == NULL)
1535 break;
1536 if (view_is_parent_view(view))
1537 begin_x = view_split_begin_x(view->begin_x);
1538 err = open_diff_view_for_commit(&diff_view, begin_x,
1539 s->selected_entry->id, s->selected_entry->commit,
1540 s->repo);
1541 if (err)
1542 break;
1543 if (view_is_parent_view(view)) {
1544 err = view_close_child(view);
1545 if (err)
1546 return err;
1547 err = view_set_child(view, diff_view);
1548 if (err) {
1549 view_close(diff_view);
1550 break;
1552 *focus_view = diff_view;
1553 view->child_focussed = 1;
1554 } else
1555 *new_view = diff_view;
1556 break;
1557 case 't':
1558 if (s->selected_entry == NULL)
1559 break;
1560 if (view_is_parent_view(view))
1561 begin_x = view_split_begin_x(view->begin_x);
1562 err = browse_commit(&tree_view, begin_x,
1563 s->selected_entry, s->repo);
1564 if (err)
1565 break;
1566 if (view_is_parent_view(view)) {
1567 err = view_close_child(view);
1568 if (err)
1569 return err;
1570 err = view_set_child(view, tree_view);
1571 if (err) {
1572 view_close(tree_view);
1573 break;
1575 *focus_view = tree_view;
1576 view->child_focussed = 1;
1577 } else
1578 *new_view = tree_view;
1579 break;
1580 case KEY_BACKSPACE:
1581 if (strcmp(s->in_repo_path, "/") == 0)
1582 break;
1583 parent_path = dirname(s->in_repo_path);
1584 if (parent_path && strcmp(parent_path, ".") != 0) {
1585 struct tog_view *lv;
1586 err = stop_log_thread(s);
1587 if (err)
1588 return err;
1589 lv = view_open(view->nlines, view->ncols,
1590 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1591 if (lv == NULL)
1592 return got_error_from_errno();
1593 err = open_log_view(lv, s->start_id, s->repo,
1594 parent_path, 0);
1595 if (err)
1596 return err;;
1597 if (view_is_parent_view(view))
1598 *new_view = lv;
1599 else {
1600 view_set_child(view->parent, lv);
1601 *focus_view = lv;
1603 return NULL;
1605 break;
1606 default:
1607 break;
1610 return err;
1613 static const struct got_error *
1614 cmd_log(int argc, char *argv[])
1616 const struct got_error *error;
1617 struct got_repository *repo = NULL;
1618 struct got_object_id *start_id = NULL;
1619 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1620 char *start_commit = NULL;
1621 int ch;
1622 struct tog_view *view;
1624 #ifndef PROFILE
1625 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1626 == -1)
1627 err(1, "pledge");
1628 #endif
1630 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1631 switch (ch) {
1632 case 'c':
1633 start_commit = optarg;
1634 break;
1635 case 'r':
1636 repo_path = realpath(optarg, NULL);
1637 if (repo_path == NULL)
1638 err(1, "-r option");
1639 break;
1640 default:
1641 usage();
1642 /* NOTREACHED */
1646 argc -= optind;
1647 argv += optind;
1649 if (argc == 0)
1650 path = strdup("");
1651 else if (argc == 1)
1652 path = strdup(argv[0]);
1653 else
1654 usage_log();
1655 if (path == NULL)
1656 return got_error_from_errno();
1658 cwd = getcwd(NULL, 0);
1659 if (cwd == NULL) {
1660 error = got_error_from_errno();
1661 goto done;
1663 if (repo_path == NULL) {
1664 repo_path = strdup(cwd);
1665 if (repo_path == NULL) {
1666 error = got_error_from_errno();
1667 goto done;
1671 error = got_repo_open(&repo, repo_path);
1672 if (error != NULL)
1673 goto done;
1675 if (start_commit == NULL)
1676 error = get_head_commit_id(&start_id, repo);
1677 else
1678 error = got_object_resolve_id_str(&start_id, repo,
1679 start_commit);
1680 if (error != NULL)
1681 goto done;
1683 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1684 if (view == NULL) {
1685 error = got_error_from_errno();
1686 goto done;
1688 error = open_log_view(view, start_id, repo, path, 1);
1689 if (error)
1690 goto done;
1691 error = view_loop(view);
1692 done:
1693 free(repo_path);
1694 free(cwd);
1695 free(path);
1696 free(start_id);
1697 if (repo)
1698 got_repo_close(repo);
1699 return error;
1702 __dead static void
1703 usage_diff(void)
1705 endwin();
1706 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1707 getprogname());
1708 exit(1);
1711 static char *
1712 parse_next_line(FILE *f, size_t *len)
1714 char *line;
1715 size_t linelen;
1716 size_t lineno;
1717 const char delim[3] = { '\0', '\0', '\0'};
1719 line = fparseln(f, &linelen, &lineno, delim, 0);
1720 if (len)
1721 *len = linelen;
1722 return line;
1725 static const struct got_error *
1726 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1727 int *last_displayed_line, int *eof, int max_lines,
1728 char * header)
1730 const struct got_error *err;
1731 int nlines = 0, nprinted = 0;
1732 char *line;
1733 size_t len;
1734 wchar_t *wline;
1735 int width;
1737 rewind(f);
1738 werase(view->window);
1740 if (header) {
1741 err = format_line(&wline, &width, header, view->ncols);
1742 if (err) {
1743 return err;
1746 if (view_needs_focus_indication(view))
1747 wstandout(view->window);
1748 waddwstr(view->window, wline);
1749 if (view_needs_focus_indication(view))
1750 wstandend(view->window);
1751 if (width < view->ncols)
1752 waddch(view->window, '\n');
1754 if (max_lines <= 1)
1755 return NULL;
1756 max_lines--;
1759 *eof = 0;
1760 while (nprinted < max_lines) {
1761 line = parse_next_line(f, &len);
1762 if (line == NULL) {
1763 *eof = 1;
1764 break;
1766 if (++nlines < *first_displayed_line) {
1767 free(line);
1768 continue;
1771 err = format_line(&wline, &width, line, view->ncols);
1772 if (err) {
1773 free(line);
1774 return err;
1776 waddwstr(view->window, wline);
1777 if (width < view->ncols)
1778 waddch(view->window, '\n');
1779 if (++nprinted == 1)
1780 *first_displayed_line = nlines;
1781 free(line);
1782 free(wline);
1783 wline = NULL;
1785 *last_displayed_line = nlines;
1787 view_vborder(view);
1789 return NULL;
1792 static char *
1793 get_datestr(time_t *time, char *datebuf)
1795 char *p, *s = ctime_r(time, datebuf);
1796 p = strchr(s, '\n');
1797 if (p)
1798 *p = '\0';
1799 return s;
1802 static const struct got_error *
1803 write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1804 FILE *outfile)
1806 const struct got_error *err = NULL;
1807 char datebuf[26];
1808 struct got_commit_object *commit;
1809 char *id_str = NULL;
1810 time_t committer_time;
1811 const char *author, *committer;
1813 err = got_object_open_as_commit(&commit, repo, commit_id);
1814 if (err)
1815 return err;
1817 err = got_object_id_str(&id_str, commit_id);
1818 if (err) {
1819 err = got_error_from_errno();
1820 goto done;
1823 if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1824 err = got_error_from_errno();
1825 goto done;
1827 if (fprintf(outfile, "from: %s\n",
1828 got_object_commit_get_author(commit)) < 0) {
1829 err = got_error_from_errno();
1830 goto done;
1832 committer_time = got_object_commit_get_committer_time(commit);
1833 if (fprintf(outfile, "date: %s UTC\n",
1834 get_datestr(&committer_time, datebuf)) < 0) {
1835 err = got_error_from_errno();
1836 goto done;
1838 author = got_object_commit_get_author(commit);
1839 committer = got_object_commit_get_committer(commit);
1840 if (strcmp(author, committer) != 0 &&
1841 fprintf(outfile, "via: %s\n", committer) < 0) {
1842 err = got_error_from_errno();
1843 goto done;
1845 if (fprintf(outfile, "%s\n",
1846 got_object_commit_get_logmsg(commit)) < 0) {
1847 err = got_error_from_errno();
1848 goto done;
1850 done:
1851 free(id_str);
1852 got_object_commit_close(commit);
1853 return err;
1856 static const struct got_error *
1857 create_diff(struct tog_diff_view_state *s)
1859 const struct got_error *err = NULL;
1860 FILE *f = NULL;
1861 int obj_type;
1863 f = got_opentemp();
1864 if (f == NULL) {
1865 err = got_error_from_errno();
1866 goto done;
1868 if (s->f)
1869 fclose(s->f);
1870 s->f = f;
1872 if (s->id1)
1873 err = got_object_get_type(&obj_type, s->repo, s->id1);
1874 else
1875 err = got_object_get_type(&obj_type, s->repo, s->id2);
1876 if (err)
1877 goto done;
1879 switch (obj_type) {
1880 case GOT_OBJ_TYPE_BLOB:
1881 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1882 s->diff_context, s->repo, f);
1883 break;
1884 case GOT_OBJ_TYPE_TREE:
1885 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1886 s->diff_context, s->repo, f);
1887 break;
1888 case GOT_OBJ_TYPE_COMMIT: {
1889 const struct got_object_id_queue *parent_ids;
1890 struct got_object_qid *pid;
1891 struct got_commit_object *commit2;
1893 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1894 if (err)
1895 break;
1896 /* Show commit info if we're diffing to a parent commit. */
1897 parent_ids = got_object_commit_get_parent_ids(commit2);
1898 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1899 if (got_object_id_cmp(s->id1, pid->id) == 0) {
1900 write_commit_info(s->id2, s->repo, f);
1901 break;
1904 got_object_commit_close(commit2);
1906 err = got_diff_objects_as_commits(s->id1, s->id2,
1907 s->diff_context, s->repo, f);
1908 break;
1910 default:
1911 err = got_error(GOT_ERR_OBJ_TYPE);
1912 break;
1914 done:
1915 if (f)
1916 fflush(f);
1917 return err;
1920 static const struct got_error *
1921 open_diff_view(struct tog_view *view, struct got_object_id *id1,
1922 struct got_object_id *id2, struct got_repository *repo)
1924 const struct got_error *err;
1926 if (id1 != NULL && id2 != NULL) {
1927 int type1, type2;
1928 err = got_object_get_type(&type1, repo, id1);
1929 if (err)
1930 return err;
1931 err = got_object_get_type(&type2, repo, id2);
1932 if (err)
1933 return err;
1935 if (type1 != type2)
1936 return got_error(GOT_ERR_OBJ_TYPE);
1939 if (id1) {
1940 view->state.diff.id1 = got_object_id_dup(id1);
1941 if (view->state.diff.id1 == NULL)
1942 return got_error_from_errno();
1943 } else
1944 view->state.diff.id1 = NULL;
1946 view->state.diff.id2 = got_object_id_dup(id2);
1947 if (view->state.diff.id2 == NULL) {
1948 free(view->state.diff.id1);
1949 view->state.diff.id1 = NULL;
1950 return got_error_from_errno();
1952 view->state.diff.f = NULL;
1953 view->state.diff.first_displayed_line = 1;
1954 view->state.diff.last_displayed_line = view->nlines;
1955 view->state.diff.diff_context = 3;
1956 view->state.diff.repo = repo;
1958 err = create_diff(&view->state.diff);
1959 if (err) {
1960 free(view->state.diff.id1);
1961 view->state.diff.id1 = NULL;
1962 free(view->state.diff.id2);
1963 view->state.diff.id2 = NULL;
1964 return err;
1967 view->show = show_diff_view;
1968 view->input = input_diff_view;
1969 view->close = close_diff_view;
1971 return NULL;
1974 static const struct got_error *
1975 close_diff_view(struct tog_view *view)
1977 const struct got_error *err = NULL;
1979 free(view->state.diff.id1);
1980 view->state.diff.id1 = NULL;
1981 free(view->state.diff.id2);
1982 view->state.diff.id2 = NULL;
1983 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1984 err = got_error_from_errno();
1985 return err;
1988 static const struct got_error *
1989 show_diff_view(struct tog_view *view)
1991 const struct got_error *err;
1992 struct tog_diff_view_state *s = &view->state.diff;
1993 char *id_str1 = NULL, *id_str2, *header;
1995 if (s->id1) {
1996 err = got_object_id_str(&id_str1, s->id1);
1997 if (err)
1998 return err;
2000 err = got_object_id_str(&id_str2, s->id2);
2001 if (err)
2002 return err;
2004 if (asprintf(&header, "diff %s %s",
2005 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2006 err = got_error_from_errno();
2007 free(id_str1);
2008 free(id_str2);
2009 return err;
2011 free(id_str1);
2012 free(id_str2);
2014 return draw_file(view, s->f, &s->first_displayed_line,
2015 &s->last_displayed_line, &s->eof, view->nlines,
2016 header);
2019 static const struct got_error *
2020 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2021 struct tog_view **focus_view, struct tog_view *view, int ch)
2023 const struct got_error *err = NULL;
2024 struct tog_diff_view_state *s = &view->state.diff;
2025 int i;
2027 switch (ch) {
2028 case 'k':
2029 case KEY_UP:
2030 if (s->first_displayed_line > 1)
2031 s->first_displayed_line--;
2032 break;
2033 case KEY_PPAGE:
2034 i = 0;
2035 while (i++ < view->nlines - 1 &&
2036 s->first_displayed_line > 1)
2037 s->first_displayed_line--;
2038 break;
2039 case 'j':
2040 case KEY_DOWN:
2041 if (!s->eof)
2042 s->first_displayed_line++;
2043 break;
2044 case KEY_NPAGE:
2045 case ' ':
2046 i = 0;
2047 while (!s->eof && i++ < view->nlines - 1) {
2048 char *line;
2049 line = parse_next_line(s->f, NULL);
2050 s->first_displayed_line++;
2051 if (line == NULL)
2052 break;
2054 break;
2055 case '[':
2056 if (s->diff_context > 0) {
2057 s->diff_context--;
2058 err = create_diff(s);
2060 break;
2061 case ']':
2062 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2063 s->diff_context++;
2064 err = create_diff(s);
2066 break;
2067 default:
2068 break;
2071 return err;
2074 static const struct got_error *
2075 cmd_diff(int argc, char *argv[])
2077 const struct got_error *error = NULL;
2078 struct got_repository *repo = NULL;
2079 struct got_object_id *id1 = NULL, *id2 = NULL;
2080 char *repo_path = NULL;
2081 char *id_str1 = NULL, *id_str2 = NULL;
2082 int ch;
2083 struct tog_view *view;
2085 #ifndef PROFILE
2086 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2087 == -1)
2088 err(1, "pledge");
2089 #endif
2091 while ((ch = getopt(argc, argv, "")) != -1) {
2092 switch (ch) {
2093 default:
2094 usage();
2095 /* NOTREACHED */
2099 argc -= optind;
2100 argv += optind;
2102 if (argc == 0) {
2103 usage_diff(); /* TODO show local worktree changes */
2104 } else if (argc == 2) {
2105 repo_path = getcwd(NULL, 0);
2106 if (repo_path == NULL)
2107 return got_error_from_errno();
2108 id_str1 = argv[0];
2109 id_str2 = argv[1];
2110 } else if (argc == 3) {
2111 repo_path = realpath(argv[0], NULL);
2112 if (repo_path == NULL)
2113 return got_error_from_errno();
2114 id_str1 = argv[1];
2115 id_str2 = argv[2];
2116 } else
2117 usage_diff();
2119 error = got_repo_open(&repo, repo_path);
2120 free(repo_path);
2121 if (error)
2122 goto done;
2124 error = got_object_resolve_id_str(&id1, repo, id_str1);
2125 if (error)
2126 goto done;
2128 error = got_object_resolve_id_str(&id2, repo, id_str2);
2129 if (error)
2130 goto done;
2132 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2133 if (view == NULL) {
2134 error = got_error_from_errno();
2135 goto done;
2137 error = open_diff_view(view, id1, id2, repo);
2138 if (error)
2139 goto done;
2140 error = view_loop(view);
2141 done:
2142 got_repo_close(repo);
2143 return error;
2146 __dead static void
2147 usage_blame(void)
2149 endwin();
2150 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2151 getprogname());
2152 exit(1);
2155 struct tog_blame_line {
2156 int annotated;
2157 struct got_object_id *id;
2160 static const struct got_error *
2161 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2162 const char *path, struct tog_blame_line *lines, int nlines,
2163 int blame_complete, int selected_line, int *first_displayed_line,
2164 int *last_displayed_line, int *eof, int max_lines)
2166 const struct got_error *err;
2167 int lineno = 0, nprinted = 0;
2168 char *line;
2169 size_t len;
2170 wchar_t *wline;
2171 int width, wlimit;
2172 struct tog_blame_line *blame_line;
2173 struct got_object_id *prev_id = NULL;
2174 char *id_str;
2176 err = got_object_id_str(&id_str, id);
2177 if (err)
2178 return err;
2180 rewind(f);
2181 werase(view->window);
2183 if (asprintf(&line, "commit %s", id_str) == -1) {
2184 err = got_error_from_errno();
2185 free(id_str);
2186 return err;
2189 err = format_line(&wline, &width, line, view->ncols);
2190 free(line);
2191 line = NULL;
2192 if (view_needs_focus_indication(view))
2193 wstandout(view->window);
2194 waddwstr(view->window, wline);
2195 if (view_needs_focus_indication(view))
2196 wstandend(view->window);
2197 free(wline);
2198 wline = NULL;
2199 if (width < view->ncols)
2200 waddch(view->window, '\n');
2202 if (asprintf(&line, "[%d/%d] %s%s",
2203 *first_displayed_line - 1 + selected_line, nlines,
2204 blame_complete ? "" : "annotating ", path) == -1) {
2205 free(id_str);
2206 return got_error_from_errno();
2208 free(id_str);
2209 err = format_line(&wline, &width, line, view->ncols);
2210 free(line);
2211 line = NULL;
2212 if (err)
2213 return err;
2214 waddwstr(view->window, wline);
2215 free(wline);
2216 wline = NULL;
2217 if (width < view->ncols)
2218 waddch(view->window, '\n');
2220 *eof = 0;
2221 while (nprinted < max_lines - 2) {
2222 line = parse_next_line(f, &len);
2223 if (line == NULL) {
2224 *eof = 1;
2225 break;
2227 if (++lineno < *first_displayed_line) {
2228 free(line);
2229 continue;
2232 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2233 err = format_line(&wline, &width, line, wlimit);
2234 if (err) {
2235 free(line);
2236 return err;
2239 if (view->focussed && nprinted == selected_line - 1)
2240 wstandout(view->window);
2242 blame_line = &lines[lineno - 1];
2243 if (blame_line->annotated && prev_id &&
2244 got_object_id_cmp(prev_id, blame_line->id) == 0)
2245 waddstr(view->window, " ");
2246 else if (blame_line->annotated) {
2247 char *id_str;
2248 err = got_object_id_str(&id_str, blame_line->id);
2249 if (err) {
2250 free(line);
2251 free(wline);
2252 return err;
2254 wprintw(view->window, "%.8s ", id_str);
2255 free(id_str);
2256 prev_id = blame_line->id;
2257 } else {
2258 waddstr(view->window, "........ ");
2259 prev_id = NULL;
2262 waddwstr(view->window, wline);
2263 while (width < wlimit) {
2264 waddch(view->window, ' ');
2265 width++;
2267 if (view->focussed && nprinted == selected_line - 1)
2268 wstandend(view->window);
2269 if (++nprinted == 1)
2270 *first_displayed_line = lineno;
2271 free(line);
2272 free(wline);
2273 wline = NULL;
2275 *last_displayed_line = lineno;
2277 view_vborder(view);
2279 return NULL;
2282 static const struct got_error *
2283 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2285 const struct got_error *err = NULL;
2286 struct tog_blame_cb_args *a = arg;
2287 struct tog_blame_line *line;
2288 int errcode;
2290 if (nlines != a->nlines ||
2291 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2292 return got_error(GOT_ERR_RANGE);
2294 errcode = pthread_mutex_lock(&tog_mutex);
2295 if (errcode)
2296 return got_error_set_errno(errcode);
2298 if (*a->quit) { /* user has quit the blame view */
2299 err = got_error(GOT_ERR_ITER_COMPLETED);
2300 goto done;
2303 if (lineno == -1)
2304 goto done; /* no change in this commit */
2306 line = &a->lines[lineno - 1];
2307 if (line->annotated)
2308 goto done;
2310 line->id = got_object_id_dup(id);
2311 if (line->id == NULL) {
2312 err = got_error_from_errno();
2313 goto done;
2315 line->annotated = 1;
2316 done:
2317 errcode = pthread_mutex_unlock(&tog_mutex);
2318 if (errcode)
2319 err = got_error_set_errno(errcode);
2320 return err;
2323 static void *
2324 blame_thread(void *arg)
2326 const struct got_error *err;
2327 struct tog_blame_thread_args *ta = arg;
2328 struct tog_blame_cb_args *a = ta->cb_args;
2329 int errcode;
2331 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2332 blame_cb, ta->cb_args);
2334 errcode = pthread_mutex_lock(&tog_mutex);
2335 if (errcode)
2336 return (void *)got_error_set_errno(errcode);
2338 got_repo_close(ta->repo);
2339 ta->repo = NULL;
2340 *ta->complete = 1;
2342 errcode = pthread_mutex_unlock(&tog_mutex);
2343 if (errcode && err == NULL)
2344 err = got_error_set_errno(errcode);
2346 return (void *)err;
2349 static struct got_object_id *
2350 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2351 int selected_line)
2353 struct tog_blame_line *line;
2355 line = &lines[first_displayed_line - 1 + selected_line - 1];
2356 if (!line->annotated)
2357 return NULL;
2359 return line->id;
2362 static const struct got_error *
2363 stop_blame(struct tog_blame *blame)
2365 const struct got_error *err = NULL;
2366 int i;
2368 if (blame->thread) {
2369 int errcode;
2370 errcode = pthread_mutex_unlock(&tog_mutex);
2371 if (errcode)
2372 return got_error_set_errno(errcode);
2373 errcode = pthread_join(blame->thread, (void **)&err);
2374 if (errcode)
2375 return got_error_set_errno(errcode);
2376 errcode = pthread_mutex_lock(&tog_mutex);
2377 if (errcode)
2378 return got_error_set_errno(errcode);
2379 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2380 err = NULL;
2381 blame->thread = NULL;
2383 if (blame->thread_args.repo) {
2384 got_repo_close(blame->thread_args.repo);
2385 blame->thread_args.repo = NULL;
2387 if (blame->f) {
2388 fclose(blame->f);
2389 blame->f = NULL;
2391 if (blame->lines) {
2392 for (i = 0; i < blame->nlines; i++)
2393 free(blame->lines[i].id);
2394 free(blame->lines);
2395 blame->lines = NULL;
2397 free(blame->cb_args.commit_id);
2398 blame->cb_args.commit_id = NULL;
2400 return err;
2403 static const struct got_error *
2404 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2405 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2406 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2407 struct got_repository *repo)
2409 const struct got_error *err = NULL;
2410 struct got_blob_object *blob = NULL;
2411 struct got_repository *thread_repo = NULL;
2412 struct got_object_id *obj_id = NULL;
2413 int obj_type;
2415 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2416 if (err)
2417 return err;
2418 if (obj_id == NULL)
2419 return got_error(GOT_ERR_NO_OBJ);
2421 err = got_object_get_type(&obj_type, repo, obj_id);
2422 if (err)
2423 goto done;
2425 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2426 err = got_error(GOT_ERR_OBJ_TYPE);
2427 goto done;
2430 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2431 if (err)
2432 goto done;
2433 blame->f = got_opentemp();
2434 if (blame->f == NULL) {
2435 err = got_error_from_errno();
2436 goto done;
2438 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2439 blame->f, blob);
2440 if (err)
2441 goto done;
2443 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2444 if (blame->lines == NULL) {
2445 err = got_error_from_errno();
2446 goto done;
2449 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2450 if (err)
2451 goto done;
2453 blame->cb_args.view = view;
2454 blame->cb_args.lines = blame->lines;
2455 blame->cb_args.nlines = blame->nlines;
2456 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2457 if (blame->cb_args.commit_id == NULL) {
2458 err = got_error_from_errno();
2459 goto done;
2461 blame->cb_args.quit = done;
2463 blame->thread_args.path = path;
2464 blame->thread_args.repo = thread_repo;
2465 blame->thread_args.cb_args = &blame->cb_args;
2466 blame->thread_args.complete = blame_complete;
2467 *blame_complete = 0;
2469 done:
2470 if (blob)
2471 got_object_blob_close(blob);
2472 free(obj_id);
2473 if (err)
2474 stop_blame(blame);
2475 return err;
2478 static const struct got_error *
2479 open_blame_view(struct tog_view *view, char *path,
2480 struct got_object_id *commit_id, struct got_repository *repo)
2482 const struct got_error *err = NULL;
2483 struct tog_blame_view_state *s = &view->state.blame;
2485 SIMPLEQ_INIT(&s->blamed_commits);
2487 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2488 if (err)
2489 return err;
2491 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2492 s->first_displayed_line = 1;
2493 s->last_displayed_line = view->nlines;
2494 s->selected_line = 1;
2495 s->blame_complete = 0;
2496 s->path = path;
2497 if (s->path == NULL)
2498 return got_error_from_errno();
2499 s->repo = repo;
2500 s->commit_id = commit_id;
2501 memset(&s->blame, 0, sizeof(s->blame));
2503 view->show = show_blame_view;
2504 view->input = input_blame_view;
2505 view->close = close_blame_view;
2507 return run_blame(&s->blame, view, &s->blame_complete,
2508 &s->first_displayed_line, &s->last_displayed_line,
2509 &s->selected_line, &s->done, &s->eof, s->path,
2510 s->blamed_commit->id, s->repo);
2513 static const struct got_error *
2514 close_blame_view(struct tog_view *view)
2516 const struct got_error *err = NULL;
2517 struct tog_blame_view_state *s = &view->state.blame;
2519 if (s->blame.thread)
2520 err = stop_blame(&s->blame);
2522 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2523 struct got_object_qid *blamed_commit;
2524 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2525 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2526 got_object_qid_free(blamed_commit);
2529 free(s->path);
2531 return err;
2534 static const struct got_error *
2535 show_blame_view(struct tog_view *view)
2537 const struct got_error *err = NULL;
2538 struct tog_blame_view_state *s = &view->state.blame;
2539 int errcode;
2541 if (s->blame.thread == NULL) {
2542 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2543 &s->blame.thread_args);
2544 if (errcode)
2545 return got_error_set_errno(errcode);
2548 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2549 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2550 s->selected_line, &s->first_displayed_line,
2551 &s->last_displayed_line, &s->eof, view->nlines);
2553 view_vborder(view);
2554 return err;
2557 static const struct got_error *
2558 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2559 struct tog_view **focus_view, struct tog_view *view, int ch)
2561 const struct got_error *err = NULL, *thread_err = NULL;
2562 struct tog_view *diff_view;
2563 struct tog_blame_view_state *s = &view->state.blame;
2564 int begin_x = 0;
2566 switch (ch) {
2567 case 'q':
2568 s->done = 1;
2569 break;
2570 case 'k':
2571 case KEY_UP:
2572 if (s->selected_line > 1)
2573 s->selected_line--;
2574 else if (s->selected_line == 1 &&
2575 s->first_displayed_line > 1)
2576 s->first_displayed_line--;
2577 break;
2578 case KEY_PPAGE:
2579 if (s->first_displayed_line == 1) {
2580 s->selected_line = 1;
2581 break;
2583 if (s->first_displayed_line > view->nlines - 2)
2584 s->first_displayed_line -=
2585 (view->nlines - 2);
2586 else
2587 s->first_displayed_line = 1;
2588 break;
2589 case 'j':
2590 case KEY_DOWN:
2591 if (s->selected_line < view->nlines - 2 &&
2592 s->first_displayed_line +
2593 s->selected_line <= s->blame.nlines)
2594 s->selected_line++;
2595 else if (s->last_displayed_line <
2596 s->blame.nlines)
2597 s->first_displayed_line++;
2598 break;
2599 case 'b':
2600 case 'p': {
2601 struct got_object_id *id = NULL;
2602 id = get_selected_commit_id(s->blame.lines,
2603 s->first_displayed_line, s->selected_line);
2604 if (id == NULL)
2605 break;
2606 if (ch == 'p') {
2607 struct got_commit_object *commit;
2608 struct got_object_qid *pid;
2609 struct got_object_id *blob_id = NULL;
2610 int obj_type;
2611 err = got_object_open_as_commit(&commit,
2612 s->repo, id);
2613 if (err)
2614 break;
2615 pid = SIMPLEQ_FIRST(
2616 got_object_commit_get_parent_ids(commit));
2617 if (pid == NULL) {
2618 got_object_commit_close(commit);
2619 break;
2621 /* Check if path history ends here. */
2622 err = got_object_id_by_path(&blob_id, s->repo,
2623 pid->id, s->path);
2624 if (err) {
2625 if (err->code == GOT_ERR_NO_TREE_ENTRY)
2626 err = NULL;
2627 got_object_commit_close(commit);
2628 break;
2630 err = got_object_get_type(&obj_type, s->repo,
2631 blob_id);
2632 free(blob_id);
2633 /* Can't blame non-blob type objects. */
2634 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2635 got_object_commit_close(commit);
2636 break;
2638 err = got_object_qid_alloc(&s->blamed_commit,
2639 pid->id);
2640 got_object_commit_close(commit);
2641 } else {
2642 if (got_object_id_cmp(id,
2643 s->blamed_commit->id) == 0)
2644 break;
2645 err = got_object_qid_alloc(&s->blamed_commit,
2646 id);
2648 if (err)
2649 break;
2650 s->done = 1;
2651 thread_err = stop_blame(&s->blame);
2652 s->done = 0;
2653 if (thread_err)
2654 break;
2655 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2656 s->blamed_commit, entry);
2657 err = run_blame(&s->blame, view, &s->blame_complete,
2658 &s->first_displayed_line, &s->last_displayed_line,
2659 &s->selected_line, &s->done, &s->eof,
2660 s->path, s->blamed_commit->id, s->repo);
2661 if (err)
2662 break;
2663 break;
2665 case 'B': {
2666 struct got_object_qid *first;
2667 first = SIMPLEQ_FIRST(&s->blamed_commits);
2668 if (!got_object_id_cmp(first->id, s->commit_id))
2669 break;
2670 s->done = 1;
2671 thread_err = stop_blame(&s->blame);
2672 s->done = 0;
2673 if (thread_err)
2674 break;
2675 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2676 got_object_qid_free(s->blamed_commit);
2677 s->blamed_commit =
2678 SIMPLEQ_FIRST(&s->blamed_commits);
2679 err = run_blame(&s->blame, view, &s->blame_complete,
2680 &s->first_displayed_line, &s->last_displayed_line,
2681 &s->selected_line, &s->done, &s->eof, s->path,
2682 s->blamed_commit->id, s->repo);
2683 if (err)
2684 break;
2685 break;
2687 case KEY_ENTER:
2688 case '\r': {
2689 struct got_object_id *id = NULL;
2690 struct got_object_qid *pid;
2691 struct got_commit_object *commit = NULL;
2692 id = get_selected_commit_id(s->blame.lines,
2693 s->first_displayed_line, s->selected_line);
2694 if (id == NULL || got_object_id_cmp(id,
2695 s->blamed_commit->id) == 0)
2696 break;
2697 err = got_object_open_as_commit(&commit, s->repo, id);
2698 if (err)
2699 break;
2700 pid = SIMPLEQ_FIRST(
2701 got_object_commit_get_parent_ids(commit));
2702 if (view_is_parent_view(view))
2703 begin_x = view_split_begin_x(view->begin_x);
2704 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2705 if (diff_view == NULL) {
2706 got_object_commit_close(commit);
2707 err = got_error_from_errno();
2708 break;
2710 err = open_diff_view(diff_view, pid ? pid->id : NULL,
2711 id, s->repo);
2712 got_object_commit_close(commit);
2713 if (err) {
2714 view_close(diff_view);
2715 break;
2717 if (view_is_parent_view(view)) {
2718 err = view_close_child(view);
2719 if (err)
2720 break;
2721 err = view_set_child(view, diff_view);
2722 if (err) {
2723 view_close(diff_view);
2724 break;
2726 *focus_view = diff_view;
2727 view->child_focussed = 1;
2728 } else
2729 *new_view = diff_view;
2730 if (err)
2731 break;
2732 break;
2734 case KEY_NPAGE:
2735 case ' ':
2736 if (s->last_displayed_line >= s->blame.nlines &&
2737 s->selected_line < view->nlines - 2) {
2738 s->selected_line = MIN(s->blame.nlines,
2739 view->nlines - 2);
2740 break;
2742 if (s->last_displayed_line + view->nlines - 2
2743 <= s->blame.nlines)
2744 s->first_displayed_line +=
2745 view->nlines - 2;
2746 else
2747 s->first_displayed_line =
2748 s->blame.nlines -
2749 (view->nlines - 3);
2750 break;
2751 case KEY_RESIZE:
2752 if (s->selected_line > view->nlines - 2) {
2753 s->selected_line = MIN(s->blame.nlines,
2754 view->nlines - 2);
2756 break;
2757 default:
2758 break;
2760 return thread_err ? thread_err : err;
2763 static const struct got_error *
2764 cmd_blame(int argc, char *argv[])
2766 const struct got_error *error;
2767 struct got_repository *repo = NULL;
2768 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 char *commit_id_str = NULL;
2771 int ch;
2772 struct tog_view *view;
2774 #ifndef PROFILE
2775 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2776 == -1)
2777 err(1, "pledge");
2778 #endif
2780 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2781 switch (ch) {
2782 case 'c':
2783 commit_id_str = optarg;
2784 break;
2785 case 'r':
2786 repo_path = realpath(optarg, NULL);
2787 if (repo_path == NULL)
2788 err(1, "-r option");
2789 break;
2790 default:
2791 usage();
2792 /* NOTREACHED */
2796 argc -= optind;
2797 argv += optind;
2799 if (argc == 1)
2800 path = argv[0];
2801 else
2802 usage_blame();
2804 cwd = getcwd(NULL, 0);
2805 if (cwd == NULL) {
2806 error = got_error_from_errno();
2807 goto done;
2809 if (repo_path == NULL) {
2810 repo_path = strdup(cwd);
2811 if (repo_path == NULL) {
2812 error = got_error_from_errno();
2813 goto done;
2818 error = got_repo_open(&repo, repo_path);
2819 if (error != NULL)
2820 return error;
2822 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2823 if (error != NULL)
2824 goto done;
2826 if (commit_id_str == NULL) {
2827 struct got_reference *head_ref;
2828 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2829 if (error != NULL)
2830 goto done;
2831 error = got_ref_resolve(&commit_id, repo, head_ref);
2832 got_ref_close(head_ref);
2833 } else {
2834 error = got_object_resolve_id_str(&commit_id, repo,
2835 commit_id_str);
2837 if (error != NULL)
2838 goto done;
2840 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2841 if (view == NULL) {
2842 error = got_error_from_errno();
2843 goto done;
2845 error = open_blame_view(view, in_repo_path, commit_id, repo);
2846 if (error)
2847 goto done;
2848 error = view_loop(view);
2849 done:
2850 free(repo_path);
2851 free(cwd);
2852 free(commit_id);
2853 if (repo)
2854 got_repo_close(repo);
2855 return error;
2858 static const struct got_error *
2859 draw_tree_entries(struct tog_view *view,
2860 struct got_tree_entry **first_displayed_entry,
2861 struct got_tree_entry **last_displayed_entry,
2862 struct got_tree_entry **selected_entry, int *ndisplayed,
2863 const char *label, int show_ids, const char *parent_path,
2864 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2866 const struct got_error *err = NULL;
2867 struct got_tree_entry *te;
2868 wchar_t *wline;
2869 int width, n;
2871 *ndisplayed = 0;
2873 werase(view->window);
2875 if (limit == 0)
2876 return NULL;
2878 err = format_line(&wline, &width, label, view->ncols);
2879 if (err)
2880 return err;
2881 if (view_needs_focus_indication(view))
2882 wstandout(view->window);
2883 waddwstr(view->window, wline);
2884 if (view_needs_focus_indication(view))
2885 wstandend(view->window);
2886 free(wline);
2887 wline = NULL;
2888 if (width < view->ncols)
2889 waddch(view->window, '\n');
2890 if (--limit <= 0)
2891 return NULL;
2892 err = format_line(&wline, &width, parent_path, view->ncols);
2893 if (err)
2894 return err;
2895 waddwstr(view->window, wline);
2896 free(wline);
2897 wline = NULL;
2898 if (width < view->ncols)
2899 waddch(view->window, '\n');
2900 if (--limit <= 0)
2901 return NULL;
2902 waddch(view->window, '\n');
2903 if (--limit <= 0)
2904 return NULL;
2906 te = SIMPLEQ_FIRST(&entries->head);
2907 if (*first_displayed_entry == NULL) {
2908 if (selected == 0) {
2909 if (view->focussed)
2910 wstandout(view->window);
2911 *selected_entry = NULL;
2913 waddstr(view->window, " ..\n"); /* parent directory */
2914 if (selected == 0 && view->focussed)
2915 wstandend(view->window);
2916 (*ndisplayed)++;
2917 if (--limit <= 0)
2918 return NULL;
2919 n = 1;
2920 } else {
2921 n = 0;
2922 while (te != *first_displayed_entry)
2923 te = SIMPLEQ_NEXT(te, entry);
2926 while (te) {
2927 char *line = NULL, *id_str = NULL;
2929 if (show_ids) {
2930 err = got_object_id_str(&id_str, te->id);
2931 if (err)
2932 return got_error_from_errno();
2934 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2935 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2936 free(id_str);
2937 return got_error_from_errno();
2939 free(id_str);
2940 err = format_line(&wline, &width, line, view->ncols);
2941 if (err) {
2942 free(line);
2943 break;
2945 if (n == selected) {
2946 if (view->focussed)
2947 wstandout(view->window);
2948 *selected_entry = te;
2950 waddwstr(view->window, wline);
2951 if (width < view->ncols)
2952 waddch(view->window, '\n');
2953 if (n == selected && view->focussed)
2954 wstandend(view->window);
2955 free(line);
2956 free(wline);
2957 wline = NULL;
2958 n++;
2959 (*ndisplayed)++;
2960 *last_displayed_entry = te;
2961 if (--limit <= 0)
2962 break;
2963 te = SIMPLEQ_NEXT(te, entry);
2966 return err;
2969 static void
2970 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2971 const struct got_tree_entries *entries, int isroot)
2973 struct got_tree_entry *te, *prev;
2974 int i;
2976 if (*first_displayed_entry == NULL)
2977 return;
2979 te = SIMPLEQ_FIRST(&entries->head);
2980 if (*first_displayed_entry == te) {
2981 if (!isroot)
2982 *first_displayed_entry = NULL;
2983 return;
2986 /* XXX this is stupid... switch to TAILQ? */
2987 for (i = 0; i < maxscroll; i++) {
2988 while (te != *first_displayed_entry) {
2989 prev = te;
2990 te = SIMPLEQ_NEXT(te, entry);
2992 *first_displayed_entry = prev;
2993 te = SIMPLEQ_FIRST(&entries->head);
2995 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2996 *first_displayed_entry = NULL;
2999 static void
3000 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3001 struct got_tree_entry *last_displayed_entry,
3002 const struct got_tree_entries *entries)
3004 struct got_tree_entry *next;
3005 int n = 0;
3007 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
3008 return;
3010 if (*first_displayed_entry)
3011 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3012 else
3013 next = SIMPLEQ_FIRST(&entries->head);
3014 while (next) {
3015 *first_displayed_entry = next;
3016 if (++n >= maxscroll)
3017 break;
3018 next = SIMPLEQ_NEXT(next, entry);
3022 static const struct got_error *
3023 tree_entry_path(char **path, struct tog_parent_trees *parents,
3024 struct got_tree_entry *te)
3026 const struct got_error *err = NULL;
3027 struct tog_parent_tree *pt;
3028 size_t len = 2; /* for leading slash and NUL */
3030 TAILQ_FOREACH(pt, parents, entry)
3031 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3032 if (te)
3033 len += strlen(te->name);
3035 *path = calloc(1, len);
3036 if (path == NULL)
3037 return got_error_from_errno();
3039 (*path)[0] = '/';
3040 pt = TAILQ_LAST(parents, tog_parent_trees);
3041 while (pt) {
3042 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3043 err = got_error(GOT_ERR_NO_SPACE);
3044 goto done;
3046 if (strlcat(*path, "/", len) >= len) {
3047 err = got_error(GOT_ERR_NO_SPACE);
3048 goto done;
3050 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3052 if (te) {
3053 if (strlcat(*path, te->name, len) >= len) {
3054 err = got_error(GOT_ERR_NO_SPACE);
3055 goto done;
3058 done:
3059 if (err) {
3060 free(*path);
3061 *path = NULL;
3063 return err;
3066 static const struct got_error *
3067 blame_tree_entry(struct tog_view **new_view, int begin_x,
3068 struct got_tree_entry *te, struct tog_parent_trees *parents,
3069 struct got_object_id *commit_id, struct got_repository *repo)
3071 const struct got_error *err = NULL;
3072 char *path;
3073 struct tog_view *blame_view;
3075 err = tree_entry_path(&path, parents, te);
3076 if (err)
3077 return err;
3079 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3080 if (blame_view == NULL)
3081 return got_error_from_errno();
3083 err = open_blame_view(blame_view, path, commit_id, repo);
3084 if (err) {
3085 view_close(blame_view);
3086 free(path);
3087 } else
3088 *new_view = blame_view;
3089 return err;
3092 static const struct got_error *
3093 log_tree_entry(struct tog_view **new_view, int begin_x,
3094 struct got_tree_entry *te, struct tog_parent_trees *parents,
3095 struct got_object_id *commit_id, struct got_repository *repo)
3097 struct tog_view *log_view;
3098 const struct got_error *err = NULL;
3099 char *path;
3101 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3102 if (log_view == NULL)
3103 return got_error_from_errno();
3105 err = tree_entry_path(&path, parents, te);
3106 if (err)
3107 return err;
3109 err = open_log_view(log_view, commit_id, repo, path, 0);
3110 if (err)
3111 view_close(log_view);
3112 else
3113 *new_view = log_view;
3114 free(path);
3115 return err;
3118 static const struct got_error *
3119 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3120 struct got_object_id *commit_id, struct got_repository *repo)
3122 const struct got_error *err = NULL;
3123 char *commit_id_str = NULL;
3124 struct tog_tree_view_state *s = &view->state.tree;
3126 TAILQ_INIT(&s->parents);
3128 err = got_object_id_str(&commit_id_str, commit_id);
3129 if (err != NULL)
3130 goto done;
3132 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3133 err = got_error_from_errno();
3134 goto done;
3137 s->root = s->tree = root;
3138 s->entries = got_object_tree_get_entries(root);
3139 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3140 s->commit_id = got_object_id_dup(commit_id);
3141 if (s->commit_id == NULL) {
3142 err = got_error_from_errno();
3143 goto done;
3145 s->repo = repo;
3147 view->show = show_tree_view;
3148 view->input = input_tree_view;
3149 view->close = close_tree_view;
3150 done:
3151 free(commit_id_str);
3152 if (err) {
3153 free(s->tree_label);
3154 s->tree_label = NULL;
3156 return err;
3159 static const struct got_error *
3160 close_tree_view(struct tog_view *view)
3162 struct tog_tree_view_state *s = &view->state.tree;
3164 free(s->tree_label);
3165 s->tree_label = NULL;
3166 free(s->commit_id);
3167 s->commit_id = NULL;
3168 while (!TAILQ_EMPTY(&s->parents)) {
3169 struct tog_parent_tree *parent;
3170 parent = TAILQ_FIRST(&s->parents);
3171 TAILQ_REMOVE(&s->parents, parent, entry);
3172 free(parent);
3175 if (s->tree != s->root)
3176 got_object_tree_close(s->tree);
3177 got_object_tree_close(s->root);
3179 return NULL;
3182 static const struct got_error *
3183 show_tree_view(struct tog_view *view)
3185 const struct got_error *err = NULL;
3186 struct tog_tree_view_state *s = &view->state.tree;
3187 char *parent_path;
3189 err = tree_entry_path(&parent_path, &s->parents, NULL);
3190 if (err)
3191 return err;
3193 err = draw_tree_entries(view, &s->first_displayed_entry,
3194 &s->last_displayed_entry, &s->selected_entry,
3195 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3196 s->entries, s->selected, view->nlines, s->tree == s->root);
3197 free(parent_path);
3199 view_vborder(view);
3200 return err;
3203 static const struct got_error *
3204 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3205 struct tog_view **focus_view, struct tog_view *view, int ch)
3207 const struct got_error *err = NULL;
3208 struct tog_tree_view_state *s = &view->state.tree;
3209 struct tog_view *log_view;
3210 int begin_x = 0;
3212 switch (ch) {
3213 case 'i':
3214 s->show_ids = !s->show_ids;
3215 break;
3216 case 'l':
3217 if (!s->selected_entry)
3218 break;
3219 if (view_is_parent_view(view))
3220 begin_x = view_split_begin_x(view->begin_x);
3221 err = log_tree_entry(&log_view, begin_x,
3222 s->selected_entry, &s->parents,
3223 s->commit_id, s->repo);
3224 if (view_is_parent_view(view)) {
3225 err = view_close_child(view);
3226 if (err)
3227 return err;
3228 err = view_set_child(view, log_view);
3229 if (err) {
3230 view_close(log_view);
3231 break;
3233 *focus_view = log_view;
3234 view->child_focussed = 1;
3235 } else
3236 *new_view = log_view;
3237 break;
3238 case 'k':
3239 case KEY_UP:
3240 if (s->selected > 0)
3241 s->selected--;
3242 if (s->selected > 0)
3243 break;
3244 tree_scroll_up(&s->first_displayed_entry, 1,
3245 s->entries, s->tree == s->root);
3246 break;
3247 case KEY_PPAGE:
3248 s->selected = 0;
3249 if (SIMPLEQ_FIRST(&s->entries->head) ==
3250 s->first_displayed_entry) {
3251 if (s->tree != s->root)
3252 s->first_displayed_entry = NULL;
3253 break;
3255 tree_scroll_up(&s->first_displayed_entry,
3256 view->nlines, s->entries,
3257 s->tree == s->root);
3258 break;
3259 case 'j':
3260 case KEY_DOWN:
3261 if (s->selected < s->ndisplayed - 1) {
3262 s->selected++;
3263 break;
3265 tree_scroll_down(&s->first_displayed_entry, 1,
3266 s->last_displayed_entry, s->entries);
3267 break;
3268 case KEY_NPAGE:
3269 tree_scroll_down(&s->first_displayed_entry,
3270 view->nlines, s->last_displayed_entry,
3271 s->entries);
3272 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3273 entry))
3274 break;
3275 /* can't scroll any further; move cursor down */
3276 if (s->selected < s->ndisplayed - 1)
3277 s->selected = s->ndisplayed - 1;
3278 break;
3279 case KEY_ENTER:
3280 case '\r':
3281 if (s->selected_entry == NULL) {
3282 struct tog_parent_tree *parent;
3283 case KEY_BACKSPACE:
3284 /* user selected '..' */
3285 if (s->tree == s->root)
3286 break;
3287 parent = TAILQ_FIRST(&s->parents);
3288 TAILQ_REMOVE(&s->parents, parent,
3289 entry);
3290 got_object_tree_close(s->tree);
3291 s->tree = parent->tree;
3292 s->entries =
3293 got_object_tree_get_entries(s->tree);
3294 s->first_displayed_entry =
3295 parent->first_displayed_entry;
3296 s->selected_entry =
3297 parent->selected_entry;
3298 s->selected = parent->selected;
3299 free(parent);
3300 } else if (S_ISDIR(s->selected_entry->mode)) {
3301 struct tog_parent_tree *parent;
3302 struct got_tree_object *child;
3303 err = got_object_open_as_tree(&child,
3304 s->repo, s->selected_entry->id);
3305 if (err)
3306 break;
3307 parent = calloc(1, sizeof(*parent));
3308 if (parent == NULL) {
3309 err = got_error_from_errno();
3310 break;
3312 parent->tree = s->tree;
3313 parent->first_displayed_entry =
3314 s->first_displayed_entry;
3315 parent->selected_entry = s->selected_entry;
3316 parent->selected = s->selected;
3317 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3318 s->tree = child;
3319 s->entries =
3320 got_object_tree_get_entries(s->tree);
3321 s->selected = 0;
3322 s->first_displayed_entry = NULL;
3323 } else if (S_ISREG(s->selected_entry->mode)) {
3324 struct tog_view *blame_view;
3325 int begin_x = view_is_parent_view(view) ?
3326 view_split_begin_x(view->begin_x) : 0;
3328 err = blame_tree_entry(&blame_view, begin_x,
3329 s->selected_entry, &s->parents, s->commit_id,
3330 s->repo);
3331 if (err)
3332 break;
3333 if (view_is_parent_view(view)) {
3334 err = view_close_child(view);
3335 if (err)
3336 return err;
3337 err = view_set_child(view, blame_view);
3338 if (err) {
3339 view_close(blame_view);
3340 break;
3342 *focus_view = blame_view;
3343 view->child_focussed = 1;
3344 } else
3345 *new_view = blame_view;
3347 break;
3348 case KEY_RESIZE:
3349 if (s->selected > view->nlines)
3350 s->selected = s->ndisplayed - 1;
3351 break;
3352 default:
3353 break;
3356 return err;
3359 __dead static void
3360 usage_tree(void)
3362 endwin();
3363 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3364 getprogname());
3365 exit(1);
3368 static const struct got_error *
3369 cmd_tree(int argc, char *argv[])
3371 const struct got_error *error;
3372 struct got_repository *repo = NULL;
3373 char *repo_path = NULL;
3374 struct got_object_id *commit_id = NULL;
3375 char *commit_id_arg = NULL;
3376 struct got_commit_object *commit = NULL;
3377 struct got_tree_object *tree = NULL;
3378 int ch;
3379 struct tog_view *view;
3381 #ifndef PROFILE
3382 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3383 == -1)
3384 err(1, "pledge");
3385 #endif
3387 while ((ch = getopt(argc, argv, "c:")) != -1) {
3388 switch (ch) {
3389 case 'c':
3390 commit_id_arg = optarg;
3391 break;
3392 default:
3393 usage();
3394 /* NOTREACHED */
3398 argc -= optind;
3399 argv += optind;
3401 if (argc == 0) {
3402 repo_path = getcwd(NULL, 0);
3403 if (repo_path == NULL)
3404 return got_error_from_errno();
3405 } else if (argc == 1) {
3406 repo_path = realpath(argv[0], NULL);
3407 if (repo_path == NULL)
3408 return got_error_from_errno();
3409 } else
3410 usage_log();
3412 error = got_repo_open(&repo, repo_path);
3413 free(repo_path);
3414 if (error != NULL)
3415 return error;
3417 if (commit_id_arg == NULL)
3418 error = get_head_commit_id(&commit_id, repo);
3419 else
3420 error = got_object_resolve_id_str(&commit_id, repo,
3421 commit_id_arg);
3422 if (error != NULL)
3423 goto done;
3425 error = got_object_open_as_commit(&commit, repo, commit_id);
3426 if (error != NULL)
3427 goto done;
3429 error = got_object_open_as_tree(&tree, repo,
3430 got_object_commit_get_tree_id(commit));
3431 if (error != NULL)
3432 goto done;
3434 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3435 if (view == NULL) {
3436 error = got_error_from_errno();
3437 goto done;
3439 error = open_tree_view(view, tree, commit_id, repo);
3440 if (error)
3441 goto done;
3442 error = view_loop(view);
3443 done:
3444 free(commit_id);
3445 if (commit)
3446 got_object_commit_close(commit);
3447 if (tree)
3448 got_object_tree_close(tree);
3449 if (repo)
3450 got_repo_close(repo);
3451 return error;
3454 static void
3455 init_curses(void)
3457 initscr();
3458 cbreak();
3459 halfdelay(1); /* Do fast refresh while initial view is loading. */
3460 noecho();
3461 nonl();
3462 intrflush(stdscr, FALSE);
3463 keypad(stdscr, TRUE);
3464 curs_set(0);
3465 signal(SIGWINCH, tog_sigwinch);
3468 __dead static void
3469 usage(void)
3471 int i;
3473 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3474 "Available commands:\n", getprogname());
3475 for (i = 0; i < nitems(tog_commands); i++) {
3476 struct tog_cmd *cmd = &tog_commands[i];
3477 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3479 exit(1);
3482 static char **
3483 make_argv(const char *arg0, const char *arg1)
3485 char **argv;
3486 int argc = (arg1 == NULL ? 1 : 2);
3488 argv = calloc(argc, sizeof(char *));
3489 if (argv == NULL)
3490 err(1, "calloc");
3491 argv[0] = strdup(arg0);
3492 if (argv[0] == NULL)
3493 err(1, "calloc");
3494 if (arg1) {
3495 argv[1] = strdup(arg1);
3496 if (argv[1] == NULL)
3497 err(1, "calloc");
3500 return argv;
3503 int
3504 main(int argc, char *argv[])
3506 const struct got_error *error = NULL;
3507 struct tog_cmd *cmd = NULL;
3508 int ch, hflag = 0;
3509 char **cmd_argv = NULL;
3511 setlocale(LC_ALL, "");
3513 while ((ch = getopt(argc, argv, "h")) != -1) {
3514 switch (ch) {
3515 case 'h':
3516 hflag = 1;
3517 break;
3518 default:
3519 usage();
3520 /* NOTREACHED */
3524 argc -= optind;
3525 argv += optind;
3526 optind = 0;
3527 optreset = 1;
3529 if (argc == 0) {
3530 if (hflag)
3531 usage();
3532 /* Build an argument vector which runs a default command. */
3533 cmd = &tog_commands[0];
3534 cmd_argv = make_argv(cmd->name, NULL);
3535 argc = 1;
3536 } else {
3537 int i;
3539 /* Did the user specific a command? */
3540 for (i = 0; i < nitems(tog_commands); i++) {
3541 if (strncmp(tog_commands[i].name, argv[0],
3542 strlen(argv[0])) == 0) {
3543 cmd = &tog_commands[i];
3544 if (hflag)
3545 tog_commands[i].cmd_usage();
3546 break;
3549 if (cmd == NULL) {
3550 /* Did the user specify a repository? */
3551 char *repo_path = realpath(argv[0], NULL);
3552 if (repo_path) {
3553 struct got_repository *repo;
3554 error = got_repo_open(&repo, repo_path);
3555 if (error == NULL)
3556 got_repo_close(repo);
3557 } else
3558 error = got_error_from_errno();
3559 if (error) {
3560 if (hflag) {
3561 fprintf(stderr, "%s: '%s' is not a "
3562 "known command\n", getprogname(),
3563 argv[0]);
3564 usage();
3566 fprintf(stderr, "%s: '%s' is neither a known "
3567 "command nor a path to a repository\n",
3568 getprogname(), argv[0]);
3569 free(repo_path);
3570 return 1;
3572 cmd = &tog_commands[0];
3573 cmd_argv = make_argv(cmd->name, repo_path);
3574 argc = 2;
3575 free(repo_path);
3579 init_curses();
3581 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3582 if (error)
3583 goto done;
3584 done:
3585 endwin();
3586 free(cmd_argv);
3587 if (error)
3588 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3589 return 0;