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 **last_displayed_entry;
135 struct commit_queue_entry **selected_entry;
136 int *selected;
137 };
139 struct tog_log_view_state {
140 struct commit_queue commits;
141 struct commit_queue_entry *first_displayed_entry;
142 struct commit_queue_entry *last_displayed_entry;
143 struct commit_queue_entry *selected_entry;
144 int selected;
145 char *in_repo_path;
146 struct got_repository *repo;
147 struct got_object_id *start_id;
148 sig_atomic_t quit;
149 pthread_t thread;
150 struct tog_log_thread_args thread_args;
151 };
153 struct tog_blame_cb_args {
154 struct tog_blame_line *lines; /* one per line */
155 int nlines;
157 struct tog_view *view;
158 struct got_object_id *commit_id;
159 FILE *f;
160 const char *path;
161 int *first_displayed_line;
162 int *last_displayed_line;
163 int *selected_line;
164 int *quit;
165 int *eof;
166 };
168 struct tog_blame_thread_args {
169 const char *path;
170 struct got_repository *repo;
171 struct tog_blame_cb_args *cb_args;
172 int *complete;
173 };
175 struct tog_blame {
176 FILE *f;
177 size_t filesize;
178 struct tog_blame_line *lines;
179 size_t nlines;
180 pthread_t thread;
181 struct tog_blame_thread_args thread_args;
182 struct tog_blame_cb_args cb_args;
183 const char *path;
184 };
186 struct tog_blame_view_state {
187 int first_displayed_line;
188 int last_displayed_line;
189 int selected_line;
190 int blame_complete;
191 int eof;
192 int done;
193 struct got_object_id_queue blamed_commits;
194 struct got_object_qid *blamed_commit;
195 char *path;
196 struct got_repository *repo;
197 struct got_object_id *commit_id;
198 struct tog_blame blame;
199 };
201 struct tog_parent_tree {
202 TAILQ_ENTRY(tog_parent_tree) entry;
203 struct got_tree_object *tree;
204 struct got_tree_entry *first_displayed_entry;
205 struct got_tree_entry *selected_entry;
206 int selected;
207 };
209 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
211 struct tog_tree_view_state {
212 char *tree_label;
213 struct got_tree_object *root;
214 struct got_tree_object *tree;
215 const struct got_tree_entries *entries;
216 struct got_tree_entry *first_displayed_entry;
217 struct got_tree_entry *last_displayed_entry;
218 struct got_tree_entry *selected_entry;
219 int nentries, ndisplayed, selected, show_ids;
220 struct tog_parent_trees parents;
221 struct got_object_id *commit_id;
222 struct got_repository *repo;
223 };
225 /*
226 * We implement two types of views: parent views and child views.
228 * The 'Tab' key switches between a parent view and its child view.
229 * Child views are shown side-by-side to their parent view, provided
230 * there is enough screen estate.
232 * When a new view is opened from within a parent view, this new view
233 * becomes a child view of the parent view, replacing any existing child.
235 * When a new view is opened from within a child view, this new view
236 * becomes a parent view which will obscure the views below until the
237 * user quits the new parent view by typing 'q'.
239 * This list of views contains parent views only.
240 * Child views are only pointed to by their parent view.
241 */
242 TAILQ_HEAD(tog_view_list_head, tog_view);
244 struct tog_view {
245 TAILQ_ENTRY(tog_view) entry;
246 WINDOW *window;
247 PANEL *panel;
248 int nlines, ncols, begin_y, begin_x;
249 int lines, cols; /* copies of LINES and COLS */
250 int focussed;
251 struct tog_view *parent;
252 struct tog_view *child;
253 int child_focussed;
255 /* type-specific state */
256 enum tog_view_type type;
257 union {
258 struct tog_diff_view_state diff;
259 struct tog_log_view_state log;
260 struct tog_blame_view_state blame;
261 struct tog_tree_view_state tree;
262 } state;
264 const struct got_error *(*show)(struct tog_view *);
265 const struct got_error *(*input)(struct tog_view **,
266 struct tog_view **, struct tog_view**, struct tog_view *, int);
267 const struct got_error *(*close)(struct tog_view *);
268 };
270 static const struct got_error *open_diff_view(struct tog_view *,
271 struct got_object *, struct got_object *, struct got_repository *);
272 static const struct got_error *show_diff_view(struct tog_view *);
273 static const struct got_error *input_diff_view(struct tog_view **,
274 struct tog_view **, struct tog_view **, struct tog_view *, int);
275 static const struct got_error* close_diff_view(struct tog_view *);
277 static const struct got_error *open_log_view(struct tog_view *,
278 struct got_object_id *, struct got_repository *, const char *, int);
279 static const struct got_error * show_log_view(struct tog_view *);
280 static const struct got_error *input_log_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error *close_log_view(struct tog_view *);
284 static const struct got_error *open_blame_view(struct tog_view *, char *,
285 struct got_object_id *, struct got_repository *);
286 static const struct got_error *show_blame_view(struct tog_view *);
287 static const struct got_error *input_blame_view(struct tog_view **,
288 struct tog_view **, struct tog_view **, struct tog_view *, int);
289 static const struct got_error *close_blame_view(struct tog_view *);
291 static const struct got_error *open_tree_view(struct tog_view *,
292 struct got_tree_object *, struct got_object_id *, struct got_repository *);
293 static const struct got_error *show_tree_view(struct tog_view *);
294 static const struct got_error *input_tree_view(struct tog_view **,
295 struct tog_view **, struct tog_view **, struct tog_view *, int);
296 static const struct got_error *close_tree_view(struct tog_view *);
298 static const struct got_error *
299 view_close(struct tog_view *view)
301 const struct got_error *err = NULL;
303 if (view->child) {
304 view_close(view->child);
305 view->child = NULL;
307 if (view->close)
308 err = view->close(view);
309 if (view->panel)
310 del_panel(view->panel);
311 if (view->window)
312 delwin(view->window);
313 free(view);
314 return err;
317 static struct tog_view *
318 view_open(int nlines, int ncols, int begin_y, int begin_x,
319 enum tog_view_type type)
321 struct tog_view *view = calloc(1, sizeof(*view));
323 if (view == NULL)
324 return NULL;
326 view->type = type;
327 view->lines = LINES;
328 view->cols = COLS;
329 view->nlines = nlines ? nlines : LINES - begin_y;
330 view->ncols = ncols ? ncols : COLS - begin_x;
331 view->begin_y = begin_y;
332 view->begin_x = begin_x;
333 view->window = newwin(nlines, ncols, begin_y, begin_x);
334 if (view->window == NULL) {
335 view_close(view);
336 return NULL;
338 view->panel = new_panel(view->window);
339 if (view->panel == NULL ||
340 set_panel_userptr(view->panel, view) != OK) {
341 view_close(view);
342 return NULL;
345 keypad(view->window, TRUE);
346 return view;
349 static int
350 view_split_begin_x(int begin_x)
352 if (begin_x > 0 || COLS < 120)
353 return 0;
354 return (COLS - MAX(COLS / 2, 80));
357 static const struct got_error *view_resize(struct tog_view *);
359 static const struct got_error *
360 view_splitscreen(struct tog_view *view)
362 const struct got_error *err = NULL;
364 view->begin_y = 0;
365 view->begin_x = view_split_begin_x(0);
366 view->nlines = LINES;
367 view->ncols = COLS - view->begin_x;
368 view->lines = LINES;
369 view->cols = COLS;
370 err = view_resize(view);
371 if (err)
372 return err;
374 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
375 return got_error_from_errno();
377 return NULL;
380 static const struct got_error *
381 view_fullscreen(struct tog_view *view)
383 const struct got_error *err = NULL;
385 view->begin_x = 0;
386 view->begin_y = 0;
387 view->nlines = LINES;
388 view->ncols = COLS;
389 view->lines = LINES;
390 view->cols = COLS;
391 err = view_resize(view);
392 if (err)
393 return err;
395 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
396 return got_error_from_errno();
398 return NULL;
401 static int
402 view_is_parent_view(struct tog_view *view)
404 return view->parent == NULL;
407 static const struct got_error *
408 view_resize(struct tog_view *view)
410 int nlines, ncols;
412 if (view->lines > LINES)
413 nlines = view->nlines - (view->lines - LINES);
414 else
415 nlines = view->nlines + (LINES - view->lines);
417 if (view->cols > COLS)
418 ncols = view->ncols - (view->cols - COLS);
419 else
420 ncols = view->ncols + (COLS - view->cols);
422 if (wresize(view->window, nlines, ncols) == ERR)
423 return got_error_from_errno();
424 replace_panel(view->panel, view->window);
426 view->nlines = nlines;
427 view->ncols = ncols;
428 view->lines = LINES;
429 view->cols = COLS;
431 if (view->child) {
432 view->child->begin_x = view_split_begin_x(view->begin_x);
433 if (view->child->begin_x == 0) {
434 view_fullscreen(view->child);
435 if (view->child->focussed)
436 show_panel(view->child->panel);
437 else
438 show_panel(view->panel);
439 } else {
440 view_splitscreen(view->child);
441 show_panel(view->child->panel);
445 return NULL;
448 static const struct got_error *
449 view_close_child(struct tog_view *view)
451 const struct got_error *err = NULL;
453 if (view->child == NULL)
454 return NULL;
456 err = view_close(view->child);
457 view->child = NULL;
458 return err;
461 static const struct got_error *
462 view_set_child(struct tog_view *view, struct tog_view *child)
464 const struct got_error *err = NULL;
466 view->child = child;
467 child->parent = view;
468 return err;
471 static int
472 view_is_splitscreen(struct tog_view *view)
474 return !view_is_parent_view(view) && view->begin_x > 0;
477 static const struct got_error *
478 view_input(struct tog_view **new, struct tog_view **dead,
479 struct tog_view **focus, int *done, struct tog_view *view,
480 struct tog_view_list_head *views)
482 const struct got_error *err = NULL;
483 struct tog_view *v;
484 int ch, errcode;
486 *new = NULL;
487 *dead = NULL;
488 *focus = NULL;
490 nodelay(stdscr, FALSE);
491 /* Allow threads to make progress while we are waiting for input. */
492 errcode = pthread_mutex_unlock(&tog_mutex);
493 if (errcode)
494 return got_error_set_errno(errcode);
495 ch = wgetch(view->window);
496 errcode = pthread_mutex_lock(&tog_mutex);
497 if (errcode)
498 return got_error_set_errno(errcode);
499 nodelay(stdscr, TRUE);
500 switch (ch) {
501 case ERR:
502 break;
503 case '\t':
504 if (view->child) {
505 *focus = view->child;
506 view->child_focussed = 1;
507 } else if (view->parent) {
508 *focus = view->parent;
509 view->parent->child_focussed = 0;
511 break;
512 case 'q':
513 err = view->input(new, dead, focus, view, ch);
514 *dead = view;
515 break;
516 case 'Q':
517 *done = 1;
518 break;
519 case 'f':
520 if (view_is_parent_view(view)) {
521 if (view->child == NULL)
522 break;
523 if (view_is_splitscreen(view->child)) {
524 *focus = view->child;
525 view->child_focussed = 1;
526 err = view_fullscreen(view->child);
527 } else
528 err = view_splitscreen(view->child);
529 if (err)
530 break;
531 err = view->child->input(new, dead, focus,
532 view->child, KEY_RESIZE);
533 } else {
534 if (view_is_splitscreen(view)) {
535 *focus = view;
536 view->parent->child_focussed = 1;
537 err = view_fullscreen(view);
538 } else {
539 err = view_splitscreen(view);
541 if (err)
542 break;
543 err = view->input(new, dead, focus, view,
544 KEY_RESIZE);
546 break;
547 case KEY_RESIZE:
548 TAILQ_FOREACH(v, views, entry) {
549 err = view_resize(v);
550 if (err)
551 return err;
552 err = v->input(new, dead, focus, v, ch);
553 if (err)
554 return err;
556 break;
557 default:
558 err = view->input(new, dead, focus, view, ch);
559 break;
562 return err;
565 void
566 view_vborder(struct tog_view *view)
568 PANEL *panel;
569 struct tog_view *view_above;
571 if (view->parent)
572 return view_vborder(view->parent);
574 panel = panel_above(view->panel);
575 if (panel == NULL)
576 return;
578 view_above = panel_userptr(panel);
579 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
580 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
583 int
584 view_needs_focus_indication(struct tog_view *view)
586 if (view_is_parent_view(view)) {
587 if (view->child == NULL || view->child_focussed)
588 return 0;
589 if (!view_is_splitscreen(view->child))
590 return 0;
591 } else if (!view_is_splitscreen(view))
592 return 0;
594 return view->focussed;
597 static const struct got_error *
598 view_loop(struct tog_view *view)
600 const struct got_error *err = NULL;
601 struct tog_view_list_head views;
602 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
603 int fast_refresh = 10;
604 int done = 0, errcode;
606 errcode = pthread_mutex_lock(&tog_mutex);
607 if (errcode)
608 return got_error_set_errno(errcode);
610 TAILQ_INIT(&views);
611 TAILQ_INSERT_HEAD(&views, view, entry);
613 main_view = view;
614 view->focussed = 1;
615 err = view->show(view);
616 if (err)
617 return err;
618 update_panels();
619 doupdate();
620 while (!TAILQ_EMPTY(&views) && !done) {
621 /* Refresh fast during initialization, then become slower. */
622 if (fast_refresh && fast_refresh-- == 0)
623 halfdelay(10); /* switch to once per second */
625 err = view_input(&new_view, &dead_view, &focus_view, &done,
626 view, &views);
627 if (err)
628 break;
629 if (dead_view) {
630 struct tog_view *prev = NULL;
632 if (view_is_parent_view(dead_view))
633 prev = TAILQ_PREV(dead_view,
634 tog_view_list_head, entry);
635 else
636 prev = view->parent;
638 if (dead_view->parent)
639 dead_view->parent->child = NULL;
640 else
641 TAILQ_REMOVE(&views, dead_view, entry);
643 err = view_close(dead_view);
644 if (err || dead_view == main_view)
645 goto done;
647 if (view == dead_view) {
648 if (focus_view)
649 view = focus_view;
650 else if (prev)
651 view = prev;
652 else if (!TAILQ_EMPTY(&views))
653 view = TAILQ_LAST(&views,
654 tog_view_list_head);
655 else
656 view = NULL;
657 if (view) {
658 if (view->child && view->child_focussed)
659 focus_view = view->child;
660 else
661 focus_view = view;
665 if (new_view) {
666 struct tog_view *v, *t;
667 /* Only allow one parent view per type. */
668 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
669 if (v->type != new_view->type)
670 continue;
671 TAILQ_REMOVE(&views, v, entry);
672 err = view_close(v);
673 if (err)
674 goto done;
675 if (v == view)
676 view = new_view;
677 break;
679 TAILQ_INSERT_TAIL(&views, new_view, entry);
680 if (focus_view == NULL)
681 focus_view = new_view;
683 if (focus_view) {
684 show_panel(focus_view->panel);
685 if (view)
686 view->focussed = 0;
687 focus_view->focussed = 1;
688 view = focus_view;
689 if (new_view)
690 show_panel(new_view->panel);
691 if (view->child && view_is_splitscreen(view->child))
692 show_panel(view->child->panel);
694 if (view) {
695 if (focus_view == NULL) {
696 view->focussed = 1;
697 show_panel(view->panel);
698 if (view->child && view_is_splitscreen(view->child))
699 show_panel(view->child->panel);
700 focus_view = view;
702 if (view->parent) {
703 err = view->parent->show(view->parent);
704 if (err)
705 goto done;
707 err = view->show(view);
708 if (err)
709 goto done;
710 if (view->child) {
711 err = view->child->show(view->child);
712 if (err)
713 goto done;
715 update_panels();
716 doupdate();
719 done:
720 while (!TAILQ_EMPTY(&views)) {
721 view = TAILQ_FIRST(&views);
722 TAILQ_REMOVE(&views, view, entry);
723 view_close(view);
726 errcode = pthread_mutex_unlock(&tog_mutex);
727 if (errcode)
728 return got_error_set_errno(errcode);
730 return err;
733 __dead static void
734 usage_log(void)
736 endwin();
737 fprintf(stderr,
738 "usage: %s log [-c commit] [-r repository-path] [path]\n",
739 getprogname());
740 exit(1);
743 /* Create newly allocated wide-character string equivalent to a byte string. */
744 static const struct got_error *
745 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
747 char *vis = NULL;
748 const struct got_error *err = NULL;
750 *ws = NULL;
751 *wlen = mbstowcs(NULL, s, 0);
752 if (*wlen == (size_t)-1) {
753 int vislen;
754 if (errno != EILSEQ)
755 return got_error_from_errno();
757 /* byte string invalid in current encoding; try to "fix" it */
758 err = got_mbsavis(&vis, &vislen, s);
759 if (err)
760 return err;
761 *wlen = mbstowcs(NULL, vis, 0);
762 if (*wlen == (size_t)-1) {
763 err = got_error_from_errno(); /* give up */
764 goto done;
768 *ws = calloc(*wlen + 1, sizeof(*ws));
769 if (*ws == NULL) {
770 err = got_error_from_errno();
771 goto done;
774 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
775 err = got_error_from_errno();
776 done:
777 free(vis);
778 if (err) {
779 free(*ws);
780 *ws = NULL;
781 *wlen = 0;
783 return err;
786 /* Format a line for display, ensuring that it won't overflow a width limit. */
787 static const struct got_error *
788 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
790 const struct got_error *err = NULL;
791 int cols = 0;
792 wchar_t *wline = NULL;
793 size_t wlen;
794 int i;
796 *wlinep = NULL;
797 *widthp = 0;
799 err = mbs2ws(&wline, &wlen, line);
800 if (err)
801 return err;
803 i = 0;
804 while (i < wlen && cols < wlimit) {
805 int width = wcwidth(wline[i]);
806 switch (width) {
807 case 0:
808 i++;
809 break;
810 case 1:
811 case 2:
812 if (cols + width <= wlimit)
813 cols += width;
814 i++;
815 break;
816 case -1:
817 if (wline[i] == L'\t')
818 cols += TABSIZE - ((cols + 1) % TABSIZE);
819 i++;
820 break;
821 default:
822 err = got_error_from_errno();
823 goto done;
826 wline[i] = L'\0';
827 if (widthp)
828 *widthp = cols;
829 done:
830 if (err)
831 free(wline);
832 else
833 *wlinep = wline;
834 return err;
837 static const struct got_error *
838 draw_commit(struct tog_view *view, struct got_commit_object *commit,
839 struct got_object_id *id)
841 const struct got_error *err = NULL;
842 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
843 char *logmsg0 = NULL, *logmsg = NULL;
844 char *author0 = NULL, *author = NULL;
845 wchar_t *wlogmsg = NULL, *wauthor = NULL;
846 int author_width, logmsg_width;
847 char *newline, *smallerthan;
848 char *line = NULL;
849 int col, limit;
850 static const size_t date_display_cols = 9;
851 static const size_t author_display_cols = 16;
852 const int avail = view->ncols;
854 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
855 &commit->tm_committer) >= sizeof(datebuf))
856 return got_error(GOT_ERR_NO_SPACE);
858 if (avail < date_display_cols)
859 limit = MIN(sizeof(datebuf) - 1, avail);
860 else
861 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
862 waddnstr(view->window, datebuf, limit);
863 col = limit + 1;
864 if (col > avail)
865 goto done;
867 author0 = strdup(commit->author);
868 if (author0 == NULL) {
869 err = got_error_from_errno();
870 goto done;
872 author = author0;
873 smallerthan = strchr(author, '<');
874 if (smallerthan)
875 *smallerthan = '\0';
876 else {
877 char *at = strchr(author, '@');
878 if (at)
879 *at = '\0';
881 limit = avail - col;
882 err = format_line(&wauthor, &author_width, author, limit);
883 if (err)
884 goto done;
885 waddwstr(view->window, wauthor);
886 col += author_width;
887 while (col <= avail && author_width < author_display_cols + 1) {
888 waddch(view->window, ' ');
889 col++;
890 author_width++;
892 if (col > avail)
893 goto done;
895 logmsg0 = strdup(commit->logmsg);
896 if (logmsg0 == NULL) {
897 err = got_error_from_errno();
898 goto done;
900 logmsg = logmsg0;
901 while (*logmsg == '\n')
902 logmsg++;
903 newline = strchr(logmsg, '\n');
904 if (newline)
905 *newline = '\0';
906 limit = avail - col;
907 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
908 if (err)
909 goto done;
910 waddwstr(view->window, wlogmsg);
911 col += logmsg_width;
912 while (col <= avail) {
913 waddch(view->window, ' ');
914 col++;
916 done:
917 free(logmsg0);
918 free(wlogmsg);
919 free(author0);
920 free(wauthor);
921 free(line);
922 return err;
925 static struct commit_queue_entry *
926 alloc_commit_queue_entry(struct got_commit_object *commit,
927 struct got_object_id *id)
929 struct commit_queue_entry *entry;
931 entry = calloc(1, sizeof(*entry));
932 if (entry == NULL)
933 return NULL;
935 entry->id = id;
936 entry->commit = commit;
937 return entry;
940 static void
941 pop_commit(struct commit_queue *commits)
943 struct commit_queue_entry *entry;
945 entry = TAILQ_FIRST(&commits->head);
946 TAILQ_REMOVE(&commits->head, entry, entry);
947 got_object_commit_close(entry->commit);
948 commits->ncommits--;
949 /* Don't free entry->id! It is owned by the commit graph. */
950 free(entry);
953 static void
954 free_commits(struct commit_queue *commits)
956 while (!TAILQ_EMPTY(&commits->head))
957 pop_commit(commits);
960 static const struct got_error *
961 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
962 int minqueue, struct got_repository *repo, const char *path)
964 const struct got_error *err = NULL;
965 int nqueued = 0;
967 /*
968 * We keep all commits open throughout the lifetime of the log
969 * view in order to avoid having to re-fetch commits from disk
970 * while updating the display.
971 */
972 while (nqueued < minqueue) {
973 struct got_object_id *id;
974 struct got_commit_object *commit;
975 struct commit_queue_entry *entry;
976 int errcode;
978 err = got_commit_graph_iter_next(&id, graph);
979 if (err) {
980 if (err->code != GOT_ERR_ITER_NEED_MORE)
981 break;
982 err = got_commit_graph_fetch_commits(graph,
983 minqueue, repo);
984 if (err)
985 return err;
986 continue;
989 if (id == NULL)
990 break;
992 err = got_object_open_as_commit(&commit, repo, id);
993 if (err)
994 break;
995 entry = alloc_commit_queue_entry(commit, id);
996 if (entry == NULL) {
997 err = got_error_from_errno();
998 break;
1001 errcode = pthread_mutex_lock(&tog_mutex);
1002 if (errcode) {
1003 err = got_error_set_errno(errcode);
1004 break;
1007 entry->idx = commits->ncommits;
1008 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1009 nqueued++;
1010 commits->ncommits++;
1012 errcode = pthread_mutex_unlock(&tog_mutex);
1013 if (errcode && err == NULL)
1014 err = got_error_set_errno(errcode);
1017 return err;
1020 static const struct got_error *
1021 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1023 const struct got_error *err = NULL;
1024 struct got_reference *head_ref;
1026 *head_id = NULL;
1028 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1029 if (err)
1030 return err;
1032 err = got_ref_resolve(head_id, repo, head_ref);
1033 got_ref_close(head_ref);
1034 if (err) {
1035 *head_id = NULL;
1036 return err;
1039 return NULL;
1042 static const struct got_error *
1043 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1044 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1045 struct commit_queue *commits, int selected_idx, int limit,
1046 const char *path, int commits_needed)
1048 const struct got_error *err = NULL;
1049 struct commit_queue_entry *entry;
1050 int ncommits, width;
1051 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1052 wchar_t *wline;
1054 entry = first;
1055 ncommits = 0;
1056 while (entry) {
1057 if (ncommits == selected_idx) {
1058 *selected = entry;
1059 break;
1061 entry = TAILQ_NEXT(entry, entry);
1062 ncommits++;
1065 if (*selected) {
1066 err = got_object_id_str(&id_str, (*selected)->id);
1067 if (err)
1068 return err;
1071 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1072 entry ? entry->idx + 1 : 0, commits->ncommits,
1073 commits_needed == 0 ? "" : " loading...") == -1)
1074 return got_error_from_errno();
1076 if (path && strcmp(path, "/") != 0) {
1077 if (asprintf(&header, "commit: %s %s%s",
1078 id_str ? id_str : "........................................",
1079 path, ncommits_str) == -1) {
1080 err = got_error_from_errno();
1081 header = NULL;
1082 goto done;
1084 } else if (asprintf(&header, "commit: %s%s",
1085 id_str ? id_str : "........................................",
1086 ncommits_str) == -1) {
1087 err = got_error_from_errno();
1088 header = NULL;
1089 goto done;
1091 err = format_line(&wline, &width, header, view->ncols);
1092 if (err)
1093 goto done;
1095 werase(view->window);
1097 if (view_needs_focus_indication(view))
1098 wstandout(view->window);
1099 waddwstr(view->window, wline);
1100 while (width < view->ncols) {
1101 waddch(view->window, ' ');
1102 width++;
1104 if (view_needs_focus_indication(view))
1105 wstandend(view->window);
1106 free(wline);
1107 if (limit <= 1)
1108 goto done;
1110 entry = first;
1111 *last = first;
1112 ncommits = 0;
1113 while (entry) {
1114 if (ncommits >= limit - 1)
1115 break;
1116 if (view->focussed && ncommits == selected_idx)
1117 wstandout(view->window);
1118 err = draw_commit(view, entry->commit, entry->id);
1119 if (view->focussed && ncommits == selected_idx)
1120 wstandend(view->window);
1121 if (err)
1122 break;
1123 ncommits++;
1124 *last = entry;
1125 entry = TAILQ_NEXT(entry, entry);
1128 view_vborder(view);
1129 done:
1130 free(id_str);
1131 free(ncommits_str);
1132 free(header);
1133 return err;
1136 static void
1137 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1138 struct commit_queue *commits)
1140 struct commit_queue_entry *entry;
1141 int nscrolled = 0;
1143 entry = TAILQ_FIRST(&commits->head);
1144 if (*first_displayed_entry == entry)
1145 return;
1147 entry = *first_displayed_entry;
1148 while (entry && nscrolled < maxscroll) {
1149 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1150 if (entry) {
1151 *first_displayed_entry = entry;
1152 nscrolled++;
1157 static const struct got_error *
1158 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1159 struct commit_queue_entry **last_displayed_entry,
1160 struct commit_queue *commits, int *log_complete, int *commits_needed,
1161 pthread_cond_t *need_commits)
1163 const struct got_error *err = NULL;
1164 struct commit_queue_entry *pentry;
1165 int nscrolled = 0;
1167 if (*last_displayed_entry == NULL)
1168 return NULL;
1170 do {
1171 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1172 if (pentry == NULL) {
1173 int errcode;
1174 if (*log_complete)
1175 return NULL;
1176 *commits_needed = maxscroll + 20;
1177 errcode = pthread_cond_signal(need_commits);
1178 if (errcode)
1179 return got_error_set_errno(errcode);
1180 return NULL;
1182 *last_displayed_entry = pentry;
1184 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1185 if (pentry == NULL)
1186 break;
1187 *first_displayed_entry = pentry;
1188 } while (++nscrolled < maxscroll);
1190 return err;
1193 static const struct got_error *
1194 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1195 struct got_object_id *commit_id, struct got_commit_object *commit,
1196 struct got_repository *repo)
1198 const struct got_error *err;
1199 struct got_object *obj1 = NULL, *obj2 = NULL;
1200 struct got_object_qid *parent_id;
1201 struct tog_view *diff_view;
1203 err = got_object_open(&obj2, repo, commit_id);
1204 if (err)
1205 return err;
1207 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1208 if (parent_id) {
1209 err = got_object_open(&obj1, repo, parent_id->id);
1210 if (err)
1211 goto done;
1214 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1215 if (diff_view == NULL) {
1216 err = got_error_from_errno();
1217 goto done;
1220 err = open_diff_view(diff_view, obj1, obj2, repo);
1221 if (err == NULL)
1222 *new_view = diff_view;
1223 done:
1224 if (obj1)
1225 got_object_close(obj1);
1226 if (obj2)
1227 got_object_close(obj2);
1228 return err;
1231 static const struct got_error *
1232 browse_commit(struct tog_view **new_view, int begin_x,
1233 struct commit_queue_entry *entry, struct got_repository *repo)
1235 const struct got_error *err = NULL;
1236 struct got_tree_object *tree;
1237 struct tog_view *tree_view;
1239 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1240 if (err)
1241 return err;
1243 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1244 if (tree_view == NULL)
1245 return got_error_from_errno();
1247 err = open_tree_view(tree_view, tree, entry->id, repo);
1248 if (err)
1249 got_object_tree_close(tree);
1250 else
1251 *new_view = tree_view;
1252 return err;
1255 static void *
1256 log_thread(void *arg)
1258 const struct got_error *err = NULL;
1259 int errcode = 0;
1260 struct tog_log_thread_args *a = arg;
1261 int done = 0;
1263 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1264 if (err)
1265 return (void *)err;
1267 while (!done && !err) {
1268 err = queue_commits(a->graph, a->commits, 1, a->repo,
1269 a->in_repo_path);
1270 if (err) {
1271 if (err->code != GOT_ERR_ITER_COMPLETED)
1272 return (void *)err;
1273 err = NULL;
1274 done = 1;
1275 } else if (a->commits_needed > 0)
1276 a->commits_needed--;
1278 errcode = pthread_mutex_lock(&tog_mutex);
1279 if (errcode)
1280 return (void *)got_error_set_errno(errcode);
1282 if (done)
1283 a->log_complete = 1;
1284 else if (*a->quit) {
1285 done = 1;
1286 a->log_complete = 1;
1287 } else if (*a->first_displayed_entry == NULL) {
1288 *a->first_displayed_entry =
1289 TAILQ_FIRST(&a->commits->head);
1290 *a->selected_entry = *a->first_displayed_entry;
1293 err = draw_commits(a->view, a->last_displayed_entry,
1294 a->selected_entry, *a->first_displayed_entry,
1295 a->commits, *a->selected, a->view->nlines,
1296 a->in_repo_path, a->commits_needed);
1298 if (done)
1299 a->commits_needed = 0;
1300 else if (a->commits_needed == 0) {
1301 errcode = pthread_cond_wait(&a->need_commits,
1302 &tog_mutex);
1303 if (errcode)
1304 err = got_error_set_errno(errcode);
1307 errcode = pthread_mutex_unlock(&tog_mutex);
1308 if (errcode && err == NULL)
1309 err = got_error_set_errno(errcode);
1311 return (void *)err;
1314 static const struct got_error *
1315 stop_log_thread(struct tog_log_view_state *s)
1317 const struct got_error *err = NULL;
1318 int errcode;
1320 if (s->thread) {
1321 s->quit = 1;
1322 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1323 if (errcode)
1324 return got_error_set_errno(errcode);
1325 errcode = pthread_mutex_unlock(&tog_mutex);
1326 if (errcode)
1327 return got_error_set_errno(errcode);
1328 errcode = pthread_join(s->thread, (void **)&err);
1329 if (errcode)
1330 return got_error_set_errno(errcode);
1331 errcode = pthread_mutex_lock(&tog_mutex);
1332 if (errcode)
1333 return got_error_set_errno(errcode);
1334 s->thread = NULL;
1337 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1338 if (errcode && err == NULL)
1339 err = got_error_set_errno(errcode);
1341 if (s->thread_args.repo) {
1342 got_repo_close(s->thread_args.repo);
1343 s->thread_args.repo = NULL;
1346 if (s->thread_args.graph) {
1347 got_commit_graph_close(s->thread_args.graph);
1348 s->thread_args.graph = NULL;
1351 return err;
1354 static const struct got_error *
1355 close_log_view(struct tog_view *view)
1357 const struct got_error *err = NULL;
1358 struct tog_log_view_state *s = &view->state.log;
1360 err = stop_log_thread(s);
1361 free_commits(&s->commits);
1362 free(s->in_repo_path);
1363 s->in_repo_path = NULL;
1364 free(s->start_id);
1365 s->start_id = NULL;
1366 return err;
1369 static const struct got_error *
1370 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1371 struct got_repository *repo, const char *path, int check_disk)
1373 const struct got_error *err = NULL;
1374 struct tog_log_view_state *s = &view->state.log;
1375 struct got_repository *thread_repo = NULL;
1376 struct got_commit_graph *thread_graph = NULL;
1377 int errcode;
1379 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1380 if (err != NULL)
1381 goto done;
1383 /* The commit queue only contains commits being displayed. */
1384 TAILQ_INIT(&s->commits.head);
1385 s->commits.ncommits = 0;
1387 s->repo = repo;
1388 s->start_id = got_object_id_dup(start_id);
1389 if (s->start_id == NULL) {
1390 err = got_error_from_errno();
1391 goto done;
1394 view->show = show_log_view;
1395 view->input = input_log_view;
1396 view->close = close_log_view;
1398 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1399 if (err)
1400 goto done;
1401 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1402 0, thread_repo);
1403 if (err)
1404 goto done;
1406 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1407 if (errcode) {
1408 err = got_error_set_errno(errcode);
1409 goto done;
1412 s->thread_args.commits_needed = view->nlines;
1413 s->thread_args.graph = thread_graph;
1414 s->thread_args.commits = &s->commits;
1415 s->thread_args.in_repo_path = s->in_repo_path;
1416 s->thread_args.start_id = s->start_id;
1417 s->thread_args.repo = thread_repo;
1418 s->thread_args.log_complete = 0;
1419 s->thread_args.quit = &s->quit;
1420 s->thread_args.view = view;
1421 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1422 s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1423 s->thread_args.selected_entry = &s->selected_entry;
1424 s->thread_args.selected = &s->selected;
1426 errcode = pthread_create(&s->thread, NULL, log_thread,
1427 &s->thread_args);
1428 if (errcode) {
1429 err = got_error_set_errno(errcode);
1430 goto done;
1433 done:
1434 if (err)
1435 close_log_view(view);
1436 return err;
1439 static const struct got_error *
1440 show_log_view(struct tog_view *view)
1442 struct tog_log_view_state *s = &view->state.log;
1444 return draw_commits(view, &s->last_displayed_entry,
1445 &s->selected_entry, s->first_displayed_entry,
1446 &s->commits, s->selected, view->nlines,
1447 s->in_repo_path, s->thread_args.commits_needed);
1450 static const struct got_error *
1451 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1452 struct tog_view **focus_view, struct tog_view *view, int ch)
1454 const struct got_error *err = NULL;
1455 struct tog_log_view_state *s = &view->state.log;
1456 char *parent_path;
1457 struct tog_view *diff_view = NULL, *tree_view = NULL;
1458 int begin_x = 0;
1460 switch (ch) {
1461 case 'q':
1462 s->quit = 1;
1463 break;
1464 case 'k':
1465 case KEY_UP:
1466 if (s->selected > 0)
1467 s->selected--;
1468 if (s->selected > 0)
1469 break;
1470 scroll_up(&s->first_displayed_entry, 1,
1471 &s->commits);
1472 break;
1473 case KEY_PPAGE:
1474 if (TAILQ_FIRST(&s->commits.head) ==
1475 s->first_displayed_entry) {
1476 s->selected = 0;
1477 break;
1479 scroll_up(&s->first_displayed_entry,
1480 view->nlines, &s->commits);
1481 break;
1482 case 'j':
1483 case KEY_DOWN:
1484 if (s->selected < MIN(view->nlines - 2,
1485 s->commits.ncommits - 1)) {
1486 s->selected++;
1487 break;
1489 err = scroll_down(&s->first_displayed_entry, 1,
1490 &s->last_displayed_entry, &s->commits,
1491 &s->thread_args.log_complete,
1492 &s->thread_args.commits_needed,
1493 &s->thread_args.need_commits);
1494 break;
1495 case KEY_NPAGE: {
1496 struct commit_queue_entry *first;
1497 first = s->first_displayed_entry;
1498 err = scroll_down(&s->first_displayed_entry,
1499 view->nlines, &s->last_displayed_entry,
1500 &s->commits, &s->thread_args.log_complete,
1501 &s->thread_args.commits_needed,
1502 &s->thread_args.need_commits);
1503 if (first == s->first_displayed_entry &&
1504 s->selected < MIN(view->nlines - 2,
1505 s->commits.ncommits - 1)) {
1506 /* can't scroll further down */
1507 s->selected = MIN(view->nlines - 2,
1508 s->commits.ncommits - 1);
1510 err = NULL;
1511 break;
1513 case KEY_RESIZE:
1514 if (s->selected > view->nlines - 2)
1515 s->selected = view->nlines - 2;
1516 if (s->selected > s->commits.ncommits - 1)
1517 s->selected = s->commits.ncommits - 1;
1518 break;
1519 case KEY_ENTER:
1520 case '\r':
1521 if (view_is_parent_view(view))
1522 begin_x = view_split_begin_x(view->begin_x);
1523 err = open_diff_view_for_commit(&diff_view, begin_x,
1524 s->selected_entry->id, s->selected_entry->commit,
1525 s->repo);
1526 if (err)
1527 break;
1528 if (view_is_parent_view(view)) {
1529 err = view_close_child(view);
1530 if (err)
1531 return err;
1532 err = view_set_child(view, diff_view);
1533 if (err) {
1534 view_close(diff_view);
1535 break;
1537 if (!view_is_splitscreen(diff_view)) {
1538 *focus_view = diff_view;
1539 view->child_focussed = 1;
1541 } else
1542 *new_view = diff_view;
1543 break;
1544 case 't':
1545 if (view_is_parent_view(view))
1546 begin_x = view_split_begin_x(view->begin_x);
1547 err = browse_commit(&tree_view, begin_x,
1548 s->selected_entry, s->repo);
1549 if (view_is_parent_view(view)) {
1550 err = view_close_child(view);
1551 if (err)
1552 return err;
1553 err = view_set_child(view, tree_view);
1554 if (err) {
1555 view_close(tree_view);
1556 break;
1558 *focus_view = tree_view;
1559 view->child_focussed = 1;
1560 } else
1561 *new_view = tree_view;
1562 break;
1563 case KEY_BACKSPACE:
1564 if (strcmp(s->in_repo_path, "/") == 0)
1565 break;
1566 parent_path = dirname(s->in_repo_path);
1567 if (parent_path && strcmp(parent_path, ".") != 0) {
1568 struct tog_view *lv;
1569 err = stop_log_thread(s);
1570 if (err)
1571 return err;
1572 lv = view_open(view->nlines, view->ncols,
1573 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1574 if (lv == NULL)
1575 return got_error_from_errno();
1576 err = open_log_view(lv, s->start_id, s->repo,
1577 parent_path, 0);
1578 if (err)
1579 return err;;
1580 if (view_is_parent_view(view))
1581 *new_view = lv;
1582 else {
1583 view_set_child(view->parent, lv);
1584 *focus_view = lv;
1586 return NULL;
1588 break;
1589 default:
1590 break;
1593 return err;
1596 static const struct got_error *
1597 cmd_log(int argc, char *argv[])
1599 const struct got_error *error;
1600 struct got_repository *repo = NULL;
1601 struct got_object_id *start_id = NULL;
1602 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1603 char *start_commit = NULL;
1604 int ch;
1605 struct tog_view *view;
1607 #ifndef PROFILE
1608 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1609 == -1)
1610 err(1, "pledge");
1611 #endif
1613 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1614 switch (ch) {
1615 case 'c':
1616 start_commit = optarg;
1617 break;
1618 case 'r':
1619 repo_path = realpath(optarg, NULL);
1620 if (repo_path == NULL)
1621 err(1, "-r option");
1622 break;
1623 default:
1624 usage();
1625 /* NOTREACHED */
1629 argc -= optind;
1630 argv += optind;
1632 if (argc == 0)
1633 path = strdup("");
1634 else if (argc == 1)
1635 path = strdup(argv[0]);
1636 else
1637 usage_log();
1638 if (path == NULL)
1639 return got_error_from_errno();
1641 cwd = getcwd(NULL, 0);
1642 if (cwd == NULL) {
1643 error = got_error_from_errno();
1644 goto done;
1646 if (repo_path == NULL) {
1647 repo_path = strdup(cwd);
1648 if (repo_path == NULL) {
1649 error = got_error_from_errno();
1650 goto done;
1654 error = got_repo_open(&repo, repo_path);
1655 if (error != NULL)
1656 goto done;
1658 if (start_commit == NULL) {
1659 error = get_head_commit_id(&start_id, repo);
1660 if (error != NULL)
1661 goto done;
1662 } else {
1663 struct got_object *obj;
1664 error = got_object_open_by_id_str(&obj, repo, start_commit);
1665 if (error == NULL) {
1666 start_id = got_object_id_dup(got_object_get_id(obj));
1667 if (start_id == NULL)
1668 error = got_error_from_errno();
1669 goto done;
1672 if (error != NULL)
1673 goto done;
1675 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1676 if (view == NULL) {
1677 error = got_error_from_errno();
1678 goto done;
1680 error = open_log_view(view, start_id, repo, path, 1);
1681 if (error)
1682 goto done;
1683 error = view_loop(view);
1684 done:
1685 free(repo_path);
1686 free(cwd);
1687 free(path);
1688 free(start_id);
1689 if (repo)
1690 got_repo_close(repo);
1691 return error;
1694 __dead static void
1695 usage_diff(void)
1697 endwin();
1698 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1699 getprogname());
1700 exit(1);
1703 static char *
1704 parse_next_line(FILE *f, size_t *len)
1706 char *line;
1707 size_t linelen;
1708 size_t lineno;
1709 const char delim[3] = { '\0', '\0', '\0'};
1711 line = fparseln(f, &linelen, &lineno, delim, 0);
1712 if (len)
1713 *len = linelen;
1714 return line;
1717 static const struct got_error *
1718 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1719 int *last_displayed_line, int *eof, int max_lines,
1720 char * header)
1722 const struct got_error *err;
1723 int nlines = 0, nprinted = 0;
1724 char *line;
1725 size_t len;
1726 wchar_t *wline;
1727 int width;
1729 rewind(f);
1730 werase(view->window);
1732 if (header) {
1733 err = format_line(&wline, &width, header, view->ncols);
1734 if (err) {
1735 return err;
1738 if (view_needs_focus_indication(view))
1739 wstandout(view->window);
1740 waddwstr(view->window, wline);
1741 if (view_needs_focus_indication(view))
1742 wstandend(view->window);
1743 if (width < view->ncols)
1744 waddch(view->window, '\n');
1746 if (max_lines <= 1)
1747 return NULL;
1748 max_lines--;
1751 *eof = 0;
1752 while (nprinted < max_lines) {
1753 line = parse_next_line(f, &len);
1754 if (line == NULL) {
1755 *eof = 1;
1756 break;
1758 if (++nlines < *first_displayed_line) {
1759 free(line);
1760 continue;
1763 err = format_line(&wline, &width, line, view->ncols);
1764 if (err) {
1765 free(line);
1766 return err;
1768 waddwstr(view->window, wline);
1769 if (width < view->ncols)
1770 waddch(view->window, '\n');
1771 if (++nprinted == 1)
1772 *first_displayed_line = nlines;
1773 free(line);
1774 free(wline);
1775 wline = NULL;
1777 *last_displayed_line = nlines;
1779 view_vborder(view);
1781 return NULL;
1784 static const struct got_error *
1785 create_diff(struct tog_diff_view_state *s)
1787 const struct got_error *err = NULL;
1788 struct got_object *obj1 = NULL, *obj2 = NULL;
1789 FILE *f = NULL;
1791 if (s->id1) {
1792 err = got_object_open(&obj1, s->repo, s->id1);
1793 if (err)
1794 return err;
1797 err = got_object_open(&obj2, s->repo, s->id2);
1798 if (err)
1799 goto done;
1801 f = got_opentemp();
1802 if (f == NULL) {
1803 err = got_error_from_errno();
1804 goto done;
1806 if (s->f)
1807 fclose(s->f);
1808 s->f = f;
1810 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1811 case GOT_OBJ_TYPE_BLOB:
1812 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1813 s->diff_context, s->repo, f);
1814 break;
1815 case GOT_OBJ_TYPE_TREE:
1816 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1817 s->diff_context, s->repo, f);
1818 break;
1819 case GOT_OBJ_TYPE_COMMIT:
1820 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1821 s->repo, f);
1822 break;
1823 default:
1824 err = got_error(GOT_ERR_OBJ_TYPE);
1825 break;
1827 done:
1828 if (obj1)
1829 got_object_close(obj1);
1830 got_object_close(obj2);
1831 if (f)
1832 fflush(f);
1833 return err;
1836 static const struct got_error *
1837 open_diff_view(struct tog_view *view, struct got_object *obj1,
1838 struct got_object *obj2, struct got_repository *repo)
1840 const struct got_error *err;
1842 if (obj1 != NULL && obj2 != NULL &&
1843 got_object_get_type(obj1) != got_object_get_type(obj2))
1844 return got_error(GOT_ERR_OBJ_TYPE);
1846 if (obj1) {
1847 struct got_object_id *id1;
1848 id1 = got_object_id_dup(got_object_get_id(obj1));
1849 if (id1 == NULL)
1850 return got_error_from_errno();
1851 view->state.diff.id1 = id1;
1852 } else
1853 view->state.diff.id1 = NULL;
1855 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1856 if (view->state.diff.id2 == NULL) {
1857 free(view->state.diff.id1);
1858 view->state.diff.id1 = NULL;
1859 return got_error_from_errno();
1861 view->state.diff.f = NULL;
1862 view->state.diff.first_displayed_line = 1;
1863 view->state.diff.last_displayed_line = view->nlines;
1864 view->state.diff.diff_context = 3;
1865 view->state.diff.repo = repo;
1867 err = create_diff(&view->state.diff);
1868 if (err) {
1869 free(view->state.diff.id1);
1870 view->state.diff.id1 = NULL;
1871 free(view->state.diff.id2);
1872 view->state.diff.id2 = NULL;
1873 return err;
1876 view->show = show_diff_view;
1877 view->input = input_diff_view;
1878 view->close = close_diff_view;
1880 return NULL;
1883 static const struct got_error *
1884 close_diff_view(struct tog_view *view)
1886 const struct got_error *err = NULL;
1888 free(view->state.diff.id1);
1889 view->state.diff.id1 = NULL;
1890 free(view->state.diff.id2);
1891 view->state.diff.id2 = NULL;
1892 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1893 err = got_error_from_errno();
1894 return err;
1897 static const struct got_error *
1898 show_diff_view(struct tog_view *view)
1900 const struct got_error *err;
1901 struct tog_diff_view_state *s = &view->state.diff;
1902 char *id_str1 = NULL, *id_str2, *header;
1904 if (s->id1) {
1905 err = got_object_id_str(&id_str1, s->id1);
1906 if (err)
1907 return err;
1909 err = got_object_id_str(&id_str2, s->id2);
1910 if (err)
1911 return err;
1913 if (asprintf(&header, "diff: %s %s",
1914 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1915 err = got_error_from_errno();
1916 free(id_str1);
1917 free(id_str2);
1918 return err;
1920 free(id_str1);
1921 free(id_str2);
1923 return draw_file(view, s->f, &s->first_displayed_line,
1924 &s->last_displayed_line, &s->eof, view->nlines,
1925 header);
1928 static const struct got_error *
1929 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1930 struct tog_view **focus_view, struct tog_view *view, int ch)
1932 const struct got_error *err = NULL;
1933 struct tog_diff_view_state *s = &view->state.diff;
1934 int i;
1936 switch (ch) {
1937 case 'k':
1938 case KEY_UP:
1939 if (s->first_displayed_line > 1)
1940 s->first_displayed_line--;
1941 break;
1942 case KEY_PPAGE:
1943 i = 0;
1944 while (i++ < view->nlines - 1 &&
1945 s->first_displayed_line > 1)
1946 s->first_displayed_line--;
1947 break;
1948 case 'j':
1949 case KEY_DOWN:
1950 if (!s->eof)
1951 s->first_displayed_line++;
1952 break;
1953 case KEY_NPAGE:
1954 case ' ':
1955 i = 0;
1956 while (!s->eof && i++ < view->nlines - 1) {
1957 char *line;
1958 line = parse_next_line(s->f, NULL);
1959 s->first_displayed_line++;
1960 if (line == NULL)
1961 break;
1963 break;
1964 case '[':
1965 if (s->diff_context > 0) {
1966 s->diff_context--;
1967 err = create_diff(s);
1969 break;
1970 case ']':
1971 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1972 s->diff_context++;
1973 err = create_diff(s);
1975 break;
1976 default:
1977 break;
1980 return err;
1983 static const struct got_error *
1984 cmd_diff(int argc, char *argv[])
1986 const struct got_error *error = NULL;
1987 struct got_repository *repo = NULL;
1988 struct got_object *obj1 = NULL, *obj2 = NULL;
1989 char *repo_path = NULL;
1990 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1991 int ch;
1992 struct tog_view *view;
1994 #ifndef PROFILE
1995 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1996 == -1)
1997 err(1, "pledge");
1998 #endif
2000 while ((ch = getopt(argc, argv, "")) != -1) {
2001 switch (ch) {
2002 default:
2003 usage();
2004 /* NOTREACHED */
2008 argc -= optind;
2009 argv += optind;
2011 if (argc == 0) {
2012 usage_diff(); /* TODO show local worktree changes */
2013 } else if (argc == 2) {
2014 repo_path = getcwd(NULL, 0);
2015 if (repo_path == NULL)
2016 return got_error_from_errno();
2017 obj_id_str1 = argv[0];
2018 obj_id_str2 = argv[1];
2019 } else if (argc == 3) {
2020 repo_path = realpath(argv[0], NULL);
2021 if (repo_path == NULL)
2022 return got_error_from_errno();
2023 obj_id_str1 = argv[1];
2024 obj_id_str2 = argv[2];
2025 } else
2026 usage_diff();
2028 error = got_repo_open(&repo, repo_path);
2029 free(repo_path);
2030 if (error)
2031 goto done;
2033 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2034 if (error)
2035 goto done;
2037 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2038 if (error)
2039 goto done;
2041 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2042 if (view == NULL) {
2043 error = got_error_from_errno();
2044 goto done;
2046 error = open_diff_view(view, obj1, obj2, repo);
2047 if (error)
2048 goto done;
2049 error = view_loop(view);
2050 done:
2051 got_repo_close(repo);
2052 if (obj1)
2053 got_object_close(obj1);
2054 if (obj2)
2055 got_object_close(obj2);
2056 return error;
2059 __dead static void
2060 usage_blame(void)
2062 endwin();
2063 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2064 getprogname());
2065 exit(1);
2068 struct tog_blame_line {
2069 int annotated;
2070 struct got_object_id *id;
2073 static const struct got_error *
2074 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2075 const char *path, struct tog_blame_line *lines, int nlines,
2076 int blame_complete, int selected_line, int *first_displayed_line,
2077 int *last_displayed_line, int *eof, int max_lines)
2079 const struct got_error *err;
2080 int lineno = 0, nprinted = 0;
2081 char *line;
2082 size_t len;
2083 wchar_t *wline;
2084 int width, wlimit;
2085 struct tog_blame_line *blame_line;
2086 struct got_object_id *prev_id = NULL;
2087 char *id_str;
2089 err = got_object_id_str(&id_str, id);
2090 if (err)
2091 return err;
2093 rewind(f);
2094 werase(view->window);
2096 if (asprintf(&line, "commit: %s", id_str) == -1) {
2097 err = got_error_from_errno();
2098 free(id_str);
2099 return err;
2102 err = format_line(&wline, &width, line, view->ncols);
2103 free(line);
2104 line = NULL;
2105 if (view_needs_focus_indication(view))
2106 wstandout(view->window);
2107 waddwstr(view->window, wline);
2108 if (view_needs_focus_indication(view))
2109 wstandend(view->window);
2110 free(wline);
2111 wline = NULL;
2112 if (width < view->ncols)
2113 waddch(view->window, '\n');
2115 if (asprintf(&line, "[%d/%d] %s%s",
2116 *first_displayed_line - 1 + selected_line, nlines,
2117 blame_complete ? "" : "annotating ", path) == -1) {
2118 free(id_str);
2119 return got_error_from_errno();
2121 free(id_str);
2122 err = format_line(&wline, &width, line, view->ncols);
2123 free(line);
2124 line = NULL;
2125 if (err)
2126 return err;
2127 waddwstr(view->window, wline);
2128 free(wline);
2129 wline = NULL;
2130 if (width < view->ncols)
2131 waddch(view->window, '\n');
2133 *eof = 0;
2134 while (nprinted < max_lines - 2) {
2135 line = parse_next_line(f, &len);
2136 if (line == NULL) {
2137 *eof = 1;
2138 break;
2140 if (++lineno < *first_displayed_line) {
2141 free(line);
2142 continue;
2145 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2146 err = format_line(&wline, &width, line, wlimit);
2147 if (err) {
2148 free(line);
2149 return err;
2152 if (view->focussed && nprinted == selected_line - 1)
2153 wstandout(view->window);
2155 blame_line = &lines[lineno - 1];
2156 if (blame_line->annotated && prev_id &&
2157 got_object_id_cmp(prev_id, blame_line->id) == 0)
2158 waddstr(view->window, " ");
2159 else if (blame_line->annotated) {
2160 char *id_str;
2161 err = got_object_id_str(&id_str, blame_line->id);
2162 if (err) {
2163 free(line);
2164 free(wline);
2165 return err;
2167 wprintw(view->window, "%.8s ", id_str);
2168 free(id_str);
2169 prev_id = blame_line->id;
2170 } else {
2171 waddstr(view->window, "........ ");
2172 prev_id = NULL;
2175 waddwstr(view->window, wline);
2176 while (width < wlimit) {
2177 waddch(view->window, ' ');
2178 width++;
2180 if (view->focussed && nprinted == selected_line - 1)
2181 wstandend(view->window);
2182 if (++nprinted == 1)
2183 *first_displayed_line = lineno;
2184 free(line);
2185 free(wline);
2186 wline = NULL;
2188 *last_displayed_line = lineno;
2190 view_vborder(view);
2192 return NULL;
2195 static const struct got_error *
2196 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2198 const struct got_error *err = NULL;
2199 struct tog_blame_cb_args *a = arg;
2200 struct tog_blame_line *line;
2201 int errcode;
2203 if (nlines != a->nlines ||
2204 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2205 return got_error(GOT_ERR_RANGE);
2207 errcode = pthread_mutex_lock(&tog_mutex);
2208 if (errcode)
2209 return got_error_set_errno(errcode);
2211 if (*a->quit) { /* user has quit the blame view */
2212 err = got_error(GOT_ERR_ITER_COMPLETED);
2213 goto done;
2216 if (lineno == -1)
2217 goto done; /* no change in this commit */
2219 line = &a->lines[lineno - 1];
2220 if (line->annotated)
2221 goto done;
2223 line->id = got_object_id_dup(id);
2224 if (line->id == NULL) {
2225 err = got_error_from_errno();
2226 goto done;
2228 line->annotated = 1;
2230 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2231 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2232 a->last_displayed_line, a->eof, a->view->nlines);
2233 done:
2234 errcode = pthread_mutex_unlock(&tog_mutex);
2235 if (errcode)
2236 err = got_error_set_errno(errcode);
2237 return err;
2240 static void *
2241 blame_thread(void *arg)
2243 const struct got_error *err;
2244 struct tog_blame_thread_args *ta = arg;
2245 struct tog_blame_cb_args *a = ta->cb_args;
2246 int errcode;
2248 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2249 blame_cb, ta->cb_args);
2251 errcode = pthread_mutex_lock(&tog_mutex);
2252 if (errcode)
2253 return (void *)got_error_set_errno(errcode);
2255 got_repo_close(ta->repo);
2256 ta->repo = NULL;
2257 *ta->complete = 1;
2258 if (!err) {
2259 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2260 a->lines, a->nlines, 1, *a->selected_line,
2261 a->first_displayed_line, a->last_displayed_line, a->eof,
2262 a->view->nlines);
2265 errcode = pthread_mutex_unlock(&tog_mutex);
2266 if (errcode && err == NULL)
2267 err = got_error_set_errno(errcode);
2269 return (void *)err;
2272 static struct got_object_id *
2273 get_selected_commit_id(struct tog_blame_line *lines,
2274 int first_displayed_line, int selected_line)
2276 struct tog_blame_line *line;
2278 line = &lines[first_displayed_line - 1 + selected_line - 1];
2279 if (!line->annotated)
2280 return NULL;
2282 return line->id;
2285 static const struct got_error *
2286 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2287 struct tog_blame_line *lines, int first_displayed_line,
2288 int selected_line, struct got_repository *repo)
2290 const struct got_error *err = NULL;
2291 struct got_commit_object *commit = NULL;
2292 struct got_object_id *selected_id;
2293 struct got_object_qid *pid;
2295 *pobj = NULL;
2296 *obj = NULL;
2298 selected_id = get_selected_commit_id(lines,
2299 first_displayed_line, selected_line);
2300 if (selected_id == NULL)
2301 return NULL;
2303 err = got_object_open(obj, repo, selected_id);
2304 if (err)
2305 goto done;
2307 err = got_object_commit_open(&commit, repo, *obj);
2308 if (err)
2309 goto done;
2311 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2312 if (pid) {
2313 err = got_object_open(pobj, repo, pid->id);
2314 if (err)
2315 goto done;
2317 done:
2318 if (commit)
2319 got_object_commit_close(commit);
2320 return err;
2323 static const struct got_error *
2324 stop_blame(struct tog_blame *blame)
2326 const struct got_error *err = NULL;
2327 int i;
2329 if (blame->thread) {
2330 int errcode;
2331 errcode = pthread_mutex_unlock(&tog_mutex);
2332 if (errcode)
2333 return got_error_set_errno(errcode);
2334 errcode = pthread_join(blame->thread, (void **)&err);
2335 if (errcode)
2336 return got_error_set_errno(errcode);
2337 errcode = pthread_mutex_lock(&tog_mutex);
2338 if (errcode)
2339 return got_error_set_errno(errcode);
2340 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2341 err = NULL;
2342 blame->thread = NULL;
2344 if (blame->thread_args.repo) {
2345 got_repo_close(blame->thread_args.repo);
2346 blame->thread_args.repo = NULL;
2348 if (blame->f) {
2349 fclose(blame->f);
2350 blame->f = NULL;
2352 for (i = 0; i < blame->nlines; i++)
2353 free(blame->lines[i].id);
2354 free(blame->lines);
2355 blame->lines = NULL;
2356 free(blame->cb_args.commit_id);
2357 blame->cb_args.commit_id = NULL;
2359 return err;
2362 static const struct got_error *
2363 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2364 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2365 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2366 struct got_repository *repo)
2368 const struct got_error *err = NULL;
2369 struct got_blob_object *blob = NULL;
2370 struct got_repository *thread_repo = NULL;
2371 struct got_object_id *obj_id = NULL;
2372 struct got_object *obj = NULL;
2373 int errcode;
2375 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2376 if (err)
2377 goto done;
2379 err = got_object_open(&obj, repo, obj_id);
2380 if (err)
2381 goto done;
2383 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2384 err = got_error(GOT_ERR_OBJ_TYPE);
2385 goto done;
2388 err = got_object_blob_open(&blob, repo, obj, 8192);
2389 if (err)
2390 goto done;
2391 blame->f = got_opentemp();
2392 if (blame->f == NULL) {
2393 err = got_error_from_errno();
2394 goto done;
2396 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2397 blame->f, blob);
2398 if (err)
2399 goto done;
2401 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2402 if (blame->lines == NULL) {
2403 err = got_error_from_errno();
2404 goto done;
2407 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2408 if (err)
2409 goto done;
2411 blame->cb_args.view = view;
2412 blame->cb_args.lines = blame->lines;
2413 blame->cb_args.nlines = blame->nlines;
2414 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2415 if (blame->cb_args.commit_id == NULL) {
2416 err = got_error_from_errno();
2417 goto done;
2419 blame->cb_args.f = blame->f;
2420 blame->cb_args.path = path;
2421 blame->cb_args.first_displayed_line = first_displayed_line;
2422 blame->cb_args.selected_line = selected_line;
2423 blame->cb_args.last_displayed_line = last_displayed_line;
2424 blame->cb_args.quit = done;
2425 blame->cb_args.eof = eof;
2427 blame->thread_args.path = path;
2428 blame->thread_args.repo = thread_repo;
2429 blame->thread_args.cb_args = &blame->cb_args;
2430 blame->thread_args.complete = blame_complete;
2431 *blame_complete = 0;
2433 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2434 &blame->thread_args);
2435 if (errcode) {
2436 err = got_error_set_errno(errcode);
2437 goto done;
2440 done:
2441 if (blob)
2442 got_object_blob_close(blob);
2443 free(obj_id);
2444 if (obj)
2445 got_object_close(obj);
2446 if (err)
2447 stop_blame(blame);
2448 return err;
2451 static const struct got_error *
2452 open_blame_view(struct tog_view *view, char *path,
2453 struct got_object_id *commit_id, struct got_repository *repo)
2455 const struct got_error *err = NULL;
2456 struct tog_blame_view_state *s = &view->state.blame;
2458 SIMPLEQ_INIT(&s->blamed_commits);
2460 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2461 if (err)
2462 return err;
2464 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2465 s->first_displayed_line = 1;
2466 s->last_displayed_line = view->nlines;
2467 s->selected_line = 1;
2468 s->blame_complete = 0;
2469 s->path = path;
2470 if (s->path == NULL)
2471 return got_error_from_errno();
2472 s->repo = repo;
2473 s->commit_id = commit_id;
2474 memset(&s->blame, 0, sizeof(s->blame));
2476 view->show = show_blame_view;
2477 view->input = input_blame_view;
2478 view->close = close_blame_view;
2480 return run_blame(&s->blame, view, &s->blame_complete,
2481 &s->first_displayed_line, &s->last_displayed_line,
2482 &s->selected_line, &s->done, &s->eof, s->path,
2483 s->blamed_commit->id, s->repo);
2486 static const struct got_error *
2487 close_blame_view(struct tog_view *view)
2489 const struct got_error *err = NULL;
2490 struct tog_blame_view_state *s = &view->state.blame;
2492 if (s->blame.thread)
2493 err = stop_blame(&s->blame);
2495 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2496 struct got_object_qid *blamed_commit;
2497 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2498 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2499 got_object_qid_free(blamed_commit);
2502 free(s->path);
2504 return err;
2507 static const struct got_error *
2508 show_blame_view(struct tog_view *view)
2510 const struct got_error *err = NULL;
2511 struct tog_blame_view_state *s = &view->state.blame;
2513 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2514 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2515 s->selected_line, &s->first_displayed_line,
2516 &s->last_displayed_line, &s->eof, view->nlines);
2518 view_vborder(view);
2519 return err;
2522 static const struct got_error *
2523 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2524 struct tog_view **focus_view, struct tog_view *view, int ch)
2526 const struct got_error *err = NULL, *thread_err = NULL;
2527 struct got_object *obj = NULL, *pobj = NULL;
2528 struct tog_view *diff_view;
2529 struct tog_blame_view_state *s = &view->state.blame;
2530 int begin_x = 0;
2532 switch (ch) {
2533 case 'q':
2534 s->done = 1;
2535 break;
2536 case 'k':
2537 case KEY_UP:
2538 if (s->selected_line > 1)
2539 s->selected_line--;
2540 else if (s->selected_line == 1 &&
2541 s->first_displayed_line > 1)
2542 s->first_displayed_line--;
2543 break;
2544 case KEY_PPAGE:
2545 if (s->first_displayed_line == 1) {
2546 s->selected_line = 1;
2547 break;
2549 if (s->first_displayed_line > view->nlines - 2)
2550 s->first_displayed_line -=
2551 (view->nlines - 2);
2552 else
2553 s->first_displayed_line = 1;
2554 break;
2555 case 'j':
2556 case KEY_DOWN:
2557 if (s->selected_line < view->nlines - 2 &&
2558 s->first_displayed_line +
2559 s->selected_line <= s->blame.nlines)
2560 s->selected_line++;
2561 else if (s->last_displayed_line <
2562 s->blame.nlines)
2563 s->first_displayed_line++;
2564 break;
2565 case 'b':
2566 case 'p': {
2567 struct got_object_id *id;
2568 id = get_selected_commit_id(s->blame.lines,
2569 s->first_displayed_line, s->selected_line);
2570 if (id == NULL || got_object_id_cmp(id,
2571 s->blamed_commit->id) == 0)
2572 break;
2573 err = open_selected_commit(&pobj, &obj,
2574 s->blame.lines, s->first_displayed_line,
2575 s->selected_line, s->repo);
2576 if (err)
2577 break;
2578 if (pobj == NULL && obj == NULL)
2579 break;
2580 if (ch == 'p' && pobj == NULL)
2581 break;
2582 s->done = 1;
2583 thread_err = stop_blame(&s->blame);
2584 s->done = 0;
2585 if (thread_err)
2586 break;
2587 id = got_object_get_id(ch == 'b' ? obj : pobj);
2588 got_object_close(obj);
2589 obj = NULL;
2590 if (pobj) {
2591 got_object_close(pobj);
2592 pobj = NULL;
2594 err = got_object_qid_alloc(&s->blamed_commit, id);
2595 if (err)
2596 goto done;
2597 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2598 s->blamed_commit, entry);
2599 err = run_blame(&s->blame, view, &s->blame_complete,
2600 &s->first_displayed_line, &s->last_displayed_line,
2601 &s->selected_line, &s->done, &s->eof,
2602 s->path, s->blamed_commit->id, s->repo);
2603 if (err)
2604 break;
2605 break;
2607 case 'B': {
2608 struct got_object_qid *first;
2609 first = SIMPLEQ_FIRST(&s->blamed_commits);
2610 if (!got_object_id_cmp(first->id, s->commit_id))
2611 break;
2612 s->done = 1;
2613 thread_err = stop_blame(&s->blame);
2614 s->done = 0;
2615 if (thread_err)
2616 break;
2617 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2618 got_object_qid_free(s->blamed_commit);
2619 s->blamed_commit =
2620 SIMPLEQ_FIRST(&s->blamed_commits);
2621 err = run_blame(&s->blame, view, &s->blame_complete,
2622 &s->first_displayed_line, &s->last_displayed_line,
2623 &s->selected_line, &s->done, &s->eof, s->path,
2624 s->blamed_commit->id, s->repo);
2625 if (err)
2626 break;
2627 break;
2629 case KEY_ENTER:
2630 case '\r':
2631 err = open_selected_commit(&pobj, &obj,
2632 s->blame.lines, s->first_displayed_line,
2633 s->selected_line, s->repo);
2634 if (err)
2635 break;
2636 if (pobj == NULL && obj == NULL)
2637 break;
2639 if (view_is_parent_view(view))
2640 begin_x = view_split_begin_x(view->begin_x);
2641 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2642 if (diff_view == NULL) {
2643 err = got_error_from_errno();
2644 break;
2646 err = open_diff_view(diff_view, pobj, obj, s->repo);
2647 if (err) {
2648 view_close(diff_view);
2649 break;
2651 if (view_is_parent_view(view)) {
2652 err = view_close_child(view);
2653 if (err)
2654 return err;
2655 err = view_set_child(view, diff_view);
2656 if (err) {
2657 view_close(diff_view);
2658 break;
2660 if (!view_is_splitscreen(diff_view)) {
2661 *focus_view = diff_view;
2662 view->child_focussed = 1;
2664 } else
2665 *new_view = diff_view;
2666 if (pobj) {
2667 got_object_close(pobj);
2668 pobj = NULL;
2670 got_object_close(obj);
2671 obj = NULL;
2672 if (err)
2673 break;
2674 break;
2675 case KEY_NPAGE:
2676 case ' ':
2677 if (s->last_displayed_line >= s->blame.nlines &&
2678 s->selected_line < view->nlines - 2) {
2679 s->selected_line = MIN(s->blame.nlines,
2680 view->nlines - 2);
2681 break;
2683 if (s->last_displayed_line + view->nlines - 2
2684 <= s->blame.nlines)
2685 s->first_displayed_line +=
2686 view->nlines - 2;
2687 else
2688 s->first_displayed_line =
2689 s->blame.nlines -
2690 (view->nlines - 3);
2691 break;
2692 case KEY_RESIZE:
2693 if (s->selected_line > view->nlines - 2) {
2694 s->selected_line = MIN(s->blame.nlines,
2695 view->nlines - 2);
2697 break;
2698 default:
2699 break;
2701 done:
2702 if (pobj)
2703 got_object_close(pobj);
2704 return thread_err ? thread_err : err;
2707 static const struct got_error *
2708 cmd_blame(int argc, char *argv[])
2710 const struct got_error *error;
2711 struct got_repository *repo = NULL;
2712 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2713 struct got_object_id *commit_id = NULL;
2714 char *commit_id_str = NULL;
2715 int ch;
2716 struct tog_view *view;
2718 #ifndef PROFILE
2719 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2720 == -1)
2721 err(1, "pledge");
2722 #endif
2724 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2725 switch (ch) {
2726 case 'c':
2727 commit_id_str = optarg;
2728 break;
2729 case 'r':
2730 repo_path = realpath(optarg, NULL);
2731 if (repo_path == NULL)
2732 err(1, "-r option");
2733 break;
2734 default:
2735 usage();
2736 /* NOTREACHED */
2740 argc -= optind;
2741 argv += optind;
2743 if (argc == 1)
2744 path = argv[0];
2745 else
2746 usage_blame();
2748 cwd = getcwd(NULL, 0);
2749 if (cwd == NULL) {
2750 error = got_error_from_errno();
2751 goto done;
2753 if (repo_path == NULL) {
2754 repo_path = strdup(cwd);
2755 if (repo_path == NULL) {
2756 error = got_error_from_errno();
2757 goto done;
2762 error = got_repo_open(&repo, repo_path);
2763 if (error != NULL)
2764 return error;
2766 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2767 if (error != NULL)
2768 goto done;
2770 if (commit_id_str == NULL) {
2771 struct got_reference *head_ref;
2772 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2773 if (error != NULL)
2774 goto done;
2775 error = got_ref_resolve(&commit_id, repo, head_ref);
2776 got_ref_close(head_ref);
2777 } else {
2778 struct got_object *obj;
2779 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2780 if (error != NULL)
2781 goto done;
2782 commit_id = got_object_id_dup(got_object_get_id(obj));
2783 if (commit_id == NULL)
2784 error = got_error_from_errno();
2785 got_object_close(obj);
2787 if (error != NULL)
2788 goto done;
2790 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2791 if (view == NULL) {
2792 error = got_error_from_errno();
2793 goto done;
2795 error = open_blame_view(view, in_repo_path, commit_id, repo);
2796 if (error)
2797 goto done;
2798 error = view_loop(view);
2799 done:
2800 free(repo_path);
2801 free(cwd);
2802 free(commit_id);
2803 if (repo)
2804 got_repo_close(repo);
2805 return error;
2808 static const struct got_error *
2809 draw_tree_entries(struct tog_view *view,
2810 struct got_tree_entry **first_displayed_entry,
2811 struct got_tree_entry **last_displayed_entry,
2812 struct got_tree_entry **selected_entry, int *ndisplayed,
2813 const char *label, int show_ids, const char *parent_path,
2814 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2816 const struct got_error *err = NULL;
2817 struct got_tree_entry *te;
2818 wchar_t *wline;
2819 int width, n;
2821 *ndisplayed = 0;
2823 werase(view->window);
2825 if (limit == 0)
2826 return NULL;
2828 err = format_line(&wline, &width, label, view->ncols);
2829 if (err)
2830 return err;
2831 if (view_needs_focus_indication(view))
2832 wstandout(view->window);
2833 waddwstr(view->window, wline);
2834 if (view_needs_focus_indication(view))
2835 wstandend(view->window);
2836 free(wline);
2837 wline = NULL;
2838 if (width < view->ncols)
2839 waddch(view->window, '\n');
2840 if (--limit <= 0)
2841 return NULL;
2842 err = format_line(&wline, &width, parent_path, view->ncols);
2843 if (err)
2844 return err;
2845 waddwstr(view->window, wline);
2846 free(wline);
2847 wline = NULL;
2848 if (width < view->ncols)
2849 waddch(view->window, '\n');
2850 if (--limit <= 0)
2851 return NULL;
2852 waddch(view->window, '\n');
2853 if (--limit <= 0)
2854 return NULL;
2856 te = SIMPLEQ_FIRST(&entries->head);
2857 if (*first_displayed_entry == NULL) {
2858 if (selected == 0) {
2859 if (view->focussed)
2860 wstandout(view->window);
2861 *selected_entry = NULL;
2863 waddstr(view->window, " ..\n"); /* parent directory */
2864 if (selected == 0 && view->focussed)
2865 wstandend(view->window);
2866 (*ndisplayed)++;
2867 if (--limit <= 0)
2868 return NULL;
2869 n = 1;
2870 } else {
2871 n = 0;
2872 while (te != *first_displayed_entry)
2873 te = SIMPLEQ_NEXT(te, entry);
2876 while (te) {
2877 char *line = NULL, *id_str = NULL;
2879 if (show_ids) {
2880 err = got_object_id_str(&id_str, te->id);
2881 if (err)
2882 return got_error_from_errno();
2884 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2885 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2886 free(id_str);
2887 return got_error_from_errno();
2889 free(id_str);
2890 err = format_line(&wline, &width, line, view->ncols);
2891 if (err) {
2892 free(line);
2893 break;
2895 if (n == selected) {
2896 if (view->focussed)
2897 wstandout(view->window);
2898 *selected_entry = te;
2900 waddwstr(view->window, wline);
2901 if (width < view->ncols)
2902 waddch(view->window, '\n');
2903 if (n == selected && view->focussed)
2904 wstandend(view->window);
2905 free(line);
2906 free(wline);
2907 wline = NULL;
2908 n++;
2909 (*ndisplayed)++;
2910 *last_displayed_entry = te;
2911 if (--limit <= 0)
2912 break;
2913 te = SIMPLEQ_NEXT(te, entry);
2916 return err;
2919 static void
2920 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2921 const struct got_tree_entries *entries, int isroot)
2923 struct got_tree_entry *te, *prev;
2924 int i;
2926 if (*first_displayed_entry == NULL)
2927 return;
2929 te = SIMPLEQ_FIRST(&entries->head);
2930 if (*first_displayed_entry == te) {
2931 if (!isroot)
2932 *first_displayed_entry = NULL;
2933 return;
2936 /* XXX this is stupid... switch to TAILQ? */
2937 for (i = 0; i < maxscroll; i++) {
2938 while (te != *first_displayed_entry) {
2939 prev = te;
2940 te = SIMPLEQ_NEXT(te, entry);
2942 *first_displayed_entry = prev;
2943 te = SIMPLEQ_FIRST(&entries->head);
2945 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2946 *first_displayed_entry = NULL;
2949 static void
2950 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2951 struct got_tree_entry *last_displayed_entry,
2952 const struct got_tree_entries *entries)
2954 struct got_tree_entry *next;
2955 int n = 0;
2957 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2958 return;
2960 if (*first_displayed_entry)
2961 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2962 else
2963 next = SIMPLEQ_FIRST(&entries->head);
2964 while (next) {
2965 *first_displayed_entry = next;
2966 if (++n >= maxscroll)
2967 break;
2968 next = SIMPLEQ_NEXT(next, entry);
2972 static const struct got_error *
2973 tree_entry_path(char **path, struct tog_parent_trees *parents,
2974 struct got_tree_entry *te)
2976 const struct got_error *err = NULL;
2977 struct tog_parent_tree *pt;
2978 size_t len = 2; /* for leading slash and NUL */
2980 TAILQ_FOREACH(pt, parents, entry)
2981 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2982 if (te)
2983 len += strlen(te->name);
2985 *path = calloc(1, len);
2986 if (path == NULL)
2987 return got_error_from_errno();
2989 (*path)[0] = '/';
2990 pt = TAILQ_LAST(parents, tog_parent_trees);
2991 while (pt) {
2992 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2993 err = got_error(GOT_ERR_NO_SPACE);
2994 goto done;
2996 if (strlcat(*path, "/", len) >= len) {
2997 err = got_error(GOT_ERR_NO_SPACE);
2998 goto done;
3000 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3002 if (te) {
3003 if (strlcat(*path, te->name, len) >= len) {
3004 err = got_error(GOT_ERR_NO_SPACE);
3005 goto done;
3008 done:
3009 if (err) {
3010 free(*path);
3011 *path = NULL;
3013 return err;
3016 static const struct got_error *
3017 blame_tree_entry(struct tog_view **new_view, int begin_x,
3018 struct got_tree_entry *te, struct tog_parent_trees *parents,
3019 struct got_object_id *commit_id, struct got_repository *repo)
3021 const struct got_error *err = NULL;
3022 char *path;
3023 struct tog_view *blame_view;
3025 err = tree_entry_path(&path, parents, te);
3026 if (err)
3027 return err;
3029 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3030 if (blame_view == NULL)
3031 return got_error_from_errno();
3033 err = open_blame_view(blame_view, path, commit_id, repo);
3034 if (err) {
3035 view_close(blame_view);
3036 free(path);
3037 } else
3038 *new_view = blame_view;
3039 return err;
3042 static const struct got_error *
3043 log_tree_entry(struct tog_view **new_view, int begin_x,
3044 struct got_tree_entry *te, struct tog_parent_trees *parents,
3045 struct got_object_id *commit_id, struct got_repository *repo)
3047 struct tog_view *log_view;
3048 const struct got_error *err = NULL;
3049 char *path;
3051 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3052 if (log_view == NULL)
3053 return got_error_from_errno();
3055 err = tree_entry_path(&path, parents, te);
3056 if (err)
3057 return err;
3059 err = open_log_view(log_view, commit_id, repo, path, 0);
3060 if (err)
3061 view_close(log_view);
3062 else
3063 *new_view = log_view;
3064 free(path);
3065 return err;
3068 static const struct got_error *
3069 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3070 struct got_object_id *commit_id, struct got_repository *repo)
3072 const struct got_error *err = NULL;
3073 char *commit_id_str = NULL;
3074 struct tog_tree_view_state *s = &view->state.tree;
3076 TAILQ_INIT(&s->parents);
3078 err = got_object_id_str(&commit_id_str, commit_id);
3079 if (err != NULL)
3080 goto done;
3082 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3083 err = got_error_from_errno();
3084 goto done;
3087 s->root = s->tree = root;
3088 s->entries = got_object_tree_get_entries(root);
3089 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3090 s->commit_id = got_object_id_dup(commit_id);
3091 if (s->commit_id == NULL) {
3092 err = got_error_from_errno();
3093 goto done;
3095 s->repo = repo;
3097 view->show = show_tree_view;
3098 view->input = input_tree_view;
3099 view->close = close_tree_view;
3100 done:
3101 free(commit_id_str);
3102 if (err) {
3103 free(s->tree_label);
3104 s->tree_label = NULL;
3106 return err;
3109 static const struct got_error *
3110 close_tree_view(struct tog_view *view)
3112 struct tog_tree_view_state *s = &view->state.tree;
3114 free(s->tree_label);
3115 s->tree_label = NULL;
3116 free(s->commit_id);
3117 s->commit_id = NULL;
3118 while (!TAILQ_EMPTY(&s->parents)) {
3119 struct tog_parent_tree *parent;
3120 parent = TAILQ_FIRST(&s->parents);
3121 TAILQ_REMOVE(&s->parents, parent, entry);
3122 free(parent);
3125 if (s->tree != s->root)
3126 got_object_tree_close(s->tree);
3127 got_object_tree_close(s->root);
3129 return NULL;
3132 static const struct got_error *
3133 show_tree_view(struct tog_view *view)
3135 const struct got_error *err = NULL;
3136 struct tog_tree_view_state *s = &view->state.tree;
3137 char *parent_path;
3139 err = tree_entry_path(&parent_path, &s->parents, NULL);
3140 if (err)
3141 return err;
3143 err = draw_tree_entries(view, &s->first_displayed_entry,
3144 &s->last_displayed_entry, &s->selected_entry,
3145 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3146 s->entries, s->selected, view->nlines, s->tree == s->root);
3147 free(parent_path);
3149 view_vborder(view);
3150 return err;
3153 static const struct got_error *
3154 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3155 struct tog_view **focus_view, struct tog_view *view, int ch)
3157 const struct got_error *err = NULL;
3158 struct tog_tree_view_state *s = &view->state.tree;
3159 struct tog_view *log_view;
3160 int begin_x = 0;
3162 switch (ch) {
3163 case 'i':
3164 s->show_ids = !s->show_ids;
3165 break;
3166 case 'l':
3167 if (!s->selected_entry)
3168 break;
3169 if (view_is_parent_view(view))
3170 begin_x = view_split_begin_x(view->begin_x);
3171 err = log_tree_entry(&log_view, begin_x,
3172 s->selected_entry, &s->parents,
3173 s->commit_id, s->repo);
3174 if (view_is_parent_view(view)) {
3175 err = view_close_child(view);
3176 if (err)
3177 return err;
3178 err = view_set_child(view, log_view);
3179 if (err) {
3180 view_close(log_view);
3181 break;
3183 *focus_view = log_view;
3184 view->child_focussed = 1;
3185 } else
3186 *new_view = log_view;
3187 break;
3188 case 'k':
3189 case KEY_UP:
3190 if (s->selected > 0)
3191 s->selected--;
3192 if (s->selected > 0)
3193 break;
3194 tree_scroll_up(&s->first_displayed_entry, 1,
3195 s->entries, s->tree == s->root);
3196 break;
3197 case KEY_PPAGE:
3198 if (SIMPLEQ_FIRST(&s->entries->head) ==
3199 s->first_displayed_entry) {
3200 if (s->tree != s->root)
3201 s->first_displayed_entry = NULL;
3202 s->selected = 0;
3203 break;
3205 tree_scroll_up(&s->first_displayed_entry,
3206 view->nlines, s->entries,
3207 s->tree == s->root);
3208 break;
3209 case 'j':
3210 case KEY_DOWN:
3211 if (s->selected < s->ndisplayed - 1) {
3212 s->selected++;
3213 break;
3215 tree_scroll_down(&s->first_displayed_entry, 1,
3216 s->last_displayed_entry, s->entries);
3217 break;
3218 case KEY_NPAGE:
3219 tree_scroll_down(&s->first_displayed_entry,
3220 view->nlines, s->last_displayed_entry,
3221 s->entries);
3222 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3223 entry))
3224 break;
3225 /* can't scroll any further; move cursor down */
3226 if (s->selected < s->ndisplayed - 1)
3227 s->selected = s->ndisplayed - 1;
3228 break;
3229 case KEY_ENTER:
3230 case '\r':
3231 if (s->selected_entry == NULL) {
3232 struct tog_parent_tree *parent;
3233 case KEY_BACKSPACE:
3234 /* user selected '..' */
3235 if (s->tree == s->root)
3236 break;
3237 parent = TAILQ_FIRST(&s->parents);
3238 TAILQ_REMOVE(&s->parents, parent,
3239 entry);
3240 got_object_tree_close(s->tree);
3241 s->tree = parent->tree;
3242 s->entries =
3243 got_object_tree_get_entries(s->tree);
3244 s->first_displayed_entry =
3245 parent->first_displayed_entry;
3246 s->selected_entry =
3247 parent->selected_entry;
3248 s->selected = parent->selected;
3249 free(parent);
3250 } else if (S_ISDIR(s->selected_entry->mode)) {
3251 struct tog_parent_tree *parent;
3252 struct got_tree_object *child;
3253 err = got_object_open_as_tree(&child,
3254 s->repo, s->selected_entry->id);
3255 if (err)
3256 break;
3257 parent = calloc(1, sizeof(*parent));
3258 if (parent == NULL) {
3259 err = got_error_from_errno();
3260 break;
3262 parent->tree = s->tree;
3263 parent->first_displayed_entry =
3264 s->first_displayed_entry;
3265 parent->selected_entry = s->selected_entry;
3266 parent->selected = s->selected;
3267 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3268 s->tree = child;
3269 s->entries =
3270 got_object_tree_get_entries(s->tree);
3271 s->selected = 0;
3272 s->first_displayed_entry = NULL;
3273 } else if (S_ISREG(s->selected_entry->mode)) {
3274 struct tog_view *blame_view;
3275 int begin_x = view_is_parent_view(view) ?
3276 view_split_begin_x(view->begin_x) : 0;
3278 err = blame_tree_entry(&blame_view, begin_x,
3279 s->selected_entry, &s->parents, s->commit_id,
3280 s->repo);
3281 if (err)
3282 break;
3283 if (view_is_parent_view(view)) {
3284 err = view_close_child(view);
3285 if (err)
3286 return err;
3287 err = view_set_child(view, blame_view);
3288 if (err) {
3289 view_close(blame_view);
3290 break;
3292 *focus_view = blame_view;
3293 view->child_focussed = 1;
3294 } else
3295 *new_view = blame_view;
3297 break;
3298 case KEY_RESIZE:
3299 if (s->selected > view->nlines)
3300 s->selected = s->ndisplayed - 1;
3301 break;
3302 default:
3303 break;
3306 return err;
3309 __dead static void
3310 usage_tree(void)
3312 endwin();
3313 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3314 getprogname());
3315 exit(1);
3318 static const struct got_error *
3319 cmd_tree(int argc, char *argv[])
3321 const struct got_error *error;
3322 struct got_repository *repo = NULL;
3323 char *repo_path = NULL;
3324 struct got_object_id *commit_id = NULL;
3325 char *commit_id_arg = NULL;
3326 struct got_commit_object *commit = NULL;
3327 struct got_tree_object *tree = NULL;
3328 int ch;
3329 struct tog_view *view;
3331 #ifndef PROFILE
3332 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3333 == -1)
3334 err(1, "pledge");
3335 #endif
3337 while ((ch = getopt(argc, argv, "c:")) != -1) {
3338 switch (ch) {
3339 case 'c':
3340 commit_id_arg = optarg;
3341 break;
3342 default:
3343 usage();
3344 /* NOTREACHED */
3348 argc -= optind;
3349 argv += optind;
3351 if (argc == 0) {
3352 repo_path = getcwd(NULL, 0);
3353 if (repo_path == NULL)
3354 return got_error_from_errno();
3355 } else if (argc == 1) {
3356 repo_path = realpath(argv[0], NULL);
3357 if (repo_path == NULL)
3358 return got_error_from_errno();
3359 } else
3360 usage_log();
3362 error = got_repo_open(&repo, repo_path);
3363 free(repo_path);
3364 if (error != NULL)
3365 return error;
3367 if (commit_id_arg == NULL) {
3368 error = get_head_commit_id(&commit_id, repo);
3369 if (error != NULL)
3370 goto done;
3371 } else {
3372 struct got_object *obj;
3373 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3374 if (error == NULL) {
3375 commit_id = got_object_id_dup(got_object_get_id(obj));
3376 if (commit_id == NULL)
3377 error = got_error_from_errno();
3380 if (error != NULL)
3381 goto done;
3383 error = got_object_open_as_commit(&commit, repo, commit_id);
3384 if (error != NULL)
3385 goto done;
3387 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3388 if (error != NULL)
3389 goto done;
3391 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3392 if (view == NULL) {
3393 error = got_error_from_errno();
3394 goto done;
3396 error = open_tree_view(view, tree, commit_id, repo);
3397 if (error)
3398 goto done;
3399 error = view_loop(view);
3400 done:
3401 free(commit_id);
3402 if (commit)
3403 got_object_commit_close(commit);
3404 if (tree)
3405 got_object_tree_close(tree);
3406 if (repo)
3407 got_repo_close(repo);
3408 return error;
3410 static void
3411 init_curses(void)
3413 initscr();
3414 cbreak();
3415 halfdelay(1); /* Do fast refresh while initial view is loading. */
3416 noecho();
3417 nonl();
3418 intrflush(stdscr, FALSE);
3419 keypad(stdscr, TRUE);
3420 curs_set(0);
3423 __dead static void
3424 usage(void)
3426 int i;
3428 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3429 "Available commands:\n", getprogname());
3430 for (i = 0; i < nitems(tog_commands); i++) {
3431 struct tog_cmd *cmd = &tog_commands[i];
3432 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3434 exit(1);
3437 static char **
3438 make_argv(const char *arg0, const char *arg1)
3440 char **argv;
3441 int argc = (arg1 == NULL ? 1 : 2);
3443 argv = calloc(argc, sizeof(char *));
3444 if (argv == NULL)
3445 err(1, "calloc");
3446 argv[0] = strdup(arg0);
3447 if (argv[0] == NULL)
3448 err(1, "calloc");
3449 if (arg1) {
3450 argv[1] = strdup(arg1);
3451 if (argv[1] == NULL)
3452 err(1, "calloc");
3455 return argv;
3458 int
3459 main(int argc, char *argv[])
3461 const struct got_error *error = NULL;
3462 struct tog_cmd *cmd = NULL;
3463 int ch, hflag = 0;
3464 char **cmd_argv = NULL;
3466 setlocale(LC_ALL, "");
3468 while ((ch = getopt(argc, argv, "h")) != -1) {
3469 switch (ch) {
3470 case 'h':
3471 hflag = 1;
3472 break;
3473 default:
3474 usage();
3475 /* NOTREACHED */
3479 argc -= optind;
3480 argv += optind;
3481 optind = 0;
3482 optreset = 1;
3484 if (argc == 0) {
3485 if (hflag)
3486 usage();
3487 /* Build an argument vector which runs a default command. */
3488 cmd = &tog_commands[0];
3489 cmd_argv = make_argv(cmd->name, NULL);
3490 argc = 1;
3491 } else {
3492 int i;
3494 /* Did the user specific a command? */
3495 for (i = 0; i < nitems(tog_commands); i++) {
3496 if (strncmp(tog_commands[i].name, argv[0],
3497 strlen(argv[0])) == 0) {
3498 cmd = &tog_commands[i];
3499 if (hflag)
3500 tog_commands[i].cmd_usage();
3501 break;
3504 if (cmd == NULL) {
3505 /* Did the user specify a repository? */
3506 char *repo_path = realpath(argv[0], NULL);
3507 if (repo_path) {
3508 struct got_repository *repo;
3509 error = got_repo_open(&repo, repo_path);
3510 if (error == NULL)
3511 got_repo_close(repo);
3512 } else
3513 error = got_error_from_errno();
3514 if (error) {
3515 if (hflag) {
3516 fprintf(stderr, "%s: '%s' is not a "
3517 "known command\n", getprogname(),
3518 argv[0]);
3519 usage();
3521 fprintf(stderr, "%s: '%s' is neither a known "
3522 "command nor a path to a repository\n",
3523 getprogname(), argv[0]);
3524 free(repo_path);
3525 return 1;
3527 cmd = &tog_commands[0];
3528 cmd_argv = make_argv(cmd->name, repo_path);
3529 argc = 2;
3530 free(repo_path);
3534 init_curses();
3536 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3537 if (error)
3538 goto done;
3539 done:
3540 endwin();
3541 free(cmd_argv);
3542 if (error)
3543 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3544 return 0;