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 nfetched, nqueued = 0, found_obj = 0;
768 int is_root_path = strcmp(path, "/") == 0;
770 err = got_commit_graph_iter_start(graph, start_id);
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 int nfetched;
778 /* Start ID's commit is already on the queue; skip over it. */
779 err = got_commit_graph_iter_next(&id, graph);
780 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
781 return err;
783 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
784 if (err)
785 return err;
788 while (1) {
789 struct got_commit_object *commit;
791 err = got_commit_graph_iter_next(&id, graph);
792 if (err) {
793 if (err->code != GOT_ERR_ITER_NEED_MORE)
794 break;
795 if (nqueued >= minqueue) {
796 err = NULL;
797 break;
799 err = got_commit_graph_fetch_commits(&nfetched,
800 graph, 1, repo);
801 if (err)
802 return err;
803 continue;
805 if (id == NULL)
806 break;
808 err = got_object_open_as_commit(&commit, repo, id);
809 if (err)
810 break;
812 if (!is_root_path) {
813 struct got_object *obj;
814 struct got_object_qid *pid;
815 int changed = 0;
817 err = got_object_open_by_path(&obj, repo, id, path);
818 if (err) {
819 got_object_commit_close(commit);
820 if (err->code == GOT_ERR_NO_OBJ &&
821 (found_obj || !init)) {
822 /* History stops here. */
823 err = got_error(GOT_ERR_ITER_COMPLETED);
825 break;
827 found_obj = 1;
829 pid = SIMPLEQ_FIRST(&commit->parent_ids);
830 if (pid != NULL) {
831 struct got_object *pobj;
832 err = got_object_open_by_path(&pobj, repo,
833 pid->id, path);
834 if (err) {
835 if (err->code != GOT_ERR_NO_OBJ) {
836 got_object_close(obj);
837 got_object_commit_close(commit);
838 break;
840 err = NULL;
841 changed = 1;
842 } else {
843 struct got_object_id *id, *pid;
844 id = got_object_get_id(obj);
845 pid = got_object_get_id(pobj);
846 changed =
847 (got_object_id_cmp(id, pid) != 0);
848 got_object_close(pobj);
851 got_object_close(obj);
852 if (!changed) {
853 got_object_commit_close(commit);
854 continue;
858 entry = alloc_commit_queue_entry(commit, id);
859 if (entry == NULL) {
860 err = got_error_from_errno();
861 break;
863 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
864 nqueued++;
865 commits->ncommits++;
868 return err;
871 static const struct got_error *
872 fetch_next_commit(struct commit_queue_entry **pentry,
873 struct commit_queue_entry *entry, struct commit_queue *commits,
874 struct got_commit_graph *graph, struct got_repository *repo,
875 const char *path)
877 const struct got_error *err = NULL;
879 *pentry = NULL;
881 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
882 if (err)
883 return err;
885 /* Next entry to display should now be available. */
886 *pentry = TAILQ_NEXT(entry, entry);
887 if (*pentry == NULL)
888 return got_error(GOT_ERR_NO_OBJ);
890 return NULL;
893 static const struct got_error *
894 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
896 const struct got_error *err = NULL;
897 struct got_reference *head_ref;
899 *head_id = NULL;
901 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
902 if (err)
903 return err;
905 err = got_ref_resolve(head_id, repo, head_ref);
906 got_ref_close(head_ref);
907 if (err) {
908 *head_id = NULL;
909 return err;
912 return NULL;
915 static const struct got_error *
916 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
917 struct commit_queue_entry **selected, struct commit_queue_entry *first,
918 struct commit_queue *commits, int selected_idx, int limit,
919 struct got_commit_graph *graph, struct got_repository *repo,
920 const char *path)
922 const struct got_error *err = NULL;
923 struct commit_queue_entry *entry;
924 int ncommits, width;
925 char *id_str, *header;
926 wchar_t *wline;
928 entry = first;
929 ncommits = 0;
930 while (entry) {
931 if (ncommits == selected_idx) {
932 *selected = entry;
933 break;
935 entry = TAILQ_NEXT(entry, entry);
936 ncommits++;
939 err = got_object_id_str(&id_str, (*selected)->id);
940 if (err)
941 return err;
943 if (path && strcmp(path, "/") != 0) {
944 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
945 err = got_error_from_errno();
946 free(id_str);
947 return err;
949 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
950 err = got_error_from_errno();
951 free(id_str);
952 return err;
954 free(id_str);
955 err = format_line(&wline, &width, header, view->ncols);
956 if (err) {
957 free(header);
958 return err;
960 free(header);
962 werase(view->window);
964 if (view_needs_focus_indication(view))
965 wstandout(view->window);
966 waddwstr(view->window, wline);
967 if (view_needs_focus_indication(view))
968 wstandend(view->window);
969 if (width < view->ncols)
970 waddch(view->window, '\n');
971 free(wline);
972 if (limit <= 1)
973 return NULL;
975 entry = first;
976 *last = first;
977 ncommits = 0;
978 while (entry) {
979 if (ncommits >= limit - 1)
980 break;
981 if (ncommits == selected_idx)
982 wstandout(view->window);
983 err = draw_commit(view, entry->commit, entry->id);
984 if (ncommits == selected_idx)
985 wstandend(view->window);
986 if (err)
987 break;
988 ncommits++;
989 *last = entry;
990 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
991 err = queue_commits(graph, commits, entry->id, 1,
992 0, repo, path);
993 if (err) {
994 if (err->code != GOT_ERR_ITER_COMPLETED)
995 return err;
996 err = NULL;
999 entry = TAILQ_NEXT(entry, entry);
1002 view_vborder(view);
1004 return err;
1007 static void
1008 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1009 struct commit_queue *commits)
1011 struct commit_queue_entry *entry;
1012 int nscrolled = 0;
1014 entry = TAILQ_FIRST(&commits->head);
1015 if (*first_displayed_entry == entry)
1016 return;
1018 entry = *first_displayed_entry;
1019 while (entry && nscrolled < maxscroll) {
1020 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1021 if (entry) {
1022 *first_displayed_entry = entry;
1023 nscrolled++;
1028 static const struct got_error *
1029 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1030 struct commit_queue_entry *last_displayed_entry,
1031 struct commit_queue *commits, struct got_commit_graph *graph,
1032 struct got_repository *repo, const char *path)
1034 const struct got_error *err = NULL;
1035 struct commit_queue_entry *pentry;
1036 int nscrolled = 0;
1038 do {
1039 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1040 if (pentry == NULL) {
1041 err = fetch_next_commit(&pentry, last_displayed_entry,
1042 commits, graph, repo, path);
1043 if (err || pentry == NULL)
1044 break;
1046 last_displayed_entry = pentry;
1048 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1049 if (pentry == NULL)
1050 break;
1051 *first_displayed_entry = pentry;
1052 } while (++nscrolled < maxscroll);
1054 return err;
1057 static const struct got_error *
1058 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1059 struct commit_queue_entry *entry, struct got_repository *repo)
1061 const struct got_error *err;
1062 struct got_object *obj1 = NULL, *obj2 = NULL;
1063 struct got_object_qid *parent_id;
1064 struct tog_view *diff_view;
1066 err = got_object_open(&obj2, repo, entry->id);
1067 if (err)
1068 return err;
1070 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1071 if (parent_id) {
1072 err = got_object_open(&obj1, repo, parent_id->id);
1073 if (err)
1074 goto done;
1077 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1078 if (diff_view == NULL) {
1079 err = got_error_from_errno();
1080 goto done;
1083 err = open_diff_view(diff_view, obj1, obj2, repo);
1084 if (err == NULL)
1085 *new_view = diff_view;
1086 done:
1087 if (obj1)
1088 got_object_close(obj1);
1089 if (obj2)
1090 got_object_close(obj2);
1091 return err;
1094 static const struct got_error *
1095 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1096 struct commit_queue_entry *entry, struct got_repository *repo)
1098 const struct got_error *err = NULL;
1099 struct got_tree_object *tree;
1100 struct tog_view *tree_view;
1102 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1103 if (err)
1104 return err;
1106 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1107 if (tree_view == NULL)
1108 return got_error_from_errno();
1110 err = open_tree_view(tree_view, tree, entry->id, repo);
1111 if (err)
1112 got_object_tree_close(tree);
1113 else
1114 *new_view = tree_view;
1115 return err;
1118 static const struct got_error *
1119 set_child_log_view(struct tog_view *view, struct tog_view *child)
1121 struct tog_log_view_state *s = &view->state.log;
1122 struct tog_diff_view_state *ds;
1123 struct commit_queue_entry *commit, *child_entry = NULL;
1124 int selected_idx = 0;
1126 if (child->type != TOG_VIEW_DIFF)
1127 return NULL;
1128 ds = &child->state.diff;
1130 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1131 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1132 child_entry = commit;
1133 break;
1136 if (child_entry == NULL)
1137 return NULL;
1139 commit = s->first_displayed_entry;
1140 while (commit) {
1141 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1142 s->selected_entry = child_entry;
1143 s->selected = selected_idx;
1144 break;
1146 if (commit == s->last_displayed_entry)
1147 break;
1148 selected_idx++;
1149 commit = TAILQ_NEXT(commit, entry);
1152 return show_log_view(view);
1155 static const struct got_error *
1156 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1157 struct got_repository *repo, const char *path)
1159 const struct got_error *err = NULL;
1160 struct got_object_id *head_id = NULL;
1161 int nfetched;
1162 struct tog_log_view_state *s = &view->state.log;
1164 err = got_repo_map_path(&s->in_repo_path, repo, path);
1165 if (err != NULL)
1166 goto done;
1168 err = get_head_commit_id(&head_id, repo);
1169 if (err)
1170 return err;
1172 /* The graph contains all commits. */
1173 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1174 if (err)
1175 goto done;
1176 /* The commit queue contains a subset of commits filtered by path. */
1177 TAILQ_INIT(&s->commits.head);
1178 s->commits.ncommits = 0;
1180 /* Populate commit graph with a sufficient number of commits. */
1181 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1182 start_id, repo);
1183 if (err)
1184 goto done;
1187 * Open the initial batch of commits, sorted in commit graph order.
1188 * We keep all commits open throughout the lifetime of the log view
1189 * in order to avoid having to re-fetch commits from disk while
1190 * updating the display.
1192 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1193 repo, s->in_repo_path);
1194 if (err) {
1195 if (err->code != GOT_ERR_ITER_COMPLETED)
1196 goto done;
1197 err = NULL;
1200 s->first_displayed_entry =
1201 TAILQ_FIRST(&s->commits.head);
1202 s->selected_entry = s->first_displayed_entry;
1203 s->repo = repo;
1205 view->show = show_log_view;
1206 view->input = input_log_view;
1207 view->close = close_log_view;
1208 view->set_child = set_child_log_view;
1209 done:
1210 free(head_id);
1211 return err;
1214 static const struct got_error *
1215 close_log_view(struct tog_view *view)
1217 struct tog_log_view_state *s = &view->state.log;
1219 if (s->graph)
1220 got_commit_graph_close(s->graph);
1221 free_commits(&s->commits);
1222 free(s->in_repo_path);
1223 return NULL;
1226 static const struct got_error *
1227 update_diff_child_view(struct tog_view *parent,
1228 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1230 const struct got_error *err = NULL;
1231 struct tog_diff_view_state *ds;
1232 struct got_object *obj1 = NULL, *obj2 = NULL;
1233 struct got_object_qid *parent_id;
1234 struct tog_view *child_view = parent->child;
1236 if (child_view == NULL)
1237 return NULL;
1238 if (child_view->type != TOG_VIEW_DIFF)
1239 return NULL;
1240 ds = &child_view->state.diff;
1241 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1242 return NULL;
1244 err = got_object_open(&obj2, repo, selected_entry->id);
1245 if (err)
1246 return err;
1248 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1249 if (parent_id) {
1250 err = got_object_open(&obj1, repo, parent_id->id);
1251 if (err)
1252 goto done;
1255 err = close_diff_view(child_view);
1256 if (err)
1257 goto done;
1259 err = open_diff_view(child_view, obj1, obj2, repo);
1260 if (err)
1261 goto done;
1262 done:
1263 if (obj1)
1264 got_object_close(obj1);
1265 if (obj2)
1266 got_object_close(obj2);
1267 return err;
1270 static const struct got_error *
1271 show_log_view(struct tog_view *view)
1273 const struct got_error *err = NULL;
1274 struct tog_log_view_state *s = &view->state.log;
1276 err = draw_commits(view, &s->last_displayed_entry,
1277 &s->selected_entry, s->first_displayed_entry,
1278 &s->commits, s->selected, view->nlines, s->graph,
1279 s->repo, s->in_repo_path);
1280 if (err)
1281 return err;
1283 return update_diff_child_view(view, s->selected_entry, s->repo);
1286 static const struct got_error *
1287 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1288 struct tog_view *view, int ch)
1290 const struct got_error *err = NULL;
1291 struct tog_log_view_state *s = &view->state.log;
1293 switch (ch) {
1294 case 'k':
1295 case KEY_UP:
1296 case '[':
1297 if (s->selected > 0)
1298 s->selected--;
1299 if (s->selected > 0)
1300 break;
1301 scroll_up(&s->first_displayed_entry, 1,
1302 &s->commits);
1303 break;
1304 case KEY_PPAGE:
1305 if (TAILQ_FIRST(&s->commits.head) ==
1306 s->first_displayed_entry) {
1307 s->selected = 0;
1308 break;
1310 scroll_up(&s->first_displayed_entry,
1311 view->nlines, &s->commits);
1312 break;
1313 case 'j':
1314 case KEY_DOWN:
1315 case ']':
1316 if (s->selected < MIN(view->nlines - 2,
1317 s->commits.ncommits - 1)) {
1318 s->selected++;
1319 break;
1321 err = scroll_down(&s->first_displayed_entry, 1,
1322 s->last_displayed_entry, &s->commits,
1323 s->graph, s->repo, s->in_repo_path);
1324 if (err) {
1325 if (err->code != GOT_ERR_ITER_COMPLETED)
1326 break;
1327 err = NULL;
1329 break;
1330 case KEY_NPAGE: {
1331 struct commit_queue_entry *first;
1332 first = s->first_displayed_entry;
1333 err = scroll_down(&s->first_displayed_entry,
1334 view->nlines, s->last_displayed_entry,
1335 &s->commits, s->graph, s->repo,
1336 s->in_repo_path);
1337 if (err == NULL)
1338 break;
1339 if (err->code != GOT_ERR_ITER_COMPLETED)
1340 break;
1341 if (first == s->first_displayed_entry &&
1342 s->selected < MIN(view->nlines - 2,
1343 s->commits.ncommits - 1)) {
1344 /* can't scroll further down */
1345 s->selected = MIN(view->nlines - 2,
1346 s->commits.ncommits - 1);
1348 err = NULL;
1349 break;
1351 case KEY_RESIZE:
1352 if (s->selected > view->nlines - 2)
1353 s->selected = view->nlines - 2;
1354 if (s->selected > s->commits.ncommits - 1)
1355 s->selected = s->commits.ncommits - 1;
1356 break;
1357 case KEY_ENTER:
1358 case '\r':
1359 err = show_commit(new_view, view, s->selected_entry,
1360 s->repo);
1361 break;
1362 case 't':
1363 err = browse_commit(new_view, view, s->selected_entry,
1364 s->repo);
1365 break;
1366 default:
1367 break;
1370 return err;
1373 static const struct got_error *
1374 cmd_log(int argc, char *argv[])
1376 const struct got_error *error;
1377 struct got_repository *repo = NULL;
1378 struct got_object_id *start_id = NULL;
1379 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1380 char *start_commit = NULL;
1381 int ch;
1382 struct tog_view *view;
1384 #ifndef PROFILE
1385 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1386 == -1)
1387 err(1, "pledge");
1388 #endif
1390 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1391 switch (ch) {
1392 case 'c':
1393 start_commit = optarg;
1394 break;
1395 case 'r':
1396 repo_path = realpath(optarg, NULL);
1397 if (repo_path == NULL)
1398 err(1, "-r option");
1399 break;
1400 default:
1401 usage();
1402 /* NOTREACHED */
1406 argc -= optind;
1407 argv += optind;
1409 if (argc == 0)
1410 path = strdup("");
1411 else if (argc == 1)
1412 path = strdup(argv[0]);
1413 else
1414 usage_log();
1415 if (path == NULL)
1416 return got_error_from_errno();
1418 cwd = getcwd(NULL, 0);
1419 if (cwd == NULL) {
1420 error = got_error_from_errno();
1421 goto done;
1423 if (repo_path == NULL) {
1424 repo_path = strdup(cwd);
1425 if (repo_path == NULL) {
1426 error = got_error_from_errno();
1427 goto done;
1431 error = got_repo_open(&repo, repo_path);
1432 if (error != NULL)
1433 goto done;
1435 if (start_commit == NULL) {
1436 error = get_head_commit_id(&start_id, repo);
1437 if (error != NULL)
1438 goto done;
1439 } else {
1440 struct got_object *obj;
1441 error = got_object_open_by_id_str(&obj, repo, start_commit);
1442 if (error == NULL) {
1443 start_id = got_object_id_dup(got_object_get_id(obj));
1444 if (start_id == NULL)
1445 error = got_error_from_errno();
1446 goto done;
1449 if (error != NULL)
1450 goto done;
1452 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1453 if (view == NULL) {
1454 error = got_error_from_errno();
1455 goto done;
1457 error = open_log_view(view, start_id, repo, path);
1458 if (error)
1459 goto done;
1460 error = view_loop(view);
1461 done:
1462 free(repo_path);
1463 free(cwd);
1464 free(path);
1465 free(start_id);
1466 if (repo)
1467 got_repo_close(repo);
1468 return error;
1471 __dead static void
1472 usage_diff(void)
1474 endwin();
1475 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1476 getprogname());
1477 exit(1);
1480 static char *
1481 parse_next_line(FILE *f, size_t *len)
1483 char *line;
1484 size_t linelen;
1485 size_t lineno;
1486 const char delim[3] = { '\0', '\0', '\0'};
1488 line = fparseln(f, &linelen, &lineno, delim, 0);
1489 if (len)
1490 *len = linelen;
1491 return line;
1494 static const struct got_error *
1495 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1496 int *last_displayed_line, int *eof, int max_lines,
1497 char * header)
1499 const struct got_error *err;
1500 int nlines = 0, nprinted = 0;
1501 char *line;
1502 size_t len;
1503 wchar_t *wline;
1504 int width;
1506 rewind(f);
1507 werase(view->window);
1509 if (header) {
1510 err = format_line(&wline, &width, header, view->ncols);
1511 if (err) {
1512 return err;
1515 if (view_needs_focus_indication(view))
1516 wstandout(view->window);
1517 waddwstr(view->window, wline);
1518 if (view_needs_focus_indication(view))
1519 wstandend(view->window);
1520 if (width < view->ncols)
1521 waddch(view->window, '\n');
1523 if (max_lines <= 1)
1524 return NULL;
1525 max_lines--;
1528 *eof = 0;
1529 while (nprinted < max_lines) {
1530 line = parse_next_line(f, &len);
1531 if (line == NULL) {
1532 *eof = 1;
1533 break;
1535 if (++nlines < *first_displayed_line) {
1536 free(line);
1537 continue;
1540 err = format_line(&wline, &width, line, view->ncols);
1541 if (err) {
1542 free(line);
1543 return err;
1545 waddwstr(view->window, wline);
1546 if (width < view->ncols)
1547 waddch(view->window, '\n');
1548 if (++nprinted == 1)
1549 *first_displayed_line = nlines;
1550 free(line);
1551 free(wline);
1552 wline = NULL;
1554 *last_displayed_line = nlines;
1556 view_vborder(view);
1558 return NULL;
1561 static const struct got_error *
1562 open_diff_view(struct tog_view *view, struct got_object *obj1,
1563 struct got_object *obj2, struct got_repository *repo)
1565 const struct got_error *err;
1566 FILE *f;
1568 if (obj1 != NULL && obj2 != NULL &&
1569 got_object_get_type(obj1) != got_object_get_type(obj2))
1570 return got_error(GOT_ERR_OBJ_TYPE);
1572 f = got_opentemp();
1573 if (f == NULL)
1574 return got_error_from_errno();
1576 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1577 case GOT_OBJ_TYPE_BLOB:
1578 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1579 repo, f);
1580 break;
1581 case GOT_OBJ_TYPE_TREE:
1582 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1583 break;
1584 case GOT_OBJ_TYPE_COMMIT:
1585 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1586 break;
1587 default:
1588 return got_error(GOT_ERR_OBJ_TYPE);
1591 fflush(f);
1593 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1594 view->state.diff.id2 = got_object_get_id(obj2);
1595 view->state.diff.f = f;
1596 view->state.diff.first_displayed_line = 1;
1597 view->state.diff.last_displayed_line = view->nlines;
1599 view->show = show_diff_view;
1600 view->input = input_diff_view;
1601 view->close = close_diff_view;
1603 return NULL;
1606 static const struct got_error *
1607 close_diff_view(struct tog_view *view)
1609 const struct got_error *err = NULL;
1611 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1612 err = got_error_from_errno();
1613 return err;
1616 static const struct got_error *
1617 show_diff_view(struct tog_view *view)
1619 const struct got_error *err;
1620 struct tog_diff_view_state *s = &view->state.diff;
1621 char *id_str1 = NULL, *id_str2, *header;
1623 if (s->id1) {
1624 err = got_object_id_str(&id_str1, s->id1);
1625 if (err)
1626 return err;
1628 err = got_object_id_str(&id_str2, s->id2);
1629 if (err)
1630 return err;
1632 if (asprintf(&header, "diff: %s %s",
1633 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1634 err = got_error_from_errno();
1635 free(id_str1);
1636 free(id_str2);
1637 return err;
1639 free(id_str1);
1640 free(id_str2);
1642 return draw_file(view, s->f, &s->first_displayed_line,
1643 &s->last_displayed_line, &s->eof, view->nlines,
1644 header);
1647 static const struct got_error *
1648 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1649 struct tog_view *view, int ch)
1651 const struct got_error *err = NULL;
1652 struct tog_diff_view_state *s = &view->state.diff;
1653 int i;
1655 switch (ch) {
1656 case 'k':
1657 case KEY_UP:
1658 if (s->first_displayed_line > 1)
1659 s->first_displayed_line--;
1660 break;
1661 case KEY_PPAGE:
1662 i = 0;
1663 while (i++ < view->nlines - 1 &&
1664 s->first_displayed_line > 1)
1665 s->first_displayed_line--;
1666 break;
1667 case 'j':
1668 case KEY_DOWN:
1669 if (!s->eof)
1670 s->first_displayed_line++;
1671 break;
1672 case KEY_NPAGE:
1673 case ' ':
1674 i = 0;
1675 while (!s->eof && i++ < view->nlines - 1) {
1676 char *line;
1677 line = parse_next_line(s->f, NULL);
1678 s->first_displayed_line++;
1679 if (line == NULL)
1680 break;
1682 break;
1683 case '[':
1684 case ']': {
1685 struct tog_log_view_state *ls;
1686 struct commit_queue_entry *entry;
1687 struct tog_view *diff_view;
1689 if (view->parent == NULL)
1690 break;
1691 if (view->parent->type != TOG_VIEW_LOG)
1692 break;
1693 ls = &view->parent->state.log;
1695 if (ch == '[') {
1696 entry = TAILQ_PREV(ls->selected_entry,
1697 commit_queue_head, entry);
1698 } else {
1699 entry = TAILQ_NEXT(ls->selected_entry, entry);
1700 if (entry == NULL) {
1701 err = fetch_next_commit(&entry,
1702 ls->selected_entry,
1703 &ls->commits, ls->graph,
1704 ls->repo, ls->in_repo_path);
1705 if (err)
1706 break;
1709 if (entry == NULL)
1710 break;
1711 err = show_commit(&diff_view, view->parent,
1712 entry, ls->repo);
1713 if (err)
1714 break;
1715 *new_view = diff_view;
1716 *dead_view = view;
1717 break;
1719 default:
1720 break;
1723 return err;
1726 static const struct got_error *
1727 cmd_diff(int argc, char *argv[])
1729 const struct got_error *error = NULL;
1730 struct got_repository *repo = NULL;
1731 struct got_object *obj1 = NULL, *obj2 = NULL;
1732 char *repo_path = NULL;
1733 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1734 int ch;
1735 struct tog_view *view;
1737 #ifndef PROFILE
1738 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1739 == -1)
1740 err(1, "pledge");
1741 #endif
1743 while ((ch = getopt(argc, argv, "")) != -1) {
1744 switch (ch) {
1745 default:
1746 usage();
1747 /* NOTREACHED */
1751 argc -= optind;
1752 argv += optind;
1754 if (argc == 0) {
1755 usage_diff(); /* TODO show local worktree changes */
1756 } else if (argc == 2) {
1757 repo_path = getcwd(NULL, 0);
1758 if (repo_path == NULL)
1759 return got_error_from_errno();
1760 obj_id_str1 = argv[0];
1761 obj_id_str2 = argv[1];
1762 } else if (argc == 3) {
1763 repo_path = realpath(argv[0], NULL);
1764 if (repo_path == NULL)
1765 return got_error_from_errno();
1766 obj_id_str1 = argv[1];
1767 obj_id_str2 = argv[2];
1768 } else
1769 usage_diff();
1771 error = got_repo_open(&repo, repo_path);
1772 free(repo_path);
1773 if (error)
1774 goto done;
1776 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1777 if (error)
1778 goto done;
1780 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1781 if (error)
1782 goto done;
1784 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1785 if (view == NULL) {
1786 error = got_error_from_errno();
1787 goto done;
1789 error = open_diff_view(view, obj1, obj2, repo);
1790 if (error)
1791 goto done;
1792 error = view_loop(view);
1793 done:
1794 got_repo_close(repo);
1795 if (obj1)
1796 got_object_close(obj1);
1797 if (obj2)
1798 got_object_close(obj2);
1799 return error;
1802 __dead static void
1803 usage_blame(void)
1805 endwin();
1806 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1807 getprogname());
1808 exit(1);
1811 struct tog_blame_line {
1812 int annotated;
1813 struct got_object_id *id;
1816 static const struct got_error *
1817 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1818 const char *path, struct tog_blame_line *lines, int nlines,
1819 int blame_complete, int selected_line, int *first_displayed_line,
1820 int *last_displayed_line, int *eof, int max_lines)
1822 const struct got_error *err;
1823 int lineno = 0, nprinted = 0;
1824 char *line;
1825 size_t len;
1826 wchar_t *wline;
1827 int width, wlimit;
1828 struct tog_blame_line *blame_line;
1829 struct got_object_id *prev_id = NULL;
1830 char *id_str;
1832 err = got_object_id_str(&id_str, id);
1833 if (err)
1834 return err;
1836 rewind(f);
1837 werase(view->window);
1839 if (asprintf(&line, "commit: %s", id_str) == -1) {
1840 err = got_error_from_errno();
1841 free(id_str);
1842 return err;
1845 err = format_line(&wline, &width, line, view->ncols);
1846 free(line);
1847 line = NULL;
1848 if (view_needs_focus_indication(view))
1849 wstandout(view->window);
1850 waddwstr(view->window, wline);
1851 if (view_needs_focus_indication(view))
1852 wstandend(view->window);
1853 free(wline);
1854 wline = NULL;
1855 if (width < view->ncols)
1856 waddch(view->window, '\n');
1858 if (asprintf(&line, "[%d/%d] %s%s",
1859 *first_displayed_line - 1 + selected_line, nlines,
1860 blame_complete ? "" : "annotating ", path) == -1) {
1861 free(id_str);
1862 return got_error_from_errno();
1864 free(id_str);
1865 err = format_line(&wline, &width, line, view->ncols);
1866 free(line);
1867 line = NULL;
1868 if (err)
1869 return err;
1870 waddwstr(view->window, wline);
1871 free(wline);
1872 wline = NULL;
1873 if (width < view->ncols)
1874 waddch(view->window, '\n');
1876 *eof = 0;
1877 while (nprinted < max_lines - 2) {
1878 line = parse_next_line(f, &len);
1879 if (line == NULL) {
1880 *eof = 1;
1881 break;
1883 if (++lineno < *first_displayed_line) {
1884 free(line);
1885 continue;
1888 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1889 err = format_line(&wline, &width, line, wlimit);
1890 if (err) {
1891 free(line);
1892 return err;
1895 if (nprinted == selected_line - 1)
1896 wstandout(view->window);
1898 blame_line = &lines[lineno - 1];
1899 if (blame_line->annotated && prev_id &&
1900 got_object_id_cmp(prev_id, blame_line->id) == 0)
1901 waddstr(view->window, " ");
1902 else if (blame_line->annotated) {
1903 char *id_str;
1904 err = got_object_id_str(&id_str, blame_line->id);
1905 if (err) {
1906 free(line);
1907 free(wline);
1908 return err;
1910 wprintw(view->window, "%.8s ", id_str);
1911 free(id_str);
1912 prev_id = blame_line->id;
1913 } else {
1914 waddstr(view->window, "........ ");
1915 prev_id = NULL;
1918 waddwstr(view->window, wline);
1919 while (width < wlimit) {
1920 waddch(view->window, ' ');
1921 width++;
1923 if (nprinted == selected_line - 1)
1924 wstandend(view->window);
1925 if (++nprinted == 1)
1926 *first_displayed_line = lineno;
1927 free(line);
1928 free(wline);
1929 wline = NULL;
1931 *last_displayed_line = lineno;
1933 view_vborder(view);
1935 return NULL;
1938 static const struct got_error *
1939 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1941 const struct got_error *err = NULL;
1942 struct tog_blame_cb_args *a = arg;
1943 struct tog_blame_line *line;
1945 if (nlines != a->nlines ||
1946 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1947 return got_error(GOT_ERR_RANGE);
1949 if (pthread_mutex_lock(a->mutex) != 0)
1950 return got_error_from_errno();
1952 if (*a->quit) { /* user has quit the blame view */
1953 err = got_error(GOT_ERR_ITER_COMPLETED);
1954 goto done;
1957 if (lineno == -1)
1958 goto done; /* no change in this commit */
1960 line = &a->lines[lineno - 1];
1961 if (line->annotated)
1962 goto done;
1964 line->id = got_object_id_dup(id);
1965 if (line->id == NULL) {
1966 err = got_error_from_errno();
1967 goto done;
1969 line->annotated = 1;
1971 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1972 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1973 a->last_displayed_line, a->eof, a->view->nlines);
1974 done:
1975 if (pthread_mutex_unlock(a->mutex) != 0)
1976 return got_error_from_errno();
1977 return err;
1980 static void *
1981 blame_thread(void *arg)
1983 const struct got_error *err;
1984 struct tog_blame_thread_args *ta = arg;
1985 struct tog_blame_cb_args *a = ta->cb_args;
1987 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1988 blame_cb, ta->cb_args);
1990 if (pthread_mutex_lock(a->mutex) != 0)
1991 return (void *)got_error_from_errno();
1993 got_repo_close(ta->repo);
1994 ta->repo = NULL;
1995 *ta->complete = 1;
1996 if (!err)
1997 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1998 a->lines, a->nlines, 1, *a->selected_line,
1999 a->first_displayed_line, a->last_displayed_line, a->eof,
2000 a->view->nlines);
2002 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2003 err = got_error_from_errno();
2005 return (void *)err;
2008 static struct got_object_id *
2009 get_selected_commit_id(struct tog_blame_line *lines,
2010 int first_displayed_line, int selected_line)
2012 struct tog_blame_line *line;
2014 line = &lines[first_displayed_line - 1 + selected_line - 1];
2015 if (!line->annotated)
2016 return NULL;
2018 return line->id;
2021 static const struct got_error *
2022 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2023 struct tog_blame_line *lines, int first_displayed_line,
2024 int selected_line, struct got_repository *repo)
2026 const struct got_error *err = NULL;
2027 struct got_commit_object *commit = NULL;
2028 struct got_object_id *selected_id;
2029 struct got_object_qid *pid;
2031 *pobj = NULL;
2032 *obj = NULL;
2034 selected_id = get_selected_commit_id(lines,
2035 first_displayed_line, selected_line);
2036 if (selected_id == NULL)
2037 return NULL;
2039 err = got_object_open(obj, repo, selected_id);
2040 if (err)
2041 goto done;
2043 err = got_object_commit_open(&commit, repo, *obj);
2044 if (err)
2045 goto done;
2047 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2048 if (pid) {
2049 err = got_object_open(pobj, repo, pid->id);
2050 if (err)
2051 goto done;
2053 done:
2054 if (commit)
2055 got_object_commit_close(commit);
2056 return err;
2059 static const struct got_error *
2060 stop_blame(struct tog_blame *blame)
2062 const struct got_error *err = NULL;
2063 int i;
2065 if (blame->thread) {
2066 if (pthread_join(blame->thread, (void **)&err) != 0)
2067 err = got_error_from_errno();
2068 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2069 err = NULL;
2070 blame->thread = NULL;
2072 if (blame->thread_args.repo) {
2073 got_repo_close(blame->thread_args.repo);
2074 blame->thread_args.repo = NULL;
2076 if (blame->f) {
2077 fclose(blame->f);
2078 blame->f = NULL;
2080 for (i = 0; i < blame->nlines; i++)
2081 free(blame->lines[i].id);
2082 free(blame->lines);
2083 blame->lines = NULL;
2084 free(blame->cb_args.commit_id);
2085 blame->cb_args.commit_id = NULL;
2087 return err;
2090 static const struct got_error *
2091 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2092 struct tog_view *view, int *blame_complete,
2093 int *first_displayed_line, int *last_displayed_line,
2094 int *selected_line, int *done, int *eof, const char *path,
2095 struct got_object_id *commit_id,
2096 struct got_repository *repo)
2098 const struct got_error *err = NULL;
2099 struct got_blob_object *blob = NULL;
2100 struct got_repository *thread_repo = NULL;
2101 struct got_object *obj;
2103 err = got_object_open_by_path(&obj, repo, commit_id, path);
2104 if (err)
2105 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 if (obj)
2167 got_object_close(obj);
2168 if (err)
2169 stop_blame(blame);
2170 return err;
2173 static const struct got_error *
2174 open_blame_view(struct tog_view *view, char *path,
2175 struct got_object_id *commit_id, struct got_repository *repo)
2177 const struct got_error *err = NULL;
2178 struct tog_blame_view_state *s = &view->state.blame;
2180 SIMPLEQ_INIT(&s->blamed_commits);
2182 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2183 return got_error_from_errno();
2185 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2186 if (err)
2187 return err;
2189 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2190 s->first_displayed_line = 1;
2191 s->last_displayed_line = view->nlines;
2192 s->selected_line = 1;
2193 s->blame_complete = 0;
2194 s->path = path;
2195 if (s->path == NULL)
2196 return got_error_from_errno();
2197 s->repo = repo;
2198 s->commit_id = commit_id;
2199 memset(&s->blame, 0, sizeof(s->blame));
2201 view->show = show_blame_view;
2202 view->input = input_blame_view;
2203 view->close = close_blame_view;
2205 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2206 &s->first_displayed_line, &s->last_displayed_line,
2207 &s->selected_line, &s->done, &s->eof, s->path,
2208 s->blamed_commit->id, s->repo);
2211 static const struct got_error *
2212 close_blame_view(struct tog_view *view)
2214 const struct got_error *err = NULL;
2215 struct tog_blame_view_state *s = &view->state.blame;
2217 if (s->blame.thread)
2218 err = stop_blame(&s->blame);
2220 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2221 struct got_object_qid *blamed_commit;
2222 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2223 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2224 got_object_qid_free(blamed_commit);
2227 free(s->path);
2229 return err;
2232 static const struct got_error *
2233 show_blame_view(struct tog_view *view)
2235 const struct got_error *err = NULL;
2236 struct tog_blame_view_state *s = &view->state.blame;
2238 if (pthread_mutex_lock(&s->mutex) != 0)
2239 return got_error_from_errno();
2241 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2242 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2243 s->selected_line, &s->first_displayed_line,
2244 &s->last_displayed_line, &s->eof, view->nlines);
2246 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2247 err = got_error_from_errno();
2249 return err;
2252 static const struct got_error *
2253 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2254 struct tog_view *view, int ch)
2256 const struct got_error *err = NULL, *thread_err = NULL;
2257 struct got_object *obj = NULL, *pobj = NULL;
2258 struct tog_view *diff_view;
2259 struct tog_blame_view_state *s = &view->state.blame;
2261 if (pthread_mutex_lock(&s->mutex) != 0) {
2262 err = got_error_from_errno();
2263 goto done;
2266 switch (ch) {
2267 case 'q':
2268 s->done = 1;
2269 if (pthread_mutex_unlock(&s->mutex) != 0) {
2270 err = got_error_from_errno();
2271 goto done;
2273 return stop_blame(&s->blame);
2274 case 'k':
2275 case KEY_UP:
2276 if (s->selected_line > 1)
2277 s->selected_line--;
2278 else if (s->selected_line == 1 &&
2279 s->first_displayed_line > 1)
2280 s->first_displayed_line--;
2281 break;
2282 case KEY_PPAGE:
2283 if (s->first_displayed_line == 1) {
2284 s->selected_line = 1;
2285 break;
2287 if (s->first_displayed_line > view->nlines - 2)
2288 s->first_displayed_line -=
2289 (view->nlines - 2);
2290 else
2291 s->first_displayed_line = 1;
2292 break;
2293 case 'j':
2294 case KEY_DOWN:
2295 if (s->selected_line < view->nlines - 2 &&
2296 s->first_displayed_line +
2297 s->selected_line <= s->blame.nlines)
2298 s->selected_line++;
2299 else if (s->last_displayed_line <
2300 s->blame.nlines)
2301 s->first_displayed_line++;
2302 break;
2303 case 'b':
2304 case 'p': {
2305 struct got_object_id *id;
2306 id = get_selected_commit_id(s->blame.lines,
2307 s->first_displayed_line, s->selected_line);
2308 if (id == NULL || got_object_id_cmp(id,
2309 s->blamed_commit->id) == 0)
2310 break;
2311 err = open_selected_commit(&pobj, &obj,
2312 s->blame.lines, s->first_displayed_line,
2313 s->selected_line, s->repo);
2314 if (err)
2315 break;
2316 if (pobj == NULL && obj == NULL)
2317 break;
2318 if (ch == 'p' && pobj == NULL)
2319 break;
2320 s->done = 1;
2321 if (pthread_mutex_unlock(&s->mutex) != 0) {
2322 err = got_error_from_errno();
2323 goto done;
2325 thread_err = stop_blame(&s->blame);
2326 s->done = 0;
2327 if (pthread_mutex_lock(&s->mutex) != 0) {
2328 err = got_error_from_errno();
2329 goto done;
2331 if (thread_err)
2332 break;
2333 id = got_object_get_id(ch == 'b' ? obj : pobj);
2334 got_object_close(obj);
2335 obj = NULL;
2336 if (pobj) {
2337 got_object_close(pobj);
2338 pobj = NULL;
2340 err = got_object_qid_alloc(&s->blamed_commit, id);
2341 if (err)
2342 goto done;
2343 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2344 s->blamed_commit, entry);
2345 err = run_blame(&s->blame, &s->mutex, view,
2346 &s->blame_complete,
2347 &s->first_displayed_line,
2348 &s->last_displayed_line,
2349 &s->selected_line, &s->done, &s->eof,
2350 s->path, s->blamed_commit->id, s->repo);
2351 if (err)
2352 break;
2353 break;
2355 case 'B': {
2356 struct got_object_qid *first;
2357 first = SIMPLEQ_FIRST(&s->blamed_commits);
2358 if (!got_object_id_cmp(first->id, s->commit_id))
2359 break;
2360 s->done = 1;
2361 if (pthread_mutex_unlock(&s->mutex) != 0) {
2362 err = got_error_from_errno();
2363 goto done;
2365 thread_err = stop_blame(&s->blame);
2366 s->done = 0;
2367 if (pthread_mutex_lock(&s->mutex) != 0) {
2368 err = got_error_from_errno();
2369 goto done;
2371 if (thread_err)
2372 break;
2373 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2374 got_object_qid_free(s->blamed_commit);
2375 s->blamed_commit =
2376 SIMPLEQ_FIRST(&s->blamed_commits);
2377 err = run_blame(&s->blame, &s->mutex, view,
2378 &s->blame_complete,
2379 &s->first_displayed_line,
2380 &s->last_displayed_line,
2381 &s->selected_line, &s->done, &s->eof, s->path,
2382 s->blamed_commit->id, s->repo);
2383 if (err)
2384 break;
2385 break;
2387 case KEY_ENTER:
2388 case '\r':
2389 err = open_selected_commit(&pobj, &obj,
2390 s->blame.lines, s->first_displayed_line,
2391 s->selected_line, s->repo);
2392 if (err)
2393 break;
2394 if (pobj == NULL && obj == NULL)
2395 break;
2396 diff_view = view_open(0, 0, 0, 0, view,
2397 TOG_VIEW_DIFF);
2398 if (diff_view == NULL) {
2399 err = got_error_from_errno();
2400 break;
2402 err = open_diff_view(diff_view, pobj, obj,
2403 s->repo);
2404 if (err) {
2405 view_close(diff_view);
2406 break;
2408 *new_view = diff_view;
2409 if (pobj) {
2410 got_object_close(pobj);
2411 pobj = NULL;
2413 got_object_close(obj);
2414 obj = NULL;
2415 if (err)
2416 break;
2417 break;
2418 case KEY_NPAGE:
2419 case ' ':
2420 if (s->last_displayed_line >= s->blame.nlines &&
2421 s->selected_line < view->nlines - 2) {
2422 s->selected_line = MIN(s->blame.nlines,
2423 view->nlines - 2);
2424 break;
2426 if (s->last_displayed_line + view->nlines - 2
2427 <= s->blame.nlines)
2428 s->first_displayed_line +=
2429 view->nlines - 2;
2430 else
2431 s->first_displayed_line =
2432 s->blame.nlines -
2433 (view->nlines - 3);
2434 break;
2435 case KEY_RESIZE:
2436 if (s->selected_line > view->nlines - 2) {
2437 s->selected_line = MIN(s->blame.nlines,
2438 view->nlines - 2);
2440 break;
2441 default:
2442 break;
2445 if (pthread_mutex_unlock(&s->mutex) != 0)
2446 err = got_error_from_errno();
2447 done:
2448 if (pobj)
2449 got_object_close(pobj);
2450 return thread_err ? thread_err : err;
2453 static const struct got_error *
2454 cmd_blame(int argc, char *argv[])
2456 const struct got_error *error;
2457 struct got_repository *repo = NULL;
2458 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2459 struct got_object_id *commit_id = NULL;
2460 char *commit_id_str = NULL;
2461 int ch;
2462 struct tog_view *view;
2464 #ifndef PROFILE
2465 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2466 == -1)
2467 err(1, "pledge");
2468 #endif
2470 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2471 switch (ch) {
2472 case 'c':
2473 commit_id_str = optarg;
2474 break;
2475 case 'r':
2476 repo_path = realpath(optarg, NULL);
2477 if (repo_path == NULL)
2478 err(1, "-r option");
2479 break;
2480 default:
2481 usage();
2482 /* NOTREACHED */
2486 argc -= optind;
2487 argv += optind;
2489 if (argc == 1)
2490 path = argv[0];
2491 else
2492 usage_blame();
2494 cwd = getcwd(NULL, 0);
2495 if (cwd == NULL) {
2496 error = got_error_from_errno();
2497 goto done;
2499 if (repo_path == NULL) {
2500 repo_path = strdup(cwd);
2501 if (repo_path == NULL) {
2502 error = got_error_from_errno();
2503 goto done;
2508 error = got_repo_open(&repo, repo_path);
2509 if (error != NULL)
2510 return error;
2512 error = got_repo_map_path(&in_repo_path, repo, path);
2513 if (error != NULL)
2514 goto done;
2516 if (commit_id_str == NULL) {
2517 struct got_reference *head_ref;
2518 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2519 if (error != NULL)
2520 goto done;
2521 error = got_ref_resolve(&commit_id, repo, head_ref);
2522 got_ref_close(head_ref);
2523 } else {
2524 struct got_object *obj;
2525 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2526 if (error != NULL)
2527 goto done;
2528 commit_id = got_object_id_dup(got_object_get_id(obj));
2529 if (commit_id == NULL)
2530 error = got_error_from_errno();
2531 got_object_close(obj);
2533 if (error != NULL)
2534 goto done;
2536 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2537 if (view == NULL) {
2538 error = got_error_from_errno();
2539 goto done;
2541 error = open_blame_view(view, in_repo_path, commit_id, repo);
2542 if (error)
2543 goto done;
2544 error = view_loop(view);
2545 done:
2546 free(repo_path);
2547 free(cwd);
2548 free(commit_id);
2549 if (repo)
2550 got_repo_close(repo);
2551 return error;
2554 static const struct got_error *
2555 draw_tree_entries(struct tog_view *view,
2556 struct got_tree_entry **first_displayed_entry,
2557 struct got_tree_entry **last_displayed_entry,
2558 struct got_tree_entry **selected_entry, int *ndisplayed,
2559 const char *label, int show_ids, const char *parent_path,
2560 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2562 const struct got_error *err = NULL;
2563 struct got_tree_entry *te;
2564 wchar_t *wline;
2565 int width, n;
2567 *ndisplayed = 0;
2569 werase(view->window);
2571 if (limit == 0)
2572 return NULL;
2574 err = format_line(&wline, &width, label, view->ncols);
2575 if (err)
2576 return err;
2577 if (view_needs_focus_indication(view))
2578 wstandout(view->window);
2579 waddwstr(view->window, wline);
2580 if (view_needs_focus_indication(view))
2581 wstandend(view->window);
2582 free(wline);
2583 wline = NULL;
2584 if (width < view->ncols)
2585 waddch(view->window, '\n');
2586 if (--limit <= 0)
2587 return NULL;
2588 err = format_line(&wline, &width, parent_path, view->ncols);
2589 if (err)
2590 return err;
2591 waddwstr(view->window, wline);
2592 free(wline);
2593 wline = NULL;
2594 if (width < view->ncols)
2595 waddch(view->window, '\n');
2596 if (--limit <= 0)
2597 return NULL;
2598 waddch(view->window, '\n');
2599 if (--limit <= 0)
2600 return NULL;
2602 te = SIMPLEQ_FIRST(&entries->head);
2603 if (*first_displayed_entry == NULL) {
2604 if (selected == 0) {
2605 wstandout(view->window);
2606 *selected_entry = NULL;
2608 waddstr(view->window, " ..\n"); /* parent directory */
2609 if (selected == 0)
2610 wstandend(view->window);
2611 (*ndisplayed)++;
2612 if (--limit <= 0)
2613 return NULL;
2614 n = 1;
2615 } else {
2616 n = 0;
2617 while (te != *first_displayed_entry)
2618 te = SIMPLEQ_NEXT(te, entry);
2621 while (te) {
2622 char *line = NULL, *id_str = NULL;
2624 if (show_ids) {
2625 err = got_object_id_str(&id_str, te->id);
2626 if (err)
2627 return got_error_from_errno();
2629 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2630 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2631 free(id_str);
2632 return got_error_from_errno();
2634 free(id_str);
2635 err = format_line(&wline, &width, line, view->ncols);
2636 if (err) {
2637 free(line);
2638 break;
2640 if (n == selected) {
2641 wstandout(view->window);
2642 *selected_entry = te;
2644 waddwstr(view->window, wline);
2645 if (width < view->ncols)
2646 waddch(view->window, '\n');
2647 if (n == selected)
2648 wstandend(view->window);
2649 free(line);
2650 free(wline);
2651 wline = NULL;
2652 n++;
2653 (*ndisplayed)++;
2654 *last_displayed_entry = te;
2655 if (--limit <= 0)
2656 break;
2657 te = SIMPLEQ_NEXT(te, entry);
2660 view_vborder(view);
2661 return err;
2664 static void
2665 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2666 const struct got_tree_entries *entries, int isroot)
2668 struct got_tree_entry *te, *prev;
2669 int i;
2671 if (*first_displayed_entry == NULL)
2672 return;
2674 te = SIMPLEQ_FIRST(&entries->head);
2675 if (*first_displayed_entry == te) {
2676 if (!isroot)
2677 *first_displayed_entry = NULL;
2678 return;
2681 /* XXX this is stupid... switch to TAILQ? */
2682 for (i = 0; i < maxscroll; i++) {
2683 while (te != *first_displayed_entry) {
2684 prev = te;
2685 te = SIMPLEQ_NEXT(te, entry);
2687 *first_displayed_entry = prev;
2688 te = SIMPLEQ_FIRST(&entries->head);
2690 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2691 *first_displayed_entry = NULL;
2694 static void
2695 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2696 struct got_tree_entry *last_displayed_entry,
2697 const struct got_tree_entries *entries)
2699 struct got_tree_entry *next;
2700 int n = 0;
2702 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2703 return;
2705 if (*first_displayed_entry)
2706 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2707 else
2708 next = SIMPLEQ_FIRST(&entries->head);
2709 while (next) {
2710 *first_displayed_entry = next;
2711 if (++n >= maxscroll)
2712 break;
2713 next = SIMPLEQ_NEXT(next, entry);
2717 static const struct got_error *
2718 tree_entry_path(char **path, struct tog_parent_trees *parents,
2719 struct got_tree_entry *te)
2721 const struct got_error *err = NULL;
2722 struct tog_parent_tree *pt;
2723 size_t len = 2; /* for leading slash and NUL */
2725 TAILQ_FOREACH(pt, parents, entry)
2726 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2727 if (te)
2728 len += strlen(te->name);
2730 *path = calloc(1, len);
2731 if (path == NULL)
2732 return got_error_from_errno();
2734 (*path)[0] = '/';
2735 pt = TAILQ_LAST(parents, tog_parent_trees);
2736 while (pt) {
2737 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2738 err = got_error(GOT_ERR_NO_SPACE);
2739 goto done;
2741 if (strlcat(*path, "/", len) >= len) {
2742 err = got_error(GOT_ERR_NO_SPACE);
2743 goto done;
2745 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2747 if (te) {
2748 if (strlcat(*path, te->name, len) >= len) {
2749 err = got_error(GOT_ERR_NO_SPACE);
2750 goto done;
2753 done:
2754 if (err) {
2755 free(*path);
2756 *path = NULL;
2758 return err;
2761 static const struct got_error *
2762 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2763 struct got_tree_entry *te, struct tog_parent_trees *parents,
2764 struct got_object_id *commit_id, struct got_repository *repo)
2766 const struct got_error *err = NULL;
2767 char *path;
2768 struct tog_view *blame_view;
2770 err = tree_entry_path(&path, parents, te);
2771 if (err)
2772 return err;
2774 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2775 if (blame_view == NULL)
2776 return got_error_from_errno();
2778 err = open_blame_view(blame_view, path, commit_id, repo);
2779 if (err) {
2780 view_close(blame_view);
2781 free(path);
2782 } else
2783 *new_view = blame_view;
2784 return err;
2787 static const struct got_error *
2788 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2789 struct got_tree_entry *te, struct tog_parent_trees *parents,
2790 struct got_object_id *commit_id, struct got_repository *repo)
2792 struct tog_view *log_view;
2793 const struct got_error *err = NULL;
2794 char *path;
2796 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2797 if (log_view == NULL)
2798 return got_error_from_errno();
2800 err = tree_entry_path(&path, parents, te);
2801 if (err)
2802 return err;
2804 err = open_log_view(log_view, commit_id, repo, path);
2805 if (err)
2806 view_close(log_view);
2807 else
2808 *new_view = log_view;
2809 free(path);
2810 return err;
2813 static const struct got_error *
2814 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2815 struct got_object_id *commit_id, struct got_repository *repo)
2817 const struct got_error *err = NULL;
2818 char *commit_id_str = NULL;
2819 struct tog_tree_view_state *s = &view->state.tree;
2821 TAILQ_INIT(&s->parents);
2823 err = got_object_id_str(&commit_id_str, commit_id);
2824 if (err != NULL)
2825 goto done;
2827 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2828 err = got_error_from_errno();
2829 goto done;
2832 s->root = s->tree = root;
2833 s->entries = got_object_tree_get_entries(root);
2834 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2835 s->commit_id = commit_id;
2836 s->repo = repo;
2838 view->show = show_tree_view;
2839 view->input = input_tree_view;
2840 view->close = close_tree_view;
2841 done:
2842 free(commit_id_str);
2843 if (err)
2844 free(s->tree_label);
2845 return err;
2848 static const struct got_error *
2849 close_tree_view(struct tog_view *view)
2851 struct tog_tree_view_state *s = &view->state.tree;
2853 free(s->tree_label);
2854 while (!TAILQ_EMPTY(&s->parents)) {
2855 struct tog_parent_tree *parent;
2856 parent = TAILQ_FIRST(&s->parents);
2857 TAILQ_REMOVE(&s->parents, parent, entry);
2858 free(parent);
2861 if (s->tree != s->root)
2862 got_object_tree_close(s->tree);
2863 got_object_tree_close(s->root);
2865 return NULL;
2868 static const struct got_error *
2869 show_tree_view(struct tog_view *view)
2871 const struct got_error *err = NULL;
2872 struct tog_tree_view_state *s = &view->state.tree;
2873 char *parent_path;
2875 err = tree_entry_path(&parent_path, &s->parents, NULL);
2876 if (err)
2877 return err;
2879 err = draw_tree_entries(view, &s->first_displayed_entry,
2880 &s->last_displayed_entry, &s->selected_entry,
2881 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2882 s->entries, s->selected, view->nlines, s->tree == s->root);
2883 free(parent_path);
2884 return err;
2887 static const struct got_error *
2888 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2889 struct tog_view *view, int ch)
2891 const struct got_error *err = NULL;
2892 struct tog_tree_view_state *s = &view->state.tree;
2894 switch (ch) {
2895 case 'i':
2896 s->show_ids = !s->show_ids;
2897 break;
2898 case 'l':
2899 if (s->selected_entry) {
2900 err = log_tree_entry(new_view, view,
2901 s->selected_entry, &s->parents,
2902 s->commit_id, s->repo);
2904 break;
2905 case 'k':
2906 case KEY_UP:
2907 if (s->selected > 0)
2908 s->selected--;
2909 if (s->selected > 0)
2910 break;
2911 tree_scroll_up(&s->first_displayed_entry, 1,
2912 s->entries, s->tree == s->root);
2913 break;
2914 case KEY_PPAGE:
2915 if (SIMPLEQ_FIRST(&s->entries->head) ==
2916 s->first_displayed_entry) {
2917 if (s->tree != s->root)
2918 s->first_displayed_entry = NULL;
2919 s->selected = 0;
2920 break;
2922 tree_scroll_up(&s->first_displayed_entry,
2923 view->nlines, s->entries,
2924 s->tree == s->root);
2925 break;
2926 case 'j':
2927 case KEY_DOWN:
2928 if (s->selected < s->ndisplayed - 1) {
2929 s->selected++;
2930 break;
2932 tree_scroll_down(&s->first_displayed_entry, 1,
2933 s->last_displayed_entry, s->entries);
2934 break;
2935 case KEY_NPAGE:
2936 tree_scroll_down(&s->first_displayed_entry,
2937 view->nlines, s->last_displayed_entry,
2938 s->entries);
2939 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2940 entry))
2941 break;
2942 /* can't scroll any further; move cursor down */
2943 if (s->selected < s->ndisplayed - 1)
2944 s->selected = s->ndisplayed - 1;
2945 break;
2946 case KEY_ENTER:
2947 case '\r':
2948 if (s->selected_entry == NULL) {
2949 struct tog_parent_tree *parent;
2950 case KEY_LEFT:
2951 /* user selected '..' */
2952 if (s->tree == s->root)
2953 break;
2954 parent = TAILQ_FIRST(&s->parents);
2955 TAILQ_REMOVE(&s->parents, parent,
2956 entry);
2957 got_object_tree_close(s->tree);
2958 s->tree = parent->tree;
2959 s->entries =
2960 got_object_tree_get_entries(s->tree);
2961 s->first_displayed_entry =
2962 parent->first_displayed_entry;
2963 s->selected_entry =
2964 parent->selected_entry;
2965 s->selected = parent->selected;
2966 free(parent);
2967 } else if (S_ISDIR(s->selected_entry->mode)) {
2968 struct tog_parent_tree *parent;
2969 struct got_tree_object *child;
2970 err = got_object_open_as_tree(&child,
2971 s->repo, s->selected_entry->id);
2972 if (err)
2973 break;
2974 parent = calloc(1, sizeof(*parent));
2975 if (parent == NULL) {
2976 err = got_error_from_errno();
2977 break;
2979 parent->tree = s->tree;
2980 parent->first_displayed_entry =
2981 s->first_displayed_entry;
2982 parent->selected_entry = s->selected_entry;
2983 parent->selected = s->selected;
2984 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2985 s->tree = child;
2986 s->entries =
2987 got_object_tree_get_entries(s->tree);
2988 s->selected = 0;
2989 s->first_displayed_entry = NULL;
2990 } else if (S_ISREG(s->selected_entry->mode)) {
2991 err = blame_tree_entry(new_view, view,
2992 s->selected_entry, &s->parents,
2993 s->commit_id, s->repo);
2994 if (err)
2995 break;
2997 break;
2998 case KEY_RESIZE:
2999 if (s->selected > view->nlines)
3000 s->selected = s->ndisplayed - 1;
3001 break;
3002 default:
3003 break;
3006 return err;
3009 __dead static void
3010 usage_tree(void)
3012 endwin();
3013 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3014 getprogname());
3015 exit(1);
3018 static const struct got_error *
3019 cmd_tree(int argc, char *argv[])
3021 const struct got_error *error;
3022 struct got_repository *repo = NULL;
3023 char *repo_path = NULL;
3024 struct got_object_id *commit_id = NULL;
3025 char *commit_id_arg = NULL;
3026 struct got_commit_object *commit = NULL;
3027 struct got_tree_object *tree = NULL;
3028 int ch;
3029 struct tog_view *view;
3031 #ifndef PROFILE
3032 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3033 == -1)
3034 err(1, "pledge");
3035 #endif
3037 while ((ch = getopt(argc, argv, "c:")) != -1) {
3038 switch (ch) {
3039 case 'c':
3040 commit_id_arg = optarg;
3041 break;
3042 default:
3043 usage();
3044 /* NOTREACHED */
3048 argc -= optind;
3049 argv += optind;
3051 if (argc == 0) {
3052 repo_path = getcwd(NULL, 0);
3053 if (repo_path == NULL)
3054 return got_error_from_errno();
3055 } else if (argc == 1) {
3056 repo_path = realpath(argv[0], NULL);
3057 if (repo_path == NULL)
3058 return got_error_from_errno();
3059 } else
3060 usage_log();
3062 error = got_repo_open(&repo, repo_path);
3063 free(repo_path);
3064 if (error != NULL)
3065 return error;
3067 if (commit_id_arg == NULL) {
3068 error = get_head_commit_id(&commit_id, repo);
3069 if (error != NULL)
3070 goto done;
3071 } else {
3072 struct got_object *obj;
3073 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3074 if (error == NULL) {
3075 commit_id = got_object_id_dup(got_object_get_id(obj));
3076 if (commit_id == NULL)
3077 error = got_error_from_errno();
3080 if (error != NULL)
3081 goto done;
3083 error = got_object_open_as_commit(&commit, repo, commit_id);
3084 if (error != NULL)
3085 goto done;
3087 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3088 if (error != NULL)
3089 goto done;
3091 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3092 if (view == NULL) {
3093 error = got_error_from_errno();
3094 goto done;
3096 error = open_tree_view(view, tree, commit_id, repo);
3097 if (error)
3098 goto done;
3099 error = view_loop(view);
3100 done:
3101 free(commit_id);
3102 if (commit)
3103 got_object_commit_close(commit);
3104 if (tree)
3105 got_object_tree_close(tree);
3106 if (repo)
3107 got_repo_close(repo);
3108 return error;
3110 static void
3111 init_curses(void)
3113 initscr();
3114 cbreak();
3115 noecho();
3116 nonl();
3117 intrflush(stdscr, FALSE);
3118 keypad(stdscr, TRUE);
3119 curs_set(0);
3122 __dead static void
3123 usage(void)
3125 int i;
3127 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3128 "Available commands:\n", getprogname());
3129 for (i = 0; i < nitems(tog_commands); i++) {
3130 struct tog_cmd *cmd = &tog_commands[i];
3131 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3133 exit(1);
3136 static char **
3137 make_argv(const char *arg0, const char *arg1)
3139 char **argv;
3140 int argc = (arg1 == NULL ? 1 : 2);
3142 argv = calloc(argc, sizeof(char *));
3143 if (argv == NULL)
3144 err(1, "calloc");
3145 argv[0] = strdup(arg0);
3146 if (argv[0] == NULL)
3147 err(1, "calloc");
3148 if (arg1) {
3149 argv[1] = strdup(arg1);
3150 if (argv[1] == NULL)
3151 err(1, "calloc");
3154 return argv;
3157 int
3158 main(int argc, char *argv[])
3160 const struct got_error *error = NULL;
3161 struct tog_cmd *cmd = NULL;
3162 int ch, hflag = 0;
3163 char **cmd_argv = NULL;
3165 setlocale(LC_ALL, "");
3167 while ((ch = getopt(argc, argv, "h")) != -1) {
3168 switch (ch) {
3169 case 'h':
3170 hflag = 1;
3171 break;
3172 default:
3173 usage();
3174 /* NOTREACHED */
3178 argc -= optind;
3179 argv += optind;
3180 optind = 0;
3181 optreset = 1;
3183 if (argc == 0) {
3184 if (hflag)
3185 usage();
3186 /* Build an argument vector which runs a default command. */
3187 cmd = &tog_commands[0];
3188 cmd_argv = make_argv(cmd->name, NULL);
3189 argc = 1;
3190 } else {
3191 int i;
3193 /* Did the user specific a command? */
3194 for (i = 0; i < nitems(tog_commands); i++) {
3195 if (strncmp(tog_commands[i].name, argv[0],
3196 strlen(argv[0])) == 0) {
3197 cmd = &tog_commands[i];
3198 if (hflag)
3199 tog_commands[i].cmd_usage();
3200 break;
3203 if (cmd == NULL) {
3204 /* Did the user specify a repository? */
3205 char *repo_path = realpath(argv[0], NULL);
3206 if (repo_path) {
3207 struct got_repository *repo;
3208 error = got_repo_open(&repo, repo_path);
3209 if (error == NULL)
3210 got_repo_close(repo);
3211 } else
3212 error = got_error_from_errno();
3213 if (error) {
3214 if (hflag) {
3215 fprintf(stderr, "%s: '%s' is not a "
3216 "known command\n", getprogname(),
3217 argv[0]);
3218 usage();
3220 fprintf(stderr, "%s: '%s' is neither a known "
3221 "command nor a path to a repository\n",
3222 getprogname(), argv[0]);
3223 free(repo_path);
3224 return 1;
3226 cmd = &tog_commands[0];
3227 cmd_argv = make_argv(cmd->name, repo_path);
3228 argc = 2;
3229 free(repo_path);
3233 init_curses();
3235 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3236 if (error)
3237 goto done;
3238 done:
3239 endwin();
3240 free(cmd_argv);
3241 if (error)
3242 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3243 return 0;