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 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 done:
2224 errcode = pthread_mutex_unlock(&tog_mutex);
2225 if (errcode)
2226 err = got_error_set_errno(errcode);
2227 return err;
2230 static void *
2231 blame_thread(void *arg)
2233 const struct got_error *err;
2234 struct tog_blame_thread_args *ta = arg;
2235 struct tog_blame_cb_args *a = ta->cb_args;
2236 int errcode;
2238 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2239 blame_cb, ta->cb_args);
2241 errcode = pthread_mutex_lock(&tog_mutex);
2242 if (errcode)
2243 return (void *)got_error_set_errno(errcode);
2245 got_repo_close(ta->repo);
2246 ta->repo = NULL;
2247 *ta->complete = 1;
2248 if (!err) {
2249 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2250 a->lines, a->nlines, 1, *a->selected_line,
2251 a->first_displayed_line, a->last_displayed_line, a->eof,
2252 a->view->nlines);
2255 errcode = pthread_mutex_unlock(&tog_mutex);
2256 if (errcode && err == NULL)
2257 err = got_error_set_errno(errcode);
2259 return (void *)err;
2262 static struct got_object_id *
2263 get_selected_commit_id(struct tog_blame_line *lines,
2264 int first_displayed_line, int selected_line)
2266 struct tog_blame_line *line;
2268 line = &lines[first_displayed_line - 1 + selected_line - 1];
2269 if (!line->annotated)
2270 return NULL;
2272 return line->id;
2275 static const struct got_error *
2276 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2277 struct tog_blame_line *lines, int first_displayed_line,
2278 int selected_line, struct got_repository *repo)
2280 const struct got_error *err = NULL;
2281 struct got_commit_object *commit = NULL;
2282 struct got_object_id *selected_id;
2283 struct got_object_qid *pid;
2285 *pobj = NULL;
2286 *obj = NULL;
2288 selected_id = get_selected_commit_id(lines,
2289 first_displayed_line, selected_line);
2290 if (selected_id == NULL)
2291 return NULL;
2293 err = got_object_open(obj, repo, selected_id);
2294 if (err)
2295 goto done;
2297 err = got_object_commit_open(&commit, repo, *obj);
2298 if (err)
2299 goto done;
2301 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2302 if (pid) {
2303 err = got_object_open(pobj, repo, pid->id);
2304 if (err)
2305 goto done;
2307 done:
2308 if (commit)
2309 got_object_commit_close(commit);
2310 return err;
2313 static const struct got_error *
2314 stop_blame(struct tog_blame *blame)
2316 const struct got_error *err = NULL;
2317 int i;
2319 if (blame->thread) {
2320 int errcode;
2321 errcode = pthread_mutex_unlock(&tog_mutex);
2322 if (errcode)
2323 return got_error_set_errno(errcode);
2324 errcode = pthread_join(blame->thread, (void **)&err);
2325 if (errcode)
2326 return got_error_set_errno(errcode);
2327 errcode = pthread_mutex_lock(&tog_mutex);
2328 if (errcode)
2329 return got_error_set_errno(errcode);
2330 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2331 err = NULL;
2332 blame->thread = NULL;
2334 if (blame->thread_args.repo) {
2335 got_repo_close(blame->thread_args.repo);
2336 blame->thread_args.repo = NULL;
2338 if (blame->f) {
2339 fclose(blame->f);
2340 blame->f = NULL;
2342 for (i = 0; i < blame->nlines; i++)
2343 free(blame->lines[i].id);
2344 free(blame->lines);
2345 blame->lines = NULL;
2346 free(blame->cb_args.commit_id);
2347 blame->cb_args.commit_id = NULL;
2349 return err;
2352 static const struct got_error *
2353 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2354 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2355 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2356 struct got_repository *repo)
2358 const struct got_error *err = NULL;
2359 struct got_blob_object *blob = NULL;
2360 struct got_repository *thread_repo = NULL;
2361 struct got_object_id *obj_id = NULL;
2362 struct got_object *obj = NULL;
2363 int errcode;
2365 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2366 if (err)
2367 goto done;
2369 err = got_object_open(&obj, repo, obj_id);
2370 if (err)
2371 goto done;
2373 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2374 err = got_error(GOT_ERR_OBJ_TYPE);
2375 goto done;
2378 err = got_object_blob_open(&blob, repo, obj, 8192);
2379 if (err)
2380 goto done;
2381 blame->f = got_opentemp();
2382 if (blame->f == NULL) {
2383 err = got_error_from_errno();
2384 goto done;
2386 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2387 blame->f, blob);
2388 if (err)
2389 goto done;
2391 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2392 if (blame->lines == NULL) {
2393 err = got_error_from_errno();
2394 goto done;
2397 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2398 if (err)
2399 goto done;
2401 blame->cb_args.view = view;
2402 blame->cb_args.lines = blame->lines;
2403 blame->cb_args.nlines = blame->nlines;
2404 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2405 if (blame->cb_args.commit_id == NULL) {
2406 err = got_error_from_errno();
2407 goto done;
2409 blame->cb_args.f = blame->f;
2410 blame->cb_args.path = path;
2411 blame->cb_args.first_displayed_line = first_displayed_line;
2412 blame->cb_args.selected_line = selected_line;
2413 blame->cb_args.last_displayed_line = last_displayed_line;
2414 blame->cb_args.quit = done;
2415 blame->cb_args.eof = eof;
2417 blame->thread_args.path = path;
2418 blame->thread_args.repo = thread_repo;
2419 blame->thread_args.cb_args = &blame->cb_args;
2420 blame->thread_args.complete = blame_complete;
2421 *blame_complete = 0;
2423 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2424 &blame->thread_args);
2425 if (errcode) {
2426 err = got_error_set_errno(errcode);
2427 goto done;
2430 done:
2431 if (blob)
2432 got_object_blob_close(blob);
2433 free(obj_id);
2434 if (obj)
2435 got_object_close(obj);
2436 if (err)
2437 stop_blame(blame);
2438 return err;
2441 static const struct got_error *
2442 open_blame_view(struct tog_view *view, char *path,
2443 struct got_object_id *commit_id, struct got_repository *repo)
2445 const struct got_error *err = NULL;
2446 struct tog_blame_view_state *s = &view->state.blame;
2448 SIMPLEQ_INIT(&s->blamed_commits);
2450 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2451 if (err)
2452 return err;
2454 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2455 s->first_displayed_line = 1;
2456 s->last_displayed_line = view->nlines;
2457 s->selected_line = 1;
2458 s->blame_complete = 0;
2459 s->path = path;
2460 if (s->path == NULL)
2461 return got_error_from_errno();
2462 s->repo = repo;
2463 s->commit_id = commit_id;
2464 memset(&s->blame, 0, sizeof(s->blame));
2466 view->show = show_blame_view;
2467 view->input = input_blame_view;
2468 view->close = close_blame_view;
2470 return run_blame(&s->blame, view, &s->blame_complete,
2471 &s->first_displayed_line, &s->last_displayed_line,
2472 &s->selected_line, &s->done, &s->eof, s->path,
2473 s->blamed_commit->id, s->repo);
2476 static const struct got_error *
2477 close_blame_view(struct tog_view *view)
2479 const struct got_error *err = NULL;
2480 struct tog_blame_view_state *s = &view->state.blame;
2482 if (s->blame.thread)
2483 err = stop_blame(&s->blame);
2485 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2486 struct got_object_qid *blamed_commit;
2487 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2488 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2489 got_object_qid_free(blamed_commit);
2492 free(s->path);
2494 return err;
2497 static const struct got_error *
2498 show_blame_view(struct tog_view *view)
2500 const struct got_error *err = NULL;
2501 struct tog_blame_view_state *s = &view->state.blame;
2503 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2504 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2505 s->selected_line, &s->first_displayed_line,
2506 &s->last_displayed_line, &s->eof, view->nlines);
2508 view_vborder(view);
2509 return err;
2512 static const struct got_error *
2513 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2514 struct tog_view **focus_view, struct tog_view *view, int ch)
2516 const struct got_error *err = NULL, *thread_err = NULL;
2517 struct got_object *obj = NULL, *pobj = NULL;
2518 struct tog_view *diff_view;
2519 struct tog_blame_view_state *s = &view->state.blame;
2520 int begin_x = 0;
2522 switch (ch) {
2523 case 'q':
2524 s->done = 1;
2525 break;
2526 case 'k':
2527 case KEY_UP:
2528 if (s->selected_line > 1)
2529 s->selected_line--;
2530 else if (s->selected_line == 1 &&
2531 s->first_displayed_line > 1)
2532 s->first_displayed_line--;
2533 break;
2534 case KEY_PPAGE:
2535 if (s->first_displayed_line == 1) {
2536 s->selected_line = 1;
2537 break;
2539 if (s->first_displayed_line > view->nlines - 2)
2540 s->first_displayed_line -=
2541 (view->nlines - 2);
2542 else
2543 s->first_displayed_line = 1;
2544 break;
2545 case 'j':
2546 case KEY_DOWN:
2547 if (s->selected_line < view->nlines - 2 &&
2548 s->first_displayed_line +
2549 s->selected_line <= s->blame.nlines)
2550 s->selected_line++;
2551 else if (s->last_displayed_line <
2552 s->blame.nlines)
2553 s->first_displayed_line++;
2554 break;
2555 case 'b':
2556 case 'p': {
2557 struct got_object_id *id;
2558 id = get_selected_commit_id(s->blame.lines,
2559 s->first_displayed_line, s->selected_line);
2560 if (id == NULL || got_object_id_cmp(id,
2561 s->blamed_commit->id) == 0)
2562 break;
2563 err = open_selected_commit(&pobj, &obj,
2564 s->blame.lines, s->first_displayed_line,
2565 s->selected_line, s->repo);
2566 if (err)
2567 break;
2568 if (pobj == NULL && obj == NULL)
2569 break;
2570 if (ch == 'p' && pobj == NULL)
2571 break;
2572 s->done = 1;
2573 thread_err = stop_blame(&s->blame);
2574 s->done = 0;
2575 if (thread_err)
2576 break;
2577 id = got_object_get_id(ch == 'b' ? obj : pobj);
2578 got_object_close(obj);
2579 obj = NULL;
2580 if (pobj) {
2581 got_object_close(pobj);
2582 pobj = NULL;
2584 err = got_object_qid_alloc(&s->blamed_commit, id);
2585 if (err)
2586 goto done;
2587 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2588 s->blamed_commit, entry);
2589 err = run_blame(&s->blame, view, &s->blame_complete,
2590 &s->first_displayed_line, &s->last_displayed_line,
2591 &s->selected_line, &s->done, &s->eof,
2592 s->path, s->blamed_commit->id, s->repo);
2593 if (err)
2594 break;
2595 break;
2597 case 'B': {
2598 struct got_object_qid *first;
2599 first = SIMPLEQ_FIRST(&s->blamed_commits);
2600 if (!got_object_id_cmp(first->id, s->commit_id))
2601 break;
2602 s->done = 1;
2603 thread_err = stop_blame(&s->blame);
2604 s->done = 0;
2605 if (thread_err)
2606 break;
2607 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2608 got_object_qid_free(s->blamed_commit);
2609 s->blamed_commit =
2610 SIMPLEQ_FIRST(&s->blamed_commits);
2611 err = run_blame(&s->blame, view, &s->blame_complete,
2612 &s->first_displayed_line, &s->last_displayed_line,
2613 &s->selected_line, &s->done, &s->eof, s->path,
2614 s->blamed_commit->id, s->repo);
2615 if (err)
2616 break;
2617 break;
2619 case KEY_ENTER:
2620 case '\r':
2621 err = open_selected_commit(&pobj, &obj,
2622 s->blame.lines, s->first_displayed_line,
2623 s->selected_line, s->repo);
2624 if (err)
2625 break;
2626 if (pobj == NULL && obj == NULL)
2627 break;
2629 if (view_is_parent_view(view))
2630 begin_x = view_split_begin_x(view->begin_x);
2631 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2632 if (diff_view == NULL) {
2633 err = got_error_from_errno();
2634 break;
2636 err = open_diff_view(diff_view, pobj, obj, s->repo);
2637 if (err) {
2638 view_close(diff_view);
2639 break;
2641 if (view_is_parent_view(view)) {
2642 err = view_close_child(view);
2643 if (err)
2644 return err;
2645 err = view_set_child(view, diff_view);
2646 if (err) {
2647 view_close(diff_view);
2648 break;
2650 if (!view_is_splitscreen(diff_view)) {
2651 *focus_view = diff_view;
2652 view->child_focussed = 1;
2654 } else
2655 *new_view = diff_view;
2656 if (pobj) {
2657 got_object_close(pobj);
2658 pobj = NULL;
2660 got_object_close(obj);
2661 obj = NULL;
2662 if (err)
2663 break;
2664 break;
2665 case KEY_NPAGE:
2666 case ' ':
2667 if (s->last_displayed_line >= s->blame.nlines &&
2668 s->selected_line < view->nlines - 2) {
2669 s->selected_line = MIN(s->blame.nlines,
2670 view->nlines - 2);
2671 break;
2673 if (s->last_displayed_line + view->nlines - 2
2674 <= s->blame.nlines)
2675 s->first_displayed_line +=
2676 view->nlines - 2;
2677 else
2678 s->first_displayed_line =
2679 s->blame.nlines -
2680 (view->nlines - 3);
2681 break;
2682 case KEY_RESIZE:
2683 if (s->selected_line > view->nlines - 2) {
2684 s->selected_line = MIN(s->blame.nlines,
2685 view->nlines - 2);
2687 break;
2688 default:
2689 break;
2691 done:
2692 if (pobj)
2693 got_object_close(pobj);
2694 return thread_err ? thread_err : err;
2697 static const struct got_error *
2698 cmd_blame(int argc, char *argv[])
2700 const struct got_error *error;
2701 struct got_repository *repo = NULL;
2702 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2703 struct got_object_id *commit_id = NULL;
2704 char *commit_id_str = NULL;
2705 int ch;
2706 struct tog_view *view;
2708 #ifndef PROFILE
2709 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2710 == -1)
2711 err(1, "pledge");
2712 #endif
2714 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2715 switch (ch) {
2716 case 'c':
2717 commit_id_str = optarg;
2718 break;
2719 case 'r':
2720 repo_path = realpath(optarg, NULL);
2721 if (repo_path == NULL)
2722 err(1, "-r option");
2723 break;
2724 default:
2725 usage();
2726 /* NOTREACHED */
2730 argc -= optind;
2731 argv += optind;
2733 if (argc == 1)
2734 path = argv[0];
2735 else
2736 usage_blame();
2738 cwd = getcwd(NULL, 0);
2739 if (cwd == NULL) {
2740 error = got_error_from_errno();
2741 goto done;
2743 if (repo_path == NULL) {
2744 repo_path = strdup(cwd);
2745 if (repo_path == NULL) {
2746 error = got_error_from_errno();
2747 goto done;
2752 error = got_repo_open(&repo, repo_path);
2753 if (error != NULL)
2754 return error;
2756 error = got_repo_map_path(&in_repo_path, repo, path);
2757 if (error != NULL)
2758 goto done;
2760 if (commit_id_str == NULL) {
2761 struct got_reference *head_ref;
2762 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2763 if (error != NULL)
2764 goto done;
2765 error = got_ref_resolve(&commit_id, repo, head_ref);
2766 got_ref_close(head_ref);
2767 } else {
2768 struct got_object *obj;
2769 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2770 if (error != NULL)
2771 goto done;
2772 commit_id = got_object_id_dup(got_object_get_id(obj));
2773 if (commit_id == NULL)
2774 error = got_error_from_errno();
2775 got_object_close(obj);
2777 if (error != NULL)
2778 goto done;
2780 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2781 if (view == NULL) {
2782 error = got_error_from_errno();
2783 goto done;
2785 error = open_blame_view(view, in_repo_path, commit_id, repo);
2786 if (error)
2787 goto done;
2788 error = view_loop(view);
2789 done:
2790 free(repo_path);
2791 free(cwd);
2792 free(commit_id);
2793 if (repo)
2794 got_repo_close(repo);
2795 return error;
2798 static const struct got_error *
2799 draw_tree_entries(struct tog_view *view,
2800 struct got_tree_entry **first_displayed_entry,
2801 struct got_tree_entry **last_displayed_entry,
2802 struct got_tree_entry **selected_entry, int *ndisplayed,
2803 const char *label, int show_ids, const char *parent_path,
2804 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2806 const struct got_error *err = NULL;
2807 struct got_tree_entry *te;
2808 wchar_t *wline;
2809 int width, n;
2811 *ndisplayed = 0;
2813 werase(view->window);
2815 if (limit == 0)
2816 return NULL;
2818 err = format_line(&wline, &width, label, view->ncols);
2819 if (err)
2820 return err;
2821 if (view_needs_focus_indication(view))
2822 wstandout(view->window);
2823 waddwstr(view->window, wline);
2824 if (view_needs_focus_indication(view))
2825 wstandend(view->window);
2826 free(wline);
2827 wline = NULL;
2828 if (width < view->ncols)
2829 waddch(view->window, '\n');
2830 if (--limit <= 0)
2831 return NULL;
2832 err = format_line(&wline, &width, parent_path, view->ncols);
2833 if (err)
2834 return err;
2835 waddwstr(view->window, wline);
2836 free(wline);
2837 wline = NULL;
2838 if (width < view->ncols)
2839 waddch(view->window, '\n');
2840 if (--limit <= 0)
2841 return NULL;
2842 waddch(view->window, '\n');
2843 if (--limit <= 0)
2844 return NULL;
2846 te = SIMPLEQ_FIRST(&entries->head);
2847 if (*first_displayed_entry == NULL) {
2848 if (selected == 0) {
2849 if (view->focussed)
2850 wstandout(view->window);
2851 *selected_entry = NULL;
2853 waddstr(view->window, " ..\n"); /* parent directory */
2854 if (selected == 0 && view->focussed)
2855 wstandend(view->window);
2856 (*ndisplayed)++;
2857 if (--limit <= 0)
2858 return NULL;
2859 n = 1;
2860 } else {
2861 n = 0;
2862 while (te != *first_displayed_entry)
2863 te = SIMPLEQ_NEXT(te, entry);
2866 while (te) {
2867 char *line = NULL, *id_str = NULL;
2869 if (show_ids) {
2870 err = got_object_id_str(&id_str, te->id);
2871 if (err)
2872 return got_error_from_errno();
2874 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2875 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2876 free(id_str);
2877 return got_error_from_errno();
2879 free(id_str);
2880 err = format_line(&wline, &width, line, view->ncols);
2881 if (err) {
2882 free(line);
2883 break;
2885 if (n == selected) {
2886 if (view->focussed)
2887 wstandout(view->window);
2888 *selected_entry = te;
2890 waddwstr(view->window, wline);
2891 if (width < view->ncols)
2892 waddch(view->window, '\n');
2893 if (n == selected && view->focussed)
2894 wstandend(view->window);
2895 free(line);
2896 free(wline);
2897 wline = NULL;
2898 n++;
2899 (*ndisplayed)++;
2900 *last_displayed_entry = te;
2901 if (--limit <= 0)
2902 break;
2903 te = SIMPLEQ_NEXT(te, entry);
2906 return err;
2909 static void
2910 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2911 const struct got_tree_entries *entries, int isroot)
2913 struct got_tree_entry *te, *prev;
2914 int i;
2916 if (*first_displayed_entry == NULL)
2917 return;
2919 te = SIMPLEQ_FIRST(&entries->head);
2920 if (*first_displayed_entry == te) {
2921 if (!isroot)
2922 *first_displayed_entry = NULL;
2923 return;
2926 /* XXX this is stupid... switch to TAILQ? */
2927 for (i = 0; i < maxscroll; i++) {
2928 while (te != *first_displayed_entry) {
2929 prev = te;
2930 te = SIMPLEQ_NEXT(te, entry);
2932 *first_displayed_entry = prev;
2933 te = SIMPLEQ_FIRST(&entries->head);
2935 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2936 *first_displayed_entry = NULL;
2939 static void
2940 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2941 struct got_tree_entry *last_displayed_entry,
2942 const struct got_tree_entries *entries)
2944 struct got_tree_entry *next;
2945 int n = 0;
2947 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2948 return;
2950 if (*first_displayed_entry)
2951 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2952 else
2953 next = SIMPLEQ_FIRST(&entries->head);
2954 while (next) {
2955 *first_displayed_entry = next;
2956 if (++n >= maxscroll)
2957 break;
2958 next = SIMPLEQ_NEXT(next, entry);
2962 static const struct got_error *
2963 tree_entry_path(char **path, struct tog_parent_trees *parents,
2964 struct got_tree_entry *te)
2966 const struct got_error *err = NULL;
2967 struct tog_parent_tree *pt;
2968 size_t len = 2; /* for leading slash and NUL */
2970 TAILQ_FOREACH(pt, parents, entry)
2971 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2972 if (te)
2973 len += strlen(te->name);
2975 *path = calloc(1, len);
2976 if (path == NULL)
2977 return got_error_from_errno();
2979 (*path)[0] = '/';
2980 pt = TAILQ_LAST(parents, tog_parent_trees);
2981 while (pt) {
2982 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2983 err = got_error(GOT_ERR_NO_SPACE);
2984 goto done;
2986 if (strlcat(*path, "/", len) >= len) {
2987 err = got_error(GOT_ERR_NO_SPACE);
2988 goto done;
2990 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2992 if (te) {
2993 if (strlcat(*path, te->name, len) >= len) {
2994 err = got_error(GOT_ERR_NO_SPACE);
2995 goto done;
2998 done:
2999 if (err) {
3000 free(*path);
3001 *path = NULL;
3003 return err;
3006 static const struct got_error *
3007 blame_tree_entry(struct tog_view **new_view, int begin_x,
3008 struct got_tree_entry *te, struct tog_parent_trees *parents,
3009 struct got_object_id *commit_id, struct got_repository *repo)
3011 const struct got_error *err = NULL;
3012 char *path;
3013 struct tog_view *blame_view;
3015 err = tree_entry_path(&path, parents, te);
3016 if (err)
3017 return err;
3019 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3020 if (blame_view == NULL)
3021 return got_error_from_errno();
3023 err = open_blame_view(blame_view, path, commit_id, repo);
3024 if (err) {
3025 view_close(blame_view);
3026 free(path);
3027 } else
3028 *new_view = blame_view;
3029 return err;
3032 static const struct got_error *
3033 log_tree_entry(struct tog_view **new_view, int begin_x,
3034 struct got_tree_entry *te, struct tog_parent_trees *parents,
3035 struct got_object_id *commit_id, struct got_repository *repo)
3037 struct tog_view *log_view;
3038 const struct got_error *err = NULL;
3039 char *path;
3041 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3042 if (log_view == NULL)
3043 return got_error_from_errno();
3045 err = tree_entry_path(&path, parents, te);
3046 if (err)
3047 return err;
3049 err = open_log_view(log_view, commit_id, repo, path);
3050 if (err)
3051 view_close(log_view);
3052 else
3053 *new_view = log_view;
3054 free(path);
3055 return err;
3058 static const struct got_error *
3059 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3060 struct got_object_id *commit_id, struct got_repository *repo)
3062 const struct got_error *err = NULL;
3063 char *commit_id_str = NULL;
3064 struct tog_tree_view_state *s = &view->state.tree;
3066 TAILQ_INIT(&s->parents);
3068 err = got_object_id_str(&commit_id_str, commit_id);
3069 if (err != NULL)
3070 goto done;
3072 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3073 err = got_error_from_errno();
3074 goto done;
3077 s->root = s->tree = root;
3078 s->entries = got_object_tree_get_entries(root);
3079 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3080 s->commit_id = got_object_id_dup(commit_id);
3081 if (s->commit_id == NULL) {
3082 err = got_error_from_errno();
3083 goto done;
3085 s->repo = repo;
3087 view->show = show_tree_view;
3088 view->input = input_tree_view;
3089 view->close = close_tree_view;
3090 done:
3091 free(commit_id_str);
3092 if (err) {
3093 free(s->tree_label);
3094 s->tree_label = NULL;
3096 return err;
3099 static const struct got_error *
3100 close_tree_view(struct tog_view *view)
3102 struct tog_tree_view_state *s = &view->state.tree;
3104 free(s->tree_label);
3105 s->tree_label = NULL;
3106 free(s->commit_id);
3107 s->commit_id = NULL;
3108 while (!TAILQ_EMPTY(&s->parents)) {
3109 struct tog_parent_tree *parent;
3110 parent = TAILQ_FIRST(&s->parents);
3111 TAILQ_REMOVE(&s->parents, parent, entry);
3112 free(parent);
3115 if (s->tree != s->root)
3116 got_object_tree_close(s->tree);
3117 got_object_tree_close(s->root);
3119 return NULL;
3122 static const struct got_error *
3123 show_tree_view(struct tog_view *view)
3125 const struct got_error *err = NULL;
3126 struct tog_tree_view_state *s = &view->state.tree;
3127 char *parent_path;
3129 err = tree_entry_path(&parent_path, &s->parents, NULL);
3130 if (err)
3131 return err;
3133 err = draw_tree_entries(view, &s->first_displayed_entry,
3134 &s->last_displayed_entry, &s->selected_entry,
3135 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3136 s->entries, s->selected, view->nlines, s->tree == s->root);
3137 free(parent_path);
3139 view_vborder(view);
3140 return err;
3143 static const struct got_error *
3144 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3145 struct tog_view **focus_view, struct tog_view *view, int ch)
3147 const struct got_error *err = NULL;
3148 struct tog_tree_view_state *s = &view->state.tree;
3149 struct tog_view *log_view;
3150 int begin_x = 0;
3152 switch (ch) {
3153 case 'i':
3154 s->show_ids = !s->show_ids;
3155 break;
3156 case 'l':
3157 if (!s->selected_entry)
3158 break;
3159 if (view_is_parent_view(view))
3160 begin_x = view_split_begin_x(view->begin_x);
3161 err = log_tree_entry(&log_view, begin_x,
3162 s->selected_entry, &s->parents,
3163 s->commit_id, s->repo);
3164 if (view_is_parent_view(view)) {
3165 err = view_close_child(view);
3166 if (err)
3167 return err;
3168 err = view_set_child(view, log_view);
3169 if (err) {
3170 view_close(log_view);
3171 break;
3173 *focus_view = log_view;
3174 view->child_focussed = 1;
3175 } else
3176 *new_view = log_view;
3177 break;
3178 case 'k':
3179 case KEY_UP:
3180 if (s->selected > 0)
3181 s->selected--;
3182 if (s->selected > 0)
3183 break;
3184 tree_scroll_up(&s->first_displayed_entry, 1,
3185 s->entries, s->tree == s->root);
3186 break;
3187 case KEY_PPAGE:
3188 if (SIMPLEQ_FIRST(&s->entries->head) ==
3189 s->first_displayed_entry) {
3190 if (s->tree != s->root)
3191 s->first_displayed_entry = NULL;
3192 s->selected = 0;
3193 break;
3195 tree_scroll_up(&s->first_displayed_entry,
3196 view->nlines, s->entries,
3197 s->tree == s->root);
3198 break;
3199 case 'j':
3200 case KEY_DOWN:
3201 if (s->selected < s->ndisplayed - 1) {
3202 s->selected++;
3203 break;
3205 tree_scroll_down(&s->first_displayed_entry, 1,
3206 s->last_displayed_entry, s->entries);
3207 break;
3208 case KEY_NPAGE:
3209 tree_scroll_down(&s->first_displayed_entry,
3210 view->nlines, s->last_displayed_entry,
3211 s->entries);
3212 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3213 entry))
3214 break;
3215 /* can't scroll any further; move cursor down */
3216 if (s->selected < s->ndisplayed - 1)
3217 s->selected = s->ndisplayed - 1;
3218 break;
3219 case KEY_ENTER:
3220 case '\r':
3221 if (s->selected_entry == NULL) {
3222 struct tog_parent_tree *parent;
3223 case KEY_BACKSPACE:
3224 /* user selected '..' */
3225 if (s->tree == s->root)
3226 break;
3227 parent = TAILQ_FIRST(&s->parents);
3228 TAILQ_REMOVE(&s->parents, parent,
3229 entry);
3230 got_object_tree_close(s->tree);
3231 s->tree = parent->tree;
3232 s->entries =
3233 got_object_tree_get_entries(s->tree);
3234 s->first_displayed_entry =
3235 parent->first_displayed_entry;
3236 s->selected_entry =
3237 parent->selected_entry;
3238 s->selected = parent->selected;
3239 free(parent);
3240 } else if (S_ISDIR(s->selected_entry->mode)) {
3241 struct tog_parent_tree *parent;
3242 struct got_tree_object *child;
3243 err = got_object_open_as_tree(&child,
3244 s->repo, s->selected_entry->id);
3245 if (err)
3246 break;
3247 parent = calloc(1, sizeof(*parent));
3248 if (parent == NULL) {
3249 err = got_error_from_errno();
3250 break;
3252 parent->tree = s->tree;
3253 parent->first_displayed_entry =
3254 s->first_displayed_entry;
3255 parent->selected_entry = s->selected_entry;
3256 parent->selected = s->selected;
3257 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3258 s->tree = child;
3259 s->entries =
3260 got_object_tree_get_entries(s->tree);
3261 s->selected = 0;
3262 s->first_displayed_entry = NULL;
3263 } else if (S_ISREG(s->selected_entry->mode)) {
3264 struct tog_view *blame_view;
3265 int begin_x = view_is_parent_view(view) ?
3266 view_split_begin_x(view->begin_x) : 0;
3268 err = blame_tree_entry(&blame_view, begin_x,
3269 s->selected_entry, &s->parents, s->commit_id,
3270 s->repo);
3271 if (err)
3272 break;
3273 if (view_is_parent_view(view)) {
3274 err = view_close_child(view);
3275 if (err)
3276 return err;
3277 err = view_set_child(view, blame_view);
3278 if (err) {
3279 view_close(blame_view);
3280 break;
3282 *focus_view = blame_view;
3283 view->child_focussed = 1;
3284 } else
3285 *new_view = blame_view;
3287 break;
3288 case KEY_RESIZE:
3289 if (s->selected > view->nlines)
3290 s->selected = s->ndisplayed - 1;
3291 break;
3292 default:
3293 break;
3296 return err;
3299 __dead static void
3300 usage_tree(void)
3302 endwin();
3303 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3304 getprogname());
3305 exit(1);
3308 static const struct got_error *
3309 cmd_tree(int argc, char *argv[])
3311 const struct got_error *error;
3312 struct got_repository *repo = NULL;
3313 char *repo_path = NULL;
3314 struct got_object_id *commit_id = NULL;
3315 char *commit_id_arg = NULL;
3316 struct got_commit_object *commit = NULL;
3317 struct got_tree_object *tree = NULL;
3318 int ch;
3319 struct tog_view *view;
3321 #ifndef PROFILE
3322 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3323 == -1)
3324 err(1, "pledge");
3325 #endif
3327 while ((ch = getopt(argc, argv, "c:")) != -1) {
3328 switch (ch) {
3329 case 'c':
3330 commit_id_arg = optarg;
3331 break;
3332 default:
3333 usage();
3334 /* NOTREACHED */
3338 argc -= optind;
3339 argv += optind;
3341 if (argc == 0) {
3342 repo_path = getcwd(NULL, 0);
3343 if (repo_path == NULL)
3344 return got_error_from_errno();
3345 } else if (argc == 1) {
3346 repo_path = realpath(argv[0], NULL);
3347 if (repo_path == NULL)
3348 return got_error_from_errno();
3349 } else
3350 usage_log();
3352 error = got_repo_open(&repo, repo_path);
3353 free(repo_path);
3354 if (error != NULL)
3355 return error;
3357 if (commit_id_arg == NULL) {
3358 error = get_head_commit_id(&commit_id, repo);
3359 if (error != NULL)
3360 goto done;
3361 } else {
3362 struct got_object *obj;
3363 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3364 if (error == NULL) {
3365 commit_id = got_object_id_dup(got_object_get_id(obj));
3366 if (commit_id == NULL)
3367 error = got_error_from_errno();
3370 if (error != NULL)
3371 goto done;
3373 error = got_object_open_as_commit(&commit, repo, commit_id);
3374 if (error != NULL)
3375 goto done;
3377 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3378 if (error != NULL)
3379 goto done;
3381 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3382 if (view == NULL) {
3383 error = got_error_from_errno();
3384 goto done;
3386 error = open_tree_view(view, tree, commit_id, repo);
3387 if (error)
3388 goto done;
3389 error = view_loop(view);
3390 done:
3391 free(commit_id);
3392 if (commit)
3393 got_object_commit_close(commit);
3394 if (tree)
3395 got_object_tree_close(tree);
3396 if (repo)
3397 got_repo_close(repo);
3398 return error;
3400 static void
3401 init_curses(void)
3403 initscr();
3404 cbreak();
3405 halfdelay(1); /* Do fast refresh while initial view is loading. */
3406 noecho();
3407 nonl();
3408 intrflush(stdscr, FALSE);
3409 keypad(stdscr, TRUE);
3410 curs_set(0);
3413 __dead static void
3414 usage(void)
3416 int i;
3418 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3419 "Available commands:\n", getprogname());
3420 for (i = 0; i < nitems(tog_commands); i++) {
3421 struct tog_cmd *cmd = &tog_commands[i];
3422 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3424 exit(1);
3427 static char **
3428 make_argv(const char *arg0, const char *arg1)
3430 char **argv;
3431 int argc = (arg1 == NULL ? 1 : 2);
3433 argv = calloc(argc, sizeof(char *));
3434 if (argv == NULL)
3435 err(1, "calloc");
3436 argv[0] = strdup(arg0);
3437 if (argv[0] == NULL)
3438 err(1, "calloc");
3439 if (arg1) {
3440 argv[1] = strdup(arg1);
3441 if (argv[1] == NULL)
3442 err(1, "calloc");
3445 return argv;
3448 int
3449 main(int argc, char *argv[])
3451 const struct got_error *error = NULL;
3452 struct tog_cmd *cmd = NULL;
3453 int ch, hflag = 0;
3454 char **cmd_argv = NULL;
3456 setlocale(LC_ALL, "");
3458 while ((ch = getopt(argc, argv, "h")) != -1) {
3459 switch (ch) {
3460 case 'h':
3461 hflag = 1;
3462 break;
3463 default:
3464 usage();
3465 /* NOTREACHED */
3469 argc -= optind;
3470 argv += optind;
3471 optind = 0;
3472 optreset = 1;
3474 if (argc == 0) {
3475 if (hflag)
3476 usage();
3477 /* Build an argument vector which runs a default command. */
3478 cmd = &tog_commands[0];
3479 cmd_argv = make_argv(cmd->name, NULL);
3480 argc = 1;
3481 } else {
3482 int i;
3484 /* Did the user specific a command? */
3485 for (i = 0; i < nitems(tog_commands); i++) {
3486 if (strncmp(tog_commands[i].name, argv[0],
3487 strlen(argv[0])) == 0) {
3488 cmd = &tog_commands[i];
3489 if (hflag)
3490 tog_commands[i].cmd_usage();
3491 break;
3494 if (cmd == NULL) {
3495 /* Did the user specify a repository? */
3496 char *repo_path = realpath(argv[0], NULL);
3497 if (repo_path) {
3498 struct got_repository *repo;
3499 error = got_repo_open(&repo, repo_path);
3500 if (error == NULL)
3501 got_repo_close(repo);
3502 } else
3503 error = got_error_from_errno();
3504 if (error) {
3505 if (hflag) {
3506 fprintf(stderr, "%s: '%s' is not a "
3507 "known command\n", getprogname(),
3508 argv[0]);
3509 usage();
3511 fprintf(stderr, "%s: '%s' is neither a known "
3512 "command nor a path to a repository\n",
3513 getprogname(), argv[0]);
3514 free(repo_path);
3515 return 1;
3517 cmd = &tog_commands[0];
3518 cmd_argv = make_argv(cmd->name, repo_path);
3519 argc = 2;
3520 free(repo_path);
3524 init_curses();
3526 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3527 if (error)
3528 goto done;
3529 done:
3530 endwin();
3531 free(cmd_argv);
3532 if (error)
3533 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3534 return 0;