Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <errno.h>
21 #define _XOPEN_SOURCE_EXTENDED
22 #include <curses.h>
23 #undef _XOPEN_SOURCE_EXTENDED
24 #include <panel.h>
25 #include <locale.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <string.h>
30 #include <err.h>
31 #include <unistd.h>
32 #include <util.h>
33 #include <limits.h>
34 #include <wchar.h>
35 #include <time.h>
36 #include <pthread.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_diff.h"
43 #include "got_opentemp.h"
44 #include "got_commit_graph.h"
45 #include "got_utf8.h"
46 #include "got_blame.h"
48 #ifndef MIN
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #endif
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct tog_cmd {
57 const char *name;
58 const struct got_error *(*cmd_main)(int, char *[]);
59 void (*cmd_usage)(void);
60 const char *descr;
61 };
63 __dead static void usage(void);
64 __dead static void usage_log(void);
65 __dead static void usage_diff(void);
66 __dead static void usage_blame(void);
67 __dead static void usage_tree(void);
69 static const struct got_error* cmd_log(int, char *[]);
70 static const struct got_error* cmd_diff(int, char *[]);
71 static const struct got_error* cmd_blame(int, char *[]);
72 static const struct got_error* cmd_tree(int, char *[]);
74 static struct tog_cmd tog_commands[] = {
75 { "log", cmd_log, usage_log,
76 "show repository history" },
77 { "diff", cmd_diff, usage_diff,
78 "compare files and directories" },
79 { "blame", cmd_blame, usage_blame,
80 "show line-by-line file history" },
81 { "tree", cmd_tree, usage_tree,
82 "browse trees in repository" },
83 };
85 enum tog_view_type {
86 TOG_VIEW_DIFF,
87 TOG_VIEW_LOG,
88 TOG_VIEW_BLAME,
89 TOG_VIEW_TREE
90 };
92 struct tog_diff_view_state {
93 struct got_object_id *id1, *id2;
94 FILE *f;
95 int first_displayed_line;
96 int last_displayed_line;
97 int eof;
98 };
100 struct commit_queue_entry {
101 TAILQ_ENTRY(commit_queue_entry) entry;
102 struct got_object_id *id;
103 struct got_commit_object *commit;
104 };
105 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 struct commit_queue {
107 int ncommits;
108 struct commit_queue_head head;
109 };
111 struct tog_log_view_state {
112 struct got_commit_graph *graph;
113 struct commit_queue commits;
114 struct commit_queue_entry *first_displayed_entry;
115 struct commit_queue_entry *last_displayed_entry;
116 struct commit_queue_entry *selected_entry;
117 int selected;
118 char *in_repo_path;
119 struct got_repository *repo;
120 };
122 struct tog_blame_cb_args {
123 pthread_mutex_t *mutex;
124 struct tog_blame_line *lines; /* one per line */
125 int nlines;
127 struct tog_view *view;
128 struct got_object_id *commit_id;
129 FILE *f;
130 const char *path;
131 int *first_displayed_line;
132 int *last_displayed_line;
133 int *selected_line;
134 int *quit;
135 int *eof;
136 };
138 struct tog_blame_thread_args {
139 const char *path;
140 struct got_repository *repo;
141 struct tog_blame_cb_args *cb_args;
142 int *complete;
143 };
145 struct tog_blame {
146 FILE *f;
147 size_t filesize;
148 struct tog_blame_line *lines;
149 size_t nlines;
150 pthread_t thread;
151 struct tog_blame_thread_args thread_args;
152 struct tog_blame_cb_args cb_args;
153 const char *path;
154 };
156 struct tog_blame_view_state {
157 int first_displayed_line;
158 int last_displayed_line;
159 int selected_line;
160 int blame_complete;
161 int eof;
162 int done;
163 pthread_mutex_t mutex;
164 struct got_object_id_queue blamed_commits;
165 struct got_object_qid *blamed_commit;
166 char *path;
167 struct got_repository *repo;
168 struct got_object_id *commit_id;
169 struct tog_blame blame;
170 };
172 struct tog_parent_tree {
173 TAILQ_ENTRY(tog_parent_tree) entry;
174 struct got_tree_object *tree;
175 struct got_tree_entry *first_displayed_entry;
176 struct got_tree_entry *selected_entry;
177 int selected;
178 };
180 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 struct tog_tree_view_state {
183 char *tree_label;
184 struct got_tree_object *root;
185 struct got_tree_object *tree;
186 const struct got_tree_entries *entries;
187 struct got_tree_entry *first_displayed_entry;
188 struct got_tree_entry *last_displayed_entry;
189 struct got_tree_entry *selected_entry;
190 int nentries, ndisplayed, selected, show_ids;
191 struct tog_parent_trees parents;
192 struct got_object_id *commit_id;
193 struct got_repository *repo;
194 };
196 TAILQ_HEAD(tog_view_list_head, tog_view);
197 struct tog_view {
198 TAILQ_ENTRY(tog_view) entry;
199 WINDOW *window;
200 PANEL *panel;
201 int nlines, ncols, begin_y, begin_x;
202 int lines, cols; /* copies of LINES and COLS */
203 int focussed;
204 struct tog_view *parent;
205 struct tog_view *child;
207 /* type-specific state */
208 enum tog_view_type type;
209 union {
210 struct tog_diff_view_state diff;
211 struct tog_log_view_state log;
212 struct tog_blame_view_state blame;
213 struct tog_tree_view_state tree;
214 } state;
216 const struct got_error *(*show)(struct tog_view *);
217 const struct got_error *(*input)(struct tog_view **,
218 struct tog_view **, struct tog_view *, int);
219 const struct got_error *(*set_child)(struct tog_view *,
220 struct tog_view *);
221 const struct got_error *(*close)(struct tog_view *);
222 };
224 static const struct got_error *open_diff_view(struct tog_view *,
225 struct got_object *, struct got_object *, struct got_repository *);
226 static const struct got_error *show_diff_view(struct tog_view *);
227 static const struct got_error *input_diff_view(struct tog_view **,
228 struct tog_view **, struct tog_view *, int);
229 static const struct got_error* close_diff_view(struct tog_view *);
231 static const struct got_error *open_log_view(struct tog_view *,
232 struct got_object_id *, struct got_repository *, const char *);
233 static const struct got_error * show_log_view(struct tog_view *);
234 static const struct got_error *input_log_view(struct tog_view **,
235 struct tog_view **, struct tog_view *, int);
236 static const struct got_error *close_log_view(struct tog_view *);
237 static const struct got_error* set_child_log_view(struct tog_view *,
238 struct tog_view *);
240 static const struct got_error *open_blame_view(struct tog_view *, char *,
241 struct got_object_id *, struct got_repository *);
242 static const struct got_error *show_blame_view(struct tog_view *);
243 static const struct got_error *input_blame_view(struct tog_view **,
244 struct tog_view **, struct tog_view *, int);
245 static const struct got_error *close_blame_view(struct tog_view *);
247 static const struct got_error *open_tree_view(struct tog_view *,
248 struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 static const struct got_error *show_tree_view(struct tog_view *);
250 static const struct got_error *input_tree_view(struct tog_view **,
251 struct tog_view **, struct tog_view *, int);
252 static const struct got_error *close_tree_view(struct tog_view *);
254 static const struct got_error *
255 view_close(struct tog_view *view)
257 const struct got_error *err = NULL;
259 if (view->child)
260 view->child->parent = NULL;
261 if (view->parent)
262 view->parent->child = NULL;
263 if (view->close)
264 err = view->close(view);
265 if (view->panel)
266 del_panel(view->panel);
267 if (view->window)
268 delwin(view->window);
269 free(view);
270 return err;
273 static struct tog_view *
274 view_open(int nlines, int ncols, int begin_y, int begin_x,
275 struct tog_view *parent, enum tog_view_type type)
277 struct tog_view *view = calloc(1, sizeof(*view));
279 if (view == NULL)
280 return NULL;
282 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 begin_x = parent->ncols - 80;
285 view->parent = parent;
286 if (parent)
287 parent->child = view;
288 view->type = type;
289 view->lines = LINES;
290 view->cols = COLS;
291 view->nlines = nlines ? nlines : LINES - begin_y;
292 view->ncols = ncols ? ncols : COLS - begin_x;
293 view->begin_y = begin_y;
294 view->begin_x = begin_x;
295 view->window = newwin(nlines, ncols, begin_y, begin_x);
296 if (view->window == NULL) {
297 view_close(view);
298 return NULL;
300 view->panel = new_panel(view->window);
301 if (view->panel == NULL) {
302 view_close(view);
303 return NULL;
306 keypad(view->window, TRUE);
307 return view;
310 static const struct got_error *
311 view_show(struct tog_view *view)
313 const struct got_error *err;
315 if (view->parent) {
316 err = view->parent->show(view->parent);
317 if (err)
318 return err;
319 show_panel(view->parent->panel);
322 err = view->show(view);
323 if (err)
324 return err;
325 show_panel(view->panel);
327 if (view->child && view->child->begin_x > view->begin_x) {
328 err = view->child->show(view->child);
329 if (err)
330 return err;
331 show_panel(view->child->panel);
334 update_panels();
335 doupdate();
337 return err;
340 static const struct got_error *
341 view_resize(struct tog_view *view)
343 int nlines, ncols;
345 while (view) {
346 if (view->lines > LINES)
347 nlines = view->nlines - (view->lines - LINES);
348 else
349 nlines = view->nlines + (LINES - view->lines);
351 if (view->cols > COLS)
352 ncols = view->ncols - (view->cols - COLS);
353 else
354 ncols = view->ncols + (COLS - view->cols);
356 if (wresize(view->window, nlines, ncols) == ERR)
357 return got_error_from_errno();
358 replace_panel(view->panel, view->window);
360 view->nlines = nlines;
361 view->ncols = ncols;
362 view->lines = LINES;
363 view->cols = COLS;
365 view = view->parent;
368 return NULL;
371 static const struct got_error *
372 view_input(struct tog_view **new, struct tog_view **dead,
373 struct tog_view **focus, int *done, struct tog_view *view,
374 struct tog_view_list_head *views)
376 const struct got_error *err = NULL;
377 struct tog_view *next, *prev;
378 int ch;
380 *new = NULL;
381 *dead = NULL;
383 nodelay(stdscr, FALSE);
384 ch = wgetch(view->window);
385 nodelay(stdscr, TRUE);
386 switch (ch) {
387 case ERR:
388 break;
389 case '\t':
390 next = TAILQ_NEXT(view, entry);
391 if (next)
392 *focus = next;
393 else
394 *focus = TAILQ_FIRST(views);
395 view->focussed = 0;
396 (*focus)->focussed = 1;
397 break;
398 case '~':
399 prev = TAILQ_PREV(view, tog_view_list_head, entry);
400 if (prev)
401 *focus = prev;
402 else
403 *focus = TAILQ_LAST(views, tog_view_list_head);
404 view->focussed = 0;
405 (*focus)->focussed = 1;
406 break;
407 case 'q':
408 err = view->input(new, dead, view, ch);
409 *dead = view;
410 break;
411 case 'Q':
412 *done = 1;
413 break;
414 case KEY_RESIZE:
415 err = view_resize(view);
416 if (err)
417 return err;
418 err = view->input(new, dead, view, ch);
419 break;
420 default:
421 err = view->input(new, dead, view, ch);
422 break;
425 return err;
428 static const struct got_error *
429 view_set_child(struct tog_view *view, struct tog_view *child)
431 const struct got_error *err;
433 if (view->set_child) {
434 err = view->set_child(view, child);
435 if (err)
436 return err;
439 view->child = child;
440 return NULL;
443 void
444 view_vborder(struct tog_view *view)
446 if (view->child == NULL)
447 return;
449 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
450 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
453 int
454 view_needs_focus_indication(struct tog_view *view)
456 if (!view->focussed)
457 return 0;
459 if (view->child && view->child->begin_x > view->begin_x)
460 return 1;
462 if (view->parent && view->begin_x > view->parent->begin_x)
463 return 1;
465 return 0;
468 static const struct got_error *
469 view_loop(struct tog_view *view)
471 const struct got_error *err = NULL;
472 struct tog_view_list_head views;
473 struct tog_view *new_view, *dead_view;
474 int done = 0;
476 TAILQ_INIT(&views);
477 TAILQ_INSERT_HEAD(&views, view, entry);
479 view->focussed = 1;
480 while (!TAILQ_EMPTY(&views) && !done) {
481 err = view_show(view);
482 if (err)
483 break;
484 err = view_input(&new_view, &dead_view, &view, &done,
485 view, &views);
486 if (err)
487 break;
488 if (dead_view) {
489 struct tog_view *v, *t;
490 TAILQ_REMOVE(&views, dead_view, entry);
491 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
492 if (v->parent == dead_view) {
493 TAILQ_REMOVE(&views, v, entry);
494 err = view_close(v);
495 if (err)
496 goto done;
499 if (dead_view->parent)
500 view = dead_view->parent;
501 else
502 view = TAILQ_LAST(&views, tog_view_list_head);
503 if (view)
504 view->focussed = 1;
505 err = view_close(dead_view);
506 if (err)
507 goto done;
509 if (new_view) {
510 struct tog_view *v, *t;
511 view->focussed = 0;
512 /* Only allow one view per type. */
513 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
514 if (v->type != new_view->type)
515 continue;
516 TAILQ_REMOVE(&views, v, entry);
517 err = view_close(v);
518 if (err)
519 goto done;
521 TAILQ_INSERT_TAIL(&views, new_view, entry);
522 if (new_view->parent) {
523 err = view_set_child(new_view->parent, new_view);
524 if (err)
525 goto done;
526 new_view->parent->focussed = 0;
528 view = new_view;
529 view->focussed = 1;
532 done:
533 while (!TAILQ_EMPTY(&views)) {
534 view = TAILQ_FIRST(&views);
535 TAILQ_REMOVE(&views, view, entry);
536 view_close(view);
538 return err;
541 __dead static void
542 usage_log(void)
544 endwin();
545 fprintf(stderr,
546 "usage: %s log [-c commit] [-r repository-path] [path]\n",
547 getprogname());
548 exit(1);
551 /* Create newly allocated wide-character string equivalent to a byte string. */
552 static const struct got_error *
553 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
555 char *vis = NULL;
556 const struct got_error *err = NULL;
558 *ws = NULL;
559 *wlen = mbstowcs(NULL, s, 0);
560 if (*wlen == (size_t)-1) {
561 int vislen;
562 if (errno != EILSEQ)
563 return got_error_from_errno();
565 /* byte string invalid in current encoding; try to "fix" it */
566 err = got_mbsavis(&vis, &vislen, s);
567 if (err)
568 return err;
569 *wlen = mbstowcs(NULL, vis, 0);
570 if (*wlen == (size_t)-1) {
571 err = got_error_from_errno(); /* give up */
572 goto done;
576 *ws = calloc(*wlen + 1, sizeof(*ws));
577 if (*ws == NULL) {
578 err = got_error_from_errno();
579 goto done;
582 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
583 err = got_error_from_errno();
584 done:
585 free(vis);
586 if (err) {
587 free(*ws);
588 *ws = NULL;
589 *wlen = 0;
591 return err;
594 /* Format a line for display, ensuring that it won't overflow a width limit. */
595 static const struct got_error *
596 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
598 const struct got_error *err = NULL;
599 int cols = 0;
600 wchar_t *wline = NULL;
601 size_t wlen;
602 int i;
604 *wlinep = NULL;
605 *widthp = 0;
607 err = mbs2ws(&wline, &wlen, line);
608 if (err)
609 return err;
611 i = 0;
612 while (i < wlen && cols < wlimit) {
613 int width = wcwidth(wline[i]);
614 switch (width) {
615 case 0:
616 i++;
617 break;
618 case 1:
619 case 2:
620 if (cols + width <= wlimit)
621 cols += width;
622 i++;
623 break;
624 case -1:
625 if (wline[i] == L'\t')
626 cols += TABSIZE - ((cols + 1) % TABSIZE);
627 i++;
628 break;
629 default:
630 err = got_error_from_errno();
631 goto done;
634 wline[i] = L'\0';
635 if (widthp)
636 *widthp = cols;
637 done:
638 if (err)
639 free(wline);
640 else
641 *wlinep = wline;
642 return err;
645 static const struct got_error *
646 draw_commit(struct tog_view *view, struct got_commit_object *commit,
647 struct got_object_id *id)
649 const struct got_error *err = NULL;
650 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
651 char *logmsg0 = NULL, *logmsg = NULL;
652 char *author0 = NULL, *author = NULL;
653 wchar_t *wlogmsg = NULL, *wauthor = NULL;
654 int author_width, logmsg_width;
655 char *newline, *smallerthan;
656 char *line = NULL;
657 int col, limit;
658 static const size_t date_display_cols = 9;
659 static const size_t author_display_cols = 16;
660 const int avail = view->ncols;
662 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
663 &commit->tm_committer) >= sizeof(datebuf))
664 return got_error(GOT_ERR_NO_SPACE);
666 if (avail < date_display_cols)
667 limit = MIN(sizeof(datebuf) - 1, avail);
668 else
669 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
670 waddnstr(view->window, datebuf, limit);
671 col = limit + 1;
672 if (col > avail)
673 goto done;
675 author0 = strdup(commit->author);
676 if (author0 == NULL) {
677 err = got_error_from_errno();
678 goto done;
680 author = author0;
681 smallerthan = strchr(author, '<');
682 if (smallerthan)
683 *smallerthan = '\0';
684 else {
685 char *at = strchr(author, '@');
686 if (at)
687 *at = '\0';
689 limit = avail - col;
690 err = format_line(&wauthor, &author_width, author, limit);
691 if (err)
692 goto done;
693 waddwstr(view->window, wauthor);
694 col += author_width;
695 while (col <= avail && author_width < author_display_cols + 1) {
696 waddch(view->window, ' ');
697 col++;
698 author_width++;
700 if (col > avail)
701 goto done;
703 logmsg0 = strdup(commit->logmsg);
704 if (logmsg0 == NULL) {
705 err = got_error_from_errno();
706 goto done;
708 logmsg = logmsg0;
709 while (*logmsg == '\n')
710 logmsg++;
711 newline = strchr(logmsg, '\n');
712 if (newline)
713 *newline = '\0';
714 limit = avail - col;
715 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
716 if (err)
717 goto done;
718 waddwstr(view->window, wlogmsg);
719 col += logmsg_width;
720 while (col <= avail) {
721 waddch(view->window, ' ');
722 col++;
724 done:
725 free(logmsg0);
726 free(wlogmsg);
727 free(author0);
728 free(wauthor);
729 free(line);
730 return err;
733 static struct commit_queue_entry *
734 alloc_commit_queue_entry(struct got_commit_object *commit,
735 struct got_object_id *id)
737 struct commit_queue_entry *entry;
739 entry = calloc(1, sizeof(*entry));
740 if (entry == NULL)
741 return NULL;
743 entry->id = id;
744 entry->commit = commit;
745 return entry;
748 static void
749 pop_commit(struct commit_queue *commits)
751 struct commit_queue_entry *entry;
753 entry = TAILQ_FIRST(&commits->head);
754 TAILQ_REMOVE(&commits->head, entry, entry);
755 got_object_commit_close(entry->commit);
756 commits->ncommits--;
757 /* Don't free entry->id! It is owned by the commit graph. */
758 free(entry);
761 static void
762 free_commits(struct commit_queue *commits)
764 while (!TAILQ_EMPTY(&commits->head))
765 pop_commit(commits);
768 static const struct got_error *
769 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
770 struct got_object_id *start_id, int minqueue,
771 struct got_repository *repo, const char *path)
773 const struct got_error *err = NULL;
774 int nqueued = 0;
776 if (start_id) {
777 err = got_commit_graph_iter_start(graph, start_id, repo);
778 if (err)
779 return err;
782 while (nqueued < minqueue) {
783 struct got_object_id *id;
784 struct got_commit_object *commit;
785 struct commit_queue_entry *entry;
787 err = got_commit_graph_iter_next(&id, graph);
788 if (err) {
789 if (err->code == GOT_ERR_ITER_COMPLETED) {
790 err = NULL;
791 break;
793 if (err->code != GOT_ERR_ITER_NEED_MORE)
794 break;
795 err = got_commit_graph_fetch_commits(graph,
796 minqueue, repo);
797 if (err)
798 return err;
799 continue;
802 if (id == NULL)
803 break;
805 err = got_object_open_as_commit(&commit, repo, id);
806 if (err)
807 break;
808 entry = alloc_commit_queue_entry(commit, id);
809 if (entry == NULL) {
810 err = got_error_from_errno();
811 break;
814 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
815 nqueued++;
816 commits->ncommits++;
819 return err;
822 static const struct got_error *
823 fetch_next_commit(struct commit_queue_entry **pentry,
824 struct commit_queue_entry *entry, struct commit_queue *commits,
825 struct got_commit_graph *graph, struct got_repository *repo,
826 const char *path)
828 const struct got_error *err = NULL;
830 *pentry = NULL;
832 err = queue_commits(graph, commits, NULL, 1, repo, path);
833 if (err)
834 return err;
836 /* Next entry to display should now be available. */
837 *pentry = TAILQ_NEXT(entry, entry);
838 return NULL;
841 static const struct got_error *
842 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
844 const struct got_error *err = NULL;
845 struct got_reference *head_ref;
847 *head_id = NULL;
849 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
850 if (err)
851 return err;
853 err = got_ref_resolve(head_id, repo, head_ref);
854 got_ref_close(head_ref);
855 if (err) {
856 *head_id = NULL;
857 return err;
860 return NULL;
863 static const struct got_error *
864 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
865 struct commit_queue_entry **selected, struct commit_queue_entry *first,
866 struct commit_queue *commits, int selected_idx, int limit,
867 struct got_commit_graph *graph, struct got_repository *repo,
868 const char *path)
870 const struct got_error *err = NULL;
871 struct commit_queue_entry *entry;
872 int ncommits, width;
873 char *id_str, *header;
874 wchar_t *wline;
876 entry = first;
877 ncommits = 0;
878 while (entry) {
879 if (ncommits == selected_idx) {
880 *selected = entry;
881 break;
883 entry = TAILQ_NEXT(entry, entry);
884 ncommits++;
887 err = got_object_id_str(&id_str, (*selected)->id);
888 if (err)
889 return err;
891 if (path && strcmp(path, "/") != 0) {
892 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
893 err = got_error_from_errno();
894 free(id_str);
895 return err;
897 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
898 err = got_error_from_errno();
899 free(id_str);
900 return err;
902 free(id_str);
903 err = format_line(&wline, &width, header, view->ncols);
904 if (err) {
905 free(header);
906 return err;
908 free(header);
910 werase(view->window);
912 if (view_needs_focus_indication(view))
913 wstandout(view->window);
914 waddwstr(view->window, wline);
915 if (view_needs_focus_indication(view))
916 wstandend(view->window);
917 if (width < view->ncols)
918 waddch(view->window, '\n');
919 free(wline);
920 if (limit <= 1)
921 return NULL;
923 entry = first;
924 *last = first;
925 ncommits = 0;
926 while (entry) {
927 if (ncommits >= limit - 1)
928 break;
929 if (ncommits == selected_idx)
930 wstandout(view->window);
931 err = draw_commit(view, entry->commit, entry->id);
932 if (ncommits == selected_idx)
933 wstandend(view->window);
934 if (err)
935 break;
936 ncommits++;
937 *last = entry;
938 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
939 err = queue_commits(graph, commits, NULL, 1,
940 repo, path);
941 if (err) {
942 if (err->code != GOT_ERR_ITER_COMPLETED)
943 return err;
944 err = NULL;
947 entry = TAILQ_NEXT(entry, entry);
950 view_vborder(view);
952 return err;
955 static void
956 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
957 struct commit_queue *commits)
959 struct commit_queue_entry *entry;
960 int nscrolled = 0;
962 entry = TAILQ_FIRST(&commits->head);
963 if (*first_displayed_entry == entry)
964 return;
966 entry = *first_displayed_entry;
967 while (entry && nscrolled < maxscroll) {
968 entry = TAILQ_PREV(entry, commit_queue_head, entry);
969 if (entry) {
970 *first_displayed_entry = entry;
971 nscrolled++;
976 static const struct got_error *
977 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
978 struct commit_queue_entry **last_displayed_entry,
979 struct commit_queue *commits, struct got_commit_graph *graph,
980 struct got_repository *repo, const char *path)
982 const struct got_error *err = NULL;
983 struct commit_queue_entry *pentry;
984 int nscrolled = 0;
986 do {
987 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
988 if (pentry == NULL) {
989 err = fetch_next_commit(&pentry, *last_displayed_entry,
990 commits, graph, repo, path);
991 if (err || pentry == NULL)
992 break;
994 *last_displayed_entry = pentry;
996 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
997 if (pentry == NULL)
998 break;
999 *first_displayed_entry = pentry;
1000 } while (++nscrolled < maxscroll);
1002 return err;
1005 static const struct got_error *
1006 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1007 struct commit_queue_entry *entry, struct got_repository *repo)
1009 const struct got_error *err;
1010 struct got_object *obj1 = NULL, *obj2 = NULL;
1011 struct got_object_qid *parent_id;
1012 struct tog_view *diff_view;
1014 err = got_object_open(&obj2, repo, entry->id);
1015 if (err)
1016 return err;
1018 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1019 if (parent_id) {
1020 err = got_object_open(&obj1, repo, parent_id->id);
1021 if (err)
1022 goto done;
1025 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1026 if (diff_view == NULL) {
1027 err = got_error_from_errno();
1028 goto done;
1031 err = open_diff_view(diff_view, obj1, obj2, repo);
1032 if (err == NULL)
1033 *new_view = diff_view;
1034 done:
1035 if (obj1)
1036 got_object_close(obj1);
1037 if (obj2)
1038 got_object_close(obj2);
1039 return err;
1042 static const struct got_error *
1043 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1044 struct commit_queue_entry *entry, struct got_repository *repo)
1046 const struct got_error *err = NULL;
1047 struct got_tree_object *tree;
1048 struct tog_view *tree_view;
1050 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1051 if (err)
1052 return err;
1054 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1055 if (tree_view == NULL)
1056 return got_error_from_errno();
1058 err = open_tree_view(tree_view, tree, entry->id, repo);
1059 if (err)
1060 got_object_tree_close(tree);
1061 else
1062 *new_view = tree_view;
1063 return err;
1066 static const struct got_error *
1067 set_child_log_view(struct tog_view *view, struct tog_view *child)
1069 struct tog_log_view_state *s = &view->state.log;
1070 struct tog_diff_view_state *ds;
1071 struct commit_queue_entry *commit, *child_entry = NULL;
1072 int selected_idx = 0;
1074 if (child->type != TOG_VIEW_DIFF)
1075 return NULL;
1076 ds = &child->state.diff;
1078 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1079 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1080 child_entry = commit;
1081 break;
1084 if (child_entry == NULL)
1085 return NULL;
1087 commit = s->first_displayed_entry;
1088 while (commit) {
1089 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1090 s->selected_entry = child_entry;
1091 s->selected = selected_idx;
1092 break;
1094 if (commit == s->last_displayed_entry)
1095 break;
1096 selected_idx++;
1097 commit = TAILQ_NEXT(commit, entry);
1100 return show_log_view(view);
1103 static const struct got_error *
1104 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1105 struct got_repository *repo, const char *path)
1107 const struct got_error *err = NULL;
1108 struct tog_log_view_state *s = &view->state.log;
1110 err = got_repo_map_path(&s->in_repo_path, repo, path);
1111 if (err != NULL)
1112 goto done;
1114 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1115 0, repo);
1116 if (err)
1117 goto done;
1118 /* The commit queue only contains commits being displayed. */
1119 TAILQ_INIT(&s->commits.head);
1120 s->commits.ncommits = 0;
1123 * Open the initial batch of commits, sorted in commit graph order.
1124 * We keep all commits open throughout the lifetime of the log view
1125 * in order to avoid having to re-fetch commits from disk while
1126 * updating the display.
1128 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1129 repo, s->in_repo_path);
1130 if (err) {
1131 if (err->code != GOT_ERR_ITER_COMPLETED)
1132 goto done;
1133 err = NULL;
1136 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1137 s->selected_entry = s->first_displayed_entry;
1138 s->repo = repo;
1140 view->show = show_log_view;
1141 view->input = input_log_view;
1142 view->close = close_log_view;
1143 view->set_child = set_child_log_view;
1144 done:
1145 return err;
1148 static const struct got_error *
1149 close_log_view(struct tog_view *view)
1151 struct tog_log_view_state *s = &view->state.log;
1153 if (s->graph)
1154 got_commit_graph_close(s->graph);
1155 free_commits(&s->commits);
1156 free(s->in_repo_path);
1157 return NULL;
1160 static const struct got_error *
1161 update_diff_child_view(struct tog_view *parent,
1162 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1164 const struct got_error *err = NULL;
1165 struct tog_diff_view_state *ds;
1166 struct got_object *obj1 = NULL, *obj2 = NULL;
1167 struct got_object_qid *parent_id;
1168 struct tog_view *child_view = parent->child;
1170 if (child_view == NULL)
1171 return NULL;
1172 if (child_view->type != TOG_VIEW_DIFF)
1173 return NULL;
1174 ds = &child_view->state.diff;
1175 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1176 return NULL;
1178 err = got_object_open(&obj2, repo, selected_entry->id);
1179 if (err)
1180 return err;
1182 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1183 if (parent_id) {
1184 err = got_object_open(&obj1, repo, parent_id->id);
1185 if (err)
1186 goto done;
1189 err = close_diff_view(child_view);
1190 if (err)
1191 goto done;
1193 err = open_diff_view(child_view, obj1, obj2, repo);
1194 if (err)
1195 goto done;
1196 done:
1197 if (obj1)
1198 got_object_close(obj1);
1199 if (obj2)
1200 got_object_close(obj2);
1201 return err;
1204 static const struct got_error *
1205 show_log_view(struct tog_view *view)
1207 const struct got_error *err = NULL;
1208 struct tog_log_view_state *s = &view->state.log;
1210 err = draw_commits(view, &s->last_displayed_entry,
1211 &s->selected_entry, s->first_displayed_entry,
1212 &s->commits, s->selected, view->nlines, s->graph,
1213 s->repo, s->in_repo_path);
1214 if (err)
1215 return err;
1217 return update_diff_child_view(view, s->selected_entry, s->repo);
1220 static const struct got_error *
1221 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1222 struct tog_view *view, int ch)
1224 const struct got_error *err = NULL;
1225 struct tog_log_view_state *s = &view->state.log;
1227 switch (ch) {
1228 case 'k':
1229 case KEY_UP:
1230 case '[':
1231 if (s->selected > 0)
1232 s->selected--;
1233 if (s->selected > 0)
1234 break;
1235 scroll_up(&s->first_displayed_entry, 1,
1236 &s->commits);
1237 break;
1238 case KEY_PPAGE:
1239 if (TAILQ_FIRST(&s->commits.head) ==
1240 s->first_displayed_entry) {
1241 s->selected = 0;
1242 break;
1244 scroll_up(&s->first_displayed_entry,
1245 view->nlines, &s->commits);
1246 break;
1247 case 'j':
1248 case KEY_DOWN:
1249 case ']':
1250 if (s->selected < MIN(view->nlines - 2,
1251 s->commits.ncommits - 1)) {
1252 s->selected++;
1253 break;
1255 err = scroll_down(&s->first_displayed_entry, 1,
1256 &s->last_displayed_entry, &s->commits,
1257 s->graph, s->repo, s->in_repo_path);
1258 if (err) {
1259 if (err->code != GOT_ERR_ITER_COMPLETED)
1260 break;
1261 err = NULL;
1263 break;
1264 case KEY_NPAGE: {
1265 struct commit_queue_entry *first;
1266 first = s->first_displayed_entry;
1267 err = scroll_down(&s->first_displayed_entry,
1268 view->nlines, &s->last_displayed_entry,
1269 &s->commits, s->graph, s->repo,
1270 s->in_repo_path);
1271 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1272 break;
1273 if (first == s->first_displayed_entry &&
1274 s->selected < MIN(view->nlines - 2,
1275 s->commits.ncommits - 1)) {
1276 /* can't scroll further down */
1277 s->selected = MIN(view->nlines - 2,
1278 s->commits.ncommits - 1);
1280 err = NULL;
1281 break;
1283 case KEY_RESIZE:
1284 if (s->selected > view->nlines - 2)
1285 s->selected = view->nlines - 2;
1286 if (s->selected > s->commits.ncommits - 1)
1287 s->selected = s->commits.ncommits - 1;
1288 break;
1289 case KEY_ENTER:
1290 case '\r':
1291 err = show_commit(new_view, view, s->selected_entry,
1292 s->repo);
1293 break;
1294 case 't':
1295 err = browse_commit(new_view, view, s->selected_entry,
1296 s->repo);
1297 break;
1298 default:
1299 break;
1302 return err;
1305 static const struct got_error *
1306 cmd_log(int argc, char *argv[])
1308 const struct got_error *error;
1309 struct got_repository *repo = NULL;
1310 struct got_object_id *start_id = NULL;
1311 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1312 char *start_commit = NULL;
1313 int ch;
1314 struct tog_view *view;
1316 #ifndef PROFILE
1317 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1318 == -1)
1319 err(1, "pledge");
1320 #endif
1322 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1323 switch (ch) {
1324 case 'c':
1325 start_commit = optarg;
1326 break;
1327 case 'r':
1328 repo_path = realpath(optarg, NULL);
1329 if (repo_path == NULL)
1330 err(1, "-r option");
1331 break;
1332 default:
1333 usage();
1334 /* NOTREACHED */
1338 argc -= optind;
1339 argv += optind;
1341 if (argc == 0)
1342 path = strdup("");
1343 else if (argc == 1)
1344 path = strdup(argv[0]);
1345 else
1346 usage_log();
1347 if (path == NULL)
1348 return got_error_from_errno();
1350 cwd = getcwd(NULL, 0);
1351 if (cwd == NULL) {
1352 error = got_error_from_errno();
1353 goto done;
1355 if (repo_path == NULL) {
1356 repo_path = strdup(cwd);
1357 if (repo_path == NULL) {
1358 error = got_error_from_errno();
1359 goto done;
1363 error = got_repo_open(&repo, repo_path);
1364 if (error != NULL)
1365 goto done;
1367 if (start_commit == NULL) {
1368 error = get_head_commit_id(&start_id, repo);
1369 if (error != NULL)
1370 goto done;
1371 } else {
1372 struct got_object *obj;
1373 error = got_object_open_by_id_str(&obj, repo, start_commit);
1374 if (error == NULL) {
1375 start_id = got_object_id_dup(got_object_get_id(obj));
1376 if (start_id == NULL)
1377 error = got_error_from_errno();
1378 goto done;
1381 if (error != NULL)
1382 goto done;
1384 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1385 if (view == NULL) {
1386 error = got_error_from_errno();
1387 goto done;
1389 error = open_log_view(view, start_id, repo, path);
1390 if (error)
1391 goto done;
1392 error = view_loop(view);
1393 done:
1394 free(repo_path);
1395 free(cwd);
1396 free(path);
1397 free(start_id);
1398 if (repo)
1399 got_repo_close(repo);
1400 return error;
1403 __dead static void
1404 usage_diff(void)
1406 endwin();
1407 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1408 getprogname());
1409 exit(1);
1412 static char *
1413 parse_next_line(FILE *f, size_t *len)
1415 char *line;
1416 size_t linelen;
1417 size_t lineno;
1418 const char delim[3] = { '\0', '\0', '\0'};
1420 line = fparseln(f, &linelen, &lineno, delim, 0);
1421 if (len)
1422 *len = linelen;
1423 return line;
1426 static const struct got_error *
1427 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1428 int *last_displayed_line, int *eof, int max_lines,
1429 char * header)
1431 const struct got_error *err;
1432 int nlines = 0, nprinted = 0;
1433 char *line;
1434 size_t len;
1435 wchar_t *wline;
1436 int width;
1438 rewind(f);
1439 werase(view->window);
1441 if (header) {
1442 err = format_line(&wline, &width, header, view->ncols);
1443 if (err) {
1444 return err;
1447 if (view_needs_focus_indication(view))
1448 wstandout(view->window);
1449 waddwstr(view->window, wline);
1450 if (view_needs_focus_indication(view))
1451 wstandend(view->window);
1452 if (width < view->ncols)
1453 waddch(view->window, '\n');
1455 if (max_lines <= 1)
1456 return NULL;
1457 max_lines--;
1460 *eof = 0;
1461 while (nprinted < max_lines) {
1462 line = parse_next_line(f, &len);
1463 if (line == NULL) {
1464 *eof = 1;
1465 break;
1467 if (++nlines < *first_displayed_line) {
1468 free(line);
1469 continue;
1472 err = format_line(&wline, &width, line, view->ncols);
1473 if (err) {
1474 free(line);
1475 return err;
1477 waddwstr(view->window, wline);
1478 if (width < view->ncols)
1479 waddch(view->window, '\n');
1480 if (++nprinted == 1)
1481 *first_displayed_line = nlines;
1482 free(line);
1483 free(wline);
1484 wline = NULL;
1486 *last_displayed_line = nlines;
1488 view_vborder(view);
1490 return NULL;
1493 static const struct got_error *
1494 open_diff_view(struct tog_view *view, struct got_object *obj1,
1495 struct got_object *obj2, struct got_repository *repo)
1497 const struct got_error *err;
1498 FILE *f;
1500 if (obj1 != NULL && obj2 != NULL &&
1501 got_object_get_type(obj1) != got_object_get_type(obj2))
1502 return got_error(GOT_ERR_OBJ_TYPE);
1504 f = got_opentemp();
1505 if (f == NULL)
1506 return got_error_from_errno();
1508 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1509 case GOT_OBJ_TYPE_BLOB:
1510 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1511 repo, f);
1512 break;
1513 case GOT_OBJ_TYPE_TREE:
1514 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1515 break;
1516 case GOT_OBJ_TYPE_COMMIT:
1517 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1518 break;
1519 default:
1520 return got_error(GOT_ERR_OBJ_TYPE);
1523 fflush(f);
1525 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1526 view->state.diff.id2 = got_object_get_id(obj2);
1527 view->state.diff.f = f;
1528 view->state.diff.first_displayed_line = 1;
1529 view->state.diff.last_displayed_line = view->nlines;
1531 view->show = show_diff_view;
1532 view->input = input_diff_view;
1533 view->close = close_diff_view;
1535 return NULL;
1538 static const struct got_error *
1539 close_diff_view(struct tog_view *view)
1541 const struct got_error *err = NULL;
1543 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1544 err = got_error_from_errno();
1545 return err;
1548 static const struct got_error *
1549 show_diff_view(struct tog_view *view)
1551 const struct got_error *err;
1552 struct tog_diff_view_state *s = &view->state.diff;
1553 char *id_str1 = NULL, *id_str2, *header;
1555 if (s->id1) {
1556 err = got_object_id_str(&id_str1, s->id1);
1557 if (err)
1558 return err;
1560 err = got_object_id_str(&id_str2, s->id2);
1561 if (err)
1562 return err;
1564 if (asprintf(&header, "diff: %s %s",
1565 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1566 err = got_error_from_errno();
1567 free(id_str1);
1568 free(id_str2);
1569 return err;
1571 free(id_str1);
1572 free(id_str2);
1574 return draw_file(view, s->f, &s->first_displayed_line,
1575 &s->last_displayed_line, &s->eof, view->nlines,
1576 header);
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 exec sendfd", NULL)
1671 == -1)
1672 err(1, "pledge");
1673 #endif
1675 while ((ch = getopt(argc, argv, "")) != -1) {
1676 switch (ch) {
1677 default:
1678 usage();
1679 /* NOTREACHED */
1683 argc -= optind;
1684 argv += optind;
1686 if (argc == 0) {
1687 usage_diff(); /* TODO show local worktree changes */
1688 } else if (argc == 2) {
1689 repo_path = getcwd(NULL, 0);
1690 if (repo_path == NULL)
1691 return got_error_from_errno();
1692 obj_id_str1 = argv[0];
1693 obj_id_str2 = argv[1];
1694 } else if (argc == 3) {
1695 repo_path = realpath(argv[0], NULL);
1696 if (repo_path == NULL)
1697 return got_error_from_errno();
1698 obj_id_str1 = argv[1];
1699 obj_id_str2 = argv[2];
1700 } else
1701 usage_diff();
1703 error = got_repo_open(&repo, repo_path);
1704 free(repo_path);
1705 if (error)
1706 goto done;
1708 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1709 if (error)
1710 goto done;
1712 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1713 if (error)
1714 goto done;
1716 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1717 if (view == NULL) {
1718 error = got_error_from_errno();
1719 goto done;
1721 error = open_diff_view(view, obj1, obj2, repo);
1722 if (error)
1723 goto done;
1724 error = view_loop(view);
1725 done:
1726 got_repo_close(repo);
1727 if (obj1)
1728 got_object_close(obj1);
1729 if (obj2)
1730 got_object_close(obj2);
1731 return error;
1734 __dead static void
1735 usage_blame(void)
1737 endwin();
1738 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1739 getprogname());
1740 exit(1);
1743 struct tog_blame_line {
1744 int annotated;
1745 struct got_object_id *id;
1748 static const struct got_error *
1749 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1750 const char *path, struct tog_blame_line *lines, int nlines,
1751 int blame_complete, int selected_line, int *first_displayed_line,
1752 int *last_displayed_line, int *eof, int max_lines)
1754 const struct got_error *err;
1755 int lineno = 0, nprinted = 0;
1756 char *line;
1757 size_t len;
1758 wchar_t *wline;
1759 int width, wlimit;
1760 struct tog_blame_line *blame_line;
1761 struct got_object_id *prev_id = NULL;
1762 char *id_str;
1764 err = got_object_id_str(&id_str, id);
1765 if (err)
1766 return err;
1768 rewind(f);
1769 werase(view->window);
1771 if (asprintf(&line, "commit: %s", id_str) == -1) {
1772 err = got_error_from_errno();
1773 free(id_str);
1774 return err;
1777 err = format_line(&wline, &width, line, view->ncols);
1778 free(line);
1779 line = NULL;
1780 if (view_needs_focus_indication(view))
1781 wstandout(view->window);
1782 waddwstr(view->window, wline);
1783 if (view_needs_focus_indication(view))
1784 wstandend(view->window);
1785 free(wline);
1786 wline = NULL;
1787 if (width < view->ncols)
1788 waddch(view->window, '\n');
1790 if (asprintf(&line, "[%d/%d] %s%s",
1791 *first_displayed_line - 1 + selected_line, nlines,
1792 blame_complete ? "" : "annotating ", path) == -1) {
1793 free(id_str);
1794 return got_error_from_errno();
1796 free(id_str);
1797 err = format_line(&wline, &width, line, view->ncols);
1798 free(line);
1799 line = NULL;
1800 if (err)
1801 return err;
1802 waddwstr(view->window, wline);
1803 free(wline);
1804 wline = NULL;
1805 if (width < view->ncols)
1806 waddch(view->window, '\n');
1808 *eof = 0;
1809 while (nprinted < max_lines - 2) {
1810 line = parse_next_line(f, &len);
1811 if (line == NULL) {
1812 *eof = 1;
1813 break;
1815 if (++lineno < *first_displayed_line) {
1816 free(line);
1817 continue;
1820 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1821 err = format_line(&wline, &width, line, wlimit);
1822 if (err) {
1823 free(line);
1824 return err;
1827 if (nprinted == selected_line - 1)
1828 wstandout(view->window);
1830 blame_line = &lines[lineno - 1];
1831 if (blame_line->annotated && prev_id &&
1832 got_object_id_cmp(prev_id, blame_line->id) == 0)
1833 waddstr(view->window, " ");
1834 else if (blame_line->annotated) {
1835 char *id_str;
1836 err = got_object_id_str(&id_str, blame_line->id);
1837 if (err) {
1838 free(line);
1839 free(wline);
1840 return err;
1842 wprintw(view->window, "%.8s ", id_str);
1843 free(id_str);
1844 prev_id = blame_line->id;
1845 } else {
1846 waddstr(view->window, "........ ");
1847 prev_id = NULL;
1850 waddwstr(view->window, wline);
1851 while (width < wlimit) {
1852 waddch(view->window, ' ');
1853 width++;
1855 if (nprinted == selected_line - 1)
1856 wstandend(view->window);
1857 if (++nprinted == 1)
1858 *first_displayed_line = lineno;
1859 free(line);
1860 free(wline);
1861 wline = NULL;
1863 *last_displayed_line = lineno;
1865 view_vborder(view);
1867 return NULL;
1870 static const struct got_error *
1871 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1873 const struct got_error *err = NULL;
1874 struct tog_blame_cb_args *a = arg;
1875 struct tog_blame_line *line;
1877 if (nlines != a->nlines ||
1878 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1879 return got_error(GOT_ERR_RANGE);
1881 if (pthread_mutex_lock(a->mutex) != 0)
1882 return got_error_from_errno();
1884 if (*a->quit) { /* user has quit the blame view */
1885 err = got_error(GOT_ERR_ITER_COMPLETED);
1886 goto done;
1889 if (lineno == -1)
1890 goto done; /* no change in this commit */
1892 line = &a->lines[lineno - 1];
1893 if (line->annotated)
1894 goto done;
1896 line->id = got_object_id_dup(id);
1897 if (line->id == NULL) {
1898 err = got_error_from_errno();
1899 goto done;
1901 line->annotated = 1;
1903 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1904 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1905 a->last_displayed_line, a->eof, a->view->nlines);
1906 done:
1907 if (pthread_mutex_unlock(a->mutex) != 0)
1908 return got_error_from_errno();
1909 return err;
1912 static void *
1913 blame_thread(void *arg)
1915 const struct got_error *err;
1916 struct tog_blame_thread_args *ta = arg;
1917 struct tog_blame_cb_args *a = ta->cb_args;
1919 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1920 blame_cb, ta->cb_args);
1922 if (pthread_mutex_lock(a->mutex) != 0)
1923 return (void *)got_error_from_errno();
1925 got_repo_close(ta->repo);
1926 ta->repo = NULL;
1927 *ta->complete = 1;
1928 if (!err)
1929 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1930 a->lines, a->nlines, 1, *a->selected_line,
1931 a->first_displayed_line, a->last_displayed_line, a->eof,
1932 a->view->nlines);
1934 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1935 err = got_error_from_errno();
1937 return (void *)err;
1940 static struct got_object_id *
1941 get_selected_commit_id(struct tog_blame_line *lines,
1942 int first_displayed_line, int selected_line)
1944 struct tog_blame_line *line;
1946 line = &lines[first_displayed_line - 1 + selected_line - 1];
1947 if (!line->annotated)
1948 return NULL;
1950 return line->id;
1953 static const struct got_error *
1954 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1955 struct tog_blame_line *lines, int first_displayed_line,
1956 int selected_line, struct got_repository *repo)
1958 const struct got_error *err = NULL;
1959 struct got_commit_object *commit = NULL;
1960 struct got_object_id *selected_id;
1961 struct got_object_qid *pid;
1963 *pobj = NULL;
1964 *obj = NULL;
1966 selected_id = get_selected_commit_id(lines,
1967 first_displayed_line, selected_line);
1968 if (selected_id == NULL)
1969 return NULL;
1971 err = got_object_open(obj, repo, selected_id);
1972 if (err)
1973 goto done;
1975 err = got_object_commit_open(&commit, repo, *obj);
1976 if (err)
1977 goto done;
1979 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1980 if (pid) {
1981 err = got_object_open(pobj, repo, pid->id);
1982 if (err)
1983 goto done;
1985 done:
1986 if (commit)
1987 got_object_commit_close(commit);
1988 return err;
1991 static const struct got_error *
1992 stop_blame(struct tog_blame *blame)
1994 const struct got_error *err = NULL;
1995 int i;
1997 if (blame->thread) {
1998 if (pthread_join(blame->thread, (void **)&err) != 0)
1999 err = got_error_from_errno();
2000 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2001 err = NULL;
2002 blame->thread = NULL;
2004 if (blame->thread_args.repo) {
2005 got_repo_close(blame->thread_args.repo);
2006 blame->thread_args.repo = NULL;
2008 if (blame->f) {
2009 fclose(blame->f);
2010 blame->f = NULL;
2012 for (i = 0; i < blame->nlines; i++)
2013 free(blame->lines[i].id);
2014 free(blame->lines);
2015 blame->lines = NULL;
2016 free(blame->cb_args.commit_id);
2017 blame->cb_args.commit_id = NULL;
2019 return err;
2022 static const struct got_error *
2023 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2024 struct tog_view *view, int *blame_complete,
2025 int *first_displayed_line, int *last_displayed_line,
2026 int *selected_line, int *done, int *eof, const char *path,
2027 struct got_object_id *commit_id,
2028 struct got_repository *repo)
2030 const struct got_error *err = NULL;
2031 struct got_blob_object *blob = NULL;
2032 struct got_repository *thread_repo = NULL;
2033 struct got_object_id *obj_id = NULL;
2034 struct got_object *obj = NULL;
2036 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2037 if (err)
2038 goto done;
2040 err = got_object_open(&obj, repo, obj_id);
2041 if (err)
2042 goto done;
2044 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2045 err = got_error(GOT_ERR_OBJ_TYPE);
2046 goto done;
2049 err = got_object_blob_open(&blob, repo, obj, 8192);
2050 if (err)
2051 goto done;
2052 blame->f = got_opentemp();
2053 if (blame->f == NULL) {
2054 err = got_error_from_errno();
2055 goto done;
2057 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2058 blame->f, blob);
2059 if (err)
2060 goto done;
2062 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2063 if (blame->lines == NULL) {
2064 err = got_error_from_errno();
2065 goto done;
2068 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2069 if (err)
2070 goto done;
2072 blame->cb_args.view = view;
2073 blame->cb_args.lines = blame->lines;
2074 blame->cb_args.nlines = blame->nlines;
2075 blame->cb_args.mutex = mutex;
2076 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2077 if (blame->cb_args.commit_id == NULL) {
2078 err = got_error_from_errno();
2079 goto done;
2081 blame->cb_args.f = blame->f;
2082 blame->cb_args.path = path;
2083 blame->cb_args.first_displayed_line = first_displayed_line;
2084 blame->cb_args.selected_line = selected_line;
2085 blame->cb_args.last_displayed_line = last_displayed_line;
2086 blame->cb_args.quit = done;
2087 blame->cb_args.eof = eof;
2089 blame->thread_args.path = path;
2090 blame->thread_args.repo = thread_repo;
2091 blame->thread_args.cb_args = &blame->cb_args;
2092 blame->thread_args.complete = blame_complete;
2093 *blame_complete = 0;
2095 if (pthread_create(&blame->thread, NULL, blame_thread,
2096 &blame->thread_args) != 0) {
2097 err = got_error_from_errno();
2098 goto done;
2101 done:
2102 if (blob)
2103 got_object_blob_close(blob);
2104 free(obj_id);
2105 if (obj)
2106 got_object_close(obj);
2107 if (err)
2108 stop_blame(blame);
2109 return err;
2112 static const struct got_error *
2113 open_blame_view(struct tog_view *view, char *path,
2114 struct got_object_id *commit_id, struct got_repository *repo)
2116 const struct got_error *err = NULL;
2117 struct tog_blame_view_state *s = &view->state.blame;
2119 SIMPLEQ_INIT(&s->blamed_commits);
2121 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2122 return got_error_from_errno();
2124 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2125 if (err)
2126 return err;
2128 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2129 s->first_displayed_line = 1;
2130 s->last_displayed_line = view->nlines;
2131 s->selected_line = 1;
2132 s->blame_complete = 0;
2133 s->path = path;
2134 if (s->path == NULL)
2135 return got_error_from_errno();
2136 s->repo = repo;
2137 s->commit_id = commit_id;
2138 memset(&s->blame, 0, sizeof(s->blame));
2140 view->show = show_blame_view;
2141 view->input = input_blame_view;
2142 view->close = close_blame_view;
2144 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2145 &s->first_displayed_line, &s->last_displayed_line,
2146 &s->selected_line, &s->done, &s->eof, s->path,
2147 s->blamed_commit->id, s->repo);
2150 static const struct got_error *
2151 close_blame_view(struct tog_view *view)
2153 const struct got_error *err = NULL;
2154 struct tog_blame_view_state *s = &view->state.blame;
2156 if (s->blame.thread)
2157 err = stop_blame(&s->blame);
2159 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2160 struct got_object_qid *blamed_commit;
2161 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2162 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2163 got_object_qid_free(blamed_commit);
2166 free(s->path);
2168 return err;
2171 static const struct got_error *
2172 show_blame_view(struct tog_view *view)
2174 const struct got_error *err = NULL;
2175 struct tog_blame_view_state *s = &view->state.blame;
2177 if (pthread_mutex_lock(&s->mutex) != 0)
2178 return got_error_from_errno();
2180 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2181 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2182 s->selected_line, &s->first_displayed_line,
2183 &s->last_displayed_line, &s->eof, view->nlines);
2185 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2186 err = got_error_from_errno();
2188 return err;
2191 static const struct got_error *
2192 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2193 struct tog_view *view, int ch)
2195 const struct got_error *err = NULL, *thread_err = NULL;
2196 struct got_object *obj = NULL, *pobj = NULL;
2197 struct tog_view *diff_view;
2198 struct tog_blame_view_state *s = &view->state.blame;
2200 if (pthread_mutex_lock(&s->mutex) != 0) {
2201 err = got_error_from_errno();
2202 goto done;
2205 switch (ch) {
2206 case 'q':
2207 s->done = 1;
2208 if (pthread_mutex_unlock(&s->mutex) != 0) {
2209 err = got_error_from_errno();
2210 goto done;
2212 return stop_blame(&s->blame);
2213 case 'k':
2214 case KEY_UP:
2215 if (s->selected_line > 1)
2216 s->selected_line--;
2217 else if (s->selected_line == 1 &&
2218 s->first_displayed_line > 1)
2219 s->first_displayed_line--;
2220 break;
2221 case KEY_PPAGE:
2222 if (s->first_displayed_line == 1) {
2223 s->selected_line = 1;
2224 break;
2226 if (s->first_displayed_line > view->nlines - 2)
2227 s->first_displayed_line -=
2228 (view->nlines - 2);
2229 else
2230 s->first_displayed_line = 1;
2231 break;
2232 case 'j':
2233 case KEY_DOWN:
2234 if (s->selected_line < view->nlines - 2 &&
2235 s->first_displayed_line +
2236 s->selected_line <= s->blame.nlines)
2237 s->selected_line++;
2238 else if (s->last_displayed_line <
2239 s->blame.nlines)
2240 s->first_displayed_line++;
2241 break;
2242 case 'b':
2243 case 'p': {
2244 struct got_object_id *id;
2245 id = get_selected_commit_id(s->blame.lines,
2246 s->first_displayed_line, s->selected_line);
2247 if (id == NULL || got_object_id_cmp(id,
2248 s->blamed_commit->id) == 0)
2249 break;
2250 err = open_selected_commit(&pobj, &obj,
2251 s->blame.lines, s->first_displayed_line,
2252 s->selected_line, s->repo);
2253 if (err)
2254 break;
2255 if (pobj == NULL && obj == NULL)
2256 break;
2257 if (ch == 'p' && pobj == NULL)
2258 break;
2259 s->done = 1;
2260 if (pthread_mutex_unlock(&s->mutex) != 0) {
2261 err = got_error_from_errno();
2262 goto done;
2264 thread_err = stop_blame(&s->blame);
2265 s->done = 0;
2266 if (pthread_mutex_lock(&s->mutex) != 0) {
2267 err = got_error_from_errno();
2268 goto done;
2270 if (thread_err)
2271 break;
2272 id = got_object_get_id(ch == 'b' ? obj : pobj);
2273 got_object_close(obj);
2274 obj = NULL;
2275 if (pobj) {
2276 got_object_close(pobj);
2277 pobj = NULL;
2279 err = got_object_qid_alloc(&s->blamed_commit, id);
2280 if (err)
2281 goto done;
2282 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2283 s->blamed_commit, entry);
2284 err = run_blame(&s->blame, &s->mutex, view,
2285 &s->blame_complete,
2286 &s->first_displayed_line,
2287 &s->last_displayed_line,
2288 &s->selected_line, &s->done, &s->eof,
2289 s->path, s->blamed_commit->id, s->repo);
2290 if (err)
2291 break;
2292 break;
2294 case 'B': {
2295 struct got_object_qid *first;
2296 first = SIMPLEQ_FIRST(&s->blamed_commits);
2297 if (!got_object_id_cmp(first->id, s->commit_id))
2298 break;
2299 s->done = 1;
2300 if (pthread_mutex_unlock(&s->mutex) != 0) {
2301 err = got_error_from_errno();
2302 goto done;
2304 thread_err = stop_blame(&s->blame);
2305 s->done = 0;
2306 if (pthread_mutex_lock(&s->mutex) != 0) {
2307 err = got_error_from_errno();
2308 goto done;
2310 if (thread_err)
2311 break;
2312 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2313 got_object_qid_free(s->blamed_commit);
2314 s->blamed_commit =
2315 SIMPLEQ_FIRST(&s->blamed_commits);
2316 err = run_blame(&s->blame, &s->mutex, view,
2317 &s->blame_complete,
2318 &s->first_displayed_line,
2319 &s->last_displayed_line,
2320 &s->selected_line, &s->done, &s->eof, s->path,
2321 s->blamed_commit->id, s->repo);
2322 if (err)
2323 break;
2324 break;
2326 case KEY_ENTER:
2327 case '\r':
2328 err = open_selected_commit(&pobj, &obj,
2329 s->blame.lines, s->first_displayed_line,
2330 s->selected_line, s->repo);
2331 if (err)
2332 break;
2333 if (pobj == NULL && obj == NULL)
2334 break;
2335 diff_view = view_open(0, 0, 0, 0, view,
2336 TOG_VIEW_DIFF);
2337 if (diff_view == NULL) {
2338 err = got_error_from_errno();
2339 break;
2341 err = open_diff_view(diff_view, pobj, obj,
2342 s->repo);
2343 if (err) {
2344 view_close(diff_view);
2345 break;
2347 *new_view = diff_view;
2348 if (pobj) {
2349 got_object_close(pobj);
2350 pobj = NULL;
2352 got_object_close(obj);
2353 obj = NULL;
2354 if (err)
2355 break;
2356 break;
2357 case KEY_NPAGE:
2358 case ' ':
2359 if (s->last_displayed_line >= s->blame.nlines &&
2360 s->selected_line < view->nlines - 2) {
2361 s->selected_line = MIN(s->blame.nlines,
2362 view->nlines - 2);
2363 break;
2365 if (s->last_displayed_line + view->nlines - 2
2366 <= s->blame.nlines)
2367 s->first_displayed_line +=
2368 view->nlines - 2;
2369 else
2370 s->first_displayed_line =
2371 s->blame.nlines -
2372 (view->nlines - 3);
2373 break;
2374 case KEY_RESIZE:
2375 if (s->selected_line > view->nlines - 2) {
2376 s->selected_line = MIN(s->blame.nlines,
2377 view->nlines - 2);
2379 break;
2380 default:
2381 break;
2384 if (pthread_mutex_unlock(&s->mutex) != 0)
2385 err = got_error_from_errno();
2386 done:
2387 if (pobj)
2388 got_object_close(pobj);
2389 return thread_err ? thread_err : err;
2392 static const struct got_error *
2393 cmd_blame(int argc, char *argv[])
2395 const struct got_error *error;
2396 struct got_repository *repo = NULL;
2397 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2398 struct got_object_id *commit_id = NULL;
2399 char *commit_id_str = NULL;
2400 int ch;
2401 struct tog_view *view;
2403 #ifndef PROFILE
2404 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2405 == -1)
2406 err(1, "pledge");
2407 #endif
2409 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2410 switch (ch) {
2411 case 'c':
2412 commit_id_str = optarg;
2413 break;
2414 case 'r':
2415 repo_path = realpath(optarg, NULL);
2416 if (repo_path == NULL)
2417 err(1, "-r option");
2418 break;
2419 default:
2420 usage();
2421 /* NOTREACHED */
2425 argc -= optind;
2426 argv += optind;
2428 if (argc == 1)
2429 path = argv[0];
2430 else
2431 usage_blame();
2433 cwd = getcwd(NULL, 0);
2434 if (cwd == NULL) {
2435 error = got_error_from_errno();
2436 goto done;
2438 if (repo_path == NULL) {
2439 repo_path = strdup(cwd);
2440 if (repo_path == NULL) {
2441 error = got_error_from_errno();
2442 goto done;
2447 error = got_repo_open(&repo, repo_path);
2448 if (error != NULL)
2449 return error;
2451 error = got_repo_map_path(&in_repo_path, repo, path);
2452 if (error != NULL)
2453 goto done;
2455 if (commit_id_str == NULL) {
2456 struct got_reference *head_ref;
2457 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2458 if (error != NULL)
2459 goto done;
2460 error = got_ref_resolve(&commit_id, repo, head_ref);
2461 got_ref_close(head_ref);
2462 } else {
2463 struct got_object *obj;
2464 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2465 if (error != NULL)
2466 goto done;
2467 commit_id = got_object_id_dup(got_object_get_id(obj));
2468 if (commit_id == NULL)
2469 error = got_error_from_errno();
2470 got_object_close(obj);
2472 if (error != NULL)
2473 goto done;
2475 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2476 if (view == NULL) {
2477 error = got_error_from_errno();
2478 goto done;
2480 error = open_blame_view(view, in_repo_path, commit_id, repo);
2481 if (error)
2482 goto done;
2483 error = view_loop(view);
2484 done:
2485 free(repo_path);
2486 free(cwd);
2487 free(commit_id);
2488 if (repo)
2489 got_repo_close(repo);
2490 return error;
2493 static const struct got_error *
2494 draw_tree_entries(struct tog_view *view,
2495 struct got_tree_entry **first_displayed_entry,
2496 struct got_tree_entry **last_displayed_entry,
2497 struct got_tree_entry **selected_entry, int *ndisplayed,
2498 const char *label, int show_ids, const char *parent_path,
2499 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2501 const struct got_error *err = NULL;
2502 struct got_tree_entry *te;
2503 wchar_t *wline;
2504 int width, n;
2506 *ndisplayed = 0;
2508 werase(view->window);
2510 if (limit == 0)
2511 return NULL;
2513 err = format_line(&wline, &width, label, view->ncols);
2514 if (err)
2515 return err;
2516 if (view_needs_focus_indication(view))
2517 wstandout(view->window);
2518 waddwstr(view->window, wline);
2519 if (view_needs_focus_indication(view))
2520 wstandend(view->window);
2521 free(wline);
2522 wline = NULL;
2523 if (width < view->ncols)
2524 waddch(view->window, '\n');
2525 if (--limit <= 0)
2526 return NULL;
2527 err = format_line(&wline, &width, parent_path, view->ncols);
2528 if (err)
2529 return err;
2530 waddwstr(view->window, wline);
2531 free(wline);
2532 wline = NULL;
2533 if (width < view->ncols)
2534 waddch(view->window, '\n');
2535 if (--limit <= 0)
2536 return NULL;
2537 waddch(view->window, '\n');
2538 if (--limit <= 0)
2539 return NULL;
2541 te = SIMPLEQ_FIRST(&entries->head);
2542 if (*first_displayed_entry == NULL) {
2543 if (selected == 0) {
2544 wstandout(view->window);
2545 *selected_entry = NULL;
2547 waddstr(view->window, " ..\n"); /* parent directory */
2548 if (selected == 0)
2549 wstandend(view->window);
2550 (*ndisplayed)++;
2551 if (--limit <= 0)
2552 return NULL;
2553 n = 1;
2554 } else {
2555 n = 0;
2556 while (te != *first_displayed_entry)
2557 te = SIMPLEQ_NEXT(te, entry);
2560 while (te) {
2561 char *line = NULL, *id_str = NULL;
2563 if (show_ids) {
2564 err = got_object_id_str(&id_str, te->id);
2565 if (err)
2566 return got_error_from_errno();
2568 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2569 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2570 free(id_str);
2571 return got_error_from_errno();
2573 free(id_str);
2574 err = format_line(&wline, &width, line, view->ncols);
2575 if (err) {
2576 free(line);
2577 break;
2579 if (n == selected) {
2580 wstandout(view->window);
2581 *selected_entry = te;
2583 waddwstr(view->window, wline);
2584 if (width < view->ncols)
2585 waddch(view->window, '\n');
2586 if (n == selected)
2587 wstandend(view->window);
2588 free(line);
2589 free(wline);
2590 wline = NULL;
2591 n++;
2592 (*ndisplayed)++;
2593 *last_displayed_entry = te;
2594 if (--limit <= 0)
2595 break;
2596 te = SIMPLEQ_NEXT(te, entry);
2599 view_vborder(view);
2600 return err;
2603 static void
2604 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2605 const struct got_tree_entries *entries, int isroot)
2607 struct got_tree_entry *te, *prev;
2608 int i;
2610 if (*first_displayed_entry == NULL)
2611 return;
2613 te = SIMPLEQ_FIRST(&entries->head);
2614 if (*first_displayed_entry == te) {
2615 if (!isroot)
2616 *first_displayed_entry = NULL;
2617 return;
2620 /* XXX this is stupid... switch to TAILQ? */
2621 for (i = 0; i < maxscroll; i++) {
2622 while (te != *first_displayed_entry) {
2623 prev = te;
2624 te = SIMPLEQ_NEXT(te, entry);
2626 *first_displayed_entry = prev;
2627 te = SIMPLEQ_FIRST(&entries->head);
2629 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2630 *first_displayed_entry = NULL;
2633 static void
2634 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2635 struct got_tree_entry *last_displayed_entry,
2636 const struct got_tree_entries *entries)
2638 struct got_tree_entry *next;
2639 int n = 0;
2641 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2642 return;
2644 if (*first_displayed_entry)
2645 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2646 else
2647 next = SIMPLEQ_FIRST(&entries->head);
2648 while (next) {
2649 *first_displayed_entry = next;
2650 if (++n >= maxscroll)
2651 break;
2652 next = SIMPLEQ_NEXT(next, entry);
2656 static const struct got_error *
2657 tree_entry_path(char **path, struct tog_parent_trees *parents,
2658 struct got_tree_entry *te)
2660 const struct got_error *err = NULL;
2661 struct tog_parent_tree *pt;
2662 size_t len = 2; /* for leading slash and NUL */
2664 TAILQ_FOREACH(pt, parents, entry)
2665 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2666 if (te)
2667 len += strlen(te->name);
2669 *path = calloc(1, len);
2670 if (path == NULL)
2671 return got_error_from_errno();
2673 (*path)[0] = '/';
2674 pt = TAILQ_LAST(parents, tog_parent_trees);
2675 while (pt) {
2676 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2677 err = got_error(GOT_ERR_NO_SPACE);
2678 goto done;
2680 if (strlcat(*path, "/", len) >= len) {
2681 err = got_error(GOT_ERR_NO_SPACE);
2682 goto done;
2684 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2686 if (te) {
2687 if (strlcat(*path, te->name, len) >= len) {
2688 err = got_error(GOT_ERR_NO_SPACE);
2689 goto done;
2692 done:
2693 if (err) {
2694 free(*path);
2695 *path = NULL;
2697 return err;
2700 static const struct got_error *
2701 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2702 struct got_tree_entry *te, struct tog_parent_trees *parents,
2703 struct got_object_id *commit_id, struct got_repository *repo)
2705 const struct got_error *err = NULL;
2706 char *path;
2707 struct tog_view *blame_view;
2709 err = tree_entry_path(&path, parents, te);
2710 if (err)
2711 return err;
2713 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2714 if (blame_view == NULL)
2715 return got_error_from_errno();
2717 err = open_blame_view(blame_view, path, commit_id, repo);
2718 if (err) {
2719 view_close(blame_view);
2720 free(path);
2721 } else
2722 *new_view = blame_view;
2723 return err;
2726 static const struct got_error *
2727 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2728 struct got_tree_entry *te, struct tog_parent_trees *parents,
2729 struct got_object_id *commit_id, struct got_repository *repo)
2731 struct tog_view *log_view;
2732 const struct got_error *err = NULL;
2733 char *path;
2735 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2736 if (log_view == NULL)
2737 return got_error_from_errno();
2739 err = tree_entry_path(&path, parents, te);
2740 if (err)
2741 return err;
2743 err = open_log_view(log_view, commit_id, repo, path);
2744 if (err)
2745 view_close(log_view);
2746 else
2747 *new_view = log_view;
2748 free(path);
2749 return err;
2752 static const struct got_error *
2753 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2754 struct got_object_id *commit_id, struct got_repository *repo)
2756 const struct got_error *err = NULL;
2757 char *commit_id_str = NULL;
2758 struct tog_tree_view_state *s = &view->state.tree;
2760 TAILQ_INIT(&s->parents);
2762 err = got_object_id_str(&commit_id_str, commit_id);
2763 if (err != NULL)
2764 goto done;
2766 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2767 err = got_error_from_errno();
2768 goto done;
2771 s->root = s->tree = root;
2772 s->entries = got_object_tree_get_entries(root);
2773 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2774 s->commit_id = commit_id;
2775 s->repo = repo;
2777 view->show = show_tree_view;
2778 view->input = input_tree_view;
2779 view->close = close_tree_view;
2780 done:
2781 free(commit_id_str);
2782 if (err)
2783 free(s->tree_label);
2784 return err;
2787 static const struct got_error *
2788 close_tree_view(struct tog_view *view)
2790 struct tog_tree_view_state *s = &view->state.tree;
2792 free(s->tree_label);
2793 while (!TAILQ_EMPTY(&s->parents)) {
2794 struct tog_parent_tree *parent;
2795 parent = TAILQ_FIRST(&s->parents);
2796 TAILQ_REMOVE(&s->parents, parent, entry);
2797 free(parent);
2800 if (s->tree != s->root)
2801 got_object_tree_close(s->tree);
2802 got_object_tree_close(s->root);
2804 return NULL;
2807 static const struct got_error *
2808 show_tree_view(struct tog_view *view)
2810 const struct got_error *err = NULL;
2811 struct tog_tree_view_state *s = &view->state.tree;
2812 char *parent_path;
2814 err = tree_entry_path(&parent_path, &s->parents, NULL);
2815 if (err)
2816 return err;
2818 err = draw_tree_entries(view, &s->first_displayed_entry,
2819 &s->last_displayed_entry, &s->selected_entry,
2820 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2821 s->entries, s->selected, view->nlines, s->tree == s->root);
2822 free(parent_path);
2823 return err;
2826 static const struct got_error *
2827 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2828 struct tog_view *view, int ch)
2830 const struct got_error *err = NULL;
2831 struct tog_tree_view_state *s = &view->state.tree;
2833 switch (ch) {
2834 case 'i':
2835 s->show_ids = !s->show_ids;
2836 break;
2837 case 'l':
2838 if (s->selected_entry) {
2839 err = log_tree_entry(new_view, view,
2840 s->selected_entry, &s->parents,
2841 s->commit_id, s->repo);
2843 break;
2844 case 'k':
2845 case KEY_UP:
2846 if (s->selected > 0)
2847 s->selected--;
2848 if (s->selected > 0)
2849 break;
2850 tree_scroll_up(&s->first_displayed_entry, 1,
2851 s->entries, s->tree == s->root);
2852 break;
2853 case KEY_PPAGE:
2854 if (SIMPLEQ_FIRST(&s->entries->head) ==
2855 s->first_displayed_entry) {
2856 if (s->tree != s->root)
2857 s->first_displayed_entry = NULL;
2858 s->selected = 0;
2859 break;
2861 tree_scroll_up(&s->first_displayed_entry,
2862 view->nlines, s->entries,
2863 s->tree == s->root);
2864 break;
2865 case 'j':
2866 case KEY_DOWN:
2867 if (s->selected < s->ndisplayed - 1) {
2868 s->selected++;
2869 break;
2871 tree_scroll_down(&s->first_displayed_entry, 1,
2872 s->last_displayed_entry, s->entries);
2873 break;
2874 case KEY_NPAGE:
2875 tree_scroll_down(&s->first_displayed_entry,
2876 view->nlines, s->last_displayed_entry,
2877 s->entries);
2878 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2879 entry))
2880 break;
2881 /* can't scroll any further; move cursor down */
2882 if (s->selected < s->ndisplayed - 1)
2883 s->selected = s->ndisplayed - 1;
2884 break;
2885 case KEY_ENTER:
2886 case '\r':
2887 if (s->selected_entry == NULL) {
2888 struct tog_parent_tree *parent;
2889 case KEY_LEFT:
2890 /* user selected '..' */
2891 if (s->tree == s->root)
2892 break;
2893 parent = TAILQ_FIRST(&s->parents);
2894 TAILQ_REMOVE(&s->parents, parent,
2895 entry);
2896 got_object_tree_close(s->tree);
2897 s->tree = parent->tree;
2898 s->entries =
2899 got_object_tree_get_entries(s->tree);
2900 s->first_displayed_entry =
2901 parent->first_displayed_entry;
2902 s->selected_entry =
2903 parent->selected_entry;
2904 s->selected = parent->selected;
2905 free(parent);
2906 } else if (S_ISDIR(s->selected_entry->mode)) {
2907 struct tog_parent_tree *parent;
2908 struct got_tree_object *child;
2909 err = got_object_open_as_tree(&child,
2910 s->repo, s->selected_entry->id);
2911 if (err)
2912 break;
2913 parent = calloc(1, sizeof(*parent));
2914 if (parent == NULL) {
2915 err = got_error_from_errno();
2916 break;
2918 parent->tree = s->tree;
2919 parent->first_displayed_entry =
2920 s->first_displayed_entry;
2921 parent->selected_entry = s->selected_entry;
2922 parent->selected = s->selected;
2923 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2924 s->tree = child;
2925 s->entries =
2926 got_object_tree_get_entries(s->tree);
2927 s->selected = 0;
2928 s->first_displayed_entry = NULL;
2929 } else if (S_ISREG(s->selected_entry->mode)) {
2930 err = blame_tree_entry(new_view, view,
2931 s->selected_entry, &s->parents,
2932 s->commit_id, s->repo);
2933 if (err)
2934 break;
2936 break;
2937 case KEY_RESIZE:
2938 if (s->selected > view->nlines)
2939 s->selected = s->ndisplayed - 1;
2940 break;
2941 default:
2942 break;
2945 return err;
2948 __dead static void
2949 usage_tree(void)
2951 endwin();
2952 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2953 getprogname());
2954 exit(1);
2957 static const struct got_error *
2958 cmd_tree(int argc, char *argv[])
2960 const struct got_error *error;
2961 struct got_repository *repo = NULL;
2962 char *repo_path = NULL;
2963 struct got_object_id *commit_id = NULL;
2964 char *commit_id_arg = NULL;
2965 struct got_commit_object *commit = NULL;
2966 struct got_tree_object *tree = NULL;
2967 int ch;
2968 struct tog_view *view;
2970 #ifndef PROFILE
2971 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2972 == -1)
2973 err(1, "pledge");
2974 #endif
2976 while ((ch = getopt(argc, argv, "c:")) != -1) {
2977 switch (ch) {
2978 case 'c':
2979 commit_id_arg = optarg;
2980 break;
2981 default:
2982 usage();
2983 /* NOTREACHED */
2987 argc -= optind;
2988 argv += optind;
2990 if (argc == 0) {
2991 repo_path = getcwd(NULL, 0);
2992 if (repo_path == NULL)
2993 return got_error_from_errno();
2994 } else if (argc == 1) {
2995 repo_path = realpath(argv[0], NULL);
2996 if (repo_path == NULL)
2997 return got_error_from_errno();
2998 } else
2999 usage_log();
3001 error = got_repo_open(&repo, repo_path);
3002 free(repo_path);
3003 if (error != NULL)
3004 return error;
3006 if (commit_id_arg == NULL) {
3007 error = get_head_commit_id(&commit_id, repo);
3008 if (error != NULL)
3009 goto done;
3010 } else {
3011 struct got_object *obj;
3012 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3013 if (error == NULL) {
3014 commit_id = got_object_id_dup(got_object_get_id(obj));
3015 if (commit_id == NULL)
3016 error = got_error_from_errno();
3019 if (error != NULL)
3020 goto done;
3022 error = got_object_open_as_commit(&commit, repo, commit_id);
3023 if (error != NULL)
3024 goto done;
3026 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3027 if (error != NULL)
3028 goto done;
3030 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3031 if (view == NULL) {
3032 error = got_error_from_errno();
3033 goto done;
3035 error = open_tree_view(view, tree, commit_id, repo);
3036 if (error)
3037 goto done;
3038 error = view_loop(view);
3039 done:
3040 free(commit_id);
3041 if (commit)
3042 got_object_commit_close(commit);
3043 if (tree)
3044 got_object_tree_close(tree);
3045 if (repo)
3046 got_repo_close(repo);
3047 return error;
3049 static void
3050 init_curses(void)
3052 initscr();
3053 cbreak();
3054 noecho();
3055 nonl();
3056 intrflush(stdscr, FALSE);
3057 keypad(stdscr, TRUE);
3058 curs_set(0);
3061 __dead static void
3062 usage(void)
3064 int i;
3066 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3067 "Available commands:\n", getprogname());
3068 for (i = 0; i < nitems(tog_commands); i++) {
3069 struct tog_cmd *cmd = &tog_commands[i];
3070 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3072 exit(1);
3075 static char **
3076 make_argv(const char *arg0, const char *arg1)
3078 char **argv;
3079 int argc = (arg1 == NULL ? 1 : 2);
3081 argv = calloc(argc, sizeof(char *));
3082 if (argv == NULL)
3083 err(1, "calloc");
3084 argv[0] = strdup(arg0);
3085 if (argv[0] == NULL)
3086 err(1, "calloc");
3087 if (arg1) {
3088 argv[1] = strdup(arg1);
3089 if (argv[1] == NULL)
3090 err(1, "calloc");
3093 return argv;
3096 int
3097 main(int argc, char *argv[])
3099 const struct got_error *error = NULL;
3100 struct tog_cmd *cmd = NULL;
3101 int ch, hflag = 0;
3102 char **cmd_argv = NULL;
3104 setlocale(LC_ALL, "");
3106 while ((ch = getopt(argc, argv, "h")) != -1) {
3107 switch (ch) {
3108 case 'h':
3109 hflag = 1;
3110 break;
3111 default:
3112 usage();
3113 /* NOTREACHED */
3117 argc -= optind;
3118 argv += optind;
3119 optind = 0;
3120 optreset = 1;
3122 if (argc == 0) {
3123 if (hflag)
3124 usage();
3125 /* Build an argument vector which runs a default command. */
3126 cmd = &tog_commands[0];
3127 cmd_argv = make_argv(cmd->name, NULL);
3128 argc = 1;
3129 } else {
3130 int i;
3132 /* Did the user specific a command? */
3133 for (i = 0; i < nitems(tog_commands); i++) {
3134 if (strncmp(tog_commands[i].name, argv[0],
3135 strlen(argv[0])) == 0) {
3136 cmd = &tog_commands[i];
3137 if (hflag)
3138 tog_commands[i].cmd_usage();
3139 break;
3142 if (cmd == NULL) {
3143 /* Did the user specify a repository? */
3144 char *repo_path = realpath(argv[0], NULL);
3145 if (repo_path) {
3146 struct got_repository *repo;
3147 error = got_repo_open(&repo, repo_path);
3148 if (error == NULL)
3149 got_repo_close(repo);
3150 } else
3151 error = got_error_from_errno();
3152 if (error) {
3153 if (hflag) {
3154 fprintf(stderr, "%s: '%s' is not a "
3155 "known command\n", getprogname(),
3156 argv[0]);
3157 usage();
3159 fprintf(stderr, "%s: '%s' is neither a known "
3160 "command nor a path to a repository\n",
3161 getprogname(), argv[0]);
3162 free(repo_path);
3163 return 1;
3165 cmd = &tog_commands[0];
3166 cmd_argv = make_argv(cmd->name, repo_path);
3167 argc = 2;
3168 free(repo_path);
3172 init_curses();
3174 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3175 if (error)
3176 goto done;
3177 done:
3178 endwin();
3179 free(cmd_argv);
3180 if (error)
3181 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3182 return 0;