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 fast_refresh = 10;
599 int done = 0, errcode;
601 errcode = pthread_mutex_lock(&tog_mutex);
602 if (errcode)
603 return got_error_set_errno(errcode);
605 TAILQ_INIT(&views);
606 TAILQ_INSERT_HEAD(&views, view, entry);
608 main_view = view;
609 view->focussed = 1;
610 err = view->show(view);
611 if (err)
612 return err;
613 update_panels();
614 doupdate();
615 while (!TAILQ_EMPTY(&views) && !done) {
616 /* Refresh fast during initialization, then become slower. */
617 if (fast_refresh && fast_refresh-- == 0)
618 halfdelay(10); /* switch to once per second */
620 err = view_input(&new_view, &dead_view, &focus_view, &done,
621 view, &views);
622 if (err)
623 break;
624 if (dead_view) {
625 struct tog_view *prev = NULL;
627 if (view_is_parent_view(dead_view))
628 prev = TAILQ_PREV(dead_view,
629 tog_view_list_head, entry);
630 else
631 prev = view->parent;
633 if (dead_view->parent)
634 dead_view->parent->child = NULL;
635 else
636 TAILQ_REMOVE(&views, dead_view, entry);
638 err = view_close(dead_view);
639 if (err || dead_view == main_view)
640 goto done;
642 if (view == dead_view) {
643 if (focus_view)
644 view = focus_view;
645 else if (prev)
646 view = prev;
647 else if (!TAILQ_EMPTY(&views))
648 view = TAILQ_LAST(&views,
649 tog_view_list_head);
650 else
651 view = NULL;
652 if (view) {
653 if (view->child && view->child_focussed)
654 focus_view = view->child;
655 else
656 focus_view = view;
660 if (new_view) {
661 struct tog_view *v, *t;
662 /* Only allow one parent view per type. */
663 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
664 if (v->type != new_view->type)
665 continue;
666 TAILQ_REMOVE(&views, v, entry);
667 err = view_close(v);
668 if (err)
669 goto done;
670 if (v == view)
671 view = new_view;
672 break;
674 TAILQ_INSERT_TAIL(&views, new_view, entry);
675 if (focus_view == NULL)
676 focus_view = new_view;
678 if (focus_view) {
679 show_panel(focus_view->panel);
680 if (view)
681 view->focussed = 0;
682 focus_view->focussed = 1;
683 view = focus_view;
684 if (new_view)
685 show_panel(new_view->panel);
686 if (view->child && view_is_splitscreen(view->child))
687 show_panel(view->child->panel);
689 if (view) {
690 if (focus_view == NULL) {
691 focus_view = view;
692 focus_view->focussed = 1;
694 if (view->parent) {
695 err = view->parent->show(view->parent);
696 if (err)
697 goto done;
699 err = view->show(view);
700 if (err)
701 goto done;
702 if (view->child) {
703 err = view->child->show(view->child);
704 if (err)
705 goto done;
707 update_panels();
708 doupdate();
711 done:
712 while (!TAILQ_EMPTY(&views)) {
713 view = TAILQ_FIRST(&views);
714 TAILQ_REMOVE(&views, view, entry);
715 view_close(view);
718 errcode = pthread_mutex_unlock(&tog_mutex);
719 if (errcode)
720 return got_error_set_errno(errcode);
722 return err;
725 __dead static void
726 usage_log(void)
728 endwin();
729 fprintf(stderr,
730 "usage: %s log [-c commit] [-r repository-path] [path]\n",
731 getprogname());
732 exit(1);
735 /* Create newly allocated wide-character string equivalent to a byte string. */
736 static const struct got_error *
737 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
739 char *vis = NULL;
740 const struct got_error *err = NULL;
742 *ws = NULL;
743 *wlen = mbstowcs(NULL, s, 0);
744 if (*wlen == (size_t)-1) {
745 int vislen;
746 if (errno != EILSEQ)
747 return got_error_from_errno();
749 /* byte string invalid in current encoding; try to "fix" it */
750 err = got_mbsavis(&vis, &vislen, s);
751 if (err)
752 return err;
753 *wlen = mbstowcs(NULL, vis, 0);
754 if (*wlen == (size_t)-1) {
755 err = got_error_from_errno(); /* give up */
756 goto done;
760 *ws = calloc(*wlen + 1, sizeof(*ws));
761 if (*ws == NULL) {
762 err = got_error_from_errno();
763 goto done;
766 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
767 err = got_error_from_errno();
768 done:
769 free(vis);
770 if (err) {
771 free(*ws);
772 *ws = NULL;
773 *wlen = 0;
775 return err;
778 /* Format a line for display, ensuring that it won't overflow a width limit. */
779 static const struct got_error *
780 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
782 const struct got_error *err = NULL;
783 int cols = 0;
784 wchar_t *wline = NULL;
785 size_t wlen;
786 int i;
788 *wlinep = NULL;
789 *widthp = 0;
791 err = mbs2ws(&wline, &wlen, line);
792 if (err)
793 return err;
795 i = 0;
796 while (i < wlen && cols < wlimit) {
797 int width = wcwidth(wline[i]);
798 switch (width) {
799 case 0:
800 i++;
801 break;
802 case 1:
803 case 2:
804 if (cols + width <= wlimit)
805 cols += width;
806 i++;
807 break;
808 case -1:
809 if (wline[i] == L'\t')
810 cols += TABSIZE - ((cols + 1) % TABSIZE);
811 i++;
812 break;
813 default:
814 err = got_error_from_errno();
815 goto done;
818 wline[i] = L'\0';
819 if (widthp)
820 *widthp = cols;
821 done:
822 if (err)
823 free(wline);
824 else
825 *wlinep = wline;
826 return err;
829 static const struct got_error *
830 draw_commit(struct tog_view *view, struct got_commit_object *commit,
831 struct got_object_id *id)
833 const struct got_error *err = NULL;
834 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
835 char *logmsg0 = NULL, *logmsg = NULL;
836 char *author0 = NULL, *author = NULL;
837 wchar_t *wlogmsg = NULL, *wauthor = NULL;
838 int author_width, logmsg_width;
839 char *newline, *smallerthan;
840 char *line = NULL;
841 int col, limit;
842 static const size_t date_display_cols = 9;
843 static const size_t author_display_cols = 16;
844 const int avail = view->ncols;
846 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
847 &commit->tm_committer) >= sizeof(datebuf))
848 return got_error(GOT_ERR_NO_SPACE);
850 if (avail < date_display_cols)
851 limit = MIN(sizeof(datebuf) - 1, avail);
852 else
853 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
854 waddnstr(view->window, datebuf, limit);
855 col = limit + 1;
856 if (col > avail)
857 goto done;
859 author0 = strdup(commit->author);
860 if (author0 == NULL) {
861 err = got_error_from_errno();
862 goto done;
864 author = author0;
865 smallerthan = strchr(author, '<');
866 if (smallerthan)
867 *smallerthan = '\0';
868 else {
869 char *at = strchr(author, '@');
870 if (at)
871 *at = '\0';
873 limit = avail - col;
874 err = format_line(&wauthor, &author_width, author, limit);
875 if (err)
876 goto done;
877 waddwstr(view->window, wauthor);
878 col += author_width;
879 while (col <= avail && author_width < author_display_cols + 1) {
880 waddch(view->window, ' ');
881 col++;
882 author_width++;
884 if (col > avail)
885 goto done;
887 logmsg0 = strdup(commit->logmsg);
888 if (logmsg0 == NULL) {
889 err = got_error_from_errno();
890 goto done;
892 logmsg = logmsg0;
893 while (*logmsg == '\n')
894 logmsg++;
895 newline = strchr(logmsg, '\n');
896 if (newline)
897 *newline = '\0';
898 limit = avail - col;
899 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
900 if (err)
901 goto done;
902 waddwstr(view->window, wlogmsg);
903 col += logmsg_width;
904 while (col <= avail) {
905 waddch(view->window, ' ');
906 col++;
908 done:
909 free(logmsg0);
910 free(wlogmsg);
911 free(author0);
912 free(wauthor);
913 free(line);
914 return err;
917 static struct commit_queue_entry *
918 alloc_commit_queue_entry(struct got_commit_object *commit,
919 struct got_object_id *id)
921 struct commit_queue_entry *entry;
923 entry = calloc(1, sizeof(*entry));
924 if (entry == NULL)
925 return NULL;
927 entry->id = id;
928 entry->commit = commit;
929 return entry;
932 static void
933 pop_commit(struct commit_queue *commits)
935 struct commit_queue_entry *entry;
937 entry = TAILQ_FIRST(&commits->head);
938 TAILQ_REMOVE(&commits->head, entry, entry);
939 got_object_commit_close(entry->commit);
940 commits->ncommits--;
941 /* Don't free entry->id! It is owned by the commit graph. */
942 free(entry);
945 static void
946 free_commits(struct commit_queue *commits)
948 while (!TAILQ_EMPTY(&commits->head))
949 pop_commit(commits);
952 static const struct got_error *
953 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
954 int minqueue, struct got_repository *repo, const char *path)
956 const struct got_error *err = NULL;
957 int nqueued = 0;
959 /*
960 * We keep all commits open throughout the lifetime of the log
961 * view in order to avoid having to re-fetch commits from disk
962 * while updating the display.
963 */
964 while (nqueued < minqueue) {
965 struct got_object_id *id;
966 struct got_commit_object *commit;
967 struct commit_queue_entry *entry;
968 int errcode;
970 err = got_commit_graph_iter_next(&id, graph);
971 if (err) {
972 if (err->code != GOT_ERR_ITER_NEED_MORE)
973 break;
974 err = got_commit_graph_fetch_commits(graph,
975 minqueue, repo);
976 if (err)
977 return err;
978 continue;
981 if (id == NULL)
982 break;
984 err = got_object_open_as_commit(&commit, repo, id);
985 if (err)
986 break;
987 entry = alloc_commit_queue_entry(commit, id);
988 if (entry == NULL) {
989 err = got_error_from_errno();
990 break;
993 errcode = pthread_mutex_lock(&tog_mutex);
994 if (errcode) {
995 err = got_error_set_errno(errcode);
996 break;
999 entry->idx = commits->ncommits;
1000 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1001 nqueued++;
1002 commits->ncommits++;
1004 errcode = pthread_mutex_unlock(&tog_mutex);
1005 if (errcode && err == NULL)
1006 err = got_error_set_errno(errcode);
1009 return err;
1012 static const struct got_error *
1013 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1015 const struct got_error *err = NULL;
1016 struct got_reference *head_ref;
1018 *head_id = NULL;
1020 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1021 if (err)
1022 return err;
1024 err = got_ref_resolve(head_id, repo, head_ref);
1025 got_ref_close(head_ref);
1026 if (err) {
1027 *head_id = NULL;
1028 return err;
1031 return NULL;
1034 static const struct got_error *
1035 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1036 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1037 struct commit_queue *commits, int selected_idx, int limit,
1038 const char *path, int commits_needed)
1040 const struct got_error *err = NULL;
1041 struct commit_queue_entry *entry;
1042 int ncommits, width;
1043 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1044 wchar_t *wline;
1046 entry = first;
1047 ncommits = 0;
1048 while (entry) {
1049 if (ncommits == selected_idx) {
1050 *selected = entry;
1051 break;
1053 entry = TAILQ_NEXT(entry, entry);
1054 ncommits++;
1057 if (*selected) {
1058 err = got_object_id_str(&id_str, (*selected)->id);
1059 if (err)
1060 return err;
1063 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1064 entry ? entry->idx + 1 : 0, commits->ncommits,
1065 commits_needed == 0 ? "" : " loading...") == -1)
1066 return got_error_from_errno();
1068 if (path && strcmp(path, "/") != 0) {
1069 if (asprintf(&header, "commit: %s %s%s",
1070 id_str ? id_str : "........................................",
1071 path, ncommits_str) == -1) {
1072 err = got_error_from_errno();
1073 header = NULL;
1074 goto done;
1076 } else if (asprintf(&header, "commit: %s%s",
1077 id_str ? id_str : "........................................",
1078 ncommits_str) == -1) {
1079 err = got_error_from_errno();
1080 header = NULL;
1081 goto done;
1083 err = format_line(&wline, &width, header, view->ncols);
1084 if (err)
1085 goto done;
1087 werase(view->window);
1089 if (view_needs_focus_indication(view))
1090 wstandout(view->window);
1091 waddwstr(view->window, wline);
1092 while (width < view->ncols) {
1093 waddch(view->window, ' ');
1094 width++;
1096 if (view_needs_focus_indication(view))
1097 wstandend(view->window);
1098 free(wline);
1099 if (limit <= 1)
1100 goto done;
1102 entry = first;
1103 *last = first;
1104 ncommits = 0;
1105 while (entry) {
1106 if (ncommits >= limit - 1)
1107 break;
1108 if (view->focussed && ncommits == selected_idx)
1109 wstandout(view->window);
1110 err = draw_commit(view, entry->commit, entry->id);
1111 if (view->focussed && ncommits == selected_idx)
1112 wstandend(view->window);
1113 if (err)
1114 break;
1115 ncommits++;
1116 *last = entry;
1117 entry = TAILQ_NEXT(entry, entry);
1120 view_vborder(view);
1121 done:
1122 free(id_str);
1123 free(ncommits_str);
1124 free(header);
1125 return err;
1128 static void
1129 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1130 struct commit_queue *commits)
1132 struct commit_queue_entry *entry;
1133 int nscrolled = 0;
1135 entry = TAILQ_FIRST(&commits->head);
1136 if (*first_displayed_entry == entry)
1137 return;
1139 entry = *first_displayed_entry;
1140 while (entry && nscrolled < maxscroll) {
1141 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1142 if (entry) {
1143 *first_displayed_entry = entry;
1144 nscrolled++;
1149 static const struct got_error *
1150 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1151 struct commit_queue_entry **last_displayed_entry,
1152 struct commit_queue *commits, int *log_complete, int *commits_needed,
1153 pthread_cond_t *need_commits)
1155 const struct got_error *err = NULL;
1156 struct commit_queue_entry *pentry;
1157 int nscrolled = 0;
1159 if (*last_displayed_entry == NULL)
1160 return NULL;
1162 do {
1163 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1164 if (pentry == NULL) {
1165 int errcode;
1166 if (*log_complete)
1167 return NULL;
1168 *commits_needed = maxscroll + 20;
1169 errcode = pthread_cond_signal(need_commits);
1170 if (errcode)
1171 return got_error_set_errno(errcode);
1172 return NULL;
1174 *last_displayed_entry = pentry;
1176 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1177 if (pentry == NULL)
1178 break;
1179 *first_displayed_entry = pentry;
1180 } while (++nscrolled < maxscroll);
1182 return err;
1185 static const struct got_error *
1186 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1187 struct got_object_id *commit_id, struct got_commit_object *commit,
1188 struct got_repository *repo)
1190 const struct got_error *err;
1191 struct got_object *obj1 = NULL, *obj2 = NULL;
1192 struct got_object_qid *parent_id;
1193 struct tog_view *diff_view;
1195 err = got_object_open(&obj2, repo, commit_id);
1196 if (err)
1197 return err;
1199 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1200 if (parent_id) {
1201 err = got_object_open(&obj1, repo, parent_id->id);
1202 if (err)
1203 goto done;
1206 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1207 if (diff_view == NULL) {
1208 err = got_error_from_errno();
1209 goto done;
1212 err = open_diff_view(diff_view, obj1, obj2, repo);
1213 if (err == NULL)
1214 *new_view = diff_view;
1215 done:
1216 if (obj1)
1217 got_object_close(obj1);
1218 if (obj2)
1219 got_object_close(obj2);
1220 return err;
1223 static const struct got_error *
1224 browse_commit(struct tog_view **new_view, int begin_x,
1225 struct commit_queue_entry *entry, struct got_repository *repo)
1227 const struct got_error *err = NULL;
1228 struct got_tree_object *tree;
1229 struct tog_view *tree_view;
1231 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1232 if (err)
1233 return err;
1235 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1236 if (tree_view == NULL)
1237 return got_error_from_errno();
1239 err = open_tree_view(tree_view, tree, entry->id, repo);
1240 if (err)
1241 got_object_tree_close(tree);
1242 else
1243 *new_view = tree_view;
1244 return err;
1247 static void *
1248 log_thread(void *arg)
1250 const struct got_error *err = NULL;
1251 int errcode = 0;
1252 struct tog_log_thread_args *a = arg;
1253 int done = 0;
1255 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1256 if (err)
1257 return (void *)err;
1259 while (!done && !err) {
1260 err = queue_commits(a->graph, a->commits, 1, a->repo,
1261 a->in_repo_path);
1262 if (err) {
1263 if (err->code != GOT_ERR_ITER_COMPLETED)
1264 return (void *)err;
1265 err = NULL;
1266 done = 1;
1267 } else if (a->commits_needed > 0)
1268 a->commits_needed--;
1270 errcode = pthread_mutex_lock(&tog_mutex);
1271 if (errcode)
1272 return (void *)got_error_set_errno(errcode);
1274 if (done)
1275 a->log_complete = 1;
1276 else if (*a->quit) {
1277 done = 1;
1278 a->log_complete = 1;
1279 } else if (*a->first_displayed_entry == NULL) {
1280 *a->first_displayed_entry =
1281 TAILQ_FIRST(&a->commits->head);
1282 *a->selected_entry = *a->first_displayed_entry;
1285 err = draw_commits(a->view, a->last_displayed_entry,
1286 a->selected_entry, *a->first_displayed_entry,
1287 a->commits, *a->selected, a->view->nlines,
1288 a->in_repo_path, a->commits_needed);
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 s->in_repo_path = NULL;
1356 free(s->start_id);
1357 s->start_id = NULL;
1358 return err;
1361 static const struct got_error *
1362 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1363 struct got_repository *repo, const char *path)
1365 const struct got_error *err = NULL;
1366 struct tog_log_view_state *s = &view->state.log;
1367 struct got_repository *thread_repo = NULL;
1368 struct got_commit_graph *thread_graph = NULL;
1369 int errcode;
1371 err = got_repo_map_path(&s->in_repo_path, repo, path);
1372 if (err != NULL)
1373 goto done;
1375 /* The commit queue only contains commits being displayed. */
1376 TAILQ_INIT(&s->commits.head);
1377 s->commits.ncommits = 0;
1379 s->repo = repo;
1380 s->start_id = got_object_id_dup(start_id);
1381 if (s->start_id == NULL) {
1382 err = got_error_from_errno();
1383 goto done;
1386 view->show = show_log_view;
1387 view->input = input_log_view;
1388 view->close = close_log_view;
1390 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1391 if (err)
1392 goto done;
1393 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1394 0, thread_repo);
1395 if (err)
1396 goto done;
1398 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1399 if (errcode) {
1400 err = got_error_set_errno(errcode);
1401 goto done;
1404 s->thread_args.commits_needed = view->nlines;
1405 s->thread_args.graph = thread_graph;
1406 s->thread_args.commits = &s->commits;
1407 s->thread_args.in_repo_path = s->in_repo_path;
1408 s->thread_args.start_id = s->start_id;
1409 s->thread_args.repo = thread_repo;
1410 s->thread_args.log_complete = 0;
1411 s->thread_args.quit = &s->quit;
1412 s->thread_args.view = view;
1413 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1414 s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1415 s->thread_args.selected_entry = &s->selected_entry;
1416 s->thread_args.selected = &s->selected;
1418 errcode = pthread_create(&s->thread, NULL, log_thread,
1419 &s->thread_args);
1420 if (errcode) {
1421 err = got_error_set_errno(errcode);
1422 goto done;
1425 done:
1426 if (err)
1427 close_log_view(view);
1428 return err;
1431 static const struct got_error *
1432 show_log_view(struct tog_view *view)
1434 struct tog_log_view_state *s = &view->state.log;
1436 return draw_commits(view, &s->last_displayed_entry,
1437 &s->selected_entry, s->first_displayed_entry,
1438 &s->commits, s->selected, view->nlines,
1439 s->in_repo_path, s->thread_args.commits_needed);
1442 static const struct got_error *
1443 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1444 struct tog_view **focus_view, struct tog_view *view, int ch)
1446 const struct got_error *err = NULL;
1447 struct tog_log_view_state *s = &view->state.log;
1448 char *parent_path;
1449 struct tog_view *diff_view = NULL, *tree_view = NULL;
1450 int begin_x = 0;
1452 switch (ch) {
1453 case 'q':
1454 s->quit = 1;
1455 break;
1456 case 'k':
1457 case KEY_UP:
1458 if (s->selected > 0)
1459 s->selected--;
1460 if (s->selected > 0)
1461 break;
1462 scroll_up(&s->first_displayed_entry, 1,
1463 &s->commits);
1464 break;
1465 case KEY_PPAGE:
1466 if (TAILQ_FIRST(&s->commits.head) ==
1467 s->first_displayed_entry) {
1468 s->selected = 0;
1469 break;
1471 scroll_up(&s->first_displayed_entry,
1472 view->nlines, &s->commits);
1473 break;
1474 case 'j':
1475 case KEY_DOWN:
1476 if (s->selected < MIN(view->nlines - 2,
1477 s->commits.ncommits - 1)) {
1478 s->selected++;
1479 break;
1481 err = scroll_down(&s->first_displayed_entry, 1,
1482 &s->last_displayed_entry, &s->commits,
1483 &s->thread_args.log_complete,
1484 &s->thread_args.commits_needed,
1485 &s->thread_args.need_commits);
1486 break;
1487 case KEY_NPAGE: {
1488 struct commit_queue_entry *first;
1489 first = s->first_displayed_entry;
1490 err = scroll_down(&s->first_displayed_entry,
1491 view->nlines, &s->last_displayed_entry,
1492 &s->commits, &s->thread_args.log_complete,
1493 &s->thread_args.commits_needed,
1494 &s->thread_args.need_commits);
1495 if (first == s->first_displayed_entry &&
1496 s->selected < MIN(view->nlines - 2,
1497 s->commits.ncommits - 1)) {
1498 /* can't scroll further down */
1499 s->selected = MIN(view->nlines - 2,
1500 s->commits.ncommits - 1);
1502 err = NULL;
1503 break;
1505 case KEY_RESIZE:
1506 if (s->selected > view->nlines - 2)
1507 s->selected = view->nlines - 2;
1508 if (s->selected > s->commits.ncommits - 1)
1509 s->selected = s->commits.ncommits - 1;
1510 break;
1511 case KEY_ENTER:
1512 case '\r':
1513 if (view_is_parent_view(view))
1514 begin_x = view_split_begin_x(view->begin_x);
1515 err = open_diff_view_for_commit(&diff_view, begin_x,
1516 s->selected_entry->id, s->selected_entry->commit,
1517 s->repo);
1518 if (err)
1519 break;
1520 if (view_is_parent_view(view)) {
1521 err = view_close_child(view);
1522 if (err)
1523 return err;
1524 err = view_set_child(view, diff_view);
1525 if (err) {
1526 view_close(diff_view);
1527 break;
1529 if (!view_is_splitscreen(diff_view)) {
1530 *focus_view = diff_view;
1531 view->child_focussed = 1;
1533 } else
1534 *new_view = diff_view;
1535 break;
1536 case 't':
1537 if (view_is_parent_view(view))
1538 begin_x = view_split_begin_x(view->begin_x);
1539 err = browse_commit(&tree_view, begin_x,
1540 s->selected_entry, s->repo);
1541 if (view_is_parent_view(view)) {
1542 err = view_close_child(view);
1543 if (err)
1544 return err;
1545 err = view_set_child(view, tree_view);
1546 if (err) {
1547 view_close(tree_view);
1548 break;
1550 *focus_view = tree_view;
1551 view->child_focussed = 1;
1552 } else
1553 *new_view = tree_view;
1554 break;
1555 case KEY_BACKSPACE:
1556 if (strcmp(s->in_repo_path, "/") == 0)
1557 break;
1558 parent_path = dirname(s->in_repo_path);
1559 if (parent_path && strcmp(parent_path, ".") != 0) {
1560 struct tog_view *lv;
1561 err = stop_log_thread(s);
1562 if (err)
1563 return err;
1564 lv = view_open(view->nlines, view->ncols,
1565 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1566 if (lv == NULL)
1567 return got_error_from_errno();
1568 err = open_log_view(lv, s->start_id, s->repo,
1569 parent_path);
1570 if (err)
1571 return err;;
1572 if (view_is_parent_view(view))
1573 *new_view = lv;
1574 else {
1575 view_set_child(view->parent, lv);
1576 *dead_view = view;
1578 return NULL;
1580 break;
1581 default:
1582 break;
1585 return err;
1588 static const struct got_error *
1589 cmd_log(int argc, char *argv[])
1591 const struct got_error *error;
1592 struct got_repository *repo = NULL;
1593 struct got_object_id *start_id = NULL;
1594 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1595 char *start_commit = NULL;
1596 int ch;
1597 struct tog_view *view;
1599 #ifndef PROFILE
1600 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1601 == -1)
1602 err(1, "pledge");
1603 #endif
1605 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1606 switch (ch) {
1607 case 'c':
1608 start_commit = optarg;
1609 break;
1610 case 'r':
1611 repo_path = realpath(optarg, NULL);
1612 if (repo_path == NULL)
1613 err(1, "-r option");
1614 break;
1615 default:
1616 usage();
1617 /* NOTREACHED */
1621 argc -= optind;
1622 argv += optind;
1624 if (argc == 0)
1625 path = strdup("");
1626 else if (argc == 1)
1627 path = strdup(argv[0]);
1628 else
1629 usage_log();
1630 if (path == NULL)
1631 return got_error_from_errno();
1633 cwd = getcwd(NULL, 0);
1634 if (cwd == NULL) {
1635 error = got_error_from_errno();
1636 goto done;
1638 if (repo_path == NULL) {
1639 repo_path = strdup(cwd);
1640 if (repo_path == NULL) {
1641 error = got_error_from_errno();
1642 goto done;
1646 error = got_repo_open(&repo, repo_path);
1647 if (error != NULL)
1648 goto done;
1650 if (start_commit == NULL) {
1651 error = get_head_commit_id(&start_id, repo);
1652 if (error != NULL)
1653 goto done;
1654 } else {
1655 struct got_object *obj;
1656 error = got_object_open_by_id_str(&obj, repo, start_commit);
1657 if (error == NULL) {
1658 start_id = got_object_id_dup(got_object_get_id(obj));
1659 if (start_id == NULL)
1660 error = got_error_from_errno();
1661 goto done;
1664 if (error != NULL)
1665 goto done;
1667 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1668 if (view == NULL) {
1669 error = got_error_from_errno();
1670 goto done;
1672 error = open_log_view(view, start_id, repo, path);
1673 if (error)
1674 goto done;
1675 error = view_loop(view);
1676 done:
1677 free(repo_path);
1678 free(cwd);
1679 free(path);
1680 free(start_id);
1681 if (repo)
1682 got_repo_close(repo);
1683 return error;
1686 __dead static void
1687 usage_diff(void)
1689 endwin();
1690 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1691 getprogname());
1692 exit(1);
1695 static char *
1696 parse_next_line(FILE *f, size_t *len)
1698 char *line;
1699 size_t linelen;
1700 size_t lineno;
1701 const char delim[3] = { '\0', '\0', '\0'};
1703 line = fparseln(f, &linelen, &lineno, delim, 0);
1704 if (len)
1705 *len = linelen;
1706 return line;
1709 static const struct got_error *
1710 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1711 int *last_displayed_line, int *eof, int max_lines,
1712 char * header)
1714 const struct got_error *err;
1715 int nlines = 0, nprinted = 0;
1716 char *line;
1717 size_t len;
1718 wchar_t *wline;
1719 int width;
1721 rewind(f);
1722 werase(view->window);
1724 if (header) {
1725 err = format_line(&wline, &width, header, view->ncols);
1726 if (err) {
1727 return err;
1730 if (view_needs_focus_indication(view))
1731 wstandout(view->window);
1732 waddwstr(view->window, wline);
1733 if (view_needs_focus_indication(view))
1734 wstandend(view->window);
1735 if (width < view->ncols)
1736 waddch(view->window, '\n');
1738 if (max_lines <= 1)
1739 return NULL;
1740 max_lines--;
1743 *eof = 0;
1744 while (nprinted < max_lines) {
1745 line = parse_next_line(f, &len);
1746 if (line == NULL) {
1747 *eof = 1;
1748 break;
1750 if (++nlines < *first_displayed_line) {
1751 free(line);
1752 continue;
1755 err = format_line(&wline, &width, line, view->ncols);
1756 if (err) {
1757 free(line);
1758 return err;
1760 waddwstr(view->window, wline);
1761 if (width < view->ncols)
1762 waddch(view->window, '\n');
1763 if (++nprinted == 1)
1764 *first_displayed_line = nlines;
1765 free(line);
1766 free(wline);
1767 wline = NULL;
1769 *last_displayed_line = nlines;
1771 view_vborder(view);
1773 return NULL;
1776 static const struct got_error *
1777 create_diff(struct tog_diff_view_state *s)
1779 const struct got_error *err = NULL;
1780 struct got_object *obj1 = NULL, *obj2 = NULL;
1781 FILE *f = NULL;
1783 if (s->id1) {
1784 err = got_object_open(&obj1, s->repo, s->id1);
1785 if (err)
1786 return err;
1789 err = got_object_open(&obj2, s->repo, s->id2);
1790 if (err)
1791 goto done;
1793 f = got_opentemp();
1794 if (f == NULL) {
1795 err = got_error_from_errno();
1796 goto done;
1798 if (s->f)
1799 fclose(s->f);
1800 s->f = f;
1802 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1803 case GOT_OBJ_TYPE_BLOB:
1804 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1805 s->diff_context, s->repo, f);
1806 break;
1807 case GOT_OBJ_TYPE_TREE:
1808 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1809 s->diff_context, s->repo, f);
1810 break;
1811 case GOT_OBJ_TYPE_COMMIT:
1812 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1813 s->repo, f);
1814 break;
1815 default:
1816 err = got_error(GOT_ERR_OBJ_TYPE);
1817 break;
1819 done:
1820 if (obj1)
1821 got_object_close(obj1);
1822 got_object_close(obj2);
1823 if (f)
1824 fflush(f);
1825 return err;
1828 static const struct got_error *
1829 open_diff_view(struct tog_view *view, struct got_object *obj1,
1830 struct got_object *obj2, struct got_repository *repo)
1832 const struct got_error *err;
1834 if (obj1 != NULL && obj2 != NULL &&
1835 got_object_get_type(obj1) != got_object_get_type(obj2))
1836 return got_error(GOT_ERR_OBJ_TYPE);
1838 if (obj1) {
1839 struct got_object_id *id1;
1840 id1 = got_object_id_dup(got_object_get_id(obj1));
1841 if (id1 == NULL)
1842 return got_error_from_errno();
1843 view->state.diff.id1 = id1;
1844 } else
1845 view->state.diff.id1 = NULL;
1847 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1848 if (view->state.diff.id2 == NULL) {
1849 free(view->state.diff.id1);
1850 view->state.diff.id1 = NULL;
1851 return got_error_from_errno();
1853 view->state.diff.f = NULL;
1854 view->state.diff.first_displayed_line = 1;
1855 view->state.diff.last_displayed_line = view->nlines;
1856 view->state.diff.diff_context = 3;
1857 view->state.diff.repo = repo;
1859 err = create_diff(&view->state.diff);
1860 if (err) {
1861 free(view->state.diff.id1);
1862 view->state.diff.id1 = NULL;
1863 free(view->state.diff.id2);
1864 view->state.diff.id2 = NULL;
1865 return err;
1868 view->show = show_diff_view;
1869 view->input = input_diff_view;
1870 view->close = close_diff_view;
1872 return NULL;
1875 static const struct got_error *
1876 close_diff_view(struct tog_view *view)
1878 const struct got_error *err = NULL;
1880 free(view->state.diff.id1);
1881 view->state.diff.id1 = NULL;
1882 free(view->state.diff.id2);
1883 view->state.diff.id2 = NULL;
1884 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1885 err = got_error_from_errno();
1886 return err;
1889 static const struct got_error *
1890 show_diff_view(struct tog_view *view)
1892 const struct got_error *err;
1893 struct tog_diff_view_state *s = &view->state.diff;
1894 char *id_str1 = NULL, *id_str2, *header;
1896 if (s->id1) {
1897 err = got_object_id_str(&id_str1, s->id1);
1898 if (err)
1899 return err;
1901 err = got_object_id_str(&id_str2, s->id2);
1902 if (err)
1903 return err;
1905 if (asprintf(&header, "diff: %s %s",
1906 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1907 err = got_error_from_errno();
1908 free(id_str1);
1909 free(id_str2);
1910 return err;
1912 free(id_str1);
1913 free(id_str2);
1915 return draw_file(view, s->f, &s->first_displayed_line,
1916 &s->last_displayed_line, &s->eof, view->nlines,
1917 header);
1920 static const struct got_error *
1921 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1922 struct tog_view **focus_view, struct tog_view *view, int ch)
1924 const struct got_error *err = NULL;
1925 struct tog_diff_view_state *s = &view->state.diff;
1926 int i;
1928 switch (ch) {
1929 case 'k':
1930 case KEY_UP:
1931 if (s->first_displayed_line > 1)
1932 s->first_displayed_line--;
1933 break;
1934 case KEY_PPAGE:
1935 i = 0;
1936 while (i++ < view->nlines - 1 &&
1937 s->first_displayed_line > 1)
1938 s->first_displayed_line--;
1939 break;
1940 case 'j':
1941 case KEY_DOWN:
1942 if (!s->eof)
1943 s->first_displayed_line++;
1944 break;
1945 case KEY_NPAGE:
1946 case ' ':
1947 i = 0;
1948 while (!s->eof && i++ < view->nlines - 1) {
1949 char *line;
1950 line = parse_next_line(s->f, NULL);
1951 s->first_displayed_line++;
1952 if (line == NULL)
1953 break;
1955 break;
1956 case '[':
1957 if (s->diff_context > 0) {
1958 s->diff_context--;
1959 err = create_diff(s);
1961 break;
1962 case ']':
1963 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1964 s->diff_context++;
1965 err = create_diff(s);
1967 break;
1968 default:
1969 break;
1972 return err;
1975 static const struct got_error *
1976 cmd_diff(int argc, char *argv[])
1978 const struct got_error *error = NULL;
1979 struct got_repository *repo = NULL;
1980 struct got_object *obj1 = NULL, *obj2 = NULL;
1981 char *repo_path = NULL;
1982 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1983 int ch;
1984 struct tog_view *view;
1986 #ifndef PROFILE
1987 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1988 == -1)
1989 err(1, "pledge");
1990 #endif
1992 while ((ch = getopt(argc, argv, "")) != -1) {
1993 switch (ch) {
1994 default:
1995 usage();
1996 /* NOTREACHED */
2000 argc -= optind;
2001 argv += optind;
2003 if (argc == 0) {
2004 usage_diff(); /* TODO show local worktree changes */
2005 } else if (argc == 2) {
2006 repo_path = getcwd(NULL, 0);
2007 if (repo_path == NULL)
2008 return got_error_from_errno();
2009 obj_id_str1 = argv[0];
2010 obj_id_str2 = argv[1];
2011 } else if (argc == 3) {
2012 repo_path = realpath(argv[0], NULL);
2013 if (repo_path == NULL)
2014 return got_error_from_errno();
2015 obj_id_str1 = argv[1];
2016 obj_id_str2 = argv[2];
2017 } else
2018 usage_diff();
2020 error = got_repo_open(&repo, repo_path);
2021 free(repo_path);
2022 if (error)
2023 goto done;
2025 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2026 if (error)
2027 goto done;
2029 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2030 if (error)
2031 goto done;
2033 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2034 if (view == NULL) {
2035 error = got_error_from_errno();
2036 goto done;
2038 error = open_diff_view(view, obj1, obj2, repo);
2039 if (error)
2040 goto done;
2041 error = view_loop(view);
2042 done:
2043 got_repo_close(repo);
2044 if (obj1)
2045 got_object_close(obj1);
2046 if (obj2)
2047 got_object_close(obj2);
2048 return error;
2051 __dead static void
2052 usage_blame(void)
2054 endwin();
2055 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2056 getprogname());
2057 exit(1);
2060 struct tog_blame_line {
2061 int annotated;
2062 struct got_object_id *id;
2065 static const struct got_error *
2066 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2067 const char *path, struct tog_blame_line *lines, int nlines,
2068 int blame_complete, int selected_line, int *first_displayed_line,
2069 int *last_displayed_line, int *eof, int max_lines)
2071 const struct got_error *err;
2072 int lineno = 0, nprinted = 0;
2073 char *line;
2074 size_t len;
2075 wchar_t *wline;
2076 int width, wlimit;
2077 struct tog_blame_line *blame_line;
2078 struct got_object_id *prev_id = NULL;
2079 char *id_str;
2081 err = got_object_id_str(&id_str, id);
2082 if (err)
2083 return err;
2085 rewind(f);
2086 werase(view->window);
2088 if (asprintf(&line, "commit: %s", id_str) == -1) {
2089 err = got_error_from_errno();
2090 free(id_str);
2091 return err;
2094 err = format_line(&wline, &width, line, view->ncols);
2095 free(line);
2096 line = NULL;
2097 if (view_needs_focus_indication(view))
2098 wstandout(view->window);
2099 waddwstr(view->window, wline);
2100 if (view_needs_focus_indication(view))
2101 wstandend(view->window);
2102 free(wline);
2103 wline = NULL;
2104 if (width < view->ncols)
2105 waddch(view->window, '\n');
2107 if (asprintf(&line, "[%d/%d] %s%s",
2108 *first_displayed_line - 1 + selected_line, nlines,
2109 blame_complete ? "" : "annotating ", path) == -1) {
2110 free(id_str);
2111 return got_error_from_errno();
2113 free(id_str);
2114 err = format_line(&wline, &width, line, view->ncols);
2115 free(line);
2116 line = NULL;
2117 if (err)
2118 return err;
2119 waddwstr(view->window, wline);
2120 free(wline);
2121 wline = NULL;
2122 if (width < view->ncols)
2123 waddch(view->window, '\n');
2125 *eof = 0;
2126 while (nprinted < max_lines - 2) {
2127 line = parse_next_line(f, &len);
2128 if (line == NULL) {
2129 *eof = 1;
2130 break;
2132 if (++lineno < *first_displayed_line) {
2133 free(line);
2134 continue;
2137 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2138 err = format_line(&wline, &width, line, wlimit);
2139 if (err) {
2140 free(line);
2141 return err;
2144 if (view->focussed && nprinted == selected_line - 1)
2145 wstandout(view->window);
2147 blame_line = &lines[lineno - 1];
2148 if (blame_line->annotated && prev_id &&
2149 got_object_id_cmp(prev_id, blame_line->id) == 0)
2150 waddstr(view->window, " ");
2151 else if (blame_line->annotated) {
2152 char *id_str;
2153 err = got_object_id_str(&id_str, blame_line->id);
2154 if (err) {
2155 free(line);
2156 free(wline);
2157 return err;
2159 wprintw(view->window, "%.8s ", id_str);
2160 free(id_str);
2161 prev_id = blame_line->id;
2162 } else {
2163 waddstr(view->window, "........ ");
2164 prev_id = NULL;
2167 waddwstr(view->window, wline);
2168 while (width < wlimit) {
2169 waddch(view->window, ' ');
2170 width++;
2172 if (view->focussed && nprinted == selected_line - 1)
2173 wstandend(view->window);
2174 if (++nprinted == 1)
2175 *first_displayed_line = lineno;
2176 free(line);
2177 free(wline);
2178 wline = NULL;
2180 *last_displayed_line = lineno;
2182 view_vborder(view);
2184 return NULL;
2187 static const struct got_error *
2188 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2190 const struct got_error *err = NULL;
2191 struct tog_blame_cb_args *a = arg;
2192 struct tog_blame_line *line;
2193 int errcode;
2195 if (nlines != a->nlines ||
2196 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2197 return got_error(GOT_ERR_RANGE);
2199 errcode = pthread_mutex_lock(&tog_mutex);
2200 if (errcode)
2201 return got_error_set_errno(errcode);
2203 if (*a->quit) { /* user has quit the blame view */
2204 err = got_error(GOT_ERR_ITER_COMPLETED);
2205 goto done;
2208 if (lineno == -1)
2209 goto done; /* no change in this commit */
2211 line = &a->lines[lineno - 1];
2212 if (line->annotated)
2213 goto done;
2215 line->id = got_object_id_dup(id);
2216 if (line->id == NULL) {
2217 err = got_error_from_errno();
2218 goto done;
2220 line->annotated = 1;
2222 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2223 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2224 a->last_displayed_line, a->eof, a->view->nlines);
2225 done:
2226 errcode = pthread_mutex_unlock(&tog_mutex);
2227 if (errcode)
2228 err = got_error_set_errno(errcode);
2229 return err;
2232 static void *
2233 blame_thread(void *arg)
2235 const struct got_error *err;
2236 struct tog_blame_thread_args *ta = arg;
2237 struct tog_blame_cb_args *a = ta->cb_args;
2238 int errcode;
2240 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2241 blame_cb, ta->cb_args);
2243 errcode = pthread_mutex_lock(&tog_mutex);
2244 if (errcode)
2245 return (void *)got_error_set_errno(errcode);
2247 got_repo_close(ta->repo);
2248 ta->repo = NULL;
2249 *ta->complete = 1;
2250 if (!err) {
2251 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2252 a->lines, a->nlines, 1, *a->selected_line,
2253 a->first_displayed_line, a->last_displayed_line, a->eof,
2254 a->view->nlines);
2257 errcode = pthread_mutex_unlock(&tog_mutex);
2258 if (errcode && err == NULL)
2259 err = got_error_set_errno(errcode);
2261 return (void *)err;
2264 static struct got_object_id *
2265 get_selected_commit_id(struct tog_blame_line *lines,
2266 int first_displayed_line, int selected_line)
2268 struct tog_blame_line *line;
2270 line = &lines[first_displayed_line - 1 + selected_line - 1];
2271 if (!line->annotated)
2272 return NULL;
2274 return line->id;
2277 static const struct got_error *
2278 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2279 struct tog_blame_line *lines, int first_displayed_line,
2280 int selected_line, struct got_repository *repo)
2282 const struct got_error *err = NULL;
2283 struct got_commit_object *commit = NULL;
2284 struct got_object_id *selected_id;
2285 struct got_object_qid *pid;
2287 *pobj = NULL;
2288 *obj = NULL;
2290 selected_id = get_selected_commit_id(lines,
2291 first_displayed_line, selected_line);
2292 if (selected_id == NULL)
2293 return NULL;
2295 err = got_object_open(obj, repo, selected_id);
2296 if (err)
2297 goto done;
2299 err = got_object_commit_open(&commit, repo, *obj);
2300 if (err)
2301 goto done;
2303 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2304 if (pid) {
2305 err = got_object_open(pobj, repo, pid->id);
2306 if (err)
2307 goto done;
2309 done:
2310 if (commit)
2311 got_object_commit_close(commit);
2312 return err;
2315 static const struct got_error *
2316 stop_blame(struct tog_blame *blame)
2318 const struct got_error *err = NULL;
2319 int i;
2321 if (blame->thread) {
2322 int errcode;
2323 errcode = pthread_mutex_unlock(&tog_mutex);
2324 if (errcode)
2325 return got_error_set_errno(errcode);
2326 errcode = pthread_join(blame->thread, (void **)&err);
2327 if (errcode)
2328 return got_error_set_errno(errcode);
2329 errcode = pthread_mutex_lock(&tog_mutex);
2330 if (errcode)
2331 return got_error_set_errno(errcode);
2332 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2333 err = NULL;
2334 blame->thread = NULL;
2336 if (blame->thread_args.repo) {
2337 got_repo_close(blame->thread_args.repo);
2338 blame->thread_args.repo = NULL;
2340 if (blame->f) {
2341 fclose(blame->f);
2342 blame->f = NULL;
2344 for (i = 0; i < blame->nlines; i++)
2345 free(blame->lines[i].id);
2346 free(blame->lines);
2347 blame->lines = NULL;
2348 free(blame->cb_args.commit_id);
2349 blame->cb_args.commit_id = NULL;
2351 return err;
2354 static const struct got_error *
2355 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2356 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2357 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2358 struct got_repository *repo)
2360 const struct got_error *err = NULL;
2361 struct got_blob_object *blob = NULL;
2362 struct got_repository *thread_repo = NULL;
2363 struct got_object_id *obj_id = NULL;
2364 struct got_object *obj = NULL;
2365 int errcode;
2367 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2368 if (err)
2369 goto done;
2371 err = got_object_open(&obj, repo, obj_id);
2372 if (err)
2373 goto done;
2375 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2376 err = got_error(GOT_ERR_OBJ_TYPE);
2377 goto done;
2380 err = got_object_blob_open(&blob, repo, obj, 8192);
2381 if (err)
2382 goto done;
2383 blame->f = got_opentemp();
2384 if (blame->f == NULL) {
2385 err = got_error_from_errno();
2386 goto done;
2388 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2389 blame->f, blob);
2390 if (err)
2391 goto done;
2393 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2394 if (blame->lines == NULL) {
2395 err = got_error_from_errno();
2396 goto done;
2399 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2400 if (err)
2401 goto done;
2403 blame->cb_args.view = view;
2404 blame->cb_args.lines = blame->lines;
2405 blame->cb_args.nlines = blame->nlines;
2406 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2407 if (blame->cb_args.commit_id == NULL) {
2408 err = got_error_from_errno();
2409 goto done;
2411 blame->cb_args.f = blame->f;
2412 blame->cb_args.path = path;
2413 blame->cb_args.first_displayed_line = first_displayed_line;
2414 blame->cb_args.selected_line = selected_line;
2415 blame->cb_args.last_displayed_line = last_displayed_line;
2416 blame->cb_args.quit = done;
2417 blame->cb_args.eof = eof;
2419 blame->thread_args.path = path;
2420 blame->thread_args.repo = thread_repo;
2421 blame->thread_args.cb_args = &blame->cb_args;
2422 blame->thread_args.complete = blame_complete;
2423 *blame_complete = 0;
2425 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2426 &blame->thread_args);
2427 if (errcode) {
2428 err = got_error_set_errno(errcode);
2429 goto done;
2432 done:
2433 if (blob)
2434 got_object_blob_close(blob);
2435 free(obj_id);
2436 if (obj)
2437 got_object_close(obj);
2438 if (err)
2439 stop_blame(blame);
2440 return err;
2443 static const struct got_error *
2444 open_blame_view(struct tog_view *view, char *path,
2445 struct got_object_id *commit_id, struct got_repository *repo)
2447 const struct got_error *err = NULL;
2448 struct tog_blame_view_state *s = &view->state.blame;
2450 SIMPLEQ_INIT(&s->blamed_commits);
2452 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2453 if (err)
2454 return err;
2456 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2457 s->first_displayed_line = 1;
2458 s->last_displayed_line = view->nlines;
2459 s->selected_line = 1;
2460 s->blame_complete = 0;
2461 s->path = path;
2462 if (s->path == NULL)
2463 return got_error_from_errno();
2464 s->repo = repo;
2465 s->commit_id = commit_id;
2466 memset(&s->blame, 0, sizeof(s->blame));
2468 view->show = show_blame_view;
2469 view->input = input_blame_view;
2470 view->close = close_blame_view;
2472 return run_blame(&s->blame, view, &s->blame_complete,
2473 &s->first_displayed_line, &s->last_displayed_line,
2474 &s->selected_line, &s->done, &s->eof, s->path,
2475 s->blamed_commit->id, s->repo);
2478 static const struct got_error *
2479 close_blame_view(struct tog_view *view)
2481 const struct got_error *err = NULL;
2482 struct tog_blame_view_state *s = &view->state.blame;
2484 if (s->blame.thread)
2485 err = stop_blame(&s->blame);
2487 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2488 struct got_object_qid *blamed_commit;
2489 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2490 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2491 got_object_qid_free(blamed_commit);
2494 free(s->path);
2496 return err;
2499 static const struct got_error *
2500 show_blame_view(struct tog_view *view)
2502 const struct got_error *err = NULL;
2503 struct tog_blame_view_state *s = &view->state.blame;
2505 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2506 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2507 s->selected_line, &s->first_displayed_line,
2508 &s->last_displayed_line, &s->eof, view->nlines);
2510 view_vborder(view);
2511 return err;
2514 static const struct got_error *
2515 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2516 struct tog_view **focus_view, struct tog_view *view, int ch)
2518 const struct got_error *err = NULL, *thread_err = NULL;
2519 struct got_object *obj = NULL, *pobj = NULL;
2520 struct tog_view *diff_view;
2521 struct tog_blame_view_state *s = &view->state.blame;
2522 int begin_x = 0;
2524 switch (ch) {
2525 case 'q':
2526 s->done = 1;
2527 break;
2528 case 'k':
2529 case KEY_UP:
2530 if (s->selected_line > 1)
2531 s->selected_line--;
2532 else if (s->selected_line == 1 &&
2533 s->first_displayed_line > 1)
2534 s->first_displayed_line--;
2535 break;
2536 case KEY_PPAGE:
2537 if (s->first_displayed_line == 1) {
2538 s->selected_line = 1;
2539 break;
2541 if (s->first_displayed_line > view->nlines - 2)
2542 s->first_displayed_line -=
2543 (view->nlines - 2);
2544 else
2545 s->first_displayed_line = 1;
2546 break;
2547 case 'j':
2548 case KEY_DOWN:
2549 if (s->selected_line < view->nlines - 2 &&
2550 s->first_displayed_line +
2551 s->selected_line <= s->blame.nlines)
2552 s->selected_line++;
2553 else if (s->last_displayed_line <
2554 s->blame.nlines)
2555 s->first_displayed_line++;
2556 break;
2557 case 'b':
2558 case 'p': {
2559 struct got_object_id *id;
2560 id = get_selected_commit_id(s->blame.lines,
2561 s->first_displayed_line, s->selected_line);
2562 if (id == NULL || got_object_id_cmp(id,
2563 s->blamed_commit->id) == 0)
2564 break;
2565 err = open_selected_commit(&pobj, &obj,
2566 s->blame.lines, s->first_displayed_line,
2567 s->selected_line, s->repo);
2568 if (err)
2569 break;
2570 if (pobj == NULL && obj == NULL)
2571 break;
2572 if (ch == 'p' && pobj == NULL)
2573 break;
2574 s->done = 1;
2575 thread_err = stop_blame(&s->blame);
2576 s->done = 0;
2577 if (thread_err)
2578 break;
2579 id = got_object_get_id(ch == 'b' ? obj : pobj);
2580 got_object_close(obj);
2581 obj = NULL;
2582 if (pobj) {
2583 got_object_close(pobj);
2584 pobj = NULL;
2586 err = got_object_qid_alloc(&s->blamed_commit, id);
2587 if (err)
2588 goto done;
2589 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2590 s->blamed_commit, entry);
2591 err = run_blame(&s->blame, view, &s->blame_complete,
2592 &s->first_displayed_line, &s->last_displayed_line,
2593 &s->selected_line, &s->done, &s->eof,
2594 s->path, s->blamed_commit->id, s->repo);
2595 if (err)
2596 break;
2597 break;
2599 case 'B': {
2600 struct got_object_qid *first;
2601 first = SIMPLEQ_FIRST(&s->blamed_commits);
2602 if (!got_object_id_cmp(first->id, s->commit_id))
2603 break;
2604 s->done = 1;
2605 thread_err = stop_blame(&s->blame);
2606 s->done = 0;
2607 if (thread_err)
2608 break;
2609 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2610 got_object_qid_free(s->blamed_commit);
2611 s->blamed_commit =
2612 SIMPLEQ_FIRST(&s->blamed_commits);
2613 err = run_blame(&s->blame, view, &s->blame_complete,
2614 &s->first_displayed_line, &s->last_displayed_line,
2615 &s->selected_line, &s->done, &s->eof, s->path,
2616 s->blamed_commit->id, s->repo);
2617 if (err)
2618 break;
2619 break;
2621 case KEY_ENTER:
2622 case '\r':
2623 err = open_selected_commit(&pobj, &obj,
2624 s->blame.lines, s->first_displayed_line,
2625 s->selected_line, s->repo);
2626 if (err)
2627 break;
2628 if (pobj == NULL && obj == NULL)
2629 break;
2631 if (view_is_parent_view(view))
2632 begin_x = view_split_begin_x(view->begin_x);
2633 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2634 if (diff_view == NULL) {
2635 err = got_error_from_errno();
2636 break;
2638 err = open_diff_view(diff_view, pobj, obj, s->repo);
2639 if (err) {
2640 view_close(diff_view);
2641 break;
2643 if (view_is_parent_view(view)) {
2644 err = view_close_child(view);
2645 if (err)
2646 return err;
2647 err = view_set_child(view, diff_view);
2648 if (err) {
2649 view_close(diff_view);
2650 break;
2652 if (!view_is_splitscreen(diff_view)) {
2653 *focus_view = diff_view;
2654 view->child_focussed = 1;
2656 } else
2657 *new_view = diff_view;
2658 if (pobj) {
2659 got_object_close(pobj);
2660 pobj = NULL;
2662 got_object_close(obj);
2663 obj = NULL;
2664 if (err)
2665 break;
2666 break;
2667 case KEY_NPAGE:
2668 case ' ':
2669 if (s->last_displayed_line >= s->blame.nlines &&
2670 s->selected_line < view->nlines - 2) {
2671 s->selected_line = MIN(s->blame.nlines,
2672 view->nlines - 2);
2673 break;
2675 if (s->last_displayed_line + view->nlines - 2
2676 <= s->blame.nlines)
2677 s->first_displayed_line +=
2678 view->nlines - 2;
2679 else
2680 s->first_displayed_line =
2681 s->blame.nlines -
2682 (view->nlines - 3);
2683 break;
2684 case KEY_RESIZE:
2685 if (s->selected_line > view->nlines - 2) {
2686 s->selected_line = MIN(s->blame.nlines,
2687 view->nlines - 2);
2689 break;
2690 default:
2691 break;
2693 done:
2694 if (pobj)
2695 got_object_close(pobj);
2696 return thread_err ? thread_err : err;
2699 static const struct got_error *
2700 cmd_blame(int argc, char *argv[])
2702 const struct got_error *error;
2703 struct got_repository *repo = NULL;
2704 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2705 struct got_object_id *commit_id = NULL;
2706 char *commit_id_str = NULL;
2707 int ch;
2708 struct tog_view *view;
2710 #ifndef PROFILE
2711 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2712 == -1)
2713 err(1, "pledge");
2714 #endif
2716 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2717 switch (ch) {
2718 case 'c':
2719 commit_id_str = optarg;
2720 break;
2721 case 'r':
2722 repo_path = realpath(optarg, NULL);
2723 if (repo_path == NULL)
2724 err(1, "-r option");
2725 break;
2726 default:
2727 usage();
2728 /* NOTREACHED */
2732 argc -= optind;
2733 argv += optind;
2735 if (argc == 1)
2736 path = argv[0];
2737 else
2738 usage_blame();
2740 cwd = getcwd(NULL, 0);
2741 if (cwd == NULL) {
2742 error = got_error_from_errno();
2743 goto done;
2745 if (repo_path == NULL) {
2746 repo_path = strdup(cwd);
2747 if (repo_path == NULL) {
2748 error = got_error_from_errno();
2749 goto done;
2754 error = got_repo_open(&repo, repo_path);
2755 if (error != NULL)
2756 return error;
2758 error = got_repo_map_path(&in_repo_path, repo, path);
2759 if (error != NULL)
2760 goto done;
2762 if (commit_id_str == NULL) {
2763 struct got_reference *head_ref;
2764 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2765 if (error != NULL)
2766 goto done;
2767 error = got_ref_resolve(&commit_id, repo, head_ref);
2768 got_ref_close(head_ref);
2769 } else {
2770 struct got_object *obj;
2771 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2772 if (error != NULL)
2773 goto done;
2774 commit_id = got_object_id_dup(got_object_get_id(obj));
2775 if (commit_id == NULL)
2776 error = got_error_from_errno();
2777 got_object_close(obj);
2779 if (error != NULL)
2780 goto done;
2782 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2783 if (view == NULL) {
2784 error = got_error_from_errno();
2785 goto done;
2787 error = open_blame_view(view, in_repo_path, commit_id, repo);
2788 if (error)
2789 goto done;
2790 error = view_loop(view);
2791 done:
2792 free(repo_path);
2793 free(cwd);
2794 free(commit_id);
2795 if (repo)
2796 got_repo_close(repo);
2797 return error;
2800 static const struct got_error *
2801 draw_tree_entries(struct tog_view *view,
2802 struct got_tree_entry **first_displayed_entry,
2803 struct got_tree_entry **last_displayed_entry,
2804 struct got_tree_entry **selected_entry, int *ndisplayed,
2805 const char *label, int show_ids, const char *parent_path,
2806 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2808 const struct got_error *err = NULL;
2809 struct got_tree_entry *te;
2810 wchar_t *wline;
2811 int width, n;
2813 *ndisplayed = 0;
2815 werase(view->window);
2817 if (limit == 0)
2818 return NULL;
2820 err = format_line(&wline, &width, label, view->ncols);
2821 if (err)
2822 return err;
2823 if (view_needs_focus_indication(view))
2824 wstandout(view->window);
2825 waddwstr(view->window, wline);
2826 if (view_needs_focus_indication(view))
2827 wstandend(view->window);
2828 free(wline);
2829 wline = NULL;
2830 if (width < view->ncols)
2831 waddch(view->window, '\n');
2832 if (--limit <= 0)
2833 return NULL;
2834 err = format_line(&wline, &width, parent_path, view->ncols);
2835 if (err)
2836 return err;
2837 waddwstr(view->window, wline);
2838 free(wline);
2839 wline = NULL;
2840 if (width < view->ncols)
2841 waddch(view->window, '\n');
2842 if (--limit <= 0)
2843 return NULL;
2844 waddch(view->window, '\n');
2845 if (--limit <= 0)
2846 return NULL;
2848 te = SIMPLEQ_FIRST(&entries->head);
2849 if (*first_displayed_entry == NULL) {
2850 if (selected == 0) {
2851 if (view->focussed)
2852 wstandout(view->window);
2853 *selected_entry = NULL;
2855 waddstr(view->window, " ..\n"); /* parent directory */
2856 if (selected == 0 && view->focussed)
2857 wstandend(view->window);
2858 (*ndisplayed)++;
2859 if (--limit <= 0)
2860 return NULL;
2861 n = 1;
2862 } else {
2863 n = 0;
2864 while (te != *first_displayed_entry)
2865 te = SIMPLEQ_NEXT(te, entry);
2868 while (te) {
2869 char *line = NULL, *id_str = NULL;
2871 if (show_ids) {
2872 err = got_object_id_str(&id_str, te->id);
2873 if (err)
2874 return got_error_from_errno();
2876 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2877 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2878 free(id_str);
2879 return got_error_from_errno();
2881 free(id_str);
2882 err = format_line(&wline, &width, line, view->ncols);
2883 if (err) {
2884 free(line);
2885 break;
2887 if (n == selected) {
2888 if (view->focussed)
2889 wstandout(view->window);
2890 *selected_entry = te;
2892 waddwstr(view->window, wline);
2893 if (width < view->ncols)
2894 waddch(view->window, '\n');
2895 if (n == selected && view->focussed)
2896 wstandend(view->window);
2897 free(line);
2898 free(wline);
2899 wline = NULL;
2900 n++;
2901 (*ndisplayed)++;
2902 *last_displayed_entry = te;
2903 if (--limit <= 0)
2904 break;
2905 te = SIMPLEQ_NEXT(te, entry);
2908 return err;
2911 static void
2912 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2913 const struct got_tree_entries *entries, int isroot)
2915 struct got_tree_entry *te, *prev;
2916 int i;
2918 if (*first_displayed_entry == NULL)
2919 return;
2921 te = SIMPLEQ_FIRST(&entries->head);
2922 if (*first_displayed_entry == te) {
2923 if (!isroot)
2924 *first_displayed_entry = NULL;
2925 return;
2928 /* XXX this is stupid... switch to TAILQ? */
2929 for (i = 0; i < maxscroll; i++) {
2930 while (te != *first_displayed_entry) {
2931 prev = te;
2932 te = SIMPLEQ_NEXT(te, entry);
2934 *first_displayed_entry = prev;
2935 te = SIMPLEQ_FIRST(&entries->head);
2937 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2938 *first_displayed_entry = NULL;
2941 static void
2942 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2943 struct got_tree_entry *last_displayed_entry,
2944 const struct got_tree_entries *entries)
2946 struct got_tree_entry *next;
2947 int n = 0;
2949 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2950 return;
2952 if (*first_displayed_entry)
2953 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2954 else
2955 next = SIMPLEQ_FIRST(&entries->head);
2956 while (next) {
2957 *first_displayed_entry = next;
2958 if (++n >= maxscroll)
2959 break;
2960 next = SIMPLEQ_NEXT(next, entry);
2964 static const struct got_error *
2965 tree_entry_path(char **path, struct tog_parent_trees *parents,
2966 struct got_tree_entry *te)
2968 const struct got_error *err = NULL;
2969 struct tog_parent_tree *pt;
2970 size_t len = 2; /* for leading slash and NUL */
2972 TAILQ_FOREACH(pt, parents, entry)
2973 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2974 if (te)
2975 len += strlen(te->name);
2977 *path = calloc(1, len);
2978 if (path == NULL)
2979 return got_error_from_errno();
2981 (*path)[0] = '/';
2982 pt = TAILQ_LAST(parents, tog_parent_trees);
2983 while (pt) {
2984 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2985 err = got_error(GOT_ERR_NO_SPACE);
2986 goto done;
2988 if (strlcat(*path, "/", len) >= len) {
2989 err = got_error(GOT_ERR_NO_SPACE);
2990 goto done;
2992 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2994 if (te) {
2995 if (strlcat(*path, te->name, len) >= len) {
2996 err = got_error(GOT_ERR_NO_SPACE);
2997 goto done;
3000 done:
3001 if (err) {
3002 free(*path);
3003 *path = NULL;
3005 return err;
3008 static const struct got_error *
3009 blame_tree_entry(struct tog_view **new_view, int begin_x,
3010 struct got_tree_entry *te, struct tog_parent_trees *parents,
3011 struct got_object_id *commit_id, struct got_repository *repo)
3013 const struct got_error *err = NULL;
3014 char *path;
3015 struct tog_view *blame_view;
3017 err = tree_entry_path(&path, parents, te);
3018 if (err)
3019 return err;
3021 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3022 if (blame_view == NULL)
3023 return got_error_from_errno();
3025 err = open_blame_view(blame_view, path, commit_id, repo);
3026 if (err) {
3027 view_close(blame_view);
3028 free(path);
3029 } else
3030 *new_view = blame_view;
3031 return err;
3034 static const struct got_error *
3035 log_tree_entry(struct tog_view **new_view, int begin_x,
3036 struct got_tree_entry *te, struct tog_parent_trees *parents,
3037 struct got_object_id *commit_id, struct got_repository *repo)
3039 struct tog_view *log_view;
3040 const struct got_error *err = NULL;
3041 char *path;
3043 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3044 if (log_view == NULL)
3045 return got_error_from_errno();
3047 err = tree_entry_path(&path, parents, te);
3048 if (err)
3049 return err;
3051 err = open_log_view(log_view, commit_id, repo, path);
3052 if (err)
3053 view_close(log_view);
3054 else
3055 *new_view = log_view;
3056 free(path);
3057 return err;
3060 static const struct got_error *
3061 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3062 struct got_object_id *commit_id, struct got_repository *repo)
3064 const struct got_error *err = NULL;
3065 char *commit_id_str = NULL;
3066 struct tog_tree_view_state *s = &view->state.tree;
3068 TAILQ_INIT(&s->parents);
3070 err = got_object_id_str(&commit_id_str, commit_id);
3071 if (err != NULL)
3072 goto done;
3074 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3075 err = got_error_from_errno();
3076 goto done;
3079 s->root = s->tree = root;
3080 s->entries = got_object_tree_get_entries(root);
3081 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3082 s->commit_id = got_object_id_dup(commit_id);
3083 if (s->commit_id == NULL) {
3084 err = got_error_from_errno();
3085 goto done;
3087 s->repo = repo;
3089 view->show = show_tree_view;
3090 view->input = input_tree_view;
3091 view->close = close_tree_view;
3092 done:
3093 free(commit_id_str);
3094 if (err) {
3095 free(s->tree_label);
3096 s->tree_label = NULL;
3098 return err;
3101 static const struct got_error *
3102 close_tree_view(struct tog_view *view)
3104 struct tog_tree_view_state *s = &view->state.tree;
3106 free(s->tree_label);
3107 s->tree_label = NULL;
3108 free(s->commit_id);
3109 s->commit_id = NULL;
3110 while (!TAILQ_EMPTY(&s->parents)) {
3111 struct tog_parent_tree *parent;
3112 parent = TAILQ_FIRST(&s->parents);
3113 TAILQ_REMOVE(&s->parents, parent, entry);
3114 free(parent);
3117 if (s->tree != s->root)
3118 got_object_tree_close(s->tree);
3119 got_object_tree_close(s->root);
3121 return NULL;
3124 static const struct got_error *
3125 show_tree_view(struct tog_view *view)
3127 const struct got_error *err = NULL;
3128 struct tog_tree_view_state *s = &view->state.tree;
3129 char *parent_path;
3131 err = tree_entry_path(&parent_path, &s->parents, NULL);
3132 if (err)
3133 return err;
3135 err = draw_tree_entries(view, &s->first_displayed_entry,
3136 &s->last_displayed_entry, &s->selected_entry,
3137 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3138 s->entries, s->selected, view->nlines, s->tree == s->root);
3139 free(parent_path);
3141 view_vborder(view);
3142 return err;
3145 static const struct got_error *
3146 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3147 struct tog_view **focus_view, struct tog_view *view, int ch)
3149 const struct got_error *err = NULL;
3150 struct tog_tree_view_state *s = &view->state.tree;
3151 struct tog_view *log_view;
3152 int begin_x = 0;
3154 switch (ch) {
3155 case 'i':
3156 s->show_ids = !s->show_ids;
3157 break;
3158 case 'l':
3159 if (!s->selected_entry)
3160 break;
3161 if (view_is_parent_view(view))
3162 begin_x = view_split_begin_x(view->begin_x);
3163 err = log_tree_entry(&log_view, begin_x,
3164 s->selected_entry, &s->parents,
3165 s->commit_id, s->repo);
3166 if (view_is_parent_view(view)) {
3167 err = view_close_child(view);
3168 if (err)
3169 return err;
3170 err = view_set_child(view, log_view);
3171 if (err) {
3172 view_close(log_view);
3173 break;
3175 *focus_view = log_view;
3176 view->child_focussed = 1;
3177 } else
3178 *new_view = log_view;
3179 break;
3180 case 'k':
3181 case KEY_UP:
3182 if (s->selected > 0)
3183 s->selected--;
3184 if (s->selected > 0)
3185 break;
3186 tree_scroll_up(&s->first_displayed_entry, 1,
3187 s->entries, s->tree == s->root);
3188 break;
3189 case KEY_PPAGE:
3190 if (SIMPLEQ_FIRST(&s->entries->head) ==
3191 s->first_displayed_entry) {
3192 if (s->tree != s->root)
3193 s->first_displayed_entry = NULL;
3194 s->selected = 0;
3195 break;
3197 tree_scroll_up(&s->first_displayed_entry,
3198 view->nlines, s->entries,
3199 s->tree == s->root);
3200 break;
3201 case 'j':
3202 case KEY_DOWN:
3203 if (s->selected < s->ndisplayed - 1) {
3204 s->selected++;
3205 break;
3207 tree_scroll_down(&s->first_displayed_entry, 1,
3208 s->last_displayed_entry, s->entries);
3209 break;
3210 case KEY_NPAGE:
3211 tree_scroll_down(&s->first_displayed_entry,
3212 view->nlines, s->last_displayed_entry,
3213 s->entries);
3214 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3215 entry))
3216 break;
3217 /* can't scroll any further; move cursor down */
3218 if (s->selected < s->ndisplayed - 1)
3219 s->selected = s->ndisplayed - 1;
3220 break;
3221 case KEY_ENTER:
3222 case '\r':
3223 if (s->selected_entry == NULL) {
3224 struct tog_parent_tree *parent;
3225 case KEY_BACKSPACE:
3226 /* user selected '..' */
3227 if (s->tree == s->root)
3228 break;
3229 parent = TAILQ_FIRST(&s->parents);
3230 TAILQ_REMOVE(&s->parents, parent,
3231 entry);
3232 got_object_tree_close(s->tree);
3233 s->tree = parent->tree;
3234 s->entries =
3235 got_object_tree_get_entries(s->tree);
3236 s->first_displayed_entry =
3237 parent->first_displayed_entry;
3238 s->selected_entry =
3239 parent->selected_entry;
3240 s->selected = parent->selected;
3241 free(parent);
3242 } else if (S_ISDIR(s->selected_entry->mode)) {
3243 struct tog_parent_tree *parent;
3244 struct got_tree_object *child;
3245 err = got_object_open_as_tree(&child,
3246 s->repo, s->selected_entry->id);
3247 if (err)
3248 break;
3249 parent = calloc(1, sizeof(*parent));
3250 if (parent == NULL) {
3251 err = got_error_from_errno();
3252 break;
3254 parent->tree = s->tree;
3255 parent->first_displayed_entry =
3256 s->first_displayed_entry;
3257 parent->selected_entry = s->selected_entry;
3258 parent->selected = s->selected;
3259 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3260 s->tree = child;
3261 s->entries =
3262 got_object_tree_get_entries(s->tree);
3263 s->selected = 0;
3264 s->first_displayed_entry = NULL;
3265 } else if (S_ISREG(s->selected_entry->mode)) {
3266 struct tog_view *blame_view;
3267 int begin_x = view_is_parent_view(view) ?
3268 view_split_begin_x(view->begin_x) : 0;
3270 err = blame_tree_entry(&blame_view, begin_x,
3271 s->selected_entry, &s->parents, s->commit_id,
3272 s->repo);
3273 if (err)
3274 break;
3275 if (view_is_parent_view(view)) {
3276 err = view_close_child(view);
3277 if (err)
3278 return err;
3279 err = view_set_child(view, blame_view);
3280 if (err) {
3281 view_close(blame_view);
3282 break;
3284 *focus_view = blame_view;
3285 view->child_focussed = 1;
3286 } else
3287 *new_view = blame_view;
3289 break;
3290 case KEY_RESIZE:
3291 if (s->selected > view->nlines)
3292 s->selected = s->ndisplayed - 1;
3293 break;
3294 default:
3295 break;
3298 return err;
3301 __dead static void
3302 usage_tree(void)
3304 endwin();
3305 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3306 getprogname());
3307 exit(1);
3310 static const struct got_error *
3311 cmd_tree(int argc, char *argv[])
3313 const struct got_error *error;
3314 struct got_repository *repo = NULL;
3315 char *repo_path = NULL;
3316 struct got_object_id *commit_id = NULL;
3317 char *commit_id_arg = NULL;
3318 struct got_commit_object *commit = NULL;
3319 struct got_tree_object *tree = NULL;
3320 int ch;
3321 struct tog_view *view;
3323 #ifndef PROFILE
3324 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3325 == -1)
3326 err(1, "pledge");
3327 #endif
3329 while ((ch = getopt(argc, argv, "c:")) != -1) {
3330 switch (ch) {
3331 case 'c':
3332 commit_id_arg = optarg;
3333 break;
3334 default:
3335 usage();
3336 /* NOTREACHED */
3340 argc -= optind;
3341 argv += optind;
3343 if (argc == 0) {
3344 repo_path = getcwd(NULL, 0);
3345 if (repo_path == NULL)
3346 return got_error_from_errno();
3347 } else if (argc == 1) {
3348 repo_path = realpath(argv[0], NULL);
3349 if (repo_path == NULL)
3350 return got_error_from_errno();
3351 } else
3352 usage_log();
3354 error = got_repo_open(&repo, repo_path);
3355 free(repo_path);
3356 if (error != NULL)
3357 return error;
3359 if (commit_id_arg == NULL) {
3360 error = get_head_commit_id(&commit_id, repo);
3361 if (error != NULL)
3362 goto done;
3363 } else {
3364 struct got_object *obj;
3365 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3366 if (error == NULL) {
3367 commit_id = got_object_id_dup(got_object_get_id(obj));
3368 if (commit_id == NULL)
3369 error = got_error_from_errno();
3372 if (error != NULL)
3373 goto done;
3375 error = got_object_open_as_commit(&commit, repo, commit_id);
3376 if (error != NULL)
3377 goto done;
3379 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3380 if (error != NULL)
3381 goto done;
3383 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3384 if (view == NULL) {
3385 error = got_error_from_errno();
3386 goto done;
3388 error = open_tree_view(view, tree, commit_id, repo);
3389 if (error)
3390 goto done;
3391 error = view_loop(view);
3392 done:
3393 free(commit_id);
3394 if (commit)
3395 got_object_commit_close(commit);
3396 if (tree)
3397 got_object_tree_close(tree);
3398 if (repo)
3399 got_repo_close(repo);
3400 return error;
3402 static void
3403 init_curses(void)
3405 initscr();
3406 cbreak();
3407 halfdelay(1); /* Do fast refresh while initial view is loading. */
3408 noecho();
3409 nonl();
3410 intrflush(stdscr, FALSE);
3411 keypad(stdscr, TRUE);
3412 curs_set(0);
3415 __dead static void
3416 usage(void)
3418 int i;
3420 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3421 "Available commands:\n", getprogname());
3422 for (i = 0; i < nitems(tog_commands); i++) {
3423 struct tog_cmd *cmd = &tog_commands[i];
3424 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3426 exit(1);
3429 static char **
3430 make_argv(const char *arg0, const char *arg1)
3432 char **argv;
3433 int argc = (arg1 == NULL ? 1 : 2);
3435 argv = calloc(argc, sizeof(char *));
3436 if (argv == NULL)
3437 err(1, "calloc");
3438 argv[0] = strdup(arg0);
3439 if (argv[0] == NULL)
3440 err(1, "calloc");
3441 if (arg1) {
3442 argv[1] = strdup(arg1);
3443 if (argv[1] == NULL)
3444 err(1, "calloc");
3447 return argv;
3450 int
3451 main(int argc, char *argv[])
3453 const struct got_error *error = NULL;
3454 struct tog_cmd *cmd = NULL;
3455 int ch, hflag = 0;
3456 char **cmd_argv = NULL;
3458 setlocale(LC_ALL, "");
3460 while ((ch = getopt(argc, argv, "h")) != -1) {
3461 switch (ch) {
3462 case 'h':
3463 hflag = 1;
3464 break;
3465 default:
3466 usage();
3467 /* NOTREACHED */
3471 argc -= optind;
3472 argv += optind;
3473 optind = 0;
3474 optreset = 1;
3476 if (argc == 0) {
3477 if (hflag)
3478 usage();
3479 /* Build an argument vector which runs a default command. */
3480 cmd = &tog_commands[0];
3481 cmd_argv = make_argv(cmd->name, NULL);
3482 argc = 1;
3483 } else {
3484 int i;
3486 /* Did the user specific a command? */
3487 for (i = 0; i < nitems(tog_commands); i++) {
3488 if (strncmp(tog_commands[i].name, argv[0],
3489 strlen(argv[0])) == 0) {
3490 cmd = &tog_commands[i];
3491 if (hflag)
3492 tog_commands[i].cmd_usage();
3493 break;
3496 if (cmd == NULL) {
3497 /* Did the user specify a repository? */
3498 char *repo_path = realpath(argv[0], NULL);
3499 if (repo_path) {
3500 struct got_repository *repo;
3501 error = got_repo_open(&repo, repo_path);
3502 if (error == NULL)
3503 got_repo_close(repo);
3504 } else
3505 error = got_error_from_errno();
3506 if (error) {
3507 if (hflag) {
3508 fprintf(stderr, "%s: '%s' is not a "
3509 "known command\n", getprogname(),
3510 argv[0]);
3511 usage();
3513 fprintf(stderr, "%s: '%s' is neither a known "
3514 "command nor a path to a repository\n",
3515 getprogname(), argv[0]);
3516 free(repo_path);
3517 return 1;
3519 cmd = &tog_commands[0];
3520 cmd_argv = make_argv(cmd->name, repo_path);
3521 argc = 2;
3522 free(repo_path);
3526 init_curses();
3528 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3529 if (error)
3530 goto done;
3531 done:
3532 endwin();
3533 free(cmd_argv);
3534 if (error)
3535 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3536 return 0;