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>
20 #include <errno.h>
21 #define _XOPEN_SOURCE_EXTENDED
22 #include <curses.h>
23 #undef _XOPEN_SOURCE_EXTENDED
24 #include <panel.h>
25 #include <locale.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <string.h>
30 #include <err.h>
31 #include <unistd.h>
32 #include <util.h>
33 #include <limits.h>
34 #include <wchar.h>
35 #include <time.h>
36 #include <pthread.h>
37 #include <libgen.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_diff.h"
44 #include "got_opentemp.h"
45 #include "got_commit_graph.h"
46 #include "got_utf8.h"
47 #include "got_blame.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef MAX
54 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
55 #endif
58 #ifndef nitems
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 #endif
62 struct tog_cmd {
63 const char *name;
64 const struct got_error *(*cmd_main)(int, char *[]);
65 void (*cmd_usage)(void);
66 const char *descr;
67 };
69 __dead static void usage(void);
70 __dead static void usage_log(void);
71 __dead static void usage_diff(void);
72 __dead static void usage_blame(void);
73 __dead static void usage_tree(void);
75 static const struct got_error* cmd_log(int, char *[]);
76 static const struct got_error* cmd_diff(int, char *[]);
77 static const struct got_error* cmd_blame(int, char *[]);
78 static const struct got_error* cmd_tree(int, char *[]);
80 static struct tog_cmd tog_commands[] = {
81 { "log", cmd_log, usage_log,
82 "show repository history" },
83 { "diff", cmd_diff, usage_diff,
84 "compare files and directories" },
85 { "blame", cmd_blame, usage_blame,
86 "show line-by-line file history" },
87 { "tree", cmd_tree, usage_tree,
88 "browse trees in repository" },
89 };
91 enum tog_view_type {
92 TOG_VIEW_DIFF,
93 TOG_VIEW_LOG,
94 TOG_VIEW_BLAME,
95 TOG_VIEW_TREE
96 };
98 struct tog_diff_view_state {
99 struct got_object_id *id1, *id2;
100 FILE *f;
101 int first_displayed_line;
102 int last_displayed_line;
103 int eof;
104 int diff_context;
105 struct got_repository *repo;
106 };
108 struct commit_queue_entry {
109 TAILQ_ENTRY(commit_queue_entry) entry;
110 struct got_object_id *id;
111 struct got_commit_object *commit;
112 int idx;
113 };
114 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
115 struct commit_queue {
116 int ncommits;
117 struct commit_queue_head head;
118 };
120 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
122 struct tog_log_thread_args {
123 pthread_cond_t need_commits;
124 int commits_needed;
125 struct got_commit_graph *graph;
126 struct commit_queue *commits;
127 const char *in_repo_path;
128 struct got_object_id *start_id;
129 struct got_repository *repo;
130 int log_complete;
131 sig_atomic_t *quit;
132 struct tog_view *view;
133 struct commit_queue_entry **first_displayed_entry;
134 struct commit_queue_entry **selected_entry;
135 };
137 struct tog_log_view_state {
138 struct commit_queue commits;
139 struct commit_queue_entry *first_displayed_entry;
140 struct commit_queue_entry *last_displayed_entry;
141 struct commit_queue_entry *selected_entry;
142 int selected;
143 char *in_repo_path;
144 struct got_repository *repo;
145 struct got_object_id *start_id;
146 sig_atomic_t quit;
147 pthread_t thread;
148 struct tog_log_thread_args thread_args;
149 };
151 struct tog_blame_cb_args {
152 struct tog_blame_line *lines; /* one per line */
153 int nlines;
155 struct tog_view *view;
156 struct got_object_id *commit_id;
157 int *quit;
158 };
160 struct tog_blame_thread_args {
161 const char *path;
162 struct got_repository *repo;
163 struct tog_blame_cb_args *cb_args;
164 int *complete;
165 };
167 struct tog_blame {
168 FILE *f;
169 size_t filesize;
170 struct tog_blame_line *lines;
171 size_t nlines;
172 pthread_t thread;
173 struct tog_blame_thread_args thread_args;
174 struct tog_blame_cb_args cb_args;
175 const char *path;
176 };
178 struct tog_blame_view_state {
179 int first_displayed_line;
180 int last_displayed_line;
181 int selected_line;
182 int blame_complete;
183 int eof;
184 int done;
185 struct got_object_id_queue blamed_commits;
186 struct got_object_qid *blamed_commit;
187 char *path;
188 struct got_repository *repo;
189 struct got_object_id *commit_id;
190 struct tog_blame blame;
191 };
193 struct tog_parent_tree {
194 TAILQ_ENTRY(tog_parent_tree) entry;
195 struct got_tree_object *tree;
196 struct got_tree_entry *first_displayed_entry;
197 struct got_tree_entry *selected_entry;
198 int selected;
199 };
201 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
203 struct tog_tree_view_state {
204 char *tree_label;
205 struct got_tree_object *root;
206 struct got_tree_object *tree;
207 const struct got_tree_entries *entries;
208 struct got_tree_entry *first_displayed_entry;
209 struct got_tree_entry *last_displayed_entry;
210 struct got_tree_entry *selected_entry;
211 int nentries, ndisplayed, selected, show_ids;
212 struct tog_parent_trees parents;
213 struct got_object_id *commit_id;
214 struct got_repository *repo;
215 };
217 /*
218 * We implement two types of views: parent views and child views.
220 * The 'Tab' key switches between a parent view and its child view.
221 * Child views are shown side-by-side to their parent view, provided
222 * there is enough screen estate.
224 * When a new view is opened from within a parent view, this new view
225 * becomes a child view of the parent view, replacing any existing child.
227 * When a new view is opened from within a child view, this new view
228 * becomes a parent view which will obscure the views below until the
229 * user quits the new parent view by typing 'q'.
231 * This list of views contains parent views only.
232 * Child views are only pointed to by their parent view.
233 */
234 TAILQ_HEAD(tog_view_list_head, tog_view);
236 struct tog_view {
237 TAILQ_ENTRY(tog_view) entry;
238 WINDOW *window;
239 PANEL *panel;
240 int nlines, ncols, begin_y, begin_x;
241 int lines, cols; /* copies of LINES and COLS */
242 int focussed;
243 struct tog_view *parent;
244 struct tog_view *child;
245 int child_focussed;
247 /* type-specific state */
248 enum tog_view_type type;
249 union {
250 struct tog_diff_view_state diff;
251 struct tog_log_view_state log;
252 struct tog_blame_view_state blame;
253 struct tog_tree_view_state tree;
254 } state;
256 const struct got_error *(*show)(struct tog_view *);
257 const struct got_error *(*input)(struct tog_view **,
258 struct tog_view **, struct tog_view**, struct tog_view *, int);
259 const struct got_error *(*close)(struct tog_view *);
260 };
262 static const struct got_error *open_diff_view(struct tog_view *,
263 struct got_object *, struct got_object *, struct got_repository *);
264 static const struct got_error *show_diff_view(struct tog_view *);
265 static const struct got_error *input_diff_view(struct tog_view **,
266 struct tog_view **, struct tog_view **, struct tog_view *, int);
267 static const struct got_error* close_diff_view(struct tog_view *);
269 static const struct got_error *open_log_view(struct tog_view *,
270 struct got_object_id *, struct got_repository *, const char *, int);
271 static const struct got_error * show_log_view(struct tog_view *);
272 static const struct got_error *input_log_view(struct tog_view **,
273 struct tog_view **, struct tog_view **, struct tog_view *, int);
274 static const struct got_error *close_log_view(struct tog_view *);
276 static const struct got_error *open_blame_view(struct tog_view *, char *,
277 struct got_object_id *, struct got_repository *);
278 static const struct got_error *show_blame_view(struct tog_view *);
279 static const struct got_error *input_blame_view(struct tog_view **,
280 struct tog_view **, struct tog_view **, struct tog_view *, int);
281 static const struct got_error *close_blame_view(struct tog_view *);
283 static const struct got_error *open_tree_view(struct tog_view *,
284 struct got_tree_object *, struct got_object_id *, struct got_repository *);
285 static const struct got_error *show_tree_view(struct tog_view *);
286 static const struct got_error *input_tree_view(struct tog_view **,
287 struct tog_view **, struct tog_view **, struct tog_view *, int);
288 static const struct got_error *close_tree_view(struct tog_view *);
290 static const struct got_error *
291 view_close(struct tog_view *view)
293 const struct got_error *err = NULL;
295 if (view->child) {
296 view_close(view->child);
297 view->child = NULL;
299 if (view->close)
300 err = view->close(view);
301 if (view->panel)
302 del_panel(view->panel);
303 if (view->window)
304 delwin(view->window);
305 free(view);
306 return err;
309 static struct tog_view *
310 view_open(int nlines, int ncols, int begin_y, int begin_x,
311 enum tog_view_type type)
313 struct tog_view *view = calloc(1, sizeof(*view));
315 if (view == NULL)
316 return NULL;
318 view->type = type;
319 view->lines = LINES;
320 view->cols = COLS;
321 view->nlines = nlines ? nlines : LINES - begin_y;
322 view->ncols = ncols ? ncols : COLS - begin_x;
323 view->begin_y = begin_y;
324 view->begin_x = begin_x;
325 view->window = newwin(nlines, ncols, begin_y, begin_x);
326 if (view->window == NULL) {
327 view_close(view);
328 return NULL;
330 view->panel = new_panel(view->window);
331 if (view->panel == NULL ||
332 set_panel_userptr(view->panel, view) != OK) {
333 view_close(view);
334 return NULL;
337 keypad(view->window, TRUE);
338 return view;
341 static int
342 view_split_begin_x(int begin_x)
344 if (begin_x > 0 || COLS < 120)
345 return 0;
346 return (COLS - MAX(COLS / 2, 80));
349 static const struct got_error *view_resize(struct tog_view *);
351 static const struct got_error *
352 view_splitscreen(struct tog_view *view)
354 const struct got_error *err = NULL;
356 view->begin_y = 0;
357 view->begin_x = view_split_begin_x(0);
358 view->nlines = LINES;
359 view->ncols = COLS - view->begin_x;
360 view->lines = LINES;
361 view->cols = COLS;
362 err = view_resize(view);
363 if (err)
364 return err;
366 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
367 return got_error_from_errno();
369 return NULL;
372 static const struct got_error *
373 view_fullscreen(struct tog_view *view)
375 const struct got_error *err = NULL;
377 view->begin_x = 0;
378 view->begin_y = 0;
379 view->nlines = LINES;
380 view->ncols = COLS;
381 view->lines = LINES;
382 view->cols = COLS;
383 err = view_resize(view);
384 if (err)
385 return err;
387 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 return got_error_from_errno();
390 return NULL;
393 static int
394 view_is_parent_view(struct tog_view *view)
396 return view->parent == NULL;
399 static const struct got_error *
400 view_resize(struct tog_view *view)
402 int nlines, ncols;
404 if (view->lines > LINES)
405 nlines = view->nlines - (view->lines - LINES);
406 else
407 nlines = view->nlines + (LINES - view->lines);
409 if (view->cols > COLS)
410 ncols = view->ncols - (view->cols - COLS);
411 else
412 ncols = view->ncols + (COLS - view->cols);
414 if (wresize(view->window, nlines, ncols) == ERR)
415 return got_error_from_errno();
416 if (replace_panel(view->panel, view->window) == ERR)
417 return got_error_from_errno();
419 view->nlines = nlines;
420 view->ncols = ncols;
421 view->lines = LINES;
422 view->cols = COLS;
424 if (view->child) {
425 view->child->begin_x = view_split_begin_x(view->begin_x);
426 if (view->child->begin_x == 0) {
427 view_fullscreen(view->child);
428 if (view->child->focussed)
429 show_panel(view->child->panel);
430 else
431 show_panel(view->panel);
432 } else {
433 view_splitscreen(view->child);
434 show_panel(view->child->panel);
438 return NULL;
441 static const struct got_error *
442 view_close_child(struct tog_view *view)
444 const struct got_error *err = NULL;
446 if (view->child == NULL)
447 return NULL;
449 err = view_close(view->child);
450 view->child = NULL;
451 return err;
454 static const struct got_error *
455 view_set_child(struct tog_view *view, struct tog_view *child)
457 const struct got_error *err = NULL;
459 view->child = child;
460 child->parent = view;
461 return err;
464 static int
465 view_is_splitscreen(struct tog_view *view)
467 return !view_is_parent_view(view) && view->begin_x > 0;
470 static const struct got_error *
471 view_input(struct tog_view **new, struct tog_view **dead,
472 struct tog_view **focus, int *done, struct tog_view *view,
473 struct tog_view_list_head *views)
475 const struct got_error *err = NULL;
476 struct tog_view *v;
477 int ch, errcode;
479 *new = NULL;
480 *dead = NULL;
481 *focus = NULL;
483 nodelay(stdscr, FALSE);
484 /* Allow threads to make progress while we are waiting for input. */
485 errcode = pthread_mutex_unlock(&tog_mutex);
486 if (errcode)
487 return got_error_set_errno(errcode);
488 ch = wgetch(view->window);
489 errcode = pthread_mutex_lock(&tog_mutex);
490 if (errcode)
491 return got_error_set_errno(errcode);
492 nodelay(stdscr, TRUE);
493 switch (ch) {
494 case ERR:
495 break;
496 case '\t':
497 if (view->child) {
498 *focus = view->child;
499 view->child_focussed = 1;
500 } else if (view->parent) {
501 *focus = view->parent;
502 view->parent->child_focussed = 0;
504 break;
505 case 'q':
506 err = view->input(new, dead, focus, view, ch);
507 *dead = view;
508 break;
509 case 'Q':
510 *done = 1;
511 break;
512 case 'f':
513 if (view_is_parent_view(view)) {
514 if (view->child == NULL)
515 break;
516 if (view_is_splitscreen(view->child)) {
517 *focus = view->child;
518 view->child_focussed = 1;
519 err = view_fullscreen(view->child);
520 } else
521 err = view_splitscreen(view->child);
522 if (err)
523 break;
524 err = view->child->input(new, dead, focus,
525 view->child, KEY_RESIZE);
526 } else {
527 if (view_is_splitscreen(view)) {
528 *focus = view;
529 view->parent->child_focussed = 1;
530 err = view_fullscreen(view);
531 } else {
532 err = view_splitscreen(view);
534 if (err)
535 break;
536 err = view->input(new, dead, focus, view,
537 KEY_RESIZE);
539 break;
540 case KEY_RESIZE:
541 TAILQ_FOREACH(v, views, entry) {
542 err = view_resize(v);
543 if (err)
544 return err;
545 err = v->input(new, dead, focus, v, ch);
546 if (err)
547 return err;
549 break;
550 default:
551 err = view->input(new, dead, focus, view, ch);
552 break;
555 return err;
558 void
559 view_vborder(struct tog_view *view)
561 PANEL *panel;
562 struct tog_view *view_above;
564 if (view->parent)
565 return view_vborder(view->parent);
567 panel = panel_above(view->panel);
568 if (panel == NULL)
569 return;
571 view_above = panel_userptr(panel);
572 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
573 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
576 int
577 view_needs_focus_indication(struct tog_view *view)
579 if (view_is_parent_view(view)) {
580 if (view->child == NULL || view->child_focussed)
581 return 0;
582 if (!view_is_splitscreen(view->child))
583 return 0;
584 } else if (!view_is_splitscreen(view))
585 return 0;
587 return view->focussed;
590 static const struct got_error *
591 view_loop(struct tog_view *view)
593 const struct got_error *err = NULL;
594 struct tog_view_list_head views;
595 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
596 int fast_refresh = 10;
597 int done = 0, errcode;
599 errcode = pthread_mutex_lock(&tog_mutex);
600 if (errcode)
601 return got_error_set_errno(errcode);
603 TAILQ_INIT(&views);
604 TAILQ_INSERT_HEAD(&views, view, entry);
606 main_view = view;
607 view->focussed = 1;
608 err = view->show(view);
609 if (err)
610 return err;
611 update_panels();
612 doupdate();
613 while (!TAILQ_EMPTY(&views) && !done) {
614 /* Refresh fast during initialization, then become slower. */
615 if (fast_refresh && fast_refresh-- == 0)
616 halfdelay(10); /* switch to once per second */
618 err = view_input(&new_view, &dead_view, &focus_view, &done,
619 view, &views);
620 if (err)
621 break;
622 if (dead_view) {
623 struct tog_view *prev = NULL;
625 if (view_is_parent_view(dead_view))
626 prev = TAILQ_PREV(dead_view,
627 tog_view_list_head, entry);
628 else
629 prev = view->parent;
631 if (dead_view->parent)
632 dead_view->parent->child = NULL;
633 else
634 TAILQ_REMOVE(&views, dead_view, entry);
636 err = view_close(dead_view);
637 if (err || dead_view == main_view)
638 goto done;
640 if (view == dead_view) {
641 if (focus_view)
642 view = focus_view;
643 else if (prev)
644 view = prev;
645 else if (!TAILQ_EMPTY(&views))
646 view = TAILQ_LAST(&views,
647 tog_view_list_head);
648 else
649 view = NULL;
650 if (view) {
651 if (view->child && view->child_focussed)
652 focus_view = view->child;
653 else
654 focus_view = view;
658 if (new_view) {
659 struct tog_view *v, *t;
660 /* Only allow one parent view per type. */
661 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
662 if (v->type != new_view->type)
663 continue;
664 TAILQ_REMOVE(&views, v, entry);
665 err = view_close(v);
666 if (err)
667 goto done;
668 if (v == view)
669 view = new_view;
670 break;
672 TAILQ_INSERT_TAIL(&views, new_view, entry);
673 if (focus_view == NULL)
674 focus_view = new_view;
676 if (focus_view) {
677 show_panel(focus_view->panel);
678 if (view)
679 view->focussed = 0;
680 focus_view->focussed = 1;
681 view = focus_view;
682 if (new_view)
683 show_panel(new_view->panel);
684 if (view->child && view_is_splitscreen(view->child))
685 show_panel(view->child->panel);
687 if (view) {
688 if (focus_view == NULL) {
689 view->focussed = 1;
690 show_panel(view->panel);
691 if (view->child && view_is_splitscreen(view->child))
692 show_panel(view->child->panel);
693 focus_view = view;
695 if (view->parent) {
696 err = view->parent->show(view->parent);
697 if (err)
698 goto done;
700 err = view->show(view);
701 if (err)
702 goto done;
703 if (view->child) {
704 err = view->child->show(view->child);
705 if (err)
706 goto done;
708 update_panels();
709 doupdate();
712 done:
713 while (!TAILQ_EMPTY(&views)) {
714 view = TAILQ_FIRST(&views);
715 TAILQ_REMOVE(&views, view, entry);
716 view_close(view);
719 errcode = pthread_mutex_unlock(&tog_mutex);
720 if (errcode)
721 return got_error_set_errno(errcode);
723 return err;
726 __dead static void
727 usage_log(void)
729 endwin();
730 fprintf(stderr,
731 "usage: %s log [-c commit] [-r repository-path] [path]\n",
732 getprogname());
733 exit(1);
736 /* Create newly allocated wide-character string equivalent to a byte string. */
737 static const struct got_error *
738 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
740 char *vis = NULL;
741 const struct got_error *err = NULL;
743 *ws = NULL;
744 *wlen = mbstowcs(NULL, s, 0);
745 if (*wlen == (size_t)-1) {
746 int vislen;
747 if (errno != EILSEQ)
748 return got_error_from_errno();
750 /* byte string invalid in current encoding; try to "fix" it */
751 err = got_mbsavis(&vis, &vislen, s);
752 if (err)
753 return err;
754 *wlen = mbstowcs(NULL, vis, 0);
755 if (*wlen == (size_t)-1) {
756 err = got_error_from_errno(); /* give up */
757 goto done;
761 *ws = calloc(*wlen + 1, sizeof(*ws));
762 if (*ws == NULL) {
763 err = got_error_from_errno();
764 goto done;
767 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
768 err = got_error_from_errno();
769 done:
770 free(vis);
771 if (err) {
772 free(*ws);
773 *ws = NULL;
774 *wlen = 0;
776 return err;
779 /* Format a line for display, ensuring that it won't overflow a width limit. */
780 static const struct got_error *
781 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
783 const struct got_error *err = NULL;
784 int cols = 0;
785 wchar_t *wline = NULL;
786 size_t wlen;
787 int i;
789 *wlinep = NULL;
790 *widthp = 0;
792 err = mbs2ws(&wline, &wlen, line);
793 if (err)
794 return err;
796 i = 0;
797 while (i < wlen && cols < wlimit) {
798 int width = wcwidth(wline[i]);
799 switch (width) {
800 case 0:
801 i++;
802 break;
803 case 1:
804 case 2:
805 if (cols + width <= wlimit)
806 cols += width;
807 i++;
808 break;
809 case -1:
810 if (wline[i] == L'\t')
811 cols += TABSIZE - ((cols + 1) % TABSIZE);
812 i++;
813 break;
814 default:
815 err = got_error_from_errno();
816 goto done;
819 wline[i] = L'\0';
820 if (widthp)
821 *widthp = cols;
822 done:
823 if (err)
824 free(wline);
825 else
826 *wlinep = wline;
827 return err;
830 static const struct got_error *
831 draw_commit(struct tog_view *view, struct got_commit_object *commit,
832 struct got_object_id *id)
834 const struct got_error *err = NULL;
835 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
836 char *logmsg0 = NULL, *logmsg = NULL;
837 char *author0 = NULL, *author = NULL;
838 wchar_t *wlogmsg = NULL, *wauthor = NULL;
839 int author_width, logmsg_width;
840 char *newline, *smallerthan;
841 char *line = NULL;
842 int col, limit;
843 static const size_t date_display_cols = 9;
844 static const size_t author_display_cols = 16;
845 const int avail = view->ncols;
847 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
848 &commit->tm_committer) >= sizeof(datebuf))
849 return got_error(GOT_ERR_NO_SPACE);
851 if (avail < date_display_cols)
852 limit = MIN(sizeof(datebuf) - 1, avail);
853 else
854 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
855 waddnstr(view->window, datebuf, limit);
856 col = limit + 1;
857 if (col > avail)
858 goto done;
860 author0 = strdup(commit->author);
861 if (author0 == NULL) {
862 err = got_error_from_errno();
863 goto done;
865 author = author0;
866 smallerthan = strchr(author, '<');
867 if (smallerthan)
868 *smallerthan = '\0';
869 else {
870 char *at = strchr(author, '@');
871 if (at)
872 *at = '\0';
874 limit = avail - col;
875 err = format_line(&wauthor, &author_width, author, limit);
876 if (err)
877 goto done;
878 waddwstr(view->window, wauthor);
879 col += author_width;
880 while (col <= avail && author_width < author_display_cols + 1) {
881 waddch(view->window, ' ');
882 col++;
883 author_width++;
885 if (col > avail)
886 goto done;
888 logmsg0 = strdup(commit->logmsg);
889 if (logmsg0 == NULL) {
890 err = got_error_from_errno();
891 goto done;
893 logmsg = logmsg0;
894 while (*logmsg == '\n')
895 logmsg++;
896 newline = strchr(logmsg, '\n');
897 if (newline)
898 *newline = '\0';
899 limit = avail - col;
900 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
901 if (err)
902 goto done;
903 waddwstr(view->window, wlogmsg);
904 col += logmsg_width;
905 while (col <= avail) {
906 waddch(view->window, ' ');
907 col++;
909 done:
910 free(logmsg0);
911 free(wlogmsg);
912 free(author0);
913 free(wauthor);
914 free(line);
915 return err;
918 static struct commit_queue_entry *
919 alloc_commit_queue_entry(struct got_commit_object *commit,
920 struct got_object_id *id)
922 struct commit_queue_entry *entry;
924 entry = calloc(1, sizeof(*entry));
925 if (entry == NULL)
926 return NULL;
928 entry->id = id;
929 entry->commit = commit;
930 return entry;
933 static void
934 pop_commit(struct commit_queue *commits)
936 struct commit_queue_entry *entry;
938 entry = TAILQ_FIRST(&commits->head);
939 TAILQ_REMOVE(&commits->head, entry, entry);
940 got_object_commit_close(entry->commit);
941 commits->ncommits--;
942 /* Don't free entry->id! It is owned by the commit graph. */
943 free(entry);
946 static void
947 free_commits(struct commit_queue *commits)
949 while (!TAILQ_EMPTY(&commits->head))
950 pop_commit(commits);
953 static const struct got_error *
954 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
955 int minqueue, struct got_repository *repo, const char *path)
957 const struct got_error *err = NULL;
958 int nqueued = 0;
960 /*
961 * We keep all commits open throughout the lifetime of the log
962 * view in order to avoid having to re-fetch commits from disk
963 * while updating the display.
964 */
965 while (nqueued < minqueue) {
966 struct got_object_id *id;
967 struct got_commit_object *commit;
968 struct commit_queue_entry *entry;
969 int errcode;
971 err = got_commit_graph_iter_next(&id, graph);
972 if (err) {
973 if (err->code != GOT_ERR_ITER_NEED_MORE)
974 break;
975 err = got_commit_graph_fetch_commits(graph,
976 minqueue, repo);
977 if (err)
978 return err;
979 continue;
982 if (id == NULL)
983 break;
985 err = got_object_open_as_commit(&commit, repo, id);
986 if (err)
987 break;
988 entry = alloc_commit_queue_entry(commit, id);
989 if (entry == NULL) {
990 err = got_error_from_errno();
991 break;
994 errcode = pthread_mutex_lock(&tog_mutex);
995 if (errcode) {
996 err = got_error_set_errno(errcode);
997 break;
1000 entry->idx = commits->ncommits;
1001 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1002 nqueued++;
1003 commits->ncommits++;
1005 errcode = pthread_mutex_unlock(&tog_mutex);
1006 if (errcode && err == NULL)
1007 err = got_error_set_errno(errcode);
1010 return err;
1013 static const struct got_error *
1014 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1016 const struct got_error *err = NULL;
1017 struct got_reference *head_ref;
1019 *head_id = NULL;
1021 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1022 if (err)
1023 return err;
1025 err = got_ref_resolve(head_id, repo, head_ref);
1026 got_ref_close(head_ref);
1027 if (err) {
1028 *head_id = NULL;
1029 return err;
1032 return NULL;
1035 static const struct got_error *
1036 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1037 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1038 struct commit_queue *commits, int selected_idx, int limit,
1039 const char *path, int commits_needed)
1041 const struct got_error *err = NULL;
1042 struct commit_queue_entry *entry;
1043 int ncommits, width;
1044 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1045 wchar_t *wline;
1047 entry = first;
1048 ncommits = 0;
1049 while (entry) {
1050 if (ncommits == selected_idx) {
1051 *selected = entry;
1052 break;
1054 entry = TAILQ_NEXT(entry, entry);
1055 ncommits++;
1058 if (*selected) {
1059 err = got_object_id_str(&id_str, (*selected)->id);
1060 if (err)
1061 return err;
1064 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1065 entry ? entry->idx + 1 : 0, commits->ncommits,
1066 commits_needed == 0 ? "" : " loading...") == -1)
1067 return got_error_from_errno();
1069 if (path && strcmp(path, "/") != 0) {
1070 if (asprintf(&header, "commit: %s %s%s",
1071 id_str ? id_str : "........................................",
1072 path, ncommits_str) == -1) {
1073 err = got_error_from_errno();
1074 header = NULL;
1075 goto done;
1077 } else if (asprintf(&header, "commit: %s%s",
1078 id_str ? id_str : "........................................",
1079 ncommits_str) == -1) {
1080 err = got_error_from_errno();
1081 header = NULL;
1082 goto done;
1084 err = format_line(&wline, &width, header, view->ncols);
1085 if (err)
1086 goto done;
1088 werase(view->window);
1090 if (view_needs_focus_indication(view))
1091 wstandout(view->window);
1092 waddwstr(view->window, wline);
1093 while (width < view->ncols) {
1094 waddch(view->window, ' ');
1095 width++;
1097 if (view_needs_focus_indication(view))
1098 wstandend(view->window);
1099 free(wline);
1100 if (limit <= 1)
1101 goto done;
1103 entry = first;
1104 *last = first;
1105 ncommits = 0;
1106 while (entry) {
1107 if (ncommits >= limit - 1)
1108 break;
1109 if (view->focussed && ncommits == selected_idx)
1110 wstandout(view->window);
1111 err = draw_commit(view, entry->commit, entry->id);
1112 if (view->focussed && ncommits == selected_idx)
1113 wstandend(view->window);
1114 if (err)
1115 break;
1116 ncommits++;
1117 *last = entry;
1118 entry = TAILQ_NEXT(entry, entry);
1121 view_vborder(view);
1122 done:
1123 free(id_str);
1124 free(ncommits_str);
1125 free(header);
1126 return err;
1129 static void
1130 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1131 struct commit_queue *commits)
1133 struct commit_queue_entry *entry;
1134 int nscrolled = 0;
1136 entry = TAILQ_FIRST(&commits->head);
1137 if (*first_displayed_entry == entry)
1138 return;
1140 entry = *first_displayed_entry;
1141 while (entry && nscrolled < maxscroll) {
1142 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1143 if (entry) {
1144 *first_displayed_entry = entry;
1145 nscrolled++;
1150 static const struct got_error *
1151 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1152 struct commit_queue_entry **last_displayed_entry,
1153 struct commit_queue *commits, int *log_complete, int *commits_needed,
1154 pthread_cond_t *need_commits)
1156 const struct got_error *err = NULL;
1157 struct commit_queue_entry *pentry;
1158 int nscrolled = 0;
1160 if (*last_displayed_entry == NULL)
1161 return NULL;
1163 do {
1164 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1165 if (pentry == NULL) {
1166 int errcode;
1167 if (*log_complete)
1168 return NULL;
1169 *commits_needed = maxscroll + 20;
1170 errcode = pthread_cond_signal(need_commits);
1171 if (errcode)
1172 return got_error_set_errno(errcode);
1173 return NULL;
1175 *last_displayed_entry = pentry;
1177 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1178 if (pentry == NULL)
1179 break;
1180 *first_displayed_entry = pentry;
1181 } while (++nscrolled < maxscroll);
1183 return err;
1186 static const struct got_error *
1187 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1188 struct got_object_id *commit_id, struct got_commit_object *commit,
1189 struct got_repository *repo)
1191 const struct got_error *err;
1192 struct got_object *obj1 = NULL, *obj2 = NULL;
1193 struct got_object_qid *parent_id;
1194 struct tog_view *diff_view;
1196 err = got_object_open(&obj2, repo, commit_id);
1197 if (err)
1198 return err;
1200 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1201 if (parent_id) {
1202 err = got_object_open(&obj1, repo, parent_id->id);
1203 if (err)
1204 goto done;
1207 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1208 if (diff_view == NULL) {
1209 err = got_error_from_errno();
1210 goto done;
1213 err = open_diff_view(diff_view, obj1, obj2, repo);
1214 if (err == NULL)
1215 *new_view = diff_view;
1216 done:
1217 if (obj1)
1218 got_object_close(obj1);
1219 if (obj2)
1220 got_object_close(obj2);
1221 return err;
1224 static const struct got_error *
1225 browse_commit(struct tog_view **new_view, int begin_x,
1226 struct commit_queue_entry *entry, struct got_repository *repo)
1228 const struct got_error *err = NULL;
1229 struct got_tree_object *tree;
1230 struct tog_view *tree_view;
1232 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1233 if (err)
1234 return err;
1236 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1237 if (tree_view == NULL)
1238 return got_error_from_errno();
1240 err = open_tree_view(tree_view, tree, entry->id, repo);
1241 if (err)
1242 got_object_tree_close(tree);
1243 else
1244 *new_view = tree_view;
1245 return err;
1248 static void *
1249 log_thread(void *arg)
1251 const struct got_error *err = NULL;
1252 int errcode = 0;
1253 struct tog_log_thread_args *a = arg;
1254 int done = 0;
1256 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1257 if (err)
1258 return (void *)err;
1260 while (!done && !err) {
1261 err = queue_commits(a->graph, a->commits, 1, a->repo,
1262 a->in_repo_path);
1263 if (err) {
1264 if (err->code != GOT_ERR_ITER_COMPLETED)
1265 return (void *)err;
1266 err = NULL;
1267 done = 1;
1268 } else if (a->commits_needed > 0)
1269 a->commits_needed--;
1271 errcode = pthread_mutex_lock(&tog_mutex);
1272 if (errcode)
1273 return (void *)got_error_set_errno(errcode);
1275 if (done)
1276 a->log_complete = 1;
1277 else if (*a->quit) {
1278 done = 1;
1279 a->log_complete = 1;
1280 } else if (*a->first_displayed_entry == NULL) {
1281 *a->first_displayed_entry =
1282 TAILQ_FIRST(&a->commits->head);
1283 *a->selected_entry = *a->first_displayed_entry;
1286 if (done)
1287 a->commits_needed = 0;
1288 else if (a->commits_needed == 0) {
1289 errcode = pthread_cond_wait(&a->need_commits,
1290 &tog_mutex);
1291 if (errcode)
1292 err = got_error_set_errno(errcode);
1295 errcode = pthread_mutex_unlock(&tog_mutex);
1296 if (errcode && err == NULL)
1297 err = got_error_set_errno(errcode);
1299 return (void *)err;
1302 static const struct got_error *
1303 stop_log_thread(struct tog_log_view_state *s)
1305 const struct got_error *err = NULL;
1306 int errcode;
1308 if (s->thread) {
1309 s->quit = 1;
1310 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1311 if (errcode)
1312 return got_error_set_errno(errcode);
1313 errcode = pthread_mutex_unlock(&tog_mutex);
1314 if (errcode)
1315 return got_error_set_errno(errcode);
1316 errcode = pthread_join(s->thread, (void **)&err);
1317 if (errcode)
1318 return got_error_set_errno(errcode);
1319 errcode = pthread_mutex_lock(&tog_mutex);
1320 if (errcode)
1321 return got_error_set_errno(errcode);
1322 s->thread = NULL;
1325 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1326 if (errcode && err == NULL)
1327 err = got_error_set_errno(errcode);
1329 if (s->thread_args.repo) {
1330 got_repo_close(s->thread_args.repo);
1331 s->thread_args.repo = NULL;
1334 if (s->thread_args.graph) {
1335 got_commit_graph_close(s->thread_args.graph);
1336 s->thread_args.graph = NULL;
1339 return err;
1342 static const struct got_error *
1343 close_log_view(struct tog_view *view)
1345 const struct got_error *err = NULL;
1346 struct tog_log_view_state *s = &view->state.log;
1348 err = stop_log_thread(s);
1349 free_commits(&s->commits);
1350 free(s->in_repo_path);
1351 s->in_repo_path = NULL;
1352 free(s->start_id);
1353 s->start_id = NULL;
1354 return err;
1357 static const struct got_error *
1358 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1359 struct got_repository *repo, const char *path, int check_disk)
1361 const struct got_error *err = NULL;
1362 struct tog_log_view_state *s = &view->state.log;
1363 struct got_repository *thread_repo = NULL;
1364 struct got_commit_graph *thread_graph = NULL;
1365 int errcode;
1367 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1368 if (err != NULL)
1369 goto done;
1371 /* The commit queue only contains commits being displayed. */
1372 TAILQ_INIT(&s->commits.head);
1373 s->commits.ncommits = 0;
1375 s->repo = repo;
1376 s->start_id = got_object_id_dup(start_id);
1377 if (s->start_id == NULL) {
1378 err = got_error_from_errno();
1379 goto done;
1382 view->show = show_log_view;
1383 view->input = input_log_view;
1384 view->close = close_log_view;
1386 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1387 if (err)
1388 goto done;
1389 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1390 0, thread_repo);
1391 if (err)
1392 goto done;
1394 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1395 if (errcode) {
1396 err = got_error_set_errno(errcode);
1397 goto done;
1400 s->thread_args.commits_needed = view->nlines;
1401 s->thread_args.graph = thread_graph;
1402 s->thread_args.commits = &s->commits;
1403 s->thread_args.in_repo_path = s->in_repo_path;
1404 s->thread_args.start_id = s->start_id;
1405 s->thread_args.repo = thread_repo;
1406 s->thread_args.log_complete = 0;
1407 s->thread_args.quit = &s->quit;
1408 s->thread_args.view = view;
1409 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1410 s->thread_args.selected_entry = &s->selected_entry;
1412 errcode = pthread_create(&s->thread, NULL, log_thread,
1413 &s->thread_args);
1414 if (errcode) {
1415 err = got_error_set_errno(errcode);
1416 goto done;
1419 done:
1420 if (err)
1421 close_log_view(view);
1422 return err;
1425 static const struct got_error *
1426 show_log_view(struct tog_view *view)
1428 struct tog_log_view_state *s = &view->state.log;
1430 return draw_commits(view, &s->last_displayed_entry,
1431 &s->selected_entry, s->first_displayed_entry,
1432 &s->commits, s->selected, view->nlines,
1433 s->in_repo_path, s->thread_args.commits_needed);
1436 static const struct got_error *
1437 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1438 struct tog_view **focus_view, struct tog_view *view, int ch)
1440 const struct got_error *err = NULL;
1441 struct tog_log_view_state *s = &view->state.log;
1442 char *parent_path;
1443 struct tog_view *diff_view = NULL, *tree_view = NULL;
1444 int begin_x = 0;
1446 switch (ch) {
1447 case 'q':
1448 s->quit = 1;
1449 break;
1450 case 'k':
1451 case KEY_UP:
1452 if (s->selected > 0)
1453 s->selected--;
1454 if (s->selected > 0)
1455 break;
1456 scroll_up(&s->first_displayed_entry, 1,
1457 &s->commits);
1458 break;
1459 case KEY_PPAGE:
1460 if (TAILQ_FIRST(&s->commits.head) ==
1461 s->first_displayed_entry) {
1462 s->selected = 0;
1463 break;
1465 scroll_up(&s->first_displayed_entry,
1466 view->nlines, &s->commits);
1467 break;
1468 case 'j':
1469 case KEY_DOWN:
1470 if (s->selected < MIN(view->nlines - 2,
1471 s->commits.ncommits - 1)) {
1472 s->selected++;
1473 break;
1475 err = scroll_down(&s->first_displayed_entry, 1,
1476 &s->last_displayed_entry, &s->commits,
1477 &s->thread_args.log_complete,
1478 &s->thread_args.commits_needed,
1479 &s->thread_args.need_commits);
1480 break;
1481 case KEY_NPAGE: {
1482 struct commit_queue_entry *first;
1483 first = s->first_displayed_entry;
1484 err = scroll_down(&s->first_displayed_entry,
1485 view->nlines, &s->last_displayed_entry,
1486 &s->commits, &s->thread_args.log_complete,
1487 &s->thread_args.commits_needed,
1488 &s->thread_args.need_commits);
1489 if (first == s->first_displayed_entry &&
1490 s->selected < MIN(view->nlines - 2,
1491 s->commits.ncommits - 1)) {
1492 /* can't scroll further down */
1493 s->selected = MIN(view->nlines - 2,
1494 s->commits.ncommits - 1);
1496 err = NULL;
1497 break;
1499 case KEY_RESIZE:
1500 if (s->selected > view->nlines - 2)
1501 s->selected = view->nlines - 2;
1502 if (s->selected > s->commits.ncommits - 1)
1503 s->selected = s->commits.ncommits - 1;
1504 break;
1505 case KEY_ENTER:
1506 case '\r':
1507 if (view_is_parent_view(view))
1508 begin_x = view_split_begin_x(view->begin_x);
1509 err = open_diff_view_for_commit(&diff_view, begin_x,
1510 s->selected_entry->id, s->selected_entry->commit,
1511 s->repo);
1512 if (err)
1513 break;
1514 if (view_is_parent_view(view)) {
1515 err = view_close_child(view);
1516 if (err)
1517 return err;
1518 err = view_set_child(view, diff_view);
1519 if (err) {
1520 view_close(diff_view);
1521 break;
1523 if (!view_is_splitscreen(diff_view)) {
1524 *focus_view = diff_view;
1525 view->child_focussed = 1;
1527 } else
1528 *new_view = diff_view;
1529 break;
1530 case 't':
1531 if (view_is_parent_view(view))
1532 begin_x = view_split_begin_x(view->begin_x);
1533 err = browse_commit(&tree_view, begin_x,
1534 s->selected_entry, s->repo);
1535 if (view_is_parent_view(view)) {
1536 err = view_close_child(view);
1537 if (err)
1538 return err;
1539 err = view_set_child(view, tree_view);
1540 if (err) {
1541 view_close(tree_view);
1542 break;
1544 *focus_view = tree_view;
1545 view->child_focussed = 1;
1546 } else
1547 *new_view = tree_view;
1548 break;
1549 case KEY_BACKSPACE:
1550 if (strcmp(s->in_repo_path, "/") == 0)
1551 break;
1552 parent_path = dirname(s->in_repo_path);
1553 if (parent_path && strcmp(parent_path, ".") != 0) {
1554 struct tog_view *lv;
1555 err = stop_log_thread(s);
1556 if (err)
1557 return err;
1558 lv = view_open(view->nlines, view->ncols,
1559 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1560 if (lv == NULL)
1561 return got_error_from_errno();
1562 err = open_log_view(lv, s->start_id, s->repo,
1563 parent_path, 0);
1564 if (err)
1565 return err;;
1566 if (view_is_parent_view(view))
1567 *new_view = lv;
1568 else {
1569 view_set_child(view->parent, lv);
1570 *focus_view = lv;
1572 return NULL;
1574 break;
1575 default:
1576 break;
1579 return err;
1582 static const struct got_error *
1583 cmd_log(int argc, char *argv[])
1585 const struct got_error *error;
1586 struct got_repository *repo = NULL;
1587 struct got_object_id *start_id = NULL;
1588 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1589 char *start_commit = NULL;
1590 int ch;
1591 struct tog_view *view;
1593 #ifndef PROFILE
1594 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1595 == -1)
1596 err(1, "pledge");
1597 #endif
1599 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1600 switch (ch) {
1601 case 'c':
1602 start_commit = optarg;
1603 break;
1604 case 'r':
1605 repo_path = realpath(optarg, NULL);
1606 if (repo_path == NULL)
1607 err(1, "-r option");
1608 break;
1609 default:
1610 usage();
1611 /* NOTREACHED */
1615 argc -= optind;
1616 argv += optind;
1618 if (argc == 0)
1619 path = strdup("");
1620 else if (argc == 1)
1621 path = strdup(argv[0]);
1622 else
1623 usage_log();
1624 if (path == NULL)
1625 return got_error_from_errno();
1627 cwd = getcwd(NULL, 0);
1628 if (cwd == NULL) {
1629 error = got_error_from_errno();
1630 goto done;
1632 if (repo_path == NULL) {
1633 repo_path = strdup(cwd);
1634 if (repo_path == NULL) {
1635 error = got_error_from_errno();
1636 goto done;
1640 error = got_repo_open(&repo, repo_path);
1641 if (error != NULL)
1642 goto done;
1644 if (start_commit == NULL) {
1645 error = get_head_commit_id(&start_id, repo);
1646 if (error != NULL)
1647 goto done;
1648 } else {
1649 struct got_object *obj;
1650 error = got_object_open_by_id_str(&obj, repo, start_commit);
1651 if (error == NULL) {
1652 start_id = got_object_id_dup(got_object_get_id(obj));
1653 if (start_id == NULL)
1654 error = got_error_from_errno();
1655 goto done;
1658 if (error != NULL)
1659 goto done;
1661 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1662 if (view == NULL) {
1663 error = got_error_from_errno();
1664 goto done;
1666 error = open_log_view(view, start_id, repo, path, 1);
1667 if (error)
1668 goto done;
1669 error = view_loop(view);
1670 done:
1671 free(repo_path);
1672 free(cwd);
1673 free(path);
1674 free(start_id);
1675 if (repo)
1676 got_repo_close(repo);
1677 return error;
1680 __dead static void
1681 usage_diff(void)
1683 endwin();
1684 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1685 getprogname());
1686 exit(1);
1689 static char *
1690 parse_next_line(FILE *f, size_t *len)
1692 char *line;
1693 size_t linelen;
1694 size_t lineno;
1695 const char delim[3] = { '\0', '\0', '\0'};
1697 line = fparseln(f, &linelen, &lineno, delim, 0);
1698 if (len)
1699 *len = linelen;
1700 return line;
1703 static const struct got_error *
1704 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1705 int *last_displayed_line, int *eof, int max_lines,
1706 char * header)
1708 const struct got_error *err;
1709 int nlines = 0, nprinted = 0;
1710 char *line;
1711 size_t len;
1712 wchar_t *wline;
1713 int width;
1715 rewind(f);
1716 werase(view->window);
1718 if (header) {
1719 err = format_line(&wline, &width, header, view->ncols);
1720 if (err) {
1721 return err;
1724 if (view_needs_focus_indication(view))
1725 wstandout(view->window);
1726 waddwstr(view->window, wline);
1727 if (view_needs_focus_indication(view))
1728 wstandend(view->window);
1729 if (width < view->ncols)
1730 waddch(view->window, '\n');
1732 if (max_lines <= 1)
1733 return NULL;
1734 max_lines--;
1737 *eof = 0;
1738 while (nprinted < max_lines) {
1739 line = parse_next_line(f, &len);
1740 if (line == NULL) {
1741 *eof = 1;
1742 break;
1744 if (++nlines < *first_displayed_line) {
1745 free(line);
1746 continue;
1749 err = format_line(&wline, &width, line, view->ncols);
1750 if (err) {
1751 free(line);
1752 return err;
1754 waddwstr(view->window, wline);
1755 if (width < view->ncols)
1756 waddch(view->window, '\n');
1757 if (++nprinted == 1)
1758 *first_displayed_line = nlines;
1759 free(line);
1760 free(wline);
1761 wline = NULL;
1763 *last_displayed_line = nlines;
1765 view_vborder(view);
1767 return NULL;
1770 static const struct got_error *
1771 create_diff(struct tog_diff_view_state *s)
1773 const struct got_error *err = NULL;
1774 struct got_object *obj1 = NULL, *obj2 = NULL;
1775 FILE *f = NULL;
1777 if (s->id1) {
1778 err = got_object_open(&obj1, s->repo, s->id1);
1779 if (err)
1780 return err;
1783 err = got_object_open(&obj2, s->repo, s->id2);
1784 if (err)
1785 goto done;
1787 f = got_opentemp();
1788 if (f == NULL) {
1789 err = got_error_from_errno();
1790 goto done;
1792 if (s->f)
1793 fclose(s->f);
1794 s->f = f;
1796 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1797 case GOT_OBJ_TYPE_BLOB:
1798 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1799 s->diff_context, s->repo, f);
1800 break;
1801 case GOT_OBJ_TYPE_TREE:
1802 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1803 s->diff_context, s->repo, f);
1804 break;
1805 case GOT_OBJ_TYPE_COMMIT:
1806 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1807 s->repo, f);
1808 break;
1809 default:
1810 err = got_error(GOT_ERR_OBJ_TYPE);
1811 break;
1813 done:
1814 if (obj1)
1815 got_object_close(obj1);
1816 got_object_close(obj2);
1817 if (f)
1818 fflush(f);
1819 return err;
1822 static const struct got_error *
1823 open_diff_view(struct tog_view *view, struct got_object *obj1,
1824 struct got_object *obj2, struct got_repository *repo)
1826 const struct got_error *err;
1828 if (obj1 != NULL && obj2 != NULL &&
1829 got_object_get_type(obj1) != got_object_get_type(obj2))
1830 return got_error(GOT_ERR_OBJ_TYPE);
1832 if (obj1) {
1833 struct got_object_id *id1;
1834 id1 = got_object_id_dup(got_object_get_id(obj1));
1835 if (id1 == NULL)
1836 return got_error_from_errno();
1837 view->state.diff.id1 = id1;
1838 } else
1839 view->state.diff.id1 = NULL;
1841 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1842 if (view->state.diff.id2 == NULL) {
1843 free(view->state.diff.id1);
1844 view->state.diff.id1 = NULL;
1845 return got_error_from_errno();
1847 view->state.diff.f = NULL;
1848 view->state.diff.first_displayed_line = 1;
1849 view->state.diff.last_displayed_line = view->nlines;
1850 view->state.diff.diff_context = 3;
1851 view->state.diff.repo = repo;
1853 err = create_diff(&view->state.diff);
1854 if (err) {
1855 free(view->state.diff.id1);
1856 view->state.diff.id1 = NULL;
1857 free(view->state.diff.id2);
1858 view->state.diff.id2 = NULL;
1859 return err;
1862 view->show = show_diff_view;
1863 view->input = input_diff_view;
1864 view->close = close_diff_view;
1866 return NULL;
1869 static const struct got_error *
1870 close_diff_view(struct tog_view *view)
1872 const struct got_error *err = NULL;
1874 free(view->state.diff.id1);
1875 view->state.diff.id1 = NULL;
1876 free(view->state.diff.id2);
1877 view->state.diff.id2 = NULL;
1878 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1879 err = got_error_from_errno();
1880 return err;
1883 static const struct got_error *
1884 show_diff_view(struct tog_view *view)
1886 const struct got_error *err;
1887 struct tog_diff_view_state *s = &view->state.diff;
1888 char *id_str1 = NULL, *id_str2, *header;
1890 if (s->id1) {
1891 err = got_object_id_str(&id_str1, s->id1);
1892 if (err)
1893 return err;
1895 err = got_object_id_str(&id_str2, s->id2);
1896 if (err)
1897 return err;
1899 if (asprintf(&header, "diff: %s %s",
1900 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1901 err = got_error_from_errno();
1902 free(id_str1);
1903 free(id_str2);
1904 return err;
1906 free(id_str1);
1907 free(id_str2);
1909 return draw_file(view, s->f, &s->first_displayed_line,
1910 &s->last_displayed_line, &s->eof, view->nlines,
1911 header);
1914 static const struct got_error *
1915 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1916 struct tog_view **focus_view, struct tog_view *view, int ch)
1918 const struct got_error *err = NULL;
1919 struct tog_diff_view_state *s = &view->state.diff;
1920 int i;
1922 switch (ch) {
1923 case 'k':
1924 case KEY_UP:
1925 if (s->first_displayed_line > 1)
1926 s->first_displayed_line--;
1927 break;
1928 case KEY_PPAGE:
1929 i = 0;
1930 while (i++ < view->nlines - 1 &&
1931 s->first_displayed_line > 1)
1932 s->first_displayed_line--;
1933 break;
1934 case 'j':
1935 case KEY_DOWN:
1936 if (!s->eof)
1937 s->first_displayed_line++;
1938 break;
1939 case KEY_NPAGE:
1940 case ' ':
1941 i = 0;
1942 while (!s->eof && i++ < view->nlines - 1) {
1943 char *line;
1944 line = parse_next_line(s->f, NULL);
1945 s->first_displayed_line++;
1946 if (line == NULL)
1947 break;
1949 break;
1950 case '[':
1951 if (s->diff_context > 0) {
1952 s->diff_context--;
1953 err = create_diff(s);
1955 break;
1956 case ']':
1957 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1958 s->diff_context++;
1959 err = create_diff(s);
1961 break;
1962 default:
1963 break;
1966 return err;
1969 static const struct got_error *
1970 cmd_diff(int argc, char *argv[])
1972 const struct got_error *error = NULL;
1973 struct got_repository *repo = NULL;
1974 struct got_object *obj1 = NULL, *obj2 = NULL;
1975 char *repo_path = NULL;
1976 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1977 int ch;
1978 struct tog_view *view;
1980 #ifndef PROFILE
1981 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1982 == -1)
1983 err(1, "pledge");
1984 #endif
1986 while ((ch = getopt(argc, argv, "")) != -1) {
1987 switch (ch) {
1988 default:
1989 usage();
1990 /* NOTREACHED */
1994 argc -= optind;
1995 argv += optind;
1997 if (argc == 0) {
1998 usage_diff(); /* TODO show local worktree changes */
1999 } else if (argc == 2) {
2000 repo_path = getcwd(NULL, 0);
2001 if (repo_path == NULL)
2002 return got_error_from_errno();
2003 obj_id_str1 = argv[0];
2004 obj_id_str2 = argv[1];
2005 } else if (argc == 3) {
2006 repo_path = realpath(argv[0], NULL);
2007 if (repo_path == NULL)
2008 return got_error_from_errno();
2009 obj_id_str1 = argv[1];
2010 obj_id_str2 = argv[2];
2011 } else
2012 usage_diff();
2014 error = got_repo_open(&repo, repo_path);
2015 free(repo_path);
2016 if (error)
2017 goto done;
2019 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2020 if (error)
2021 goto done;
2023 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2024 if (error)
2025 goto done;
2027 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2028 if (view == NULL) {
2029 error = got_error_from_errno();
2030 goto done;
2032 error = open_diff_view(view, obj1, obj2, repo);
2033 if (error)
2034 goto done;
2035 error = view_loop(view);
2036 done:
2037 got_repo_close(repo);
2038 if (obj1)
2039 got_object_close(obj1);
2040 if (obj2)
2041 got_object_close(obj2);
2042 return error;
2045 __dead static void
2046 usage_blame(void)
2048 endwin();
2049 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2050 getprogname());
2051 exit(1);
2054 struct tog_blame_line {
2055 int annotated;
2056 struct got_object_id *id;
2059 static const struct got_error *
2060 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2061 const char *path, struct tog_blame_line *lines, int nlines,
2062 int blame_complete, int selected_line, int *first_displayed_line,
2063 int *last_displayed_line, int *eof, int max_lines)
2065 const struct got_error *err;
2066 int lineno = 0, nprinted = 0;
2067 char *line;
2068 size_t len;
2069 wchar_t *wline;
2070 int width, wlimit;
2071 struct tog_blame_line *blame_line;
2072 struct got_object_id *prev_id = NULL;
2073 char *id_str;
2075 err = got_object_id_str(&id_str, id);
2076 if (err)
2077 return err;
2079 rewind(f);
2080 werase(view->window);
2082 if (asprintf(&line, "commit: %s", id_str) == -1) {
2083 err = got_error_from_errno();
2084 free(id_str);
2085 return err;
2088 err = format_line(&wline, &width, line, view->ncols);
2089 free(line);
2090 line = NULL;
2091 if (view_needs_focus_indication(view))
2092 wstandout(view->window);
2093 waddwstr(view->window, wline);
2094 if (view_needs_focus_indication(view))
2095 wstandend(view->window);
2096 free(wline);
2097 wline = NULL;
2098 if (width < view->ncols)
2099 waddch(view->window, '\n');
2101 if (asprintf(&line, "[%d/%d] %s%s",
2102 *first_displayed_line - 1 + selected_line, nlines,
2103 blame_complete ? "" : "annotating ", path) == -1) {
2104 free(id_str);
2105 return got_error_from_errno();
2107 free(id_str);
2108 err = format_line(&wline, &width, line, view->ncols);
2109 free(line);
2110 line = NULL;
2111 if (err)
2112 return err;
2113 waddwstr(view->window, wline);
2114 free(wline);
2115 wline = NULL;
2116 if (width < view->ncols)
2117 waddch(view->window, '\n');
2119 *eof = 0;
2120 while (nprinted < max_lines - 2) {
2121 line = parse_next_line(f, &len);
2122 if (line == NULL) {
2123 *eof = 1;
2124 break;
2126 if (++lineno < *first_displayed_line) {
2127 free(line);
2128 continue;
2131 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2132 err = format_line(&wline, &width, line, wlimit);
2133 if (err) {
2134 free(line);
2135 return err;
2138 if (view->focussed && nprinted == selected_line - 1)
2139 wstandout(view->window);
2141 blame_line = &lines[lineno - 1];
2142 if (blame_line->annotated && prev_id &&
2143 got_object_id_cmp(prev_id, blame_line->id) == 0)
2144 waddstr(view->window, " ");
2145 else if (blame_line->annotated) {
2146 char *id_str;
2147 err = got_object_id_str(&id_str, blame_line->id);
2148 if (err) {
2149 free(line);
2150 free(wline);
2151 return err;
2153 wprintw(view->window, "%.8s ", id_str);
2154 free(id_str);
2155 prev_id = blame_line->id;
2156 } else {
2157 waddstr(view->window, "........ ");
2158 prev_id = NULL;
2161 waddwstr(view->window, wline);
2162 while (width < wlimit) {
2163 waddch(view->window, ' ');
2164 width++;
2166 if (view->focussed && nprinted == selected_line - 1)
2167 wstandend(view->window);
2168 if (++nprinted == 1)
2169 *first_displayed_line = lineno;
2170 free(line);
2171 free(wline);
2172 wline = NULL;
2174 *last_displayed_line = lineno;
2176 view_vborder(view);
2178 return NULL;
2181 static const struct got_error *
2182 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2184 const struct got_error *err = NULL;
2185 struct tog_blame_cb_args *a = arg;
2186 struct tog_blame_line *line;
2187 int errcode;
2189 if (nlines != a->nlines ||
2190 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2191 return got_error(GOT_ERR_RANGE);
2193 errcode = pthread_mutex_lock(&tog_mutex);
2194 if (errcode)
2195 return got_error_set_errno(errcode);
2197 if (*a->quit) { /* user has quit the blame view */
2198 err = got_error(GOT_ERR_ITER_COMPLETED);
2199 goto done;
2202 if (lineno == -1)
2203 goto done; /* no change in this commit */
2205 line = &a->lines[lineno - 1];
2206 if (line->annotated)
2207 goto done;
2209 line->id = got_object_id_dup(id);
2210 if (line->id == NULL) {
2211 err = got_error_from_errno();
2212 goto done;
2214 line->annotated = 1;
2215 done:
2216 errcode = pthread_mutex_unlock(&tog_mutex);
2217 if (errcode)
2218 err = got_error_set_errno(errcode);
2219 return err;
2222 static void *
2223 blame_thread(void *arg)
2225 const struct got_error *err;
2226 struct tog_blame_thread_args *ta = arg;
2227 struct tog_blame_cb_args *a = ta->cb_args;
2228 int errcode;
2230 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2231 blame_cb, ta->cb_args);
2233 errcode = pthread_mutex_lock(&tog_mutex);
2234 if (errcode)
2235 return (void *)got_error_set_errno(errcode);
2237 got_repo_close(ta->repo);
2238 ta->repo = NULL;
2239 *ta->complete = 1;
2241 errcode = pthread_mutex_unlock(&tog_mutex);
2242 if (errcode && err == NULL)
2243 err = got_error_set_errno(errcode);
2245 return (void *)err;
2248 static struct got_object_id *
2249 get_selected_commit_id(struct tog_blame_line *lines,
2250 int first_displayed_line, int selected_line)
2252 struct tog_blame_line *line;
2254 line = &lines[first_displayed_line - 1 + selected_line - 1];
2255 if (!line->annotated)
2256 return NULL;
2258 return line->id;
2261 static const struct got_error *
2262 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2263 struct tog_blame_line *lines, int first_displayed_line,
2264 int selected_line, struct got_repository *repo)
2266 const struct got_error *err = NULL;
2267 struct got_commit_object *commit = NULL;
2268 struct got_object_id *selected_id;
2269 struct got_object_qid *pid;
2271 *pobj = NULL;
2272 *obj = NULL;
2274 selected_id = get_selected_commit_id(lines,
2275 first_displayed_line, selected_line);
2276 if (selected_id == NULL)
2277 return NULL;
2279 err = got_object_open(obj, repo, selected_id);
2280 if (err)
2281 goto done;
2283 err = got_object_commit_open(&commit, repo, *obj);
2284 if (err)
2285 goto done;
2287 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2288 if (pid) {
2289 err = got_object_open(pobj, repo, pid->id);
2290 if (err)
2291 goto done;
2293 done:
2294 if (commit)
2295 got_object_commit_close(commit);
2296 return err;
2299 static const struct got_error *
2300 stop_blame(struct tog_blame *blame)
2302 const struct got_error *err = NULL;
2303 int i;
2305 if (blame->thread) {
2306 int errcode;
2307 errcode = pthread_mutex_unlock(&tog_mutex);
2308 if (errcode)
2309 return got_error_set_errno(errcode);
2310 errcode = pthread_join(blame->thread, (void **)&err);
2311 if (errcode)
2312 return got_error_set_errno(errcode);
2313 errcode = pthread_mutex_lock(&tog_mutex);
2314 if (errcode)
2315 return got_error_set_errno(errcode);
2316 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2317 err = NULL;
2318 blame->thread = NULL;
2320 if (blame->thread_args.repo) {
2321 got_repo_close(blame->thread_args.repo);
2322 blame->thread_args.repo = NULL;
2324 if (blame->f) {
2325 fclose(blame->f);
2326 blame->f = NULL;
2328 for (i = 0; i < blame->nlines; i++)
2329 free(blame->lines[i].id);
2330 free(blame->lines);
2331 blame->lines = NULL;
2332 free(blame->cb_args.commit_id);
2333 blame->cb_args.commit_id = NULL;
2335 return err;
2338 static const struct got_error *
2339 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2340 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2341 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2342 struct got_repository *repo)
2344 const struct got_error *err = NULL;
2345 struct got_blob_object *blob = NULL;
2346 struct got_repository *thread_repo = NULL;
2347 struct got_object_id *obj_id = NULL;
2348 struct got_object *obj = NULL;
2349 int errcode;
2351 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2352 if (err)
2353 goto done;
2355 err = got_object_open(&obj, repo, obj_id);
2356 if (err)
2357 goto done;
2359 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2360 err = got_error(GOT_ERR_OBJ_TYPE);
2361 goto done;
2364 err = got_object_blob_open(&blob, repo, obj, 8192);
2365 if (err)
2366 goto done;
2367 blame->f = got_opentemp();
2368 if (blame->f == NULL) {
2369 err = got_error_from_errno();
2370 goto done;
2372 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2373 blame->f, blob);
2374 if (err)
2375 goto done;
2377 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2378 if (blame->lines == NULL) {
2379 err = got_error_from_errno();
2380 goto done;
2383 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2384 if (err)
2385 goto done;
2387 blame->cb_args.view = view;
2388 blame->cb_args.lines = blame->lines;
2389 blame->cb_args.nlines = blame->nlines;
2390 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2391 if (blame->cb_args.commit_id == NULL) {
2392 err = got_error_from_errno();
2393 goto done;
2395 blame->cb_args.quit = done;
2397 blame->thread_args.path = path;
2398 blame->thread_args.repo = thread_repo;
2399 blame->thread_args.cb_args = &blame->cb_args;
2400 blame->thread_args.complete = blame_complete;
2401 *blame_complete = 0;
2403 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2404 &blame->thread_args);
2405 if (errcode) {
2406 err = got_error_set_errno(errcode);
2407 goto done;
2410 done:
2411 if (blob)
2412 got_object_blob_close(blob);
2413 free(obj_id);
2414 if (obj)
2415 got_object_close(obj);
2416 if (err)
2417 stop_blame(blame);
2418 return err;
2421 static const struct got_error *
2422 open_blame_view(struct tog_view *view, char *path,
2423 struct got_object_id *commit_id, struct got_repository *repo)
2425 const struct got_error *err = NULL;
2426 struct tog_blame_view_state *s = &view->state.blame;
2428 SIMPLEQ_INIT(&s->blamed_commits);
2430 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2431 if (err)
2432 return err;
2434 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2435 s->first_displayed_line = 1;
2436 s->last_displayed_line = view->nlines;
2437 s->selected_line = 1;
2438 s->blame_complete = 0;
2439 s->path = path;
2440 if (s->path == NULL)
2441 return got_error_from_errno();
2442 s->repo = repo;
2443 s->commit_id = commit_id;
2444 memset(&s->blame, 0, sizeof(s->blame));
2446 view->show = show_blame_view;
2447 view->input = input_blame_view;
2448 view->close = close_blame_view;
2450 return run_blame(&s->blame, view, &s->blame_complete,
2451 &s->first_displayed_line, &s->last_displayed_line,
2452 &s->selected_line, &s->done, &s->eof, s->path,
2453 s->blamed_commit->id, s->repo);
2456 static const struct got_error *
2457 close_blame_view(struct tog_view *view)
2459 const struct got_error *err = NULL;
2460 struct tog_blame_view_state *s = &view->state.blame;
2462 if (s->blame.thread)
2463 err = stop_blame(&s->blame);
2465 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2466 struct got_object_qid *blamed_commit;
2467 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2468 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2469 got_object_qid_free(blamed_commit);
2472 free(s->path);
2474 return err;
2477 static const struct got_error *
2478 show_blame_view(struct tog_view *view)
2480 const struct got_error *err = NULL;
2481 struct tog_blame_view_state *s = &view->state.blame;
2483 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2484 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2485 s->selected_line, &s->first_displayed_line,
2486 &s->last_displayed_line, &s->eof, view->nlines);
2488 view_vborder(view);
2489 return err;
2492 static const struct got_error *
2493 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2494 struct tog_view **focus_view, struct tog_view *view, int ch)
2496 const struct got_error *err = NULL, *thread_err = NULL;
2497 struct got_object *obj = NULL, *pobj = NULL;
2498 struct tog_view *diff_view;
2499 struct tog_blame_view_state *s = &view->state.blame;
2500 int begin_x = 0;
2502 switch (ch) {
2503 case 'q':
2504 s->done = 1;
2505 break;
2506 case 'k':
2507 case KEY_UP:
2508 if (s->selected_line > 1)
2509 s->selected_line--;
2510 else if (s->selected_line == 1 &&
2511 s->first_displayed_line > 1)
2512 s->first_displayed_line--;
2513 break;
2514 case KEY_PPAGE:
2515 if (s->first_displayed_line == 1) {
2516 s->selected_line = 1;
2517 break;
2519 if (s->first_displayed_line > view->nlines - 2)
2520 s->first_displayed_line -=
2521 (view->nlines - 2);
2522 else
2523 s->first_displayed_line = 1;
2524 break;
2525 case 'j':
2526 case KEY_DOWN:
2527 if (s->selected_line < view->nlines - 2 &&
2528 s->first_displayed_line +
2529 s->selected_line <= s->blame.nlines)
2530 s->selected_line++;
2531 else if (s->last_displayed_line <
2532 s->blame.nlines)
2533 s->first_displayed_line++;
2534 break;
2535 case 'b':
2536 case 'p': {
2537 struct got_object_id *id;
2538 id = get_selected_commit_id(s->blame.lines,
2539 s->first_displayed_line, s->selected_line);
2540 if (id == NULL || got_object_id_cmp(id,
2541 s->blamed_commit->id) == 0)
2542 break;
2543 err = open_selected_commit(&pobj, &obj,
2544 s->blame.lines, s->first_displayed_line,
2545 s->selected_line, s->repo);
2546 if (err)
2547 break;
2548 if (pobj == NULL && obj == NULL)
2549 break;
2550 if (ch == 'p' && pobj == NULL)
2551 break;
2552 s->done = 1;
2553 thread_err = stop_blame(&s->blame);
2554 s->done = 0;
2555 if (thread_err)
2556 break;
2557 id = got_object_get_id(ch == 'b' ? obj : pobj);
2558 got_object_close(obj);
2559 obj = NULL;
2560 if (pobj) {
2561 got_object_close(pobj);
2562 pobj = NULL;
2564 err = got_object_qid_alloc(&s->blamed_commit, id);
2565 if (err)
2566 goto done;
2567 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2568 s->blamed_commit, entry);
2569 err = run_blame(&s->blame, view, &s->blame_complete,
2570 &s->first_displayed_line, &s->last_displayed_line,
2571 &s->selected_line, &s->done, &s->eof,
2572 s->path, s->blamed_commit->id, s->repo);
2573 if (err)
2574 break;
2575 break;
2577 case 'B': {
2578 struct got_object_qid *first;
2579 first = SIMPLEQ_FIRST(&s->blamed_commits);
2580 if (!got_object_id_cmp(first->id, s->commit_id))
2581 break;
2582 s->done = 1;
2583 thread_err = stop_blame(&s->blame);
2584 s->done = 0;
2585 if (thread_err)
2586 break;
2587 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2588 got_object_qid_free(s->blamed_commit);
2589 s->blamed_commit =
2590 SIMPLEQ_FIRST(&s->blamed_commits);
2591 err = run_blame(&s->blame, view, &s->blame_complete,
2592 &s->first_displayed_line, &s->last_displayed_line,
2593 &s->selected_line, &s->done, &s->eof, s->path,
2594 s->blamed_commit->id, s->repo);
2595 if (err)
2596 break;
2597 break;
2599 case KEY_ENTER:
2600 case '\r':
2601 err = open_selected_commit(&pobj, &obj,
2602 s->blame.lines, s->first_displayed_line,
2603 s->selected_line, s->repo);
2604 if (err)
2605 break;
2606 if (pobj == NULL && obj == NULL)
2607 break;
2609 if (view_is_parent_view(view))
2610 begin_x = view_split_begin_x(view->begin_x);
2611 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2612 if (diff_view == NULL) {
2613 err = got_error_from_errno();
2614 break;
2616 err = open_diff_view(diff_view, pobj, obj, s->repo);
2617 if (err) {
2618 view_close(diff_view);
2619 break;
2621 if (view_is_parent_view(view)) {
2622 err = view_close_child(view);
2623 if (err)
2624 return err;
2625 err = view_set_child(view, diff_view);
2626 if (err) {
2627 view_close(diff_view);
2628 break;
2630 if (!view_is_splitscreen(diff_view)) {
2631 *focus_view = diff_view;
2632 view->child_focussed = 1;
2634 } else
2635 *new_view = diff_view;
2636 if (pobj) {
2637 got_object_close(pobj);
2638 pobj = NULL;
2640 got_object_close(obj);
2641 obj = NULL;
2642 if (err)
2643 break;
2644 break;
2645 case KEY_NPAGE:
2646 case ' ':
2647 if (s->last_displayed_line >= s->blame.nlines &&
2648 s->selected_line < view->nlines - 2) {
2649 s->selected_line = MIN(s->blame.nlines,
2650 view->nlines - 2);
2651 break;
2653 if (s->last_displayed_line + view->nlines - 2
2654 <= s->blame.nlines)
2655 s->first_displayed_line +=
2656 view->nlines - 2;
2657 else
2658 s->first_displayed_line =
2659 s->blame.nlines -
2660 (view->nlines - 3);
2661 break;
2662 case KEY_RESIZE:
2663 if (s->selected_line > view->nlines - 2) {
2664 s->selected_line = MIN(s->blame.nlines,
2665 view->nlines - 2);
2667 break;
2668 default:
2669 break;
2671 done:
2672 if (pobj)
2673 got_object_close(pobj);
2674 return thread_err ? thread_err : err;
2677 static const struct got_error *
2678 cmd_blame(int argc, char *argv[])
2680 const struct got_error *error;
2681 struct got_repository *repo = NULL;
2682 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2683 struct got_object_id *commit_id = NULL;
2684 char *commit_id_str = NULL;
2685 int ch;
2686 struct tog_view *view;
2688 #ifndef PROFILE
2689 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2690 == -1)
2691 err(1, "pledge");
2692 #endif
2694 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2695 switch (ch) {
2696 case 'c':
2697 commit_id_str = optarg;
2698 break;
2699 case 'r':
2700 repo_path = realpath(optarg, NULL);
2701 if (repo_path == NULL)
2702 err(1, "-r option");
2703 break;
2704 default:
2705 usage();
2706 /* NOTREACHED */
2710 argc -= optind;
2711 argv += optind;
2713 if (argc == 1)
2714 path = argv[0];
2715 else
2716 usage_blame();
2718 cwd = getcwd(NULL, 0);
2719 if (cwd == NULL) {
2720 error = got_error_from_errno();
2721 goto done;
2723 if (repo_path == NULL) {
2724 repo_path = strdup(cwd);
2725 if (repo_path == NULL) {
2726 error = got_error_from_errno();
2727 goto done;
2732 error = got_repo_open(&repo, repo_path);
2733 if (error != NULL)
2734 return error;
2736 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2737 if (error != NULL)
2738 goto done;
2740 if (commit_id_str == NULL) {
2741 struct got_reference *head_ref;
2742 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2743 if (error != NULL)
2744 goto done;
2745 error = got_ref_resolve(&commit_id, repo, head_ref);
2746 got_ref_close(head_ref);
2747 } else {
2748 struct got_object *obj;
2749 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2750 if (error != NULL)
2751 goto done;
2752 commit_id = got_object_id_dup(got_object_get_id(obj));
2753 if (commit_id == NULL)
2754 error = got_error_from_errno();
2755 got_object_close(obj);
2757 if (error != NULL)
2758 goto done;
2760 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2761 if (view == NULL) {
2762 error = got_error_from_errno();
2763 goto done;
2765 error = open_blame_view(view, in_repo_path, commit_id, repo);
2766 if (error)
2767 goto done;
2768 error = view_loop(view);
2769 done:
2770 free(repo_path);
2771 free(cwd);
2772 free(commit_id);
2773 if (repo)
2774 got_repo_close(repo);
2775 return error;
2778 static const struct got_error *
2779 draw_tree_entries(struct tog_view *view,
2780 struct got_tree_entry **first_displayed_entry,
2781 struct got_tree_entry **last_displayed_entry,
2782 struct got_tree_entry **selected_entry, int *ndisplayed,
2783 const char *label, int show_ids, const char *parent_path,
2784 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2786 const struct got_error *err = NULL;
2787 struct got_tree_entry *te;
2788 wchar_t *wline;
2789 int width, n;
2791 *ndisplayed = 0;
2793 werase(view->window);
2795 if (limit == 0)
2796 return NULL;
2798 err = format_line(&wline, &width, label, view->ncols);
2799 if (err)
2800 return err;
2801 if (view_needs_focus_indication(view))
2802 wstandout(view->window);
2803 waddwstr(view->window, wline);
2804 if (view_needs_focus_indication(view))
2805 wstandend(view->window);
2806 free(wline);
2807 wline = NULL;
2808 if (width < view->ncols)
2809 waddch(view->window, '\n');
2810 if (--limit <= 0)
2811 return NULL;
2812 err = format_line(&wline, &width, parent_path, view->ncols);
2813 if (err)
2814 return err;
2815 waddwstr(view->window, wline);
2816 free(wline);
2817 wline = NULL;
2818 if (width < view->ncols)
2819 waddch(view->window, '\n');
2820 if (--limit <= 0)
2821 return NULL;
2822 waddch(view->window, '\n');
2823 if (--limit <= 0)
2824 return NULL;
2826 te = SIMPLEQ_FIRST(&entries->head);
2827 if (*first_displayed_entry == NULL) {
2828 if (selected == 0) {
2829 if (view->focussed)
2830 wstandout(view->window);
2831 *selected_entry = NULL;
2833 waddstr(view->window, " ..\n"); /* parent directory */
2834 if (selected == 0 && view->focussed)
2835 wstandend(view->window);
2836 (*ndisplayed)++;
2837 if (--limit <= 0)
2838 return NULL;
2839 n = 1;
2840 } else {
2841 n = 0;
2842 while (te != *first_displayed_entry)
2843 te = SIMPLEQ_NEXT(te, entry);
2846 while (te) {
2847 char *line = NULL, *id_str = NULL;
2849 if (show_ids) {
2850 err = got_object_id_str(&id_str, te->id);
2851 if (err)
2852 return got_error_from_errno();
2854 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2855 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2856 free(id_str);
2857 return got_error_from_errno();
2859 free(id_str);
2860 err = format_line(&wline, &width, line, view->ncols);
2861 if (err) {
2862 free(line);
2863 break;
2865 if (n == selected) {
2866 if (view->focussed)
2867 wstandout(view->window);
2868 *selected_entry = te;
2870 waddwstr(view->window, wline);
2871 if (width < view->ncols)
2872 waddch(view->window, '\n');
2873 if (n == selected && view->focussed)
2874 wstandend(view->window);
2875 free(line);
2876 free(wline);
2877 wline = NULL;
2878 n++;
2879 (*ndisplayed)++;
2880 *last_displayed_entry = te;
2881 if (--limit <= 0)
2882 break;
2883 te = SIMPLEQ_NEXT(te, entry);
2886 return err;
2889 static void
2890 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2891 const struct got_tree_entries *entries, int isroot)
2893 struct got_tree_entry *te, *prev;
2894 int i;
2896 if (*first_displayed_entry == NULL)
2897 return;
2899 te = SIMPLEQ_FIRST(&entries->head);
2900 if (*first_displayed_entry == te) {
2901 if (!isroot)
2902 *first_displayed_entry = NULL;
2903 return;
2906 /* XXX this is stupid... switch to TAILQ? */
2907 for (i = 0; i < maxscroll; i++) {
2908 while (te != *first_displayed_entry) {
2909 prev = te;
2910 te = SIMPLEQ_NEXT(te, entry);
2912 *first_displayed_entry = prev;
2913 te = SIMPLEQ_FIRST(&entries->head);
2915 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2916 *first_displayed_entry = NULL;
2919 static void
2920 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2921 struct got_tree_entry *last_displayed_entry,
2922 const struct got_tree_entries *entries)
2924 struct got_tree_entry *next;
2925 int n = 0;
2927 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2928 return;
2930 if (*first_displayed_entry)
2931 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2932 else
2933 next = SIMPLEQ_FIRST(&entries->head);
2934 while (next) {
2935 *first_displayed_entry = next;
2936 if (++n >= maxscroll)
2937 break;
2938 next = SIMPLEQ_NEXT(next, entry);
2942 static const struct got_error *
2943 tree_entry_path(char **path, struct tog_parent_trees *parents,
2944 struct got_tree_entry *te)
2946 const struct got_error *err = NULL;
2947 struct tog_parent_tree *pt;
2948 size_t len = 2; /* for leading slash and NUL */
2950 TAILQ_FOREACH(pt, parents, entry)
2951 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2952 if (te)
2953 len += strlen(te->name);
2955 *path = calloc(1, len);
2956 if (path == NULL)
2957 return got_error_from_errno();
2959 (*path)[0] = '/';
2960 pt = TAILQ_LAST(parents, tog_parent_trees);
2961 while (pt) {
2962 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2963 err = got_error(GOT_ERR_NO_SPACE);
2964 goto done;
2966 if (strlcat(*path, "/", len) >= len) {
2967 err = got_error(GOT_ERR_NO_SPACE);
2968 goto done;
2970 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2972 if (te) {
2973 if (strlcat(*path, te->name, len) >= len) {
2974 err = got_error(GOT_ERR_NO_SPACE);
2975 goto done;
2978 done:
2979 if (err) {
2980 free(*path);
2981 *path = NULL;
2983 return err;
2986 static const struct got_error *
2987 blame_tree_entry(struct tog_view **new_view, int begin_x,
2988 struct got_tree_entry *te, struct tog_parent_trees *parents,
2989 struct got_object_id *commit_id, struct got_repository *repo)
2991 const struct got_error *err = NULL;
2992 char *path;
2993 struct tog_view *blame_view;
2995 err = tree_entry_path(&path, parents, te);
2996 if (err)
2997 return err;
2999 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3000 if (blame_view == NULL)
3001 return got_error_from_errno();
3003 err = open_blame_view(blame_view, path, commit_id, repo);
3004 if (err) {
3005 view_close(blame_view);
3006 free(path);
3007 } else
3008 *new_view = blame_view;
3009 return err;
3012 static const struct got_error *
3013 log_tree_entry(struct tog_view **new_view, int begin_x,
3014 struct got_tree_entry *te, struct tog_parent_trees *parents,
3015 struct got_object_id *commit_id, struct got_repository *repo)
3017 struct tog_view *log_view;
3018 const struct got_error *err = NULL;
3019 char *path;
3021 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3022 if (log_view == NULL)
3023 return got_error_from_errno();
3025 err = tree_entry_path(&path, parents, te);
3026 if (err)
3027 return err;
3029 err = open_log_view(log_view, commit_id, repo, path, 0);
3030 if (err)
3031 view_close(log_view);
3032 else
3033 *new_view = log_view;
3034 free(path);
3035 return err;
3038 static const struct got_error *
3039 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3040 struct got_object_id *commit_id, struct got_repository *repo)
3042 const struct got_error *err = NULL;
3043 char *commit_id_str = NULL;
3044 struct tog_tree_view_state *s = &view->state.tree;
3046 TAILQ_INIT(&s->parents);
3048 err = got_object_id_str(&commit_id_str, commit_id);
3049 if (err != NULL)
3050 goto done;
3052 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3053 err = got_error_from_errno();
3054 goto done;
3057 s->root = s->tree = root;
3058 s->entries = got_object_tree_get_entries(root);
3059 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3060 s->commit_id = got_object_id_dup(commit_id);
3061 if (s->commit_id == NULL) {
3062 err = got_error_from_errno();
3063 goto done;
3065 s->repo = repo;
3067 view->show = show_tree_view;
3068 view->input = input_tree_view;
3069 view->close = close_tree_view;
3070 done:
3071 free(commit_id_str);
3072 if (err) {
3073 free(s->tree_label);
3074 s->tree_label = NULL;
3076 return err;
3079 static const struct got_error *
3080 close_tree_view(struct tog_view *view)
3082 struct tog_tree_view_state *s = &view->state.tree;
3084 free(s->tree_label);
3085 s->tree_label = NULL;
3086 free(s->commit_id);
3087 s->commit_id = NULL;
3088 while (!TAILQ_EMPTY(&s->parents)) {
3089 struct tog_parent_tree *parent;
3090 parent = TAILQ_FIRST(&s->parents);
3091 TAILQ_REMOVE(&s->parents, parent, entry);
3092 free(parent);
3095 if (s->tree != s->root)
3096 got_object_tree_close(s->tree);
3097 got_object_tree_close(s->root);
3099 return NULL;
3102 static const struct got_error *
3103 show_tree_view(struct tog_view *view)
3105 const struct got_error *err = NULL;
3106 struct tog_tree_view_state *s = &view->state.tree;
3107 char *parent_path;
3109 err = tree_entry_path(&parent_path, &s->parents, NULL);
3110 if (err)
3111 return err;
3113 err = draw_tree_entries(view, &s->first_displayed_entry,
3114 &s->last_displayed_entry, &s->selected_entry,
3115 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3116 s->entries, s->selected, view->nlines, s->tree == s->root);
3117 free(parent_path);
3119 view_vborder(view);
3120 return err;
3123 static const struct got_error *
3124 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3125 struct tog_view **focus_view, struct tog_view *view, int ch)
3127 const struct got_error *err = NULL;
3128 struct tog_tree_view_state *s = &view->state.tree;
3129 struct tog_view *log_view;
3130 int begin_x = 0;
3132 switch (ch) {
3133 case 'i':
3134 s->show_ids = !s->show_ids;
3135 break;
3136 case 'l':
3137 if (!s->selected_entry)
3138 break;
3139 if (view_is_parent_view(view))
3140 begin_x = view_split_begin_x(view->begin_x);
3141 err = log_tree_entry(&log_view, begin_x,
3142 s->selected_entry, &s->parents,
3143 s->commit_id, s->repo);
3144 if (view_is_parent_view(view)) {
3145 err = view_close_child(view);
3146 if (err)
3147 return err;
3148 err = view_set_child(view, log_view);
3149 if (err) {
3150 view_close(log_view);
3151 break;
3153 *focus_view = log_view;
3154 view->child_focussed = 1;
3155 } else
3156 *new_view = log_view;
3157 break;
3158 case 'k':
3159 case KEY_UP:
3160 if (s->selected > 0)
3161 s->selected--;
3162 if (s->selected > 0)
3163 break;
3164 tree_scroll_up(&s->first_displayed_entry, 1,
3165 s->entries, s->tree == s->root);
3166 break;
3167 case KEY_PPAGE:
3168 if (SIMPLEQ_FIRST(&s->entries->head) ==
3169 s->first_displayed_entry) {
3170 if (s->tree != s->root)
3171 s->first_displayed_entry = NULL;
3172 s->selected = 0;
3173 break;
3175 tree_scroll_up(&s->first_displayed_entry,
3176 view->nlines, s->entries,
3177 s->tree == s->root);
3178 break;
3179 case 'j':
3180 case KEY_DOWN:
3181 if (s->selected < s->ndisplayed - 1) {
3182 s->selected++;
3183 break;
3185 tree_scroll_down(&s->first_displayed_entry, 1,
3186 s->last_displayed_entry, s->entries);
3187 break;
3188 case KEY_NPAGE:
3189 tree_scroll_down(&s->first_displayed_entry,
3190 view->nlines, s->last_displayed_entry,
3191 s->entries);
3192 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3193 entry))
3194 break;
3195 /* can't scroll any further; move cursor down */
3196 if (s->selected < s->ndisplayed - 1)
3197 s->selected = s->ndisplayed - 1;
3198 break;
3199 case KEY_ENTER:
3200 case '\r':
3201 if (s->selected_entry == NULL) {
3202 struct tog_parent_tree *parent;
3203 case KEY_BACKSPACE:
3204 /* user selected '..' */
3205 if (s->tree == s->root)
3206 break;
3207 parent = TAILQ_FIRST(&s->parents);
3208 TAILQ_REMOVE(&s->parents, parent,
3209 entry);
3210 got_object_tree_close(s->tree);
3211 s->tree = parent->tree;
3212 s->entries =
3213 got_object_tree_get_entries(s->tree);
3214 s->first_displayed_entry =
3215 parent->first_displayed_entry;
3216 s->selected_entry =
3217 parent->selected_entry;
3218 s->selected = parent->selected;
3219 free(parent);
3220 } else if (S_ISDIR(s->selected_entry->mode)) {
3221 struct tog_parent_tree *parent;
3222 struct got_tree_object *child;
3223 err = got_object_open_as_tree(&child,
3224 s->repo, s->selected_entry->id);
3225 if (err)
3226 break;
3227 parent = calloc(1, sizeof(*parent));
3228 if (parent == NULL) {
3229 err = got_error_from_errno();
3230 break;
3232 parent->tree = s->tree;
3233 parent->first_displayed_entry =
3234 s->first_displayed_entry;
3235 parent->selected_entry = s->selected_entry;
3236 parent->selected = s->selected;
3237 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3238 s->tree = child;
3239 s->entries =
3240 got_object_tree_get_entries(s->tree);
3241 s->selected = 0;
3242 s->first_displayed_entry = NULL;
3243 } else if (S_ISREG(s->selected_entry->mode)) {
3244 struct tog_view *blame_view;
3245 int begin_x = view_is_parent_view(view) ?
3246 view_split_begin_x(view->begin_x) : 0;
3248 err = blame_tree_entry(&blame_view, begin_x,
3249 s->selected_entry, &s->parents, s->commit_id,
3250 s->repo);
3251 if (err)
3252 break;
3253 if (view_is_parent_view(view)) {
3254 err = view_close_child(view);
3255 if (err)
3256 return err;
3257 err = view_set_child(view, blame_view);
3258 if (err) {
3259 view_close(blame_view);
3260 break;
3262 *focus_view = blame_view;
3263 view->child_focussed = 1;
3264 } else
3265 *new_view = blame_view;
3267 break;
3268 case KEY_RESIZE:
3269 if (s->selected > view->nlines)
3270 s->selected = s->ndisplayed - 1;
3271 break;
3272 default:
3273 break;
3276 return err;
3279 __dead static void
3280 usage_tree(void)
3282 endwin();
3283 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3284 getprogname());
3285 exit(1);
3288 static const struct got_error *
3289 cmd_tree(int argc, char *argv[])
3291 const struct got_error *error;
3292 struct got_repository *repo = NULL;
3293 char *repo_path = NULL;
3294 struct got_object_id *commit_id = NULL;
3295 char *commit_id_arg = NULL;
3296 struct got_commit_object *commit = NULL;
3297 struct got_tree_object *tree = NULL;
3298 int ch;
3299 struct tog_view *view;
3301 #ifndef PROFILE
3302 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3303 == -1)
3304 err(1, "pledge");
3305 #endif
3307 while ((ch = getopt(argc, argv, "c:")) != -1) {
3308 switch (ch) {
3309 case 'c':
3310 commit_id_arg = optarg;
3311 break;
3312 default:
3313 usage();
3314 /* NOTREACHED */
3318 argc -= optind;
3319 argv += optind;
3321 if (argc == 0) {
3322 repo_path = getcwd(NULL, 0);
3323 if (repo_path == NULL)
3324 return got_error_from_errno();
3325 } else if (argc == 1) {
3326 repo_path = realpath(argv[0], NULL);
3327 if (repo_path == NULL)
3328 return got_error_from_errno();
3329 } else
3330 usage_log();
3332 error = got_repo_open(&repo, repo_path);
3333 free(repo_path);
3334 if (error != NULL)
3335 return error;
3337 if (commit_id_arg == NULL) {
3338 error = get_head_commit_id(&commit_id, repo);
3339 if (error != NULL)
3340 goto done;
3341 } else {
3342 struct got_object *obj;
3343 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3344 if (error == NULL) {
3345 commit_id = got_object_id_dup(got_object_get_id(obj));
3346 if (commit_id == NULL)
3347 error = got_error_from_errno();
3350 if (error != NULL)
3351 goto done;
3353 error = got_object_open_as_commit(&commit, repo, commit_id);
3354 if (error != NULL)
3355 goto done;
3357 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3358 if (error != NULL)
3359 goto done;
3361 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3362 if (view == NULL) {
3363 error = got_error_from_errno();
3364 goto done;
3366 error = open_tree_view(view, tree, commit_id, repo);
3367 if (error)
3368 goto done;
3369 error = view_loop(view);
3370 done:
3371 free(commit_id);
3372 if (commit)
3373 got_object_commit_close(commit);
3374 if (tree)
3375 got_object_tree_close(tree);
3376 if (repo)
3377 got_repo_close(repo);
3378 return error;
3380 static void
3381 init_curses(void)
3383 initscr();
3384 cbreak();
3385 halfdelay(1); /* Do fast refresh while initial view is loading. */
3386 noecho();
3387 nonl();
3388 intrflush(stdscr, FALSE);
3389 keypad(stdscr, TRUE);
3390 curs_set(0);
3393 __dead static void
3394 usage(void)
3396 int i;
3398 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3399 "Available commands:\n", getprogname());
3400 for (i = 0; i < nitems(tog_commands); i++) {
3401 struct tog_cmd *cmd = &tog_commands[i];
3402 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3404 exit(1);
3407 static char **
3408 make_argv(const char *arg0, const char *arg1)
3410 char **argv;
3411 int argc = (arg1 == NULL ? 1 : 2);
3413 argv = calloc(argc, sizeof(char *));
3414 if (argv == NULL)
3415 err(1, "calloc");
3416 argv[0] = strdup(arg0);
3417 if (argv[0] == NULL)
3418 err(1, "calloc");
3419 if (arg1) {
3420 argv[1] = strdup(arg1);
3421 if (argv[1] == NULL)
3422 err(1, "calloc");
3425 return argv;
3428 int
3429 main(int argc, char *argv[])
3431 const struct got_error *error = NULL;
3432 struct tog_cmd *cmd = NULL;
3433 int ch, hflag = 0;
3434 char **cmd_argv = NULL;
3436 setlocale(LC_ALL, "");
3438 while ((ch = getopt(argc, argv, "h")) != -1) {
3439 switch (ch) {
3440 case 'h':
3441 hflag = 1;
3442 break;
3443 default:
3444 usage();
3445 /* NOTREACHED */
3449 argc -= optind;
3450 argv += optind;
3451 optind = 0;
3452 optreset = 1;
3454 if (argc == 0) {
3455 if (hflag)
3456 usage();
3457 /* Build an argument vector which runs a default command. */
3458 cmd = &tog_commands[0];
3459 cmd_argv = make_argv(cmd->name, NULL);
3460 argc = 1;
3461 } else {
3462 int i;
3464 /* Did the user specific a command? */
3465 for (i = 0; i < nitems(tog_commands); i++) {
3466 if (strncmp(tog_commands[i].name, argv[0],
3467 strlen(argv[0])) == 0) {
3468 cmd = &tog_commands[i];
3469 if (hflag)
3470 tog_commands[i].cmd_usage();
3471 break;
3474 if (cmd == NULL) {
3475 /* Did the user specify a repository? */
3476 char *repo_path = realpath(argv[0], NULL);
3477 if (repo_path) {
3478 struct got_repository *repo;
3479 error = got_repo_open(&repo, repo_path);
3480 if (error == NULL)
3481 got_repo_close(repo);
3482 } else
3483 error = got_error_from_errno();
3484 if (error) {
3485 if (hflag) {
3486 fprintf(stderr, "%s: '%s' is not a "
3487 "known command\n", getprogname(),
3488 argv[0]);
3489 usage();
3491 fprintf(stderr, "%s: '%s' is neither a known "
3492 "command nor a path to a repository\n",
3493 getprogname(), argv[0]);
3494 free(repo_path);
3495 return 1;
3497 cmd = &tog_commands[0];
3498 cmd_argv = make_argv(cmd->name, repo_path);
3499 argc = 2;
3500 free(repo_path);
3504 init_curses();
3506 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3507 if (error)
3508 goto done;
3509 done:
3510 endwin();
3511 free(cmd_argv);
3512 if (error)
3513 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3514 return 0;