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 TAILQ_HEAD(tog_view_list_head, tog_view);
199 struct tog_view {
200 TAILQ_ENTRY(tog_view) entry;
201 WINDOW *window;
202 PANEL *panel;
203 int nlines, ncols, begin_y, begin_x;
204 int lines, cols; /* copies of LINES and COLS */
205 int focussed;
206 struct tog_view *parent;
207 struct tog_view *child;
209 /* type-specific state */
210 enum tog_view_type type;
211 union {
212 struct tog_diff_view_state diff;
213 struct tog_log_view_state log;
214 struct tog_blame_view_state blame;
215 struct tog_tree_view_state tree;
216 } state;
218 const struct got_error *(*show)(struct tog_view *);
219 const struct got_error *(*update_siblings)(int *, struct tog_view *,
220 struct tog_view*);
221 const struct got_error *(*input)(struct tog_view **,
222 struct tog_view **, struct tog_view *, int);
223 const struct got_error *(*set_child)(struct tog_view *,
224 struct tog_view *);
225 const struct got_error *(*close)(struct tog_view *);
226 };
228 static const struct got_error *open_diff_view(struct tog_view *,
229 struct got_object *, struct got_object *, struct got_repository *);
230 static const struct got_error *show_diff_view(struct tog_view *);
231 static const struct got_error *update_siblings_diff_view(int *,
232 struct tog_view *, struct tog_view *);
233 static const struct got_error *input_diff_view(struct tog_view **,
234 struct tog_view **, struct tog_view *, int);
235 static const struct got_error* close_diff_view(struct tog_view *);
237 static const struct got_error *open_log_view(struct tog_view *,
238 struct got_object_id *, struct got_repository *, const char *);
239 static const struct got_error * show_log_view(struct tog_view *);
240 static const struct got_error *update_siblings_log_view(int *,
241 struct tog_view *, struct tog_view *);
242 static const struct got_error *input_log_view(struct tog_view **,
243 struct tog_view **, struct tog_view *, int);
244 static const struct got_error *close_log_view(struct tog_view *);
245 static const struct got_error* set_child_log_view(struct tog_view *,
246 struct tog_view *);
248 static const struct got_error *open_blame_view(struct tog_view *, char *,
249 struct got_object_id *, struct got_repository *);
250 static const struct got_error *show_blame_view(struct tog_view *);
251 static const struct got_error *update_siblings_blame_view(int *,
252 struct tog_view *, struct tog_view *);
253 static const struct got_error *input_blame_view(struct tog_view **,
254 struct tog_view **, struct tog_view *, int);
255 static const struct got_error *close_blame_view(struct tog_view *);
257 static const struct got_error *open_tree_view(struct tog_view *,
258 struct got_tree_object *, struct got_object_id *, struct got_repository *);
259 static const struct got_error *show_tree_view(struct tog_view *);
260 static const struct got_error *update_siblings_tree_view(int *,
261 struct tog_view *, struct tog_view *);
262 static const struct got_error *input_tree_view(struct tog_view **,
263 struct tog_view **, struct tog_view *, int);
264 static const struct got_error *close_tree_view(struct tog_view *);
266 static const struct got_error *
267 view_close(struct tog_view *view)
269 const struct got_error *err = NULL;
271 if (view->close)
272 err = view->close(view);
273 if (view->panel)
274 del_panel(view->panel);
275 if (view->window)
276 delwin(view->window);
277 free(view);
278 return err;
281 static struct tog_view *
282 view_open(int nlines, int ncols, int begin_y, int begin_x,
283 enum tog_view_type type)
285 struct tog_view *view = calloc(1, sizeof(*view));
287 if (view == NULL)
288 return NULL;
290 view->type = type;
291 view->lines = LINES;
292 view->cols = COLS;
293 view->nlines = nlines ? nlines : LINES - begin_y;
294 view->ncols = ncols ? ncols : COLS - begin_x;
295 view->begin_y = begin_y;
296 view->begin_x = begin_x;
297 view->window = newwin(nlines, ncols, begin_y, begin_x);
298 if (view->window == NULL) {
299 view_close(view);
300 return NULL;
302 view->panel = new_panel(view->window);
303 if (view->panel == NULL ||
304 set_panel_userptr(view->panel, view) != OK) {
305 view_close(view);
306 return NULL;
309 keypad(view->window, TRUE);
310 return view;
313 static const struct got_error *
314 view_show(struct tog_view *view, struct tog_view_list_head *views)
316 const struct got_error *err;
317 struct tog_view *v;
319 err = view->show(view);
320 if (err)
321 return err;
323 if (!view->focussed)
324 return NULL;
326 TAILQ_FOREACH(v, views, entry) {
327 int updated = 0;
328 err = view->update_siblings(&updated, view, v);
329 if (err)
330 return err;
331 if (updated) {
332 err = v->show(v);
333 if (err)
334 return err;
338 return err;
341 static int
342 view_split_begin_x(int begin_x)
344 if (begin_x > 0)
345 return 0;
346 return (COLS >= 120 ? COLS/2 : 0);
349 static const struct got_error *
350 view_resize(struct tog_view *view)
352 int nlines, ncols;
354 if (view->lines > LINES)
355 nlines = view->nlines - (view->lines - LINES);
356 else
357 nlines = view->nlines + (LINES - view->lines);
359 if (view->cols > COLS)
360 ncols = view->ncols - (view->cols - COLS);
361 else
362 ncols = view->ncols + (COLS - view->cols);
364 if (wresize(view->window, nlines, ncols) == ERR)
365 return got_error_from_errno();
366 replace_panel(view->panel, view->window);
368 view->nlines = nlines;
369 view->ncols = ncols;
370 view->lines = LINES;
371 view->cols = COLS;
373 return NULL;
376 static const struct got_error *
377 view_splitscreen(struct tog_view *view)
379 const struct got_error *err = NULL;
381 view->begin_y = 0;
382 view->begin_x = view_split_begin_x(0);
383 view->nlines = LINES;
384 view->ncols = COLS - view->begin_x;
385 view->lines = LINES;
386 view->cols = COLS;
387 err = view_resize(view);
388 if (err)
389 return err;
391 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
392 return got_error_from_errno();
394 return NULL;
397 static const struct got_error *
398 view_fullscreen(struct tog_view *view)
400 const struct got_error *err = NULL;
402 view->begin_x = 0;
403 view->begin_y = 0;
404 view->nlines = LINES;
405 view->ncols = COLS;
406 view->lines = LINES;
407 view->cols = COLS;
408 err = view_resize(view);
409 if (err)
410 return err;
412 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
413 return got_error_from_errno();
415 return NULL;
418 static const struct got_error *
419 view_input(struct tog_view **new, struct tog_view **dead,
420 struct tog_view **focus, int *done, struct tog_view *view,
421 struct tog_view_list_head *views)
423 const struct got_error *err = NULL;
424 struct tog_view *next, *prev, *v;
425 int ch;
427 *new = NULL;
428 *dead = NULL;
429 *focus = NULL;
431 nodelay(stdscr, FALSE);
432 ch = wgetch(view->window);
433 nodelay(stdscr, TRUE);
434 switch (ch) {
435 case ERR:
436 break;
437 case '\t':
438 next = TAILQ_NEXT(view, entry);
439 if (next)
440 *focus = next;
441 else
442 *focus = TAILQ_FIRST(views);
443 break;
444 case '~':
445 prev = TAILQ_PREV(view, tog_view_list_head, entry);
446 if (prev)
447 *focus = prev;
448 else
449 *focus = TAILQ_LAST(views, tog_view_list_head);
450 break;
451 case 'q':
452 err = view->input(new, dead, view, ch);
453 *dead = view;
454 break;
455 case 'Q':
456 *done = 1;
457 break;
458 case 'f':
459 if (view->begin_x == 0)
460 err = view_splitscreen(view);
461 else
462 err = view_fullscreen(view);
463 if (err)
464 break;
465 err = view->input(new, dead, view, KEY_RESIZE);
466 if (err)
467 break;
468 *focus = view;
469 break;
470 case KEY_RESIZE:
471 TAILQ_FOREACH(v, views, entry) {
472 err = view_resize(v);
473 if (err)
474 return err;
475 err = v->input(new, dead, v, ch);
477 break;
478 default:
479 err = view->input(new, dead, view, ch);
480 break;
483 return err;
486 void
487 view_vborder(struct tog_view *view)
489 PANEL *panel;
490 struct tog_view *view_above;
492 panel = panel_above(view->panel);
493 if (panel == NULL)
494 return;
496 view_above = panel_userptr(panel);
497 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
498 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
501 int
502 view_needs_focus_indication(struct tog_view *view)
504 PANEL *panel;
506 if (!view->focussed)
507 return 0;
509 panel = panel_above(view->panel);
510 if (panel) {
511 struct tog_view *view_above = panel_userptr(panel);
512 if (view_above->begin_x > view->begin_x)
513 return 1;
516 panel = panel_below(view->panel);
517 if (panel) {
518 struct tog_view *view_below = panel_userptr(panel);
519 if (view->begin_x > view_below->begin_x)
520 return 1;
523 return 1;
526 static const struct got_error *
527 view_loop(struct tog_view *view)
529 const struct got_error *err = NULL;
530 struct tog_view_list_head views;
531 struct tog_view *new_view, *dead_view, *focus_view, *v;
532 int done = 0;
534 TAILQ_INIT(&views);
535 TAILQ_INSERT_HEAD(&views, view, entry);
537 view->focussed = 1;
538 err = view_show(view, &views);
539 if (err)
540 return err;
541 update_panels();
542 doupdate();
543 while (!TAILQ_EMPTY(&views) && !done) {
544 err = view_input(&new_view, &dead_view, &focus_view, &done,
545 view, &views);
546 if (err)
547 break;
548 if (dead_view) {
549 TAILQ_REMOVE(&views, dead_view, entry);
550 err = view_close(dead_view);
551 if (err)
552 goto done;
553 if (view == dead_view) {
554 if (focus_view)
555 view = focus_view;
556 else if (!TAILQ_EMPTY(&views)) {
557 view = TAILQ_LAST(&views,
558 tog_view_list_head);
559 focus_view = view;
560 } else
561 view = NULL;
564 if (new_view) {
565 struct tog_view *t;
566 /* Only allow one view per type. */
567 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
568 if (v->type != new_view->type)
569 continue;
570 TAILQ_REMOVE(&views, v, entry);
571 err = view_close(v);
572 if (err)
573 goto done;
574 if (v == view)
575 view = new_view;
576 break;
578 TAILQ_INSERT_TAIL(&views, new_view, entry);
579 focus_view = new_view;
581 if (focus_view) {
582 show_panel(focus_view->panel);
583 if (view) {
584 if (focus_view->begin_x == 0 &&
585 view->begin_x > 0 &&
586 focus_view != new_view)
587 show_panel(view->panel);
588 view->focussed = 0;
590 focus_view->focussed = 1;
591 view = focus_view;
593 TAILQ_FOREACH(v, &views, entry) {
594 err = view_show(v, &views);
595 if (err)
596 break;
598 update_panels();
599 doupdate();
601 done:
602 while (!TAILQ_EMPTY(&views)) {
603 view = TAILQ_FIRST(&views);
604 TAILQ_REMOVE(&views, view, entry);
605 view_close(view);
607 return err;
610 __dead static void
611 usage_log(void)
613 endwin();
614 fprintf(stderr,
615 "usage: %s log [-c commit] [-r repository-path] [path]\n",
616 getprogname());
617 exit(1);
620 /* Create newly allocated wide-character string equivalent to a byte string. */
621 static const struct got_error *
622 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
624 char *vis = NULL;
625 const struct got_error *err = NULL;
627 *ws = NULL;
628 *wlen = mbstowcs(NULL, s, 0);
629 if (*wlen == (size_t)-1) {
630 int vislen;
631 if (errno != EILSEQ)
632 return got_error_from_errno();
634 /* byte string invalid in current encoding; try to "fix" it */
635 err = got_mbsavis(&vis, &vislen, s);
636 if (err)
637 return err;
638 *wlen = mbstowcs(NULL, vis, 0);
639 if (*wlen == (size_t)-1) {
640 err = got_error_from_errno(); /* give up */
641 goto done;
645 *ws = calloc(*wlen + 1, sizeof(*ws));
646 if (*ws == NULL) {
647 err = got_error_from_errno();
648 goto done;
651 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
652 err = got_error_from_errno();
653 done:
654 free(vis);
655 if (err) {
656 free(*ws);
657 *ws = NULL;
658 *wlen = 0;
660 return err;
663 /* Format a line for display, ensuring that it won't overflow a width limit. */
664 static const struct got_error *
665 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
667 const struct got_error *err = NULL;
668 int cols = 0;
669 wchar_t *wline = NULL;
670 size_t wlen;
671 int i;
673 *wlinep = NULL;
674 *widthp = 0;
676 err = mbs2ws(&wline, &wlen, line);
677 if (err)
678 return err;
680 i = 0;
681 while (i < wlen && cols < wlimit) {
682 int width = wcwidth(wline[i]);
683 switch (width) {
684 case 0:
685 i++;
686 break;
687 case 1:
688 case 2:
689 if (cols + width <= wlimit)
690 cols += width;
691 i++;
692 break;
693 case -1:
694 if (wline[i] == L'\t')
695 cols += TABSIZE - ((cols + 1) % TABSIZE);
696 i++;
697 break;
698 default:
699 err = got_error_from_errno();
700 goto done;
703 wline[i] = L'\0';
704 if (widthp)
705 *widthp = cols;
706 done:
707 if (err)
708 free(wline);
709 else
710 *wlinep = wline;
711 return err;
714 static const struct got_error *
715 draw_commit(struct tog_view *view, struct got_commit_object *commit,
716 struct got_object_id *id)
718 const struct got_error *err = NULL;
719 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
720 char *logmsg0 = NULL, *logmsg = NULL;
721 char *author0 = NULL, *author = NULL;
722 wchar_t *wlogmsg = NULL, *wauthor = NULL;
723 int author_width, logmsg_width;
724 char *newline, *smallerthan;
725 char *line = NULL;
726 int col, limit;
727 static const size_t date_display_cols = 9;
728 static const size_t author_display_cols = 16;
729 const int avail = view->ncols;
731 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
732 &commit->tm_committer) >= sizeof(datebuf))
733 return got_error(GOT_ERR_NO_SPACE);
735 if (avail < date_display_cols)
736 limit = MIN(sizeof(datebuf) - 1, avail);
737 else
738 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
739 waddnstr(view->window, datebuf, limit);
740 col = limit + 1;
741 if (col > avail)
742 goto done;
744 author0 = strdup(commit->author);
745 if (author0 == NULL) {
746 err = got_error_from_errno();
747 goto done;
749 author = author0;
750 smallerthan = strchr(author, '<');
751 if (smallerthan)
752 *smallerthan = '\0';
753 else {
754 char *at = strchr(author, '@');
755 if (at)
756 *at = '\0';
758 limit = avail - col;
759 err = format_line(&wauthor, &author_width, author, limit);
760 if (err)
761 goto done;
762 waddwstr(view->window, wauthor);
763 col += author_width;
764 while (col <= avail && author_width < author_display_cols + 1) {
765 waddch(view->window, ' ');
766 col++;
767 author_width++;
769 if (col > avail)
770 goto done;
772 logmsg0 = strdup(commit->logmsg);
773 if (logmsg0 == NULL) {
774 err = got_error_from_errno();
775 goto done;
777 logmsg = logmsg0;
778 while (*logmsg == '\n')
779 logmsg++;
780 newline = strchr(logmsg, '\n');
781 if (newline)
782 *newline = '\0';
783 limit = avail - col;
784 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
785 if (err)
786 goto done;
787 waddwstr(view->window, wlogmsg);
788 col += logmsg_width;
789 while (col <= avail) {
790 waddch(view->window, ' ');
791 col++;
793 done:
794 free(logmsg0);
795 free(wlogmsg);
796 free(author0);
797 free(wauthor);
798 free(line);
799 return err;
802 static struct commit_queue_entry *
803 alloc_commit_queue_entry(struct got_commit_object *commit,
804 struct got_object_id *id)
806 struct commit_queue_entry *entry;
808 entry = calloc(1, sizeof(*entry));
809 if (entry == NULL)
810 return NULL;
812 entry->id = id;
813 entry->commit = commit;
814 return entry;
817 static void
818 pop_commit(struct commit_queue *commits)
820 struct commit_queue_entry *entry;
822 entry = TAILQ_FIRST(&commits->head);
823 TAILQ_REMOVE(&commits->head, entry, entry);
824 got_object_commit_close(entry->commit);
825 commits->ncommits--;
826 /* Don't free entry->id! It is owned by the commit graph. */
827 free(entry);
830 static void
831 free_commits(struct commit_queue *commits)
833 while (!TAILQ_EMPTY(&commits->head))
834 pop_commit(commits);
837 static const struct got_error *
838 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
839 struct got_object_id *start_id, int minqueue,
840 struct got_repository *repo, const char *path)
842 const struct got_error *err = NULL;
843 int nqueued = 0;
845 if (start_id) {
846 err = got_commit_graph_iter_start(graph, start_id, repo);
847 if (err)
848 return err;
851 while (nqueued < minqueue) {
852 struct got_object_id *id;
853 struct got_commit_object *commit;
854 struct commit_queue_entry *entry;
856 err = got_commit_graph_iter_next(&id, graph);
857 if (err) {
858 if (err->code == GOT_ERR_ITER_COMPLETED) {
859 err = NULL;
860 break;
862 if (err->code != GOT_ERR_ITER_NEED_MORE)
863 break;
864 err = got_commit_graph_fetch_commits(graph,
865 minqueue, repo);
866 if (err)
867 return err;
868 continue;
871 if (id == NULL)
872 break;
874 err = got_object_open_as_commit(&commit, repo, id);
875 if (err)
876 break;
877 entry = alloc_commit_queue_entry(commit, id);
878 if (entry == NULL) {
879 err = got_error_from_errno();
880 break;
883 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
884 nqueued++;
885 commits->ncommits++;
888 return err;
891 static const struct got_error *
892 fetch_next_commit(struct commit_queue_entry **pentry,
893 struct commit_queue_entry *entry, struct commit_queue *commits,
894 struct got_commit_graph *graph, struct got_repository *repo,
895 const char *path)
897 const struct got_error *err = NULL;
899 *pentry = NULL;
901 err = queue_commits(graph, commits, NULL, 1, repo, path);
902 if (err)
903 return err;
905 /* Next entry to display should now be available. */
906 *pentry = TAILQ_NEXT(entry, entry);
907 return NULL;
910 static const struct got_error *
911 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
913 const struct got_error *err = NULL;
914 struct got_reference *head_ref;
916 *head_id = NULL;
918 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
919 if (err)
920 return err;
922 err = got_ref_resolve(head_id, repo, head_ref);
923 got_ref_close(head_ref);
924 if (err) {
925 *head_id = NULL;
926 return err;
929 return NULL;
932 static const struct got_error *
933 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
934 struct commit_queue_entry **selected, struct commit_queue_entry *first,
935 struct commit_queue *commits, int selected_idx, int limit,
936 struct got_commit_graph *graph, struct got_repository *repo,
937 const char *path)
939 const struct got_error *err = NULL;
940 struct commit_queue_entry *entry;
941 int ncommits, width;
942 char *id_str, *header;
943 wchar_t *wline;
945 entry = first;
946 ncommits = 0;
947 while (entry) {
948 if (ncommits == selected_idx) {
949 *selected = entry;
950 break;
952 entry = TAILQ_NEXT(entry, entry);
953 ncommits++;
956 err = got_object_id_str(&id_str, (*selected)->id);
957 if (err)
958 return err;
960 if (path && strcmp(path, "/") != 0) {
961 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
962 err = got_error_from_errno();
963 free(id_str);
964 return err;
966 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
967 err = got_error_from_errno();
968 free(id_str);
969 return err;
971 free(id_str);
972 err = format_line(&wline, &width, header, view->ncols);
973 if (err) {
974 free(header);
975 return err;
977 free(header);
979 werase(view->window);
981 if (view_needs_focus_indication(view))
982 wstandout(view->window);
983 waddwstr(view->window, wline);
984 if (view_needs_focus_indication(view))
985 wstandend(view->window);
986 if (width < view->ncols)
987 waddch(view->window, '\n');
988 free(wline);
989 if (limit <= 1)
990 return NULL;
992 entry = first;
993 *last = first;
994 ncommits = 0;
995 while (entry) {
996 if (ncommits >= limit - 1)
997 break;
998 if (view->focussed && ncommits == selected_idx)
999 wstandout(view->window);
1000 err = draw_commit(view, entry->commit, entry->id);
1001 if (view->focussed && ncommits == selected_idx)
1002 wstandend(view->window);
1003 if (err)
1004 break;
1005 ncommits++;
1006 *last = entry;
1007 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1008 err = queue_commits(graph, commits, NULL, 1,
1009 repo, path);
1010 if (err) {
1011 if (err->code != GOT_ERR_ITER_COMPLETED)
1012 return err;
1013 err = NULL;
1016 entry = TAILQ_NEXT(entry, entry);
1019 view_vborder(view);
1021 return err;
1024 static void
1025 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1026 struct commit_queue *commits)
1028 struct commit_queue_entry *entry;
1029 int nscrolled = 0;
1031 entry = TAILQ_FIRST(&commits->head);
1032 if (*first_displayed_entry == entry)
1033 return;
1035 entry = *first_displayed_entry;
1036 while (entry && nscrolled < maxscroll) {
1037 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1038 if (entry) {
1039 *first_displayed_entry = entry;
1040 nscrolled++;
1045 static const struct got_error *
1046 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1047 struct commit_queue_entry **last_displayed_entry,
1048 struct commit_queue *commits, struct got_commit_graph *graph,
1049 struct got_repository *repo, const char *path)
1051 const struct got_error *err = NULL;
1052 struct commit_queue_entry *pentry;
1053 int nscrolled = 0;
1055 do {
1056 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1057 if (pentry == NULL) {
1058 err = fetch_next_commit(&pentry, *last_displayed_entry,
1059 commits, graph, repo, path);
1060 if (err || pentry == NULL)
1061 break;
1063 *last_displayed_entry = pentry;
1065 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1066 if (pentry == NULL)
1067 break;
1068 *first_displayed_entry = pentry;
1069 } while (++nscrolled < maxscroll);
1071 return err;
1074 static const struct got_error *
1075 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1076 struct got_object_id *commit_id, struct got_commit_object *commit,
1077 struct got_repository *repo)
1079 const struct got_error *err;
1080 struct got_object *obj1 = NULL, *obj2 = NULL;
1081 struct got_object_qid *parent_id;
1082 struct tog_view *diff_view;
1084 err = got_object_open(&obj2, repo, commit_id);
1085 if (err)
1086 return err;
1088 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1089 if (parent_id) {
1090 err = got_object_open(&obj1, repo, parent_id->id);
1091 if (err)
1092 goto done;
1095 diff_view = view_open(0, 0, 0, view_split_begin_x(begin_x),
1096 TOG_VIEW_DIFF);
1097 if (diff_view == NULL) {
1098 err = got_error_from_errno();
1099 goto done;
1102 err = open_diff_view(diff_view, obj1, obj2, repo);
1103 if (err == NULL)
1104 *new_view = diff_view;
1105 done:
1106 if (obj1)
1107 got_object_close(obj1);
1108 if (obj2)
1109 got_object_close(obj2);
1110 return err;
1113 static const struct got_error *
1114 browse_commit(struct tog_view **new_view, int begin_x,
1115 struct commit_queue_entry *entry, struct got_repository *repo)
1117 const struct got_error *err = NULL;
1118 struct got_tree_object *tree;
1119 struct tog_view *tree_view;
1121 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1122 if (err)
1123 return err;
1125 tree_view = view_open(0, 0, 0, view_split_begin_x(begin_x),
1126 TOG_VIEW_TREE);
1127 if (tree_view == NULL)
1128 return got_error_from_errno();
1130 err = open_tree_view(tree_view, tree, entry->id, repo);
1131 if (err)
1132 got_object_tree_close(tree);
1133 else
1134 *new_view = tree_view;
1135 return err;
1138 static const struct got_error *
1139 set_child_log_view(struct tog_view *view, struct tog_view *child)
1141 struct tog_log_view_state *s = &view->state.log;
1142 struct tog_diff_view_state *ds;
1143 struct commit_queue_entry *commit, *child_entry = NULL;
1144 int selected_idx = 0;
1146 if (child->type != TOG_VIEW_DIFF)
1147 return NULL;
1148 ds = &child->state.diff;
1150 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1151 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1152 child_entry = commit;
1153 break;
1156 if (child_entry == NULL)
1157 return NULL;
1159 commit = s->first_displayed_entry;
1160 while (commit) {
1161 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1162 s->selected_entry = child_entry;
1163 s->selected = selected_idx;
1164 break;
1166 if (commit == s->last_displayed_entry)
1167 break;
1168 selected_idx++;
1169 commit = TAILQ_NEXT(commit, entry);
1172 return show_log_view(view);
1175 static const struct got_error *
1176 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1177 struct got_repository *repo, const char *path)
1179 const struct got_error *err = NULL;
1180 struct tog_log_view_state *s = &view->state.log;
1182 err = got_repo_map_path(&s->in_repo_path, repo, path);
1183 if (err != NULL)
1184 goto done;
1186 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1187 0, repo);
1188 if (err)
1189 goto done;
1190 /* The commit queue only contains commits being displayed. */
1191 TAILQ_INIT(&s->commits.head);
1192 s->commits.ncommits = 0;
1195 * Open the initial batch of commits, sorted in commit graph order.
1196 * We keep all commits open throughout the lifetime of the log view
1197 * in order to avoid having to re-fetch commits from disk while
1198 * updating the display.
1200 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1201 repo, s->in_repo_path);
1202 if (err) {
1203 if (err->code != GOT_ERR_ITER_COMPLETED)
1204 goto done;
1205 err = NULL;
1208 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1209 s->selected_entry = s->first_displayed_entry;
1210 s->repo = repo;
1211 s->start_id = got_object_id_dup(start_id);
1212 if (s->start_id == NULL) {
1213 err = got_error_from_errno();
1214 goto done;
1217 view->show = show_log_view;
1218 view->update_siblings = update_siblings_log_view;
1219 view->input = input_log_view;
1220 view->close = close_log_view;
1221 view->set_child = set_child_log_view;
1222 done:
1223 return err;
1226 static const struct got_error *
1227 close_log_view(struct tog_view *view)
1229 struct tog_log_view_state *s = &view->state.log;
1231 if (s->graph)
1232 got_commit_graph_close(s->graph);
1233 free_commits(&s->commits);
1234 free(s->in_repo_path);
1235 free(s->start_id);
1236 return NULL;
1239 static const struct got_error *
1240 update_diff_view(struct tog_view *diff_view,
1241 struct got_object_id *commit_id, struct got_commit_object *commit,
1242 struct got_repository *repo)
1244 const struct got_error *err = NULL;
1245 struct tog_diff_view_state *ds;
1246 struct got_object *obj1 = NULL, *obj2 = NULL;
1247 struct got_object_qid *parent_id;
1249 ds = &diff_view->state.diff;
1250 if (got_object_id_cmp(ds->id2, commit_id) == 0)
1251 return NULL;
1253 err = got_object_open(&obj2, repo, commit_id);
1254 if (err)
1255 return err;
1257 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1258 if (parent_id) {
1259 err = got_object_open(&obj1, repo, parent_id->id);
1260 if (err)
1261 goto done;
1264 err = close_diff_view(diff_view);
1265 if (err)
1266 goto done;
1268 err = open_diff_view(diff_view, obj1, obj2, repo);
1269 if (err)
1270 goto done;
1271 done:
1272 if (obj1)
1273 got_object_close(obj1);
1274 if (obj2)
1275 got_object_close(obj2);
1276 return err;
1279 static const struct got_error *
1280 show_log_view(struct tog_view *view)
1282 const struct got_error *err = NULL;
1283 struct tog_log_view_state *s = &view->state.log;
1285 return draw_commits(view, &s->last_displayed_entry,
1286 &s->selected_entry, s->first_displayed_entry,
1287 &s->commits, s->selected, view->nlines, s->graph,
1288 s->repo, s->in_repo_path);
1289 if (err)
1290 return err;
1293 static const struct got_error *
1294 update_siblings_log_view(int *updated, struct tog_view *view,
1295 struct tog_view *sibling_view)
1297 const struct got_error *err = NULL;
1298 struct tog_log_view_state *s = &view->state.log;
1300 switch (sibling_view->type) {
1301 case TOG_VIEW_DIFF:
1302 err = update_diff_view(sibling_view, s->selected_entry->id,
1303 s->selected_entry->commit, s->repo);
1304 *updated = 1;
1305 break;
1306 default:
1307 break;
1310 return err;
1313 static const struct got_error *
1314 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1315 struct tog_view *view, int ch)
1317 const struct got_error *err = NULL;
1318 struct tog_log_view_state *s = &view->state.log;
1319 char *parent_path;
1321 switch (ch) {
1322 case 'k':
1323 case KEY_UP:
1324 case '[':
1325 if (s->selected > 0)
1326 s->selected--;
1327 if (s->selected > 0)
1328 break;
1329 scroll_up(&s->first_displayed_entry, 1,
1330 &s->commits);
1331 break;
1332 case KEY_PPAGE:
1333 if (TAILQ_FIRST(&s->commits.head) ==
1334 s->first_displayed_entry) {
1335 s->selected = 0;
1336 break;
1338 scroll_up(&s->first_displayed_entry,
1339 view->nlines, &s->commits);
1340 break;
1341 case 'j':
1342 case KEY_DOWN:
1343 case ']':
1344 if (s->selected < MIN(view->nlines - 2,
1345 s->commits.ncommits - 1)) {
1346 s->selected++;
1347 break;
1349 err = scroll_down(&s->first_displayed_entry, 1,
1350 &s->last_displayed_entry, &s->commits,
1351 s->graph, s->repo, s->in_repo_path);
1352 if (err) {
1353 if (err->code != GOT_ERR_ITER_COMPLETED)
1354 break;
1355 err = NULL;
1357 break;
1358 case KEY_NPAGE: {
1359 struct commit_queue_entry *first;
1360 first = s->first_displayed_entry;
1361 err = scroll_down(&s->first_displayed_entry,
1362 view->nlines, &s->last_displayed_entry,
1363 &s->commits, s->graph, s->repo,
1364 s->in_repo_path);
1365 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1366 break;
1367 if (first == s->first_displayed_entry &&
1368 s->selected < MIN(view->nlines - 2,
1369 s->commits.ncommits - 1)) {
1370 /* can't scroll further down */
1371 s->selected = MIN(view->nlines - 2,
1372 s->commits.ncommits - 1);
1374 err = NULL;
1375 break;
1377 case KEY_RESIZE:
1378 if (s->selected > view->nlines - 2)
1379 s->selected = view->nlines - 2;
1380 if (s->selected > s->commits.ncommits - 1)
1381 s->selected = s->commits.ncommits - 1;
1382 break;
1383 case KEY_ENTER:
1384 case '\r':
1385 err = open_diff_view_for_commit(new_view, view->begin_x,
1386 s->selected_entry->id, s->selected_entry->commit,
1387 s->repo);
1388 break;
1389 case 't':
1390 err = browse_commit(new_view, view->begin_x,
1391 s->selected_entry, s->repo);
1392 break;
1393 case KEY_BACKSPACE:
1394 if (strcmp(s->in_repo_path, "/") == 0)
1395 break;
1396 parent_path = dirname(s->in_repo_path);
1397 if (parent_path && strcmp(parent_path, ".") != 0) {
1398 struct tog_view *lv;
1399 lv = view_open(view->nlines, view->ncols,
1400 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1401 if (lv == NULL)
1402 return got_error_from_errno();
1403 err = open_log_view(lv, s->start_id, s->repo,
1404 parent_path);
1405 if (err)
1406 break;
1407 *new_view = lv;
1409 break;
1410 default:
1411 break;
1414 return err;
1417 static const struct got_error *
1418 cmd_log(int argc, char *argv[])
1420 const struct got_error *error;
1421 struct got_repository *repo = NULL;
1422 struct got_object_id *start_id = NULL;
1423 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1424 char *start_commit = NULL;
1425 int ch;
1426 struct tog_view *view;
1428 #ifndef PROFILE
1429 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1430 == -1)
1431 err(1, "pledge");
1432 #endif
1434 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1435 switch (ch) {
1436 case 'c':
1437 start_commit = optarg;
1438 break;
1439 case 'r':
1440 repo_path = realpath(optarg, NULL);
1441 if (repo_path == NULL)
1442 err(1, "-r option");
1443 break;
1444 default:
1445 usage();
1446 /* NOTREACHED */
1450 argc -= optind;
1451 argv += optind;
1453 if (argc == 0)
1454 path = strdup("");
1455 else if (argc == 1)
1456 path = strdup(argv[0]);
1457 else
1458 usage_log();
1459 if (path == NULL)
1460 return got_error_from_errno();
1462 cwd = getcwd(NULL, 0);
1463 if (cwd == NULL) {
1464 error = got_error_from_errno();
1465 goto done;
1467 if (repo_path == NULL) {
1468 repo_path = strdup(cwd);
1469 if (repo_path == NULL) {
1470 error = got_error_from_errno();
1471 goto done;
1475 error = got_repo_open(&repo, repo_path);
1476 if (error != NULL)
1477 goto done;
1479 if (start_commit == NULL) {
1480 error = get_head_commit_id(&start_id, repo);
1481 if (error != NULL)
1482 goto done;
1483 } else {
1484 struct got_object *obj;
1485 error = got_object_open_by_id_str(&obj, repo, start_commit);
1486 if (error == NULL) {
1487 start_id = got_object_id_dup(got_object_get_id(obj));
1488 if (start_id == NULL)
1489 error = got_error_from_errno();
1490 goto done;
1493 if (error != NULL)
1494 goto done;
1496 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1497 if (view == NULL) {
1498 error = got_error_from_errno();
1499 goto done;
1501 error = open_log_view(view, start_id, repo, path);
1502 if (error)
1503 goto done;
1504 error = view_loop(view);
1505 done:
1506 free(repo_path);
1507 free(cwd);
1508 free(path);
1509 free(start_id);
1510 if (repo)
1511 got_repo_close(repo);
1512 return error;
1515 __dead static void
1516 usage_diff(void)
1518 endwin();
1519 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1520 getprogname());
1521 exit(1);
1524 static char *
1525 parse_next_line(FILE *f, size_t *len)
1527 char *line;
1528 size_t linelen;
1529 size_t lineno;
1530 const char delim[3] = { '\0', '\0', '\0'};
1532 line = fparseln(f, &linelen, &lineno, delim, 0);
1533 if (len)
1534 *len = linelen;
1535 return line;
1538 static const struct got_error *
1539 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1540 int *last_displayed_line, int *eof, int max_lines,
1541 char * header)
1543 const struct got_error *err;
1544 int nlines = 0, nprinted = 0;
1545 char *line;
1546 size_t len;
1547 wchar_t *wline;
1548 int width;
1550 rewind(f);
1551 werase(view->window);
1553 if (header) {
1554 err = format_line(&wline, &width, header, view->ncols);
1555 if (err) {
1556 return err;
1559 if (view_needs_focus_indication(view))
1560 wstandout(view->window);
1561 waddwstr(view->window, wline);
1562 if (view_needs_focus_indication(view))
1563 wstandend(view->window);
1564 if (width < view->ncols)
1565 waddch(view->window, '\n');
1567 if (max_lines <= 1)
1568 return NULL;
1569 max_lines--;
1572 *eof = 0;
1573 while (nprinted < max_lines) {
1574 line = parse_next_line(f, &len);
1575 if (line == NULL) {
1576 *eof = 1;
1577 break;
1579 if (++nlines < *first_displayed_line) {
1580 free(line);
1581 continue;
1584 err = format_line(&wline, &width, line, view->ncols);
1585 if (err) {
1586 free(line);
1587 return err;
1589 waddwstr(view->window, wline);
1590 if (width < view->ncols)
1591 waddch(view->window, '\n');
1592 if (++nprinted == 1)
1593 *first_displayed_line = nlines;
1594 free(line);
1595 free(wline);
1596 wline = NULL;
1598 *last_displayed_line = nlines;
1600 view_vborder(view);
1602 return NULL;
1605 static const struct got_error *
1606 open_diff_view(struct tog_view *view, struct got_object *obj1,
1607 struct got_object *obj2, struct got_repository *repo)
1609 const struct got_error *err;
1610 FILE *f;
1612 if (obj1 != NULL && obj2 != NULL &&
1613 got_object_get_type(obj1) != got_object_get_type(obj2))
1614 return got_error(GOT_ERR_OBJ_TYPE);
1616 f = got_opentemp();
1617 if (f == NULL)
1618 return got_error_from_errno();
1620 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1621 case GOT_OBJ_TYPE_BLOB:
1622 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1623 repo, f);
1624 break;
1625 case GOT_OBJ_TYPE_TREE:
1626 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1627 break;
1628 case GOT_OBJ_TYPE_COMMIT:
1629 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1630 break;
1631 default:
1632 return got_error(GOT_ERR_OBJ_TYPE);
1635 fflush(f);
1637 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1638 view->state.diff.id2 = got_object_get_id(obj2);
1639 view->state.diff.f = f;
1640 view->state.diff.first_displayed_line = 1;
1641 view->state.diff.last_displayed_line = view->nlines;
1643 view->show = show_diff_view;
1644 view->update_siblings = update_siblings_diff_view;
1645 view->input = input_diff_view;
1646 view->close = close_diff_view;
1648 return NULL;
1651 static const struct got_error *
1652 close_diff_view(struct tog_view *view)
1654 const struct got_error *err = NULL;
1656 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1657 err = got_error_from_errno();
1658 return err;
1661 static const struct got_error *
1662 show_diff_view(struct tog_view *view)
1664 const struct got_error *err;
1665 struct tog_diff_view_state *s = &view->state.diff;
1666 char *id_str1 = NULL, *id_str2, *header;
1668 if (s->id1) {
1669 err = got_object_id_str(&id_str1, s->id1);
1670 if (err)
1671 return err;
1673 err = got_object_id_str(&id_str2, s->id2);
1674 if (err)
1675 return err;
1677 if (asprintf(&header, "diff: %s %s",
1678 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1679 err = got_error_from_errno();
1680 free(id_str1);
1681 free(id_str2);
1682 return err;
1684 free(id_str1);
1685 free(id_str2);
1687 return draw_file(view, s->f, &s->first_displayed_line,
1688 &s->last_displayed_line, &s->eof, view->nlines,
1689 header);
1692 static const struct got_error *
1693 update_siblings_diff_view(int *updated, struct tog_view *view,
1694 struct tog_view *sibling_view)
1696 return NULL;
1699 static const struct got_error *
1700 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1701 struct tog_view *view, int ch)
1703 const struct got_error *err = NULL;
1704 struct tog_diff_view_state *s = &view->state.diff;
1705 int i;
1707 switch (ch) {
1708 case 'k':
1709 case KEY_UP:
1710 if (s->first_displayed_line > 1)
1711 s->first_displayed_line--;
1712 break;
1713 case KEY_PPAGE:
1714 i = 0;
1715 while (i++ < view->nlines - 1 &&
1716 s->first_displayed_line > 1)
1717 s->first_displayed_line--;
1718 break;
1719 case 'j':
1720 case KEY_DOWN:
1721 if (!s->eof)
1722 s->first_displayed_line++;
1723 break;
1724 case KEY_NPAGE:
1725 case ' ':
1726 i = 0;
1727 while (!s->eof && i++ < view->nlines - 1) {
1728 char *line;
1729 line = parse_next_line(s->f, NULL);
1730 s->first_displayed_line++;
1731 if (line == NULL)
1732 break;
1734 break;
1735 default:
1736 break;
1739 return err;
1742 static const struct got_error *
1743 cmd_diff(int argc, char *argv[])
1745 const struct got_error *error = NULL;
1746 struct got_repository *repo = NULL;
1747 struct got_object *obj1 = NULL, *obj2 = NULL;
1748 char *repo_path = NULL;
1749 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1750 int ch;
1751 struct tog_view *view;
1753 #ifndef PROFILE
1754 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1755 == -1)
1756 err(1, "pledge");
1757 #endif
1759 while ((ch = getopt(argc, argv, "")) != -1) {
1760 switch (ch) {
1761 default:
1762 usage();
1763 /* NOTREACHED */
1767 argc -= optind;
1768 argv += optind;
1770 if (argc == 0) {
1771 usage_diff(); /* TODO show local worktree changes */
1772 } else if (argc == 2) {
1773 repo_path = getcwd(NULL, 0);
1774 if (repo_path == NULL)
1775 return got_error_from_errno();
1776 obj_id_str1 = argv[0];
1777 obj_id_str2 = argv[1];
1778 } else if (argc == 3) {
1779 repo_path = realpath(argv[0], NULL);
1780 if (repo_path == NULL)
1781 return got_error_from_errno();
1782 obj_id_str1 = argv[1];
1783 obj_id_str2 = argv[2];
1784 } else
1785 usage_diff();
1787 error = got_repo_open(&repo, repo_path);
1788 free(repo_path);
1789 if (error)
1790 goto done;
1792 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1793 if (error)
1794 goto done;
1796 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1797 if (error)
1798 goto done;
1800 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1801 if (view == NULL) {
1802 error = got_error_from_errno();
1803 goto done;
1805 error = open_diff_view(view, obj1, obj2, repo);
1806 if (error)
1807 goto done;
1808 error = view_loop(view);
1809 done:
1810 got_repo_close(repo);
1811 if (obj1)
1812 got_object_close(obj1);
1813 if (obj2)
1814 got_object_close(obj2);
1815 return error;
1818 __dead static void
1819 usage_blame(void)
1821 endwin();
1822 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1823 getprogname());
1824 exit(1);
1827 struct tog_blame_line {
1828 int annotated;
1829 struct got_object_id *id;
1832 static const struct got_error *
1833 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1834 const char *path, struct tog_blame_line *lines, int nlines,
1835 int blame_complete, int selected_line, int *first_displayed_line,
1836 int *last_displayed_line, int *eof, int max_lines)
1838 const struct got_error *err;
1839 int lineno = 0, nprinted = 0;
1840 char *line;
1841 size_t len;
1842 wchar_t *wline;
1843 int width, wlimit;
1844 struct tog_blame_line *blame_line;
1845 struct got_object_id *prev_id = NULL;
1846 char *id_str;
1848 err = got_object_id_str(&id_str, id);
1849 if (err)
1850 return err;
1852 rewind(f);
1853 werase(view->window);
1855 if (asprintf(&line, "commit: %s", id_str) == -1) {
1856 err = got_error_from_errno();
1857 free(id_str);
1858 return err;
1861 err = format_line(&wline, &width, line, view->ncols);
1862 free(line);
1863 line = NULL;
1864 if (view_needs_focus_indication(view))
1865 wstandout(view->window);
1866 waddwstr(view->window, wline);
1867 if (view_needs_focus_indication(view))
1868 wstandend(view->window);
1869 free(wline);
1870 wline = NULL;
1871 if (width < view->ncols)
1872 waddch(view->window, '\n');
1874 if (asprintf(&line, "[%d/%d] %s%s",
1875 *first_displayed_line - 1 + selected_line, nlines,
1876 blame_complete ? "" : "annotating ", path) == -1) {
1877 free(id_str);
1878 return got_error_from_errno();
1880 free(id_str);
1881 err = format_line(&wline, &width, line, view->ncols);
1882 free(line);
1883 line = NULL;
1884 if (err)
1885 return err;
1886 waddwstr(view->window, wline);
1887 free(wline);
1888 wline = NULL;
1889 if (width < view->ncols)
1890 waddch(view->window, '\n');
1892 *eof = 0;
1893 while (nprinted < max_lines - 2) {
1894 line = parse_next_line(f, &len);
1895 if (line == NULL) {
1896 *eof = 1;
1897 break;
1899 if (++lineno < *first_displayed_line) {
1900 free(line);
1901 continue;
1904 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1905 err = format_line(&wline, &width, line, wlimit);
1906 if (err) {
1907 free(line);
1908 return err;
1911 if (view->focussed && nprinted == selected_line - 1)
1912 wstandout(view->window);
1914 blame_line = &lines[lineno - 1];
1915 if (blame_line->annotated && prev_id &&
1916 got_object_id_cmp(prev_id, blame_line->id) == 0)
1917 waddstr(view->window, " ");
1918 else if (blame_line->annotated) {
1919 char *id_str;
1920 err = got_object_id_str(&id_str, blame_line->id);
1921 if (err) {
1922 free(line);
1923 free(wline);
1924 return err;
1926 wprintw(view->window, "%.8s ", id_str);
1927 free(id_str);
1928 prev_id = blame_line->id;
1929 } else {
1930 waddstr(view->window, "........ ");
1931 prev_id = NULL;
1934 waddwstr(view->window, wline);
1935 while (width < wlimit) {
1936 waddch(view->window, ' ');
1937 width++;
1939 if (view->focussed && nprinted == selected_line - 1)
1940 wstandend(view->window);
1941 if (++nprinted == 1)
1942 *first_displayed_line = lineno;
1943 free(line);
1944 free(wline);
1945 wline = NULL;
1947 *last_displayed_line = lineno;
1949 view_vborder(view);
1951 return NULL;
1954 static const struct got_error *
1955 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1957 const struct got_error *err = NULL;
1958 struct tog_blame_cb_args *a = arg;
1959 struct tog_blame_line *line;
1961 if (nlines != a->nlines ||
1962 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1963 return got_error(GOT_ERR_RANGE);
1965 if (pthread_mutex_lock(a->mutex) != 0)
1966 return got_error_from_errno();
1968 if (*a->quit) { /* user has quit the blame view */
1969 err = got_error(GOT_ERR_ITER_COMPLETED);
1970 goto done;
1973 if (lineno == -1)
1974 goto done; /* no change in this commit */
1976 line = &a->lines[lineno - 1];
1977 if (line->annotated)
1978 goto done;
1980 line->id = got_object_id_dup(id);
1981 if (line->id == NULL) {
1982 err = got_error_from_errno();
1983 goto done;
1985 line->annotated = 1;
1987 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1988 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1989 a->last_displayed_line, a->eof, a->view->nlines);
1990 done:
1991 if (pthread_mutex_unlock(a->mutex) != 0)
1992 return got_error_from_errno();
1993 return err;
1996 static void *
1997 blame_thread(void *arg)
1999 const struct got_error *err;
2000 struct tog_blame_thread_args *ta = arg;
2001 struct tog_blame_cb_args *a = ta->cb_args;
2003 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2004 blame_cb, ta->cb_args);
2006 if (pthread_mutex_lock(a->mutex) != 0)
2007 return (void *)got_error_from_errno();
2009 got_repo_close(ta->repo);
2010 ta->repo = NULL;
2011 *ta->complete = 1;
2012 if (!err)
2013 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2014 a->lines, a->nlines, 1, *a->selected_line,
2015 a->first_displayed_line, a->last_displayed_line, a->eof,
2016 a->view->nlines);
2018 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2019 err = got_error_from_errno();
2021 return (void *)err;
2024 static struct got_object_id *
2025 get_selected_commit_id(struct tog_blame_line *lines,
2026 int first_displayed_line, int selected_line)
2028 struct tog_blame_line *line;
2030 line = &lines[first_displayed_line - 1 + selected_line - 1];
2031 if (!line->annotated)
2032 return NULL;
2034 return line->id;
2037 static const struct got_error *
2038 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2039 struct tog_blame_line *lines, int first_displayed_line,
2040 int selected_line, struct got_repository *repo)
2042 const struct got_error *err = NULL;
2043 struct got_commit_object *commit = NULL;
2044 struct got_object_id *selected_id;
2045 struct got_object_qid *pid;
2047 *pobj = NULL;
2048 *obj = NULL;
2050 selected_id = get_selected_commit_id(lines,
2051 first_displayed_line, selected_line);
2052 if (selected_id == NULL)
2053 return NULL;
2055 err = got_object_open(obj, repo, selected_id);
2056 if (err)
2057 goto done;
2059 err = got_object_commit_open(&commit, repo, *obj);
2060 if (err)
2061 goto done;
2063 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2064 if (pid) {
2065 err = got_object_open(pobj, repo, pid->id);
2066 if (err)
2067 goto done;
2069 done:
2070 if (commit)
2071 got_object_commit_close(commit);
2072 return err;
2075 static const struct got_error *
2076 stop_blame(struct tog_blame *blame)
2078 const struct got_error *err = NULL;
2079 int i;
2081 if (blame->thread) {
2082 if (pthread_join(blame->thread, (void **)&err) != 0)
2083 err = got_error_from_errno();
2084 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2085 err = NULL;
2086 blame->thread = NULL;
2088 if (blame->thread_args.repo) {
2089 got_repo_close(blame->thread_args.repo);
2090 blame->thread_args.repo = NULL;
2092 if (blame->f) {
2093 fclose(blame->f);
2094 blame->f = NULL;
2096 for (i = 0; i < blame->nlines; i++)
2097 free(blame->lines[i].id);
2098 free(blame->lines);
2099 blame->lines = NULL;
2100 free(blame->cb_args.commit_id);
2101 blame->cb_args.commit_id = NULL;
2103 return err;
2106 static const struct got_error *
2107 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2108 struct tog_view *view, int *blame_complete,
2109 int *first_displayed_line, int *last_displayed_line,
2110 int *selected_line, int *done, int *eof, const char *path,
2111 struct got_object_id *commit_id,
2112 struct got_repository *repo)
2114 const struct got_error *err = NULL;
2115 struct got_blob_object *blob = NULL;
2116 struct got_repository *thread_repo = NULL;
2117 struct got_object_id *obj_id = NULL;
2118 struct got_object *obj = NULL;
2120 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2121 if (err)
2122 goto done;
2124 err = got_object_open(&obj, repo, obj_id);
2125 if (err)
2126 goto done;
2128 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2129 err = got_error(GOT_ERR_OBJ_TYPE);
2130 goto done;
2133 err = got_object_blob_open(&blob, repo, obj, 8192);
2134 if (err)
2135 goto done;
2136 blame->f = got_opentemp();
2137 if (blame->f == NULL) {
2138 err = got_error_from_errno();
2139 goto done;
2141 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2142 blame->f, blob);
2143 if (err)
2144 goto done;
2146 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2147 if (blame->lines == NULL) {
2148 err = got_error_from_errno();
2149 goto done;
2152 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2153 if (err)
2154 goto done;
2156 blame->cb_args.view = view;
2157 blame->cb_args.lines = blame->lines;
2158 blame->cb_args.nlines = blame->nlines;
2159 blame->cb_args.mutex = mutex;
2160 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2161 if (blame->cb_args.commit_id == NULL) {
2162 err = got_error_from_errno();
2163 goto done;
2165 blame->cb_args.f = blame->f;
2166 blame->cb_args.path = path;
2167 blame->cb_args.first_displayed_line = first_displayed_line;
2168 blame->cb_args.selected_line = selected_line;
2169 blame->cb_args.last_displayed_line = last_displayed_line;
2170 blame->cb_args.quit = done;
2171 blame->cb_args.eof = eof;
2173 blame->thread_args.path = path;
2174 blame->thread_args.repo = thread_repo;
2175 blame->thread_args.cb_args = &blame->cb_args;
2176 blame->thread_args.complete = blame_complete;
2177 *blame_complete = 0;
2179 if (pthread_create(&blame->thread, NULL, blame_thread,
2180 &blame->thread_args) != 0) {
2181 err = got_error_from_errno();
2182 goto done;
2185 done:
2186 if (blob)
2187 got_object_blob_close(blob);
2188 free(obj_id);
2189 if (obj)
2190 got_object_close(obj);
2191 if (err)
2192 stop_blame(blame);
2193 return err;
2196 static const struct got_error *
2197 open_blame_view(struct tog_view *view, char *path,
2198 struct got_object_id *commit_id, struct got_repository *repo)
2200 const struct got_error *err = NULL;
2201 struct tog_blame_view_state *s = &view->state.blame;
2203 SIMPLEQ_INIT(&s->blamed_commits);
2205 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2206 return got_error_from_errno();
2208 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2209 if (err)
2210 return err;
2212 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2213 s->first_displayed_line = 1;
2214 s->last_displayed_line = view->nlines;
2215 s->selected_line = 1;
2216 s->blame_complete = 0;
2217 s->path = path;
2218 if (s->path == NULL)
2219 return got_error_from_errno();
2220 s->repo = repo;
2221 s->commit_id = commit_id;
2222 memset(&s->blame, 0, sizeof(s->blame));
2224 view->show = show_blame_view;
2225 view->update_siblings = update_siblings_blame_view;
2226 view->input = input_blame_view;
2227 view->close = close_blame_view;
2229 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2230 &s->first_displayed_line, &s->last_displayed_line,
2231 &s->selected_line, &s->done, &s->eof, s->path,
2232 s->blamed_commit->id, s->repo);
2235 static const struct got_error *
2236 close_blame_view(struct tog_view *view)
2238 const struct got_error *err = NULL;
2239 struct tog_blame_view_state *s = &view->state.blame;
2241 if (s->blame.thread)
2242 err = stop_blame(&s->blame);
2244 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2245 struct got_object_qid *blamed_commit;
2246 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2247 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2248 got_object_qid_free(blamed_commit);
2251 free(s->path);
2253 return err;
2256 static const struct got_error *
2257 show_blame_view(struct tog_view *view)
2259 const struct got_error *err = NULL;
2260 struct tog_blame_view_state *s = &view->state.blame;
2262 if (pthread_mutex_lock(&s->mutex) != 0)
2263 return got_error_from_errno();
2265 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2266 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2267 s->selected_line, &s->first_displayed_line,
2268 &s->last_displayed_line, &s->eof, view->nlines);
2270 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2271 err = got_error_from_errno();
2273 return err;
2276 static const struct got_error *
2277 update_siblings_blame_view(int *updated, struct tog_view *view,
2278 struct tog_view *sibling_view)
2280 return NULL;
2283 static const struct got_error *
2284 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2285 struct tog_view *view, int ch)
2287 const struct got_error *err = NULL, *thread_err = NULL;
2288 struct got_object *obj = NULL, *pobj = NULL;
2289 struct tog_view *diff_view;
2290 struct tog_blame_view_state *s = &view->state.blame;
2292 if (pthread_mutex_lock(&s->mutex) != 0) {
2293 err = got_error_from_errno();
2294 goto done;
2297 switch (ch) {
2298 case 'q':
2299 s->done = 1;
2300 if (pthread_mutex_unlock(&s->mutex) != 0) {
2301 err = got_error_from_errno();
2302 goto done;
2304 return stop_blame(&s->blame);
2305 case 'k':
2306 case KEY_UP:
2307 if (s->selected_line > 1)
2308 s->selected_line--;
2309 else if (s->selected_line == 1 &&
2310 s->first_displayed_line > 1)
2311 s->first_displayed_line--;
2312 break;
2313 case KEY_PPAGE:
2314 if (s->first_displayed_line == 1) {
2315 s->selected_line = 1;
2316 break;
2318 if (s->first_displayed_line > view->nlines - 2)
2319 s->first_displayed_line -=
2320 (view->nlines - 2);
2321 else
2322 s->first_displayed_line = 1;
2323 break;
2324 case 'j':
2325 case KEY_DOWN:
2326 if (s->selected_line < view->nlines - 2 &&
2327 s->first_displayed_line +
2328 s->selected_line <= s->blame.nlines)
2329 s->selected_line++;
2330 else if (s->last_displayed_line <
2331 s->blame.nlines)
2332 s->first_displayed_line++;
2333 break;
2334 case 'b':
2335 case 'p': {
2336 struct got_object_id *id;
2337 id = get_selected_commit_id(s->blame.lines,
2338 s->first_displayed_line, s->selected_line);
2339 if (id == NULL || got_object_id_cmp(id,
2340 s->blamed_commit->id) == 0)
2341 break;
2342 err = open_selected_commit(&pobj, &obj,
2343 s->blame.lines, s->first_displayed_line,
2344 s->selected_line, s->repo);
2345 if (err)
2346 break;
2347 if (pobj == NULL && obj == NULL)
2348 break;
2349 if (ch == 'p' && pobj == NULL)
2350 break;
2351 s->done = 1;
2352 if (pthread_mutex_unlock(&s->mutex) != 0) {
2353 err = got_error_from_errno();
2354 goto done;
2356 thread_err = stop_blame(&s->blame);
2357 s->done = 0;
2358 if (pthread_mutex_lock(&s->mutex) != 0) {
2359 err = got_error_from_errno();
2360 goto done;
2362 if (thread_err)
2363 break;
2364 id = got_object_get_id(ch == 'b' ? obj : pobj);
2365 got_object_close(obj);
2366 obj = NULL;
2367 if (pobj) {
2368 got_object_close(pobj);
2369 pobj = NULL;
2371 err = got_object_qid_alloc(&s->blamed_commit, id);
2372 if (err)
2373 goto done;
2374 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2375 s->blamed_commit, entry);
2376 err = run_blame(&s->blame, &s->mutex, view,
2377 &s->blame_complete,
2378 &s->first_displayed_line,
2379 &s->last_displayed_line,
2380 &s->selected_line, &s->done, &s->eof,
2381 s->path, s->blamed_commit->id, s->repo);
2382 if (err)
2383 break;
2384 break;
2386 case 'B': {
2387 struct got_object_qid *first;
2388 first = SIMPLEQ_FIRST(&s->blamed_commits);
2389 if (!got_object_id_cmp(first->id, s->commit_id))
2390 break;
2391 s->done = 1;
2392 if (pthread_mutex_unlock(&s->mutex) != 0) {
2393 err = got_error_from_errno();
2394 goto done;
2396 thread_err = stop_blame(&s->blame);
2397 s->done = 0;
2398 if (pthread_mutex_lock(&s->mutex) != 0) {
2399 err = got_error_from_errno();
2400 goto done;
2402 if (thread_err)
2403 break;
2404 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2405 got_object_qid_free(s->blamed_commit);
2406 s->blamed_commit =
2407 SIMPLEQ_FIRST(&s->blamed_commits);
2408 err = run_blame(&s->blame, &s->mutex, view,
2409 &s->blame_complete,
2410 &s->first_displayed_line,
2411 &s->last_displayed_line,
2412 &s->selected_line, &s->done, &s->eof, s->path,
2413 s->blamed_commit->id, s->repo);
2414 if (err)
2415 break;
2416 break;
2418 case KEY_ENTER:
2419 case '\r':
2420 err = open_selected_commit(&pobj, &obj,
2421 s->blame.lines, s->first_displayed_line,
2422 s->selected_line, s->repo);
2423 if (err)
2424 break;
2425 if (pobj == NULL && obj == NULL)
2426 break;
2427 diff_view = view_open(0, 0, 0,
2428 view_split_begin_x(view->begin_x), TOG_VIEW_DIFF);
2429 if (diff_view == NULL) {
2430 err = got_error_from_errno();
2431 break;
2433 err = open_diff_view(diff_view, pobj, obj,
2434 s->repo);
2435 if (err) {
2436 view_close(diff_view);
2437 break;
2439 *new_view = diff_view;
2440 if (pobj) {
2441 got_object_close(pobj);
2442 pobj = NULL;
2444 got_object_close(obj);
2445 obj = NULL;
2446 if (err)
2447 break;
2448 break;
2449 case KEY_NPAGE:
2450 case ' ':
2451 if (s->last_displayed_line >= s->blame.nlines &&
2452 s->selected_line < view->nlines - 2) {
2453 s->selected_line = MIN(s->blame.nlines,
2454 view->nlines - 2);
2455 break;
2457 if (s->last_displayed_line + view->nlines - 2
2458 <= s->blame.nlines)
2459 s->first_displayed_line +=
2460 view->nlines - 2;
2461 else
2462 s->first_displayed_line =
2463 s->blame.nlines -
2464 (view->nlines - 3);
2465 break;
2466 case KEY_RESIZE:
2467 if (s->selected_line > view->nlines - 2) {
2468 s->selected_line = MIN(s->blame.nlines,
2469 view->nlines - 2);
2471 break;
2472 default:
2473 break;
2476 if (pthread_mutex_unlock(&s->mutex) != 0)
2477 err = got_error_from_errno();
2478 done:
2479 if (pobj)
2480 got_object_close(pobj);
2481 return thread_err ? thread_err : err;
2484 static const struct got_error *
2485 cmd_blame(int argc, char *argv[])
2487 const struct got_error *error;
2488 struct got_repository *repo = NULL;
2489 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2490 struct got_object_id *commit_id = NULL;
2491 char *commit_id_str = NULL;
2492 int ch;
2493 struct tog_view *view;
2495 #ifndef PROFILE
2496 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2497 == -1)
2498 err(1, "pledge");
2499 #endif
2501 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2502 switch (ch) {
2503 case 'c':
2504 commit_id_str = optarg;
2505 break;
2506 case 'r':
2507 repo_path = realpath(optarg, NULL);
2508 if (repo_path == NULL)
2509 err(1, "-r option");
2510 break;
2511 default:
2512 usage();
2513 /* NOTREACHED */
2517 argc -= optind;
2518 argv += optind;
2520 if (argc == 1)
2521 path = argv[0];
2522 else
2523 usage_blame();
2525 cwd = getcwd(NULL, 0);
2526 if (cwd == NULL) {
2527 error = got_error_from_errno();
2528 goto done;
2530 if (repo_path == NULL) {
2531 repo_path = strdup(cwd);
2532 if (repo_path == NULL) {
2533 error = got_error_from_errno();
2534 goto done;
2539 error = got_repo_open(&repo, repo_path);
2540 if (error != NULL)
2541 return error;
2543 error = got_repo_map_path(&in_repo_path, repo, path);
2544 if (error != NULL)
2545 goto done;
2547 if (commit_id_str == NULL) {
2548 struct got_reference *head_ref;
2549 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2550 if (error != NULL)
2551 goto done;
2552 error = got_ref_resolve(&commit_id, repo, head_ref);
2553 got_ref_close(head_ref);
2554 } else {
2555 struct got_object *obj;
2556 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2557 if (error != NULL)
2558 goto done;
2559 commit_id = got_object_id_dup(got_object_get_id(obj));
2560 if (commit_id == NULL)
2561 error = got_error_from_errno();
2562 got_object_close(obj);
2564 if (error != NULL)
2565 goto done;
2567 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2568 if (view == NULL) {
2569 error = got_error_from_errno();
2570 goto done;
2572 error = open_blame_view(view, in_repo_path, commit_id, repo);
2573 if (error)
2574 goto done;
2575 error = view_loop(view);
2576 done:
2577 free(repo_path);
2578 free(cwd);
2579 free(commit_id);
2580 if (repo)
2581 got_repo_close(repo);
2582 return error;
2585 static const struct got_error *
2586 draw_tree_entries(struct tog_view *view,
2587 struct got_tree_entry **first_displayed_entry,
2588 struct got_tree_entry **last_displayed_entry,
2589 struct got_tree_entry **selected_entry, int *ndisplayed,
2590 const char *label, int show_ids, const char *parent_path,
2591 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2593 const struct got_error *err = NULL;
2594 struct got_tree_entry *te;
2595 wchar_t *wline;
2596 int width, n;
2598 *ndisplayed = 0;
2600 werase(view->window);
2602 if (limit == 0)
2603 return NULL;
2605 err = format_line(&wline, &width, label, view->ncols);
2606 if (err)
2607 return err;
2608 if (view_needs_focus_indication(view))
2609 wstandout(view->window);
2610 waddwstr(view->window, wline);
2611 if (view_needs_focus_indication(view))
2612 wstandend(view->window);
2613 free(wline);
2614 wline = NULL;
2615 if (width < view->ncols)
2616 waddch(view->window, '\n');
2617 if (--limit <= 0)
2618 return NULL;
2619 err = format_line(&wline, &width, parent_path, view->ncols);
2620 if (err)
2621 return err;
2622 waddwstr(view->window, wline);
2623 free(wline);
2624 wline = NULL;
2625 if (width < view->ncols)
2626 waddch(view->window, '\n');
2627 if (--limit <= 0)
2628 return NULL;
2629 waddch(view->window, '\n');
2630 if (--limit <= 0)
2631 return NULL;
2633 te = SIMPLEQ_FIRST(&entries->head);
2634 if (*first_displayed_entry == NULL) {
2635 if (selected == 0) {
2636 if (view->focussed)
2637 wstandout(view->window);
2638 *selected_entry = NULL;
2640 waddstr(view->window, " ..\n"); /* parent directory */
2641 if (selected == 0 && view->focussed)
2642 wstandend(view->window);
2643 (*ndisplayed)++;
2644 if (--limit <= 0)
2645 return NULL;
2646 n = 1;
2647 } else {
2648 n = 0;
2649 while (te != *first_displayed_entry)
2650 te = SIMPLEQ_NEXT(te, entry);
2653 while (te) {
2654 char *line = NULL, *id_str = NULL;
2656 if (show_ids) {
2657 err = got_object_id_str(&id_str, te->id);
2658 if (err)
2659 return got_error_from_errno();
2661 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2662 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2663 free(id_str);
2664 return got_error_from_errno();
2666 free(id_str);
2667 err = format_line(&wline, &width, line, view->ncols);
2668 if (err) {
2669 free(line);
2670 break;
2672 if (n == selected) {
2673 if (view->focussed)
2674 wstandout(view->window);
2675 *selected_entry = te;
2677 waddwstr(view->window, wline);
2678 if (width < view->ncols)
2679 waddch(view->window, '\n');
2680 if (n == selected && view->focussed)
2681 wstandend(view->window);
2682 free(line);
2683 free(wline);
2684 wline = NULL;
2685 n++;
2686 (*ndisplayed)++;
2687 *last_displayed_entry = te;
2688 if (--limit <= 0)
2689 break;
2690 te = SIMPLEQ_NEXT(te, entry);
2693 view_vborder(view);
2694 return err;
2697 static void
2698 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2699 const struct got_tree_entries *entries, int isroot)
2701 struct got_tree_entry *te, *prev;
2702 int i;
2704 if (*first_displayed_entry == NULL)
2705 return;
2707 te = SIMPLEQ_FIRST(&entries->head);
2708 if (*first_displayed_entry == te) {
2709 if (!isroot)
2710 *first_displayed_entry = NULL;
2711 return;
2714 /* XXX this is stupid... switch to TAILQ? */
2715 for (i = 0; i < maxscroll; i++) {
2716 while (te != *first_displayed_entry) {
2717 prev = te;
2718 te = SIMPLEQ_NEXT(te, entry);
2720 *first_displayed_entry = prev;
2721 te = SIMPLEQ_FIRST(&entries->head);
2723 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2724 *first_displayed_entry = NULL;
2727 static void
2728 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2729 struct got_tree_entry *last_displayed_entry,
2730 const struct got_tree_entries *entries)
2732 struct got_tree_entry *next;
2733 int n = 0;
2735 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2736 return;
2738 if (*first_displayed_entry)
2739 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2740 else
2741 next = SIMPLEQ_FIRST(&entries->head);
2742 while (next) {
2743 *first_displayed_entry = next;
2744 if (++n >= maxscroll)
2745 break;
2746 next = SIMPLEQ_NEXT(next, entry);
2750 static const struct got_error *
2751 tree_entry_path(char **path, struct tog_parent_trees *parents,
2752 struct got_tree_entry *te)
2754 const struct got_error *err = NULL;
2755 struct tog_parent_tree *pt;
2756 size_t len = 2; /* for leading slash and NUL */
2758 TAILQ_FOREACH(pt, parents, entry)
2759 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2760 if (te)
2761 len += strlen(te->name);
2763 *path = calloc(1, len);
2764 if (path == NULL)
2765 return got_error_from_errno();
2767 (*path)[0] = '/';
2768 pt = TAILQ_LAST(parents, tog_parent_trees);
2769 while (pt) {
2770 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2771 err = got_error(GOT_ERR_NO_SPACE);
2772 goto done;
2774 if (strlcat(*path, "/", len) >= len) {
2775 err = got_error(GOT_ERR_NO_SPACE);
2776 goto done;
2778 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2780 if (te) {
2781 if (strlcat(*path, te->name, len) >= len) {
2782 err = got_error(GOT_ERR_NO_SPACE);
2783 goto done;
2786 done:
2787 if (err) {
2788 free(*path);
2789 *path = NULL;
2791 return err;
2794 static const struct got_error *
2795 blame_tree_entry(struct tog_view **new_view, int begin_x,
2796 struct got_tree_entry *te, struct tog_parent_trees *parents,
2797 struct got_object_id *commit_id, struct got_repository *repo)
2799 const struct got_error *err = NULL;
2800 char *path;
2801 struct tog_view *blame_view;
2803 err = tree_entry_path(&path, parents, te);
2804 if (err)
2805 return err;
2807 blame_view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2808 if (blame_view == NULL)
2809 return got_error_from_errno();
2811 err = open_blame_view(blame_view, path, commit_id, repo);
2812 if (err) {
2813 view_close(blame_view);
2814 free(path);
2815 } else
2816 *new_view = blame_view;
2817 return err;
2820 static const struct got_error *
2821 log_tree_entry(struct tog_view **new_view,
2822 struct got_tree_entry *te, struct tog_parent_trees *parents,
2823 struct got_object_id *commit_id, struct got_repository *repo)
2825 struct tog_view *log_view;
2826 const struct got_error *err = NULL;
2827 char *path;
2829 log_view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2830 if (log_view == NULL)
2831 return got_error_from_errno();
2833 err = tree_entry_path(&path, parents, te);
2834 if (err)
2835 return err;
2837 err = open_log_view(log_view, commit_id, repo, path);
2838 if (err)
2839 view_close(log_view);
2840 else
2841 *new_view = log_view;
2842 free(path);
2843 return err;
2846 static const struct got_error *
2847 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2848 struct got_object_id *commit_id, struct got_repository *repo)
2850 const struct got_error *err = NULL;
2851 char *commit_id_str = NULL;
2852 struct tog_tree_view_state *s = &view->state.tree;
2854 TAILQ_INIT(&s->parents);
2856 err = got_object_id_str(&commit_id_str, commit_id);
2857 if (err != NULL)
2858 goto done;
2860 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2861 err = got_error_from_errno();
2862 goto done;
2865 s->root = s->tree = root;
2866 s->entries = got_object_tree_get_entries(root);
2867 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2868 s->commit_id = got_object_id_dup(commit_id);
2869 if (s->commit_id == NULL) {
2870 err = got_error_from_errno();
2871 goto done;
2873 s->repo = repo;
2875 view->show = show_tree_view;
2876 view->update_siblings = update_siblings_tree_view;
2877 view->input = input_tree_view;
2878 view->close = close_tree_view;
2879 done:
2880 free(commit_id_str);
2881 if (err) {
2882 free(s->tree_label);
2883 s->tree_label = NULL;
2885 return err;
2888 static const struct got_error *
2889 close_tree_view(struct tog_view *view)
2891 struct tog_tree_view_state *s = &view->state.tree;
2893 free(s->tree_label);
2894 s->tree_label = NULL;
2895 free(s->commit_id);
2896 s->commit_id = NULL;
2897 while (!TAILQ_EMPTY(&s->parents)) {
2898 struct tog_parent_tree *parent;
2899 parent = TAILQ_FIRST(&s->parents);
2900 TAILQ_REMOVE(&s->parents, parent, entry);
2901 free(parent);
2904 if (s->tree != s->root)
2905 got_object_tree_close(s->tree);
2906 got_object_tree_close(s->root);
2908 return NULL;
2911 static const struct got_error *
2912 show_tree_view(struct tog_view *view)
2914 const struct got_error *err = NULL;
2915 struct tog_tree_view_state *s = &view->state.tree;
2916 char *parent_path;
2918 err = tree_entry_path(&parent_path, &s->parents, NULL);
2919 if (err)
2920 return err;
2922 err = draw_tree_entries(view, &s->first_displayed_entry,
2923 &s->last_displayed_entry, &s->selected_entry,
2924 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2925 s->entries, s->selected, view->nlines, s->tree == s->root);
2926 free(parent_path);
2927 return err;
2930 static const struct got_error *
2931 update_siblings_tree_view(int *updated, struct tog_view *view,
2932 struct tog_view *sibling_view)
2934 return NULL;
2937 static const struct got_error *
2938 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2939 struct tog_view *view, int ch)
2941 const struct got_error *err = NULL;
2942 struct tog_tree_view_state *s = &view->state.tree;
2944 switch (ch) {
2945 case 'i':
2946 s->show_ids = !s->show_ids;
2947 break;
2948 case 'l':
2949 if (s->selected_entry) {
2950 err = log_tree_entry(new_view,
2951 s->selected_entry, &s->parents,
2952 s->commit_id, s->repo);
2954 break;
2955 case 'k':
2956 case KEY_UP:
2957 if (s->selected > 0)
2958 s->selected--;
2959 if (s->selected > 0)
2960 break;
2961 tree_scroll_up(&s->first_displayed_entry, 1,
2962 s->entries, s->tree == s->root);
2963 break;
2964 case KEY_PPAGE:
2965 if (SIMPLEQ_FIRST(&s->entries->head) ==
2966 s->first_displayed_entry) {
2967 if (s->tree != s->root)
2968 s->first_displayed_entry = NULL;
2969 s->selected = 0;
2970 break;
2972 tree_scroll_up(&s->first_displayed_entry,
2973 view->nlines, s->entries,
2974 s->tree == s->root);
2975 break;
2976 case 'j':
2977 case KEY_DOWN:
2978 if (s->selected < s->ndisplayed - 1) {
2979 s->selected++;
2980 break;
2982 tree_scroll_down(&s->first_displayed_entry, 1,
2983 s->last_displayed_entry, s->entries);
2984 break;
2985 case KEY_NPAGE:
2986 tree_scroll_down(&s->first_displayed_entry,
2987 view->nlines, s->last_displayed_entry,
2988 s->entries);
2989 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2990 entry))
2991 break;
2992 /* can't scroll any further; move cursor down */
2993 if (s->selected < s->ndisplayed - 1)
2994 s->selected = s->ndisplayed - 1;
2995 break;
2996 case KEY_ENTER:
2997 case '\r':
2998 if (s->selected_entry == NULL) {
2999 struct tog_parent_tree *parent;
3000 case KEY_BACKSPACE:
3001 /* user selected '..' */
3002 if (s->tree == s->root)
3003 break;
3004 parent = TAILQ_FIRST(&s->parents);
3005 TAILQ_REMOVE(&s->parents, parent,
3006 entry);
3007 got_object_tree_close(s->tree);
3008 s->tree = parent->tree;
3009 s->entries =
3010 got_object_tree_get_entries(s->tree);
3011 s->first_displayed_entry =
3012 parent->first_displayed_entry;
3013 s->selected_entry =
3014 parent->selected_entry;
3015 s->selected = parent->selected;
3016 free(parent);
3017 } else if (S_ISDIR(s->selected_entry->mode)) {
3018 struct tog_parent_tree *parent;
3019 struct got_tree_object *child;
3020 err = got_object_open_as_tree(&child,
3021 s->repo, s->selected_entry->id);
3022 if (err)
3023 break;
3024 parent = calloc(1, sizeof(*parent));
3025 if (parent == NULL) {
3026 err = got_error_from_errno();
3027 break;
3029 parent->tree = s->tree;
3030 parent->first_displayed_entry =
3031 s->first_displayed_entry;
3032 parent->selected_entry = s->selected_entry;
3033 parent->selected = s->selected;
3034 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3035 s->tree = child;
3036 s->entries =
3037 got_object_tree_get_entries(s->tree);
3038 s->selected = 0;
3039 s->first_displayed_entry = NULL;
3040 } else if (S_ISREG(s->selected_entry->mode)) {
3041 err = blame_tree_entry(new_view, view->begin_x,
3042 s->selected_entry, &s->parents,
3043 s->commit_id, s->repo);
3044 if (err)
3045 break;
3047 break;
3048 case KEY_RESIZE:
3049 if (s->selected > view->nlines)
3050 s->selected = s->ndisplayed - 1;
3051 break;
3052 default:
3053 break;
3056 return err;
3059 __dead static void
3060 usage_tree(void)
3062 endwin();
3063 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3064 getprogname());
3065 exit(1);
3068 static const struct got_error *
3069 cmd_tree(int argc, char *argv[])
3071 const struct got_error *error;
3072 struct got_repository *repo = NULL;
3073 char *repo_path = NULL;
3074 struct got_object_id *commit_id = NULL;
3075 char *commit_id_arg = NULL;
3076 struct got_commit_object *commit = NULL;
3077 struct got_tree_object *tree = NULL;
3078 int ch;
3079 struct tog_view *view;
3081 #ifndef PROFILE
3082 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3083 == -1)
3084 err(1, "pledge");
3085 #endif
3087 while ((ch = getopt(argc, argv, "c:")) != -1) {
3088 switch (ch) {
3089 case 'c':
3090 commit_id_arg = optarg;
3091 break;
3092 default:
3093 usage();
3094 /* NOTREACHED */
3098 argc -= optind;
3099 argv += optind;
3101 if (argc == 0) {
3102 repo_path = getcwd(NULL, 0);
3103 if (repo_path == NULL)
3104 return got_error_from_errno();
3105 } else if (argc == 1) {
3106 repo_path = realpath(argv[0], NULL);
3107 if (repo_path == NULL)
3108 return got_error_from_errno();
3109 } else
3110 usage_log();
3112 error = got_repo_open(&repo, repo_path);
3113 free(repo_path);
3114 if (error != NULL)
3115 return error;
3117 if (commit_id_arg == NULL) {
3118 error = get_head_commit_id(&commit_id, repo);
3119 if (error != NULL)
3120 goto done;
3121 } else {
3122 struct got_object *obj;
3123 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3124 if (error == NULL) {
3125 commit_id = got_object_id_dup(got_object_get_id(obj));
3126 if (commit_id == NULL)
3127 error = got_error_from_errno();
3130 if (error != NULL)
3131 goto done;
3133 error = got_object_open_as_commit(&commit, repo, commit_id);
3134 if (error != NULL)
3135 goto done;
3137 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3138 if (error != NULL)
3139 goto done;
3141 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3142 if (view == NULL) {
3143 error = got_error_from_errno();
3144 goto done;
3146 error = open_tree_view(view, tree, commit_id, repo);
3147 if (error)
3148 goto done;
3149 error = view_loop(view);
3150 done:
3151 free(commit_id);
3152 if (commit)
3153 got_object_commit_close(commit);
3154 if (tree)
3155 got_object_tree_close(tree);
3156 if (repo)
3157 got_repo_close(repo);
3158 return error;
3160 static void
3161 init_curses(void)
3163 initscr();
3164 cbreak();
3165 noecho();
3166 nonl();
3167 intrflush(stdscr, FALSE);
3168 keypad(stdscr, TRUE);
3169 curs_set(0);
3172 __dead static void
3173 usage(void)
3175 int i;
3177 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3178 "Available commands:\n", getprogname());
3179 for (i = 0; i < nitems(tog_commands); i++) {
3180 struct tog_cmd *cmd = &tog_commands[i];
3181 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3183 exit(1);
3186 static char **
3187 make_argv(const char *arg0, const char *arg1)
3189 char **argv;
3190 int argc = (arg1 == NULL ? 1 : 2);
3192 argv = calloc(argc, sizeof(char *));
3193 if (argv == NULL)
3194 err(1, "calloc");
3195 argv[0] = strdup(arg0);
3196 if (argv[0] == NULL)
3197 err(1, "calloc");
3198 if (arg1) {
3199 argv[1] = strdup(arg1);
3200 if (argv[1] == NULL)
3201 err(1, "calloc");
3204 return argv;
3207 int
3208 main(int argc, char *argv[])
3210 const struct got_error *error = NULL;
3211 struct tog_cmd *cmd = NULL;
3212 int ch, hflag = 0;
3213 char **cmd_argv = NULL;
3215 setlocale(LC_ALL, "");
3217 while ((ch = getopt(argc, argv, "h")) != -1) {
3218 switch (ch) {
3219 case 'h':
3220 hflag = 1;
3221 break;
3222 default:
3223 usage();
3224 /* NOTREACHED */
3228 argc -= optind;
3229 argv += optind;
3230 optind = 0;
3231 optreset = 1;
3233 if (argc == 0) {
3234 if (hflag)
3235 usage();
3236 /* Build an argument vector which runs a default command. */
3237 cmd = &tog_commands[0];
3238 cmd_argv = make_argv(cmd->name, NULL);
3239 argc = 1;
3240 } else {
3241 int i;
3243 /* Did the user specific a command? */
3244 for (i = 0; i < nitems(tog_commands); i++) {
3245 if (strncmp(tog_commands[i].name, argv[0],
3246 strlen(argv[0])) == 0) {
3247 cmd = &tog_commands[i];
3248 if (hflag)
3249 tog_commands[i].cmd_usage();
3250 break;
3253 if (cmd == NULL) {
3254 /* Did the user specify a repository? */
3255 char *repo_path = realpath(argv[0], NULL);
3256 if (repo_path) {
3257 struct got_repository *repo;
3258 error = got_repo_open(&repo, repo_path);
3259 if (error == NULL)
3260 got_repo_close(repo);
3261 } else
3262 error = got_error_from_errno();
3263 if (error) {
3264 if (hflag) {
3265 fprintf(stderr, "%s: '%s' is not a "
3266 "known command\n", getprogname(),
3267 argv[0]);
3268 usage();
3270 fprintf(stderr, "%s: '%s' is neither a known "
3271 "command nor a path to a repository\n",
3272 getprogname(), argv[0]);
3273 free(repo_path);
3274 return 1;
3276 cmd = &tog_commands[0];
3277 cmd_argv = make_argv(cmd->name, repo_path);
3278 argc = 2;
3279 free(repo_path);
3283 init_curses();
3285 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3286 if (error)
3287 goto done;
3288 done:
3289 endwin();
3290 free(cmd_argv);
3291 if (error)
3292 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3293 return 0;