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>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_diff.h"
43 #include "got_opentemp.h"
44 #include "got_commit_graph.h"
45 #include "got_utf8.h"
46 #include "got_blame.h"
48 #ifndef MIN
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #endif
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct tog_cmd {
57 const char *name;
58 const struct got_error *(*cmd_main)(int, char *[]);
59 void (*cmd_usage)(void);
60 const char *descr;
61 };
63 __dead static void usage(void);
64 __dead static void usage_log(void);
65 __dead static void usage_diff(void);
66 __dead static void usage_blame(void);
67 __dead static void usage_tree(void);
69 static const struct got_error* cmd_log(int, char *[]);
70 static const struct got_error* cmd_diff(int, char *[]);
71 static const struct got_error* cmd_blame(int, char *[]);
72 static const struct got_error* cmd_tree(int, char *[]);
74 static struct tog_cmd tog_commands[] = {
75 { "log", cmd_log, usage_log,
76 "show repository history" },
77 { "diff", cmd_diff, usage_diff,
78 "compare files and directories" },
79 { "blame", cmd_blame, usage_blame,
80 "show line-by-line file history" },
81 { "tree", cmd_tree, usage_tree,
82 "browse trees in repository" },
83 };
85 enum tog_view_type {
86 TOG_VIEW_DIFF,
87 TOG_VIEW_LOG,
88 TOG_VIEW_BLAME,
89 TOG_VIEW_TREE
90 };
92 struct tog_diff_view_state {
93 struct got_object_id *id1, *id2;
94 FILE *f;
95 int first_displayed_line;
96 int last_displayed_line;
97 int eof;
98 };
100 struct commit_queue_entry {
101 TAILQ_ENTRY(commit_queue_entry) entry;
102 struct got_object_id *id;
103 struct got_commit_object *commit;
104 };
105 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 struct commit_queue {
107 int ncommits;
108 struct commit_queue_head head;
109 };
111 struct tog_log_view_state {
112 struct got_commit_graph *graph;
113 struct commit_queue commits;
114 struct commit_queue_entry *first_displayed_entry;
115 struct commit_queue_entry *last_displayed_entry;
116 struct commit_queue_entry *selected_entry;
117 int selected;
118 char *in_repo_path;
119 struct got_repository *repo;
120 };
122 struct tog_blame_cb_args {
123 pthread_mutex_t *mutex;
124 struct tog_blame_line *lines; /* one per line */
125 int nlines;
127 struct tog_view *view;
128 struct got_object_id *commit_id;
129 FILE *f;
130 const char *path;
131 int *first_displayed_line;
132 int *last_displayed_line;
133 int *selected_line;
134 int *quit;
135 int *eof;
136 };
138 struct tog_blame_thread_args {
139 const char *path;
140 struct got_repository *repo;
141 struct tog_blame_cb_args *cb_args;
142 int *complete;
143 };
145 struct tog_blame {
146 FILE *f;
147 size_t filesize;
148 struct tog_blame_line *lines;
149 size_t nlines;
150 pthread_t thread;
151 struct tog_blame_thread_args thread_args;
152 struct tog_blame_cb_args cb_args;
153 const char *path;
154 };
156 struct tog_blame_view_state {
157 int first_displayed_line;
158 int last_displayed_line;
159 int selected_line;
160 int blame_complete;
161 int eof;
162 int done;
163 pthread_mutex_t mutex;
164 struct got_object_id_queue blamed_commits;
165 struct got_object_qid *blamed_commit;
166 char *path;
167 struct got_repository *repo;
168 struct got_object_id *commit_id;
169 struct tog_blame blame;
170 };
172 struct tog_parent_tree {
173 TAILQ_ENTRY(tog_parent_tree) entry;
174 struct got_tree_object *tree;
175 struct got_tree_entry *first_displayed_entry;
176 struct got_tree_entry *selected_entry;
177 int selected;
178 };
180 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 struct tog_tree_view_state {
183 char *tree_label;
184 struct got_tree_object *root;
185 struct got_tree_object *tree;
186 const struct got_tree_entries *entries;
187 struct got_tree_entry *first_displayed_entry;
188 struct got_tree_entry *last_displayed_entry;
189 struct got_tree_entry *selected_entry;
190 int nentries, ndisplayed, selected, show_ids;
191 struct tog_parent_trees parents;
192 struct got_object_id *commit_id;
193 struct got_repository *repo;
194 };
196 TAILQ_HEAD(tog_view_list_head, tog_view);
197 struct tog_view {
198 TAILQ_ENTRY(tog_view) entry;
199 WINDOW *window;
200 PANEL *panel;
201 int nlines, ncols, begin_y, begin_x;
202 int lines, cols; /* copies of LINES and COLS */
203 int focussed;
204 struct tog_view *parent;
205 struct tog_view *child;
207 /* type-specific state */
208 enum tog_view_type type;
209 union {
210 struct tog_diff_view_state diff;
211 struct tog_log_view_state log;
212 struct tog_blame_view_state blame;
213 struct tog_tree_view_state tree;
214 } state;
216 const struct got_error *(*show)(struct tog_view *);
217 const struct got_error *(*input)(struct tog_view **,
218 struct tog_view **, struct tog_view *, int);
219 const struct got_error *(*set_child)(struct tog_view *,
220 struct tog_view *);
221 const struct got_error *(*close)(struct tog_view *);
222 };
224 static const struct got_error *open_diff_view(struct tog_view *,
225 struct got_object *, struct got_object *, struct got_repository *);
226 static const struct got_error *show_diff_view(struct tog_view *);
227 static const struct got_error *input_diff_view(struct tog_view **,
228 struct tog_view **, struct tog_view *, int);
229 static const struct got_error* close_diff_view(struct tog_view *);
231 static const struct got_error *open_log_view(struct tog_view *,
232 struct got_object_id *, struct got_repository *, const char *);
233 static const struct got_error * show_log_view(struct tog_view *);
234 static const struct got_error *input_log_view(struct tog_view **,
235 struct tog_view **, struct tog_view *, int);
236 static const struct got_error *close_log_view(struct tog_view *);
237 static const struct got_error* set_child_log_view(struct tog_view *,
238 struct tog_view *);
240 static const struct got_error *open_blame_view(struct tog_view *, char *,
241 struct got_object_id *, struct got_repository *);
242 static const struct got_error *show_blame_view(struct tog_view *);
243 static const struct got_error *input_blame_view(struct tog_view **,
244 struct tog_view **, struct tog_view *, int);
245 static const struct got_error *close_blame_view(struct tog_view *);
247 static const struct got_error *open_tree_view(struct tog_view *,
248 struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 static const struct got_error *show_tree_view(struct tog_view *);
250 static const struct got_error *input_tree_view(struct tog_view **,
251 struct tog_view **, struct tog_view *, int);
252 static const struct got_error *close_tree_view(struct tog_view *);
254 static const struct got_error *
255 view_close(struct tog_view *view)
257 const struct got_error *err = NULL;
259 if (view->child)
260 view->child->parent = NULL;
261 if (view->parent)
262 view->parent->child = NULL;
263 if (view->close)
264 err = view->close(view);
265 if (view->panel)
266 del_panel(view->panel);
267 if (view->window)
268 delwin(view->window);
269 free(view);
270 return err;
273 static struct tog_view *
274 view_open(int nlines, int ncols, int begin_y, int begin_x,
275 struct tog_view *parent, enum tog_view_type type)
277 struct tog_view *view = calloc(1, sizeof(*view));
279 if (view == NULL)
280 return NULL;
282 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 begin_x = parent->ncols - 80;
285 view->parent = parent;
286 if (parent)
287 parent->child = view;
288 view->type = type;
289 view->lines = LINES;
290 view->cols = COLS;
291 view->nlines = nlines ? nlines : LINES - begin_y;
292 view->ncols = ncols ? ncols : COLS - begin_x;
293 view->begin_y = begin_y;
294 view->begin_x = begin_x;
295 view->window = newwin(nlines, ncols, begin_y, begin_x);
296 if (view->window == NULL) {
297 view_close(view);
298 return NULL;
300 view->panel = new_panel(view->window);
301 if (view->panel == NULL) {
302 view_close(view);
303 return NULL;
306 keypad(view->window, TRUE);
307 return view;
310 static const struct got_error *
311 view_show(struct tog_view *view)
313 const struct got_error *err;
315 if (view->parent) {
316 err = view->parent->show(view->parent);
317 if (err)
318 return err;
319 show_panel(view->parent->panel);
322 err = view->show(view);
323 if (err)
324 return err;
325 show_panel(view->panel);
327 if (view->child && view->child->begin_x > view->begin_x) {
328 err = view->child->show(view->child);
329 if (err)
330 return err;
331 show_panel(view->child->panel);
334 update_panels();
335 doupdate();
337 return err;
340 static const struct got_error *
341 view_resize(struct tog_view *view)
343 int nlines, ncols;
345 while (view) {
346 if (view->lines > LINES)
347 nlines = view->nlines - (view->lines - LINES);
348 else
349 nlines = view->nlines + (LINES - view->lines);
351 if (view->cols > COLS)
352 ncols = view->ncols - (view->cols - COLS);
353 else
354 ncols = view->ncols + (COLS - view->cols);
356 if (wresize(view->window, nlines, ncols) == ERR)
357 return got_error_from_errno();
358 replace_panel(view->panel, view->window);
360 view->nlines = nlines;
361 view->ncols = ncols;
362 view->lines = LINES;
363 view->cols = COLS;
365 view = view->parent;
368 return NULL;
371 static const struct got_error *
372 view_input(struct tog_view **new, struct tog_view **dead,
373 struct tog_view **focus, int *done, struct tog_view *view,
374 struct tog_view_list_head *views)
376 const struct got_error *err = NULL;
377 struct tog_view *next, *prev;
378 int ch;
380 *new = NULL;
381 *dead = NULL;
383 nodelay(stdscr, FALSE);
384 ch = wgetch(view->window);
385 nodelay(stdscr, TRUE);
386 switch (ch) {
387 case ERR:
388 break;
389 case '\t':
390 next = TAILQ_NEXT(view, entry);
391 if (next)
392 *focus = next;
393 else
394 *focus = TAILQ_FIRST(views);
395 view->focussed = 0;
396 (*focus)->focussed = 1;
397 break;
398 case KEY_BACKSPACE:
399 prev = TAILQ_PREV(view, tog_view_list_head, entry);
400 if (prev)
401 *focus = prev;
402 else
403 *focus = TAILQ_LAST(views, tog_view_list_head);
404 view->focussed = 0;
405 (*focus)->focussed = 1;
406 break;
407 case 'q':
408 err = view->input(new, dead, view, ch);
409 *dead = view;
410 break;
411 case 'Q':
412 *done = 1;
413 break;
414 case KEY_RESIZE:
415 err = view_resize(view);
416 if (err)
417 return err;
418 err = view->input(new, dead, view, ch);
419 break;
420 default:
421 err = view->input(new, dead, view, ch);
422 break;
425 return err;
428 static const struct got_error *
429 view_set_child(struct tog_view *view, struct tog_view *child)
431 const struct got_error *err;
433 if (view->set_child) {
434 err = view->set_child(view, child);
435 if (err)
436 return err;
439 view->child = child;
440 return NULL;
443 void
444 view_vborder(struct tog_view *view)
446 if (view->child == NULL)
447 return;
449 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
450 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
453 int
454 view_needs_focus_indication(struct tog_view *view)
456 if (!view->focussed)
457 return 0;
459 if (view->child && view->child->begin_x > view->begin_x)
460 return 1;
462 if (view->parent && view->begin_x > view->parent->begin_x)
463 return 1;
465 return 0;
468 static const struct got_error *
469 view_loop(struct tog_view *view)
471 const struct got_error *err = NULL;
472 struct tog_view_list_head views;
473 struct tog_view *new_view, *dead_view;
474 int done = 0;
476 TAILQ_INIT(&views);
477 TAILQ_INSERT_HEAD(&views, view, entry);
479 view->focussed = 1;
480 while (!TAILQ_EMPTY(&views) && !done) {
481 err = view_show(view);
482 if (err)
483 break;
484 err = view_input(&new_view, &dead_view, &view, &done,
485 view, &views);
486 if (err)
487 break;
488 if (dead_view) {
489 struct tog_view *v, *t;
490 TAILQ_REMOVE(&views, dead_view, entry);
491 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
492 if (v->parent == dead_view) {
493 TAILQ_REMOVE(&views, v, entry);
494 err = view_close(v);
495 if (err)
496 goto done;
499 if (dead_view->parent)
500 view = dead_view->parent;
501 else
502 view = TAILQ_LAST(&views, tog_view_list_head);
503 if (view)
504 view->focussed = 1;
505 err = view_close(dead_view);
506 if (err)
507 goto done;
509 if (new_view) {
510 view->focussed = 0;
511 /* TODO: de-duplicate! */
512 TAILQ_INSERT_TAIL(&views, new_view, entry);
513 if (new_view->parent) {
514 err = view_set_child(new_view->parent, new_view);
515 if (err)
516 goto done;
517 new_view->parent->focussed = 0;
519 view = new_view;
520 view->focussed = 1;
523 done:
524 while (!TAILQ_EMPTY(&views)) {
525 view = TAILQ_FIRST(&views);
526 TAILQ_REMOVE(&views, view, entry);
527 view_close(view);
529 return err;
532 __dead static void
533 usage_log(void)
535 endwin();
536 fprintf(stderr,
537 "usage: %s log [-c commit] [-r repository-path] [path]\n",
538 getprogname());
539 exit(1);
542 /* Create newly allocated wide-character string equivalent to a byte string. */
543 static const struct got_error *
544 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
546 char *vis = NULL;
547 const struct got_error *err = NULL;
549 *ws = NULL;
550 *wlen = mbstowcs(NULL, s, 0);
551 if (*wlen == (size_t)-1) {
552 int vislen;
553 if (errno != EILSEQ)
554 return got_error_from_errno();
556 /* byte string invalid in current encoding; try to "fix" it */
557 err = got_mbsavis(&vis, &vislen, s);
558 if (err)
559 return err;
560 *wlen = mbstowcs(NULL, vis, 0);
561 if (*wlen == (size_t)-1) {
562 err = got_error_from_errno(); /* give up */
563 goto done;
567 *ws = calloc(*wlen + 1, sizeof(*ws));
568 if (*ws == NULL) {
569 err = got_error_from_errno();
570 goto done;
573 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
574 err = got_error_from_errno();
575 done:
576 free(vis);
577 if (err) {
578 free(*ws);
579 *ws = NULL;
580 *wlen = 0;
582 return err;
585 /* Format a line for display, ensuring that it won't overflow a width limit. */
586 static const struct got_error *
587 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
589 const struct got_error *err = NULL;
590 int cols = 0;
591 wchar_t *wline = NULL;
592 size_t wlen;
593 int i;
595 *wlinep = NULL;
596 *widthp = 0;
598 err = mbs2ws(&wline, &wlen, line);
599 if (err)
600 return err;
602 i = 0;
603 while (i < wlen && cols < wlimit) {
604 int width = wcwidth(wline[i]);
605 switch (width) {
606 case 0:
607 i++;
608 break;
609 case 1:
610 case 2:
611 if (cols + width <= wlimit)
612 cols += width;
613 i++;
614 break;
615 case -1:
616 if (wline[i] == L'\t')
617 cols += TABSIZE - ((cols + 1) % TABSIZE);
618 i++;
619 break;
620 default:
621 err = got_error_from_errno();
622 goto done;
625 wline[i] = L'\0';
626 if (widthp)
627 *widthp = cols;
628 done:
629 if (err)
630 free(wline);
631 else
632 *wlinep = wline;
633 return err;
636 static const struct got_error *
637 draw_commit(struct tog_view *view, struct got_commit_object *commit,
638 struct got_object_id *id)
640 const struct got_error *err = NULL;
641 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
642 char *logmsg0 = NULL, *logmsg = NULL;
643 char *author0 = NULL, *author = NULL;
644 wchar_t *wlogmsg = NULL, *wauthor = NULL;
645 int author_width, logmsg_width;
646 char *newline, *smallerthan;
647 char *line = NULL;
648 int col, limit;
649 static const size_t date_display_cols = 9;
650 static const size_t author_display_cols = 16;
651 const int avail = view->ncols;
653 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
654 &commit->tm_committer) >= sizeof(datebuf))
655 return got_error(GOT_ERR_NO_SPACE);
657 if (avail < date_display_cols)
658 limit = MIN(sizeof(datebuf) - 1, avail);
659 else
660 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
661 waddnstr(view->window, datebuf, limit);
662 col = limit + 1;
663 if (col > avail)
664 goto done;
666 author0 = strdup(commit->author);
667 if (author0 == NULL) {
668 err = got_error_from_errno();
669 goto done;
671 author = author0;
672 smallerthan = strchr(author, '<');
673 if (smallerthan)
674 *smallerthan = '\0';
675 else {
676 char *at = strchr(author, '@');
677 if (at)
678 *at = '\0';
680 limit = avail - col;
681 err = format_line(&wauthor, &author_width, author, limit);
682 if (err)
683 goto done;
684 waddwstr(view->window, wauthor);
685 col += author_width;
686 while (col <= avail && author_width < author_display_cols + 1) {
687 waddch(view->window, ' ');
688 col++;
689 author_width++;
691 if (col > avail)
692 goto done;
694 logmsg0 = strdup(commit->logmsg);
695 if (logmsg0 == NULL) {
696 err = got_error_from_errno();
697 goto done;
699 logmsg = logmsg0;
700 while (*logmsg == '\n')
701 logmsg++;
702 newline = strchr(logmsg, '\n');
703 if (newline)
704 *newline = '\0';
705 limit = avail - col;
706 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
707 if (err)
708 goto done;
709 waddwstr(view->window, wlogmsg);
710 col += logmsg_width;
711 while (col <= avail) {
712 waddch(view->window, ' ');
713 col++;
715 done:
716 free(logmsg0);
717 free(wlogmsg);
718 free(author0);
719 free(wauthor);
720 free(line);
721 return err;
724 static struct commit_queue_entry *
725 alloc_commit_queue_entry(struct got_commit_object *commit,
726 struct got_object_id *id)
728 struct commit_queue_entry *entry;
730 entry = calloc(1, sizeof(*entry));
731 if (entry == NULL)
732 return NULL;
734 entry->id = id;
735 entry->commit = commit;
736 return entry;
739 static void
740 pop_commit(struct commit_queue *commits)
742 struct commit_queue_entry *entry;
744 entry = TAILQ_FIRST(&commits->head);
745 TAILQ_REMOVE(&commits->head, entry, entry);
746 got_object_commit_close(entry->commit);
747 commits->ncommits--;
748 /* Don't free entry->id! It is owned by the commit graph. */
749 free(entry);
752 static void
753 free_commits(struct commit_queue *commits)
755 while (!TAILQ_EMPTY(&commits->head))
756 pop_commit(commits);
759 static const struct got_error *
760 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
761 struct got_object_id *start_id, int minqueue, int init,
762 struct got_repository *repo, const char *path)
764 const struct got_error *err = NULL;
765 struct got_object_id *id;
766 struct commit_queue_entry *entry;
767 int nqueued = 0, found_obj = 0;
768 int is_root_path = strcmp(path, "/") == 0;
770 err = got_commit_graph_iter_start(graph, start_id, repo);
771 if (err)
772 return err;
774 entry = TAILQ_LAST(&commits->head, commit_queue_head);
775 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
776 /* Start ID's commit is already on the queue; skip over it. */
777 err = got_commit_graph_iter_next(&id, graph);
778 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
779 return err;
781 err = got_commit_graph_fetch_commits(graph, 1, repo);
782 if (err)
783 return err;
786 while (1) {
787 struct got_commit_object *commit;
789 err = got_commit_graph_iter_next(&id, graph);
790 if (err) {
791 if (err->code != GOT_ERR_ITER_NEED_MORE)
792 break;
793 if (nqueued >= minqueue) {
794 err = NULL;
795 break;
797 err = got_commit_graph_fetch_commits(graph, 1, repo);
798 if (err)
799 return err;
800 continue;
802 if (id == NULL)
803 break;
805 err = got_object_open_as_commit(&commit, repo, id);
806 if (err)
807 break;
809 if (!is_root_path) {
810 struct got_object_id *obj_id = NULL;
811 struct got_object_qid *pid;
812 int changed = 0;
814 err = got_object_id_by_path(&obj_id, repo, id, path);
815 if (err) {
816 got_object_commit_close(commit);
817 if (err->code == GOT_ERR_NO_OBJ &&
818 (found_obj || !init)) {
819 /* History stops here. */
820 err = got_error(GOT_ERR_ITER_COMPLETED);
822 break;
824 found_obj = 1;
826 pid = SIMPLEQ_FIRST(&commit->parent_ids);
827 if (pid != NULL) {
828 struct got_object_id *pobj_id;
829 err = got_object_id_by_path(&pobj_id, repo,
830 pid->id, path);
831 if (err) {
832 if (err->code != GOT_ERR_NO_OBJ) {
833 got_object_commit_close(commit);
834 free(obj_id);
835 break;
837 err = NULL;
838 changed = 1;
839 } else {
840 changed = (got_object_id_cmp(obj_id,
841 pobj_id) != 0);
843 free(pobj_id);
845 free(obj_id);
846 if (!changed) {
847 got_object_commit_close(commit);
848 continue;
852 entry = alloc_commit_queue_entry(commit, id);
853 if (entry == NULL) {
854 err = got_error_from_errno();
855 break;
857 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
858 nqueued++;
859 commits->ncommits++;
862 return err;
865 static const struct got_error *
866 fetch_next_commit(struct commit_queue_entry **pentry,
867 struct commit_queue_entry *entry, struct commit_queue *commits,
868 struct got_commit_graph *graph, struct got_repository *repo,
869 const char *path)
871 const struct got_error *err = NULL;
873 *pentry = NULL;
875 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
876 if (err)
877 return err;
879 /* Next entry to display should now be available. */
880 *pentry = TAILQ_NEXT(entry, entry);
881 if (*pentry == NULL)
882 return got_error(GOT_ERR_NO_OBJ);
884 return NULL;
887 static const struct got_error *
888 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
890 const struct got_error *err = NULL;
891 struct got_reference *head_ref;
893 *head_id = NULL;
895 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
896 if (err)
897 return err;
899 err = got_ref_resolve(head_id, repo, head_ref);
900 got_ref_close(head_ref);
901 if (err) {
902 *head_id = NULL;
903 return err;
906 return NULL;
909 static const struct got_error *
910 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
911 struct commit_queue_entry **selected, struct commit_queue_entry *first,
912 struct commit_queue *commits, int selected_idx, int limit,
913 struct got_commit_graph *graph, struct got_repository *repo,
914 const char *path)
916 const struct got_error *err = NULL;
917 struct commit_queue_entry *entry;
918 int ncommits, width;
919 char *id_str, *header;
920 wchar_t *wline;
922 entry = first;
923 ncommits = 0;
924 while (entry) {
925 if (ncommits == selected_idx) {
926 *selected = entry;
927 break;
929 entry = TAILQ_NEXT(entry, entry);
930 ncommits++;
933 err = got_object_id_str(&id_str, (*selected)->id);
934 if (err)
935 return err;
937 if (path && strcmp(path, "/") != 0) {
938 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
939 err = got_error_from_errno();
940 free(id_str);
941 return err;
943 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
944 err = got_error_from_errno();
945 free(id_str);
946 return err;
948 free(id_str);
949 err = format_line(&wline, &width, header, view->ncols);
950 if (err) {
951 free(header);
952 return err;
954 free(header);
956 werase(view->window);
958 if (view_needs_focus_indication(view))
959 wstandout(view->window);
960 waddwstr(view->window, wline);
961 if (view_needs_focus_indication(view))
962 wstandend(view->window);
963 if (width < view->ncols)
964 waddch(view->window, '\n');
965 free(wline);
966 if (limit <= 1)
967 return NULL;
969 entry = first;
970 *last = first;
971 ncommits = 0;
972 while (entry) {
973 if (ncommits >= limit - 1)
974 break;
975 if (ncommits == selected_idx)
976 wstandout(view->window);
977 err = draw_commit(view, entry->commit, entry->id);
978 if (ncommits == selected_idx)
979 wstandend(view->window);
980 if (err)
981 break;
982 ncommits++;
983 *last = entry;
984 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
985 err = queue_commits(graph, commits, entry->id, 1,
986 0, repo, path);
987 if (err) {
988 if (err->code != GOT_ERR_ITER_COMPLETED)
989 return err;
990 err = NULL;
993 entry = TAILQ_NEXT(entry, entry);
996 view_vborder(view);
998 return err;
1001 static void
1002 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1003 struct commit_queue *commits)
1005 struct commit_queue_entry *entry;
1006 int nscrolled = 0;
1008 entry = TAILQ_FIRST(&commits->head);
1009 if (*first_displayed_entry == entry)
1010 return;
1012 entry = *first_displayed_entry;
1013 while (entry && nscrolled < maxscroll) {
1014 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1015 if (entry) {
1016 *first_displayed_entry = entry;
1017 nscrolled++;
1022 static const struct got_error *
1023 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1024 struct commit_queue_entry *last_displayed_entry,
1025 struct commit_queue *commits, struct got_commit_graph *graph,
1026 struct got_repository *repo, const char *path)
1028 const struct got_error *err = NULL;
1029 struct commit_queue_entry *pentry;
1030 int nscrolled = 0;
1032 do {
1033 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1034 if (pentry == NULL) {
1035 err = fetch_next_commit(&pentry, last_displayed_entry,
1036 commits, graph, repo, path);
1037 if (err || pentry == NULL)
1038 break;
1040 last_displayed_entry = pentry;
1042 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1043 if (pentry == NULL)
1044 break;
1045 *first_displayed_entry = pentry;
1046 } while (++nscrolled < maxscroll);
1048 return err;
1051 static const struct got_error *
1052 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1053 struct commit_queue_entry *entry, struct got_repository *repo)
1055 const struct got_error *err;
1056 struct got_object *obj1 = NULL, *obj2 = NULL;
1057 struct got_object_qid *parent_id;
1058 struct tog_view *diff_view;
1060 err = got_object_open(&obj2, repo, entry->id);
1061 if (err)
1062 return err;
1064 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1065 if (parent_id) {
1066 err = got_object_open(&obj1, repo, parent_id->id);
1067 if (err)
1068 goto done;
1071 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1072 if (diff_view == NULL) {
1073 err = got_error_from_errno();
1074 goto done;
1077 err = open_diff_view(diff_view, obj1, obj2, repo);
1078 if (err == NULL)
1079 *new_view = diff_view;
1080 done:
1081 if (obj1)
1082 got_object_close(obj1);
1083 if (obj2)
1084 got_object_close(obj2);
1085 return err;
1088 static const struct got_error *
1089 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1090 struct commit_queue_entry *entry, struct got_repository *repo)
1092 const struct got_error *err = NULL;
1093 struct got_tree_object *tree;
1094 struct tog_view *tree_view;
1096 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1097 if (err)
1098 return err;
1100 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1101 if (tree_view == NULL)
1102 return got_error_from_errno();
1104 err = open_tree_view(tree_view, tree, entry->id, repo);
1105 if (err)
1106 got_object_tree_close(tree);
1107 else
1108 *new_view = tree_view;
1109 return err;
1112 static const struct got_error *
1113 set_child_log_view(struct tog_view *view, struct tog_view *child)
1115 struct tog_log_view_state *s = &view->state.log;
1116 struct tog_diff_view_state *ds;
1117 struct commit_queue_entry *commit, *child_entry = NULL;
1118 int selected_idx = 0;
1120 if (child->type != TOG_VIEW_DIFF)
1121 return NULL;
1122 ds = &child->state.diff;
1124 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1125 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1126 child_entry = commit;
1127 break;
1130 if (child_entry == NULL)
1131 return NULL;
1133 commit = s->first_displayed_entry;
1134 while (commit) {
1135 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1136 s->selected_entry = child_entry;
1137 s->selected = selected_idx;
1138 break;
1140 if (commit == s->last_displayed_entry)
1141 break;
1142 selected_idx++;
1143 commit = TAILQ_NEXT(commit, entry);
1146 return show_log_view(view);
1149 static const struct got_error *
1150 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1151 struct got_repository *repo, const char *path)
1153 const struct got_error *err = NULL;
1154 struct got_object_id *head_id = NULL;
1155 int nfetched;
1156 struct tog_log_view_state *s = &view->state.log;
1158 err = got_repo_map_path(&s->in_repo_path, repo, path);
1159 if (err != NULL)
1160 goto done;
1162 err = get_head_commit_id(&head_id, repo);
1163 if (err)
1164 return err;
1166 /* The graph contains all commits. */
1167 err = got_commit_graph_open(&s->graph, head_id, "/", 0, repo);
1168 if (err)
1169 goto done;
1170 /* The commit queue contains a subset of commits filtered by path. */
1171 TAILQ_INIT(&s->commits.head);
1172 s->commits.ncommits = 0;
1174 /* Populate commit graph with a sufficient number of commits. */
1175 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1176 start_id, repo);
1177 if (err)
1178 goto done;
1181 * Open the initial batch of commits, sorted in commit graph order.
1182 * We keep all commits open throughout the lifetime of the log view
1183 * in order to avoid having to re-fetch commits from disk while
1184 * updating the display.
1186 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1187 repo, s->in_repo_path);
1188 if (err) {
1189 if (err->code != GOT_ERR_ITER_COMPLETED)
1190 goto done;
1191 err = NULL;
1194 s->first_displayed_entry =
1195 TAILQ_FIRST(&s->commits.head);
1196 s->selected_entry = s->first_displayed_entry;
1197 s->repo = repo;
1199 view->show = show_log_view;
1200 view->input = input_log_view;
1201 view->close = close_log_view;
1202 view->set_child = set_child_log_view;
1203 done:
1204 free(head_id);
1205 return err;
1208 static const struct got_error *
1209 close_log_view(struct tog_view *view)
1211 struct tog_log_view_state *s = &view->state.log;
1213 if (s->graph)
1214 got_commit_graph_close(s->graph);
1215 free_commits(&s->commits);
1216 free(s->in_repo_path);
1217 return NULL;
1220 static const struct got_error *
1221 update_diff_child_view(struct tog_view *parent,
1222 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1224 const struct got_error *err = NULL;
1225 struct tog_diff_view_state *ds;
1226 struct got_object *obj1 = NULL, *obj2 = NULL;
1227 struct got_object_qid *parent_id;
1228 struct tog_view *child_view = parent->child;
1230 if (child_view == NULL)
1231 return NULL;
1232 if (child_view->type != TOG_VIEW_DIFF)
1233 return NULL;
1234 ds = &child_view->state.diff;
1235 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1236 return NULL;
1238 err = got_object_open(&obj2, repo, selected_entry->id);
1239 if (err)
1240 return err;
1242 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1243 if (parent_id) {
1244 err = got_object_open(&obj1, repo, parent_id->id);
1245 if (err)
1246 goto done;
1249 err = close_diff_view(child_view);
1250 if (err)
1251 goto done;
1253 err = open_diff_view(child_view, obj1, obj2, repo);
1254 if (err)
1255 goto done;
1256 done:
1257 if (obj1)
1258 got_object_close(obj1);
1259 if (obj2)
1260 got_object_close(obj2);
1261 return err;
1264 static const struct got_error *
1265 show_log_view(struct tog_view *view)
1267 const struct got_error *err = NULL;
1268 struct tog_log_view_state *s = &view->state.log;
1270 err = draw_commits(view, &s->last_displayed_entry,
1271 &s->selected_entry, s->first_displayed_entry,
1272 &s->commits, s->selected, view->nlines, s->graph,
1273 s->repo, s->in_repo_path);
1274 if (err)
1275 return err;
1277 return update_diff_child_view(view, s->selected_entry, s->repo);
1280 static const struct got_error *
1281 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1282 struct tog_view *view, int ch)
1284 const struct got_error *err = NULL;
1285 struct tog_log_view_state *s = &view->state.log;
1287 switch (ch) {
1288 case 'k':
1289 case KEY_UP:
1290 case '[':
1291 if (s->selected > 0)
1292 s->selected--;
1293 if (s->selected > 0)
1294 break;
1295 scroll_up(&s->first_displayed_entry, 1,
1296 &s->commits);
1297 break;
1298 case KEY_PPAGE:
1299 if (TAILQ_FIRST(&s->commits.head) ==
1300 s->first_displayed_entry) {
1301 s->selected = 0;
1302 break;
1304 scroll_up(&s->first_displayed_entry,
1305 view->nlines, &s->commits);
1306 break;
1307 case 'j':
1308 case KEY_DOWN:
1309 case ']':
1310 if (s->selected < MIN(view->nlines - 2,
1311 s->commits.ncommits - 1)) {
1312 s->selected++;
1313 break;
1315 err = scroll_down(&s->first_displayed_entry, 1,
1316 s->last_displayed_entry, &s->commits,
1317 s->graph, s->repo, s->in_repo_path);
1318 if (err) {
1319 if (err->code != GOT_ERR_ITER_COMPLETED)
1320 break;
1321 err = NULL;
1323 break;
1324 case KEY_NPAGE: {
1325 struct commit_queue_entry *first;
1326 first = s->first_displayed_entry;
1327 err = scroll_down(&s->first_displayed_entry,
1328 view->nlines, s->last_displayed_entry,
1329 &s->commits, s->graph, s->repo,
1330 s->in_repo_path);
1331 if (err == NULL)
1332 break;
1333 if (err->code != GOT_ERR_ITER_COMPLETED)
1334 break;
1335 if (first == s->first_displayed_entry &&
1336 s->selected < MIN(view->nlines - 2,
1337 s->commits.ncommits - 1)) {
1338 /* can't scroll further down */
1339 s->selected = MIN(view->nlines - 2,
1340 s->commits.ncommits - 1);
1342 err = NULL;
1343 break;
1345 case KEY_RESIZE:
1346 if (s->selected > view->nlines - 2)
1347 s->selected = view->nlines - 2;
1348 if (s->selected > s->commits.ncommits - 1)
1349 s->selected = s->commits.ncommits - 1;
1350 break;
1351 case KEY_ENTER:
1352 case '\r':
1353 err = show_commit(new_view, view, s->selected_entry,
1354 s->repo);
1355 break;
1356 case 't':
1357 err = browse_commit(new_view, view, s->selected_entry,
1358 s->repo);
1359 break;
1360 default:
1361 break;
1364 return err;
1367 static const struct got_error *
1368 cmd_log(int argc, char *argv[])
1370 const struct got_error *error;
1371 struct got_repository *repo = NULL;
1372 struct got_object_id *start_id = NULL;
1373 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1374 char *start_commit = NULL;
1375 int ch;
1376 struct tog_view *view;
1378 #ifndef PROFILE
1379 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1380 == -1)
1381 err(1, "pledge");
1382 #endif
1384 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1385 switch (ch) {
1386 case 'c':
1387 start_commit = optarg;
1388 break;
1389 case 'r':
1390 repo_path = realpath(optarg, NULL);
1391 if (repo_path == NULL)
1392 err(1, "-r option");
1393 break;
1394 default:
1395 usage();
1396 /* NOTREACHED */
1400 argc -= optind;
1401 argv += optind;
1403 if (argc == 0)
1404 path = strdup("");
1405 else if (argc == 1)
1406 path = strdup(argv[0]);
1407 else
1408 usage_log();
1409 if (path == NULL)
1410 return got_error_from_errno();
1412 cwd = getcwd(NULL, 0);
1413 if (cwd == NULL) {
1414 error = got_error_from_errno();
1415 goto done;
1417 if (repo_path == NULL) {
1418 repo_path = strdup(cwd);
1419 if (repo_path == NULL) {
1420 error = got_error_from_errno();
1421 goto done;
1425 error = got_repo_open(&repo, repo_path);
1426 if (error != NULL)
1427 goto done;
1429 if (start_commit == NULL) {
1430 error = get_head_commit_id(&start_id, repo);
1431 if (error != NULL)
1432 goto done;
1433 } else {
1434 struct got_object *obj;
1435 error = got_object_open_by_id_str(&obj, repo, start_commit);
1436 if (error == NULL) {
1437 start_id = got_object_id_dup(got_object_get_id(obj));
1438 if (start_id == NULL)
1439 error = got_error_from_errno();
1440 goto done;
1443 if (error != NULL)
1444 goto done;
1446 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1447 if (view == NULL) {
1448 error = got_error_from_errno();
1449 goto done;
1451 error = open_log_view(view, start_id, repo, path);
1452 if (error)
1453 goto done;
1454 error = view_loop(view);
1455 done:
1456 free(repo_path);
1457 free(cwd);
1458 free(path);
1459 free(start_id);
1460 if (repo)
1461 got_repo_close(repo);
1462 return error;
1465 __dead static void
1466 usage_diff(void)
1468 endwin();
1469 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1470 getprogname());
1471 exit(1);
1474 static char *
1475 parse_next_line(FILE *f, size_t *len)
1477 char *line;
1478 size_t linelen;
1479 size_t lineno;
1480 const char delim[3] = { '\0', '\0', '\0'};
1482 line = fparseln(f, &linelen, &lineno, delim, 0);
1483 if (len)
1484 *len = linelen;
1485 return line;
1488 static const struct got_error *
1489 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1490 int *last_displayed_line, int *eof, int max_lines,
1491 char * header)
1493 const struct got_error *err;
1494 int nlines = 0, nprinted = 0;
1495 char *line;
1496 size_t len;
1497 wchar_t *wline;
1498 int width;
1500 rewind(f);
1501 werase(view->window);
1503 if (header) {
1504 err = format_line(&wline, &width, header, view->ncols);
1505 if (err) {
1506 return err;
1509 if (view_needs_focus_indication(view))
1510 wstandout(view->window);
1511 waddwstr(view->window, wline);
1512 if (view_needs_focus_indication(view))
1513 wstandend(view->window);
1514 if (width < view->ncols)
1515 waddch(view->window, '\n');
1517 if (max_lines <= 1)
1518 return NULL;
1519 max_lines--;
1522 *eof = 0;
1523 while (nprinted < max_lines) {
1524 line = parse_next_line(f, &len);
1525 if (line == NULL) {
1526 *eof = 1;
1527 break;
1529 if (++nlines < *first_displayed_line) {
1530 free(line);
1531 continue;
1534 err = format_line(&wline, &width, line, view->ncols);
1535 if (err) {
1536 free(line);
1537 return err;
1539 waddwstr(view->window, wline);
1540 if (width < view->ncols)
1541 waddch(view->window, '\n');
1542 if (++nprinted == 1)
1543 *first_displayed_line = nlines;
1544 free(line);
1545 free(wline);
1546 wline = NULL;
1548 *last_displayed_line = nlines;
1550 view_vborder(view);
1552 return NULL;
1555 static const struct got_error *
1556 open_diff_view(struct tog_view *view, struct got_object *obj1,
1557 struct got_object *obj2, struct got_repository *repo)
1559 const struct got_error *err;
1560 FILE *f;
1562 if (obj1 != NULL && obj2 != NULL &&
1563 got_object_get_type(obj1) != got_object_get_type(obj2))
1564 return got_error(GOT_ERR_OBJ_TYPE);
1566 f = got_opentemp();
1567 if (f == NULL)
1568 return got_error_from_errno();
1570 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1571 case GOT_OBJ_TYPE_BLOB:
1572 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1573 repo, f);
1574 break;
1575 case GOT_OBJ_TYPE_TREE:
1576 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1577 break;
1578 case GOT_OBJ_TYPE_COMMIT:
1579 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1580 break;
1581 default:
1582 return got_error(GOT_ERR_OBJ_TYPE);
1585 fflush(f);
1587 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1588 view->state.diff.id2 = got_object_get_id(obj2);
1589 view->state.diff.f = f;
1590 view->state.diff.first_displayed_line = 1;
1591 view->state.diff.last_displayed_line = view->nlines;
1593 view->show = show_diff_view;
1594 view->input = input_diff_view;
1595 view->close = close_diff_view;
1597 return NULL;
1600 static const struct got_error *
1601 close_diff_view(struct tog_view *view)
1603 const struct got_error *err = NULL;
1605 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1606 err = got_error_from_errno();
1607 return err;
1610 static const struct got_error *
1611 show_diff_view(struct tog_view *view)
1613 const struct got_error *err;
1614 struct tog_diff_view_state *s = &view->state.diff;
1615 char *id_str1 = NULL, *id_str2, *header;
1617 if (s->id1) {
1618 err = got_object_id_str(&id_str1, s->id1);
1619 if (err)
1620 return err;
1622 err = got_object_id_str(&id_str2, s->id2);
1623 if (err)
1624 return err;
1626 if (asprintf(&header, "diff: %s %s",
1627 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1628 err = got_error_from_errno();
1629 free(id_str1);
1630 free(id_str2);
1631 return err;
1633 free(id_str1);
1634 free(id_str2);
1636 return draw_file(view, s->f, &s->first_displayed_line,
1637 &s->last_displayed_line, &s->eof, view->nlines,
1638 header);
1641 static const struct got_error *
1642 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1643 struct tog_view *view, int ch)
1645 const struct got_error *err = NULL;
1646 struct tog_diff_view_state *s = &view->state.diff;
1647 int i;
1649 switch (ch) {
1650 case 'k':
1651 case KEY_UP:
1652 if (s->first_displayed_line > 1)
1653 s->first_displayed_line--;
1654 break;
1655 case KEY_PPAGE:
1656 i = 0;
1657 while (i++ < view->nlines - 1 &&
1658 s->first_displayed_line > 1)
1659 s->first_displayed_line--;
1660 break;
1661 case 'j':
1662 case KEY_DOWN:
1663 if (!s->eof)
1664 s->first_displayed_line++;
1665 break;
1666 case KEY_NPAGE:
1667 case ' ':
1668 i = 0;
1669 while (!s->eof && i++ < view->nlines - 1) {
1670 char *line;
1671 line = parse_next_line(s->f, NULL);
1672 s->first_displayed_line++;
1673 if (line == NULL)
1674 break;
1676 break;
1677 case '[':
1678 case ']': {
1679 struct tog_log_view_state *ls;
1680 struct commit_queue_entry *entry;
1681 struct tog_view *diff_view;
1683 if (view->parent == NULL)
1684 break;
1685 if (view->parent->type != TOG_VIEW_LOG)
1686 break;
1687 ls = &view->parent->state.log;
1689 if (ch == '[') {
1690 entry = TAILQ_PREV(ls->selected_entry,
1691 commit_queue_head, entry);
1692 } else {
1693 entry = TAILQ_NEXT(ls->selected_entry, entry);
1694 if (entry == NULL) {
1695 err = fetch_next_commit(&entry,
1696 ls->selected_entry,
1697 &ls->commits, ls->graph,
1698 ls->repo, ls->in_repo_path);
1699 if (err)
1700 break;
1703 if (entry == NULL)
1704 break;
1705 err = show_commit(&diff_view, view->parent,
1706 entry, ls->repo);
1707 if (err)
1708 break;
1709 *new_view = diff_view;
1710 *dead_view = view;
1711 break;
1713 default:
1714 break;
1717 return err;
1720 static const struct got_error *
1721 cmd_diff(int argc, char *argv[])
1723 const struct got_error *error = NULL;
1724 struct got_repository *repo = NULL;
1725 struct got_object *obj1 = NULL, *obj2 = NULL;
1726 char *repo_path = NULL;
1727 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1728 int ch;
1729 struct tog_view *view;
1731 #ifndef PROFILE
1732 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1733 == -1)
1734 err(1, "pledge");
1735 #endif
1737 while ((ch = getopt(argc, argv, "")) != -1) {
1738 switch (ch) {
1739 default:
1740 usage();
1741 /* NOTREACHED */
1745 argc -= optind;
1746 argv += optind;
1748 if (argc == 0) {
1749 usage_diff(); /* TODO show local worktree changes */
1750 } else if (argc == 2) {
1751 repo_path = getcwd(NULL, 0);
1752 if (repo_path == NULL)
1753 return got_error_from_errno();
1754 obj_id_str1 = argv[0];
1755 obj_id_str2 = argv[1];
1756 } else if (argc == 3) {
1757 repo_path = realpath(argv[0], NULL);
1758 if (repo_path == NULL)
1759 return got_error_from_errno();
1760 obj_id_str1 = argv[1];
1761 obj_id_str2 = argv[2];
1762 } else
1763 usage_diff();
1765 error = got_repo_open(&repo, repo_path);
1766 free(repo_path);
1767 if (error)
1768 goto done;
1770 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1771 if (error)
1772 goto done;
1774 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1775 if (error)
1776 goto done;
1778 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1779 if (view == NULL) {
1780 error = got_error_from_errno();
1781 goto done;
1783 error = open_diff_view(view, obj1, obj2, repo);
1784 if (error)
1785 goto done;
1786 error = view_loop(view);
1787 done:
1788 got_repo_close(repo);
1789 if (obj1)
1790 got_object_close(obj1);
1791 if (obj2)
1792 got_object_close(obj2);
1793 return error;
1796 __dead static void
1797 usage_blame(void)
1799 endwin();
1800 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1801 getprogname());
1802 exit(1);
1805 struct tog_blame_line {
1806 int annotated;
1807 struct got_object_id *id;
1810 static const struct got_error *
1811 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1812 const char *path, struct tog_blame_line *lines, int nlines,
1813 int blame_complete, int selected_line, int *first_displayed_line,
1814 int *last_displayed_line, int *eof, int max_lines)
1816 const struct got_error *err;
1817 int lineno = 0, nprinted = 0;
1818 char *line;
1819 size_t len;
1820 wchar_t *wline;
1821 int width, wlimit;
1822 struct tog_blame_line *blame_line;
1823 struct got_object_id *prev_id = NULL;
1824 char *id_str;
1826 err = got_object_id_str(&id_str, id);
1827 if (err)
1828 return err;
1830 rewind(f);
1831 werase(view->window);
1833 if (asprintf(&line, "commit: %s", id_str) == -1) {
1834 err = got_error_from_errno();
1835 free(id_str);
1836 return err;
1839 err = format_line(&wline, &width, line, view->ncols);
1840 free(line);
1841 line = NULL;
1842 if (view_needs_focus_indication(view))
1843 wstandout(view->window);
1844 waddwstr(view->window, wline);
1845 if (view_needs_focus_indication(view))
1846 wstandend(view->window);
1847 free(wline);
1848 wline = NULL;
1849 if (width < view->ncols)
1850 waddch(view->window, '\n');
1852 if (asprintf(&line, "[%d/%d] %s%s",
1853 *first_displayed_line - 1 + selected_line, nlines,
1854 blame_complete ? "" : "annotating ", path) == -1) {
1855 free(id_str);
1856 return got_error_from_errno();
1858 free(id_str);
1859 err = format_line(&wline, &width, line, view->ncols);
1860 free(line);
1861 line = NULL;
1862 if (err)
1863 return err;
1864 waddwstr(view->window, wline);
1865 free(wline);
1866 wline = NULL;
1867 if (width < view->ncols)
1868 waddch(view->window, '\n');
1870 *eof = 0;
1871 while (nprinted < max_lines - 2) {
1872 line = parse_next_line(f, &len);
1873 if (line == NULL) {
1874 *eof = 1;
1875 break;
1877 if (++lineno < *first_displayed_line) {
1878 free(line);
1879 continue;
1882 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1883 err = format_line(&wline, &width, line, wlimit);
1884 if (err) {
1885 free(line);
1886 return err;
1889 if (nprinted == selected_line - 1)
1890 wstandout(view->window);
1892 blame_line = &lines[lineno - 1];
1893 if (blame_line->annotated && prev_id &&
1894 got_object_id_cmp(prev_id, blame_line->id) == 0)
1895 waddstr(view->window, " ");
1896 else if (blame_line->annotated) {
1897 char *id_str;
1898 err = got_object_id_str(&id_str, blame_line->id);
1899 if (err) {
1900 free(line);
1901 free(wline);
1902 return err;
1904 wprintw(view->window, "%.8s ", id_str);
1905 free(id_str);
1906 prev_id = blame_line->id;
1907 } else {
1908 waddstr(view->window, "........ ");
1909 prev_id = NULL;
1912 waddwstr(view->window, wline);
1913 while (width < wlimit) {
1914 waddch(view->window, ' ');
1915 width++;
1917 if (nprinted == selected_line - 1)
1918 wstandend(view->window);
1919 if (++nprinted == 1)
1920 *first_displayed_line = lineno;
1921 free(line);
1922 free(wline);
1923 wline = NULL;
1925 *last_displayed_line = lineno;
1927 view_vborder(view);
1929 return NULL;
1932 static const struct got_error *
1933 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1935 const struct got_error *err = NULL;
1936 struct tog_blame_cb_args *a = arg;
1937 struct tog_blame_line *line;
1939 if (nlines != a->nlines ||
1940 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1941 return got_error(GOT_ERR_RANGE);
1943 if (pthread_mutex_lock(a->mutex) != 0)
1944 return got_error_from_errno();
1946 if (*a->quit) { /* user has quit the blame view */
1947 err = got_error(GOT_ERR_ITER_COMPLETED);
1948 goto done;
1951 if (lineno == -1)
1952 goto done; /* no change in this commit */
1954 line = &a->lines[lineno - 1];
1955 if (line->annotated)
1956 goto done;
1958 line->id = got_object_id_dup(id);
1959 if (line->id == NULL) {
1960 err = got_error_from_errno();
1961 goto done;
1963 line->annotated = 1;
1965 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1966 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1967 a->last_displayed_line, a->eof, a->view->nlines);
1968 done:
1969 if (pthread_mutex_unlock(a->mutex) != 0)
1970 return got_error_from_errno();
1971 return err;
1974 static void *
1975 blame_thread(void *arg)
1977 const struct got_error *err;
1978 struct tog_blame_thread_args *ta = arg;
1979 struct tog_blame_cb_args *a = ta->cb_args;
1981 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1982 blame_cb, ta->cb_args);
1984 if (pthread_mutex_lock(a->mutex) != 0)
1985 return (void *)got_error_from_errno();
1987 got_repo_close(ta->repo);
1988 ta->repo = NULL;
1989 *ta->complete = 1;
1990 if (!err)
1991 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1992 a->lines, a->nlines, 1, *a->selected_line,
1993 a->first_displayed_line, a->last_displayed_line, a->eof,
1994 a->view->nlines);
1996 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1997 err = got_error_from_errno();
1999 return (void *)err;
2002 static struct got_object_id *
2003 get_selected_commit_id(struct tog_blame_line *lines,
2004 int first_displayed_line, int selected_line)
2006 struct tog_blame_line *line;
2008 line = &lines[first_displayed_line - 1 + selected_line - 1];
2009 if (!line->annotated)
2010 return NULL;
2012 return line->id;
2015 static const struct got_error *
2016 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2017 struct tog_blame_line *lines, int first_displayed_line,
2018 int selected_line, struct got_repository *repo)
2020 const struct got_error *err = NULL;
2021 struct got_commit_object *commit = NULL;
2022 struct got_object_id *selected_id;
2023 struct got_object_qid *pid;
2025 *pobj = NULL;
2026 *obj = NULL;
2028 selected_id = get_selected_commit_id(lines,
2029 first_displayed_line, selected_line);
2030 if (selected_id == NULL)
2031 return NULL;
2033 err = got_object_open(obj, repo, selected_id);
2034 if (err)
2035 goto done;
2037 err = got_object_commit_open(&commit, repo, *obj);
2038 if (err)
2039 goto done;
2041 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2042 if (pid) {
2043 err = got_object_open(pobj, repo, pid->id);
2044 if (err)
2045 goto done;
2047 done:
2048 if (commit)
2049 got_object_commit_close(commit);
2050 return err;
2053 static const struct got_error *
2054 stop_blame(struct tog_blame *blame)
2056 const struct got_error *err = NULL;
2057 int i;
2059 if (blame->thread) {
2060 if (pthread_join(blame->thread, (void **)&err) != 0)
2061 err = got_error_from_errno();
2062 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2063 err = NULL;
2064 blame->thread = NULL;
2066 if (blame->thread_args.repo) {
2067 got_repo_close(blame->thread_args.repo);
2068 blame->thread_args.repo = NULL;
2070 if (blame->f) {
2071 fclose(blame->f);
2072 blame->f = NULL;
2074 for (i = 0; i < blame->nlines; i++)
2075 free(blame->lines[i].id);
2076 free(blame->lines);
2077 blame->lines = NULL;
2078 free(blame->cb_args.commit_id);
2079 blame->cb_args.commit_id = NULL;
2081 return err;
2084 static const struct got_error *
2085 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2086 struct tog_view *view, int *blame_complete,
2087 int *first_displayed_line, int *last_displayed_line,
2088 int *selected_line, int *done, int *eof, const char *path,
2089 struct got_object_id *commit_id,
2090 struct got_repository *repo)
2092 const struct got_error *err = NULL;
2093 struct got_blob_object *blob = NULL;
2094 struct got_repository *thread_repo = NULL;
2095 struct got_object_id *obj_id = NULL;
2096 struct got_object *obj = NULL;
2098 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2099 if (err)
2100 goto done;
2102 err = got_object_open(&obj, repo, obj_id);
2103 if (err)
2104 goto done;
2106 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2107 err = got_error(GOT_ERR_OBJ_TYPE);
2108 goto done;
2111 err = got_object_blob_open(&blob, repo, obj, 8192);
2112 if (err)
2113 goto done;
2114 blame->f = got_opentemp();
2115 if (blame->f == NULL) {
2116 err = got_error_from_errno();
2117 goto done;
2119 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2120 blame->f, blob);
2121 if (err)
2122 goto done;
2124 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2125 if (blame->lines == NULL) {
2126 err = got_error_from_errno();
2127 goto done;
2130 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2131 if (err)
2132 goto done;
2134 blame->cb_args.view = view;
2135 blame->cb_args.lines = blame->lines;
2136 blame->cb_args.nlines = blame->nlines;
2137 blame->cb_args.mutex = mutex;
2138 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2139 if (blame->cb_args.commit_id == NULL) {
2140 err = got_error_from_errno();
2141 goto done;
2143 blame->cb_args.f = blame->f;
2144 blame->cb_args.path = path;
2145 blame->cb_args.first_displayed_line = first_displayed_line;
2146 blame->cb_args.selected_line = selected_line;
2147 blame->cb_args.last_displayed_line = last_displayed_line;
2148 blame->cb_args.quit = done;
2149 blame->cb_args.eof = eof;
2151 blame->thread_args.path = path;
2152 blame->thread_args.repo = thread_repo;
2153 blame->thread_args.cb_args = &blame->cb_args;
2154 blame->thread_args.complete = blame_complete;
2155 *blame_complete = 0;
2157 if (pthread_create(&blame->thread, NULL, blame_thread,
2158 &blame->thread_args) != 0) {
2159 err = got_error_from_errno();
2160 goto done;
2163 done:
2164 if (blob)
2165 got_object_blob_close(blob);
2166 free(obj_id);
2167 if (obj)
2168 got_object_close(obj);
2169 if (err)
2170 stop_blame(blame);
2171 return err;
2174 static const struct got_error *
2175 open_blame_view(struct tog_view *view, char *path,
2176 struct got_object_id *commit_id, struct got_repository *repo)
2178 const struct got_error *err = NULL;
2179 struct tog_blame_view_state *s = &view->state.blame;
2181 SIMPLEQ_INIT(&s->blamed_commits);
2183 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2184 return got_error_from_errno();
2186 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2187 if (err)
2188 return err;
2190 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2191 s->first_displayed_line = 1;
2192 s->last_displayed_line = view->nlines;
2193 s->selected_line = 1;
2194 s->blame_complete = 0;
2195 s->path = path;
2196 if (s->path == NULL)
2197 return got_error_from_errno();
2198 s->repo = repo;
2199 s->commit_id = commit_id;
2200 memset(&s->blame, 0, sizeof(s->blame));
2202 view->show = show_blame_view;
2203 view->input = input_blame_view;
2204 view->close = close_blame_view;
2206 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2207 &s->first_displayed_line, &s->last_displayed_line,
2208 &s->selected_line, &s->done, &s->eof, s->path,
2209 s->blamed_commit->id, s->repo);
2212 static const struct got_error *
2213 close_blame_view(struct tog_view *view)
2215 const struct got_error *err = NULL;
2216 struct tog_blame_view_state *s = &view->state.blame;
2218 if (s->blame.thread)
2219 err = stop_blame(&s->blame);
2221 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2222 struct got_object_qid *blamed_commit;
2223 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2224 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2225 got_object_qid_free(blamed_commit);
2228 free(s->path);
2230 return err;
2233 static const struct got_error *
2234 show_blame_view(struct tog_view *view)
2236 const struct got_error *err = NULL;
2237 struct tog_blame_view_state *s = &view->state.blame;
2239 if (pthread_mutex_lock(&s->mutex) != 0)
2240 return got_error_from_errno();
2242 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2243 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2244 s->selected_line, &s->first_displayed_line,
2245 &s->last_displayed_line, &s->eof, view->nlines);
2247 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2248 err = got_error_from_errno();
2250 return err;
2253 static const struct got_error *
2254 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2255 struct tog_view *view, int ch)
2257 const struct got_error *err = NULL, *thread_err = NULL;
2258 struct got_object *obj = NULL, *pobj = NULL;
2259 struct tog_view *diff_view;
2260 struct tog_blame_view_state *s = &view->state.blame;
2262 if (pthread_mutex_lock(&s->mutex) != 0) {
2263 err = got_error_from_errno();
2264 goto done;
2267 switch (ch) {
2268 case 'q':
2269 s->done = 1;
2270 if (pthread_mutex_unlock(&s->mutex) != 0) {
2271 err = got_error_from_errno();
2272 goto done;
2274 return stop_blame(&s->blame);
2275 case 'k':
2276 case KEY_UP:
2277 if (s->selected_line > 1)
2278 s->selected_line--;
2279 else if (s->selected_line == 1 &&
2280 s->first_displayed_line > 1)
2281 s->first_displayed_line--;
2282 break;
2283 case KEY_PPAGE:
2284 if (s->first_displayed_line == 1) {
2285 s->selected_line = 1;
2286 break;
2288 if (s->first_displayed_line > view->nlines - 2)
2289 s->first_displayed_line -=
2290 (view->nlines - 2);
2291 else
2292 s->first_displayed_line = 1;
2293 break;
2294 case 'j':
2295 case KEY_DOWN:
2296 if (s->selected_line < view->nlines - 2 &&
2297 s->first_displayed_line +
2298 s->selected_line <= s->blame.nlines)
2299 s->selected_line++;
2300 else if (s->last_displayed_line <
2301 s->blame.nlines)
2302 s->first_displayed_line++;
2303 break;
2304 case 'b':
2305 case 'p': {
2306 struct got_object_id *id;
2307 id = get_selected_commit_id(s->blame.lines,
2308 s->first_displayed_line, s->selected_line);
2309 if (id == NULL || got_object_id_cmp(id,
2310 s->blamed_commit->id) == 0)
2311 break;
2312 err = open_selected_commit(&pobj, &obj,
2313 s->blame.lines, s->first_displayed_line,
2314 s->selected_line, s->repo);
2315 if (err)
2316 break;
2317 if (pobj == NULL && obj == NULL)
2318 break;
2319 if (ch == 'p' && pobj == NULL)
2320 break;
2321 s->done = 1;
2322 if (pthread_mutex_unlock(&s->mutex) != 0) {
2323 err = got_error_from_errno();
2324 goto done;
2326 thread_err = stop_blame(&s->blame);
2327 s->done = 0;
2328 if (pthread_mutex_lock(&s->mutex) != 0) {
2329 err = got_error_from_errno();
2330 goto done;
2332 if (thread_err)
2333 break;
2334 id = got_object_get_id(ch == 'b' ? obj : pobj);
2335 got_object_close(obj);
2336 obj = NULL;
2337 if (pobj) {
2338 got_object_close(pobj);
2339 pobj = NULL;
2341 err = got_object_qid_alloc(&s->blamed_commit, id);
2342 if (err)
2343 goto done;
2344 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2345 s->blamed_commit, entry);
2346 err = run_blame(&s->blame, &s->mutex, view,
2347 &s->blame_complete,
2348 &s->first_displayed_line,
2349 &s->last_displayed_line,
2350 &s->selected_line, &s->done, &s->eof,
2351 s->path, s->blamed_commit->id, s->repo);
2352 if (err)
2353 break;
2354 break;
2356 case 'B': {
2357 struct got_object_qid *first;
2358 first = SIMPLEQ_FIRST(&s->blamed_commits);
2359 if (!got_object_id_cmp(first->id, s->commit_id))
2360 break;
2361 s->done = 1;
2362 if (pthread_mutex_unlock(&s->mutex) != 0) {
2363 err = got_error_from_errno();
2364 goto done;
2366 thread_err = stop_blame(&s->blame);
2367 s->done = 0;
2368 if (pthread_mutex_lock(&s->mutex) != 0) {
2369 err = got_error_from_errno();
2370 goto done;
2372 if (thread_err)
2373 break;
2374 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2375 got_object_qid_free(s->blamed_commit);
2376 s->blamed_commit =
2377 SIMPLEQ_FIRST(&s->blamed_commits);
2378 err = run_blame(&s->blame, &s->mutex, view,
2379 &s->blame_complete,
2380 &s->first_displayed_line,
2381 &s->last_displayed_line,
2382 &s->selected_line, &s->done, &s->eof, s->path,
2383 s->blamed_commit->id, s->repo);
2384 if (err)
2385 break;
2386 break;
2388 case KEY_ENTER:
2389 case '\r':
2390 err = open_selected_commit(&pobj, &obj,
2391 s->blame.lines, s->first_displayed_line,
2392 s->selected_line, s->repo);
2393 if (err)
2394 break;
2395 if (pobj == NULL && obj == NULL)
2396 break;
2397 diff_view = view_open(0, 0, 0, 0, view,
2398 TOG_VIEW_DIFF);
2399 if (diff_view == NULL) {
2400 err = got_error_from_errno();
2401 break;
2403 err = open_diff_view(diff_view, pobj, obj,
2404 s->repo);
2405 if (err) {
2406 view_close(diff_view);
2407 break;
2409 *new_view = diff_view;
2410 if (pobj) {
2411 got_object_close(pobj);
2412 pobj = NULL;
2414 got_object_close(obj);
2415 obj = NULL;
2416 if (err)
2417 break;
2418 break;
2419 case KEY_NPAGE:
2420 case ' ':
2421 if (s->last_displayed_line >= s->blame.nlines &&
2422 s->selected_line < view->nlines - 2) {
2423 s->selected_line = MIN(s->blame.nlines,
2424 view->nlines - 2);
2425 break;
2427 if (s->last_displayed_line + view->nlines - 2
2428 <= s->blame.nlines)
2429 s->first_displayed_line +=
2430 view->nlines - 2;
2431 else
2432 s->first_displayed_line =
2433 s->blame.nlines -
2434 (view->nlines - 3);
2435 break;
2436 case KEY_RESIZE:
2437 if (s->selected_line > view->nlines - 2) {
2438 s->selected_line = MIN(s->blame.nlines,
2439 view->nlines - 2);
2441 break;
2442 default:
2443 break;
2446 if (pthread_mutex_unlock(&s->mutex) != 0)
2447 err = got_error_from_errno();
2448 done:
2449 if (pobj)
2450 got_object_close(pobj);
2451 return thread_err ? thread_err : err;
2454 static const struct got_error *
2455 cmd_blame(int argc, char *argv[])
2457 const struct got_error *error;
2458 struct got_repository *repo = NULL;
2459 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2460 struct got_object_id *commit_id = NULL;
2461 char *commit_id_str = NULL;
2462 int ch;
2463 struct tog_view *view;
2465 #ifndef PROFILE
2466 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2467 == -1)
2468 err(1, "pledge");
2469 #endif
2471 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2472 switch (ch) {
2473 case 'c':
2474 commit_id_str = optarg;
2475 break;
2476 case 'r':
2477 repo_path = realpath(optarg, NULL);
2478 if (repo_path == NULL)
2479 err(1, "-r option");
2480 break;
2481 default:
2482 usage();
2483 /* NOTREACHED */
2487 argc -= optind;
2488 argv += optind;
2490 if (argc == 1)
2491 path = argv[0];
2492 else
2493 usage_blame();
2495 cwd = getcwd(NULL, 0);
2496 if (cwd == NULL) {
2497 error = got_error_from_errno();
2498 goto done;
2500 if (repo_path == NULL) {
2501 repo_path = strdup(cwd);
2502 if (repo_path == NULL) {
2503 error = got_error_from_errno();
2504 goto done;
2509 error = got_repo_open(&repo, repo_path);
2510 if (error != NULL)
2511 return error;
2513 error = got_repo_map_path(&in_repo_path, repo, path);
2514 if (error != NULL)
2515 goto done;
2517 if (commit_id_str == NULL) {
2518 struct got_reference *head_ref;
2519 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2520 if (error != NULL)
2521 goto done;
2522 error = got_ref_resolve(&commit_id, repo, head_ref);
2523 got_ref_close(head_ref);
2524 } else {
2525 struct got_object *obj;
2526 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2527 if (error != NULL)
2528 goto done;
2529 commit_id = got_object_id_dup(got_object_get_id(obj));
2530 if (commit_id == NULL)
2531 error = got_error_from_errno();
2532 got_object_close(obj);
2534 if (error != NULL)
2535 goto done;
2537 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2538 if (view == NULL) {
2539 error = got_error_from_errno();
2540 goto done;
2542 error = open_blame_view(view, in_repo_path, commit_id, repo);
2543 if (error)
2544 goto done;
2545 error = view_loop(view);
2546 done:
2547 free(repo_path);
2548 free(cwd);
2549 free(commit_id);
2550 if (repo)
2551 got_repo_close(repo);
2552 return error;
2555 static const struct got_error *
2556 draw_tree_entries(struct tog_view *view,
2557 struct got_tree_entry **first_displayed_entry,
2558 struct got_tree_entry **last_displayed_entry,
2559 struct got_tree_entry **selected_entry, int *ndisplayed,
2560 const char *label, int show_ids, const char *parent_path,
2561 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2563 const struct got_error *err = NULL;
2564 struct got_tree_entry *te;
2565 wchar_t *wline;
2566 int width, n;
2568 *ndisplayed = 0;
2570 werase(view->window);
2572 if (limit == 0)
2573 return NULL;
2575 err = format_line(&wline, &width, label, view->ncols);
2576 if (err)
2577 return err;
2578 if (view_needs_focus_indication(view))
2579 wstandout(view->window);
2580 waddwstr(view->window, wline);
2581 if (view_needs_focus_indication(view))
2582 wstandend(view->window);
2583 free(wline);
2584 wline = NULL;
2585 if (width < view->ncols)
2586 waddch(view->window, '\n');
2587 if (--limit <= 0)
2588 return NULL;
2589 err = format_line(&wline, &width, parent_path, view->ncols);
2590 if (err)
2591 return err;
2592 waddwstr(view->window, wline);
2593 free(wline);
2594 wline = NULL;
2595 if (width < view->ncols)
2596 waddch(view->window, '\n');
2597 if (--limit <= 0)
2598 return NULL;
2599 waddch(view->window, '\n');
2600 if (--limit <= 0)
2601 return NULL;
2603 te = SIMPLEQ_FIRST(&entries->head);
2604 if (*first_displayed_entry == NULL) {
2605 if (selected == 0) {
2606 wstandout(view->window);
2607 *selected_entry = NULL;
2609 waddstr(view->window, " ..\n"); /* parent directory */
2610 if (selected == 0)
2611 wstandend(view->window);
2612 (*ndisplayed)++;
2613 if (--limit <= 0)
2614 return NULL;
2615 n = 1;
2616 } else {
2617 n = 0;
2618 while (te != *first_displayed_entry)
2619 te = SIMPLEQ_NEXT(te, entry);
2622 while (te) {
2623 char *line = NULL, *id_str = NULL;
2625 if (show_ids) {
2626 err = got_object_id_str(&id_str, te->id);
2627 if (err)
2628 return got_error_from_errno();
2630 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2631 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2632 free(id_str);
2633 return got_error_from_errno();
2635 free(id_str);
2636 err = format_line(&wline, &width, line, view->ncols);
2637 if (err) {
2638 free(line);
2639 break;
2641 if (n == selected) {
2642 wstandout(view->window);
2643 *selected_entry = te;
2645 waddwstr(view->window, wline);
2646 if (width < view->ncols)
2647 waddch(view->window, '\n');
2648 if (n == selected)
2649 wstandend(view->window);
2650 free(line);
2651 free(wline);
2652 wline = NULL;
2653 n++;
2654 (*ndisplayed)++;
2655 *last_displayed_entry = te;
2656 if (--limit <= 0)
2657 break;
2658 te = SIMPLEQ_NEXT(te, entry);
2661 view_vborder(view);
2662 return err;
2665 static void
2666 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2667 const struct got_tree_entries *entries, int isroot)
2669 struct got_tree_entry *te, *prev;
2670 int i;
2672 if (*first_displayed_entry == NULL)
2673 return;
2675 te = SIMPLEQ_FIRST(&entries->head);
2676 if (*first_displayed_entry == te) {
2677 if (!isroot)
2678 *first_displayed_entry = NULL;
2679 return;
2682 /* XXX this is stupid... switch to TAILQ? */
2683 for (i = 0; i < maxscroll; i++) {
2684 while (te != *first_displayed_entry) {
2685 prev = te;
2686 te = SIMPLEQ_NEXT(te, entry);
2688 *first_displayed_entry = prev;
2689 te = SIMPLEQ_FIRST(&entries->head);
2691 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2692 *first_displayed_entry = NULL;
2695 static void
2696 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2697 struct got_tree_entry *last_displayed_entry,
2698 const struct got_tree_entries *entries)
2700 struct got_tree_entry *next;
2701 int n = 0;
2703 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2704 return;
2706 if (*first_displayed_entry)
2707 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2708 else
2709 next = SIMPLEQ_FIRST(&entries->head);
2710 while (next) {
2711 *first_displayed_entry = next;
2712 if (++n >= maxscroll)
2713 break;
2714 next = SIMPLEQ_NEXT(next, entry);
2718 static const struct got_error *
2719 tree_entry_path(char **path, struct tog_parent_trees *parents,
2720 struct got_tree_entry *te)
2722 const struct got_error *err = NULL;
2723 struct tog_parent_tree *pt;
2724 size_t len = 2; /* for leading slash and NUL */
2726 TAILQ_FOREACH(pt, parents, entry)
2727 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2728 if (te)
2729 len += strlen(te->name);
2731 *path = calloc(1, len);
2732 if (path == NULL)
2733 return got_error_from_errno();
2735 (*path)[0] = '/';
2736 pt = TAILQ_LAST(parents, tog_parent_trees);
2737 while (pt) {
2738 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2739 err = got_error(GOT_ERR_NO_SPACE);
2740 goto done;
2742 if (strlcat(*path, "/", len) >= len) {
2743 err = got_error(GOT_ERR_NO_SPACE);
2744 goto done;
2746 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2748 if (te) {
2749 if (strlcat(*path, te->name, len) >= len) {
2750 err = got_error(GOT_ERR_NO_SPACE);
2751 goto done;
2754 done:
2755 if (err) {
2756 free(*path);
2757 *path = NULL;
2759 return err;
2762 static const struct got_error *
2763 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2764 struct got_tree_entry *te, struct tog_parent_trees *parents,
2765 struct got_object_id *commit_id, struct got_repository *repo)
2767 const struct got_error *err = NULL;
2768 char *path;
2769 struct tog_view *blame_view;
2771 err = tree_entry_path(&path, parents, te);
2772 if (err)
2773 return err;
2775 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2776 if (blame_view == NULL)
2777 return got_error_from_errno();
2779 err = open_blame_view(blame_view, path, commit_id, repo);
2780 if (err) {
2781 view_close(blame_view);
2782 free(path);
2783 } else
2784 *new_view = blame_view;
2785 return err;
2788 static const struct got_error *
2789 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2790 struct got_tree_entry *te, struct tog_parent_trees *parents,
2791 struct got_object_id *commit_id, struct got_repository *repo)
2793 struct tog_view *log_view;
2794 const struct got_error *err = NULL;
2795 char *path;
2797 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2798 if (log_view == NULL)
2799 return got_error_from_errno();
2801 err = tree_entry_path(&path, parents, te);
2802 if (err)
2803 return err;
2805 err = open_log_view(log_view, commit_id, repo, path);
2806 if (err)
2807 view_close(log_view);
2808 else
2809 *new_view = log_view;
2810 free(path);
2811 return err;
2814 static const struct got_error *
2815 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2816 struct got_object_id *commit_id, struct got_repository *repo)
2818 const struct got_error *err = NULL;
2819 char *commit_id_str = NULL;
2820 struct tog_tree_view_state *s = &view->state.tree;
2822 TAILQ_INIT(&s->parents);
2824 err = got_object_id_str(&commit_id_str, commit_id);
2825 if (err != NULL)
2826 goto done;
2828 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2829 err = got_error_from_errno();
2830 goto done;
2833 s->root = s->tree = root;
2834 s->entries = got_object_tree_get_entries(root);
2835 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2836 s->commit_id = commit_id;
2837 s->repo = repo;
2839 view->show = show_tree_view;
2840 view->input = input_tree_view;
2841 view->close = close_tree_view;
2842 done:
2843 free(commit_id_str);
2844 if (err)
2845 free(s->tree_label);
2846 return err;
2849 static const struct got_error *
2850 close_tree_view(struct tog_view *view)
2852 struct tog_tree_view_state *s = &view->state.tree;
2854 free(s->tree_label);
2855 while (!TAILQ_EMPTY(&s->parents)) {
2856 struct tog_parent_tree *parent;
2857 parent = TAILQ_FIRST(&s->parents);
2858 TAILQ_REMOVE(&s->parents, parent, entry);
2859 free(parent);
2862 if (s->tree != s->root)
2863 got_object_tree_close(s->tree);
2864 got_object_tree_close(s->root);
2866 return NULL;
2869 static const struct got_error *
2870 show_tree_view(struct tog_view *view)
2872 const struct got_error *err = NULL;
2873 struct tog_tree_view_state *s = &view->state.tree;
2874 char *parent_path;
2876 err = tree_entry_path(&parent_path, &s->parents, NULL);
2877 if (err)
2878 return err;
2880 err = draw_tree_entries(view, &s->first_displayed_entry,
2881 &s->last_displayed_entry, &s->selected_entry,
2882 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2883 s->entries, s->selected, view->nlines, s->tree == s->root);
2884 free(parent_path);
2885 return err;
2888 static const struct got_error *
2889 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2890 struct tog_view *view, int ch)
2892 const struct got_error *err = NULL;
2893 struct tog_tree_view_state *s = &view->state.tree;
2895 switch (ch) {
2896 case 'i':
2897 s->show_ids = !s->show_ids;
2898 break;
2899 case 'l':
2900 if (s->selected_entry) {
2901 err = log_tree_entry(new_view, view,
2902 s->selected_entry, &s->parents,
2903 s->commit_id, s->repo);
2905 break;
2906 case 'k':
2907 case KEY_UP:
2908 if (s->selected > 0)
2909 s->selected--;
2910 if (s->selected > 0)
2911 break;
2912 tree_scroll_up(&s->first_displayed_entry, 1,
2913 s->entries, s->tree == s->root);
2914 break;
2915 case KEY_PPAGE:
2916 if (SIMPLEQ_FIRST(&s->entries->head) ==
2917 s->first_displayed_entry) {
2918 if (s->tree != s->root)
2919 s->first_displayed_entry = NULL;
2920 s->selected = 0;
2921 break;
2923 tree_scroll_up(&s->first_displayed_entry,
2924 view->nlines, s->entries,
2925 s->tree == s->root);
2926 break;
2927 case 'j':
2928 case KEY_DOWN:
2929 if (s->selected < s->ndisplayed - 1) {
2930 s->selected++;
2931 break;
2933 tree_scroll_down(&s->first_displayed_entry, 1,
2934 s->last_displayed_entry, s->entries);
2935 break;
2936 case KEY_NPAGE:
2937 tree_scroll_down(&s->first_displayed_entry,
2938 view->nlines, s->last_displayed_entry,
2939 s->entries);
2940 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2941 entry))
2942 break;
2943 /* can't scroll any further; move cursor down */
2944 if (s->selected < s->ndisplayed - 1)
2945 s->selected = s->ndisplayed - 1;
2946 break;
2947 case KEY_ENTER:
2948 case '\r':
2949 if (s->selected_entry == NULL) {
2950 struct tog_parent_tree *parent;
2951 case KEY_LEFT:
2952 /* user selected '..' */
2953 if (s->tree == s->root)
2954 break;
2955 parent = TAILQ_FIRST(&s->parents);
2956 TAILQ_REMOVE(&s->parents, parent,
2957 entry);
2958 got_object_tree_close(s->tree);
2959 s->tree = parent->tree;
2960 s->entries =
2961 got_object_tree_get_entries(s->tree);
2962 s->first_displayed_entry =
2963 parent->first_displayed_entry;
2964 s->selected_entry =
2965 parent->selected_entry;
2966 s->selected = parent->selected;
2967 free(parent);
2968 } else if (S_ISDIR(s->selected_entry->mode)) {
2969 struct tog_parent_tree *parent;
2970 struct got_tree_object *child;
2971 err = got_object_open_as_tree(&child,
2972 s->repo, s->selected_entry->id);
2973 if (err)
2974 break;
2975 parent = calloc(1, sizeof(*parent));
2976 if (parent == NULL) {
2977 err = got_error_from_errno();
2978 break;
2980 parent->tree = s->tree;
2981 parent->first_displayed_entry =
2982 s->first_displayed_entry;
2983 parent->selected_entry = s->selected_entry;
2984 parent->selected = s->selected;
2985 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2986 s->tree = child;
2987 s->entries =
2988 got_object_tree_get_entries(s->tree);
2989 s->selected = 0;
2990 s->first_displayed_entry = NULL;
2991 } else if (S_ISREG(s->selected_entry->mode)) {
2992 err = blame_tree_entry(new_view, view,
2993 s->selected_entry, &s->parents,
2994 s->commit_id, s->repo);
2995 if (err)
2996 break;
2998 break;
2999 case KEY_RESIZE:
3000 if (s->selected > view->nlines)
3001 s->selected = s->ndisplayed - 1;
3002 break;
3003 default:
3004 break;
3007 return err;
3010 __dead static void
3011 usage_tree(void)
3013 endwin();
3014 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3015 getprogname());
3016 exit(1);
3019 static const struct got_error *
3020 cmd_tree(int argc, char *argv[])
3022 const struct got_error *error;
3023 struct got_repository *repo = NULL;
3024 char *repo_path = NULL;
3025 struct got_object_id *commit_id = NULL;
3026 char *commit_id_arg = NULL;
3027 struct got_commit_object *commit = NULL;
3028 struct got_tree_object *tree = NULL;
3029 int ch;
3030 struct tog_view *view;
3032 #ifndef PROFILE
3033 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3034 == -1)
3035 err(1, "pledge");
3036 #endif
3038 while ((ch = getopt(argc, argv, "c:")) != -1) {
3039 switch (ch) {
3040 case 'c':
3041 commit_id_arg = optarg;
3042 break;
3043 default:
3044 usage();
3045 /* NOTREACHED */
3049 argc -= optind;
3050 argv += optind;
3052 if (argc == 0) {
3053 repo_path = getcwd(NULL, 0);
3054 if (repo_path == NULL)
3055 return got_error_from_errno();
3056 } else if (argc == 1) {
3057 repo_path = realpath(argv[0], NULL);
3058 if (repo_path == NULL)
3059 return got_error_from_errno();
3060 } else
3061 usage_log();
3063 error = got_repo_open(&repo, repo_path);
3064 free(repo_path);
3065 if (error != NULL)
3066 return error;
3068 if (commit_id_arg == NULL) {
3069 error = get_head_commit_id(&commit_id, repo);
3070 if (error != NULL)
3071 goto done;
3072 } else {
3073 struct got_object *obj;
3074 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3075 if (error == NULL) {
3076 commit_id = got_object_id_dup(got_object_get_id(obj));
3077 if (commit_id == NULL)
3078 error = got_error_from_errno();
3081 if (error != NULL)
3082 goto done;
3084 error = got_object_open_as_commit(&commit, repo, commit_id);
3085 if (error != NULL)
3086 goto done;
3088 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3089 if (error != NULL)
3090 goto done;
3092 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3093 if (view == NULL) {
3094 error = got_error_from_errno();
3095 goto done;
3097 error = open_tree_view(view, tree, commit_id, repo);
3098 if (error)
3099 goto done;
3100 error = view_loop(view);
3101 done:
3102 free(commit_id);
3103 if (commit)
3104 got_object_commit_close(commit);
3105 if (tree)
3106 got_object_tree_close(tree);
3107 if (repo)
3108 got_repo_close(repo);
3109 return error;
3111 static void
3112 init_curses(void)
3114 initscr();
3115 cbreak();
3116 noecho();
3117 nonl();
3118 intrflush(stdscr, FALSE);
3119 keypad(stdscr, TRUE);
3120 curs_set(0);
3123 __dead static void
3124 usage(void)
3126 int i;
3128 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3129 "Available commands:\n", getprogname());
3130 for (i = 0; i < nitems(tog_commands); i++) {
3131 struct tog_cmd *cmd = &tog_commands[i];
3132 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3134 exit(1);
3137 static char **
3138 make_argv(const char *arg0, const char *arg1)
3140 char **argv;
3141 int argc = (arg1 == NULL ? 1 : 2);
3143 argv = calloc(argc, sizeof(char *));
3144 if (argv == NULL)
3145 err(1, "calloc");
3146 argv[0] = strdup(arg0);
3147 if (argv[0] == NULL)
3148 err(1, "calloc");
3149 if (arg1) {
3150 argv[1] = strdup(arg1);
3151 if (argv[1] == NULL)
3152 err(1, "calloc");
3155 return argv;
3158 int
3159 main(int argc, char *argv[])
3161 const struct got_error *error = NULL;
3162 struct tog_cmd *cmd = NULL;
3163 int ch, hflag = 0;
3164 char **cmd_argv = NULL;
3166 setlocale(LC_ALL, "");
3168 while ((ch = getopt(argc, argv, "h")) != -1) {
3169 switch (ch) {
3170 case 'h':
3171 hflag = 1;
3172 break;
3173 default:
3174 usage();
3175 /* NOTREACHED */
3179 argc -= optind;
3180 argv += optind;
3181 optind = 0;
3182 optreset = 1;
3184 if (argc == 0) {
3185 if (hflag)
3186 usage();
3187 /* Build an argument vector which runs a default command. */
3188 cmd = &tog_commands[0];
3189 cmd_argv = make_argv(cmd->name, NULL);
3190 argc = 1;
3191 } else {
3192 int i;
3194 /* Did the user specific a command? */
3195 for (i = 0; i < nitems(tog_commands); i++) {
3196 if (strncmp(tog_commands[i].name, argv[0],
3197 strlen(argv[0])) == 0) {
3198 cmd = &tog_commands[i];
3199 if (hflag)
3200 tog_commands[i].cmd_usage();
3201 break;
3204 if (cmd == NULL) {
3205 /* Did the user specify a repository? */
3206 char *repo_path = realpath(argv[0], NULL);
3207 if (repo_path) {
3208 struct got_repository *repo;
3209 error = got_repo_open(&repo, repo_path);
3210 if (error == NULL)
3211 got_repo_close(repo);
3212 } else
3213 error = got_error_from_errno();
3214 if (error) {
3215 if (hflag) {
3216 fprintf(stderr, "%s: '%s' is not a "
3217 "known command\n", getprogname(),
3218 argv[0]);
3219 usage();
3221 fprintf(stderr, "%s: '%s' is neither a known "
3222 "command nor a path to a repository\n",
3223 getprogname(), argv[0]);
3224 free(repo_path);
3225 return 1;
3227 cmd = &tog_commands[0];
3228 cmd_argv = make_argv(cmd->name, repo_path);
3229 argc = 2;
3230 free(repo_path);
3234 init_curses();
3236 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3237 if (error)
3238 goto done;
3239 done:
3240 endwin();
3241 free(cmd_argv);
3242 if (error)
3243 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3244 return 0;