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 nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct tog_cmd {
58 const char *name;
59 const struct got_error *(*cmd_main)(int, char *[]);
60 void (*cmd_usage)(void);
61 const char *descr;
62 };
64 __dead static void usage(void);
65 __dead static void usage_log(void);
66 __dead static void usage_diff(void);
67 __dead static void usage_blame(void);
68 __dead static void usage_tree(void);
70 static const struct got_error* cmd_log(int, char *[]);
71 static const struct got_error* cmd_diff(int, char *[]);
72 static const struct got_error* cmd_blame(int, char *[]);
73 static const struct got_error* cmd_tree(int, char *[]);
75 static struct tog_cmd tog_commands[] = {
76 { "log", cmd_log, usage_log,
77 "show repository history" },
78 { "diff", cmd_diff, usage_diff,
79 "compare files and directories" },
80 { "blame", cmd_blame, usage_blame,
81 "show line-by-line file history" },
82 { "tree", cmd_tree, usage_tree,
83 "browse trees in repository" },
84 };
86 enum tog_view_type {
87 TOG_VIEW_DIFF,
88 TOG_VIEW_LOG,
89 TOG_VIEW_BLAME,
90 TOG_VIEW_TREE
91 };
93 struct tog_diff_view_state {
94 struct got_object_id *id1, *id2;
95 FILE *f;
96 int first_displayed_line;
97 int last_displayed_line;
98 int eof;
99 int diff_context;
100 struct got_repository *repo;
101 };
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
117 struct tog_log_thread_args {
118 pthread_cond_t need_commits;
119 int commits_needed;
120 struct got_commit_graph *graph;
121 struct commit_queue *commits;
122 const char *in_repo_path;
123 struct got_object_id *start_id;
124 struct got_repository *repo;
125 int log_complete;
126 sig_atomic_t *quit;
127 struct tog_view *view;
128 struct commit_queue_entry **first_displayed_entry;
129 struct commit_queue_entry **last_displayed_entry;
130 struct commit_queue_entry **selected_entry;
131 int *selected;
132 };
134 struct tog_log_view_state {
135 struct commit_queue commits;
136 struct commit_queue_entry *first_displayed_entry;
137 struct commit_queue_entry *last_displayed_entry;
138 struct commit_queue_entry *selected_entry;
139 int selected;
140 char *in_repo_path;
141 struct got_repository *repo;
142 struct got_object_id *start_id;
143 sig_atomic_t quit;
144 pthread_t thread;
145 struct tog_log_thread_args thread_args;
146 };
148 struct tog_blame_cb_args {
149 struct tog_blame_line *lines; /* one per line */
150 int nlines;
152 struct tog_view *view;
153 struct got_object_id *commit_id;
154 FILE *f;
155 const char *path;
156 int *first_displayed_line;
157 int *last_displayed_line;
158 int *selected_line;
159 int *quit;
160 int *eof;
161 };
163 struct tog_blame_thread_args {
164 const char *path;
165 struct got_repository *repo;
166 struct tog_blame_cb_args *cb_args;
167 int *complete;
168 };
170 struct tog_blame {
171 FILE *f;
172 size_t filesize;
173 struct tog_blame_line *lines;
174 size_t nlines;
175 pthread_t thread;
176 struct tog_blame_thread_args thread_args;
177 struct tog_blame_cb_args cb_args;
178 const char *path;
179 };
181 struct tog_blame_view_state {
182 int first_displayed_line;
183 int last_displayed_line;
184 int selected_line;
185 int blame_complete;
186 int eof;
187 int done;
188 struct got_object_id_queue blamed_commits;
189 struct got_object_qid *blamed_commit;
190 char *path;
191 struct got_repository *repo;
192 struct got_object_id *commit_id;
193 struct tog_blame blame;
194 };
196 struct tog_parent_tree {
197 TAILQ_ENTRY(tog_parent_tree) entry;
198 struct got_tree_object *tree;
199 struct got_tree_entry *first_displayed_entry;
200 struct got_tree_entry *selected_entry;
201 int selected;
202 };
204 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
206 struct tog_tree_view_state {
207 char *tree_label;
208 struct got_tree_object *root;
209 struct got_tree_object *tree;
210 const struct got_tree_entries *entries;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *last_displayed_entry;
213 struct got_tree_entry *selected_entry;
214 int nentries, ndisplayed, selected, show_ids;
215 struct tog_parent_trees parents;
216 struct got_object_id *commit_id;
217 struct got_repository *repo;
218 };
220 /*
221 * We implement two types of views: parent views and child views.
223 * The 'Tab' key switches between a parent view and its child view.
224 * Child views are shown side-by-side to their parent view, provided
225 * there is enough screen estate.
227 * When a new view is opened from within a parent view, this new view
228 * becomes a child view of the parent view, replacing any existing child.
230 * When a new view is opened from within a child view, this new view
231 * becomes a parent view which will obscure the views below until the
232 * user quits the new parent view by typing 'q'.
234 * This list of views contains parent views only.
235 * Child views are only pointed to by their parent view.
236 */
237 TAILQ_HEAD(tog_view_list_head, tog_view);
239 struct tog_view {
240 TAILQ_ENTRY(tog_view) entry;
241 WINDOW *window;
242 PANEL *panel;
243 int nlines, ncols, begin_y, begin_x;
244 int lines, cols; /* copies of LINES and COLS */
245 int focussed;
246 struct tog_view *parent;
247 struct tog_view *child;
248 int child_focussed;
250 /* type-specific state */
251 enum tog_view_type type;
252 union {
253 struct tog_diff_view_state diff;
254 struct tog_log_view_state log;
255 struct tog_blame_view_state blame;
256 struct tog_tree_view_state tree;
257 } state;
259 const struct got_error *(*show)(struct tog_view *);
260 const struct got_error *(*input)(struct tog_view **,
261 struct tog_view **, struct tog_view**, struct tog_view *, int);
262 const struct got_error *(*close)(struct tog_view *);
263 };
265 static const struct got_error *open_diff_view(struct tog_view *,
266 struct got_object *, struct got_object *, struct got_repository *);
267 static const struct got_error *show_diff_view(struct tog_view *);
268 static const struct got_error *input_diff_view(struct tog_view **,
269 struct tog_view **, struct tog_view **, struct tog_view *, int);
270 static const struct got_error* close_diff_view(struct tog_view *);
272 static const struct got_error *open_log_view(struct tog_view *,
273 struct got_object_id *, struct got_repository *, const char *);
274 static const struct got_error * show_log_view(struct tog_view *);
275 static const struct got_error *input_log_view(struct tog_view **,
276 struct tog_view **, struct tog_view **, struct tog_view *, int);
277 static const struct got_error *close_log_view(struct tog_view *);
279 static const struct got_error *open_blame_view(struct tog_view *, char *,
280 struct got_object_id *, struct got_repository *);
281 static const struct got_error *show_blame_view(struct tog_view *);
282 static const struct got_error *input_blame_view(struct tog_view **,
283 struct tog_view **, struct tog_view **, struct tog_view *, int);
284 static const struct got_error *close_blame_view(struct tog_view *);
286 static const struct got_error *open_tree_view(struct tog_view *,
287 struct got_tree_object *, struct got_object_id *, struct got_repository *);
288 static const struct got_error *show_tree_view(struct tog_view *);
289 static const struct got_error *input_tree_view(struct tog_view **,
290 struct tog_view **, struct tog_view **, struct tog_view *, int);
291 static const struct got_error *close_tree_view(struct tog_view *);
293 static const struct got_error *
294 view_close(struct tog_view *view)
296 const struct got_error *err = NULL;
298 if (view->child) {
299 view_close(view->child);
300 view->child = NULL;
302 if (view->close)
303 err = view->close(view);
304 if (view->panel)
305 del_panel(view->panel);
306 if (view->window)
307 delwin(view->window);
308 free(view);
309 return err;
312 static struct tog_view *
313 view_open(int nlines, int ncols, int begin_y, int begin_x,
314 enum tog_view_type type)
316 struct tog_view *view = calloc(1, sizeof(*view));
318 if (view == NULL)
319 return NULL;
321 view->type = type;
322 view->lines = LINES;
323 view->cols = COLS;
324 view->nlines = nlines ? nlines : LINES - begin_y;
325 view->ncols = ncols ? ncols : COLS - begin_x;
326 view->begin_y = begin_y;
327 view->begin_x = begin_x;
328 view->window = newwin(nlines, ncols, begin_y, begin_x);
329 if (view->window == NULL) {
330 view_close(view);
331 return NULL;
333 view->panel = new_panel(view->window);
334 if (view->panel == NULL ||
335 set_panel_userptr(view->panel, view) != OK) {
336 view_close(view);
337 return NULL;
340 keypad(view->window, TRUE);
341 return view;
344 static int
345 view_split_begin_x(int begin_x)
347 if (begin_x > 0)
348 return 0;
349 return (COLS >= 120 ? COLS/2 : 0);
352 static const struct got_error *view_resize(struct tog_view *);
354 static const struct got_error *
355 view_splitscreen(struct tog_view *view)
357 const struct got_error *err = NULL;
359 view->begin_y = 0;
360 view->begin_x = view_split_begin_x(0);
361 view->nlines = LINES;
362 view->ncols = COLS - view->begin_x;
363 view->lines = LINES;
364 view->cols = COLS;
365 err = view_resize(view);
366 if (err)
367 return err;
369 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
370 return got_error_from_errno();
372 return NULL;
375 static const struct got_error *
376 view_fullscreen(struct tog_view *view)
378 const struct got_error *err = NULL;
380 view->begin_x = 0;
381 view->begin_y = 0;
382 view->nlines = LINES;
383 view->ncols = COLS;
384 view->lines = LINES;
385 view->cols = COLS;
386 err = view_resize(view);
387 if (err)
388 return err;
390 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
391 return got_error_from_errno();
393 return NULL;
396 static int
397 view_is_parent_view(struct tog_view *view)
399 return view->parent == NULL;
402 static const struct got_error *
403 view_resize(struct tog_view *view)
405 int nlines, ncols;
407 if (view->lines > LINES)
408 nlines = view->nlines - (view->lines - LINES);
409 else
410 nlines = view->nlines + (LINES - view->lines);
412 if (view->cols > COLS)
413 ncols = view->ncols - (view->cols - COLS);
414 else
415 ncols = view->ncols + (COLS - view->cols);
417 if (wresize(view->window, nlines, ncols) == ERR)
418 return got_error_from_errno();
419 replace_panel(view->panel, view->window);
421 view->nlines = nlines;
422 view->ncols = ncols;
423 view->lines = LINES;
424 view->cols = COLS;
426 if (view->child) {
427 view->child->begin_x = view_split_begin_x(view->begin_x);
428 if (view->child->begin_x == 0) {
429 view_fullscreen(view->child);
430 if (view->child->focussed)
431 show_panel(view->child->panel);
432 else
433 show_panel(view->panel);
434 } else {
435 view_splitscreen(view->child);
436 show_panel(view->child->panel);
440 return NULL;
443 static const struct got_error *
444 view_close_child(struct tog_view *view)
446 const struct got_error *err = NULL;
448 if (view->child == NULL)
449 return NULL;
451 err = view_close(view->child);
452 view->child = NULL;
453 return err;
456 static const struct got_error *
457 view_set_child(struct tog_view *view, struct tog_view *child)
459 const struct got_error *err = NULL;
461 view->child = child;
462 child->parent = view;
463 return err;
466 static int
467 view_is_splitscreen(struct tog_view *view)
469 return !view_is_parent_view(view) && view->begin_x > 0;
472 static const struct got_error *
473 view_input(struct tog_view **new, struct tog_view **dead,
474 struct tog_view **focus, int *done, struct tog_view *view,
475 struct tog_view_list_head *views)
477 const struct got_error *err = NULL;
478 struct tog_view *v;
479 int ch, errcode;
481 *new = NULL;
482 *dead = NULL;
483 *focus = NULL;
485 nodelay(stdscr, FALSE);
486 /* Allow threads to make progress while we are waiting for input. */
487 errcode = pthread_mutex_unlock(&tog_mutex);
488 if (errcode)
489 return got_error_set_errno(errcode);
490 ch = wgetch(view->window);
491 errcode = pthread_mutex_lock(&tog_mutex);
492 if (errcode)
493 return got_error_set_errno(errcode);
494 nodelay(stdscr, TRUE);
495 switch (ch) {
496 case ERR:
497 break;
498 case '\t':
499 if (view->child) {
500 *focus = view->child;
501 view->child_focussed = 1;
502 } else if (view->parent) {
503 *focus = view->parent;
504 view->parent->child_focussed = 0;
506 break;
507 case 'q':
508 err = view->input(new, dead, focus, view, ch);
509 *dead = view;
510 break;
511 case 'Q':
512 *done = 1;
513 break;
514 case 'f':
515 if (view_is_parent_view(view)) {
516 if (view->child == NULL)
517 break;
518 if (view_is_splitscreen(view->child)) {
519 *focus = view->child;
520 view->child_focussed = 1;
521 err = view_fullscreen(view->child);
522 } else
523 err = view_splitscreen(view->child);
524 if (err)
525 break;
526 err = view->child->input(new, dead, focus,
527 view->child, KEY_RESIZE);
528 } else {
529 if (view_is_splitscreen(view)) {
530 *focus = view;
531 view->parent->child_focussed = 1;
532 err = view_fullscreen(view);
533 } else {
534 err = view_splitscreen(view);
536 if (err)
537 break;
538 err = view->input(new, dead, focus, view,
539 KEY_RESIZE);
541 break;
542 case KEY_RESIZE:
543 TAILQ_FOREACH(v, views, entry) {
544 err = view_resize(v);
545 if (err)
546 return err;
547 err = v->input(new, dead, focus, v, ch);
548 if (err)
549 return err;
551 break;
552 default:
553 err = view->input(new, dead, focus, view, ch);
554 break;
557 return err;
560 void
561 view_vborder(struct tog_view *view)
563 PANEL *panel;
564 struct tog_view *view_above;
566 if (view->parent)
567 return view_vborder(view->parent);
569 panel = panel_above(view->panel);
570 if (panel == NULL)
571 return;
573 view_above = panel_userptr(panel);
574 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
575 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
578 int
579 view_needs_focus_indication(struct tog_view *view)
581 if (view_is_parent_view(view)) {
582 if (view->child == NULL || view->child_focussed)
583 return 0;
584 if (!view_is_splitscreen(view->child))
585 return 0;
586 } else if (!view_is_splitscreen(view))
587 return 0;
589 return view->focussed;
592 static const struct got_error *
593 view_loop(struct tog_view *view)
595 const struct got_error *err = NULL;
596 struct tog_view_list_head views;
597 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
598 int done = 0, errcode;
600 errcode = pthread_mutex_lock(&tog_mutex);
601 if (errcode)
602 return got_error_set_errno(errcode);
604 TAILQ_INIT(&views);
605 TAILQ_INSERT_HEAD(&views, view, entry);
607 main_view = view;
608 view->focussed = 1;
609 err = view->show(view);
610 if (err)
611 return err;
612 update_panels();
613 doupdate();
614 while (!TAILQ_EMPTY(&views) && !done) {
615 err = view_input(&new_view, &dead_view, &focus_view, &done,
616 view, &views);
617 if (err)
618 break;
619 if (dead_view) {
620 struct tog_view *prev = NULL;
622 if (view_is_parent_view(dead_view))
623 prev = TAILQ_PREV(dead_view,
624 tog_view_list_head, entry);
625 else
626 prev = view->parent;
628 if (dead_view->parent)
629 dead_view->parent->child = NULL;
630 else
631 TAILQ_REMOVE(&views, dead_view, entry);
633 err = view_close(dead_view);
634 if (err || dead_view == main_view)
635 goto done;
637 if (view == dead_view) {
638 if (focus_view)
639 view = focus_view;
640 else if (prev)
641 view = prev;
642 else if (!TAILQ_EMPTY(&views))
643 view = TAILQ_LAST(&views,
644 tog_view_list_head);
645 else
646 view = NULL;
647 if (view) {
648 if (view->child && view->child_focussed)
649 focus_view = view->child;
650 else
651 focus_view = view;
655 if (new_view) {
656 struct tog_view *v, *t;
657 /* Only allow one parent view per type. */
658 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
659 if (v->type != new_view->type)
660 continue;
661 TAILQ_REMOVE(&views, v, entry);
662 err = view_close(v);
663 if (err)
664 goto done;
665 if (v == view)
666 view = new_view;
667 break;
669 TAILQ_INSERT_TAIL(&views, new_view, entry);
670 if (focus_view == NULL)
671 focus_view = new_view;
673 if (focus_view) {
674 show_panel(focus_view->panel);
675 if (view)
676 view->focussed = 0;
677 focus_view->focussed = 1;
678 view = focus_view;
679 if (new_view)
680 show_panel(new_view->panel);
681 if (view->child && view_is_splitscreen(view->child))
682 show_panel(view->child->panel);
684 if (view) {
685 if (focus_view == NULL) {
686 focus_view = view;
687 focus_view->focussed = 1;
689 if (view->parent) {
690 err = view->parent->show(view->parent);
691 if (err)
692 goto done;
694 err = view->show(view);
695 if (err)
696 goto done;
697 if (view->child) {
698 err = view->child->show(view->child);
699 if (err)
700 goto done;
702 update_panels();
703 doupdate();
706 done:
707 while (!TAILQ_EMPTY(&views)) {
708 view = TAILQ_FIRST(&views);
709 TAILQ_REMOVE(&views, view, entry);
710 view_close(view);
713 errcode = pthread_mutex_unlock(&tog_mutex);
714 if (errcode)
715 return got_error_set_errno(errcode);
717 return err;
720 __dead static void
721 usage_log(void)
723 endwin();
724 fprintf(stderr,
725 "usage: %s log [-c commit] [-r repository-path] [path]\n",
726 getprogname());
727 exit(1);
730 /* Create newly allocated wide-character string equivalent to a byte string. */
731 static const struct got_error *
732 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
734 char *vis = NULL;
735 const struct got_error *err = NULL;
737 *ws = NULL;
738 *wlen = mbstowcs(NULL, s, 0);
739 if (*wlen == (size_t)-1) {
740 int vislen;
741 if (errno != EILSEQ)
742 return got_error_from_errno();
744 /* byte string invalid in current encoding; try to "fix" it */
745 err = got_mbsavis(&vis, &vislen, s);
746 if (err)
747 return err;
748 *wlen = mbstowcs(NULL, vis, 0);
749 if (*wlen == (size_t)-1) {
750 err = got_error_from_errno(); /* give up */
751 goto done;
755 *ws = calloc(*wlen + 1, sizeof(*ws));
756 if (*ws == NULL) {
757 err = got_error_from_errno();
758 goto done;
761 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
762 err = got_error_from_errno();
763 done:
764 free(vis);
765 if (err) {
766 free(*ws);
767 *ws = NULL;
768 *wlen = 0;
770 return err;
773 /* Format a line for display, ensuring that it won't overflow a width limit. */
774 static const struct got_error *
775 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
777 const struct got_error *err = NULL;
778 int cols = 0;
779 wchar_t *wline = NULL;
780 size_t wlen;
781 int i;
783 *wlinep = NULL;
784 *widthp = 0;
786 err = mbs2ws(&wline, &wlen, line);
787 if (err)
788 return err;
790 i = 0;
791 while (i < wlen && cols < wlimit) {
792 int width = wcwidth(wline[i]);
793 switch (width) {
794 case 0:
795 i++;
796 break;
797 case 1:
798 case 2:
799 if (cols + width <= wlimit)
800 cols += width;
801 i++;
802 break;
803 case -1:
804 if (wline[i] == L'\t')
805 cols += TABSIZE - ((cols + 1) % TABSIZE);
806 i++;
807 break;
808 default:
809 err = got_error_from_errno();
810 goto done;
813 wline[i] = L'\0';
814 if (widthp)
815 *widthp = cols;
816 done:
817 if (err)
818 free(wline);
819 else
820 *wlinep = wline;
821 return err;
824 static const struct got_error *
825 draw_commit(struct tog_view *view, struct got_commit_object *commit,
826 struct got_object_id *id)
828 const struct got_error *err = NULL;
829 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
830 char *logmsg0 = NULL, *logmsg = NULL;
831 char *author0 = NULL, *author = NULL;
832 wchar_t *wlogmsg = NULL, *wauthor = NULL;
833 int author_width, logmsg_width;
834 char *newline, *smallerthan;
835 char *line = NULL;
836 int col, limit;
837 static const size_t date_display_cols = 9;
838 static const size_t author_display_cols = 16;
839 const int avail = view->ncols;
841 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
842 &commit->tm_committer) >= sizeof(datebuf))
843 return got_error(GOT_ERR_NO_SPACE);
845 if (avail < date_display_cols)
846 limit = MIN(sizeof(datebuf) - 1, avail);
847 else
848 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
849 waddnstr(view->window, datebuf, limit);
850 col = limit + 1;
851 if (col > avail)
852 goto done;
854 author0 = strdup(commit->author);
855 if (author0 == NULL) {
856 err = got_error_from_errno();
857 goto done;
859 author = author0;
860 smallerthan = strchr(author, '<');
861 if (smallerthan)
862 *smallerthan = '\0';
863 else {
864 char *at = strchr(author, '@');
865 if (at)
866 *at = '\0';
868 limit = avail - col;
869 err = format_line(&wauthor, &author_width, author, limit);
870 if (err)
871 goto done;
872 waddwstr(view->window, wauthor);
873 col += author_width;
874 while (col <= avail && author_width < author_display_cols + 1) {
875 waddch(view->window, ' ');
876 col++;
877 author_width++;
879 if (col > avail)
880 goto done;
882 logmsg0 = strdup(commit->logmsg);
883 if (logmsg0 == NULL) {
884 err = got_error_from_errno();
885 goto done;
887 logmsg = logmsg0;
888 while (*logmsg == '\n')
889 logmsg++;
890 newline = strchr(logmsg, '\n');
891 if (newline)
892 *newline = '\0';
893 limit = avail - col;
894 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
895 if (err)
896 goto done;
897 waddwstr(view->window, wlogmsg);
898 col += logmsg_width;
899 while (col <= avail) {
900 waddch(view->window, ' ');
901 col++;
903 done:
904 free(logmsg0);
905 free(wlogmsg);
906 free(author0);
907 free(wauthor);
908 free(line);
909 return err;
912 static struct commit_queue_entry *
913 alloc_commit_queue_entry(struct got_commit_object *commit,
914 struct got_object_id *id)
916 struct commit_queue_entry *entry;
918 entry = calloc(1, sizeof(*entry));
919 if (entry == NULL)
920 return NULL;
922 entry->id = id;
923 entry->commit = commit;
924 return entry;
927 static void
928 pop_commit(struct commit_queue *commits)
930 struct commit_queue_entry *entry;
932 entry = TAILQ_FIRST(&commits->head);
933 TAILQ_REMOVE(&commits->head, entry, entry);
934 got_object_commit_close(entry->commit);
935 commits->ncommits--;
936 /* Don't free entry->id! It is owned by the commit graph. */
937 free(entry);
940 static void
941 free_commits(struct commit_queue *commits)
943 while (!TAILQ_EMPTY(&commits->head))
944 pop_commit(commits);
947 static const struct got_error *
948 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
949 int minqueue, struct got_repository *repo, const char *path)
951 const struct got_error *err = NULL;
952 int nqueued = 0;
954 /*
955 * We keep all commits open throughout the lifetime of the log
956 * view in order to avoid having to re-fetch commits from disk
957 * while updating the display.
958 */
959 while (nqueued < minqueue) {
960 struct got_object_id *id;
961 struct got_commit_object *commit;
962 struct commit_queue_entry *entry;
963 int errcode;
965 err = got_commit_graph_iter_next(&id, graph);
966 if (err) {
967 if (err->code != GOT_ERR_ITER_NEED_MORE)
968 break;
969 err = got_commit_graph_fetch_commits(graph,
970 minqueue, repo);
971 if (err)
972 return err;
973 continue;
976 if (id == NULL)
977 break;
979 err = got_object_open_as_commit(&commit, repo, id);
980 if (err)
981 break;
982 entry = alloc_commit_queue_entry(commit, id);
983 if (entry == NULL) {
984 err = got_error_from_errno();
985 break;
988 errcode = pthread_mutex_lock(&tog_mutex);
989 if (errcode) {
990 err = got_error_set_errno(errcode);
991 break;
994 entry->idx = commits->ncommits;
995 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
996 nqueued++;
997 commits->ncommits++;
999 errcode = pthread_mutex_unlock(&tog_mutex);
1000 if (errcode && err == NULL)
1001 err = got_error_set_errno(errcode);
1004 return err;
1007 static const struct got_error *
1008 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1010 const struct got_error *err = NULL;
1011 struct got_reference *head_ref;
1013 *head_id = NULL;
1015 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1016 if (err)
1017 return err;
1019 err = got_ref_resolve(head_id, repo, head_ref);
1020 got_ref_close(head_ref);
1021 if (err) {
1022 *head_id = NULL;
1023 return err;
1026 return NULL;
1029 static const struct got_error *
1030 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1031 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1032 struct commit_queue *commits, int selected_idx, int limit,
1033 const char *path, int commits_needed)
1035 const struct got_error *err = NULL;
1036 struct commit_queue_entry *entry;
1037 int ncommits, width;
1038 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1039 wchar_t *wline;
1041 entry = first;
1042 ncommits = 0;
1043 while (entry) {
1044 if (ncommits == selected_idx) {
1045 *selected = entry;
1046 break;
1048 entry = TAILQ_NEXT(entry, entry);
1049 ncommits++;
1052 if (*selected) {
1053 err = got_object_id_str(&id_str, (*selected)->id);
1054 if (err)
1055 return err;
1058 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1059 entry ? entry->idx + 1 : 0, commits->ncommits,
1060 commits_needed == 0 ? "" : " loading...") == -1)
1061 return got_error_from_errno();
1063 if (path && strcmp(path, "/") != 0) {
1064 if (asprintf(&header, "commit: %s %s%s",
1065 id_str ? id_str : "........................................",
1066 path, ncommits_str) == -1) {
1067 err = got_error_from_errno();
1068 header = NULL;
1069 goto done;
1071 } else if (asprintf(&header, "commit: %s%s",
1072 id_str ? id_str : "........................................",
1073 ncommits_str) == -1) {
1074 err = got_error_from_errno();
1075 header = NULL;
1076 goto done;
1078 err = format_line(&wline, &width, header, view->ncols);
1079 if (err)
1080 goto done;
1082 werase(view->window);
1084 if (view_needs_focus_indication(view))
1085 wstandout(view->window);
1086 waddwstr(view->window, wline);
1087 while (width < view->ncols) {
1088 waddch(view->window, ' ');
1089 width++;
1091 if (view_needs_focus_indication(view))
1092 wstandend(view->window);
1093 free(wline);
1094 if (limit <= 1)
1095 goto done;
1097 entry = first;
1098 *last = first;
1099 ncommits = 0;
1100 while (entry) {
1101 if (ncommits >= limit - 1)
1102 break;
1103 if (view->focussed && ncommits == selected_idx)
1104 wstandout(view->window);
1105 err = draw_commit(view, entry->commit, entry->id);
1106 if (view->focussed && ncommits == selected_idx)
1107 wstandend(view->window);
1108 if (err)
1109 break;
1110 ncommits++;
1111 *last = entry;
1112 entry = TAILQ_NEXT(entry, entry);
1115 view_vborder(view);
1116 done:
1117 free(id_str);
1118 free(ncommits_str);
1119 free(header);
1120 return err;
1123 static void
1124 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1125 struct commit_queue *commits)
1127 struct commit_queue_entry *entry;
1128 int nscrolled = 0;
1130 entry = TAILQ_FIRST(&commits->head);
1131 if (*first_displayed_entry == entry)
1132 return;
1134 entry = *first_displayed_entry;
1135 while (entry && nscrolled < maxscroll) {
1136 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1137 if (entry) {
1138 *first_displayed_entry = entry;
1139 nscrolled++;
1144 static const struct got_error *
1145 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1146 struct commit_queue_entry **last_displayed_entry,
1147 struct commit_queue *commits, int *log_complete, int *commits_needed,
1148 pthread_cond_t *need_commits)
1150 const struct got_error *err = NULL;
1151 struct commit_queue_entry *pentry;
1152 int nscrolled = 0;
1154 if (*last_displayed_entry == NULL)
1155 return NULL;
1157 do {
1158 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1159 if (pentry == NULL) {
1160 int errcode;
1161 if (*log_complete)
1162 return NULL;
1163 *commits_needed = maxscroll + 20;
1164 errcode = pthread_cond_signal(need_commits);
1165 if (errcode)
1166 return got_error_set_errno(errcode);
1167 return NULL;
1169 *last_displayed_entry = pentry;
1171 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1172 if (pentry == NULL)
1173 break;
1174 *first_displayed_entry = pentry;
1175 } while (++nscrolled < maxscroll);
1177 return err;
1180 static const struct got_error *
1181 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1182 struct got_object_id *commit_id, struct got_commit_object *commit,
1183 struct got_repository *repo)
1185 const struct got_error *err;
1186 struct got_object *obj1 = NULL, *obj2 = NULL;
1187 struct got_object_qid *parent_id;
1188 struct tog_view *diff_view;
1190 err = got_object_open(&obj2, repo, commit_id);
1191 if (err)
1192 return err;
1194 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1195 if (parent_id) {
1196 err = got_object_open(&obj1, repo, parent_id->id);
1197 if (err)
1198 goto done;
1201 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1202 if (diff_view == NULL) {
1203 err = got_error_from_errno();
1204 goto done;
1207 err = open_diff_view(diff_view, obj1, obj2, repo);
1208 if (err == NULL)
1209 *new_view = diff_view;
1210 done:
1211 if (obj1)
1212 got_object_close(obj1);
1213 if (obj2)
1214 got_object_close(obj2);
1215 return err;
1218 static const struct got_error *
1219 browse_commit(struct tog_view **new_view, int begin_x,
1220 struct commit_queue_entry *entry, struct got_repository *repo)
1222 const struct got_error *err = NULL;
1223 struct got_tree_object *tree;
1224 struct tog_view *tree_view;
1226 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1227 if (err)
1228 return err;
1230 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1231 if (tree_view == NULL)
1232 return got_error_from_errno();
1234 err = open_tree_view(tree_view, tree, entry->id, repo);
1235 if (err)
1236 got_object_tree_close(tree);
1237 else
1238 *new_view = tree_view;
1239 return err;
1242 static void *
1243 log_thread(void *arg)
1245 const struct got_error *err = NULL;
1246 int errcode = 0;
1247 struct tog_log_thread_args *a = arg;
1248 int done = 0;
1250 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1251 if (err)
1252 return (void *)err;
1254 while (!done && !err) {
1255 err = queue_commits(a->graph, a->commits, 1, a->repo,
1256 a->in_repo_path);
1257 if (err) {
1258 if (err->code != GOT_ERR_ITER_COMPLETED)
1259 return (void *)err;
1260 err = NULL;
1261 done = 1;
1262 } else if (a->commits_needed > 0)
1263 a->commits_needed--;
1265 errcode = pthread_mutex_lock(&tog_mutex);
1266 if (errcode)
1267 return (void *)got_error_set_errno(errcode);
1269 if (done)
1270 a->log_complete = 1;
1271 else if (*a->quit) {
1272 done = 1;
1273 a->log_complete = 1;
1274 } else if (*a->first_displayed_entry == NULL) {
1275 *a->first_displayed_entry =
1276 TAILQ_FIRST(&a->commits->head);
1277 *a->selected_entry = *a->first_displayed_entry;
1280 err = draw_commits(a->view, a->last_displayed_entry,
1281 a->selected_entry, *a->first_displayed_entry,
1282 a->commits, *a->selected, a->view->nlines,
1283 a->in_repo_path, a->commits_needed);
1284 #if 0 /* XXX ncurses isn't thread-safe */
1285 if (!err) {
1286 update_panels();
1287 doupdate();
1289 #endif
1290 if (done)
1291 a->commits_needed = 0;
1292 else if (a->commits_needed == 0) {
1293 errcode = pthread_cond_wait(&a->need_commits,
1294 &tog_mutex);
1295 if (errcode)
1296 err = got_error_set_errno(errcode);
1299 errcode = pthread_mutex_unlock(&tog_mutex);
1300 if (errcode && err == NULL)
1301 err = got_error_set_errno(errcode);
1303 return (void *)err;
1306 static const struct got_error *
1307 stop_log_thread(struct tog_log_view_state *s)
1309 const struct got_error *err = NULL;
1310 int errcode;
1312 if (s->thread) {
1313 s->quit = 1;
1314 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1315 if (errcode)
1316 return got_error_set_errno(errcode);
1317 errcode = pthread_mutex_unlock(&tog_mutex);
1318 if (errcode)
1319 return got_error_set_errno(errcode);
1320 errcode = pthread_join(s->thread, (void **)&err);
1321 if (errcode)
1322 return got_error_set_errno(errcode);
1323 errcode = pthread_mutex_lock(&tog_mutex);
1324 if (errcode)
1325 return got_error_set_errno(errcode);
1326 s->thread = NULL;
1329 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1330 if (errcode && err == NULL)
1331 err = got_error_set_errno(errcode);
1333 if (s->thread_args.repo) {
1334 got_repo_close(s->thread_args.repo);
1335 s->thread_args.repo = NULL;
1338 if (s->thread_args.graph) {
1339 got_commit_graph_close(s->thread_args.graph);
1340 s->thread_args.graph = NULL;
1343 return err;
1346 static const struct got_error *
1347 close_log_view(struct tog_view *view)
1349 const struct got_error *err = NULL;
1350 struct tog_log_view_state *s = &view->state.log;
1352 err = stop_log_thread(s);
1353 free_commits(&s->commits);
1354 free(s->in_repo_path);
1355 free(s->start_id);
1356 return err;
1359 static const struct got_error *
1360 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1361 struct got_repository *repo, const char *path)
1363 const struct got_error *err = NULL;
1364 struct tog_log_view_state *s = &view->state.log;
1365 struct got_repository *thread_repo = NULL;
1366 struct got_commit_graph *thread_graph = NULL;
1367 int errcode;
1369 err = got_repo_map_path(&s->in_repo_path, repo, path);
1370 if (err != NULL)
1371 goto done;
1373 /* The commit queue only contains commits being displayed. */
1374 TAILQ_INIT(&s->commits.head);
1375 s->commits.ncommits = 0;
1377 s->repo = repo;
1378 s->start_id = got_object_id_dup(start_id);
1379 if (s->start_id == NULL) {
1380 err = got_error_from_errno();
1381 goto done;
1384 view->show = show_log_view;
1385 view->input = input_log_view;
1386 view->close = close_log_view;
1388 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1389 if (err)
1390 goto done;
1391 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1392 0, thread_repo);
1393 if (err)
1394 goto done;
1396 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1397 if (errcode) {
1398 err = got_error_set_errno(errcode);
1399 goto done;
1402 s->thread_args.commits_needed = view->nlines;
1403 s->thread_args.graph = thread_graph;
1404 s->thread_args.commits = &s->commits;
1405 s->thread_args.in_repo_path = s->in_repo_path;
1406 s->thread_args.start_id = s->start_id;
1407 s->thread_args.repo = thread_repo;
1408 s->thread_args.log_complete = 0;
1409 s->thread_args.quit = &s->quit;
1410 s->thread_args.view = view;
1411 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1412 s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1413 s->thread_args.selected_entry = &s->selected_entry;
1414 s->thread_args.selected = &s->selected;
1416 errcode = pthread_create(&s->thread, NULL, log_thread,
1417 &s->thread_args);
1418 if (errcode) {
1419 err = got_error_set_errno(errcode);
1420 goto done;
1423 done:
1424 if (err)
1425 close_log_view(view);
1426 return err;
1429 static const struct got_error *
1430 show_log_view(struct tog_view *view)
1432 struct tog_log_view_state *s = &view->state.log;
1434 return draw_commits(view, &s->last_displayed_entry,
1435 &s->selected_entry, s->first_displayed_entry,
1436 &s->commits, s->selected, view->nlines,
1437 s->in_repo_path, s->thread_args.commits_needed);
1440 static const struct got_error *
1441 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1442 struct tog_view **focus_view, struct tog_view *view, int ch)
1444 const struct got_error *err = NULL;
1445 struct tog_log_view_state *s = &view->state.log;
1446 char *parent_path;
1447 struct tog_view *diff_view = NULL, *tree_view = NULL;
1448 int begin_x = 0;
1450 switch (ch) {
1451 case 'q':
1452 s->quit = 1;
1453 break;
1454 case 'k':
1455 case KEY_UP:
1456 if (s->selected > 0)
1457 s->selected--;
1458 if (s->selected > 0)
1459 break;
1460 scroll_up(&s->first_displayed_entry, 1,
1461 &s->commits);
1462 break;
1463 case KEY_PPAGE:
1464 if (TAILQ_FIRST(&s->commits.head) ==
1465 s->first_displayed_entry) {
1466 s->selected = 0;
1467 break;
1469 scroll_up(&s->first_displayed_entry,
1470 view->nlines, &s->commits);
1471 break;
1472 case 'j':
1473 case KEY_DOWN:
1474 if (s->selected < MIN(view->nlines - 2,
1475 s->commits.ncommits - 1)) {
1476 s->selected++;
1477 break;
1479 err = scroll_down(&s->first_displayed_entry, 1,
1480 &s->last_displayed_entry, &s->commits,
1481 &s->thread_args.log_complete,
1482 &s->thread_args.commits_needed,
1483 &s->thread_args.need_commits);
1484 break;
1485 case KEY_NPAGE: {
1486 struct commit_queue_entry *first;
1487 first = s->first_displayed_entry;
1488 err = scroll_down(&s->first_displayed_entry,
1489 view->nlines, &s->last_displayed_entry,
1490 &s->commits, &s->thread_args.log_complete,
1491 &s->thread_args.commits_needed,
1492 &s->thread_args.need_commits);
1493 if (first == s->first_displayed_entry &&
1494 s->selected < MIN(view->nlines - 2,
1495 s->commits.ncommits - 1)) {
1496 /* can't scroll further down */
1497 s->selected = MIN(view->nlines - 2,
1498 s->commits.ncommits - 1);
1500 err = NULL;
1501 break;
1503 case KEY_RESIZE:
1504 if (s->selected > view->nlines - 2)
1505 s->selected = view->nlines - 2;
1506 if (s->selected > s->commits.ncommits - 1)
1507 s->selected = s->commits.ncommits - 1;
1508 break;
1509 case KEY_ENTER:
1510 case '\r':
1511 if (view_is_parent_view(view))
1512 begin_x = view_split_begin_x(view->begin_x);
1513 err = open_diff_view_for_commit(&diff_view, begin_x,
1514 s->selected_entry->id, s->selected_entry->commit,
1515 s->repo);
1516 if (err)
1517 break;
1518 if (view_is_parent_view(view)) {
1519 err = view_close_child(view);
1520 if (err)
1521 return err;
1522 err = view_set_child(view, diff_view);
1523 if (err) {
1524 view_close(diff_view);
1525 break;
1527 if (!view_is_splitscreen(diff_view)) {
1528 *focus_view = diff_view;
1529 view->child_focussed = 1;
1531 } else
1532 *new_view = diff_view;
1533 break;
1534 case 't':
1535 if (view_is_parent_view(view))
1536 begin_x = view_split_begin_x(view->begin_x);
1537 err = browse_commit(&tree_view, begin_x,
1538 s->selected_entry, s->repo);
1539 if (view_is_parent_view(view)) {
1540 err = view_close_child(view);
1541 if (err)
1542 return err;
1543 err = view_set_child(view, tree_view);
1544 if (err) {
1545 view_close(tree_view);
1546 break;
1548 *focus_view = tree_view;
1549 view->child_focussed = 1;
1550 } else
1551 *new_view = tree_view;
1552 break;
1553 case KEY_BACKSPACE:
1554 if (strcmp(s->in_repo_path, "/") == 0)
1555 break;
1556 parent_path = dirname(s->in_repo_path);
1557 if (parent_path && strcmp(parent_path, ".") != 0) {
1558 struct tog_view *lv;
1559 err = stop_log_thread(s);
1560 if (err)
1561 return err;
1562 lv = view_open(view->nlines, view->ncols,
1563 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1564 if (lv == NULL)
1565 return got_error_from_errno();
1566 err = open_log_view(lv, s->start_id, s->repo,
1567 parent_path);
1568 if (err)
1569 return err;;
1570 if (view_is_parent_view(view))
1571 *new_view = lv;
1572 else {
1573 view_set_child(view->parent, lv);
1574 *dead_view = view;
1576 return NULL;
1578 break;
1579 default:
1580 break;
1583 return err;
1586 static const struct got_error *
1587 cmd_log(int argc, char *argv[])
1589 const struct got_error *error;
1590 struct got_repository *repo = NULL;
1591 struct got_object_id *start_id = NULL;
1592 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1593 char *start_commit = NULL;
1594 int ch;
1595 struct tog_view *view;
1597 #ifndef PROFILE
1598 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1599 == -1)
1600 err(1, "pledge");
1601 #endif
1603 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1604 switch (ch) {
1605 case 'c':
1606 start_commit = optarg;
1607 break;
1608 case 'r':
1609 repo_path = realpath(optarg, NULL);
1610 if (repo_path == NULL)
1611 err(1, "-r option");
1612 break;
1613 default:
1614 usage();
1615 /* NOTREACHED */
1619 argc -= optind;
1620 argv += optind;
1622 if (argc == 0)
1623 path = strdup("");
1624 else if (argc == 1)
1625 path = strdup(argv[0]);
1626 else
1627 usage_log();
1628 if (path == NULL)
1629 return got_error_from_errno();
1631 cwd = getcwd(NULL, 0);
1632 if (cwd == NULL) {
1633 error = got_error_from_errno();
1634 goto done;
1636 if (repo_path == NULL) {
1637 repo_path = strdup(cwd);
1638 if (repo_path == NULL) {
1639 error = got_error_from_errno();
1640 goto done;
1644 error = got_repo_open(&repo, repo_path);
1645 if (error != NULL)
1646 goto done;
1648 if (start_commit == NULL) {
1649 error = get_head_commit_id(&start_id, repo);
1650 if (error != NULL)
1651 goto done;
1652 } else {
1653 struct got_object *obj;
1654 error = got_object_open_by_id_str(&obj, repo, start_commit);
1655 if (error == NULL) {
1656 start_id = got_object_id_dup(got_object_get_id(obj));
1657 if (start_id == NULL)
1658 error = got_error_from_errno();
1659 goto done;
1662 if (error != NULL)
1663 goto done;
1665 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1666 if (view == NULL) {
1667 error = got_error_from_errno();
1668 goto done;
1670 error = open_log_view(view, start_id, repo, path);
1671 if (error)
1672 goto done;
1673 error = view_loop(view);
1674 done:
1675 free(repo_path);
1676 free(cwd);
1677 free(path);
1678 free(start_id);
1679 if (repo)
1680 got_repo_close(repo);
1681 return error;
1684 __dead static void
1685 usage_diff(void)
1687 endwin();
1688 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1689 getprogname());
1690 exit(1);
1693 static char *
1694 parse_next_line(FILE *f, size_t *len)
1696 char *line;
1697 size_t linelen;
1698 size_t lineno;
1699 const char delim[3] = { '\0', '\0', '\0'};
1701 line = fparseln(f, &linelen, &lineno, delim, 0);
1702 if (len)
1703 *len = linelen;
1704 return line;
1707 static const struct got_error *
1708 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1709 int *last_displayed_line, int *eof, int max_lines,
1710 char * header)
1712 const struct got_error *err;
1713 int nlines = 0, nprinted = 0;
1714 char *line;
1715 size_t len;
1716 wchar_t *wline;
1717 int width;
1719 rewind(f);
1720 werase(view->window);
1722 if (header) {
1723 err = format_line(&wline, &width, header, view->ncols);
1724 if (err) {
1725 return err;
1728 if (view_needs_focus_indication(view))
1729 wstandout(view->window);
1730 waddwstr(view->window, wline);
1731 if (view_needs_focus_indication(view))
1732 wstandend(view->window);
1733 if (width < view->ncols)
1734 waddch(view->window, '\n');
1736 if (max_lines <= 1)
1737 return NULL;
1738 max_lines--;
1741 *eof = 0;
1742 while (nprinted < max_lines) {
1743 line = parse_next_line(f, &len);
1744 if (line == NULL) {
1745 *eof = 1;
1746 break;
1748 if (++nlines < *first_displayed_line) {
1749 free(line);
1750 continue;
1753 err = format_line(&wline, &width, line, view->ncols);
1754 if (err) {
1755 free(line);
1756 return err;
1758 waddwstr(view->window, wline);
1759 if (width < view->ncols)
1760 waddch(view->window, '\n');
1761 if (++nprinted == 1)
1762 *first_displayed_line = nlines;
1763 free(line);
1764 free(wline);
1765 wline = NULL;
1767 *last_displayed_line = nlines;
1769 view_vborder(view);
1771 return NULL;
1774 static const struct got_error *
1775 create_diff(struct tog_diff_view_state *s)
1777 const struct got_error *err = NULL;
1778 struct got_object *obj1 = NULL, *obj2 = NULL;
1779 FILE *f = NULL;
1781 if (s->id1) {
1782 err = got_object_open(&obj1, s->repo, s->id1);
1783 if (err)
1784 return err;
1787 err = got_object_open(&obj2, s->repo, s->id2);
1788 if (err)
1789 goto done;
1791 f = got_opentemp();
1792 if (f == NULL) {
1793 err = got_error_from_errno();
1794 goto done;
1796 if (s->f)
1797 fclose(s->f);
1798 s->f = f;
1800 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1801 case GOT_OBJ_TYPE_BLOB:
1802 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1803 s->diff_context, s->repo, f);
1804 break;
1805 case GOT_OBJ_TYPE_TREE:
1806 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1807 s->diff_context, s->repo, f);
1808 break;
1809 case GOT_OBJ_TYPE_COMMIT:
1810 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1811 s->repo, f);
1812 break;
1813 default:
1814 err = got_error(GOT_ERR_OBJ_TYPE);
1815 break;
1817 done:
1818 if (obj1)
1819 got_object_close(obj1);
1820 got_object_close(obj2);
1821 if (f)
1822 fflush(f);
1823 return err;
1826 static const struct got_error *
1827 open_diff_view(struct tog_view *view, struct got_object *obj1,
1828 struct got_object *obj2, struct got_repository *repo)
1830 const struct got_error *err;
1832 if (obj1 != NULL && obj2 != NULL &&
1833 got_object_get_type(obj1) != got_object_get_type(obj2))
1834 return got_error(GOT_ERR_OBJ_TYPE);
1836 if (obj1) {
1837 struct got_object_id *id1;
1838 id1 = got_object_id_dup(got_object_get_id(obj1));
1839 if (id1 == NULL)
1840 return got_error_from_errno();
1841 view->state.diff.id1 = id1;
1842 } else
1843 view->state.diff.id1 = NULL;
1845 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1846 if (view->state.diff.id2 == NULL) {
1847 free(view->state.diff.id1);
1848 view->state.diff.id1 = NULL;
1849 return got_error_from_errno();
1851 view->state.diff.f = NULL;
1852 view->state.diff.first_displayed_line = 1;
1853 view->state.diff.last_displayed_line = view->nlines;
1854 view->state.diff.diff_context = 3;
1855 view->state.diff.repo = repo;
1857 err = create_diff(&view->state.diff);
1858 if (err) {
1859 free(view->state.diff.id1);
1860 view->state.diff.id1 = NULL;
1861 free(view->state.diff.id2);
1862 view->state.diff.id2 = NULL;
1863 return err;
1866 view->show = show_diff_view;
1867 view->input = input_diff_view;
1868 view->close = close_diff_view;
1870 return NULL;
1873 static const struct got_error *
1874 close_diff_view(struct tog_view *view)
1876 const struct got_error *err = NULL;
1878 free(view->state.diff.id1);
1879 view->state.diff.id1 = NULL;
1880 free(view->state.diff.id2);
1881 view->state.diff.id2 = NULL;
1882 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1883 err = got_error_from_errno();
1884 return err;
1887 static const struct got_error *
1888 show_diff_view(struct tog_view *view)
1890 const struct got_error *err;
1891 struct tog_diff_view_state *s = &view->state.diff;
1892 char *id_str1 = NULL, *id_str2, *header;
1894 if (s->id1) {
1895 err = got_object_id_str(&id_str1, s->id1);
1896 if (err)
1897 return err;
1899 err = got_object_id_str(&id_str2, s->id2);
1900 if (err)
1901 return err;
1903 if (asprintf(&header, "diff: %s %s",
1904 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1905 err = got_error_from_errno();
1906 free(id_str1);
1907 free(id_str2);
1908 return err;
1910 free(id_str1);
1911 free(id_str2);
1913 return draw_file(view, s->f, &s->first_displayed_line,
1914 &s->last_displayed_line, &s->eof, view->nlines,
1915 header);
1918 static const struct got_error *
1919 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1920 struct tog_view **focus_view, struct tog_view *view, int ch)
1922 const struct got_error *err = NULL;
1923 struct tog_diff_view_state *s = &view->state.diff;
1924 int i;
1926 switch (ch) {
1927 case 'k':
1928 case KEY_UP:
1929 if (s->first_displayed_line > 1)
1930 s->first_displayed_line--;
1931 break;
1932 case KEY_PPAGE:
1933 i = 0;
1934 while (i++ < view->nlines - 1 &&
1935 s->first_displayed_line > 1)
1936 s->first_displayed_line--;
1937 break;
1938 case 'j':
1939 case KEY_DOWN:
1940 if (!s->eof)
1941 s->first_displayed_line++;
1942 break;
1943 case KEY_NPAGE:
1944 case ' ':
1945 i = 0;
1946 while (!s->eof && i++ < view->nlines - 1) {
1947 char *line;
1948 line = parse_next_line(s->f, NULL);
1949 s->first_displayed_line++;
1950 if (line == NULL)
1951 break;
1953 break;
1954 case '[':
1955 if (s->diff_context > 0) {
1956 s->diff_context--;
1957 err = create_diff(s);
1959 break;
1960 case ']':
1961 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1962 s->diff_context++;
1963 err = create_diff(s);
1965 break;
1966 default:
1967 break;
1970 return err;
1973 static const struct got_error *
1974 cmd_diff(int argc, char *argv[])
1976 const struct got_error *error = NULL;
1977 struct got_repository *repo = NULL;
1978 struct got_object *obj1 = NULL, *obj2 = NULL;
1979 char *repo_path = NULL;
1980 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1981 int ch;
1982 struct tog_view *view;
1984 #ifndef PROFILE
1985 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1986 == -1)
1987 err(1, "pledge");
1988 #endif
1990 while ((ch = getopt(argc, argv, "")) != -1) {
1991 switch (ch) {
1992 default:
1993 usage();
1994 /* NOTREACHED */
1998 argc -= optind;
1999 argv += optind;
2001 if (argc == 0) {
2002 usage_diff(); /* TODO show local worktree changes */
2003 } else if (argc == 2) {
2004 repo_path = getcwd(NULL, 0);
2005 if (repo_path == NULL)
2006 return got_error_from_errno();
2007 obj_id_str1 = argv[0];
2008 obj_id_str2 = argv[1];
2009 } else if (argc == 3) {
2010 repo_path = realpath(argv[0], NULL);
2011 if (repo_path == NULL)
2012 return got_error_from_errno();
2013 obj_id_str1 = argv[1];
2014 obj_id_str2 = argv[2];
2015 } else
2016 usage_diff();
2018 error = got_repo_open(&repo, repo_path);
2019 free(repo_path);
2020 if (error)
2021 goto done;
2023 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2024 if (error)
2025 goto done;
2027 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2028 if (error)
2029 goto done;
2031 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2032 if (view == NULL) {
2033 error = got_error_from_errno();
2034 goto done;
2036 error = open_diff_view(view, obj1, obj2, repo);
2037 if (error)
2038 goto done;
2039 error = view_loop(view);
2040 done:
2041 got_repo_close(repo);
2042 if (obj1)
2043 got_object_close(obj1);
2044 if (obj2)
2045 got_object_close(obj2);
2046 return error;
2049 __dead static void
2050 usage_blame(void)
2052 endwin();
2053 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2054 getprogname());
2055 exit(1);
2058 struct tog_blame_line {
2059 int annotated;
2060 struct got_object_id *id;
2063 static const struct got_error *
2064 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2065 const char *path, struct tog_blame_line *lines, int nlines,
2066 int blame_complete, int selected_line, int *first_displayed_line,
2067 int *last_displayed_line, int *eof, int max_lines)
2069 const struct got_error *err;
2070 int lineno = 0, nprinted = 0;
2071 char *line;
2072 size_t len;
2073 wchar_t *wline;
2074 int width, wlimit;
2075 struct tog_blame_line *blame_line;
2076 struct got_object_id *prev_id = NULL;
2077 char *id_str;
2079 err = got_object_id_str(&id_str, id);
2080 if (err)
2081 return err;
2083 rewind(f);
2084 werase(view->window);
2086 if (asprintf(&line, "commit: %s", id_str) == -1) {
2087 err = got_error_from_errno();
2088 free(id_str);
2089 return err;
2092 err = format_line(&wline, &width, line, view->ncols);
2093 free(line);
2094 line = NULL;
2095 if (view_needs_focus_indication(view))
2096 wstandout(view->window);
2097 waddwstr(view->window, wline);
2098 if (view_needs_focus_indication(view))
2099 wstandend(view->window);
2100 free(wline);
2101 wline = NULL;
2102 if (width < view->ncols)
2103 waddch(view->window, '\n');
2105 if (asprintf(&line, "[%d/%d] %s%s",
2106 *first_displayed_line - 1 + selected_line, nlines,
2107 blame_complete ? "" : "annotating ", path) == -1) {
2108 free(id_str);
2109 return got_error_from_errno();
2111 free(id_str);
2112 err = format_line(&wline, &width, line, view->ncols);
2113 free(line);
2114 line = NULL;
2115 if (err)
2116 return err;
2117 waddwstr(view->window, wline);
2118 free(wline);
2119 wline = NULL;
2120 if (width < view->ncols)
2121 waddch(view->window, '\n');
2123 *eof = 0;
2124 while (nprinted < max_lines - 2) {
2125 line = parse_next_line(f, &len);
2126 if (line == NULL) {
2127 *eof = 1;
2128 break;
2130 if (++lineno < *first_displayed_line) {
2131 free(line);
2132 continue;
2135 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2136 err = format_line(&wline, &width, line, wlimit);
2137 if (err) {
2138 free(line);
2139 return err;
2142 if (view->focussed && nprinted == selected_line - 1)
2143 wstandout(view->window);
2145 blame_line = &lines[lineno - 1];
2146 if (blame_line->annotated && prev_id &&
2147 got_object_id_cmp(prev_id, blame_line->id) == 0)
2148 waddstr(view->window, " ");
2149 else if (blame_line->annotated) {
2150 char *id_str;
2151 err = got_object_id_str(&id_str, blame_line->id);
2152 if (err) {
2153 free(line);
2154 free(wline);
2155 return err;
2157 wprintw(view->window, "%.8s ", id_str);
2158 free(id_str);
2159 prev_id = blame_line->id;
2160 } else {
2161 waddstr(view->window, "........ ");
2162 prev_id = NULL;
2165 waddwstr(view->window, wline);
2166 while (width < wlimit) {
2167 waddch(view->window, ' ');
2168 width++;
2170 if (view->focussed && nprinted == selected_line - 1)
2171 wstandend(view->window);
2172 if (++nprinted == 1)
2173 *first_displayed_line = lineno;
2174 free(line);
2175 free(wline);
2176 wline = NULL;
2178 *last_displayed_line = lineno;
2180 view_vborder(view);
2182 return NULL;
2185 static const struct got_error *
2186 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2188 const struct got_error *err = NULL;
2189 struct tog_blame_cb_args *a = arg;
2190 struct tog_blame_line *line;
2191 int errcode;
2193 if (nlines != a->nlines ||
2194 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2195 return got_error(GOT_ERR_RANGE);
2197 errcode = pthread_mutex_lock(&tog_mutex);
2198 if (errcode)
2199 return got_error_set_errno(errcode);
2201 if (*a->quit) { /* user has quit the blame view */
2202 err = got_error(GOT_ERR_ITER_COMPLETED);
2203 goto done;
2206 if (lineno == -1)
2207 goto done; /* no change in this commit */
2209 line = &a->lines[lineno - 1];
2210 if (line->annotated)
2211 goto done;
2213 line->id = got_object_id_dup(id);
2214 if (line->id == NULL) {
2215 err = got_error_from_errno();
2216 goto done;
2218 line->annotated = 1;
2220 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2221 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2222 a->last_displayed_line, a->eof, a->view->nlines);
2223 #if 0 /* XXX ncurses isn't thread-safe */
2224 if (!err) {
2225 update_panels();
2226 doupdate();
2228 #endif
2229 done:
2230 errcode = pthread_mutex_unlock(&tog_mutex);
2231 if (errcode)
2232 err = got_error_set_errno(errcode);
2233 return err;
2236 static void *
2237 blame_thread(void *arg)
2239 const struct got_error *err;
2240 struct tog_blame_thread_args *ta = arg;
2241 struct tog_blame_cb_args *a = ta->cb_args;
2242 int errcode;
2244 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2245 blame_cb, ta->cb_args);
2247 errcode = pthread_mutex_lock(&tog_mutex);
2248 if (errcode)
2249 return (void *)got_error_set_errno(errcode);
2251 got_repo_close(ta->repo);
2252 ta->repo = NULL;
2253 *ta->complete = 1;
2254 if (!err) {
2255 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2256 a->lines, a->nlines, 1, *a->selected_line,
2257 a->first_displayed_line, a->last_displayed_line, a->eof,
2258 a->view->nlines);
2259 #if 0 /* XXX ncurses isn't thread-safe */
2260 if (!err) {
2261 update_panels();
2262 doupdate();
2264 #endif
2267 errcode = pthread_mutex_unlock(&tog_mutex);
2268 if (errcode && err == NULL)
2269 err = got_error_set_errno(errcode);
2271 return (void *)err;
2274 static struct got_object_id *
2275 get_selected_commit_id(struct tog_blame_line *lines,
2276 int first_displayed_line, int selected_line)
2278 struct tog_blame_line *line;
2280 line = &lines[first_displayed_line - 1 + selected_line - 1];
2281 if (!line->annotated)
2282 return NULL;
2284 return line->id;
2287 static const struct got_error *
2288 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2289 struct tog_blame_line *lines, int first_displayed_line,
2290 int selected_line, struct got_repository *repo)
2292 const struct got_error *err = NULL;
2293 struct got_commit_object *commit = NULL;
2294 struct got_object_id *selected_id;
2295 struct got_object_qid *pid;
2297 *pobj = NULL;
2298 *obj = NULL;
2300 selected_id = get_selected_commit_id(lines,
2301 first_displayed_line, selected_line);
2302 if (selected_id == NULL)
2303 return NULL;
2305 err = got_object_open(obj, repo, selected_id);
2306 if (err)
2307 goto done;
2309 err = got_object_commit_open(&commit, repo, *obj);
2310 if (err)
2311 goto done;
2313 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2314 if (pid) {
2315 err = got_object_open(pobj, repo, pid->id);
2316 if (err)
2317 goto done;
2319 done:
2320 if (commit)
2321 got_object_commit_close(commit);
2322 return err;
2325 static const struct got_error *
2326 stop_blame(struct tog_blame *blame)
2328 const struct got_error *err = NULL;
2329 int i;
2331 if (blame->thread) {
2332 int errcode;
2333 errcode = pthread_mutex_unlock(&tog_mutex);
2334 if (errcode)
2335 return got_error_set_errno(errcode);
2336 errcode = pthread_join(blame->thread, (void **)&err);
2337 if (errcode)
2338 return got_error_set_errno(errcode);
2339 errcode = pthread_mutex_lock(&tog_mutex);
2340 if (errcode)
2341 return got_error_set_errno(errcode);
2342 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2343 err = NULL;
2344 blame->thread = NULL;
2346 if (blame->thread_args.repo) {
2347 got_repo_close(blame->thread_args.repo);
2348 blame->thread_args.repo = NULL;
2350 if (blame->f) {
2351 fclose(blame->f);
2352 blame->f = NULL;
2354 for (i = 0; i < blame->nlines; i++)
2355 free(blame->lines[i].id);
2356 free(blame->lines);
2357 blame->lines = NULL;
2358 free(blame->cb_args.commit_id);
2359 blame->cb_args.commit_id = NULL;
2361 return err;
2364 static const struct got_error *
2365 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2366 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2367 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2368 struct got_repository *repo)
2370 const struct got_error *err = NULL;
2371 struct got_blob_object *blob = NULL;
2372 struct got_repository *thread_repo = NULL;
2373 struct got_object_id *obj_id = NULL;
2374 struct got_object *obj = NULL;
2375 int errcode;
2377 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2378 if (err)
2379 goto done;
2381 err = got_object_open(&obj, repo, obj_id);
2382 if (err)
2383 goto done;
2385 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2386 err = got_error(GOT_ERR_OBJ_TYPE);
2387 goto done;
2390 err = got_object_blob_open(&blob, repo, obj, 8192);
2391 if (err)
2392 goto done;
2393 blame->f = got_opentemp();
2394 if (blame->f == NULL) {
2395 err = got_error_from_errno();
2396 goto done;
2398 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2399 blame->f, blob);
2400 if (err)
2401 goto done;
2403 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2404 if (blame->lines == NULL) {
2405 err = got_error_from_errno();
2406 goto done;
2409 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2410 if (err)
2411 goto done;
2413 blame->cb_args.view = view;
2414 blame->cb_args.lines = blame->lines;
2415 blame->cb_args.nlines = blame->nlines;
2416 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2417 if (blame->cb_args.commit_id == NULL) {
2418 err = got_error_from_errno();
2419 goto done;
2421 blame->cb_args.f = blame->f;
2422 blame->cb_args.path = path;
2423 blame->cb_args.first_displayed_line = first_displayed_line;
2424 blame->cb_args.selected_line = selected_line;
2425 blame->cb_args.last_displayed_line = last_displayed_line;
2426 blame->cb_args.quit = done;
2427 blame->cb_args.eof = eof;
2429 blame->thread_args.path = path;
2430 blame->thread_args.repo = thread_repo;
2431 blame->thread_args.cb_args = &blame->cb_args;
2432 blame->thread_args.complete = blame_complete;
2433 *blame_complete = 0;
2435 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2436 &blame->thread_args);
2437 if (errcode) {
2438 err = got_error_set_errno(errcode);
2439 goto done;
2442 done:
2443 if (blob)
2444 got_object_blob_close(blob);
2445 free(obj_id);
2446 if (obj)
2447 got_object_close(obj);
2448 if (err)
2449 stop_blame(blame);
2450 return err;
2453 static const struct got_error *
2454 open_blame_view(struct tog_view *view, char *path,
2455 struct got_object_id *commit_id, struct got_repository *repo)
2457 const struct got_error *err = NULL;
2458 struct tog_blame_view_state *s = &view->state.blame;
2460 SIMPLEQ_INIT(&s->blamed_commits);
2462 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2463 if (err)
2464 return err;
2466 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2467 s->first_displayed_line = 1;
2468 s->last_displayed_line = view->nlines;
2469 s->selected_line = 1;
2470 s->blame_complete = 0;
2471 s->path = path;
2472 if (s->path == NULL)
2473 return got_error_from_errno();
2474 s->repo = repo;
2475 s->commit_id = commit_id;
2476 memset(&s->blame, 0, sizeof(s->blame));
2478 view->show = show_blame_view;
2479 view->input = input_blame_view;
2480 view->close = close_blame_view;
2482 return run_blame(&s->blame, view, &s->blame_complete,
2483 &s->first_displayed_line, &s->last_displayed_line,
2484 &s->selected_line, &s->done, &s->eof, s->path,
2485 s->blamed_commit->id, s->repo);
2488 static const struct got_error *
2489 close_blame_view(struct tog_view *view)
2491 const struct got_error *err = NULL;
2492 struct tog_blame_view_state *s = &view->state.blame;
2494 if (s->blame.thread)
2495 err = stop_blame(&s->blame);
2497 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2498 struct got_object_qid *blamed_commit;
2499 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2500 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2501 got_object_qid_free(blamed_commit);
2504 free(s->path);
2506 return err;
2509 static const struct got_error *
2510 show_blame_view(struct tog_view *view)
2512 const struct got_error *err = NULL;
2513 struct tog_blame_view_state *s = &view->state.blame;
2515 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2516 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2517 s->selected_line, &s->first_displayed_line,
2518 &s->last_displayed_line, &s->eof, view->nlines);
2520 view_vborder(view);
2521 return err;
2524 static const struct got_error *
2525 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2526 struct tog_view **focus_view, struct tog_view *view, int ch)
2528 const struct got_error *err = NULL, *thread_err = NULL;
2529 struct got_object *obj = NULL, *pobj = NULL;
2530 struct tog_view *diff_view;
2531 struct tog_blame_view_state *s = &view->state.blame;
2532 int begin_x = 0;
2534 switch (ch) {
2535 case 'q':
2536 s->done = 1;
2537 break;
2538 case 'k':
2539 case KEY_UP:
2540 if (s->selected_line > 1)
2541 s->selected_line--;
2542 else if (s->selected_line == 1 &&
2543 s->first_displayed_line > 1)
2544 s->first_displayed_line--;
2545 break;
2546 case KEY_PPAGE:
2547 if (s->first_displayed_line == 1) {
2548 s->selected_line = 1;
2549 break;
2551 if (s->first_displayed_line > view->nlines - 2)
2552 s->first_displayed_line -=
2553 (view->nlines - 2);
2554 else
2555 s->first_displayed_line = 1;
2556 break;
2557 case 'j':
2558 case KEY_DOWN:
2559 if (s->selected_line < view->nlines - 2 &&
2560 s->first_displayed_line +
2561 s->selected_line <= s->blame.nlines)
2562 s->selected_line++;
2563 else if (s->last_displayed_line <
2564 s->blame.nlines)
2565 s->first_displayed_line++;
2566 break;
2567 case 'b':
2568 case 'p': {
2569 struct got_object_id *id;
2570 id = get_selected_commit_id(s->blame.lines,
2571 s->first_displayed_line, s->selected_line);
2572 if (id == NULL || got_object_id_cmp(id,
2573 s->blamed_commit->id) == 0)
2574 break;
2575 err = open_selected_commit(&pobj, &obj,
2576 s->blame.lines, s->first_displayed_line,
2577 s->selected_line, s->repo);
2578 if (err)
2579 break;
2580 if (pobj == NULL && obj == NULL)
2581 break;
2582 if (ch == 'p' && pobj == NULL)
2583 break;
2584 s->done = 1;
2585 thread_err = stop_blame(&s->blame);
2586 s->done = 0;
2587 if (thread_err)
2588 break;
2589 id = got_object_get_id(ch == 'b' ? obj : pobj);
2590 got_object_close(obj);
2591 obj = NULL;
2592 if (pobj) {
2593 got_object_close(pobj);
2594 pobj = NULL;
2596 err = got_object_qid_alloc(&s->blamed_commit, id);
2597 if (err)
2598 goto done;
2599 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2600 s->blamed_commit, entry);
2601 err = run_blame(&s->blame, view, &s->blame_complete,
2602 &s->first_displayed_line, &s->last_displayed_line,
2603 &s->selected_line, &s->done, &s->eof,
2604 s->path, s->blamed_commit->id, s->repo);
2605 if (err)
2606 break;
2607 break;
2609 case 'B': {
2610 struct got_object_qid *first;
2611 first = SIMPLEQ_FIRST(&s->blamed_commits);
2612 if (!got_object_id_cmp(first->id, s->commit_id))
2613 break;
2614 s->done = 1;
2615 thread_err = stop_blame(&s->blame);
2616 s->done = 0;
2617 if (thread_err)
2618 break;
2619 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2620 got_object_qid_free(s->blamed_commit);
2621 s->blamed_commit =
2622 SIMPLEQ_FIRST(&s->blamed_commits);
2623 err = run_blame(&s->blame, view, &s->blame_complete,
2624 &s->first_displayed_line, &s->last_displayed_line,
2625 &s->selected_line, &s->done, &s->eof, s->path,
2626 s->blamed_commit->id, s->repo);
2627 if (err)
2628 break;
2629 break;
2631 case KEY_ENTER:
2632 case '\r':
2633 err = open_selected_commit(&pobj, &obj,
2634 s->blame.lines, s->first_displayed_line,
2635 s->selected_line, s->repo);
2636 if (err)
2637 break;
2638 if (pobj == NULL && obj == NULL)
2639 break;
2641 if (view_is_parent_view(view))
2642 begin_x = view_split_begin_x(view->begin_x);
2643 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2644 if (diff_view == NULL) {
2645 err = got_error_from_errno();
2646 break;
2648 err = open_diff_view(diff_view, pobj, obj, s->repo);
2649 if (err) {
2650 view_close(diff_view);
2651 break;
2653 if (view_is_parent_view(view)) {
2654 err = view_close_child(view);
2655 if (err)
2656 return err;
2657 err = view_set_child(view, diff_view);
2658 if (err) {
2659 view_close(diff_view);
2660 break;
2662 if (!view_is_splitscreen(diff_view)) {
2663 *focus_view = diff_view;
2664 view->child_focussed = 1;
2666 } else
2667 *new_view = diff_view;
2668 if (pobj) {
2669 got_object_close(pobj);
2670 pobj = NULL;
2672 got_object_close(obj);
2673 obj = NULL;
2674 if (err)
2675 break;
2676 break;
2677 case KEY_NPAGE:
2678 case ' ':
2679 if (s->last_displayed_line >= s->blame.nlines &&
2680 s->selected_line < view->nlines - 2) {
2681 s->selected_line = MIN(s->blame.nlines,
2682 view->nlines - 2);
2683 break;
2685 if (s->last_displayed_line + view->nlines - 2
2686 <= s->blame.nlines)
2687 s->first_displayed_line +=
2688 view->nlines - 2;
2689 else
2690 s->first_displayed_line =
2691 s->blame.nlines -
2692 (view->nlines - 3);
2693 break;
2694 case KEY_RESIZE:
2695 if (s->selected_line > view->nlines - 2) {
2696 s->selected_line = MIN(s->blame.nlines,
2697 view->nlines - 2);
2699 break;
2700 default:
2701 break;
2703 done:
2704 if (pobj)
2705 got_object_close(pobj);
2706 return thread_err ? thread_err : err;
2709 static const struct got_error *
2710 cmd_blame(int argc, char *argv[])
2712 const struct got_error *error;
2713 struct got_repository *repo = NULL;
2714 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2715 struct got_object_id *commit_id = NULL;
2716 char *commit_id_str = NULL;
2717 int ch;
2718 struct tog_view *view;
2720 #ifndef PROFILE
2721 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2722 == -1)
2723 err(1, "pledge");
2724 #endif
2726 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2727 switch (ch) {
2728 case 'c':
2729 commit_id_str = optarg;
2730 break;
2731 case 'r':
2732 repo_path = realpath(optarg, NULL);
2733 if (repo_path == NULL)
2734 err(1, "-r option");
2735 break;
2736 default:
2737 usage();
2738 /* NOTREACHED */
2742 argc -= optind;
2743 argv += optind;
2745 if (argc == 1)
2746 path = argv[0];
2747 else
2748 usage_blame();
2750 cwd = getcwd(NULL, 0);
2751 if (cwd == NULL) {
2752 error = got_error_from_errno();
2753 goto done;
2755 if (repo_path == NULL) {
2756 repo_path = strdup(cwd);
2757 if (repo_path == NULL) {
2758 error = got_error_from_errno();
2759 goto done;
2764 error = got_repo_open(&repo, repo_path);
2765 if (error != NULL)
2766 return error;
2768 error = got_repo_map_path(&in_repo_path, repo, path);
2769 if (error != NULL)
2770 goto done;
2772 if (commit_id_str == NULL) {
2773 struct got_reference *head_ref;
2774 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2775 if (error != NULL)
2776 goto done;
2777 error = got_ref_resolve(&commit_id, repo, head_ref);
2778 got_ref_close(head_ref);
2779 } else {
2780 struct got_object *obj;
2781 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2782 if (error != NULL)
2783 goto done;
2784 commit_id = got_object_id_dup(got_object_get_id(obj));
2785 if (commit_id == NULL)
2786 error = got_error_from_errno();
2787 got_object_close(obj);
2789 if (error != NULL)
2790 goto done;
2792 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2793 if (view == NULL) {
2794 error = got_error_from_errno();
2795 goto done;
2797 error = open_blame_view(view, in_repo_path, commit_id, repo);
2798 if (error)
2799 goto done;
2800 error = view_loop(view);
2801 done:
2802 free(repo_path);
2803 free(cwd);
2804 free(commit_id);
2805 if (repo)
2806 got_repo_close(repo);
2807 return error;
2810 static const struct got_error *
2811 draw_tree_entries(struct tog_view *view,
2812 struct got_tree_entry **first_displayed_entry,
2813 struct got_tree_entry **last_displayed_entry,
2814 struct got_tree_entry **selected_entry, int *ndisplayed,
2815 const char *label, int show_ids, const char *parent_path,
2816 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2818 const struct got_error *err = NULL;
2819 struct got_tree_entry *te;
2820 wchar_t *wline;
2821 int width, n;
2823 *ndisplayed = 0;
2825 werase(view->window);
2827 if (limit == 0)
2828 return NULL;
2830 err = format_line(&wline, &width, label, view->ncols);
2831 if (err)
2832 return err;
2833 if (view_needs_focus_indication(view))
2834 wstandout(view->window);
2835 waddwstr(view->window, wline);
2836 if (view_needs_focus_indication(view))
2837 wstandend(view->window);
2838 free(wline);
2839 wline = NULL;
2840 if (width < view->ncols)
2841 waddch(view->window, '\n');
2842 if (--limit <= 0)
2843 return NULL;
2844 err = format_line(&wline, &width, parent_path, view->ncols);
2845 if (err)
2846 return err;
2847 waddwstr(view->window, wline);
2848 free(wline);
2849 wline = NULL;
2850 if (width < view->ncols)
2851 waddch(view->window, '\n');
2852 if (--limit <= 0)
2853 return NULL;
2854 waddch(view->window, '\n');
2855 if (--limit <= 0)
2856 return NULL;
2858 te = SIMPLEQ_FIRST(&entries->head);
2859 if (*first_displayed_entry == NULL) {
2860 if (selected == 0) {
2861 if (view->focussed)
2862 wstandout(view->window);
2863 *selected_entry = NULL;
2865 waddstr(view->window, " ..\n"); /* parent directory */
2866 if (selected == 0 && view->focussed)
2867 wstandend(view->window);
2868 (*ndisplayed)++;
2869 if (--limit <= 0)
2870 return NULL;
2871 n = 1;
2872 } else {
2873 n = 0;
2874 while (te != *first_displayed_entry)
2875 te = SIMPLEQ_NEXT(te, entry);
2878 while (te) {
2879 char *line = NULL, *id_str = NULL;
2881 if (show_ids) {
2882 err = got_object_id_str(&id_str, te->id);
2883 if (err)
2884 return got_error_from_errno();
2886 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2887 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2888 free(id_str);
2889 return got_error_from_errno();
2891 free(id_str);
2892 err = format_line(&wline, &width, line, view->ncols);
2893 if (err) {
2894 free(line);
2895 break;
2897 if (n == selected) {
2898 if (view->focussed)
2899 wstandout(view->window);
2900 *selected_entry = te;
2902 waddwstr(view->window, wline);
2903 if (width < view->ncols)
2904 waddch(view->window, '\n');
2905 if (n == selected && view->focussed)
2906 wstandend(view->window);
2907 free(line);
2908 free(wline);
2909 wline = NULL;
2910 n++;
2911 (*ndisplayed)++;
2912 *last_displayed_entry = te;
2913 if (--limit <= 0)
2914 break;
2915 te = SIMPLEQ_NEXT(te, entry);
2918 return err;
2921 static void
2922 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2923 const struct got_tree_entries *entries, int isroot)
2925 struct got_tree_entry *te, *prev;
2926 int i;
2928 if (*first_displayed_entry == NULL)
2929 return;
2931 te = SIMPLEQ_FIRST(&entries->head);
2932 if (*first_displayed_entry == te) {
2933 if (!isroot)
2934 *first_displayed_entry = NULL;
2935 return;
2938 /* XXX this is stupid... switch to TAILQ? */
2939 for (i = 0; i < maxscroll; i++) {
2940 while (te != *first_displayed_entry) {
2941 prev = te;
2942 te = SIMPLEQ_NEXT(te, entry);
2944 *first_displayed_entry = prev;
2945 te = SIMPLEQ_FIRST(&entries->head);
2947 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2948 *first_displayed_entry = NULL;
2951 static void
2952 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2953 struct got_tree_entry *last_displayed_entry,
2954 const struct got_tree_entries *entries)
2956 struct got_tree_entry *next;
2957 int n = 0;
2959 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2960 return;
2962 if (*first_displayed_entry)
2963 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2964 else
2965 next = SIMPLEQ_FIRST(&entries->head);
2966 while (next) {
2967 *first_displayed_entry = next;
2968 if (++n >= maxscroll)
2969 break;
2970 next = SIMPLEQ_NEXT(next, entry);
2974 static const struct got_error *
2975 tree_entry_path(char **path, struct tog_parent_trees *parents,
2976 struct got_tree_entry *te)
2978 const struct got_error *err = NULL;
2979 struct tog_parent_tree *pt;
2980 size_t len = 2; /* for leading slash and NUL */
2982 TAILQ_FOREACH(pt, parents, entry)
2983 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2984 if (te)
2985 len += strlen(te->name);
2987 *path = calloc(1, len);
2988 if (path == NULL)
2989 return got_error_from_errno();
2991 (*path)[0] = '/';
2992 pt = TAILQ_LAST(parents, tog_parent_trees);
2993 while (pt) {
2994 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2995 err = got_error(GOT_ERR_NO_SPACE);
2996 goto done;
2998 if (strlcat(*path, "/", len) >= len) {
2999 err = got_error(GOT_ERR_NO_SPACE);
3000 goto done;
3002 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3004 if (te) {
3005 if (strlcat(*path, te->name, len) >= len) {
3006 err = got_error(GOT_ERR_NO_SPACE);
3007 goto done;
3010 done:
3011 if (err) {
3012 free(*path);
3013 *path = NULL;
3015 return err;
3018 static const struct got_error *
3019 blame_tree_entry(struct tog_view **new_view, int begin_x,
3020 struct got_tree_entry *te, struct tog_parent_trees *parents,
3021 struct got_object_id *commit_id, struct got_repository *repo)
3023 const struct got_error *err = NULL;
3024 char *path;
3025 struct tog_view *blame_view;
3027 err = tree_entry_path(&path, parents, te);
3028 if (err)
3029 return err;
3031 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3032 if (blame_view == NULL)
3033 return got_error_from_errno();
3035 err = open_blame_view(blame_view, path, commit_id, repo);
3036 if (err) {
3037 view_close(blame_view);
3038 free(path);
3039 } else
3040 *new_view = blame_view;
3041 return err;
3044 static const struct got_error *
3045 log_tree_entry(struct tog_view **new_view, int begin_x,
3046 struct got_tree_entry *te, struct tog_parent_trees *parents,
3047 struct got_object_id *commit_id, struct got_repository *repo)
3049 struct tog_view *log_view;
3050 const struct got_error *err = NULL;
3051 char *path;
3053 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3054 if (log_view == NULL)
3055 return got_error_from_errno();
3057 err = tree_entry_path(&path, parents, te);
3058 if (err)
3059 return err;
3061 err = open_log_view(log_view, commit_id, repo, path);
3062 if (err)
3063 view_close(log_view);
3064 else
3065 *new_view = log_view;
3066 free(path);
3067 return err;
3070 static const struct got_error *
3071 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3072 struct got_object_id *commit_id, struct got_repository *repo)
3074 const struct got_error *err = NULL;
3075 char *commit_id_str = NULL;
3076 struct tog_tree_view_state *s = &view->state.tree;
3078 TAILQ_INIT(&s->parents);
3080 err = got_object_id_str(&commit_id_str, commit_id);
3081 if (err != NULL)
3082 goto done;
3084 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3085 err = got_error_from_errno();
3086 goto done;
3089 s->root = s->tree = root;
3090 s->entries = got_object_tree_get_entries(root);
3091 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3092 s->commit_id = got_object_id_dup(commit_id);
3093 if (s->commit_id == NULL) {
3094 err = got_error_from_errno();
3095 goto done;
3097 s->repo = repo;
3099 view->show = show_tree_view;
3100 view->input = input_tree_view;
3101 view->close = close_tree_view;
3102 done:
3103 free(commit_id_str);
3104 if (err) {
3105 free(s->tree_label);
3106 s->tree_label = NULL;
3108 return err;
3111 static const struct got_error *
3112 close_tree_view(struct tog_view *view)
3114 struct tog_tree_view_state *s = &view->state.tree;
3116 free(s->tree_label);
3117 s->tree_label = NULL;
3118 free(s->commit_id);
3119 s->commit_id = NULL;
3120 while (!TAILQ_EMPTY(&s->parents)) {
3121 struct tog_parent_tree *parent;
3122 parent = TAILQ_FIRST(&s->parents);
3123 TAILQ_REMOVE(&s->parents, parent, entry);
3124 free(parent);
3127 if (s->tree != s->root)
3128 got_object_tree_close(s->tree);
3129 got_object_tree_close(s->root);
3131 return NULL;
3134 static const struct got_error *
3135 show_tree_view(struct tog_view *view)
3137 const struct got_error *err = NULL;
3138 struct tog_tree_view_state *s = &view->state.tree;
3139 char *parent_path;
3141 err = tree_entry_path(&parent_path, &s->parents, NULL);
3142 if (err)
3143 return err;
3145 err = draw_tree_entries(view, &s->first_displayed_entry,
3146 &s->last_displayed_entry, &s->selected_entry,
3147 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3148 s->entries, s->selected, view->nlines, s->tree == s->root);
3149 free(parent_path);
3151 view_vborder(view);
3152 return err;
3155 static const struct got_error *
3156 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3157 struct tog_view **focus_view, struct tog_view *view, int ch)
3159 const struct got_error *err = NULL;
3160 struct tog_tree_view_state *s = &view->state.tree;
3161 struct tog_view *log_view;
3162 int begin_x = 0;
3164 switch (ch) {
3165 case 'i':
3166 s->show_ids = !s->show_ids;
3167 break;
3168 case 'l':
3169 if (!s->selected_entry)
3170 break;
3171 if (view_is_parent_view(view))
3172 begin_x = view_split_begin_x(view->begin_x);
3173 err = log_tree_entry(&log_view, begin_x,
3174 s->selected_entry, &s->parents,
3175 s->commit_id, s->repo);
3176 if (view_is_parent_view(view)) {
3177 err = view_close_child(view);
3178 if (err)
3179 return err;
3180 err = view_set_child(view, log_view);
3181 if (err) {
3182 view_close(log_view);
3183 break;
3185 *focus_view = log_view;
3186 view->child_focussed = 1;
3187 } else
3188 *new_view = log_view;
3189 break;
3190 case 'k':
3191 case KEY_UP:
3192 if (s->selected > 0)
3193 s->selected--;
3194 if (s->selected > 0)
3195 break;
3196 tree_scroll_up(&s->first_displayed_entry, 1,
3197 s->entries, s->tree == s->root);
3198 break;
3199 case KEY_PPAGE:
3200 if (SIMPLEQ_FIRST(&s->entries->head) ==
3201 s->first_displayed_entry) {
3202 if (s->tree != s->root)
3203 s->first_displayed_entry = NULL;
3204 s->selected = 0;
3205 break;
3207 tree_scroll_up(&s->first_displayed_entry,
3208 view->nlines, s->entries,
3209 s->tree == s->root);
3210 break;
3211 case 'j':
3212 case KEY_DOWN:
3213 if (s->selected < s->ndisplayed - 1) {
3214 s->selected++;
3215 break;
3217 tree_scroll_down(&s->first_displayed_entry, 1,
3218 s->last_displayed_entry, s->entries);
3219 break;
3220 case KEY_NPAGE:
3221 tree_scroll_down(&s->first_displayed_entry,
3222 view->nlines, s->last_displayed_entry,
3223 s->entries);
3224 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3225 entry))
3226 break;
3227 /* can't scroll any further; move cursor down */
3228 if (s->selected < s->ndisplayed - 1)
3229 s->selected = s->ndisplayed - 1;
3230 break;
3231 case KEY_ENTER:
3232 case '\r':
3233 if (s->selected_entry == NULL) {
3234 struct tog_parent_tree *parent;
3235 case KEY_BACKSPACE:
3236 /* user selected '..' */
3237 if (s->tree == s->root)
3238 break;
3239 parent = TAILQ_FIRST(&s->parents);
3240 TAILQ_REMOVE(&s->parents, parent,
3241 entry);
3242 got_object_tree_close(s->tree);
3243 s->tree = parent->tree;
3244 s->entries =
3245 got_object_tree_get_entries(s->tree);
3246 s->first_displayed_entry =
3247 parent->first_displayed_entry;
3248 s->selected_entry =
3249 parent->selected_entry;
3250 s->selected = parent->selected;
3251 free(parent);
3252 } else if (S_ISDIR(s->selected_entry->mode)) {
3253 struct tog_parent_tree *parent;
3254 struct got_tree_object *child;
3255 err = got_object_open_as_tree(&child,
3256 s->repo, s->selected_entry->id);
3257 if (err)
3258 break;
3259 parent = calloc(1, sizeof(*parent));
3260 if (parent == NULL) {
3261 err = got_error_from_errno();
3262 break;
3264 parent->tree = s->tree;
3265 parent->first_displayed_entry =
3266 s->first_displayed_entry;
3267 parent->selected_entry = s->selected_entry;
3268 parent->selected = s->selected;
3269 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3270 s->tree = child;
3271 s->entries =
3272 got_object_tree_get_entries(s->tree);
3273 s->selected = 0;
3274 s->first_displayed_entry = NULL;
3275 } else if (S_ISREG(s->selected_entry->mode)) {
3276 struct tog_view *blame_view;
3277 int begin_x = view_is_parent_view(view) ?
3278 view_split_begin_x(view->begin_x) : 0;
3280 err = blame_tree_entry(&blame_view, begin_x,
3281 s->selected_entry, &s->parents, s->commit_id,
3282 s->repo);
3283 if (err)
3284 break;
3285 if (view_is_parent_view(view)) {
3286 err = view_close_child(view);
3287 if (err)
3288 return err;
3289 err = view_set_child(view, blame_view);
3290 if (err) {
3291 view_close(blame_view);
3292 break;
3294 *focus_view = blame_view;
3295 view->child_focussed = 1;
3296 } else
3297 *new_view = blame_view;
3299 break;
3300 case KEY_RESIZE:
3301 if (s->selected > view->nlines)
3302 s->selected = s->ndisplayed - 1;
3303 break;
3304 default:
3305 break;
3308 return err;
3311 __dead static void
3312 usage_tree(void)
3314 endwin();
3315 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3316 getprogname());
3317 exit(1);
3320 static const struct got_error *
3321 cmd_tree(int argc, char *argv[])
3323 const struct got_error *error;
3324 struct got_repository *repo = NULL;
3325 char *repo_path = NULL;
3326 struct got_object_id *commit_id = NULL;
3327 char *commit_id_arg = NULL;
3328 struct got_commit_object *commit = NULL;
3329 struct got_tree_object *tree = NULL;
3330 int ch;
3331 struct tog_view *view;
3333 #ifndef PROFILE
3334 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3335 == -1)
3336 err(1, "pledge");
3337 #endif
3339 while ((ch = getopt(argc, argv, "c:")) != -1) {
3340 switch (ch) {
3341 case 'c':
3342 commit_id_arg = optarg;
3343 break;
3344 default:
3345 usage();
3346 /* NOTREACHED */
3350 argc -= optind;
3351 argv += optind;
3353 if (argc == 0) {
3354 repo_path = getcwd(NULL, 0);
3355 if (repo_path == NULL)
3356 return got_error_from_errno();
3357 } else if (argc == 1) {
3358 repo_path = realpath(argv[0], NULL);
3359 if (repo_path == NULL)
3360 return got_error_from_errno();
3361 } else
3362 usage_log();
3364 error = got_repo_open(&repo, repo_path);
3365 free(repo_path);
3366 if (error != NULL)
3367 return error;
3369 if (commit_id_arg == NULL) {
3370 error = get_head_commit_id(&commit_id, repo);
3371 if (error != NULL)
3372 goto done;
3373 } else {
3374 struct got_object *obj;
3375 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3376 if (error == NULL) {
3377 commit_id = got_object_id_dup(got_object_get_id(obj));
3378 if (commit_id == NULL)
3379 error = got_error_from_errno();
3382 if (error != NULL)
3383 goto done;
3385 error = got_object_open_as_commit(&commit, repo, commit_id);
3386 if (error != NULL)
3387 goto done;
3389 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3390 if (error != NULL)
3391 goto done;
3393 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3394 if (view == NULL) {
3395 error = got_error_from_errno();
3396 goto done;
3398 error = open_tree_view(view, tree, commit_id, repo);
3399 if (error)
3400 goto done;
3401 error = view_loop(view);
3402 done:
3403 free(commit_id);
3404 if (commit)
3405 got_object_commit_close(commit);
3406 if (tree)
3407 got_object_tree_close(tree);
3408 if (repo)
3409 got_repo_close(repo);
3410 return error;
3412 static void
3413 init_curses(void)
3415 initscr();
3416 cbreak();
3417 halfdelay(10);
3418 noecho();
3419 nonl();
3420 intrflush(stdscr, FALSE);
3421 keypad(stdscr, TRUE);
3422 curs_set(0);
3425 __dead static void
3426 usage(void)
3428 int i;
3430 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3431 "Available commands:\n", getprogname());
3432 for (i = 0; i < nitems(tog_commands); i++) {
3433 struct tog_cmd *cmd = &tog_commands[i];
3434 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3436 exit(1);
3439 static char **
3440 make_argv(const char *arg0, const char *arg1)
3442 char **argv;
3443 int argc = (arg1 == NULL ? 1 : 2);
3445 argv = calloc(argc, sizeof(char *));
3446 if (argv == NULL)
3447 err(1, "calloc");
3448 argv[0] = strdup(arg0);
3449 if (argv[0] == NULL)
3450 err(1, "calloc");
3451 if (arg1) {
3452 argv[1] = strdup(arg1);
3453 if (argv[1] == NULL)
3454 err(1, "calloc");
3457 return argv;
3460 int
3461 main(int argc, char *argv[])
3463 const struct got_error *error = NULL;
3464 struct tog_cmd *cmd = NULL;
3465 int ch, hflag = 0;
3466 char **cmd_argv = NULL;
3468 setlocale(LC_ALL, "");
3470 while ((ch = getopt(argc, argv, "h")) != -1) {
3471 switch (ch) {
3472 case 'h':
3473 hflag = 1;
3474 break;
3475 default:
3476 usage();
3477 /* NOTREACHED */
3481 argc -= optind;
3482 argv += optind;
3483 optind = 0;
3484 optreset = 1;
3486 if (argc == 0) {
3487 if (hflag)
3488 usage();
3489 /* Build an argument vector which runs a default command. */
3490 cmd = &tog_commands[0];
3491 cmd_argv = make_argv(cmd->name, NULL);
3492 argc = 1;
3493 } else {
3494 int i;
3496 /* Did the user specific a command? */
3497 for (i = 0; i < nitems(tog_commands); i++) {
3498 if (strncmp(tog_commands[i].name, argv[0],
3499 strlen(argv[0])) == 0) {
3500 cmd = &tog_commands[i];
3501 if (hflag)
3502 tog_commands[i].cmd_usage();
3503 break;
3506 if (cmd == NULL) {
3507 /* Did the user specify a repository? */
3508 char *repo_path = realpath(argv[0], NULL);
3509 if (repo_path) {
3510 struct got_repository *repo;
3511 error = got_repo_open(&repo, repo_path);
3512 if (error == NULL)
3513 got_repo_close(repo);
3514 } else
3515 error = got_error_from_errno();
3516 if (error) {
3517 if (hflag) {
3518 fprintf(stderr, "%s: '%s' is not a "
3519 "known command\n", getprogname(),
3520 argv[0]);
3521 usage();
3523 fprintf(stderr, "%s: '%s' is neither a known "
3524 "command nor a path to a repository\n",
3525 getprogname(), argv[0]);
3526 free(repo_path);
3527 return 1;
3529 cmd = &tog_commands[0];
3530 cmd_argv = make_argv(cmd->name, repo_path);
3531 argc = 2;
3532 free(repo_path);
3536 init_curses();
3538 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3539 if (error)
3540 goto done;
3541 done:
3542 endwin();
3543 free(cmd_argv);
3544 if (error)
3545 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3546 return 0;