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 *id;
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 struct tog_view *parent;
204 struct tog_view *child;
206 /* type-specific state */
207 enum tog_view_type type;
208 union {
209 struct tog_diff_view_state diff;
210 struct tog_log_view_state log;
211 struct tog_blame_view_state blame;
212 struct tog_tree_view_state tree;
213 } state;
215 const struct got_error *(*show)(struct tog_view *);
216 const struct got_error *(*input)(struct tog_view **,
217 struct tog_view **, struct tog_view *, int);
218 const struct got_error *(*set_child)(struct tog_view *,
219 struct tog_view *);
220 const struct got_error *(*close)(struct tog_view *);
221 };
223 static const struct got_error *open_diff_view(struct tog_view *,
224 struct got_object *, struct got_object *, struct got_repository *);
225 static const struct got_error *show_diff_view(struct tog_view *);
226 static const struct got_error *input_diff_view(struct tog_view **,
227 struct tog_view **, struct tog_view *, int);
228 static const struct got_error* close_diff_view(struct tog_view *);
230 static const struct got_error *open_log_view(struct tog_view *,
231 struct got_object_id *, struct got_repository *, const char *);
232 static const struct got_error * show_log_view(struct tog_view *);
233 static const struct got_error *input_log_view(struct tog_view **,
234 struct tog_view **, struct tog_view *, int);
235 static const struct got_error *close_log_view(struct tog_view *);
236 static const struct got_error* set_child_log_view(struct tog_view *,
237 struct tog_view *);
239 static const struct got_error *open_blame_view(struct tog_view *, char *,
240 struct got_object_id *, struct got_repository *);
241 static const struct got_error *show_blame_view(struct tog_view *);
242 static const struct got_error *input_blame_view(struct tog_view **,
243 struct tog_view **, struct tog_view *, int);
244 static const struct got_error *close_blame_view(struct tog_view *);
246 static const struct got_error *open_tree_view(struct tog_view *,
247 struct got_tree_object *, struct got_object_id *, struct got_repository *);
248 static const struct got_error *show_tree_view(struct tog_view *);
249 static const struct got_error *input_tree_view(struct tog_view **,
250 struct tog_view **, struct tog_view *, int);
251 static const struct got_error *close_tree_view(struct tog_view *);
253 static const struct got_error *
254 view_close(struct tog_view *view)
256 const struct got_error *err = NULL;
258 if (view->child)
259 view->child->parent = NULL;
260 if (view->parent)
261 view->parent->child = NULL;
262 if (view->close)
263 err = view->close(view);
264 if (view->panel)
265 del_panel(view->panel);
266 if (view->window)
267 delwin(view->window);
268 free(view);
269 return err;
272 static struct tog_view *
273 view_open(int nlines, int ncols, int begin_y, int begin_x,
274 struct tog_view *parent, enum tog_view_type type)
276 struct tog_view *view = calloc(1, sizeof(*view));
278 if (view == NULL)
279 return NULL;
281 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
282 begin_x = parent->ncols - 80;
284 view->parent = parent;
285 if (parent)
286 parent->child = view;
287 view->type = type;
288 view->lines = LINES;
289 view->cols = COLS;
290 view->nlines = nlines ? nlines : LINES - begin_y;
291 view->ncols = ncols ? ncols : COLS - begin_x;
292 view->begin_y = begin_y;
293 view->begin_x = begin_x;
294 view->window = newwin(nlines, ncols, begin_y, begin_x);
295 if (view->window == NULL) {
296 view_close(view);
297 return NULL;
299 view->panel = new_panel(view->window);
300 if (view->panel == NULL) {
301 view_close(view);
302 return NULL;
305 keypad(view->window, TRUE);
306 return view;
309 static const struct got_error *
310 view_show(struct tog_view *view)
312 const struct got_error *err;
314 if (view->parent) {
315 err = view->parent->show(view->parent);
316 if (err)
317 return err;
318 show_panel(view->parent->panel);
321 err = view->show(view);
322 if (err)
323 return err;
324 show_panel(view->panel);
326 if (view->child) {
327 err = view->child->show(view->child);
328 if (err)
329 return err;
330 show_panel(view->child->panel);
333 update_panels();
334 doupdate();
336 return err;
339 static const struct got_error *
340 view_resize(struct tog_view *view)
342 int nlines, ncols;
344 while (view) {
345 if (view->lines > LINES)
346 nlines = view->nlines - (view->lines - LINES);
347 else
348 nlines = view->nlines + (LINES - view->lines);
350 if (view->cols > COLS)
351 ncols = view->ncols - (view->cols - COLS);
352 else
353 ncols = view->ncols + (COLS - view->cols);
355 if (wresize(view->window, nlines, ncols) == ERR)
356 return got_error_from_errno();
357 replace_panel(view->panel, view->window);
359 view->nlines = nlines;
360 view->ncols = ncols;
361 view->lines = LINES;
362 view->cols = COLS;
364 view = view->parent;
367 return NULL;
370 static const struct got_error *
371 view_input(struct tog_view **new, struct tog_view **dead,
372 struct tog_view **focus, int *done, struct tog_view *view,
373 struct tog_view_list_head *views)
375 const struct got_error *err = NULL;
376 struct tog_view *next, *prev;
377 int ch;
379 *new = NULL;
380 *dead = NULL;
382 nodelay(stdscr, FALSE);
383 ch = wgetch(view->window);
384 nodelay(stdscr, TRUE);
385 switch (ch) {
386 case ERR:
387 break;
388 case '\t':
389 next = TAILQ_NEXT(view, entry);
390 if (next)
391 *focus = next;
392 else
393 *focus = TAILQ_FIRST(views);
394 break;
395 case KEY_BACKSPACE:
396 prev = TAILQ_PREV(view, tog_view_list_head, entry);
397 if (prev)
398 *focus = prev;
399 else
400 *focus = TAILQ_LAST(views, tog_view_list_head);
401 break;
402 case 'q':
403 err = view->input(new, dead, view, ch);
404 *dead = view;
405 break;
406 case 'Q':
407 *done = 1;
408 break;
409 case KEY_RESIZE:
410 err = view_resize(view);
411 if (err)
412 return err;
413 err = view->input(new, dead, view, ch);
414 break;
415 default:
416 err = view->input(new, dead, view, ch);
417 break;
420 return err;
423 static const struct got_error *
424 view_set_child(struct tog_view *view, struct tog_view *child)
426 const struct got_error *err;
428 if (view->set_child) {
429 err = view->set_child(view, child);
430 if (err)
431 return err;
434 view->child = child;
435 return NULL;
438 static const struct got_error *
439 view_loop(struct tog_view *view)
441 const struct got_error *err = NULL;
442 struct tog_view_list_head views;
443 struct tog_view *new_view, *dead_view;
444 int done = 0;
446 TAILQ_INIT(&views);
447 TAILQ_INSERT_HEAD(&views, view, entry);
449 while (!TAILQ_EMPTY(&views) && !done) {
450 err = view_show(view);
451 if (err)
452 break;
453 err = view_input(&new_view, &dead_view, &view, &done,
454 view, &views);
455 if (err)
456 break;
457 if (dead_view) {
458 TAILQ_REMOVE(&views, dead_view, entry);
459 if (dead_view->parent)
460 view = dead_view->parent;
461 else
462 view = TAILQ_LAST(&views, tog_view_list_head);
463 if (dead_view->child) {
464 TAILQ_REMOVE(&views, dead_view->child, entry);
465 err = view_close(dead_view->child);
466 if (err)
467 goto done;
469 err = view_close(dead_view);
470 if (err)
471 goto done;
473 if (new_view) {
474 /* TODO: de-duplicate! */
475 TAILQ_INSERT_TAIL(&views, new_view, entry);
476 if (new_view->parent) {
477 err = view_set_child(new_view->parent, new_view);
478 if (err)
479 goto done;
481 view = new_view;
484 done:
485 while (!TAILQ_EMPTY(&views)) {
486 view = TAILQ_FIRST(&views);
487 TAILQ_REMOVE(&views, view, entry);
488 view_close(view);
490 return err;
493 __dead static void
494 usage_log(void)
496 endwin();
497 fprintf(stderr,
498 "usage: %s log [-c commit] [-r repository-path] [path]\n",
499 getprogname());
500 exit(1);
503 /* Create newly allocated wide-character string equivalent to a byte string. */
504 static const struct got_error *
505 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
507 char *vis = NULL;
508 const struct got_error *err = NULL;
510 *ws = NULL;
511 *wlen = mbstowcs(NULL, s, 0);
512 if (*wlen == (size_t)-1) {
513 int vislen;
514 if (errno != EILSEQ)
515 return got_error_from_errno();
517 /* byte string invalid in current encoding; try to "fix" it */
518 err = got_mbsavis(&vis, &vislen, s);
519 if (err)
520 return err;
521 *wlen = mbstowcs(NULL, vis, 0);
522 if (*wlen == (size_t)-1) {
523 err = got_error_from_errno(); /* give up */
524 goto done;
528 *ws = calloc(*wlen + 1, sizeof(*ws));
529 if (*ws == NULL) {
530 err = got_error_from_errno();
531 goto done;
534 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
535 err = got_error_from_errno();
536 done:
537 free(vis);
538 if (err) {
539 free(*ws);
540 *ws = NULL;
541 *wlen = 0;
543 return err;
546 /* Format a line for display, ensuring that it won't overflow a width limit. */
547 static const struct got_error *
548 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
550 const struct got_error *err = NULL;
551 int cols = 0;
552 wchar_t *wline = NULL;
553 size_t wlen;
554 int i;
556 *wlinep = NULL;
557 *widthp = 0;
559 err = mbs2ws(&wline, &wlen, line);
560 if (err)
561 return err;
563 i = 0;
564 while (i < wlen && cols < wlimit) {
565 int width = wcwidth(wline[i]);
566 switch (width) {
567 case 0:
568 i++;
569 break;
570 case 1:
571 case 2:
572 if (cols + width <= wlimit) {
573 cols += width;
574 i++;
576 break;
577 case -1:
578 if (wline[i] == L'\t')
579 cols += TABSIZE - ((cols + 1) % TABSIZE);
580 i++;
581 break;
582 default:
583 err = got_error_from_errno();
584 goto done;
587 wline[i] = L'\0';
588 if (widthp)
589 *widthp = cols;
590 done:
591 if (err)
592 free(wline);
593 else
594 *wlinep = wline;
595 return err;
598 static const struct got_error *
599 draw_commit(struct tog_view *view, struct got_commit_object *commit,
600 struct got_object_id *id)
602 const struct got_error *err = NULL;
603 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
604 char *logmsg0 = NULL, *logmsg = NULL;
605 char *author0 = NULL, *author = NULL;
606 wchar_t *wlogmsg = NULL, *wauthor = NULL;
607 int author_width, logmsg_width;
608 char *newline, *smallerthan;
609 char *line = NULL;
610 int col, limit;
611 static const size_t date_display_cols = 9;
612 static const size_t author_display_cols = 16;
613 const int avail = view->ncols;
615 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
616 &commit->tm_committer) >= sizeof(datebuf))
617 return got_error(GOT_ERR_NO_SPACE);
619 if (avail < date_display_cols)
620 limit = MIN(sizeof(datebuf) - 1, avail);
621 else
622 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
623 waddnstr(view->window, datebuf, limit);
624 col = limit + 1;
625 if (col > avail)
626 goto done;
628 author0 = strdup(commit->author);
629 if (author0 == NULL) {
630 err = got_error_from_errno();
631 goto done;
633 author = author0;
634 smallerthan = strchr(author, '<');
635 if (smallerthan)
636 *smallerthan = '\0';
637 else {
638 char *at = strchr(author, '@');
639 if (at)
640 *at = '\0';
642 limit = avail - col;
643 err = format_line(&wauthor, &author_width, author, limit);
644 if (err)
645 goto done;
646 waddwstr(view->window, wauthor);
647 col += author_width;
648 while (col <= avail && author_width < author_display_cols + 1) {
649 waddch(view->window, ' ');
650 col++;
651 author_width++;
653 if (col > avail)
654 goto done;
656 logmsg0 = strdup(commit->logmsg);
657 if (logmsg0 == NULL) {
658 err = got_error_from_errno();
659 goto done;
661 logmsg = logmsg0;
662 while (*logmsg == '\n')
663 logmsg++;
664 newline = strchr(logmsg, '\n');
665 if (newline)
666 *newline = '\0';
667 limit = avail - col;
668 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
669 if (err)
670 goto done;
671 waddwstr(view->window, wlogmsg);
672 col += logmsg_width;
673 while (col <= avail) {
674 waddch(view->window, ' ');
675 col++;
677 done:
678 free(logmsg0);
679 free(wlogmsg);
680 free(author0);
681 free(wauthor);
682 free(line);
683 return err;
686 static struct commit_queue_entry *
687 alloc_commit_queue_entry(struct got_commit_object *commit,
688 struct got_object_id *id)
690 struct commit_queue_entry *entry;
692 entry = calloc(1, sizeof(*entry));
693 if (entry == NULL)
694 return NULL;
696 entry->id = id;
697 entry->commit = commit;
698 return entry;
701 static void
702 pop_commit(struct commit_queue *commits)
704 struct commit_queue_entry *entry;
706 entry = TAILQ_FIRST(&commits->head);
707 TAILQ_REMOVE(&commits->head, entry, entry);
708 got_object_commit_close(entry->commit);
709 commits->ncommits--;
710 /* Don't free entry->id! It is owned by the commit graph. */
711 free(entry);
714 static void
715 free_commits(struct commit_queue *commits)
717 while (!TAILQ_EMPTY(&commits->head))
718 pop_commit(commits);
721 static const struct got_error *
722 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
723 struct got_object_id *start_id, int minqueue, int init,
724 struct got_repository *repo, const char *path)
726 const struct got_error *err = NULL;
727 struct got_object_id *id;
728 struct commit_queue_entry *entry;
729 int nfetched, nqueued = 0, found_obj = 0;
730 int is_root_path = strcmp(path, "/") == 0;
732 err = got_commit_graph_iter_start(graph, start_id);
733 if (err)
734 return err;
736 entry = TAILQ_LAST(&commits->head, commit_queue_head);
737 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
738 int nfetched;
740 /* Start ID's commit is already on the queue; skip over it. */
741 err = got_commit_graph_iter_next(&id, graph);
742 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
743 return err;
745 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
746 if (err)
747 return err;
750 while (1) {
751 struct got_commit_object *commit;
753 err = got_commit_graph_iter_next(&id, graph);
754 if (err) {
755 if (err->code != GOT_ERR_ITER_NEED_MORE)
756 break;
757 if (nqueued >= minqueue) {
758 err = NULL;
759 break;
761 err = got_commit_graph_fetch_commits(&nfetched,
762 graph, 1, repo);
763 if (err)
764 return err;
765 continue;
767 if (id == NULL)
768 break;
770 err = got_object_open_as_commit(&commit, repo, id);
771 if (err)
772 break;
774 if (!is_root_path) {
775 struct got_object *obj;
776 struct got_object_qid *pid;
777 int changed = 0;
779 err = got_object_open_by_path(&obj, repo, id, path);
780 if (err) {
781 got_object_commit_close(commit);
782 if (err->code == GOT_ERR_NO_OBJ &&
783 (found_obj || !init)) {
784 /* History stops here. */
785 err = got_error(GOT_ERR_ITER_COMPLETED);
787 break;
789 found_obj = 1;
791 pid = SIMPLEQ_FIRST(&commit->parent_ids);
792 if (pid != NULL) {
793 struct got_object *pobj;
794 err = got_object_open_by_path(&pobj, repo,
795 pid->id, path);
796 if (err) {
797 if (err->code != GOT_ERR_NO_OBJ) {
798 got_object_close(obj);
799 got_object_commit_close(commit);
800 break;
802 err = NULL;
803 changed = 1;
804 } else {
805 struct got_object_id *id, *pid;
806 id = got_object_get_id(obj);
807 if (id == NULL) {
808 err = got_error_from_errno();
809 got_object_close(obj);
810 got_object_close(pobj);
811 break;
813 pid = got_object_get_id(pobj);
814 if (pid == NULL) {
815 err = got_error_from_errno();
816 free(id);
817 got_object_close(obj);
818 got_object_close(pobj);
819 break;
821 changed =
822 (got_object_id_cmp(id, pid) != 0);
823 got_object_close(pobj);
824 free(id);
825 free(pid);
828 got_object_close(obj);
829 if (!changed) {
830 got_object_commit_close(commit);
831 continue;
835 entry = alloc_commit_queue_entry(commit, id);
836 if (entry == NULL) {
837 err = got_error_from_errno();
838 break;
840 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
841 nqueued++;
842 commits->ncommits++;
845 return err;
848 static const struct got_error *
849 fetch_next_commit(struct commit_queue_entry **pentry,
850 struct commit_queue_entry *entry, struct commit_queue *commits,
851 struct got_commit_graph *graph, struct got_repository *repo,
852 const char *path)
854 const struct got_error *err = NULL;
856 *pentry = NULL;
858 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
859 if (err)
860 return err;
862 /* Next entry to display should now be available. */
863 *pentry = TAILQ_NEXT(entry, entry);
864 if (*pentry == NULL)
865 return got_error(GOT_ERR_NO_OBJ);
867 return NULL;
870 static const struct got_error *
871 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
873 const struct got_error *err = NULL;
874 struct got_reference *head_ref;
876 *head_id = NULL;
878 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
879 if (err)
880 return err;
882 err = got_ref_resolve(head_id, repo, head_ref);
883 got_ref_close(head_ref);
884 if (err) {
885 *head_id = NULL;
886 return err;
889 return NULL;
892 static const struct got_error *
893 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
894 struct commit_queue_entry **selected, struct commit_queue_entry *first,
895 struct commit_queue *commits, int selected_idx, int limit,
896 struct got_commit_graph *graph, struct got_repository *repo,
897 const char *path)
899 const struct got_error *err = NULL;
900 struct commit_queue_entry *entry;
901 int ncommits, width;
902 char *id_str, *header;
903 wchar_t *wline;
905 entry = first;
906 ncommits = 0;
907 while (entry) {
908 if (ncommits == selected_idx) {
909 *selected = entry;
910 break;
912 entry = TAILQ_NEXT(entry, entry);
913 ncommits++;
916 err = got_object_id_str(&id_str, (*selected)->id);
917 if (err)
918 return err;
920 if (path && strcmp(path, "/") != 0) {
921 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
922 err = got_error_from_errno();
923 free(id_str);
924 return err;
926 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
927 err = got_error_from_errno();
928 free(id_str);
929 return err;
931 free(id_str);
932 err = format_line(&wline, &width, header, view->ncols);
933 if (err) {
934 free(header);
935 return err;
937 free(header);
939 werase(view->window);
941 waddwstr(view->window, wline);
942 if (width < view->ncols)
943 waddch(view->window, '\n');
944 free(wline);
945 if (limit <= 1)
946 return NULL;
948 entry = first;
949 *last = first;
950 ncommits = 0;
951 while (entry) {
952 if (ncommits >= limit - 1)
953 break;
954 if (ncommits == selected_idx)
955 wstandout(view->window);
956 err = draw_commit(view, entry->commit, entry->id);
957 if (ncommits == selected_idx)
958 wstandend(view->window);
959 if (err)
960 break;
961 ncommits++;
962 *last = entry;
963 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
964 err = queue_commits(graph, commits, entry->id, 1,
965 0, repo, path);
966 if (err) {
967 if (err->code != GOT_ERR_ITER_COMPLETED)
968 return err;
969 err = NULL;
972 entry = TAILQ_NEXT(entry, entry);
975 update_panels();
977 return err;
980 static void
981 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
982 struct commit_queue *commits)
984 struct commit_queue_entry *entry;
985 int nscrolled = 0;
987 entry = TAILQ_FIRST(&commits->head);
988 if (*first_displayed_entry == entry)
989 return;
991 entry = *first_displayed_entry;
992 while (entry && nscrolled < maxscroll) {
993 entry = TAILQ_PREV(entry, commit_queue_head, entry);
994 if (entry) {
995 *first_displayed_entry = entry;
996 nscrolled++;
1001 static const struct got_error *
1002 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1003 struct commit_queue_entry *last_displayed_entry,
1004 struct commit_queue *commits, struct got_commit_graph *graph,
1005 struct got_repository *repo, const char *path)
1007 const struct got_error *err = NULL;
1008 struct commit_queue_entry *pentry;
1009 int nscrolled = 0;
1011 do {
1012 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1013 if (pentry == NULL) {
1014 err = fetch_next_commit(&pentry, last_displayed_entry,
1015 commits, graph, repo, path);
1016 if (err || pentry == NULL)
1017 break;
1019 last_displayed_entry = pentry;
1021 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1022 if (pentry == NULL)
1023 break;
1024 *first_displayed_entry = pentry;
1025 } while (++nscrolled < maxscroll);
1027 return err;
1030 static const struct got_error *
1031 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1032 struct commit_queue_entry *entry, struct got_repository *repo)
1034 const struct got_error *err;
1035 struct got_object *obj1 = NULL, *obj2 = NULL;
1036 struct got_object_qid *parent_id;
1037 struct tog_view *diff_view;
1039 err = got_object_open(&obj2, repo, entry->id);
1040 if (err)
1041 return err;
1043 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1044 if (parent_id) {
1045 err = got_object_open(&obj1, repo, parent_id->id);
1046 if (err)
1047 goto done;
1050 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1051 if (diff_view == NULL) {
1052 err = got_error_from_errno();
1053 goto done;
1056 err = open_diff_view(diff_view, obj1, obj2, repo);
1057 if (err == NULL)
1058 *new_view = diff_view;
1059 done:
1060 if (obj1)
1061 got_object_close(obj1);
1062 if (obj2)
1063 got_object_close(obj2);
1064 return err;
1067 static const struct got_error *
1068 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1069 struct commit_queue_entry *entry, struct got_repository *repo)
1071 const struct got_error *err = NULL;
1072 struct got_tree_object *tree;
1073 struct tog_view *tree_view;
1075 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1076 if (err)
1077 return err;
1079 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1080 if (tree_view == NULL)
1081 return got_error_from_errno();
1083 err = open_tree_view(tree_view, tree, entry->id, repo);
1084 if (err)
1085 got_object_tree_close(tree);
1086 else
1087 *new_view = tree_view;
1088 return err;
1091 static const struct got_error *
1092 set_child_log_view(struct tog_view *view, struct tog_view *child)
1094 struct tog_log_view_state *s = &view->state.log;
1095 struct tog_diff_view_state *ds;
1096 struct commit_queue_entry *commit, *child_entry = NULL;
1097 int selected_idx = 0;
1099 if (child->type != TOG_VIEW_DIFF)
1100 return NULL;
1101 ds = &child->state.diff;
1103 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1104 if (got_object_id_cmp(commit->id, ds->id) == 0) {
1105 child_entry = commit;
1106 break;
1109 if (child_entry == NULL)
1110 return NULL;
1112 commit = s->first_displayed_entry;
1113 while (commit) {
1114 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1115 s->selected_entry = child_entry;
1116 s->selected = selected_idx;
1117 break;
1119 if (commit == s->last_displayed_entry)
1120 break;
1121 selected_idx++;
1122 commit = TAILQ_NEXT(commit, entry);
1125 return show_log_view(view);
1128 static const struct got_error *
1129 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1130 struct got_repository *repo, const char *path)
1132 const struct got_error *err = NULL;
1133 struct got_object_id *head_id = NULL;
1134 int nfetched;
1135 struct tog_log_view_state *s = &view->state.log;
1137 err = got_repo_map_path(&s->in_repo_path, repo, path);
1138 if (err != NULL)
1139 goto done;
1141 err = get_head_commit_id(&head_id, repo);
1142 if (err)
1143 return err;
1145 /* The graph contains all commits. */
1146 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1147 if (err)
1148 goto done;
1149 /* The commit queue contains a subset of commits filtered by path. */
1150 TAILQ_INIT(&s->commits.head);
1151 s->commits.ncommits = 0;
1153 /* Populate commit graph with a sufficient number of commits. */
1154 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1155 start_id, repo);
1156 if (err)
1157 goto done;
1160 * Open the initial batch of commits, sorted in commit graph order.
1161 * We keep all commits open throughout the lifetime of the log view
1162 * in order to avoid having to re-fetch commits from disk while
1163 * updating the display.
1165 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1166 repo, s->in_repo_path);
1167 if (err) {
1168 if (err->code != GOT_ERR_ITER_COMPLETED)
1169 goto done;
1170 err = NULL;
1173 s->first_displayed_entry =
1174 TAILQ_FIRST(&s->commits.head);
1175 s->selected_entry = s->first_displayed_entry;
1176 s->repo = repo;
1178 view->show = show_log_view;
1179 view->input = input_log_view;
1180 view->close = close_log_view;
1181 view->set_child = set_child_log_view;
1182 done:
1183 free(head_id);
1184 return err;
1187 static const struct got_error *
1188 close_log_view(struct tog_view *view)
1190 struct tog_log_view_state *s = &view->state.log;
1192 if (s->graph)
1193 got_commit_graph_close(s->graph);
1194 free_commits(&s->commits);
1195 free(s->in_repo_path);
1196 return NULL;
1199 static const struct got_error *
1200 update_diff_child_view(struct tog_view *parent,
1201 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1203 const struct got_error *err = NULL;
1204 struct tog_diff_view_state *ds;
1205 struct got_object *obj1 = NULL, *obj2 = NULL;
1206 struct got_object_qid *parent_id;
1207 struct tog_view *child_view = parent->child;
1209 if (child_view == NULL)
1210 return NULL;
1211 if (child_view->type != TOG_VIEW_DIFF)
1212 return NULL;
1213 ds = &child_view->state.diff;
1214 if (got_object_id_cmp(ds->id, selected_entry->id) == 0)
1215 return NULL;
1217 err = got_object_open(&obj2, repo, selected_entry->id);
1218 if (err)
1219 return err;
1221 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1222 if (parent_id) {
1223 err = got_object_open(&obj1, repo, parent_id->id);
1224 if (err)
1225 goto done;
1228 err = close_diff_view(child_view);
1229 if (err)
1230 goto done;
1232 err = open_diff_view(child_view, obj1, obj2, repo);
1233 if (err)
1234 goto done;
1235 done:
1236 if (obj1)
1237 got_object_close(obj1);
1238 if (obj2)
1239 got_object_close(obj2);
1240 return err;
1243 static const struct got_error *
1244 show_log_view(struct tog_view *view)
1246 const struct got_error *err = NULL;
1247 struct tog_log_view_state *s = &view->state.log;
1249 err = draw_commits(view, &s->last_displayed_entry,
1250 &s->selected_entry, s->first_displayed_entry,
1251 &s->commits, s->selected, view->nlines, s->graph,
1252 s->repo, s->in_repo_path);
1253 if (err)
1254 return err;
1256 return update_diff_child_view(view, s->selected_entry, s->repo);
1259 static const struct got_error *
1260 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1261 struct tog_view *view, int ch)
1263 const struct got_error *err = NULL;
1264 struct tog_log_view_state *s = &view->state.log;
1266 switch (ch) {
1267 case 'k':
1268 case KEY_UP:
1269 case '[':
1270 if (s->selected > 0)
1271 s->selected--;
1272 if (s->selected > 0)
1273 break;
1274 scroll_up(&s->first_displayed_entry, 1,
1275 &s->commits);
1276 break;
1277 case KEY_PPAGE:
1278 if (TAILQ_FIRST(&s->commits.head) ==
1279 s->first_displayed_entry) {
1280 s->selected = 0;
1281 break;
1283 scroll_up(&s->first_displayed_entry,
1284 view->nlines, &s->commits);
1285 break;
1286 case 'j':
1287 case KEY_DOWN:
1288 case ']':
1289 if (s->selected < MIN(view->nlines - 2,
1290 s->commits.ncommits - 1)) {
1291 s->selected++;
1292 break;
1294 err = scroll_down(&s->first_displayed_entry, 1,
1295 s->last_displayed_entry, &s->commits,
1296 s->graph, s->repo, s->in_repo_path);
1297 if (err) {
1298 if (err->code != GOT_ERR_ITER_COMPLETED)
1299 break;
1300 err = NULL;
1302 break;
1303 case KEY_NPAGE: {
1304 struct commit_queue_entry *first;
1305 first = s->first_displayed_entry;
1306 err = scroll_down(&s->first_displayed_entry,
1307 view->nlines, s->last_displayed_entry,
1308 &s->commits, s->graph, s->repo,
1309 s->in_repo_path);
1310 if (err == NULL)
1311 break;
1312 if (err->code != GOT_ERR_ITER_COMPLETED)
1313 break;
1314 if (first == s->first_displayed_entry &&
1315 s->selected < MIN(view->nlines - 2,
1316 s->commits.ncommits - 1)) {
1317 /* can't scroll further down */
1318 s->selected = MIN(view->nlines - 2,
1319 s->commits.ncommits - 1);
1321 err = NULL;
1322 break;
1324 case KEY_RESIZE:
1325 if (s->selected > view->nlines - 2)
1326 s->selected = view->nlines - 2;
1327 if (s->selected > s->commits.ncommits - 1)
1328 s->selected = s->commits.ncommits - 1;
1329 break;
1330 case KEY_ENTER:
1331 case '\r':
1332 err = show_commit(new_view, view, s->selected_entry,
1333 s->repo);
1334 break;
1335 case 't':
1336 err = browse_commit(new_view, view, s->selected_entry,
1337 s->repo);
1338 break;
1339 default:
1340 break;
1343 return err;
1346 static const struct got_error *
1347 cmd_log(int argc, char *argv[])
1349 const struct got_error *error;
1350 struct got_repository *repo = NULL;
1351 struct got_object_id *start_id = NULL;
1352 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1353 char *start_commit = NULL;
1354 int ch;
1355 struct tog_view *view;
1357 #ifndef PROFILE
1358 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1359 err(1, "pledge");
1360 #endif
1362 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1363 switch (ch) {
1364 case 'c':
1365 start_commit = optarg;
1366 break;
1367 case 'r':
1368 repo_path = realpath(optarg, NULL);
1369 if (repo_path == NULL)
1370 err(1, "-r option");
1371 break;
1372 default:
1373 usage();
1374 /* NOTREACHED */
1378 argc -= optind;
1379 argv += optind;
1381 if (argc == 0)
1382 path = strdup("");
1383 else if (argc == 1)
1384 path = strdup(argv[0]);
1385 else
1386 usage_log();
1387 if (path == NULL)
1388 return got_error_from_errno();
1390 cwd = getcwd(NULL, 0);
1391 if (cwd == NULL) {
1392 error = got_error_from_errno();
1393 goto done;
1395 if (repo_path == NULL) {
1396 repo_path = strdup(cwd);
1397 if (repo_path == NULL) {
1398 error = got_error_from_errno();
1399 goto done;
1403 error = got_repo_open(&repo, repo_path);
1404 if (error != NULL)
1405 goto done;
1407 if (start_commit == NULL) {
1408 error = get_head_commit_id(&start_id, repo);
1409 if (error != NULL)
1410 goto done;
1411 } else {
1412 struct got_object *obj;
1413 error = got_object_open_by_id_str(&obj, repo, start_commit);
1414 if (error == NULL) {
1415 start_id = got_object_get_id(obj);
1416 if (start_id == NULL)
1417 error = got_error_from_errno();
1418 goto done;
1421 if (error != NULL)
1422 goto done;
1424 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1425 if (view == NULL) {
1426 error = got_error_from_errno();
1427 goto done;
1429 error = open_log_view(view, start_id, repo, path);
1430 if (error)
1431 goto done;
1432 error = view_loop(view);
1433 done:
1434 free(repo_path);
1435 free(cwd);
1436 free(path);
1437 free(start_id);
1438 if (repo)
1439 got_repo_close(repo);
1440 return error;
1443 __dead static void
1444 usage_diff(void)
1446 endwin();
1447 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1448 getprogname());
1449 exit(1);
1452 static char *
1453 parse_next_line(FILE *f, size_t *len)
1455 char *line;
1456 size_t linelen;
1457 size_t lineno;
1458 const char delim[3] = { '\0', '\0', '\0'};
1460 line = fparseln(f, &linelen, &lineno, delim, 0);
1461 if (len)
1462 *len = linelen;
1463 return line;
1466 static const struct got_error *
1467 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1468 int *last_displayed_line, int *eof, int max_lines)
1470 const struct got_error *err;
1471 int nlines = 0, nprinted = 0;
1472 char *line;
1473 size_t len;
1474 wchar_t *wline;
1475 int width;
1477 rewind(f);
1478 werase(view->window);
1480 *eof = 0;
1481 while (nprinted < max_lines) {
1482 line = parse_next_line(f, &len);
1483 if (line == NULL) {
1484 *eof = 1;
1485 break;
1487 if (++nlines < *first_displayed_line) {
1488 free(line);
1489 continue;
1492 err = format_line(&wline, &width, line, view->ncols);
1493 if (err) {
1494 free(line);
1495 free(wline);
1496 return err;
1498 waddwstr(view->window, wline);
1499 if (width < view->ncols)
1500 waddch(view->window, '\n');
1501 if (++nprinted == 1)
1502 *first_displayed_line = nlines;
1503 free(line);
1504 free(wline);
1505 wline = NULL;
1507 *last_displayed_line = nlines;
1509 update_panels();
1511 return NULL;
1514 static const struct got_error *
1515 open_diff_view(struct tog_view *view, struct got_object *obj1,
1516 struct got_object *obj2, struct got_repository *repo)
1518 const struct got_error *err;
1519 FILE *f;
1521 if (obj1 != NULL && obj2 != NULL &&
1522 got_object_get_type(obj1) != got_object_get_type(obj2))
1523 return got_error(GOT_ERR_OBJ_TYPE);
1525 f = got_opentemp();
1526 if (f == NULL)
1527 return got_error_from_errno();
1529 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1530 case GOT_OBJ_TYPE_BLOB:
1531 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1532 break;
1533 case GOT_OBJ_TYPE_TREE:
1534 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1535 break;
1536 case GOT_OBJ_TYPE_COMMIT:
1537 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1538 break;
1539 default:
1540 return got_error(GOT_ERR_OBJ_TYPE);
1543 fflush(f);
1545 view->state.diff.id = got_object_get_id(obj2);
1546 if (view->state.diff.id == NULL)
1547 return got_error_from_errno();
1548 view->state.diff.f = f;
1549 view->state.diff.first_displayed_line = 1;
1550 view->state.diff.last_displayed_line = view->nlines;
1552 view->show = show_diff_view;
1553 view->input = input_diff_view;
1554 view->close = close_diff_view;
1556 return NULL;
1559 static const struct got_error *
1560 close_diff_view(struct tog_view *view)
1562 const struct got_error *err = NULL;
1564 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1565 err = got_error_from_errno();
1566 free(view->state.diff.id);
1567 return err;
1570 static const struct got_error *
1571 show_diff_view(struct tog_view *view)
1573 struct tog_diff_view_state *s = &view->state.diff;
1575 return draw_file(view, s->f, &s->first_displayed_line,
1576 &s->last_displayed_line, &s->eof, view->nlines);
1579 static const struct got_error *
1580 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1581 struct tog_view *view, int ch)
1583 const struct got_error *err = NULL;
1584 struct tog_diff_view_state *s = &view->state.diff;
1585 int i;
1587 switch (ch) {
1588 case 'k':
1589 case KEY_UP:
1590 if (s->first_displayed_line > 1)
1591 s->first_displayed_line--;
1592 break;
1593 case KEY_PPAGE:
1594 i = 0;
1595 while (i++ < view->nlines - 1 &&
1596 s->first_displayed_line > 1)
1597 s->first_displayed_line--;
1598 break;
1599 case 'j':
1600 case KEY_DOWN:
1601 if (!s->eof)
1602 s->first_displayed_line++;
1603 break;
1604 case KEY_NPAGE:
1605 case ' ':
1606 i = 0;
1607 while (!s->eof && i++ < view->nlines - 1) {
1608 char *line;
1609 line = parse_next_line(s->f, NULL);
1610 s->first_displayed_line++;
1611 if (line == NULL)
1612 break;
1614 break;
1615 case '[':
1616 case ']': {
1617 struct tog_log_view_state *ls;
1618 struct commit_queue_entry *entry;
1619 struct tog_view *diff_view;
1621 if (view->parent == NULL)
1622 break;
1623 if (view->parent->type != TOG_VIEW_LOG)
1624 break;
1625 ls = &view->parent->state.log;
1627 if (ch == '[') {
1628 entry = TAILQ_PREV(ls->selected_entry,
1629 commit_queue_head, entry);
1630 } else {
1631 entry = TAILQ_NEXT(ls->selected_entry, entry);
1632 if (entry == NULL) {
1633 err = fetch_next_commit(&entry,
1634 ls->selected_entry,
1635 &ls->commits, ls->graph,
1636 ls->repo, ls->in_repo_path);
1637 if (err)
1638 break;
1641 if (entry == NULL)
1642 break;
1643 err = show_commit(&diff_view, view->parent,
1644 entry, ls->repo);
1645 if (err)
1646 break;
1647 *new_view = diff_view;
1648 *dead_view = view;
1649 break;
1651 default:
1652 break;
1655 return err;
1658 static const struct got_error *
1659 cmd_diff(int argc, char *argv[])
1661 const struct got_error *error = NULL;
1662 struct got_repository *repo = NULL;
1663 struct got_object *obj1 = NULL, *obj2 = NULL;
1664 char *repo_path = NULL;
1665 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1666 int ch;
1667 struct tog_view *view;
1669 #ifndef PROFILE
1670 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1671 err(1, "pledge");
1672 #endif
1674 while ((ch = getopt(argc, argv, "")) != -1) {
1675 switch (ch) {
1676 default:
1677 usage();
1678 /* NOTREACHED */
1682 argc -= optind;
1683 argv += optind;
1685 if (argc == 0) {
1686 usage_diff(); /* TODO show local worktree changes */
1687 } else if (argc == 2) {
1688 repo_path = getcwd(NULL, 0);
1689 if (repo_path == NULL)
1690 return got_error_from_errno();
1691 obj_id_str1 = argv[0];
1692 obj_id_str2 = argv[1];
1693 } else if (argc == 3) {
1694 repo_path = realpath(argv[0], NULL);
1695 if (repo_path == NULL)
1696 return got_error_from_errno();
1697 obj_id_str1 = argv[1];
1698 obj_id_str2 = argv[2];
1699 } else
1700 usage_diff();
1702 error = got_repo_open(&repo, repo_path);
1703 free(repo_path);
1704 if (error)
1705 goto done;
1707 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1708 if (error)
1709 goto done;
1711 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1712 if (error)
1713 goto done;
1715 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1716 if (view == NULL) {
1717 error = got_error_from_errno();
1718 goto done;
1720 error = open_diff_view(view, obj1, obj2, repo);
1721 if (error)
1722 goto done;
1723 error = view_loop(view);
1724 done:
1725 got_repo_close(repo);
1726 if (obj1)
1727 got_object_close(obj1);
1728 if (obj2)
1729 got_object_close(obj2);
1730 return error;
1733 __dead static void
1734 usage_blame(void)
1736 endwin();
1737 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1738 getprogname());
1739 exit(1);
1742 struct tog_blame_line {
1743 int annotated;
1744 struct got_object_id *id;
1747 static const struct got_error *
1748 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1749 const char *path, struct tog_blame_line *lines, int nlines,
1750 int blame_complete, int selected_line, int *first_displayed_line,
1751 int *last_displayed_line, int *eof, int max_lines)
1753 const struct got_error *err;
1754 int lineno = 0, nprinted = 0;
1755 char *line;
1756 size_t len;
1757 wchar_t *wline;
1758 int width, wlimit;
1759 struct tog_blame_line *blame_line;
1760 struct got_object_id *prev_id = NULL;
1761 char *id_str;
1763 err = got_object_id_str(&id_str, id);
1764 if (err)
1765 return err;
1767 rewind(f);
1768 werase(view->window);
1770 if (asprintf(&line, "commit: %s", id_str) == -1) {
1771 err = got_error_from_errno();
1772 free(id_str);
1773 return err;
1776 err = format_line(&wline, &width, line, view->ncols);
1777 free(line);
1778 line = NULL;
1779 waddwstr(view->window, wline);
1780 free(wline);
1781 wline = NULL;
1782 if (width < view->ncols)
1783 waddch(view->window, '\n');
1785 if (asprintf(&line, "[%d/%d] %s%s",
1786 *first_displayed_line - 1 + selected_line, nlines,
1787 blame_complete ? "" : "annotating ", path) == -1) {
1788 free(id_str);
1789 return got_error_from_errno();
1791 free(id_str);
1792 err = format_line(&wline, &width, line, view->ncols);
1793 free(line);
1794 line = NULL;
1795 if (err)
1796 return err;
1797 waddwstr(view->window, wline);
1798 free(wline);
1799 wline = NULL;
1800 if (width < view->ncols)
1801 waddch(view->window, '\n');
1803 *eof = 0;
1804 while (nprinted < max_lines - 2) {
1805 line = parse_next_line(f, &len);
1806 if (line == NULL) {
1807 *eof = 1;
1808 break;
1810 if (++lineno < *first_displayed_line) {
1811 free(line);
1812 continue;
1815 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1816 err = format_line(&wline, &width, line, wlimit);
1817 if (err) {
1818 free(line);
1819 return err;
1822 if (nprinted == selected_line - 1)
1823 wstandout(view->window);
1825 blame_line = &lines[lineno - 1];
1826 if (blame_line->annotated && prev_id &&
1827 got_object_id_cmp(prev_id, blame_line->id) == 0)
1828 waddstr(view->window, " ");
1829 else if (blame_line->annotated) {
1830 char *id_str;
1831 err = got_object_id_str(&id_str, blame_line->id);
1832 if (err) {
1833 free(line);
1834 free(wline);
1835 return err;
1837 wprintw(view->window, "%.8s ", id_str);
1838 free(id_str);
1839 prev_id = blame_line->id;
1840 } else {
1841 waddstr(view->window, "........ ");
1842 prev_id = NULL;
1845 waddwstr(view->window, wline);
1846 while (width < wlimit) {
1847 waddch(view->window, ' ');
1848 width++;
1850 if (nprinted == selected_line - 1)
1851 wstandend(view->window);
1852 if (++nprinted == 1)
1853 *first_displayed_line = lineno;
1854 free(line);
1855 free(wline);
1856 wline = NULL;
1858 *last_displayed_line = lineno;
1860 update_panels();
1862 return NULL;
1865 static const struct got_error *
1866 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1868 const struct got_error *err = NULL;
1869 struct tog_blame_cb_args *a = arg;
1870 struct tog_blame_line *line;
1872 if (nlines != a->nlines ||
1873 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1874 return got_error(GOT_ERR_RANGE);
1876 if (pthread_mutex_lock(a->mutex) != 0)
1877 return got_error_from_errno();
1879 if (*a->quit) { /* user has quit the blame view */
1880 err = got_error(GOT_ERR_ITER_COMPLETED);
1881 goto done;
1884 if (lineno == -1)
1885 goto done; /* no change in this commit */
1887 line = &a->lines[lineno - 1];
1888 if (line->annotated)
1889 goto done;
1891 line->id = got_object_id_dup(id);
1892 if (line->id == NULL) {
1893 err = got_error_from_errno();
1894 goto done;
1896 line->annotated = 1;
1898 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1899 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1900 a->last_displayed_line, a->eof, a->view->nlines);
1901 done:
1902 if (pthread_mutex_unlock(a->mutex) != 0)
1903 return got_error_from_errno();
1904 return err;
1907 static void *
1908 blame_thread(void *arg)
1910 const struct got_error *err;
1911 struct tog_blame_thread_args *ta = arg;
1912 struct tog_blame_cb_args *a = ta->cb_args;
1914 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1915 blame_cb, ta->cb_args);
1917 if (pthread_mutex_lock(a->mutex) != 0)
1918 return (void *)got_error_from_errno();
1920 got_repo_close(ta->repo);
1921 ta->repo = NULL;
1922 *ta->complete = 1;
1923 if (!err)
1924 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1925 a->lines, a->nlines, 1, *a->selected_line,
1926 a->first_displayed_line, a->last_displayed_line, a->eof,
1927 a->view->nlines);
1929 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1930 err = got_error_from_errno();
1932 return (void *)err;
1935 static struct got_object_id *
1936 get_selected_commit_id(struct tog_blame_line *lines,
1937 int first_displayed_line, int selected_line)
1939 struct tog_blame_line *line;
1941 line = &lines[first_displayed_line - 1 + selected_line - 1];
1942 if (!line->annotated)
1943 return NULL;
1945 return line->id;
1948 static const struct got_error *
1949 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1950 struct tog_blame_line *lines, int first_displayed_line,
1951 int selected_line, struct got_repository *repo)
1953 const struct got_error *err = NULL;
1954 struct got_commit_object *commit = NULL;
1955 struct got_object_id *selected_id;
1956 struct got_object_qid *pid;
1958 *pobj = NULL;
1959 *obj = NULL;
1961 selected_id = get_selected_commit_id(lines,
1962 first_displayed_line, selected_line);
1963 if (selected_id == NULL)
1964 return NULL;
1966 err = got_object_open(obj, repo, selected_id);
1967 if (err)
1968 goto done;
1970 err = got_object_commit_open(&commit, repo, *obj);
1971 if (err)
1972 goto done;
1974 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1975 if (pid) {
1976 err = got_object_open(pobj, repo, pid->id);
1977 if (err)
1978 goto done;
1980 done:
1981 if (commit)
1982 got_object_commit_close(commit);
1983 return err;
1986 static const struct got_error *
1987 stop_blame(struct tog_blame *blame)
1989 const struct got_error *err = NULL;
1990 int i;
1992 if (blame->thread) {
1993 if (pthread_join(blame->thread, (void **)&err) != 0)
1994 err = got_error_from_errno();
1995 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1996 err = NULL;
1997 blame->thread = NULL;
1999 if (blame->thread_args.repo) {
2000 got_repo_close(blame->thread_args.repo);
2001 blame->thread_args.repo = NULL;
2003 if (blame->f) {
2004 fclose(blame->f);
2005 blame->f = NULL;
2007 for (i = 0; i < blame->nlines; i++)
2008 free(blame->lines[i].id);
2009 free(blame->lines);
2010 blame->lines = NULL;
2011 free(blame->cb_args.commit_id);
2012 blame->cb_args.commit_id = NULL;
2014 return err;
2017 static const struct got_error *
2018 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2019 struct tog_view *view, int *blame_complete,
2020 int *first_displayed_line, int *last_displayed_line,
2021 int *selected_line, int *done, int *eof, const char *path,
2022 struct got_object_id *commit_id,
2023 struct got_repository *repo)
2025 const struct got_error *err = NULL;
2026 struct got_blob_object *blob = NULL;
2027 struct got_repository *thread_repo = NULL;
2028 struct got_object *obj;
2030 err = got_object_open_by_path(&obj, repo, commit_id, path);
2031 if (err)
2032 goto done;
2033 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2034 err = got_error(GOT_ERR_OBJ_TYPE);
2035 goto done;
2038 err = got_object_blob_open(&blob, repo, obj, 8192);
2039 if (err)
2040 goto done;
2041 blame->f = got_opentemp();
2042 if (blame->f == NULL) {
2043 err = got_error_from_errno();
2044 goto done;
2046 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2047 blame->f, blob);
2048 if (err)
2049 goto done;
2051 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2052 if (blame->lines == NULL) {
2053 err = got_error_from_errno();
2054 goto done;
2057 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2058 if (err)
2059 goto done;
2061 blame->cb_args.view = view;
2062 blame->cb_args.lines = blame->lines;
2063 blame->cb_args.nlines = blame->nlines;
2064 blame->cb_args.mutex = mutex;
2065 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2066 if (blame->cb_args.commit_id == NULL) {
2067 err = got_error_from_errno();
2068 goto done;
2070 blame->cb_args.f = blame->f;
2071 blame->cb_args.path = path;
2072 blame->cb_args.first_displayed_line = first_displayed_line;
2073 blame->cb_args.selected_line = selected_line;
2074 blame->cb_args.last_displayed_line = last_displayed_line;
2075 blame->cb_args.quit = done;
2076 blame->cb_args.eof = eof;
2078 blame->thread_args.path = path;
2079 blame->thread_args.repo = thread_repo;
2080 blame->thread_args.cb_args = &blame->cb_args;
2081 blame->thread_args.complete = blame_complete;
2082 *blame_complete = 0;
2084 if (pthread_create(&blame->thread, NULL, blame_thread,
2085 &blame->thread_args) != 0) {
2086 err = got_error_from_errno();
2087 goto done;
2090 done:
2091 if (blob)
2092 got_object_blob_close(blob);
2093 if (obj)
2094 got_object_close(obj);
2095 if (err)
2096 stop_blame(blame);
2097 return err;
2100 static const struct got_error *
2101 open_blame_view(struct tog_view *view, char *path,
2102 struct got_object_id *commit_id, struct got_repository *repo)
2104 const struct got_error *err = NULL;
2105 struct tog_blame_view_state *s = &view->state.blame;
2107 SIMPLEQ_INIT(&s->blamed_commits);
2109 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2110 return got_error_from_errno();
2112 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2113 if (err)
2114 return err;
2116 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2117 s->first_displayed_line = 1;
2118 s->last_displayed_line = view->nlines;
2119 s->selected_line = 1;
2120 s->blame_complete = 0;
2121 s->path = path;
2122 if (s->path == NULL)
2123 return got_error_from_errno();
2124 s->repo = repo;
2125 s->commit_id = commit_id;
2126 memset(&s->blame, 0, sizeof(s->blame));
2128 view->show = show_blame_view;
2129 view->input = input_blame_view;
2130 view->close = close_blame_view;
2132 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2133 &s->first_displayed_line, &s->last_displayed_line,
2134 &s->selected_line, &s->done, &s->eof, s->path,
2135 s->blamed_commit->id, s->repo);
2138 static const struct got_error *
2139 close_blame_view(struct tog_view *view)
2141 const struct got_error *err = NULL;
2142 struct tog_blame_view_state *s = &view->state.blame;
2144 if (s->blame.thread)
2145 err = stop_blame(&s->blame);
2147 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2148 struct got_object_qid *blamed_commit;
2149 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2150 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2151 got_object_qid_free(blamed_commit);
2154 free(s->path);
2156 return err;
2159 static const struct got_error *
2160 show_blame_view(struct tog_view *view)
2162 const struct got_error *err = NULL;
2163 struct tog_blame_view_state *s = &view->state.blame;
2165 if (pthread_mutex_lock(&s->mutex) != 0)
2166 return got_error_from_errno();
2168 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2169 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2170 s->selected_line, &s->first_displayed_line,
2171 &s->last_displayed_line, &s->eof, view->nlines);
2173 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2174 err = got_error_from_errno();
2176 return err;
2179 static const struct got_error *
2180 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2181 struct tog_view *view, int ch)
2183 const struct got_error *err = NULL, *thread_err = NULL;
2184 struct got_object *obj = NULL, *pobj = NULL;
2185 struct tog_view *diff_view;
2186 struct tog_blame_view_state *s = &view->state.blame;
2188 if (pthread_mutex_lock(&s->mutex) != 0) {
2189 err = got_error_from_errno();
2190 goto done;
2193 switch (ch) {
2194 case 'q':
2195 s->done = 1;
2196 if (pthread_mutex_unlock(&s->mutex) != 0) {
2197 err = got_error_from_errno();
2198 goto done;
2200 return stop_blame(&s->blame);
2201 case 'k':
2202 case KEY_UP:
2203 if (s->selected_line > 1)
2204 s->selected_line--;
2205 else if (s->selected_line == 1 &&
2206 s->first_displayed_line > 1)
2207 s->first_displayed_line--;
2208 break;
2209 case KEY_PPAGE:
2210 if (s->first_displayed_line == 1) {
2211 s->selected_line = 1;
2212 break;
2214 if (s->first_displayed_line > view->nlines - 2)
2215 s->first_displayed_line -=
2216 (view->nlines - 2);
2217 else
2218 s->first_displayed_line = 1;
2219 break;
2220 case 'j':
2221 case KEY_DOWN:
2222 if (s->selected_line < view->nlines - 2 &&
2223 s->first_displayed_line +
2224 s->selected_line <= s->blame.nlines)
2225 s->selected_line++;
2226 else if (s->last_displayed_line <
2227 s->blame.nlines)
2228 s->first_displayed_line++;
2229 break;
2230 case 'b':
2231 case 'p': {
2232 struct got_object_id *id;
2233 id = get_selected_commit_id(s->blame.lines,
2234 s->first_displayed_line, s->selected_line);
2235 if (id == NULL || got_object_id_cmp(id,
2236 s->blamed_commit->id) == 0)
2237 break;
2238 err = open_selected_commit(&pobj, &obj,
2239 s->blame.lines, s->first_displayed_line,
2240 s->selected_line, s->repo);
2241 if (err)
2242 break;
2243 if (pobj == NULL && obj == NULL)
2244 break;
2245 if (ch == 'p' && pobj == NULL)
2246 break;
2247 s->done = 1;
2248 if (pthread_mutex_unlock(&s->mutex) != 0) {
2249 err = got_error_from_errno();
2250 goto done;
2252 thread_err = stop_blame(&s->blame);
2253 s->done = 0;
2254 if (pthread_mutex_lock(&s->mutex) != 0) {
2255 err = got_error_from_errno();
2256 goto done;
2258 if (thread_err)
2259 break;
2260 id = got_object_get_id(ch == 'b' ? obj : pobj);
2261 got_object_close(obj);
2262 obj = NULL;
2263 if (pobj) {
2264 got_object_close(pobj);
2265 pobj = NULL;
2267 if (id == NULL) {
2268 err = got_error_from_errno();
2269 break;
2271 err = got_object_qid_alloc(
2272 &s->blamed_commit, id);
2273 free(id);
2274 if (err)
2275 goto done;
2276 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2277 s->blamed_commit, entry);
2278 err = run_blame(&s->blame, &s->mutex, view,
2279 &s->blame_complete,
2280 &s->first_displayed_line,
2281 &s->last_displayed_line,
2282 &s->selected_line, &s->done, &s->eof,
2283 s->path, s->blamed_commit->id, s->repo);
2284 if (err)
2285 break;
2286 break;
2288 case 'B': {
2289 struct got_object_qid *first;
2290 first = SIMPLEQ_FIRST(&s->blamed_commits);
2291 if (!got_object_id_cmp(first->id, s->commit_id))
2292 break;
2293 s->done = 1;
2294 if (pthread_mutex_unlock(&s->mutex) != 0) {
2295 err = got_error_from_errno();
2296 goto done;
2298 thread_err = stop_blame(&s->blame);
2299 s->done = 0;
2300 if (pthread_mutex_lock(&s->mutex) != 0) {
2301 err = got_error_from_errno();
2302 goto done;
2304 if (thread_err)
2305 break;
2306 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2307 got_object_qid_free(s->blamed_commit);
2308 s->blamed_commit =
2309 SIMPLEQ_FIRST(&s->blamed_commits);
2310 err = run_blame(&s->blame, &s->mutex, view,
2311 &s->blame_complete,
2312 &s->first_displayed_line,
2313 &s->last_displayed_line,
2314 &s->selected_line, &s->done, &s->eof, s->path,
2315 s->blamed_commit->id, s->repo);
2316 if (err)
2317 break;
2318 break;
2320 case KEY_ENTER:
2321 case '\r':
2322 err = open_selected_commit(&pobj, &obj,
2323 s->blame.lines, s->first_displayed_line,
2324 s->selected_line, s->repo);
2325 if (err)
2326 break;
2327 if (pobj == NULL && obj == NULL)
2328 break;
2329 diff_view = view_open(0, 0, 0, 0, view,
2330 TOG_VIEW_DIFF);
2331 if (diff_view == NULL) {
2332 err = got_error_from_errno();
2333 break;
2335 err = open_diff_view(diff_view, pobj, obj,
2336 s->repo);
2337 if (err) {
2338 view_close(diff_view);
2339 break;
2341 *new_view = diff_view;
2342 if (pobj) {
2343 got_object_close(pobj);
2344 pobj = NULL;
2346 got_object_close(obj);
2347 obj = NULL;
2348 if (err)
2349 break;
2350 break;
2351 case KEY_NPAGE:
2352 case ' ':
2353 if (s->last_displayed_line >= s->blame.nlines &&
2354 s->selected_line < view->nlines - 2) {
2355 s->selected_line = MIN(s->blame.nlines,
2356 view->nlines - 2);
2357 break;
2359 if (s->last_displayed_line + view->nlines - 2
2360 <= s->blame.nlines)
2361 s->first_displayed_line +=
2362 view->nlines - 2;
2363 else
2364 s->first_displayed_line =
2365 s->blame.nlines -
2366 (view->nlines - 3);
2367 break;
2368 case KEY_RESIZE:
2369 if (s->selected_line > view->nlines - 2) {
2370 s->selected_line = MIN(s->blame.nlines,
2371 view->nlines - 2);
2373 break;
2374 default:
2375 break;
2378 if (pthread_mutex_unlock(&s->mutex) != 0)
2379 err = got_error_from_errno();
2380 done:
2381 if (pobj)
2382 got_object_close(pobj);
2383 return thread_err ? thread_err : err;
2386 static const struct got_error *
2387 cmd_blame(int argc, char *argv[])
2389 const struct got_error *error;
2390 struct got_repository *repo = NULL;
2391 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2392 struct got_object_id *commit_id = NULL;
2393 char *commit_id_str = NULL;
2394 int ch;
2395 struct tog_view *view;
2397 #ifndef PROFILE
2398 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2399 err(1, "pledge");
2400 #endif
2402 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2403 switch (ch) {
2404 case 'c':
2405 commit_id_str = optarg;
2406 break;
2407 case 'r':
2408 repo_path = realpath(optarg, NULL);
2409 if (repo_path == NULL)
2410 err(1, "-r option");
2411 break;
2412 default:
2413 usage();
2414 /* NOTREACHED */
2418 argc -= optind;
2419 argv += optind;
2421 if (argc == 1)
2422 path = argv[0];
2423 else
2424 usage_blame();
2426 cwd = getcwd(NULL, 0);
2427 if (cwd == NULL) {
2428 error = got_error_from_errno();
2429 goto done;
2431 if (repo_path == NULL) {
2432 repo_path = strdup(cwd);
2433 if (repo_path == NULL) {
2434 error = got_error_from_errno();
2435 goto done;
2440 error = got_repo_open(&repo, repo_path);
2441 if (error != NULL)
2442 return error;
2444 error = got_repo_map_path(&in_repo_path, repo, path);
2445 if (error != NULL)
2446 goto done;
2448 if (commit_id_str == NULL) {
2449 struct got_reference *head_ref;
2450 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2451 if (error != NULL)
2452 goto done;
2453 error = got_ref_resolve(&commit_id, repo, head_ref);
2454 got_ref_close(head_ref);
2455 } else {
2456 struct got_object *obj;
2457 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2458 if (error != NULL)
2459 goto done;
2460 commit_id = got_object_get_id(obj);
2461 if (commit_id == NULL)
2462 error = got_error_from_errno();
2463 got_object_close(obj);
2465 if (error != NULL)
2466 goto done;
2468 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2469 if (view == NULL) {
2470 error = got_error_from_errno();
2471 goto done;
2473 error = open_blame_view(view, in_repo_path, commit_id, repo);
2474 if (error)
2475 goto done;
2476 error = view_loop(view);
2477 done:
2478 free(repo_path);
2479 free(cwd);
2480 free(commit_id);
2481 if (repo)
2482 got_repo_close(repo);
2483 return error;
2486 static const struct got_error *
2487 draw_tree_entries(struct tog_view *view,
2488 struct got_tree_entry **first_displayed_entry,
2489 struct got_tree_entry **last_displayed_entry,
2490 struct got_tree_entry **selected_entry, int *ndisplayed,
2491 const char *label, int show_ids, const char *parent_path,
2492 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2494 const struct got_error *err = NULL;
2495 struct got_tree_entry *te;
2496 wchar_t *wline;
2497 int width, n;
2499 *ndisplayed = 0;
2501 werase(view->window);
2503 if (limit == 0)
2504 return NULL;
2506 err = format_line(&wline, &width, label, view->ncols);
2507 if (err)
2508 return err;
2509 waddwstr(view->window, wline);
2510 free(wline);
2511 wline = NULL;
2512 if (width < view->ncols)
2513 waddch(view->window, '\n');
2514 if (--limit <= 0)
2515 return NULL;
2516 err = format_line(&wline, &width, parent_path, view->ncols);
2517 if (err)
2518 return err;
2519 waddwstr(view->window, wline);
2520 free(wline);
2521 wline = NULL;
2522 if (width < view->ncols)
2523 waddch(view->window, '\n');
2524 if (--limit <= 0)
2525 return NULL;
2526 waddch(view->window, '\n');
2527 if (--limit <= 0)
2528 return NULL;
2530 te = SIMPLEQ_FIRST(&entries->head);
2531 if (*first_displayed_entry == NULL) {
2532 if (selected == 0) {
2533 wstandout(view->window);
2534 *selected_entry = NULL;
2536 waddstr(view->window, " ..\n"); /* parent directory */
2537 if (selected == 0)
2538 wstandend(view->window);
2539 (*ndisplayed)++;
2540 if (--limit <= 0)
2541 return NULL;
2542 n = 1;
2543 } else {
2544 n = 0;
2545 while (te != *first_displayed_entry)
2546 te = SIMPLEQ_NEXT(te, entry);
2549 while (te) {
2550 char *line = NULL, *id_str = NULL;
2552 if (show_ids) {
2553 err = got_object_id_str(&id_str, te->id);
2554 if (err)
2555 return got_error_from_errno();
2557 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2558 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2559 free(id_str);
2560 return got_error_from_errno();
2562 free(id_str);
2563 err = format_line(&wline, &width, line, view->ncols);
2564 if (err) {
2565 free(line);
2566 break;
2568 if (n == selected) {
2569 wstandout(view->window);
2570 *selected_entry = te;
2572 waddwstr(view->window, wline);
2573 if (width < view->ncols)
2574 waddch(view->window, '\n');
2575 if (n == selected)
2576 wstandend(view->window);
2577 free(line);
2578 free(wline);
2579 wline = NULL;
2580 n++;
2581 (*ndisplayed)++;
2582 *last_displayed_entry = te;
2583 if (--limit <= 0)
2584 break;
2585 te = SIMPLEQ_NEXT(te, entry);
2588 return err;
2591 static void
2592 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2593 const struct got_tree_entries *entries, int isroot)
2595 struct got_tree_entry *te, *prev;
2596 int i;
2598 if (*first_displayed_entry == NULL)
2599 return;
2601 te = SIMPLEQ_FIRST(&entries->head);
2602 if (*first_displayed_entry == te) {
2603 if (!isroot)
2604 *first_displayed_entry = NULL;
2605 return;
2608 /* XXX this is stupid... switch to TAILQ? */
2609 for (i = 0; i < maxscroll; i++) {
2610 while (te != *first_displayed_entry) {
2611 prev = te;
2612 te = SIMPLEQ_NEXT(te, entry);
2614 *first_displayed_entry = prev;
2615 te = SIMPLEQ_FIRST(&entries->head);
2617 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2618 *first_displayed_entry = NULL;
2621 static void
2622 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2623 struct got_tree_entry *last_displayed_entry,
2624 const struct got_tree_entries *entries)
2626 struct got_tree_entry *next;
2627 int n = 0;
2629 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2630 return;
2632 if (*first_displayed_entry)
2633 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2634 else
2635 next = SIMPLEQ_FIRST(&entries->head);
2636 while (next) {
2637 *first_displayed_entry = next;
2638 if (++n >= maxscroll)
2639 break;
2640 next = SIMPLEQ_NEXT(next, entry);
2644 static const struct got_error *
2645 tree_entry_path(char **path, struct tog_parent_trees *parents,
2646 struct got_tree_entry *te)
2648 const struct got_error *err = NULL;
2649 struct tog_parent_tree *pt;
2650 size_t len = 2; /* for leading slash and NUL */
2652 TAILQ_FOREACH(pt, parents, entry)
2653 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2654 if (te)
2655 len += strlen(te->name);
2657 *path = calloc(1, len);
2658 if (path == NULL)
2659 return got_error_from_errno();
2661 (*path)[0] = '/';
2662 pt = TAILQ_LAST(parents, tog_parent_trees);
2663 while (pt) {
2664 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2665 err = got_error(GOT_ERR_NO_SPACE);
2666 goto done;
2668 if (strlcat(*path, "/", len) >= len) {
2669 err = got_error(GOT_ERR_NO_SPACE);
2670 goto done;
2672 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2674 if (te) {
2675 if (strlcat(*path, te->name, len) >= len) {
2676 err = got_error(GOT_ERR_NO_SPACE);
2677 goto done;
2680 done:
2681 if (err) {
2682 free(*path);
2683 *path = NULL;
2685 return err;
2688 static const struct got_error *
2689 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2690 struct got_tree_entry *te, struct tog_parent_trees *parents,
2691 struct got_object_id *commit_id, struct got_repository *repo)
2693 const struct got_error *err = NULL;
2694 char *path;
2695 struct tog_view *blame_view;
2697 err = tree_entry_path(&path, parents, te);
2698 if (err)
2699 return err;
2701 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2702 if (blame_view == NULL)
2703 return got_error_from_errno();
2705 err = open_blame_view(blame_view, path, commit_id, repo);
2706 if (err) {
2707 view_close(blame_view);
2708 free(path);
2709 } else
2710 *new_view = blame_view;
2711 return err;
2714 static const struct got_error *
2715 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2716 struct got_tree_entry *te, struct tog_parent_trees *parents,
2717 struct got_object_id *commit_id, struct got_repository *repo)
2719 struct tog_view *log_view;
2720 const struct got_error *err = NULL;
2721 char *path;
2723 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2724 if (log_view == NULL)
2725 return got_error_from_errno();
2727 err = tree_entry_path(&path, parents, te);
2728 if (err)
2729 return err;
2731 err = open_log_view(log_view, commit_id, repo, path);
2732 if (err)
2733 view_close(log_view);
2734 else
2735 *new_view = log_view;
2736 free(path);
2737 return err;
2740 static const struct got_error *
2741 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2742 struct got_object_id *commit_id, struct got_repository *repo)
2744 const struct got_error *err = NULL;
2745 char *commit_id_str = NULL;
2746 struct tog_tree_view_state *s = &view->state.tree;
2748 TAILQ_INIT(&s->parents);
2750 err = got_object_id_str(&commit_id_str, commit_id);
2751 if (err != NULL)
2752 goto done;
2754 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2755 err = got_error_from_errno();
2756 goto done;
2759 s->root = s->tree = root;
2760 s->entries = got_object_tree_get_entries(root);
2761 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2762 s->commit_id = commit_id;
2763 s->repo = repo;
2765 view->show = show_tree_view;
2766 view->input = input_tree_view;
2767 view->close = close_tree_view;
2768 done:
2769 free(commit_id_str);
2770 if (err)
2771 free(s->tree_label);
2772 return err;
2775 static const struct got_error *
2776 close_tree_view(struct tog_view *view)
2778 struct tog_tree_view_state *s = &view->state.tree;
2780 free(s->tree_label);
2781 while (!TAILQ_EMPTY(&s->parents)) {
2782 struct tog_parent_tree *parent;
2783 parent = TAILQ_FIRST(&s->parents);
2784 TAILQ_REMOVE(&s->parents, parent, entry);
2785 free(parent);
2788 if (s->tree != s->root)
2789 got_object_tree_close(s->tree);
2790 got_object_tree_close(s->root);
2792 return NULL;
2795 static const struct got_error *
2796 show_tree_view(struct tog_view *view)
2798 const struct got_error *err = NULL;
2799 struct tog_tree_view_state *s = &view->state.tree;
2800 char *parent_path;
2802 err = tree_entry_path(&parent_path, &s->parents, NULL);
2803 if (err)
2804 return err;
2806 err = draw_tree_entries(view, &s->first_displayed_entry,
2807 &s->last_displayed_entry, &s->selected_entry,
2808 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2809 s->entries, s->selected, view->nlines, s->tree == s->root);
2810 free(parent_path);
2811 return err;
2814 static const struct got_error *
2815 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2816 struct tog_view *view, int ch)
2818 const struct got_error *err = NULL;
2819 struct tog_tree_view_state *s = &view->state.tree;
2821 switch (ch) {
2822 case 'i':
2823 s->show_ids = !s->show_ids;
2824 break;
2825 case 'l':
2826 if (s->selected_entry) {
2827 err = log_tree_entry(new_view, view,
2828 s->selected_entry, &s->parents,
2829 s->commit_id, s->repo);
2831 break;
2832 case 'k':
2833 case KEY_UP:
2834 if (s->selected > 0)
2835 s->selected--;
2836 if (s->selected > 0)
2837 break;
2838 tree_scroll_up(&s->first_displayed_entry, 1,
2839 s->entries, s->tree == s->root);
2840 break;
2841 case KEY_PPAGE:
2842 if (SIMPLEQ_FIRST(&s->entries->head) ==
2843 s->first_displayed_entry) {
2844 if (s->tree != s->root)
2845 s->first_displayed_entry = NULL;
2846 s->selected = 0;
2847 break;
2849 tree_scroll_up(&s->first_displayed_entry,
2850 view->nlines, s->entries,
2851 s->tree == s->root);
2852 break;
2853 case 'j':
2854 case KEY_DOWN:
2855 if (s->selected < s->ndisplayed - 1) {
2856 s->selected++;
2857 break;
2859 tree_scroll_down(&s->first_displayed_entry, 1,
2860 s->last_displayed_entry, s->entries);
2861 break;
2862 case KEY_NPAGE:
2863 tree_scroll_down(&s->first_displayed_entry,
2864 view->nlines, s->last_displayed_entry,
2865 s->entries);
2866 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2867 entry))
2868 break;
2869 /* can't scroll any further; move cursor down */
2870 if (s->selected < s->ndisplayed - 1)
2871 s->selected = s->ndisplayed - 1;
2872 break;
2873 case KEY_ENTER:
2874 case '\r':
2875 if (s->selected_entry == NULL) {
2876 struct tog_parent_tree *parent;
2877 case KEY_LEFT:
2878 /* user selected '..' */
2879 if (s->tree == s->root)
2880 break;
2881 parent = TAILQ_FIRST(&s->parents);
2882 TAILQ_REMOVE(&s->parents, parent,
2883 entry);
2884 got_object_tree_close(s->tree);
2885 s->tree = parent->tree;
2886 s->entries =
2887 got_object_tree_get_entries(s->tree);
2888 s->first_displayed_entry =
2889 parent->first_displayed_entry;
2890 s->selected_entry =
2891 parent->selected_entry;
2892 s->selected = parent->selected;
2893 free(parent);
2894 } else if (S_ISDIR(s->selected_entry->mode)) {
2895 struct tog_parent_tree *parent;
2896 struct got_tree_object *child;
2897 err = got_object_open_as_tree(&child,
2898 s->repo, s->selected_entry->id);
2899 if (err)
2900 break;
2901 parent = calloc(1, sizeof(*parent));
2902 if (parent == NULL) {
2903 err = got_error_from_errno();
2904 break;
2906 parent->tree = s->tree;
2907 parent->first_displayed_entry =
2908 s->first_displayed_entry;
2909 parent->selected_entry = s->selected_entry;
2910 parent->selected = s->selected;
2911 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2912 s->tree = child;
2913 s->entries =
2914 got_object_tree_get_entries(s->tree);
2915 s->selected = 0;
2916 s->first_displayed_entry = NULL;
2917 } else if (S_ISREG(s->selected_entry->mode)) {
2918 err = blame_tree_entry(new_view, view,
2919 s->selected_entry, &s->parents,
2920 s->commit_id, s->repo);
2921 if (err)
2922 break;
2924 break;
2925 case KEY_RESIZE:
2926 if (s->selected > view->nlines)
2927 s->selected = s->ndisplayed - 1;
2928 break;
2929 default:
2930 break;
2933 return err;
2936 __dead static void
2937 usage_tree(void)
2939 endwin();
2940 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2941 getprogname());
2942 exit(1);
2945 static const struct got_error *
2946 cmd_tree(int argc, char *argv[])
2948 const struct got_error *error;
2949 struct got_repository *repo = NULL;
2950 char *repo_path = NULL;
2951 struct got_object_id *commit_id = NULL;
2952 char *commit_id_arg = NULL;
2953 struct got_commit_object *commit = NULL;
2954 struct got_tree_object *tree = NULL;
2955 int ch;
2956 struct tog_view *view;
2958 #ifndef PROFILE
2959 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2960 err(1, "pledge");
2961 #endif
2963 while ((ch = getopt(argc, argv, "c:")) != -1) {
2964 switch (ch) {
2965 case 'c':
2966 commit_id_arg = optarg;
2967 break;
2968 default:
2969 usage();
2970 /* NOTREACHED */
2974 argc -= optind;
2975 argv += optind;
2977 if (argc == 0) {
2978 repo_path = getcwd(NULL, 0);
2979 if (repo_path == NULL)
2980 return got_error_from_errno();
2981 } else if (argc == 1) {
2982 repo_path = realpath(argv[0], NULL);
2983 if (repo_path == NULL)
2984 return got_error_from_errno();
2985 } else
2986 usage_log();
2988 error = got_repo_open(&repo, repo_path);
2989 free(repo_path);
2990 if (error != NULL)
2991 return error;
2993 if (commit_id_arg == NULL) {
2994 error = get_head_commit_id(&commit_id, repo);
2995 if (error != NULL)
2996 goto done;
2997 } else {
2998 struct got_object *obj;
2999 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3000 if (error == NULL) {
3001 commit_id = got_object_get_id(obj);
3002 if (commit_id == NULL)
3003 error = got_error_from_errno();
3006 if (error != NULL)
3007 goto done;
3009 error = got_object_open_as_commit(&commit, repo, commit_id);
3010 if (error != NULL)
3011 goto done;
3013 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3014 if (error != NULL)
3015 goto done;
3017 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3018 if (view == NULL) {
3019 error = got_error_from_errno();
3020 goto done;
3022 error = open_tree_view(view, tree, commit_id, repo);
3023 if (error)
3024 goto done;
3025 error = view_loop(view);
3026 done:
3027 free(commit_id);
3028 if (commit)
3029 got_object_commit_close(commit);
3030 if (tree)
3031 got_object_tree_close(tree);
3032 if (repo)
3033 got_repo_close(repo);
3034 return error;
3036 static void
3037 init_curses(void)
3039 initscr();
3040 cbreak();
3041 noecho();
3042 nonl();
3043 intrflush(stdscr, FALSE);
3044 keypad(stdscr, TRUE);
3045 curs_set(0);
3048 __dead static void
3049 usage(void)
3051 int i;
3053 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3054 "Available commands:\n", getprogname());
3055 for (i = 0; i < nitems(tog_commands); i++) {
3056 struct tog_cmd *cmd = &tog_commands[i];
3057 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3059 exit(1);
3062 static char **
3063 make_argv(const char *arg0, const char *arg1)
3065 char **argv;
3066 int argc = (arg1 == NULL ? 1 : 2);
3068 argv = calloc(argc, sizeof(char *));
3069 if (argv == NULL)
3070 err(1, "calloc");
3071 argv[0] = strdup(arg0);
3072 if (argv[0] == NULL)
3073 err(1, "calloc");
3074 if (arg1) {
3075 argv[1] = strdup(arg1);
3076 if (argv[1] == NULL)
3077 err(1, "calloc");
3080 return argv;
3083 int
3084 main(int argc, char *argv[])
3086 const struct got_error *error = NULL;
3087 struct tog_cmd *cmd = NULL;
3088 int ch, hflag = 0;
3089 char **cmd_argv = NULL;
3091 setlocale(LC_ALL, "");
3093 while ((ch = getopt(argc, argv, "h")) != -1) {
3094 switch (ch) {
3095 case 'h':
3096 hflag = 1;
3097 break;
3098 default:
3099 usage();
3100 /* NOTREACHED */
3104 argc -= optind;
3105 argv += optind;
3106 optind = 0;
3107 optreset = 1;
3109 if (argc == 0) {
3110 if (hflag)
3111 usage();
3112 /* Build an argument vector which runs a default command. */
3113 cmd = &tog_commands[0];
3114 cmd_argv = make_argv(cmd->name, NULL);
3115 argc = 1;
3116 } else {
3117 int i;
3119 /* Did the user specific a command? */
3120 for (i = 0; i < nitems(tog_commands); i++) {
3121 if (strncmp(tog_commands[i].name, argv[0],
3122 strlen(argv[0])) == 0) {
3123 cmd = &tog_commands[i];
3124 if (hflag)
3125 tog_commands[i].cmd_usage();
3126 break;
3129 if (cmd == NULL) {
3130 /* Did the user specify a repository? */
3131 char *repo_path = realpath(argv[0], NULL);
3132 if (repo_path) {
3133 struct got_repository *repo;
3134 error = got_repo_open(&repo, repo_path);
3135 if (error == NULL)
3136 got_repo_close(repo);
3137 } else
3138 error = got_error_from_errno();
3139 if (error) {
3140 if (hflag) {
3141 fprintf(stderr, "%s: '%s' is not a "
3142 "known command\n", getprogname(),
3143 argv[0]);
3144 usage();
3146 fprintf(stderr, "%s: '%s' is neither a known "
3147 "command nor a path to a repository\n",
3148 getprogname(), argv[0]);
3149 free(repo_path);
3150 return 1;
3152 cmd = &tog_commands[0];
3153 cmd_argv = make_argv(cmd->name, repo_path);
3154 argc = 2;
3155 free(repo_path);
3159 init_curses();
3161 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3162 if (error)
3163 goto done;
3164 done:
3165 endwin();
3166 free(cmd_argv);
3167 if (error)
3168 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3169 return 0;