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 void
439 view_vborder(struct tog_view *view)
441 if (view->child == NULL)
442 return;
444 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
445 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
448 static const struct got_error *
449 view_loop(struct tog_view *view)
451 const struct got_error *err = NULL;
452 struct tog_view_list_head views;
453 struct tog_view *new_view, *dead_view;
454 int done = 0;
456 TAILQ_INIT(&views);
457 TAILQ_INSERT_HEAD(&views, view, entry);
459 while (!TAILQ_EMPTY(&views) && !done) {
460 err = view_show(view);
461 if (err)
462 break;
463 err = view_input(&new_view, &dead_view, &view, &done,
464 view, &views);
465 if (err)
466 break;
467 if (dead_view) {
468 TAILQ_REMOVE(&views, dead_view, entry);
469 TAILQ_FOREACH(view, &views, entry) {
470 if (view->parent == dead_view)
471 view->parent = NULL;
473 if (dead_view->parent)
474 view = dead_view->parent;
475 else
476 view = TAILQ_LAST(&views, tog_view_list_head);
477 if (dead_view->child) {
478 TAILQ_REMOVE(&views, dead_view->child, entry);
479 err = view_close(dead_view->child);
480 if (err)
481 goto done;
483 err = view_close(dead_view);
484 if (err)
485 goto done;
487 if (new_view) {
488 /* TODO: de-duplicate! */
489 TAILQ_INSERT_TAIL(&views, new_view, entry);
490 if (new_view->parent) {
491 err = view_set_child(new_view->parent, new_view);
492 if (err)
493 goto done;
495 view = new_view;
498 done:
499 while (!TAILQ_EMPTY(&views)) {
500 view = TAILQ_FIRST(&views);
501 TAILQ_REMOVE(&views, view, entry);
502 view_close(view);
504 return err;
507 __dead static void
508 usage_log(void)
510 endwin();
511 fprintf(stderr,
512 "usage: %s log [-c commit] [-r repository-path] [path]\n",
513 getprogname());
514 exit(1);
517 /* Create newly allocated wide-character string equivalent to a byte string. */
518 static const struct got_error *
519 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
521 char *vis = NULL;
522 const struct got_error *err = NULL;
524 *ws = NULL;
525 *wlen = mbstowcs(NULL, s, 0);
526 if (*wlen == (size_t)-1) {
527 int vislen;
528 if (errno != EILSEQ)
529 return got_error_from_errno();
531 /* byte string invalid in current encoding; try to "fix" it */
532 err = got_mbsavis(&vis, &vislen, s);
533 if (err)
534 return err;
535 *wlen = mbstowcs(NULL, vis, 0);
536 if (*wlen == (size_t)-1) {
537 err = got_error_from_errno(); /* give up */
538 goto done;
542 *ws = calloc(*wlen + 1, sizeof(*ws));
543 if (*ws == NULL) {
544 err = got_error_from_errno();
545 goto done;
548 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
549 err = got_error_from_errno();
550 done:
551 free(vis);
552 if (err) {
553 free(*ws);
554 *ws = NULL;
555 *wlen = 0;
557 return err;
560 /* Format a line for display, ensuring that it won't overflow a width limit. */
561 static const struct got_error *
562 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
564 const struct got_error *err = NULL;
565 int cols = 0;
566 wchar_t *wline = NULL;
567 size_t wlen;
568 int i;
570 *wlinep = NULL;
571 *widthp = 0;
573 err = mbs2ws(&wline, &wlen, line);
574 if (err)
575 return err;
577 i = 0;
578 while (i < wlen && cols < wlimit) {
579 int width = wcwidth(wline[i]);
580 switch (width) {
581 case 0:
582 i++;
583 break;
584 case 1:
585 case 2:
586 if (cols + width <= wlimit) {
587 cols += width;
588 i++;
590 break;
591 case -1:
592 if (wline[i] == L'\t')
593 cols += TABSIZE - ((cols + 1) % TABSIZE);
594 i++;
595 break;
596 default:
597 err = got_error_from_errno();
598 goto done;
601 wline[i] = L'\0';
602 if (widthp)
603 *widthp = cols;
604 done:
605 if (err)
606 free(wline);
607 else
608 *wlinep = wline;
609 return err;
612 static const struct got_error *
613 draw_commit(struct tog_view *view, struct got_commit_object *commit,
614 struct got_object_id *id)
616 const struct got_error *err = NULL;
617 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
618 char *logmsg0 = NULL, *logmsg = NULL;
619 char *author0 = NULL, *author = NULL;
620 wchar_t *wlogmsg = NULL, *wauthor = NULL;
621 int author_width, logmsg_width;
622 char *newline, *smallerthan;
623 char *line = NULL;
624 int col, limit;
625 static const size_t date_display_cols = 9;
626 static const size_t author_display_cols = 16;
627 const int avail = view->ncols;
629 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
630 &commit->tm_committer) >= sizeof(datebuf))
631 return got_error(GOT_ERR_NO_SPACE);
633 if (avail < date_display_cols)
634 limit = MIN(sizeof(datebuf) - 1, avail);
635 else
636 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
637 waddnstr(view->window, datebuf, limit);
638 col = limit + 1;
639 if (col > avail)
640 goto done;
642 author0 = strdup(commit->author);
643 if (author0 == NULL) {
644 err = got_error_from_errno();
645 goto done;
647 author = author0;
648 smallerthan = strchr(author, '<');
649 if (smallerthan)
650 *smallerthan = '\0';
651 else {
652 char *at = strchr(author, '@');
653 if (at)
654 *at = '\0';
656 limit = avail - col;
657 err = format_line(&wauthor, &author_width, author, limit);
658 if (err)
659 goto done;
660 waddwstr(view->window, wauthor);
661 col += author_width;
662 while (col <= avail && author_width < author_display_cols + 1) {
663 waddch(view->window, ' ');
664 col++;
665 author_width++;
667 if (col > avail)
668 goto done;
670 logmsg0 = strdup(commit->logmsg);
671 if (logmsg0 == NULL) {
672 err = got_error_from_errno();
673 goto done;
675 logmsg = logmsg0;
676 while (*logmsg == '\n')
677 logmsg++;
678 newline = strchr(logmsg, '\n');
679 if (newline)
680 *newline = '\0';
681 limit = avail - col;
682 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
683 if (err)
684 goto done;
685 waddwstr(view->window, wlogmsg);
686 col += logmsg_width;
687 while (col <= avail) {
688 waddch(view->window, ' ');
689 col++;
691 done:
692 free(logmsg0);
693 free(wlogmsg);
694 free(author0);
695 free(wauthor);
696 free(line);
697 return err;
700 static struct commit_queue_entry *
701 alloc_commit_queue_entry(struct got_commit_object *commit,
702 struct got_object_id *id)
704 struct commit_queue_entry *entry;
706 entry = calloc(1, sizeof(*entry));
707 if (entry == NULL)
708 return NULL;
710 entry->id = id;
711 entry->commit = commit;
712 return entry;
715 static void
716 pop_commit(struct commit_queue *commits)
718 struct commit_queue_entry *entry;
720 entry = TAILQ_FIRST(&commits->head);
721 TAILQ_REMOVE(&commits->head, entry, entry);
722 got_object_commit_close(entry->commit);
723 commits->ncommits--;
724 /* Don't free entry->id! It is owned by the commit graph. */
725 free(entry);
728 static void
729 free_commits(struct commit_queue *commits)
731 while (!TAILQ_EMPTY(&commits->head))
732 pop_commit(commits);
735 static const struct got_error *
736 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
737 struct got_object_id *start_id, int minqueue, int init,
738 struct got_repository *repo, const char *path)
740 const struct got_error *err = NULL;
741 struct got_object_id *id;
742 struct commit_queue_entry *entry;
743 int nfetched, nqueued = 0, found_obj = 0;
744 int is_root_path = strcmp(path, "/") == 0;
746 err = got_commit_graph_iter_start(graph, start_id);
747 if (err)
748 return err;
750 entry = TAILQ_LAST(&commits->head, commit_queue_head);
751 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
752 int nfetched;
754 /* Start ID's commit is already on the queue; skip over it. */
755 err = got_commit_graph_iter_next(&id, graph);
756 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
757 return err;
759 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
760 if (err)
761 return err;
764 while (1) {
765 struct got_commit_object *commit;
767 err = got_commit_graph_iter_next(&id, graph);
768 if (err) {
769 if (err->code != GOT_ERR_ITER_NEED_MORE)
770 break;
771 if (nqueued >= minqueue) {
772 err = NULL;
773 break;
775 err = got_commit_graph_fetch_commits(&nfetched,
776 graph, 1, repo);
777 if (err)
778 return err;
779 continue;
781 if (id == NULL)
782 break;
784 err = got_object_open_as_commit(&commit, repo, id);
785 if (err)
786 break;
788 if (!is_root_path) {
789 struct got_object *obj;
790 struct got_object_qid *pid;
791 int changed = 0;
793 err = got_object_open_by_path(&obj, repo, id, path);
794 if (err) {
795 got_object_commit_close(commit);
796 if (err->code == GOT_ERR_NO_OBJ &&
797 (found_obj || !init)) {
798 /* History stops here. */
799 err = got_error(GOT_ERR_ITER_COMPLETED);
801 break;
803 found_obj = 1;
805 pid = SIMPLEQ_FIRST(&commit->parent_ids);
806 if (pid != NULL) {
807 struct got_object *pobj;
808 err = got_object_open_by_path(&pobj, repo,
809 pid->id, path);
810 if (err) {
811 if (err->code != GOT_ERR_NO_OBJ) {
812 got_object_close(obj);
813 got_object_commit_close(commit);
814 break;
816 err = NULL;
817 changed = 1;
818 } else {
819 struct got_object_id *id, *pid;
820 id = got_object_get_id(obj);
821 if (id == NULL) {
822 err = got_error_from_errno();
823 got_object_close(obj);
824 got_object_close(pobj);
825 break;
827 pid = got_object_get_id(pobj);
828 if (pid == NULL) {
829 err = got_error_from_errno();
830 free(id);
831 got_object_close(obj);
832 got_object_close(pobj);
833 break;
835 changed =
836 (got_object_id_cmp(id, pid) != 0);
837 got_object_close(pobj);
838 free(id);
839 free(pid);
842 got_object_close(obj);
843 if (!changed) {
844 got_object_commit_close(commit);
845 continue;
849 entry = alloc_commit_queue_entry(commit, id);
850 if (entry == NULL) {
851 err = got_error_from_errno();
852 break;
854 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
855 nqueued++;
856 commits->ncommits++;
859 return err;
862 static const struct got_error *
863 fetch_next_commit(struct commit_queue_entry **pentry,
864 struct commit_queue_entry *entry, struct commit_queue *commits,
865 struct got_commit_graph *graph, struct got_repository *repo,
866 const char *path)
868 const struct got_error *err = NULL;
870 *pentry = NULL;
872 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
873 if (err)
874 return err;
876 /* Next entry to display should now be available. */
877 *pentry = TAILQ_NEXT(entry, entry);
878 if (*pentry == NULL)
879 return got_error(GOT_ERR_NO_OBJ);
881 return NULL;
884 static const struct got_error *
885 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
887 const struct got_error *err = NULL;
888 struct got_reference *head_ref;
890 *head_id = NULL;
892 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
893 if (err)
894 return err;
896 err = got_ref_resolve(head_id, repo, head_ref);
897 got_ref_close(head_ref);
898 if (err) {
899 *head_id = NULL;
900 return err;
903 return NULL;
906 static const struct got_error *
907 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
908 struct commit_queue_entry **selected, struct commit_queue_entry *first,
909 struct commit_queue *commits, int selected_idx, int limit,
910 struct got_commit_graph *graph, struct got_repository *repo,
911 const char *path)
913 const struct got_error *err = NULL;
914 struct commit_queue_entry *entry;
915 int ncommits, width;
916 char *id_str, *header;
917 wchar_t *wline;
919 entry = first;
920 ncommits = 0;
921 while (entry) {
922 if (ncommits == selected_idx) {
923 *selected = entry;
924 break;
926 entry = TAILQ_NEXT(entry, entry);
927 ncommits++;
930 err = got_object_id_str(&id_str, (*selected)->id);
931 if (err)
932 return err;
934 if (path && strcmp(path, "/") != 0) {
935 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
936 err = got_error_from_errno();
937 free(id_str);
938 return err;
940 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
941 err = got_error_from_errno();
942 free(id_str);
943 return err;
945 free(id_str);
946 err = format_line(&wline, &width, header, view->ncols);
947 if (err) {
948 free(header);
949 return err;
951 free(header);
953 werase(view->window);
955 waddwstr(view->window, wline);
956 if (width < view->ncols)
957 waddch(view->window, '\n');
958 free(wline);
959 if (limit <= 1)
960 return NULL;
962 entry = first;
963 *last = first;
964 ncommits = 0;
965 while (entry) {
966 if (ncommits >= limit - 1)
967 break;
968 if (ncommits == selected_idx)
969 wstandout(view->window);
970 err = draw_commit(view, entry->commit, entry->id);
971 if (ncommits == selected_idx)
972 wstandend(view->window);
973 if (err)
974 break;
975 ncommits++;
976 *last = entry;
977 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
978 err = queue_commits(graph, commits, entry->id, 1,
979 0, repo, path);
980 if (err) {
981 if (err->code != GOT_ERR_ITER_COMPLETED)
982 return err;
983 err = NULL;
986 entry = TAILQ_NEXT(entry, entry);
989 view_vborder(view);
991 return err;
994 static void
995 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
996 struct commit_queue *commits)
998 struct commit_queue_entry *entry;
999 int nscrolled = 0;
1001 entry = TAILQ_FIRST(&commits->head);
1002 if (*first_displayed_entry == entry)
1003 return;
1005 entry = *first_displayed_entry;
1006 while (entry && nscrolled < maxscroll) {
1007 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1008 if (entry) {
1009 *first_displayed_entry = entry;
1010 nscrolled++;
1015 static const struct got_error *
1016 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1017 struct commit_queue_entry *last_displayed_entry,
1018 struct commit_queue *commits, struct got_commit_graph *graph,
1019 struct got_repository *repo, const char *path)
1021 const struct got_error *err = NULL;
1022 struct commit_queue_entry *pentry;
1023 int nscrolled = 0;
1025 do {
1026 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1027 if (pentry == NULL) {
1028 err = fetch_next_commit(&pentry, last_displayed_entry,
1029 commits, graph, repo, path);
1030 if (err || pentry == NULL)
1031 break;
1033 last_displayed_entry = pentry;
1035 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1036 if (pentry == NULL)
1037 break;
1038 *first_displayed_entry = pentry;
1039 } while (++nscrolled < maxscroll);
1041 return err;
1044 static const struct got_error *
1045 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1046 struct commit_queue_entry *entry, struct got_repository *repo)
1048 const struct got_error *err;
1049 struct got_object *obj1 = NULL, *obj2 = NULL;
1050 struct got_object_qid *parent_id;
1051 struct tog_view *diff_view;
1053 err = got_object_open(&obj2, repo, entry->id);
1054 if (err)
1055 return err;
1057 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1058 if (parent_id) {
1059 err = got_object_open(&obj1, repo, parent_id->id);
1060 if (err)
1061 goto done;
1064 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1065 if (diff_view == NULL) {
1066 err = got_error_from_errno();
1067 goto done;
1070 err = open_diff_view(diff_view, obj1, obj2, repo);
1071 if (err == NULL)
1072 *new_view = diff_view;
1073 done:
1074 if (obj1)
1075 got_object_close(obj1);
1076 if (obj2)
1077 got_object_close(obj2);
1078 return err;
1081 static const struct got_error *
1082 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1083 struct commit_queue_entry *entry, struct got_repository *repo)
1085 const struct got_error *err = NULL;
1086 struct got_tree_object *tree;
1087 struct tog_view *tree_view;
1089 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1090 if (err)
1091 return err;
1093 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1094 if (tree_view == NULL)
1095 return got_error_from_errno();
1097 err = open_tree_view(tree_view, tree, entry->id, repo);
1098 if (err)
1099 got_object_tree_close(tree);
1100 else
1101 *new_view = tree_view;
1102 return err;
1105 static const struct got_error *
1106 set_child_log_view(struct tog_view *view, struct tog_view *child)
1108 struct tog_log_view_state *s = &view->state.log;
1109 struct tog_diff_view_state *ds;
1110 struct commit_queue_entry *commit, *child_entry = NULL;
1111 int selected_idx = 0;
1113 if (child->type != TOG_VIEW_DIFF)
1114 return NULL;
1115 ds = &child->state.diff;
1117 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1118 if (got_object_id_cmp(commit->id, ds->id) == 0) {
1119 child_entry = commit;
1120 break;
1123 if (child_entry == NULL)
1124 return NULL;
1126 commit = s->first_displayed_entry;
1127 while (commit) {
1128 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1129 s->selected_entry = child_entry;
1130 s->selected = selected_idx;
1131 break;
1133 if (commit == s->last_displayed_entry)
1134 break;
1135 selected_idx++;
1136 commit = TAILQ_NEXT(commit, entry);
1139 return show_log_view(view);
1142 static const struct got_error *
1143 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1144 struct got_repository *repo, const char *path)
1146 const struct got_error *err = NULL;
1147 struct got_object_id *head_id = NULL;
1148 int nfetched;
1149 struct tog_log_view_state *s = &view->state.log;
1151 err = got_repo_map_path(&s->in_repo_path, repo, path);
1152 if (err != NULL)
1153 goto done;
1155 err = get_head_commit_id(&head_id, repo);
1156 if (err)
1157 return err;
1159 /* The graph contains all commits. */
1160 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1161 if (err)
1162 goto done;
1163 /* The commit queue contains a subset of commits filtered by path. */
1164 TAILQ_INIT(&s->commits.head);
1165 s->commits.ncommits = 0;
1167 /* Populate commit graph with a sufficient number of commits. */
1168 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1169 start_id, repo);
1170 if (err)
1171 goto done;
1174 * Open the initial batch of commits, sorted in commit graph order.
1175 * We keep all commits open throughout the lifetime of the log view
1176 * in order to avoid having to re-fetch commits from disk while
1177 * updating the display.
1179 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1180 repo, s->in_repo_path);
1181 if (err) {
1182 if (err->code != GOT_ERR_ITER_COMPLETED)
1183 goto done;
1184 err = NULL;
1187 s->first_displayed_entry =
1188 TAILQ_FIRST(&s->commits.head);
1189 s->selected_entry = s->first_displayed_entry;
1190 s->repo = repo;
1192 view->show = show_log_view;
1193 view->input = input_log_view;
1194 view->close = close_log_view;
1195 view->set_child = set_child_log_view;
1196 done:
1197 free(head_id);
1198 return err;
1201 static const struct got_error *
1202 close_log_view(struct tog_view *view)
1204 struct tog_log_view_state *s = &view->state.log;
1206 if (s->graph)
1207 got_commit_graph_close(s->graph);
1208 free_commits(&s->commits);
1209 free(s->in_repo_path);
1210 return NULL;
1213 static const struct got_error *
1214 update_diff_child_view(struct tog_view *parent,
1215 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1217 const struct got_error *err = NULL;
1218 struct tog_diff_view_state *ds;
1219 struct got_object *obj1 = NULL, *obj2 = NULL;
1220 struct got_object_qid *parent_id;
1221 struct tog_view *child_view = parent->child;
1223 if (child_view == NULL)
1224 return NULL;
1225 if (child_view->type != TOG_VIEW_DIFF)
1226 return NULL;
1227 ds = &child_view->state.diff;
1228 if (got_object_id_cmp(ds->id, selected_entry->id) == 0)
1229 return NULL;
1231 err = got_object_open(&obj2, repo, selected_entry->id);
1232 if (err)
1233 return err;
1235 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1236 if (parent_id) {
1237 err = got_object_open(&obj1, repo, parent_id->id);
1238 if (err)
1239 goto done;
1242 err = close_diff_view(child_view);
1243 if (err)
1244 goto done;
1246 err = open_diff_view(child_view, obj1, obj2, repo);
1247 if (err)
1248 goto done;
1249 done:
1250 if (obj1)
1251 got_object_close(obj1);
1252 if (obj2)
1253 got_object_close(obj2);
1254 return err;
1257 static const struct got_error *
1258 show_log_view(struct tog_view *view)
1260 const struct got_error *err = NULL;
1261 struct tog_log_view_state *s = &view->state.log;
1263 err = draw_commits(view, &s->last_displayed_entry,
1264 &s->selected_entry, s->first_displayed_entry,
1265 &s->commits, s->selected, view->nlines, s->graph,
1266 s->repo, s->in_repo_path);
1267 if (err)
1268 return err;
1270 return update_diff_child_view(view, s->selected_entry, s->repo);
1273 static const struct got_error *
1274 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1275 struct tog_view *view, int ch)
1277 const struct got_error *err = NULL;
1278 struct tog_log_view_state *s = &view->state.log;
1280 switch (ch) {
1281 case 'k':
1282 case KEY_UP:
1283 case '[':
1284 if (s->selected > 0)
1285 s->selected--;
1286 if (s->selected > 0)
1287 break;
1288 scroll_up(&s->first_displayed_entry, 1,
1289 &s->commits);
1290 break;
1291 case KEY_PPAGE:
1292 if (TAILQ_FIRST(&s->commits.head) ==
1293 s->first_displayed_entry) {
1294 s->selected = 0;
1295 break;
1297 scroll_up(&s->first_displayed_entry,
1298 view->nlines, &s->commits);
1299 break;
1300 case 'j':
1301 case KEY_DOWN:
1302 case ']':
1303 if (s->selected < MIN(view->nlines - 2,
1304 s->commits.ncommits - 1)) {
1305 s->selected++;
1306 break;
1308 err = scroll_down(&s->first_displayed_entry, 1,
1309 s->last_displayed_entry, &s->commits,
1310 s->graph, s->repo, s->in_repo_path);
1311 if (err) {
1312 if (err->code != GOT_ERR_ITER_COMPLETED)
1313 break;
1314 err = NULL;
1316 break;
1317 case KEY_NPAGE: {
1318 struct commit_queue_entry *first;
1319 first = s->first_displayed_entry;
1320 err = scroll_down(&s->first_displayed_entry,
1321 view->nlines, s->last_displayed_entry,
1322 &s->commits, s->graph, s->repo,
1323 s->in_repo_path);
1324 if (err == NULL)
1325 break;
1326 if (err->code != GOT_ERR_ITER_COMPLETED)
1327 break;
1328 if (first == s->first_displayed_entry &&
1329 s->selected < MIN(view->nlines - 2,
1330 s->commits.ncommits - 1)) {
1331 /* can't scroll further down */
1332 s->selected = MIN(view->nlines - 2,
1333 s->commits.ncommits - 1);
1335 err = NULL;
1336 break;
1338 case KEY_RESIZE:
1339 if (s->selected > view->nlines - 2)
1340 s->selected = view->nlines - 2;
1341 if (s->selected > s->commits.ncommits - 1)
1342 s->selected = s->commits.ncommits - 1;
1343 break;
1344 case KEY_ENTER:
1345 case '\r':
1346 err = show_commit(new_view, view, s->selected_entry,
1347 s->repo);
1348 break;
1349 case 't':
1350 err = browse_commit(new_view, view, s->selected_entry,
1351 s->repo);
1352 break;
1353 default:
1354 break;
1357 return err;
1360 static const struct got_error *
1361 cmd_log(int argc, char *argv[])
1363 const struct got_error *error;
1364 struct got_repository *repo = NULL;
1365 struct got_object_id *start_id = NULL;
1366 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1367 char *start_commit = NULL;
1368 int ch;
1369 struct tog_view *view;
1371 #ifndef PROFILE
1372 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1373 err(1, "pledge");
1374 #endif
1376 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1377 switch (ch) {
1378 case 'c':
1379 start_commit = optarg;
1380 break;
1381 case 'r':
1382 repo_path = realpath(optarg, NULL);
1383 if (repo_path == NULL)
1384 err(1, "-r option");
1385 break;
1386 default:
1387 usage();
1388 /* NOTREACHED */
1392 argc -= optind;
1393 argv += optind;
1395 if (argc == 0)
1396 path = strdup("");
1397 else if (argc == 1)
1398 path = strdup(argv[0]);
1399 else
1400 usage_log();
1401 if (path == NULL)
1402 return got_error_from_errno();
1404 cwd = getcwd(NULL, 0);
1405 if (cwd == NULL) {
1406 error = got_error_from_errno();
1407 goto done;
1409 if (repo_path == NULL) {
1410 repo_path = strdup(cwd);
1411 if (repo_path == NULL) {
1412 error = got_error_from_errno();
1413 goto done;
1417 error = got_repo_open(&repo, repo_path);
1418 if (error != NULL)
1419 goto done;
1421 if (start_commit == NULL) {
1422 error = get_head_commit_id(&start_id, repo);
1423 if (error != NULL)
1424 goto done;
1425 } else {
1426 struct got_object *obj;
1427 error = got_object_open_by_id_str(&obj, repo, start_commit);
1428 if (error == NULL) {
1429 start_id = got_object_get_id(obj);
1430 if (start_id == NULL)
1431 error = got_error_from_errno();
1432 goto done;
1435 if (error != NULL)
1436 goto done;
1438 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1439 if (view == NULL) {
1440 error = got_error_from_errno();
1441 goto done;
1443 error = open_log_view(view, start_id, repo, path);
1444 if (error)
1445 goto done;
1446 error = view_loop(view);
1447 done:
1448 free(repo_path);
1449 free(cwd);
1450 free(path);
1451 free(start_id);
1452 if (repo)
1453 got_repo_close(repo);
1454 return error;
1457 __dead static void
1458 usage_diff(void)
1460 endwin();
1461 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1462 getprogname());
1463 exit(1);
1466 static char *
1467 parse_next_line(FILE *f, size_t *len)
1469 char *line;
1470 size_t linelen;
1471 size_t lineno;
1472 const char delim[3] = { '\0', '\0', '\0'};
1474 line = fparseln(f, &linelen, &lineno, delim, 0);
1475 if (len)
1476 *len = linelen;
1477 return line;
1480 static const struct got_error *
1481 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1482 int *last_displayed_line, int *eof, int max_lines)
1484 const struct got_error *err;
1485 int nlines = 0, nprinted = 0;
1486 char *line;
1487 size_t len;
1488 wchar_t *wline;
1489 int width;
1491 rewind(f);
1492 werase(view->window);
1494 *eof = 0;
1495 while (nprinted < max_lines) {
1496 line = parse_next_line(f, &len);
1497 if (line == NULL) {
1498 *eof = 1;
1499 break;
1501 if (++nlines < *first_displayed_line) {
1502 free(line);
1503 continue;
1506 err = format_line(&wline, &width, line, view->ncols);
1507 if (err) {
1508 free(line);
1509 return err;
1511 waddwstr(view->window, wline);
1512 if (width < view->ncols)
1513 waddch(view->window, '\n');
1514 if (++nprinted == 1)
1515 *first_displayed_line = nlines;
1516 free(line);
1517 free(wline);
1518 wline = NULL;
1520 *last_displayed_line = nlines;
1522 view_vborder(view);
1524 return NULL;
1527 static const struct got_error *
1528 open_diff_view(struct tog_view *view, struct got_object *obj1,
1529 struct got_object *obj2, struct got_repository *repo)
1531 const struct got_error *err;
1532 FILE *f;
1534 if (obj1 != NULL && obj2 != NULL &&
1535 got_object_get_type(obj1) != got_object_get_type(obj2))
1536 return got_error(GOT_ERR_OBJ_TYPE);
1538 f = got_opentemp();
1539 if (f == NULL)
1540 return got_error_from_errno();
1542 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1543 case GOT_OBJ_TYPE_BLOB:
1544 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1545 break;
1546 case GOT_OBJ_TYPE_TREE:
1547 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1548 break;
1549 case GOT_OBJ_TYPE_COMMIT:
1550 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1551 break;
1552 default:
1553 return got_error(GOT_ERR_OBJ_TYPE);
1556 fflush(f);
1558 view->state.diff.id = got_object_get_id(obj2);
1559 if (view->state.diff.id == NULL)
1560 return got_error_from_errno();
1561 view->state.diff.f = f;
1562 view->state.diff.first_displayed_line = 1;
1563 view->state.diff.last_displayed_line = view->nlines;
1565 view->show = show_diff_view;
1566 view->input = input_diff_view;
1567 view->close = close_diff_view;
1569 return NULL;
1572 static const struct got_error *
1573 close_diff_view(struct tog_view *view)
1575 const struct got_error *err = NULL;
1577 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1578 err = got_error_from_errno();
1579 free(view->state.diff.id);
1580 return err;
1583 static const struct got_error *
1584 show_diff_view(struct tog_view *view)
1586 struct tog_diff_view_state *s = &view->state.diff;
1588 return draw_file(view, s->f, &s->first_displayed_line,
1589 &s->last_displayed_line, &s->eof, view->nlines);
1592 static const struct got_error *
1593 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1594 struct tog_view *view, int ch)
1596 const struct got_error *err = NULL;
1597 struct tog_diff_view_state *s = &view->state.diff;
1598 int i;
1600 switch (ch) {
1601 case 'k':
1602 case KEY_UP:
1603 if (s->first_displayed_line > 1)
1604 s->first_displayed_line--;
1605 break;
1606 case KEY_PPAGE:
1607 i = 0;
1608 while (i++ < view->nlines - 1 &&
1609 s->first_displayed_line > 1)
1610 s->first_displayed_line--;
1611 break;
1612 case 'j':
1613 case KEY_DOWN:
1614 if (!s->eof)
1615 s->first_displayed_line++;
1616 break;
1617 case KEY_NPAGE:
1618 case ' ':
1619 i = 0;
1620 while (!s->eof && i++ < view->nlines - 1) {
1621 char *line;
1622 line = parse_next_line(s->f, NULL);
1623 s->first_displayed_line++;
1624 if (line == NULL)
1625 break;
1627 break;
1628 case '[':
1629 case ']': {
1630 struct tog_log_view_state *ls;
1631 struct commit_queue_entry *entry;
1632 struct tog_view *diff_view;
1634 if (view->parent == NULL)
1635 break;
1636 if (view->parent->type != TOG_VIEW_LOG)
1637 break;
1638 ls = &view->parent->state.log;
1640 if (ch == '[') {
1641 entry = TAILQ_PREV(ls->selected_entry,
1642 commit_queue_head, entry);
1643 } else {
1644 entry = TAILQ_NEXT(ls->selected_entry, entry);
1645 if (entry == NULL) {
1646 err = fetch_next_commit(&entry,
1647 ls->selected_entry,
1648 &ls->commits, ls->graph,
1649 ls->repo, ls->in_repo_path);
1650 if (err)
1651 break;
1654 if (entry == NULL)
1655 break;
1656 err = show_commit(&diff_view, view->parent,
1657 entry, ls->repo);
1658 if (err)
1659 break;
1660 *new_view = diff_view;
1661 *dead_view = view;
1662 break;
1664 default:
1665 break;
1668 return err;
1671 static const struct got_error *
1672 cmd_diff(int argc, char *argv[])
1674 const struct got_error *error = NULL;
1675 struct got_repository *repo = NULL;
1676 struct got_object *obj1 = NULL, *obj2 = NULL;
1677 char *repo_path = NULL;
1678 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1679 int ch;
1680 struct tog_view *view;
1682 #ifndef PROFILE
1683 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1684 err(1, "pledge");
1685 #endif
1687 while ((ch = getopt(argc, argv, "")) != -1) {
1688 switch (ch) {
1689 default:
1690 usage();
1691 /* NOTREACHED */
1695 argc -= optind;
1696 argv += optind;
1698 if (argc == 0) {
1699 usage_diff(); /* TODO show local worktree changes */
1700 } else if (argc == 2) {
1701 repo_path = getcwd(NULL, 0);
1702 if (repo_path == NULL)
1703 return got_error_from_errno();
1704 obj_id_str1 = argv[0];
1705 obj_id_str2 = argv[1];
1706 } else if (argc == 3) {
1707 repo_path = realpath(argv[0], NULL);
1708 if (repo_path == NULL)
1709 return got_error_from_errno();
1710 obj_id_str1 = argv[1];
1711 obj_id_str2 = argv[2];
1712 } else
1713 usage_diff();
1715 error = got_repo_open(&repo, repo_path);
1716 free(repo_path);
1717 if (error)
1718 goto done;
1720 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1721 if (error)
1722 goto done;
1724 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1725 if (error)
1726 goto done;
1728 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1729 if (view == NULL) {
1730 error = got_error_from_errno();
1731 goto done;
1733 error = open_diff_view(view, obj1, obj2, repo);
1734 if (error)
1735 goto done;
1736 error = view_loop(view);
1737 done:
1738 got_repo_close(repo);
1739 if (obj1)
1740 got_object_close(obj1);
1741 if (obj2)
1742 got_object_close(obj2);
1743 return error;
1746 __dead static void
1747 usage_blame(void)
1749 endwin();
1750 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1751 getprogname());
1752 exit(1);
1755 struct tog_blame_line {
1756 int annotated;
1757 struct got_object_id *id;
1760 static const struct got_error *
1761 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1762 const char *path, struct tog_blame_line *lines, int nlines,
1763 int blame_complete, int selected_line, int *first_displayed_line,
1764 int *last_displayed_line, int *eof, int max_lines)
1766 const struct got_error *err;
1767 int lineno = 0, nprinted = 0;
1768 char *line;
1769 size_t len;
1770 wchar_t *wline;
1771 int width, wlimit;
1772 struct tog_blame_line *blame_line;
1773 struct got_object_id *prev_id = NULL;
1774 char *id_str;
1776 err = got_object_id_str(&id_str, id);
1777 if (err)
1778 return err;
1780 rewind(f);
1781 werase(view->window);
1783 if (asprintf(&line, "commit: %s", id_str) == -1) {
1784 err = got_error_from_errno();
1785 free(id_str);
1786 return err;
1789 err = format_line(&wline, &width, line, view->ncols);
1790 free(line);
1791 line = NULL;
1792 waddwstr(view->window, wline);
1793 free(wline);
1794 wline = NULL;
1795 if (width < view->ncols)
1796 waddch(view->window, '\n');
1798 if (asprintf(&line, "[%d/%d] %s%s",
1799 *first_displayed_line - 1 + selected_line, nlines,
1800 blame_complete ? "" : "annotating ", path) == -1) {
1801 free(id_str);
1802 return got_error_from_errno();
1804 free(id_str);
1805 err = format_line(&wline, &width, line, view->ncols);
1806 free(line);
1807 line = NULL;
1808 if (err)
1809 return err;
1810 waddwstr(view->window, wline);
1811 free(wline);
1812 wline = NULL;
1813 if (width < view->ncols)
1814 waddch(view->window, '\n');
1816 *eof = 0;
1817 while (nprinted < max_lines - 2) {
1818 line = parse_next_line(f, &len);
1819 if (line == NULL) {
1820 *eof = 1;
1821 break;
1823 if (++lineno < *first_displayed_line) {
1824 free(line);
1825 continue;
1828 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1829 err = format_line(&wline, &width, line, wlimit);
1830 if (err) {
1831 free(line);
1832 return err;
1835 if (nprinted == selected_line - 1)
1836 wstandout(view->window);
1838 blame_line = &lines[lineno - 1];
1839 if (blame_line->annotated && prev_id &&
1840 got_object_id_cmp(prev_id, blame_line->id) == 0)
1841 waddstr(view->window, " ");
1842 else if (blame_line->annotated) {
1843 char *id_str;
1844 err = got_object_id_str(&id_str, blame_line->id);
1845 if (err) {
1846 free(line);
1847 free(wline);
1848 return err;
1850 wprintw(view->window, "%.8s ", id_str);
1851 free(id_str);
1852 prev_id = blame_line->id;
1853 } else {
1854 waddstr(view->window, "........ ");
1855 prev_id = NULL;
1858 waddwstr(view->window, wline);
1859 while (width < wlimit) {
1860 waddch(view->window, ' ');
1861 width++;
1863 if (nprinted == selected_line - 1)
1864 wstandend(view->window);
1865 if (++nprinted == 1)
1866 *first_displayed_line = lineno;
1867 free(line);
1868 free(wline);
1869 wline = NULL;
1871 *last_displayed_line = lineno;
1873 view_vborder(view);
1875 return NULL;
1878 static const struct got_error *
1879 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1881 const struct got_error *err = NULL;
1882 struct tog_blame_cb_args *a = arg;
1883 struct tog_blame_line *line;
1885 if (nlines != a->nlines ||
1886 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1887 return got_error(GOT_ERR_RANGE);
1889 if (pthread_mutex_lock(a->mutex) != 0)
1890 return got_error_from_errno();
1892 if (*a->quit) { /* user has quit the blame view */
1893 err = got_error(GOT_ERR_ITER_COMPLETED);
1894 goto done;
1897 if (lineno == -1)
1898 goto done; /* no change in this commit */
1900 line = &a->lines[lineno - 1];
1901 if (line->annotated)
1902 goto done;
1904 line->id = got_object_id_dup(id);
1905 if (line->id == NULL) {
1906 err = got_error_from_errno();
1907 goto done;
1909 line->annotated = 1;
1911 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1912 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1913 a->last_displayed_line, a->eof, a->view->nlines);
1914 done:
1915 if (pthread_mutex_unlock(a->mutex) != 0)
1916 return got_error_from_errno();
1917 return err;
1920 static void *
1921 blame_thread(void *arg)
1923 const struct got_error *err;
1924 struct tog_blame_thread_args *ta = arg;
1925 struct tog_blame_cb_args *a = ta->cb_args;
1927 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1928 blame_cb, ta->cb_args);
1930 if (pthread_mutex_lock(a->mutex) != 0)
1931 return (void *)got_error_from_errno();
1933 got_repo_close(ta->repo);
1934 ta->repo = NULL;
1935 *ta->complete = 1;
1936 if (!err)
1937 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1938 a->lines, a->nlines, 1, *a->selected_line,
1939 a->first_displayed_line, a->last_displayed_line, a->eof,
1940 a->view->nlines);
1942 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1943 err = got_error_from_errno();
1945 return (void *)err;
1948 static struct got_object_id *
1949 get_selected_commit_id(struct tog_blame_line *lines,
1950 int first_displayed_line, int selected_line)
1952 struct tog_blame_line *line;
1954 line = &lines[first_displayed_line - 1 + selected_line - 1];
1955 if (!line->annotated)
1956 return NULL;
1958 return line->id;
1961 static const struct got_error *
1962 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1963 struct tog_blame_line *lines, int first_displayed_line,
1964 int selected_line, struct got_repository *repo)
1966 const struct got_error *err = NULL;
1967 struct got_commit_object *commit = NULL;
1968 struct got_object_id *selected_id;
1969 struct got_object_qid *pid;
1971 *pobj = NULL;
1972 *obj = NULL;
1974 selected_id = get_selected_commit_id(lines,
1975 first_displayed_line, selected_line);
1976 if (selected_id == NULL)
1977 return NULL;
1979 err = got_object_open(obj, repo, selected_id);
1980 if (err)
1981 goto done;
1983 err = got_object_commit_open(&commit, repo, *obj);
1984 if (err)
1985 goto done;
1987 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1988 if (pid) {
1989 err = got_object_open(pobj, repo, pid->id);
1990 if (err)
1991 goto done;
1993 done:
1994 if (commit)
1995 got_object_commit_close(commit);
1996 return err;
1999 static const struct got_error *
2000 stop_blame(struct tog_blame *blame)
2002 const struct got_error *err = NULL;
2003 int i;
2005 if (blame->thread) {
2006 if (pthread_join(blame->thread, (void **)&err) != 0)
2007 err = got_error_from_errno();
2008 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2009 err = NULL;
2010 blame->thread = NULL;
2012 if (blame->thread_args.repo) {
2013 got_repo_close(blame->thread_args.repo);
2014 blame->thread_args.repo = NULL;
2016 if (blame->f) {
2017 fclose(blame->f);
2018 blame->f = NULL;
2020 for (i = 0; i < blame->nlines; i++)
2021 free(blame->lines[i].id);
2022 free(blame->lines);
2023 blame->lines = NULL;
2024 free(blame->cb_args.commit_id);
2025 blame->cb_args.commit_id = NULL;
2027 return err;
2030 static const struct got_error *
2031 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2032 struct tog_view *view, int *blame_complete,
2033 int *first_displayed_line, int *last_displayed_line,
2034 int *selected_line, int *done, int *eof, const char *path,
2035 struct got_object_id *commit_id,
2036 struct got_repository *repo)
2038 const struct got_error *err = NULL;
2039 struct got_blob_object *blob = NULL;
2040 struct got_repository *thread_repo = NULL;
2041 struct got_object *obj;
2043 err = got_object_open_by_path(&obj, repo, commit_id, path);
2044 if (err)
2045 goto done;
2046 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2047 err = got_error(GOT_ERR_OBJ_TYPE);
2048 goto done;
2051 err = got_object_blob_open(&blob, repo, obj, 8192);
2052 if (err)
2053 goto done;
2054 blame->f = got_opentemp();
2055 if (blame->f == NULL) {
2056 err = got_error_from_errno();
2057 goto done;
2059 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2060 blame->f, blob);
2061 if (err)
2062 goto done;
2064 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2065 if (blame->lines == NULL) {
2066 err = got_error_from_errno();
2067 goto done;
2070 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2071 if (err)
2072 goto done;
2074 blame->cb_args.view = view;
2075 blame->cb_args.lines = blame->lines;
2076 blame->cb_args.nlines = blame->nlines;
2077 blame->cb_args.mutex = mutex;
2078 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2079 if (blame->cb_args.commit_id == NULL) {
2080 err = got_error_from_errno();
2081 goto done;
2083 blame->cb_args.f = blame->f;
2084 blame->cb_args.path = path;
2085 blame->cb_args.first_displayed_line = first_displayed_line;
2086 blame->cb_args.selected_line = selected_line;
2087 blame->cb_args.last_displayed_line = last_displayed_line;
2088 blame->cb_args.quit = done;
2089 blame->cb_args.eof = eof;
2091 blame->thread_args.path = path;
2092 blame->thread_args.repo = thread_repo;
2093 blame->thread_args.cb_args = &blame->cb_args;
2094 blame->thread_args.complete = blame_complete;
2095 *blame_complete = 0;
2097 if (pthread_create(&blame->thread, NULL, blame_thread,
2098 &blame->thread_args) != 0) {
2099 err = got_error_from_errno();
2100 goto done;
2103 done:
2104 if (blob)
2105 got_object_blob_close(blob);
2106 if (obj)
2107 got_object_close(obj);
2108 if (err)
2109 stop_blame(blame);
2110 return err;
2113 static const struct got_error *
2114 open_blame_view(struct tog_view *view, char *path,
2115 struct got_object_id *commit_id, struct got_repository *repo)
2117 const struct got_error *err = NULL;
2118 struct tog_blame_view_state *s = &view->state.blame;
2120 SIMPLEQ_INIT(&s->blamed_commits);
2122 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2123 return got_error_from_errno();
2125 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2126 if (err)
2127 return err;
2129 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2130 s->first_displayed_line = 1;
2131 s->last_displayed_line = view->nlines;
2132 s->selected_line = 1;
2133 s->blame_complete = 0;
2134 s->path = path;
2135 if (s->path == NULL)
2136 return got_error_from_errno();
2137 s->repo = repo;
2138 s->commit_id = commit_id;
2139 memset(&s->blame, 0, sizeof(s->blame));
2141 view->show = show_blame_view;
2142 view->input = input_blame_view;
2143 view->close = close_blame_view;
2145 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2146 &s->first_displayed_line, &s->last_displayed_line,
2147 &s->selected_line, &s->done, &s->eof, s->path,
2148 s->blamed_commit->id, s->repo);
2151 static const struct got_error *
2152 close_blame_view(struct tog_view *view)
2154 const struct got_error *err = NULL;
2155 struct tog_blame_view_state *s = &view->state.blame;
2157 if (s->blame.thread)
2158 err = stop_blame(&s->blame);
2160 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2161 struct got_object_qid *blamed_commit;
2162 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2163 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2164 got_object_qid_free(blamed_commit);
2167 free(s->path);
2169 return err;
2172 static const struct got_error *
2173 show_blame_view(struct tog_view *view)
2175 const struct got_error *err = NULL;
2176 struct tog_blame_view_state *s = &view->state.blame;
2178 if (pthread_mutex_lock(&s->mutex) != 0)
2179 return got_error_from_errno();
2181 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2182 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2183 s->selected_line, &s->first_displayed_line,
2184 &s->last_displayed_line, &s->eof, view->nlines);
2186 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2187 err = got_error_from_errno();
2189 return err;
2192 static const struct got_error *
2193 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2194 struct tog_view *view, int ch)
2196 const struct got_error *err = NULL, *thread_err = NULL;
2197 struct got_object *obj = NULL, *pobj = NULL;
2198 struct tog_view *diff_view;
2199 struct tog_blame_view_state *s = &view->state.blame;
2201 if (pthread_mutex_lock(&s->mutex) != 0) {
2202 err = got_error_from_errno();
2203 goto done;
2206 switch (ch) {
2207 case 'q':
2208 s->done = 1;
2209 if (pthread_mutex_unlock(&s->mutex) != 0) {
2210 err = got_error_from_errno();
2211 goto done;
2213 return stop_blame(&s->blame);
2214 case 'k':
2215 case KEY_UP:
2216 if (s->selected_line > 1)
2217 s->selected_line--;
2218 else if (s->selected_line == 1 &&
2219 s->first_displayed_line > 1)
2220 s->first_displayed_line--;
2221 break;
2222 case KEY_PPAGE:
2223 if (s->first_displayed_line == 1) {
2224 s->selected_line = 1;
2225 break;
2227 if (s->first_displayed_line > view->nlines - 2)
2228 s->first_displayed_line -=
2229 (view->nlines - 2);
2230 else
2231 s->first_displayed_line = 1;
2232 break;
2233 case 'j':
2234 case KEY_DOWN:
2235 if (s->selected_line < view->nlines - 2 &&
2236 s->first_displayed_line +
2237 s->selected_line <= s->blame.nlines)
2238 s->selected_line++;
2239 else if (s->last_displayed_line <
2240 s->blame.nlines)
2241 s->first_displayed_line++;
2242 break;
2243 case 'b':
2244 case 'p': {
2245 struct got_object_id *id;
2246 id = get_selected_commit_id(s->blame.lines,
2247 s->first_displayed_line, s->selected_line);
2248 if (id == NULL || got_object_id_cmp(id,
2249 s->blamed_commit->id) == 0)
2250 break;
2251 err = open_selected_commit(&pobj, &obj,
2252 s->blame.lines, s->first_displayed_line,
2253 s->selected_line, s->repo);
2254 if (err)
2255 break;
2256 if (pobj == NULL && obj == NULL)
2257 break;
2258 if (ch == 'p' && pobj == NULL)
2259 break;
2260 s->done = 1;
2261 if (pthread_mutex_unlock(&s->mutex) != 0) {
2262 err = got_error_from_errno();
2263 goto done;
2265 thread_err = stop_blame(&s->blame);
2266 s->done = 0;
2267 if (pthread_mutex_lock(&s->mutex) != 0) {
2268 err = got_error_from_errno();
2269 goto done;
2271 if (thread_err)
2272 break;
2273 id = got_object_get_id(ch == 'b' ? obj : pobj);
2274 got_object_close(obj);
2275 obj = NULL;
2276 if (pobj) {
2277 got_object_close(pobj);
2278 pobj = NULL;
2280 if (id == NULL) {
2281 err = got_error_from_errno();
2282 break;
2284 err = got_object_qid_alloc(
2285 &s->blamed_commit, id);
2286 free(id);
2287 if (err)
2288 goto done;
2289 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2290 s->blamed_commit, entry);
2291 err = run_blame(&s->blame, &s->mutex, view,
2292 &s->blame_complete,
2293 &s->first_displayed_line,
2294 &s->last_displayed_line,
2295 &s->selected_line, &s->done, &s->eof,
2296 s->path, s->blamed_commit->id, s->repo);
2297 if (err)
2298 break;
2299 break;
2301 case 'B': {
2302 struct got_object_qid *first;
2303 first = SIMPLEQ_FIRST(&s->blamed_commits);
2304 if (!got_object_id_cmp(first->id, s->commit_id))
2305 break;
2306 s->done = 1;
2307 if (pthread_mutex_unlock(&s->mutex) != 0) {
2308 err = got_error_from_errno();
2309 goto done;
2311 thread_err = stop_blame(&s->blame);
2312 s->done = 0;
2313 if (pthread_mutex_lock(&s->mutex) != 0) {
2314 err = got_error_from_errno();
2315 goto done;
2317 if (thread_err)
2318 break;
2319 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2320 got_object_qid_free(s->blamed_commit);
2321 s->blamed_commit =
2322 SIMPLEQ_FIRST(&s->blamed_commits);
2323 err = run_blame(&s->blame, &s->mutex, view,
2324 &s->blame_complete,
2325 &s->first_displayed_line,
2326 &s->last_displayed_line,
2327 &s->selected_line, &s->done, &s->eof, s->path,
2328 s->blamed_commit->id, s->repo);
2329 if (err)
2330 break;
2331 break;
2333 case KEY_ENTER:
2334 case '\r':
2335 err = open_selected_commit(&pobj, &obj,
2336 s->blame.lines, s->first_displayed_line,
2337 s->selected_line, s->repo);
2338 if (err)
2339 break;
2340 if (pobj == NULL && obj == NULL)
2341 break;
2342 diff_view = view_open(0, 0, 0, 0, view,
2343 TOG_VIEW_DIFF);
2344 if (diff_view == NULL) {
2345 err = got_error_from_errno();
2346 break;
2348 err = open_diff_view(diff_view, pobj, obj,
2349 s->repo);
2350 if (err) {
2351 view_close(diff_view);
2352 break;
2354 *new_view = diff_view;
2355 if (pobj) {
2356 got_object_close(pobj);
2357 pobj = NULL;
2359 got_object_close(obj);
2360 obj = NULL;
2361 if (err)
2362 break;
2363 break;
2364 case KEY_NPAGE:
2365 case ' ':
2366 if (s->last_displayed_line >= s->blame.nlines &&
2367 s->selected_line < view->nlines - 2) {
2368 s->selected_line = MIN(s->blame.nlines,
2369 view->nlines - 2);
2370 break;
2372 if (s->last_displayed_line + view->nlines - 2
2373 <= s->blame.nlines)
2374 s->first_displayed_line +=
2375 view->nlines - 2;
2376 else
2377 s->first_displayed_line =
2378 s->blame.nlines -
2379 (view->nlines - 3);
2380 break;
2381 case KEY_RESIZE:
2382 if (s->selected_line > view->nlines - 2) {
2383 s->selected_line = MIN(s->blame.nlines,
2384 view->nlines - 2);
2386 break;
2387 default:
2388 break;
2391 if (pthread_mutex_unlock(&s->mutex) != 0)
2392 err = got_error_from_errno();
2393 done:
2394 if (pobj)
2395 got_object_close(pobj);
2396 return thread_err ? thread_err : err;
2399 static const struct got_error *
2400 cmd_blame(int argc, char *argv[])
2402 const struct got_error *error;
2403 struct got_repository *repo = NULL;
2404 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2405 struct got_object_id *commit_id = NULL;
2406 char *commit_id_str = NULL;
2407 int ch;
2408 struct tog_view *view;
2410 #ifndef PROFILE
2411 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2412 err(1, "pledge");
2413 #endif
2415 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2416 switch (ch) {
2417 case 'c':
2418 commit_id_str = optarg;
2419 break;
2420 case 'r':
2421 repo_path = realpath(optarg, NULL);
2422 if (repo_path == NULL)
2423 err(1, "-r option");
2424 break;
2425 default:
2426 usage();
2427 /* NOTREACHED */
2431 argc -= optind;
2432 argv += optind;
2434 if (argc == 1)
2435 path = argv[0];
2436 else
2437 usage_blame();
2439 cwd = getcwd(NULL, 0);
2440 if (cwd == NULL) {
2441 error = got_error_from_errno();
2442 goto done;
2444 if (repo_path == NULL) {
2445 repo_path = strdup(cwd);
2446 if (repo_path == NULL) {
2447 error = got_error_from_errno();
2448 goto done;
2453 error = got_repo_open(&repo, repo_path);
2454 if (error != NULL)
2455 return error;
2457 error = got_repo_map_path(&in_repo_path, repo, path);
2458 if (error != NULL)
2459 goto done;
2461 if (commit_id_str == NULL) {
2462 struct got_reference *head_ref;
2463 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2464 if (error != NULL)
2465 goto done;
2466 error = got_ref_resolve(&commit_id, repo, head_ref);
2467 got_ref_close(head_ref);
2468 } else {
2469 struct got_object *obj;
2470 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2471 if (error != NULL)
2472 goto done;
2473 commit_id = got_object_get_id(obj);
2474 if (commit_id == NULL)
2475 error = got_error_from_errno();
2476 got_object_close(obj);
2478 if (error != NULL)
2479 goto done;
2481 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2482 if (view == NULL) {
2483 error = got_error_from_errno();
2484 goto done;
2486 error = open_blame_view(view, in_repo_path, commit_id, repo);
2487 if (error)
2488 goto done;
2489 error = view_loop(view);
2490 done:
2491 free(repo_path);
2492 free(cwd);
2493 free(commit_id);
2494 if (repo)
2495 got_repo_close(repo);
2496 return error;
2499 static const struct got_error *
2500 draw_tree_entries(struct tog_view *view,
2501 struct got_tree_entry **first_displayed_entry,
2502 struct got_tree_entry **last_displayed_entry,
2503 struct got_tree_entry **selected_entry, int *ndisplayed,
2504 const char *label, int show_ids, const char *parent_path,
2505 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2507 const struct got_error *err = NULL;
2508 struct got_tree_entry *te;
2509 wchar_t *wline;
2510 int width, n;
2512 *ndisplayed = 0;
2514 werase(view->window);
2516 if (limit == 0)
2517 return NULL;
2519 err = format_line(&wline, &width, label, view->ncols);
2520 if (err)
2521 return err;
2522 waddwstr(view->window, wline);
2523 free(wline);
2524 wline = NULL;
2525 if (width < view->ncols)
2526 waddch(view->window, '\n');
2527 if (--limit <= 0)
2528 return NULL;
2529 err = format_line(&wline, &width, parent_path, view->ncols);
2530 if (err)
2531 return err;
2532 waddwstr(view->window, wline);
2533 free(wline);
2534 wline = NULL;
2535 if (width < view->ncols)
2536 waddch(view->window, '\n');
2537 if (--limit <= 0)
2538 return NULL;
2539 waddch(view->window, '\n');
2540 if (--limit <= 0)
2541 return NULL;
2543 te = SIMPLEQ_FIRST(&entries->head);
2544 if (*first_displayed_entry == NULL) {
2545 if (selected == 0) {
2546 wstandout(view->window);
2547 *selected_entry = NULL;
2549 waddstr(view->window, " ..\n"); /* parent directory */
2550 if (selected == 0)
2551 wstandend(view->window);
2552 (*ndisplayed)++;
2553 if (--limit <= 0)
2554 return NULL;
2555 n = 1;
2556 } else {
2557 n = 0;
2558 while (te != *first_displayed_entry)
2559 te = SIMPLEQ_NEXT(te, entry);
2562 while (te) {
2563 char *line = NULL, *id_str = NULL;
2565 if (show_ids) {
2566 err = got_object_id_str(&id_str, te->id);
2567 if (err)
2568 return got_error_from_errno();
2570 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2571 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2572 free(id_str);
2573 return got_error_from_errno();
2575 free(id_str);
2576 err = format_line(&wline, &width, line, view->ncols);
2577 if (err) {
2578 free(line);
2579 break;
2581 if (n == selected) {
2582 wstandout(view->window);
2583 *selected_entry = te;
2585 waddwstr(view->window, wline);
2586 if (width < view->ncols)
2587 waddch(view->window, '\n');
2588 if (n == selected)
2589 wstandend(view->window);
2590 free(line);
2591 free(wline);
2592 wline = NULL;
2593 n++;
2594 (*ndisplayed)++;
2595 *last_displayed_entry = te;
2596 if (--limit <= 0)
2597 break;
2598 te = SIMPLEQ_NEXT(te, entry);
2601 view_vborder(view);
2602 return err;
2605 static void
2606 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2607 const struct got_tree_entries *entries, int isroot)
2609 struct got_tree_entry *te, *prev;
2610 int i;
2612 if (*first_displayed_entry == NULL)
2613 return;
2615 te = SIMPLEQ_FIRST(&entries->head);
2616 if (*first_displayed_entry == te) {
2617 if (!isroot)
2618 *first_displayed_entry = NULL;
2619 return;
2622 /* XXX this is stupid... switch to TAILQ? */
2623 for (i = 0; i < maxscroll; i++) {
2624 while (te != *first_displayed_entry) {
2625 prev = te;
2626 te = SIMPLEQ_NEXT(te, entry);
2628 *first_displayed_entry = prev;
2629 te = SIMPLEQ_FIRST(&entries->head);
2631 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2632 *first_displayed_entry = NULL;
2635 static void
2636 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2637 struct got_tree_entry *last_displayed_entry,
2638 const struct got_tree_entries *entries)
2640 struct got_tree_entry *next;
2641 int n = 0;
2643 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2644 return;
2646 if (*first_displayed_entry)
2647 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2648 else
2649 next = SIMPLEQ_FIRST(&entries->head);
2650 while (next) {
2651 *first_displayed_entry = next;
2652 if (++n >= maxscroll)
2653 break;
2654 next = SIMPLEQ_NEXT(next, entry);
2658 static const struct got_error *
2659 tree_entry_path(char **path, struct tog_parent_trees *parents,
2660 struct got_tree_entry *te)
2662 const struct got_error *err = NULL;
2663 struct tog_parent_tree *pt;
2664 size_t len = 2; /* for leading slash and NUL */
2666 TAILQ_FOREACH(pt, parents, entry)
2667 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2668 if (te)
2669 len += strlen(te->name);
2671 *path = calloc(1, len);
2672 if (path == NULL)
2673 return got_error_from_errno();
2675 (*path)[0] = '/';
2676 pt = TAILQ_LAST(parents, tog_parent_trees);
2677 while (pt) {
2678 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2679 err = got_error(GOT_ERR_NO_SPACE);
2680 goto done;
2682 if (strlcat(*path, "/", len) >= len) {
2683 err = got_error(GOT_ERR_NO_SPACE);
2684 goto done;
2686 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2688 if (te) {
2689 if (strlcat(*path, te->name, len) >= len) {
2690 err = got_error(GOT_ERR_NO_SPACE);
2691 goto done;
2694 done:
2695 if (err) {
2696 free(*path);
2697 *path = NULL;
2699 return err;
2702 static const struct got_error *
2703 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2704 struct got_tree_entry *te, struct tog_parent_trees *parents,
2705 struct got_object_id *commit_id, struct got_repository *repo)
2707 const struct got_error *err = NULL;
2708 char *path;
2709 struct tog_view *blame_view;
2711 err = tree_entry_path(&path, parents, te);
2712 if (err)
2713 return err;
2715 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2716 if (blame_view == NULL)
2717 return got_error_from_errno();
2719 err = open_blame_view(blame_view, path, commit_id, repo);
2720 if (err) {
2721 view_close(blame_view);
2722 free(path);
2723 } else
2724 *new_view = blame_view;
2725 return err;
2728 static const struct got_error *
2729 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2730 struct got_tree_entry *te, struct tog_parent_trees *parents,
2731 struct got_object_id *commit_id, struct got_repository *repo)
2733 struct tog_view *log_view;
2734 const struct got_error *err = NULL;
2735 char *path;
2737 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2738 if (log_view == NULL)
2739 return got_error_from_errno();
2741 err = tree_entry_path(&path, parents, te);
2742 if (err)
2743 return err;
2745 err = open_log_view(log_view, commit_id, repo, path);
2746 if (err)
2747 view_close(log_view);
2748 else
2749 *new_view = log_view;
2750 free(path);
2751 return err;
2754 static const struct got_error *
2755 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2756 struct got_object_id *commit_id, struct got_repository *repo)
2758 const struct got_error *err = NULL;
2759 char *commit_id_str = NULL;
2760 struct tog_tree_view_state *s = &view->state.tree;
2762 TAILQ_INIT(&s->parents);
2764 err = got_object_id_str(&commit_id_str, commit_id);
2765 if (err != NULL)
2766 goto done;
2768 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2769 err = got_error_from_errno();
2770 goto done;
2773 s->root = s->tree = root;
2774 s->entries = got_object_tree_get_entries(root);
2775 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2776 s->commit_id = commit_id;
2777 s->repo = repo;
2779 view->show = show_tree_view;
2780 view->input = input_tree_view;
2781 view->close = close_tree_view;
2782 done:
2783 free(commit_id_str);
2784 if (err)
2785 free(s->tree_label);
2786 return err;
2789 static const struct got_error *
2790 close_tree_view(struct tog_view *view)
2792 struct tog_tree_view_state *s = &view->state.tree;
2794 free(s->tree_label);
2795 while (!TAILQ_EMPTY(&s->parents)) {
2796 struct tog_parent_tree *parent;
2797 parent = TAILQ_FIRST(&s->parents);
2798 TAILQ_REMOVE(&s->parents, parent, entry);
2799 free(parent);
2802 if (s->tree != s->root)
2803 got_object_tree_close(s->tree);
2804 got_object_tree_close(s->root);
2806 return NULL;
2809 static const struct got_error *
2810 show_tree_view(struct tog_view *view)
2812 const struct got_error *err = NULL;
2813 struct tog_tree_view_state *s = &view->state.tree;
2814 char *parent_path;
2816 err = tree_entry_path(&parent_path, &s->parents, NULL);
2817 if (err)
2818 return err;
2820 err = draw_tree_entries(view, &s->first_displayed_entry,
2821 &s->last_displayed_entry, &s->selected_entry,
2822 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2823 s->entries, s->selected, view->nlines, s->tree == s->root);
2824 free(parent_path);
2825 return err;
2828 static const struct got_error *
2829 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2830 struct tog_view *view, int ch)
2832 const struct got_error *err = NULL;
2833 struct tog_tree_view_state *s = &view->state.tree;
2835 switch (ch) {
2836 case 'i':
2837 s->show_ids = !s->show_ids;
2838 break;
2839 case 'l':
2840 if (s->selected_entry) {
2841 err = log_tree_entry(new_view, view,
2842 s->selected_entry, &s->parents,
2843 s->commit_id, s->repo);
2845 break;
2846 case 'k':
2847 case KEY_UP:
2848 if (s->selected > 0)
2849 s->selected--;
2850 if (s->selected > 0)
2851 break;
2852 tree_scroll_up(&s->first_displayed_entry, 1,
2853 s->entries, s->tree == s->root);
2854 break;
2855 case KEY_PPAGE:
2856 if (SIMPLEQ_FIRST(&s->entries->head) ==
2857 s->first_displayed_entry) {
2858 if (s->tree != s->root)
2859 s->first_displayed_entry = NULL;
2860 s->selected = 0;
2861 break;
2863 tree_scroll_up(&s->first_displayed_entry,
2864 view->nlines, s->entries,
2865 s->tree == s->root);
2866 break;
2867 case 'j':
2868 case KEY_DOWN:
2869 if (s->selected < s->ndisplayed - 1) {
2870 s->selected++;
2871 break;
2873 tree_scroll_down(&s->first_displayed_entry, 1,
2874 s->last_displayed_entry, s->entries);
2875 break;
2876 case KEY_NPAGE:
2877 tree_scroll_down(&s->first_displayed_entry,
2878 view->nlines, s->last_displayed_entry,
2879 s->entries);
2880 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2881 entry))
2882 break;
2883 /* can't scroll any further; move cursor down */
2884 if (s->selected < s->ndisplayed - 1)
2885 s->selected = s->ndisplayed - 1;
2886 break;
2887 case KEY_ENTER:
2888 case '\r':
2889 if (s->selected_entry == NULL) {
2890 struct tog_parent_tree *parent;
2891 case KEY_LEFT:
2892 /* user selected '..' */
2893 if (s->tree == s->root)
2894 break;
2895 parent = TAILQ_FIRST(&s->parents);
2896 TAILQ_REMOVE(&s->parents, parent,
2897 entry);
2898 got_object_tree_close(s->tree);
2899 s->tree = parent->tree;
2900 s->entries =
2901 got_object_tree_get_entries(s->tree);
2902 s->first_displayed_entry =
2903 parent->first_displayed_entry;
2904 s->selected_entry =
2905 parent->selected_entry;
2906 s->selected = parent->selected;
2907 free(parent);
2908 } else if (S_ISDIR(s->selected_entry->mode)) {
2909 struct tog_parent_tree *parent;
2910 struct got_tree_object *child;
2911 err = got_object_open_as_tree(&child,
2912 s->repo, s->selected_entry->id);
2913 if (err)
2914 break;
2915 parent = calloc(1, sizeof(*parent));
2916 if (parent == NULL) {
2917 err = got_error_from_errno();
2918 break;
2920 parent->tree = s->tree;
2921 parent->first_displayed_entry =
2922 s->first_displayed_entry;
2923 parent->selected_entry = s->selected_entry;
2924 parent->selected = s->selected;
2925 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2926 s->tree = child;
2927 s->entries =
2928 got_object_tree_get_entries(s->tree);
2929 s->selected = 0;
2930 s->first_displayed_entry = NULL;
2931 } else if (S_ISREG(s->selected_entry->mode)) {
2932 err = blame_tree_entry(new_view, view,
2933 s->selected_entry, &s->parents,
2934 s->commit_id, s->repo);
2935 if (err)
2936 break;
2938 break;
2939 case KEY_RESIZE:
2940 if (s->selected > view->nlines)
2941 s->selected = s->ndisplayed - 1;
2942 break;
2943 default:
2944 break;
2947 return err;
2950 __dead static void
2951 usage_tree(void)
2953 endwin();
2954 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2955 getprogname());
2956 exit(1);
2959 static const struct got_error *
2960 cmd_tree(int argc, char *argv[])
2962 const struct got_error *error;
2963 struct got_repository *repo = NULL;
2964 char *repo_path = NULL;
2965 struct got_object_id *commit_id = NULL;
2966 char *commit_id_arg = NULL;
2967 struct got_commit_object *commit = NULL;
2968 struct got_tree_object *tree = NULL;
2969 int ch;
2970 struct tog_view *view;
2972 #ifndef PROFILE
2973 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2974 err(1, "pledge");
2975 #endif
2977 while ((ch = getopt(argc, argv, "c:")) != -1) {
2978 switch (ch) {
2979 case 'c':
2980 commit_id_arg = optarg;
2981 break;
2982 default:
2983 usage();
2984 /* NOTREACHED */
2988 argc -= optind;
2989 argv += optind;
2991 if (argc == 0) {
2992 repo_path = getcwd(NULL, 0);
2993 if (repo_path == NULL)
2994 return got_error_from_errno();
2995 } else if (argc == 1) {
2996 repo_path = realpath(argv[0], NULL);
2997 if (repo_path == NULL)
2998 return got_error_from_errno();
2999 } else
3000 usage_log();
3002 error = got_repo_open(&repo, repo_path);
3003 free(repo_path);
3004 if (error != NULL)
3005 return error;
3007 if (commit_id_arg == NULL) {
3008 error = get_head_commit_id(&commit_id, repo);
3009 if (error != NULL)
3010 goto done;
3011 } else {
3012 struct got_object *obj;
3013 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3014 if (error == NULL) {
3015 commit_id = got_object_get_id(obj);
3016 if (commit_id == NULL)
3017 error = got_error_from_errno();
3020 if (error != NULL)
3021 goto done;
3023 error = got_object_open_as_commit(&commit, repo, commit_id);
3024 if (error != NULL)
3025 goto done;
3027 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3028 if (error != NULL)
3029 goto done;
3031 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3032 if (view == NULL) {
3033 error = got_error_from_errno();
3034 goto done;
3036 error = open_tree_view(view, tree, commit_id, repo);
3037 if (error)
3038 goto done;
3039 error = view_loop(view);
3040 done:
3041 free(commit_id);
3042 if (commit)
3043 got_object_commit_close(commit);
3044 if (tree)
3045 got_object_tree_close(tree);
3046 if (repo)
3047 got_repo_close(repo);
3048 return error;
3050 static void
3051 init_curses(void)
3053 initscr();
3054 cbreak();
3055 noecho();
3056 nonl();
3057 intrflush(stdscr, FALSE);
3058 keypad(stdscr, TRUE);
3059 curs_set(0);
3062 __dead static void
3063 usage(void)
3065 int i;
3067 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3068 "Available commands:\n", getprogname());
3069 for (i = 0; i < nitems(tog_commands); i++) {
3070 struct tog_cmd *cmd = &tog_commands[i];
3071 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3073 exit(1);
3076 static char **
3077 make_argv(const char *arg0, const char *arg1)
3079 char **argv;
3080 int argc = (arg1 == NULL ? 1 : 2);
3082 argv = calloc(argc, sizeof(char *));
3083 if (argv == NULL)
3084 err(1, "calloc");
3085 argv[0] = strdup(arg0);
3086 if (argv[0] == NULL)
3087 err(1, "calloc");
3088 if (arg1) {
3089 argv[1] = strdup(arg1);
3090 if (argv[1] == NULL)
3091 err(1, "calloc");
3094 return argv;
3097 int
3098 main(int argc, char *argv[])
3100 const struct got_error *error = NULL;
3101 struct tog_cmd *cmd = NULL;
3102 int ch, hflag = 0;
3103 char **cmd_argv = NULL;
3105 setlocale(LC_ALL, "");
3107 while ((ch = getopt(argc, argv, "h")) != -1) {
3108 switch (ch) {
3109 case 'h':
3110 hflag = 1;
3111 break;
3112 default:
3113 usage();
3114 /* NOTREACHED */
3118 argc -= optind;
3119 argv += optind;
3120 optind = 0;
3121 optreset = 1;
3123 if (argc == 0) {
3124 if (hflag)
3125 usage();
3126 /* Build an argument vector which runs a default command. */
3127 cmd = &tog_commands[0];
3128 cmd_argv = make_argv(cmd->name, NULL);
3129 argc = 1;
3130 } else {
3131 int i;
3133 /* Did the user specific a command? */
3134 for (i = 0; i < nitems(tog_commands); i++) {
3135 if (strncmp(tog_commands[i].name, argv[0],
3136 strlen(argv[0])) == 0) {
3137 cmd = &tog_commands[i];
3138 if (hflag)
3139 tog_commands[i].cmd_usage();
3140 break;
3143 if (cmd == NULL) {
3144 /* Did the user specify a repository? */
3145 char *repo_path = realpath(argv[0], NULL);
3146 if (repo_path) {
3147 struct got_repository *repo;
3148 error = got_repo_open(&repo, repo_path);
3149 if (error == NULL)
3150 got_repo_close(repo);
3151 } else
3152 error = got_error_from_errno();
3153 if (error) {
3154 if (hflag) {
3155 fprintf(stderr, "%s: '%s' is not a "
3156 "known command\n", getprogname(),
3157 argv[0]);
3158 usage();
3160 fprintf(stderr, "%s: '%s' is neither a known "
3161 "command nor a path to a repository\n",
3162 getprogname(), argv[0]);
3163 free(repo_path);
3164 return 1;
3166 cmd = &tog_commands[0];
3167 cmd_argv = make_argv(cmd->name, repo_path);
3168 argc = 2;
3169 free(repo_path);
3173 init_curses();
3175 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3176 if (error)
3177 goto done;
3178 done:
3179 endwin();
3180 free(cmd_argv);
3181 if (error)
3182 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3183 return 0;