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 };
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 };
106 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
107 struct commit_queue {
108 int ncommits;
109 struct commit_queue_head head;
110 };
112 struct tog_log_view_state {
113 struct got_commit_graph *graph;
114 struct commit_queue commits;
115 struct commit_queue_entry *first_displayed_entry;
116 struct commit_queue_entry *last_displayed_entry;
117 struct commit_queue_entry *selected_entry;
118 int selected;
119 char *in_repo_path;
120 struct got_repository *repo;
121 struct got_object_id *start_id;
122 };
124 struct tog_blame_cb_args {
125 pthread_mutex_t *mutex;
126 struct tog_blame_line *lines; /* one per line */
127 int nlines;
129 struct tog_view *view;
130 struct got_object_id *commit_id;
131 FILE *f;
132 const char *path;
133 int *first_displayed_line;
134 int *last_displayed_line;
135 int *selected_line;
136 int *quit;
137 int *eof;
138 };
140 struct tog_blame_thread_args {
141 const char *path;
142 struct got_repository *repo;
143 struct tog_blame_cb_args *cb_args;
144 int *complete;
145 };
147 struct tog_blame {
148 FILE *f;
149 size_t filesize;
150 struct tog_blame_line *lines;
151 size_t nlines;
152 pthread_t thread;
153 struct tog_blame_thread_args thread_args;
154 struct tog_blame_cb_args cb_args;
155 const char *path;
156 };
158 struct tog_blame_view_state {
159 int first_displayed_line;
160 int last_displayed_line;
161 int selected_line;
162 int blame_complete;
163 int eof;
164 int done;
165 pthread_mutex_t mutex;
166 struct got_object_id_queue blamed_commits;
167 struct got_object_qid *blamed_commit;
168 char *path;
169 struct got_repository *repo;
170 struct got_object_id *commit_id;
171 struct tog_blame blame;
172 };
174 struct tog_parent_tree {
175 TAILQ_ENTRY(tog_parent_tree) entry;
176 struct got_tree_object *tree;
177 struct got_tree_entry *first_displayed_entry;
178 struct got_tree_entry *selected_entry;
179 int selected;
180 };
182 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
184 struct tog_tree_view_state {
185 char *tree_label;
186 struct got_tree_object *root;
187 struct got_tree_object *tree;
188 const struct got_tree_entries *entries;
189 struct got_tree_entry *first_displayed_entry;
190 struct got_tree_entry *last_displayed_entry;
191 struct got_tree_entry *selected_entry;
192 int nentries, ndisplayed, selected, show_ids;
193 struct tog_parent_trees parents;
194 struct got_object_id *commit_id;
195 struct got_repository *repo;
196 };
198 /*
199 * We implement two types of views: parent views and child views.
201 * The 'Tab' key switches between a parent view and its child view.
202 * Child views are shown side-by-side to their parent view, provided
203 * there is enough screen estate.
205 * When a new view is opened from within a parent view, this new view
206 * becomes a child view of the parent view, replacing any existing child.
208 * When a new view is opened from within a child view, this new view
209 * becomes a parent view which will obscure the views below until the
210 * user quits the new parent view by typing 'q'.
212 * This list of views contains parent views only.
213 * Child views are only pointed to by their parent view.
214 */
215 TAILQ_HEAD(tog_view_list_head, tog_view);
217 struct tog_view {
218 TAILQ_ENTRY(tog_view) entry;
219 WINDOW *window;
220 PANEL *panel;
221 int nlines, ncols, begin_y, begin_x;
222 int lines, cols; /* copies of LINES and COLS */
223 int focussed;
224 struct tog_view *parent;
225 struct tog_view *child;
226 int child_focussed;
228 /* type-specific state */
229 enum tog_view_type type;
230 union {
231 struct tog_diff_view_state diff;
232 struct tog_log_view_state log;
233 struct tog_blame_view_state blame;
234 struct tog_tree_view_state tree;
235 } state;
237 const struct got_error *(*show)(struct tog_view *);
238 const struct got_error *(*input)(struct tog_view **,
239 struct tog_view **, struct tog_view**, struct tog_view *, int);
240 const struct got_error *(*close)(struct tog_view *);
241 };
243 static const struct got_error *open_diff_view(struct tog_view *,
244 struct got_object *, struct got_object *, struct got_repository *);
245 static const struct got_error *show_diff_view(struct tog_view *);
246 static const struct got_error *input_diff_view(struct tog_view **,
247 struct tog_view **, struct tog_view **, struct tog_view *, int);
248 static const struct got_error* close_diff_view(struct tog_view *);
250 static const struct got_error *open_log_view(struct tog_view *,
251 struct got_object_id *, struct got_repository *, const char *);
252 static const struct got_error * show_log_view(struct tog_view *);
253 static const struct got_error *input_log_view(struct tog_view **,
254 struct tog_view **, struct tog_view **, struct tog_view *, int);
255 static const struct got_error *close_log_view(struct tog_view *);
257 static const struct got_error *open_blame_view(struct tog_view *, char *,
258 struct got_object_id *, struct got_repository *);
259 static const struct got_error *show_blame_view(struct tog_view *);
260 static const struct got_error *input_blame_view(struct tog_view **,
261 struct tog_view **, struct tog_view **, struct tog_view *, int);
262 static const struct got_error *close_blame_view(struct tog_view *);
264 static const struct got_error *open_tree_view(struct tog_view *,
265 struct got_tree_object *, struct got_object_id *, struct got_repository *);
266 static const struct got_error *show_tree_view(struct tog_view *);
267 static const struct got_error *input_tree_view(struct tog_view **,
268 struct tog_view **, struct tog_view **, struct tog_view *, int);
269 static const struct got_error *close_tree_view(struct tog_view *);
271 static const struct got_error *
272 view_close(struct tog_view *view)
274 const struct got_error *err = NULL;
276 if (view->child) {
277 view_close(view->child);
278 view->child = NULL;
280 if (view->close)
281 err = view->close(view);
282 if (view->panel)
283 del_panel(view->panel);
284 if (view->window)
285 delwin(view->window);
286 free(view);
287 return err;
290 static struct tog_view *
291 view_open(int nlines, int ncols, int begin_y, int begin_x,
292 enum tog_view_type type)
294 struct tog_view *view = calloc(1, sizeof(*view));
296 if (view == NULL)
297 return NULL;
299 view->type = type;
300 view->lines = LINES;
301 view->cols = COLS;
302 view->nlines = nlines ? nlines : LINES - begin_y;
303 view->ncols = ncols ? ncols : COLS - begin_x;
304 view->begin_y = begin_y;
305 view->begin_x = begin_x;
306 view->window = newwin(nlines, ncols, begin_y, begin_x);
307 if (view->window == NULL) {
308 view_close(view);
309 return NULL;
311 view->panel = new_panel(view->window);
312 if (view->panel == NULL ||
313 set_panel_userptr(view->panel, view) != OK) {
314 view_close(view);
315 return NULL;
318 keypad(view->window, TRUE);
319 return view;
322 static int
323 view_split_begin_x(int begin_x)
325 if (begin_x > 0)
326 return 0;
327 return (COLS >= 120 ? COLS/2 : 0);
330 static const struct got_error *
331 view_resize(struct tog_view *view)
333 int nlines, ncols;
335 if (view->lines > LINES)
336 nlines = view->nlines - (view->lines - LINES);
337 else
338 nlines = view->nlines + (LINES - view->lines);
340 if (view->cols > COLS)
341 ncols = view->ncols - (view->cols - COLS);
342 else
343 ncols = view->ncols + (COLS - view->cols);
345 if (wresize(view->window, nlines, ncols) == ERR)
346 return got_error_from_errno();
347 replace_panel(view->panel, view->window);
349 view->nlines = nlines;
350 view->ncols = ncols;
351 view->lines = LINES;
352 view->cols = COLS;
354 return NULL;
357 static int
358 view_is_parent_view(struct tog_view *view)
360 return view->parent == NULL;
363 static const struct got_error *
364 view_close_child(struct tog_view *view)
366 const struct got_error *err;
368 if (view->child == NULL)
369 return NULL;
371 err = view_close(view->child);
372 view->child = NULL;
373 return err;
376 static const struct got_error *
377 view_set_child(struct tog_view *view, struct tog_view *child)
379 const struct got_error *err = NULL;
381 view->child = child;
382 child->parent = view;
383 return err;
386 static int
387 view_is_splitscreen(struct tog_view *view)
389 return !view_is_parent_view(view) && view->begin_x > 0;
392 static const struct got_error *
393 view_splitscreen(struct tog_view *view)
395 const struct got_error *err = NULL;
397 view->begin_y = 0;
398 view->begin_x = view_split_begin_x(0);
399 view->nlines = LINES;
400 view->ncols = COLS - view->begin_x;
401 view->lines = LINES;
402 view->cols = COLS;
403 err = view_resize(view);
404 if (err)
405 return err;
407 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 return got_error_from_errno();
410 return NULL;
413 static const struct got_error *
414 view_fullscreen(struct tog_view *view)
416 const struct got_error *err = NULL;
418 view->begin_x = 0;
419 view->begin_y = 0;
420 view->nlines = LINES;
421 view->ncols = COLS;
422 view->lines = LINES;
423 view->cols = COLS;
424 err = view_resize(view);
425 if (err)
426 return err;
428 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
429 return got_error_from_errno();
431 return NULL;
434 static const struct got_error *
435 view_input(struct tog_view **new, struct tog_view **dead,
436 struct tog_view **focus, int *done, struct tog_view *view,
437 struct tog_view_list_head *views)
439 const struct got_error *err = NULL;
440 struct tog_view *v;
441 int ch;
443 *new = NULL;
444 *dead = NULL;
445 *focus = NULL;
447 nodelay(stdscr, FALSE);
448 ch = wgetch(view->window);
449 nodelay(stdscr, TRUE);
450 switch (ch) {
451 case ERR:
452 break;
453 case '\t':
454 if (view->child) {
455 *focus = view->child;
456 view->child_focussed = 1;
457 } else if (view->parent) {
458 *focus = view->parent;
459 view->parent->child_focussed = 0;
461 break;
462 case 'q':
463 err = view->input(new, dead, focus, view, ch);
464 *dead = view;
465 break;
466 case 'Q':
467 *done = 1;
468 break;
469 case 'f':
470 if (view_is_parent_view(view)) {
471 if (view->child == NULL)
472 break;
473 if (view_is_splitscreen(view->child)) {
474 *focus = view->child;
475 view->child_focussed = 1;
476 err = view_fullscreen(view->child);
477 } else
478 err = view_splitscreen(view->child);
479 if (err)
480 break;
481 err = view->child->input(new, dead, focus,
482 view->child, KEY_RESIZE);
483 } else {
484 if (view_is_splitscreen(view)) {
485 *focus = view;
486 view->parent->child_focussed = 1;
487 err = view_fullscreen(view);
488 } else {
489 err = view_splitscreen(view);
491 if (err)
492 break;
493 err = view->input(new, dead, focus, view,
494 KEY_RESIZE);
496 break;
497 case KEY_RESIZE:
498 TAILQ_FOREACH(v, views, entry) {
499 err = view_resize(v);
500 if (err)
501 return err;
502 err = v->input(new, dead, focus, v, ch);
504 break;
505 default:
506 err = view->input(new, dead, focus, view, ch);
507 break;
510 return err;
513 void
514 view_vborder(struct tog_view *view)
516 PANEL *panel;
517 struct tog_view *view_above;
519 if (view->parent)
520 return view_vborder(view->parent);
522 panel = panel_above(view->panel);
523 if (panel == NULL)
524 return;
526 view_above = panel_userptr(panel);
527 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
528 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
531 int
532 view_needs_focus_indication(struct tog_view *view)
534 if (view_is_parent_view(view)) {
535 if (view->child == NULL || view->child_focussed)
536 return 0;
537 if (!view_is_splitscreen(view->child))
538 return 0;
539 } else if (!view_is_splitscreen(view))
540 return 0;
542 return view->focussed;
545 static const struct got_error *
546 view_loop(struct tog_view *view)
548 const struct got_error *err = NULL;
549 struct tog_view_list_head views;
550 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
551 int done = 0;
553 TAILQ_INIT(&views);
554 TAILQ_INSERT_HEAD(&views, view, entry);
556 main_view = view;
557 view->focussed = 1;
558 err = view->show(view);
559 if (err)
560 return err;
561 update_panels();
562 doupdate();
563 while (!TAILQ_EMPTY(&views) && !done) {
564 err = view_input(&new_view, &dead_view, &focus_view, &done,
565 view, &views);
566 if (err)
567 break;
568 if (dead_view) {
569 struct tog_view *prev = NULL;
571 if (view_is_parent_view(dead_view))
572 prev = TAILQ_PREV(dead_view,
573 tog_view_list_head, entry);
574 else
575 prev = view->parent;
577 if (dead_view->parent)
578 dead_view->parent->child = NULL;
579 else
580 TAILQ_REMOVE(&views, dead_view, entry);
582 err = view_close(dead_view);
583 if (err || dead_view == main_view)
584 goto done;
586 if (view == dead_view) {
587 if (focus_view)
588 view = focus_view;
589 else if (prev)
590 view = prev;
591 else if (!TAILQ_EMPTY(&views))
592 view = TAILQ_LAST(&views,
593 tog_view_list_head);
594 else
595 view = NULL;
596 if (view) {
597 if (view->child && view->child_focussed)
598 focus_view = view->child;
599 else
600 focus_view = view;
604 if (new_view) {
605 TAILQ_INSERT_TAIL(&views, new_view, entry);
606 if (focus_view == NULL)
607 focus_view = new_view;
609 if (focus_view) {
610 show_panel(focus_view->panel);
611 if (view)
612 view->focussed = 0;
613 focus_view->focussed = 1;
614 view = focus_view;
615 if (new_view)
616 show_panel(new_view->panel);
617 if (view->child && view_is_splitscreen(view->child))
618 show_panel(view->child->panel);
620 if (view) {
621 if (view->parent) {
622 err = view->parent->show(view->parent);
623 if (err)
624 return err;
626 err = view->show(view);
627 if (err)
628 return err;
629 if (view->child) {
630 err = view->child->show(view->child);
631 if (err)
632 return err;
635 update_panels();
636 doupdate();
638 done:
639 while (!TAILQ_EMPTY(&views)) {
640 view = TAILQ_FIRST(&views);
641 TAILQ_REMOVE(&views, view, entry);
642 view_close(view);
644 return err;
647 __dead static void
648 usage_log(void)
650 endwin();
651 fprintf(stderr,
652 "usage: %s log [-c commit] [-r repository-path] [path]\n",
653 getprogname());
654 exit(1);
657 /* Create newly allocated wide-character string equivalent to a byte string. */
658 static const struct got_error *
659 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
661 char *vis = NULL;
662 const struct got_error *err = NULL;
664 *ws = NULL;
665 *wlen = mbstowcs(NULL, s, 0);
666 if (*wlen == (size_t)-1) {
667 int vislen;
668 if (errno != EILSEQ)
669 return got_error_from_errno();
671 /* byte string invalid in current encoding; try to "fix" it */
672 err = got_mbsavis(&vis, &vislen, s);
673 if (err)
674 return err;
675 *wlen = mbstowcs(NULL, vis, 0);
676 if (*wlen == (size_t)-1) {
677 err = got_error_from_errno(); /* give up */
678 goto done;
682 *ws = calloc(*wlen + 1, sizeof(*ws));
683 if (*ws == NULL) {
684 err = got_error_from_errno();
685 goto done;
688 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
689 err = got_error_from_errno();
690 done:
691 free(vis);
692 if (err) {
693 free(*ws);
694 *ws = NULL;
695 *wlen = 0;
697 return err;
700 /* Format a line for display, ensuring that it won't overflow a width limit. */
701 static const struct got_error *
702 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
704 const struct got_error *err = NULL;
705 int cols = 0;
706 wchar_t *wline = NULL;
707 size_t wlen;
708 int i;
710 *wlinep = NULL;
711 *widthp = 0;
713 err = mbs2ws(&wline, &wlen, line);
714 if (err)
715 return err;
717 i = 0;
718 while (i < wlen && cols < wlimit) {
719 int width = wcwidth(wline[i]);
720 switch (width) {
721 case 0:
722 i++;
723 break;
724 case 1:
725 case 2:
726 if (cols + width <= wlimit)
727 cols += width;
728 i++;
729 break;
730 case -1:
731 if (wline[i] == L'\t')
732 cols += TABSIZE - ((cols + 1) % TABSIZE);
733 i++;
734 break;
735 default:
736 err = got_error_from_errno();
737 goto done;
740 wline[i] = L'\0';
741 if (widthp)
742 *widthp = cols;
743 done:
744 if (err)
745 free(wline);
746 else
747 *wlinep = wline;
748 return err;
751 static const struct got_error *
752 draw_commit(struct tog_view *view, struct got_commit_object *commit,
753 struct got_object_id *id)
755 const struct got_error *err = NULL;
756 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
757 char *logmsg0 = NULL, *logmsg = NULL;
758 char *author0 = NULL, *author = NULL;
759 wchar_t *wlogmsg = NULL, *wauthor = NULL;
760 int author_width, logmsg_width;
761 char *newline, *smallerthan;
762 char *line = NULL;
763 int col, limit;
764 static const size_t date_display_cols = 9;
765 static const size_t author_display_cols = 16;
766 const int avail = view->ncols;
768 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
769 &commit->tm_committer) >= sizeof(datebuf))
770 return got_error(GOT_ERR_NO_SPACE);
772 if (avail < date_display_cols)
773 limit = MIN(sizeof(datebuf) - 1, avail);
774 else
775 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
776 waddnstr(view->window, datebuf, limit);
777 col = limit + 1;
778 if (col > avail)
779 goto done;
781 author0 = strdup(commit->author);
782 if (author0 == NULL) {
783 err = got_error_from_errno();
784 goto done;
786 author = author0;
787 smallerthan = strchr(author, '<');
788 if (smallerthan)
789 *smallerthan = '\0';
790 else {
791 char *at = strchr(author, '@');
792 if (at)
793 *at = '\0';
795 limit = avail - col;
796 err = format_line(&wauthor, &author_width, author, limit);
797 if (err)
798 goto done;
799 waddwstr(view->window, wauthor);
800 col += author_width;
801 while (col <= avail && author_width < author_display_cols + 1) {
802 waddch(view->window, ' ');
803 col++;
804 author_width++;
806 if (col > avail)
807 goto done;
809 logmsg0 = strdup(commit->logmsg);
810 if (logmsg0 == NULL) {
811 err = got_error_from_errno();
812 goto done;
814 logmsg = logmsg0;
815 while (*logmsg == '\n')
816 logmsg++;
817 newline = strchr(logmsg, '\n');
818 if (newline)
819 *newline = '\0';
820 limit = avail - col;
821 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
822 if (err)
823 goto done;
824 waddwstr(view->window, wlogmsg);
825 col += logmsg_width;
826 while (col <= avail) {
827 waddch(view->window, ' ');
828 col++;
830 done:
831 free(logmsg0);
832 free(wlogmsg);
833 free(author0);
834 free(wauthor);
835 free(line);
836 return err;
839 static struct commit_queue_entry *
840 alloc_commit_queue_entry(struct got_commit_object *commit,
841 struct got_object_id *id)
843 struct commit_queue_entry *entry;
845 entry = calloc(1, sizeof(*entry));
846 if (entry == NULL)
847 return NULL;
849 entry->id = id;
850 entry->commit = commit;
851 return entry;
854 static void
855 pop_commit(struct commit_queue *commits)
857 struct commit_queue_entry *entry;
859 entry = TAILQ_FIRST(&commits->head);
860 TAILQ_REMOVE(&commits->head, entry, entry);
861 got_object_commit_close(entry->commit);
862 commits->ncommits--;
863 /* Don't free entry->id! It is owned by the commit graph. */
864 free(entry);
867 static void
868 free_commits(struct commit_queue *commits)
870 while (!TAILQ_EMPTY(&commits->head))
871 pop_commit(commits);
874 static const struct got_error *
875 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
876 struct got_object_id *start_id, int minqueue,
877 struct got_repository *repo, const char *path)
879 const struct got_error *err = NULL;
880 int nqueued = 0;
882 if (start_id) {
883 err = got_commit_graph_iter_start(graph, start_id, repo);
884 if (err)
885 return err;
888 while (nqueued < minqueue) {
889 struct got_object_id *id;
890 struct got_commit_object *commit;
891 struct commit_queue_entry *entry;
893 err = got_commit_graph_iter_next(&id, graph);
894 if (err) {
895 if (err->code == GOT_ERR_ITER_COMPLETED) {
896 err = NULL;
897 break;
899 if (err->code != GOT_ERR_ITER_NEED_MORE)
900 break;
901 err = got_commit_graph_fetch_commits(graph,
902 minqueue, repo);
903 if (err)
904 return err;
905 continue;
908 if (id == NULL)
909 break;
911 err = got_object_open_as_commit(&commit, repo, id);
912 if (err)
913 break;
914 entry = alloc_commit_queue_entry(commit, id);
915 if (entry == NULL) {
916 err = got_error_from_errno();
917 break;
920 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
921 nqueued++;
922 commits->ncommits++;
925 return err;
928 static const struct got_error *
929 fetch_next_commit(struct commit_queue_entry **pentry,
930 struct commit_queue_entry *entry, struct commit_queue *commits,
931 struct got_commit_graph *graph, struct got_repository *repo,
932 const char *path)
934 const struct got_error *err = NULL;
936 *pentry = NULL;
938 err = queue_commits(graph, commits, NULL, 1, repo, path);
939 if (err)
940 return err;
942 /* Next entry to display should now be available. */
943 *pentry = TAILQ_NEXT(entry, entry);
944 return NULL;
947 static const struct got_error *
948 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
950 const struct got_error *err = NULL;
951 struct got_reference *head_ref;
953 *head_id = NULL;
955 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
956 if (err)
957 return err;
959 err = got_ref_resolve(head_id, repo, head_ref);
960 got_ref_close(head_ref);
961 if (err) {
962 *head_id = NULL;
963 return err;
966 return NULL;
969 static const struct got_error *
970 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
971 struct commit_queue_entry **selected, struct commit_queue_entry *first,
972 struct commit_queue *commits, int selected_idx, int limit,
973 struct got_commit_graph *graph, struct got_repository *repo,
974 const char *path)
976 const struct got_error *err = NULL;
977 struct commit_queue_entry *entry;
978 int ncommits, width;
979 char *id_str, *header;
980 wchar_t *wline;
982 entry = first;
983 ncommits = 0;
984 while (entry) {
985 if (ncommits == selected_idx) {
986 *selected = entry;
987 break;
989 entry = TAILQ_NEXT(entry, entry);
990 ncommits++;
993 err = got_object_id_str(&id_str, (*selected)->id);
994 if (err)
995 return err;
997 if (path && strcmp(path, "/") != 0) {
998 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
999 err = got_error_from_errno();
1000 free(id_str);
1001 return err;
1003 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
1004 err = got_error_from_errno();
1005 free(id_str);
1006 return err;
1008 free(id_str);
1009 err = format_line(&wline, &width, header, view->ncols);
1010 if (err) {
1011 free(header);
1012 return err;
1014 free(header);
1016 werase(view->window);
1018 if (view_needs_focus_indication(view))
1019 wstandout(view->window);
1020 waddwstr(view->window, wline);
1021 if (view_needs_focus_indication(view))
1022 wstandend(view->window);
1023 if (width < view->ncols)
1024 waddch(view->window, '\n');
1025 free(wline);
1026 if (limit <= 1)
1027 return NULL;
1029 entry = first;
1030 *last = first;
1031 ncommits = 0;
1032 while (entry) {
1033 if (ncommits >= limit - 1)
1034 break;
1035 if (view->focussed && ncommits == selected_idx)
1036 wstandout(view->window);
1037 err = draw_commit(view, entry->commit, entry->id);
1038 if (view->focussed && ncommits == selected_idx)
1039 wstandend(view->window);
1040 if (err)
1041 break;
1042 ncommits++;
1043 *last = entry;
1044 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1045 err = queue_commits(graph, commits, NULL, 1,
1046 repo, path);
1047 if (err) {
1048 if (err->code != GOT_ERR_ITER_COMPLETED)
1049 return err;
1050 err = NULL;
1053 entry = TAILQ_NEXT(entry, entry);
1056 view_vborder(view);
1058 return err;
1061 static void
1062 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1063 struct commit_queue *commits)
1065 struct commit_queue_entry *entry;
1066 int nscrolled = 0;
1068 entry = TAILQ_FIRST(&commits->head);
1069 if (*first_displayed_entry == entry)
1070 return;
1072 entry = *first_displayed_entry;
1073 while (entry && nscrolled < maxscroll) {
1074 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1075 if (entry) {
1076 *first_displayed_entry = entry;
1077 nscrolled++;
1082 static const struct got_error *
1083 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1084 struct commit_queue_entry **last_displayed_entry,
1085 struct commit_queue *commits, struct got_commit_graph *graph,
1086 struct got_repository *repo, const char *path)
1088 const struct got_error *err = NULL;
1089 struct commit_queue_entry *pentry;
1090 int nscrolled = 0;
1092 do {
1093 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1094 if (pentry == NULL) {
1095 err = fetch_next_commit(&pentry, *last_displayed_entry,
1096 commits, graph, repo, path);
1097 if (err || pentry == NULL)
1098 break;
1100 *last_displayed_entry = pentry;
1102 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1103 if (pentry == NULL)
1104 break;
1105 *first_displayed_entry = pentry;
1106 } while (++nscrolled < maxscroll);
1108 return err;
1111 static const struct got_error *
1112 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1113 struct got_object_id *commit_id, struct got_commit_object *commit,
1114 struct got_repository *repo)
1116 const struct got_error *err;
1117 struct got_object *obj1 = NULL, *obj2 = NULL;
1118 struct got_object_qid *parent_id;
1119 struct tog_view *diff_view;
1121 err = got_object_open(&obj2, repo, commit_id);
1122 if (err)
1123 return err;
1125 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1126 if (parent_id) {
1127 err = got_object_open(&obj1, repo, parent_id->id);
1128 if (err)
1129 goto done;
1132 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1133 if (diff_view == NULL) {
1134 err = got_error_from_errno();
1135 goto done;
1138 err = open_diff_view(diff_view, obj1, obj2, repo);
1139 if (err == NULL)
1140 *new_view = diff_view;
1141 done:
1142 if (obj1)
1143 got_object_close(obj1);
1144 if (obj2)
1145 got_object_close(obj2);
1146 return err;
1149 static const struct got_error *
1150 browse_commit(struct tog_view **new_view, int begin_x,
1151 struct commit_queue_entry *entry, struct got_repository *repo)
1153 const struct got_error *err = NULL;
1154 struct got_tree_object *tree;
1155 struct tog_view *tree_view;
1157 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1158 if (err)
1159 return err;
1161 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1162 if (tree_view == NULL)
1163 return got_error_from_errno();
1165 err = open_tree_view(tree_view, tree, entry->id, repo);
1166 if (err)
1167 got_object_tree_close(tree);
1168 else
1169 *new_view = tree_view;
1170 return err;
1173 static const struct got_error *
1174 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1175 struct got_repository *repo, const char *path)
1177 const struct got_error *err = NULL;
1178 struct tog_log_view_state *s = &view->state.log;
1180 err = got_repo_map_path(&s->in_repo_path, repo, path);
1181 if (err != NULL)
1182 goto done;
1184 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1185 0, repo);
1186 if (err)
1187 goto done;
1188 /* The commit queue only contains commits being displayed. */
1189 TAILQ_INIT(&s->commits.head);
1190 s->commits.ncommits = 0;
1193 * Open the initial batch of commits, sorted in commit graph order.
1194 * We keep all commits open throughout the lifetime of the log view
1195 * in order to avoid having to re-fetch commits from disk while
1196 * updating the display.
1198 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1199 repo, s->in_repo_path);
1200 if (err) {
1201 if (err->code != GOT_ERR_ITER_COMPLETED)
1202 goto done;
1203 err = NULL;
1206 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1207 s->selected_entry = s->first_displayed_entry;
1208 s->repo = repo;
1209 s->start_id = got_object_id_dup(start_id);
1210 if (s->start_id == NULL) {
1211 err = got_error_from_errno();
1212 goto done;
1215 view->show = show_log_view;
1216 view->input = input_log_view;
1217 view->close = close_log_view;
1218 done:
1219 return err;
1222 static const struct got_error *
1223 close_log_view(struct tog_view *view)
1225 struct tog_log_view_state *s = &view->state.log;
1227 if (s->graph)
1228 got_commit_graph_close(s->graph);
1229 free_commits(&s->commits);
1230 free(s->in_repo_path);
1231 free(s->start_id);
1232 return NULL;
1235 static const struct got_error *
1236 show_log_view(struct tog_view *view)
1238 const struct got_error *err = NULL;
1239 struct tog_log_view_state *s = &view->state.log;
1241 return draw_commits(view, &s->last_displayed_entry,
1242 &s->selected_entry, s->first_displayed_entry,
1243 &s->commits, s->selected, view->nlines, s->graph,
1244 s->repo, s->in_repo_path);
1245 if (err)
1246 return err;
1249 static const struct got_error *
1250 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1251 struct tog_view **focus_view, struct tog_view *view, int ch)
1253 const struct got_error *err = NULL;
1254 struct tog_log_view_state *s = &view->state.log;
1255 char *parent_path;
1256 struct tog_view *diff_view = NULL, *tree_view = NULL;
1257 int begin_x = 0;
1259 switch (ch) {
1260 case 'k':
1261 case KEY_UP:
1262 case '[':
1263 if (s->selected > 0)
1264 s->selected--;
1265 if (s->selected > 0)
1266 break;
1267 scroll_up(&s->first_displayed_entry, 1,
1268 &s->commits);
1269 break;
1270 case KEY_PPAGE:
1271 if (TAILQ_FIRST(&s->commits.head) ==
1272 s->first_displayed_entry) {
1273 s->selected = 0;
1274 break;
1276 scroll_up(&s->first_displayed_entry,
1277 view->nlines, &s->commits);
1278 break;
1279 case 'j':
1280 case KEY_DOWN:
1281 case ']':
1282 if (s->selected < MIN(view->nlines - 2,
1283 s->commits.ncommits - 1)) {
1284 s->selected++;
1285 break;
1287 err = scroll_down(&s->first_displayed_entry, 1,
1288 &s->last_displayed_entry, &s->commits,
1289 s->graph, s->repo, s->in_repo_path);
1290 if (err) {
1291 if (err->code != GOT_ERR_ITER_COMPLETED)
1292 break;
1293 err = NULL;
1295 break;
1296 case KEY_NPAGE: {
1297 struct commit_queue_entry *first;
1298 first = s->first_displayed_entry;
1299 err = scroll_down(&s->first_displayed_entry,
1300 view->nlines, &s->last_displayed_entry,
1301 &s->commits, s->graph, s->repo,
1302 s->in_repo_path);
1303 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1304 break;
1305 if (first == s->first_displayed_entry &&
1306 s->selected < MIN(view->nlines - 2,
1307 s->commits.ncommits - 1)) {
1308 /* can't scroll further down */
1309 s->selected = MIN(view->nlines - 2,
1310 s->commits.ncommits - 1);
1312 err = NULL;
1313 break;
1315 case KEY_RESIZE:
1316 if (s->selected > view->nlines - 2)
1317 s->selected = view->nlines - 2;
1318 if (s->selected > s->commits.ncommits - 1)
1319 s->selected = s->commits.ncommits - 1;
1320 break;
1321 case KEY_ENTER:
1322 case '\r':
1323 if (view_is_parent_view(view))
1324 begin_x = view_split_begin_x(view->begin_x);
1325 err = open_diff_view_for_commit(&diff_view, begin_x,
1326 s->selected_entry->id, s->selected_entry->commit,
1327 s->repo);
1328 if (err)
1329 break;
1330 if (view_is_parent_view(view)) {
1331 err = view_close_child(view);
1332 if (err)
1333 return err;
1334 err = view_set_child(view, diff_view);
1335 if (err) {
1336 view_close(diff_view);
1337 break;
1339 if (!view_is_splitscreen(diff_view)) {
1340 *focus_view = diff_view;
1341 view->child_focussed = 1;
1343 } else
1344 *new_view = diff_view;
1345 break;
1346 case 't':
1347 if (view_is_parent_view(view))
1348 begin_x = view_split_begin_x(view->begin_x);
1349 err = browse_commit(&tree_view, begin_x,
1350 s->selected_entry, s->repo);
1351 if (view_is_parent_view(view)) {
1352 err = view_close_child(view);
1353 if (err)
1354 return err;
1355 err = view_set_child(view, tree_view);
1356 if (err) {
1357 view_close(tree_view);
1358 break;
1360 *focus_view = tree_view;
1361 view->child_focussed = 1;
1362 } else
1363 *new_view = tree_view;
1364 break;
1365 case KEY_BACKSPACE:
1366 if (strcmp(s->in_repo_path, "/") == 0)
1367 break;
1368 parent_path = dirname(s->in_repo_path);
1369 if (parent_path && strcmp(parent_path, ".") != 0) {
1370 struct tog_view *lv;
1371 lv = view_open(view->nlines, view->ncols,
1372 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1373 if (lv == NULL)
1374 return got_error_from_errno();
1375 err = open_log_view(lv, s->start_id, s->repo,
1376 parent_path);
1377 if (err)
1378 break;
1379 if (view_is_parent_view(view))
1380 *new_view = lv;
1381 else {
1382 view_set_child(view->parent, lv);
1383 *dead_view = view;
1386 break;
1387 default:
1388 break;
1391 return err;
1394 static const struct got_error *
1395 cmd_log(int argc, char *argv[])
1397 const struct got_error *error;
1398 struct got_repository *repo = NULL;
1399 struct got_object_id *start_id = NULL;
1400 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1401 char *start_commit = NULL;
1402 int ch;
1403 struct tog_view *view;
1405 #ifndef PROFILE
1406 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1407 == -1)
1408 err(1, "pledge");
1409 #endif
1411 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1412 switch (ch) {
1413 case 'c':
1414 start_commit = optarg;
1415 break;
1416 case 'r':
1417 repo_path = realpath(optarg, NULL);
1418 if (repo_path == NULL)
1419 err(1, "-r option");
1420 break;
1421 default:
1422 usage();
1423 /* NOTREACHED */
1427 argc -= optind;
1428 argv += optind;
1430 if (argc == 0)
1431 path = strdup("");
1432 else if (argc == 1)
1433 path = strdup(argv[0]);
1434 else
1435 usage_log();
1436 if (path == NULL)
1437 return got_error_from_errno();
1439 cwd = getcwd(NULL, 0);
1440 if (cwd == NULL) {
1441 error = got_error_from_errno();
1442 goto done;
1444 if (repo_path == NULL) {
1445 repo_path = strdup(cwd);
1446 if (repo_path == NULL) {
1447 error = got_error_from_errno();
1448 goto done;
1452 error = got_repo_open(&repo, repo_path);
1453 if (error != NULL)
1454 goto done;
1456 if (start_commit == NULL) {
1457 error = get_head_commit_id(&start_id, repo);
1458 if (error != NULL)
1459 goto done;
1460 } else {
1461 struct got_object *obj;
1462 error = got_object_open_by_id_str(&obj, repo, start_commit);
1463 if (error == NULL) {
1464 start_id = got_object_id_dup(got_object_get_id(obj));
1465 if (start_id == NULL)
1466 error = got_error_from_errno();
1467 goto done;
1470 if (error != NULL)
1471 goto done;
1473 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1474 if (view == NULL) {
1475 error = got_error_from_errno();
1476 goto done;
1478 error = open_log_view(view, start_id, repo, path);
1479 if (error)
1480 goto done;
1481 error = view_loop(view);
1482 done:
1483 free(repo_path);
1484 free(cwd);
1485 free(path);
1486 free(start_id);
1487 if (repo)
1488 got_repo_close(repo);
1489 return error;
1492 __dead static void
1493 usage_diff(void)
1495 endwin();
1496 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1497 getprogname());
1498 exit(1);
1501 static char *
1502 parse_next_line(FILE *f, size_t *len)
1504 char *line;
1505 size_t linelen;
1506 size_t lineno;
1507 const char delim[3] = { '\0', '\0', '\0'};
1509 line = fparseln(f, &linelen, &lineno, delim, 0);
1510 if (len)
1511 *len = linelen;
1512 return line;
1515 static const struct got_error *
1516 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1517 int *last_displayed_line, int *eof, int max_lines,
1518 char * header)
1520 const struct got_error *err;
1521 int nlines = 0, nprinted = 0;
1522 char *line;
1523 size_t len;
1524 wchar_t *wline;
1525 int width;
1527 rewind(f);
1528 werase(view->window);
1530 if (header) {
1531 err = format_line(&wline, &width, header, view->ncols);
1532 if (err) {
1533 return err;
1536 if (view_needs_focus_indication(view))
1537 wstandout(view->window);
1538 waddwstr(view->window, wline);
1539 if (view_needs_focus_indication(view))
1540 wstandend(view->window);
1541 if (width < view->ncols)
1542 waddch(view->window, '\n');
1544 if (max_lines <= 1)
1545 return NULL;
1546 max_lines--;
1549 *eof = 0;
1550 while (nprinted < max_lines) {
1551 line = parse_next_line(f, &len);
1552 if (line == NULL) {
1553 *eof = 1;
1554 break;
1556 if (++nlines < *first_displayed_line) {
1557 free(line);
1558 continue;
1561 err = format_line(&wline, &width, line, view->ncols);
1562 if (err) {
1563 free(line);
1564 return err;
1566 waddwstr(view->window, wline);
1567 if (width < view->ncols)
1568 waddch(view->window, '\n');
1569 if (++nprinted == 1)
1570 *first_displayed_line = nlines;
1571 free(line);
1572 free(wline);
1573 wline = NULL;
1575 *last_displayed_line = nlines;
1577 view_vborder(view);
1579 return NULL;
1582 static const struct got_error *
1583 open_diff_view(struct tog_view *view, struct got_object *obj1,
1584 struct got_object *obj2, struct got_repository *repo)
1586 const struct got_error *err;
1587 FILE *f;
1589 if (obj1 != NULL && obj2 != NULL &&
1590 got_object_get_type(obj1) != got_object_get_type(obj2))
1591 return got_error(GOT_ERR_OBJ_TYPE);
1593 f = got_opentemp();
1594 if (f == NULL)
1595 return got_error_from_errno();
1597 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1598 case GOT_OBJ_TYPE_BLOB:
1599 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1600 repo, f);
1601 break;
1602 case GOT_OBJ_TYPE_TREE:
1603 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1604 break;
1605 case GOT_OBJ_TYPE_COMMIT:
1606 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1607 break;
1608 default:
1609 return got_error(GOT_ERR_OBJ_TYPE);
1612 fflush(f);
1614 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1615 view->state.diff.id2 = got_object_get_id(obj2);
1616 view->state.diff.f = f;
1617 view->state.diff.first_displayed_line = 1;
1618 view->state.diff.last_displayed_line = view->nlines;
1620 view->show = show_diff_view;
1621 view->input = input_diff_view;
1622 view->close = close_diff_view;
1624 return NULL;
1627 static const struct got_error *
1628 close_diff_view(struct tog_view *view)
1630 const struct got_error *err = NULL;
1632 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1633 err = got_error_from_errno();
1634 return err;
1637 static const struct got_error *
1638 show_diff_view(struct tog_view *view)
1640 const struct got_error *err;
1641 struct tog_diff_view_state *s = &view->state.diff;
1642 char *id_str1 = NULL, *id_str2, *header;
1644 if (s->id1) {
1645 err = got_object_id_str(&id_str1, s->id1);
1646 if (err)
1647 return err;
1649 err = got_object_id_str(&id_str2, s->id2);
1650 if (err)
1651 return err;
1653 if (asprintf(&header, "diff: %s %s",
1654 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1655 err = got_error_from_errno();
1656 free(id_str1);
1657 free(id_str2);
1658 return err;
1660 free(id_str1);
1661 free(id_str2);
1663 return draw_file(view, s->f, &s->first_displayed_line,
1664 &s->last_displayed_line, &s->eof, view->nlines,
1665 header);
1668 static const struct got_error *
1669 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1670 struct tog_view **focus_view, struct tog_view *view, int ch)
1672 const struct got_error *err = NULL;
1673 struct tog_diff_view_state *s = &view->state.diff;
1674 int i;
1676 switch (ch) {
1677 case 'k':
1678 case KEY_UP:
1679 if (s->first_displayed_line > 1)
1680 s->first_displayed_line--;
1681 break;
1682 case KEY_PPAGE:
1683 i = 0;
1684 while (i++ < view->nlines - 1 &&
1685 s->first_displayed_line > 1)
1686 s->first_displayed_line--;
1687 break;
1688 case 'j':
1689 case KEY_DOWN:
1690 if (!s->eof)
1691 s->first_displayed_line++;
1692 break;
1693 case KEY_NPAGE:
1694 case ' ':
1695 i = 0;
1696 while (!s->eof && i++ < view->nlines - 1) {
1697 char *line;
1698 line = parse_next_line(s->f, NULL);
1699 s->first_displayed_line++;
1700 if (line == NULL)
1701 break;
1703 break;
1704 default:
1705 break;
1708 return err;
1711 static const struct got_error *
1712 cmd_diff(int argc, char *argv[])
1714 const struct got_error *error = NULL;
1715 struct got_repository *repo = NULL;
1716 struct got_object *obj1 = NULL, *obj2 = NULL;
1717 char *repo_path = NULL;
1718 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1719 int ch;
1720 struct tog_view *view;
1722 #ifndef PROFILE
1723 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1724 == -1)
1725 err(1, "pledge");
1726 #endif
1728 while ((ch = getopt(argc, argv, "")) != -1) {
1729 switch (ch) {
1730 default:
1731 usage();
1732 /* NOTREACHED */
1736 argc -= optind;
1737 argv += optind;
1739 if (argc == 0) {
1740 usage_diff(); /* TODO show local worktree changes */
1741 } else if (argc == 2) {
1742 repo_path = getcwd(NULL, 0);
1743 if (repo_path == NULL)
1744 return got_error_from_errno();
1745 obj_id_str1 = argv[0];
1746 obj_id_str2 = argv[1];
1747 } else if (argc == 3) {
1748 repo_path = realpath(argv[0], NULL);
1749 if (repo_path == NULL)
1750 return got_error_from_errno();
1751 obj_id_str1 = argv[1];
1752 obj_id_str2 = argv[2];
1753 } else
1754 usage_diff();
1756 error = got_repo_open(&repo, repo_path);
1757 free(repo_path);
1758 if (error)
1759 goto done;
1761 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1762 if (error)
1763 goto done;
1765 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1766 if (error)
1767 goto done;
1769 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1770 if (view == NULL) {
1771 error = got_error_from_errno();
1772 goto done;
1774 error = open_diff_view(view, obj1, obj2, repo);
1775 if (error)
1776 goto done;
1777 error = view_loop(view);
1778 done:
1779 got_repo_close(repo);
1780 if (obj1)
1781 got_object_close(obj1);
1782 if (obj2)
1783 got_object_close(obj2);
1784 return error;
1787 __dead static void
1788 usage_blame(void)
1790 endwin();
1791 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1792 getprogname());
1793 exit(1);
1796 struct tog_blame_line {
1797 int annotated;
1798 struct got_object_id *id;
1801 static const struct got_error *
1802 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1803 const char *path, struct tog_blame_line *lines, int nlines,
1804 int blame_complete, int selected_line, int *first_displayed_line,
1805 int *last_displayed_line, int *eof, int max_lines)
1807 const struct got_error *err;
1808 int lineno = 0, nprinted = 0;
1809 char *line;
1810 size_t len;
1811 wchar_t *wline;
1812 int width, wlimit;
1813 struct tog_blame_line *blame_line;
1814 struct got_object_id *prev_id = NULL;
1815 char *id_str;
1817 err = got_object_id_str(&id_str, id);
1818 if (err)
1819 return err;
1821 rewind(f);
1822 werase(view->window);
1824 if (asprintf(&line, "commit: %s", id_str) == -1) {
1825 err = got_error_from_errno();
1826 free(id_str);
1827 return err;
1830 err = format_line(&wline, &width, line, view->ncols);
1831 free(line);
1832 line = NULL;
1833 if (view_needs_focus_indication(view))
1834 wstandout(view->window);
1835 waddwstr(view->window, wline);
1836 if (view_needs_focus_indication(view))
1837 wstandend(view->window);
1838 free(wline);
1839 wline = NULL;
1840 if (width < view->ncols)
1841 waddch(view->window, '\n');
1843 if (asprintf(&line, "[%d/%d] %s%s",
1844 *first_displayed_line - 1 + selected_line, nlines,
1845 blame_complete ? "" : "annotating ", path) == -1) {
1846 free(id_str);
1847 return got_error_from_errno();
1849 free(id_str);
1850 err = format_line(&wline, &width, line, view->ncols);
1851 free(line);
1852 line = NULL;
1853 if (err)
1854 return err;
1855 waddwstr(view->window, wline);
1856 free(wline);
1857 wline = NULL;
1858 if (width < view->ncols)
1859 waddch(view->window, '\n');
1861 *eof = 0;
1862 while (nprinted < max_lines - 2) {
1863 line = parse_next_line(f, &len);
1864 if (line == NULL) {
1865 *eof = 1;
1866 break;
1868 if (++lineno < *first_displayed_line) {
1869 free(line);
1870 continue;
1873 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1874 err = format_line(&wline, &width, line, wlimit);
1875 if (err) {
1876 free(line);
1877 return err;
1880 if (view->focussed && nprinted == selected_line - 1)
1881 wstandout(view->window);
1883 blame_line = &lines[lineno - 1];
1884 if (blame_line->annotated && prev_id &&
1885 got_object_id_cmp(prev_id, blame_line->id) == 0)
1886 waddstr(view->window, " ");
1887 else if (blame_line->annotated) {
1888 char *id_str;
1889 err = got_object_id_str(&id_str, blame_line->id);
1890 if (err) {
1891 free(line);
1892 free(wline);
1893 return err;
1895 wprintw(view->window, "%.8s ", id_str);
1896 free(id_str);
1897 prev_id = blame_line->id;
1898 } else {
1899 waddstr(view->window, "........ ");
1900 prev_id = NULL;
1903 waddwstr(view->window, wline);
1904 while (width < wlimit) {
1905 waddch(view->window, ' ');
1906 width++;
1908 if (view->focussed && nprinted == selected_line - 1)
1909 wstandend(view->window);
1910 if (++nprinted == 1)
1911 *first_displayed_line = lineno;
1912 free(line);
1913 free(wline);
1914 wline = NULL;
1916 *last_displayed_line = lineno;
1918 view_vborder(view);
1920 return NULL;
1923 static const struct got_error *
1924 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1926 const struct got_error *err = NULL;
1927 struct tog_blame_cb_args *a = arg;
1928 struct tog_blame_line *line;
1930 if (nlines != a->nlines ||
1931 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1932 return got_error(GOT_ERR_RANGE);
1934 if (pthread_mutex_lock(a->mutex) != 0)
1935 return got_error_from_errno();
1937 if (*a->quit) { /* user has quit the blame view */
1938 err = got_error(GOT_ERR_ITER_COMPLETED);
1939 goto done;
1942 if (lineno == -1)
1943 goto done; /* no change in this commit */
1945 line = &a->lines[lineno - 1];
1946 if (line->annotated)
1947 goto done;
1949 line->id = got_object_id_dup(id);
1950 if (line->id == NULL) {
1951 err = got_error_from_errno();
1952 goto done;
1954 line->annotated = 1;
1956 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1957 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1958 a->last_displayed_line, a->eof, a->view->nlines);
1959 done:
1960 if (pthread_mutex_unlock(a->mutex) != 0)
1961 return got_error_from_errno();
1962 return err;
1965 static void *
1966 blame_thread(void *arg)
1968 const struct got_error *err;
1969 struct tog_blame_thread_args *ta = arg;
1970 struct tog_blame_cb_args *a = ta->cb_args;
1972 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1973 blame_cb, ta->cb_args);
1975 if (pthread_mutex_lock(a->mutex) != 0)
1976 return (void *)got_error_from_errno();
1978 got_repo_close(ta->repo);
1979 ta->repo = NULL;
1980 *ta->complete = 1;
1981 if (!err)
1982 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1983 a->lines, a->nlines, 1, *a->selected_line,
1984 a->first_displayed_line, a->last_displayed_line, a->eof,
1985 a->view->nlines);
1987 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1988 err = got_error_from_errno();
1990 return (void *)err;
1993 static struct got_object_id *
1994 get_selected_commit_id(struct tog_blame_line *lines,
1995 int first_displayed_line, int selected_line)
1997 struct tog_blame_line *line;
1999 line = &lines[first_displayed_line - 1 + selected_line - 1];
2000 if (!line->annotated)
2001 return NULL;
2003 return line->id;
2006 static const struct got_error *
2007 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2008 struct tog_blame_line *lines, int first_displayed_line,
2009 int selected_line, struct got_repository *repo)
2011 const struct got_error *err = NULL;
2012 struct got_commit_object *commit = NULL;
2013 struct got_object_id *selected_id;
2014 struct got_object_qid *pid;
2016 *pobj = NULL;
2017 *obj = NULL;
2019 selected_id = get_selected_commit_id(lines,
2020 first_displayed_line, selected_line);
2021 if (selected_id == NULL)
2022 return NULL;
2024 err = got_object_open(obj, repo, selected_id);
2025 if (err)
2026 goto done;
2028 err = got_object_commit_open(&commit, repo, *obj);
2029 if (err)
2030 goto done;
2032 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2033 if (pid) {
2034 err = got_object_open(pobj, repo, pid->id);
2035 if (err)
2036 goto done;
2038 done:
2039 if (commit)
2040 got_object_commit_close(commit);
2041 return err;
2044 static const struct got_error *
2045 stop_blame(struct tog_blame *blame)
2047 const struct got_error *err = NULL;
2048 int i;
2050 if (blame->thread) {
2051 if (pthread_join(blame->thread, (void **)&err) != 0)
2052 err = got_error_from_errno();
2053 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2054 err = NULL;
2055 blame->thread = NULL;
2057 if (blame->thread_args.repo) {
2058 got_repo_close(blame->thread_args.repo);
2059 blame->thread_args.repo = NULL;
2061 if (blame->f) {
2062 fclose(blame->f);
2063 blame->f = NULL;
2065 for (i = 0; i < blame->nlines; i++)
2066 free(blame->lines[i].id);
2067 free(blame->lines);
2068 blame->lines = NULL;
2069 free(blame->cb_args.commit_id);
2070 blame->cb_args.commit_id = NULL;
2072 return err;
2075 static const struct got_error *
2076 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2077 struct tog_view *view, int *blame_complete,
2078 int *first_displayed_line, int *last_displayed_line,
2079 int *selected_line, int *done, int *eof, const char *path,
2080 struct got_object_id *commit_id,
2081 struct got_repository *repo)
2083 const struct got_error *err = NULL;
2084 struct got_blob_object *blob = NULL;
2085 struct got_repository *thread_repo = NULL;
2086 struct got_object_id *obj_id = NULL;
2087 struct got_object *obj = NULL;
2089 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2090 if (err)
2091 goto done;
2093 err = got_object_open(&obj, repo, obj_id);
2094 if (err)
2095 goto done;
2097 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2098 err = got_error(GOT_ERR_OBJ_TYPE);
2099 goto done;
2102 err = got_object_blob_open(&blob, repo, obj, 8192);
2103 if (err)
2104 goto done;
2105 blame->f = got_opentemp();
2106 if (blame->f == NULL) {
2107 err = got_error_from_errno();
2108 goto done;
2110 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2111 blame->f, blob);
2112 if (err)
2113 goto done;
2115 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2116 if (blame->lines == NULL) {
2117 err = got_error_from_errno();
2118 goto done;
2121 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2122 if (err)
2123 goto done;
2125 blame->cb_args.view = view;
2126 blame->cb_args.lines = blame->lines;
2127 blame->cb_args.nlines = blame->nlines;
2128 blame->cb_args.mutex = mutex;
2129 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2130 if (blame->cb_args.commit_id == NULL) {
2131 err = got_error_from_errno();
2132 goto done;
2134 blame->cb_args.f = blame->f;
2135 blame->cb_args.path = path;
2136 blame->cb_args.first_displayed_line = first_displayed_line;
2137 blame->cb_args.selected_line = selected_line;
2138 blame->cb_args.last_displayed_line = last_displayed_line;
2139 blame->cb_args.quit = done;
2140 blame->cb_args.eof = eof;
2142 blame->thread_args.path = path;
2143 blame->thread_args.repo = thread_repo;
2144 blame->thread_args.cb_args = &blame->cb_args;
2145 blame->thread_args.complete = blame_complete;
2146 *blame_complete = 0;
2148 if (pthread_create(&blame->thread, NULL, blame_thread,
2149 &blame->thread_args) != 0) {
2150 err = got_error_from_errno();
2151 goto done;
2154 done:
2155 if (blob)
2156 got_object_blob_close(blob);
2157 free(obj_id);
2158 if (obj)
2159 got_object_close(obj);
2160 if (err)
2161 stop_blame(blame);
2162 return err;
2165 static const struct got_error *
2166 open_blame_view(struct tog_view *view, char *path,
2167 struct got_object_id *commit_id, struct got_repository *repo)
2169 const struct got_error *err = NULL;
2170 struct tog_blame_view_state *s = &view->state.blame;
2172 SIMPLEQ_INIT(&s->blamed_commits);
2174 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2175 return got_error_from_errno();
2177 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2178 if (err)
2179 return err;
2181 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2182 s->first_displayed_line = 1;
2183 s->last_displayed_line = view->nlines;
2184 s->selected_line = 1;
2185 s->blame_complete = 0;
2186 s->path = path;
2187 if (s->path == NULL)
2188 return got_error_from_errno();
2189 s->repo = repo;
2190 s->commit_id = commit_id;
2191 memset(&s->blame, 0, sizeof(s->blame));
2193 view->show = show_blame_view;
2194 view->input = input_blame_view;
2195 view->close = close_blame_view;
2197 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2198 &s->first_displayed_line, &s->last_displayed_line,
2199 &s->selected_line, &s->done, &s->eof, s->path,
2200 s->blamed_commit->id, s->repo);
2203 static const struct got_error *
2204 close_blame_view(struct tog_view *view)
2206 const struct got_error *err = NULL;
2207 struct tog_blame_view_state *s = &view->state.blame;
2209 if (s->blame.thread)
2210 err = stop_blame(&s->blame);
2212 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2213 struct got_object_qid *blamed_commit;
2214 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2215 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2216 got_object_qid_free(blamed_commit);
2219 free(s->path);
2221 return err;
2224 static const struct got_error *
2225 show_blame_view(struct tog_view *view)
2227 const struct got_error *err = NULL;
2228 struct tog_blame_view_state *s = &view->state.blame;
2230 if (pthread_mutex_lock(&s->mutex) != 0)
2231 return got_error_from_errno();
2233 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2234 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2235 s->selected_line, &s->first_displayed_line,
2236 &s->last_displayed_line, &s->eof, view->nlines);
2238 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2239 err = got_error_from_errno();
2241 view_vborder(view);
2242 return err;
2245 static const struct got_error *
2246 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2247 struct tog_view **focus_view, struct tog_view *view, int ch)
2249 const struct got_error *err = NULL, *thread_err = NULL;
2250 struct got_object *obj = NULL, *pobj = NULL;
2251 struct tog_view *diff_view;
2252 struct tog_blame_view_state *s = &view->state.blame;
2253 int begin_x = 0;
2255 if (pthread_mutex_lock(&s->mutex) != 0) {
2256 err = got_error_from_errno();
2257 goto done;
2260 switch (ch) {
2261 case 'q':
2262 s->done = 1;
2263 if (pthread_mutex_unlock(&s->mutex) != 0) {
2264 err = got_error_from_errno();
2265 goto done;
2267 return stop_blame(&s->blame);
2268 case 'k':
2269 case KEY_UP:
2270 if (s->selected_line > 1)
2271 s->selected_line--;
2272 else if (s->selected_line == 1 &&
2273 s->first_displayed_line > 1)
2274 s->first_displayed_line--;
2275 break;
2276 case KEY_PPAGE:
2277 if (s->first_displayed_line == 1) {
2278 s->selected_line = 1;
2279 break;
2281 if (s->first_displayed_line > view->nlines - 2)
2282 s->first_displayed_line -=
2283 (view->nlines - 2);
2284 else
2285 s->first_displayed_line = 1;
2286 break;
2287 case 'j':
2288 case KEY_DOWN:
2289 if (s->selected_line < view->nlines - 2 &&
2290 s->first_displayed_line +
2291 s->selected_line <= s->blame.nlines)
2292 s->selected_line++;
2293 else if (s->last_displayed_line <
2294 s->blame.nlines)
2295 s->first_displayed_line++;
2296 break;
2297 case 'b':
2298 case 'p': {
2299 struct got_object_id *id;
2300 id = get_selected_commit_id(s->blame.lines,
2301 s->first_displayed_line, s->selected_line);
2302 if (id == NULL || got_object_id_cmp(id,
2303 s->blamed_commit->id) == 0)
2304 break;
2305 err = open_selected_commit(&pobj, &obj,
2306 s->blame.lines, s->first_displayed_line,
2307 s->selected_line, s->repo);
2308 if (err)
2309 break;
2310 if (pobj == NULL && obj == NULL)
2311 break;
2312 if (ch == 'p' && pobj == NULL)
2313 break;
2314 s->done = 1;
2315 if (pthread_mutex_unlock(&s->mutex) != 0) {
2316 err = got_error_from_errno();
2317 goto done;
2319 thread_err = stop_blame(&s->blame);
2320 s->done = 0;
2321 if (pthread_mutex_lock(&s->mutex) != 0) {
2322 err = got_error_from_errno();
2323 goto done;
2325 if (thread_err)
2326 break;
2327 id = got_object_get_id(ch == 'b' ? obj : pobj);
2328 got_object_close(obj);
2329 obj = NULL;
2330 if (pobj) {
2331 got_object_close(pobj);
2332 pobj = NULL;
2334 err = got_object_qid_alloc(&s->blamed_commit, id);
2335 if (err)
2336 goto done;
2337 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2338 s->blamed_commit, entry);
2339 err = run_blame(&s->blame, &s->mutex, view,
2340 &s->blame_complete,
2341 &s->first_displayed_line,
2342 &s->last_displayed_line,
2343 &s->selected_line, &s->done, &s->eof,
2344 s->path, s->blamed_commit->id, s->repo);
2345 if (err)
2346 break;
2347 break;
2349 case 'B': {
2350 struct got_object_qid *first;
2351 first = SIMPLEQ_FIRST(&s->blamed_commits);
2352 if (!got_object_id_cmp(first->id, s->commit_id))
2353 break;
2354 s->done = 1;
2355 if (pthread_mutex_unlock(&s->mutex) != 0) {
2356 err = got_error_from_errno();
2357 goto done;
2359 thread_err = stop_blame(&s->blame);
2360 s->done = 0;
2361 if (pthread_mutex_lock(&s->mutex) != 0) {
2362 err = got_error_from_errno();
2363 goto done;
2365 if (thread_err)
2366 break;
2367 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2368 got_object_qid_free(s->blamed_commit);
2369 s->blamed_commit =
2370 SIMPLEQ_FIRST(&s->blamed_commits);
2371 err = run_blame(&s->blame, &s->mutex, view,
2372 &s->blame_complete,
2373 &s->first_displayed_line,
2374 &s->last_displayed_line,
2375 &s->selected_line, &s->done, &s->eof, s->path,
2376 s->blamed_commit->id, s->repo);
2377 if (err)
2378 break;
2379 break;
2381 case KEY_ENTER:
2382 case '\r':
2383 err = open_selected_commit(&pobj, &obj,
2384 s->blame.lines, s->first_displayed_line,
2385 s->selected_line, s->repo);
2386 if (err)
2387 break;
2388 if (pobj == NULL && obj == NULL)
2389 break;
2391 if (view_is_parent_view(view))
2392 begin_x = view_split_begin_x(view->begin_x);
2393 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2394 if (diff_view == NULL) {
2395 err = got_error_from_errno();
2396 break;
2398 err = open_diff_view(diff_view, pobj, obj, s->repo);
2399 if (err) {
2400 view_close(diff_view);
2401 break;
2403 if (view_is_parent_view(view)) {
2404 err = view_close_child(view);
2405 if (err)
2406 return err;
2407 err = view_set_child(view, diff_view);
2408 if (err) {
2409 view_close(diff_view);
2410 break;
2412 if (!view_is_splitscreen(diff_view)) {
2413 *focus_view = diff_view;
2414 view->child_focussed = 1;
2416 } else
2417 *new_view = diff_view;
2418 if (pobj) {
2419 got_object_close(pobj);
2420 pobj = NULL;
2422 got_object_close(obj);
2423 obj = NULL;
2424 if (err)
2425 break;
2426 break;
2427 case KEY_NPAGE:
2428 case ' ':
2429 if (s->last_displayed_line >= s->blame.nlines &&
2430 s->selected_line < view->nlines - 2) {
2431 s->selected_line = MIN(s->blame.nlines,
2432 view->nlines - 2);
2433 break;
2435 if (s->last_displayed_line + view->nlines - 2
2436 <= s->blame.nlines)
2437 s->first_displayed_line +=
2438 view->nlines - 2;
2439 else
2440 s->first_displayed_line =
2441 s->blame.nlines -
2442 (view->nlines - 3);
2443 break;
2444 case KEY_RESIZE:
2445 if (s->selected_line > view->nlines - 2) {
2446 s->selected_line = MIN(s->blame.nlines,
2447 view->nlines - 2);
2449 break;
2450 default:
2451 break;
2454 if (pthread_mutex_unlock(&s->mutex) != 0)
2455 err = got_error_from_errno();
2456 done:
2457 if (pobj)
2458 got_object_close(pobj);
2459 return thread_err ? thread_err : err;
2462 static const struct got_error *
2463 cmd_blame(int argc, char *argv[])
2465 const struct got_error *error;
2466 struct got_repository *repo = NULL;
2467 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2468 struct got_object_id *commit_id = NULL;
2469 char *commit_id_str = NULL;
2470 int ch;
2471 struct tog_view *view;
2473 #ifndef PROFILE
2474 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2475 == -1)
2476 err(1, "pledge");
2477 #endif
2479 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2480 switch (ch) {
2481 case 'c':
2482 commit_id_str = optarg;
2483 break;
2484 case 'r':
2485 repo_path = realpath(optarg, NULL);
2486 if (repo_path == NULL)
2487 err(1, "-r option");
2488 break;
2489 default:
2490 usage();
2491 /* NOTREACHED */
2495 argc -= optind;
2496 argv += optind;
2498 if (argc == 1)
2499 path = argv[0];
2500 else
2501 usage_blame();
2503 cwd = getcwd(NULL, 0);
2504 if (cwd == NULL) {
2505 error = got_error_from_errno();
2506 goto done;
2508 if (repo_path == NULL) {
2509 repo_path = strdup(cwd);
2510 if (repo_path == NULL) {
2511 error = got_error_from_errno();
2512 goto done;
2517 error = got_repo_open(&repo, repo_path);
2518 if (error != NULL)
2519 return error;
2521 error = got_repo_map_path(&in_repo_path, repo, path);
2522 if (error != NULL)
2523 goto done;
2525 if (commit_id_str == NULL) {
2526 struct got_reference *head_ref;
2527 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2528 if (error != NULL)
2529 goto done;
2530 error = got_ref_resolve(&commit_id, repo, head_ref);
2531 got_ref_close(head_ref);
2532 } else {
2533 struct got_object *obj;
2534 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2535 if (error != NULL)
2536 goto done;
2537 commit_id = got_object_id_dup(got_object_get_id(obj));
2538 if (commit_id == NULL)
2539 error = got_error_from_errno();
2540 got_object_close(obj);
2542 if (error != NULL)
2543 goto done;
2545 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2546 if (view == NULL) {
2547 error = got_error_from_errno();
2548 goto done;
2550 error = open_blame_view(view, in_repo_path, commit_id, repo);
2551 if (error)
2552 goto done;
2553 error = view_loop(view);
2554 done:
2555 free(repo_path);
2556 free(cwd);
2557 free(commit_id);
2558 if (repo)
2559 got_repo_close(repo);
2560 return error;
2563 static const struct got_error *
2564 draw_tree_entries(struct tog_view *view,
2565 struct got_tree_entry **first_displayed_entry,
2566 struct got_tree_entry **last_displayed_entry,
2567 struct got_tree_entry **selected_entry, int *ndisplayed,
2568 const char *label, int show_ids, const char *parent_path,
2569 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2571 const struct got_error *err = NULL;
2572 struct got_tree_entry *te;
2573 wchar_t *wline;
2574 int width, n;
2576 *ndisplayed = 0;
2578 werase(view->window);
2580 if (limit == 0)
2581 return NULL;
2583 err = format_line(&wline, &width, label, view->ncols);
2584 if (err)
2585 return err;
2586 if (view_needs_focus_indication(view))
2587 wstandout(view->window);
2588 waddwstr(view->window, wline);
2589 if (view_needs_focus_indication(view))
2590 wstandend(view->window);
2591 free(wline);
2592 wline = NULL;
2593 if (width < view->ncols)
2594 waddch(view->window, '\n');
2595 if (--limit <= 0)
2596 return NULL;
2597 err = format_line(&wline, &width, parent_path, view->ncols);
2598 if (err)
2599 return err;
2600 waddwstr(view->window, wline);
2601 free(wline);
2602 wline = NULL;
2603 if (width < view->ncols)
2604 waddch(view->window, '\n');
2605 if (--limit <= 0)
2606 return NULL;
2607 waddch(view->window, '\n');
2608 if (--limit <= 0)
2609 return NULL;
2611 te = SIMPLEQ_FIRST(&entries->head);
2612 if (*first_displayed_entry == NULL) {
2613 if (selected == 0) {
2614 if (view->focussed)
2615 wstandout(view->window);
2616 *selected_entry = NULL;
2618 waddstr(view->window, " ..\n"); /* parent directory */
2619 if (selected == 0 && view->focussed)
2620 wstandend(view->window);
2621 (*ndisplayed)++;
2622 if (--limit <= 0)
2623 return NULL;
2624 n = 1;
2625 } else {
2626 n = 0;
2627 while (te != *first_displayed_entry)
2628 te = SIMPLEQ_NEXT(te, entry);
2631 while (te) {
2632 char *line = NULL, *id_str = NULL;
2634 if (show_ids) {
2635 err = got_object_id_str(&id_str, te->id);
2636 if (err)
2637 return got_error_from_errno();
2639 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2640 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2641 free(id_str);
2642 return got_error_from_errno();
2644 free(id_str);
2645 err = format_line(&wline, &width, line, view->ncols);
2646 if (err) {
2647 free(line);
2648 break;
2650 if (n == selected) {
2651 if (view->focussed)
2652 wstandout(view->window);
2653 *selected_entry = te;
2655 waddwstr(view->window, wline);
2656 if (width < view->ncols)
2657 waddch(view->window, '\n');
2658 if (n == selected && view->focussed)
2659 wstandend(view->window);
2660 free(line);
2661 free(wline);
2662 wline = NULL;
2663 n++;
2664 (*ndisplayed)++;
2665 *last_displayed_entry = te;
2666 if (--limit <= 0)
2667 break;
2668 te = SIMPLEQ_NEXT(te, entry);
2671 return err;
2674 static void
2675 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2676 const struct got_tree_entries *entries, int isroot)
2678 struct got_tree_entry *te, *prev;
2679 int i;
2681 if (*first_displayed_entry == NULL)
2682 return;
2684 te = SIMPLEQ_FIRST(&entries->head);
2685 if (*first_displayed_entry == te) {
2686 if (!isroot)
2687 *first_displayed_entry = NULL;
2688 return;
2691 /* XXX this is stupid... switch to TAILQ? */
2692 for (i = 0; i < maxscroll; i++) {
2693 while (te != *first_displayed_entry) {
2694 prev = te;
2695 te = SIMPLEQ_NEXT(te, entry);
2697 *first_displayed_entry = prev;
2698 te = SIMPLEQ_FIRST(&entries->head);
2700 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2701 *first_displayed_entry = NULL;
2704 static void
2705 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2706 struct got_tree_entry *last_displayed_entry,
2707 const struct got_tree_entries *entries)
2709 struct got_tree_entry *next;
2710 int n = 0;
2712 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2713 return;
2715 if (*first_displayed_entry)
2716 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2717 else
2718 next = SIMPLEQ_FIRST(&entries->head);
2719 while (next) {
2720 *first_displayed_entry = next;
2721 if (++n >= maxscroll)
2722 break;
2723 next = SIMPLEQ_NEXT(next, entry);
2727 static const struct got_error *
2728 tree_entry_path(char **path, struct tog_parent_trees *parents,
2729 struct got_tree_entry *te)
2731 const struct got_error *err = NULL;
2732 struct tog_parent_tree *pt;
2733 size_t len = 2; /* for leading slash and NUL */
2735 TAILQ_FOREACH(pt, parents, entry)
2736 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2737 if (te)
2738 len += strlen(te->name);
2740 *path = calloc(1, len);
2741 if (path == NULL)
2742 return got_error_from_errno();
2744 (*path)[0] = '/';
2745 pt = TAILQ_LAST(parents, tog_parent_trees);
2746 while (pt) {
2747 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2748 err = got_error(GOT_ERR_NO_SPACE);
2749 goto done;
2751 if (strlcat(*path, "/", len) >= len) {
2752 err = got_error(GOT_ERR_NO_SPACE);
2753 goto done;
2755 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2757 if (te) {
2758 if (strlcat(*path, te->name, len) >= len) {
2759 err = got_error(GOT_ERR_NO_SPACE);
2760 goto done;
2763 done:
2764 if (err) {
2765 free(*path);
2766 *path = NULL;
2768 return err;
2771 static const struct got_error *
2772 blame_tree_entry(struct tog_view **new_view, int begin_x,
2773 struct got_tree_entry *te, struct tog_parent_trees *parents,
2774 struct got_object_id *commit_id, struct got_repository *repo)
2776 const struct got_error *err = NULL;
2777 char *path;
2778 struct tog_view *blame_view;
2780 err = tree_entry_path(&path, parents, te);
2781 if (err)
2782 return err;
2784 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
2785 if (blame_view == NULL)
2786 return got_error_from_errno();
2788 err = open_blame_view(blame_view, path, commit_id, repo);
2789 if (err) {
2790 view_close(blame_view);
2791 free(path);
2792 } else
2793 *new_view = blame_view;
2794 return err;
2797 static const struct got_error *
2798 log_tree_entry(struct tog_view **new_view, int begin_x,
2799 struct got_tree_entry *te, struct tog_parent_trees *parents,
2800 struct got_object_id *commit_id, struct got_repository *repo)
2802 struct tog_view *log_view;
2803 const struct got_error *err = NULL;
2804 char *path;
2806 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
2807 if (log_view == NULL)
2808 return got_error_from_errno();
2810 err = tree_entry_path(&path, parents, te);
2811 if (err)
2812 return err;
2814 err = open_log_view(log_view, commit_id, repo, path);
2815 if (err)
2816 view_close(log_view);
2817 else
2818 *new_view = log_view;
2819 free(path);
2820 return err;
2823 static const struct got_error *
2824 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2825 struct got_object_id *commit_id, struct got_repository *repo)
2827 const struct got_error *err = NULL;
2828 char *commit_id_str = NULL;
2829 struct tog_tree_view_state *s = &view->state.tree;
2831 TAILQ_INIT(&s->parents);
2833 err = got_object_id_str(&commit_id_str, commit_id);
2834 if (err != NULL)
2835 goto done;
2837 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2838 err = got_error_from_errno();
2839 goto done;
2842 s->root = s->tree = root;
2843 s->entries = got_object_tree_get_entries(root);
2844 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2845 s->commit_id = got_object_id_dup(commit_id);
2846 if (s->commit_id == NULL) {
2847 err = got_error_from_errno();
2848 goto done;
2850 s->repo = repo;
2852 view->show = show_tree_view;
2853 view->input = input_tree_view;
2854 view->close = close_tree_view;
2855 done:
2856 free(commit_id_str);
2857 if (err) {
2858 free(s->tree_label);
2859 s->tree_label = NULL;
2861 return err;
2864 static const struct got_error *
2865 close_tree_view(struct tog_view *view)
2867 struct tog_tree_view_state *s = &view->state.tree;
2869 free(s->tree_label);
2870 s->tree_label = NULL;
2871 free(s->commit_id);
2872 s->commit_id = NULL;
2873 while (!TAILQ_EMPTY(&s->parents)) {
2874 struct tog_parent_tree *parent;
2875 parent = TAILQ_FIRST(&s->parents);
2876 TAILQ_REMOVE(&s->parents, parent, entry);
2877 free(parent);
2880 if (s->tree != s->root)
2881 got_object_tree_close(s->tree);
2882 got_object_tree_close(s->root);
2884 return NULL;
2887 static const struct got_error *
2888 show_tree_view(struct tog_view *view)
2890 const struct got_error *err = NULL;
2891 struct tog_tree_view_state *s = &view->state.tree;
2892 char *parent_path;
2894 err = tree_entry_path(&parent_path, &s->parents, NULL);
2895 if (err)
2896 return err;
2898 err = draw_tree_entries(view, &s->first_displayed_entry,
2899 &s->last_displayed_entry, &s->selected_entry,
2900 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2901 s->entries, s->selected, view->nlines, s->tree == s->root);
2902 free(parent_path);
2904 view_vborder(view);
2905 return err;
2908 static const struct got_error *
2909 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2910 struct tog_view **focus_view, struct tog_view *view, int ch)
2912 const struct got_error *err = NULL;
2913 struct tog_tree_view_state *s = &view->state.tree;
2914 struct tog_view *log_view;
2915 int begin_x = 0;
2917 switch (ch) {
2918 case 'i':
2919 s->show_ids = !s->show_ids;
2920 break;
2921 case 'l':
2922 if (!s->selected_entry)
2923 break;
2924 if (view_is_parent_view(view))
2925 begin_x = view_split_begin_x(view->begin_x);
2926 err = log_tree_entry(&log_view, begin_x,
2927 s->selected_entry, &s->parents,
2928 s->commit_id, s->repo);
2929 if (view_is_parent_view(view)) {
2930 err = view_close_child(view);
2931 if (err)
2932 return err;
2933 err = view_set_child(view, log_view);
2934 if (err) {
2935 view_close(log_view);
2936 break;
2938 *focus_view = log_view;
2939 view->child_focussed = 1;
2940 } else
2941 *new_view = log_view;
2942 break;
2943 case 'k':
2944 case KEY_UP:
2945 if (s->selected > 0)
2946 s->selected--;
2947 if (s->selected > 0)
2948 break;
2949 tree_scroll_up(&s->first_displayed_entry, 1,
2950 s->entries, s->tree == s->root);
2951 break;
2952 case KEY_PPAGE:
2953 if (SIMPLEQ_FIRST(&s->entries->head) ==
2954 s->first_displayed_entry) {
2955 if (s->tree != s->root)
2956 s->first_displayed_entry = NULL;
2957 s->selected = 0;
2958 break;
2960 tree_scroll_up(&s->first_displayed_entry,
2961 view->nlines, s->entries,
2962 s->tree == s->root);
2963 break;
2964 case 'j':
2965 case KEY_DOWN:
2966 if (s->selected < s->ndisplayed - 1) {
2967 s->selected++;
2968 break;
2970 tree_scroll_down(&s->first_displayed_entry, 1,
2971 s->last_displayed_entry, s->entries);
2972 break;
2973 case KEY_NPAGE:
2974 tree_scroll_down(&s->first_displayed_entry,
2975 view->nlines, s->last_displayed_entry,
2976 s->entries);
2977 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2978 entry))
2979 break;
2980 /* can't scroll any further; move cursor down */
2981 if (s->selected < s->ndisplayed - 1)
2982 s->selected = s->ndisplayed - 1;
2983 break;
2984 case KEY_ENTER:
2985 case '\r':
2986 if (s->selected_entry == NULL) {
2987 struct tog_parent_tree *parent;
2988 case KEY_BACKSPACE:
2989 /* user selected '..' */
2990 if (s->tree == s->root)
2991 break;
2992 parent = TAILQ_FIRST(&s->parents);
2993 TAILQ_REMOVE(&s->parents, parent,
2994 entry);
2995 got_object_tree_close(s->tree);
2996 s->tree = parent->tree;
2997 s->entries =
2998 got_object_tree_get_entries(s->tree);
2999 s->first_displayed_entry =
3000 parent->first_displayed_entry;
3001 s->selected_entry =
3002 parent->selected_entry;
3003 s->selected = parent->selected;
3004 free(parent);
3005 } else if (S_ISDIR(s->selected_entry->mode)) {
3006 struct tog_parent_tree *parent;
3007 struct got_tree_object *child;
3008 err = got_object_open_as_tree(&child,
3009 s->repo, s->selected_entry->id);
3010 if (err)
3011 break;
3012 parent = calloc(1, sizeof(*parent));
3013 if (parent == NULL) {
3014 err = got_error_from_errno();
3015 break;
3017 parent->tree = s->tree;
3018 parent->first_displayed_entry =
3019 s->first_displayed_entry;
3020 parent->selected_entry = s->selected_entry;
3021 parent->selected = s->selected;
3022 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3023 s->tree = child;
3024 s->entries =
3025 got_object_tree_get_entries(s->tree);
3026 s->selected = 0;
3027 s->first_displayed_entry = NULL;
3028 } else if (S_ISREG(s->selected_entry->mode)) {
3029 struct tog_view *blame_view;
3030 int begin_x = view_is_parent_view(view) ?
3031 view_split_begin_x(view->begin_x) : 0;
3033 err = blame_tree_entry(&blame_view, begin_x,
3034 s->selected_entry, &s->parents, s->commit_id,
3035 s->repo);
3036 if (err)
3037 break;
3038 if (view_is_parent_view(view)) {
3039 err = view_close_child(view);
3040 if (err)
3041 return err;
3042 err = view_set_child(view, blame_view);
3043 if (err) {
3044 view_close(blame_view);
3045 break;
3047 *focus_view = blame_view;
3048 view->child_focussed = 1;
3049 } else
3050 *new_view = blame_view;
3052 break;
3053 case KEY_RESIZE:
3054 if (s->selected > view->nlines)
3055 s->selected = s->ndisplayed - 1;
3056 break;
3057 default:
3058 break;
3061 return err;
3064 __dead static void
3065 usage_tree(void)
3067 endwin();
3068 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3069 getprogname());
3070 exit(1);
3073 static const struct got_error *
3074 cmd_tree(int argc, char *argv[])
3076 const struct got_error *error;
3077 struct got_repository *repo = NULL;
3078 char *repo_path = NULL;
3079 struct got_object_id *commit_id = NULL;
3080 char *commit_id_arg = NULL;
3081 struct got_commit_object *commit = NULL;
3082 struct got_tree_object *tree = NULL;
3083 int ch;
3084 struct tog_view *view;
3086 #ifndef PROFILE
3087 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3088 == -1)
3089 err(1, "pledge");
3090 #endif
3092 while ((ch = getopt(argc, argv, "c:")) != -1) {
3093 switch (ch) {
3094 case 'c':
3095 commit_id_arg = optarg;
3096 break;
3097 default:
3098 usage();
3099 /* NOTREACHED */
3103 argc -= optind;
3104 argv += optind;
3106 if (argc == 0) {
3107 repo_path = getcwd(NULL, 0);
3108 if (repo_path == NULL)
3109 return got_error_from_errno();
3110 } else if (argc == 1) {
3111 repo_path = realpath(argv[0], NULL);
3112 if (repo_path == NULL)
3113 return got_error_from_errno();
3114 } else
3115 usage_log();
3117 error = got_repo_open(&repo, repo_path);
3118 free(repo_path);
3119 if (error != NULL)
3120 return error;
3122 if (commit_id_arg == NULL) {
3123 error = get_head_commit_id(&commit_id, repo);
3124 if (error != NULL)
3125 goto done;
3126 } else {
3127 struct got_object *obj;
3128 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3129 if (error == NULL) {
3130 commit_id = got_object_id_dup(got_object_get_id(obj));
3131 if (commit_id == NULL)
3132 error = got_error_from_errno();
3135 if (error != NULL)
3136 goto done;
3138 error = got_object_open_as_commit(&commit, repo, commit_id);
3139 if (error != NULL)
3140 goto done;
3142 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3143 if (error != NULL)
3144 goto done;
3146 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3147 if (view == NULL) {
3148 error = got_error_from_errno();
3149 goto done;
3151 error = open_tree_view(view, tree, commit_id, repo);
3152 if (error)
3153 goto done;
3154 error = view_loop(view);
3155 done:
3156 free(commit_id);
3157 if (commit)
3158 got_object_commit_close(commit);
3159 if (tree)
3160 got_object_tree_close(tree);
3161 if (repo)
3162 got_repo_close(repo);
3163 return error;
3165 static void
3166 init_curses(void)
3168 initscr();
3169 cbreak();
3170 noecho();
3171 nonl();
3172 intrflush(stdscr, FALSE);
3173 keypad(stdscr, TRUE);
3174 curs_set(0);
3177 __dead static void
3178 usage(void)
3180 int i;
3182 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3183 "Available commands:\n", getprogname());
3184 for (i = 0; i < nitems(tog_commands); i++) {
3185 struct tog_cmd *cmd = &tog_commands[i];
3186 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3188 exit(1);
3191 static char **
3192 make_argv(const char *arg0, const char *arg1)
3194 char **argv;
3195 int argc = (arg1 == NULL ? 1 : 2);
3197 argv = calloc(argc, sizeof(char *));
3198 if (argv == NULL)
3199 err(1, "calloc");
3200 argv[0] = strdup(arg0);
3201 if (argv[0] == NULL)
3202 err(1, "calloc");
3203 if (arg1) {
3204 argv[1] = strdup(arg1);
3205 if (argv[1] == NULL)
3206 err(1, "calloc");
3209 return argv;
3212 int
3213 main(int argc, char *argv[])
3215 const struct got_error *error = NULL;
3216 struct tog_cmd *cmd = NULL;
3217 int ch, hflag = 0;
3218 char **cmd_argv = NULL;
3220 setlocale(LC_ALL, "");
3222 while ((ch = getopt(argc, argv, "h")) != -1) {
3223 switch (ch) {
3224 case 'h':
3225 hflag = 1;
3226 break;
3227 default:
3228 usage();
3229 /* NOTREACHED */
3233 argc -= optind;
3234 argv += optind;
3235 optind = 0;
3236 optreset = 1;
3238 if (argc == 0) {
3239 if (hflag)
3240 usage();
3241 /* Build an argument vector which runs a default command. */
3242 cmd = &tog_commands[0];
3243 cmd_argv = make_argv(cmd->name, NULL);
3244 argc = 1;
3245 } else {
3246 int i;
3248 /* Did the user specific a command? */
3249 for (i = 0; i < nitems(tog_commands); i++) {
3250 if (strncmp(tog_commands[i].name, argv[0],
3251 strlen(argv[0])) == 0) {
3252 cmd = &tog_commands[i];
3253 if (hflag)
3254 tog_commands[i].cmd_usage();
3255 break;
3258 if (cmd == NULL) {
3259 /* Did the user specify a repository? */
3260 char *repo_path = realpath(argv[0], NULL);
3261 if (repo_path) {
3262 struct got_repository *repo;
3263 error = got_repo_open(&repo, repo_path);
3264 if (error == NULL)
3265 got_repo_close(repo);
3266 } else
3267 error = got_error_from_errno();
3268 if (error) {
3269 if (hflag) {
3270 fprintf(stderr, "%s: '%s' is not a "
3271 "known command\n", getprogname(),
3272 argv[0]);
3273 usage();
3275 fprintf(stderr, "%s: '%s' is neither a known "
3276 "command nor a path to a repository\n",
3277 getprogname(), argv[0]);
3278 free(repo_path);
3279 return 1;
3281 cmd = &tog_commands[0];
3282 cmd_argv = make_argv(cmd->name, repo_path);
3283 argc = 2;
3284 free(repo_path);
3288 init_curses();
3290 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3291 if (error)
3292 goto done;
3293 done:
3294 endwin();
3295 free(cmd_argv);
3296 if (error)
3297 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3298 return 0;