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++;
615 break;
616 case -1:
617 if (wline[i] == L'\t')
618 cols += TABSIZE - ((cols + 1) % TABSIZE);
619 i++;
620 break;
621 default:
622 err = got_error_from_errno();
623 goto done;
626 wline[i] = L'\0';
627 if (widthp)
628 *widthp = cols;
629 done:
630 if (err)
631 free(wline);
632 else
633 *wlinep = wline;
634 return err;
637 static const struct got_error *
638 draw_commit(struct tog_view *view, struct got_commit_object *commit,
639 struct got_object_id *id)
641 const struct got_error *err = NULL;
642 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
643 char *logmsg0 = NULL, *logmsg = NULL;
644 char *author0 = NULL, *author = NULL;
645 wchar_t *wlogmsg = NULL, *wauthor = NULL;
646 int author_width, logmsg_width;
647 char *newline, *smallerthan;
648 char *line = NULL;
649 int col, limit;
650 static const size_t date_display_cols = 9;
651 static const size_t author_display_cols = 16;
652 const int avail = view->ncols;
654 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
655 &commit->tm_committer) >= sizeof(datebuf))
656 return got_error(GOT_ERR_NO_SPACE);
658 if (avail < date_display_cols)
659 limit = MIN(sizeof(datebuf) - 1, avail);
660 else
661 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
662 waddnstr(view->window, datebuf, limit);
663 col = limit + 1;
664 if (col > avail)
665 goto done;
667 author0 = strdup(commit->author);
668 if (author0 == NULL) {
669 err = got_error_from_errno();
670 goto done;
672 author = author0;
673 smallerthan = strchr(author, '<');
674 if (smallerthan)
675 *smallerthan = '\0';
676 else {
677 char *at = strchr(author, '@');
678 if (at)
679 *at = '\0';
681 limit = avail - col;
682 err = format_line(&wauthor, &author_width, author, limit);
683 if (err)
684 goto done;
685 waddwstr(view->window, wauthor);
686 col += author_width;
687 while (col <= avail && author_width < author_display_cols + 1) {
688 waddch(view->window, ' ');
689 col++;
690 author_width++;
692 if (col > avail)
693 goto done;
695 logmsg0 = strdup(commit->logmsg);
696 if (logmsg0 == NULL) {
697 err = got_error_from_errno();
698 goto done;
700 logmsg = logmsg0;
701 while (*logmsg == '\n')
702 logmsg++;
703 newline = strchr(logmsg, '\n');
704 if (newline)
705 *newline = '\0';
706 limit = avail - col;
707 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
708 if (err)
709 goto done;
710 waddwstr(view->window, wlogmsg);
711 col += logmsg_width;
712 while (col <= avail) {
713 waddch(view->window, ' ');
714 col++;
716 done:
717 free(logmsg0);
718 free(wlogmsg);
719 free(author0);
720 free(wauthor);
721 free(line);
722 return err;
725 static struct commit_queue_entry *
726 alloc_commit_queue_entry(struct got_commit_object *commit,
727 struct got_object_id *id)
729 struct commit_queue_entry *entry;
731 entry = calloc(1, sizeof(*entry));
732 if (entry == NULL)
733 return NULL;
735 entry->id = id;
736 entry->commit = commit;
737 return entry;
740 static void
741 pop_commit(struct commit_queue *commits)
743 struct commit_queue_entry *entry;
745 entry = TAILQ_FIRST(&commits->head);
746 TAILQ_REMOVE(&commits->head, entry, entry);
747 got_object_commit_close(entry->commit);
748 commits->ncommits--;
749 /* Don't free entry->id! It is owned by the commit graph. */
750 free(entry);
753 static void
754 free_commits(struct commit_queue *commits)
756 while (!TAILQ_EMPTY(&commits->head))
757 pop_commit(commits);
760 static const struct got_error *
761 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
762 struct got_object_id *start_id, int minqueue, int init,
763 struct got_repository *repo, const char *path)
765 const struct got_error *err = NULL;
766 struct got_object_id *id;
767 struct commit_queue_entry *entry;
768 int nfetched, nqueued = 0, found_obj = 0;
769 int is_root_path = strcmp(path, "/") == 0;
771 err = got_commit_graph_iter_start(graph, start_id);
772 if (err)
773 return err;
775 entry = TAILQ_LAST(&commits->head, commit_queue_head);
776 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
777 int nfetched;
779 /* Start ID's commit is already on the queue; skip over it. */
780 err = got_commit_graph_iter_next(&id, graph);
781 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
782 return err;
784 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
785 if (err)
786 return err;
789 while (1) {
790 struct got_commit_object *commit;
792 err = got_commit_graph_iter_next(&id, graph);
793 if (err) {
794 if (err->code != GOT_ERR_ITER_NEED_MORE)
795 break;
796 if (nqueued >= minqueue) {
797 err = NULL;
798 break;
800 err = got_commit_graph_fetch_commits(&nfetched,
801 graph, 1, repo);
802 if (err)
803 return err;
804 continue;
806 if (id == NULL)
807 break;
809 err = got_object_open_as_commit(&commit, repo, id);
810 if (err)
811 break;
813 if (!is_root_path) {
814 struct got_object *obj;
815 struct got_object_qid *pid;
816 int changed = 0;
818 err = got_object_open_by_path(&obj, repo, id, path);
819 if (err) {
820 got_object_commit_close(commit);
821 if (err->code == GOT_ERR_NO_OBJ &&
822 (found_obj || !init)) {
823 /* History stops here. */
824 err = got_error(GOT_ERR_ITER_COMPLETED);
826 break;
828 found_obj = 1;
830 pid = SIMPLEQ_FIRST(&commit->parent_ids);
831 if (pid != NULL) {
832 struct got_object *pobj;
833 err = got_object_open_by_path(&pobj, repo,
834 pid->id, path);
835 if (err) {
836 if (err->code != GOT_ERR_NO_OBJ) {
837 got_object_close(obj);
838 got_object_commit_close(commit);
839 break;
841 err = NULL;
842 changed = 1;
843 } else {
844 struct got_object_id *id, *pid;
845 id = got_object_get_id(obj);
846 if (id == NULL) {
847 err = got_error_from_errno();
848 got_object_close(obj);
849 got_object_close(pobj);
850 break;
852 pid = got_object_get_id(pobj);
853 if (pid == NULL) {
854 err = got_error_from_errno();
855 free(id);
856 got_object_close(obj);
857 got_object_close(pobj);
858 break;
860 changed =
861 (got_object_id_cmp(id, pid) != 0);
862 got_object_close(pobj);
863 free(id);
864 free(pid);
867 got_object_close(obj);
868 if (!changed) {
869 got_object_commit_close(commit);
870 continue;
874 entry = alloc_commit_queue_entry(commit, id);
875 if (entry == NULL) {
876 err = got_error_from_errno();
877 break;
879 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
880 nqueued++;
881 commits->ncommits++;
884 return err;
887 static const struct got_error *
888 fetch_next_commit(struct commit_queue_entry **pentry,
889 struct commit_queue_entry *entry, struct commit_queue *commits,
890 struct got_commit_graph *graph, struct got_repository *repo,
891 const char *path)
893 const struct got_error *err = NULL;
895 *pentry = NULL;
897 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
898 if (err)
899 return err;
901 /* Next entry to display should now be available. */
902 *pentry = TAILQ_NEXT(entry, entry);
903 if (*pentry == NULL)
904 return got_error(GOT_ERR_NO_OBJ);
906 return NULL;
909 static const struct got_error *
910 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
912 const struct got_error *err = NULL;
913 struct got_reference *head_ref;
915 *head_id = NULL;
917 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
918 if (err)
919 return err;
921 err = got_ref_resolve(head_id, repo, head_ref);
922 got_ref_close(head_ref);
923 if (err) {
924 *head_id = NULL;
925 return err;
928 return NULL;
931 static const struct got_error *
932 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
933 struct commit_queue_entry **selected, struct commit_queue_entry *first,
934 struct commit_queue *commits, int selected_idx, int limit,
935 struct got_commit_graph *graph, struct got_repository *repo,
936 const char *path)
938 const struct got_error *err = NULL;
939 struct commit_queue_entry *entry;
940 int ncommits, width;
941 char *id_str, *header;
942 wchar_t *wline;
944 entry = first;
945 ncommits = 0;
946 while (entry) {
947 if (ncommits == selected_idx) {
948 *selected = entry;
949 break;
951 entry = TAILQ_NEXT(entry, entry);
952 ncommits++;
955 err = got_object_id_str(&id_str, (*selected)->id);
956 if (err)
957 return err;
959 if (path && strcmp(path, "/") != 0) {
960 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
961 err = got_error_from_errno();
962 free(id_str);
963 return err;
965 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
966 err = got_error_from_errno();
967 free(id_str);
968 return err;
970 free(id_str);
971 err = format_line(&wline, &width, header, view->ncols);
972 if (err) {
973 free(header);
974 return err;
976 free(header);
978 werase(view->window);
980 if (view_needs_focus_indication(view))
981 wstandout(view->window);
982 waddwstr(view->window, wline);
983 if (view_needs_focus_indication(view))
984 wstandend(view->window);
985 if (width < view->ncols)
986 waddch(view->window, '\n');
987 free(wline);
988 if (limit <= 1)
989 return NULL;
991 entry = first;
992 *last = first;
993 ncommits = 0;
994 while (entry) {
995 if (ncommits >= limit - 1)
996 break;
997 if (ncommits == selected_idx)
998 wstandout(view->window);
999 err = draw_commit(view, entry->commit, entry->id);
1000 if (ncommits == selected_idx)
1001 wstandend(view->window);
1002 if (err)
1003 break;
1004 ncommits++;
1005 *last = entry;
1006 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1007 err = queue_commits(graph, commits, entry->id, 1,
1008 0, repo, path);
1009 if (err) {
1010 if (err->code != GOT_ERR_ITER_COMPLETED)
1011 return err;
1012 err = NULL;
1015 entry = TAILQ_NEXT(entry, entry);
1018 view_vborder(view);
1020 return err;
1023 static void
1024 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1025 struct commit_queue *commits)
1027 struct commit_queue_entry *entry;
1028 int nscrolled = 0;
1030 entry = TAILQ_FIRST(&commits->head);
1031 if (*first_displayed_entry == entry)
1032 return;
1034 entry = *first_displayed_entry;
1035 while (entry && nscrolled < maxscroll) {
1036 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1037 if (entry) {
1038 *first_displayed_entry = entry;
1039 nscrolled++;
1044 static const struct got_error *
1045 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1046 struct commit_queue_entry *last_displayed_entry,
1047 struct commit_queue *commits, struct got_commit_graph *graph,
1048 struct got_repository *repo, const char *path)
1050 const struct got_error *err = NULL;
1051 struct commit_queue_entry *pentry;
1052 int nscrolled = 0;
1054 do {
1055 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1056 if (pentry == NULL) {
1057 err = fetch_next_commit(&pentry, last_displayed_entry,
1058 commits, graph, repo, path);
1059 if (err || pentry == NULL)
1060 break;
1062 last_displayed_entry = pentry;
1064 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1065 if (pentry == NULL)
1066 break;
1067 *first_displayed_entry = pentry;
1068 } while (++nscrolled < maxscroll);
1070 return err;
1073 static const struct got_error *
1074 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1075 struct commit_queue_entry *entry, struct got_repository *repo)
1077 const struct got_error *err;
1078 struct got_object *obj1 = NULL, *obj2 = NULL;
1079 struct got_object_qid *parent_id;
1080 struct tog_view *diff_view;
1082 err = got_object_open(&obj2, repo, entry->id);
1083 if (err)
1084 return err;
1086 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1087 if (parent_id) {
1088 err = got_object_open(&obj1, repo, parent_id->id);
1089 if (err)
1090 goto done;
1093 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1094 if (diff_view == NULL) {
1095 err = got_error_from_errno();
1096 goto done;
1099 err = open_diff_view(diff_view, obj1, obj2, repo);
1100 if (err == NULL)
1101 *new_view = diff_view;
1102 done:
1103 if (obj1)
1104 got_object_close(obj1);
1105 if (obj2)
1106 got_object_close(obj2);
1107 return err;
1110 static const struct got_error *
1111 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1112 struct commit_queue_entry *entry, struct got_repository *repo)
1114 const struct got_error *err = NULL;
1115 struct got_tree_object *tree;
1116 struct tog_view *tree_view;
1118 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1119 if (err)
1120 return err;
1122 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1123 if (tree_view == NULL)
1124 return got_error_from_errno();
1126 err = open_tree_view(tree_view, tree, entry->id, repo);
1127 if (err)
1128 got_object_tree_close(tree);
1129 else
1130 *new_view = tree_view;
1131 return err;
1134 static const struct got_error *
1135 set_child_log_view(struct tog_view *view, struct tog_view *child)
1137 struct tog_log_view_state *s = &view->state.log;
1138 struct tog_diff_view_state *ds;
1139 struct commit_queue_entry *commit, *child_entry = NULL;
1140 int selected_idx = 0;
1142 if (child->type != TOG_VIEW_DIFF)
1143 return NULL;
1144 ds = &child->state.diff;
1146 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1147 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1148 child_entry = commit;
1149 break;
1152 if (child_entry == NULL)
1153 return NULL;
1155 commit = s->first_displayed_entry;
1156 while (commit) {
1157 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1158 s->selected_entry = child_entry;
1159 s->selected = selected_idx;
1160 break;
1162 if (commit == s->last_displayed_entry)
1163 break;
1164 selected_idx++;
1165 commit = TAILQ_NEXT(commit, entry);
1168 return show_log_view(view);
1171 static const struct got_error *
1172 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1173 struct got_repository *repo, const char *path)
1175 const struct got_error *err = NULL;
1176 struct got_object_id *head_id = NULL;
1177 int nfetched;
1178 struct tog_log_view_state *s = &view->state.log;
1180 err = got_repo_map_path(&s->in_repo_path, repo, path);
1181 if (err != NULL)
1182 goto done;
1184 err = get_head_commit_id(&head_id, repo);
1185 if (err)
1186 return err;
1188 /* The graph contains all commits. */
1189 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1190 if (err)
1191 goto done;
1192 /* The commit queue contains a subset of commits filtered by path. */
1193 TAILQ_INIT(&s->commits.head);
1194 s->commits.ncommits = 0;
1196 /* Populate commit graph with a sufficient number of commits. */
1197 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1198 start_id, repo);
1199 if (err)
1200 goto done;
1203 * Open the initial batch of commits, sorted in commit graph order.
1204 * We keep all commits open throughout the lifetime of the log view
1205 * in order to avoid having to re-fetch commits from disk while
1206 * updating the display.
1208 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1209 repo, s->in_repo_path);
1210 if (err) {
1211 if (err->code != GOT_ERR_ITER_COMPLETED)
1212 goto done;
1213 err = NULL;
1216 s->first_displayed_entry =
1217 TAILQ_FIRST(&s->commits.head);
1218 s->selected_entry = s->first_displayed_entry;
1219 s->repo = repo;
1221 view->show = show_log_view;
1222 view->input = input_log_view;
1223 view->close = close_log_view;
1224 view->set_child = set_child_log_view;
1225 done:
1226 free(head_id);
1227 return err;
1230 static const struct got_error *
1231 close_log_view(struct tog_view *view)
1233 struct tog_log_view_state *s = &view->state.log;
1235 if (s->graph)
1236 got_commit_graph_close(s->graph);
1237 free_commits(&s->commits);
1238 free(s->in_repo_path);
1239 return NULL;
1242 static const struct got_error *
1243 update_diff_child_view(struct tog_view *parent,
1244 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1246 const struct got_error *err = NULL;
1247 struct tog_diff_view_state *ds;
1248 struct got_object *obj1 = NULL, *obj2 = NULL;
1249 struct got_object_qid *parent_id;
1250 struct tog_view *child_view = parent->child;
1252 if (child_view == NULL)
1253 return NULL;
1254 if (child_view->type != TOG_VIEW_DIFF)
1255 return NULL;
1256 ds = &child_view->state.diff;
1257 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1258 return NULL;
1260 err = got_object_open(&obj2, repo, selected_entry->id);
1261 if (err)
1262 return err;
1264 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1265 if (parent_id) {
1266 err = got_object_open(&obj1, repo, parent_id->id);
1267 if (err)
1268 goto done;
1271 err = close_diff_view(child_view);
1272 if (err)
1273 goto done;
1275 err = open_diff_view(child_view, obj1, obj2, repo);
1276 if (err)
1277 goto done;
1278 done:
1279 if (obj1)
1280 got_object_close(obj1);
1281 if (obj2)
1282 got_object_close(obj2);
1283 return err;
1286 static const struct got_error *
1287 show_log_view(struct tog_view *view)
1289 const struct got_error *err = NULL;
1290 struct tog_log_view_state *s = &view->state.log;
1292 err = draw_commits(view, &s->last_displayed_entry,
1293 &s->selected_entry, s->first_displayed_entry,
1294 &s->commits, s->selected, view->nlines, s->graph,
1295 s->repo, s->in_repo_path);
1296 if (err)
1297 return err;
1299 return update_diff_child_view(view, s->selected_entry, s->repo);
1302 static const struct got_error *
1303 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1304 struct tog_view *view, int ch)
1306 const struct got_error *err = NULL;
1307 struct tog_log_view_state *s = &view->state.log;
1309 switch (ch) {
1310 case 'k':
1311 case KEY_UP:
1312 case '[':
1313 if (s->selected > 0)
1314 s->selected--;
1315 if (s->selected > 0)
1316 break;
1317 scroll_up(&s->first_displayed_entry, 1,
1318 &s->commits);
1319 break;
1320 case KEY_PPAGE:
1321 if (TAILQ_FIRST(&s->commits.head) ==
1322 s->first_displayed_entry) {
1323 s->selected = 0;
1324 break;
1326 scroll_up(&s->first_displayed_entry,
1327 view->nlines, &s->commits);
1328 break;
1329 case 'j':
1330 case KEY_DOWN:
1331 case ']':
1332 if (s->selected < MIN(view->nlines - 2,
1333 s->commits.ncommits - 1)) {
1334 s->selected++;
1335 break;
1337 err = scroll_down(&s->first_displayed_entry, 1,
1338 s->last_displayed_entry, &s->commits,
1339 s->graph, s->repo, s->in_repo_path);
1340 if (err) {
1341 if (err->code != GOT_ERR_ITER_COMPLETED)
1342 break;
1343 err = NULL;
1345 break;
1346 case KEY_NPAGE: {
1347 struct commit_queue_entry *first;
1348 first = s->first_displayed_entry;
1349 err = scroll_down(&s->first_displayed_entry,
1350 view->nlines, s->last_displayed_entry,
1351 &s->commits, s->graph, s->repo,
1352 s->in_repo_path);
1353 if (err == NULL)
1354 break;
1355 if (err->code != GOT_ERR_ITER_COMPLETED)
1356 break;
1357 if (first == s->first_displayed_entry &&
1358 s->selected < MIN(view->nlines - 2,
1359 s->commits.ncommits - 1)) {
1360 /* can't scroll further down */
1361 s->selected = MIN(view->nlines - 2,
1362 s->commits.ncommits - 1);
1364 err = NULL;
1365 break;
1367 case KEY_RESIZE:
1368 if (s->selected > view->nlines - 2)
1369 s->selected = view->nlines - 2;
1370 if (s->selected > s->commits.ncommits - 1)
1371 s->selected = s->commits.ncommits - 1;
1372 break;
1373 case KEY_ENTER:
1374 case '\r':
1375 err = show_commit(new_view, view, s->selected_entry,
1376 s->repo);
1377 break;
1378 case 't':
1379 err = browse_commit(new_view, view, s->selected_entry,
1380 s->repo);
1381 break;
1382 default:
1383 break;
1386 return err;
1389 static const struct got_error *
1390 cmd_log(int argc, char *argv[])
1392 const struct got_error *error;
1393 struct got_repository *repo = NULL;
1394 struct got_object_id *start_id = NULL;
1395 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1396 char *start_commit = NULL;
1397 int ch;
1398 struct tog_view *view;
1400 #ifndef PROFILE
1401 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1402 == -1)
1403 err(1, "pledge");
1404 #endif
1406 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1407 switch (ch) {
1408 case 'c':
1409 start_commit = optarg;
1410 break;
1411 case 'r':
1412 repo_path = realpath(optarg, NULL);
1413 if (repo_path == NULL)
1414 err(1, "-r option");
1415 break;
1416 default:
1417 usage();
1418 /* NOTREACHED */
1422 argc -= optind;
1423 argv += optind;
1425 if (argc == 0)
1426 path = strdup("");
1427 else if (argc == 1)
1428 path = strdup(argv[0]);
1429 else
1430 usage_log();
1431 if (path == NULL)
1432 return got_error_from_errno();
1434 cwd = getcwd(NULL, 0);
1435 if (cwd == NULL) {
1436 error = got_error_from_errno();
1437 goto done;
1439 if (repo_path == NULL) {
1440 repo_path = strdup(cwd);
1441 if (repo_path == NULL) {
1442 error = got_error_from_errno();
1443 goto done;
1447 error = got_repo_open(&repo, repo_path);
1448 if (error != NULL)
1449 goto done;
1451 if (start_commit == NULL) {
1452 error = get_head_commit_id(&start_id, repo);
1453 if (error != NULL)
1454 goto done;
1455 } else {
1456 struct got_object *obj;
1457 error = got_object_open_by_id_str(&obj, repo, start_commit);
1458 if (error == NULL) {
1459 start_id = got_object_get_id(obj);
1460 if (start_id == NULL)
1461 error = got_error_from_errno();
1462 goto done;
1465 if (error != NULL)
1466 goto done;
1468 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1469 if (view == NULL) {
1470 error = got_error_from_errno();
1471 goto done;
1473 error = open_log_view(view, start_id, repo, path);
1474 if (error)
1475 goto done;
1476 error = view_loop(view);
1477 done:
1478 free(repo_path);
1479 free(cwd);
1480 free(path);
1481 free(start_id);
1482 if (repo)
1483 got_repo_close(repo);
1484 return error;
1487 __dead static void
1488 usage_diff(void)
1490 endwin();
1491 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1492 getprogname());
1493 exit(1);
1496 static char *
1497 parse_next_line(FILE *f, size_t *len)
1499 char *line;
1500 size_t linelen;
1501 size_t lineno;
1502 const char delim[3] = { '\0', '\0', '\0'};
1504 line = fparseln(f, &linelen, &lineno, delim, 0);
1505 if (len)
1506 *len = linelen;
1507 return line;
1510 static const struct got_error *
1511 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1512 int *last_displayed_line, int *eof, int max_lines,
1513 char * header)
1515 const struct got_error *err;
1516 int nlines = 0, nprinted = 0;
1517 char *line;
1518 size_t len;
1519 wchar_t *wline;
1520 int width;
1522 rewind(f);
1523 werase(view->window);
1525 if (header) {
1526 err = format_line(&wline, &width, header, view->ncols);
1527 if (err) {
1528 return err;
1531 if (view_needs_focus_indication(view))
1532 wstandout(view->window);
1533 waddwstr(view->window, wline);
1534 if (view_needs_focus_indication(view))
1535 wstandend(view->window);
1536 if (width < view->ncols)
1537 waddch(view->window, '\n');
1539 if (max_lines <= 1)
1540 return NULL;
1541 max_lines--;
1544 *eof = 0;
1545 while (nprinted < max_lines) {
1546 line = parse_next_line(f, &len);
1547 if (line == NULL) {
1548 *eof = 1;
1549 break;
1551 if (++nlines < *first_displayed_line) {
1552 free(line);
1553 continue;
1556 err = format_line(&wline, &width, line, view->ncols);
1557 if (err) {
1558 free(line);
1559 return err;
1561 waddwstr(view->window, wline);
1562 if (width < view->ncols)
1563 waddch(view->window, '\n');
1564 if (++nprinted == 1)
1565 *first_displayed_line = nlines;
1566 free(line);
1567 free(wline);
1568 wline = NULL;
1570 *last_displayed_line = nlines;
1572 view_vborder(view);
1574 return NULL;
1577 static const struct got_error *
1578 open_diff_view(struct tog_view *view, struct got_object *obj1,
1579 struct got_object *obj2, struct got_repository *repo)
1581 const struct got_error *err;
1582 FILE *f;
1584 if (obj1 != NULL && obj2 != NULL &&
1585 got_object_get_type(obj1) != got_object_get_type(obj2))
1586 return got_error(GOT_ERR_OBJ_TYPE);
1588 f = got_opentemp();
1589 if (f == NULL)
1590 return got_error_from_errno();
1592 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1593 case GOT_OBJ_TYPE_BLOB:
1594 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1595 break;
1596 case GOT_OBJ_TYPE_TREE:
1597 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1598 break;
1599 case GOT_OBJ_TYPE_COMMIT:
1600 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1601 break;
1602 default:
1603 return got_error(GOT_ERR_OBJ_TYPE);
1606 fflush(f);
1608 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1609 view->state.diff.id2 = got_object_get_id(obj2);
1610 if (view->state.diff.id2 == NULL)
1611 return got_error_from_errno();
1612 view->state.diff.f = f;
1613 view->state.diff.first_displayed_line = 1;
1614 view->state.diff.last_displayed_line = view->nlines;
1616 view->show = show_diff_view;
1617 view->input = input_diff_view;
1618 view->close = close_diff_view;
1620 return NULL;
1623 static const struct got_error *
1624 close_diff_view(struct tog_view *view)
1626 const struct got_error *err = NULL;
1628 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1629 err = got_error_from_errno();
1630 free(view->state.diff.id1);
1631 free(view->state.diff.id2);
1632 return err;
1635 static const struct got_error *
1636 show_diff_view(struct tog_view *view)
1638 const struct got_error *err;
1639 struct tog_diff_view_state *s = &view->state.diff;
1640 char *id_str1 = NULL, *id_str2, *header;
1642 if (s->id1) {
1643 err = got_object_id_str(&id_str1, s->id1);
1644 if (err)
1645 return err;
1647 err = got_object_id_str(&id_str2, s->id2);
1648 if (err)
1649 return err;
1651 if (asprintf(&header, "diff: %s %s",
1652 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1653 err = got_error_from_errno();
1654 free(id_str1);
1655 free(id_str2);
1656 return err;
1658 free(id_str1);
1659 free(id_str2);
1661 return draw_file(view, s->f, &s->first_displayed_line,
1662 &s->last_displayed_line, &s->eof, view->nlines,
1663 header);
1666 static const struct got_error *
1667 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1668 struct tog_view *view, int ch)
1670 const struct got_error *err = NULL;
1671 struct tog_diff_view_state *s = &view->state.diff;
1672 int i;
1674 switch (ch) {
1675 case 'k':
1676 case KEY_UP:
1677 if (s->first_displayed_line > 1)
1678 s->first_displayed_line--;
1679 break;
1680 case KEY_PPAGE:
1681 i = 0;
1682 while (i++ < view->nlines - 1 &&
1683 s->first_displayed_line > 1)
1684 s->first_displayed_line--;
1685 break;
1686 case 'j':
1687 case KEY_DOWN:
1688 if (!s->eof)
1689 s->first_displayed_line++;
1690 break;
1691 case KEY_NPAGE:
1692 case ' ':
1693 i = 0;
1694 while (!s->eof && i++ < view->nlines - 1) {
1695 char *line;
1696 line = parse_next_line(s->f, NULL);
1697 s->first_displayed_line++;
1698 if (line == NULL)
1699 break;
1701 break;
1702 case '[':
1703 case ']': {
1704 struct tog_log_view_state *ls;
1705 struct commit_queue_entry *entry;
1706 struct tog_view *diff_view;
1708 if (view->parent == NULL)
1709 break;
1710 if (view->parent->type != TOG_VIEW_LOG)
1711 break;
1712 ls = &view->parent->state.log;
1714 if (ch == '[') {
1715 entry = TAILQ_PREV(ls->selected_entry,
1716 commit_queue_head, entry);
1717 } else {
1718 entry = TAILQ_NEXT(ls->selected_entry, entry);
1719 if (entry == NULL) {
1720 err = fetch_next_commit(&entry,
1721 ls->selected_entry,
1722 &ls->commits, ls->graph,
1723 ls->repo, ls->in_repo_path);
1724 if (err)
1725 break;
1728 if (entry == NULL)
1729 break;
1730 err = show_commit(&diff_view, view->parent,
1731 entry, ls->repo);
1732 if (err)
1733 break;
1734 *new_view = diff_view;
1735 *dead_view = view;
1736 break;
1738 default:
1739 break;
1742 return err;
1745 static const struct got_error *
1746 cmd_diff(int argc, char *argv[])
1748 const struct got_error *error = NULL;
1749 struct got_repository *repo = NULL;
1750 struct got_object *obj1 = NULL, *obj2 = NULL;
1751 char *repo_path = NULL;
1752 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1753 int ch;
1754 struct tog_view *view;
1756 #ifndef PROFILE
1757 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1758 == -1)
1759 err(1, "pledge");
1760 #endif
1762 while ((ch = getopt(argc, argv, "")) != -1) {
1763 switch (ch) {
1764 default:
1765 usage();
1766 /* NOTREACHED */
1770 argc -= optind;
1771 argv += optind;
1773 if (argc == 0) {
1774 usage_diff(); /* TODO show local worktree changes */
1775 } else if (argc == 2) {
1776 repo_path = getcwd(NULL, 0);
1777 if (repo_path == NULL)
1778 return got_error_from_errno();
1779 obj_id_str1 = argv[0];
1780 obj_id_str2 = argv[1];
1781 } else if (argc == 3) {
1782 repo_path = realpath(argv[0], NULL);
1783 if (repo_path == NULL)
1784 return got_error_from_errno();
1785 obj_id_str1 = argv[1];
1786 obj_id_str2 = argv[2];
1787 } else
1788 usage_diff();
1790 error = got_repo_open(&repo, repo_path);
1791 free(repo_path);
1792 if (error)
1793 goto done;
1795 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1796 if (error)
1797 goto done;
1799 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1800 if (error)
1801 goto done;
1803 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1804 if (view == NULL) {
1805 error = got_error_from_errno();
1806 goto done;
1808 error = open_diff_view(view, obj1, obj2, repo);
1809 if (error)
1810 goto done;
1811 error = view_loop(view);
1812 done:
1813 got_repo_close(repo);
1814 if (obj1)
1815 got_object_close(obj1);
1816 if (obj2)
1817 got_object_close(obj2);
1818 return error;
1821 __dead static void
1822 usage_blame(void)
1824 endwin();
1825 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1826 getprogname());
1827 exit(1);
1830 struct tog_blame_line {
1831 int annotated;
1832 struct got_object_id *id;
1835 static const struct got_error *
1836 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1837 const char *path, struct tog_blame_line *lines, int nlines,
1838 int blame_complete, int selected_line, int *first_displayed_line,
1839 int *last_displayed_line, int *eof, int max_lines)
1841 const struct got_error *err;
1842 int lineno = 0, nprinted = 0;
1843 char *line;
1844 size_t len;
1845 wchar_t *wline;
1846 int width, wlimit;
1847 struct tog_blame_line *blame_line;
1848 struct got_object_id *prev_id = NULL;
1849 char *id_str;
1851 err = got_object_id_str(&id_str, id);
1852 if (err)
1853 return err;
1855 rewind(f);
1856 werase(view->window);
1858 if (asprintf(&line, "commit: %s", id_str) == -1) {
1859 err = got_error_from_errno();
1860 free(id_str);
1861 return err;
1864 err = format_line(&wline, &width, line, view->ncols);
1865 free(line);
1866 line = NULL;
1867 if (view_needs_focus_indication(view))
1868 wstandout(view->window);
1869 waddwstr(view->window, wline);
1870 if (view_needs_focus_indication(view))
1871 wstandend(view->window);
1872 free(wline);
1873 wline = NULL;
1874 if (width < view->ncols)
1875 waddch(view->window, '\n');
1877 if (asprintf(&line, "[%d/%d] %s%s",
1878 *first_displayed_line - 1 + selected_line, nlines,
1879 blame_complete ? "" : "annotating ", path) == -1) {
1880 free(id_str);
1881 return got_error_from_errno();
1883 free(id_str);
1884 err = format_line(&wline, &width, line, view->ncols);
1885 free(line);
1886 line = NULL;
1887 if (err)
1888 return err;
1889 waddwstr(view->window, wline);
1890 free(wline);
1891 wline = NULL;
1892 if (width < view->ncols)
1893 waddch(view->window, '\n');
1895 *eof = 0;
1896 while (nprinted < max_lines - 2) {
1897 line = parse_next_line(f, &len);
1898 if (line == NULL) {
1899 *eof = 1;
1900 break;
1902 if (++lineno < *first_displayed_line) {
1903 free(line);
1904 continue;
1907 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1908 err = format_line(&wline, &width, line, wlimit);
1909 if (err) {
1910 free(line);
1911 return err;
1914 if (nprinted == selected_line - 1)
1915 wstandout(view->window);
1917 blame_line = &lines[lineno - 1];
1918 if (blame_line->annotated && prev_id &&
1919 got_object_id_cmp(prev_id, blame_line->id) == 0)
1920 waddstr(view->window, " ");
1921 else if (blame_line->annotated) {
1922 char *id_str;
1923 err = got_object_id_str(&id_str, blame_line->id);
1924 if (err) {
1925 free(line);
1926 free(wline);
1927 return err;
1929 wprintw(view->window, "%.8s ", id_str);
1930 free(id_str);
1931 prev_id = blame_line->id;
1932 } else {
1933 waddstr(view->window, "........ ");
1934 prev_id = NULL;
1937 waddwstr(view->window, wline);
1938 while (width < wlimit) {
1939 waddch(view->window, ' ');
1940 width++;
1942 if (nprinted == selected_line - 1)
1943 wstandend(view->window);
1944 if (++nprinted == 1)
1945 *first_displayed_line = lineno;
1946 free(line);
1947 free(wline);
1948 wline = NULL;
1950 *last_displayed_line = lineno;
1952 view_vborder(view);
1954 return NULL;
1957 static const struct got_error *
1958 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1960 const struct got_error *err = NULL;
1961 struct tog_blame_cb_args *a = arg;
1962 struct tog_blame_line *line;
1964 if (nlines != a->nlines ||
1965 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1966 return got_error(GOT_ERR_RANGE);
1968 if (pthread_mutex_lock(a->mutex) != 0)
1969 return got_error_from_errno();
1971 if (*a->quit) { /* user has quit the blame view */
1972 err = got_error(GOT_ERR_ITER_COMPLETED);
1973 goto done;
1976 if (lineno == -1)
1977 goto done; /* no change in this commit */
1979 line = &a->lines[lineno - 1];
1980 if (line->annotated)
1981 goto done;
1983 line->id = got_object_id_dup(id);
1984 if (line->id == NULL) {
1985 err = got_error_from_errno();
1986 goto done;
1988 line->annotated = 1;
1990 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1991 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1992 a->last_displayed_line, a->eof, a->view->nlines);
1993 done:
1994 if (pthread_mutex_unlock(a->mutex) != 0)
1995 return got_error_from_errno();
1996 return err;
1999 static void *
2000 blame_thread(void *arg)
2002 const struct got_error *err;
2003 struct tog_blame_thread_args *ta = arg;
2004 struct tog_blame_cb_args *a = ta->cb_args;
2006 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2007 blame_cb, ta->cb_args);
2009 if (pthread_mutex_lock(a->mutex) != 0)
2010 return (void *)got_error_from_errno();
2012 got_repo_close(ta->repo);
2013 ta->repo = NULL;
2014 *ta->complete = 1;
2015 if (!err)
2016 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2017 a->lines, a->nlines, 1, *a->selected_line,
2018 a->first_displayed_line, a->last_displayed_line, a->eof,
2019 a->view->nlines);
2021 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2022 err = got_error_from_errno();
2024 return (void *)err;
2027 static struct got_object_id *
2028 get_selected_commit_id(struct tog_blame_line *lines,
2029 int first_displayed_line, int selected_line)
2031 struct tog_blame_line *line;
2033 line = &lines[first_displayed_line - 1 + selected_line - 1];
2034 if (!line->annotated)
2035 return NULL;
2037 return line->id;
2040 static const struct got_error *
2041 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2042 struct tog_blame_line *lines, int first_displayed_line,
2043 int selected_line, struct got_repository *repo)
2045 const struct got_error *err = NULL;
2046 struct got_commit_object *commit = NULL;
2047 struct got_object_id *selected_id;
2048 struct got_object_qid *pid;
2050 *pobj = NULL;
2051 *obj = NULL;
2053 selected_id = get_selected_commit_id(lines,
2054 first_displayed_line, selected_line);
2055 if (selected_id == NULL)
2056 return NULL;
2058 err = got_object_open(obj, repo, selected_id);
2059 if (err)
2060 goto done;
2062 err = got_object_commit_open(&commit, repo, *obj);
2063 if (err)
2064 goto done;
2066 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2067 if (pid) {
2068 err = got_object_open(pobj, repo, pid->id);
2069 if (err)
2070 goto done;
2072 done:
2073 if (commit)
2074 got_object_commit_close(commit);
2075 return err;
2078 static const struct got_error *
2079 stop_blame(struct tog_blame *blame)
2081 const struct got_error *err = NULL;
2082 int i;
2084 if (blame->thread) {
2085 if (pthread_join(blame->thread, (void **)&err) != 0)
2086 err = got_error_from_errno();
2087 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2088 err = NULL;
2089 blame->thread = NULL;
2091 if (blame->thread_args.repo) {
2092 got_repo_close(blame->thread_args.repo);
2093 blame->thread_args.repo = NULL;
2095 if (blame->f) {
2096 fclose(blame->f);
2097 blame->f = NULL;
2099 for (i = 0; i < blame->nlines; i++)
2100 free(blame->lines[i].id);
2101 free(blame->lines);
2102 blame->lines = NULL;
2103 free(blame->cb_args.commit_id);
2104 blame->cb_args.commit_id = NULL;
2106 return err;
2109 static const struct got_error *
2110 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2111 struct tog_view *view, int *blame_complete,
2112 int *first_displayed_line, int *last_displayed_line,
2113 int *selected_line, int *done, int *eof, const char *path,
2114 struct got_object_id *commit_id,
2115 struct got_repository *repo)
2117 const struct got_error *err = NULL;
2118 struct got_blob_object *blob = NULL;
2119 struct got_repository *thread_repo = NULL;
2120 struct got_object *obj;
2122 err = got_object_open_by_path(&obj, repo, commit_id, path);
2123 if (err)
2124 goto done;
2125 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2126 err = got_error(GOT_ERR_OBJ_TYPE);
2127 goto done;
2130 err = got_object_blob_open(&blob, repo, obj, 8192);
2131 if (err)
2132 goto done;
2133 blame->f = got_opentemp();
2134 if (blame->f == NULL) {
2135 err = got_error_from_errno();
2136 goto done;
2138 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2139 blame->f, blob);
2140 if (err)
2141 goto done;
2143 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2144 if (blame->lines == NULL) {
2145 err = got_error_from_errno();
2146 goto done;
2149 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2150 if (err)
2151 goto done;
2153 blame->cb_args.view = view;
2154 blame->cb_args.lines = blame->lines;
2155 blame->cb_args.nlines = blame->nlines;
2156 blame->cb_args.mutex = mutex;
2157 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2158 if (blame->cb_args.commit_id == NULL) {
2159 err = got_error_from_errno();
2160 goto done;
2162 blame->cb_args.f = blame->f;
2163 blame->cb_args.path = path;
2164 blame->cb_args.first_displayed_line = first_displayed_line;
2165 blame->cb_args.selected_line = selected_line;
2166 blame->cb_args.last_displayed_line = last_displayed_line;
2167 blame->cb_args.quit = done;
2168 blame->cb_args.eof = eof;
2170 blame->thread_args.path = path;
2171 blame->thread_args.repo = thread_repo;
2172 blame->thread_args.cb_args = &blame->cb_args;
2173 blame->thread_args.complete = blame_complete;
2174 *blame_complete = 0;
2176 if (pthread_create(&blame->thread, NULL, blame_thread,
2177 &blame->thread_args) != 0) {
2178 err = got_error_from_errno();
2179 goto done;
2182 done:
2183 if (blob)
2184 got_object_blob_close(blob);
2185 if (obj)
2186 got_object_close(obj);
2187 if (err)
2188 stop_blame(blame);
2189 return err;
2192 static const struct got_error *
2193 open_blame_view(struct tog_view *view, char *path,
2194 struct got_object_id *commit_id, struct got_repository *repo)
2196 const struct got_error *err = NULL;
2197 struct tog_blame_view_state *s = &view->state.blame;
2199 SIMPLEQ_INIT(&s->blamed_commits);
2201 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2202 return got_error_from_errno();
2204 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2205 if (err)
2206 return err;
2208 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2209 s->first_displayed_line = 1;
2210 s->last_displayed_line = view->nlines;
2211 s->selected_line = 1;
2212 s->blame_complete = 0;
2213 s->path = path;
2214 if (s->path == NULL)
2215 return got_error_from_errno();
2216 s->repo = repo;
2217 s->commit_id = commit_id;
2218 memset(&s->blame, 0, sizeof(s->blame));
2220 view->show = show_blame_view;
2221 view->input = input_blame_view;
2222 view->close = close_blame_view;
2224 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2225 &s->first_displayed_line, &s->last_displayed_line,
2226 &s->selected_line, &s->done, &s->eof, s->path,
2227 s->blamed_commit->id, s->repo);
2230 static const struct got_error *
2231 close_blame_view(struct tog_view *view)
2233 const struct got_error *err = NULL;
2234 struct tog_blame_view_state *s = &view->state.blame;
2236 if (s->blame.thread)
2237 err = stop_blame(&s->blame);
2239 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2240 struct got_object_qid *blamed_commit;
2241 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2242 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2243 got_object_qid_free(blamed_commit);
2246 free(s->path);
2248 return err;
2251 static const struct got_error *
2252 show_blame_view(struct tog_view *view)
2254 const struct got_error *err = NULL;
2255 struct tog_blame_view_state *s = &view->state.blame;
2257 if (pthread_mutex_lock(&s->mutex) != 0)
2258 return got_error_from_errno();
2260 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2261 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2262 s->selected_line, &s->first_displayed_line,
2263 &s->last_displayed_line, &s->eof, view->nlines);
2265 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2266 err = got_error_from_errno();
2268 return err;
2271 static const struct got_error *
2272 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2273 struct tog_view *view, int ch)
2275 const struct got_error *err = NULL, *thread_err = NULL;
2276 struct got_object *obj = NULL, *pobj = NULL;
2277 struct tog_view *diff_view;
2278 struct tog_blame_view_state *s = &view->state.blame;
2280 if (pthread_mutex_lock(&s->mutex) != 0) {
2281 err = got_error_from_errno();
2282 goto done;
2285 switch (ch) {
2286 case 'q':
2287 s->done = 1;
2288 if (pthread_mutex_unlock(&s->mutex) != 0) {
2289 err = got_error_from_errno();
2290 goto done;
2292 return stop_blame(&s->blame);
2293 case 'k':
2294 case KEY_UP:
2295 if (s->selected_line > 1)
2296 s->selected_line--;
2297 else if (s->selected_line == 1 &&
2298 s->first_displayed_line > 1)
2299 s->first_displayed_line--;
2300 break;
2301 case KEY_PPAGE:
2302 if (s->first_displayed_line == 1) {
2303 s->selected_line = 1;
2304 break;
2306 if (s->first_displayed_line > view->nlines - 2)
2307 s->first_displayed_line -=
2308 (view->nlines - 2);
2309 else
2310 s->first_displayed_line = 1;
2311 break;
2312 case 'j':
2313 case KEY_DOWN:
2314 if (s->selected_line < view->nlines - 2 &&
2315 s->first_displayed_line +
2316 s->selected_line <= s->blame.nlines)
2317 s->selected_line++;
2318 else if (s->last_displayed_line <
2319 s->blame.nlines)
2320 s->first_displayed_line++;
2321 break;
2322 case 'b':
2323 case 'p': {
2324 struct got_object_id *id;
2325 id = get_selected_commit_id(s->blame.lines,
2326 s->first_displayed_line, s->selected_line);
2327 if (id == NULL || got_object_id_cmp(id,
2328 s->blamed_commit->id) == 0)
2329 break;
2330 err = open_selected_commit(&pobj, &obj,
2331 s->blame.lines, s->first_displayed_line,
2332 s->selected_line, s->repo);
2333 if (err)
2334 break;
2335 if (pobj == NULL && obj == NULL)
2336 break;
2337 if (ch == 'p' && pobj == NULL)
2338 break;
2339 s->done = 1;
2340 if (pthread_mutex_unlock(&s->mutex) != 0) {
2341 err = got_error_from_errno();
2342 goto done;
2344 thread_err = stop_blame(&s->blame);
2345 s->done = 0;
2346 if (pthread_mutex_lock(&s->mutex) != 0) {
2347 err = got_error_from_errno();
2348 goto done;
2350 if (thread_err)
2351 break;
2352 id = got_object_get_id(ch == 'b' ? obj : pobj);
2353 got_object_close(obj);
2354 obj = NULL;
2355 if (pobj) {
2356 got_object_close(pobj);
2357 pobj = NULL;
2359 if (id == NULL) {
2360 err = got_error_from_errno();
2361 break;
2363 err = got_object_qid_alloc(
2364 &s->blamed_commit, id);
2365 free(id);
2366 if (err)
2367 goto done;
2368 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2369 s->blamed_commit, entry);
2370 err = run_blame(&s->blame, &s->mutex, view,
2371 &s->blame_complete,
2372 &s->first_displayed_line,
2373 &s->last_displayed_line,
2374 &s->selected_line, &s->done, &s->eof,
2375 s->path, s->blamed_commit->id, s->repo);
2376 if (err)
2377 break;
2378 break;
2380 case 'B': {
2381 struct got_object_qid *first;
2382 first = SIMPLEQ_FIRST(&s->blamed_commits);
2383 if (!got_object_id_cmp(first->id, s->commit_id))
2384 break;
2385 s->done = 1;
2386 if (pthread_mutex_unlock(&s->mutex) != 0) {
2387 err = got_error_from_errno();
2388 goto done;
2390 thread_err = stop_blame(&s->blame);
2391 s->done = 0;
2392 if (pthread_mutex_lock(&s->mutex) != 0) {
2393 err = got_error_from_errno();
2394 goto done;
2396 if (thread_err)
2397 break;
2398 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2399 got_object_qid_free(s->blamed_commit);
2400 s->blamed_commit =
2401 SIMPLEQ_FIRST(&s->blamed_commits);
2402 err = run_blame(&s->blame, &s->mutex, view,
2403 &s->blame_complete,
2404 &s->first_displayed_line,
2405 &s->last_displayed_line,
2406 &s->selected_line, &s->done, &s->eof, s->path,
2407 s->blamed_commit->id, s->repo);
2408 if (err)
2409 break;
2410 break;
2412 case KEY_ENTER:
2413 case '\r':
2414 err = open_selected_commit(&pobj, &obj,
2415 s->blame.lines, s->first_displayed_line,
2416 s->selected_line, s->repo);
2417 if (err)
2418 break;
2419 if (pobj == NULL && obj == NULL)
2420 break;
2421 diff_view = view_open(0, 0, 0, 0, view,
2422 TOG_VIEW_DIFF);
2423 if (diff_view == NULL) {
2424 err = got_error_from_errno();
2425 break;
2427 err = open_diff_view(diff_view, pobj, obj,
2428 s->repo);
2429 if (err) {
2430 view_close(diff_view);
2431 break;
2433 *new_view = diff_view;
2434 if (pobj) {
2435 got_object_close(pobj);
2436 pobj = NULL;
2438 got_object_close(obj);
2439 obj = NULL;
2440 if (err)
2441 break;
2442 break;
2443 case KEY_NPAGE:
2444 case ' ':
2445 if (s->last_displayed_line >= s->blame.nlines &&
2446 s->selected_line < view->nlines - 2) {
2447 s->selected_line = MIN(s->blame.nlines,
2448 view->nlines - 2);
2449 break;
2451 if (s->last_displayed_line + view->nlines - 2
2452 <= s->blame.nlines)
2453 s->first_displayed_line +=
2454 view->nlines - 2;
2455 else
2456 s->first_displayed_line =
2457 s->blame.nlines -
2458 (view->nlines - 3);
2459 break;
2460 case KEY_RESIZE:
2461 if (s->selected_line > view->nlines - 2) {
2462 s->selected_line = MIN(s->blame.nlines,
2463 view->nlines - 2);
2465 break;
2466 default:
2467 break;
2470 if (pthread_mutex_unlock(&s->mutex) != 0)
2471 err = got_error_from_errno();
2472 done:
2473 if (pobj)
2474 got_object_close(pobj);
2475 return thread_err ? thread_err : err;
2478 static const struct got_error *
2479 cmd_blame(int argc, char *argv[])
2481 const struct got_error *error;
2482 struct got_repository *repo = NULL;
2483 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2484 struct got_object_id *commit_id = NULL;
2485 char *commit_id_str = NULL;
2486 int ch;
2487 struct tog_view *view;
2489 #ifndef PROFILE
2490 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2491 == -1)
2492 err(1, "pledge");
2493 #endif
2495 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2496 switch (ch) {
2497 case 'c':
2498 commit_id_str = optarg;
2499 break;
2500 case 'r':
2501 repo_path = realpath(optarg, NULL);
2502 if (repo_path == NULL)
2503 err(1, "-r option");
2504 break;
2505 default:
2506 usage();
2507 /* NOTREACHED */
2511 argc -= optind;
2512 argv += optind;
2514 if (argc == 1)
2515 path = argv[0];
2516 else
2517 usage_blame();
2519 cwd = getcwd(NULL, 0);
2520 if (cwd == NULL) {
2521 error = got_error_from_errno();
2522 goto done;
2524 if (repo_path == NULL) {
2525 repo_path = strdup(cwd);
2526 if (repo_path == NULL) {
2527 error = got_error_from_errno();
2528 goto done;
2533 error = got_repo_open(&repo, repo_path);
2534 if (error != NULL)
2535 return error;
2537 error = got_repo_map_path(&in_repo_path, repo, path);
2538 if (error != NULL)
2539 goto done;
2541 if (commit_id_str == NULL) {
2542 struct got_reference *head_ref;
2543 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2544 if (error != NULL)
2545 goto done;
2546 error = got_ref_resolve(&commit_id, repo, head_ref);
2547 got_ref_close(head_ref);
2548 } else {
2549 struct got_object *obj;
2550 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2551 if (error != NULL)
2552 goto done;
2553 commit_id = got_object_get_id(obj);
2554 if (commit_id == NULL)
2555 error = got_error_from_errno();
2556 got_object_close(obj);
2558 if (error != NULL)
2559 goto done;
2561 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2562 if (view == NULL) {
2563 error = got_error_from_errno();
2564 goto done;
2566 error = open_blame_view(view, in_repo_path, commit_id, repo);
2567 if (error)
2568 goto done;
2569 error = view_loop(view);
2570 done:
2571 free(repo_path);
2572 free(cwd);
2573 free(commit_id);
2574 if (repo)
2575 got_repo_close(repo);
2576 return error;
2579 static const struct got_error *
2580 draw_tree_entries(struct tog_view *view,
2581 struct got_tree_entry **first_displayed_entry,
2582 struct got_tree_entry **last_displayed_entry,
2583 struct got_tree_entry **selected_entry, int *ndisplayed,
2584 const char *label, int show_ids, const char *parent_path,
2585 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2587 const struct got_error *err = NULL;
2588 struct got_tree_entry *te;
2589 wchar_t *wline;
2590 int width, n;
2592 *ndisplayed = 0;
2594 werase(view->window);
2596 if (limit == 0)
2597 return NULL;
2599 err = format_line(&wline, &width, label, view->ncols);
2600 if (err)
2601 return err;
2602 if (view_needs_focus_indication(view))
2603 wstandout(view->window);
2604 waddwstr(view->window, wline);
2605 if (view_needs_focus_indication(view))
2606 wstandend(view->window);
2607 free(wline);
2608 wline = NULL;
2609 if (width < view->ncols)
2610 waddch(view->window, '\n');
2611 if (--limit <= 0)
2612 return NULL;
2613 err = format_line(&wline, &width, parent_path, view->ncols);
2614 if (err)
2615 return err;
2616 waddwstr(view->window, wline);
2617 free(wline);
2618 wline = NULL;
2619 if (width < view->ncols)
2620 waddch(view->window, '\n');
2621 if (--limit <= 0)
2622 return NULL;
2623 waddch(view->window, '\n');
2624 if (--limit <= 0)
2625 return NULL;
2627 te = SIMPLEQ_FIRST(&entries->head);
2628 if (*first_displayed_entry == NULL) {
2629 if (selected == 0) {
2630 wstandout(view->window);
2631 *selected_entry = NULL;
2633 waddstr(view->window, " ..\n"); /* parent directory */
2634 if (selected == 0)
2635 wstandend(view->window);
2636 (*ndisplayed)++;
2637 if (--limit <= 0)
2638 return NULL;
2639 n = 1;
2640 } else {
2641 n = 0;
2642 while (te != *first_displayed_entry)
2643 te = SIMPLEQ_NEXT(te, entry);
2646 while (te) {
2647 char *line = NULL, *id_str = NULL;
2649 if (show_ids) {
2650 err = got_object_id_str(&id_str, te->id);
2651 if (err)
2652 return got_error_from_errno();
2654 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2655 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2656 free(id_str);
2657 return got_error_from_errno();
2659 free(id_str);
2660 err = format_line(&wline, &width, line, view->ncols);
2661 if (err) {
2662 free(line);
2663 break;
2665 if (n == selected) {
2666 wstandout(view->window);
2667 *selected_entry = te;
2669 waddwstr(view->window, wline);
2670 if (width < view->ncols)
2671 waddch(view->window, '\n');
2672 if (n == selected)
2673 wstandend(view->window);
2674 free(line);
2675 free(wline);
2676 wline = NULL;
2677 n++;
2678 (*ndisplayed)++;
2679 *last_displayed_entry = te;
2680 if (--limit <= 0)
2681 break;
2682 te = SIMPLEQ_NEXT(te, entry);
2685 view_vborder(view);
2686 return err;
2689 static void
2690 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2691 const struct got_tree_entries *entries, int isroot)
2693 struct got_tree_entry *te, *prev;
2694 int i;
2696 if (*first_displayed_entry == NULL)
2697 return;
2699 te = SIMPLEQ_FIRST(&entries->head);
2700 if (*first_displayed_entry == te) {
2701 if (!isroot)
2702 *first_displayed_entry = NULL;
2703 return;
2706 /* XXX this is stupid... switch to TAILQ? */
2707 for (i = 0; i < maxscroll; i++) {
2708 while (te != *first_displayed_entry) {
2709 prev = te;
2710 te = SIMPLEQ_NEXT(te, entry);
2712 *first_displayed_entry = prev;
2713 te = SIMPLEQ_FIRST(&entries->head);
2715 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2716 *first_displayed_entry = NULL;
2719 static void
2720 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2721 struct got_tree_entry *last_displayed_entry,
2722 const struct got_tree_entries *entries)
2724 struct got_tree_entry *next;
2725 int n = 0;
2727 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2728 return;
2730 if (*first_displayed_entry)
2731 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2732 else
2733 next = SIMPLEQ_FIRST(&entries->head);
2734 while (next) {
2735 *first_displayed_entry = next;
2736 if (++n >= maxscroll)
2737 break;
2738 next = SIMPLEQ_NEXT(next, entry);
2742 static const struct got_error *
2743 tree_entry_path(char **path, struct tog_parent_trees *parents,
2744 struct got_tree_entry *te)
2746 const struct got_error *err = NULL;
2747 struct tog_parent_tree *pt;
2748 size_t len = 2; /* for leading slash and NUL */
2750 TAILQ_FOREACH(pt, parents, entry)
2751 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2752 if (te)
2753 len += strlen(te->name);
2755 *path = calloc(1, len);
2756 if (path == NULL)
2757 return got_error_from_errno();
2759 (*path)[0] = '/';
2760 pt = TAILQ_LAST(parents, tog_parent_trees);
2761 while (pt) {
2762 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2763 err = got_error(GOT_ERR_NO_SPACE);
2764 goto done;
2766 if (strlcat(*path, "/", len) >= len) {
2767 err = got_error(GOT_ERR_NO_SPACE);
2768 goto done;
2770 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2772 if (te) {
2773 if (strlcat(*path, te->name, len) >= len) {
2774 err = got_error(GOT_ERR_NO_SPACE);
2775 goto done;
2778 done:
2779 if (err) {
2780 free(*path);
2781 *path = NULL;
2783 return err;
2786 static const struct got_error *
2787 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2788 struct got_tree_entry *te, struct tog_parent_trees *parents,
2789 struct got_object_id *commit_id, struct got_repository *repo)
2791 const struct got_error *err = NULL;
2792 char *path;
2793 struct tog_view *blame_view;
2795 err = tree_entry_path(&path, parents, te);
2796 if (err)
2797 return err;
2799 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2800 if (blame_view == NULL)
2801 return got_error_from_errno();
2803 err = open_blame_view(blame_view, path, commit_id, repo);
2804 if (err) {
2805 view_close(blame_view);
2806 free(path);
2807 } else
2808 *new_view = blame_view;
2809 return err;
2812 static const struct got_error *
2813 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2814 struct got_tree_entry *te, struct tog_parent_trees *parents,
2815 struct got_object_id *commit_id, struct got_repository *repo)
2817 struct tog_view *log_view;
2818 const struct got_error *err = NULL;
2819 char *path;
2821 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2822 if (log_view == NULL)
2823 return got_error_from_errno();
2825 err = tree_entry_path(&path, parents, te);
2826 if (err)
2827 return err;
2829 err = open_log_view(log_view, commit_id, repo, path);
2830 if (err)
2831 view_close(log_view);
2832 else
2833 *new_view = log_view;
2834 free(path);
2835 return err;
2838 static const struct got_error *
2839 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2840 struct got_object_id *commit_id, struct got_repository *repo)
2842 const struct got_error *err = NULL;
2843 char *commit_id_str = NULL;
2844 struct tog_tree_view_state *s = &view->state.tree;
2846 TAILQ_INIT(&s->parents);
2848 err = got_object_id_str(&commit_id_str, commit_id);
2849 if (err != NULL)
2850 goto done;
2852 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2853 err = got_error_from_errno();
2854 goto done;
2857 s->root = s->tree = root;
2858 s->entries = got_object_tree_get_entries(root);
2859 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2860 s->commit_id = commit_id;
2861 s->repo = repo;
2863 view->show = show_tree_view;
2864 view->input = input_tree_view;
2865 view->close = close_tree_view;
2866 done:
2867 free(commit_id_str);
2868 if (err)
2869 free(s->tree_label);
2870 return err;
2873 static const struct got_error *
2874 close_tree_view(struct tog_view *view)
2876 struct tog_tree_view_state *s = &view->state.tree;
2878 free(s->tree_label);
2879 while (!TAILQ_EMPTY(&s->parents)) {
2880 struct tog_parent_tree *parent;
2881 parent = TAILQ_FIRST(&s->parents);
2882 TAILQ_REMOVE(&s->parents, parent, entry);
2883 free(parent);
2886 if (s->tree != s->root)
2887 got_object_tree_close(s->tree);
2888 got_object_tree_close(s->root);
2890 return NULL;
2893 static const struct got_error *
2894 show_tree_view(struct tog_view *view)
2896 const struct got_error *err = NULL;
2897 struct tog_tree_view_state *s = &view->state.tree;
2898 char *parent_path;
2900 err = tree_entry_path(&parent_path, &s->parents, NULL);
2901 if (err)
2902 return err;
2904 err = draw_tree_entries(view, &s->first_displayed_entry,
2905 &s->last_displayed_entry, &s->selected_entry,
2906 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2907 s->entries, s->selected, view->nlines, s->tree == s->root);
2908 free(parent_path);
2909 return err;
2912 static const struct got_error *
2913 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2914 struct tog_view *view, int ch)
2916 const struct got_error *err = NULL;
2917 struct tog_tree_view_state *s = &view->state.tree;
2919 switch (ch) {
2920 case 'i':
2921 s->show_ids = !s->show_ids;
2922 break;
2923 case 'l':
2924 if (s->selected_entry) {
2925 err = log_tree_entry(new_view, view,
2926 s->selected_entry, &s->parents,
2927 s->commit_id, s->repo);
2929 break;
2930 case 'k':
2931 case KEY_UP:
2932 if (s->selected > 0)
2933 s->selected--;
2934 if (s->selected > 0)
2935 break;
2936 tree_scroll_up(&s->first_displayed_entry, 1,
2937 s->entries, s->tree == s->root);
2938 break;
2939 case KEY_PPAGE:
2940 if (SIMPLEQ_FIRST(&s->entries->head) ==
2941 s->first_displayed_entry) {
2942 if (s->tree != s->root)
2943 s->first_displayed_entry = NULL;
2944 s->selected = 0;
2945 break;
2947 tree_scroll_up(&s->first_displayed_entry,
2948 view->nlines, s->entries,
2949 s->tree == s->root);
2950 break;
2951 case 'j':
2952 case KEY_DOWN:
2953 if (s->selected < s->ndisplayed - 1) {
2954 s->selected++;
2955 break;
2957 tree_scroll_down(&s->first_displayed_entry, 1,
2958 s->last_displayed_entry, s->entries);
2959 break;
2960 case KEY_NPAGE:
2961 tree_scroll_down(&s->first_displayed_entry,
2962 view->nlines, s->last_displayed_entry,
2963 s->entries);
2964 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2965 entry))
2966 break;
2967 /* can't scroll any further; move cursor down */
2968 if (s->selected < s->ndisplayed - 1)
2969 s->selected = s->ndisplayed - 1;
2970 break;
2971 case KEY_ENTER:
2972 case '\r':
2973 if (s->selected_entry == NULL) {
2974 struct tog_parent_tree *parent;
2975 case KEY_LEFT:
2976 /* user selected '..' */
2977 if (s->tree == s->root)
2978 break;
2979 parent = TAILQ_FIRST(&s->parents);
2980 TAILQ_REMOVE(&s->parents, parent,
2981 entry);
2982 got_object_tree_close(s->tree);
2983 s->tree = parent->tree;
2984 s->entries =
2985 got_object_tree_get_entries(s->tree);
2986 s->first_displayed_entry =
2987 parent->first_displayed_entry;
2988 s->selected_entry =
2989 parent->selected_entry;
2990 s->selected = parent->selected;
2991 free(parent);
2992 } else if (S_ISDIR(s->selected_entry->mode)) {
2993 struct tog_parent_tree *parent;
2994 struct got_tree_object *child;
2995 err = got_object_open_as_tree(&child,
2996 s->repo, s->selected_entry->id);
2997 if (err)
2998 break;
2999 parent = calloc(1, sizeof(*parent));
3000 if (parent == NULL) {
3001 err = got_error_from_errno();
3002 break;
3004 parent->tree = s->tree;
3005 parent->first_displayed_entry =
3006 s->first_displayed_entry;
3007 parent->selected_entry = s->selected_entry;
3008 parent->selected = s->selected;
3009 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3010 s->tree = child;
3011 s->entries =
3012 got_object_tree_get_entries(s->tree);
3013 s->selected = 0;
3014 s->first_displayed_entry = NULL;
3015 } else if (S_ISREG(s->selected_entry->mode)) {
3016 err = blame_tree_entry(new_view, view,
3017 s->selected_entry, &s->parents,
3018 s->commit_id, s->repo);
3019 if (err)
3020 break;
3022 break;
3023 case KEY_RESIZE:
3024 if (s->selected > view->nlines)
3025 s->selected = s->ndisplayed - 1;
3026 break;
3027 default:
3028 break;
3031 return err;
3034 __dead static void
3035 usage_tree(void)
3037 endwin();
3038 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3039 getprogname());
3040 exit(1);
3043 static const struct got_error *
3044 cmd_tree(int argc, char *argv[])
3046 const struct got_error *error;
3047 struct got_repository *repo = NULL;
3048 char *repo_path = NULL;
3049 struct got_object_id *commit_id = NULL;
3050 char *commit_id_arg = NULL;
3051 struct got_commit_object *commit = NULL;
3052 struct got_tree_object *tree = NULL;
3053 int ch;
3054 struct tog_view *view;
3056 #ifndef PROFILE
3057 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3058 == -1)
3059 err(1, "pledge");
3060 #endif
3062 while ((ch = getopt(argc, argv, "c:")) != -1) {
3063 switch (ch) {
3064 case 'c':
3065 commit_id_arg = optarg;
3066 break;
3067 default:
3068 usage();
3069 /* NOTREACHED */
3073 argc -= optind;
3074 argv += optind;
3076 if (argc == 0) {
3077 repo_path = getcwd(NULL, 0);
3078 if (repo_path == NULL)
3079 return got_error_from_errno();
3080 } else if (argc == 1) {
3081 repo_path = realpath(argv[0], NULL);
3082 if (repo_path == NULL)
3083 return got_error_from_errno();
3084 } else
3085 usage_log();
3087 error = got_repo_open(&repo, repo_path);
3088 free(repo_path);
3089 if (error != NULL)
3090 return error;
3092 if (commit_id_arg == NULL) {
3093 error = get_head_commit_id(&commit_id, repo);
3094 if (error != NULL)
3095 goto done;
3096 } else {
3097 struct got_object *obj;
3098 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3099 if (error == NULL) {
3100 commit_id = got_object_get_id(obj);
3101 if (commit_id == NULL)
3102 error = got_error_from_errno();
3105 if (error != NULL)
3106 goto done;
3108 error = got_object_open_as_commit(&commit, repo, commit_id);
3109 if (error != NULL)
3110 goto done;
3112 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3113 if (error != NULL)
3114 goto done;
3116 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3117 if (view == NULL) {
3118 error = got_error_from_errno();
3119 goto done;
3121 error = open_tree_view(view, tree, commit_id, repo);
3122 if (error)
3123 goto done;
3124 error = view_loop(view);
3125 done:
3126 free(commit_id);
3127 if (commit)
3128 got_object_commit_close(commit);
3129 if (tree)
3130 got_object_tree_close(tree);
3131 if (repo)
3132 got_repo_close(repo);
3133 return error;
3135 static void
3136 init_curses(void)
3138 initscr();
3139 cbreak();
3140 noecho();
3141 nonl();
3142 intrflush(stdscr, FALSE);
3143 keypad(stdscr, TRUE);
3144 curs_set(0);
3147 __dead static void
3148 usage(void)
3150 int i;
3152 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3153 "Available commands:\n", getprogname());
3154 for (i = 0; i < nitems(tog_commands); i++) {
3155 struct tog_cmd *cmd = &tog_commands[i];
3156 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3158 exit(1);
3161 static char **
3162 make_argv(const char *arg0, const char *arg1)
3164 char **argv;
3165 int argc = (arg1 == NULL ? 1 : 2);
3167 argv = calloc(argc, sizeof(char *));
3168 if (argv == NULL)
3169 err(1, "calloc");
3170 argv[0] = strdup(arg0);
3171 if (argv[0] == NULL)
3172 err(1, "calloc");
3173 if (arg1) {
3174 argv[1] = strdup(arg1);
3175 if (argv[1] == NULL)
3176 err(1, "calloc");
3179 return argv;
3182 int
3183 main(int argc, char *argv[])
3185 const struct got_error *error = NULL;
3186 struct tog_cmd *cmd = NULL;
3187 int ch, hflag = 0;
3188 char **cmd_argv = NULL;
3190 setlocale(LC_ALL, "");
3192 while ((ch = getopt(argc, argv, "h")) != -1) {
3193 switch (ch) {
3194 case 'h':
3195 hflag = 1;
3196 break;
3197 default:
3198 usage();
3199 /* NOTREACHED */
3203 argc -= optind;
3204 argv += optind;
3205 optind = 0;
3206 optreset = 1;
3208 if (argc == 0) {
3209 if (hflag)
3210 usage();
3211 /* Build an argument vector which runs a default command. */
3212 cmd = &tog_commands[0];
3213 cmd_argv = make_argv(cmd->name, NULL);
3214 argc = 1;
3215 } else {
3216 int i;
3218 /* Did the user specific a command? */
3219 for (i = 0; i < nitems(tog_commands); i++) {
3220 if (strncmp(tog_commands[i].name, argv[0],
3221 strlen(argv[0])) == 0) {
3222 cmd = &tog_commands[i];
3223 if (hflag)
3224 tog_commands[i].cmd_usage();
3225 break;
3228 if (cmd == NULL) {
3229 /* Did the user specify a repository? */
3230 char *repo_path = realpath(argv[0], NULL);
3231 if (repo_path) {
3232 struct got_repository *repo;
3233 error = got_repo_open(&repo, repo_path);
3234 if (error == NULL)
3235 got_repo_close(repo);
3236 } else
3237 error = got_error_from_errno();
3238 if (error) {
3239 if (hflag) {
3240 fprintf(stderr, "%s: '%s' is not a "
3241 "known command\n", getprogname(),
3242 argv[0]);
3243 usage();
3245 fprintf(stderr, "%s: '%s' is neither a known "
3246 "command nor a path to a repository\n",
3247 getprogname(), argv[0]);
3248 free(repo_path);
3249 return 1;
3251 cmd = &tog_commands[0];
3252 cmd_argv = make_argv(cmd->name, repo_path);
3253 argc = 2;
3254 free(repo_path);
3258 init_curses();
3260 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3261 if (error)
3262 goto done;
3263 done:
3264 endwin();
3265 free(cmd_argv);
3266 if (error)
3267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3268 return 0;